Hex Artifact Content
Not logged in

Artifact baf46b9bddb2ed86ab395d5cb648b5d012c02900:

File src/sqlite3.c part of check-in [0baf4be679] - Update the built-in SQLite sources to version 3.6.12. by drh on 2009-03-31 12:42:49.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 31 32 2e 20 20  ersion 3.6.12.  
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 35 34 38 37 20 6c 69 6e 65  rst.** 5487 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 33 2d 33 31 20 31 32 3a 31   2009-03-31 12:1
04c0: 39 3a 35 39 20 55 54 43 2e 0a 2a 2f 0a 23 64 65  9:59 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 34 38 20 32 30 30 39 2f 30 33 2f 32 35 20  .848 2009/03/25 
0790: 31 36 3a 35 31 3a 34 33 20 64 72 68 20 45 78 70  16:51:43 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 33 36 20 32 30 30 39 2f 30 33 2f 32   1.436 2009/03/2
4970: 30 20 31 33 3a 31 35 3a 33 30 20 64 72 68 20 45  0 13:15:30 drh E
4980: 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
4990: 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66  _SQLITE3_H_.#def
49a0: 69 6e 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a  ine _SQLITE3_H_.
49b0: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67  #include <stdarg
49c0: 2e 68 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65  .h>     /* Neede
49d0: 64 20 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69  d for the defini
49e0: 74 69 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20  tion of va_list 
49f0: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  */../*.** Make s
4a00: 75 72 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20  ure we can call 
4a10: 74 68 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20  this stuff from 
4a20: 43 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78  C++..*/.#if 0.ex
4a30: 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69  tern "C" {.#endi
4a40: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68  f.../*.** Add th
4a50: 65 20 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65  e ability to ove
4a60: 72 72 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a  rride 'extern'.*
4a70: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
4a80: 5f 45 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65  _EXTERN.# define
4a90: 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65   SQLITE_EXTERN e
4aa0: 78 74 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 2f 2a  xtern.#endif../*
4ab0: 0a 2a 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20  .** These no-op 
4ac0: 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20  macros are used 
4ad0: 69 6e 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65  in front of inte
4ae0: 72 66 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74  rfaces to mark t
4af0: 68 6f 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63  hose.** interfac
4b00: 65 73 20 61 73 20 65 69 74 68 65 72 20 64 65 70  es as either dep
4b10: 72 65 63 61 74 65 64 20 6f 72 20 65 78 70 65 72  recated or exper
4b20: 69 6d 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70  imental.  New ap
4b30: 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68  plications.** sh
4b40: 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70  ould not use dep
4b50: 72 65 63 61 74 65 64 20 69 6e 74 72 66 61 63 65  recated intrface
4b60: 73 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70  s - they are sup
4b70: 70 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72  port for backwar
4b80: 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c  ds.** compatibil
4b90: 69 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69  ity only.  Appli
4ba0: 63 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73  cation writers s
4bb0: 68 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74  hould be aware t
4bc0: 68 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e  hat.** experimen
4bd0: 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61  tal interfaces a
4be0: 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  re subject to ch
4bf0: 61 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65  ange in point re
4c00: 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  leases..**.** Th
4c10: 65 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20  ese macros used 
4c20: 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61  to resolve to va
4c30: 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63  rious kinds of c
4c40: 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68  ompiler magic th
4c50: 61 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65  at.** would gene
4c60: 72 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73  rate warning mes
4c70: 73 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20  sages when they 
4c80: 77 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20  were used.  But 
4c90: 74 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72  that.** compiler
4ca0: 20 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20   magic ended up 
4cb0: 67 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20  generating such 
4cc0: 61 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20  a flurry of bug 
4cd0: 72 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20  reports.** that 
4ce0: 77 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74  we have taken it
4cf0: 20 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e   all out and gon
4d00: 65 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20  e back to using 
4d10: 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d  simple.** noop m
4d20: 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  acros..*/.#defin
4d30: 65 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41  e SQLITE_DEPRECA
4d40: 54 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  TED.#define SQLI
4d50: 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  TE_EXPERIMENTAL.
4d60: 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68  ./*.** Ensure th
4d70: 65 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65  ese symbols were
4d80: 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20   not defined by 
4d90: 73 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65  some previous he
4da0: 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69  ader file..*/.#i
4db0: 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53  fdef SQLITE_VERS
4dc0: 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  ION.# undef SQLI
4dd0: 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69  TE_VERSION.#endi
4de0: 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
4df0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23  VERSION_NUMBER.#
4e00: 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45   undef SQLITE_VE
4e10: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e  RSION_NUMBER.#en
4e20: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  dif../*.** CAPI3
4e30: 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d  REF: Compile-Tim
4e40: 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f  e Library Versio
4e50: 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31  n Numbers {H1001
4e60: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S60100>.**.*
4e70: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52  * The SQLITE_VER
4e80: 53 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f  SION and SQLITE_
4e90: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23  VERSION_NUMBER #
4ea0: 64 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68  defines in.** th
4eb0: 65 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65  e sqlite3.h file
4ec0: 20 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72   specify the ver
4ed0: 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77  sion of SQLite w
4ee0: 69 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61  ith which.** tha
4ef0: 74 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73  t header file is
4f00: 20 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a   associated..**.
4f10: 2a 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22  ** The "version"
4f20: 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20   of SQLite is a 
4f30: 73 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f  string of the fo
4f40: 72 6d 20 22 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54  rm "X.Y.Z"..** T
4f50: 68 65 20 70 68 72 61 73 65 20 22 61 6c 70 68 61  he phrase "alpha
4f60: 22 20 6f 72 20 22 62 65 74 61 22 20 6d 69 67 68  " or "beta" migh
4f70: 74 20 62 65 20 61 70 70 65 6e 64 65 64 20 61 66  t be appended af
4f80: 74 65 72 20 74 68 65 20 5a 2e 0a 2a 2a 20 54 68  ter the Z..** Th
4f90: 65 20 58 20 76 61 6c 75 65 20 69 73 20 6d 61 6a  e X value is maj
4fa0: 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  or version numbe
4fb0: 72 20 61 6c 77 61 79 73 20 33 20 69 6e 20 53 51  r always 3 in SQ
4fc0: 4c 69 74 65 33 2e 0a 2a 2a 20 54 68 65 20 58 20  Lite3..** The X 
4fd0: 76 61 6c 75 65 20 6f 6e 6c 79 20 63 68 61 6e 67  value only chang
4fe0: 65 73 20 77 68 65 6e 20 62 61 63 6b 77 61 72 64  es when backward
4ff0: 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  s compatibility 
5000: 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e 20 61 6e 64  is.** broken and
5010: 20 77 65 20 69 6e 74 65 6e 64 20 74 6f 20 6e 65   we intend to ne
5020: 76 65 72 20 62 72 65 61 6b 20 62 61 63 6b 77 61  ver break backwa
5030: 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  rds compatibilit
5040: 79 2e 0a 2a 2a 20 54 68 65 20 59 20 76 61 6c 75  y..** The Y valu
5050: 65 20 69 73 20 74 68 65 20 6d 69 6e 6f 72 20 76  e is the minor v
5060: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e  ersion number an
5070: 64 20 6f 6e 6c 79 20 63 68 61 6e 67 65 73 20 77  d only changes w
5080: 68 65 6e 0a 2a 2a 20 74 68 65 72 65 20 61 72 65  hen.** there are
5090: 20 6d 61 6a 6f 72 20 66 65 61 74 75 72 65 20 65   major feature e
50a0: 6e 68 61 6e 63 65 6d 65 6e 74 73 20 74 68 61 74  nhancements that
50b0: 20 61 72 65 20 66 6f 72 77 61 72 64 73 20 63 6f   are forwards co
50c0: 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 62 75 74 20  mpatible.** but 
50d0: 6e 6f 74 20 62 61 63 6b 77 61 72 64 73 20 63 6f  not backwards co
50e0: 6d 70 61 74 69 62 6c 65 2e 0a 2a 2a 20 54 68 65  mpatible..** The
50f0: 20 5a 20 76 61 6c 75 65 20 69 73 20 74 68 65 20   Z value is the 
5100: 72 65 6c 65 61 73 65 20 6e 75 6d 62 65 72 20 61  release number a
5110: 6e 64 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  nd is incremente
5120: 64 20 77 69 74 68 0a 2a 2a 20 65 61 63 68 20 72  d with.** each r
5130: 65 6c 65 61 73 65 20 62 75 74 20 72 65 73 65 74  elease but reset
5140: 73 20 62 61 63 6b 20 74 6f 20 30 20 77 68 65 6e  s back to 0 when
5150: 65 76 65 72 20 59 20 69 73 20 69 6e 63 72 65 6d  ever Y is increm
5160: 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  ented..**.** See
5170: 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
5180: 6c 69 62 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e  libversion()] an
5190: 64 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  d [sqlite3_libve
51a0: 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d 2e  rsion_number()].
51b0: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
51c0: 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b 48  nts: [H10011] [H
51d0: 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 6e  10014].*/.#defin
51e0: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  e SQLITE_VERSION
51f0: 20 20 20 20 20 20 20 20 20 22 33 2e 36 2e 31 32           "3.6.12
5200: 22 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ".#define SQLITE
5210: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20  _VERSION_NUMBER 
5220: 20 33 30 30 36 30 31 32 0a 0a 2f 2a 0a 2a 2a 20   3006012../*.** 
5230: 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69  CAPI3REF: Run-Ti
5240: 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69  me Library Versi
5250: 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30  on Numbers {H100
5260: 32 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 20  20} <S60100>.** 
5270: 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65  KEYWORDS: sqlite
5280: 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20  3_version.**.** 
5290: 54 68 65 73 65 20 66 65 61 74 75 72 65 73 20 70  These features p
52a0: 72 6f 76 69 64 65 20 74 68 65 20 73 61 6d 65 20  rovide the same 
52b0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74  information as t
52c0: 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49  he [SQLITE_VERSI
52d0: 4f 4e 5d 0a 2a 2a 20 61 6e 64 20 5b 53 51 4c 49  ON].** and [SQLI
52e0: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45  TE_VERSION_NUMBE
52f0: 52 5d 20 23 64 65 66 69 6e 65 73 20 69 6e 20 74  R] #defines in t
5300: 68 65 20 68 65 61 64 65 72 2c 20 62 75 74 20 61  he header, but a
5310: 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  re associated.**
5320: 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72   with the librar
5330: 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  y instead of the
5340: 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20 20 43   header file.  C
5350: 61 75 74 69 6f 75 73 20 70 72 6f 67 72 61 6d 6d  autious programm
5360: 65 72 73 20 6d 69 67 68 74 0a 2a 2a 20 69 6e 63  ers might.** inc
5370: 6c 75 64 65 20 61 20 63 68 65 63 6b 20 69 6e 20  lude a check in 
5380: 74 68 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f  their applicatio
5390: 6e 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  n to verify that
53a0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  .** sqlite3_libv
53b0: 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20  ersion_number() 
53c0: 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 74  always returns t
53d0: 68 65 20 76 61 6c 75 65 0a 2a 2a 20 5b 53 51 4c  he value.** [SQL
53e0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42  ITE_VERSION_NUMB
53f0: 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ER]..**.** The s
5400: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
5410: 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  n() function ret
5420: 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 69 6e  urns the same in
5430: 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 69 73 0a  formation as is.
5440: 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ** in the sqlite
5450: 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69  3_version[] stri
5460: 6e 67 20 63 6f 6e 73 74 61 6e 74 2e 20 20 54 68  ng constant.  Th
5470: 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 72  e function is pr
5480: 6f 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20 75 73  ovided.** for us
5490: 65 20 69 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20  e in DLLs since 
54a0: 44 4c 4c 20 75 73 65 72 73 20 75 73 75 61 6c 6c  DLL users usuall
54b0: 79 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 64 69  y do not have di
54c0: 72 65 63 74 20 61 63 63 65 73 73 20 74 6f 20 73  rect access to s
54d0: 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 74 61 6e  tring.** constan
54e0: 74 73 20 77 69 74 68 69 6e 20 74 68 65 20 44 4c  ts within the DL
54f0: 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  L..**.** Require
5500: 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d 20  ments: [H10021] 
5510: 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32 33  [H10022] [H10023
5520: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
5530: 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
5540: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53  e3_version[] = S
5550: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53  QLITE_VERSION;.S
5560: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
5570: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69  char *sqlite3_li
5580: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a  bversion(void);.
5590: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
55a0: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
55b0: 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a  n_number(void);.
55c0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
55d0: 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20   Test To See If 
55e0: 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54  The Library Is T
55f0: 68 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30  hreadsafe {H1010
5600: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S60100>.**.*
5610: 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20  * SQLite can be 
5620: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72  compiled with or
5630: 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73   without mutexes
5640: 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b  .  When.** the [
5650: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
5660: 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  E] C preprocesso
5670: 72 20 6d 61 63 72 6f 20 31 20 6f 72 20 32 2c 20  r macro 1 or 2, 
5680: 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65  mutexes.** are e
5690: 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74  nabled and SQLit
56a0: 65 20 69 73 20 74 68 72 65 61 64 73 61 66 65 2e  e is threadsafe.
56b0: 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53    When the.** [S
56c0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
56d0: 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a  ] macro is 0, .*
56e0: 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 72  * the mutexes ar
56f0: 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68  e omitted.  With
5700: 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c  out the mutexes,
5710: 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a   it is not safe.
5720: 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 74 65  ** to use SQLite
5730: 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66 72   concurrently fr
5740: 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  om more than one
5750: 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45   thread..**.** E
5760: 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 73 20  nabling mutexes 
5770: 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 72 61  incurs a measura
5780: 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ble performance 
5790: 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69  penalty..** So i
57a0: 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 75 74  f speed is of ut
57b0: 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c  most importance,
57c0: 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 65 20   it makes sense 
57d0: 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68  to disable.** th
57e0: 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 74 20  e mutexes.  But 
57f0: 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 66 65  for maximum safe
5800: 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 6f 75  ty, mutexes shou
5810: 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a  ld be enabled..*
5820: 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 65  * The default be
5830: 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75  havior is for mu
5840: 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62  texes to be enab
5850: 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  led..**.** This 
5860: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65  interface can be
5870: 20 75 73 65 64 20 62 79 20 61 20 70 72 6f 67 72   used by a progr
5880: 61 6d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  am to make sure 
5890: 74 68 61 74 20 74 68 65 0a 2a 2a 20 76 65 72 73  that the.** vers
58a0: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68  ion of SQLite th
58b0: 61 74 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67  at it is linking
58c0: 20 61 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d   against was com
58d0: 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68  piled with.** th
58e0: 65 20 64 65 73 69 72 65 64 20 73 65 74 74 69 6e  e desired settin
58f0: 67 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45  g of the [SQLITE
5900: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63  _THREADSAFE] mac
5910: 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ro..**.** This i
5920: 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79 20 72 65  nterface only re
5930: 70 6f 72 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d  ports on the com
5940: 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74 65 78 20  pile-time mutex 
5950: 73 65 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68  setting.** of th
5960: 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44  e [SQLITE_THREAD
5970: 53 41 46 45 5d 20 66 6c 61 67 2e 20 20 49 66 20  SAFE] flag.  If 
5980: 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
5990: 65 64 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54  ed with.** SQLIT
59a0: 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 20 74  E_THREADSAFE=1 t
59b0: 68 65 6e 20 6d 75 74 65 78 65 73 20 61 72 65 20  hen mutexes are 
59c0: 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  enabled by defau
59d0: 6c 74 20 62 75 74 0a 2a 2a 20 63 61 6e 20 62 65  lt but.** can be
59e0: 20 66 75 6c 6c 79 20 6f 72 20 70 61 72 74 69 61   fully or partia
59f0: 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 75 73 69  lly disabled usi
5a00: 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71  ng a call to [sq
5a10: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 0a  lite3_config()].
5a20: 2a 2a 20 77 69 74 68 20 74 68 65 20 76 65 72 62  ** with the verb
5a30: 73 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  s [SQLITE_CONFIG
5a40: 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 2c 20  _SINGLETHREAD], 
5a50: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
5a60: 55 4c 54 49 54 48 52 45 41 44 5d 2c 0a 2a 2a 20  ULTITHREAD],.** 
5a70: 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  or [SQLITE_CONFI
5a80: 47 5f 4d 55 54 45 58 5d 2e 20 20 54 68 65 20 72  G_MUTEX].  The r
5a90: 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
5aa0: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  his function sho
5ab0: 77 73 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 20 64  ws.** only the d
5ac0: 65 66 61 75 6c 74 20 63 6f 6d 70 69 6c 65 2d 74  efault compile-t
5ad0: 69 6d 65 20 73 65 74 74 69 6e 67 2c 20 6e 6f 74  ime setting, not
5ae0: 20 61 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63 68   any run-time ch
5af0: 61 6e 67 65 73 0a 2a 2a 20 74 6f 20 74 68 61 74  anges.** to that
5b00: 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   setting..**.** 
5b10: 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69  See the [threadi
5b20: 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e  ng mode] documen
5b30: 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74  tation for addit
5b40: 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
5b50: 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  n..**.** Require
5b60: 6d 65 6e 74 73 3a 20 5b 48 31 30 31 30 31 5d 20  ments: [H10101] 
5b70: 5b 48 31 30 31 30 32 5d 0a 2a 2f 0a 53 51 4c 49  [H10102].*/.SQLI
5b80: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
5b90: 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 76 6f  e3_threadsafe(vo
5ba0: 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  id);../*.** CAPI
5bb0: 33 52 45 46 3a 20 44 61 74 61 62 61 73 65 20 43  3REF: Database C
5bc0: 6f 6e 6e 65 63 74 69 6f 6e 20 48 61 6e 64 6c 65  onnection Handle
5bd0: 20 7b 48 31 32 30 30 30 7d 20 3c 53 34 30 32 30   {H12000} <S4020
5be0: 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
5bf0: 7b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  {database connec
5c00: 74 69 6f 6e 7d 20 7b 64 61 74 61 62 61 73 65 20  tion} {database 
5c10: 63 6f 6e 6e 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a  connections}.**.
5c20: 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 53 51 4c  ** Each open SQL
5c30: 69 74 65 20 64 61 74 61 62 61 73 65 20 69 73 20  ite database is 
5c40: 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61  represented by a
5c50: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
5c60: 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
5c70: 65 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75  e opaque structu
5c80: 72 65 20 6e 61 6d 65 64 20 22 73 71 6c 69 74 65  re named "sqlite
5c90: 33 22 2e 20 20 49 74 20 69 73 20 75 73 65 66 75  3".  It is usefu
5ca0: 6c 20 74 6f 20 74 68 69 6e 6b 20 6f 66 20 61 6e  l to think of an
5cb0: 20 73 71 6c 69 74 65 33 0a 2a 2a 20 70 6f 69 6e   sqlite3.** poin
5cc0: 74 65 72 20 61 73 20 61 6e 20 6f 62 6a 65 63 74  ter as an object
5cd0: 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  .  The [sqlite3_
5ce0: 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65  open()], [sqlite
5cf0: 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 61 6e 64  3_open16()], and
5d00: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  .** [sqlite3_ope
5d10: 6e 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63  n_v2()] interfac
5d20: 65 73 20 61 72 65 20 69 74 73 20 63 6f 6e 73 74  es are its const
5d30: 72 75 63 74 6f 72 73 2c 20 61 6e 64 20 5b 73 71  ructors, and [sq
5d40: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 0a 2a  lite3_close()].*
5d50: 2a 20 69 73 20 69 74 73 20 64 65 73 74 72 75 63  * is its destruc
5d60: 74 6f 72 2e 20 20 54 68 65 72 65 20 61 72 65 20  tor.  There are 
5d70: 6d 61 6e 79 20 6f 74 68 65 72 20 69 6e 74 65 72  many other inter
5d80: 66 61 63 65 73 20 28 73 75 63 68 20 61 73 0a 2a  faces (such as.*
5d90: 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
5da0: 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74  re_v2()], [sqlit
5db0: 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
5dc0: 6f 6e 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73  on()], and.** [s
5dd0: 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
5de0: 6f 75 74 28 29 5d 20 74 6f 20 6e 61 6d 65 20 62  out()] to name b
5df0: 75 74 20 74 68 72 65 65 29 20 74 68 61 74 20 61  ut three) that a
5e00: 72 65 20 6d 65 74 68 6f 64 73 20 6f 6e 20 61 6e  re methods on an
5e10: 0a 2a 2a 20 73 71 6c 69 74 65 33 20 6f 62 6a 65  .** sqlite3 obje
5e20: 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ct..*/.typedef s
5e30: 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 73 71  truct sqlite3 sq
5e40: 6c 69 74 65 33 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  lite3;../*.** CA
5e50: 50 49 33 52 45 46 3a 20 36 34 2d 42 69 74 20 49  PI3REF: 64-Bit I
5e60: 6e 74 65 67 65 72 20 54 79 70 65 73 20 7b 48 31  nteger Types {H1
5e70: 30 32 30 30 7d 20 3c 53 31 30 31 31 30 3e 0a 2a  0200} <S10110>.*
5e80: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
5e90: 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f  te_int64 sqlite_
5ea0: 75 69 6e 74 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63  uint64.**.** Bec
5eb0: 61 75 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f  ause there is no
5ec0: 20 63 72 6f 73 73 2d 70 6c 61 74 66 6f 72 6d 20   cross-platform 
5ed0: 77 61 79 20 74 6f 20 73 70 65 63 69 66 79 20 36  way to specify 6
5ee0: 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74 79  4-bit integer ty
5ef0: 70 65 73 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e  pes.** SQLite in
5f00: 63 6c 75 64 65 73 20 74 79 70 65 64 65 66 73 20  cludes typedefs 
5f10: 66 6f 72 20 36 34 2d 62 69 74 20 73 69 67 6e 65  for 64-bit signe
5f20: 64 20 61 6e 64 20 75 6e 73 69 67 6e 65 64 20 69  d and unsigned i
5f30: 6e 74 65 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54  ntegers..**.** T
5f40: 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  he sqlite3_int64
5f50: 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 75 69 6e   and sqlite3_uin
5f60: 74 36 34 20 61 72 65 20 74 68 65 20 70 72 65 66  t64 are the pref
5f70: 65 72 72 65 64 20 74 79 70 65 20 64 65 66 69 6e  erred type defin
5f80: 69 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73  itions..** The s
5f90: 71 6c 69 74 65 5f 69 6e 74 36 34 20 61 6e 64 20  qlite_int64 and 
5fa0: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 74 79  sqlite_uint64 ty
5fb0: 70 65 73 20 61 72 65 20 73 75 70 70 6f 72 74 65  pes are supporte
5fc0: 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 0a  d for backwards.
5fd0: 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ** compatibility
5fe0: 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 52 65 71   only..**.** Req
5ff0: 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 32  uirements: [H102
6000: 30 31 5d 20 5b 48 31 30 32 30 32 5d 0a 2a 2f 0a  01] [H10202].*/.
6010: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 4e  #ifdef SQLITE_IN
6020: 54 36 34 5f 54 59 50 45 0a 20 20 74 79 70 65 64  T64_TYPE.  typed
6030: 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f  ef SQLITE_INT64_
6040: 54 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36  TYPE sqlite_int6
6050: 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73  4;.  typedef uns
6060: 69 67 6e 65 64 20 53 51 4c 49 54 45 5f 49 4e 54  igned SQLITE_INT
6070: 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 75  64_TYPE sqlite_u
6080: 69 6e 74 36 34 3b 0a 23 65 6c 69 66 20 64 65 66  int64;.#elif def
6090: 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 7c  ined(_MSC_VER) |
60a0: 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c  | defined(__BORL
60b0: 41 4e 44 43 5f 5f 29 0a 20 20 74 79 70 65 64 65  ANDC__).  typede
60c0: 66 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  f __int64 sqlite
60d0: 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65  _int64;.  typede
60e0: 66 20 75 6e 73 69 67 6e 65 64 20 5f 5f 69 6e 74  f unsigned __int
60f0: 36 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  64 sqlite_uint64
6100: 3b 0a 23 65 6c 73 65 0a 20 20 74 79 70 65 64 65  ;.#else.  typede
6110: 66 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20  f long long int 
6120: 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20  sqlite_int64;.  
6130: 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
6140: 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73   long long int s
6150: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65  qlite_uint64;.#e
6160: 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c  ndif.typedef sql
6170: 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  ite_int64 sqlite
6180: 33 5f 69 6e 74 36 34 3b 0a 74 79 70 65 64 65 66  3_int64;.typedef
6190: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
61a0: 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 3b 0a 0a  qlite3_uint64;..
61b0: 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69  /*.** If compili
61c0: 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73  ng for a process
61d0: 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c  or that lacks fl
61e0: 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70  oating point sup
61f0: 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74  port,.** substit
6200: 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20  ute integer for 
6210: 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 2e 0a  floating-point..
6220: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
6230: 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
6240: 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f  OINT.# define do
6250: 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 69 6e 74  uble sqlite3_int
6260: 36 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  64.#endif../*.**
6270: 20 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 69   CAPI3REF: Closi
6280: 6e 67 20 41 20 44 61 74 61 62 61 73 65 20 43 6f  ng A Database Co
6290: 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32 30 31 30  nnection {H12010
62a0: 7d 20 3c 53 33 30 31 30 30 3e 3c 53 34 30 32 30  } <S30100><S4020
62b0: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0>.**.** This ro
62c0: 75 74 69 6e 65 20 69 73 20 74 68 65 20 64 65 73  utine is the des
62d0: 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20  tructor for the 
62e0: 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74  [sqlite3] object
62f0: 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74  ..**.** Applicat
6300: 69 6f 6e 73 20 73 68 6f 75 6c 64 20 5b 73 71 6c  ions should [sql
6310: 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c 20  ite3_finalize | 
6320: 66 69 6e 61 6c 69 7a 65 5d 20 61 6c 6c 20 5b 70  finalize] all [p
6330: 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
6340: 74 73 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69  ts].** and [sqli
6350: 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 7c  te3_blob_close |
6360: 20 63 6c 6f 73 65 5d 20 61 6c 6c 20 5b 42 4c 4f   close] all [BLO
6370: 42 20 68 61 6e 64 6c 65 73 5d 20 61 73 73 6f 63  B handles] assoc
6380: 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68  iated with.** th
6390: 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65  e [sqlite3] obje
63a0: 63 74 20 70 72 69 6f 72 20 74 6f 20 61 74 74 65  ct prior to atte
63b0: 6d 70 74 69 6e 67 20 74 6f 20 63 6c 6f 73 65 20  mpting to close 
63c0: 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54  the object..** T
63d0: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74  he [sqlite3_next
63e0: 5f 73 74 6d 74 28 29 5d 20 69 6e 74 65 72 66 61  _stmt()] interfa
63f0: 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ce can be used t
6400: 6f 20 6c 6f 63 61 74 65 20 61 6c 6c 0a 2a 2a 20  o locate all.** 
6410: 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
6420: 65 6e 74 73 5d 20 61 73 73 6f 63 69 61 74 65 64  ents] associated
6430: 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 73   with a [databas
6440: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 66  e connection] if
6450: 20 64 65 73 69 72 65 64 2e 0a 2a 2a 20 54 79 70   desired..** Typ
6460: 69 63 61 6c 20 63 6f 64 65 20 6d 69 67 68 74 20  ical code might 
6470: 6c 6f 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a 0a  look like this:.
6480: 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
6490: 65 3e 3c 70 72 65 3e 0a 2a 2a 20 73 71 6c 69 74  e><pre>.** sqlit
64a0: 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a  e3_stmt *pStmt;.
64b0: 2a 2a 20 77 68 69 6c 65 28 20 28 70 53 74 6d 74  ** while( (pStmt
64c0: 20 3d 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f   = sqlite3_next_
64d0: 73 74 6d 74 28 64 62 2c 20 30 29 29 21 3d 30 20  stmt(db, 0))!=0 
64e0: 29 7b 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 73  ){.** &nbsp;   s
64f0: 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
6500: 70 53 74 6d 74 29 3b 0a 2a 2a 20 7d 0a 2a 2a 20  pStmt);.** }.** 
6510: 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
6520: 74 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71  te>.**.** If [sq
6530: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69  lite3_close()] i
6540: 73 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20  s invoked while 
6550: 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
6560: 20 6f 70 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72   open,.** the tr
6570: 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 75 74  ansaction is aut
6580: 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65  omatically rolle
6590: 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  d back..**.** Th
65a0: 65 20 43 20 70 61 72 61 6d 65 74 65 72 20 74 6f  e C parameter to
65b0: 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28   [sqlite3_close(
65c0: 43 29 5d 20 6d 75 73 74 20 62 65 20 65 69 74 68  C)] must be eith
65d0: 65 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69  er a NULL.** poi
65e0: 6e 74 65 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69  nter or an [sqli
65f0: 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e  te3] object poin
6600: 74 65 72 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  ter obtained.** 
6610: 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70  from [sqlite3_op
6620: 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  en()], [sqlite3_
6630: 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a  open16()], or.**
6640: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
6650: 32 28 29 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72  2()], and not pr
6660: 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e  eviously closed.
6670: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
6680: 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d  nts:.** [H12011]
6690: 20 5b 48 31 32 30 31 32 5d 20 5b 48 31 32 30 31   [H12012] [H1201
66a0: 33 5d 20 5b 48 31 32 30 31 34 5d 20 5b 48 31 32  3] [H12014] [H12
66b0: 30 31 35 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f  015] [H12019].*/
66c0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
66d0: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71  sqlite3_close(sq
66e0: 6c 69 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  lite3 *);../*.**
66f0: 20 54 68 65 20 74 79 70 65 20 66 6f 72 20 61 20   The type for a 
6700: 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
6710: 6e 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c 65  n..** This is le
6720: 67 61 63 79 20 61 6e 64 20 64 65 70 72 65 63 61  gacy and depreca
6730: 74 65 64 2e 20 20 49 74 20 69 73 20 69 6e 63 6c  ted.  It is incl
6740: 75 64 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69  uded for histori
6750: 63 61 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69  cal.** compatibi
6760: 6c 69 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20  lity and is not 
6770: 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74  documented..*/.t
6780: 79 70 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c  ypedef int (*sql
6790: 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76  ite3_callback)(v
67a0: 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c  oid*,int,char**,
67b0: 20 63 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a   char**);../*.**
67c0: 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53   CAPI3REF: One-S
67d0: 74 65 70 20 51 75 65 72 79 20 45 78 65 63 75 74  tep Query Execut
67e0: 69 6f 6e 20 49 6e 74 65 72 66 61 63 65 20 7b 48  ion Interface {H
67f0: 31 32 31 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a  12100} <S10000>.
6800: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
6810: 33 5f 65 78 65 63 28 29 20 69 6e 74 65 72 66 61  3_exec() interfa
6820: 63 65 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65  ce is a convenie
6830: 6e 74 20 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e  nt way of runnin
6840: 67 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  g one or more.**
6850: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
6860: 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74  without having t
6870: 6f 20 77 72 69 74 65 20 61 20 6c 6f 74 20 6f 66  o write a lot of
6880: 20 43 20 63 6f 64 65 2e 20 20 54 68 65 20 55 54   C code.  The UT
6890: 46 2d 38 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53  F-8 encoded.** S
68a0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  QL statements ar
68b0: 65 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 74  e passed in as t
68c0: 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
68d0: 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65  ter to sqlite3_e
68e0: 78 65 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74  xec()..** The st
68f0: 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61  atements are eva
6900: 6c 75 61 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e  luated one by on
6910: 65 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61  e until either a
6920: 6e 20 65 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e  n error or.** an
6930: 20 69 6e 74 65 72 72 75 70 74 20 69 73 20 65 6e   interrupt is en
6940: 63 6f 75 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e  countered, or un
6950: 74 69 6c 20 74 68 65 79 20 61 72 65 20 61 6c 6c  til they are all
6960: 20 64 6f 6e 65 2e 20 20 54 68 65 20 33 72 64 20   done.  The 3rd 
6970: 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20  parameter.** is 
6980: 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c  an optional call
6990: 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 6e 76  back that is inv
69a0: 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  oked once for ea
69b0: 63 68 20 72 6f 77 20 6f 66 20 61 6e 79 20 71 75  ch row of any qu
69c0: 65 72 79 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70  ery.** results p
69d0: 72 6f 64 75 63 65 64 20 62 79 20 74 68 65 20 53  roduced by the S
69e0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  QL statements.  
69f0: 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65  The 5th paramete
6a00: 72 20 74 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a  r tells where.**
6a10: 20 74 6f 20 77 72 69 74 65 20 61 6e 79 20 65 72   to write any er
6a20: 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a  ror messages..**
6a30: 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 65  .** The error me
6a40: 73 73 61 67 65 20 70 61 73 73 65 64 20 62 61 63  ssage passed bac
6a50: 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 74  k through the 5t
6a60: 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 68  h parameter is h
6a70: 65 6c 64 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79  eld.** in memory
6a80: 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
6a90: 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
6aa0: 5d 2e 20 20 54 6f 20 61 76 6f 69 64 20 61 20 6d  ].  To avoid a m
6ab0: 65 6d 6f 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74  emory leak,.** t
6ac0: 68 65 20 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69  he calling appli
6ad0: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61  cation should ca
6ae0: 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  ll [sqlite3_free
6af0: 28 29 5d 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72  ()] on any error
6b00: 0a 2a 2a 20 6d 65 73 73 61 67 65 20 72 65 74 75  .** message retu
6b10: 72 6e 65 64 20 74 68 72 6f 75 67 68 20 74 68 65  rned through the
6b20: 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 77   5th parameter w
6b30: 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73  hen it has finis
6b40: 68 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65  hed using.** the
6b50: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
6b60: 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
6b70: 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68   statement in th
6b80: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
6b90: 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d  is NULL or an em
6ba0: 70 74 79 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72  pty string.** or
6bb0: 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69   a string contai
6bc0: 6e 69 6e 67 20 6f 6e 6c 79 20 77 68 69 74 65 73  ning only whites
6bd0: 70 61 63 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74  pace and comment
6be0: 73 2c 20 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a  s, then no SQL.*
6bf0: 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  * statements are
6c00: 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 74   evaluated and t
6c10: 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e  he database is n
6c20: 6f 74 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a  ot changed..**.*
6c30: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78  * The sqlite3_ex
6c40: 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  ec() interface i
6c50: 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e  s implemented in
6c60: 20 74 65 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71   terms of.** [sq
6c70: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
6c80: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74  ()], [sqlite3_st
6c90: 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69  ep()], and [sqli
6ca0: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e  te3_finalize()].
6cb0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
6cc0: 65 78 65 63 28 29 20 72 6f 75 74 69 6e 65 20 64  exec() routine d
6cd0: 6f 65 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74  oes nothing to t
6ce0: 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74  he database that
6cf0: 20 63 61 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a   cannot be done.
6d00: 2a 2a 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70  ** by [sqlite3_p
6d10: 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73  repare_v2()], [s
6d20: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20  qlite3_step()], 
6d30: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  and [sqlite3_fin
6d40: 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  alize()]..**.** 
6d50: 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
6d60: 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ter to [sqlite3_
6d70: 65 78 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20  exec()] must be 
6d80: 61 6e 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65  an valid and ope
6d90: 6e 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  n.** [database c
6da0: 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a  onnection]..**.*
6db0: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
6dc0: 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e  onnection must n
6dd0: 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69  ot be closed whi
6de0: 6c 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65  le.** [sqlite3_e
6df0: 78 65 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e  xec()] is runnin
6e00: 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  g..**.** The cal
6e10: 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68  ling function sh
6e20: 6f 75 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65  ould use [sqlite
6e30: 33 5f 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65  3_free()] to fre
6e40: 65 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20  e.** the memory 
6e50: 74 68 61 74 20 2a 65 72 72 6d 73 67 20 69 73 20  that *errmsg is 
6e60: 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
6e70: 20 6f 6e 63 65 20 74 68 65 20 65 72 72 6f 72 0a   once the error.
6e80: 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f  ** message is no
6e90: 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a   longer needed..
6ea0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 73 74  **.** The SQL st
6eb0: 61 74 65 6d 65 6e 74 20 74 65 78 74 20 69 6e 20  atement text in 
6ec0: 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
6ed0: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78  r to [sqlite3_ex
6ee0: 65 63 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65  ec()].** must re
6ef0: 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 77  main unchanged w
6f00: 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 65 78  hile [sqlite3_ex
6f10: 65 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67  ec()] is running
6f20: 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
6f30: 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31  ents:.** [H12101
6f40: 5d 20 5b 48 31 32 31 30 32 5d 20 5b 48 31 32 31  ] [H12102] [H121
6f50: 30 34 5d 20 5b 48 31 32 31 30 35 5d 20 5b 48 31  04] [H12105] [H1
6f60: 32 31 30 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b  2107] [H12110] [
6f70: 48 31 32 31 31 33 5d 20 5b 48 31 32 31 31 36 5d  H12113] [H12116]
6f80: 0a 2a 2a 20 5b 48 31 32 31 31 39 5d 20 5b 48 31  .** [H12119] [H1
6f90: 32 31 32 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b  2122] [H12125] [
6fa0: 48 31 32 31 33 31 5d 20 5b 48 31 32 31 33 34 5d  H12131] [H12134]
6fb0: 20 5b 48 31 32 31 33 37 5d 20 5b 48 31 32 31 33   [H12137] [H1213
6fc0: 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  8].*/.SQLITE_API
6fd0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65   int sqlite3_exe
6fe0: 63 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 20  c(.  sqlite3*,  
6ff0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7010: 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62  /* An open datab
7020: 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ase */.  const c
7030: 68 61 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20  har *sql,       
7040: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7050: 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65      /* SQL to be
7060: 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20   evaluated */.  
7070: 69 6e 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28  int (*callback)(
7080: 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a  void*,int,char**
7090: 2c 63 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61  ,char**),  /* Ca
70a0: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
70b0: 2a 2f 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20  */.  void *,    
70c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70e0: 2f 2a 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20  /* 1st argument 
70f0: 74 6f 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  to callback */. 
7100: 20 63 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 20   char **errmsg  
7110: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7120: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
7130: 72 72 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e  rror msg written
7140: 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a   here */.);../*.
7150: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73  ** CAPI3REF: Res
7160: 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 30 32 31  ult Codes {H1021
7170: 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b  0} <S10700>.** K
7180: 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f  EYWORDS: SQLITE_
7190: 4f 4b 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20  OK {error code} 
71a0: 7b 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a  {error codes}.**
71b0: 20 4b 45 59 57 4f 52 44 53 3a 20 7b 72 65 73 75   KEYWORDS: {resu
71c0: 6c 74 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74  lt code} {result
71d0: 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61   codes}.**.** Ma
71e0: 6e 79 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69  ny SQLite functi
71f0: 6f 6e 73 20 72 65 74 75 72 6e 20 61 6e 20 69 6e  ons return an in
7200: 74 65 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64  teger result cod
7210: 65 20 66 72 6f 6d 20 74 68 65 20 73 65 74 20 73  e from the set s
7220: 68 6f 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20  hown.** here in 
7230: 6f 72 64 65 72 20 74 6f 20 69 6e 64 69 63 61 74  order to indicat
7240: 65 73 20 73 75 63 63 65 73 73 20 6f 72 20 66 61  es success or fa
7250: 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  ilure..**.** New
7260: 20 65 72 72 6f 72 20 63 6f 64 65 73 20 6d 61 79   error codes may
7270: 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
7280: 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ure versions of 
7290: 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65  SQLite..**.** Se
72a0: 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f  e also: [SQLITE_
72b0: 49 4f 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74  IOERR_READ | ext
72c0: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
72d0: 65 73 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  es].*/.#define S
72e0: 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 20  QLITE_OK        
72f0: 20 20 20 30 20 20 20 2f 2a 20 53 75 63 63 65 73     0   /* Succes
7300: 73 66 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f  sful result */./
7310: 2a 20 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65  * beginning-of-e
7320: 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64  rror-codes */.#d
7330: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 52 52  efine SQLITE_ERR
7340: 4f 52 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a  OR        1   /*
7350: 20 53 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69   SQL error or mi
7360: 73 73 69 6e 67 20 64 61 74 61 62 61 73 65 20 2a  ssing database *
7370: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7380: 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20 20 32 20  _INTERNAL     2 
7390: 20 20 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f    /* Internal lo
73a0: 67 69 63 20 65 72 72 6f 72 20 69 6e 20 53 51 4c  gic error in SQL
73b0: 69 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ite */.#define S
73c0: 51 4c 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20  QLITE_PERM      
73d0: 20 20 20 33 20 20 20 2f 2a 20 41 63 63 65 73 73     3   /* Access
73e0: 20 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69   permission deni
73f0: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
7400: 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20  LITE_ABORT      
7410: 20 20 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63    4   /* Callbac
7420: 6b 20 72 6f 75 74 69 6e 65 20 72 65 71 75 65 73  k routine reques
7430: 74 65 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a  ted an abort */.
7440: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42  #define SQLITE_B
7450: 55 53 59 20 20 20 20 20 20 20 20 20 35 20 20 20  USY         5   
7460: 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
7470: 66 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a  file is locked *
7480: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7490: 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 36 20  _LOCKED       6 
74a0: 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20    /* A table in 
74b0: 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
74c0: 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e  locked */.#defin
74d0: 65 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20  e SQLITE_NOMEM  
74e0: 20 20 20 20 20 20 37 20 20 20 2f 2a 20 41 20 6d        7   /* A m
74f0: 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 2a  alloc() failed *
7500: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7510: 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20  _READONLY     8 
7520: 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20    /* Attempt to 
7530: 77 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79  write a readonly
7540: 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65   database */.#de
7550: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  fine SQLITE_INTE
7560: 52 52 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20  RRUPT    9   /* 
7570: 4f 70 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e  Operation termin
7580: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ated by sqlite3_
7590: 69 6e 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 64  interrupt()*/.#d
75a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
75b0: 52 52 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a  RR       10   /*
75c0: 20 53 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69   Some kind of di
75d0: 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  sk I/O error occ
75e0: 75 72 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  urred */.#define
75f0: 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
7600: 20 20 20 20 31 31 20 20 20 2f 2a 20 54 68 65 20      11   /* The 
7610: 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d  database disk im
7620: 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64  age is malformed
7630: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
7640: 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31  TE_NOTFOUND    1
7650: 32 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e  2   /* NOT USED.
7660: 20 54 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 64   Table or record
7670: 20 6e 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64   not found */.#d
7680: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c  efine SQLITE_FUL
7690: 4c 20 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a  L        13   /*
76a0: 20 49 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65   Insertion faile
76b0: 64 20 62 65 63 61 75 73 65 20 64 61 74 61 62 61  d because databa
76c0: 73 65 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64  se is full */.#d
76d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e  efine SQLITE_CAN
76e0: 54 4f 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a  TOPEN    14   /*
76f0: 20 55 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20   Unable to open 
7700: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
7710: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
7720: 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20  ITE_PROTOCOL    
7730: 31 35 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44  15   /* NOT USED
7740: 2e 20 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20  . Database lock 
7750: 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a  protocol error *
7760: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7770: 5f 45 4d 50 54 59 20 20 20 20 20 20 20 31 36 20  _EMPTY       16 
7780: 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73    /* Database is
7790: 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e   empty */.#defin
77a0: 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  e SQLITE_SCHEMA 
77b0: 20 20 20 20 20 31 37 20 20 20 2f 2a 20 54 68 65       17   /* The
77c0: 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
77d0: 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66   changed */.#def
77e0: 69 6e 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49  ine SQLITE_TOOBI
77f0: 47 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20 53  G      18   /* S
7800: 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78  tring or BLOB ex
7810: 63 65 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 74  ceeds size limit
7820: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
7830: 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 31  TE_CONSTRAINT  1
7840: 39 20 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 65  9   /* Abort due
7850: 20 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 76   to constraint v
7860: 69 6f 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66  iolation */.#def
7870: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41  ine SQLITE_MISMA
7880: 54 43 48 20 20 20 20 32 30 20 20 20 2f 2a 20 44  TCH    20   /* D
7890: 61 74 61 20 74 79 70 65 20 6d 69 73 6d 61 74 63  ata type mismatc
78a0: 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  h */.#define SQL
78b0: 49 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20  ITE_MISUSE      
78c0: 32 31 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 20  21   /* Library 
78d0: 75 73 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79  used incorrectly
78e0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
78f0: 54 45 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 32  TE_NOLFS       2
7900: 32 20 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 66  2   /* Uses OS f
7910: 65 61 74 75 72 65 73 20 6e 6f 74 20 73 75 70 70  eatures not supp
7920: 6f 72 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f  orted on host */
7930: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7940: 41 55 54 48 20 20 20 20 20 20 20 20 32 33 20 20  AUTH        23  
7950: 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f   /* Authorizatio
7960: 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66  n denied */.#def
7970: 69 6e 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41  ine SQLITE_FORMA
7980: 54 20 20 20 20 20 20 32 34 20 20 20 2f 2a 20 41  T      24   /* A
7990: 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
79a0: 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a  e format error *
79b0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
79c0: 5f 52 41 4e 47 45 20 20 20 20 20 20 20 32 35 20  _RANGE       25 
79d0: 20 20 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 74    /* 2nd paramet
79e0: 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69  er to sqlite3_bi
79f0: 6e 64 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  nd out of range 
7a00: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7a10: 45 5f 4e 4f 54 41 44 42 20 20 20 20 20 20 32 36  E_NOTADB      26
7a20: 20 20 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65     /* File opene
7a30: 64 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20  d that is not a 
7a40: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
7a50: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7a60: 52 4f 57 20 20 20 20 20 20 20 20 20 31 30 30 20  ROW         100 
7a70: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70   /* sqlite3_step
7a80: 28 29 20 68 61 73 20 61 6e 6f 74 68 65 72 20 72  () has another r
7a90: 6f 77 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 66  ow ready */.#def
7aa0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  ine SQLITE_DONE 
7ab0: 20 20 20 20 20 20 20 31 30 31 20 20 2f 2a 20 73         101  /* s
7ac0: 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68 61  qlite3_step() ha
7ad0: 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63 75  s finished execu
7ae0: 74 69 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f  ting */./* end-o
7af0: 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f  f-error-codes */
7b00: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
7b10: 3a 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c  : Extended Resul
7b20: 74 20 43 6f 64 65 73 20 7b 48 31 30 32 32 30 7d  t Codes {H10220}
7b30: 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59   <S10700>.** KEY
7b40: 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64  WORDS: {extended
7b50: 20 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78   error code} {ex
7b60: 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
7b70: 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  es}.** KEYWORDS:
7b80: 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c   {extended resul
7b90: 74 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65  t code} {extende
7ba0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a  d result codes}.
7bb0: 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65 66  **.** In its def
7bc0: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69  ault configurati
7bd0: 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50 49 20 72  on, SQLite API r
7be0: 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6f  outines return o
7bf0: 6e 65 20 6f 66 20 32 36 20 69 6e 74 65 67 65 72  ne of 26 integer
7c00: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c  .** [SQLITE_OK |
7c10: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20   result codes]. 
7c20: 20 48 6f 77 65 76 65 72 2c 20 65 78 70 65 72 69   However, experi
7c30: 65 6e 63 65 20 68 61 73 20 73 68 6f 77 6e 20 74  ence has shown t
7c40: 68 61 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74  hat many of.** t
7c50: 68 65 73 65 20 72 65 73 75 6c 74 20 63 6f 64 65  hese result code
7c60: 73 20 61 72 65 20 74 6f 6f 20 63 6f 61 72 73 65  s are too coarse
7c70: 2d 67 72 61 69 6e 65 64 2e 20 20 54 68 65 79 20  -grained.  They 
7c80: 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61  do not provide a
7c90: 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d  s.** much inform
7ca0: 61 74 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62  ation about prob
7cb0: 6c 65 6d 73 20 61 73 20 70 72 6f 67 72 61 6d 6d  lems as programm
7cc0: 65 72 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20  ers might like. 
7cd0: 20 49 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f   In an effort to
7ce0: 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 68 69 73  .** address this
7cf0: 2c 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73  , newer versions
7d00: 20 6f 66 20 53 51 4c 69 74 65 20 28 76 65 72 73   of SQLite (vers
7d10: 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61  ion 3.3.8 and la
7d20: 74 65 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20  ter) include.** 
7d30: 73 75 70 70 6f 72 74 20 66 6f 72 20 61 64 64 69  support for addi
7d40: 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f  tional result co
7d50: 64 65 73 20 74 68 61 74 20 70 72 6f 76 69 64 65  des that provide
7d60: 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20 69   more detailed i
7d70: 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62  nformation.** ab
7d80: 6f 75 74 20 65 72 72 6f 72 73 2e 20 54 68 65 20  out errors. The 
7d90: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
7da0: 63 6f 64 65 73 20 61 72 65 20 65 6e 61 62 6c 65  codes are enable
7db0: 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a  d or disabled.**
7dc0: 20 6f 6e 20 61 20 70 65 72 20 64 61 74 61 62 61   on a per databa
7dd0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61  se connection ba
7de0: 73 69 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  sis using the.**
7df0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64   [sqlite3_extend
7e00: 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28  ed_result_codes(
7e10: 29 5d 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f  )] API..**.** So
7e20: 6d 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61  me of the availa
7e30: 62 6c 65 20 65 78 74 65 6e 64 65 64 20 72 65 73  ble extended res
7e40: 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 6c 69  ult codes are li
7e50: 73 74 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e  sted here..** On
7e60: 65 20 6d 61 79 20 65 78 70 65 63 74 20 74 68 65  e may expect the
7e70: 20 6e 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e   number of exten
7e80: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
7e90: 20 77 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a   will be expand.
7ea0: 2a 2a 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53  ** over time.  S
7eb0: 6f 66 74 77 61 72 65 20 74 68 61 74 20 75 73 65  oftware that use
7ec0: 73 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  s extended resul
7ed0: 74 20 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65  t codes should e
7ee0: 78 70 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20  xpect.** to see 
7ef0: 6e 65 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73  new result codes
7f00: 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
7f10: 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ses of SQLite..*
7f20: 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
7f30: 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77  OK result code w
7f40: 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74  ill never be ext
7f50: 65 6e 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20  ended.  It will 
7f60: 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61  always.** be exa
7f70: 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64  ctly zero..*/.#d
7f80: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
7f90: 52 52 5f 52 45 41 44 20 20 20 20 20 20 20 20 20  RR_READ         
7fa0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
7fb0: 52 52 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65  RR | (1<<8)).#de
7fc0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52  fine SQLITE_IOER
7fd0: 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 20 20 20  R_SHORT_READ    
7fe0: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52      (SQLITE_IOER
7ff0: 52 20 7c 20 28 32 3c 3c 38 29 29 0a 23 64 65 66  R | (2<<8)).#def
8000: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8010: 5f 57 52 49 54 45 20 20 20 20 20 20 20 20 20 20  _WRITE          
8020: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8030: 20 7c 20 28 33 3c 3c 38 29 29 0a 23 64 65 66 69   | (3<<8)).#defi
8040: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
8050: 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 20 20  FSYNC           
8060: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
8070: 7c 20 28 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (4<<8)).#defin
8080: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  e SQLITE_IOERR_D
8090: 49 52 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20  IR_FSYNC        
80a0: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
80b0: 20 28 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (5<<8)).#define
80c0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52   SQLITE_IOERR_TR
80d0: 55 4e 43 41 54 45 20 20 20 20 20 20 20 20 20 20  UNCATE          
80e0: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
80f0: 28 36 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (6<<8)).#define 
8100: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54  SQLITE_IOERR_FST
8110: 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 28  AT             (
8120: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
8130: 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  7<<8)).#define S
8140: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
8150: 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 28 53  CK            (S
8160: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 38  QLITE_IOERR | (8
8170: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
8180: 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43  LITE_IOERR_RDLOC
8190: 4b 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51  K            (SQ
81a0: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c  LITE_IOERR | (9<
81b0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
81c0: 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45  ITE_IOERR_DELETE
81d0: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
81e0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c  ITE_IOERR | (10<
81f0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8200: 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
8210: 44 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c  D           (SQL
8220: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c  ITE_IOERR | (11<
8230: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8240: 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
8250: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
8260: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c  ITE_IOERR | (12<
8270: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8280: 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53  ITE_IOERR_ACCESS
8290: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
82a0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c  ITE_IOERR | (13<
82b0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
82c0: 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
82d0: 45 53 45 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c  ESERVEDLOCK (SQL
82e0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c  ITE_IOERR | (14<
82f0: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8300: 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20  ITE_IOERR_LOCK  
8310: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
8320: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c  ITE_IOERR | (15<
8330: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8340: 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20  ITE_IOERR_CLOSE 
8350: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
8360: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c  ITE_IOERR | (16<
8370: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8380: 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c  ITE_IOERR_DIR_CL
8390: 4f 53 45 20 20 20 20 20 20 20 20 20 28 53 51 4c  OSE         (SQL
83a0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c  ITE_IOERR | (17<
83b0: 3c 38 29 29 0a 0a 23 64 65 66 69 6e 65 20 53 51  <8))..#define SQ
83c0: 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52  LITE_LOCKED_SHAR
83d0: 45 44 43 41 43 48 45 20 20 20 20 20 20 28 53 51  EDCACHE      (SQ
83e0: 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 7c 20 28 31  LITE_LOCKED | (1
83f0: 3c 3c 38 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41  <<8) )../*.** CA
8400: 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20 46 6f  PI3REF: Flags Fo
8410: 72 20 46 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72  r File Open Oper
8420: 61 74 69 6f 6e 73 20 7b 48 31 30 32 33 30 7d 20  ations {H10230} 
8430: 3c 48 31 31 31 32 30 3e 20 3c 48 31 32 37 30 30  <H11120> <H12700
8440: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 62 69  >.**.** These bi
8450: 74 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 74  t values are int
8460: 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e  ended for use in
8470: 20 74 68 65 0a 2a 2a 20 33 72 64 20 70 61 72 61   the.** 3rd para
8480: 6d 65 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71  meter to the [sq
8490: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
84a0: 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 0a 2a   interface and.*
84b0: 2a 20 69 6e 20 74 68 65 20 34 74 68 20 70 61 72  * in the 4th par
84c0: 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 78 4f  ameter to the xO
84d0: 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  pen method of th
84e0: 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66  e.** [sqlite3_vf
84f0: 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64  s] object..*/.#d
8500: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8510: 4e 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 20  N_READONLY      
8520: 20 20 20 30 78 30 30 30 30 30 30 30 31 0a 23 64     0x00000001.#d
8530: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8540: 4e 5f 52 45 41 44 57 52 49 54 45 20 20 20 20 20  N_READWRITE     
8550: 20 20 20 30 78 30 30 30 30 30 30 30 32 0a 23 64     0x00000002.#d
8560: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8570: 4e 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 20  N_CREATE        
8580: 20 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64     0x00000004.#d
8590: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
85a0: 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20  N_DELETEONCLOSE 
85b0: 20 20 20 30 78 30 30 30 30 30 30 30 38 0a 23 64     0x00000008.#d
85c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
85d0: 4e 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  N_EXCLUSIVE     
85e0: 20 20 20 30 78 30 30 30 30 30 30 31 30 0a 23 64     0x00000010.#d
85f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8600: 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 20 20  N_MAIN_DB       
8610: 20 20 20 30 78 30 30 30 30 30 31 30 30 0a 23 64     0x00000100.#d
8620: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8630: 4e 5f 54 45 4d 50 5f 44 42 20 20 20 20 20 20 20  N_TEMP_DB       
8640: 20 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64     0x00000200.#d
8650: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8660: 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 20  N_TRANSIENT_DB  
8670: 20 20 20 30 78 30 30 30 30 30 34 30 30 0a 23 64     0x00000400.#d
8680: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8690: 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20  N_MAIN_JOURNAL  
86a0: 20 20 20 30 78 30 30 30 30 30 38 30 30 0a 23 64     0x00000800.#d
86b0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
86c0: 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20  N_TEMP_JOURNAL  
86d0: 20 20 20 30 78 30 30 30 30 31 30 30 30 0a 23 64     0x00001000.#d
86e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
86f0: 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 20  N_SUBJOURNAL    
8700: 20 20 20 30 78 30 30 30 30 32 30 30 30 0a 23 64     0x00002000.#d
8710: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8720: 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
8730: 20 20 20 30 78 30 30 30 30 34 30 30 30 0a 23 64     0x00004000.#d
8740: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8750: 4e 5f 4e 4f 4d 55 54 45 58 20 20 20 20 20 20 20  N_NOMUTEX       
8760: 20 20 20 30 78 30 30 30 30 38 30 30 30 0a 23 64     0x00008000.#d
8770: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45  efine SQLITE_OPE
8780: 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 20 20 20 20  N_FULLMUTEX     
8790: 20 20 20 30 78 30 30 30 31 30 30 30 30 0a 0a 2f     0x00010000../
87a0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
87b0: 65 76 69 63 65 20 43 68 61 72 61 63 74 65 72 69  evice Characteri
87c0: 73 74 69 63 73 20 7b 48 31 30 32 34 30 7d 20 3c  stics {H10240} <
87d0: 48 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  H11120>.**.** Th
87e0: 65 20 78 44 65 76 69 63 65 43 61 70 61 62 69 6c  e xDeviceCapabil
87f0: 69 74 69 65 73 20 6d 65 74 68 6f 64 20 6f 66 20  ities method of 
8800: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
8810: 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65  methods].** obje
8820: 63 74 20 72 65 74 75 72 6e 73 20 61 6e 20 69 6e  ct returns an in
8830: 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 61  teger which is a
8840: 20 76 65 63 74 6f 72 20 6f 66 20 74 68 65 20 74   vector of the t
8850: 68 65 73 65 0a 2a 2a 20 62 69 74 20 76 61 6c 75  hese.** bit valu
8860: 65 73 20 65 78 70 72 65 73 73 69 6e 67 20 49 2f  es expressing I/
8870: 4f 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  O characteristic
8880: 73 20 6f 66 20 74 68 65 20 6d 61 73 73 20 73 74  s of the mass st
8890: 6f 72 61 67 65 0a 2a 2a 20 64 65 76 69 63 65 20  orage.** device 
88a0: 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 66  that holds the f
88b0: 69 6c 65 20 74 68 61 74 20 74 68 65 20 5b 73 71  ile that the [sq
88c0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
88d0: 5d 0a 2a 2a 20 72 65 66 65 72 73 20 74 6f 2e 0a  ].** refers to..
88e0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
88f0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72  _IOCAP_ATOMIC pr
8900: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61  operty means tha
8910: 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a  t all writes of.
8920: 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20  ** any size are 
8930: 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c  atomic.  The SQL
8940: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
8950: 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65  nnn values.** me
8960: 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 20 6f  an that writes o
8970: 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72  f blocks that ar
8980: 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73  e nnn bytes in s
8990: 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61  ize and.** are a
89a0: 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64  ligned to an add
89b0: 72 65 73 73 20 77 68 69 63 68 20 69 73 20 61 6e  ress which is an
89c0: 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c   integer multipl
89d0: 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20  e of.** nnn are 
89e0: 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c  atomic.  The SQL
89f0: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
8a00: 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e  PPEND value mean
8a10: 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64  s.** that when d
8a20: 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 20  ata is appended 
8a30: 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64  to a file, the d
8a40: 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a  ata is appended.
8a50: 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 74 68  ** first then th
8a60: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69  e size of the fi
8a70: 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20  le is extended, 
8a80: 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a  never the other.
8a90: 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20  ** way around.  
8aa0: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
8ab0: 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70  _SEQUENTIAL prop
8ac0: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a  erty means that.
8ad0: 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ** information i
8ae0: 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73  s written to dis
8af0: 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72  k in the same or
8b00: 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20  der as calls.** 
8b10: 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a 2f 0a  to xWrite()..*/.
8b20: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8b30: 4f 43 41 50 5f 41 54 4f 4d 49 43 20 20 20 20 20  OCAP_ATOMIC     
8b40: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31 0a       0x00000001.
8b50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8b60: 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 20 20  OCAP_ATOMIC512  
8b70: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 32 0a       0x00000002.
8b80: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8b90: 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 20 20 20  OCAP_ATOMIC1K   
8ba0: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 34 0a       0x00000004.
8bb0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8bc0: 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20  OCAP_ATOMIC2K   
8bd0: 20 20 20 20 20 30 78 30 30 30 30 30 30 30 38 0a       0x00000008.
8be0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8bf0: 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 20 20 20  OCAP_ATOMIC4K   
8c00: 20 20 20 20 20 30 78 30 30 30 30 30 30 31 30 0a       0x00000010.
8c10: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8c20: 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 20 20 20  OCAP_ATOMIC8K   
8c30: 20 20 20 20 20 30 78 30 30 30 30 30 30 32 30 0a       0x00000020.
8c40: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8c50: 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b 20 20  OCAP_ATOMIC16K  
8c60: 20 20 20 20 20 30 78 30 30 30 30 30 30 34 30 0a       0x00000040.
8c70: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8c80: 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 20 20  OCAP_ATOMIC32K  
8c90: 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 0a       0x00000080.
8ca0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8cb0: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20  OCAP_ATOMIC64K  
8cc0: 20 20 20 20 20 30 78 30 30 30 30 30 31 30 30 0a       0x00000100.
8cd0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8ce0: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
8cf0: 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30 0a       0x00000200.
8d00: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8d10: 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20  OCAP_SEQUENTIAL 
8d20: 20 20 20 20 20 30 78 30 30 30 30 30 34 30 30 0a       0x00000400.
8d30: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
8d40: 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4c 65   File Locking Le
8d50: 76 65 6c 73 20 7b 48 31 30 32 35 30 7d 20 3c 48  vels {H10250} <H
8d60: 31 31 31 32 30 3e 20 3c 48 31 31 33 31 30 3e 0a  11120> <H11310>.
8d70: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65  **.** SQLite use
8d80: 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69  s one of these i
8d90: 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 73  nteger values as
8da0: 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61   the second.** a
8db0: 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 73  rgument to calls
8dc0: 20 69 74 20 6d 61 6b 65 73 20 74 6f 20 74 68 65   it makes to the
8dd0: 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e   xLock() and xUn
8de0: 6c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 73 0a 2a  lock() methods.*
8df0: 2a 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33  * of an [sqlite3
8e00: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  _io_methods] obj
8e10: 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ect..*/.#define 
8e20: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45  SQLITE_LOCK_NONE
8e30: 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66            0.#def
8e40: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  ine SQLITE_LOCK_
8e50: 53 48 41 52 45 44 20 20 20 20 20 20 20 20 31 0a  SHARED        1.
8e60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
8e70: 4f 43 4b 5f 52 45 53 45 52 56 45 44 20 20 20 20  OCK_RESERVED    
8e80: 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
8e90: 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 20  TE_LOCK_PENDING 
8ea0: 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20        3.#define 
8eb0: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c  SQLITE_LOCK_EXCL
8ec0: 55 53 49 56 45 20 20 20 20 20 34 0a 0a 2f 2a 0a  USIVE     4../*.
8ed0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 79 6e  ** CAPI3REF: Syn
8ee0: 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 20 54 79 70  chronization Typ
8ef0: 65 20 46 6c 61 67 73 20 7b 48 31 30 32 36 30 7d  e Flags {H10260}
8f00: 20 3c 48 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20   <H11120>.**.** 
8f10: 57 68 65 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f  When SQLite invo
8f20: 6b 65 73 20 74 68 65 20 78 53 79 6e 63 28 29 20  kes the xSync() 
8f30: 6d 65 74 68 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20  method of an.** 
8f40: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68  [sqlite3_io_meth
8f50: 6f 64 73 5d 20 6f 62 6a 65 63 74 20 69 74 20 75  ods] object it u
8f60: 73 65 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f  ses a combinatio
8f70: 6e 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 69 6e  n of.** these in
8f80: 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 73 20  teger values as 
8f90: 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
8fa0: 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ent..**.** When 
8fb0: 74 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  the SQLITE_SYNC_
8fc0: 44 41 54 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73  DATAONLY flag is
8fd0: 20 75 73 65 64 2c 20 69 74 20 6d 65 61 6e 73 20   used, it means 
8fe0: 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 79 6e 63  that the.** sync
8ff0: 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20   operation only 
9000: 6e 65 65 64 73 20 74 6f 20 66 6c 75 73 68 20 64  needs to flush d
9010: 61 74 61 20 74 6f 20 6d 61 73 73 20 73 74 6f 72  ata to mass stor
9020: 61 67 65 2e 20 20 49 6e 6f 64 65 0a 2a 2a 20 69  age.  Inode.** i
9030: 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20  nformation need 
9040: 6e 6f 74 20 62 65 20 66 6c 75 73 68 65 64 2e 20  not be flushed. 
9050: 54 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  The SQLITE_SYNC_
9060: 4e 4f 52 4d 41 4c 20 66 6c 61 67 20 6d 65 61 6e  NORMAL flag mean
9070: 73 0a 2a 2a 20 74 6f 20 75 73 65 20 6e 6f 72 6d  s.** to use norm
9080: 61 6c 20 66 73 79 6e 63 28 29 20 73 65 6d 61 6e  al fsync() seman
9090: 74 69 63 73 2e 20 54 68 65 20 53 51 4c 49 54 45  tics. The SQLITE
90a0: 5f 53 59 4e 43 5f 46 55 4c 4c 20 66 6c 61 67 20  _SYNC_FULL flag 
90b0: 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20  means.** to use 
90c0: 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66  Mac OS X style f
90d0: 75 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20  ullsync instead 
90e0: 6f 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23  of fsync()..*/.#
90f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59  define SQLITE_SY
9100: 4e 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20  NC_NORMAL       
9110: 20 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65   0x00002.#define
9120: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c   SQLITE_SYNC_FUL
9130: 4c 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  L          0x000
9140: 30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  03.#define SQLIT
9150: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20  E_SYNC_DATAONLY 
9160: 20 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a       0x00010../*
9170: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53  .** CAPI3REF: OS
9180: 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20   Interface Open 
9190: 46 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31  File Handle {H11
91a0: 31 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a  110} <S20110>.**
91b0: 0a 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f  .** An [sqlite3_
91c0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70  file] object rep
91d0: 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20  resents an open 
91e0: 66 69 6c 65 20 69 6e 20 74 68 65 20 4f 53 0a 2a  file in the OS.*
91f0: 2a 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65  * interface laye
9200: 72 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 4f  r.  Individual O
9210: 53 20 69 6e 74 65 72 66 61 63 65 20 69 6d 70 6c  S interface impl
9220: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c  ementations will
9230: 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73 75 62 63  .** want to subc
9240: 6c 61 73 73 20 74 68 69 73 20 6f 62 6a 65 63 74  lass this object
9250: 20 62 79 20 61 70 70 65 6e 64 69 6e 67 20 61 64   by appending ad
9260: 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 0a  ditional fields.
9270: 2a 2a 20 66 6f 72 20 74 68 65 69 72 20 6f 77 6e  ** for their own
9280: 20 75 73 65 2e 20 20 54 68 65 20 70 4d 65 74 68   use.  The pMeth
9290: 6f 64 73 20 65 6e 74 72 79 20 69 73 20 61 20 70  ods entry is a p
92a0: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ointer to an.** 
92b0: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68  [sqlite3_io_meth
92c0: 6f 64 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74  ods] object that
92d0: 20 64 65 66 69 6e 65 73 20 6d 65 74 68 6f 64 73   defines methods
92e0: 20 66 6f 72 20 70 65 72 66 6f 72 6d 69 6e 67 0a   for performing.
92f0: 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 74 69 6f 6e  ** I/O operation
9300: 73 20 6f 6e 20 74 68 65 20 6f 70 65 6e 20 66 69  s on the open fi
9310: 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  le..*/.typedef s
9320: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 66 69  truct sqlite3_fi
9330: 6c 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 3b  le sqlite3_file;
9340: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
9350: 66 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73  file {.  const s
9360: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f  truct sqlite3_io
9370: 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f  _methods *pMetho
9380: 64 73 3b 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20  ds;  /* Methods 
9390: 66 6f 72 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65  for an open file
93a0: 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.};../*.** CA
93b0: 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72  PI3REF: OS Inter
93c0: 66 61 63 65 20 46 69 6c 65 20 56 69 72 74 75 61  face File Virtua
93d0: 6c 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74  l Methods Object
93e0: 20 7b 48 31 31 31 32 30 7d 20 3c 53 32 30 31 31   {H11120} <S2011
93f0: 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 66  0>.**.** Every f
9400: 69 6c 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68  ile opened by th
9410: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  e [sqlite3_vfs] 
9420: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 70 6f 70  xOpen method pop
9430: 75 6c 61 74 65 73 20 61 6e 0a 2a 2a 20 5b 73 71  ulates an.** [sq
9440: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65  lite3_file] obje
9450: 63 74 20 28 6f 72 2c 20 6d 6f 72 65 20 63 6f 6d  ct (or, more com
9460: 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 63 6c 61 73  monly, a subclas
9470: 73 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  s of the.** [sql
9480: 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63  ite3_file] objec
9490: 74 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65  t) with a pointe
94a0: 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
94b0: 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 2e   of this object.
94c0: 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20  .** This object 
94d0: 64 65 66 69 6e 65 73 20 74 68 65 20 6d 65 74 68  defines the meth
94e0: 6f 64 73 20 75 73 65 64 20 74 6f 20 70 65 72 66  ods used to perf
94f0: 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f 70 65 72  orm various oper
9500: 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 61 69 6e 73  ations.** agains
9510: 74 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20  t the open file 
9520: 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 74  represented by t
9530: 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  he [sqlite3_file
9540: 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  ] object..**.** 
9550: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  The flags argume
9560: 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d 61 79 20  nt to xSync may 
9570: 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54  be one of [SQLIT
9580: 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 5d 20 6f  E_SYNC_NORMAL] o
9590: 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 59 4e  r.** [SQLITE_SYN
95a0: 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 20 66 69  C_FULL].  The fi
95b0: 72 73 74 20 63 68 6f 69 63 65 20 69 73 20 74 68  rst choice is th
95c0: 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29  e normal fsync()
95d0: 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
95e0: 63 68 6f 69 63 65 20 69 73 20 61 20 4d 61 63 20  choice is a Mac 
95f0: 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c 6c 73  OS X style fulls
9600: 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 4c 49 54  ync.  The [SQLIT
9610: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 5d  E_SYNC_DATAONLY]
9620: 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 62 65 20  .** flag may be 
9630: 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e 64 69 63  ORed in to indic
9640: 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 20 74 68  ate that only th
9650: 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 66 69  e data of the fi
9660: 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 20 69 74  le.** and not it
9670: 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 20 74 6f  s inode needs to
9680: 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a   be synced..**.*
9690: 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61  * The integer va
96a0: 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28 29 20  lues to xLock() 
96b0: 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 61 72  and xUnlock() ar
96c0: 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75 6c 3e  e one of.** <ul>
96d0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
96e0: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20  _LOCK_NONE],.** 
96f0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
9700: 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 3c 6c  K_SHARED],.** <l
9710: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  i> [SQLITE_LOCK_
9720: 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20 3c 6c  RESERVED],.** <l
9730: 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  i> [SQLITE_LOCK_
9740: 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a 2a 20  PENDING], or.** 
9750: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
9760: 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a 2a 2a  K_EXCLUSIVE]..**
9770: 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b 28   </ul>.** xLock(
9780: 29 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20  ) increases the 
9790: 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28 29 20  lock. xUnlock() 
97a0: 64 65 63 72 65 61 73 65 73 20 74 68 65 20 6c 6f  decreases the lo
97b0: 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 68 65 63  ck..** The xChec
97c0: 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 20  kReservedLock() 
97d0: 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 20 77 68  method checks wh
97e0: 65 74 68 65 72 20 61 6e 79 20 64 61 74 61 62 61  ether any databa
97f0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 2a  se connection,.*
9800: 2a 20 65 69 74 68 65 72 20 69 6e 20 74 68 69 73  * either in this
9810: 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 73   process or in s
9820: 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ome other proces
9830: 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20  s, is holding a 
9840: 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50 45 4e  RESERVED,.** PEN
9850: 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55 53 49  DING, or EXCLUSI
9860: 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66  VE lock on the f
9870: 69 6c 65 2e 20 20 49 74 20 72 65 74 75 72 6e 73  ile.  It returns
9880: 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 75 63 68   true.** if such
9890: 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 61   a lock exists a
98a0: 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69  nd false otherwi
98b0: 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 46  se..**.** The xF
98c0: 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 65 74  ileControl() met
98d0: 68 6f 64 20 69 73 20 61 20 67 65 6e 65 72 69 63  hod is a generic
98e0: 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
98f0: 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d 0a 2a 2a  allows custom.**
9900: 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74   VFS implementat
9910: 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 74 6c 79  ions to directly
9920: 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f 70 65 6e   control an open
9930: 20 66 69 6c 65 20 75 73 69 6e 67 20 74 68 65 0a   file using the.
9940: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  ** [sqlite3_file
9950: 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65  _control()] inte
9960: 72 66 61 63 65 2e 20 20 54 68 65 20 73 65 63 6f  rface.  The seco
9970: 6e 64 20 22 6f 70 22 20 61 72 67 75 6d 65 6e 74  nd "op" argument
9980: 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65   is an.** intege
9990: 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 74  r opcode.  The t
99a0: 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73  hird argument is
99b0: 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69 6e 74   a generic point
99c0: 65 72 20 69 6e 74 65 6e 64 65 64 20 74 6f 0a 2a  er intended to.*
99d0: 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72  * point to a str
99e0: 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 20  ucture that may 
99f0: 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65 6e 74  contain argument
9a00: 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20 77 68  s or space in wh
9a10: 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20  ich to.** write 
9a20: 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e 20 20  return values.  
9a30: 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73 20 66  Potential uses f
9a40: 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  or xFileControl(
9a50: 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 66 75  ) might be.** fu
9a60: 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61 62 6c  nctions to enabl
9a70: 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63 6b 73  e blocking locks
9a80: 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73 2c 20   with timeouts, 
9a90: 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a  to change the.**
9aa0: 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
9ab0: 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 74  y (for example t
9ac0: 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65 20 6c  o use dot-file l
9ad0: 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75 69 72  ocks), to inquir
9ae0: 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 73  e.** about the s
9af0: 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63 6b 2c  tatus of a lock,
9b00: 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73 74 61   or to break sta
9b10: 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 53  le locks.  The S
9b20: 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 72 65  QLite.** core re
9b30: 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63 6f 64  serves all opcod
9b40: 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30  es less than 100
9b50: 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75 73 65   for its own use
9b60: 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45 5f 46  ..** A [SQLITE_F
9b70: 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 7c  CNTL_LOCKSTATE |
9b80: 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64 65 73   list of opcodes
9b90: 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20  ] less than 100 
9ba0: 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a  is available..**
9bb0: 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   Applications th
9bc0: 61 74 20 64 65 66 69 6e 65 20 61 20 63 75 73 74  at define a cust
9bd0: 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  om xFileControl 
9be0: 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 75 73  method should us
9bf0: 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 67 72 65  e opcodes.** gre
9c00: 61 74 65 72 20 74 68 61 6e 20 31 30 30 20 74 6f  ater than 100 to
9c10: 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63 74 73   avoid conflicts
9c20: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 65 63  ..**.** The xSec
9c30: 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64  torSize() method
9c40: 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 65 63   returns the sec
9c50: 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 0a  tor size of the.
9c60: 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74 20 75  ** device that u
9c70: 6e 64 65 72 6c 69 65 73 20 74 68 65 20 66 69 6c  nderlies the fil
9c80: 65 2e 20 20 54 68 65 20 73 65 63 74 6f 72 20 73  e.  The sector s
9c90: 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 6d 69  ize is the.** mi
9ca0: 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 68 61 74  nimum write that
9cb0: 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65   can be performe
9cc0: 64 20 77 69 74 68 6f 75 74 20 64 69 73 74 75 72  d without distur
9cd0: 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 62 79  bing.** other by
9ce0: 74 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  tes in the file.
9cf0: 20 20 54 68 65 20 78 44 65 76 69 63 65 43 68 61    The xDeviceCha
9d00: 72 61 63 74 65 72 69 73 74 69 63 73 28 29 0a 2a  racteristics().*
9d10: 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73  * method returns
9d20: 20 61 20 62 69 74 20 76 65 63 74 6f 72 20 64 65   a bit vector de
9d30: 73 63 72 69 62 69 6e 67 20 62 65 68 61 76 69 6f  scribing behavio
9d40: 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75 6e 64  rs of the.** und
9d50: 65 72 6c 79 69 6e 67 20 64 65 76 69 63 65 3a 0a  erlying device:.
9d60: 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
9d70: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
9d80: 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c 6c 69 3e  _ATOMIC].** <li>
9d90: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41   [SQLITE_IOCAP_A
9da0: 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c 6c 69  TOMIC512].** <li
9db0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
9dc0: 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c 6c 69  ATOMIC1K].** <li
9dd0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
9de0: 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 3c 6c 69  ATOMIC2K].** <li
9df0: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
9e00: 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c 6c 69  ATOMIC4K].** <li
9e10: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
9e20: 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c 6c 69  ATOMIC8K].** <li
9e30: 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  > [SQLITE_IOCAP_
9e40: 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20 3c 6c  ATOMIC16K].** <l
9e50: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
9e60: 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a 20 3c  _ATOMIC32K].** <
9e70: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
9e80: 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a 2a 20  P_ATOMIC64K].** 
9e90: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
9ea0: 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 5d 0a  AP_SAFE_APPEND].
9eb0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
9ec0: 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c  IOCAP_SEQUENTIAL
9ed0: 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  ].** </ul>.**.**
9ee0: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41   The SQLITE_IOCA
9ef0: 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72 74  P_ATOMIC propert
9f00: 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c 6c  y means that all
9f10: 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61 6e   writes of.** an
9f20: 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d 69  y size are atomi
9f30: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  c.  The SQLITE_I
9f40: 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76  OCAP_ATOMICnnn v
9f50: 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74 68  alues.** mean th
9f60: 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c 6f  at writes of blo
9f70: 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e 6e  cks that are nnn
9f80: 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61   bytes in size a
9f90: 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e 65  nd.** are aligne
9fa0: 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73 20  d to an address 
9fb0: 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74 65  which is an inte
9fc0: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 0a  ger multiple of.
9fd0: 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d 69  ** nnn are atomi
9fe0: 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  c.  The SQLITE_I
9ff0: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
a000: 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a 20   value means.** 
a010: 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69  that when data i
a020: 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20  s appended to a 
a030: 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20 69  file, the data i
a040: 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66 69  s appended.** fi
a050: 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69 7a  rst then the siz
a060: 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73  e of the file is
a070: 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65 72   extended, never
a080: 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77 61   the other.** wa
a090: 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20 53  y around.  The S
a0a0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55  QLITE_IOCAP_SEQU
a0b0: 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 20  ENTIAL property 
a0c0: 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e  means that.** in
a0d0: 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72 69  formation is wri
a0e0: 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e 20  tten to disk in 
a0f0: 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20 61  the same order a
a100: 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57  s calls.** to xW
a110: 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  rite()..**.** If
a120: 20 78 52 65 61 64 28 29 20 72 65 74 75 72 6e 73   xRead() returns
a130: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48   SQLITE_IOERR_SH
a140: 4f 52 54 5f 52 45 41 44 20 69 74 20 6d 75 73 74  ORT_READ it must
a150: 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a 20 69 6e   also fill.** in
a160: 20 74 68 65 20 75 6e 72 65 61 64 20 70 6f 72 74   the unread port
a170: 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 75 66 66  ions of the buff
a180: 65 72 20 77 69 74 68 20 7a 65 72 6f 73 2e 20 20  er with zeros.  
a190: 41 20 56 46 53 20 74 68 61 74 0a 2a 2a 20 66 61  A VFS that.** fa
a1a0: 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c  ils to zero-fill
a1b0: 20 73 68 6f 72 74 20 72 65 61 64 73 20 6d 69 67   short reads mig
a1c0: 68 74 20 73 65 65 6d 20 74 6f 20 77 6f 72 6b 2e  ht seem to work.
a1d0: 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 61    However,.** fa
a1e0: 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f 2d 66 69  ilure to zero-fi
a1f0: 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 77  ll short reads w
a200: 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 6c  ill eventually l
a210: 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 61 62 61  ead to.** databa
a220: 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a  se corruption..*
a230: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
a240: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
a250: 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  ods sqlite3_io_m
a260: 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73  ethods;.struct s
a270: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
a280: 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73 69  s {.  int iVersi
a290: 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f  on;.  int (*xClo
a2a0: 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  se)(sqlite3_file
a2b0: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65 61  *);.  int (*xRea
a2c0: 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  d)(sqlite3_file*
a2d0: 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d  , void*, int iAm
a2e0: 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  t, sqlite3_int64
a2f0: 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28   iOfst);.  int (
a300: 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74 65 33  *xWrite)(sqlite3
a310: 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f  _file*, const vo
a320: 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73  id*, int iAmt, s
a330: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66  qlite3_int64 iOf
a340: 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 54 72  st);.  int (*xTr
a350: 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f  uncate)(sqlite3_
a360: 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69  file*, sqlite3_i
a370: 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20 69 6e  nt64 size);.  in
a380: 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74  t (*xSync)(sqlit
a390: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 66 6c  e3_file*, int fl
a3a0: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  ags);.  int (*xF
a3b0: 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74 65 33  ileSize)(sqlite3
a3c0: 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f  _file*, sqlite3_
a3d0: 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b 0a 20  int64 *pSize);. 
a3e0: 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28 73 71   int (*xLock)(sq
a3f0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
a400: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e 6c 6f  );.  int (*xUnlo
a410: 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck)(sqlite3_file
a420: 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  *, int);.  int (
a430: 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  *xCheckReservedL
a440: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock)(sqlite3_fil
a450: 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  e*, int *pResOut
a460: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 65  );.  int (*xFile
a470: 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74 65 33  Control)(sqlite3
a480: 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70 2c 20  _file*, int op, 
a490: 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20 20 69  void *pArg);.  i
a4a0: 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a 65  nt (*xSectorSize
a4b0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  )(sqlite3_file*)
a4c0: 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76 69 63  ;.  int (*xDevic
a4d0: 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
a4e0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  )(sqlite3_file*)
a4f0: 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e 61  ;.  /* Additiona
a500: 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20 62 65  l methods may be
a510: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
a520: 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d 3b 0a   releases */.};.
a530: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
a540: 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65 20 43   Standard File C
a550: 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73 20 7b  ontrol Opcodes {
a560: 48 31 31 33 31 30 7d 20 3c 53 33 30 38 30 30 3e  H11310} <S30800>
a570: 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74  .**.** These int
a580: 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 61  eger constants a
a590: 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 74  re opcodes for t
a5a0: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  he xFileControl 
a5b0: 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65  method.** of the
a5c0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
a5d0: 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61 6e 64  hods] object and
a5e0: 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65   for the [sqlite
a5f0: 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29  3_file_control()
a600: 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a  ].** interface..
a610: 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54  **.** The [SQLIT
a620: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
a630: 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65  E] opcode is use
a640: 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e  d for debugging.
a650: 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f 64 65    This.** opcode
a660: 20 63 61 75 73 65 73 20 74 68 65 20 78 46 69 6c   causes the xFil
a670: 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20  eControl method 
a680: 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72  to write the cur
a690: 72 65 6e 74 20 73 74 61 74 65 20 6f 66 0a 2a 2a  rent state of.**
a6a0: 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e 65 20 6f   the lock (one o
a6b0: 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e  f [SQLITE_LOCK_N
a6c0: 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f  ONE], [SQLITE_LO
a6d0: 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20 5b  CK_SHARED],.** [
a6e0: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45  SQLITE_LOCK_RESE
a6f0: 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c  RVED], [SQLITE_L
a700: 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72  OCK_PENDING], or
a710: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58   [SQLITE_LOCK_EX
a720: 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 69 6e 74  CLUSIVE]).** int
a730: 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  o an integer tha
a740: 74 20 74 68 65 20 70 41 72 67 20 61 72 67 75 6d  t the pArg argum
a750: 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 54  ent points to. T
a760: 68 69 73 20 63 61 70 61 62 69 6c 69 74 79 0a 2a  his capability.*
a770: 2a 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67  * is used during
a780: 20 74 65 73 74 69 6e 67 20 61 6e 64 20 6f 6e 6c   testing and onl
a790: 79 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 75  y needs to be su
a7a0: 70 70 6f 72 74 65 64 20 77 68 65 6e 20 53 51 4c  pported when SQL
a7b0: 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 73 20 64  ITE_TEST.** is d
a7c0: 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 65 66 69  efined..*/.#defi
a7d0: 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  ne SQLITE_FCNTL_
a7e0: 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 20 20 20  LOCKSTATE       
a7f0: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
a800: 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_GET_LOCKPROXYF
a810: 49 4c 45 20 20 20 20 20 20 32 0a 23 64 65 66 69  ILE      2.#defi
a820: 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f  ne SQLITE_SET_LO
a830: 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20  CKPROXYFILE     
a840: 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   3.#define SQLIT
a850: 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 20 20 20  E_LAST_ERRNO    
a860: 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a           4../*.*
a870: 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
a880: 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31 31 30  x Handle {H17110
a890: 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a 2a  } <S20130>.**.**
a8a0: 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64 75 6c   The mutex modul
a8b0: 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65 20  e within SQLite 
a8c0: 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74 65 33  defines [sqlite3
a8d0: 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20 61 6e  _mutex] to be an
a8e0: 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74 79 70  .** abstract typ
a8f0: 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20 6f 62  e for a mutex ob
a900: 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c 69 74  ject.  The SQLit
a910: 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c 6f 6f  e core never loo
a920: 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69 6e 74  ks.** at the int
a930: 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
a940: 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71 6c 69  tion of an [sqli
a950: 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49 74 20  te3_mutex].  It 
a960: 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20 77 69  only.** deals wi
a970: 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  th pointers to t
a980: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  he [sqlite3_mute
a990: 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  x] object..**.**
a9a0: 20 4d 75 74 65 78 65 73 20 61 72 65 20 63 72 65   Mutexes are cre
a9b0: 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69  ated using [sqli
a9c0: 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
a9d0: 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  )]..*/.typedef s
a9e0: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75  truct sqlite3_mu
a9f0: 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tex sqlite3_mute
aa00: 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  x;../*.** CAPI3R
aa10: 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 65  EF: OS Interface
aa20: 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34 30 7d   Object {H11140}
aa30: 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20100>.**.** 
aa40: 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
aa50: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f  he sqlite3_vfs o
aa60: 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68  bject defines th
aa70: 65 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 77  e interface betw
aa80: 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  een.** the SQLit
aa90: 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65 20 75  e core and the u
aaa0: 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
aab0: 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54 68 65  ing system.  The
aac0: 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74 68 65   "vfs".** in the
aad0: 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 62 6a   name of the obj
aae0: 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72 20 22  ect stands for "
aaf0: 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73  virtual file sys
ab00: 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tem"..**.** The 
ab10: 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 56 65  value of the iVe
ab20: 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 69  rsion field is i
ab30: 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74 20 6d  nitially 1 but m
ab40: 61 79 20 62 65 20 6c 61 72 67 65 72 20 69 6e 0a  ay be larger in.
ab50: 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73 69 6f  ** future versio
ab60: 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41  ns of SQLite.  A
ab70: 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
ab80: 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64   may be appended
ab90: 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65   to this.** obje
aba0: 63 74 20 77 68 65 6e 20 74 68 65 20 69 56 65 72  ct when the iVer
abb0: 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20 69 6e  sion value is in
abc0: 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65 20 74  creased.  Note t
abd0: 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72  hat the structur
abe0: 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69  e.** of the sqli
abf0: 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 63  te3_vfs object c
ac00: 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 74 72  hanges in the tr
ac10: 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77 65 65  ansaction betwee
ac20: 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73  n.** SQLite vers
ac30: 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20 33 2e  ion 3.5.9 and 3.
ac40: 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68 65 20  6.0 and yet the 
ac50: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 77  iVersion field w
ac60: 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66 69  as not.** modifi
ac70: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a  ed..**.** The sz
ac80: 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69 73 20  OsFile field is 
ac90: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
aca0: 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71 6c 69  subclassed [sqli
acb0: 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74 72  te3_file].** str
acc0: 75 63 74 75 72 65 20 75 73 65 64 20 62 79 20 74  ucture used by t
acd0: 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61 74 68  his VFS.  mxPath
ace0: 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 78 69  name is the maxi
acf0: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a  mum length of.**
ad00: 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 74   a pathname in t
ad10: 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52  his VFS..**.** R
ad20: 65 67 69 73 74 65 72 65 64 20 73 71 6c 69 74 65  egistered sqlite
ad30: 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 61 72  3_vfs objects ar
ad40: 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b  e kept on a link
ad50: 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64 20 62  ed list formed b
ad60: 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74 20 70  y.** the pNext p
ad70: 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b 73 71  ointer.  The [sq
ad80: 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
ad90: 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71  er()].** and [sq
ada0: 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
adb0: 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66 61 63  ster()] interfac
adc0: 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73 20 6c  es manage this l
add0: 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68 72 65  ist.** in a thre
ade0: 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20 54 68  ad-safe way.  Th
adf0: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66  e [sqlite3_vfs_f
ae00: 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61 63 65  ind()] interface
ae10: 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74 68 65  .** searches the
ae20: 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65 72 20   list.  Neither 
ae30: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
ae40: 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56 46 53  code nor the VFS
ae50: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
ae60: 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68  on should use th
ae70: 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e  e pNext pointer.
ae80: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65 78 74  .**.** The pNext
ae90: 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 6f 6e   field is the on
aea0: 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20  ly field in the 
aeb0: 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 73  sqlite3_vfs.** s
aec0: 74 72 75 63 74 75 72 65 20 74 68 61 74 20 53 51  tructure that SQ
aed0: 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72 20 6d  Lite will ever m
aee0: 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65 20 77  odify.  SQLite w
aef0: 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73 73 0a  ill only access.
af00: 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69  ** or modify thi
af10: 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20 68 6f  s field while ho
af20: 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c  lding a particul
af30: 61 72 20 73 74 61 74 69 63 20 6d 75 74 65 78 2e  ar static mutex.
af40: 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74  .** The applicat
af50: 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ion should never
af60: 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69 6e 67   modify anything
af70: 20 77 69 74 68 69 6e 20 74 68 65 20 73 71 6c 69   within the sqli
af80: 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65 63  te3_vfs.** objec
af90: 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a 65 63  t once the objec
afa0: 74 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73  t has been regis
afb0: 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tered..**.** The
afc0: 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68 6f 6c   zName field hol
afd0: 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ds the name of t
afe0: 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e 20 20  he VFS module.  
aff0: 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a 2a 2a  The name must.**
b000: 20 62 65 20 75 6e 69 71 75 65 20 61 63 72 6f 73   be unique acros
b010: 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75 6c 65  s all VFS module
b020: 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  s..**.** SQLite 
b030: 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65 20 74  will guarantee t
b040: 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d  hat the zFilenam
b050: 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78  e parameter to x
b060: 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 74 68 65  Open.** is eithe
b070: 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  r a NULL pointer
b080: 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74 61 69   or string obtai
b090: 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 46 75 6c  ned.** from xFul
b0a0: 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 20 53 51  lPathname().  SQ
b0b0: 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 75 61  Lite further gua
b0c0: 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a 20  rantees that.** 
b0d0: 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20  the string will 
b0e0: 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 6e 63  be valid and unc
b0f0: 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 43 6c  hanged until xCl
b100: 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c  ose() is.** call
b110: 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74  ed. Because of t
b120: 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74  he previous sent
b130: 65 6e 73 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 71  ense,.** the [sq
b140: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e 20  lite3_file] can 
b150: 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20 70  safely store a p
b160: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a  ointer to the.**
b170: 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74 20   filename if it 
b180: 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62 65  needs to remembe
b190: 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 66  r the filename f
b1a0: 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a  or some reason..
b1b0: 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65 6e  ** If the zFilen
b1c0: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  ame parameter is
b1d0: 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c 4c   xOpen is a NULL
b1e0: 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78 4f   pointer then xO
b1f0: 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 69  pen.** must invi
b200: 74 65 20 69 74 73 20 6f 77 6e 20 74 65 6d 70 6f  te its own tempo
b210: 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74 68  rary name for th
b220: 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65 76 65  e file.  Wheneve
b230: 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c 65 6e  r the .** xFilen
b240: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  ame parameter is
b250: 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20 61 6c   NULL it will al
b260: 73 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 74  so be the case t
b270: 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61 67 73  hat the.** flags
b280: 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c 20   parameter will 
b290: 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f  include [SQLITE_
b2a0: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
b2b0: 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  SE]..**.** The f
b2c0: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f  lags argument to
b2d0: 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75 64 65   xOpen() include
b2e0: 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74 20 69  s all bits set i
b2f0: 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73 20 61  n.** the flags a
b300: 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69  rgument to [sqli
b310: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20  te3_open_v2()]. 
b320: 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f   Or if [sqlite3_
b330: 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73  open()].** or [s
b340: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
b350: 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 66   is used, then f
b360: 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20 61 74  lags includes at
b370: 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c 49 54   least.** [SQLIT
b380: 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
b390: 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  ] | [SQLITE_OPEN
b3a0: 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20 49 66  _CREATE]. .** If
b3b0: 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73 20 61   xOpen() opens a
b3c0: 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c 79 20   file read-only 
b3d0: 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a 70 4f  then it sets *pO
b3e0: 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20 69 6e  utFlags to.** in
b3f0: 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50  clude [SQLITE_OP
b400: 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f  EN_READONLY].  O
b410: 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a 70 4f  ther bits in *pO
b420: 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65 20 73  utFlags may be s
b430: 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  et..**.** SQLite
b440: 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64 20 6f   will also add o
b450: 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
b460: 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65  ing flags to the
b470: 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 61 6c 6c   xOpen().** call
b480: 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  , depending on t
b490: 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e 67 20  he object being 
b4a0: 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75  opened:.**.** <u
b4b0: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c  l>.** <li>  [SQL
b4c0: 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
b4d0: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49  ].** <li>  [SQLI
b4e0: 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
b4f0: 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b  RNAL].** <li>  [
b500: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
b510: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  _DB].** <li>  [S
b520: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
b530: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e  JOURNAL].** <li>
b540: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54    [SQLITE_OPEN_T
b550: 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a 2a 20  RANSIENT_DB].** 
b560: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
b570: 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d 0a 2a  EN_SUBJOURNAL].*
b580: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f  * <li>  [SQLITE_
b590: 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
b5a0: 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  NAL].** </ul>.**
b5b0: 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 2f 4f  .** The file I/O
b5c0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
b5d0: 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 6a 65  can use the obje
b5e0: 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 74 6f  ct type flags to
b5f0: 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20 77  .** change the w
b600: 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 74 68  ay it deals with
b610: 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 78 61   files.  For exa
b620: 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61  mple, an applica
b630: 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f 65  tion.** that doe
b640: 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74  s not care about
b650: 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 79 20   crash recovery 
b660: 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67 68  or rollback migh
b670: 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f 70  t make.** the op
b680: 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20  en of a journal 
b690: 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 57  file a no-op.  W
b6a0: 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a 6f  rites to this jo
b6b0: 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20 61  urnal would.** a
b6c0: 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20 61  lso be no-ops, a
b6d0: 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20 74  nd any attempt t
b6e0: 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72 6e  o read the journ
b6f0: 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 0a  al would return.
b700: 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2e  ** SQLITE_IOERR.
b710: 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65    Or the impleme
b720: 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72 65  ntation might re
b730: 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20 64  cognize that a d
b740: 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20  atabase.** file 
b750: 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70 61  will be doing pa
b760: 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74 6f  ge-aligned secto
b770: 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74  r reads and writ
b780: 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a 2a  es in a random.*
b790: 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 74 20  * order and set 
b7a0: 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73 79  up its I/O subsy
b7b0: 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c 79  stem accordingly
b7c0: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d  ..**.** SQLite m
b7d0: 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f 6e  ight also add on
b7e0: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
b7f0: 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65 20  ng flags to the 
b800: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a 2a  xOpen method:.**
b810: 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
b820: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45   [SQLITE_OPEN_DE
b830: 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20  LETEONCLOSE].** 
b840: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45  <li> [SQLITE_OPE
b850: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a 20  N_EXCLUSIVE].** 
b860: 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  </ul>.**.** The 
b870: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c  [SQLITE_OPEN_DEL
b880: 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c 61 67  ETEONCLOSE] flag
b890: 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65 20   means the file 
b8a0: 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 65 6c  should be.** del
b8b0: 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 73 20  eted when it is 
b8c0: 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 5b 53 51  closed.  The [SQ
b8d0: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
b8e0: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c  ONCLOSE].** will
b8f0: 20 62 65 20 73 65 74 20 66 6f 72 20 54 45 4d 50   be set for TEMP
b900: 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a 6f 75    databases, jou
b910: 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20 73 75  rnals and for su
b920: 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a  bjournals..**.**
b930: 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45   The [SQLITE_OPE
b940: 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c 61  N_EXCLUSIVE] fla
b950: 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65  g means the file
b960: 20 73 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65   should be opene
b970: 64 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69  d.** for exclusi
b980: 76 65 20 61 63 63 65 73 73 2e 20 20 54 68 69 73  ve access.  This
b990: 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72   flag is set for
b9a0: 20 61 6c 6c 20 66 69 6c 65 73 20 65 78 63 65 70   all files excep
b9b0: 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6d 61 69  t.** for the mai
b9c0: 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
b9d0: 0a 2a 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20  .**.** At least 
b9e0: 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 20 6f  szOsFile bytes o
b9f0: 66 20 6d 65 6d 6f 72 79 20 61 72 65 20 61 6c 6c  f memory are all
ba00: 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69 74 65  ocated by SQLite
ba10: 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  .** to hold the 
ba20: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20   [sqlite3_file] 
ba30: 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64  structure passed
ba40: 20 61 73 20 74 68 65 20 74 68 69 72 64 0a 2a 2a   as the third.**
ba50: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70   argument to xOp
ba60: 65 6e 2e 20 20 54 68 65 20 78 4f 70 65 6e 20 6d  en.  The xOpen m
ba70: 65 74 68 6f 64 20 64 6f 65 73 20 6e 6f 74 20 68  ethod does not h
ba80: 61 76 65 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 61  ave to.** alloca
ba90: 74 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65  te the structure
baa0: 3b 20 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 74  ; it should just
bab0: 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a   fill it in..**.
bac0: 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67  ** The flags arg
bad0: 75 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73  ument to xAccess
bae0: 28 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54  () may be [SQLIT
baf0: 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d  E_ACCESS_EXISTS]
bb00: 0a 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20  .** to test for 
bb10: 74 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66  the existence of
bb20: 20 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c   a file, or [SQL
bb30: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
bb40: 52 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74  RITE] to.** test
bb50: 20 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 20   whether a file 
bb60: 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20  is readable and 
bb70: 77 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51  writable, or [SQ
bb80: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
bb90: 5d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65  ].** to test whe
bba0: 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 61  ther a file is a
bbb0: 74 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65  t least readable
bbc0: 2e 20 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e  .   The file can
bbd0: 20 62 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f   be a.** directo
bbe0: 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  ry..**.** SQLite
bbf0: 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c   will always all
bc00: 6f 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d  ocate at least m
bc10: 78 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65  xPathname+1 byte
bc20: 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74  s for the.** out
bc30: 70 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c  put buffer xFull
bc40: 50 61 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65  Pathname.  The e
bc50: 78 61 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65  xact size of the
bc60: 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a   output buffer.*
bc70: 2a 20 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64  * is also passed
bc80: 20 61 73 20 61 20 70 61 72 61 6d 65 74 65 72 20   as a parameter 
bc90: 74 6f 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73  to both  methods
bca0: 2e 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20  . If the output 
bcb0: 62 75 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74  buffer.** is not
bcc0: 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b   large enough, [
bcd0: 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d  SQLITE_CANTOPEN]
bce0: 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72   should be retur
bcf0: 6e 65 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20  ned. Since this 
bd00: 69 73 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73  is.** handled as
bd10: 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62   a fatal error b
bd20: 79 20 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d  y SQLite, vfs im
bd30: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68  plementations sh
bd40: 6f 75 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a  ould endeavor.**
bd50: 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73   to prevent this
bd60: 20 62 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61   by setting mxPa
bd70: 74 68 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66  thname to a suff
bd80: 69 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76  iciently large v
bd90: 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alue..**.** The 
bda0: 78 52 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78  xRandomness(), x
bdb0: 53 6c 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75  Sleep(), and xCu
bdc0: 72 72 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65  rrentTime() inte
bdd0: 72 66 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f  rfaces.** are no
bde0: 74 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72  t strictly a par
bdf0: 74 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73  t of the filesys
be00: 74 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72  tem, but they ar
be10: 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e  e.** included in
be20: 20 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75   the VFS structu
be30: 72 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e  re for completen
be40: 65 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e  ess..** The xRan
be50: 64 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69  domness() functi
be60: 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72  on attempts to r
be70: 65 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74  eturn nBytes byt
be80: 65 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75  es.** of good-qu
be90: 61 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73  ality randomness
bea0: 20 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65   into zOut.  The
beb0: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
bec0: 0a 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e  .** the actual n
bed0: 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
bee0: 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74  f randomness obt
bef0: 61 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53  ained..** The xS
bf00: 6c 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61  leep() method ca
bf10: 75 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67  uses the calling
bf20: 20 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70   thread to sleep
bf30: 20 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74   for at.** least
bf40: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   the number of m
bf50: 69 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65  icroseconds give
bf60: 6e 2e 20 20 54 68 65 20 78 43 75 72 72 65 6e 74  n.  The xCurrent
bf70: 54 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  Time().** method
bf80: 20 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61   returns a Julia
bf90: 6e 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72  n Day Number for
bfa0: 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74   the current dat
bfb0: 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a  e and time..**.*
bfc0: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
bfd0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c   sqlite3_vfs sql
bfe0: 69 74 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74  ite3_vfs;.struct
bff0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20   sqlite3_vfs {. 
c000: 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20   int iVersion;  
c010: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72            /* Str
c020: 75 63 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e  ucture version n
c030: 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73  umber */.  int s
c040: 7a 4f 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20  zOsFile;        
c050: 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73      /* Size of s
c060: 75 62 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65  ubclassed sqlite
c070: 33 5f 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  3_file */.  int 
c080: 6d 78 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20  mxPathname;     
c090: 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
c0a0: 66 69 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65  file pathname le
c0b0: 6e 67 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ngth */.  sqlite
c0c0: 33 5f 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20  3_vfs *pNext;   
c0d0: 20 20 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73     /* Next regis
c0e0: 74 65 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63  tered VFS */.  c
c0f0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
c100: 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ;       /* Name 
c110: 6f 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20  of this virtual 
c120: 66 69 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20  file system */. 
c130: 20 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b   void *pAppData;
c140: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
c150: 6e 74 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74  nter to applicat
c160: 69 6f 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74  ion-specific dat
c170: 61 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70  a */.  int (*xOp
c180: 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  en)(sqlite3_vfs*
c190: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
c1a0: 61 6d 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c  ame, sqlite3_fil
c1b0: 65 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  e*,.            
c1c0: 20 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e     int flags, in
c1d0: 74 20 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20  t *pOutFlags);. 
c1e0: 20 69 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28   int (*xDelete)(
c1f0: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f  sqlite3_vfs*, co
c200: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
c210: 20 69 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20   int syncDir);. 
c220: 20 69 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28   int (*xAccess)(
c230: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f  sqlite3_vfs*, co
c240: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
c250: 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20   int flags, int 
c260: 2a 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74  *pResOut);.  int
c270: 20 28 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65   (*xFullPathname
c280: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  )(sqlite3_vfs*, 
c290: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
c2a0: 65 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61  e, int nOut, cha
c2b0: 72 20 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64  r *zOut);.  void
c2c0: 20 2a 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c   *(*xDlOpen)(sql
c2d0: 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74  ite3_vfs*, const
c2e0: 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
c2f0: 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45  );.  void (*xDlE
c300: 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66  rror)(sqlite3_vf
c310: 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63  s*, int nByte, c
c320: 68 61 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20  har *zErrMsg);. 
c330: 20 76 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d   void (*(*xDlSym
c340: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76  )(sqlite3_vfs*,v
c350: 6f 69 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  oid*, const char
c360: 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64   *zSymbol))(void
c370: 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43  );.  void (*xDlC
c380: 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66  lose)(sqlite3_vf
c390: 73 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  s*, void*);.  in
c3a0: 74 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29  t (*xRandomness)
c3b0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69  (sqlite3_vfs*, i
c3c0: 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a  nt nByte, char *
c3d0: 7a 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78  zOut);.  int (*x
c3e0: 53 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76  Sleep)(sqlite3_v
c3f0: 66 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65  fs*, int microse
c400: 63 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a  conds);.  int (*
c410: 78 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71  xCurrentTime)(sq
c420: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62  lite3_vfs*, doub
c430: 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47  le*);.  int (*xG
c440: 65 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c  etLastError)(sql
c450: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20  ite3_vfs*, int, 
c460: 63 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65  char *);.  /* Ne
c470: 77 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20  w fields may be 
c480: 61 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75  appended in figu
c490: 72 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68  re versions.  Th
c4a0: 65 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20  e iVersion.  ** 
c4b0: 76 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65  value will incre
c4c0: 6d 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68  ment whenever th
c4d0: 69 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d  is happens. */.}
c4e0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
c4f0: 46 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65  F: Flags for the
c500: 20 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74   xAccess VFS met
c510: 68 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31  hod {H11190} <H1
c520: 31 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  1140>.**.** Thes
c530: 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61  e integer consta
c540: 6e 74 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  nts can be used 
c550: 61 73 20 74 68 65 20 74 68 69 72 64 20 70 61 72  as the third par
c560: 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65  ameter to.** the
c570: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20   xAccess method 
c580: 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76  of an [sqlite3_v
c590: 66 73 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44  fs] object. {END
c5a0: 7d 20 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e  }  They determin
c5b0: 65 0a 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f  e.** what kind o
c5c0: 66 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68  f permissions th
c5d0: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64  e xAccess method
c5e0: 20 69 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e   is looking for.
c5f0: 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f  .** With SQLITE_
c600: 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74  ACCESS_EXISTS, t
c610: 68 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f  he xAccess metho
c620: 64 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63  d.** simply chec
c630: 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66  ks whether the f
c640: 69 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57  ile exists..** W
c650: 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ith SQLITE_ACCES
c660: 53 5f 52 45 41 44 57 52 49 54 45 2c 20 74 68 65  S_READWRITE, the
c670: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a   xAccess method.
c680: 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65  ** checks whethe
c690: 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f  r the file is bo
c6a0: 74 68 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20  th readable and 
c6b0: 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74  writable..** Wit
c6c0: 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  h SQLITE_ACCESS_
c6d0: 52 45 41 44 2c 20 74 68 65 20 78 41 63 63 65 73  READ, the xAcces
c6e0: 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63  s method.** chec
c6f0: 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66  ks whether the f
c700: 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e  ile is readable.
c710: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
c720: 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
c730: 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51      0.#define SQ
c740: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
c750: 57 52 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20  WRITE 1.#define 
c760: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
c770: 41 44 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a  AD      2../*.**
c780: 20 43 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69   CAPI3REF: Initi
c790: 61 6c 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65  alize The SQLite
c7a0: 20 4c 69 62 72 61 72 79 20 7b 48 31 30 31 33 30   Library {H10130
c7b0: 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30  } <S20000><S3010
c7c0: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
c7d0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
c7e0: 29 20 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61  ) routine initia
c7f0: 6c 69 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c  lizes the.** SQL
c800: 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68  ite library.  Th
c810: 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  e sqlite3_shutdo
c820: 77 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  wn() routine.** 
c830: 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20  deallocates any 
c840: 72 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77  resources that w
c850: 65 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  ere allocated by
c860: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
c870: 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 63  ize()..**.** A c
c880: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69  all to sqlite3_i
c890: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 61  nitialize() is a
c8a0: 6e 20 22 65 66 66 65 63 74 69 76 65 22 20 63 61  n "effective" ca
c8b0: 6c 6c 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 74  ll if it is.** t
c8c0: 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 73 71  he first time sq
c8d0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
c8e0: 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 64 75  () is invoked du
c8f0: 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d  ring the lifetim
c900: 65 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 63  e of.** the proc
c910: 65 73 73 2c 20 6f 72 20 69 66 20 69 74 20 69 73  ess, or if it is
c920: 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
c930: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
c940: 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a  ze() is invoked.
c950: 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63  ** following a c
c960: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  all to sqlite3_s
c970: 68 75 74 64 6f 77 6e 28 29 2e 20 20 4f 6e 6c 79  hutdown().  Only
c980: 20 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61   an effective ca
c990: 6c 6c 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33  ll.** of sqlite3
c9a0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 6f  _initialize() do
c9b0: 65 73 20 61 6e 79 20 69 6e 69 74 69 61 6c 69 7a  es any initializ
c9c0: 61 74 69 6f 6e 2e 20 20 41 6c 6c 20 6f 74 68 65  ation.  All othe
c9d0: 72 20 63 61 6c 6c 73 0a 2a 2a 20 61 72 65 20 68  r calls.** are h
c9e0: 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 0a  armless no-ops..
c9f0: 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68 65  **.** Among othe
ca00: 72 20 74 68 69 6e 67 73 2c 20 73 71 6c 69 74 65  r things, sqlite
ca10: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73  3_initialize() s
ca20: 68 61 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73  hall invoke.** s
ca30: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
ca40: 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 73 71  .  Similarly, sq
ca50: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
ca60: 0a 2a 2a 20 73 68 61 6c 6c 20 69 6e 76 6f 6b 65  .** shall invoke
ca70: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
ca80: 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  )..**.** The sql
ca90: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
caa0: 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  ) routine return
cab0: 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e  s [SQLITE_OK] on
cac0: 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 49 66 20   success..** If 
cad0: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2c  for some reason,
cae0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
caf0: 69 7a 65 28 29 20 69 73 20 75 6e 61 62 6c 65 20  ize() is unable 
cb00: 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a  to initialize.**
cb10: 20 74 68 65 20 6c 69 62 72 61 72 79 20 28 70 65   the library (pe
cb20: 72 68 61 70 73 20 69 74 20 69 73 20 75 6e 61 62  rhaps it is unab
cb30: 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  le to allocate a
cb40: 20 6e 65 65 64 65 64 20 72 65 73 6f 75 72 63 65   needed resource
cb50: 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 20 6d 75   such.** as a mu
cb60: 74 65 78 29 20 69 74 20 72 65 74 75 72 6e 73 20  tex) it returns 
cb70: 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  an [error code] 
cb80: 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49  other than [SQLI
cb90: 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  TE_OK]..**.** Th
cba0: 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  e sqlite3_initia
cbb0: 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 69  lize() routine i
cbc0: 73 20 63 61 6c 6c 65 64 20 69 6e 74 65 72 6e 61  s called interna
cbd0: 6c 6c 79 20 62 79 20 6d 61 6e 79 20 6f 74 68 65  lly by many othe
cbe0: 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 65  r.** SQLite inte
cbf0: 72 66 61 63 65 73 20 73 6f 20 74 68 61 74 20 61  rfaces so that a
cc00: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 75 73  n application us
cc10: 75 61 6c 6c 79 20 64 6f 65 73 20 6e 6f 74 20 6e  ually does not n
cc20: 65 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65  eed to.** invoke
cc30: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
cc40: 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 2e 20  ize() directly. 
cc50: 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 5b 73   For example, [s
cc60: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a  qlite3_open()].*
cc70: 2a 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 5f  * calls sqlite3_
cc80: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 6f 20  initialize() so 
cc90: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
cca0: 72 79 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d  ry will be autom
ccb0: 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 69 6e 69 74  atically.** init
ccc0: 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 5b 73 71  ialized when [sq
ccd0: 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 69 73  lite3_open()] is
cce0: 20 63 61 6c 6c 65 64 20 69 66 20 69 74 20 68 61   called if it ha
ccf0: 73 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c  s not be initial
cd00: 69 7a 65 64 0a 2a 2a 20 61 6c 72 65 61 64 79 2e  ized.** already.
cd10: 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51    However, if SQ
cd20: 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
cd30: 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c 49 54   with the [SQLIT
cd40: 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d  E_OMIT_AUTOINIT]
cd50: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  .** compile-time
cd60: 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68   option, then th
cd70: 65 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c  e automatic call
cd80: 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69  s to sqlite3_ini
cd90: 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 61 72 65  tialize().** are
cda0: 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 74 68 65   omitted and the
cdb0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
cdc0: 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 69  t call sqlite3_i
cdd0: 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72 65  nitialize() dire
cde0: 63 74 6c 79 0a 2a 2a 20 70 72 69 6f 72 20 74 6f  ctly.** prior to
cdf0: 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72   using any other
ce00: 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
ce10: 65 2e 20 20 46 6f 72 20 6d 61 78 69 6d 75 6d 20  e.  For maximum 
ce20: 70 6f 72 74 61 62 69 6c 69 74 79 2c 0a 2a 2a 20  portability,.** 
ce30: 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  it is recommende
ce40: 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69  d that applicati
ce50: 6f 6e 73 20 61 6c 77 61 79 73 20 69 6e 76 6f 6b  ons always invok
ce60: 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  e sqlite3_initia
ce70: 6c 69 7a 65 28 29 0a 2a 2a 20 64 69 72 65 63 74  lize().** direct
ce80: 6c 79 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e  ly prior to usin
ce90: 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69  g any other SQLi
cea0: 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46  te interface.  F
ceb0: 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 0a 2a  uture releases.*
cec0: 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20  * of SQLite may 
ced0: 72 65 71 75 69 72 65 20 74 68 69 73 2e 20 20 49  require this.  I
cee0: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74  n other words, t
cef0: 68 65 20 62 65 68 61 76 69 6f 72 20 65 78 68 69  he behavior exhi
cf00: 62 69 74 65 64 0a 2a 2a 20 77 68 65 6e 20 53 51  bited.** when SQ
cf10: 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
cf20: 20 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 4f 4d   with [SQLITE_OM
cf30: 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20 6d 69 67  IT_AUTOINIT] mig
cf40: 68 74 20 62 65 63 6f 6d 65 20 74 68 65 0a 2a 2a  ht become the.**
cf50: 20 64 65 66 61 75 6c 74 20 62 65 68 61 76 69 6f   default behavio
cf60: 72 20 69 6e 20 73 6f 6d 65 20 66 75 74 75 72 65  r in some future
cf70: 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69   release of SQLi
cf80: 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  te..**.** The sq
cf90: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20  lite3_os_init() 
cfa0: 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6f 70 65  routine does ope
cfb0: 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70  rating-system sp
cfc0: 65 63 69 66 69 63 0a 2a 2a 20 69 6e 69 74 69 61  ecific.** initia
cfd0: 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  lization of the 
cfe0: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
cff0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f   The sqlite3_os_
d000: 65 6e 64 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  end().** routine
d010: 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66 65   undoes the effe
d020: 63 74 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73  ct of sqlite3_os
d030: 5f 69 6e 69 74 28 29 2e 20 20 54 79 70 69 63 61  _init().  Typica
d040: 6c 20 74 61 73 6b 73 0a 2a 2a 20 70 65 72 66 6f  l tasks.** perfo
d050: 72 6d 65 64 20 62 79 20 74 68 65 73 65 20 72 6f  rmed by these ro
d060: 75 74 69 6e 65 73 20 69 6e 63 6c 75 64 65 20 61  utines include a
d070: 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 64 65 61  llocation or dea
d080: 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20  llocation.** of 
d090: 73 74 61 74 69 63 20 72 65 73 6f 75 72 63 65 73  static resources
d0a0: 2c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  , initialization
d0b0: 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61   of global varia
d0c0: 62 6c 65 73 2c 0a 2a 2a 20 73 65 74 74 69 6e 67  bles,.** setting
d0d0: 20 75 70 20 61 20 64 65 66 61 75 6c 74 20 5b 73   up a default [s
d0e0: 71 6c 69 74 65 33 5f 76 66 73 5d 20 6d 6f 64 75  qlite3_vfs] modu
d0f0: 6c 65 2c 20 6f 72 20 73 65 74 74 69 6e 67 20 75  le, or setting u
d100: 70 0a 2a 2a 20 61 20 64 65 66 61 75 6c 74 20 63  p.** a default c
d110: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 75 73 69  onfiguration usi
d120: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ng [sqlite3_conf
d130: 69 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ig()]..**.** The
d140: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f   application sho
d150: 75 6c 64 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65  uld never invoke
d160: 20 65 69 74 68 65 72 20 73 71 6c 69 74 65 33 5f   either sqlite3_
d170: 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 6f 72 20  os_init().** or 
d180: 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29  sqlite3_os_end()
d190: 20 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65 20   directly.  The 
d1a0: 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75  application shou
d1b0: 6c 64 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 0a 2a  ld only invoke.*
d1c0: 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  * sqlite3_initia
d1d0: 6c 69 7a 65 28 29 20 61 6e 64 20 73 71 6c 69 74  lize() and sqlit
d1e0: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20  e3_shutdown().  
d1f0: 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69  The sqlite3_os_i
d200: 6e 69 74 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61  nit().** interfa
d210: 63 65 20 69 73 20 63 61 6c 6c 65 64 20 61 75 74  ce is called aut
d220: 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 73 71  omatically by sq
d230: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
d240: 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65  () and.** sqlite
d250: 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73 20 63 61  3_os_end() is ca
d260: 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  lled by sqlite3_
d270: 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 41 70 70  shutdown().  App
d280: 72 6f 70 72 69 61 74 65 0a 2a 2a 20 69 6d 70 6c  ropriate.** impl
d290: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20  ementations for 
d2a0: 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
d2b0: 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73  ) and sqlite3_os
d2c0: 5f 65 6e 64 28 29 0a 2a 2a 20 61 72 65 20 62 75  _end().** are bu
d2d0: 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 20  ilt into SQLite 
d2e0: 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70 69  when it is compi
d2f0: 6c 65 64 20 66 6f 72 20 75 6e 69 78 2c 20 77 69  led for unix, wi
d300: 6e 64 6f 77 73 2c 20 6f 72 20 6f 73 2f 32 2e 0a  ndows, or os/2..
d310: 2a 2a 20 57 68 65 6e 20 62 75 69 6c 74 20 66 6f  ** When built fo
d320: 72 20 6f 74 68 65 72 20 70 6c 61 74 66 6f 72 6d  r other platform
d330: 73 20 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51  s (using the [SQ
d340: 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d  LITE_OS_OTHER=1]
d350: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a   compile-time.**
d360: 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70   option) the app
d370: 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75  lication must su
d380: 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20  pply a suitable 
d390: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66  implementation f
d3a0: 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  or.** sqlite3_os
d3b0: 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69  _init() and sqli
d3c0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41  te3_os_end().  A
d3d0: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75  n application-su
d3e0: 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d  pplied.** implem
d3f0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
d400: 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72  te3_os_init() or
d410: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
d420: 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e  ).** must return
d430: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20   [SQLITE_OK] on 
d440: 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65  success and some
d450: 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f   other [error co
d460: 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c  de] upon.** fail
d470: 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ure..*/.SQLITE_A
d480: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
d490: 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b  nitialize(void);
d4a0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
d4b0: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
d4c0: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41  (void);.SQLITE_A
d4d0: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
d4e0: 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51  s_init(void);.SQ
d4f0: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
d500: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64  ite3_os_end(void
d510: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
d520: 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20  EF: Configuring 
d530: 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61  The SQLite Libra
d540: 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 30  ry {H14100} <S20
d550: 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 20  000><S30200>.** 
d560: 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
d570: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
d580: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
d590: 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b  e is used to mak
d5a0: 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75  e global configu
d5b0: 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65  ration.** change
d5c0: 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f  s to SQLite in o
d5d0: 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c  rder to tune SQL
d5e0: 69 74 65 20 74 6f 20 74 68 65 20 73 70 65 63 69  ite to the speci
d5f0: 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a 20  fic needs of.** 
d600: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e  the application.
d610: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f    The default co
d620: 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73 20 72  nfiguration is r
d630: 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6d  ecommended for m
d640: 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  ost.** applicati
d650: 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69 73 20  ons and so this 
d660: 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c  routine is usual
d670: 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79  ly not necessary
d680: 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76  .  It is.** prov
d690: 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20  ided to support 
d6a0: 72 61 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e  rare application
d6b0: 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c 20 6e  s with unusual n
d6c0: 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eeds..**.** The 
d6d0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
d6e0: 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f   interface is no
d6f0: 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 54  t threadsafe.  T
d700: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a  he application.*
d710: 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20 74 68  * must insure th
d720: 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c 69  at no other SQLi
d730: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  te interfaces ar
d740: 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 68  e invoked by oth
d750: 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 68  er.** threads wh
d760: 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ile sqlite3_conf
d770: 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 2e  ig() is running.
d780: 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 73    Furthermore, s
d790: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 0a  qlite3_config().
d7a0: 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 69  ** may only be i
d7b0: 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20  nvoked prior to 
d7c0: 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61 6c 69  library initiali
d7d0: 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20  zation using.** 
d7e0: 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c  [sqlite3_initial
d7f0: 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20  ize()] or after 
d800: 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 6c  shutdown by [sql
d810: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d  ite3_shutdown()]
d820: 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 76  ..** Note, howev
d830: 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74 65 33  er, that sqlite3
d840: 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 65  _config() can be
d850: 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74 20   called as part 
d860: 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d  of the.** implem
d870: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 61  entation of an a
d880: 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
d890: 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f 69  ed [sqlite3_os_i
d8a0: 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  nit()]..**.** Th
d8b0: 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
d8c0: 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66   to sqlite3_conf
d8d0: 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74 65 67  ig() is an integ
d8e0: 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f  er.** [SQLITE_CO
d8f0: 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41  NFIG_SINGLETHREA
d900: 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  D | configuratio
d910: 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64  n option] that d
d920: 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61  etermines.** wha
d930: 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51  t property of SQ
d940: 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f  Lite is to be co
d950: 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62 73 65  nfigured.  Subse
d960: 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a  quent arguments.
d970: 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e  ** vary dependin
d980: 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 45  g on the [SQLITE
d990: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
d9a0: 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61  READ | configura
d9b0: 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20  tion option].** 
d9c0: 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67  in the first arg
d9d0: 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  ument..**.** Whe
d9e0: 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  n a configuratio
d9f0: 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2c  n option is set,
da00: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
da10: 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  ) returns [SQLIT
da20: 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 65  E_OK]..** If the
da30: 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f   option is unkno
da40: 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69 73 20  wn or SQLite is 
da50: 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20 74 68  unable to set th
da60: 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e  e option.** then
da70: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
da80: 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f  turns a non-zero
da90: 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a   [error code]..*
daa0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
dab0: 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 5b  s:.** [H14103] [
dac0: 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32 30 5d  H14106] [H14120]
dad0: 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34 31 32   [H14123] [H1412
dae0: 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 34  6] [H14129] [H14
daf0: 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 2a  132] [H14135].**
db00: 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34 31 34   [H14138] [H1414
db10: 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 34  1] [H14144] [H14
db20: 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 48  147] [H14150] [H
db30: 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36 5d 20  14153] [H14156] 
db40: 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 34  [H14159].** [H14
db50: 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 48  162] [H14165] [H
db60: 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45  14168].*/.SQLITE
db70: 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
db80: 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c  RIMENTAL int sql
db90: 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 2c  ite3_config(int,
dba0: 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   ...);../*.** CA
dbb0: 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72  PI3REF: Configur
dbc0: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
dbd0: 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 7d  ctions  {H14200}
dbe0: 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50   <S20000>.** EXP
dbf0: 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
dc00: 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63  The sqlite3_db_c
dc10: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
dc20: 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b  e is used to mak
dc30: 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a  e configuration.
dc40: 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20  ** changes to a 
dc50: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
dc60: 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65  tion].  The inte
dc70: 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72  rface is similar
dc80: 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
dc90: 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74  config()] except
dca0: 20 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65   that the change
dcb0: 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e  s apply to a sin
dcc0: 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  gle.** [database
dcd0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70   connection] (sp
dce0: 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 66  ecified in the f
dcf0: 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 20  irst argument). 
dd00: 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   The.** sqlite3_
dd10: 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65  db_config() inte
dd20: 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 62  rface can only b
dd30: 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61 74 65  e used immediate
dd40: 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20  ly after.** the 
dd50: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
dd60: 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 20 75  ion is created u
dd70: 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70  sing [sqlite3_op
dd80: 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  en()],.** [sqlit
dd90: 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72  e3_open16()], or
dda0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
ddb0: 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  2()].  .**.** Th
ddc0: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
ddd0: 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f  t to sqlite3_db_
dde0: 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20  config(D,V,...) 
ddf0: 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 69   is the.** confi
de00: 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20 2d 20  guration verb - 
de10: 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20  an integer code 
de20: 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 77  that indicates w
de30: 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f 66  hat.** aspect of
de40: 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
de50: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65  onnection] is be
de60: 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a  ing configured..
de70: 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f 69  ** The only choi
de80: 63 65 20 66 6f 72 20 74 68 69 73 20 76 61 6c 75  ce for this valu
de90: 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 43  e is [SQLITE_DBC
dea0: 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d  ONFIG_LOOKASIDE]
deb0: 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 61  ..** New verbs a
dec0: 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  re likely to be 
ded0: 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20  added in future 
dee0: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
def0: 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61  te..** Additiona
df00: 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65 70 65  l arguments depe
df10: 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62 2e 0a  nd on the verb..
df20: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
df30: 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d 20  ts:.** [H14203] 
df40: 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32 30 39  [H14206] [H14209
df50: 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 32  ] [H14212] [H142
df60: 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  15].*/.SQLITE_AP
df70: 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
df80: 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
df90: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69  3_db_config(sqli
dfa0: 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e  te3*, int op, ..
dfb0: 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
dfc0: 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f  REF: Memory Allo
dfd0: 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20  cation Routines 
dfe0: 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30  {H10155} <S20120
dff0: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
e000: 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  L.**.** An insta
e010: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
e020: 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  ct defines the i
e030: 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e  nterface between
e040: 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c   SQLite.** and l
e050: 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
e060: 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69  allocation routi
e070: 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  nes..**.** This 
e080: 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 20 69  object is used i
e090: 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65  n only one place
e0a0: 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69   in the SQLite i
e0b0: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70  nterface..** A p
e0c0: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
e0d0: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
e0e0: 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 67 75  ject is the argu
e0f0: 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69  ment to.** [sqli
e100: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68  te3_config()] wh
e110: 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61  en the configura
e120: 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a  tion option is.*
e130: 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
e140: 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 42 79 20 63 72  _MALLOC].  By cr
e150: 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e  eating an instan
e160: 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
e170: 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67  t.** and passing
e180: 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f   it to [sqlite3_
e190: 63 6f 6e 66 69 67 28 29 5d 20 64 75 72 69 6e 67  config()] during
e1a0: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20   configuration, 
e1b0: 61 6e 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  an.** applicatio
e1c0: 6e 20 63 61 6e 20 73 70 65 63 69 66 79 20 61 6e  n can specify an
e1d0: 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 6d   alternative mem
e1e0: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
e1f0: 75 62 73 79 73 74 65 6d 0a 2a 2a 20 66 6f 72 20  ubsystem.** for 
e200: 53 51 4c 69 74 65 20 74 6f 20 75 73 65 20 66 6f  SQLite to use fo
e210: 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e  r all of its dyn
e220: 61 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65 64  amic memory need
e230: 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  s..**.** Note th
e240: 61 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73 20  at SQLite comes 
e250: 77 69 74 68 20 61 20 62 75 69 6c 74 2d 69 6e 20  with a built-in 
e260: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
e270: 20 74 68 61 74 20 69 73 0a 2a 2a 20 70 65 72 66   that is.** perf
e280: 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 20 66  ectly adequate f
e290: 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c 6d  or the overwhelm
e2a0: 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20  ing majority of 
e2b0: 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20  applications.** 
e2c0: 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f 62  and that this ob
e2d0: 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73 65  ject is only use
e2e0: 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d 69  ful to a tiny mi
e2f0: 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 63  nority of applic
e300: 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 73  ations.** with s
e310: 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f 72  pecialized memor
e320: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
e330: 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69 73  uirements.  This
e340: 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61 6c   object is.** al
e350: 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20 74  so used during t
e360: 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74 65  esting of SQLite
e370: 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65   in order to spe
e380: 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74  cify an alternat
e390: 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c  ive.** memory al
e3a0: 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69 6d  locator that sim
e3b0: 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75  ulates memory ou
e3c0: 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64  t-of-memory cond
e3d0: 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 64  itions in.** ord
e3e0: 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68 61  er to verify tha
e3f0: 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65 72  t SQLite recover
e400: 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72 6f  s gracefully fro
e410: 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 74  m such.** condit
e420: 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ions..**.** The 
e430: 78 4d 61 6c 6c 6f 63 2c 20 78 46 72 65 65 2c 20  xMalloc, xFree, 
e440: 61 6e 64 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74  and xRealloc met
e450: 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20 6c  hods must work l
e460: 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c 6f  ike the.** mallo
e470: 63 28 29 2c 20 66 72 65 65 28 29 2c 20 61 6e 64  c(), free(), and
e480: 20 72 65 61 6c 6c 6f 63 28 29 20 66 75 6e 63 74   realloc() funct
e490: 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 74  ions from the st
e4a0: 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 2e 0a  andard library..
e4b0: 2a 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75  **.** xSize shou
e4c0: 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 61 6c  ld return the al
e4d0: 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20  located size of 
e4e0: 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
e4f0: 69 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c  ion.** previousl
e500: 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
e510: 78 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c  xMalloc or xReal
e520: 6c 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61  loc.  The alloca
e530: 74 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20 61  ted size.** is a
e540: 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 61  lways at least a
e550: 73 20 62 69 67 20 61 73 20 74 68 65 20 72 65 71  s big as the req
e560: 75 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 20  uested size but 
e570: 6d 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a 2a  may be larger..*
e580: 2a 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64 75  *.** The xRoundu
e590: 70 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73  p method returns
e5a0: 20 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20 74   what would be t
e5b0: 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a  he allocated siz
e5c0: 65 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 79  e of.** a memory
e5d0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76 65   allocation give
e5e0: 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72  n a particular r
e5f0: 65 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20 20  equested size.  
e600: 4d 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  Most memory.** a
e610: 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20  llocators round 
e620: 75 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  up memory alloca
e630: 74 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20 74  tions at least t
e640: 6f 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74 69  o the next multi
e650: 70 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 6f  ple.** of 8.  So
e660: 6d 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f  me allocators ro
e670: 75 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72 67  und up to a larg
e680: 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20 74  er multiple or t
e690: 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a  o a power of 2..
e6a0: 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 20  **.** The xInit 
e6b0: 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69 7a  method initializ
e6c0: 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  es the memory al
e6d0: 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20 65  locator.  (For e
e6e0: 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d 69  xample,.** it mi
e6f0: 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e 79  ght allocate any
e700: 20 72 65 71 75 69 72 65 20 6d 75 74 65 78 65 73   require mutexes
e710: 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20 69   or initialize i
e720: 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a 20  nternal data.** 
e730: 73 74 72 75 63 74 75 72 65 73 2e 20 20 54 68 65  structures.  The
e740: 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f   xShutdown metho
e750: 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 6e  d is invoked (in
e760: 64 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a 20  directly) by.** 
e770: 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77  [sqlite3_shutdow
e780: 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64 20  n()] and should 
e790: 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72  deallocate any r
e7a0: 65 73 6f 75 72 63 65 73 20 61 63 71 75 69 72 65  esources acquire
e7b0: 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20 20  d.** by xInit.  
e7c0: 54 68 65 20 70 41 70 70 44 61 74 61 20 70 6f 69  The pAppData poi
e7d0: 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20  nter is used as 
e7e0: 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65 74  the only paramet
e7f0: 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 61  er to.** xInit a
e800: 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a 2f  nd xShutdown..*/
e810: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
e820: 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
e830: 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  ods sqlite3_mem_
e840: 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20  methods;.struct 
e850: 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
e860: 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a  ods {.  void *(*
e870: 78 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20  xMalloc)(int);  
e880: 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
e890: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63   allocation func
e8a0: 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tion */.  void (
e8b0: 2a 78 46 72 65 65 29 28 76 6f 69 64 2a 29 3b 20  *xFree)(void*); 
e8c0: 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65           /* Free
e8d0: 20 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74   a prior allocat
e8e0: 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28  ion */.  void *(
e8f0: 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a  *xRealloc)(void*
e900: 2c 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a  ,int);  /* Resiz
e910: 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e an allocation 
e920: 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 7a 65  */.  int (*xSize
e930: 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20  )(void*);       
e940: 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68      /* Return th
e950: 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c  e size of an all
e960: 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  ocation */.  int
e970: 20 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 6e 74   (*xRoundup)(int
e980: 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  );          /* R
e990: 6f 75 6e 64 20 75 70 20 72 65 71 75 65 73 74 20  ound up request 
e9a0: 73 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69  size to allocati
e9b0: 6f 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74  on size */.  int
e9c0: 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29   (*xInit)(void*)
e9d0: 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ;           /* I
e9e0: 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
e9f0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a  mory allocator *
ea00: 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75 74  /.  void (*xShut
ea10: 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20  down)(void*);   
ea20: 20 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69     /* Deinitiali
ea30: 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
ea40: 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69  locator */.  voi
ea50: 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20 20  d *pAppData;    
ea60: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
ea70: 72 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74  rgument to xInit
ea80: 28 29 20 61 6e 64 20 78 53 68 75 74 64 6f 77 6e  () and xShutdown
ea90: 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  () */.};../*.** 
eaa0: 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67  CAPI3REF: Config
eab0: 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20  uration Options 
eac0: 7b 48 31 30 31 36 30 7d 20 3c 53 32 30 30 30 30  {H10160} <S20000
ead0: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
eae0: 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f  L.**.** These co
eaf0: 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20  nstants are the 
eb00: 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65  available intege
eb10: 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  r configuration 
eb20: 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20  options that.** 
eb30: 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 73  can be passed as
eb40: 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
eb50: 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69  ent to the [sqli
eb60: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e  te3_config()] in
eb70: 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e  terface..**.** N
eb80: 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ew configuration
eb90: 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20   options may be 
eba0: 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20  added in future 
ebb0: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
ebc0: 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20  te..** Existing 
ebd0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
ebe0: 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64  tions might be d
ebf0: 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70  iscontinued.  Ap
ec00: 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68  plications.** sh
ec10: 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72  ould check the r
ec20: 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
ec30: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
ec40: 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  )] to make sure 
ec50: 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c  that.** the call
ec60: 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73   worked.  The [s
ec70: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
ec80: 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20   interface will 
ec90: 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d  return a.** non-
eca0: 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65  zero [error code
ecb0: 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e  ] if a discontin
ecc0: 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  ued or unsupport
ecd0: 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ed configuration
ece0: 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e   option.** is in
ecf0: 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c  voked..**.** <dl
ed00: 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  >.** <dt>SQLITE_
ed10: 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52  CONFIG_SINGLETHR
ed20: 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  EAD</dt>.** <dd>
ed30: 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67  There are no arg
ed40: 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f  uments to this o
ed50: 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74  ption.  This opt
ed60: 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20  ion disables.** 
ed70: 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e 64  all mutexing and
ed80: 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e 74   puts SQLite int
ed90: 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20 69  o a mode where i
eda0: 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73  t can only be us
edb0: 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c  ed.** by a singl
edc0: 65 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a  e thread.</dd>.*
edd0: 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
ede0: 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45  CONFIG_MULTITHRE
edf0: 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  AD</dt>.** <dd>T
ee00: 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75  here are no argu
ee10: 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70  ments to this op
ee20: 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69  tion.  This opti
ee30: 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d  on disables.** m
ee40: 75 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61  utexing on [data
ee50: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
ee60: 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73   and [prepared s
ee70: 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74  tatement] object
ee80: 73 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63  s..** The applic
ee90: 61 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73  ation is respons
eea0: 69 62 6c 65 20 66 6f 72 20 73 65 72 69 61 6c 69  ible for seriali
eeb0: 7a 69 6e 67 20 61 63 63 65 73 73 20 74 6f 0a 2a  zing access to.*
eec0: 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
eed0: 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72  ections] and [pr
eee0: 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
eef0: 73 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d  s].  But other m
ef00: 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e  utexes.** are en
ef10: 61 62 6c 65 64 20 73 6f 20 74 68 61 74 20 53 51  abled so that SQ
ef20: 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 73 61 66  Lite will be saf
ef30: 65 20 74 6f 20 75 73 65 20 69 6e 20 61 20 6d 75  e to use in a mu
ef40: 6c 74 69 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20  lti-threaded.** 
ef50: 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c  environment as l
ef60: 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 74 68  ong as no two th
ef70: 72 65 61 64 73 20 61 74 74 65 6d 70 74 20 74 6f  reads attempt to
ef80: 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a   use the same.**
ef90: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
efa0: 63 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 73 61  ction] at the sa
efb0: 6d 65 20 74 69 6d 65 2e 20 20 53 65 65 20 74 68  me time.  See th
efc0: 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  e [threading mod
efd0: 65 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74  e].** documentat
efe0: 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ion for addition
eff0: 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c  al information.<
f000: 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
f010: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52  QLITE_CONFIG_SER
f020: 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20  IALIZED</dt>.** 
f030: 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f  <dd>There are no
f040: 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
f050: 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73  is option.  This
f060: 20 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a   option enables.
f070: 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 20 69  ** all mutexes i
f080: 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 72 65 63  ncluding the rec
f090: 75 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65  ursive.** mutexe
f0a0: 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63  s on [database c
f0b0: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b  onnection] and [
f0c0: 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
f0d0: 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20  nt] objects..** 
f0e0: 49 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 77 68  In this mode (wh
f0f0: 69 63 68 20 69 73 20 74 68 65 20 64 65 66 61 75  ich is the defau
f100: 6c 74 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69  lt when SQLite i
f110: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a  s compiled with.
f120: 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41  ** [SQLITE_THREA
f130: 44 53 41 46 45 3d 31 5d 29 20 74 68 65 20 53 51  DSAFE=1]) the SQ
f140: 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c  Lite library wil
f150: 6c 20 69 74 73 65 6c 66 20 73 65 72 69 61 6c 69  l itself seriali
f160: 7a 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20  ze access.** to 
f170: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
f180: 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70  tions] and [prep
f190: 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d  ared statements]
f1a0: 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20   so that the.** 
f1b0: 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20 66  application is f
f1c0: 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 20 73  ree to use the s
f1d0: 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ame [database co
f1e0: 6e 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65  nnection] or the
f1f0: 0a 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 61 72  .** same [prepar
f200: 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e  ed statement] in
f210: 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61   different threa
f220: 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ds at the same t
f230: 69 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20  ime..** See the 
f240: 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
f250: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
f260: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
f270: 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a  formation.</dd>.
f280: 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
f290: 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f  _CONFIG_MALLOC</
f2a0: 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
f2b0: 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73  option takes a s
f2c0: 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77  ingle argument w
f2d0: 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65  hich is a pointe
f2e0: 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61  r to an.** insta
f2f0: 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  nce of the [sqli
f300: 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d  te3_mem_methods]
f310: 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
f320: 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66   argument specif
f330: 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69  ies.** alternati
f340: 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  ve low-level mem
f350: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
f360: 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73  outines to be us
f370: 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  ed in place of.*
f380: 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  * the memory all
f390: 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
f3a0: 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69   built into SQLi
f3b0: 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  te.</dd>.**.** <
f3c0: 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
f3d0: 5f 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a  _GETMALLOC</dt>.
f3e0: 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69  ** <dd>This opti
f3f0: 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c  on takes a singl
f400: 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68  e argument which
f410: 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
f420: 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20   an.** instance 
f430: 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
f440: 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72  mem_methods] str
f450: 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73 71  ucture.  The [sq
f460: 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
f470: 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  s].** structure 
f480: 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74  is filled with t
f490: 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66  he currently def
f4a0: 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ined memory allo
f4b0: 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e  cation routines.
f4c0: 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20  .** This option 
f4d0: 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f  can be used to o
f4e0: 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61  verload the defa
f4f0: 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ult memory alloc
f500: 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65  ation.** routine
f510: 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65 72  s with a wrapper
f520: 20 74 68 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e   that simulation
f530: 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  s memory allocat
f540: 69 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a  ion failure or.*
f550: 2a 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20  * tracks memory 
f560: 75 73 61 67 65 2c 20 66 6f 72 20 65 78 61 6d 70  usage, for examp
f570: 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  le.</dd>.**.** <
f580: 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
f590: 5f 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a  _MEMSTATUS</dt>.
f5a0: 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69  ** <dd>This opti
f5b0: 6f 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c 65 20  on takes single 
f5c0: 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 65  argument of type
f5d0: 20 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 74 65   int, interprete
f5e0: 64 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65  d as a .** boole
f5f0: 61 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 6c 65  an, which enable
f600: 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68  s or disables th
f610: 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20  e collection of 
f620: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
f630: 6e 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 63 73  n .** statistics
f640: 2e 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64 2c  . When disabled,
f650: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53   the following S
f660: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73  QLite interfaces
f670: 20 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d   become .** non-
f680: 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20  operational:.** 
f690: 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e    <ul>.**   <li>
f6a0: 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79   [sqlite3_memory
f6b0: 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c  _used()].**   <l
f6c0: 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  i> [sqlite3_memo
f6d0: 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d 0a  ry_highwater()].
f6e0: 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74  **   <li> [sqlit
f6f0: 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
f700: 69 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20  it()].**   <li> 
f710: 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28  [sqlite3_status(
f720: 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a  )].**   </ul>.**
f730: 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74   </dd>.**.** <dt
f740: 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  >SQLITE_CONFIG_S
f750: 43 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c  CRATCH</dt>.** <
f760: 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73  dd>This option s
f770: 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69  pecifies a stati
f780: 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  c memory buffer 
f790: 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20  that SQLite can 
f7a0: 75 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74  use for.** scrat
f7b0: 63 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72  ch memory.  Ther
f7c0: 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67 75  e are three argu
f7d0: 6d 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74 65  ments:  A pointe
f7e0: 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c  r to the memory,
f7f0: 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20   the.** size of 
f800: 65 61 63 68 20 73 63 72 61 74 63 68 20 62 75 66  each scratch buf
f810: 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 74 68  fer (sz), and th
f820: 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 66 66  e number of buff
f830: 65 72 73 20 28 4e 29 2e 20 20 54 68 65 20 73 7a  ers (N).  The sz
f840: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73  .** argument mus
f850: 74 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 20  t be a multiple 
f860: 6f 66 20 31 36 2e 20 54 68 65 20 73 7a 20 70 61  of 16. The sz pa
f870: 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62  rameter should b
f880: 65 20 61 20 66 65 77 20 62 79 74 65 73 0a 2a 2a  e a few bytes.**
f890: 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
f8a0: 20 61 63 74 75 61 6c 20 73 63 72 61 74 63 68 20   actual scratch 
f8b0: 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 64  space required d
f8c0: 75 65 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72  ue internal over
f8d0: 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72  head..** The fir
f8e0: 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 73  st.** argument s
f8f0: 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20 61  hould point to a
f900: 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  n allocation of 
f910: 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79  at least sz*N by
f920: 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a  tes of memory..*
f930: 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73  * SQLite will us
f940: 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f  e no more than o
f950: 6e 65 20 73 63 72 61 74 63 68 20 62 75 66 66 65  ne scratch buffe
f960: 72 20 61 74 20 6f 6e 63 65 20 70 65 72 20 74 68  r at once per th
f970: 72 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68  read, so.** N sh
f980: 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 74  ould be set to t
f990: 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 78 69  he expected maxi
f9a0: 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  mum number of th
f9b0: 72 65 61 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a  reads.  The sz.*
f9c0: 2a 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75  * parameter shou
f9d0: 6c 64 20 62 65 20 36 20 74 69 6d 65 73 20 74 68  ld be 6 times th
f9e0: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61  e size of the la
f9f0: 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70  rgest database p
fa00: 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72  age size..** Scr
fa10: 61 74 63 68 20 62 75 66 66 65 72 73 20 61 72 65  atch buffers are
fa20: 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66   used as part of
fa30: 20 74 68 65 20 62 74 72 65 65 20 62 61 6c 61 6e   the btree balan
fa40: 63 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49  ce operation.  I
fa50: 66 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 20 62  f.** The btree b
fa60: 61 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 61 64  alancer needs ad
fa70: 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20  ditional memory 
fa80: 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70  beyond what is p
fa90: 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73 63  rovided by.** sc
faa0: 72 61 74 63 68 20 62 75 66 66 65 72 73 20 6f 72  ratch buffers or
fab0: 20 69 66 20 6e 6f 20 73 63 72 61 74 63 68 20 62   if no scratch b
fac0: 75 66 66 65 72 20 73 70 61 63 65 20 69 73 20 73  uffer space is s
fad0: 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 53  pecified, then S
fae0: 51 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f  QLite.** goes to
faf0: 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
fb00: 28 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74 68  ()] to obtain th
fb10: 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64  e memory it need
fb20: 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  s.</dd>.**.** <d
fb30: 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
fb40: 50 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a  PAGECACHE</dt>.*
fb50: 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
fb60: 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74  n specifies a st
fb70: 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66  atic memory buff
fb80: 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 63  er that SQLite c
fb90: 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68  an use for.** th
fba0: 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
fbb0: 63 61 63 68 65 20 77 69 74 68 20 74 68 65 20 64  cache with the d
fbc0: 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68  efault page cach
fbd0: 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e  e implemenation.
fbe0: 20 20 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69    .** This confi
fbf0: 67 75 72 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20  guration should 
fc00: 6e 6f 74 20 62 65 20 75 73 65 64 20 69 66 20 61  not be used if a
fc10: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  n application-de
fc20: 66 69 6e 65 20 70 61 67 65 0a 2a 2a 20 63 61 63  fine page.** cac
fc30: 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
fc40: 6e 20 69 73 20 6c 6f 61 64 65 64 20 75 73 69 6e  n is loaded usin
fc50: 67 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e  g the SQLITE_CON
fc60: 46 49 47 5f 50 43 41 43 48 45 20 6f 70 74 69 6f  FIG_PCACHE optio
fc70: 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  n..** There are 
fc80: 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20  three arguments 
fc90: 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20  to this option: 
fca0: 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
fcb0: 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20  .** memory, the 
fcc0: 73 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 67  size of each pag
fcd0: 65 20 62 75 66 66 65 72 20 28 73 7a 29 2c 20 61  e buffer (sz), a
fce0: 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nd the number of
fcf0: 20 70 61 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 54   pages (N)..** T
fd00: 68 65 20 73 7a 20 61 72 67 75 6d 65 6e 74 20 6d  he sz argument m
fd10: 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f  ust be a power o
fd20: 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31  f two between 51
fd30: 32 20 61 6e 64 20 33 32 37 36 38 2e 20 20 54 68  2 and 32768.  Th
fd40: 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
fd50: 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74  ent should point
fd60: 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f   to an allocatio
fd70: 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a  n of at least sz
fd80: 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  *N bytes of memo
fd90: 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  ry..** SQLite wi
fda0: 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f 72  ll use the memor
fdb0: 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68  y provided by th
fdc0: 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
fdd0: 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 0a   to satisfy its.
fde0: 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20  ** memory needs 
fdf0: 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20  for the first N 
fe00: 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61 64  pages that it ad
fe10: 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49 66  ds to cache.  If
fe20: 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70   additional.** p
fe30: 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79  age cache memory
fe40: 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f 6e   is needed beyon
fe50: 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 64  d what is provid
fe60: 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 6f  ed by this optio
fe70: 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74  n, then.** SQLit
fe80: 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74  e goes to [sqlit
fe90: 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72  e3_malloc()] for
fea0: 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 20   the additional 
feb0: 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a 2a  storage space..*
fec0: 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  * The implementa
fed0: 74 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20 6f  tion might use o
fee0: 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
fef0: 65 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20 68  e N buffers to h
ff00: 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61  old .** memory a
ff10: 63 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d  ccounting inform
ff20: 61 74 69 6f 6e 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a  ation. </dd>.**.
ff30: 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
ff40: 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a  NFIG_HEAP</dt>.*
ff50: 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
ff60: 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74  n specifies a st
ff70: 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66  atic memory buff
ff80: 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 77  er that SQLite w
ff90: 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61  ill use.** for a
ffa0: 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69  ll of its dynami
ffb0: 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  c memory allocat
ffc0: 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64  ion needs beyond
ffd0: 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a   those provided.
ffe0: 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54  ** for by [SQLIT
fff0: 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
10000 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f  ] and [SQLITE_CO
10010 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e  NFIG_PAGECACHE].
10020 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68  .** There are th
10030 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41  ree arguments: A
10040 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
10050 6d 65 6d 6f 72 79 2c 20 74 68 65 20 6e 75 6d 62  memory, the numb
10060 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69  er of.** bytes i
10070 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66  n the memory buf
10080 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e  fer, and the min
10090 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  imum allocation 
100a0 73 69 7a 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65  size.  If.** the
100b0 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 28   first pointer (
100c0 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74  the memory point
100d0 65 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  er) is NULL, the
100e0 6e 20 53 51 4c 69 74 65 20 72 65 76 65 72 74 73  n SQLite reverts
100f0 0a 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74 73  .** to using its
10100 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20   default memory 
10110 61 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20 73  allocator (the s
10120 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69  ystem malloc() i
10130 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a  mplementation),.
10140 2a 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70  ** undoing any p
10150 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20  rior invocation 
10160 6f 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  of [SQLITE_CONFI
10170 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74  G_MALLOC].  If t
10180 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69  he.** memory poi
10190 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  nter is not NULL
101a0 20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 4c   and either [SQL
101b0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
101c0 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54  S3] or.** [SQLIT
101d0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  E_ENABLE_MEMSYS5
101e0 5d 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74  ] are defined, t
101f0 68 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74  hen the alternat
10200 69 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  ive memory.** al
10210 6c 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61 67  locator is engag
10220 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c  ed to handle all
10230 20 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f   of SQLites memo
10240 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65  ry allocation ne
10250 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  eds.</dd>.**.** 
10260 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
10270 47 5f 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20  G_MUTEX</dt>.** 
10280 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
10290 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
102a0 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
102b0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
102c0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
102d0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  the [sqlite3_mut
102e0 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75  ex_methods] stru
102f0 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 67 75  cture.  The argu
10300 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a  ment specifies.*
10310 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f  * alternative lo
10320 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 72 6f  w-level mutex ro
10330 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65  utines to be use
10340 64 20 69 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 68  d in place.** th
10350 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73  e mutex routines
10360 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69   built into SQLi
10370 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  te.</dd>.**.** <
10380 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
10390 5f 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a  _GETMUTEX</dt>.*
103a0 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
103b0 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
103c0 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
103d0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
103e0 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
103f0 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  f the [sqlite3_m
10400 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74  utex_methods] st
10410 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a  ructure.  The.**
10420 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
10430 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75  methods].** stru
10440 63 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64 20  cture is filled 
10450 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  with the current
10460 6c 79 20 64 65 66 69 6e 65 64 20 6d 75 74 65 78  ly defined mutex
10470 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68   routines..** Th
10480 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65  is option can be
10490 20 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61   used to overloa
104a0 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 75  d the default mu
104b0 74 65 78 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  tex allocation.*
104c0 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20  * routines with 
104d0 61 20 77 72 61 70 70 65 72 20 75 73 65 64 20 74  a wrapper used t
104e0 6f 20 74 72 61 63 6b 20 6d 75 74 65 78 20 75 73  o track mutex us
104f0 61 67 65 20 66 6f 72 20 70 65 72 66 6f 72 6d 61  age for performa
10500 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67  nce.** profiling
10510 20 6f 72 20 74 65 73 74 69 6e 67 2c 20 66 6f 72   or testing, for
10520 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a   example.</dd>.*
10530 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
10540 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
10550 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
10560 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74  s option takes t
10570 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61  wo arguments tha
10580 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  t determine the 
10590 64 65 66 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f 72  default.** memor
105a0 79 20 61 6c 6c 63 61 74 69 6f 6e 20 6c 6f 6f 6b  y allcation look
105b0 61 73 69 64 65 20 6f 70 74 69 6d 69 7a 61 74 69  aside optimizati
105c0 6f 6e 2e 20 20 54 68 65 20 66 69 72 73 74 20 61  on.  The first a
105d0 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a  rgument is the.*
105e0 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c  * size of each l
105f0 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20  ookaside buffer 
10600 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 73 65 63  slot and the sec
10610 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ond is the numbe
10620 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 6c  r of.** slots al
10630 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 20  located to each 
10640 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
10650 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ion.</dd>.**.** 
10660 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
10670 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a  G_PCACHE</dt>.**
10680 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
10690 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20   takes a single 
106a0 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69  argument which i
106b0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a  s a pointer to.*
106c0 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63  * an [sqlite3_pc
106d0 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62  ache_methods] ob
106e0 6a 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a 65  ject.  This obje
106f0 63 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65  ct specifies the
10700 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f   interface.** to
10710 20 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63   a custom page c
10720 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ache implementat
10730 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b  ion.  SQLite mak
10740 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  es a copy of the
10750 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75  .** object and u
10760 73 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 20  ses it for page 
10770 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  cache memory all
10780 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a  ocations.</dd>.*
10790 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
107a0 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45  CONFIG_GETPCACHE
107b0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
107c0 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61  s option takes a
107d0 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74   single argument
107e0 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e   which is a poin
107f0 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71  ter to an.** [sq
10800 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
10810 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53  hods] object.  S
10820 51 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 20  QLite copies of 
10830 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70  the current.** p
10840 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d  age cache implem
10850 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68  entation into th
10860 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a  at object.</dd>.
10870 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23  **.** </dl>.*/.#
10880 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
10890 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41  NFIG_SINGLETHREA
108a0 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a  D  1  /* nil */.
108b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
108c0 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41  ONFIG_MULTITHREA
108d0 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f  D   2  /* nil */
108e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
108f0 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45  CONFIG_SERIALIZE
10900 44 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a  D    3  /* nil *
10910 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
10920 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20  _CONFIG_MALLOC  
10930 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69        4  /* sqli
10940 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a  te3_mem_methods*
10950 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
10960 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c  TE_CONFIG_GETMAL
10970 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71  LOC     5  /* sq
10980 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
10990 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  s* */.#define SQ
109a0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41  LITE_CONFIG_SCRA
109b0 54 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20  TCH       6  /* 
109c0 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69  void*, int sz, i
109d0 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nt N */.#define 
109e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
109f0 47 45 43 41 43 48 45 20 20 20 20 20 37 20 20 2f  GECACHE     7  /
10a00 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c  * void*, int sz,
10a10 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e   int N */.#defin
10a20 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
10a30 48 45 41 50 20 20 20 20 20 20 20 20 20 20 38 20  HEAP          8 
10a40 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e   /* void*, int n
10a50 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f  Byte, int min */
10a60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
10a70 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53  CONFIG_MEMSTATUS
10a80 20 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65       9  /* boole
10a90 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  an */.#define SQ
10aa0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45  LITE_CONFIG_MUTE
10ab0 58 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20  X        10  /* 
10ac0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
10ad0 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e  thods* */.#defin
10ae0 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
10af0 47 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20  GETMUTEX     11 
10b00 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   /* sqlite3_mute
10b10 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a  x_methods* */./*
10b20 20 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49   previously SQLI
10b30 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41  TE_CONFIG_CHUNKA
10b40 4c 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73  LLOC 12 which is
10b50 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20   now unused. */ 
10b60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
10b70 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
10b80 20 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 69      13  /* int i
10b90 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  nt */.#define SQ
10ba0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43  LITE_CONFIG_PCAC
10bb0 48 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a 20  HE       14  /* 
10bc0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
10bd0 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69  ethods* */.#defi
10be0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
10bf0 5f 47 45 54 50 43 41 43 48 45 20 20 20 20 31 35  _GETPCACHE    15
10c00 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61    /* sqlite3_pca
10c10 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a  che_methods* */.
10c20 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
10c30 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f   Configuration O
10c40 70 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20  ptions {H10170} 
10c50 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45  <S20000>.** EXPE
10c60 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
10c70 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61  hese constants a
10c80 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  re the available
10c90 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75   integer configu
10ca0 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74  ration options t
10cb0 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61  hat.** can be pa
10cc0 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
10cd0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
10ce0 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63  he [sqlite3_db_c
10cf0 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61  onfig()] interfa
10d00 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f  ce..**.** New co
10d10 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
10d20 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64  ons may be added
10d30 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
10d40 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ses of SQLite..*
10d50 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69  * Existing confi
10d60 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  guration options
10d70 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e   might be discon
10d80 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61  tinued.  Applica
10d90 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20  tions.** should 
10da0 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e  check the return
10db0 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69   code from [sqli
10dc0 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d  te3_db_config()]
10dd0 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
10de0 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77  at.** the call w
10df0 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c  orked.  The [sql
10e00 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29  ite3_db_config()
10e10 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c  ] interface will
10e20 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e   return a.** non
10e30 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64  -zero [error cod
10e40 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69  e] if a disconti
10e50 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72  nued or unsuppor
10e60 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ted configuratio
10e70 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69  n option.** is i
10e80 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64  nvoked..**.** <d
10e90 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  l>.** <dt>SQLITE
10ea0 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  _DBCONFIG_LOOKAS
10eb0 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  IDE</dt>.** <dd>
10ec0 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
10ed0 73 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e  s three addition
10ee0 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61  al arguments tha
10ef0 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  t determine the 
10f00 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d  .** [lookaside m
10f10 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d  emory allocator]
10f20 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66   configuration f
10f30 6f 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  or the [database
10f40 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a   connection]..**
10f50 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
10f60 65 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 70  ent (the third p
10f70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
10f80 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29  ite3_db_config()
10f90 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65  ] is a.** pointe
10fa0 72 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 62 75  r to a memory bu
10fb0 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20  ffer to use for 
10fc0 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
10fd0 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20  .  The first.** 
10fe0 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20  argument may be 
10ff0 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61  NULL in which ca
11000 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61  se SQLite will a
11010 6c 6c 6f 63 61 74 65 20 74 68 65 20 6c 6f 6f 6b  llocate the look
11020 61 73 69 64 65 0a 2a 2a 20 62 75 66 66 65 72 20  aside.** buffer 
11030 69 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 71  itself using [sq
11040 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
11050 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
11060 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20  ument is the.** 
11070 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f  size of each loo
11080 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c  kaside buffer sl
11090 6f 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 64  ot and the third
110a0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
110b0 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c   number of.** sl
110c0 6f 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 6f  ots.  The size o
110d0 66 20 74 68 65 20 62 75 66 66 65 72 20 69 6e 20  f the buffer in 
110e0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
110f0 6e 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 74  nt must be great
11100 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71  er than.** or eq
11110 75 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 75  ual to the produ
11120 63 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64  ct of the second
11130 20 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d   and third argum
11140 65 6e 74 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ents.</dd>.**.**
11150 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e   </dl>.*/.#defin
11160 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49  e SQLITE_DBCONFI
11170 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 31  G_LOOKASIDE    1
11180 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 6e  001  /* void* in
11190 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  t int */.../*.**
111a0 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c   CAPI3REF: Enabl
111b0 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74  e Or Disable Ext
111c0 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f 64  ended Result Cod
111d0 65 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 30  es {H12200} <S10
111e0 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  700>.**.** The s
111f0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
11200 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 72  result_codes() r
11210 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f  outine enables o
11220 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a 2a  r disables the.*
11230 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75  * [extended resu
11240 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 72  lt codes] featur
11250 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 65  e of SQLite. The
11260 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
11270 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 69  .** codes are di
11280 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c  sabled by defaul
11290 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c  t for historical
112a0 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 63   compatibility c
112b0 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a 2a  onsiderations..*
112c0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
112d0 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20 5b  s:.** [H12201] [
112e0 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49 54  H12202].*/.SQLIT
112f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
11300 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c  3_extended_resul
11310 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 2a  t_codes(sqlite3*
11320 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f  , int onoff);../
11330 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c  *.** CAPI3REF: L
11340 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 64  ast Insert Rowid
11350 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 30   {H12220} <S1070
11360 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e  0>.**.** Each en
11370 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65  try in an SQLite
11380 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69   table has a uni
11390 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65  que 64-bit signe
113a0 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79  d.** integer key
113b0 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f 57   called the [ROW
113c0 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20 54  ID | "rowid"]. T
113d0 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77 61  he rowid is alwa
113e0 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20  ys available.** 
113f0 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 64  as an undeclared
11400 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 4f   column named RO
11410 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 4f  WID, OID, or _RO
11420 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 20  WID_ as long as 
11430 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20 61  those.** names a
11440 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 64  re not also used
11450 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 64   by explicitly d
11460 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 2e  eclared columns.
11470 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   If.** the table
11480 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66   has a column of
11490 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20 50   type [INTEGER P
114a0 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65 6e  RIMARY KEY] then
114b0 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20   that column.** 
114c0 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61 73  is another alias
114d0 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 0a   for the rowid..
114e0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
114f0 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b  ne returns the [
11500 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d 6f  rowid] of the mo
11510 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 63  st recent.** suc
11520 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d  cessful [INSERT]
11530 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
11540 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 74  se from the [dat
11550 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
11560 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73  ].** in the firs
11570 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20  t argument.  If 
11580 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49  no successful [I
11590 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 20  NSERT]s.** have 
115a0 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f 6e  ever occurred on
115b0 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63   that database c
115c0 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f 20  onnection, zero 
115d0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
115e0 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 54  ** If an [INSERT
115f0 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20  ] occurs within 
11600 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e 20  a trigger, then 
11610 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74  the [rowid] of t
11620 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 72  he inserted.** r
11630 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  ow is returned b
11640 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  y this routine a
11650 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72  s long as the tr
11660 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67  igger is running
11670 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 68  ..** But once th
11680 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 6e  e trigger termin
11690 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20  ates, the value 
116a0 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73  returned by this
116b0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 65   routine.** reve
116c0 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20  rts to the last 
116d0 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 62  value inserted b
116e0 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 65  efore the trigge
116f0 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41  r fired..**.** A
11700 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 20  n [INSERT] that 
11710 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 63  fails due to a c
11720 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74  onstraint violat
11730 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20  ion is not a.** 
11740 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45  successful [INSE
11750 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74  RT] and does not
11760 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   change the valu
11770 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  e returned by th
11780 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 20  is.** routine.  
11790 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 46  Thus INSERT OR F
117a0 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 49  AIL, INSERT OR I
117b0 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f 52  GNORE, INSERT OR
117c0 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e   ROLLBACK,.** an
117d0 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f 52  d INSERT OR ABOR
117e0 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65  T make no change
117f0 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20  s to the return 
11800 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a  value of this.**
11810 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68   routine when th
11820 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 61  eir insertion fa
11830 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 52  ils.  When INSER
11840 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20  T OR REPLACE.** 
11850 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e  encounters a con
11860 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
11870 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66  n, it does not f
11880 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53  ail.  The.** INS
11890 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f  ERT continues to
118a0 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65   completion afte
118b0 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20  r deleting rows 
118c0 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74  that caused.** t
118d0 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72  he constraint pr
118e0 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20  oblem so INSERT 
118f0 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20  OR REPLACE will 
11900 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a  always change.**
11910 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
11920 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66  e of this interf
11930 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  ace..**.** For t
11940 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74  he purposes of t
11950 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 20  his routine, an 
11960 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e 73  [INSERT] is cons
11970 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 20  idered to.** be 
11980 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e 20  successful even 
11990 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71 75  if it is subsequ
119a0 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  ently rolled bac
119b0 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  k..**.** Require
119c0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 32  ments:.** [H1222
119d0 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a 2a  1] [H12223].**.*
119e0 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20  * If a separate 
119f0 74 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73 20  thread performs 
11a00 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f  a new [INSERT] o
11a10 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 61  n the same.** da
11a20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
11a30 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71 6c  n while the [sql
11a40 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
11a50 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75 6e  _rowid()].** fun
11a60 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67  ction is running
11a70 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65   and thus change
11a80 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72  s the last inser
11a90 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74 68  t [rowid],.** th
11aa0 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  en the value ret
11ab0 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65  urned by [sqlite
11ac0 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
11ad0 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e 70  wid()] is.** unp
11ae0 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6d  redictable and m
11af0 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 65  ight not equal e
11b00 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f 72  ither the old or
11b10 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73 74   the new.** last
11b20 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2e   insert [rowid].
11b30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
11b40 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c  qlite3_int64 sql
11b50 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
11b60 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a 29  _rowid(sqlite3*)
11b70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
11b80 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75 6d  F: Count The Num
11b90 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69  ber Of Rows Modi
11ba0 66 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c 53  fied {H12240} <S
11bb0 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  10600>.**.** Thi
11bc0 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
11bd0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
11be0 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74   database rows t
11bf0 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64  hat were changed
11c00 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64 20  .** or inserted 
11c10 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74 68  or deleted by th
11c20 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
11c30 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73 74  completed SQL st
11c40 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74 68  atement.** on th
11c50 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
11c60 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65  ection] specifie
11c70 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 70  d by the first p
11c80 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e 6c  arameter..** Onl
11c90 79 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 61  y changes that a
11ca0 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65 63  re directly spec
11cb0 69 66 69 65 64 20 62 79 20 74 68 65 20 5b 49 4e  ified by the [IN
11cc0 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d 2c  SERT], [UPDATE],
11cd0 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20  .** or [DELETE] 
11ce0 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f  statement are co
11cf0 75 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61 72  unted.  Auxiliar
11d00 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64  y changes caused
11d10 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 20   by.** triggers 
11d20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e  are not counted.
11d30 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65   Use the [sqlite
11d40 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
11d50 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74  )] function.** t
11d60 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61 6c  o find the total
11d70 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67   number of chang
11d80 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 61  es including cha
11d90 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20 74  nges caused by t
11da0 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41  riggers..**.** A
11db0 20 22 72 6f 77 20 63 68 61 6e 67 65 22 20 69 73   "row change" is
11dc0 20 61 20 63 68 61 6e 67 65 20 74 6f 20 61 20 73   a change to a s
11dd0 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73  ingle row of a s
11de0 69 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 63  ingle table.** c
11df0 61 75 73 65 64 20 62 79 20 61 6e 20 49 4e 53 45  aused by an INSE
11e00 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55  RT, DELETE, or U
11e10 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
11e20 20 20 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20 61    Rows that.** a
11e30 72 65 20 63 68 61 6e 67 65 64 20 61 73 20 73 69  re changed as si
11e40 64 65 20 65 66 66 65 63 74 73 20 6f 66 20 52 45  de effects of RE
11e50 50 4c 41 43 45 20 63 6f 6e 73 74 72 61 69 6e 74  PLACE constraint
11e60 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a 20   resolution,.** 
11e70 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54 20  rollback, ABORT 
11e80 70 72 6f 63 65 73 73 69 6e 67 2c 20 44 52 4f 50  processing, DROP
11e90 20 54 41 42 4c 45 2c 20 6f 72 20 62 79 20 61 6e   TABLE, or by an
11ea0 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 63 68 61  y other.** mecha
11eb0 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20 63 6f 75  nisms do not cou
11ec0 6e 74 20 61 73 20 64 69 72 65 63 74 20 72 6f 77  nt as direct row
11ed0 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20   changes..**.** 
11ee0 41 20 22 74 72 69 67 67 65 72 20 63 6f 6e 74 65  A "trigger conte
11ef0 78 74 22 20 69 73 20 61 20 73 63 6f 70 65 20 6f  xt" is a scope o
11f00 66 20 65 78 65 63 75 74 69 6f 6e 20 74 68 61 74  f execution that
11f10 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a 2a 20 65   begins and.** e
11f20 6e 64 73 20 77 69 74 68 20 74 68 65 20 73 63 72  nds with the scr
11f30 69 70 74 20 6f 66 20 61 20 74 72 69 67 67 65 72  ipt of a trigger
11f40 2e 20 20 4d 6f 73 74 20 53 51 4c 20 73 74 61 74  .  Most SQL stat
11f50 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 65 76  ements are.** ev
11f60 61 6c 75 61 74 65 64 20 6f 75 74 73 69 64 65 20  aluated outside 
11f70 6f 66 20 61 6e 79 20 74 72 69 67 67 65 72 2e 20  of any trigger. 
11f80 20 54 68 69 73 20 69 73 20 74 68 65 20 22 74 6f   This is the "to
11f90 70 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 69 67  p level".** trig
11fa0 67 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 49 66  ger context.  If
11fb0 20 61 20 74 72 69 67 67 65 72 20 66 69 72 65 73   a trigger fires
11fc0 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c 65   from the top le
11fd0 76 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 74 72  vel, a.** new tr
11fe0 69 67 67 65 72 20 63 6f 6e 74 65 78 74 20 69 73  igger context is
11ff0 20 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65   entered for the
12000 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 61   duration of tha
12010 74 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 65 72  t one.** trigger
12020 2e 20 20 53 75 62 74 72 69 67 67 65 72 73 20 63  .  Subtriggers c
12030 72 65 61 74 65 20 73 75 62 63 6f 6e 74 65 78 74  reate subcontext
12040 73 20 66 6f 72 20 74 68 65 69 72 20 64 75 72 61  s for their dura
12050 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c  tion..**.** Call
12060 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78 65  ing [sqlite3_exe
12070 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  c()] or [sqlite3
12080 5f 73 74 65 70 28 29 5d 20 72 65 63 75 72 73 69  _step()] recursi
12090 76 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  vely does.** not
120a0 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 74 72   create a new tr
120b0 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a  igger context..*
120c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
120d0 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  on returns the n
120e0 75 6d 62 65 72 20 6f 66 20 64 69 72 65 63 74 20  umber of direct 
120f0 72 6f 77 20 63 68 61 6e 67 65 73 20 69 6e 20 74  row changes in t
12100 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e  he.** most recen
12110 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  t INSERT, UPDATE
12120 2c 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74  , or DELETE stat
12130 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65  ement within the
12140 20 73 61 6d 65 0a 2a 2a 20 74 72 69 67 67 65 72   same.** trigger
12150 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20   context..**.** 
12160 54 68 75 73 2c 20 77 68 65 6e 20 63 61 6c 6c 65  Thus, when calle
12170 64 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c  d from the top l
12180 65 76 65 6c 2c 20 74 68 69 73 20 66 75 6e 63 74  evel, this funct
12190 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 0a  ion returns the.
121a0 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  ** number of cha
121b0 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74  nges in the most
121c0 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20   recent INSERT, 
121d0 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54  UPDATE, or DELET
121e0 45 0a 2a 2a 20 74 68 61 74 20 61 6c 73 6f 20 6f  E.** that also o
121f0 63 63 75 72 72 65 64 20 61 74 20 74 68 65 20 74  ccurred at the t
12200 6f 70 20 6c 65 76 65 6c 2e 20 20 57 69 74 68 69  op level.  Withi
12210 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20  n the body of a 
12220 74 72 69 67 67 65 72 2c 0a 2a 2a 20 74 68 65 20  trigger,.** the 
12230 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
12240 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20  ) interface can 
12250 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69 6e  be called to fin
12260 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  d the number of.
12270 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  ** changes in th
12280 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
12290 63 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52 54  completed INSERT
122a0 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c  , UPDATE, or DEL
122b0 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ETE.** statement
122c0 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79   within the body
122d0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72 69   of the same tri
122e0 67 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72  gger..** However
122f0 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 72 65 74  , the number ret
12300 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69  urned does not i
12310 6e 63 6c 75 64 65 20 63 68 61 6e 67 65 73 0a 2a  nclude changes.*
12320 2a 20 63 61 75 73 65 64 20 62 79 20 73 75 62 74  * caused by subt
12330 72 69 67 67 65 72 73 20 73 69 6e 63 65 20 74 68  riggers since th
12340 6f 73 65 20 68 61 76 65 20 74 68 65 69 72 20 6f  ose have their o
12350 77 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a  wn context..**.*
12360 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65  * SQLite impleme
12370 6e 74 73 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20  nts the command 
12380 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62  "DELETE FROM tab
12390 6c 65 22 20 77 69 74 68 6f 75 74 20 61 20 57 48  le" without a WH
123a0 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79  ERE clause.** by
123b0 20 64 72 6f 70 70 69 6e 67 20 61 6e 64 20 72 65   dropping and re
123c0 63 72 65 61 74 69 6e 67 20 74 68 65 20 74 61 62  creating the tab
123d0 6c 65 2e 20 20 44 6f 69 6e 67 20 73 6f 20 69 73  le.  Doing so is
123e0 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 61   much faster tha
123f0 6e 20 67 6f 69 6e 67 0a 2a 2a 20 74 68 72 6f 75  n going.** throu
12400 67 68 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20  gh and deleting 
12410 69 6e 64 69 76 69 64 75 61 6c 20 65 6c 65 6d 65  individual eleme
12420 6e 74 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62  nts from the tab
12430 6c 65 2e 20 20 42 65 63 61 75 73 65 20 6f 66 20  le.  Because of 
12440 74 68 69 73 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61  this.** optimiza
12450 74 69 6f 6e 2c 20 74 68 65 20 64 65 6c 65 74 69  tion, the deleti
12460 6f 6e 73 20 69 6e 20 22 44 45 4c 45 54 45 20 46  ons in "DELETE F
12470 52 4f 4d 20 74 61 62 6c 65 22 20 61 72 65 20 6e  ROM table" are n
12480 6f 74 20 72 6f 77 20 63 68 61 6e 67 65 73 20 61  ot row changes a
12490 6e 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62  nd.** will not b
124a0 65 20 63 6f 75 6e 74 65 64 20 62 79 20 74 68 65  e counted by the
124b0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
124c0 28 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 74  () or [sqlite3_t
124d0 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 0a  otal_changes()].
124e0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2c 20 72 65  ** functions, re
124f0 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20  gardless of the 
12500 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
12510 74 73 20 74 68 61 74 20 77 65 72 65 20 6f 72 69  ts that were ori
12520 67 69 6e 61 6c 6c 79 0a 2a 2a 20 69 6e 20 74 68  ginally.** in th
12530 65 20 74 61 62 6c 65 2e 20 20 54 6f 20 67 65 74  e table.  To get
12540 20 61 6e 20 61 63 63 75 72 61 74 65 20 63 6f 75   an accurate cou
12550 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  nt of the number
12560 20 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65 64   of rows deleted
12570 2c 20 75 73 65 0a 2a 2a 20 22 44 45 4c 45 54 45  , use.** "DELETE
12580 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52   FROM table WHER
12590 45 20 31 22 20 69 6e 73 74 65 61 64 2e 20 20 4f  E 1" instead.  O
125a0 72 20 72 65 63 6f 6d 70 69 6c 65 20 75 73 69 6e  r recompile usin
125b0 67 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45  g the.** [SQLITE
125c0 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f  _OMIT_TRUNCATE_O
125d0 50 54 49 4d 49 5a 41 54 49 4f 4e 5d 20 63 6f 6d  PTIMIZATION] com
125e0 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
125f0 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 0a   to disable the.
12600 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  ** optimization 
12610 6f 6e 20 61 6c 6c 20 71 75 65 72 69 65 73 2e 0a  on all queries..
12620 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
12630 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d 20  ts:.** [H12241] 
12640 5b 48 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20 49  [H12243].**.** I
12650 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72  f a separate thr
12660 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65  ead makes change
12670 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61  s on the same da
12680 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
12690 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69  n.** while [sqli
126a0 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69  te3_changes()] i
126b0 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74  s running then t
126c0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
126d0 64 0a 2a 2a 20 69 73 20 75 6e 70 72 65 64 69 63  d.** is unpredic
126e0 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65  table and not me
126f0 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c  aningful..*/.SQL
12700 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
12710 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c 69  te3_changes(sqli
12720 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
12730 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e 75  PI3REF: Total Nu
12740 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64  mber Of Rows Mod
12750 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20 3c  ified {H12260} <
12760 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10600>.**.** Th
12770 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
12780 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
12790 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61  f row changes ca
127a0 75 73 65 64 20 62 79 20 49 4e 53 45 52 54 2c 0a  used by INSERT,.
127b0 2a 2a 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  ** UPDATE or DEL
127c0 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 73  ETE statements s
127d0 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62 61  ince the [databa
127e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77  se connection] w
127f0 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54 68  as opened..** Th
12800 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65 73  e count includes
12810 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72 6f   all changes fro
12820 6d 20 61 6c 6c 20 74 72 69 67 67 65 72 20 63 6f  m all trigger co
12830 6e 74 65 78 74 73 2e 20 20 48 6f 77 65 76 65 72  ntexts.  However
12840 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 64  ,.** the count d
12850 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20  oes not include 
12860 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f 20  changes used to 
12870 69 6d 70 6c 65 6d 65 6e 74 20 52 45 50 4c 41 43  implement REPLAC
12880 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a  E constraints,.*
12890 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f  * do rollbacks o
128a0 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69  r ABORT processi
128b0 6e 67 2c 20 6f 72 20 44 52 4f 50 20 74 61 62 6c  ng, or DROP tabl
128c0 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a  e processing..**
128d0 20 54 68 65 20 63 68 61 6e 67 65 73 20 61 72 65   The changes are
128e0 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e   counted as soon
128f0 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e   as the statemen
12900 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65  t that makes the
12910 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65  m is.** complete
12920 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 61 74  d (when the stat
12930 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 20  ement handle is 
12940 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74  passed to [sqlit
12950 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a  e3_reset()] or.*
12960 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  * [sqlite3_final
12970 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53  ize()])..**.** S
12980 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73  QLite implements
12990 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22 44 45   the command "DE
129a0 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 22  LETE FROM table"
129b0 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52 45   without a WHERE
129c0 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 20 64 72   clause.** by dr
129d0 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 63 72 65  opping and recre
129e0 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e  ating the table.
129f0 20 20 28 54 68 69 73 20 69 73 20 6d 75 63 68 20    (This is much 
12a00 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f 69 6e  faster than goin
12a10 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61 6e 64  g.** through and
12a20 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69 76 69   deleting indivi
12a30 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20 66 72  dual elements fr
12a40 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 29 20 20  om the table.)  
12a50 42 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 0a  Because of this.
12a60 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c  ** optimization,
12a70 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 73 20 69   the deletions i
12a80 6e 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74  n "DELETE FROM t
12a90 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20 72 6f  able" are not ro
12aa0 77 20 63 68 61 6e 67 65 73 20 61 6e 64 0a 2a 2a  w changes and.**
12ab0 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 75   will not be cou
12ac0 6e 74 65 64 20 62 79 20 74 68 65 20 73 71 6c 69  nted by the sqli
12ad0 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 72  te3_changes() or
12ae0 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f   [sqlite3_total_
12af0 63 68 61 6e 67 65 73 28 29 5d 0a 2a 2a 20 66 75  changes()].** fu
12b00 6e 63 74 69 6f 6e 73 2c 20 72 65 67 61 72 64 6c  nctions, regardl
12b10 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ess of the numbe
12b20 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 74 68  r of elements th
12b30 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e 61 6c  at were original
12b40 6c 79 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62  ly.** in the tab
12b50 6c 65 2e 20 20 54 6f 20 67 65 74 20 61 6e 20 61  le.  To get an a
12b60 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20 6f 66  ccurate count of
12b70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
12b80 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75 73 65  ows deleted, use
12b90 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52 4f 4d  .** "DELETE FROM
12ba0 20 74 61 62 6c 65 20 57 48 45 52 45 20 31 22 20   table WHERE 1" 
12bb0 69 6e 73 74 65 61 64 2e 20 20 20 4f 72 20 72 65  instead.   Or re
12bc0 63 6f 6d 70 69 6c 65 20 75 73 69 6e 67 20 74 68  compile using th
12bd0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4d 49  e.** [SQLITE_OMI
12be0 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d  T_TRUNCATE_OPTIM
12bf0 49 5a 41 54 49 4f 4e 5d 20 63 6f 6d 70 69 6c 65  IZATION] compile
12c00 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 6f 20  -time option to 
12c10 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 6f  disable the.** o
12c20 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61  ptimization on a
12c30 6c 6c 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a  ll queries..**.*
12c40 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b  * See also the [
12c50 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
12c60 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  )] interface..**
12c70 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
12c80 3a 0a 2a 2a 20 5b 48 31 32 32 36 31 5d 20 5b 48  :.** [H12261] [H
12c90 31 32 32 36 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20  12263].**.** If 
12ca0 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61  a separate threa
12cb0 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20  d makes changes 
12cc0 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  on the same data
12cd0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
12ce0 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65  ** while [sqlite
12cf0 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
12d00 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68  )] is running th
12d10 65 6e 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  en the value.** 
12d20 72 65 74 75 72 6e 65 64 20 69 73 20 75 6e 70 72  returned is unpr
12d30 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f  edictable and no
12d40 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f  t meaningful..*/
12d50 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
12d60 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
12d70 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b  anges(sqlite3*);
12d80 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
12d90 3a 20 49 6e 74 65 72 72 75 70 74 20 41 20 4c 6f  : Interrupt A Lo
12da0 6e 67 2d 52 75 6e 6e 69 6e 67 20 51 75 65 72 79  ng-Running Query
12db0 20 7b 48 31 32 32 37 30 7d 20 3c 53 33 30 35 30   {H12270} <S3050
12dc0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  0>.**.** This fu
12dd0 6e 63 74 69 6f 6e 20 63 61 75 73 65 73 20 61 6e  nction causes an
12de0 79 20 70 65 6e 64 69 6e 67 20 64 61 74 61 62 61  y pending databa
12df0 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  se operation to 
12e00 61 62 6f 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74  abort and.** ret
12e10 75 72 6e 20 61 74 20 69 74 73 20 65 61 72 6c 69  urn at its earli
12e20 65 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e  est opportunity.
12e30 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
12e40 20 74 79 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61   typically.** ca
12e50 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65  lled in response
12e60 20 74 6f 20 61 20 75 73 65 72 20 61 63 74 69 6f   to a user actio
12e70 6e 20 73 75 63 68 20 61 73 20 70 72 65 73 73 69  n such as pressi
12e80 6e 67 20 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f  ng "Cancel".** o
12e90 72 20 43 74 72 6c 2d 43 20 77 68 65 72 65 20 74  r Ctrl-C where t
12ea0 68 65 20 75 73 65 72 20 77 61 6e 74 73 20 61 20  he user wants a 
12eb0 6c 6f 6e 67 20 71 75 65 72 79 20 6f 70 65 72 61  long query opera
12ec0 74 69 6f 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20  tion to halt.** 
12ed0 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a  immediately..**.
12ee0 2a 2a 20 49 74 20 69 73 20 73 61 66 65 20 74 6f  ** It is safe to
12ef0 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
12f00 6e 65 20 66 72 6f 6d 20 61 20 74 68 72 65 61 64  ne from a thread
12f10 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
12f20 74 68 65 0a 2a 2a 20 74 68 72 65 61 64 20 74 68  the.** thread th
12f30 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  at is currently 
12f40 72 75 6e 6e 69 6e 67 20 74 68 65 20 64 61 74 61  running the data
12f50 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20  base operation. 
12f60 20 42 75 74 20 69 74 0a 2a 2a 20 69 73 20 6e 6f   But it.** is no
12f70 74 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74  t safe to call t
12f80 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68  his routine with
12f90 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   a [database con
12fa0 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a  nection] that.**
12fb0 20 69 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69   is closed or mi
12fc0 67 68 74 20 63 6c 6f 73 65 20 62 65 66 6f 72 65  ght close before
12fd0 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
12fe0 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a  pt() returns..**
12ff0 0a 2a 2a 20 49 66 20 61 6e 20 53 51 4c 20 6f 70  .** If an SQL op
13000 65 72 61 74 69 6f 6e 20 69 73 20 76 65 72 79 20  eration is very 
13010 6e 65 61 72 6c 79 20 66 69 6e 69 73 68 65 64 20  nearly finished 
13020 61 74 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e  at the time when
13030 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  .** sqlite3_inte
13040 72 72 75 70 74 28 29 20 69 73 20 63 61 6c 6c 65  rrupt() is calle
13050 64 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74  d, then it might
13060 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f 70 70   not have an opp
13070 6f 72 74 75 6e 69 74 79 0a 2a 2a 20 74 6f 20 62  ortunity.** to b
13080 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 6e  e interrupted an
13090 64 20 6d 69 67 68 74 20 63 6f 6e 74 69 6e 75 65  d might continue
130a0 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a   to completion..
130b0 2a 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 6f 70 65  **.** An SQL ope
130c0 72 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69  ration that is i
130d0 6e 74 65 72 72 75 70 74 65 64 20 77 69 6c 6c 20  nterrupted will 
130e0 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49  return [SQLITE_I
130f0 4e 54 45 52 52 55 50 54 5d 2e 0a 2a 2a 20 49 66  NTERRUPT]..** If
13100 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 65 64   the interrupted
13110 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69   SQL operation i
13120 73 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44  s an INSERT, UPD
13130 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a  ATE, or DELETE.*
13140 2a 20 74 68 61 74 20 69 73 20 69 6e 73 69 64 65  * that is inside
13150 20 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61   an explicit tra
13160 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74  nsaction, then t
13170 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61  he entire transa
13180 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65  ction.** will be
13190 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74   rolled back aut
131a0 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a  omatically..**.*
131b0 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  * A call to sqli
131c0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
131d0 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e  has no effect on
131e0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a   SQL statements.
131f0 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 72  ** that are star
13200 74 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65  ted after sqlite
13210 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 65  3_interrupt() re
13220 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  turns..**.** Req
13230 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
13240 31 32 32 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a  12271] [H12272].
13250 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74  **.** If the dat
13260 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13270 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73   closes while [s
13280 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
13290 28 29 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e  ()].** is runnin
132a0 67 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67  g then bad thing
132b0 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61  s will likely ha
132c0 70 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ppen..*/.SQLITE_
132d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
132e0 5f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74  _interrupt(sqlit
132f0 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  e3*);../*.** CAP
13300 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65  I3REF: Determine
13310 20 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65   If An SQL State
13320 6d 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65  ment Is Complete
13330 20 7b 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30   {H10510} <S7020
13340 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
13350 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 66  outines are usef
13360 75 6c 20 66 6f 72 20 63 6f 6d 6d 61 6e 64 2d 6c  ul for command-l
13370 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65 74  ine input to det
13380 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a 2a  ermine if the.**
13390 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
133a0 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 74 6f  ed text seems to
133b0 20 66 6f 72 6d 20 63 6f 6d 70 6c 65 74 65 20 61   form complete a
133c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f   SQL statement o
133d0 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f 6e  r.** if addition
133e0 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65 64  al input is need
133f0 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69 6e  ed before sendin
13400 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f 0a  g the text into.
13410 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70 61  ** SQLite for pa
13420 72 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72 6f  rsing.  These ro
13430 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 72  utines return tr
13440 75 65 20 69 66 20 74 68 65 20 69 6e 70 75 74 20  ue if the input 
13450 73 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72  string.** appear
13460 73 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65  s to be a comple
13470 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  te SQL statement
13480 2e 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69  .  A statement i
13490 73 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a  s judged to be.*
134a0 2a 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74  * complete if it
134b0 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d   ends with a sem
134c0 69 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64  icolon token and
134d0 20 69 73 20 6e 6f 74 20 61 20 66 72 61 67 6d 65   is not a fragme
134e0 6e 74 20 6f 66 20 61 0a 2a 2a 20 43 52 45 41 54  nt of a.** CREAT
134f0 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d  E TRIGGER statem
13500 65 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e 73  ent.  Semicolons
13510 20 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 64   that are embedd
13520 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 72  ed within.** str
13530 69 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 20  ing literals or 
13540 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65  quoted identifie
13550 72 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65  r names or comme
13560 6e 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 69  nts are not.** i
13570 6e 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e  ndependent token
13580 73 20 28 74 68 65 79 20 61 72 65 20 70 61 72 74  s (they are part
13590 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e   of the token in
135a0 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65 0a   which they are.
135b0 2a 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64  ** embedded) and
135c0 20 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75   thus do not cou
135d0 6e 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e  nt as a statemen
135e0 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a  t terminator..**
135f0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
13600 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20  es do not parse 
13610 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
13620 74 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20  ts thus.** will 
13630 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 61  not detect synta
13640 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65  ctically incorre
13650 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65  ct SQL..**.** Re
13660 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30  quirements: [H10
13670 35 31 31 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a  511] [H10512].**
13680 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f  .** The input to
13690 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65   [sqlite3_comple
136a0 74 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20  te()] must be a 
136b0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a  zero-terminated.
136c0 2a 2a 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e  ** UTF-8 string.
136d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  .**.** The input
136e0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d   to [sqlite3_com
136f0 70 6c 65 74 65 31 36 28 29 5d 20 6d 75 73 74 20  plete16()] must 
13700 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  be a zero-termin
13710 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73  ated.** UTF-16 s
13720 74 72 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 20  tring in native 
13730 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53  byte order..*/.S
13740 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
13750 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63  lite3_complete(c
13760 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 29 3b  onst char *sql);
13770 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
13780 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
13790 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  16(const void *s
137a0 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ql);../*.** CAPI
137b0 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41  3REF: Register A
137c0 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e   Callback To Han
137d0 64 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20  dle SQLITE_BUSY 
137e0 45 72 72 6f 72 73 20 7b 48 31 32 33 31 30 7d 20  Errors {H12310} 
137f0 3c 53 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S40400>.**.** T
13800 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
13810 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63   a callback func
13820 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20  tion that might 
13830 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65  be invoked whene
13840 76 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70  ver.** an attemp
13850 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65  t is made to ope
13860 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62  n a database tab
13870 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20  le that another 
13880 74 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f  thread.** or pro
13890 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e  cess has locked.
138a0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75  .**.** If the bu
138b0 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e  sy callback is N
138c0 55 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54  ULL, then [SQLIT
138d0 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49  E_BUSY] or [SQLI
138e0 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44  TE_IOERR_BLOCKED
138f0 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  ].** is returned
13900 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 6f   immediately upo
13910 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74  n encountering t
13920 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20  he lock. If the 
13930 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  busy callback.**
13940 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
13950 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  en the callback 
13960 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20  will be invoked 
13970 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e  with two argumen
13980 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ts..**.** The fi
13990 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
139a0 74 68 65 20 68 61 6e 64 6c 65 72 20 69 73 20 61  the handler is a
139b0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69   copy of the voi
139c0 64 2a 20 70 6f 69 6e 74 65 72 20 77 68 69 63 68  d* pointer which
139d0 0a 2a 2a 20 69 73 20 74 68 65 20 74 68 69 72 64  .** is the third
139e0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
139f0 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
13a00 72 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  r().  The second
13a10 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20   argument to.** 
13a20 74 68 65 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c  the handler call
13a30 62 61 63 6b 20 69 73 20 74 68 65 20 6e 75 6d 62  back is the numb
13a40 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74  er of times that
13a50 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
13a60 72 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e  r has.** been in
13a70 76 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c  voked for this l
13a80 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 49  ocking event.  I
13a90 66 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61  f the.** busy ca
13aa0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30  llback returns 0
13ab0 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69  , then no additi
13ac0 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72  onal attempts ar
13ad0 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63  e made to.** acc
13ae0 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
13af0 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53   and [SQLITE_BUS
13b00 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f  Y] or [SQLITE_IO
13b10 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20  ERR_BLOCKED] is 
13b20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20  returned..** If 
13b30 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  the callback ret
13b40 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
13b50 68 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65  hen another atte
13b60 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74  mpt.** is made t
13b70 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62  o open the datab
13b80 61 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20  ase for reading 
13b90 61 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72 65  and the cycle re
13ba0 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  peats..**.** The
13bb0 20 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62   presence of a b
13bc0 75 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73  usy handler does
13bd0 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74   not guarantee t
13be0 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 69  hat it will be i
13bf0 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74  nvoked.** when t
13c00 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e  here is lock con
13c10 74 65 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69  tention. If SQLi
13c20 74 65 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68  te determines th
13c30 61 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  at invoking the 
13c40 62 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20  busy.** handler 
13c50 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 20  could result in 
13c60 61 20 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77  a deadlock, it w
13c70 69 6c 6c 20 67 6f 20 61 68 65 61 64 20 61 6e 64  ill go ahead and
13c80 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
13c90 42 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c  BUSY].** or [SQL
13ca0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
13cb0 44 5d 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e  D] instead of in
13cc0 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20  voking the busy 
13cd0 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73  handler..** Cons
13ce0 69 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20  ider a scenario 
13cf0 77 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73  where one proces
13d00 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72  s is holding a r
13d10 65 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a  ead lock that.**
13d20 20 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f   it is trying to
13d30 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65   promote to a re
13d40 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a  served lock and.
13d50 2a 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63  ** a second proc
13d60 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  ess is holding a
13d70 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74   reserved lock t
13d80 68 61 74 20 69 74 20 69 73 20 74 72 79 69 6e 67  hat it is trying
13d90 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  .** to promote t
13da0 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  o an exclusive l
13db0 6f 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20  ock.  The first 
13dc0 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70  process cannot p
13dd0 72 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73  roceed.** becaus
13de0 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20  e it is blocked 
13df0 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e  by the second an
13e00 64 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f  d the second pro
13e10 63 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70  cess cannot.** p
13e20 72 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 69  roceed because i
13e30 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
13e40 74 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 62  the first.  If b
13e50 6f 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a  oth processes.**
13e60 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   invoke the busy
13e70 20 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68   handlers, neith
13e80 65 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79  er will make any
13e90 20 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72   progress.  Ther
13ea0 65 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65  efore,.** SQLite
13eb0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
13ec0 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66  _BUSY] for the f
13ed0 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f  irst process, ho
13ee0 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a  ping that this.*
13ef0 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68  * will induce th
13f00 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20  e first process 
13f10 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 72  to release its r
13f20 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c  ead lock and all
13f30 6f 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  ow.** the second
13f40 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63   process to proc
13f50 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  eed..**.** The d
13f60 65 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c  efault busy call
13f70 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a  back is NULL..**
13f80 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
13f90 42 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63  BUSY] error is c
13fa0 6f 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c  onverted to [SQL
13fb0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
13fc0 44 5d 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74  D].** when SQLit
13fd0 65 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64  e is in the midd
13fe0 6c 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72  le of a large tr
13ff0 61 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20  ansaction where 
14000 61 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67  all the.** chang
14010 65 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20  es will not fit 
14020 69 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  into the in-memo
14030 72 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74  ry cache.  SQLit
14040 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64  e will.** alread
14050 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45  y hold a RESERVE
14060 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  D lock on the da
14070 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74  tabase file, but
14080 20 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20   it needs.** to 
14090 70 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63  promote this loc
140a0 6b 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73  k to EXCLUSIVE s
140b0 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 70  o that it can sp
140c0 69 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67  ill cache.** pag
140d0 65 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  es into the data
140e0 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75  base file withou
140f0 74 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72  t harm to concur
14100 72 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e  rent.** readers.
14110 20 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c    If it is unabl
14120 65 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65  e to promote the
14130 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20   lock, then the 
14140 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63  in-memory.** cac
14150 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20  he will be left 
14160 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
14170 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20  nt state and so 
14180 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64  the error.** cod
14190 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72  e is promoted fr
141a0 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c  om the relativel
141b0 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45  y benign [SQLITE
141c0 5f 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65  _BUSY] to.** the
141d0 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51   more severe [SQ
141e0 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
141f0 45 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72  ED].  This error
14200 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a   code promotion.
14210 2a 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74  ** forces an aut
14220 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20  omatic rollback 
14230 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20  of the changes. 
14240 20 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68   See the.** <a h
14250 72 65 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 69  ref="/cvstrac/wi
14260 6b 69 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46  ki?p=CorruptionF
14270 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f  ollowingBusyErro
14280 72 22 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f  r">.** Corruptio
14290 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72  nFollowingBusyEr
142a0 72 6f 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67  ror</a> wiki pag
142b0 65 20 66 6f 72 20 61 20 64 69 73 63 75 73 73 69  e for a discussi
142c0 6f 6e 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69  on of why.** thi
142d0 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a  s is important..
142e0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20  **.** There can 
142f0 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65  only be a single
14300 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 65   busy handler de
14310 66 69 6e 65 64 20 66 6f 72 20 65 61 63 68 0a 2a  fined for each.*
14320 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
14330 65 63 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e  ection].  Settin
14340 67 20 61 20 6e 65 77 20 62 75 73 79 20 68 61 6e  g a new busy han
14350 64 6c 65 72 20 63 6c 65 61 72 73 20 61 6e 79 0a  dler clears any.
14360 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65  ** previously se
14370 74 20 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65  t handler.  Note
14380 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73   that calling [s
14390 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
143a0 6f 75 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61  out()].** will a
143b0 6c 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72  lso set or clear
143c0 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
143d0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73  r..**.** The bus
143e0 79 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c  y callback shoul
143f0 64 20 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61  d not take any a
14400 63 74 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64  ctions which mod
14410 69 66 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  ify the.** datab
14420 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
14430 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
14440 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41  busy handler.  A
14450 6e 79 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a  ny such actions.
14460 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64  ** result in und
14470 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e  efined behavior.
14480 0a 2a 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d  .** .** Requirem
14490 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31  ents:.** [H12311
144a0 5d 20 5b 48 31 32 33 31 32 5d 20 5b 48 31 32 33  ] [H12312] [H123
144b0 31 34 5d 20 5b 48 31 32 33 31 36 5d 20 5b 48 31  14] [H12316] [H1
144c0 32 33 31 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75  2318].**.** A bu
144d0 73 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20  sy handler must 
144e0 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61  not close the da
144f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14500 6e 0a 2a 2a 20 6f 72 20 5b 70 72 65 70 61 72 65  n.** or [prepare
14510 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61  d statement] tha
14520 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75  t invoked the bu
14530 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53  sy handler..*/.S
14540 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
14550 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
14560 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  er(sqlite3*, int
14570 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20  (*)(void*,int), 
14580 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  void*);../*.** C
14590 41 50 49 33 52 45 46 3a 20 53 65 74 20 41 20 42  API3REF: Set A B
145a0 75 73 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32  usy Timeout {H12
145b0 33 34 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a  340} <S40410>.**
145c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
145d0 20 73 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33   sets a [sqlite3
145e0 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20  _busy_handler | 
145f0 62 75 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68  busy handler] th
14600 61 74 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72  at sleeps.** for
14610 20 61 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f   a specified amo
14620 75 6e 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e  unt of time when
14630 20 61 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b   a table is lock
14640 65 64 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72  ed.  The handler
14650 0a 2a 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d  .** will sleep m
14660 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e  ultiple times un
14670 74 69 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73  til at least "ms
14680 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f  " milliseconds o
14690 66 20 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61  f sleeping.** ha
146a0 76 65 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20  ve accumulated. 
146b0 7b 48 31 32 33 34 33 7d 20 41 66 74 65 72 20 22  {H12343} After "
146c0 6d 73 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  ms" milliseconds
146d0 20 6f 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a   of sleeping,.**
146e0 20 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74   the handler ret
146f0 75 72 6e 73 20 30 20 77 68 69 63 68 20 63 61 75  urns 0 which cau
14700 73 65 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ses [sqlite3_ste
14710 70 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a  p()] to return.*
14720 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  * [SQLITE_BUSY] 
14730 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  or [SQLITE_IOERR
14740 5f 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a  _BLOCKED]..**.**
14750 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f   Calling this ro
14760 75 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72  utine with an ar
14770 67 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e  gument less than
14780 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72   or equal to zer
14790 6f 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61  o.** turns off a
147a0 6c 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73  ll busy handlers
147b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61  ..**.** There ca
147c0 6e 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67  n only be a sing
147d0 6c 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  le busy handler 
147e0 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72  for a particular
147f0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f  .** [database co
14800 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e  nnection] any an
14810 79 20 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20  y given moment. 
14820 20 49 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79   If another busy
14830 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20   handler.** was 
14840 64 65 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20  defined  (using 
14850 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61  [sqlite3_busy_ha
14860 6e 64 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20  ndler()]) prior 
14870 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68  to calling.** th
14880 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74  is routine, that
14890 20 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64   other busy hand
148a0 6c 65 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a  ler is cleared..
148b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
148c0 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20  ts:.** [H12341] 
148d0 5b 48 31 32 33 34 33 5d 20 5b 48 31 32 33 34 34  [H12343] [H12344
148e0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
148f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
14900 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33  _timeout(sqlite3
14910 2a 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a  *, int ms);../*.
14920 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e  ** CAPI3REF: Con
14930 76 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65  venience Routine
14940 73 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75  s For Running Qu
14950 65 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c  eries {H12370} <
14960 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65  S10000>.**.** De
14970 66 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72  finition: A <b>r
14980 65 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20  esult table</b> 
14990 69 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73  is memory data s
149a0 74 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64  tructure created
149b0 20 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69   by the.** [sqli
149c0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d  te3_get_table()]
149d0 20 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72   interface.  A r
149e0 65 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f  esult table reco
149f0 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c  rds the.** compl
14a00 65 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74  ete query result
14a10 73 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f  s from one or mo
14a20 72 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a  re queries..**.*
14a30 2a 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63  * The table conc
14a40 65 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e  eptually has a n
14a50 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e  umber of rows an
14a60 64 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a  d columns.  But.
14a70 2a 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73  ** these numbers
14a80 20 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66   are not part of
14a90 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c   the result tabl
14aa0 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65  e itself.  These
14ab0 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20  .** numbers are 
14ac0 6f 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74  obtained separat
14ad0 65 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74  ely.  Let N be t
14ae0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
14af0 73 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68  s.** and M be th
14b00 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
14b10 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73  mns..**.** A res
14b20 75 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20  ult table is an 
14b30 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72  array of pointer
14b40 73 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  s to zero-termin
14b50 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e  ated UTF-8 strin
14b60 67 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  gs..** There are
14b70 20 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74   (N+1)*M element
14b80 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20  s in the array. 
14b90 20 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69   The first M poi
14ba0 6e 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74  nters point.** t
14bb0 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  o zero-terminate
14bc0 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20  d strings that  
14bd0 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65  contain the name
14be0 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73  s of the columns
14bf0 2e 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69  ..** The remaini
14c00 6e 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70  ng entries all p
14c10 6f 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65  oint to query re
14c20 73 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c  sults.  NULL val
14c30 75 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e  ues result.** in
14c40 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20   NULL pointers. 
14c50 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65   All other value
14c60 73 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55  s are in their U
14c70 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  TF-8 zero-termin
14c80 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72  ated.** string r
14c90 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73  epresentation as
14ca0 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
14cb0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
14cc0 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65  t()]..**.** A re
14cd0 73 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74  sult table might
14ce0 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20   consist of one 
14cf0 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61  or more memory a
14d00 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49  llocations..** I
14d10 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f  t is not safe to
14d20 20 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74   pass a result t
14d30 61 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f  able directly to
14d40 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
14d50 5d 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74  ]..** A result t
14d60 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64  able should be d
14d70 65 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67  eallocated using
14d80 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74   [sqlite3_free_t
14d90 61 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41  able()]..**.** A
14da0 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20  s an example of 
14db0 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
14dc0 20 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65   format, suppose
14dd0 20 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a   a query result.
14de0 2a 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  ** is as follows
14df0 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
14e00 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20  ote><pre>.**    
14e10 20 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20      Name        
14e20 7c 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20  | Age.**        
14e30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14e40 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20  -------.**      
14e50 20 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20    Alice       | 
14e60 34 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62  43.**        Bob
14e70 20 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a           | 28.**
14e80 20 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20          Cindy   
14e90 20 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72      | 21.** </pr
14ea0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
14eb0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
14ec0 74 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32  two column (M==2
14ed0 29 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73  ) and three rows
14ee0 20 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74   (N==3).  Thus t
14ef0 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62  he.** result tab
14f00 6c 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73  le has 8 entries
14f10 2e 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72  .  Suppose the r
14f20 65 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73  esult table is s
14f30 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61  tored.** in an a
14f40 72 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73  rray names azRes
14f50 75 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73  ult.  Then azRes
14f60 75 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63  ult holds this c
14f70 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62  ontent:.**.** <b
14f80 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
14f90 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
14fa0 6c 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d  lt&#91;0] = "Nam
14fb0 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a  e";.**        az
14fc0 52 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20  Result&#91;1] = 
14fd0 22 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20  "Age";.**       
14fe0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d   azResult&#91;2]
14ff0 20 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20   = "Alice";.**  
15000 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
15010 39 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a  91;3] = "43";.**
15020 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
15030 26 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b  &#91;4] = "Bob";
15040 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
15050 75 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38  ult&#91;5] = "28
15060 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
15070 65 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22  esult&#91;6] = "
15080 43 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20  Cindy";.**      
15090 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37    azResult&#91;7
150a0 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70  ] = "21";.** </p
150b0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
150c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
150d0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66  e3_get_table() f
150e0 75 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65  unction evaluate
150f0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  s one or more.**
15100 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72   semicolon-separ
15110 61 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  ated SQL stateme
15120 6e 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d  nts in the zero-
15130 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
15140 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74  .** string of it
15150 73 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  s 2nd parameter.
15160 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72    It returns a r
15170 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74  esult table to t
15180 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69  he.** pointer gi
15190 76 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70  ven in its 3rd p
151a0 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  arameter..**.** 
151b0 41 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e  After the callin
151c0 67 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66  g function has f
151d0 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68  inished using th
151e0 65 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f  e result, it sho
151f0 75 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20  uld.** pass the 
15200 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  pointer to the r
15210 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73  esult table to s
15220 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
15230 65 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a  e() in order to.
15240 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d  ** release the m
15250 65 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d  emory that was m
15260 61 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73  alloced.  Becaus
15270 65 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65  e of the way the
15280 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  .** [sqlite3_mal
15290 6c 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77  loc()] happens w
152a0 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65  ithin sqlite3_ge
152b0 74 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63  t_table(), the c
152c0 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
152d0 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20  on must not try 
152e0 74 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33  to call [sqlite3
152f0 5f 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c  _free()] directl
15300 79 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c  y.  Only.** [sql
15310 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
15320 29 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65  )] is able to re
15330 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79  lease the memory
15340 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61   properly and sa
15350 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fely..**.** The 
15360 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
15370 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  e() interface is
15380 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
15390 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64  a wrapper around
153a0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65  .** [sqlite3_exe
153b0 63 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74  c()].  The sqlit
153c0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72  e3_get_table() r
153d0 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20  outine does not 
153e0 68 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74  have access.** t
153f0 6f 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64  o any internal d
15400 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f  ata structures o
15410 66 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73  f SQLite.  It us
15420 65 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c  es only the publ
15430 69 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ic.** interface 
15440 64 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41  defined here.  A
15450 73 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c  s a consequence,
15460 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63   errors that occ
15470 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61  ur in the.** wra
15480 70 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69  pper layer outsi
15490 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e  de of the intern
154a0 61 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  al [sqlite3_exec
154b0 28 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74  ()] call are not
154c0 0a 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e  .** reflected in
154d0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
154e0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72  s to [sqlite3_er
154f0 72 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c  rcode()] or [sql
15500 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a  ite3_errmsg()]..
15510 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
15520 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20  ts:.** [H12371] 
15530 5b 48 31 32 33 37 33 5d 20 5b 48 31 32 33 37 34  [H12373] [H12374
15540 5d 20 5b 48 31 32 33 37 36 5d 20 5b 48 31 32 33  ] [H12376] [H123
15550 37 39 5d 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a  79] [H12382].*/.
15560 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
15570 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
15580 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
15590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20            /* An 
155a0 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f  open database */
155b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
155c0 53 71 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20  Sql,     /* SQL 
155d0 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20  to be evaluated 
155e0 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a  */.  char ***paz
155f0 52 65 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65  Result,    /* Re
15600 73 75 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65  sults of the que
15610 72 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52  ry */.  int *pnR
15620 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ow,           /*
15630 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c   Number of resul
15640 74 20 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68  t rows written h
15650 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ere */.  int *pn
15660 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f  Column,        /
15670 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75  * Number of resu
15680 6c 74 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74  lt columns writt
15690 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61  en here */.  cha
156a0 72 20 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20  r **pzErrmsg    
156b0 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20     /* Error msg 
156c0 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
156d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
156e0 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  id sqlite3_free_
156f0 74 61 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73  table(char **res
15700 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ult);../*.** CAP
15710 49 33 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64  I3REF: Formatted
15720 20 53 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67   String Printing
15730 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34   Functions {H174
15740 30 30 7d 20 3c 53 37 30 30 30 30 3e 3c 53 32 30  00} <S70000><S20
15750 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  000>.**.** These
15760 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 77 6f   routines are wo
15770 72 6b 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 20  rkalikes of the 
15780 22 70 72 69 6e 74 66 28 29 22 20 66 61 6d 69 6c  "printf()" famil
15790 79 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  y of functions.*
157a0 2a 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64  * from the stand
157b0 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a  ard C library..*
157c0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
157d0 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73  _mprintf() and s
157e0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
157f0 29 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65  ) routines write
15800 20 74 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74   their.** result
15810 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  s into memory ob
15820 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
15830 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a  ite3_malloc()]..
15840 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 72  ** The strings r
15850 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65  eturned by these
15860 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 68   two routines sh
15870 6f 75 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61  ould be.** relea
15880 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  sed by [sqlite3_
15890 66 72 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 72  free()].  Both r
158a0 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 61  outines return a
158b0 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  .** NULL pointer
158c0 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   if [sqlite3_mal
158d0 6c 6f 63 28 29 5d 20 69 73 20 75 6e 61 62 6c 65  loc()] is unable
158e0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f   to allocate eno
158f0 75 67 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f  ugh.** memory to
15900 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74   hold the result
15910 69 6e 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  ing string..**.*
15920 2a 20 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70  * In sqlite3_snp
15930 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20  rintf() routine 
15940 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 73  is similar to "s
15950 6e 70 72 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a  nprintf()" from.
15960 2a 2a 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  ** the standard 
15970 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  C library.  The 
15980 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 65  result is writte
15990 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75  n into the.** bu
159a0 66 66 65 72 20 73 75 70 70 6c 69 65 64 20 61 73  ffer supplied as
159b0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
159c0 6d 65 74 65 72 20 77 68 6f 73 65 20 73 69 7a 65  meter whose size
159d0 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20   is given by.** 
159e0 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  the first parame
159f0 74 65 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74  ter. Note that t
15a00 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a  he order of the.
15a10 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61 72  ** first two par
15a20 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 72  ameters is rever
15a30 73 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74  sed from snprint
15a40 66 28 29 2e 20 20 54 68 69 73 20 69 73 20 61 6e  f().  This is an
15a50 0a 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 61  .** historical a
15a60 63 63 69 64 65 6e 74 20 74 68 61 74 20 63 61 6e  ccident that can
15a70 6e 6f 74 20 62 65 20 66 69 78 65 64 20 77 69 74  not be fixed wit
15a80 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a  hout breaking.**
15a90 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
15aa0 74 69 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20  tibility.  Note 
15ab0 61 6c 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65  also that sqlite
15ac0 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20  3_snprintf().** 
15ad0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
15ae0 72 20 74 6f 20 69 74 73 20 62 75 66 66 65 72 20  r to its buffer 
15af0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e  instead of the n
15b00 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72  umber of.** char
15b10 61 63 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20  acters actually 
15b20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
15b30 20 62 75 66 66 65 72 2e 20 20 57 65 20 61 64 6d   buffer.  We adm
15b40 69 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e  it that.** the n
15b50 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
15b60 65 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c  ers written woul
15b70 64 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66  d be a more usef
15b80 75 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c  ul return.** val
15b90 75 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74  ue but we cannot
15ba0 20 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c   change the impl
15bb0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
15bc0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
15bd0 0a 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20  .** now without 
15be0 62 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69  breaking compati
15bf0 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73  bility..**.** As
15c00 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66   long as the buf
15c10 66 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 61  fer size is grea
15c20 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73  ter than zero, s
15c30 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
15c40 29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20  ).** guarantees 
15c50 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72 20  that the buffer 
15c60 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74  is always zero-t
15c70 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20  erminated.  The 
15c80 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74  first.** paramet
15c90 65 72 20 22 6e 22 20 69 73 20 74 68 65 20 74 6f  er "n" is the to
15ca0 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20  tal size of the 
15cb0 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e  buffer, includin
15cc0 67 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74  g space for.** t
15cd0 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  he zero terminat
15ce0 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67  or.  So the long
15cf0 65 73 74 20 73 74 72 69 6e 67 20 74 68 61 74 20  est string that 
15d00 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c  can be completel
15d10 79 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 6c  y.** written wil
15d20 6c 20 62 65 20 6e 2d 31 20 63 68 61 72 61 63 74  l be n-1 charact
15d30 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ers..**.** These
15d40 20 72 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d   routines all im
15d50 70 6c 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64  plement some add
15d60 69 74 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69  itional formatti
15d70 6e 67 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68  ng.** options th
15d80 61 74 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f  at are useful fo
15d90 72 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 53  r constructing S
15da0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  QL statements..*
15db0 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 75  * All of the usu
15dc0 61 6c 20 70 72 69 6e 74 66 28 29 20 66 6f 72 6d  al printf() form
15dd0 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61  atting options a
15de0 70 70 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 69  pply.  In additi
15df0 6f 6e 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20  on, there.** is 
15e00 61 72 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20  are "%q", "%Q", 
15e10 61 6e 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73  and "%z" options
15e20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f  ..**.** The %q o
15e30 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65  ption works like
15e40 20 25 73 20 69 6e 20 74 68 61 74 20 69 74 20 73   %s in that it s
15e50 75 62 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c  ubstitutes a nul
15e60 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20  l-terminated.** 
15e70 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20  string from the 
15e80 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20  argument list.  
15e90 42 75 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62  But %q also doub
15ea0 6c 65 73 20 65 76 65 72 79 20 27 5c 27 27 20 63  les every '\'' c
15eb0 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20  haracter..** %q 
15ec0 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20  is designed for 
15ed0 75 73 65 20 69 6e 73 69 64 65 20 61 20 73 74 72  use inside a str
15ee0 69 6e 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79  ing literal.  By
15ef0 20 64 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27   doubling each '
15f00 5c 27 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72  \''.** character
15f10 20 69 74 20 65 73 63 61 70 65 73 20 74 68 61 74   it escapes that
15f20 20 63 68 61 72 61 63 74 65 72 20 61 6e 64 20 61   character and a
15f30 6c 6c 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69  llows it to be i
15f40 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20  nserted into.** 
15f50 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  the string..**.*
15f60 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61  * For example, a
15f70 73 73 75 6d 65 20 74 68 65 20 73 74 72 69 6e 67  ssume the string
15f80 20 76 61 72 69 61 62 6c 65 20 7a 54 65 78 74 20   variable zText 
15f90 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61 73  contains text as
15fa0 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
15fb0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
15fc0 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 78  >.**  char *zTex
15fd0 74 20 3d 20 22 49 74 27 73 20 61 20 68 61 70 70  t = "It's a happ
15fe0 79 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72  y day!";.** </pr
15ff0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
16000 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73  **.** One can us
16010 65 20 74 68 69 73 20 74 65 78 74 20 69 6e 20 61  e this text in a
16020 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
16030 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
16040 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
16050 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53  re>.**  char *zS
16060 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  QL = sqlite3_mpr
16070 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54  intf("INSERT INT
16080 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 27  O table VALUES('
16090 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a  %q')", zText);.*
160a0 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  *  sqlite3_exec(
160b0 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20  db, zSQL, 0, 0, 
160c0 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  0);.**  sqlite3_
160d0 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c  free(zSQL);.** <
160e0 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74  /pre></blockquot
160f0 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65  e>.**.** Because
16100 20 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20 73   the %q format s
16110 74 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74  tring is used, t
16120 68 65 20 27 5c 27 27 20 63 68 61 72 61 63 74 65  he '\'' characte
16130 72 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73  r in zText.** is
16140 20 65 73 63 61 70 65 64 20 61 6e 64 20 74 68 65   escaped and the
16150 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20 69   SQL generated i
16160 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
16170 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
16180 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54  <pre>.**  INSERT
16190 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c   INTO table1 VAL
161a0 55 45 53 28 27 49 74 27 27 73 20 61 20 68 61 70  UES('It''s a hap
161b0 70 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70  py day!').** </p
161c0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
161d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63  .**.** This is c
161e0 6f 72 72 65 63 74 2e 20 20 48 61 64 20 77 65 20  orrect.  Had we 
161f0 75 73 65 64 20 25 73 20 69 6e 73 74 65 61 64 20  used %s instead 
16200 6f 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72  of %q, the gener
16210 61 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c  ated SQL.** woul
16220 64 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69  d have looked li
16230 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c  ke this:.**.** <
16240 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
16250 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .**  INSERT INTO
16260 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27   table1 VALUES('
16270 49 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79  It's a happy day
16280 21 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f  !');.** </pre></
16290 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a  blockquote>.**.*
162a0 2a 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65 78  * This second ex
162b0 61 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c 20  ample is an SQL 
162c0 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 41  syntax error.  A
162d0 73 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c 65  s a general rule
162e0 20 79 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61   you should.** a
162f0 6c 77 61 79 73 20 75 73 65 20 25 71 20 69 6e 73  lways use %q ins
16300 74 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e 20  tead of %s when 
16310 69 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20 69  inserting text i
16320 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74  nto a string lit
16330 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eral..**.** The 
16340 25 51 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20  %Q option works 
16350 6c 69 6b 65 20 25 71 20 65 78 63 65 70 74 20 69  like %q except i
16360 74 20 61 6c 73 6f 20 61 64 64 73 20 73 69 6e 67  t also adds sing
16370 6c 65 20 71 75 6f 74 65 73 20 61 72 6f 75 6e 64  le quotes around
16380 0a 2a 2a 20 74 68 65 20 6f 75 74 73 69 64 65 20  .** the outside 
16390 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 73 74 72  of the total str
163a0 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ing.  Additional
163b0 6c 79 2c 20 69 66 20 74 68 65 20 70 61 72 61 6d  ly, if the param
163c0 65 74 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61  eter in the.** a
163d0 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 20  rgument list is 
163e0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
163f0 25 51 20 73 75 62 73 74 69 74 75 74 65 73 20 74  %Q substitutes t
16400 68 65 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28  he text "NULL" (
16410 77 69 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c  without.** singl
16420 65 20 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61  e quotes) in pla
16430 63 65 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74  ce of the %Q opt
16440 69 6f 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78  ion.  So, for ex
16450 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64  ample, one could
16460 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f   say:.**.** <blo
16470 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
16480 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73    char *zSQL = s
16490 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
164a0 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c  INSERT INTO tabl
164b0 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a  e VALUES(%Q)", z
164c0 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74  Text);.**  sqlit
164d0 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c  e3_exec(db, zSQL
164e0 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20  , 0, 0, 0);.**  
164f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51  sqlite3_free(zSQ
16500 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  L);.** </pre></b
16510 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
16520 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20   The code above 
16530 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f  will render a co
16540 72 72 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d  rrect SQL statem
16550 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a  ent in the zSQL.
16560 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e  ** variable even
16570 20 69 66 20 74 68 65 20 7a 54 65 78 74 20 76 61   if the zText va
16580 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c  riable is a NULL
16590 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
165a0 54 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74  The "%z" formatt
165b0 69 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73  ing option works
165c0 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25   exactly like "%
165d0 73 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61  s" with the.** a
165e0 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74  ddition that aft
165f0 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61  er the string ha
16600 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20  s been read and 
16610 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74  copied into.** t
16620 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69  he result, [sqli
16630 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63  te3_free()] is c
16640 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70  alled on the inp
16650 75 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d  ut string. {END}
16660 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
16670 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d  nts:.** [H17403]
16680 20 5b 48 31 37 34 30 36 5d 20 5b 48 31 37 34 30   [H17406] [H1740
16690 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  7].*/.SQLITE_API
166a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d   char *sqlite3_m
166b0 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61  printf(const cha
166c0 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  r*,...);.SQLITE_
166d0 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
166e0 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74  3_vmprintf(const
166f0 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29   char*, va_list)
16700 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  ;.SQLITE_API cha
16710 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  r *sqlite3_snpri
16720 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f  ntf(int,char*,co
16730 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
16740 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
16750 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  : Memory Allocat
16760 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48  ion Subsystem {H
16770 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a  17300} <S20000>.
16780 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65  **.** The SQLite
16790 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 73   core  uses thes
167a0 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73  e three routines
167b0 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20   for all of its 
167c0 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20  own.** internal 
167d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
167e0 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20  n needs. "Core" 
167f0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  in the previous 
16800 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73  sentence.** does
16810 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65   not include ope
16820 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70  rating-system sp
16830 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65  ecific VFS imple
16840 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a  mentation.  The.
16850 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75  ** Windows VFS u
16860 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f  ses native mallo
16870 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66  c() and free() f
16880 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f  or some operatio
16890 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ns..**.** The sq
168a0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72  lite3_malloc() r
168b0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
168c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c   pointer to a bl
168d0 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79  ock.** of memory
168e0 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65   at least N byte
168f0 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65  s in length, whe
16900 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 61  re N is the para
16910 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c  meter..** If sql
16920 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73  ite3_malloc() is
16930 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69   unable to obtai
16940 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 65  n sufficient fre
16950 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20  e.** memory, it 
16960 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70  returns a NULL p
16970 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20  ointer.  If the 
16980 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a  parameter N to.*
16990 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  * sqlite3_malloc
169a0 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65  () is zero or ne
169b0 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69  gative then sqli
169c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74  te3_malloc() ret
169d0 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70  urns.** a NULL p
169e0 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61  ointer..**.** Ca
169f0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72  lling sqlite3_fr
16a00 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e  ee() with a poin
16a10 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72  ter previously r
16a20 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71  eturned.** by sq
16a30 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f  lite3_malloc() o
16a40 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  r sqlite3_reallo
16a50 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61  c() releases tha
16a60 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74  t memory so.** t
16a70 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20  hat it might be 
16a80 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c  reused.  The sql
16a90 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74  ite3_free() rout
16aa0 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f  ine is.** a no-o
16ab0 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77  p if is called w
16ac0 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ith a NULL point
16ad0 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e  er.  Passing a N
16ae0 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  ULL pointer.** t
16af0 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  o sqlite3_free()
16b00 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41   is harmless.  A
16b10 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64  fter being freed
16b20 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75  , memory.** shou
16b30 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65  ld neither be re
16b40 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20  ad nor written. 
16b50 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72   Even reading pr
16b60 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a  eviously freed.*
16b70 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72  * memory might r
16b80 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65  esult in a segme
16b90 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72  ntation fault or
16ba0 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72   other severe er
16bb0 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63  ror..** Memory c
16bc0 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67  orruption, a seg
16bd0 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c  mentation fault,
16be0 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65   or other severe
16bf0 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20   error.** might 
16c00 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65  result if sqlite
16c10 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c  3_free() is call
16c20 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55  ed with a non-NU
16c30 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a  LL pointer that.
16c40 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69  ** was not obtai
16c50 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
16c60 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c  _malloc() or sql
16c70 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a  ite3_realloc()..
16c80 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
16c90 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65  3_realloc() inte
16ca0 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74  rface attempts t
16cb0 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72  o resize a.** pr
16cc0 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ior memory alloc
16cd0 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c  ation to be at l
16ce0 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68  east N bytes, wh
16cf0 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20  ere N is the.** 
16d00 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
16d10 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c  .  The memory al
16d20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72  location to be r
16d30 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 69  esized is the fi
16d40 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  rst.** parameter
16d50 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74 20  .  If the first 
16d60 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
16d70 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a  ite3_realloc().*
16d80 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  * is a NULL poin
16d90 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 68  ter then its beh
16da0 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63  avior is identic
16db0 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a  al to calling.**
16dc0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
16dd0 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  N) where N is th
16de0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
16df0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  er to sqlite3_re
16e00 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74  alloc()..** If t
16e10 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
16e20 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
16e30 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f  ealloc() is zero
16e40 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20   or.** negative 
16e50 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f  then the behavio
16e60 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65  r is exactly the
16e70 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67   same as calling
16e80 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65  .** sqlite3_free
16e90 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74  (P) where P is t
16ea0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
16eb0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  er to sqlite3_re
16ec0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69  alloc()..** sqli
16ed0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65  te3_realloc() re
16ee0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
16ef0 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  to a memory allo
16f00 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20  cation.** of at 
16f10 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e  least N bytes in
16f20 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66   size or NULL if
16f30 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f   sufficient memo
16f40 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c  ry is unavailabl
16f50 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68  e..** If M is th
16f60 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72  e size of the pr
16f70 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  ior allocation, 
16f80 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79  then min(N,M) by
16f90 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72  tes.** of the pr
16fa0 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  ior allocation a
16fb0 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  re copied into t
16fc0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
16fd0 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a  buffer returned.
16fe0 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65  ** by sqlite3_re
16ff0 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20  alloc() and the 
17000 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
17010 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66   is freed..** If
17020 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
17030 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c  () returns NULL,
17040 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20   then the prior 
17050 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73  allocation.** is
17060 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a   not freed..**.*
17070 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74  * The memory ret
17080 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
17090 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71  _malloc() and sq
170a0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a  lite3_realloc().
170b0 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69  ** is always ali
170c0 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74  gned to at least
170d0 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64   an 8 byte bound
170e0 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  ary. {END}.**.**
170f0 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70   The default imp
17100 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
17110 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
17120 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75  tion subsystem u
17130 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f  ses.** the mallo
17140 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61  c(), realloc() a
17150 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 64  nd free() provid
17160 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61  ed by the standa
17170 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a  rd C library..**
17180 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65   {H17382} Howeve
17190 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20  r, if SQLite is 
171a0 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
171b0 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f  e.** SQLITE_MEMO
171c0 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f  RY_SIZE=<i>NNN</
171d0 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  i> C preprocesso
171e0 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c  r macro (where <
171f0 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20  i>NNN</i>.** is 
17200 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65  an integer), the
17210 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20  n SQLite create 
17220 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20 6f  a static array o
17230 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69  f at least.** <i
17240 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69  >NNN</i> bytes i
17250 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20  n size and uses 
17260 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 61  that array for a
17270 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69  ll of its dynami
17280 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  c.** memory allo
17290 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45  cation needs. {E
172a0 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20  ND}  Additional 
172b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
172c0 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20   options.** may 
172d0 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75  be added in futu
172e0 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a  re releases..**.
172f0 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72  ** In SQLite ver
17300 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33  sion 3.5.0 and 3
17310 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73  .5.1, it was pos
17320 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a  sible to define.
17330 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d  ** the SQLITE_OM
17340 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41  IT_MEMORY_ALLOCA
17350 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64  TION which would
17360 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 74   cause the built
17370 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  -in.** implement
17380 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72  ation of these r
17390 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d  outines to be om
173a0 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 70  itted.  That cap
173b0 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f  ability.** is no
173c0 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64   longer provided
173d0 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e  .  Only built-in
173e0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
173f0 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a  rs can be used..
17400 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77  **.** The Window
17410 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c  s OS interface l
17420 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68  ayer calls.** th
17430 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28  e system malloc(
17440 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72  ) and free() dir
17450 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65  ectly when conve
17460 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d  rting.** filenam
17470 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 55  es between the U
17480 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73  TF-8 encoding us
17490 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20  ed by SQLite.** 
174a0 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 6c  and whatever fil
174b0 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69  ename encoding i
174c0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61  s used by the pa
174d0 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73  rticular Windows
174e0 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e  .** installation
174f0 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  .  Memory alloca
17500 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20  tion errors are 
17510 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a  detected, but.**
17520 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 74   they are report
17530 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49  ed back as [SQLI
17540 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a  TE_CANTOPEN] or.
17550 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  ** [SQLITE_IOERR
17560 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53  ] rather than [S
17570 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a  QLITE_NOMEM]..**
17580 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
17590 3a 0a 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48  :.** [H17303] [H
175a0 31 37 33 30 34 5d 20 5b 48 31 37 33 30 35 5d 20  17304] [H17305] 
175b0 5b 48 31 37 33 30 36 5d 20 5b 48 31 37 33 31 30  [H17306] [H17310
175c0 5d 20 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 33  ] [H17312] [H173
175d0 31 35 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20  15] [H17318].** 
175e0 5b 48 31 37 33 32 31 5d 20 5b 48 31 37 33 32 32  [H17321] [H17322
175f0 5d 20 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a  ] [H17323].**.**
17600 20 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67   The pointer arg
17610 75 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74  uments to [sqlit
17620 65 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b  e3_free()] and [
17630 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
17640 29 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69  )].** must be ei
17650 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73  ther NULL or els
17660 65 20 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69  e pointers obtai
17670 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72  ned from a prior
17680 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  .** invocation o
17690 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  f [sqlite3_mallo
176a0 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  c()] or [sqlite3
176b0 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74  _realloc()] that
176c0 20 68 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74   have.** not yet
176d0 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a   been released..
176e0 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63  **.** The applic
176f0 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72  ation must not r
17700 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79  ead or write any
17710 20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c   part of.** a bl
17720 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66  ock of memory af
17730 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20  ter it has been 
17740 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a 2a  released using.*
17750 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  * [sqlite3_free(
17760 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  )] or [sqlite3_r
17770 65 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51  ealloc()]..*/.SQ
17780 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
17790 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e  qlite3_malloc(in
177a0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  t);.SQLITE_API v
177b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61  oid *sqlite3_rea
177c0 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29  lloc(void*, int)
177d0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
177e0 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76  d sqlite3_free(v
177f0 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid*);../*.** CA
17800 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41  PI3REF: Memory A
17810 6c 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74  llocator Statist
17820 69 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33  ics {H17370} <S3
17830 30 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  0210>.**.** SQLi
17840 74 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73  te provides thes
17850 65 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73  e two interfaces
17860 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f   for reporting o
17870 6e 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20  n the status.** 
17880 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
17890 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69  malloc()], [sqli
178a0 74 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64  te3_free()], and
178b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
178c0 63 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  c()].** routines
178d0 2c 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65  , which form the
178e0 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79   built-in memory
178f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
17900 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ystem..**.** Req
17910 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
17920 31 37 33 37 31 5d 20 5b 48 31 37 33 37 33 5d 20  17371] [H17373] 
17930 5b 48 31 37 33 37 34 5d 20 5b 48 31 37 33 37 35  [H17374] [H17375
17940 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
17950 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
17960 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65  lite3_memory_use
17970 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  d(void);.SQLITE_
17980 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
17990 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  4 sqlite3_memory
179a0 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72  _highwater(int r
179b0 65 73 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a  esetFlag);../*.*
179c0 2a 20 43 41 50 49 33 52 45 46 3a 20 50 73 65 75  * CAPI3REF: Pseu
179d0 64 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72  do-Random Number
179e0 20 47 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 33   Generator {H173
179f0 39 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a  90} <S20000>.**.
17a00 2a 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69  ** SQLite contai
17a10 6e 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74  ns a high-qualit
17a20 79 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  y pseudo-random 
17a30 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
17a40 20 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0a   (PRNG) used to.
17a50 2a 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d  ** select random
17a60 20 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 73   [ROWID | ROWIDs
17a70 5d 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67  ] when inserting
17a80 20 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74   new records int
17a90 6f 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a  o a table that.*
17aa0 2a 20 61 6c 72 65 61 64 79 20 75 73 65 73 20 74  * already uses t
17ab0 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69  he largest possi
17ac0 62 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68  ble [ROWID].  Th
17ad0 65 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75  e PRNG is also u
17ae0 73 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62  sed for.** the b
17af0 75 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29  uild-in random()
17b00 20 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28   and randomblob(
17b10 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e  ) SQL functions.
17b20 20 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65    This interface
17b30 20 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69   allows.** appli
17b40 63 61 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73  cations to acces
17b50 73 20 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20  s the same PRNG 
17b60 66 6f 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73  for other purpos
17b70 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c  es..**.** A call
17b80 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
17b90 20 73 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20   stores N bytes 
17ba0 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e  of randomness in
17bb0 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a  to buffer P..**.
17bc0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  ** The first tim
17bd0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
17be0 73 20 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65  s invoked (eithe
17bf0 72 20 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20  r internally or 
17c00 62 79 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63  by.** the applic
17c10 61 74 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20  ation) the PRNG 
17c20 69 73 20 73 65 65 64 65 64 20 75 73 69 6e 67 20  is seeded using 
17c30 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69  randomness obtai
17c40 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ned.** from the 
17c50 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68  xRandomness meth
17c60 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c  od of the defaul
17c70 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  t [sqlite3_vfs] 
17c80 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c  object..** On al
17c90 6c 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76  l subsequent inv
17ca0 6f 63 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73  ocations, the ps
17cb0 65 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20  eudo-randomness 
17cc0 69 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20  is generated.** 
17cd0 69 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77  internally and w
17ce0 69 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20  ithout recourse 
17cf0 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  to the [sqlite3_
17d00 76 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73  vfs] xRandomness
17d10 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a  .** method..**.*
17d20 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
17d30 2a 2a 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53  ** [H17392].*/.S
17d40 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
17d50 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
17d60 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50  s(int N, void *P
17d70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
17d80 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65  EF: Compile-Time
17d90 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43   Authorization C
17da0 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30  allbacks {H12500
17db0 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S70100>.**.**
17dc0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
17dd0 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f 72  gisters a author
17de0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69  izer callback wi
17df0 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a  th a particular.
17e00 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
17e10 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69  nection], suppli
17e20 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  ed in the first 
17e30 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65  argument..** The
17e40 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
17e50 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
17e60 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  as SQL statement
17e70 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70  s are being comp
17e80 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69  iled.** by [sqli
17e90 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f  te3_prepare()] o
17ea0 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b  r its variants [
17eb0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
17ec0 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  v2()],.** [sqlit
17ed0 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20  e3_prepare16()] 
17ee0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  and [sqlite3_pre
17ef0 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41  pare16_v2()].  A
17f00 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69  t various.** poi
17f10 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 63  nts during the c
17f20 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65  ompilation proce
17f30 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20  ss, as logic is 
17f40 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a  being created.**
17f50 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69   to perform vari
17f60 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65  ous actions, the
17f70 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
17f80 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
17f90 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f  to.** see if tho
17fa0 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61  se actions are a
17fb0 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74  llowed.  The aut
17fc0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
17fd0 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72   should.** retur
17fe0 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f  n [SQLITE_OK] to
17ff0 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f   allow the actio
18000 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52  n, [SQLITE_IGNOR
18010 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74  E] to disallow t
18020 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61  he.** specific a
18030 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20  ction but allow 
18040 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
18050 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f  t to continue to
18060 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c   be.** compiled,
18070 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59   or [SQLITE_DENY
18080 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65  ] to cause the e
18090 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d  ntire SQL statem
180a0 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a  ent to be.** rej
180b0 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 72  ected with an er
180c0 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 74  ror.  If the aut
180d0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
180e0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20   returns.** any 
180f0 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e  value other than
18100 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
18110 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f  , [SQLITE_OK], o
18120 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a  r [SQLITE_DENY].
18130 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c  ** then the [sql
18140 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
18150 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74  )] or equivalent
18160 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67   call that trigg
18170 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68  ered.** the auth
18180 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c  orizer will fail
18190 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d   with an error m
181a0 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  essage..**.** Wh
181b0 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  en the callback 
181c0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
181d0 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  OK], that means 
181e0 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a  the operation.**
181f0 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b   requested is ok
18200 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c  .  When the call
18210 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51  back returns [SQ
18220 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a  LITE_DENY], the.
18230 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
18240 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75  are_v2()] or equ
18250 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61  ivalent call tha
18260 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 0a  t triggered the.
18270 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69  ** authorizer wi
18280 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20  ll fail with an 
18290 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78  error message ex
182a0 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a  plaining that.**
182b0 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65   access is denie
182c0 64 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 6f  d.  If the autho
182d0 72 69 7a 65 72 20 63 6f 64 65 20 69 73 20 5b 53  rizer code is [S
182e0 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 61  QLITE_READ].** a
182f0 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  nd the callback 
18300 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
18310 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65  IGNORE] then the
18320 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
18330 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d 65  atement] stateme
18340 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  nt is constructe
18350 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 0a  d to substitute.
18360 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20  ** a NULL value 
18370 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20  in place of the 
18380 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
18390 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20  t would have.** 
183a0 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51  been read if [SQ
183b0 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65  LITE_OK] had bee
183c0 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  n returned.  The
183d0 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
183e0 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 62  .** return can b
183f0 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 61  e used to deny a
18400 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 72  n untrusted user
18410 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 76   access to indiv
18420 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e 73  idual.** columns
18430 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 0a   of a table..**.
18440 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72  ** The first par
18450 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75  ameter to the au
18460 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
18470 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  k is a copy of t
18480 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61  he third.** para
18490 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  meter to the sql
184a0 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
184b0 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e  zer() interface.
184c0 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   The second para
184d0 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  meter.** to the 
184e0 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69  callback is an i
184f0 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f 43  nteger [SQLITE_C
18500 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64  OPY | action cod
18510 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 65  e] that specifie
18520 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 75  s.** the particu
18530 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 65  lar action to be
18540 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 65   authorized. The
18550 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 73   third through s
18560 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 0a  ixth parameters.
18570 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ** to the callba
18580 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d  ck are zero-term
18590 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74  inated strings t
185a0 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69  hat contain addi
185b0 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c  tional.** detail
185c0 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 69  s about the acti
185d0 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69  on to be authori
185e0 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 75  zed..**.** An au
185f0 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65 64  thorizer is used
18600 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70   when [sqlite3_p
18610 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 69  repare | prepari
18620 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ng].** SQL state
18630 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75 6e  ments from an un
18640 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c 20  trusted source, 
18650 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  to ensure that t
18660 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
18670 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79 20  s.** do not try 
18680 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20 74  to access data t
18690 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  hey are not allo
186a0 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20 74  wed to see, or t
186b0 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74 0a  hat they do not.
186c0 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75 74  ** try to execut
186d0 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61 74  e malicious stat
186e0 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d 61  ements that dama
186f0 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ge the database.
18700 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65    For.** example
18710 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  , an application
18720 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73 65   may allow a use
18730 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69 74  r to enter arbit
18740 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65 72  rary.** SQL quer
18750 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74 69  ies for evaluati
18760 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73 65  on by a database
18770 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c 69  .  But the appli
18780 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20 6e  cation does.** n
18790 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65 72  ot want the user
187a0 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 6d   to be able to m
187b0 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63 68  ake arbitrary ch
187c0 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20  anges to the.** 
187d0 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61 75  database.  An au
187e0 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20 74  thorizer could t
187f0 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70 6c  hen be put in pl
18800 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a 2a  ace while the.**
18810 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51   user-entered SQ
18820 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c 69  L is being [sqli
18830 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72  te3_prepare | pr
18840 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a 20  epared] that.** 
18850 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79 74  disallows everyt
18860 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45 4c  hing except [SEL
18870 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73 2e  ECT] statements.
18880 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69  .**.** Applicati
18890 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ons that need to
188a0 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72 6f   process SQL fro
188b0 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72  m untrusted sour
188c0 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c 73  ces.** might als
188d0 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65 72  o consider lower
188e0 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69 6d  ing resource lim
188f0 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  its using [sqlit
18900 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 61  e3_limit()].** a
18910 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74 61  nd limiting data
18920 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67 20  base size using 
18930 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f  the [max_page_co
18940 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a 2a  unt] [PRAGMA].**
18950 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20   in addition to 
18960 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72 69  using an authori
18970 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  zer..**.** Only 
18980 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72 69  a single authori
18990 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70 6c  zer can be in pl
189a0 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61 73  ace on a databas
189b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
189c0 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63 68  at a time.  Each
189d0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
189e0 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20  _set_authorizer 
189f0 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a 2a  overrides the.**
18a00 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e 20   previous call. 
18a10 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75 74   Disable the aut
18a20 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74 61  horizer by insta
18a30 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61 6c  lling a NULL cal
18a40 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61 75  lback..** The au
18a50 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73 61  thorizer is disa
18a60 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e  bled by default.
18a70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
18a80 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d  rizer callback m
18a90 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68  ust not do anyth
18aa0 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f  ing that will mo
18ab0 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61  dify.** the data
18ac0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
18ad0 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65  that invoked the
18ae0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
18af0 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68  back..** Note th
18b00 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  at [sqlite3_prep
18b10 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73  are_v2()] and [s
18b20 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62  qlite3_step()] b
18b30 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72  oth modify their
18b40 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
18b50 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65  nections for the
18b60 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64   meaning of "mod
18b70 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72  ify" in this par
18b80 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57 68  agraph..**.** Wh
18b90 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  en [sqlite3_prep
18ba0 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73 65  are_v2()] is use
18bb0 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20 73  d to prepare a s
18bc0 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a 2a  tatement, the.**
18bd0 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68 74   statement might
18be0 20 62 65 20 72 65 70 72 65 70 61 72 65 64 20 64   be reprepared d
18bf0 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73  uring [sqlite3_s
18c00 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61 20  tep()] due to a 
18c10 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67  .** schema chang
18c20 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 61  e.  Hence, the a
18c30 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c  pplication shoul
18c40 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  d ensure that th
18c50 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75 74  e.** correct aut
18c60 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
18c70 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61 63   remains in plac
18c80 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73 71  e during the [sq
18c90 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a  lite3_step()]..*
18ca0 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  *.** Note that t
18cb0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
18cc0 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65  llback is invoke
18cd0 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a 2a  d only during.**
18ce0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
18cf0 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69  e()] or its vari
18d00 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a 61  ants.  Authoriza
18d10 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 70  tion is not.** p
18d20 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67 20  erformed during 
18d30 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75 61  statement evalua
18d40 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65 33  tion in [sqlite3
18d50 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _step()]..**.** 
18d60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
18d70 20 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 35 30   [H12501] [H1250
18d80 32 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 32  2] [H12503] [H12
18d90 35 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 48  504] [H12505] [H
18da0 31 32 35 30 36 5d 20 5b 48 31 32 35 30 37 5d 20  12506] [H12507] 
18db0 5b 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 32  [H12510].** [H12
18dc0 35 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 48  511] [H12512] [H
18dd0 31 32 35 32 30 5d 20 5b 48 31 32 35 32 31 5d 20  12520] [H12521] 
18de0 5b 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c 49  [H12522].*/.SQLI
18df0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
18e00 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
18e10 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20  r(.  sqlite3*,. 
18e20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
18e30 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
18e40 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
18e50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
18e60 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64  t char*),.  void
18e70 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a   *pUserData.);..
18e80 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
18e90 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72  Authorizer Retur
18ea0 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 39 30 7d  n Codes {H12590}
18eb0 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20   <H12500>.**.** 
18ec0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74  The [sqlite3_set
18ed0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75  _authorizer | au
18ee0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
18ef0 6b 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74  k function] must
18f00 0a 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65  .** return eithe
18f10 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72  r [SQLITE_OK] or
18f20 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77   one of these tw
18f30 6f 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f  o constants in o
18f40 72 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61  rder.** to signa
18f50 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 72  l SQLite whether
18f60 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69   or not the acti
18f70 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e  on is permitted.
18f80 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71    See the.** [sq
18f90 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
18fa0 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65  izer | authorize
18fb0 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d  r documentation]
18fc0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a   for additional.
18fd0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  ** information..
18fe0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
18ff0 45 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20  E_DENY   1   /* 
19000 41 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74  Abort the SQL st
19010 61 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20  atement with an 
19020 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65  error */.#define
19030 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32   SQLITE_IGNORE 2
19040 20 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f     /* Don't allo
19050 77 20 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f  w access, but do
19060 6e 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20  n't generate an 
19070 65 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  error */../*.** 
19080 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72  CAPI3REF: Author
19090 69 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65  izer Action Code
190a0 73 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 35  s {H12550} <H125
190b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73  00>.**.** The [s
190c0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
190d0 72 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61  rizer()] interfa
190e0 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63  ce registers a c
190f0 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
19100 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f  .** that is invo
19110 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65  ked to authorize
19120 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61   certain SQL sta
19130 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20  tement actions. 
19140 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70   The.** second p
19150 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
19160 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69  callback is an i
19170 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74  nteger code that
19180 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68   specifies.** wh
19190 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69  at action is bei
191a0 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20  ng authorized.  
191b0 54 68 65 73 65 20 61 72 65 20 74 68 65 20 69 6e  These are the in
191c0 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64  teger action cod
191d0 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61  es that.** the a
191e0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
191f0 63 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65 64  ck may be passed
19200 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63  ..**.** These ac
19210 74 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73  tion code values
19220 20 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69   signify what ki
19230 6e 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20  nd of operation 
19240 69 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 68  is to be.** auth
19250 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64  orized.  The 3rd
19260 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74   and 4th paramet
19270 65 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f  ers to the autho
19280 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c  rization.** call
19290 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69  back function wi
192a0 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 73  ll be parameters
192b0 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69   or NULL dependi
192c0 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74  ng on which of t
192d0 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73  hese.** codes is
192e0 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63   used as the sec
192f0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20  ond parameter.  
19300 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65  The 5th paramete
19310 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68  r to the.** auth
19320 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
19330 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
19340 68 65 20 64 61 74 61 62 61 73 65 20 28 22 6d 61  he database ("ma
19350 69 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20  in", "temp",.** 
19360 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61  etc.) if applica
19370 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70 61  ble.  The 6th pa
19380 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61  rameter to the a
19390 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
193a0 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d  ck.** is the nam
193b0 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d  e of the inner-m
193c0 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76  ost trigger or v
193d0 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70  iew that is resp
193e0 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74  onsible for.** t
193f0 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70  he access attemp
19400 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69  t or NULL if thi
19410 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74  s access attempt
19420 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f   is directly fro
19430 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53  m.** top-level S
19440 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 52  QL code..**.** R
19450 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
19460 5b 48 31 32 35 35 31 5d 20 5b 48 31 32 35 35 32  [H12551] [H12552
19470 5d 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 35  ] [H12553] [H125
19480 35 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  54].*/./********
19490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
194b0 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a  *** 3rd ********
194c0 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a  **** 4th *******
194d0 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ****/.#define SQ
194e0 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45  LITE_CREATE_INDE
194f0 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 2f  X          1   /
19500 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20  * Index Name    
19510 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
19520 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
19530 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45  ITE_CREATE_TABLE
19540 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a            2   /*
19550 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
19560 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
19570 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
19580 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49  TE_CREATE_TEMP_I
19590 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20  NDEX     3   /* 
195a0 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20  Index Name      
195b0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
195c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
195d0 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41  E_CREATE_TEMP_TA
195e0 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54  BLE     4   /* T
195f0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
19600 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
19610 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
19620 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49  _CREATE_TEMP_TRI
19630 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72  GGER   5   /* Tr
19640 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61  igger Name    Ta
19650 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
19660 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
19670 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57  CREATE_TEMP_VIEW
19680 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65        6   /* Vie
19690 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c  w Name       NUL
196a0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
196b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
196c0 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20  REATE_TRIGGER   
196d0 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67       7   /* Trig
196e0 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
196f0 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
19700 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
19710 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 20  EATE_VIEW       
19720 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20      8   /* View 
19730 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20  Name       NULL 
19740 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
19750 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c  efine SQLITE_DEL
19760 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ETE             
19770 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20     9   /* Table 
19780 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
19790 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
197a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
197b0 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20  _INDEX          
197c0 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e   10   /* Index N
197d0 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e  ame      Table N
197e0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
197f0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
19800 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20  TABLE           
19810 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  11   /* Table Na
19820 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
19830 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
19840 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54  ne SQLITE_DROP_T
19850 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31  EMP_INDEX      1
19860 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d  2   /* Index Nam
19870 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  e      Table Nam
19880 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
19890 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  e SQLITE_DROP_TE
198a0 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33  MP_TABLE      13
198b0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
198c0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
198d0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
198e0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
198f0 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 20  P_TRIGGER    14 
19900 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d    /* Trigger Nam
19910 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20  e    Table Name 
19920 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
19930 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
19940 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 20  _VIEW       15  
19950 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20   /* View Name   
19960 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
19970 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
19980 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47  QLITE_DROP_TRIGG
19990 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 20  ER         16   
199a0 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20  /* Trigger Name 
199b0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
199c0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
199d0 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20  LITE_DROP_VIEW  
199e0 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 2f            17   /
199f0 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20  * View Name     
19a00 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
19a10 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
19a20 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20  ITE_INSERT      
19a30 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a           18   /*
19a40 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
19a50 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
19a60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
19a70 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20  TE_PRAGMA       
19a80 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20          19   /* 
19a90 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20  Pragma Name     
19aa0 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20  1st arg or NULL 
19ab0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
19ac0 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20  E_READ          
19ad0 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54         20   /* T
19ae0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43  able Name      C
19af0 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a  olumn Name     *
19b00 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
19b10 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20  _SELECT         
19b20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55        21   /* NU
19b30 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55  LL            NU
19b40 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
19b50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
19b60 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20  TRANSACTION     
19b70 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65       22   /* Ope
19b80 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c  ration       NUL
19b90 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
19ba0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
19bb0 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20  PDATE           
19bc0 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c      23   /* Tabl
19bd0 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75  e Name      Colu
19be0 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23  mn Name     */.#
19bf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54  define SQLITE_AT
19c00 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  TACH            
19c10 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e     24   /* Filen
19c20 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20  ame        NULL 
19c30 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
19c40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54  efine SQLITE_DET
19c50 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20  ACH             
19c60 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61    25   /* Databa
19c70 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20  se Name   NULL  
19c80 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
19c90 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45  fine SQLITE_ALTE
19ca0 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20  R_TABLE         
19cb0 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73   26   /* Databas
19cc0 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e  e Name   Table N
19cd0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
19ce0 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44  ine SQLITE_REIND
19cf0 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EX              
19d00 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61  27   /* Index Na
19d10 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
19d20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
19d30 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a  ne SQLITE_ANALYZ
19d40 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  E              2
19d50 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  8   /* Table Nam
19d60 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
19d70 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
19d80 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
19d90 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 39  VTABLE        29
19da0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
19db0 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d        Module Nam
19dc0 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  e     */.#define
19dd0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41   SQLITE_DROP_VTA
19de0 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 20  BLE          30 
19df0 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
19e00 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65       Module Name
19e10 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
19e20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20  SQLITE_FUNCTION 
19e30 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 20              31  
19e40 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20   /* NULL        
19e50 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d      Function Nam
19e60 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  e   */.#define S
19e70 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 20  QLITE_SAVEPOINT 
19e80 20 20 20 20 20 20 20 20 20 20 20 33 32 20 20 20             32   
19e90 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20  /* Operation    
19ea0 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 6d     Savepoint Nam
19eb0 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e  */.#define SQ
19ec0 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20  LITE_COPY       
19ed0 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f             0   /
19ee0 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64  * No longer used
19ef0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33   */../*.** CAPI3
19f00 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64  REF: Tracing And
19f10 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74   Profiling Funct
19f20 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c 53  ions {H12280} <S
19f30 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49  60400>.** EXPERI
19f40 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65  MENTAL.**.** The
19f50 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 69  se routines regi
19f60 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75  ster callback fu
19f70 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e  nctions that can
19f80 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20   be used for.** 
19f90 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66  tracing and prof
19fa0 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 74  iling the execut
19fb0 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65  ion of SQL state
19fc0 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ments..**.** The
19fd0 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
19fe0 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79  on registered by
19ff0 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29   sqlite3_trace()
1a000 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a 2a   is invoked at.*
1a010 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 20  * various times 
1a020 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 74  when an SQL stat
1a030 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 72  ement is being r
1a040 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73  un by [sqlite3_s
1a050 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 63  tep()]..** The c
1a060 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
1a070 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 6e  a UTF-8 renderin
1a080 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  g of the SQL sta
1a090 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 61  tement text.** a
1a0a0 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  s the statement 
1a0b0 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 65  first begins exe
1a0c0 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 6f  cuting.  Additio
1a0d0 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f 63  nal callbacks oc
1a0e0 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 74  cur.** as each t
1a0f0 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f 67  riggered subprog
1a100 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20  ram is entered. 
1a110 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66   The callbacks f
1a120 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63  or triggers.** c
1a130 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53  ontain a UTF-8 S
1a140 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20  QL comment that 
1a150 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 74  identifies the t
1a160 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  rigger..**.** Th
1a170 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
1a180 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62  ion registered b
1a190 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c  y sqlite3_profil
1a1a0 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a  e() is invoked.*
1a1b0 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 74  * as each SQL st
1a1c0 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73  atement finishes
1a1d0 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 63  .  The profile c
1a1e0 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73  allback contains
1a1f0 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  .** the original
1a200 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20   statement text 
1a210 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 20  and an estimate 
1a220 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69  of wall-clock ti
1a230 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f 6e  me.** of how lon
1a240 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  g that statement
1a250 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a 2a   took to run..**
1a260 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1a270 3a 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b 48  :.** [H12281] [H
1a280 31 32 32 38 32 5d 20 5b 48 31 32 32 38 33 5d 20  12282] [H12283] 
1a290 5b 48 31 32 32 38 34 5d 20 5b 48 31 32 32 38 35  [H12284] [H12285
1a2a0 5d 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 32  ] [H12287] [H122
1a2b0 38 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 20  88] [H12289].** 
1a2c0 5b 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c 49  [H12290].*/.SQLI
1a2d0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
1a2e0 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20  PERIMENTAL void 
1a2f0 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73  *sqlite3_trace(s
1a300 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78  qlite3*, void(*x
1a310 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
1a320 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a  st char*), void*
1a330 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  );.SQLITE_API SQ
1a340 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
1a350 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  L void *sqlite3_
1a360 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a  profile(sqlite3*
1a370 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66  ,.   void(*xProf
1a380 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ile)(void*,const
1a390 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75   char*,sqlite3_u
1a3a0 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a  int64), void*);.
1a3b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1a3c0 20 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 20   Query Progress 
1a3d0 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31  Callbacks {H1291
1a3e0 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a  0} <S60400>.**.*
1a3f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
1a400 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c  onfigures a call
1a410 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20  back function - 
1a420 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20  the.** progress 
1a430 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20  callback - that 
1a440 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f  is invoked perio
1a450 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c  dically during l
1a460 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63  ong.** running c
1a470 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
1a480 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74  _exec()], [sqlit
1a490 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a  e3_step()] and.*
1a4a0 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  * [sqlite3_get_t
1a4b0 61 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61  able()].  An exa
1a4c0 6d 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69  mple use for thi
1a4d0 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69  s.** interface i
1a4e0 73 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20  s to keep a GUI 
1a4f0 75 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 61  updated during a
1a500 20 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a   large query..**
1a510 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72  .** If the progr
1a520 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  ess callback ret
1a530 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
1a540 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a  he operation is.
1a550 2a 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20  ** interrupted. 
1a560 20 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61   This feature ca
1a570 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70  n be used to imp
1a580 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e  lement a.** "Can
1a590 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61  cel" button on a
1a5a0 20 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69   GUI progress di
1a5b0 61 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20  alog box..**.** 
1a5c0 54 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e  The progress han
1a5d0 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f  dler must not do
1a5e0 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77   anything that w
1a5f0 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68  ill modify.** th
1a600 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1a610 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b  ction that invok
1a620 65 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  ed the progress 
1a630 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65  handler..** Note
1a640 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70   that [sqlite3_p
1a650 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64  repare_v2()] and
1a660 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1a670 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68  ] both modify th
1a680 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  eir.** database 
1a690 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20  connections for 
1a6a0 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22  the meaning of "
1a6b0 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20  modify" in this 
1a6c0 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a  paragraph..**.**
1a6d0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
1a6e0 2a 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 39  * [H12911] [H129
1a6f0 31 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 31  12] [H12913] [H1
1a700 32 39 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 5b  2914] [H12915] [
1a710 48 31 32 39 31 36 5d 20 5b 48 31 32 39 31 37 5d  H12916] [H12917]
1a720 20 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f 0a   [H12918].**.*/.
1a730 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1a740 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
1a750 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33  _handler(sqlite3
1a760 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76  *, int, int(*)(v
1a770 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  oid*), void*);..
1a780 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1a790 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61  Opening A New Da
1a7a0 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
1a7b0 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32  n {H12700} <S402
1a7c0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
1a7d0 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e  routines open an
1a7e0 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
1a7f0 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65   file whose name
1a800 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
1a810 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  .** filename arg
1a820 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e  ument. The filen
1a830 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ame argument is 
1a840 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55  interpreted as U
1a850 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69  TF-8 for.** sqli
1a860 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73  te3_open() and s
1a870 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
1a880 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69   and as UTF-16 i
1a890 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74  n the native byt
1a8a0 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73  e.** order for s
1a8b0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e  qlite3_open16().
1a8c0 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   A [database con
1a8d0 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20  nection] handle 
1a8e0 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65  is usually.** re
1a8f0 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c  turned in *ppDb,
1a900 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f   even if an erro
1a910 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f  r occurs.  The o
1a920 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73  nly exception is
1a930 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69   that.** if SQLi
1a940 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  te is unable to 
1a950 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  allocate memory 
1a960 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c  to hold the [sql
1a970 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a  ite3] object,.**
1a980 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20   a NULL will be 
1a990 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70  written into *pp
1a9a0 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  Db instead of a 
1a9b0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b  pointer to the [
1a9c0 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65  sqlite3].** obje
1a9d0 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  ct. If the datab
1a9e0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61  ase is opened (a
1a9f0 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73  nd/or created) s
1aa00 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65  uccessfully, the
1aa10 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  n.** [SQLITE_OK]
1aa20 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f   is returned.  O
1aa30 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72  therwise an [err
1aa40 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
1aa50 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73  rned.  The.** [s
1aa60 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d  qlite3_errmsg()]
1aa70 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   or [sqlite3_err
1aa80 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65  msg16()] routine
1aa90 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
1aaa0 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e   obtain.** an En
1aab0 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64  glish language d
1aac0 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
1aad0 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  e error..**.** T
1aae0 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64  he default encod
1aaf0 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61  ing for the data
1ab00 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46  base will be UTF
1ab10 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33  -8 if.** sqlite3
1ab20 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74  _open() or sqlit
1ab30 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
1ab40 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54  called and.** UT
1ab50 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69  F-16 in the nati
1ab60 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66  ve byte order if
1ab70 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
1ab80 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  ) is used..**.**
1ab90 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
1aba0 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
1abb0 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65  when it is opene
1abc0 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20  d, resources.** 
1abd0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1abe0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
1abf0 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65  nnection] handle
1ac00 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61   should be relea
1ac10 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e  sed by.** passin
1ac20 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  g it to [sqlite3
1ac30 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69  _close()] when i
1ac40 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72  t is no longer r
1ac50 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  equired..**.** T
1ac60 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  he sqlite3_open_
1ac70 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77  v2() interface w
1ac80 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65  orks like sqlite
1ac90 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65  3_open().** exce
1aca0 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70  pt that it accep
1acb0 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61  ts two additiona
1acc0 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72  l parameters for
1acd0 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74   additional cont
1ace0 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20  rol.** over the 
1acf0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e  new database con
1ad00 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c  nection.  The fl
1ad10 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61  ags parameter ca
1ad20 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a  n take one of.**
1ad30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
1ad40 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74  hree values, opt
1ad50 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64  ionally combined
1ad60 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53   with the .** [S
1ad70 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
1ad80 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4f  EX] or [SQLITE_O
1ad90 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66  PEN_FULLMUTEX] f
1ada0 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  lags:.**.** <dl>
1adb0 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f  .** <dt>[SQLITE_
1adc0 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f  OPEN_READONLY]</
1add0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64  dt>.** <dd>The d
1ade0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
1adf0 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d  d in read-only m
1ae00 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74  ode.  If the dat
1ae10 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a  abase does not.*
1ae20 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c  * already exist,
1ae30 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
1ae40 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  urned.</dd>.**.*
1ae50 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50  * <dt>[SQLITE_OP
1ae60 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64  EN_READWRITE]</d
1ae70 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61  t>.** <dd>The da
1ae80 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
1ae90 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64   for reading and
1aea0 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73   writing if poss
1aeb0 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67  ible, or reading
1aec0 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20  .** only if the 
1aed0 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72  file is write pr
1aee0 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f  otected by the o
1aef0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e  perating system.
1af00 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63    In either.** c
1af10 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ase the database
1af20 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78   must already ex
1af30 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ist, otherwise a
1af40 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
1af50 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
1af60 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  <dt>[SQLITE_OPEN
1af70 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53  _READWRITE] | [S
1af80 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
1af90 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  E]</dt>.** <dd>T
1afa0 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  he database is o
1afb0 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e  pened for readin
1afc0 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61  g and writing, a
1afd0 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74  nd is creates it
1afe0 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e   if.** it does n
1aff0 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
1b000 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65  . This is the be
1b010 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61  havior that is a
1b020 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a  lways used for.*
1b030 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
1b040 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65   and sqlite3_ope
1b050 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c  n16().</dd>.** <
1b060 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  /dl>.**.** If th
1b070 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
1b080 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  to sqlite3_open_
1b090 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20  v2() is not one 
1b0a0 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e  of the.** combin
1b0b0 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f  ations shown abo
1b0c0 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  ve or one of the
1b0d0 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68   combinations sh
1b0e0 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e  own above combin
1b0f0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b  ed.** with the [
1b100 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
1b110 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  TEX] or [SQLITE_
1b120 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20  OPEN_FULLMUTEX] 
1b130 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74  flags,.** then t
1b140 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
1b150 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
1b160 49 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f  If the [SQLITE_O
1b170 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61  PEN_NOMUTEX] fla
1b180 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74  g is set, then t
1b190 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1b1a0 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20  ection.** opens 
1b1b0 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72  in the multi-thr
1b1c0 65 61 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  ead [threading m
1b1d0 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20  ode] as long as 
1b1e0 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61  the single-threa
1b1f0 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f  d.** mode has no
1b200 74 20 62 65 65 6e 20 73 65 74 20 61 74 20 63 6f  t been set at co
1b210 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74  mpile-time or st
1b220 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 68  art-time.  If th
1b230 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45  e.** [SQLITE_OPE
1b240 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61  N_FULLMUTEX] fla
1b250 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 74 68  g is set then th
1b260 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1b270 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69  ction opens.** i
1b280 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64  n the serialized
1b290 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
1b2a0 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d  ] unless single-
1b2b0 74 68 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 72  thread was.** pr
1b2c0 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 65  eviously selecte
1b2d0 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  d at compile-tim
1b2e0 65 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e  e or start-time.
1b2f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
1b300 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f  lename is ":memo
1b310 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 69  ry:", then a pri
1b320 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 20  vate, temporary 
1b330 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
1b340 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 64  se.** is created
1b350 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74   for the connect
1b360 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d 65  ion.  This in-me
1b370 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69  mory database wi
1b380 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a 2a  ll vanish when.*
1b390 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  * the database c
1b3a0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
1b3b0 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72  sed.  Future ver
1b3c0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
1b3d0 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 73  might.** make us
1b3e0 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  e of additional 
1b3f0 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65  special filename
1b400 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
1b410 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63  h the ":" charac
1b420 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 65  ter..** It is re
1b430 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 77  commended that w
1b440 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  hen a database f
1b450 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c 79  ilename actually
1b460 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 68   does begin with
1b470 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 63  .** a ":" charac
1b480 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 70  ter you should p
1b490 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61  refix the filena
1b4a0 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e 61  me with a pathna
1b4b0 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 2e  me such as.** ".
1b4c0 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 69  /" to avoid ambi
1b4d0 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  guity..**.** If 
1b4e0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  the filename is 
1b4f0 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c  an empty string,
1b500 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c   then a private,
1b510 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f 6e   temporary.** on
1b520 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77  -disk database w
1b530 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 20  ill be created. 
1b540 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 61   This private da
1b550 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a 2a  tabase will be.*
1b560 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
1b570 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e 20  deleted as soon 
1b580 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  as the database 
1b590 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c  connection is cl
1b5a0 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  osed..**.** The 
1b5b0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
1b5c0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   to sqlite3_open
1b5d0 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 6d  _v2() is the nam
1b5e0 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  e of the.** [sql
1b5f0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
1b600 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
1b610 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
1b620 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  em interface tha
1b630 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74  t.** the new dat
1b640 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1b650 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 66   should use.  If
1b660 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   the fourth para
1b670 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e 55  meter is.** a NU
1b680 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20  LL pointer then 
1b690 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c  the default [sql
1b6a0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
1b6b0 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   is used..**.** 
1b6c0 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 6f  <b>Note to Windo
1b6d0 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54  ws users:</b>  T
1b6e0 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64  he encoding used
1b6f0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d   for the filenam
1b700 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66  e argument.** of
1b710 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
1b720 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  and sqlite3_open
1b730 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 54  _v2() must be UT
1b740 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65  F-8, not whateve
1b750 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73  r.** codepage is
1b760 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e   currently defin
1b770 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63  ed.  Filenames c
1b780 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e  ontaining intern
1b790 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61  ational.** chara
1b7a0 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f  cters must be co
1b7b0 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38  nverted to UTF-8
1b7c0 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e   prior to passin
1b7d0 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 73  g them into.** s
1b7e0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72  qlite3_open() or
1b7f0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
1b800 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ()..**.** Requir
1b810 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37  ements:.** [H127
1b820 30 31 5d 20 5b 48 31 32 37 30 32 5d 20 5b 48 31  01] [H12702] [H1
1b830 32 37 30 33 5d 20 5b 48 31 32 37 30 34 5d 20 5b  2703] [H12704] [
1b840 48 31 32 37 30 36 5d 20 5b 48 31 32 37 30 37 5d  H12706] [H12707]
1b850 20 5b 48 31 32 37 30 39 5d 20 5b 48 31 32 37 31   [H12709] [H1271
1b860 31 5d 0a 2a 2a 20 5b 48 31 32 37 31 32 5d 20 5b  1].** [H12712] [
1b870 48 31 32 37 31 33 5d 20 5b 48 31 32 37 31 34 5d  H12713] [H12714]
1b880 20 5b 48 31 32 37 31 37 5d 20 5b 48 31 32 37 31   [H12717] [H1271
1b890 39 5d 20 5b 48 31 32 37 32 31 5d 20 5b 48 31 32  9] [H12721] [H12
1b8a0 37 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  723].*/.SQLITE_A
1b8b0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
1b8c0 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  pen(.  const cha
1b8d0 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f  r *filename,   /
1b8e0 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e  * Database filen
1b8f0 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20  ame (UTF-8) */. 
1b900 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20   sqlite3 **ppDb 
1b910 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
1b920 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c   SQLite db handl
1b930 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  e */.);.SQLITE_A
1b940 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
1b950 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 76  pen16(.  const v
1b960 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20  oid *filename,  
1b970 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
1b980 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 20 2a  ename (UTF-16) *
1b990 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
1b9a0 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  Db          /* O
1b9b0 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61  UT: SQLite db ha
1b9c0 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54  ndle */.);.SQLIT
1b9d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b9e0 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e  3_open_v2(.  con
1b9f0 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d  st char *filenam
1ba00 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  e,   /* Database
1ba10 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38   filename (UTF-8
1ba20 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  ) */.  sqlite3 *
1ba30 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20 2f  *ppDb,         /
1ba40 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62  * OUT: SQLite db
1ba50 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74   handle */.  int
1ba60 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
1ba70 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f       /* Flags */
1ba80 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1ba90 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  Vfs        /* Na
1baa0 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65  me of VFS module
1bab0 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a 2f   to use */.);../
1bac0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
1bad0 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 4d  rror Codes And M
1bae0 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 30 7d  essages {H12800}
1baf0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S60200>.**.** 
1bb00 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 63  The sqlite3_errc
1bb10 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65 20  ode() interface 
1bb20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 65  returns the nume
1bb30 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f 64 65  ric [result code
1bb40 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65  ] or.** [extende
1bb50 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 66  d result code] f
1bb60 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  or the most rece
1bb70 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74 65  nt failed sqlite
1bb80 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a 2a 20  3_* API call.** 
1bb90 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1bba0 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  a [database conn
1bbb0 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 70 72  ection]. If a pr
1bbc0 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61 69  ior API call fai
1bbd0 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 6d  led.** but the m
1bbe0 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 20 63  ost recent API c
1bbf0 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20 74  all succeeded, t
1bc00 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
1bc10 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  from.** sqlite3_
1bc20 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 6e 64  errcode() is und
1bc30 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 71 6c  efined.  The sql
1bc40 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72  ite3_extended_er
1bc50 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65 72  rcode().** inter
1bc60 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d 65  face is the same
1bc70 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
1bc80 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 74  always returns t
1bc90 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64  he .** [extended
1bca0 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 65 76   result code] ev
1bcb0 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 65 64  en when extended
1bcc0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72   result codes ar
1bcd0 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a 2a  e.** disabled..*
1bce0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
1bcf0 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71  _errmsg() and sq
1bd00 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29  lite3_errmsg16()
1bd10 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d   return English-
1bd20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78 74  language.** text
1bd30 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
1bd40 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65 69  the error, as ei
1bd50 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54  ther UTF-8 or UT
1bd60 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 65 6c  F-16 respectivel
1bd70 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f 20  y..** Memory to 
1bd80 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6d  hold the error m
1bd90 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 73  essage string is
1bda0 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e 61   managed interna
1bdb0 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c  lly..** The appl
1bdc0 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  ication does not
1bdd0 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61   need to worry a
1bde0 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68 65  bout freeing the
1bdf0 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77 65   result..** Howe
1be00 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 73  ver, the error s
1be10 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6f  tring might be o
1be20 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64 65  verwritten or de
1be30 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20  allocated by.** 
1be40 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1be50 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65   to other SQLite
1be60 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 74   interface funct
1be70 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  ions..**.** When
1be80 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20   the serialized 
1be90 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
1bea0 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 20 6d   is in use, it m
1beb0 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a 20 63  ight be the.** c
1bec0 61 73 65 20 74 68 61 74 20 61 20 73 65 63 6f 6e  ase that a secon
1bed0 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 6f  d error occurs o
1bee0 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72  n a separate thr
1bef0 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e 0a 2a  ead in between.*
1bf00 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 74 68  * the time of th
1bf10 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 61 6e  e first error an
1bf20 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68  d the call to th
1bf30 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 2e 0a  ese interfaces..
1bf40 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61 70  ** When that hap
1bf50 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e 64  pens, the second
1bf60 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72   error will be r
1bf70 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74 68  eported since th
1bf80 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ese.** interface
1bf90 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74 20  s always report 
1bfa0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
1bfb0 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f 69  result.  To avoi
1bfc0 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68 20  d.** this, each 
1bfd0 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61 69  thread can obtai
1bfe0 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65 20  n exclusive use 
1bff0 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  of the [database
1c000 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a 2a   connection] D.*
1c010 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 5b 73  * by invoking [s
1c020 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1c030 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f  er]([sqlite3_db_
1c040 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 6f 72  mutex](D)) befor
1c050 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 74  e beginning.** t
1c060 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e 76 6f  o use D and invo
1c070 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75  king [sqlite3_mu
1c080 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 6c 69  tex_leave]([sqli
1c090 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 29  te3_db_mutex](D)
1c0a0 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c 20 63  ) after.** all c
1c0b0 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e 74 65  alls to the inte
1c0c0 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 68 65  rfaces listed he
1c0d0 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 65 64  re are completed
1c0e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e  ..**.** If an in
1c0f0 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77 69  terface fails wi
1c100 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  th SQLITE_MISUSE
1c110 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65  , that means the
1c120 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 77 61   interface.** wa
1c130 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f 72 72  s invoked incorr
1c140 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 70 70  ectly by the app
1c150 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 74 68  lication.  In th
1c160 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a 2a 20  at case, the.** 
1c170 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d  error code and m
1c180 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 6d 61  essage may or ma
1c190 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2a  y not be set..**
1c1a0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1c1b0 3a 0a 2a 2a 20 5b 48 31 32 38 30 31 5d 20 5b 48  :.** [H12801] [H
1c1c0 31 32 38 30 32 5d 20 5b 48 31 32 38 30 33 5d 20  12802] [H12803] 
1c1d0 5b 48 31 32 38 30 37 5d 20 5b 48 31 32 38 30 38  [H12807] [H12808
1c1e0 5d 20 5b 48 31 32 38 30 39 5d 0a 2a 2f 0a 53 51  ] [H12809].*/.SQ
1c1f0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1c200 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73 71 6c  ite3_errcode(sql
1c210 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54  ite3 *db);.SQLIT
1c220 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1c230 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f  3_extended_errco
1c240 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  de(sqlite3 *db);
1c250 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1c260 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
1c270 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a 29  errmsg(sqlite3*)
1c280 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
1c290 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1c2a0 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65  _errmsg16(sqlite
1c2b0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
1c2c0 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74 65 6d  3REF: SQL Statem
1c2d0 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 30  ent Object {H130
1c2e0 30 30 7d 20 3c 48 31 33 30 31 30 3e 0a 2a 2a 20  00} <H13010>.** 
1c2f0 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 65 70 61  KEYWORDS: {prepa
1c300 72 65 64 20 73 74 61 74 65 6d 65 6e 74 7d 20 7b  red statement} {
1c310 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1c320 6e 74 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  nts}.**.** An in
1c330 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
1c340 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73  bject represents
1c350 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74   a single SQL st
1c360 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 69 73  atement..** This
1c370 20 6f 62 6a 65 63 74 20 69 73 20 76 61 72 69 6f   object is vario
1c380 75 73 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 61 20  usly known as a 
1c390 22 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  "prepared statem
1c3a0 65 6e 74 22 20 6f 72 20 61 0a 2a 2a 20 22 63 6f  ent" or a.** "co
1c3b0 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65  mpiled SQL state
1c3c0 6d 65 6e 74 22 20 6f 72 20 73 69 6d 70 6c 79 20  ment" or simply 
1c3d0 61 73 20 61 20 22 73 74 61 74 65 6d 65 6e 74 22  as a "statement"
1c3e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 66 65  ..**.** The life
1c3f0 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   of a statement 
1c400 6f 62 6a 65 63 74 20 67 6f 65 73 20 73 6f 6d 65  object goes some
1c410 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a  thing like this:
1c420 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c  .**.** <ol>.** <
1c430 6c 69 3e 20 43 72 65 61 74 65 20 74 68 65 20 6f  li> Create the o
1c440 62 6a 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c  bject using [sql
1c450 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
1c460 29 5d 20 6f 72 20 61 20 72 65 6c 61 74 65 64 0a  )] or a related.
1c470 2a 2a 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e  **      function
1c480 2e 0a 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64 20 76  ..** <li> Bind v
1c490 61 6c 75 65 73 20 74 6f 20 5b 68 6f 73 74 20 70  alues to [host p
1c4a0 61 72 61 6d 65 74 65 72 73 5d 20 75 73 69 6e 67  arameters] using
1c4b0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   the sqlite3_bin
1c4c0 64 5f 2a 28 29 0a 2a 2a 20 20 20 20 20 20 69 6e  d_*().**      in
1c4d0 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 3c 6c 69  terfaces..** <li
1c4e0 3e 20 52 75 6e 20 74 68 65 20 53 51 4c 20 62 79  > Run the SQL by
1c4f0 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65   calling [sqlite
1c500 33 5f 73 74 65 70 28 29 5d 20 6f 6e 65 20 6f 72  3_step()] one or
1c510 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 20   more times..** 
1c520 3c 6c 69 3e 20 52 65 73 65 74 20 74 68 65 20 73  <li> Reset the s
1c530 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b  tatement using [
1c540 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
1c550 20 74 68 65 6e 20 67 6f 20 62 61 63 6b 0a 2a 2a   then go back.**
1c560 20 20 20 20 20 20 74 6f 20 73 74 65 70 20 32 2e        to step 2.
1c570 20 20 44 6f 20 74 68 69 73 20 7a 65 72 6f 20 6f    Do this zero o
1c580 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a  r more times..**
1c590 20 3c 6c 69 3e 20 44 65 73 74 72 6f 79 20 74 68   <li> Destroy th
1c5a0 65 20 6f 62 6a 65 63 74 20 75 73 69 6e 67 20 5b  e object using [
1c5b0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
1c5c0 28 29 5d 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a  ()]..** </ol>.**
1c5d0 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 64 6f 63  .** Refer to doc
1c5e0 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 69 6e  umentation on in
1c5f0 64 69 76 69 64 75 61 6c 20 6d 65 74 68 6f 64 73  dividual methods
1c600 20 61 62 6f 76 65 20 66 6f 72 20 61 64 64 69 74   above for addit
1c610 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ional.** informa
1c620 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
1c630 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
1c640 73 74 6d 74 20 73 71 6c 69 74 65 33 5f 73 74 6d  stmt sqlite3_stm
1c650 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  t;../*.** CAPI3R
1c660 45 46 3a 20 52 75 6e 2d 74 69 6d 65 20 4c 69 6d  EF: Run-time Lim
1c670 69 74 73 20 7b 48 31 32 37 36 30 7d 20 3c 53 32  its {H12760} <S2
1c680 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0600>.**.** This
1c690 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77   interface allow
1c6a0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 76 61  s the size of va
1c6b0 72 69 6f 75 73 20 63 6f 6e 73 74 72 75 63 74 73  rious constructs
1c6c0 20 74 6f 20 62 65 20 6c 69 6d 69 74 65 64 0a 2a   to be limited.*
1c6d0 2a 20 6f 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f  * on a connectio
1c6e0 6e 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  n by connection 
1c6f0 62 61 73 69 73 2e 20 20 54 68 65 20 66 69 72 73  basis.  The firs
1c700 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  t parameter is t
1c710 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20  he.** [database 
1c720 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 68 6f 73  connection] whos
1c730 65 20 6c 69 6d 69 74 20 69 73 20 74 6f 20 62 65  e limit is to be
1c740 20 73 65 74 20 6f 72 20 71 75 65 72 69 65 64 2e   set or queried.
1c750 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20    The.** second 
1c760 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 65  parameter is one
1c770 20 6f 66 20 74 68 65 20 5b 6c 69 6d 69 74 20 63   of the [limit c
1c780 61 74 65 67 6f 72 69 65 73 5d 20 74 68 61 74 20  ategories] that 
1c790 64 65 66 69 6e 65 20 61 0a 2a 2a 20 63 6c 61 73  define a.** clas
1c7a0 73 20 6f 66 20 63 6f 6e 73 74 72 75 63 74 73 20  s of constructs 
1c7b0 74 6f 20 62 65 20 73 69 7a 65 20 6c 69 6d 69 74  to be size limit
1c7c0 65 64 2e 20 20 54 68 65 20 74 68 69 72 64 20 70  ed.  The third p
1c7d0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a  arameter is the.
1c7e0 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 66 6f 72  ** new limit for
1c7f0 20 74 68 61 74 20 63 6f 6e 73 74 72 75 63 74 2e   that construct.
1c800 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 72    The function r
1c810 65 74 75 72 6e 73 20 74 68 65 20 6f 6c 64 20 6c  eturns the old l
1c820 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  imit..**.** If t
1c830 68 65 20 6e 65 77 20 6c 69 6d 69 74 20 69 73 20  he new limit is 
1c840 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
1c850 72 2c 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20  r, the limit is 
1c860 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 46 6f  unchanged..** Fo
1c870 72 20 74 68 65 20 6c 69 6d 69 74 20 63 61 74 65  r the limit cate
1c880 67 6f 72 79 20 6f 66 20 53 51 4c 49 54 45 5f 4c  gory of SQLITE_L
1c890 49 4d 49 54 5f 58 59 5a 20 74 68 65 72 65 20 69  IMIT_XYZ there i
1c8a0 73 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20  s a .** [limits 
1c8b0 7c 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75  | hard upper bou
1c8c0 6e 64 5d 0a 2a 2a 20 73 65 74 20 62 79 20 61 20  nd].** set by a 
1c8d0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 43 20 70  compile-time C p
1c8e0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
1c8f0 6f 20 6e 61 6d 65 64 20 0a 2a 2a 20 5b 6c 69 6d  o named .** [lim
1c900 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d 41 58  its | SQLITE_MAX
1c910 5f 58 59 5a 5d 2e 0a 2a 2a 20 28 54 68 65 20 22  _XYZ]..** (The "
1c920 5f 4c 49 4d 49 54 5f 22 20 69 6e 20 74 68 65 20  _LIMIT_" in the 
1c930 6e 61 6d 65 20 69 73 20 63 68 61 6e 67 65 64 20  name is changed 
1c940 74 6f 20 22 5f 4d 41 58 5f 22 2e 29 0a 2a 2a 20  to "_MAX_".).** 
1c950 41 74 74 65 6d 70 74 73 20 74 6f 20 69 6e 63 72  Attempts to incr
1c960 65 61 73 65 20 61 20 6c 69 6d 69 74 20 61 62 6f  ease a limit abo
1c970 76 65 20 69 74 73 20 68 61 72 64 20 75 70 70 65  ve its hard uppe
1c980 72 20 62 6f 75 6e 64 20 61 72 65 0a 2a 2a 20 73  r bound are.** s
1c990 69 6c 65 6e 74 6c 79 20 74 72 75 6e 63 61 74 65  ilently truncate
1c9a0 64 20 74 6f 20 74 68 65 20 68 61 72 64 20 75 70  d to the hard up
1c9b0 70 65 72 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a  per limit..**.**
1c9c0 20 52 75 6e 20 74 69 6d 65 20 6c 69 6d 69 74 73   Run time limits
1c9d0 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f   are intended fo
1c9e0 72 20 75 73 65 20 69 6e 20 61 70 70 6c 69 63 61  r use in applica
1c9f0 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6e 61 67  tions that manag
1ca00 65 0a 2a 2a 20 62 6f 74 68 20 74 68 65 69 72 20  e.** both their 
1ca10 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 64 61 74  own internal dat
1ca20 61 62 61 73 65 20 61 6e 64 20 61 6c 73 6f 20 64  abase and also d
1ca30 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61 72  atabases that ar
1ca40 65 20 63 6f 6e 74 72 6f 6c 6c 65 64 0a 2a 2a 20  e controlled.** 
1ca50 62 79 20 75 6e 74 72 75 73 74 65 64 20 65 78 74  by untrusted ext
1ca60 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 2e 20 20  ernal sources.  
1ca70 41 6e 20 65 78 61 6d 70 6c 65 20 61 70 70 6c 69  An example appli
1ca80 63 61 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20  cation might be 
1ca90 61 0a 2a 2a 20 77 65 62 20 62 72 6f 77 73 65 72  a.** web browser
1caa0 20 74 68 61 74 20 68 61 73 20 69 74 73 20 6f 77   that has its ow
1cab0 6e 20 64 61 74 61 62 61 73 65 73 20 66 6f 72 20  n databases for 
1cac0 73 74 6f 72 69 6e 67 20 68 69 73 74 6f 72 79 20  storing history 
1cad0 61 6e 64 0a 2a 2a 20 73 65 70 61 72 61 74 65 20  and.** separate 
1cae0 64 61 74 61 62 61 73 65 73 20 63 6f 6e 74 72 6f  databases contro
1caf0 6c 6c 65 64 20 62 79 20 4a 61 76 61 53 63 72 69  lled by JavaScri
1cb00 70 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  pt applications 
1cb10 64 6f 77 6e 6c 6f 61 64 65 64 0a 2a 2a 20 6f 66  downloaded.** of
1cb20 66 20 74 68 65 20 49 6e 74 65 72 6e 65 74 2e 20  f the Internet. 
1cb30 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 61   The internal da
1cb40 74 61 62 61 73 65 73 20 63 61 6e 20 62 65 20 67  tabases can be g
1cb50 69 76 65 6e 20 74 68 65 0a 2a 2a 20 6c 61 72 67  iven the.** larg
1cb60 65 2c 20 64 65 66 61 75 6c 74 20 6c 69 6d 69 74  e, default limit
1cb70 73 2e 20 20 44 61 74 61 62 61 73 65 73 20 6d 61  s.  Databases ma
1cb80 6e 61 67 65 64 20 62 79 20 65 78 74 65 72 6e 61  naged by externa
1cb90 6c 20 73 6f 75 72 63 65 73 20 63 61 6e 0a 2a 2a  l sources can.**
1cba0 20 62 65 20 67 69 76 65 6e 20 6d 75 63 68 20 73   be given much s
1cbb0 6d 61 6c 6c 65 72 20 6c 69 6d 69 74 73 20 64 65  maller limits de
1cbc0 73 69 67 6e 65 64 20 74 6f 20 70 72 65 76 65 6e  signed to preven
1cbd0 74 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 73 65  t a denial of se
1cbe0 72 76 69 63 65 0a 2a 2a 20 61 74 74 61 63 6b 2e  rvice.** attack.
1cbf0 20 20 44 65 76 65 6c 6f 70 65 72 73 20 6d 69 67    Developers mig
1cc00 68 74 20 61 6c 73 6f 20 77 61 6e 74 20 74 6f 20  ht also want to 
1cc10 75 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33  use the [sqlite3
1cc20 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
1cc30 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  )].** interface 
1cc40 74 6f 20 66 75 72 74 68 65 72 20 63 6f 6e 74 72  to further contr
1cc50 6f 6c 20 75 6e 74 72 75 73 74 65 64 20 53 51 4c  ol untrusted SQL
1cc60 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  .  The size of t
1cc70 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  he database.** c
1cc80 72 65 61 74 65 64 20 62 79 20 61 6e 20 75 6e 74  reated by an unt
1cc90 72 75 73 74 65 64 20 73 63 72 69 70 74 20 63 61  rusted script ca
1cca0 6e 20 62 65 20 63 6f 6e 74 61 69 6e 65 64 20 75  n be contained u
1ccb0 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 6d 61 78  sing the.** [max
1ccc0 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52  _page_count] [PR
1ccd0 41 47 4d 41 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  AGMA]..**.** New
1cce0 20 72 75 6e 2d 74 69 6d 65 20 6c 69 6d 69 74 20   run-time limit 
1ccf0 63 61 74 65 67 6f 72 69 65 73 20 6d 61 79 20 62  categories may b
1cd00 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
1cd10 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a  e releases..**.*
1cd20 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
1cd30 2a 2a 20 5b 48 31 32 37 36 32 5d 20 5b 48 31 32  ** [H12762] [H12
1cd40 37 36 36 5d 20 5b 48 31 32 37 36 39 5d 0a 2a 2f  766] [H12769].*/
1cd50 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1cd60 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71  sqlite3_limit(sq
1cd70 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20  lite3*, int id, 
1cd80 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a  int newVal);../*
1cd90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75  .** CAPI3REF: Ru
1cda0 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74  n-Time Limit Cat
1cdb0 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d  egories {H12790}
1cdc0 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59   <H12760>.** KEY
1cdd0 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61  WORDS: {limit ca
1cde0 74 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63  tegory} {limit c
1cdf0 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a  ategories}.**.**
1ce00 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   These constants
1ce10 20 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20   define various 
1ce20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6c 69 6d 69  performance limi
1ce30 74 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62  ts.** that can b
1ce40 65 20 6c 6f 77 65 72 65 64 20 61 74 20 72 75 6e  e lowered at run
1ce50 2d 74 69 6d 65 20 75 73 69 6e 67 20 5b 73 71 6c  -time using [sql
1ce60 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e 0a 2a  ite3_limit()]..*
1ce70 2a 20 54 68 65 20 73 79 6e 6f 70 73 69 73 20 6f  * The synopsis o
1ce80 66 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f  f the meanings o
1ce90 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 69  f the various li
1cea0 6d 69 74 73 20 69 73 20 73 68 6f 77 6e 20 62 65  mits is shown be
1ceb0 6c 6f 77 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e  low..** Addition
1cec0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  al information i
1ced0 73 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 5b  s available at [
1cee0 6c 69 6d 69 74 73 20 7c 20 4c 69 6d 69 74 73 20  limits | Limits 
1cef0 69 6e 20 53 51 4c 69 74 65 5d 2e 0a 2a 2a 0a 2a  in SQLite]..**.*
1cf00 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
1cf10 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1cf20 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  H</dt>.** <dd>Th
1cf30 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f  e maximum size o
1cf40 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 20  f any string or 
1cf50 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65 20 72 6f  BLOB or table ro
1cf60 77 2e 3c 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  w.<dd>.**.** <dt
1cf70 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51  >SQLITE_LIMIT_SQ
1cf80 4c 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a  L_LENGTH</dt>.**
1cf90 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d   <dd>The maximum
1cfa0 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e 20 53 51   length of an SQ
1cfb0 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64  L statement.</dd
1cfc0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1cfd0 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 3c  TE_LIMIT_COLUMN<
1cfe0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
1cff0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1d000 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74  f columns in a t
1d010 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 20  able definition 
1d020 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 72 65 73  or in the.** res
1d030 75 6c 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45  ult set of a [SE
1d040 4c 45 43 54 5d 20 6f 72 20 74 68 65 20 6d 61 78  LECT] or the max
1d050 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63  imum number of c
1d060 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64  olumns in an ind
1d070 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61 6e 20 4f  ex.** or in an O
1d080 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
1d090 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f 64 64 3e   BY clause.</dd>
1d0a0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
1d0b0 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50  E_LIMIT_EXPR_DEP
1d0c0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  TH</dt>.** <dd>T
1d0d0 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68  he maximum depth
1d0e0 20 6f 66 20 74 68 65 20 70 61 72 73 65 20 74 72   of the parse tr
1d0f0 65 65 20 6f 6e 20 61 6e 79 20 65 78 70 72 65 73  ee on any expres
1d100 73 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  sion.</dd>.**.**
1d110 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49   <dt>SQLITE_LIMI
1d120 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
1d130 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  T</dt>.** <dd>Th
1d140 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1d150 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63   of terms in a c
1d160 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73  ompound SELECT s
1d170 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a  tatement.</dd>.*
1d180 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
1d190 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50 3c 2f 64  LIMIT_VDBE_OP</d
1d1a0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1d1b0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1d1c0 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20  instructions in 
1d1d0 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  a virtual machin
1d1e0 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 75 73 65  e program.** use
1d1f0 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  d to implement a
1d200 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  n SQL statement.
1d210 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
1d220 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e  SQLITE_LIMIT_FUN
1d230 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74 3e 0a 2a  CTION_ARG</dt>.*
1d240 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1d250 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  m number of argu
1d260 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75 6e 63 74  ments on a funct
1d270 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ion.</dd>.**.** 
1d280 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1d290 5f 41 54 54 41 43 48 45 44 3c 2f 64 74 3e 0a 2a  _ATTACHED</dt>.*
1d2a0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1d2b0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 5b 41 54 54  m number of [ATT
1d2c0 41 43 48 20 7c 20 61 74 74 61 63 68 65 64 20 64  ACH | attached d
1d2d0 61 74 61 62 61 73 65 73 5d 2e 3c 2f 64 64 3e 0a  atabases].</dd>.
1d2e0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
1d2f0 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54  _LIMIT_LIKE_PATT
1d300 45 52 4e 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a  ERN_LENGTH</dt>.
1d310 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
1d320 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  um length of the
1d330 20 70 61 74 74 65 72 6e 20 61 72 67 75 6d 65 6e   pattern argumen
1d340 74 20 74 6f 20 74 68 65 20 5b 4c 49 4b 45 5d 20  t to the [LIKE] 
1d350 6f 72 0a 2a 2a 20 5b 47 4c 4f 42 5d 20 6f 70 65  or.** [GLOB] ope
1d360 72 61 74 6f 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a  rators.</dd>.**.
1d370 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
1d380 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
1d390 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  BER</dt>.** <dd>
1d3a0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1d3b0 65 72 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20  er of variables 
1d3c0 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  in an SQL statem
1d3d0 65 6e 74 20 74 68 61 74 20 63 61 6e 0a 2a 2a 20  ent that can.** 
1d3e0 62 65 20 62 6f 75 6e 64 2e 3c 2f 64 64 3e 0a 2a  be bound.</dd>.*
1d3f0 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69  * </dl>.*/.#defi
1d400 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1d410 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20  LENGTH          
1d420 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66            0.#def
1d430 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
1d440 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 20 20 20 20  _SQL_LENGTH     
1d450 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65             1.#de
1d460 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
1d470 54 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20  T_COLUMN        
1d480 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64              2.#d
1d490 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
1d4a0 49 54 5f 45 58 50 52 5f 44 45 50 54 48 20 20 20  IT_EXPR_DEPTH   
1d4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23               3.#
1d4c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
1d4d0 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
1d4e0 45 43 54 20 20 20 20 20 20 20 20 20 20 20 34 0a  ECT           4.
1d4f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
1d500 49 4d 49 54 5f 56 44 42 45 5f 4f 50 20 20 20 20  IMIT_VDBE_OP    
1d510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
1d520 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1d530 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41  LIMIT_FUNCTION_A
1d540 52 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RG              
1d550 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  6.#define SQLITE
1d560 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 20  _LIMIT_ATTACHED 
1d570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d580 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   7.#define SQLIT
1d590 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54  E_LIMIT_LIKE_PAT
1d5a0 54 45 52 4e 5f 4c 45 4e 47 54 48 20 20 20 20 20  TERN_LENGTH     
1d5b0 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    8.#define SQLI
1d5c0 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c  TE_LIMIT_VARIABL
1d5d0 45 5f 4e 55 4d 42 45 52 20 20 20 20 20 20 20 20  E_NUMBER        
1d5e0 20 20 20 39 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49     9../*.** CAPI
1d5f0 33 52 45 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20  3REF: Compiling 
1d600 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74  An SQL Statement
1d610 20 7b 48 31 33 30 31 30 7d 20 3c 53 31 30 30 30   {H13010} <S1000
1d620 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
1d630 7b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63  {SQL statement c
1d640 6f 6d 70 69 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54  ompiler}.**.** T
1d650 6f 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c  o execute an SQL
1d660 20 71 75 65 72 79 2c 20 69 74 20 6d 75 73 74 20   query, it must 
1d670 66 69 72 73 74 20 62 65 20 63 6f 6d 70 69 6c 65  first be compile
1d680 64 20 69 6e 74 6f 20 61 20 62 79 74 65 2d 63 6f  d into a byte-co
1d690 64 65 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73  de.** program us
1d6a0 69 6e 67 20 6f 6e 65 20 6f 66 20 74 68 65 73 65  ing one of these
1d6b0 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a   routines..**.**
1d6c0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
1d6d0 65 6e 74 2c 20 22 64 62 22 2c 20 69 73 20 61 20  ent, "db", is a 
1d6e0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
1d6f0 74 69 6f 6e 5d 20 6f 62 74 61 69 6e 65 64 20 66  tion] obtained f
1d700 72 6f 6d 20 61 0a 2a 2a 20 70 72 69 6f 72 20 63  rom a.** prior c
1d710 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
1d720 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65  open()], [sqlite
1d730 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f 72 20  3_open_v2()] or 
1d740 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
1d750 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  )]..**.** The se
1d760 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22  cond argument, "
1d770 7a 53 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74  zSql", is the st
1d780 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f  atement to be co
1d790 6d 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a  mpiled, encoded.
1d7a0 2a 2a 20 61 73 20 65 69 74 68 65 72 20 55 54 46  ** as either UTF
1d7b0 2d 38 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54  -8 or UTF-16.  T
1d7c0 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  he sqlite3_prepa
1d7d0 72 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  re() and sqlite3
1d7e0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a  _prepare_v2().**
1d7f0 20 69 6e 74 65 72 66 61 63 65 73 20 75 73 65 20   interfaces use 
1d800 55 54 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74  UTF-8, and sqlit
1d810 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 20 61  e3_prepare16() a
1d820 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nd sqlite3_prepa
1d830 72 65 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65  re16_v2().** use
1d840 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49   UTF-16..**.** I
1d850 66 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75  f the nByte argu
1d860 6d 65 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61  ment is less tha
1d870 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71  n zero, then zSq
1d880 6c 20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20  l is read up to 
1d890 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72  the.** first zer
1d8a0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66  o terminator. If
1d8b0 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65   nByte is non-ne
1d8c0 67 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20  gative, then it 
1d8d0 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a  is the maximum.*
1d8e0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74  * number of  byt
1d8f0 65 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71  es read from zSq
1d900 6c 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69  l.  When nByte i
1d910 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20  s non-negative, 
1d920 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69  the.** zSql stri
1d930 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65  ng ends at eithe
1d940 72 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30  r the first '\00
1d950 30 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63  0' or '\u0000' c
1d960 68 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74  haracter or.** t
1d970 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65  he nByte-th byte
1d980 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65  , whichever come
1d990 73 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20  s first. If the 
1d9a0 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20  caller knows.** 
1d9b0 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65  that the supplie
1d9c0 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d  d string is nul-
1d9d0 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e  terminated, then
1d9e0 20 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c   there is a smal
1d9f0 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  l.** performance
1da00 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65   advantage to be
1da10 20 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69   gained by passi
1da20 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61  ng an nByte para
1da30 6d 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73  meter that.** is
1da40 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75   equal to the nu
1da50 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
1da60 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
1da70 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f  g <i>including</
1da80 69 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65  i>.** the nul-te
1da90 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a  rminator bytes..
1daa0 2a 2a 0a 2a 2a 20 2a 70 7a 54 61 69 6c 20 69 73  **.** *pzTail is
1dab0 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74   made to point t
1dac0 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  o the first byte
1dad0 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
1dae0 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 53 51   the.** first SQ
1daf0 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a  L statement in z
1db00 53 71 6c 2e 20 20 54 68 65 73 65 20 72 6f 75 74  Sql.  These rout
1db10 69 6e 65 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c  ines only compil
1db20 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 73  e the first.** s
1db30 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c  tatement in zSql
1db40 2c 20 73 6f 20 2a 70 7a 54 61 69 6c 20 69 73 20  , so *pzTail is 
1db50 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f  left pointing to
1db60 20 77 68 61 74 20 72 65 6d 61 69 6e 73 0a 2a 2a   what remains.**
1db70 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 0a   uncompiled..**.
1db80 2a 2a 20 2a 70 70 53 74 6d 74 20 69 73 20 6c 65  ** *ppStmt is le
1db90 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61  ft pointing to a
1dba0 20 63 6f 6d 70 69 6c 65 64 20 5b 70 72 65 70 61   compiled [prepa
1dbb0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
1dbc0 68 61 74 20 63 61 6e 20 62 65 0a 2a 2a 20 65 78  hat can be.** ex
1dbd0 65 63 75 74 65 64 20 75 73 69 6e 67 20 5b 73 71  ecuted using [sq
1dbe0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 20 20  lite3_step()].  
1dbf0 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  If there is an e
1dc00 72 72 6f 72 2c 20 2a 70 70 53 74 6d 74 20 69 73  rror, *ppStmt is
1dc10 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e   set.** to NULL.
1dc20 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 74    If the input t
1dc30 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20  ext contains no 
1dc40 53 51 4c 20 28 69 66 20 74 68 65 20 69 6e 70 75  SQL (if the inpu
1dc50 74 20 69 73 20 61 6e 20 65 6d 70 74 79 0a 2a 2a  t is an empty.**
1dc60 20 73 74 72 69 6e 67 20 6f 72 20 61 20 63 6f 6d   string or a com
1dc70 6d 65 6e 74 29 20 74 68 65 6e 20 2a 70 70 53 74  ment) then *ppSt
1dc80 6d 74 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c  mt is set to NUL
1dc90 4c 2e 0a 2a 2a 20 7b 41 31 33 30 31 38 7d 20 54  L..** {A13018} T
1dca0 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65  he calling proce
1dcb0 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e 73 69  dure is responsi
1dcc0 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 6e 67  ble for deleting
1dcd0 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a   the compiled.**
1dce0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75   SQL statement u
1dcf0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
1dd00 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 72 20  nalize()] after 
1dd10 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  it has finished 
1dd20 77 69 74 68 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f  with it..**.** O
1dd30 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49  n success, [SQLI
1dd40 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e  TE_OK] is return
1dd50 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e  ed, otherwise an
1dd60 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
1dd70 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
1dd80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   The sqlite3_pre
1dd90 70 61 72 65 5f 76 32 28 29 20 61 6e 64 20 73 71  pare_v2() and sq
1dda0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
1ddb0 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 73 20  v2() interfaces 
1ddc0 61 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64  are.** recommend
1ddd0 65 64 20 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70  ed for all new p
1dde0 72 6f 67 72 61 6d 73 2e 20 54 68 65 20 74 77 6f  rograms. The two
1ddf0 20 6f 6c 64 65 72 20 69 6e 74 65 72 66 61 63 65   older interface
1de00 73 20 61 72 65 20 72 65 74 61 69 6e 65 64 0a 2a  s are retained.*
1de10 2a 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20  * for backwards 
1de20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 62  compatibility, b
1de30 75 74 20 74 68 65 69 72 20 75 73 65 20 69 73 20  ut their use is 
1de40 64 69 73 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20  discouraged..** 
1de50 49 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65  In the "v2" inte
1de60 72 66 61 63 65 73 2c 20 74 68 65 20 70 72 65 70  rfaces, the prep
1de70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
1de80 2a 20 74 68 61 74 20 69 73 20 72 65 74 75 72 6e  * that is return
1de90 65 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33  ed (the [sqlite3
1dea0 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 29 20 63  _stmt] object) c
1deb0 6f 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20 6f  ontains a copy o
1dec0 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61  f the.** origina
1ded0 6c 20 53 51 4c 20 74 65 78 74 2e 20 54 68 69 73  l SQL text. This
1dee0 20 63 61 75 73 65 73 20 74 68 65 20 5b 73 71 6c   causes the [sql
1def0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 69 6e 74  ite3_step()] int
1df00 65 72 66 61 63 65 20 74 6f 0a 2a 2a 20 62 65 68  erface to.** beh
1df10 61 76 65 20 61 20 64 69 66 66 65 72 65 6e 74 6c  ave a differentl
1df20 79 20 69 6e 20 74 77 6f 20 77 61 79 73 3a 0a 2a  y in two ways:.*
1df30 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ol>.** <li
1df40 3e 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61  >.** If the data
1df50 62 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e  base schema chan
1df60 67 65 73 2c 20 69 6e 73 74 65 61 64 20 6f 66 20  ges, instead of 
1df70 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54  returning [SQLIT
1df80 45 5f 53 43 48 45 4d 41 5d 20 61 73 20 69 74 0a  E_SCHEMA] as it.
1df90 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 64 20 74  ** always used t
1dfa0 6f 20 64 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73  o do, [sqlite3_s
1dfb0 74 65 70 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f  tep()] will auto
1dfc0 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70  matically recomp
1dfd0 69 6c 65 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73  ile the SQL.** s
1dfe0 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 72 79  tatement and try
1dff0 20 74 6f 20 72 75 6e 20 69 74 20 61 67 61 69 6e   to run it again
1e000 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61  .  If the schema
1e010 20 68 61 73 20 63 68 61 6e 67 65 64 20 69 6e 0a   has changed in.
1e020 2a 2a 20 61 20 77 61 79 20 74 68 61 74 20 6d 61  ** a way that ma
1e030 6b 65 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e  kes the statemen
1e040 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69  t no longer vali
1e050 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  d, [sqlite3_step
1e060 28 29 5d 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a  ()] will still.*
1e070 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45  * return [SQLITE
1e080 5f 53 43 48 45 4d 41 5d 2e 20 20 42 75 74 20 75  _SCHEMA].  But u
1e090 6e 6c 69 6b 65 20 74 68 65 20 6c 65 67 61 63 79  nlike the legacy
1e0a0 20 62 65 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49   behavior, [SQLI
1e0b0 54 45 5f 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a  TE_SCHEMA] is.**
1e0c0 20 6e 6f 77 20 61 20 66 61 74 61 6c 20 65 72 72   now a fatal err
1e0d0 6f 72 2e 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71  or.  Calling [sq
1e0e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1e0f0 28 29 5d 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e  ()] again will n
1e100 6f 74 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65  ot make the.** e
1e110 72 72 6f 72 20 67 6f 20 61 77 61 79 2e 20 20 4e  rror go away.  N
1e120 6f 74 65 3a 20 75 73 65 20 5b 73 71 6c 69 74 65  ote: use [sqlite
1e130 33 5f 65 72 72 6d 73 67 28 29 5d 20 74 6f 20 66  3_errmsg()] to f
1e140 69 6e 64 20 74 68 65 20 74 65 78 74 0a 2a 2a 20  ind the text.** 
1e150 6f 66 20 74 68 65 20 70 61 72 73 69 6e 67 20 65  of the parsing e
1e160 72 72 6f 72 20 74 68 61 74 20 72 65 73 75 6c 74  rror that result
1e170 73 20 69 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f  s in an [SQLITE_
1e180 53 43 48 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a  SCHEMA] return..
1e190 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c  ** </li>.**.** <
1e1a0 6c 69 3e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65  li>.** When an e
1e1b0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 5b 73 71  rror occurs, [sq
1e1c0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69  lite3_step()] wi
1e1d0 6c 6c 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66  ll return one of
1e1e0 20 74 68 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a   the detailed.**
1e1f0 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f   [error codes] o
1e200 72 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f  r [extended erro
1e210 72 20 63 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c  r codes].  The l
1e220 65 67 61 63 79 20 62 65 68 61 76 69 6f 72 20 77  egacy behavior w
1e230 61 73 20 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69  as that.** [sqli
1e240 74 65 33 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c  te3_step()] woul
1e250 64 20 6f 6e 6c 79 20 72 65 74 75 72 6e 20 61 20  d only return a 
1e260 67 65 6e 65 72 69 63 20 5b 53 51 4c 49 54 45 5f  generic [SQLITE_
1e270 45 52 52 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f  ERROR] result co
1e280 64 65 0a 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f  de.** and you wo
1e290 75 6c 64 20 68 61 76 65 20 74 6f 20 6d 61 6b 65  uld have to make
1e2a0 20 61 20 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74   a second call t
1e2b0 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  o [sqlite3_reset
1e2c0 28 29 5d 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20  ()] in order.** 
1e2d0 74 6f 20 66 69 6e 64 20 74 68 65 20 75 6e 64 65  to find the unde
1e2e0 72 6c 79 69 6e 67 20 63 61 75 73 65 20 6f 66 20  rlying cause of 
1e2f0 74 68 65 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74  the problem. Wit
1e300 68 20 74 68 65 20 22 76 32 22 20 70 72 65 70 61  h the "v2" prepa
1e310 72 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73  re.** interfaces
1e320 2c 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  , the underlying
1e330 20 72 65 61 73 6f 6e 20 66 6f 72 20 74 68 65 20   reason for the 
1e340 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
1e350 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a  d immediately..*
1e360 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e  * </li>.** </ol>
1e370 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
1e380 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 31 31 5d  nts:.** [H13011]
1e390 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 33 30 31   [H13012] [H1301
1e3a0 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 48 31 33  3] [H13014] [H13
1e3b0 30 31 35 5d 20 5b 48 31 33 30 31 36 5d 20 5b 48  015] [H13016] [H
1e3c0 31 33 30 31 39 5d 20 5b 48 31 33 30 32 31 5d 0a  13019] [H13021].
1e3d0 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  **.*/.SQLITE_API
1e3e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
1e3f0 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20  pare(.  sqlite3 
1e400 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
1e410 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
1e420 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
1e430 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
1e440 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  /* SQL statement
1e450 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  , UTF-8 encoded 
1e460 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20  */.  int nByte, 
1e470 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e480 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  Maximum length o
1e490 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
1e4a0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
1e4b0 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a  mt **ppStmt,  /*
1e4c0 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20   OUT: Statement 
1e4d0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
1e4e0 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20  t char **pzTail 
1e4f0 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e      /* OUT: Poin
1e500 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f  ter to unused po
1e510 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f  rtion of zSql */
1e520 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
1e530 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nt sqlite3_prepa
1e540 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  re_v2(.  sqlite3
1e550 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
1e560 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
1e570 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
1e580 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  har *zSql,      
1e590 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
1e5a0 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  t, UTF-8 encoded
1e5b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c   */.  int nByte,
1e5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e5d0 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   Maximum length 
1e5e0 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
1e5f0 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
1e600 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f  tmt **ppStmt,  /
1e610 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74  * OUT: Statement
1e620 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
1e630 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  st char **pzTail
1e640 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69       /* OUT: Poi
1e650 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70  nter to unused p
1e660 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a  ortion of zSql *
1e670 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  /.);.SQLITE_API 
1e680 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  int sqlite3_prep
1e690 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33  are16(.  sqlite3
1e6a0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
1e6b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
1e6c0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  dle */.  const v
1e6d0 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  oid *zSql,      
1e6e0 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
1e6f0 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  t, UTF-16 encode
1e700 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  d */.  int nByte
1e710 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1e720 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
1e730 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
1e740 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
1e750 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
1e760 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e  /* OUT: Statemen
1e770 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  t handle */.  co
1e780 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
1e790 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f  l     /* OUT: Po
1e7a0 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20  inter to unused 
1e7b0 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20  portion of zSql 
1e7c0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  */.);.SQLITE_API
1e7d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
1e7e0 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 71 6c  pare16_v2(.  sql
1e7f0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
1e800 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1e810 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
1e820 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20  st void *zSql,  
1e830 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74       /* SQL stat
1e840 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e  ement, UTF-16 en
1e850 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  coded */.  int n
1e860 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  Byte,           
1e870 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65     /* Maximum le
1e880 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
1e890 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
1e8a0 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
1e8b0 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74  t,  /* OUT: Stat
1e8c0 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a  ement handle */.
1e8d0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70    const void **p
1e8e0 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54  zTail     /* OUT
1e8f0 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75  : Pointer to unu
1e900 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a  sed portion of z
1e910 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  Sql */.);../*.**
1e920 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69   CAPI3REF: Retri
1e930 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e 74 20  eving Statement 
1e940 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 3c 48 31  SQL {H13100} <H1
1e950 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  3000>.**.** This
1e960 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62   interface can b
1e970 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65  e used to retrie
1e980 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 79 20  ve a saved copy 
1e990 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a  of the original.
1e9a0 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73 65 64  ** SQL text used
1e9b0 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b 70 72   to create a [pr
1e9c0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1e9d0 5d 20 69 66 20 74 68 61 74 20 73 74 61 74 65 6d  ] if that statem
1e9e0 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 69  ent was.** compi
1e9f0 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72  led using either
1ea00 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
1ea10 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69  e_v2()] or [sqli
1ea20 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
1ea30 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
1ea40 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
1ea50 31 30 31 5d 20 5b 48 31 33 31 30 32 5d 20 5b 48  101] [H13102] [H
1ea60 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13103].*/.SQLITE
1ea70 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
1ea80 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c  *sqlite3_sql(sql
1ea90 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1eaa0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
1eab0 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20  EF: Dynamically 
1eac0 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62 6a 65  Typed Value Obje
1ead0 63 74 20 7b 48 31 35 30 30 30 7d 20 3c 53 32 30  ct {H15000} <S20
1eae0 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  200>.** KEYWORDS
1eaf0 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 73 71 6c  : {protected sql
1eb00 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75 6e 70  ite3_value} {unp
1eb10 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
1eb20 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53 51  _value}.**.** SQ
1eb30 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 71  Lite uses the sq
1eb40 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
1eb50 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20  ct to represent 
1eb60 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a 20 74 68  all values.** th
1eb70 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64  at can be stored
1eb80 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 74   in a database t
1eb90 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75 73 65  able. SQLite use
1eba0 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69 6e 67  s dynamic typing
1ebb0 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 61 6c 75  .** for the valu
1ebc0 65 73 20 69 74 20 73 74 6f 72 65 73 2e 20 56 61  es it stores. Va
1ebd0 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 73  lues stored in s
1ebe0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
1ebf0 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 69  ects.** can be i
1ec00 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 74 69 6e  ntegers, floatin
1ec10 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 2c 20  g point values, 
1ec20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 73 2c 20  strings, BLOBs, 
1ec30 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41  or NULL..**.** A
1ec40 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  n sqlite3_value 
1ec50 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20 65 69  object may be ei
1ec60 74 68 65 72 20 22 70 72 6f 74 65 63 74 65 64 22  ther "protected"
1ec70 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 74 65 64   or "unprotected
1ec80 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e 74 65 72  "..** Some inter
1ec90 66 61 63 65 73 20 72 65 71 75 69 72 65 20 61 20  faces require a 
1eca0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
1ecb0 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 65 72 20  3_value.  Other 
1ecc0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 77 69  interfaces.** wi
1ecd0 6c 6c 20 61 63 63 65 70 74 20 65 69 74 68 65 72  ll accept either
1ece0 20 61 20 70 72 6f 74 65 63 74 65 64 20 6f 72 20   a protected or 
1ecf0 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73  an unprotected s
1ed00 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a  qlite3_value..**
1ed10 20 45 76 65 72 79 20 69 6e 74 65 72 66 61 63 65   Every interface
1ed20 20 74 68 61 74 20 61 63 63 65 70 74 73 20 73 71   that accepts sq
1ed30 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 72 67 75  lite3_value argu
1ed40 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 73 0a  ments specifies.
1ed50 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ** whether or no
1ed60 74 20 69 74 20 72 65 71 75 69 72 65 73 20 61 20  t it requires a 
1ed70 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
1ed80 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  3_value..**.** T
1ed90 68 65 20 74 65 72 6d 73 20 22 70 72 6f 74 65 63  he terms "protec
1eda0 74 65 64 22 20 61 6e 64 20 22 75 6e 70 72 6f 74  ted" and "unprot
1edb0 65 63 74 65 64 22 20 72 65 66 65 72 20 74 6f 20  ected" refer to 
1edc0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a  whether or not.*
1edd0 2a 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c  * a mutex is hel
1ede0 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 6c 20 6d  d.  A internal m
1edf0 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72  utex is held for
1ee00 20 61 20 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20   a protected.** 
1ee10 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
1ee20 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75 74 65  ject but no mute
1ee30 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 6e  x is held for an
1ee40 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20   unprotected.** 
1ee50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
1ee60 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69 74 65  ject.  If SQLite
1ee70 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20   is compiled to 
1ee80 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64  be single-thread
1ee90 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b 53 51 4c  ed.** (with [SQL
1eea0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30  ITE_THREADSAFE=0
1eeb0 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 71 6c 69  ] and with [sqli
1eec0 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28 29  te3_threadsafe()
1eed0 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 29 0a 2a  ] returning 0).*
1eee0 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 65 20 69  * or if SQLite i
1eef0 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 20  s run in one of 
1ef00 72 65 64 75 63 65 64 20 6d 75 74 65 78 20 6d 6f  reduced mutex mo
1ef10 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  des .** [SQLITE_
1ef20 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52  CONFIG_SINGLETHR
1ef30 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  EAD] or [SQLITE_
1ef40 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45  CONFIG_MULTITHRE
1ef50 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72  AD].** then ther
1ef60 65 20 69 73 20 6e 6f 20 64 69 73 74 69 6e 63 74  e is no distinct
1ef70 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74  ion between prot
1ef80 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74  ected and unprot
1ef90 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
1efa0 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 61  _value objects a
1efb0 6e 64 20 74 68 65 79 20 63 61 6e 20 62 65 20 75  nd they can be u
1efc0 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67 65 61  sed interchangea
1efd0 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a  bly.  However,.*
1efe0 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f  * for maximum co
1eff0 64 65 20 70 6f 72 74 61 62 69 6c 69 74 79 20 69  de portability i
1f000 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  t is recommended
1f010 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69 6f   that applicatio
1f020 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b 65  ns.** still make
1f030 20 74 68 65 20 64 69 73 74 69 6e 63 74 69 6f 6e   the distinction
1f040 20 62 65 74 77 65 65 6e 20 62 65 74 77 65 65 6e   between between
1f050 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75   protected and u
1f060 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71  nprotected.** sq
1f070 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
1f080 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20 6e 6f  cts even when no
1f090 74 20 73 74 72 69 63 74 6c 79 20 72 65 71 75 69  t strictly requi
1f0a0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  red..**.** The s
1f0b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
1f0c0 65 63 74 73 20 74 68 61 74 20 61 72 65 20 70 61  ects that are pa
1f0d0 73 73 65 64 20 61 73 20 70 61 72 61 6d 65 74 65  ssed as paramete
1f0e0 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69  rs into the.** i
1f0f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
1f100 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65   [application-de
1f110 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69  fined SQL functi
1f120 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 65 63 74  ons] are protect
1f130 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ed..** The sqlit
1f140 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
1f150 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b  returned by.** [
1f160 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
1f170 61 6c 75 65 28 29 5d 20 69 73 20 75 6e 70 72 6f  alue()] is unpro
1f180 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e 70 72 6f  tected..** Unpro
1f190 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
1f1a0 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 6d 61 79  alue objects may
1f1b0 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69   only be used wi
1f1c0 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72  th.** [sqlite3_r
1f1d0 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 20 61  esult_value()] a
1f1e0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  nd [sqlite3_bind
1f1f0 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 54 68  _value()]..** Th
1f200 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
1f210 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f  _blob | sqlite3_
1f220 76 61 6c 75 65 5f 74 79 70 65 28 29 5d 20 66 61  value_type()] fa
1f230 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e 74 65 72  mily of.** inter
1f240 66 61 63 65 73 20 72 65 71 75 69 72 65 20 70 72  faces require pr
1f250 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
1f260 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a  value objects..*
1f270 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
1f280 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 61 6c   Mem sqlite3_val
1f290 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ue;../*.** CAPI3
1f2a0 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74 69 6f  REF: SQL Functio
1f2b0 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65 63 74  n Context Object
1f2c0 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 30 32 30   {H16001} <S2020
1f2d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e  0>.**.** The con
1f2e0 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 61 6e  text in which an
1f2f0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 65 78   SQL function ex
1f300 65 63 75 74 65 73 20 69 73 20 73 74 6f 72 65 64  ecutes is stored
1f310 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65   in an.** sqlite
1f320 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74  3_context object
1f330 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  .  A pointer to 
1f340 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  an sqlite3_conte
1f350 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 69 73 20  xt object.** is 
1f360 61 6c 77 61 79 73 20 66 69 72 73 74 20 70 61 72  always first par
1f370 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 70 6c 69  ameter to [appli
1f380 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
1f390 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a  QL functions]..*
1f3a0 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * The applicatio
1f3b0 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75  n-defined SQL fu
1f3c0 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
1f3d0 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 73 73 20  ation will pass 
1f3e0 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  this.** pointer 
1f3f0 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 63 61 6c  through into cal
1f400 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  ls to [sqlite3_r
1f410 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 71 6c 69  esult_int | sqli
1f420 74 65 33 5f 72 65 73 75 6c 74 28 29 5d 2c 0a 2a  te3_result()],.*
1f430 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72 65  * [sqlite3_aggre
1f440 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 5d 2c  gate_context()],
1f450 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64   [sqlite3_user_d
1f460 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  ata()],.** [sqli
1f470 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
1f480 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 6c 69 74  andle()], [sqlit
1f490 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29  e3_get_auxdata()
1f4a0 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 73 71  ],.** and/or [sq
1f4b0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
1f4c0 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  a()]..*/.typedef
1f4d0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
1f4e0 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 65 33 5f  context sqlite3_
1f4f0 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20  context;../*.** 
1f500 43 41 50 49 33 52 45 46 3a 20 42 69 6e 64 69 6e  CAPI3REF: Bindin
1f510 67 20 56 61 6c 75 65 73 20 54 6f 20 50 72 65 70  g Values To Prep
1f520 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 73 20  ared Statements 
1f530 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 33 30 30  {H13500} <S70300
1f540 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
1f550 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 7d 20  host parameter} 
1f560 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73  {host parameters
1f570 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65  } {host paramete
1f580 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 59 57 4f  r name}.** KEYWO
1f590 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 61 6d 65  RDS: {SQL parame
1f5a0 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 61 6d 65  ter} {SQL parame
1f5b0 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 74 65 72  ters} {parameter
1f5c0 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a 2a 20   binding}.**.** 
1f5d0 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e  In the SQL strin
1f5e0 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c  gs input to [sql
1f5f0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
1f600 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72 69 61  )] and its varia
1f610 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c 73  nts,.** literals
1f620 20 6d 61 79 20 62 65 20 72 65 70 6c 61 63 65 64   may be replaced
1f630 20 62 79 20 61 20 5b 70 61 72 61 6d 65 74 65 72   by a [parameter
1f640 5d 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 73  ] in one of thes
1f650 65 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 3c  e forms:.**.** <
1f660 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a  ul>.** <li>  ?.*
1f670 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20  * <li>  ?NNN.** 
1f680 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 3c 6c  <li>  :VVV.** <l
1f690 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c 6c 69 3e  i>  @VVV.** <li>
1f6a0 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a    $VVV.** </ul>.
1f6b0 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 70 61 72  **.** In the par
1f6c0 61 6d 65 74 65 72 20 66 6f 72 6d 73 20 73 68 6f  ameter forms sho
1f6d0 77 6e 20 61 62 6f 76 65 20 4e 4e 4e 20 69 73 20  wn above NNN is 
1f6e0 61 6e 20 69 6e 74 65 67 65 72 20 6c 69 74 65 72  an integer liter
1f6f0 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 56 20 69  al,.** and VVV i
1f700 73 20 61 6e 20 61 6c 70 68 61 2d 6e 75 6d 65 72  s an alpha-numer
1f710 69 63 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d  ic parameter nam
1f720 65 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66  e. The values of
1f730 20 74 68 65 73 65 0a 2a 2a 20 70 61 72 61 6d 65   these.** parame
1f740 74 65 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c 65  ters (also calle
1f750 64 20 22 68 6f 73 74 20 70 61 72 61 6d 65 74 65  d "host paramete
1f760 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 53 51 4c  r names" or "SQL
1f770 20 70 61 72 61 6d 65 74 65 72 73 22 29 0a 2a 2a   parameters").**
1f780 20 63 61 6e 20 62 65 20 73 65 74 20 75 73 69 6e   can be set usin
1f790 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69  g the sqlite3_bi
1f7a0 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  nd_*() routines 
1f7b0 64 65 66 69 6e 65 64 20 68 65 72 65 2e 0a 2a 2a  defined here..**
1f7c0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72  .** The first ar
1f7d0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71  gument to the sq
1f7e0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72  lite3_bind_*() r
1f7f0 6f 75 74 69 6e 65 73 20 69 73 20 61 6c 77 61 79  outines is alway
1f800 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  s.** a pointer t
1f810 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  o the [sqlite3_s
1f820 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72 65 74 75  tmt] object retu
1f830 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71  rned from.** [sq
1f840 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1f850 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 61  ()] or its varia
1f860 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nts..**.** The s
1f870 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
1f880 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
1f890 68 65 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  he SQL parameter
1f8a0 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a 2a 20 54   to be set..** T
1f8b0 68 65 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20  he leftmost SQL 
1f8c0 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e  parameter has an
1f8d0 20 69 6e 64 65 78 20 6f 66 20 31 2e 20 20 57 68   index of 1.  Wh
1f8e0 65 6e 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  en the same name
1f8f0 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74  d.** SQL paramet
1f900 65 72 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20  er is used more 
1f910 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65 63 6f 6e  than once, secon
1f920 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
1f930 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63 65 73 20  .** occurrences 
1f940 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 69 6e  have the same in
1f950 64 65 78 20 61 73 20 74 68 65 20 66 69 72 73 74  dex as the first
1f960 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a 2a 2a 20   occurrence..** 
1f970 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20 6e 61  The index for na
1f980 6d 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 63  med parameters c
1f990 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 75 70 20  an be looked up 
1f9a0 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71  using the.** [sq
1f9b0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
1f9c0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 20 41 50  eter_index()] AP
1f9d0 49 20 69 66 20 64 65 73 69 72 65 64 2e 20 20 54  I if desired.  T
1f9e0 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66 6f 72 20  he index.** for 
1f9f0 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65 74 65 72  "?NNN" parameter
1fa00 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f  s is the value o
1fa10 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20 4e 4e  f NNN..** The NN
1fa20 4e 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20  N value must be 
1fa30 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 74 68  between 1 and th
1fa40 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  e [sqlite3_limit
1fa50 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ()].** parameter
1fa60 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56   [SQLITE_LIMIT_V
1fa70 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20  ARIABLE_NUMBER] 
1fa80 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 3a 20  (default value: 
1fa90 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  999)..**.** The 
1faa0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69  third argument i
1fab0 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62  s the value to b
1fac0 69 6e 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d  ind to the param
1fad0 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  eter..**.** In t
1fae0 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68  hose routines th
1faf0 61 74 20 68 61 76 65 20 61 20 66 6f 75 72 74 68  at have a fourth
1fb00 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76   argument, its v
1fb10 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e  alue is the.** n
1fb20 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1fb30 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e  n the parameter.
1fb40 20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74    To be clear: t
1fb50 68 65 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a  he value is the.
1fb60 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e  ** number of <u>
1fb70 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65  bytes</u> in the
1fb80 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20   value, not the 
1fb90 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
1fba0 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ters..** If the 
1fbb0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
1fbc0 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
1fbd0 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
1fbe0 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 74 68 65  string is.** the
1fbf0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1fc00 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74   up to the first
1fc10 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
1fc20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74  ..**.** The fift
1fc30 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  h argument to sq
1fc40 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
1fc50 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ), sqlite3_bind_
1fc60 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73  text(), and.** s
1fc70 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
1fc80 31 36 28 29 20 69 73 20 61 20 64 65 73 74 72 75  16() is a destru
1fc90 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73  ctor used to dis
1fca0 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42  pose of the BLOB
1fcb0 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66   or.** string af
1fcc0 74 65 72 20 53 51 4c 69 74 65 20 68 61 73 20 66  ter SQLite has f
1fcd0 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e  inished with it.
1fce0 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72   If the fifth ar
1fcf0 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 68 65  gument is.** the
1fd00 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 20 5b   special value [
1fd10 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20  SQLITE_STATIC], 
1fd20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 73 73 75  then SQLite assu
1fd30 6d 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  mes that the.** 
1fd40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 69  information is i
1fd50 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d 61 6e 61  n static, unmana
1fd60 67 65 64 20 73 70 61 63 65 20 61 6e 64 20 64 6f  ged space and do
1fd70 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
1fd80 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 74  e freed..** If t
1fd90 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e  he fifth argumen
1fda0 74 20 68 61 73 20 74 68 65 20 76 61 6c 75 65 20  t has the value 
1fdb0 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e  [SQLITE_TRANSIEN
1fdc0 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69  T], then.** SQLi
1fdd0 74 65 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e  te makes its own
1fde0 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66   private copy of
1fdf0 20 74 68 65 20 64 61 74 61 20 69 6d 6d 65 64 69   the data immedi
1fe00 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 0a 2a 2a  ately, before.**
1fe10 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   the sqlite3_bin
1fe20 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 20 72 65  d_*() routine re
1fe30 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  turns..**.** The
1fe40 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65   sqlite3_bind_ze
1fe50 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74 69 6e 65  roblob() routine
1fe60 20 62 69 6e 64 73 20 61 20 42 4c 4f 42 20 6f 66   binds a BLOB of
1fe70 20 6c 65 6e 67 74 68 20 4e 20 74 68 61 74 0a 2a   length N that.*
1fe80 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68  * is filled with
1fe90 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a 65 72 6f   zeroes.  A zero
1fea0 62 6c 6f 62 20 75 73 65 73 20 61 20 66 69 78 65  blob uses a fixe
1feb0 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  d amount of memo
1fec0 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61 6e 20 69  ry.** (just an i
1fed0 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c 64 20 69  nteger to hold i
1fee0 74 73 20 73 69 7a 65 29 20 77 68 69 6c 65 20 69  ts size) while i
1fef0 74 20 69 73 20 62 65 69 6e 67 20 70 72 6f 63 65  t is being proce
1ff00 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62 6c 6f  ssed..** Zeroblo
1ff10 62 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  bs are intended 
1ff20 74 6f 20 73 65 72 76 65 20 61 73 20 70 6c 61 63  to serve as plac
1ff30 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 42 4c 4f  eholders for BLO
1ff40 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63 6f 6e 74  Bs whose.** cont
1ff50 65 6e 74 20 69 73 20 6c 61 74 65 72 20 77 72 69  ent is later wri
1ff60 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73  tten using.** [s
1ff70 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
1ff80 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42   | incremental B
1ff90 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74 69 6e 65  LOB I/O] routine
1ffa0 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65  s..** A negative
1ffb0 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 7a   value for the z
1ffc0 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c 74 73 20  eroblob results 
1ffd0 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68  in a zero-length
1ffe0 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   BLOB..**.** The
1fff0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28   sqlite3_bind_*(
20000 29 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20  ) routines must 
20010 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 0a  be called after.
20020 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
20030 61 72 65 5f 76 32 28 29 5d 20 28 61 6e 64 20 69  are_v2()] (and i
20040 74 73 20 76 61 72 69 61 6e 74 73 29 20 6f 72 20  ts variants) or 
20050 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
20060 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f 72 65 20  ] and.** before 
20070 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
20080 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73 20 61 72  ..** Bindings ar
20090 65 20 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79  e not cleared by
200a0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65   the [sqlite3_re
200b0 73 65 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a  set()] routine..
200c0 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d  ** Unbound param
200d0 65 74 65 72 73 20 61 72 65 20 69 6e 74 65 72 70  eters are interp
200e0 72 65 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a  reted as NULL..*
200f0 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
20100 6e 65 73 20 72 65 74 75 72 6e 20 5b 53 51 4c 49  nes return [SQLI
20110 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73  TE_OK] on succes
20120 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  s or an error co
20130 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e  de if.** anythin
20140 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 5b  g goes wrong.  [
20150 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20 69 73  SQLITE_RANGE] is
20160 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
20170 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e   parameter.** in
20180 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61  dex is out of ra
20190 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45 5f 4e 4f  nge.  [SQLITE_NO
201a0 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 6e 65 64  MEM] is returned
201b0 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69   if malloc() fai
201c0 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d  ls..** [SQLITE_M
201d0 49 53 55 53 45 5d 20 6d 69 67 68 74 20 62 65 20  ISUSE] might be 
201e0 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 73  returned if thes
201f0 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63  e routines are c
20200 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20 76 69  alled on a.** vi
20210 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68  rtual machine th
20220 61 74 20 69 73 20 74 68 65 20 77 72 6f 6e 67 20  at is the wrong 
20230 73 74 61 74 65 20 6f 72 20 77 68 69 63 68 20 68  state or which h
20240 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
20250 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 44 65  finalized..** De
20260 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69 73 75 73  tection of misus
20270 65 20 69 73 20 75 6e 72 65 6c 69 61 62 6c 65 2e  e is unreliable.
20280 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73    Applications s
20290 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70 65 6e 64  hould not depend
202a0 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45 5f 4d 49  .** on SQLITE_MI
202b0 53 55 53 45 20 72 65 74 75 72 6e 73 2e 20 20 53  SUSE returns.  S
202c0 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 73 20  QLITE_MISUSE is 
202d0 69 6e 74 65 6e 64 65 64 20 74 6f 20 69 6e 64 69  intended to indi
202e0 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c 6f 67 69  cate a.** a logi
202f0 63 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20 61  c error in the a
20300 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 46 75 74  pplication.  Fut
20310 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ure versions of 
20320 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20  SQLite might.** 
20330 70 61 6e 69 63 20 72 61 74 68 65 72 20 74 68 61  panic rather tha
20340 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  n return SQLITE_
20350 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20 53 65  MISUSE..**.** Se
20360 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
20370 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
20380 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71  count()],.** [sq
20390 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
203a0 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e  eter_name()], an
203b0 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  d [sqlite3_bind_
203c0 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28  parameter_index(
203d0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  )]..**.** Requir
203e0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 35  ements:.** [H135
203f0 30 36 5d 20 5b 48 31 33 35 30 39 5d 20 5b 48 31  06] [H13509] [H1
20400 33 35 31 32 5d 20 5b 48 31 33 35 31 35 5d 20 5b  3512] [H13515] [
20410 48 31 33 35 31 38 5d 20 5b 48 31 33 35 32 31 5d  H13518] [H13521]
20420 20 5b 48 31 33 35 32 34 5d 20 5b 48 31 33 35 32   [H13524] [H1352
20430 37 5d 0a 2a 2a 20 5b 48 31 33 35 33 30 5d 20 5b  7].** [H13530] [
20440 48 31 33 35 33 33 5d 20 5b 48 31 33 35 33 36 5d  H13533] [H13536]
20450 20 5b 48 31 33 35 33 39 5d 20 5b 48 31 33 35 34   [H13539] [H1354
20460 32 5d 20 5b 48 31 33 35 34 35 5d 20 5b 48 31 33  2] [H13545] [H13
20470 35 34 38 5d 20 5b 48 31 33 35 35 31 5d 0a 2a 2a  548] [H13551].**
20480 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
20490 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
204a0 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d  blob(sqlite3_stm
204b0 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76  t*, int, const v
204c0 6f 69 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69  oid*, int n, voi
204d0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
204e0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
204f0 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65  ite3_bind_double
20500 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
20510 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51  int, double);.SQ
20520 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
20530 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71  ite3_bind_int(sq
20540 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
20550 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  , int);.SQLITE_A
20560 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
20570 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65  ind_int64(sqlite
20580 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 73 71  3_stmt*, int, sq
20590 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51  lite3_int64);.SQ
205a0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
205b0 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73  ite3_bind_null(s
205c0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
205d0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  t);.SQLITE_API i
205e0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
205f0 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  text(sqlite3_stm
20600 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  t*, int, const c
20610 68 61 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69  har*, int n, voi
20620 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
20630 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
20640 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
20650 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
20660 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  int, const void*
20670 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
20680 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
20690 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
206a0 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65  ind_value(sqlite
206b0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
206c0 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  nst sqlite3_valu
206d0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
206e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
206f0 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65  _zeroblob(sqlite
20700 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e  3_stmt*, int, in
20710 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  t n);../*.** CAP
20720 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66  I3REF: Number Of
20730 20 53 51 4c 20 50 61 72 61 6d 65 74 65 72 73 20   SQL Parameters 
20740 7b 48 31 33 36 30 30 7d 20 3c 53 37 30 33 30 30  {H13600} <S70300
20750 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  >.**.** This rou
20760 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64  tine can be used
20770 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d   to find the num
20780 62 65 72 20 6f 66 20 5b 53 51 4c 20 70 61 72 61  ber of [SQL para
20790 6d 65 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61 20  meters].** in a 
207a0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
207b0 65 6e 74 5d 2e 20 20 53 51 4c 20 70 61 72 61 6d  ent].  SQL param
207c0 65 74 65 72 73 20 61 72 65 20 74 6f 6b 65 6e 73  eters are tokens
207d0 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20   of the.** form 
207e0 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a 41  "?", "?NNN", ":A
207f0 41 41 22 2c 20 22 24 41 41 41 22 2c 20 6f 72 20  AA", "$AAA", or 
20800 22 40 41 41 41 22 20 74 68 61 74 20 73 65 72 76  "@AAA" that serv
20810 65 20 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f 6c  e as.** placehol
20820 64 65 72 73 20 66 6f 72 20 76 61 6c 75 65 73 20  ders for values 
20830 74 68 61 74 20 61 72 65 20 5b 73 71 6c 69 74 65  that are [sqlite
20840 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 6f  3_bind_blob | bo
20850 75 6e 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 70  und].** to the p
20860 61 72 61 6d 65 74 65 72 73 20 61 74 20 61 20 6c  arameters at a l
20870 61 74 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  ater time..**.**
20880 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63   This routine ac
20890 74 75 61 6c 6c 79 20 72 65 74 75 72 6e 73 20 74  tually returns t
208a0 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
208b0 6c 61 72 67 65 73 74 20 28 72 69 67 68 74 6d 6f  largest (rightmo
208c0 73 74 29 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  st).** parameter
208d0 2e 20 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20  . For all forms 
208e0 65 78 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69  except ?NNN, thi
208f0 73 20 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e  s will correspon
20900 64 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  d to the.** numb
20910 65 72 20 6f 66 20 75 6e 69 71 75 65 20 70 61 72  er of unique par
20920 61 6d 65 74 65 72 73 2e 20 20 49 66 20 70 61 72  ameters.  If par
20930 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f  ameters of the ?
20940 4e 4e 4e 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a  NNN are used,.**
20950 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 67 61   there may be ga
20960 70 73 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a  ps in the list..
20970 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
20980 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
20990 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28  ob|sqlite3_bind(
209a0 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  )],.** [sqlite3_
209b0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e  bind_parameter_n
209c0 61 6d 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b  ame()], and.** [
209d0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
209e0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e  ameter_index()].
209f0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
20a00 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 30 31 5d  nts:.** [H13601]
20a10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
20a20 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
20a30 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28  parameter_count(
20a40 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
20a50 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
20a60 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f 73 74 20   Name Of A Host 
20a70 50 61 72 61 6d 65 74 65 72 20 7b 48 31 33 36 32  Parameter {H1362
20a80 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a  0} <S70300>.**.*
20a90 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
20aa0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
20ab0 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   to the name of 
20ac0 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53 51 4c  the n-th.** [SQL
20ad0 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 61   parameter] in a
20ae0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
20af0 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20 70 61  ment]..** SQL pa
20b00 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20  rameters of the 
20b10 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22  form "?NNN" or "
20b20 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 20  :AAA" or "@AAA" 
20b30 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 68 61 76  or "$AAA".** hav
20b40 65 20 61 20 6e 61 6d 65 20 77 68 69 63 68 20 69  e a name which i
20b50 73 20 74 68 65 20 73 74 72 69 6e 67 20 22 3f 4e  s the string "?N
20b60 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72  NN" or ":AAA" or
20b70 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41   "@AAA" or "$AAA
20b80 22 0a 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c  ".** respectivel
20b90 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  y..** In other w
20ba0 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74 69 61  ords, the initia
20bb0 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f 72 20  l ":" or "$" or 
20bc0 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a 20 69 73  "@" or "?".** is
20bd0 20 69 6e 63 6c 75 64 65 64 20 61 73 20 70 61 72   included as par
20be0 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 2e 0a 2a  t of the name..*
20bf0 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20  * Parameters of 
20c00 74 68 65 20 66 6f 72 6d 20 22 3f 22 20 77 69 74  the form "?" wit
20c10 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77 69 6e 67  hout a following
20c20 20 69 6e 74 65 67 65 72 20 68 61 76 65 20 6e 6f   integer have no
20c30 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 61 72 65   name.** and are
20c40 20 61 6c 73 6f 20 72 65 66 65 72 72 65 64 20 74   also referred t
20c50 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f 75 73 20  o as "anonymous 
20c60 70 61 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a  parameters"..**.
20c70 2a 2a 20 54 68 65 20 66 69 72 73 74 20 68 6f 73  ** The first hos
20c80 74 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 20  t parameter has 
20c90 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2c 20 6e  an index of 1, n
20ca0 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ot 0..**.** If t
20cb0 68 65 20 76 61 6c 75 65 20 6e 20 69 73 20 6f 75  he value n is ou
20cc0 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66  t of range or if
20cd0 20 74 68 65 20 6e 2d 74 68 20 70 61 72 61 6d 65   the n-th parame
20ce0 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 6c 65  ter is.** namele
20cf0 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73  ss, then NULL is
20d00 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20   returned.  The 
20d10 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
20d20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 69 6e 20  is.** always in 
20d30 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 65  UTF-8 encoding e
20d40 76 65 6e 20 69 66 20 74 68 65 20 6e 61 6d 65 64  ven if the named
20d50 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 0a 2a   parameter was.*
20d60 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 73 70 65  * originally spe
20d70 63 69 66 69 65 64 20 61 73 20 55 54 46 2d 31 36  cified as UTF-16
20d80 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   in [sqlite3_pre
20d90 70 61 72 65 31 36 28 29 5d 20 6f 72 0a 2a 2a 20  pare16()] or.** 
20da0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
20db0 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  16_v2()]..**.** 
20dc0 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
20dd0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c  e3_bind_blob|sql
20de0 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a  ite3_bind()],.**
20df0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
20e00 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29  arameter_count()
20e10 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74  ], and.** [sqlit
20e20 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
20e30 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a  r_index()]..**.*
20e40 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
20e50 2a 2a 20 5b 48 31 33 36 32 31 5d 0a 2a 2f 0a 53  ** [H13621].*/.S
20e60 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
20e70 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69  char *sqlite3_bi
20e80 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
20e90 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
20ea0 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   int);../*.** CA
20eb0 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66  PI3REF: Index Of
20ec0 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69 74   A Parameter Wit
20ed0 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b  h A Given Name {
20ee0 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e  H13640} <S70300>
20ef0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
20f00 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51  e index of an SQ
20f10 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76 65  L parameter give
20f20 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65  n its name.  The
20f30 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20  .** index value 
20f40 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69 74  returned is suit
20f50 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20  able for use as 
20f60 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61  the second.** pa
20f70 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
20f80 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71  te3_bind_blob|sq
20f90 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20  lite3_bind()].  
20fa0 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74  A zero.** is ret
20fb0 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63  urned if no matc
20fc0 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69  hing parameter i
20fd0 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61  s found.  The pa
20fe0 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20  rameter.** name 
20ff0 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e  must be given in
21000 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74   UTF-8 even if t
21010 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
21020 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65  ement.** was pre
21030 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31  pared from UTF-1
21040 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71  6 text using [sq
21050 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
21060 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  v2()]..**.** See
21070 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
21080 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65  bind_blob|sqlite
21090 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73  3_bind()],.** [s
210a0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
210b0 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20  meter_count()], 
210c0 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  and.** [sqlite3_
210d0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
210e0 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52  ndex()]..**.** R
210f0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
21100 5b 48 31 33 36 34 31 5d 0a 2a 2f 0a 53 51 4c 49  [H13641].*/.SQLI
21110 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
21120 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
21130 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f  r_index(sqlite3_
21140 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61  stmt*, const cha
21150 72 20 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a  r *zName);../*.*
21160 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 65  * CAPI3REF: Rese
21170 74 20 41 6c 6c 20 42 69 6e 64 69 6e 67 73 20 4f  t All Bindings O
21180 6e 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61  n A Prepared Sta
21190 74 65 6d 65 6e 74 20 7b 48 31 33 36 36 30 7d 20  tement {H13660} 
211a0 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 43  <S70300>.**.** C
211b0 6f 6e 74 72 61 72 79 20 74 6f 20 74 68 65 20 69  ontrary to the i
211c0 6e 74 75 69 74 69 6f 6e 20 6f 66 20 6d 61 6e 79  ntuition of many
211d0 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  , [sqlite3_reset
211e0 28 29 5d 20 64 6f 65 73 20 6e 6f 74 20 72 65 73  ()] does not res
211f0 65 74 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74  et.** the [sqlit
21200 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62  e3_bind_blob | b
21210 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 61 20 5b 70  indings] on a [p
21220 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
21230 74 5d 2e 0a 2a 2a 20 55 73 65 20 74 68 69 73 20  t]..** Use this 
21240 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 73 65 74  routine to reset
21250 20 61 6c 6c 20 68 6f 73 74 20 70 61 72 61 6d 65   all host parame
21260 74 65 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a  ters to NULL..**
21270 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
21280 3a 0a 2a 2a 20 5b 48 31 33 36 36 31 5d 0a 2a 2f  :.** [H13661].*/
21290 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
212a0 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69  sqlite3_clear_bi
212b0 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73  ndings(sqlite3_s
212c0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tmt*);../*.** CA
212d0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f  PI3REF: Number O
212e0 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52  f Columns In A R
212f0 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31  esult Set {H1371
21300 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a  0} <S10700>.**.*
21310 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
21320 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
21330 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
21340 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
21350 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
21360 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72  atement]. This r
21370 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30  outine returns 0
21380 20 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20   if pStmt is an 
21390 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
213a0 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72   that does not r
213b0 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20  eturn data (for 
213c0 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41  example an [UPDA
213d0 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  TE])..**.** Requ
213e0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
213f0 33 37 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3711].*/.SQLITE_
21400 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
21410 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c  column_count(sql
21420 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
21430 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
21440 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73  EF: Column Names
21450 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74   In A Result Set
21460 20 7b 48 31 33 37 32 30 7d 20 3c 53 31 30 37 30   {H13720} <S1070
21470 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
21480 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74  outines return t
21490 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64  he name assigned
214a0 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72   to a particular
214b0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 74 68   column.** in th
214c0 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
214d0 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65  a [SELECT] state
214e0 6d 65 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74  ment.  The sqlit
214f0 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29  e3_column_name()
21500 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 65  .** interface re
21510 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
21520 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  to a zero-termin
21530 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e  ated UTF-8 strin
21540 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  g.** and sqlite3
21550 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29  _column_name16()
21560 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
21570 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72  er to a zero-ter
21580 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31  minated.** UTF-1
21590 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 66  6 string.  The f
215a0 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69  irst parameter i
215b0 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20  s the [prepared 
215c0 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68  statement].** th
215d0 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
215e0 65 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65  e [SELECT] state
215f0 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64  ment. The second
21600 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
21610 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62  e.** column numb
21620 65 72 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73  er.  The leftmos
21630 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62  t column is numb
21640 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 0..**.** The 
21650 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
21660 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64  pointer is valid
21670 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68   until either th
21680 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
21690 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73  ement].** is des
216a0 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74  troyed by [sqlit
216b0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f  e3_finalize()] o
216c0 72 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74  r until the next
216d0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69   call to.** sqli
216e0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28  te3_column_name(
216f0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ) or sqlite3_col
21700 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 6f 6e 20  umn_name16() on 
21710 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 2e  the same column.
21720 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65  .**.** If sqlite
21730 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  3_malloc() fails
21740 20 64 75 72 69 6e 67 20 74 68 65 20 70 72 6f 63   during the proc
21750 65 73 73 69 6e 67 20 6f 66 20 65 69 74 68 65 72  essing of either
21760 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 6f 72   routine.** (for
21770 20 65 78 61 6d 70 6c 65 20 64 75 72 69 6e 67 20   example during 
21780 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f  a conversion fro
21790 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31  m UTF-8 to UTF-1
217a0 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 4e 55 4c  6) then a.** NUL
217b0 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74  L pointer is ret
217c0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
217d0 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 73 75 6c   name of a resul
217e0 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20  t column is the 
217f0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 41 53  value of the "AS
21800 22 20 63 6c 61 75 73 65 20 66 6f 72 0a 2a 2a 20  " clause for.** 
21810 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 66 20  that column, if 
21820 74 68 65 72 65 20 69 73 20 61 6e 20 41 53 20 63  there is an AS c
21830 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 72 65  lause.  If there
21840 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 75 73 65   is no AS clause
21850 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e 61 6d  .** then the nam
21860 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e of the column 
21870 69 73 20 75 6e 73 70 65 63 69 66 69 65 64 20 61  is unspecified a
21880 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72  nd may change fr
21890 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65 61 73  om.** one releas
218a0 65 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 74  e of SQLite to t
218b0 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 52  he next..**.** R
218c0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
218d0 5b 48 31 33 37 32 31 5d 20 5b 48 31 33 37 32 33  [H13721] [H13723
218e0 5d 20 5b 48 31 33 37 32 34 5d 20 5b 48 31 33 37  ] [H13724] [H137
218f0 32 35 5d 20 5b 48 31 33 37 32 36 5d 20 5b 48 31  25] [H13726] [H1
21900 33 37 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3727].*/.SQLITE_
21910 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
21920 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
21930 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
21940 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54  *, int N);.SQLIT
21950 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
21960 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
21970 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
21980 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 0a  stmt*, int N);..
21990 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
219a0 53 6f 75 72 63 65 20 4f 66 20 44 61 74 61 20 49  Source Of Data I
219b0 6e 20 41 20 51 75 65 72 79 20 52 65 73 75 6c 74  n A Query Result
219c0 20 7b 48 31 33 37 34 30 7d 20 3c 53 31 30 37 30   {H13740} <S1070
219d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
219e0 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20  outines provide 
219f0 61 20 6d 65 61 6e 73 20 74 6f 20 64 65 74 65 72  a means to deter
21a00 6d 69 6e 65 20 77 68 61 74 20 63 6f 6c 75 6d 6e  mine what column
21a10 20 6f 66 20 77 68 61 74 0a 2a 2a 20 74 61 62 6c   of what.** tabl
21a20 65 20 69 6e 20 77 68 69 63 68 20 64 61 74 61 62  e in which datab
21a30 61 73 65 20 61 20 72 65 73 75 6c 74 20 6f 66 20  ase a result of 
21a40 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65  a [SELECT] state
21a50 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 2e  ment comes from.
21a60 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
21a70 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
21a80 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20  table or column 
21a90 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20  can be returned 
21aa0 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 55  as.** either a U
21ab0 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 73  TF-8 or UTF-16 s
21ac0 74 72 69 6e 67 2e 20 20 54 68 65 20 5f 64 61 74  tring.  The _dat
21ad0 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65 73 20  abase_ routines 
21ae0 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64 61  return.** the da
21af0 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74 68 65  tabase name, the
21b00 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e 65   _table_ routine
21b10 73 20 72 65 74 75 72 6e 20 74 68 65 20 74 61 62  s return the tab
21b20 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a 20  le name, and.** 
21b30 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75 74  the origin_ rout
21b40 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20  ines return the 
21b50 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a 20  column name..** 
21b60 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  The returned str
21b70 69 6e 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74  ing is valid unt
21b80 69 6c 20 74 68 65 20 5b 70 72 65 70 61 72 65 64  il the [prepared
21b90 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64   statement] is d
21ba0 65 73 74 72 6f 79 65 64 0a 2a 2a 20 75 73 69 6e  estroyed.** usin
21bb0 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  g [sqlite3_final
21bc0 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20  ize()] or until 
21bd0 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61  the same informa
21be0 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65  tion is requeste
21bf0 64 0a 2a 2a 20 61 67 61 69 6e 20 69 6e 20 61 20  d.** again in a 
21c00 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69  different encodi
21c10 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61  ng..**.** The na
21c20 6d 65 73 20 72 65 74 75 72 6e 65 64 20 61 72 65  mes returned are
21c30 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 75 6e   the original un
21c40 2d 61 6c 69 61 73 65 64 20 6e 61 6d 65 73 20 6f  -aliased names o
21c50 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
21c60 65 2c 20 74 61 62 6c 65 2c 20 61 6e 64 20 63 6f  e, table, and co
21c70 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lumn..**.** The 
21c80 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
21c90 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
21ca0 63 61 6c 6c 73 20 69 73 20 61 20 5b 70 72 65 70  calls is a [prep
21cb0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
21cc0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69  .** These functi
21cd0 6f 6e 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72  ons return infor
21ce0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
21cf0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75   Nth column retu
21d00 72 6e 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73  rned by.** the s
21d10 74 61 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20  tatement, where 
21d20 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  N is the second 
21d30 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
21d40 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
21d50 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72  Nth column retur
21d60 6e 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65  ned by the state
21d70 6d 65 6e 74 20 69 73 20 61 6e 20 65 78 70 72 65  ment is an expre
21d80 73 73 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75 62 71  ssion or.** subq
21d90 75 65 72 79 20 61 6e 64 20 69 73 20 6e 6f 74 20  uery and is not 
21da0 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20  a column value, 
21db0 74 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 73  then all of thes
21dc0 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  e functions retu
21dd0 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54 68 65  rn.** NULL.  The
21de0 73 65 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  se routine might
21df0 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 4e 55 4c   also return NUL
21e00 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  L if a memory al
21e10 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 0a 2a  location error.*
21e20 2a 20 6f 63 63 75 72 73 2e 20 20 4f 74 68 65 72  * occurs.  Other
21e30 77 69 73 65 2c 20 74 68 65 79 20 72 65 74 75 72  wise, they retur
21e40 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
21e50 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  e attached datab
21e60 61 73 65 2c 20 74 61 62 6c 65 0a 2a 2a 20 61 6e  ase, table.** an
21e70 64 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 71 75  d column that qu
21e80 65 72 79 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  ery result colum
21e90 6e 20 77 61 73 20 65 78 74 72 61 63 74 65 64 20  n was extracted 
21ea0 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77  from..**.** As w
21eb0 69 74 68 20 61 6c 6c 20 6f 74 68 65 72 20 53 51  ith all other SQ
21ec0 4c 69 74 65 20 41 50 49 73 2c 20 74 68 6f 73 65  Lite APIs, those
21ed0 20 70 6f 73 74 66 69 78 65 64 20 77 69 74 68 20   postfixed with 
21ee0 22 31 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20 55  "16" return.** U
21ef0 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 73 74  TF-16 encoded st
21f00 72 69 6e 67 73 2c 20 74 68 65 20 6f 74 68 65 72  rings, the other
21f10 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
21f20 6e 20 55 54 46 2d 38 2e 20 7b 45 4e 44 7d 0a 2a  n UTF-8. {END}.*
21f30 2a 0a 2a 2a 20 54 68 65 73 65 20 41 50 49 73 20  *.** These APIs 
21f40 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62  are only availab
21f50 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72  le if the librar
21f60 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77  y was compiled w
21f70 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  ith the.** [SQLI
21f80 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
21f90 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65  _METADATA] C-pre
21fa0 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c  processor symbol
21fb0 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
21fc0 7b 41 31 33 37 35 31 7d 0a 2a 2a 20 49 66 20 74  {A13751}.** If t
21fd0 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61  wo or more threa
21fe0 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d  ds call one or m
21ff0 6f 72 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75  ore of these rou
22000 74 69 6e 65 73 20 61 67 61 69 6e 73 74 20 74 68  tines against th
22010 65 20 73 61 6d 65 0a 2a 2a 20 70 72 65 70 61 72  e same.** prepar
22020 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  ed statement and
22030 20 63 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 73   column at the s
22040 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68  ame time then th
22050 65 20 72 65 73 75 6c 74 73 20 61 72 65 0a 2a 2a  e results are.**
22060 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
22070 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
22080 2a 2a 20 5b 48 31 33 37 34 31 5d 20 5b 48 31 33  ** [H13741] [H13
22090 37 34 32 5d 20 5b 48 31 33 37 34 33 5d 20 5b 48  742] [H13743] [H
220a0 31 33 37 34 34 5d 20 5b 48 31 33 37 34 35 5d 20  13744] [H13745] 
220b0 5b 48 31 33 37 34 36 5d 20 5b 48 31 33 37 34 38  [H13746] [H13748
220c0 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f  ].**.** If two o
220d0 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63  r more threads c
220e0 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a  all one or more.
220f0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ** [sqlite3_colu
22100 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
22110 20 7c 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61   | column metada
22120 74 61 20 69 6e 74 65 72 66 61 63 65 73 5d 0a 2a  ta interfaces].*
22130 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b  * for the same [
22140 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
22150 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63  nt] and result c
22160 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 20 74 68 65 20  olumn.** at the 
22170 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74  same time then t
22180 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75  he results are u
22190 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  ndefined..*/.SQL
221a0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
221b0 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
221c0 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
221d0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
221e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
221f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
22200 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
22210 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74  ase_name16(sqlit
22220 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53  e3_stmt*,int);.S
22230 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
22240 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
22250 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28  lumn_table_name(
22260 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
22270 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
22280 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
22290 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
222a0 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73  name16(sqlite3_s
222b0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  tmt*,int);.SQLIT
222c0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
222d0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
222e0 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c  _origin_name(sql
222f0 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
22300 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
22310 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
22320 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
22330 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
22340 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t*,int);../*.** 
22350 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72  CAPI3REF: Declar
22360 65 64 20 44 61 74 61 74 79 70 65 20 4f 66 20 41  ed Datatype Of A
22370 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 7b 48   Query Result {H
22380 31 33 37 36 30 7d 20 3c 53 31 30 37 30 30 3e 0a  13760} <S10700>.
22390 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
223a0 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 5b  parameter is a [
223b0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
223c0 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  nt]..** If this 
223d0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b  statement is a [
223e0 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e  SELECT] statemen
223f0 74 20 61 6e 64 20 74 68 65 20 4e 74 68 20 63 6f  t and the Nth co
22400 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72  lumn of the.** r
22410 65 74 75 72 6e 65 64 20 72 65 73 75 6c 74 20 73  eturned result s
22420 65 74 20 6f 66 20 74 68 61 74 20 5b 53 45 4c 45  et of that [SELE
22430 43 54 5d 20 69 73 20 61 20 74 61 62 6c 65 20 63  CT] is a table c
22440 6f 6c 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a 2a 2a  olumn (not an.**
22450 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73   expression or s
22460 75 62 71 75 65 72 79 29 20 74 68 65 6e 20 74 68  ubquery) then th
22470 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 20  e declared type 
22480 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  of the table.** 
22490 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e  column is return
224a0 65 64 2e 20 20 49 66 20 74 68 65 20 4e 74 68 20  ed.  If the Nth 
224b0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65  column of the re
224c0 73 75 6c 74 20 73 65 74 20 69 73 20 61 6e 0a 2a  sult set is an.*
224d0 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20  * expression or 
224e0 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e 20 61  subquery, then a
224f0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73   NULL pointer is
22500 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68   returned..** Th
22510 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
22520 67 20 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d  g is always UTF-
22530 38 20 65 6e 63 6f 64 65 64 2e 20 7b 45 4e 44 7d  8 encoded. {END}
22540 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
22550 6c 65 2c 20 67 69 76 65 6e 20 74 68 65 20 64 61  le, given the da
22560 74 61 62 61 73 65 20 73 63 68 65 6d 61 3a 0a 2a  tabase schema:.*
22570 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  *.** CREATE TABL
22580 45 20 74 31 28 63 31 20 56 41 52 49 41 4e 54 29  E t1(c1 VARIANT)
22590 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ;.**.** and the 
225a0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d  following statem
225b0 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c  ent to be compil
225c0 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 43 54  ed:.**.** SELECT
225d0 20 63 31 20 2b 20 31 2c 20 63 31 20 46 52 4f 4d   c1 + 1, c1 FROM
225e0 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20   t1;.**.** this 
225f0 72 6f 75 74 69 6e 65 20 77 6f 75 6c 64 20 72 65  routine would re
22600 74 75 72 6e 20 74 68 65 20 73 74 72 69 6e 67 20  turn the string 
22610 22 56 41 52 49 41 4e 54 22 20 66 6f 72 20 74 68  "VARIANT" for th
22620 65 20 73 65 63 6f 6e 64 20 72 65 73 75 6c 74 0a  e second result.
22630 2a 2a 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 31 29  ** column (i==1)
22640 2c 20 61 6e 64 20 61 20 4e 55 4c 4c 20 70 6f 69  , and a NULL poi
22650 6e 74 65 72 20 66 6f 72 20 74 68 65 20 66 69 72  nter for the fir
22660 73 74 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  st result column
22670 20 28 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 53   (i==0)..**.** S
22680 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d  QLite uses dynam
22690 69 63 20 72 75 6e 2d 74 69 6d 65 20 74 79 70 69  ic run-time typi
226a0 6e 67 2e 20 20 53 6f 20 6a 75 73 74 20 62 65 63  ng.  So just bec
226b0 61 75 73 65 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a  ause a column.**
226c0 20 69 73 20 64 65 63 6c 61 72 65 64 20 74 6f 20   is declared to 
226d0 63 6f 6e 74 61 69 6e 20 61 20 70 61 72 74 69 63  contain a partic
226e0 75 6c 61 72 20 74 79 70 65 20 64 6f 65 73 20 6e  ular type does n
226f0 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65  ot mean that the
22700 0a 2a 2a 20 64 61 74 61 20 73 74 6f 72 65 64 20  .** data stored 
22710 69 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69  in that column i
22720 73 20 6f 66 20 74 68 65 20 64 65 63 6c 61 72 65  s of the declare
22730 64 20 74 79 70 65 2e 20 20 53 51 4c 69 74 65 20  d type.  SQLite 
22740 69 73 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79 20 74  is.** strongly t
22750 79 70 65 64 2c 20 62 75 74 20 74 68 65 20 74 79  yped, but the ty
22760 70 69 6e 67 20 69 73 20 64 79 6e 61 6d 69 63 20  ping is dynamic 
22770 6e 6f 74 20 73 74 61 74 69 63 2e 20 20 54 79 70  not static.  Typ
22780 65 0a 2a 2a 20 69 73 20 61 73 73 6f 63 69 61 74  e.** is associat
22790 65 64 20 77 69 74 68 20 69 6e 64 69 76 69 64 75  ed with individu
227a0 61 6c 20 76 61 6c 75 65 73 2c 20 6e 6f 74 20 77  al values, not w
227b0 69 74 68 20 74 68 65 20 63 6f 6e 74 61 69 6e 65  ith the containe
227c0 72 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 68 6f  rs.** used to ho
227d0 6c 64 20 74 68 6f 73 65 20 76 61 6c 75 65 73 2e  ld those values.
227e0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
227f0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 36 31 5d  nts:.** [H13761]
22800 20 5b 48 31 33 37 36 32 5d 20 5b 48 31 33 37 36   [H13762] [H1376
22810 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  3].*/.SQLITE_API
22820 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
22830 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
22840 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  type(sqlite3_stm
22850 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
22860 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
22870 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
22880 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65  ecltype16(sqlite
22890 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f  3_stmt*,int);../
228a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
228b0 76 61 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53  valuate An SQL S
228c0 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 32 30 30  tatement {H13200
228d0 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10000>.**.**
228e0 20 41 66 74 65 72 20 61 20 5b 70 72 65 70 61 72   After a [prepar
228f0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61  ed statement] ha
22900 73 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20  s been prepared 
22910 75 73 69 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20  using either.** 
22920 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
22930 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  _v2()] or [sqlit
22940 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
22950 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  )] or one of the
22960 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72   legacy.** inter
22970 66 61 63 65 73 20 5b 73 71 6c 69 74 65 33 5f 70  faces [sqlite3_p
22980 72 65 70 61 72 65 28 29 5d 20 6f 72 20 5b 73 71  repare()] or [sq
22990 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28  lite3_prepare16(
229a0 29 5d 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  )], this functio
229b0 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 63 61 6c  n.** must be cal
229c0 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  led one or more 
229d0 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75 61 74  times to evaluat
229e0 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e  e the statement.
229f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69  .**.** The detai
22a00 6c 73 20 6f 66 20 74 68 65 20 62 65 68 61 76 69  ls of the behavi
22a10 6f 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  or of the sqlite
22a20 33 5f 73 74 65 70 28 29 20 69 6e 74 65 72 66 61  3_step() interfa
22a30 63 65 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20  ce depend.** on 
22a40 77 68 65 74 68 65 72 20 74 68 65 20 73 74 61 74  whether the stat
22a50 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72  ement was prepar
22a60 65 64 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77  ed using the new
22a70 65 72 20 22 76 32 22 20 69 6e 74 65 72 66 61 63  er "v2" interfac
22a80 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  e.** [sqlite3_pr
22a90 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
22aa0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
22ab0 31 36 5f 76 32 28 29 5d 20 6f 72 20 74 68 65 20  16_v2()] or the 
22ac0 6f 6c 64 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20  older legacy.** 
22ad0 69 6e 74 65 72 66 61 63 65 20 5b 73 71 6c 69 74  interface [sqlit
22ae0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e  e3_prepare()] an
22af0 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  d [sqlite3_prepa
22b00 72 65 31 36 28 29 5d 2e 20 20 54 68 65 20 75 73  re16()].  The us
22b10 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20  e of the.** new 
22b20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 20 69  "v2" interface i
22b30 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f  s recommended fo
22b40 72 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f  r new applicatio
22b50 6e 73 20 62 75 74 20 74 68 65 20 6c 65 67 61 63  ns but the legac
22b60 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 77  y.** interface w
22b70 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20  ill continue to 
22b80 62 65 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a  be supported..**
22b90 0a 2a 2a 20 49 6e 20 74 68 65 20 6c 65 67 61 63  .** In the legac
22ba0 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  y interface, the
22bb0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69   return value wi
22bc0 6c 6c 20 62 65 20 65 69 74 68 65 72 20 5b 53 51  ll be either [SQ
22bd0 4c 49 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b  LITE_BUSY],.** [
22be0 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53  SQLITE_DONE], [S
22bf0 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c  QLITE_ROW], [SQL
22c00 49 54 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b  ITE_ERROR], or [
22c10 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a  SQLITE_MISUSE]..
22c20 2a 2a 20 57 69 74 68 20 74 68 65 20 22 76 32 22  ** With the "v2"
22c30 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20   interface, any 
22c40 6f 66 20 74 68 65 20 6f 74 68 65 72 20 5b 72 65  of the other [re
22c50 73 75 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a  sult codes] or.*
22c60 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75  * [extended resu
22c70 6c 74 20 63 6f 64 65 73 5d 20 6d 69 67 68 74 20  lt codes] might 
22c80 62 65 20 72 65 74 75 72 6e 65 64 20 61 73 20 77  be returned as w
22c90 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49  ell..**.** [SQLI
22ca0 54 45 5f 42 55 53 59 5d 20 6d 65 61 6e 73 20 74  TE_BUSY] means t
22cb0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
22cc0 20 65 6e 67 69 6e 65 20 77 61 73 20 75 6e 61 62   engine was unab
22cd0 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 68  le to acquire th
22ce0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6c 6f  e.** database lo
22cf0 63 6b 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20  cks it needs to 
22d00 64 6f 20 69 74 73 20 6a 6f 62 2e 20 20 49 66 20  do its job.  If 
22d10 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
22d20 20 61 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f   a [COMMIT].** o
22d30 72 20 6f 63 63 75 72 73 20 6f 75 74 73 69 64 65  r occurs outside
22d40 20 6f 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20   of an explicit 
22d50 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
22d60 6e 20 79 6f 75 20 63 61 6e 20 72 65 74 72 79 20  n you can retry 
22d70 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  the.** statement
22d80 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d  .  If the statem
22d90 65 6e 74 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f  ent is not a [CO
22da0 4d 4d 49 54 5d 20 61 6e 64 20 6f 63 63 75 72 73  MMIT] and occurs
22db0 20 77 69 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70   within a.** exp
22dc0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f  licit transactio
22dd0 6e 20 74 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c  n then you shoul
22de0 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74  d rollback the t
22df0 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72  ransaction befor
22e00 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e  e.** continuing.
22e10 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44  .**.** [SQLITE_D
22e20 4f 4e 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20  ONE] means that 
22e30 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61  the statement ha
22e40 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63 75  s finished execu
22e50 74 69 6e 67 0a 2a 2a 20 73 75 63 63 65 73 73 66  ting.** successf
22e60 75 6c 6c 79 2e 20 20 73 71 6c 69 74 65 33 5f 73  ully.  sqlite3_s
22e70 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74  tep() should not
22e80 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e   be called again
22e90 20 6f 6e 20 74 68 69 73 20 76 69 72 74 75 61 6c   on this virtual
22ea0 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 77 69 74 68  .** machine with
22eb0 6f 75 74 20 66 69 72 73 74 20 63 61 6c 6c 69 6e  out first callin
22ec0 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  g [sqlite3_reset
22ed0 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65  ()] to reset the
22ee0 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68   virtual.** mach
22ef0 69 6e 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20  ine back to its 
22f00 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a  initial state..*
22f10 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20  *.** If the SQL 
22f20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
22f30 65 78 65 63 75 74 65 64 20 72 65 74 75 72 6e 73  executed returns
22f40 20 61 6e 79 20 64 61 74 61 2c 20 74 68 65 6e 20   any data, then 
22f50 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20  [SQLITE_ROW].** 
22f60 69 73 20 72 65 74 75 72 6e 65 64 20 65 61 63 68  is returned each
22f70 20 74 69 6d 65 20 61 20 6e 65 77 20 72 6f 77 20   time a new row 
22f80 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64 79  of data is ready
22f90 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20   for processing 
22fa0 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72  by the.** caller
22fb0 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6d 61 79  . The values may
22fc0 20 62 65 20 61 63 63 65 73 73 65 64 20 75 73 69   be accessed usi
22fd0 6e 67 20 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61  ng the [column a
22fe0 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d  ccess functions]
22ff0 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65  ..** sqlite3_ste
23000 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 67  p() is called ag
23010 61 69 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20  ain to retrieve 
23020 74 68 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20  the next row of 
23030 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c  data..**.** [SQL
23040 49 54 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73  ITE_ERROR] means
23050 20 74 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65   that a run-time
23060 20 65 72 72 6f 72 20 28 73 75 63 68 20 61 73 20   error (such as 
23070 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  a constraint.** 
23080 76 69 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f  violation) has o
23090 63 63 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65  ccurred.  sqlite
230a0 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20  3_step() should 
230b0 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67  not be called ag
230c0 61 69 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d  ain on.** the VM
230d0 2e 20 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69  . More informati
230e0 6f 6e 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20  on may be found 
230f0 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  by calling [sqli
23100 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a  te3_errmsg()]..*
23110 2a 20 57 69 74 68 20 74 68 65 20 6c 65 67 61 63  * With the legac
23120 79 20 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d  y interface, a m
23130 6f 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72  ore specific err
23140 6f 72 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61  or code (for exa
23150 6d 70 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45  mple,.** [SQLITE
23160 5f 49 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51  _INTERRUPT], [SQ
23170 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53  LITE_SCHEMA], [S
23180 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20  QLITE_CORRUPT], 
23190 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a  and so forth).**
231a0 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64   can be obtained
231b0 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c   by calling [sql
231c0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e  ite3_reset()] on
231d0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
231e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49  d statement].  I
231f0 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72  n the "v2" inter
23200 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72  face,.** the mor
23210 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72  e specific error
23220 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
23230 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 73 71  d directly by sq
23240 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a  lite3_step()..**
23250 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55  .** [SQLITE_MISU
23260 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74  SE] means that t
23270 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  he this routine 
23280 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70  was called inapp
23290 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50  ropriately..** P
232a0 65 72 68 61 70 73 20 69 74 20 77 61 73 20 63 61  erhaps it was ca
232b0 6c 6c 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61  lled on a [prepa
232c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
232d0 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61  hat has.** alrea
232e0 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33  dy been [sqlite3
232f0 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61  _finalize | fina
23300 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65  lized] or on one
23310 20 74 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65   that had.** pre
23320 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64  viously returned
23330 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
23340 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  or [SQLITE_DONE]
23350 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a  .  Or it could.*
23360 2a 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  * be the case th
23370 61 74 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  at the same data
23380 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
23390 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 62 79  is being used by
233a0 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20   two or.** more 
233b0 74 68 72 65 61 64 73 20 61 74 20 74 68 65 20 73  threads at the s
233c0 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69  ame moment in ti
233d0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f  me..**.** <b>Goo
233e0 66 79 20 49 6e 74 65 72 66 61 63 65 20 41 6c 65  fy Interface Ale
233f0 72 74 3a 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c  rt:</b> In the l
23400 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c  egacy interface,
23410 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65   the sqlite3_ste
23420 70 28 29 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79  p().** API alway
23430 73 20 72 65 74 75 72 6e 73 20 61 20 67 65 6e 65  s returns a gene
23440 72 69 63 20 65 72 72 6f 72 20 63 6f 64 65 2c 20  ric error code, 
23450 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20  [SQLITE_ERROR], 
23460 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a  following any.**
23470 20 65 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61   error other tha
23480 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  n [SQLITE_BUSY] 
23490 61 6e 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55  and [SQLITE_MISU
234a0 53 45 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63  SE].  You must c
234b0 61 6c 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  all.** [sqlite3_
234c0 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c  reset()] or [sql
234d0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
234e0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e   in order to fin
234f0 64 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20  d one of the.** 
23500 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20  specific [error 
23510 63 6f 64 65 73 5d 20 74 68 61 74 20 62 65 74 74  codes] that bett
23520 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68 65  er describes the
23530 20 65 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64   error..** We ad
23540 6d 69 74 20 74 68 61 74 20 74 68 69 73 20 69 73  mit that this is
23550 20 61 20 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e   a goofy design.
23560 20 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 68 61    The problem ha
23570 73 20 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20  s been fixed.** 
23580 77 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e  with the "v2" in
23590 74 65 72 66 61 63 65 2e 20 20 49 66 20 79 6f 75  terface.  If you
235a0 20 70 72 65 70 61 72 65 20 61 6c 6c 20 6f 66 20   prepare all of 
235b0 79 6f 75 72 20 53 51 4c 20 73 74 61 74 65 6d 65  your SQL stateme
235c0 6e 74 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74  nts.** using eit
235d0 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  her [sqlite3_pre
235e0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73  pare_v2()] or [s
235f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
23600 5f 76 32 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a  _v2()] instead.*
23610 2a 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 20  * of the legacy 
23620 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
23630 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
23640 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 69 6e  _prepare16()] in
23650 74 65 72 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65  terfaces,.** the
23660 6e 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69  n the more speci
23670 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73  fic [error codes
23680 5d 20 61 72 65 20 72 65 74 75 72 6e 65 64 20 64  ] are returned d
23690 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71  irectly.** by sq
236a0 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 54  lite3_step().  T
236b0 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 22 76  he use of the "v
236c0 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  2" interface is 
236d0 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a  recommended..**.
236e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
236f0 0a 2a 2a 20 5b 48 31 33 32 30 32 5d 20 5b 48 31  .** [H13202] [H1
23700 35 33 30 34 5d 20 5b 48 31 35 33 30 36 5d 20 5b  5304] [H15306] [
23710 48 31 35 33 30 38 5d 20 5b 48 31 35 33 31 30 5d  H15308] [H15310]
23720 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
23730 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  nt sqlite3_step(
23740 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
23750 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
23760 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
23770 6e 73 20 69 6e 20 61 20 72 65 73 75 6c 74 20 73  ns in a result s
23780 65 74 20 7b 48 31 33 37 37 30 7d 20 3c 53 31 30  et {H13770} <S10
23790 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  700>.**.** Retur
237a0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
237b0 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63   values in the c
237c0 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68  urrent row of th
237d0 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a  e result set..**
237e0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
237f0 3a 0a 2a 2a 20 5b 48 31 33 37 37 31 5d 20 5b 48  :.** [H13771] [H
23800 31 33 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13772].*/.SQLITE
23810 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
23820 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69  _data_count(sqli
23830 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
23840 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
23850 46 3a 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44  F: Fundamental D
23860 61 74 61 74 79 70 65 73 20 7b 48 31 30 32 36 35  atatypes {H10265
23870 7d 20 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32  } <S10110><S1012
23880 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
23890 53 51 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a  SQLITE_TEXT.**.*
238a0 2a 20 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79  * {H10266} Every
238b0 20 76 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65   value in SQLite
238c0 20 68 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65   has one of five
238d0 20 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74   fundamental dat
238e0 61 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75  atypes:.**.** <u
238f0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69  l>.** <li> 64-bi
23900 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
23910 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20  .** <li> 64-bit 
23920 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f  IEEE floating po
23930 69 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c  int number.** <l
23940 69 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69  i> string.** <li
23950 3e 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e  > BLOB.** <li> N
23960 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e  ULL.** </ul> {EN
23970 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  D}.**.** These c
23980 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64  onstants are cod
23990 65 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74  es for each of t
239a0 68 6f 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a  hose types..**.*
239b0 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
239c0 53 51 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73  SQLITE_TEXT cons
239d0 74 61 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73  tant was also us
239e0 65 64 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72  ed in SQLite ver
239f0 73 69 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20  sion 2.** for a 
23a00 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65  completely diffe
23a10 72 65 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53  rent meaning.  S
23a20 6f 66 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e  oftware that lin
23a30 6b 73 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a  ks against both.
23a40 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  ** SQLite versio
23a50 6e 20 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76  n 2 and SQLite v
23a60 65 72 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20  ersion 3 should 
23a70 75 73 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54  use SQLITE3_TEXT
23a80 2c 20 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f  , not.** SQLITE_
23a90 54 45 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  TEXT..*/.#define
23aa0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20   SQLITE_INTEGER 
23ab0 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
23ac0 45 5f 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65  E_FLOAT    2.#de
23ad0 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  fine SQLITE_BLOB
23ae0 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53       4.#define S
23af0 51 4c 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35  QLITE_NULL     5
23b00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
23b10 45 58 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  EXT.# undef SQLI
23b20 54 45 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20  TE_TEXT.#else.# 
23b30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
23b40 58 54 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a  XT     3.#endif.
23b50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f  #define SQLITE3_
23b60 54 45 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a  TEXT     3../*.*
23b70 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75  * CAPI3REF: Resu
23b80 6c 74 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41  lt Values From A
23b90 20 51 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20   Query {H13800} 
23ba0 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57  <S10700>.** KEYW
23bb0 4f 52 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63  ORDS: {column ac
23bc0 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a  cess functions}.
23bd0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
23be0 69 6e 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72  ines form the "r
23bf0 65 73 75 6c 74 20 73 65 74 20 71 75 65 72 79 22  esult set query"
23c00 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
23c10 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
23c20 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74   return informat
23c30 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67  ion about a sing
23c40 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  le column of the
23c50 20 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75   current.** resu
23c60 6c 74 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72  lt row of a quer
23c70 79 2e 20 20 49 6e 20 65 76 65 72 79 20 63 61 73  y.  In every cas
23c80 65 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  e the first argu
23c90 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65  ment is a pointe
23ca0 72 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65  r.** to the [pre
23cb0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
23cc0 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 65   that is being e
23cd0 76 61 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73  valuated (the [s
23ce0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a  qlite3_stmt*].**
23cf0 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e   that was return
23d00 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
23d10 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f  _prepare_v2()] o
23d20 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72  r one of its var
23d30 69 61 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68  iants).** and th
23d40 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
23d50 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f  t is the index o
23d60 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72  f the column for
23d70 20 77 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69   which informati
23d80 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  on.** should be 
23d90 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c  returned.  The l
23da0 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  eftmost column o
23db0 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
23dc0 20 68 61 73 20 74 68 65 20 69 6e 64 65 78 20 30   has the index 0
23dd0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
23de0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 6f 65  QL statement doe
23df0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
23e00 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64  point to a valid
23e10 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 65 0a   row, or if the.
23e20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20  ** column index 
23e30 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c  is out of range,
23e40 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75   the result is u
23e50 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65  ndefined..** The
23e60 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 79 20  se routines may 
23e70 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77  only be called w
23e80 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63  hen the most rec
23e90 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b  ent call to.** [
23ea0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
23eb0 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51  has returned [SQ
23ec0 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65  LITE_ROW] and ne
23ed0 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  ither.** [sqlite
23ee0 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b  3_reset()] nor [
23ef0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
23f00 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 63 61  ()] have been ca
23f10 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e 74 6c  lled subsequentl
23f20 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20  y..** If any of 
23f30 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  these routines a
23f40 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  re called after 
23f50 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
23f60 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ] or.** [sqlite3
23f70 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20  _finalize()] or 
23f80 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73  after [sqlite3_s
23f90 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 75 72  tep()] has retur
23fa0 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67  ned.** something
23fb0 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c   other than [SQL
23fc0 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65  ITE_ROW], the re
23fd0 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69  sults are undefi
23fe0 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69  ned..** If [sqli
23ff0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b  te3_step()] or [
24000 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
24010 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e   or [sqlite3_fin
24020 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20  alize()].** are 
24030 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64 69  called from a di
24040 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 77  fferent thread w
24050 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 65 73  hile any of thes
24060 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72  e routines.** ar
24070 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20  e pending, then 
24080 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  the results are 
24090 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
240a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c   The sqlite3_col
240b0 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 74 69  umn_type() routi
240c0 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a  ne returns the.*
240d0 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  * [SQLITE_INTEGE
240e0 52 20 7c 20 64 61 74 61 74 79 70 65 20 63 6f 64  R | datatype cod
240f0 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69 74 69  e] for the initi
24100 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a 2a 20  al data type.** 
24110 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  of the result co
24120 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74 75 72  lumn.  The retur
24130 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f 6e 65  ned value is one
24140 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45   of [SQLITE_INTE
24150 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45  GER],.** [SQLITE
24160 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45  _FLOAT], [SQLITE
24170 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f  _TEXT], [SQLITE_
24180 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54  BLOB], or [SQLIT
24190 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61  E_NULL].  The va
241a0 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  lue.** returned 
241b0 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  by sqlite3_colum
241c0 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e 6c 79  n_type() is only
241d0 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e   meaningful if n
241e0 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72  o type.** conver
241f0 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63 75 72  sions have occur
24200 72 65 64 20 61 73 20 64 65 73 63 72 69 62 65 64  red as described
24210 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72 20 61   below.  After a
24220 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e   type conversion
24230 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72  ,.** the value r
24240 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
24250 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29  e3_column_type()
24260 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20   is undefined.  
24270 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f  Future.** versio
24280 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79  ns of SQLite may
24290 20 63 68 61 6e 67 65 20 74 68 65 20 62 65 68 61   change the beha
242a0 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f  vior of sqlite3_
242b0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a  column_type().**
242c0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70   following a typ
242d0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a  e conversion..**
242e0 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c  .** If the resul
242f0 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 55  t is a BLOB or U
24300 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68 65 6e  TF-8 string then
24310 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c   the sqlite3_col
24320 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72  umn_bytes().** r
24330 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
24340 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
24350 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f 42 20  es in that BLOB 
24360 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66  or string..** If
24370 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
24380 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2c 20   UTF-16 string, 
24390 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c  then sqlite3_col
243a0 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e 76  umn_bytes() conv
243b0 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69  erts.** the stri
243c0 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20  ng to UTF-8 and 
243d0 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65  then returns the
243e0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
243f0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75  ..** If the resu
24400 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20  lt is a numeric 
24410 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74  value then sqlit
24420 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
24430 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74  ) uses.** [sqlit
24440 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74  e3_snprintf()] t
24450 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 76  o convert that v
24460 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d 38 20  alue to a UTF-8 
24470 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72  string and retur
24480 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  ns.** the number
24490 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 61   of bytes in tha
244a0 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65  t string..** The
244b0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
244c0 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  does not include
244d0 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e   the zero termin
244e0 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e 64 0a  ator at the end.
244f0 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  ** of the string
24500 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79 3a 20  .  For clarity: 
24510 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
24520 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ed is the number
24530 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20   of.** bytes in 
24540 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20  the string, not 
24550 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
24560 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  aracters..**.** 
24570 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64  Strings returned
24580 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   by sqlite3_colu
24590 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71  mn_text() and sq
245a0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
245b0 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65  t16(),.** even e
245c0 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20 61 72  mpty strings, ar
245d0 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20 74 65  e always zero te
245e0 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 72  rminated.  The r
245f0 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66  eturn.** value f
24600 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  rom sqlite3_colu
24610 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 20  mn_blob() for a 
24620 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42  zero-length BLOB
24630 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79   is an arbitrary
24640 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73  .** pointer, pos
24650 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c  sibly even a NUL
24660 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
24670 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c   The sqlite3_col
24680 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 72 6f  umn_bytes16() ro
24690 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72  utine is similar
246a0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
246b0 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 62 75  mn_bytes().** bu
246c0 74 20 6c 65 61 76 65 73 20 74 68 65 20 72 65 73  t leaves the res
246d0 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20 69 6e  ult in UTF-16 in
246e0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
246f0 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54  er instead of UT
24700 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f  F-8..** The zero
24710 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e   terminator is n
24720 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74  ot included in t
24730 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a  his count..**.**
24740 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65 74 75   The object retu
24750 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  rned by [sqlite3
24760 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d  _column_value()]
24770 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f   is an.** [unpro
24780 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
24790 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41  alue] object.  A
247a0 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71  n unprotected sq
247b0 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
247c0 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62  ct.** may only b
247d0 65 20 75 73 65 64 20 77 69 74 68 20 5b 73 71 6c  e used with [sql
247e0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28  ite3_bind_value(
247f0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
24800 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e  result_value()].
24810 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e 70 72  .** If the [unpr
24820 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
24830 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 72 65  value] object re
24840 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71  turned by.** [sq
24850 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
24860 75 65 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e  ue()] is used in
24870 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79 2c 20   any other way, 
24880 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a  including calls.
24890 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c  ** to routines l
248a0 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  ike [sqlite3_val
248b0 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69  ue_int()], [sqli
248c0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29  te3_value_text()
248d0 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65  ],.** or [sqlite
248e0 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d  3_value_bytes()]
248f0 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76  , then the behav
24900 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
24910 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
24920 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 20 74  utines attempt t
24930 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61  o convert the va
24940 6c 75 65 20 77 68 65 72 65 20 61 70 70 72 6f 70  lue where approp
24950 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65  riate.  For.** e
24960 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 69  xample, if the i
24970 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e  nternal represen
24980 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20  tation is FLOAT 
24990 61 6e 64 20 61 20 74 65 78 74 20 72 65 73 75 6c  and a text resul
249a0 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65  t.** is requeste
249b0 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72  d, [sqlite3_snpr
249c0 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65 64 20  intf()] is used 
249d0 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65  internally to pe
249e0 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e  rform the.** con
249f0 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69  version automati
24a00 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c  cally.  The foll
24a10 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 74 61  owing table deta
24a20 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 73 69  ils the conversi
24a30 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  ons.** that are 
24a40 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c  applied:.**.** <
24a50 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c  blockquote>.** <
24a60 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22  table border="1"
24a70 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e  >.** <tr><th> In
24a80 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c  ternal<br>Type <
24a90 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c 62 72  th> Requested<br
24aa0 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76  >Type <th>  Conv
24ab0 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72  ersion.**.** <tr
24ac0 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c  ><td>  NULL    <
24ad0 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74  td> INTEGER   <t
24ae0 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 0a 2a  d> Result is 0.*
24af0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c  * <tr><td>  NULL
24b00 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20      <td>  FLOAT 
24b10 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69     <td> Result i
24b20 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64  s 0.0.** <tr><td
24b30 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20  >  NULL    <td> 
24b40 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 52    TEXT    <td> R
24b50 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f  esult is NULL po
24b60 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64  inter.** <tr><td
24b70 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20  >  NULL    <td> 
24b80 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52    BLOB    <td> R
24b90 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f  esult is NULL po
24ba0 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64  inter.** <tr><td
24bb0 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20  > INTEGER  <td> 
24bc0 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43   FLOAT    <td> C
24bd0 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e 74 65  onvert from inte
24be0 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20  ger to float.** 
24bf0 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52  <tr><td> INTEGER
24c00 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20    <td>   TEXT   
24c10 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64   <td> ASCII rend
24c20 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74  ering of the int
24c30 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  eger.** <tr><td>
24c40 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20   INTEGER  <td>  
24c50 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61   BLOB    <td> Sa
24c60 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d 3e 54  me as INTEGER->T
24c70 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  EXT.** <tr><td> 
24c80 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e   FLOAT   <td> IN
24c90 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e  TEGER   <td> Con
24ca0 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20  vert from float 
24cb0 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74  to integer.** <t
24cc0 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20  r><td>  FLOAT   
24cd0 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c  <td>   TEXT    <
24ce0 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72  td> ASCII render
24cf0 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 74  ing of the float
24d00 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c  .** <tr><td>  FL
24d10 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f  OAT   <td>   BLO
24d20 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61  B    <td> Same a
24d30 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a  s FLOAT->TEXT.**
24d40 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20   <tr><td>  TEXT 
24d50 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20     <td> INTEGER 
24d60 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 69 28    <td> Use atoi(
24d70 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54  ).** <tr><td>  T
24d80 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f  EXT    <td>  FLO
24d90 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 20 61  AT    <td> Use a
24da0 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64  tof().** <tr><td
24db0 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20  >  TEXT    <td> 
24dc0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e    BLOB    <td> N
24dd0 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e  o change.** <tr>
24de0 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74  <td>  BLOB    <t
24df0 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64  d> INTEGER   <td
24e00 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58  > Convert to TEX
24e10 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 69 28  T then use atoi(
24e20 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42  ).** <tr><td>  B
24e30 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f  LOB    <td>  FLO
24e40 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65  AT    <td> Conve
24e50 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20  rt to TEXT then 
24e60 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74  use atof().** <t
24e70 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20  r><td>  BLOB    
24e80 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c  <td>   TEXT    <
24e90 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f 20 74  td> Add a zero t
24ea0 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65 65  erminator if nee
24eb0 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a  ded.** </table>.
24ec0 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  ** </blockquote>
24ed0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65  .**.** The table
24ee0 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66   above makes ref
24ef0 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61  erence to standa
24f00 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e  rd C library fun
24f10 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a  ctions atoi().**
24f20 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51   and atof().  SQ
24f30 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65  Lite does not re
24f40 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20 66  ally use these f
24f50 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61  unctions.  It ha
24f60 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75  s its.** own equ
24f70 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c  ivalent internal
24f80 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20   routines.  The 
24f90 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28  atoi() and atof(
24fa0 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75  ) names are.** u
24fb0 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65  sed in the table
24fc0 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64   for brevity and
24fd0 20 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72   because they ar
24fe0 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f  e familiar to mo
24ff0 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d  st.** C programm
25000 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ers..**.** Note 
25010 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 20 63  that when type c
25020 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72  onversions occur
25030 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72  , pointers retur
25040 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20  ned by prior.** 
25050 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
25060 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20  _column_blob(), 
25070 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
25080 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a  ext(), and/or.**
25090 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
250a0 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 65 20  text16() may be 
250b0 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20  invalidated..** 
250c0 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73  Type conversions
250d0 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76   and pointer inv
250e0 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74  alidations might
250f0 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65   occur.** in the
25100 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73   following cases
25110 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
25120 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c  <li> The initial
25130 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c   content is a BL
25140 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63  OB and sqlite3_c
25150 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a  olumn_text() or.
25160 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  **      sqlite3_
25170 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20  column_text16() 
25180 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65  is called.  A ze
25190 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69  ro-terminator mi
251a0 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64  ght.**      need
251b0 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20   to be added to 
251c0 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e  the string.</li>
251d0 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69  .** <li> The ini
251e0 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20  tial content is 
251f0 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64 20 73  UTF-8 text and s
25200 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
25210 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20  tes16() or.**   
25220 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
25230 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61  n_text16() is ca
25240 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65  lled.  The conte
25250 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65  nt must be conve
25260 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20  rted.**      to 
25270 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20  UTF-16.</li>.** 
25280 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c  <li> The initial
25290 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d   content is UTF-
252a0 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69  16 text and sqli
252b0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
252c0 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71  () or.**      sq
252d0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
252e0 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  t() is called.  
252f0 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74  The content must
25300 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a   be converted.**
25310 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c        to UTF-8.<
25320 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  /li>.** </ul>.**
25330 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20  .** Conversions 
25340 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62 65  between UTF-16be
25350 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 72   and UTF-16le ar
25360 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e  e always done in
25370 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a   place and do.**
25380 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20   not invalidate 
25390 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c  a prior pointer,
253a0 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73   though of cours
253b0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
253c0 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 74   the buffer.** t
253d0 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70 6f  hat the prior po
253e0 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20  inter points to 
253f0 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 6d  will have been m
25400 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72 20  odified.  Other 
25410 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76  kinds.** of conv
25420 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20  ersion are done 
25430 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69 74  in place when it
25440 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62 75   is possible, bu
25450 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 65 79  t sometimes they
25460 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73 73  .** are not poss
25470 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f 73  ible and in thos
25480 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70 6f  e cases prior po
25490 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61 6c  inters are inval
254a0 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  idated..**.** Th
254b0 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 61 73  e safest and eas
254c0 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 72  iest to remember
254d0 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 6e   policy is to in
254e0 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74 69  voke these routi
254f0 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66  nes.** in one of
25500 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77   the following w
25510 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  ays:.**.** <ul>.
25520 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f  **  <li>sqlite3_
25530 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f  column_text() fo
25540 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65  llowed by sqlite
25550 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
25560 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71  </li>.**  <li>sq
25570 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
25580 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  b() followed by 
25590 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
255a0 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20  ytes()</li>.**  
255b0 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75  <li>sqlite3_colu
255c0 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c 6c  mn_text16() foll
255d0 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  owed by sqlite3_
255e0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
255f0 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  </li>.** </ul>.*
25600 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f  *.** In other wo
25610 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20  rds, you should 
25620 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c  call sqlite3_col
25630 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73  umn_text(),.** s
25640 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
25650 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33  ob(), or sqlite3
25660 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29  _column_text16()
25670 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65 20   first to force 
25680 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e  the result.** in
25690 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 20 66  to the desired f
256a0 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f  ormat, then invo
256b0 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ke sqlite3_colum
256c0 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20  n_bytes() or.** 
256d0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
256e0 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 6e 64  ytes16() to find
256f0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
25700 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74   result.  Do not
25710 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f   mix calls.** to
25720 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25730 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 74 65  text() or sqlite
25740 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20  3_column_blob() 
25750 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  with calls to.**
25760 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25770 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 20 64  bytes16(), and d
25780 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20  o not mix calls 
25790 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  to sqlite3_colum
257a0 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 77 69  n_text16().** wi
257b0 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  th calls to sqli
257c0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
257d0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  ()..**.** The po
257e0 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 20  inters returned 
257f0 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20  are valid until 
25800 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f  a type conversio
25810 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20 64  n occurs as.** d
25820 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20  escribed above, 
25830 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65  or until [sqlite
25840 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71  3_step()] or [sq
25850 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
25860 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  r.** [sqlite3_fi
25870 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 61 6c  nalize()] is cal
25880 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79  led.  The memory
25890 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 68   space used to h
258a0 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61  old strings.** a
258b0 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72 65 65  nd BLOBs is free
258c0 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e  d automatically.
258d0 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20    Do <b>not</b> 
258e0 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72  pass the pointer
258f0 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73  s returned.** [s
25900 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
25910 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  ob()], [sqlite3_
25920 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20  column_text()], 
25930 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71  etc. into.** [sq
25940 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a  lite3_free()]..*
25950 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79  *.** If a memory
25960 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
25970 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
25980 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f  the evaluation o
25990 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73  f any.** of thes
259a0 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 64 65  e routines, a de
259b0 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 20 72  fault value is r
259c0 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 64 65  eturned.  The de
259d0 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69  fault value.** i
259e0 73 20 65 69 74 68 65 72 20 74 68 65 20 69 6e 74  s either the int
259f0 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c 6f 61  eger 0, the floa
25a00 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65  ting point numbe
25a10 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c  r 0.0, or a NULL
25a20 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53 75  .** pointer.  Su
25a30 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
25a40 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  o [sqlite3_errco
25a50 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72  de()] will retur
25a60 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d  n.** [SQLITE_NOM
25a70 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  EM]..**.** Requi
25a80 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
25a90 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20 5b 48  803] [H13806] [H
25aa0 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32 5d 20  13809] [H13812] 
25ab0 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38 31 38  [H13815] [H13818
25ac0 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31 33 38  ] [H13821] [H138
25ad0 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37 5d 20  24].** [H13827] 
25ae0 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51 4c 49  [H13830].*/.SQLI
25af0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
25b00 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
25b10 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73  n_blob(sqlite3_s
25b20 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
25b30 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
25b40 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
25b50 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ytes(sqlite3_stm
25b60 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
25b70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
25b80 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
25b90 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  es16(sqlite3_stm
25ba0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
25bb0 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
25bc0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25bd0 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
25be0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
25bf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
25c00 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
25c10 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt(sqlite3_stmt*
25c20 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c  , int iCol);.SQL
25c30 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
25c40 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 6f  int64 sqlite3_co
25c50 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74  lumn_int64(sqlit
25c60 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
25c70 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ol);.SQLITE_API 
25c80 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
25c90 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
25ca0 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33  umn_text(sqlite3
25cb0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
25cc0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
25cd0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
25ce0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
25cf0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
25d00 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
25d10 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
25d20 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c  _column_type(sql
25d30 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
25d40 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
25d50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  I sqlite3_value 
25d60 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
25d70 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74  value(sqlite3_st
25d80 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
25d90 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
25da0 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 70 61   Destroy A Prepa
25db0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62  red Statement Ob
25dc0 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 3c 53  ject {H13300} <S
25dd0 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e 0a 2a  70300><S30100>.*
25de0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
25df0 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 6e 63  _finalize() func
25e00 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
25e10 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 65 70  o delete a [prep
25e20 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
25e30 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74 65  .** If the state
25e40 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 74 65  ment was execute
25e50 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f  d successfully o
25e60 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 20 61  r not executed a
25e70 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a 20 53  t all, then.** S
25e80 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
25e90 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 74 69  rned. If executi
25ea0 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  on of the statem
25eb0 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 6e 20  ent failed then 
25ec0 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64  an.** [error cod
25ed0 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20  e] or [extended 
25ee0 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72  error code] is r
25ef0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
25f00 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20  his routine can 
25f10 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79  be called at any
25f20 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 74 68   point during th
25f30 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  e execution of t
25f40 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20  he.** [prepared 
25f50 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 66 20  statement].  If 
25f60 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
25f70 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 63  ine has not.** c
25f80 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 74 69  ompleted executi
25f90 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  on when this rou
25fa0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
25fb0 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a 2a 20  that is like.** 
25fc0 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 6e 20  encountering an 
25fd0 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 71 6c  error or an [sql
25fe0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 7c  ite3_interrupt |
25ff0 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a 2a 20   interrupt]..** 
26000 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 61 74  Incomplete updat
26010 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64  es may be rolled
26020 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e 73 61   back and transa
26030 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 64 2c  ctions canceled,
26040 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  .** depending on
26050 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 6e 63   the circumstanc
26060 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b  es, and the.** [
26070 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 74 75  error code] retu
26080 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b 53 51  rned will be [SQ
26090 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a  LITE_ABORT]..**.
260a0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
260b0 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b 48 31  .** [H11302] [H1
260c0 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  1304].*/.SQLITE_
260d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
260e0 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  finalize(sqlite3
260f0 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a  _stmt *pStmt);..
26100 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
26110 52 65 73 65 74 20 41 20 50 72 65 70 61 72 65 64  Reset A Prepared
26120 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63   Statement Objec
26130 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37 30 33  t {H13330} <S703
26140 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
26150 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 66 75  lite3_reset() fu
26160 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
26170 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70 72 65   to reset a [pre
26180 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
26190 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63 6b 20  .** object back 
261a0 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73  to its initial s
261b0 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f 20 62  tate, ready to b
261c0 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e 0a 2a  e re-executed..*
261d0 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74 65 6d  * Any SQL statem
261e0 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20 74 68  ent variables th
261f0 61 74 20 68 61 64 20 76 61 6c 75 65 73 20 62 6f  at had values bo
26200 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73 69 6e  und to them usin
26210 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65  g.** the [sqlite
26220 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 73 71  3_bind_blob | sq
26230 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 41  lite3_bind_*() A
26240 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65 69 72  PI] retain their
26250 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 65 20   values..** Use 
26260 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62  [sqlite3_clear_b
26270 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 72 65  indings()] to re
26280 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e 67 73  set the bindings
26290 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 32 7d  ..**.** {H11332}
262a0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65   The [sqlite3_re
262b0 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63  set(S)] interfac
262c0 65 20 72 65 73 65 74 73 20 74 68 65 20 5b 70 72  e resets the [pr
262d0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
262e0 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ] S.**          
262f0 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67 69  back to the begi
26300 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 72 6f  nning of its pro
26310 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  gram..**.** {H11
26320 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f 73 74  334} If the most
26330 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20   recent call to 
26340 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29  [sqlite3_step(S)
26350 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20  ] for the.**    
26360 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20        [prepared 
26370 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72 65 74  statement] S ret
26380 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 4f  urned [SQLITE_RO
26390 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f  W] or [SQLITE_DO
263a0 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  NE],.**         
263b0 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f   or if [sqlite3_
263c0 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e 65 76  step(S)] has nev
263d0 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 63  er before been c
263e0 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a 20 20  alled on S,.**  
263f0 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71          then [sq
26400 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20  lite3_reset(S)] 
26410 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
26420 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33  OK]..**.** {H113
26430 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73 74 20  36} If the most 
26440 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b  recent call to [
26450 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d  sqlite3_step(S)]
26460 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20   for the.**     
26470 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73       [prepared s
26480 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 64 69  tatement] S indi
26490 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72 2c 20  cated an error, 
264a0 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  then.**         
264b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
264c0 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e 20 61  S)] returns an a
264d0 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f  ppropriate [erro
264e0 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  r code]..**.** {
264f0 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73 71 6c  H11338} The [sql
26500 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 69  ite3_reset(S)] i
26510 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 6e 6f  nterface does no
26520 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  t change the val
26530 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ues.**          
26540 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 33 5f  of any [sqlite3_
26550 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 69 6e  bind_blob|bindin
26560 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 65 70  gs] on the [prep
26570 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
26580 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  S..*/.SQLITE_API
26590 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 73   int sqlite3_res
265a0 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  et(sqlite3_stmt 
265b0 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
265c0 43 41 50 49 33 52 45 46 3a 20 43 72 65 61 74 65  CAPI3REF: Create
265d0 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53 51 4c   Or Redefine SQL
265e0 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 31   Functions {H161
265f0 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20  00} <S20200>.** 
26600 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e 63 74  KEYWORDS: {funct
26610 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75  ion creation rou
26620 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52  tines}.** KEYWOR
26630 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f 6e  DS: {application
26640 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e  -defined SQL fun
26650 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57 4f 52  ction}.** KEYWOR
26660 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 6f 6e  DS: {application
26670 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e  -defined SQL fun
26680 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68  ctions}.**.** Th
26690 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e  ese two function
266a0 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c 79 20  s (collectively 
266b0 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63 74 69  known as "functi
266c0 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f 75 74  on creation rout
266d0 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20 75 73  ines").** are us
266e0 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 66 75  ed to add SQL fu
266f0 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65  nctions or aggre
26700 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65 64 65  gates or to rede
26710 66 69 6e 65 20 74 68 65 20 62 65 68 61 76 69 6f  fine the behavio
26720 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69 6e 67  r.** of existing
26730 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f   SQL functions o
26740 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 54  r aggregates.  T
26750 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e  he only differen
26760 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 0a 2a  ce between the.*
26770 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20 74 68  * two is that th
26780 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
26790 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  er, the name of 
267a0 74 68 65 20 28 73 63 61 6c 61 72 29 20 66 75 6e  the (scalar) fun
267b0 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72  ction or.** aggr
267c0 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f 64 65  egate, is encode
267d0 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 20 73  d in UTF-8 for s
267e0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
267f0 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55 54 46  nction() and UTF
26800 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74  -16.** for sqlit
26810 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
26820 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  on16()..**.** Th
26830 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
26840 72 20 69 73 20 74 68 65 20 5b 64 61 74 61 62 61  r is the [databa
26850 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74  se connection] t
26860 6f 20 77 68 69 63 68 20 74 68 65 20 53 51 4c 0a  o which the SQL.
26870 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74  ** function is t
26880 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49 66 20  o be added.  If 
26890 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72 61 6d  a single program
268a0 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61 6e 20   uses more than 
268b0 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  one database.** 
268c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74 65 72  connection inter
268d0 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51 4c 20  nally, then SQL 
268e0 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 20 62  functions must b
268f0 65 20 61 64 64 65 64 20 69 6e 64 69 76 69 64 75  e added individu
26900 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63 68 20  ally to.** each 
26910 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
26920 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ion..**.** The s
26930 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
26940 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
26950 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
26960 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 6f 72  to be created or
26970 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20  .** redefined.  
26980 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  The length of th
26990 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 74 65  e name is limite
269a0 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 2c 20  d to 255 bytes, 
269b0 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a 2a 20  exclusive of.** 
269c0 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  the zero-termina
269d0 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  tor.  Note that 
269e0 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 68 20  the name length 
269f0 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 74 65  limit is in byte
26a00 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 61 63  s, not.** charac
26a10 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 65 6d  ters.  Any attem
26a20 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 20 66  pt to create a f
26a30 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 6c  unction with a l
26a40 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 77 69  onger name.** wi
26a50 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b 53 51  ll result in [SQ
26a60 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65 69 6e  LITE_ERROR] bein
26a70 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  g returned..**.*
26a80 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61  * The third para
26a90 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a 2a 20  meter (nArg).** 
26aa0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
26ab0 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20   arguments that 
26ac0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
26ad0 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65   or.** aggregate
26ae0 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69 73 20   takes. If this 
26af0 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67  parameter is neg
26b00 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 65 20  ative, then the 
26b10 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a  SQL function or.
26b20 2a 2a 20 61 67 67 72 65 67 61 74 65 20 6d 61 79  ** aggregate may
26b30 20 74 61 6b 65 20 61 6e 79 20 6e 75 6d 62 65 72   take any number
26b40 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a   of arguments..*
26b50 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20  *.** The fourth 
26b60 70 61 72 61 6d 65 74 65 72 2c 20 65 54 65 78 74  parameter, eText
26b70 52 65 70 2c 20 73 70 65 63 69 66 69 65 73 20 77  Rep, specifies w
26b80 68 61 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55  hat.** [SQLITE_U
26b90 54 46 38 20 7c 20 74 65 78 74 20 65 6e 63 6f 64  TF8 | text encod
26ba0 69 6e 67 5d 20 74 68 69 73 20 53 51 4c 20 66 75  ing] this SQL fu
26bb0 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 73 20 66  nction prefers f
26bc0 6f 72 0a 2a 2a 20 69 74 73 20 70 61 72 61 6d 65  or.** its parame
26bd0 74 65 72 73 2e 20 20 41 6e 79 20 53 51 4c 20 66  ters.  Any SQL f
26be0 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
26bf0 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65  tation should be
26c00 20 61 62 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a   able to work.**
26c10 20 77 6f 72 6b 20 77 69 74 68 20 55 54 46 2d 38   work with UTF-8
26c20 2c 20 55 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55  , UTF-16le, or U
26c30 54 46 2d 31 36 62 65 2e 20 20 42 75 74 20 73 6f  TF-16be.  But so
26c40 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  me implementatio
26c50 6e 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72  ns may be.** mor
26c60 65 20 65 66 66 69 63 69 65 6e 74 20 77 69 74 68  e efficient with
26c70 20 6f 6e 65 20 65 6e 63 6f 64 69 6e 67 20 74 68   one encoding th
26c80 61 6e 20 61 6e 6f 74 68 65 72 2e 20 20 49 74 20  an another.  It 
26c90 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 2a 2a  is allowed to.**
26ca0 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
26cb0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
26cc0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65  ) or sqlite3_cre
26cd0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
26ce0 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d   multiple.** tim
26cf0 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  es with the same
26d00 20 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69   function but wi
26d10 74 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c  th different val
26d20 75 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e  ues of eTextRep.
26d30 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c  .** When multipl
26d40 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
26d50 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75  s of the same fu
26d60 6e 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c  nction are avail
26d70 61 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20  able, SQLite.** 
26d80 77 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e  will pick the on
26d90 65 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20  e that involves 
26da0 74 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74  the least amount
26db0 20 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73   of data convers
26dc0 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  ion..** If there
26dd0 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c   is only a singl
26de0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
26df0 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20   which does not 
26e00 63 61 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a  care what text.*
26e10 2a 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73  * encoding is us
26e20 65 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75  ed, then the fou
26e30 72 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f  rth argument sho
26e40 75 6c 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41  uld be [SQLITE_A
26e50 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  NY]..**.** The f
26e60 69 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69  ifth parameter i
26e70 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 70  s an arbitrary p
26e80 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70  ointer.  The imp
26e90 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
26ea0 68 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63  he.** function c
26eb0 61 6e 20 67 61 69 6e 20 61 63 63 65 73 73 20 74  an gain access t
26ec0 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75  o this pointer u
26ed0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73  sing [sqlite3_us
26ee0 65 72 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a  er_data()]..**.*
26ef0 2a 20 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65  * The seventh, e
26f00 69 67 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20  ighth and ninth 
26f10 70 61 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e  parameters, xFun
26f20 63 2c 20 78 53 74 65 70 20 61 6e 64 20 78 46 69  c, xStep and xFi
26f30 6e 61 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e  nal, are.** poin
26f40 74 65 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61  ters to C-langua
26f50 67 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ge functions tha
26f60 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  t implement the 
26f70 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a  SQL function or.
26f80 2a 2a 20 61 67 67 72 65 67 61 74 65 2e 20 41 20  ** aggregate. A 
26f90 73 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74  scalar SQL funct
26fa0 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20  ion requires an 
26fb0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
26fc0 66 20 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63  f the xFunc.** c
26fd0 61 6c 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55  allback only, NU
26fe0 4c 4c 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75  LL pointers shou
26ff0 6c 64 20 62 65 20 70 61 73 73 65 64 20 61 73 20  ld be passed as 
27000 74 68 65 20 78 53 74 65 70 20 61 6e 64 20 78 46  the xStep and xF
27010 69 6e 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65  inal.** paramete
27020 72 73 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65  rs. An aggregate
27030 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65   SQL function re
27040 71 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d  quires an implem
27050 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65  entation of xSte
27060 70 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20  p.** and xFinal 
27070 61 6e 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20  and NULL should 
27080 62 65 20 70 61 73 73 65 64 20 66 6f 72 20 78 46  be passed for xF
27090 75 6e 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61  unc. To delete a
270a0 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51  n existing.** SQ
270b0 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67  L function or ag
270c0 67 72 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55  gregate, pass NU
270d0 4c 4c 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65  LL for all three
270e0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61   function callba
270f0 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  cks..**.** It is
27100 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65   permitted to re
27110 67 69 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20  gister multiple 
27120 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
27130 6f 66 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66  of the same.** f
27140 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68  unctions with th
27150 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20  e same name but 
27160 77 69 74 68 20 65 69 74 68 65 72 20 64 69 66 66  with either diff
27170 65 72 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66  ering numbers of
27180 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72  .** arguments or
27190 20 64 69 66 66 65 72 69 6e 67 20 70 72 65 66 65   differing prefe
271a0 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69  rred text encodi
271b0 6e 67 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ngs.  SQLite wil
271c0 6c 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70  l use.** the imp
271d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 6f 73 74  lementation most
271e0 20 63 6c 6f 73 65 6c 79 20 6d 61 74 63 68 65 73   closely matches
271f0 20 74 68 65 20 77 61 79 20 69 6e 20 77 68 69 63   the way in whic
27200 68 20 74 68 65 0a 2a 2a 20 53 51 4c 20 66 75 6e  h the.** SQL fun
27210 63 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20 20  ction is used.  
27220 41 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  A function imple
27230 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 20 61  mentation with a
27240 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 0a 2a 2a   non-negative.**
27250 20 6e 41 72 67 20 70 61 72 61 6d 65 74 65 72 20   nArg parameter 
27260 69 73 20 61 20 62 65 74 74 65 72 20 6d 61 74 63  is a better matc
27270 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f  h than a functio
27280 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
27290 20 77 69 74 68 0a 2a 2a 20 61 20 6e 65 67 61 74   with.** a negat
272a0 69 76 65 20 6e 41 72 67 2e 20 20 41 20 66 75 6e  ive nArg.  A fun
272b0 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20  ction where the 
272c0 70 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  preferred text e
272d0 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d 61 74 63 68  ncoding.** match
272e0 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  es the database 
272f0 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20 62 65  encoding is a be
27300 74 74 65 72 0a 2a 2a 20 6d 61 74 63 68 20 74 68  tter.** match th
27310 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 68  an a function wh
27320 65 72 65 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  ere the encoding
27330 20 69 73 20 64 69 66 66 65 72 65 6e 74 2e 20 20   is different.  
27340 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77  .** A function w
27350 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64 69 6e  here the encodin
27360 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20  g difference is 
27370 62 65 74 77 65 65 6e 20 55 54 46 31 36 6c 65 20  between UTF16le 
27380 61 6e 64 20 55 54 46 31 36 62 65 0a 2a 2a 20 69  and UTF16be.** i
27390 73 20 61 20 63 6c 6f 73 65 72 20 6d 61 74 63 68  s a closer match
273a0 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e   than a function
273b0 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f 64   where the encod
273c0 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 20 69  ing difference i
273d0 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 55 54 46  s.** between UTF
273e0 38 20 61 6e 64 20 55 54 46 31 36 2e 0a 2a 2a 0a  8 and UTF16..**.
273f0 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 66 75 6e 63  ** Built-in func
27400 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 6f 76 65  tions may be ove
27410 72 6c 6f 61 64 65 64 20 62 79 20 6e 65 77 20 61  rloaded by new a
27420 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
27430 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  ed functions..**
27440 20 54 68 65 20 66 69 72 73 74 20 61 70 70 6c 69   The first appli
27450 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
27460 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 67  unction with a g
27470 69 76 65 6e 20 6e 61 6d 65 20 6f 76 65 72 72 69  iven name overri
27480 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 75 69 6c 74  des all.** built
27490 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e  -in functions in
274a0 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62   the same [datab
274b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
274c0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61  with the same na
274d0 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 71 75 65 6e  me..** Subsequen
274e0 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  t application-de
274f0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20  fined functions 
27500 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  of the same name
27510 20 6f 6e 6c 79 20 6f 76 65 72 72 69 64 65 20 0a   only override .
27520 2a 2a 20 70 72 69 6f 72 20 61 70 70 6c 69 63 61  ** prior applica
27530 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
27540 63 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20  ctions that are 
27550 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 66  an exact match f
27560 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  or the.** number
27570 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 61   of parameters a
27580 6e 64 20 70 72 65 66 65 72 72 65 64 20 65 6e 63  nd preferred enc
27590 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  oding..**.** An 
275a0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
275b0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ned function is 
275c0 70 65 72 6d 69 74 74 65 64 20 74 6f 20 63 61 6c  permitted to cal
275d0 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 74  l other.** SQLit
275e0 65 20 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48  e interfaces.  H
275f0 6f 77 65 76 65 72 2c 20 73 75 63 68 20 63 61 6c  owever, such cal
27600 6c 73 20 6d 75 73 74 20 6e 6f 74 0a 2a 2a 20 63  ls must not.** c
27610 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73  lose the databas
27620 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e 6f 72  e connection nor
27630 20 66 69 6e 61 6c 69 7a 65 20 6f 72 20 72 65 73   finalize or res
27640 65 74 20 74 68 65 20 70 72 65 70 61 72 65 64 0a  et the prepared.
27650 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  ** statement in 
27660 77 68 69 63 68 20 74 68 65 20 66 75 6e 63 74 69  which the functi
27670 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a  on is running..*
27680 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
27690 73 3a 0a 2a 2a 20 5b 48 31 36 31 30 33 5d 20 5b  s:.** [H16103] [
276a0 48 31 36 31 30 36 5d 20 5b 48 31 36 31 30 39 5d  H16106] [H16109]
276b0 20 5b 48 31 36 31 31 32 5d 20 5b 48 31 36 31 31   [H16112] [H1611
276c0 38 5d 20 5b 48 31 36 31 32 31 5d 20 5b 48 31 36  8] [H16121] [H16
276d0 31 32 34 5d 20 5b 48 31 36 31 32 37 5d 0a 2a 2a  124] [H16127].**
276e0 20 5b 48 31 36 31 33 30 5d 20 5b 48 31 36 31 33   [H16130] [H1613
276f0 33 5d 20 5b 48 31 36 31 33 36 5d 20 5b 48 31 36  3] [H16136] [H16
27700 31 33 39 5d 20 5b 48 31 36 31 34 32 5d 0a 2a 2f  139] [H16142].*/
27710 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
27720 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
27730 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74  unction(.  sqlit
27740 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
27750 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e  char *zFunctionN
27760 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c  ame,.  int nArg,
27770 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
27780 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20  .  void *pApp,. 
27790 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
277a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
277b0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
277c0 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  e**),.  void (*x
277d0 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
277e0 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
277f0 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76  e3_value**),.  v
27800 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71  oid (*xFinal)(sq
27810 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a  lite3_context*).
27820 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
27830 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
27840 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 73  _function16(.  s
27850 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
27860 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74  nst void *zFunct
27870 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e  ionName,.  int n
27880 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74  Arg,.  int eText
27890 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70  Rep,.  void *pAp
278a0 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  p,.  void (*xFun
278b0 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
278c0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
278d0 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
278e0 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65   (*xStep)(sqlite
278f0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
27900 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
27910 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  .  void (*xFinal
27920 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
27930 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  t*).);../*.** CA
27940 50 49 33 52 45 46 3a 20 54 65 78 74 20 45 6e 63  PI3REF: Text Enc
27950 6f 64 69 6e 67 73 20 7b 48 31 30 32 36 37 7d 20  odings {H10267} 
27960 3c 53 35 30 32 30 30 3e 20 3c 48 31 36 31 30 30  <S50200> <H16100
27970 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f  >.**.** These co
27980 6e 73 74 61 6e 74 20 64 65 66 69 6e 65 20 69 6e  nstant define in
27990 74 65 67 65 72 20 63 6f 64 65 73 20 74 68 61 74  teger codes that
279a0 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 76   represent the v
279b0 61 72 69 6f 75 73 0a 2a 2a 20 74 65 78 74 20 65  arious.** text e
279c0 6e 63 6f 64 69 6e 67 73 20 73 75 70 70 6f 72 74  ncodings support
279d0 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2f  ed by SQLite..*/
279e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
279f0 55 54 46 38 20 20 20 20 20 20 20 20 20 20 20 31  UTF8           1
27a00 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
27a10 55 54 46 31 36 4c 45 20 20 20 20 20 20 20 20 32  UTF16LE        2
27a20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
27a30 55 54 46 31 36 42 45 20 20 20 20 20 20 20 20 33  UTF16BE        3
27a40 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
27a50 55 54 46 31 36 20 20 20 20 20 20 20 20 20 20 34  UTF16          4
27a60 20 20 20 20 2f 2a 20 55 73 65 20 6e 61 74 69 76      /* Use nativ
27a70 65 20 62 79 74 65 20 6f 72 64 65 72 20 2a 2f 0a  e byte order */.
27a80 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
27a90 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 35 20  NY            5 
27aa0 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 72     /* sqlite3_cr
27ab0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 6f 6e  eate_function on
27ac0 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ly */.#define SQ
27ad0 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
27ae0 45 44 20 20 38 20 20 20 20 2f 2a 20 73 71 6c 69  ED  8    /* sqli
27af0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
27b00 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a  tion only */../*
27b10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
27b20 70 72 65 63 61 74 65 64 20 46 75 6e 63 74 69 6f  precated Functio
27b30 6e 73 0a 2a 2a 20 44 45 50 52 45 43 41 54 45 44  ns.** DEPRECATED
27b40 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e  .**.** These fun
27b50 63 74 69 6f 6e 73 20 61 72 65 20 5b 64 65 70 72  ctions are [depr
27b60 65 63 61 74 65 64 5d 2e 20 20 49 6e 20 6f 72 64  ecated].  In ord
27b70 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 0a 2a  er to maintain.*
27b80 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  * backwards comp
27b90 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f  atibility with o
27ba0 6c 64 65 72 20 63 6f 64 65 2c 20 74 68 65 73 65  lder code, these
27bb0 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6e 74 69   functions conti
27bc0 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 65 20 73 75  nue .** to be su
27bd0 70 70 6f 72 74 65 64 2e 20 20 48 6f 77 65 76 65  pported.  Howeve
27be0 72 2c 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69  r, new applicati
27bf0 6f 6e 73 20 73 68 6f 75 6c 64 20 61 76 6f 69 64  ons should avoid
27c00 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f 66 20 74  .** the use of t
27c10 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20  hese functions. 
27c20 20 54 6f 20 68 65 6c 70 20 65 6e 63 6f 75 72 61   To help encoura
27c30 67 65 20 70 65 6f 70 6c 65 20 74 6f 20 61 76 6f  ge people to avo
27c40 69 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 73  id.** using thes
27c50 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 65 20  e functions, we 
27c60 61 72 65 20 6e 6f 74 20 67 6f 69 6e 67 20 74 6f  are not going to
27c70 20 74 65 6c 6c 20 79 6f 75 20 77 68 61 74 20 74   tell you what t
27c80 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 69 66 6e 64  hey do..*/.#ifnd
27c90 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
27ca0 45 50 52 45 43 41 54 45 44 0a 53 51 4c 49 54 45  EPRECATED.SQLITE
27cb0 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
27cc0 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74  ECATED int sqlit
27cd0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
27ce0 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
27cf0 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  xt*);.SQLITE_API
27d00 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
27d10 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  ED int sqlite3_e
27d20 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f 73  xpired(sqlite3_s
27d30 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  tmt*);.SQLITE_AP
27d40 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41  I SQLITE_DEPRECA
27d50 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TED int sqlite3_
27d60 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
27d70 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  s(sqlite3_stmt*,
27d80 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b   sqlite3_stmt*);
27d90 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
27da0 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e  TE_DEPRECATED in
27db0 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c  t sqlite3_global
27dc0 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29 3b 0a  _recover(void);.
27dd0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
27de0 45 5f 44 45 50 52 45 43 41 54 45 44 20 76 6f 69  E_DEPRECATED voi
27df0 64 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64  d sqlite3_thread
27e00 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 29 3b 0a  _cleanup(void);.
27e10 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
27e20 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74  E_DEPRECATED int
27e30 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f   sqlite3_memory_
27e40 61 6c 61 72 6d 28 76 6f 69 64 28 2a 29 28 76 6f  alarm(void(*)(vo
27e50 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36  id*,sqlite3_int6
27e60 34 2c 69 6e 74 29 2c 76 6f 69 64 2a 2c 73 71 6c  4,int),void*,sql
27e70 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 23 65 6e  ite3_int64);.#en
27e80 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  dif../*.** CAPI3
27e90 52 45 46 3a 20 4f 62 74 61 69 6e 69 6e 67 20 53  REF: Obtaining S
27ea0 51 4c 20 46 75 6e 63 74 69 6f 6e 20 50 61 72 61  QL Function Para
27eb0 6d 65 74 65 72 20 56 61 6c 75 65 73 20 7b 48 31  meter Values {H1
27ec0 35 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a  5100} <S20200>.*
27ed0 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61 6e 67 75  *.** The C-langu
27ee0 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  age implementati
27ef0 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e 63 74 69  on of SQL functi
27f00 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74  ons and aggregat
27f10 65 73 20 75 73 65 73 0a 2a 2a 20 74 68 69 73 20  es uses.** this 
27f20 73 65 74 20 6f 66 20 69 6e 74 65 72 66 61 63 65  set of interface
27f30 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 61 63 63   routines to acc
27f40 65 73 73 20 74 68 65 20 70 61 72 61 6d 65 74 65  ess the paramete
27f50 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a 2a 20 74  r values on.** t
27f60 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61  he function or a
27f70 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  ggregate..**.** 
27f80 54 68 65 20 78 46 75 6e 63 20 28 66 6f 72 20 73  The xFunc (for s
27f90 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 73 29  calar functions)
27fa0 20 6f 72 20 78 53 74 65 70 20 28 66 6f 72 20 61   or xStep (for a
27fb0 67 67 72 65 67 61 74 65 73 29 20 70 61 72 61 6d  ggregates) param
27fc0 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b 73 71 6c  eters.** to [sql
27fd0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
27fe0 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c  tion()] and [sql
27ff0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
28000 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 64 65 66  tion16()].** def
28010 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73 20 74 68  ine callbacks th
28020 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  at implement the
28030 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61   SQL functions a
28040 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a  nd aggregates..*
28050 2a 20 54 68 65 20 34 74 68 20 70 61 72 61 6d 65  * The 4th parame
28060 74 65 72 20 74 6f 20 74 68 65 73 65 20 63 61 6c  ter to these cal
28070 6c 62 61 63 6b 73 20 69 73 20 61 6e 20 61 72 72  lbacks is an arr
28080 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74  ay of pointers t
28090 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74 65 64 20  o.** [protected 
280a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
280b0 62 6a 65 63 74 73 2e 20 20 54 68 65 72 65 20 69  bjects.  There i
280c0 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 33 5f 76  s one [sqlite3_v
280d0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72  alue] object for
280e0 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 6d 65 74  .** each paramet
280f0 65 72 20 74 6f 20 74 68 65 20 53 51 4c 20 66 75  er to the SQL fu
28100 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20 72  nction.  These r
28110 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
28120 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63 74 20 76   to.** extract v
28130 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 5b  alues from the [
28140 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
28150 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  bjects..**.** Th
28160 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72  ese routines wor
28170 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b 70 72 6f  k only with [pro
28180 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
28190 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a  alue] objects..*
281a0 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f  * Any attempt to
281b0 20 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 69   use these routi
281c0 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e 70 72 6f  nes on an [unpro
281d0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
281e0 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20  alue].** object 
281f0 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66  results in undef
28200 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a  ined behavior..*
28210 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
28220 6e 65 73 20 77 6f 72 6b 20 6a 75 73 74 20 6c 69  nes work just li
28230 6b 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ke the correspon
28240 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e 20 61 63 63  ding [column acc
28250 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 0a 2a  ess functions].*
28260 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 20 74  * except that  t
28270 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 61  hese routines ta
28280 6b 65 20 61 20 73 69 6e 67 6c 65 20 5b 70 72 6f  ke a single [pro
28290 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
282a0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20  alue] object.** 
282b0 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 20  pointer instead 
282c0 6f 66 20 61 20 5b 73 71 6c 69 74 65 33 5f 73 74  of a [sqlite3_st
282d0 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 20 61 6e 64  mt*] pointer and
282e0 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6c 75   an integer colu
282f0 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a  mn number..**.**
28300 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c   The sqlite3_val
28310 75 65 5f 74 65 78 74 31 36 28 29 20 69 6e 74 65  ue_text16() inte
28320 72 66 61 63 65 20 65 78 74 72 61 63 74 73 20 61  rface extracts a
28330 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a   UTF-16 string.*
28340 2a 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20  * in the native 
28350 62 79 74 65 2d 6f 72 64 65 72 20 6f 66 20 74 68  byte-order of th
28360 65 20 68 6f 73 74 20 6d 61 63 68 69 6e 65 2e 20  e host machine. 
28370 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   The.** sqlite3_
28380 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 29  value_text16be()
28390 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 61 6c   and sqlite3_val
283a0 75 65 5f 74 65 78 74 31 36 6c 65 28 29 20 69 6e  ue_text16le() in
283b0 74 65 72 66 61 63 65 73 0a 2a 2a 20 65 78 74 72  terfaces.** extr
283c0 61 63 74 20 55 54 46 2d 31 36 20 73 74 72 69 6e  act UTF-16 strin
283d0 67 73 20 61 73 20 62 69 67 2d 65 6e 64 69 61 6e  gs as big-endian
283e0 20 61 6e 64 20 6c 69 74 74 6c 65 2d 65 6e 64 69   and little-endi
283f0 61 6e 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  an respectively.
28400 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
28410 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63  e3_value_numeric
28420 5f 74 79 70 65 28 29 20 69 6e 74 65 72 66 61 63  _type() interfac
28430 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 61 70  e attempts to ap
28440 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 69 63 20 61  ply.** numeric a
28450 66 66 69 6e 69 74 79 20 74 6f 20 74 68 65 20 76  ffinity to the v
28460 61 6c 75 65 2e 20 20 54 68 69 73 20 6d 65 61 6e  alue.  This mean
28470 73 20 74 68 61 74 20 61 6e 20 61 74 74 65 6d 70  s that an attemp
28480 74 20 69 73 0a 2a 2a 20 6d 61 64 65 20 74 6f 20  t is.** made to 
28490 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c 75  convert the valu
284a0 65 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  e to an integer 
284b0 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  or floating poin
284c0 74 2e 20 20 49 66 0a 2a 2a 20 73 75 63 68 20 61  t.  If.** such a
284d0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70   conversion is p
284e0 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 20  ossible without 
284f0 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74  loss of informat
28500 69 6f 6e 20 28 69 6e 20 6f 74 68 65 72 0a 2a 2a  ion (in other.**
28510 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 76   words, if the v
28520 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67  alue is a string
28530 20 74 68 61 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65   that looks like
28540 20 61 20 6e 75 6d 62 65 72 29 0a 2a 2a 20 74 68   a number).** th
28550 65 6e 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  en the conversio
28560 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20  n is performed. 
28570 20 4f 74 68 65 72 77 69 73 65 20 6e 6f 20 63 6f   Otherwise no co
28580 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 2e  nversion occurs.
28590 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
285a0 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61 74 79  INTEGER | dataty
285b0 70 65 5d 20 61 66 74 65 72 20 63 6f 6e 76 65 72  pe] after conver
285c0 73 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64  sion is returned
285d0 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73 65 20 70  ..**.** Please p
285e0 61 79 20 70 61 72 74 69 63 75 6c 61 72 20 61 74  ay particular at
285f0 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68 65 20 66  tention to the f
28600 61 63 74 20 74 68 61 74 20 74 68 65 20 70 6f 69  act that the poi
28610 6e 74 65 72 20 72 65 74 75 72 6e 65 64 0a 2a 2a  nter returned.**
28620 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 76   from [sqlite3_v
28630 61 6c 75 65 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73  alue_blob()], [s
28640 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
28650 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c  t()], or.** [sql
28660 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
28670 36 28 29 5d 20 63 61 6e 20 62 65 20 69 6e 76 61  6()] can be inva
28680 6c 69 64 61 74 65 64 20 62 79 20 61 20 73 75 62  lidated by a sub
28690 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a  sequent call to.
286a0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ** [sqlite3_valu
286b0 65 5f 62 79 74 65 73 28 29 5d 2c 20 5b 73 71 6c  e_bytes()], [sql
286c0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
286d0 31 36 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  16()], [sqlite3_
286e0 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a  value_text()],.*
286f0 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61  * or [sqlite3_va
28700 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 2e 0a 2a  lue_text16()]..*
28710 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
28720 6e 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  nes must be call
28730 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65  ed from the same
28740 20 74 68 72 65 61 64 20 61 73 0a 2a 2a 20 74 68   thread as.** th
28750 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74  e SQL function t
28760 68 61 74 20 73 75 70 70 6c 69 65 64 20 74 68 65  hat supplied the
28770 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a   [sqlite3_value*
28780 5d 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a  ] parameters..**
28790 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
287a0 3a 0a 2a 2a 20 5b 48 31 35 31 30 33 5d 20 5b 48  :.** [H15103] [H
287b0 31 35 31 30 36 5d 20 5b 48 31 35 31 30 39 5d 20  15106] [H15109] 
287c0 5b 48 31 35 31 31 32 5d 20 5b 48 31 35 31 31 35  [H15112] [H15115
287d0 5d 20 5b 48 31 35 31 31 38 5d 20 5b 48 31 35 31  ] [H15118] [H151
287e0 32 31 5d 20 5b 48 31 35 31 32 34 5d 0a 2a 2a 20  21] [H15124].** 
287f0 5b 48 31 35 31 32 37 5d 20 5b 48 31 35 31 33 30  [H15127] [H15130
28800 5d 20 5b 48 31 35 31 33 33 5d 20 5b 48 31 35 31  ] [H15133] [H151
28810 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  36].*/.SQLITE_AP
28820 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
28830 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
28840 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
28850 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
28860 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
28870 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c  ytes(sqlite3_val
28880 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
28890 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
288a0 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  ue_bytes16(sqlit
288b0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
288c0 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71  TE_API double sq
288d0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
288e0 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
288f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
28900 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
28910 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c  _int(sqlite3_val
28920 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
28930 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
28940 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
28950 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  64(sqlite3_value
28960 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
28970 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
28980 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar *sqlite3_valu
28990 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76  e_text(sqlite3_v
289a0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
289b0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
289c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
289d0 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  t16(sqlite3_valu
289e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
289f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
28a00 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
28a10 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
28a20 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
28a30 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
28a40 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62  e3_value_text16b
28a50 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  e(sqlite3_value*
28a60 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
28a70 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
28a80 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  type(sqlite3_val
28a90 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
28aa0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
28ab0 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
28ac0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28ad0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
28ae0 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67 61  : Obtain Aggrega
28af0 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74  te Function Cont
28b00 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53 32  ext {H16210} <S2
28b10 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0200>.**.** The 
28b20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
28b30 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20  f aggregate SQL 
28b40 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68  functions use th
28b50 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61 6c  is routine to al
28b60 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72 75  locate.** a stru
28b70 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69 6e  cture for storin
28b80 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a 2a  g their state..*
28b90 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
28ba0 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ime the sqlite3_
28bb0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
28bc0 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63  t() routine is c
28bd0 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20 70  alled for a.** p
28be0 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 67  articular aggreg
28bf0 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c 6f  ate, SQLite allo
28c00 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66 20  cates nBytes of 
28c10 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20 6f  memory, zeroes o
28c20 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f 72  ut that.** memor
28c30 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  y, and returns a
28c40 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
28c50 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75  On second and su
28c60 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
28c70 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67 67  o.** sqlite3_agg
28c80 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29  regate_context()
28c90 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61 67   for the same ag
28ca0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
28cb0 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20 73   index,.** the s
28cc0 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 65  ame buffer is re
28cd0 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70 6c  turned. The impl
28ce0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
28cf0 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e 20  e aggregate can 
28d00 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72  use.** the retur
28d10 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61 63  ned buffer to ac
28d20 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a 2a  cumulate data..*
28d30 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74 6f  *.** SQLite auto
28d40 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73 20  matically frees 
28d50 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75  the allocated bu
28d60 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61 67  ffer when the ag
28d70 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72 79  gregate.** query
28d80 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a   concludes..**.*
28d90 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
28da0 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20  meter should be 
28db0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a  a copy of the.**
28dc0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   [sqlite3_contex
28dd0 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  t | SQL function
28de0 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 69   context] that i
28df0 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61  s the first para
28e00 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  meter.** to the 
28e10 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
28e20 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
28e30 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 66   the aggregate f
28e40 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  unction..**.** T
28e50 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
28e60 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
28e70 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
28e80 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  in which.** the 
28e90 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 75  aggregate SQL fu
28ea0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
28eb0 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  g..**.** Require
28ec0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 31  ments:.** [H1621
28ed0 31 5d 20 5b 48 31 36 32 31 33 5d 20 5b 48 31 36  1] [H16213] [H16
28ee0 32 31 35 5d 20 5b 48 31 36 32 31 37 5d 0a 2a 2f  215] [H16217].*/
28ef0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
28f00 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67   *sqlite3_aggreg
28f10 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69  ate_context(sqli
28f20 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e  te3_context*, in
28f30 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a 2a  t nBytes);../*.*
28f40 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65 72  * CAPI3REF: User
28f50 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74 69   Data For Functi
28f60 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c 53 32  ons {H16240} <S2
28f70 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0200>.**.** The 
28f80 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
28f90 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  a() interface re
28fa0 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 0a  turns a copy of.
28fb0 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ** the pointer t
28fc0 68 61 74 20 77 61 73 20 74 68 65 20 70 55 73 65  hat was the pUse
28fd0 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 72 20  rData parameter 
28fe0 28 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 74  (the 5th paramet
28ff0 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73  er).** of the [s
29000 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
29010 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64  nction()].** and
29020 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
29030 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72  _function16()] r
29040 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f 72 69  outines that ori
29050 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73  ginally.** regis
29060 74 65 72 65 64 20 74 68 65 20 61 70 70 6c 69 63  tered the applic
29070 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 75  ation defined fu
29080 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a 2a  nction. {END}.**
29090 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
290a0 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
290b0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68  from the same th
290c0 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a  read in which.**
290d0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
290e0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
290f0 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a  n is running..**
29100 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
29110 3a 0a 2a 2a 20 5b 48 31 36 32 34 33 5d 0a 2a 2f  :.** [H16243].*/
29120 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
29130 20 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64   *sqlite3_user_d
29140 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ata(sqlite3_cont
29150 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ext*);../*.** CA
29160 50 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 65  PI3REF: Database
29170 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 46 6f 72 20   Connection For 
29180 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 32 35  Functions {H1625
29190 30 7d 20 3c 53 36 30 36 30 30 3e 3c 53 32 30 32  0} <S60600><S202
291a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
291b0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
291c0 5f 68 61 6e 64 6c 65 28 29 20 69 6e 74 65 72 66  _handle() interf
291d0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f  ace returns a co
291e0 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 70 6f 69  py of.** the poi
291f0 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 64 61 74  nter to the [dat
29200 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
29210 5d 20 28 74 68 65 20 31 73 74 20 70 61 72 61 6d  ] (the 1st param
29220 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20  eter).** of the 
29230 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
29240 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61  function()].** a
29250 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  nd [sqlite3_crea
29260 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d  te_function16()]
29270 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f   routines that o
29280 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67  riginally.** reg
29290 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c  istered the appl
292a0 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  ication defined 
292b0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
292c0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
292d0 20 5b 48 31 36 32 35 33 5d 0a 2a 2f 0a 53 51 4c   [H16253].*/.SQL
292e0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20  ITE_API sqlite3 
292f0 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  *sqlite3_context
29300 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74  _db_handle(sqlit
29310 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f  e3_context*);../
29320 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46  *.** CAPI3REF: F
29330 75 6e 63 74 69 6f 6e 20 41 75 78 69 6c 69 61 72  unction Auxiliar
29340 79 20 44 61 74 61 20 7b 48 31 36 32 37 30 7d 20  y Data {H16270} 
29350 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
29360 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f  he following two
29370 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62   functions may b
29380 65 20 75 73 65 64 20 62 79 20 73 63 61 6c 61 72  e used by scalar
29390 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 74   SQL functions t
293a0 6f 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 20 6d  o.** associate m
293b0 65 74 61 64 61 74 61 20 77 69 74 68 20 61 72 67  etadata with arg
293c0 75 6d 65 6e 74 20 76 61 6c 75 65 73 2e 20 49 66  ument values. If
293d0 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20   the same value 
293e0 69 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20  is passed to.** 
293f0 6d 75 6c 74 69 70 6c 65 20 69 6e 76 6f 63 61 74  multiple invocat
29400 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
29410 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 64 75   SQL function du
29420 72 69 6e 67 20 71 75 65 72 79 20 65 78 65 63 75  ring query execu
29430 74 69 6f 6e 2c 20 75 6e 64 65 72 0a 2a 2a 20 73  tion, under.** s
29440 6f 6d 65 20 63 69 72 63 75 6d 73 74 61 6e 63 65  ome circumstance
29450 73 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  s the associated
29460 20 6d 65 74 61 64 61 74 61 20 6d 61 79 20 62 65   metadata may be
29470 20 70 72 65 73 65 72 76 65 64 2e 20 54 68 69 73   preserved. This
29480 20 6d 61 79 0a 2a 2a 20 62 65 20 75 73 65 64 2c   may.** be used,
29490 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f   for example, to
294a0 20 61 64 64 20 61 20 72 65 67 75 6c 61 72 2d 65   add a regular-e
294b0 78 70 72 65 73 73 69 6f 6e 20 6d 61 74 63 68 69  xpression matchi
294c0 6e 67 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75 6e  ng scalar.** fun
294d0 63 74 69 6f 6e 2e 20 54 68 65 20 63 6f 6d 70 69  ction. The compi
294e0 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  led version of t
294f0 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65  he regular expre
29500 73 73 69 6f 6e 20 69 73 20 73 74 6f 72 65 64 20  ssion is stored 
29510 61 73 0a 2a 2a 20 6d 65 74 61 64 61 74 61 20 61  as.** metadata a
29520 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
29530 68 65 20 53 51 4c 20 76 61 6c 75 65 20 70 61 73  he SQL value pas
29540 73 65 64 20 61 73 20 74 68 65 20 72 65 67 75 6c  sed as the regul
29550 61 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  ar expression.**
29560 20 70 61 74 74 65 72 6e 2e 20 20 54 68 65 20 63   pattern.  The c
29570 6f 6d 70 69 6c 65 64 20 72 65 67 75 6c 61 72 20  ompiled regular 
29580 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 20 62  expression can b
29590 65 20 72 65 75 73 65 64 20 6f 6e 20 6d 75 6c 74  e reused on mult
295a0 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  iple.** invocati
295b0 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ons of the same 
295c0 66 75 6e 63 74 69 6f 6e 20 73 6f 20 74 68 61 74  function so that
295d0 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61   the original pa
295e0 74 74 65 72 6e 20 73 74 72 69 6e 67 0a 2a 2a 20  ttern string.** 
295f0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
29600 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 6f   be recompiled o
29610 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f  n each invocatio
29620 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  n..**.** The sql
29630 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
29640 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
29650 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
29660 6f 20 74 68 65 20 6d 65 74 61 64 61 74 61 0a 2a  o the metadata.*
29670 2a 20 61 73 73 6f 63 69 61 74 65 64 20 62 79 20  * associated by 
29680 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  the sqlite3_set_
29690 61 75 78 64 61 74 61 28 29 20 66 75 6e 63 74 69  auxdata() functi
296a0 6f 6e 20 77 69 74 68 20 74 68 65 20 4e 74 68 20  on with the Nth 
296b0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 76 61 6c 75  argument.** valu
296c0 65 20 74 6f 20 74 68 65 20 61 70 70 6c 69 63 61  e to the applica
296d0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
296e0 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 6d 65 74  ction. If no met
296f0 61 64 61 74 61 20 68 61 73 20 62 65 65 6e 20 65  adata has been e
29700 76 65 72 0a 2a 2a 20 62 65 65 6e 20 73 65 74 20  ver.** been set 
29710 66 6f 72 20 74 68 65 20 4e 74 68 20 61 72 67 75  for the Nth argu
29720 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 75 6e 63  ment of the func
29730 74 69 6f 6e 2c 20 6f 72 20 69 66 20 74 68 65 20  tion, or if the 
29740 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
29750 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65   function parame
29760 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20  ter has changed 
29770 73 69 6e 63 65 20 74 68 65 20 6d 65 74 61 2d 64  since the meta-d
29780 61 74 61 20 77 61 73 20 73 65 74 2c 0a 2a 2a 20  ata was set,.** 
29790 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 67 65 74  then sqlite3_get
297a0 5f 61 75 78 64 61 74 61 28 29 20 72 65 74 75 72  _auxdata() retur
297b0 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ns a NULL pointe
297c0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  r..**.** The sql
297d0 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
297e0 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 61 76  () interface sav
297f0 65 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 0a  es the metadata.
29800 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ** pointed to by
29810 20 69 74 73 20 33 72 64 20 70 61 72 61 6d 65 74   its 3rd paramet
29820 65 72 20 61 73 20 74 68 65 20 6d 65 74 61 64 61  er as the metada
29830 74 61 20 66 6f 72 20 74 68 65 20 4e 2d 74 68 0a  ta for the N-th.
29840 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74  ** argument of t
29850 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
29860 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e  efined function.
29870 20 20 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20    Subsequent.** 
29880 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
29890 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 6d  _get_auxdata() m
298a0 69 67 68 74 20 72 65 74 75 72 6e 20 74 68 69 73  ight return this
298b0 20 64 61 74 61 2c 20 69 66 20 69 74 20 68 61 73   data, if it has
298c0 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20 64 65 73  .** not been des
298d0 74 72 6f 79 65 64 2e 0a 2a 2a 20 49 66 20 69 74  troyed..** If it
298e0 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51   is not NULL, SQ
298f0 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65  Lite will invoke
29900 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a   the destructor.
29910 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65  ** function give
29920 6e 20 62 79 20 74 68 65 20 34 74 68 20 70 61 72  n by the 4th par
29930 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
29940 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20  3_set_auxdata() 
29950 6f 6e 0a 2a 2a 20 74 68 65 20 6d 65 74 61 64 61  on.** the metada
29960 74 61 20 77 68 65 6e 20 74 68 65 20 63 6f 72 72  ta when the corr
29970 65 73 70 6f 6e 64 69 6e 67 20 66 75 6e 63 74 69  esponding functi
29980 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 63 68 61  on parameter cha
29990 6e 67 65 73 0a 2a 2a 20 6f 72 20 77 68 65 6e 20  nges.** or when 
299a0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
299b0 74 20 63 6f 6d 70 6c 65 74 65 73 2c 20 77 68 69  t completes, whi
299c0 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72  chever comes fir
299d0 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  st..**.** SQLite
299e0 20 69 73 20 66 72 65 65 20 74 6f 20 63 61 6c 6c   is free to call
299f0 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
29a00 61 6e 64 20 64 72 6f 70 20 6d 65 74 61 64 61 74  and drop metadat
29a10 61 20 6f 6e 20 61 6e 79 0a 2a 2a 20 70 61 72 61  a on any.** para
29a20 6d 65 74 65 72 20 6f 66 20 61 6e 79 20 66 75 6e  meter of any fun
29a30 63 74 69 6f 6e 20 61 74 20 61 6e 79 20 74 69 6d  ction at any tim
29a40 65 2e 20 20 54 68 65 20 6f 6e 6c 79 20 67 75 61  e.  The only gua
29a50 72 61 6e 74 65 65 20 69 73 20 74 68 61 74 0a 2a  rantee is that.*
29a60 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  * the destructor
29a70 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
29a80 62 65 66 6f 72 65 20 74 68 65 20 6d 65 74 61 64  before the metad
29a90 61 74 61 20 69 73 20 64 72 6f 70 70 65 64 2e 0a  ata is dropped..
29aa0 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63 74 69 63  **.** In practic
29ab0 65 2c 20 6d 65 74 61 64 61 74 61 20 69 73 20 70  e, metadata is p
29ac0 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65 6e  reserved between
29ad0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20   function calls 
29ae0 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  for.** expressio
29af0 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 73  ns that are cons
29b00 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20  tant at compile 
29b10 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63 6c 75  time. This inclu
29b20 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a 2a 20 76  des literal.** v
29b30 61 6c 75 65 73 20 61 6e 64 20 53 51 4c 20 76 61  alues and SQL va
29b40 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  riables..**.** T
29b50 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75  hese routines mu
29b60 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f  st be called fro
29b70 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  m the same threa
29b80 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68  d in which.** th
29b90 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69  e SQL function i
29ba0 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a  s running..**.**
29bb0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
29bc0 2a 20 5b 48 31 36 32 37 32 5d 20 5b 48 31 36 32  * [H16272] [H162
29bd0 37 34 5d 20 5b 48 31 36 32 37 36 5d 20 5b 48 31  74] [H16276] [H1
29be0 36 32 37 37 5d 20 5b 48 31 36 32 37 38 5d 20 5b  6277] [H16278] [
29bf0 48 31 36 32 37 39 5d 0a 2a 2f 0a 53 51 4c 49 54  H16279].*/.SQLIT
29c00 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
29c10 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
29c20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
29c30 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45  , int N);.SQLITE
29c40 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
29c50 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 71  3_set_auxdata(sq
29c60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
29c70 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f  int N, void*, vo
29c80 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  id (*)(void*));.
29c90 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
29ca0 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69  : Constants Defi
29cb0 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73  ning Special Des
29cc0 74 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72  tructor Behavior
29cd0 20 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 30   {H10280} <S3010
29ce0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61  0>.**.** These a
29cf0 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65  re special value
29d00 73 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 75  s for the destru
29d10 63 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 73  ctor that is pas
29d20 73 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a  sed in as the.**
29d30 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20   final argument 
29d40 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65  to routines like
29d50 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
29d60 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 68  _blob()].  If th
29d70 65 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20  e destructor.** 
29d80 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49  argument is SQLI
29d90 54 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d 65  TE_STATIC, it me
29da0 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e  ans that the con
29db0 74 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20  tent pointer is 
29dc0 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20  constant.** and 
29dd0 77 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e 67  will never chang
29de0 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  e.  It does not 
29df0 6e 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 72  need to be destr
29e00 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 51  oyed.  The.** SQ
29e10 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76  LITE_TRANSIENT v
29e20 61 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20  alue means that 
29e30 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c  the content will
29e40 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69   likely change i
29e50 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75  n.** the near fu
29e60 74 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 51  ture and that SQ
29e70 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65  Lite should make
29e80 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65   its own private
29e90 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20   copy of.** the 
29ea0 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72  content before r
29eb0 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  eturning..**.** 
29ec0 54 68 65 20 74 79 70 65 64 65 66 20 69 73 20 6e  The typedef is n
29ed0 65 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b  ecessary to work
29ee0 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73   around problems
29ef0 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43   in certain.** C
29f00 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53  ++ compilers.  S
29f10 65 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e  ee ticket #2191.
29f20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64  .*/.typedef void
29f30 20 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72   (*sqlite3_destr
29f40 75 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64  uctor_type)(void
29f50 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  *);.#define SQLI
29f60 54 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28  TE_STATIC      (
29f70 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63  (sqlite3_destruc
29f80 74 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66  tor_type)0).#def
29f90 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ine SQLITE_TRANS
29fa0 49 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33  IENT   ((sqlite3
29fb0 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65  _destructor_type
29fc0 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  )-1)../*.** CAPI
29fd0 33 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68  3REF: Setting Th
29fe0 65 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53  e Result Of An S
29ff0 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 36  QL Function {H16
2a000 34 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a  400} <S20200>.**
2a010 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
2a020 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74  es are used by t
2a030 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 6e  he xFunc or xFin
2a040 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61  al callbacks tha
2a050 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 53  t.** implement S
2a060 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64  QL functions and
2a070 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 65   aggregates.  Se
2a080 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72  e.** [sqlite3_cr
2a090 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d  eate_function()]
2a0a0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72   and [sqlite3_cr
2a0b0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
2a0c0 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69  )].** for additi
2a0d0 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
2a0e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75  ..**.** These fu
2a0f0 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 72  nctions work ver
2a100 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20  y much like the 
2a110 5b 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69  [parameter bindi
2a120 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a  ng] family of.**
2a130 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20   functions used 
2a140 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 74  to bind values t
2a150 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  o host parameter
2a160 73 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 74  s in prepared st
2a170 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 66  atements..** Ref
2a180 65 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 70  er to the [SQL p
2a190 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d 65  arameter] docume
2a1a0 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69  ntation for addi
2a1b0 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
2a1c0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  on..**.** The sq
2a1d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
2a1e0 62 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  b() interface se
2a1f0 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72  ts the result fr
2a200 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61  om.** an applica
2a210 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
2a220 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20  ction to be the 
2a230 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65  BLOB whose conte
2a240 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a 2a  nt is pointed.**
2a250 20 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e   to by the secon
2a260 64 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20  d parameter and 
2a270 77 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73  which is N bytes
2a280 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73   long where N is
2a290 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 61   the.** third pa
2a2a0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  rameter..**.** T
2a2b0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2a2c0 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 74  t_zeroblob() int
2a2d0 65 72 66 61 63 65 73 20 73 65 74 20 74 68 65 20  erfaces set the 
2a2e0 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65  result of.** the
2a2f0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
2a300 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ined function to
2a310 20 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61   be a BLOB conta
2a320 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a  ining all zero.*
2a330 2a 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79  * bytes and N by
2a340 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65  tes in size, whe
2a350 72 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75  re N is the valu
2a360 65 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72  e of the 2nd par
2a370 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ameter..**.** Th
2a380 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2a390 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66  _double() interf
2a3a0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73  ace sets the res
2a3b0 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61  ult from.** an a
2a3c0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2a3d0 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
2a3e0 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  e a floating poi
2a3f0 6e 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 69  nt value specifi
2a400 65 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64  ed.** by its 2nd
2a410 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
2a420 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2a430 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20  ult_error() and 
2a440 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2a450 72 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f  rror16() functio
2a460 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20  ns.** cause the 
2a470 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20  implemented SQL 
2a480 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f  function to thro
2a490 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a  w an exception..
2a4a0 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74  ** SQLite uses t
2a4b0 68 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65  he string pointe
2a4c0 64 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32  d to by the.** 2
2a4d0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  nd parameter of 
2a4e0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2a4f0 72 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65  rror() or sqlite
2a500 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
2a510 28 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78  ().** as the tex
2a520 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65  t of an error me
2a530 73 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69  ssage.  SQLite i
2a540 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 65 72  nterprets the er
2a550 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73  ror.** message s
2a560 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74  tring from sqlit
2a570 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2a580 29 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c 69  ) as UTF-8. SQLi
2a590 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 73  te.** interprets
2a5a0 20 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f 6d   the string from
2a5b0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2a5c0 65 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 46  error16() as UTF
2a5d0 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a 2a  -16 in native.**
2a5e0 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 66   byte order.  If
2a5f0 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   the third param
2a600 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
2a610 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a  result_error().*
2a620 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73  * or sqlite3_res
2a630 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73  ult_error16() is
2a640 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53   negative then S
2a650 51 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 74  QLite takes as t
2a660 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73  he error.** mess
2a670 61 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 20  age all text up 
2a680 74 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73  through the firs
2a690 74 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72  t zero character
2a6a0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 72  ..** If the thir
2a6b0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  d parameter to s
2a6c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2a6d0 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69  ror() or.** sqli
2a6e0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2a6f0 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61  16() is non-nega
2a700 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65  tive then SQLite
2a710 20 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e 79   takes that many
2a720 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63  .** bytes (not c
2a730 68 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20  haracters) from 
2a740 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2a750 72 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d  r as the error m
2a760 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 73  essage..** The s
2a770 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2a780 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ror() and sqlite
2a790 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
2a7a0 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6d  ().** routines m
2a7b0 61 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f  ake a private co
2a7c0 70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  py of the error 
2a7d0 6d 65 73 73 61 67 65 20 74 65 78 74 20 62 65 66  message text bef
2a7e0 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 75  ore.** they retu
2a7f0 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20  rn.  Hence, the 
2a800 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
2a810 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20   can deallocate 
2a820 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 65  or.** modify the
2a830 20 74 65 78 74 20 61 66 74 65 72 20 74 68 65 79   text after they
2a840 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20   return without 
2a850 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  harm..** The sql
2a860 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2a870 72 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f  r_code() functio
2a880 6e 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 72  n changes the er
2a890 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75  ror code.** retu
2a8a0 72 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 61  rned by SQLite a
2a8b0 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e  s a result of an
2a8c0 20 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e 63   error in a func
2a8d0 74 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 6c  tion.  By defaul
2a8e0 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20  t,.** the error 
2a8f0 63 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45  code is SQLITE_E
2a900 52 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 75  RROR.  A subsequ
2a910 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ent call to sqli
2a920 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2a930 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  ().** or sqlite3
2a940 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
2a950 29 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72  ) resets the err
2a960 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54  or code to SQLIT
2a970 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54  E_ERROR..**.** T
2a980 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2a990 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 72  t_toobig() inter
2a9a0 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c 69  face causes SQLi
2a9b0 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65  te to throw an e
2a9c0 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69  rror.** indicati
2a9d0 6e 67 20 74 68 61 74 20 61 20 73 74 72 69 6e 67  ng that a string
2a9e0 20 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 6c   or BLOB is to l
2a9f0 6f 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74  ong to represent
2aa00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2aa10 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d  te3_result_nomem
2aa20 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75  () interface cau
2aa30 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68  ses SQLite to th
2aa40 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20  row an error.** 
2aa50 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
2aa60 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
2aa70 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a  ion failed..**.*
2aa80 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2aa90 73 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72  sult_int() inter
2aaa0 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
2aab0 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66  turn value.** of
2aac0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2aad0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2aae0 6e 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62  n to be the 32-b
2aaf0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
2ab00 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e  r.** value given
2ab10 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75   in the 2nd argu
2ab20 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  ment..** The sql
2ab30 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
2ab40 34 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  4() interface se
2ab50 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ts the return va
2ab60 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70  lue.** of the ap
2ab70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2ab80 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
2ab90 20 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e   the 64-bit sign
2aba0 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61  ed integer.** va
2abb0 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65  lue given in the
2abc0 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   2nd argument..*
2abd0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2abe0 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69  _result_null() i
2abf0 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68  nterface sets th
2ac00 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
2ac10 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61  * of the applica
2ac20 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
2ac30 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c  ction to be NULL
2ac40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2ac50 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
2ac60 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ), sqlite3_resul
2ac70 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 73  t_text16(),.** s
2ac80 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2ac90 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 71  xt16le(), and sq
2aca0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2acb0 74 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 63  t16be() interfac
2acc0 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 65  es.** set the re
2acd0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
2ace0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
2acf0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2ad00 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 73  o be.** a text s
2ad10 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 72  tring which is r
2ad20 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 54  epresented as UT
2ad30 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 69  F-8, UTF-16 nati
2ad40 76 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a 2a  ve byte order,.*
2ad50 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20  * UTF-16 little 
2ad60 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31  endian, or UTF-1
2ad70 36 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 65  6 big endian, re
2ad80 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 53  spectively..** S
2ad90 51 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 20  QLite takes the 
2ada0 74 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f 6d  text result from
2adb0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2adc0 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e 64   from.** the 2nd
2add0 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68   parameter of th
2ade0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2adf0 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65  _text* interface
2ae00 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64  s..** If the 3rd
2ae10 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
2ae20 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2ae30 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65  _text* interface
2ae40 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 65  s.** is negative
2ae50 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61  , then SQLite ta
2ae60 6b 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 20  kes result text 
2ae70 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72  from the 2nd par
2ae80 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 67  ameter.** throug
2ae90 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  h the first zero
2aea0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49   character..** I
2aeb0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  f the 3rd parame
2aec0 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
2aed0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
2aee0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73  interfaces.** is
2aef0 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74   non-negative, t
2af00 68 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 65  hen as many byte
2af10 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 72  s (not character
2af20 73 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a 2a  s) of the text.*
2af30 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  * pointed to by 
2af40 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2af50 72 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 74  r are taken as t
2af60 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
2af70 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 69  efined.** functi
2af80 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66  on result..** If
2af90 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74   the 4th paramet
2afa0 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  er to the sqlite
2afb0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69  3_result_text* i
2afc0 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20  nterfaces.** or 
2afd0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
2afe0 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c  lob is a non-NUL
2aff0 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20  L pointer, then 
2b000 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 61  SQLite calls tha
2b010 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73  t.** function as
2b020 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
2b030 6f 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 42  on the text or B
2b040 4c 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e 20  LOB result when 
2b050 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68  it has.** finish
2b060 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65  ed using that re
2b070 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  sult..** If the 
2b080 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  4th parameter to
2b090 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
2b0a0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
2b0b0 61 63 65 73 20 6f 72 0a 2a 2a 20 73 71 6c 69 74  aces or.** sqlit
2b0c0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69  e3_result_blob i
2b0d0 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f  s the special co
2b0e0 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54  nstant SQLITE_ST
2b0f0 41 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74  ATIC, then SQLit
2b100 65 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61  e.** assumes tha
2b110 74 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c  t the text or BL
2b120 4f 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20  OB result is in 
2b130 63 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61  constant space a
2b140 6e 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63  nd does not.** c
2b150 6f 70 79 20 74 68 65 20 69 74 20 6f 72 20 63 61  opy the it or ca
2b160 6c 6c 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  ll a destructor 
2b170 77 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69  when it has fini
2b180 73 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20  shed using that 
2b190 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68  result..** If th
2b1a0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 4th parameter 
2b1b0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  to the sqlite3_r
2b1c0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65  esult_text* inte
2b1d0 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c  rfaces.** or sql
2b1e0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
2b1f0 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20   is the special 
2b200 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f  constant SQLITE_
2b210 54 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65  TRANSIENT.** the
2b220 6e 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61  n SQLite makes a
2b230 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73   copy of the res
2b240 75 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f  ult into space o
2b250 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
2b260 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61  from [sqlite3_ma
2b270 6c 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69  lloc()] before i
2b280 74 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a  t returns..**.**
2b290 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2b2a0 75 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65  ult_value() inte
2b2b0 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72  rface sets the r
2b2c0 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20  esult of.** the 
2b2d0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
2b2e0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ned function to 
2b2f0 62 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a  be a copy the.**
2b300 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71   [unprotected sq
2b310 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
2b320 65 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79  ect specified by
2b330 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
2b340 65 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69  er.  The.** sqli
2b350 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
2b360 28 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b  () interface mak
2b370 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  es a copy of the
2b380 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
2b390 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20  .** so that the 
2b3a0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20  [sqlite3_value] 
2b3b0 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
2b3c0 20 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63   parameter may c
2b3d0 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64  hange or.** be d
2b3e0 65 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72  eallocated after
2b3f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2b400 76 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20  value() returns 
2b410 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a  without harm..**
2b420 20 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71   A [protected sq
2b430 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
2b440 65 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62  ect may always b
2b450 65 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a  e used where an.
2b460 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20  ** [unprotected 
2b470 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
2b480 62 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65  bject is require
2b490 64 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20  d, so either.** 
2b4a0 6b 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33  kind of [sqlite3
2b4b0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63  _value] object c
2b4c0 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20  an be used with 
2b4d0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a  this interface..
2b4e0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72  **.** If these r
2b4f0 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c  outines are call
2b500 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  ed from within t
2b510 68 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72  he different thr
2b520 65 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20  ead.** than the 
2b530 6f 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  one containing t
2b540 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
2b550 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
2b560 74 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a  that received.**
2b570 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f   the [sqlite3_co
2b580 6e 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20  ntext] pointer, 
2b590 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  the results are 
2b5a0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
2b5b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
2b5c0 2a 20 5b 48 31 36 34 30 33 5d 20 5b 48 31 36 34  * [H16403] [H164
2b5d0 30 36 5d 20 5b 48 31 36 34 30 39 5d 20 5b 48 31  06] [H16409] [H1
2b5e0 36 34 31 32 5d 20 5b 48 31 36 34 31 35 5d 20 5b  6412] [H16415] [
2b5f0 48 31 36 34 31 38 5d 20 5b 48 31 36 34 32 31 5d  H16418] [H16421]
2b600 20 5b 48 31 36 34 32 34 5d 0a 2a 2a 20 5b 48 31   [H16424].** [H1
2b610 36 34 32 37 5d 20 5b 48 31 36 34 33 30 5d 20 5b  6427] [H16430] [
2b620 48 31 36 34 33 33 5d 20 5b 48 31 36 34 33 36 5d  H16433] [H16436]
2b630 20 5b 48 31 36 34 33 39 5d 20 5b 48 31 36 34 34   [H16439] [H1644
2b640 32 5d 20 5b 48 31 36 34 34 35 5d 20 5b 48 31 36  2] [H16445] [H16
2b650 34 34 38 5d 0a 2a 2a 20 5b 48 31 36 34 35 31 5d  448].** [H16451]
2b660 20 5b 48 31 36 34 35 34 5d 20 5b 48 31 36 34 35   [H16454] [H1645
2b670 37 5d 20 5b 48 31 36 34 36 30 5d 20 5b 48 31 36  7] [H16460] [H16
2b680 34 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  463].*/.SQLITE_A
2b690 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2b6a0 72 65 73 75 6c 74 5f 62 6c 6f 62 28 73 71 6c 69  result_blob(sqli
2b6b0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
2b6c0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  nst void*, int, 
2b6d0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
2b6e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2b6f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2b700 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63  double(sqlite3_c
2b710 6f 6e 74 65 78 74 2a 2c 20 64 6f 75 62 6c 65 29  ontext*, double)
2b720 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
2b730 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
2b740 5f 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63  _error(sqlite3_c
2b750 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63  ontext*, const c
2b760 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  har*, int);.SQLI
2b770 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2b780 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2b790 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  16(sqlite3_conte
2b7a0 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  xt*, const void*
2b7b0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  , int);.SQLITE_A
2b7c0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2b7d0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
2b7e0 62 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  big(sqlite3_cont
2b7f0 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ext*);.SQLITE_AP
2b800 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2b810 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
2b820 6d 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  m(sqlite3_contex
2b830 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  t*);.SQLITE_API 
2b840 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2b850 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73  ult_error_code(s
2b860 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2b870 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50   int);.SQLITE_AP
2b880 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2b890 65 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65  esult_int(sqlite
2b8a0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29  3_context*, int)
2b8b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
2b8c0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
2b8d0 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63  _int64(sqlite3_c
2b8e0 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33  ontext*, sqlite3
2b8f0 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f  _int64);.SQLITE_
2b900 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2b910 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c  _result_null(sql
2b920 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
2b930 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2b940 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2b950 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ext(sqlite3_cont
2b960 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  ext*, const char
2b970 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28  *, int, void(*)(
2b980 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  void*));.SQLITE_
2b990 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2b9a0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 73  _result_text16(s
2b9b0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2b9c0 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
2b9d0 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  t, void(*)(void*
2b9e0 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ));.SQLITE_API v
2b9f0 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
2ba00 6c 74 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69  lt_text16le(sqli
2ba10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
2ba20 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76  nst void*, int,v
2ba30 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2ba40 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2ba50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2ba60 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f  ext16be(sqlite3_
2ba70 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
2ba80 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28  void*, int,void(
2ba90 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
2baa0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2bab0 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
2bac0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2bad0 2a 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *, sqlite3_value
2bae0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
2baf0 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
2bb00 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69  lt_zeroblob(sqli
2bb10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e  te3_context*, in
2bb20 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  t n);../*.** CAP
2bb30 49 33 52 45 46 3a 20 44 65 66 69 6e 65 20 4e 65  I3REF: Define Ne
2bb40 77 20 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75  w Collating Sequ
2bb50 65 6e 63 65 73 20 7b 48 31 36 36 30 30 7d 20 3c  ences {H16600} <
2bb60 53 32 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20300>.**.** Th
2bb70 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  ese functions ar
2bb80 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 6e 65  e used to add ne
2bb90 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  w collation sequ
2bba0 65 6e 63 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20  ences to the.** 
2bbb0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
2bbc0 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 20  tion] specified 
2bbd0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
2bbe0 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ument..**.** The
2bbf0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77   name of the new
2bc00 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2bc10 6e 63 65 20 69 73 20 73 70 65 63 69 66 69 65 64  nce is specified
2bc20 20 61 73 20 61 20 55 54 46 2d 38 20 73 74 72 69   as a UTF-8 stri
2bc30 6e 67 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65  ng.** for sqlite
2bc40 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
2bc50 6f 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  on() and sqlite3
2bc60 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2bc70 6e 5f 76 32 28 29 0a 2a 2a 20 61 6e 64 20 61 20  n_v2().** and a 
2bc80 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 66 6f  UTF-16 string fo
2bc90 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  r sqlite3_create
2bca0 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 20  _collation16(). 
2bcb0 49 6e 20 61 6c 6c 20 63 61 73 65 73 0a 2a 2a 20  In all cases.** 
2bcc0 74 68 65 20 6e 61 6d 65 20 69 73 20 70 61 73 73  the name is pass
2bcd0 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
2bce0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
2bcf0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  nt..**.** The th
2bd00 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 61 79  ird argument may
2bd10 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 63   be one of the c
2bd20 6f 6e 73 74 61 6e 74 73 20 5b 53 51 4c 49 54 45  onstants [SQLITE
2bd30 5f 55 54 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49  _UTF8],.** [SQLI
2bd40 54 45 5f 55 54 46 31 36 4c 45 5d 20 6f 72 20 5b  TE_UTF16LE] or [
2bd50 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c  SQLITE_UTF16BE],
2bd60 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
2bd70 20 74 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69   the user-suppli
2bd80 65 64 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78  ed.** routine ex
2bd90 70 65 63 74 73 20 74 6f 20 62 65 20 70 61 73 73  pects to be pass
2bda0 65 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73  ed pointers to s
2bdb0 74 72 69 6e 67 73 20 65 6e 63 6f 64 65 64 20 75  trings encoded u
2bdc0 73 69 6e 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55  sing UTF-8,.** U
2bdd0 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64  TF-16 little-end
2bde0 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62  ian, or UTF-16 b
2bdf0 69 67 2d 65 6e 64 69 61 6e 2c 20 72 65 73 70 65  ig-endian, respe
2be00 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20  ctively. The.** 
2be10 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d  third argument m
2be20 69 67 68 74 20 61 6c 73 6f 20 62 65 20 5b 53 51  ight also be [SQ
2be30 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
2be40 45 44 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20  ED] to indicate 
2be50 74 68 61 74 0a 2a 2a 20 74 68 65 20 72 6f 75 74  that.** the rout
2be60 69 6e 65 20 65 78 70 65 63 74 73 20 70 6f 69 6e  ine expects poin
2be70 74 65 72 73 20 74 6f 20 31 36 2d 62 69 74 20 77  ters to 16-bit w
2be80 6f 72 64 20 61 6c 69 67 6e 65 64 20 73 74 72 69  ord aligned stri
2be90 6e 67 73 0a 2a 2a 20 6f 66 20 55 54 46 2d 31 36  ngs.** of UTF-16
2bea0 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62   in the native b
2beb0 79 74 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65  yte order of the
2bec0 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a   host computer..
2bed0 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  **.** A pointer 
2bee0 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 70 70  to the user supp
2bef0 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d 75 73  lied routine mus
2bf00 74 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74  t be passed as t
2bf10 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 67 75  he fifth.** argu
2bf20 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 73 20  ment.  If it is 
2bf30 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 74 68  NULL, this is th
2bf40 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 74 69  e same as deleti
2bf50 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ng the collation
2bf60 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 73 6f  .** sequence (so
2bf70 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e   that SQLite can
2bf80 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e 79 6d  not call it anym
2bf90 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 74 69  ore)..** Each ti
2bfa0 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  me the applicati
2bfb0 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 6e 63  on supplied func
2bfc0 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2c  tion is invoked,
2bfd0 20 69 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a   it is passed.**
2bfe0 20 61 73 20 69 74 73 20 66 69 72 73 74 20 70 61   as its first pa
2bff0 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 20 6f  rameter a copy o
2c000 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 73 73  f the void* pass
2c010 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68  ed as the fourth
2c020 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
2c030 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2c040 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 73 71  ollation() or sq
2c050 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2c060 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a  lation16()..**.*
2c070 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20  * The remaining 
2c080 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
2c090 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70   application-sup
2c0a0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61 72  plied routine ar
2c0b0 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a 2a  e two strings,.*
2c0c0 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e 74  * each represent
2c0d0 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68 2c  ed by a (length,
2c0e0 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64 20   data) pair and 
2c0f0 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 65  encoded in the e
2c100 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74 20  ncoding.** that 
2c110 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 68  was passed as th
2c120 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
2c130 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74   when the collat
2c140 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61 73  ion sequence was
2c150 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e 20  .** registered. 
2c160 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 6c 69  {END}  The appli
2c170 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 63  cation defined c
2c180 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  ollation routine
2c190 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72   should.** retur
2c1a0 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f  n negative, zero
2c1b0 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20   or positive if 
2c1c0 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
2c1d0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 0a 2a   is less than,.*
2c1e0 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  * equal to, or g
2c1f0 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
2c200 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e 20 69  second string. i
2c210 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d 20 53  .e. (STRING1 - S
2c220 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a 20 54  TRING2)..**.** T
2c230 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  he sqlite3_creat
2c240 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29  e_collation_v2()
2c250 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69   works like sqli
2c260 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2c270 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 70 74  tion().** except
2c280 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61   that it takes a
2c290 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74  n extra argument
2c2a0 20 77 68 69 63 68 20 69 73 20 61 20 64 65 73 74   which is a dest
2c2b0 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 74 68  ructor for.** th
2c2c0 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 54 68  e collation.  Th
2c2d0 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 73 20  e destructor is 
2c2e0 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
2c2f0 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a 2a 20  collation is.** 
2c300 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 69 73  destroyed and is
2c310 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f   passed a copy o
2c320 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72  f the fourth par
2c330 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 6f 69  ameter void* poi
2c340 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 73  nter.** of the s
2c350 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2c360 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a 2a 2a  llation_v2()..**
2c370 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 65 20   Collations are 
2c380 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 74  destroyed when t
2c390 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 64 64  hey are overridd
2c3a0 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 6c 6c  en by later call
2c3b0 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c  s to the.** coll
2c3c0 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 66  ation creation f
2c3d0 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 65 6e  unctions or when
2c3e0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
2c3f0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63 6c  onnection] is cl
2c400 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 5b 73  osed.** using [s
2c410 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e  qlite3_close()].
2c420 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
2c430 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 36 30 33 5d  nts:.** [H16603]
2c440 20 5b 48 31 36 36 30 34 5d 20 5b 48 31 36 36 30   [H16604] [H1660
2c450 36 5d 20 5b 48 31 36 36 30 39 5d 20 5b 48 31 36  6] [H16609] [H16
2c460 36 31 32 5d 20 5b 48 31 36 36 31 35 5d 20 5b 48  612] [H16615] [H
2c470 31 36 36 31 38 5d 20 5b 48 31 36 36 32 31 5d 0a  16618] [H16621].
2c480 2a 2a 20 5b 48 31 36 36 32 34 5d 20 5b 48 31 36  ** [H16624] [H16
2c490 36 32 37 5d 20 5b 48 31 36 36 33 30 5d 0a 2a 2f  627] [H16630].*/
2c4a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2c4b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2c4c0 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c 69  ollation(.  sqli
2c4d0 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 63  te3*, .  const c
2c4e0 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69  har *zName, .  i
2c4f0 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20  nt eTextRep, .  
2c500 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43  void*,.  int(*xC
2c510 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e  ompare)(void*,in
2c520 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
2c530 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29  t,const void*).)
2c540 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2c550 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2c560 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a 20 20  collation_v2(.  
2c570 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e  sqlite3*, .  con
2c580 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
2c590 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
2c5a0 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74   .  void*,.  int
2c5b0 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
2c5c0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2c5d0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2c5e0 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 73  *),.  void(*xDes
2c5f0 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29 3b 0a  troy)(void*).);.
2c600 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2c610 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2c620 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c  llation16(.  sql
2c630 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20  ite3*, .  const 
2c640 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69  void *zName,.  i
2c650 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20  nt eTextRep, .  
2c660 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43  void*,.  int(*xC
2c670 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e  ompare)(void*,in
2c680 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
2c690 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29  t,const void*).)
2c6a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2c6b0 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e 65 65  F: Collation Nee
2c6c0 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48  ded Callbacks {H
2c6d0 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a  16700} <S20300>.
2c6e0 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 68  **.** To avoid h
2c6f0 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73 74 65  aving to registe
2c700 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f 6e 20  r all collation 
2c710 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f 72 65  sequences before
2c720 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63   a database.** c
2c730 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20 73 69  an be used, a si
2c740 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20 66 75  ngle callback fu
2c750 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 72 65  nction may be re
2c760 67 69 73 74 65 72 65 64 20 77 69 74 68 20 74 68  gistered with th
2c770 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
2c780 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 62 65  onnection] to be
2c790 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 65 72   called whenever
2c7a0 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20 63 6f   an undefined co
2c7b0 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65  llation.** seque
2c7c0 6e 63 65 20 69 73 20 72 65 71 75 69 72 65 64 2e  nce is required.
2c7d0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 75  .**.** If the fu
2c7e0 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74  nction is regist
2c7f0 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 73  ered using the s
2c800 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2c810 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c 0a 2a  _needed() API,.*
2c820 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70 61 73  * then it is pas
2c830 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  sed the names of
2c840 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61   undefined colla
2c850 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 61  tion sequences a
2c860 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65 6e 63  s strings.** enc
2c870 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 20 7b  oded in UTF-8. {
2c880 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c 69 74  H16703} If sqlit
2c890 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
2c8a0 64 65 64 31 36 28 29 20 69 73 20 75 73 65 64 2c  ded16() is used,
2c8b0 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 61 72  .** the names ar
2c8c0 65 20 70 61 73 73 65 64 20 61 73 20 55 54 46 2d  e passed as UTF-
2c8d0 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 6e 61  16 in machine na
2c8e0 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e  tive byte order.
2c8f0 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 65 69  .** A call to ei
2c900 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 72 65  ther function re
2c910 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69 73 74  places any exist
2c920 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  ing callback..**
2c930 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 61 6c  .** When the cal
2c940 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
2c950 2c 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  , the first argu
2c960 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73 20 61  ment passed is a
2c970 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68 65 20   copy.** of the 
2c980 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
2c990 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  to sqlite3_colla
2c9a0 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 6f 72  tion_needed() or
2c9b0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  .** sqlite3_coll
2c9c0 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29  ation_needed16()
2c9d0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  .  The second ar
2c9e0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 64 61  gument is the da
2c9f0 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
2ca00 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69 72 64  tion.  The third
2ca10 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65   argument is one
2ca20 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54 46 38   of [SQLITE_UTF8
2ca30 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  ], [SQLITE_UTF16
2ca40 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49  BE],.** or [SQLI
2ca50 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69 6e 64  TE_UTF16LE], ind
2ca60 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f 73 74  icating the most
2ca70 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72 6d 20   desirable form 
2ca80 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  of the collation
2ca90 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66 75 6e  .** sequence fun
2caa0 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64 2e 20  ction required. 
2cab0 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   The fourth para
2cac0 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d  meter is the nam
2cad0 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 71 75  e of the.** requ
2cae0 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  ired collation s
2caf0 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  equence..**.** T
2cb00 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
2cb10 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 67 69  tion should regi
2cb20 73 74 65 72 20 74 68 65 20 64 65 73 69 72 65 64  ster the desired
2cb30 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69 6e 67   collation using
2cb40 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  .** [sqlite3_cre
2cb50 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 5d  ate_collation()]
2cb60 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  , [sqlite3_creat
2cb70 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 5d  e_collation16()]
2cb80 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  , or.** [sqlite3
2cb90 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2cba0 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52  n_v2()]..**.** R
2cbb0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
2cbc0 5b 48 31 36 37 30 32 5d 20 5b 48 31 36 37 30 34  [H16702] [H16704
2cbd0 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 2f 0a 53 51  ] [H16706].*/.SQ
2cbe0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2cbf0 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
2cc00 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74 65 33  eeded(.  sqlite3
2cc10 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 20 0a 20 20  *, .  void*, .  
2cc20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71  void(*)(void*,sq
2cc30 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74  lite3*,int eText
2cc40 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  Rep,const char*)
2cc50 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
2cc60 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  nt sqlite3_colla
2cc70 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20  tion_needed16(. 
2cc80 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f   sqlite3*, .  vo
2cc90 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a 29 28 76  id*,.  void(*)(v
2cca0 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
2ccb0 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
2ccc0 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a   void*).);../*.*
2ccd0 2a 20 53 70 65 63 69 66 79 20 74 68 65 20 6b 65  * Specify the ke
2cce0 79 20 66 6f 72 20 61 6e 20 65 6e 63 72 79 70 74  y for an encrypt
2ccf0 65 64 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  ed database.  Th
2cd00 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  is routine shoul
2cd10 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 72  d be.** called r
2cd20 69 67 68 74 20 61 66 74 65 72 20 73 71 6c 69 74  ight after sqlit
2cd30 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a  e3_open()..**.**
2cd40 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 6d 70   The code to imp
2cd50 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 49 20  lement this API 
2cd60 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
2cd70 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20 72   in the public r
2cd80 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c  elease.** of SQL
2cd90 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ite..*/.SQLITE_A
2cda0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6b  PI int sqlite3_k
2cdb0 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ey(.  sqlite3 *d
2cdc0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
2cdd0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2cde0 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64 20 2a   to be rekeyed *
2cdf0 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
2ce00 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20  pKey, int nKey  
2ce10 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f     /* The key */
2ce20 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  .);../*.** Chang
2ce30 65 20 74 68 65 20 6b 65 79 20 6f 6e 20 61 6e 20  e the key on an 
2ce40 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20 20  open database.  
2ce50 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 64  If the current d
2ce60 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a  atabase is not.*
2ce70 2a 20 65 6e 63 72 79 70 74 65 64 2c 20 74 68 69  * encrypted, thi
2ce80 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65  s routine will e
2ce90 6e 63 72 79 70 74 20 69 74 2e 20 20 49 66 20 70  ncrypt it.  If p
2cea0 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d  New==0 or nNew==
2ceb0 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  0, the.** databa
2cec0 73 65 20 69 73 20 64 65 63 72 79 70 74 65 64 2e  se is decrypted.
2ced0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
2cee0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69  to implement thi
2cef0 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61  s API is not ava
2cf00 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75  ilable in the pu
2cf10 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20  blic release.** 
2cf20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51  of SQLite..*/.SQ
2cf30 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2cf40 69 74 65 33 5f 72 65 6b 65 79 28 0a 20 20 73 71  ite3_rekey(.  sq
2cf50 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2cf60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2cf70 44 61 74 61 62 61 73 65 20 74 6f 20 62 65 20 72  Database to be r
2cf80 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  ekeyed */.  cons
2cf90 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
2cfa0 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 54 68  t nKey     /* Th
2cfb0 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a  e new key */.);.
2cfc0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2cfd0 20 53 75 73 70 65 6e 64 20 45 78 65 63 75 74 69   Suspend Executi
2cfe0 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74 20 54  on For A Short T
2cff0 69 6d 65 20 7b 48 31 30 35 33 30 7d 20 3c 53 34  ime {H10530} <S4
2d000 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0410>.**.** The 
2d010 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 29 20  sqlite3_sleep() 
2d020 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65 73 20  function causes 
2d030 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65  the current thre
2d040 61 64 20 74 6f 20 73 75 73 70 65 6e 64 20 65 78  ad to suspend ex
2d050 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61  ecution.** for a
2d060 74 20 6c 65 61 73 74 20 61 20 6e 75 6d 62 65 72  t least a number
2d070 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73   of milliseconds
2d080 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 69 74   specified in it
2d090 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a  s parameter..**.
2d0a0 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72 61 74  ** If the operat
2d0b0 69 6e 67 20 73 79 73 74 65 6d 20 64 6f 65 73 20  ing system does 
2d0c0 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 6c 65 65  not support slee
2d0d0 70 20 72 65 71 75 65 73 74 73 20 77 69 74 68 0a  p requests with.
2d0e0 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 20 74  ** millisecond t
2d0f0 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 20  ime resolution, 
2d100 74 68 65 6e 20 74 68 65 20 74 69 6d 65 20 77 69  then the time wi
2d110 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64 20 75 70  ll be rounded up
2d120 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 61 72 65   to.** the neare
2d130 73 74 20 73 65 63 6f 6e 64 2e 20 54 68 65 20 6e  st second. The n
2d140 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65  umber of millise
2d150 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61  conds of sleep a
2d160 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65  ctually.** reque
2d170 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 70  sted from the op
2d180 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
2d190 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
2d1a0 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65  * SQLite impleme
2d1b0 6e 74 73 20 74 68 69 73 20 69 6e 74 65 72 66 61  nts this interfa
2d1c0 63 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 74 68  ce by calling th
2d1d0 65 20 78 53 6c 65 65 70 28 29 0a 2a 2a 20 6d 65  e xSleep().** me
2d1e0 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61  thod of the defa
2d1f0 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  ult [sqlite3_vfs
2d200 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  ] object..**.** 
2d210 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48  Requirements: [H
2d220 31 30 35 33 33 5d 20 5b 48 31 30 35 33 36 5d 0a  10533] [H10536].
2d230 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2d240 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28  t sqlite3_sleep(
2d250 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  int);../*.** CAP
2d260 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 54  I3REF: Name Of T
2d270 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e  he Folder Holdin
2d280 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69 6c 65  g Temporary File
2d290 73 20 7b 48 31 30 33 31 30 7d 20 3c 53 32 30 30  s {H10310} <S200
2d2a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69  00>.**.** If thi
2d2b0 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  s global variabl
2d2c0 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69  e is made to poi
2d2d0 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77  nt to a string w
2d2e0 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6e  hich is.** the n
2d2f0 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 72 20  ame of a folder 
2d300 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 6f 72  (a.k.a. director
2d310 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 6d  y), then all tem
2d320 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a 2a 20  porary files.** 
2d330 63 72 65 61 74 65 64 20 62 79 20 53 51 4c 69 74  created by SQLit
2d340 65 20 77 69 6c 6c 20 62 65 20 70 6c 61 63 65 64  e will be placed
2d350 20 69 6e 20 74 68 61 74 20 64 69 72 65 63 74 6f   in that directo
2d360 72 79 2e 20 20 49 66 20 74 68 69 73 20 76 61 72  ry.  If this var
2d370 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 4e 55  iable.** is a NU
2d380 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e  LL pointer, then
2d390 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72 6d 73   SQLite performs
2d3a0 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e   a search for an
2d3b0 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
2d3c0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64  temporary file d
2d3d0 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  irectory..**.** 
2d3e0 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  It is not safe t
2d3f0 6f 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61  o modify this va
2d400 72 69 61 62 6c 65 20 6f 6e 63 65 20 61 20 5b 64  riable once a [d
2d410 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2d420 6f 6e 5d 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  on].** has been 
2d430 6f 70 65 6e 65 64 2e 20 20 49 74 20 69 73 20 69  opened.  It is i
2d440 6e 74 65 6e 64 65 64 20 74 68 61 74 20 74 68 69  ntended that thi
2d450 73 20 76 61 72 69 61 62 6c 65 20 62 65 20 73 65  s variable be se
2d460 74 20 6f 6e 63 65 0a 2a 2a 20 61 73 20 70 61 72  t once.** as par
2d470 74 20 6f 66 20 70 72 6f 63 65 73 73 20 69 6e 69  t of process ini
2d480 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20  tialization and 
2d490 62 65 66 6f 72 65 20 61 6e 79 20 53 51 4c 69 74  before any SQLit
2d4a0 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 72  e interface.** r
2d4b0 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62 65 65  outines have bee
2d4c0 6e 20 63 61 6c 6c 20 61 6e 64 20 72 65 6d 61 69  n call and remai
2d4d0 6e 20 75 6e 63 68 61 6e 67 65 64 20 74 68 65 72  n unchanged ther
2d4e0 65 61 66 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  eafter..*/.SQLIT
2d4f0 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
2d500 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
2d510 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ry;../*.** CAPI3
2d520 52 45 46 3a 20 54 65 73 74 20 46 6f 72 20 41 75  REF: Test For Au
2d530 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64 65 20 7b  to-Commit Mode {
2d540 48 31 32 39 33 30 7d 20 3c 53 36 30 32 30 30 3e  H12930} <S60200>
2d550 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61  .** KEYWORDS: {a
2d560 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 7d 0a  utocommit mode}.
2d570 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2d580 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
2d590 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
2d5a0 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72  urns non-zero or
2d5b0 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 65 20  .** zero if the 
2d5c0 67 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 63  given database c
2d5d0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 72 20  onnection is or 
2d5e0 69 73 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f  is not in autoco
2d5f0 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72 65  mmit mode,.** re
2d600 73 70 65 63 74 69 76 65 6c 79 2e 20 20 41 75 74  spectively.  Aut
2d610 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
2d620 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a  on by default..*
2d630 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64  * Autocommit mod
2d640 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79  e is disabled by
2d650 20 61 20 5b 42 45 47 49 4e 5d 20 73 74 61 74 65   a [BEGIN] state
2d660 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d  ment..** Autocom
2d670 6d 69 74 20 6d 6f 64 65 20 69 73 20 72 65 2d 65  mit mode is re-e
2d680 6e 61 62 6c 65 64 20 62 79 20 61 20 5b 43 4f 4d  nabled by a [COM
2d690 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42 41 43  MIT] or [ROLLBAC
2d6a0 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 65 72  K]..**.** If cer
2d6b0 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 20 65 72  tain kinds of er
2d6c0 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e 20 61 20  rors occur on a 
2d6d0 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e  statement within
2d6e0 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 65 6d 65   a multi-stateme
2d6f0 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  nt.** transactio
2d700 6e 20 28 65 72 72 6f 72 73 20 69 6e 63 6c 75 64  n (errors includ
2d710 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46 55 4c 4c  ing [SQLITE_FULL
2d720 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  ], [SQLITE_IOERR
2d730 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f  ],.** [SQLITE_NO
2d740 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55  MEM], [SQLITE_BU
2d750 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 54 45  SY], and [SQLITE
2d760 5f 49 4e 54 45 52 52 55 50 54 5d 29 20 74 68 65  _INTERRUPT]) the
2d770 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63  n the.** transac
2d780 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 72 6f  tion might be ro
2d790 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61  lled back automa
2d7a0 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 6f 6e  tically.  The on
2d7b0 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20 66 69 6e  ly way to.** fin
2d7c0 64 20 6f 75 74 20 77 68 65 74 68 65 72 20 53 51  d out whether SQ
2d7d0 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  Lite automatical
2d7e0 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74  ly rolled back t
2d7f0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  he transaction a
2d800 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72  fter.** an error
2d810 20 69 73 20 74 6f 20 75 73 65 20 74 68 69 73 20   is to use this 
2d820 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
2d830 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  If another threa
2d840 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61 75  d changes the au
2d850 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73 20  tocommit status 
2d860 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  of the database.
2d870 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68  ** connection wh
2d880 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ile this routine
2d890 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65   is running, the
2d8a0 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  n the return val
2d8b0 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e  ue.** is undefin
2d8c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ed..**.** Requir
2d8d0 65 6d 65 6e 74 73 3a 20 5b 48 31 32 39 33 31 5d  ements: [H12931]
2d8e0 20 5b 48 31 32 39 33 32 5d 20 5b 48 31 32 39 33   [H12932] [H1293
2d8f0 33 5d 20 5b 48 31 32 39 33 34 5d 0a 2a 2f 0a 53  3] [H12934].*/.S
2d900 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2d910 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f  lite3_get_autoco
2d920 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a  mmit(sqlite3*);.
2d930 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2d940 20 46 69 6e 64 20 54 68 65 20 44 61 74 61 62 61   Find The Databa
2d950 73 65 20 48 61 6e 64 6c 65 20 4f 66 20 41 20 50  se Handle Of A P
2d960 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e  repared Statemen
2d970 74 20 7b 48 31 33 31 32 30 7d 20 3c 53 36 30 36  t {H13120} <S606
2d980 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
2d990 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20  lite3_db_handle 
2d9a0 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
2d9b0 73 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  s the [database 
2d9c0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64  connection] hand
2d9d0 6c 65 0a 2a 2a 20 74 6f 20 77 68 69 63 68 20 61  le.** to which a
2d9e0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2d9f0 6d 65 6e 74 5d 20 62 65 6c 6f 6e 67 73 2e 20 20  ment] belongs.  
2da00 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  The [database co
2da10 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 72 65 74  nnection].** ret
2da20 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
2da30 5f 64 62 5f 68 61 6e 64 6c 65 20 69 73 20 74 68  _db_handle is th
2da40 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65  e same [database
2da50 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61   connection] tha
2da60 74 20 77 61 73 20 74 68 65 20 66 69 72 73 74 20  t was the first 
2da70 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74  argument.** to t
2da80 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  he [sqlite3_prep
2da90 61 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28  are_v2()] call (
2daa0 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29  or its variants)
2dab0 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74   that was used t
2dac0 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20  o.** create the 
2dad0 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65  statement in the
2dae0 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a   first place..**
2daf0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2db00 3a 20 5b 48 31 33 31 32 33 5d 0a 2a 2f 0a 53 51  : [H13123].*/.SQ
2db10 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
2db20 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e   *sqlite3_db_han
2db30 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  dle(sqlite3_stmt
2db40 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
2db50 52 45 46 3a 20 46 69 6e 64 20 74 68 65 20 6e 65  REF: Find the ne
2db60 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74  xt prepared stat
2db70 65 6d 65 6e 74 20 7b 48 31 33 31 34 30 7d 20 3c  ement {H13140} <
2db80 53 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S60600>.**.** Th
2db90 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  is interface ret
2dba0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
2dbb0 6f 20 74 68 65 20 6e 65 78 74 20 5b 70 72 65 70  o the next [prep
2dbc0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
2dbd0 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d 74 20 61  after.** pStmt a
2dbe0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2dbf0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
2dc00 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e 20 20 49  nection] pDb.  I
2dc10 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 0a  f pStmt is NULL.
2dc20 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 69 6e 74  ** then this int
2dc30 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
2dc40 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2dc50 66 69 72 73 74 20 70 72 65 70 61 72 65 64 20 73  first prepared s
2dc60 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 73 73 6f  tatement.** asso
2dc70 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
2dc80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2dc90 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 6e 6f 20  ion pDb.  If no 
2dca0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2dcb0 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69 65 73 20  nt.** satisfies 
2dcc0 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6f  the conditions o
2dcd0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  f this routine, 
2dce0 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  it returns NULL.
2dcf0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64 61 74 61  .**.** The [data
2dd00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
2dd10 20 70 6f 69 6e 74 65 72 20 44 20 69 6e 20 61 20   pointer D in a 
2dd20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69  call to.** [sqli
2dd30 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c  te3_next_stmt(D,
2dd40 53 29 5d 20 6d 75 73 74 20 72 65 66 65 72 20 74  S)] must refer t
2dd50 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61  o an open databa
2dd60 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
2dd70 20 61 6e 64 20 69 6e 20 70 61 72 74 69 63 75 6c   and in particul
2dd80 61 72 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 61  ar must not be a
2dd90 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
2dda0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2ddb0 73 3a 20 5b 48 31 33 31 34 33 5d 20 5b 48 31 33  s: [H13143] [H13
2ddc0 31 34 36 5d 20 5b 48 31 33 31 34 39 5d 20 5b 48  146] [H13149] [H
2ddd0 31 33 31 35 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13152].*/.SQLITE
2dde0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d  _API sqlite3_stm
2ddf0 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f  t *sqlite3_next_
2de00 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44  stmt(sqlite3 *pD
2de10 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  b, sqlite3_stmt 
2de20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
2de30 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74  CAPI3REF: Commit
2de40 20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f   And Rollback No
2de50 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62  tification Callb
2de60 61 63 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c 53  acks {H12950} <S
2de70 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  60400>.**.** The
2de80 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f   sqlite3_commit_
2de90 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65  hook() interface
2dea0 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c   registers a cal
2deb0 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f  lback.** functio
2dec0 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  n to be invoked 
2ded0 77 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73  whenever a trans
2dee0 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74  action is commit
2def0 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c  ted..** Any call
2df00 62 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72  back set by a pr
2df10 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73  evious call to s
2df20 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
2df30 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ok().** for the 
2df40 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
2df50 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72  nnection is over
2df60 72 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73  ridden..** The s
2df70 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f  qlite3_rollback_
2df80 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65  hook() interface
2df90 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c   registers a cal
2dfa0 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f  lback.** functio
2dfb0 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  n to be invoked 
2dfc0 77 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73  whenever a trans
2dfd0 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74  action is commit
2dfe0 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c  ted..** Any call
2dff0 62 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72  back set by a pr
2e000 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73  evious call to s
2e010 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
2e020 6f 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ok().** for the 
2e030 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
2e040 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72  nnection is over
2e050 72 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70  ridden..** The p
2e060 41 72 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20  Arg argument is 
2e070 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74  passed through t
2e080 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a  o the callback..
2e090 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61  ** If the callba
2e0a0 63 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68  ck on a commit h
2e0b0 6f 6f 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  ook function ret
2e0c0 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a  urns non-zero,.*
2e0d0 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69  * then the commi
2e0e0 74 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69  t is converted i
2e0f0 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a  nto a rollback..
2e100 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72  **.** If another
2e110 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72   function was pr
2e120 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65  eviously registe
2e130 72 65 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67  red, its.** pArg
2e140 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e   value is return
2e150 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e  ed.  Otherwise N
2e160 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
2e170 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62  .**.** The callb
2e180 61 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ack implementati
2e190 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61  on must not do a
2e1a0 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c  nything that wil
2e1b0 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20  l modify.** the 
2e1c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2e1d0 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  ion that invoked
2e1e0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20   the callback.  
2e1f0 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74  Any actions.** t
2e200 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74  o modify the dat
2e210 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2e220 20 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65   must be deferre
2e230 64 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68  d until after th
2e240 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20  e.** completion 
2e250 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
2e260 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61  step()] call tha
2e270 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20  t triggered the 
2e280 63 6f 6d 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c  commit.** or rol
2e290 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68  lback hook in th
2e2a0 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a  e first place..*
2e2b0 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c  * Note that [sql
2e2c0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
2e2d0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
2e2e0 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64  step()] both mod
2e2f0 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74  ify their.** dat
2e300 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2e310 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e  s for the meanin
2e320 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e  g of "modify" in
2e330 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e   this paragraph.
2e340 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69  .**.** Registeri
2e350 6e 67 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69  ng a NULL functi
2e360 6f 6e 20 64 69 73 61 62 6c 65 73 20 74 68 65 20  on disables the 
2e370 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  callback..**.** 
2e380 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
2e390 20 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61 20   of this API, a 
2e3a0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
2e3b0 61 69 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e  aid to have been
2e3c0 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  .** rolled back 
2e3d0 69 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22  if an explicit "
2e3e0 52 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d  ROLLBACK" statem
2e3f0 65 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 2c  ent is executed,
2e400 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20   or.** an error 
2e410 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61  or constraint ca
2e420 75 73 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74  uses an implicit
2e430 20 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63   rollback to occ
2e440 75 72 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62  ur..** The rollb
2e450 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ack callback is 
2e460 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61  not invoked if a
2e470 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a   transaction is.
2e480 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ** automatically
2e490 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63   rolled back bec
2e4a0 61 75 73 65 20 74 68 65 20 64 61 74 61 62 61 73  ause the databas
2e4b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
2e4c0 63 6c 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72  closed..** The r
2e4d0 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b  ollback callback
2e4e0 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20   is not invoked 
2e4f0 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  if a transaction
2e500 20 69 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61   is.** rolled ba
2e510 63 6b 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d  ck because a com
2e520 6d 69 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  mit callback ret
2e530 75 72 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a  urned non-zero..
2e540 2a 2a 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20  ** <todo> Check 
2e550 6f 6e 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a  on this </todo>.
2e560 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
2e570 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20  ts:.** [H12951] 
2e580 5b 48 31 32 39 35 32 5d 20 5b 48 31 32 39 35 33  [H12952] [H12953
2e590 5d 20 5b 48 31 32 39 35 34 5d 20 5b 48 31 32 39  ] [H12954] [H129
2e5a0 35 35 5d 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20  55].** [H12961] 
2e5b0 5b 48 31 32 39 36 32 5d 20 5b 48 31 32 39 36 33  [H12962] [H12963
2e5c0 5d 20 5b 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51  ] [H12964].*/.SQ
2e5d0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
2e5e0 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
2e5f0 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ok(sqlite3*, int
2e600 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64  (*)(void*), void
2e610 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
2e620 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c  oid *sqlite3_rol
2e630 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74  lback_hook(sqlit
2e640 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  e3*, void(*)(voi
2e650 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  d *), void*);../
2e660 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
2e670 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 66  ata Change Notif
2e680 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b  ication Callback
2e690 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 34  s {H12970} <S604
2e6a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
2e6b0 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
2e6c0 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  k() interface re
2e6d0 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61  gisters a callba
2e6e0 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77  ck function.** w
2e6f0 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73  ith the [databas
2e700 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64  e connection] id
2e710 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
2e720 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a  first argument.*
2e730 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  * to be invoked 
2e740 77 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 69  whenever a row i
2e750 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72  s updated, inser
2e760 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a  ted or deleted..
2e770 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20  ** Any callback 
2e780 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f 75  set by a previou
2e790 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66  s call to this f
2e7a0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74  unction.** for t
2e7b0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
2e7c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f   connection is o
2e7d0 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a  verridden..**.**
2e7e0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
2e7f0 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65  ment is a pointe
2e800 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  r to the functio
2e810 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e  n to invoke when
2e820 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64   a.** row is upd
2e830 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f  ated, inserted o
2e840 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68  r deleted..** Th
2e850 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
2e860 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
2e870 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68   is a copy of th
2e880 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
2e890 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75  .** to sqlite3_u
2e8a0 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a  pdate_hook()..**
2e8b0 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c   The second call
2e8c0 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73  back argument is
2e8d0 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f   one of [SQLITE_
2e8e0 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45  INSERT], [SQLITE
2e8f0 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20  _DELETE],.** or 
2e900 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c  [SQLITE_UPDATE],
2e910 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
2e920 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74  e operation that
2e930 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c   caused the call
2e940 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e  back.** to be in
2e950 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68  voked..** The th
2e960 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61  ird and fourth a
2e970 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
2e980 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
2e990 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
2e9a0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64  .** database and
2e9b0 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74   table name cont
2e9c0 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63  aining the affec
2e9d0 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20  ted row..** The 
2e9e0 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70  final callback p
2e9f0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
2ea00 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 72  [rowid] of the r
2ea10 6f 77 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61  ow..** In the ca
2ea20 73 65 20 6f 66 20 61 6e 20 75 70 64 61 74 65 2c  se of an update,
2ea30 20 74 68 69 73 20 69 73 20 74 68 65 20 5b 72 6f   this is the [ro
2ea40 77 69 64 5d 20 61 66 74 65 72 20 74 68 65 20 75  wid] after the u
2ea50 70 64 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63  pdate takes plac
2ea60 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64  e..**.** The upd
2ea70 61 74 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20  ate hook is not 
2ea80 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74  invoked when int
2ea90 65 72 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62  ernal system tab
2eaa0 6c 65 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66  les are.** modif
2eab0 69 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65  ied (i.e. sqlite
2eac0 5f 6d 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69  _master and sqli
2ead0 74 65 5f 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a  te_sequence)..**
2eae0 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 68  .** The update h
2eaf0 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ook implementati
2eb00 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61  on must not do a
2eb10 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c  nything that wil
2eb20 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20  l modify.** the 
2eb30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2eb40 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  ion that invoked
2eb50 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   the update hook
2eb60 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a  .  Any actions.*
2eb70 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20  * to modify the 
2eb80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2eb90 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 65  ion must be defe
2eba0 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 72  rred until after
2ebb0 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69   the.** completi
2ebc0 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  on of the [sqlit
2ebd0 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20  e3_step()] call 
2ebe0 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 74  that triggered t
2ebf0 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a  he update hook..
2ec00 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71  ** Note that [sq
2ec10 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
2ec20 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
2ec30 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f  _step()] both mo
2ec40 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61  dify their.** da
2ec50 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2ec60 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69  ns for the meani
2ec70 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69  ng of "modify" i
2ec80 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
2ec90 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68  ..**.** If anoth
2eca0 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  er function was 
2ecb0 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73  previously regis
2ecc0 74 65 72 65 64 2c 20 69 74 73 20 70 41 72 67 20  tered, its pArg 
2ecd0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65 74 75  value.** is retu
2ece0 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65  rned.  Otherwise
2ecf0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
2ed00 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  d..**.** Require
2ed10 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 37  ments:.** [H1297
2ed20 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48 31 32  1] [H12973] [H12
2ed30 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20 5b 48  975] [H12977] [H
2ed40 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31 5d 20  12979] [H12981] 
2ed50 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39 38 36  [H12983] [H12986
2ed60 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
2ed70 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 70  void *sqlite3_up
2ed80 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c  date_hook(.  sql
2ed90 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 28 2a  ite3*, .  void(*
2eda0 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c 63 68  )(void *,int ,ch
2edb0 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20  ar const *,char 
2edc0 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 33 5f  const *,sqlite3_
2edd0 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 2a 0a  int64),.  void*.
2ede0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2edf0 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 69  EF: Enable Or Di
2ee00 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 61 67  sable Shared Pag
2ee10 65 72 20 43 61 63 68 65 20 7b 48 31 30 33 33 30  er Cache {H10330
2ee20 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20 4b 45  } <S30900>.** KE
2ee30 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65 64 20  YWORDS: {shared 
2ee40 63 61 63 68 65 7d 20 7b 73 68 61 72 65 64 20 63  cache} {shared c
2ee50 61 63 68 65 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a  ache mode}.**.**
2ee60 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e   This routine en
2ee70 61 62 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65  ables or disable
2ee80 73 20 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66  s the sharing of
2ee90 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61   the database ca
2eea0 63 68 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d  che.** and schem
2eeb0 61 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  a data structure
2eec0 73 20 62 65 74 77 65 65 6e 20 5b 64 61 74 61 62  s between [datab
2eed0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c  ase connection |
2eee0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a   connections].**
2eef0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74   to the same dat
2ef00 61 62 61 73 65 2e 20 53 68 61 72 69 6e 67 20 69  abase. Sharing i
2ef10 73 20 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65  s enabled if the
2ef20 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75   argument is tru
2ef30 65 0a 2a 2a 20 61 6e 64 20 64 69 73 61 62 6c 65  e.** and disable
2ef40 64 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e  d if the argumen
2ef50 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a  t is false..**.*
2ef60 2a 20 43 61 63 68 65 20 73 68 61 72 69 6e 67 20  * Cache sharing 
2ef70 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 64  is enabled and d
2ef80 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6e 20 65  isabled for an e
2ef90 6e 74 69 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a  ntire process..*
2efa0 2a 20 54 68 69 73 20 69 73 20 61 20 63 68 61 6e  * This is a chan
2efb0 67 65 20 61 73 20 6f 66 20 53 51 4c 69 74 65 20  ge as of SQLite 
2efc0 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49  version 3.5.0. I
2efd0 6e 20 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73  n prior versions
2efe0 20 6f 66 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73   of SQLite,.** s
2eff0 68 61 72 69 6e 67 20 77 61 73 20 65 6e 61 62 6c  haring was enabl
2f000 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66  ed or disabled f
2f010 6f 72 20 65 61 63 68 20 74 68 72 65 61 64 20 73  or each thread s
2f020 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a  eparately..**.**
2f030 20 54 68 65 20 63 61 63 68 65 20 73 68 61 72 69   The cache shari
2f040 6e 67 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74  ng mode set by t
2f050 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 65 66  his interface ef
2f060 66 65 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71  fects all subseq
2f070 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f  uent.** calls to
2f080 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
2f090 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ], [sqlite3_open
2f0a0 5f 76 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c  _v2()], and [sql
2f0b0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a  ite3_open16()]..
2f0c0 2a 2a 20 45 78 69 73 74 69 6e 67 20 64 61 74 61  ** Existing data
2f0d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
2f0e0 20 63 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68   continue use th
2f0f0 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a  e sharing mode.*
2f100 2a 20 74 68 61 74 20 77 61 73 20 69 6e 20 65 66  * that was in ef
2f110 66 65 63 74 20 61 74 20 74 68 65 20 74 69 6d 65  fect at the time
2f120 20 74 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65   they were opene
2f130 64 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c  d..**.** Virtual
2f140 20 74 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62   tables cannot b
2f150 65 20 75 73 65 64 20 77 69 74 68 20 61 20 73 68  e used with a sh
2f160 61 72 65 64 20 63 61 63 68 65 2e 20 20 57 68 65  ared cache.  Whe
2f170 6e 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68  n shared.** cach
2f180 65 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  e is enabled, th
2f190 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  e [sqlite3_creat
2f1a0 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49 20  e_module()] API 
2f1b0 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72  used to register
2f1c0 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c  .** virtual tabl
2f1d0 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72  es will always r
2f1e0 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a  eturn an error..
2f1f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2f200 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  ne returns [SQLI
2f210 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 64  TE_OK] if shared
2f220 20 63 61 63 68 65 20 77 61 73 20 65 6e 61 62 6c   cache was enabl
2f230 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a  ed or disabled.*
2f240 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20  * successfully. 
2f250 20 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   An [error code]
2f260 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 74 68   is returned oth
2f270 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68  erwise..**.** Sh
2f280 61 72 65 64 20 63 61 63 68 65 20 69 73 20 64 69  ared cache is di
2f290 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c  sabled by defaul
2f2a0 74 2e 20 42 75 74 20 74 68 69 73 20 6d 69 67 68  t. But this migh
2f2b0 74 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66  t change in.** f
2f2c0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f  uture releases o
2f2d0 66 20 53 51 4c 69 74 65 2e 20 20 41 70 70 6c 69  f SQLite.  Appli
2f2e0 63 61 74 69 6f 6e 73 20 74 68 61 74 20 63 61 72  cations that car
2f2f0 65 20 61 62 6f 75 74 20 73 68 61 72 65 64 0a 2a  e about shared.*
2f300 2a 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 20  * cache setting 
2f310 73 68 6f 75 6c 64 20 73 65 74 20 69 74 20 65 78  should set it ex
2f320 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20  plicitly..**.** 
2f330 53 65 65 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69  See Also:  [SQLi
2f340 74 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20  te Shared-Cache 
2f350 4d 6f 64 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75  Mode].**.** Requ
2f360 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33  irements: [H1033
2f370 31 5d 20 5b 48 31 30 33 33 36 5d 20 5b 48 31 30  1] [H10336] [H10
2f380 33 33 37 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f  337] [H10339].*/
2f390 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2f3a0 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73  sqlite3_enable_s
2f3b0 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29  hared_cache(int)
2f3c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2f3d0 46 3a 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72  F: Attempt To Fr
2f3e0 65 65 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b  ee Heap Memory {
2f3f0 48 31 37 33 34 30 7d 20 3c 53 33 30 32 32 30 3e  H17340} <S30220>
2f400 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2f410 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
2f420 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74  y() interface at
2f430 74 65 6d 70 74 73 20 74 6f 20 66 72 65 65 20 4e  tempts to free N
2f440 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61   bytes.** of hea
2f450 70 20 6d 65 6d 6f 72 79 20 62 79 20 64 65 61 6c  p memory by deal
2f460 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73  locating non-ess
2f470 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c  ential memory al
2f480 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c  locations.** hel
2f490 64 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73  d by the databas
2f4a0 65 20 6c 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d  e library. {END}
2f4b0 20 20 4d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f    Memory used to
2f4c0 20 63 61 63 68 65 20 64 61 74 61 62 61 73 65 0a   cache database.
2f4d0 2a 2a 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72  ** pages to impr
2f4e0 6f 76 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ove performance 
2f4f0 69 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66  is an example of
2f500 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d   non-essential m
2f510 65 6d 6f 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65  emory..** sqlite
2f520 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
2f530 28 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  () returns the n
2f540 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61  umber of bytes a
2f550 63 74 75 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a  ctually freed,.*
2f560 2a 20 77 68 69 63 68 20 6d 69 67 68 74 20 62 65  * which might be
2f570 20 6d 6f 72 65 20 6f 72 20 6c 65 73 73 20 74 68   more or less th
2f580 61 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65  an the amount re
2f590 71 75 65 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52  quested..**.** R
2f5a0 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31  equirements: [H1
2f5b0 37 33 34 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a  7341] [H17342].*
2f5c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2f5d0 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
2f5e0 5f 6d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f  _memory(int);../
2f5f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49  *.** CAPI3REF: I
2f600 6d 70 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e  mpose A Limit On
2f610 20 48 65 61 70 20 53 69 7a 65 20 7b 48 31 37 33   Heap Size {H173
2f620 35 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a  50} <S30220>.**.
2f630 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73  ** The sqlite3_s
2f640 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29  oft_heap_limit()
2f650 20 69 6e 74 65 72 66 61 63 65 20 70 6c 61 63 65   interface place
2f660 73 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74  s a "soft" limit
2f670 0a 2a 2a 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e  .** on the amoun
2f680 74 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79  t of heap memory
2f690 20 74 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c   that may be all
2f6a0 6f 63 61 74 65 64 20 62 79 20 53 51 4c 69 74 65  ocated by SQLite
2f6b0 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72  ..** If an inter
2f6c0 6e 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  nal allocation i
2f6d0 73 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74  s requested that
2f6e0 20 77 6f 75 6c 64 20 65 78 63 65 65 64 20 74 68   would exceed th
2f6f0 65 0a 2a 2a 20 73 6f 66 74 20 68 65 61 70 20 6c  e.** soft heap l
2f700 69 6d 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72  imit, [sqlite3_r
2f710 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d  elease_memory()]
2f720 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20   is invoked one 
2f730 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73  or.** more times
2f740 20 74 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65   to free up some
2f750 20 73 70 61 63 65 20 62 65 66 6f 72 65 20 74 68   space before th
2f760 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  e allocation is 
2f770 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a  performed..**.**
2f780 20 54 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61   The limit is ca
2f790 6c 6c 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63  lled "soft", bec
2f7a0 61 75 73 65 20 69 66 20 5b 73 71 6c 69 74 65 33  ause if [sqlite3
2f7b0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
2f7c0 29 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65  )].** cannot fre
2f7d0 65 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d  e sufficient mem
2f7e0 6f 72 79 20 74 6f 20 70 72 65 76 65 6e 74 20 74  ory to prevent t
2f7f0 68 65 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65  he limit from be
2f800 69 6e 67 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a  ing exceeded,.**
2f810 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 61   the memory is a
2f820 6c 6c 6f 63 61 74 65 64 20 61 6e 79 77 61 79 20  llocated anyway 
2f830 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  and the current 
2f840 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65  operation procee
2f850 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61  ds..**.** A nega
2f860 74 69 76 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c  tive or zero val
2f870 75 65 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74  ue for N means t
2f880 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hat there is no 
2f890 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20  soft heap limit 
2f8a0 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  and.** [sqlite3_
2f8b0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
2f8c0 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63  ] will only be c
2f8d0 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72  alled when memor
2f8e0 79 20 69 73 20 65 78 68 61 75 73 74 65 64 2e 0a  y is exhausted..
2f8f0 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76  ** The default v
2f900 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73 6f 66  alue for the sof
2f910 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20  t heap limit is 
2f920 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  zero..**.** SQLi
2f930 74 65 20 6d 61 6b 65 73 20 61 20 62 65 73 74 20  te makes a best 
2f940 65 66 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20  effort to honor 
2f950 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69  the soft heap li
2f960 6d 69 74 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74  mit..** But if t
2f970 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
2f980 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e  it cannot be hon
2f990 6f 72 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20  ored, execution 
2f9a0 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65  will.** continue
2f9b0 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f   without error o
2f9c0 72 20 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20  r notification. 
2f9d0 20 54 68 69 73 20 69 73 20 77 68 79 20 74 68 65   This is why the
2f9e0 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c   limit is.** cal
2f9f0 6c 65 64 20 61 20 22 73 6f 66 74 22 20 6c 69 6d  led a "soft" lim
2fa00 69 74 2e 20 20 49 74 20 69 73 20 61 64 76 69 73  it.  It is advis
2fa10 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20  ory only..**.** 
2fa20 50 72 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20  Prior to SQLite 
2fa30 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74  version 3.5.0, t
2fa40 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  his routine only
2fa50 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65   constrained the
2fa60 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63   memory.** alloc
2fa70 61 74 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65  ated by a single
2fa80 20 74 68 72 65 61 64 20 2d 20 74 68 65 20 73 61   thread - the sa
2fa90 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69  me thread in whi
2faa0 63 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  ch this routine.
2fab0 2a 2a 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e  ** runs.  Beginn
2fac0 69 6e 67 20 77 69 74 68 20 53 51 4c 69 74 65 20  ing with SQLite 
2fad0 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74  version 3.5.0, t
2fae0 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
2faf0 69 74 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64  it is.** applied
2fb00 20 74 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e   to all threads.
2fb10 20 54 68 65 20 76 61 6c 75 65 20 73 70 65 63 69   The value speci
2fb20 66 69 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66  fied for the sof
2fb30 74 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20  t heap limit.** 
2fb40 69 73 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e  is an upper boun
2fb50 64 20 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d  d on the total m
2fb60 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
2fb70 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73   for all threads
2fb80 2e 20 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20  . In.** version 
2fb90 33 2e 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e  3.5.0 there is n
2fba0 6f 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20  o mechanism for 
2fbb0 6c 69 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61  limiting the hea
2fbc0 70 20 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69  p usage for.** i
2fbd0 6e 64 69 76 69 64 75 61 6c 20 74 68 72 65 61 64  ndividual thread
2fbe0 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  s..**.** Require
2fbf0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35  ments:.** [H1635
2fc00 31 5d 20 5b 48 31 36 33 35 32 5d 20 5b 48 31 36  1] [H16352] [H16
2fc10 33 35 33 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48  353] [H16354] [H
2fc20 31 36 33 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a  16355] [H16358].
2fc30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
2fc40 69 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f  id sqlite3_soft_
2fc50 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b  heap_limit(int);
2fc60 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2fc70 3a 20 45 78 74 72 61 63 74 20 4d 65 74 61 64 61  : Extract Metada
2fc80 74 61 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d  ta About A Colum
2fc90 6e 20 4f 66 20 41 20 54 61 62 6c 65 20 7b 48 31  n Of A Table {H1
2fca0 32 38 35 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a  2850} <S60300>.*
2fcb0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2fcc0 65 20 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61  e returns metada
2fcd0 74 61 20 61 62 6f 75 74 20 61 20 73 70 65 63 69  ta about a speci
2fce0 66 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20  fic column of a 
2fcf0 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61  specific.** data
2fd00 62 61 73 65 20 74 61 62 6c 65 20 61 63 63 65 73  base table acces
2fd10 73 69 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20  sible using the 
2fd20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
2fd30 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20  tion] handle.** 
2fd40 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
2fd50 72 73 74 20 66 75 6e 63 74 69 6f 6e 20 61 72 67  rst function arg
2fd60 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ument..**.** The
2fd70 20 63 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74   column is ident
2fd80 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63  ified by the sec
2fd90 6f 6e 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66  ond, third and f
2fda0 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 73  ourth parameters
2fdb0 20 74 6f 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63   to.** this func
2fdc0 74 69 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64  tion. The second
2fdd0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 65 69   parameter is ei
2fde0 74 68 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66  ther the name of
2fdf0 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
2fe00 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22   (i.e. "main", "
2fe10 74 65 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61  temp" or an atta
2fe20 63 68 65 64 20 64 61 74 61 62 61 73 65 29 20 63  ched database) c
2fe30 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70  ontaining the sp
2fe40 65 63 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65  ecified.** table
2fe50 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20   or NULL. If it 
2fe60 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c  is NULL, then al
2fe70 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  l attached datab
2fe80 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65  ases are searche
2fe90 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62  d.** for the tab
2fea0 6c 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  le using the sam
2feb0 65 20 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64  e algorithm used
2fec0 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65   by the database
2fed0 20 65 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65   engine to.** re
2fee0 73 6f 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65  solve unqualifie
2fef0 64 20 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63  d table referenc
2ff00 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  es..**.** The th
2ff10 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70  ird and fourth p
2ff20 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69  arameters to thi
2ff30 73 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74  s function are t
2ff40 68 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c  he table and col
2ff50 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74  umn.** name of t
2ff60 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d  he desired colum
2ff70 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
2ff80 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73   Neither of thes
2ff90 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  e parameters.** 
2ffa0 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a  may be NULL..**.
2ffb0 2a 2a 20 4d 65 74 61 64 61 74 61 20 69 73 20 72  ** Metadata is r
2ffc0 65 74 75 72 6e 65 64 20 62 79 20 77 72 69 74 69  eturned by writi
2ffd0 6e 67 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79  ng to the memory
2ffe0 20 6c 6f 63 61 74 69 6f 6e 73 20 70 61 73 73 65   locations passe
2fff0 64 20 61 73 20 74 68 65 20 35 74 68 0a 2a 2a 20  d as the 5th.** 
30000 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 70  and subsequent p
30010 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69  arameters to thi
30020 73 20 66 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20  s function. Any 
30030 6f 66 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e  of these argumen
30040 74 73 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c  ts may be.** NUL
30050 4c 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  L, in which case
30060 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
30070 6e 67 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65  ng element of me
30080 74 61 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65  tadata is omitte
30090 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  d..**.** <blockq
300a0 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20  uote>.** <table 
300b0 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c  border="1">.** <
300c0 74 72 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65  tr><th> Paramete
300d0 72 20 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72  r <th> Output<br
300e0 3e 54 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63  >Type <th>  Desc
300f0 72 69 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74  ription.**.** <t
30100 72 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20  r><td> 5th <td> 
30110 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e  const char* <td>
30120 20 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74   Data type.** <t
30130 72 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20  r><td> 6th <td> 
30140 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e  const char* <td>
30150 20 4e 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74   Name of default
30160 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
30170 6e 63 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  nce.** <tr><td> 
30180 37 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20  7th <td> int    
30190 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69       <td> True i
301a0 66 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e  f column has a N
301b0 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
301c0 6e 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38  nt.** <tr><td> 8
301d0 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20  th <td> int     
301e0 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66      <td> True if
301f0 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20   column is part 
30200 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  of the PRIMARY K
30210 45 59 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39  EY.** <tr><td> 9
30220 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20  th <td> int     
30230 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66      <td> True if
30240 20 63 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f   column is [AUTO
30250 49 4e 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f  INCREMENT].** </
30260 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63  table>.** </bloc
30270 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68  kquote>.**.** Th
30280 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64  e memory pointed
30290 20 74 6f 20 62 79 20 74 68 65 20 63 68 61 72 61   to by the chara
302a0 63 74 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65  cter pointers re
302b0 74 75 72 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a  turned for the.*
302c0 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79  * declaration ty
302d0 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e  pe and collation
302e0 20 73 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c   sequence is val
302f0 69 64 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68  id only until th
30300 65 20 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74  e next.** call t
30310 6f 20 61 6e 79 20 53 51 4c 69 74 65 20 41 50 49  o any SQLite API
30320 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
30330 20 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65   If the specifie
30340 64 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 61  d table is actua
30350 6c 6c 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b  lly a view, an [
30360 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72  error code] is r
30370 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
30380 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  f the specified 
30390 63 6f 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64  column is "rowid
303a0 22 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f  ", "oid" or "_ro
303b0 77 69 64 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20  wid_" and an.** 
303c0 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59  [INTEGER PRIMARY
303d0 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73   KEY] column has
303e0 20 62 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79   been explicitly
303f0 20 64 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20   declared, then 
30400 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61  the output.** pa
30410 72 61 6d 65 74 65 72 73 20 61 72 65 20 73 65 74  rameters are set
30420 20 66 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69   for the explici
30430 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c  tly declared col
30440 75 6d 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73  umn. If there is
30450 20 6e 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c   no.** explicitl
30460 79 20 64 65 63 6c 61 72 65 64 20 5b 49 4e 54 45  y declared [INTE
30470 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d  GER PRIMARY KEY]
30480 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68   column, then th
30490 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61  e output.** para
304a0 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20 61  meters are set a
304b0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
304c0 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61   <pre>.**     da
304d0 74 61 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45  ta type: "INTEGE
304e0 52 22 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74  R".**     collat
304f0 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42  ion sequence: "B
30500 49 4e 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f  INARY".**     no
30510 74 20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20  t null: 0.**    
30520 20 70 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a   primary key: 1.
30530 2a 2a 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72  **     auto incr
30540 65 6d 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72  ement: 0.** </pr
30550 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  e>.**.** This fu
30560 6e 63 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20  nction may load 
30570 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65  one or more sche
30580 6d 61 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73  mas from databas
30590 65 20 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a  e files. If an.*
305a0 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  * error occurs d
305b0 75 72 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65  uring this proce
305c0 73 73 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65  ss, or if the re
305d0 71 75 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72  quested table or
305e0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f   column.** canno
305f0 74 20 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b  t be found, an [
30600 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72  error code] is r
30610 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65  eturned and an e
30620 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6c 65 66  rror message lef
30630 74 0a 2a 2a 20 69 6e 20 74 68 65 20 5b 64 61 74  t.** in the [dat
30640 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
30650 5d 20 28 74 6f 20 62 65 20 72 65 74 72 69 65 76  ] (to be retriev
30660 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
30670 5f 65 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a  _errmsg())..**.*
30680 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e  * This API is on
30690 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20  ly available if 
306a0 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20  the library was 
306b0 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
306c0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41  e.** [SQLITE_ENA
306d0 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44  BLE_COLUMN_METAD
306e0 41 54 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73  ATA] C-preproces
306f0 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e  sor symbol defin
30700 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
30710 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61  I int sqlite3_ta
30720 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64  ble_column_metad
30730 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ata(.  sqlite3 *
30740 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
30750 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e     /* Connection
30760 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
30770 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65  st char *zDbName
30780 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  ,        /* Data
30790 62 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c  base name or NUL
307a0 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  L */.  const cha
307b0 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20  r *zTableName,  
307c0 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65     /* Table name
307d0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
307e0 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20   *zColumnName,  
307f0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65    /* Column name
30800 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   */.  char const
30810 20 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20   **pzDataType,  
30820 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63    /* OUTPUT: Dec
30830 6c 61 72 65 64 20 64 61 74 61 20 74 79 70 65 20  lared data type 
30840 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  */.  char const 
30850 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20  **pzCollSeq,    
30860 20 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c   /* OUTPUT: Coll
30870 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e  ation sequence n
30880 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e  ame */.  int *pN
30890 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20  otNull,         
308a0 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20       /* OUTPUT: 
308b0 54 72 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c  True if NOT NULL
308c0 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73   constraint exis
308d0 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72  ts */.  int *pPr
308e0 69 6d 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20  imaryKey,       
308f0 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54      /* OUTPUT: T
30900 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61  rue if column pa
30910 72 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e  rt of PK */.  in
30920 74 20 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20  t *pAutoinc     
30930 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
30940 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c  PUT: True if col
30950 75 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72  umn is auto-incr
30960 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  ement */.);../*.
30970 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61  ** CAPI3REF: Loa
30980 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b  d An Extension {
30990 48 31 32 36 30 30 7d 20 3c 53 32 30 35 30 30 3e  H12600} <S20500>
309a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
309b0 72 66 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53  rface loads an S
309c0 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20  QLite extension 
309d0 6c 69 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65  library from the
309e0 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a   named file..**.
309f0 2a 2a 20 7b 48 31 32 36 30 31 7d 20 54 68 65 20  ** {H12601} The 
30a00 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74  sqlite3_load_ext
30a10 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61  ension() interfa
30a20 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c  ce attempts to l
30a30 6f 61 64 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20  oad an.**       
30a40 20 20 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73     SQLite extens
30a50 69 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74  ion library cont
30a60 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c  ained in the fil
30a70 65 20 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b  e zFile..**.** {
30a80 48 31 32 36 30 32 7d 20 54 68 65 20 65 6e 74 72  H12602} The entr
30a90 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63  y point is zProc
30aa0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d  ..**.** {H12603}
30ab0 20 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 30 2c   zProc may be 0,
30ac0 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74   in which case t
30ad0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 65  he name of the e
30ae0 6e 74 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20  ntry point.**   
30af0 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 73 20         defaults 
30b00 74 6f 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65  to "sqlite3_exte
30b10 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a  nsion_init"..**.
30b20 2a 2a 20 7b 48 31 32 36 30 34 7d 20 54 68 65 20  ** {H12604} The 
30b30 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74  sqlite3_load_ext
30b40 65 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61  ension() interfa
30b50 63 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a  ce shall return.
30b60 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c  **          [SQL
30b70 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65  ITE_OK] on succe
30b80 73 73 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45  ss and [SQLITE_E
30b90 52 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69  RROR] if somethi
30ba0 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a  ng goes wrong..*
30bb0 2a 0a 2a 2a 20 7b 48 31 32 36 30 35 7d 20 49 66  *.** {H12605} If
30bc0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
30bd0 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73   and pzErrMsg is
30be0 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65   not 0, then the
30bf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
30c00 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
30c10 73 69 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63  sion()] interfac
30c20 65 20 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20  e shall attempt 
30c30 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  to.**          f
30c40 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69  ill *pzErrMsg wi
30c50 74 68 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  th error message
30c60 20 74 65 78 74 20 73 74 6f 72 65 64 20 69 6e 20   text stored in 
30c70 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20  memory.**       
30c80 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d     obtained from
30c90 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
30ca0 28 29 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20  ()]. {END}  The 
30cb0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
30cc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 6f  .**          sho
30cd0 75 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d 65  uld free this me
30ce0 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20  mory by calling 
30cf0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
30d00 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d  ..**.** {H12606}
30d10 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   Extension loadi
30d20 6e 67 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c  ng must be enabl
30d30 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20  ed using.**     
30d40 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e       [sqlite3_en
30d50 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  able_load_extens
30d60 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20  ion()] prior to 
30d70 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49  calling this API
30d80 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 74  ,.**          ot
30d90 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72  herwise an error
30da0 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65   will be returne
30db0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
30dc0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61   int sqlite3_loa
30dd0 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73  d_extension(.  s
30de0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
30df0 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65       /* Load the
30e00 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20   extension into 
30e10 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f  this database co
30e20 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f  nnection */.  co
30e30 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c  nst char *zFile,
30e40 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
30e50 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
30e60 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74  y containing ext
30e70 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ension */.  cons
30e80 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20  t char *zProc,  
30e90 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74    /* Entry point
30ea0 2e 20 20 44 65 72 69 76 65 64 20 66 72 6f 6d 20  .  Derived from 
30eb0 7a 46 69 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20  zFile if 0 */.  
30ec0 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20  char **pzErrMsg 
30ed0 20 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72        /* Put err
30ee0 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20  or message here 
30ef0 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a  if not 0 */.);..
30f00 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
30f10 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c  Enable Or Disabl
30f20 65 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64  e Extension Load
30f30 69 6e 67 20 7b 48 31 32 36 32 30 7d 20 3c 53 32  ing {H12620} <S2
30f40 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61  0500>.**.** So a
30f50 73 20 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65  s not to open se
30f60 63 75 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20  curity holes in 
30f70 6f 6c 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f  older applicatio
30f80 6e 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 75  ns that are.** u
30f90 6e 70 72 65 70 61 72 65 64 20 74 6f 20 64 65 61  nprepared to dea
30fa0 6c 20 77 69 74 68 20 65 78 74 65 6e 73 69 6f 6e  l with extension
30fb0 20 6c 6f 61 64 69 6e 67 2c 20 61 6e 64 20 61 73   loading, and as
30fc0 20 61 20 6d 65 61 6e 73 20 6f 66 20 64 69 73 61   a means of disa
30fd0 62 6c 69 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69  bling.** extensi
30fe0 6f 6e 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65  on loading while
30ff0 20 65 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72   evaluating user
31000 2d 65 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68  -entered SQL, th
31010 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a  e following API.
31020 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20 74  ** is provided t
31030 6f 20 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69  o turn the [sqli
31040 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  te3_load_extensi
31050 6f 6e 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20  on()] mechanism 
31060 6f 6e 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a  on and off..**.*
31070 2a 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  * Extension load
31080 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20 64 65  ing is off by de
31090 66 61 75 6c 74 2e 20 53 65 65 20 74 69 63 6b 65  fault. See ticke
310a0 74 20 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b  t #1863..**.** {
310b0 48 31 32 36 32 31 7d 20 43 61 6c 6c 20 74 68 65  H12621} Call the
310c0 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
310d0 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29  load_extension()
310e0 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e   routine with on
310f0 6f 66 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20  off==1.**       
31100 20 20 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e     to turn exten
31110 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20  sion loading on 
31120 61 6e 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68  and call it with
31130 20 6f 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72   onoff==0 to tur
31140 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74  n.**          it
31150 20 62 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e   back off again.
31160 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20  .**.** {H12622} 
31170 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  Extension loadin
31180 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61  g is off by defa
31190 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ult..*/.SQLITE_A
311a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
311b0 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e  nable_load_exten
311c0 73 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62  sion(sqlite3 *db
311d0 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f  , int onoff);../
311e0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41  *.** CAPI3REF: A
311f0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61  utomatically Loa
31200 64 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20  d An Extensions 
31210 7b 48 31 32 36 34 30 7d 20 3c 53 32 30 35 30 30  {H12640} <S20500
31220 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  >.**.** This API
31230 20 63 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20   can be invoked 
31240 61 74 20 70 72 6f 67 72 61 6d 20 73 74 61 72 74  at program start
31250 75 70 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 72  up in order to r
31260 65 67 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f  egister.** one o
31270 72 20 6d 6f 72 65 20 73 74 61 74 69 63 61 6c 6c  r more staticall
31280 79 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69  y linked extensi
31290 6f 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ons that will be
312a0 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f   available.** to
312b0 20 61 6c 6c 20 6e 65 77 20 5b 64 61 74 61 62 61   all new [databa
312c0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e  se connections].
312d0 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69   {END}.**.** Thi
312e0 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73  s routine stores
312f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
31300 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61  e extension in a
31310 6e 20 61 72 72 61 79 20 74 68 61 74 20 69 73 0a  n array that is.
31320 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ** obtained from
31330 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
31340 28 29 5d 2e 20 20 49 66 20 79 6f 75 20 72 75 6e  ()].  If you run
31350 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63   a memory leak c
31360 68 65 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75  hecker.** on you
31370 72 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74  r program and it
31380 20 72 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20   reports a leak 
31390 62 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 20  because of this 
313a0 61 72 72 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a  array, invoke.**
313b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f   [sqlite3_reset_
313c0 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29  auto_extension()
313d0 5d 20 70 72 69 6f 72 20 74 6f 20 73 68 75 74 64  ] prior to shutd
313e0 6f 77 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20  own to free the 
313f0 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48  memory..**.** {H
31400 31 32 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63  12641} This func
31410 74 69 6f 6e 20 72 65 67 69 73 74 65 72 73 20 61  tion registers a
31420 6e 20 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72  n extension entr
31430 79 20 70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a  y point that is.
31440 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f  **          auto
31450 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65  matically invoke
31460 64 20 77 68 65 6e 65 76 65 72 20 61 20 6e 65 77  d whenever a new
31470 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
31480 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20  ction].**       
31490 20 20 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69     is opened usi
314a0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ng [sqlite3_open
314b0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ()], [sqlite3_op
314c0 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20  en16()],.**     
314d0 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33       or [sqlite3
314e0 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a  _open_v2()]..**.
314f0 2a 2a 20 7b 48 31 32 36 34 32 7d 20 44 75 70 6c  ** {H12642} Dupl
31500 69 63 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73  icate extensions
31510 20 61 72 65 20 64 65 74 65 63 74 65 64 20 73 6f   are detected so
31520 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f   calling this ro
31530 75 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20  utine.**        
31540 20 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73    multiple times
31550 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 65   with the same e
31560 78 74 65 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d  xtension is harm
31570 6c 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  less..**.** {H12
31580 36 34 33 7d 20 54 68 69 73 20 72 6f 75 74 69 6e  643} This routin
31590 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74  e stores a point
315a0 65 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73  er to the extens
315b0 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 0a  ion in an array.
315c0 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 61 74  **          that
315d0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
315e0 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  m [sqlite3_mallo
315f0 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  c()]..**.** {H12
31600 36 34 34 7d 20 41 75 74 6f 6d 61 74 69 63 20 65  644} Automatic e
31610 78 74 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20  xtensions apply 
31620 61 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61  across all threa
31630 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ds..*/.SQLITE_AP
31640 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75  I int sqlite3_au
31650 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69  to_extension(voi
31660 64 20 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29  d (*xEntryPoint)
31670 28 76 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20  (void));../*.** 
31680 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20  CAPI3REF: Reset 
31690 41 75 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73  Automatic Extens
316a0 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32  ion Loading {H12
316b0 36 36 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a  660} <S20500>.**
316c0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
316d0 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70  n disables all p
316e0 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74  reviously regist
316f0 65 72 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a  ered automatic.*
31700 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45  * extensions. {E
31710 4e 44 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74  ND}  It undoes t
31720 68 65 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c  he effect of all
31730 20 70 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74   prior.** [sqlit
31740 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f  e3_auto_extensio
31750 6e 28 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a  n()] calls..**.*
31760 2a 20 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20  * {H12661} This 
31770 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65  function disable
31780 73 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79  s all previously
31790 20 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20   registered.**  
317a0 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69          automati
317b0 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a  c extensions..**
317c0 0a 2a 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69  .** {H12662} Thi
317d0 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62  s function disab
317e0 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78  les automatic ex
317f0 74 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20  tensions in all 
31800 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49  threads..*/.SQLI
31810 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
31820 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65  te3_reset_auto_e
31830 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a  xtension(void);.
31840 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52  ./*.****** EXPER
31850 49 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63  IMENTAL - subjec
31860 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68  t to change with
31870 6f 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a  out notice *****
31880 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
31890 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  The interface to
318a0 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62   the virtual-tab
318b0 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20  le mechanism is 
318c0 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64  currently consid
318d0 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78  ered.** to be ex
318e0 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65  perimental.  The
318f0 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74   interface might
31900 20 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d   change in incom
31910 70 61 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a  patible ways..**
31920 20 49 66 20 74 68 69 73 20 69 73 20 61 20 70 72   If this is a pr
31930 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64  oblem for you, d
31940 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e  o not use the in
31950 74 65 72 66 61 63 65 20 61 74 20 74 68 69 73 20  terface at this 
31960 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  time..**.** When
31970 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62   the virtual-tab
31980 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61  le mechanism sta
31990 62 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c  bilizes, we will
319a0 20 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20   declare the.** 
319b0 69 6e 74 65 72 66 61 63 65 20 66 69 78 65 64 2c  interface fixed,
319c0 20 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65   support it inde
319d0 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65  finitely, and re
319e0 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e  move this commen
319f0 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72  t..*/../*.** Str
31a00 75 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20  uctures used by 
31a10 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
31a20 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74  e interface.*/.t
31a30 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
31a40 6c 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74  lite3_vtab sqlit
31a50 65 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66  e3_vtab;.typedef
31a60 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
31a70 69 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74  index_info sqlit
31a80 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74  e3_index_info;.t
31a90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
31aa0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
31ab0 72 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  r sqlite3_vtab_c
31ac0 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73  ursor;.typedef s
31ad0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f  truct sqlite3_mo
31ae0 64 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64  dule sqlite3_mod
31af0 75 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ule;../*.** CAPI
31b00 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61  3REF: Virtual Ta
31b10 62 6c 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30  ble Object {H180
31b20 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20  00} <S20400>.** 
31b30 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65  KEYWORDS: sqlite
31b40 33 5f 6d 6f 64 75 6c 65 0a 2a 2a 20 45 58 50 45  3_module.** EXPE
31b50 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41  RIMENTAL.**.** A
31b60 20 6d 6f 64 75 6c 65 20 69 73 20 61 20 63 6c 61   module is a cla
31b70 73 73 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61  ss of virtual ta
31b80 62 6c 65 73 2e 20 20 45 61 63 68 20 6d 6f 64 75  bles.  Each modu
31b90 6c 65 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a  le is defined.**
31ba0 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   by an instance 
31bb0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
31bc0 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69   structure.  Thi
31bd0 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73  s structure cons
31be0 69 73 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79 20 6f  ists.** mostly o
31bf0 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 74 68  f methods for th
31c00 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20  e module..**.** 
31c10 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69  This interface i
31c20 73 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61  s experimental a
31c30 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f  nd is subject to
31c40 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65   change or.** re
31c50 6d 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 65 20  moval in future 
31c60 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
31c70 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  te..*/.struct sq
31c80 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20  lite3_module {. 
31c90 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20   int iVersion;. 
31ca0 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28   int (*xCreate)(
31cb0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a  sqlite3*, void *
31cc0 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20  pAux,.          
31cd0 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63       int argc, c
31ce0 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
31cf0 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20  *argv,.         
31d00 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
31d10 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61  ab **ppVTab, cha
31d20 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43  r**);.  int (*xC
31d30 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a  onnect)(sqlite3*
31d40 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20  , void *pAux,.  
31d50 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
31d60 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
31d70 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20  r *const*argv,. 
31d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
31d90 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56  lite3_vtab **ppV
31da0 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20  Tab, char**);.  
31db0 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78  int (*xBestIndex
31dc0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
31dd0 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69  pVTab, sqlite3_i
31de0 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69  ndex_info*);.  i
31df0 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74  nt (*xDisconnect
31e00 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
31e10 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a  pVTab);.  int (*
31e20 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65  xDestroy)(sqlite
31e30 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
31e40 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73    int (*xOpen)(s
31e50 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
31e60 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ab, sqlite3_vtab
31e70 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
31e80 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c  or);.  int (*xCl
31e90 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61  ose)(sqlite3_vta
31ea0 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e  b_cursor*);.  in
31eb0 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c  t (*xFilter)(sql
31ec0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
31ed0 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63  *, int idxNum, c
31ee0 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74  onst char *idxSt
31ef0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
31f00 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c     int argc, sql
31f10 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
31f20 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78  v);.  int (*xNex
31f30 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  t)(sqlite3_vtab_
31f40 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20  cursor*);.  int 
31f50 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f  (*xEof)(sqlite3_
31f60 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20  vtab_cursor*);. 
31f70 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28   int (*xColumn)(
31f80 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
31f90 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f  sor*, sqlite3_co
31fa0 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20  ntext*, int);.  
31fb0 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71  int (*xRowid)(sq
31fc0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
31fd0 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  r*, sqlite3_int6
31fe0 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e  4 *pRowid);.  in
31ff0 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c  t (*xUpdate)(sql
32000 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74  ite3_vtab *, int
32010 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
32020 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  **, sqlite3_int6
32030 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42  4 *);.  int (*xB
32040 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  egin)(sqlite3_vt
32050 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e  ab *pVTab);.  in
32060 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74  t (*xSync)(sqlit
32070 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b  e3_vtab *pVTab);
32080 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74  .  int (*xCommit
32090 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
320a0 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a  pVTab);.  int (*
320b0 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74  xRollback)(sqlit
320c0 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b  e3_vtab *pVTab);
320d0 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75  .  int (*xFindFu
320e0 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f  nction)(sqlite3_
320f0 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74  vtab *pVtab, int
32100 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61   nArg, const cha
32110 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  r *zName,.      
32120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32130 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29   void (**pxFunc)
32140 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
32150 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
32160 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20  lue**),.        
32170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
32180 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20  oid **ppArg);.  
32190 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73  int (*xRename)(s
321a0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
321b0 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
321c0 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  zNew);.};../*.**
321d0 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75   CAPI3REF: Virtu
321e0 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e  al Table Indexin
321f0 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48  g Information {H
32200 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18100} <S20400>.
32210 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c  ** KEYWORDS: sql
32220 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a  ite3_index_info.
32230 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
32240 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
32250 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
32260 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73  ucture and its s
32270 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20  ubstructures is 
32280 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20  used to.** pass 
32290 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f  information into
322a0 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65   and receive the
322b0 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20   reply from the 
322c0 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65  xBestIndex.** me
322d0 74 68 6f 64 20 6f 66 20 61 6e 20 73 71 6c 69 74  thod of an sqlit
322e0 65 33 5f 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20  e3_module.  The 
322f0 66 69 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49  fields under **I
32300 6e 70 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a  nputs** are the.
32310 2a 2a 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65  ** inputs to xBe
32320 73 74 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20  stIndex and are 
32330 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73  read-only.  xBes
32340 74 49 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69  tIndex inserts i
32350 74 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e  ts.** results in
32360 74 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73  to the **Outputs
32370 2a 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a  ** fields..**.**
32380 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74   The aConstraint
32390 5b 5d 20 61 72 72 61 79 20 72 65 63 6f 72 64 73  [] array records
323a0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f   WHERE clause co
323b0 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65  nstraints of the
323c0 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72   form:.**.** <pr
323d0 65 3e 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72  e>column OP expr
323e0 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65  </pre>.**.** whe
323f0 72 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b  re OP is =, &lt;
32400 2c 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f  , &lt;=, &gt;, o
32410 72 20 26 67 74 3b 3d 2e 20 20 54 68 65 20 70 61  r &gt;=.  The pa
32420 72 74 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f  rticular operato
32430 72 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69  r is.** stored i
32440 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e  n aConstraint[].
32450 6f 70 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f  op.  The index o
32460 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  f the column is 
32470 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f  stored in.** aCo
32480 6e 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75  nstraint[].iColu
32490 6d 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74  mn.  aConstraint
324a0 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55  [].usable is TRU
324b0 45 20 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72  E if the.** expr
324c0 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61   on the right-ha
324d0 6e 64 20 73 69 64 65 20 63 61 6e 20 62 65 20 65  nd side can be e
324e0 76 61 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68  valuated (and th
324f0 75 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  us the constrain
32500 74 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20  t.** is usable) 
32510 61 6e 64 20 66 61 6c 73 65 20 69 66 20 69 74 20  and false if it 
32520 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  cannot..**.** Th
32530 65 20 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f  e optimizer auto
32540 6d 61 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74  matically invert
32550 73 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66  s terms of the f
32560 6f 72 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c  orm "expr OP col
32570 75 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65  umn".** and make
32580 73 20 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69  s other simplifi
32590 63 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 57  cations to the W
325a0 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61  HERE clause in a
325b0 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20  n attempt to.** 
325c0 67 65 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52  get as many WHER
325d0 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69  E clause terms i
325e0 6e 74 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f  nto the form sho
325f0 77 6e 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73  wn above as poss
32600 69 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f  ible..** The aCo
32610 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79  nstraint[] array
32620 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48   only reports WH
32630 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73  ERE clause terms
32640 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a   in the correct.
32650 2a 2a 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66  ** form that ref
32660 65 72 20 74 6f 20 74 68 65 20 70 61 72 74 69 63  er to the partic
32670 75 6c 61 72 20 76 69 72 74 75 61 6c 20 74 61 62  ular virtual tab
32680 6c 65 20 62 65 69 6e 67 20 71 75 65 72 69 65 64  le being queried
32690 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74  ..**.** Informat
326a0 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52  ion about the OR
326b0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73  DER BY clause is
326c0 20 73 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65   stored in aOrde
326d0 72 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74  rBy[]..** Each t
326e0 65 72 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20  erm of aOrderBy 
326f0 72 65 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e  records a column
32700 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
32710 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54   clause..**.** T
32720 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65  he xBestIndex me
32730 74 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61  thod must fill a
32740 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
32750 5d 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69  ] with informati
32760 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74  on.** about what
32770 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70   parameters to p
32780 61 73 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20  ass to xFilter. 
32790 20 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20   If argvIndex>0 
327a0 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68  then.** the righ
327b0 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t-hand side of t
327c0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
327d0 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69   aConstraint[] i
327e0 73 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61  s evaluated.** a
327f0 6e 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61  nd becomes the a
32800 72 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72  rgvIndex-th entr
32810 79 20 69 6e 20 61 72 67 76 2e 20 20 49 66 20 61  y in argv.  If a
32820 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
32830 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 75  ].omit.** is tru
32840 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73  e, then the cons
32850 74 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d 65  traint is assume
32860 64 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 61  d to be fully ha
32870 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  ndled by the.** 
32880 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e  virtual table an
32890 64 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64  d is not checked
328a0 20 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74 65   again by SQLite
328b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78 4e  ..**.** The idxN
328c0 75 6d 20 61 6e 64 20 69 64 78 50 74 72 20 76 61  um and idxPtr va
328d0 6c 75 65 73 20 61 72 65 20 72 65 63 6f 72 64 65  lues are recorde
328e0 64 20 61 6e 64 20 70 61 73 73 65 64 20 69 6e 74  d and passed int
328f0 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 20 73 71  o xFilter..** sq
32900 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 73 20  lite3_free() is 
32910 75 73 65 64 20 74 6f 20 66 72 65 65 20 69 64 78  used to free idx
32920 50 74 72 20 69 66 20 6e 65 65 64 54 6f 46 72 65  Ptr if needToFre
32930 65 49 64 78 50 74 72 20 69 73 20 74 72 75 65 2e  eIdxPtr is true.
32940 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72  .**.** The order
32950 42 79 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73  ByConsumed means
32960 20 74 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f   that output fro
32970 6d 20 78 46 69 6c 74 65 72 20 77 69 6c 6c 20 6f  m xFilter will o
32980 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63  ccur in.** the c
32990 6f 72 72 65 63 74 20 6f 72 64 65 72 20 74 6f 20  orrect order to 
329a0 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45  satisfy the ORDE
329b0 52 20 42 59 20 63 6c 61 75 73 65 20 73 6f 20 74  R BY clause so t
329c0 68 61 74 20 6e 6f 20 73 65 70 61 72 61 74 65 0a  hat no separate.
329d0 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 70 20  ** sorting step 
329e0 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a  is required..**.
329f0 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74 65 64  ** The estimated
32a00 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20 61 6e  Cost value is an
32a10 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65   estimate of the
32a20 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 20 74   cost of doing t
32a30 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72  he.** particular
32a40 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c   lookup.  A full
32a50 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62 6c 65   scan of a table
32a60 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65 73 20   with N entries 
32a70 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61  should have.** a
32a80 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41 20 62   cost of N.  A b
32a90 69 6e 61 72 79 20 73 65 61 72 63 68 20 6f 66 20  inary search of 
32aa0 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74  a table of N ent
32ab0 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65  ries should have
32ac0 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70   a.** cost of ap
32ad0 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28  proximately log(
32ae0 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  N)..**.** This i
32af0 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65  nterface is expe
32b00 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20  rimental and is 
32b10 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
32b20 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20  e or.** removal 
32b30 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
32b40 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f  es of SQLite..*/
32b50 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
32b60 69 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f  index_info {.  /
32b70 2a 20 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e  * Inputs */.  in
32b80 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20  t nConstraint;  
32b90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
32ba0 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
32bb0 20 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a   aConstraint */.
32bc0 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
32bd0 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
32be0 74 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f  t {.     int iCo
32bf0 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20  lumn;           
32c00 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20     /* Column on 
32c10 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
32c20 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  f constraint */.
32c30 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68       unsigned ch
32c40 61 72 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f  ar op;         /
32c50 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65  * Constraint ope
32c60 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e  rator */.     un
32c70 73 69 67 6e 65 64 20 63 68 61 72 20 75 73 61 62  signed char usab
32c80 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  le;     /* True 
32c90 69 66 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  if this constrai
32ca0 6e 74 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a  nt is usable */.
32cb0 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66       int iTermOf
32cc0 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f  fset;          /
32cd0 2a 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  * Used internall
32ce0 79 20 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73  y - xBestIndex s
32cf0 68 6f 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a  hould ignore */.
32d00 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74    } *aConstraint
32d10 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
32d20 54 61 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63  Table of WHERE c
32d30 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74  lause constraint
32d40 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65  s */.  int nOrde
32d50 72 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  rBy;            
32d60 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
32d70 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45  erms in the ORDE
32d80 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  R BY clause */. 
32d90 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
32da0 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a  index_orderby {.
32db0 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e       int iColumn
32dc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
32dd0 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  * Column number 
32de0 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64  */.     unsigned
32df0 20 63 68 61 72 20 64 65 73 63 3b 20 20 20 20 20   char desc;     
32e00 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45    /* True for DE
32e10 53 43 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 41  SC.  False for A
32e20 53 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64  SC. */.  } *aOrd
32e30 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 20 20  erBy;           
32e40 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
32e50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
32e60 2f 2a 20 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20  /* Outputs */.  
32e70 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
32e80 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f  ndex_constraint_
32e90 75 73 61 67 65 20 7b 0a 20 20 20 20 69 6e 74 20  usage {.    int 
32ea0 61 72 67 76 49 6e 64 65 78 3b 20 20 20 20 20 20  argvIndex;      
32eb0 20 20 20 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63       /* if >0, c
32ec0 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 70 61 72  onstraint is par
32ed0 74 20 6f 66 20 61 72 67 76 20 74 6f 20 78 46 69  t of argv to xFi
32ee0 6c 74 65 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69  lter */.    unsi
32ef0 67 6e 65 64 20 63 68 61 72 20 6f 6d 69 74 3b 20  gned char omit; 
32f00 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63       /* Do not c
32f10 6f 64 65 20 61 20 74 65 73 74 20 66 6f 72 20 74  ode a test for t
32f20 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a  his constraint *
32f30 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69  /.  } *aConstrai
32f40 6e 74 55 73 61 67 65 3b 0a 20 20 69 6e 74 20 69  ntUsage;.  int i
32f50 64 78 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20  dxNum;          
32f60 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
32f70 75 73 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79  used to identify
32f80 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20   the index */.  
32f90 63 68 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20  char *idxStr;   
32fa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
32fb0 72 69 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f  ring, possibly o
32fc0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
32fd0 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20  ite3_malloc */. 
32fe0 20 69 6e 74 20 6e 65 65 64 54 6f 46 72 65 65 49   int needToFreeI
32ff0 64 78 53 74 72 3b 20 20 20 20 20 20 2f 2a 20 46  dxStr;      /* F
33000 72 65 65 20 69 64 78 53 74 72 20 75 73 69 6e 67  ree idxStr using
33010 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20   sqlite3_free() 
33020 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74  if true */.  int
33030 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64   orderByConsumed
33040 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
33050 69 66 20 6f 75 74 70 75 74 20 69 73 20 61 6c 72  if output is alr
33060 65 61 64 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a  eady ordered */.
33070 20 20 64 6f 75 62 6c 65 20 65 73 74 69 6d 61 74    double estimat
33080 65 64 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20  edCost;      /* 
33090 45 73 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f  Estimated cost o
330a0 66 20 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64  f using this ind
330b0 65 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65  ex */.};.#define
330c0 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
330d0 4e 53 54 52 41 49 4e 54 5f 45 51 20 20 20 20 32  NSTRAINT_EQ    2
330e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
330f0 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
33100 5f 47 54 20 20 20 20 34 0a 23 64 65 66 69 6e 65  _GT    4.#define
33110 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
33120 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 38  NSTRAINT_LE    8
33130 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
33140 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
33150 5f 4c 54 20 20 20 20 31 36 0a 23 64 65 66 69 6e  _LT    16.#defin
33160 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
33170 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 20 20 20  ONSTRAINT_GE    
33180 33 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  32.#define SQLIT
33190 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
331a0 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a  NT_MATCH 64../*.
331b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67  ** CAPI3REF: Reg
331c0 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20  ister A Virtual 
331d0 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61  Table Implementa
331e0 74 69 6f 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53  tion {H18200} <S
331f0 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49  20400>.** EXPERI
33200 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  MENTAL.**.** Thi
33210 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
33220 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 61 20  d to register a 
33230 6e 65 77 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  new module name 
33240 77 69 74 68 20 61 0a 2a 2a 20 5b 64 61 74 61 62  with a.** [datab
33250 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
33260 20 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d    Module names m
33270 75 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65  ust be registere
33280 64 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61  d before.** crea
33290 74 69 6e 67 20 6e 65 77 20 76 69 72 74 75 61 6c  ting new virtual
332a0 20 74 61 62 6c 65 73 20 6f 6e 20 74 68 65 20 6d   tables on the m
332b0 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65  odule, or before
332c0 20 75 73 69 6e 67 0a 2a 2a 20 70 72 65 65 78 69   using.** preexi
332d0 73 74 69 6e 67 20 76 69 72 74 75 61 6c 20 74 61  sting virtual ta
332e0 62 6c 65 73 20 6f 66 20 74 68 65 20 6d 6f 64 75  bles of the modu
332f0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  le..**.** This i
33300 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65  nterface is expe
33310 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20  rimental and is 
33320 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
33330 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20  e or.** removal 
33340 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
33350 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f  es of SQLite..*/
33360 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
33370 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
33380 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
33390 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c  te_module(.  sql
333a0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
333b0 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74          /* SQLit
333c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  e connection to 
333d0 72 65 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20  register module 
333e0 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  with */.  const 
333f0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
33400 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
33410 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
33420 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
33430 64 75 6c 65 20 2a 2c 20 20 20 20 2f 2a 20 4d 65  dule *,    /* Me
33440 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f  thods for the mo
33450 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  dule */.  void *
33460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33470 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64       /* Client d
33480 61 74 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f  ata for xCreate/
33490 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a  xConnect */.);..
334a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
334b0 52 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75  Register A Virtu
334c0 61 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65  al Table Impleme
334d0 6e 74 61 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d  ntation {H18210}
334e0 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50   <S20400>.** EXP
334f0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
33500 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
33510 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65  identical to the
33520 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
33530 5f 6d 6f 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f  _module()] metho
33540 64 20 61 62 6f 76 65 2c 0a 2a 2a 20 65 78 63 65  d above,.** exce
33550 70 74 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 77  pt that it allow
33560 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66  s a destructor f
33570 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 73 70  unction to be sp
33580 65 63 69 66 69 65 64 2e 20 49 74 20 69 73 0a 2a  ecified. It is.*
33590 2a 20 65 76 65 6e 20 6d 6f 72 65 20 65 78 70 65  * even more expe
335a0 72 69 6d 65 6e 74 61 6c 20 74 68 61 6e 20 74 68  rimental than th
335b0 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 76 69  e rest of the vi
335c0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 41 50 49  rtual tables API
335d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
335e0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
335f0 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TAL int sqlite3_
33600 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32  create_module_v2
33610 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
33620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33630 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74  * SQLite connect
33640 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20  ion to register 
33650 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a 20  module with */. 
33660 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
33670 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  me,         /* N
33680 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  ame of the modul
33690 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e */.  const sql
336a0 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20 20  ite3_module *,  
336b0 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72    /* Methods for
336c0 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20   the module */. 
336d0 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20   void *,        
336e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
336f0 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20 78  lient data for x
33700 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20  Create/xConnect 
33710 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65 73 74  */.  void(*xDest
33720 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20 20 20  roy)(void*)     
33730 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75  /* Module destru
33740 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ctor function */
33750 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
33760 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62  REF: Virtual Tab
33770 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62 6a 65  le Instance Obje
33780 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53 32 30  ct {H18010} <S20
33790 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  400>.** KEYWORDS
337a0 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 0a 2a  : sqlite3_vtab.*
337b0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
337c0 2a 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75 6c  *.** Every modul
337d0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
337e0 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 73 73   uses a subclass
337f0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
33800 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74  g structure.** t
33810 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72  o describe a par
33820 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 65  ticular instance
33830 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20   of the module. 
33840 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 77   Each subclass w
33850 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72  ill.** be tailor
33860 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 66  ed to the specif
33870 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20  ic needs of the 
33880 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
33890 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75  ation..** The pu
338a0 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75  rpose of this su
338b0 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64  perclass is to d
338c0 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69  efine certain fi
338d0 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a  elds that are.**
338e0 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d   common to all m
338f0 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
33900 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72  tions..**.** Vir
33910 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68  tual tables meth
33920 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65  ods can set an e
33930 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20  rror message by 
33940 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73  assigning a.** s
33950 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66  tring obtained f
33960 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72  rom [sqlite3_mpr
33970 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d  intf()] to zErrM
33980 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20  sg.  The method 
33990 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63  should.** take c
339a0 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 69  are that any pri
339b0 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 65  or string is fre
339c0 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  ed by a call to 
339d0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
339e0 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73  .** prior to ass
339f0 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72  igning a new str
33a00 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20  ing to zErrMsg. 
33a10 20 41 66 74 65 72 20 74 68 65 20 65 72 72 6f 72   After the error
33a20 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64   message.** is d
33a30 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 74  elivered up to t
33a40 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63  he client applic
33a50 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e  ation, the strin
33a60 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  g will be automa
33a70 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64  tically.** freed
33a80 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65   by sqlite3_free
33a90 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d  () and the zErrM
33aa0 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65  sg field will be
33ab0 20 7a 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a 2a   zeroed.  Note.*
33ac0 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 6d  * that sqlite3_m
33ad0 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c  printf() and sql
33ae0 69 74 65 33 5f 66 72 65 65 28 29 20 61 72 65 20  ite3_free() are 
33af0 75 73 65 64 20 6f 6e 20 74 68 65 20 7a 45 72 72  used on the zErr
33b00 4d 73 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69 6e  Msg field.** sin
33b10 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ce virtual table
33b20 73 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 69  s are commonly i
33b30 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c 6f  mplemented in lo
33b40 61 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e  adable extension
33b50 73 20 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e 6f  s which.** do no
33b60 74 20 68 61 76 65 20 61 63 63 65 73 73 20 74 6f  t have access to
33b70 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
33b80 29 20 6f 72 20 73 71 6c 69 74 65 33 46 72 65 65  ) or sqlite3Free
33b90 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ()..**.** This i
33ba0 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65  nterface is expe
33bb0 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20  rimental and is 
33bc0 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
33bd0 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20  e or.** removal 
33be0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
33bf0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f  es of SQLite..*/
33c00 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
33c10 76 74 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73  vtab {.  const s
33c20 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
33c30 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20  Module;  /* The 
33c40 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20  module for this 
33c50 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
33c60 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
33c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c80 20 20 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72     /* Used inter
33c90 6e 61 6c 6c 79 20 2a 2f 0a 20 20 63 68 61 72 20  nally */.  char 
33ca0 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20  *zErrMsg;       
33cb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72             /* Er
33cc0 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d  ror message from
33cd0 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
33ce0 28 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75  () */.  /* Virtu
33cf0 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  al table impleme
33d00 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79  ntations will ty
33d10 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69  pically add addi
33d20 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f  tional fields */
33d30 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .};../*.** CAPI3
33d40 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62  REF: Virtual Tab
33d50 6c 65 20 43 75 72 73 6f 72 20 4f 62 6a 65 63 74  le Cursor Object
33d60 20 20 7b 48 31 38 30 32 30 7d 20 3c 53 32 30 34    {H18020} <S204
33d70 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  00>.** KEYWORDS:
33d80 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
33d90 72 73 6f 72 0a 2a 2a 20 45 58 50 45 52 49 4d 45  rsor.** EXPERIME
33da0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79  NTAL.**.** Every
33db0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
33dc0 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75  tation uses a su
33dd0 62 63 6c 61 73 73 20 6f 66 20 74 68 65 20 66 6f  bclass of the fo
33de0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
33df0 65 0a 2a 2a 20 74 6f 20 64 65 73 63 72 69 62 65  e.** to describe
33e00 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f   cursors that po
33e10 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 76 69 72  int into the vir
33e20 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 61  tual table and a
33e30 72 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f  re used.** to lo
33e40 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 76  op through the v
33e50 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 43  irtual table.  C
33e60 75 72 73 6f 72 73 20 61 72 65 20 63 72 65 61 74  ursors are creat
33e70 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  ed using the.** 
33e80 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20  xOpen method of 
33e90 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63  the module.  Eac
33ea0 68 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  h module impleme
33eb0 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66  ntation will def
33ec0 69 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65  ine.** the conte
33ed0 6e 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73  nt of a cursor s
33ee0 74 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74  tructure to suit
33ef0 20 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a   its own needs..
33f00 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72  **.** This super
33f10 63 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20  class exists in 
33f20 6f 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20  order to define 
33f30 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75  fields of the cu
33f40 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65  rsor that.** are
33f50 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69   common to all i
33f60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a  mplementations..
33f70 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
33f80 66 61 63 65 20 69 73 20 65 78 70 65 72 69 6d 65  face is experime
33f90 6e 74 61 6c 20 61 6e 64 20 69 73 20 73 75 62 6a  ntal and is subj
33fa0 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72  ect to change or
33fb0 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e 20 66  .** removal in f
33fc0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f  uture releases o
33fd0 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 72  f SQLite..*/.str
33fe0 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62  uct sqlite3_vtab
33ff0 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  _cursor {.  sqli
34000 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
34010 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c        /* Virtual
34020 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63   table of this c
34030 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56 69  ursor */.  /* Vi
34040 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c  rtual table impl
34050 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c  ementations will
34060 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61   typically add a
34070 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
34080 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.};../*.** CA
34090 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 20  PI3REF: Declare 
340a0 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41 20  The Schema Of A 
340b0 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48  Virtual Table {H
340c0 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18280} <S20400>.
340d0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
340e0 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74  **.** The xCreat
340f0 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20 6d  e and xConnect m
34100 65 74 68 6f 64 73 20 6f 66 20 61 20 6d 6f 64 75  ethods of a modu
34110 6c 65 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  le use the follo
34120 77 69 6e 67 20 41 50 49 0a 2a 2a 20 74 6f 20 64  wing API.** to d
34130 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61  eclare the forma
34140 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64  t (the names and
34150 20 64 61 74 61 74 79 70 65 73 20 6f 66 20 74 68   datatypes of th
34160 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a  e columns) of.**
34170 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
34180 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65  les they impleme
34190 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  nt..**.** This i
341a0 6e 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65  nterface is expe
341b0 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20  rimental and is 
341c0 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
341d0 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20  e or.** removal 
341e0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
341f0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f  es of SQLite..*/
34200 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
34210 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
34220 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  int sqlite3_decl
34230 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33  are_vtab(sqlite3
34240 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
34250 43 72 65 61 74 65 54 61 62 6c 65 29 3b 0a 0a 2f  CreateTable);../
34260 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
34270 76 65 72 6c 6f 61 64 20 41 20 46 75 6e 63 74 69  verload A Functi
34280 6f 6e 20 46 6f 72 20 41 20 56 69 72 74 75 61 6c  on For A Virtual
34290 20 54 61 62 6c 65 20 7b 48 31 38 33 30 30 7d 20   Table {H18300} 
342a0 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45  <S20400>.** EXPE
342b0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 56  RIMENTAL.**.** V
342c0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61  irtual tables ca
342d0 6e 20 70 72 6f 76 69 64 65 20 61 6c 74 65 72 6e  n provide altern
342e0 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61  ative implementa
342f0 74 69 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f  tions of functio
34300 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  ns.** using the 
34310 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65  xFindFunction me
34320 74 68 6f 64 2e 20 20 42 75 74 20 67 6c 6f 62 61  thod.  But globa
34330 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  l versions of th
34340 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ose functions.**
34350 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f   must exist in o
34360 72 64 65 72 20 74 6f 20 62 65 20 6f 76 65 72 6c  rder to be overl
34370 6f 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  oaded..**.** Thi
34380 73 20 41 50 49 20 6d 61 6b 65 73 20 73 75 72 65  s API makes sure
34390 20 61 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f   a global versio
343a0 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20  n of a function 
343b0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
343c0 72 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75  r.** name and nu
343d0 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65  mber of paramete
343e0 72 73 20 65 78 69 73 74 73 2e 20 20 49 66 20 6e  rs exists.  If n
343f0 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20  o such function 
34400 65 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65  exists.** before
34410 20 74 68 69 73 20 41 50 49 20 69 73 20 63 61 6c   this API is cal
34420 6c 65 64 2c 20 61 20 6e 65 77 20 66 75 6e 63 74  led, a new funct
34430 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 2e 20  ion is created. 
34440 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
34450 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65  ion.** of the ne
34460 77 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79  w function alway
34470 73 20 63 61 75 73 65 73 20 61 6e 20 65 78 63 65  s causes an exce
34480 70 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f  ption to be thro
34490 77 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e  wn.  So.** the n
344a0 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e  ew function is n
344b0 6f 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74  ot good for anyt
344c0 68 69 6e 67 20 62 79 20 69 74 73 65 6c 66 2e 20  hing by itself. 
344d0 20 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72   Its only.** pur
344e0 70 6f 73 65 20 69 73 20 74 6f 20 62 65 20 61 20  pose is to be a 
344f0 70 6c 61 63 65 68 6f 6c 64 65 72 20 66 75 6e 63  placeholder func
34500 74 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65  tion that can be
34510 20 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62   overloaded.** b
34520 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  y virtual tables
34530 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  ..**.** This API
34540 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69   should be consi
34550 64 65 72 65 64 20 70 61 72 74 20 6f 66 20 74 68  dered part of th
34560 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
34570 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 77 68  interface,.** wh
34580 69 63 68 20 69 73 20 65 78 70 65 72 69 6d 65 6e  ich is experimen
34590 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20  tal and subject 
345a0 74 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 53 51  to change..*/.SQ
345b0 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
345c0 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74  EXPERIMENTAL int
345d0 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
345e0 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74  d_function(sqlit
345f0 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  e3*, const char 
34600 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20  *zFuncName, int 
34610 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  nArg);../*.** Th
34620 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
34630 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
34640 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66 69 6e   mechanism defin
34650 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b 20 75  ed above (back u
34660 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d 65 6e  p.** to a commen
34670 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73 69 6d  t remarkably sim
34680 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65  ilar to this one
34690 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63  ) is currently c
346a0 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20  onsidered.** to 
346b0 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e  be experimental.
346c0 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20    The interface 
346d0 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20  might change in 
346e0 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79  incompatible way
346f0 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73  s..** If this is
34700 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79   a problem for y
34710 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74  ou, do not use t
34720 68 65 20 69 6e 74 65 72 66 61 63 65 20 61 74 20  he interface at 
34730 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  this time..**.**
34740 20 57 68 65 6e 20 74 68 65 20 76 69 72 74 75 61   When the virtua
34750 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73  l-table mechanis
34760 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65  m stabilizes, we
34770 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68   will declare th
34780 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66  e.** interface f
34790 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74  ixed, support it
347a0 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61   indefinitely, a
347b0 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63  nd remove this c
347c0 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  omment..**.*****
347d0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d  * EXPERIMENTAL -
347e0 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
347f0 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63  ge without notic
34800 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e **************
34810 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .*/../*.** CAPI3
34820 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20 54 6f  REF: A Handle To
34830 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48   An Open BLOB {H
34840 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30 3e 0a  17800} <S30230>.
34850 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 42 4c  ** KEYWORDS: {BL
34860 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c 4f 42  OB handle} {BLOB
34870 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a 2a 20   handles}.**.** 
34880 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
34890 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65  his object repre
348a0 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 42 4c  sents an open BL
348b0 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 5b  OB on which.** [
348c0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
348d0 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20  n | incremental 
348e0 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 62 65  BLOB I/O] can be
348f0 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f   performed..** O
34900 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 20 74  bjects of this t
34910 79 70 65 20 61 72 65 20 63 72 65 61 74 65 64 20  ype are created 
34920 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  by [sqlite3_blob
34930 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20  _open()].** and 
34940 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71  destroyed by [sq
34950 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
34960 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  ()]..** The [sql
34970 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29  ite3_blob_read()
34980 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62  ] and [sqlite3_b
34990 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69 6e 74  lob_write()] int
349a0 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e 20 62  erfaces.** can b
349b0 65 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f  e used to read o
349c0 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20 73 75  r write small su
349d0 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74 68 65  bsections of the
349e0 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 5b 73   BLOB..** The [s
349f0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65  qlite3_blob_byte
34a00 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72  s()] interface r
34a10 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20  eturns the size 
34a20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e 20 62  of the BLOB in b
34a30 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ytes..*/.typedef
34a40 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
34a50 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62 6c 6f  blob sqlite3_blo
34a60 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  b;../*.** CAPI3R
34a70 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 42 20  EF: Open A BLOB 
34a80 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20  For Incremental 
34a90 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c 53 33  I/O {H17810} <S3
34aa0 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0230>.**.** This
34ab0 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70 65 6e   interfaces open
34ac0 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  s a [BLOB handle
34ad0 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20 74 68   | handle] to th
34ae0 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64 0a 2a  e BLOB located.*
34af0 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c 20 63  * in row iRow, c
34b00 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74  olumn zColumn, t
34b10 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e 20 64  able zTable in d
34b20 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a 2a 20  atabase zDb;.** 
34b30 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  in other words, 
34b40 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20 74 68  the same BLOB th
34b50 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65  at would be sele
34b60 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c  cted by:.**.** <
34b70 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45 4c 45  pre>.**     SELE
34b80 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20  CT zColumn FROM 
34b90 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45 52 45  zDb.zTable WHERE
34ba0 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f 77 3b   [rowid] = iRow;
34bb0 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e 44 7d  .** </pre> {END}
34bc0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6c  .**.** If the fl
34bd0 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 69 73  ags parameter is
34be0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 74   non-zero, the t
34bf0 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65  he BLOB is opene
34c00 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a 20 61 6e  d for read.** an
34c10 64 20 77 72 69 74 65 20 61 63 63 65 73 73 2e 20  d write access. 
34c20 49 66 20 69 74 20 69 73 20 7a 65 72 6f 2c 20 74  If it is zero, t
34c30 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65  he BLOB is opene
34c40 64 20 66 6f 72 20 72 65 61 64 20 61 63 63 65 73  d for read acces
34c50 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  s..**.** Note th
34c60 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  at the database 
34c70 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 65 20  name is not the 
34c80 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20 63 6f  filename that co
34c90 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 64 61  ntains.** the da
34ca0 74 61 62 61 73 65 20 62 75 74 20 72 61 74 68 65  tabase but rathe
34cb0 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e  r the symbolic n
34cc0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
34cd0 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 61  ase that.** is a
34ce0 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74 68 65  ssigned when the
34cf0 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 6e   database is con
34d00 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b 41 54  nected using [AT
34d10 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20 74 68  TACH]..** For th
34d20 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
34d30 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 62 61  file, the databa
34d40 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61 69 6e  se name is "main
34d50 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50 20 74  "..** For TEMP t
34d60 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74 61 62  ables, the datab
34d70 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74 65 6d  ase name is "tem
34d80 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  p"..**.** On suc
34d90 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b  cess, [SQLITE_OK
34da0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
34db0 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f 42 20  d the new [BLOB 
34dc0 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 74 74  handle] is writt
34dd0 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62  en.** to *ppBlob
34de0 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b  . Otherwise an [
34df0 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72  error code] is r
34e00 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 79 20  eturned and any 
34e10 76 61 6c 75 65 20 77 72 69 74 74 65 6e 0a 2a 2a  value written.**
34e20 20 74 6f 20 2a 70 70 42 6c 6f 62 20 73 68 6f 75   to *ppBlob shou
34e30 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 20 62  ld not be used b
34e40 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a  y the caller..**
34e50 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
34e60 65 74 73 20 74 68 65 20 5b 64 61 74 61 62 61 73  ets the [databas
34e70 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 65 72  e connection] er
34e80 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d 65 73  ror code and mes
34e90 73 61 67 65 0a 2a 2a 20 61 63 63 65 73 73 69 62  sage.** accessib
34ea0 6c 65 20 76 69 61 20 5b 73 71 6c 69 74 65 33 5f  le via [sqlite3_
34eb0 65 72 72 63 6f 64 65 28 29 5d 20 61 6e 64 20 5b  errcode()] and [
34ec0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
34ed0 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  ]..**.** If the 
34ee0 72 6f 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20  row that a BLOB 
34ef0 68 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f  handle points to
34f00 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20   is modified by 
34f10 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20  an.** [UPDATE], 
34f20 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20  [DELETE], or by 
34f30 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69  [ON CONFLICT] si
34f40 64 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68  de-effects.** th
34f50 65 6e 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64  en the BLOB hand
34f60 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  le is marked as 
34f70 22 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68  "expired"..** Th
34f80 69 73 20 69 73 20 74 72 75 65 20 69 66 20 61 6e  is is true if an
34f90 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  y column of the 
34fa0 72 6f 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20  row is changed, 
34fb0 65 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a  even a column.**
34fc0 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20   other than the 
34fd0 6f 6e 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e  one the BLOB han
34fe0 64 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a  dle is open on..
34ff0 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  ** Calls to [sql
35000 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29  ite3_blob_read()
35010 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62  ] and [sqlite3_b
35020 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72  lob_write()] for
35030 0a 2a 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c  .** a expired BL
35040 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77  OB handle fail w
35050 69 74 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f  ith an return co
35060 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42  de of [SQLITE_AB
35070 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73  ORT]..** Changes
35080 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20   written into a 
35090 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68  BLOB prior to th
350a0 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20  e BLOB expiring 
350b0 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62  are not.** rollb
350c0 61 63 6b 20 62 79 20 74 68 65 20 65 78 70 69 72  ack by the expir
350d0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f  ation of the BLO
350e0 42 2e 20 20 53 75 63 68 20 63 68 61 6e 67 65 73  B.  Such changes
350f0 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79   will eventually
35100 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68  .** commit if th
35110 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
35120 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c  ntinues to compl
35130 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  etion..**.** Req
35140 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
35150 31 37 38 31 33 5d 20 5b 48 31 37 38 31 34 5d 20  17813] [H17814] 
35160 5b 48 31 37 38 31 36 5d 20 5b 48 31 37 38 31 39  [H17816] [H17819
35170 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48 31 37 38  ] [H17821] [H178
35180 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  24].*/.SQLITE_AP
35190 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
351a0 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74  ob_open(.  sqlit
351b0 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  e3*,.  const cha
351c0 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20  r *zDb,.  const 
351d0 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20  char *zTable,.  
351e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
351f0 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69  umn,.  sqlite3_i
35200 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74  nt64 iRow,.  int
35210 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65   flags,.  sqlite
35220 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a  3_blob **ppBlob.
35230 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
35240 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42  EF: Close A BLOB
35250 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d   Handle {H17830}
35260 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
35270 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b  Closes an open [
35280 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a  BLOB handle]..**
35290 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c  .** Closing a BL
352a0 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74  OB shall cause t
352b0 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
352c0 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74  action to commit
352d0 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 65  .** if there are
352e0 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c   no other BLOBs,
352f0 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 72 65 70   no pending prep
35300 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2c  ared statements,
35310 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 61   and the.** data
35320 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
35330 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69  is in [autocommi
35340 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61  t mode]..** If a
35350 6e 79 20 77 72 69 74 65 73 20 77 65 72 65 20 6d  ny writes were m
35360 61 64 65 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c  ade to the BLOB,
35370 20 74 68 65 79 20 6d 69 67 68 74 20 62 65 20 68   they might be h
35380 65 6c 64 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20  eld in cache.** 
35390 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f 73 65 20  until the close 
353a0 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 74 68 65  operation if the
353b0 79 20 77 69 6c 6c 20 66 69 74 2e 20 7b 45 4e 44  y will fit. {END
353c0 7d 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20  }.**.** Closing 
353d0 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66  the BLOB often f
353e0 6f 72 63 65 73 20 74 68 65 20 63 68 61 6e 67 65  orces the change
353f0 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b  s.** out to disk
35400 20 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49   and so if any I
35410 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c  /O errors occur,
35420 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c   they will likel
35430 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68  y occur.** at th
35440 65 20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20  e time when the 
35450 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20  BLOB is closed. 
35460 20 7b 48 31 37 38 33 33 7d 20 41 6e 79 20 65 72   {H17833} Any er
35470 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72 20  rors that occur 
35480 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69 6e  during.** closin
35490 67 20 61 72 65 20 72 65 70 6f 72 74 65 64 20 61  g are reported a
354a0 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74  s a non-zero ret
354b0 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  urn value..**.**
354c0 20 54 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f   The BLOB is clo
354d0 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61  sed unconditiona
354e0 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 20 74 68  lly.  Even if th
354f0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
35500 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63  ns.** an error c
35510 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 73  ode, the BLOB is
35520 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a 2a   still closed..*
35530 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
35540 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d 20 5b  s:.** [H17833] [
35550 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 39 5d  H17836] [H17839]
35560 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
35570 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
35580 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c  close(sqlite3_bl
35590 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ob *);../*.** CA
355a0 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e 20 54  PI3REF: Return T
355b0 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70  he Size Of An Op
355c0 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d  en BLOB {H17840}
355d0 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
355e0 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65  Returns the size
355f0 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65   in bytes of the
35600 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 6c 65   BLOB accessible
35610 20 76 69 61 20 74 68 65 20 6f 70 65 6e 0a 2a 2a   via the open.**
35620 20 5b 5d 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20   []BLOB handle] 
35630 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75  in its only argu
35640 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ment..**.** Requ
35650 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
35660 37 38 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  7843].*/.SQLITE_
35670 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
35680 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74  blob_bytes(sqlit
35690 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a  e3_blob *);../*.
356a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 61  ** CAPI3REF: Rea
356b0 64 20 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c  d Data From A BL
356c0 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79  OB Incrementally
356d0 20 7b 48 31 37 38 35 30 7d 20 3c 53 33 30 32 33   {H17850} <S3023
356e0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  0>.**.** This fu
356f0 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
35700 6f 20 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d  o read data from
35710 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68   an open [BLOB h
35720 61 6e 64 6c 65 5d 20 69 6e 74 6f 20 61 0a 2a 2a  andle] into a.**
35730 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64   caller-supplied
35740 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73   buffer. N bytes
35750 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70   of data are cop
35760 69 65 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20  ied into buffer 
35770 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70  Z.** from the op
35780 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e  en BLOB, startin
35790 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66  g at offset iOff
357a0 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 66  set..**.** If of
357b0 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20  fset iOffset is 
357c0 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65  less than N byte
357d0 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f  s from the end o
357e0 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b  f the BLOB,.** [
357f0 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73  SQLITE_ERROR] is
35800 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f   returned and no
35810 20 64 61 74 61 20 69 73 20 72 65 61 64 2e 20 20   data is read.  
35820 49 66 20 4e 20 6f 72 20 69 4f 66 66 73 65 74 20  If N or iOffset 
35830 69 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20  is.** less than 
35840 7a 65 72 6f 2c 20 5b 53 51 4c 49 54 45 5f 45 52  zero, [SQLITE_ER
35850 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ROR] is returned
35860 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20   and no data is 
35870 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61  read..**.** An a
35880 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 66  ttempt to read f
35890 72 6f 6d 20 61 6e 20 65 78 70 69 72 65 64 20 5b  rom an expired [
358a0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69  BLOB handle] fai
358b0 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72  ls with an.** er
358c0 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c  ror code of [SQL
358d0 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a  ITE_ABORT]..**.*
358e0 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51  * On success, SQ
358f0 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
35900 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ned..** Otherwis
35910 65 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64  e, an [error cod
35920 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64  e] or an [extend
35930 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  ed error code] i
35940 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
35950 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
35960 2a 2a 20 5b 48 31 37 38 35 33 5d 20 5b 48 31 37  ** [H17853] [H17
35970 38 35 36 5d 20 5b 48 31 37 38 35 39 5d 20 5b 48  856] [H17859] [H
35980 31 37 38 36 32 5d 20 5b 48 31 37 38 36 33 5d 20  17862] [H17863] 
35990 5b 48 31 37 38 36 35 5d 20 5b 48 31 37 38 36 38  [H17865] [H17868
359a0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
359b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
359c0 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c  _read(sqlite3_bl
359d0 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69  ob *, void *Z, i
359e0 6e 74 20 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65  nt N, int iOffse
359f0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
35a00 52 45 46 3a 20 57 72 69 74 65 20 44 61 74 61 20  REF: Write Data 
35a10 49 6e 74 6f 20 41 20 42 4c 4f 42 20 49 6e 63 72  Into A BLOB Incr
35a20 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 37  ementally {H1787
35a30 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a  0} <S30230>.**.*
35a40 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
35a50 69 73 20 75 73 65 64 20 74 6f 20 77 72 69 74 65  is used to write
35a60 20 64 61 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70   data into an op
35a70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  en [BLOB handle]
35a80 20 66 72 6f 6d 20 61 0a 2a 2a 20 63 61 6c 6c 65   from a.** calle
35a90 72 2d 73 75 70 70 6c 69 65 64 20 62 75 66 66 65  r-supplied buffe
35aa0 72 2e 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61  r. N bytes of da
35ab0 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66 72  ta are copied fr
35ac0 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 5a 0a  om the buffer Z.
35ad0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e  ** into the open
35ae0 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20   BLOB, starting 
35af0 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65  at offset iOffse
35b00 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
35b10 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 70 61  [BLOB handle] pa
35b20 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
35b30 74 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 6e  t argument was n
35b40 6f 74 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a  ot opened for.**
35b50 20 77 72 69 74 69 6e 67 20 28 74 68 65 20 66 6c   writing (the fl
35b60 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ags parameter to
35b70 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
35b80 70 65 6e 28 29 5d 20 77 61 73 20 7a 65 72 6f 29  pen()] was zero)
35b90 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69  ,.** this functi
35ba0 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  on returns [SQLI
35bb0 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a  TE_READONLY]..**
35bc0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
35bd0 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66  n may only modif
35be0 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  y the contents o
35bf0 66 20 74 68 65 20 42 4c 4f 42 3b 20 69 74 20 69  f the BLOB; it i
35c00 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c  s.** not possibl
35c10 65 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68  e to increase th
35c20 65 20 73 69 7a 65 20 6f 66 20 61 20 42 4c 4f 42  e size of a BLOB
35c30 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 2e   using this API.
35c40 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f  .** If offset iO
35c50 66 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 68  ffset is less th
35c60 61 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20  an N bytes from 
35c70 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42  the end of the B
35c80 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  LOB,.** [SQLITE_
35c90 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e  ERROR] is return
35ca0 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69  ed and no data i
35cb0 73 20 77 72 69 74 74 65 6e 2e 20 20 49 66 20 4e  s written.  If N
35cc0 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e   is.** less than
35cd0 20 7a 65 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52   zero [SQLITE_ER
35ce0 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ROR] is returned
35cf0 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20   and no data is 
35d00 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 41  written..**.** A
35d10 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69  n attempt to wri
35d20 74 65 20 74 6f 20 61 6e 20 65 78 70 69 72 65 64  te to an expired
35d30 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66   [BLOB handle] f
35d40 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20  ails with an.** 
35d50 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53  error code of [S
35d60 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57  QLITE_ABORT].  W
35d70 72 69 74 65 73 20 74 6f 20 74 68 65 20 42 4c 4f  rites to the BLO
35d80 42 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 0a  B that occurred.
35d90 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 5b 42  ** before the [B
35da0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 70 69  LOB handle] expi
35db0 72 65 64 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c  red are not roll
35dc0 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 0a 2a  ed back by the.*
35dd0 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20  * expiration of 
35de0 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75  the handle, thou
35df0 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74 68 6f  gh of course tho
35e00 73 65 20 63 68 61 6e 67 65 73 20 6d 69 67 68 74  se changes might
35e10 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 76  .** have been ov
35e20 65 72 77 72 69 74 74 65 6e 20 62 79 20 74 68 65  erwritten by the
35e30 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
35e40 65 78 70 69 72 65 64 20 74 68 65 20 42 4c 4f 42  expired the BLOB
35e50 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79   handle.** or by
35e60 20 6f 74 68 65 72 20 69 6e 64 65 70 65 6e 64 65   other independe
35e70 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  nt statements..*
35e80 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c  *.** On success,
35e90 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
35ea0 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72  turned..** Other
35eb0 77 69 73 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72  wise, an  [error
35ec0 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78   code] or an [ex
35ed0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
35ee0 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  e] is returned..
35ef0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
35f00 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 37 33 5d 20  ts:.** [H17873] 
35f10 5b 48 31 37 38 37 34 5d 20 5b 48 31 37 38 37 35  [H17874] [H17875
35f20 5d 20 5b 48 31 37 38 37 36 5d 20 5b 48 31 37 38  ] [H17876] [H178
35f30 37 37 5d 20 5b 48 31 37 38 37 39 5d 20 5b 48 31  77] [H17879] [H1
35f40 37 38 38 32 5d 20 5b 48 31 37 38 38 35 5d 0a 2a  7882] [H17885].*
35f50 2a 20 5b 48 31 37 38 38 38 5d 0a 2a 2f 0a 53 51  * [H17888].*/.SQ
35f60 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
35f70 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
35f80 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20  sqlite3_blob *, 
35f90 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69  const void *z, i
35fa0 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65  nt n, int iOffse
35fb0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
35fc0 52 45 46 3a 20 56 69 72 74 75 61 6c 20 46 69 6c  REF: Virtual Fil
35fd0 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 63 74 73  e System Objects
35fe0 20 7b 48 31 31 32 30 30 7d 20 3c 53 32 30 31 30   {H11200} <S2010
35ff0 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75  0>.**.** A virtu
36000 61 6c 20 66 69 6c 65 73 79 73 74 65 6d 20 28 56  al filesystem (V
36010 46 53 29 20 69 73 20 61 6e 20 5b 73 71 6c 69 74  FS) is an [sqlit
36020 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 0a 2a  e3_vfs] object.*
36030 2a 20 74 68 61 74 20 53 51 4c 69 74 65 20 75 73  * that SQLite us
36040 65 73 20 74 6f 20 69 6e 74 65 72 61 63 74 0a 2a  es to interact.*
36050 2a 20 77 69 74 68 20 74 68 65 20 75 6e 64 65 72  * with the under
36060 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  lying operating 
36070 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74 20 53 51  system.  Most SQ
36080 4c 69 74 65 20 62 75 69 6c 64 73 20 63 6f 6d 65  Lite builds come
36090 20 77 69 74 68 20 61 0a 2a 2a 20 73 69 6e 67 6c   with a.** singl
360a0 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 74 68  e default VFS th
360b0 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  at is appropriat
360c0 65 20 66 6f 72 20 74 68 65 20 68 6f 73 74 20 63  e for the host c
360d0 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e 65 77 20  omputer..** New 
360e0 56 46 53 65 73 20 63 61 6e 20 62 65 20 72 65 67  VFSes can be reg
360f0 69 73 74 65 72 65 64 20 61 6e 64 20 65 78 69 73  istered and exis
36100 74 69 6e 67 20 56 46 53 65 73 20 63 61 6e 20 62  ting VFSes can b
36110 65 20 75 6e 72 65 67 69 73 74 65 72 65 64 2e 0a  e unregistered..
36120 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
36130 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20   interfaces are 
36140 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20  provided..**.** 
36150 54 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  The sqlite3_vfs_
36160 66 69 6e 64 28 29 20 69 6e 74 65 72 66 61 63 65  find() interface
36170 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
36180 65 72 20 74 6f 20 61 20 56 46 53 20 67 69 76 65  er to a VFS give
36190 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a 2a 20 4e  n its name..** N
361a0 61 6d 65 73 20 61 72 65 20 63 61 73 65 20 73 65  ames are case se
361b0 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e 61 6d 65  nsitive..** Name
361c0 73 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69  s are zero-termi
361d0 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69  nated UTF-8 stri
361e0 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  ngs..** If there
361f0 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 61 20   is no match, a 
36200 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20  NULL pointer is 
36210 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20  returned..** If 
36220 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e 55 4c 4c  zVfsName is NULL
36230 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75 6c   then the defaul
36240 74 20 56 46 53 20 69 73 20 72 65 74 75 72 6e 65  t VFS is returne
36250 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 56 46 53  d..**.** New VFS
36260 65 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65  es are registere
36270 64 20 77 69 74 68 20 73 71 6c 69 74 65 33 5f 76  d with sqlite3_v
36280 66 73 5f 72 65 67 69 73 74 65 72 28 29 2e 0a 2a  fs_register()..*
36290 2a 20 45 61 63 68 20 6e 65 77 20 56 46 53 20 62  * Each new VFS b
362a0 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75  ecomes the defau
362b0 6c 74 20 56 46 53 20 69 66 20 74 68 65 20 6d 61  lt VFS if the ma
362c0 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 73 20 73  keDflt flag is s
362d0 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20  et..** The same 
362e0 56 46 53 20 63 61 6e 20 62 65 20 72 65 67 69 73  VFS can be regis
362f0 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74  tered multiple t
36300 69 6d 65 73 20 77 69 74 68 6f 75 74 20 69 6e 6a  imes without inj
36310 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 6b 65 20  ury..** To make 
36320 61 6e 20 65 78 69 73 74 69 6e 67 20 56 46 53 20  an existing VFS 
36330 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74  into the default
36340 20 56 46 53 2c 20 72 65 67 69 73 74 65 72 20 69   VFS, register i
36350 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 74 68 20  t again.** with 
36360 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61  the makeDflt fla
36370 67 20 73 65 74 2e 20 20 49 66 20 74 77 6f 20 64  g set.  If two d
36380 69 66 66 65 72 65 6e 74 20 56 46 53 65 73 20 77  ifferent VFSes w
36390 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20  ith the.** same 
363a0 6e 61 6d 65 20 61 72 65 20 72 65 67 69 73 74 65  name are registe
363b0 72 65 64 2c 20 74 68 65 20 62 65 68 61 76 69 6f  red, the behavio
363c0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20  r is undefined. 
363d0 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 69 73 20   If a.** VFS is 
363e0 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20  registered with 
363f0 61 20 6e 61 6d 65 20 74 68 61 74 20 69 73 20 4e  a name that is N
36400 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79 20  ULL or an empty 
36410 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 65 6e 20  string,.** then 
36420 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
36430 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
36440 20 55 6e 72 65 67 69 73 74 65 72 20 61 20 56 46   Unregister a VF
36450 53 20 77 69 74 68 20 74 68 65 20 73 71 6c 69 74  S with the sqlit
36460 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
36470 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  r() interface..*
36480 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74  * If the default
36490 20 56 46 53 20 69 73 20 75 6e 72 65 67 69 73 74   VFS is unregist
364a0 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 20 56 46  ered, another VF
364b0 53 20 69 73 20 63 68 6f 73 65 6e 20 61 73 0a 2a  S is chosen as.*
364c0 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20  * the default.  
364d0 54 68 65 20 63 68 6f 69 63 65 20 66 6f 72 20 74  The choice for t
364e0 68 65 20 6e 65 77 20 56 46 53 20 69 73 20 61 72  he new VFS is ar
364f0 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 52  bitrary..**.** R
36500 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
36510 5b 48 31 31 32 30 33 5d 20 5b 48 31 31 32 30 36  [H11203] [H11206
36520 5d 20 5b 48 31 31 32 30 39 5d 20 5b 48 31 31 32  ] [H11209] [H112
36530 31 32 5d 20 5b 48 31 31 32 31 35 5d 20 5b 48 31  12] [H11215] [H1
36540 31 32 31 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  1218].*/.SQLITE_
36550 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66 73 20  API sqlite3_vfs 
36560 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e  *sqlite3_vfs_fin
36570 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56  d(const char *zV
36580 66 73 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f  fsName);.SQLITE_
36590 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
365a0 76 66 73 5f 72 65 67 69 73 74 65 72 28 73 71 6c  vfs_register(sql
365b0 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6d  ite3_vfs*, int m
365c0 61 6b 65 44 66 6c 74 29 3b 0a 53 51 4c 49 54 45  akeDflt);.SQLITE
365d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
365e0 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28  _vfs_unregister(
365f0 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b 0a 0a  sqlite3_vfs*);..
36600 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
36610 4d 75 74 65 78 65 73 20 7b 48 31 37 30 30 30 7d  Mutexes {H17000}
36620 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20000>.**.** 
36630 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20  The SQLite core 
36640 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74 69  uses these routi
36650 6e 65 73 20 66 6f 72 20 74 68 72 65 61 64 0a 2a  nes for thread.*
36660 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f  * synchronizatio
36670 6e 2e 20 54 68 6f 75 67 68 20 74 68 65 79 20 61  n. Though they a
36680 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  re intended for 
36690 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20  internal.** use 
366a0 62 79 20 53 51 4c 69 74 65 2c 20 63 6f 64 65 20  by SQLite, code 
366b0 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e  that links again
366c0 73 74 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20  st SQLite is.** 
366d0 70 65 72 6d 69 74 74 65 64 20 74 6f 20 75 73 65  permitted to use
366e0 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 6f   any of these ro
366f0 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  utines..**.** Th
36700 65 20 53 51 4c 69 74 65 20 73 6f 75 72 63 65 20  e SQLite source 
36710 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73 20 6d 75  code contains mu
36720 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  ltiple implement
36730 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 65  ations.** of the
36740 73 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65  se mutex routine
36750 73 2e 20 20 41 6e 20 61 70 70 72 6f 70 72 69 61  s.  An appropria
36760 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  te implementatio
36770 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63 74 65 64  n.** is selected
36780 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61   automatically a
36790 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20  t compile-time. 
367a0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   The following.*
367b0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
367c0 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20  s are available 
367d0 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f  in the SQLite co
367e0 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  re:.**.** <ul>.*
367f0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f  * <li>   SQLITE_
36800 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20 3c 6c 69  MUTEX_OS2.** <li
36810 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  >   SQLITE_MUTEX
36820 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c 6c 69 3e  _PTHREAD.** <li>
36830 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f     SQLITE_MUTEX_
36840 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51  W32.** <li>   SQ
36850 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a  LITE_MUTEX_NOOP.
36860 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
36870 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  he SQLITE_MUTEX_
36880 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e 74 61 74  NOOP implementat
36890 69 6f 6e 20 69 73 20 61 20 73 65 74 20 6f 66 20  ion is a set of 
368a0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 74 68 61 74  routines.** that
368b0 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c 20 6c 6f   does no real lo
368c0 63 6b 69 6e 67 20 61 6e 64 20 69 73 20 61 70 70  cking and is app
368d0 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65  ropriate for use
368e0 20 69 6e 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 2d   in.** a single-
368f0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
36900 74 69 6f 6e 2e 20 20 54 68 65 20 53 51 4c 49 54  tion.  The SQLIT
36910 45 5f 4d 55 54 45 58 5f 4f 53 32 2c 0a 2a 2a 20  E_MUTEX_OS2,.** 
36920 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48  SQLITE_MUTEX_PTH
36930 52 45 41 44 2c 20 61 6e 64 20 53 51 4c 49 54 45  READ, and SQLITE
36940 5f 4d 55 54 45 58 5f 57 33 32 20 69 6d 70 6c 65  _MUTEX_W32 imple
36950 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 72  mentations.** ar
36960 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  e appropriate fo
36970 72 20 75 73 65 20 6f 6e 20 4f 53 2f 32 2c 20 55  r use on OS/2, U
36980 6e 69 78 2c 20 61 6e 64 20 57 69 6e 64 6f 77 73  nix, and Windows
36990 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74  ..**.** If SQLit
369a0 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
369b0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55  th the SQLITE_MU
369c0 54 45 58 5f 41 50 50 44 45 46 20 70 72 65 70 72  TEX_APPDEF prepr
369d0 6f 63 65 73 73 6f 72 0a 2a 2a 20 6d 61 63 72 6f  ocessor.** macro
369e0 20 64 65 66 69 6e 65 64 20 28 77 69 74 68 20 22   defined (with "
369f0 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41  -DSQLITE_MUTEX_A
36a00 50 50 44 45 46 3d 31 22 29 2c 20 74 68 65 6e 20  PPDEF=1"), then 
36a10 6e 6f 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c  no mutex.** impl
36a20 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 69 6e  ementation is in
36a30 63 6c 75 64 65 64 20 77 69 74 68 20 74 68 65 20  cluded with the 
36a40 6c 69 62 72 61 72 79 2e 20 49 6e 20 74 68 69 73  library. In this
36a50 20 63 61 73 65 20 74 68 65 0a 2a 2a 20 61 70 70   case the.** app
36a60 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75  lication must su
36a70 70 70 6c 79 20 61 20 63 75 73 74 6f 6d 20 6d 75  pply a custom mu
36a80 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
36a90 6f 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  on using the.** 
36aa0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
36ab0 55 54 45 58 5d 20 6f 70 74 69 6f 6e 20 6f 66 20  UTEX] option of 
36ac0 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  the sqlite3_conf
36ad0 69 67 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  ig() function.**
36ae0 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
36af0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
36b00 7a 65 28 29 20 6f 72 20 61 6e 79 20 6f 74 68 65  ze() or any othe
36b10 72 20 70 75 62 6c 69 63 20 73 71 6c 69 74 65 33  r public sqlite3
36b20 5f 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68  _.** function th
36b30 61 74 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33  at calls sqlite3
36b40 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a  _initialize()..*
36b50 2a 0a 2a 2a 20 7b 48 31 37 30 31 31 7d 20 54 68  *.** {H17011} Th
36b60 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
36b70 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20  alloc() routine 
36b80 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a  allocates a new.
36b90 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74  ** mutex and ret
36ba0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
36bb0 6f 20 69 74 2e 20 7b 48 31 37 30 31 32 7d 20 49  o it. {H17012} I
36bc0 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c  f it returns NUL
36bd0 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20  L.** that means 
36be0 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75  that a mutex cou
36bf0 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61  ld not be alloca
36c00 74 65 64 2e 20 7b 48 31 37 30 31 33 7d 20 53 51  ted. {H17013} SQ
36c10 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77  Lite.** will unw
36c20 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e  ind its stack an
36c30 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  d return an erro
36c40 72 2e 20 7b 48 31 37 30 31 34 7d 20 54 68 65 20  r. {H17014} The 
36c50 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73  argument.** to s
36c60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
36c70 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74  oc() is one of t
36c80 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
36c90 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75  stants:.**.** <u
36ca0 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  l>.** <li>  SQLI
36cb0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a  TE_MUTEX_FAST.**
36cc0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
36cd0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
36ce0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
36cf0 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
36d00 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  R.** <li>  SQLIT
36d10 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
36d20 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  EM.** <li>  SQLI
36d30 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
36d40 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  MEM2.** <li>  SQ
36d50 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
36d60 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20  C_PRNG.** <li>  
36d70 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
36d80 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20  TIC_LRU.** <li> 
36d90 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
36da0 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75  ATIC_LRU2.** </u
36db0 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 35  l>.**.** {H17015
36dc0 7d 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20  } The first two 
36dd0 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20  constants cause 
36de0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
36df0 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a  loc() to create.
36e00 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20  ** a new mutex. 
36e10 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69   The new mutex i
36e20 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e  s recursive when
36e30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
36e40 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73  CURSIVE.** is us
36e50 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73  ed but not neces
36e60 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53  sarily so when S
36e70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
36e80 20 69 73 20 75 73 65 64 2e 20 7b 45 4e 44 7d 0a   is used. {END}.
36e90 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70  ** The mutex imp
36ea0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73  lementation does
36eb0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b   not need to mak
36ec0 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a  e a distinction.
36ed0 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54  ** between SQLIT
36ee0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
36ef0 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54  E and SQLITE_MUT
36f00 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f  EX_FAST if it do
36f10 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74  es.** not want t
36f20 6f 2e 20 20 7b 48 31 37 30 31 36 7d 20 42 75 74  o.  {H17016} But
36f30 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c   SQLite will onl
36f40 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63 75  y request a recu
36f50 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a  rsive mutex in.*
36f60 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69 74  * cases where it
36f70 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e   really needs on
36f80 65 2e 20 20 7b 45 4e 44 7d 20 49 66 20 61 20 66  e.  {END} If a f
36f90 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73  aster non-recurs
36fa0 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70  ive mutex.** imp
36fb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
36fc0 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
36fd0 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74  host platform, t
36fe0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
36ff0 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75  em.** might retu
37000 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20  rn such a mutex 
37010 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53  in response to S
37020 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
37030 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 37 7d  ..**.** {H17017}
37040 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77   The other allow
37050 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  ed parameters to
37060 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
37070 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75  lloc() each retu
37080 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
37090 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65  to a static pree
370a0 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 7b  xisting mutex. {
370b0 45 4e 44 7d 20 20 46 6f 75 72 20 73 74 61 74 69  END}  Four stati
370c0 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a  c mutexes are.**
370d0 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72   used by the cur
370e0 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20  rent version of 
370f0 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20  SQLite.  Future 
37100 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
37110 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64  te.** may add ad
37120 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20  ditional static 
37130 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63  mutexes.  Static
37140 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72   mutexes are for
37150 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65   internal.** use
37160 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e   by SQLite only.
37170 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74    Applications t
37180 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d  hat use SQLite m
37190 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a  utexes should.**
371a0 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79   use only the dy
371b0 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65  namic mutexes re
371c0 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45  turned by SQLITE
371d0 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a  _MUTEX_FAST or.*
371e0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  * SQLITE_MUTEX_R
371f0 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20  ECURSIVE..**.** 
37200 7b 48 31 37 30 31 38 7d 20 4e 6f 74 65 20 74 68  {H17018} Note th
37210 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65  at if one of the
37220 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70   dynamic mutex p
37230 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54  arameters (SQLIT
37240 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20  E_MUTEX_FAST.** 
37250 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  or SQLITE_MUTEX_
37260 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73  RECURSIVE) is us
37270 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  ed then sqlite3_
37280 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a  mutex_alloc().**
37290 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65   returns a diffe
372a0 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76  rent mutex on ev
372b0 65 72 79 20 63 61 6c 6c 2e 20 20 7b 48 31 37 30  ery call.  {H170
372c0 33 34 7d 20 42 75 74 20 66 6f 72 20 74 68 65 20  34} But for the 
372d0 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20  static.** mutex 
372e0 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20  types, the same 
372f0 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65  mutex is returne
37300 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20  d on every call 
37310 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20  that has.** the 
37320 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72  same type number
37330 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 39 7d  ..**.** {H17019}
37340 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
37350 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74 69 6e  ex_free() routin
37360 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
37370 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c  previously.** al
37380 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69 63 20  located dynamic 
37390 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 30 7d 20  mutex. {H17020} 
373a0 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
373b0 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
373c0 65 76 65 72 79 0a 2a 2a 20 64 79 6e 61 6d 69 63  every.** dynamic
373d0 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61   mutex that it a
373e0 6c 6c 6f 63 61 74 65 73 2e 20 7b 41 31 37 30 32  llocates. {A1702
373f0 31 7d 20 54 68 65 20 64 79 6e 61 6d 69 63 20 6d  1} The dynamic m
37400 75 74 65 78 65 73 20 6d 75 73 74 20 6e 6f 74 20  utexes must not 
37410 62 65 20 69 6e 0a 2a 2a 20 75 73 65 20 77 68 65  be in.** use whe
37420 6e 20 74 68 65 79 20 61 72 65 20 64 65 61 6c 6c  n they are deall
37430 6f 63 61 74 65 64 2e 20 7b 41 31 37 30 32 32 7d  ocated. {A17022}
37440 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 64   Attempting to d
37450 65 61 6c 6c 6f 63 61 74 65 20 61 20 73 74 61 74  eallocate a stat
37460 69 63 0a 2a 2a 20 6d 75 74 65 78 20 72 65 73 75  ic.** mutex resu
37470 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64  lts in undefined
37480 20 62 65 68 61 76 69 6f 72 2e 20 7b 48 31 37 30   behavior. {H170
37490 32 33 7d 20 53 51 4c 69 74 65 20 6e 65 76 65 72  23} SQLite never
374a0 20 64 65 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20   deallocates.** 
374b0 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 2e 20  a static mutex. 
374c0 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20  {END}.**.** The 
374d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
374e0 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ter() and sqlite
374f0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
37500 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a  utines attempt.*
37510 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74  * to enter a mut
37520 65 78 2e 20 7b 48 31 37 30 32 34 7d 20 49 66 20  ex. {H17024} If 
37530 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
37540 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  s already within
37550 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73   the mutex,.** s
37560 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
37570 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20  er() will block 
37580 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
37590 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74  x_try() will ret
375a0 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  urn.** SQLITE_BU
375b0 53 59 2e 20 7b 48 31 37 30 32 35 7d 20 20 54 68  SY. {H17025}  Th
375c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
375d0 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20  try() interface 
375e0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
375f0 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63  OK].** upon succ
37600 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 7b  essful entry.  {
37610 48 31 37 30 32 36 7d 20 4d 75 74 65 78 65 73 20  H17026} Mutexes 
37620 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a  created using.**
37630 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
37640 43 55 52 53 49 56 45 20 63 61 6e 20 62 65 20 65  CURSIVE can be e
37650 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20  ntered multiple 
37660 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d  times by the sam
37670 65 20 74 68 72 65 61 64 2e 0a 2a 2a 20 7b 48 31  e thread..** {H1
37680 37 30 32 37 7d 20 49 6e 20 73 75 63 68 20 63 61  7027} In such ca
37690 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65  ses the,.** mute
376a0 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64  x must be exited
376b0 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72   an equal number
376c0 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65   of times before
376d0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a   another thread.
376e0 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 7b  ** can enter.  {
376f0 41 31 37 30 32 38 7d 20 49 66 20 74 68 65 20 73  A17028} If the s
37700 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73  ame thread tries
37710 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74   to enter any ot
37720 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 6d  her.** kind of m
37730 75 74 65 78 20 6d 6f 72 65 20 74 68 61 6e 20 6f  utex more than o
37740 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f  nce, the behavio
37750 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  r is undefined..
37760 2a 2a 20 7b 48 31 37 30 32 39 7d 20 53 51 4c 69  ** {H17029} SQLi
37770 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 65 78  te will never ex
37780 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 20 62 65  hibit.** such be
37790 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20 6f 77  havior in its ow
377a0 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78 65 73  n use of mutexes
377b0 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73  ..**.** Some sys
377c0 74 65 6d 73 20 28 66 6f 72 20 65 78 61 6d 70 6c  tems (for exampl
377d0 65 2c 20 57 69 6e 64 6f 77 73 20 39 35 29 20 64  e, Windows 95) d
377e0 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68  o not support th
377f0 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 69  e operation.** i
37800 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 73 71  mplemented by sq
37810 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
37820 29 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79 73  ).  On those sys
37830 74 65 6d 73 2c 20 73 71 6c 69 74 65 33 5f 6d 75  tems, sqlite3_mu
37840 74 65 78 5f 74 72 79 28 29 0a 2a 2a 20 77 69 6c  tex_try().** wil
37850 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  l always return 
37860 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 7b 48  SQLITE_BUSY.  {H
37870 31 37 30 33 30 7d 20 54 68 65 20 53 51 4c 69 74  17030} The SQLit
37880 65 20 63 6f 72 65 20 6f 6e 6c 79 20 65 76 65 72  e core only ever
37890 20 75 73 65 73 0a 2a 2a 20 73 71 6c 69 74 65 33   uses.** sqlite3
378a0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 61 73 20  _mutex_try() as 
378b0 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  an optimization 
378c0 73 6f 20 74 68 69 73 20 69 73 20 61 63 63 65 70  so this is accep
378d0 74 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a  table behavior..
378e0 2a 2a 0a 2a 2a 20 7b 48 31 37 30 33 31 7d 20 54  **.** {H17031} T
378f0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
37900 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65  _leave() routine
37910 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74   exits a mutex t
37920 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69  hat was.** previ
37930 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79  ously entered by
37940 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
37950 2e 20 20 7b 41 31 37 30 33 32 7d 20 54 68 65 20  .  {A17032} The 
37960 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75  behavior.** is u
37970 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
37980 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72  mutex is not cur
37990 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 62  rently entered b
379a0 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67  y the.** calling
379b0 20 74 68 72 65 61 64 20 6f 72 20 69 73 20 6e 6f   thread or is no
379c0 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  t currently allo
379d0 63 61 74 65 64 2e 20 20 7b 48 31 37 30 33 33 7d  cated.  {H17033}
379e0 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20   SQLite will.** 
379f0 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e  never do either.
37a00 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20   {END}.**.** If 
37a10 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  the argument to 
37a20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
37a30 74 65 72 28 29 2c 20 73 71 6c 69 74 65 33 5f 6d  ter(), sqlite3_m
37a40 75 74 65 78 5f 74 72 79 28 29 2c 20 6f 72 0a 2a  utex_try(), or.*
37a50 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
37a60 6c 65 61 76 65 28 29 20 69 73 20 61 20 4e 55 4c  leave() is a NUL
37a70 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20  L pointer, then 
37a80 61 6c 6c 20 74 68 72 65 65 20 72 6f 75 74 69 6e  all three routin
37a90 65 73 0a 2a 2a 20 62 65 68 61 76 65 20 61 73 20  es.** behave as 
37aa0 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  no-ops..**.** Se
37ab0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
37ac0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 61  _mutex_held()] a
37ad0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  nd [sqlite3_mute
37ae0 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 2e 0a 2a 2f  x_notheld()]..*/
37af0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
37b00 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74  te3_mutex *sqlit
37b10 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 69  e3_mutex_alloc(i
37b20 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
37b30 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
37b40 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f  ex_free(sqlite3_
37b50 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f  mutex*);.SQLITE_
37b60 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
37b70 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c  _mutex_enter(sql
37b80 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51  ite3_mutex*);.SQ
37b90 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
37ba0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 73  ite3_mutex_try(s
37bb0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
37bc0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
37bd0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
37be0 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
37bf0 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  x*);../*.** CAPI
37c00 33 52 45 46 3a 20 4d 75 74 65 78 20 4d 65 74 68  3REF: Mutex Meth
37c10 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31 37 31  ods Object {H171
37c20 32 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 20  20} <S20130>.** 
37c30 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
37c40 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
37c50 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
37c60 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c 6f 77   defines the low
37c70 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 0a  -level routines.
37c80 2a 2a 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63  ** used to alloc
37c90 61 74 65 20 61 6e 64 20 75 73 65 20 6d 75 74 65  ate and use mute
37ca0 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55 73 75 61 6c  xes..**.** Usual
37cb0 6c 79 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20  ly, the default 
37cc0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
37cd0 74 69 6f 6e 73 20 70 72 6f 76 69 64 65 64 20 62  tions provided b
37ce0 79 20 53 51 4c 69 74 65 20 61 72 65 0a 2a 2a 20  y SQLite are.** 
37cf0 73 75 66 66 69 63 69 65 6e 74 2c 20 68 6f 77 65  sufficient, howe
37d00 76 65 72 20 74 68 65 20 75 73 65 72 20 68 61 73  ver the user has
37d10 20 74 68 65 20 6f 70 74 69 6f 6e 20 6f 66 20 73   the option of s
37d20 75 62 73 74 69 74 75 74 69 6e 67 20 61 20 63 75  ubstituting a cu
37d30 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  stom.** implemen
37d40 74 61 74 69 6f 6e 20 66 6f 72 20 73 70 65 63 69  tation for speci
37d50 61 6c 69 7a 65 64 20 64 65 70 6c 6f 79 6d 65 6e  alized deploymen
37d60 74 73 20 6f 72 20 73 79 73 74 65 6d 73 20 66 6f  ts or systems fo
37d70 72 20 77 68 69 63 68 20 53 51 4c 69 74 65 0a 2a  r which SQLite.*
37d80 2a 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69  * does not provi
37d90 64 65 20 61 20 73 75 69 74 61 62 6c 65 20 69 6d  de a suitable im
37da0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 6e  plementation. In
37db0 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20   this case, the 
37dc0 75 73 65 72 0a 2a 2a 20 63 72 65 61 74 65 73 20  user.** creates 
37dd0 61 6e 64 20 70 6f 70 75 6c 61 74 65 73 20 61 6e  and populates an
37de0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
37df0 73 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 70  s structure to p
37e00 61 73 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  ass.** to sqlite
37e10 33 5f 63 6f 6e 66 69 67 28 29 20 61 6c 6f 6e 67  3_config() along
37e20 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c 49 54   with the [SQLIT
37e30 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20  E_CONFIG_MUTEX] 
37e40 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41 64 64 69 74  option..** Addit
37e50 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20 69 6e 73 74  ionally, an inst
37e60 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
37e70 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 75 73  ucture can be us
37e80 65 64 20 61 73 20 61 6e 0a 2a 2a 20 6f 75 74 70  ed as an.** outp
37e90 75 74 20 76 61 72 69 61 62 6c 65 20 77 68 65 6e  ut variable when
37ea0 20 71 75 65 72 79 69 6e 67 20 74 68 65 20 73 79   querying the sy
37eb0 73 74 65 6d 20 66 6f 72 20 74 68 65 20 63 75 72  stem for the cur
37ec0 72 65 6e 74 20 6d 75 74 65 78 0a 2a 2a 20 69 6d  rent mutex.** im
37ed0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 75 73  plementation, us
37ee0 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54 45 5f  ing the [SQLITE_
37ef0 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 5d  CONFIG_GETMUTEX]
37f00 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54   option..**.** T
37f10 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 6d 65  he xMutexInit me
37f20 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62 79 20  thod defined by 
37f30 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
37f40 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a 2a 20  s invoked as.** 
37f50 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d 20 69  part of system i
37f60 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 62 79  nitialization by
37f70 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69   the sqlite3_ini
37f80 74 69 61 6c 69 7a 65 28 29 20 66 75 6e 63 74 69  tialize() functi
37f90 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 30 30 31 7d 20  on..** {H17001} 
37fa0 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 72  The xMutexInit r
37fb0 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 62 65 20  outine shall be 
37fc0 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65  called by SQLite
37fd0 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 0a 2a   once for each.*
37fe0 2a 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c  * effective call
37ff0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69   to [sqlite3_ini
38000 74 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a  tialize()]..**.*
38010 2a 20 54 68 65 20 78 4d 75 74 65 78 45 6e 64 20  * The xMutexEnd 
38020 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62  method defined b
38030 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
38040 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a   is invoked as.*
38050 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d  * part of system
38060 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74 68 65   shutdown by the
38070 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
38080 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68  n() function. Th
38090 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e.** implementat
380a0 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 74 68  ion of this meth
380b0 6f 64 20 69 73 20 65 78 70 65 63 74 65 64 20 74  od is expected t
380c0 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6f 75  o release all ou
380d0 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72 65 73  tstanding.** res
380e0 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65 64 20  ources obtained 
380f0 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74  by the mutex met
38100 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  hods implementat
38110 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 0a  ion, especially.
38120 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69 6e 65  ** those obtaine
38130 64 20 62 79 20 74 68 65 20 78 4d 75 74 65 78 49  d by the xMutexI
38140 6e 69 74 20 6d 65 74 68 6f 64 2e 20 7b 48 31 37  nit method. {H17
38150 30 30 33 7d 20 54 68 65 20 78 4d 75 74 65 78 45  003} The xMutexE
38160 6e 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63  nd().** interfac
38170 65 20 73 68 61 6c 6c 20 62 65 20 69 6e 76 6f 6b  e shall be invok
38180 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
38190 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
381a0 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a 2a  3_shutdown()]..*
381b0 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69  *.** The remaini
381c0 6e 67 20 73 65 76 65 6e 20 6d 65 74 68 6f 64 73  ng seven methods
381d0 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 69 73   defined by this
381e0 20 73 74 72 75 63 74 75 72 65 20 28 78 4d 75 74   structure (xMut
381f0 65 78 41 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d 75 74  exAlloc,.** xMut
38200 65 78 46 72 65 65 2c 20 78 4d 75 74 65 78 45 6e  exFree, xMutexEn
38210 74 65 72 2c 20 78 4d 75 74 65 78 54 72 79 2c 20  ter, xMutexTry, 
38220 78 4d 75 74 65 78 4c 65 61 76 65 2c 20 78 4d 75  xMutexLeave, xMu
38230 74 65 78 48 65 6c 64 20 61 6e 64 0a 2a 2a 20 78  texHeld and.** x
38240 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 20 69 6d  MutexNotheld) im
38250 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c  plement the foll
38260 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73  owing interfaces
38270 20 28 72 65 73 70 65 63 74 69 76 65 6c 79 29 3a   (respectively):
38280 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20  .**.** <ul>.**  
38290 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
382a0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 3c  mutex_alloc()] <
382b0 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20  /li>.**   <li>  
382c0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66  [sqlite3_mutex_f
382d0 72 65 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20  ree()] </li>.** 
382e0 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33    <li>  [sqlite3
382f0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 5d 20  _mutex_enter()] 
38300 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20  </li>.**   <li> 
38310 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
38320 74 72 79 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20  try()] </li>.** 
38330 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33    <li>  [sqlite3
38340 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 5d 20  _mutex_leave()] 
38350 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20  </li>.**   <li> 
38360 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
38370 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a  held()] </li>.**
38380 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65     <li>  [sqlite
38390 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
383a0 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c  )] </li>.** </ul
383b0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79  >.**.** The only
383c0 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 74   difference is t
383d0 68 61 74 20 74 68 65 20 70 75 62 6c 69 63 20 73  hat the public s
383e0 71 6c 69 74 65 33 5f 58 58 58 20 66 75 6e 63 74  qlite3_XXX funct
383f0 69 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65 64 0a  ions enumerated.
38400 2a 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e 74 6c  ** above silentl
38410 79 20 69 67 6e 6f 72 65 20 61 6e 79 20 69 6e 76  y ignore any inv
38420 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 70 61  ocations that pa
38430 73 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ss a NULL pointe
38440 72 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20  r instead.** of 
38450 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20 68 61  a valid mutex ha
38460 6e 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c 65 6d  ndle. The implem
38470 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
38480 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64   methods defined
38490 0a 2a 2a 20 62 79 20 74 68 69 73 20 73 74 72 75  .** by this stru
384a0 63 74 75 72 65 20 61 72 65 20 6e 6f 74 20 72 65  cture are not re
384b0 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65  quired to handle
384c0 20 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20   this case, the 
384d0 72 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20 70 61  results.** of pa
384e0 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69  ssing a NULL poi
384f0 6e 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20  nter instead of 
38500 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20 68 61  a valid mutex ha
38510 6e 64 6c 65 20 61 72 65 20 75 6e 64 65 66 69 6e  ndle are undefin
38520 65 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74 20 69  ed.** (i.e. it i
38530 73 20 61 63 63 65 70 74 61 62 6c 65 20 74 6f 20  s acceptable to 
38540 70 72 6f 76 69 64 65 20 61 6e 20 69 6d 70 6c 65  provide an imple
38550 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 73  mentation that s
38560 65 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a 20 69  egfaults if.** i
38570 74 20 69 73 20 70 61 73 73 65 64 20 61 20 4e 55  t is passed a NU
38580 4c 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a 2f 0a  LL pointer)..*/.
38590 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
385a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
385b0 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 75 74  hods sqlite3_mut
385c0 65 78 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75  ex_methods;.stru
385d0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ct sqlite3_mutex
385e0 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74  _methods {.  int
385f0 20 28 2a 78 4d 75 74 65 78 49 6e 69 74 29 28 76   (*xMutexInit)(v
38600 6f 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d  oid);.  int (*xM
38610 75 74 65 78 45 6e 64 29 28 76 6f 69 64 29 3b 0a  utexEnd)(void);.
38620 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
38630 2a 28 2a 78 4d 75 74 65 78 41 6c 6c 6f 63 29 28  *(*xMutexAlloc)(
38640 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  int);.  void (*x
38650 4d 75 74 65 78 46 72 65 65 29 28 73 71 6c 69 74  MutexFree)(sqlit
38660 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76  e3_mutex *);.  v
38670 6f 69 64 20 28 2a 78 4d 75 74 65 78 45 6e 74 65  oid (*xMutexEnte
38680 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  r)(sqlite3_mutex
38690 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75   *);.  int (*xMu
386a0 74 65 78 54 72 79 29 28 73 71 6c 69 74 65 33 5f  texTry)(sqlite3_
386b0 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64  mutex *);.  void
386c0 20 28 2a 78 4d 75 74 65 78 4c 65 61 76 65 29 28   (*xMutexLeave)(
386d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29  sqlite3_mutex *)
386e0 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78  ;.  int (*xMutex
386f0 48 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75  Held)(sqlite3_mu
38700 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  tex *);.  int (*
38710 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 28 73  xMutexNotheld)(s
38720 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b  qlite3_mutex *);
38730 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .};../*.** CAPI3
38740 52 45 46 3a 20 4d 75 74 65 78 20 56 65 72 69 66  REF: Mutex Verif
38750 69 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73  ication Routines
38760 20 7b 48 31 37 30 38 30 7d 20 3c 53 32 30 31 33   {H17080} <S2013
38770 30 3e 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a  0> <S30800>.**.*
38780 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
38790 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73  tex_held() and s
387a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
387b0 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 73 0a  held() routines.
387c0 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  ** are intended 
387d0 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61  for use inside a
387e0 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
387f0 74 73 2e 20 7b 48 31 37 30 38 31 7d 20 54 68 65  ts. {H17081} The
38800 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a 2a 20   SQLite core.** 
38810 6e 65 76 65 72 20 75 73 65 73 20 74 68 65 73 65  never uses these
38820 20 72 6f 75 74 69 6e 65 73 20 65 78 63 65 70 74   routines except
38830 20 69 6e 73 69 64 65 20 61 6e 20 61 73 73 65 72   inside an asser
38840 74 28 29 20 61 6e 64 20 61 70 70 6c 69 63 61 74  t() and applicat
38850 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 64 76 69  ions.** are advi
38860 73 65 64 20 74 6f 20 66 6f 6c 6c 6f 77 20 74 68  sed to follow th
38870 65 20 6c 65 61 64 20 6f 66 20 74 68 65 20 63 6f  e lead of the co
38880 72 65 2e 20 20 7b 48 31 37 30 38 32 7d 20 54 68  re.  {H17082} Th
38890 65 20 63 6f 72 65 20 6f 6e 6c 79 0a 2a 2a 20 70  e core only.** p
388a0 72 6f 76 69 64 65 73 20 69 6d 70 6c 65 6d 65 6e  rovides implemen
388b0 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 73  tations for thes
388c0 65 20 72 6f 75 74 69 6e 65 73 20 77 68 65 6e 20  e routines when 
388d0 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 0a 2a  it is compiled.*
388e0 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  * with the SQLIT
388f0 45 5f 44 45 42 55 47 20 66 6c 61 67 2e 20 20 7b  E_DEBUG flag.  {
38900 41 31 37 30 38 37 7d 20 45 78 74 65 72 6e 61 6c  A17087} External
38910 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
38920 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e  ations.** are on
38930 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 70  ly required to p
38940 72 6f 76 69 64 65 20 74 68 65 73 65 20 72 6f 75  rovide these rou
38950 74 69 6e 65 73 20 69 66 20 53 51 4c 49 54 45 5f  tines if SQLITE_
38960 44 45 42 55 47 20 69 73 0a 2a 2a 20 64 65 66 69  DEBUG is.** defi
38970 6e 65 64 20 61 6e 64 20 69 66 20 4e 44 45 42 55  ned and if NDEBU
38980 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  G is not defined
38990 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 33 7d  ..**.** {H17083}
389a0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
389b0 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 72  should return tr
389c0 75 65 20 69 66 20 74 68 65 20 6d 75 74 65 78 20  ue if the mutex 
389d0 69 6e 20 74 68 65 69 72 20 61 72 67 75 6d 65 6e  in their argumen
389e0 74 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 72 20  t.** is held or 
389f0 6e 6f 74 20 68 65 6c 64 2c 20 72 65 73 70 65 63  not held, respec
38a00 74 69 76 65 6c 79 2c 20 62 79 20 74 68 65 20 63  tively, by the c
38a10 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 2e 0a 2a  alling thread..*
38a20 2a 0a 2a 2a 20 7b 58 31 37 30 38 34 7d 20 54 68  *.** {X17084} Th
38a30 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
38a40 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
38a50 20 74 6f 20 70 72 6f 76 69 64 65 64 20 76 65 72   to provided ver
38a60 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 0a 2a  sions of these.*
38a70 2a 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  * routines that 
38a80 61 63 74 75 61 6c 6c 79 20 77 6f 72 6b 2e 20 49  actually work. I
38a90 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  f the implementa
38aa0 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 70 72  tion does not pr
38ab0 6f 76 69 64 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a  ovide working.**
38ac0 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
38ad0 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 69 74 20  se routines, it 
38ae0 73 68 6f 75 6c 64 20 61 74 20 6c 65 61 73 74 20  should at least 
38af0 70 72 6f 76 69 64 65 20 73 74 75 62 73 20 74 68  provide stubs th
38b00 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74  at always.** ret
38b10 75 72 6e 20 74 72 75 65 20 73 6f 20 74 68 61 74  urn true so that
38b20 20 6f 6e 65 20 64 6f 65 73 20 6e 6f 74 20 67 65   one does not ge
38b30 74 20 73 70 75 72 69 6f 75 73 20 61 73 73 65 72  t spurious asser
38b40 74 69 6f 6e 20 66 61 69 6c 75 72 65 73 2e 0a 2a  tion failures..*
38b50 2a 0a 2a 2a 20 7b 48 31 37 30 38 35 7d 20 49 66  *.** {H17085} If
38b60 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f   the argument to
38b70 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
38b80 65 6c 64 28 29 20 69 73 20 61 20 4e 55 4c 4c 20  eld() is a NULL 
38b90 70 6f 69 6e 74 65 72 20 74 68 65 6e 0a 2a 2a 20  pointer then.** 
38ba0 74 68 65 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  the routine shou
38bb0 6c 64 20 72 65 74 75 72 6e 20 31 2e 20 20 7b 45  ld return 1.  {E
38bc0 4e 44 7d 20 54 68 69 73 20 73 65 65 6d 73 20 63  ND} This seems c
38bd0 6f 75 6e 74 65 72 2d 69 6e 74 75 69 74 69 76 65  ounter-intuitive
38be0 20 73 69 6e 63 65 0a 2a 2a 20 63 6c 65 61 72 6c   since.** clearl
38bf0 79 20 74 68 65 20 6d 75 74 65 78 20 63 61 6e 6e  y the mutex cann
38c00 6f 74 20 62 65 20 68 65 6c 64 20 69 66 20 69 74  ot be held if it
38c10 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e   does not exist.
38c20 20 20 42 75 74 20 74 68 65 0a 2a 2a 20 74 68 65    But the.** the
38c30 20 72 65 61 73 6f 6e 20 74 68 65 20 6d 75 74 65   reason the mute
38c40 78 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  x does not exist
38c50 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65 20   is because the 
38c60 62 75 69 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a 20  build is not.** 
38c70 75 73 69 6e 67 20 6d 75 74 65 78 65 73 2e 20 20  using mutexes.  
38c80 41 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 61  And we do not wa
38c90 6e 74 20 74 68 65 20 61 73 73 65 72 74 28 29 20  nt the assert() 
38ca0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a  containing the.*
38cb0 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  * call to sqlite
38cc0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 74  3_mutex_held() t
38cd0 6f 20 66 61 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e  o fail, so a non
38ce0 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 69 73 0a  -zero return is.
38cf0 2a 2a 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  ** the appropria
38d00 74 65 20 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20  te thing to do. 
38d10 20 7b 48 31 37 30 38 36 7d 20 54 68 65 20 73 71   {H17086} The sq
38d20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
38d30 65 6c 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61  eld().** interfa
38d40 63 65 20 73 68 6f 75 6c 64 20 61 6c 73 6f 20 72  ce should also r
38d50 65 74 75 72 6e 20 31 20 77 68 65 6e 20 67 69 76  eturn 1 when giv
38d60 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  en a NULL pointe
38d70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
38d80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74   int sqlite3_mut
38d90 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 5f  ex_held(sqlite3_
38da0 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f  mutex*);.SQLITE_
38db0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
38dc0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71  mutex_notheld(sq
38dd0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a  lite3_mutex*);..
38de0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
38df0 4d 75 74 65 78 20 54 79 70 65 73 20 7b 48 31 37  Mutex Types {H17
38e00 30 30 31 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a  001} <H17000>.**
38e10 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
38e20 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 20  _mutex_alloc()] 
38e30 69 6e 74 65 72 66 61 63 65 20 74 61 6b 65 73 20  interface takes 
38e40 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  a single argumen
38e50 74 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 6f 6e  t.** which is on
38e60 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
38e70 65 72 20 63 6f 6e 73 74 61 6e 74 73 2e 0a 2a 2a  er constants..**
38e80 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 73  .** The set of s
38e90 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 6d 61  tatic mutexes ma
38ea0 79 20 63 68 61 6e 67 65 20 66 72 6f 6d 20 6f 6e  y change from on
38eb0 65 20 53 51 4c 69 74 65 20 72 65 6c 65 61 73 65  e SQLite release
38ec0 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74 2e   to the.** next.
38ed0 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74    Applications t
38ee0 68 61 74 20 6f 76 65 72 72 69 64 65 20 74 68 65  hat override the
38ef0 20 62 75 69 6c 74 2d 69 6e 20 6d 75 74 65 78 20   built-in mutex 
38f00 6c 6f 67 69 63 20 6d 75 73 74 20 62 65 0a 2a 2a  logic must be.**
38f10 20 70 72 65 70 61 72 65 64 20 74 6f 20 61 63 63   prepared to acc
38f20 6f 6d 6d 6f 64 61 74 65 20 61 64 64 69 74 69 6f  ommodate additio
38f30 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78  nal static mutex
38f40 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  es..*/.#define S
38f50 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
38f60 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23               0.#
38f70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
38f80 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 20 20  TEX_RECURSIVE   
38f90 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53       1.#define S
38fa0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
38fb0 49 43 5f 4d 41 53 54 45 52 20 20 20 20 32 0a 23  IC_MASTER    2.#
38fc0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
38fd0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20  TEX_STATIC_MEM  
38fe0 20 20 20 20 20 33 20 20 2f 2a 20 73 71 6c 69 74       3  /* sqlit
38ff0 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23  e3_malloc() */.#
39000 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
39010 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 20  TEX_STATIC_MEM2 
39020 20 20 20 20 20 34 20 20 2f 2a 20 4e 4f 54 20 55       4  /* NOT U
39030 53 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  SED */.#define S
39040 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
39050 49 43 5f 4f 50 45 4e 20 20 20 20 20 20 34 20 20  IC_OPEN      4  
39060 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f  /* sqlite3BtreeO
39070 70 65 6e 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  pen() */.#define
39080 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
39090 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 35  ATIC_PRNG      5
390a0 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72 61 6e    /* sqlite3_ran
390b0 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  dom() */.#define
390c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
390d0 41 54 49 43 5f 4c 52 55 20 20 20 20 20 20 20 36  ATIC_LRU       6
390e0 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 69    /* lru page li
390f0 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  st */.#define SQ
39100 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
39110 43 5f 4c 52 55 32 20 20 20 20 20 20 37 20 20 2f  C_LRU2      7  /
39120 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20  * lru page list 
39130 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  */../*.** CAPI3R
39140 45 46 3a 20 52 65 74 72 69 65 76 65 20 74 68 65  EF: Retrieve the
39150 20 6d 75 74 65 78 20 66 6f 72 20 61 20 64 61 74   mutex for a dat
39160 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
39170 20 7b 48 31 37 30 30 32 7d 20 3c 48 31 37 30 30   {H17002} <H1700
39180 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  0>.**.** This in
39190 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
391a0 61 20 70 6f 69 6e 74 65 72 20 74 68 65 20 5b 73  a pointer the [s
391b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 6f 62  qlite3_mutex] ob
391c0 6a 65 63 74 20 74 68 61 74 20 0a 2a 2a 20 73 65  ject that .** se
391d0 72 69 61 6c 69 7a 65 73 20 61 63 63 65 73 73 20  rializes access 
391e0 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  to the [database
391f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 67 69 76   connection] giv
39200 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65  en in the argume
39210 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 5b  nt.** when the [
39220 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20  threading mode] 
39230 69 73 20 53 65 72 69 61 6c 69 7a 65 64 2e 0a 2a  is Serialized..*
39240 2a 20 49 66 20 74 68 65 20 5b 74 68 72 65 61 64  * If the [thread
39250 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 69 6e  ing mode] is Sin
39260 67 6c 65 2d 74 68 72 65 61 64 20 6f 72 20 4d 75  gle-thread or Mu
39270 6c 74 69 2d 74 68 72 65 61 64 20 74 68 65 6e 20  lti-thread then 
39280 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
39290 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70  returns a NULL p
392a0 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
392b0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75  E_API sqlite3_mu
392c0 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f  tex *sqlite3_db_
392d0 6d 75 74 65 78 28 73 71 6c 69 74 65 33 2a 29 3b  mutex(sqlite3*);
392e0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
392f0 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43 6f 6e 74  : Low-Level Cont
39300 72 6f 6c 20 4f 66 20 44 61 74 61 62 61 73 65 20  rol Of Database 
39310 46 69 6c 65 73 20 7b 48 31 31 33 30 30 7d 20 3c  Files {H11300} <
39320 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48  S30800>.**.** {H
39330 31 31 33 30 31 7d 20 54 68 65 20 5b 73 71 6c 69  11301} The [sqli
39340 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
39350 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 6d 61  ()] interface ma
39360 6b 65 73 20 61 20 64 69 72 65 63 74 20 63 61 6c  kes a direct cal
39370 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c  l to the.** xFil
39380 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20  eControl method 
39390 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33  for the [sqlite3
393a0 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  _io_methods] obj
393b0 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  ect associated.*
393c0 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  * with a particu
393d0 6c 61 72 20 64 61 74 61 62 61 73 65 20 69 64 65  lar database ide
393e0 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73  ntified by the s
393f0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20  econd argument. 
39400 7b 48 31 31 33 30 32 7d 20 54 68 65 0a 2a 2a 20  {H11302} The.** 
39410 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
39420 62 61 73 65 20 69 73 20 74 68 65 20 6e 61 6d 65  base is the name
39430 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 65   assigned to the
39440 20 64 61 74 61 62 61 73 65 20 62 79 20 74 68 65   database by the
39450 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 6c 61 6e  .** <a href="lan
39460 67 5f 61 74 74 61 63 68 2e 68 74 6d 6c 22 3e 41  g_attach.html">A
39470 54 54 41 43 48 3c 2f 61 3e 20 53 51 4c 20 63 6f  TTACH</a> SQL co
39480 6d 6d 61 6e 64 20 74 68 61 74 20 6f 70 65 6e 65  mmand that opene
39490 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  d the.** databas
394a0 65 2e 20 7b 48 31 31 33 30 33 7d 20 54 6f 20 63  e. {H11303} To c
394b0 6f 6e 74 72 6f 6c 20 74 68 65 20 6d 61 69 6e 20  ontrol the main 
394c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 75  database file, u
394d0 73 65 20 74 68 65 20 6e 61 6d 65 20 22 6d 61 69  se the name "mai
394e0 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e 55 4c 4c 20  n".** or a NULL 
394f0 70 6f 69 6e 74 65 72 2e 20 7b 48 31 31 33 30 34  pointer. {H11304
39500 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20  } The third and 
39510 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
39520 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  s to this routin
39530 65 0a 2a 2a 20 61 72 65 20 70 61 73 73 65 64 20  e.** are passed 
39540 64 69 72 65 63 74 6c 79 20 74 68 72 6f 75 67 68  directly through
39550 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 61   to the second a
39560 6e 64 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  nd third paramet
39570 65 72 73 20 6f 66 0a 2a 2a 20 74 68 65 20 78 46  ers of.** the xF
39580 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f  ileControl metho
39590 64 2e 20 20 7b 48 31 31 33 30 35 7d 20 54 68 65  d.  {H11305} The
395a0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
395b0 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
395c0 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 63 6f  l.** method beco
395d0 6d 65 73 20 74 68 65 20 72 65 74 75 72 6e 20 76  mes the return v
395e0 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75  alue of this rou
395f0 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  tine..**.** {H11
39600 33 30 36 7d 20 49 66 20 74 68 65 20 73 65 63 6f  306} If the seco
39610 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 7a 44  nd parameter (zD
39620 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e 6f 74 20  bName) does not 
39630 6d 61 74 63 68 20 74 68 65 20 6e 61 6d 65 20 6f  match the name o
39640 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e 20 64 61  f any.** open da
39650 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
39660 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69  n SQLITE_ERROR i
39670 73 20 72 65 74 75 72 6e 65 64 2e 20 7b 48 31 31  s returned. {H11
39680 33 30 37 7d 20 54 68 69 73 20 65 72 72 6f 72 0a  307} This error.
39690 2a 2a 20 63 6f 64 65 20 69 73 20 6e 6f 74 20 72  ** code is not r
396a0 65 6d 65 6d 62 65 72 65 64 20 61 6e 64 20 77 69  emembered and wi
396b0 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63 61 6c 6c  ll not be recall
396c0 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 65  ed by [sqlite3_e
396d0 72 72 63 6f 64 65 28 29 5d 0a 2a 2a 20 6f 72 20  rrcode()].** or 
396e0 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  [sqlite3_errmsg(
396f0 29 5d 2e 20 7b 41 31 31 33 30 38 7d 20 54 68 65  )]. {A11308} The
39700 20 75 6e 64 65 72 6c 79 69 6e 67 20 78 46 69 6c   underlying xFil
39710 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20  eControl method 
39720 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 72 65  might.** also re
39730 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
39740 52 2e 20 20 7b 41 31 31 33 30 39 7d 20 54 68 65  R.  {A11309} The
39750 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20  re is no way to 
39760 64 69 73 74 69 6e 67 75 69 73 68 20 62 65 74 77  distinguish betw
39770 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72  een.** an incorr
39780 65 63 74 20 7a 44 62 4e 61 6d 65 20 61 6e 64 20  ect zDbName and 
39790 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  an SQLITE_ERROR 
397a0 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 65 20  return from the 
397b0 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 78 46  underlying.** xF
397c0 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f  ileControl metho
397d0 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53  d. {END}.**.** S
397e0 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45  ee also: [SQLITE
397f0 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
39800 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
39810 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  int sqlite3_file
39820 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  _control(sqlite3
39830 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
39840 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20  DbName, int op, 
39850 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  void*);../*.** C
39860 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67  API3REF: Testing
39870 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 31 34   Interface {H114
39880 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a  00} <S30800>.**.
39890 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74  ** The sqlite3_t
398a0 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e  est_control() in
398b0 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20  terface is used 
398c0 74 6f 20 72 65 61 64 20 6f 75 74 20 69 6e 74 65  to read out inte
398d0 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 65 20 6f 66  rnal.** state of
398e0 20 53 51 4c 69 74 65 20 61 6e 64 20 74 6f 20 69   SQLite and to i
398f0 6e 6a 65 63 74 20 66 61 75 6c 74 73 20 69 6e 74  nject faults int
39900 6f 20 53 51 4c 69 74 65 20 66 6f 72 20 74 65 73  o SQLite for tes
39910 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73  ting.** purposes
39920 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72  .  The first par
39930 61 6d 65 74 65 72 20 69 73 20 61 6e 20 6f 70 65  ameter is an ope
39940 72 61 74 69 6f 6e 20 63 6f 64 65 20 74 68 61 74  ration code that
39950 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 74   determines.** t
39960 68 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61 6e 69  he number, meani
39970 6e 67 2c 20 61 6e 64 20 6f 70 65 72 61 74 69 6f  ng, and operatio
39980 6e 20 6f 66 20 61 6c 6c 20 73 75 62 73 65 71 75  n of all subsequ
39990 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 2e 0a  ent parameters..
399a0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
399b0 66 61 63 65 20 69 73 20 6e 6f 74 20 66 6f 72 20  face is not for 
399c0 75 73 65 20 62 79 20 61 70 70 6c 69 63 61 74 69  use by applicati
399d0 6f 6e 73 2e 20 20 49 74 20 65 78 69 73 74 73 20  ons.  It exists 
399e0 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 20 76 65  solely.** for ve
399f0 72 69 66 79 69 6e 67 20 74 68 65 20 63 6f 72 72  rifying the corr
39a00 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
39a10 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
39a20 61 72 79 2e 20 20 44 65 70 65 6e 64 69 6e 67 0a  ary.  Depending.
39a30 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65 20 53 51  ** on how the SQ
39a40 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69 73 20  Lite library is 
39a50 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69 73 20 69  compiled, this i
39a60 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 6e  nterface might n
39a70 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ot exist..**.** 
39a80 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74  The details of t
39a90 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64  he operation cod
39aa0 65 73 2c 20 74 68 65 69 72 20 6d 65 61 6e 69 6e  es, their meanin
39ab0 67 73 2c 20 74 68 65 20 70 61 72 61 6d 65 74 65  gs, the paramete
39ac0 72 73 0a 2a 2a 20 74 68 65 79 20 74 61 6b 65 2c  rs.** they take,
39ad0 20 61 6e 64 20 77 68 61 74 20 74 68 65 79 20 64   and what they d
39ae0 6f 20 61 72 65 20 61 6c 6c 20 73 75 62 6a 65 63  o are all subjec
39af0 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68  t to change with
39b00 6f 75 74 20 6e 6f 74 69 63 65 2e 0a 2a 2a 20 55  out notice..** U
39b10 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 20 74 68  nlike most of th
39b20 65 20 53 51 4c 69 74 65 20 41 50 49 2c 20 74 68  e SQLite API, th
39b30 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e  is function is n
39b40 6f 74 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  ot guaranteed to
39b50 0a 2a 2a 20 6f 70 65 72 61 74 65 20 63 6f 6e 73  .** operate cons
39b60 69 73 74 65 6e 74 6c 79 20 66 72 6f 6d 20 6f 6e  istently from on
39b70 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65  e release to the
39b80 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45   next..*/.SQLITE
39b90 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
39ba0 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e  _test_control(in
39bb0 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a  t op, ...);../*.
39bc0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 73  ** CAPI3REF: Tes
39bd0 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 4f  ting Interface O
39be0 70 65 72 61 74 69 6f 6e 20 43 6f 64 65 73 20 7b  peration Codes {
39bf0 48 31 31 34 31 30 7d 20 3c 48 31 31 34 30 30 3e  H11410} <H11400>
39c00 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  .**.** These con
39c10 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 76  stants are the v
39c20 61 6c 69 64 20 6f 70 65 72 61 74 69 6f 6e 20 63  alid operation c
39c30 6f 64 65 20 70 61 72 61 6d 65 74 65 72 73 20 75  ode parameters u
39c40 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69  sed.** as the fi
39c50 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
39c60 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f  [sqlite3_test_co
39c70 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  ntrol()]..**.** 
39c80 54 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73  These parameters
39c90 20 61 6e 64 20 74 68 65 69 72 20 6d 65 61 6e 69   and their meani
39ca0 6e 67 73 20 61 72 65 20 73 75 62 6a 65 63 74 20  ngs are subject 
39cb0 74 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 77 69 74  to change.** wit
39cc0 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20 20 54 68  hout notice.  Th
39cd0 65 73 65 20 76 61 6c 75 65 73 20 61 72 65 20 66  ese values are f
39ce0 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f  or testing purpo
39cf0 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70  ses only..** App
39d00 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64  lications should
39d10 20 6e 6f 74 20 75 73 65 20 61 6e 79 20 6f 66 20   not use any of 
39d20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73  these parameters
39d30 20 6f 72 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69   or the.** [sqli
39d40 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
39d50 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  ()] interface..*
39d60 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
39d70 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53  _TESTCTRL_PRNG_S
39d80 41 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20  AVE             
39d90 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c     5.#define SQL
39da0 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e  ITE_TESTCTRL_PRN
39db0 47 5f 52 45 53 54 4f 52 45 20 20 20 20 20 20 20  G_RESTORE       
39dc0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
39dd0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
39de0 50 52 4e 47 5f 52 45 53 45 54 20 20 20 20 20 20  PRNG_RESET      
39df0 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69           7.#defi
39e00 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
39e10 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 20 20  RL_BITVEC_TEST  
39e20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64              8.#d
39e30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
39e40 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 4e 53 54  TCTRL_FAULT_INST
39e50 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 39  ALL            9
39e60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
39e70 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f  TESTCTRL_BENIGN_
39e80 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20 20  MALLOC_HOOKS    
39e90 20 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   10.#define SQLI
39ea0 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44  TE_TESTCTRL_PEND
39eb0 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20  ING_BYTE        
39ec0 20 20 20 20 31 31 0a 0a 2f 2a 0a 2a 2a 20 43 41      11../*.** CA
39ed0 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20 52  PI3REF: SQLite R
39ee0 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b 48  untime Status {H
39ef0 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a  17200} <S60200>.
39f00 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
39f10 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
39f20 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
39f30 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65  retrieve runtime
39f40 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
39f50 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65  ion.** about the
39f60 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66 20   preformance of 
39f70 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74 69  SQLite, and opti
39f80 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74 20  onally to reset 
39f90 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68 77  various.** highw
39fa0 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68 65  ater marks.  The
39fb0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
39fc0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  is an integer co
39fd0 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70  de for.** the sp
39fe0 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65 72  ecific parameter
39ff0 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52 65   to measure.  Re
3a000 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65 72  cognized integer
3a010 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66   codes.** are of
3a020 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49 54   the form [SQLIT
3a030 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
3a040 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53 54  USED | SQLITE_ST
3a050 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54 68  ATUS_...]..** Th
3a060 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
3a070 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  of the parameter
3a080 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 74   is returned int
3a090 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 20  o *pCurrent..** 
3a0a0 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f  The highest reco
3a0b0 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  rded value is re
3a0c0 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67 68  turned in *pHigh
3a0d0 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a 2a  water.  If the.*
3a0e0 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 74  * resetFlag is t
3a0f0 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69  rue, then the hi
3a100 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61 6c  ghest record val
3a110 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74 65  ue is reset afte
3a120 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72  r.** *pHighwater
3a130 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f 6d   is written. Som
3a140 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f 20  e parameters do 
3a150 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20 68  not record the h
3a160 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e  ighest.** value.
3a170 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72 61    For those para
3a180 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e  meters.** nothin
3a190 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  g is written int
3a1a0 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 6e  o *pHighwater an
3a1b0 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67 20  d the resetFlag 
3a1c0 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 4f  is ignored..** O
3a1d0 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 20  ther parameters 
3a1e0 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 20  record only the 
3a1f0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61  highwater mark a
3a200 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 65  nd not the curre
3a210 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f  nt.** value.  Fo
3a220 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 70  r these latter p
3a230 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 6e  arameters nothin
3a240 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  g is written int
3a250 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 0a  o *pCurrent..**.
3a260 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3a270 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
3a280 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  K on success and
3a290 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 5b   a non-zero.** [
3a2a0 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66  error code] on f
3a2b0 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ailure..**.** Th
3a2c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
3a2d0 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73 20  readsafe but is 
3a2e0 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68 69  not atomic.  Thi
3a2f0 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 0a 2a 2a  s routine can.**
3a300 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20 6f 74   called while ot
3a310 68 65 72 20 74 68 72 65 61 64 73 20 61 72 65 20  her threads are 
3a320 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 61 6d 65  running the same
3a330 20 6f 72 20 64 69 66 66 65 72 65 6e 74 20 53 51   or different SQ
3a340 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63  Lite.** interfac
3a350 65 73 2e 20 20 48 6f 77 65 76 65 72 20 74 68 65  es.  However the
3a360 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64   values returned
3a370 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e   in *pCurrent an
3a380 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72  d.** *pHighwater
3a390 20 72 65 66 6c 65 63 74 20 74 68 65 20 73 74 61   reflect the sta
3a3a0 74 75 73 20 6f 66 20 53 51 4c 69 74 65 20 61 74  tus of SQLite at
3a3b0 20 64 69 66 66 65 72 65 6e 74 20 70 6f 69 6e 74   different point
3a3c0 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 61 6e 64  s in time.** and
3a3d0 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
3a3e0 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72  that another thr
3a3f0 65 61 64 20 6d 69 67 68 74 20 63 68 61 6e 67 65  ead might change
3a400 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a   the parameter.*
3a410 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65  * in between the
3a420 20 74 69 6d 65 73 20 77 68 65 6e 20 2a 70 43 75   times when *pCu
3a430 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 69 67 68  rrent and *pHigh
3a440 77 61 74 65 72 20 61 72 65 20 77 72 69 74 74 65  water are writte
3a450 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  n..**.** See als
3a460 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73  o: [sqlite3_db_s
3a470 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 51 4c 49  tatus()].*/.SQLI
3a480 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
3a490 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
3a4a0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e  qlite3_status(in
3a4b0 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72  t op, int *pCurr
3a4c0 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77  ent, int *pHighw
3a4d0 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46  ater, int resetF
3a4e0 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41  lag);.../*.** CA
3a4f0 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50  PI3REF: Status P
3a500 61 72 61 6d 65 74 65 72 73 20 7b 48 31 37 32 35  arameters {H1725
3a510 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 2a 20 45  0} <H17200>.** E
3a520 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
3a530 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20  * These integer 
3a540 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 67 6e  constants design
3a550 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 6e 2d  ate various run-
3a560 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 72 61  time status para
3a570 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 63  meters.** that c
3a580 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  an be returned b
3a590 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  y [sqlite3_statu
3a5a0 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  s()]..**.** <dl>
3a5b0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3a5c0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
3a5d0 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
3a5e0 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  is parameter is 
3a5f0 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d 6f 75  the current amou
3a600 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 68 65  nt of memory che
3a610 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73 69 6e  cked out.** usin
3a620 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  g [sqlite3_mallo
3a630 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64 69 72  c()], either dir
3a640 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 63  ectly or indirec
3a650 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66 69 67  tly.  The.** fig
3a660 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63 61 6c  ure includes cal
3a670 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71 6c 69  ls made to [sqli
3a680 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 79  te3_malloc()] by
3a690 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
3a6a0 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c  .** and internal
3a6b0 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 62 79   memory usage by
3a6c0 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
3a6d0 61 72 79 2e 20 20 53 63 72 61 74 63 68 20 6d 65  ary.  Scratch me
3a6e0 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 6c  mory.** controll
3a6f0 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f  ed by [SQLITE_CO
3a700 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61 6e  NFIG_SCRATCH] an
3a710 64 20 61 75 78 69 6c 69 61 72 79 20 70 61 67 65  d auxiliary page
3a720 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79  -cache.** memory
3a730 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b   controlled by [
3a740 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
3a750 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f 74 20  GECACHE] is not 
3a760 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a 20 74  included in.** t
3a770 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e 20 20  his parameter.  
3a780 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74 75 72  The amount retur
3a790 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d 20 6f  ned is the sum o
3a7a0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
3a7b0 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72 65 70  .** sizes as rep
3a7c0 6f 72 74 65 64 20 62 79 20 74 68 65 20 78 53 69  orted by the xSi
3a7d0 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b 73 71  ze method in [sq
3a7e0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
3a7f0 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  s].</dd>.**.** <
3a800 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
3a810 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64 74  _MALLOC_SIZE</dt
3a820 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61  >.** <dd>This pa
3a830 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20  rameter records 
3a840 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f  the largest memo
3a850 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65  ry allocation re
3a860 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20  quest.** handed 
3a870 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  to [sqlite3_mall
3a880 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  oc()] or [sqlite
3a890 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28 6f 72  3_realloc()] (or
3a8a0 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 65 72 6e   their.** intern
3a8b0 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 73 29 2e  al equivalents).
3a8c0 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65    Only the value
3a8d0 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65   returned in the
3a8e0 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20  .** *pHighwater 
3a8f0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
3a900 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20  lite3_status()] 
3a910 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20  is of interest. 
3a920 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77   .** The value w
3a930 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
3a940 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65  *pCurrent parame
3a950 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ter is undefined
3a960 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
3a970 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  >SQLITE_STATUS_P
3a980 41 47 45 43 41 43 48 45 5f 55 53 45 44 3c 2f 64  AGECACHE_USED</d
3a990 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  t>.** <dd>This p
3a9a0 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73  arameter returns
3a9b0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
3a9c0 61 67 65 73 20 75 73 65 64 20 6f 75 74 20 6f 66  ages used out of
3a9d0 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 63 61 63   the.** [pagecac
3a9e0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
3a9f0 74 6f 72 5d 20 74 68 61 74 20 77 61 73 20 63 6f  tor] that was co
3aa00 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 20 0a  nfigured using .
3aa10 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
3aa20 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 20 20 54  G_PAGECACHE].  T
3aa30 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 65 74 75  he.** value retu
3aa40 72 6e 65 64 20 69 73 20 69 6e 20 70 61 67 65 73  rned is in pages
3aa50 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 73 2e 3c  , not in bytes.<
3aa60 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
3aa70 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
3aa80 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 3c  ECACHE_OVERFLOW<
3aa90 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3aaa0 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72   parameter retur
3aab0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
3aac0 20 62 79 74 65 73 20 6f 66 20 70 61 67 65 20 63   bytes of page c
3aad0 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  ache.** allocati
3aae0 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 20 6e  on which could n
3aaf0 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 65 64  ot be statisfied
3ab00 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f   by the [SQLITE_
3ab10 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
3ab20 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64 20  ].** buffer and 
3ab30 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f 20  where forced to 
3ab40 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71 6c  overflow to [sql
3ab50 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20  ite3_malloc()]. 
3ab60 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64   The.** returned
3ab70 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 73 20   value includes 
3ab80 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74  allocations that
3ab90 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63 61   overflowed beca
3aba0 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 68 65 72  use they.** wher
3abb0 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 74 68 65  e too large (the
3abc0 79 20 77 65 72 65 20 6c 61 72 67 65 72 20 74 68  y were larger th
3abd0 61 6e 20 74 68 65 20 22 73 7a 22 20 70 61 72 61  an the "sz" para
3abe0 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 53 51 4c  meter to.** [SQL
3abf0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
3ac00 41 43 48 45 5d 29 20 61 6e 64 20 61 6c 6c 6f 63  ACHE]) and alloc
3ac10 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72  ations that over
3ac20 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 0a 2a  flowed because.*
3ac30 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 73 20 6c  * no space was l
3ac40 65 66 74 20 69 6e 20 74 68 65 20 70 61 67 65 20  eft in the page 
3ac50 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  cache.</dd>.**.*
3ac60 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
3ac70 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49  TUS_PAGECACHE_SI
3ac80 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ZE</dt>.** <dd>T
3ac90 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
3aca0 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73  cords the larges
3acb0 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  t memory allocat
3acc0 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68  ion request.** h
3acd0 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 65 63 61  anded to [pageca
3ace0 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  che memory alloc
3acf0 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65  ator].  Only the
3ad00 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
3ad10 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68  in the.** *pHigh
3ad20 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20  water parameter 
3ad30 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74  to [sqlite3_stat
3ad40 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65  us()] is of inte
3ad50 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76  rest.  .** The v
3ad60 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74  alue written int
3ad70 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20  o the *pCurrent 
3ad80 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64  parameter is und
3ad90 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a  efined.</dd>.**.
3ada0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
3adb0 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45  ATUS_SCRATCH_USE
3adc0 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  D</dt>.** <dd>Th
3add0 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74  is parameter ret
3ade0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
3adf0 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 75  of allocations u
3ae00 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a  sed out of the.*
3ae10 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  * [scratch memor
3ae20 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e  y allocator] con
3ae30 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a  figured using.**
3ae40 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
3ae50 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20 76  SCRATCH].  The v
3ae60 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
3ae70 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c   in allocations,
3ae80 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 65 73   not.** in bytes
3ae90 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e 67 6c  .  Since a singl
3aea0 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c  e thread may onl
3aeb0 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 61 74  y have one scrat
3aec0 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ch allocation.**
3aed0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20   outstanding at 
3aee0 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 61 6d  time, this param
3aef0 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f 72 74  eter also report
3af00 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
3af10 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 6e 67  threads.** using
3af20 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20   scratch memory 
3af30 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
3af40 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
3af50 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53  >SQLITE_STATUS_S
3af60 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 3c  CRATCH_OVERFLOW<
3af70 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3af80 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72   parameter retur
3af90 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
3afa0 20 62 79 74 65 73 20 6f 66 20 73 63 72 61 74 63   bytes of scratc
3afb0 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  h memory.** allo
3afc0 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75  cation which cou
3afd0 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73  ld not be statis
3afe0 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c  fied by the [SQL
3aff0 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
3b000 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e  CH].** buffer an
3b010 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74  d where forced t
3b020 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73  o overflow to [s
3b030 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
3b040 2e 20 20 54 68 65 20 76 61 6c 75 65 73 0a 2a 2a  .  The values.**
3b050 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64   returned includ
3b060 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65 63 61  e overflows beca
3b070 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65  use the requeste
3b080 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73  d allocation was
3b090 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 72 20 28   too.** larger (
3b0a0 74 68 61 74 20 69 73 2c 20 62 65 63 61 75 73 65  that is, because
3b0b0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 61   the requested a
3b0c0 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6c 61  llocation was la
3b0d0 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a  rger than the.**
3b0e0 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20   "sz" parameter 
3b0f0 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  to [SQLITE_CONFI
3b100 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e 64 20  G_SCRATCH]) and 
3b110 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72 61 74  because no scrat
3b120 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 73 6c 6f  ch buffer.** slo
3b130 74 73 20 77 65 72 65 20 61 76 61 69 6c 61 62 6c  ts were availabl
3b140 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a  e..** </dd>.**.*
3b150 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
3b160 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45  TUS_SCRATCH_SIZE
3b170 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
3b180 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f  s parameter reco
3b190 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20  rds the largest 
3b1a0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
3b1b0 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e  n request.** han
3b1c0 64 65 64 20 74 6f 20 5b 73 63 72 61 74 63 68 20  ded to [scratch 
3b1d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
3b1e0 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c  ].  Only the val
3b1f0 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74  ue returned in t
3b200 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65  he.** *pHighwate
3b210 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  r parameter to [
3b220 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29  sqlite3_status()
3b230 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74  ] is of interest
3b240 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .  .** The value
3b250 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
3b260 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61  e *pCurrent para
3b270 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e  meter is undefin
3b280 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ed.</dd>.**.** <
3b290 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
3b2a0 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c 2f 64  _PARSER_STACK</d
3b2b0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  t>.** <dd>This p
3b2c0 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73  arameter records
3b2d0 20 74 68 65 20 64 65 65 70 65 73 74 20 70 61 72   the deepest par
3b2e0 73 65 72 20 73 74 61 63 6b 2e 20 20 49 74 20 69  ser stack.  It i
3b2f0 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 6e  s only.** meanin
3b300 67 66 75 6c 20 69 66 20 53 51 4c 69 74 65 20 69  gful if SQLite i
3b310 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
3b320 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b  [YYTRACKMAXSTACK
3b330 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a 2a 2a 20  DEPTH].</dd>.** 
3b340 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  </dl>.**.** New 
3b350 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72  status parameter
3b360 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 66  s may be added f
3b370 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d 65  rom time to time
3b380 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
3b390 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
3b3a0 59 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 20  Y_USED          
3b3b0 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
3b3c0 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
3b3d0 45 5f 55 53 45 44 20 20 20 20 20 20 20 31 0a 23  E_USED       1.#
3b3e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3b3f0 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f  ATUS_PAGECACHE_O
3b400 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 65 66  VERFLOW   2.#def
3b410 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3b420 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 20 20  S_SCRATCH_USED  
3b430 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65         3.#define
3b440 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53   SQLITE_STATUS_S
3b450 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20  CRATCH_OVERFLOW 
3b460 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51      4.#define SQ
3b470 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c  LITE_STATUS_MALL
3b480 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20  OC_SIZE         
3b490 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   5.#define SQLIT
3b4a0 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f  E_STATUS_PARSER_
3b4b0 53 54 41 43 4b 20 20 20 20 20 20 20 20 20 36 0a  STACK         6.
3b4c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3b4d0 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
3b4e0 53 49 5a 45 20 20 20 20 20 20 20 37 0a 23 64 65  SIZE       7.#de
3b4f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
3b500 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 20  US_SCRATCH_SIZE 
3b510 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a 0a 2a 2a          8../*.**
3b520 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62   CAPI3REF: Datab
3b530 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 53  ase Connection S
3b540 74 61 74 75 73 20 7b 48 31 37 35 30 30 7d 20 3c  tatus {H17500} <
3b550 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S60200>.** EXPER
3b560 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
3b570 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  is interface is 
3b580 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65  used to retrieve
3b590 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20   runtime status 
3b5a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20  information .** 
3b5b0 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20 5b  about a single [
3b5c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3b5d0 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72 73 74  ion].  The first
3b5e0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
3b5f0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
3b600 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 20 74  nection object t
3b610 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65  o be interrogate
3b620 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61  d.  The second a
3b630 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68  rgument.** is th
3b640 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 69  e parameter to i
3b650 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 43 75 72  nterrogate.  Cur
3b660 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79  rently, the only
3b670 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 0a 2a   allowed value.*
3b680 2a 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64  * for the second
3b690 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 5b 53   parameter is [S
3b6a0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c  QLITE_DBSTATUS_L
3b6b0 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d 2e 0a  OOKASIDE_USED]..
3b6c0 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6f 70  ** Additional op
3b6d0 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c  tions will likel
3b6e0 79 20 61 70 70 65 61 72 20 69 6e 20 66 75 74 75  y appear in futu
3b6f0 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53  re releases of S
3b700 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  QLite..**.** The
3b710 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f   current value o
3b720 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
3b730 70 61 72 61 6d 65 74 65 72 20 69 73 20 77 72 69  parameter is wri
3b740 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 0a  tten into *pCur.
3b750 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67 68 65  ** and the highe
3b760 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73  st instantaneous
3b770 20 76 61 6c 75 65 20 69 73 20 77 72 69 74 74 65   value is writte
3b780 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72 2e 20  n into *pHiwtr. 
3b790 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 73 65 74   If.** the reset
3b7a0 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65  Flg is true, the
3b7b0 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 69 6e  n the highest in
3b7c0 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75  stantaneous valu
3b7d0 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 20 62 61  e is.** reset ba
3b7e0 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 63  ck down to the c
3b7f0 75 72 72 65 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a  urrent value..**
3b800 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
3b810 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d  qlite3_status()]
3b820 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74   and [sqlite3_st
3b830 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2f  mt_status()]..*/
3b840 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
3b850 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
3b860 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73  int sqlite3_db_s
3b870 74 61 74 75 73 28 73 71 6c 69 74 65 33 2a 2c 20  tatus(sqlite3*, 
3b880 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75  int op, int *pCu
3b890 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 72 2c 20  r, int *pHiwtr, 
3b8a0 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a  int resetFlg);..
3b8b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3b8c0 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72  Status Parameter
3b8d0 73 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 63  s for database c
3b8e0 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 31 37 35  onnections {H175
3b8f0 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 2a 2a 20  20} <H17500>.** 
3b900 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
3b910 2a 2a 20 53 74 61 74 75 73 20 76 65 72 62 73 20  ** Status verbs 
3b920 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f  for [sqlite3_db_
3b930 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a  status()]..**.**
3b940 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c   <dl>.** <dt>SQL
3b950 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f  ITE_DBSTATUS_LOO
3b960 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e  KASIDE_USED</dt>
3b970 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72  .** <dd>This par
3b980 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74  ameter returns t
3b990 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f  he number of loo
3b9a0 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c  kaside memory sl
3b9b0 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a  ots currently.**
3b9c0 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64   checked out.</d
3b9d0 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23  d>.** </dl>.*/.#
3b9e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42  define SQLITE_DB
3b9f0 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45  STATUS_LOOKASIDE
3ba00 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f 2a  _USED     0.../*
3ba10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 72  .** CAPI3REF: Pr
3ba20 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
3ba30 20 53 74 61 74 75 73 20 7b 48 31 37 35 35 30 7d   Status {H17550}
3ba40 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50   <S60200>.** EXP
3ba50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
3ba60 45 61 63 68 20 70 72 65 70 61 72 65 64 20 73 74  Each prepared st
3ba70 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69 6e  atement maintain
3ba80 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53 51  s various.** [SQ
3ba90 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f  LITE_STMTSTATUS_
3baa0 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73 5d  SORT | counters]
3bab0 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68   that measure th
3bac0 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74  e number.** of t
3bad0 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66  imes it has perf
3bae0 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f  ormed specific o
3baf0 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65 73  perations.  Thes
3bb00 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a 2a  e counters can.*
3bb10 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f 6e  * be used to mon
3bb20 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72 6d  itor the perform
3bb30 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 69 73  ance characteris
3bb40 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 65 70  tics of the prep
3bb50 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ared.** statemen
3bb60 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  ts.  For example
3bb70 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20  , if the number 
3bb80 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 20 67  of table steps g
3bb90 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a 2a  reatly exceeds.*
3bba0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
3bbb0 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 6f  table searches o
3bbc0 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20 74  r result rows, t
3bbd0 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20 74  hat would tend t
3bbe0 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68  o indicate.** th
3bbf0 61 74 20 74 68 65 20 70 72 65 70 61 72 65 64 20  at the prepared 
3bc00 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73 69  statement is usi
3bc10 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20  ng a full table 
3bc20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 61 6e  scan rather than
3bc30 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 0a  .** an index.  .
3bc40 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
3bc50 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
3bc60 72 65 74 72 69 65 76 65 20 61 6e 64 20 72 65 73  retrieve and res
3bc70 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65  et counter value
3bc80 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72 65  s from.** a [pre
3bc90 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
3bca0 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
3bcb0 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72 65  ument is the pre
3bcc0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a  pared statement.
3bcd0 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  ** object to be 
3bce0 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54  interrogated.  T
3bcf0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
3bd00 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65  nt.** is an inte
3bd10 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 73  ger code for a s
3bd20 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 5f  pecific [SQLITE_
3bd30 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20  STMTSTATUS_SORT 
3bd40 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f  | counter].** to
3bd50 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64   be interrogated
3bd60 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e  . .** The curren
3bd70 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72  t value of the r
3bd80 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72  equested counter
3bd90 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
3bda0 20 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67   If the resetFlg
3bdb0 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
3bdc0 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65  he counter is re
3bdd0 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65  set to zero afte
3bde0 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66  r this.** interf
3bdf0 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  ace call returns
3be00 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
3be10 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  : [sqlite3_statu
3be20 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  s()] and [sqlite
3be30 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a  3_db_status()]..
3be40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
3be50 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
3be60 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  L int sqlite3_st
3be70 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65  mt_status(sqlite
3be80 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c  3_stmt*, int op,
3be90 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a  int resetFlg);..
3bea0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3beb0 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72  Status Parameter
3bec0 73 20 66 6f 72 20 70 72 65 70 61 72 65 64 20 73  s for prepared s
3bed0 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35 37  tatements {H1757
3bee0 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20 45  0} <H17550>.** E
3bef0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
3bf00 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65  * These preproce
3bf10 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69  ssor macros defi
3bf20 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73  ne integer codes
3bf30 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74   that name count
3bf40 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73  er.** values ass
3bf50 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
3bf60 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73   [sqlite3_stmt_s
3bf70 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61  tatus()] interfa
3bf80 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69  ce..** The meani
3bf90 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f  ngs of the vario
3bfa0 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20  us counters are 
3bfb0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
3bfc0 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
3bfd0 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f  LITE_STMTSTATUS_
3bfe0 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f 64  FULLSCAN_STEP</d
3bff0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69  t>.** <dd>This i
3c000 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
3c010 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69 74  times that SQLit
3c020 65 20 68 61 73 20 73 74 65 70 70 65 64 20 66 6f  e has stepped fo
3c030 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74 61  rward in.** a ta
3c040 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20 61  ble as part of a
3c050 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e   full table scan
3c060 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72 73  .  Large numbers
3c070 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74 65   for this counte
3c080 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61 74  r.** may indicat
3c090 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 20  e opportunities 
3c0a0 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  for performance 
3c0b0 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72 6f  improvement thro
3c0c0 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c 20  ugh .** careful 
3c0d0 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c  use of indices.<
3c0e0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
3c0f0 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
3c100 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  _SORT</dt>.** <d
3c110 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e 75  d>This is the nu
3c120 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 65  mber of sort ope
3c130 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76  rations that hav
3c140 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 41  e occurred..** A
3c150 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20   non-zero value 
3c160 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20  in this counter 
3c170 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e 20  may indicate an 
3c180 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a 2a  opportunity to.*
3c190 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 65  * improvement pe
3c1a0 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 67  rformance throug
3c1b0 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66  h careful use of
3c1c0 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a   indices.</dd>.*
3c1d0 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64  *.** </dl>.*/.#d
3c1e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d  efine SQLITE_STM
3c1f0 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e  TSTATUS_FULLSCAN
3c200 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64 65 66  _STEP     1.#def
3c210 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 53  ine SQLITE_STMTS
3c220 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20 20  TATUS_SORT      
3c230 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a          2../*.**
3c240 20 43 41 50 49 33 52 45 46 3a 20 43 75 73 74 6f   CAPI3REF: Custo
3c250 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f 62 6a  m Page Cache Obj
3c260 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ect.** EXPERIMEN
3c270 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  TAL.**.** The sq
3c280 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74 79 70  lite3_pcache typ
3c290 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20 49 74  e is opaque.  It
3c2a0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
3c2b0 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 67 61  by.** the plugga
3c2c0 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65  ble module.  The
3c2d0 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68 61 73   SQLite core has
3c2e0 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66   no knowledge of
3c2f0 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f 72 20  .** its size or 
3c300 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75  internal structu
3c310 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64 65 61  re and never dea
3c320 6c 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73  ls with the.** s
3c330 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 6f 62  qlite3_pcache ob
3c340 6a 65 63 74 20 65 78 63 65 70 74 20 62 79 20 68  ject except by h
3c350 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 73 69  olding and passi
3c360 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 74  ng pointers.** t
3c370 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a  o the object..**
3c380 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 65 33  .** See [sqlite3
3c390 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d  _pcache_methods]
3c3a0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
3c3b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
3c3c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
3c3d0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 73 71  qlite3_pcache sq
3c3e0 6c 69 74 65 33 5f 70 63 61 63 68 65 3b 0a 0a 2f  lite3_pcache;../
3c3f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41  *.** CAPI3REF: A
3c400 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 69 6e  pplication Defin
3c410 65 64 20 50 61 67 65 20 43 61 63 68 65 2e 0a 2a  ed Page Cache..*
3c420 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3c430 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  *.** The [sqlite
3c440 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54  3_config]([SQLIT
3c450 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d  E_CONFIG_PCACHE]
3c460 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63 65  , ...) interface
3c470 20 63 61 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72   can.** register
3c480 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20   an alternative 
3c490 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65  page cache imple
3c4a0 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20 70 61 73  mentation by pas
3c4b0 73 69 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69  sing in an .** i
3c4c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73  nstance of the s
3c4d0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
3c4e0 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 2e  thods structure.
3c4f0 20 54 68 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66   The majority of
3c500 20 74 68 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65   the .** heap me
3c510 6d 6f 72 79 20 75 73 65 64 20 62 79 20 73 71 6c  mory used by sql
3c520 69 74 65 20 69 73 20 75 73 65 64 20 62 79 20 74  ite is used by t
3c530 68 65 20 70 61 67 65 20 63 61 63 68 65 20 74 6f  he page cache to
3c540 20 63 61 63 68 65 20 64 61 74 61 20 72 65 61 64   cache data read
3c550 20 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65   .** from, or re
3c560 61 64 79 20 74 6f 20 62 65 20 77 72 69 74 74 65  ady to be writte
3c570 6e 20 74 6f 2c 20 74 68 65 20 64 61 74 61 62 61  n to, the databa
3c580 73 65 20 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c  se file. By impl
3c590 65 6d 65 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63  ementing a .** c
3c5a0 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65  ustom page cache
3c5b0 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 2c   using this API,
3c5c0 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20   an application 
3c5d0 63 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65  can control more
3c5e0 20 0a 2a 2a 20 70 72 65 63 69 73 65 6c 79 20 74   .** precisely t
3c5f0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  he amount of mem
3c600 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 62 79 20  ory consumed by 
3c610 73 71 6c 69 74 65 2c 20 74 68 65 20 77 61 79 20  sqlite, the way 
3c620 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20 73 61 69  in which .** sai
3c630 64 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f  d memory is allo
3c640 63 61 74 65 64 20 61 6e 64 20 72 65 6c 65 61 73  cated and releas
3c650 65 64 2c 20 61 6e 64 20 74 68 65 20 70 6f 6c 69  ed, and the poli
3c660 63 69 65 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a  cies used to .**
3c670 20 64 65 74 65 72 6d 69 6e 65 20 65 78 61 63 74   determine exact
3c680 6c 79 20 77 68 69 63 68 20 70 61 72 74 73 20 6f  ly which parts o
3c690 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  f a database fil
3c6a0 65 20 61 72 65 20 63 61 63 68 65 64 20 61 6e 64  e are cached and
3c6b0 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e   for .** how lon
3c6c0 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e  g..**.** The con
3c6d0 74 65 6e 74 73 20 6f 66 20 74 68 65 20 73 74 72  tents of the str
3c6e0 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 69 65  ucture are copie
3c6f0 64 20 74 6f 20 61 6e 20 69 6e 74 65 72 6e 61 6c  d to an internal
3c700 20 62 75 66 66 65 72 20 62 79 20 73 71 6c 69 74   buffer by sqlit
3c710 65 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20  e.** within the 
3c720 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
3c730 5f 63 6f 6e 66 69 67 5d 2e 0a 2a 2a 0a 2a 2a 20  _config]..**.** 
3c740 54 68 65 20 78 49 6e 69 74 28 29 20 6d 65 74 68  The xInit() meth
3c750 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63  od is called onc
3c760 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c 20  e for each call 
3c770 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74  to [sqlite3_init
3c780 69 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 28 75 73  ialize()].** (us
3c790 75 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65 20  ually only once 
3c7a0 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74  during the lifet
3c7b0 69 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63 65  ime of the proce
3c7c0 73 73 29 2e 20 49 74 20 69 73 20 70 61 73 73 65  ss). It is passe
3c7d0 64 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74  d.** a copy of t
3c7e0 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
3c7f0 65 5f 6d 65 74 68 6f 64 73 2e 70 41 72 67 20 76  e_methods.pArg v
3c800 61 6c 75 65 2e 20 49 74 20 63 61 6e 20 62 65 20  alue. It can be 
3c810 75 73 65 64 20 74 6f 20 73 65 74 0a 2a 2a 20 75  used to set.** u
3c820 70 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75  p global structu
3c830 72 65 73 20 61 6e 64 20 6d 75 74 65 78 65 73 20  res and mutexes 
3c840 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20  required by the 
3c850 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68  custom page cach
3c860 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  e .** implementa
3c870 74 69 6f 6e 2e 20 54 68 65 20 78 53 68 75 74 64  tion. The xShutd
3c880 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 20  own() method is 
3c890 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
3c8a0 69 6e 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  in .** [sqlite3_
3c8b0 73 68 75 74 64 6f 77 6e 28 29 5d 2c 20 69 66 20  shutdown()], if 
3c8c0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
3c8d0 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 49  invokes this API
3c8e0 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64  . It can be used
3c8f0 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20 75 70 20  .** to clean up 
3c900 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  any outstanding 
3c910 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 65  resources before
3c920 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f 77   process shutdow
3c930 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  n, if required..
3c940 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74  **.** The xCreat
3c950 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73  e() method is us
3c960 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20  ed to construct 
3c970 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e 73 74  a new cache inst
3c980 61 6e 63 65 2e 20 54 68 65 0a 2a 2a 20 66 69 72  ance. The.** fir
3c990 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20 73 7a  st parameter, sz
3c9a0 50 61 67 65 2c 20 69 73 20 74 68 65 20 73 69 7a  Page, is the siz
3c9b0 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
3c9c0 65 20 70 61 67 65 73 20 74 68 61 74 20 6d 75 73  e pages that mus
3c9d0 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f 63 61 74 65  t.** be allocate
3c9e0 64 20 62 79 20 74 68 65 20 63 61 63 68 65 2e 20  d by the cache. 
3c9f0 73 7a 50 61 67 65 20 77 69 6c 6c 20 6e 6f 74 20  szPage will not 
3ca00 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77  be a power of tw
3ca10 6f 2e 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64  o. The.** second
3ca20 20 61 72 67 75 6d 65 6e 74 2c 20 62 50 75 72 67   argument, bPurg
3ca30 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65 20 69  eable, is true i
3ca40 66 20 74 68 65 20 63 61 63 68 65 20 62 65 69 6e  f the cache bein
3ca50 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c 0a 2a  g created will.*
3ca60 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63 61 63  * be used to cac
3ca70 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
3ca80 73 20 72 65 61 64 20 66 72 6f 6d 20 61 20 66 69  s read from a fi
3ca90 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73  le stored on dis
3caa0 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69  k, or.** false i
3cab0 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72  f it is used for
3cac0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
3cad0 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68  tabase. The cach
3cae0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3caf0 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76  .** does not hav
3cb00 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67  e to do anything
3cb10 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 6f   special based o
3cb20 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 62  n the value of b
3cb30 50 75 72 67 65 61 62 6c 65 2c 0a 2a 2a 20 69 74  Purgeable,.** it
3cb40 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69 73   is purely advis
3cb50 6f 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ory. .**.** The 
3cb60 78 43 61 63 68 65 73 69 7a 65 28 29 20 6d 65 74  xCachesize() met
3cb70 68 6f 64 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  hod may be calle
3cb80 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 62 79  d at any time by
3cb90 20 53 51 4c 69 74 65 20 74 6f 20 73 65 74 20 74   SQLite to set t
3cba0 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65 64 20  he.** suggested 
3cbb0 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 2d 73 69  maximum cache-si
3cbc0 7a 65 20 28 6e 75 6d 62 65 72 20 6f 66 20 70 61  ze (number of pa
3cbd0 67 65 73 20 73 74 6f 72 65 64 20 62 79 29 20 74  ges stored by) t
3cbe0 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 6e 73 74  he cache.** inst
3cbf0 61 6e 63 65 20 70 61 73 73 65 64 20 61 73 20 74  ance passed as t
3cc00 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
3cc10 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 76  t. This is the v
3cc20 61 6c 75 65 20 63 6f 6e 66 69 67 75 72 65 64 20  alue configured 
3cc30 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c  using.** the SQL
3cc40 69 74 65 20 22 5b 50 52 41 47 4d 41 20 63 61 63  ite "[PRAGMA cac
3cc50 68 65 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e  he_size]" comman
3cc60 64 2e 20 41 73 20 77 69 74 68 20 74 68 65 20 62  d. As with the b
3cc70 50 75 72 67 65 61 62 6c 65 20 70 61 72 61 6d 65  Purgeable parame
3cc80 74 65 72 2c 0a 2a 2a 20 74 68 65 20 69 6d 70 6c  ter,.** the impl
3cc90 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f  ementation is no
3cca0 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 64 6f  t required to do
3ccb0 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63 69 61   anything specia
3ccc0 6c 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 76  l with this.** v
3ccd0 61 6c 75 65 2c 20 69 74 20 69 73 20 61 64 76 69  alue, it is advi
3cce0 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a  sory only..**.**
3ccf0 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74 28   The xPagecount(
3cd00 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  ) method should 
3cd10 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
3cd20 72 20 6f 66 20 70 61 67 65 73 20 63 75 72 72 65  r of pages curre
3cd30 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20 69  ntly.** stored i
3cd40 6e 20 74 68 65 20 63 61 63 68 65 20 73 75 70 70  n the cache supp
3cd50 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d  lied as an argum
3cd60 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  ent..** .** The 
3cd70 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64 20  xFetch() method 
3cd80 69 73 20 75 73 65 64 20 74 6f 20 66 65 74 63 68  is used to fetch
3cd90 20 61 20 70 61 67 65 20 61 6e 64 20 72 65 74 75   a page and retu
3cda0 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
3cdb0 69 74 2e 20 0a 2a 2a 20 41 20 27 70 61 67 65 27  it. .** A 'page'
3cdc0 2c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 65 78  , in this contex
3cdd0 74 2c 20 69 73 20 61 20 62 75 66 66 65 72 20 6f  t, is a buffer o
3cde0 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 20 61  f szPage bytes a
3cdf0 6c 69 67 6e 65 64 20 61 74 20 61 6e 0a 2a 2a 20  ligned at an.** 
3ce00 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e  8-byte boundary.
3ce10 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20   The page to be 
3ce20 66 65 74 63 68 65 64 20 69 73 20 64 65 74 65 72  fetched is deter
3ce30 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b 65 79  mined by the key
3ce40 2e 20 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d  . The.** mimimum
3ce50 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 31 2e   key value is 1.
3ce60 20 41 66 74 65 72 20 69 74 20 68 61 73 20 62 65   After it has be
3ce70 65 6e 20 72 65 74 72 69 65 76 65 64 20 75 73 69  en retrieved usi
3ce80 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65 20 70  ng xFetch, the p
3ce90 61 67 65 20 0a 2a 2a 20 69 73 20 63 6f 6e 73 69  age .** is consi
3cea0 64 65 72 65 64 20 74 6f 20 62 65 20 70 69 6e 6e  dered to be pinn
3ceb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
3cec0 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20   requested page 
3ced0 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
3cee0 65 20 70 61 67 65 20 63 61 63 68 65 2c 20 74 68  e page cache, th
3cef0 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a  en a pointer to.
3cf00 2a 2a 20 74 68 65 20 63 61 63 68 65 64 20 62 75  ** the cached bu
3cf10 66 66 65 72 20 73 68 6f 75 6c 64 20 62 65 20 72  ffer should be r
3cf20 65 74 75 72 6e 65 64 20 77 69 74 68 20 69 74 73  eturned with its
3cf30 20 63 6f 6e 74 65 6e 74 73 20 69 6e 74 61 63 74   contents intact
3cf40 2e 20 49 66 20 74 68 65 0a 2a 2a 20 70 61 67 65  . If the.** page
3cf50 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
3cf60 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68  in the cache, th
3cf70 65 6e 20 74 68 65 20 65 78 70 65 63 74 65 64 20  en the expected 
3cf80 62 65 68 61 76 69 6f 75 72 20 6f 66 20 74 68 65  behaviour of the
3cf90 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 64 65 74  .** cache is det
3cfa0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 76  ermined by the v
3cfb0 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61  alue of the crea
3cfc0 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72  teFlag parameter
3cfd0 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 78 46   passed.** to xF
3cfe0 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e 67 20  etch, according 
3cff0 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
3d000 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c 74   table:.**.** <t
3d010 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 77 69  able border=1 wi
3d020 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63 65  dth=85% align=ce
3d030 6e 74 65 72 3e 0a 2a 2a 20 20 20 3c 74 72 3e 3c  nter>.**   <tr><
3d040 74 68 3e 63 72 65 61 74 65 46 6c 61 67 3c 74 68  th>createFlag<th
3d050 3e 45 78 70 65 63 74 65 64 20 42 65 68 61 76 69  >Expected Behavi
3d060 6f 75 72 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74 64  our.**   <tr><td
3d070 3e 30 3c 74 64 3e 4e 55 4c 4c 20 73 68 6f 75 6c  >0<td>NULL shoul
3d080 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 4e  d be returned. N
3d090 6f 20 6e 65 77 20 63 61 63 68 65 20 65 6e 74 72  o new cache entr
3d0a0 79 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a  y is created..**
3d0b0 20 20 20 3c 74 72 3e 3c 74 64 3e 31 3c 74 64 3e     <tr><td>1<td>
3d0c0 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73  If createFlag is
3d0d0 20 73 65 74 20 74 6f 20 31 2c 20 74 68 69 73 20   set to 1, this 
3d0e0 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 0a  indicates that .
3d0f0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3d100 20 20 53 51 4c 69 74 65 20 69 73 20 68 6f 6c 64    SQLite is hold
3d110 69 6e 67 20 70 69 6e 6e 65 64 20 70 61 67 65 73  ing pinned pages
3d120 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 6e 70   that can be unp
3d130 69 6e 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  inned.**        
3d140 20 20 20 20 20 20 20 20 62 79 20 77 72 69 74 69          by writi
3d150 6e 67 20 74 68 65 69 72 20 63 6f 6e 74 65 6e 74  ng their content
3d160 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  s to the databas
3d170 65 20 66 69 6c 65 20 28 61 0a 2a 2a 20 20 20 20  e file (a.**    
3d180 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 61              rela
3d190 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69 76 65  tively expensive
3d1a0 20 6f 70 65 72 61 74 69 6f 6e 29 2e 20 49 6e 20   operation). In 
3d1b0 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e 20 74  this situation t
3d1c0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
3d1d0 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 6c 65       cache imple
3d1e0 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 74 77  mentation has tw
3d1f0 6f 20 63 68 6f 69 63 65 73 3a 20 69 74 20 63 61  o choices: it ca
3d200 6e 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2c 0a 2a  n return NULL,.*
3d210 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3d220 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53   in which case S
3d230 51 4c 69 74 65 20 77 69 6c 6c 20 61 74 74 65 6d  QLite will attem
3d240 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 20  pt to unpin one 
3d250 6f 72 20 6d 6f 72 65 20 0a 2a 2a 20 20 20 20 20  or more .**     
3d260 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 73             pages
3d270 20 62 65 66 6f 72 65 20 72 65 2d 72 65 71 75 65   before re-reque
3d280 73 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 70  sting the same p
3d290 61 67 65 2c 20 6f 72 20 69 74 20 63 61 6e 0a 2a  age, or it can.*
3d2a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3d2b0 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
3d2c0 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20  page and return 
3d2d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
3d2e0 20 49 66 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20   If a new.**    
3d2f0 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65              page
3d300 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74   is allocated, t
3d310 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 73 69  hen the first si
3d320 7a 65 6f 66 28 76 6f 69 64 2a 29 20 62 79 74 65  zeof(void*) byte
3d330 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  s of.**         
3d340 20 20 20 20 20 20 20 69 74 20 28 61 74 20 6c 65         it (at le
3d350 61 73 74 29 20 6d 75 73 74 20 62 65 20 7a 65 72  ast) must be zer
3d360 6f 65 64 20 62 65 66 6f 72 65 20 69 74 20 69 73  oed before it is
3d370 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 20 20   returned..**   
3d380 3c 74 72 3e 3c 74 64 3e 32 3c 74 64 3e 49 66 20  <tr><td>2<td>If 
3d390 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 73 65  createFlag is se
3d3a0 74 20 74 6f 20 32 2c 20 74 68 65 6e 20 53 51 4c  t to 2, then SQL
3d3b0 69 74 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69  ite is not holdi
3d3c0 6e 67 20 61 6e 79 0a 2a 2a 20 20 20 20 20 20 20  ng any.**       
3d3d0 20 20 20 20 20 20 20 20 20 70 69 6e 6e 65 64 20           pinned 
3d3e0 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64  pages associated
3d3f0 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66   with the specif
3d400 69 63 20 63 61 63 68 65 20 70 61 73 73 65 64 0a  ic cache passed.
3d410 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3d420 20 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61    as the first a
3d430 72 67 75 6d 65 6e 74 20 74 6f 20 78 46 65 74 63  rgument to xFetc
3d440 68 28 29 20 74 68 61 74 20 63 61 6e 20 62 65 20  h() that can be 
3d450 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65 0a 2a 2a  unpinned. The.**
3d460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d470 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  cache implementa
3d480 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 74 74 65  tion should atte
3d490 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  mpt to allocate 
3d4a0 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20  a new.**        
3d4b0 20 20 20 20 20 20 20 20 63 61 63 68 65 20 65 6e          cache en
3d4c0 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 61  try and return a
3d4d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
3d4e0 41 67 61 69 6e 2c 20 74 68 65 20 66 69 72 73 74  Again, the first
3d4f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3d500 20 20 20 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29     sizeof(void*)
3d510 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61   bytes of the pa
3d520 67 65 20 73 68 6f 75 6c 64 20 62 65 20 7a 65 72  ge should be zer
3d530 6f 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20 20  oed before .**  
3d540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 74                it
3d550 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
3d560 20 74 68 65 20 78 46 65 74 63 68 28 29 20 6d 65   the xFetch() me
3d570 74 68 6f 64 20 72 65 74 75 72 6e 73 20 4e 55 4c  thod returns NUL
3d580 4c 20 77 68 65 6e 20 0a 2a 2a 20 20 20 20 20 20  L when .**      
3d590 20 20 20 20 20 20 20 20 20 20 63 72 65 61 74 65            create
3d5a0 46 6c 61 67 3d 3d 32 2c 20 53 51 4c 69 74 65 20  Flag==2, SQLite 
3d5b0 61 73 73 75 6d 65 73 20 74 68 61 74 20 61 20 6d  assumes that a m
3d5c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
3d5d0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
3d5e0 20 20 20 20 66 61 69 6c 65 64 20 61 6e 64 20 72      failed and r
3d5f0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4e 4f  eturns SQLITE_NO
3d600 4d 45 4d 20 74 6f 20 74 68 65 20 75 73 65 72 2e  MEM to the user.
3d610 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 0a  .** </table>.**.
3d620 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20 63  ** xUnpin() is c
3d630 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20  alled by SQLite 
3d640 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74  with a pointer t
3d650 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70 69  o a currently pi
3d660 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73 20  nned page.** as 
3d670 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  its second argum
3d680 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69 72  ent. If the thir
3d690 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69 73  d parameter, dis
3d6a0 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72  card, is non-zer
3d6b0 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70  o,.** then the p
3d6c0 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65 76  age should be ev
3d6d0 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 63  icted from the c
3d6e0 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 61  ache. In this ca
3d6f0 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61 73  se SQLite .** as
3d700 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 6e  sumes that the n
3d710 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61 67  ext time the pag
3d720 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20 66  e is retrieved f
3d730 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75 73  rom the cache us
3d740 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74 63  ing.** the xFetc
3d750 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20 77  h() method, it w
3d760 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20 49  ill be zeroed. I
3d770 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70 61  f the discard pa
3d780 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a 65  rameter is.** ze
3d790 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67  ro, then the pag
3d7a0 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  e is considered 
3d7b0 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20  to be unpinned. 
3d7c0 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d  The cache implem
3d7d0 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20  entation.** may 
3d7e0 63 68 6f 6f 73 65 20 74 6f 20 72 65 63 6c 61 69  choose to reclai
3d7f0 6d 20 28 66 72 65 65 20 6f 72 20 72 65 63 79 63  m (free or recyc
3d800 6c 65 29 20 75 6e 70 69 6e 6e 65 64 20 70 61 67  le) unpinned pag
3d810 65 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a  es at any time..
3d820 2a 2a 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65  ** SQLite assume
3d830 73 20 74 68 61 74 20 6e 65 78 74 20 74 69 6d 65  s that next time
3d840 20 74 68 65 20 70 61 67 65 20 69 73 20 72 65 74   the page is ret
3d850 72 69 65 76 65 64 20 66 72 6f 6d 20 74 68 65 20  rieved from the 
3d860 63 61 63 68 65 0a 2a 2a 20 69 74 20 77 69 6c 6c  cache.** it will
3d870 20 65 69 74 68 65 72 20 62 65 20 7a 65 72 6f 65   either be zeroe
3d880 64 2c 20 6f 72 20 63 6f 6e 74 61 69 6e 20 74 68  d, or contain th
3d890 65 20 73 61 6d 65 20 64 61 74 61 20 74 68 61 74  e same data that
3d8a0 20 69 74 20 64 69 64 20 77 68 65 6e 20 69 74 0a   it did when it.
3d8b0 2a 2a 20 77 61 73 20 75 6e 70 69 6e 6e 65 64 2e  ** was unpinned.
3d8c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65  .**.** The cache
3d8d0 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
3d8e0 20 74 6f 20 70 65 72 66 6f 72 6d 20 61 6e 79 20   to perform any 
3d8f0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69  reference counti
3d900 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0a 2a 2a  ng. A single .**
3d910 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70 69 6e 28   call to xUnpin(
3d920 29 20 75 6e 70 69 6e 73 20 74 68 65 20 70 61 67  ) unpins the pag
3d930 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  e regardless of 
3d940 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72  the number of pr
3d950 69 6f 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f  ior calls .** to
3d960 20 78 46 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a   xFetch()..**.**
3d970 20 54 68 65 20 78 52 65 6b 65 79 28 29 20 6d 65   The xRekey() me
3d980 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20  thod is used to 
3d990 63 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 76  change the key v
3d9a0 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 20  alue associated 
3d9b0 77 69 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65  with the.** page
3d9c0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
3d9d0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 66  econd argument f
3d9e0 72 6f 6d 20 6f 6c 64 4b 65 79 20 74 6f 20 6e 65  rom oldKey to ne
3d9f0 77 4b 65 79 2e 20 49 66 20 74 68 65 20 63 61 63  wKey. If the cac
3da00 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  he.** previously
3da10 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74   contains an ent
3da20 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
3da30 74 68 20 6e 65 77 4b 65 79 2c 20 69 74 20 73 68  th newKey, it sh
3da40 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 69 73 63 61  ould be.** disca
3da50 72 64 65 64 2e 20 41 6e 79 20 70 72 69 6f 72 20  rded. Any prior 
3da60 63 61 63 68 65 20 65 6e 74 72 79 20 61 73 73 6f  cache entry asso
3da70 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b  ciated with newK
3da80 65 79 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  ey is guaranteed
3da90 20 6e 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 70 69   not.** to be pi
3daa0 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  nned..**.** When
3dab0 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68   SQLite calls th
3dac0 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 6d 65  e xTruncate() me
3dad0 74 68 6f 64 2c 20 74 68 65 20 63 61 63 68 65 20  thod, the cache 
3dae0 6d 75 73 74 20 64 69 73 63 61 72 64 20 61 6c 6c  must discard all
3daf0 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 63 61 63  .** existing cac
3db00 68 65 20 65 6e 74 72 69 65 73 20 77 69 74 68 20  he entries with 
3db10 70 61 67 65 20 6e 75 6d 62 65 72 73 20 28 6b 65  page numbers (ke
3db20 79 73 29 20 67 72 65 61 74 65 72 20 74 68 61 6e  ys) greater than
3db30 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74 6f 20   or equal.** to 
3db40 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
3db50 20 69 4c 69 6d 69 74 20 70 61 72 61 6d 65 74 65   iLimit paramete
3db60 72 20 70 61 73 73 65 64 20 74 6f 20 78 54 72 75  r passed to xTru
3db70 6e 63 61 74 65 28 29 2e 20 49 66 20 61 6e 79 0a  ncate(). If any.
3db80 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61 67 65  ** of these page
3db90 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20 74 68  s are pinned, th
3dba0 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c  ey are implicitl
3dbb0 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e  y unpinned, mean
3dbc0 69 6e 67 20 74 68 61 74 0a 2a 2a 20 74 68 65 79  ing that.** they
3dbd0 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20 64   can be safely d
3dbe0 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20  iscarded..**.** 
3dbf0 54 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d  The xDestroy() m
3dc00 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f  ethod is used to
3dc10 20 64 65 6c 65 74 65 20 61 20 63 61 63 68 65 20   delete a cache 
3dc20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 78 43 72  allocated by xCr
3dc30 65 61 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72  eate()..** All r
3dc40 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61  esources associa
3dc50 74 65 64 20 77 69 74 68 20 74 68 65 20 73 70 65  ted with the spe
3dc60 63 69 66 69 65 64 20 63 61 63 68 65 20 73 68 6f  cified cache sho
3dc70 75 6c 64 20 62 65 20 66 72 65 65 64 2e 20 41 66  uld be freed. Af
3dc80 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74  ter.** calling t
3dc90 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65  he xDestroy() me
3dca0 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63 6f 6e  thod, SQLite con
3dcb0 73 69 64 65 72 73 20 74 68 65 20 5b 73 71 6c 69  siders the [sqli
3dcc0 74 65 33 5f 70 63 61 63 68 65 2a 5d 0a 2a 2a 20  te3_pcache*].** 
3dcd0 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64 2c 20  handle invalid, 
3dce0 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75 73 65  and will not use
3dcf0 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f 74 68   it with any oth
3dd00 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  er sqlite3_pcach
3dd10 65 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 66 75 6e  e_methods.** fun
3dd20 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64  ctions..*/.typed
3dd30 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
3dd40 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73  3_pcache_methods
3dd50 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
3dd60 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20  methods;.struct 
3dd70 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
3dd80 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20  ethods {.  void 
3dd90 2a 70 41 72 67 3b 0a 20 20 69 6e 74 20 28 2a 78  *pArg;.  int (*x
3dda0 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 0a 20 20  Init)(void*);.  
3ddb0 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e  void (*xShutdown
3ddc0 29 28 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69  )(void*);.  sqli
3ddd0 74 65 33 5f 70 63 61 63 68 65 20 2a 28 2a 78 43  te3_pcache *(*xC
3dde0 72 65 61 74 65 29 28 69 6e 74 20 73 7a 50 61 67  reate)(int szPag
3ddf0 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c  e, int bPurgeabl
3de00 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 43 61  e);.  void (*xCa
3de10 63 68 65 73 69 7a 65 29 28 73 71 6c 69 74 65 33  chesize)(sqlite3
3de20 5f 70 63 61 63 68 65 2a 2c 20 69 6e 74 20 6e 43  _pcache*, int nC
3de30 61 63 68 65 73 69 7a 65 29 3b 0a 20 20 69 6e 74  achesize);.  int
3de40 20 28 2a 78 50 61 67 65 63 6f 75 6e 74 29 28 73   (*xPagecount)(s
3de50 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b  qlite3_pcache*);
3de60 0a 20 20 76 6f 69 64 20 2a 28 2a 78 46 65 74 63  .  void *(*xFetc
3de70 68 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  h)(sqlite3_pcach
3de80 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6b 65 79  e*, unsigned key
3de90 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67  , int createFlag
3dea0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 70  );.  void (*xUnp
3deb0 69 6e 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63  in)(sqlite3_pcac
3dec0 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20  he*, void*, int 
3ded0 64 69 73 63 61 72 64 29 3b 0a 20 20 76 6f 69 64  discard);.  void
3dee0 20 28 2a 78 52 65 6b 65 79 29 28 73 71 6c 69 74   (*xRekey)(sqlit
3def0 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 64  e3_pcache*, void
3df00 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b  *, unsigned oldK
3df10 65 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e 65 77  ey, unsigned new
3df20 4b 65 79 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  Key);.  void (*x
3df30 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65  Truncate)(sqlite
3df40 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67  3_pcache*, unsig
3df50 6e 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20 20 76  ned iLimit);.  v
3df60 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28  oid (*xDestroy)(
3df70 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29  sqlite3_pcache*)
3df80 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ;.};../*.** CAPI
3df90 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63  3REF: Online Bac
3dfa0 6b 75 70 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58  kup Object.** EX
3dfb0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
3dfc0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63   The sqlite3_bac
3dfd0 6b 75 70 20 6f 62 6a 65 63 74 20 72 65 63 6f 72  kup object recor
3dfe0 64 73 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61  ds state informa
3dff0 74 69 6f 6e 20 61 62 6f 75 74 20 61 6e 20 6f 6e  tion about an on
3e000 67 6f 69 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20  going.** online 
3e010 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
3e020 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62  .  The sqlite3_b
3e030 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 69 73 20  ackup object is 
3e040 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 61 20  created by.** a 
3e050 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
3e060 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20  _backup_init()] 
3e070 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79 65 64  and is destroyed
3e080 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a   by a call to.**
3e090 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70   [sqlite3_backup
3e0a0 5f 66 69 6e 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a  _finish()]..**.*
3e0b0 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69  * See Also: [Usi
3e0c0 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e  ng the SQLite On
3e0d0 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d  line Backup API]
3e0e0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
3e0f0 63 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ct sqlite3_backu
3e100 70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  p sqlite3_backup
3e110 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3e120 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70  F: Online Backup
3e130 20 41 50 49 2e 0a 2a 2a 20 45 58 50 45 52 49 4d   API..** EXPERIM
3e140 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ENTAL.**.** This
3e150 20 41 50 49 20 69 73 20 75 73 65 64 20 74 6f 20   API is used to 
3e160 6f 76 65 72 77 72 69 74 65 20 74 68 65 20 63 6f  overwrite the co
3e170 6e 74 65 6e 74 73 20 6f 66 20 6f 6e 65 20 64 61  ntents of one da
3e180 74 61 62 61 73 65 20 77 69 74 68 20 74 68 61 74  tabase with that
3e190 0a 2a 2a 20 6f 66 20 61 6e 6f 74 68 65 72 2e 20  .** of another. 
3e1a0 49 74 20 69 73 20 75 73 65 66 75 6c 20 65 69 74  It is useful eit
3e1b0 68 65 72 20 66 6f 72 20 63 72 65 61 74 69 6e 67  her for creating
3e1c0 20 62 61 63 6b 75 70 73 20 6f 66 20 64 61 74 61   backups of data
3e1d0 62 61 73 65 73 20 6f 72 0a 2a 2a 20 66 6f 72 20  bases or.** for 
3e1e0 63 6f 70 79 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72  copying in-memor
3e1f0 79 20 64 61 74 61 62 61 73 65 73 20 74 6f 20 6f  y databases to o
3e200 72 20 66 72 6f 6d 20 70 65 72 73 69 73 74 65 6e  r from persisten
3e210 74 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20  t files. .**.** 
3e220 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67  See Also: [Using
3e230 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69   the SQLite Onli
3e240 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a  ne Backup API].*
3e250 2a 0a 2a 2a 20 45 78 63 6c 75 73 69 76 65 20 61  *.** Exclusive a
3e260 63 63 65 73 73 20 69 73 20 72 65 71 75 69 72 65  ccess is require
3e270 64 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61  d to the destina
3e280 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 66 6f  tion database fo
3e290 72 20 74 68 65 20 0a 2a 2a 20 64 75 72 61 74 69  r the .** durati
3e2a0 6f 6e 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  on of the operat
3e2b0 69 6f 6e 2e 20 48 6f 77 65 76 65 72 20 74 68 65  ion. However the
3e2c0 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
3e2d0 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64   is only.** read
3e2e0 2d 6c 6f 63 6b 65 64 20 77 68 69 6c 65 20 69 74  -locked while it
3e2f0 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69   is actually bei
3e300 6e 67 20 72 65 61 64 2c 20 69 74 20 69 73 20 6e  ng read, it is n
3e310 6f 74 20 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e  ot locked.** con
3e320 74 69 6e 75 6f 75 73 6c 79 20 66 6f 72 20 74 68  tinuously for th
3e330 65 20 65 6e 74 69 72 65 20 6f 70 65 72 61 74 69  e entire operati
3e340 6f 6e 2e 20 54 68 75 73 2c 20 74 68 65 20 62 61  on. Thus, the ba
3e350 63 6b 75 70 20 6d 61 79 20 62 65 0a 2a 2a 20 70  ckup may be.** p
3e360 65 72 66 6f 72 6d 65 64 20 6f 6e 20 61 20 6c 69  erformed on a li
3e370 76 65 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ve database with
3e380 6f 75 74 20 70 72 65 76 65 6e 74 69 6e 67 20 6f  out preventing o
3e390 74 68 65 72 20 75 73 65 72 73 20 66 72 6f 6d 0a  ther users from.
3e3a0 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68  ** writing to th
3e3b0 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 61  e database for a
3e3c0 6e 20 65 78 74 65 6e 64 65 64 20 70 65 72 69 6f  n extended perio
3e3d0 64 20 6f 66 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a  d of time..** .*
3e3e0 2a 20 54 6f 20 70 65 72 66 6f 72 6d 20 61 20 62  * To perform a b
3e3f0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 3a  ackup operation:
3e400 20 0a 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20   .**   <ol>.**  
3e410 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65     <li><b>sqlite
3e420 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c  3_backup_init()<
3e430 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  /b> is called on
3e440 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  ce to initialize
3e450 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
3e460 62 61 63 6b 75 70 2c 20 0a 2a 2a 20 20 20 20 20  backup, .**     
3e470 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62  <li><b>sqlite3_b
3e480 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e  ackup_step()</b>
3e490 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f   is called one o
3e4a0 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20  r more times to 
3e4b0 74 72 61 6e 73 66 65 72 20 0a 2a 2a 20 20 20 20  transfer .**    
3e4c0 20 20 20 20 20 74 68 65 20 64 61 74 61 20 62 65       the data be
3e4d0 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 64 61  tween the two da
3e4e0 74 61 62 61 73 65 73 2c 20 61 6e 64 20 66 69 6e  tabases, and fin
3e4f0 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e  ally.**     <li>
3e500 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  <b>sqlite3_backu
3e510 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 20 69  p_finish()</b> i
3e520 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 6c 65  s called to rele
3e530 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65  ase all resource
3e540 73 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 73  s .**         as
3e550 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
3e560 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  e backup operati
3e570 6f 6e 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a  on. .**   </ol>.
3e580 2a 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20  ** There should 
3e590 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63  be exactly one c
3e5a0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  all to sqlite3_b
3e5b0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66  ackup_finish() f
3e5c0 6f 72 20 65 61 63 68 0a 2a 2a 20 73 75 63 63 65  or each.** succe
3e5d0 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 73 71  ssful call to sq
3e5e0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69  lite3_backup_ini
3e5f0 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71  t()..**.** <b>sq
3e600 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69  lite3_backup_ini
3e610 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68  t()</b>.**.** Th
3e620 65 20 66 69 72 73 74 20 74 77 6f 20 61 72 67 75  e first two argu
3e630 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20  ments passed to 
3e640 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  [sqlite3_backup_
3e650 69 6e 69 74 28 29 5d 20 61 72 65 20 74 68 65 20  init()] are the 
3e660 64 61 74 61 62 61 73 65 0a 2a 2a 20 68 61 6e 64  database.** hand
3e670 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  le associated wi
3e680 74 68 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  th the destinati
3e690 6f 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  on database and 
3e6a0 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
3e6b0 65 20 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 74  e .** used to at
3e6c0 74 61 63 68 20 74 68 65 20 64 65 73 74 69 6e 61  tach the destina
3e6d0 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 74 6f  tion database to
3e6e0 20 74 68 65 20 68 61 6e 64 6c 65 2e 20 54 68 65   the handle. The
3e6f0 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a   database name.*
3e700 2a 20 69 73 20 22 6d 61 69 6e 22 20 66 6f 72 20  * is "main" for 
3e710 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
3e720 65 2c 20 22 74 65 6d 70 22 20 66 6f 72 20 74 68  e, "temp" for th
3e730 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
3e740 62 61 73 65 2c 20 6f 72 0a 2a 2a 20 74 68 65 20  base, or.** the 
3e750 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64 20 61  name specified a
3e760 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 5b 41  s part of the [A
3e770 54 54 41 43 48 5d 20 73 74 61 74 65 6d 65 6e 74  TTACH] statement
3e780 20 69 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   if the destinat
3e790 69 6f 6e 20 69 73 0a 2a 2a 20 61 6e 20 61 74 74  ion is.** an att
3e7a0 61 63 68 65 64 20 64 61 74 61 62 61 73 65 2e 20  ached database. 
3e7b0 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f  The third and fo
3e7c0 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 70  urth arguments p
3e7d0 61 73 73 65 64 20 74 6f 20 0a 2a 2a 20 73 71 6c  assed to .** sql
3e7e0 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
3e7f0 28 29 20 69 64 65 6e 74 69 66 79 20 74 68 65 20  () identify the 
3e800 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3e810 74 69 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 64 61 74  tion].** and dat
3e820 61 62 61 73 65 20 6e 61 6d 65 20 75 73 65 64 0a  abase name used.
3e830 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ** to access the
3e840 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
3e850 2e 20 54 68 65 20 76 61 6c 75 65 73 20 70 61 73  . The values pas
3e860 73 65 64 20 66 6f 72 20 74 68 65 20 73 6f 75 72  sed for the sour
3e870 63 65 20 61 6e 64 20 0a 2a 2a 20 64 65 73 74 69  ce and .** desti
3e880 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65  nation [database
3e890 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 61 72   connection] par
3e8a0 61 6d 65 74 65 72 73 20 6d 75 73 74 20 6e 6f 74  ameters must not
3e8b0 20 62 65 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a   be the same..**
3e8c0 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
3e8d0 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 73 71  occurs within sq
3e8e0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69  lite3_backup_ini
3e8f0 74 28 29 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69  t(), then NULL i
3e900 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e  s returned.** an
3e910 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  d an error code 
3e920 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67  and error messag
3e930 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
3e940 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
3e950 6e 65 63 74 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73  nection] .** pas
3e960 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
3e970 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 79 20   argument. They 
3e980 6d 61 79 20 62 65 20 72 65 74 72 69 65 76 65 64  may be retrieved
3e990 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73   using the.** [s
3e9a0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
3e9b0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  ], [sqlite3_errm
3e9c0 73 67 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69  sg()], and [sqli
3e9d0 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20  te3_errmsg16()] 
3e9e0 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74  functions..** Ot
3e9f0 68 65 72 77 69 73 65 2c 20 69 66 20 73 75 63 63  herwise, if succ
3ea00 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e 74 65  essful, a pointe
3ea10 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74 65 33  r to an [sqlite3
3ea20 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20  _backup] object 
3ea30 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20  is.** returned. 
3ea40 54 68 69 73 20 70 6f 69 6e 74 65 72 20 6d 61 79  This pointer may
3ea50 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68   be used with th
3ea60 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  e sqlite3_backup
3ea70 5f 73 74 65 70 28 29 20 61 6e 64 0a 2a 2a 20 73  _step() and.** s
3ea80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
3ea90 6e 69 73 68 28 29 20 66 75 6e 63 74 69 6f 6e 73  nish() functions
3eaa0 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20   to perform the 
3eab0 73 70 65 63 69 66 69 65 64 20 62 61 63 6b 75 70  specified backup
3eac0 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 0a   .** operation..
3ead0 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33  **.** <b>sqlite3
3eae0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f  _backup_step()</
3eaf0 62 3e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  b>.**.** Functio
3eb00 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  n [sqlite3_backu
3eb10 70 5f 73 74 65 70 28 29 5d 20 69 73 20 75 73 65  p_step()] is use
3eb20 64 20 74 6f 20 63 6f 70 79 20 75 70 20 74 6f 20  d to copy up to 
3eb30 6e 50 61 67 65 20 70 61 67 65 73 20 62 65 74 77  nPage pages betw
3eb40 65 65 6e 20 0a 2a 2a 20 74 68 65 20 73 6f 75 72  een .** the sour
3eb50 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69  ce and destinati
3eb60 6f 6e 20 64 61 74 61 62 61 73 65 73 2c 20 77 68  on databases, wh
3eb70 65 72 65 20 6e 50 61 67 65 20 69 73 20 74 68 65  ere nPage is the
3eb80 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 0a 2a   value of the .*
3eb90 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  * second paramet
3eba0 65 72 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  er passed to sql
3ebb0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
3ebc0 28 29 2e 20 49 66 20 6e 50 61 67 65 20 69 73 20  (). If nPage is 
3ebd0 61 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 76 61  a negative.** va
3ebe0 6c 75 65 2c 20 61 6c 6c 20 72 65 6d 61 69 6e 69  lue, all remaini
3ebf0 6e 67 20 73 6f 75 72 63 65 20 70 61 67 65 73 20  ng source pages 
3ec00 61 72 65 20 63 6f 70 69 65 64 2e 20 49 66 20 74  are copied. If t
3ec10 68 65 20 72 65 71 75 69 72 65 64 20 70 61 67 65  he required page
3ec20 73 20 61 72 65 20 0a 2a 2a 20 73 75 63 63 65 73  s are .** succes
3ec30 66 75 6c 6c 79 20 63 6f 70 69 65 64 2c 20 62 75  fully copied, bu
3ec40 74 20 74 68 65 72 65 20 61 72 65 20 73 74 69 6c  t there are stil
3ec50 6c 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 20  l more pages to 
3ec60 63 6f 70 79 20 62 65 66 6f 72 65 20 74 68 65 20  copy before the 
3ec70 0a 2a 2a 20 62 61 63 6b 75 70 20 69 73 20 63 6f  .** backup is co
3ec80 6d 70 6c 65 74 65 2c 20 69 74 20 72 65 74 75 72  mplete, it retur
3ec90 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20  ns [SQLITE_OK]. 
3eca0 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75  If no error occu
3ecb0 72 65 64 20 61 6e 64 20 74 68 65 72 65 20 0a 2a  red and there .*
3ecc0 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61  * are no more pa
3ecd0 67 65 73 20 74 6f 20 63 6f 70 79 2c 20 74 68 65  ges to copy, the
3ece0 6e 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20  n [SQLITE_DONE] 
3ecf0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
3ed00 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63  an error .** occ
3ed10 75 72 73 2c 20 74 68 65 6e 20 61 6e 20 53 51 4c  urs, then an SQL
3ed20 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ite error code i
3ed30 73 20 72 65 74 75 72 6e 65 64 2e 20 41 73 20 77  s returned. As w
3ed40 65 6c 6c 20 61 73 20 5b 53 51 4c 49 54 45 5f 4f  ell as [SQLITE_O
3ed50 4b 5d 20 61 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54  K] and.** [SQLIT
3ed60 45 5f 44 4f 4e 45 5d 2c 20 61 20 63 61 6c 6c 20  E_DONE], a call 
3ed70 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
3ed80 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 65 74  p_step() may ret
3ed90 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44  urn [SQLITE_READ
3eda0 4f 4e 4c 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  ONLY],.** [SQLIT
3edb0 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54  E_NOMEM], [SQLIT
3edc0 45 5f 42 55 53 59 5d 2c 20 5b 53 51 4c 49 54 45  E_BUSY], [SQLITE
3edd0 5f 4c 4f 43 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a  _LOCKED], or an.
3ede0 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  ** [SQLITE_IOERR
3edf0 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45  _ACCESS | SQLITE
3ee00 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 78 74 65  _IOERR_XXX] exte
3ee10 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 2e  nded error code.
3ee20 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61  .**.** As well a
3ee30 73 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65  s the case where
3ee40 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
3ee50 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
3ee60 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a  as opened for.**
3ee70 20 72 65 61 64 2d 6f 6e 6c 79 20 61 63 63 65 73   read-only acces
3ee80 73 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  s, sqlite3_backu
3ee90 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 65 74  p_step() may ret
3eea0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44  urn [SQLITE_READ
3eeb0 4f 4e 4c 59 5d 20 69 66 0a 2a 2a 20 74 68 65 20  ONLY] if.** the 
3eec0 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61  destination is a
3eed0 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
3eee0 62 61 73 65 20 77 69 74 68 20 61 20 64 69 66 66  base with a diff
3eef0 65 72 65 6e 74 20 70 61 67 65 20 73 69 7a 65 0a  erent page size.
3ef00 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72  ** from the sour
3ef10 63 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  ce database..**.
3ef20 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 62 61  ** If sqlite3_ba
3ef30 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 6e  ckup_step() cann
3ef40 6f 74 20 6f 62 74 61 69 6e 20 61 20 72 65 71 75  ot obtain a requ
3ef50 69 72 65 64 20 66 69 6c 65 2d 73 79 73 74 65 6d  ired file-system
3ef60 20 6c 6f 63 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74   lock, then.** t
3ef70 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  he [sqlite3_busy
3ef80 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 2d  _handler | busy-
3ef90 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e  handler function
3efa0 5d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20  ].** is invoked 
3efb0 28 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69  (if one is speci
3efc0 66 69 65 64 29 2e 20 49 66 20 74 68 65 20 0a 2a  fied). If the .*
3efd0 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 72  * busy-handler r
3efe0 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20  eturns non-zero 
3eff0 62 65 66 6f 72 65 20 74 68 65 20 6c 6f 63 6b 20  before the lock 
3f000 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68  is available, th
3f010 65 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42  en .** [SQLITE_B
3f020 55 53 59 5d 20 69 73 20 72 65 74 75 72 6e 65 64  USY] is returned
3f030 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   to the caller. 
3f040 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
3f050 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69   call to.** sqli
3f060 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
3f070 29 20 63 61 6e 20 62 65 20 72 65 74 72 69 65 64  ) can be retried
3f080 20 6c 61 74 65 72 2e 20 49 66 20 74 68 65 20 73   later. If the s
3f090 6f 75 72 63 65 0a 2a 2a 20 5b 64 61 74 61 62 61  ource.** [databa
3f0a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a  se connection].*
3f0b0 2a 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20  * is being used 
3f0c0 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20  to write to the 
3f0d0 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
3f0e0 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 62 61 63  when sqlite3_bac
3f0f0 6b 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20 69 73  kup_step().** is
3f100 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 5b 53   called, then [S
3f110 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 69 73  QLITE_LOCKED] is
3f120 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69   returned immedi
3f130 61 74 65 6c 79 2e 20 41 67 61 69 6e 2c 20 69 6e  ately. Again, in
3f140 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68   this.** case th
3f150 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
3f160 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
3f170 63 61 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c  can be retried l
3f180 61 74 65 72 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b  ater on. If.** [
3f190 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43  SQLITE_IOERR_ACC
3f1a0 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45  ESS | SQLITE_IOE
3f1b0 52 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45  RR_XXX], [SQLITE
3f1c0 5f 4e 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b  _NOMEM], or.** [
3f1d0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
3f1e0 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
3f1f0 65 6e 20 0a 2a 2a 20 74 68 65 72 65 20 69 73 20  en .** there is 
3f200 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65 74 72  no point in retr
3f210 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f  ying the call to
3f220 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
3f230 73 74 65 70 28 29 2e 20 54 68 65 73 65 20 0a 2a  step(). These .*
3f240 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63 6f 6e  * errors are con
3f250 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e 20 41  sidered fatal. A
3f260 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
3f270 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
3f280 74 20 61 63 63 65 70 74 20 0a 2a 2a 20 74 68 61  t accept .** tha
3f290 74 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65  t the backup ope
3f2a0 72 61 74 69 6f 6e 20 68 61 73 20 66 61 69 6c 65  ration has faile
3f2b0 64 20 61 6e 64 20 70 61 73 73 20 74 68 65 20 62  d and pass the b
3f2c0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20  ackup operation 
3f2d0 68 61 6e 64 6c 65 20 0a 2a 2a 20 74 6f 20 74 68  handle .** to th
3f2e0 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  e sqlite3_backup
3f2f0 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 72 65 6c  _finish() to rel
3f300 65 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 20  ease associated 
3f310 72 65 73 6f 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a  resources..**.**
3f320 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 66   Following the f
3f330 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  irst call to sql
3f340 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
3f350 28 29 2c 20 61 6e 20 65 78 63 6c 75 73 69 76 65  (), an exclusive
3f360 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61   lock is.** obta
3f370 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 65 73 74  ined on the dest
3f380 69 6e 61 74 69 6f 6e 20 66 69 6c 65 2e 20 49 74  ination file. It
3f390 20 69 73 20 6e 6f 74 20 72 65 6c 65 61 73 65 64   is not released
3f3a0 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 0a 2a   until either .*
3f3b0 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  * sqlite3_backup
3f3c0 5f 66 69 6e 69 73 68 28 29 20 69 73 20 63 61 6c  _finish() is cal
3f3d0 6c 65 64 20 6f 72 20 74 68 65 20 62 61 63 6b 75  led or the backu
3f3e0 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 63  p operation is c
3f3f0 6f 6d 70 6c 65 74 65 20 0a 2a 2a 20 61 6e 64 20  omplete .** and 
3f400 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
3f410 74 65 70 28 29 20 72 65 74 75 72 6e 73 20 5b 53  tep() returns [S
3f420 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 41 64 64  QLITE_DONE]. Add
3f430 69 74 69 6f 6e 61 6c 6c 79 2c 20 65 61 63 68 20  itionally, each 
3f440 74 69 6d 65 20 0a 2a 2a 20 61 20 63 61 6c 6c 20  time .** a call 
3f450 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
3f460 70 5f 73 74 65 70 28 29 20 69 73 20 6d 61 64 65  p_step() is made
3f470 20 61 20 5b 73 68 61 72 65 64 20 6c 6f 63 6b 5d   a [shared lock]
3f480 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 0a   is obtained on.
3f490 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  ** the source da
3f4a0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69  tabase file. Thi
3f4b0 73 20 6c 6f 63 6b 20 69 73 20 72 65 6c 65 61 73  s lock is releas
3f4c0 65 64 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a  ed before the.**
3f4d0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
3f4e0 73 74 65 70 28 29 20 63 61 6c 6c 20 72 65 74 75  step() call retu
3f4f0 72 6e 73 2e 20 42 65 63 61 75 73 65 20 74 68 65  rns. Because the
3f500 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
3f510 20 69 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65   is not.** locke
3f520 64 20 62 65 74 77 65 65 6e 20 63 61 6c 6c 73 20  d between calls 
3f530 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
3f540 70 5f 73 74 65 70 28 29 2c 20 69 74 20 6d 61 79  p_step(), it may
3f550 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6d 69 64   be modified mid
3f560 2d 77 61 79 0a 2a 2a 20 74 68 72 6f 75 67 68 20  -way.** through 
3f570 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65  the backup proce
3f580 64 75 72 65 2e 20 49 66 20 74 68 65 20 73 6f 75  dure. If the sou
3f590 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
3f5a0 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a  modified by an.*
3f5b0 2a 20 65 78 74 65 72 6e 61 6c 20 70 72 6f 63 65  * external proce
3f5c0 73 73 20 6f 72 20 76 69 61 20 61 20 64 61 74 61  ss or via a data
3f5d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
3f5e0 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f  other than the o
3f5f0 6e 65 20 62 65 69 6e 67 0a 2a 2a 20 75 73 65 64  ne being.** used
3f600 20 62 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f   by the backup o
3f610 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74  peration, then t
3f620 68 65 20 62 61 63 6b 75 70 20 77 69 6c 6c 20 62  he backup will b
3f630 65 20 74 72 61 6e 73 70 61 72 65 6e 74 6c 79 0a  e transparently.
3f640 2a 2a 20 72 65 73 74 61 72 74 65 64 20 62 79 20  ** restarted by 
3f650 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  the next call to
3f660 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
3f670 73 74 65 70 28 29 2e 20 49 66 20 74 68 65 20 73  step(). If the s
3f680 6f 75 72 63 65 20 0a 2a 2a 20 64 61 74 61 62 61  ource .** databa
3f690 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62  se is modified b
3f6a0 79 20 74 68 65 20 75 73 69 6e 67 20 74 68 65 20  y the using the 
3f6b0 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
3f6c0 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73 20 75  nnection as is u
3f6d0 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 62 61  sed.** by the ba
3f6e0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20  ckup operation, 
3f6f0 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20  then the backup 
3f700 64 61 74 61 62 61 73 65 20 69 73 20 74 72 61 6e  database is tran
3f710 73 70 61 72 65 6e 74 6c 79 20 0a 2a 2a 20 75 70  sparently .** up
3f720 64 61 74 65 64 20 61 74 20 74 68 65 20 73 61 6d  dated at the sam
3f730 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62  e time..**.** <b
3f740 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  >sqlite3_backup_
3f750 66 69 6e 69 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a  finish()</b>.**.
3f760 2a 2a 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f  ** Once sqlite3_
3f770 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 68 61  backup_step() ha
3f780 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49  s returned [SQLI
3f790 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20 77 68 65  TE_DONE], or whe
3f7a0 6e 20 74 68 65 20 0a 2a 2a 20 61 70 70 6c 69 63  n the .** applic
3f7b0 61 74 69 6f 6e 20 77 69 73 68 65 73 20 74 6f 20  ation wishes to 
3f7c0 61 62 61 6e 64 6f 6e 20 74 68 65 20 62 61 63 6b  abandon the back
3f7d0 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68  up operation, th
3f7e0 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  e [sqlite3_backu
3f7f0 70 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 73 68 6f  p].** object sho
3f800 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 74 6f  uld be passed to
3f810 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
3f820 66 69 6e 69 73 68 28 29 2e 20 54 68 69 73 20 72  finish(). This r
3f830 65 6c 65 61 73 65 73 20 61 6c 6c 0a 2a 2a 20 72  eleases all.** r
3f840 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61  esources associa
3f850 74 65 64 20 77 69 74 68 20 74 68 65 20 62 61 63  ted with the bac
3f860 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49  kup operation. I
3f870 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  f sqlite3_backup
3f880 5f 73 74 65 70 28 29 0a 2a 2a 20 68 61 73 20 6e  _step().** has n
3f890 6f 74 20 79 65 74 20 72 65 74 75 72 6e 65 64 20  ot yet returned 
3f8a0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 74  [SQLITE_DONE], t
3f8b0 68 65 6e 20 61 6e 79 20 61 63 74 69 76 65 20 77  hen any active w
3f8c0 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
3f8d0 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 65 73 74 69   on the.** desti
3f8e0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
3f8f0 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  is rolled back. 
3f900 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63  The [sqlite3_bac
3f910 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 20 69  kup] object is i
3f920 6e 76 61 6c 69 64 0a 2a 2a 20 61 6e 64 20 6d 61  nvalid.** and ma
3f930 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 66 6f  y not be used fo
3f940 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c 20 74  llowing a call t
3f950 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
3f960 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a  _finish()..**.**
3f970 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
3f980 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62  ned by sqlite3_b
3f990 61 63 6b 75 70 5f 66 69 6e 69 73 68 20 69 73 20  ackup_finish is 
3f9a0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 6e  [SQLITE_OK] if n
3f9b0 6f 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72  o error.** occur
3f9c0 72 65 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20  red, regardless 
3f9d0 6f 72 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  or whether or no
3f9e0 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t sqlite3_backup
3f9f0 5f 73 74 65 70 28 29 20 77 61 73 20 63 61 6c 6c  _step() was call
3fa00 65 64 0a 2a 2a 20 61 20 73 75 66 66 69 63 69 65  ed.** a sufficie
3fa10 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  nt number of tim
3fa20 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74  es to complete t
3fa30 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
3fa40 69 6f 6e 2e 20 4f 72 2c 20 69 66 0a 2a 2a 20 61  ion. Or, if.** a
3fa50 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20  n out-of-memory 
3fa60 63 6f 6e 64 69 74 69 6f 6e 20 6f 72 20 49 4f 20  condition or IO 
3fa70 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 64 75  error occured du
3fa80 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 0a 2a  ring a call to.*
3fa90 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  * sqlite3_backup
3faa0 5f 73 74 65 70 28 29 20 74 68 65 6e 20 5b 53 51  _step() then [SQ
3fab0 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61  LITE_NOMEM] or a
3fac0 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45  n.** [SQLITE_IOE
3fad0 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49  RR_ACCESS | SQLI
3fae0 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 72  TE_IOERR_XXX] er
3faf0 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20 72  ror code.** is r
3fb00 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73  eturned. In this
3fb10 20 63 61 73 65 20 74 68 65 20 65 72 72 6f 72 20   case the error 
3fb20 63 6f 64 65 20 61 6e 64 20 61 6e 20 65 72 72 6f  code and an erro
3fb30 72 20 6d 65 73 73 61 67 65 20 61 72 65 0a 2a 2a  r message are.**
3fb40 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
3fb50 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74  destination [dat
3fb60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3fb70 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 74 75 72  ]..**.** A retur
3fb80 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42 55 53  n of [SQLITE_BUS
3fb90 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f  Y] or [SQLITE_LO
3fba0 43 4b 45 44 5d 20 66 72 6f 6d 20 73 71 6c 69 74  CKED] from sqlit
3fbb0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
3fbc0 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 65 72   is.** not a per
3fbd0 6d 61 6e 65 6e 74 20 65 72 72 6f 72 20 61 6e 64  manent error and
3fbe0 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74   does not affect
3fbf0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
3fc00 65 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  e of.** sqlite3_
3fc10 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e  backup_finish().
3fc20 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65  .**.** <b>sqlite
3fc30 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69  3_backup_remaini
3fc40 6e 67 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 61  ng(), sqlite3_ba
3fc50 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29  ckup_pagecount()
3fc60 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  </b>.**.** Each 
3fc70 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
3fc80 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 73 65  backup_step() se
3fc90 74 73 20 74 77 6f 20 76 61 6c 75 65 73 20 73 74  ts two values st
3fca0 6f 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a  ored internally.
3fcb0 2a 2a 20 62 79 20 61 6e 20 5b 73 71 6c 69 74 65  ** by an [sqlite
3fcc0 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74  3_backup] object
3fcd0 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  . The number of 
3fce0 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f 20 62  pages still to b
3fcf0 65 20 62 61 63 6b 65 64 0a 2a 2a 20 75 70 2c 20  e backed.** up, 
3fd00 77 68 69 63 68 20 6d 61 79 20 62 65 20 71 75 65  which may be que
3fd10 72 69 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ried by sqlite3_
3fd20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67  backup_remaining
3fd30 28 29 2c 20 61 6e 64 20 74 68 65 20 74 6f 74 61  (), and the tota
3fd40 6c 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70  l.** number of p
3fd50 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f 75 72  ages in the sour
3fd60 63 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ce database file
3fd70 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 71  , which may be q
3fd80 75 65 72 69 65 64 20 62 79 0a 2a 2a 20 73 71 6c  ueried by.** sql
3fd90 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65  ite3_backup_page
3fda0 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  count()..**.** T
3fdb0 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  he values return
3fdc0 65 64 20 62 79 20 74 68 65 73 65 20 66 75 6e 63  ed by these func
3fdd0 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20 75  tions are only u
3fde0 70 64 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c  pdated by.** sql
3fdf0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
3fe00 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63  (). If the sourc
3fe10 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f  e database is mo
3fe20 64 69 66 69 65 64 20 64 75 72 69 6e 67 20 61 20  dified during a 
3fe30 62 61 63 6b 75 70 0a 2a 2a 20 6f 70 65 72 61 74  backup.** operat
3fe40 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 76 61  ion, then the va
3fe50 6c 75 65 73 20 61 72 65 20 6e 6f 74 20 75 70 64  lues are not upd
3fe60 61 74 65 64 20 74 6f 20 61 63 63 6f 75 6e 74 20  ated to account 
3fe70 66 6f 72 20 61 6e 79 20 65 78 74 72 61 0a 2a 2a  for any extra.**
3fe80 20 70 61 67 65 73 20 74 68 61 74 20 6e 65 65 64   pages that need
3fe90 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 6f   to be updated o
3fea0 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  r the size of th
3feb0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
3fec0 65 20 66 69 6c 65 0a 2a 2a 20 63 68 61 6e 67 69  e file.** changi
3fed0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e  ng..**.** <b>Con
3fee0 63 75 72 72 65 6e 74 20 55 73 61 67 65 20 6f 66  current Usage of
3fef0 20 44 61 74 61 62 61 73 65 20 48 61 6e 64 6c 65   Database Handle
3ff00 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  s</b>.**.** The 
3ff10 73 6f 75 72 63 65 20 5b 64 61 74 61 62 61 73 65  source [database
3ff20 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6d 61 79   connection] may
3ff30 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20   be used by the 
3ff40 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 6f 72 20  application for 
3ff50 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65  other.** purpose
3ff60 73 20 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70  s while a backup
3ff70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 6e   operation is un
3ff80 64 65 72 77 61 79 20 6f 72 20 62 65 69 6e 67 20  derway or being 
3ff90 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20  initialized..** 
3ffa0 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
3ffb0 70 69 6c 65 64 20 61 6e 64 20 63 6f 6e 66 69 67  piled and config
3ffc0 75 72 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20  ured to support 
3ffd0 74 68 72 65 61 64 73 61 66 65 20 64 61 74 61 62  threadsafe datab
3ffe0 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
3fff0 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 6f 75  ns, then the sou
40000 72 63 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  rce database con
40010 6e 65 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 75  nection may be u
40020 73 65 64 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79  sed concurrently
40030 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  .** from within 
40040 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e 0a 2a  other threads..*
40050 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68  *.** However, th
40060 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  e application mu
40070 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  st guarantee tha
40080 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  t the destinatio
40090 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  n database.** co
400a0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
400b0 69 73 20 6e 6f 74 20 70 61 73 73 65 64 20 74 6f  is not passed to
400c0 20 61 6e 79 20 6f 74 68 65 72 20 41 50 49 20 28   any other API (
400d0 62 79 20 61 6e 79 20 74 68 72 65 61 64 29 20 61  by any thread) a
400e0 66 74 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33  fter .** sqlite3
400f0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69  _backup_init() i
40100 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 62 65 66  s called and bef
40110 6f 72 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f  ore the correspo
40120 6e 64 69 6e 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a  nding call to.**
40130 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
40140 66 69 6e 69 73 68 28 29 2e 20 55 6e 66 6f 72 74  finish(). Unfort
40150 75 6e 61 74 65 6c 79 20 53 51 4c 69 74 65 20 64  unately SQLite d
40160 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c  oes not currentl
40170 79 20 63 68 65 63 6b 0a 2a 2a 20 66 6f 72 20 74  y check.** for t
40180 68 69 73 2c 20 69 66 20 74 68 65 20 61 70 70 6c  his, if the appl
40190 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 75 73 65  ication does use
401a0 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
401b0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
401c0 63 74 69 6f 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f  ction].** for so
401d0 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65  me other purpose
401e0 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70   during a backup
401f0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 6e   operation, thin
40200 67 73 20 6d 61 79 20 61 70 70 65 61 72 20 74 6f  gs may appear to
40210 0a 2a 2a 20 77 6f 72 6b 20 63 6f 72 72 65 63 74  .** work correct
40220 6c 79 20 62 75 74 20 69 6e 20 66 61 63 74 20 62  ly but in fact b
40230 65 20 73 75 62 74 6c 79 20 6d 61 6c 66 75 6e 63  e subtly malfunc
40240 74 69 6f 6e 69 6e 67 2e 20 20 55 73 65 20 6f 66  tioning.  Use of
40250 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74   the.** destinat
40260 69 6f 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ion database con
40270 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 61 20  nection while a 
40280 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70 72 6f  backup is in pro
40290 67 72 65 73 73 20 6d 69 67 68 74 0a 2a 2a 20 61  gress might.** a
402a0 6c 73 6f 20 63 61 75 73 65 20 61 20 6d 75 74 65  lso cause a mute
402b0 78 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  x deadlock..**.*
402c0 2a 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 69  * Furthermore, i
402d0 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 5b 73 68  f running in [sh
402e0 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d  ared cache mode]
402f0 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  , the applicatio
40300 6e 20 6d 75 73 74 0a 2a 2a 20 67 75 61 72 61 6e  n must.** guaran
40310 74 65 65 20 74 68 61 74 20 74 68 65 20 73 68 61  tee that the sha
40320 72 65 64 20 63 61 63 68 65 20 75 73 65 64 20 62  red cache used b
40330 79 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  y the destinatio
40340 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73  n database.** is
40350 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 77 68   not accessed wh
40360 69 6c 65 20 74 68 65 20 62 61 63 6b 75 70 20 69  ile the backup i
40370 73 20 72 75 6e 6e 69 6e 67 2e 20 49 6e 20 70 72  s running. In pr
40380 61 63 74 69 63 65 20 74 68 69 73 20 6d 65 61 6e  actice this mean
40390 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 61 70  s.** that the ap
403a0 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67  plication must g
403b0 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68  uarantee that th
403c0 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 69  e file-system fi
403d0 6c 65 20 62 65 69 6e 67 20 0a 2a 2a 20 62 61 63  le being .** bac
403e0 6b 65 64 20 75 70 20 74 6f 20 69 73 20 6e 6f 74  ked up to is not
403f0 20 61 63 63 65 73 73 65 64 20 62 79 20 61 6e 79   accessed by any
40400 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68   connection with
40410 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 2c 0a  in the process,.
40420 2a 2a 20 6e 6f 74 20 6a 75 73 74 20 74 68 65 20  ** not just the 
40430 73 70 65 63 69 66 69 63 20 63 6f 6e 6e 65 63 74  specific connect
40440 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 61 73  ion that was pas
40450 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  sed to sqlite3_b
40460 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a  ackup_init()..**
40470 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
40480 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20  _backup] object 
40490 69 74 73 65 6c 66 20 69 73 20 70 61 72 74 69 61  itself is partia
404a0 6c 6c 79 20 74 68 72 65 61 64 73 61 66 65 2e 20  lly threadsafe. 
404b0 4d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 68 72  Multiple .** thr
404c0 65 61 64 73 20 6d 61 79 20 73 61 66 65 6c 79 20  eads may safely 
404d0 6d 61 6b 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f  make multiple co
404e0 6e 63 75 72 72 65 6e 74 20 63 61 6c 6c 73 20 74  ncurrent calls t
404f0 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
40500 5f 73 74 65 70 28 29 2e 0a 2a 2a 20 48 6f 77 65  _step()..** Howe
40510 76 65 72 2c 20 74 68 65 20 73 71 6c 69 74 65 33  ver, the sqlite3
40520 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e  _backup_remainin
40530 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  g() and sqlite3_
40540 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
40550 28 29 0a 2a 2a 20 41 50 49 73 20 61 72 65 20 6e  ().** APIs are n
40560 6f 74 20 73 74 72 69 63 74 6c 79 20 73 70 65 61  ot strictly spea
40570 6b 69 6e 67 20 74 68 72 65 61 64 73 61 66 65 2e  king threadsafe.
40580 20 49 66 20 74 68 65 79 20 61 72 65 20 69 6e 76   If they are inv
40590 6f 6b 65 64 20 61 74 20 74 68 65 0a 2a 2a 20 73  oked at the.** s
405a0 61 6d 65 20 74 69 6d 65 20 61 73 20 61 6e 6f 74  ame time as anot
405b0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 69 6e  her thread is in
405c0 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 62  voking sqlite3_b
405d0 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 74 20  ackup_step() it 
405e0 69 73 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74  is.** possible t
405f0 68 61 74 20 74 68 65 79 20 72 65 74 75 72 6e 20  hat they return 
40600 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 73 2e 0a  invalid values..
40610 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
40620 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71  lite3_backup *sq
40630 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69  lite3_backup_ini
40640 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 44  t(.  sqlite3 *pD
40650 65 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  est,            
40660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
40670 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
40680 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
40690 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 65 73  const char *zDes
406a0 74 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20  tName,          
406b0 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e         /* Destin
406c0 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 6e  ation database n
406d0 61 6d 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ame */.  sqlite3
406e0 20 2a 70 53 6f 75 72 63 65 2c 20 20 20 20 20 20   *pSource,      
406f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40700 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 62 61  /* Source databa
40710 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  se handle */.  c
40720 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 6f 75 72  onst char *zSour
40730 63 65 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20  ceName          
40740 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
40750 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  database name */
40760 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
40770 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  nt sqlite3_backu
40780 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 62  p_step(sqlite3_b
40790 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50  ackup *p, int nP
407a0 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  age);.SQLITE_API
407b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63   int sqlite3_bac
407c0 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c 69 74  kup_finish(sqlit
407d0 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53  e3_backup *p);.S
407e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
407f0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d  lite3_backup_rem
40800 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62  aining(sqlite3_b
40810 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54  ackup *p);.SQLIT
40820 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
40830 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75  3_backup_pagecou
40840 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  nt(sqlite3_backu
40850 70 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  p *p);../*.** CA
40860 50 49 33 52 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e  PI3REF: Unlock N
40870 6f 74 69 66 69 63 61 74 69 6f 6e 0a 2a 2a 20 45  otification.** E
40880 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
40890 2a 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69  * When running i
408a0 6e 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d  n shared-cache m
408b0 6f 64 65 2c 20 61 20 64 61 74 61 62 61 73 65 20  ode, a database 
408c0 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20 66 61  operation may fa
408d0 69 6c 20 77 69 74 68 0a 2a 2a 20 61 6e 20 5b 53  il with.** an [S
408e0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 65 72  QLITE_LOCKED] er
408f0 72 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 69  ror if the requi
40900 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65  red locks on the
40910 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6f 72   shared-cache or
40920 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74  .** individual t
40930 61 62 6c 65 73 20 77 69 74 68 69 6e 20 74 68 65  ables within the
40940 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 61   shared-cache ca
40950 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
40960 2e 20 53 65 65 0a 2a 2a 20 5b 53 51 4c 69 74 65  . See.** [SQLite
40970 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f   Shared-Cache Mo
40980 64 65 5d 20 66 6f 72 20 61 20 64 65 73 63 72 69  de] for a descri
40990 70 74 69 6f 6e 20 6f 66 20 73 68 61 72 65 64 2d  ption of shared-
409a0 63 61 63 68 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a  cache locking. .
409b0 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61 79 20  ** This API may 
409c0 62 65 20 75 73 65 64 20 74 6f 20 72 65 67 69 73  be used to regis
409d0 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74  ter a callback t
409e0 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  hat SQLite will 
409f0 69 6e 76 6f 6b 65 20 0a 2a 2a 20 77 68 65 6e 20  invoke .** when 
40a00 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  the connection c
40a10 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67  urrently holding
40a20 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f   the required lo
40a30 63 6b 20 72 65 6c 69 6e 71 75 69 73 68 65 73 20  ck relinquishes 
40a40 69 74 2e 0a 2a 2a 20 54 68 69 73 20 41 50 49 20  it..** This API 
40a50 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
40a60 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79  e if the library
40a70 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69   was compiled wi
40a80 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54  th the.** [SQLIT
40a90 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f  E_ENABLE_UNLOCK_
40aa0 4e 4f 54 49 46 59 5d 20 43 2d 70 72 65 70 72 6f  NOTIFY] C-prepro
40ab0 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65  cessor symbol de
40ac0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  fined..**.** See
40ad0 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68   Also: [Using th
40ae0 65 20 53 51 4c 69 74 65 20 55 6e 6c 6f 63 6b 20  e SQLite Unlock 
40af0 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 46 65 61  Notification Fea
40b00 74 75 72 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61  ture]..**.** Sha
40b10 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b 73 20  red-cache locks 
40b20 61 72 65 20 72 65 6c 65 61 73 65 64 20 77 68 65  are released whe
40b30 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  n a database con
40b40 6e 65 63 74 69 6f 6e 20 63 6f 6e 63 6c 75 64 65  nection conclude
40b50 73 0a 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74  s.** its current
40b60 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 65 69   transaction, ei
40b70 74 68 65 72 20 62 79 20 63 6f 6d 6d 69 74 74 69  ther by committi
40b80 6e 67 20 69 74 20 6f 72 20 72 6f 6c 6c 69 6e 67  ng it or rolling
40b90 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a   it back. .**.**
40ba0 20 57 68 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69   When a connecti
40bb0 6f 6e 20 28 6b 6e 6f 77 6e 20 61 73 20 74 68 65  on (known as the
40bc0 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
40bd0 69 6f 6e 29 20 66 61 69 6c 73 20 74 6f 20 6f 62  ion) fails to ob
40be0 74 61 69 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64  tain a.** shared
40bf0 2d 63 61 63 68 65 20 6c 6f 63 6b 20 61 6e 64 20  -cache lock and 
40c00 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73  SQLITE_LOCKED is
40c10 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
40c20 20 63 61 6c 6c 65 72 2c 20 74 68 65 0a 2a 2a 20   caller, the.** 
40c30 69 64 65 6e 74 69 74 79 20 6f 66 20 74 68 65 20  identity of the 
40c40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
40c50 69 6f 6e 20 28 74 68 65 20 62 6c 6f 63 6b 69 6e  ion (the blockin
40c60 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68  g connection) th
40c70 61 74 0a 2a 2a 20 68 61 73 20 6c 6f 63 6b 65 64  at.** has locked
40c80 20 74 68 65 20 72 65 71 75 69 72 65 64 20 72 65   the required re
40c90 73 6f 75 72 63 65 20 69 73 20 73 74 6f 72 65 64  source is stored
40ca0 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 41 66 74   internally. Aft
40cb0 65 72 20 61 6e 20 0a 2a 2a 20 61 70 70 6c 69 63  er an .** applic
40cc0 61 74 69 6f 6e 20 72 65 63 65 69 76 65 73 20 61  ation receives a
40cd0 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  n SQLITE_LOCKED 
40ce0 65 72 72 6f 72 2c 20 69 74 20 6d 61 79 20 63 61  error, it may ca
40cf0 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  ll the.** sqlite
40d00 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
40d10 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20 74 68  ) method with th
40d20 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
40d30 74 69 6f 6e 20 68 61 6e 64 6c 65 20 61 73 20 0a  tion handle as .
40d40 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 72 67  ** the first arg
40d50 75 6d 65 6e 74 20 74 6f 20 72 65 67 69 73 74 65  ument to registe
40d60 72 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b  r for a callback
40d70 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e   that will be in
40d80 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68  voked.** when th
40d90 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65  e blocking conne
40da0 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 20 74  ctions current t
40db0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
40dc0 6e 63 6c 75 64 65 64 2e 20 54 68 65 0a 2a 2a 20  ncluded. The.** 
40dd0 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
40de0 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ked from within 
40df0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  the [sqlite3_ste
40e00 70 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63  p] or [sqlite3_c
40e10 6c 6f 73 65 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68  lose].** call th
40e20 61 74 20 63 6f 6e 63 6c 75 64 65 73 20 74 68 65  at concludes the
40e30 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63   blocking connec
40e40 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f  tions transactio
40e50 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69  n..**.** If sqli
40e60 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
40e70 79 28 29 20 69 73 20 63 61 6c 6c 65 64 20 69 6e  y() is called in
40e80 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65   a multi-threade
40e90 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a  d application,.*
40ea0 2a 20 74 68 65 72 65 20 69 73 20 61 20 63 68 61  * there is a cha
40eb0 6e 63 65 20 74 68 61 74 20 74 68 65 20 62 6c 6f  nce that the blo
40ec0 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  cking connection
40ed0 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61   will have alrea
40ee0 64 79 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65 64 20  dy.** concluded 
40ef0 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  its transaction 
40f00 62 79 20 74 68 65 20 74 69 6d 65 20 73 71 6c 69  by the time sqli
40f10 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
40f20 79 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a  y() is invoked..
40f30 2a 2a 20 49 66 20 74 68 69 73 20 68 61 70 70 65  ** If this happe
40f40 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 70 65  ns, then the spe
40f50 63 69 66 69 65 64 20 63 61 6c 6c 62 61 63 6b 20  cified callback 
40f60 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64  is invoked immed
40f70 69 61 74 65 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20  iately,.** from 
40f80 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20  within the call 
40f90 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  to sqlite3_unloc
40fa0 6b 5f 6e 6f 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a  k_notify()..**.*
40fb0 2a 20 49 66 20 74 68 65 20 62 6c 6f 63 6b 65 64  * If the blocked
40fc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61   connection is a
40fd0 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74  ttempting to obt
40fe0 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ain a write-lock
40ff0 20 6f 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d   on a.** shared-
41000 63 61 63 68 65 20 74 61 62 6c 65 2c 20 61 6e 64  cache table, and
41010 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f   more than one o
41020 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ther connection 
41030 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 73 0a  currently holds.
41040 2a 2a 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  ** a read-lock o
41050 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  n the same table
41060 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 72  , then SQLite ar
41070 62 69 74 72 61 72 69 6c 79 20 73 65 6c 65 63 74  bitrarily select
41080 73 20 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65  s one of .** the
41090 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
410a0 6e 73 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  ns to use as the
410b0 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63   blocking connec
410c0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  tion..**.** Ther
410d0 65 20 6d 61 79 20 62 65 20 61 74 20 6d 6f 73 74  e may be at most
410e0 20 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69   one unlock-noti
410f0 66 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 67 69  fy callback regi
41100 73 74 65 72 65 64 20 62 79 20 61 20 0a 2a 2a 20  stered by a .** 
41110 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
41120 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75  on. If sqlite3_u
41130 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69  nlock_notify() i
41140 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  s called when th
41150 65 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e  e.** blocked con
41160 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 20  nection already 
41170 68 61 73 20 61 20 72 65 67 69 73 74 65 72 65 64  has a registered
41180 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
41190 61 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e  allback,.** then
411a0 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62 61 63   the new callbac
411b0 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f  k replaces the o
411c0 6c 64 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75  ld. If sqlite3_u
411d0 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69  nlock_notify() i
411e0 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69 74 68  s.** called with
411f0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
41200 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72  as its second ar
41210 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 61 6e 79  gument, then any
41220 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c   existing.** unl
41230 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
41240 61 63 6b 20 69 73 20 63 61 6e 63 65 6c 6c 65 64  ack is cancelled
41250 2e 20 54 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f  . The blocked co
41260 6e 6e 65 63 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e  nnections .** un
41270 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
41280 62 61 63 6b 20 6d 61 79 20 61 6c 73 6f 20 62 65  back may also be
41290 20 63 61 6e 63 65 6c 65 64 20 62 79 20 63 6c 6f   canceled by clo
412a0 73 69 6e 67 20 74 68 65 20 62 6c 6f 63 6b 65 64  sing the blocked
412b0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75  .** connection u
412c0 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c  sing [sqlite3_cl
412d0 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  ose()]..**.** Th
412e0 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  e unlock-notify 
412f0 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20  callback is not 
41300 72 65 65 6e 74 72 61 6e 74 2e 20 49 66 20 61 6e  reentrant. If an
41310 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76   application inv
41320 6f 6b 65 73 0a 2a 2a 20 61 6e 79 20 73 71 6c 69  okes.** any sqli
41330 74 65 33 5f 78 78 78 20 41 50 49 20 66 75 6e 63  te3_xxx API func
41340 74 69 6f 6e 73 20 66 72 6f 6d 20 77 69 74 68 69  tions from withi
41350 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  n an unlock-noti
41360 66 79 20 63 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a  fy callback, a.*
41370 2a 20 63 72 61 73 68 20 6f 72 20 64 65 61 64 6c  * crash or deadl
41380 6f 63 6b 20 6d 61 79 20 62 65 20 74 68 65 20 72  ock may be the r
41390 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c  esult..**.** Unl
413a0 65 73 73 20 64 65 61 64 6c 6f 63 6b 20 69 73 20  ess deadlock is 
413b0 64 65 74 65 63 74 65 64 20 28 73 65 65 20 62 65  detected (see be
413c0 6c 6f 77 29 2c 20 73 71 6c 69 74 65 33 5f 75 6e  low), sqlite3_un
413d0 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 61 6c  lock_notify() al
413e0 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 73 20  ways.** returns 
413f0 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a  SQLITE_OK..**.**
41400 20 3c 62 3e 43 61 6c 6c 62 61 63 6b 20 49 6e 76   <b>Callback Inv
41410 6f 63 61 74 69 6f 6e 20 44 65 74 61 69 6c 73 3c  ocation Details<
41420 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  /b>.**.** When a
41430 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  n unlock-notify 
41440 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69  callback is regi
41450 73 74 65 72 65 64 2c 20 74 68 65 20 61 70 70 6c  stered, the appl
41460 69 63 61 74 69 6f 6e 20 70 72 6f 76 69 64 65 73  ication provides
41470 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 76 6f   a .** single vo
41480 69 64 2a 20 70 6f 69 6e 74 65 72 20 74 68 61 74  id* pointer that
41490 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68   is passed to th
414a0 65 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20  e callback when 
414b0 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a  it is invoked..*
414c0 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 73  * However, the s
414d0 69 67 6e 61 74 75 72 65 20 6f 66 20 74 68 65 20  ignature of the 
414e0 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
414f0 6e 20 61 6c 6c 6f 77 73 20 53 51 4c 69 74 65 20  n allows SQLite 
41500 74 6f 20 70 61 73 73 0a 2a 2a 20 69 74 20 61 6e  to pass.** it an
41510 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20   array of void* 
41520 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73  context pointers
41530 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  . The first argu
41540 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 0a 2a  ment passed to.*
41550 2a 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  * an unlock-noti
41560 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61  fy callback is a
41570 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61   pointer to an a
41580 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 70 6f  rray of void* po
41590 69 6e 74 65 72 73 2c 0a 2a 2a 20 61 6e 64 20 74  inters,.** and t
415a0 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65  he second is the
415b0 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
415c0 65 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e  es in the array.
415d0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 62 6c  .**.** When a bl
415e0 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
415f0 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ns transaction i
41600 73 20 63 6f 6e 63 6c 75 64 65 64 2c 20 74 68 65  s concluded, the
41610 72 65 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72  re may be.** mor
41620 65 20 74 68 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b  e than one block
41630 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  ed connection th
41640 61 74 20 68 61 73 20 72 65 67 69 73 74 65 72 65  at has registere
41650 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d  d for an unlock-
41660 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61  notify.** callba
41670 63 6b 2e 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f  ck. If two or mo
41680 72 65 20 73 75 63 68 20 62 6c 6f 63 6b 65 64 20  re such blocked 
41690 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 61 76 65  connections have
416a0 20 73 70 65 63 69 66 69 65 64 20 74 68 65 0a 2a   specified the.*
416b0 2a 20 73 61 6d 65 20 63 61 6c 6c 62 61 63 6b 20  * same callback 
416c0 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 69  function, then i
416d0 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69  nstead of invoki
416e0 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ng the callback 
416f0 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74  function.** mult
41700 69 70 6c 65 20 74 69 6d 65 73 2c 20 69 74 20 69  iple times, it i
41710 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 77  s invoked once w
41720 69 74 68 20 74 68 65 20 73 65 74 20 6f 66 20 76  ith the set of v
41730 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69  oid* context poi
41740 6e 74 65 72 73 0a 2a 2a 20 73 70 65 63 69 66 69  nters.** specifi
41750 65 64 20 62 79 20 74 68 65 20 62 6c 6f 63 6b 65  ed by the blocke
41760 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 62 75  d connections bu
41770 6e 64 6c 65 64 20 74 6f 67 65 74 68 65 72 20 69  ndled together i
41780 6e 74 6f 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2a  nto an array..**
41790 20 54 68 69 73 20 67 69 76 65 73 20 74 68 65 20   This gives the 
417a0 61 70 70 6c 69 63 61 74 69 6f 6e 20 61 6e 20 6f  application an o
417b0 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 70 72  pportunity to pr
417c0 69 6f 72 69 74 69 7a 65 20 61 6e 79 20 61 63 74  ioritize any act
417d0 69 6f 6e 73 20 0a 2a 2a 20 72 65 6c 61 74 65 64  ions .** related
417e0 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66 20 75   to the set of u
417f0 6e 62 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73  nblocked databas
41800 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a  e connections..*
41810 2a 0a 2a 2a 20 3c 62 3e 44 65 61 64 6c 6f 63 6b  *.** <b>Deadlock
41820 20 44 65 74 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a   Detection</b>.*
41830 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68  *.** Assuming th
41840 61 74 20 61 66 74 65 72 20 72 65 67 69 73 74 65  at after registe
41850 72 69 6e 67 20 66 6f 72 20 61 6e 20 75 6e 6c 6f  ring for an unlo
41860 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
41870 63 6b 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73  ck a .** databas
41880 65 20 77 61 69 74 73 20 66 6f 72 20 74 68 65 20  e waits for the 
41890 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69  callback to be i
418a0 73 73 75 65 64 20 62 65 66 6f 72 65 20 74 61 6b  ssued before tak
418b0 69 6e 67 20 61 6e 79 20 66 75 72 74 68 65 72 0a  ing any further.
418c0 2a 2a 20 61 63 74 69 6f 6e 20 28 61 20 72 65 61  ** action (a rea
418d0 73 6f 6e 61 62 6c 65 20 61 73 73 75 6d 70 74 69  sonable assumpti
418e0 6f 6e 29 2c 20 74 68 65 6e 20 75 73 69 6e 67 20  on), then using 
418f0 74 68 69 73 20 41 50 49 20 6d 61 79 20 63 61 75  this API may cau
41900 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63  se the.** applic
41910 61 74 69 6f 6e 20 74 6f 20 64 65 61 64 6c 6f 63  ation to deadloc
41920 6b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  k. For example, 
41930 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 20  if connection X 
41940 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 0a 2a  is waiting for.*
41950 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 59 27 73  * connection Y's
41960 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20   transaction to 
41970 62 65 20 63 6f 6e 63 6c 75 64 65 64 2c 20 61 6e  be concluded, an
41980 64 20 73 69 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e  d similarly conn
41990 65 63 74 69 6f 6e 0a 2a 2a 20 59 20 69 73 20 77  ection.** Y is w
419a0 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63  aiting on connec
419b0 74 69 6f 6e 20 58 27 73 20 74 72 61 6e 73 61 63  tion X's transac
419c0 74 69 6f 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68  tion, then neith
419d0 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  er connection.**
419e0 20 77 69 6c 6c 20 70 72 6f 63 65 65 64 20 61 6e   will proceed an
419f0 64 20 74 68 65 20 73 79 73 74 65 6d 20 6d 61 79  d the system may
41a00 20 72 65 6d 61 69 6e 20 64 65 61 64 6c 6f 63 6b   remain deadlock
41a10 65 64 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2e  ed indefinitely.
41a20 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20  .**.** To avoid 
41a30 74 68 69 73 20 73 63 65 6e 61 72 69 6f 2c 20 74  this scenario, t
41a40 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  he sqlite3_unloc
41a50 6b 5f 6e 6f 74 69 66 79 28 29 20 70 65 72 66 6f  k_notify() perfo
41a60 72 6d 73 20 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20  rms deadlock.** 
41a70 64 65 74 65 63 74 69 6f 6e 2e 20 49 66 20 61 20  detection. If a 
41a80 67 69 76 65 6e 20 63 61 6c 6c 20 74 6f 20 73 71  given call to sq
41a90 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
41aa0 69 66 79 28 29 20 77 6f 75 6c 64 20 70 75 74 20  ify() would put 
41ab0 74 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20 69 6e  the.** system in
41ac0 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74   a deadlocked st
41ad0 61 74 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45  ate, then SQLITE
41ae0 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72  _LOCKED is retur
41af0 6e 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e  ned and no.** un
41b00 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
41b10 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 72  back is register
41b20 65 64 2e 20 54 68 65 20 73 79 73 74 65 6d 20 69  ed. The system i
41b30 73 20 73 61 69 64 20 74 6f 20 62 65 20 69 6e 0a  s said to be in.
41b40 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20  ** a deadlocked 
41b50 73 74 61 74 65 20 69 66 20 63 6f 6e 6e 65 63 74  state if connect
41b60 69 6f 6e 20 41 20 68 61 73 20 72 65 67 69 73 74  ion A has regist
41b70 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f  ered for an unlo
41b80 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c  ck-notify.** cal
41b90 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e  lback on the con
41ba0 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65  clusion of conne
41bb0 63 74 69 6f 6e 20 42 27 73 20 74 72 61 6e 73 61  ction B's transa
41bc0 63 74 69 6f 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65  ction, and conne
41bd0 63 74 69 6f 6e 0a 2a 2a 20 42 20 68 61 73 20 69  ction.** B has i
41be0 74 73 65 6c 66 20 72 65 67 69 73 74 65 72 65 64  tself registered
41bf0 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e   for an unlock-n
41c00 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 77  otify callback w
41c10 68 65 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  hen connection.*
41c20 2a 20 41 27 73 20 74 72 61 6e 73 61 63 74 69 6f  * A's transactio
41c30 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20  n is concluded. 
41c40 49 6e 64 69 72 65 63 74 20 64 65 61 64 6c 6f 63  Indirect deadloc
41c50 6b 20 69 73 20 61 6c 73 6f 20 64 65 74 65 63 74  k is also detect
41c60 65 64 2c 20 73 6f 0a 2a 2a 20 74 68 65 20 73 79  ed, so.** the sy
41c70 73 74 65 6d 20 69 73 20 61 6c 73 6f 20 63 6f 6e  stem is also con
41c80 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 64 65  sidered to be de
41c90 61 64 6c 6f 63 6b 65 64 20 69 66 20 63 6f 6e 6e  adlocked if conn
41ca0 65 63 74 69 6f 6e 20 42 20 68 61 73 0a 2a 2a 20  ection B has.** 
41cb0 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 61  registered for a
41cc0 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  n unlock-notify 
41cd0 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20  callback on the 
41ce0 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f  conclusion of co
41cf0 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 43 27 73 20  nnection.** C's 
41d00 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 77 68 65  transaction, whe
41d10 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 43 20  re connection C 
41d20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f  is waiting on co
41d30 6e 6e 65 63 74 69 6f 6e 20 41 2e 20 41 6e 79 0a  nnection A. Any.
41d40 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76  ** number of lev
41d50 65 6c 73 20 6f 66 20 69 6e 64 69 72 65 63 74 69  els of indirecti
41d60 6f 6e 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a  on are allowed..
41d70 2a 2a 0a 2a 2a 20 3c 62 3e 54 68 65 20 22 44 52  **.** <b>The "DR
41d80 4f 50 20 54 41 42 4c 45 22 20 45 78 63 65 70 74  OP TABLE" Except
41d90 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68  ion</b>.**.** Wh
41da0 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71  en a call to [sq
41db0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65  lite3_step()] re
41dc0 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4c 4f 43  turns SQLITE_LOC
41dd0 4b 45 44 2c 20 69 74 20 69 73 20 61 6c 6d 6f 73  KED, it is almos
41de0 74 20 0a 2a 2a 20 61 6c 77 61 79 73 20 61 70 70  t .** always app
41df0 72 6f 70 72 69 61 74 65 20 74 6f 20 63 61 6c 6c  ropriate to call
41e00 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f   sqlite3_unlock_
41e10 6e 6f 74 69 66 79 28 29 2e 20 54 68 65 72 65 20  notify(). There 
41e20 69 73 20 68 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f  is however,.** o
41e30 6e 65 20 65 78 63 65 70 74 69 6f 6e 2e 20 57 68  ne exception. Wh
41e40 65 6e 20 65 78 65 63 75 74 69 6e 67 20 61 20 22  en executing a "
41e50 44 52 4f 50 20 54 41 42 4c 45 22 20 6f 72 20 22  DROP TABLE" or "
41e60 44 52 4f 50 20 49 4e 44 45 58 22 20 73 74 61 74  DROP INDEX" stat
41e70 65 6d 65 6e 74 2c 0a 2a 2a 20 53 51 4c 69 74 65  ement,.** SQLite
41e80 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
41e90 20 61 72 65 20 61 6e 79 20 63 75 72 72 65 6e 74   are any current
41ea0 6c 79 20 65 78 65 63 75 74 69 6e 67 20 53 45 4c  ly executing SEL
41eb0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a  ECT statements.*
41ec0 2a 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74 6f  * that belong to
41ed0 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63   the same connec
41ee0 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 61  tion. If there a
41ef0 72 65 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  re, SQLITE_LOCKE
41f00 44 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  D is.** returned
41f10 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
41f20 68 65 72 65 20 69 73 20 6e 6f 20 22 62 6c 6f 63  here is no "bloc
41f30 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 22  king connection"
41f40 2c 20 73 6f 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a  , so invoking.**
41f50 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f   sqlite3_unlock_
41f60 6e 6f 74 69 66 79 28 29 20 72 65 73 75 6c 74 73  notify() results
41f70 20 69 6e 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e   in the unlock-n
41f80 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 62  otify callback b
41f90 65 69 6e 67 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20  eing.** invoked 
41fa0 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 66 20  immediately. If 
41fb0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
41fc0 74 68 65 6e 20 72 65 2d 61 74 74 65 6d 70 74 73  then re-attempts
41fd0 20 74 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45   the "DROP TABLE
41fe0 22 0a 2a 2a 20 6f 72 20 22 44 52 4f 50 20 49 4e  ".** or "DROP IN
41ff0 44 45 58 22 20 71 75 65 72 79 2c 20 61 6e 20 69  DEX" query, an i
42000 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 20 6d 69 67  nfinite loop mig
42010 68 74 20 62 65 20 74 68 65 20 72 65 73 75 6c 74  ht be the result
42020 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 77 61 79 20  ..**.** One way 
42030 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62  around this prob
42040 6c 65 6d 20 69 73 20 74 6f 20 63 68 65 63 6b 20  lem is to check 
42050 74 68 65 20 65 78 74 65 6e 64 65 64 20 65 72 72  the extended err
42060 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64  or code returned
42070 0a 2a 2a 20 62 79 20 61 6e 20 73 71 6c 69 74 65  .** by an sqlite
42080 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 2e 20 49  3_step() call. I
42090 66 20 74 68 65 72 65 20 69 73 20 61 20 62 6c 6f  f there is a blo
420a0 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  cking connection
420b0 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 78  , then the.** ex
420c0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
420d0 65 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  e is set to SQLI
420e0 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
420f0 43 41 43 48 45 2e 20 4f 74 68 65 72 77 69 73 65  CACHE. Otherwise
42100 2c 20 69 6e 0a 2a 2a 20 74 68 65 20 73 70 65 63  , in.** the spec
42110 69 61 6c 20 22 44 52 4f 50 20 54 41 42 4c 45 2f  ial "DROP TABLE/
42120 49 4e 44 45 58 22 20 63 61 73 65 2c 20 74 68 65  INDEX" case, the
42130 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20   extended error 
42140 63 6f 64 65 20 69 73 20 6a 75 73 74 20 0a 2a 2a  code is just .**
42150 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a   SQLITE_LOCKED..
42160 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
42170 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  t sqlite3_unlock
42180 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c 69 74  _notify(.  sqlit
42190 65 33 20 2a 70 42 6c 6f 63 6b 65 64 2c 20 20 20  e3 *pBlocked,   
421a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
421b0 20 20 20 20 20 20 20 2f 2a 20 57 61 69 74 69 6e         /* Waitin
421c0 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  g connection */.
421d0 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79    void (*xNotify
421e0 29 28 76 6f 69 64 20 2a 2a 61 70 41 72 67 2c 20  )(void **apArg, 
421f0 69 6e 74 20 6e 41 72 67 29 2c 20 20 20 20 2f 2a  int nArg),    /*
42200 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   Callback functi
42210 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a  on to invoke */.
42220 20 20 76 6f 69 64 20 2a 70 4e 6f 74 69 66 79 41    void *pNotifyA
42230 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
42240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42250 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73   Argument to pas
42260 73 20 74 6f 20 78 4e 6f 74 69 66 79 20 2a 2f 0a  s to xNotify */.
42270 29 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 74  );../*.** Undo t
42280 68 65 20 68 61 63 6b 20 74 68 61 74 20 63 6f 6e  he hack that con
42290 76 65 72 74 73 20 66 6c 6f 61 74 69 6e 67 20 70  verts floating p
422a0 6f 69 6e 74 20 74 79 70 65 73 20 74 6f 20 69 6e  oint types to in
422b0 74 65 67 65 72 20 66 6f 72 0a 2a 2a 20 62 75 69  teger for.** bui
422c0 6c 64 73 20 6f 6e 20 70 72 6f 63 65 73 73 6f 72  lds on processor
422d0 73 20 77 69 74 68 6f 75 74 20 66 6c 6f 61 74 69  s without floati
422e0 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74  ng point support
422f0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
42300 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
42310 5f 50 4f 49 4e 54 0a 23 20 75 6e 64 65 66 20 64  _POINT.# undef d
42320 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 0a 23 69  ouble.#endif..#i
42330 66 20 30 0a 7d 20 20 2f 2a 20 45 6e 64 20 6f 66  f 0.}  /* End of
42340 20 74 68 65 20 27 65 78 74 65 72 6e 20 22 43 22   the 'extern "C"
42350 27 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65 6e 64 69  ' block */.#endi
42360 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  f.#endif../*****
42370 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
42380 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a   sqlite3.h *****
42390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
423a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
423b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
423c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
423d0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
423e0 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
423f0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
42400 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
42410 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
42420 65 20 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20  e hash.h in the 
42430 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
42440 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
42450 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
42460 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
42470 66 69 6c 65 20 68 61 73 68 2e 68 20 2a 2a 2a 2a  file hash.h ****
42480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
424a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
424b0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32  2001 September 2
424c0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
424d0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
424e0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
424f0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
42500 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
42510 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
42520 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
42530 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
42540 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
42550 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
42560 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
42570 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
42580 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
42590 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
425a0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
425b0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
425c0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
425d0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
425e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
425f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42620 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
42630 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20  header file for 
42640 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 73 68  the generic hash
42650 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 61  -table implemena
42660 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e 20  tion.** used in 
42670 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  SQLite..**.** $I
42680 64 3a 20 68 61 73 68 2e 68 2c 76 20 31 2e 31 32  d: hash.h,v 1.12
42690 20 32 30 30 38 2f 31 30 2f 31 30 20 31 37 3a 34   2008/10/10 17:4
426a0 31 3a 32 39 20 64 72 68 20 45 78 70 20 24 0a 2a  1:29 drh Exp $.*
426b0 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  /.#ifndef _SQLIT
426c0 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e  E_HASH_H_.#defin
426d0 65 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48  e _SQLITE_HASH_H
426e0 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  _../* Forward de
426f0 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74  clarations of st
42700 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70  ructures. */.typ
42710 65 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68  edef struct Hash
42720 20 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73   Hash;.typedef s
42730 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 48  truct HashElem H
42740 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63  ashElem;../* A c
42750 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62  omplete hash tab
42760 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  le is an instanc
42770 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
42780 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
42790 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f   The internals o
427a0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
427b0 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f   are intended to
427c0 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c   be opaque -- cl
427d0 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f  ient.** code sho
427e0 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20  uld not attempt 
427f0 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64  to access or mod
42800 69 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f  ify the fields o
42810 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
42820 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43  .** directly.  C
42830 68 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63  hange this struc
42840 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69  ture only by usi
42850 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  ng the routines 
42860 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65  below..** Howeve
42870 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68 65 20 22  r, many of the "
42880 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20  procedures" and 
42890 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20  "functions" for 
428a0 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a  modifying and.**
428b0 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20   accessing this 
428c0 73 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65  structure are re
428d0 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20  ally macros, so 
428e0 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20  we can't really 
428f0 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72  make.** this str
42900 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a  ucture opaque..*
42910 2f 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0a  /.struct Hash {.
42920 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63    unsigned int c
42930 6f 70 79 4b 65 79 3a 20 31 3b 20 20 2f 2a 20 54  opyKey: 1;  /* T
42940 72 75 65 20 69 66 20 63 6f 70 79 20 6f 66 20 6b  rue if copy of k
42950 65 79 20 6d 61 64 65 20 6f 6e 20 69 6e 73 65 72  ey made on inser
42960 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  t */.  unsigned 
42970 69 6e 74 20 68 74 73 69 7a 65 20 3a 20 33 31 3b  int htsize : 31;
42980 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75   /* Number of bu
42990 63 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 73  ckets in the has
429a0 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73  h table */.  uns
429b0 69 67 6e 65 64 20 69 6e 74 20 63 6f 75 6e 74 3b  igned int count;
429c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
429d0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
429e0 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48  his table */.  H
429f0 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20  ashElem *first; 
42a00 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
42a10 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
42a20 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20   the array */.  
42a30 73 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20 20  struct _ht {    
42a40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65            /* the
42a50 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
42a60 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20     int count;   
42a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42a80 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
42a90 65 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73  es with this has
42aa0 68 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c 65  h */.    HashEle
42ab0 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 20  m *chain;       
42ac0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
42ad0 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 77 69  o first entry wi
42ae0 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a  th this hash */.
42af0 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20    } *ht;.};../* 
42b00 45 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20  Each element in 
42b10 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69  the hash table i
42b20 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
42b30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a   the following .
42b40 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41  ** structure.  A
42b50 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20  ll elements are 
42b60 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67  stored on a sing
42b70 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64  le doubly-linked
42b80 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61   list..**.** Aga
42b90 69 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74 75  in, this structu
42ba0 72 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  re is intended t
42bb0 6f 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75 74  o be opaque, but
42bc0 20 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c 79   it can't really
42bd0 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62 65  .** be opaque be
42be0 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64  cause it is used
42bf0 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73   by macros..*/.s
42c00 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 7b  truct HashElem {
42c10 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78  .  HashElem *nex
42c20 74 2c 20 2a 70 72 65 76 3b 20 20 20 2f 2a 20 4e  t, *prev;   /* N
42c30 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73  ext and previous
42c40 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65   elements in the
42c50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64   table */.  void
42c60 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20   *data;         
42c70 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73 73       /* Data ass
42c80 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69  ociated with thi
42c90 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 76  s element */.  v
42ca0 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e  oid *pKey; int n
42cb0 4b 65 79 3b 20 20 20 20 2f 2a 20 4b 65 79 20 61  Key;    /* Key a
42cc0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
42cd0 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d  his element */.}
42ce0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20  ;../*.** Access 
42cf0 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64 65  routines.  To de
42d00 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20 4e  lete, insert a N
42d10 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  ULL pointer..*/.
42d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
42d30 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49  oid sqlite3HashI
42d40 6e 69 74 28 48 61 73 68 2a 2c 20 69 6e 74 20 63  nit(Hash*, int c
42d50 6f 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f  opyKey);.SQLITE_
42d60 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
42d70 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
42d80 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  Hash*, const voi
42d90 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
42da0 79 2c 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b  y, void *pData);
42db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
42dc0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73  void *sqlite3Has
42dd0 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68  hFind(const Hash
42de0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  *, const void *p
42df0 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a  Key, int nKey);.
42e00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 48  SQLITE_PRIVATE H
42e10 61 73 68 45 6c 65 6d 20 2a 73 71 6c 69 74 65 33  ashElem *sqlite3
42e20 48 61 73 68 46 69 6e 64 45 6c 65 6d 28 63 6f 6e  HashFindElem(con
42e30 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20  st Hash*, const 
42e40 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
42e50 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nKey);.SQLITE_PR
42e60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
42e70 65 33 48 61 73 68 43 6c 65 61 72 28 48 61 73 68  e3HashClear(Hash
42e80 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  *);../*.** Macro
42e90 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  s for looping ov
42ea0 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20  er all elements 
42eb0 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e  of a hash table.
42ec0 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a    The idiom is.*
42ed0 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  * like this:.**.
42ee0 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a 2a 2a 20  **   Hash h;.** 
42ef0 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a    HashElem *p;.*
42f00 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72  *   ....**   for
42f10 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (p=sqliteHashFir
42f20 73 74 28 26 68 29 3b 20 70 3b 20 70 3d 73 71 6c  st(&h); p; p=sql
42f30 69 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b  iteHashNext(p)){
42f40 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72 75  .**     SomeStru
42f50 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20 73  cture *pData = s
42f60 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29  qliteHashData(p)
42f70 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73  ;.**     // do s
42f80 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 44  omething with pD
42f90 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64  ata.**   }.*/.#d
42fa0 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
42fb0 46 69 72 73 74 28 48 29 20 20 28 28 48 29 2d 3e  First(H)  ((H)->
42fc0 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65 20 73  first).#define s
42fd0 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 45 29  qliteHashNext(E)
42fe0 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23     ((E)->next).#
42ff0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
43000 68 44 61 74 61 28 45 29 20 20 20 28 28 45 29 2d  hData(E)   ((E)-
43010 3e 64 61 74 61 29 0a 23 64 65 66 69 6e 65 20 73  >data).#define s
43020 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45 29 20  qliteHashKey(E) 
43030 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 0a 23     ((E)->pKey).#
43040 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
43050 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28 45 29  hKeysize(E) ((E)
43060 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a 2a 20 4e  ->nKey)../*.** N
43070 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
43080 20 69 6e 20 61 20 68 61 73 68 20 74 61 62 6c 65   in a hash table
43090 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  .*/.#define sqli
430a0 74 65 48 61 73 68 43 6f 75 6e 74 28 48 29 20 20  teHashCount(H)  
430b0 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a 0a 23 65  ((H)->count)..#e
430c0 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f  ndif /* _SQLITE_
430d0 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  HASH_H_ */../***
430e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
430f0 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  of hash.h ******
43100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
43130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
43140 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
43150 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
43160 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
43170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
43180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
43190 75 64 65 20 70 61 72 73 65 2e 68 20 69 6e 20 74  ude parse.h in t
431a0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
431b0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
431c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
431d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
431e0 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 68 20 2a  n file parse.h *
431f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66  **********/.#def
43220 69 6e 65 20 54 4b 5f 53 45 4d 49 20 20 20 20 20  ine TK_SEMI     
43230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43240 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
43250 20 54 4b 5f 45 58 50 4c 41 49 4e 20 20 20 20 20   TK_EXPLAIN     
43260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43270 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 54 4b      2.#define TK
43280 5f 51 55 45 52 59 20 20 20 20 20 20 20 20 20 20  _QUERY          
43290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
432a0 20 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c   3.#define TK_PL
432b0 41 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AN              
432c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a                4.
432d0 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 47 49 4e  #define TK_BEGIN
432e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
432f0 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65             5.#de
43300 66 69 6e 65 20 54 4b 5f 54 52 41 4e 53 41 43 54  fine TK_TRANSACT
43310 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  ION             
43320 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e          6.#defin
43330 65 20 54 4b 5f 44 45 46 45 52 52 45 44 20 20 20  e TK_DEFERRED   
43340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43350 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 54       7.#define T
43360 4b 5f 49 4d 4d 45 44 49 41 54 45 20 20 20 20 20  K_IMMEDIATE     
43370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43380 20 20 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45    8.#define TK_E
43390 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20  XCLUSIVE        
433a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
433b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d  .#define TK_COMM
433c0 49 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IT              
433d0 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64             10.#d
433e0 65 66 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20  efine TK_END    
433f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43400 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69          11.#defi
43410 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20  ne TK_ROLLBACK  
43420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43430 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20       12.#define 
43440 54 4b 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20  TK_SAVEPOINT    
43450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43460 20 20 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    13.#define TK_
43470 52 45 4c 45 41 53 45 20 20 20 20 20 20 20 20 20  RELEASE         
43480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
43490 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20  4.#define TK_TO 
434a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
434b0 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23              15.#
434c0 64 65 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20  define TK_TABLE 
434d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
434e0 20 20 20 20 20 20 20 20 20 31 36 0a 23 64 65 66           16.#def
434f0 69 6e 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20  ine TK_CREATE   
43500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43510 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65        17.#define
43520 20 54 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20   TK_IF          
43530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43540 20 20 20 31 38 0a 23 64 65 66 69 6e 65 20 54 4b     18.#define TK
43550 5f 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20  _NOT            
43560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43570 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58  19.#define TK_EX
43580 49 53 54 53 20 20 20 20 20 20 20 20 20 20 20 20  ISTS            
43590 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a               20.
435a0 23 64 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20  #define TK_TEMP 
435b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
435c0 20 20 20 20 20 20 20 20 20 20 32 31 0a 23 64 65            21.#de
435d0 66 69 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20  fine TK_LP      
435e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
435f0 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e         22.#defin
43600 65 20 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20  e TK_RP         
43610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43620 20 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 54      23.#define T
43630 4b 5f 41 53 20 20 20 20 20 20 20 20 20 20 20 20  K_AS            
43640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43650 20 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   24.#define TK_C
43660 4f 4d 4d 41 20 20 20 20 20 20 20 20 20 20 20 20  OMMA            
43670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35                25
43680 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20  .#define TK_ID  
43690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
436a0 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23 64             26.#d
436b0 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44  efine TK_INDEXED
436c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
436d0 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 66 69          27.#defi
436e0 6e 65 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20  ne TK_ABORT     
436f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43700 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20       28.#define 
43710 54 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 20  TK_AFTER        
43720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43730 20 20 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    29.#define TK_
43740 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20  ANALYZE         
43750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
43760 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43  0.#define TK_ASC
43770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43780 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23              31.#
43790 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 48  define TK_ATTACH
437a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
437b0 20 20 20 20 20 20 20 20 20 33 32 0a 23 64 65 66           32.#def
437c0 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 20  ine TK_BEFORE   
437d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
437e0 20 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65        33.#define
437f0 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20 20   TK_BY          
43800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43810 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 54 4b     34.#define TK
43820 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 20 20  _CASCADE        
43830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43840 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41  35.#define TK_CA
43850 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ST              
43860 20 20 20 20 20 20 20 20 20 20 20 20 20 33 36 0a               36.
43870 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d  #define TK_COLUM
43880 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20  NKW             
43890 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65            37.#de
438a0 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43 54  fine TK_CONFLICT
438b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
438c0 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e         38.#defin
438d0 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20 20  e TK_DATABASE   
438e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
438f0 20 20 20 20 33 39 0a 23 64 65 66 69 6e 65 20 54      39.#define T
43900 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20 20  K_DESC          
43910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43920 20 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44   40.#define TK_D
43930 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  ETACH           
43940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31                41
43950 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43 48  .#define TK_EACH
43960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43970 20 20 20 20 20 20 20 20 20 20 20 34 32 0a 23 64             42.#d
43980 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20 20  efine TK_FAIL   
43990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
439a0 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69          43.#defi
439b0 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20 20  ne TK_FOR       
439c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
439d0 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20       44.#define 
439e0 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20 20  TK_IGNORE       
439f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43a00 20 20 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    45.#define TK_
43a10 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20  INITIALLY       
43a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
43a30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53  6.#define TK_INS
43a40 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20  TEAD            
43a50 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a 23              47.#
43a60 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b  define TK_LIKE_K
43a70 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  W               
43a80 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66           48.#def
43a90 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20 20  ine TK_MATCH    
43aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ab0 20 20 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65        49.#define
43ac0 20 54 4b 5f 4b 45 59 20 20 20 20 20 20 20 20 20   TK_KEY         
43ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ae0 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 54 4b     50.#define TK
43af0 5f 4f 46 20 20 20 20 20 20 20 20 20 20 20 20 20  _OF             
43b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43b10 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46  51.#define TK_OF
43b20 46 53 45 54 20 20 20 20 20 20 20 20 20 20 20 20  FSET            
43b30 20 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a               52.
43b40 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d  #define TK_PRAGM
43b50 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  A               
43b60 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65            53.#de
43b70 66 69 6e 65 20 54 4b 5f 52 41 49 53 45 20 20 20  fine TK_RAISE   
43b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43b90 20 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e         54.#defin
43ba0 65 20 54 4b 5f 52 45 50 4c 41 43 45 20 20 20 20  e TK_REPLACE    
43bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43bc0 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 20 54      55.#define T
43bd0 4b 5f 52 45 53 54 52 49 43 54 20 20 20 20 20 20  K_RESTRICT      
43be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43bf0 20 35 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   56.#define TK_R
43c00 4f 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OW              
43c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37                57
43c20 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 49 47  .#define TK_TRIG
43c30 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  GER             
43c40 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64             58.#d
43c50 65 66 69 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20  efine TK_VACUUM 
43c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43c70 20 20 20 20 20 20 20 20 35 39 0a 23 64 65 66 69          59.#defi
43c80 6e 65 20 54 4b 5f 56 49 45 57 20 20 20 20 20 20  ne TK_VIEW      
43c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ca0 20 20 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20       60.#define 
43cb0 54 4b 5f 56 49 52 54 55 41 4c 20 20 20 20 20 20  TK_VIRTUAL      
43cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43cd0 20 20 36 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    61.#define TK_
43ce0 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  REINDEX         
43cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
43d00 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e  2.#define TK_REN
43d10 41 4d 45 20 20 20 20 20 20 20 20 20 20 20 20 20  AME             
43d20 20 20 20 20 20 20 20 20 20 20 20 20 36 33 0a 23              63.#
43d30 64 65 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f  define TK_CTIME_
43d40 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  KW              
43d50 20 20 20 20 20 20 20 20 20 36 34 0a 23 64 65 66           64.#def
43d60 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20 20 20 20  ine TK_ANY      
43d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43d80 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65        65.#define
43d90 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 20 20 20   TK_OR          
43da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43db0 20 20 20 36 36 0a 23 64 65 66 69 6e 65 20 54 4b     66.#define TK
43dc0 5f 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20  _AND            
43dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43de0 36 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53  67.#define TK_IS
43df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e00 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 0a               68.
43e10 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 54 57 45  #define TK_BETWE
43e20 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EN              
43e30 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 64 65            69.#de
43e40 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20 20 20 20  fine TK_IN      
43e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e60 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e         70.#defin
43e70 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20  e TK_ISNULL     
43e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e90 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 20 54      71.#define T
43ea0 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20  K_NOTNULL       
43eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ec0 20 37 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e   72.#define TK_N
43ed0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
43ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 33                73
43ef0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 51 20 20  .#define TK_EQ  
43f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43f10 20 20 20 20 20 20 20 20 20 20 20 37 34 0a 23 64             74.#d
43f20 65 66 69 6e 65 20 54 4b 5f 47 54 20 20 20 20 20  efine TK_GT     
43f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43f40 20 20 20 20 20 20 20 20 37 35 0a 23 64 65 66 69          75.#defi
43f50 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20  ne TK_LE        
43f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43f70 20 20 20 20 20 37 36 0a 23 64 65 66 69 6e 65 20       76.#define 
43f80 54 4b 5f 4c 54 20 20 20 20 20 20 20 20 20 20 20  TK_LT           
43f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43fa0 20 20 37 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    77.#define TK_
43fb0 47 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  GE              
43fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
43fd0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 53 43  8.#define TK_ESC
43fe0 41 50 45 20 20 20 20 20 20 20 20 20 20 20 20 20  APE             
43ff0 20 20 20 20 20 20 20 20 20 20 20 20 37 39 0a 23              79.#
44000 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44  define TK_BITAND
44010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44020 20 20 20 20 20 20 20 20 20 38 30 0a 23 64 65 66           80.#def
44030 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20 20 20 20  ine TK_BITOR    
44040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44050 20 20 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65        81.#define
44060 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 20 20 20   TK_LSHIFT      
44070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44080 20 20 20 38 32 0a 23 64 65 66 69 6e 65 20 54 4b     82.#define TK
44090 5f 52 53 48 49 46 54 20 20 20 20 20 20 20 20 20  _RSHIFT         
440a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440b0 38 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c  83.#define TK_PL
440c0 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  US              
440d0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 34 0a               84.
440e0 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53  #define TK_MINUS
440f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44100 20 20 20 20 20 20 20 20 20 20 38 35 0a 23 64 65            85.#de
44110 66 69 6e 65 20 54 4b 5f 53 54 41 52 20 20 20 20  fine TK_STAR    
44120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44130 20 20 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e         86.#defin
44140 65 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 20 20  e TK_SLASH      
44150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44160 20 20 20 20 38 37 0a 23 64 65 66 69 6e 65 20 54      87.#define T
44170 4b 5f 52 45 4d 20 20 20 20 20 20 20 20 20 20 20  K_REM           
44180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44190 20 38 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   88.#define TK_C
441a0 4f 4e 43 41 54 20 20 20 20 20 20 20 20 20 20 20  ONCAT           
441b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 39                89
441c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c  .#define TK_COLL
441d0 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ATE             
441e0 20 20 20 20 20 20 20 20 20 20 20 39 30 0a 23 64             90.#d
441f0 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20  efine TK_UMINUS 
44200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44210 20 20 20 20 20 20 20 20 39 31 0a 23 64 65 66 69          91.#defi
44220 6e 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20  ne TK_UPLUS     
44230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44240 20 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20       92.#define 
44250 54 4b 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20  TK_BITNOT       
44260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44270 20 20 39 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    93.#define TK_
44280 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 20  STRING          
44290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
442a0 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49  4.#define TK_JOI
442b0 4e 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20 20  N_KW            
442c0 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a 23              95.#
442d0 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52  define TK_CONSTR
442e0 41 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20  AINT            
442f0 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66           96.#def
44300 69 6e 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 20  ine TK_DEFAULT  
44310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44320 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65        97.#define
44330 20 54 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20   TK_NULL        
44340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44350 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 54 4b     98.#define TK
44360 5f 50 52 49 4d 41 52 59 20 20 20 20 20 20 20 20  _PRIMARY        
44370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44380 39 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e  99.#define TK_UN
44390 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20 20  IQUE            
443a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 30               100
443b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43  .#define TK_CHEC
443c0 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K               
443d0 20 20 20 20 20 20 20 20 20 20 20 31 30 31 0a 23             101.#
443e0 64 65 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45  define TK_REFERE
443f0 4e 43 45 53 20 20 20 20 20 20 20 20 20 20 20 20  NCES            
44400 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65           102.#de
44410 66 69 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52  fine TK_AUTOINCR
44420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44430 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 66 69         103.#defi
44440 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20  ne TK_ON        
44450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44460 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65       104.#define
44470 20 54 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20   TK_DELETE      
44480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44490 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65 20 54     105.#define T
444a0 4b 5f 55 50 44 41 54 45 20 20 20 20 20 20 20 20  K_UPDATE        
444b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
444c0 20 31 30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   106.#define TK_
444d0 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20  INSERT          
444e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
444f0 30 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45  07.#define TK_SE
44500 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
44510 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38               108
44520 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45  .#define TK_DEFE
44530 52 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  RRABLE          
44540 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23             109.#
44550 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47  define TK_FOREIG
44560 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
44570 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64 65           110.#de
44580 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20  fine TK_DROP    
44590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445a0 20 20 20 20 20 20 20 31 31 31 0a 23 64 65 66 69         111.#defi
445b0 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20  ne TK_UNION     
445c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445d0 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65       112.#define
445e0 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20   TK_ALL         
445f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44600 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 54     113.#define T
44610 4b 5f 45 58 43 45 50 54 20 20 20 20 20 20 20 20  K_EXCEPT        
44620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44630 20 31 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f   114.#define TK_
44640 49 4e 54 45 52 53 45 43 54 20 20 20 20 20 20 20  INTERSECT       
44650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
44660 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45  15.#define TK_SE
44670 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20  LECT            
44680 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36               116
44690 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54  .#define TK_DIST
446a0 49 4e 43 54 20 20 20 20 20 20 20 20 20 20 20 20  INCT            
446b0 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23             117.#
446c0 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20  define TK_DOT   
446d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
446e0 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64 65           118.#de
446f0 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20  fine TK_FROM    
44700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44710 20 20 20 20 20 20 20 31 31 39 0a 23 64 65 66 69         119.#defi
44720 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20  ne TK_JOIN      
44730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44740 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65       120.#define
44750 20 54 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20   TK_USING       
44760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44770 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 54     121.#define T
44780 4b 5f 4f 52 44 45 52 20 20 20 20 20 20 20 20 20  K_ORDER         
44790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
447a0 20 31 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   122.#define TK_
447b0 47 52 4f 55 50 20 20 20 20 20 20 20 20 20 20 20  GROUP           
447c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
447d0 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41  23.#define TK_HA
447e0 56 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20  VING            
447f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 34               124
44800 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49  .#define TK_LIMI
44810 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
44820 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0a 23             125.#
44830 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20  define TK_WHERE 
44840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44850 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 64 65           126.#de
44860 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20  fine TK_INTO    
44870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44880 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 66 69         127.#defi
44890 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20  ne TK_VALUES    
448a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
448b0 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65       128.#define
448c0 20 54 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20   TK_INTEGER     
448d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
448e0 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 54     129.#define T
448f0 4b 5f 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20  K_FLOAT         
44900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44910 20 31 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   130.#define TK_
44920 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20 20  BLOB            
44930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
44940 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  31.#define TK_RE
44950 47 49 53 54 45 52 20 20 20 20 20 20 20 20 20 20  GISTER          
44960 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 32               132
44970 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49  .#define TK_VARI
44980 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  ABLE            
44990 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23             133.#
449a0 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20  define TK_CASE  
449b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
449c0 20 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65           134.#de
449d0 66 69 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20  fine TK_WHEN    
449e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
449f0 20 20 20 20 20 20 20 31 33 35 0a 23 64 65 66 69         135.#defi
44a00 6e 65 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20  ne TK_THEN      
44a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a20 20 20 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65       136.#define
44a30 20 54 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20   TK_ELSE        
44a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a50 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 54     137.#define T
44a60 4b 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  K_INDEX         
44a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a80 20 31 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   138.#define TK_
44a90 41 4c 54 45 52 20 20 20 20 20 20 20 20 20 20 20  ALTER           
44aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
44ab0 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44  39.#define TK_AD
44ac0 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
44ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 30               140
44ae0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54  .#define TK_TO_T
44af0 45 58 54 20 20 20 20 20 20 20 20 20 20 20 20 20  EXT             
44b00 20 20 20 20 20 20 20 20 20 20 20 31 34 31 0a 23             141.#
44b10 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f  define TK_TO_BLO
44b20 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  B               
44b30 20 20 20 20 20 20 20 20 20 31 34 32 0a 23 64 65           142.#de
44b40 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52  fine TK_TO_NUMER
44b50 49 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IC              
44b60 20 20 20 20 20 20 20 31 34 33 0a 23 64 65 66 69         143.#defi
44b70 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20  ne TK_TO_INT    
44b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b90 20 20 20 20 20 31 34 34 0a 23 64 65 66 69 6e 65       144.#define
44ba0 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20   TK_TO_REAL     
44bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44bc0 20 20 20 31 34 35 0a 23 64 65 66 69 6e 65 20 54     145.#define T
44bd0 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20 20 20  K_END_OF_FILE   
44be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44bf0 20 31 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   146.#define TK_
44c00 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20 20 20  ILLEGAL         
44c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
44c20 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 50  47.#define TK_SP
44c30 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ACE             
44c40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 38               148
44c50 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 43 4c  .#define TK_UNCL
44c60 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20 20 20  OSED_STRING     
44c70 20 20 20 20 20 20 20 20 20 20 20 31 34 39 0a 23             149.#
44c80 64 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43 54 49  define TK_FUNCTI
44c90 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ON              
44ca0 20 20 20 20 20 20 20 20 20 31 35 30 0a 23 64 65           150.#de
44cb0 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20  fine TK_COLUMN  
44cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44cd0 20 20 20 20 20 20 20 31 35 31 0a 23 64 65 66 69         151.#defi
44ce0 6e 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  ne TK_AGG_FUNCTI
44cf0 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ON              
44d00 20 20 20 20 20 31 35 32 0a 23 64 65 66 69 6e 65       152.#define
44d10 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 20   TK_AGG_COLUMN  
44d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44d30 20 20 20 31 35 33 0a 23 64 65 66 69 6e 65 20 54     153.#define T
44d40 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20 20 20  K_CONST_FUNC    
44d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44d60 20 31 35 34 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   154../*********
44d70 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72  ***** End of par
44d80 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  se.h ***********
44d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44db0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
44dc0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
44dd0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
44de0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
44df0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
44e00 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c  ****/.#include <
44e10 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64  stdio.h>.#includ
44e20 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e  e <stdlib.h>.#in
44e30 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e  clude <string.h>
44e40 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72  .#include <asser
44e50 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  t.h>.#include <s
44e60 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20  tddef.h>../*.** 
44e70 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72  If compiling for
44e80 20 61 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61   a processor tha
44e90 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67  t lacks floating
44ea0 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a   point support,.
44eb0 2a 2a 20 73 75 62 73 74 69 74 75 74 65 20 69 6e  ** substitute in
44ec0 74 65 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69  teger for floati
44ed0 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64  ng-point.*/.#ifd
44ee0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
44ef0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20  LOATING_POINT.# 
44f00 64 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71  define double sq
44f10 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 64 65 66  lite_int64.# def
44f20 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54  ine LONGDOUBLE_T
44f30 59 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  YPE sqlite_int64
44f40 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  .# ifndef SQLITE
44f50 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20 64 65 66  _BIG_DBL.#   def
44f60 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  ine SQLITE_BIG_D
44f70 42 4c 20 28 30 78 37 66 66 66 66 66 66 66 66 66  BL (0x7fffffffff
44f80 66 66 66 66 66 66 29 0a 23 20 65 6e 64 69 66 0a  ffffff).# endif.
44f90 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
44fa0 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55  OMIT_DATETIME_FU
44fb0 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65 20 53  NCS 1.# define S
44fc0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
44fd0 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54   1.# undef SQLIT
44fe0 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36  E_MIXED_ENDIAN_6
44ff0 34 42 49 54 5f 46 4c 4f 41 54 0a 23 65 6e 64 69  4BIT_FLOAT.#endi
45000 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
45010 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69 6e  _BIG_DBL.# defin
45020 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  e SQLITE_BIG_DBL
45030 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a 0a   (1e99).#endif..
45040 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50 44  /*.** OMIT_TEMPD
45050 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 69 66  B is set to 1 if
45060 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d   SQLITE_OMIT_TEM
45070 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c 20  PDB is defined, 
45080 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61 72  or 0.** afterwar
45090 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20 6d  d. Having this m
450a0 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20 74  acro allows us t
450b0 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 63 6f  o cause the C co
450c0 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d  mpiler .** to om
450d0 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 79 20  it code used by 
450e0 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74 68  TEMP tables with
450f0 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64 65  out messy #ifnde
45100 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f  f statements..*/
45110 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
45120 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66 69  MIT_TEMPDB.#defi
45130 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 31  ne OMIT_TEMPDB 1
45140 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
45150 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65 6e  MIT_TEMPDB 0.#en
45160 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  dif../*.** If th
45170 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
45180 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 74  o is set to 1, t
45190 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20  hen NULL values 
451a0 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a  are considered.*
451b0 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e 20  * distinct when 
451c0 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 74  determining whet
451d0 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20 65  her or not two e
451e0 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20 73  ntries are the s
451f0 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49 51  ame.** in a UNIQ
45200 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20  UE index.  This 
45210 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 74 67  is the way Postg
45220 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20 44  reSQL, Oracle, D
45230 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43  B2, MySQL,.** OC
45240 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62 69  ELOT, and Firebi
45250 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54 68  rd all work.  Th
45260 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 78 70  e SQL92 spec exp
45270 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 68 69  licitly says thi
45280 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79 20  s.** is the way 
45290 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 70 6f  things are suppo
452a0 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a  se to work..**.*
452b0 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * If the followi
452c0 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20  ng macro is set 
452d0 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73 20  to 0, the NULLs 
452e0 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20 66  are indistinct f
452f0 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 69  or.** a UNIQUE i
45300 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20 6d  ndex.  In this m
45310 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e 6c  ode, you can onl
45320 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20  y have a single 
45330 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66 6f  NULL entry.** fo
45340 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61  r a column decla
45350 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68 69  red UNIQUE.  Thi
45360 73 20 69 73 20 74 68 65 20 77 61 79 20 49 6e 66  s is the way Inf
45370 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53 65  ormix and SQL Se
45380 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f  rver.** work..*/
45390 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44 49  .#define NULL_DI
453a0 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51 55  STINCT_FOR_UNIQU
453b0 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22  E 1../*.** The "
453c0 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75 6d  file format" num
453d0 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ber is an intege
453e0 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d  r that is increm
453f0 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a 2a  ented whenever.*
45400 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65 6c  * the VDBE-level
45410 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68 61   file format cha
45420 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  nges.  The follo
45430 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69  wing macros defi
45440 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64 65  ne the.** the de
45450 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d 61  fault file forma
45460 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62 61  t for new databa
45470 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78 69  ses and the maxi
45480 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 0a  mum file format.
45490 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62 72  ** that the libr
454a0 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a 2f  ary can read..*/
454b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
454c0 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20  MAX_FILE_FORMAT 
454d0 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  4.#ifndef SQLITE
454e0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f  _DEFAULT_FILE_FO
454f0 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53 51  RMAT.# define SQ
45500 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
45510 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64 69  E_FORMAT 1.#endi
45520 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 65  f../*.** Provide
45530 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65   a default value
45540 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d 50   for SQLITE_TEMP
45550 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 69  _STORE in case i
45560 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69  t is not specifi
45570 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d  ed.** on the com
45580 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66  mand-line.*/.#if
45590 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  ndef SQLITE_TEMP
455a0 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 20  _STORE.# define 
455b0 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
455c0 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  E 1.#endif../*.*
455d0 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 64  * GCC does not d
455e0 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 74  efine the offset
455f0 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 65  of() macro so we
45600 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 69  'll have to do i
45610 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e 0a  t.** ourselves..
45620 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 65  */.#ifndef offse
45630 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 73  tof.#define offs
45640 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c 46  etof(STRUCTURE,F
45650 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 68  IELD) ((int)((ch
45660 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 45  ar*)&((STRUCTURE
45670 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 65  *)0)->FIELD)).#e
45680 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ndif../*.** Chec
45690 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73  k to see if this
456a0 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 42   machine uses EB
456b0 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 6c  CDIC.  (Yes, bel
456c0 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f  ieve it or.** no
456d0 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 69  t, there are sti
456e0 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 20  ll machines out 
456f0 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 45  there that use E
45700 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 27  BCDIC.).*/.#if '
45710 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 64  A' == '\301'.# d
45720 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 43  efine SQLITE_EBC
45730 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65  DIC 1.#else.# de
45740 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 49  fine SQLITE_ASCI
45750 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  I 1.#endif../*.*
45760 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b 6e  * Integers of kn
45770 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 73  own sizes.  Thes
45780 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 74  e typedefs might
45790 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 68   change for arch
457a0 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 65  itectures.** whe
457b0 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 72  re the sizes ver
457c0 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f 72  y.  Preprocessor
457d0 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 69   macros are avai
457e0 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 68  lable so that th
457f0 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 62  e.** types can b
45800 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72  e conveniently r
45810 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70  edefined at comp
45820 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20  ile-type.  Like 
45830 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
45840 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 54      cc '-DUINTPT
45850 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67  R_TYPE=long long
45860 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66   int' ....*/.#if
45870 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45  ndef UINT32_TYPE
45880 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49  .# ifdef HAVE_UI
45890 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e 65  NT32_T.#  define
458a0 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 6e   UINT32_TYPE uin
458b0 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  t32_t.# else.#  
458c0 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59  define UINT32_TY
458d0 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a  PE unsigned int.
458e0 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
458f0 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 59  ifndef UINT16_TY
45900 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f  PE.# ifdef HAVE_
45910 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69  UINT16_T.#  defi
45920 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ne UINT16_TYPE u
45930 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23  int16_t.# else.#
45940 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f    define UINT16_
45950 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68  TYPE unsigned sh
45960 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a  ort int.# endif.
45970 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49  #endif.#ifndef I
45980 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65  NT16_TYPE.# ifde
45990 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a 23  f HAVE_INT16_T.#
459a0 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54    define INT16_T
459b0 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 6c  YPE int16_t.# el
459c0 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54  se.#  define INT
459d0 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 6e  16_TYPE short in
459e0 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  t.# endif.#endif
459f0 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f 54  .#ifndef UINT8_T
45a00 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
45a10 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69  _UINT8_T.#  defi
45a20 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 69  ne UINT8_TYPE ui
45a30 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  nt8_t.# else.#  
45a40 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50  define UINT8_TYP
45a50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a  E unsigned char.
45a60 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
45a70 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45  ifndef INT8_TYPE
45a80 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e  .# ifdef HAVE_IN
45a90 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49  T8_T.#  define I
45aa0 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 0a  NT8_TYPE int8_t.
45ab0 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
45ac0 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e 65   INT8_TYPE signe
45ad0 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23  d char.# endif.#
45ae0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f  endif.#ifndef LO
45af0 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20  NGDOUBLE_TYPE.# 
45b00 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c  define LONGDOUBL
45b10 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62  E_TYPE long doub
45b20 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65  le.#endif.typede
45b30 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  f sqlite_int64 i
45b40 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  64;          /* 
45b50 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  8-byte signed in
45b60 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66  teger */.typedef
45b70 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75   sqlite_uint64 u
45b80 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38  64;         /* 8
45b90 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69  -byte unsigned i
45ba0 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
45bb0 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33  f UINT32_TYPE u3
45bc0 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  2;           /* 
45bd0 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  4-byte unsigned 
45be0 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
45bf0 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ef UINT16_TYPE u
45c00 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  16;           /*
45c10 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   2-byte unsigned
45c20 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
45c30 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69  def INT16_TYPE i
45c40 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  16;            /
45c50 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 2-byte signed 
45c60 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
45c70 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38  ef UINT8_TYPE u8
45c80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
45c90 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   1-byte unsigned
45ca0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
45cb0 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 38  def INT8_TYPE i8
45cc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
45cd0 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 1-byte signed 
45ce0 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a  integer */../*.*
45cf0 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65  * Macros to dete
45d00 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68  rmine whether th
45d10 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67  e machine is big
45d20 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61   or little endia
45d30 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20  n,.** evaluated 
45d40 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23  at runtime..*/.#
45d50 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41  ifdef SQLITE_AMA
45d60 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45  LGAMATION.SQLITE
45d70 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69  _PRIVATE const i
45d80 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20  nt sqlite3one = 
45d90 31 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f  1;.#else.SQLITE_
45da0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e  PRIVATE const in
45db0 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65  t sqlite3one;.#e
45dc0 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64  ndif.#if defined
45dd0 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
45de0 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
45df0 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c  efined(_M_IX86)\
45e00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
45e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
45e20 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
45e30 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  4) || defined(__
45e40 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69  x86_64__).# defi
45e50 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  ne SQLITE_BIGEND
45e60 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 6e  IAN    0.# defin
45e70 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45  e SQLITE_LITTLEE
45e80 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e 65  NDIAN 1.# define
45e90 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
45ea0 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31  IVE  SQLITE_UTF1
45eb0 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  6LE.#else.# defi
45ec0 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  ne SQLITE_BIGEND
45ed0 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a  IAN    (*(char *
45ee0 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d  )(&sqlite3one)==
45ef0 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  0).# define SQLI
45f00 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20  TE_LITTLEENDIAN 
45f10 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69  (*(char *)(&sqli
45f20 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65  te3one)==1).# de
45f30 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31  fine SQLITE_UTF1
45f40 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 5f  6NATIVE (SQLITE_
45f50 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45  BIGENDIAN?SQLITE
45f60 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 5f  _UTF16BE:SQLITE_
45f70 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a  UTF16LE).#endif.
45f80 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73  ./*.** Constants
45f90 20 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 74   for the largest
45fa0 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f   and smallest po
45fb0 73 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 69  ssible 64-bit si
45fc0 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a  gned integers..*
45fd0 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 61  * These macros a
45fe0 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77  re designed to w
45ff0 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e  ork correctly on
46000 20 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e 64   both 32-bit and
46010 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69   64-bit.** compi
46020 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  lers..*/.#define
46030 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 20   LARGEST_INT64  
46040 28 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 69  (0xffffffff|(((i
46050 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c  64)0x7fffffff)<<
46060 33 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d 41  32)).#define SMA
46070 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 69  LLEST_INT64 (((i
46080 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 54  64)-1) - LARGEST
46090 5f 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20  _INT64)../* .** 
460a0 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65  Round up a numbe
460b0 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61  r to the next la
460c0 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66  rger multiple of
460d0 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 65   8.  This is use
460e0 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d  d.** to force 8-
460f0 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f  byte alignment o
46100 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65  n 64-bit archite
46110 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69  ctures..*/.#defi
46120 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 20  ne ROUND8(x)    
46130 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f   (((x)+7)&~7)../
46140 2a 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20  *.** Round down 
46150 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 6d  to the nearest m
46160 75 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a  ultiple of 8.*/.
46170 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57  #define ROUNDDOW
46180 4e 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a 0a  N8(x) ((x)&~7)..
46190 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
461a0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
461b0 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ng structure is 
461c0 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
461d0 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a  e busy-handler.*
461e0 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  * callback for a
461f0 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61   given sqlite ha
46200 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ndle. .**.** The
46210 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64   sqlite.busyHand
46220 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  ler member of th
46230 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 20  e sqlite struct 
46240 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73  contains the bus
46250 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f  y.** callback fo
46260 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  r the database h
46270 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65  andle. Each page
46280 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65  r opened via the
46290 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c   sqlite.** handl
462a0 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 6f  e is passed a po
462b0 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e  inter to sqlite.
462c0 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65  busyHandler. The
462d0 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a   busy-handler.**
462e0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72   callback is cur
462f0 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f  rently invoked o
46300 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  nly from within 
46310 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65  pager.c..*/.type
46320 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 48  def struct BusyH
46330 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c  andler BusyHandl
46340 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48  er;.struct BusyH
46350 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28  andler {.  int (
46360 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69  *xFunc)(void *,i
46370 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73  nt);  /* The bus
46380 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  y callback */.  
46390 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20  void *pArg;     
463a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
463b0 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 20  rst arg to busy 
463c0 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  callback */.  in
463d0 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20  t nBusy;        
463e0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72           /* Incr
463f0 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63  emented with eac
46400 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d  h busy call */.}
46410 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66  ;../*.** Name of
46420 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61   the master data
46430 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 65  base table.  The
46440 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
46450 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 73   table.** is a s
46460 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 61  pecial table tha
46470 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  t holds the name
46480 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 73  s and attributes
46490 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 20   of all.** user 
464a0 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63  tables and indic
464b0 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  es..*/.#define M
464c0 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 20  ASTER_NAME      
464d0 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22   "sqlite_master"
464e0 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41  .#define TEMP_MA
464f0 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69  STER_NAME  "sqli
46500 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0a  te_temp_master".
46510 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 2d  ./*.** The root-
46520 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 74  page of the mast
46530 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  er database tabl
46540 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41  e..*/.#define MA
46550 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 20  STER_ROOT       
46560 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d  1../*.** The nam
46570 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  e of the schema 
46580 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  table..*/.#defin
46590 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 78  e SCHEMA_TABLE(x
465a0 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 44  )  ((!OMIT_TEMPD
465b0 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 5f  B)&&(x==1)?TEMP_
465c0 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 54  MASTER_NAME:MAST
465d0 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20  ER_NAME)../*.** 
465e0 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d 61  A convenience ma
465f0 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e 73  cro that returns
46600 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
46610 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e  lements in.** an
46620 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 69   array..*/.#defi
46630 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 20  ne ArraySize(X) 
46640 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66     ((int)(sizeof
46650 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29  (X)/sizeof(X[0])
46660 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  ))../*.** The fo
46670 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61 73  llowing value as
46680 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d 65   a destructor me
46690 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69 74  ans to use sqlit
466a0 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20 54  e3DbFree()..** T
466b0 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  his is an intern
466c0 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f 20  al extension to 
466d0 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 61 6e  SQLITE_STATIC an
466e0 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  d SQLITE_TRANSIE
466f0 4e 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  NT..*/.#define S
46700 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20 20  QLITE_DYNAMIC   
46710 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75  ((sqlite3_destru
46720 63 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74 65  ctor_type)sqlite
46730 33 44 62 46 72 65 65 29 0a 0a 2f 2a 0a 2a 2a 20  3DbFree)../*.** 
46740 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4d 49 54  When SQLITE_OMIT
46750 5f 57 53 44 20 69 73 20 64 65 66 69 6e 65 64 2c  _WSD is defined,
46760 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
46770 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 66 6f  he target platfo
46780 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73  rm does.** not s
46790 75 70 70 6f 72 74 20 57 72 69 74 61 62 6c 65 20  upport Writable 
467a0 53 74 61 74 69 63 20 44 61 74 61 20 28 57 53 44  Static Data (WSD
467b0 29 20 73 75 63 68 20 61 73 20 67 6c 6f 62 61 6c  ) such as global
467c0 20 61 6e 64 20 73 74 61 74 69 63 20 76 61 72 69   and static vari
467d0 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c 20 76 61  ables..** All va
467e0 72 69 61 62 6c 65 73 20 6d 75 73 74 20 65 69 74  riables must eit
467f0 68 65 72 20 62 65 20 6f 6e 20 74 68 65 20 73 74  her be on the st
46800 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 63 61 6c  ack or dynamical
46810 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f  ly allocated fro
46820 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 2e 20 20  m.** the heap.  
46830 57 68 65 6e 20 57 53 44 20 69 73 20 75 6e 73 75  When WSD is unsu
46840 70 70 6f 72 74 65 64 2c 20 74 68 65 20 76 61 72  pported, the var
46850 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 6f  iable declaratio
46860 6e 73 20 73 63 61 74 74 65 72 65 64 0a 2a 2a 20  ns scattered.** 
46870 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 53  throughout the S
46880 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 73 74 20  QLite code must 
46890 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 6e 74 73  become constants
468a0 20 69 6e 73 74 65 61 64 2e 20 20 54 68 65 20 53   instead.  The S
468b0 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 6d 61 63  QLITE_WSD.** mac
468c0 72 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  ro is used for t
468d0 68 69 73 20 70 75 72 70 6f 73 65 2e 20 20 41 6e  his purpose.  An
468e0 64 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 66  d instead of ref
468f0 65 72 65 6e 63 69 6e 67 20 74 68 65 20 76 61 72  erencing the var
46900 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 63 74 6c  iable.** directl
46910 79 2c 20 77 65 20 75 73 65 20 69 74 73 20 63 6f  y, we use its co
46920 6e 73 74 61 6e 74 20 61 73 20 61 20 6b 65 79 20  nstant as a key 
46930 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 72 75  to lookup the ru
46940 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64  n-time allocated
46950 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74 20  .** buffer that 
46960 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 72 69 61  holds real varia
46970 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e 73 74 61  ble.  The consta
46980 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20 69  nt is also the i
46990 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20 66 6f  nitializer.** fo
469a0 72 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61  r the run-time a
469b0 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e  llocated buffer.
469c0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 75 73  .**.** In the us
469d0 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 57  ual case where W
469e0 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c  SD is supported,
469f0 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44 20   the SQLITE_WSD 
46a00 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a 20 6d 61  and GLOBAL.** ma
46a10 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f  cros become no-o
46a20 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65 72 6f  ps and have zero
46a30 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d 70   performance imp
46a40 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  act..*/.#ifdef S
46a50 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20  QLITE_OMIT_WSD. 
46a60 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
46a70 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 64 65 66  WSD const.  #def
46a80 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20  ine GLOBAL(t,v) 
46a90 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 5f 77 73  (*(t*)sqlite3_ws
46aa0 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a 29 26 28  d_find((void*)&(
46ab0 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 29 29 0a  v), sizeof(v))).
46ac0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
46ad0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 47 4c  3GlobalConfig GL
46ae0 4f 42 41 4c 28 73 74 72 75 63 74 20 53 71 6c 69  OBAL(struct Sqli
46af0 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 6c 69 74  te3Config, sqlit
46b00 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c 49 54 45  e3Config).SQLITE
46b10 5f 41 50 49 20 20 20 69 6e 74 20 73 71 6c 69 74  _API   int sqlit
46b20 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 6e 74 20  e3_wsd_init(int 
46b30 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 4c 49 54  N, int J);.SQLIT
46b40 45 5f 41 50 49 20 20 20 76 6f 69 64 20 2a 73 71  E_API   void *sq
46b50 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 76  lite3_wsd_find(v
46b60 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c 29 3b 0a  oid *K, int L);.
46b70 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
46b80 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 20 23 64  SQLITE_WSD .  #d
46b90 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76  efine GLOBAL(t,v
46ba0 29 20 76 0a 20 20 23 64 65 66 69 6e 65 20 73 71  ) v.  #define sq
46bb0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
46bc0 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 0a  g sqlite3Config.
46bd0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
46be0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
46bf0 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 73  os are used to s
46c00 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65 72  uppress compiler
46c10 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74 6f   warnings and to
46c20 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c 65 61  .** make it clea
46c30 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61 64 65  r to human reade
46c40 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63 74 69  rs when a functi
46c50 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  on parameter is 
46c60 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0a 2a 2a  deliberately .**
46c70 20 6c 65 66 74 20 75 6e 75 73 65 64 20 77 69 74   left unused wit
46c80 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20  hin the body of 
46c90 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73  a function. This
46ca0 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e 73   usually happens
46cb0 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 6e 63 74   when.** a funct
46cc0 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 76 69  ion is called vi
46cd0 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  a a function poi
46ce0 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 6d 70 6c  nter. For exampl
46cf0 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d  e the .** implem
46d00 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53  entation of an S
46d10 51 4c 20 61 67 67 72 65 67 61 74 65 20 73 74 65  QL aggregate ste
46d20 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 6e  p callback may n
46d30 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a 20 70 61  ot use the.** pa
46d40 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 69  rameter indicati
46d50 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ng the number of
46d60 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65   arguments passe
46d70 64 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 61  d to the aggrega
46d80 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 6b 6e 6f  te,.** if it kno
46d90 77 73 20 74 68 61 74 20 74 68 69 73 20 69 73 20  ws that this is 
46da0 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 77 68 65  enforced elsewhe
46db0 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  re..**.** When a
46dc0 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65   function parame
46dd0 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20  ter is not used 
46de0 61 74 20 61 6c 6c 20 77 69 74 68 69 6e 20 74 68  at all within th
46df0 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e 63  e body of a func
46e00 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 73 20 67  tion,.** it is g
46e10 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 64 20 22  enerally named "
46e20 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 4e 6f 74  NotUsed" or "Not
46e30 55 73 65 64 32 22 20 74 6f 20 6d 61 6b 65 20 74  Used2" to make t
46e40 68 69 6e 67 73 20 65 76 65 6e 20 63 6c 65 61 72  hings even clear
46e50 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  er..** However, 
46e60 74 68 65 73 65 20 6d 61 63 72 6f 73 20 6d 61 79  these macros may
46e70 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 6f   also be used to
46e80 20 73 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e   suppress warnin
46e90 67 73 20 72 65 6c 61 74 65 64 20 74 6f 0a 2a 2a  gs related to.**
46ea0 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74   parameters that
46eb0 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20   may or may not 
46ec0 62 65 20 75 73 65 64 20 64 65 70 65 6e 64 69 6e  be used dependin
46ed0 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  g on compilation
46ee0 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 46 6f 72   options..** For
46ef0 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 65 20 70   example those p
46f00 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c 79 20 75  arameters only u
46f10 73 65 64 20 69 6e 20 61 73 73 65 72 74 28 29 20  sed in assert() 
46f20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 6e 20 74  statements. In t
46f30 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 20 74 68  hese.** cases th
46f40 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  e parameters are
46f50 20 6e 61 6d 65 64 20 61 73 20 70 65 72 20 74 68   named as per th
46f60 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74 69  e usual conventi
46f70 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ons..*/.#define 
46f80 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
46f90 28 78 29 20 28 76 6f 69 64 29 28 78 29 0a 23 64  (x) (void)(x).#d
46fa0 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52  efine UNUSED_PAR
46fb0 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e 55  AMETER2(x,y) UNU
46fc0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29  SED_PARAMETER(x)
46fd0 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45  ,UNUSED_PARAMETE
46fe0 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77  R(y)../*.** Forw
46ff0 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 74  ard references t
47000 6f 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2f 0a  o structures.*/.
47010 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41  typedef struct A
47020 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 6f 3b 0a  ggInfo AggInfo;.
47030 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41  typedef struct A
47040 75 74 68 43 6f 6e 74 65 78 74 20 41 75 74 68 43  uthContext AuthC
47050 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20  ontext;.typedef 
47060 73 74 72 75 63 74 20 42 69 74 76 65 63 20 42 69  struct Bitvec Bi
47070 74 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73 74  tvec;.typedef st
47080 72 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 53  ruct RowSet RowS
47090 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  et;.typedef stru
470a0 63 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53  ct CollSeq CollS
470b0 65 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  eq;.typedef stru
470c0 63 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e  ct Column Column
470d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
470e0 20 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20   Db Db;.typedef 
470f0 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 63  struct Schema Sc
47100 68 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74  hema;.typedef st
47110 72 75 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a  ruct Expr Expr;.
47120 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45  typedef struct E
47130 78 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74  xprList ExprList
47140 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
47150 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65   FKey FKey;.type
47160 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44  def struct FuncD
47170 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65  ef FuncDef;.type
47180 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44  def struct FuncD
47190 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 61  efHash FuncDefHa
471a0 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  sh;.typedef stru
471b0 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74  ct IdList IdList
471c0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
471d0 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 79   Index Index;.ty
471e0 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79  pedef struct Key
471f0 43 6c 61 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a  Class KeyClass;.
47200 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4b  typedef struct K
47210 65 79 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a  eyInfo KeyInfo;.
47220 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c  typedef struct L
47230 6f 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69  ookaside Lookasi
47240 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  de;.typedef stru
47250 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74  ct LookasideSlot
47260 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a   LookasideSlot;.
47270 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d  typedef struct M
47280 6f 64 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79  odule Module;.ty
47290 70 65 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d  pedef struct Nam
472a0 65 43 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e  eContext NameCon
472b0 74 65 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74  text;.typedef st
472c0 72 75 63 74 20 50 61 72 73 65 20 50 61 72 73 65  ruct Parse Parse
472d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
472e0 20 53 61 76 65 70 6f 69 6e 74 20 53 61 76 65 70   Savepoint Savep
472f0 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74  oint;.typedef st
47300 72 75 63 74 20 53 65 6c 65 63 74 20 53 65 6c 65  ruct Select Sele
47310 63 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ct;.typedef stru
47320 63 74 20 53 72 63 4c 69 73 74 20 53 72 63 4c 69  ct SrcList SrcLi
47330 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  st;.typedef stru
47340 63 74 20 53 74 72 41 63 63 75 6d 20 53 74 72 41  ct StrAccum StrA
47350 63 63 75 6d 3b 0a 74 79 70 65 64 65 66 20 73 74  ccum;.typedef st
47360 72 75 63 74 20 54 61 62 6c 65 20 54 61 62 6c 65  ruct Table Table
47370 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
47380 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61 62 6c 65   TableLock Table
47390 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74  Lock;.typedef st
473a0 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f 6b 65 6e  ruct Token Token
473b0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
473c0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 54 72   TriggerStack Tr
473d0 69 67 67 65 72 53 74 61 63 6b 3b 0a 74 79 70 65  iggerStack;.type
473e0 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67  def struct Trigg
473f0 65 72 53 74 65 70 20 54 72 69 67 67 65 72 53 74  erStep TriggerSt
47400 65 70 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ep;.typedef stru
47410 63 74 20 54 72 69 67 67 65 72 20 54 72 69 67 67  ct Trigger Trigg
47420 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  er;.typedef stru
47430 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  ct UnpackedRecor
47440 64 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  d UnpackedRecord
47450 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
47460 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 72 3b 0a   Walker Walker;.
47470 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
47480 68 65 72 65 50 6c 61 6e 20 57 68 65 72 65 50 6c  herePlan WherePl
47490 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  an;.typedef stru
474a0 63 74 20 57 68 65 72 65 49 6e 66 6f 20 57 68 65  ct WhereInfo Whe
474b0 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20  reInfo;.typedef 
474c0 73 74 72 75 63 74 20 57 68 65 72 65 4c 65 76 65  struct WhereLeve
474d0 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b 0a 0a 2f  l WhereLevel;../
474e0 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f 75 72 63  *.** Defer sourc
474f0 69 6e 67 20 76 64 62 65 2e 68 20 61 6e 64 20 62  ing vdbe.h and b
47500 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 61 66 74  tree.h until aft
47510 65 72 20 74 68 65 20 22 75 38 22 20 61 6e 64 20  er the "u8" and 
47520 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 6c 65 72  .** "BusyHandler
47530 22 20 74 79 70 65 64 65 66 73 2e 20 76 64 62 65  " typedefs. vdbe
47540 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73  .h also requires
47550 20 61 20 66 65 77 20 6f 66 20 74 68 65 20 6f 70   a few of the op
47560 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  aque.** pointer 
47570 74 79 70 65 73 20 28 69 2e 65 2e 20 46 75 6e 63  types (i.e. Func
47580 44 65 66 29 20 64 65 66 69 6e 65 64 20 61 62 6f  Def) defined abo
47590 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ve..*/./********
475a0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62  ****** Include b
475b0 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20 6d 69  tree.h in the mi
475c0 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
475d0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
475e0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
475f0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
47600 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a  e btree.h ******
47610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47630 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
47640 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
47650 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
47660 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
47670 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
47680 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
47690 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
476a0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
476b0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
476c0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
476d0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
476e0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
476f0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
47700 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
47710 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
47720 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
47730 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
47740 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
47750 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
47760 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
47770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
477a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
477b0 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
477c0 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e  e defines the in
477d0 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65  terface that the
477e0 20 73 71 6c 69 74 65 20 42 2d 54 72 65 65 20 66   sqlite B-Tree f
477f0 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d  ile.** subsystem
47800 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  .  See comments 
47810 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f  in the source co
47820 64 65 20 66 6f 72 20 61 20 64 65 74 61 69 6c 65  de for a detaile
47830 64 20 64 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a  d description.**
47840 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 69 6e   of what each in
47850 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 20  terface routine 
47860 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  does..**.** @(#)
47870 20 24 49 64 3a 20 62 74 72 65 65 2e 68 2c 76 20   $Id: btree.h,v 
47880 31 2e 31 31 31 20 32 30 30 39 2f 30 33 2f 31 38  1.111 2009/03/18
47890 20 31 30 3a 33 33 3a 30 31 20 64 61 6e 69 65 6c   10:33:01 daniel
478a0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23  k1977 Exp $.*/.#
478b0 69 66 6e 64 65 66 20 5f 42 54 52 45 45 5f 48 5f  ifndef _BTREE_H_
478c0 0a 23 64 65 66 69 6e 65 20 5f 42 54 52 45 45 5f  .#define _BTREE_
478d0 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 69  H_../* TODO: Thi
478e0 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 73 20  s definition is 
478f0 6a 75 73 74 20 69 6e 63 6c 75 64 65 64 20 73 6f  just included so
47900 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 63   other modules c
47910 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e 65  ompile. It.** ne
47920 65 64 73 20 74 6f 20 62 65 20 72 65 76 69 73 69  eds to be revisi
47930 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ted..*/.#define 
47940 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d  SQLITE_N_BTREE_M
47950 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 66  ETA 10../*.** If
47960 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f 6e 2d   defined as non-
47970 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 75 75  zero, auto-vacuu
47980 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79 20  m is enabled by 
47990 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 77 69  default. Otherwi
479a0 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 62 65  se.** it must be
479b0 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 20 65   turned on for e
479c0 61 63 68 20 64 61 74 61 62 61 73 65 20 75 73 69  ach database usi
479d0 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 6f 5f  ng "PRAGMA auto_
479e0 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f 0a  vacuum = 1"..*/.
479f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
47a00 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
47a10 4d 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49  M.  #define SQLI
47a20 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56  TE_DEFAULT_AUTOV
47a30 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a  ACUUM 0.#endif..
47a40 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55  #define BTREE_AU
47a50 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 20 30 20  TOVACUUM_NONE 0 
47a60 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74         /* Do not
47a70 20 64 6f 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   do auto-vacuum 
47a80 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45  */.#define BTREE
47a90 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c  _AUTOVACUUM_FULL
47aa0 20 31 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20   1        /* Do 
47ab0 66 75 6c 6c 20 61 75 74 6f 2d 76 61 63 75 75 6d  full auto-vacuum
47ac0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
47ad0 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43  E_AUTOVACUUM_INC
47ae0 52 20 32 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  R 2        /* In
47af0 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
47b00 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61   */../*.** Forwa
47b10 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20  rd declarations 
47b20 6f 66 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a  of structure.*/.
47b30 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
47b40 74 72 65 65 20 42 74 72 65 65 3b 0a 74 79 70 65  tree Btree;.type
47b50 64 65 66 20 73 74 72 75 63 74 20 42 74 43 75 72  def struct BtCur
47b60 73 6f 72 20 42 74 43 75 72 73 6f 72 3b 0a 74 79  sor BtCursor;.ty
47b70 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 53  pedef struct BtS
47b80 68 61 72 65 64 20 42 74 53 68 61 72 65 64 3b 0a  hared BtShared;.
47b90 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
47ba0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 42  treeMutexArray B
47bb0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3b 0a  treeMutexArray;.
47bc0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  ./*.** This stru
47bd0 63 74 75 72 65 20 72 65 63 6f 72 64 73 20 61 6c  cture records al
47be0 6c 20 6f 66 20 74 68 65 20 42 74 72 65 65 73 20  l of the Btrees 
47bf0 74 68 61 74 20 6e 65 65 64 20 74 6f 20 68 6f 6c  that need to hol
47c00 64 0a 2a 2a 20 61 20 6d 75 74 65 78 20 62 65 66  d.** a mutex bef
47c10 6f 72 65 20 77 65 20 65 6e 74 65 72 20 73 71 6c  ore we enter sql
47c20 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20  ite3VdbeExec(). 
47c30 20 54 68 65 20 42 74 72 65 65 73 20 61 72 65 0a   The Btrees are.
47c40 2a 2a 20 61 72 65 20 70 6c 61 63 65 64 20 69 6e  ** are placed in
47c50 20 61 42 74 72 65 65 5b 5d 20 69 6e 20 6f 72 64   aBtree[] in ord
47c60 65 72 20 6f 66 20 61 42 74 72 65 65 5b 5d 2d 3e  er of aBtree[]->
47c70 70 42 74 2e 20 20 54 68 61 74 20 77 61 79 2c 0a  pBt.  That way,.
47c80 2a 2a 20 77 65 20 63 61 6e 20 61 6c 77 61 79 73  ** we can always
47c90 20 6c 6f 63 6b 20 61 6e 64 20 75 6e 6c 6f 63 6b   lock and unlock
47ca0 20 74 68 65 6d 20 61 6c 6c 20 71 75 69 63 6b 6c   them all quickl
47cb0 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 72  y..*/.struct Btr
47cc0 65 65 4d 75 74 65 78 41 72 72 61 79 20 7b 0a 20  eeMutexArray {. 
47cd0 20 69 6e 74 20 6e 4d 75 74 65 78 3b 0a 20 20 42   int nMutex;.  B
47ce0 74 72 65 65 20 2a 61 42 74 72 65 65 5b 53 51 4c  tree *aBtree[SQL
47cf0 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44  ITE_MAX_ATTACHED
47d00 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 54 45  +1];.};...SQLITE
47d10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
47d20 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20  ite3BtreeOpen(. 
47d30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
47d40 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d  lename,   /* Nam
47d50 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  e of database fi
47d60 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20  le to open */.  
47d70 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
47d80 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f           /* Asso
47d90 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20  ciated database 
47da0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
47db0 42 74 72 65 65 20 2a 2a 2c 20 20 20 20 20 20 20  Btree **,       
47dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
47dd0 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a 20 68  rn open Btree* h
47de0 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ere */.  int fla
47df0 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
47e00 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20    /* Flags */.  
47e10 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20  int vfsFlags    
47e20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
47e30 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68  s passed through
47e40 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a 2f 0a   to VFS open */.
47e50 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73  );../* The flags
47e60 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
47e70 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 20 63  lite3BtreeOpen c
47e80 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73  an be the bitwis
47e90 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66  e or of the.** f
47ea0 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e  ollowing values.
47eb0 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54 68  .**.** NOTE:  Th
47ec0 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 74 20  ese values must 
47ed0 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65 73  match the corres
47ee0 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f 20 76  ponding PAGER_ v
47ef0 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61 67 65  alues in.** page
47f00 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  r.h..*/.#define 
47f10 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e  BTREE_OMIT_JOURN
47f20 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e 6f 74  AL  1  /* Do not
47f30 20 75 73 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e   use journal.  N
47f40 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64  o argument */.#d
47f50 65 66 69 6e 65 20 42 54 52 45 45 5f 4e 4f 5f 52  efine BTREE_NO_R
47f60 45 41 44 4c 4f 43 4b 20 20 20 32 20 20 2f 2a 20  EADLOCK   2  /* 
47f70 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f  Omit readlocks o
47f80 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73  n readonly files
47f90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
47fa0 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 20 20  E_MEMORY        
47fb0 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20  4  /* In-memory 
47fc0 44 42 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74  DB.  No argument
47fd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
47fe0 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 20  E_READONLY      
47ff0 38 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64  8  /* Open the d
48000 61 74 61 62 61 73 65 20 69 6e 20 72 65 61 64 2d  atabase in read-
48010 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f 0a 23 64 65  only mode */.#de
48020 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44 57  fine BTREE_READW
48030 52 49 54 45 20 20 20 20 31 36 20 20 2f 2a 20 4f  RITE    16  /* O
48040 70 65 6e 20 66 6f 72 20 62 6f 74 68 20 72 65 61  pen for both rea
48050 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67  ding and writing
48060 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
48070 45 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 33  E_CREATE       3
48080 32 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65  2  /* Create the
48090 20 64 61 74 61 62 61 73 65 20 69 66 20 69 74 20   database if it 
480a0 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 2a  does not exist *
480b0 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
480c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
480d0 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a 29 3b  eeClose(Btree*);
480e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
480f0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
48100 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74 72  SetCacheSize(Btr
48110 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ee*,int);.SQLITE
48120 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
48130 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65  ite3BtreeSetSafe
48140 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a 2c 69  tyLevel(Btree*,i
48150 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
48160 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
48170 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61  te3BtreeSyncDisa
48180 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a 53 51  bled(Btree*);.SQ
48190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
481a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
481b0 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a 2c  PageSize(Btree*,
481c0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
481d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
481e0 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
481f0 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0a 53 51  Size(Btree*);.SQ
48200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
48210 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78   sqlite3BtreeMax
48220 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 2a  PageCount(Btree*
48230 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
48240 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48250 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65  3BtreeGetReserve
48260 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
48270 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
48280 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74 6f  ite3BtreeSetAuto
48290 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 2c 20  Vacuum(Btree *, 
482a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
482b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
482c0 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63 75  BtreeGetAutoVacu
482d0 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c  um(Btree *);.SQL
482e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
482f0 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
48300 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 2c 69 6e  nTrans(Btree*,in
48310 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
48320 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
48330 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  reeCommitPhaseOn
48340 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e 73 74 20  e(Btree*, const 
48350 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 3b 0a  char *zMaster);.
48360 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
48370 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
48380 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74  ommitPhaseTwo(Bt
48390 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
483a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
483b0 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72  3BtreeCommit(Btr
483c0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
483d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
483e0 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74  BtreeRollback(Bt
483f0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
48400 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48410 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28  3BtreeBeginStmt(
48420 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Btree*,int);.SQL
48430 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
48440 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
48450 74 65 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20  teTable(Btree*, 
48460 69 6e 74 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29  int*, int flags)
48470 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
48480 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
48490 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65  eIsInTrans(Btree
484a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
484b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
484c0 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
484d0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
484e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
484f0 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63  ite3BtreeIsInBac
48500 6b 75 70 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  kup(Btree*);.SQL
48510 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
48520 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 53 63   *sqlite3BtreeSc
48530 68 65 6d 61 28 42 74 72 65 65 20 2a 2c 20 69 6e  hema(Btree *, in
48540 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20  t, void(*)(void 
48550 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
48560 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
48570 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64  treeSchemaLocked
48580 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54  (Btree *);.SQLIT
48590 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
485a0 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61  lite3BtreeLockTa
485b0 62 6c 65 28 42 74 72 65 65 20 2a 2c 20 69 6e 74  ble(Btree *, int
485c0 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52  , u8);.SQLITE_PR
485d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
485e0 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28  3BtreeSavepoint(
485f0 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 69 6e  Btree *, int, in
48600 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
48610 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
48620 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46  sqlite3BtreeGetF
48630 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a 29  ilename(Btree *)
48640 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
48650 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
48660 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72  ite3BtreeGetJour
48670 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 29  nalname(Btree *)
48680 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
48690 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
486a0 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20  eCopyFile(Btree 
486b0 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51  *, Btree *);..SQ
486c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
486d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63   sqlite3BtreeInc
486e0 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29  rVacuum(Btree *)
486f0 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20  ;../* The flags 
48700 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
48710 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
48720 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 20  able can be the 
48730 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66  bitwise OR.** of
48740 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
48750 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65  lags:.*/.#define
48760 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20   BTREE_INTKEY   
48770 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20    1    /* Table 
48780 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20  has only 64-bit 
48790 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b  signed integer k
487a0 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  eys */.#define B
487b0 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 20  TREE_ZERODATA   
487c0 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61  2    /* Table ha
487d0 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f  s keys only - no
487e0 20 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65   data */.#define
487f0 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 20   BTREE_LEAFDATA 
48800 20 20 34 20 20 20 20 2f 2a 20 44 61 74 61 20 73    4    /* Data s
48810 74 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 73 20  tored in leaves 
48820 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 20 49  only.  Implies I
48830 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45  NTKEY */..SQLITE
48840 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
48850 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62  ite3BtreeDropTab
48860 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20  le(Btree*, int, 
48870 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
48880 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48890 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
488a0 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Btree*, int, in
488b0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
488c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
488d0 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65  treeGetMeta(Btre
488e0 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32  e*, int idx, u32
488f0 20 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54   *pValue);.SQLIT
48900 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
48910 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
48920 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74  Meta(Btree*, int
48930 20 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29   idx, u32 value)
48940 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
48950 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
48960 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73  eeTripAllCursors
48970 28 42 74 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a  (Btree*, int);..
48980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
48990 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
489a0 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c  ursor(.  Btree*,
489b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
489c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
489d0 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e   BTree containin
489e0 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  g table to open 
489f0 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c  */.  int iTable,
48a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
48a20 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 20  ex of root page 
48a30 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c  */.  int wrFlag,
48a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66            /* 1 f
48a60 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20 66  or writing.  0 f
48a70 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a  or read-only */.
48a80 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f    struct KeyInfo
48a90 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *,              
48aa0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
48ab0 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70  argument to comp
48ac0 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  are function */.
48ad0 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
48ae0 73 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20  sor             
48af0 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
48b00 74 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72 20  to write cursor 
48b10 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a  structure */.);.
48b20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
48b30 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
48b40 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b  ursorSize(void);
48b50 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
48b60 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
48b70 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43  eCloseCursor(BtC
48b80 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ursor*);.SQLITE_
48b90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
48ba0 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 0a  te3BtreeMoveto(.
48bb0 20 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 63    BtCursor*,.  c
48bc0 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
48bd0 0a 20 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 69  .  i64 nKey,.  i
48be0 6e 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a  nt bias,.  int *
48bf0 70 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50  pRes.);.SQLITE_P
48c00 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
48c10 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
48c20 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f  acked(.  BtCurso
48c30 72 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 52 65  r*,.  UnpackedRe
48c40 63 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20  cord *pUnKey,.  
48c50 69 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 69 6e  i64 intKey,.  in
48c60 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70  t bias,.  int *p
48c70 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Res.);.SQLITE_PR
48c80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48c90 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d  3BtreeCursorHasM
48ca0 6f 76 65 64 28 42 74 43 75 72 73 6f 72 2a 2c 20  oved(BtCursor*, 
48cb0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
48cc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48cd0 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43  3BtreeDelete(BtC
48ce0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ursor*);.SQLITE_
48cf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
48d00 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 42  te3BtreeInsert(B
48d10 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20  tCursor*, const 
48d20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20  void *pKey, i64 
48d30 6e 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20  nKey,.          
48d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d50 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f          const vo
48d60 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  id *pData, int n
48d70 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
48d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d90 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72          int nZer
48da0 6f 2c 20 69 6e 74 20 62 69 61 73 29 3b 0a 53 51  o, int bias);.SQ
48db0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
48dc0 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72   sqlite3BtreeFir
48dd0 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e  st(BtCursor*, in
48de0 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45  t *pRes);.SQLITE
48df0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
48e00 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74  ite3BtreeLast(Bt
48e10 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52  Cursor*, int *pR
48e20 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  es);.SQLITE_PRIV
48e30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
48e40 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f  treeNext(BtCurso
48e50 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a  r*, int *pRes);.
48e60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
48e70 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  nt sqlite3BtreeE
48e80 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53  of(BtCursor*);.S
48e90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
48ea0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 6c  t sqlite3BtreeFl
48eb0 61 67 73 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  ags(BtCursor*);.
48ec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
48ed0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  nt sqlite3BtreeP
48ee0 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72  revious(BtCursor
48ef0 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53  *, int *pRes);.S
48f00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
48f10 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  t sqlite3BtreeKe
48f20 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c  ySize(BtCursor*,
48f30 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51   i64 *pSize);.SQ
48f40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
48f50 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
48f60 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20  (BtCursor*, u32 
48f70 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c  offset, u32 amt,
48f80 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
48f90 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20  PRIVATE sqlite3 
48fa0 2a 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72  *sqlite3BtreeCur
48fb0 73 6f 72 44 62 28 63 6f 6e 73 74 20 42 74 43 75  sorDb(const BtCu
48fc0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
48fd0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69  RIVATE const voi
48fe0 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b  d *sqlite3BtreeK
48ff0 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72  eyFetch(BtCursor
49000 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53  *, int *pAmt);.S
49010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
49020 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
49030 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28  3BtreeDataFetch(
49040 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
49050 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pAmt);.SQLITE_PR
49060 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
49070 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42  3BtreeDataSize(B
49080 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70  tCursor*, u32 *p
49090 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Size);.SQLITE_PR
490a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
490b0 33 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72  3BtreeData(BtCur
490c0 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74  sor*, u32 offset
490d0 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a  , u32 amt, void*
490e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
490f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
49100 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69  reeSetCachedRowi
49110 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 73 71 6c  d(BtCursor*, sql
49120 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c  ite3_int64);.SQL
49130 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
49140 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
49150 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52  3BtreeGetCachedR
49160 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a 29 3b  owid(BtCursor*);
49170 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
49180 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
49190 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63  reeIntegrityChec
491a0 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61  k(Btree*, int *a
491b0 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c  Root, int nRoot,
491c0 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c   int, int*);.SQL
491d0 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 72 75  ITE_PRIVATE stru
491e0 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65  ct Pager *sqlite
491f0 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 65  3BtreePager(Btre
49200 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  e*);..SQLITE_PRI
49210 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
49220 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43  BtreePutData(BtC
49230 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73  ursor*, u32 offs
49240 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69  et, u32 amt, voi
49250 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
49260 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
49270 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c  BtreeCacheOverfl
49280 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a  ow(BtCursor *);.
49290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
492a0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
492b0 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75  ClearCursor(BtCu
492c0 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 6e 64 65  rsor *);..#ifnde
492d0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54  f SQLITE_OMIT_BT
492e0 52 45 45 43 4f 55 4e 54 0a 53 51 4c 49 54 45 5f  REECOUNT.SQLITE_
492f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
49300 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74  te3BtreeCount(Bt
49310 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29  Cursor *, i64 *)
49320 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
49330 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
49340 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
49350 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
49360 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 2a  orInfo(BtCursor*
49370 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  , int*, int);.SQ
49380 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
49390 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  d sqlite3BtreeCu
493a0 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 2a 29  rsorList(Btree*)
493b0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
493c0 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 75 73  If we are not us
493d0 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 68 65  ing shared cache
493e0 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  , then there is 
493f0 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 75 73  no need to.** us
49400 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 63 63  e mutexes to acc
49410 65 73 73 20 74 68 65 20 42 74 53 68 61 72 65 64  ess the BtShared
49420 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 53 6f   structures.  So
49430 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 6e 74   make the.** Ent
49440 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 72 6f  er and Leave pro
49450 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 2e 0a  cedures no-ops..
49460 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
49470 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
49480 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c  ED_CACHE) && SQL
49490 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53  ITE_THREADSAFE.S
494a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
494b0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
494c0 65 45 6e 74 65 72 28 42 74 72 65 65 2a 29 3b 0a  eEnter(Btree*);.
494d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
494e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
494f0 65 65 4c 65 61 76 65 28 42 74 72 65 65 2a 29 3b  eeLeave(Btree*);
49500 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
49510 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
49520 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65  e is used inside
49530 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
49540 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 51  ents only. */.SQ
49550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
49560 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  nt sqlite3BtreeH
49570 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 2a  oldsMutex(Btree*
49580 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  );.#endif.SQLITE
49590 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
495a0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
495b0 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  rCursor(BtCursor
495c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
495d0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
495e0 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f  3BtreeLeaveCurso
495f0 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51  r(BtCursor*);.SQ
49600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
49610 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
49620 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33  EnterAll(sqlite3
49630 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
49640 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
49650 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73  3BtreeLeaveAll(s
49660 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65  qlite3*);.#ifnde
49670 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68  f NDEBUG.  /* Th
49680 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
49690 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ed inside assert
496a0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e  () statements on
496b0 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly. */.SQLITE_PR
496c0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
496d0 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
496e0 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a  Mutexes(sqlite3*
496f0 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  );.#endif.SQLITE
49700 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
49710 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
49720 78 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 65  xArrayEnter(Btre
49730 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53  eMutexArray*);.S
49740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
49750 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
49760 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
49770 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  (BtreeMutexArray
49780 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
49790 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
497a0 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  3BtreeMutexArray
497b0 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65  Insert(BtreeMute
497c0 78 41 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29  xArray*, Btree*)
497d0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
497e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
497f0 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  er(X).# define s
49800 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
49810 28 58 29 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  (X).#ifndef NDEB
49820 55 47 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75  UG.  /* This rou
49830 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73  tine is used ins
49840 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
49850 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f  tements only. */
49860 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
49870 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
49880 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 23 20 64  (X) 1.#endif.# d
49890 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
498a0 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29  eeEnterCursor(X)
498b0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
498c0 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f  3BtreeLeaveCurso
498d0 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71  r(X).# define sq
498e0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
498f0 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ll(X).# define s
49900 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
49910 41 6c 6c 28 58 29 0a 23 69 66 6e 64 65 66 20 4e  All(X).#ifndef N
49920 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 69 73 20  DEBUG.  /* This 
49930 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
49940 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
49950 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e  statements only.
49960 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 71 6c   */.# define sql
49970 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
49980 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23 65  lMutexes(X) 1.#e
49990 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 73 71  ndif.# define sq
499a0 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
499b0 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20 64  rrayEnter(X).# d
499c0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
499d0 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76  eeMutexArrayLeav
499e0 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71  e(X).# define sq
499f0 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
49a00 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29 0a  rrayInsert(X,Y).
49a10 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 20  #endif...#endif 
49a20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f 0a  /* _BTREE_H_ */.
49a30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
49a40 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 20   End of btree.h 
49a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
49a80 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
49a90 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
49aa0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
49ab0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
49ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
49ad0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
49ae0 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 20   Include vdbe.h 
49af0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
49b00 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
49b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
49b20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
49b30 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
49b40 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
49b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
49b70 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
49b80 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
49b90 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
49ba0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
49bb0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
49bc0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
49bd0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
49be0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
49bf0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
49c00 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
49c10 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
49c20 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
49c30 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
49c40 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
49c50 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
49c60 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
49c70 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
49c80 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
49c90 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
49ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49ce0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 65  *******.** Heade
49cf0 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 56  r file for the V
49d00 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 65 20  irtual DataBase 
49d10 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a 2a  Engine (VDBE).**
49d20 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
49d30 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  defines the inte
49d40 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 72  rface to the vir
49d50 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e  tual database en
49d60 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 2e  gine.** or VDBE.
49d70 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c 65    The VDBE imple
49d80 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61 63  ments an abstrac
49d90 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 72  t machine that r
49da0 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 20  uns a.** simple 
49db0 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 73  program to acces
49dc0 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65  s and modify the
49dd0 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61   underlying data
49de0 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  base..**.** $Id:
49df0 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 34 30 20   vdbe.h,v 1.140 
49e00 32 30 30 39 2f 30 32 2f 31 39 20 31 34 3a 33 39  2009/02/19 14:39
49e10 3a 32 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :25 danielk1977 
49e20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
49e30 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f   _SQLITE_VDBE_H_
49e40 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
49e50 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  _VDBE_H_../*.** 
49e60 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69 73  A single VDBE is
49e70 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63   an opaque struc
49e80 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62 65  ture named "Vdbe
49e90 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e 65  ".  Only routine
49ea0 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 72  s.** in the sour
49eb0 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56 64  ce file sqliteVd
49ec0 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 64  be.c are allowed
49ed0 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73 69   to see the insi
49ee0 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73  des.** of this s
49ef0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70  tructure..*/.typ
49f00 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
49f10 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   Vdbe;../*.** Th
49f20 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66  e names of the f
49f30 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64  ollowing types d
49f40 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49  eclared in vdbeI
49f50 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72 65  nt.h are require
49f60 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 62  d.** for the Vdb
49f70 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a  eOp definition..
49f80 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
49f90 74 20 56 64 62 65 46 75 6e 63 20 56 64 62 65 46  t VdbeFunc VdbeF
49fa0 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72  unc;.typedef str
49fb0 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 0a 2f 2a  uct Mem Mem;../*
49fc0 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73  .** A single ins
49fd0 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20  truction of the 
49fe0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
49ff0 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a  has an opcode.**
4a000 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20   and as many as 
4a010 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20  three operands. 
4a020 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e   The instruction
4a030 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a 20   is recorded.** 
4a040 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  as an instance o
4a050 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
4a060 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 74  structure:.*/.st
4a070 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 20  ruct VdbeOp {.  
4a080 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20  u8 opcode;      
4a090 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72      /* What oper
4a0a0 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d  ation to perform
4a0b0 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61   */.  signed cha
4a0c0 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e 65  r p4type; /* One
4a0d0 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20 63   of the P4_xxx c
4a0e0 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34 20  onstants for p4 
4a0f0 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 3b  */.  u8 opflags;
4a100 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20           /* Not 
4a110 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a  currently used *
4a120 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20 20  /.  u8 p5;      
4a130 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74 68          /* Fifth
4a140 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e   parameter is an
4a150 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63   unsigned charac
4a160 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b  ter */.  int p1;
4a170 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4a180 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f  First operand */
4a190 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20  .  int p2;      
4a1a0 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64         /* Second
4a1b0 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65   parameter (ofte
4a1c0 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69  n the jump desti
4a1d0 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e 74  nation) */.  int
4a1e0 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 20   p3;            
4a1f0 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70 61   /* The third pa
4a200 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e 69  rameter */.  uni
4a210 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  on {            
4a220 20 2f 2a 20 66 6f 72 74 68 20 70 61 72 61 6d 65   /* forth parame
4a230 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ter */.    int i
4a240 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4a250 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c    /* Integer val
4a260 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d 50 34  ue if p4type==P4
4a270 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76 6f  _INT32 */.    vo
4a280 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  id *p;          
4a290 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69 63 20       /* Generic 
4a2a0 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 63  pointer */.    c
4a2b0 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
4a2c0 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
4a2d0 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73 74 72   to data for str
4a2e0 69 6e 67 20 28 63 68 61 72 20 61 72 72 61 79 29  ing (char array)
4a2f0 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20 69 36   types */.    i6
4a300 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20 20 20  4 *pI64;        
4a310 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
4a320 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49  n p4type is P4_I
4a330 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75 62  NT64 */.    doub
4a340 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20  le *pReal;      
4a350 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
4a360 70 34 74 79 70 65 20 69 73 20 50 34 5f 52 45 41  p4type is P4_REA
4a370 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66  L */.    FuncDef
4a380 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20   *pFunc;        
4a390 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74  /* Used when p4t
4a3a0 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43 44 45  ype is P4_FUNCDE
4a3b0 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 75 6e  F */.    VdbeFun
4a3c0 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 20  c *pVdbeFunc;   
4a3d0 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74  /* Used when p4t
4a3e0 79 70 65 20 69 73 20 50 34 5f 56 44 42 45 46 55  ype is P4_VDBEFU
4a3f0 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65  NC */.    CollSe
4a400 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20  q *pColl;       
4a410 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
4a420 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c 53  type is P4_COLLS
4a430 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a 70  EQ */.    Mem *p
4a440 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
4a450 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
4a460 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d 20 2a  type is P4_MEM *
4a470 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74  /.    sqlite3_vt
4a480 61 62 20 2a 70 56 74 61 62 3b 20 20 20 2f 2a 20  ab *pVtab;   /* 
4a490 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65  Used when p4type
4a4a0 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a 20   is P4_VTAB */. 
4a4b0 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
4a4c0 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65  Info;     /* Use
4a4d0 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4a4e0 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a 20   P4_KEYINFO */. 
4a4f0 20 20 20 69 6e 74 20 2a 61 69 3b 20 20 20 20 20     int *ai;     
4a500 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
4a510 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4a520 20 50 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f 0a   P4_INTARRAY */.
4a530 20 20 7d 20 70 34 3b 0a 23 69 66 64 65 66 20 53    } p4;.#ifdef S
4a540 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 63 68  QLITE_DEBUG.  ch
4a550 61 72 20 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20  ar *zComment;   
4a560 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e         /* Commen
4a570 74 20 74 6f 20 69 6d 70 72 6f 76 65 20 72 65 61  t to improve rea
4a580 64 61 62 69 6c 69 74 79 20 2a 2f 0a 23 65 6e 64  dability */.#end
4a590 69 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50  if.#ifdef VDBE_P
4a5a0 52 4f 46 49 4c 45 0a 20 20 69 6e 74 20 63 6e 74  ROFILE.  int cnt
4a5b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4a5c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
4a5d0 69 6d 65 73 20 74 68 69 73 20 69 6e 73 74 72 75  imes this instru
4a5e0 63 74 69 6f 6e 20 77 61 73 20 65 78 65 63 75 74  ction was execut
4a5f0 65 64 20 2a 2f 0a 20 20 75 36 34 20 63 79 63 6c  ed */.  u64 cycl
4a600 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  es;             
4a610 20 2f 2a 20 54 6f 74 61 6c 20 74 69 6d 65 20 73   /* Total time s
4a620 70 65 6e 74 20 65 78 65 63 75 74 69 6e 67 20 74  pent executing t
4a630 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
4a640 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70  */.#endif.};.typ
4a650 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
4a660 4f 70 20 56 64 62 65 4f 70 3b 0a 0a 2f 2a 0a 2a  Op VdbeOp;../*.*
4a670 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 73  * A smaller vers
4a680 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 73  ion of VdbeOp us
4a690 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65 41  ed for the VdbeA
4a6a0 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 74  ddOpList() funct
4a6b0 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 69  ion because.** i
4a6c0 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73 20  t takes up less 
4a6d0 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  space..*/.struct
4a6e0 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20   VdbeOpList {.  
4a6f0 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20  u8 opcode;      
4a700 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72      /* What oper
4a710 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d  ation to perform
4a720 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61   */.  signed cha
4a730 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 72  r p1;     /* Fir
4a740 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  st operand */.  
4a750 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b 20  signed char p2; 
4a760 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61      /* Second pa
4a770 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74  rameter (often t
4a780 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74  he jump destinat
4a790 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 64  ion) */.  signed
4a7a0 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f 2a   char p3;     /*
4a7b0 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   Third parameter
4a7c0 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73   */.};.typedef s
4a7d0 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74  truct VdbeOpList
4a7e0 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a   VdbeOpList;../*
4a7f0 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
4a800 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 33 74  es of VdbeOp.p3t
4a810 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ype.*/.#define P
4a820 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20 20  4_NOTUSED    0  
4a830 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61 6d   /* The P4 param
4a840 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64  eter is not used
4a850 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 44   */.#define P4_D
4a860 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a  YNAMIC  (-1)  /*
4a870 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   Pointer to a st
4a880 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72  ring obtained fr
4a890 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
4a8a0 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  ) */.#define P4_
4a8b0 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20 2f  STATIC   (-2)  /
4a8c0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  * Pointer to a s
4a8d0 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a  tatic string */.
4a8e0 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53  #define P4_COLLS
4a8f0 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 20  EQ  (-4)  /* P4 
4a900 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
4a910 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74  a CollSeq struct
4a920 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
4a930 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29 20  4_FUNCDEF  (-5) 
4a940 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
4a950 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65 66  ter to a FuncDef
4a960 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
4a970 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f  efine P4_KEYINFO
4a980 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 73    (-6)  /* P4 is
4a990 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4a9a0 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
4a9b0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
4a9c0 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20 2f  VDBEFUNC (-7)  /
4a9d0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
4a9e0 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63 20  r to a VdbeFunc 
4a9f0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65  structure */.#de
4aa00 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 20  fine P4_MEM     
4aa10 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 20   (-8)  /* P4 is 
4aa20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d  a pointer to a M
4aa30 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72 65  em*    structure
4aa40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 54   */.#define P4_T
4aa50 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f 2a  RANSIENT (-9) /*
4aa60 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
4aa70 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20   to a transient 
4aa80 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
4aa90 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28 2d  e P4_VTAB     (-
4aaa0 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20 70  10) /* P4 is a p
4aab0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c  ointer to an sql
4aac0 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
4aad0 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
4aae0 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 29  4_MPRINTF  (-11)
4aaf0 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72 69   /* P4 is a stri
4ab00 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ng obtained from
4ab10 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
4ab20 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  () */.#define P4
4ab30 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29 20  _REAL     (-12) 
4ab40 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 69  /* P4 is a 64-bi
4ab50 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  t floating point
4ab60 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e   value */.#defin
4ab70 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28 2d  e P4_INT64    (-
4ab80 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36  13) /* P4 is a 6
4ab90 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
4aba0 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  eger */.#define 
4abb0 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 34  P4_INT32    (-14
4abc0 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32 2d  ) /* P4 is a 32-
4abd0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
4abe0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  er */.#define P4
4abf0 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29 20  _INTARRAY (-15) 
4ac00 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74 6f  /* P4 is a vecto
4ac10 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 65  r of 32-bit inte
4ac20 67 65 72 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e  gers */../* When
4ac30 20 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67   adding a P4 arg
4ac40 75 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b  ument using P4_K
4ac50 45 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f  EYINFO, a copy o
4ac60 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  f the KeyInfo st
4ac70 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61  ructure.** is ma
4ac80 64 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69  de.  That copy i
4ac90 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65  s freed when the
4aca0 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a   Vdbe is finaliz
4acb0 65 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a  ed.  But if the.
4acc0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50  ** argument is P
4acd0 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
4ace0 46 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e  F, the passed in
4acf0 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64   pointer is used
4ad00 2e 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67  .  It still.** g
4ad10 65 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74  ets freed when t
4ad20 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c  he Vdbe is final
4ad30 69 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c  ized so it still
4ad40 20 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69   should be obtai
4ad50 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69  ned.** from a si
4ad60 6e 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  ngle sqliteMallo
4ad70 63 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70  c().  But no cop
4ad80 79 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68  y is made and th
4ad90 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e  e calling.** fun
4ada0 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f  ction should *no
4adb0 74 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74  t* try to free t
4adc0 68 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23  he KeyInfo..*/.#
4add0 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
4ade0 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a  O_HANDOFF (-16).
4adf0 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e  #define P4_KEYIN
4ae00 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29  FO_STATIC  (-17)
4ae10 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65  ../*.** The Vdbe
4ae20 2e 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20  .aColName array 
4ae30 63 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20  contains 5n Mem 
4ae40 73 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 72  structures, wher
4ae50 65 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e  e n is the .** n
4ae60 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
4ae70 20 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e 65   of data returne
4ae80 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  d by the stateme
4ae90 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43  nt..*/.#define C
4aea0 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20  OLNAME_NAME     
4aeb0 30 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  0.#define COLNAM
4aec0 45 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65  E_DECLTYPE 1.#de
4aed0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54  fine COLNAME_DAT
4aee0 41 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20  ABASE 2.#define 
4aef0 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20  COLNAME_TABLE   
4af00 20 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41   3.#define COLNA
4af10 4d 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69  ME_COLUMN   4.#i
4af20 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
4af30 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
4af40 54 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e  TA.# define COLN
4af50 41 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20  AME_N        5  
4af60 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
4af70 20 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d   COLNAME_xxx sym
4af80 62 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20  bols */.#else.# 
4af90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
4afa0 54 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64  T_DECLTYPE.#   d
4afb0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
4afc0 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53       1      /* S
4afd0 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61  tore only the na
4afe0 6d 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20  me */.# else.#  
4aff0 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f   define COLNAME_
4b000 4e 20 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a  N      2      /*
4b010 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20   Store the name 
4b020 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a  and decltype */.
4b030 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  # endif.#endif..
4b040 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
4b050 69 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72  ing macro conver
4b060 74 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 64  ts a relative ad
4b070 64 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 20  dress in the p2 
4b080 66 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64  field.** of a Vd
4b090 62 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 69  beOp structure i
4b0a0 6e 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e  nto a negative n
4b0b0 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a  umber so that .*
4b0c0 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  * sqlite3VdbeAdd
4b0d0 4f 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74  OpList() knows t
4b0e0 68 61 74 20 74 68 65 20 61 64 64 72 65 73 73 20  hat the address 
4b0f0 69 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61  is relative.  Ca
4b100 6c 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63  lling.** the mac
4b110 72 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65  ro again restore
4b120 73 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a  s the address..*
4b130 2f 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 58  /.#define ADDR(X
4b140 29 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a  )  (-1-(X))../*.
4b150 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20  ** The makefile 
4b160 73 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63  scans the vdbe.c
4b170 20 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64   source file and
4b180 20 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70   creates the "op
4b190 63 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64  codes.h".** head
4b1a0 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66  er file that def
4b1b0 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f  ines a number fo
4b1c0 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73  r each opcode us
4b1d0 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a  ed by the VDBE..
4b1e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4b1f0 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64  ** Include opcod
4b200 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  es.h in the midd
4b210 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a  le of vdbe.h ***
4b220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b230 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4b240 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70  ** Begin file op
4b250 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  codes.h ********
4b260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b280 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61  */./* Automatica
4b290 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20  lly generated.  
4b2a0 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f  Do not edit */./
4b2b0 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f  * See the mkopco
4b2c0 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66  deh.awk script f
4b2d0 6f 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64  or details */.#d
4b2e0 65 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20  efine OP_VNext  
4b2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
4b310 23 64 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e  #define OP_Affin
4b320 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20 20  ity             
4b330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b340 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c  2.#define OP_Col
4b350 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  umn             
4b360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b370 20 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53    3.#define OP_S
4b380 65 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20  etCookie        
4b390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b3a0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50      4.#define OP
4b3b0 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20 20 20  _Seek           
4b3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b3d0 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20        5.#define 
4b3e0 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20  OP_Real         
4b3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b400 20 20 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73        130   /* s
4b410 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20  ame as TK_FLOAT 
4b420 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4b430 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20 20 20  _Sequence       
4b440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b450 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
4b460 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20  OP_Savepoint    
4b470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b480 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e          7.#defin
4b490 65 20 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20  e OP_Ge         
4b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b4b0 20 20 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a           78   /*
4b4c0 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20   same as TK_GE  
4b4d0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4b4e0 4f 50 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20  OP_RowKey       
4b4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b500 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e          8.#defin
4b510 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20  e OP_SCopy      
4b520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b530 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66            9.#def
4b540 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20  ine OP_Eq       
4b550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b560 20 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20             74   
4b570 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51  /* same as TK_EQ
4b580 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
4b590 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20  e OP_OpenWrite  
4b5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b5b0 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66           10.#def
4b5c0 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20  ine OP_NotNull  
4b5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b5e0 20 20 20 20 20 20 20 20 20 20 20 37 32 20 20 20             72   
4b5f0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f  /* same as TK_NO
4b600 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e  TNULL  */.#defin
4b610 65 20 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20  e OP_If         
4b620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b630 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66           11.#def
4b640 69 6e 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20  ine OP_ToInt    
4b650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b660 20 20 20 20 20 20 20 20 20 20 31 34 34 20 20 20            144   
4b670 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
4b680 5f 49 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e  _INT   */.#defin
4b690 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20  e OP_String8    
4b6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b6b0 20 20 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a           94   /*
4b6c0 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49   same as TK_STRI
4b6d0 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NG   */.#define 
4b6e0 4f 50 5f 56 52 6f 77 69 64 20 20 20 20 20 20 20  OP_VRowid       
4b6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b700 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e         12.#defin
4b710 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 20  e OP_CollSeq    
4b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b730 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66           13.#def
4b740 69 6e 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20  ine OP_OpenRead 
4b750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b760 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 64             14.#d
4b770 65 66 69 6e 65 20 4f 50 5f 45 78 70 69 72 65 20  efine OP_Expire 
4b780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b790 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a               15.
4b7a0 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 74 6f 43  #define OP_AutoC
4b7b0 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 20  ommit           
4b7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4b7d0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 74 20  6.#define OP_Gt 
4b7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b800 20 37 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   75   /* same as
4b810 20 54 4b 5f 47 54 20 20 20 20 20 20 20 2a 2f 0a   TK_GT       */.
4b820 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 63  #define OP_Pagec
4b830 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  ount            
4b840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4b850 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74  7.#define OP_Int
4b860 65 67 72 69 74 79 43 6b 20 20 20 20 20 20 20 20  egrityCk        
4b870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b880 20 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   18.#define OP_S
4b890 6f 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ort             
4b8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b8b0 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50     20.#define OP
4b8c0 5f 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 20  _Copy           
4b8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b8e0 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20       21.#define 
4b8f0 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20 20 20  OP_Trace        
4b900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b910 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e         22.#defin
4b920 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20  e OP_Function   
4b930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b940 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66           23.#def
4b950 69 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20 20 20  ine OP_IfNeg    
4b960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b970 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64             24.#d
4b980 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20  efine OP_And    
4b990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 37 20               67 
4b9b0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4b9c0 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  AND      */.#def
4b9d0 69 6e 65 20 4f 50 5f 53 75 62 74 72 61 63 74 20  ine OP_Subtract 
4b9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b9f0 20 20 20 20 20 20 20 20 20 20 20 38 35 20 20 20             85   
4ba00 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49  /* same as TK_MI
4ba10 4e 55 53 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  NUS    */.#defin
4ba20 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20 20 20 20  e OP_Noop       
4ba30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ba40 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66           25.#def
4ba50 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20  ine OP_Return   
4ba60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ba70 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23 64             26.#d
4ba80 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64  efine OP_Remaind
4ba90 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
4baa0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 20               88 
4bab0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4bac0 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  REM      */.#def
4bad0 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20  ine OP_NewRowid 
4bae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4baf0 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64             27.#d
4bb00 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c  efine OP_Multipl
4bb10 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4bb20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36 20               86 
4bb30 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4bb40 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66  STAR     */.#def
4bb50 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20  ine OP_Variable 
4bb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bb70 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23 64             28.#d
4bb80 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20  efine OP_String 
4bb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bba0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 39 0a               29.
4bbb0 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41  #define OP_RealA
4bbc0 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20  ffinity         
4bbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
4bbe0 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65  0.#define OP_VRe
4bbf0 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
4bc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bc10 20 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50   31.#define OP_P
4bc20 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20  arseSchema      
4bc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bc40 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50     32.#define OP
4bc50 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20  _VOpen          
4bc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bc70 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20       33.#define 
4bc80 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20  OP_Close        
4bc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bca0 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e         34.#defin
4bcb0 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78  e OP_CreateIndex
4bcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bcd0 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66           35.#def
4bce0 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20  ine OP_IsUnique 
4bcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bd00 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 64             36.#d
4bd10 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e  efine OP_NotFoun
4bd20 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
4bd30 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 0a               37.
4bd40 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34  #define OP_Int64
4bd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
4bd70 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73  8.#define OP_Mus
4bd80 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 20  tBeInt          
4bd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bda0 20 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48   39.#define OP_H
4bdb0 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  alt             
4bdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bdd0 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50     40.#define OP
4bde0 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  _Rowid          
4bdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4be00 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20       41.#define 
4be10 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20  OP_IdxLT        
4be20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4be30 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e         42.#defin
4be40 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20  e OP_AddImm     
4be50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4be60 20 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 66           43.#def
4be70 69 6e 65 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74  ine OP_Statement
4be80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4be90 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23 64             44.#d
4bea0 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61  efine OP_RowData
4beb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35 0a               45.
4bed0 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61  #define OP_MemMa
4bee0 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
4bef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
4bf00 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 72 20  6.#define OP_Or 
4bf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bf30 20 36 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   66   /* same as
4bf40 20 54 4b 5f 4f 52 20 20 20 20 20 20 20 2a 2f 0a   TK_OR       */.
4bf50 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 78  #define OP_NotEx
4bf60 69 73 74 73 20 20 20 20 20 20 20 20 20 20 20 20  ists            
4bf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
4bf80 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73  7.#define OP_Gos
4bf90 75 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ub              
4bfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bfb0 20 34 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44   48.#define OP_D
4bfc0 69 76 69 64 65 20 20 20 20 20 20 20 20 20 20 20  ivide           
4bfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bfe0 20 20 20 38 37 20 20 20 2f 2a 20 73 61 6d 65 20     87   /* same 
4bff0 61 73 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a  as TK_SLASH    *
4c000 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74  /.#define OP_Int
4c010 65 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20  eger            
4c020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c030 20 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54   49.#define OP_T
4c040 6f 4e 75 6d 65 72 69 63 20 20 20 20 20 20 20 20  oNumeric        
4c050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c060 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20    143   /* same 
4c070 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43  as TK_TO_NUMERIC
4c080 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72  */.#define OP_Pr
4c090 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ev              
4c0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c0b0 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f    50.#define OP_
4c0c0 52 6f 77 53 65 74 52 65 61 64 20 20 20 20 20 20  RowSetRead      
4c0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c0e0 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 4f      51.#define O
4c0f0 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20  P_Concat        
4c100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c110 20 20 20 20 20 20 38 39 20 20 20 2f 2a 20 73 61        89   /* sa
4c120 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20  me as TK_CONCAT 
4c130 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4c140 52 6f 77 53 65 74 41 64 64 20 20 20 20 20 20 20  RowSetAdd       
4c150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c160 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f      52.#define O
4c170 50 5f 42 69 74 41 6e 64 20 20 20 20 20 20 20 20  P_BitAnd        
4c180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c190 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61        80   /* sa
4c1a0 6d 65 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 20  me as TK_BITAND 
4c1b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4c1c0 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 20  VColumn         
4c1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c1e0 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f      53.#define O
4c1f0 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 20 20  P_CreateTable   
4c200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c210 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65        54.#define
4c220 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 20 20 20   OP_Last        
4c230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c240 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69          55.#defi
4c250 6e 65 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20  ne OP_SeekLe    
4c260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c270 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65            56.#de
4c280 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20  fine OP_IsNull  
4c290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c2a0 20 20 20 20 20 20 20 20 20 20 20 20 37 31 20 20              71  
4c2b0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49   /* same as TK_I
4c2c0 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69  SNULL   */.#defi
4c2d0 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d  ne OP_IncrVacuum
4c2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c2f0 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 64 65            57.#de
4c300 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64  fine OP_IdxRowid
4c310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c320 20 20 20 20 20 20 20 20 20 20 20 20 35 38 0a 23              58.#
4c330 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 52  define OP_ShiftR
4c340 69 67 68 74 20 20 20 20 20 20 20 20 20 20 20 20  ight            
4c350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33                83
4c360 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4c370 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65  _RSHIFT   */.#de
4c380 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f 75  fine OP_ResetCou
4c390 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
4c3a0 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23              59.#
4c3b0 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 74 65 78  define OP_Contex
4c3c0 74 50 75 73 68 20 20 20 20 20 20 20 20 20 20 20  tPush           
4c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30                60
4c3e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59 69 65 6c  .#define OP_Yiel
4c3f0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
4c400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c410 36 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72  61.#define OP_Dr
4c420 6f 70 54 72 69 67 67 65 72 20 20 20 20 20 20 20  opTrigger       
4c430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c440 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    62.#define OP_
4c450 44 72 6f 70 49 6e 64 65 78 20 20 20 20 20 20 20  DropIndex       
4c460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c470 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f      63.#define O
4c480 50 5f 49 64 78 47 45 20 20 20 20 20 20 20 20 20  P_IdxGE         
4c490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c4a0 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65        64.#define
4c4b0 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20   OP_IdxDelete   
4c4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c4d0 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66 69          65.#defi
4c4e0 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 20  ne OP_Vacuum    
4c4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c500 20 20 20 20 20 20 20 20 20 20 36 38 0a 23 64 65            68.#de
4c510 66 69 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20  fine OP_IfNot   
4c520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c530 20 20 20 20 20 20 20 20 20 20 20 20 36 39 0a 23              69.#
4c540 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61  define OP_DropTa
4c550 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ble             
4c560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30                70
4c570 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b  .#define OP_Seek
4c580 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Lt              
4c590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c5a0 37 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 61  79.#define OP_Ma
4c5b0 6b 65 52 65 63 6f 72 64 20 20 20 20 20 20 20 20  keRecord        
4c5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c5d0 20 20 39 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f    90.#define OP_
4c5e0 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20  ToBlob          
4c5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c600 20 20 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65     142   /* same
4c610 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20   as TK_TO_BLOB  
4c620 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65  */.#define OP_Re
4c630 73 75 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20  sultRow         
4c640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c650 20 20 39 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    91.#define OP_
4c660 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20  Delete          
4c670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c680 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f      92.#define O
4c690 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20  P_AggFinal      
4c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c6b0 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65        95.#define
4c6c0 20 4f 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20   OP_Compare     
4c6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c6e0 20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 69          96.#defi
4c6f0 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20  ne OP_ShiftLeft 
4c700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c710 20 20 20 20 20 20 20 20 20 20 38 32 20 20 20 2f            82   /
4c720 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48  * same as TK_LSH
4c730 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  IFT   */.#define
4c740 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20   OP_Goto        
4c750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c760 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66 69          97.#defi
4c770 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20  ne OP_TableLock 
4c780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c790 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 65            98.#de
4c7a0 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20  fine OP_Clear   
4c7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c7c0 20 20 20 20 20 20 20 20 20 20 20 20 39 39 0a 23              99.#
4c7d0 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20  define OP_Le    
4c7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36                76
4c800 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4c810 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _LE       */.#de
4c820 66 69 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 6f  fine OP_VerifyCo
4c830 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20  okie            
4c840 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23             100.#
4c850 64 65 66 69 6e 65 20 4f 50 5f 41 67 67 53 74 65  define OP_AggSte
4c860 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
4c870 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 31               101
4c880 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65  .#define OP_ToTe
4c890 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xt              
4c8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4c8b0 34 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  41   /* same as 
4c8c0 54 4b 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23  TK_TO_TEXT  */.#
4c8d0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20  define OP_Not   
4c8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39                19
4c900 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4c910 5f 4e 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65  _NOT      */.#de
4c920 66 69 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20  fine OP_ToReal  
4c930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c940 20 20 20 20 20 20 20 20 20 20 20 31 34 35 20 20             145  
4c950 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
4c960 4f 5f 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69  O_REAL  */.#defi
4c970 6e 65 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75  ne OP_SetNumColu
4c980 6d 6e 73 20 20 20 20 20 20 20 20 20 20 20 20 20  mns             
4c990 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65           102.#de
4c9a0 66 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61 63 74  fine OP_Transact
4c9b0 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
4c9c0 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23             103.#
4c9d0 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 65  define OP_VFilte
4c9e0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
4c9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34               104
4ca00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20  .#define OP_Ne  
4ca10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ca20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ca30 37 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  73   /* same as 
4ca40 54 4b 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23  TK_NE       */.#
4ca50 64 65 66 69 6e 65 20 4f 50 5f 56 44 65 73 74 72  define OP_VDestr
4ca60 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  oy              
4ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35               105
4ca80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 74  .#define OP_Cont
4ca90 65 78 74 50 6f 70 20 20 20 20 20 20 20 20 20 20  extPop          
4caa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4cab0 30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69  06.#define OP_Bi
4cac0 74 4f 72 20 20 20 20 20 20 20 20 20 20 20 20 20  tOr             
4cad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cae0 20 20 38 31 20 20 20 2f 2a 20 73 61 6d 65 20 61    81   /* same a
4caf0 73 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 2a 2f  s TK_BITOR    */
4cb00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74  .#define OP_Next
4cb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4cb30 30 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  07.#define OP_Co
4cb40 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  unt             
4cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb60 20 31 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   108.#define OP_
4cb70 49 64 78 49 6e 73 65 72 74 20 20 20 20 20 20 20  IdxInsert       
4cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb90 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20 4f     109.#define O
4cba0 50 5f 4c 74 20 20 20 20 20 20 20 20 20 20 20 20  P_Lt            
4cbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cbc0 20 20 20 20 20 20 37 37 20 20 20 2f 2a 20 73 61        77   /* sa
4cbd0 6d 65 20 61 73 20 54 4b 5f 4c 54 20 20 20 20 20  me as TK_LT     
4cbe0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4cbf0 53 65 65 6b 47 65 20 20 20 20 20 20 20 20 20 20  SeekGe          
4cc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cc10 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f     110.#define O
4cc20 50 5f 49 6e 73 65 72 74 20 20 20 20 20 20 20 20  P_Insert        
4cc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cc40 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65       111.#define
4cc50 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20 20   OP_Destroy     
4cc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cc70 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66 69         112.#defi
4cc80 6e 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  ne OP_ReadCookie
4cc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cca0 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64 65           113.#de
4ccb0 66 69 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c  fine OP_LoadAnal
4ccc0 79 73 69 73 20 20 20 20 20 20 20 20 20 20 20 20  ysis            
4ccd0 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23             114.#
4cce0 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69  define OP_Explai
4ccf0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
4cd00 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 35               115
4cd10 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74  .#define OP_Halt
4cd20 49 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  IfNull          
4cd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4cd40 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70  16.#define OP_Op
4cd50 65 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20 20  enPseudo        
4cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cd70 20 31 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f   117.#define OP_
4cd80 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20  OpenEphemeral   
4cd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cda0 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f     118.#define O
4cdb0 50 5f 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  P_Null          
4cdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cdd0 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65       119.#define
4cde0 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20   OP_Move        
4cdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce00 20 20 20 20 20 20 20 31 32 30 0a 23 64 65 66 69         120.#defi
4ce10 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20  ne OP_Blob      
4ce20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce30 20 20 20 20 20 20 20 20 20 31 32 31 0a 23 64 65           121.#de
4ce40 66 69 6e 65 20 4f 50 5f 41 64 64 20 20 20 20 20  fine OP_Add     
4ce50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce60 20 20 20 20 20 20 20 20 20 20 20 20 38 34 20 20              84  
4ce70 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50   /* same as TK_P
4ce80 4c 55 53 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  LUS     */.#defi
4ce90 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20  ne OP_Rewind    
4cea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ceb0 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65           122.#de
4cec0 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20  fine OP_SeekGt  
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cee0 20 20 20 20 20 20 20 20 20 20 20 31 32 33 0a 23             123.#
4cef0 64 65 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e  define OP_VBegin
4cf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cf10 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 34               124
4cf20 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64  .#define OP_VUpd
4cf30 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ate             
4cf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4cf50 32 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66  25.#define OP_If
4cf60 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20  Zero            
4cf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cf80 20 31 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f   126.#define OP_
4cf90 42 69 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20  BitNot          
4cfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cfb0 20 20 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65      93   /* same
4cfc0 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20   as TK_BITNOT   
4cfd0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43  */.#define OP_VC
4cfe0 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20  reate           
4cff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d000 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f   127.#define OP_
4d010 46 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20 20  Found           
4d020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d030 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f     128.#define O
4d040 50 5f 49 66 50 6f 73 20 20 20 20 20 20 20 20 20  P_IfPos         
4d050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d060 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65       129.#define
4d070 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20   OP_NullRow     
4d080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d090 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69         131.#defi
4d0a0 6e 65 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20  ne OP_Jump      
4d0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d0c0 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65           132.#de
4d0d0 66 69 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74  fine OP_Permutat
4d0e0 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
4d0f0 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 0a             133..
4d100 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
4d110 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20 61   opcode values a
4d120 72 65 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f  re never used */
4d130 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
4d140 73 65 64 5f 31 33 34 20 20 20 20 20 20 20 20 20  sed_134         
4d150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4d160 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  34.#define OP_No
4d170 74 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 20  tUsed_135       
4d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d190 20 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f   135.#define OP_
4d1a0 4e 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 20  NotUsed_136     
4d1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d1c0 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f     136.#define O
4d1d0 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20  P_NotUsed_137   
4d1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d1f0 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65       137.#define
4d200 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20   OP_NotUsed_138 
4d210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d220 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69         138.#defi
4d230 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33  ne OP_NotUsed_13
4d240 39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  9               
4d250 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65           139.#de
4d260 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
4d270 31 34 30 20 20 20 20 20 20 20 20 20 20 20 20 20  140             
4d280 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a             140..
4d290 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73  ./* Properties s
4d2a0 75 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72  uch as "out2" or
4d2b0 20 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65   "jump" that are
4d2c0 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a   specified in.**
4d2d0 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77   comments follow
4d2e0 69 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 66  ing the "case" f
4d2f0 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69  or each opcode i
4d300 6e 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20  n the vdbe.c.** 
4d310 61 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f  are encoded into
4d320 20 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 66   bitvectors as f
4d330 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69  ollows:.*/.#defi
4d340 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20  ne OPFLG_JUMP   
4d350 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20           0x0001 
4d360 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f   /* jump:  P2 ho
4d370 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a  lds jmp target *
4d380 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
4d390 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20  OUT2_PRERELEASE 
4d3a0 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d  0x0002  /* out2-
4d3b0 70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23  prerelease: */.#
4d3c0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31  define OPFLG_IN1
4d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
4d3e0 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50  004  /* in1:   P
4d3f0 31 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f  1 is an input */
4d400 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49  .#define OPFLG_I
4d410 4e 32 20 20 20 20 20 20 20 20 20 20 20 20 20 30  N2             0
4d420 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20  x0008  /* in2:  
4d430 20 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20   P2 is an input 
4d440 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47  */.#define OPFLG
4d450 5f 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 20  _IN3            
4d460 20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a   0x0010  /* in3:
4d470 20 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75     P3 is an inpu
4d480 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  t */.#define OPF
4d490 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20  LG_OUT3         
4d4a0 20 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75     0x0020  /* ou
4d4b0 74 33 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75  t3:  P3 is an ou
4d4c0 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tput */.#define 
4d4d0 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45  OPFLG_INITIALIZE
4d4e0 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30  R {\./*   0 */ 0
4d4f0 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x00, 0x01, 0x00,
4d500 20 30 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30   0x00, 0x10, 0x0
4d510 38 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a  8, 0x02, 0x00,\.
4d520 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20  /*   8 */ 0x00, 
4d530 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35  0x04, 0x00, 0x05
4d540 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78  , 0x02, 0x00, 0x
4d550 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31  00, 0x00,\./*  1
4d560 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 2c  6 */ 0x00, 0x02,
4d570 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30   0x00, 0x04, 0x0
4d580 31 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30  1, 0x04, 0x00, 0
4d590 78 30 30 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20  x00,\./*  24 */ 
4d5a0 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 34  0x05, 0x00, 0x04
4d5b0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78  , 0x02, 0x00, 0x
4d5c0 30 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 5c  02, 0x04, 0x00,\
4d5d0 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c  ./*  32 */ 0x00,
4d5e0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
4d5f0 32 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30  2, 0x11, 0x11, 0
4d600 78 30 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20  x02, 0x05,\./*  
4d610 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32  40 */ 0x00, 0x02
4d620 2c 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78  , 0x11, 0x04, 0x
4d630 30 30 2c 20 30 78 30 30 2c 20 30 78 30 63 2c 20  00, 0x00, 0x0c, 
4d640 30 78 31 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f  0x11,\./*  48 */
4d650 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30   0x01, 0x02, 0x0
4d660 31 2c 20 30 78 32 31 2c 20 30 78 30 38 2c 20 30  1, 0x21, 0x08, 0
4d670 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c  x00, 0x02, 0x01,
4d680 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 78 31 31  \./*  56 */ 0x11
4d690 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
4d6a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20  00, 0x00, 0x04, 
4d6b0 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20  0x00, 0x00,\./* 
4d6c0 20 36 34 20 2a 2f 20 30 78 31 31 2c 20 30 78 30   64 */ 0x11, 0x0
4d6d0 30 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  0, 0x2c, 0x2c, 0
4d6e0 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 30 2c  x00, 0x05, 0x00,
4d6f0 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 37 32 20 2a   0x05,\./*  72 *
4d700 2f 20 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78  / 0x05, 0x15, 0x
4d710 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20  15, 0x15, 0x15, 
4d720 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 31  0x15, 0x15, 0x11
4d730 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 32  ,\./*  80 */ 0x2
4d740 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  c, 0x2c, 0x2c, 0
4d750 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c  x2c, 0x2c, 0x2c,
4d760 20 30 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a   0x2c, 0x2c,\./*
4d770 20 20 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78    88 */ 0x2c, 0x
4d780 32 63 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  2c, 0x00, 0x00, 
4d790 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32  0x00, 0x04, 0x02
4d7a0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20  , 0x00,\./*  96 
4d7b0 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  */ 0x00, 0x01, 0
4d7c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
4d7d0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
4d7e0 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78  0,\./* 104 */ 0x
4d7f0 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  01, 0x00, 0x00, 
4d800 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 38  0x01, 0x02, 0x08
4d810 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 5c 0a 2f  , 0x11, 0x00,\./
4d820 2a 20 31 31 32 20 2a 2f 20 30 78 30 32 2c 20 30  * 112 */ 0x02, 0
4d830 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x02, 0x00, 0x00,
4d840 20 30 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30   0x10, 0x00, 0x0
4d850 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30  0, 0x02,\./* 120
4d860 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20   */ 0x00, 0x02, 
4d870 30 78 30 31 2c 20 30 78 31 31 2c 20 30 78 30 30  0x01, 0x11, 0x00
4d880 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78  , 0x00, 0x05, 0x
4d890 30 30 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30  00,\./* 128 */ 0
4d8a0 78 31 31 2c 20 30 78 30 35 2c 20 30 78 30 32 2c  x11, 0x05, 0x02,
4d8b0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
4d8c0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a  0, 0x00, 0x00,\.
4d8d0 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20  /* 136 */ 0x00, 
4d8e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
4d8f0 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78  , 0x00, 0x04, 0x
4d900 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34  04, 0x04,\./* 14
4d910 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c  4 */ 0x04, 0x04,
4d920 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
4d930 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65  ** End of opcode
4d940 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.h ************
4d950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4d960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4d970 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4d980 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
4d990 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
4d9a0 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a  in vdbe.h ******
4d9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4d9c0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74  */../*.** Protot
4d9d0 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42  ypes for the VDB
4d9e0 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65  E interface.  Se
4d9f0 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68  e comments on th
4da00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
4da10 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69  .** for a descri
4da20 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61  ption of what ea
4da30 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ch of these rout
4da40 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51  ines does..*/.SQ
4da50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62  LITE_PRIVATE Vdb
4da60 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72  e *sqlite3VdbeCr
4da70 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a  eate(sqlite3*);.
4da80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4da90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
4daa0 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b  dOp0(Vdbe*,int);
4dab0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4dac0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
4dad0 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c  ddOp1(Vdbe*,int,
4dae0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4daf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4db00 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a  VdbeAddOp2(Vdbe*
4db10 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
4db20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4db30 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
4db40 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op3(Vdbe*,int,in
4db50 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  t,int,int);.SQLI
4db60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4db70 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
4db80 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69  (Vdbe*,int,int,i
4db90 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  nt,int,const cha
4dba0 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c  r *zP4,int);.SQL
4dbb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4dbc0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
4dbd0 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20  List(Vdbe*, int 
4dbe0 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20  nOp, VdbeOpList 
4dbf0 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c  const *aOp);.SQL
4dc00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4dc10 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
4dc20 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20  geP1(Vdbe*, int 
4dc30 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53  addr, int P1);.S
4dc40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4dc50 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
4dc60 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e  angeP2(Vdbe*, in
4dc70 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b  t addr, int P2);
4dc80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4dc90 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
4dca0 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20  ChangeP3(Vdbe*, 
4dcb0 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33  int addr, int P3
4dcc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4dcd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
4dce0 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a  beChangeP5(Vdbe*
4dcf0 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45  , u8 P5);.SQLITE
4dd00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4dd10 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
4dd20 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64  e(Vdbe*, int add
4dd30 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
4dd40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
4dd50 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
4dd60 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c  Vdbe*, int addr,
4dd70 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
4dd80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4dd90 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
4dda0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
4ddb0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50  , const char *zP
4ddc0 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54  4, int N);.SQLIT
4ddd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4dde0 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
4ddf0 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b  ree(Vdbe*, int);
4de00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4de10 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56  VdbeOp *sqlite3V
4de20 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20  dbeGetOp(Vdbe*, 
4de30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4de40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4de50 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64  VdbeMakeLabel(Vd
4de60 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
4de70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4de80 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65  3VdbeDelete(Vdbe
4de90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4dea0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
4deb0 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62  dbeMakeReady(Vdb
4dec0 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69  e*,int,int,int,i
4ded0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
4dee0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
4def0 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65  dbeFinalize(Vdbe
4df00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4df10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
4df20 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
4df30 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  Vdbe*, int);.SQL
4df40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4df50 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
4df60 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23  ntAddr(Vdbe*);.#
4df70 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
4df80 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
4df90 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
4dfa0 56 64 62 65 54 72 61 63 65 28 56 64 62 65 2a 2c  VdbeTrace(Vdbe*,
4dfb0 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a 53  FILE*);.#endif.S
4dfc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4dfd0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  id sqlite3VdbeRe
4dfe0 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 64  setStepResult(Vd
4dff0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
4e000 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4e010 56 64 62 65 52 65 73 65 74 28 56 64 62 65 2a 29  VdbeReset(Vdbe*)
4e020 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4e030 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
4e040 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65  eSetNumCols(Vdbe
4e050 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
4e060 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4e070 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
4e080 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  (Vdbe*, int, int
4e090 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
4e0a0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
4e0b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4e0c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
4e0d0 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62  CountChanges(Vdb
4e0e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
4e0f0 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  ATE sqlite3 *sql
4e100 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65 2a  ite3VdbeDb(Vdbe*
4e110 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4e120 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
4e130 62 65 53 65 74 53 71 6c 28 56 64 62 65 2a 2c 20  beSetSql(Vdbe*, 
4e140 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
4e150 6e 74 20 6e 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  nt n, int);.SQLI
4e160 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4e170 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 28  sqlite3VdbeSwap(
4e180 56 64 62 65 2a 2c 56 64 62 65 2a 29 3b 0a 0a 23  Vdbe*,Vdbe*);..#
4e190 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
4e1a0 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
4e1b0 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49  EMENT.SQLITE_PRI
4e1c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4e1d0 56 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72  VdbeReleaseMemor
4e1e0 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 53  y(int);.#endif.S
4e1f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e  QLITE_PRIVATE Un
4e200 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71  packedRecord *sq
4e210 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55  lite3VdbeRecordU
4e220 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69  npack(KeyInfo*,i
4e230 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 0a  nt,const void*,.
4e240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4e260 20 20 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64          Unpacked
4e270 52 65 63 6f 72 64 2a 2c 69 6e 74 29 3b 0a 53 51  Record*,int);.SQ
4e280 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4e290 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
4e2a0 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
4e2b0 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  d(UnpackedRecord
4e2c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4e2d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
4e2e0 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
4e2f0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
4e300 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29  UnpackedRecord*)
4e310 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  ;...#ifndef NDEB
4e320 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
4e330 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
4e340 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65  VdbeComment(Vdbe
4e350 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
4e360 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56  ...);.# define V
4e370 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73  dbeComment(X)  s
4e380 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e  qlite3VdbeCommen
4e390 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t X.SQLITE_PRIVA
4e3a0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
4e3b0 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74  3VdbeNoopComment
4e3c0 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68  (Vdbe*, const ch
4e3d0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66  ar*, ...);.# def
4e3e0 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ine VdbeNoopComm
4e3f0 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 33 56  ent(X)  sqlite3V
4e400 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58  dbeNoopComment X
4e410 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
4e420 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23  VdbeComment(X).#
4e430 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70   define VdbeNoop
4e440 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69  Comment(X).#endi
4e450 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  f..#endif../****
4e460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
4e470 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.h *******
4e480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
4e4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
4e4c0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
4e4d0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
4e4e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
4e4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
4e500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
4e510 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 74 68  de pager.h in th
4e520 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
4e530 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
4e540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
4e550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
4e560 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a   file pager.h **
4e570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
4e5a0 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
4e5b0 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
4e5c0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
4e5d0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
4e5e0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
4e5f0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
4e600 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
4e610 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
4e620 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
4e630 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
4e640 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
4e650 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
4e660 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
4e670 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
4e680 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
4e690 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
4e6a0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
4e6b0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
4e6c0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
4e6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e710 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
4e720 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68   file defines th
4e730 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74  e interface that
4e740 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 67 65   the sqlite page
4e750 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73   cache.** subsys
4e760 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 20 63  tem.  The page c
4e770 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 72  ache subsystem r
4e780 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 20  eads and writes 
4e790 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a  a file a page.**
4e7a0 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 70   at a time and p
4e7b0 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 6e 61  rovides a journa
4e7c0 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a  l for rollback..
4e7d0 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
4e7e0 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30 30 20  pager.h,v 1.100 
4e7f0 32 30 30 39 2f 30 32 2f 30 33 20 31 36 3a 35 31  2009/02/03 16:51
4e800 3a 32 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :25 danielk1977 
4e810 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
4e820 66 20 5f 50 41 47 45 52 5f 48 5f 0a 23 64 65 66  f _PAGER_H_.#def
4e830 69 6e 65 20 5f 50 41 47 45 52 5f 48 5f 0a 0a 2f  ine _PAGER_H_../
4e840 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 6d 61 78  *.** Default max
4e850 69 6d 75 6d 20 73 69 7a 65 20 66 6f 72 20 70 65  imum size for pe
4e860 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c  rsistent journal
4e870 20 66 69 6c 65 73 2e 20 41 20 6e 65 67 61 74 69   files. A negati
4e880 76 65 20 0a 2a 2a 20 76 61 6c 75 65 20 6d 65 61  ve .** value mea
4e890 6e 73 20 6e 6f 20 6c 69 6d 69 74 2e 20 54 68 69  ns no limit. Thi
4e8a0 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 6f  s value may be o
4e8b0 76 65 72 72 69 64 64 65 6e 20 75 73 69 6e 67 20  verridden using 
4e8c0 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50  the .** sqlite3P
4e8d0 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c  agerJournalSizeL
4e8e0 69 6d 69 74 28 29 20 41 50 49 2e 20 53 65 65 20  imit() API. See 
4e8f0 61 6c 73 6f 20 22 50 52 41 47 4d 41 20 6a 6f 75  also "PRAGMA jou
4e900 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 22  rnal_size_limit"
4e910 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
4e920 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52  ITE_DEFAULT_JOUR
4e930 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 0a 20  NAL_SIZE_LIMIT. 
4e940 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
4e950 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f  DEFAULT_JOURNAL_
4e960 53 49 5a 45 5f 4c 49 4d 49 54 20 2d 31 0a 23 65  SIZE_LIMIT -1.#e
4e970 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
4e980 74 79 70 65 20 75 73 65 64 20 74 6f 20 72 65 70  type used to rep
4e990 72 65 73 65 6e 74 20 61 20 70 61 67 65 20 6e 75  resent a page nu
4e9a0 6d 62 65 72 2e 20 20 54 68 65 20 66 69 72 73 74  mber.  The first
4e9b0 20 70 61 67 65 20 69 6e 20 61 20 66 69 6c 65 0a   page in a file.
4e9c0 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67  ** is called pag
4e9d0 65 20 31 2e 20 20 30 20 69 73 20 75 73 65 64 20  e 1.  0 is used 
4e9e0 74 6f 20 72 65 70 72 65 73 65 6e 74 20 22 6e 6f  to represent "no
4e9f0 74 20 61 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79  t a page"..*/.ty
4ea00 70 65 64 65 66 20 75 33 32 20 50 67 6e 6f 3b 0a  pedef u32 Pgno;.
4ea10 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e  ./*.** Each open
4ea20 20 66 69 6c 65 20 69 73 20 6d 61 6e 61 67 65 64   file is managed
4ea30 20 62 79 20 61 20 73 65 70 61 72 61 74 65 20 69   by a separate i
4ea40 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 22  nstance of the "
4ea50 50 61 67 65 72 22 20 73 74 72 75 63 74 75 72 65  Pager" structure
4ea60 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
4ea70 75 63 74 20 50 61 67 65 72 20 50 61 67 65 72 3b  uct Pager Pager;
4ea80 0a 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74  ../*.** Handle t
4ea90 79 70 65 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a  ype for pages..*
4eaa0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
4eab0 20 50 67 48 64 72 20 44 62 50 61 67 65 3b 0a 0a   PgHdr DbPage;..
4eac0 2f 2a 0a 2a 2a 20 50 61 67 65 20 6e 75 6d 62 65  /*.** Page numbe
4ead0 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 20  r PAGER_MJ_PGNO 
4eae0 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 69 6e  is never used in
4eaf0 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62   an SQLite datab
4eb00 61 73 65 20 28 69 74 20 69 73 0a 2a 2a 20 72 65  ase (it is.** re
4eb10 73 65 72 76 65 64 20 66 6f 72 20 77 6f 72 6b 69  served for worki
4eb20 6e 67 20 61 72 6f 75 6e 64 20 61 20 77 69 6e 64  ng around a wind
4eb30 6f 77 73 2f 70 6f 73 69 78 20 69 6e 63 6f 6d 70  ows/posix incomp
4eb40 61 74 69 62 69 6c 69 74 79 29 2e 20 49 74 20 69  atibility). It i
4eb50 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74 68 65  s.** used in the
4eb60 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 69 67 6e   journal to sign
4eb70 69 66 79 20 74 68 61 74 20 74 68 65 20 72 65 6d  ify that the rem
4eb80 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6a 6f  ainder of the jo
4eb90 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 69  urnal file .** i
4eba0 73 20 64 65 76 6f 74 65 64 20 74 6f 20 73 74 6f  s devoted to sto
4ebb0 72 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6a 6f  ring a master jo
4ebc0 75 72 6e 61 6c 20 6e 61 6d 65 20 2d 20 74 68 65  urnal name - the
4ebd0 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70  re are no more p
4ebe0 61 67 65 73 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 20  ages to.** roll 
4ebf0 62 61 63 6b 2e 20 53 65 65 20 63 6f 6d 6d 65 6e  back. See commen
4ec00 74 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ts for function 
4ec10 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e  writeMasterJourn
4ec20 61 6c 28 29 20 69 6e 20 70 61 67 65 72 2e 63 20  al() in pager.c 
4ec30 0a 2a 2a 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  .** for details.
4ec40 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45  .*/.#define PAGE
4ec50 52 5f 4d 4a 5f 50 47 4e 4f 28 78 29 20 28 28 50  R_MJ_PGNO(x) ((P
4ec60 67 6e 6f 29 28 28 50 45 4e 44 49 4e 47 5f 42 59  gno)((PENDING_BY
4ec70 54 45 2f 28 28 78 29 2d 3e 70 61 67 65 53 69 7a  TE/((x)->pageSiz
4ec80 65 29 29 2b 31 29 29 0a 0a 2f 2a 0a 2a 2a 20 41  e))+1))../*.** A
4ec90 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f  llowed values fo
4eca0 72 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61  r the flags para
4ecb0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
4ecc0 50 61 67 65 72 4f 70 65 6e 28 29 2e 0a 2a 2a 0a  PagerOpen()..**.
4ecd0 2a 2a 20 4e 4f 54 45 3a 20 54 68 65 73 65 20 76  ** NOTE: These v
4ece0 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68  alues must match
4ecf0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
4ed00 6e 67 20 42 54 52 45 45 5f 20 76 61 6c 75 65 73  ng BTREE_ values
4ed10 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a 2a 2f 0a   in btree.h..*/.
4ed20 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4f 4d  #define PAGER_OM
4ed30 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30 78 30 30  IT_JOURNAL  0x00
4ed40 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20  01    /* Do not 
4ed50 75 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a  use a rollback j
4ed60 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e  ournal */.#defin
4ed70 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c  e PAGER_NO_READL
4ed80 4f 43 4b 20 20 20 30 78 30 30 30 32 20 20 20 20  OCK   0x0002    
4ed90 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b  /* Omit readlock
4eda0 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69  s on readonly fi
4edb0 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61  les */../*.** Va
4edc0 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74  lid values for t
4edd0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
4ede0 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67  nt to sqlite3Pag
4edf0 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 29 2e  erLockingMode().
4ee00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45  .*/.#define PAGE
4ee10 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55  R_LOCKINGMODE_QU
4ee20 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66  ERY      -1.#def
4ee30 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  ine PAGER_LOCKIN
4ee40 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 20 20 20  GMODE_NORMAL    
4ee50 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45    0.#define PAGE
4ee60 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
4ee70 43 4c 55 53 49 56 45 20 20 20 31 0a 0a 2f 2a 0a  CLUSIVE   1../*.
4ee80 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 73 20  ** Valid values 
4ee90 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 61  for the second a
4eea0 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
4eeb0 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
4eec0 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  de()..*/.#define
4eed0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
4eee0 44 45 5f 51 55 45 52 59 20 20 20 20 20 20 2d 31  DE_QUERY      -1
4eef0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a  .#define PAGER_J
4ef00 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54  OURNALMODE_DELET
4ef10 45 20 20 20 20 20 20 30 20 20 20 2f 2a 20 43 6f  E      0   /* Co
4ef20 6d 6d 69 74 20 62 79 20 64 65 6c 65 74 69 6e 67  mmit by deleting
4ef30 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
4ef40 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a  .#define PAGER_J
4ef50 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49  OURNALMODE_PERSI
4ef60 53 54 20 20 20 20 20 31 20 20 20 2f 2a 20 43 6f  ST     1   /* Co
4ef70 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69 6e 67 20  mmit by zeroing 
4ef80 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a  journal header *
4ef90 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
4efa0 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20  JOURNALMODE_OFF 
4efb0 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a 20 4a          2   /* J
4efc0 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65 64 2e 20  ournal omitted. 
4efd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
4efe0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52  R_JOURNALMODE_TR
4eff0 55 4e 43 41 54 45 20 20 20 20 33 20 20 20 2f 2a  UNCATE    3   /*
4f000 20 43 6f 6d 6d 69 74 20 62 79 20 74 72 75 6e 63   Commit by trunc
4f010 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 2a 2f  ating journal */
4f020 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a  .#define PAGER_J
4f030 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
4f040 59 20 20 20 20 20 20 34 20 20 20 2f 2a 20 49 6e  Y      4   /* In
4f050 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20  -memory journal 
4f060 66 69 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  file */../*.** T
4f070 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20  he remainder of 
4f080 74 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  this file contai
4f090 6e 73 20 74 68 65 20 64 65 63 6c 61 72 61 74 69  ns the declarati
4f0a0 6f 6e 73 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ons of the funct
4f0b0 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b  ions.** that mak
4f0c0 65 20 75 70 20 74 68 65 20 50 61 67 65 72 20 73  e up the Pager s
4f0d0 75 62 2d 73 79 73 74 65 6d 20 41 50 49 2e 20 53  ub-system API. S
4f0e0 65 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 63  ee source code c
4f0f0 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 0a 2a 2a 20  omments for .** 
4f100 61 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72  a detailed descr
4f110 69 70 74 69 6f 6e 20 6f 66 20 65 61 63 68 20 72  iption of each r
4f120 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 0a 2f 2a 20 4f  outine..*/../* O
4f130 70 65 6e 20 61 6e 64 20 63 6c 6f 73 65 20 61 20  pen and close a 
4f140 50 61 67 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  Pager connection
4f150 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  . */ .SQLITE_PRI
4f160 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4f170 50 61 67 65 72 4f 70 65 6e 28 73 71 6c 69 74 65  PagerOpen(sqlite
4f180 33 5f 76 66 73 20 2a 2c 20 50 61 67 65 72 20 2a  3_vfs *, Pager *
4f190 2a 70 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20  *ppPager, const 
4f1a0 63 68 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c 69  char*, int,int,i
4f1b0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
4f1c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
4f1d0 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65 72 20  agerClose(Pager 
4f1e0 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49 54 45  *pPager);.SQLITE
4f1f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4f200 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c  ite3PagerReadFil
4f210 65 68 65 61 64 65 72 28 50 61 67 65 72 2a 2c 20  eheader(Pager*, 
4f220 69 6e 74 2c 20 75 6e 73 69 67 6e 65 64 20 63 68  int, unsigned ch
4f230 61 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69  ar*);../* Functi
4f240 6f 6e 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 66  ons used to conf
4f250 69 67 75 72 65 20 61 20 50 61 67 65 72 20 6f 62  igure a Pager ob
4f260 6a 65 63 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f  ject. */.SQLITE_
4f270 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4f280 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79  ite3PagerSetBusy
4f290 68 61 6e 64 6c 65 72 28 50 61 67 65 72 2a 2c 20  handler(Pager*, 
4f2a0 69 6e 74 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20  int(*)(void *), 
4f2b0 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  void *);.SQLITE_
4f2c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4f2d0 69 74 65 33 50 61 67 65 72 53 65 74 52 65 69 6e  ite3PagerSetRein
4f2e0 69 74 65 72 28 50 61 67 65 72 2a 2c 20 76 6f 69  iter(Pager*, voi
4f2f0 64 28 2a 29 28 44 62 50 61 67 65 2a 29 29 3b 0a  d(*)(DbPage*));.
4f300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4f310 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  nt sqlite3PagerS
4f320 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72  etPagesize(Pager
4f330 2a 2c 20 75 31 36 2a 29 3b 0a 53 51 4c 49 54 45  *, u16*);.SQLITE
4f340 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4f350 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65  ite3PagerMaxPage
4f360 43 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e  Count(Pager*, in
4f370 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4f380 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
4f390 61 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65  agerSetCachesize
4f3a0 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53  (Pager*, int);.S
4f3b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4f3c0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
4f3d0 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61  etSafetyLevel(Pa
4f3e0 67 65 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ger*,int,int);.S
4f3f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4f400 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f  t sqlite3PagerLo
4f410 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 20  ckingMode(Pager 
4f420 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
4f430 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4f440 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d  te3PagerJournalM
4f450 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74  ode(Pager *, int
4f460 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4f470 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 61 67  E i64 sqlite3Pag
4f480 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  erJournalSizeLim
4f490 69 74 28 50 61 67 65 72 20 2a 2c 20 69 36 34 29  it(Pager *, i64)
4f4a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4f4b0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
4f4c0 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61  **sqlite3PagerBa
4f4d0 63 6b 75 70 50 74 72 28 50 61 67 65 72 2a 29 3b  ckupPtr(Pager*);
4f4e0 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75  ../* Functions u
4f4f0 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e  sed to obtain an
4f500 64 20 72 65 6c 65 61 73 65 20 70 61 67 65 20 72  d release page r
4f510 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 20 0a 53  eferences. */ .S
4f520 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4f530 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63  t sqlite3PagerAc
4f540 71 75 69 72 65 28 50 61 67 65 72 20 2a 70 50 61  quire(Pager *pPa
4f550 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  ger, Pgno pgno, 
4f560 44 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c  DbPage **ppPage,
4f570 20 69 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23   int clrFlag);.#
4f580 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61  define sqlite3Pa
4f590 67 65 72 47 65 74 28 41 2c 42 2c 43 29 20 73 71  gerGet(A,B,C) sq
4f5a0 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72  lite3PagerAcquir
4f5b0 65 28 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54  e(A,B,C,0).SQLIT
4f5c0 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65  E_PRIVATE DbPage
4f5d0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f   *sqlite3PagerLo
4f5e0 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67  okup(Pager *pPag
4f5f0 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a  er, Pgno pgno);.
4f600 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4f610 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
4f620 52 65 66 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  Ref(DbPage*);.SQ
4f630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4f640 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e  d sqlite3PagerUn
4f650 72 65 66 28 44 62 50 61 67 65 2a 29 3b 0a 0a 2f  ref(DbPage*);../
4f660 2a 20 4f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20  * Operations on 
4f670 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 2e  page references.
4f680 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
4f690 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
4f6a0 67 65 72 57 72 69 74 65 28 44 62 50 61 67 65 2a  gerWrite(DbPage*
4f6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4f6c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
4f6d0 67 65 72 44 6f 6e 74 57 72 69 74 65 28 44 62 50  gerDontWrite(DbP
4f6e0 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  age*);.SQLITE_PR
4f6f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4f700 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50  3PagerMovepage(P
4f710 61 67 65 72 2a 2c 44 62 50 61 67 65 2a 2c 50 67  ager*,DbPage*,Pg
4f720 6e 6f 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  no,int);.SQLITE_
4f730 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4f740 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63  te3PagerPageRefc
4f750 6f 75 6e 74 28 44 62 50 61 67 65 2a 29 3b 0a 53  ount(DbPage*);.S
4f760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4f770 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
4f780 47 65 74 44 61 74 61 28 44 62 50 61 67 65 20 2a  GetData(DbPage *
4f790 29 3b 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ); .SQLITE_PRIVA
4f7a0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
4f7b0 50 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62  PagerGetExtra(Db
4f7c0 50 61 67 65 20 2a 29 3b 20 0a 0a 2f 2a 20 46 75  Page *); ../* Fu
4f7d0 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20  nctions used to 
4f7e0 6d 61 6e 61 67 65 20 70 61 67 65 72 20 74 72 61  manage pager tra
4f7f0 6e 73 61 63 74 69 6f 6e 73 20 61 6e 64 20 73 61  nsactions and sa
4f800 76 65 70 6f 69 6e 74 73 2e 20 2a 2f 0a 53 51 4c  vepoints. */.SQL
4f810 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4f820 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
4f830 63 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e  count(Pager*, in
4f840 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
4f850 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
4f860 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 2a  agerBegin(Pager*
4f870 2c 20 69 6e 74 20 65 78 46 6c 61 67 29 3b 0a 53  , int exFlag);.S
4f880 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4f890 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
4f8a0 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67  mmitPhaseOne(Pag
4f8b0 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a  er*,const char *
4f8c0 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53  zMaster, int);.S
4f8d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4f8e0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  t sqlite3PagerSy
4f8f0 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nc(Pager *pPager
4f900 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4f910 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
4f920 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
4f930 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
4f940 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4f950 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
4f960 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  k(Pager*);.SQLIT
4f970 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4f980 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61  lite3PagerOpenSa
4f990 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
4f9a0 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53  Pager, int n);.S
4f9b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4f9c0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  t sqlite3PagerSa
4f9d0 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
4f9e0 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69  Pager, int op, i
4f9f0 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a  nt iSavepoint);.
4fa00 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73  ./* Functions us
4fa10 65 64 20 74 6f 20 71 75 65 72 79 20 70 61 67 65  ed to query page
4fa20 72 20 73 74 61 74 65 20 61 6e 64 20 63 6f 6e 66  r state and conf
4fa30 69 67 75 72 61 74 69 6f 6e 2e 20 2a 2f 0a 53 51  iguration. */.SQ
4fa40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20  LITE_PRIVATE u8 
4fa50 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65  sqlite3PagerIsre
4fa60 61 64 6f 6e 6c 79 28 50 61 67 65 72 2a 29 3b 0a  adonly(Pager*);.
4fa70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4fa80 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
4fa90 65 66 63 6f 75 6e 74 28 50 61 67 65 72 2a 29 3b  efcount(Pager*);
4faa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4fab0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
4fac0 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65  te3PagerFilename
4fad0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
4fae0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
4faf0 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69  qlite3_vfs *sqli
4fb00 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65  te3PagerVfs(Page
4fb10 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
4fb20 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ATE sqlite3_file
4fb30 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
4fb40 6c 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  le(Pager*);.SQLI
4fb50 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
4fb60 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61   char *sqlite3Pa
4fb70 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50  gerJournalname(P
4fb80 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
4fb90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4fba0 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61  e3PagerNosync(Pa
4fbb0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
4fbc0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
4fbd0 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63  te3PagerTempSpac
4fbe0 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  e(Pager*);.SQLIT
4fbf0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4fc00 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64  lite3PagerIsMemd
4fc10 62 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46  b(Pager*);../* F
4fc20 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f  unctions used to
4fc30 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 64 61   truncate the da
4fc40 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a  tabase file. */.
4fc50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4fc60 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
4fc70 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61  TruncateImage(Pa
4fc80 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a 0a 2f 2a 20  ger*,Pgno);../* 
4fc90 55 73 65 64 20 62 79 20 65 6e 63 72 79 70 74 69  Used by encrypti
4fca0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 2a  on extensions. *
4fcb0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
4fcc0 48 41 53 5f 43 4f 44 45 43 0a 53 51 4c 49 54 45  HAS_CODEC.SQLITE
4fcd0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4fce0 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
4fcf0 6f 64 65 63 28 50 61 67 65 72 2a 2c 76 6f 69 64  odec(Pager*,void
4fd00 2a 28 2a 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a  *(*)(void*,void*
4fd10 2c 50 67 6e 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a  ,Pgno,int),void*
4fd20 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 75  );.#endif../* Fu
4fd30 6e 63 74 69 6f 6e 73 20 74 6f 20 73 75 70 70 6f  nctions to suppo
4fd40 72 74 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  rt testing and d
4fd50 65 62 75 67 67 69 6e 67 2e 20 2a 2f 0a 23 69 66  ebugging. */.#if
4fd60 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
4fd70 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
4fd80 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45  ITE_TEST).SQLITE
4fd90 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20  _PRIVATE   Pgno 
4fda0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
4fdb0 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b  number(DbPage*);
4fdc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4fdd0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67    int sqlite3Pag
4fde0 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62  erIswriteable(Db
4fdf0 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 23  Page*);.#endif.#
4fe00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
4fe10 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
4fe20 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50     int *sqlite3P
4fe30 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 2a  agerStats(Pager*
4fe40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4fe50 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
4fe60 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67  PagerRefdump(Pag
4fe70 65 72 2a 29 3b 0a 20 20 76 6f 69 64 20 64 69 73  er*);.  void dis
4fe80 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
4fe90 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a  o_errors(void);.
4fea0 20 20 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69    void enable_si
4feb0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
4fec0 73 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23  s(void);.#else.#
4fed0 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f   define disable_
4fee0 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
4fef0 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65  ors().# define e
4ff00 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
4ff10 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64  io_errors().#end
4ff20 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50  if..#endif /* _P
4ff30 41 47 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  AGER_H_ */../***
4ff40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
4ff50 6f 66 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a  of pager.h *****
4ff60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4ff70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4ff80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
4ff90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
4ffa0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
4ffb0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
4ffc0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
4ffd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
4ffe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
4fff0 75 64 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20  ude pcache.h in 
50000 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
50010 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
50020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
50030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
50040 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20  n file pcache.h 
50050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
50080 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35  * 2008 August 05
50090 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
500a0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
500b0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
500c0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
500d0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
500e0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
500f0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
50100 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
50110 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
50120 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
50130 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
50140 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
50150 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
50160 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
50170 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
50180 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
50190 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
501a0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
501b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
501c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
501d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
501e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
501f0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
50200 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
50210 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74  interface that t
50220 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63  he sqlite page c
50230 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65  ache.** subsyste
50240 6d 2e 20 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  m. .**.** @(#) $
50250 49 64 3a 20 70 63 61 63 68 65 2e 68 2c 76 20 31  Id: pcache.h,v 1
50260 2e 31 39 20 32 30 30 39 2f 30 31 2f 32 30 20 31  .19 2009/01/20 1
50270 37 3a 30 36 3a 32 37 20 64 61 6e 69 65 6c 6b 31  7:06:27 danielk1
50280 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  977 Exp $.*/..#i
50290 66 6e 64 65 66 20 5f 50 43 41 43 48 45 5f 48 5f  fndef _PCACHE_H_
502a0 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
502b0 20 50 67 48 64 72 20 50 67 48 64 72 3b 0a 74 79   PgHdr PgHdr;.ty
502c0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61  pedef struct PCa
502d0 63 68 65 20 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a  che PCache;../*.
502e0 2a 2a 20 45 76 65 72 79 20 70 61 67 65 20 69 6e  ** Every page in
502f0 20 74 68 65 20 63 61 63 68 65 20 69 73 20 63 6f   the cache is co
50300 6e 74 72 6f 6c 6c 65 64 20 62 79 20 61 6e 20 69  ntrolled by an i
50310 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
50320 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75  ollowing.** stru
50330 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  cture..*/.struct
50340 20 50 67 48 64 72 20 7b 0a 20 20 76 6f 69 64 20   PgHdr {.  void 
50350 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *pData;         
50360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
50370 74 65 6e 74 20 6f 66 20 74 68 69 73 20 70 61 67  tent of this pag
50380 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 78  e */.  void *pEx
50390 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  tra;            
503a0 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 63        /* Extra c
503b0 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 50 67 48 64  ontent */.  PgHd
503c0 72 20 2a 70 44 69 72 74 79 3b 20 20 20 20 20 20  r *pDirty;      
503d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
503e0 61 6e 73 69 65 6e 74 20 6c 69 73 74 20 6f 66 20  ansient list of 
503f0 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20  dirty pages */. 
50400 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20   Pgno pgno;     
50410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50420 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66  /* Page number f
50430 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  or this page */.
50440 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
50450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50460 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 74 68   /* The pager th
50470 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20  is page is part 
50480 6f 66 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  of */.#ifdef SQL
50490 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
504a0 20 20 75 33 32 20 70 61 67 65 48 61 73 68 3b 20    u32 pageHash; 
504b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504c0 20 2f 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65   /* Hash of page
504d0 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 23 65 6e 64   content */.#end
504e0 69 66 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20  if.  u16 flags; 
504f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50500 20 20 20 20 2f 2a 20 50 47 48 44 52 20 66 6c 61      /* PGHDR fla
50510 67 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77  gs defined below
50520 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a   */..  /********
50530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20  **************. 
50570 20 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f   ** Elements abo
50580 76 65 20 61 72 65 20 70 75 62 6c 69 63 2e 20 20  ve are public.  
50590 41 6c 6c 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  All that follows
505a0 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 70   is private to p
505b0 63 61 63 68 65 2e 63 0a 20 20 2a 2a 20 61 6e 64  cache.c.  ** and
505c0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61   should not be a
505d0 63 63 65 73 73 65 64 20 62 79 20 6f 74 68 65 72  ccessed by other
505e0 20 6d 6f 64 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20   modules..  */. 
505f0 20 69 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20   i16 nRef;      
50600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50610 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65  /* Number of use
50620 72 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  rs of this page 
50630 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  */.  PCache *pCa
50640 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  che;            
50650 20 20 20 20 2f 2a 20 43 61 63 68 65 20 74 68 61      /* Cache tha
50660 74 20 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65  t owns this page
50670 20 2a 2f 0a 0a 20 20 50 67 48 64 72 20 2a 70 44   */..  PgHdr *pD
50680 69 72 74 79 4e 65 78 74 3b 20 20 20 20 20 20 20  irtyNext;       
50690 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6c        /* Next el
506a0 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66  ement in list of
506b0 20 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a   dirty pages */.
506c0 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 50    PgHdr *pDirtyP
506d0 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  rev;            
506e0 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 65 6c 65   /* Previous ele
506f0 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 20  ment in list of 
50700 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 7d  dirty pages */.}
50710 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65 73  ;../* Bit values
50720 20 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67 73   for PgHdr.flags
50730 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44   */.#define PGHD
50740 52 5f 44 49 52 54 59 20 20 20 20 20 20 20 20 20  R_DIRTY         
50750 20 20 20 20 30 78 30 30 32 20 20 2f 2a 20 50 61      0x002  /* Pa
50760 67 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a  ge has changed *
50770 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f  /.#define PGHDR_
50780 4e 45 45 44 5f 53 59 4e 43 20 20 20 20 20 20 20  NEED_SYNC       
50790 20 20 30 78 30 30 34 20 20 2f 2a 20 46 73 79 6e    0x004  /* Fsyn
507a0 63 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  c the rollback j
507b0 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 0a 20 20  ournal before.  
507c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507e0 20 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20       ** writing 
507f0 74 68 69 73 20 70 61 67 65 20 74 6f 20 74 68 65  this page to the
50800 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65   database */.#de
50810 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f  fine PGHDR_NEED_
50820 52 45 41 44 20 20 20 20 20 20 20 20 20 30 78 30  READ         0x0
50830 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 69  08  /* Content i
50840 73 20 75 6e 72 65 61 64 20 2a 2f 0a 23 64 65 66  s unread */.#def
50850 69 6e 65 20 50 47 48 44 52 5f 52 45 55 53 45 5f  ine PGHDR_REUSE_
50860 55 4e 4c 49 4b 45 4c 59 20 20 20 20 30 78 30 31  UNLIKELY    0x01
50870 30 20 20 2f 2a 20 41 20 68 69 6e 74 20 74 68 61  0  /* A hint tha
50880 74 20 72 65 75 73 65 20 69 73 20 75 6e 6c 69 6b  t reuse is unlik
50890 65 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ely */.#define P
508a0 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20  GHDR_DONT_WRITE 
508b0 20 20 20 20 20 20 20 30 78 30 32 30 20 20 2f 2a         0x020  /*
508c0 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f   Do not write co
508d0 6e 74 65 6e 74 20 74 6f 20 64 69 73 6b 20 2a 2f  ntent to disk */
508e0 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ../* Initialize 
508f0 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65  and shutdown the
50900 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73   page cache subs
50910 79 73 74 65 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f  ystem */.SQLITE_
50920 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
50930 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c  te3PcacheInitial
50940 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ize(void);.SQLIT
50950 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
50960 71 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74  qlite3PcacheShut
50970 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20  down(void);../* 
50980 50 61 67 65 20 63 61 63 68 65 20 62 75 66 66 65  Page cache buffe
50990 72 20 6d 61 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a  r management:.**
509a0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
509b0 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 49 54 45  implement SQLITE
509c0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
509d0 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  E..*/.SQLITE_PRI
509e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
509f0 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74  3PCacheBufferSet
50a00 75 70 28 76 6f 69 64 20 2a 2c 20 69 6e 74 20 73  up(void *, int s
50a10 7a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43  z, int n);../* C
50a20 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  reate a new page
50a30 72 20 63 61 63 68 65 2e 0a 2a 2a 20 55 6e 64 65  r cache..** Unde
50a40 72 20 6d 65 6d 6f 72 79 20 73 74 72 65 73 73 2c  r memory stress,
50a50 20 69 6e 76 6f 6b 65 20 78 53 74 72 65 73 73 20   invoke xStress 
50a60 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70  to try to make p
50a70 61 67 65 73 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f  ages clean..** O
50a80 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64 20 75 6e  nly clean and un
50a90 70 69 6e 6e 65 64 20 70 61 67 65 73 20 63 61 6e  pinned pages can
50aa0 20 62 65 20 72 65 63 6c 61 69 6d 65 64 2e 0a 2a   be reclaimed..*
50ab0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
50ac0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
50ad0 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73  cheOpen(.  int s
50ae0 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  zPage,          
50af0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
50b00 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20  e of every page 
50b10 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61  */.  int szExtra
50b20 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
50b30 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61      /* Extra spa
50b40 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ce associated wi
50b50 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  th each page */.
50b60 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65    int bPurgeable
50b70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
50b80 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65   /* True if page
50b90 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67  s are on backing
50ba0 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20   store */.  int 
50bb0 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a  (*xStress)(void*
50bc0 2c 20 50 67 48 64 72 2a 29 2c 20 2f 2a 20 43 61  , PgHdr*), /* Ca
50bd0 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b  ll to try to mak
50be0 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f  e pages clean */
50bf0 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73  .  void *pStress
50c00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
50c10 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
50c20 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43   xStress */.  PC
50c30 61 63 68 65 20 2a 70 54 6f 49 6e 69 74 20 20 20  ache *pToInit   
50c40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
50c50 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  Preallocated spa
50c60 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 68  ce for the PCach
50c70 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69  e */.);../* Modi
50c80 66 79 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  fy the page-size
50c90 20 61 66 74 65 72 20 74 68 65 20 63 61 63 68 65   after the cache
50ca0 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65   has been create
50cb0 64 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d. */.SQLITE_PRI
50cc0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
50cd0 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69  3PcacheSetPageSi
50ce0 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74  ze(PCache *, int
50cf0 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68  );../* Return th
50d00 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
50d10 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65  of a PCache obje
50d20 63 74 2e 20 20 55 73 65 64 20 74 6f 20 70 72 65  ct.  Used to pre
50d30 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 74 6f 72  allocate.** stor
50d40 61 67 65 20 73 70 61 63 65 2e 0a 2a 2f 0a 53 51  age space..*/.SQ
50d50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
50d60 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69   sqlite3PcacheSi
50d70 7a 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e  ze(void);../* On
50d80 65 20 72 65 6c 65 61 73 65 20 70 65 72 20 73 75  e release per su
50d90 63 63 65 73 73 66 75 6c 20 66 65 74 63 68 2e 20  ccessful fetch. 
50da0 20 50 61 67 65 20 69 73 20 70 69 6e 6e 65 64 20   Page is pinned 
50db0 75 6e 74 69 6c 20 72 65 6c 65 61 73 65 64 2e 0a  until released..
50dc0 2a 2a 20 52 65 66 65 72 65 6e 63 65 20 63 6f 75  ** Reference cou
50dd0 6e 74 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45  nted. .*/.SQLITE
50de0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
50df0 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28  ite3PcacheFetch(
50e00 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 2c 20 69  PCache*, Pgno, i
50e10 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 50  nt createFlag, P
50e20 67 48 64 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  gHdr**);.SQLITE_
50e30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
50e40 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
50e50 65 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49  e(PgHdr*);..SQLI
50e60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
50e70 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f  sqlite3PcacheDro
50e80 70 28 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20  p(PgHdr*);      
50e90 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 70 61 67     /* Remove pag
50ea0 65 20 66 72 6f 6d 20 63 61 63 68 65 20 2a 2f 0a  e from cache */.
50eb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
50ec0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
50ed0 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72  eMakeDirty(PgHdr
50ee0 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73  *);    /* Make s
50ef0 75 72 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b  ure page is mark
50f00 65 64 20 64 69 72 74 79 20 2a 2f 0a 53 51 4c 49  ed dirty */.SQLI
50f10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
50f20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
50f30 65 43 6c 65 61 6e 28 50 67 48 64 72 2a 29 3b 20  eClean(PgHdr*); 
50f40 20 20 20 2f 2a 20 4d 61 72 6b 20 61 20 73 69 6e     /* Mark a sin
50f50 67 6c 65 20 70 61 67 65 20 61 73 20 63 6c 65 61  gle page as clea
50f60 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n */.SQLITE_PRIV
50f70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
50f80 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50  PcacheCleanAll(P
50f90 43 61 63 68 65 2a 29 3b 20 20 20 20 2f 2a 20 4d  Cache*);    /* M
50fa0 61 72 6b 20 61 6c 6c 20 64 69 72 74 79 20 6c 69  ark all dirty li
50fb0 73 74 20 70 61 67 65 73 20 61 73 20 63 6c 65 61  st pages as clea
50fc0 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 61 6e 67 65 20  n */../* Change 
50fd0 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20  a page number.  
50fe0 55 73 65 64 20 62 79 20 69 6e 63 72 2d 76 61 63  Used by incr-vac
50ff0 75 75 6d 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  uum. */.SQLITE_P
51000 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
51010 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 50 67  te3PcacheMove(Pg
51020 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a  Hdr*, Pgno);../*
51030 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70 61 67 65   Remove all page
51040 73 20 77 69 74 68 20 70 67 6e 6f 3e 78 2e 20 20  s with pgno>x.  
51050 52 65 73 65 74 20 74 68 65 20 63 61 63 68 65 20  Reset the cache 
51060 69 66 20 78 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54  if x==0 */.SQLIT
51070 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
51080 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e  qlite3PcacheTrun
51090 63 61 74 65 28 50 43 61 63 68 65 2a 2c 20 50 67  cate(PCache*, Pg
510a0 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 47 65 74 20 61  no x);../* Get a
510b0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72   list of all dir
510c0 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ty pages in the 
510d0 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79  cache, sorted by
510e0 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
510f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50  SQLITE_PRIVATE P
51100 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61  gHdr *sqlite3Pca
51110 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61  cheDirtyList(PCa
51120 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 73 65 74  che*);../* Reset
51130 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 63   and close the c
51140 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 53  ache object */.S
51150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
51160 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
51170 43 6c 6f 73 65 28 50 43 61 63 68 65 2a 29 3b 0a  Close(PCache*);.
51180 0a 2f 2a 20 43 6c 65 61 72 20 66 6c 61 67 73 20  ./* Clear flags 
51190 66 72 6f 6d 20 70 61 67 65 73 20 6f 66 20 74 68  from pages of th
511a0 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a  e page cache */.
511b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
511c0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
511d0 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28  eClearSyncFlags(
511e0 50 43 61 63 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44  PCache *);../* D
511f0 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65  iscard the conte
51200 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65  nts of the cache
51210 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
51220 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
51230 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68  cacheClear(PCach
51240 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  e*);../* Return 
51250 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
51260 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   of outstanding 
51270 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20  page references 
51280 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
51290 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
512a0 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63  cheRefCount(PCac
512b0 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d  he*);../* Increm
512c0 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63  ent the referenc
512d0 65 20 63 6f 75 6e 74 20 6f 66 20 61 6e 20 65 78  e count of an ex
512e0 69 73 74 69 6e 67 20 70 61 67 65 20 2a 2f 0a 53  isting page */.S
512f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
51300 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
51310 52 65 66 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51  Ref(PgHdr*);..SQ
51320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
51330 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61   sqlite3PcachePa
51340 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72  geRefcount(PgHdr
51350 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74  *);../* Return t
51360 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
51370 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64 20  of pages stored 
51380 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a  in the cache */.
51390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
513a0 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
513b0 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65  Pagecount(PCache
513c0 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  *);..#ifdef SQLI
513d0 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f  TE_CHECK_PAGES./
513e0 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f 75 67  * Iterate throug
513f0 68 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  h all dirty page
51400 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72  s currently stor
51410 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e  ed in the cache.
51420 20 54 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61   This.** interfa
51430 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ce is only avail
51440 61 62 6c 65 20 69 66 20 53 51 4c 49 54 45 5f 43  able if SQLITE_C
51450 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65  HECK_PAGES is de
51460 66 69 6e 65 64 20 77 68 65 6e 20 74 68 65 20 0a  fined when the .
51470 2a 2a 20 6c 69 62 72 61 72 79 20 69 73 20 62 75  ** library is bu
51480 69 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ilt..*/.SQLITE_P
51490 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
514a0 74 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65  te3PcacheIterate
514b0 44 69 72 74 79 28 50 43 61 63 68 65 20 2a 70 43  Dirty(PCache *pC
514c0 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49 74  ache, void (*xIt
514d0 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0a 23  er)(PgHdr *));.#
514e0 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 61 6e  endif../* Set an
514f0 64 20 67 65 74 20 74 68 65 20 73 75 67 67 65 73  d get the sugges
51500 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 66  ted cache-size f
51510 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64  or the specified
51520 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 2a 2a   pager-cache..**
51530 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 61 6c  .** If no global
51540 20 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f 6e 66   maximum is conf
51550 69 67 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65  igured, then the
51560 20 73 79 73 74 65 6d 20 61 74 74 65 6d 70 74 73   system attempts
51570 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 68 65   to limit.** the
51580 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
51590 20 70 61 67 65 73 20 63 61 63 68 65 64 20 62 79   pages cached by
515a0 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 72   purgeable pager
515b0 2d 63 61 63 68 65 73 20 74 6f 20 74 68 65 20 73  -caches to the s
515c0 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 67  um.** of the sug
515d0 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a  gested cache-siz
515e0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
515f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
51600 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65  e3PcacheSetCache
51610 73 69 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69  size(PCache *, i
51620 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  nt);.#ifdef SQLI
51630 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  TE_TEST.SQLITE_P
51640 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
51650 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65  e3PcacheGetCache
51660 73 69 7a 65 28 50 43 61 63 68 65 20 2a 29 3b 0a  size(PCache *);.
51670 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
51680 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
51690 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f  ORY_MANAGEMENT./
516a0 2a 20 54 72 79 20 74 6f 20 72 65 74 75 72 6e 20  * Try to return 
516b0 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 74  memory used by t
516c0 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65  he pcache module
516d0 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 6d 65 6d   to the main mem
516e0 6f 72 79 20 68 65 61 70 20 2a 2f 0a 53 51 4c 49  ory heap */.SQLI
516f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
51700 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65  qlite3PcacheRele
51710 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a  aseMemory(int);.
51720 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
51730 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
51740 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
51750 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74  qlite3PcacheStat
51760 73 28 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a  s(int*,int*,int*
51770 2c 69 6e 74 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  ,int*);.#endif..
51780 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
51790 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68  oid sqlite3PCach
517a0 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64  eSetDefault(void
517b0 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50  );..#endif /* _P
517c0 43 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a  CACHE_H_ */../**
517d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
517e0 20 6f 66 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a   of pcache.h ***
517f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
51820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
51830 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
51840 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c   left off in sql
51850 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
51860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
51870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
51880 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20 74 68  clude os.h in th
51890 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
518a0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
518b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
518c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
518d0 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20 2a 2a  gin file os.h **
518e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
518f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
51910 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
51920 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 16.**.** The 
51930 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
51940 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
51950 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
51960 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
51970 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
51980 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
51990 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
519a0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
519b0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
519c0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
519d0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
519e0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
519f0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
51a00 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
51a10 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
51a20 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
51a30 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
51a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
51a90 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
51aa0 20 28 74 6f 67 65 74 68 65 72 20 77 69 74 68 20   (together with 
51ab0 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 20 73  is companion C s
51ac0 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 6c 65 0a  ource-code file.
51ad0 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 65 6d  ** "os.c") attem
51ae0 70 74 20 74 6f 20 61 62 73 74 72 61 63 74 20 74  pt to abstract t
51af0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70  he underlying op
51b00 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73  erating system s
51b10 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 53 51  o that.** the SQ
51b20 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c  Lite library wil
51b30 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68 20 50  l work on both P
51b40 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 6f 77 73  OSIX and windows
51b50 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20   systems..**.** 
51b60 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
51b70 20 69 73 20 23 69 6e 63 6c 75 64 65 2d 65 64 20   is #include-ed 
51b80 62 79 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 61  by sqliteInt.h a
51b90 6e 64 20 74 68 75 73 20 65 6e 64 73 20 75 70 0a  nd thus ends up.
51ba0 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75 64 65  ** being include
51bb0 64 20 62 79 20 65 76 65 72 79 20 73 6f 75 72 63  d by every sourc
51bc0 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  e file..**.** $I
51bd0 64 3a 20 6f 73 2e 68 2c 76 20 31 2e 31 30 38 20  d: os.h,v 1.108 
51be0 32 30 30 39 2f 30 32 2f 30 35 20 31 36 3a 33 31  2009/02/05 16:31
51bf0 3a 34 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :46 drh Exp $.*/
51c00 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  .#ifndef _SQLITE
51c10 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  _OS_H_.#define _
51c20 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a  SQLITE_OS_H_../*
51c30 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69  .** Figure out i
51c40 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  f we are dealing
51c50 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e 64   with Unix, Wind
51c60 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74 68  ows, or some oth
51c70 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20  er.** operating 
51c80 73 79 73 74 65 6d 2e 20 20 41 66 74 65 72 20 74  system.  After t
51c90 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
51ca0 63 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 73 73  ck of preprocess
51cb0 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20   macros,.** all 
51cc0 6f 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  of SQLITE_OS_UNI
51cd0 58 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  X, SQLITE_OS_WIN
51ce0 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c  , SQLITE_OS_OS2,
51cf0 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 5f 4f   and SQLITE_OS_O
51d00 54 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 64 65  THER .** will de
51d10 66 69 6e 65 64 20 74 6f 20 65 69 74 68 65 72 20  fined to either 
51d20 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66 20  1 or 0.  One of 
51d30 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62 65  the four will be
51d40 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20 0a   1.  The other .
51d50 2a 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 62 65  ** three will be
51d60 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e   0..*/.#if defin
51d70 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  ed(SQLITE_OS_OTH
51d80 45 52 29 0a 23 20 69 66 20 53 51 4c 49 54 45 5f  ER).# if SQLITE_
51d90 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20 20  OS_OTHER==1.#   
51da0 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f  undef SQLITE_OS_
51db0 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65 20  UNIX.#   define 
51dc0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30  SQLITE_OS_UNIX 0
51dd0 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54  .#   undef SQLIT
51de0 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 65 66  E_OS_WIN.#   def
51df0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
51e00 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51  N 0.#   undef SQ
51e10 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20  LITE_OS_OS2.#   
51e20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
51e30 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23 20  _OS2 0.# else.# 
51e40 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f    undef SQLITE_O
51e50 53 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 66 0a  S_OTHER.# endif.
51e60 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 69  #endif.#if !defi
51e70 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 55 4e  ned(SQLITE_OS_UN
51e80 49 58 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  IX) && !defined(
51e90 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29  SQLITE_OS_OTHER)
51ea0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
51eb0 5f 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69 66  _OS_OTHER 0.# if
51ec0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  ndef SQLITE_OS_W
51ed0 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e 65  IN.#   if define
51ee0 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66  d(_WIN32) || def
51ef0 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20 64  ined(WIN32) || d
51f00 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f  efined(__CYGWIN_
51f10 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  _) || defined(__
51f20 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64 65  MINGW32__) || de
51f30 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43  fined(__BORLANDC
51f40 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65  __).#     define
51f50 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 31   SQLITE_OS_WIN 1
51f60 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51  .#     define SQ
51f70 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23  LITE_OS_UNIX 0.#
51f80 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
51f90 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 20  TE_OS_OS2 0.#   
51fa0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 45  elif defined(__E
51fb0 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  MX__) || defined
51fc0 28 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65  (_OS2) || define
51fd0 64 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65  d(OS2) || define
51fe0 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66 69  d(_OS2_) || defi
51ff0 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20  ned(__OS2__).#  
52000 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
52010 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20  _OS_WIN 0.#     
52020 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
52030 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65  _UNIX 0.#     de
52040 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  fine SQLITE_OS_O
52050 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23 20  S2 1.#   else.# 
52060 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
52070 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20  E_OS_WIN 0.#    
52080 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
52090 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 20 64  S_UNIX 1.#     d
520a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
520b0 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a 23  OS2 0.#  endif.#
520c0 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20   else.#  define 
520d0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30  SQLITE_OS_UNIX 0
520e0 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  .#  define SQLIT
520f0 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e 64  E_OS_OS2 0.# end
52100 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64 65  if.#else.# ifnde
52110 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a  f SQLITE_OS_WIN.
52120 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  #  define SQLITE
52130 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64 69  _OS_WIN 0.# endi
52140 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
52150 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20  Determine if we 
52160 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
52170 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69   WindowsCE - whi
52180 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a  ch has a much.**
52190 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f   reduced API..*/
521a0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49  .#if defined(_WI
521b0 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e  N32_WCE).# defin
521c0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  e SQLITE_OS_WINC
521d0 45 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  E 1.#else.# defi
521e0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  ne SQLITE_OS_WIN
521f0 43 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  CE 0.#endif.../*
52200 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 6d  .** Define the m
52210 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61  aximum size of a
52220 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 6e   temporary filen
52230 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ame.*/.#if SQLIT
52240 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75  E_OS_WIN.# inclu
52250 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23  de <windows.h>.#
52260 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54   define SQLITE_T
52270 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 4d 41  EMPNAME_SIZE (MA
52280 58 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c 69 66  X_PATH+50).#elif
52290 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23   SQLITE_OS_OS2.#
522a0 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20   if (__GNUC__ > 
522b0 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d  3 || __GNUC__ ==
522c0 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e   3 && __GNUC_MIN
522d0 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64 65  OR__ >= 3) && de
522e0 66 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f 4d  fined(OS2_HIGH_M
522f0 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75 64  EMORY).#  includ
52300 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f 2a  e <os2safe.h> /*
52310 20 68 61 73 20 74 6f 20 62 65 20 69 6e 63 6c 75   has to be inclu
52320 64 65 64 20 62 65 66 6f 72 65 20 6f 73 32 2e 68  ded before os2.h
52330 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f 20   for linking to 
52340 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66 0a  work */.# endif.
52350 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f  # define INCL_DO
52360 53 44 41 54 45 54 49 4d 45 0a 23 20 64 65 66 69  SDATETIME.# defi
52370 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d  ne INCL_DOSFILEM
52380 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c  GR.# define INCL
52390 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 65 66  _DOSERRORS.# def
523a0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53 43  ine INCL_DOSMISC
523b0 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
523c0 4f 53 50 52 4f 43 45 53 53 0a 23 20 64 65 66 69  OSPROCESS.# defi
523d0 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c  ne INCL_DOSMODUL
523e0 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e  EMGR.# define IN
523f0 43 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 45 53  CL_DOSSEMAPHORES
52400 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 2e  .# include <os2.
52410 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 63  h>.# include <uc
52420 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20  onv.h>.# define 
52430 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f  SQLITE_TEMPNAME_
52440 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54 48  SIZE (CCHMAXPATH
52450 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64 65  COMP).#else.# de
52460 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50  fine SQLITE_TEMP
52470 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23 65  NAME_SIZE 200.#e
52480 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65 20  ndif../* If the 
52490 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 63  SET_FULLSYNC mac
524a0 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  ro is not define
524b0 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d 61  d above, then ma
524c0 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70  ke it.** a no-op
524d0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 54 5f  .*/.#ifndef SET_
524e0 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69 6e  FULLSYNC.# defin
524f0 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28 78  e SET_FULLSYNC(x
52500 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,y).#endif../*.*
52510 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69  * The default si
52520 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63  ze of a disk sec
52530 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  tor.*/.#ifndef S
52540 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
52550 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66 69  CTOR_SIZE.# defi
52560 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
52570 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31  T_SECTOR_SIZE 51
52580 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  2.#endif../*.** 
52590 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20  Temporary files 
525a0 61 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74 69  are named starti
525b0 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72 65  ng with this pre
525c0 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  fix followed by 
525d0 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70  16 random.** alp
525e0 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63  hanumeric charac
525f0 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 6c  ters, and no fil
52600 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 65  e extension. The
52610 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  y are stored in 
52620 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61 6e  the.** OS's stan
52630 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20 66  dard temporary f
52640 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20 61  ile directory, a
52650 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64 20 70  nd are deleted p
52660 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a 2a  rior to exit..**
52670 20 49 66 20 73 71 6c 69 74 65 20 69 73 20 62 65   If sqlite is be
52680 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69 6e 20  ing embedded in 
52690 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 6d 2c  another program,
526a0 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20 74 6f   you may wish to
526b0 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 70   change the.** p
526c0 72 65 66 69 78 20 74 6f 20 72 65 66 6c 65 63 74  refix to reflect
526d0 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 73 20   your program's 
526e0 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 69 66  name, so that if
526f0 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 65 78   your program ex
52700 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72 65  its.** premature
52710 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61 72  ly, old temporar
52720 79 20 66 69 6c 65 73 20 63 61 6e 20 62 65 20 65  y files can be e
52730 61 73 69 6c 79 20 69 64 65 6e 74 69 66 69 65 64  asily identified
52740 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 6f  . This can be do
52750 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 53 51  ne.** using -DSQ
52760 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
52770 52 45 46 49 58 3d 6d 79 70 72 65 66 69 78 5f 20  REFIX=myprefix_ 
52780 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  on the compiler 
52790 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a  command line..**
527a0 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 3a 20  .** 2006-10-31: 
527b0 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 72 65   The default pre
527c0 66 69 78 20 75 73 65 64 20 74 6f 20 62 65 20 22  fix used to be "
527d0 73 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 20 74  sqlite_".  But t
527e0 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 73 74  hen.** Mcafee st
527f0 61 72 74 65 64 20 75 73 69 6e 67 20 53 51 4c 69  arted using SQLi
52800 74 65 20 69 6e 20 74 68 65 69 72 20 61 6e 74 69  te in their anti
52810 2d 76 69 72 75 73 20 70 72 6f 64 75 63 74 20 61  -virus product a
52820 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 65 64  nd it.** started
52830 20 70 75 74 74 69 6e 67 20 66 69 6c 65 73 20 77   putting files w
52840 69 74 68 20 74 68 65 20 22 73 71 6c 69 74 65 22  ith the "sqlite"
52850 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 3a 2f   name in the c:/
52860 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20  temp folder..** 
52870 54 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d 61 6e  This annoyed man
52880 79 20 77 69 6e 64 6f 77 73 20 75 73 65 72 73 2e  y windows users.
52890 20 20 54 68 6f 73 65 20 75 73 65 72 73 20 77 6f    Those users wo
528a0 75 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 0a 2a  uld then do a .*
528b0 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 68 20  * Google search 
528c0 66 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 66 69  for "sqlite", fi
528d0 6e 64 20 74 68 65 20 74 65 6c 65 70 68 6f 6e 65  nd the telephone
528e0 20 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 65 0a   numbers of the.
528f0 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 61 6e  ** developers an
52900 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 20 74  d call to wake t
52910 68 65 6d 20 75 70 20 61 74 20 6e 69 67 68 74 20  hem up at night 
52920 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a  and complain..**
52930 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e   For this reason
52940 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61  , the default na
52950 6d 65 20 70 72 65 66 69 78 20 69 73 20 63 68 61  me prefix is cha
52960 6e 67 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69  nged to be "sqli
52970 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 64 20  te" .** spelled 
52980 62 61 63 6b 77 61 72 64 73 2e 20 20 53 6f 20 74  backwards.  So t
52990 68 65 20 74 65 6d 70 20 66 69 6c 65 73 20 61 72  he temp files ar
529a0 65 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 66 69  e still identifi
529b0 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 62 6f  ed, but.** anybo
529c0 64 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68 20  dy smart enough 
529d0 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68  to figure out th
529e0 65 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20 6c  e code is also l
529f0 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a 20 65  ikely smart.** e
52a00 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74 68  nough to know th
52a10 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64  at calling the d
52a20 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e 6f  eveloper will no
52a30 74 20 68 65 6c 70 20 67 65 74 20 72 69 64 0a 2a  t help get rid.*
52a40 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a  * of the file..*
52a50 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
52a60 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49  _TEMP_FILE_PREFI
52a70 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  X.# define SQLIT
52a80 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
52a90 49 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 65 6e  IX "etilqs_".#en
52aa0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
52ab0 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 20  ollowing values 
52ac0 6d 61 79 20 62 65 20 70 61 73 73 65 64 20 61 73  may be passed as
52ad0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
52ae0 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 74  ment to.** sqlit
52af0 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 65 20  e3OsLock(). The 
52b00 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 65 78  various locks ex
52b10 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c 6f 77  hibit the follow
52b20 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a 0a 2a  ing semantics:.*
52b30 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 20 20  *.** SHARED:    
52b40 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 70 72  Any number of pr
52b50 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64  ocesses may hold
52b60 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 73   a SHARED lock s
52b70 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a  imultaneously..*
52b80 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41 20 73  * RESERVED:  A s
52b90 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61  ingle process ma
52ba0 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45  y hold a RESERVE
52bb0 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65  D lock on a file
52bc0 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   at.**          
52bd0 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 68 65    any time. Othe
52be0 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20  r processes may 
52bf0 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 6e 20  hold and obtain 
52c00 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73  new SHARED locks
52c10 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20 20  ..** PENDING:   
52c20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73  A single process
52c30 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 4e 44   may hold a PEND
52c40 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69  ING lock on a fi
52c50 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20  le at.**        
52c60 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d 65      any one time
52c70 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41 52 45  . Existing SHARE
52c80 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72 73  D locks may pers
52c90 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77 0a  ist, but no new.
52ca0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 53 48  **            SH
52cb0 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 62  ARED locks may b
52cc0 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f 74  e obtained by ot
52cd0 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a  her processes..*
52ce0 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e 20  * EXCLUSIVE: An 
52cf0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 70  EXCLUSIVE lock p
52d00 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68  recludes all oth
52d10 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  er locks..**.** 
52d20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79  PENDING_LOCK may
52d30 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64 20 64   not be passed d
52d40 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c 69 74  irectly to sqlit
52d50 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 73 74  e3OsLock(). Inst
52d60 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 65 73  ead, a.** proces
52d70 73 20 74 68 61 74 20 72 65 71 75 65 73 74 73 20  s that requests 
52d80 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
52d90 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 6f  k may actually o
52da0 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 0a  btain a PENDING.
52db0 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 63 61  ** lock. This ca
52dc0 6e 20 62 65 20 75 70 67 72 61 64 65 64 20 74 6f  n be upgraded to
52dd0 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
52de0 63 6b 20 62 79 20 61 20 73 75 62 73 65 71 75 65  ck by a subseque
52df0 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71  nt call to.** sq
52e00 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a  lite3OsLock()..*
52e10 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43  /.#define NO_LOC
52e20 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66  K         0.#def
52e30 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ine SHARED_LOCK 
52e40 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 52 45      1.#define RE
52e50 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 32 0a  SERVED_LOCK   2.
52e60 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f  #define PENDING_
52e70 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 69 6e  LOCK    3.#defin
52e80 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  e EXCLUSIVE_LOCK
52e90 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20    4../*.** File 
52ea0 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20 20  Locking Notes:  
52eb0 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77 69  (Mostly about wi
52ec0 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20 73  ndows but also s
52ed0 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e 69  ome info for Uni
52ee0 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e  x).**.** We cann
52ef0 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65 45  ot use LockFileE
52f00 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c  x() or UnlockFil
52f10 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f 39  eEx() on Win95/9
52f20 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a 2a 20  8/ME because.** 
52f30 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  those functions 
52f40 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
52f50 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20 6f 6e  e.  So we use on
52f60 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 61 6e  ly LockFile() an
52f70 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  d.** UnlockFile(
52f80 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c  )..**.** LockFil
52f90 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e 6f 74  e() prevents not
52fa0 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20 62 75   just writing bu
52fb0 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 20 62  t also reading b
52fc0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  y other processe
52fd0 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 5f 4c  s..** A SHARED_L
52fe0 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  OCK is obtained 
52ff0 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 69 6e  by locking a sin
53000 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f  gle randomly-cho
53010 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f 75 74  sen .** byte out
53020 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 20 72   of a specific r
53030 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e 20 54  ange of bytes. T
53040 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 73 20  he lock byte is 
53050 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a 2a 20  obtained at .** 
53060 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 73 65  random so two se
53070 70 61 72 61 74 65 20 72 65 61 64 65 72 73 20 63  parate readers c
53080 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 63 65  an probably acce
53090 73 73 20 74 68 65 20 66 69 6c 65 20 61 74 20 74  ss the file at t
530a0 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65  he .** same time
530b0 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20 61 72  , unless they ar
530c0 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 63 68  e unlucky and ch
530d0 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20 6c 6f  oose the same lo
530e0 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e 20 45  ck byte..** An E
530f0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 69 73  XCLUSIVE_LOCK is
53100 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63   obtained by loc
53110 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 20 69  king all bytes i
53120 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2a 20  n the range..** 
53130 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62  There can only b
53140 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 20 41  e one writer.  A
53150 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 69   RESERVED_LOCK i
53160 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f  s obtained by lo
53170 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e 67 6c  cking.** a singl
53180 65 20 62 79 74 65 20 6f 66 20 74 68 65 20 66 69  e byte of the fi
53190 6c 65 20 74 68 61 74 20 69 73 20 64 65 73 69 67  le that is desig
531a0 6e 61 74 65 64 20 61 73 20 74 68 65 20 72 65 73  nated as the res
531b0 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 65 2e  erved lock byte.
531c0 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f 4c 4f  .** A PENDING_LO
531d0 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62  CK is obtained b
531e0 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 73 69  y locking a desi
531f0 67 6e 61 74 65 64 20 62 79 74 65 20 64 69 66 66  gnated byte diff
53200 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68  erent from.** th
53210 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  e RESERVED_LOCK 
53220 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57  byte..**.** On W
53230 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74 65  inNT/2K/XP syste
53240 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  ms, LockFileEx()
53250 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65 45   and UnlockFileE
53260 78 28 29 20 61 72 65 20 61 76 61 69 6c 61 62 6c  x() are availabl
53270 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e  e,.** which mean
53280 73 20 77 65 20 63 61 6e 20 75 73 65 20 72 65 61  s we can use rea
53290 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73  der/writer locks
532a0 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f 77  .  When reader/w
532b0 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61  riter locks.** a
532c0 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c 6f 63  re used, the loc
532d0 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e 20 74  k is placed on t
532e0 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20 6f 66  he same range of
532f0 20 62 79 74 65 73 20 74 68 61 74 20 69 73 20 75   bytes that is u
53300 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f 62 61  sed.** for proba
53310 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 6e 67  bilistic locking
53320 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e   in Win95/98/ME.
53330 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f 63    Hence, the loc
53340 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a 20 77  king scheme.** w
53350 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 6f 20  ill support two 
53360 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 72 65  or more Win95 re
53370 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f 72 20  aders or two or 
53380 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 64 65  more WinNT reade
53390 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 69 6e  rs..** But a sin
533a0 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64 65 72  gle Win95 reader
533b0 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61   will lock out a
533c0 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73  ll WinNT readers
533d0 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a 2a 2a   and a single.**
533e0 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20 77 69   WinNT reader wi
533f0 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20  ll lock out all 
53400 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65 61 64  other Win95 read
53410 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ers..**.** The f
53420 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 6e 65  ollowing #define
53430 73 20 73 70 65 63 69 66 79 20 74 68 65 20 72 61  s specify the ra
53440 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75 73 65  nge of bytes use
53450 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a  d for locking..*
53460 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73  * SHARED_SIZE is
53470 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
53480 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 69  ytes available i
53490 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f 6d 20  n the pool from 
534a0 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e 64 6f  which.** a rando
534b0 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65 63 74  m byte is select
534c0 65 64 20 66 6f 72 20 61 20 73 68 61 72 65 64 20  ed for a shared 
534d0 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f 6c 20  lock.  The pool 
534e0 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a 2a 20  of bytes for.** 
534f0 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 65 67  shared locks beg
53500 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f 46 49  ins at SHARED_FI
53510 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  RST. .**.** The 
53520 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72  same locking str
53530 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79 74  ategy and.** byt
53540 65 20 72 61 6e 67 65 73 20 61 72 65 20 75 73 65  e ranges are use
53550 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69  d for Unix.  Thi
53560 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68  s leaves open th
53570 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20  e possiblity of 
53580 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74  having.** client
53590 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e  s on win95, winN
535a0 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20  T, and unix all 
535b0 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73  talking to the s
535c0 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0a  ame shared file.
535d0 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69  ** and all locki
535e0 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  ng correctly.  T
535f0 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65  o do so would re
53600 71 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61  quire that samba
53610 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a 2a   (or whatever.**
53620 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20 75   tool is being u
53630 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68 61  sed for file sha
53640 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74 73  ring) implements
53650 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c 79   locks correctly
53660 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e 64   between.** wind
53670 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20 49  ows and unix.  I
53680 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61 74  'm guessing that
53690 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74 6f   isn't likely to
536a0 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79 0a   happen, but by.
536b0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ** using the sam
536c0 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20  e locking range 
536d0 77 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20  we are at least 
536e0 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73 73  open to the poss
536f0 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c  ibility..**.** L
53700 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f 77  ocking in window
53710 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e 20  s is manditory. 
53720 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e   For this reason
53730 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 72  , we cannot stor
53740 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74 61  e.** actual data
53750 20 69 6e 20 74 68 65 20 62 79 74 65 73 20 75 73   in the bytes us
53760 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20  ed for locking. 
53770 20 54 68 65 20 70 61 67 65 72 20 6e 65 76 65 72   The pager never
53780 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74 68   allocates.** th
53790 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64  e pages involved
537a0 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 72   in locking ther
537b0 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f 53  efore.  SHARED_S
537c0 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64 20  IZE is selected 
537d0 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 6c  so.** that all l
537e0 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f 6e  ocks will fit on
537f0 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 65   a single page e
53800 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69 6d  ven at the minim
53810 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a  um page size..**
53820 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64 65   PENDING_BYTE de
53830 66 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e 6e  fines the beginn
53840 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73  ing of the locks
53850 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20 50 45  .  By default PE
53860 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69 73  NDING_BYTE.** is
53870 20 73 65 74 20 68 69 67 68 20 73 6f 20 74 68 61   set high so tha
53880 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20  t we don't have 
53890 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 75  to allocate an u
538a0 6e 75 73 65 64 20 70 61 67 65 20 65 78 63 65 70  nused page excep
538b0 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c 61  t.** for very la
538c0 72 67 65 20 64 61 74 61 62 61 73 65 73 2e 20 20  rge databases.  
538d0 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20 74  But one should t
538e0 65 73 74 20 74 68 65 20 70 61 67 65 20 73 6b 69  est the page ski
538f0 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a 20  pping logic .** 
53900 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44 49  by setting PENDI
53910 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64 20  NG_BYTE low and 
53920 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69  running the enti
53930 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73 75  re regression su
53940 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67  ite..**.** Chang
53950 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ing the value of
53960 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65   PENDING_BYTE re
53970 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c  sults in a subtl
53980 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a 2a  y incompatible.*
53990 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20  * file format.  
539a0 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77  Depending on how
539b0 20 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20   it is changed, 
539c0 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f  you might not no
539d0 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63 6f  tice.** the inco
539e0 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68  mpatibility righ
539f0 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e  t away, even run
53a00 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72  ning a full regr
53a10 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20  ession test..** 
53a20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 61  The default loca
53a30 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47 5f  tion of PENDING_
53a40 42 59 54 45 20 69 73 20 74 68 65 20 66 69 72 73  BYTE is the firs
53a50 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a  t byte past the.
53a60 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2e  ** 1GB boundary.
53a70 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  .**.*/.#define P
53a80 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20  ENDING_BYTE     
53a90 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42   sqlite3PendingB
53aa0 79 74 65 0a 23 64 65 66 69 6e 65 20 52 45 53 45  yte.#define RESE
53ab0 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 28 50  RVED_BYTE     (P
53ac0 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 0a 23  ENDING_BYTE+1).#
53ad0 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 46 49  define SHARED_FI
53ae0 52 53 54 20 20 20 20 20 20 28 50 45 4e 44 49 4e  RST      (PENDIN
53af0 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 69 6e  G_BYTE+2).#defin
53b00 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20 20 20  e SHARED_SIZE   
53b10 20 20 20 20 35 31 30 0a 0a 2f 2a 20 0a 2a 2a 20      510../* .** 
53b20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63  Functions for ac
53b30 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  cessing sqlite3_
53b40 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 2a 2f  file methods .*/
53b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53b60 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f  int sqlite3OsClo
53b70 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  se(sqlite3_file*
53b80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
53b90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52  E int sqlite3OsR
53ba0 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ead(sqlite3_file
53bb0 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d  *, void*, int am
53bc0 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a  t, i64 offset);.
53bd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53be0 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  nt sqlite3OsWrit
53bf0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  e(sqlite3_file*,
53c00 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
53c10 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65  t amt, i64 offse
53c20 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
53c30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
53c40 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33  Truncate(sqlite3
53c50 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a 65  _file*, i64 size
53c60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
53c70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53  E int sqlite3OsS
53c80 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ync(sqlite3_file
53c90 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
53ca0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
53cb0 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71  te3OsFileSize(sq
53cc0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34  lite3_file*, i64
53cd0 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45   *pSize);.SQLITE
53ce0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53cf0 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74  ite3OsLock(sqlit
53d00 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
53d10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53d20 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  nt sqlite3OsUnlo
53d30 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ck(sqlite3_file*
53d40 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
53d50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
53d60 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
53d70 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  dLock(sqlite3_fi
53d80 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65  le *id, int *pRe
53d90 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sOut);.SQLITE_PR
53da0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
53db0 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73  3OsFileControl(s
53dc0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74  qlite3_file*,int
53dd0 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65  ,void*);.#define
53de0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42   SQLITE_FCNTL_DB
53df0 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63 61 30  _UNCHANGED 0xca0
53e00 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50 52 49  93fa0.SQLITE_PRI
53e10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53e20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  OsSectorSize(sql
53e30 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a  ite3_file *id);.
53e40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53e50 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69  nt sqlite3OsDevi
53e60 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
53e70 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
53e80 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e  id);../* .** Fun
53e90 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73  ctions for acces
53ea0 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76 66 73  sing sqlite3_vfs
53eb0 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c   methods .*/.SQL
53ec0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53ed0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 73 71  sqlite3OsOpen(sq
53ee0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
53ef0 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74  st char *, sqlit
53f00 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c 20 69  e3_file*, int, i
53f10 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt *);.SQLITE_PR
53f20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
53f30 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65  3OsDelete(sqlite
53f40 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63  3_vfs *, const c
53f50 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  har *, int);.SQL
53f60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53f70 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
53f80 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63  sqlite3_vfs *, c
53f90 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
53fa0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b  , int *pResOut);
53fb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53fc0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c  int sqlite3OsFul
53fd0 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65  lPathname(sqlite
53fe0 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63  3_vfs *, const c
53ff0 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72  har *, int, char
54000 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   *);.#ifndef SQL
54010 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
54020 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50  TENSION.SQLITE_P
54030 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
54040 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c  ite3OsDlOpen(sql
54050 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73  ite3_vfs *, cons
54060 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  t char *);.SQLIT
54070 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
54080 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28  qlite3OsDlError(
54090 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69  sqlite3_vfs *, i
540a0 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nt, char *);.SQL
540b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
540c0 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79   (*sqlite3OsDlSy
540d0 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  m(sqlite3_vfs *,
540e0 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63   void *, const c
540f0 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b 0a 53  har *))(void);.S
54100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
54110 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c  id sqlite3OsDlCl
54120 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ose(sqlite3_vfs 
54130 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65 6e 64  *, void *);.#end
54140 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
54150 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
54160 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
54170 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
54180 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
54190 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63  e3_vfs *, int, c
541a0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har *);.SQLITE_P
541b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
541c0 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65  e3OsSleep(sqlite
541d0 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b 0a 53  3_vfs *, int);.S
541e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
541f0 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65  t sqlite3OsCurre
54200 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76  ntTime(sqlite3_v
54210 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a  fs *, double*);.
54220 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e  ./*.** Convenien
54230 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72  ce functions for
54240 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f   opening and clo
54250 73 69 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67  sing files using
54260 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c   .** sqlite3_mal
54270 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20  loc() to obtain 
54280 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 66 69  space for the fi
54290 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74  le-handle struct
542a0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
542b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
542c0 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73  e3OsOpenMalloc(s
542d0 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f  qlite3_vfs *, co
542e0 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69  nst char *, sqli
542f0 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74  te3_file **, int
54300 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ,int*);.SQLITE_P
54310 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54320 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71  e3OsCloseFree(sq
54330 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a  lite3_file *);..
54340 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54  #endif /* _SQLIT
54350 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  E_OS_H_ */../***
54360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
54370 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  of os.h ********
54380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
543a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
543b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
543c0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
543d0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
543e0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
543f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
54400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
54410 75 64 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74  ude mutex.h in t
54420 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
54430 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
54440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
54450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
54460 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a  n file mutex.h *
54470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
544a0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38  * 2007 August 28
544b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
544c0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
544d0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
544e0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
544f0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
54500 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
54510 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
54520 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
54530 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
54540 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
54550 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
54560 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
54570 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
54580 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
54590 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
545a0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
545b0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
545c0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
545d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
545e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
545f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
54610 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
54620 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d  contains the com
54630 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f 72 20 61  mon header for a
54640 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  ll mutex impleme
54650 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65  ntations..** The
54660 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 68 65 61   sqliteInt.h hea
54670 64 65 72 20 23 69 6e 63 6c 75 64 65 73 20 74 68  der #includes th
54680 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20  is file so that 
54690 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a  it is available.
546a0 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65  ** to all source
546b0 20 66 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61   files.  We brea
546c0 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65  k it out in an e
546d0 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68  ffort to keep th
546e0 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 74 65 72  e code.** better
546f0 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a   organized..**.*
54700 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65 20  * NOTE:  source 
54710 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e 6f  files should *no
54720 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73  t* #include this
54730 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 69 72   header file dir
54740 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 63 65  ectly..** Source
54750 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 23 69   files should #i
54760 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c 69 74  nclude the sqlit
54770 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e 64 20  eInt.h file and 
54780 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0a 2a 2a  let that file.**
54790 20 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6f 6e   include this on
547a0 65 20 69 6e 64 69 72 65 63 74 6c 79 2e 0a 2a 2a  e indirectly..**
547b0 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 68  .** $Id: mutex.h
547c0 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 30 2f 30  ,v 1.9 2008/10/0
547d0 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45  7 15:25:48 drh E
547e0 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  xp $.*/.../*.** 
547f0 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74 20  Figure out what 
54800 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63  version of the c
54810 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68 65  ode to use.  The
54820 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a 0a   choices are.**.
54830 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  **   SQLITE_MUTE
54840 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 20 4e  X_OMIT         N
54850 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 20  o mutex logic.  
54860 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 2e 20  Not even stubs. 
54870 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   The.**         
54880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54890 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d 70 6c      mutexes impl
548a0 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  emention cannot 
548b0 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a 2a  be overridden.**
548c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
548d0 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20               at 
548e0 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a  start-time..**.*
548f0 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  *   SQLITE_MUTEX
54900 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 46 6f  _NOOP         Fo
54910 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65  r single-threade
54920 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20  d applications. 
54930 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   No.**          
54940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54950 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73     mutual exclus
54960 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 2e  ion is provided.
54970 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 20    But this.**   
54980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54990 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c 65 6d            implem
549a0 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 20  entation can be 
549b0 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a 2a 2a  overridden at.**
549c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
549d0 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
549e0 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20  rt-time..**.**  
549f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54   SQLITE_MUTEX_PT
54a00 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20 6d  HREADS     For m
54a10 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70  ulti-threaded ap
54a20 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 6e  plications on Un
54a30 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  ix..**.**   SQLI
54a40 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20  TE_MUTEX_W32    
54a50 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d        For multi-
54a60 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
54a70 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0a  tions on Win32..
54a80 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
54a90 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20  UTEX_OS2        
54aa0 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65    For multi-thre
54ab0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
54ac0 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69  s on OS/2..*/.#i
54ad0 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 41 44  f !SQLITE_THREAD
54ae0 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SAFE.# define SQ
54af0 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a  LITE_MUTEX_OMIT.
54b00 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
54b10 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
54b20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
54b30 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 69  MUTEX_NOOP).#  i
54b40 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  f SQLITE_OS_UNIX
54b50 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  .#    define SQL
54b60 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
54b70 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54  DS.#  elif SQLIT
54b80 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 65  E_OS_WIN.#    de
54b90 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
54ba0 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 53 51  X_W32.#  elif SQ
54bb0 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20  LITE_OS_OS2.#   
54bc0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
54bd0 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 65  UTEX_OS2.#  else
54be0 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  .#    define SQL
54bf0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23  ITE_MUTEX_NOOP.#
54c00 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a    endif.#endif..
54c10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  #ifdef SQLITE_MU
54c20 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49  TEX_OMIT./*.** I
54c30 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  f this is a no-o
54c40 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  p implementation
54c50 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 72  , implement ever
54c60 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 6f 73  ything as macros
54c70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  ..*/.#define sql
54c80 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
54c90 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 65 33  (X)    ((sqlite3
54ca0 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69  _mutex*)8).#defi
54cb0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
54cc0 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 6e 65  _free(X).#define
54cd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
54ce0 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e 65 20  nter(X).#define 
54cf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
54d00 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 54 45  y(X)      SQLITE
54d10 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _OK.#define sqli
54d20 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
54d30 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  X).#define sqlit
54d40 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 29  e3_mutex_held(X)
54d50 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 73       1.#define s
54d60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
54d70 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 66 69  held(X)  1.#defi
54d80 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ne sqlite3MutexA
54d90 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 28 73  lloc(X)      ((s
54da0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29  qlite3_mutex*)8)
54db0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
54dc0 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 20 20  MutexInit()     
54dd0 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65     SQLITE_OK.#de
54de0 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65  fine sqlite3Mute
54df0 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f 2a  xEnd().#endif /*
54e00 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
54e10 4f 4d 49 54 5f 4d 55 54 45 58 29 20 2a 2f 0a 0a  OMIT_MUTEX) */..
54e20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
54e30 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 2a  End of mutex.h *
54e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
54e70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
54e80 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
54e90 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
54ea0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
54eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
54ec0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74  ../*.** Each dat
54ed0 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62 65  abase file to be
54ee0 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65   accessed by the
54ef0 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69 6e   system is an in
54f00 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65  stance.** of the
54f10 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
54f20 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72 65  ture.  There are
54f30 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f 66   normally two of
54f40 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65   these structure
54f50 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69  s.** in the sqli
54f60 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20  te.aDb[] array. 
54f70 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20 6d   aDb[0] is the m
54f80 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
54f90 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d 20  e and.** aDb[1] 
54fa0 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
54fb0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f 6c  file used to hol
54fc0 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  d temporary tabl
54fd0 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a  es.  Additional.
54fe0 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61 79  ** databases may
54ff0 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a 2f   be attached..*/
55000 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20 63  .struct Db {.  c
55010 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
55020 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
55030 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  his database */.
55040 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20    Btree *pBt;   
55050 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 2a         /* The B*
55060 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 66  Tree structure f
55070 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65  or this database
55080 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e   file */.  u8 in
55090 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20  Trans;          
550a0 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 62  /* 0: not writab
550b0 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 74  le.  1: Transact
550c0 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 6f  ion.  2: Checkpo
550d0 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 65  int */.  u8 safe
550e0 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f 2a  ty_level;     /*
550f0 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65 20   How aggressive 
55100 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 61 20  at syncing data 
55110 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 76 6f 69  to disk */.  voi
55120 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20  d *pAux;        
55130 20 20 20 20 20 20 20 2f 2a 20 41 75 78 69 6c 69         /* Auxili
55140 61 72 79 20 64 61 74 61 2e 20 20 55 73 75 61 6c  ary data.  Usual
55150 6c 79 20 4e 55 4c 4c 20 2a 2f 0a 20 20 76 6f 69  ly NULL */.  voi
55160 64 20 28 2a 78 46 72 65 65 41 75 78 29 28 76 6f  d (*xFreeAux)(vo
55170 69 64 2a 29 3b 20 20 2f 2a 20 52 6f 75 74 69 6e  id*);  /* Routin
55180 65 20 74 6f 20 66 72 65 65 20 70 41 75 78 20 2a  e to free pAux *
55190 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  /.  Schema *pSch
551a0 65 6d 61 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ema;     /* Poin
551b0 74 65 72 20 74 6f 20 64 61 74 61 62 61 73 65 20  ter to database 
551c0 73 63 68 65 6d 61 20 28 70 6f 73 73 69 62 6c 79  schema (possibly
551d0 20 73 68 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a   shared) */.};..
551e0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
551f0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
55200 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 74 6f  ng structure sto
55210 72 65 73 20 61 20 64 61 74 61 62 61 73 65 20 73  res a database s
55220 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  chema..**.** If 
55230 74 68 65 72 65 20 61 72 65 20 6e 6f 20 76 69 72  there are no vir
55240 74 75 61 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66  tual tables conf
55250 69 67 75 72 65 64 20 69 6e 20 74 68 69 73 20 73  igured in this s
55260 63 68 65 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63  chema, the.** Sc
55270 68 65 6d 61 2e 64 62 20 76 61 72 69 61 62 6c 65  hema.db variable
55280 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
55290 20 41 66 74 65 72 20 74 68 65 20 66 69 72 73 74   After the first
552a0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a   virtual table.*
552b0 2a 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64  * has been added
552c0 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 70  , it is set to p
552d0 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61  oint to the data
552e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
552f0 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 61  .** used to crea
55300 74 65 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  te the connectio
55310 6e 2e 20 4f 6e 63 65 20 61 20 76 69 72 74 75 61  n. Once a virtua
55320 6c 20 74 61 62 6c 65 20 68 61 73 20 62 65 65 6e  l table has been
55330 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65  .** added to the
55340 20 53 63 68 65 6d 61 20 73 74 72 75 63 74 75 72   Schema structur
55350 65 20 61 6e 64 20 74 68 65 20 53 63 68 65 6d 61  e and the Schema
55360 2e 64 62 20 76 61 72 69 61 62 6c 65 20 70 6f 70  .db variable pop
55370 75 6c 61 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79  ulated, .** only
55380 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 63   that database c
55390 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 75 73  onnection may us
553a0 65 20 74 68 65 20 53 63 68 65 6d 61 20 74 6f 20  e the Schema to 
553b0 70 72 65 70 61 72 65 20 0a 2a 2a 20 73 74 61 74  prepare .** stat
553c0 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63  ements..*/.struc
553d0 74 20 53 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74  t Schema {.  int
553e0 20 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20   schema_cookie; 
553f0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73 63    /* Database sc
55400 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d  hema version num
55410 62 65 72 20 66 6f 72 20 74 68 69 73 20 66 69 6c  ber for this fil
55420 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 62 6c 48  e */.  Hash tblH
55430 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ash;        /* A
55440 6c 6c 20 74 61 62 6c 65 73 20 69 6e 64 65 78 65  ll tables indexe
55450 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48  d by name */.  H
55460 61 73 68 20 69 64 78 48 61 73 68 3b 20 20 20 20  ash idxHash;    
55470 20 20 20 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65      /* All (name
55480 64 29 20 69 6e 64 69 63 65 73 20 69 6e 64 65 78  d) indices index
55490 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20  ed by name */.  
554a0 48 61 73 68 20 74 72 69 67 48 61 73 68 3b 20 20  Hash trigHash;  
554b0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67       /* All trig
554c0 67 65 72 73 20 69 6e 64 65 78 65 64 20 62 79 20  gers indexed by 
554d0 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 61  name */.  Hash a
554e0 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 2f  FKey;          /
554f0 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 73 20 69  * Foreign keys i
55500 6e 64 65 78 65 64 20 62 79 20 74 6f 2d 74 61 62  ndexed by to-tab
55510 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  le */.  Table *p
55520 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20  SeqTab;      /* 
55530 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65  The sqlite_seque
55540 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62  nce table used b
55550 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20  y AUTOINCREMENT 
55560 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72  */.  u8 file_for
55570 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68  mat;      /* Sch
55580 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69  ema format versi
55590 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65  on for this file
555a0 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20   */.  u8 enc;   
555b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
555c0 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64  xt encoding used
555d0 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73   by this databas
555e0 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73  e */.  u16 flags
555f0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;           /* F
55600 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65 64 20  lags associated 
55610 77 69 74 68 20 74 68 69 73 20 73 63 68 65 6d 61  with this schema
55620 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 5f   */.  int cache_
55630 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75  size;      /* Nu
55640 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f  mber of pages to
55650 20 75 73 65 20 69 6e 20 74 68 65 20 63 61 63 68   use in the cach
55660 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  e */.#ifndef SQL
55670 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
55680 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 20  TABLE.  sqlite3 
55690 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  *db;         /* 
556a0 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69  "Owner" connecti
556b0 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20  on. See comment 
556c0 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a  above */.#endif.
556d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20  };../*.** These 
556e0 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73  macros can be us
556f0 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c  ed to test, set,
55700 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69   or clear bits i
55710 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61  n the .** Db.fla
55720 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65  gs field..*/.#de
55730 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70 65 72  fine DbHasProper
55740 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 28  ty(D,I,P)     ((
55750 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68  (D)->aDb[I].pSch
55760 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d  ema->flags&(P))=
55770 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 44 62  =(P)).#define Db
55780 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 44  HasAnyProperty(D
55790 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e 61 44  ,I,P)  (((D)->aD
557a0 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c  b[I].pSchema->fl
557b0 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65  ags&(P))!=0).#de
557c0 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70 65 72  fine DbSetProper
557d0 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 44  ty(D,I,P)     (D
557e0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d  )->aDb[I].pSchem
557f0 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64  a->flags|=(P).#d
55800 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50 72 6f  efine DbClearPro
55810 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 28  perty(D,I,P)   (
55820 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65  D)->aDb[I].pSche
55830 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a  ma->flags&=~(P).
55840 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
55850 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 44 42  alues for the DB
55860 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a  .flags field..**
55870 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d  .** The DB_Schem
55880 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20  aLoaded flag is 
55890 73 65 74 20 61 66 74 65 72 20 74 68 65 20 64 61  set after the da
558a0 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61  tabase schema ha
558b0 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69  s been.** read i
558c0 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73  nto internal has
558d0 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  h tables..**.** 
558e0 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20  DB_UnresetViews 
558f0 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f  means that one o
55900 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76  r more views hav
55910 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74  e column names t
55920 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e  hat.** have been
55930 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66   filled out.  If
55940 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e   the schema chan
55950 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d  ges, these colum
55960 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a  n names might.**
55970 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20   changes and so 
55980 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65  the view will ne
55990 65 64 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a  ed to be reset..
559a0 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63  */.#define DB_Sc
559b0 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78  hemaLoaded    0x
559c0 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68  0001  /* The sch
559d0 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61  ema has been loa
559e0 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44  ded */.#define D
559f0 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 20  B_UnresetViews  
55a00 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d    0x0002  /* Som
55a10 65 20 76 69 65 77 73 20 68 61 76 65 20 64 65 66  e views have def
55a20 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  ined column name
55a30 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f  s */.#define DB_
55a40 45 6d 70 74 79 20 20 20 20 20 20 20 20 20 20 20  Empty           
55a50 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66  0x0004  /* The f
55a60 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65  ile is empty (le
55a70 6e 67 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f  ngth 0 bytes) */
55a80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62  ../*.** The numb
55a90 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20  er of different 
55aa0 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20  kinds of things 
55ab0 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69  that can be limi
55ac0 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  ted.** using the
55ad0 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29   sqlite3_limit()
55ae0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23   interface..*/.#
55af0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f  define SQLITE_N_
55b00 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49  LIMIT (SQLITE_LI
55b10 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
55b20 42 45 52 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  BER+1)../*.** Lo
55b30 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69  okaside malloc i
55b40 73 20 61 20 73 65 74 20 6f 66 20 66 69 78 65 64  s a set of fixed
55b50 2d 73 69 7a 65 20 62 75 66 66 65 72 73 20 74 68  -size buffers th
55b60 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a  at can be used.*
55b70 2a 20 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61  * to satisfy sma
55b80 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d  ll transient mem
55b90 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
55ba0 65 71 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65  equests for obje
55bb0 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  cts.** associate
55bc0 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  d with a particu
55bd0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  lar database con
55be0 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73  nection.  The us
55bf0 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64  e of.** lookasid
55c00 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65  e malloc provide
55c10 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20  s a significant 
55c20 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61  performance enha
55c30 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72  ncement.** (appr
55c40 6f 78 20 31 30 25 29 20 62 79 20 61 76 6f 69 64  ox 10%) by avoid
55c50 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c  ing numerous mal
55c60 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 65 73 74  loc/free request
55c70 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a  s while parsing.
55c80 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
55c90 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f  s..**.** The Loo
55ca0 6b 61 73 69 64 65 20 73 74 72 75 63 74 75 72 65  kaside structure
55cb0 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61   holds configura
55cc0 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tion information
55cd0 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f   about the.** lo
55ce0 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73  okaside malloc s
55cf0 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20  ubsystem.  Each 
55d00 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79  available memory
55d10 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a   allocation in.*
55d20 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20  * the lookaside 
55d30 73 75 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f  subsystem is sto
55d40 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20  red on a linked 
55d50 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64  list of Lookasid
55d60 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73  eSlot.** objects
55d70 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64  ..**.** Lookasid
55d80 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72  e allocations ar
55d90 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66  e only allowed f
55da0 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20  or objects that 
55db0 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  are associated.*
55dc0 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  * with a particu
55dd0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  lar database con
55de0 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c  nection.  Hence,
55df0 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74   schema informat
55e00 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65  ion cannot.** be
55e10 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61   stored in looka
55e20 73 69 64 65 20 62 65 63 61 75 73 65 20 69 6e 20  side because in 
55e30 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64  shared cache mod
55e40 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66  e the schema inf
55e50 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73  ormation.** is s
55e60 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c  hared by multipl
55e70 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
55e80 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f  ctions.  Therefo
55e90 72 65 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e  re, while parsin
55ea0 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f  g.** schema info
55eb0 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f  rmation, the Loo
55ec0 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
55ed0 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 20  flag is cleared 
55ee0 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61  so that.** looka
55ef0 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  side allocations
55f00 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f   are not used to
55f10 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73   construct the s
55f20 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a  chema objects..*
55f30 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69  /.struct Lookasi
55f40 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20  de {.  u16 sz;  
55f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
55f60 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62  * Size of each b
55f70 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a  uffer in bytes *
55f80 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b  /.  u8 bEnabled;
55f90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
55fa0 61 6c 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20  alse to disable 
55fb0 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c  new lookaside al
55fc0 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75  locations */.  u
55fd0 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20  8 bMalloced;    
55fe0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
55ff0 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65  f pStart obtaine
56000 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
56010 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74  alloc() */.  int
56020 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20   nOut;          
56030 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
56040 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65 6e  f buffers curren
56050 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20  tly checked out 
56060 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20  */.  int mxOut; 
56070 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
56080 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66  Highwater mark f
56090 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f  or nOut */.  Loo
560a0 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 65  kasideSlot *pFre
560b0 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  e;   /* List of 
560c0 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 65 72  available buffer
560d0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74  s */.  void *pSt
560e0 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  art;           /
560f0 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20  * First byte of 
56100 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79  available memory
56110 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64   space */.  void
56120 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20   *pEnd;         
56130 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
56140 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76  e past end of av
56150 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f  ailable space */
56160 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61  .};.struct Looka
56170 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f  sideSlot {.  Loo
56180 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78  kasideSlot *pNex
56190 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 75  t;    /* Next bu
561a0 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74  ffer in the list
561b0 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 72 73   of free buffers
561c0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
561d0 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66  hash table for f
561e0 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
561f0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20  ons..**.** Hash 
56200 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 74 72  each FuncDef str
56210 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20  ucture into one 
56220 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 48 61  of the FuncDefHa
56230 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a  sh.a[] slots..**
56240 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20   Collisions are 
56250 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 2e 70  on the FuncDef.p
56260 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73  Hash chain..*/.s
56270 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 73  truct FuncDefHas
56280 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61  h {.  FuncDef *a
56290 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48  [23];       /* H
562a0 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75  ash table for fu
562b0 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f  nctions */.};../
562c0 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61  *.** Each databa
562d0 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  se is an instanc
562e0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
562f0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
56300 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c  .** The sqlite.l
56310 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73  astRowid records
56320 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74   the last insert
56330 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64   rowid generated
56340 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74   by an.** insert
56350 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73   statement.  Ins
56360 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f  erts on views do
56370 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73 20   not affect its 
56380 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20  value.  Each.** 
56390 74 72 69 67 67 65 72 20 68 61 73 20 69 74 73 20  trigger has its 
563a0 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20  own context, so 
563b0 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63  that lastRowid c
563c0 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 69 6e  an be updated in
563d0 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73  side.** triggers
563e0 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20   as usual.  The 
563f0 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77  previous value w
56400 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20  ill be restored 
56410 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 72  once the trigger
56420 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e  .** exits.  Upon
56430 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f   entering a befo
56440 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66  re or instead of
56450 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f   trigger, lastRo
56460 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e  wid is no.** lon
56470 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 65 72  ger (since after
56480 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29   version 2.8.12)
56490 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a   reset to -1..**
564a0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e  .** The sqlite.n
564b0 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20  Change does not 
564c0 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69  count changes wi
564d0 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e  thin triggers an
564e0 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f  d keeps no.** co
564f0 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 72 65  ntext.  It is re
56500 73 65 74 20 61 74 20 73 74 61 72 74 20 6f 66 20  set at start of 
56510 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a  sqlite3_exec..**
56520 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68   The sqlite.lsCh
56530 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73 20  ange represents 
56540 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
56550 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68  anges made by th
56560 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74  e last.** insert
56570 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c  , update, or del
56580 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ete statement.  
56590 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74  It remains const
565a0 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74  ant throughout t
565b0 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20  he.** length of 
565c0 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  a statement and 
565d0 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 64 20  is then updated 
565e0 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e  by OP_SetCounts.
565f0 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20    It keeps a.** 
56600 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75  context stack ju
56610 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69  st like lastRowi
56620 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f  d so that the co
56630 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a  unt of changes.*
56640 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67  * within a trigg
56650 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f  er is not seen o
56660 75 74 73 69 64 65 20 74 68 65 20 74 72 69 67 67  utside the trigg
56670 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20  er.  Changes to 
56680 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20  views do not.** 
56690 61 66 66 65 63 74 20 74 68 65 20 76 61 6c 75 65  affect the value
566a0 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a   of lsChange..**
566b0 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68   The sqlite.csCh
566c0 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b  ange keeps track
566d0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
566e0 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65  f current change
566f0 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20  s (since.** the 
56700 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20  last statement) 
56710 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 75  and is used to u
56720 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43  pdate sqlite_lsC
56730 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  hange..**.** The
56740 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65   member variable
56750 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65  s sqlite.errCode
56760 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67  , sqlite.zErrMsg
56770 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72   and sqlite.zErr
56780 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74  Msg16.** store t
56790 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65  he most recent e
567a0 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69  rror code and, i
567b0 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74  f applicable, st
567c0 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74  ring. The.** int
567d0 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73  ernal function s
567e0 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69 73  qlite3Error() is
567f0 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 65   used to set the
56800 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20  se variables.** 
56810 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f  consistently..*/
56820 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 20  .struct sqlite3 
56830 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
56840 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20  *pVfs;          
56850 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63    /* OS Interfac
56860 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20  e */.  int nDb; 
56870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56880 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
56890 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65  f backends curre
568a0 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20  ntly in use */. 
568b0 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20   Db *aDb;       
568c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
568d0 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a  * All backends *
568e0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20  /.  int flags;  
568f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56900 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f    /* Miscellaneo
56910 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65  us flags. See be
56920 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65  low */.  int ope
56930 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  nFlags;         
56940 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
56950 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
56960 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f  3_vfs.xOpen() */
56970 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20  .  int errCode; 
56980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56990 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
569a0 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49  error code (SQLI
569b0 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65  TE_*) */.  int e
569c0 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  rrMask;         
569d0 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65           /* & re
569e0 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20  sult codes with 
569f0 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74 75  this before retu
56a00 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75  rning */.  u8 au
56a10 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20  toCommit;       
56a20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
56a30 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
56a40 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73  . */.  u8 temp_s
56a50 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20 20  tore;           
56a60 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20       /* 1: file 
56a70 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66  2: memory 0: def
56a80 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c  ault */.  u8 mal
56a90 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20  locFailed;      
56aa0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
56ab0 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e 20  if we have seen 
56ac0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  a malloc failure
56ad0 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63   */.  u8 dfltLoc
56ae0 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  kMode;          
56af0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c      /* Default l
56b00 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20  ocking-mode for 
56b10 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a  attached dbs */.
56b20 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c    u8 dfltJournal
56b30 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Mode;           
56b40 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e  /* Default journ
56b50 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61  al mode for atta
56b60 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69  ched dbs */.  si
56b70 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41 75  gned char nextAu
56b80 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41  tovac;      /* A
56b90 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61  utovac setting a
56ba0 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e  fter VACUUM if >
56bb0 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74  =0 */.  int next
56bc0 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20  Pagesize;       
56bd0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a        /* Pagesiz
56be0 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69  e after VACUUM i
56bf0 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  f >0 */.  int nT
56c00 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  able;           
56c10 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
56c20 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74  r of tables in t
56c30 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
56c40 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43   CollSeq *pDfltC
56c50 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f  oll;           /
56c60 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f  * The default co
56c70 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
56c80 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69   (BINARY) */.  i
56c90 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20  64 lastRowid;   
56ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
56cb0 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65  ROWID of most re
56cc0 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65  cent insert (see
56cd0 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69 36 34   above) */.  i64
56ce0 20 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 3b 20   priorNewRowid; 
56cf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
56d00 73 74 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65  st randomly gene
56d10 72 61 74 65 64 20 52 4f 57 49 44 20 2a 2f 0a 20  rated ROWID */. 
56d20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20   u32 magic;     
56d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
56d40 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66  * Magic number f
56d50 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72  or detect librar
56d60 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e  y misuse */.  in
56d70 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20  t nChange;      
56d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
56d90 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
56da0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
56db0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74  () */.  int nTot
56dc0 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20  alChange;       
56dd0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72        /* Value r
56de0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
56df0 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
56e00 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  () */.  sqlite3_
56e10 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20  mutex *mutex;   
56e20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74        /* Connect
56e30 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69  ion mutex */.  i
56e40 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  nt aLimit[SQLITE
56e50 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20  _N_LIMIT];   /* 
56e60 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75  Limits */.  stru
56e70 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e  ct sqlite3InitIn
56e80 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66  fo {      /* Inf
56e90 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75  ormation used du
56ea0 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
56eb0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ion */.    int i
56ec0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
56ed0 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62         /* When b
56ee0 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69  ack is being ini
56ef0 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20  tialized */.    
56f00 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20  int newTnum;    
56f10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
56f20 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ootpage of table
56f30 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a   being initializ
56f40 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73  ed */.    u8 bus
56f50 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
56f60 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66        /* TRUE if
56f70 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69   currently initi
56f80 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d 20 69  alizing */.  } i
56f90 6e 69 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65  nit;.  int nExte
56fa0 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20  nsion;          
56fb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
56fc0 66 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69  f loaded extensi
56fd0 6f 6e 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a  ons */.  void **
56fe0 61 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20  aExtension;     
56ff0 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
57000 6f 66 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  of shared librar
57010 79 20 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73  y handles */.  s
57020 74 72 75 63 74 20 56 64 62 65 20 2a 70 56 64 62  truct Vdbe *pVdb
57030 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
57040 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 76  List of active v
57050 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20  irtual machines 
57060 2a 2f 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56  */.  int activeV
57070 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20  dbeCnt;         
57080 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
57090 56 44 42 45 73 20 63 75 72 72 65 6e 74 6c 79 20  VDBEs currently 
570a0 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69  executing */.  i
570b0 6e 74 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b  nt writeVdbeCnt;
570c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
570d0 4e 75 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65  Number of active
570e0 20 56 44 42 45 73 20 74 68 61 74 20 61 72 65 20   VDBEs that are 
570f0 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69  writing */.  voi
57100 64 20 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64  d (*xTrace)(void
57110 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20  *,const char*); 
57120 20 20 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20         /* Trace 
57130 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
57140 69 64 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20  id *pTraceArg;  
57150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57160 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
57170 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61 63 65  ent to the trace
57180 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
57190 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28  oid (*xProfile)(
571a0 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
571b0 2a 2c 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66  *,u64);  /* Prof
571c0 69 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a  iling function *
571d0 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69  /.  void *pProfi
571e0 6c 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  leArg;          
571f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
57200 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f   Argument to pro
57210 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  file function */
57220 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74  .  void *pCommit
57230 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
57240 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
57250 20 74 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62   to xCommitCallb
57260 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e  ack() */   .  in
57270 74 20 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62  t (*xCommitCallb
57280 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20  ack)(void*);    
57290 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76  /* Invoked at ev
572a0 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20  ery commit. */. 
572b0 20 76 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b   void *pRollback
572c0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
572d0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
572e0 6f 20 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  o xRollbackCallb
572f0 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f  ack() */   .  vo
57300 69 64 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61  id (*xRollbackCa
57310 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20  llback)(void*); 
57320 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76  /* Invoked at ev
57330 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20  ery commit. */. 
57340 20 76 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72   void *pUpdateAr
57350 67 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64  g;.  void (*xUpd
57360 61 74 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  ateCallback)(voi
57370 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  d*,int, const ch
57380 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
57390 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20  sqlite_int64);. 
573a0 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64   void(*xCollNeed
573b0 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  ed)(void*,sqlite
573c0 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
573d0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
573e0 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65  void(*xCollNeede
573f0 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  d16)(void*,sqlit
57400 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
57410 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20  ,const void*);. 
57420 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64   void *pCollNeed
57430 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33  edArg;.  sqlite3
57440 5f 76 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20  _value *pErr;   
57450 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72         /* Most r
57460 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73  ecent error mess
57470 61 67 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  age */.  char *z
57480 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20  ErrMsg;         
57490 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72         /* Most r
574a0 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73  ecent error mess
574b0 61 67 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64  age (UTF-8 encod
574c0 65 64 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ed) */.  char *z
574d0 45 72 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20  ErrMsg16;       
574e0 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72         /* Most r
574f0 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73  ecent error mess
57500 61 67 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f  age (UTF-16 enco
57510 64 65 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20  ded) */.  union 
57520 7b 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69  {.    volatile i
57530 6e 74 20 69 73 49 6e 74 65 72 72 75 70 74 65 64  nt isInterrupted
57540 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c  ; /* True if sql
57550 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68  ite3_interrupt h
57560 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a  as been called *
57570 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74  /.    double not
57580 55 73 65 64 31 3b 20 20 20 20 20 20 20 20 20 20  Used1;          
57590 20 20 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20    /* Spacer */. 
575a0 20 7d 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69   } u1;.  Lookasi
575b0 64 65 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20  de lookaside;   
575c0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73         /* Lookas
575d0 69 64 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69  ide malloc confi
575e0 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e  guration */.#ifn
575f0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
57600 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
57610 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69  int (*xAuth)(voi
57620 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
57630 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
57640 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
57650 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20   char*);.       
57660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57670 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65           /* Acce
57680 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ss authorization
57690 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
576a0 6f 69 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20  oid *pAuthArg;  
576b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
576c0 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  1st argument to 
576d0 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68 20  the access auth 
576e0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64  function */.#end
576f0 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
57700 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f  E_OMIT_PROGRESS_
57710 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28  CALLBACK.  int (
57720 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64  *xProgress)(void
57730 20 2a 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20   *);     /* The 
57740 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
57750 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72  k */.  void *pPr
57760 6f 67 72 65 73 73 41 72 67 3b 20 20 20 20 20 20  ogressArg;      
57770 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
57780 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73   to the progress
57790 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69   callback */.  i
577a0 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b  nt nProgressOps;
577b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
577c0 4e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65  Number of opcode
577d0 73 20 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63  s for progress c
577e0 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69  allback */.#endi
577f0 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
57800 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
57810 4c 45 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c  LE.  Hash aModul
57820 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
57830 20 20 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20     /* populated 
57840 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  by sqlite3_creat
57850 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20  e_module() */.  
57860 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20  Table *pVTab;   
57870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
57880 20 76 74 61 62 20 77 69 74 68 20 61 63 74 69 76   vtab with activ
57890 65 20 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65  e Connect/Create
578a0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 71 6c   method */.  sql
578b0 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72  ite3_vtab **aVTr
578c0 61 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 56 69  ans;       /* Vi
578d0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74  rtual tables wit
578e0 68 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69  h open transacti
578f0 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54  ons */.  int nVT
57900 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20  rans;           
57910 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61         /* Alloca
57920 74 65 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72  ted size of aVTr
57930 61 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ans */.#endif.  
57940 46 75 6e 63 44 65 66 48 61 73 68 20 61 46 75 6e  FuncDefHash aFun
57950 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c;            /*
57960 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 63   Hash table of c
57970 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69  onnection functi
57980 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20 61 43  ons */.  Hash aC
57990 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 20 20  ollSeq;         
579a0 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 6f         /* All co
579b0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
579c0 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c  s */.  BusyHandl
579d0 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72 3b 20  er busyHandler; 
579e0 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63 61 6c       /* Busy cal
579f0 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 62  lback */.  int b
57a00 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 20  usyTimeout;     
57a10 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 73 79           /* Busy
57a20 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 74   handler timeout
57a30 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20 44  , in msec */.  D
57a40 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b 20  b aDbStatic[2]; 
57a50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
57a60 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f 72  Static space for
57a70 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20 62   the 2 default b
57a80 61 63 6b 65 6e 64 73 20 2a 2f 0a 23 69 66 64 65  ackends */.#ifde
57a90 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 73  f SQLITE_SSE.  s
57aa0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 65  qlite3_stmt *pFe
57ab0 74 63 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  tch;         /* 
57ac0 55 73 65 64 20 62 79 20 53 53 45 20 74 6f 20 66  Used by SSE to f
57ad0 65 74 63 68 20 73 74 6f 72 65 64 20 73 74 61 74  etch stored stat
57ae0 65 6d 65 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66  ements */.#endif
57af0 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53  .  Savepoint *pS
57b00 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20  avepoint;       
57b10 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69   /* List of acti
57b20 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f  ve savepoints */
57b30 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e  .  int nSavepoin
57b40 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
57b50 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
57b60 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61  n-transaction sa
57b70 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  vepoints */.  in
57b80 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20  t nStatement;   
57b90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
57ba0 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20  umber of nested 
57bb0 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61  statement-transa
57bc0 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20  ctions  */.  u8 
57bd0 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76  isTransactionSav
57be0 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72  epoint;    /* Tr
57bf0 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d  ue if the outerm
57c00 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73  ost savepoint is
57c10 20 61 20 54 53 20 2a 2f 0a 0a 23 69 66 64 65 66   a TS */..#ifdef
57c20 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55   SQLITE_ENABLE_U
57c30 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f  NLOCK_NOTIFY.  /
57c40 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
57c50 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c  variables are al
57c60 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74  l protected by t
57c70 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52  he STATIC_MASTER
57c80 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f   .  ** mutex, no
57c90 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74  t by sqlite3.mut
57ca0 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 73 65  ex. They are use
57cb0 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74  d by code in not
57cc0 69 66 79 2e 63 2e 20 0a 20 20 2a 2f 0a 20 20 73  ify.c. .  */.  s
57cd0 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e  qlite3 *pBlockin
57ce0 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20  gConnection; /* 
57cf0 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  Connection that 
57d00 63 61 75 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f  caused SQLITE_LO
57d10 43 4b 45 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65  CKED */.  sqlite
57d20 33 20 2a 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  3 *pUnlockConnec
57d30 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  tion;           
57d40 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  /* Connection to
57d50 20 77 61 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63   watch for unloc
57d60 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e  k */.  void *pUn
57d70 6c 6f 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20  lockArg;        
57d80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
57d90 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c  Argument to xUnl
57da0 6f 63 6b 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76  ockNotify */.  v
57db0 6f 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74  oid (*xUnlockNot
57dc0 69 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e  ify)(void **, in
57dd0 74 29 3b 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e  t);  /* Unlock n
57de0 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a  otify callback *
57df0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 4e 65  /.  sqlite3 *pNe
57e00 78 74 42 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20  xtBlocked;      
57e10 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73    /* Next in lis
57e20 74 20 6f 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64  t of all blocked
57e30 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a   connections */.
57e40 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
57e50 20 41 20 6d 61 63 72 6f 20 74 6f 20 64 69 73 63   A macro to disc
57e60 6f 76 65 72 20 74 68 65 20 65 6e 63 6f 64 69 6e  over the encodin
57e70 67 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e  g of a database.
57e80 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 4e 43 28  .*/.#define ENC(
57e90 64 62 29 20 28 28 64 62 29 2d 3e 61 44 62 5b 30  db) ((db)->aDb[0
57ea0 5d 2e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a  ].pSchema->enc).
57eb0 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20  ./*.** Possible 
57ec0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73  values for the s
57ed0 71 6c 69 74 65 2e 66 6c 61 67 73 20 61 6e 64 20  qlite.flags and 
57ee0 6f 72 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c  or Db.flags fiel
57ef0 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c  ds..**.** On sql
57f00 69 74 65 2e 66 6c 61 67 73 2c 20 74 68 65 20 53  ite.flags, the S
57f10 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 76 61  QLITE_InTrans va
57f20 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 77  lue means that w
57f30 65 20 68 61 76 65 0a 2a 2a 20 65 78 65 63 75 74  e have.** execut
57f40 65 64 20 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20  ed a BEGIN.  On 
57f50 44 62 2e 66 6c 61 67 73 2c 20 53 51 4c 49 54 45  Db.flags, SQLITE
57f60 5f 49 6e 54 72 61 6e 73 20 6d 65 61 6e 73 20 61  _InTrans means a
57f70 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72   statement.** tr
57f80 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
57f90 69 76 65 20 6f 6e 20 74 68 61 74 20 70 61 72 74  ive on that part
57fa0 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20  icular database 
57fb0 66 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  file..*/.#define
57fc0 20 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63   SQLITE_VdbeTrac
57fd0 65 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30  e      0x0000000
57fe0 31 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72  1  /* True to tr
57ff0 61 63 65 20 56 44 42 45 20 65 78 65 63 75 74 69  ace VDBE executi
58000 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  on */.#define SQ
58010 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 20 20 20  LITE_InTrans    
58020 20 20 20 20 30 78 30 30 30 30 30 30 30 38 20 20      0x00000008  
58030 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20  /* True if in a 
58040 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23  transaction */.#
58050 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e  define SQLITE_In
58060 74 65 72 6e 43 68 61 6e 67 65 73 20 20 30 78 30  ternChanges  0x0
58070 30 30 30 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f  0000010  /* Unco
58080 6d 6d 69 74 74 65 64 20 48 61 73 68 20 74 61 62  mmitted Hash tab
58090 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64  le changes */.#d
580a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c  efine SQLITE_Ful
580b0 6c 43 6f 6c 4e 61 6d 65 73 20 20 20 30 78 30 30  lColNames   0x00
580c0 30 30 30 30 32 30 20 20 2f 2a 20 53 68 6f 77 20  000020  /* Show 
580d0 66 75 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  full column name
580e0 73 20 6f 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23  s on SELECT */.#
580f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 68  define SQLITE_Sh
58100 6f 72 74 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30  ortColNames  0x0
58110 30 30 30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77  0000040  /* Show
58120 20 73 68 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e   short columns n
58130 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ames */.#define 
58140 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
58150 20 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30        0x00000080
58160 20 20 2f 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20    /* Count rows 
58170 63 68 61 6e 67 65 64 20 62 79 20 49 4e 53 45 52  changed by INSER
58180 54 2c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  T, */.          
58190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
581a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
581b0 2f 2a 20 20 20 44 45 4c 45 54 45 2c 20 6f 72 20  /*   DELETE, or 
581c0 55 50 44 41 54 45 20 61 6e 64 20 72 65 74 75 72  UPDATE and retur
581d0 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  n */.           
581e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
581f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
58200 2a 20 20 20 74 68 65 20 63 6f 75 6e 74 20 75 73  *   the count us
58210 69 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20  ing a callback. 
58220 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
58230 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20  E_NullCallback  
58240 20 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20   0x00000100  /* 
58250 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  Invoke the callb
58260 61 63 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20  ack once if the 
58270 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
58280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58290 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
582a0 20 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20    result set is 
582b0 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65  empty */.#define
582c0 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65   SQLITE_SqlTrace
582d0 20 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30         0x0000020
582e0 30 20 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e  0  /* Debug prin
582f0 74 20 53 51 4c 20 61 73 20 69 74 20 65 78 65 63  t SQL as it exec
58300 75 74 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  utes */.#define 
58310 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69  SQLITE_VdbeListi
58320 6e 67 20 20 20 20 30 78 30 30 30 30 30 34 30 30  ng    0x00000400
58330 20 20 2f 2a 20 44 65 62 75 67 20 6c 69 73 74 69    /* Debug listi
58340 6e 67 73 20 6f 66 20 56 44 42 45 20 70 72 6f 67  ngs of VDBE prog
58350 72 61 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rams */.#define 
58360 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65  SQLITE_WriteSche
58370 6d 61 20 20 20 20 30 78 30 30 30 30 30 38 30 30  ma    0x00000800
58380 20 20 2f 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74    /* OK to updat
58390 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  e SQLITE_MASTER 
583a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
583b0 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20  E_NoReadlock    
583c0 20 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20   0x00001000  /* 
583d0 52 65 61 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d  Readlocks are om
583e0 69 74 74 65 64 20 77 68 65 6e 20 0a 20 20 20 20  itted when .    
583f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58410 20 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 69        ** accessi
58420 6e 67 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74  ng read-only dat
58430 61 62 61 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e  abases */.#defin
58440 65 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43  e SQLITE_IgnoreC
58450 68 65 63 6b 73 20 20 20 30 78 30 30 30 30 32 30  hecks   0x000020
58460 30 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e  00  /* Do not en
58470 66 6f 72 63 65 20 63 68 65 63 6b 20 63 6f 6e 73  force check cons
58480 74 72 61 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69  traints */.#defi
58490 6e 65 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e  ne SQLITE_ReadUn
584a0 63 6f 6d 6d 69 74 74 65 64 20 30 78 30 30 30 30  committed 0x0000
584b0 34 30 30 30 20 2f 2a 20 46 6f 72 20 73 68 61 72  4000 /* For shar
584c0 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f  ed-cache mode */
584d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
584e0 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 20 20 30  LegacyFileFmt  0
584f0 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 43 72  x00008000  /* Cr
58500 65 61 74 65 20 6e 65 77 20 64 61 74 61 62 61 73  eate new databas
58510 65 73 20 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a  es in format 1 *
58520 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
58530 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 20  _FullFSync      
58540 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 55  0x00010000  /* U
58550 73 65 20 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e  se full fsync on
58560 20 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a   the backend */.
58570 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
58580 6f 61 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78  oadExtension  0x
58590 30 30 30 32 30 30 30 30 20 20 2f 2a 20 45 6e 61  00020000  /* Ena
585a0 62 6c 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ble load_extensi
585b0 6f 6e 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53  on */..#define S
585c0 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f  QLITE_RecoveryMo
585d0 64 65 20 20 20 30 78 30 30 30 34 30 30 30 30 20  de   0x00040000 
585e0 20 2f 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d   /* Ignore schem
585f0 61 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66  a errors */.#def
58600 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 61 72 65  ine SQLITE_Share
58610 64 43 61 63 68 65 20 20 20 20 30 78 30 30 30 38  dCache    0x0008
58620 30 30 30 30 20 20 2f 2a 20 43 61 63 68 65 20 73  0000  /* Cache s
58630 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65  haring is enable
58640 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
58650 49 54 45 5f 56 74 61 62 20 20 20 20 20 20 20 20  ITE_Vtab        
58660 20 20 20 30 78 30 30 31 30 30 30 30 30 20 20 2f     0x00100000  /
58670 2a 20 54 68 65 72 65 20 65 78 69 73 74 73 20 61  * There exists a
58680 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
58690 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
586a0 5f 43 6f 6d 6d 69 74 42 75 73 79 20 20 20 20 20  _CommitBusy     
586b0 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 49  0x00200000  /* I
586c0 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66  n the process of
586d0 20 63 6f 6d 6d 69 74 74 69 6e 67 20 2a 2f 0a 23   committing */.#
586e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65  define SQLITE_Re
586f0 76 65 72 73 65 4f 72 64 65 72 20 20 20 30 78 30  verseOrder   0x0
58700 30 34 30 30 30 30 30 20 20 2f 2a 20 52 65 76 65  0400000  /* Reve
58710 72 73 65 20 75 6e 6f 72 64 65 72 65 64 20 53 45  rse unordered SE
58720 4c 45 43 54 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  LECTs */../*.** 
58730 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
58740 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 6d  for the sqlite.m
58750 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 54  agic field..** T
58760 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f  he numbers are o
58770 62 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64 6f  btained at rando
58780 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 70  m and have no sp
58790 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f  ecial meaning, o
587a0 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 69  ther.** than bei
587b0 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d  ng distinct from
587c0 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f   one another..*/
587d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
587e0 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 30  MAGIC_OPEN     0
587f0 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44 61  xa029a697  /* Da
58800 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20 2a  tabase is open *
58810 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
58820 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 20  _MAGIC_CLOSED   
58830 30 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20 44  0x9f3c2d33  /* D
58840 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65  atabase is close
58850 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
58860 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 20  ITE_MAGIC_SICK  
58870 20 20 20 30 78 34 62 37 37 31 32 39 30 20 20 2f     0x4b771290  /
58880 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 77 61 69  * Error and awai
58890 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 64  ting close */.#d
588a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
588b0 49 43 5f 42 55 53 59 20 20 20 20 20 30 78 66 30  IC_BUSY     0xf0
588c0 33 62 37 39 30 36 20 20 2f 2a 20 44 61 74 61 62  3b7906  /* Datab
588d0 61 73 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  ase currently in
588e0 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20   use */.#define 
588f0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52  SQLITE_MAGIC_ERR
58900 4f 52 20 20 20 20 30 78 62 35 33 35 37 39 33 30  OR    0xb5357930
58910 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4d    /* An SQLITE_M
58920 49 53 55 53 45 20 65 72 72 6f 72 20 6f 63 63 75  ISUSE error occu
58930 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45  rred */../*.** E
58940 61 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ach SQL function
58950 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61   is defined by a
58960 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
58970 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73  e following.** s
58980 74 72 75 63 74 75 72 65 2e 20 20 41 20 70 6f 69  tructure.  A poi
58990 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
589a0 75 63 74 75 72 65 20 69 73 20 73 74 6f 72 65 64  ucture is stored
589b0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61   in the sqlite.a
589c0 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 62  Func.** hash tab
589d0 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 70  le.  When multip
589e0 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76  le functions hav
589f0 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c  e the same name,
58a00 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a   the hash table.
58a10 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6c  ** points to a l
58a20 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68  inked list of th
58a30 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a  ese structures..
58a40 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65  */.struct FuncDe
58a50 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b 20  f {.  i16 nArg; 
58a60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
58a70 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
58a80 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c  s.  -1 means unl
58a90 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 69  imited */.  u8 i
58aa0 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20 20  PrefEnc;        
58ab0 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65   /* Preferred te
58ac0 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 4c  xt encoding (SQL
58ad0 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c 20  ITE_UTF8, 16LE, 
58ae0 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 6c  16BE) */.  u8 fl
58af0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
58b00 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74  /* Some combinat
58b10 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 55  ion of SQLITE_FU
58b20 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  NC_* */.  void *
58b30 70 55 73 65 72 44 61 74 61 3b 20 20 20 20 20 2f  pUserData;     /
58b40 2a 20 55 73 65 72 20 64 61 74 61 20 70 61 72 61  * User data para
58b50 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 44  meter */.  FuncD
58b60 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  ef *pNext;      
58b70 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69 6f 6e  /* Next function
58b80 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 20   with same name 
58b90 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  */.  void (*xFun
58ba0 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
58bb0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
58bc0 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 67  value**); /* Reg
58bd0 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ular function */
58be0 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29  .  void (*xStep)
58bf0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
58c00 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
58c10 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 65  lue**); /* Aggre
58c20 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 76  gate step */.  v
58c30 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 29  oid (*xFinalize)
58c40 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
58c50 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *);             
58c60 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20     /* Aggregate 
58c70 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 63  finalizer */.  c
58c80 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
58c90 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 20      /* SQL name 
58ca0 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  of the function.
58cb0 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70   */.  FuncDef *p
58cc0 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e 65  Hash;      /* Ne
58cd0 78 74 20 77 69 74 68 20 61 20 64 69 66 66 65 72  xt with a differ
58ce0 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68 65  ent name but the
58cf0 20 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d 3b   same hash */.};
58d00 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65  ../*.** Possible
58d10 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63   values for Func
58d20 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65  Def.flags.*/.#de
58d30 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43  fine SQLITE_FUNC
58d40 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20 2f  _LIKE     0x01 /
58d50 2a 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72 20  * Candidate for 
58d60 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a  the LIKE optimiz
58d70 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ation */.#define
58d80 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53   SQLITE_FUNC_CAS
58d90 45 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43 61  E     0x02 /* Ca
58da0 73 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 4b  se-sensitive LIK
58db0 45 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e 20  E-type function 
58dc0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
58dd0 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 20  E_FUNC_EPHEM    
58de0 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 65 72 61  0x04 /* Ephemera
58df0 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68 20  l.  Delete with 
58e00 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20  VDBE */.#define 
58e10 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
58e20 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 6c  COLL 0x08 /* sql
58e30 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
58e40 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63 61  eq() might be ca
58e50 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  lled */.#define 
58e60 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49 56  SQLITE_FUNC_PRIV
58e70 41 54 45 20 20 30 78 31 30 20 2f 2a 20 41 6c 6c  ATE  0x10 /* All
58e80 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61  owed for interna
58e90 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64  l use only */.#d
58ea0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
58eb0 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 32 30 20  C_COUNT    0x20 
58ec0 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 6e  /* Built-in coun
58ed0 74 28 2a 29 20 61 67 67 72 65 67 61 74 65 20 2a  t(*) aggregate *
58ee0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  /../*.** The fol
58ef0 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 6d 61 63  lowing three mac
58f00 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e 28 29 2c  ros, FUNCTION(),
58f10 20 4c 49 4b 45 46 55 4e 43 28 29 20 61 6e 64 20   LIKEFUNC() and 
58f20 41 47 47 52 45 47 41 54 45 28 29 20 61 72 65 0a  AGGREGATE() are.
58f30 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  ** used to creat
58f40 65 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65  e the initialize
58f50 72 73 20 66 6f 72 20 74 68 65 20 46 75 6e 63 44  rs for the FuncD
58f60 65 66 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a  ef structures..*
58f70 2a 0a 2a 2a 20 20 20 46 55 4e 43 54 49 4f 4e 28  *.**   FUNCTION(
58f80 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72  zName, nArg, iAr
58f90 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 0a 2a  g, bNC, xFunc).*
58fa0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72  *     Used to cr
58fb0 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75  eate a scalar fu
58fc0 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
58fd0 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20  n of a function 
58fe0 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 69 6d  zName .**     im
58ff0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 43 20 66  plemented by C f
59000 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63 20 74 68  unction xFunc th
59010 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20  at accepts nArg 
59020 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 0a 2a  arguments. The.*
59030 2a 20 20 20 20 20 76 61 6c 75 65 20 70 61 73 73  *     value pass
59040 65 64 20 61 73 20 69 41 72 67 20 69 73 20 63 61  ed as iArg is ca
59050 73 74 20 74 6f 20 61 20 28 76 6f 69 64 2a 29 20  st to a (void*) 
59060 61 6e 64 20 6d 61 64 65 20 61 76 61 69 6c 61 62  and made availab
59070 6c 65 0a 2a 2a 20 20 20 20 20 61 73 20 74 68 65  le.**     as the
59080 20 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69   user-data (sqli
59090 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29  te3_user_data())
590a0 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f   for the functio
590b0 6e 2e 20 49 66 20 0a 2a 2a 20 20 20 20 20 61 72  n. If .**     ar
590c0 67 75 6d 65 6e 74 20 62 4e 43 20 69 73 20 74 72  gument bNC is tr
590d0 75 65 2c 20 74 68 65 6e 20 74 68 65 20 46 75 6e  ue, then the Fun
590e0 63 44 65 66 2e 6e 65 65 64 43 6f 6c 6c 61 74 65  cDef.needCollate
590f0 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a   flag is set..**
59100 0a 2a 2a 20 20 20 41 47 47 52 45 47 41 54 45 28  .**   AGGREGATE(
59110 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72  zName, nArg, iAr
59120 67 2c 20 62 4e 43 2c 20 78 53 74 65 70 2c 20 78  g, bNC, xStep, x
59130 46 69 6e 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73  Final).**     Us
59140 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20  ed to create an 
59150 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
59160 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d  on definition im
59170 70 6c 65 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20  plemented by.** 
59180 20 20 20 20 74 68 65 20 43 20 66 75 6e 63 74 69      the C functi
59190 6f 6e 73 20 78 53 74 65 70 20 61 6e 64 20 78 46  ons xStep and xF
591a0 69 6e 61 6c 2e 20 54 68 65 20 66 69 72 73 74 20  inal. The first 
591b0 66 6f 75 72 20 70 61 72 61 6d 65 74 65 72 73 0a  four parameters.
591c0 2a 2a 20 20 20 20 20 61 72 65 20 69 6e 74 65 72  **     are inter
591d0 70 72 65 74 65 64 20 69 6e 20 74 68 65 20 73 61  preted in the sa
591e0 6d 65 20 77 61 79 20 61 73 20 74 68 65 20 66 69  me way as the fi
591f0 72 73 74 20 34 20 70 61 72 61 6d 65 74 65 72 73  rst 4 parameters
59200 20 74 6f 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54   to.**     FUNCT
59210 49 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c  ION()..**.**   L
59220 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e  IKEFUNC(zName, n
59230 41 72 67 2c 20 70 41 72 67 2c 20 66 6c 61 67 73  Arg, pArg, flags
59240 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f  ).**     Used to
59250 20 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72   create a scalar
59260 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
59270 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69  tion of a functi
59280 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20  on zName .**    
59290 20 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41   that accepts nA
592a0 72 67 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64  rg arguments and
592b0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
592c0 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a  by a call to C .
592d0 2a 2a 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20  **     function 
592e0 6c 69 6b 65 46 75 6e 63 2e 20 41 72 67 75 6d 65  likeFunc. Argume
592f0 6e 74 20 70 41 72 67 20 69 73 20 63 61 73 74 20  nt pArg is cast 
59300 74 6f 20 61 20 28 76 6f 69 64 20 2a 29 20 61 6e  to a (void *) an
59310 64 20 6d 61 64 65 0a 2a 2a 20 20 20 20 20 61 76  d made.**     av
59320 61 69 6c 61 62 6c 65 20 61 73 20 74 68 65 20 66  ailable as the f
59330 75 6e 63 74 69 6f 6e 20 75 73 65 72 2d 64 61 74  unction user-dat
59340 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  a (sqlite3_user_
59350 64 61 74 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20  data()). The.** 
59360 20 20 20 20 46 75 6e 63 44 65 66 2e 66 6c 61 67      FuncDef.flag
59370 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  s variable is se
59380 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70  t to the value p
59390 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6c 61  assed as the fla
593a0 67 73 0a 2a 2a 20 20 20 20 20 70 61 72 61 6d 65  gs.**     parame
593b0 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ter..*/.#define 
593c0 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20  FUNCTION(zName, 
593d0 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c  nArg, iArg, bNC,
593e0 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72   xFunc) \.  {nAr
593f0 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g, SQLITE_UTF8, 
59400 62 4e 43 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e  bNC*8, SQLITE_IN
59410 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20  T_TO_PTR(iArg), 
59420 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20  0, xFunc, 0, 0, 
59430 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69  #zName, 0}.#defi
59440 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28  ne STR_FUNCTION(
59450 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72  zName, nArg, pAr
59460 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c  g, bNC, xFunc) \
59470 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45  .  {nArg, SQLITE
59480 5f 55 54 46 38 2c 20 62 4e 43 2a 38 2c 20 70 41  _UTF8, bNC*8, pA
59490 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c  rg, 0, xFunc, 0,
594a0 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23   0, #zName, 0}.#
594b0 64 65 66 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28  define LIKEFUNC(
594c0 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67  zName, nArg, arg
594d0 2c 20 66 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41  , flags) \.  {nA
594e0 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  rg, SQLITE_UTF8,
594f0 20 66 6c 61 67 73 2c 20 28 76 6f 69 64 20 2a 29   flags, (void *)
59500 61 72 67 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63  arg, 0, likeFunc
59510 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20  , 0, 0, #zName, 
59520 30 7d 0a 23 64 65 66 69 6e 65 20 41 47 47 52 45  0}.#define AGGRE
59530 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67  GATE(zName, nArg
59540 2c 20 61 72 67 2c 20 6e 63 2c 20 78 53 74 65 70  , arg, nc, xStep
59550 2c 20 78 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e  , xFinal) \.  {n
59560 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Arg, SQLITE_UTF8
59570 2c 20 6e 63 2a 38 2c 20 53 51 4c 49 54 45 5f 49  , nc*8, SQLITE_I
59580 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20  NT_TO_PTR(arg), 
59590 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e  0, 0, xStep,xFin
595a0 61 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a  al,#zName,0}../*
595b0 0a 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20  .** All current 
595c0 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73  savepoints are s
595d0 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65  tored in a linke
595e0 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20  d list starting 
595f0 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53  at.** sqlite3.pS
59600 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69  avepoint. The fi
59610 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74  rst element in t
59620 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d  he list is the m
59630 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20  ost recently.** 
59640 6f 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74  opened savepoint
59650 2e 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65  . Savepoints are
59660 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69   added to the li
59670 73 74 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a  st by the vdbe.*
59680 2a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69  * OP_Savepoint i
59690 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  nstruction..*/.s
596a0 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20  truct Savepoint 
596b0 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
596c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
596d0 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70          /* Savep
596e0 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74  oint name (nul-t
596f0 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20  erminated) */.  
59700 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74  Savepoint *pNext
59710 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
59720 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 73 61      /* Parent sa
59730 76 65 70 6f 69 6e 74 20 28 69 66 20 61 6e 79 29  vepoint (if any)
59740 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
59750 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
59760 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  used as the seco
59770 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
59780 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74  sqlite3Savepoint
59790 28 29 2c 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68  (),.** and as th
597a0 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f  e P1 argument to
597b0 20 74 68 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e   the OP_Savepoin
597c0 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
597d0 2f 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f  /.#define SAVEPO
597e0 49 4e 54 5f 42 45 47 49 4e 20 20 20 20 20 20 30  INT_BEGIN      0
597f0 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49  .#define SAVEPOI
59800 4e 54 5f 52 45 4c 45 41 53 45 20 20 20 20 31 0a  NT_RELEASE    1.
59810 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e  #define SAVEPOIN
59820 54 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a  T_ROLLBACK   2..
59830 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69  ./*.** Each SQLi
59840 74 65 20 6d 6f 64 75 6c 65 20 28 76 69 72 74 75  te module (virtu
59850 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74  al table definit
59860 69 6f 6e 29 20 69 73 20 64 65 66 69 6e 65 64 20  ion) is defined 
59870 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63  by an.** instanc
59880 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
59890 6e 67 20 73 74 72 75 63 74 75 72 65 2c 20 73 74  ng structure, st
598a0 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69  ored in the sqli
598b0 74 65 33 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68  te3.aModule.** h
598c0 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ash table..*/.st
598d0 72 75 63 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20  ruct Module {.  
598e0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
598f0 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20  dule *pModule;  
59900 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b       /* Callback
59910 20 70 6f 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63   pointers */.  c
59920 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
59930 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
59940 20 20 20 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73      /* Name pass
59950 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64  ed to create_mod
59960 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20  ule() */.  void 
59970 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20  *pAux;          
59980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59990 2f 2a 20 70 41 75 78 20 70 61 73 73 65 64 20 74  /* pAux passed t
599a0 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28  o create_module(
599b0 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  ) */.  void (*xD
599c0 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b  estroy)(void *);
599d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
599e0 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72  odule destructor
599f0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a   function */.};.
59a00 0a 2f 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  ./*.** informati
59a10 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f  on about each co
59a20 6c 75 6d 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74  lumn of an SQL t
59a30 61 62 6c 65 20 69 73 20 68 65 6c 64 20 69 6e 20  able is held in 
59a40 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f  an instance.** o
59a50 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
59a60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75  ..*/.struct Colu
59a70 6d 6e 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  mn {.  char *zNa
59a80 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  me;     /* Name 
59a90 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a  of this column *
59aa0 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b  /.  Expr *pDflt;
59ab0 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
59ac0 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f  value of this co
59ad0 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  lumn */.  char *
59ae0 7a 54 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 61  zType;     /* Da
59af0 74 61 20 74 79 70 65 20 66 6f 72 20 74 68 69 73  ta type for this
59b00 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61   column */.  cha
59b10 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a  r *zColl;     /*
59b20 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
59b30 6e 63 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75  nce.  If NULL, u
59b40 73 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a  se the default *
59b50 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20  /.  u8 notNull; 
59b60 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
59b70 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e  there is a NOT N
59b80 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a  ULL constraint *
59b90 2f 0a 20 20 75 38 20 69 73 50 72 69 6d 4b 65 79  /.  u8 isPrimKey
59ba0 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  ;    /* True if 
59bb0 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 70  this column is p
59bc0 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41  art of the PRIMA
59bd0 52 59 20 4b 45 59 20 2a 2f 0a 20 20 63 68 61 72  RY KEY */.  char
59be0 20 61 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20   affinity;   /* 
59bf0 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54  One of the SQLIT
59c00 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73  E_AFF_... values
59c10 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
59c20 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
59c30 41 42 4c 45 0a 20 20 75 38 20 69 73 48 69 64 64  ABLE.  u8 isHidd
59c40 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  en;     /* True 
59c50 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69  if this column i
59c60 73 20 27 68 69 64 64 65 6e 27 20 2a 2f 0a 23 65  s 'hidden' */.#e
59c70 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ndif.};../*.** A
59c80 20 22 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75   "Collating Sequ
59c90 65 6e 63 65 22 20 69 73 20 64 65 66 69 6e 65 64  ence" is defined
59ca0 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   by an instance 
59cb0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
59cc0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 43  .** structure. C
59cd0 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20 61 20 63  onceptually, a c
59ce0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
59cf0 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  e consists of a 
59d00 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 61 20 63 6f  name and.** a co
59d10 6d 70 61 72 69 73 6f 6e 20 72 6f 75 74 69 6e 65  mparison routine
59d20 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
59d30 65 20 6f 72 64 65 72 20 6f 66 20 74 68 61 74 20  e order of that 
59d40 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  sequence..**.** 
59d50 54 68 65 72 65 20 6d 61 79 20 74 77 6f 20 73 65  There may two se
59d60 70 61 72 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74  parate implement
59d70 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 63 6f  ations of the co
59d80 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  llation function
59d90 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 70 72  , one.** that pr
59da0 6f 63 65 73 73 65 73 20 74 65 78 74 20 69 6e 20  ocesses text in 
59db0 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 28  UTF-8 encoding (
59dc0 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 29 20 61 6e  CollSeq.xCmp) an
59dd0 64 20 61 6e 6f 74 68 65 72 20 74 68 61 74 0a 2a  d another that.*
59de0 2a 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 74  * processes text
59df0 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
59e00 31 36 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70  16 (CollSeq.xCmp
59e10 31 36 29 2c 20 75 73 69 6e 67 20 74 68 65 20 6d  16), using the m
59e20 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 65  achine.** native
59e30 20 62 79 74 65 20 6f 72 64 65 72 2e 20 57 68 65   byte order. Whe
59e40 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  n a collation se
59e50 71 75 65 6e 63 65 20 69 73 20 69 6e 76 6f 6b 65  quence is invoke
59e60 64 2c 20 53 51 4c 69 74 65 20 73 65 6c 65 63 74  d, SQLite select
59e70 73 0a 2a 2a 20 74 68 65 20 76 65 72 73 69 6f 6e  s.** the version
59e80 20 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69   that will requi
59e90 72 65 20 74 68 65 20 6c 65 61 73 74 20 65 78 70  re the least exp
59ea0 65 6e 73 69 76 65 20 65 6e 63 6f 64 69 6e 67 0a  ensive encoding.
59eb0 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73 2c  ** translations,
59ec0 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54   if any..**.** T
59ed0 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72  he CollSeq.pUser
59ee0 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65   member variable
59ef0 20 69 73 20 61 6e 20 65 78 74 72 61 20 70 61 72   is an extra par
59f00 61 6d 65 74 65 72 20 74 68 61 74 20 70 61 73 73  ameter that pass
59f10 65 64 20 69 6e 0a 2a 2a 20 61 73 20 74 68 65 20  ed in.** as the 
59f20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
59f30 6f 20 74 68 65 20 55 54 46 2d 38 20 63 6f 6d 70  o the UTF-8 comp
59f40 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c  arison function,
59f50 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65   xCmp..** CollSe
59f60 71 2e 70 55 73 65 72 31 36 20 69 73 20 74 68 65  q.pUser16 is the
59f70 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 20   equivalent for 
59f80 74 68 65 20 55 54 46 2d 31 36 20 63 6f 6d 70 61  the UTF-16 compa
59f90 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a  rison function,.
59fa0 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a  ** xCmp16..**.**
59fb0 20 49 66 20 62 6f 74 68 20 43 6f 6c 6c 53 65 71   If both CollSeq
59fc0 2e 78 43 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 65  .xCmp and CollSe
59fd0 71 2e 78 43 6d 70 31 36 20 61 72 65 20 4e 55 4c  q.xCmp16 are NUL
59fe0 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  L, it means that
59ff0 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e   the.** collatin
5a000 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 75 6e  g sequence is un
5a010 64 65 66 69 6e 65 64 2e 20 20 49 6e 64 69 63 65  defined.  Indice
5a020 73 20 62 75 69 6c 74 20 6f 6e 20 61 6e 20 75 6e  s built on an un
5a030 64 65 66 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61  defined.** colla
5a040 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6d 61  ting sequence ma
5a050 79 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72  y not be read or
5a060 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72   written..*/.str
5a070 75 63 74 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 20  uct CollSeq {.  
5a080 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
5a090 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
5a0a0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
5a0b0 65 71 75 65 6e 63 65 2c 20 55 54 46 2d 38 20 65  equence, UTF-8 e
5a0c0 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20 65  ncoded */.  u8 e
5a0d0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
5a0e0 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69    /* Text encodi
5a0f0 6e 67 20 68 61 6e 64 6c 65 64 20 62 79 20 78 43  ng handled by xC
5a100 6d 70 28 29 20 2a 2f 0a 20 20 75 38 20 74 79 70  mp() */.  u8 typ
5a110 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
5a120 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51  /* One of the SQ
5a130 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61  LITE_COLL_... va
5a140 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  lues below */.  
5a150 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20 20  void *pUser;    
5a160 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
5a170 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6d 70 28  rgument to xCmp(
5a180 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d  ) */.  int (*xCm
5a190 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f  p)(void*,int, co
5a1a0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  nst void*, int, 
5a1b0 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20  const void*);.  
5a1c0 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
5a1d0 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63  d*);  /* Destruc
5a1e0 74 6f 72 20 66 6f 72 20 70 55 73 65 72 20 2a 2f  tor for pUser */
5a1f0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77  .};../*.** Allow
5a200 65 64 20 76 61 6c 75 65 73 20 6f 66 20 43 6f 6c  ed values of Col
5a210 6c 53 65 71 2e 74 79 70 65 3a 0a 2a 2f 0a 23 64  lSeq.type:.*/.#d
5a220 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c  efine SQLITE_COL
5a230 4c 5f 42 49 4e 41 52 59 20 20 31 20 20 2f 2a 20  L_BINARY  1  /* 
5a240 54 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d 63  The default memc
5a250 6d 70 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20 73  mp() collating s
5a260 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69  equence */.#defi
5a270 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e  ne SQLITE_COLL_N
5a280 4f 43 41 53 45 20 20 32 20 20 2f 2a 20 54 68 65  OCASE  2  /* The
5a290 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 45   built-in NOCASE
5a2a0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5a2b0 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nce */.#define S
5a2c0 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 52  QLITE_COLL_REVER
5a2d0 53 45 20 33 20 20 2f 2a 20 54 68 65 20 62 75 69  SE 3  /* The bui
5a2e0 6c 74 2d 69 6e 20 52 45 56 45 52 53 45 20 63 6f  lt-in REVERSE co
5a2f0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
5a300 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5a310 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20 20  TE_COLL_USER    
5a320 30 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20  0  /* Any other 
5a330 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c  user-defined col
5a340 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
5a350 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74  */../*.** A sort
5a360 20 6f 72 64 65 72 20 63 61 6e 20 62 65 20 65 69   order can be ei
5a370 74 68 65 72 20 41 53 43 20 6f 72 20 44 45 53 43  ther ASC or DESC
5a380 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5a390 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 20  ITE_SO_ASC      
5a3a0 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61   0  /* Sort in a
5a3b0 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  scending order *
5a3c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5a3d0 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31 20  _SO_DESC      1 
5a3e0 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65   /* Sort in asce
5a3f0 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 0a  nding order */..
5a400 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 66  /*.** Column aff
5a410 69 6e 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a 0a  inity types..**.
5a420 2a 2a 20 54 68 65 73 65 20 75 73 65 64 20 74 6f  ** These used to
5a430 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e   have mnemonic n
5a440 61 6d 65 20 6c 69 6b 65 20 27 69 27 20 66 6f 72  ame like 'i' for
5a450 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
5a460 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20 66  GER and.** 't' f
5a470 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  or SQLITE_AFF_TE
5a480 58 54 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20  XT.  But we can 
5a490 73 61 76 65 20 61 20 6c 69 74 74 6c 65 20 73 70  save a little sp
5a4a0 61 63 65 20 61 6e 64 20 69 6d 70 72 6f 76 65 0a  ace and improve.
5a4b0 2a 2a 20 74 68 65 20 73 70 65 65 64 20 61 20 6c  ** the speed a l
5a4c0 69 74 74 6c 65 20 62 79 20 6e 75 6d 62 65 72 69  ittle by numberi
5a4d0 6e 67 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f  ng the values co
5a4e0 6e 73 65 63 75 74 69 76 65 6c 79 2e 20 20 0a 2a  nsecutively.  .*
5a4f0 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68 65 72 20  *.** But rather 
5a500 74 68 61 6e 20 73 74 61 72 74 20 77 69 74 68 20  than start with 
5a510 30 20 6f 72 20 31 2c 20 77 65 20 62 65 67 69 6e  0 or 1, we begin
5a520 20 77 69 74 68 20 27 61 27 2e 20 20 54 68 61 74   with 'a'.  That
5a530 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75   way,.** when mu
5a540 6c 74 69 70 6c 65 20 61 66 66 69 6e 69 74 79 20  ltiple affinity 
5a550 74 79 70 65 73 20 61 72 65 20 63 6f 6e 63 61 74  types are concat
5a560 65 6e 61 74 65 64 20 69 6e 74 6f 20 61 20 73 74  enated into a st
5a570 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75 73 65 64  ring and.** used
5a580 20 61 73 20 74 68 65 20 50 34 20 6f 70 65 72 61   as the P4 opera
5a590 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c 20 62 65  nd, they will be
5a5a0 20 6d 6f 72 65 20 72 65 61 64 61 62 6c 65 2e 0a   more readable..
5a5b0 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f 20  **.** Note also 
5a5c0 74 68 61 74 20 74 68 65 20 6e 75 6d 65 72 69 63  that the numeric
5a5d0 20 74 79 70 65 73 20 61 72 65 20 67 72 6f 75 70   types are group
5a5e0 65 64 20 74 6f 67 65 74 68 65 72 20 73 6f 20 74  ed together so t
5a5f0 68 61 74 20 74 65 73 74 69 6e 67 0a 2a 2a 20 66  hat testing.** f
5a600 6f 72 20 61 20 6e 75 6d 65 72 69 63 20 74 79 70  or a numeric typ
5a610 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 63 6f  e is a single co
5a620 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65  mparison..*/.#de
5a630 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5a640 54 45 58 54 20 20 20 20 20 27 61 27 0a 23 64 65  TEXT     'a'.#de
5a650 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5a660 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a 23 64 65  NONE     'b'.#de
5a670 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5a680 4e 55 4d 45 52 49 43 20 20 27 63 27 0a 23 64 65  NUMERIC  'c'.#de
5a690 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5a6a0 49 4e 54 45 47 45 52 20 20 27 64 27 0a 23 64 65  INTEGER  'd'.#de
5a6b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  fine SQLITE_AFF_
5a6c0 52 45 41 4c 20 20 20 20 20 27 65 27 0a 0a 23 64  REAL     'e'..#d
5a6d0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 4e  efine sqlite3IsN
5a6e0 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 58  umericAffinity(X
5a6f0 29 20 20 28 28 58 29 3e 3d 53 51 4c 49 54 45 5f  )  ((X)>=SQLITE_
5a700 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a  AFF_NUMERIC)../*
5a710 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41  .** The SQLITE_A
5a720 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65 73 20 6d  FF_MASK values m
5a730 61 73 6b 73 20 6f 66 66 20 74 68 65 20 73 69 67  asks off the sig
5a740 6e 69 66 69 63 61 6e 74 20 62 69 74 73 20 6f 66  nificant bits of
5a750 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20   an.** affinity 
5a760 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64 65 66 69  value. .*/.#defi
5a770 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41  ne SQLITE_AFF_MA
5a780 53 4b 20 20 20 20 20 30 78 36 37 0a 0a 2f 2a 0a  SK     0x67../*.
5a790 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 62 69  ** Additional bi
5a7a0 74 20 76 61 6c 75 65 73 20 74 68 61 74 20 63 61  t values that ca
5a7b0 6e 20 62 65 20 4f 52 65 64 20 77 69 74 68 20 61  n be ORed with a
5a7c0 6e 20 61 66 66 69 6e 69 74 79 20 77 69 74 68 6f  n affinity witho
5a7d0 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74  ut.** changing t
5a7e0 68 65 20 61 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a  he affinity..*/.
5a7f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4a  #define SQLITE_J
5a800 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30 78 30 38  UMPIFNULL   0x08
5a810 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66 20 65 69    /* jumps if ei
5a820 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
5a830 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NULL */.#define 
5a840 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 20  SQLITE_STOREP2  
5a850 20 20 20 20 30 78 31 30 20 20 2f 2a 20 53 74 6f      0x10  /* Sto
5a860 72 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  re result in reg
5a870 5b 50 32 5d 20 72 61 74 68 65 72 20 74 68 61 6e  [P2] rather than
5a880 20 6a 75 6d 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   jump */../*.** 
5a890 45 61 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69  Each SQL table i
5a8a0 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e  s represented in
5a8b0 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e   memory by an in
5a8c0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a  stance of the.**
5a8d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
5a8e0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c  ture..**.** Tabl
5a8f0 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e  e.zName is the n
5a900 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
5a910 2e 20 20 54 68 65 20 63 61 73 65 20 6f 66 20 74  .  The case of t
5a920 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43  he original.** C
5a930 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
5a940 65 6d 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c  ement is stored,
5a950 20 62 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74   but case is not
5a960 20 73 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72   significant for
5a970 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e  .** comparisons.
5a980 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f  .**.** Table.nCo
5a990 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  l is the number 
5a9a0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
5a9b0 69 73 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65  is table.  Table
5a9c0 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f  .aCol is a.** po
5a9d0 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61  inter to an arra
5a9e0 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75  y of Column stru
5a9f0 63 74 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20  ctures, one for 
5aa00 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  each column..**.
5aa10 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  ** If the table 
5aa20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50  has an INTEGER P
5aa30 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
5aa40 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
5aa50 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20  the index of.** 
5aa60 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  the column that 
5aa70 69 73 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f  is that key.   O
5aa80 74 68 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69  therwise Table.i
5aa90 50 4b 65 79 20 69 73 20 6e 65 67 61 74 69 76 65  PKey is negative
5aaa0 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20  .  Note.** that 
5aab0 74 68 65 20 64 61 74 61 74 79 70 65 20 6f 66 20  the datatype of 
5aac0 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
5aad0 6d 75 73 74 20 62 65 20 49 4e 54 45 47 45 52 20  must be INTEGER 
5aae0 66 6f 72 20 74 68 69 73 20 66 69 65 6c 64 20 74  for this field t
5aaf0 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e  o.** be set.  An
5ab00 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
5ab10 20 4b 45 59 20 69 73 20 75 73 65 64 20 61 73 20   KEY is used as 
5ab20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61  the rowid for ea
5ab30 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65  ch row of.** the
5ab40 20 74 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61   table.  If a ta
5ab50 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47  ble has no INTEG
5ab60 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
5ab70 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f  then a random ro
5ab80 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61  wid.** is genera
5ab90 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77  ted for each row
5aba0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20   of the table.  
5abb0 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79  TF_HasPrimaryKey
5abc0 20 69 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68   is set if.** th
5abd0 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20  e table has any 
5abe0 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54  PRIMARY KEY, INT
5abf0 45 47 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73  EGER or otherwis
5ac00 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74  e..**.** Table.t
5ac10 6e 75 6d 20 69 73 20 74 68 65 20 70 61 67 65 20  num is the page 
5ac20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72  number for the r
5ac30 6f 6f 74 20 42 54 72 65 65 20 70 61 67 65 20 6f  oot BTree page o
5ac40 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74  f the table in t
5ac50 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
5ac60 69 6c 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69  ile.  If Table.i
5ac70 44 62 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  Db is the index 
5ac80 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
5ac90 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a  table backend.**
5aca0 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d   in sqlite.aDb[]
5acb0 2e 20 20 30 20 69 73 20 66 6f 72 20 74 68 65 20  .  0 is for the 
5acc0 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e  main database an
5acd0 64 20 31 20 69 73 20 66 6f 72 20 74 68 65 20 66  d 1 is for the f
5ace0 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64  ile that.** hold
5acf0 73 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  s temporary tabl
5ad00 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20  es and indices. 
5ad10 20 49 66 20 54 46 5f 45 70 68 65 6d 65 72 61 6c   If TF_Ephemeral
5ad20 20 69 73 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20   is set.** then 
5ad30 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 74 6f  the table is sto
5ad40 72 65 64 20 69 6e 20 61 20 66 69 6c 65 20 74 68  red in a file th
5ad50 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  at is automatica
5ad60 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77  lly deleted.** w
5ad70 68 65 6e 20 74 68 65 20 56 44 42 45 20 63 75 72  hen the VDBE cur
5ad80 73 6f 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65  sor to the table
5ad90 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20   is closed.  In 
5ada0 74 68 69 73 20 63 61 73 65 20 54 61 62 6c 65 2e  this case Table.
5adb0 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20  tnum .** refers 
5adc0 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
5add0 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  er that holds th
5ade0 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f  e table open, no
5adf0 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a  t to the root.**
5ae00 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54   page number.  T
5ae10 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 73 20  ransient tables 
5ae20 61 72 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  are used to hold
5ae30 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   the results of 
5ae40 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72 79 20 74  a.** sub-query t
5ae50 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 73 74  hat appears inst
5ae60 65 61 64 20 6f 66 20 61 20 72 65 61 6c 20 74 61  ead of a real ta
5ae70 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  ble name in the 
5ae80 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20  FROM clause .** 
5ae90 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
5aea0 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74  ement..*/.struct
5aeb0 20 54 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74   Table {.  sqlit
5aec0 65 33 20 2a 64 62 4d 65 6d 3b 20 20 20 20 20 20  e3 *dbMem;      
5aed0 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 74 69 6f 6e  /* DB connection
5aee0 20 75 73 65 64 20 66 6f 72 20 6c 6f 6f 6b 61 73   used for lookas
5aef0 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e  ide allocations.
5af00 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
5af10 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  e;         /* Na
5af20 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
5af30 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74  or view */.  int
5af40 20 69 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20   iPKey;         
5af50 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61    /* If not nega
5af60 74 69 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69  tive, use aCol[i
5af70 50 4b 65 79 5d 20 61 73 20 74 68 65 20 70 72 69  PKey] as the pri
5af80 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  mary key */.  in
5af90 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t nCol;         
5afa0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5afb0 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20  columns in this 
5afc0 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d  table */.  Colum
5afd0 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20  n *aCol;        
5afe0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
5aff0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e  bout each column
5b000 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e   */.  Index *pIn
5b010 64 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69  dex;       /* Li
5b020 73 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65  st of SQL indexe
5b030 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  s on this table.
5b040 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20   */.  int tnum; 
5b050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
5b060 6f 74 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f  ot BTree node fo
5b070 72 20 74 68 69 73 20 74 61 62 6c 65 20 28 73 65  r this table (se
5b080 65 20 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f  e note above) */
5b090 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
5b0a0 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20  ct;     /* NULL 
5b0b0 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69  for tables.  Poi
5b0c0 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f  nts to definitio
5b0d0 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a  n if a view. */.
5b0e0 20 20 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20    u16 nRef;     
5b0f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5b100 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
5b110 74 68 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20  this Table */.  
5b120 75 38 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20  u8 tabFlags;    
5b130 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
5b140 54 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20  TF_* values */. 
5b150 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20   u8 keyConf;    
5b160 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f        /* What to
5b170 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75   do in case of u
5b180 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69  niqueness confli
5b190 63 74 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20  ct on iPKey */. 
5b1a0 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20   FKey *pFKey;   
5b1b0 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20        /* Linked 
5b1c0 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65  list of all fore
5b1d0 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73  ign keys in this
5b1e0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
5b1f0 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20   *zColAff;      
5b200 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e   /* String defin
5b210 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79  ing the affinity
5b220 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20   of each column 
5b230 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
5b240 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45  E_OMIT_CHECK.  E
5b250 78 70 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20  xpr *pCheck;    
5b260 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f      /* The AND o
5b270 66 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73  f all CHECK cons
5b280 74 72 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69  traints */.#endi
5b290 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
5b2a0 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45  _OMIT_ALTERTABLE
5b2b0 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66  .  int addColOff
5b2c0 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65  set;    /* Offse
5b2d0 74 20 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c  t in CREATE TABL
5b2e0 45 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20  E stmt to add a 
5b2f0 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65  new column */.#e
5b300 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
5b310 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
5b320 54 41 42 4c 45 0a 20 20 4d 6f 64 75 6c 65 20 2a  TABLE.  Module *
5b330 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  pMod;        /* 
5b340 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69  Pointer to the i
5b350 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
5b360 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20   the module */. 
5b370 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
5b380 56 74 61 62 3b 20 2f 2a 20 50 6f 69 6e 74 65 72  Vtab; /* Pointer
5b390 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69   to the module i
5b3a0 6e 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74  nstance */.  int
5b3b0 20 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20   nModuleArg;    
5b3c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
5b3d0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
5b3e0 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72  module */.  char
5b3f0 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20   **azModuleArg; 
5b400 20 2f 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20   /* Text of all 
5b410 6d 6f 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d  module args. [0]
5b420 20 69 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20   is module name 
5b430 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 67  */.#endif.  Trig
5b440 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
5b450 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67   /* List of trig
5b460 67 65 72 73 20 73 74 6f 72 65 64 20 69 6e 20 70  gers stored in p
5b470 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 65  Schema */.  Sche
5b480 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20  ma *pSchema;    
5b490 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20   /* Schema that 
5b4a0 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61  contains this ta
5b4b0 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ble */.  Table *
5b4c0 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a  pNextZombie;  /*
5b4d0 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72   Next on the Par
5b4e0 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69  se.pZombieTab li
5b4f0 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  st */.};../*.** 
5b500 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66  Allowed values f
5b510 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73  or Tabe.tabFlags
5b520 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  ..*/.#define TF_
5b530 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20  Readonly        
5b540 30 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d  0x01    /* Read-
5b550 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c  only system tabl
5b560 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  e */.#define TF_
5b570 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20  Ephemeral       
5b580 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70  0x02    /* An ep
5b590 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f  hemeral table */
5b5a0 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50  .#define TF_HasP
5b5b0 72 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34  rimaryKey   0x04
5b5c0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73      /* Table has
5b5d0 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a   a primary key *
5b5e0 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74  /.#define TF_Aut
5b5f0 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30  oincrement   0x0
5b600 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20  8    /* Integer 
5b610 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61  primary key is a
5b620 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a  utoincrement */.
5b630 23 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75  #define TF_Virtu
5b640 61 6c 20 20 20 20 20 20 20 20 20 30 78 31 30 20  al         0x10 
5b650 20 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75     /* Is a virtu
5b660 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66  al table */.#def
5b670 69 6e 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64  ine TF_NeedMetad
5b680 61 74 61 20 20 20 20 30 78 32 30 20 20 20 20 2f  ata    0x20    /
5b690 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61  * aCol[].zType a
5b6a0 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20  nd aCol[].pColl 
5b6b0 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a  missing */..../*
5b6c0 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20  .** Test to see 
5b6d0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  whether or not a
5b6e0 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74   table is a virt
5b6f0 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ual table.  This
5b700 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61   is.** done as a
5b710 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69   macro so that i
5b720 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69  t will be optimi
5b730 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72  zed out when vir
5b740 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75  tual.** table su
5b750 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64  pport is omitted
5b760 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e   from the build.
5b770 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
5b780 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
5b790 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49  ABLE.#  define I
5b7a0 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20  sVirtual(X)     
5b7b0 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73   (((X)->tabFlags
5b7c0 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d   & TF_Virtual)!=
5b7d0 30 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48  0).#  define IsH
5b7e0 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28  iddenColumn(X) (
5b7f0 28 58 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23  (X)->isHidden).#
5b800 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49  else.#  define I
5b810 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20 20  sVirtual(X)     
5b820 20 30 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48   0.#  define IsH
5b830 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30  iddenColumn(X) 0
5b840 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
5b850 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ach foreign key 
5b860 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e  constraint is an
5b870 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
5b880 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
5b890 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f  ture..**.** A fo
5b8a0 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73  reign key is ass
5b8b0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 77 6f  ociated with two
5b8c0 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66   tables.  The "f
5b8d0 72 6f 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a  rom" table is.**
5b8e0 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
5b8f0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46  contains the REF
5b900 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74  ERENCES clause t
5b910 68 61 74 20 63 72 65 61 74 65 73 20 74 68 65 20  hat creates the 
5b920 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20  foreign.** key. 
5b930 20 54 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20   The "to" table 
5b940 69 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  is the table tha
5b950 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68  t is named in th
5b960 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  e REFERENCES cla
5b970 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  use..** Consider
5b980 20 74 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a   this example:.*
5b990 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
5b9a0 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20  TABLE ex1(.**   
5b9b0 20 20 20 20 61 20 49 4e 54 45 47 45 52 20 50 52      a INTEGER PR
5b9c0 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20  IMARY KEY,.**   
5b9d0 20 20 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f      b INTEGER CO
5b9e0 4e 53 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46  NSTRAINT fk1 REF
5b9f0 45 52 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a  ERENCES ex2(x).*
5ba00 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46  *     );.**.** F
5ba10 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22  or foreign key "
5ba20 66 6b 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74  fk1", the from-t
5ba30 61 62 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e  able is "ex1" an
5ba40 64 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69  d the to-table i
5ba50 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45  s "ex2"..**.** E
5ba60 61 63 68 20 52 45 46 45 52 45 4e 43 45 53 20 63  ach REFERENCES c
5ba70 6c 61 75 73 65 20 67 65 6e 65 72 61 74 65 73 20  lause generates 
5ba80 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
5ba90 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
5baa0 75 63 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20  ucture.** which 
5bab0 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  is attached to t
5bac0 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20  he from-table.  
5bad0 54 68 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65  The to-table nee
5bae0 64 20 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e  d not exist when
5baf0 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62  .** the from-tab
5bb00 6c 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  le is created.  
5bb10 54 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66  The existence of
5bb20 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73   the to-table is
5bb30 20 6e 6f 74 20 63 68 65 63 6b 65 64 0a 2a 2a 20   not checked.** 
5bb40 75 6e 74 69 6c 20 61 6e 20 61 74 74 65 6d 70 74  until an attempt
5bb50 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65   is made to inse
5bb60 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65  rt data into the
5bb70 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a   from-table..**.
5bb80 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 61 46  ** The sqlite.aF
5bb90 4b 65 79 20 68 61 73 68 20 74 61 62 6c 65 20 73  Key hash table s
5bba0 74 6f 72 65 73 20 70 6f 69 6e 74 65 72 73 20 74  tores pointers t
5bbb0 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
5bbc0 0a 2a 2a 20 67 69 76 65 6e 20 74 68 65 20 6e 61  .** given the na
5bbd0 6d 65 20 6f 66 20 61 20 74 6f 2d 74 61 62 6c 65  me of a to-table
5bbe0 2e 20 20 46 6f 72 20 65 61 63 68 20 74 6f 2d 74  .  For each to-t
5bbf0 61 62 6c 65 2c 20 61 6c 6c 20 66 6f 72 65 69 67  able, all foreig
5bc00 6e 20 6b 65 79 73 0a 2a 2a 20 61 73 73 6f 63 69  n keys.** associ
5bc10 61 74 65 64 20 77 69 74 68 20 74 68 61 74 20 74  ated with that t
5bc20 61 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 6c 69  able are on a li
5bc30 6e 6b 65 64 20 6c 69 73 74 20 75 73 69 6e 67 20  nked list using 
5bc40 74 68 65 20 46 4b 65 79 2e 70 4e 65 78 74 54 6f  the FKey.pNextTo
5bc50 0a 2a 2a 20 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74  .** field..*/.st
5bc60 72 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54 61  ruct FKey {.  Ta
5bc70 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 20  ble *pFrom;     
5bc80 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61  /* The table tha
5bc90 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52  t contains the R
5bca0 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65  EFERENCES clause
5bcb0 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78   */.  FKey *pNex
5bcc0 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20  tFrom;  /* Next 
5bcd0 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70  foreign key in p
5bce0 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a  From */.  char *
5bcf0 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  zTo;        /* N
5bd00 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61  ame of table tha
5bd10 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73  t the key points
5bd20 20 74 6f 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70   to */.  FKey *p
5bd30 4e 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65  NextTo;    /* Ne
5bd40 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74  xt foreign key t
5bd50 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 7a 54  hat points to zT
5bd60 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  o */.  int nCol;
5bd70 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5bd80 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
5bd90 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 73   this key */.  s
5bda0 74 72 75 63 74 20 73 43 6f 6c 4d 61 70 20 7b 20  truct sColMap { 
5bdb0 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20 63   /* Mapping of c
5bdc0 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f 6d 20  olumns in pFrom 
5bdd0 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a 54  to columns in zT
5bde0 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 46 72  o */.    int iFr
5bdf0 6f 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49  om;         /* I
5be00 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69  ndex of column i
5be10 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 63  n pFrom */.    c
5be20 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20  har *zCol;      
5be30 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c    /* Name of col
5be40 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49 66 20  umn in zTo.  If 
5be50 30 20 75 73 65 20 50 52 49 4d 41 52 59 20 4b 45  0 use PRIMARY KE
5be60 59 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 20  Y */.  } *aCol; 
5be70 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
5be80 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f  entry for each o
5be90 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 20  f nCol column s 
5bea0 2a 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72  */.  u8 isDeferr
5beb0 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ed;    /* True i
5bec0 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65  f constraint che
5bed0 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65  cking is deferre
5bee0 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f  d till COMMIT */
5bef0 0a 20 20 75 38 20 75 70 64 61 74 65 43 6f 6e 66  .  u8 updateConf
5bf00 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72  ;    /* How to r
5bf10 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73  esolve conflicts
5bf20 20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 55   that occur on U
5bf30 50 44 41 54 45 20 2a 2f 0a 20 20 75 38 20 64 65  PDATE */.  u8 de
5bf40 6c 65 74 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20  leteConf;    /* 
5bf50 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76 65 20 63  How to resolve c
5bf60 6f 6e 66 6c 69 63 74 73 20 74 68 61 74 20 6f 63  onflicts that oc
5bf70 63 75 72 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f  cur on DELETE */
5bf80 0a 20 20 75 38 20 69 6e 73 65 72 74 43 6f 6e 66  .  u8 insertConf
5bf90 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72  ;    /* How to r
5bfa0 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73  esolve conflicts
5bfb0 20 74 68 61 74 20 6f 63 63 75 72 20 6f 6e 20 49   that occur on I
5bfc0 4e 53 45 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  NSERT */.};../*.
5bfd0 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72  ** SQLite suppor
5bfe0 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e  ts many differen
5bff0 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76  t ways to resolv
5c000 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  e a constraint.*
5c010 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41  * error.  ROLLBA
5c020 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65  CK processing me
5c030 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74  ans that a const
5c040 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a  raint violation.
5c050 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70  ** causes the op
5c060 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65  eration in proce
5c070 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66  ss to fail and f
5c080 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
5c090 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f  ransaction.** to
5c0a0 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   be rolled back.
5c0b0 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69    ABORT processi
5c0c0 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65  ng means the ope
5c0d0 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73  ration in proces
5c0e0 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61  s.** fails and a
5c0f0 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73  ny prior changes
5c100 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f   from that one o
5c110 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63  peration are bac
5c120 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20  ked out,.** but 
5c130 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
5c140 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61  is not rolled ba
5c150 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73  ck.  FAIL proces
5c160 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a  sing means that.
5c170 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  ** the operation
5c180 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f   in progress sto
5c190 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  ps and returns a
5c1a0 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42  n error code.  B
5c1b0 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e  ut prior.** chan
5c1c0 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73  ges due to the s
5c1d0 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72  ame operation ar
5c1e0 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74  e not backed out
5c1f0 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b   and no rollback
5c200 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e  .** occurs.  IGN
5c210 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74  ORE means that t
5c220 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f  he particular ro
5c230 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68  w that caused th
5c240 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  e constraint.** 
5c250 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73  error is not ins
5c260 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64  erted or updated
5c270 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f  .  Processing co
5c280 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65  ntinues and no e
5c290 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72  rror.** is retur
5c2a0 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65  ned.  REPLACE me
5c2b0 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73  ans that preexis
5c2c0 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f  ting database ro
5c2d0 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a  ws that caused.*
5c2e0 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74  * a UNIQUE const
5c2f0 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20  raint violation 
5c300 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74  are removed so t
5c310 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65  hat the new inse
5c320 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20  rt or.** update 
5c330 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72  can proceed.  Pr
5c340 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75  ocessing continu
5c350 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20  es and no error 
5c360 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a  is reported..**.
5c370 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54  ** RESTRICT, SET
5c380 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44  NULL, and CASCAD
5c390 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20  E actions apply 
5c3a0 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20  only to foreign 
5c3b0 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43  keys..** RESTRIC
5c3c0 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  T is the same as
5c3d0 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44   ABORT for IMMED
5c3e0 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79  IATE foreign key
5c3f0 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d  s and the.** sam
5c400 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f  e as ROLLBACK fo
5c410 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e  r DEFERRED keys.
5c420 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20    SETNULL means 
5c430 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e  that the foreign
5c440 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74  .** key is set t
5c450 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45  o NULL.  CASCADE
5c460 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45   means that a DE
5c470 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f  LETE or UPDATE o
5c480 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e  f the.** referen
5c490 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73  ced table row is
5c4a0 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f   propagated into
5c4b0 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f   the row that ho
5c4c0 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69  lds the.** forei
5c4d0 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54  gn key..** .** T
5c4e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d  he following sym
5c4f0 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65  bolic values are
5c500 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20   used to record 
5c510 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66  which type.** of
5c520 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e   action to take.
5c530 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e  .*/.#define OE_N
5c540 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54  one     0   /* T
5c550 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74  here is no const
5c560 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a  raint to check *
5c570 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c  /.#define OE_Rol
5c580 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69  lback 1   /* Fai
5c590 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  l the operation 
5c5a0 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65  and rollback the
5c5b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a   transaction */.
5c5c0 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74  #define OE_Abort
5c5d0 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20      2   /* Back 
5c5e0 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20  out changes but 
5c5f0 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  do no rollback t
5c600 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64  ransaction */.#d
5c610 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20  efine OE_Fail   
5c620 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68    3   /* Stop th
5c630 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20  e operation but 
5c640 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20  leave all prior 
5c650 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69  changes */.#defi
5c660 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34  ne OE_Ignore   4
5c670 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65     /* Ignore the
5c680 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64   error. Do not d
5c690 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20  o the INSERT or 
5c6a0 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e  UPDATE */.#defin
5c6b0 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20  e OE_Replace  5 
5c6c0 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73    /* Delete exis
5c6d0 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65  ting record, the
5c6e0 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55  n do INSERT or U
5c6f0 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e  PDATE */..#defin
5c700 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20  e OE_Restrict 6 
5c710 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f    /* OE_Abort fo
5c720 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f  r IMMEDIATE, OE_
5c730 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46  Rollback for DEF
5c740 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65  ERRED */.#define
5c750 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20   OE_SetNull  7  
5c760 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65   /* Set the fore
5c770 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f  ign key value to
5c780 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
5c790 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20   OE_SetDflt  8  
5c7a0 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65   /* Set the fore
5c7b0 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f  ign key value to
5c7c0 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a   its default */.
5c7d0 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61  #define OE_Casca
5c7e0 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61  de  9   /* Casca
5c7f0 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a  de the changes *
5c800 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65  /..#define OE_De
5c810 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f  fault  99  /* Do
5c820 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65   whatever the de
5c830 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20  fault action is 
5c840 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  */.../*.** An in
5c850 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
5c860 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
5c870 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
5c880 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
5c890 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56  ment to sqlite3V
5c8a0 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e  dbeKeyCompare an
5c8b0 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e  d is used to con
5c8c0 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d  trol the .** com
5c8d0 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74  parison of the t
5c8e0 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a  wo index keys..*
5c8f0 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f  /.struct KeyInfo
5c900 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
5c910 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
5c920 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
5c930 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b  ion */.  u8 enc;
5c940 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5c950 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20  Text encoding - 
5c960 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f  one of the TEXT_
5c970 55 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20  Utf* values */. 
5c980 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20   u16 nField;    
5c990 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5c9a0 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f  f entries in aCo
5c9b0 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53  ll[] */.  u8 *aS
5c9c0 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a  ortOrder;     /*
5c9d0 20 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61   If defined an a
5c9e0 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20  SortOrder[i] is 
5c9f0 74 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20  true, sort DESC 
5ca00 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43  */.  CollSeq *aC
5ca10 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c  oll[1];  /* Coll
5ca20 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
5ca30 6f 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20  or each term of 
5ca40 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f  the key */.};../
5ca50 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
5ca60 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
5ca70 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  g structure hold
5ca80 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  s information ab
5ca90 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20  out a.** single 
5caa0 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 61  index record tha
5cab0 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  t has already be
5cac0 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e  en parsed out in
5cad0 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a  to individual.**
5cae0 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41   values..**.** A
5caf0 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62   record is an ob
5cb00 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69  ject that contai
5cb10 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66  ns one or more f
5cb20 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a  ields of data..*
5cb30 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 73  * Records are us
5cb40 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
5cb50 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62  content of a tab
5cb60 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74  le row and to st
5cb70 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f  ore.** the key o
5cb80 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62  f an index.  A b
5cb90 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  lob encoding of 
5cba0 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 61  a record is crea
5cbb0 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50  ted by.** the OP
5cbc0 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f  _MakeRecord opco
5cbd0 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 61  de of the VDBE a
5cbe0 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c  nd is disassembl
5cbf0 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f  ed by the.** OP_
5cc00 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a  Column opcode..*
5cc10 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74  *.** This struct
5cc20 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f  ure holds a reco
5cc30 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65  rd that has alre
5cc40 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 65  ady been disasse
5cc50 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74  mbled.** into it
5cc60 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69  s constituent fi
5cc70 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  elds..*/.struct 
5cc80 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b  UnpackedRecord {
5cc90 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
5cca0 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74  Info;  /* Collat
5ccb0 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64  ion and sort-ord
5ccc0 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a  er information *
5ccd0 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20  /.  u16 nField; 
5cce0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5ccf0 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
5cd00 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36  apMem[] */.  u16
5cd10 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
5cd20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74   /* Boolean sett
5cd30 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f  ings.  UNPACKED_
5cd40 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 4d  ... below */.  M
5cd50 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20  em *aMem;       
5cd60 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a     /* Values */.
5cd70 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65  };../*.** Allowe
5cd80 64 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61  d values of Unpa
5cd90 63 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73  ckedRecord.flags
5cda0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41  .*/.#define UNPA
5cdb0 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20  CKED_NEED_FREE  
5cdc0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65     0x0001  /* Me
5cdd0 6d 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c  mory is from sql
5cde0 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a  ite3Malloc() */.
5cdf0 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44  #define UNPACKED
5ce00 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30  _NEED_DESTROY  0
5ce10 78 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b  x0002  /* apMem[
5ce20 5d 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65  ]s should all be
5ce30 20 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64   destroyed */.#d
5ce40 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49  efine UNPACKED_I
5ce50 47 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30  GNORE_ROWID  0x0
5ce60 30 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74  004  /* Ignore t
5ce70 72 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e  railing rowid on
5ce80 20 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65   key1 */.#define
5ce90 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45   UNPACKED_INCRKE
5cea0 59 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20  Y       0x0008  
5ceb0 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79  /* Make this key
5cec0 20 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67   an epsilon larg
5ced0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e  er */.#define UN
5cee0 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41  PACKED_PREFIX_MA
5cef0 54 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20  TCH  0x0010  /* 
5cf00 41 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69  A prefix match i
5cf10 73 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20  s considered OK 
5cf20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53  */../*.** Each S
5cf30 51 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72  QL index is repr
5cf40 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72  esented in memor
5cf50 79 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61  y by an.** insta
5cf60 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
5cf70 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
5cf80 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  **.** The column
5cf90 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  s of the table t
5cfa0 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e  hat are to be in
5cfb0 64 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69  dexed are descri
5cfc0 62 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69  bed.** by the ai
5cfd0 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f  Column[] field o
5cfe0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
5cff0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
5d000 73 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61  suppose.** we ha
5d010 76 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ve the following
5d020 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78   table and index
5d030 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41  :.**.**     CREA
5d040 54 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20  TE TABLE Ex1(c1 
5d050 69 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20  int, c2 int, c3 
5d060 74 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52  text);.**     CR
5d070 45 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f  EATE INDEX Ex2 O
5d080 4e 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a  N Ex1(c3,c1);.**
5d090 0a 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65  .** In the Table
5d0a0 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72   structure descr
5d0b0 69 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d  ibing Ex1, nCol=
5d0c0 3d 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65  =3 because there
5d0d0 20 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f   are.** three co
5d0e0 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62  lumns in the tab
5d0f0 6c 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65  le.  In the Inde
5d100 78 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63  x structure desc
5d110 72 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e  ribing.** Ex2, n
5d120 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20  Column==2 since 
5d130 32 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d  2 of the 3 colum
5d140 6e 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e  ns of Ex1 are in
5d150 64 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61  dexed..** The va
5d160 6c 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20  lue of aiColumn 
5d170 69 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f  is {2, 0}.  aiCo
5d180 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75  lumn[0]==2 becau
5d190 73 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74  se the .** first
5d1a0 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e   column to be in
5d1b0 64 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61  dexed (c3) has a
5d1c0 6e 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20  n index of 2 in 
5d1d0 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54  Ex1.aCol[]..** T
5d1e0 68 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e  he second column
5d1f0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28   to be indexed (
5d200 63 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78  c1) has an index
5d210 20 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e   of 0 in.** Ex1.
5d220 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78  aCol[], hence Ex
5d230 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30  2.aiColumn[1]==0
5d240 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65  ..**.** The Inde
5d250 78 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20  x.onError field 
5d260 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68  determines wheth
5d270 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e  er or not the in
5d280 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  dexed columns.**
5d290 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20   must be unique 
5d2a0 61 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69  and what to do i
5d2b0 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20  f they are not. 
5d2c0 20 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72   When Index.onEr
5d2d0 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20  ror=OE_None,.** 
5d2e0 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73  it means this is
5d2f0 20 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e   not a unique in
5d300 64 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20  dex.  Otherwise 
5d310 69 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69  it is a unique i
5d320 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ndex.** and the 
5d330 76 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f  value of Index.o
5d340 6e 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20  nError indicate 
5d350 74 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69  the which confli
5d360 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a  ct resolution .*
5d370 2a 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65  * algorithm to e
5d380 6d 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61  mploy whenever a
5d390 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64  n attempt is mad
5d3a0 65 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f  e to insert a no
5d3b0 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d  n-unique.** elem
5d3c0 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49  ent..*/.struct I
5d3d0 6e 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a  ndex {.  char *z
5d3e0 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d  Name;     /* Nam
5d3f0 65 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20  e of this index 
5d400 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
5d410 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
5d420 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
5d430 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20  e table used by 
5d440 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20  this index */.  
5d450 69 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20  int *aiColumn;  
5d460 20 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e   /* Which column
5d470 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68  s are used by th
5d480 69 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69  is index.  1st i
5d490 73 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  s 0 */.  unsigne
5d4a0 64 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20  d *aiRowEst; /* 
5d4b0 52 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a  Result of ANALYZ
5d4c0 45 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c  E: Est. rows sel
5d4d0 65 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f  ected by each co
5d4e0 6c 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20  lumn */.  Table 
5d4f0 2a 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68  *pTable;   /* Th
5d500 65 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e  e SQL table bein
5d510 67 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69  g indexed */.  i
5d520 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20  nt tnum;        
5d530 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69  /* Page containi
5d540 6e 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20  ng root of this 
5d550 69 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73  index in databas
5d560 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f  e file */.  u8 o
5d570 6e 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20  nError;      /* 
5d580 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e  OE_Abort, OE_Ign
5d590 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c  ore, OE_Replace,
5d5a0 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20   or OE_None */. 
5d5b0 20 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20   u8 autoIndex;  
5d5c0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20    /* True if is 
5d5d0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72  automatically cr
5d5e0 65 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e  eated (ex: by UN
5d5f0 49 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20  IQUE) */.  char 
5d600 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53  *zColAff;   /* S
5d610 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74  tring defining t
5d620 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65  he affinity of e
5d630 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  ach column */.  
5d640 49 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20  Index *pNext;   
5d650 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64   /* The next ind
5d660 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
5d670 74 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c  th the same tabl
5d680 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70  e */.  Schema *p
5d690 53 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d  Schema; /* Schem
5d6a0 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69  a containing thi
5d6b0 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20  s index */.  u8 
5d6c0 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a  *aSortOrder;  /*
5d6d0 20 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49   Array of size I
5d6e0 6e 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72  ndex.nColumn. Tr
5d6f0 75 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d  ue==DESC, False=
5d700 3d 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a  =ASC */.  char *
5d710 2a 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72  *azColl;   /* Ar
5d720 72 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e  ray of collation
5d730 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20   sequence names 
5d740 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a  for index */.};.
5d750 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65  ./*.** Each toke
5d760 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20  n coming out of 
5d770 74 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20  the lexer is an 
5d780 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74  instance of.** t
5d790 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20  his structure.  
5d7a0 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20  Tokens are also 
5d7b0 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
5d7c0 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  an expression..*
5d7d0 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b  *.** Note if Tok
5d7e0 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b  en.z==0 then Tok
5d7f0 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e  en.dyn and Token
5d800 2e 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  .n are undefined
5d810 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74   and.** may cont
5d820 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65  ain random value
5d830 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20  s.  Do not make 
5d840 61 6e 79 20 61 73 73 75 6d 70 74 69 6f 6e 73 20  any assumptions 
5d850 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a  about Token.dyn.
5d860 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77  ** and Token.n w
5d870 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a  hen Token.z==0..
5d880 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20  */.struct Token 
5d890 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
5d8a0 65 64 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54  ed char *z; /* T
5d8b0 65 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  ext of the token
5d8c0 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d  .  Not NULL-term
5d8d0 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73  inated! */.  uns
5d8e0 69 67 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20  igned dyn  : 1; 
5d8f0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
5d900 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79   malloced memory
5d910 2c 20 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74  , false for stat
5d920 69 63 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ic */.  unsigned
5d930 20 6e 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20   n    : 31;     
5d940 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
5d950 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20  racters in this 
5d960 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  token */.};../*.
5d970 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
5d980 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
5d990 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d   contains inform
5d9a0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20  ation needed to 
5d9b0 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65  generate.** code
5d9c0 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68   for a SELECT th
5d9d0 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72  at contains aggr
5d9e0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e  egate functions.
5d9f0 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f  .**.** If Expr.o
5da00 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
5da10 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54   or TK_AGG_FUNCT
5da20 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41  ION then Expr.pA
5da30 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70  ggInfo is a.** p
5da40 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
5da50 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45  tructure.  The E
5da60 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c  xpr.iColumn fiel
5da70 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69  d is the index i
5da80 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f  n.** AggInfo.aCo
5da90 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61  l[] or AggInfo.a
5daa0 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d  Func[] of inform
5dab0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20  ation needed to 
5dac0 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65  generate.** code
5dad0 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a   for that node..
5dae0 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47  **.** AggInfo.pG
5daf0 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e  roupBy and AggIn
5db00 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70  fo.aFunc.pExpr p
5db10 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77  oint to fields w
5db20 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69  ithin the.** ori
5db30 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72  ginal Select str
5db40 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
5db50 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54  ribes the SELECT
5db60 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
5db70 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20  se.** fields do 
5db80 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66  not need to be f
5db90 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f  reed when deallo
5dba0 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e  cating the AggIn
5dbb0 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  fo structure..*/
5dbc0 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20  .struct AggInfo 
5dbd0 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64  {.  u8 directMod
5dbe0 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  e;          /* D
5dbf0 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20  irect rendering 
5dc00 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20  mode means take 
5dc10 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20  data directly.  
5dc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5dc30 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20          ** from 
5dc40 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61  source tables ra
5dc50 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61  ther than from a
5dc60 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20  ccumulators */. 
5dc70 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64   u8 useSortingId
5dc80 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64  x;       /* In d
5dc90 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65  irect mode, refe
5dca0 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e  rence the sortin
5dcb0 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20  g index rather. 
5dcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5dcd0 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e           ** than
5dce0 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
5dcf0 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69  e */.  int sorti
5dd00 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f  ngIdx;         /
5dd10 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
5dd20 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  of the sorting i
5dd30 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ndex */.  ExprLi
5dd40 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20  st *pGroupBy;   
5dd50 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62    /* The group b
5dd60 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  y clause */.  in
5dd70 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e  t nSortingColumn
5dd80 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
5dd90 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
5dda0 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  e sorting index 
5ddb0 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49  */.  struct AggI
5ddc0 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20  nfo_col {    /* 
5ddd0 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  For each column 
5dde0 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74  used in source t
5ddf0 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62  ables */.    Tab
5de00 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
5de10 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
5de20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
5de30 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20   iTable;        
5de40 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
5de50 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f  number of the so
5de60 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  urce table */.  
5de70 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
5de80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
5de90 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68  lumn number with
5dea0 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61  in the source ta
5deb0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ble */.    int i
5dec0 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20  SorterColumn;   
5ded0 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75      /* Column nu
5dee0 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74  mber in the sort
5def0 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20  ing index */.   
5df00 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20   int iMem;      
5df10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
5df20 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61  ory location tha
5df30 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75  t acts as accumu
5df40 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70  lator */.    Exp
5df50 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
5df60 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69        /* The ori
5df70 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e  ginal expression
5df80 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20   */.  } *aCol;. 
5df90 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20   int nColumn;   
5dfa0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5dfb0 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69  er of used entri
5dfc0 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a  es in aCol[] */.
5dfd0 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c    int nColumnAll
5dfe0 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  oc;       /* Num
5dff0 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c  ber of slots all
5e000 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b  ocated for aCol[
5e010 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75  ] */.  int nAccu
5e020 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f  mulator;       /
5e030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
5e040 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68  mns that show th
5e050 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74  rough to the out
5e060 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20  put..           
5e070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
5e080 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c  * Additional col
5e090 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e  umns are used on
5e0a0 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73  ly as parameters
5e0b0 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20   to.            
5e0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
5e0d0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
5e0e0 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  ions */.  struct
5e0f0 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20   AggInfo_func { 
5e100 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67    /* For each ag
5e110 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
5e120 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45   */.    Expr *pE
5e130 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
5e140 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65   /* Expression e
5e150 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63  ncoding the func
5e160 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63  tion */.    Func
5e170 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20  Def *pFunc;     
5e180 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72       /* The aggr
5e190 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69  egate function i
5e1a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  mplementation */
5e1b0 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20  .    int iMem;  
5e1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5e1d0 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   Memory location
5e1e0 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63   that acts as ac
5e1f0 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20  cumulator */.   
5e200 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20   int iDistinct; 
5e210 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68            /* Eph
5e220 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65  emeral table use
5e230 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53  d to enforce DIS
5e240 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46  TINCT */.  } *aF
5e250 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63  unc;.  int nFunc
5e260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
5e270 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
5e280 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a  ies in aFunc[] *
5e290 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c  /.  int nFuncAll
5e2a0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  oc;         /* N
5e2b0 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61  umber of slots a
5e2c0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75  llocated for aFu
5e2d0 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  nc[] */.};../*.*
5e2e0 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61  * Each node of a
5e2f0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  n expression in 
5e300 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69  the parse tree i
5e310 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a  s an instance.**
5e320 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
5e330 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f  re..**.** Expr.o
5e340 70 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e  p is the opcode.
5e350 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   The integer par
5e360 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20  ser token codes 
5e370 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73  are reused.** as
5e380 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 46   opcodes here. F
5e390 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  or example, the 
5e3a0 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 54  parser defines T
5e3b0 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 6e  K_GE to be an in
5e3c0 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 65  teger.** code re
5e3d0 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22  presenting the "
5e3e0 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 54 68  >=" operator. Th
5e3f0 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20  is same integer 
5e400 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 0a 2a  code is reused.*
5e410 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 74  * to represent t
5e420 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 6e 2d  he greater-than-
5e430 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 65 72  or-equal-to oper
5e440 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 70 72  ator in the expr
5e450 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a  ession.** tree..
5e460 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
5e470 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51  ression is an SQ
5e480 4c 20 6c 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e  L literal (TK_IN
5e490 54 45 47 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c  TEGER, TK_FLOAT,
5e4a0 20 54 4b 5f 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72   TK_BLOB, .** or
5e4b0 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20 74 68 65   TK_STRING), the
5e4c0 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e  n Expr.token con
5e4d0 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f  tains the text o
5e4e0 66 20 74 68 65 20 53 51 4c 20 6c 69 74 65 72 61  f the SQL litera
5e4f0 6c 2e 20 49 66 0a 2a 2a 20 74 68 65 20 65 78 70  l. If.** the exp
5e500 72 65 73 73 69 6f 6e 20 69 73 20 61 20 76 61 72  ression is a var
5e510 69 61 62 6c 65 20 28 54 4b 5f 56 41 52 49 41 42  iable (TK_VARIAB
5e520 4c 45 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74  LE), then Expr.t
5e530 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68  oken contains th
5e540 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e  e .** variable n
5e550 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c 20 69 66  ame. Finally, if
5e560 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
5e570 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  is an SQL functi
5e580 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49 4f 4e 29  on (TK_FUNCTION)
5e590 2c 0a 2a 2a 20 74 68 65 6e 20 45 78 70 72 2e 74  ,.** then Expr.t
5e5a0 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68  oken contains th
5e5b0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  e name of the fu
5e5c0 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78  nction..**.** Ex
5e5d0 70 72 2e 70 52 69 67 68 74 20 61 6e 64 20 45 78  pr.pRight and Ex
5e5e0 70 72 2e 70 4c 65 66 74 20 61 72 65 20 74 68 65  pr.pLeft are the
5e5f0 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20   left and right 
5e600 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 6f  subexpressions o
5e610 66 20 61 0a 2a 2a 20 62 69 6e 61 72 79 20 6f 70  f a.** binary op
5e620 65 72 61 74 6f 72 2e 20 45 69 74 68 65 72 20 6f  erator. Either o
5e630 72 20 62 6f 74 68 20 6d 61 79 20 62 65 20 4e 55  r both may be NU
5e640 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78  LL..**.** Expr.x
5e650 2e 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74  .pList is a list
5e660 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 69 66   of arguments if
5e670 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
5e680 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  is an SQL functi
5e690 6f 6e 2c 0a 2a 2a 20 61 20 43 41 53 45 20 65 78  on,.** a CASE ex
5e6a0 70 72 65 73 73 69 6f 6e 20 6f 72 20 61 6e 20 49  pression or an I
5e6b0 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20  N expression of 
5e6c0 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20  the form "<lhs> 
5e6d0 49 4e 20 28 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29  IN (<y>, <z>...)
5e6e0 22 2e 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 53 65  "..** Expr.x.pSe
5e6f0 6c 65 63 74 20 69 73 20 75 73 65 64 20 69 66 20  lect is used if 
5e700 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
5e710 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f  s a sub-select o
5e720 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  r an expression 
5e730 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22  of.** the form "
5e740 3c 6c 68 73 3e 20 49 4e 20 28 53 45 4c 45 43 54  <lhs> IN (SELECT
5e750 20 2e 2e 2e 29 22 2e 20 49 66 20 74 68 65 20 45   ...)". If the E
5e760 50 5f 78 49 73 53 65 6c 65 63 74 20 62 69 74 20  P_xIsSelect bit 
5e770 69 73 20 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a  is set in the.**
5e780 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b   Expr.flags mask
5e790 2c 20 74 68 65 6e 20 45 78 70 72 2e 78 2e 70 53  , then Expr.x.pS
5e7a0 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64 2e 20  elect is valid. 
5e7b0 4f 74 68 65 72 77 69 73 65 2c 20 45 78 70 72 2e  Otherwise, Expr.
5e7c0 78 2e 70 4c 69 73 74 20 69 73 20 0a 2a 2a 20 76  x.pList is .** v
5e7d0 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  alid..**.** An e
5e7e0 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65  xpression of the
5e7f0 20 66 6f 72 6d 20 49 44 20 6f 72 20 49 44 2e 49   form ID or ID.I
5e800 44 20 72 65 66 65 72 73 20 74 6f 20 61 20 63 6f  D refers to a co
5e810 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e  lumn in a table.
5e820 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20 65 78 70  .** For such exp
5e830 72 65 73 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f  ressions, Expr.o
5e840 70 20 69 73 20 73 65 74 20 74 6f 20 54 4b 5f 43  p is set to TK_C
5e850 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69  OLUMN and Expr.i
5e860 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20  Table is.** the 
5e870 69 6e 74 65 67 65 72 20 63 75 72 73 6f 72 20 6e  integer cursor n
5e880 75 6d 62 65 72 20 6f 66 20 61 20 56 44 42 45 20  umber of a VDBE 
5e890 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
5e8a0 74 6f 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e  to that table an
5e8b0 64 0a 2a 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d  d.** Expr.iColum
5e8c0 6e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  n is the column 
5e8d0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73  number for the s
5e8e0 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20  pecific column. 
5e8f0 20 49 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 65   If the.** expre
5e900 73 73 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73  ssion is used as
5e910 20 61 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20   a result in an 
5e920 61 67 67 72 65 67 61 74 65 20 53 45 4c 45 43 54  aggregate SELECT
5e930 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61  , then the.** va
5e940 6c 75 65 20 69 73 20 61 6c 73 6f 20 73 74 6f 72  lue is also stor
5e950 65 64 20 69 6e 20 74 68 65 20 45 78 70 72 2e 69  ed in the Expr.i
5e960 41 67 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  Agg column in th
5e970 65 20 61 67 67 72 65 67 61 74 65 20 73 6f 20 74  e aggregate so t
5e980 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65  hat.** it can be
5e990 20 61 63 63 65 73 73 65 64 20 61 66 74 65 72 20   accessed after 
5e9a0 61 6c 6c 20 61 67 67 72 65 67 61 74 65 73 20 61  all aggregates a
5e9b0 72 65 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a  re computed..**.
5e9c0 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
5e9d0 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75  sion is an unbou
5e9e0 6e 64 20 76 61 72 69 61 62 6c 65 20 6d 61 72 6b  nd variable mark
5e9f0 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e 20 6d  er (a question m
5ea00 61 72 6b 20 0a 2a 2a 20 63 68 61 72 61 63 74 65  ark .** characte
5ea10 72 20 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69  r '?' in the ori
5ea20 67 69 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20  ginal SQL) then 
5ea30 74 68 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20  the Expr.iTable 
5ea40 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 20  holds the index 
5ea50 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  .** number for t
5ea60 68 61 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a  hat variable..**
5ea70 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
5ea80 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 71 75  ssion is a subqu
5ea90 65 72 79 20 74 68 65 6e 20 45 78 70 72 2e 69 43  ery then Expr.iC
5eaa0 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69  olumn holds an i
5eab0 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67 69 73 74  nteger.** regist
5eac0 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69  er number contai
5ead0 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ning the result 
5eae0 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  of the subquery.
5eaf0 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 75 62 71    If the.** subq
5eb00 75 65 72 79 20 67 69 76 65 73 20 61 20 63 6f 6e  uery gives a con
5eb10 73 74 61 6e 74 20 72 65 73 75 6c 74 2c 20 74 68  stant result, th
5eb20 65 6e 20 69 54 61 62 6c 65 20 69 73 20 2d 31 2e  en iTable is -1.
5eb30 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72    If the subquer
5eb40 79 0a 2a 2a 20 67 69 76 65 73 20 61 20 64 69 66  y.** gives a dif
5eb50 66 65 72 65 6e 74 20 61 6e 73 77 65 72 20 61 74  ferent answer at
5eb60 20 64 69 66 66 65 72 65 6e 74 20 74 69 6d 65 73   different times
5eb70 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e   during statemen
5eb80 74 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20  t processing.** 
5eb90 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 74  then iTable is t
5eba0 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20  he address of a 
5ebb0 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
5ebc0 63 6f 6d 70 75 74 65 73 20 74 68 65 20 73 75 62  computes the sub
5ebd0 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  query..**.** If 
5ebe0 74 68 65 20 45 78 70 72 20 69 73 20 6f 66 20 74  the Expr is of t
5ebf0 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61  ype OP_Column, a
5ec00 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 74 20  nd the table it 
5ec10 69 73 20 73 65 6c 65 63 74 69 6e 67 20 66 72 6f  is selecting fro
5ec20 6d 0a 2a 2a 20 69 73 20 61 20 64 69 73 6b 20 74  m.** is a disk t
5ec30 61 62 6c 65 20 6f 72 20 74 68 65 20 22 6f 6c 64  able or the "old
5ec40 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  .*" pseudo-table
5ec50 2c 20 74 68 65 6e 20 70 54 61 62 20 70 6f 69 6e  , then pTab poin
5ec60 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72  ts to the.** cor
5ec70 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65  responding table
5ec80 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a   definition..**.
5ec90 2a 2a 20 41 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f  ** ALLOCATION NO
5eca0 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20  TES:.**.** Expr 
5ecb0 73 74 72 75 63 74 75 72 65 73 20 6d 61 79 20 62  structures may b
5ecc0 65 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74  e stored as part
5ecd0 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   of the in-memor
5ece0 79 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  y database schem
5ecf0 61 2c 0a 2a 2a 20 66 6f 72 20 65 78 61 6d 70 6c  a,.** for exampl
5ed00 65 20 61 73 20 70 61 72 74 20 6f 66 20 74 72 69  e as part of tri
5ed10 67 67 65 72 2c 20 76 69 65 77 20 6f 72 20 74 61  gger, view or ta
5ed20 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e  ble definitions.
5ed30 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a   In this case,.*
5ed40 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  * the amount of 
5ed50 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20  memory consumed 
5ed60 62 79 20 63 6f 6d 70 6c 65 78 20 65 78 70 72 65  by complex expre
5ed70 73 73 69 6f 6e 73 20 6d 61 79 20 62 65 20 73 69  ssions may be si
5ed80 67 6e 69 66 69 63 61 6e 74 2e 0a 2a 2a 20 46 6f  gnificant..** Fo
5ed90 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 6c  r this reason, l
5eda0 65 73 73 20 74 68 61 6e 20 73 69 7a 65 6f 66 28  ess than sizeof(
5edb0 45 78 70 72 29 20 62 79 74 65 73 20 6d 61 79 20  Expr) bytes may 
5edc0 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  be allocated for
5edd0 20 73 6f 6d 65 20 0a 2a 2a 20 45 78 70 72 20 73   some .** Expr s
5ede0 74 72 75 63 74 73 20 73 74 6f 72 65 64 20 61 73  tructs stored as
5edf0 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 2d   part of the in-
5ee00 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
5ee10 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66  schema..**.** If
5ee20 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20   the EP_Reduced 
5ee30 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 45  flag is set in E
5ee40 78 70 72 2e 66 6c 61 67 73 2c 20 74 68 65 6e 20  xpr.flags, then 
5ee50 6f 6e 6c 79 20 45 58 50 52 5f 52 45 44 55 43 45  only EXPR_REDUCE
5ee60 44 53 49 5a 45 0a 2a 2a 20 62 79 74 65 73 20 6f  DSIZE.** bytes o
5ee70 66 20 73 70 61 63 65 20 61 72 65 20 61 6c 6c 6f  f space are allo
5ee80 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 65 78  cated for the ex
5ee90 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75  pression structu
5eea0 72 65 2e 20 54 68 69 73 20 69 73 20 65 6e 6f 75  re. This is enou
5eeb0 67 68 0a 2a 2a 20 73 70 61 63 65 20 74 6f 20 73  gh.** space to s
5eec0 74 6f 72 65 20 61 6c 6c 20 66 69 65 6c 64 73 20  tore all fields 
5eed0 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64  up to and includ
5eee0 69 6e 67 20 74 68 65 20 22 54 6f 6b 65 6e 20 73  ing the "Token s
5eef0 70 61 6e 3b 22 20 66 69 65 6c 64 2e 0a 2a 2a 0a  pan;" field..**.
5ef00 2a 2a 20 49 66 20 74 68 65 20 45 50 5f 54 6f 6b  ** If the EP_Tok
5ef10 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 73  enOnly flag is s
5ef20 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 73  et in Expr.flags
5ef30 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 45 58 50 52  , then only EXPR
5ef40 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 0a 2a  _TOKENONLYSIZE.*
5ef50 2a 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  * bytes of space
5ef60 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 66   are allocated f
5ef70 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  or the expressio
5ef80 6e 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 69  n structure. Thi
5ef90 73 20 69 73 20 65 6e 6f 75 67 68 0a 2a 2a 20 73  s is enough.** s
5efa0 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 61 6c  pace to store al
5efb0 6c 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 61  l fields up to a
5efc0 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  nd including the
5efd0 20 22 54 6f 6b 65 6e 20 74 6f 6b 65 6e 3b 22 20   "Token token;" 
5efe0 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  field..*/.struct
5eff0 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b   Expr {.  u8 op;
5f000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5f010 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65   /* Operation pe
5f020 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20  rformed by this 
5f030 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61  node */.  char a
5f040 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20  ffinity;        
5f050 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
5f060 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   of the column o
5f070 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c  r 0 if not a col
5f080 75 6d 6e 20 2a 2f 0a 20 20 56 56 41 5f 4f 4e 4c  umn */.  VVA_ONL
5f090 59 28 75 38 20 76 76 61 46 6c 61 67 73 3b 29 20  Y(u8 vvaFlags;) 
5f0a0 2f 2a 20 46 6c 61 67 73 20 75 73 65 64 20 66 6f  /* Flags used fo
5f0b0 72 20 56 56 26 41 20 6f 6e 6c 79 2e 20 20 45 56  r VV&A only.  EV
5f0c0 56 41 5f 2a 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20  VA_* below. */. 
5f0d0 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20   u16 flags;     
5f0e0 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f          /* Vario
5f0f0 75 73 20 66 6c 61 67 73 2e 20 20 45 50 5f 2a 20  us flags.  EP_* 
5f100 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 54  See below */.  T
5f110 6f 6b 65 6e 20 74 6f 6b 65 6e 3b 20 20 20 20 20  oken token;     
5f120 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 72        /* An oper
5f130 61 6e 64 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20  and token */..  
5f140 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 54 6f 6b  /* If the EP_Tok
5f150 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 73  enOnly flag is s
5f160 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66  et in the Expr.f
5f170 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20  lags mask, then 
5f180 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73  no.  ** space is
5f190 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
5f1a0 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20  he fields below 
5f1b0 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61  this point. An a
5f1c0 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61  ttempt to.  ** a
5f1d0 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20  ccess them will 
5f1e0 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66  result in a segf
5f1f0 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74  ault or malfunct
5f200 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a  ion. .  ********
5f210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
5f250 20 20 54 6f 6b 65 6e 20 73 70 61 6e 3b 20 20 20    Token span;   
5f260 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70           /* Comp
5f270 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65  lete text of the
5f280 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 0a   expression */..
5f290 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 53    /* If the EP_S
5f2a0 70 61 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20  panOnly flag is 
5f2b0 73 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e  set in the Expr.
5f2c0 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e  flags mask, then
5f2d0 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69   no.  ** space i
5f2e0 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
5f2f0 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77  the fields below
5f300 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20   this point. An 
5f310 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20  attempt to.  ** 
5f320 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c  access them will
5f330 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67   result in a seg
5f340 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63  fault or malfunc
5f350 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a  tion. .  *******
5f360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
5f3a0 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20  .  Expr *pLeft; 
5f3b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66            /* Lef
5f3c0 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 45  t subnode */.  E
5f3d0 78 70 72 20 2a 70 52 69 67 68 74 3b 20 20 20 20  xpr *pRight;    
5f3e0 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 73        /* Right s
5f3f0 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 75 6e 69 6f  ubnode */.  unio
5f400 6e 20 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74  n {.    ExprList
5f410 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20   *pList;     /* 
5f420 46 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  Function argumen
5f430 74 73 20 6f 72 20 69 6e 20 22 3c 65 78 70 72 3e  ts or in "<expr>
5f440 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73 74 29   IN (<expr-list)
5f450 22 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20  " */.    Select 
5f460 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a  *pSelect;     /*
5f470 20 55 73 65 64 20 66 6f 72 20 73 75 62 2d 73 65   Used for sub-se
5f480 6c 65 63 74 73 20 61 6e 64 20 22 3c 65 78 70 72  lects and "<expr
5f490 3e 20 49 4e 20 28 3c 73 65 6c 65 63 74 3e 29 22  > IN (<select>)"
5f4a0 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20 43 6f 6c   */.  } x;.  Col
5f4b0 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20  lSeq *pColl;    
5f4c0 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61      /* The colla
5f4d0 74 69 6f 6e 20 74 79 70 65 20 6f 66 20 74 68 65  tion type of the
5f4e0 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 2a 2f 0a   column or 0 */.
5f4f0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f  .  /* If the EP_
5f500 52 65 64 75 63 65 64 20 66 6c 61 67 20 69 73 20  Reduced flag is 
5f510 73 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e  set in the Expr.
5f520 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e  flags mask, then
5f530 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69   no.  ** space i
5f540 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
5f550 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77  the fields below
5f560 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20   this point. An 
5f570 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20  attempt to.  ** 
5f580 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c  access them will
5f590 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67   result in a seg
5f5a0 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63  fault or malfunc
5f5b0 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a  tion..  ********
5f5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
5f600 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 43    int iTable, iC
5f610 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 65 6e  olumn;   /* When
5f620 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 2c 20   op==TK_COLUMN, 
5f630 74 68 65 6e 20 74 68 69 73 20 65 78 70 72 20 6e  then this expr n
5f640 6f 64 65 20 6d 65 61 6e 73 20 74 68 65 0a 20 20  ode means the.  
5f650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5f660 20 20 20 20 20 20 20 2a 2a 20 69 43 6f 6c 75 6d         ** iColum
5f670 6e 2d 74 68 20 66 69 65 6c 64 20 6f 66 20 74 68  n-th field of th
5f680 65 20 69 54 61 62 6c 65 2d 74 68 20 74 61 62 6c  e iTable-th tabl
5f690 65 2e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20  e. */.  AggInfo 
5f6a0 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 20 20 2f  *pAggInfo;     /
5f6b0 2a 20 55 73 65 64 20 62 79 20 54 4b 5f 41 47 47  * Used by TK_AGG
5f6c0 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 4b 5f 41  _COLUMN and TK_A
5f6d0 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a 2f 0a 20  GG_FUNCTION */. 
5f6e0 20 69 6e 74 20 69 41 67 67 3b 20 20 20 20 20 20   int iAgg;      
5f6f0 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68          /* Which
5f700 20 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e   entry in pAggIn
5f710 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 6f 72 20 2d 3e  fo->aCol[] or ->
5f720 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 69 6e 74  aFunc[] */.  int
5f730 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65   iRightJoinTable
5f740 3b 20 20 20 2f 2a 20 49 66 20 45 50 5f 46 72 6f  ;   /* If EP_Fro
5f750 6d 4a 6f 69 6e 2c 20 74 68 65 20 72 69 67 68 74  mJoin, the right
5f760 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f   table of the jo
5f770 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  in */.  Table *p
5f780 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Tab;           /
5f790 2a 20 54 61 62 6c 65 20 66 6f 72 20 54 4b 5f 43  * Table for TK_C
5f7a0 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e  OLUMN expression
5f7b0 73 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s. */.#if SQLITE
5f7c0 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e  _MAX_EXPR_DEPTH>
5f7d0 30 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 3b  0.  int nHeight;
5f7e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65             /* He
5f7f0 69 67 68 74 20 6f 66 20 74 68 65 20 74 72 65 65  ight of the tree
5f800 20 68 65 61 64 65 64 20 62 79 20 74 68 69 73 20   headed by this 
5f810 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  node */.#endif.}
5f820 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
5f830 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20 6d  lowing are the m
5f840 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 20  eanings of bits 
5f850 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67  in the Expr.flag
5f860 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66  s field..*/.#def
5f870 69 6e 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20  ine EP_FromJoin 
5f880 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 72 69    0x0001  /* Ori
5f890 67 69 6e 61 74 65 64 20 69 6e 20 4f 4e 20 6f 72  ginated in ON or
5f8a0 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66   USING clause of
5f8b0 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69   a join */.#defi
5f8c0 6e 65 20 45 50 5f 41 67 67 20 20 20 20 20 20 20  ne EP_Agg       
5f8d0 20 30 78 30 30 30 32 20 20 2f 2a 20 43 6f 6e 74   0x0002  /* Cont
5f8e0 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ains one or more
5f8f0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
5f900 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ions */.#define 
5f910 45 50 5f 52 65 73 6f 6c 76 65 64 20 20 20 30 78  EP_Resolved   0x
5f920 30 30 30 34 20 20 2f 2a 20 49 44 73 20 68 61 76  0004  /* IDs hav
5f930 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20  e been resolved 
5f940 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 64  to COLUMNs */.#d
5f950 65 66 69 6e 65 20 45 50 5f 45 72 72 6f 72 20 20  efine EP_Error  
5f960 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 45      0x0008  /* E
5f970 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69  xpression contai
5f980 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 65  ns one or more e
5f990 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65  rrors */.#define
5f9a0 20 45 50 5f 44 69 73 74 69 6e 63 74 20 20 20 30   EP_Distinct   0
5f9b0 78 30 30 31 30 20 20 2f 2a 20 41 67 67 72 65 67  x0010  /* Aggreg
5f9c0 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  ate function wit
5f9d0 68 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f  h DISTINCT keywo
5f9e0 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  rd */.#define EP
5f9f0 5f 56 61 72 53 65 6c 65 63 74 20 20 30 78 30 30  _VarSelect  0x00
5fa00 32 30 20 20 2f 2a 20 70 53 65 6c 65 63 74 20 69  20  /* pSelect i
5fa10 73 20 63 6f 72 72 65 6c 61 74 65 64 2c 20 6e 6f  s correlated, no
5fa20 74 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 23 64  t constant */.#d
5fa30 65 66 69 6e 65 20 45 50 5f 44 65 71 75 6f 74 65  efine EP_Dequote
5fa40 64 20 20 20 30 78 30 30 34 30 20 20 2f 2a 20 54  d   0x0040  /* T
5fa50 72 75 65 20 69 66 20 74 68 65 20 73 74 72 69 6e  rue if the strin
5fa60 67 20 68 61 73 20 62 65 65 6e 20 64 65 71 75 6f  g has been dequo
5fa70 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ted */.#define E
5fa80 50 5f 49 6e 66 69 78 46 75 6e 63 20 20 30 78 30  P_InfixFunc  0x0
5fa90 30 38 30 20 20 2f 2a 20 54 72 75 65 20 66 6f 72  080  /* True for
5faa0 20 61 6e 20 69 6e 66 69 78 20 66 75 6e 63 74 69   an infix functi
5fab0 6f 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20  on: LIKE, GLOB, 
5fac0 65 74 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  etc */.#define E
5fad0 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 30 78 30  P_ExpCollate 0x0
5fae0 31 30 30 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e  100  /* Collatin
5faf0 67 20 73 65 71 75 65 6e 63 65 20 73 70 65 63 69  g sequence speci
5fb00 66 69 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20  fied explicitly 
5fb10 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 6e  */.#define EP_An
5fb20 79 41 66 66 20 20 20 20 20 30 78 30 32 30 30 20  yAff     0x0200 
5fb30 20 2f 2a 20 43 61 6e 20 74 61 6b 65 20 61 20 63   /* Can take a c
5fb40 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20  ached column of 
5fb50 61 6e 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a  any affinity */.
5fb60 23 64 65 66 69 6e 65 20 45 50 5f 46 69 78 65 64  #define EP_Fixed
5fb70 44 65 73 74 20 20 30 78 30 34 30 30 20 20 2f 2a  Dest  0x0400  /*
5fb80 20 52 65 73 75 6c 74 20 6e 65 65 64 65 64 20 69   Result needed i
5fb90 6e 20 61 20 73 70 65 63 69 66 69 63 20 72 65 67  n a specific reg
5fba0 69 73 74 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  ister */.#define
5fbb0 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 20 20 30   EP_IntValue   0
5fbc0 78 30 38 30 30 20 20 2f 2a 20 49 6e 74 65 67 65  x0800  /* Intege
5fbd0 72 20 76 61 6c 75 65 20 63 6f 6e 74 61 69 6e 65  r value containe
5fbe0 64 20 69 6e 20 69 54 61 62 6c 65 20 2a 2f 0a 23  d in iTable */.#
5fbf0 64 65 66 69 6e 65 20 45 50 5f 78 49 73 53 65 6c  define EP_xIsSel
5fc00 65 63 74 20 20 30 78 31 30 30 30 20 20 2f 2a 20  ect  0x1000  /* 
5fc10 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76 61 6c  x.pSelect is val
5fc20 69 64 20 28 6f 74 68 65 72 77 69 73 65 20 78 2e  id (otherwise x.
5fc30 70 4c 69 73 74 20 69 73 29 20 2a 2f 0a 0a 23 64  pList is) */..#d
5fc40 65 66 69 6e 65 20 45 50 5f 52 65 64 75 63 65 64  efine EP_Reduced
5fc50 20 20 20 20 30 78 32 30 30 30 20 20 2f 2a 20 45      0x2000  /* E
5fc60 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45 58  xpr struct is EX
5fc70 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 62  PR_REDUCEDSIZE b
5fc80 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65  ytes only */.#de
5fc90 66 69 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  fine EP_TokenOnl
5fca0 79 20 20 30 78 34 30 30 30 20 20 2f 2a 20 45 78  y  0x4000  /* Ex
5fcb0 70 72 20 73 74 72 75 63 74 20 69 73 20 45 58 50  pr struct is EXP
5fcc0 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20  R_TOKENONLYSIZE 
5fcd0 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64  bytes only */.#d
5fce0 65 66 69 6e 65 20 45 50 5f 53 70 61 6e 4f 6e 6c  efine EP_SpanOnl
5fcf0 79 20 20 20 30 78 38 30 30 30 20 20 2f 2a 20 45  y   0x8000  /* E
5fd00 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45 58  xpr struct is EX
5fd10 50 52 5f 53 50 41 4e 4f 4e 4c 59 53 49 5a 45 20  PR_SPANONLYSIZE 
5fd20 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f  bytes only */../
5fd30 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
5fd40 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69  ng are the meani
5fd50 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74  ngs of bits in t
5fd60 68 65 20 45 78 70 72 2e 76 76 61 46 6c 61 67 73  he Expr.vvaFlags
5fd70 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 69 73 20   field..** This 
5fd80 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6f  information is o
5fd90 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 53 51  nly used when SQ
5fda0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
5fdb0 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f   with.** SQLITE_
5fdc0 44 45 42 55 47 20 64 65 66 69 6e 65 64 2e 0a 2a  DEBUG defined..*
5fdd0 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
5fde0 0a 23 64 65 66 69 6e 65 20 45 56 56 41 5f 52 65  .#define EVVA_Re
5fdf0 61 64 4f 6e 6c 79 54 6f 6b 65 6e 20 20 30 78 30  adOnlyToken  0x0
5fe00 31 20 20 2f 2a 20 45 78 70 72 2e 74 6f 6b 65 6e  1  /* Expr.token
5fe10 2e 7a 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 20  .z is read-only 
5fe20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  */.#endif../*.**
5fe30 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61   These macros ca
5fe40 6e 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73  n be used to tes
5fe50 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72  t, set, or clear
5fe60 20 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a   bits in the .**
5fe70 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65 6c   Expr.flags fiel
5fe80 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 78  d..*/.#define Ex
5fe90 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 45 2c  prHasProperty(E,
5fea0 50 29 20 20 20 20 20 28 28 28 45 29 2d 3e 66 6c  P)     (((E)->fl
5feb0 61 67 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23  ags&(P))==(P)).#
5fec0 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 41 6e  define ExprHasAn
5fed0 79 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  yProperty(E,P)  
5fee0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29  (((E)->flags&(P)
5fef0 29 21 3d 30 29 0a 23 64 65 66 69 6e 65 20 45 78  )!=0).#define Ex
5ff00 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 45 2c  prSetProperty(E,
5ff10 50 29 20 20 20 20 20 28 45 29 2d 3e 66 6c 61 67  P)     (E)->flag
5ff20 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 45  s|=(P).#define E
5ff30 78 70 72 43 6c 65 61 72 50 72 6f 70 65 72 74 79  xprClearProperty
5ff40 28 45 2c 50 29 20 20 20 28 45 29 2d 3e 66 6c 61  (E,P)   (E)->fla
5ff50 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20  gs&=~(P)../*.** 
5ff60 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d  Macros to determ
5ff70 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ine the number o
5ff80 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
5ff90 20 62 79 20 61 20 6e 6f 72 6d 61 6c 20 45 78 70   by a normal Exp
5ffa0 72 20 0a 2a 2a 20 73 74 72 75 63 74 2c 20 61 6e  r .** struct, an
5ffb0 20 45 78 70 72 20 73 74 72 75 63 74 20 77 69 74   Expr struct wit
5ffc0 68 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64  h the EP_Reduced
5ffd0 20 66 6c 61 67 20 73 65 74 20 69 6e 20 45 78 70   flag set in Exp
5ffe0 72 2e 66 6c 61 67 73 20 0a 2a 2a 20 61 6e 64 20  r.flags .** and 
5fff0 61 6e 20 45 78 70 72 20 73 74 72 75 63 74 20 77  an Expr struct w
60000 69 74 68 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e  ith the EP_Token
60010 4f 6e 6c 79 20 66 6c 61 67 20 73 65 74 2e 0a 2a  Only flag set..*
60020 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 46  /.#define EXPR_F
60030 55 4c 4c 53 49 5a 45 20 20 20 20 20 20 20 20 20  ULLSIZE         
60040 20 20 73 69 7a 65 6f 66 28 45 78 70 72 29 0a 23    sizeof(Expr).#
60050 64 65 66 69 6e 65 20 45 58 50 52 5f 52 45 44 55  define EXPR_REDU
60060 43 45 44 53 49 5a 45 20 20 20 20 20 20 20 20 6f  CEDSIZE        o
60070 66 66 73 65 74 6f 66 28 45 78 70 72 2c 69 54 61  ffsetof(Expr,iTa
60080 62 6c 65 29 0a 23 64 65 66 69 6e 65 20 45 58 50  ble).#define EXP
60090 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20  R_TOKENONLYSIZE 
600a0 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78       offsetof(Ex
600b0 70 72 2c 73 70 61 6e 29 0a 23 64 65 66 69 6e 65  pr,span).#define
600c0 20 45 58 50 52 5f 53 50 41 4e 4f 4e 4c 59 53 49   EXPR_SPANONLYSI
600d0 5a 45 20 20 20 20 20 20 20 6f 66 66 73 65 74 6f  ZE       offseto
600e0 66 28 45 78 70 72 2c 70 4c 65 66 74 29 0a 0a 2f  f(Expr,pLeft)../
600f0 2a 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65  *.** Flags passe
60100 64 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  d to the sqlite3
60110 45 78 70 72 44 75 70 28 29 20 66 75 6e 63 74 69  ExprDup() functi
60120 6f 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61 64  on. See the head
60130 65 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61  er comment .** a
60140 62 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70 72  bove sqlite3Expr
60150 44 75 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c  Dup() for detail
60160 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58  s..*/.#define EX
60170 50 52 44 55 50 5f 52 45 44 55 43 45 20 20 20 20  PRDUP_REDUCE    
60180 20 20 20 20 20 30 78 30 30 30 31 0a 23 64 65 66       0x0001.#def
60190 69 6e 65 20 45 58 50 52 44 55 50 5f 53 50 41 4e  ine EXPRDUP_SPAN
601a0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30             0x000
601b0 32 0a 23 64 65 66 69 6e 65 20 45 58 50 52 44 55  2.#define EXPRDU
601c0 50 5f 44 49 53 54 49 4e 43 54 53 50 41 4e 20 20  P_DISTINCTSPAN  
601d0 20 30 78 30 30 30 34 0a 0a 2f 2a 0a 2a 2a 20 41   0x0004../*.** A
601e0 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
601f0 69 6f 6e 73 2e 20 20 45 61 63 68 20 65 78 70 72  ions.  Each expr
60200 65 73 73 69 6f 6e 20 6d 61 79 20 6f 70 74 69 6f  ession may optio
60210 6e 61 6c 6c 79 20 68 61 76 65 20 61 0a 2a 2a 20  nally have a.** 
60220 6e 61 6d 65 2e 20 20 41 6e 20 65 78 70 72 2f 6e  name.  An expr/n
60230 61 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  ame combination 
60240 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 73  can be used in s
60250 65 76 65 72 61 6c 20 77 61 79 73 2c 20 73 75 63  everal ways, suc
60260 68 0a 2a 2a 20 61 73 20 74 68 65 20 6c 69 73 74  h.** as the list
60270 20 6f 66 20 22 65 78 70 72 20 41 53 20 49 44 22   of "expr AS ID"
60280 20 66 69 65 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e   fields followin
60290 67 20 61 20 22 53 45 4c 45 43 54 22 20 6f 72 20  g a "SELECT" or 
602a0 69 6e 20 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f  in the.** list o
602b0 66 20 22 49 44 20 3d 20 65 78 70 72 22 20 69 74  f "ID = expr" it
602c0 65 6d 73 20 69 6e 20 61 6e 20 55 50 44 41 54 45  ems in an UPDATE
602d0 2e 20 20 41 20 6c 69 73 74 20 6f 66 20 65 78 70  .  A list of exp
602e0 72 65 73 73 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20  ressions can.** 
602f0 61 6c 73 6f 20 62 65 20 75 73 65 64 20 61 73 20  also be used as 
60300 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  the argument to 
60310 61 20 66 75 6e 63 74 69 6f 6e 2c 20 69 6e 20 77  a function, in w
60320 68 69 63 68 20 63 61 73 65 20 74 68 65 20 61 2e  hich case the a.
60330 7a 4e 61 6d 65 0a 2a 2a 20 66 69 65 6c 64 20 69  zName.** field i
60340 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73  s not used..*/.s
60350 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 20 7b  truct ExprList {
60360 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20  .  int nExpr;   
60370 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
60380 62 65 72 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ber of expressio
60390 6e 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a  ns on the list *
603a0 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20  /.  int nAlloc; 
603b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
603c0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
603d0 61 6c 6c 6f 63 61 74 65 64 20 62 65 6c 6f 77 20  allocated below 
603e0 2a 2f 0a 20 20 69 6e 74 20 69 45 43 75 72 73 6f  */.  int iECurso
603f0 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  r;          /* V
60400 44 42 45 20 43 75 72 73 6f 72 20 61 73 73 6f 63  DBE Cursor assoc
60410 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
60420 45 78 70 72 4c 69 73 74 20 2a 2f 0a 20 20 73 74  ExprList */.  st
60430 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
60440 65 6d 20 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  em {.    Expr *p
60450 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
60460 2f 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20 65  /* The list of e
60470 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20  xpressions */.  
60480 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
60490 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65           /* Toke
604a0 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  n associated wit
604b0 68 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  h this expressio
604c0 6e 20 2a 2f 0a 20 20 20 20 75 38 20 73 6f 72 74  n */.    u8 sort
604d0 4f 72 64 65 72 3b 20 20 20 20 20 20 20 20 20 20  Order;          
604e0 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 20 6f 72  /* 1 for DESC or
604f0 20 30 20 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20   0 for ASC */.  
60500 20 20 75 38 20 64 6f 6e 65 3b 20 20 20 20 20 20    u8 done;      
60510 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 66 6c           /* A fl
60520 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77  ag to indicate w
60530 68 65 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69  hen processing i
60540 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20  s finished */.  
60550 20 20 75 31 36 20 69 43 6f 6c 3b 20 20 20 20 20    u16 iCol;     
60560 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
60570 4f 52 44 45 52 20 42 59 2c 20 63 6f 6c 75 6d 6e  ORDER BY, column
60580 20 6e 75 6d 62 65 72 20 69 6e 20 72 65 73 75 6c   number in resul
60590 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 75 31 36  t set */.    u16
605a0 20 69 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20   iAlias;        
605b0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74      /* Index int
605c0 6f 20 50 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d  o Parse.aAlias[]
605d0 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20   for zName */.  
605e0 7d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20  } *a;           
605f0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e         /* One en
60600 74 72 79 20 66 6f 72 20 65 61 63 68 20 65 78 70  try for each exp
60610 72 65 73 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f  ression */.};../
60620 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
60630 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
60640 72 65 20 63 61 6e 20 68 6f 6c 64 20 61 20 73 69  re can hold a si
60650 6d 70 6c 65 20 6c 69 73 74 20 6f 66 20 69 64 65  mple list of ide
60660 6e 74 69 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63  ntifiers,.** suc
60670 68 20 61 73 20 74 68 65 20 6c 69 73 74 20 22 61  h as the list "a
60680 2c 62 2c 63 22 20 69 6e 20 74 68 65 20 66 6f 6c  ,b,c" in the fol
60690 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
606a0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e  s:.**.**      IN
606b0 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c  SERT INTO t(a,b,
606c0 63 29 20 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a  c) VALUES ...;.*
606d0 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 49 4e  *      CREATE IN
606e0 44 45 58 20 69 64 78 20 4f 4e 20 74 28 61 2c 62  DEX idx ON t(a,b
606f0 2c 63 29 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45  ,c);.**      CRE
60700 41 54 45 20 54 52 49 47 47 45 52 20 74 72 69 67  ATE TRIGGER trig
60710 20 42 45 46 4f 52 45 20 55 50 44 41 54 45 20 4f   BEFORE UPDATE O
60720 4e 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a  N t(a,b,c) ...;.
60730 2a 2a 0a 2a 2a 20 54 68 65 20 49 64 4c 69 73 74  **.** The IdList
60740 2e 61 2e 69 64 78 20 66 69 65 6c 64 20 69 73 20  .a.idx field is 
60750 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 49 64  used when the Id
60760 4c 69 73 74 20 72 65 70 72 65 73 65 6e 74 73 20  List represents 
60770 74 68 65 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63  the list of.** c
60780 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65  olumn names afte
60790 72 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  r a table name i
607a0 6e 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74  n an INSERT stat
607b0 65 6d 65 6e 74 2e 20 20 49 6e 20 74 68 65 20 73  ement.  In the s
607c0 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20  tatement.**.**  
607d0 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
607e0 28 61 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a  (a,b,c) ....**.*
607f0 2a 20 49 66 20 22 61 22 20 69 73 20 74 68 65 20  * If "a" is the 
60800 6b 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  k-th column of t
60810 61 62 6c 65 20 22 74 22 2c 20 74 68 65 6e 20 49  able "t", then I
60820 64 4c 69 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d  dList.a[0].idx==
60830 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c  k..*/.struct IdL
60840 69 73 74 20 7b 0a 20 20 73 74 72 75 63 74 20 49  ist {.  struct I
60850 64 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20  dList_item {.   
60860 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
60870 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
60880 65 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a  e identifier */.
60890 20 20 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20      int idx;    
608a0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
608b0 6e 20 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f  n some Table.aCo
608c0 6c 5b 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20  l[] of a column 
608d0 6e 61 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20  named zName */. 
608e0 20 7d 20 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64   } *a;.  int nId
608f0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
60900 62 65 72 20 6f 66 20 69 64 65 6e 74 69 66 69 65  ber of identifie
60910 72 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a  rs on the list *
60920 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20  /.  int nAlloc; 
60930 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
60940 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
60950 74 65 64 20 66 6f 72 20 61 5b 5d 20 62 65 6c 6f  ted for a[] belo
60960 77 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  w */.};../*.** T
60970 68 65 20 62 69 74 6d 61 73 6b 20 64 61 74 61 74  he bitmask datat
60980 79 70 65 20 64 65 66 69 6e 65 64 20 62 65 6c 6f  ype defined belo
60990 77 20 69 73 20 75 73 65 64 20 66 6f 72 20 76 61  w is used for va
609a0 72 69 6f 75 73 20 6f 70 74 69 6d 69 7a 61 74 69  rious optimizati
609b0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67  ons..**.** Chang
609c0 69 6e 67 20 74 68 69 73 20 66 72 6f 6d 20 61 20  ing this from a 
609d0 36 34 2d 62 69 74 20 74 6f 20 61 20 33 32 2d 62  64-bit to a 32-b
609e0 69 74 20 74 79 70 65 20 6c 69 6d 69 74 73 20 74  it type limits t
609f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
60a00 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e  tables in a join
60a10 20 74 6f 20 33 32 20 69 6e 73 74 65 61 64 20 6f   to 32 instead o
60a20 66 20 36 34 2e 20 20 42 75 74 20 69 74 20 61 6c  f 64.  But it al
60a30 73 6f 20 72 65 64 75 63 65 73 20 74 68 65 20 73  so reduces the s
60a40 69 7a 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 69  ize.** of the li
60a50 62 72 61 72 79 20 62 79 20 37 33 38 20 62 79 74  brary by 738 byt
60a60 65 73 20 6f 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74  es on ix86..*/.t
60a70 79 70 65 64 65 66 20 75 36 34 20 42 69 74 6d 61  ypedef u64 Bitma
60a80 73 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  sk;../*.** The n
60a90 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e  umber of bits in
60aa0 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 22 42 4d   a Bitmask.  "BM
60ab0 53 22 20 6d 65 61 6e 73 20 22 42 69 74 4d 61 73  S" means "BitMas
60ac0 6b 20 53 69 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66  k Size"..*/.#def
60ad0 69 6e 65 20 42 4d 53 20 20 28 28 69 6e 74 29 28  ine BMS  ((int)(
60ae0 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a  sizeof(Bitmask)*
60af0 38 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  8))../*.** The f
60b00 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
60b10 72 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65  re describes the
60b20 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
60b30 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
60b40 6e 74 2e 0a 2a 2a 20 45 61 63 68 20 74 61 62 6c  nt..** Each tabl
60b50 65 20 6f 72 20 73 75 62 71 75 65 72 79 20 69 6e  e or subquery in
60b60 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
60b70 20 69 73 20 61 20 73 65 70 61 72 61 74 65 20 65   is a separate e
60b80 6c 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20 74 68 65  lement of.** the
60b90 20 53 72 63 4c 69 73 74 2e 61 5b 5d 20 61 72 72   SrcList.a[] arr
60ba0 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74  ay..**.** With t
60bb0 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 6d  he addition of m
60bc0 75 6c 74 69 70 6c 65 20 64 61 74 61 62 61 73 65  ultiple database
60bd0 20 73 75 70 70 6f 72 74 2c 20 74 68 65 20 66 6f   support, the fo
60be0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
60bf0 65 0a 2a 2a 20 63 61 6e 20 61 6c 73 6f 20 62 65  e.** can also be
60c00 20 75 73 65 64 20 74 6f 20 64 65 73 63 72 69 62   used to describ
60c10 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74  e a particular t
60c20 61 62 6c 65 20 73 75 63 68 20 61 73 20 74 68 65  able such as the
60c30 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 69   table that.** i
60c40 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e  s modified by an
60c50 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c   INSERT, DELETE,
60c60 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65   or UPDATE state
60c70 6d 65 6e 74 2e 20 20 49 6e 20 73 74 61 6e 64 61  ment.  In standa
60c80 72 64 20 53 51 4c 2c 0a 2a 2a 20 73 75 63 68 20  rd SQL,.** such 
60c90 61 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20  a table must be 
60ca0 61 20 73 69 6d 70 6c 65 20 6e 61 6d 65 3a 20 49  a simple name: I
60cb0 44 2e 20 20 42 75 74 20 69 6e 20 53 51 4c 69 74  D.  But in SQLit
60cc0 65 2c 20 74 68 65 20 74 61 62 6c 65 20 63 61 6e  e, the table can
60cd0 0a 2a 2a 20 6e 6f 77 20 62 65 20 69 64 65 6e 74  .** now be ident
60ce0 69 66 69 65 64 20 62 79 20 61 20 64 61 74 61 62  ified by a datab
60cf0 61 73 65 20 6e 61 6d 65 2c 20 61 20 64 6f 74 2c  ase name, a dot,
60d00 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   then the table 
60d10 6e 61 6d 65 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a  name: ID.ID..**.
60d20 2a 2a 20 54 68 65 20 6a 6f 69 6e 74 79 70 65 20  ** The jointype 
60d30 73 74 61 72 74 73 20 6f 75 74 20 73 68 6f 77 69  starts out showi
60d40 6e 67 20 74 68 65 20 6a 6f 69 6e 20 74 79 70 65  ng the join type
60d50 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 75 72   between the cur
60d60 72 65 6e 74 20 74 61 62 6c 65 0a 2a 2a 20 61 6e  rent table.** an
60d70 64 20 74 68 65 20 6e 65 78 74 20 74 61 62 6c 65  d the next table
60d80 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 20 54   on the list.  T
60d90 68 65 20 70 61 72 73 65 72 20 62 75 69 6c 64 73  he parser builds
60da0 20 74 68 65 20 6c 69 73 74 20 74 68 69 73 20 77   the list this w
60db0 61 79 2e 0a 2a 2a 20 42 75 74 20 73 71 6c 69 74  ay..** But sqlit
60dc0 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f  e3SrcListShiftJo
60dd0 69 6e 54 79 70 65 28 29 20 6c 61 74 65 72 20 73  inType() later s
60de0 68 69 66 74 73 20 74 68 65 20 6a 6f 69 6e 74 79  hifts the jointy
60df0 70 65 73 20 73 6f 20 74 68 61 74 20 65 61 63 68  pes so that each
60e00 0a 2a 2a 20 6a 6f 69 6e 74 79 70 65 20 65 78 70  .** jointype exp
60e10 72 65 73 73 65 73 20 74 68 65 20 6a 6f 69 6e 20  resses the join 
60e20 62 65 74 77 65 65 6e 20 74 68 65 20 74 61 62 6c  between the tabl
60e30 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f  e and the previo
60e40 75 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72  us table..*/.str
60e50 75 63 74 20 53 72 63 4c 69 73 74 20 7b 0a 20 20  uct SrcList {.  
60e60 69 31 36 20 6e 53 72 63 3b 20 20 20 20 20 20 20  i16 nSrc;       
60e70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61   /* Number of ta
60e80 62 6c 65 73 20 6f 72 20 73 75 62 71 75 65 72 69  bles or subqueri
60e90 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
60ea0 6c 61 75 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e  lause */.  i16 n
60eb0 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e  Alloc;      /* N
60ec0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
60ed0 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b   allocated in a[
60ee0 5d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72  ] below */.  str
60ef0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
60f00 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 61   {.    char *zDa
60f10 74 61 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65  tabase;  /* Name
60f20 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c   of database hol
60f30 64 69 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20  ding this table 
60f40 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61  */.    char *zNa
60f50 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  me;      /* Name
60f60 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
60f70 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 6c 69 61  .    char *zAlia
60f80 73 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 22 42  s;     /* The "B
60f90 22 20 70 61 72 74 20 6f 66 20 61 20 22 41 20 41  " part of a "A A
60fa0 53 20 42 22 20 70 68 72 61 73 65 2e 20 20 7a 4e  S B" phrase.  zN
60fb0 61 6d 65 20 69 73 20 74 68 65 20 22 41 22 20 2a  ame is the "A" *
60fc0 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  /.    Table *pTa
60fd0 62 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 53 51  b;      /* An SQ
60fe0 4c 20 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f  L table correspo
60ff0 6e 64 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a  nding to zName *
61000 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53  /.    Select *pS
61010 65 6c 65 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c  elect;  /* A SEL
61020 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 75 73  ECT statement us
61030 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61  ed in place of a
61040 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
61050 20 20 20 75 38 20 69 73 50 6f 70 75 6c 61 74 65     u8 isPopulate
61060 64 3b 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72  d;   /* Temporar
61070 79 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74  y table associat
61080 65 64 20 77 69 74 68 20 53 45 4c 45 43 54 20 69  ed with SELECT i
61090 73 20 70 6f 70 75 6c 61 74 65 64 20 2a 2f 0a 20  s populated */. 
610a0 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 3b 20     u8 jointype; 
610b0 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20       /* Type of 
610c0 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 68 69  join between thi
610d0 73 20 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70  s able and the p
610e0 72 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20 20 75  revious */.    u
610f0 38 20 6e 6f 74 49 6e 64 65 78 65 64 3b 20 20 20  8 notIndexed;   
61100 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72   /* True if ther
61110 65 20 69 73 20 61 20 4e 4f 54 20 49 4e 44 45 58  e is a NOT INDEX
61120 45 44 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  ED clause */.   
61130 20 69 6e 74 20 69 43 75 72 73 6f 72 3b 20 20 20   int iCursor;   
61140 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63     /* The VDBE c
61150 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65  ursor number use
61160 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73  d to access this
61170 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78   table */.    Ex
61180 70 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20  pr *pOn;        
61190 2f 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65  /* The ON clause
611a0 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20   of a join */.  
611b0 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67    IdList *pUsing
611c0 3b 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47  ;   /* The USING
611d0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69   clause of a joi
611e0 6e 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b  n */.    Bitmask
611f0 20 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69   colUsed;  /* Bi
61200 74 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74 20 69  t N (1<<N) set i
61210 66 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54  f column N of pT
61220 61 62 20 69 73 20 75 73 65 64 20 2a 2f 0a 20 20  ab is used */.  
61230 20 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20    char *zIndex; 
61240 20 20 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65      /* Identifie
61250 72 20 66 72 6f 6d 20 22 49 4e 44 45 58 45 44 20  r from "INDEXED 
61260 42 59 20 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61  BY <zIndex>" cla
61270 75 73 65 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78  use */.    Index
61280 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20   *pIndex;    /* 
61290 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20  Index structure 
612a0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
612b0 20 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e 79 20   zIndex, if any 
612c0 2a 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20  */.  } a[1];    
612d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
612e0 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 69  entry for each i
612f0 64 65 6e 74 69 66 69 65 72 20 6f 6e 20 74 68 65  dentifier on the
61300 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   list */.};../*.
61310 2a 2a 20 50 65 72 6d 69 74 74 65 64 20 76 61 6c  ** Permitted val
61320 75 65 73 20 6f 66 20 74 68 65 20 53 72 63 4c 69  ues of the SrcLi
61330 73 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20 66 69  st.a.jointype fi
61340 65 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  eld.*/.#define J
61350 54 5f 49 4e 4e 45 52 20 20 20 20 20 30 78 30 30  T_INNER     0x00
61360 30 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e  01    /* Any kin
61370 64 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20 63 72  d of inner or cr
61380 6f 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  oss join */.#def
61390 69 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20 20 20  ine JT_CROSS    
613a0 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20 45 78   0x0002    /* Ex
613b0 70 6c 69 63 69 74 20 75 73 65 20 6f 66 20 74 68  plicit use of th
613c0 65 20 43 52 4f 53 53 20 6b 65 79 77 6f 72 64 20  e CROSS keyword 
613d0 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4e 41  */.#define JT_NA
613e0 54 55 52 41 4c 20 20 20 30 78 30 30 30 34 20 20  TURAL   0x0004  
613f0 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
61400 22 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e 20 2a  "natural" join *
61410 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c 45 46  /.#define JT_LEF
61420 54 20 20 20 20 20 20 30 78 30 30 30 38 20 20 20  T      0x0008   
61430 20 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72 20 6a   /* Left outer j
61440 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  oin */.#define J
61450 54 5f 52 49 47 48 54 20 20 20 20 20 30 78 30 30  T_RIGHT     0x00
61460 31 30 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f  10    /* Right o
61470 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65  uter join */.#de
61480 66 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20 20 20  fine JT_OUTER   
61490 20 20 30 78 30 30 32 30 20 20 20 20 2f 2a 20 54    0x0020    /* T
614a0 68 65 20 22 4f 55 54 45 52 22 20 6b 65 79 77 6f  he "OUTER" keywo
614b0 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
614c0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 45 52 52 4f  .#define JT_ERRO
614d0 52 20 20 20 20 20 30 78 30 30 34 30 20 20 20 20  R     0x0040    
614e0 2f 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e  /* unknown or un
614f0 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74  supported join t
61500 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41  ype */.../*.** A
61510 20 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63   WherePlan objec
61520 74 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74  t holds informat
61530 69 6f 6e 20 74 68 61 74 20 64 65 73 63 72 69 62  ion that describ
61540 65 73 20 61 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73  es a lookup.** s
61550 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54  trategy..**.** T
61560 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 69 6e  his object is in
61570 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61  tended to be opa
61580 71 75 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74  que outside of t
61590 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c  he where.c modul
615a0 65 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e 63 6c  e..** It is incl
615b0 75 64 65 64 20 68 65 72 65 20 6f 6e 6c 79 20 73  uded here only s
615c0 6f 20 74 68 61 74 20 74 68 61 74 20 63 6f 6d 70  o that that comp
615d0 69 6c 65 72 20 77 69 6c 6c 20 6b 6e 6f 77 20 68  iler will know h
615e0 6f 77 20 62 69 67 20 69 74 0a 2a 2a 20 69 73 2e  ow big it.** is.
615f0 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 69    None of the fi
61600 65 6c 64 73 20 69 6e 20 74 68 69 73 20 6f 62 6a  elds in this obj
61610 65 63 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73  ect should be us
61620 65 64 20 6f 75 74 73 69 64 65 20 6f 66 0a 2a 2a  ed outside of.**
61630 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64   the where.c mod
61640 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69  ule..**.** Withi
61650 6e 20 74 68 65 20 75 6e 69 6f 6e 2c 20 70 49 64  n the union, pId
61660 78 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77  x is only used w
61670 68 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52  hen wsFlags&WHER
61680 45 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75  E_INDEXED is tru
61690 65 2e 0a 2a 2a 20 70 54 65 72 6d 20 69 73 20 6f  e..** pTerm is o
616a0 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73  nly used when ws
616b0 46 6c 61 67 73 26 57 48 45 52 45 5f 4d 55 4c 54  Flags&WHERE_MULT
616c0 49 5f 4f 52 20 69 73 20 74 72 75 65 2e 20 20 41  I_OR is true.  A
616d0 6e 64 20 70 56 74 61 62 49 64 78 0a 2a 2a 20 69  nd pVtabIdx.** i
616e0 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e  s only used when
616f0 20 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 56   wsFlags&WHERE_V
61700 49 52 54 55 41 4c 54 41 42 4c 45 20 69 73 20 74  IRTUALTABLE is t
61710 72 75 65 2e 20 20 49 74 20 69 73 20 6e 65 76 65  rue.  It is neve
61720 72 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68  r the.** case th
61730 61 74 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  at more than one
61740 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74   of these condit
61750 69 6f 6e 73 20 69 73 20 74 72 75 65 2e 0a 2a 2f  ions is true..*/
61760 0a 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61  .struct WherePla
61770 6e 20 7b 0a 20 20 75 33 32 20 77 73 46 6c 61 67  n {.  u32 wsFlag
61780 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
61790 20 20 20 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20       /* WHERE_* 
617a0 66 6c 61 67 73 20 74 68 61 74 20 64 65 73 63 72  flags that descr
617b0 69 62 65 20 74 68 65 20 73 74 72 61 74 65 67 79  ibe the strategy
617c0 20 2a 2f 0a 20 20 75 33 32 20 6e 45 71 3b 20 20   */.  u32 nEq;  
617d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
617e0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
617f0 66 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73  f == constraints
61800 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20   */.  union {.  
61810 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
61820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61830 20 2f 2a 20 49 6e 64 65 78 20 77 68 65 6e 20 57   /* Index when W
61840 48 45 52 45 5f 49 4e 44 45 58 45 44 20 69 73 20  HERE_INDEXED is 
61850 74 72 75 65 20 2a 2f 0a 20 20 20 20 73 74 72 75  true */.    stru
61860 63 74 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  ct WhereTerm *pT
61870 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20 57 48  erm;       /* WH
61880 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20  ERE clause term 
61890 66 6f 72 20 4f 52 2d 73 65 61 72 63 68 20 2a 2f  for OR-search */
618a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64  .    sqlite3_ind
618b0 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64  ex_info *pVtabId
618c0 78 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74  x;  /* Virtual t
618d0 61 62 6c 65 20 69 6e 64 65 78 20 74 6f 20 75 73  able index to us
618e0 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a  e */.  } u;.};..
618f0 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 6e  /*.** For each n
61900 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20 61 20  ested loop in a 
61910 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 6d 70  WHERE clause imp
61920 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 68 65  lementation, the
61930 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20 73 74   WhereInfo.** st
61940 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
61950 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e   a single instan
61960 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
61970 74 75 72 65 2e 20 20 54 68 69 73 20 73 74 72 75  ture.  This stru
61980 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e 74 65  cture.** is inte
61990 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69 76 61  nded to be priva
619a0 74 65 20 74 68 65 20 74 68 65 20 77 68 65 72 65  te the the where
619b0 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 73 68  .c module and sh
619c0 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61  ould not be.** a
619d0 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 69 65  ccess or modifie
619e0 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c  d by other modul
619f0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 49  es..**.** The pI
61a00 64 78 49 6e 66 6f 20 66 69 65 6c 64 20 69 73 20  dxInfo field is 
61a10 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69 63  used to help pic
61a20 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78  k the best index
61a30 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c   on a.** virtual
61a40 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49 64   table.  The pId
61a50 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63 6f  xInfo pointer co
61a60 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67 0a  ntains indexing.
61a70 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ** information f
61a80 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c  or the i-th tabl
61a90 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  e in the FROM cl
61aa0 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f 72  ause before reor
61ab0 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74  dering..** All t
61ac0 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e  he pIdxInfo poin
61ad0 74 65 72 73 20 61 72 65 20 66 72 65 65 64 20 62  ters are freed b
61ae0 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28  y whereInfoFree(
61af0 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a 2a  ) in where.c..**
61b00 20 41 6c 6c 20 6f 74 68 65 72 20 69 6e 66 6f 72   All other infor
61b10 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 69 2d  mation in the i-
61b20 74 68 20 57 68 65 72 65 4c 65 76 65 6c 20 6f 62  th WhereLevel ob
61b30 6a 65 63 74 20 66 6f 72 20 74 68 65 20 69 2d 74  ject for the i-t
61b40 68 20 74 61 62 6c 65 0a 2a 2a 20 61 66 74 65 72  h table.** after
61b50 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 72 64   FROM clause ord
61b60 65 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74  ering..*/.struct
61b70 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a 20 20   WhereLevel {.  
61b80 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20  WherePlan plan; 
61b90 20 20 20 20 20 20 2f 2a 20 71 75 65 72 79 20 70        /* query p
61ba0 6c 61 6e 20 66 6f 72 20 74 68 69 73 20 65 6c 65  lan for this ele
61bb0 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d  ment of the FROM
61bc0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
61bd0 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20 20 20   iLeftJoin;     
61be0 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c     /* Memory cel
61bf0 6c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  l used to implem
61c00 65 6e 74 20 4c 45 46 54 20 4f 55 54 45 52 20 4a  ent LEFT OUTER J
61c10 4f 49 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  OIN */.  int iTa
61c20 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f  bCur;          /
61c30 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
61c40 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  r used to access
61c50 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
61c60 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20  int iIdxCur;    
61c70 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
61c80 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f  E cursor used to
61c90 20 61 63 63 65 73 73 20 70 49 64 78 20 2a 2f 0a   access pIdx */.
61ca0 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b 20 20    int addrBrk;  
61cb0 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
61cc0 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75  here to break ou
61cd0 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  t of the loop */
61ce0 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b 20  .  int addrNxt; 
61cf0 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
61d00 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74   here to start t
61d10 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69  he next IN combi
61d20 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nation */.  int 
61d30 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 20 20  addrCont;       
61d40 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
61d50 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
61d60 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79  the next loop cy
61d70 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  cle */.  int add
61d80 72 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 2f  rFirst;        /
61d90 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74  * First instruct
61da0 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20  ion of interior 
61db0 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
61dc0 20 75 38 20 69 46 72 6f 6d 3b 20 20 20 20 20 20   u8 iFrom;      
61dd0 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20         /* Which 
61de0 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52 4f  entry in the FRO
61df0 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 75 38  M clause */.  u8
61e00 20 6f 70 2c 20 70 35 3b 20 20 20 20 20 20 20 20   op, p5;        
61e10 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 61 6e      /* Opcode an
61e20 64 20 50 35 20 6f 66 20 74 68 65 20 6f 70 63 6f  d P5 of the opco
61e30 64 65 20 74 68 61 74 20 65 6e 64 73 20 74 68 65  de that ends the
61e40 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 70   loop */.  int p
61e50 31 2c 20 70 32 3b 20 20 20 20 20 20 20 20 20 20  1, p2;          
61e60 20 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6f 66 20   /* Operands of 
61e70 74 68 65 20 6f 70 63 6f 64 65 20 75 73 65 64 20  the opcode used 
61e80 74 6f 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70  to ends the loop
61e90 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20   */.  union {   
61ea0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
61eb0 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20  nformation that 
61ec0 64 65 70 65 6e 64 73 20 6f 6e 20 70 6c 61 6e 2e  depends on plan.
61ed0 77 73 46 6c 61 67 73 20 2a 2f 0a 20 20 20 20 73  wsFlags */.    s
61ee0 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20 69 6e  truct {.      in
61ef0 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20  t nIn;          
61f00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
61f10 20 65 6e 74 72 69 65 73 20 69 6e 20 61 49 6e 4c   entries in aInL
61f20 6f 6f 70 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 73  oop[] */.      s
61f30 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20  truct InLoop {. 
61f40 20 20 20 20 20 20 20 69 6e 74 20 69 43 75 72 3b         int iCur;
61f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
61f60 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
61f70 20 75 73 65 64 20 62 79 20 74 68 69 73 20 49 4e   used by this IN
61f80 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20   operator */.   
61f90 20 20 20 20 20 69 6e 74 20 61 64 64 72 49 6e 54       int addrInT
61fa0 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  op;         /* T
61fb0 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f  op of the IN loo
61fc0 70 20 2a 2f 0a 20 20 20 20 20 20 7d 20 2a 61 49  p */.      } *aI
61fd0 6e 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20  nLoop;          
61fe0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
61ff0 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 65  about each neste
62000 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  d IN operator */
62010 0a 20 20 20 20 7d 20 69 6e 3b 20 20 20 20 20 20  .    } in;      
62020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
62030 65 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77 73 46  ed when plan.wsF
62040 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 5f 41 42  lags&WHERE_IN_AB
62050 4c 45 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20  LE */.  } u;..  
62060 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
62070 20 66 69 65 6c 64 20 69 73 20 72 65 61 6c 6c 79   field is really
62080 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65   not part of the
62090 20 63 75 72 72 65 6e 74 20 6c 65 76 65 6c 2e 20   current level. 
620a0 20 42 75 74 0a 20 20 2a 2a 20 77 65 20 6e 65 65   But.  ** we nee
620b0 64 20 61 20 70 6c 61 63 65 20 74 6f 20 63 61 63  d a place to cac
620c0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
620d0 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69   index informati
620e0 6f 6e 20 66 6f 72 20 65 61 63 68 0a 20 20 2a 2a  on for each.  **
620f0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
62100 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
62110 65 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 4c  e and the WhereL
62120 65 76 65 6c 20 73 74 72 75 63 74 75 72 65 20 69  evel structure i
62130 73 0a 20 20 2a 2a 20 61 20 63 6f 6e 76 65 6e 69  s.  ** a conveni
62140 65 6e 74 20 70 6c 61 63 65 20 73 69 6e 63 65 20  ent place since 
62150 74 68 65 72 65 20 69 73 20 6f 6e 65 20 57 68 65  there is one Whe
62160 72 65 4c 65 76 65 6c 20 66 6f 72 20 65 61 63 68  reLevel for each
62170 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a   FROM clause.  *
62180 2a 20 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a  * element..  */.
62190 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
621a0 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20  info *pIdxInfo; 
621b0 20 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f 20 66   /* Index info f
621c0 6f 72 20 6e 2d 74 68 20 73 6f 75 72 63 65 20 74  or n-th source t
621d0 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  able */.};../*.*
621e0 2a 20 46 6c 61 67 73 20 61 70 70 72 6f 70 72 69  * Flags appropri
621f0 61 74 65 20 66 6f 72 20 74 68 65 20 77 63 74 72  ate for the wctr
62200 6c 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72  lFlags parameter
62210 20 6f 66 20 73 71 6c 69 74 65 33 57 68 65 72 65   of sqlite3Where
62220 42 65 67 69 6e 28 29 2e 0a 2a 2f 0a 23 64 65 66  Begin()..*/.#def
62230 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42  ine WHERE_ORDERB
62240 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30  Y_NORMAL   0x000
62250 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64  0 /* No-op */.#d
62260 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
62270 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30  RBY_MIN      0x0
62280 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20  001 /* ORDER BY 
62290 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d  processing for m
622a0 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65  in() func */.#de
622b0 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52  fine WHERE_ORDER
622c0 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30  BY_MAX      0x00
622d0 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70  02 /* ORDER BY p
622e0 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61  rocessing for ma
622f0 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66  x() func */.#def
62300 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53  ine WHERE_ONEPAS
62310 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30  S_DESIRED  0x000
62320 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20  4 /* Want to do 
62330 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f  one-pass UPDATE/
62340 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e  DELETE */.#defin
62350 65 20 57 48 45 52 45 5f 46 49 4c 4c 5f 52 4f 57  e WHERE_FILL_ROW
62360 53 45 54 20 20 20 20 20 20 30 78 30 30 30 38 20  SET      0x0008 
62370 20 2f 2a 20 53 61 76 65 20 72 65 73 75 6c 74 73   /* Save results
62380 20 69 6e 20 61 20 52 6f 77 53 65 74 20 6f 62 6a   in a RowSet obj
62390 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ect */.#define W
623a0 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 20  HERE_OMIT_OPEN  
623b0 20 20 20 20 20 20 30 78 30 30 31 30 20 20 2f 2a        0x0010  /*
623c0 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 72   Table cursor ar
623d0 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a  e already open *
623e0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
623f0 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 20  OMIT_CLOSE      
62400 20 30 78 30 30 32 30 20 20 2f 2a 20 4f 6d 69 74   0x0020  /* Omit
62410 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20   close of table 
62420 26 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20  & index cursors 
62430 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48  */../*.** The WH
62440 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65  ERE clause proce
62450 73 73 69 6e 67 20 72 6f 75 74 69 6e 65 20 68 61  ssing routine ha
62460 73 20 74 77 6f 20 68 61 6c 76 65 73 2e 20 20 54  s two halves.  T
62470 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 74  he.** first part
62480 20 64 6f 65 73 20 74 68 65 20 73 74 61 72 74 20   does the start 
62490 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f  of the WHERE loo
624a0 70 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64  p and the second
624b0 0a 2a 2a 20 68 61 6c 66 20 64 6f 65 73 20 74 68  .** half does th
624c0 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 57 48  e tail of the WH
624d0 45 52 45 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e  ERE loop.  An in
624e0 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69  stance of.** thi
624f0 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72  s structure is r
62500 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 66  eturned by the f
62510 69 72 73 74 20 68 61 6c 66 20 61 6e 64 20 70 61  irst half and pa
62520 73 73 65 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  ssed.** into the
62530 20 73 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20   second half to 
62540 67 69 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e  give some contin
62550 75 69 74 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  uity..*/.struct 
62560 57 68 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61  WhereInfo {.  Pa
62570 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20  rse *pParse;    
62580 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
62590 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
625a0 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75  g context */.  u
625b0 31 36 20 77 63 74 72 6c 46 6c 61 67 73 3b 20 20  16 wctrlFlags;  
625c0 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69      /* Flags ori
625d0 67 69 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 74  ginally passed t
625e0 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  o sqlite3WhereBe
625f0 67 69 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b  gin() */.  u8 ok
62600 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20  OnePass;        
62610 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65  /* Ok to use one
62620 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20  -pass algorithm 
62630 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45  for UPDATE or DE
62640 4c 45 54 45 20 2a 2f 0a 20 20 69 6e 74 20 72 65  LETE */.  int re
62650 67 52 6f 77 53 65 74 3b 20 20 20 20 20 20 20 20  gRowSet;        
62660 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72           /* Stor
62670 65 20 72 6f 77 69 64 73 20 69 6e 20 74 68 69 73  e rowids in this
62680 20 72 6f 77 73 65 74 20 69 66 20 3e 3d 30 20 2a   rowset if >=0 *
62690 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
626a0 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  bList;          
626b0 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61     /* List of ta
626c0 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e  bles in the join
626d0 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b 20   */.  int iTop; 
626e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
626f0 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72 79       /* The very
62700 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
62710 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f 0a  e WHERE loop */.
62720 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b    int iContinue;
62730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62740 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
62750 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e   continue with n
62760 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  ext record */.  
62770 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20 20  int iBreak;     
62780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
62790 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62  * Jump here to b
627a0 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20  reak out of the 
627b0 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c  loop */.  int nL
627c0 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  evel;           
627d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
627e0 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f 6f  er of nested loo
627f0 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 57 68  p */.  struct Wh
62800 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20  ereClause *pWC; 
62810 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f        /* Decompo
62820 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 48  sition of the WH
62830 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
62840 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d 3b  WhereLevel a[1];
62850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
62860 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * Information ab
62870 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 6c 6f  out each nest lo
62880 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a 7d  op in WHERE */.}
62890 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65 43  ;../*.** A NameC
628a0 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 20 61  ontext defines a
628b0 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63   context in whic
628c0 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 61 62  h to resolve tab
628d0 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a  le and column.**
628e0 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f 6e   names.  The con
628f0 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f 66  text consists of
62900 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65   a list of table
62910 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 74 29  s (the pSrcList)
62920 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61 20   field and.** a 
62930 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78  list of named ex
62940 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73 74  pression (pEList
62950 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65 78  ).  The named ex
62960 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d 61  pression list ma
62970 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 54  y.** be NULL.  T
62980 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70 6f  he pSrc correspo
62990 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d 20  nds to the FROM 
629a0 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
629b0 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65 20  CT or.** to the 
629c0 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 65 72  table being oper
629d0 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 45 52  ated on by INSER
629e0 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45  T, UPDATE, or DE
629f0 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70 45  LETE.  The.** pE
62a00 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73  List corresponds
62a10 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73   to the result s
62a20 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61  et of a SELECT a
62a30 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a 2a  nd is NULL for.*
62a40 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e  * other statemen
62a50 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43 6f  ts..**.** NameCo
62a60 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 6e 65  ntexts can be ne
62a70 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 73 6f  sted.  When reso
62a80 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68 65  lving names, the
62a90 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a 20   inner-most .** 
62aa0 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 72 63  context is searc
62ab0 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 20 6e  hed first.  If n
62ac0 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  o match is found
62ad0 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 65 72  , the next outer
62ae0 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 63  .** context is c
62af0 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 65 72  hecked.  If ther
62b00 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d 61  e is still no ma
62b10 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 63 6f  tch, the next co
62b20 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 65 63  ntext.** is chec
62b30 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f 63 65  ked.  This proce
62b40 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e 74  ss continues unt
62b50 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 74 63  il either a matc
62b60 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f 72  h is found.** or
62b70 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61 72   all contexts ar
62b80 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e 20 61  e check.  When a
62b90 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c   match is found,
62ba0 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 65 72   the nRef member
62bb0 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65   of.** the conte
62bc0 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  xt containing th
62bd0 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 72 65  e match is incre
62be0 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 45  mented. .**.** E
62bf0 61 63 68 20 73 75 62 71 75 65 72 79 20 67 65 74  ach subquery get
62c00 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e 74  s a new NameCont
62c10 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 74 20  ext.  The pNext 
62c20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20  field points to 
62c30 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65  the.** NameConte
62c40 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  xt in the parent
62c50 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 74 68   query.  Thus th
62c60 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 63 61  e process of sca
62c70 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61 6d  nning the.** Nam
62c80 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63 6f  eContext list co
62c90 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65 61  rresponds to sea
62ca0 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20 73  rching through s
62cb0 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74 65  uccessively oute
62cc0 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20  r.** subqueries 
62cd0 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d 61  looking for a ma
62ce0 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4e  tch..*/.struct N
62cf0 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20 50  ameContext {.  P
62d00 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
62d10 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
62d20 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  r */.  SrcList *
62d30 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20 4f  pSrcList;   /* O
62d40 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c 65  ne or more table
62d50 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76  s used to resolv
62d60 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78 70  e names */.  Exp
62d70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20  rList *pEList;  
62d80 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c 69    /* Optional li
62d90 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 72  st of named expr
62da0 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  essions */.  int
62db0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
62dc0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
62dd0 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 62 79  ames resolved by
62de0 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 2a 2f   this context */
62df0 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20  .  int nErr;    
62e00 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
62e10 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f  r of errors enco
62e20 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 72 65  untered while re
62e30 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a 2f  solving names */
62e40 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b 20  .  u8 allowAgg; 
62e50 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65          /* Aggre
62e60 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  gate functions a
62e70 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a 20  llowed here */. 
62e80 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 20 20   u8 hasAgg;     
62e90 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
62ea0 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20   aggregates are 
62eb0 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73 43  seen */.  u8 isC
62ec0 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f  heck;          /
62ed0 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f 6c 76  * True if resolv
62ee0 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 20 43  ing names in a C
62ef0 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20  HECK constraint 
62f00 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 68 3b  */.  int nDepth;
62f10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70            /* Dep
62f20 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 20 72  th of subquery r
62f30 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72 20  ecursion. 1 for 
62f40 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a  no recursion */.
62f50 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49    AggInfo *pAggI
62f60 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  nfo;   /* Inform
62f70 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 67 72  ation about aggr
62f80 65 67 61 74 65 73 20 61 74 20 74 68 69 73 20 6c  egates at this l
62f90 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  evel */.  NameCo
62fa0 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 20 2f  ntext *pNext;  /
62fb0 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e 61 6d  * Next outer nam
62fc0 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c 4c  e context.  NULL
62fd0 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20 2a   for outermost *
62fe0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
62ff0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
63000 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
63010 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20  re contains all 
63020 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6e  information.** n
63030 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 61 74  eeded to generat
63040 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e  e code for a sin
63050 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  gle SELECT state
63060 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69 6d  ment..**.** nLim
63070 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20  it is set to -1 
63080 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4c  if there is no L
63090 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 6e 4f  IMIT clause.  nO
630a0 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f 20  ffset is set to 
630b0 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  0..** If there i
630c0 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  s a LIMIT clause
630d0 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 65 74  , the parser set
630e0 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65 20  s nLimit to the 
630f0 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20  value of the.** 
63100 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65  limit and nOffse
63110 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f  t to the value o
63120 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 6f 72  f the offset (or
63130 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 6e   0 if there is n
63140 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20 20  ot.** offset).  
63150 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e 4c  But later on, nL
63160 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 74  imit and nOffset
63170 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d 6f   become the memo
63180 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20  ry locations.** 
63190 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 61 74  in the VDBE that
631a0 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 6d 69   record the limi
631b0 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 6f 75  t and offset cou
631c0 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64 64  nters..**.** add
631d0 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74 72  rOpenEphm[] entr
631e0 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  ies contain the 
631f0 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70  address of OP_Op
63200 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f  enEphemeral opco
63210 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61 64  des..** These ad
63220 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 65 20  dresses must be 
63230 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 20 77  stored so that w
63240 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 61 6e  e can go back an
63250 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68 65  d fill in.** the
63260 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64 20   P4_KEYINFO and 
63270 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 6c 61  P2 parameters la
63280 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 74 68  ter.  Neither th
63290 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a 2a  e KeyInfo nor.**
632a0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
632b0 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 61 6e  olumns in P2 can
632c0 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74 20   be computed at 
632d0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a 2a  the same time.**
632e0 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45   as the OP_OpenE
632f0 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  phm instruction 
63300 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 73 65  is coded because
63310 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 69   not.** enough i
63320 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
63330 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75   the compound qu
63340 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20  ery is known at 
63350 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20 54  that point..** T
63360 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61  he KeyInfo for a
63370 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 61  ddrOpenTran[0] a
63380 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 20  nd [1] contains 
63390 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
633a0 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72  ces.** for the r
633b0 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65 20  esult set.  The 
633c0 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 72  KeyInfo for addr
633d0 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e 74  OpenTran[2] cont
633e0 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a  ains collating.*
633f0 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72 20  * sequences for 
63400 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
63410 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  use..*/.struct S
63420 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 4c 69  elect {.  ExprLi
63430 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20  st *pEList;     
63440 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 20 6f   /* The fields o
63450 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a  f the result */.
63460 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20    u8 op;        
63470 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
63480 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b 5f  of: TK_UNION TK_
63490 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43 54  ALL TK_INTERSECT
634a0 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20 20   TK_EXCEPT */.  
634b0 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20  char affinity;  
634c0 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52 65         /* MakeRe
634d0 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 20 61  cord with this a
634e0 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 54 5f  ffinity for SRT_
634f0 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 65 6c  Set */.  u16 sel
63500 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
63510 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a 20  /* Various SF_* 
63520 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63 4c  values */.  SrcL
63530 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20  ist *pSrc;      
63540 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63     /* The FROM c
63550 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20  lause */.  Expr 
63560 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20  *pWhere;        
63570 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
63580 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lause */.  ExprL
63590 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20  ist *pGroupBy;  
635a0 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42    /* The GROUP B
635b0 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  Y clause */.  Ex
635c0 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20  pr *pHaving;    
635d0 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49       /* The HAVI
635e0 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  NG clause */.  E
635f0 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
63600 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44  y;    /* The ORD
63610 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
63620 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72    Select *pPrior
63630 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 6f  ;        /* Prio
63640 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f  r select in a co
63650 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74  mpound select st
63660 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c  atement */.  Sel
63670 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  ect *pNext;     
63680 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c 65      /* Next sele
63690 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20 69  ct to the left i
636a0 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a  n a compound */.
636b0 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68 74    Select *pRight
636c0 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67 68  most;    /* Righ
636d0 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e  t-most select in
636e0 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
636f0 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ct statement */.
63700 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20    Expr *pLimit; 
63710 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49           /* LIMI
63720 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55  T expression. NU
63730 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65  LL means not use
63740 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f  d. */.  Expr *pO
63750 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f  ffset;         /
63760 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 73 73  * OFFSET express
63770 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20  ion. NULL means 
63780 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 69  not used. */.  i
63790 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73  nt iLimit, iOffs
637a0 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20  et;   /* Memory 
637b0 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e  registers holdin
637c0 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 45 54  g LIMIT & OFFSET
637d0 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
637e0 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  nt addrOpenEphm[
637f0 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e  3];   /* OP_Open
63800 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 72 65  Ephem opcodes re
63810 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 73 65  lated to this se
63820 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  lect */.};../*.*
63830 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
63840 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c 46   for Select.selF
63850 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 22 20  lags.  The "SF" 
63860 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 66 6f  prefix stands fo
63870 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c 61  r.** "Select Fla
63880 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  g"..*/.#define S
63890 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20 20  F_Distinct      
638a0 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 74    0x0001  /* Out
638b0 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44 49  put should be DI
638c0 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 6e  STINCT */.#defin
638d0 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 20  e SF_Resolved   
638e0 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20       0x0002  /* 
638f0 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 76 65  Identifiers have
63900 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 2a   been resolved *
63910 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67 67  /.#define SF_Agg
63920 72 65 67 61 74 65 20 20 20 20 20 20 20 30 78 30  regate       0x0
63930 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73  004  /* Contains
63940 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
63950 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ions */.#define 
63960 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c  SF_UsesEphemeral
63970 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55 73     0x0008  /* Us
63980 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d  es the OpenEphem
63990 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a 23  eral opcode */.#
639a0 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e 64  define SF_Expand
639b0 65 64 20 20 20 20 20 20 20 20 30 78 30 30 31 30  ed        0x0010
639c0 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65    /* sqlite3Sele
639d0 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c 65  ctExpand() calle
639e0 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 65  d on this */.#de
639f0 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65 49  fine SF_HasTypeI
63a00 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 20 20  nfo     0x0020  
63a10 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72 69  /* FROM subqueri
63a20 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d 65  es have Table me
63a30 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  tadata */.../*.*
63a40 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f 66  * The results of
63a50 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 62 65   a select can be
63a60 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20   distributed in 
63a70 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 20 54  several ways.  T
63a80 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 65 66  he.** "SRT" pref
63a90 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 43 54  ix means "SELECT
63aa0 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e 0a 2a   Result Type"..*
63ab0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 55 6e  /.#define SRT_Un
63ac0 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 2f 2a  ion        1  /*
63ad0 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73   Store result as
63ae0 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65   keys in an inde
63af0 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  x */.#define SRT
63b00 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 32 20  _Except       2 
63b10 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 75 6c   /* Remove resul
63b20 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20 69  t from a UNION i
63b30 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ndex */.#define 
63b40 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20 20  SRT_Exists      
63b50 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 20 69   3  /* Store 1 i
63b60 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  f the result is 
63b70 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65  not empty */.#de
63b80 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72 64  fine SRT_Discard
63b90 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f 20 6e        4  /* Do n
63ba0 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 73 75  ot save the resu
63bb0 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f 0a  lts anywhere */.
63bc0 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59  ./* The ORDER BY
63bd0 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f 72   clause is ignor
63be0 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68  ed for all of th
63bf0 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66 69  e above */.#defi
63c00 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65  ne IgnorableOrde
63c10 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 65 73  rby(X) ((X->eDes
63c20 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 64 29  t)<=SRT_Discard)
63c30 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f 75  ..#define SRT_Ou
63c40 74 70 75 74 20 20 20 20 20 20 20 35 20 20 2f 2a  tput       5  /*
63c50 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f 77   Output each row
63c60 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23 64   of result */.#d
63c70 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20 20  efine SRT_Mem   
63c80 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74 6f         6  /* Sto
63c90 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20 6d  re result in a m
63ca0 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23 64  emory cell */.#d
63cb0 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20 20  efine SRT_Set   
63cc0 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74 6f         7  /* Sto
63cd0 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b 65  re results as ke
63ce0 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a  ys in an index *
63cf0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54 61  /.#define SRT_Ta
63d00 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f 2a  ble        8  /*
63d10 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73   Store result as
63d20 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61 75   data with an au
63d30 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a 2f  tomatic rowid */
63d40 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70 68  .#define SRT_Eph
63d50 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a 20  emTab     9  /* 
63d60 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e 74  Create transient
63d70 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20 6c   tab and store l
63d80 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a 2f  ike SRT_Table */
63d90 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 6f 72  .#define SRT_Cor
63da0 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f 2a 20  outine   10  /* 
63db0 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67 6c  Generate a singl
63dc0 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20  e row of result 
63dd0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75  */../*.** A stru
63de0 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 63 75  cture used to cu
63df0 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 68 61  stomize the beha
63e00 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 53  vior of sqlite3S
63e10 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a 20  elect(). See.** 
63e20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 73  comments above s
63e30 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 66  qlite3Select() f
63e40 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 74  or details..*/.t
63e50 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65  ypedef struct Se
63e60 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 74 44  lectDest SelectD
63e70 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 6c 65  est;.struct Sele
63e80 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65 44  ctDest {.  u8 eD
63e90 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  est;         /* 
63ea0 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f  How to dispose o
63eb0 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a 2f  f the results */
63ec0 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 3b 20  .  u8 affinity; 
63ed0 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79       /* Affinity
63ee0 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 73 74   used when eDest
63ef0 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 69  ==SRT_Set */.  i
63f00 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 20 20  nt iParm;       
63f10 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 72 20   /* A parameter 
63f20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 65 73  used by the eDes
63f30 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f  t disposal metho
63f40 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 3b  d */.  int iMem;
63f50 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
63f60 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 20   register where 
63f70 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 69 74  results are writ
63f80 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65  ten */.  int nMe
63f90 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  m;         /* Nu
63fa0 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 72  mber of register
63fb0 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 7d  s allocated */.}
63fc0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20  ;../*.** An SQL 
63fd0 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20  parser context. 
63fe0 20 41 20 63 6f 70 79 20 6f 66 20 74 68 69 73 20   A copy of this 
63ff0 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73  structure is pas
64000 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  sed through.** t
64010 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 64 6f  he parser and do
64020 77 6e 20 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20  wn into all the 
64030 70 61 72 73 65 72 20 61 63 74 69 6f 6e 20 72 6f  parser action ro
64040 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74  utine in order t
64050 6f 0a 2a 2a 20 63 61 72 72 79 20 61 72 6f 75 6e  o.** carry aroun
64060 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  d information th
64070 61 74 20 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20  at is global to 
64080 74 68 65 20 65 6e 74 69 72 65 20 70 61 72 73 65  the entire parse
64090 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75  ..**.** The stru
640a0 63 74 75 72 65 20 69 73 20 64 69 76 69 64 65 64  cture is divided
640b0 20 69 6e 74 6f 20 74 77 6f 20 70 61 72 74 73 2e   into two parts.
640c0 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65    When the parse
640d0 72 20 61 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65  r and code.** ge
640e0 6e 65 72 61 74 65 20 63 61 6c 6c 20 74 68 65 6d  nerate call them
640f0 73 65 6c 76 65 73 20 72 65 63 75 72 73 69 76 65  selves recursive
64100 6c 79 2c 20 74 68 65 20 66 69 72 73 74 20 70 61  ly, the first pa
64110 72 74 20 6f 66 20 74 68 65 20 73 74 72 75 63 74  rt of the struct
64120 75 72 65 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61  ure.** is consta
64130 6e 74 20 62 75 74 20 74 68 65 20 73 65 63 6f 6e  nt but the secon
64140 64 20 70 61 72 74 20 69 73 20 72 65 73 65 74 20  d part is reset 
64150 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
64160 20 61 6e 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65   and end of.** e
64170 61 63 68 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a  ach recursion..*
64180 2a 0a 2a 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c  *.** The nTableL
64190 6f 63 6b 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f  ock and aTableLo
641a0 63 6b 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ck variables are
641b0 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68   only used if th
641c0 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 0a  e shared-cache .
641d0 2a 2a 20 66 65 61 74 75 72 65 20 69 73 20 65 6e  ** feature is en
641e0 61 62 6c 65 64 20 28 69 66 20 73 71 6c 69 74 65  abled (if sqlite
641f0 33 54 73 64 28 29 2d 3e 75 73 65 53 68 61 72 65  3Tsd()->useShare
64200 64 44 61 74 61 20 69 73 20 74 72 75 65 29 2e 20  dData is true). 
64210 54 68 65 79 20 61 72 65 0a 2a 2a 20 75 73 65 64  They are.** used
64220 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65   to store the se
64230 74 20 6f 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73  t of table-locks
64240 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
64250 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
64260 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75  .** compiled. Fu
64270 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 54 61  nction sqlite3Ta
64280 62 6c 65 4c 6f 63 6b 28 29 20 69 73 20 75 73 65  bleLock() is use
64290 64 20 74 6f 20 61 64 64 20 65 6e 74 72 69 65 73  d to add entries
642a0 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e   to the.** list.
642b0 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 72 73 65  .*/.struct Parse
642c0 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
642d0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
642e0 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73   main database s
642f0 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
64300 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
64310 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
64320 65 20 66 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e  e from execution
64330 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72   */.  char *zErr
64340 4d 73 67 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  Msg;       /* An
64350 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a   error message *
64360 2f 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b  /.  Vdbe *pVdbe;
64370 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65           /* An e
64380 6e 67 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74  ngine for execut
64390 69 6e 67 20 64 61 74 61 62 61 73 65 20 62 79 74  ing database byt
643a0 65 63 6f 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f  ecode */.  u8 co
643b0 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20 20 20 20  lNamesSet;      
643c0 2f 2a 20 54 52 55 45 20 61 66 74 65 72 20 4f 50  /* TRUE after OP
643d0 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20  _ColumnName has 
643e0 62 65 65 6e 20 69 73 73 75 65 64 20 74 6f 20 70  been issued to p
643f0 56 64 62 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d  Vdbe */.  u8 nam
64400 65 43 6c 61 73 68 3b 20 20 20 20 20 20 20 20 2f  eClash;        /
64410 2a 20 41 20 70 65 72 6d 61 6e 65 6e 74 20 74 61  * A permanent ta
64420 62 6c 65 20 6e 61 6d 65 20 63 6c 61 73 68 65 73  ble name clashes
64430 20 77 69 74 68 20 74 65 6d 70 20 74 61 62 6c 65   with temp table
64440 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68   name */.  u8 ch
64450 65 63 6b 53 63 68 65 6d 61 3b 20 20 20 20 20 20  eckSchema;      
64460 2f 2a 20 43 61 75 73 65 73 20 73 63 68 65 6d 61  /* Causes schema
64470 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20 61 66   cookie check af
64480 74 65 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ter an error */.
64490 20 20 75 38 20 6e 65 73 74 65 64 3b 20 20 20 20    u8 nested;    
644a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
644b0 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73   of nested calls
644c0 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 2f 63   to the parser/c
644d0 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f  ode generator */
644e0 0a 20 20 75 38 20 70 61 72 73 65 45 72 72 6f 72  .  u8 parseError
644f0 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
64500 61 66 74 65 72 20 61 20 70 61 72 73 69 6e 67 20  after a parsing 
64510 65 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23  error.  Ticket #
64520 31 37 39 34 20 2a 2f 0a 20 20 75 38 20 6e 54 65  1794 */.  u8 nTe
64530 6d 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f  mpReg;         /
64540 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 6d 70  * Number of temp
64550 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20  orary registers 
64560 69 6e 20 61 54 65 6d 70 52 65 67 5b 5d 20 2a 2f  in aTempReg[] */
64570 0a 20 20 75 38 20 6e 54 65 6d 70 49 6e 55 73 65  .  u8 nTempInUse
64580 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
64590 72 20 6f 66 20 61 54 65 6d 70 52 65 67 5b 5d 20  r of aTempReg[] 
645a0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65  currently checke
645b0 64 20 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61  d out */.  int a
645c0 54 65 6d 70 52 65 67 5b 38 5d 3b 20 20 20 20 20  TempReg[8];     
645d0 2f 2a 20 48 6f 6c 64 69 6e 67 20 61 72 65 61 20  /* Holding area 
645e0 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 72 65  for temporary re
645f0 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  gisters */.  int
64600 20 6e 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20   nRangeReg;     
64610 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
64620 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73   temporary regis
64630 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69  ter block */.  i
64640 6e 74 20 69 52 61 6e 67 65 52 65 67 3b 20 20 20  nt iRangeReg;   
64650 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67      /* First reg
64660 69 73 74 65 72 20 69 6e 20 74 65 6d 70 6f 72 61  ister in tempora
64670 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63  ry register bloc
64680 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b  k */.  int nErr;
64690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
646a0 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
646b0 73 65 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  seen */.  int nT
646c0 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ab;            /
646d0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 72 65 76  * Number of prev
646e0 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
646f0 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 2a 2f   VDBE cursors */
64700 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20  .  int nMem;    
64710 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
64720 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  r of memory cell
64730 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f  s used so far */
64740 0a 20 20 69 6e 74 20 6e 53 65 74 3b 20 20 20 20  .  int nSet;    
64750 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
64760 72 20 6f 66 20 73 65 74 73 20 75 73 65 64 20 73  r of sets used s
64770 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63  o far */.  int c
64780 6b 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20  kBase;          
64790 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72  /* Base register
647a0 20 6f 66 20 64 61 74 61 20 64 75 72 69 6e 67 20   of data during 
647b0 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74  check constraint
647c0 73 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 61 62  s */.  int disab
647d0 6c 65 43 6f 6c 43 61 63 68 65 3b 20 2f 2a 20 54  leColCache; /* T
647e0 72 75 65 20 74 6f 20 64 69 73 61 62 6c 65 20 61  rue to disable a
647f0 64 64 69 6e 67 20 74 6f 20 63 6f 6c 75 6d 6e 20  dding to column 
64800 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  cache */.  int n
64810 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20  ColCache;       
64820 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
64830 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 6c 75  ries in the colu
64840 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e  mn cache */.  in
64850 74 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 20  t iColCache;    
64860 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79     /* Next entry
64870 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 74 6f   of the cache to
64880 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 73 74   replace */.  st
64890 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 7b  ruct yColCache {
648a0 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65 3b  .    int iTable;
648b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
648c0 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ble cursor numbe
648d0 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f  r */.    int iCo
648e0 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  lumn;          /
648f0 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e  * Table column n
64900 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 63 68 61  umber */.    cha
64910 72 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20  r affChange;    
64920 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
64930 69 73 20 72 65 67 69 73 74 65 72 20 68 61 73 20  is register has 
64940 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 79 20  had an affinity 
64950 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 69 6e  change */.    in
64960 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20  t iReg;         
64970 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
64980 68 6f 6c 64 69 6e 67 20 76 61 6c 75 65 20 6f 66  holding value of
64990 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   this column */.
649a0 20 20 7d 20 61 43 6f 6c 43 61 63 68 65 5b 31 30    } aColCache[10
649b0 5d 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 66 6f  ];     /* One fo
649c0 72 20 65 61 63 68 20 76 61 6c 69 64 20 63 6f 6c  r each valid col
649d0 75 6d 6e 20 63 61 63 68 65 20 65 6e 74 72 79 20  umn cache entry 
649e0 2a 2f 0a 20 20 75 33 32 20 77 72 69 74 65 4d 61  */.  u32 writeMa
649f0 73 6b 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61  sk;       /* Sta
64a00 72 74 20 61 20 77 72 69 74 65 20 74 72 61 6e 73  rt a write trans
64a10 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20  action on these 
64a20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75  databases */.  u
64a30 33 32 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20  32 cookieMask;  
64a40 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f      /* Bitmask o
64a50 66 20 73 63 68 65 6d 61 20 76 65 72 69 66 69 65  f schema verifie
64a60 64 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20  d databases */. 
64a70 20 69 6e 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b   int cookieGoto;
64a80 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
64a90 20 6f 66 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63   of OP_Goto to c
64aa0 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 73  ookie verifier s
64ab0 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
64ac0 6e 74 20 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53  nt cookieValue[S
64ad0 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
64ae0 45 44 2b 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65  ED+2];  /* Value
64af0 73 20 6f 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20  s of cookies to 
64b00 76 65 72 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65  verify */.#ifnde
64b10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
64b20 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74  ARED_CACHE.  int
64b30 20 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20   nTableLock;    
64b40 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
64b50 20 6c 6f 63 6b 73 20 69 6e 20 61 54 61 62 6c 65   locks in aTable
64b60 4c 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c  Lock */.  TableL
64b70 6f 63 6b 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b  ock *aTableLock;
64b80 20 2f 2a 20 52 65 71 75 69 72 65 64 20 74 61 62   /* Required tab
64b90 6c 65 20 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61  le locks for sha
64ba0 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a  red-cache mode *
64bb0 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 72  /.#endif.  int r
64bc0 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20  egRowid;        
64bd0 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
64be0 69 6e 67 20 72 6f 77 69 64 20 6f 66 20 43 52 45  ing rowid of CRE
64bf0 41 54 45 20 54 41 42 4c 45 20 65 6e 74 72 79 20  ATE TABLE entry 
64c00 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 6f 74  */.  int regRoot
64c10 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67  ;         /* Reg
64c20 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f  ister holding ro
64c30 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  ot page number f
64c40 6f 72 20 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a  or new objects *
64c50 2f 0a 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69 73  /..  /* Above is
64c60 20 63 6f 6e 73 74 61 6e 74 20 62 65 74 77 65 65   constant betwee
64c70 6e 20 72 65 63 75 72 73 69 6f 6e 73 2e 20 20 42  n recursions.  B
64c80 65 6c 6f 77 20 69 73 20 72 65 73 65 74 20 62 65  elow is reset be
64c90 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 0a 20  fore and after. 
64ca0 20 2a 2a 20 65 61 63 68 20 72 65 63 75 72 73 69   ** each recursi
64cb0 6f 6e 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 56 61  on */..  int nVa
64cc0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
64cd0 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 76   Number of '?' v
64ce0 61 72 69 61 62 6c 65 73 20 73 65 65 6e 20 69 6e  ariables seen in
64cf0 20 74 68 65 20 53 51 4c 20 73 6f 20 66 61 72 20   the SQL so far 
64d00 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70  */.  int nVarExp
64d10 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  r;        /* Num
64d20 62 65 72 20 6f 66 20 75 73 65 64 20 73 6c 6f 74  ber of used slot
64d30 73 20 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d  s in apVarExpr[]
64d40 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78   */.  int nVarEx
64d50 70 72 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 4e 75  prAlloc;   /* Nu
64d60 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 65  mber of allocate
64d70 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72  d slots in apVar
64d80 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 70 72  Expr[] */.  Expr
64d90 20 2a 2a 61 70 56 61 72 45 78 70 72 3b 20 20 20   **apVarExpr;   
64da0 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20   /* Pointers to 
64db0 3a 61 61 61 20 61 6e 64 20 24 61 61 61 61 20 77  :aaa and $aaaa w
64dc0 69 6c 64 63 61 72 64 20 65 78 70 72 65 73 73 69  ildcard expressi
64dd0 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  ons */.  int nAl
64de0 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ias;          /*
64df0 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 73   Number of alias
64e00 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f  ed result set co
64e10 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  lumns */.  int n
64e20 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 20  AliasAlloc;     
64e30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c  /* Number of all
64e40 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 66 6f 72  ocated slots for
64e50 20 61 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 69   aAlias[] */.  i
64e60 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20  nt *aAlias;     
64e70 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
64e80 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 69  used to hold ali
64e90 61 73 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a 20  ased result */. 
64ea0 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20   u8 explain;    
64eb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
64ec0 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61   the EXPLAIN fla
64ed0 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68  g is found on th
64ee0 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b  e query */.  Tok
64ef0 65 6e 20 73 45 72 72 54 6f 6b 65 6e 3b 20 20 20  en sErrToken;   
64f00 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 61    /* The token a
64f10 74 20 77 68 69 63 68 20 74 68 65 20 65 72 72 6f  t which the erro
64f20 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 20 20  r occurred */.  
64f30 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e  Token sNameToken
64f40 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69  ;    /* Token wi
64f50 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20 73  th unqualified s
64f60 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d  chema object nam
64f70 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61  e */.  Token sLa
64f80 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54  stToken;    /* T
64f90 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 61  he last token pa
64fa0 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rsed */.  const 
64fb0 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 2f  char *zSql;    /
64fc0 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 2a  * All SQL text *
64fd0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
64fe0 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c 20  zTail;   /* All 
64ff0 53 51 4c 20 74 65 78 74 20 70 61 73 74 20 74 68  SQL text past th
65000 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c 6f 6e  e last semicolon
65010 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54 61 62   parsed */.  Tab
65020 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b 20 20  le *pNewTable;  
65030 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62 65 69    /* A table bei
65040 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  ng constructed b
65050 79 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 2a  y CREATE TABLE *
65060 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e 65  /.  Trigger *pNe
65070 77 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f 2a  wTrigger;     /*
65080 20 54 72 69 67 67 65 72 20 75 6e 64 65 72 20 63   Trigger under c
65090 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20 43 52  onstruct by a CR
650a0 45 41 54 45 20 54 52 49 47 47 45 52 20 2a 2f 0a  EATE TRIGGER */.
650b0 20 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a    TriggerStack *
650c0 74 72 69 67 53 74 61 63 6b 3b 20 20 2f 2a 20 54  trigStack;  /* T
650d0 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73 20 62  rigger actions b
650e0 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20  eing coded */.  
650f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 75 74  const char *zAut
65100 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20 54 68 65  hContext; /* The
65110 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   6th parameter t
65120 6f 20 64 62 2d 3e 78 41 75 74 68 20 63 61 6c 6c  o db->xAuth call
65130 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e 64 65 66  backs */.#ifndef
65140 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
65150 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 6f 6b 65  TUALTABLE.  Toke
65160 6e 20 73 41 72 67 3b 20 20 20 20 20 20 20 20 20  n sArg;         
65170 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65         /* Comple
65180 74 65 20 74 65 78 74 20 6f 66 20 61 20 6d 6f 64  te text of a mod
65190 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  ule argument */.
651a0 20 20 75 38 20 64 65 63 6c 61 72 65 56 74 61 62    u8 declareVtab
651b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
651c0 54 72 75 65 20 69 66 20 69 6e 73 69 64 65 20 73  True if inside s
651d0 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
651e0 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  tab() */.  int n
651f0 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20  VtabLock;       
65200 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
65210 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  of virtual table
65220 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 54  s to lock */.  T
65230 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 4c 6f 63  able **apVtabLoc
65240 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  k;        /* Poi
65250 6e 74 65 72 20 74 6f 20 76 69 72 74 75 61 6c 20  nter to virtual 
65260 74 61 62 6c 65 73 20 6e 65 65 64 69 6e 67 20 6c  tables needing l
65270 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66  ocking */.#endif
65280 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 3b 20  .  int nHeight; 
65290 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
652a0 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65  pression tree he
652b0 69 67 68 74 20 6f 66 20 63 75 72 72 65 6e 74 20  ight of current 
652c0 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f 0a 20 20  sub-select */.  
652d0 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69 65 54 61  Table *pZombieTa
652e0 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  b;      /* List 
652f0 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 73  of Table objects
65300 20 74 6f 20 64 65 6c 65 74 65 20 61 66 74 65 72   to delete after
65310 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a 7d 3b 0a   code gen */.};.
65320 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
65330 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
65340 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f 44 45  .  #define IN_DE
65350 43 4c 41 52 45 5f 56 54 41 42 20 30 0a 23 65 6c  CLARE_VTAB 0.#el
65360 73 65 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f  se.  #define IN_
65370 44 45 43 4c 41 52 45 5f 56 54 41 42 20 28 70 50  DECLARE_VTAB (pP
65380 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61  arse->declareVta
65390 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  b).#endif../*.**
653a0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
653b0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
653c0 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 64  ructure can be d
653d0 65 63 6c 61 72 65 64 20 6f 6e 20 61 20 73 74 61  eclared on a sta
653e0 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a 2a 20 74  ck and used.** t
653f0 6f 20 73 61 76 65 20 74 68 65 20 50 61 72 73 65  o save the Parse
65400 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 76 61  .zAuthContext va
65410 6c 75 65 20 73 6f 20 74 68 61 74 20 69 74 20 63  lue so that it c
65420 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 6c  an be restored l
65430 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ater..*/.struct 
65440 41 75 74 68 43 6f 6e 74 65 78 74 20 7b 0a 20 20  AuthContext {.  
65450 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 75 74  const char *zAut
65460 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f 2a 20 50  hContext;   /* P
65470 75 74 20 73 61 76 65 64 20 50 61 72 73 65 2e 7a  ut saved Parse.z
65480 41 75 74 68 43 6f 6e 74 65 78 74 20 68 65 72 65  AuthContext here
65490 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
654a0 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rse;            
654b0 20 20 2f 2a 20 54 68 65 20 50 61 72 73 65 20 73    /* The Parse s
654c0 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d 3b 0a 0a  tructure */.};..
654d0 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c 64 20 66  /*.** Bitfield f
654e0 6c 61 67 73 20 66 6f 72 20 50 32 20 76 61 6c 75  lags for P2 valu
654f0 65 20 69 6e 20 4f 50 5f 49 6e 73 65 72 74 20 61  e in OP_Insert a
65500 6e 64 20 4f 50 5f 44 65 6c 65 74 65 0a 2a 2f 0a  nd OP_Delete.*/.
65510 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4e  #define OPFLAG_N
65520 43 48 41 4e 47 45 20 20 20 31 20 20 20 20 2f 2a  CHANGE   1    /*
65530 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64   Set to update d
65540 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64  b->nChange */.#d
65550 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53  efine OPFLAG_LAS
65560 54 52 4f 57 49 44 20 32 20 20 20 20 2f 2a 20 53  TROWID 2    /* S
65570 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d  et to update db-
65580 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f 0a 23 64  >lastRowid */.#d
65590 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 49 53 55  efine OPFLAG_ISU
655a0 50 44 41 54 45 20 20 34 20 20 20 20 2f 2a 20 54  PDATE  4    /* T
655b0 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73  his OP_Insert is
655c0 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a   an sql UPDATE *
655d0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47  /.#define OPFLAG
655e0 5f 41 50 50 45 4e 44 20 20 20 20 38 20 20 20 20  _APPEND    8    
655f0 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65 6c  /* This is likel
65600 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
65610 64 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61 63 68  d */../*. * Each
65620 20 74 72 69 67 67 65 72 20 70 72 65 73 65 6e 74   trigger present
65630 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
65640 20 73 63 68 65 6d 61 20 69 73 20 73 74 6f 72 65   schema is store
65650 64 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  d as an instance
65660 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 20 54 72   of. * struct Tr
65670 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a 20 50 6f  igger. . *. * Po
65680 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 74 61 6e  inters to instan
65690 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 72  ces of struct Tr
656a0 69 67 67 65 72 20 61 72 65 20 73 74 6f 72 65 64  igger are stored
656b0 20 69 6e 20 74 77 6f 20 77 61 79 73 2e 0a 20 2a   in two ways.. *
656c0 20 31 2e 20 49 6e 20 74 68 65 20 22 74 72 69 67   1. In the "trig
656d0 48 61 73 68 22 20 68 61 73 68 20 74 61 62 6c 65  Hash" hash table
656e0 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 73 71   (part of the sq
656f0 6c 69 74 65 33 2a 20 74 68 61 74 20 72 65 70 72  lite3* that repr
65700 65 73 65 6e 74 73 20 74 68 65 20 0a 20 2a 20 20  esents the . *  
65710 20 20 64 61 74 61 62 61 73 65 29 2e 20 54 68 69    database). Thi
65720 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 67 65 72  s allows Trigger
65730 20 73 74 72 75 63 74 75 72 65 73 20 74 6f 20 62   structures to b
65740 65 20 72 65 74 72 69 65 76 65 64 20 62 79 20 6e  e retrieved by n
65750 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c 20 74  ame.. * 2. All t
65760 72 69 67 67 65 72 73 20 61 73 73 6f 63 69 61 74  riggers associat
65770 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65  ed with a single
65780 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 20 6c 69   table form a li
65790 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 69 6e 67  nked list, using
657a0 20 74 68 65 0a 20 2a 20 20 20 20 70 4e 65 78 74   the. *    pNext
657b0 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 72 75 63   member of struc
657c0 74 20 54 72 69 67 67 65 72 2e 20 41 20 70 6f 69  t Trigger. A poi
657d0 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73  nter to the firs
657e0 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  t element of the
657f0 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 20 6c 69  . *    linked li
65800 73 74 20 69 73 20 73 74 6f 72 65 64 20 61 73 20  st is stored as 
65810 74 68 65 20 22 70 54 72 69 67 67 65 72 22 20 6d  the "pTrigger" m
65820 65 6d 62 65 72 20 6f 66 20 74 68 65 20 61 73 73  ember of the ass
65830 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 20 73 74  ociated. *    st
65840 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 2a 0a 20  ruct Table.. *. 
65850 2a 20 54 68 65 20 22 73 74 65 70 5f 6c 69 73 74  * The "step_list
65860 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73 20  " member points 
65870 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c 65  to the first ele
65880 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e 6b 65 64  ment of a linked
65890 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 61 69 6e   list. * contain
658a0 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74 61 74  ing the SQL stat
658b0 65 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 64  ements specified
658c0 20 61 73 20 74 68 65 20 74 72 69 67 67 65 72 20   as the trigger 
658d0 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73 74 72  program.. */.str
658e0 75 63 74 20 54 72 69 67 67 65 72 20 7b 0a 20 20  uct Trigger {.  
658f0 63 68 61 72 20 2a 6e 61 6d 65 3b 20 20 20 20 20  char *name;     
65900 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
65910 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67 67  ame of the trigg
65920 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
65930 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 63            */.  c
65940 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 20 20 20  har *table;     
65950 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
65960 62 6c 65 20 6f 72 20 76 69 65 77 20 74 6f 20 77  ble or view to w
65970 68 69 63 68 20 74 68 65 20 74 72 69 67 67 65 72  hich the trigger
65980 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 20 75 38   applies */.  u8
65990 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   op;            
659a0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
659b0 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50  TK_DELETE, TK_UP
659c0 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20  DATE, TK_INSERT 
659d0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 75 38 20          */.  u8 
659e0 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 20 20 20  tr_tm;          
659f0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
65a00 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 54  RIGGER_BEFORE, T
65a10 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a  RIGGER_AFTER */.
65a20 20 20 45 78 70 72 20 2a 70 57 68 65 6e 3b 20 20    Expr *pWhen;  
65a30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
65a40 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20   WHEN clause of 
65a50 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 28  the expression (
65a60 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a  may be NULL) */.
65a70 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d    IdList *pColum
65a80 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49 66 20  ns;       /* If 
65a90 74 68 69 73 20 69 73 20 61 6e 20 55 50 44 41 54  this is an UPDAT
65aa0 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73  E OF <column-lis
65ab0 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20  t> trigger,.    
65ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65ad0 20 20 20 20 20 20 20 20 20 74 68 65 20 3c 63 6f           the <co
65ae0 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 73 74  lumn-list> is st
65af0 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 54  ored here */.  T
65b00 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e 3b 20  oken nameToken; 
65b10 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20         /* Token 
65b20 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 4e 61 6d 65  containing zName
65b30 2e 20 55 73 65 20 64 75 72 69 6e 67 20 70 61 72  . Use during par
65b40 73 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20 20 53  sing only */.  S
65b50 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20  chema *pSchema; 
65b60 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61         /* Schema
65b70 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
65b80 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 63 68  trigger */.  Sch
65b90 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d 61 3b  ema *pTabSchema;
65ba0 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63       /* Schema c
65bb0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
65bc0 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ble */.  Trigger
65bd0 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73 74 3b  Step *step_list;
65be0 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 6f 66   /* Link list of
65bf0 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
65c00 20 73 74 65 70 73 20 20 20 20 20 20 20 20 20 20   steps          
65c10 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20     */.  Trigger 
65c20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
65c30 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72 20  /* Next trigger 
65c40 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
65c50 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a  the table */.};.
65c60 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67 65 72  ./*.** A trigger
65c70 20 69 73 20 65 69 74 68 65 72 20 61 20 42 45 46   is either a BEF
65c80 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45 52 20  ORE or an AFTER 
65c90 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 66 6f  trigger.  The fo
65ca0 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74  llowing constant
65cb0 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 77  s.** determine w
65cc0 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  hich. .**.** If 
65cd0 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74 69 70  there are multip
65ce0 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79 6f 75  le triggers, you
65cf0 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65 20 42   might of some B
65d00 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65 20 41  EFORE and some A
65d10 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 61 74  FTER..** In that
65d20 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f 6e 73   cases, the cons
65d30 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61 6e 20  tants below can 
65d40 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68 65 72  be ORed together
65d50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 49  ..*/.#define TRI
65d60 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31 0a 23  GGER_BEFORE  1.#
65d70 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 5f 41  define TRIGGER_A
65d80 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 2a 20  FTER   2../*. * 
65d90 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73  An instance of s
65da0 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65  truct TriggerSte
65db0 70 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f  p is used to sto
65dc0 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20  re a single SQL 
65dd0 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74 68 61  statement. * tha
65de0 74 20 69 73 20 61 20 70 61 72 74 20 6f 66 20 61  t is a part of a
65df0 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d   trigger-program
65e00 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 6e 63  . . *. * Instanc
65e10 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 72 69  es of struct Tri
65e20 67 67 65 72 53 74 65 70 20 61 72 65 20 73 74 6f  ggerStep are sto
65e30 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 79 20  red in a singly 
65e40 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c 69 6e  linked list (lin
65e50 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74 68 65  ked. * using the
65e60 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 72 29   "pNext" member)
65e70 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 74   referenced by t
65e80 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 20 6d  he "step_list" m
65e90 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a 20 2a  ember of the . *
65ea0 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 72 75   associated stru
65eb0 63 74 20 54 72 69 67 67 65 72 20 69 6e 73 74 61  ct Trigger insta
65ec0 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74 20 65  nce. The first e
65ed0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6c 69  lement of the li
65ee0 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20 2a 20  nked list is. * 
65ef0 74 68 65 20 66 69 72 73 74 20 73 74 65 70 20 6f  the first step o
65f00 66 20 74 68 65 20 74 72 69 67 67 65 72 2d 70 72  f the trigger-pr
65f10 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 54 68  ogram.. * . * Th
65f20 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20 69 6e  e "op" member in
65f30 64 69 63 61 74 65 73 20 77 68 65 74 68 65 72 20  dicates whether 
65f40 74 68 69 73 20 69 73 20 61 20 22 44 45 4c 45 54  this is a "DELET
65f50 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20 22 55  E", "INSERT", "U
65f60 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22 53 45  PDATE" or. * "SE
65f70 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e 74 2e  LECT" statement.
65f80 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66   The meanings of
65f90 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d 62 65   the other membe
65fa0 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  rs is determined
65fb0 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61 6c 75   by the . * valu
65fc0 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66 6f 6c  e of "op" as fol
65fd0 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f 70 20  lows:. *. * (op 
65fe0 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a 20 2a  == TK_INSERT). *
65ff0 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20 73 74   orconf    -> st
66000 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f 4e 46  ores the ON CONF
66010 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 0a 20  LICT algorithm. 
66020 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e 20 49  * pSelect   -> I
66030 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53  f this is an INS
66040 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c  ERT INTO ... SEL
66050 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e  ECT ... statemen
66060 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20  t, then. *      
66070 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74 6f          this sto
66080 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  res a pointer to
66090 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
660a0 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69 73 65  ement. Otherwise
660b0 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 65 74   NULL.. * target
660c0 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68      -> A token h
660d0 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20  olding the name 
660e0 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
660f0 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20  insert into.. * 
66100 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20  pExprList -> If 
66110 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52  this is an INSER
66120 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45  T INTO ... VALUE
66130 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c  S ... statement,
66140 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20   then. *        
66150 20 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65        this store
66160 73 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69  s values to be i
66170 6e 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69  nserted. Otherwi
66180 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c  se NULL.. * pIdL
66190 69 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73  ist   -> If this
661a0 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e   is an INSERT IN
661b0 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d  TO ... (<column-
661c0 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e  names>) VALUES .
661d0 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20  .. . *          
661e0 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74      statement, t
661f0 68 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20  hen this stores 
66200 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73  the column-names
66210 20 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20   to be. *       
66220 20 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20         inserted 
66230 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20  into.. *. * (op 
66240 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a  == TK_DELETE). *
66250 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20   target    -> A 
66260 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68  token holding th
66270 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
66280 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72  ble to delete fr
66290 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20  om.. * pWhere   
662a0 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c   -> The WHERE cl
662b0 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45  ause of the DELE
662c0 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  TE statement if 
662d0 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
662e0 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  .. *            
662f0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
66300 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20  .. * . * (op == 
66310 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61  TK_UPDATE). * ta
66320 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b  rget    -> A tok
66330 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e  en holding the n
66340 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
66350 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77 73 20   to update rows 
66360 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20  of.. * pWhere   
66370 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c   -> The WHERE cl
66380 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41  ause of the UPDA
66390 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  TE statement if 
663a0 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
663b0 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  .. *            
663c0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
663d0 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d  .. * pExprList -
663e0 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68 65 20  > A list of the 
663f0 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64 61 74  columns to updat
66400 65 20 61 6e 64 20 74 68 65 20 65 78 70 72 65 73  e and the expres
66410 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74 65 0a  sions to update.
66420 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
66430 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c  them to. See sql
66440 69 74 65 33 55 70 64 61 74 65 28 29 20 64 6f 63  ite3Update() doc
66450 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70  umentation of "p
66460 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20 20 20  Changes". *     
66470 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e           argumen
66480 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63  t.. * . */.struc
66490 74 20 54 72 69 67 67 65 72 53 74 65 70 20 7b 0a  t TriggerStep {.
664a0 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20    int op;       
664b0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
664c0 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55   TK_DELETE, TK_U
664d0 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54  PDATE, TK_INSERT
664e0 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20  , TK_SELECT */. 
664f0 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20   int orconf;    
66500 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c        /* OE_Roll
66510 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54  back etc. */.  T
66520 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 20 20  rigger *pTrig;  
66530 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67      /* The trigg
66540 65 72 20 74 68 61 74 20 74 68 69 73 20 73 74 65  er that this ste
66550 70 20 69 73 20 61 20 70 61 72 74 20 6f 66 20 2a  p is a part of *
66560 2f 0a 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  /..  Select *pSe
66570 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c  lect;     /* Val
66580 69 64 20 66 6f 72 20 53 45 4c 45 43 54 20 61 6e  id for SELECT an
66590 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a 20 20 20  d sometimes .   
665a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
665b0 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 73 74         INSERT st
665c0 65 70 73 20 28 77 68 65 6e 20 70 45 78 70 72 4c  eps (when pExprL
665d0 69 73 74 20 3d 3d 20 30 29 20 2a 2f 0a 20 20 54  ist == 0) */.  T
665e0 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20 20 20  oken target;    
665f0 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72      /* Valid for
66600 20 44 45 4c 45 54 45 2c 20 55 50 44 41 54 45 2c   DELETE, UPDATE,
66610 20 49 4e 53 45 52 54 20 73 74 65 70 73 20 2a 2f   INSERT steps */
66620 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b  .  Expr *pWhere;
66630 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64          /* Valid
66640 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44   for DELETE, UPD
66650 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 45  ATE steps */.  E
66660 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c 69  xprList *pExprLi
66670 73 74 3b 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72  st; /* Valid for
66680 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
66690 74 73 20 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73  ts and sometimes
666a0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
666b0 20 20 20 20 20 20 20 20 20 20 20 20 20 49 4e 53               INS
666c0 45 52 54 20 73 74 65 70 73 20 28 77 68 65 6e 20  ERT steps (when 
666d0 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 20 20 20  pSelect == 0)   
666e0 20 20 20 20 20 20 2a 2f 0a 20 20 49 64 4c 69 73        */.  IdLis
666f0 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 20  t *pIdList;     
66700 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 49 4e 53  /* Valid for INS
66710 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  ERT statements o
66720 6e 6c 79 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  nly */.  Trigger
66730 53 74 65 70 20 2a 70 4e 65 78 74 3b 20 20 2f 2a  Step *pNext;  /*
66740 20 4e 65 78 74 20 69 6e 20 74 68 65 20 6c 69 6e   Next in the lin
66750 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 54 72 69 67  k-list */.  Trig
66760 67 65 72 53 74 65 70 20 2a 70 4c 61 73 74 3b 20  gerStep *pLast; 
66770 20 2f 2a 20 4c 61 73 74 20 65 6c 65 6d 65 6e 74   /* Last element
66780 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e 20 56   in link-list. V
66790 61 6c 69 64 20 66 6f 72 20 31 73 74 20 65 6c 65  alid for 1st ele
667a0 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  m only */.};../*
667b0 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  . * An instance 
667c0 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  of struct Trigge
667d0 72 53 74 61 63 6b 20 73 74 6f 72 65 73 20 69 6e  rStack stores in
667e0 66 6f 72 6d 61 74 69 6f 6e 20 72 65 71 75 69 72  formation requir
667f0 65 64 20 64 75 72 69 6e 67 20 63 6f 64 65 0a 20  ed during code. 
66800 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20  * generation of 
66810 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72  a single trigger
66820 20 70 72 6f 67 72 61 6d 2e 20 57 68 69 6c 65 20   program. While 
66830 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  the trigger prog
66840 72 61 6d 20 69 73 20 62 65 69 6e 67 0a 20 2a 20  ram is being. * 
66850 63 6f 64 65 64 2c 20 69 74 73 20 61 73 73 6f 63  coded, its assoc
66860 69 61 74 65 64 20 54 72 69 67 67 65 72 53 74 61  iated TriggerSta
66870 63 6b 20 69 6e 73 74 61 6e 63 65 20 69 73 20 70  ck instance is p
66880 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65  ointed to by the
66890 0a 20 2a 20 22 70 54 72 69 67 67 65 72 53 74 61  . * "pTriggerSta
668a0 63 6b 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  ck" member of th
668b0 65 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72  e Parse structur
668c0 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 70 54 61  e.. *. * The pTa
668d0 62 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 73 20  b member points 
668e0 74 6f 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  to the table tha
668f0 74 20 74 72 69 67 67 65 72 73 20 61 72 65 20 62  t triggers are b
66900 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e 2e 20 54  eing coded on. T
66910 68 65 20 0a 20 2a 20 6e 65 77 49 64 78 20 6d 65  he . * newIdx me
66920 6d 62 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 68  mber contains th
66930 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76  e index of the v
66940 64 62 65 20 63 75 72 73 6f 72 20 74 68 61 74 20  dbe cursor that 
66950 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 74 65  points at the te
66960 6d 70 0a 20 2a 20 74 61 62 6c 65 20 74 68 61 74  mp. * table that
66970 20 73 74 6f 72 65 73 20 74 68 65 20 6e 65 77 2e   stores the new.
66980 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20 49 66  * references. If
66990 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65   new.* reference
669a0 73 20 61 72 65 20 6e 6f 74 20 76 61 6c 69 64 0a  s are not valid.
669b0 20 2a 20 66 6f 72 20 74 68 65 20 74 72 69 67 67   * for the trigg
669c0 65 72 20 62 65 69 6e 67 20 63 6f 64 65 64 20 28  er being coded (
669d0 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 4f  for example an O
669e0 4e 20 44 45 4c 45 54 45 20 74 72 69 67 67 65 72  N DELETE trigger
669f0 29 2c 20 74 68 65 6e 20 6e 65 77 49 64 78 0a 20  ), then newIdx. 
66a00 2a 20 69 73 20 73 65 74 20 74 6f 20 2d 31 2e 20  * is set to -1. 
66a10 54 68 65 20 6f 6c 64 49 64 78 20 6d 65 6d 62 65  The oldIdx membe
66a20 72 20 69 73 20 61 6e 61 6c 6f 67 6f 75 73 20 74  r is analogous t
66a30 6f 20 6e 65 77 49 64 78 2c 20 66 6f 72 20 6f 6c  o newIdx, for ol
66a40 64 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0a  d.* references..
66a50 20 2a 0a 20 2a 20 54 68 65 20 4f 4e 20 43 4f 4e   *. * The ON CON
66a60 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 6f 20  FLICT policy to 
66a70 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  be used for the 
66a80 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20  trigger program 
66a90 73 74 65 70 73 20 69 73 20 73 74 6f 72 65 64 20  steps is stored 
66aa0 0a 20 2a 20 61 73 20 74 68 65 20 6f 72 63 6f 6e  . * as the orcon
66ab0 66 20 6d 65 6d 62 65 72 2e 20 49 66 20 74 68 69  f member. If thi
66ac0 73 20 69 73 20 4f 45 5f 44 65 66 61 75 6c 74 2c  s is OE_Default,
66ad0 20 74 68 65 6e 20 74 68 65 20 4f 4e 20 43 4f 4e   then the ON CON
66ae0 46 4c 49 43 54 20 63 6c 61 75 73 65 20 0a 20 2a  FLICT clause . *
66af0 20 73 70 65 63 69 66 69 65 64 20 66 6f 72 20 69   specified for i
66b00 6e 64 69 76 69 64 75 61 6c 20 74 72 69 67 67 65  ndividual trigge
66b10 72 73 20 73 74 65 70 73 20 69 73 20 75 73 65 64  rs steps is used
66b20 2e 0a 20 2a 0a 20 2a 20 73 74 72 75 63 74 20 54  .. *. * struct T
66b30 72 69 67 67 65 72 53 74 61 63 6b 20 68 61 73 20  riggerStack has 
66b40 61 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 72  a "pNext" member
66b50 2c 20 74 6f 20 61 6c 6c 6f 77 20 6c 69 6e 6b 65  , to allow linke
66b60 64 20 6c 69 73 74 73 20 74 6f 20 62 65 0a 20 2a  d lists to be. *
66b70 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 57 68   constructed. Wh
66b80 65 6e 20 63 6f 64 69 6e 67 20 6e 65 73 74 65 64  en coding nested
66b90 20 74 72 69 67 67 65 72 73 20 28 74 72 69 67 67   triggers (trigg
66ba0 65 72 73 20 66 69 72 65 64 20 62 79 20 6f 74 68  ers fired by oth
66bb0 65 72 20 74 72 69 67 67 65 72 73 29 0a 20 2a 20  er triggers). * 
66bc0 65 61 63 68 20 6e 65 73 74 65 64 20 74 72 69 67  each nested trig
66bd0 67 65 72 20 73 74 6f 72 65 73 20 69 74 73 20 70  ger stores its p
66be0 61 72 65 6e 74 20 74 72 69 67 67 65 72 27 73 20  arent trigger's 
66bf0 54 72 69 67 67 65 72 53 74 61 63 6b 20 61 73 20  TriggerStack as 
66c00 74 68 65 20 22 70 4e 65 78 74 22 20 0a 20 2a 20  the "pNext" . * 
66c10 70 6f 69 6e 74 65 72 2e 20 4f 6e 63 65 20 74 68  pointer. Once th
66c20 65 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72  e nested trigger
66c30 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64 2c   has been coded,
66c40 20 74 68 65 20 70 4e 65 78 74 20 76 61 6c 75 65   the pNext value
66c50 20 69 73 20 72 65 73 74 6f 72 65 64 0a 20 2a 20   is restored. * 
66c60 74 6f 20 74 68 65 20 70 54 72 69 67 67 65 72 53  to the pTriggerS
66c70 74 61 63 6b 20 6d 65 6d 62 65 72 20 6f 66 20 74  tack member of t
66c80 68 65 20 50 61 72 73 65 20 73 74 75 63 74 75 72  he Parse stuctur
66c90 65 20 61 6e 64 20 63 6f 64 69 6e 67 20 6f 66 20  e and coding of 
66ca0 74 68 65 20 70 61 72 65 6e 74 0a 20 2a 20 74 72  the parent. * tr
66cb0 69 67 67 65 72 20 63 6f 6e 74 69 6e 75 65 73 2e  igger continues.
66cc0 0a 20 2a 0a 20 2a 20 42 65 66 6f 72 65 20 61 20  . *. * Before a 
66cd0 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 20 69  nested trigger i
66ce0 73 20 63 6f 64 65 64 2c 20 74 68 65 20 6c 69 6e  s coded, the lin
66cf0 6b 65 64 20 6c 69 73 74 20 70 6f 69 6e 74 65 64  ked list pointed
66d00 20 74 6f 20 62 79 20 74 68 65 20 0a 20 2a 20 70   to by the . * p
66d10 54 72 69 67 67 65 72 53 74 61 63 6b 20 69 73 20  TriggerStack is 
66d20 73 63 61 6e 6e 65 64 20 74 6f 20 65 6e 73 75 72  scanned to ensur
66d30 65 20 74 68 61 74 20 74 68 65 20 74 72 69 67 67  e that the trigg
66d40 65 72 20 69 73 20 6e 6f 74 20 61 62 6f 75 74 20  er is not about 
66d50 74 6f 20 62 65 20 63 6f 64 65 64 0a 20 2a 20 72  to be coded. * r
66d60 65 63 75 72 73 69 76 65 6c 79 2e 20 49 66 20 74  ecursively. If t
66d70 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73  his condition is
66d80 20 64 65 74 65 63 74 65 64 2c 20 74 68 65 20 6e   detected, the n
66d90 65 73 74 65 64 20 74 72 69 67 67 65 72 20 69 73  ested trigger is
66da0 20 6e 6f 74 20 63 6f 64 65 64 2e 0a 20 2a 2f 0a   not coded.. */.
66db0 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74  struct TriggerSt
66dc0 61 63 6b 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ack {.  Table *p
66dd0 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Tab;         /* 
66de0 54 61 62 6c 65 20 74 68 61 74 20 74 72 69 67 67  Table that trigg
66df0 65 72 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c  ers are currentl
66e00 79 20 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e  y being coded on
66e10 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78   */.  int newIdx
66e20 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
66e30 64 65 78 20 6f 66 20 76 64 62 65 20 63 75 72 73  dex of vdbe curs
66e40 6f 72 20 74 6f 20 22 6e 65 77 22 20 74 65 6d 70  or to "new" temp
66e50 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
66e60 6f 6c 64 49 64 78 3b 20 20 20 20 20 20 20 20 20  oldIdx;         
66e70 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76 64 62   /* Index of vdb
66e80 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6f 6c 64  e cursor to "old
66e90 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a  " temp table */.
66ea0 20 20 75 33 32 20 6e 65 77 43 6f 6c 4d 61 73 6b    u32 newColMask
66eb0 3b 0a 20 20 75 33 32 20 6f 6c 64 43 6f 6c 4d 61  ;.  u32 oldColMa
66ec0 73 6b 3b 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66  sk;.  int orconf
66ed0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  ;          /* Cu
66ee0 72 72 65 6e 74 20 6f 72 63 6f 6e 66 20 70 6f 6c  rrent orconf pol
66ef0 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e  icy */.  int ign
66f00 6f 72 65 4a 75 6d 70 3b 20 20 20 20 20 20 2f 2a  oreJump;      /*
66f10 20 77 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74   where to jump t
66f20 6f 20 66 6f 72 20 61 20 52 41 49 53 45 28 49 47  o for a RAISE(IG
66f30 4e 4f 52 45 29 20 2a 2f 0a 20 20 54 72 69 67 67  NORE) */.  Trigg
66f40 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20  er *pTrigger;   
66f50 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 63  /* The trigger c
66f60 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63  urrently being c
66f70 6f 64 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65  oded */.  Trigge
66f80 72 53 74 61 63 6b 20 2a 70 4e 65 78 74 3b 20 2f  rStack *pNext; /
66f90 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72 20 64  * Next trigger d
66fa0 6f 77 6e 20 6f 6e 20 74 68 65 20 74 72 69 67 67  own on the trigg
66fb0 65 72 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b 0a 0a  er stack */.};..
66fc0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
66fd0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f  ing structure co
66fe0 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69  ntains informati
66ff0 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 73  on used by the s
67000 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a 20 72  qliteFix....** r
67010 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65 79 20  outines as they 
67020 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74  walk the parse t
67030 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61 74 61  ree to make data
67040 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 0a  base references.
67050 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20 0a 2a  ** explicit.  .*
67060 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
67070 20 44 62 46 69 78 65 72 20 44 62 46 69 78 65 72   DbFixer DbFixer
67080 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78 65 72  ;.struct DbFixer
67090 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72   {.  Parse *pPar
670a0 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  se;      /* The 
670b0 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e  parsing context.
670c0 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73    Error messages
670d0 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
670e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
670f0 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73  Db;    /* Make s
67100 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 73 20  ure all objects 
67110 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  are contained in
67120 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
67130 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
67140 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 65 20  zType;  /* Type 
67150 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72  of the container
67160 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f   - used for erro
67170 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20  r messages */.  
67180 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61  const Token *pNa
67190 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  me; /* Name of t
671a0 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20 75  he container - u
671b0 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 65  sed for error me
671c0 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ssages */.};../*
671d0 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65 64 20  .** An objected 
671e0 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75 6c 61  used to accumula
671f0 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20 61  te the text of a
67200 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 77 65   string where we
67210 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73  .** do not neces
67220 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f 77 20  sarily know how 
67230 62 69 67 20 74 68 65 20 73 74 72 69 6e 67 20 77  big the string w
67240 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20 65 6e  ill be in the en
67250 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74 72  d..*/.struct Str
67260 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69 74 65  Accum {.  sqlite
67270 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f  3 *db;         /
67280 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 61 62  * Optional datab
67290 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64  ase for lookasid
672a0 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c 4c 20  e.  Can be NULL 
672b0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61 73 65  */.  char *zBase
672c0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62  ;         /* A b
672d0 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  ase allocation. 
672e0 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63   Not from malloc
672f0 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 65  . */.  char *zTe
67300 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  xt;         /* T
67310 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c 65 63  he string collec
67320 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  ted so far */.  
67330 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20 20 20  int  nChar;     
67340 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
67350 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 6f 20  f the string so 
67360 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 41  far */.  int  nA
67370 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a  lloc;         /*
67380 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65   Amount of space
67390 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 7a 54   allocated in zT
673a0 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20 6d 78  ext */.  int  mx
673b0 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a  Alloc;        /*
673c0 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   Maximum allowed
673d0 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20 2a   string length *
673e0 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f 63 46  /.  u8   mallocF
673f0 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65 63 6f  ailed;   /* Beco
67400 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e 79 20  mes true if any 
67410 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
67420 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75 38 20  n fails */.  u8 
67430 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 20 20    useMalloc;    
67440 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 54 65    /* True if zTe
67450 78 74 20 69 73 20 65 6e 6c 61 72 67 65 61 62 6c  xt is enlargeabl
67460 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 20  e using realloc 
67470 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42 69 67  */.  u8   tooBig
67480 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 63  ;         /* Bec
67490 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73 74 72  omes true if str
674a0 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65 64 73  ing size exceeds
674b0 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f   limits */.};../
674c0 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74  *.** A pointer t
674d0 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
674e0 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 6d   is used to comm
674f0 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d 61 74  unicate informat
67500 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69  ion.** from sqli
67510 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f 50  te3Init and OP_P
67520 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 6f 20  arseSchema into 
67530 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 74 43  the sqlite3InitC
67540 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70 65  allback..*/.type
67550 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 20 73  def struct {.  s
67560 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
67570 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
67580 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c  se being initial
67590 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44  ized */.  int iD
675a0 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
675b0 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 74 61   0 for main data
675c0 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 45 4d  base.  1 for TEM
675d0 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 41 43  P, 2.. for ATTAC
675e0 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  Hed */.  char **
675f0 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a 20  pzErrMsg;    /* 
67600 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74  Error message st
67610 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 69  ored here */.  i
67620 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
67630 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
67640 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f  e stored here */
67650 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f 2a  .} InitData;../*
67660 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 63 6f  .** Structure co
67670 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c 20  ntaining global 
67680 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 64 61  configuration da
67690 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c 69 74  ta for the SQLit
676a0 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  e library..**.**
676b0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
676c0 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73 6f  also contains so
676d0 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61  me state informa
676e0 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  tion..*/.struct 
676f0 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 7b 0a  Sqlite3Config {.
67700 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74 3b 20    int bMemstat; 
67710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67720 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65      /* True to e
67730 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 74 61  nable memory sta
67740 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62 43 6f  tus */.  int bCo
67750 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20  reMutex;        
67760 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
67770 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63 6f 72  ue to enable cor
67780 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20  e mutexing */.  
67790 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78 3b 20  int bFullMutex; 
677a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
677b0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61    /* True to ena
677c0 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78 69 6e  ble full mutexin
677d0 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 74 72  g */.  int mxStr
677e0 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  len;            
677f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
67800 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e 67 74  mum string lengt
67810 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c 6f 6f  h */.  int szLoo
67820 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20  kaside;         
67830 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
67840 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ult lookaside bu
67850 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20 69  ffer size */.  i
67860 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b 20 20  nt nLookaside;  
67870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67880 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b   /* Default look
67890 61 73 69 64 65 20 62 75 66 66 65 72 20 63 6f 75  aside buffer cou
678a0 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  nt */.  sqlite3_
678b0 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 20 20  mem_methods m;  
678c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77            /* Low
678d0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
678e0 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72 66 61  location interfa
678f0 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ce */.  sqlite3_
67900 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 6d 75  mutex_methods mu
67910 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 77  tex;      /* Low
67920 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69 6e 74  -level mutex int
67930 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69  erface */.  sqli
67940 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
67950 64 73 20 70 63 61 63 68 65 3b 20 20 20 20 2f 2a  ds pcache;    /*
67960 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 67 65 2d   Low-level page-
67970 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65 20  cache interface 
67980 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 65 61 70  */.  void *pHeap
67990 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
679a0 20 20 20 20 20 20 20 2f 2a 20 48 65 61 70 20 73         /* Heap s
679b0 74 6f 72 61 67 65 20 73 70 61 63 65 20 2a 2f 0a  torage space */.
679c0 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 20 20 20    int nHeap;    
679d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
679e0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70      /* Size of p
679f0 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  Heap[] */.  int 
67a00 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b 20 20 20  mnReq, mxReq;   
67a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
67a20 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 68 65 61   Min and max hea
67a30 70 20 72 65 71 75 65 73 74 73 20 73 69 7a 65 73  p requests sizes
67a40 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 72   */.  void *pScr
67a50 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
67a60 20 20 20 20 20 20 20 20 2f 2a 20 53 63 72 61 74          /* Scrat
67a70 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69  ch memory */.  i
67a80 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20  nt szScratch;   
67a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67aa0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68   /* Size of each
67ab0 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20   scratch buffer 
67ac0 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 61 74 63  */.  int nScratc
67ad0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
67ae0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
67af0 20 6f 66 20 73 63 72 61 74 63 68 20 62 75 66 66   of scratch buff
67b00 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ers */.  void *p
67b10 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
67b20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
67b30 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  ge cache memory 
67b40 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b  */.  int szPage;
67b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67b60 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
67b70 66 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 70  f each page in p
67b80 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  Page[] */.  int 
67b90 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  nPage;          
67ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
67bb0 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
67bc0 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20   in pPage[] */. 
67bd0 20 69 6e 74 20 6d 78 50 61 72 73 65 72 53 74 61   int mxParserSta
67be0 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ck;             
67bf0 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d 20 64 65     /* maximum de
67c00 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65  pth of the parse
67c10 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  r stack */.  int
67c20 20 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62   sharedCacheEnab
67c30 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f  led;           /
67c40 2a 20 74 72 75 65 20 69 66 20 73 68 61 72 65 64  * true if shared
67c50 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 6e 61 62  -cache mode enab
67c60 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20  led */.  /* The 
67c70 61 62 6f 76 65 20 6d 69 67 68 74 20 62 65 20 69  above might be i
67c80 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 6e 6f  nitialized to no
67c90 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20 66 6f 6c  n-zero.  The fol
67ca0 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74 6f 20 61  lowing need to a
67cb0 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e 69 74 69  lways.  ** initi
67cc0 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c 20 68 6f  ally be zero, ho
67cd0 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20  wever. */.  int 
67ce0 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  isInit;         
67cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
67d00 20 54 72 75 65 20 61 66 74 65 72 20 69 6e 69 74   True after init
67d10 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 73 20 66  ialization has f
67d20 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 69 6e 74  inished */.  int
67d30 20 69 6e 50 72 6f 67 72 65 73 73 3b 20 20 20 20   inProgress;    
67d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
67d50 2a 20 54 72 75 65 20 77 68 69 6c 65 20 69 6e 69  * True while ini
67d60 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e 20 70  tialization in p
67d70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74  rogress */.  int
67d80 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20   isMallocInit;  
67d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
67da0 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c  * True after mal
67db0 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  loc is initializ
67dc0 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
67dd0 6d 75 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65  mutex *pInitMute
67de0 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74  x;        /* Mut
67df0 65 78 20 75 73 65 64 20 62 79 20 73 71 6c 69 74  ex used by sqlit
67e00 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
67e10 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69  */.  int nRefIni
67e20 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  tMutex;         
67e30 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
67e40 20 6f 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e   of users of pIn
67e50 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f  itMutex */.};../
67e60 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69  *.** Context poi
67e70 6e 74 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e  nter passed down
67e80 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 72 65   through the tre
67e90 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63  e-walk..*/.struc
67ea0 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74  t Walker {.  int
67eb0 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   (*xExprCallback
67ec0 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a  )(Walker*, Expr*
67ed0 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61  );     /* Callba
67ee0 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ck for expressio
67ef0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  ns */.  int (*xS
67f00 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57  electCallback)(W
67f10 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b  alker*,Select*);
67f20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f    /* Callback fo
67f30 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50  r SELECTs */.  P
67f40 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
67f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67f60 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
67f70 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a  er context.  */.
67f80 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20    union {       
67f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
67fb0 78 74 72 61 20 64 61 74 61 20 66 6f 72 20 63 61  xtra data for ca
67fc0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61  llback */.    Na
67fd0 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20  meContext *pNC; 
67fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67ff0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69           /* Nami
68000 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
68010 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
68020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68030 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
68040 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f  Integer value */
68050 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46  .  } u;.};../* F
68060 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
68070 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ons */.SQLITE_PR
68080 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
68090 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72  3WalkExpr(Walker
680a0 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
680b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
680c0 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
680d0 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c  t(Walker*, ExprL
680e0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
680f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
68100 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b  3WalkSelect(Walk
68110 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53  er*, Select*);.S
68120 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
68130 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  t sqlite3WalkSel
68140 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c  ectExpr(Walker*,
68150 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54   Select*);.SQLIT
68160 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
68170 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46  lite3WalkSelectF
68180 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c  rom(Walker*, Sel
68190 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65  ect*);../*.** Re
681a0 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74  turn code from t
681b0 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61  he parse-tree wa
681c0 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73  lking primitives
681d0 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61   and their.** ca
681e0 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66  llbacks..*/.#def
681f0 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  ine WRC_Continue
68200 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69      0   /* Conti
68210 6e 75 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68  nue down into ch
68220 69 6c 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e  ildren */.#defin
68230 65 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20  e WRC_Prune     
68240 20 20 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68    1   /* Omit ch
68250 69 6c 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69  ildren but conti
68260 6e 75 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c  nue walking sibl
68270 69 6e 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ings */.#define 
68280 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20  WRC_Abort       
68290 32 20 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74  2   /* Abandon t
682a0 68 65 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a  he tree walk */.
682b0 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20  ./*.** Assuming 
682c0 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  zIn points to th
682d0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
682e0 61 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  a UTF-8 characte
682f0 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49  r,.** advance zI
68300 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  n to point to th
68310 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
68320 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63  the next UTF-8 c
68330 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65  haracter..*/.#de
68340 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50  fine SQLITE_SKIP
68350 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20  _UTF8(zIn) {    
68360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68370 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a      \.  if( (*(z
68380 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20  In++))>=0xc0 ){ 
68390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
683a0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
683b0 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20     while( (*zIn 
683c0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b  & 0xc0)==0x80 ){
683d0 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20   zIn++; }       
683e0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
683f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
68420 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51  .}../*.** The SQ
68430 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
68440 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65  T macro can be e
68450 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74  ither a constant
68460 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e   (for production
68470 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61  .** builds) or a
68480 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28   function call (
68490 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20  for debugging). 
684a0 20 49 66 20 69 74 20 69 73 20 61 20 66 75 6e 63   If it is a func
684b0 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74  tion call,.** it
684c0 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72   allows the oper
684d0 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72  ator to set a br
684e0 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20  eakpoint at the 
684f0 73 70 6f 74 20 77 68 65 72 65 20 64 61 74 61 62  spot where datab
68500 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  ase.** corruptio
68510 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63  n is first detec
68520 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ted..*/.#ifdef S
68530 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
68540 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
68550 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28   sqlite3Corrupt(
68560 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20  void);.# define 
68570 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
68580 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75  KPT sqlite3Corru
68590 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  pt().#else.# def
685a0 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  ine SQLITE_CORRU
685b0 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43  PT_BKPT SQLITE_C
685c0 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f  ORRUPT.#endif../
685d0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
685e0 6e 67 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20  ng macros mimic 
685f0 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62  the standard lib
68600 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74  rary functions t
68610 6f 75 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73  oupper(),.** iss
68620 70 61 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28  pace(), isalnum(
68630 29 2c 20 69 73 64 69 67 69 74 28 29 20 61 6e 64  ), isdigit() and
68640 20 69 73 78 64 69 67 69 74 28 29 2c 20 72 65 73   isxdigit(), res
68650 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a  pectively. The.*
68660 2a 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e  * sqlite version
68670 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20  s only work for 
68680 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 73  ASCII characters
68690 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  , regardless of 
686a0 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65  locale..*/.#ifde
686b0 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23  f SQLITE_ASCII.#
686c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54   define sqlite3T
686d0 6f 75 70 70 65 72 28 78 29 20 20 28 28 78 29 26  oupper(x)  ((x)&
686e0 7e 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61  ~(sqlite3CtypeMa
686f0 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  p[(unsigned char
68700 29 28 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64  )(x)]&0x20)).# d
68710 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73  efine sqlite3Iss
68720 70 61 63 65 28 78 29 20 20 20 28 73 71 6c 69 74  pace(x)   (sqlit
68730 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69  e3CtypeMap[(unsi
68740 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30  gned char)(x)]&0
68750 78 30 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71  x01).# define sq
68760 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20  lite3Isalnum(x) 
68770 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d    (sqlite3CtypeM
68780 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61  ap[(unsigned cha
68790 72 29 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64  r)(x)]&0x06).# d
687a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61  efine sqlite3Isa
687b0 6c 70 68 61 28 78 29 20 20 20 28 73 71 6c 69 74  lpha(x)   (sqlit
687c0 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69  e3CtypeMap[(unsi
687d0 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30  gned char)(x)]&0
687e0 78 30 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71  x02).# define sq
687f0 6c 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20  lite3Isdigit(x) 
68800 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d    (sqlite3CtypeM
68810 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61  ap[(unsigned cha
68820 72 29 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64  r)(x)]&0x04).# d
68830 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78  efine sqlite3Isx
68840 64 69 67 69 74 28 78 29 20 20 28 73 71 6c 69 74  digit(x)  (sqlit
68850 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69  e3CtypeMap[(unsi
68860 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 30  gned char)(x)]&0
68870 78 30 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71  x08).# define sq
68880 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20  lite3Tolower(x) 
68890 20 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 54    (sqlite3UpperT
688a0 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64  oLower[(unsigned
688b0 20 63 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73   char)(x)]).#els
688c0 65 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79  e.# include <cty
688d0 70 65 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 73  pe.h>.# define s
688e0 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29  qlite3Toupper(x)
688f0 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69     toupper((unsi
68900 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23  gned char)(x)).#
68910 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
68920 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 70  sspace(x)   issp
68930 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ace((unsigned ch
68940 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65  ar)(x)).# define
68950 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28   sqlite3Isalnum(
68960 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e  x)   isalnum((un
68970 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29  signed char)(x))
68980 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
68990 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 73  3Isalpha(x)   is
689a0 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20  alpha((unsigned 
689b0 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69  char)(x)).# defi
689c0 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  ne sqlite3Isdigi
689d0 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 28  t(x)   isdigit((
689e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
689f0 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  )).# define sqli
68a00 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20  te3Isxdigit(x)  
68a10 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e  isxdigit((unsign
68a20 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
68a30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c  efine sqlite3Tol
68a40 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65  ower(x)   tolowe
68a50 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  r((unsigned char
68a60 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  )(x)).#endif../*
68a70 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e  .** Internal fun
68a80 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73  ction prototypes
68a90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
68aa0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
68ab0 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  rICmp(const char
68ac0 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
68ad0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
68ae0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72  E int sqlite3Str
68af0 4e 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  NICmp(const char
68b00 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
68b10 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
68b20 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
68b30 65 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74  e3IsNumber(const
68b40 20 63 68 61 72 2a 2c 20 69 6e 74 2a 2c 20 75 38   char*, int*, u8
68b50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
68b60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72  E int sqlite3Str
68b70 6c 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f  len(sqlite3*, co
68b80 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
68b90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
68ba0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63  qlite3Strlen30(c
68bb0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a 53 51  onst char*);..SQ
68bc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
68bd0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e   sqlite3MallocIn
68be0 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  it(void);.SQLITE
68bf0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
68c00 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76  lite3MallocEnd(v
68c10 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
68c20 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
68c30 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53  e3Malloc(int);.S
68c40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
68c50 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  id *sqlite3Mallo
68c60 63 5a 65 72 6f 28 69 6e 74 29 3b 0a 53 51 4c 49  cZero(int);.SQLI
68c70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
68c80 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  *sqlite3DbMalloc
68c90 5a 65 72 6f 28 73 71 6c 69 74 65 33 2a 2c 20 69  Zero(sqlite3*, i
68ca0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
68cb0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
68cc0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c  3DbMallocRaw(sql
68cd0 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ite3*, int);.SQL
68ce0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
68cf0 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75   *sqlite3DbStrDu
68d00 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  p(sqlite3*,const
68d10 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
68d20 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
68d30 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 73  lite3DbStrNDup(s
68d40 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
68d50 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  ar*, int);.SQLIT
68d60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
68d70 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76  sqlite3Realloc(v
68d80 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  oid*, int);.SQLI
68d90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
68da0 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f  *sqlite3DbReallo
68db0 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33 20  cOrFree(sqlite3 
68dc0 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b  *, void *, int);
68dd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
68de0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52  void *sqlite3DbR
68df0 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a  ealloc(sqlite3 *
68e00 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b 0a  , void *, int);.
68e10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
68e20 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 65  oid sqlite3DbFre
68e30 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64  e(sqlite3*, void
68e40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
68e50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61  TE int sqlite3Ma
68e60 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 2a 29 3b  llocSize(void*);
68e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
68e80 69 6e 74 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  int sqlite3DbMal
68e90 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74 65 33 2a  locSize(sqlite3*
68ea0 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  , void*);.SQLITE
68eb0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
68ec0 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c  qlite3ScratchMal
68ed0 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  loc(int);.SQLITE
68ee0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
68ef0 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65  lite3ScratchFree
68f00 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f  (void*);.SQLITE_
68f10 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
68f20 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
68f30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
68f40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
68f50 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 2a 29  3PageFree(void*)
68f60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
68f70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
68f80 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29  SetDefault(void)
68f90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
68fa0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 6e   void sqlite3Ben
68fb0 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 76  ignMallocHooks(v
68fc0 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 2c 20 76  oid (*)(void), v
68fd0 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 29 3b 0a  oid (*)(void));.
68fe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
68ff0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79  nt sqlite3Memory
69000 41 6c 61 72 6d 28 76 6f 69 64 20 28 2a 29 28 76  Alarm(void (*)(v
69010 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  oid*, sqlite3_in
69020 74 36 34 2c 20 69 6e 74 29 2c 20 76 6f 69 64 2a  t64, int), void*
69030 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  , sqlite3_int64)
69040 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
69050 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 0a  _ENABLE_MEMSYS3.
69060 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
69070 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d  onst sqlite3_mem
69080 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65  _methods *sqlite
69090 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76  3MemGetMemsys3(v
690a0 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  oid);.#endif.#if
690b0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
690c0 45 5f 4d 45 4d 53 59 53 35 0a 53 51 4c 49 54 45  E_MEMSYS5.SQLITE
690d0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
690e0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
690f0 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65  ds *sqlite3MemGe
69100 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 3b 0a  tMemsys5(void);.
69110 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66  #endif...#ifndef
69120 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d   SQLITE_MUTEX_OM
69130 49 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  IT.SQLITE_PRIVAT
69140 45 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E   sqlite3_mute
69150 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74  x_methods *sqlit
69160 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76  e3DefaultMutex(v
69170 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
69180 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 5f 6d  VATE   sqlite3_m
69190 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d 75 74  utex *sqlite3Mut
691a0 65 78 41 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51  exAlloc(int);.SQ
691b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
691c0 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49  nt sqlite3MutexI
691d0 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  nit(void);.SQLIT
691e0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
691f0 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28  sqlite3MutexEnd(
69200 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 53  void);.#endif..S
69210 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
69220 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56  t sqlite3StatusV
69230 61 6c 75 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54  alue(int);.SQLIT
69240 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
69250 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
69260 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
69270 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
69280 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28  qlite3StatusSet(
69290 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49  int, int);..SQLI
692a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
692b0 71 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62  qlite3IsNaN(doub
692c0 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  le);..SQLITE_PRI
692d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
692e0 33 56 58 50 72 69 6e 74 66 28 53 74 72 41 63 63  3VXPrintf(StrAcc
692f0 75 6d 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  um*, int, const 
69300 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b  char*, va_list);
69310 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
69320 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72  char *sqlite3MPr
69330 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c 63 6f  intf(sqlite3*,co
69340 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
69350 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
69360 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50  char *sqlite3VMP
69370 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c 63  rintf(sqlite3*,c
69380 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c  onst char*, va_l
69390 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ist);.SQLITE_PRI
693a0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
693b0 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c 69 74  e3MAppendf(sqlit
693c0 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20  e3*,char*,const 
693d0 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 69 66 20  char*,...);.#if 
693e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
693f0 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  EST) || defined(
69400 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 53 51  SQLITE_DEBUG).SQ
69410 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
69420 6f 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 67  oid sqlite3Debug
69430 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61  Printf(const cha
69440 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e 64 69 66  r*, ...);.#endif
69450 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
69460 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45  ITE_TEST).SQLITE
69470 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
69480 2a 73 71 6c 69 74 65 33 54 65 73 74 54 65 78 74  *sqlite3TestText
69490 54 6f 50 74 72 28 63 6f 6e 73 74 20 63 68 61 72  ToPtr(const char
694a0 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  *);.#endif.SQLIT
694b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
694c0 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
694d0 63 68 61 72 20 2a 2a 2c 20 73 71 6c 69 74 65 33  char **, sqlite3
694e0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
694f0 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
69500 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
69510 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 2a  3ErrorMsg(Parse*
69520 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e  , const char*, .
69530 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..);.SQLITE_PRIV
69540 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
69550 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65  ErrorClear(Parse
69560 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
69570 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
69580 65 71 75 6f 74 65 28 63 68 61 72 2a 29 3b 0a 53  equote(char*);.S
69590 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
695a0 69 64 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74  id sqlite3Dequot
695b0 65 45 78 70 72 28 45 78 70 72 2a 29 3b 0a 53 51  eExpr(Expr*);.SQ
695c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
695d0 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43   sqlite3KeywordC
695e0 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ode(const unsign
695f0 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a  ed char*, int);.
69600 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
69610 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72  nt sqlite3RunPar
69620 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73  ser(Parse*, cons
69630 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20 2a 2a  t char*, char **
69640 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
69650 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69  E void sqlite3Fi
69660 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 73 65  nishCoding(Parse
69670 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
69680 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
69690 74 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a 29  tTempReg(Parse*)
696a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
696b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c   void sqlite3Rel
696c0 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72 73  easeTempReg(Pars
696d0 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
696e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
696f0 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
69700 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Parse*,int);.SQL
69710 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
69720 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
69730 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c  empRange(Parse*,
69740 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
69750 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
69760 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69 74  qlite3Expr(sqlit
69770 65 33 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c  e3*, int, Expr*,
69780 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f   Expr*, const To
69790 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
697a0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
697b0 74 65 33 50 45 78 70 72 28 50 61 72 73 65 2a 2c  te3PExpr(Parse*,
697c0 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78 70   int, Expr*, Exp
697d0 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a  r*, const Token*
697e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
697f0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 52  E Expr *sqlite3R
69800 65 67 69 73 74 65 72 45 78 70 72 28 50 61 72 73  egisterExpr(Pars
69810 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  e*,Token*);.SQLI
69820 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
69830 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28  *sqlite3ExprAnd(
69840 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20  sqlite3*,Expr*, 
69850 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
69860 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
69870 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 70 72  te3ExprSpan(Expr
69880 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 29  *,Token*,Token*)
69890 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
698a0 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
698b0 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 73 65  prFunction(Parse
698c0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b  *,ExprList*, Tok
698d0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
698e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
698f0 33 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75  3ExprAssignVarNu
69900 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45 78 70  mber(Parse*, Exp
69910 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
69920 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
69930 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69 74 65  ExprClear(sqlite
69940 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  3*, Expr*);.SQLI
69950 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
69960 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
69970 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72  e(sqlite3*, Expr
69980 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
69990 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c  TE ExprList *sql
699a0 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
699b0 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69  nd(Parse*,ExprLi
699c0 73 74 2a 2c 45 78 70 72 2a 2c 54 6f 6b 65 6e 2a  st*,Expr*,Token*
699d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
699e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
699f0 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c  prListDelete(sql
69a00 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a  ite3*, ExprList*
69a10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
69a20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69  E int sqlite3Ini
69a30 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72  t(sqlite3*, char
69a40 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
69a50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
69a60 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64  nitCallback(void
69a70 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20  *, int, char**, 
69a80 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  char**);.SQLITE_
69a90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
69aa0 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65  ite3Pragma(Parse
69ab0 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c  *,Token*,Token*,
69ac0 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Token*,int);.SQL
69ad0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
69ae0 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
69af0 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69  ernalSchema(sqli
69b00 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  te3*, int);.SQLI
69b10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
69b20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73  sqlite3BeginPars
69b30 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53  e(Parse*,int);.S
69b40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
69b50 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74  id sqlite3Commit
69b60 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28  InternalChanges(
69b70 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
69b80 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
69b90 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65  *sqlite3ResultSe
69ba0 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 2a  tOfSelect(Parse*
69bb0 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54  ,Select*);.SQLIT
69bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
69bd0 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72  qlite3OpenMaster
69be0 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20 69  Table(Parse *, i
69bf0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
69c00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
69c10 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 73 65  StartTable(Parse
69c20 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c  *,Token*,Token*,
69c30 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29  int,int,int,int)
69c40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
69c50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
69c60 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f  Column(Parse*,To
69c70 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
69c80 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
69c90 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72  e3AddNotNull(Par
69ca0 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  se*, int);.SQLIT
69cb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
69cc0 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79  qlite3AddPrimary
69cd0 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72  Key(Parse*, Expr
69ce0 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  List*, int, int,
69cf0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
69d00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
69d10 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72  e3AddCheckConstr
69d20 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78 70  aint(Parse*, Exp
69d30 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
69d40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
69d50 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61  AddColumnType(Pa
69d60 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51  rse*,Token*);.SQ
69d70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
69d80 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61  d sqlite3AddDefa
69d90 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a 2c  ultValue(Parse*,
69da0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
69db0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
69dc0 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70  te3AddCollateTyp
69dd0 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
69de0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
69df0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e  E void sqlite3En
69e00 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f  dTable(Parse*,To
69e10 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65  ken*,Token*,Sele
69e20 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  ct*);..SQLITE_PR
69e30 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 71  IVATE Bitvec *sq
69e40 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
69e50 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50  e(u32);.SQLITE_P
69e60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
69e70 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69 74  e3BitvecTest(Bit
69e80 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49  vec*, u32);.SQLI
69e90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
69ea0 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
69eb0 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53  Bitvec*, u32);.S
69ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
69ed0 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  id sqlite3Bitvec
69ee0 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20 75  Clear(Bitvec*, u
69ef0 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  32);.SQLITE_PRIV
69f00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
69f10 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 42 69  BitvecDestroy(Bi
69f20 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tvec*);.SQLITE_P
69f30 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
69f40 65 33 42 69 74 76 65 63 53 69 7a 65 28 42 69 74  e3BitvecSize(Bit
69f50 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  vec*);.SQLITE_PR
69f60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
69f70 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65  3BitvecBuiltinTe
69f80 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53  st(int,int*);..S
69f90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 52 6f  QLITE_PRIVATE Ro
69fa0 77 53 65 74 20 2a 73 71 6c 69 74 65 33 52 6f 77  wSet *sqlite3Row
69fb0 53 65 74 49 6e 69 74 28 73 71 6c 69 74 65 33 2a  SetInit(sqlite3*
69fc0 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65  , void*, unsigne
69fd0 64 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  d int);.SQLITE_P
69fe0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
69ff0 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 52  te3RowSetClear(R
6a000 6f 77 53 65 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  owSet*);.SQLITE_
6a010 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6a020 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74  ite3RowSetInsert
6a030 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 29 3b 0a  (RowSet*, i64);.
6a040 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6a050 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  nt sqlite3RowSet
6a060 4e 65 78 74 28 52 6f 77 53 65 74 2a 2c 20 69 36  Next(RowSet*, i6
6a070 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  4*);..SQLITE_PRI
6a080 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6a090 33 43 72 65 61 74 65 56 69 65 77 28 50 61 72 73  3CreateView(Pars
6a0a0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
6a0b0 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c  ,Token*,Select*,
6a0c0 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 21  int,int);..#if !
6a0d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
6a0e0 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
6a0f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
6a100 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a  T_VIRTUALTABLE).
6a110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6a120 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77   int sqlite3View
6a130 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50  GetColumnNames(P
6a140 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23  arse*,Table*);.#
6a150 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
6a160 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
6a170 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0a 23  mnNames(A,B) 0.#
6a180 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
6a190 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6a1a0 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73  e3DropTable(Pars
6a1b0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
6a1c0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6a1d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6a1e0 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
6a1f0 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Table*);.SQLITE_
6a200 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6a210 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 65  ite3Insert(Parse
6a220 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
6a230 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c  rList*, Select*,
6a240 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a   IdList*, int);.
6a250 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6a260 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61  oid *sqlite3Arra
6a270 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65  yAllocate(sqlite
6a280 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  3*,void*,int,int
6a290 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29  ,int*,int*,int*)
6a2a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6a2b0 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
6a2c0 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  IdListAppend(sql
6a2d0 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20  ite3*, IdList*, 
6a2e0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6a2f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6a300 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49  te3IdListIndex(I
6a310 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61  dList*,const cha
6a320 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6a330 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
6a340 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
6a350 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63  ge(sqlite3*, Src
6a360 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  List*, int, int)
6a370 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6a380 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
6a390 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73  3SrcListAppend(s
6a3a0 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74  qlite3*, SrcList
6a3b0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
6a3c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6a3d0 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69  TE SrcList *sqli
6a3e0 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
6a3f0 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c  FromTerm(Parse*,
6a400 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e   SrcList*, Token
6a410 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20  *, Token*,.     
6a420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6a440 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a   Token*, Select*
6a450 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a  , Expr*, IdList*
6a460 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6a470 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
6a480 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50  cListIndexedBy(P
6a490 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20  arse *, SrcList 
6a4a0 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c  *, Token *);.SQL
6a4b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6a4c0 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79  sqlite3IndexedBy
6a4d0 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20  Lookup(Parse *, 
6a4e0 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
6a4f0 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tem *);.SQLITE_P
6a500 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6a510 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
6a520 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a  oinType(SrcList*
6a530 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6a540 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
6a550 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f  cListAssignCurso
6a560 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  rs(Parse*, SrcLi
6a570 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
6a580 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6a590 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71  3IdListDelete(sq
6a5a0 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29  lite3*, IdList*)
6a5b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6a5c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63   void sqlite3Src
6a5d0 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
6a5e0 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a  e3*, SrcList*);.
6a5f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6a600 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
6a610 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f  eIndex(Parse*,To
6a620 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c  ken*,Token*,SrcL
6a630 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  ist*,ExprList*,i
6a640 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20  nt,Token*,.     
6a650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6a660 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20     Token*, int, 
6a670 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6a680 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6a690 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65  3DropIndex(Parse
6a6a0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74  *, SrcList*, int
6a6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6a6c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c  E int sqlite3Sel
6a6d0 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65  ect(Parse*, Sele
6a6e0 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a  ct*, SelectDest*
6a6f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6a700 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
6a710 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65  3SelectNew(Parse
6a720 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c  *,ExprList*,SrcL
6a730 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c  ist*,Expr*,ExprL
6a740 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20  ist*,.          
6a750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
6a760 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  xpr*,ExprList*,i
6a770 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b  nt,Expr*,Expr*);
6a780 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6a790 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65  void sqlite3Sele
6a7a0 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  ctDelete(sqlite3
6a7b0 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
6a7c0 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
6a7d0 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  e *sqlite3SrcLis
6a7e0 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20  tLookup(Parse*, 
6a7f0 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
6a800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6a810 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28  lite3IsReadOnly(
6a820 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
6a830 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6a840 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6a850 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65  3OpenTable(Parse
6a860 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74  *, int iCur, int
6a870 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e   iDb, Table*, in
6a880 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  t);.#if defined(
6a890 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50  SQLITE_ENABLE_UP
6a8a0 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49  DATE_DELETE_LIMI
6a8b0 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  T) && !defined(S
6a8c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
6a8d0 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  ERY).SQLITE_PRIV
6a8e0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
6a8f0 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73  3LimitWhere(Pars
6a900 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20  e *, SrcList *, 
6a910 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74  Expr *, ExprList
6a920 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72   *, Expr *, Expr
6a930 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e   *, char *);.#en
6a940 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
6a950 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
6a960 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a  eleteFrom(Parse*
6a970 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72  , SrcList*, Expr
6a980 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6a990 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
6a9a0 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72  pdate(Parse*, Sr
6a9b0 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74  cList*, ExprList
6a9c0 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a  *, Expr*, int);.
6a9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57  SQLITE_PRIVATE W
6a9e0 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65  hereInfo *sqlite
6a9f0 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73  3WhereBegin(Pars
6aa00 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
6aa10 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c  pr*, ExprList**,
6aa20 20 75 38 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54   u8, int);.SQLIT
6aa30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6aa40 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57  qlite3WhereEnd(W
6aa50 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49  hereInfo*);.SQLI
6aa60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6aa70 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65  qlite3ExprCodeGe
6aa80 74 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20  tColumn(Parse*, 
6aa90 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Table*, int, int
6aaa0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
6aab0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6aac0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
6aad0 4d 6f 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  Move(Parse*, int
6aae0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
6aaf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6ab00 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
6ab10 43 6f 70 79 28 50 61 72 73 65 2a 2c 20 69 6e 74  Copy(Parse*, int
6ab20 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
6ab30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6ab40 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61   sqlite3ExprClea
6ab50 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72  rColumnCache(Par
6ab60 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  se*, int);.SQLIT
6ab70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6ab80 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
6ab90 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61  ffinityChange(Pa
6aba0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  rse*, int, int);
6abb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6abc0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
6abd0 57 72 69 74 61 62 6c 65 52 65 67 69 73 74 65 72  WritableRegister
6abe0 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Parse*,int);.SQ
6abf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6ac00 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72  d sqlite3ExprHar
6ac10 64 43 6f 70 79 28 50 61 72 73 65 2a 2c 69 6e 74  dCopy(Parse*,int
6ac20 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
6ac30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6ac40 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 2a  3ExprCode(Parse*
6ac50 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53  , Expr*, int);.S
6ac60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6ac70 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
6ac80 65 54 65 6d 70 28 50 61 72 73 65 2a 2c 20 45 78  eTemp(Parse*, Ex
6ac90 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  pr*, int*);.SQLI
6aca0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6acb0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
6acc0 72 67 65 74 28 50 61 72 73 65 2a 2c 20 45 78 70  rget(Parse*, Exp
6acd0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
6ace0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6acf0 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43  ite3ExprCodeAndC
6ad00 61 63 68 65 28 50 61 72 73 65 2a 2c 20 45 78 70  ache(Parse*, Exp
6ad10 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
6ad20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6ad30 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e  lite3ExprCodeCon
6ad40 73 74 61 6e 74 73 28 50 61 72 73 65 2a 2c 20 45  stants(Parse*, E
6ad50 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6ad60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6ad70 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
6ad80 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69  t(Parse*, ExprLi
6ad90 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  st*, int, int);.
6ada0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6adb0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
6adc0 66 54 72 75 65 28 50 61 72 73 65 2a 2c 20 45 78  fTrue(Parse*, Ex
6add0 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  pr*, int, int);.
6ade0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6adf0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
6ae00 66 46 61 6c 73 65 28 50 61 72 73 65 2a 2c 20 45  fFalse(Parse*, E
6ae10 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  xpr*, int, int);
6ae20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ae30 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 69  Table *sqlite3Fi
6ae40 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a  ndTable(sqlite3*
6ae50 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f  ,const char*, co
6ae60 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
6ae70 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65  TE_PRIVATE Table
6ae80 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54   *sqlite3LocateT
6ae90 61 62 6c 65 28 50 61 72 73 65 2a 2c 69 6e 74 20  able(Parse*,int 
6aea0 69 73 56 69 65 77 2c 63 6f 6e 73 74 20 63 68 61  isView,const cha
6aeb0 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29  r*, const char*)
6aec0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6aed0 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46   Index *sqlite3F
6aee0 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33  indIndex(sqlite3
6aef0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  *,const char*, c
6af00 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
6af10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6af20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
6af30 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71 6c  dDeleteTable(sql
6af40 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  ite3*,int,const 
6af50 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
6af60 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6af70 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
6af80 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33 2a  teIndex(sqlite3*
6af90 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
6afa0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6afb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
6afc0 63 75 75 6d 28 50 61 72 73 65 2a 29 3b 0a 53 51  cuum(Parse*);.SQ
6afd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6afe0 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75   sqlite3RunVacuu
6aff0 6d 28 63 68 61 72 2a 2a 2c 20 73 71 6c 69 74 65  m(char**, sqlite
6b000 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
6b010 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
6b020 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73  3NameFromToken(s
6b030 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29  qlite3*, Token*)
6b040 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b050 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
6b060 43 6f 6d 70 61 72 65 28 45 78 70 72 2a 2c 20 45  Compare(Expr*, E
6b070 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6b080 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6b090 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
6b0a0 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f 6e 74  regates(NameCont
6b0b0 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  ext*, Expr*);.SQ
6b0c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6b0d0 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61  d sqlite3ExprAna
6b0e0 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 6d 65  lyzeAggList(Name
6b0f0 43 6f 6e 74 65 78 74 2a 2c 45 78 70 72 4c 69 73  Context*,ExprLis
6b100 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6b110 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65  ATE Vdbe *sqlite
6b120 33 47 65 74 56 64 62 65 28 50 61 72 73 65 2a 29  3GetVdbe(Parse*)
6b130 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b140 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72   Expr *sqlite3Cr
6b150 65 61 74 65 49 64 45 78 70 72 28 50 61 72 73 65  eateIdExpr(Parse
6b160 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29   *, const char*)
6b170 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b180 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e   void sqlite3Prn
6b190 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29  gSaveState(void)
6b1a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b1b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e   void sqlite3Prn
6b1c0 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 76 6f  gRestoreState(vo
6b1d0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
6b1e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6b1f0 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 76  PrngResetState(v
6b200 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
6b210 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6b220 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c  3RollbackAll(sql
6b230 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
6b240 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6b250 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
6b260 65 6d 61 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  ema(Parse*, int)
6b270 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b280 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
6b290 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61  inTransaction(Pa
6b2a0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
6b2b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6b2c0 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61  sqlite3CommitTra
6b2d0 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29  nsaction(Parse*)
6b2e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b2f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c   void sqlite3Rol
6b300 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e  lbackTransaction
6b310 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
6b320 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6b330 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 50  lite3Savepoint(P
6b340 61 72 73 65 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65  arse*, int, Toke
6b350 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
6b360 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6b370 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28  CloseSavepoints(
6b380 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49  sqlite3 *);.SQLI
6b390 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6b3a0 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
6b3b0 74 61 6e 74 28 45 78 70 72 2a 29 3b 0a 53 51 4c  tant(Expr*);.SQL
6b3c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6b3d0 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
6b3e0 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70  stantNotJoin(Exp
6b3f0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6b400 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
6b410 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46  xprIsConstantOrF
6b420 75 6e 63 74 69 6f 6e 28 45 78 70 72 2a 29 3b 0a  unction(Expr*);.
6b430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6b440 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  nt sqlite3ExprIs
6b450 49 6e 74 65 67 65 72 28 45 78 70 72 2a 2c 20 69  Integer(Expr*, i
6b460 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
6b470 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6b480 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68  IsRowid(const ch
6b490 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
6b4a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6b4b0 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
6b4c0 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  te(Parse*, Table
6b4d0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
6b4e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6b4f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65  E void sqlite3Ge
6b500 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65  nerateRowIndexDe
6b510 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62  lete(Parse*, Tab
6b520 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b  le*, int, int*);
6b530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6b540 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65 72  int sqlite3Gener
6b550 61 74 65 49 6e 64 65 78 4b 65 79 28 50 61 72 73  ateIndexKey(Pars
6b560 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e 74 2c  e*, Index*, int,
6b570 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
6b580 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6b590 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43  sqlite3GenerateC
6b5a0 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28  onstraintChecks(
6b5b0 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c 69 6e  Parse*,Table*,in
6b5c0 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 20 20 20  t,int,.         
6b5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b5e0 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 2a              int*
6b5f0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
6b600 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6b610 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
6b620 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28  mpleteInsertion(
6b630 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
6b640 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20  int, int, int*, 
6b650 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  int, int, int);.
6b660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6b670 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  nt sqlite3OpenTa
6b680 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61  bleAndIndices(Pa
6b690 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e  rse*, Table*, in
6b6a0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6b6b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6b6c0 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
6b6d0 65 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20  eration(Parse*, 
6b6e0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
6b6f0 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
6b700 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 73  sqlite3ExprDup(s
6b710 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e  qlite3*,Expr*,in
6b720 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6b730 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54  TE void sqlite3T
6b740 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33  okenCopy(sqlite3
6b750 2a 2c 54 6f 6b 65 6e 2a 2c 63 6f 6e 73 74 20 54  *,Token*,const T
6b760 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6b770 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20  RIVATE ExprList 
6b780 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74  *sqlite3ExprList
6b790 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70  Dup(sqlite3*,Exp
6b7a0 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  rList*,int);.SQL
6b7b0 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c  ITE_PRIVATE SrcL
6b7c0 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ist *sqlite3SrcL
6b7d0 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  istDup(sqlite3*,
6b7e0 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53  SrcList*,int);.S
6b7f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64  QLITE_PRIVATE Id
6b800 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c  List *sqlite3IdL
6b810 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  istDup(sqlite3*,
6b820 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  IdList*);.SQLITE
6b830 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20  _PRIVATE Select 
6b840 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75  *sqlite3SelectDu
6b850 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63  p(sqlite3*,Selec
6b860 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
6b870 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6b880 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72  ite3FuncDefInser
6b890 74 28 46 75 6e 63 44 65 66 48 61 73 68 2a 2c 20  t(FuncDefHash*, 
6b8a0 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54  FuncDef*);.SQLIT
6b8b0 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65  E_PRIVATE FuncDe
6b8c0 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75  f *sqlite3FindFu
6b8d0 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c  nction(sqlite3*,
6b8e0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
6b8f0 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c  int,u8,int);.SQL
6b900 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6b910 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
6b920 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73  BuiltinFunctions
6b930 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
6b940 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6b950 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44  sqlite3RegisterD
6b960 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73  ateTimeFunctions
6b970 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50  (void);.SQLITE_P
6b980 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6b990 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61  te3RegisterGloba
6b9a0 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29  lFunctions(void)
6b9b0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
6b9c0 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
6b9d0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6b9e0 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74  e3SafetyOn(sqlit
6b9f0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
6ba00 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6ba10 65 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69  e3SafetyOff(sqli
6ba20 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64  te3*);.#else.# d
6ba30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66  efine sqlite3Saf
6ba40 65 74 79 4f 6e 28 41 29 20 30 0a 23 20 64 65 66  etyOn(A) 0.# def
6ba50 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74  ine sqlite3Safet
6ba60 79 4f 66 66 28 41 29 20 30 0a 23 65 6e 64 69 66  yOff(A) 0.#endif
6ba70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ba80 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
6ba90 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33  yCheckOk(sqlite3
6baa0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6bab0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61  TE int sqlite3Sa
6bac0 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f  fetyCheckSickOrO
6bad0 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  k(sqlite3*);.SQL
6bae0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6baf0 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
6bb00 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  okie(Parse*, int
6bb10 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  );..#if !defined
6bb20 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
6bb30 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  W) && !defined(S
6bb40 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
6bb50 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ER).SQLITE_PRIVA
6bb60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
6bb70 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 50  aterializeView(P
6bb80 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45  arse*, Table*, E
6bb90 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64  xpr*, int);.#end
6bba0 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
6bbb0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
6bbc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6bbd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
6bbe0 69 6e 54 72 69 67 67 65 72 28 50 61 72 73 65 2a  inTrigger(Parse*
6bbf0 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c  , Token*,Token*,
6bc00 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c  int,int,IdList*,
6bc10 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20  SrcList*,.      
6bc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bc30 20 20 20 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20       Expr*,int, 
6bc40 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6bc50 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
6bc60 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72  te3FinishTrigger
6bc70 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72  (Parse*, Trigger
6bc80 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  Step*, Token*);.
6bc90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6bca0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
6bcb0 70 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c  pTrigger(Parse*,
6bcc0 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b   SrcList*, int);
6bcd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6bce0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72    void sqlite3Dr
6bcf0 6f 70 54 72 69 67 67 65 72 50 74 72 28 50 61 72  opTriggerPtr(Par
6bd00 73 65 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a  se*, Trigger*);.
6bd10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6bd20 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65   Trigger *sqlite
6bd30 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 50  3TriggersExist(P
6bd40 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20  arse *, Table*, 
6bd50 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  int, ExprList*, 
6bd60 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c  int *pMask);.SQL
6bd70 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72  ITE_PRIVATE   Tr
6bd80 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72  igger *sqlite3Tr
6bd90 69 67 67 65 72 4c 69 73 74 28 50 61 72 73 65 20  iggerList(Parse 
6bda0 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c  *, Table *);.SQL
6bdb0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
6bdc0 74 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77  t sqlite3CodeRow
6bdd0 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20  Trigger(Parse*, 
6bde0 54 72 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20  Trigger *, int, 
6bdf0 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20  ExprList*, int, 
6be00 54 61 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20  Table *,.       
6be10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6be20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69       int, int, i
6be30 6e 74 2c 20 69 6e 74 2c 20 75 33 32 2a 2c 20 75  nt, int, u32*, u
6be40 33 32 2a 29 3b 0a 20 20 76 6f 69 64 20 73 71 6c  32*);.  void sql
6be50 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73 28  iteViewTriggers(
6be60 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
6be70 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72  Expr*, int, Expr
6be80 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
6be90 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
6bea0 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
6beb0 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c  erStep(sqlite3*,
6bec0 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a   TriggerStep*);.
6bed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6bee0 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
6bef0 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65  lite3TriggerSele
6bf00 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c  ctStep(sqlite3*,
6bf10 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45  Select*);.SQLITE
6bf20 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
6bf30 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
6bf40 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
6bf50 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a  (sqlite3*,Token*
6bf60 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20  , IdList*,.     
6bf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bf90 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c     ExprList*,Sel
6bfa0 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ect*,int);.SQLIT
6bfb0 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67  E_PRIVATE   Trig
6bfc0 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
6bfd0 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65  TriggerUpdateSte
6bfe0 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e  p(sqlite3*,Token
6bff0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70  *,ExprList*, Exp
6c000 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
6c010 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
6c020 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
6c030 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70  riggerDeleteStep
6c040 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a  (sqlite3*,Token*
6c050 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6c060 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
6c070 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
6c080 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54  gger(sqlite3*, T
6c090 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  rigger*);.SQLITE
6c0a0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
6c0b0 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
6c0c0 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71  DeleteTrigger(sq
6c0d0 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74  lite3*,int,const
6c0e0 20 63 68 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23   char*);.#else.#
6c0f0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54   define sqlite3T
6c100 72 69 67 67 65 72 73 45 78 69 73 74 28 42 2c 43  riggersExist(B,C
6c110 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69  ,D,E,F) 0.# defi
6c120 6e 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  ne sqlite3Delete
6c130 54 72 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64  Trigger(A,B).# d
6c140 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f  efine sqlite3Dro
6c150 70 54 72 69 67 67 65 72 50 74 72 28 41 2c 42 29  pTriggerPtr(A,B)
6c160 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6c170 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
6c180 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23  Trigger(A,B,C).#
6c190 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43   define sqlite3C
6c1a0 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c  odeRowTrigger(A,
6c1b0 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c  B,C,D,E,F,G,H,I,
6c1c0 4a 2c 4b 2c 4c 29 20 30 0a 23 65 6e 64 69 66 0a  J,K,L) 0.#endif.
6c1d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6c1e0 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54  int sqlite3JoinT
6c1f0 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  ype(Parse*, Toke
6c200 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65  n*, Token*, Toke
6c210 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
6c220 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6c230 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79  CreateForeignKey
6c240 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73  (Parse*, ExprLis
6c250 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72  t*, Token*, Expr
6c260 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  List*, int);.SQL
6c270 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6c280 20 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72   sqlite3DeferFor
6c290 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20  eignKey(Parse*, 
6c2a0 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  int);.#ifndef SQ
6c2b0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
6c2c0 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50  IZATION.SQLITE_P
6c2d0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
6c2e0 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 50 61  lite3AuthRead(Pa
6c2f0 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d  rse*,Expr*,Schem
6c300 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51  a*,SrcList*);.SQ
6c310 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
6c320 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  nt sqlite3AuthCh
6c330 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20  eck(Parse*,int, 
6c340 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e  const char*, con
6c350 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20  st char*, const 
6c360 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
6c370 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
6c380 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
6c390 50 75 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74  Push(Parse*, Aut
6c3a0 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  hContext*, const
6c3b0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
6c3c0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
6c3d0 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
6c3e0 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74  tPop(AuthContext
6c3f0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  *);.#else.# defi
6c400 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 52 65  ne sqlite3AuthRe
6c410 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 65  ad(a,b,c,d).# de
6c420 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68  fine sqlite3Auth
6c430 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29  Check(a,b,c,d,e)
6c440 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20      SQLITE_OK.# 
6c450 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75  define sqlite3Au
6c460 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 61 2c  thContextPush(a,
6c470 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 73 71  b,c).# define sq
6c480 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
6c490 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64 29 28  Pop(a)  ((void)(
6c4a0 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  a)).#endif.SQLIT
6c4b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6c4c0 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72  qlite3Attach(Par
6c4d0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72  se*, Expr*, Expr
6c4e0 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
6c4f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6c500 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72  qlite3Detach(Par
6c510 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  se*, Expr*);.SQL
6c520 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6c530 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74  sqlite3BtreeFact
6c540 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65  ory(const sqlite
6c550 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
6c560 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20  r *zFilename,.  
6c570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c580 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a 6f 75       int omitJou
6c590 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63 68 65  rnal, int nCache
6c5a0 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 42 74 72  , int flags, Btr
6c5b0 65 65 20 2a 2a 70 70 42 74 72 65 65 29 3b 0a 53  ee **ppBtree);.S
6c5c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6c5d0 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  t sqlite3FixInit
6c5e0 28 44 62 46 69 78 65 72 2a 2c 20 50 61 72 73 65  (DbFixer*, Parse
6c5f0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
6c600 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  ar*, const Token
6c610 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6c620 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
6c630 78 53 72 63 4c 69 73 74 28 44 62 46 69 78 65 72  xSrcList(DbFixer
6c640 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51  *, SrcList*);.SQ
6c650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6c660 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63   sqlite3FixSelec
6c670 74 28 44 62 46 69 78 65 72 2a 2c 20 53 65 6c 65  t(DbFixer*, Sele
6c680 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ct*);.SQLITE_PRI
6c690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6c6a0 46 69 78 45 78 70 72 28 44 62 46 69 78 65 72 2a  FixExpr(DbFixer*
6c6b0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6c6c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6c6d0 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28  ite3FixExprList(
6c6e0 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 4c 69  DbFixer*, ExprLi
6c6f0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
6c700 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6c710 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 44  FixTriggerStep(D
6c720 62 46 69 78 65 72 2a 2c 20 54 72 69 67 67 65 72  bFixer*, Trigger
6c730 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Step*);.SQLITE_P
6c740 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6c750 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61  e3AtoF(const cha
6c760 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a  r *z, double*);.
6c770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6c780 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74  nt sqlite3GetInt
6c790 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  32(const char *,
6c7a0 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   int*);.SQLITE_P
6c7b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6c7c0 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63  e3FitsIn64Bits(c
6c7d0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
6c7e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c7f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66  E int sqlite3Utf
6c800 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20  16ByteLen(const 
6c810 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74  void *pData, int
6c820 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f   nChar);.SQLITE_
6c830 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6c840 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63  te3Utf8CharLen(c
6c850 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
6c860 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51  , int nByte);.SQ
6c870 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6c880 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
6c890 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73  (const u8*, cons
6c8a0 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 2a  t u8*, const u8*
6c8b0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  *);../*.** Routi
6c8c0 6e 65 73 20 74 6f 20 72 65 61 64 20 61 6e 64 20  nes to read and 
6c8d0 77 72 69 74 65 20 76 61 72 69 61 62 6c 65 2d 6c  write variable-l
6c8e0 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20  ength integers. 
6c8f0 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 0a 2a   These used to.*
6c900 2a 20 62 65 20 64 65 66 69 6e 65 64 20 6c 6f 63  * be defined loc
6c910 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77 65  ally, but now we
6c920 20 75 73 65 20 74 68 65 20 76 61 72 69 6e 74 20   use the varint 
6c930 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20  routines in the 
6c940 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20  util.c.** file. 
6c950 20 43 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65   Code should use
6c960 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 73   the MACRO forms
6c970 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 56   below, as the V
6c980 61 72 69 6e 74 33 32 20 76 65 72 73 69 6f 6e 73  arint32 versions
6c990 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64 20 74 6f  .** are coded to
6c9a0 20 61 73 73 75 6d 65 20 74 68 65 20 73 69 6e 67   assume the sing
6c9b0 6c 65 20 62 79 74 65 20 63 61 73 65 20 69 73 20  le byte case is 
6c9c0 61 6c 72 65 61 64 79 20 68 61 6e 64 6c 65 64 20  already handled 
6c9d0 28 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 4d  (which .** the M
6c9e0 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29 2e  ACRO form does).
6c9f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
6ca00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75  TE int sqlite3Pu
6ca10 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64  tVarint(unsigned
6ca20 20 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53 51   char*, u64);.SQ
6ca30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ca40 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
6ca50 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61  t32(unsigned cha
6ca60 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45  r*, u32);.SQLITE
6ca70 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69  _PRIVATE u8 sqli
6ca80 74 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e  te3GetVarint(con
6ca90 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
6caa0 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 51 4c 49   *, u64 *);.SQLI
6cab0 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71  TE_PRIVATE u8 sq
6cac0 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
6cad0 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
6cae0 63 68 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a  char *, u32 *);.
6caf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6cb00 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74  nt sqlite3Varint
6cb10 4c 65 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a  Len(u64 v);../*.
6cb20 2a 2a 20 54 68 65 20 68 65 61 64 65 72 20 6f 66  ** The header of
6cb30 20 61 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73   a record consis
6cb40 74 73 20 6f 66 20 61 20 73 65 71 75 65 6e 63 65  ts of a sequence
6cb50 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
6cb60 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68   integers..** Th
6cb70 65 73 65 20 69 6e 74 65 67 65 72 73 20 61 72 65  ese integers are
6cb80 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 73   almost always s
6cb90 6d 61 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e 63  mall and are enc
6cba0 6f 64 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65  oded as a single
6cbb0 20 62 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 6f   byte..** The fo
6cbc0 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 74  llowing macros t
6cbd0 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 74 68  ake advantage th
6cbe0 69 73 20 66 61 63 74 20 74 6f 20 70 72 6f 76 69  is fact to provi
6cbf0 64 65 20 61 20 66 61 73 74 20 65 6e 63 6f 64 65  de a fast encode
6cc00 0a 2a 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 6f  .** and decode o
6cc10 66 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 69  f the integers i
6cc20 6e 20 61 20 72 65 63 6f 72 64 20 68 65 61 64 65  n a record heade
6cc30 72 2e 20 20 49 74 20 69 73 20 66 61 73 74 65 72  r.  It is faster
6cc40 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a   for the common.
6cc50 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 74 68  ** case where th
6cc60 65 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 73  e integer is a s
6cc70 69 6e 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20  ingle byte.  It 
6cc80 69 73 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77  is a little slow
6cc90 65 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69  er when the.** i
6cca0 6e 74 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72  nteger is two or
6ccb0 20 6d 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75   more bytes.  Bu
6ccc0 74 20 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20  t overall it is 
6ccd0 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  faster..**.** Th
6cce0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72  e following expr
6ccf0 65 73 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69  essions are equi
6cd00 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  valent:.**.**   
6cd10 20 20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74    x = sqlite3Get
6cd20 56 61 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20  Varint32( A, &B 
6cd30 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71  );.**     x = sq
6cd40 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32  lite3PutVarint32
6cd50 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20  ( A, B );.**.** 
6cd60 20 20 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e      x = getVarin
6cd70 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 20  t32( A, B );.** 
6cd80 20 20 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e      x = putVarin
6cd90 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a  t32( A, B );.**.
6cda0 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61  */.#define getVa
6cdb0 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38  rint32(A,B)  (u8
6cdc0 29 28 28 2a 28 41 29 3c 28 75 38 29 30 78 38 30  )((*(A)<(u8)0x80
6cdd0 29 20 3f 20 28 28 42 29 20 3d 20 28 75 33 32 29  ) ? ((B) = (u32)
6cde0 2a 28 41 29 29 2c 31 20 3a 20 73 71 6c 69 74 65  *(A)),1 : sqlite
6cdf0 33 47 65 74 56 61 72 69 6e 74 33 32 28 28 41 29  3GetVarint32((A)
6ce00 2c 20 28 75 33 32 20 2a 29 26 28 42 29 29 29 0a  , (u32 *)&(B))).
6ce10 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e  #define putVarin
6ce20 74 33 32 28 41 2c 42 29 20 20 28 75 38 29 28 28  t32(A,B)  (u8)((
6ce30 28 75 33 32 29 28 42 29 3c 28 75 33 32 29 30 78  (u32)(B)<(u32)0x
6ce40 38 30 29 20 3f 20 28 2a 28 41 29 20 3d 20 28 75  80) ? (*(A) = (u
6ce50 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 42 29  nsigned char)(B)
6ce60 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 50 75 74  ),1 : sqlite3Put
6ce70 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28 42  Varint32((A), (B
6ce80 29 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 56  ))).#define getV
6ce90 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33  arint    sqlite3
6cea0 47 65 74 56 61 72 69 6e 74 0a 23 64 65 66 69 6e  GetVarint.#defin
6ceb0 65 20 70 75 74 56 61 72 69 6e 74 20 20 20 20 73  e putVarint    s
6cec0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 0a  qlite3PutVarint.
6ced0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6cee0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 64   void sqlite3Ind
6cef0 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64  exAffinityStr(Vd
6cf00 62 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a  be *, Index *);.
6cf10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6cf20 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65  oid sqlite3Table
6cf30 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65  AffinityStr(Vdbe
6cf40 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51   *, Table *);.SQ
6cf50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
6cf60 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65  r sqlite3Compare
6cf70 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70  Affinity(Expr *p
6cf80 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29  Expr, char aff2)
6cf90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6cfa0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65   int sqlite3Inde
6cfb0 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72  xAffinityOk(Expr
6cfc0 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64   *pExpr, char id
6cfd0 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c  x_affinity);.SQL
6cfe0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
6cff0 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
6d000 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
6d010 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6d020 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f  E int sqlite3Ato
6d030 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  i64(const char*,
6d040 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   i64*);.SQLITE_P
6d050 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6d060 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33  te3Error(sqlite3
6d070 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
6d080 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  ar*,...);.SQLITE
6d090 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
6d0a0 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
6d0b0 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
6d0c0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b  char *z, int n);
6d0d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d0e0 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61  int sqlite3TwoPa
6d0f0 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20  rtName(Parse *, 
6d100 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a  Token *, Token *
6d110 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c  , Token **);.SQL
6d120 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
6d130 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45  t char *sqlite3E
6d140 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49  rrStr(int);.SQLI
6d150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6d160 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
6d170 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b  (Parse *pParse);
6d180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d190 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
6d1a0 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69  FindCollSeq(sqli
6d1b0 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e  te3*,u8 enc, con
6d1c0 73 74 20 63 68 61 72 20 2a 2c 69 6e 74 2c 69 6e  st char *,int,in
6d1d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6d1e0 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
6d1f0 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
6d200 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
6d210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
6d220 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 3b 0a 53  e, int nName);.S
6d230 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
6d240 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78  llSeq *sqlite3Ex
6d250 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  prCollSeq(Parse 
6d260 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
6d270 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Expr);.SQLITE_PR
6d280 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
6d290 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50  te3ExprSetColl(P
6d2a0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
6d2b0 70 72 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a  pr *, Token *);.
6d2c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6d2d0 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43  nt sqlite3CheckC
6d2e0 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20  ollSeq(Parse *, 
6d2f0 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49  CollSeq *);.SQLI
6d300 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6d310 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63  qlite3CheckObjec
6d320 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63  tName(Parse *, c
6d330 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
6d340 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6d350 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
6d360 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20  Changes(sqlite3 
6d370 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45  *, int);..SQLITE
6d380 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
6d390 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75  oid *sqlite3Valu
6d3a0 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61  eText(sqlite3_va
6d3b0 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54  lue*, u8);.SQLIT
6d3c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d3d0 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
6d3e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20  sqlite3_value*, 
6d3f0 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  u8);.SQLITE_PRIV
6d400 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6d410 56 61 6c 75 65 53 65 74 53 74 72 28 73 71 6c 69  ValueSetStr(sqli
6d420 74 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 2c  te3_value*, int,
6d430 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 38   const void *,u8
6d440 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
6d450 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 28             void(
6d460 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
6d470 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6d480 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
6d490 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
6d4a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6d4b0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
6d4c0 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
6d4d0 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49  sqlite3 *);.SQLI
6d4e0 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
6d4f0 2a 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38  *sqlite3Utf16to8
6d500 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73  (sqlite3 *, cons
6d510 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53  t void*, int);.S
6d520 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6d530 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  t sqlite3ValueFr
6d540 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a  omExpr(sqlite3 *
6d550 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75 38  , Expr *, u8, u8
6d560 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
6d570 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
6d580 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6d590 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69  ValueApplyAffini
6d5a0 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ty(sqlite3_value
6d5b0 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69 66   *, u8, u8);.#if
6d5c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c  ndef SQLITE_AMAL
6d5d0 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f  GAMATION.SQLITE_
6d5e0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e  PRIVATE const un
6d5f0 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69  signed char sqli
6d600 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
6d610 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ];.SQLITE_PRIVAT
6d620 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  E const unsigned
6d630 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79   char sqlite3Cty
6d640 70 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f  peMap[];.SQLITE_
6d650 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57  PRIVATE SQLITE_W
6d660 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65  SD struct Sqlite
6d670 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43  3Config sqlite3C
6d680 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f 50 52  onfig;.SQLITE_PR
6d690 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44  IVATE SQLITE_WSD
6d6a0 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c   FuncDefHash sql
6d6b0 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69  ite3GlobalFuncti
6d6c0 6f 6e 73 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ons;.SQLITE_PRIV
6d6d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
6d6e0 65 6e 64 69 6e 67 42 79 74 65 3b 0a 23 65 6e 64  endingByte;.#end
6d6f0 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
6d700 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f  E void sqlite3Ro
6d710 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62 2a 2c  otPageMoved(Db*,
6d720 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
6d730 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6d740 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50  sqlite3Reindex(P
6d750 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  arse*, Token*, T
6d760 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6d770 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6d780 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e  te3AlterFunction
6d790 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  s(sqlite3*);.SQL
6d7a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6d7b0 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e   sqlite3AlterRen
6d7c0 61 6d 65 54 61 62 6c 65 28 50 61 72 73 65 2a 2c  ameTable(Parse*,
6d7d0 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e   SrcList*, Token
6d7e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6d7f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
6d800 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73  tToken(const uns
6d810 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 69 6e  igned char *, in
6d820 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t *);.SQLITE_PRI
6d830 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6d840 33 4e 65 73 74 65 64 50 61 72 73 65 28 50 61 72  3NestedParse(Par
6d850 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  se*, const char*
6d860 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50  , ...);.SQLITE_P
6d870 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6d880 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
6d890 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69  dStatements(sqli
6d8a0 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
6d8b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6d8c0 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28  e3CodeSubselect(
6d8d0 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c  Parse *, Expr *,
6d8e0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
6d8f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6d900 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65  sqlite3SelectPre
6d910 70 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74  p(Parse*, Select
6d920 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29  *, NameContext*)
6d930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6d940 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f   int sqlite3Reso
6d950 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 4e 61 6d  lveExprNames(Nam
6d960 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 2a  eContext*, Expr*
6d970 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6d980 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
6d990 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73  solveSelectNames
6d9a0 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a  (Parse*, Select*
6d9b0 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29 3b  , NameContext*);
6d9c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d9d0 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  int sqlite3Resol
6d9e0 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 50  veOrderGroupBy(P
6d9f0 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20  arse*, Select*, 
6da00 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74  ExprList*, const
6da10 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
6da20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6da30 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c  ite3ColumnDefaul
6da40 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20  t(Vdbe *, Table 
6da50 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
6da60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6da70 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41  ite3AlterFinishA
6da80 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a  ddColumn(Parse *
6da90 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49  , Token *);.SQLI
6daa0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6dab0 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69  sqlite3AlterBegi
6dac0 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65  nAddColumn(Parse
6dad0 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 29 3b 0a   *, SrcList *);.
6dae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
6daf0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47  ollSeq *sqlite3G
6db00 65 74 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65  etCollSeq(sqlite
6db10 33 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63  3*, CollSeq *, c
6db20 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
6db30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6db40 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 41 66  E char sqlite3Af
6db50 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74  finityType(const
6db60 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
6db70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6db80 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 61 72  lite3Analyze(Par
6db90 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b  se*, Token*, Tok
6dba0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
6dbb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6dbc0 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65  InvokeBusyHandle
6dbd0 72 28 42 75 73 79 48 61 6e 64 6c 65 72 2a 29 3b  r(BusyHandler*);
6dbe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6dbf0 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44  int sqlite3FindD
6dc00 62 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65  b(sqlite3*, Toke
6dc10 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
6dc20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
6dc30 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69 74 65  indDbName(sqlite
6dc40 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  3 *, const char 
6dc50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6dc60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e  TE int sqlite3An
6dc70 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74  alysisLoad(sqlit
6dc80 65 33 2a 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51  e3*,int iDB);.SQ
6dc90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6dca0 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74  d sqlite3Default
6dcb0 52 6f 77 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a  RowEst(Index*);.
6dcc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6dcd0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
6dce0 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73  terLikeFunctions
6dcf0 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b  (sqlite3*, int);
6dd00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6dd10 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b  int sqlite3IsLik
6dd20 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  eFunction(sqlite
6dd30 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68  3*,Expr*,int*,ch
6dd40 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
6dd50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6dd60 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d  3MinimumFileForm
6dd70 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  at(Parse*, int, 
6dd80 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6dd90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6dda0 33 53 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64  3SchemaFree(void
6ddb0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
6ddc0 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69  ATE Schema *sqli
6ddd0 74 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c  te3SchemaGet(sql
6dde0 69 74 65 33 20 2a 2c 20 42 74 72 65 65 20 2a 29  ite3 *, Btree *)
6ddf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6de00 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65   int sqlite3Sche
6de10 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65  maToIndex(sqlite
6de20 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 29  3 *db, Schema *)
6de30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6de40 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65   KeyInfo *sqlite
6de50 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61  3IndexKeyinfo(Pa
6de60 72 73 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b  rse *, Index *);
6de70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6de80 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74  int sqlite3Creat
6de90 65 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c  eFunc(sqlite3 *,
6dea0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
6deb0 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c  nt, int, void *,
6dec0 20 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c   .  void (*)(sql
6ded0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
6dee0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
6def0 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28  **),.  void (*)(
6df00 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
6df10 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
6df20 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29  ue **), void (*)
6df30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
6df40 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
6df50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41  ATE int sqlite3A
6df60 70 69 45 78 69 74 28 73 71 6c 69 74 65 33 20 2a  piExit(sqlite3 *
6df70 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  db, int);.SQLITE
6df80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6df90 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61  ite3OpenTempData
6dfa0 62 61 73 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a  base(Parse *);..
6dfb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6dfc0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63  oid sqlite3StrAc
6dfd0 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d  cumInit(StrAccum
6dfe0 2a 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69  *, char*, int, i
6dff0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6e000 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6e010 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53  StrAccumAppend(S
6e020 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63  trAccum*,const c
6e030 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  har*,int);.SQLIT
6e040 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
6e050 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
6e060 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d 2a 29  inish(StrAccum*)
6e070 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
6e090 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63  AccumReset(StrAc
6e0a0 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  cum*);.SQLITE_PR
6e0b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6e0c0 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
6e0d0 28 53 65 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74  (SelectDest*,int
6e0e0 2c 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  ,int);..SQLITE_P
6e0f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6e100 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74  te3BackupRestart
6e110 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
6e120 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e130 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
6e140 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69  ackupUpdate(sqli
6e150 74 65 33 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67  te3_backup *, Pg
6e160 6e 6f 2c 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b  no, const u8 *);
6e170 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ../*.** The inte
6e180 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45 4d  rface to the LEM
6e190 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61 72  ON-generated par
6e1a0 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ser.*/.SQLITE_PR
6e1b0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6e1c0 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76  te3ParserAlloc(v
6e1d0 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29  oid*(*)(size_t))
6e1e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e1f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
6e200 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c 20 76  serFree(void*, v
6e210 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
6e220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6e230 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
6e240 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f  r(void*, int, To
6e250 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 23 69  ken, Parse*);.#i
6e260 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
6e270 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45  TACKDEPTH.SQLITE
6e280 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
6e290 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63  qlite3ParserStac
6e2a0 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 65  kPeak(void*);.#e
6e2b0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49  ndif..SQLITE_PRI
6e2c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6e2d0 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  AutoLoadExtensio
6e2e0 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 69  ns(sqlite3*);.#i
6e2f0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
6e300 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
6e310 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e320 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c    void sqlite3Cl
6e330 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71  oseExtensions(sq
6e340 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23  lite3*);.#else.#
6e350 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43   define sqlite3C
6e360 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 58  loseExtensions(X
6e370 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  ).#endif..#ifnde
6e380 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
6e390 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54  ARED_CACHE.SQLIT
6e3a0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
6e3b0 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
6e3c0 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c 20  k(Parse *, int, 
6e3d0 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20 63  int, u8, const c
6e3e0 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20  har *);.#else.  
6e3f0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54  #define sqlite3T
6e400 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79  ableLock(v,w,x,y
6e410 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  ,z).#endif..#ifd
6e420 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
6e430 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6e440 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 54  int sqlite3Utf8T
6e450 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  o8(unsigned char
6e460 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  *);.#endif..#ifd
6e470 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
6e480 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 64  IRTUALTABLE.#  d
6e490 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61  efine sqlite3Vta
6e4a0 62 43 6c 65 61 72 28 58 29 0a 23 20 20 64 65 66  bClear(X).#  def
6e4b0 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 53  ine sqlite3VtabS
6e4c0 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 54 45 5f  ync(X,Y) SQLITE_
6e4d0 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  OK.#  define sql
6e4e0 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b  ite3VtabRollback
6e4f0 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71  (X).#  define sq
6e500 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28  lite3VtabCommit(
6e510 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  X).#  define sql
6e520 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64  ite3VtabInSync(d
6e530 62 29 20 30 0a 23 65 6c 73 65 0a 53 51 4c 49 54  b) 0.#else.SQLIT
6e540 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69  E_PRIVATE    voi
6e550 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65  d sqlite3VtabCle
6e560 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49  ar(Table*);.SQLI
6e570 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e  TE_PRIVATE    in
6e580 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e  t sqlite3VtabSyn
6e590 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  c(sqlite3 *db, c
6e5a0 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  har **);.SQLITE_
6e5b0 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73  PRIVATE    int s
6e5c0 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
6e5d0 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  ck(sqlite3 *db);
6e5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e5f0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74     int sqlite3Vt
6e600 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33  abCommit(sqlite3
6e610 20 2a 64 62 29 3b 0a 23 20 20 64 65 66 69 6e 65   *db);.#  define
6e620 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79   sqlite3VtabInSy
6e630 6e 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e 56  nc(db) ((db)->nV
6e640 54 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29 2d  Trans>0 && (db)-
6e650 3e 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65 6e  >aVTrans==0).#en
6e660 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
6e670 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
6e680 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28  tabMakeWritable(
6e690 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a  Parse*,Table*);.
6e6a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6e6b0 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4c  oid sqlite3VtabL
6e6c0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ock(sqlite3_vtab
6e6d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e6e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
6e6f0 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  tabUnlock(sqlite
6e700 33 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62  3*, sqlite3_vtab
6e710 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e720 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
6e730 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 50 61  tabBeginParse(Pa
6e740 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  rse*, Token*, To
6e750 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  ken*, Token*);.S
6e760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6e770 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 69  id sqlite3VtabFi
6e780 6e 69 73 68 50 61 72 73 65 28 50 61 72 73 65 2a  nishParse(Parse*
6e790 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
6e7a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6e7b0 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69  qlite3VtabArgIni
6e7c0 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  t(Parse*);.SQLIT
6e7d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6e7e0 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78 74  qlite3VtabArgExt
6e7f0 65 6e 64 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  end(Parse*, Toke
6e800 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
6e810 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
6e820 74 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73 71  tabCallCreate(sq
6e830 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e  lite3*, int, con
6e840 73 74 20 63 68 61 72 20 2a 2c 20 63 68 61 72 20  st char *, char 
6e850 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
6e860 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
6e870 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50  tabCallConnect(P
6e880 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a  arse*, Table*);.
6e890 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6e8a0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
6e8b0 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65  llDestroy(sqlite
6e8c0 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  3*, int, const c
6e8d0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har *);.SQLITE_P
6e8e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6e8f0 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c 69  e3VtabBegin(sqli
6e900 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 5f 76  te3 *, sqlite3_v
6e910 74 61 62 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tab *);.SQLITE_P
6e920 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a  RIVATE FuncDef *
6e930 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c  sqlite3VtabOverl
6e940 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  oadFunction(sqli
6e950 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20  te3 *,FuncDef*, 
6e960 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 2a 29  int nArg, Expr*)
6e970 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e980 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76   void sqlite3Inv
6e990 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c  alidFunction(sql
6e9a0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
6e9b0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
6e9c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e9d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72  TE int sqlite3Tr
6e9e0 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73  ansferBindings(s
6e9f0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73  qlite3_stmt *, s
6ea00 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a  qlite3_stmt *);.
6ea10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6ea20 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70  nt sqlite3Reprep
6ea30 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  are(Vdbe*);.SQLI
6ea40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6ea50 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43  sqlite3ExprListC
6ea60 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 65  heckLength(Parse
6ea70 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f  *, ExprList*, co
6ea80 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
6ea90 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
6eaa0 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72  eq *sqlite3Binar
6eab0 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28  yCompareCollSeq(
6eac0 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c  Parse *, Expr *,
6ead0 20 45 78 70 72 20 2a 29 3b 0a 0a 0a 2f 2a 0a 2a   Expr *);.../*.*
6eae0 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 75 6c  * Available faul
6eaf0 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 53 68  t injectors.  Sh
6eb00 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 65 64  ould be numbered
6eb10 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
6eb20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  0..*/.#define SQ
6eb30 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54  LITE_FAULTINJECT
6eb40 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 30 0a  OR_MALLOC     0.
6eb50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
6eb60 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55  AULTINJECTOR_COU
6eb70 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a  NT      1../*.**
6eb80 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   The interface t
6eb90 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 66 61  o the code in fa
6eba0 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 20 69  ult.c used for i
6ebb0 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 6e 69  dentifying "beni
6ebc0 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61  gn".** malloc fa
6ebd0 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 73 20  ilures. This is 
6ebe0 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 66 20  only present if 
6ebf0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
6ec00 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 20 6e  TIN_TEST.** is n
6ec10 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  ot defined..*/.#
6ec20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
6ec30 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
6ec40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6ec50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
6ec60 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76  inBenignMalloc(v
6ec70 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
6ec80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
6ec90 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
6eca0 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a  oc(void);.#else.
6ecb0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
6ecc0 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
6ecd0 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 20 73  oc().  #define s
6ece0 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
6ecf0 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 0a 0a  alloc().#endif..
6ed00 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58  #define IN_INDEX
6ed10 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 20 20  _ROWID          
6ed20 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e   1.#define IN_IN
6ed30 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 20 20  DEX_EPH         
6ed40 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 49 4e      2.#define IN
6ed50 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 20 20  _INDEX_INDEX    
6ed60 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 45 5f         3.SQLITE_
6ed70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6ed80 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50  te3FindInIndex(P
6ed90 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20  arse *, Expr *, 
6eda0 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53  int*);..#ifdef S
6edb0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
6edc0 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 54 45  MIC_WRITE.SQLITE
6edd0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
6ede0 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65  qlite3JournalOpe
6edf0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  n(sqlite3_vfs *,
6ee00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73   const char *, s
6ee10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20 69  qlite3_file *, i
6ee20 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
6ee30 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
6ee40 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a  qlite3JournalSiz
6ee50 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 29  e(sqlite3_vfs *)
6ee60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ee70 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f     int sqlite3Jo
6ee80 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69  urnalCreate(sqli
6ee90 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 65 6c  te3_file *);.#el
6eea0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  se.  #define sql
6eeb0 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  ite3JournalSize(
6eec0 70 56 66 73 29 20 28 28 70 56 66 73 29 2d 3e 73  pVfs) ((pVfs)->s
6eed0 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 66 0a  zOsFile).#endif.
6eee0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6eef0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a  void sqlite3MemJ
6ef00 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74  ournalOpen(sqlit
6ef10 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 4c 49  e3_file *);.SQLI
6ef20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6ef30 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
6ef40 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49  Size(void);.SQLI
6ef50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6ef60 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e  qlite3IsMemJourn
6ef70 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  al(sqlite3_file 
6ef80 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  *);..#if SQLITE_
6ef90 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30  MAX_EXPR_DEPTH>0
6efa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6efb0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78    void sqlite3Ex
6efc0 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72 73  prSetHeight(Pars
6efd0 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
6efe0 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *p);.SQLITE_PRIV
6eff0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
6f000 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68  3SelectExprHeigh
6f010 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 51 4c  t(Select *);.SQL
6f020 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
6f030 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65  t sqlite3ExprChe
6f040 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 2a 2c  ckHeight(Parse*,
6f050 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20 23   int);.#else.  #
6f060 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78  define sqlite3Ex
6f070 70 72 53 65 74 48 65 69 67 68 74 28 78 2c 79 29  prSetHeight(x,y)
6f080 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
6f090 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
6f0a0 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 69 6e  ht(x) 0.  #defin
6f0b0 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65  e sqlite3ExprChe
6f0c0 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a 23 65  ckHeight(x,y).#e
6f0d0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49  ndif..SQLITE_PRI
6f0e0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
6f0f0 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75  Get4byte(const u
6f100 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  8*);.SQLITE_PRIV
6f110 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f120 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 75 33  Put4byte(u8*, u3
6f130 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  2);..#ifdef SQLI
6f140 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b  TE_ENABLE_UNLOCK
6f150 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 54 45 5f 50  _NOTIFY.SQLITE_P
6f160 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
6f170 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
6f180 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a  locked(sqlite3 *
6f190 2c 20 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51  , sqlite3 *);.SQ
6f1a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
6f1b0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  oid sqlite3Conne
6f1c0 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71  ctionUnlocked(sq
6f1d0 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49  lite3 *db);.SQLI
6f1e0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
6f1f0 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  d sqlite3Connect
6f200 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74 65  ionClosed(sqlite
6f210 33 20 2a 64 62 29 3b 0a 23 65 6c 73 65 0a 20 20  3 *db);.#else.  
6f220 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43  #define sqlite3C
6f230 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64  onnectionBlocked
6f240 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 20  (x,y).  #define 
6f250 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
6f260 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 0a 20 20 23  nUnlocked(x).  #
6f270 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f  define sqlite3Co
6f280 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 78  nnectionClosed(x
6f290 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65  ).#endif...#ifde
6f2a0 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 23 69 6e  f SQLITE_SSE.#in
6f2b0 63 6c 75 64 65 20 22 73 73 65 49 6e 74 2e 68 22  clude "sseInt.h"
6f2c0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
6f2d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
6f2e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
6f2f0 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  id sqlite3Parser
6f300 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 63 68 61  Trace(FILE*, cha
6f310 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a  r *);.#endif../*
6f320 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54  .** If the SQLIT
6f330 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 41 43 45  E_ENABLE IOTRACE
6f340 20 65 78 69 73 74 73 20 74 68 65 6e 20 74 68 65   exists then the
6f350 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
6f360 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f 54 72 61  .** sqlite3IoTra
6f370 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ce is a pointer 
6f380 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b 65  to a printf-like
6f390 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 6f   routine used to
6f3a0 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74 72  .** print I/O tr
6f3b0 61 63 69 6e 67 20 6d 65 73 73 61 67 65 73 2e 20  acing messages. 
6f3c0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
6f3d0 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45  E_ENABLE_IOTRACE
6f3e0 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41 43  .# define IOTRAC
6f3f0 45 28 41 29 20 20 69 66 28 20 73 71 6c 69 74 65  E(A)  if( sqlite
6f400 33 49 6f 54 72 61 63 65 20 29 7b 20 73 71 6c 69  3IoTrace ){ sqli
6f410 74 65 33 49 6f 54 72 61 63 65 20 41 3b 20 7d 0a  te3IoTrace A; }.
6f420 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6f430 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
6f440 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65  eIOTraceSql(Vdbe
6f450 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f460 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65  TE void (*sqlite
6f470 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20  3IoTrace)(const 
6f480 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c 73  char*,...);.#els
6f490 65 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41  e.# define IOTRA
6f4a0 43 45 28 41 29 0a 23 20 64 65 66 69 6e 65 20 73  CE(A).# define s
6f4b0 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63  qlite3VdbeIOTrac
6f4c0 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a  eSql(X).#endif..
6f4d0 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
6f4e0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73  ******* End of s
6f4f0 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
6f500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f520 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
6f530 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
6f540 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a  le global.c ****
6f550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f570 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
6f580 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a 2a  08 June 13.**.**
6f590 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
6f5a0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
6f5b0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
6f5c0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
6f5d0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
6f5e0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
6f5f0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
6f600 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
6f610 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
6f620 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
6f630 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
6f640 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
6f650 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
6f660 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
6f670 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
6f680 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
6f690 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
6f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6f6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
6f6f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
6f700 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f  ns definitions o
6f710 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  f global variabl
6f720 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73 2e  es and contants.
6f730 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 67 6c 6f 62  .**.** $Id: glob
6f740 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 39  al.c,v 1.12 2009
6f750 2f 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20  /02/05 16:31:46 
6f760 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f  drh Exp $.*/.../
6f770 2a 20 41 6e 20 61 72 72 61 79 20 74 6f 20 6d 61  * An array to ma
6f780 70 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 65  p all upper-case
6f790 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f   characters into
6f7a0 20 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e   their correspon
6f7b0 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61  ding.** lower-ca
6f7c0 73 65 20 63 68 61 72 61 63 74 65 72 2e 20 0a 2a  se character. .*
6f7d0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79  *.** SQLite only
6f7e0 20 63 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 53   considers US-AS
6f7f0 43 49 49 20 28 6f 72 20 45 42 43 44 49 43 29 20  CII (or EBCDIC) 
6f800 63 68 61 72 61 63 74 65 72 73 2e 20 20 57 65 20  characters.  We 
6f810 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65  do not.** handle
6f820 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e   case conversion
6f830 73 20 66 6f 72 20 74 68 65 20 55 54 46 20 63 68  s for the UTF ch
6f840 61 72 61 63 74 65 72 20 73 65 74 20 73 69 6e 63  aracter set sinc
6f850 65 20 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20  e the tables.** 
6f860 69 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61  involved are nea
6f870 72 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62 69  rly as big or bi
6f880 67 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65  gger than SQLite
6f890 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49   itself..*/.SQLI
6f8a0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
6f8b0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73   unsigned char s
6f8c0 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
6f8d0 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20  er[] = {.#ifdef 
6f8e0 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20  SQLITE_ASCII.   
6f8f0 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33     0,  1,  2,  3
6f900 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37  ,  4,  5,  6,  7
6f910 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31  ,  8,  9, 10, 11
6f920 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35  , 12, 13, 14, 15
6f930 2c 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31  , 16, 17,.     1
6f940 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32  8, 19, 20, 21, 2
6f950 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32  2, 23, 24, 25, 2
6f960 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33  6, 27, 28, 29, 3
6f970 30 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33  0, 31, 32, 33, 3
6f980 34 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20  4, 35,.     36, 
6f990 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20  37, 38, 39, 40, 
6f9a0 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20  41, 42, 43, 44, 
6f9b0 34 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20  45, 46, 47, 48, 
6f9c0 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20  49, 50, 51, 52, 
6f9d0 35 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c  53,.     54, 55,
6f9e0 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c   56, 57, 58, 59,
6f9f0 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c   60, 61, 62, 63,
6fa00 20 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c   64, 97, 98, 99,
6fa10 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c  100,101,102,103,
6fa20 0a 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36  .    104,105,106
6fa30 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30  ,107,108,109,110
6fa40 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34  ,111,112,113,114
6fa50 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38  ,115,116,117,118
6fa60 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20  ,119,120,121,.  
6fa70 20 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39    122, 91, 92, 9
6fa80 33 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39  3, 94, 95, 96, 9
6fa90 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
6faa0 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
6fab0 35 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31  5,106,107,.    1
6fac0 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31  08,109,110,111,1
6fad0 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31  12,113,114,115,1
6fae0 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31  16,117,118,119,1
6faf0 32 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31  20,121,122,123,1
6fb00 32 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c  24,125,.    126,
6fb10 31 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c  127,128,129,130,
6fb20 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c  131,132,133,134,
6fb30 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c  135,136,137,138,
6fb40 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c  139,140,141,142,
6fb50 31 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35  143,.    144,145
6fb60 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39  ,146,147,148,149
6fb70 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33  ,150,151,152,153
6fb80 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37  ,154,155,156,157
6fb90 2c 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31  ,158,159,160,161
6fba0 2c 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36  ,.    162,163,16
6fbb0 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36  4,165,166,167,16
6fbc0 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37  8,169,170,171,17
6fbd0 32 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37  2,173,174,175,17
6fbe0 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20  6,177,178,179,. 
6fbf0 20 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31     180,181,182,1
6fc00 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31  83,184,185,186,1
6fc10 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31  87,188,189,190,1
6fc20 39 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31  91,192,193,194,1
6fc30 39 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20  95,196,197,.    
6fc40 31 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c  198,199,200,201,
6fc50 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c  202,203,204,205,
6fc60 32 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c  206,207,208,209,
6fc70 32 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c  210,211,212,213,
6fc80 32 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36  214,215,.    216
6fc90 2c 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30  ,217,218,219,220
6fca0 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34  ,221,222,223,224
6fcb0 2c 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38  ,225,226,227,228
6fcc0 2c 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32  ,229,230,231,232
6fcd0 2c 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33  ,233,.    234,23
6fce0 35 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33  5,236,237,238,23
6fcf0 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34  9,240,241,242,24
6fd00 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34  3,244,245,246,24
6fd10 37 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35  7,248,249,250,25
6fd20 31 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32  1,.    252,253,2
6fd30 35 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69  54,255.#endif.#i
6fd40 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
6fd50 49 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20  IC.      0,  1, 
6fd60 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20   2,  3,  4,  5, 
6fd70 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20   6,  7,  8,  9, 
6fd80 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20  10, 11, 12, 13, 
6fd90 31 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f  14, 15, /* 0x */
6fda0 0a 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38  .     16, 17, 18
6fdb0 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32  , 19, 20, 21, 22
6fdc0 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36  , 23, 24, 25, 26
6fdd0 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30  , 27, 28, 29, 30
6fde0 2c 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20  , 31, /* 1x */. 
6fdf0 20 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20      32, 33, 34, 
6fe00 33 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20  35, 36, 37, 38, 
6fe10 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20  39, 40, 41, 42, 
6fe20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20  43, 44, 45, 46, 
6fe30 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20  47, /* 2x */.   
6fe40 20 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31    48, 49, 50, 51
6fe50 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35  , 52, 53, 54, 55
6fe60 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39  , 56, 57, 58, 59
6fe70 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33  , 60, 61, 62, 63
6fe80 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20  , /* 3x */.     
6fe90 36 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20  64, 65, 66, 67, 
6fea0 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20  68, 69, 70, 71, 
6feb0 37 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20  72, 73, 74, 75, 
6fec0 37 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20  76, 77, 78, 79, 
6fed0 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30  /* 4x */.     80
6fee0 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34  , 81, 82, 83, 84
6fef0 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38  , 85, 86, 87, 88
6ff00 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32  , 89, 90, 91, 92
6ff10 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a  , 93, 94, 95, /*
6ff20 20 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20   5x */.     96, 
6ff30 39 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20  97, 66, 67, 68, 
6ff40 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20  69, 70, 71, 72, 
6ff50 37 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31  73,106,107,108,1
6ff60 30 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36  09,110,111, /* 6
6ff70 78 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31  x */.    112, 81
6ff80 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35  , 82, 83, 84, 85
6ff90 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39  , 86, 87, 88, 89
6ffa0 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35  ,122,123,124,125
6ffb0 2c 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20  ,126,127, /* 7x 
6ffc0 2a 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31  */.    128,129,1
6ffd0 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31  30,131,132,133,1
6ffe0 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31  34,135,136,137,1
6fff0 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31  38,139,140,141,1
70000 34 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f  42,143, /* 8x */
70010 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36  .    144,145,146
70020 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30  ,147,148,149,150
70030 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34  ,151,152,153,154
70040 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36  ,155,156,157,156
70050 2c 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20  ,159, /* 9x */. 
70060 20 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31     160,161,162,1
70070 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31  63,164,165,166,1
70080 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31  67,168,169,170,1
70090 37 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31  71,140,141,142,1
700a0 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20  75, /* Ax */.   
700b0 20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39   176,177,178,179
700c0 2c 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33  ,180,181,182,183
700d0 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37  ,184,185,186,187
700e0 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31  ,188,189,190,191
700f0 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31  , /* Bx */.    1
70100 39 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31  92,129,130,131,1
70110 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31  32,133,134,135,1
70120 33 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32  36,137,202,203,2
70130 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20  04,205,206,207, 
70140 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38  /* Cx */.    208
70150 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38  ,145,146,147,148
70160 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32  ,149,150,151,152
70170 2c 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30  ,153,218,219,220
70180 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a  ,221,222,223, /*
70190 20 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32   Dx */.    224,2
701a0 32 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31  25,162,163,164,1
701b0 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31  65,166,167,168,1
701c0 36 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32  69,232,203,204,2
701d0 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45  05,206,207, /* E
701e0 78 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30  x */.    239,240
701f0 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34  ,241,242,243,244
70200 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38  ,245,246,247,248
70210 2c 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31  ,249,219,220,221
70220 2c 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20  ,222,255, /* Fx 
70230 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
70240 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
70250 67 20 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75  g 256 byte looku
70260 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20  p table is used 
70270 74 6f 20 73 75 70 70 6f 72 74 20 53 51 4c 69 74  to support SQLit
70280 65 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65  es built-in.** e
70290 71 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 68  quivalents to th
702a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e  e following stan
702b0 64 61 72 64 20 6c 69 62 72 61 72 79 20 66 75 6e  dard library fun
702c0 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ctions:.**.**   
702d0 69 73 73 70 61 63 65 28 29 20 20 20 20 20 20 20  isspace()       
702e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
702f0 20 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70   0x01.**   isalp
70300 68 61 28 29 20 20 20 20 20 20 20 20 20 20 20 20  ha()            
70310 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 32              0x02
70320 0a 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29 20  .**   isdigit() 
70330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70340 20 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 20         0x04.**  
70350 20 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20   isalnum()      
70360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70370 20 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 64    0x06.**   isxd
70380 69 67 69 74 28 29 20 20 20 20 20 20 20 20 20 20  igit()          
70390 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
703a0 38 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 29  8.**   toupper()
703b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
703c0 20 20 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0a          0x20.**.
703d0 2a 2a 20 42 69 74 20 30 78 32 30 20 69 73 20 73  ** Bit 0x20 is s
703e0 65 74 20 69 66 20 74 68 65 20 6d 61 70 70 65 64  et if the mapped
703f0 20 63 68 61 72 61 63 74 65 72 20 72 65 71 75 69   character requi
70400 72 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20  res translation 
70410 74 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 65  to upper.** case
70420 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63 68  . i.e. if the ch
70430 61 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f 77  aracter is a low
70440 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 63 68  er-case ASCII ch
70450 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78  aracter..** If x
70460 20 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65   is a lower-case
70470 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72   ASCII character
70480 2c 20 74 68 65 6e 20 69 74 73 20 75 70 70 65 72  , then its upper
70490 2d 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74  -case equivalent
704a0 0a 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 32 30  .** is (x - 0x20
704b0 29 2e 20 54 68 65 72 65 66 6f 72 65 20 74 6f 75  ). Therefore tou
704c0 70 70 65 72 28 29 20 63 61 6e 20 62 65 20 69 6d  pper() can be im
704d0 70 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a  plemented as:.**
704e0 0a 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 70  .**   (x & ~(map
704f0 5b 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a  [x]&0x20)).**.**
70500 20 53 74 61 6e 64 61 72 64 20 66 75 6e 63 74 69   Standard functi
70510 6f 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20  on tolower() is 
70520 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e  implemented usin
70530 67 20 74 68 65 20 73 71 6c 69 74 65 33 55 70 70  g the sqlite3Upp
70540 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61  erToLower[].** a
70550 72 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20  rray. tolower() 
70560 69 73 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 74  is used more oft
70570 65 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 72 28  en than toupper(
70580 29 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  ) by SQLite..**.
70590 2a 2a 20 53 51 4c 69 74 65 27 73 20 76 65 72 73  ** SQLite's vers
705a0 69 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63  ions are identic
705b0 61 6c 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61  al to the standa
705c0 72 64 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75  rd versions assu
705d0 6d 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65  ming a.** locale
705e0 20 6f 66 20 22 43 22 2e 20 54 68 65 79 20 61 72   of "C". They ar
705f0 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73  e implemented as
70600 20 6d 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74   macros in sqlit
70610 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65  eInt.h..*/.#ifde
70620 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53  f SQLITE_ASCII.S
70630 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
70640 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
70650 72 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61  r sqlite3CtypeMa
70660 70 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30  p[256] = {.  0x0
70670 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
70680 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70690 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
706a0 20 30 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e   00..07    .....
706b0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
706c0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c  x01, 0x01, 0x01,
706d0 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30   0x01, 0x01, 0x0
706e0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e  0, 0x00,  /* 08.
706f0 2e 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .0f    ........ 
70700 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  */.  0x00, 0x00,
70710 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70720 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
70730 78 30 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20  x00,  /* 10..17 
70740 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
70750 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70760 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
70770 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70780 20 20 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e    /* 18..1f    .
70790 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
707a0 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  1, 0x00, 0x00, 0
707b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
707c0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
707d0 20 32 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24   20..27     !"#$
707e0 25 26 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  %&' */.  0x00, 0
707f0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70800 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70810 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e  0, 0x00,  /* 28.
70820 2e 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20  .2f    ()*+,-./ 
70830 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c  */.  0x0c, 0x0c,
70840 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30   0x0c, 0x0c, 0x0
70850 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30  c, 0x0c, 0x0c, 0
70860 78 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20  x0c,  /* 30..37 
70870 20 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20     01234567 */. 
70880 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30   0x0c, 0x0c, 0x0
70890 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
708a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
708b0 20 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38    /* 38..3f    8
708c0 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78  9:;<=>? */..  0x
708d0 30 30 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20  00, 0x0a, 0x0a, 
708e0 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61  0x0a, 0x0a, 0x0a
708f0 2c 20 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f  , 0x0a, 0x02,  /
70900 2a 20 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43  * 40..47    @ABC
70910 44 45 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20  DEFG */.  0x02, 
70920 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
70930 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
70940 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38  02, 0x02,  /* 48
70950 2e 2e 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f  ..4f    HIJKLMNO
70960 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32   */.  0x02, 0x02
70970 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
70980 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
70990 30 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37  0x02,  /* 50..57
709a0 20 20 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a      PQRSTUVW */.
709b0 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78    0x02, 0x02, 0x
709c0 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  02, 0x00, 0x00, 
709d0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
709e0 2c 20 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20  ,  /* 58..5f    
709f0 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78  XYZ[\]^_ */.  0x
70a00 30 30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20  00, 0x2a, 0x2a, 
70a10 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61  0x2a, 0x2a, 0x2a
70a20 2c 20 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f  , 0x2a, 0x22,  /
70a30 2a 20 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63  * 60..67    `abc
70a40 64 65 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20  defg */.  0x22, 
70a50 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
70a60 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
70a70 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38  22, 0x22,  /* 68
70a80 2e 2e 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f  ..6f    hijklmno
70a90 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32   */.  0x22, 0x22
70aa0 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
70ab0 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
70ac0 30 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37  0x22,  /* 70..77
70ad0 20 20 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a      pqrstuvw */.
70ae0 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78    0x22, 0x22, 0x
70af0 32 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  22, 0x00, 0x00, 
70b00 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
70b10 2c 20 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20  ,  /* 78..7f    
70b20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30  xyz{|}~. */..  0
70b30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70b40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70b50 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
70b60 2f 2a 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e  /* 80..87    ...
70b70 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
70b80 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70b90 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
70ba0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38  x00, 0x00,  /* 8
70bb0 38 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..8f    .......
70bc0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
70bd0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
70be0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70bf0 20 30 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e 39   0x00,  /* 90..9
70c00 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
70c10 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
70c20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70c30 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70c40 30 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20  0,  /* 98..9f   
70c50 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
70c60 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70c70 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70c80 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
70c90 2f 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e  /* a0..a7    ...
70ca0 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
70cb0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70cc0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
70cd0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61  x00, 0x00,  /* a
70ce0 38 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..af    .......
70cf0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
70d00 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
70d10 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70d20 20 30 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e 62   0x00,  /* b0..b
70d30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
70d40 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
70d50 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
70d60 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
70d70 30 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20  0,  /* b8..bf   
70d80 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20   ........ */..  
70d90 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
70da0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
70db0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
70dc0 20 2f 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e   /* c0..c7    ..
70dd0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
70de0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
70df0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
70e00 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
70e10 63 38 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e  c8..cf    ......
70e20 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
70e30 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
70e40 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
70e50 2c 20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 2e  , 0x00,  /* d0..
70e60 64 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  d7    ........ *
70e70 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
70e80 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
70e90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
70ea0 30 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20  00,  /* d8..df  
70eb0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
70ec0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
70ed0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
70ee0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
70ef0 20 2f 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e   /* e0..e7    ..
70f00 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
70f10 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
70f20 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
70f30 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
70f40 65 38 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e  e8..ef    ......
70f50 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
70f60 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
70f70 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
70f80 2c 20 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 2e  , 0x00,  /* f0..
70f90 66 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  f7    ........ *
70fa0 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
70fb0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
70fc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
70fd0 30 30 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20  00   /* f8..ff  
70fe0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b    ........ */.};
70ff0 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a  .#endif..../*.**
71000 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
71010 69 6e 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e  ingleton contain
71020 73 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e  s the global con
71030 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a  figuration for.*
71040 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  * the SQLite lib
71050 72 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rary..*/.SQLITE_
71060 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57  PRIVATE SQLITE_W
71070 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65  SD struct Sqlite
71080 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43  3Config sqlite3C
71090 6f 6e 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c  onfig = {.   SQL
710a0 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53  ITE_DEFAULT_MEMS
710b0 54 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73  TATUS,  /* bMems
710c0 74 61 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20  tat */.   1,    
710d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
710e0 20 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74       /* bCoreMut
710f0 65 78 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f  ex */.   SQLITE_
71100 54 48 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20  THREADSAFE==1,  
71110 20 20 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65      /* bFullMute
71120 78 20 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66  x */.   0x7fffff
71130 66 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  fe,             
71140 20 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a     /* mxStrlen *
71150 2f 0a 20 20 20 31 30 30 2c 20 20 20 20 20 20 20  /.   100,       
71160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71170 2f 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a  /* szLookaside *
71180 2f 0a 20 20 20 35 30 30 2c 20 20 20 20 20 20 20  /.   500,       
71190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
711a0 2f 2a 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f  /* nLookaside */
711b0 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30  .   {0,0,0,0,0,0
711c0 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f  ,0,0},         /
711d0 2a 20 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30  * m */.   {0,0,0
711e0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20  ,0,0,0,0,0,0},  
711f0 20 20 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f       /* mutex */
71200 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30  .   {0,0,0,0,0,0
71210 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f  ,0,0,0,0,0},   /
71220 2a 20 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28  * pcache */.   (
71230 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20  void*)0,        
71240 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65            /* pHe
71250 61 70 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ap */.   0,     
71260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71270 20 20 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a      /* nHeap */.
71280 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20     0, 0,        
71290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
712a0 20 6d 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20   mnHeap, mxHeap 
712b0 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20  */.   (void*)0, 
712c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
712d0 20 2f 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a   /* pScratch */.
712e0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
712f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
71300 20 73 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20   szScratch */.  
71310 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
71320 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
71330 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76  Scratch */.   (v
71340 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20  oid*)0,         
71350 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67           /* pPag
71360 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  e */.   0,      
71370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71380 20 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a     /* szPage */.
71390 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
713a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
713b0 20 6e 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20   nPage */.   0, 
713c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
713d0 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72          /* mxPar
713e0 73 65 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30  serStack */.   0
713f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
71400 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61            /* sha
71410 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20  redCacheEnabled 
71420 2a 2f 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65  */.   /* All the
71430 20 72 65 73 74 20 6e 65 65 64 20 74 6f 20 61 6c   rest need to al
71440 77 61 79 73 20 62 65 20 7a 65 72 6f 20 2a 2f 0a  ways be zero */.
71450 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
71460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
71470 20 69 73 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c   isInit */.   0,
71480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71490 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 50 72           /* inPr
714a0 6f 67 72 65 73 73 20 2a 2f 0a 20 20 20 30 2c 20  ogress */.   0, 
714b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
714c0 20 20 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 6c          /* isMal
714d0 6c 6f 63 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c  locInit */.   0,
714e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
714f0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 49 6e 69           /* pIni
71500 74 4d 75 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20  tMutex */.   0, 
71510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71520 20 20 20 20 20 20 20 20 2f 2a 20 6e 52 65 66 49          /* nRefI
71530 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a  nitMutex */.};..
71540 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c  ./*.** Hash tabl
71550 65 20 66 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e  e for global fun
71560 63 74 69 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f  ctions - functio
71570 6e 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  ns common to all
71580 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
71590 6e 65 63 74 69 6f 6e 73 2e 20 20 41 66 74 65 72  nections.  After
715a0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c   initialization,
715b0 20 74 68 69 73 20 74 61 62 6c 65 20 69 73 0a 2a   this table is.*
715c0 2a 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a  * read-only..*/.
715d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
715e0 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65  QLITE_WSD FuncDe
715f0 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f  fHash sqlite3Glo
71600 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f  balFunctions;../
71610 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  *.** The value o
71620 66 20 74 68 65 20 22 70 65 6e 64 69 6e 67 22 20  f the "pending" 
71630 62 79 74 65 20 6d 75 73 74 20 62 65 20 30 78 34  byte must be 0x4
71640 30 30 30 30 30 30 30 20 28 31 20 62 79 74 65 20  0000000 (1 byte 
71650 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69  past the.** 1-gi
71660 62 61 62 79 74 65 20 62 6f 75 6e 64 61 72 79 29  babyte boundary)
71670 20 69 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c 65   in a compatible
71680 20 64 61 74 61 62 61 73 65 2e 20 20 53 51 4c 69   database.  SQLi
71690 74 65 20 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a  te never uses.**
716a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61   the database pa
716b0 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ge that contains
716c0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74   the pending byt
716d0 65 2e 20 20 49 74 20 6e 65 76 65 72 20 61 74 74  e.  It never att
716e0 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64  empts.** to read
716f0 20 6f 72 20 77 72 69 74 65 20 74 68 61 74 20 70   or write that p
71700 61 67 65 2e 20 20 54 68 65 20 70 65 6e 64 69 6e  age.  The pendin
71710 67 20 62 79 74 65 20 70 61 67 65 20 69 73 20 73  g byte page is s
71720 65 74 20 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72  et assign.** for
71730 20 75 73 65 20 62 79 20 74 68 65 20 56 46 53 20   use by the VFS 
71740 6c 61 79 65 72 73 20 61 73 20 73 70 61 63 65 20  layers as space 
71750 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c  for managing fil
71760 65 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44  e locks..**.** D
71770 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 69  uring testing, i
71780 74 20 69 73 20 6f 66 74 65 6e 20 64 65 73 69 72  t is often desir
71790 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20 74 68 65  able to move the
717a0 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f   pending byte to
717b0 0a 2a 2a 20 61 20 64 69 66 66 65 72 65 6e 74 20  .** a different 
717c0 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20  position in the 
717d0 66 69 6c 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f  file.  This allo
717e0 77 73 20 63 6f 64 65 20 74 68 61 74 20 68 61 73  ws code that has
717f0 20 74 6f 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68   to.** deal with
71800 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74   the pending byt
71810 65 20 74 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65  e to run on file
71820 73 20 74 68 61 74 20 61 72 65 20 6d 75 63 68 20  s that are much 
71830 73 6d 61 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20  smaller.** than 
71840 31 20 47 69 42 2e 20 20 54 68 65 20 73 71 6c 69  1 GiB.  The sqli
71850 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
71860 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e  () interface can
71870 20 62 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d   be used to.** m
71880 6f 76 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20  ove the pending 
71890 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f  byte..**.** IMPO
718a0 52 54 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67  RTANT:  Changing
718b0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74   the pending byt
718c0 65 20 74 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f  e to any value o
718d0 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34  ther than.** 0x4
718e0 30 30 30 30 30 30 30 20 72 65 73 75 6c 74 73 20  0000000 results 
718f0 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62  in an incompatib
71900 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
71910 20 66 6f 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e   format!.** Chan
71920 67 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67  ging the pending
71930 20 62 79 74 65 20 64 75 72 69 6e 67 20 6f 70 65   byte during ope
71940 72 61 74 69 6e 67 20 72 65 73 75 6c 74 73 20 69  rating results i
71950 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61  n undefined.** a
71960 6e 64 20 64 69 6c 65 74 65 72 69 6f 75 73 20 62  nd dileterious b
71970 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  ehavior..*/.SQLI
71980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
71990 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74  qlite3PendingByt
719a0 65 20 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a  e = 0x40000000;.
719b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
719c0 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e 63   End of global.c
719d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
719e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
719f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
71a00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
71a10 20 42 65 67 69 6e 20 66 69 6c 65 20 73 74 61 74   Begin file stat
71a20 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  us.c ***********
71a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
71a50 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65  ./*.** 2008 June
71a60 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   18.**.** The au
71a70 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
71a80 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
71a90 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
71aa0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
71ab0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
71ac0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
71ad0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
71ae0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
71af0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
71b00 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
71b10 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
71b20 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
71b30 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
71b40 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
71b50 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
71b60 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
71b70 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
71b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
71bc0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f  **.**.** This mo
71bd0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  dule implements 
71be0 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74  the sqlite3_stat
71bf0 75 73 28 29 20 69 6e 74 65 72 66 61 63 65 20 61  us() interface a
71c00 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75  nd related.** fu
71c10 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a  nctionality..**.
71c20 2a 2a 20 24 49 64 3a 20 73 74 61 74 75 73 2e 63  ** $Id: status.c
71c30 2c 76 20 31 2e 39 20 32 30 30 38 2f 30 39 2f 30  ,v 1.9 2008/09/0
71c40 32 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45  2 00:52:52 drh E
71c50 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56  xp $.*/../*.** V
71c60 61 72 69 61 62 6c 65 73 20 69 6e 20 77 68 69 63  ariables in whic
71c70 68 20 74 6f 20 72 65 63 6f 72 64 20 73 74 61 74  h to record stat
71c80 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  us information..
71c90 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
71ca0 74 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70  t sqlite3StatTyp
71cb0 65 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70  e sqlite3StatTyp
71cc0 65 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  e;.static SQLITE
71cd0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69  _WSD struct sqli
71ce0 74 65 33 53 74 61 74 54 79 70 65 20 7b 0a 20 20  te3StatType {.  
71cf0 69 6e 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b  int nowValue[9];
71d00 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
71d10 65 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69  ent value */.  i
71d20 6e 74 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20  nt mxValue[9];  
71d30 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
71d40 75 6d 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71  um value */.} sq
71d50 6c 69 74 65 33 53 74 61 74 20 3d 20 7b 20 7b 30  lite3Stat = { {0
71d60 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a  ,}, {0,} };.../*
71d70 20 54 68 65 20 22 77 73 64 53 74 61 74 22 20 6d   The "wsdStat" m
71d80 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76  acro will resolv
71d90 65 20 74 6f 20 74 68 65 20 73 74 61 74 75 73 20  e to the status 
71da0 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73  information.** s
71db0 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66  tate vector.  If
71dc0 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63   writable static
71dd0 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f   data is unsuppo
71de0 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67  rted on the targ
71df0 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74  et,.** we have t
71e00 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61  o locate the sta
71e10 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e  te vector at run
71e20 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d  -time.  In the m
71e30 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61  ore common.** ca
71e40 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c  se where writabl
71e50 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
71e60 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53   supported, wsdS
71e70 74 61 74 20 63 61 6e 20 72 65 66 65 72 20 64 69  tat can refer di
71e80 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65  rectly.** to the
71e90 20 22 73 71 6c 69 74 65 33 53 74 61 74 22 20 73   "sqlite3Stat" s
71ea0 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c  tate vector decl
71eb0 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23  ared above..*/.#
71ec0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
71ed0 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77  T_WSD.# define w
71ee0 73 64 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69  sdStatInit  sqli
71ef0 74 65 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d  te3StatType *x =
71f00 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33   &GLOBAL(sqlite3
71f10 53 74 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33  StatType,sqlite3
71f20 53 74 61 74 29 0a 23 20 64 65 66 69 6e 65 20 77  Stat).# define w
71f30 73 64 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73  sdStat x[0].#els
71f40 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74  e.# define wsdSt
71f50 61 74 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20  atInit.# define 
71f60 77 73 64 53 74 61 74 20 73 71 6c 69 74 65 33 53  wsdStat sqlite3S
71f70 74 61 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  tat.#endif../*.*
71f80 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72  * Return the cur
71f90 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 20  rent value of a 
71fa0 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72  status parameter
71fb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
71fc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
71fd0 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 20 6f  tatusValue(int o
71fe0 70 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69  p){.  wsdStatIni
71ff0 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e  t;.  assert( op>
72000 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69  =0 && op<ArraySi
72010 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  ze(wsdStat.nowVa
72020 6c 75 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  lue) );.  return
72030 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
72040 65 5b 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e[op];.}../*.** 
72050 41 64 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c  Add N to the val
72060 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 72  ue of a status r
72070 65 63 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73  ecord.  It is as
72080 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 0a 2a  sumed that the.*
72090 2a 20 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61  * caller holds a
720a0 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73  ppropriate locks
720b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
720c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
720d0 53 74 61 74 75 73 41 64 64 28 69 6e 74 20 6f 70  StatusAdd(int op
720e0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53  , int N){.  wsdS
720f0 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72  tatInit;.  asser
72100 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41  t( op>=0 && op<A
72110 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74  rraySize(wsdStat
72120 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20  .nowValue) );.  
72130 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
72140 5b 6f 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28  [op] += N;.  if(
72150 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
72160 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78  e[op]>wsdStat.mx
72170 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20  Value[op] ){.   
72180 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65   wsdStat.mxValue
72190 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e  [op] = wsdStat.n
721a0 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d  owValue[op];.  }
721b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
721c0 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61  e value of a sta
721d0 74 75 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c  tus to X..*/.SQL
721e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
721f0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
72200 74 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29  t(int op, int X)
72210 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b  {.  wsdStatInit;
72220 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30  .  assert( op>=0
72230 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65   && op<ArraySize
72240 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75  (wsdStat.nowValu
72250 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e  e) );.  wsdStat.
72260 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58  nowValue[op] = X
72270 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61 74 2e  ;.  if( wsdStat.
72280 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64  nowValue[op]>wsd
72290 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d  Stat.mxValue[op]
722a0 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e   ){.    wsdStat.
722b0 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73  mxValue[op] = ws
722c0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f  dStat.nowValue[o
722d0 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p];.  }.}../*.**
722e0 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69 6e   Query status in
722f0 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
72300 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
72310 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
72320 74 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69  t reading or wri
72330 74 69 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a  ting an aligned.
72340 2a 2a 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  ** 32-bit intege
72350 72 20 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f  r is an atomic o
72360 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 68  peration.  If th
72370 61 74 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73  at assumption is
72380 20 6e 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68   not true,.** th
72390 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
723a0 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
723b0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
723c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61   int sqlite3_sta
723d0 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20  tus(int op, int 
723e0 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a  *pCurrent, int *
723f0 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20  pHighwater, int 
72400 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 73  resetFlag){.  ws
72410 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 28  dStatInit;.  if(
72420 20 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72   op<0 || op>=Arr
72430 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e  aySize(wsdStat.n
72440 6f 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20  owValue) ){.    
72450 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
72460 53 55 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75  SUSE;.  }.  *pCu
72470 72 72 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e  rrent = wsdStat.
72480 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20  nowValue[op];.  
72490 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 77 73  *pHighwater = ws
724a0 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70  dStat.mxValue[op
724b0 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c  ];.  if( resetFl
724c0 61 67 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61  ag ){.    wsdSta
724d0 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20  t.mxValue[op] = 
724e0 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
724f0 5b 6f 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  [op];.  }.  retu
72500 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
72510 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61  ./*.** Query sta
72520 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  tus information 
72530 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74  for a single dat
72540 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
72550 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
72560 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74  nt sqlite3_db_st
72570 61 74 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20  atus(.  sqlite3 
72580 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  *db,          /*
72590 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
725a0 6e 6e 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73  nnection whose s
725b0 74 61 74 75 73 20 69 73 20 64 65 73 69 72 65 64  tatus is desired
725c0 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
725d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
725e0 74 61 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20  tatus verb */.  
725f0 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20  int *pCurrent,  
72600 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63        /* Write c
72610 75 72 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72  urrent value her
72620 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67  e */.  int *pHig
72630 68 77 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20  hwater,      /* 
72640 57 72 69 74 65 20 68 69 67 68 2d 77 61 74 65 72  Write high-water
72650 20 6d 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20   mark here */.  
72660 69 6e 74 20 72 65 73 65 74 46 6c 61 67 20 20 20  int resetFlag   
72670 20 20 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68        /* Reset h
72680 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69  igh-water mark i
72690 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73  f true */.){.  s
726a0 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
726b0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53   case SQLITE_DBS
726c0 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f  TATUS_LOOKASIDE_
726d0 55 53 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70  USED: {.      *p
726e0 43 75 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f  Current = db->lo
726f0 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20  okaside.nOut;.  
72700 20 20 20 20 2a 70 48 69 67 68 77 61 74 65 72 20      *pHighwater 
72710 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  = db->lookaside.
72720 6d 78 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 28  mxOut;.      if(
72730 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20   resetFlag ){.  
72740 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73        db->lookas
72750 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e  ide.mxOut = db->
72760 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a  lookaside.nOut;.
72770 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
72780 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
72790 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
727a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
727b0 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ROR;.    }.  }. 
727c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
727d0 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  K;.}../*********
727e0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61  ***** End of sta
727f0 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tus.c **********
72800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72820 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
72830 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
72840 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   date.c ********
72850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72870 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
72880 20 4f 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a   October 31.**.*
72890 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
728a0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
728b0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
728c0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
728d0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
728e0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
728f0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
72900 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
72910 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
72920 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
72930 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
72940 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
72950 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
72960 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
72970 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
72980 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
72990 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
729a0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
729b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
729f0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
72a00 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
72a10 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
72a20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a  date and time.**
72a30 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53   functions for S
72a40 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  QLite.  .**.** T
72a50 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65  here is only one
72a60 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c   exported symbol
72a70 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20   in this file - 
72a80 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  the function.** 
72a90 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44  sqlite3RegisterD
72aa0 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73  ateTimeFunctions
72ab0 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20  () found at the 
72ac0 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69  bottom of the fi
72ad0 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72  le..** All other
72ae0 20 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73   code has file s
72af0 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  cope..**.** $Id:
72b00 20 64 61 74 65 2e 63 2c 76 20 31 2e 31 30 33 20   date.c,v 1.103 
72b10 32 30 30 39 2f 30 32 2f 30 34 20 30 33 3a 35 39  2009/02/04 03:59
72b20 3a 32 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a  :25 shane Exp $.
72b30 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f  **.** SQLite pro
72b40 63 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 73  cesses all times
72b50 20 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a 75   and dates as Ju
72b60 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 73  lian Day numbers
72b70 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 20  .  The.** dates 
72b80 61 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73 74  and times are st
72b90 6f 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d 62  ored as the numb
72ba0 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65  er of days since
72bb0 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65   noon.** in Gree
72bc0 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65  nwich on Novembe
72bd0 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e 20  r 24, 4714 B.C. 
72be0 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
72bf0 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61   Gregorian.** ca
72c00 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 0a  lendar system. .
72c10 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31  **.** 1970-01-01
72c20 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20   00:00:00 is JD 
72c30 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30  2440587.5.** 200
72c40 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30  0-01-01 00:00:00
72c50 20 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e 35   is JD 2451544.5
72c60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c  .**.** This impl
72c70 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 65  emention require
72c80 73 20 79 65 61 72 73 20 74 6f 20 62 65 20 65 78  s years to be ex
72c90 70 72 65 73 73 65 64 20 61 73 20 61 20 34 2d 64  pressed as a 4-d
72ca0 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77  igit number.** w
72cb0 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20  hich means that 
72cc0 6f 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77 65  only dates betwe
72cd0 65 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61 6e  en 0000-01-01 an
72ce0 64 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61 6e  d 9999-12-31 can
72cf0 0a 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e 74  .** be represent
72d00 65 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20  ed, even though 
72d10 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65  julian day numbe
72d20 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20  rs allow a much 
72d30 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f  wider.** range o
72d40 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  f dates..**.** T
72d50 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c  he Gregorian cal
72d60 65 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73 20  endar system is 
72d70 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74  used for all dat
72d80 65 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a  es and times,.**
72d90 20 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61 74   even those that
72da0 20 70 72 65 64 61 74 65 20 74 68 65 20 47 72 65   predate the Gre
72db0 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e  gorian calendar.
72dc0 20 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73 75    Historians usu
72dd0 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 20  ally.** use the 
72de0 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 20  Julian calendar 
72df0 66 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72 20  for dates prior 
72e00 74 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61 6e  to 1582-10-15 an
72e10 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61  d for some.** da
72e20 74 65 73 20 61 66 74 65 72 77 61 72 64 73 2c 20  tes afterwards, 
72e30 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63  depending on loc
72e40 61 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66 20  ale.  Beware of 
72e50 74 68 69 73 20 64 69 66 66 65 72 65 6e 63 65 2e  this difference.
72e60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65  .**.** The conve
72e70 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73  rsion algorithms
72e80 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   are implemented
72e90 20 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72 69   based on descri
72ea0 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65  ptions.** in the
72eb0 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a   following text:
72ec0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e  .**.**      Jean
72ed0 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 41   Meeus.**      A
72ee0 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f  stronomical Algo
72ef0 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 74  rithms, 2nd Edit
72f00 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20  ion, 1998.**    
72f10 20 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36 2d    ISBM 0-943396-
72f20 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c  61-1.**      Wil
72f30 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a  lmann-Bell, Inc.
72f40 2a 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e 64  **      Richmond
72f50 2c 20 56 69 72 67 69 6e 69 61 20 28 55 53 41 29  , Virginia (USA)
72f60 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69  .*/.#include <ti
72f70 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53  me.h>..#ifndef S
72f80 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54  QLITE_OMIT_DATET
72f90 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a  IME_FUNCS../*.**
72fa0 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 6f   On recent Windo
72fb0 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 68  ws platforms, th
72fc0 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 20  e localtime_s() 
72fd0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 69  function is avai
72fe0 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 74  lable.** as part
72ff0 20 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 20   of the "Secure 
73000 43 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 65  CRT". It is esse
73010 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c 65  ntially equivale
73020 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74  nt to .** localt
73030 69 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 6c  ime_r() availabl
73040 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f 53  e under most POS
73050 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 78  IX platforms, ex
73060 63 65 70 74 20 74 68 61 74 20 74 68 65 20 0a 2a  cept that the .*
73070 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 70  * order of the p
73080 61 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76  arameters is rev
73090 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  ersed..**.** See
730a0 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63   http://msdn.mic
730b0 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73  rosoft.com/en-us
730c0 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79  /library/a442x3y
730d0 65 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a 2a  e(VS.80).aspx..*
730e0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 72  *.** If the user
730f0 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 74   has not indicat
73100 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c 74  ed to use localt
73110 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 6c  ime_r() or local
73120 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 65  time_s().** alre
73130 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 61  ady, check for a
73140 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e 76  n MSVC build env
73150 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 72  ironment that pr
73160 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c  ovides .** local
73170 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66  time_s()..*/.#if
73180 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c   !defined(HAVE_L
73190 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 21  OCALTIME_R) && !
731a0 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43  defined(HAVE_LOC
731b0 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a 20  ALTIME_S) && \. 
731c0 20 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 43      defined(_MSC
731d0 5f 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 64  _VER) && defined
731e0 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f 44  (_CRT_INSECURE_D
731f0 45 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 6e  EPRECATE).#defin
73200 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45  e HAVE_LOCALTIME
73210 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  _S 1.#endif../*.
73220 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 66  ** A structure f
73230 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e  or holding a sin
73240 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d  gle date and tim
73250 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  e..*/.typedef st
73260 72 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 61  ruct DateTime Da
73270 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44  teTime;.struct D
73280 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69  ateTime {.  sqli
73290 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f  te3_int64 iJD; /
732a0 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 79  * The julian day
732b0 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 36   number times 86
732c0 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20  400000 */.  int 
732d0 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f  Y, M, D;       /
732e0 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61  * Year, month, a
732f0 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20  nd day */.  int 
73300 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  h, m;          /
73310 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74  * Hour and minut
73320 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20  es */.  int tz; 
73330 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69             /* Ti
73340 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e  mezone offset in
73350 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f   minutes */.  do
73360 75 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 20  uble s;         
73370 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20   /* Seconds */. 
73380 20 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b 20   char validYMD; 
73390 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20      /* True (1) 
733a0 69 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c  if Y,M,D are val
733b0 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c  id */.  char val
733c0 69 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54 72  idHMS;     /* Tr
733d0 75 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73 20  ue (1) if h,m,s 
733e0 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63  are valid */.  c
733f0 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 20  har validJD;    
73400 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66    /* True (1) if
73410 20 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a 2f   iJD is valid */
73420 0a 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a 3b  .  char validTZ;
73430 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31        /* True (1
73440 29 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 64  ) if tz is valid
73450 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43   */.};.../*.** C
73460 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e 74  onvert zDate int
73470 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e  o one or more in
73480 74 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 6f  tegers.  Additio
73490 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a  nal arguments.**
734a0 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 20   come in groups 
734b0 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  of 5 as follows:
734c0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20  .**.**       N  
734d0 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 64       number of d
734e0 69 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e 74  igits in the int
734f0 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69  eger.**       mi
73500 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c  n     minimum al
73510 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74  lowed value of t
73520 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  he integer.**   
73530 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 69      max     maxi
73540 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75  mum allowed valu
73550 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  e of the integer
73560 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 20  .**       nextC 
73570 20 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65    first characte
73580 72 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 65  r after the inte
73590 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 61  ger.**       pVa
735a0 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 72  l    where to wr
735b0 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 73  ite the integers
735c0 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f   value..**.** Co
735d0 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e  nversions contin
735e0 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74  ue until one wit
735f0 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 6e  h nextC==0 is en
73600 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68  countered..** Th
73610 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  e function retur
73620 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
73630 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e 76   successful conv
73640 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74  ersions..*/.stat
73650 69 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 73  ic int getDigits
73660 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61  (const char *zDa
73670 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  te, ...){.  va_l
73680 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 61  ist ap;.  int va
73690 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e  l;.  int N;.  in
736a0 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78  t min;.  int max
736b0 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20  ;.  int nextC;. 
736c0 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e   int *pVal;.  in
736d0 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f  t cnt = 0;.  va_
736e0 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 29  start(ap, zDate)
736f0 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20  ;.  do{.    N = 
73700 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
73710 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72  .    min = va_ar
73720 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
73730 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  max = va_arg(ap,
73740 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 43   int);.    nextC
73750 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
73760 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 76  t);.    pVal = v
73770 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b  a_arg(ap, int*);
73780 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20  .    val = 0;.  
73790 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a    while( N-- ){.
737a0 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
737b0 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65  e3Isdigit(*zDate
737c0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  ) ){.        got
737d0 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b  o end_getDigits;
737e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76  .      }.      v
737f0 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a  al = val*10 + *z
73800 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20  Date - '0';.    
73810 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d    zDate++;.    }
73820 0a 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e  .    if( val<min
73830 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28   || val>max || (
73840 6e 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74  nextC!=0 && next
73850 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20  C!=*zDate) ){.  
73860 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74      goto end_get
73870 44 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20  Digits;.    }.  
73880 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20    *pVal = val;. 
73890 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
738a0 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28  cnt++;.  }while(
738b0 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65   nextC );.end_ge
738c0 74 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e  tDigits:.  va_en
738d0 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  d(ap);.  return 
738e0 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  cnt;.}../*.** Re
738f0 61 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d  ad text from z[]
73900 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74   and convert int
73910 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  o a floating poi
73920 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75  nt number.  Retu
73930 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  rn.** the number
73940 20 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65   of digits conve
73950 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rted..*/.#define
73960 20 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65   getValue sqlite
73970 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  3AtoF../*.** Par
73980 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78  se a timezone ex
73990 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65  tension on the e
739a0 6e 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d  nd of a date-tim
739b0 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73  e..** The extens
739c0 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ion is of the fo
739d0 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  rm:.**.**       
739e0 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a   (+/-)HH:MM.**.*
739f0 2a 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20  * Or the "zulu" 
73a00 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  notation:.**.** 
73a10 20 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49         Z.**.** I
73a20 66 20 74 68 65 20 70 61 72 73 65 20 69 73 20 73  f the parse is s
73a30 75 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65  uccessful, write
73a40 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   the number of m
73a50 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61  inutes.** of cha
73a60 6e 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64  nge in p->tz and
73a70 20 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61   return 0.  If a
73a80 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63   parser error oc
73a90 63 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20  curs,.** return 
73aa0 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20  non-zero..**.** 
73ab0 41 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66  A missing specif
73ac0 69 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69  ier is not consi
73ad0 64 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a  dered an error..
73ae0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
73af0 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73  rseTimezone(cons
73b00 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44  t char *zDate, D
73b10 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69  ateTime *p){.  i
73b20 6e 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e  nt sgn = 0;.  in
73b30 74 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e  t nHr, nMn;.  in
73b40 74 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 71  t c;.  while( sq
73b50 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44  lite3Isspace(*zD
73b60 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b  ate) ){ zDate++;
73b70 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a   }.  p->tz = 0;.
73b80 20 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 20    c = *zDate;.  
73b90 69 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 20  if( c=='-' ){.  
73ba0 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65    sgn = -1;.  }e
73bb0 6c 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 29  lse if( c=='+' )
73bc0 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a  {.    sgn = +1;.
73bd0 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
73be0 5a 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a  Z' || c=='z' ){.
73bf0 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20      zDate++;.   
73c00 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b   goto zulu_time;
73c10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
73c20 74 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a 20  turn c!=0;.  }. 
73c30 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 20   zDate++;.  if( 
73c40 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c  getDigits(zDate,
73c50 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c 20   2, 0, 14, ':', 
73c60 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c 20  &nHr, 2, 0, 59, 
73c70 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20  0, &nMn)!=2 ){. 
73c80 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
73c90 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20  .  zDate += 5;. 
73ca0 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d   p->tz = sgn*(nM
73cb0 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c  n + nHr*60);.zul
73cc0 75 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 28  u_time:.  while(
73cd0 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
73ce0 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65  *zDate) ){ zDate
73cf0 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a  ++; }.  return *
73d00 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  zDate!=0;.}../*.
73d10 2a 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f  ** Parse times o
73d20 66 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d  f the form HH:MM
73d30 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20   or HH:MM:SS or 
73d40 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a  HH:MM:SS.FFFF..*
73d50 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e  * The HH, MM, an
73d60 64 20 53 53 20 6d 75 73 74 20 65 61 63 68 20 62  d SS must each b
73d70 65 20 65 78 61 63 74 6c 79 20 32 20 64 69 67 69  e exactly 2 digi
73d80 74 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63  ts.  The.** frac
73d90 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46  tional seconds F
73da0 46 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f  FFF can be one o
73db0 72 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a  r more digits..*
73dc0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66  *.** Return 1 if
73dd0 20 74 68 65 72 65 20 69 73 20 61 20 70 61 72 73   there is a pars
73de0 69 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20  ing error and 0 
73df0 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73  on success..*/.s
73e00 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 48  tatic int parseH
73e10 68 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72  hMmSs(const char
73e20 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d   *zDate, DateTim
73e30 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20  e *p){.  int h, 
73e40 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d  m, s;.  double m
73e50 73 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67  s = 0.0;.  if( g
73e60 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20  etDigits(zDate, 
73e70 32 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26  2, 0, 24, ':', &
73e80 68 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20  h, 2, 0, 59, 0, 
73e90 26 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65  &m)!=2 ){.    re
73ea0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44  turn 1;.  }.  zD
73eb0 61 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20  ate += 5;.  if( 
73ec0 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20  *zDate==':' ){. 
73ed0 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
73ee0 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44  if( getDigits(zD
73ef0 61 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30  ate, 2, 0, 59, 0
73f00 2c 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20  , &s)!=1 ){.    
73f10 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
73f20 7d 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32  }.    zDate += 2
73f30 3b 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65  ;.    if( *zDate
73f40 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65 33  =='.' && sqlite3
73f50 49 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31 5d  Isdigit(zDate[1]
73f60 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c  ) ){.      doubl
73f70 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b 0a  e rScale = 1.0;.
73f80 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20        zDate++;. 
73f90 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69       while( sqli
73fa0 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74  te3Isdigit(*zDat
73fb0 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73  e) ){.        ms
73fc0 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44   = ms*10.0 + *zD
73fd0 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  ate - '0';.     
73fe0 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e     rScale *= 10.
73ff0 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65  0;.        zDate
74000 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
74010 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a    ms /= rScale;.
74020 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
74030 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20     s = 0;.  }.  
74040 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a  p->validJD = 0;.
74050 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20    p->validHMS = 
74060 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20  1;.  p->h = h;. 
74070 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e   p->m = m;.  p->
74080 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66  s = s + ms;.  if
74090 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28  ( parseTimezone(
740a0 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75  zDate, p) ) retu
740b0 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64  rn 1;.  p->valid
740c0 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f  TZ = (p->tz!=0)?
740d0 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  1:0;.  return 0;
740e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
740f0 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44  t from YYYY-MM-D
74100 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75  D HH:MM:SS to ju
74110 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c  lian day.  We al
74120 77 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74  ways assume.** t
74130 68 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d  hat the YYYY-MM-
74140 44 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20  DD is according 
74150 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e  to the Gregorian
74160 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a   calendar..**.**
74170 20 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65   Reference:  Mee
74180 75 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74  us page 61.*/.st
74190 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
741a0 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29  eJD(DateTime *p)
741b0 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c  {.  int Y, M, D,
741c0 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a   A, B, X1, X2;..
741d0 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44    if( p->validJD
741e0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
741f0 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a   p->validYMD ){.
74200 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20      Y = p->Y;.  
74210 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20    M = p->M;.    
74220 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73  D = p->D;.  }els
74230 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b  e{.    Y = 2000;
74240 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73    /* If no YMD s
74250 70 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65  pecified, assume
74260 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a   2000-Jan-01 */.
74270 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44      M = 1;.    D
74280 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 1;.  }.  if( 
74290 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b  M<=2 ){.    Y--;
742a0 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20  .    M += 12;.  
742b0 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20  }.  A = Y/100;. 
742c0 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f   B = 2 - A + (A/
742d0 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35  4);.  X1 = 36525
742e0 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20  *(Y+4716)/100;. 
742f0 20 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b   X2 = 306001*(M+
74300 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69  1)/10000;.  p->i
74310 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  JD = (sqlite3_in
74320 74 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20  t64)((X1 + X2 + 
74330 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29  D + B - 1524.5 )
74340 20 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20   * 86400000);.  
74350 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a  p->validJD = 1;.
74360 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d    if( p->validHM
74370 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20  S ){.    p->iJD 
74380 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20  += p->h*3600000 
74390 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28  + p->m*60000 + (
743a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70  sqlite3_int64)(p
743b0 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69  ->s*1000);.    i
743c0 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b  f( p->validTZ ){
743d0 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d  .      p->iJD -=
743e0 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20   p->tz*60000;.  
743f0 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20      p->validYMD 
74400 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  = 0;.      p->va
74410 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20  lidHMS = 0;.    
74420 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30    p->validTZ = 0
74430 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
74440 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73  *.** Parse dates
74450 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a   of the form.**.
74460 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44  **     YYYY-MM-D
74470 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a  D HH:MM:SS.FFF.*
74480 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44  *     YYYY-MM-DD
74490 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20   HH:MM:SS.**    
744a0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
744b0 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d  M.**     YYYY-MM
744c0 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  -DD.**.** Write 
744d0 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20  the result into 
744e0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72  the DateTime str
744f0 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72  ucture and retur
74500 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73  n 0.** on succes
74510 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 69  s and 1 if the i
74520 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e  nput string is n
74530 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  ot a well-formed
74540 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61  .** date..*/.sta
74550 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 79  tic int parseYyy
74560 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72  yMmDd(const char
74570 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d   *zDate, DateTim
74580 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20  e *p){.  int Y, 
74590 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66  M, D, neg;..  if
745a0 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20  ( zDate[0]=='-' 
745b0 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a  ){.    zDate++;.
745c0 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d      neg = 1;.  }
745d0 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20  else{.    neg = 
745e0 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74  0;.  }.  if( get
745f0 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30  Digits(zDate,4,0
74600 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31  ,9999,'-',&Y,2,1
74610 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33  ,12,'-',&M,2,1,3
74620 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20  1,0,&D)!=3 ){.  
74630 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
74640 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20    zDate += 10;. 
74650 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
74660 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 7c  sspace(*zDate) |
74670 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61  | 'T'==*(u8*)zDa
74680 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d  te ){ zDate++; }
74690 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d  .  if( parseHhMm
746a0 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20  Ss(zDate, p)==0 
746b0 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74  ){.    /* We got
746c0 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d   the time */.  }
746d0 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d  else if( *zDate=
746e0 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c  =0 ){.    p->val
746f0 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c  idHMS = 0;.  }el
74700 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  se{.    return 1
74710 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64  ;.  }.  p->valid
74720 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c  JD = 0;.  p->val
74730 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e  idYMD = 1;.  p->
74740 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59  Y = neg ? -Y : Y
74750 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20  ;.  p->M = M;.  
74760 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20  p->D = D;.  if( 
74770 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20  p->validTZ ){.  
74780 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
74790 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
747a0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
747b0 20 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72   time to the cur
747c0 72 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74  rent time report
747d0 65 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f  ed by the VFS.*/
747e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
747f0 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e  DateTimeToCurren
74800 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  t(sqlite3_contex
74810 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65  t *context, Date
74820 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62  Time *p){.  doub
74830 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20  le r;.  sqlite3 
74840 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
74850 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
74860 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69  context);.  sqli
74870 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
74880 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a  (db->pVfs, &r);.
74890 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69    p->iJD = (sqli
748a0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34  te3_int64)(r*864
748b0 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a  00000.0 + 0.5);.
748c0 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31    p->validJD = 1
748d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
748e0 70 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20  pt to parse the 
748f0 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74  given string int
74900 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e  o a Julian Day N
74910 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a  umber.  Return.*
74920 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
74930 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  errors..**.** Th
74940 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
74950 61 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73  acceptable forms
74960 20 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73   for the input s
74970 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tring:.**.**    
74980 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
74990 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48  MM:SS.FFF  +/-HH
749a0 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44  :MM.**      DDDD
749b0 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77  .DD .**      now
749c0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69  .**.** In the fi
749d0 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f  rst form, the +/
749e0 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73  -HH:MM is always
749f0 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20   optional.  The 
74a00 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65  fractional.** se
74a10 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20  conds extension 
74a20 28 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20  (the ".FFF") is 
74a30 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73  optional.  The s
74a40 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a  econds portion.*
74a50 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73  * (":SS.FFF") is
74a60 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65   option.  The ye
74a70 61 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20  ar and date can 
74a80 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f  be omitted as lo
74a90 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69  ng.** as there i
74aa0 73 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e  s a time string.
74ab0 20 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e    The time strin
74ac0 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64  g can be omitted
74ad0 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74   as long.** as t
74ae0 68 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61  here is a year a
74af0 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74  nd date..*/.stat
74b00 69 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65  ic int parseDate
74b10 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65  OrTime(.  sqlite
74b20 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
74b30 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  xt, .  const cha
74b40 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74  r *zDate, .  Dat
74b50 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 66  eTime *p.){.  if
74b60 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28  ( parseYyyyMmDd(
74b70 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20  zDate,p)==0 ){. 
74b80 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
74b90 65 6c 73 65 20 69 66 28 20 70 61 72 73 65 48 68  else if( parseHh
74ba0 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d  MmSs(zDate, p)==
74bb0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
74bc0 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  0;.  }else if( s
74bd0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44  qlite3StrICmp(zD
74be0 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a  ate,"now")==0){.
74bf0 20 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54      setDateTimeT
74c00 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74  oCurrent(context
74c10 2c 20 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , p);.    return
74c20 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
74c30 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28  sqlite3IsNumber(
74c40 7a 44 61 74 65 2c 20 30 2c 20 53 51 4c 49 54 45  zDate, 0, SQLITE
74c50 5f 55 54 46 38 29 20 29 7b 0a 20 20 20 20 64 6f  _UTF8) ){.    do
74c60 75 62 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 56  uble r;.    getV
74c70 61 6c 75 65 28 7a 44 61 74 65 2c 20 26 72 29 3b  alue(zDate, &r);
74c80 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73  .    p->iJD = (s
74c90 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
74ca0 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35  86400000.0 + 0.5
74cb0 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a  );.    p->validJ
74cc0 44 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  D = 1;.    retur
74cd0 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
74ce0 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n 1;.}../*.** Co
74cf0 6d 70 75 74 65 20 74 68 65 20 59 65 61 72 2c 20  mpute the Year, 
74d00 4d 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66  Month, and Day f
74d10 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  rom the julian d
74d20 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  ay number..*/.st
74d30 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
74d40 65 59 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70  eYMD(DateTime *p
74d50 29 7b 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42  ){.  int Z, A, B
74d60 2c 20 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20  , C, D, E, X1;. 
74d70 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44   if( p->validYMD
74d80 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
74d90 20 21 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a   !p->validJD ){.
74da0 20 20 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b      p->Y = 2000;
74db0 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20  .    p->M = 1;. 
74dc0 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d     p->D = 1;.  }
74dd0 65 6c 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69  else{.    Z = (i
74de0 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33  nt)((p->iJD + 43
74df0 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30  200000)/86400000
74e00 29 3b 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 29  );.    A = (int)
74e10 28 28 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35  ((Z - 1867216.25
74e20 29 2f 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 20  )/36524.25);.   
74e30 20 41 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d   A = Z + 1 + A -
74e40 20 28 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20   (A/4);.    B = 
74e50 41 20 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20  A + 1524;.    C 
74e60 3d 20 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32  = (int)((B - 122
74e70 2e 31 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20  .1)/365.25);.   
74e80 20 44 20 3d 20 28 33 36 35 32 35 2a 43 29 2f 31   D = (36525*C)/1
74e90 30 30 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 74  00;.    E = (int
74ea0 29 28 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 29  )((B-D)/30.6001)
74eb0 3b 0a 20 20 20 20 58 31 20 3d 20 28 69 6e 74 29  ;.    X1 = (int)
74ec0 28 33 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20  (30.6001*E);.   
74ed0 20 70 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20   p->D = B - D - 
74ee0 58 31 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45  X1;.    p->M = E
74ef0 3c 31 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33  <14 ? E-1 : E-13
74f00 3b 0a 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e  ;.    p->Y = p->
74f10 4d 3e 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a  M>2 ? C - 4716 :
74f20 20 43 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20   C - 4715;.  }. 
74f30 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31   p->validYMD = 1
74f40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
74f50 74 65 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e  te the Hour, Min
74f60 75 74 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73  ute, and Seconds
74f70 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e   from the julian
74f80 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a   day number..*/.
74f90 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70  static void comp
74fa0 75 74 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20  uteHMS(DateTime 
74fb0 2a 70 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20  *p){.  int s;.  
74fc0 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20  if( p->validHMS 
74fd0 29 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70  ) return;.  comp
74fe0 75 74 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20  uteJD(p);.  s = 
74ff0 28 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20  (int)((p->iJD + 
75000 34 33 32 30 30 30 30 30 29 20 25 20 38 36 34 30  43200000) % 8640
75010 30 30 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20  0000);.  p->s = 
75020 73 2f 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20  s/1000.0;.  s = 
75030 28 69 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e  (int)p->s;.  p->
75040 73 20 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d  s -= s;.  p->h =
75050 20 73 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20   s/3600;.  s -= 
75060 70 2d 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e  p->h*3600;.  p->
75070 6d 20 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73  m = s/60;.  p->s
75080 20 2b 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b   += s - p->m*60;
75090 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d  .  p->validHMS =
750a0 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d   1;.}../*.** Com
750b0 70 75 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e  pute both YMD an
750c0 64 20 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20  d HMS.*/.static 
750d0 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f  void computeYMD_
750e0 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29  HMS(DateTime *p)
750f0 7b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70  {.  computeYMD(p
75100 29 3b 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 28  );.  computeHMS(
75110 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  p);.}../*.** Cle
75120 61 72 20 74 68 65 20 59 4d 44 20 61 6e 64 20 48  ar the YMD and H
75130 4d 53 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f  MS and the TZ.*/
75140 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65  .static void cle
75150 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74  arYMD_HMS_TZ(Dat
75160 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e  eTime *p){.  p->
75170 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20  validYMD = 0;.  
75180 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b  p->validHMS = 0;
75190 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20  .  p->validTZ = 
751a0 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  0;.}..#ifndef SQ
751b0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54  LITE_OMIT_LOCALT
751c0 49 4d 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  IME./*.** Comput
751d0 65 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65  e the difference
751e0 20 28 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64   (in millisecond
751f0 73 29 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f  s).** between lo
75200 63 61 6c 74 69 6d 65 20 61 6e 64 20 55 54 43 20  caltime and UTC 
75210 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20  (a.k.a. GMT).** 
75220 66 6f 72 20 74 68 65 20 74 69 6d 65 20 76 61 6c  for the time val
75230 75 65 20 70 20 77 68 65 72 65 20 70 20 69 73 20  ue p where p is 
75240 69 6e 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69  in UTC..*/.stati
75250 63 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  c sqlite3_int64 
75260 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28  localtimeOffset(
75270 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
75280 44 61 74 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20  DateTime x, y;. 
75290 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d   time_t t;.  x =
752a0 20 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d   *p;.  computeYM
752b0 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 28  D_HMS(&x);.  if(
752c0 20 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59   x.Y<1971 || x.Y
752d0 3e 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 78 2e  >=2038 ){.    x.
752e0 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 2e  Y = 2000;.    x.
752f0 4d 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d  M = 1;.    x.D =
75300 20 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b   1;.    x.h = 0;
75310 0a 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20  .    x.m = 0;.  
75320 20 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d    x.s = 0.0;.  }
75330 20 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20   else {.    int 
75340 73 20 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b 20  s = (int)(x.s + 
75350 30 2e 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20  0.5);.    x.s = 
75360 73 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20  s;.  }.  x.tz = 
75370 30 3b 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d  0;.  x.validJD =
75380 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28   0;.  computeJD(
75390 26 78 29 3b 0a 20 20 74 20 3d 20 78 2e 69 4a 44  &x);.  t = x.iJD
753a0 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36  /1000 - 21086676
753b0 2a 28 69 36 34 29 31 30 30 30 30 3b 0a 23 69 66  *(i64)10000;.#if
753c0 64 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49  def HAVE_LOCALTI
753d0 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72  ME_R.  {.    str
753e0 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20  uct tm sLocal;. 
753f0 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 26     localtime_r(&
75400 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 20 20  t, &sLocal);.   
75410 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.Y = sLocal.tm
75420 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20  _year + 1900;.  
75430 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.M = sLocal.t
75440 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79  m_mon + 1;.    y
75450 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d  .D = sLocal.tm_m
75460 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73  day;.    y.h = s
75470 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20  Local.tm_hour;. 
75480 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e     y.m = sLocal.
75490 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20  tm_min;.    y.s 
754a0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b  = sLocal.tm_sec;
754b0 0a 20 20 7d 0a 23 65 6c 69 66 20 64 65 66 69 6e  .  }.#elif defin
754c0 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ed(HAVE_LOCALTIM
754d0 45 5f 53 29 0a 20 20 7b 0a 20 20 20 20 73 74 72  E_S).  {.    str
754e0 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20  uct tm sLocal;. 
754f0 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26     localtime_s(&
75500 73 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20  sLocal, &t);.   
75510 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.Y = sLocal.tm
75520 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20  _year + 1900;.  
75530 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.M = sLocal.t
75540 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79  m_mon + 1;.    y
75550 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d  .D = sLocal.tm_m
75560 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73  day;.    y.h = s
75570 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20  Local.tm_hour;. 
75580 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e     y.m = sLocal.
75590 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20  tm_min;.    y.s 
755a0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b  = sLocal.tm_sec;
755b0 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20  .  }.#else.  {. 
755c0 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54     struct tm *pT
755d0 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  m;.    sqlite3_m
755e0 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
755f0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
75600 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
75610 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70  _MASTER));.    p
75620 54 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26  Tm = localtime(&
75630 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54  t);.    y.Y = pT
75640 6d 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30  m->tm_year + 190
75650 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d  0;.    y.M = pTm
75660 2d 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20  ->tm_mon + 1;.  
75670 20 20 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f    y.D = pTm->tm_
75680 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20  mday;.    y.h = 
75690 70 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20  pTm->tm_hour;.  
756a0 20 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f    y.m = pTm->tm_
756b0 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70  min;.    y.s = p
756c0 54 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20  Tm->tm_sec;.    
756d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
756e0 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
756f0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
75700 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
75710 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ));.  }.#endif. 
75720 20 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b   y.validYMD = 1;
75730 0a 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20  .  y.validHMS = 
75740 31 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d  1;.  y.validJD =
75750 20 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20   0;.  y.validTZ 
75760 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44  = 0;.  computeJD
75770 28 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79  (&y);.  return y
75780 2e 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a  .iJD - x.iJD;.}.
75790 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
757a0 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20  _OMIT_LOCALTIME 
757b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73  */../*.** Proces
757c0 73 20 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20  s a modifier to 
757d0 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d  a date-time stam
757e0 70 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72  p.  The modifier
757f0 73 20 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c  s are.** as foll
75800 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e  ows:.**.**     N
75810 4e 4e 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e  NN days.**     N
75820 4e 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20  NN hours.**     
75830 4e 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20  NNN minutes.**  
75840 20 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f     NNN.NNNN seco
75850 6e 64 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d  nds.**     NNN m
75860 6f 6e 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e  onths.**     NNN
75870 20 79 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74   years.**     st
75880 61 72 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20  art of month.** 
75890 20 20 20 20 73 74 61 72 74 20 6f 66 20 79 65 61      start of yea
758a0 72 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f  r.**     start o
758b0 66 20 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74  f week.**     st
758c0 61 72 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20  art of day.**   
758d0 20 20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20    weekday N.**  
758e0 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20     unixepoch.** 
758f0 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a      localtime.**
75900 20 20 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52       utc.**.** R
75910 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65  eturn 0 on succe
75920 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72  ss and 1 if ther
75930 65 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66  e is any kind of
75940 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69   error..*/.stati
75950 63 20 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66  c int parseModif
75960 69 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ier(const char *
75970 7a 4d 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a  zMod, DateTime *
75980 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31  p){.  int rc = 1
75990 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75  ;.  int n;.  dou
759a0 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a  ble r;.  char *z
759b0 2c 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20  , zBuf[30];.  z 
759c0 3d 20 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d  = zBuf;.  for(n=
759d0 30 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a  0; n<ArraySize(z
759e0 42 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e  Buf)-1 && zMod[n
759f0 5d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e  ]; n++){.    z[n
75a00 5d 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65  ] = (char)sqlite
75a10 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75  3UpperToLower[(u
75a20 38 29 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a  8)zMod[n]];.  }.
75a30 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77    z[n] = 0;.  sw
75a40 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69  itch( z[0] ){.#i
75a50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
75a60 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20  T_LOCALTIME.    
75a70 63 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20  case 'l': {.    
75a80 20 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d    /*    localtim
75a90 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  e.      **.     
75aa0 20 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65   ** Assuming the
75ab0 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61   current time va
75ac0 6c 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e  lue is UTC (a.k.
75ad0 61 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69  a. GMT), shift i
75ae0 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68  t to.      ** sh
75af0 6f 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20  ow local time.. 
75b00 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
75b10 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63  ( strcmp(z, "loc
75b20 61 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20  altime")==0 ){. 
75b30 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
75b40 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
75b50 69 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65  iJD += localtime
75b60 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20  Offset(p);.     
75b70 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f     clearYMD_HMS_
75b80 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72  TZ(p);.        r
75b90 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 0;.      }. 
75ba0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
75bb0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73  }.#endif.    cas
75bc0 65 20 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f  e 'u': {.      /
75bd0 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e  *.      **    un
75be0 69 78 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a  ixepoch.      **
75bf0 0a 20 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20  .      ** Treat 
75c00 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
75c10 65 20 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74  e of p->iJD as t
75c20 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20  he number of.   
75c30 20 20 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69     ** seconds si
75c40 6e 63 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65  nce 1970.  Conve
75c50 72 74 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c  rt to a real jul
75c60 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a  ian day number..
75c70 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
75c80 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e  f( strcmp(z, "un
75c90 69 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20  ixepoch")==0 && 
75ca0 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20  p->validJD ){.  
75cb0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 70        p->iJD = p
75cc0 2d 3e 69 4a 44 2f 38 36 34 30 30 20 2b 20 32 31  ->iJD/86400 + 21
75cd0 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30  086676*(i64)1000
75ce0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c  0000;.        cl
75cf0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
75d00 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
75d10 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65  ;.      }.#ifnde
75d20 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
75d30 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 6c  CALTIME.      el
75d40 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c  se if( strcmp(z,
75d50 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20   "utc")==0 ){.  
75d60 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
75d70 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20 20  t64 c1;.        
75d80 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
75d90 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c        c1 = local
75da0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20  timeOffset(p);. 
75db0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d         p->iJD -=
75dc0 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65   c1;.        cle
75dd0 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
75de0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
75df0 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d  += c1 - localtim
75e00 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20  eOffset(p);.    
75e10 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
75e20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
75e30 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
75e40 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20    case 'w': {.  
75e50 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20      /*.      ** 
75e60 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20     weekday N.   
75e70 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d     **.      ** M
75e80 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20  ove the date to 
75e90 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e  the same time on
75ea0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72   the next occurr
75eb0 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a  ence of.      **
75ec0 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65   weekday N where
75ed0 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d   0==Sunday, 1==M
75ee0 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f  onday, and so fo
75ef0 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20  rth.  If the.   
75f00 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c     ** date is al
75f10 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70  ready on the app
75f20 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79  ropriate weekday
75f30 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  , this is a no-o
75f40 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  p..      */.    
75f50 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c    if( strncmp(z,
75f60 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d   "weekday ", 8)=
75f70 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26  =0 && getValue(&
75f80 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20  z[8],&r)>0.     
75f90 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
75fa0 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20  n=(int)r)==r && 
75fb0 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a 20  n>=0 && r<7 ){. 
75fc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
75fd0 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 20  nt64 Z;.        
75fe0 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70  computeYMD_HMS(p
75ff0 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61  );.        p->va
76000 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20  lidTZ = 0;.     
76010 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20     p->validJD = 
76020 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  0;.        compu
76030 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20  teJD(p);.       
76040 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b 20   Z = ((p->iJD + 
76050 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 30  129600000)/86400
76060 30 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20  000) % 7;.      
76070 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d 3d    if( Z>n ) Z -=
76080 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   7;.        p->i
76090 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 36  JD += (n - Z)*86
760a0 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20  400000;.        
760b0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
760c0 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  p);.        rc =
760d0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
760e0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
760f0 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20     case 's': {. 
76100 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a       /*.      **
76110 20 20 20 20 73 74 61 72 74 20 6f 66 20 54 54 54      start of TTT
76120 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  TT.      **.    
76130 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61    ** Move the da
76140 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20  te backwards to 
76150 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
76160 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 79   the current day
76170 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f  ,.      ** or mo
76180 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20  nth or year..   
76190 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
761a0 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72  strncmp(z, "star
761b0 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20  t of ", 9)!=0 ) 
761c0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b  break;.      z +
761d0 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75  = 9;.      compu
761e0 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20  teYMD(p);.      
761f0 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b  p->validHMS = 1;
76200 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d  .      p->h = p-
76210 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  >m = 0;.      p-
76220 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20  >s = 0.0;.      
76230 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
76240 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44        p->validJD
76250 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
76260 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22  strcmp(z,"month"
76270 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
76280 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20  p->D = 1;.      
76290 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
762a0 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70  }else if( strcmp
762b0 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b  (z,"year")==0 ){
762c0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
762d0 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20  YMD(p);.        
762e0 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20  p->M = 1;.      
762f0 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20    p->D = 1;.    
76300 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
76310 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
76320 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29  mp(z,"day")==0 )
76330 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  {.        rc = 0
76340 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
76350 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
76360 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63   case '+':.    c
76370 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73  ase '-':.    cas
76380 65 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20  e '0':.    case 
76390 27 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32  '1':.    case '2
763a0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a  ':.    case '3':
763b0 0a 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20  .    case '4':. 
763c0 20 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20     case '5':.   
763d0 20 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63   case '6':.    c
763e0 61 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73  ase '7':.    cas
763f0 65 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20  e '8':.    case 
76400 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75  '9': {.      dou
76410 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b 0a 20 20  ble rRounder;.  
76420 20 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75 65      n = getValue
76430 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 61  (z, &r);.      a
76440 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20  ssert( n>=1 );. 
76450 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27       if( z[n]=='
76460 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  :' ){.        /*
76470 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 74   A modifier of t
76480 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a  he form (+|-)HH:
76490 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 28  MM:SS.FFF adds (
764a0 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74 68  or subtracts) th
764b0 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 65  e.        ** spe
764c0 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66  cified number of
764d0 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c   hours, minutes,
764e0 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72   seconds, and fr
764f0 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73  actional seconds
76500 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  .        ** to t
76510 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e  he time.  The ".
76520 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 74  FFF" may be omit
76530 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e 46  ted.  The ":SS.F
76540 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 20  FF" may be.     
76550 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20     ** omitted.. 
76560 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
76570 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32    const char *z2
76580 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 61   = z;.        Da
76590 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 20  teTime tx;.     
765a0 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
765b0 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 66   day;.        if
765c0 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69  ( !sqlite3Isdigi
765d0 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a 20  t(*z2) ) z2++;. 
765e0 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 74         memset(&t
765f0 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78 29  x, 0, sizeof(tx)
76600 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
76610 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 26  arseHhMmSs(z2, &
76620 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  tx) ) break;.   
76630 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26       computeJD(&
76640 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78 2e  tx);.        tx.
76650 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 3b  iJD -= 43200000;
76660 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20 74  .        day = t
76670 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b 0a  x.iJD/86400000;.
76680 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d          tx.iJD -
76690 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b 0a  = day*86400000;.
766a0 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d          if( z[0]
766b0 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20 3d  =='-' ) tx.iJD =
766c0 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20   -tx.iJD;.      
766d0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
766e0 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44          clearYMD
766f0 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20  _HMS_TZ(p);.    
76700 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74 78      p->iJD += tx
76710 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72 63  .iJD;.        rc
76720 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72   = 0;.        br
76730 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
76740 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20 20     z += n;.     
76750 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
76760 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b  sspace(*z) ) z++
76770 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69  ;.      n = sqli
76780 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a  te3Strlen30(z);.
76790 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c        if( n>10 |
767a0 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20  | n<3 ) break;. 
767b0 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d       if( z[n-1]=
767c0 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d  ='s' ){ z[n-1] =
767d0 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20   0; n--; }.     
767e0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
767f0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
76800 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72 3c     rRounder = r<
76810 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35 3b  0 ? -0.5 : +0.5;
76820 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 20  .      if( n==3 
76830 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79  && strcmp(z,"day
76840 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
76850 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
76860 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34  te3_int64)(r*864
76870 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64  00000.0 + rRound
76880 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  er);.      }else
76890 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72   if( n==4 && str
768a0 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30  cmp(z,"hour")==0
768b0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
768c0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69  JD += (sqlite3_i
768d0 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30  nt64)(r*(8640000
768e0 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f 75  0.0/24.0) + rRou
768f0 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
76900 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73  se if( n==6 && s
76910 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 22  trcmp(z,"minute"
76920 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
76930 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
76940 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34  e3_int64)(r*(864
76950 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30  00000.0/(24.0*60
76960 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29  .0)) + rRounder)
76970 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
76980 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70  ( n==6 && strcmp
76990 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20  (z,"second")==0 
769a0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  ){.        p->iJ
769b0 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  D += (sqlite3_in
769c0 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 30  t64)(r*(86400000
769d0 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30  .0/(24.0*60.0*60
769e0 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29  .0)) + rRounder)
769f0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
76a00 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d 70  ( n==5 && strcmp
76a10 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29  (z,"month")==0 )
76a20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 2c  {.        int x,
76a30 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   y;.        comp
76a40 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20  uteYMD_HMS(p);. 
76a50 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 28         p->M += (
76a60 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 78  int)r;.        x
76a70 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e   = p->M>0 ? (p->
76a80 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d  M-1)/12 : (p->M-
76a90 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20  12)/12;.        
76aa0 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20  p->Y += x;.     
76ab0 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b     p->M -= x*12;
76ac0 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  .        p->vali
76ad0 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dJD = 0;.       
76ae0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
76af0 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74 29         y = (int)
76b00 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79  r;.        if( y
76b10 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=r ){.         
76b20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
76b30 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20  te3_int64)((r - 
76b40 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 30  y)*30.0*86400000
76b50 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  .0 + rRounder);.
76b60 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
76b70 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26  }else if( n==4 &
76b80 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72  & strcmp(z,"year
76b90 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
76ba0 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72 3b   int y = (int)r;
76bb0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
76bc0 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20  YMD_HMS(p);.    
76bd0 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a 20      p->Y += y;. 
76be0 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a         p->validJ
76bf0 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63  D = 0;.        c
76c00 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20  omputeJD(p);.   
76c10 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b       if( y!=r ){
76c20 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  .          p->iJ
76c30 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  D += (sqlite3_in
76c40 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36 35  t64)((r - y)*365
76c50 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20  .0*86400000.0 + 
76c60 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
76c70 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
76c80 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31  {.        rc = 1
76c90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
76ca0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
76cb0 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  p);.      break;
76cc0 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
76cd0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61  lt: {.      brea
76ce0 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  k;.    }.  }.  r
76cf0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
76d00 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 20  ** Process time 
76d10 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
76d20 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20  ts.  argv[0] is 
76d30 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d  a date-time stam
76d40 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 6e  p..** argv[1] an
76d50 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  d following are 
76d60 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 73  modifiers.  Pars
76d70 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 77  e them all and w
76d80 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75  rite.** the resu
76d90 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f 20  lting time into 
76da0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72  the DateTime str
76db0 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 72  ucture p.  Retur
76dc0 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73  n 0.** on succes
76dd0 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65  s and 1 if there
76de0 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 2e   are any errors.
76df0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
76e00 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 74  are zero paramet
76e10 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 67  ers (if even arg
76e20 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e 65  v[0] is undefine
76e30 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 6d  d).** then assum
76e40 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75  e a default valu
76e50 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 61  e of "now" for a
76e60 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69  rgv[0]..*/.stati
76e70 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 20  c int isDate(.  
76e80 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
76e90 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74  *context, .  int
76ea0 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65   argc, .  sqlite
76eb0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c 20  3_value **argv, 
76ec0 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29  .  DateTime *p.)
76ed0 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e  {.  int i;.  con
76ee0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
76ef0 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70 65   *z;.  int eType
76f00 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ;.  memset(p, 0,
76f10 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20   sizeof(*p));.  
76f20 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a 20  if( argc==0 ){. 
76f30 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f     setDateTimeTo
76f40 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c  Current(context,
76f50 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   p);.  }else if(
76f60 20 28 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65   (eType = sqlite
76f70 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
76f80 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 45 5f 46  v[0]))==SQLITE_F
76f90 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 20 20 20  LOAT.           
76fa0 20 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65          || eType
76fb0 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  ==SQLITE_INTEGER
76fc0 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d   ){.    p->iJD =
76fd0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
76fe0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64  (sqlite3_value_d
76ff0 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 2a 38  ouble(argv[0])*8
77000 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29  6400000.0 + 0.5)
77010 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44  ;.    p->validJD
77020 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
77030 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76     z = sqlite3_v
77040 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
77050 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 7c  ]);.    if( !z |
77060 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d  | parseDateOrTim
77070 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72  e(context, (char
77080 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 20  *)z, p) ){.     
77090 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
770a0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20  .  }.  for(i=1; 
770b0 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20  i<argc; i++){.  
770c0 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69 74    if( (z = sqlit
770d0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
770e0 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70 61  gv[i]))==0 || pa
770f0 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68 61  rseModifier((cha
77100 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20  r*)z, p) ){.    
77110 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
77120 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
77130 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ;.}.../*.** The 
77140 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
77150 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  es implement the
77160 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61 6e   various date an
77170 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 73  d time functions
77180 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  .** of SQLite..*
77190 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c 69  /../*.**    juli
771a0 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 4e  anday( TIMESTRIN
771b0 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e  G, MOD, MOD, ...
771c0 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ).**.** Return t
771d0 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  he julian day nu
771e0 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74 65  mber of the date
771f0 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
77200 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 73  e arguments.*/.s
77210 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69 61  tatic void julia
77220 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c 69  ndayFunc(.  sqli
77230 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
77240 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
77250 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
77260 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61  e **argv.){.  Da
77270 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20  teTime x;.  if( 
77280 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20  isDate(context, 
77290 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d  argc, argv, &x)=
772a0 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74  =0 ){.    comput
772b0 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c  eJD(&x);.    sql
772c0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
772d0 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 2e 69 4a  le(context, x.iJ
772e0 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20  D/86400000.0);. 
772f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64   }.}../*.**    d
77300 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54 52  atetime( TIMESTR
77310 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e  ING, MOD, MOD, .
77320 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ..).**.** Return
77330 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
77340 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76  M:SS.*/.static v
77350 6f 69 64 20 64 61 74 65 74 69 6d 65 46 75 6e 63  oid datetimeFunc
77360 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
77370 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
77380 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
77390 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
773a0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
773b0 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63  ;.  if( isDate(c
773c0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
773d0 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20  gv, &x)==0 ){.  
773e0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
773f0 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44  ;.    computeYMD
77400 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71  _HMS(&x);.    sq
77410 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
77420 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
77430 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30  f, "%04d-%02d-%0
77440 32 64 20 25 30 32 64 3a 25 30 32 64 3a 25 30 32  2d %02d:%02d:%02
77450 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d",.            
77460 20 20 20 20 20 20 20 20 20 78 2e 59 2c 20 78 2e           x.Y, x.
77470 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e 6d  M, x.D, x.h, x.m
77480 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 3b 0a 20  , (int)(x.s));. 
77490 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
774a0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
774b0 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zBuf, -1, SQLITE
774c0 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
774d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 74 69 6d  .}../*.**    tim
774e0 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d  e( TIMESTRING, M
774f0 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a  OD, MOD, ...).**
77500 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 3a 4d 4d  .** Return HH:MM
77510 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  :SS.*/.static vo
77520 69 64 20 74 69 6d 65 46 75 6e 63 28 0a 20 20 73  id timeFunc(.  s
77530 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
77540 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
77550 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
77560 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
77570 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69   DateTime x;.  i
77580 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78  f( isDate(contex
77590 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26  t, argc, argv, &
775a0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61  x)==0 ){.    cha
775b0 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20  r zBuf[100];.   
775c0 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29 3b   computeHMS(&x);
775d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
775e0 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75  rintf(sizeof(zBu
775f0 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64 3a  f), zBuf, "%02d:
77600 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68 2c  %02d:%02d", x.h,
77610 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29 3b   x.m, (int)x.s);
77620 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
77630 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
77640 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49  , zBuf, -1, SQLI
77650 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
77660 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64   }.}../*.**    d
77670 61 74 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c  ate( TIMESTRING,
77680 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a   MOD, MOD, ...).
77690 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59  **.** Return YYY
776a0 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 61 74 69  Y-MM-DD.*/.stati
776b0 63 20 76 6f 69 64 20 64 61 74 65 46 75 6e 63 28  c void dateFunc(
776c0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
776d0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
776e0 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
776f0 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
77700 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b  ){.  DateTime x;
77710 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f  .  if( isDate(co
77720 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
77730 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20  v, &x)==0 ){.   
77740 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b   char zBuf[100];
77750 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28  .    computeYMD(
77760 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  &x);.    sqlite3
77770 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
77780 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25  (zBuf), zBuf, "%
77790 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c 20  04d-%02d-%02d", 
777a0 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b 0a  x.Y, x.M, x.D);.
777b0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
777c0 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
777d0 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54   zBuf, -1, SQLIT
777e0 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
777f0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73 74  }.}../*.**    st
77800 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c 20  rftime( FORMAT, 
77810 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
77820 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
77830 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67   Return a string
77840 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46 4f   described by FO
77850 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69 6f  RMAT.  Conversio
77860 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ns as follows:.*
77870 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20 6f  *.**   %d  day o
77880 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66 20  f month.**   %f 
77890 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 73   ** fractional s
778a0 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a 2a  econds  SS.SSS.*
778b0 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30 2d  *   %H  hour 00-
778c0 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79 20  24.**   %j  day 
778d0 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36 0a  of year 000-366.
778e0 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c 69  **   %J  ** Juli
778f0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a 2a  an day number.**
77900 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31 2d     %m  month 01-
77910 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e 75  12.**   %M  minu
77920 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 73  te 00-59.**   %s
77930 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20    seconds since 
77940 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20 20  1970-01-01.**   
77950 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d 35  %S  seconds 00-5
77960 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20 6f  9.**   %w  day o
77970 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e 64  f week 0-6  sund
77980 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20 77  ay==0.**   %W  w
77990 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d 35  eek of year 00-5
779a0 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72 20  3.**   %Y  year 
779b0 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20 25  0000-9999.**   %
779c0 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20 76  %  %.*/.static v
779d0 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e 63  oid strftimeFunc
779e0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
779f0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
77a00 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
77a10 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
77a20 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
77a30 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 73 69 7a  ;.  u64 n;.  siz
77a40 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 61 72 20  e_t i,j;.  char 
77a50 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *z;.  sqlite3 *d
77a60 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b;.  const char 
77a70 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 63  *zFmt = (const c
77a80 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
77a90 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
77aa0 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30  ;.  char zBuf[10
77ab0 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 3d  0];.  if( zFmt==
77ac0 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e 74  0 || isDate(cont
77ad0 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72 67  ext, argc-1, arg
77ae0 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75 72  v+1, &x) ) retur
77af0 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  n;.  db = sqlite
77b00 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
77b10 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
77b20 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46  for(i=0, n=1; zF
77b30 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29  mt[i]; i++, n++)
77b40 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69  {.    if( zFmt[i
77b50 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20  ]=='%' ){.      
77b60 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31  switch( zFmt[i+1
77b70 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  ] ){.        cas
77b80 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 63  e 'd':.        c
77b90 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 20  ase 'H':.       
77ba0 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 20   case 'm':.     
77bb0 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 20     case 'M':.   
77bc0 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a 20       case 'S':. 
77bd0 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 3a         case 'W':
77be0 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a  .          n++;.
77bf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c            /* fal
77c00 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20  l thru */.      
77c10 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 20    case 'w':.    
77c20 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 20      case '%':.  
77c30 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
77c40 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a         case 'f':
77c50 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  .          n += 
77c60 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  8;.          bre
77c70 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
77c80 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'j':.          
77c90 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20  n += 3;.        
77ca0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
77cb0 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 20   case 'Y':.     
77cc0 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20       n += 8;.   
77cd0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
77ce0 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 0a        case 's':.
77cf0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27          case 'J'
77d00 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  :.          n +=
77d10 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62   50;.          b
77d20 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65  reak;.        de
77d30 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 20  fault:.         
77d40 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 52   return;  /* ERR
77d50 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e 55  OR.  return a NU
77d60 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  LL */.      }.  
77d70 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20      i++;.    }. 
77d80 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e   }.  testcase( n
77d90 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31  ==sizeof(zBuf)-1
77da0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
77db0 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 20  n==sizeof(zBuf) 
77dc0 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
77dd0 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69  ==(u64)db->aLimi
77de0 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
77df0 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 74 65  ENGTH]+1 );.  te
77e00 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29  stcase( n==(u64)
77e10 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
77e20 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
77e30 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f  );.  if( n<sizeo
77e40 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 7a  f(zBuf) ){.    z
77e50 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 65   = zBuf;.  }else
77e60 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d 3e   if( n>(u64)db->
77e70 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
77e80 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
77e90 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
77ea0 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63  t_error_toobig(c
77eb0 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74  ontext);.    ret
77ec0 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  urn;.  }else{.  
77ed0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 4d    z = sqlite3DbM
77ee0 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e  allocRaw(db, (in
77ef0 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d  t)n);.    if( z=
77f00 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
77f10 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
77f20 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
77f30 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
77f40 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75     }.  }.  compu
77f50 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70  teJD(&x);.  comp
77f60 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a  uteYMD_HMS(&x);.
77f70 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d    for(i=j=0; zFm
77f80 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  t[i]; i++){.    
77f90 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27  if( zFmt[i]!='%'
77fa0 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d   ){.      z[j++]
77fb0 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20   = zFmt[i];.    
77fc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b  }else{.      i++
77fd0 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20  ;.      switch( 
77fe0 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20  zFmt[i] ){.     
77ff0 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73 71     case 'd':  sq
78000 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
78010 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78  , &z[j],"%02d",x
78020 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b  .D); j+=2; break
78030 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
78040 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  f': {.          
78050 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a  double s = x.s;.
78060 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 3e            if( s>
78070 35 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e  59.999 ) s = 59.
78080 39 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73  999;.          s
78090 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
780a0 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66  7, &z[j],"%06.3f
780b0 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20  ", s);.         
780c0 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72   j += sqlite3Str
780d0 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20  len30(&z[j]);.  
780e0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
780f0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
78100 20 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c 69   case 'H':  sqli
78110 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20  te3_snprintf(3, 
78120 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 68  &z[j],"%02d",x.h
78130 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a  ); j+=2; break;.
78140 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27          case 'W'
78150 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 2a  : /* Fall thru *
78160 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  /.        case '
78170 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  j': {.          
78180 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20 20  int nDay;       
78190 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
781a0 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 31 73  of days since 1s
781b0 74 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a 2f  t day of year */
781c0 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 65 54  .          DateT
781d0 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20 20  ime y = x;.     
781e0 20 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d       y.validJD =
781f0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e   0;.          y.
78200 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  M = 1;.         
78210 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20   y.D = 1;.      
78220 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79      computeJD(&y
78230 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 61  );.          nDa
78240 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69 4a 44  y = (int)((x.iJD
78250 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29  -y.iJD+43200000)
78260 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20  /86400000);.    
78270 20 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69        if( zFmt[i
78280 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 20  ]=='W' ){.      
78290 20 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 20        int wd;   
782a0 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54  /* 0=Monday, 1=T
782b0 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75  uesday, ... 6=Su
782c0 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 20  nday */.        
782d0 20 20 20 20 77 64 20 3d 20 28 69 6e 74 29 28 28      wd = (int)((
782e0 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29  (x.iJD+43200000)
782f0 2f 38 36 34 30 30 30 30 30 29 25 37 29 3b 0a 20  /86400000)%7);. 
78300 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
78310 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26  e3_snprintf(3, &
78320 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44 61  z[j],"%02d",(nDa
78330 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20 20  y+7-wd)/7);.    
78340 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b 0a          j += 2;.
78350 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
78360 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
78370 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 34 2c  ite3_snprintf(4,
78380 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e 44   &z[j],"%03d",nD
78390 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  ay+1);.         
783a0 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20 20     j += 3;.     
783b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
783c0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
783d0 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  }.        case '
783e0 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  J': {.          
783f0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
78400 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 36  (20, &z[j],"%.16
78410 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30 30  g",x.iJD/8640000
78420 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  0.0);.          
78430 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  j+=sqlite3Strlen
78440 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20  30(&z[j]);.     
78450 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
78460 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61      }.        ca
78470 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 33  se 'm':  sqlite3
78480 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b  _snprintf(3, &z[
78490 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b 20  j],"%02d",x.M); 
784a0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20  j+=2; break;.   
784b0 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 20       case 'M':  
784c0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
784d0 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  (3, &z[j],"%02d"
784e0 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65  ,x.m); j+=2; bre
784f0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
78500 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 20   's': {.        
78510 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
78520 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 64 22  tf(30,&z[j],"%d"
78530 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
78540 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e               (in
78550 74 29 28 78 2e 69 4a 44 2f 31 30 30 30 2e 30 20  t)(x.iJD/1000.0 
78560 2d 20 32 31 30 38 36 36 37 36 30 30 30 30 2e 30  - 210866760000.0
78570 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20  ));.          j 
78580 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
78590 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20  30(&z[j]);.     
785a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
785b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61      }.        ca
785c0 73 65 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33  se 'S':  sqlite3
785d0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a  _snprintf(3,&z[j
785e0 5d 2c 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e  ],"%02d",(int)x.
785f0 73 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b  s); j+=2; break;
78600 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 77  .        case 'w
78610 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ': {.          z
78620 5b 6a 2b 2b 5d 20 3d 20 28 63 68 61 72 29 28 28  [j++] = (char)((
78630 28 78 2e 69 4a 44 2b 31 32 39 36 30 30 30 30 30  (x.iJD+129600000
78640 29 2f 38 36 34 30 30 30 30 30 29 20 25 20 37 29  )/86400000) % 7)
78650 20 2b 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20   + '0';.        
78660 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
78670 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20   }.        case 
78680 27 59 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  'Y': {.         
78690 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
786a0 66 28 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22  f(5,&z[j],"%04d"
786b0 2c 78 2e 59 29 3b 20 6a 2b 3d 73 71 6c 69 74 65  ,x.Y); j+=sqlite
786c0 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29  3Strlen30(&z[j])
786d0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
786e0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
786f0 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20       default:   
78700 7a 5b 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72  z[j++] = '%'; br
78710 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
78720 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20   }.  }.  z[j] = 
78730 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  0;.  sqlite3_res
78740 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
78750 2c 20 7a 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20  , z, -1,.       
78760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
78770 3d 3d 7a 42 75 66 20 3f 20 53 51 4c 49 54 45 5f  ==zBuf ? SQLITE_
78780 54 52 41 4e 53 49 45 4e 54 20 3a 20 53 51 4c 49  TRANSIENT : SQLI
78790 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a  TE_DYNAMIC);.}..
787a0 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69  /*.** current_ti
787b0 6d 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  me().**.** This 
787c0 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
787d0 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20   the same value 
787e0 61 73 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a  as time('now')..
787f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
78800 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69  timeFunc(.  sqli
78810 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
78820 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
78830 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
78840 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a  alue **NotUsed2.
78850 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
78860 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
78870 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 74 69 6d  NotUsed2);.  tim
78880 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30  eFunc(context, 0
78890 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63  , 0);.}../*.** c
788a0 75 72 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a 2a  urrent_date().**
788b0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
788c0 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61  n returns the sa
788d0 6d 65 20 76 61 6c 75 65 20 61 73 20 64 61 74 65  me value as date
788e0 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74  ('now')..*/.stat
788f0 69 63 20 76 6f 69 64 20 63 64 61 74 65 46 75 6e  ic void cdateFun
78900 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
78910 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
78920 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20   int NotUsed,.  
78930 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
78940 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e  NotUsed2.){.  UN
78950 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
78960 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
78970 32 29 3b 0a 20 20 64 61 74 65 46 75 6e 63 28 63  2);.  dateFunc(c
78980 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d  ontext, 0, 0);.}
78990 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f  ../*.** current_
789a0 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a  timestamp().**.*
789b0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
789c0 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65  returns the same
789d0 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 74 69   value as dateti
789e0 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74  me('now')..*/.st
789f0 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73  atic void ctimes
78a00 74 61 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69  tampFunc(.  sqli
78a10 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
78a20 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
78a30 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
78a40 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a  alue **NotUsed2.
78a50 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
78a60 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
78a70 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74  NotUsed2);.  dat
78a80 65 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78  etimeFunc(contex
78a90 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64  t, 0, 0);.}.#end
78aa0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
78ab0 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54  QLITE_OMIT_DATET
78ac0 49 4d 45 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23  IME_FUNCS) */..#
78ad0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
78ae0 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53  T_DATETIME_FUNCS
78af0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69  ./*.** If the li
78b00 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65  brary is compile
78b10 64 20 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 75  d to omit the fu
78b20 6c 6c 2d 73 63 61 6c 65 20 64 61 74 65 20 61 6e  ll-scale date an
78b30 64 20 74 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69  d time.** handli
78b40 6e 67 20 28 74 6f 20 67 65 74 20 61 20 73 6d 61  ng (to get a sma
78b50 6c 6c 65 72 20 62 69 6e 61 72 79 29 2c 20 74 68  ller binary), th
78b60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69  e following mini
78b70 6d 61 6c 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f  mal version.** o
78b80 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20  f the functions 
78b90 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20  current_time(), 
78ba0 63 75 72 72 65 6e 74 5f 64 61 74 65 28 29 20 61  current_date() a
78bb0 6e 64 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73  nd current_times
78bc0 74 61 6d 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e  tamp().** are in
78bd0 63 6c 75 64 65 64 20 69 6e 73 74 65 61 64 2e 20  cluded instead. 
78be0 54 68 69 73 20 69 73 20 74 6f 20 73 75 70 70 6f  This is to suppo
78bf0 72 74 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72  rt column declar
78c00 61 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69  ations that.** i
78c10 6e 63 6c 75 64 65 20 22 44 45 46 41 55 4c 54 20  nclude "DEFAULT 
78c20 43 55 52 52 45 4e 54 5f 54 49 4d 45 22 20 65 74  CURRENT_TIME" et
78c30 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  c..**.** This fu
78c40 6e 63 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20  nction uses the 
78c50 43 2d 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69  C-library functi
78c60 6f 6e 73 20 74 69 6d 65 28 29 2c 20 67 6d 74 69  ons time(), gmti
78c70 6d 65 28 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66  me().** and strf
78c80 74 69 6d 65 28 29 2e 20 54 68 65 20 66 6f 72 6d  time(). The form
78c90 61 74 20 73 74 72 69 6e 67 20 74 6f 20 70 61 73  at string to pas
78ca0 73 20 74 6f 20 73 74 72 66 74 69 6d 65 28 29 20  s to strftime() 
78cb0 69 73 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61  is supplied.** a
78cc0 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 61 20  s the user-data 
78cd0 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  for the function
78ce0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
78cf0 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63   currentTimeFunc
78d00 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
78d10 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
78d20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
78d30 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
78d40 0a 29 7b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a  .){.  time_t t;.
78d50 20 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20    char *zFormat 
78d60 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
78d70 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74  3_user_data(cont
78d80 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20  ext);.  sqlite3 
78d90 2a 64 62 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54  *db;.  double rT
78da0 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30  ;.  char zBuf[20
78db0 5d 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  ];..  UNUSED_PAR
78dc0 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
78dd0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
78de0 28 61 72 67 76 29 3b 0a 0a 20 20 64 62 20 3d 20  (argv);..  db = 
78df0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
78e00 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
78e10 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43  t);.  sqlite3OsC
78e20 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70  urrentTime(db->p
78e30 56 66 73 2c 20 26 72 54 29 3b 0a 23 69 66 6e 64  Vfs, &rT);.#ifnd
78e40 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
78e50 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
78e60 74 20 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20  t = 86400.0*(rT 
78e70 2d 20 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30  - 2440587.5) + 0
78e80 2e 35 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 77  .5;.#else.  /* w
78e90 69 74 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20  ithout floating 
78ea0 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 20 72  point support, r
78eb0 54 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a  T will have.  **
78ec0 20 61 6c 72 65 61 64 79 20 6c 6f 73 74 20 66 72   already lost fr
78ed0 61 63 74 69 6f 6e 61 6c 20 64 61 79 20 70 72 65  actional day pre
78ee0 63 69 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 74  cision..  */.  t
78ef0 20 3d 20 38 36 34 30 30 20 2a 20 28 72 54 20 2d   = 86400 * (rT -
78f00 20 32 34 34 30 35 38 37 29 20 2d 20 34 33 32 30   2440587) - 4320
78f10 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  0;.#endif.#ifdef
78f20 20 48 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20   HAVE_GMTIME_R. 
78f30 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d   {.    struct tm
78f40 20 73 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d   sNow;.    gmtim
78f50 65 5f 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a  e_r(&t, &sNow);.
78f60 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75      strftime(zBu
78f70 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20  f, 20, zFormat, 
78f80 26 73 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73  &sNow);.  }.#els
78f90 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74  e.  {.    struct
78fa0 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71   tm *pTm;.    sq
78fb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
78fc0 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  r(sqlite3MutexAl
78fd0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
78fe0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
78ff0 3b 0a 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69  ;.    pTm = gmti
79000 6d 65 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66  me(&t);.    strf
79010 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a  time(zBuf, 20, z
79020 46 6f 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20  Format, pTm);.  
79030 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
79040 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74  leave(sqlite3Mut
79050 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
79060 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
79070 45 52 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ER));.  }.#endif
79080 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
79090 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
790a0 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54   zBuf, -1, SQLIT
790b0 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a  E_TRANSIENT);.}.
790c0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
790d0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69  is function regi
790e0 73 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68  stered all of th
790f0 65 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69  e above C functi
79100 6f 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75  ons as SQL.** fu
79110 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73  nctions.  This s
79120 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c  hould be the onl
79130 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69  y routine in thi
79140 73 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65  s file with.** e
79150 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e  xternal linkage.
79160 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
79170 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
79180 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46  egisterDateTimeF
79190 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a  unctions(void){.
791a0 20 20 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f    static SQLITE_
791b0 57 53 44 20 46 75 6e 63 44 65 66 20 61 44 61 74  WSD FuncDef aDat
791c0 65 54 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b  eTimeFuncs[] = {
791d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
791e0 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55  OMIT_DATETIME_FU
791f0 4e 43 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  NCS.    FUNCTION
79200 28 6a 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20  (julianday,     
79210 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c     -1, 0, 0, jul
79220 69 61 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20  iandayFunc ),.  
79230 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c    FUNCTION(date,
79240 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c               -1,
79250 20 30 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20   0, 0, dateFunc 
79260 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
79270 54 49 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20  TION(time,      
79280 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c         -1, 0, 0,
79290 20 74 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29   timeFunc      )
792a0 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64  ,.    FUNCTION(d
792b0 61 74 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20  atetime,        
792c0 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74   -1, 0, 0, datet
792d0 69 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20  imeFunc  ),.    
792e0 46 55 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d  FUNCTION(strftim
792f0 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30  e,         -1, 0
79300 2c 20 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e  , 0, strftimeFun
79310 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49  c  ),.    FUNCTI
79320 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c  ON(current_time,
79330 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63        0, 0, 0, c
79340 74 69 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a  timeFunc     ),.
79350 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72      FUNCTION(cur
79360 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20  rent_timestamp, 
79370 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74  0, 0, 0, ctimest
79380 61 6d 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55  ampFunc),.    FU
79390 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64  NCTION(current_d
793a0 61 74 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20  ate,      0, 0, 
793b0 30 2c 20 63 64 61 74 65 46 75 6e 63 20 20 20 20  0, cdateFunc    
793c0 20 29 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54   ),.#else.    ST
793d0 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65  R_FUNCTION(curre
793e0 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c  nt_time,      0,
793f0 20 22 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20   "%H:%M:%S",    
79400 20 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74        0, current
79410 54 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53  TimeFunc),.    S
79420 54 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72  TR_FUNCTION(curr
79430 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30  ent_timestamp, 0
79440 2c 20 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20  , "%Y-%m-%d",   
79450 20 20 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e         0, curren
79460 74 54 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20  tTimeFunc),.    
79470 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72  STR_FUNCTION(cur
79480 72 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20  rent_date,      
79490 30 2c 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a  0, "%Y-%m-%d %H:
794a0 25 4d 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65  %M:%S", 0, curre
794b0 6e 74 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e  ntTimeFunc),.#en
794c0 64 69 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69  dif.  };.  int i
794d0 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20  ;.  FuncDefHash 
794e0 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c  *pHash = &GLOBAL
794f0 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71  (FuncDefHash, sq
79500 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74  lite3GlobalFunct
79510 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66  ions);.  FuncDef
79520 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44   *aFunc = (FuncD
79530 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63  ef*)&GLOBAL(Func
79540 44 65 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75  Def, aDateTimeFu
79550 6e 63 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30  ncs);..  for(i=0
79560 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44  ; i<ArraySize(aD
79570 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69  ateTimeFuncs); i
79580 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
79590 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48  FuncDefInsert(pH
795a0 61 73 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b  ash, &aFunc[i]);
795b0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  .  }.}../*******
795c0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64  ******* End of d
795d0 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ate.c **********
795e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
795f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79600 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
79610 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
79620 6c 65 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  le os.c ********
79630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79650 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
79660 30 35 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a  05 November 29.*
79670 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
79680 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
79690 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
796a0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
796b0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
796c0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
796d0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
796e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
796f0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
79700 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
79710 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
79720 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
79730 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
79740 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
79750 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
79760 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
79770 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
79780 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
79790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
797d0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
797e0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69  le contains OS i
797f0 6e 74 65 72 66 61 63 65 20 63 6f 64 65 20 74 68  nterface code th
79800 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20  at is common to 
79810 61 6c 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74  all.** architect
79820 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ures..**.** $Id:
79830 20 6f 73 2e 63 2c 76 20 31 2e 31 32 36 20 32 30   os.c,v 1.126 20
79840 30 39 2f 30 33 2f 32 35 20 31 34 3a 32 34 3a 34  09/03/25 14:24:4
79850 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  2 drh Exp $.*/.#
79860 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f  define _SQLITE_O
79870 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53  S_C_ 1.#undef _S
79880 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a  QLITE_OS_C_../*.
79890 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 53  ** The default S
798a0 51 4c 69 74 65 20 73 71 6c 69 74 65 33 5f 76 66  QLite sqlite3_vf
798b0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
798c0 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74  s do not allocat
798d0 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74  e.** memory (act
798e0 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63  ually, os_unix.c
798f0 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 6d 61   allocates a sma
79900 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  ll amount of mem
79910 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68  ory.** from with
79920 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75  in OsOpen()), bu
79930 74 20 73 6f 6d 65 20 74 68 69 72 64 2d 70 61 72  t some third-par
79940 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ty implementatio
79950 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65  ns may..** So we
79960 20 74 65 73 74 20 74 68 65 20 65 66 66 65 63 74   test the effect
79970 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  s of a malloc() 
79980 66 61 69 6c 69 6e 67 20 61 6e 64 20 74 68 65 20  failing and the 
79990 73 71 6c 69 74 65 33 4f 73 58 58 58 28 29 0a 2a  sqlite3OsXXX().*
799a0 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  * function retur
799b0 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f 45 52  ning SQLITE_IOER
799c0 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68  R_NOMEM using th
799d0 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54  e DO_OS_MALLOC_T
799e0 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a  EST macro..**.**
799f0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
79a00 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 73  unctions are ins
79a10 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61  trumented for ma
79a20 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 0a  lloc() failure .
79a30 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a  ** testing:.**.*
79a40 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f  *     sqlite3OsO
79a50 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  pen().**     sql
79a60 69 74 65 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20  ite3OsRead().** 
79a70 20 20 20 20 73 71 6c 69 74 65 33 4f 73 57 72 69      sqlite3OsWri
79a80 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  te().**     sqli
79a90 74 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20  te3OsSync().**  
79aa0 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b     sqlite3OsLock
79ab0 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66  ().**.*/.#if def
79ac0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
79ad0 29 20 26 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f  ) && (SQLITE_OS_
79ae0 57 49 4e 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e  WIN==0).  #defin
79af0 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54  e DO_OS_MALLOC_T
79b00 45 53 54 20 69 66 20 28 31 29 20 7b 20 20 20 20  EST if (1) {    
79b10 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 76 6f          \.    vo
79b20 69 64 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20  id *pTstAlloc = 
79b30 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30  sqlite3Malloc(10
79b40 29 3b 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69  );       \.    i
79b50 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72  f (!pTstAlloc) r
79b60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
79b70 52 52 5f 4e 4f 4d 45 4d 3b 20 20 5c 0a 20 20 20  RR_NOMEM;  \.   
79b80 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54   sqlite3_free(pT
79b90 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20  stAlloc);       
79ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
79bb0 20 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69   }.#else.  #defi
79bc0 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  ne DO_OS_MALLOC_
79bd0 54 45 53 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  TEST.#endif../*.
79be0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
79bf0 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f   routines are co
79c00 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65  nvenience wrappe
79c10 72 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64  rs around method
79c20 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69  s.** of the sqli
79c30 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e  te3_file object.
79c40 20 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79    This is mostly
79c50 20 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 20   just syntactic 
79c60 73 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66  sugar. All.** of
79c70 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63   this would be c
79c80 6f 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61  ompletely automa
79c90 74 69 63 20 69 66 20 53 51 4c 69 74 65 20 77 65  tic if SQLite we
79ca0 72 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a  re coded using.*
79cb0 2a 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66  * C++ instead of
79cc0 20 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f   plain old C..*/
79cd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
79ce0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f  int sqlite3OsClo
79cf0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
79d00 2a 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20  *pId){.  int rc 
79d10 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
79d20 66 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73  f( pId->pMethods
79d30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64   ){.    rc = pId
79d40 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f  ->pMethods->xClo
79d50 73 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64  se(pId);.    pId
79d60 2d 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a  ->pMethods = 0;.
79d70 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
79d80 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
79d90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52  E int sqlite3OsR
79da0 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ead(sqlite3_file
79db0 20 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66   *id, void *pBuf
79dc0 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f  , int amt, i64 o
79dd0 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f  ffset){.  DO_OS_
79de0 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72  MALLOC_TEST;.  r
79df0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
79e00 64 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42  ds->xRead(id, pB
79e10 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29  uf, amt, offset)
79e20 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
79e30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
79e40 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69  Write(sqlite3_fi
79e50 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f  le *id, const vo
79e60 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d  id *pBuf, int am
79e70 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a  t, i64 offset){.
79e80 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
79e90 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64  EST;.  return id
79ea0 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69  ->pMethods->xWri
79eb0 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74  te(id, pBuf, amt
79ec0 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c  , offset);.}.SQL
79ed0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
79ee0 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
79ef0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
79f00 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20  id, i64 size){. 
79f10 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
79f20 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28  hods->xTruncate(
79f30 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c  id, size);.}.SQL
79f40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
79f50 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71  sqlite3OsSync(sq
79f60 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
79f70 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f  int flags){.  DO
79f80 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b  _OS_MALLOC_TEST;
79f90 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
79fa0 65 74 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64  ethods->xSync(id
79fb0 2c 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49  , flags);.}.SQLI
79fc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
79fd0 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
79fe0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
79ff0 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a  d, i64 *pSize){.
7a000 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
7a010 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64  EST;.  return id
7a020 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c  ->pMethods->xFil
7a030 65 53 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29  eSize(id, pSize)
7a040 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
7a050 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
7a060 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
7a070 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54  e *id, int lockT
7a080 79 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  ype){.  DO_OS_MA
7a090 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74  LLOC_TEST;.  ret
7a0a0 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
7a0b0 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b  ->xLock(id, lock
7a0c0 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  Type);.}.SQLITE_
7a0d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7a0e0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69  te3OsUnlock(sqli
7a0f0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
7a100 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72  t lockType){.  r
7a110 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
7a120 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20  ds->xUnlock(id, 
7a130 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c  lockType);.}.SQL
7a140 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7a150 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65  sqlite3OsCheckRe
7a160 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
7a170 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
7a180 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f   *pResOut){.  DO
7a190 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b  _OS_MALLOC_TEST;
7a1a0 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
7a1b0 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65  ethods->xCheckRe
7a1c0 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70  servedLock(id, p
7a1d0 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54  ResOut);.}.SQLIT
7a1e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7a1f0 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
7a200 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ol(sqlite3_file 
7a210 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
7a220 64 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75  d *pArg){.  retu
7a230 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
7a240 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64  >xFileControl(id
7a250 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53  , op, pArg);.}.S
7a260 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
7a270 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  t sqlite3OsSecto
7a280 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
7a290 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28  le *id){.  int (
7a2a0 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71  *xSectorSize)(sq
7a2b0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69  lite3_file*) = i
7a2c0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65  d->pMethods->xSe
7a2d0 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75  ctorSize;.  retu
7a2e0 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20  rn (xSectorSize 
7a2f0 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64  ? xSectorSize(id
7a300 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ) : SQLITE_DEFAU
7a310 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b  LT_SECTOR_SIZE);
7a320 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
7a330 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44  E int sqlite3OsD
7a340 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
7a350 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c  tics(sqlite3_fil
7a360 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  e *id){.  return
7a370 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
7a380 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
7a390 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a  stics(id);.}../*
7a3a0 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f  .** The next gro
7a3b0 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61  up of routines a
7a3c0 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77  re convenience w
7a3d0 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74  rappers around t
7a3e0 68 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64  he.** VFS method
7a3f0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
7a400 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7a410 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  OsOpen(.  sqlite
7a420 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20  3_vfs *pVfs, .  
7a430 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
7a440 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  h, .  sqlite3_fi
7a450 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e  le *pFile, .  in
7a460 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20  t flags, .  int 
7a470 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20  *pFlagsOut.){.  
7a480 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f  int rc;.  DO_OS_
7a490 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72  MALLOC_TEST;.  r
7a4a0 63 20 3d 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28  c = pVfs->xOpen(
7a4b0 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69  pVfs, zPath, pFi
7a4c0 6c 65 2c 20 66 6c 61 67 73 2c 20 70 46 6c 61 67  le, flags, pFlag
7a4d0 73 4f 75 74 29 3b 0a 20 20 61 73 73 65 72 74 28  sOut);.  assert(
7a4e0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
7a4f0 7c 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  | pFile->pMethod
7a500 73 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  s==0 );.  return
7a510 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   rc;.}.SQLITE_PR
7a520 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7a530 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65  3OsDelete(sqlite
7a540 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e  3_vfs *pVfs, con
7a550 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
7a560 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20  int dirSync){.  
7a570 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 65  return pVfs->xDe
7a580 6c 65 74 65 28 70 56 66 73 2c 20 7a 50 61 74 68  lete(pVfs, zPath
7a590 2c 20 64 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51  , dirSync);.}.SQ
7a5a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
7a5b0 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
7a5c0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
7a5d0 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20  *pVfs, .  const 
7a5e0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20  char *zPath, .  
7a5f0 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e  int flags, .  in
7a600 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20  t *pResOut.){.  
7a610 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
7a620 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73  T;.  return pVfs
7a630 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 2c 20  ->xAccess(pVfs, 
7a640 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 52  zPath, flags, pR
7a650 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45  esOut);.}.SQLITE
7a660 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
7a670 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61  ite3OsFullPathna
7a680 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  me(.  sqlite3_vf
7a690 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73  s *pVfs, .  cons
7a6a0 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a  t char *zPath, .
7a6b0 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20    int nPathOut, 
7a6c0 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f 75  .  char *zPathOu
7a6d0 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  t.){.  return pV
7a6e0 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d  fs->xFullPathnam
7a6f0 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e  e(pVfs, zPath, n
7a700 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75  PathOut, zPathOu
7a710 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  t);.}.#ifndef SQ
7a720 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
7a730 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f  XTENSION.SQLITE_
7a740 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
7a750 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71  lite3OsDlOpen(sq
7a760 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
7a770 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
7a780 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  th){.  return pV
7a790 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73  fs->xDlOpen(pVfs
7a7a0 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49  , zPath);.}.SQLI
7a7b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
7a7c0 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72  sqlite3OsDlError
7a7d0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
7a7e0 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63  fs, int nByte, c
7a7f0 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20  har *zBufOut){. 
7a800 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28   pVfs->xDlError(
7a810 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75  pVfs, nByte, zBu
7a820 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  fOut);.}.SQLITE_
7a830 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73  PRIVATE void (*s
7a840 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71  qlite3OsDlSym(sq
7a850 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
7a860 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f   void *pHdle, co
7a870 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29  nst char *zSym))
7a880 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  (void){.  return
7a890 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56   pVfs->xDlSym(pV
7a8a0 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29  fs, pHdle, zSym)
7a8b0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
7a8c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f  TE void sqlite3O
7a8d0 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  sDlClose(sqlite3
7a8e0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
7a8f0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56   *pHandle){.  pV
7a900 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66  fs->xDlClose(pVf
7a910 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23  s, pHandle);.}.#
7a920 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
7a930 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
7a940 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ION */.SQLITE_PR
7a950 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7a960 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71  3OsRandomness(sq
7a970 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
7a980 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72   int nByte, char
7a990 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65   *zBufOut){.  re
7a9a0 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64  turn pVfs->xRand
7a9b0 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79  omness(pVfs, nBy
7a9c0 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a  te, zBufOut);.}.
7a9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
7a9e0 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  nt sqlite3OsSlee
7a9f0 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  p(sqlite3_vfs *p
7aa00 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29  Vfs, int nMicro)
7aa10 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
7aa20 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d  >xSleep(pVfs, nM
7aa30 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  icro);.}.SQLITE_
7aa40 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7aa50 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
7aa60 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
7aa70 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d  fs, double *pTim
7aa80 65 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  eOut){.  return 
7aa90 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69  pVfs->xCurrentTi
7aaa0 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75  me(pVfs, pTimeOu
7aab0 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  t);.}..SQLITE_PR
7aac0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7aad0 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20  3OsOpenMalloc(. 
7aae0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
7aaf0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
7ab00 72 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c  r *zFile, .  sql
7ab10 69 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69  ite3_file **ppFi
7ab20 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73  le, .  int flags
7ab30 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61  ,.  int *pOutFla
7ab40 67 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  gs.){.  int rc =
7ab50 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
7ab60 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
7ab70 46 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20  File;.  pFile = 
7ab80 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
7ab90 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56  sqlite3Malloc(pV
7aba0 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20  fs->szOsFile);. 
7abb0 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20   if( pFile ){.  
7abc0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
7abd0 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65  Open(pVfs, zFile
7abe0 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20  , pFile, flags, 
7abf0 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20  pOutFlags);.    
7ac00 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
7ac10 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
7ac20 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a  e3_free(pFile);.
7ac30 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
7ac40 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65   *ppFile = pFile
7ac50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
7ac60 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
7ac70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7ac80 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
7ac90 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
7aca0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  File){.  int rc 
7acb0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  = SQLITE_OK;.  a
7acc0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
7acd0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
7ace0 43 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20  Close(pFile);.  
7acf0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69  sqlite3_free(pFi
7ad00 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  le);.  return rc
7ad10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c  ;.}../*.** The l
7ad20 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73  ist of all regis
7ad30 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d  tered VFS implem
7ad40 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  entations..*/.st
7ad50 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73  atic sqlite3_vfs
7ad60 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76 66   * SQLITE_WSD vf
7ad70 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 69  sList = 0;.#defi
7ad80 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 41  ne vfsList GLOBA
7ad90 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  L(sqlite3_vfs *,
7ada0 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a 2a   vfsList)../*.**
7adb0 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62 79   Locate a VFS by
7adc0 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e 61   name.  If no na
7add0 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69 6d  me is given, sim
7ade0 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a 2a  ply return the.*
7adf0 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20 74  * first VFS on t
7ae00 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49  he list..*/.SQLI
7ae10 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76  TE_API sqlite3_v
7ae20 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f  fs *sqlite3_vfs_
7ae30 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20  find(const char 
7ae40 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65  *zVfs){.  sqlite
7ae50 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b  3_vfs *pVfs = 0;
7ae60 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
7ae70 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33  ADSAFE.  sqlite3
7ae80 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23  _mutex *mutex;.#
7ae90 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
7aea0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
7aeb0 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71  IT.  int rc = sq
7aec0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
7aed0 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  ();.  if( rc ) r
7aee0 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a  eturn 0;.#endif.
7aef0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
7af00 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20  DSAFE.  mutex = 
7af10 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
7af20 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
7af30 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23  TATIC_MASTER);.#
7af40 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
7af50 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
7af60 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d  x);.  for(pVfs =
7af70 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20   vfsList; pVfs; 
7af80 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74  pVfs=pVfs->pNext
7af90 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d  ){.    if( zVfs=
7afa0 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
7afb0 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c  if( strcmp(zVfs,
7afc0 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30   pVfs->zName)==0
7afd0 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20   ) break;.  }.  
7afe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
7aff0 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65  ave(mutex);.  re
7b000 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a  turn pVfs;.}../*
7b010 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53  .** Unlink a VFS
7b020 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64   from the linked
7b030 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20   list.*/.static 
7b040 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73  void vfsUnlink(s
7b050 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
7b060 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
7b070 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
7b080 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
7b090 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
7b0a0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29  TATIC_MASTER)) )
7b0b0 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20  ;.  if( pVfs==0 
7b0c0 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20  ){.    /* No-op 
7b0d0 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76  */.  }else if( v
7b0e0 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a  fsList==pVfs ){.
7b0f0 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56      vfsList = pV
7b100 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c  fs->pNext;.  }el
7b110 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 29  se if( vfsList )
7b120 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66  {.    sqlite3_vf
7b130 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a  s *p = vfsList;.
7b140 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e      while( p->pN
7b150 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21  ext && p->pNext!
7b160 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70  =pVfs ){.      p
7b170 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   = p->pNext;.   
7b180 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e   }.    if( p->pN
7b190 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20  ext==pVfs ){.   
7b1a0 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56     p->pNext = pV
7b1b0 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  fs->pNext;.    }
7b1c0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
7b1d0 67 69 73 74 65 72 20 61 20 56 46 53 20 77 69 74  gister a VFS wit
7b1e0 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49  h the system.  I
7b1f0 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f  t is harmless to
7b200 20 72 65 67 69 73 74 65 72 20 74 68 65 20 73 61   register the sa
7b210 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70  me.** VFS multip
7b220 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e  le times.  The n
7b230 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74  ew VFS becomes t
7b240 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61  he default if ma
7b250 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75  keDflt is.** tru
7b260 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
7b270 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73   int sqlite3_vfs
7b280 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65  _register(sqlite
7b290 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
7b2a0 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73 71   makeDflt){.  sq
7b2b0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
7b2c0 65 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20  ex = 0;.#ifndef 
7b2d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
7b2e0 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20  INIT.  int rc = 
7b2f0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
7b300 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ze();.  if( rc )
7b310 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64   return rc;.#end
7b320 69 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c  if.  mutex = sql
7b330 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
7b340 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
7b350 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71  IC_MASTER);.  sq
7b360 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
7b370 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55  r(mutex);.  vfsU
7b380 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 69  nlink(pVfs);.  i
7b390 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 76  f( makeDflt || v
7b3a0 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20  fsList==0 ){.   
7b3b0 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76   pVfs->pNext = v
7b3c0 66 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 4c  fsList;.    vfsL
7b3d0 69 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 65  ist = pVfs;.  }e
7b3e0 6c 73 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70  lse{.    pVfs->p
7b3f0 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 2d 3e  Next = vfsList->
7b400 70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 4c 69  pNext;.    vfsLi
7b410 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73  st->pNext = pVfs
7b420 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 76  ;.  }.  assert(v
7b430 66 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74  fsList);.  sqlit
7b440 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
7b450 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
7b460 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
7b470 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61  .** Unregister a
7b480 20 56 46 53 20 73 6f 20 74 68 61 74 20 69 74 20   VFS so that it 
7b490 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 63  is no longer acc
7b4a0 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  essible..*/.SQLI
7b4b0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
7b4c0 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
7b4d0 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  r(sqlite3_vfs *p
7b4e0 56 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 45  Vfs){.#if SQLITE
7b4f0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71  _THREADSAFE.  sq
7b500 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
7b510 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
7b520 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
7b530 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
7b540 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c  R);.#endif.  sql
7b550 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
7b560 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e  (mutex);.  vfsUn
7b570 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 71  link(pVfs);.  sq
7b580 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
7b590 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  e(mutex);.  retu
7b5a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
7b5b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
7b5c0 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a 2a   End of os.c ***
7b5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
7b600 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
7b610 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61 75 6c   Begin file faul
7b620 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
7b630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
7b650 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e 20  ./*.** 2008 Jan 
7b660 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
7b670 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
7b680 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
7b690 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
7b6a0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
7b6b0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
7b6c0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
7b6d0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
7b6e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
7b6f0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
7b700 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
7b710 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
7b720 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
7b730 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
7b740 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
7b750 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
7b760 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
7b770 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
7b780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b7c0 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 61 75  *.**.** $Id: fau
7b7d0 6c 74 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38  lt.c,v 1.11 2008
7b7e0 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 20  /09/02 00:52:52 
7b7f0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
7b800 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
7b810 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 73  ntains code to s
7b820 75 70 70 6f 72 74 20 74 68 65 20 63 6f 6e 63 65  upport the conce
7b830 70 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 20 0a  pt of "benign" .
7b840 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ** malloc failur
7b850 65 73 20 28 77 68 65 6e 20 74 68 65 20 78 4d 61  es (when the xMa
7b860 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 6c 6c  lloc() or xReall
7b870 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74  oc() method of t
7b880 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65  he.** sqlite3_me
7b890 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74  m_methods struct
7b8a0 75 72 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c  ure fails to all
7b8b0 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66  ocate a block of
7b8c0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 72   memory.** and r
7b8d0 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a  eturns 0). .**.*
7b8e0 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 61  * Most malloc fa
7b8f0 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62  ilures are non-b
7b900 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 68 65  enign. After the
7b910 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 65 0a  y occur, SQLite.
7b920 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 65 20  ** abandons the 
7b930 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f  current operatio
7b940 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e  n and returns an
7b950 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75   error code (usu
7b960 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  ally.** SQLITE_N
7b970 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 73 65  OMEM) to the use
7b980 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65  r. However, some
7b990 74 69 6d 65 73 20 61 20 66 61 75 6c 74 20 69 73  times a fault is
7b9a0 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
7b9b0 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20 65  .** fatal. For e
7b9c0 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61 6c  xample, if a mal
7b9d0 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20  loc fails while 
7b9e0 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73 68 20  resizing a hash 
7b9f0 74 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a 20  table, this .** 
7ba00 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 65  is completely re
7ba10 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c 79  coverable simply
7ba20 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e 67   by not carrying
7ba30 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65 2e   out the resize.
7ba40 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 74 61   The .** hash ta
7ba50 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75  ble will continu
7ba60 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f  e to function no
7ba70 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61  rmally.  So a ma
7ba80 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a 2a  lloc failure .**
7ba90 20 64 75 72 69 6e 67 20 61 20 68 61 73 68 20 74   during a hash t
7baa0 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20 61  able resize is a
7bab0 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a 2a   benign fault..*
7bac0 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  /...#ifndef SQLI
7bad0 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
7bae0 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62  TEST../*.** Glob
7baf0 61 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2f  al variables..*/
7bb00 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
7bb10 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
7bb20 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f  s BenignMallocHo
7bb30 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 4c 49  oks;.static SQLI
7bb40 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 42 65  TE_WSD struct Be
7bb50 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20  nignMallocHooks 
7bb60 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69  {.  void (*xBeni
7bb70 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 3b 0a  gnBegin)(void);.
7bb80 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e    void (*xBenign
7bb90 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73 71  End)(void);.} sq
7bba0 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 30  lite3Hooks = { 0
7bbb0 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22  , 0 };../* The "
7bbc0 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20  wsdHooks" macro 
7bbd0 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20  will resolve to 
7bbe0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
7bbf0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
7bc00 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  s.** structure. 
7bc10 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61   If writable sta
7bc20 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75  tic data is unsu
7bc30 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74  pported on the t
7bc40 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76  arget,.** we hav
7bc50 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  e to locate the 
7bc60 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20  state vector at 
7bc70 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68  run-time.  In th
7bc80 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a  e more common.**
7bc90 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69 74   case where writ
7bca0 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
7bcb0 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77   is supported, w
7bcc0 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65  sdHooks can refe
7bcd0 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f  r directly.** to
7bce0 20 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f 6f   the "sqlite3Hoo
7bcf0 6b 73 22 20 73 74 61 74 65 20 76 65 63 74 6f 72  ks" state vector
7bd00 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e   declared above.
7bd10 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
7bd20 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66  E_OMIT_WSD.# def
7bd30 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74  ine wsdHooksInit
7bd40 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f   \.  BenignMallo
7bd50 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c 4f  cHooks *x = &GLO
7bd60 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  BAL(BenignMalloc
7bd70 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f 6f  Hooks,sqlite3Hoo
7bd80 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64  ks).# define wsd
7bd90 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 65  Hooks x[0].#else
7bda0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f  .# define wsdHoo
7bdb0 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20  ksInit.# define 
7bdc0 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65 33  wsdHooks sqlite3
7bdd0 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f  Hooks.#endif.../
7bde0 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f  *.** Register ho
7bdf0 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e  oks to call when
7be00 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
7be10 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0a  ignMalloc() and.
7be20 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  ** sqlite3EndBen
7be30 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20  ignMalloc() are 
7be40 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 69  called, respecti
7be50 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  vely..*/.SQLITE_
7be60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
7be70 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  ite3BenignMalloc
7be80 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28 2a  Hooks(.  void (*
7be90 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f  xBenignBegin)(vo
7bea0 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 42  id),.  void (*xB
7beb0 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 0a  enignEnd)(void).
7bec0 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69  ){.  wsdHooksIni
7bed0 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42  t;.  wsdHooks.xB
7bee0 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 42 65  enignBegin = xBe
7bef0 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73 64  nignBegin;.  wsd
7bf00 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64  Hooks.xBenignEnd
7bf10 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d   = xBenignEnd;.}
7bf20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73 71  ../*.** This (sq
7bf30 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
7bf40 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 6c 6c 65  lloc()) is calle
7bf50 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f 64 65  d by SQLite code
7bf60 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61   to indicate tha
7bf70 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20  t.** subsequent 
7bf80 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20  malloc failures 
7bf90 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 63 61  are benign. A ca
7bfa0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 6e 64  ll to sqlite3End
7bfb0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a  BenignMalloc().*
7bfc0 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  * indicates that
7bfd0 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c   subsequent mall
7bfe0 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20  oc failures are 
7bff0 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53  non-benign..*/.S
7c000 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7c010 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  id sqlite3BeginB
7c020 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64  enignMalloc(void
7c030 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69  ){.  wsdHooksIni
7c040 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b  t;.  if( wsdHook
7c050 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 29  s.xBenignBegin )
7c060 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78  {.    wsdHooks.x
7c070 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a 20  BenignBegin();. 
7c080 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56   }.}.SQLITE_PRIV
7c090 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
7c0a0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
7c0b0 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b  void){.  wsdHook
7c0c0 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64  sInit;.  if( wsd
7c0d0 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64  Hooks.xBenignEnd
7c0e0 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73   ){.    wsdHooks
7c0f0 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a 20  .xBenignEnd();. 
7c100 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20 2f   }.}..#endif   /
7c110 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  * #ifndef SQLITE
7c120 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
7c130 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ST */../********
7c140 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61  ****** End of fa
7c150 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ult.c **********
7c160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c180 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
7c190 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
7c1a0 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e mem0.c *******
7c1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c1d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
7c1e0 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a 0a  8 October 28.**.
7c1f0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
7c200 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
7c210 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
7c220 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
7c230 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
7c240 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
7c250 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
7c260 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
7c270 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
7c280 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7c290 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
7c2a0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
7c2b0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
7c2c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7c2d0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
7c2e0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
7c2f0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
7c300 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
7c310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
7c350 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
7c360 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d  ains a no-op mem
7c370 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64  ory allocation d
7c380 72 69 76 65 72 73 20 66 6f 72 20 75 73 65 20 77  rivers for use w
7c390 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45  hen.** SQLITE_ZE
7c3a0 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66  RO_MALLOC is def
7c3b0 69 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63  ined.  The alloc
7c3c0 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 69 6d  ation drivers im
7c3d0 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 72  plemented.** her
7c3e0 65 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20  e always fail.  
7c3f0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20  SQLite will not 
7c400 6f 70 65 72 61 74 65 20 77 69 74 68 20 74 68 65  operate with the
7c410 73 65 20 64 72 69 76 65 72 73 2e 20 20 54 68 65  se drivers.  The
7c420 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79  se.** are merely
7c430 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 20   placeholders.  
7c440 52 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75 73  Real drivers mus
7c450 74 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64  t be substituted
7c460 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65   using.** sqlite
7c470 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f 72  3_config() befor
7c480 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70  e SQLite will op
7c490 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  erate..**.** $Id
7c4a0 3a 20 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 20 32  : mem0.c,v 1.1 2
7c4b0 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 3a  008/10/28 18:58:
7c4c0 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  20 drh Exp $.*/.
7c4d0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  ./*.** This vers
7c4e0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
7c4f0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74  y allocator is t
7c500 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20  he default.  It 
7c510 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20  is.** used when 
7c520 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20  no other memory 
7c530 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65  allocator is spe
7c540 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d  cified using com
7c550 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63  pile-time.** mac
7c560 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ros..*/.#ifdef S
7c570 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f  QLITE_ZERO_MALLO
7c580 43 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 76  C../*.** No-op v
7c590 65 72 73 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 6d  ersions of all m
7c5a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
7c5b0 20 72 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 74 61   routines.*/.sta
7c5c0 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  tic void *sqlite
7c5d0 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  3MemMalloc(int n
7c5e0 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b  Byte){ return 0;
7c5f0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73   }.static void s
7c600 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f  qlite3MemFree(vo
7c610 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74  id *pPrior){ ret
7c620 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f  urn; }.static vo
7c630 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65  id *sqlite3MemRe
7c640 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69  alloc(void *pPri
7c650 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 20  or, int nByte){ 
7c660 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74  return 0; }.stat
7c670 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
7c680 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69  mSize(void *pPri
7c690 6f 72 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d  or){ return 0; }
7c6a0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
7c6b0 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e  te3MemRoundup(in
7c6c0 74 20 6e 29 7b 20 72 65 74 75 72 6e 20 6e 3b 20  t n){ return n; 
7c6d0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  }.static int sql
7c6e0 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64  ite3MemInit(void
7c6f0 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75   *NotUsed){ retu
7c700 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
7c710 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
7c720 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76  te3MemShutdown(v
7c730 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72  oid *NotUsed){ r
7c740 65 74 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn; }../*.** 
7c750 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
7c760 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
7c770 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
7c780 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b  th external link
7c790 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c  age..**.** Popul
7c7a0 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65  ate the low-leve
7c7b0 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
7c7c0 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  ion function poi
7c7d0 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69  nters in.** sqli
7c7e0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
7c7f0 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  m with pointers 
7c800 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  to the routines 
7c810 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f  in this file..*/
7c820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7c830 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53  void sqlite3MemS
7c840 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b  etDefault(void){
7c850 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
7c860 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
7c870 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f  ods defaultMetho
7c880 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69  ds = {.     sqli
7c890 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20  te3MemMalloc,.  
7c8a0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65     sqlite3MemFre
7c8b0 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  e,.     sqlite3M
7c8c0 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  emRealloc,.     
7c8d0 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a  sqlite3MemSize,.
7c8e0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52       sqlite3MemR
7c8f0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c  oundup,.     sql
7c900 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20  ite3MemInit,.   
7c910 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74    sqlite3MemShut
7c920 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d  down,.     0.  }
7c930 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ;.  sqlite3_conf
7c940 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
7c950 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c  _MALLOC, &defaul
7c960 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65  tMethods);.}..#e
7c970 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 5a  ndif /* SQLITE_Z
7c980 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f  ERO_MALLOC */../
7c990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
7c9a0 6e 64 20 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a 2a  nd of mem0.c ***
7c9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
7c9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
7c9f0 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 31 2e 63  egin file mem1.c
7ca00 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
7ca10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ca20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
7ca30 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74  *.** 2007 August
7ca40 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   14.**.** The au
7ca50 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
7ca60 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
7ca70 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
7ca80 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
7ca90 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
7caa0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
7cab0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
7cac0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
7cad0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
7cae0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
7caf0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
7cb00 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
7cb10 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
7cb20 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
7cb30 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
7cb40 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
7cb50 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
7cb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7cb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7cb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7cb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7cba0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
7cbb0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d  le contains low-
7cbc0 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
7cbd0 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20  ocation drivers 
7cbe0 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69  for when.** SQLi
7cbf0 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20  te will use the 
7cc00 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61  standard C-libra
7cc10 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f  ry malloc/reallo
7cc20 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63 65  c/free interface
7cc30 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68  .** to obtain th
7cc40 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64  e memory it need
7cc50 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  s..**.** This fi
7cc60 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c  le contains impl
7cc70 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
7cc80 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  he low-level mem
7cc90 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  ory allocation.*
7cca0 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63 69  * routines speci
7ccb0 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69  fied in the sqli
7ccc0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
7ccd0 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49  object..**.** $I
7cce0 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 33 30  d: mem1.c,v 1.30
7ccf0 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34 3a 33   2009/03/23 04:3
7cd00 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37  3:33 danielk1977
7cd10 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
7cd20 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
7cd30 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
7cd40 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66  cator is the def
7cd50 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20  ault.  It is.** 
7cd60 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68  used when no oth
7cd70 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  er memory alloca
7cd80 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64  tor is specified
7cd90 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74   using compile-t
7cda0 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a  ime.** macros..*
7cdb0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
7cdc0 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f  SYSTEM_MALLOC../
7cdd0 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f 63  *.** Like malloc
7cde0 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 72  (), but remember
7cdf0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
7ce00 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73   allocation.** s
7ce10 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66 69  o that we can fi
7ce20 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 69 6e  nd it later usin
7ce30 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65  g sqlite3MemSize
7ce40 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  ()..**.** For th
7ce50 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75  is low-level rou
7ce60 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 75 61  tine, we are gua
7ce70 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 42 79  ranteed that nBy
7ce80 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a 20  te>0 because.** 
7ce90 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d  cases of nByte<=
7cea0 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63  0 will be interc
7ceb0 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20  epted and dealt 
7cec0 77 69 74 68 20 62 79 20 68 69 67 68 65 72 20 6c  with by higher l
7ced0 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  evel.** routines
7cee0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
7cef0 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c   *sqlite3MemMall
7cf00 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  oc(int nByte){. 
7cf10 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
7cf20 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79  p;.  assert( nBy
7cf30 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20  te>0 );.  nByte 
7cf40 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b  = ROUND8(nByte);
7cf50 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  .  p = malloc( n
7cf60 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20  Byte+8 );.  if( 
7cf70 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20  p ){.    p[0] = 
7cf80 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a  nByte;.    p++;.
7cf90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f    }.  return (vo
7cfa0 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  id *)p;.}../*.**
7cfb0 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62 75 74   Like free() but
7cfc0 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f 63   works for alloc
7cfd0 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 20  ations obtained 
7cfe0 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d 4d  from sqlite3MemM
7cff0 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 71  alloc().** or sq
7d000 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28  lite3MemRealloc(
7d010 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69  )..**.** For thi
7d020 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74  s low-level rout
7d030 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 79 20  ine, we already 
7d040 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72  know that pPrior
7d050 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 73  !=0 since.** cas
7d060 65 73 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d  es where pPrior=
7d070 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65  =0 will have bee
7d080 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64  n intecepted and
7d090 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 62   dealt with.** b
7d0a0 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72  y higher-level r
7d0b0 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74  outines..*/.stat
7d0c0 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  ic void sqlite3M
7d0d0 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72  emFree(void *pPr
7d0e0 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ior){.  sqlite3_
7d0f0 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69  int64 *p = (sqli
7d100 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f  te3_int64*)pPrio
7d110 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72  r;.  assert( pPr
7d120 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b  ior!=0 );.  p--;
7d130 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f  .  free(p);.}../
7d140 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f  *.** Like reallo
7d150 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20  c().  Resize an 
7d160 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69  allocation previ
7d170 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66  ously obtained f
7d180 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65  rom.** sqlite3Me
7d190 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a  mMalloc()..**.**
7d1a0 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65   For this low-le
7d1b0 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77  vel interface, w
7d1c0 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69  e know that pPri
7d1d0 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68  or!=0.  Cases wh
7d1e0 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30  ere.** pPrior==0
7d1f0 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e   while have been
7d200 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 20   intercepted by 
7d210 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75  higher-level rou
7d220 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69  tine and.** redi
7d230 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f  rected to xMallo
7d240 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77  c.  Similarly, w
7d250 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74  e know that nByt
7d260 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20  e>0 becauses.** 
7d270 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 79 74  cases where nByt
7d280 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62  e<=0 will have b
7d290 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20  een intercepted 
7d2a0 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a  by higher-level.
7d2b0 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20  ** routines and 
7d2c0 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 46  redirected to xF
7d2d0 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ree..*/.static v
7d2e0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52  oid *sqlite3MemR
7d2f0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
7d300 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  ior, int nByte){
7d310 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
7d320 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69   *p = (sqlite3_i
7d330 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20  nt64*)pPrior;.  
7d340 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d  assert( pPrior!=
7d350 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a  0 && nByte>0 );.
7d360 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38    nByte = ROUND8
7d370 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 28  (nByte);.  p = (
7d380 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70  sqlite3_int64*)p
7d390 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20  Prior;.  p--;.  
7d3a0 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e  p = realloc(p, n
7d3b0 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20  Byte+8 );.  if( 
7d3c0 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20  p ){.    p[0] = 
7d3d0 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a  nByte;.    p++;.
7d3e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f    }.  return (vo
7d3f0 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  id*)p;.}../*.** 
7d400 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63  Report the alloc
7d410 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70  ated size of a p
7d420 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d  rior return from
7d430 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72   xMalloc().** or
7d440 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a   xRealloc()..*/.
7d450 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
7d460 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a  e3MemSize(void *
7d470 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74  pPrior){.  sqlit
7d480 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69  e3_int64 *p;.  i
7d490 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 72  f( pPrior==0 ) r
7d4a0 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 28  eturn 0;.  p = (
7d4b0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70  sqlite3_int64*)p
7d4c0 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20  Prior;.  p--;.  
7d4d0 72 65 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 5d  return (int)p[0]
7d4e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64  ;.}../*.** Round
7d4f0 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69   up a request si
7d500 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76  ze to the next v
7d510 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  alid allocation 
7d520 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  size..*/.static 
7d530 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f  int sqlite3MemRo
7d540 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20  undup(int n){.  
7d550 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29  return ROUND8(n)
7d560 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
7d570 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c  alize this modul
7d580 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
7d590 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28   sqlite3MemInit(
7d5a0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
7d5b0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
7d5c0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
7d5d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
7d5e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
7d5f0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75  ialize this modu
7d600 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
7d610 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75  id sqlite3MemShu
7d620 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
7d630 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
7d640 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
7d650 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
7d660 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
7d670 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72  ne is the only r
7d680 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66  outine in this f
7d690 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61  ile with externa
7d6a0 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a  l linkage..**.**
7d6b0 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f   Populate the lo
7d6c0 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
7d6d0 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
7d6e0 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a  on pointers in.*
7d6f0 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
7d700 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69  onfig.m with poi
7d710 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75  nters to the rou
7d720 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
7d730 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
7d740 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
7d750 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
7d760 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
7d770 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
7d780 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  m_methods defaul
7d790 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20  tMethods = {.   
7d7a0 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c    sqlite3MemMall
7d7b0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
7d7c0 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71  MemFree,.     sq
7d7d0 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c  lite3MemRealloc,
7d7e0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
7d7f0 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74  Size,.     sqlit
7d800 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20  e3MemRoundup,.  
7d810 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69     sqlite3MemIni
7d820 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  t,.     sqlite3M
7d830 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20  emShutdown,.    
7d840 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65   0.  };.  sqlite
7d850 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
7d860 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26  CONFIG_MALLOC, &
7d870 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b  defaultMethods);
7d880 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
7d890 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c  LITE_SYSTEM_MALL
7d8a0 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  OC */../********
7d8b0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
7d8c0 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m1.c ***********
7d8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d8f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
7d900 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
7d910 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e mem2.c *******
7d920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d940 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
7d950 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a  7 August 15.**.*
7d960 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
7d970 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
7d980 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
7d990 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
7d9a0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
7d9b0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
7d9c0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
7d9d0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
7d9e0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
7d9f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
7da00 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
7da10 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
7da20 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
7da30 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
7da40 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
7da50 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
7da60 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
7da70 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
7da80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7da90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7daa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7dab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
7dac0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
7dad0 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  ins low-level me
7dae0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
7daf0 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e  drivers for when
7db00 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
7db10 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64  use the standard
7db20 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f   C-library mallo
7db30 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69  c/realloc/free i
7db40 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f  nterface.** to o
7db50 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79  btain the memory
7db60 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20   it needs while 
7db70 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61  adding lots of a
7db80 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67  dditional debugg
7db90 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  ing.** informati
7dba0 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63  on to each alloc
7dbb0 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74  ation in order t
7dbc0 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e  o help detect an
7dbd0 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20  d fix memory.** 
7dbe0 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79  leaks and memory
7dbf0 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a   usage errors..*
7dc00 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
7dc10 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e  ontains implemen
7dc20 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c  tations of the l
7dc30 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
7dc40 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f  allocation.** ro
7dc50 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 64  utines specified
7dc60 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f   in the sqlite3_
7dc70 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  mem_methods obje
7dc80 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  ct..**.** $Id: m
7dc90 65 6d 32 2e 63 2c 76 20 31 2e 34 35 20 32 30 30  em2.c,v 1.45 200
7dca0 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 33  9/03/23 04:33:33
7dcb0 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
7dcc0 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   $.*/../*.** Thi
7dcd0 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  s version of the
7dce0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
7dcf0 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 69  r is used only i
7dd00 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f  f the.** SQLITE_
7dd10 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f 20 69  MEMDEBUG macro i
7dd20 73 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69 66  s defined.*/.#if
7dd30 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45  def SQLITE_MEMDE
7dd40 42 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62  BUG../*.** The b
7dd50 61 63 6b 74 72 61 63 65 20 66 75 6e 63 74 69 6f  acktrace functio
7dd60 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20 61  nality is only a
7dd70 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20 47 4c  vailable with GL
7dd80 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f  IBC.*/.#ifdef __
7dd90 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 72 6e  GLIBC__.  extern
7dda0 20 69 6e 74 20 62 61 63 6b 74 72 61 63 65 28 76   int backtrace(v
7ddb0 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65 78  oid**,int);.  ex
7ddc0 74 65 72 6e 20 76 6f 69 64 20 62 61 63 6b 74 72  tern void backtr
7ddd0 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 76  ace_symbols_fd(v
7dde0 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c 69  oid*const*,int,i
7ddf0 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  nt);.#else.# def
7de00 69 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41 2c  ine backtrace(A,
7de10 42 29 20 31 0a 23 20 64 65 66 69 6e 65 20 62 61  B) 1.# define ba
7de20 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f  cktrace_symbols_
7de30 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66  fd(A,B,C).#endif
7de40 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 6d  ../*.** Each mem
7de50 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c  ory allocation l
7de60 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ooks like this:.
7de70 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  **.**  ---------
7de80 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7de90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7dea0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7deb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
7dec0 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 62  **  | Title |  b
7ded0 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72  acktrace pointer
7dee0 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  s |  MemBlockHdr
7def0 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 7c   |  allocation |
7df00 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a 20    EndGuard |.** 
7df10 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
7df20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7df30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7df40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7df50 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
7df60 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
7df70 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 61  code sees only a
7df80 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
7df90 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 20  allocation.  We 
7dfa0 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b 20  have.** to back 
7dfb0 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c 6f  up from the allo
7dfc0 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74  cation pointer t
7dfd0 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c  o find the MemBl
7dfe0 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a 20  ockHdr.  The.** 
7dff0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c 6c  MemBlockHdr tell
7e000 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f 66  s us the size of
7e010 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
7e020 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  and the number o
7e030 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 70  f.** backtrace p
7e040 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65 20  ointers.  There 
7e050 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64 20  is also a guard 
7e060 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64 20  word at the end 
7e070 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f  of the.** MemBlo
7e080 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 74  ckHdr..*/.struct
7e090 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a 20   MemBlockHdr {. 
7e0a0 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20 20   i64 iSize;     
7e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e0c0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
7e0d0 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  this allocation 
7e0e0 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  */.  struct MemB
7e0f0 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20  lockHdr *pNext, 
7e100 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b  *pPrev;  /* Link
7e110 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75  ed list of all u
7e120 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f  nfreed memory */
7e130 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61  .  char nBacktra
7e140 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ce;             
7e150 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
7e160 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20 6f   of backtraces o
7e170 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0a  n this alloc */.
7e180 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63    char nBacktrac
7e190 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20  eSlots;         
7e1a0 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62        /* Availab
7e1b0 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f  le backtrace slo
7e1c0 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e 54  ts */.  short nT
7e1d0 69 74 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  itle;           
7e1e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
7e1f0 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69  ytes of title; i
7e200 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0a  ncludes '\0' */.
7e210 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72 64    int iForeGuard
7e220 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
7e230 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20 77        /* Guard w
7e240 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20 2a  ord for sanity *
7e250 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 72  /.};../*.** Guar
7e260 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 69  d words.*/.#defi
7e270 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 78 38  ne FOREGUARD 0x8
7e280 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65 20  0F5E153.#define 
7e290 52 45 41 52 47 55 41 52 44 20 30 78 45 34 36 37  REARGUARD 0xE467
7e2a0 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62  6B53../*.** Numb
7e2b0 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 7a  er of malloc siz
7e2c0 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f 20  e increments to 
7e2d0 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e  track..*/.#defin
7e2e0 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a 0a  e NCSIZE  1000..
7e2f0 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65  /*.** All of the
7e300 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65   static variable
7e310 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d  s used by this m
7e320 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63  odule are collec
7e330 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69  ted.** into a si
7e340 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e  ngle structure n
7e350 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 69  amed "mem".  Thi
7e360 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65  s is to keep the
7e370 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61  .** static varia
7e380 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61  bles organized a
7e390 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d  nd to reduce nam
7e3a0 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e  espace pollution
7e3b0 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f  .** when this mo
7e3c0 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64  dule is combined
7e3d0 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74   with other in t
7e3e0 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e  he amalgamation.
7e3f0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
7e400 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a  t {.  .  /*.  **
7e410 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f   Mutex to contro
7e420 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  l access to the 
7e430 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
7e440 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a  n subsystem..  *
7e450 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  /.  sqlite3_mute
7e460 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a  x *mutex;..  /*.
7e470 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 61    ** Head and ta
7e480 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c  il of a linked l
7e490 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 74  ist of all outst
7e4a0 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f  anding allocatio
7e4b0 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 74  ns.  */.  struct
7e4c0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 46   MemBlockHdr *pF
7e4d0 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20 4d  irst;.  struct M
7e4e0 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 73  emBlockHdr *pLas
7e4f0 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20  t;.  .  /*.  ** 
7e500 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65  The number of le
7e510 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 63  vels of backtrac
7e520 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 77  e to save in new
7e530 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20   allocations..  
7e540 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 72  */.  int nBacktr
7e550 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42  ace;.  void (*xB
7e560 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69  acktrace)(int, i
7e570 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a 20  nt, void **);.. 
7e580 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 74   /*.  ** Title t
7e590 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69 6e  ext to insert in
7e5a0 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 62   front of each b
7e5b0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  lock.  */.  int 
7e5c0 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 2f  nTitle;        /
7e5d0 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74 6c  * Bytes of zTitl
7e5e0 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 6c  e to save.  Incl
7e5f0 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70 61  udes '\0' and pa
7e600 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20  dding */.  char 
7e610 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f 2a  zTitle[100];  /*
7e620 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74 20   The title text 
7e630 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 73  */..  /* .  ** s
7e640 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61  qlite3MallocDisa
7e650 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e 74  llow() increment
7e660 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  s the following 
7e670 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 71  counter..  ** sq
7e680 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77  lite3MallocAllow
7e690 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69 74  () decrements it
7e6a0 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73  ..  */.  int dis
7e6b0 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f 74  allow; /* Do not
7e6c0 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 6c   allow memory al
7e6d0 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f  location */..  /
7e6e0 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73 74  *.  ** Gather st
7e6f0 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65 20  atistics on the 
7e700 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  sizes of memory 
7e710 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a  allocations..  *
7e720 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 74  * nAlloc[i] is t
7e730 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c  he number of all
7e740 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  ocation attempts
7e750 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 74   of i*8.  ** byt
7e760 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 69  es.  i==NCSIZE i
7e770 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
7e780 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d  allocation attem
7e790 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 7a  pts for.  ** siz
7e7a0 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 53  es more than NCS
7e7b0 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 2a  IZE*8 bytes..  *
7e7c0 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b 4e  /.  int nAlloc[N
7e7d0 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a 20  CSIZE];      /* 
7e7e0 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
7e7f0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  allocations */. 
7e800 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e 43   int nCurrent[NC
7e810 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 72  SIZE];    /* Cur
7e820 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 61  rent number of a
7e830 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20  llocations */.  
7e840 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e 43  int mxCurrent[NC
7e850 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 68  SIZE];   /* High
7e860 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e  water mark for n
7e870 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d 65  Current */..} me
7e880 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73  m;.../*.** Adjus
7e890 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 73  t memory usage s
7e8a0 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 61  tatistics.*/.sta
7e8b0 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74 53  tic void adjustS
7e8c0 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c 20  tats(int iSize, 
7e8d0 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b 0a  int increment){.
7e8e0 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 38    int i = ROUND8
7e8f0 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 28  (iSize)/8;.  if(
7e900 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a 20   i>NCSIZE-1 ){. 
7e910 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d 20     i = NCSIZE - 
7e920 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 63  1;.  }.  if( inc
7e930 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 20  rement>0 ){.    
7e940 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b  mem.nAlloc[i]++;
7e950 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e  .    mem.nCurren
7e960 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 20  t[i]++;.    if( 
7e970 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e  mem.nCurrent[i]>
7e980 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d  mem.mxCurrent[i]
7e990 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d 78   ){.      mem.mx
7e9a0 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d  Current[i] = mem
7e9b0 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 20  .nCurrent[i];.  
7e9c0 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
7e9d0 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
7e9e0 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  --;.    assert( 
7e9f0 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e  mem.nCurrent[i]>
7ea00 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  =0 );.  }.}../*.
7ea10 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c 6f  ** Given an allo
7ea20 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 65  cation, find the
7ea30 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f 72   MemBlockHdr for
7ea40 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f 6e   that allocation
7ea50 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
7ea60 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65 20  tine checks the 
7ea70 67 75 61 72 64 73 20 61 74 20 65 69 74 68 65 72  guards at either
7ea80 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c 6f   end of the allo
7ea90 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66  cation and.** if
7eaa0 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72 72   they are incorr
7eab0 65 63 74 20 69 74 20 61 73 73 65 72 74 73 2e 0a  ect it asserts..
7eac0 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
7ead0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 71   MemBlockHdr *sq
7eae0 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65  lite3MemsysGetHe
7eaf0 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c 6f  ader(void *pAllo
7eb00 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 63  cation){.  struc
7eb10 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
7eb20 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20  ;.  int *pInt;. 
7eb30 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 20   u8 *pU8;.  int 
7eb40 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 3d  nReserve;..  p =
7eb50 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63   (struct MemBloc
7eb60 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 6f  kHdr*)pAllocatio
7eb70 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 65  n;.  p--;.  asse
7eb80 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 72  rt( p->iForeGuar
7eb90 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 52  d==(int)FOREGUAR
7eba0 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 20  D );.  nReserve 
7ebb0 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 7a  = ROUND8(p->iSiz
7ebc0 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 6e  e);.  pInt = (in
7ebd0 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a  t*)pAllocation;.
7ebe0 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41 6c    pU8 = (u8*)pAl
7ebf0 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 65  location;.  asse
7ec00 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 76  rt( pInt[nReserv
7ec10 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d 3d  e/sizeof(int)]==
7ec20 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 20 29  (int)REARGUARD )
7ec30 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65 63  ;.  /* This chec
7ec40 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22 65  ks any of the "e
7ec50 78 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c 6f  xtra" bytes allo
7ec60 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 74  cated due.  ** t
7ec70 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 6f  o rounding up to
7ec80 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64   an 8 byte bound
7ec90 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a 20  ary to ensure . 
7eca0 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27 74   ** they haven't
7ecb0 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65   been overwritte
7ecc0 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  n..  */.  while(
7ecd0 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 2d   nReserve-- > p-
7ece0 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 74 28  >iSize ) assert(
7ecf0 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d 3d   pU8[nReserve]==
7ed00 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 6e  0x65 );.  return
7ed10 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   p;.}../*.** Ret
7ed20 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
7ed30 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c  f bytes currentl
7ed40 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61  y allocated at a
7ed50 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 61  ddress p..*/.sta
7ed60 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  tic int sqlite3M
7ed70 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b  emSize(void *p){
7ed80 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
7ed90 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 69  ckHdr *pHdr;.  i
7eda0 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74  f( !p ){.    ret
7edb0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 64  urn 0;.  }.  pHd
7edc0 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79  r = sqlite3Memsy
7edd0 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20  sGetHeader(p);. 
7ede0 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 53   return pHdr->iS
7edf0 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ize;.}../*.** In
7ee00 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
7ee10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
7ee20 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61  ubsystem..*/.sta
7ee30 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  tic int sqlite3M
7ee40 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74  emInit(void *Not
7ee50 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
7ee60 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
7ee70 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73  d);.  assert( (s
7ee80 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65 6d  izeof(struct Mem
7ee90 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d 20  BlockHdr)&7) == 
7eea0 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  0 );.  if( !sqli
7eeb0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
7eec0 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20  bMemstat ){.    
7eed0 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 61  /* If memory sta
7eee0 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  tus is enabled, 
7eef0 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 2e  then the malloc.
7ef00 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20 61  c wrapper will a
7ef10 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 6f  lready.    ** ho
7ef20 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45  ld the STATIC_ME
7ef30 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 65  M mutex when the
7ef40 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20 61   routines here a
7ef50 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a 20  re invoked. */. 
7ef60 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 73     mem.mutex = s
7ef70 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
7ef80 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
7ef90 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20  ATIC_MEM);.  }. 
7efa0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
7efb0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e  K;.}../*.** Dein
7efc0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
7efd0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
7efe0 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61  ubsystem..*/.sta
7eff0 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
7f000 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  MemShutdown(void
7f010 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
7f020 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
7f030 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e 6d  otUsed);.  mem.m
7f040 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  utex = 0;.}../*.
7f050 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65  ** Round up a re
7f060 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68  quest size to th
7f070 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c  e next valid all
7f080 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f  ocation size..*/
7f090 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
7f0a0 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e  te3MemRoundup(in
7f0b0 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52  t n){.  return R
7f0c0 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a  OUND8(n);.}../*.
7f0d0 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74  ** Allocate nByt
7f0e0 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  e bytes of memor
7f0f0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  y..*/.static voi
7f100 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  d *sqlite3MemMal
7f110 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  loc(int nByte){.
7f120 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
7f130 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f  kHdr *pHdr;.  vo
7f140 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72  id **pBt;.  char
7f150 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74   *z;.  int *pInt
7f160 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b  ;.  void *p = 0;
7f170 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65  .  int totalSize
7f180 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65  ;.  int nReserve
7f190 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
7f1a0 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65  x_enter(mem.mute
7f1b0 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65  x);.  assert( me
7f1c0 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b  m.disallow==0 );
7f1d0 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f  .  nReserve = RO
7f1e0 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 74  UND8(nByte);.  t
7f1f0 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65  otalSize = nRese
7f200 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48  rve + sizeof(*pH
7f210 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74  dr) + sizeof(int
7f220 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) +.            
7f230 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63     mem.nBacktrac
7f240 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20  e*sizeof(void*) 
7f250 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20  + mem.nTitle;.  
7f260 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c  p = malloc(total
7f270 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 29  Size);.  if( p )
7f280 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20  {.    z = p;.   
7f290 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26   pBt = (void**)&
7f2a0 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20  z[mem.nTitle];. 
7f2b0 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75 63     pHdr = (struc
7f2c0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 26  t MemBlockHdr*)&
7f2d0 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 61  pBt[mem.nBacktra
7f2e0 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70  ce];.    pHdr->p
7f2f0 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 48  Next = 0;.    pH
7f300 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d 2e  dr->pPrev = mem.
7f310 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20 6d  pLast;.    if( m
7f320 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20  em.pLast ){.    
7f330 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e 65    mem.pLast->pNe
7f340 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d  xt = pHdr;.    }
7f350 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e  else{.      mem.
7f360 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20  pFirst = pHdr;. 
7f370 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61     }.    mem.pLa
7f380 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 70  st = pHdr;.    p
7f390 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20  Hdr->iForeGuard 
7f3a0 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 20  = FOREGUARD;.   
7f3b0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
7f3c0 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 61  eSlots = mem.nBa
7f3d0 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48 64  cktrace;.    pHd
7f3e0 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d 2e  r->nTitle = mem.
7f3f0 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 20  nTitle;.    if( 
7f400 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 29  mem.nBacktrace )
7f410 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61 41  {.      void *aA
7f420 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 70  ddr[40];.      p
7f430 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20  Hdr->nBacktrace 
7f440 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 64 64  = backtrace(aAdd
7f450 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63  r, mem.nBacktrac
7f460 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d 65  e+1)-1;.      me
7f470 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 72  mcpy(pBt, &aAddr
7f480 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b  [1], pHdr->nBack
7f490 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69  trace*sizeof(voi
7f4a0 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65  d*));.      asse
7f4b0 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 20  rt(pBt[0]);.    
7f4c0 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74    if( mem.xBackt
7f4d0 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20  race ){.        
7f4e0 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e  mem.xBacktrace(n
7f4f0 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63  Byte, pHdr->nBac
7f500 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72  ktrace-1, &aAddr
7f510 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  [1]);.      }.  
7f520 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
7f530 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20  Hdr->nBacktrace 
7f540 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
7f550 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b  f( mem.nTitle ){
7f560 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c  .      memcpy(z,
7f570 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d   mem.zTitle, mem
7f580 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a  .nTitle);.    }.
7f590 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20      pHdr->iSize 
7f5a0 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a  = nByte;.    adj
7f5b0 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20  ustStats(nByte, 
7f5c0 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20  +1);.    pInt = 
7f5d0 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a  (int*)&pHdr[1];.
7f5e0 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76      pInt[nReserv
7f5f0 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d  e/sizeof(int)] =
7f600 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20   REARGUARD;.    
7f610 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 36  memset(pInt, 0x6
7f620 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20  5, nReserve);.  
7f630 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e    p = (void*)pIn
7f640 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  t;.  }.  sqlite3
7f650 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
7f660 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  .mutex);.  retur
7f670 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  n p; .}../*.** F
7f680 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73  ree memory..*/.s
7f690 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
7f6a0 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a  e3MemFree(void *
7f6b0 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63  pPrior){.  struc
7f6c0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
7f6d0 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42  Hdr;.  void **pB
7f6e0 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  t;.  char *z;.  
7f6f0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 47  assert( sqlite3G
7f700 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
7f710 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 65  stat || mem.mute
7f720 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 3d  x!=0 );.  pHdr =
7f730 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65   sqlite3MemsysGe
7f740 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b  tHeader(pPrior);
7f750 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a  .  pBt = (void**
7f760 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d 20  )pHdr;.  pBt -= 
7f770 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
7f780 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 33  Slots;.  sqlite3
7f790 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
7f7a0 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70  .mutex);.  if( p
7f7b0 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20  Hdr->pPrev ){.  
7f7c0 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e    assert( pHdr->
7f7d0 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48  pPrev->pNext==pH
7f7e0 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e  dr );.    pHdr->
7f7f0 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70  pPrev->pNext = p
7f800 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65  Hdr->pNext;.  }e
7f810 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
7f820 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64   mem.pFirst==pHd
7f830 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69  r );.    mem.pFi
7f840 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78  rst = pHdr->pNex
7f850 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64  t;.  }.  if( pHd
7f860 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  r->pNext ){.    
7f870 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e  assert( pHdr->pN
7f880 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72  ext->pPrev==pHdr
7f890 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e   );.    pHdr->pN
7f8a0 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64  ext->pPrev = pHd
7f8b0 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73  r->pPrev;.  }els
7f8c0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  e{.    assert( m
7f8d0 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29  em.pLast==pHdr )
7f8e0 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20  ;.    mem.pLast 
7f8f0 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20  = pHdr->pPrev;. 
7f900 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29   }.  z = (char*)
7f910 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72  pBt;.  z -= pHdr
7f920 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a 75  ->nTitle;.  adju
7f930 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53  stStats(pHdr->iS
7f940 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d 73  ize, -1);.  mems
7f950 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65  et(z, 0x2b, size
7f960 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e  of(void*)*pHdr->
7f970 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20  nBacktraceSlots 
7f980 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20  + sizeof(*pHdr) 
7f990 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +.              
7f9a0 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20      pHdr->iSize 
7f9b0 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20  + sizeof(int) + 
7f9c0 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20  pHdr->nTitle);. 
7f9d0 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69   free(z);.  sqli
7f9e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
7f9f0 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a  mem.mutex);  .}.
7fa00 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
7fa10 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
7fa20 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
7fa30 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46  ocation..**.** F
7fa40 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69 6e  or this debuggin
7fa50 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  g implementation
7fa60 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61  , we *always* ma
7fa70 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ke a copy of the
7fa80 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  .** allocation i
7fa90 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20  nto a new place 
7faa0 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74  in memory.  In t
7fab0 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 20  his way, if the 
7fac0 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c  .** higher level
7fad0 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70   code is using p
7fae0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c  ointer to the ol
7faf0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74  d allocation, it
7fb00 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72   is .** much mor
7fb10 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61  e likely to brea
7fb20 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63  k and we are muc
7fb30 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f  h more liking to
7fb40 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72   find.** the err
7fb50 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  or..*/.static vo
7fb60 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65  id *sqlite3MemRe
7fb70 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69  alloc(void *pPri
7fb80 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a  or, int nByte){.
7fb90 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
7fba0 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a 20  kHdr *pOldHdr;. 
7fbb0 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 61   void *pNew;.  a
7fbc0 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c  ssert( mem.disal
7fbd0 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c 64  low==0 );.  pOld
7fbe0 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d  Hdr = sqlite3Mem
7fbf0 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50 72  sysGetHeader(pPr
7fc00 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73  ior);.  pNew = s
7fc10 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28  qlite3MemMalloc(
7fc20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 4e  nByte);.  if( pN
7fc30 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  ew ){.    memcpy
7fc40 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 6e  (pNew, pPrior, n
7fc50 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 53  Byte<pOldHdr->iS
7fc60 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 4f  ize ? nByte : pO
7fc70 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20  ldHdr->iSize);. 
7fc80 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c     if( nByte>pOl
7fc90 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a 20  dHdr->iSize ){. 
7fca0 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63       memset(&((c
7fcb0 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48  har*)pNew)[pOldH
7fcc0 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 62  dr->iSize], 0x2b
7fcd0 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 64  , nByte - pOldHd
7fce0 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 7d  r->iSize);.    }
7fcf0 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46  .    sqlite3MemF
7fd00 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d  ree(pPrior);.  }
7fd10 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
7fd20 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74  }../*.** Populat
7fd30 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20  e the low-level 
7fd40 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
7fd50 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  n function point
7fd60 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65  ers in.** sqlite
7fd70 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20  3GlobalConfig.m 
7fd80 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f  with pointers to
7fd90 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   the routines in
7fda0 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53   this file..*/.S
7fdb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7fdc0 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74  id sqlite3MemSet
7fdd0 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20  Default(void){. 
7fde0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
7fdf0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
7fe00 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73  s defaultMethods
7fe10 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65   = {.     sqlite
7fe20 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20  3MemMalloc,.    
7fe30 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c   sqlite3MemFree,
7fe40 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
7fe50 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71  Realloc,.     sq
7fe60 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20  lite3MemSize,.  
7fe70 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75     sqlite3MemRou
7fe80 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74  ndup,.     sqlit
7fe90 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20  e3MemInit,.     
7fea0 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f  sqlite3MemShutdo
7feb0 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a  wn,.     0.  };.
7fec0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67    sqlite3_config
7fed0 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  (SQLITE_CONFIG_M
7fee0 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d  ALLOC, &defaultM
7fef0 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ethods);.}../*.*
7ff00 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72  * Set the number
7ff10 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65   of backtrace le
7ff20 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61  vels kept for ea
7ff30 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a  ch allocation..*
7ff40 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72  * A value of zer
7ff50 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b  o turns off back
7ff60 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75  tracing.  The nu
7ff70 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 72  mber is always r
7ff80 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20  ounded.** up to 
7ff90 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e  a multiple of 2.
7ffa0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
7ffb0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
7ffc0 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65  emdebugBacktrace
7ffd0 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69  (int depth){.  i
7ffe0 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65  f( depth<0 ){ de
7fff0 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28  pth = 0; }.  if(
80000 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70   depth>20 ){ dep
80010 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70  th = 20; }.  dep
80020 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30  th = (depth+1)&0
80030 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b  xfe;.  mem.nBack
80040 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d  trace = depth;.}
80050 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
80060 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
80070 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 61  debugBacktraceCa
80080 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42  llback(void (*xB
80090 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69  acktrace)(int, i
800a0 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20  nt, void **)){. 
800b0 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20   mem.xBacktrace 
800c0 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a  = xBacktrace;.}.
800d0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74  ./*.** Set the t
800e0 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20  itle string for 
800f0 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63  subsequent alloc
80100 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ations..*/.SQLIT
80110 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
80120 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65  qlite3MemdebugSe
80130 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61  ttitle(const cha
80140 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 6e  r *zTitle){.  un
80150 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 73  signed int n = s
80160 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
80170 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71  Title) + 1;.  sq
80180 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
80190 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  r(mem.mutex);.  
801a0 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65  if( n>=sizeof(me
801b0 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20  m.zTitle) ) n = 
801c0 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c  sizeof(mem.zTitl
801d0 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d  e)-1;.  memcpy(m
801e0 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c  em.zTitle, zTitl
801f0 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69  e, n);.  mem.zTi
80200 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65  tle[n] = 0;.  me
80210 6d 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44  m.nTitle = ROUND
80220 38 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  8(n);.  sqlite3_
80230 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e  mutex_leave(mem.
80240 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54  mutex);.}..SQLIT
80250 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
80260 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79  qlite3MemdebugSy
80270 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d  nc(){.  struct M
80280 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72  emBlockHdr *pHdr
80290 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d  ;.  for(pHdr=mem
802a0 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70  .pFirst; pHdr; p
802b0 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29  Hdr=pHdr->pNext)
802c0 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74  {.    void **pBt
802d0 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b   = (void**)pHdr;
802e0 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72  .    pBt -= pHdr
802f0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  ->nBacktraceSlot
80300 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b  s;.    mem.xBack
80310 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a  trace(pHdr->iSiz
80320 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72  e, pHdr->nBacktr
80330 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b  ace-1, &pBt[1]);
80340 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  .  }.}../*.** Op
80350 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69  en the file indi
80360 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20  cated and write 
80370 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66  a log of all unf
80380 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  reed memory .** 
80390 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f  allocations into
803a0 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51   that log..*/.SQ
803b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
803c0 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  d sqlite3Memdebu
803d0 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  gDump(const char
803e0 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20   *zFilename){.  
803f0 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72  FILE *out;.  str
80400 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
80410 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a  *pHdr;.  void **
80420 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  pBt;.  int i;.  
80430 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c  out = fopen(zFil
80440 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69  ename, "w");.  i
80450 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  f( out==0 ){.   
80460 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
80470 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
80480 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
80490 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
804a0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20  s **\n",.       
804b0 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69               zFi
804c0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  lename);.    ret
804d0 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70  urn;.  }.  for(p
804e0 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20  Hdr=mem.pFirst; 
804f0 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d  pHdr; pHdr=pHdr-
80500 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61  >pNext){.    cha
80510 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48  r *z = (char*)pH
80520 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64  dr;.    z -= pHd
80530 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f  r->nBacktraceSlo
80540 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29  ts*sizeof(void*)
80550 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b   + pHdr->nTitle;
80560 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
80570 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74  , "**** %lld byt
80580 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73  es at %p from %s
80590 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20   ****\n", .     
805a0 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69         pHdr->iSi
805b0 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48  ze, &pHdr[1], pH
805c0 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a  dr->nTitle ? z :
805d0 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28   "???");.    if(
805e0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
805f0 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73  e ){.      fflus
80600 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42  h(out);.      pB
80610 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72  t = (void**)pHdr
80620 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70  ;.      pBt -= p
80630 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53  Hdr->nBacktraceS
80640 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b  lots;.      back
80650 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64  trace_symbols_fd
80660 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63  (pBt, pHdr->nBac
80670 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f  ktrace, fileno(o
80680 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69  ut));.      fpri
80690 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a  ntf(out, "\n");.
806a0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69      }.  }.  fpri
806b0 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53  ntf(out, "COUNTS
806c0 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30  :\n");.  for(i=0
806d0 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b  ; i<NCSIZE-1; i+
806e0 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e  +){.    if( mem.
806f0 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20  nAlloc[i] ){.   
80700 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
80710 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31  "   %5d: %10d %1
80720 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20  0d %10d\n", .   
80730 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65           i*8, me
80740 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d  m.nAlloc[i], mem
80750 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65  .nCurrent[i], me
80760 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b  m.mxCurrent[i]);
80770 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
80780 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49   mem.nAlloc[NCSI
80790 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72  ZE-1] ){.    fpr
807a0 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35  intf(out, "   %5
807b0 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30  d: %10d %10d %10
807c0 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
807d0 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d     NCSIZE*8-8, m
807e0 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45  em.nAlloc[NCSIZE
807f0 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  -1],.           
80800 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e    mem.nCurrent[N
80810 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78  CSIZE-1], mem.mx
80820 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31  Current[NCSIZE-1
80830 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65  ]);.  }.  fclose
80840 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (out);.}../*.** 
80850 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
80860 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74  r of times sqlit
80870 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61  e3MemMalloc() ha
80880 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a  s been called..*
80890 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
808a0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64   int sqlite3Memd
808b0 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28  ebugMallocCount(
808c0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
808d0 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20  t nTotal = 0;.  
808e0 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a  for(i=0; i<NCSIZ
808f0 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f  E; i++){.    nTo
80900 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f  tal += mem.nAllo
80910 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  c[i];.  }.  retu
80920 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23  rn nTotal;.}...#
80930 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
80940 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a  MEMDEBUG */../**
80950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
80960 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a   of mem2.c *****
80970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
809a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
809b0 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a  in file mem3.c *
809c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
809d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
809e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
809f0 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20  ** 2007 October 
80a00 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  14.**.** The aut
80a10 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
80a20 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
80a30 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
80a40 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
80a50 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
80a60 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
80a70 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
80a80 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
80a90 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
80aa0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
80ab0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
80ac0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
80ad0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
80ae0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
80af0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
80b00 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
80b10 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
80b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80b60 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
80b70 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75  ontains the C fu
80b80 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70  nctions that imp
80b90 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a  lement a memory.
80ba0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ** allocation su
80bb0 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20  bsystem for use 
80bc0 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a  by SQLite. .**.*
80bd0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
80be0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
80bf0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
80c00 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75  m omits all.** u
80c10 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20  se of malloc(). 
80c20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20  The SQLite user 
80c30 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b  supplies a block
80c40 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65   of memory.** be
80c50 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c  fore calling sql
80c60 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
80c70 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c  ) from which all
80c80 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  ocations.** are 
80c90 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65  made and returne
80ca0 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63  d by the xMalloc
80cb0 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28  () and xRealloc(
80cc0 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ) .** implementa
80cd0 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69  tions. Once sqli
80ce0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
80cf0 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
80d00 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20  ,.** the amount 
80d10 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61  of memory availa
80d20 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73  ble to SQLite is
80d30 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f   fixed and canno
80d40 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e  t.** be changed.
80d50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  .**.** This vers
80d60 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
80d70 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
80d80 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64  system is includ
80d90 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69  ed.** in the bui
80da0 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54  ld only if SQLIT
80db0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33  E_ENABLE_MEMSYS3
80dc0 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a   is defined..**.
80dd0 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c 76  ** $Id: mem3.c,v
80de0 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f 31 39   1.25 2008/11/19
80df0 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c   16:52:44 daniel
80e00 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
80e10 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
80e20 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
80e30 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e   allocator is on
80e40 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  ly built into th
80e50 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c  e library.** SQL
80e60 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
80e70 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44  S3 is defined. D
80e80 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d  efining this sym
80e90 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  bol does not.** 
80ea0 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69  mean that the li
80eb0 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61  brary will use a
80ec0 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20   memory-pool by 
80ed0 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68  default, just th
80ee0 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69  at.** it is avai
80ef0 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f  lable. The mempo
80f00 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ol allocator is 
80f10 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 6c  activated by cal
80f20 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ling.** sqlite3_
80f30 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66  config()..*/.#if
80f40 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
80f50 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a  E_MEMSYS3../*.**
80f60 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69   Maximum size (i
80f70 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66  n Mem3Blocks) of
80f80 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b   a "small" chunk
80f90 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f  ..*/.#define MX_
80fa0 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a  SMALL 10.../*.**
80fb0 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c   Number of freel
80fc0 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a  ist hash slots.*
80fd0 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48  /.#define N_HASH
80fe0 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65    61../*.** A me
80ff0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
81000 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22  (also called a "
81010 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73  chunk") consists
81020 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d   of two or .** m
81030 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65  ore blocks where
81040 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38   each block is 8
81050 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 72   bytes.  The fir
81060 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 0a  st 8 bytes are .
81070 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 74  ** a header that
81080 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64   is not returned
81090 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a   to the user..**
810a0 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74  .** A chunk is t
810b0 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b  wo or more block
810c0 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 72  s that is either
810d0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a   checked out or.
810e0 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 69  ** free.  The fi
810f0 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f  rst block has fo
81100 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68  rmat u.hdr.  u.h
81110 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74  dr.size4x is 4 t
81120 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65  imes the.** size
81130 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
81140 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20  on in blocks if 
81150 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
81160 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75  s free..** The u
81170 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69  .hdr.size4x&1 bi
81180 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65  t is true if the
81190 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
811a0 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c  d out and.** fal
811b0 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20  se if the chunk 
811c0 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  is on the freeli
811d0 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73  st.  The u.hdr.s
811e0 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69  ize4x&2 bit.** i
811f0 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72  s true if the pr
81200 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20  evious chunk is 
81210 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20  checked out and 
81220 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20  false if the.** 
81230 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69  previous chunk i
81240 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68  s free.  The u.h
81250 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c  dr.prevSize fiel
81260 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66  d is the size of
81270 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73  .** the previous
81280 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73   chunk in blocks
81290 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
812a0 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65   chunk is on the
812b0 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66  .** freelist. If
812c0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68   the previous ch
812d0 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f  unk is checked o
812e0 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64  ut, then.** u.hd
812f0 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62  r.prevSize can b
81300 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  e part of the da
81310 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e  ta for that chun
81320 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20  k and should.** 
81330 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77  not be read or w
81340 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65  ritten..**.** We
81350 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20   often identify 
81360 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69  a chunk by its i
81370 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f  ndex in mem3.aPo
81380 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74  ol[].  When.** t
81390 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65  his is done, the
813a0 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66   chunk index ref
813b0 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e  ers to the secon
813c0 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68  d block of.** th
813d0 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69  e chunk.  In thi
813e0 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 74  s way, the first
813f0 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e   chunk has an in
81400 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63  dex of 1..** A c
81410 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20  hunk index of 0 
81420 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63  means "no such c
81430 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65  hunk" and is the
81440 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f   equivalent.** o
81450 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  f a NULL pointer
81460 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ..**.** The seco
81470 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65  nd block of free
81480 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68   chunks is of th
81490 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20  e form u.list.  
814a0 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64  The.** two field
814b0 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d  s form a double-
814c0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63  linked list of c
814d0 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64  hunks of related
814e0 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74   sizes..** Point
814f0 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 20  ers to the head 
81500 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 20  of the list are 
81510 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61  stored in mem3.a
81520 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72  iSmall[] .** for
81530 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20   smaller chunks 
81540 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b  and mem3.aiHash[
81550 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75  ] for larger chu
81560 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nks..**.** The s
81570 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61  econd block of a
81580 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64   chunk is user d
81590 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b  ata if the chunk
815a0 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20   is checked .** 
815b0 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b  out.  If a chunk
815c0 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c   is checked out,
815d0 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 6d   the user data m
815e0 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a  ay extend into.*
815f0 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76  * the u.hdr.prev
81600 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68  Size value of th
81610 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e  e following chun
81620 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  k..*/.typedef st
81630 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d  ruct Mem3Block M
81640 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74  em3Block;.struct
81650 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75   Mem3Block {.  u
81660 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63  nion {.    struc
81670 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72  t {.      u32 pr
81680 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a  evSize;   /* Siz
81690 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68  e of previous ch
816a0 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  unk in Mem3Block
816b0 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20   elements */.   
816c0 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20     u32 size4x;  
816d0 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a     /* 4x the siz
816e0 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75  e of current chu
816f0 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20  nk in Mem3Block 
81700 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  elements */.    
81710 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63  } hdr;.    struc
81720 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65  t {.      u32 ne
81730 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  xt;       /* Ind
81740 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ex in mem3.aPool
81750 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20  [] of next free 
81760 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75  chunk */.      u
81770 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f  32 prev;       /
81780 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e  * Index in mem3.
81790 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69  aPool[] of previ
817a0 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a  ous free chunk *
817b0 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20  /.    } list;.  
817c0 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  } u;.};../*.** A
817d0 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63  ll of the static
817e0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
817f0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  by this module a
81800 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20  re collected.** 
81810 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74  into a single st
81820 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d  ructure named "m
81830 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 74  em3".  This is t
81840 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
81850 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
81860 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
81870 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
81880 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
81890 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
818a0 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
818b0 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
818c0 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
818d0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
818e0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61  struct Mem3Globa
818f0 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65  l {.  /*.  ** Me
81900 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66  mory available f
81910 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e  or allocation. n
81920 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65  Pool is the size
81930 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 20   of the array.  
81940 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  ** (in Mem3Block
81950 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  s) pointed to by
81960 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20   aPool less 2.. 
81970 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b   */.  u32 nPool;
81980 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50  .  Mem3Block *aP
81990 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ool;..  /*.  ** 
819a0 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 65  True if we are e
819b0 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74  valuating an out
819c0 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62  -of-memory callb
819d0 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ack..  */.  int 
819e0 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20  alarmBusy;.  .  
819f0 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f  /*.  ** Mutex to
81a00 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20   control access 
81a10 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  to the memory al
81a20 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
81a30 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  em..  */.  sqlit
81a40 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
81a50 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  .  .  /*.  ** Th
81a60 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74  e minimum amount
81a70 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 74   of free space t
81a80 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 6e  hat we have seen
81a90 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d  ..  */.  u32 mnM
81aa0 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  aster;..  /*.  *
81ab0 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 65  * iMaster is the
81ac0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61   index of the ma
81ad0 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73  ster chunk.  Mos
81ae0 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e  t new allocation
81af0 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66  s.  ** occur off
81b00 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20   of this chunk. 
81b10 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65   szMaster is the
81b20 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c   size (in Mem3Bl
81b30 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68  ocks).  ** of th
81b40 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72  e current master
81b50 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 20  .  iMaster is 0 
81b60 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20  if there is not 
81b70 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20  master chunk..  
81b80 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 68  ** The master ch
81b90 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69  unk is not in ei
81ba0 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 5b  ther the aiHash[
81bb0 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a  ] or aiSmall[]..
81bc0 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74    */.  u32 iMast
81bd0 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74  er;.  u32 szMast
81be0 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41  er;..  /*.  ** A
81bf0 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66  rray of lists of
81c00 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63   free blocks acc
81c10 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c  ording to the bl
81c20 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66  ock size .  ** f
81c30 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b  or smaller chunk
81c40 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20  s, or a hash on 
81c50 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66  the block size f
81c60 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63  or larger.  ** c
81c70 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33  hunks..  */.  u3
81c80 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41  2 aiSmall[MX_SMA
81c90 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20  LL-1];   /* For 
81ca0 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20  sizes 2 through 
81cb0 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73  MX_SMALL, inclus
81cc0 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48  ive */.  u32 aiH
81cd0 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20  ash[N_HASH];    
81ce0 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73      /* For sizes
81cf0 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20   MX_SMALL+1 and 
81d00 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33  larger */.} mem3
81d10 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b   = { 97535575 };
81d20 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47  ..#define mem3 G
81d30 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d  LOBAL(struct Mem
81d40 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a  3Global, mem3)..
81d50 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65  /*.** Unlink the
81d60 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61   chunk at mem3.a
81d70 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73  Pool[i] from lis
81d80 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  t it is currentl
81d90 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74  y.** on.  *pRoot
81da0 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 61   is the list tha
81db0 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20  t i is a member 
81dc0 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  of..*/.static vo
81dd0 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b  id memsys3Unlink
81de0 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20  FromList(u32 i, 
81df0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75  u32 *pRoot){.  u
81e00 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61  32 next = mem3.a
81e10 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[i].u.list.n
81e20 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20  ext;.  u32 prev 
81e30 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e  = mem3.aPool[i].
81e40 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61  u.list.prev;.  a
81e50 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
81e60 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
81e70 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
81e80 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  rev==0 ){.    *p
81e90 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d  Root = next;.  }
81ea0 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61  else{.    mem3.a
81eb0 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73  Pool[prev].u.lis
81ec0 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20  t.next = next;. 
81ed0 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b   }.  if( next ){
81ee0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
81ef0 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65  next].u.list.pre
81f00 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20  v = prev;.  }.  
81f10 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
81f20 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20  list.next = 0;. 
81f30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75   mem3.aPool[i].u
81f40 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a  .list.prev = 0;.
81f50 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  }../*.** Unlink 
81f60 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64  the chunk at ind
81f70 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68  ex i from .** wh
81f80 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63  atever list is c
81f90 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65  urrently a membe
81fa0 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  r of..*/.static 
81fb0 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69  void memsys3Unli
81fc0 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32  nk(u32 i){.  u32
81fd0 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61   size, hash;.  a
81fe0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
81ff0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
82000 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
82010 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
82020 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
82030 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73   & 1)==0 );.  as
82040 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20  sert( i>=1 );.  
82050 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
82060 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
82070 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28  e4x/4;.  assert(
82080 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f   size==mem3.aPoo
82090 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64  l[i+size-1].u.hd
820a0 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20  r.prevSize );.  
820b0 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20  assert( size>=2 
820c0 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d  );.  if( size <=
820d0 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20   MX_SMALL ){.   
820e0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
820f0 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  omList(i, &mem3.
82100 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29  aiSmall[size-2])
82110 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68  ;.  }else{.    h
82120 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48  ash = size % N_H
82130 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33  ASH;.    memsys3
82140 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69  UnlinkFromList(i
82150 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
82160 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ash]);.  }.}../*
82170 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75  .** Link the chu
82180 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c  nk at mem3.aPool
82190 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f  [i] so that is o
821a0 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65  n the list roote
821b0 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a  d.** at *pRoot..
821c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
821d0 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69  emsys3LinkIntoLi
821e0 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70  st(u32 i, u32 *p
821f0 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Root){.  assert(
82200 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
82210 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
82220 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  );.  mem3.aPool[
82230 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d  i].u.list.next =
82240 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e   *pRoot;.  mem3.
82250 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
82260 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20  prev = 0;.  if( 
82270 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65  *pRoot ){.    me
82280 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d  m3.aPool[*pRoot]
82290 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69  .u.list.prev = i
822a0 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d  ;.  }.  *pRoot =
822b0 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e   i;.}../*.** Lin
822c0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69  k the chunk at i
822d0 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68  ndex i into eith
822e0 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  er the appropria
822f0 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e  te.** small chun
82300 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20  k list, or into 
82310 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20  the large chunk 
82320 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  hash table..*/.s
82330 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
82340 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20  s3Link(u32 i){. 
82350 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b   u32 size, hash;
82360 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
82370 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
82380 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  m3.mutex) );.  a
82390 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20  ssert( i>=1 );. 
823a0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61   assert( (mem3.a
823b0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
823c0 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29  size4x & 1)==0 )
823d0 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e  ;.  size = mem3.
823e0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
823f0 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73  .size4x/4;.  ass
82400 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e  ert( size==mem3.
82410 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e  aPool[i+size-1].
82420 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29  u.hdr.prevSize )
82430 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
82440 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a  >=2 );.  if( siz
82450 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b  e <= MX_SMALL ){
82460 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  .    memsys3Link
82470 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d  IntoList(i, &mem
82480 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32  3.aiSmall[size-2
82490 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ]);.  }else{.   
824a0 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e   hash = size % N
824b0 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79  _HASH;.    memsy
824c0 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69  s3LinkIntoList(i
824d0 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
824e0 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ash]);.  }.}../*
824f0 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49  .** If the STATI
82500 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e  C_MEM mutex is n
82510 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c  ot already held,
82520 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20   obtain it now. 
82530 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c  The mutex.** wil
82540 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c  l already be hel
82550 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63  d (obtained by c
82560 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29  ode in malloc.c)
82570 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c   if.** sqlite3Gl
82580 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53  obalConfig.bMemS
82590 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a  tat is true..*/.
825a0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
825b0 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a  ys3Enter(void){.
825c0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
825d0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
825e0 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75  at==0 && mem3.mu
825f0 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65  tex==0 ){.    me
82600 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  m3.mutex = sqlit
82610 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
82620 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
82630 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  _MEM);.  }.  sql
82640 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
82650 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a  (mem3.mutex);.}.
82660 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
82670 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a  ys3Leave(void){.
82680 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
82690 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78  leave(mem3.mutex
826a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  );.}../*.** Call
826b0 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 75  ed when we are u
826c0 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79  nable to satisfy
826d0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f   an allocation o
826e0 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  f nBytes..*/.sta
826f0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
82700 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20  OutOfMemory(int 
82710 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d  nByte){.  if( !m
82720 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b  em3.alarmBusy ){
82730 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42  .    mem3.alarmB
82740 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73  usy = 1;.    ass
82750 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
82760 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
82770 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ex) );.    sqlit
82780 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
82790 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em3.mutex);.    
827a0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
827b0 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20  memory(nByte);. 
827c0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
827d0 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65  _enter(mem3.mute
827e0 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61  x);.    mem3.ala
827f0 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a  rmBusy = 0;.  }.
82800 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20  }.../*.** Chunk 
82810 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 6e  i is a free chun
82820 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20  k that has been 
82830 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73  unlinked.  Adjus
82840 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70  t its .** size p
82850 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68  arameters for ch
82860 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75  eck-out and retu
82870 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
82880 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72  the .** user por
82890 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e  tion of the chun
828a0 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
828b0 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f  d *memsys3Checko
828c0 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42  ut(u32 i, u32 nB
828d0 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a  lock){.  u32 x;.
828e0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
828f0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
82900 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
82910 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20  sert( i>=1 );.  
82920 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f  assert( mem3.aPo
82930 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
82940 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29  ze4x/4==nBlock )
82950 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33  ;.  assert( mem3
82960 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
82970 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
82980 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78  e==nBlock );.  x
82990 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
829a0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b  1].u.hdr.size4x;
829b0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d  .  mem3.aPool[i-
829c0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
829d0 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c  = nBlock*4 | 1 |
829e0 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61   (x&2);.  mem3.a
829f0 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
82a00 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
82a10 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33  = nBlock;.  mem3
82a20 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
82a30 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
82a40 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26  |= 2;.  return &
82a50 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d  mem3.aPool[i];.}
82a60 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20  ../*.** Carve a 
82a70 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65  piece off of the
82a80 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33   end of the mem3
82a90 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68  .iMaster free ch
82aa0 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  unk..** Return a
82ab0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
82ac0 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  new allocation. 
82ad0 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74   Or, if the mast
82ae0 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e  er chunk.** is n
82af0 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c  ot large enough,
82b00 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74   return 0..*/.st
82b10 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79  atic void *memsy
82b20 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32  s3FromMaster(u32
82b30 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65   nBlock){.  asse
82b40 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
82b50 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
82b60 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
82b70 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e  mem3.szMaster>=n
82b80 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e  Block );.  if( n
82b90 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61  Block>=mem3.szMa
82ba0 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a  ster-1 ){.    /*
82bb0 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 20   Use the entire 
82bc0 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f  master */.    vo
82bd0 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43  id *p = memsys3C
82be0 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61  heckout(mem3.iMa
82bf0 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73  ster, mem3.szMas
82c00 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69  ter);.    mem3.i
82c10 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
82c20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20  mem3.szMaster = 
82c30 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61  0;.    mem3.mnMa
82c40 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65  ster = 0;.    re
82c50 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b  turn p;.  }else{
82c60 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68  .    /* Split th
82c70 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20  e master block. 
82c80 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c   Return the tail
82c90 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77  . */.    u32 new
82ca0 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d  i, x;.    newi =
82cb0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20   mem3.iMaster + 
82cc0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20  mem3.szMaster - 
82cd0 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65  nBlock;.    asse
82ce0 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e  rt( newi > mem3.
82cf0 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20  iMaster+1 );.   
82d00 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
82d10 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
82d20 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
82d30 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63  prevSize = nBloc
82d40 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  k;.    mem3.aPoo
82d50 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d  l[mem3.iMaster+m
82d60 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e  em3.szMaster-1].
82d70 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20  u.hdr.size4x |= 
82d80 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  2;.    mem3.aPoo
82d90 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e  l[newi-1].u.hdr.
82da0 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a  size4x = nBlock*
82db0 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e  4 + 1;.    mem3.
82dc0 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f  szMaster -= nBlo
82dd0 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ck;.    mem3.aPo
82de0 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72  ol[newi-1].u.hdr
82df0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33  .prevSize = mem3
82e00 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78  .szMaster;.    x
82e10 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65   = mem3.aPool[me
82e20 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  m3.iMaster-1].u.
82e30 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a  hdr.size4x & 2;.
82e40 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
82e50 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
82e60 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65  .hdr.size4x = me
82e70 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20  m3.szMaster*4 | 
82e80 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e  x;.    if( mem3.
82e90 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e  szMaster < mem3.
82ea0 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  mnMaster ){.    
82eb0 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20    mem3.mnMaster 
82ec0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
82ed0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
82ee0 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61  n (void*)&mem3.a
82ef0 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a  Pool[newi];.  }.
82f00 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20  }../*.** *pRoot 
82f10 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61  is the head of a
82f20 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 68   list of free ch
82f30 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65  unks of the same
82f40 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65   size.** or same
82f50 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20   size hash.  In 
82f60 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52  other words, *pR
82f70 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20  oot is an entry 
82f80 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d  in either.** mem
82f90 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d  3.aiSmall[] or m
82fa0 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a  em3.aiHash[].  .
82fb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
82fc0 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20  ne examines all 
82fd0 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67  entries on the g
82fe0 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72  iven list and tr
82ff0 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73  ies.** to coales
83000 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 20  ce each entries 
83010 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72  with adjacent fr
83020 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a  ee chunks.  .**.
83030 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 20  ** If it sees a 
83040 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61  chunk that is la
83050 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69  rger than mem3.i
83060 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61  Master, it repla
83070 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72  ces .** the curr
83080 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ent mem3.iMaster
83090 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61   with the new la
830a0 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20  rger chunk.  In 
830b0 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69  order for.** thi
830c0 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72  s mem3.iMaster r
830d0 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f  eplacement to wo
830e0 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63  rk, the master c
830f0 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20  hunk must be.** 
83100 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20  linked into the 
83110 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68  hash tables.  Th
83120 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f  at is not the no
83130 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a  rmal state of.**
83140 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75   affairs, of cou
83150 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  rse.  The callin
83160 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c  g routine must l
83170 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a  ink the master.*
83180 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69  * chunk before i
83190 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75  nvoking this rou
831a0 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20  tine, then must 
831b0 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73  unlink the (poss
831c0 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29  ibly.** changed)
831d0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e   master chunk on
831e0 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ce this routine 
831f0 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f  has finished..*/
83200 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
83210 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70  sys3Merge(u32 *p
83220 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65  Root){.  u32 iNe
83230 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20  xt, prev, size, 
83240 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28  i, x;..  assert(
83250 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
83260 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
83270 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f  );.  for(i=*pRoo
83280 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29  t; i>0; i=iNext)
83290 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65  {.    iNext = me
832a0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69  m3.aPool[i].u.li
832b0 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a  st.next;.    siz
832c0 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  e = mem3.aPool[i
832d0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
832e0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73  ;.    assert( (s
832f0 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20  ize&1)==0 );.   
83300 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30   if( (size&2)==0
83310 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73   ){.      memsys
83320 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
83330 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20  i, pRoot);.     
83340 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d   assert( i > mem
83350 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
83360 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20  dr.prevSize );. 
83370 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20       prev = i - 
83380 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
83390 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a  u.hdr.prevSize;.
833a0 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d        if( prev==
833b0 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20  iNext ){.       
833c0 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50   iNext = mem3.aP
833d0 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74  ool[prev].u.list
833e0 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  .next;.      }. 
833f0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
83400 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20  nk(prev);.      
83410 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f  size = i + size/
83420 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20  4 - prev;.      
83430 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70  x = mem3.aPool[p
83440 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  rev-1].u.hdr.siz
83450 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d  e4x & 2;.      m
83460 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31  em3.aPool[prev-1
83470 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
83480 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20   size*4 | x;.   
83490 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72     mem3.aPool[pr
834a0 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72  ev+size-1].u.hdr
834b0 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65  .prevSize = size
834c0 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c  ;.      memsys3L
834d0 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20  ink(prev);.     
834e0 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d   i = prev;.    }
834f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65  else{.      size
83500 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20   /= 4;.    }.   
83510 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73   if( size>mem3.s
83520 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20  zMaster ){.     
83530 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20   mem3.iMaster = 
83540 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  i;.      mem3.sz
83550 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20  Master = size;. 
83560 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
83570 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b  * Return a block
83580 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74   of memory of at
83590 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e   least nBytes in
835a0 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e   size..** Return
835b0 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e   NULL if unable.
835c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
835d0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
835e0 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20  t the necessary 
835f0 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c  mutexes, if any,
83600 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   are.** already 
83610 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
83620 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66  er. Hence "Unsaf
83630 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e"..*/.static vo
83640 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f  id *memsys3Mallo
83650 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74  cUnsafe(int nByt
83660 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75  e){.  u32 i;.  u
83670 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32  32 nBlock;.  u32
83680 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65   toFree;..  asse
83690 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
836a0 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
836b0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
836c0 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b  sizeof(Mem3Block
836d0 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42  )==8 );.  if( nB
836e0 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e  yte<=12 ){.    n
836f0 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c  Block = 2;.  }el
83700 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d  se{.    nBlock =
83710 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b   (nByte + 11)/8;
83720 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
83730 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f  Block>=2 );..  /
83740 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c  * STEP 1:.  ** L
83750 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79  ook for an entry
83760 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20   of the correct 
83770 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74  size in either t
83780 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68  he small.  ** ch
83790 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20  unk table or in 
837a0 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20  the large chunk 
837b0 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69  hash table.  Thi
837c0 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73  s is.  ** succes
837d0 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65  sful most of the
837e0 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74   time (about 9 t
837f0 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e  imes out of 10).
83800 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f  .  */.  if( nBlo
83810 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29  ck <= MX_SMALL )
83820 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61  {.    i = mem3.a
83830 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d  iSmall[nBlock-2]
83840 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b  ;.    if( i>0 ){
83850 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  .      memsys3Un
83860 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20  linkFromList(i, 
83870 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42  &mem3.aiSmall[nB
83880 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20  lock-2]);.      
83890 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68  return memsys3Ch
838a0 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b  eckout(i, nBlock
838b0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
838c0 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d  {.    int hash =
838d0 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48   nBlock % N_HASH
838e0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33  ;.    for(i=mem3
838f0 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69  .aiHash[hash]; i
83900 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  >0; i=mem3.aPool
83910 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [i].u.list.next)
83920 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33  {.      if( mem3
83930 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
83940 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f  r.size4x/4==nBlo
83950 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ck ){.        me
83960 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c  msys3UnlinkFromL
83970 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48  ist(i, &mem3.aiH
83980 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20  ash[hash]);.    
83990 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79      return memsy
839a0 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42  s3Checkout(i, nB
839b0 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lock);.      }. 
839c0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53     }.  }..  /* S
839d0 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20  TEP 2:.  ** Try 
839e0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61  to satisfy the a
839f0 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72  llocation by car
83a00 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66  ving a piece off
83a10 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a   of the end.  **
83a20 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   of the master c
83a30 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70  hunk.  This step
83a40 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69   usually works i
83a50 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a  f step 1 fails..
83a60 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e    */.  if( mem3.
83a70 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b  szMaster>=nBlock
83a80 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
83a90 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72  emsys3FromMaster
83aa0 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a  (nBlock);.  }...
83ab0 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20    /* STEP 3:  . 
83ac0 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   ** Loop through
83ad0 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f   the entire memo
83ae0 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73  ry pool.  Coales
83af0 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65  ce adjacent free
83b00 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52  .  ** chunks.  R
83b10 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73  ecompute the mas
83b20 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65  ter chunk as the
83b30 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 68   largest free ch
83b40 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74  unk..  ** Then t
83b50 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69  ry again to sati
83b60 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  sfy the allocati
83b70 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 20  on by carving a 
83b80 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f  piece off.  ** o
83b90 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
83ba0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20   master chunk.  
83bb0 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 6e  This step happen
83bc0 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65  s very.  ** rare
83bd0 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20  ly (we hope!).  
83be0 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d  */.  for(toFree=
83bf0 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65  nBlock*16; toFre
83c00 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36  e<(mem3.nPool*16
83c10 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b  ); toFree *= 2){
83c20 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f  .    memsys3OutO
83c30 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b  fMemory(toFree);
83c40 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d  .    if( mem3.iM
83c50 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
83c60 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e  emsys3Link(mem3.
83c70 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  iMaster);.      
83c80 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30  mem3.iMaster = 0
83c90 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d  ;.      mem3.szM
83ca0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d  aster = 0;.    }
83cb0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
83cc0 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20  N_HASH; i++){.  
83cd0 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65      memsys3Merge
83ce0 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d  (&mem3.aiHash[i]
83cf0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
83d00 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c  (i=0; i<MX_SMALL
83d10 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  -1; i++){.      
83d20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65  memsys3Merge(&me
83d30 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a  m3.aiSmall[i]);.
83d40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65      }.    if( me
83d50 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20  m3.szMaster ){. 
83d60 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
83d70 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29  nk(mem3.iMaster)
83d80 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33  ;.      if( mem3
83d90 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63  .szMaster>=nBloc
83da0 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  k ){.        ret
83db0 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d  urn memsys3FromM
83dc0 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20  aster(nBlock);. 
83dd0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
83de0 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f  ..  /* If none o
83df0 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b  f the above work
83e00 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c  ed, then we fail
83e10 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b  . */.  return 0;
83e20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
83e30 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65  n outstanding me
83e40 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  mory allocation.
83e50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
83e60 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
83e70 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20  t the necessary 
83e80 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c  mutexes, if any,
83e90 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   are.** already 
83ea0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
83eb0 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66  er. Hence "Unsaf
83ec0 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73  e"..*/.void mems
83ed0 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f  ys3FreeUnsafe(vo
83ee0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d  id *pOld){.  Mem
83ef0 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d  3Block *p = (Mem
83f00 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20  3Block*)pOld;.  
83f10 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a  int i;.  u32 siz
83f20 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20  e, x;.  assert( 
83f30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
83f40 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29  ld(mem3.mutex) )
83f50 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65  ;.  assert( p>me
83f60 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d  m3.aPool && p<&m
83f70 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e  em3.aPool[mem3.n
83f80 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70  Pool] );.  i = p
83f90 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20   - mem3.aPool;. 
83fa0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61   assert( (mem3.a
83fb0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
83fc0 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a  size4x&1)==1 );.
83fd0 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50    size = mem3.aP
83fe0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
83ff0 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72  ize4x/4;.  asser
84000 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e  t( i+size<=mem3.
84010 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d  nPool+1 );.  mem
84020 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
84030 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b  dr.size4x &= ~1;
84040 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b  .  mem3.aPool[i+
84050 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  size-1].u.hdr.pr
84060 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20  evSize = size;. 
84070 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69   mem3.aPool[i+si
84080 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  ze-1].u.hdr.size
84090 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73  4x &= ~2;.  mems
840a0 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f  ys3Link(i);..  /
840b0 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20  * Try to expand 
840c0 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67  the master using
840d0 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64   the newly freed
840e0 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20   chunk */.  if( 
840f0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a  mem3.iMaster ){.
84100 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33      while( (mem3
84110 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
84120 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
84130 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20  e4x&2)==0 ){.   
84140 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61     size = mem3.a
84150 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
84160 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  r-1].u.hdr.prevS
84170 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  ize;.      mem3.
84180 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b  iMaster -= size;
84190 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  .      mem3.szMa
841a0 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20  ster += size;.  
841b0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
841c0 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b  k(mem3.iMaster);
841d0 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e  .      x = mem3.
841e0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
841f0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
84200 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65  4x & 2;.      me
84210 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
84220 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
84230 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d  ize4x = mem3.szM
84240 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20  aster*4 | x;.   
84250 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
84260 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
84270 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
84280 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d  r.prevSize = mem
84290 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20  3.szMaster;.    
842a0 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61  }.    x = mem3.a
842b0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
842c0 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
842d0 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65  x & 2;.    while
842e0 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65  ( (mem3.aPool[me
842f0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
84300 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
84310 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29  r.size4x&1)==0 )
84320 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55  {.      memsys3U
84330 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74  nlink(mem3.iMast
84340 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
84350 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  );.      mem3.sz
84360 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61  Master += mem3.a
84370 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
84380 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d  r+mem3.szMaster-
84390 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
843a0 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  4;.      mem3.aP
843b0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
843c0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
843d0 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   = mem3.szMaster
843e0 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65  *4 | x;.      me
843f0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
84400 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73  aster+mem3.szMas
84410 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ter-1].u.hdr.pre
84420 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d  vSize = mem3.szM
84430 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  aster;.    }.  }
84440 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
84450 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20   the size of an 
84460 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f  outstanding allo
84470 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73  cation, in bytes
84480 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72  .  The.** size r
84490 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68  eturned omits th
844a0 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 20  e 8-byte header 
844b0 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 20  overhead.  This 
844c0 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f  only.** works fo
844d0 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72  r chunks that ar
844e0 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63  e currently chec
844f0 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74  ked out..*/.stat
84500 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69  ic int memsys3Si
84510 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d  ze(void *p){.  M
84520 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b  em3Block *pBlock
84530 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
84540 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63  eturn 0;.  pBloc
84550 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29  k = (Mem3Block*)
84560 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 42  p;.  assert( (pB
84570 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73  lock[-1].u.hdr.s
84580 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20  ize4x&1)!=0 );. 
84590 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b   return (pBlock[
845a0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
845b0 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f  &~3)*2 - 4;.}../
845c0 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20  *.** Round up a 
845d0 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20  request size to 
845e0 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61  the next valid a
845f0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a  llocation size..
84600 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
84610 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74  msys3Roundup(int
84620 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32   n){.  if( n<=12
84630 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
84640 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  2;.  }else{.    
84650 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e  return ((n+11)&~
84660 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f  7) - 4;.  }.}../
84670 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42  *.** Allocate nB
84680 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a  ytes of memory..
84690 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
846a0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e  memsys3Malloc(in
846b0 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c  t nBytes){.  sql
846c0 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20  ite3_int64 *p;. 
846d0 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e   assert( nBytes>
846e0 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  0 );          /*
846f0 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72   malloc.c filter
84700 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65 71  s out 0 byte req
84710 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79  uests */.  memsy
84720 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d  s3Enter();.  p =
84730 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e   memsys3MallocUn
84740 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20  safe(nBytes);.  
84750 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a  memsys3Leave();.
84760 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
84770 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  p; .}../*.** Fre
84780 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69  e memory..*/.voi
84790 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f  d memsys3Free(vo
847a0 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61  id *pPrior){.  a
847b0 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b  ssert( pPrior );
847c0 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28  .  memsys3Enter(
847d0 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65  );.  memsys3Free
847e0 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a  Unsafe(pPrior);.
847f0 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29    memsys3Leave()
84800 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
84810 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  e the size of an
84820 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79   existing memory
84830 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76   allocation.*/.v
84840 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c  oid *memsys3Real
84850 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
84860 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20  , int nBytes){. 
84870 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69   int nOld;.  voi
84880 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69  d *p;.  if( pPri
84890 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  or==0 ){.    ret
848a0 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  urn sqlite3_mall
848b0 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a  oc(nBytes);.  }.
848c0 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20    if( nBytes<=0 
848d0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
848e0 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20  ree(pPrior);.   
848f0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
84900 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53   nOld = memsys3S
84910 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69  ize(pPrior);.  i
84920 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20  f( nBytes<=nOld 
84930 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d  && nBytes>=nOld-
84940 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72  128 ){.    retur
84950 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20  n pPrior;.  }.  
84960 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a  memsys3Enter();.
84970 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c    p = memsys3Mal
84980 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73  locUnsafe(nBytes
84990 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
849a0 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65    if( nOld<nByte
849b0 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  s ){.      memcp
849c0 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c  y(p, pPrior, nOl
849d0 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
849e0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70       memcpy(p, p
849f0 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a  Prior, nBytes);.
84a00 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73      }.    memsys
84a10 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69  3FreeUnsafe(pPri
84a20 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79  or);.  }.  memsy
84a30 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74  s3Leave();.  ret
84a40 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
84a50 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  Initialize this 
84a60 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  module..*/.stati
84a70 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69  c int memsys3Ini
84a80 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
84a90 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
84aa0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
84ab0 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f   if( !sqlite3Glo
84ac0 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20  balConfig.pHeap 
84ad0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
84ae0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
84af0 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 6f  .  /* Store a po
84b00 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d  inter to the mem
84b10 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f  ory block in glo
84b20 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d 65  bal structure me
84b30 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  m3. */.  assert(
84b40 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63   sizeof(Mem3Bloc
84b50 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 2e  k)==8 );.  mem3.
84b60 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f  aPool = (Mem3Blo
84b70 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  ck *)sqlite3Glob
84b80 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a  alConfig.pHeap;.
84b90 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28    mem3.nPool = (
84ba0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
84bb0 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a 65  fig.nHeap / size
84bc0 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 2d  of(Mem3Block)) -
84bd0 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61   2;..  /* Initia
84be0 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 20  lize the master 
84bf0 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33  block. */.  mem3
84c00 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33  .szMaster = mem3
84c10 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d  .nPool;.  mem3.m
84c20 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73  nMaster = mem3.s
84c30 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 2e  zMaster;.  mem3.
84c40 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 6d  iMaster = 1;.  m
84c50 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68  em3.aPool[0].u.h
84c60 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 6d  dr.size4x = (mem
84c70 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 2b  3.szMaster<<2) +
84c80 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c   2;.  mem3.aPool
84c90 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68  [mem3.nPool].u.h
84ca0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65  dr.prevSize = me
84cb0 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33  m3.nPool;.  mem3
84cc0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f  .aPool[mem3.nPoo
84cd0 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  l].u.hdr.size4x 
84ce0 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  = 1;..  return S
84cf0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
84d00 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20  ** Deinitialize 
84d10 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  this module..*/.
84d20 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
84d30 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  ys3Shutdown(void
84d40 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
84d50 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
84d60 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
84d70 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70  n;.}..../*.** Op
84d80 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69  en the file indi
84d90 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20  cated and write 
84da0 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66  a log of all unf
84db0 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  reed memory .** 
84dc0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f  allocations into
84dd0 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51   that log..*/.SQ
84de0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
84df0 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 33  d sqlite3Memsys3
84e00 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20  Dump(const char 
84e10 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 69 66  *zFilename){.#if
84e20 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
84e30 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20  .  FILE *out;.  
84e40 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 33 32 20  u32 i, j;.  u32 
84e50 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 46 69 6c  size;.  if( zFil
84e60 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c  ename==0 || zFil
84e70 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20  ename[0]==0 ){. 
84e80 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b     out = stdout;
84e90 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75  .  }else{.    ou
84ea0 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e  t = fopen(zFilen
84eb0 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69  ame, "w");.    i
84ec0 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  f( out==0 ){.   
84ed0 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
84ee0 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f  r, "** Unable to
84ef0 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64   output memory d
84f00 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a  ebug output log:
84f10 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20   %s **\n",.     
84f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84f30 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20   zFilename);.   
84f40 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
84f50 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e  .  }.  memsys3En
84f60 74 65 72 28 29 3b 0a 20 20 66 70 72 69 6e 74 66  ter();.  fprintf
84f70 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e  (out, "CHUNKS:\n
84f80 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69  ");.  for(i=1; i
84f90 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b  <=mem3.nPool; i+
84fa0 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69  =size/4){.    si
84fb0 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
84fc0 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
84fd0 78 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f  x;.    if( size/
84fe0 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70  4<=1 ){.      fp
84ff0 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73  rintf(out, "%p s
85000 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d  ize error\n", &m
85010 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20  em3.aPool[i]);. 
85020 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29       assert( 0 )
85030 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
85040 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 73 69     }.    if( (si
85050 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d 33  ze&1)==0 && mem3
85060 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d  .aPool[i+size/4-
85070 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
85080 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 20 20  e!=size/4 ){.   
85090 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
850a0 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20 64 6f  "%p tail size do
850b0 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e 22 2c  es not match\n",
850c0 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29   &mem3.aPool[i])
850d0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
850e0 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
850f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
85100 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73  ((mem3.aPool[i+s
85110 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 73  ize/4-1].u.hdr.s
85120 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 3d 28 73  ize4x&2)>>1)!=(s
85130 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 20 20 20  ize&1) ){.      
85140 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70  fprintf(out, "%p
85150 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 74 20 62   tail checkout b
85160 69 74 20 69 73 20 69 6e 63 6f 72 72 65 63 74 5c  it is incorrect\
85170 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  n", &mem3.aPool[
85180 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  i]);.      asser
85190 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72  t( 0 );.      br
851a0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
851b0 66 28 20 73 69 7a 65 26 31 20 29 7b 0a 20 20 20  f( size&1 ){.   
851c0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
851d0 22 25 70 20 25 36 64 20 62 79 74 65 73 20 63 68  "%p %6d bytes ch
851e0 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 26 6d  ecked out\n", &m
851f0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73  em3.aPool[i], (s
85200 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20  ize/4)*8-8);.   
85210 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70   }else{.      fp
85220 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25  rintf(out, "%p %
85230 36 64 20 62 79 74 65 73 20 66 72 65 65 25 73 5c  6d bytes free%s\
85240 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  n", &mem3.aPool[
85250 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38  i], (size/4)*8-8
85260 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
85270 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73      i==mem3.iMas
85280 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72  ter ? " **master
85290 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 20 20 7d  **" : "");.    }
852a0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
852b0 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b  i<MX_SMALL-1; i+
852c0 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33  +){.    if( mem3
852d0 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29  .aiSmall[i]==0 )
852e0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66   continue;.    f
852f0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 73 6d 61  printf(out, "sma
85300 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20  ll(%2d):", i);. 
85310 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e     for(j = mem3.
85320 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b  aiSmall[i]; j>0;
85330 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d   j=mem3.aPool[j]
85340 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20  .u.list.next){. 
85350 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
85360 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65  , " %p(%d)", &me
85370 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20  m3.aPool[j],.   
85380 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33             (mem3
85390 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64  .aPool[j-1].u.hd
853a0 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29  r.size4x/4)*8-8)
853b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69  ;.    }.    fpri
853c0 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20  ntf(out, "\n"); 
853d0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
853e0 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a  i<N_HASH; i++){.
853f0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 48      if( mem3.aiH
85400 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  ash[i]==0 ) cont
85410 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74  inue;.    fprint
85420 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64  f(out, "hash(%2d
85430 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72  ):", i);.    for
85440 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68  (j = mem3.aiHash
85450 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33  [i]; j>0; j=mem3
85460 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74  .aPool[j].u.list
85470 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70  .next){.      fp
85480 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28  rintf(out, " %p(
85490 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f  %d)", &mem3.aPoo
854a0 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  l[j],.          
854b0 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b      (mem3.aPool[
854c0 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  j-1].u.hdr.size4
854d0 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d  x/4)*8-8);.    }
854e0 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
854f0 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20  , "\n"); .  }.  
85500 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 61  fprintf(out, "ma
85510 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33  ster=%d\n", mem3
85520 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 66 70 72  .iMaster);.  fpr
85530 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f 77 55 73  intf(out, "nowUs
85540 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e  ed=%d\n", mem3.n
85550 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a  Pool*8 - mem3.sz
85560 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72  Master*8);.  fpr
85570 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65  intf(out, "mxUse
85580 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50  d=%d\n", mem3.nP
85590 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d  ool*8 - mem3.mnM
855a0 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 71 6c 69  aster*8);.  sqli
855b0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
855c0 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 69  mem3.mutex);.  i
855d0 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29  f( out==stdout )
855e0 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74 64  {.    fflush(std
855f0 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  out);.  }else{. 
85600 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a     fclose(out);.
85610 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53    }.#else.  UNUS
85620 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69  ED_PARAMETER(zFi
85630 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a  lename);.#endif.
85640 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
85650 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c  utine is the onl
85660 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69  y routine in thi
85670 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65  s file with exte
85680 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65  rnal .** linkage
85690 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65  ..**.** Populate
856a0 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d   the low-level m
856b0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
856c0 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65   function pointe
856d0 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  rs in.** sqlite3
856e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77  GlobalConfig.m w
856f0 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ith pointers to 
85700 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  the routines in 
85710 74 68 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a  this file. The.*
85720 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63  * arguments spec
85730 69 66 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66  ify the block of
85740 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67   memory to manag
85750 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
85760 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61  utine is only ca
85770 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  lled by sqlite3_
85780 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68  config(), and th
85790 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f  erefore.** is no
857a0 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65  t required to be
857b0 20 74 68 72 65 61 64 73 61 66 65 20 28 69 74 20   threadsafe (it 
857c0 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49  is not)..*/.SQLI
857d0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
857e0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
857f0 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d  hods *sqlite3Mem
85800 47 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29  GetMemsys3(void)
85810 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
85820 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
85830 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68  hods mempoolMeth
85840 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d  ods = {.     mem
85850 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20  sys3Malloc,.    
85860 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20   memsys3Free,.  
85870 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f     memsys3Reallo
85880 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53  c,.     memsys3S
85890 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  ize,.     memsys
858a0 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d  3Roundup,.     m
858b0 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20  emsys3Init,.    
858c0 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e   memsys3Shutdown
858d0 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20  ,.     0.  };.  
858e0 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d  return &mempoolM
858f0 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69  ethods;.}..#endi
85900 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  f /* SQLITE_ENAB
85910 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f  LE_MEMSYS3 */../
85920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
85930 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a  nd of mem3.c ***
85940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
85970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
85980 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63  egin file mem5.c
85990 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
859a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
859b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
859c0 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65  *.** 2007 Octobe
859d0 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 14.**.** The a
859e0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
859f0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
85a00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
85a10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
85a20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
85a30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
85a40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
85a50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
85a60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
85a70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
85a80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
85a90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
85aa0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
85ab0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
85ac0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
85ad0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
85ae0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
85af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85b30 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
85b40 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
85b50 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
85b60 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72  mplement a memor
85b70 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
85b80 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73  subsystem for us
85b90 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a  e by SQLite. .**
85ba0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
85bb0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
85bc0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
85bd0 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a  tem omits all.**
85be0 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29   use of malloc()
85bf0 2e 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65  . The SQLite use
85c00 72 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f  r supplies a blo
85c10 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ck of memory.** 
85c20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73  before calling s
85c30 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
85c40 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61  e() from which a
85c50 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72  llocations.** ar
85c60 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72  e made and retur
85c70 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c  ned by the xMall
85c80 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f  oc() and xReallo
85c90 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  c() .** implemen
85ca0 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71  tations. Once sq
85cb0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
85cc0 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  () has been call
85cd0 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e  ed,.** the amoun
85ce0 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69  t of memory avai
85cf0 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20  lable to SQLite 
85d00 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e  is fixed and can
85d10 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65  not.** be change
85d20 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65  d..**.** This ve
85d30 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  rsion of the mem
85d40 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
85d50 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c  ubsystem is incl
85d60 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62  uded.** in the b
85d70 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c  uild only if SQL
85d80 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
85d90 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  S5 is defined..*
85da0 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 35 2e 63  *.** $Id: mem5.c
85db0 2c 76 20 31 2e 31 39 20 32 30 30 38 2f 31 31 2f  ,v 1.19 2008/11/
85dc0 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69  19 16:52:44 dani
85dd0 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
85de0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
85df0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
85e00 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
85e10 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a  used only when .
85e20 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
85e30 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69  _MEMSYS5 is defi
85e40 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ned..*/.#ifdef S
85e50 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
85e60 53 59 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69  SYS5../*.** A mi
85e70 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e  nimum allocation
85e80 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
85e90 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
85ea0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c   structure..** L
85eb0 61 72 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  arger allocation
85ec0 73 20 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f  s are an array o
85ed0 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
85ee0 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20  es where the.** 
85ef0 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61  size of the arra
85f00 79 20 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20  y is a power of 
85f10 32 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  2..*/.typedef st
85f20 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65  ruct Mem5Link Me
85f30 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d  m5Link;.struct M
85f40 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20  em5Link {.  int 
85f50 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49  next;       /* I
85f60 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65  ndex of next fre
85f70 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74  e chunk */.  int
85f80 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20   prev;       /* 
85f90 49 6e 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75  Index of previou
85fa0 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a  s free chunk */.
85fb0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75  };../*.** Maximu
85fc0 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c  m size of any al
85fd0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c  location is ((1<
85fe0 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 6e 41  <LOGMAX)*mem5.nA
85ff0 74 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d  tom). Since.** m
86000 65 6d 35 2e 6e 41 74 6f 6d 20 69 73 20 61 6c 77  em5.nAtom is alw
86010 61 79 73 20 61 74 20 6c 65 61 73 74 20 38 2c 20  ays at least 8, 
86020 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c  this is not real
86030 6c 79 20 61 20 70 72 61 63 74 69 63 61 6c 0a 2a  ly a practical.*
86040 2a 20 6c 69 6d 69 74 61 74 69 6f 6e 2e 0a 2a 2f  * limitation..*/
86050 0a 23 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20  .#define LOGMAX 
86060 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20  30../*.** Masks 
86070 75 73 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43  used for mem5.aC
86080 74 72 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a  trl[] elements..
86090 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f  */.#define CTRL_
860a0 4c 4f 47 53 49 5a 45 20 20 30 78 31 66 20 20 20  LOGSIZE  0x1f   
860b0 20 2f 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66   /* Log2 Size of
860c0 20 74 68 69 73 20 62 6c 6f 63 6b 20 72 65 6c 61   this block rela
860d0 74 69 76 65 20 74 6f 20 50 4f 57 32 5f 4d 49 4e  tive to POW2_MIN
860e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c   */.#define CTRL
860f0 5f 46 52 45 45 20 20 20 20 20 30 78 32 30 20 20  _FREE     0x20  
86100 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74    /* True if not
86110 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
86120 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ./*.** All of th
86130 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
86140 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  es used by this 
86150 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65  module are colle
86160 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73  cted.** into a s
86170 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20  ingle structure 
86180 6e 61 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54  named "mem5".  T
86190 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74  his is to keep t
861a0 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72  he.** static var
861b0 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64  iables organized
861c0 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e   and to reduce n
861d0 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69  amespace polluti
861e0 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  on.** when this 
861f0 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e  module is combin
86200 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e  ed with other in
86210 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f   the amalgamatio
86220 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c  n..*/.static SQL
86230 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d  ITE_WSD struct M
86240 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a  em5Global {.  /*
86250 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61  .  ** Memory ava
86260 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63  ilable for alloc
86270 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74  ation.  */.  int
86280 20 6e 41 74 6f 6d 3b 20 20 20 20 20 20 20 2f 2a   nAtom;       /*
86290 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62   Smallest possib
862a0 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  le allocation in
862b0 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20   bytes */.  int 
862c0 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20  nBlock;      /* 
862d0 4e 75 6d 62 65 72 20 6f 66 20 6e 41 74 6f 6d 20  Number of nAtom 
862e0 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20  sized blocks in 
862f0 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a  zPool */.  u8 *z
86300 50 6f 6f 6c 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20  Pool;.  .  /*.  
86310 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74  ** Mutex to cont
86320 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68  rol access to th
86330 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
86340 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20  ion subsystem.. 
86350 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
86360 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f  tex *mutex;..  /
86370 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e  *.  ** Performan
86380 63 65 20 73 74 61 74 69 73 74 69 63 73 0a 20 20  ce statistics.  
86390 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b  */.  u64 nAlloc;
863a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
863b0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c  l number of call
863c0 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20  s to malloc */. 
863d0 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b   u64 totalAlloc;
863e0 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66       /* Total of
863f0 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c   all malloc call
86400 73 20 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74  s - includes int
86410 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20  ernal frag */.  
86420 75 36 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b  u64 totalExcess;
86430 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74      /* Total int
86440 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74  ernal fragmentat
86450 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72  ion */.  u32 cur
86460 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20  rentOut;     /* 
86470 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74  Current checkout
86480 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65  , including inte
86490 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69  rnal fragmentati
864a0 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72  on */.  u32 curr
864b0 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43  entCount;   /* C
864c0 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
864d0 20 64 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f   distinct checko
864e0 75 74 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78  uts */.  u32 max
864f0 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Out;         /* 
86500 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61  Maximum instanta
86510 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74  neous currentOut
86520 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75   */.  u32 maxCou
86530 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78  nt;       /* Max
86540 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f  imum instantaneo
86550 75 73 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20  us currentCount 
86560 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75  */.  u32 maxRequ
86570 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67  est;     /* Larg
86580 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28  est allocation (
86590 65 78 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74  exclusive of int
865a0 65 72 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20  ernal frag) */. 
865b0 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74   .  /*.  ** List
865c0 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73  s of free blocks
865d0 20 6f 66 20 76 61 72 69 6f 75 73 20 73 69 7a 65   of various size
865e0 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69  s..  */.  int ai
865f0 46 72 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b  Freelist[LOGMAX+
86600 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53  1];..  /*.  ** S
86610 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e  pace for trackin
86620 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61  g which blocks a
86630 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61  re checked out a
86640 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a  nd the size.  **
86650 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20   of each block. 
86660 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c   One byte per bl
86670 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a  ock..  */.  u8 *
86680 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d  aCtrl;..} mem5 =
86690 20 7b 20 31 39 38 30 34 31 36 37 20 7d 3b 0a 0a   { 19804167 };..
866a0 23 64 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c 4f  #define mem5 GLO
866b0 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 47  BAL(struct Mem5G
866c0 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 23 64  lobal, mem5)..#d
866d0 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69  efine MEM5LINK(i
866e0 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a  dx) ((Mem5Link *
866f0 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69  )(&mem5.zPool[(i
86700 64 78 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 29  dx)*mem5.nAtom])
86710 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  )../*.** Unlink 
86720 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d  the chunk at mem
86730 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20  5.aPool[i] from 
86740 6c 69 73 74 20 69 74 20 69 73 20 63 75 72 72 65  list it is curre
86750 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20  ntly.** on.  It 
86760 73 68 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 20  should be found 
86770 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69  on mem5.aiFreeli
86780 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f  st[iLogsize]..*/
86790 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
867a0 73 79 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69  sys5Unlink(int i
867b0 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b  , int iLogsize){
867c0 0a 20 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65  .  int next, pre
867d0 76 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  v;.  assert( i>=
867e0 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f  0 && i<mem5.nBlo
867f0 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ck );.  assert( 
86800 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69  iLogsize>=0 && i
86810 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20  Logsize<=LOGMAX 
86820 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
86830 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54  m5.aCtrl[i] & CT
86840 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f  RL_LOGSIZE)==iLo
86850 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74  gsize );..  next
86860 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e   = MEM5LINK(i)->
86870 6e 65 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d  next;.  prev = M
86880 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76  EM5LINK(i)->prev
86890 3b 0a 20 20 69 66 28 20 70 72 65 76 3c 30 20 29  ;.  if( prev<0 )
868a0 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65  {.    mem5.aiFre
868b0 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20  elist[iLogsize] 
868c0 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b  = next;.  }else{
868d0 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72  .    MEM5LINK(pr
868e0 65 76 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74  ev)->next = next
868f0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74  ;.  }.  if( next
86900 3e 3d 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c  >=0 ){.    MEM5L
86910 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 20  INK(next)->prev 
86920 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f  = prev;.  }.}../
86930 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68  *.** Link the ch
86940 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f  unk at mem5.aPoo
86950 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20  l[i] so that is 
86960 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a  on the iLogsize.
86970 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f  ** free list..*/
86980 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
86990 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20  sys5Link(int i, 
869a0 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20  int iLogsize){. 
869b0 20 69 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 74   int x;.  assert
869c0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
869d0 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29  held(mem5.mutex)
869e0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e   );.  assert( i>
869f0 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c  =0 && i<mem5.nBl
86a00 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ock );.  assert(
86a10 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20   iLogsize>=0 && 
86a20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58  iLogsize<=LOGMAX
86a30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d   );.  assert( (m
86a40 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43  em5.aCtrl[i] & C
86a50 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c  TRL_LOGSIZE)==iL
86a60 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d  ogsize );..  x =
86a70 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65   MEM5LINK(i)->ne
86a80 78 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65  xt = mem5.aiFree
86a90 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a  list[iLogsize];.
86aa0 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70    MEM5LINK(i)->p
86ab0 72 65 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20  rev = -1;.  if( 
86ac0 78 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  x>=0 ){.    asse
86ad0 72 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63  rt( x<mem5.nBloc
86ae0 6b 20 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e  k );.    MEM5LIN
86af0 4b 28 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a  K(x)->prev = i;.
86b00 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65    }.  mem5.aiFre
86b10 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20  elist[iLogsize] 
86b20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  = i;.}../*.** If
86b30 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20   the STATIC_MEM 
86b40 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72  mutex is not alr
86b50 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69  eady held, obtai
86b60 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75  n it now. The mu
86b70 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65  tex.** will alre
86b80 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74  ady be held (obt
86b90 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e  ained by code in
86ba0 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a   malloc.c) if.**
86bb0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
86bc0 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73  nfig.bMemStat is
86bd0 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   true..*/.static
86be0 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74   void memsys5Ent
86bf0 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  er(void){.  if( 
86c00 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
86c10 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20  fig.bMemstat==0 
86c20 26 26 20 6d 65 6d 35 2e 6d 75 74 65 78 3d 3d 30  && mem5.mutex==0
86c30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74   ){.    mem5.mut
86c40 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
86c50 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
86c60 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b  TEX_STATIC_MEM);
86c70 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
86c80 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e  utex_enter(mem5.
86c90 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63  mutex);.}.static
86ca0 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 65 61   void memsys5Lea
86cb0 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ve(void){.  sqli
86cc0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
86cd0 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a  mem5.mutex);.}..
86ce0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
86cf0 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73   size of an outs
86d00 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69  tanding allocati
86d10 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54  on, in bytes.  T
86d20 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72  he.** size retur
86d30 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d  ned omits the 8-
86d40 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 65 72  byte header over
86d50 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79  head.  This only
86d60 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68  .** works for ch
86d70 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 63 75  unks that are cu
86d80 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20  rrently checked 
86d90 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  out..*/.static i
86da0 6e 74 20 6d 65 6d 73 79 73 35 53 69 7a 65 28 76  nt memsys5Size(v
86db0 6f 69 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  oid *p){.  int i
86dc0 53 69 7a 65 20 3d 20 30 3b 0a 20 20 69 66 28 20  Size = 0;.  if( 
86dd0 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d  p ){.    int i =
86de0 20 28 28 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a   ((u8 *)p-mem5.z
86df0 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d  Pool)/mem5.nAtom
86e00 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e  ;.    assert( i>
86e10 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c  =0 && i<mem5.nBl
86e20 6f 63 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65  ock );.    iSize
86e30 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 2a 20   = mem5.nAtom * 
86e40 28 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72  (1 << (mem5.aCtr
86e50 6c 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a  l[i]&CTRL_LOGSIZ
86e60 45 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  E));.  }.  retur
86e70 6e 20 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n iSize;.}../*.*
86e80 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74  * Find the first
86e90 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72   entry on the fr
86ea0 65 65 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e  eelist iLogsize.
86eb0 20 20 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a    Unlink that.**
86ec0 20 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72   entry and retur
86ed0 6e 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f  n its index. .*/
86ee0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
86ef0 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69  ys5UnlinkFirst(i
86f00 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20  nt iLogsize){.  
86f10 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69  int i;.  int iFi
86f20 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rst;..  assert( 
86f30 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69  iLogsize>=0 && i
86f40 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20  Logsize<=LOGMAX 
86f50 29 3b 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20  );.  i = iFirst 
86f60 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73  = mem5.aiFreelis
86f70 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61  t[iLogsize];.  a
86f80 73 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30  ssert( iFirst>=0
86f90 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30   );.  while( i>0
86fa0 20 29 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46   ){.    if( i<iF
86fb0 69 72 73 74 20 29 20 69 46 69 72 73 74 20 3d 20  irst ) iFirst = 
86fc0 69 3b 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c  i;.    i = MEM5L
86fd0 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20  INK(i)->next;.  
86fe0 7d 0a 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e  }.  memsys5Unlin
86ff0 6b 28 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69  k(iFirst, iLogsi
87000 7a 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46  ze);.  return iF
87010 69 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  irst;.}../*.** R
87020 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66  eturn a block of
87030 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65   memory of at le
87040 61 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69  ast nBytes in si
87050 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55  ze..** Return NU
87060 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2f  LL if unable..*/
87070 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
87080 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66  msys5MallocUnsaf
87090 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  e(int nByte){.  
870a0 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
870b0 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d   /* Index of a m
870c0 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74  em5.aPool[] slot
870d0 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20   */.  int iBin; 
870e0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
870f0 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65  into mem5.aiFree
87100 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  list[] */.  int 
87110 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20  iFullSz;     /* 
87120 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69  Size of allocati
87130 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f  on rounded up to
87140 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20   power of 2 */. 
87150 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20   int iLogsize;  
87160 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75    /* Log2 of iFu
87170 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f  llSz/POW2_MIN */
87180 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 74 72 61 63  ..  /* Keep trac
87190 6b 20 6f 66 20 74 68 65 20 6d 61 78 69 6d 75 6d  k of the maximum
871a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
871b0 65 73 74 2e 20 20 45 76 65 6e 20 75 6e 66 75 6c  est.  Even unful
871c0 66 69 6c 6c 65 64 0a 20 20 2a 2a 20 72 65 71 75  filled.  ** requ
871d0 65 73 74 73 20 61 72 65 20 63 6f 75 6e 74 65 64  ests are counted
871e0 20 2a 2f 0a 20 20 69 66 28 20 28 75 33 32 29 6e   */.  if( (u32)n
871f0 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 52 65 71  Byte>mem5.maxReq
87200 75 65 73 74 20 29 7b 0a 20 20 20 20 6d 65 6d 35  uest ){.    mem5
87210 2e 6d 61 78 52 65 71 75 65 73 74 20 3d 20 6e 42  .maxRequest = nB
87220 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  yte;.  }..  /* R
87230 6f 75 6e 64 20 6e 42 79 74 65 20 75 70 20 74 6f  ound nByte up to
87240 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20   the next valid 
87250 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a  power of two */.
87260 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65    for(iFullSz=me
87270 6d 35 2e 6e 41 74 6f 6d 2c 20 69 4c 6f 67 73 69  m5.nAtom, iLogsi
87280 7a 65 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42  ze=0; iFullSz<nB
87290 79 74 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20  yte; iFullSz *= 
872a0 32 2c 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d  2, iLogsize++){}
872b0 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
872c0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
872d0 5b 69 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61  [iLogsize] conta
872e0 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  ins at least one
872f0 20 66 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b   free.  ** block
87300 2e 20 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20  .  If not, then 
87310 73 70 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66  split a block of
87320 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72   the next larger
87330 20 70 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74   power of.  ** t
87340 77 6f 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63  wo in order to c
87350 72 65 61 74 65 20 61 20 6e 65 77 20 66 72 65 65  reate a new free
87360 20 62 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69   block of size i
87370 4c 6f 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20  Logsize..  */.  
87380 66 6f 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a  for(iBin=iLogsiz
87390 65 3b 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69  e; mem5.aiFreeli
873a0 73 74 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42  st[iBin]<0 && iB
873b0 69 6e 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e  in<=LOGMAX; iBin
873c0 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e  ++){}.  if( iBin
873d0 3e 4c 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e  >LOGMAX ) return
873e0 20 30 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73   0;.  i = memsys
873f0 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69  5UnlinkFirst(iBi
87400 6e 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69  n);.  while( iBi
87410 6e 3e 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20  n>iLogsize ){.  
87420 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a    int newSize;..
87430 20 20 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20      iBin--;.    
87440 6e 65 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69  newSize = 1 << i
87450 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43  Bin;.    mem5.aC
87460 74 72 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d  trl[i+newSize] =
87470 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69   CTRL_FREE | iBi
87480 6e 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69  n;.    memsys5Li
87490 6e 6b 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42  nk(i+newSize, iB
874a0 69 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e  in);.  }.  mem5.
874b0 61 43 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73  aCtrl[i] = iLogs
874c0 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74  ize;..  /* Updat
874d0 65 20 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66  e allocator perf
874e0 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69  ormance statisti
874f0 63 73 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41  cs. */.  mem5.nA
87500 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74  lloc++;.  mem5.t
87510 6f 74 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75  otalAlloc += iFu
87520 6c 6c 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74  llSz;.  mem5.tot
87530 61 6c 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c  alExcess += iFul
87540 6c 53 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d  lSz - nByte;.  m
87550 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
87560 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65  ++;.  mem5.curre
87570 6e 74 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a  ntOut += iFullSz
87580 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78  ;.  if( mem5.max
87590 43 6f 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65  Count<mem5.curre
875a0 6e 74 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d  ntCount ) mem5.m
875b0 61 78 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63  axCount = mem5.c
875c0 75 72 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69  urrentCount;.  i
875d0 66 28 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d  f( mem5.maxOut<m
875e0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29  em5.currentOut )
875f0 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d   mem5.maxOut = m
87600 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a  em5.currentOut;.
87610 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70  .  /* Return a p
87620 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c  ointer to the al
87630 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20  located memory. 
87640 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69  */.  return (voi
87650 64 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69  d*)&mem5.zPool[i
87660 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 7d 0a  *mem5.nAtom];.}.
87670 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f  ./*.** Free an o
87680 75 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72  utstanding memor
87690 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f  y allocation..*/
876a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
876b0 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 76  sys5FreeUnsafe(v
876c0 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33  oid *pOld){.  u3
876d0 32 20 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65  2 size, iLogsize
876e0 3b 0a 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 20  ;.  int iBlock; 
876f0 20 20 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20              ..  
87700 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 74 6f  /* Set iBlock to
87710 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
87720 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 20  e block pointed 
87730 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a 20  to by pOld in . 
87740 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 6f 66   ** the array of
87750 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 62 79 74 65   mem5.nAtom byte
87760 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 64 20   blocks pointed 
87770 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c  to by mem5.zPool
87780 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20  ..  */.  iBlock 
87790 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65  = ((u8 *)pOld-me
877a0 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e  m5.zPool)/mem5.n
877b0 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  Atom;..  /* Chec
877c0 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74  k that the point
877d0 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74  er pOld points t
877e0 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66  o a valid, non-f
877f0 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20  ree block. */.  
87800 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d  assert( iBlock>=
87810 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35  0 && iBlock<mem5
87820 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73  .nBlock );.  ass
87830 65 72 74 28 20 28 28 75 38 20 2a 29 70 4f 6c 64  ert( ((u8 *)pOld
87840 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d  -mem5.zPool)%mem
87850 35 2e 6e 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20  5.nAtom==0 );.  
87860 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43  assert( (mem5.aC
87870 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54  trl[iBlock] & CT
87880 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a  RL_FREE)==0 );..
87890 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d    iLogsize = mem
878a0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
878b0 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a  & CTRL_LOGSIZE;.
878c0 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67    size = 1<<iLog
878d0 73 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20  size;.  assert( 
878e0 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75  iBlock+size-1<(u
878f0 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29  32)mem5.nBlock )
87900 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b  ;..  mem5.aCtrl[
87910 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f  iBlock] |= CTRL_
87920 46 52 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74  FREE;.  mem5.aCt
87930 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31  rl[iBlock+size-1
87940 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a  ] |= CTRL_FREE;.
87950 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63    assert( mem5.c
87960 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b  urrentCount>0 );
87970 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e  .  assert( mem5.
87980 63 75 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a  currentOut>=(siz
87990 65 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 29 20 29 3b  e*mem5.nAtom) );
879a0 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43  .  mem5.currentC
879b0 6f 75 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63  ount--;.  mem5.c
879c0 75 72 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a  urrentOut -= siz
879d0 65 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20  e*mem5.nAtom;.  
879e0 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72  assert( mem5.cur
879f0 72 65 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d  rentOut>0 || mem
87a00 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d  5.currentCount==
87a10 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  0 );.  assert( m
87a20 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
87a30 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65  >0 || mem5.curre
87a40 6e 74 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d  ntOut==0 );..  m
87a50 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b  em5.aCtrl[iBlock
87a60 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20  ] = CTRL_FREE | 
87a70 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c  iLogsize;.  whil
87a80 65 28 20 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d  e( iLogsize<LOGM
87a90 41 58 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42  AX ){.    int iB
87aa0 75 64 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69  uddy;.    if( (i
87ab0 42 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29  Block>>iLogsize)
87ac0 20 26 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42   & 1 ){.      iB
87ad0 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20  uddy = iBlock - 
87ae0 73 69 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  size;.    }else{
87af0 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20  .      iBuddy = 
87b00 69 42 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20  iBlock + size;. 
87b10 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
87b20 20 69 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20   iBuddy>=0 );.  
87b30 20 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31    if( (iBuddy+(1
87b40 3c 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d  <<iLogsize))>mem
87b50 35 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b  5.nBlock ) break
87b60 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61  ;.    if( mem5.a
87b70 43 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43  Ctrl[iBuddy]!=(C
87b80 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73  TRL_FREE | iLogs
87b90 69 7a 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  ize) ) break;.  
87ba0 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28    memsys5Unlink(
87bb0 69 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65  iBuddy, iLogsize
87bc0 29 3b 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b  );.    iLogsize+
87bd0 2b 3b 0a 20 20 20 20 69 66 28 20 69 42 75 64 64  +;.    if( iBudd
87be0 79 3c 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20  y<iBlock ){.    
87bf0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75    mem5.aCtrl[iBu
87c00 64 64 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45  ddy] = CTRL_FREE
87c10 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20   | iLogsize;.   
87c20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42     mem5.aCtrl[iB
87c30 6c 6f 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20  lock] = 0;.     
87c40 20 69 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79   iBlock = iBuddy
87c50 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
87c60 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42     mem5.aCtrl[iB
87c70 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45  lock] = CTRL_FRE
87c80 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20  E | iLogsize;.  
87c90 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69      mem5.aCtrl[i
87ca0 42 75 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20  Buddy] = 0;.    
87cb0 7d 0a 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b  }.    size *= 2;
87cc0 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69  .  }.  memsys5Li
87cd0 6e 6b 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73  nk(iBlock, iLogs
87ce0 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ize);.}../*.** A
87cf0 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f  llocate nBytes o
87d00 66 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74  f memory.*/.stat
87d10 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35  ic void *memsys5
87d20 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65  Malloc(int nByte
87d30 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  s){.  sqlite3_in
87d40 74 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66  t64 *p = 0;.  if
87d50 28 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20  ( nBytes>0 ){.  
87d60 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29    memsys5Enter()
87d70 3b 0a 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73  ;.    p = memsys
87d80 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42  5MallocUnsafe(nB
87d90 79 74 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79  ytes);.    memsy
87da0 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20  s5Leave();.  }. 
87db0 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70   return (void*)p
87dc0 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  ; .}../*.** Free
87dd0 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74   memory..*/.stat
87de0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46  ic void memsys5F
87df0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
87e00 29 7b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d  ){.  if( pPrior=
87e10 3d 30 20 29 7b 0a 61 73 73 65 72 74 28 30 29 3b  =0 ){.assert(0);
87e20 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
87e30 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
87e40 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 65  );.  memsys5Free
87e50 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a  Unsafe(pPrior);.
87e60 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29    memsys5Leave()
87e70 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  ;  .}../*.** Cha
87e80 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  nge the size of 
87e90 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f  an existing memo
87ea0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f  ry allocation.*/
87eb0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
87ec0 6d 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69  msys5Realloc(voi
87ed0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e  d *pPrior, int n
87ee0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f  Bytes){.  int nO
87ef0 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  ld;.  void *p;. 
87f00 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29   if( pPrior==0 )
87f10 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d  {.    return mem
87f20 73 79 73 35 4d 61 6c 6c 6f 63 28 6e 42 79 74 65  sys5Malloc(nByte
87f30 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42  s);.  }.  if( nB
87f40 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 6d  ytes<=0 ){.    m
87f50 65 6d 73 79 73 35 46 72 65 65 28 70 50 72 69 6f  emsys5Free(pPrio
87f60 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  r);.    return 0
87f70 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d  ;.  }.  nOld = m
87f80 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f  emsys5Size(pPrio
87f90 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  r);.  if( nBytes
87fa0 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65  <=nOld ){.    re
87fb0 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d  turn pPrior;.  }
87fc0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
87fd0 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35  );.  p = memsys5
87fe0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79  MallocUnsafe(nBy
87ff0 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  tes);.  if( p ){
88000 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70  .    memcpy(p, p
88010 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20  Prior, nOld);.  
88020 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73    memsys5FreeUns
88030 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d  afe(pPrior);.  }
88040 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
88050 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
88060 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70  ../*.** Round up
88070 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20   a request size 
88080 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69  to the next vali
88090 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  d allocation siz
880a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
880b0 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28   memsys5Roundup(
880c0 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46  int n){.  int iF
880d0 75 6c 6c 53 7a 3b 0a 20 20 66 6f 72 28 69 46 75  ullSz;.  for(iFu
880e0 6c 6c 53 7a 3d 6d 65 6d 35 2e 6e 41 74 6f 6d 3b  llSz=mem5.nAtom;
880f0 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c   iFullSz<n; iFul
88100 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74  lSz *= 2);.  ret
88110 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a  urn iFullSz;.}..
88120 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
88130 73 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65  s5Log(int iValue
88140 29 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20  ){.  int iLog;. 
88150 20 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c   for(iLog=0; (1<
88160 3c 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69  <iLog)<iValue; i
88170 4c 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e  Log++);.  return
88180 20 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   iLog;.}../*.** 
88190 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  Initialize this 
881a0 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  module..*/.stati
881b0 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69  c int memsys5Ini
881c0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
881d0 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e  {.  int ii;.  in
881e0 74 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65  t nByte = sqlite
881f0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48  3GlobalConfig.nH
88200 65 61 70 3b 0a 20 20 75 38 20 2a 7a 42 79 74 65  eap;.  u8 *zByte
88210 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33   = (u8 *)sqlite3
88220 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65  GlobalConfig.pHe
88230 61 70 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f  ap;.  int nMinLo
88240 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
88250 20 20 20 2f 2a 20 4c 6f 67 20 6f 66 20 6d 69 6e     /* Log of min
88260 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  imum allocation 
88270 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2a 2f 0a  size in bytes*/.
88280 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 0a    int iOffset;..
88290 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
882a0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20  ER(NotUsed);..  
882b0 69 66 28 20 21 7a 42 79 74 65 20 29 7b 0a 20 20  if( !zByte ){.  
882c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
882d0 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 6e 4d  ERROR;.  }..  nM
882e0 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c  inLog = memsys5L
882f0 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  og(sqlite3Global
88300 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20  Config.mnReq);. 
88310 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3d 20 28 31   mem5.nAtom = (1
88320 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68  <<nMinLog);.  wh
88330 69 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66  ile( (int)sizeof
88340 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e  (Mem5Link)>mem5.
88350 6e 41 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 6d  nAtom ){.    mem
88360 35 2e 6e 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 6e  5.nAtom = mem5.n
88370 41 74 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a  Atom << 1;.  }..
88380 20 20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20    mem5.nBlock = 
88390 28 6e 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 6e  (nByte / (mem5.n
883a0 41 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29  Atom+sizeof(u8))
883b0 29 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20  );.  mem5.zPool 
883c0 3d 20 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e  = zByte;.  mem5.
883d0 61 43 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d  aCtrl = (u8 *)&m
883e0 65 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e  em5.zPool[mem5.n
883f0 42 6c 6f 63 6b 2a 6d 65 6d 35 2e 6e 41 74 6f 6d  Block*mem5.nAtom
88400 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ];..  for(ii=0; 
88410 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b  ii<=LOGMAX; ii++
88420 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72  ){.    mem5.aiFr
88430 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b  eelist[ii] = -1;
88440 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20  .  }..  iOffset 
88450 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f  = 0;.  for(ii=LO
88460 47 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d  GMAX; ii>=0; ii-
88470 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c  -){.    int nAll
88480 6f 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20  oc = (1<<ii);.  
88490 20 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e    if( (iOffset+n
884a0 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c  Alloc)<=mem5.nBl
884b0 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ock ){.      mem
884c0 35 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d  5.aCtrl[iOffset]
884d0 20 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45   = ii | CTRL_FRE
884e0 45 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35  E;.      memsys5
884f0 4c 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69  Link(iOffset, ii
88500 29 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74  );.      iOffset
88510 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20   += nAlloc;.    
88520 7d 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f  }.    assert((iO
88530 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65  ffset+nAlloc)>me
88540 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a  m5.nBlock);.  }.
88550 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
88560 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
88570 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  initialize this 
88580 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  module..*/.stati
88590 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 53 68  c void memsys5Sh
885a0 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74  utdown(void *Not
885b0 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
885c0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
885d0 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  d);.  return;.}.
885e0 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20  ./*.** Open the 
885f0 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61  file indicated a
88600 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f  nd write a log o
88610 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65  f all unfreed me
88620 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74  mory .** allocat
88630 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c  ions into that l
88640 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  og..*/.SQLITE_PR
88650 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
88660 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 63 6f  e3Memsys5Dump(co
88670 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
88680 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  ame){.#ifdef SQL
88690 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45  ITE_DEBUG.  FILE
886a0 20 2a 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20   *out;.  int i, 
886b0 6a 2c 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e  j, n;.  int nMin
886c0 4c 6f 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c  Log;..  if( zFil
886d0 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c  ename==0 || zFil
886e0 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20  ename[0]==0 ){. 
886f0 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b     out = stdout;
88700 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75  .  }else{.    ou
88710 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e  t = fopen(zFilen
88720 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69  ame, "w");.    i
88730 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  f( out==0 ){.   
88740 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
88750 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f  r, "** Unable to
88760 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64   output memory d
88770 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a  ebug output log:
88780 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20   %s **\n",.     
88790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
887a0 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20   zFilename);.   
887b0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
887c0 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e  .  }.  memsys5En
887d0 74 65 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67  ter();.  nMinLog
887e0 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65   = memsys5Log(me
887f0 6d 35 2e 6e 41 74 6f 6d 29 3b 0a 20 20 66 6f 72  m5.nAtom);.  for
88800 28 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20  (i=0; i<=LOGMAX 
88810 26 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b  && i+nMinLog<32;
88820 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e   i++){.    for(n
88830 3d 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65  =0, j=mem5.aiFre
88840 65 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20  elist[i]; j>=0; 
88850 6a 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d  j = MEM5LINK(j)-
88860 3e 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20  >next, n++){}.  
88870 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
88880 66 72 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f  freelist items o
88890 66 20 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22  f size %d: %d\n"
888a0 2c 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c 3c 20  , mem5.nAtom << 
888b0 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72  i, n);.  }.  fpr
888c0 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e  intf(out, "mem5.
888d0 6e 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25  nAlloc       = %
888e0 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c  llu\n", mem5.nAl
888f0 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  loc);.  fprintf(
88900 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c  out, "mem5.total
88910 41 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e  Alloc   = %llu\n
88920 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c  ", mem5.totalAll
88930 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  oc);.  fprintf(o
88940 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45  ut, "mem5.totalE
88950 78 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22  xcess  = %llu\n"
88960 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65  , mem5.totalExce
88970 73 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  ss);.  fprintf(o
88980 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e  ut, "mem5.curren
88990 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20  tOut   = %u\n", 
889a0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29  mem5.currentOut)
889b0 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ;.  fprintf(out,
889c0 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f   "mem5.currentCo
889d0 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d  unt = %u\n", mem
889e0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b  5.currentCount);
889f0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
88a00 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20  "mem5.maxOut    
88a10 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35     = %u\n", mem5
88a20 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69  .maxOut);.  fpri
88a30 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d  ntf(out, "mem5.m
88a40 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75  axCount     = %u
88a50 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75  \n", mem5.maxCou
88a60 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  nt);.  fprintf(o
88a70 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71  ut, "mem5.maxReq
88a80 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20  uest   = %u\n", 
88a90 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29  mem5.maxRequest)
88aa0 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65  ;.  memsys5Leave
88ab0 28 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73  ();.  if( out==s
88ac0 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c  tdout ){.    ffl
88ad0 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d  ush(stdout);.  }
88ae0 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65  else{.    fclose
88af0 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 65  (out);.  }.#else
88b00 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
88b10 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  TER(zFilename);.
88b20 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
88b30 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
88b40 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
88b50 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
88b60 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20  th external .** 
88b70 6c 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75  linkage. It retu
88b80 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
88b90 20 61 20 73 74 61 74 69 63 20 73 71 6c 69 74 65   a static sqlite
88ba0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a  3_mem_methods.**
88bb0 20 73 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65   struct populate
88bc0 64 20 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79  d with the memsy
88bd0 73 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53  s5 methods..*/.S
88be0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
88bf0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
88c00 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
88c10 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f  MemGetMemsys5(vo
88c20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  id){.  static co
88c30 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
88c40 6d 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d  methods memsys5M
88c50 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20  ethods = {.     
88c60 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20  memsys5Malloc,. 
88c70 20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c      memsys5Free,
88c80 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61  .     memsys5Rea
88c90 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79  lloc,.     memsy
88ca0 73 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d  s5Size,.     mem
88cb0 73 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20  sys5Roundup,.   
88cc0 20 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20    memsys5Init,. 
88cd0 20 20 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64      memsys5Shutd
88ce0 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
88cf0 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79  .  return &memsy
88d00 73 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65  s5Methods;.}..#e
88d10 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
88d20 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f  NABLE_MEMSYS5 */
88d30 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
88d40 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20  * End of mem5.c 
88d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88d80 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
88d90 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74  * Begin file mut
88da0 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.c ***********
88db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88dd0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67  /./*.** 2007 Aug
88de0 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 14.**.** The
88df0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
88e00 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
88e10 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
88e20 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
88e30 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
88e40 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
88e50 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
88e60 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
88e70 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
88e80 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
88e90 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
88ea0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
88eb0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
88ec0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
88ed0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
88ee0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
88ef0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
88f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88f40 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
88f50 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
88f60 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
88f70 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
88f80 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
88f90 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
88fa0 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e  e that is common
88fb0 20 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65   across all mute
88fc0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
88fd0 73 2e 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  s...**.** $Id: m
88fe0 75 74 65 78 2e 63 2c 76 20 31 2e 33 30 20 32 30  utex.c,v 1.30 20
88ff0 30 39 2f 30 32 2f 31 37 20 31 36 3a 32 39 3a 31  09/02/17 16:29:1
89000 31 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  1 danielk1977 Ex
89010 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  p $.*/..#ifndef 
89020 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49  SQLITE_MUTEX_OMI
89030 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  T./*.** Initiali
89040 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 79 73  ze the mutex sys
89050 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tem..*/.SQLITE_P
89060 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
89070 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64  e3MutexInit(void
89080 29 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ){ .  int rc = S
89090 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
890a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
890b0 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29  fig.bCoreMutex )
890c0 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74  {.    if( !sqlit
890d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
890e0 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63  utex.xMutexAlloc
890f0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
89100 74 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20  the xMutexAlloc 
89110 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f 74 20 62  method has not b
89120 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 74 68  een set, then th
89130 65 20 75 73 65 72 20 64 69 64 20 6e 6f 74 0a 20  e user did not. 
89140 20 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20       ** install 
89150 61 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  a mutex implemen
89160 74 61 74 69 6f 6e 20 76 69 61 20 73 71 6c 69 74  tation via sqlit
89170 65 33 5f 63 6f 6e 66 69 67 28 29 20 70 72 69 6f  e3_config() prio
89180 72 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73  r to .      ** s
89190 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
891a0 65 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64  e() being called
891b0 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70  . This block cop
891c0 69 65 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a  ies pointers to.
891d0 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66        ** the def
891e0 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ault implementat
891f0 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ion into the sql
89200 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
89210 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20   structure..    
89220 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
89230 65 20 64 61 6e 67 65 72 20 69 73 20 74 68 61 74  e danger is that
89240 20 61 6c 74 68 6f 75 67 68 20 73 71 6c 69 74 65   although sqlite
89250 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 6e 6f  3_config() is no
89260 74 20 61 20 74 68 72 65 61 64 73 61 66 65 0a 20  t a threadsafe. 
89270 20 20 20 20 20 2a 2a 20 41 50 49 2c 20 73 71 6c       ** API, sql
89280 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
89290 29 20 69 73 2c 20 61 6e 64 20 73 6f 20 6d 75 6c  ) is, and so mul
892a0 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 6d 61  tiple threads ma
892b0 79 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 61 74  y be.      ** at
892c0 74 65 6d 70 74 69 6e 67 20 74 6f 20 72 75 6e 20  tempting to run 
892d0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 69  this function si
892e0 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 20 54 6f  multaneously. To
892f0 20 67 75 61 72 64 20 77 72 69 74 65 0a 20 20 20   guard write.   
89300 20 20 20 2a 2a 20 61 63 63 65 73 73 20 74 6f 20     ** access to 
89310 74 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  the sqlite3Globa
89320 6c 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75 72  lConfig structur
89330 65 2c 20 74 68 65 20 27 4d 41 53 54 45 52 27 20  e, the 'MASTER' 
89340 73 74 61 74 69 63 20 6d 75 74 65 78 0a 20 20 20  static mutex.   
89350 20 20 20 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65     ** is obtaine
89360 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69  d before modifyi
89370 6e 67 20 69 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  ng it..      */.
89380 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
89390 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 20 3d  tex_methods *p =
893a0 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d   sqlite3DefaultM
893b0 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 73 71  utex();.      sq
893c0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61  lite3_mutex *pMa
893d0 73 74 65 72 20 3d 20 30 3b 0a 20 20 0a 20 20 20  ster = 0;.  .   
893e0 20 20 20 72 63 20 3d 20 70 2d 3e 78 4d 75 74 65     rc = p->xMute
893f0 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 20 20 69  xInit();.      i
89400 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
89410 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 61 73   ){.        pMas
89420 74 65 72 20 3d 20 70 2d 3e 78 4d 75 74 65 78 41  ter = p->xMutexA
89430 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
89440 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
89450 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
89460 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  (pMaster);.     
89470 20 20 20 70 2d 3e 78 4d 75 74 65 78 45 6e 74 65     p->xMutexEnte
89480 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  r(pMaster);.    
89490 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
894a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
894b0 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f  mutex.xMutexAllo
894c0 63 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20  c==0 .          
894d0 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f     || sqlite3Glo
894e0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
894f0 78 4d 75 74 65 78 41 6c 6c 6f 63 3d 3d 70 2d 3e  xMutexAlloc==p->
89500 78 4d 75 74 65 78 41 6c 6c 6f 63 0a 20 20 20 20  xMutexAlloc.    
89510 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69      );.        i
89520 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61  f( !sqlite3Globa
89530 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
89540 75 74 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  utexAlloc ){.   
89550 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c         sqlite3Gl
89560 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
89570 20 3d 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 7d   = *p;.        }
89580 0a 20 20 20 20 20 20 20 20 70 2d 3e 78 4d 75 74  .        p->xMut
89590 65 78 4c 65 61 76 65 28 70 4d 61 73 74 65 72 29  exLeave(pMaster)
895a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
895b0 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
895c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
895d0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
895e0 49 6e 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20 20  Init();.    }.  
895f0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
89600 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77  }../*.** Shutdow
89610 6e 20 74 68 65 20 6d 75 74 65 78 20 73 79 73 74  n the mutex syst
89620 65 6d 2e 20 54 68 69 73 20 63 61 6c 6c 20 66 72  em. This call fr
89630 65 65 73 20 72 65 73 6f 75 72 63 65 73 20 61 6c  ees resources al
89640 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 73 71  located by.** sq
89650 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29  lite3MutexInit()
89660 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
89670 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
89680 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 0a 20  utexEnd(void){. 
89690 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
896a0 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  _OK;.  if( sqlit
896b0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
896c0 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 64 20 29  utex.xMutexEnd )
896d0 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
896e0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
896f0 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 64 28 29  utex.xMutexEnd()
89700 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
89710 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72  c;.}../*.** Retr
89720 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74  ieve a pointer t
89730 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78  o a static mutex
89740 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e   or allocate a n
89750 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a  ew dynamic one..
89760 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
89770 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c  lite3_mutex *sql
89780 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
89790 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65  (int id){.#ifnde
897a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
897b0 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c  TOINIT.  if( sql
897c0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
897d0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  ) ) return 0;.#e
897e0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71  ndif.  return sq
897f0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
89800 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c  g.mutex.xMutexAl
89810 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49  loc(id);.}..SQLI
89820 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
89830 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65  e3_mutex *sqlite
89840 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20  3MutexAlloc(int 
89850 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69  id){.  if( !sqli
89860 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
89870 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20  bCoreMutex ){.  
89880 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
89890 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
898a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
898b0 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69  ex.xMutexAlloc(i
898c0 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  d);.}../*.** Fre
898d0 65 20 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  e a dynamic mute
898e0 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  x..*/.SQLITE_API
898f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
89900 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33  tex_free(sqlite3
89910 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66  _mutex *p){.  if
89920 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
89930 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
89940 75 74 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28  utex.xMutexFree(
89950 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
89960 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65   Obtain the mute
89970 78 20 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68  x p. If some oth
89980 65 72 20 74 68 72 65 61 64 20 61 6c 72 65 61 64  er thread alread
89990 79 20 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c  y has the mutex,
899a0 20 62 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20   block.** until 
899b0 69 74 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e  it can be obtain
899c0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
899d0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d  I void sqlite3_m
899e0 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
899f0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
89a00 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c  if( p ){.    sql
89a10 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
89a20 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74  .mutex.xMutexEnt
89a30 65 72 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  er(p);.  }.}../*
89a40 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d  .** Obtain the m
89a50 75 74 65 78 20 70 2e 20 49 66 20 73 75 63 63 65  utex p. If succe
89a60 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51  ssful, return SQ
89a70 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69  LITE_OK. Otherwi
89a80 73 65 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a  se, if another.*
89a90 2a 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74  * thread holds t
89aa0 68 65 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20  he mutex and it 
89ab0 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e  cannot be obtain
89ac0 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ed, return SQLIT
89ad0 45 5f 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54  E_BUSY..*/.SQLIT
89ae0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
89af0 33 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69  3_mutex_try(sqli
89b00 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
89b10 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
89b20 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  _OK;.  if( p ){.
89b30 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
89b40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
89b50 75 74 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70  utex.xMutexTry(p
89b60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
89b70 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
89b80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
89b90 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65  eave() routine e
89ba0 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61  xits a mutex tha
89bb0 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
89bc0 0a 2a 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74  .** entered by t
89bd0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
89be0 20 54 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   The behavior is
89bf0 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
89c00 65 20 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e  e mutex .** is n
89c10 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
89c20 65 72 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20  ered. If a NULL 
89c30 70 6f 69 6e 74 65 72 20 69 73 20 70 61 73 73 65  pointer is passe
89c40 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
89c50 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
89c60 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  n is a no-op..*/
89c70 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
89c80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
89c90 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
89ca0 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  ex *p){.  if( p 
89cb0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
89cc0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
89cd0 2e 78 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b  .xMutexLeave(p);
89ce0 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
89cf0 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65  NDEBUG./*.** The
89d00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
89d10 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
89d20 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
89d30 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a  ) routine are.**
89d40 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73   intended for us
89d50 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28  e inside assert(
89d60 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f  ) statements..*/
89d70 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
89d80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
89d90 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
89da0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
89db0 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c  ==0 || sqlite3Gl
89dc0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
89dd0 2e 78 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a  .xMutexHeld(p);.
89de0 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
89df0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
89e00 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
89e10 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
89e20 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  rn p==0 || sqlit
89e30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
89e40 75 74 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65  utex.xMutexNothe
89e50 6c 64 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  ld(p);.}.#endif.
89e60 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
89e70 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a  E_OMIT_MUTEX */.
89e80 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
89e90 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20   End of mutex.c 
89ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
89ed0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
89ee0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
89ef0 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a  x_noop.c *******
89f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
89f20 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f  ./*.** 2008 Octo
89f30 62 65 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 07.**.** The
89f40 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
89f50 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
89f60 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
89f70 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
89f80 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
89f90 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
89fa0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
89fb0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
89fc0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
89fd0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
89fe0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
89ff0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
8a000 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
8a010 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
8a020 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
8a030 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
8a040 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
8a050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8a060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8a070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8a080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8a090 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
8a0a0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
8a0b0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
8a0c0 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
8a0d0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  es..**.** This i
8a0e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e  mplementation in
8a0f0 20 74 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20   this file does 
8a100 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20  not provide any 
8a110 6d 75 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73  mutual.** exclus
8a120 69 6f 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20  ion and is thus 
8a130 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65  suitable for use
8a140 20 6f 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61   only in applica
8a150 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73  tions.** that us
8a160 65 20 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69  e SQLite in a si
8a170 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68  ngle thread.  Th
8a180 65 20 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e  e routines defin
8a190 65 64 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70  ed.** here are p
8a1a0 6c 61 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41  lace-holders.  A
8a1b0 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20  pplications can 
8a1c0 73 75 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69  substitute worki
8a1d0 6e 67 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74  ng.** mutex rout
8a1e0 69 6e 65 73 20 61 74 20 73 74 61 72 74 2d 74 69  ines at start-ti
8a1f0 6d 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a  me using the.**.
8a200 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
8a210 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e  onfig(SQLITE_CON
8a220 46 49 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a  FIG_MUTEX,...).*
8a230 2a 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a  *.** interface..
8a240 2a 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65  **.** If compile
8a250 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45  d with SQLITE_DE
8a260 42 55 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69  BUG, then additi
8a270 6f 6e 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e  onal logic is in
8a280 73 65 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64  serted.** that d
8a290 6f 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69  oes error checki
8a2a0 6e 67 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f  ng on mutexes to
8a2b0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20   make sure they 
8a2c0 61 72 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c  are being.** cal
8a2d0 6c 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a  led correctly..*
8a2e0 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f  *.** $Id: mutex_
8a2f0 6e 6f 6f 70 2e 63 2c 76 20 31 2e 33 20 32 30 30  noop.c,v 1.3 200
8a300 38 2f 31 32 2f 30 35 20 31 37 3a 31 37 3a 30 38  8/12/05 17:17:08
8a310 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
8a320 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
8a330 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26  TE_MUTEX_NOOP) &
8a340 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
8a350 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53  E_DEBUG)./*.** S
8a360 74 75 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72  tub routines for
8a370 20 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f   all mutex metho
8a380 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ds..**.** This r
8a390 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20  outines provide 
8a3a0 6e 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73  no mutual exclus
8a3b0 69 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65  ion or error che
8a3c0 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  cking..*/.static
8a3d0 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65   int noopMutexHe
8a3e0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
8a3f0 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20   *p){ return 1; 
8a400 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f  }.static int noo
8a410 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71  pMutexNotheld(sq
8a420 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
8a430 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61   return 1; }.sta
8a440 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65  tic int noopMute
8a450 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74  xInit(void){ ret
8a460 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
8a470 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70  .static int noop
8a480 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20  MutexEnd(void){ 
8a490 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
8a4a0 3b 20 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74  ; }.static sqlit
8a4b0 65 33 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75  e3_mutex *noopMu
8a4c0 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29  texAlloc(int id)
8a4d0 7b 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65  { return (sqlite
8a4e0 33 5f 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74  3_mutex*)8; }.st
8a4f0 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75  atic void noopMu
8a500 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
8a510 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72  mutex *p){ retur
8a520 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  n; }.static void
8a530 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28   noopMutexEnter(
8a540 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
8a550 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61  ){ return; }.sta
8a560 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65  tic int noopMute
8a570 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  xTry(sqlite3_mut
8a580 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53  ex *p){ return S
8a590 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74  QLITE_OK; }.stat
8a5a0 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65  ic void noopMute
8a5b0 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d  xLeave(sqlite3_m
8a5c0 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e  utex *p){ return
8a5d0 3b 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ; }..SQLITE_PRIV
8a5e0 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ATE sqlite3_mute
8a5f0 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74  x_methods *sqlit
8a600 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76  e3DefaultMutex(v
8a610 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73  oid){.  static s
8a620 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
8a630 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a  hods sMutex = {.
8a640 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69      noopMutexIni
8a650 74 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  t,.    noopMutex
8a660 45 6e 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  End,.    noopMut
8a670 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f  exAlloc,.    noo
8a680 70 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  pMutexFree,.    
8a690 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a  noopMutexEnter,.
8a6a0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79      noopMutexTry
8a6b0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c  ,.    noopMutexL
8a6c0 65 61 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d  eave,..    noopM
8a6d0 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f  utexHeld,.    no
8a6e0 6f 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20  opMutexNotheld. 
8a6f0 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73   };..  return &s
8a700 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20  Mutex;.}.#endif 
8a710 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
8a720 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26  E_MUTEX_NOOP) &&
8a730 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
8a740 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20  _DEBUG) */..#if 
8a750 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
8a760 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65  UTEX_NOOP) && de
8a770 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
8a780 55 47 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69  UG)./*.** In thi
8a790 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
8a7a0 2c 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67  , error checking
8a7b0 20 69 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72   is provided for
8a7c0 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20   testing.** and 
8a7d0 64 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73  debugging purpos
8a7e0 65 73 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73  es.  The mutexes
8a7f0 20 73 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72   still do not pr
8a800 6f 76 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74  ovide any.** mut
8a810 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a  ual exclusion..*
8a820 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74  /../*.** The mut
8a830 65 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72  ex object.*/.str
8a840 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  uct sqlite3_mute
8a850 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20  x {.  int id;   
8a860 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74    /* The mutex t
8a870 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  ype */.  int cnt
8a880 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  ;    /* Number o
8a890 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75  f entries withou
8a8a0 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61  t a matching lea
8a8b0 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ve */.};../*.** 
8a8c0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
8a8d0 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c  x_held() and sql
8a8e0 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
8a8f0 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65  ld() routine are
8a900 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72  .** intended for
8a910 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65   use inside asse
8a920 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
8a930 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
8a940 65 62 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71  ebugMutexHeld(sq
8a950 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
8a960 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c  .  return p==0 |
8a970 7c 20 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74  | p->cnt>0;.}.st
8a980 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75  atic int debugMu
8a990 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74  texNotheld(sqlit
8a9a0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
8a9b0 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
8a9c0 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  ->cnt==0;.}../*.
8a9d0 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e  ** Initialize an
8a9e0 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74  d deinitialize t
8a9f0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
8aa00 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  em..*/.static in
8aa10 74 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74  t debugMutexInit
8aa20 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
8aa30 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74  QLITE_OK; }.stat
8aa40 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
8aa50 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75  xEnd(void){ retu
8aa60 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
8aa70 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
8aa80 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
8aa90 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
8aaa0 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65  es a new.** mute
8aab0 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  x and returns a 
8aac0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
8aad0 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  If it returns NU
8aae0 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73  LL.** that means
8aaf0 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f   that a mutex co
8ab00 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63  uld not be alloc
8ab10 61 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63  ated. .*/.static
8ab20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
8ab30 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28  debugMutexAlloc(
8ab40 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69  int id){.  stati
8ab50 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
8ab60 61 53 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71  aStatic[6];.  sq
8ab70 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65  lite3_mutex *pNe
8ab80 77 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28  w = 0;.  switch(
8ab90 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20   id ){.    case 
8aba0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
8abb0 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  T:.    case SQLI
8abc0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
8abd0 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77  VE: {.      pNew
8abe0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
8abf0 28 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b  (sizeof(*pNew));
8ac00 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
8ac10 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d  ){.        pNew-
8ac20 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20  >id = id;.      
8ac30 20 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b    pNew->cnt = 0;
8ac40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
8ac50 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
8ac60 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
8ac70 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d   assert( id-2 >=
8ac80 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65   0 );.      asse
8ac90 72 74 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29  rt( id-2 < (int)
8aca0 28 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29  (sizeof(aStatic)
8acb0 2f 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b  /sizeof(aStatic[
8acc0 30 5d 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e  0])) );.      pN
8acd0 65 77 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64  ew = &aStatic[id
8ace0 2d 32 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  -2];.      pNew-
8acf0 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20  >id = id;.      
8ad00 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
8ad10 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
8ad20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
8ad30 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65  utine deallocate
8ad40 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61  s a previously a
8ad50 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a  llocated mutex..
8ad60 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
8ad70 65 62 75 67 4d 75 74 65 78 46 72 65 65 28 73 71  ebugMutexFree(sq
8ad80 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
8ad90 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e  .  assert( p->cn
8ada0 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t==0 );.  assert
8adb0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
8adc0 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d  MUTEX_FAST || p-
8add0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
8ade0 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20  X_RECURSIVE );. 
8adf0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
8ae00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
8ae10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
8ae20 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  er() and sqlite3
8ae30 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75  _mutex_try() rou
8ae40 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a  tines attempt.**
8ae50 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65   to enter a mute
8ae60 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74  x.  If another t
8ae70 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79  hread is already
8ae80 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65   within the mute
8ae90 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75  x,.** sqlite3_mu
8aea0 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c  tex_enter() will
8aeb0 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74   block and sqlit
8aec0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77  e3_mutex_try() w
8aed0 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  ill return.** SQ
8aee0 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20  LITE_BUSY.  The 
8aef0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
8af00 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  y() interface re
8af10 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a  turns SQLITE_OK.
8af20 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66  ** upon successf
8af30 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78  ul entry.  Mutex
8af40 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
8af50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
8af60 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62  CURSIVE can.** b
8af70 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70  e entered multip
8af80 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20  le times by the 
8af90 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e  same thread.  In
8afa0 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c   such cases the,
8afb0 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62  .** mutex must b
8afc0 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61  e exited an equa
8afd0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  l number of time
8afe0 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72  s before another
8aff0 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65   thread.** can e
8b000 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61  nter.  If the sa
8b010 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20  me thread tries 
8b020 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68  to enter any oth
8b030 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78  er kind of mutex
8b040 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  .** more than on
8b050 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72  ce, the behavior
8b060 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
8b070 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65  /.static void de
8b080 62 75 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71  bugMutexEnter(sq
8b090 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
8b0a0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
8b0b0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
8b0c0 45 43 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75  ECURSIVE || debu
8b0d0 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29  gMutexNotheld(p)
8b0e0 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a   );.  p->cnt++;.
8b0f0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62  }.static int deb
8b100 75 67 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74  ugMutexTry(sqlit
8b110 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
8b120 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
8b130 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
8b140 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75  RSIVE || debugMu
8b150 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
8b160 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72  .  p->cnt++;.  r
8b170 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
8b180 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
8b190 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
8b1a0 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74  e() routine exit
8b1b0 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77  s a mutex that w
8b1c0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  as.** previously
8b1d0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20   entered by the 
8b1e0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68  same thread.  Th
8b1f0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
8b200 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
8b210 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
8b220 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
8b230 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75   or.** is not cu
8b240 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65  rrently allocate
8b250 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  d.  SQLite will 
8b260 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e  never do either.
8b270 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8b280 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28  debugMutexLeave(
8b290 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
8b2a0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64 65 62  ){.  assert( deb
8b2b0 75 67 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29  ugMutexHeld(p) )
8b2c0 3b 0a 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20  ;.  p->cnt--;.  
8b2d0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
8b2e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
8b2f0 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75  RSIVE || debugMu
8b300 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
8b310 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
8b320 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  TE sqlite3_mutex
8b330 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65  _methods *sqlite
8b340 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f  3DefaultMutex(vo
8b350 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71  id){.  static sq
8b360 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
8b370 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20  ods sMutex = {. 
8b380 20 20 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69     debugMutexIni
8b390 74 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65  t,.    debugMute
8b3a0 78 45 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d  xEnd,.    debugM
8b3b0 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64  utexAlloc,.    d
8b3c0 65 62 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20  ebugMutexFree,. 
8b3d0 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74     debugMutexEnt
8b3e0 65 72 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74  er,.    debugMut
8b3f0 65 78 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67  exTry,.    debug
8b400 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20  MutexLeave,..   
8b410 20 64 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c   debugMutexHeld,
8b420 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e  .    debugMutexN
8b430 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72  otheld.  };..  r
8b440 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d  eturn &sMutex;.}
8b450 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
8b460 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ed(SQLITE_MUTEX_
8b470 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64  NOOP) && defined
8b480 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a  (SQLITE_DEBUG) *
8b490 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
8b4a0 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f  ** End of mutex_
8b4b0 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  noop.c *********
8b4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b4e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
8b4f0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75  ** Begin file mu
8b500 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a  tex_os2.c ******
8b510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b530 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75  */./*.** 2007 Au
8b540 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68  gust 28.**.** Th
8b550 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
8b560 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
8b570 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
8b580 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
8b590 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
8b5a0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
8b5b0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
8b5c0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
8b5d0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
8b5e0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
8b5f0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
8b600 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
8b610 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
8b620 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
8b630 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
8b640 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
8b650 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
8b660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b6a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
8b6b0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
8b6c0 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61   C functions tha
8b6d0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65  t implement mute
8b6e0 78 65 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a  xes for OS/2.**.
8b6f0 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73  ** $Id: mutex_os
8b700 32 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f  2.c,v 1.11 2008/
8b710 31 31 2f 32 32 20 31 39 3a 35 30 3a 35 34 20 70  11/22 19:50:54 p
8b720 77 65 69 6c 62 61 63 68 65 72 20 45 78 70 20 24  weilbacher Exp $
8b730 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63  .*/../*.** The c
8b740 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
8b750 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66   is only used if
8b760 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53   SQLITE_MUTEX_OS
8b770 32 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a  2 is defined..**
8b780 20 53 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68   See the mutex.h
8b790 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c   file for detail
8b7a0 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  s..*/.#ifdef SQL
8b7b0 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f  ITE_MUTEX_OS2../
8b7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8b7d0 2a 2a 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65  ****** OS/2 Mute
8b7e0 78 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x Implementation
8b7f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
8b800 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
8b810 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
8b820 6e 20 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20  n of mutexes is 
8b830 62 75 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20  built using the 
8b840 4f 53 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a  OS/2 API..*/../*
8b850 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62  .** The mutex ob
8b860 6a 65 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63  ject.** Each rec
8b870 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 20  ursive mutex is 
8b880 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
8b890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
8b8a0 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63  ucture..*/.struc
8b8b0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  t sqlite3_mutex 
8b8c0 7b 0a 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20  {.  HMTX mutex; 
8b8d0 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63        /* Mutex c
8b8e0 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c  ontrolling the l
8b8f0 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64  ock */.  int  id
8b900 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75  ;          /* Mu
8b910 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e  tex type */.  in
8b920 74 20 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  t  nRef;        
8b930 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66  /* Number of ref
8b940 65 72 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44  erences */.  TID
8b950 20 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f    owner;       /
8b960 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67  * Thread holding
8b970 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d   this mutex */.}
8b980 3b 0a 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d  ;..#define OS2_M
8b990 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
8b9a0 20 20 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a     0,0,0,0../*.*
8b9b0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  * Initialize and
8b9c0 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   deinitialize th
8b9d0 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
8b9e0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m..*/.static int
8b9f0 20 6f 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f   os2MutexInit(vo
8ba00 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
8ba10 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20  TE_OK; }.static 
8ba20 69 6e 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28  int os2MutexEnd(
8ba30 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
8ba40 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a  LITE_OK; }../*.*
8ba50 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
8ba60 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74  tex_alloc() rout
8ba70 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ine allocates a 
8ba80 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64  new.** mutex and
8ba90 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
8baa0 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74  er to it.  If it
8bab0 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a   returns NULL.**
8bac0 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
8bad0 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e   a mutex could n
8bae0 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e  ot be allocated.
8baf0 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c   .** SQLite will
8bb00 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63   unwind its stac
8bb10 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20  k and return an 
8bb20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75  error.  The argu
8bb30 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  ment.** to sqlit
8bb40 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
8bb50 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65   is one of these
8bb60 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
8bb70 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  ts:.**.** <ul>.*
8bb80 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
8bb90 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20  UTEX_FAST       
8bba0 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69          0.** <li
8bbb0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
8bbc0 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20  RECURSIVE       
8bbd0 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51     1.** <li>  SQ
8bbe0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
8bbf0 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a  C_MASTER      2.
8bc00 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
8bc10 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
8bc20 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c           3.** <l
8bc30 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
8bc40 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20  _STATIC_PRNG    
8bc50 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a      4.** </ul>.*
8bc60 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
8bc70 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75  wo constants cau
8bc80 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  se sqlite3_mutex
8bc90 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61  _alloc() to crea
8bca0 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65  te.** a new mute
8bcb0 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65  x.  The new mute
8bcc0 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77  x is recursive w
8bcd0 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
8bce0 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73  _RECURSIVE.** is
8bcf0 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65   used but not ne
8bd00 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65  cessarily so whe
8bd10 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  n SQLITE_MUTEX_F
8bd20 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20  AST is used..** 
8bd30 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  The mutex implem
8bd40 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  entation does no
8bd50 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61  t need to make a
8bd60 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20   distinction.** 
8bd70 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d  between SQLITE_M
8bd80 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61  UTEX_RECURSIVE a
8bd90 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  nd SQLITE_MUTEX_
8bda0 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a  FAST if it does.
8bdb0 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20  ** not want to. 
8bdc0 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c   But SQLite will
8bdd0 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20   only request a 
8bde0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
8bdf0 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72  in.** cases wher
8be00 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64  e it really need
8be10 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73  s one.  If a fas
8be20 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76  ter non-recursiv
8be30 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65  e mutex.** imple
8be40 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61  mentation is ava
8be50 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f  ilable on the ho
8be60 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65  st platform, the
8be70 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
8be80 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e  .** might return
8be90 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e   such a mutex in
8bea0 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c   response to SQL
8beb0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a  ITE_MUTEX_FAST..
8bec0 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20  **.** The other 
8bed0 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65  allowed paramete
8bee0 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  rs to sqlite3_mu
8bef0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68  tex_alloc() each
8bf00 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   return.** a poi
8bf10 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63  nter to a static
8bf20 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74   preexisting mut
8bf30 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 74 69  ex.  Three stati
8bf40 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a  c mutexes are.**
8bf50 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72   used by the cur
8bf60 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20  rent version of 
8bf70 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20  SQLite.  Future 
8bf80 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
8bf90 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64  te.** may add ad
8bfa0 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20  ditional static 
8bfb0 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63  mutexes.  Static
8bfc0 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72   mutexes are for
8bfd0 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65   internal.** use
8bfe0 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e   by SQLite only.
8bff0 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74    Applications t
8c000 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d  hat use SQLite m
8c010 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a  utexes should.**
8c020 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79   use only the dy
8c030 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65  namic mutexes re
8c040 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45  turned by SQLITE
8c050 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a  _MUTEX_FAST or.*
8c060 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  * SQLITE_MUTEX_R
8c070 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20  ECURSIVE..**.** 
8c080 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65  Note that if one
8c090 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20   of the dynamic 
8c0a0 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73  mutex parameters
8c0b0 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46   (SQLITE_MUTEX_F
8c0c0 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45  AST.** or SQLITE
8c0d0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
8c0e0 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73  ) is used then s
8c0f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
8c100 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20  oc().** returns 
8c110 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65  a different mute
8c120 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e  x on every call.
8c130 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74    But for the st
8c140 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79  atic.** mutex ty
8c150 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75  pes, the same mu
8c160 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20  tex is returned 
8c170 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68  on every call th
8c180 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61  at has.** the sa
8c190 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a  me type number..
8c1a0 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  */.static sqlite
8c1b0 33 5f 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65  3_mutex *os2Mute
8c1c0 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65  xAlloc(int iType
8c1d0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
8c1e0 65 78 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20  ex *p = NULL;.  
8c1f0 73 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b  switch( iType ){
8c200 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
8c210 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20  _MUTEX_FAST:.   
8c220 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54   case SQLITE_MUT
8c230 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a  EX_RECURSIVE: {.
8c240 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65        p = sqlite
8c250 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a  3MallocZero( siz
8c260 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20  eof(*p) );.     
8c270 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
8c280 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
8c290 0a 20 20 20 20 20 20 20 20 69 66 28 20 44 6f 73  .        if( Dos
8c2a0 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20  CreateMutexSem( 
8c2b0 30 2c 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c  0, &p->mutex, 0,
8c2c0 20 46 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45   FALSE ) != NO_E
8c2d0 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20  RROR ){.        
8c2e0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20    sqlite3_free( 
8c2f0 70 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  p );.          p
8c300 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20   = NULL;.       
8c310 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
8c320 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
8c330 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
8c340 20 20 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69     static volati
8c350 6c 65 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20  le int isInit = 
8c360 30 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  0;.      static 
8c370 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74  sqlite3_mutex st
8c380 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20  aticMutexes[] = 
8c390 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f  {.        { OS2_
8c3a0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
8c3b0 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20  R, },.        { 
8c3c0 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  OS2_MUTEX_INITIA
8c3d0 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20  LIZER, },.      
8c3e0 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e    { OS2_MUTEX_IN
8c3f0 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20  ITIALIZER, },.  
8c400 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45        { OS2_MUTE
8c410 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d  X_INITIALIZER, }
8c420 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f  ,.        { OS2_
8c430 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
8c440 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20  R, },.        { 
8c450 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  OS2_MUTEX_INITIA
8c460 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20  LIZER, },.      
8c470 7d 3b 0a 20 20 20 20 20 20 69 66 20 28 20 21 69  };.      if ( !i
8c480 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20  sInit ){.       
8c490 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20   APIRET rc;.    
8c4a0 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20      PTIB ptib;. 
8c4b0 20 20 20 20 20 20 20 50 50 49 42 20 70 70 69 62         PPIB ppib
8c4c0 3b 0a 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d  ;.        HMTX m
8c4d0 75 74 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68  utex;.        ch
8c4e0 61 72 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20  ar name[32];.   
8c4f0 20 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42       DosGetInfoB
8c500 6c 6f 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70  locks( &ptib, &p
8c510 70 69 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73  pib );.        s
8c520 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
8c530 20 73 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e   sizeof(name), n
8c540 61 6d 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53  ame, "\\SEM32\\S
8c550 51 4c 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20  QLITE%04x",.    
8c560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8c570 20 20 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f        ppib->pib_
8c580 75 6c 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20  ulpid );.       
8c590 20 77 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20   while( !isInit 
8c5a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74  ){.          mut
8c5b0 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ex = 0;.        
8c5c0 20 20 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65    rc = DosCreate
8c5d0 4d 75 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20  MutexSem( name, 
8c5e0 26 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45  &mutex, 0, FALSE
8c5f0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
8c600 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc == NO_ERROR 
8c610 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75  ){.            u
8c620 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20  nsigned int i;. 
8c630 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21             if( !
8c640 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20  isInit ){.      
8c650 20 20 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d          for( i =
8c660 20 30 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73   0; i < sizeof(s
8c670 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69  taticMutexes)/si
8c680 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78  zeof(staticMutex
8c690 65 73 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20  es[0]); i++ ){. 
8c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44                 D
8c6b0 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d  osCreateMutexSem
8c6c0 28 20 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65  ( 0, &staticMute
8c6d0 78 65 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c  xes[i].mutex, 0,
8c6e0 20 46 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20   FALSE );.      
8c6f0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
8c700 20 20 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d          isInit =
8c710 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
8c720 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 6f  }.            Do
8c730 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20  sCloseMutexSem( 
8c740 6d 75 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20  mutex );.       
8c750 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20     }else if( rc 
8c760 3d 3d 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41  == ERROR_DUPLICA
8c770 54 45 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20  TE_NAME ){.     
8c780 20 20 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28         DosSleep(
8c790 20 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20   1 );.          
8c7a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
8c7b0 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20     return p;.   
8c7c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
8c7d0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
8c7e0 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32   assert( iType-2
8c7f0 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61   >= 0 );.      a
8c800 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c  ssert( iType-2 <
8c810 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75   sizeof(staticMu
8c820 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74  texes)/sizeof(st
8c830 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20  aticMutexes[0]) 
8c840 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74  );.      p = &st
8c850 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70  aticMutexes[iTyp
8c860 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69  e-2];.      p->i
8c870 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20  d = iType;.     
8c880 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
8c890 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
8c8a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
8c8b0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73  tine deallocates
8c8c0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c   a previously al
8c8d0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a  located mutex..*
8c8e0 2a 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65  * SQLite is care
8c8f0 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ful to deallocat
8c900 65 20 65 76 65 72 79 20 6d 75 74 65 78 20 74 68  e every mutex th
8c910 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e  at it allocates.
8c920 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8c930 6f 73 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c  os2MutexFree(sql
8c940 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
8c950 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
8c960 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  urn;.  assert( p
8c970 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61  ->nRef==0 );.  a
8c980 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
8c990 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
8c9a0 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
8c9b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
8c9c0 20 29 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75   );.  DosCloseMu
8c9d0 74 65 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78  texSem( p->mutex
8c9e0 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   );.  sqlite3_fr
8c9f0 65 65 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ee( p );.}../*.*
8ca00 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
8ca10 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
8ca20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
8ca30 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
8ca40 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
8ca50 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e   a mutex.  If an
8ca60 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
8ca70 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74  already within t
8ca80 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c  he mutex,.** sql
8ca90 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
8caa0 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e  () will block an
8cab0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
8cac0 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72  try() will retur
8cad0 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59  n.** SQLITE_BUSY
8cae0 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  .  The sqlite3_m
8caf0 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72  utex_try() inter
8cb00 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c  face returns SQL
8cb10 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73  ITE_OK.** upon s
8cb20 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e  uccessful entry.
8cb30 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65    Mutexes create
8cb40 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d  d using SQLITE_M
8cb50 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63  UTEX_RECURSIVE c
8cb60 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64  an.** be entered
8cb70 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
8cb80 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
8cb90 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73  ad.  In such cas
8cba0 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78  es the,.** mutex
8cbb0 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20   must be exited 
8cbc0 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20  an equal number 
8cbd0 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20  of times before 
8cbe0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a  another thread.*
8cbf0 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66  * can enter.  If
8cc00 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
8cc10 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
8cc20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f  any other kind o
8cc30 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20  f mutex.** more 
8cc40 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62  than once, the b
8cc50 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
8cc60 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ined..*/.static 
8cc70 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74  void os2MutexEnt
8cc80 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
8cc90 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b   *p){.  TID tid;
8cca0 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a  .  PID holder1;.
8ccb0 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b    ULONG holder2;
8ccc0 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
8ccd0 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
8cce0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
8ccf0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
8cd00 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64   os2MutexNotheld
8cd10 28 70 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75  (p) );.  DosRequ
8cd20 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d  estMutexSem(p->m
8cd30 75 74 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49  utex, SEM_INDEFI
8cd40 4e 49 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f  NITE_WAIT);.  Do
8cd50 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
8cd60 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72  ->mutex, &holder
8cd70 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72  1, &tid, &holder
8cd80 32 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d  2);.  p->owner =
8cd90 20 74 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b   tid;.  p->nRef+
8cda0 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  +;.}.static int 
8cdb0 6f 73 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69  os2MutexTry(sqli
8cdc0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
8cdd0 20 69 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74   int rc;.  TID t
8cde0 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72  id;.  PID holder
8cdf0 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65  1;.  ULONG holde
8ce00 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  r2;.  if( p==0 )
8ce10 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
8ce20 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  K;.  assert( p->
8ce30 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
8ce40 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73  _RECURSIVE || os
8ce50 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29  2MutexNotheld(p)
8ce60 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71   );.  if( DosReq
8ce70 75 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e  uestMutexSem(p->
8ce80 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44  mutex, SEM_IMMED
8ce90 49 41 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20  IATE_RETURN) == 
8cea0 4e 4f 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20  NO_ERROR) {.    
8ceb0 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d  DosQueryMutexSem
8cec0 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64  (p->mutex, &hold
8ced0 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64  er1, &tid, &hold
8cee0 65 72 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e  er2);.    p->own
8cef0 65 72 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d  er = tid;.    p-
8cf00 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20  >nRef++;.    rc 
8cf10 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
8cf20 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d   else {.    rc =
8cf30 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
8cf40 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
8cf50 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
8cf60 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
8cf70 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
8cf80 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
8cf90 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
8cfa0 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
8cfb0 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
8cfc0 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20   behavior.** is 
8cfd0 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
8cfe0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75   mutex is not cu
8cff0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
8d000 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  or.** is not cur
8d010 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
8d020 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
8d030 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a  ever do either..
8d040 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
8d050 73 32 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c  s2MutexLeave(sql
8d060 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
8d070 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44    TID tid;.  PID
8d080 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e   holder1;.  ULON
8d090 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28  G holder2;.  if(
8d0a0 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   p==0 ) return;.
8d0b0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
8d0c0 66 3e 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72  f>0 );.  DosQuer
8d0d0 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  yMutexSem(p->mut
8d0e0 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74  ex, &holder1, &t
8d0f0 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20  id, &holder2);. 
8d100 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65   assert( p->owne
8d110 72 3d 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e  r==tid );.  p->n
8d120 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28  Ref--;.  assert(
8d130 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70   p->nRef==0 || p
8d140 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
8d150 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a  EX_RECURSIVE );.
8d160 20 20 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65    DosReleaseMute
8d170 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a  xSem(p->mutex);.
8d180 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
8d190 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65  _DEBUG./*.** The
8d1a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
8d1b0 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
8d1c0 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
8d1d0 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a  ) routine are.**
8d1e0 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73   intended for us
8d1f0 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28  e inside assert(
8d200 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f  ) statements..*/
8d210 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d  .static int os2M
8d220 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33  utexHeld(sqlite3
8d230 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49  _mutex *p){.  TI
8d240 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64  D tid;.  PID pid
8d250 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e  ;.  ULONG ulCoun
8d260 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a  t;.  PTIB ptib;.
8d270 20 20 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20    if( p!=0 ) {. 
8d280 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78     DosQueryMutex
8d290 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70  Sem(p->mutex, &p
8d2a0 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75  id, &tid, &ulCou
8d2b0 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  nt);.  } else {.
8d2c0 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
8d2d0 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c  ocks(&ptib, NULL
8d2e0 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69  );.    tid = pti
8d2f0 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69  b->tib_ptib2->ti
8d300 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20  b2_ultid;.  }.  
8d310 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28  return p==0 || (
8d320 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d  p->nRef!=0 && p-
8d330 3e 6f 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a  >owner==tid);.}.
8d340 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75  static int os2Mu
8d350 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74  texNotheld(sqlit
8d360 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
8d370 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70  TID tid;.  PID p
8d380 69 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f  id;.  ULONG ulCo
8d390 75 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62  unt;.  PTIB ptib
8d3a0 3b 0a 20 20 69 66 28 20 70 21 3d 20 30 20 29 20  ;.  if( p!= 0 ) 
8d3b0 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75  {.    DosQueryMu
8d3c0 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c  texSem(p->mutex,
8d3d0 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c   &pid, &tid, &ul
8d3e0 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65  Count);.  } else
8d3f0 20 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66   {.    DosGetInf
8d400 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e  oBlocks(&ptib, N
8d410 55 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20  ULL);.    tid = 
8d420 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d  ptib->tib_ptib2-
8d430 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d  >tib2_ultid;.  }
8d440 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c  .  return p==0 |
8d450 7c 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20  | p->nRef==0 || 
8d460 70 2d 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d  p->owner!=tid;.}
8d470 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f  .#endif..SQLITE_
8d480 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
8d490 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73  mutex_methods *s
8d4a0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74  qlite3DefaultMut
8d4b0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ex(void){.  stat
8d4c0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
8d4d0 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20  _methods sMutex 
8d4e0 3d 20 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  = {.    os2Mutex
8d4f0 49 6e 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74  Init,.    os2Mut
8d500 65 78 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75  exEnd,.    os2Mu
8d510 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73  texAlloc,.    os
8d520 32 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  2MutexFree,.    
8d530 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20  os2MutexEnter,. 
8d540 20 20 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a     os2MutexTry,.
8d550 20 20 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76      os2MutexLeav
8d560 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  e,.#ifdef SQLITE
8d570 5f 44 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75  _DEBUG.    os2Mu
8d580 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32  texHeld,.    os2
8d590 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e  MutexNotheld.#en
8d5a0 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75  dif.  };..  retu
8d5b0 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65  rn &sMutex;.}.#e
8d5c0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
8d5d0 55 54 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a  UTEX_OS2 */../**
8d5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
8d5f0 20 6f 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20   of mutex_os2.c 
8d600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
8d630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
8d640 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e  in file mutex_un
8d650 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ix.c ***********
8d660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
8d680 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32  ** 2007 August 2
8d690 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
8d6a0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
8d6b0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
8d6c0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
8d6d0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
8d6e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
8d6f0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
8d700 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
8d710 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
8d720 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
8d730 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
8d740 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
8d750 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
8d760 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
8d770 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
8d780 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
8d790 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
8d7a0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
8d7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d7f0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
8d800 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
8d810 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
8d820 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f  ement mutexes fo
8d830 72 20 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a  r pthreads.**.**
8d840 20 24 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78   $Id: mutex_unix
8d850 2e 63 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31  .c,v 1.16 2008/1
8d860 32 2f 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72  2/08 18:19:18 dr
8d870 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
8d880 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
8d890 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
8d8a0 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63  used if we are c
8d8b0 6f 6d 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73  ompiling threads
8d8c0 61 66 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69  afe.** under uni
8d8d0 78 20 77 69 74 68 20 70 74 68 72 65 61 64 73 2e  x with pthreads.
8d8e0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
8d8f0 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   this implementa
8d900 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20  tion requires a 
8d910 76 65 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65  version of pthre
8d920 61 64 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70  ads that.** supp
8d930 6f 72 74 73 20 72 65 63 75 72 73 69 76 65 20 6d  orts recursive m
8d940 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65  utexes..*/.#ifde
8d950 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50  f SQLITE_MUTEX_P
8d960 54 48 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64  THREADS..#includ
8d970 65 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a  e <pthread.h>...
8d980 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72  /*.** Each recur
8d990 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e  sive mutex is an
8d9a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
8d9b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
8d9c0 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
8d9d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a  sqlite3_mutex {.
8d9e0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
8d9f0 74 20 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20  t mutex;     /* 
8da00 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e  Mutex controllin
8da10 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  g the lock */.  
8da20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20 20  int id;         
8da30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75             /* Mu
8da40 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e  tex type */.  in
8da50 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
8da60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
8da70 65 72 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20  er of entrances 
8da80 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f  */.  pthread_t o
8da90 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  wner;           
8daa0 2f 2a 20 54 68 72 65 61 64 20 74 68 61 74 20 69  /* Thread that i
8dab0 73 20 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75  s within this mu
8dac0 74 65 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  tex */.#ifdef SQ
8dad0 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74  LITE_DEBUG.  int
8dae0 20 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20   trace;         
8daf0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
8db00 74 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65 73  to trace changes
8db10 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69   */.#endif.};.#i
8db20 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
8db30 47 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  G.#define SQLITE
8db40 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
8db50 5a 45 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55  ZER { PTHREAD_MU
8db60 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
8db70 20 30 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f   0, 0, (pthread_
8db80 74 29 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23  t)0, 0 }.#else.#
8db90 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d  define SQLITE3_M
8dba0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
8dbb0 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58   { PTHREAD_MUTEX
8dbc0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c  _INITIALIZER, 0,
8dbd0 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30   0, (pthread_t)0
8dbe0 20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   }.#endif../*.**
8dbf0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
8dc00 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71  ex_held() and sq
8dc10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
8dc20 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72  eld() routine ar
8dc30 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f  e.** intended fo
8dc40 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64  r use only insid
8dc50 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  e assert() state
8dc60 6d 65 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20  ments.  On some 
8dc70 70 6c 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68  platforms,.** th
8dc80 65 72 65 20 6d 69 67 68 74 20 62 65 20 72 61 63  ere might be rac
8dc90 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61  e conditions tha
8dca0 74 20 63 61 6e 20 63 61 75 73 65 20 74 68 65 73  t can cause thes
8dcb0 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a  e routines to.**
8dcc0 20 64 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65   deliver incorre
8dcd0 63 74 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20  ct results.  In 
8dce0 70 61 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70  particular, if p
8dcf0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69  thread_equal() i
8dd00 73 0a 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d  s.** not an atom
8dd10 69 63 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68  ic operation, th
8dd20 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  en these routine
8dd30 73 20 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79  s might delivery
8dd40 0a 2a 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65  .** incorrect re
8dd50 73 75 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20  sults.  On most 
8dd60 70 6c 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65  platforms, pthre
8dd70 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20  ad_equal() is a 
8dd80 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  .** comparison o
8dd90 66 20 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61  f two integers a
8dda0 6e 64 20 69 73 20 74 68 65 72 65 66 6f 72 65 20  nd is therefore 
8ddb0 61 74 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20  atomic.  But we 
8ddc0 61 72 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74  are.** told that
8ddd0 20 48 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63   HPUX is not suc
8dde0 68 20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49  h a platform.  I
8ddf0 66 20 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65  f so, then these
8de00 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c   routines.** wil
8de10 6c 20 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72  l not always wor
8de20 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48  k correctly on H
8de30 50 55 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68  PUX..**.** On th
8de40 6f 73 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68  ose platforms wh
8de50 65 72 65 20 70 74 68 72 65 61 64 5f 65 71 75 61  ere pthread_equa
8de60 6c 28 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69  l() is not atomi
8de70 63 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f  c, SQLite.** sho
8de80 75 6c 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20  uld be compiled 
8de90 77 69 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45  without -DSQLITE
8dea0 5f 44 45 42 55 47 20 61 6e 64 20 77 69 74 68 20  _DEBUG and with 
8deb0 2d 44 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d  -DNDEBUG to.** m
8dec0 61 6b 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65  ake sure no asse
8ded0 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
8dee0 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e  are evaluated an
8def0 64 20 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a  d hence these.**
8df00 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65   routines are ne
8df10 76 65 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23  ver called..*/.#
8df20 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
8df30 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  UG) || defined(S
8df40 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61  QLITE_DEBUG).sta
8df50 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d  tic int pthreadM
8df60 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33  utexHeld(sqlite3
8df70 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65  _mutex *p){.  re
8df80 74 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30  turn (p->nRef!=0
8df90 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61   && pthread_equa
8dfa0 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72  l(p->owner, pthr
8dfb0 65 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a  ead_self()));.}.
8dfc0 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65  static int pthre
8dfd0 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73  adMutexNotheld(s
8dfe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
8dff0 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52  {.  return p->nR
8e000 65 66 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64  ef==0 || pthread
8e010 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c  _equal(p->owner,
8e020 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29   pthread_self())
8e030 3d 3d 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ==0;.}.#endif../
8e040 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
8e050 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65  and deinitialize
8e060 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
8e070 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  stem..*/.static 
8e080 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78  int pthreadMutex
8e090 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75  Init(void){ retu
8e0a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
8e0b0 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65  static int pthre
8e0c0 61 64 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29  adMutexEnd(void)
8e0d0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
8e0e0 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OK; }../*.** The
8e0f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
8e100 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61  lloc() routine a
8e110 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a  llocates a new.*
8e120 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75  * mutex and retu
8e130 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
8e140 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75   it.  If it retu
8e150 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74  rns NULL.** that
8e160 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75   means that a mu
8e170 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  tex could not be
8e180 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c   allocated.  SQL
8e190 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69  ite.** will unwi
8e1a0 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64  nd its stack and
8e1b0 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
8e1c0 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a  .  The argument.
8e1d0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  ** to sqlite3_mu
8e1e0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f  tex_alloc() is o
8e1f0 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ne of these inte
8e200 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a  ger constants:.*
8e210 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
8e220 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
8e230 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  FAST.** <li>  SQ
8e240 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
8e250 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  SIVE.** <li>  SQ
8e260 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
8e270 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e  C_MASTER.** <li>
8e280 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
8e290 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69  TATIC_MEM.** <li
8e2a0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
8e2b0 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c  STATIC_MEM2.** <
8e2c0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
8e2d0 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a  X_STATIC_PRNG.**
8e2e0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
8e2f0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a  TEX_STATIC_LRU.*
8e300 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
8e310 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73  e first two cons
8e320 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69  tants cause sqli
8e330 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
8e340 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61  ) to create.** a
8e350 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65   new mutex.  The
8e360 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65   new mutex is re
8e370 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c  cursive when SQL
8e380 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
8e390 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62  IVE.** is used b
8e3a0 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  ut not necessari
8e3b0 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54  ly so when SQLIT
8e3c0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20  E_MUTEX_FAST is 
8e3d0 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74  used..** The mut
8e3e0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
8e3f0 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
8e400 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e  to make a distin
8e410 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  ction.** between
8e420 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
8e430 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49  CURSIVE and SQLI
8e440 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66  TE_MUTEX_FAST if
8e450 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
8e460 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51  want to.  But SQ
8e470 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72  Lite will only r
8e480 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69  equest a recursi
8e490 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63  ve mutex in.** c
8e4a0 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65  ases where it re
8e4b0 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20  ally needs one. 
8e4c0 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e   If a faster non
8e4d0 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78  -recursive mutex
8e4e0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
8e4f0 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  on is available 
8e500 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74  on the host plat
8e510 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20  form, the mutex 
8e520 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67  subsystem.** mig
8e530 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61  ht return such a
8e540 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e   mutex in respon
8e550 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54  se to SQLITE_MUT
8e560 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54  EX_FAST..**.** T
8e570 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64  he other allowed
8e580 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73   parameters to s
8e590 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
8e5a0 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e  oc() each return
8e5b0 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
8e5c0 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69   a static preexi
8e5d0 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68  sting mutex.  Th
8e5e0 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78  ree static mutex
8e5f0 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62  es are.** used b
8e600 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65  y the current ve
8e610 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e  rsion of SQLite.
8e620 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e    Future version
8e630 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d  s of SQLite.** m
8e640 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61  ay add additiona
8e650 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  l static mutexes
8e660 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65  .  Static mutexe
8e670 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e  s are for intern
8e680 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c  al.** use by SQL
8e690 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69  ite only.  Appli
8e6a0 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65  cations that use
8e6b0 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20   SQLite mutexes 
8e6c0 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e  should.** use on
8e6d0 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d  ly the dynamic m
8e6e0 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20  utexes returned 
8e6f0 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  by SQLITE_MUTEX_
8e700 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54  FAST or.** SQLIT
8e710 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
8e720 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  E..**.** Note th
8e730 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65  at if one of the
8e740 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70   dynamic mutex p
8e750 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54  arameters (SQLIT
8e760 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20  E_MUTEX_FAST.** 
8e770 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  or SQLITE_MUTEX_
8e780 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73  RECURSIVE) is us
8e790 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  ed then sqlite3_
8e7a0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a  mutex_alloc().**
8e7b0 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65   returns a diffe
8e7c0 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76  rent mutex on ev
8e7d0 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66  ery call.  But f
8e7e0 6f 72 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a  or the static .*
8e7f0 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74  * mutex types, t
8e800 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73  he same mutex is
8e810 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65   returned on eve
8e820 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73  ry call that has
8e830 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70  .** the same typ
8e840 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
8e850 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tic sqlite3_mute
8e860 78 20 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41  x *pthreadMutexA
8e870 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b  lloc(int iType){
8e880 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
8e890 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75  3_mutex staticMu
8e8a0 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20  texes[] = {.    
8e8b0 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e  SQLITE3_MUTEX_IN
8e8c0 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53  ITIALIZER,.    S
8e8d0 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
8e8e0 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51  TIALIZER,.    SQ
8e8f0 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54  LITE3_MUTEX_INIT
8e900 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c  IALIZER,.    SQL
8e910 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
8e920 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49  ALIZER,.    SQLI
8e930 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
8e940 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54  LIZER,.    SQLIT
8e950 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
8e960 49 5a 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69  IZER.  };.  sqli
8e970 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20  te3_mutex *p;.  
8e980 73 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b  switch( iType ){
8e990 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
8e9a0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
8e9b0 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71  : {.      p = sq
8e9c0 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
8e9d0 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20   sizeof(*p) );. 
8e9e0 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69       if( p ){.#i
8e9f0 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45  fdef SQLITE_HOME
8ea00 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f  GROWN_RECURSIVE_
8ea10 4d 55 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a  MUTEX.        /*
8ea20 20 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75   If recursive mu
8ea30 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76  texes are not av
8ea40 61 69 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c  ailable, we will
8ea50 20 68 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20   have to.       
8ea60 20 2a 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77   ** build our ow
8ea70 6e 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a  n.  See below. *
8ea80 2f 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61  /.        pthrea
8ea90 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d  d_mutex_init(&p-
8eaa0 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73  >mutex, 0);.#els
8eab0 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  e.        /* Use
8eac0 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74   a recursive mut
8ead0 65 78 20 69 66 20 69 74 20 69 73 20 61 76 61 69  ex if it is avai
8eae0 6c 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  lable */.       
8eaf0 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74   pthread_mutexat
8eb00 74 72 5f 74 20 72 65 63 75 72 73 69 76 65 41 74  tr_t recursiveAt
8eb10 74 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72  tr;.        pthr
8eb20 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e  ead_mutexattr_in
8eb30 69 74 28 26 72 65 63 75 72 73 69 76 65 41 74 74  it(&recursiveAtt
8eb40 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72  r);.        pthr
8eb50 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65  ead_mutexattr_se
8eb60 74 74 79 70 65 28 26 72 65 63 75 72 73 69 76 65  ttype(&recursive
8eb70 41 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55  Attr, PTHREAD_MU
8eb80 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a  TEX_RECURSIVE);.
8eb90 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f          pthread_
8eba0 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d  mutex_init(&p->m
8ebb0 75 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 65  utex, &recursive
8ebc0 41 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70  Attr);.        p
8ebd0 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72  thread_mutexattr
8ebe0 5f 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 73  _destroy(&recurs
8ebf0 69 76 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66  iveAttr);.#endif
8ec00 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d  .        p->id =
8ec10 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a   iType;.      }.
8ec20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
8ec30 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
8ec40 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b  TE_MUTEX_FAST: {
8ec50 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74  .      p = sqlit
8ec60 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69  e3MallocZero( si
8ec70 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20  zeof(*p) );.    
8ec80 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
8ec90 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65     p->id = iType
8eca0 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61  ;.        pthrea
8ecb0 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d  d_mutex_init(&p-
8ecc0 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20  >mutex, 0);.    
8ecd0 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
8ece0 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
8ecf0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  lt: {.      asse
8ed00 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30  rt( iType-2 >= 0
8ed10 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
8ed20 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61  ( iType-2 < Arra
8ed30 79 53 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65  ySize(staticMute
8ed40 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20  xes) );.      p 
8ed50 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73  = &staticMutexes
8ed60 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20  [iType-2];.     
8ed70 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
8ed80 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
8ed90 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
8eda0 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  p;.}.../*.** Thi
8edb0 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f  s routine deallo
8edc0 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73  cates a previous
8edd0 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  ly.** allocated 
8ede0 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69  mutex.  SQLite i
8edf0 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61  s careful to dea
8ee00 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a  llocate every.**
8ee10 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61   mutex that it a
8ee20 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61  llocates..*/.sta
8ee30 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 64  tic void pthread
8ee40 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65  MutexFree(sqlite
8ee50 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
8ee60 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
8ee70 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
8ee80 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
8ee90 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64  EX_FAST || p->id
8eea0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
8eeb0 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74  ECURSIVE );.  pt
8eec0 68 72 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74  hread_mutex_dest
8eed0 72 6f 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a  roy(&p->mutex);.
8eee0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
8eef0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
8ef00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
8ef10 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ter() and sqlite
8ef20 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
8ef30 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a  utines attempt.*
8ef40 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74  * to enter a mut
8ef50 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20  ex.  If another 
8ef60 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64  thread is alread
8ef70 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74  y within the mut
8ef80 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ex,.** sqlite3_m
8ef90 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c  utex_enter() wil
8efa0 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69  l block and sqli
8efb0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
8efc0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53  will return.** S
8efd0 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65  QLITE_BUSY.  The
8efe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
8eff0 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ry() interface r
8f000 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
8f010 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73  .** upon success
8f020 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65  ful entry.  Mute
8f030 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e  xes created usin
8f040 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  g SQLITE_MUTEX_R
8f050 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20  ECURSIVE can.** 
8f060 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69  be entered multi
8f070 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65  ple times by the
8f080 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49   same thread.  I
8f090 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65  n such cases the
8f0a0 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20  ,.** mutex must 
8f0b0 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75  be exited an equ
8f0c0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  al number of tim
8f0d0 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65  es before anothe
8f0e0 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20  r thread.** can 
8f0f0 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73  enter.  If the s
8f100 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73  ame thread tries
8f110 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74   to enter any ot
8f120 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65  her kind of mute
8f130 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f  x.** more than o
8f140 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f  nce, the behavio
8f150 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  r is undefined..
8f160 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
8f170 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72  threadMutexEnter
8f180 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
8f190 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
8f1a0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
8f1b0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70  X_RECURSIVE || p
8f1c0 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65  threadMutexNothe
8f1d0 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66  ld(p) );..#ifdef
8f1e0 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57   SQLITE_HOMEGROW
8f1f0 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45  N_RECURSIVE_MUTE
8f200 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73  X.  /* If recurs
8f210 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20  ive mutexes are 
8f220 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74  not available, t
8f230 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67  hen we have to g
8f240 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e  row.  ** our own
8f250 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  .  This implemen
8f260 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  tation assumes t
8f270 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61  hat pthread_equa
8f280 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d  l().  ** is atom
8f290 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e  ic - that it can
8f2a0 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20  not be deceived 
8f2b0 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65  into thinking se
8f2c0 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f  lf.  ** and p->o
8f2d0 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69  wner are equal i
8f2e0 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67  f p->owner chang
8f2f0 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76  es between two v
8f300 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20  alues.  ** that 
8f310 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f  are not equal to
8f320 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20   self while the 
8f330 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61  comparison is ta
8f340 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a  king place..  **
8f350 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
8f360 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65  tion also assume
8f370 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63  s a coherent cac
8f380 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20  he - that .  ** 
8f390 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73  separate process
8f3a0 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64  es cannot read d
8f3b0 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20  ifferent values 
8f3c0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20  from the same.  
8f3d0 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68  ** address at th
8f3e0 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66  e same time.  If
8f3f0 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65   either of these
8f400 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a   two conditions.
8f410 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74    ** are not met
8f420 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78  , then the mutex
8f430 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64  es will fail and
8f440 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72   problems will r
8f450 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a  esult..  */.  {.
8f460 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65      pthread_t se
8f470 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  lf = pthread_sel
8f480 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  f();.    if( p->
8f490 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61  nRef>0 && pthrea
8f4a0 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72  d_equal(p->owner
8f4b0 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20  , self) ){.     
8f4c0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
8f4d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68  }else{.      pth
8f4e0 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28  read_mutex_lock(
8f4f0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  &p->mutex);.    
8f500 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
8f510 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d  f==0 );.      p-
8f520 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20  >owner = self;. 
8f530 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31       p->nRef = 1
8f540 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73  ;.    }.  }.#els
8f550 65 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62  e.  /* Use the b
8f560 75 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76  uilt-in recursiv
8f570 65 20 6d 75 74 65 78 65 73 20 69 66 20 74 68 65  e mutexes if the
8f580 79 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e  y are available.
8f590 0a 20 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f  .  */.  pthread_
8f5a0 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d  mutex_lock(&p->m
8f5b0 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65  utex);.  p->owne
8f5c0 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66  r = pthread_self
8f5d0 28 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  ();.  p->nRef++;
8f5e0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
8f5f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69  SQLITE_DEBUG.  i
8f600 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20  f( p->trace ){. 
8f610 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72     printf("enter
8f620 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 77   mutex %p (%d) w
8f630 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20  ith nRef=%d\n", 
8f640 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e  p, p->trace, p->
8f650 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  nRef);.  }.#endi
8f660 66 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70  f.}.static int p
8f670 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 28 73  threadMutexTry(s
8f680 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
8f690 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
8f6a0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
8f6b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
8f6c0 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75  IVE || pthreadMu
8f6d0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
8f6e0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
8f6f0 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53  HOMEGROWN_RECURS
8f700 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49  IVE_MUTEX.  /* I
8f710 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  f recursive mute
8f720 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  xes are not avai
8f730 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68  lable, then we h
8f740 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a  ave to grow.  **
8f750 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20   our own.  This 
8f760 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61  implementation a
8f770 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72  ssumes that pthr
8f780 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a  ead_equal().  **
8f790 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61   is atomic - tha
8f7a0 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64  t it cannot be d
8f7b0 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69  eceived into thi
8f7c0 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20  nking self.  ** 
8f7d0 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65  and p->owner are
8f7e0 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e   equal if p->own
8f7f0 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65  er changes betwe
8f800 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20  en two values.  
8f810 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20  ** that are not 
8f820 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68  equal to self wh
8f830 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ile the comparis
8f840 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61  on is taking pla
8f850 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d  ce..  ** This im
8f860 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73  plementation als
8f870 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65  o assumes a cohe
8f880 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61  rent cache - tha
8f890 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65  t .  ** separate
8f8a0 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f   processes canno
8f8b0 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74  t read different
8f8c0 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65   values from the
8f8d0 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65   same.  ** addre
8f8e0 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ss at the same t
8f8f0 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20  ime.  If either 
8f900 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e  of these two con
8f910 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65  ditions.  ** are
8f920 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74   not met, then t
8f930 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20  he mutexes will 
8f940 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d  fail and problem
8f950 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20  s will result.. 
8f960 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72   */.  {.    pthr
8f970 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68  ead_t self = pth
8f980 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20  read_self();.   
8f990 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26   if( p->nRef>0 &
8f9a0 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  & pthread_equal(
8f9b0 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20  p->owner, self) 
8f9c0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66  ){.      p->nRef
8f9d0 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ++;.      rc = S
8f9e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65  QLITE_OK;.    }e
8f9f0 6c 73 65 20 69 66 28 20 70 74 68 72 65 61 64 5f  lse if( pthread_
8fa00 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70  mutex_trylock(&p
8fa10 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20  ->mutex)==0 ){. 
8fa20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
8fa30 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20  nRef==0 );.     
8fa40 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66   p->owner = self
8fa50 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20  ;.      p->nRef 
8fa60 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 1;.      rc = 
8fa70 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
8fa80 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
8fa90 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
8faa0 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20    }.  }.#else.  
8fab0 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c 74  /* Use the built
8fac0 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75  -in recursive mu
8fad0 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61 72  texes if they ar
8fae0 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a  e available..  *
8faf0 2f 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f  /.  if( pthread_
8fb00 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70  mutex_trylock(&p
8fb10 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20  ->mutex)==0 ){. 
8fb20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74     p->owner = pt
8fb30 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20  hread_self();.  
8fb40 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20    p->nRef++;.   
8fb50 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
8fb60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
8fb70 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
8fb80 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    }.#endif..#ifd
8fb90 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
8fba0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
8fbb0 5f 4f 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20  _OK && p->trace 
8fbc0 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65  ){.    printf("e
8fbd0 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28 25  nter mutex %p (%
8fbe0 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c  d) with nRef=%d\
8fbf0 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c  n", p, p->trace,
8fc00 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23   p->nRef);.  }.#
8fc10 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72  endif.  return r
8fc20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
8fc30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
8fc40 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78  ave() routine ex
8fc50 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74  its a mutex that
8fc60 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
8fc70 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68  ly entered by th
8fc80 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
8fc90 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  The behavior.** 
8fca0 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20  is undefined if 
8fcb0 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  the mutex is not
8fcc0 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
8fcd0 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20  ed or.** is not 
8fce0 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
8fcf0 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ted.  SQLite wil
8fd00 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65  l never do eithe
8fd10 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
8fd20 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65  d pthreadMutexLe
8fd30 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
8fd40 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
8fd50 20 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c   pthreadMutexHel
8fd60 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65  d(p) );.  p->nRe
8fd70 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  f--;.  assert( p
8fd80 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
8fd90 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
8fda0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 0a 23  _RECURSIVE );..#
8fdb0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d  ifdef SQLITE_HOM
8fdc0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45  EGROWN_RECURSIVE
8fdd0 5f 4d 55 54 45 58 0a 20 20 69 66 28 20 70 2d 3e  _MUTEX.  if( p->
8fde0 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70  nRef==0 ){.    p
8fdf0 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c  thread_mutex_unl
8fe00 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a  ock(&p->mutex);.
8fe10 20 20 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 72    }.#else.  pthr
8fe20 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b  ead_mutex_unlock
8fe30 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e  (&p->mutex);.#en
8fe40 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
8fe50 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70  TE_DEBUG.  if( p
8fe60 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70  ->trace ){.    p
8fe70 72 69 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 74  rintf("leave mut
8fe80 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20  ex %p (%d) with 
8fe90 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70  nRef=%d\n", p, p
8fea0 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66  ->trace, p->nRef
8feb0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  );.  }.#endif.}.
8fec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8fed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
8fee0 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65  thods *sqlite3De
8fef0 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29  faultMutex(void)
8ff00 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
8ff10 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
8ff20 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20   sMutex = {.    
8ff30 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74  pthreadMutexInit
8ff40 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74  ,.    pthreadMut
8ff50 65 78 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 65  exEnd,.    pthre
8ff60 61 64 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20  adMutexAlloc,.  
8ff70 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72    pthreadMutexFr
8ff80 65 65 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d  ee,.    pthreadM
8ff90 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 70  utexEnter,.    p
8ffa0 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 2c 0a  threadMutexTry,.
8ffb0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
8ffc0 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51  Leave,.#ifdef SQ
8ffd0 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70  LITE_DEBUG.    p
8ffe0 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 2c  threadMutexHeld,
8fff0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65  .    pthreadMute
90000 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20  xNotheld.#else. 
90010 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64     0,.    0.#end
90020 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72  if.  };..  retur
90030 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65  n &sMutex;.}..#e
90040 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
90050 55 54 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a  UTEX_PTHREAD */.
90060 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
90070 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e   End of mutex_un
90080 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ix.c ***********
90090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
900a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
900b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
900c0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
900d0 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  x_w32.c ********
900e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
900f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
90100 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
90110 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 14.**.** The 
90120 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
90130 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
90140 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
90150 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
90160 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
90170 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
90180 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
90190 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
901a0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
901b0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
901c0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
901d0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
901e0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
901f0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
90200 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
90210 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
90220 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
90230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90270 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
90280 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
90290 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
902a0 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65  implement mutexe
902b0 73 20 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a  s for win32.**.*
902c0 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32  * $Id: mutex_w32
902d0 2e 63 2c 76 20 31 2e 31 35 20 32 30 30 39 2f 30  .c,v 1.15 2009/0
902e0 31 2f 33 30 20 31 36 3a 30 39 3a 32 33 20 73 68  1/30 16:09:23 sh
902f0 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  ane Exp $.*/../*
90300 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
90310 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
90320 79 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65  y used if we are
90330 20 63 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69   compiling multi
90340 74 68 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61  threaded.** on a
90350 20 77 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a   win32 system..*
90360 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
90370 4d 55 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a  MUTEX_W32../*.**
90380 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20   Each recursive 
90390 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74  mutex is an inst
903a0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
903b0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
903c0 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
903d0 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49  e3_mutex {.  CRI
903e0 54 49 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75  TICAL_SECTION mu
903f0 74 65 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78  tex;    /* Mutex
90400 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65   controlling the
90410 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69   lock */.  int i
90420 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
90430 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74        /* Mutex t
90440 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ype */.  int nRe
90450 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
90460 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
90470 20 65 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20   enterances */. 
90480 20 44 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20   DWORD owner;   
90490 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
904a0 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68  hread holding th
904b0 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a  is mutex */.};..
904c0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
904d0 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20  e (non-zero) if 
904e0 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75  we are running u
904f0 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32  nder WinNT, Win2
90500 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20  K, WinXP,.** or 
90510 57 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66  WinCE.  Return f
90520 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20  alse (zero) for 
90530 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72  Win95, Win98, or
90540 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65   WinME..**.** He
90550 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73  re is an interes
90560 74 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e  ting observation
90570 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c  :  Win95, Win98,
90580 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a   and WinME lack.
90590 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45  ** the LockFileE
905a0 78 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65  x() API.  But we
905b0 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69   can still stati
905c0 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e  cally link again
905d0 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61  st that.** API a
905e0 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e  s long as we don
905f0 27 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72  't call it win r
90600 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f  unning Win95/98/
90610 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a  ME.  A call to.*
90620 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * this routine i
90630 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
90640 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20  ine if the host 
90650 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f  is Win95/98/ME o
90660 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50  r.** WinNT/2K/XP
90670 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c   so that we will
90680 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72   know whether or
90690 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65   not we can safe
906a0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c  ly call.** the L
906b0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
906c0 0a 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54  .**.** mutexIsNT
906d0 28 29 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  () is only used 
906e0 66 6f 72 20 74 68 65 20 54 72 79 45 6e 74 65 72  for the TryEnter
906f0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
90700 29 20 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77  ) API call,.** w
90710 68 69 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61  hich is only ava
90720 69 6c 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61  ilable if your a
90730 70 70 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63  pplication was c
90740 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a  ompiled with .**
90750 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65   _WIN32_WINNT de
90760 66 69 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65  fined to a value
90770 20 3e 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72   >= 0x0400.  Cur
90780 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79  rently, the only
90790 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45  .** call to TryE
907a0 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74  nterCriticalSect
907b0 69 6f 6e 28 29 20 69 73 20 23 69 66 64 65 66 27  ion() is #ifdef'
907c0 65 64 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65  ed out, so #ifde
907d0 66 20 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61  f .** this out a
907e0 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30  s well..*/.#if 0
907f0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
90800 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75  INCE.# define mu
90810 74 65 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23  texIsNT()  (1).#
90820 65 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e  else.  static in
90830 74 20 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64  t mutexIsNT(void
90840 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e  ){.    static in
90850 74 20 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20  t osType = 0;.  
90860 20 20 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20    if( osType==0 
90870 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49  ){.      OSVERSI
90880 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20  ONINFO sInfo;.  
90890 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65      sInfo.dwOSVe
908a0 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20  rsionInfoSize = 
908b0 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20  sizeof(sInfo);. 
908c0 20 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45       GetVersionE
908d0 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20  x(&sInfo);.     
908e0 20 6f 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e   osType = sInfo.
908f0 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45  dwPlatformId==VE
90900 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32  R_PLATFORM_WIN32
90910 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20  _NT ? 2 : 1;.   
90920 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73   }.    return os
90930 54 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e  Type==2;.  }.#en
90940 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53  dif /* SQLITE_OS
90950 5f 57 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66  _WINCE */.#endif
90960 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
90970 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20  DEBUG./*.** The 
90980 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
90990 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  ld() and sqlite3
909a0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
909b0 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20   routine are.** 
909c0 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
909d0 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73   only inside ass
909e0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
909f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
90a00 77 69 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c  winMutexHeld(sql
90a10 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
90a20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66    return p->nRef
90a30 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d  !=0 && p->owner=
90a40 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61  =GetCurrentThrea
90a50 64 49 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20  dId();.}.static 
90a60 69 6e 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68  int winMutexNoth
90a70 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
90a80 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  x *p){.  return 
90a90 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
90aa0 3e 6f 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65  >owner!=GetCurre
90ab0 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a  ntThreadId();.}.
90ac0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49  #endif.../*.** I
90ad0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65  nitialize and de
90ae0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  initialize the m
90af0 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a  utex subsystem..
90b00 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
90b10 6e 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29  nMutexInit(void)
90b20 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
90b30 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74  OK; }.static int
90b40 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69   winMutexEnd(voi
90b50 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
90b60 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK; }../*.** T
90b70 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
90b80 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65  _alloc() routine
90b90 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77   allocates a new
90ba0 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65  .** mutex and re
90bb0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
90bc0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65  to it.  If it re
90bd0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  turns NULL.** th
90be0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  at means that a 
90bf0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20  mutex could not 
90c00 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53  be allocated.  S
90c10 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e  QLite.** will un
90c20 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61  wind its stack a
90c30 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  nd return an err
90c40 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e  or.  The argumen
90c50 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  t.** to sqlite3_
90c60 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73  mutex_alloc() is
90c70 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e   one of these in
90c80 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a  teger constants:
90c90 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
90ca0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
90cb0 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20  X_FAST          
90cc0 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20       0.** <li>  
90cd0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
90ce0 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20 20  URSIVE          
90cf0 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  1.** <li>  SQLIT
90d00 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
90d10 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a 20  ASTER      2.** 
90d20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
90d30 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20  EX_STATIC_MEM   
90d40 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20        3.** <li> 
90d50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
90d60 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 20  ATIC_PRNG       
90d70 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a   4.** </ul>.**.*
90d80 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20  * The first two 
90d90 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20  constants cause 
90da0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
90db0 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a  loc() to create.
90dc0 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20  ** a new mutex. 
90dd0 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69   The new mutex i
90de0 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e  s recursive when
90df0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
90e00 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73  CURSIVE.** is us
90e10 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73  ed but not neces
90e20 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53  sarily so when S
90e30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
90e40 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65   is used..** The
90e50 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
90e60 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e  ation does not n
90e70 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69  eed to make a di
90e80 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74  stinction.** bet
90e90 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  ween SQLITE_MUTE
90ea0 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 20  X_RECURSIVE and 
90eb0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
90ec0 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20  T if it does.** 
90ed0 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75  not want to.  Bu
90ee0 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e  t SQLite will on
90ef0 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63  ly request a rec
90f00 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a  ursive mutex in.
90f10 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69  ** cases where i
90f20 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f  t really needs o
90f30 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 72  ne.  If a faster
90f40 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d   non-recursive m
90f50 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  utex.** implemen
90f60 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61  tation is availa
90f70 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20  ble on the host 
90f80 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75  platform, the mu
90f90 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a  tex subsystem.**
90fa0 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 75   might return su
90fb0 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65  ch a mutex in re
90fc0 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45  sponse to SQLITE
90fd0 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a  _MUTEX_FAST..**.
90fe0 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c  ** The other all
90ff0 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 20  owed parameters 
91000 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
91010 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65  _alloc() each re
91020 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
91030 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72  r to a static pr
91040 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e  eexisting mutex.
91050 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20 6d    Three static m
91060 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73  utexes are.** us
91070 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e  ed by the curren
91080 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c  t version of SQL
91090 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72  ite.  Future ver
910a0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a  sions of SQLite.
910b0 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74  ** may add addit
910c0 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74  ional static mut
910d0 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75  exes.  Static mu
910e0 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e  texes are for in
910f0 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79  ternal.** use by
91100 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41   SQLite only.  A
91110 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
91120 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65   use SQLite mute
91130 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73  xes should.** us
91140 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d  e only the dynam
91150 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72  ic mutexes retur
91160 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55  ned by SQLITE_MU
91170 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53  TEX_FAST or.** S
91180 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
91190 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  RSIVE..**.** Not
911a0 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66  e that if one of
911b0 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
911c0 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53  ex parameters (S
911d0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
911e0 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55  .** or SQLITE_MU
911f0 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69  TEX_RECURSIVE) i
91200 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69  s used then sqli
91210 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
91220 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64  ).** returns a d
91230 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f  ifferent mutex o
91240 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42  n every call.  B
91250 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69  ut for the stati
91260 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65  c .** mutex type
91270 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65  s, the same mute
91280 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  x is returned on
91290 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74   every call that
912a0 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65   has.** the same
912b0 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f   type number..*/
912c0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
912d0 6d 75 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41  mutex *winMutexA
912e0 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b  lloc(int iType){
912f0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
91300 20 2a 70 3b 0a 0a 20 20 73 77 69 74 63 68 28 20   *p;..  switch( 
91310 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  iType ){.    cas
91320 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  e SQLITE_MUTEX_F
91330 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51  AST:.    case SQ
91340 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
91350 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20  SIVE: {.      p 
91360 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
91370 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20  ero( sizeof(*p) 
91380 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29  );.      if( p )
91390 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20  {.        p->id 
913a0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20  = iType;.       
913b0 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69   InitializeCriti
913c0 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d  calSection(&p->m
913d0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20  utex);.      }. 
913e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
913f0 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
91400 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71  .      static sq
91410 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74  lite3_mutex stat
91420 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b 0a 20 20  icMutexes[6];.  
91430 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 69      static int i
91440 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  sInit = 0;.     
91450 20 77 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20   while( !isInit 
91460 29 7b 0a 20 20 20 20 20 20 20 20 73 74 61 74 69  ){.        stati
91470 63 20 6c 6f 6e 67 20 6c 6f 63 6b 20 3d 20 30 3b  c long lock = 0;
91480 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 6e 74  .        if( Int
91490 65 72 6c 6f 63 6b 65 64 49 6e 63 72 65 6d 65 6e  erlockedIncremen
914a0 74 28 26 6c 6f 63 6b 29 3d 3d 31 20 29 7b 0a 20  t(&lock)==1 ){. 
914b0 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a           int i;.
914c0 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
914d0 30 3b 20 69 3c 73 69 7a 65 6f 66 28 73 74 61 74  0; i<sizeof(stat
914e0 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65 6f  icMutexes)/sizeo
914f0 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b  f(staticMutexes[
91500 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  0]); i++){.     
91510 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a         Initializ
91520 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  eCriticalSection
91530 28 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b  (&staticMutexes[
91540 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  i].mutex);.     
91550 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
91560 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20   isInit = 1;.   
91570 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
91580 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a        Sleep(1);.
91590 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
915a0 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
915b0 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a  iType-2 >= 0 );.
915c0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54        assert( iT
915d0 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 73  ype-2 < sizeof(s
915e0 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69  taticMutexes)/si
915f0 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78  zeof(staticMutex
91600 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 20  es[0]) );.      
91610 70 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78  p = &staticMutex
91620 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20  es[iType-2];.   
91630 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65     p->id = iType
91640 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
91650 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
91660 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  n p;.}.../*.** T
91670 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c  his routine deal
91680 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f  locates a previo
91690 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  usly.** allocate
916a0 64 20 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65  d mutex.  SQLite
916b0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64   is careful to d
916c0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a  eallocate every.
916d0 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 20 69 74  ** mutex that it
916e0 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73   allocates..*/.s
916f0 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75  tatic void winMu
91700 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
91710 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
91720 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65  ert( p );.  asse
91730 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  rt( p->nRef==0 )
91740 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
91750 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
91760 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  FAST || p->id==S
91770 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
91780 52 53 49 56 45 20 29 3b 0a 20 20 44 65 6c 65 74  RSIVE );.  Delet
91790 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  eCriticalSection
917a0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73  (&p->mutex);.  s
917b0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
917c0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
917d0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
917e0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
917f0 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69  utex_try() routi
91800 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  nes attempt.** t
91810 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e  o enter a mutex.
91820 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72    If another thr
91830 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
91840 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
91850 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
91860 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
91870 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
91880 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
91890 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
918a0 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71  TE_BUSY.  The sq
918b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
918c0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
918d0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  rns SQLITE_OK.**
918e0 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c   upon successful
918f0 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73   entry.  Mutexes
91900 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53   created using S
91910 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
91920 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20  RSIVE can.** be 
91930 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  entered multiple
91940 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61   times by the sa
91950 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73  me thread.  In s
91960 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
91970 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
91980 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
91990 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
919a0 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
919b0 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
919c0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65  er.  If the same
919d0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f   thread tries to
919e0 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72   enter any other
919f0 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a   kind of mutex.*
91a00 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
91a10 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
91a20 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
91a30 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d  static void winM
91a40 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65  utexEnter(sqlite
91a50 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
91a60 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
91a70 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
91a80 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78  SIVE || winMutex
91a90 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
91aa0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
91ab0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b  tion(&p->mutex);
91ac0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65  .  p->owner = Ge
91ad0 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64  tCurrentThreadId
91ae0 28 29 3b 20 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b  (); .  p->nRef++
91af0 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ;.}.static int w
91b00 69 6e 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74  inMutexTry(sqlit
91b10 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
91b20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
91b30 42 55 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20  BUSY;.  assert( 
91b40 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
91b50 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
91b60 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64   winMutexNotheld
91b70 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a  (p) );.  /*.  **
91b80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
91b90 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
91ba0 20 69 73 20 76 65 72 79 20 72 61 72 65 6c 79 20   is very rarely 
91bb0 75 73 65 64 2c 20 61 6e 64 20 77 68 65 6e 20 69  used, and when i
91bc0 74 0a 20 20 2a 2a 20 69 73 20 75 73 65 64 20 69  t.  ** is used i
91bd0 74 20 69 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f  t is merely an o
91be0 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f  ptimization.  So
91bf0 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 69 74   it is OK for it
91c00 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   to always.  ** 
91c10 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a  fail.  .  **.  *
91c20 2a 20 54 68 65 20 54 72 79 45 6e 74 65 72 43 72  * The TryEnterCr
91c30 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20  iticalSection() 
91c40 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c  interface is onl
91c50 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57  y available on W
91c60 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73  inNT..  ** And s
91c70 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70  ome windows comp
91c80 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69  ilers complain i
91c90 66 20 79 6f 75 20 74 72 79 20 74 6f 20 75 73 65  f you try to use
91ca0 20 69 74 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a   it without.  **
91cb0 20 66 69 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d   first doing som
91cc0 65 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20  e #defines that 
91cd0 70 72 65 76 65 6e 74 20 53 51 4c 69 74 65 20 66  prevent SQLite f
91ce0 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20  rom building on 
91cf0 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20  Win98..  ** For 
91d00 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20  that reason, we 
91d10 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f  will omit this o
91d20 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20  ptimization for 
91d30 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74  now.  See.  ** t
91d40 69 63 6b 65 74 20 23 32 36 38 35 2e 0a 20 20 2a  icket #2685..  *
91d50 2f 0a 23 69 66 20 30 0a 20 20 69 66 28 20 6d 75  /.#if 0.  if( mu
91d60 74 65 78 49 73 4e 54 28 29 20 26 26 20 54 72 79  texIsNT() && Try
91d70 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
91d80 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20  tion(&p->mutex) 
91d90 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20  ){.    p->owner 
91da0 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  = GetCurrentThre
91db0 61 64 49 64 28 29 3b 0a 20 20 20 20 70 2d 3e 6e  adId();.    p->n
91dc0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
91dd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23  SQLITE_OK;.  }.#
91de0 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41  else.  UNUSED_PA
91df0 52 41 4d 45 54 45 52 28 70 29 3b 0a 23 65 6e 64  RAMETER(p);.#end
91e00 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  if.  return rc;.
91e10 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
91e20 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
91e30 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
91e40 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
91e50 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
91e60 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
91e70 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
91e80 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20   behavior.** is 
91e90 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
91ea0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75   mutex is not cu
91eb0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
91ec0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  or.** is not cur
91ed0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
91ee0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
91ef0 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a  ever do either..
91f00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
91f10 69 6e 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c  inMutexLeave(sql
91f20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
91f30 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
91f40 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  f>0 );.  assert(
91f50 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75   p->owner==GetCu
91f60 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 20  rrentThreadId() 
91f70 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a  );.  p->nRef--;.
91f80 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
91f90 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  f==0 || p->id==S
91fa0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
91fb0 52 53 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65  RSIVE );.  Leave
91fc0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
91fd0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53  &p->mutex);.}..S
91fe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
91ff0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
92000 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61  ods *sqlite3Defa
92010 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  ultMutex(void){.
92020 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
92030 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
92040 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 77 69  Mutex = {.    wi
92050 6e 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20  nMutexInit,.    
92060 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20  winMutexEnd,.   
92070 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a   winMutexAlloc,.
92080 20 20 20 20 77 69 6e 4d 75 74 65 78 46 72 65 65      winMutexFree
92090 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e  ,.    winMutexEn
920a0 74 65 72 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  ter,.    winMute
920b0 78 54 72 79 2c 0a 20 20 20 20 77 69 6e 4d 75 74  xTry,.    winMut
920c0 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20  exLeave,.#ifdef 
920d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
920e0 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 2c 0a 20   winMutexHeld,. 
920f0 20 20 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65     winMutexNothe
92100 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a  ld.#else.    0,.
92110 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d      0.#endif.  }
92120 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75  ;..  return &sMu
92130 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  tex;.}.#endif /*
92140 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
92150 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  2 */../*********
92160 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74  ***** End of mut
92170 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ex_w32.c *******
92180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
921a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
921b0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
921c0 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a   malloc.c ******
921d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
921e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
921f0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
92200 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
92210 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
92220 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
92230 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
92240 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
92250 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
92260 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
92270 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
92280 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
92290 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
922a0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
922b0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
922c0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
922d0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
922e0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
922f0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
92300 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
92310 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
92320 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
92330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
92370 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** Memory alloca
92380 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75  tion functions u
92390 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73  sed throughout s
923a0 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  qlite..**.** $Id
923b0 3a 20 6d 61 6c 6c 6f 63 2e 63 2c 76 20 31 2e 36  : malloc.c,v 1.6
923c0 31 20 32 30 30 39 2f 30 33 2f 32 34 20 31 35 3a  1 2009/03/24 15:
923d0 30 38 3a 31 30 20 64 72 68 20 45 78 70 20 24 0a  08:10 drh Exp $.
923e0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
923f0 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e  outine runs when
92400 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
92410 63 61 74 6f 72 20 73 65 65 73 20 74 68 61 74 20  cator sees that 
92420 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d  the.** total mem
92430 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  ory allocation i
92440 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65  s about to excee
92450 64 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a  d the soft heap.
92460 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61  ** limit..*/.sta
92470 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61  tic void softHea
92480 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a  pLimitEnforcer(.
92490 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c    void *NotUsed,
924a0 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36   .  sqlite3_int6
924b0 34 20 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e  4 NotUsed2,.  in
924c0 74 20 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20  t allocSize.){. 
924d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
924e0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
924f0 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
92500 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
92510 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f  allocSize);.}../
92520 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66  *.** Set the sof
92530 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69  t heap-size limi
92540 74 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72  t for the librar
92550 79 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72  y. Passing a zer
92560 6f 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76  o or .** negativ
92570 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  e value indicate
92580 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53  s no limit..*/.S
92590 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
925a0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
925b0 5f 6c 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20  _limit(int n){. 
925c0 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20   sqlite3_uint64 
925d0 69 4c 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76  iLimit;.  int ov
925e0 65 72 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30  erage;.  if( n<0
925f0 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d   ){.    iLimit =
92600 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
92610 20 69 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d   iLimit = n;.  }
92620 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  .  sqlite3_initi
92630 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 69  alize();.  if( i
92640 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 73  Limit>0 ){.    s
92650 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72  qlite3MemoryAlar
92660 6d 28 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45  m(softHeapLimitE
92670 6e 66 6f 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d  nforcer, 0, iLim
92680 69 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  it);.  }else{.  
92690 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41    sqlite3MemoryA
926a0 6c 61 72 6d 28 30 2c 20 30 2c 20 30 29 3b 0a 20  larm(0, 0, 0);. 
926b0 20 7d 0a 20 20 6f 76 65 72 61 67 65 20 3d 20 28   }.  overage = (
926c0 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 6d 65 6d  int)(sqlite3_mem
926d0 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 28 69 36  ory_used() - (i6
926e0 34 29 6e 29 3b 0a 20 20 69 66 28 20 6f 76 65 72  4)n);.  if( over
926f0 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  age>0 ){.    sql
92700 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
92710 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 20  ory(overage);.  
92720 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  }.}../*.** Attem
92730 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 75 70  pt to release up
92740 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 6e   to n bytes of n
92750 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
92760 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a  ory currently.**
92770 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65 2e   held by SQLite.
92780 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e   An example of n
92790 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
927a0 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 73  ory is memory us
927b0 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 64  ed to.** cache d
927c0 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68  atabase pages th
927d0 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65  at are not curre
927e0 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a  ntly in use..*/.
927f0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
92800 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
92810 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69  emory(int n){.#i
92820 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
92830 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
92840 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20  MENT.  int nRet 
92850 3d 20 30 3b 0a 23 69 66 20 30 0a 20 20 6e 52 65  = 0;.#if 0.  nRe
92860 74 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65  t += sqlite3Vdbe
92870 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29  ReleaseMemory(n)
92880 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 52 65 74 20  ;.#endif.  nRet 
92890 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  += sqlite3Pcache
928a0 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d  ReleaseMemory(n-
928b0 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  nRet);.  return 
928c0 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 4e  nRet;.#else.  UN
928d0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
928e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
928f0 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a  TE_OK;.#endif.}.
92900 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66  ./*.** State inf
92910 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74  ormation local t
92920 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  o the memory all
92930 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
92940 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c  m..*/.static SQL
92950 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d  ITE_WSD struct M
92960 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a  em0Global {.  /*
92970 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20   Number of free 
92980 70 61 67 65 73 20 66 6f 72 20 73 63 72 61 74 63  pages for scratc
92990 68 20 61 6e 64 20 70 61 67 65 2d 63 61 63 68 65  h and page-cache
929a0 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 32   memory */.  u32
929b0 20 6e 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20   nScratchFree;. 
929c0 20 75 33 32 20 6e 50 61 67 65 46 72 65 65 3b 0a   u32 nPageFree;.
929d0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
929e0 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20   *mutex;        
929f0 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 72   /* Mutex to ser
92a00 69 61 6c 69 7a 65 20 61 63 63 65 73 73 20 2a 2f  ialize access */
92a10 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  /*.  ** The 
92a20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61  alarm callback a
92a30 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73  nd its arguments
92a40 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65  .  The mem0.mute
92a50 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a  x lock will.  **
92a60 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 74   be held while t
92a70 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72  he callback is r
92a80 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 69  unning.  Recursi
92a90 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20  ve calls into.  
92aa0 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75  ** the memory su
92ab0 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f  bsystem are allo
92ac0 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20  wed, but no new 
92ad0 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62  callbacks will b
92ae0 65 0a 20 20 2a 2a 20 69 73 73 75 65 64 2e 20 20  e.  ** issued.  
92af0 54 68 65 20 61 6c 61 72 6d 42 75 73 79 20 76 61  The alarmBusy va
92b00 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  riable is set to
92b10 20 70 72 65 76 65 6e 74 20 72 65 63 75 72 73 69   prevent recursi
92b20 76 65 0a 20 20 2a 2a 20 63 61 6c 6c 62 61 63 6b  ve.  ** callback
92b30 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  s..  */.  sqlite
92b40 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 72  3_int64 alarmThr
92b50 65 73 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28  eshold;.  void (
92b60 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28  *alarmCallback)(
92b70 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69  void*, sqlite3_i
92b80 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69  nt64,int);.  voi
92b90 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0a 20 20 69  d *alarmArg;.  i
92ba0 6e 74 20 61 6c 61 72 6d 42 75 73 79 3b 0a 0a 20  nt alarmBusy;.. 
92bb0 20 2f 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72   /*.  ** Pointer
92bc0 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  s to the end of 
92bd0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
92be0 66 69 67 2e 70 53 63 72 61 74 63 68 20 61 6e 64  fig.pScratch and
92bf0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f  .  ** sqlite3Glo
92c00 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20  balConfig.pPage 
92c10 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65  to a block of me
92c20 6d 6f 72 79 20 74 68 61 74 20 72 65 63 6f 72 64  mory that record
92c30 73 0a 20 20 2a 2a 20 77 68 69 63 68 20 70 61 67  s.  ** which pag
92c40 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  es are available
92c50 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53  ..  */.  u32 *aS
92c60 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33  cratchFree;.  u3
92c70 32 20 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 20  2 *aPageFree;.} 
92c80 6d 65 6d 30 20 3d 20 7b 20 36 32 35 36 30 39 35  mem0 = { 6256095
92c90 35 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  5, 0, 0, 0, 0, 0
92ca0 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 64  , 0, 0, 0 };..#d
92cb0 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41  efine mem0 GLOBA
92cc0 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f  L(struct Mem0Glo
92cd0 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a  bal, mem0)../*.*
92ce0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
92cf0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
92d00 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  on subsystem..*/
92d10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
92d20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  int sqlite3Mallo
92d30 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 69  cInit(void){.  i
92d40 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
92d50 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63  Config.m.xMalloc
92d60 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
92d70 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
92d80 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  );.  }.  memset(
92d90 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66  &mem0, 0, sizeof
92da0 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 73  (mem0));.  if( s
92db0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
92dc0 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b  ig.bCoreMutex ){
92dd0 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 20  .    mem0.mutex 
92de0 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
92df0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
92e00 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20  _STATIC_MEM);.  
92e10 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  }.  if( sqlite3G
92e20 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
92e30 61 74 63 68 20 26 26 20 73 71 6c 69 74 65 33 47  atch && sqlite3G
92e40 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63  lobalConfig.szSc
92e50 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 20  ratch>=100.     
92e60 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
92e70 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68  lConfig.nScratch
92e80 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  >=0 ){.    int i
92e90 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
92ea0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
92eb0 74 63 68 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38  tch = ROUNDDOWN8
92ec0 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
92ed0 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2d 34  nfig.szScratch-4
92ee0 29 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 53 63 72  );.    mem0.aScr
92ef0 61 74 63 68 46 72 65 65 20 3d 20 28 75 33 32 2a  atchFree = (u32*
92f00 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65  )&((char*)sqlite
92f10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53  3GlobalConfig.pS
92f20 63 72 61 74 63 68 29 0a 20 20 20 20 20 20 20 20  cratch).        
92f30 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
92f40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
92f50 7a 53 63 72 61 74 63 68 2a 73 71 6c 69 74 65 33  zScratch*sqlite3
92f60 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63  GlobalConfig.nSc
92f70 72 61 74 63 68 5d 3b 0a 20 20 20 20 66 6f 72 28  ratch];.    for(
92f80 69 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c  i=0; i<sqlite3Gl
92f90 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
92fa0 74 63 68 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e  tch; i++){ mem0.
92fb0 61 53 63 72 61 74 63 68 46 72 65 65 5b 69 5d 20  aScratchFree[i] 
92fc0 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e  = i; }.    mem0.
92fd0 6e 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 73  nScratchFree = s
92fe0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
92ff0 69 67 2e 6e 53 63 72 61 74 63 68 3b 0a 20 20 7d  ig.nScratch;.  }
93000 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
93010 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53  3GlobalConfig.pS
93020 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20  cratch = 0;.    
93030 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
93040 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20  fig.szScratch = 
93050 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  0;.  }.  if( sql
93060 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
93070 2e 70 50 61 67 65 20 26 26 20 73 71 6c 69 74 65  .pPage && sqlite
93080 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
93090 50 61 67 65 3e 3d 35 31 32 0a 20 20 20 20 20 20  Page>=512.      
930a0 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  && sqlite3Global
930b0 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3e 3d 31 20  Config.nPage>=1 
930c0 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
930d0 20 20 69 6e 74 20 6f 76 65 72 68 65 61 64 3b 0a    int overhead;.
930e0 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 52 4f 55      int sz = ROU
930f0 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47  NDDOWN8(sqlite3G
93100 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61  lobalConfig.szPa
93110 67 65 29 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  ge);.    int n =
93120 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
93130 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20  nfig.nPage;.    
93140 6f 76 65 72 68 65 61 64 20 3d 20 28 34 2a 6e 20  overhead = (4*n 
93150 2b 20 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20  + sz - 1)/sz;.  
93160 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
93170 6f 6e 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f  onfig.nPage -= o
93180 76 65 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d  verhead;.    mem
93190 30 2e 61 50 61 67 65 46 72 65 65 20 3d 20 28 75  0.aPageFree = (u
931a0 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c  32*)&((char*)sql
931b0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
931c0 2e 70 50 61 67 65 29 0a 20 20 20 20 20 20 20 20  .pPage).        
931d0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
931e0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
931f0 7a 50 61 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f  zPage*sqlite3Glo
93200 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d  balConfig.nPage]
93210 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
93220 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  <sqlite3GlobalCo
93230 6e 66 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29  nfig.nPage; i++)
93240 7b 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65  { mem0.aPageFree
93250 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d  [i] = i; }.    m
93260 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 20 3d 20  em0.nPageFree = 
93270 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
93280 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c  fig.nPage;.  }el
93290 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  se{.    sqlite3G
932a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
932b0 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  e = 0;.    sqlit
932c0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
932d0 7a 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  zPage = 0;.  }. 
932e0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47   return sqlite3G
932f0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49  lobalConfig.m.xI
93300 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  nit(sqlite3Globa
93310 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61  lConfig.m.pAppDa
93320 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  ta);.}../*.** De
93330 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  initialize the m
93340 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
93350 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53   subsystem..*/.S
93360 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
93370 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  id sqlite3Malloc
93380 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  End(void){.  if(
93390 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
933a0 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e  nfig.m.xShutdown
933b0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   ){.    sqlite3G
933c0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53  lobalConfig.m.xS
933d0 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47  hutdown(sqlite3G
933e0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41  lobalConfig.m.pA
933f0 70 70 44 61 74 61 29 3b 0a 20 20 7d 0a 20 20 6d  ppData);.  }.  m
93400 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20  emset(&mem0, 0, 
93410 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d  sizeof(mem0));.}
93420 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
93430 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  he amount of mem
93440 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68  ory currently ch
93450 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51  ecked out..*/.SQ
93460 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
93470 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d  _int64 sqlite3_m
93480 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29  emory_used(void)
93490 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20  {.  int n, mx;. 
934a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72   sqlite3_int64 r
934b0 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  es;.  sqlite3_st
934c0 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54  atus(SQLITE_STAT
934d0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20  US_MEMORY_USED, 
934e0 26 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72  &n, &mx, 0);.  r
934f0 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  es = (sqlite3_in
93500 74 36 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20  t64)n;  /* Work 
93510 61 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f  around bug in Bo
93520 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20  rland C. Ticket 
93530 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72  #3216 */.  retur
93540 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n res;.}../*.** 
93550 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d  Return the maxim
93560 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  um amount of mem
93570 6f 72 79 20 74 68 61 74 20 68 61 73 20 65 76 65  ory that has eve
93580 72 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65  r been.** checke
93590 64 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68  d out since eith
935a0 65 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  er the beginning
935b0 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73   of this process
935c0 0a 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65  .** or since the
935d0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73   most recent res
935e0 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  et..*/.SQLITE_AP
935f0 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  I sqlite3_int64 
93600 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68  sqlite3_memory_h
93610 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73  ighwater(int res
93620 65 74 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e  etFlag){.  int n
93630 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f  , mx;.  sqlite3_
93640 69 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c  int64 res;.  sql
93650 69 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49  ite3_status(SQLI
93660 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
93670 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20  _USED, &n, &mx, 
93680 72 65 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 65  resetFlag);.  re
93690 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  s = (sqlite3_int
936a0 36 34 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20  64)mx;  /* Work 
936b0 61 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f  around bug in Bo
936c0 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20  rland C. Ticket 
936d0 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72  #3216 */.  retur
936e0 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n res;.}../*.** 
936f0 43 68 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d  Change the alarm
93700 20 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c   callback.*/.SQL
93710 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
93720 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61  sqlite3MemoryAla
93730 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c  rm(.  void(*xCal
93740 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72  lback)(void *pAr
93750 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  g, sqlite3_int64
93760 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20   used,int N),.  
93770 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71  void *pArg,.  sq
93780 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72  lite3_int64 iThr
93790 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69  eshold.){.  sqli
937a0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
937b0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d  mem0.mutex);.  m
937c0 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
937d0 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20  k = xCallback;. 
937e0 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d   mem0.alarmArg =
937f0 20 70 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c   pArg;.  mem0.al
93800 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69  armThreshold = i
93810 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c  Threshold;.  sql
93820 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
93830 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
93840 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
93850 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
93860 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41  ITE_OMIT_DEPRECA
93870 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63  TED./*.** Deprec
93880 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e  ated external in
93890 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e  terface.  Intern
938a0 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63  al/core SQLite c
938b0 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61  ode.** should ca
938c0 6c 6c 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79  ll sqlite3Memory
938d0 41 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Alarm..*/.SQLITE
938e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
938f0 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20  _memory_alarm(. 
93900 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b   void(*xCallback
93910 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71  )(void *pArg, sq
93920 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64  lite3_int64 used
93930 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20  ,int N),.  void 
93940 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33  *pArg,.  sqlite3
93950 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c  _int64 iThreshol
93960 64 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  d.){.  return sq
93970 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d  lite3MemoryAlarm
93980 28 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67  (xCallback, pArg
93990 2c 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d  , iThreshold);.}
939a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
939b0 72 69 67 67 65 72 20 74 68 65 20 61 6c 61 72 6d  rigger the alarm
939c0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64   .*/.static void
939d0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c   sqlite3MallocAl
939e0 61 72 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  arm(int nByte){.
939f0 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61    void (*xCallba
93a00 63 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  ck)(void*,sqlite
93a10 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20  3_int64,int);.  
93a20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f  sqlite3_int64 no
93a30 77 55 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70  wUsed;.  void *p
93a40 41 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e  Arg;.  if( mem0.
93a50 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30  alarmCallback==0
93a60 20 7c 7c 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75   || mem0.alarmBu
93a70 73 79 20 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  sy  ) return;.  
93a80 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d  mem0.alarmBusy =
93a90 20 31 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20   1;.  xCallback 
93aa0 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c  = mem0.alarmCall
93ab0 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20  back;.  nowUsed 
93ac0 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56  = sqlite3StatusV
93ad0 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54  alue(SQLITE_STAT
93ae0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b  US_MEMORY_USED);
93af0 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61  .  pArg = mem0.a
93b00 6c 61 72 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74  larmArg;.  sqlit
93b10 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
93b20 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43  em0.mutex);.  xC
93b30 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f  allback(pArg, no
93b40 77 55 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20  wUsed, nByte);. 
93b50 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
93b60 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
93b70 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75  ;.  mem0.alarmBu
93b80 73 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  sy = 0;.}../*.**
93b90 20 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   Do a memory all
93ba0 6f 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61  ocation with sta
93bb0 74 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72  tistics and alar
93bc0 6d 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a  ms.  Assume the.
93bd0 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61  ** lock is alrea
93be0 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74  dy held..*/.stat
93bf0 69 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74  ic int mallocWit
93c00 68 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f  hAlarm(int n, vo
93c10 69 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20  id **pp){.  int 
93c20 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70  nFull;.  void *p
93c30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
93c40 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
93c50 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em0.mutex) );.  
93c60 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  nFull = sqlite3G
93c70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
93c80 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c  oundup(n);.  sql
93c90 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51  ite3StatusSet(SQ
93ca0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c  LITE_STATUS_MALL
93cb0 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69  OC_SIZE, n);.  i
93cc0 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c  f( mem0.alarmCal
93cd0 6c 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20  lback!=0 ){.    
93ce0 69 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69  int nUsed = sqli
93cf0 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53  te3StatusValue(S
93d00 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
93d10 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69  ORY_USED);.    i
93d20 66 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e  f( nUsed+nFull >
93d30 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65  = mem0.alarmThre
93d40 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 73  shold ){.      s
93d50 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72  qlite3MallocAlar
93d60 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a  m(nFull);.    }.
93d70 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65    }.  p = sqlite
93d80 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
93d90 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a  xMalloc(nFull);.
93da0 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20 6d 65    if( p==0 && me
93db0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
93dc0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d   ){.    sqlite3M
93dd0 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c  allocAlarm(nFull
93de0 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  );.    p = sqlit
93df0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
93e00 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b  .xMalloc(nFull);
93e10 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a  .  }.  if( p ){.
93e20 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69      nFull = sqli
93e30 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
93e40 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
93e50 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
93e60 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
93e70 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20  , nFull);.  }.  
93e80 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72  *pp = p;.  retur
93e90 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a  n nFull;.}../*.*
93ea0 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
93eb0 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  y.  This routine
93ec0 20 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 33   is like sqlite3
93ed0 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 74  _malloc() except
93ee0 20 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 75   that it.** assu
93ef0 6d 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73  mes the memory s
93f00 75 62 73 79 73 74 65 6d 20 68 61 73 20 61 6c 72  ubsystem has alr
93f10 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61  eady been initia
93f20 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  lized..*/.SQLITE
93f30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
93f40 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74  qlite3Malloc(int
93f50 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a   n){.  void *p;.
93f60 20 20 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 4e 45    if( n<=0 || NE
93f70 56 45 52 28 6e 3e 3d 30 78 37 66 66 66 66 66 30  VER(n>=0x7fffff0
93f80 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  0) ){.    /* The
93f90 20 4e 45 56 45 52 28 6e 3e 3d 30 78 37 66 66 66   NEVER(n>=0x7fff
93fa0 66 66 30 30 29 20 74 65 72 6d 20 69 73 20 61 64  ff00) term is ad
93fb0 64 65 64 20 6f 75 74 20 6f 66 20 70 61 72 61 6e  ded out of paran
93fc0 6f 69 61 2e 20 20 57 65 20 77 61 6e 74 20 74 6f  oia.  We want to
93fd0 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 61 62 73   make.    ** abs
93fe0 6f 6c 75 74 65 6c 79 20 73 75 72 65 20 74 68 61  olutely sure tha
93ff0 74 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69  t there is nothi
94000 6e 67 20 77 69 74 68 69 6e 20 53 51 4c 69 74 65  ng within SQLite
94010 20 74 68 61 74 20 63 61 6e 20 63 61 75 73 65 20   that can cause 
94020 61 0a 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79 20  a.    ** memory 
94030 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20  allocation of a 
94040 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
94050 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74 68  which is near th
94060 65 20 6d 61 78 69 6d 75 6d 20 73 69 67 6e 65 64  e maximum signed
94070 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20  .    ** integer 
94080 76 61 6c 75 65 20 61 6e 64 20 74 68 75 73 20 63  value and thus c
94090 61 75 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20  ause an integer 
940a0 6f 76 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20  overflow inside 
940b0 6f 66 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29  of the xMalloc()
940c0 0a 20 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e  .    ** implemen
940d0 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 6e 3e 3d  tation.  The n>=
940e0 30 78 37 66 66 66 66 66 30 30 20 67 69 76 65 73  0x7fffff00 gives
940f0 20 75 73 20 32 35 35 20 62 79 74 65 73 20 6f 66   us 255 bytes of
94100 20 68 65 61 64 72 6f 6f 6d 2e 20 20 54 68 65 0a   headroom.  The.
94110 20 20 20 20 2a 2a 20 74 65 73 74 20 73 68 6f 75      ** test shou
94120 6c 64 20 6e 65 76 65 72 20 62 65 20 74 72 75 65  ld never be true
94130 20 62 65 63 61 75 73 65 20 53 51 4c 49 54 45 5f   because SQLITE_
94140 4d 41 58 5f 4c 45 4e 47 54 48 20 73 68 6f 75 6c  MAX_LENGTH shoul
94150 64 20 62 65 20 6d 75 63 68 0a 20 20 20 20 2a 2a  d be much.    **
94160 20 6c 65 73 73 20 74 68 61 6e 20 30 78 37 66 66   less than 0x7ff
94170 66 66 66 30 30 20 61 6e 64 20 69 74 20 73 68 6f  fff00 and it sho
94180 75 6c 64 20 63 61 74 63 68 20 6c 61 72 67 65 20  uld catch large 
94190 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
941a0 6e 73 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65  ns.    ** before
941b0 20 74 68 65 79 20 72 65 61 63 68 20 74 68 69 73   they reach this
941c0 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 70   point. */.    p
941d0 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
941e0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
941f0 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
94200 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
94210 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
94220 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f  utex);.    mallo
94230 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70  cWithAlarm(n, &p
94240 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
94250 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
94260 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b  mutex);.  }else{
94270 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
94280 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
94290 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20  Malloc(n);.  }. 
942a0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
942b0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
942c0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
942d0 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72  llocation is for
942e0 20 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c   use by the appl
942f0 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73  ication..** Firs
94300 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  t make sure the 
94310 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d  memory subsystem
94320 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c   is initialized,
94330 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20   then do the.** 
94340 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  allocation..*/.S
94350 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
94360 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69  sqlite3_malloc(i
94370 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53  nt n){.#ifndef S
94380 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
94390 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  NIT.  if( sqlite
943a0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29  3_initialize() )
943b0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
943c0 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  f.  return sqlit
943d0 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a  e3Malloc(n);.}..
943e0 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61  /*.** Each threa
943f0 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20  d may only have 
94400 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e  a single outstan
94410 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  ding allocation 
94420 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68  from.** xScratch
94430 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65  Malloc().  We ve
94440 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72  rify this constr
94450 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67  aint in the sing
94460 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63  le-threaded.** c
94470 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 20 73  ase by setting s
94480 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74  cratchAllocOut t
94490 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f  o 1 when an allo
944a0 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74  cation.** is out
944b0 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e  standing clearin
944c0 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c  g it when the al
944d0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
944e0 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  d..*/.#if SQLITE
944f0 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
94500 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
94510 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63  G).static int sc
94520 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20  ratchAllocOut = 
94530 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  0;.#endif.../*.*
94540 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
94550 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  y that is to be 
94560 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65  used and release
94570 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a  d right away..**
94580 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
94590 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f   similar to allo
945a0 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 74 20  ca() in that it 
945b0 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a  is not intended.
945c0 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e  ** for situation
945d0 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f  s where the memo
945e0 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64  ry might be held
945f0 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69   long-term.  Thi
94600 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
94610 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20  intended to get 
94620 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61  memory to old la
94630 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61  rge transient da
94640 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73  ta.** structures
94650 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20   that would not 
94660 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20  normally fit on 
94670 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a  the stack of an.
94680 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63  ** embedded proc
94690 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  essor..*/.SQLITE
946a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
946b0 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c  qlite3ScratchMal
946c0 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  loc(int n){.  vo
946d0 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  id *p;.  assert(
946e0 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c   n>0 );..#if SQL
946f0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
94700 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44  0 && !defined(ND
94710 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66  EBUG).  /* Verif
94720 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74  y that no more t
94730 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20  han one scratch 
94740 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74  allocation per t
94750 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75  hread.  ** is ou
94760 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65  tstanding at one
94770 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73   time.  (This is
94780 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e   only checked in
94790 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65   the.  ** single
947a0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73  -threaded case s
947b0 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e  ince checking in
947c0 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61   the multi-threa
947d0 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f  ded case.  ** wo
947e0 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65  uld be much more
947f0 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a   complicated.) *
94800 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 72 61  /.  assert( scra
94810 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29  tchAllocOut==0 )
94820 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  ;.#endif..  if( 
94830 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
94840 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20  fig.szScratch<n 
94850 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61  ){.    goto scra
94860 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20  tch_overflow;.  
94870 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c  }else{  .    sql
94880 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
94890 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
948a0 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61    if( mem0.nScra
948b0 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20  tchFree==0 ){.  
948c0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
948d0 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
948e0 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ex);.      goto 
948f0 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77  scratch_overflow
94900 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
94910 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
94920 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63  i = mem0.aScratc
94930 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63  hFree[--mem0.nSc
94940 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20  ratchFree];.    
94950 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c    i *= sqlite3Gl
94960 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
94970 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69  atch;.      sqli
94980 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
94990 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
949a0 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20  CH_USED, 1);.   
949b0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
949c0 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Set(SQLITE_STATU
949d0 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20  S_SCRATCH_SIZE, 
949e0 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
949f0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
94a00 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
94a10 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63   p = (void*)&((c
94a20 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  har*)sqlite3Glob
94a30 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
94a40 68 29 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73  h)[i];.      ass
94a50 65 72 74 28 20 20 28 28 28 75 38 2a 29 70 20 2d  ert(  (((u8*)p -
94a60 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30   (u8*)0) & 7)==0
94a70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69   );.    }.  }.#i
94a80 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
94a90 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e  AFE==0 && !defin
94aa0 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72  ed(NDEBUG).  scr
94ab0 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70  atchAllocOut = p
94ac0 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72  !=0;.#endif..  r
94ad0 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63  eturn p;..scratc
94ae0 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66  h_overflow:.  if
94af0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
94b00 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
94b10 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
94b20 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
94b30 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  utex);.    sqlit
94b40 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49  e3StatusSet(SQLI
94b50 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
94b60 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20  H_SIZE, n);.    
94b70 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c  n = mallocWithAl
94b80 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20  arm(n, &p);.    
94b90 69 66 28 20 70 20 29 20 73 71 6c 69 74 65 33 53  if( p ) sqlite3S
94ba0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
94bb0 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f  STATUS_SCRATCH_O
94bc0 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20  VERFLOW, n);.   
94bd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
94be0 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
94bf0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
94c00 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
94c10 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63  Config.m.xMalloc
94c20 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c  (n);.  }.#if SQL
94c30 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
94c40 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44  0 && !defined(ND
94c50 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 68 41  EBUG).  scratchA
94c60 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a  llocOut = p!=0;.
94c70 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
94c80 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f  p;    .}.SQLITE_
94c90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
94ca0 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28  ite3ScratchFree(
94cb0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20  void *p){.  if( 
94cc0 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45  p ){..#if SQLITE
94cd0 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
94ce0 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
94cf0 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79  G).    /* Verify
94d00 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68   that no more th
94d10 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61  an one scratch a
94d20 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68  llocation per th
94d30 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f  read.    ** is o
94d40 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e  utstanding at on
94d50 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69  e time.  (This i
94d60 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69  s only checked i
94d70 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e  n the.    ** sin
94d80 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73  gle-threaded cas
94d90 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67  e since checking
94da0 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68   in the multi-th
94db0 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 20 20  readed case.    
94dc0 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68  ** would be much
94dd0 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65   more complicate
94de0 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  d.) */.    asser
94df0 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f  t( scratchAllocO
94e00 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 73 63 72  ut==1 );.    scr
94e10 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30  atchAllocOut = 0
94e20 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66  ;.#endif..    if
94e30 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
94e40 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 3d 3d  onfig.pScratch==
94e50 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  0.           || 
94e60 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  p<sqlite3GlobalC
94e70 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 0a 20  onfig.pScratch. 
94e80 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d            || p>=
94e90 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72  (void*)mem0.aScr
94ea0 61 74 63 68 46 72 65 65 20 29 7b 0a 20 20 20 20  atchFree ){.    
94eb0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
94ec0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
94ed0 61 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  at ){.        in
94ee0 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  t iSize = sqlite
94ef0 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a  3MallocSize(p);.
94f00 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
94f10 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
94f20 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20  .mutex);.       
94f30 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
94f40 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
94f50 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57  SCRATCH_OVERFLOW
94f60 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20  , -iSize);.     
94f70 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
94f80 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
94f90 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d  S_MEMORY_USED, -
94fa0 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20  iSize);.        
94fb0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
94fc0 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
94fd0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
94fe0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
94ff0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d  .mutex);.      }
95000 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
95010 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
95020 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20  g.m.xFree(p);.  
95030 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
95040 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
95050 20 20 20 20 69 20 3d 20 28 69 6e 74 29 28 28 75      i = (int)((u
95060 38 2a 29 70 20 2d 20 28 75 38 2a 29 73 71 6c 69  8*)p - (u8*)sqli
95070 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
95080 70 53 63 72 61 74 63 68 29 3b 0a 20 20 20 20 20  pScratch);.     
95090 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f   i /= sqlite3Glo
950a0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
950b0 74 63 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72  tch;.      asser
950c0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c  t( i>=0 && i<sql
950d0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
950e0 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20  .nScratch );.   
950f0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
95100 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
95110 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  x);.      assert
95120 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46  ( mem0.nScratchF
95130 72 65 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33  ree<(u32)sqlite3
95140 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63  GlobalConfig.nSc
95150 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d  ratch );.      m
95160 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65  em0.aScratchFree
95170 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72  [mem0.nScratchFr
95180 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20  ee++] = i;.     
95190 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
951a0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
951b0 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31  SCRATCH_USED, -1
951c0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
951d0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
951e0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a  0.mutex);.    }.
951f0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55    }.}../*.** TRU
95200 45 20 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b  E if p is a look
95210 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  aside memory all
95220 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a  ocation from db.
95230 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
95240 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45  E_OMIT_LOOKASIDE
95250 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f  .static int isLo
95260 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 65 33 20  okaside(sqlite3 
95270 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20  *db, void *p){. 
95280 20 72 65 74 75 72 6e 20 64 62 20 26 26 20 70 20   return db && p 
95290 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73  && p>=db->lookas
952a0 69 64 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c  ide.pStart && p<
952b0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45  db->lookaside.pE
952c0 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66  nd;.}.#else.#def
952d0 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28  ine isLookaside(
952e0 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f  A,B) 0.#endif../
952f0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
95300 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79  size of a memory
95310 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76   allocation prev
95320 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20  iously obtained 
95330 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d  from.** sqlite3M
95340 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74  alloc() or sqlit
95350 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  e3_malloc()..*/.
95360 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
95370 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  nt sqlite3Malloc
95380 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Size(void *p){. 
95390 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47   return sqlite3G
953a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53  lobalConfig.m.xS
953b0 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45  ize(p);.}.SQLITE
953c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
953d0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
953e0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f  (sqlite3 *db, vo
953f0 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  id *p){.  assert
95400 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ( db==0 || sqlit
95410 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
95420 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
95430 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ( p==0 ){.    re
95440 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20  turn 0;.  }else 
95450 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28  if( isLookaside(
95460 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65  db, p) ){.    re
95470 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  turn db->lookasi
95480 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a  de.sz;.  }else{.
95490 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
954a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
954b0 2e 78 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d  .xSize(p);.  }.}
954c0 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d  ../*.** Free mem
954d0 6f 72 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f  ory previously o
954e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
954f0 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  ite3Malloc()..*/
95500 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
95510 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f   sqlite3_free(vo
95520 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  id *p){.  if( p=
95530 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
95540 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
95550 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20  Config.bMemstat 
95560 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
95570 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e  utex_enter(mem0.
95580 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69  mutex);.    sqli
95590 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
955a0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
955b0 59 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33  Y_USED, -sqlite3
955c0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a  MallocSize(p));.
955d0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
955e0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28  lConfig.m.xFree(
955f0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  p);.    sqlite3_
95600 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
95610 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65  .mutex);.  }else
95620 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
95630 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65  balConfig.m.xFre
95640 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  e(p);.  }.}../*.
95650 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74  ** Free memory t
95660 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 73 73  hat might be ass
95670 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70  ociated with a p
95680 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
95690 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
956a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
956b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
956c0 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a  DbFree(sqlite3 *
956d0 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20  db, void *p){.  
956e0 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c  assert( db==0 ||
956f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
95700 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
95710 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73  ;.  if( isLookas
95720 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20  ide(db, p) ){.  
95730 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20    LookasideSlot 
95740 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69  *pBuf = (Lookasi
95750 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70  deSlot*)p;.    p
95760 42 75 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d  Buf->pNext = db-
95770 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
95780 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
95790 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66  ide.pFree = pBuf
957a0 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
957b0 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65  ide.nOut--;.  }e
957c0 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
957d0 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  _free(p);.  }.}.
957e0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
957f0 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
95800 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
95810 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54  ocation.*/.SQLIT
95820 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
95830 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76  sqlite3Realloc(v
95840 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e  oid *pOld, int n
95850 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f  Bytes){.  int nO
95860 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64  ld, nNew;.  void
95870 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f   *pNew;.  if( pO
95880 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ld==0 ){.    ret
95890 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  urn sqlite3Mallo
958a0 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20  c(nBytes);.  }. 
958b0 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 7c   if( nBytes<=0 |
958c0 7c 20 4e 45 56 45 52 28 6e 42 79 74 65 73 3e 3d  | NEVER(nBytes>=
958d0 30 78 37 66 66 66 66 66 30 30 29 20 29 7b 0a 20  0x7fffff00) ){. 
958e0 20 20 20 2f 2a 20 54 68 65 20 4e 45 56 45 52 28     /* The NEVER(
958f0 2e 2e 2e 29 20 74 65 72 6d 20 69 73 20 65 78 70  ...) term is exp
95900 6c 61 69 6e 65 64 20 69 6e 20 63 6f 6d 6d 65 6e  lained in commen
95910 74 73 20 6f 6e 20 73 71 6c 69 74 65 33 4d 61 6c  ts on sqlite3Mal
95920 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 73 71 6c  loc() */.    sql
95930 69 74 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b  ite3_free(pOld);
95940 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
95950 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c 69   }.  nOld = sqli
95960 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4f  te3MallocSize(pO
95970 6c 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ld);.  if( sqlit
95980 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
95990 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73  Memstat ){.    s
959a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
959b0 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
959c0 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
959d0 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54  sSet(SQLITE_STAT
959e0 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20  US_MALLOC_SIZE, 
959f0 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 6e 4e 65  nBytes);.    nNe
95a00 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  w = sqlite3Globa
95a10 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64  lConfig.m.xRound
95a20 75 70 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  up(nBytes);.    
95a30 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29  if( nOld==nNew )
95a40 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70  {.      pNew = p
95a50 4f 6c 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Old;.    }else{.
95a60 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
95a70 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c  3StatusValue(SQL
95a80 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
95a90 59 5f 55 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c  Y_USED)+nNew-nOl
95aa0 64 20 3e 3d 20 0a 20 20 20 20 20 20 20 20 20 20  d >= .          
95ab0 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65    mem0.alarmThre
95ac0 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 20  shold ){.       
95ad0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c   sqlite3MallocAl
95ae0 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a  arm(nNew-nOld);.
95af0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e        }.      pN
95b00 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ew = sqlite3Glob
95b10 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c  alConfig.m.xReal
95b20 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b  loc(pOld, nNew);
95b30 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d  .      if( pNew=
95b40 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d  =0 && mem0.alarm
95b50 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Callback ){.    
95b60 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f      sqlite3Mallo
95b70 63 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a  cAlarm(nBytes);.
95b80 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73          pNew = s
95b90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
95ba0 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f  ig.m.xRealloc(pO
95bb0 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20  ld, nNew);.     
95bc0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65   }.      if( pNe
95bd0 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 4e 65  w ){.        nNe
95be0 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  w = sqlite3Mallo
95bf0 63 53 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20  cSize(pNew);.   
95c00 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
95c10 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
95c20 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c  TUS_MEMORY_USED,
95c30 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20   nNew-nOld);.   
95c40 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
95c50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
95c60 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  ve(mem0.mutex);.
95c70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65    }else{.    pNe
95c80 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  w = sqlite3Globa
95c90 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c  lConfig.m.xReall
95ca0 6f 63 28 70 4f 6c 64 2c 20 6e 42 79 74 65 73 29  oc(pOld, nBytes)
95cb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
95cc0 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  New;.}../*.** Th
95cd0 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61  e public interfa
95ce0 63 65 20 74 6f 20 73 71 6c 69 74 65 33 52 65 61  ce to sqlite3Rea
95cf0 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20 73 75 72 65  lloc.  Make sure
95d00 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79   that the memory
95d10 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 20 69 73  .** subsystem is
95d20 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69   initialized pri
95d30 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73  or to invoking s
95d40 71 6c 69 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f  qliteRealloc..*/
95d50 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
95d60 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   *sqlite3_reallo
95d70 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e  c(void *pOld, in
95d80 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  t n){.#ifndef SQ
95d90 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
95da0 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  IT.  if( sqlite3
95db0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20  _initialize() ) 
95dc0 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
95dd0 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
95de0 33 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e  3Realloc(pOld, n
95df0 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  );.}.../*.** All
95e00 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d  ocate and zero m
95e10 65 6d 6f 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54  emory..*/ .SQLIT
95e20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
95e30 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
95e40 6f 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  o(int n){.  void
95e50 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c   *p = sqlite3Mal
95e60 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20  loc(n);.  if( p 
95e70 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c  ){.    memset(p,
95e80 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65   0, n);.  }.  re
95e90 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
95ea0 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65   Allocate and ze
95eb0 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74  ro memory.  If t
95ec0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  he allocation fa
95ed0 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65  ils, make.** the
95ee0 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c   mallocFailed fl
95ef0 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63  ag in the connec
95f00 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  tion pointer..*/
95f10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
95f20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d  void *sqlite3DbM
95f30 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65  allocZero(sqlite
95f40 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20  3 *db, int n){. 
95f50 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74   void *p = sqlit
95f60 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
95f70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  , n);.  if( p ){
95f80 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  .    memset(p, 0
95f90 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , n);.  }.  retu
95fa0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn p;.}../*.** A
95fb0 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f  llocate and zero
95fc0 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65   memory.  If the
95fd0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
95fe0 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d  s, make.** the m
95ff0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
96000 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   in the connecti
96010 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  on pointer..**.*
96020 2a 20 49 66 20 64 62 21 3d 30 20 61 6e 64 20 64  * If db!=0 and d
96030 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
96040 69 73 20 74 72 75 65 20 28 69 6e 64 69 63 61 74  is true (indicat
96050 69 6e 67 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c  ing a prior mall
96060 6f 63 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e  oc.** failure on
96070 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
96080 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74  se connection) t
96090 68 65 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72  hen always retur
960a0 6e 20 30 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f  n 0..** Hence fo
960b0 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  r a particular d
960c0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
960d0 6f 6e 2c 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20  on, once malloc 
960e0 73 74 61 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e  starts.** failin
960f0 67 2c 20 69 74 20 66 61 69 6c 73 20 63 6f 6e 73  g, it fails cons
96100 69 73 74 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d  istently until m
96110 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 72  allocFailed is r
96120 65 73 65 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73  eset..** This is
96130 20 61 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73   an important as
96140 73 75 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65  sumption.  There
96150 20 61 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73   are many places
96160 20 69 6e 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20   in the.** code 
96170 74 68 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c  that do things l
96180 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
96190 20 20 20 20 20 20 20 20 69 6e 74 20 2a 61 20 3d          int *a =
961a0 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62   (int*)sqlite3Db
961b0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31 30  MallocRaw(db, 10
961c0 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69  0);.**         i
961d0 6e 74 20 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71  nt *b = (int*)sq
961e0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
961f0 28 64 62 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20  (db, 200);.**   
96200 20 20 20 20 20 20 69 66 28 20 62 20 29 20 61 5b        if( b ) a[
96210 31 30 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49  10] = 9;.**.** I
96220 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
96230 66 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 6d  f a subsequent m
96240 61 6c 6c 6f 63 20 28 65 78 3a 20 22 62 22 29 20  alloc (ex: "b") 
96250 77 6f 72 6b 65 64 2c 20 69 74 20 69 73 20 61 73  worked, it is as
96260 73 75 6d 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c  sumed.** that al
96270 6c 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20  l prior mallocs 
96280 28 65 78 3a 20 22 61 22 29 20 77 6f 72 6b 65 64  (ex: "a") worked
96290 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   too..*/.SQLITE_
962a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
962b0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
962c0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
962d0 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b  t n){.  void *p;
962e0 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30  .  assert( db==0
962f0 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
96300 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
96310 29 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ) );.#ifndef SQL
96320 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49  ITE_OMIT_LOOKASI
96330 44 45 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20  DE.  if( db ){. 
96340 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74     LookasideSlot
96350 20 2a 70 42 75 66 3b 0a 20 20 20 20 69 66 28 20   *pBuf;.    if( 
96360 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
96370 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
96380 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
96390 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
963a0 62 45 6e 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64  bEnabled && n<=d
963b0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a  b->lookaside.sz.
963c0 20 20 20 20 20 20 20 20 20 26 26 20 28 70 42 75           && (pBu
963d0 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  f = db->lookasid
963e0 65 2e 70 46 72 65 65 29 21 3d 30 20 29 7b 0a 20  e.pFree)!=0 ){. 
963f0 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69       db->lookasi
96400 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 2d  de.pFree = pBuf-
96410 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 64 62  >pNext;.      db
96420 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74  ->lookaside.nOut
96430 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62  ++;.      if( db
96440 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74  ->lookaside.nOut
96450 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d  >db->lookaside.m
96460 78 4f 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20  xOut ){.        
96470 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78  db->lookaside.mx
96480 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  Out = db->lookas
96490 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20  ide.nOut;.      
964a0 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28  }.      return (
964b0 76 6f 69 64 2a 29 70 42 75 66 3b 0a 20 20 20 20  void*)pBuf;.    
964c0 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66  }.  }.#else.  if
964d0 28 20 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c  ( db && db->mall
964e0 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
964f0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65  return 0;.  }.#e
96500 6e 64 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74  ndif.  p = sqlit
96510 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69  e3Malloc(n);.  i
96520 66 28 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20  f( !p && db ){. 
96530 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
96540 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  led = 1;.  }.  r
96550 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
96560 2a 20 52 65 73 69 7a 65 20 74 68 65 20 62 6c 6f  * Resize the blo
96570 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69  ck of memory poi
96580 6e 74 65 64 20 74 6f 20 62 79 20 70 20 74 6f 20  nted to by p to 
96590 6e 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 0a  n bytes. If the.
965a0 2a 2a 20 72 65 73 69 7a 65 20 66 61 69 6c 73 2c  ** resize fails,
965b0 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46   set the mallocF
965c0 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68  ailed flag in th
965d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a  e connection obj
965e0 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ect..*/.SQLITE_P
965f0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
96600 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71  ite3DbRealloc(sq
96610 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
96620 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  *p, int n){.  vo
96630 69 64 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20  id *pNew = 0;.  
96640 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
96650 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
96660 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
96670 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
96680 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
96690 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ed==0 ){.    if(
966a0 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72   p==0 ){.      r
966b0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 44 62 4d  eturn sqlite3DbM
966c0 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b  allocRaw(db, n);
966d0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
966e0 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70  sLookaside(db, p
966f0 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  ) ){.      if( n
96700 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  <=db->lookaside.
96710 73 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  sz ){.        re
96720 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a  turn p;.      }.
96730 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
96740 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
96750 64 62 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66  db, n);.      if
96760 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( pNew ){.      
96770 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 70    memcpy(pNew, p
96780 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  , db->lookaside.
96790 73 7a 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  sz);.        sql
967a0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
967b0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
967c0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77  else{.      pNew
967d0 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
967e0 6f 63 28 70 2c 20 6e 29 3b 0a 20 20 20 20 20 20  oc(p, n);.      
967f0 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20  if( !pNew ){.   
96800 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
96810 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
96820 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
96830 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
96840 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
96850 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49  reallocate p.  I
96860 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 61 74 69  f the reallocati
96870 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 66  on fails, then f
96880 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74  ree p.** and set
96890 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65   the mallocFaile
968a0 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 64 61  d flag in the da
968b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
968c0 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
968d0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
968e0 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
968f0 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76  e(sqlite3 *db, v
96900 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a  oid *p, int n){.
96910 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20    void *pNew;.  
96920 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
96930 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e  Realloc(db, p, n
96940 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29  );.  if( !pNew )
96950 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
96960 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a  ree(db, p);.  }.
96970 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
96980 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63  ../*.** Make a c
96990 6f 70 79 20 6f 66 20 61 20 73 74 72 69 6e 67 20  opy of a string 
969a0 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  in memory obtain
969b0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61  ed from sqliteMa
969c0 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65 20 0a 2a  lloc(). These .*
969d0 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c  * functions call
969e0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61   sqlite3MallocRa
969f0 77 28 29 20 64 69 72 65 63 74 6c 79 20 69 6e 73  w() directly ins
96a00 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65 4d 61  tead of sqliteMa
96a10 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20  lloc(). This.** 
96a20 69 73 20 62 65 63 61 75 73 65 20 77 68 65 6e 20  is because when 
96a30 6d 65 6d 6f 72 79 20 64 65 62 75 67 67 69 6e 67  memory debugging
96a40 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74   is turned on, t
96a50 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f  hese two functio
96a60 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65  ns are .** calle
96a70 64 20 76 69 61 20 6d 61 63 72 6f 73 20 74 68 61  d via macros tha
96a80 74 20 72 65 63 6f 72 64 20 74 68 65 20 63 75 72  t record the cur
96a90 72 65 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c 69  rent file and li
96aa0 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65  ne number in the
96ab0 0a 2a 2a 20 54 68 72 65 61 64 44 61 74 61 20 73  .** ThreadData s
96ac0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c  tructure..*/.SQL
96ad0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
96ae0 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75   *sqlite3DbStrDu
96af0 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  p(sqlite3 *db, c
96b00 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
96b10 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73   char *zNew;.  s
96b20 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a  ize_t n;.  if( z
96b30 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
96b40 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 28  n 0;.  }.  n = (
96b50 64 62 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c  db ? sqlite3Strl
96b60 65 6e 28 64 62 2c 20 7a 29 20 3a 20 73 71 6c 69  en(db, z) : sqli
96b70 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 29 2b  te3Strlen30(z))+
96b80 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26  1;.  assert( (n&
96b90 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29  0x7fffffff)==n )
96ba0 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  zNew = sqlit
96bb0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
96bc0 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28  , (int)n);.  if(
96bd0 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d   zNew ){.    mem
96be0 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b  cpy(zNew, z, n);
96bf0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e  .  }.  return zN
96c00 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  ew;.}.SQLITE_PRI
96c10 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
96c20 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69  e3DbStrNDup(sqli
96c30 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
96c40 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a  har *z, int n){.
96c50 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20    char *zNew;.  
96c60 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  if( z==0 ){.    
96c70 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
96c80 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 66  assert( (n&0x7ff
96c90 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a  fffff)==n );.  z
96ca0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
96cb0 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31  allocRaw(db, n+1
96cc0 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b  );.  if( zNew ){
96cd0 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77  .    memcpy(zNew
96ce0 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65  , z, n);.    zNe
96cf0 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  w[n] = 0;.  }.  
96d00 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a  return zNew;.}..
96d10 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73  /*.** Create a s
96d20 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a  tring from the z
96d30 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20  Fromat argument 
96d40 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20  and the va_list 
96d50 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a  that follows..**
96d60 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e   Store the strin
96d70 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  g in memory obta
96d80 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
96d90 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b  Malloc() and mak
96da0 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74  e *pz.** point t
96db0 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a  o that string..*
96dc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
96dd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74   void sqlite3Set
96de0 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a  String(char **pz
96df0 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  , sqlite3 *db, c
96e00 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
96e10 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
96e20 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a  ist ap;.  char *
96e30 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61  z;..  va_start(a
96e40 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a  p, zFormat);.  z
96e50 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
96e60 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  tf(db, zFormat, 
96e70 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
96e80 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
96e90 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a  ee(db, *pz);.  *
96ea0 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  pz = z;.}.../*.*
96eb0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
96ec0 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62  must be called b
96ed0 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e  efore exiting an
96ee0 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28  y API function (
96ef0 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69  i.e. .** returni
96f00 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68  ng control to th
96f10 65 20 75 73 65 72 29 20 74 68 61 74 20 68 61 73  e user) that has
96f20 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f   called sqlite3_
96f30 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c  malloc or.** sql
96f40 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a  ite3_realloc..**
96f50 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
96f60 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c   value is normal
96f70 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ly a copy of the
96f80 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
96f90 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63   to this.** func
96fa0 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69  tion. However, i
96fb0 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
96fc0 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 72 65  lure has occurre
96fd0 64 20 73 69 6e 63 65 20 74 68 65 20 70 72 65 76  d since the prev
96fe0 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  ious.** invocati
96ff0 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  on SQLITE_NOMEM 
97000 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74  is returned inst
97010 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ead. .**.** If t
97020 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
97030 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55  t, db, is not NU
97040 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28  LL and a malloc(
97050 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  ) error has occu
97060 72 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  rred,.** then th
97070 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72  e connection err
97080 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c  or-code (the val
97090 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
970a0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
970b0 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  ).** is set to S
970c0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a  QLITE_NOMEM..*/.
970d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
970e0 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  nt sqlite3ApiExi
970f0 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69  t(sqlite3* db, i
97100 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20  nt rc){.  /* If 
97110 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73  the db handle is
97120 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
97130 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65  we must hold the
97140 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64   connection hand
97150 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65  le.  ** mutex he
97160 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68  re. Otherwise th
97170 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73  e read (and poss
97180 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64  ible write) of d
97190 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
971a0 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c  .  ** is unsafe,
971b0 20 61 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20   as is the call 
971c0 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  to sqlite3Error(
971d0 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  )..  */.  assert
971e0 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33  ( !db || sqlite3
971f0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
97200 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
97210 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f  db && (db->mallo
97220 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53  cFailed || rc==S
97230 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
97240 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  M) ){.    sqlite
97250 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
97260 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20  E_NOMEM, 0);.   
97270 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
97280 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  d = 0;.    rc = 
97290 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
972a0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20  }.  return rc & 
972b0 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73  (db ? db->errMas
972c0 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a  k : 0xff);.}../*
972d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
972e0 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a  d of malloc.c **
972f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
97320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
97330 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e  gin file printf.
97340 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
97350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
97370 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22  .** The "printf"
97380 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f   code that follo
97390 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68  ws dates from th
973a0 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 69 73  e 1980's.  It is
973b0 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69   in.** the publi
973c0 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f  c domain.  The o
973d0 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73  riginal comments
973e0 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65   are included he
973f0 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65  re for.** comple
97400 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72  teness.  They ar
97410 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61  e very out-of-da
97420 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 65 20  te but might be 
97430 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20  useful as.** an 
97440 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72  historical refer
97450 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74  ence.  Most of t
97460 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73  he "enhancements
97470 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b  " have been back
97480 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61  ed.** out so tha
97490 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c  t the functional
974a0 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73  ity is now the s
974b0 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 20  ame as standard 
974c0 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20  printf()..**.** 
974d0 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20  $Id: printf.c,v 
974e0 31 2e 39 39 20 32 30 30 38 2f 31 32 2f 31 30 20  1.99 2008/12/10 
974f0 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78 70  19:26:24 drh Exp
97500 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   $.**.**********
97510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97550 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
97560 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20  wing modules is 
97570 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c  an enhanced repl
97580 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20  acement for the 
97590 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74  "printf" subrout
975a0 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e  ines.** found in
975b0 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20   the standard C 
975c0 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f  library.  The fo
975d0 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d  llowing enhancem
975e0 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70  ents are.** supp
975f0 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  orted:.**.**    
97600 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20    +  Additional 
97610 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20  functions.  The 
97620 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20  standard set of 
97630 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f  "printf" functio
97640 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e  ns.**         in
97650 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66  cludes printf, f
97660 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c  printf, sprintf,
97670 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e   vprintf, vfprin
97680 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  tf, and.**      
97690 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68     vsprintf.  Th
976a0 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74  is module adds t
976b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
976c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20  .**           * 
976d0 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72   snprintf -- Wor
976e0 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c  ks like sprintf,
976f0 20 62 75 74 20 68 61 73 20 61 6e 20 65 78 74 72   but has an extr
97700 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20  a argument.**   
97710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97720 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20         which is 
97730 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
97740 62 75 66 66 65 72 20 77 72 69 74 74 65 6e 20 74  buffer written t
97750 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  o..**.**        
97760 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d     *  mprintf --
97770 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72    Similar to spr
97780 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75  intf.  Writes ou
97790 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a  tput to memory.*
977a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
977b0 20 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69             obtai
977c0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e  ned from malloc.
977d0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
977e0 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20   *  xprintf --  
977f0 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e  Calls a function
97800 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f   to dispose of o
97810 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  utput..**.**    
97820 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74         *  nprint
97830 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c  f --  No output,
97840 20 62 75 74 20 72 65 74 75 72 6e 73 20 74 68 65   but returns the
97850 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   number of chara
97860 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  cters.**        
97870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97880 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76    that would hav
97890 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79  e been output by
978a0 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20   printf..**.**  
978b0 20 20 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d           *  A v-
978c0 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73   version (ex: vs
978d0 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72  nprintf) of ever
978e0 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c  y function is al
978f0 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  so.**           
97900 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a     supplied..**.
97910 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66 65 77  **      +  A few
97920 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74   extensions to t
97930 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f  he formatting no
97940 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f  tation are suppo
97950 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  rted:.**.**     
97960 20 20 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22        *  The "="
97970 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74   flag (similar t
97980 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20 74 68  o "-") causes th
97990 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a  e output to be.*
979a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  *              b
979b0 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68  e centered in th
979c0 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20  e appropriately 
979d0 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a  sized field..**.
979e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
979f0 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74  The %b field out
97a00 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20  puts an integer 
97a10 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69  in binary notati
97a20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  on..**.**       
97a30 20 20 20 20 2a 20 20 54 68 65 20 25 63 20 66 69      *  The %c fi
97a40 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20  eld now accepts 
97a50 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68  a precision.  Th
97a60 65 20 63 68 61 72 61 63 74 65 72 20 6f 75 74 70  e character outp
97a70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
97a80 20 20 20 69 73 20 72 65 70 65 61 74 65 64 20 62     is repeated b
97a90 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  y the number of 
97aa0 74 69 6d 65 73 20 74 68 65 20 70 72 65 63 69 73  times the precis
97ab0 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a  ion specifies..*
97ac0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
97ad0 20 20 54 68 65 20 25 27 20 66 69 65 6c 64 20 77    The %' field w
97ae0 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75  orks like %c, bu
97af0 74 20 74 61 6b 65 73 20 61 73 20 69 74 73 20 63  t takes as its c
97b00 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20  haracter the.** 
97b10 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78               nex
97b20 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74  t character of t
97b30 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67  he format string
97b40 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  , instead of the
97b50 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20   next.**        
97b60 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20        argument. 
97b70 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70   For example,  p
97b80 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20  rintf("%.78'-") 
97b90 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73   prints 78 minus
97ba0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
97bb0 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65   signs, the same
97bc0 20 61 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37   as  printf("%.7
97bd0 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20  8c",'-')..**.** 
97be0 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d       +  When com
97bf0 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20  piled using GCC 
97c00 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68 69 73  on a SPARC, this
97c10 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e   version of prin
97c20 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  tf is.**        
97c30 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65   faster than the
97c40 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74 66 20   library printf 
97c50 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a  for SUN OS 4.1..
97c60 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c  **.**      +  Al
97c70 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  l functions are 
97c80 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e  fully reentrant.
97c90 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .**.*/../*.** Co
97ca0 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66  nversion types f
97cb0 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73  all into various
97cc0 20 63 61 74 65 67 6f 72 69 65 73 20 61 73 20 64   categories as d
97cd0 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a  efined by the.**
97ce0 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65   following enume
97cf0 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  ration..*/.#defi
97d00 6e 65 20 65 74 52 41 44 49 58 20 20 20 20 20 20  ne etRADIX      
97d10 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79   1 /* Integer ty
97d20 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f  pes.  %d, %x, %o
97d30 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a  , and so forth *
97d40 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41  /.#define etFLOA
97d50 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f  T       2 /* Flo
97d60 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66  ating point.  %f
97d70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58   */.#define etEX
97d80 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45  P         3 /* E
97d90 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74  xponentional not
97da0 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45  ation. %e and %E
97db0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45   */.#define etGE
97dc0 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46  NERIC     4 /* F
97dd0 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e  loating or expon
97de0 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e  ential, dependin
97df0 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25  g on exponent. %
97e00 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  g */.#define etS
97e10 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20  IZE        5 /* 
97e20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66  Return number of
97e30 20 63 68 61 72 61 63 74 65 72 73 20 70 72 6f 63   characters proc
97e40 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e  essed so far. %n
97e50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54   */.#define etST
97e60 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53  RING      6 /* S
97e70 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64  trings. %s */.#d
97e80 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e  efine etDYNSTRIN
97e90 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63  G   7 /* Dynamic
97ea0 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ally allocated s
97eb0 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64  trings. %z */.#d
97ec0 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20  efine etPERCENT 
97ed0 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74      8 /* Percent
97ee0 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23   symbol. %% */.#
97ef0 64 65 66 69 6e 65 20 65 74 43 48 41 52 58 20 20  define etCHARX  
97f00 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63       9 /* Charac
97f10 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54  ters. %c */./* T
97f20 68 65 20 72 65 73 74 20 61 72 65 20 65 78 74 65  he rest are exte
97f30 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d  nsions, not norm
97f40 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72  ally found in pr
97f50 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e  intf() */.#defin
97f60 65 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31  e etSQLESCAPE  1
97f70 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74  0 /* Strings wit
97f80 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20  h '\'' doubled. 
97f90 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %q */.#define e
97fa0 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f  tSQLESCAPE2 11 /
97fb0 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27  * Strings with '
97fc0 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20  \'' doubled and 
97fd0 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a  enclosed in '',.
97fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97ff0 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70            NULL p
98000 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64  ointers replaced
98010 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25   by SQL NULL.  %
98020 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54  Q */.#define etT
98030 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20  OKEN      12 /* 
98040 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54  a pointer to a T
98050 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a  oken structure *
98060 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c  /.#define etSRCL
98070 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70  IST    13 /* a p
98080 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c  ointer to a SrcL
98090 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65  ist */.#define e
980a0 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f  tPOINTER    14 /
980b0 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73  * The %p convers
980c0 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65  ion */.#define e
980d0 74 53 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f  tSQLESCAPE3 15 /
980e0 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20  * %w -> Strings 
980f0 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65  with '\"' double
98100 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f  d */.#define etO
98110 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20  RDINAL    16 /* 
98120 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20  %r -> 1st, 2nd, 
98130 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20  3rd, 4th, etc.  
98140 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a  English only */.
98150 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79  ../*.** An "etBy
98160 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20  te" is an 8-bit 
98170 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a  unsigned value..
98180 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67  */.typedef unsig
98190 6e 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b  ned char etByte;
981a0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69  ../*.** Each bui
981b0 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ltin conversion 
981c0 63 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74  character (ex: t
981d0 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20  he 'd' in "%d") 
981e0 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20  is described.** 
981f0 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
98200 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
98210 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70  structure.*/.typ
98220 65 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69  edef struct et_i
98230 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72  nfo {   /* Infor
98240 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63  mation about eac
98250 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a  h format field *
98260 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65  /.  char fmttype
98270 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
98280 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64  The format field
98290 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a   code letter */.
982a0 20 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20    etByte base;  
982b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
982c0 65 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78  e base for radix
982d0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20   conversion */. 
982e0 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20   etByte flags;  
982f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
98300 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47   or more of FLAG
98310 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f  _ constants belo
98320 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79  w */.  etByte ty
98330 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pe;             
98340 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61  /* Conversion pa
98350 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79  radigm */.  etBy
98360 74 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20  te charset;     
98370 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
98380 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66  nto aDigits[] of
98390 20 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69   the digits stri
983a0 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70  ng */.  etByte p
983b0 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  refix;          
983c0 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
983d0 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65  aPrefix[] of the
983e0 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a   prefix string *
983f0 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a  /.} et_info;../*
98400 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
98410 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66  es for et_info.f
98420 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lags.*/.#define 
98430 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20  FLAG_SIGNED  1  
98440 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
98450 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65  e value to conve
98460 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a  rt is signed */.
98470 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54  #define FLAG_INT
98480 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72  ERN  2     /* Tr
98490 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e  ue if for intern
984a0 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23  al use only */.#
984b0 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49  define FLAG_STRI
984c0 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c  NG  4     /* All
984d0 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63  ow infinity prec
984e0 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  ision */.../*.**
984f0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
98500 61 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64  able is searched
98510 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74   linearly, so it
98520 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20   is good to put 
98530 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71  the.** most freq
98540 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76  uently used conv
98550 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72  ersion types fir
98560 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  st..*/.static co
98570 6e 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73  nst char aDigits
98580 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39  [] = "0123456789
98590 41 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39  ABCDEF0123456789
985a0 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20  abcdef";.static 
985b0 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66  const char aPref
985c0 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58  ix[] = "-x0\000X
985d0 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  0";.static const
985e0 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f   et_info fmtinfo
985f0 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c  [] = {.  {  'd',
98600 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c   10, 1, etRADIX,
98610 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
98620 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20   {  's',  0, 4, 
98630 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c  etSTRING,     0,
98640 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c    0 },.  {  'g',
98650 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49    0, 1, etGENERI
98660 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20  C,    30, 0 },. 
98670 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20   {  'z',  0, 4, 
98680 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c  etDYNSTRING,  0,
98690 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c    0 },.  {  'q',
986a0 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43    0, 4, etSQLESC
986b0 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20  APE,  0,  0 },. 
986c0 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20   {  'Q',  0, 4, 
986d0 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c  etSQLESCAPE2, 0,
986e0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c    0 },.  {  'w',
986f0 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43    0, 4, etSQLESC
98700 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20  APE3, 0,  0 },. 
98710 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20   {  'c',  0, 0, 
98720 65 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c  etCHARX,      0,
98730 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c    0 },.  {  'o',
98740 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c    8, 0, etRADIX,
98750 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20        0,  2 },. 
98760 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20   {  'u', 10, 0, 
98770 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
98780 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c    0 },.  {  'x',
98790 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c   16, 0, etRADIX,
987a0 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20        16, 1 },. 
987b0 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20   {  'X', 16, 0, 
987c0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
987d0 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53    4 },.#ifndef S
987e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
987f0 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27  ING_POINT.  {  '
98800 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f  f',  0, 1, etFLO
98810 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  AT,      0,  0 }
98820 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20  ,.  {  'e',  0, 
98830 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20  1, etEXP,       
98840 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27   30, 0 },.  {  '
98850 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50  E',  0, 1, etEXP
98860 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d  ,        14, 0 }
98870 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20  ,.  {  'G',  0, 
98880 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20  1, etGENERIC,   
98890 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66   14, 0 },.#endif
988a0 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31  .  {  'i', 10, 1
988b0 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
988c0 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e  0,  0 },.  {  'n
988d0 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45  ',  0, 0, etSIZE
988e0 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  ,       0,  0 },
988f0 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30  .  {  '%',  0, 0
98900 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20  , etPERCENT,    
98910 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70  0,  0 },.  {  'p
98920 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e  ', 16, 0, etPOIN
98930 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c  TER,    0,  1 },
98940 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32  .  {  'T',  0, 2
98950 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20  , etTOKEN,      
98960 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53  0,  0 },.  {  'S
98970 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c  ',  0, 2, etSRCL
98980 49 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c  IST,    0,  0 },
98990 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33  .  {  'r', 10, 3
989a0 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20  , etORDINAL,    
989b0 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a  0,  0 },.};../*.
989c0 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ** If SQLITE_OMI
989d0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
989e0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
989f0 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c  n none of the fl
98a00 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20  oating point.** 
98a10 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c  conversions will
98a20 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65   work..*/.#ifnde
98a30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
98a40 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a  OATING_POINT./*.
98a50 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64  ** "*val" is a d
98a60 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20  ouble such that 
98a70 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30  0.1 <= *val < 10
98a80 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  .0.** Return the
98a90 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20   ascii code for 
98aa0 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69  the leading digi
98ab0 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a  t of *val, then.
98ac0 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61  ** multiply "*va
98ad0 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65  l" by 10.0 to re
98ae0 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a  normalize..**.**
98af0 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20   Example:.**    
98b00 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c   input:     *val
98b10 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20   = 3.14159.**   
98b20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61    output:    *va
98b30 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75  l = 1.4159    fu
98b40 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20  nction return = 
98b50 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  '3'.**.** The co
98b60 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e  unter *cnt is in
98b70 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74  cremented each t
98b80 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e  ime.  After coun
98b90 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31  ter exceeds.** 1
98ba0 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  6 (the number of
98bb0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   significant dig
98bc0 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20  its in a 64-bit 
98bd0 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a  float) '0' is.**
98be0 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64   always returned
98bf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
98c00 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e   et_getdigit(LON
98c10 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61  GDOUBLE_TYPE *va
98c20 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20  l, int *cnt){.  
98c30 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e  int digit;.  LON
98c40 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a  GDOUBLE_TYPE d;.
98c50 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e    if( (*cnt)++ >
98c60 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30  = 16 ) return '0
98c70 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e  ';.  digit = (in
98c80 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69  t)*val;.  d = di
98c90 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20  git;.  digit += 
98ca0 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a  '0';.  *val = (*
98cb0 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20  val - d)*10.0;. 
98cc0 20 72 65 74 75 72 6e 20 28 63 68 61 72 29 64 69   return (char)di
98cd0 67 69 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  git;.}.#endif /*
98ce0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
98cf0 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a  ATING_POINT */..
98d00 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73  /*.** Append N s
98d10 70 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20  pace characters 
98d20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  to the given str
98d30 69 6e 67 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73  ing buffer..*/.s
98d40 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e  tatic void appen
98d50 64 53 70 61 63 65 28 53 74 72 41 63 63 75 6d 20  dSpace(StrAccum 
98d60 2a 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b  *pAccum, int N){
98d70 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
98d80 63 68 61 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d  char zSpaces[] =
98d90 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
98da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
98db0 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69  ;.  while( N>=(i
98dc0 6e 74 29 73 69 7a 65 6f 66 28 7a 53 70 61 63 65  nt)sizeof(zSpace
98dd0 73 29 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  s)-1 ){.    sqli
98de0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
98df0 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65  d(pAccum, zSpace
98e00 73 2c 20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65  s, sizeof(zSpace
98e10 73 29 2d 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20  s)-1);.    N -= 
98e20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d  sizeof(zSpaces)-
98e30 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30  1;.  }.  if( N>0
98e40 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
98e50 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
98e60 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e  ccum, zSpaces, N
98e70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
98e80 4f 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74 68  On machines with
98e90 20 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73   a small stack s
98ea0 69 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65 64  ize, you can red
98eb0 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c  efine the.** SQL
98ec0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
98ed0 5a 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68  ZE to be less th
98ee0 61 6e 20 33 35 30 2e 20 20 42 75 74 20 62 65 77  an 350.  But bew
98ef0 61 72 65 20 2d 20 66 6f 72 0a 2a 2a 20 73 6d 61  are - for.** sma
98f00 6c 6c 65 72 20 76 61 6c 75 65 73 20 73 6f 6d 65  ller values some
98f10 20 25 66 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20   %f conversions 
98f20 6d 61 79 20 67 6f 20 69 6e 74 6f 20 61 6e 20 69  may go into an i
98f30 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 2e 0a 2a 2f  nfinite loop..*/
98f40 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
98f50 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a 23  PRINT_BUF_SIZE.#
98f60 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50   define SQLITE_P
98f70 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33 35  RINT_BUF_SIZE 35
98f80 30 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  0.#endif.#define
98f90 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 54   etBUFSIZE SQLIT
98fa0 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45  E_PRINT_BUF_SIZE
98fb0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
98fc0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a   output buffer *
98fd0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f  /../*.** The roo
98fe0 74 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20  t program.  All 
98ff0 76 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20  variations call 
99000 74 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a  this core..**.**
99010 20 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75   INPUTS:.**   fu
99020 6e 63 20 20 20 54 68 69 73 20 69 73 20 61 20 70  nc   This is a p
99030 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
99040 74 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72 65  tion taking thre
99050 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20  e arguments.**  
99060 20 20 20 20 20 20 20 20 20 20 31 2e 20 41 20 70            1. A p
99070 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69  ointer to anythi
99080 6e 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68 65  ng.  Same as the
99090 20 22 61 72 67 22 20 70 61 72 61 6d 65 74 65 72   "arg" parameter
990a0 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
990b0 32 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  2. A pointer to 
990c0 74 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61 72  the list of char
990d0 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74  acters to be out
990e0 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  put.**          
990f0 20 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69 73       (Note, this
99100 20 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c   list is NOT nul
99110 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a  l terminated.).*
99120 2a 20 20 20 20 20 20 20 20 20 20 20 20 33 2e 20  *            3. 
99130 41 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65  An integer numbe
99140 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
99150 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a  to be output..**
99160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
99170 4e 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62 65  Note: This numbe
99180 72 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f 2e  r might be zero.
99190 29 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20  ).**.**   arg   
991a0 20 54 68 69 73 20 69 73 20 74 68 65 20 70 6f 69   This is the poi
991b0 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67  nter to anything
991c0 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 70   which will be p
991d0 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20  assed as the.** 
991e0 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 61           first a
991f0 72 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63  rgument to "func
99200 22 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20 77  ".  Use it for w
99210 68 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b 65  hatever you like
99220 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20  ..**.**   fmt   
99230 20 54 68 69 73 20 69 73 20 74 68 65 20 66 6f 72   This is the for
99240 6d 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20 69  mat string, as i
99250 6e 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e  n the usual prin
99260 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20  t..**.**   ap   
99270 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e    This is a poin
99280 74 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f 66  ter to a list of
99290 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d   arguments.  Sam
992a0 65 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20  e as in.**      
992b0 20 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a      vfprint..**.
992c0 2a 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20  ** OUTPUTS:.**  
992d0 20 20 20 20 20 20 20 20 54 68 65 20 72 65 74 75          The retu
992e0 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
992f0 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
99300 63 68 61 72 61 63 74 65 72 73 20 73 65 6e 74 20  characters sent 
99310 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  to.**          t
99320 68 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e  he function "fun
99330 63 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31 20  c".  Returns -1 
99340 6f 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a  on a error..**.*
99350 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
99360 6f 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 61  order in which a
99370 75 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62 6c  utomatic variabl
99380 65 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20  es are declared 
99390 62 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74  below.** seems t
993a0 6f 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69 66  o make a big dif
993b0 66 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65 72  ference in deter
993c0 6d 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74 20  mining how fast 
993d0 74 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77 69  this beast.** wi
993e0 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ll run..*/.SQLIT
993f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
99400 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 0a  qlite3VXPrintf(.
99410 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63    StrAccum *pAcc
99420 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  um,             
99430 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61       /* Accumula
99440 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  te results here 
99450 2a 2f 0a 20 20 69 6e 74 20 75 73 65 45 78 74 65  */.  int useExte
99460 6e 64 65 64 2c 20 20 20 20 20 20 20 20 20 20 20  nded,           
99470 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77          /* Allow
99480 20 65 78 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76   extended %-conv
99490 65 72 73 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e  ersions */.  con
994a0 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20  st char *fmt,   
994b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
994c0 2f 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67  /* Format string
994d0 20 2a 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70   */.  va_list ap
994e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
994f0 20 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75           /* argu
99500 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ments */.){.  in
99510 74 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  t c;            
99520 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
99530 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68   character in th
99540 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  e format string 
99550 2a 2f 0a 20 20 63 68 61 72 20 2a 62 75 66 70 74  */.  char *bufpt
99560 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
99570 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
99580 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66  e conversion buf
99590 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65  fer */.  int pre
995a0 63 69 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20  cision;         
995b0 20 20 20 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e      /* Precision
995c0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
995d0 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c  field */.  int l
995e0 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20  ength;          
995f0 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
99600 6f 66 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a  of the field */.
99610 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20    int idx;      
99620 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
99630 41 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  A general purpos
99640 65 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  e loop counter *
99650 2f 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20  /.  int width;  
99660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
99670 2a 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63  * Width of the c
99680 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a  urrent field */.
99690 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65    etByte flag_le
996a0 66 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20  ftjustify;   /* 
996b0 54 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67  True if "-" flag
996c0 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
996d0 20 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75   etByte flag_plu
996e0 73 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54  ssign;      /* T
996f0 72 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20  rue if "+" flag 
99700 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
99710 65 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e  etByte flag_blan
99720 6b 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72  ksign;     /* Tr
99730 75 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69  ue if " " flag i
99740 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65  s present */.  e
99750 74 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72  tByte flag_alter
99760 6e 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75  nateform; /* Tru
99770 65 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73  e if "#" flag is
99780 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74   present */.  et
99790 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72  Byte flag_altfor
997a0 6d 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  m2;      /* True
997b0 20 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20   if "!" flag is 
997c0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
997d0 79 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  yte flag_zeropad
997e0 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
997f0 69 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63  if field width c
99800 6f 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77  onstant starts w
99810 69 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74  ith zero */.  et
99820 42 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20  Byte flag_long; 
99830 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
99840 20 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20   if "l" flag is 
99850 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
99860 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e  yte flag_longlon
99870 67 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  g;      /* True 
99880 69 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67  if the "ll" flag
99890 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
998a0 20 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20   etByte done;   
998b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
998c0 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20  oop termination 
998d0 66 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65  flag */.  sqlite
998e0 5f 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75  _uint64 longvalu
998f0 65 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f  e;   /* Value fo
99900 72 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20  r integer types 
99910 2a 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f  */.  LONGDOUBLE_
99920 54 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20  TYPE realvalue; 
99930 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61  /* Value for rea
99940 6c 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e  l types */.  con
99950 73 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f  st et_info *info
99960 70 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  p;      /* Point
99970 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  er to the approp
99980 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63  riate info struc
99990 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62  ture */.  char b
999a0 75 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20  uf[etBUFSIZE];  
999b0 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69       /* Conversi
999c0 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63  on buffer */.  c
999d0 68 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20  har prefix;     
999e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
999f0 66 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20  fix character.  
99a00 22 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20  "+" or "-" or " 
99a10 22 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20  " or '\0'. */.  
99a20 65 74 42 79 74 65 20 78 74 79 70 65 20 3d 20 30  etByte xtype = 0
99a30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ;          /* Co
99a40 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67  nversion paradig
99a50 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78  m */.  char *zEx
99a60 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  tra;            
99a70 20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72    /* Extra memor
99a80 79 20 75 73 65 64 20 66 6f 72 20 65 74 54 43 4c  y used for etTCL
99a90 45 53 43 41 50 45 20 63 6f 6e 76 65 72 73 69 6f  ESCAPE conversio
99aa0 6e 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ns */.#ifndef SQ
99ab0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
99ac0 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20  NG_POINT.  int  
99ad0 65 78 70 2c 20 65 32 3b 20 20 20 20 20 20 20 20  exp, e2;        
99ae0 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e        /* exponen
99af0 74 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62 65 72  t of real number
99b00 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f  s */.  double ro
99b10 75 6e 64 65 72 3b 20 20 20 20 20 20 20 20 20 20  under;          
99b20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 72 6f    /* Used for ro
99b30 75 6e 64 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20  unding floating 
99b40 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a  point values */.
99b50 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70    etByte flag_dp
99b60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
99b70 54 72 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20  True if decimal 
99b80 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20  point should be 
99b90 73 68 6f 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74  shown */.  etByt
99ba0 65 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20  e flag_rtz;     
99bb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
99bc0 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20   trailing zeros 
99bd0 73 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65  should be remove
99be0 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  d */.  etByte fl
99bf0 61 67 5f 65 78 70 3b 20 20 20 20 20 20 20 20 20  ag_exp;         
99c00 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 6f 72    /* True to for
99c10 63 65 20 64 69 73 70 6c 61 79 20 6f 66 20 74 68  ce display of th
99c20 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20  e exponent */.  
99c30 69 6e 74 20 6e 73 64 3b 20 20 20 20 20 20 20 20  int nsd;        
99c40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
99c50 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63  mber of signific
99c60 61 6e 74 20 64 69 67 69 74 73 20 72 65 74 75 72  ant digits retur
99c70 6e 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20  ned */.#endif.. 
99c80 20 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62   length = 0;.  b
99c90 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  ufpt = 0;.  for(
99ca0 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b  ; (c=(*fmt))!=0;
99cb0 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 66 28   ++fmt){.    if(
99cc0 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20   c!='%' ){.     
99cd0 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 20 20   int amt;.      
99ce0 62 75 66 70 74 20 3d 20 28 63 68 61 72 20 2a 29  bufpt = (char *)
99cf0 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d  fmt;.      amt =
99d00 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   1;.      while(
99d10 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27   (c=(*++fmt))!='
99d20 25 27 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74  %' && c!=0 ) amt
99d30 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ++;.      sqlite
99d40 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
99d50 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61  pAccum, bufpt, a
99d60 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63  mt);.      if( c
99d70 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
99d80 20 7d 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a   }.    if( (c=(*
99d90 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20  ++fmt))==0 ){.  
99da0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
99db0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
99dc0 2c 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 20 20  , "%", 1);.     
99dd0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
99de0 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68    /* Find out wh
99df0 61 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65  at flags are pre
99e00 73 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67  sent */.    flag
99e10 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66  _leftjustify = f
99e20 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66  lag_plussign = f
99e30 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20  lag_blanksign = 
99e40 0a 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72  .     flag_alter
99e50 6e 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f  nateform = flag_
99e60 61 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f  altform2 = flag_
99e70 7a 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20  zeropad = 0;.   
99e80 20 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64   done = 0;.    d
99e90 6f 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  o{.      switch(
99ea0 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61   c ){.        ca
99eb0 73 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c  se '-':   flag_l
99ec0 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20  eftjustify = 1; 
99ed0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
99ee0 20 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66     case '+':   f
99ef0 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31  lag_plussign = 1
99f00 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
99f10 20 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27          case ' '
99f20 3a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69  :   flag_blanksi
99f30 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72  gn = 1;       br
99f40 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
99f50 65 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c  e '#':   flag_al
99f60 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b  ternateform = 1;
99f70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
99f80 20 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c    case '!':   fl
99f90 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b  ag_altform2 = 1;
99fa0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
99fb0 20 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a         case '0':
99fc0 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20     flag_zeropad 
99fd0 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65  = 1;         bre
99fe0 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61  ak;.        defa
99ff0 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31  ult:    done = 1
9a000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
9a010 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
9a020 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f  .    }while( !do
9a030 6e 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74  ne && (c=(*++fmt
9a040 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20  ))!=0 );.    /* 
9a050 47 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69  Get the field wi
9a060 64 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68  dth */.    width
9a070 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d   = 0;.    if( c=
9a080 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69  ='*' ){.      wi
9a090 64 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  dth = va_arg(ap,
9a0a0 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  int);.      if( 
9a0b0 77 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20  width<0 ){.     
9a0c0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
9a0d0 69 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ify = 1;.       
9a0e0 20 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b   width = -width;
9a0f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
9a100 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d   = *++fmt;.    }
9a110 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c  else{.      whil
9a120 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d  e( c>='0' && c<=
9a130 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77  '9' ){.        w
9a140 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20  idth = width*10 
9a150 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  + c - '0';.     
9a160 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
9a170 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
9a180 20 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42   if( width > etB
9a190 55 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20  UFSIZE-10 ){.   
9a1a0 20 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46     width = etBUF
9a1b0 53 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20  SIZE-10;.    }. 
9a1c0 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72     /* Get the pr
9a1d0 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69  ecision */.    i
9a1e0 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  f( c=='.' ){.   
9a1f0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30     precision = 0
9a200 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66  ;.      c = *++f
9a210 6d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d  mt;.      if( c=
9a220 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
9a230 70 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61  precision = va_a
9a240 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
9a250 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
9a260 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20  n<0 ) precision 
9a270 3d 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20  = -precision;.  
9a280 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
9a290 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
9a2a0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e         while( c>
9a2b0 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29  ='0' && c<='9' )
9a2c0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63  {.          prec
9a2d0 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f  ision = precisio
9a2e0 6e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a  n*10 + c - '0';.
9a2f0 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b            c = *+
9a300 2b 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  +fmt;.        }.
9a310 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
9a320 65 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69  e{.      precisi
9a330 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  on = -1;.    }. 
9a340 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f     /* Get the co
9a350 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f  nversion type mo
9a360 64 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66  difier */.    if
9a370 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20  ( c=='l' ){.    
9a380 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b    flag_long = 1;
9a390 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d  .      c = *++fm
9a3a0 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  t;.      if( c==
9a3b0 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66  'l' ){.        f
9a3c0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31  lag_longlong = 1
9a3d0 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b  ;.        c = *+
9a3e0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73  +fmt;.      }els
9a3f0 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  e{.        flag_
9a400 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20  longlong = 0;.  
9a410 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
9a420 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67  .      flag_long
9a430 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67   = flag_longlong
9a440 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
9a450 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66  /* Fetch the inf
9a460 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  o entry for the 
9a470 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66  field */.    inf
9a480 6f 70 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  op = 0;.    for(
9a490 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 79  idx=0; idx<Array
9a4a0 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 69  Size(fmtinfo); i
9a4b0 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  dx++){.      if(
9a4c0 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d   c==fmtinfo[idx]
9a4d0 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 20  .fmttype ){.    
9a4e0 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74      infop = &fmt
9a4f0 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20  info[idx];.     
9a500 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e 64     if( useExtend
9a510 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c  ed || (infop->fl
9a520 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 52  ags & FLAG_INTER
9a530 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  N)==0 ){.       
9a540 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f 70     xtype = infop
9a550 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20 20  ->type;.        
9a560 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
9a570 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
9a580 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
9a590 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
9a5a0 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a      zExtra = 0;.
9a5b0 20 20 20 20 69 66 28 20 69 6e 66 6f 70 3d 3d 30      if( infop==0
9a5c0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
9a5d0 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a  ;.    }...    /*
9a5e0 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69   Limit the preci
9a5f0 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20  sion to prevent 
9a600 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b  overflowing buf[
9a610 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73  ] during convers
9a620 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ion */.    if( p
9a630 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49  recision>etBUFSI
9a640 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d  ZE-40 && (infop-
9a650 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54  >flags & FLAG_ST
9a660 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20  RING)==0 ){.    
9a670 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74    precision = et
9a680 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20  BUFSIZE-40;.    
9a690 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  }..    /*.    **
9a6a0 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
9a6b0 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e  variables are in
9a6c0 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c  itialized as fol
9a6d0 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  lows:.    **.   
9a6e0 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72   **   flag_alter
9a6f0 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20  nateform        
9a700 20 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20    TRUE if a '#' 
9a710 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20  is present..    
9a720 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72  **   flag_altfor
9a730 6d 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m2              
9a740 20 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69   TRUE if a '!' i
9a750 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a  s present..    *
9a760 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  *   flag_plussig
9a770 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
9a780 54 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73  TRUE if a '+' is
9a790 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a   present..    **
9a7a0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
9a7b0 69 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54  ify            T
9a7c0 52 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20  RUE if a '-' is 
9a7d0 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68  present or if th
9a7e0 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  e.    **        
9a7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a800 20 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64         field wid
9a810 74 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e  th was negative.
9a820 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a  .    **   flag_z
9a830 65 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20  eropad          
9a840 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68        TRUE if th
9a850 65 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69  e width began wi
9a860 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66  th 0..    **   f
9a870 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20  lag_long        
9a880 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20             TRUE 
9a890 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c  if the letter 'l
9a8a0 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64  ' (ell) prefixed
9a8b0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
9a8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a8d0 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72        the conver
9a8e0 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a  sion character..
9a8f0 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f      **   flag_lo
9a900 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20  nglong          
9a910 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65       TRUE if the
9a920 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c   letter 'll' (el
9a930 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a  l ell) prefixed.
9a940 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
9a950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a960 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73       the convers
9a970 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20  ion character.. 
9a980 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61     **   flag_bla
9a990 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20  nksign          
9a9a0 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20      TRUE if a ' 
9a9b0 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  ' is present..  
9a9c0 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20    **   width    
9a9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a9e0 20 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64     The specified
9a9f0 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54   field width.  T
9aa00 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20  his is.    **   
9aa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9aa20 20 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61              alwa
9aa30 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  ys non-negative.
9aa40 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65    Zero is the de
9aa50 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20  fault..    **   
9aa60 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20  precision       
9aa70 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
9aa80 73 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73  specified precis
9aa90 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c  ion.  The defaul
9aaa0 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  t.    **        
9aab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9aac0 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20         is -1..  
9aad0 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20    **   xtype    
9aae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9aaf0 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20     The class of 
9ab00 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a  the conversion..
9ab10 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20      **   infop  
9ab20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ab30 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20       Pointer to 
9ab40 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
9ab50 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20  info struct..   
9ab60 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20   */.    switch( 
9ab70 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63  xtype ){.      c
9ab80 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20  ase etPOINTER:. 
9ab90 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67         flag_long
9aba0 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68  long = sizeof(ch
9abb0 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34  ar*)==sizeof(i64
9abc0 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  );.        flag_
9abd0 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68  long = sizeof(ch
9abe0 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e  ar*)==sizeof(lon
9abf0 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  g int);.        
9ac00 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
9ac10 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61  into the next ca
9ac20 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  se */.      case
9ac30 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20   etORDINAL:.    
9ac40 20 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a    case etRADIX:.
9ac50 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f          if( info
9ac60 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f  p->flags & FLAG_
9ac70 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20  SIGNED ){.      
9ac80 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20      i64 v;.     
9ac90 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f       if( flag_lo
9aca0 6e 67 6c 6f 6e 67 20 29 20 20 20 76 20 3d 20 76  nglong )   v = v
9acb0 61 5f 61 72 67 28 61 70 2c 69 36 34 29 3b 0a 20  a_arg(ap,i64);. 
9acc0 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66           else if
9acd0 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 20 20 76  ( flag_long )  v
9ace0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e   = va_arg(ap,lon
9acf0 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  g int);.        
9ad00 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20    else          
9ad10 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61          v = va_a
9ad20 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
9ad30 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b        if( v<0 ){
9ad40 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e  .            lon
9ad50 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20  gvalue = -v;.   
9ad60 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
9ad70 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20  = '-';.         
9ad80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
9ad90 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20      longvalue = 
9ada0 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  v;.            i
9adb0 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e  f( flag_plussign
9adc0 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78   )        prefix
9add0 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20   = '+';.        
9ade0 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61      else if( fla
9adf0 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70  g_blanksign )  p
9ae00 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20  refix = ' ';.   
9ae10 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20           else   
9ae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ae30 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a      prefix = 0;.
9ae40 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
9ae50 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9ae60 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f       if( flag_lo
9ae70 6e 67 6c 6f 6e 67 20 29 20 20 20 6c 6f 6e 67 76  nglong )   longv
9ae80 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70  alue = va_arg(ap
9ae90 2c 75 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20  ,u64);.         
9aea0 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c   else if( flag_l
9aeb0 6f 6e 67 20 29 20 20 6c 6f 6e 67 76 61 6c 75 65  ong )  longvalue
9aec0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73   = va_arg(ap,uns
9aed0 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b  igned long int);
9aee0 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20  .          else 
9aef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9af00 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f   longvalue = va_
9af10 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20  arg(ap,unsigned 
9af20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  int);.          
9af30 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20  prefix = 0;.    
9af40 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
9af50 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29  ( longvalue==0 )
9af60 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66   flag_alternatef
9af70 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  orm = 0;.       
9af80 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61   if( flag_zeropa
9af90 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c 77  d && precision<w
9afa0 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29  idth-(prefix!=0)
9afb0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72   ){.          pr
9afc0 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d  ecision = width-
9afd0 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20 20  (prefix!=0);.   
9afe0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
9aff0 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 42 55  ufpt = &buf[etBU
9b000 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 20  FSIZE-1];.      
9b010 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 4f    if( xtype==etO
9b020 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20  RDINAL ){.      
9b030 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
9b040 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22   char zOrd[] = "
9b050 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 20  thstndrd";.     
9b060 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 6e       int x = (in
9b070 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31  t)(longvalue % 1
9b080 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0);.          if
9b090 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76  ( x>=4 || (longv
9b0a0 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29  alue/10)%10==1 )
9b0b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20  {.            x 
9b0c0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 0;.          }
9b0d0 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b 65  .          buf[e
9b0e0 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 7a 4f  tBUFSIZE-3] = zO
9b0f0 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 20 20  rd[x*2];.       
9b100 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45     buf[etBUFSIZE
9b110 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b 31  -2] = zOrd[x*2+1
9b120 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66  ];.          buf
9b130 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20  pt -= 2;.       
9b140 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20   }.        {.   
9b150 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20         register 
9b160 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65 74  const char *cset
9b170 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72 65  ;      /* Use re
9b180 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65 65  gisters for spee
9b190 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72  d */.          r
9b1a0 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73 65  egister int base
9b1b0 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65 74  ;.          cset
9b1c0 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66 6f   = &aDigits[info
9b1d0 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20  p->charset];.   
9b1e0 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69 6e         base = in
9b1f0 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20  fop->base;.     
9b200 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20 20       do{        
9b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b230 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f     /* Convert to
9b240 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20 20   ascii */.      
9b250 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29        *(--bufpt)
9b260 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75   = cset[longvalu
9b270 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20 20  e%base];.       
9b280 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
9b290 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b   longvalue/base;
9b2a0 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c  .          }whil
9b2b0 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29  e( longvalue>0 )
9b2c0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
9b2d0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
9b2e0 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a  t)(&buf[etBUFSIZ
9b2f0 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20  E-1]-bufpt);.   
9b300 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65       for(idx=pre
9b310 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69  cision-length; i
9b320 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20  dx>0; idx--){.  
9b330 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70          *(--bufp
9b340 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20  t) = '0';       
9b350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b360 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61        /* Zero pa
9b370 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  d */.        }. 
9b380 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69         if( prefi
9b390 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d  x ) *(--bufpt) =
9b3a0 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20   prefix;        
9b3b0 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69         /* Add si
9b3c0 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  gn */.        if
9b3d0 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65  ( flag_alternate
9b3e0 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70  form && infop->p
9b3f0 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a  refix ){      /*
9b400 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 22   Add "0" or "0x"
9b410 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f   */.          co
9b420 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20  nst char *pre;. 
9b430 20 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b           char x;
9b440 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d  .          pre =
9b450 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d   &aPrefix[infop-
9b460 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20  >prefix];.      
9b470 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70      for(; (x=(*p
9b480 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20  re))!=0; pre++) 
9b490 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a  *(--bufpt) = x;.
9b4a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9b4b0 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29    length = (int)
9b4c0 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d  (&buf[etBUFSIZE-
9b4d0 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 20 20  1]-bufpt);.     
9b4e0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
9b4f0 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20 20  case etFLOAT:.  
9b500 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a 0a      case etEXP:.
9b510 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45 4e        case etGEN
9b520 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72 65  ERIC:.        re
9b530 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67  alvalue = va_arg
9b540 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69 66  (ap,double);.#if
9b550 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
9b560 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
9b570 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63          if( prec
9b580 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73  ision<0 ) precis
9b590 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20  ion = 6;        
9b5a0 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20   /* Set default 
9b5b0 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20  precision */.   
9b5c0 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69       if( precisi
9b5d0 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d 31  on>etBUFSIZE/2-1
9b5e0 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20  0 ) precision = 
9b5f0 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b 0a  etBUFSIZE/2-10;.
9b600 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c          if( real
9b610 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20 20  value<0.0 ){.   
9b620 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
9b630 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a 20   = -realvalue;. 
9b640 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
9b650 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 7d  = '-';.        }
9b660 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
9b670 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  if( flag_plussig
9b680 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72 65  n )          pre
9b690 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20  fix = '+';.     
9b6a0 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c       else if( fl
9b6b0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20  ag_blanksign )  
9b6c0 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a    prefix = ' ';.
9b6d0 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20            else  
9b6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b6f0 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20         prefix = 
9b700 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
9b710 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d       if( xtype==
9b720 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72 65  etGENERIC && pre
9b730 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63 69  cision>0 ) preci
9b740 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20 20  sion--;.#if 0.  
9b750 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69 6e        /* Roundin
9b760 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53 44  g works like BSD
9b770 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 61   when the consta
9b780 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73 65  nt 0.4999 is use
9b790 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20 20  d.  Wierd! */.  
9b7a0 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72        for(idx=pr
9b7b0 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72  ecision, rounder
9b7c0 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b 20  =0.4999; idx>0; 
9b7d0 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d  idx--, rounder*=
9b7e0 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  0.1);.#else.    
9b7f0 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73 20      /* It makes 
9b800 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75 73  more sense to us
9b810 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20 20  e 0.5 */.       
9b820 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69   for(idx=precisi
9b830 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35 3b  on, rounder=0.5;
9b840 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72   idx>0; idx--, r
9b850 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a 23  ounder*=0.1){}.#
9b860 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66  endif.        if
9b870 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 54  ( xtype==etFLOAT
9b880 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20   ) realvalue += 
9b890 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20  rounder;.       
9b8a0 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 65   /* Normalize re
9b8b0 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 69  alvalue to withi
9b8c0 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 6c  n 10.0 > realval
9b8d0 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20 20  ue >= 1.0 */.   
9b8e0 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20 20       exp = 0;.  
9b8f0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
9b900 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 29 72  3IsNaN((double)r
9b910 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20  ealvalue) ){.   
9b920 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22         bufpt = "
9b930 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20  NaN";.          
9b940 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20  length = 3;.    
9b950 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
9b960 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
9b970 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30  f( realvalue>0.0
9b980 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68   ){.          wh
9b990 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d  ile( realvalue>=
9b9a0 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30  1e32 && exp<=350
9b9b0 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
9b9c0 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b   1e-32; exp+=32;
9b9d0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
9b9e0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31  le( realvalue>=1
9b9f0 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29  e8 && exp<=350 )
9ba00 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31  { realvalue *= 1
9ba10 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20  e-8; exp+=8; }. 
9ba20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
9ba30 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20  realvalue>=10.0 
9ba40 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72  && exp<=350 ){ r
9ba50 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b  ealvalue *= 0.1;
9ba60 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20   exp++; }.      
9ba70 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76      while( realv
9ba80 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61  alue<1e-8 ){ rea
9ba90 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65  lvalue *= 1e8; e
9baa0 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20  xp-=8; }.       
9bab0 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
9bac0 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76  lue<1.0 ){ realv
9bad0 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78  alue *= 10.0; ex
9bae0 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  p--; }.         
9baf0 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a   if( exp>350 ){.
9bb00 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
9bb10 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20  prefix=='-' ){. 
9bb20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66               buf
9bb30 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20  pt = "-Inf";.   
9bb40 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
9bb50 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29  f( prefix=='+' )
9bb60 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
9bb70 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a  bufpt = "+Inf";.
9bb80 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
9bb90 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
9bba0 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a   bufpt = "Inf";.
9bbb0 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
9bbc0 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
9bbd0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
9bbe0 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20  30(bufpt);.     
9bbf0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
9bc00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9bc10 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70    }.        bufp
9bc20 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20  t = buf;.       
9bc30 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49   /*.        ** I
9bc40 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70 65  f the field type
9bc50 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20 74   is etGENERIC, t
9bc60 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20 65  hen convert to e
9bc70 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20 20  ither etEXP.    
9bc80 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f 41      ** or etFLOA
9bc90 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  T, as appropriat
9bca0 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
9bcb0 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20 3d        flag_exp =
9bcc0 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a 20   xtype==etEXP;. 
9bcd0 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65         if( xtype
9bce0 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20 20  !=etFLOAT ){.   
9bcf0 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
9bd00 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20   += rounder;.   
9bd10 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76         if( realv
9bd20 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 65  alue>=10.0 ){ re
9bd30 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20  alvalue *= 0.1; 
9bd40 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  exp++; }.       
9bd50 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78   }.        if( x
9bd60 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20  type==etGENERIC 
9bd70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  ){.          fla
9bd80 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61 6c  g_rtz = !flag_al
9bd90 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20 20  ternateform;.   
9bda0 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c 2d         if( exp<-
9bdb0 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73 69  4 || exp>precisi
9bdc0 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  on ){.          
9bdd0 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50 3b    xtype = etEXP;
9bde0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
9bdf0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72  {.            pr
9be00 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73  ecision = precis
9be10 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20 20  ion - exp;.     
9be20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65         xtype = e
9be30 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20 20  tFLOAT;.        
9be40 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
9be50 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  e{.          fla
9be60 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20 20  g_rtz = 0;.     
9be70 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
9be80 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b   xtype==etEXP ){
9be90 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d 20  .          e2 = 
9bea0 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
9beb0 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d  {.          e2 =
9bec0 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a   exp;.        }.
9bed0 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 3b          nsd = 0;
9bee0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 70  .        flag_dp
9bef0 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 20   = (precision>0 
9bf00 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c 74  ?1:0) | flag_alt
9bf10 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c 61  ernateform | fla
9bf20 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 20  g_altform2;.    
9bf30 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e 20      /* The sign 
9bf40 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 20  in front of the 
9bf50 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20  number */.      
9bf60 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b 0a    if( prefix ){.
9bf70 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
9bf80 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a 20  t++) = prefix;. 
9bf90 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9bfa0 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f 72   /* Digits prior
9bfb0 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c 20   to the decimal 
9bfc0 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20  point */.       
9bfd0 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20 20   if( e2<0 ){.   
9bfe0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
9bff0 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  ) = '0';.       
9c000 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
9c010 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20 65    for(; e2>=0; e
9c020 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  2--){.          
9c030 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65    *(bufpt++) = e
9c040 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c  t_getdigit(&real
9c050 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20  value,&nsd);.   
9c060 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9c070 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68   }.        /* Th
9c080 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
9c090 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  */.        if( f
9c0a0 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20  lag_dp ){.      
9c0b0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
9c0c0 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a   '.';.        }.
9c0d0 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20 64          /* "0" d
9c0e0 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20  igits after the 
9c0f0 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62 75  decimal point bu
9c100 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72  t before the fir
9c110 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69  st.        ** si
9c120 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 20  gnificant digit 
9c130 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f  of the number */
9c140 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32 2b  .        for(e2+
9c150 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73 69  +; e2<0; precisi
9c160 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 20  on--, e2++){.   
9c170 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
9c180 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20 20  recision>0 );.  
9c190 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
9c1a0 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20  +) = '0';.      
9c1b0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53    }.        /* S
9c1c0 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74  ignificant digit
9c1d0 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69  s after the deci
9c1e0 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20  mal point */.   
9c1f0 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 65       while( (pre
9c200 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a 20  cision--)>0 ){. 
9c210 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
9c220 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69  ++) = et_getdigi
9c230 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73  t(&realvalue,&ns
9c240 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  d);.        }.  
9c250 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20        /* Remove 
9c260 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 61  trailing zeros a
9c270 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e 6f  nd the "." if no
9c280 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 74   digits follow t
9c290 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 20  he "." */.      
9c2a0 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 26    if( flag_rtz &
9c2b0 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20  & flag_dp ){.   
9c2c0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 75         while( bu
9c2d0 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 2a  fpt[-1]=='0' ) *
9c2e0 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a 20  (--bufpt) = 0;. 
9c2f0 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
9c300 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 20   bufpt>buf );.  
9c310 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70          if( bufp
9c320 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20  t[-1]=='.' ){.  
9c330 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c            if( fl
9c340 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a 20  ag_altform2 ){. 
9c350 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62               *(b
9c360 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20  ufpt++) = '0';. 
9c370 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
9c380 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
9c390 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a  *(--bufpt) = 0;.
9c3a0 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
9c3b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9c3c0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41    }.        /* A
9c3d0 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 75  dd the "eNNN" su
9c3e0 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 20  ffix */.        
9c3f0 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c 20  if( flag_exp || 
9c400 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b 0a  xtype==etEXP ){.
9c410 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
9c420 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69  t++) = aDigits[i
9c430 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a  nfop->charset];.
9c440 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78            if( ex
9c450 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  p<0 ){.         
9c460 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
9c470 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b  '-'; exp = -exp;
9c480 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
9c490 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28  {.            *(
9c4a0 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b 0a  bufpt++) = '+';.
9c4b0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
9c4c0 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d 31        if( exp>=1
9c4d0 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  00 ){.          
9c4e0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28    *(bufpt++) = (
9c4f0 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 2b  char)((exp/100)+
9c500 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a 20  '0');        /* 
9c510 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20  100's digit */. 
9c520 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 25             exp %
9c530 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 20  = 100;.         
9c540 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62   }.          *(b
9c550 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29  ufpt++) = (char)
9c560 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 20  (exp/10+'0');   
9c570 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 27            /* 10'
9c580 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20  s digit */.     
9c590 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
9c5a0 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 2b  = (char)(exp%10+
9c5b0 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20  '0');           
9c5c0 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 2a    /* 1's digit *
9c5d0 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  /.        }.    
9c5e0 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b 0a      *bufpt = 0;.
9c5f0 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
9c600 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 72  converted number
9c610 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64   is in buf[] and
9c620 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64   zero terminated
9c630 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 20  . Output it..   
9c640 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61       ** Note tha
9c650 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20  t the number is 
9c660 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64  in the usual ord
9c670 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64  er, not reversed
9c680 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 20   as with.       
9c690 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76   ** integer conv
9c6a0 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20  ersions. */.    
9c6b0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
9c6c0 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b 0a 20  t)(bufpt-buf);. 
9c6d0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
9c6e0 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  uf;..        /* 
9c6f0 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 41  Special case:  A
9c700 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73  dd leading zeros
9c710 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65 72   if the flag_zer
9c720 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20 20  opad flag is.   
9c730 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64 20       ** set and 
9c740 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74 20  we are not left 
9c750 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20 20  justified */.   
9c760 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65       if( flag_ze
9c770 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f 6c  ropad && !flag_l
9c780 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c 65  eftjustify && le
9c790 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a 20  ngth < width){. 
9c7a0 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a           int i;.
9c7b0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 50            int nP
9c7c0 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65 6e  ad = width - len
9c7d0 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20 66  gth;.          f
9c7e0 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d 6e  or(i=width; i>=n
9c7f0 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  Pad; i--){.     
9c800 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d 20         bufpt[i] 
9c810 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d 3b  = bufpt[i-nPad];
9c820 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
9c830 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66 69         i = prefi
9c840 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 20  x!=0;.          
9c850 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29 20  while( nPad-- ) 
9c860 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30 27  bufpt[i++] = '0'
9c870 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67  ;.          leng
9c880 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20 20  th = width;.    
9c890 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
9c8a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
9c8b0 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a 20    case etSIZE:. 
9c8c0 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67 28         *(va_arg(
9c8d0 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63 63  ap,int*)) = pAcc
9c8e0 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20  um->nChar;.     
9c8f0 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74     length = widt
9c900 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  h = 0;.        b
9c910 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
9c920 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 20 20   etPERCENT:.    
9c930 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 25 27      buf[0] = '%'
9c940 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20  ;.        bufpt 
9c950 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 6c  = buf;.        l
9c960 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 20 20  ength = 1;.     
9c970 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
9c980 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20 20  case etCHARX:.  
9c990 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 72 67        c = va_arg
9c9a0 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20  (ap,int);.      
9c9b0 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 61 72    buf[0] = (char
9c9c0 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  )c;.        if( 
9c9d0 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a  precision>=0 ){.
9c9e0 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 64            for(id
9c9f0 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 73 69  x=1; idx<precisi
9ca00 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 5b 69  on; idx++) buf[i
9ca10 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20  dx] = (char)c;. 
9ca20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
9ca30 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20  = precision;.   
9ca40 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
9ca50 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b        length =1;
9ca60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
9ca70 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
9ca80 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
9ca90 20 20 20 20 20 63 61 73 65 20 65 74 53 54 52 49       case etSTRI
9caa0 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  NG:.      case e
9cab0 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20  tDYNSTRING:.    
9cac0 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f 61      bufpt = va_a
9cad0 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20  rg(ap,char*);.  
9cae0 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d        if( bufpt=
9caf0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
9cb00 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20 20  bufpt = "";.    
9cb10 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78 74      }else if( xt
9cb20 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47  ype==etDYNSTRING
9cb30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45   ){.          zE
9cb40 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20 20  xtra = bufpt;.  
9cb50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
9cb60 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30  if( precision>=0
9cb70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
9cb80 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 6e 67  r(length=0; leng
9cb90 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 26 20  th<precision && 
9cba0 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 6c  bufpt[length]; l
9cbb0 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 20  ength++){}.     
9cbc0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
9cbd0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c      length = sql
9cbe0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66  ite3Strlen30(buf
9cbf0 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pt);.        }. 
9cc00 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
9cc10 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53      case etSQLES
9cc20 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73 65  CAPE:.      case
9cc30 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a 20   etSQLESCAPE2:. 
9cc40 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45       case etSQLE
9cc50 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20 20  SCAPE3: {.      
9cc60 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 69    int i, j, n, i
9cc70 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69  snull;.        i
9cc80 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20  nt needQuote;.  
9cc90 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a 20        char ch;. 
9cca0 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20         char q = 
9ccb0 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53  ((xtype==etSQLES
9ccc0 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29  CAPE3)?'"':'\'')
9ccd0 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61  ;   /* Quote cha
9cce0 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  racter */.      
9ccf0 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d    char *escarg =
9cd00 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a   va_arg(ap,char*
9cd10 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c  );.        isnul
9cd20 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20  l = escarg==0;. 
9cd30 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c         if( isnul
9cd40 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74  l ) escarg = (xt
9cd50 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45  ype==etSQLESCAPE
9cd60 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e  2 ? "NULL" : "(N
9cd70 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20  ULL)");.        
9cd80 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65  for(i=n=0; (ch=e
9cd90 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b  scarg[i])!=0; i+
9cda0 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
9cdb0 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a  ( ch==q )  n++;.
9cdc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9cdd0 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69    needQuote = !i
9cde0 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d  snull && xtype==
9cdf0 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20  etSQLESCAPE2;.  
9ce00 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31        n += i + 1
9ce10 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a   + needQuote*2;.
9ce20 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74          if( n>et
9ce30 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20  BUFSIZE ){.     
9ce40 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78       bufpt = zEx
9ce50 74 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  tra = sqlite3Mal
9ce60 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20  loc( n );.      
9ce70 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30      if( bufpt==0
9ce80 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
9ce90 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61  pAccum->mallocFa
9cea0 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
9ceb0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
9cec0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9ced0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
9cee0 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
9cef0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9cf00 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20    j = 0;.       
9cf10 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29   if( needQuote )
9cf20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b   bufpt[j++] = q;
9cf30 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
9cf40 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29  ; (ch=escarg[i])
9cf50 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  !=0; i++){.     
9cf60 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20       bufpt[j++] 
9cf70 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20  = ch;.          
9cf80 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 70  if( ch==q ) bufp
9cf90 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20  t[j++] = ch;.   
9cfa0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
9cfb0 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62  f( needQuote ) b
9cfc0 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20  ufpt[j++] = q;. 
9cfd0 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d 20         bufpt[j] 
9cfe0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  = 0;.        len
9cff0 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20  gth = j;.       
9d000 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 6f   /* The precisio
9d010 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20  n is ignored on 
9d020 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20  %q and %Q */.   
9d030 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 63       /* if( prec
9d040 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63  ision>=0 && prec
9d050 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c  ision<length ) l
9d060 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f  ength = precisio
9d070 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72  n; */.        br
9d080 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
9d090 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a     case etTOKEN:
9d0a0 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e   {.        Token
9d0b0 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72   *pToken = va_ar
9d0c0 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20  g(ap, Token*);. 
9d0d0 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65         if( pToke
9d0e0 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  n ){.          s
9d0f0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
9d100 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f  pend(pAccum, (co
9d110 6e 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e  nst char*)pToken
9d120 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b  ->z, pToken->n);
9d130 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
9d140 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74     length = widt
9d150 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  h = 0;.        b
9d160 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
9d170 20 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49      case etSRCLI
9d180 53 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72  ST: {.        Sr
9d190 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61  cList *pSrc = va
9d1a0 5f 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74  _arg(ap, SrcList
9d1b0 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  *);.        int 
9d1c0 6b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  k = va_arg(ap, i
9d1d0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72  nt);.        str
9d1e0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
9d1f0 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d   *pItem = &pSrc-
9d200 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61  >a[k];.        a
9d210 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b  ssert( k>=0 && k
9d220 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20  <pSrc->nSrc );. 
9d230 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
9d240 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20  ->zDatabase ){. 
9d250 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
9d260 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
9d270 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44  Accum, pItem->zD
9d280 61 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20  atabase, -1);.  
9d290 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
9d2a0 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
9d2b0 63 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20  ccum, ".", 1);. 
9d2c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9d2d0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
9d2e0 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70  Append(pAccum, p
9d2f0 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29  Item->zName, -1)
9d300 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  ;.        length
9d310 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20   = width = 0;.  
9d320 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
9d330 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64     }.    }/* End
9d340 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 68 65   switch over the
9d350 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a   format type */.
9d360 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68      /*.    ** Th
9d370 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f  e text of the co
9d380 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e  nversion is poin
9d390 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 70 74  ted to by "bufpt
9d3a0 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20  " and is.    ** 
9d3b0 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63 74  "length" charact
9d3c0 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66  ers long.  The f
9d3d0 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 22 77  ield width is "w
9d3e0 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a  idth".  Do.    *
9d3f0 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 20  * the output..  
9d400 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 6c    */.    if( !fl
9d410 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 29  ag_leftjustify )
9d420 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65 72  {.      register
9d430 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 20   int nspace;.   
9d440 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74     nspace = widt
9d450 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20  h-length;.      
9d460 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a  if( nspace>0 ){.
9d470 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53 70          appendSp
9d480 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61  ace(pAccum, nspa
9d490 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ce);.      }.   
9d4a0 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74   }.    if( lengt
9d4b0 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  h>0 ){.      sql
9d4c0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
9d4d0 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74  nd(pAccum, bufpt
9d4e0 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d  , length);.    }
9d4f0 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65  .    if( flag_le
9d500 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20  ftjustify ){.   
9d510 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20     register int 
9d520 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73  nspace;.      ns
9d530 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e  pace = width-len
9d540 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  gth;.      if( n
9d550 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20  space>0 ){.     
9d560 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 70     appendSpace(p
9d570 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a  Accum, nspace);.
9d580 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
9d590 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a    if( zExtra ){.
9d5a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
9d5b0 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20  ee(zExtra);.    
9d5c0 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20  }.  }/* End for 
9d5d0 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f  loop over the fo
9d5e0 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d  rmat string */.}
9d5f0 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74   /* End of funct
9d600 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70  ion */../*.** Ap
9d610 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 20  pend N bytes of 
9d620 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74  text from z to t
9d630 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65  he StrAccum obje
9d640 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ct..*/.SQLITE_PR
9d650 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
9d660 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
9d670 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f  (StrAccum *p, co
9d680 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
9d690 20 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f   N){.  if( p->to
9d6a0 6f 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63  oBig | p->malloc
9d6b0 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
9d6c0 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  turn;.  }.  if( 
9d6d0 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73  N<0 ){.    N = s
9d6e0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
9d6f0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d  );.  }.  if( N==
9d700 30 20 7c 7c 20 7a 3d 3d 30 20 29 7b 0a 20 20 20  0 || z==0 ){.   
9d710 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
9d720 66 28 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d  f( p->nChar+N >=
9d730 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20   p->nAlloc ){.  
9d740 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20    char *zNew;.  
9d750 20 20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 6c    if( !p->useMal
9d760 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  loc ){.      p->
9d770 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20  tooBig = 1;.    
9d780 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20    N = p->nAlloc 
9d790 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b 0a  - p->nChar - 1;.
9d7a0 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 29        if( N<=0 )
9d7b0 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
9d7c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
9d7d0 6c 73 65 7b 0a 20 20 20 20 20 20 69 36 34 20 73  lse{.      i64 s
9d7e0 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 3b  zNew = p->nChar;
9d7f0 0a 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d 20  .      szNew += 
9d800 4e 20 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 28  N + 1;.      if(
9d810 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 6c   szNew > p->mxAl
9d820 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73  loc ){.        s
9d830 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
9d840 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
9d850 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20  p->tooBig = 1;. 
9d860 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
9d870 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
9d880 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20      p->nAlloc = 
9d890 28 69 6e 74 29 73 7a 4e 65 77 3b 0a 20 20 20 20  (int)szNew;.    
9d8a0 20 20 7d 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d    }.      zNew =
9d8b0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
9d8c0 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 41  Raw(p->db, p->nA
9d8d0 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 20 20 69 66  lloc );.      if
9d8e0 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( zNew ){.      
9d8f0 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 70    memcpy(zNew, p
9d900 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68 61  ->zText, p->nCha
9d910 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  r);.        sqli
9d920 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74  te3StrAccumReset
9d930 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
9d940 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0a 20 20  zText = zNew;.  
9d950 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9d960 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c     p->mallocFail
9d970 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ed = 1;.        
9d980 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
9d990 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20  eset(p);.       
9d9a0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
9d9b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d  .    }.  }.  mem
9d9c0 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d  cpy(&p->zText[p-
9d9d0 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a  >nChar], z, N);.
9d9e0 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b    p->nChar += N;
9d9f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68  .}../*.** Finish
9da00 20 6f 66 66 20 61 20 73 74 72 69 6e 67 20 62 79   off a string by
9da10 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 69 74 20   making sure it 
9da20 69 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  is zero-terminat
9da30 65 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ed..** Return a 
9da40 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  pointer to the r
9da50 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e  esulting string.
9da60 20 20 52 65 74 75 72 6e 20 61 20 4e 55 4c 4c 0a    Return a NULL.
9da70 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 61 6e  ** pointer if an
9da80 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20  y kind of error 
9da90 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e  was encountered.
9daa0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9dab0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
9dac0 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53  StrAccumFinish(S
9dad0 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69  trAccum *p){.  i
9dae0 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20  f( p->zText ){. 
9daf0 20 20 20 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e     p->zText[p->n
9db00 43 68 61 72 5d 20 3d 20 30 3b 0a 20 20 20 20 69  Char] = 0;.    i
9db10 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20  f( p->useMalloc 
9db20 26 26 20 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e  && p->zText==p->
9db30 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20 70  zBase ){.      p
9db40 2d 3e 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65  ->zText = sqlite
9db50 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e  3DbMallocRaw(p->
9db60 64 62 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 20 29  db, p->nChar+1 )
9db70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a  ;.      if( p->z
9db80 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Text ){.        
9db90 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 2c  memcpy(p->zText,
9dba0 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e 43   p->zBase, p->nC
9dbb0 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65  har+1);.      }e
9dbc0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  lse{.        p->
9dbd0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
9dbe0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
9dbf0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e    }.  return p->
9dc00 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zText;.}../*.** 
9dc10 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75  Reset an StrAccu
9dc20 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61  m string.  Recla
9dc30 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20  im all malloced 
9dc40 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  memory..*/.SQLIT
9dc50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
9dc60 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
9dc70 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 29  set(StrAccum *p)
9dc80 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74  {.  if( p->zText
9dc90 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20  !=p->zBase ){.  
9dca0 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
9dcb0 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78 74 29  p->db, p->zText)
9dcc0 3b 0a 20 20 7d 0a 20 20 70 2d 3e 7a 54 65 78 74  ;.  }.  p->zText
9dcd0 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49   = 0;.}../*.** I
9dce0 6e 69 74 69 61 6c 69 7a 65 20 61 20 73 74 72 69  nitialize a stri
9dcf0 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 2a  ng accumulator.*
9dd00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9dd10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
9dd20 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63  AccumInit(StrAcc
9dd30 75 6d 20 2a 70 2c 20 63 68 61 72 20 2a 7a 42 61  um *p, char *zBa
9dd40 73 65 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6d  se, int n, int m
9dd50 78 29 7b 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d  x){.  p->zText =
9dd60 20 70 2d 3e 7a 42 61 73 65 20 3d 20 7a 42 61 73   p->zBase = zBas
9dd70 65 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b 0a  e;.  p->db = 0;.
9dd80 20 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b 0a    p->nChar = 0;.
9dd90 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b    p->nAlloc = n;
9dda0 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20  .  p->mxAlloc = 
9ddb0 6d 78 3b 0a 20 20 70 2d 3e 75 73 65 4d 61 6c 6c  mx;.  p->useMall
9ddc0 6f 63 20 3d 20 31 3b 0a 20 20 70 2d 3e 74 6f 6f  oc = 1;.  p->too
9ddd0 42 69 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 6d 61  Big = 0;.  p->ma
9dde0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
9ddf0 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
9de00 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
9de10 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
9de20 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68  alloc().  Use th
9de30 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d  e internal.** %-
9de40 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e  conversion exten
9de50 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sions..*/.SQLITE
9de60 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
9de70 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73  qlite3VMPrintf(s
9de80 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
9de90 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
9dea0 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20   va_list ap){.  
9deb0 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20  char *z;.  char 
9dec0 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49  zBase[SQLITE_PRI
9ded0 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20  NT_BUF_SIZE];.  
9dee0 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20  StrAccum acc;.  
9def0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49  sqlite3StrAccumI
9df00 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c  nit(&acc, zBase,
9df10 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a   sizeof(zBase),.
9df20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9df30 20 20 20 20 20 20 64 62 20 3f 20 64 62 2d 3e 61        db ? db->a
9df40 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
9df50 49 54 5f 4c 45 4e 47 54 48 5d 20 3a 20 53 51 4c  IT_LENGTH] : SQL
9df60 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b  ITE_MAX_LENGTH);
9df70 0a 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b 0a  .  acc.db = db;.
9df80 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74    sqlite3VXPrint
9df90 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d  f(&acc, 1, zForm
9dfa0 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73  at, ap);.  z = s
9dfb0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
9dfc0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 66  nish(&acc);.  if
9dfd0 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c  ( acc.mallocFail
9dfe0 65 64 20 26 26 20 64 62 20 29 7b 0a 20 20 20 20  ed && db ){.    
9dff0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
9e000 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
9e010 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn z;.}../*.** P
9e020 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
9e030 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
9e040 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
9e050 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  Use the internal
9e060 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  .** %-conversion
9e070 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
9e080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
9e090 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69  har *sqlite3MPri
9e0a0 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ntf(sqlite3 *db,
9e0b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
9e0c0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
9e0d0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
9e0e0 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28   *z;.  va_start(
9e0f0 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
9e100 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  z = sqlite3VMPri
9e110 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
9e120 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
9e130 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  p);.  return z;.
9e140 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71  }../*.** Like sq
9e150 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20  lite3MPrintf(), 
9e160 62 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  but call sqlite3
9e170 44 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72  DbFree() on zStr
9e180 20 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e   after formattin
9e190 67 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  g.** the string 
9e1a0 61 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72  and before retur
9e1b0 6e 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75  nning.  This rou
9e1c0 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64  tine is intended
9e1d0 20 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74   to be used.** t
9e1e0 6f 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73  o modify an exis
9e1f0 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f  ting string.  Fo
9e200 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
9e210 20 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74         x = sqlit
9e220 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c  e3MPrintf(db, x,
9e230 20 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66   "prefix %s suff
9e240 69 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53  ix", x);.**.*/.S
9e250 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
9e260 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65  ar *sqlite3MAppe
9e270 6e 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ndf(sqlite3 *db,
9e280 20 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e   char *zStr, con
9e290 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
9e2a0 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
9e2b0 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b  t ap;.  char *z;
9e2c0 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
9e2d0 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20  zFormat);.  z = 
9e2e0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
9e2f0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
9e300 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
9e310 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
9e320 64 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74  db, zStr);.  ret
9e330 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
9e340 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
9e350 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
9e360 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
9e370 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65  .  Omit the inte
9e380 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72  rnal.** %-conver
9e390 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e  sion extensions.
9e3a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
9e3b0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70  har *sqlite3_vmp
9e3c0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
9e3d0 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69   *zFormat, va_li
9e3e0 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a  st ap){.  char *
9e3f0 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b  z;.  char zBase[
9e400 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
9e410 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63  _SIZE];.  StrAcc
9e420 75 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20  um acc;.#ifndef 
9e430 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
9e440 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74  INIT.  if( sqlit
9e450 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
9e460 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64  ) return 0;.#end
9e470 69 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41  if.  sqlite3StrA
9e480 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a  ccumInit(&acc, z
9e490 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61  Base, sizeof(zBa
9e4a0 73 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  se), SQLITE_MAX_
9e4b0 4c 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74  LENGTH);.  sqlit
9e4c0 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c  e3VXPrintf(&acc,
9e4d0 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29   0, zFormat, ap)
9e4e0 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53  ;.  z = sqlite3S
9e4f0 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61  trAccumFinish(&a
9e500 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b  cc);.  return z;
9e510 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  .}../*.** Print 
9e520 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
9e530 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
9e540 33 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f  3_malloc()().  O
9e550 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  mit the internal
9e560 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  .** %-conversion
9e570 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
9e580 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
9e590 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66  *sqlite3_mprintf
9e5a0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f  (const char *zFo
9e5b0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
9e5c0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
9e5d0 20 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   *z;.#ifndef SQL
9e5e0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
9e5f0 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
9e600 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72  initialize() ) r
9e610 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a  eturn 0;.#endif.
9e620 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
9e630 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73  Format);.  z = s
9e640 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
9e650 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
9e660 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65  va_end(ap);.  re
9e670 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn z;.}../*.**
9e680 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
9e690 66 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73  f() works like s
9e6a0 6e 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74  nprintf() except
9e6b0 20 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73   that it ignores
9e6c0 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
9e6d0 6c 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e  locale settings.
9e6e0 20 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74    This is import
9e6f0 61 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62  ant for SQLite b
9e700 65 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65  ecause we.** are
9e710 20 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65   not able to use
9e720 20 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65   a "," as the de
9e730 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70  cimal point in p
9e740 6c 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a  lace of "." as.*
9e750 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73  * specified by s
9e760 6f 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a  ome locales..*/.
9e770 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
9e780 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  *sqlite3_snprint
9e790 66 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a  f(int n, char *z
9e7a0 42 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Buf, const char 
9e7b0 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
9e7c0 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f    char *z;.  va_
9e7d0 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63  list ap;.  StrAc
9e7e0 63 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20  cum acc;..  if( 
9e7f0 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  n<=0 ){.    retu
9e800 72 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73  rn zBuf;.  }.  s
9e810 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
9e820 69 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e  it(&acc, zBuf, n
9e830 2c 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d  , 0);.  acc.useM
9e840 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f  alloc = 0;.  va_
9e850 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74  start(ap,zFormat
9e860 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72  );.  sqlite3VXPr
9e870 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46  intf(&acc, 0, zF
9e880 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
9e890 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20  _end(ap);.  z = 
9e8a0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
9e8b0 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72  inish(&acc);.  r
9e8c0 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20  eturn z;.}..#if 
9e8d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
9e8e0 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65  EBUG)./*.** A ve
9e8f0 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28  rsion of printf(
9e900 29 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e  ) that understan
9e910 64 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66  ds %lld.  Used f
9e920 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a  or debugging..**
9e930 20 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75   The printf() bu
9e940 69 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65  ilt into some ve
9e950 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77  rsions of window
9e960 73 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72  s does not under
9e970 73 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e  stand %lld.** an
9e980 64 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79  d segfaults if y
9e990 6f 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e  ou give it a lon
9e9a0 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53  g long int..*/.S
9e9b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9e9c0 69 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  id sqlite3DebugP
9e9d0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
9e9e0 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
9e9f0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
9ea00 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20   StrAccum acc;. 
9ea10 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b   char zBuf[500];
9ea20 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
9ea30 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75  umInit(&acc, zBu
9ea40 66 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c  f, sizeof(zBuf),
9ea50 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61   0);.  acc.useMa
9ea60 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73  lloc = 0;.  va_s
9ea70 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29  tart(ap,zFormat)
9ea80 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  ;.  sqlite3VXPri
9ea90 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f  ntf(&acc, 0, zFo
9eaa0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
9eab0 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74  end(ap);.  sqlit
9eac0 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68  e3StrAccumFinish
9ead0 28 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74  (&acc);.  fprint
9eae0 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a  f(stdout,"%s", z
9eaf0 42 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73  Buf);.  fflush(s
9eb00 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66  tdout);.}.#endif
9eb10 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
9eb20 2a 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e  * End of printf.
9eb30 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
9eb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9eb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9eb60 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
9eb70 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e  * Begin file ran
9eb80 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dom.c **********
9eb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9eba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9ebb0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
9ebc0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
9ebd0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
9ebe0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
9ebf0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
9ec00 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
9ec10 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
9ec20 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
9ec30 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
9ec40 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
9ec50 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
9ec60 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
9ec70 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
9ec80 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
9ec90 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
9eca0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
9ecb0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
9ecc0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
9ecd0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
9ece0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9ecf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9ed00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9ed10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9ed20 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
9ed30 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
9ed40 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
9ed50 20 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d   a pseudo-random
9ed60 20 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72   number.** gener
9ed70 61 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20  ator (PRNG) for 
9ed80 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61  SQLite..**.** Ra
9ed90 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65  ndom numbers are
9eda0 20 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66   used by some of
9edb0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61   the database ba
9edc0 63 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a  ckends in order.
9edd0 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72  ** to generate r
9ede0 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65  andom integer ke
9edf0 79 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72  ys for tables or
9ee00 20 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65   random filename
9ee10 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61  s..**.** $Id: ra
9ee20 6e 64 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30  ndom.c,v 1.29 20
9ee30 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32  08/12/10 19:26:2
9ee40 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  4 drh Exp $.*/..
9ee50 0a 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20  ./* All threads 
9ee60 73 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72  share a single r
9ee70 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
9ee80 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20  erator..** This 
9ee90 73 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65  structure is the
9eea0 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f   current state o
9eeb0 66 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e  f the generator.
9eec0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
9eed0 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c  E_WSD struct sql
9eee0 69 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20  ite3PrngType {. 
9eef0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69   unsigned char i
9ef00 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20  sInit;          
9ef10 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69  /* True if initi
9ef20 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69  alized */.  unsi
9ef30 67 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20  gned char i, j; 
9ef40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
9ef50 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f  ate variables */
9ef60 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
9ef70 20 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20   s[256];        
9ef80 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61    /* State varia
9ef90 62 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65  bles */.} sqlite
9efa0 33 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65  3Prng;../*.** Ge
9efb0 74 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74  t a single 8-bit
9efc0 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72   random value fr
9efd0 6f 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e  om the RC4 PRNG.
9efe0 20 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d    The Mutex.** m
9eff0 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c  ust be held whil
9f000 65 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73  e executing this
9f010 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
9f020 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65  Why not just use
9f030 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f   a library rando
9f040 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65  m generator like
9f050 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74   lrand48() for t
9f060 68 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20  his?.** Because 
9f070 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20  the OP_NewRowid 
9f080 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44  opcode in the VD
9f090 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61  BE depends on ha
9f0a0 76 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67  ving a very.** g
9f0b0 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61  ood source of ra
9f0c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54  ndom numbers.  T
9f0d0 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62  he lrand48() lib
9f0e0 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61  rary function ma
9f0f0 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f  y.** well be goo
9f100 64 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d  d enough.  But m
9f110 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61  aybe not.  Or ma
9f120 79 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61  ybe lrand48() ha
9f130 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65  s some.** subtle
9f140 20 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d   problems on som
9f150 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63  e systems that c
9f160 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c  ould cause probl
9f170 65 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64  ems.  It is hard
9f180 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f  .** to know.  To
9f190 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69   minimize the ri
9f1a0 73 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64  sk of problems d
9f1b0 75 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34  ue to bad lrand4
9f1c0 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  8().** implement
9f1d0 61 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75  ations, SQLite u
9f1e0 73 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20  ses this random 
9f1f0 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
9f200 20 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34   based.** on RC4
9f210 2c 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20  , which we know 
9f220 77 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e  works very well.
9f230 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20  .**.** (Later): 
9f240 20 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65   Actually, OP_Ne
9f250 77 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20  wRowid does not 
9f260 64 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64  depend on a good
9f270 20 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61   source of.** ra
9f280 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72  ndomness any mor
9f290 65 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20  e.  But we will 
9f2a0 6c 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20  leave this code 
9f2b0 69 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e  in all the same.
9f2c0 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61  .*/.static u8 ra
9f2d0 6e 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a  ndomByte(void){.
9f2e0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
9f2f0 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77  t;...  /* The "w
9f300 73 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69  sdPrng" macro wi
9f310 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  ll resolve to th
9f320 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  e pseudo-random 
9f330 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
9f340 0a 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74  .  ** state vect
9f350 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65  or.  If writable
9f360 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20   static data is 
9f370 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74  unsupported on t
9f380 68 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20  he target,.  ** 
9f390 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74  we have to locat
9f3a0 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74  e the state vect
9f3b0 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20  or at run-time. 
9f3c0 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d   In the more com
9f3d0 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68  mon.  ** case wh
9f3e0 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61  ere writable sta
9f3f0 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70  tic data is supp
9f400 6f 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63  orted, wsdPrng c
9f410 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c  an refer directl
9f420 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73  y.  ** to the "s
9f430 71 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74  qlite3Prng" stat
9f440 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65  e vector declare
9f450 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69  d above..  */.#i
9f460 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
9f470 5f 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71  _WSD.  struct sq
9f480 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70  lite3PrngType *p
9f490 20 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63   = &GLOBAL(struc
9f4a0 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70  t sqlite3PrngTyp
9f4b0 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b  e, sqlite3Prng);
9f4c0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e  .# define wsdPrn
9f4d0 67 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64  g p[0].#else.# d
9f4e0 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71  efine wsdPrng sq
9f4f0 6c 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66  lite3Prng.#endif
9f500 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ...  /* Initiali
9f510 7a 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  ze the state of 
9f520 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  the random numbe
9f530 72 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65  r generator once
9f540 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74  ,.  ** the first
9f550 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69   time this routi
9f560 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54  ne is called.  T
9f570 68 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f  he seed value do
9f580 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64  es.  ** not need
9f590 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f   to contain a lo
9f5a0 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20  t of randomness 
9f5b0 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74  since we are not
9f5c0 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20  .  ** trying to 
9f5d0 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70  do secure encryp
9f5e0 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67  tion or anything
9f5f0 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20   like that....  
9f600 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20  **.  ** Nothing 
9f610 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20  in this file or 
9f620 61 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e  anywhere else in
9f630 20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79   SQLite does any
9f640 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e   kind of.  ** en
9f650 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52  cryption.  The R
9f660 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20  C4 algorithm is 
9f670 62 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20  being used as a 
9f680 50 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e  PRNG (pseudo-ran
9f690 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20  dom.  ** number 
9f6a0 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61  generator) not a
9f6b0 73 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20  s an encryption 
9f6c0 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69  device..  */.  i
9f6d0 66 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e  f( !wsdPrng.isIn
9f6e0 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  it ){.    int i;
9f6f0 0a 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d  .    char k[256]
9f700 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20  ;.    wsdPrng.j 
9f710 3d 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67  = 0;.    wsdPrng
9f720 2e 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  .i = 0;.    sqli
9f730 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28  te3OsRandomness(
9f740 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64  sqlite3_vfs_find
9f750 28 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20  (0), 256, k);.  
9f760 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36    for(i=0; i<256
9f770 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73  ; i++){.      ws
9f780 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38  dPrng.s[i] = (u8
9f790 29 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  )i;.    }.    fo
9f7a0 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b  r(i=0; i<256; i+
9f7b0 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e  +){.      wsdPrn
9f7c0 67 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73  g.j += wsdPrng.s
9f7d0 5b 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20  [i] + k[i];.    
9f7e0 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b    t = wsdPrng.s[
9f7f0 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20  wsdPrng.j];.    
9f800 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50    wsdPrng.s[wsdP
9f810 72 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67  rng.j] = wsdPrng
9f820 2e 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64  .s[i];.      wsd
9f830 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20  Prng.s[i] = t;. 
9f840 20 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67     }.    wsdPrng
9f850 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d  .isInit = 1;.  }
9f860 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
9f870 61 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c  and return singl
9f880 65 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20  e random byte.  
9f890 2a 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b  */.  wsdPrng.i++
9f8a0 3b 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e  ;.  t = wsdPrng.
9f8b0 73 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20  s[wsdPrng.i];.  
9f8c0 77 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a  wsdPrng.j += t;.
9f8d0 20 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50    wsdPrng.s[wsdP
9f8e0 72 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67  rng.i] = wsdPrng
9f8f0 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20  .s[wsdPrng.j];. 
9f900 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
9f910 6e 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b  ng.j] = t;.  t +
9f920 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50  = wsdPrng.s[wsdP
9f930 72 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e  rng.i];.  return
9f940 20 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d   wsdPrng.s[t];.}
9f950 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e  ../*.** Return N
9f960 20 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a   random bytes..*
9f970 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
9f980 64 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d  d sqlite3_random
9f990 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64  ness(int N, void
9f9a0 20 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67   *pBuf){.  unsig
9f9b0 6e 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d  ned char *zBuf =
9f9c0 20 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54   pBuf;.#if SQLIT
9f9d0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73  E_THREADSAFE.  s
9f9e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
9f9f0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
9fa00 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
9fa10 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47  UTEX_STATIC_PRNG
9fa20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  );.#endif.  sqli
9fa30 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9fa40 6d 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28  mutex);.  while(
9fa50 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42   N-- ){.    *(zB
9fa60 75 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79  uf++) = randomBy
9fa70 74 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  te();.  }.  sqli
9fa80 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9fa90 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64  mutex);.}..#ifnd
9faa0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
9fab0 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a  UILTIN_TEST./*.*
9fac0 2a 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75  * For testing pu
9fad0 72 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74  rposes, we somet
9fae0 69 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65  imes want to pre
9faf0 73 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20  serve the state 
9fb00 6f 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72  of.** PRNG and r
9fb10 65 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20  estore the PRNG 
9fb20 74 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61  to its saved sta
9fb30 74 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69  te at a later ti
9fb40 6d 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73  me, or.** to res
9fb50 65 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69  et the PRNG to i
9fb60 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65  ts initial state
9fb70 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
9fb80 73 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20  s accomplish.** 
9fb90 74 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a  those tasks..**.
9fba0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74  ** The sqlite3_t
9fbb0 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e  est_control() in
9fbc0 74 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68  terface calls th
9fbd0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a  ese routines to.
9fbe0 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50  ** control the P
9fbf0 52 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  RNG..*/.static S
9fc00 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
9fc10 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
9fc20 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e   sqlite3SavedPrn
9fc30 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  g;.SQLITE_PRIVAT
9fc40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
9fc50 6e 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64  ngSaveState(void
9fc60 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20  ){.  memcpy(.   
9fc70 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20   &GLOBAL(struct 
9fc80 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c  sqlite3PrngType,
9fc90 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e   sqlite3SavedPrn
9fca0 67 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28  g),.    &GLOBAL(
9fcb0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
9fcc0 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50  ngType, sqlite3P
9fcd0 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66  rng),.    sizeof
9fce0 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20  (sqlite3Prng).  
9fcf0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
9fd00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
9fd10 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65  PrngRestoreState
9fd20 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79  (void){.  memcpy
9fd30 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74  (.    &GLOBAL(st
9fd40 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
9fd50 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e  Type, sqlite3Prn
9fd60 67 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28  g),.    &GLOBAL(
9fd70 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
9fd80 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53  ngType, sqlite3S
9fd90 61 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73  avedPrng),.    s
9fda0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e  izeof(sqlite3Prn
9fdb0 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45  g).  );.}.SQLITE
9fdc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
9fdd0 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74  lite3PrngResetSt
9fde0 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f  ate(void){.  GLO
9fdf0 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74  BAL(struct sqlit
9fe00 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69  e3PrngType, sqli
9fe10 74 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20  te3Prng).isInit 
9fe20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  = 0;.}.#endif /*
9fe30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
9fe40 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a  LTIN_TEST */../*
9fe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
9fe60 64 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a  d of random.c **
9fe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fe90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
9fea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
9feb0 67 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a  gin file utf.c *
9fec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
9fef0 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31  .** 2004 April 1
9ff00 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
9ff10 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
9ff20 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
9ff30 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
9ff40 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
9ff50 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
9ff60 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
9ff70 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
9ff80 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
9ff90 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
9ffa0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
9ffb0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
9ffc0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
9ffd0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
9ffe0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
9fff0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
a0000 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
a0010 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
a0020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0060 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
a0070 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20  ntains routines 
a0080 75 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74  used to translat
a0090 65 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c  e between UTF-8,
a00a0 20 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46   .** UTF-16, UTF
a00b0 2d 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31  -16BE, and UTF-1
a00c0 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  6LE..**.** $Id: 
a00d0 75 74 66 2e 63 2c 76 20 31 2e 37 31 20 32 30 30  utf.c,v 1.71 200
a00e0 39 2f 30 33 2f 33 31 20 30 33 3a 34 31 3a 35 37  9/03/31 03:41:57
a00f0 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2a 0a   shane Exp $.**.
a0100 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d  ** Notes on UTF-
a0110 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d  8:.**.**   Byte-
a0120 30 20 20 20 20 42 79 74 65 2d 31 20 20 20 20 42  0    Byte-1    B
a0130 79 74 65 2d 32 20 20 20 20 42 79 74 65 2d 33 20  yte-2    Byte-3 
a0140 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78     Value.**  0xx
a0150 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20  xxxxx           
a0160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0170 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30        00000000 0
a0180 30 30 30 30 30 30 30 20 30 78 78 78 78 78 78 78  0000000 0xxxxxxx
a0190 0a 2a 2a 20 20 31 31 30 79 79 79 79 79 20 20 31  .**  110yyyyy  1
a01a0 30 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20  0xxxxxx         
a01b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30                00
a01c0 30 30 30 30 30 30 20 30 30 30 30 30 79 79 79 20  000000 00000yyy 
a01d0 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31  yyxxxxxx.**  111
a01e0 30 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20  0zzzz  10yyyyyy 
a01f0 20 31 30 78 78 78 78 78 78 20 20 20 20 20 20 20   10xxxxxx       
a0200 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 7a        00000000 z
a0210 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78  zzzyyyy yyxxxxxx
a0220 0a 2a 2a 20 20 31 31 31 31 30 75 75 75 20 20 31  .**  11110uuu  1
a0230 30 75 75 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79  0uuzzzz  10yyyyy
a0240 79 20 20 31 30 78 78 78 78 78 78 20 20 20 30 30  y  10xxxxxx   00
a0250 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20  0uuuuu zzzzyyyy 
a0260 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a  yyxxxxxx.**.**.*
a0270 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31  * Notes on UTF-1
a0280 36 3a 20 20 28 77 69 74 68 20 77 77 77 77 2b 31  6:  (with wwww+1
a0290 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20  ==uuuuu).**.**  
a02a0 20 20 20 20 57 6f 72 64 2d 30 20 20 20 20 20 20      Word-0      
a02b0 20 20 20 20 20 20 20 20 20 57 6f 72 64 2d 31 20           Word-1 
a02c0 20 20 20 20 20 20 20 20 20 56 61 6c 75 65 0a 2a           Value.*
a02d0 2a 20 20 31 31 30 31 31 30 77 77 20 77 77 7a 7a  *  110110ww wwzz
a02e0 7a 7a 79 79 20 20 20 31 31 30 31 31 31 79 79 20  zzyy   110111yy 
a02f0 79 79 78 78 78 78 78 78 20 20 20 20 30 30 30 75  yyxxxxxx    000u
a0300 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79  uuuu zzzzyyyy yy
a0310 78 78 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79  xxxxxx.**  zzzzy
a0320 79 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20  yyy yyxxxxxx    
a0330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0340 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a      00000000 zzz
a0350 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a  zyyyy yyxxxxxx.*
a0360 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42  *.**.** BOM or B
a0370 79 74 65 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a  yte Order Mark:.
a0380 2a 2a 20 20 20 20 20 30 78 66 66 20 30 78 66 65  **     0xff 0xfe
a0390 20 20 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e     little-endian
a03a0 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a   utf-16 follows.
a03b0 2a 2a 20 20 20 20 20 30 78 66 65 20 30 78 66 66  **     0xfe 0xff
a03c0 20 20 20 62 69 67 2d 65 6e 64 69 61 6e 20 75 74     big-endian ut
a03d0 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a  f-16 follows.**.
a03e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
a03f0 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49  ** Include vdbeI
a0400 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  nt.h in the midd
a0410 6c 65 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a  le of utf.c ****
a0420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0430 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
a0440 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
a0450 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  beInt.h ********
a0460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0480 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65  */./*.** 2003 Se
a0490 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20  ptember 6.**.** 
a04a0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
a04b0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
a04c0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
a04d0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
a04e0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
a04f0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
a0500 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
a0510 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
a0520 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
a0530 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
a0540 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
a0550 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
a0560 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
a0570 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
a0580 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
a0590 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
a05a0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
a05b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a05c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a05d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a05e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a05f0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
a0600 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66   is the header f
a0610 69 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74  ile for informat
a0620 69 6f 6e 20 74 68 61 74 20 69 73 20 70 72 69 76  ion that is priv
a0630 61 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44  ate to the.** VD
a0640 42 45 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d  BE.  This inform
a0650 61 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c  ation used to al
a0660 6c 20 62 65 20 61 74 20 74 68 65 20 74 6f 70 20  l be at the top 
a0670 6f 66 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a  of the single.**
a0680 20 73 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c   source code fil
a0690 65 20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65  e "vdbe.c".  Whe
a06a0 6e 20 74 68 61 74 20 66 69 6c 65 20 62 65 63 61  n that file beca
a06b0 6d 65 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72  me too big (over
a06c0 0a 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c  .** 6000 lines l
a06d0 6f 6e 67 29 20 69 74 20 77 61 73 20 73 70 6c 69  ong) it was spli
a06e0 74 20 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61  t up into severa
a06f0 6c 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20  l smaller files 
a0700 61 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64  and.** this head
a0710 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77  er information w
a0720 61 73 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e  as factored out.
a0730 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
a0740 49 6e 74 2e 68 2c 76 20 31 2e 31 36 36 20 32 30  Int.h,v 1.166 20
a0750 30 39 2f 30 33 2f 31 38 20 31 30 3a 33 33 3a 30  09/03/18 10:33:0
a0760 32 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  2 danielk1977 Ex
a0770 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  p $.*/.#ifndef _
a0780 56 44 42 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69  VDBEINT_H_.#defi
a0790 6e 65 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 0a  ne _VDBEINT_H_..
a07a0 2f 2a 0a 2a 2a 20 69 6e 74 54 6f 4b 65 79 28 29  /*.** intToKey()
a07b0 20 61 6e 64 20 6b 65 79 54 6f 49 6e 74 28 29 20   and keyToInt() 
a07c0 75 73 65 64 20 74 6f 20 74 72 61 6e 73 66 6f 72  used to transfor
a07d0 6d 20 74 68 65 20 72 6f 77 69 64 2e 20 20 42 75  m the rowid.  Bu
a07e0 74 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 6c 61  t with.** the la
a07f0 74 65 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66  test versions of
a0800 20 74 68 65 20 64 65 73 69 67 6e 20 74 68 65 79   the design they
a0810 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a   are no-ops..*/.
a0820 23 64 65 66 69 6e 65 20 6b 65 79 54 6f 49 6e 74  #define keyToInt
a0830 28 58 29 20 20 20 28 58 29 0a 23 64 65 66 69 6e  (X)   (X).#defin
a0840 65 20 69 6e 74 54 6f 4b 65 79 28 58 29 20 20 20  e intToKey(X)   
a0850 28 58 29 0a 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20  (X).../*.** SQL 
a0860 69 73 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e  is translated in
a0870 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  to a sequence of
a0880 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 6f   instructions to
a0890 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20   be.** executed 
a08a0 62 79 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63  by a virtual mac
a08b0 68 69 6e 65 2e 20 20 45 61 63 68 20 69 6e 73 74  hine.  Each inst
a08c0 72 75 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ruction is an in
a08d0 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65  stance.** of the
a08e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
a08f0 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ture..*/.typedef
a0900 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 4f   struct VdbeOp O
a0910 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61  p;../*.** Boolea
a0920 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65  n values.*/.type
a0930 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61  def unsigned cha
a0940 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41  r Bool;../*.** A
a0950 20 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69   cursor is a poi
a0960 6e 74 65 72 20 69 6e 74 6f 20 61 20 73 69 6e 67  nter into a sing
a0970 6c 65 20 42 54 72 65 65 20 77 69 74 68 69 6e 20  le BTree within 
a0980 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
a0990 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63  .** The cursor c
a09a0 61 6e 20 73 65 65 6b 20 74 6f 20 61 20 42 54 72  an seek to a BTr
a09b0 65 65 20 65 6e 74 72 79 20 77 69 74 68 20 61 20  ee entry with a 
a09c0 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 2c 20  particular key, 
a09d0 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20  or.** loop over 
a09e0 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f 66 20 74  all entries of t
a09f0 68 65 20 42 74 72 65 65 2e 20 20 59 6f 75 20 63  he Btree.  You c
a0a00 61 6e 20 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e  an also insert n
a0a10 65 77 20 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72  ew BTree.** entr
a0a20 69 65 73 20 6f 72 20 72 65 74 72 69 65 76 65 20  ies or retrieve 
a0a30 74 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20  the key or data 
a0a40 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74  from the entry t
a0a50 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a  hat the cursor.*
a0a60 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  * is currently p
a0a70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a  ointing to..** .
a0a80 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20  ** Every cursor 
a0a90 74 68 61 74 20 74 68 65 20 76 69 72 74 75 61 6c  that the virtual
a0aa0 20 6d 61 63 68 69 6e 65 20 68 61 73 20 6f 70 65   machine has ope
a0ab0 6e 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  n is represented
a0ac0 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   by an.** instan
a0ad0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
a0ae0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
a0af0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65  *.** If the Vdbe
a0b00 43 75 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72  Cursor.isTrigger
a0b10 52 6f 77 20 66 6c 61 67 20 69 73 20 73 65 74 20  Row flag is set 
a0b20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
a0b30 69 73 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20  is cursor is.** 
a0b40 72 65 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20  really a single 
a0b50 72 6f 77 20 74 68 61 74 20 72 65 70 72 65 73 65  row that represe
a0b60 6e 74 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f  nts the NEW or O
a0b70 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20  LD pseudo-table 
a0b80 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67  of.** a row trig
a0b90 67 65 72 2e 20 20 54 68 65 20 64 61 74 61 20 66  ger.  The data f
a0ba0 6f 72 20 74 68 65 20 72 6f 77 20 69 73 20 73 74  or the row is st
a0bb0 6f 72 65 64 20 69 6e 20 56 64 62 65 43 75 72 73  ored in VdbeCurs
a0bc0 6f 72 2e 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20  or.pData and.** 
a0bd0 74 68 65 20 72 6f 77 69 64 20 69 73 20 69 6e 20  the rowid is in 
a0be0 56 64 62 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e  VdbeCursor.iKey.
a0bf0 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 43  .*/.struct VdbeC
a0c00 75 72 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 73  ursor {.  BtCurs
a0c10 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20  or *pCursor;    
a0c20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 73 74  /* The cursor st
a0c30 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20 62  ructure of the b
a0c40 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  ackend */.  int 
a0c50 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
a0c60 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75    /* Index of cu
a0c70 72 73 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e  rsor database in
a0c80 20 64 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d   db->aDb[] (or -
a0c90 31 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74  1) */.  i64 last
a0ca0 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a  Rowid;        /*
a0cb0 20 4c 61 73 74 20 72 6f 77 69 64 20 66 72 6f 6d   Last rowid from
a0cc0 20 61 20 4e 65 78 74 20 6f 72 20 4e 65 78 74 49   a Next or NextI
a0cd0 64 78 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  dx operation */.
a0ce0 20 20 42 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20    Bool zeroed;  
a0cf0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a0d00 69 66 20 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e  if zeroed out an
a0d10 64 20 72 65 61 64 79 20 66 6f 72 20 72 65 75 73  d ready for reus
a0d20 65 20 2a 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69  e */.  Bool rowi
a0d30 64 49 73 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20  dIsValid;    /* 
a0d40 54 72 75 65 20 69 66 20 6c 61 73 74 52 6f 77 69  True if lastRowi
a0d50 64 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20  d is valid */.  
a0d60 42 6f 6f 6c 20 61 74 46 69 72 73 74 3b 20 20 20  Bool atFirst;   
a0d70 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a0d80 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 66 69 72   pointing to fir
a0d90 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f  st entry */.  Bo
a0da0 6f 6c 20 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69  ol useRandomRowi
a0db0 64 3b 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  d;  /* Generate 
a0dc0 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  new record numbe
a0dd0 72 73 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79  rs semi-randomly
a0de0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52   */.  Bool nullR
a0df0 6f 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ow;         /* T
a0e00 72 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20  rue if pointing 
a0e10 74 6f 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f  to a row with no
a0e20 20 64 61 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20   data */.  Bool 
a0e30 70 73 65 75 64 6f 54 61 62 6c 65 3b 20 20 20 20  pseudoTable;    
a0e40 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 4e 45   /* This is a NE
a0e50 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d  W or OLD pseudo-
a0e60 74 61 62 6c 65 73 20 6f 66 20 61 20 74 72 69 67  tables of a trig
a0e70 67 65 72 20 2a 2f 0a 20 20 42 6f 6f 6c 20 65 70  ger */.  Bool ep
a0e80 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 3b 0a  hemPseudoTable;.
a0e90 20 20 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d    Bool deferredM
a0ea0 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c  oveto;  /* A cal
a0eb0 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
a0ec0 65 4d 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 65  eMoveto() is nee
a0ed0 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73  ded */.  Bool is
a0ee0 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f  Table;         /
a0ef0 2a 20 54 72 75 65 20 69 66 20 61 20 74 61 62 6c  * True if a tabl
a0f00 65 20 72 65 71 75 69 72 69 6e 67 20 69 6e 74 65  e requiring inte
a0f10 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f  ger keys */.  Bo
a0f20 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20 20 20 20  ol isIndex;     
a0f30 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
a0f40 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69  n index containi
a0f50 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e  ng keys only - n
a0f60 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 36 34 20  o data */.  i64 
a0f70 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 20 20 20  movetoTarget;   
a0f80 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
a0f90 20 74 68 65 20 64 65 66 65 72 72 65 64 20 73 71   the deferred sq
a0fa0 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
a0fb0 28 29 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70  () */.  Btree *p
a0fc0 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Bt;           /*
a0fd0 20 53 65 70 61 72 61 74 65 20 66 69 6c 65 20 68   Separate file h
a0fe0 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79  olding temporary
a0ff0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
a1000 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  nData;          
a1010 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
a1020 79 74 65 73 20 69 6e 20 70 44 61 74 61 20 2a 2f  ytes in pData */
a1030 0a 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20  .  char *pData; 
a1040 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
a1050 20 66 6f 72 20 61 20 4e 45 57 20 6f 72 20 4f 4c   for a NEW or OL
a1060 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a  D pseudo-table *
a1070 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20  /.  i64 iKey;   
a1080 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79            /* Key
a1090 20 66 6f 72 20 74 68 65 20 4e 45 57 20 6f 72 20   for the NEW or 
a10a0 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  OLD pseudo-table
a10b0 20 72 6f 77 20 2a 2f 0a 20 20 4b 65 79 49 6e 66   row */.  KeyInf
a10c0 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20  o *pKeyInfo;    
a10d0 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 20 69 6e  /* Info about in
a10e0 64 65 78 20 6b 65 79 73 20 6e 65 65 64 65 64 20  dex keys needed 
a10f0 62 79 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73  by index cursors
a1100 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   */.  int nField
a1110 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
a1120 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20  umber of fields 
a1130 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f  in the header */
a1140 0a 20 20 69 36 34 20 73 65 71 43 6f 75 6e 74 3b  .  i64 seqCount;
a1150 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 71 75           /* Sequ
a1160 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  ence counter */.
a1170 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63    sqlite3_vtab_c
a1180 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73  ursor *pVtabCurs
a1190 6f 72 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73  or;  /* The curs
a11a0 6f 72 20 66 6f 72 20 61 20 76 69 72 74 75 61 6c  or for a virtual
a11b0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73   table */.  cons
a11c0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
a11d0 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f   *pModule;     /
a11e0 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 20 63 75 72  * Module for cur
a11f0 73 6f 72 20 70 56 74 61 62 43 75 72 73 6f 72 20  sor pVtabCursor 
a1200 2a 2f 0a 0a 20 20 2f 2a 20 43 61 63 68 65 64 20  */..  /* Cached 
a1210 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
a1220 74 20 74 68 65 20 68 65 61 64 65 72 20 66 6f 72  t the header for
a1230 20 74 68 65 20 64 61 74 61 20 72 65 63 6f 72 64   the data record
a1240 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 63   that the.  ** c
a1250 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74  ursor is current
a1260 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  ly pointing to. 
a1270 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 63   Only valid if c
a1280 61 63 68 65 56 61 6c 69 64 20 69 73 20 74 72 75  acheValid is tru
a1290 65 2e 0a 20 20 2a 2a 20 61 52 6f 77 20 6d 69 67  e..  ** aRow mig
a12a0 68 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 70 68  ht point to (eph
a12b0 65 6d 65 72 61 6c 29 20 64 61 74 61 20 66 6f 72  emeral) data for
a12c0 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
a12d0 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 0a 20 20  , or it might.  
a12e0 2a 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f  ** be NULL..  */
a12f0 0a 20 20 69 6e 74 20 63 61 63 68 65 53 74 61 74  .  int cacheStat
a1300 75 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68  us;      /* Cach
a1310 65 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68  e is valid if th
a1320 69 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e  is matches Vdbe.
a1330 63 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 69 6e  cacheCtr */.  in
a1340 74 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20  t payloadSize;  
a1350 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
a1360 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
a1370 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
a1380 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20  u32 *aType;     
a1390 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76 61        /* Type va
a13a0 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e 74  lues for all ent
a13b0 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ries in the reco
a13c0 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66  rd */.  u32 *aOf
a13d0 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  fset;         /*
a13e0 20 43 61 63 68 65 64 20 6f 66 66 73 65 74 73 20   Cached offsets 
a13f0 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
a1400 65 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 61 74  each columns dat
a1410 61 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f 77 3b  a */.  u8 *aRow;
a1420 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a1430 44 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  Data for the cur
a1440 72 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 6c 6c  rent row, if all
a1450 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a   on one page */.
a1460 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  };.typedef struc
a1470 74 20 56 64 62 65 43 75 72 73 6f 72 20 56 64 62  t VdbeCursor Vdb
a1480 65 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20  eCursor;../*.** 
a1490 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64 62 65  A value for Vdbe
a14a0 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61 6c 69  Cursor.cacheVali
a14b0 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65  d that means the
a14c0 20 63 61 63 68 65 20 69 73 20 61 6c 77 61 79 73   cache is always
a14d0 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65   invalid..*/.#de
a14e0 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41 4c 45  fine CACHE_STALE
a14f0 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e   0../*.** Intern
a1500 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 20 6d  ally, the vdbe m
a1510 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 72 6c  anipulates nearl
a1520 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73  y all SQL values
a1530 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63   as Mem.** struc
a1540 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65 6d 20  tures. Each Mem 
a1550 73 74 72 75 63 74 20 6d 61 79 20 63 61 63 68 65  struct may cache
a1560 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 65 73   multiple repres
a1570 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 69 6e  entations (strin
a1580 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65 74  g,.** integer et
a1590 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d 65 20  c.) of the same 
a15a0 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 65 20  value.  A value 
a15b0 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 4d  (and therefore M
a15c0 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a 2a 2a  em structure).**
a15d0 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69   has the followi
a15e0 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a  ng properties:.*
a15f0 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 65 20  *.** Each value 
a1600 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74 20 74  has a manifest t
a1610 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 65 73  ype. The manifes
a1620 74 20 74 79 70 65 20 6f 66 20 74 68 65 20 76 61  t type of the va
a1630 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e  lue stored.** in
a1640 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20 69 73   a Mem struct is
a1650 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
a1660 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d   MemType(Mem*) m
a1670 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65 20 69  acro. The type i
a1680 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49  s.** one of SQLI
a1690 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f  TE_NULL, SQLITE_
a16a0 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 45 5f  INTEGER, SQLITE_
a16b0 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58  REAL, SQLITE_TEX
a16c0 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42  T or.** SQLITE_B
a16d0 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d  LOB..*/.struct M
a16e0 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20  em {.  union {. 
a16f0 20 20 20 69 36 34 20 69 3b 20 20 20 20 20 20 20     i64 i;       
a1700 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
a1710 72 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20  r value. */.    
a1720 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20  int nZero;      
a1730 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
a1740 20 62 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73   bit MEM_Zero is
a1750 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f   set in flags */
a1760 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44  .    FuncDef *pD
a1770 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 64  ef;      /* Used
a1780 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73   only when flags
a1790 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20  ==MEM_Agg */.   
a17a0 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74   RowSet *pRowSet
a17b0 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c  ;    /* Used onl
a17c0 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45  y when flags==ME
a17d0 4d 5f 52 6f 77 53 65 74 20 2a 2f 0a 20 20 7d 20  M_RowSet */.  } 
a17e0 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20  u;.  double r;  
a17f0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c           /* Real
a1800 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69   value */.  sqli
a1810 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
a1820 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65  /* The associate
a1830 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  d database conne
a1840 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20  ction */.  char 
a1850 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  *z;            /
a1860 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42  * String or BLOB
a1870 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20   value */.  int 
a1880 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
a1890 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
a18a0 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e  racters in strin
a18b0 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69  g value, excludi
a18c0 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36  ng '\0' */.  u16
a18d0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
a18e0 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61   /* Some combina
a18f0 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c  tion of MEM_Null
a1900 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44  , MEM_Str, MEM_D
a1910 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38  yn, etc. */.  u8
a1920 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20    type;         
a1930 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49    /* One of SQLI
a1940 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f  TE_NULL, SQLITE_
a1950 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54  TEXT, SQLITE_INT
a1960 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75  EGER, etc */.  u
a1970 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20  8  enc;         
a1980 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46     /* SQLITE_UTF
a1990 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  8, SQLITE_UTF16B
a19a0 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  E, SQLITE_UTF16L
a19b0 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  E */.  void (*xD
a19c0 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a  el)(void *);  /*
a19d0 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61   If not null, ca
a19e0 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
a19f0 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a   to delete Mem.z
a1a00 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c   */.  char *zMal
a1a10 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e  loc;      /* Dyn
a1a20 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f  amic buffer allo
a1a30 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
a1a40 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a  _malloc() */.};.
a1a50 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20  ./* One or more 
a1a60 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
a1a70 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 74   flags are set t
a1a80 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 76  o indicate the v
a1a90 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73  alidOK.** repres
a1aa0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
a1ab0 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
a1ac0 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e   the Mem struct.
a1ad0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45  .**.** If the ME
a1ae0 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73  M_Null flag is s
a1af0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  et, then the val
a1b00 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c  ue is an SQL NUL
a1b10 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f  L value..** No o
a1b20 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62  ther flags may b
a1b30 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61  e set in this ca
a1b40 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
a1b50 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73   MEM_Str flag is
a1b60 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20   set then Mem.z 
a1b70 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 69  points at a stri
a1b80 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
a1b90 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68  n..** Usually th
a1ba0 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e  is is encoded in
a1bb0 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64   the same unicod
a1bc0 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68  e encoding as th
a1bd0 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61  e main.** databa
a1be0 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f  se (see below fo
a1bf0 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49  r exceptions). I
a1c00 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66  f the MEM_Term f
a1c10 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73  lag is also.** s
a1c20 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72  et, then the str
a1c30 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69  ing is nul termi
a1c40 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49  nated. The MEM_I
a1c50 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20  nt and MEM_Real 
a1c60 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f  .** flags may co
a1c70 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 4d  exist with the M
a1c80 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a  EM_Str flag..**.
a1c90 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74  ** Multiple of t
a1ca0 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20  hese values can 
a1cb0 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c  appear in Mem.fl
a1cc0 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f  ags.  But only o
a1cd0 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20  ne.** at a time 
a1ce0 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65  can appear in Me
a1cf0 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69  m.type..*/.#defi
a1d00 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20  ne MEM_Null     
a1d10 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c   0x0001   /* Val
a1d20 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64  ue is NULL */.#d
a1d30 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20  efine MEM_Str   
a1d40 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20      0x0002   /* 
a1d50 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  Value is a strin
a1d60 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  g */.#define MEM
a1d70 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30  _Int       0x000
a1d80 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  4   /* Value is 
a1d90 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64  an integer */.#d
a1da0 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20  efine MEM_Real  
a1db0 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20      0x0008   /* 
a1dc0 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20  Value is a real 
a1dd0 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e  number */.#defin
a1de0 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20  e MEM_Blob      
a1df0 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75  0x0010   /* Valu
a1e00 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23  e is a BLOB */.#
a1e10 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65  define MEM_RowSe
a1e20 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a  t    0x0020   /*
a1e30 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53   Value is a RowS
a1e40 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65  et object */.#de
a1e50 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61 73  fine MEM_TypeMas
a1e60 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a 20 4d  k  0x00ff   /* M
a1e70 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74 73  ask of type bits
a1e80 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 65 72   */../* Whenever
a1e90 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 61 20   Mem contains a 
a1ea0 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f 72 20  valid string or 
a1eb0 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 61 74  blob representat
a1ec0 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74  ion, one of.** t
a1ed0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61  he following fla
a1ee0 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 20 74  gs must be set t
a1ef0 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
a1f00 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e  memory managemen
a1f10 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f 72 20  t.** policy for 
a1f20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 4d 5f  Mem.z.  The MEM_
a1f30 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c 73 20  Term flag tells 
a1f40 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  us whether or no
a1f50 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20  t the.** string 
a1f60 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 30 30  is \000 or \u000
a1f70 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a  0 terminated.*/.
a1f80 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 72 6d  #define MEM_Term
a1f90 20 20 20 20 20 20 30 78 30 32 30 30 20 20 20 2f        0x0200   /
a1fa0 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 73 20  * String rep is 
a1fb0 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 2a  nul terminated *
a1fc0 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44 79  /.#define MEM_Dy
a1fd0 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 20 20  n       0x0400  
a1fe0 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c   /* Need to call
a1ff0 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 6f 6e   sqliteFree() on
a2000 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e   Mem.z */.#defin
a2010 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20 20  e MEM_Static    
a2020 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d 2e  0x0800   /* Mem.
a2030 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74  z points to a st
a2040 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23  atic string */.#
a2050 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65 6d  define MEM_Ephem
a2060 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f 2a       0x1000   /*
a2070 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f   Mem.z points to
a2080 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74   an ephemeral st
a2090 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ring */.#define 
a20a0 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30 78  MEM_Agg       0x
a20b0 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20  2000   /* Mem.z 
a20c0 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67 67  points to an agg
a20d0 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78   function contex
a20e0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  t */.#define MEM
a20f0 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 30 30  _Zero      0x400
a2100 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e  0   /* Mem.i con
a2110 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 20 30  tains count of 0
a2120 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 62 6c  s appended to bl
a2130 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  ob */..#ifdef SQ
a2140 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
a2150 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d 5f  OB.  #undef MEM_
a2160 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20 4d  Zero.  #define M
a2170 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a 23  EM_Zero 0x0000.#
a2180 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c  endif.../*.** Cl
a2190 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e 67  ear any existing
a21a0 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f 6d   type flags from
a21b0 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c 61   a Mem and repla
a21c0 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a 2a  ce them with f.*
a21d0 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65 74  /.#define MemSet
a21e0 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20 5c  TypeFlag(p, f) \
a21f0 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 20  .   ((p)->flags 
a2200 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e 28  = ((p)->flags&~(
a2210 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45 4d  MEM_TypeMask|MEM
a2220 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20  _Zero))|f).../* 
a2230 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a 75  A VdbeFunc is ju
a2240 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64 65  st a FuncDef (de
a2250 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49  fined in sqliteI
a2260 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74 61  nt.h) that conta
a2270 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61  ins.** additiona
a2280 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  l information ab
a2290 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69 6e  out auxiliary in
a22a0 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64 20  formation bound 
a22b0 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20  to arguments.** 
a22c0 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  of the function.
a22d0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74    This is used t
a22e0 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
a22f0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
a2300 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71 6c  ata().** and sql
a2310 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
a2320 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22 61  () APIs.  The "a
a2330 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65 20  uxdata" is some 
a2340 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a 2a  auxiliary data.*
a2350 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61 73  * that can be as
a2360 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
a2370 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e  constant argumen
a2380 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e  t to a function.
a2390 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73    This.** allows
a23a0 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68 20   functions such 
a23b0 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20 63  as "regexp" to c
a23c0 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f 6e  ompile their con
a23d0 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a 2a  stant regular.**
a23e0 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67 75   expression argu
a23f0 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72 65  ment once and re
a2400 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c 65  used the compile
a2410 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74 69  d code for multi
a2420 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f  ple.** invocatio
a2430 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64  ns..*/.struct Vd
a2440 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63 44  beFunc {.  FuncD
a2450 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20  ef *pFunc;      
a2460 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
a2470 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68  definition of th
a2480 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
a2490 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20 20  int nAux;       
a24a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a24b0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
a24c0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
a24d0 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73 74   apAux[] */.  st
a24e0 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a 20  ruct AuxData {. 
a24f0 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20     void *pAux;  
a2500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2510 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f 72   /* Aux data for
a2520 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d 65   the i-th argume
a2530 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28  nt */.    void (
a2540 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20 2a  *xDelete)(void *
a2550 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72  );      /* Destr
a2560 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61 75  uctor for the au
a2570 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61 70  x data */.  } ap
a2580 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20 20  Aux[1];         
a2590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
a25a0 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20 66   slot for each f
a25b0 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
a25c0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
a25d0 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67 75  e "context" argu
a25e0 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74 61  ment for a insta
a25f0 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e 2e  llable function.
a2600 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61    A pointer to a
a2610 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  n.** instance of
a2620 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
a2630 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  is the first arg
a2640 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f 75  ument to the rou
a2650 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69 6d  tines used.** im
a2660 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20  plement the SQL 
a2670 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  functions..**.**
a2680 20 54 68 65 72 65 20 69 73 20 61 20 74 79 70 65   There is a type
a2690 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 74 72  def for this str
a26a0 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74 65  ucture in sqlite
a26b0 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75 74  .h.  So all rout
a26c0 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68  ines,.** even th
a26d0 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61  e public interfa
a26e0 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63 61  ce to SQLite, ca
a26f0 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72 20  n use a pointer 
a2700 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
a2710 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20 66  e..** But this f
a2720 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  ile is the only 
a2730 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 65 20  place where the 
a2740 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c 73  internal details
a2750 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 75   of this.** stru
a2760 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e 2e  cture are known.
a2770 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  .**.** This stru
a2780 63 74 75 72 65 20 69 73 20 64 65 66 69 6e 65 64  cture is defined
a2790 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65 49   inside of vdbeI
a27a0 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 74 20  nt.h because it 
a27b0 75 73 65 73 20 73 75 62 73 74 72 75 63 74 75 72  uses substructur
a27c0 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69 63  es.** (Mem) whic
a27d0 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69 6e  h are only defin
a27e0 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74 72  ed there..*/.str
a27f0 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  uct sqlite3_cont
a2800 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66 20  ext {.  FuncDef 
a2810 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f 2a  *pFunc;       /*
a2820 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e 63   Pointer to func
a2830 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tion information
a2840 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53 54  .  MUST BE FIRST
a2850 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20 2a   */.  VdbeFunc *
a2860 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20 41  pVdbeFunc;  /* A
a2870 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69 66  uxilary data, if
a2880 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20 4d   created. */.  M
a2890 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20 20  em s;           
a28a0 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75       /* The retu
a28b0 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  rn value is stor
a28c0 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 6d  ed here */.  Mem
a28d0 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20   *pMem;         
a28e0 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c     /* Memory cel
a28f0 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  l used to store 
a2900 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78  aggregate contex
a2910 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72  t */.  int isErr
a2920 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  or;          /* 
a2930 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72  Error code retur
a2940 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e 63 74  ned by the funct
a2950 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65  ion. */.  CollSe
a2960 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20  q *pColl;       
a2970 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
a2980 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  uence */.};../*.
a2990 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 74 75  ** A Set structu
a29a0 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 71  re is used for q
a29b0 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f 20  uick testing to 
a29c0 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a 2a  see if a value.*
a29d0 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 73  * is part of a s
a29e0 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73 20  mall set.  Sets 
a29f0 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  are used to impl
a2a00 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65 0a  ement code like.
a2a10 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20 20  ** this:.**     
a2a20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28 27         x.y IN ('
a2a30 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27 29  hi','hoo','hum')
a2a40 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
a2a50 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72 75  ct Set Set;.stru
a2a60 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68 20  ct Set {.  Hash 
a2a70 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20  hash;           
a2a80 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a 75    /* A set is ju
a2a90 73 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 20  st a hash table 
a2aa0 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70  */.  HashElem *p
a2ab0 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  rev;        /* P
a2ac0 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73 73  reviously access
a2ad0 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20 2a  ed hash elemen *
a2ae0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f  /.};../*.** A Co
a2af0 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65  ntext stores the
a2b00 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77   last insert row
a2b10 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61  id, the last sta
a2b20 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f  tement change co
a2b30 75 6e 74 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20  unt,.** and the 
a2b40 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e  current statemen
a2b50 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 28  t change count (
a2b60 69 2e 65 2e 20 63 68 61 6e 67 65 73 20 73 69 6e  i.e. changes sin
a2b70 63 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e  ce last statemen
a2b80 74 29 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 65  t)..** The curre
a2b90 6e 74 20 6b 65 79 6c 69 73 74 20 69 73 20 61 6c  nt keylist is al
a2ba0 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  so stored in the
a2bb0 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 20 45 6c 65   context..** Ele
a2bc0 6d 65 6e 74 73 20 6f 66 20 43 6f 6e 74 65 78 74  ments of Context
a2bd0 20 73 74 72 75 63 74 75 72 65 20 74 79 70 65 20   structure type 
a2be0 6d 61 6b 65 20 75 70 20 74 68 65 20 43 6f 6e 74  make up the Cont
a2bf0 65 78 74 53 74 61 63 6b 2c 20 77 68 69 63 68 20  extStack, which 
a2c00 69 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 62 79  is.** updated by
a2c10 20 74 68 65 20 43 6f 6e 74 65 78 74 50 75 73 68   the ContextPush
a2c20 20 61 6e 64 20 43 6f 6e 74 65 78 74 50 6f 70 20   and ContextPop 
a2c30 6f 70 63 6f 64 65 73 20 28 75 73 65 64 20 62 79  opcodes (used by
a2c40 20 74 72 69 67 67 65 72 73 29 2e 0a 2a 2a 20 54   triggers)..** T
a2c50 68 65 20 63 6f 6e 74 65 78 74 20 69 73 20 70 75  he context is pu
a2c60 73 68 65 64 20 62 65 66 6f 72 65 20 65 78 65 63  shed before exec
a2c70 75 74 69 6e 67 20 61 20 74 72 69 67 67 65 72 20  uting a trigger 
a2c80 61 20 70 6f 70 70 65 64 20 77 68 65 6e 20 74 68  a popped when th
a2c90 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69 6e  e.** trigger fin
a2ca0 69 73 68 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  ishes..*/.typede
a2cb0 66 20 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74  f struct Context
a2cc0 20 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74   Context;.struct
a2cd0 20 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 36 34   Context {.  i64
a2ce0 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 2f   lastRowid;    /
a2cf0 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  * Last insert ro
a2d00 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73  wid (sqlite3.las
a2d10 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74  tRowid) */.  int
a2d20 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f   nChange;      /
a2d30 2a 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e  * Statement chan
a2d40 67 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67  ges (Vdbe.nChang
a2d50 65 73 29 20 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f  es)     */.};../
a2d60 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
a2d70 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
a2d80 6d 61 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73  machine.  This s
a2d90 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
a2da0 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a  s the complete.*
a2db0 2a 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 76  * state of the v
a2dc0 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a  irtual machine..
a2dd0 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74  **.** The "sqlit
a2de0 65 33 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75  e3_stmt" structu
a2df0 72 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20  re pointer that 
a2e00 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  is returned by s
a2e10 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29  qlite3_compile()
a2e20 0a 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20  .** is really a 
a2e30 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e  pointer to an in
a2e40 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
a2e50 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
a2e60 54 68 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d  The Vdbe.inVtabM
a2e70 65 74 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69  ethod variable i
a2e80 73 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72  s set to non-zer
a2e90 6f 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69  o for the durati
a2ea0 6f 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72  on of.** any vir
a2eb0 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f  tual table metho
a2ec0 64 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61  d invocations ma
a2ed0 64 65 20 62 79 20 74 68 65 20 76 64 62 65 20 70  de by the vdbe p
a2ee0 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a  rogram. It is.**
a2ef0 20 73 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44   set to 2 for xD
a2f00 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61  estroy method ca
a2f10 6c 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c  lls and 1 for al
a2f20 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e  l other methods.
a2f30 20 54 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c   This.** variabl
a2f40 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 77  e is used for tw
a2f50 6f 20 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61  o purposes: to a
a2f60 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65  llow xDestroy me
a2f70 74 68 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65  thods to execute
a2f80 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22  .** "DROP TABLE"
a2f90 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20   statements and 
a2fa0 74 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20  to prevent some 
a2fb0 6e 61 73 74 79 20 73 69 64 65 20 65 66 66 65 63  nasty side effec
a2fc0 74 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20  ts of.** malloc 
a2fd0 66 61 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c  failure when SQL
a2fe0 69 74 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72  ite is invoked r
a2ff0 65 63 75 72 73 69 76 65 6c 79 20 62 79 20 61 20  ecursively by a 
a3000 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a  virtual table .*
a3010 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f  * method functio
a3020 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62  n..*/.struct Vdb
a3030 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e {.  sqlite3 *d
a3040 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b;        /* The
a3050 20 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20   whole database 
a3060 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 76  */.  Vdbe *pPrev
a3070 2c 2a 70 4e 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b  ,*pNext; /* Link
a3080 65 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73  ed list of VDBEs
a3090 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56   with the same V
a30a0 64 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20  dbe.db */.  int 
a30b0 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  nOp;            
a30c0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73  /* Number of ins
a30d0 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 68 65  tructions in the
a30e0 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e   program */.  in
a30f0 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20  t nOpAlloc;     
a3100 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
a3110 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  lots allocated f
a3120 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70  or aOp[] */.  Op
a3130 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20   *aOp;          
a3140 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f    /* Space to ho
a3150 6c 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  ld the virtual m
a3160 61 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61 6d  achine's program
a3170 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c   */.  int nLabel
a3180 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
a3190 62 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73  ber of labels us
a31a0 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62  ed */.  int nLab
a31b0 65 6c 41 6c 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e  elAlloc;    /* N
a31c0 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61  umber of slots a
a31d0 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c 61 62  llocated in aLab
a31e0 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  el[] */.  int *a
a31f0 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a  Label;        /*
a3200 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
a3210 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d  he labels */.  M
a3220 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20  em **apArg;     
a3230 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20     /* Arguments 
a3240 74 6f 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  to currently exe
a3250 63 75 74 69 6e 67 20 75 73 65 72 20 66 75 6e 63  cuting user func
a3260 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61  tion */.  Mem *a
a3270 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a  ColName;      /*
a3280 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f   Column names to
a3290 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74   return */.  int
a32a0 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20   nCursor;       
a32b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c   /* Number of sl
a32c0 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a  ots in apCsr[] *
a32d0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
a32e0 2a 61 70 43 73 72 3b 20 2f 2a 20 4f 6e 65 20 65  *apCsr; /* One e
a32f0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61  lement of this a
a3300 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70  rray for each op
a3310 65 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69  en cursor */.  i
a3320 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20 20  nt nVar;        
a3330 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
a3340 65 6e 74 72 69 65 73 20 69 6e 20 61 56 61 72 5b  entries in aVar[
a3350 5d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72  ] */.  Mem *aVar
a3360 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61  ;          /* Va
a3370 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f 50 5f  lues for the OP_
a3380 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e  Variable opcode.
a3390 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56   */.  char **azV
a33a0 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ar;       /* Nam
a33b0 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a  e of variables *
a33c0 2f 0a 20 20 69 6e 74 20 6f 6b 56 61 72 3b 20 20  /.  int okVar;  
a33d0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a33e0 69 66 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62  if azVar[] has b
a33f0 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  een initialized 
a3400 2a 2f 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20  */.  u32 magic; 
a3410 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a3420 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72  Magic number for
a3430 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   sanity checking
a3440 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20   */.  int nMem; 
a3450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a3460 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72   Number of memor
a3470 79 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72  y locations curr
a3480 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ently allocated 
a3490 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20  */.  Mem *aMem; 
a34a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a34b0 54 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  The memory locat
a34c0 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 63 61  ions */.  int ca
a34d0 63 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20  cheCtr;         
a34e0 20 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20    /* VdbeCursor 
a34f0 72 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61  row cache genera
a3500 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  tion counter */.
a3510 20 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74 61    int contextSta
a3520 63 6b 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e 64  ckTop;    /* Ind
a3530 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e  ex of top elemen
a3540 74 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74  t in the context
a3550 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   stack */.  int 
a3560 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74  contextStackDept
a3570 68 3b 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20  h;  /* The size 
a3580 6f 66 20 74 68 65 20 22 63 6f 6e 74 65 78 74 22  of the "context"
a3590 20 73 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74   stack */.  Cont
a35a0 65 78 74 20 2a 63 6f 6e 74 65 78 74 53 74 61 63  ext *contextStac
a35b0 6b 3b 20 20 2f 2a 20 53 74 61 63 6b 20 75 73 65  k;  /* Stack use
a35c0 64 20 62 79 20 6f 70 63 6f 64 65 73 20 43 6f 6e  d by opcodes Con
a35d0 74 65 78 74 50 75 73 68 20 26 20 43 6f 6e 74 65  textPush & Conte
a35e0 78 74 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70 63  xtPop*/.  int pc
a35f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a3600 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d    /* The program
a3610 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
a3620 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
a3630 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
a3640 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e  o return */.  in
a3650 74 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20  t errorAction;  
a3660 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72        /* Recover
a3670 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69  y action to do i
a3680 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72  n case of an err
a3690 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73  or */.  int nRes
a36a0 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
a36b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
a36c0 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f 77 20  umns in one row 
a36d0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
a36e0 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a  t */.  char **az
a36f0 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f  ResColumn;     /
a3700 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 6f 6e 65  * Values for one
a3710 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a   row of result *
a3720 2f 20 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d  / .  char *zErrM
a3730 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sg;          /* 
a3740 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  Error message wr
a3750 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
a3760 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65 74 3b  Mem *pResultSet;
a3770 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
a3780 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f  er to an array o
a3790 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 75  f results */.  u
a37a0 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20  8 explain;      
a37b0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a37c0 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e  f EXPLAIN presen
a37d0 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  t on SQL command
a37e0 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43   */.  u8 changeC
a37f0 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  ntOn;         /*
a3800 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20   True to update 
a3810 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
a3820 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72  er */.  u8 expir
a3830 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
a3840 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56  /* True if the V
a3850 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  M needs to be re
a3860 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38  compiled */.  u8
a3870 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72   minWriteFileFor
a3880 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d  mat;  /* Minimum
a3890 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72   file format for
a38a0 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61   writable databa
a38b0 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38  se files */.  u8
a38c0 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20   inVtabMethod;  
a38d0 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d        /* See com
a38e0 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20  ments above */. 
a38f0 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 72   u8 usesStmtJour
a3900 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  nal;     /* True
a3910 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 65   if uses a state
a3920 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  ment journal */.
a3930 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20    u8 readOnly;  
a3940 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
a3950 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20  e for read-only 
a3960 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  statements */.  
a3970 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b 20  u8 isPrepareV2; 
a3980 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a3990 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 68  if prepared with
a39a0 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a 2f   prepare_v2() */
a39b0 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20  .  int nChange; 
a39c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
a39d0 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67  mber of db chang
a39e0 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61  es made since la
a39f0 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 36  st reset */.  i6
a3a00 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20  4 startTime;    
a3a10 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68        /* Time wh
a3a20 65 6e 20 71 75 65 72 79 20 73 74 61 72 74 65 64  en query started
a3a30 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66   - used for prof
a3a40 69 6c 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62  iling */.  int b
a3a50 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 20 20 20  treeMask;       
a3a60 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
a3a70 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69   db->aDb[] entri
a3a80 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f  es referenced */
a3a90 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  .  BtreeMutexArr
a3aa0 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e  ay aMutex; /* An
a3ab0 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20   array of Btree 
a3ac0 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65  used here and ne
a3ad0 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20  eding locks */. 
a3ae0 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d   int aCounter[2]
a3af0 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
a3b00 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c  ters used by sql
a3b10 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
a3b20 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53  () */.  char *zS
a3b30 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ql;           /*
a3b40 20 54 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c   Text of the SQL
a3b50 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
a3b60 67 65 6e 65 72 61 74 65 64 20 74 68 69 73 20 2a  generated this *
a3b70 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 65 65 3b  /.  void *pFree;
a3b80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
a3b90 72 65 65 20 74 68 69 73 20 77 68 65 6e 20 64 65  ree this when de
a3ba0 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62 65 20  leting the vdbe 
a3bb0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
a3bc0 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74  _DEBUG.  FILE *t
a3bd0 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 2f  race;          /
a3be0 2a 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 75  * Write an execu
a3bf0 74 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 2c  tion trace here,
a3c00 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a   if not NULL */.
a3c10 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 53 74  #endif.  int iSt
a3c20 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20  atement;        
a3c30 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75   /* Statement nu
a3c40 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20 68 61  mber (or 0 if ha
a3c50 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d  s not opened stm
a3c60 74 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  t) */.#ifdef SQL
a3c70 49 54 45 5f 53 53 45 0a 20 20 69 6e 74 20 66 65  ITE_SSE.  int fe
a3c80 74 63 68 49 64 3b 20 20 20 20 20 20 20 20 20 20  tchId;          
a3c90 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d  /* Statement num
a3ca0 62 65 72 20 75 73 65 64 20 62 79 20 73 71 6c 69  ber used by sqli
a3cb0 74 65 33 5f 66 65 74 63 68 5f 73 74 61 74 65 6d  te3_fetch_statem
a3cc0 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 72 75  ent */.  int lru
a3cd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a3ce0 2a 20 43 6f 75 6e 74 65 72 20 75 73 65 64 20 66  * Counter used f
a3cf0 6f 72 20 4c 52 55 20 63 61 63 68 65 20 72 65 70  or LRU cache rep
a3d00 6c 61 63 65 6d 65 6e 74 20 2a 2f 0a 23 65 6e 64  lacement */.#end
a3d10 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
a3d20 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
a3d30 41 4e 41 47 45 4d 45 4e 54 0a 20 20 56 64 62 65  ANAGEMENT.  Vdbe
a3d40 20 2a 70 4c 72 75 50 72 65 76 3b 0a 20 20 56 64   *pLruPrev;.  Vd
a3d50 62 65 20 2a 70 4c 72 75 4e 65 78 74 3b 0a 23 65  be *pLruNext;.#e
a3d60 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  ndif.};../*.** T
a3d70 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
a3d80 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   allowed values 
a3d90 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a  for Vdbe.magic.*
a3da0 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d  /.#define VDBE_M
a3db0 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78  AGIC_INIT     0x
a3dc0 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42  26bceaa5    /* B
a3dd0 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70  uilding a VDBE p
a3de0 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e  rogram */.#defin
a3df0 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  e VDBE_MAGIC_RUN
a3e00 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33        0xbdf20da3
a3e10 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72      /* VDBE is r
a3e20 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20  eady to execute 
a3e30 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
a3e40 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30  MAGIC_HALT     0
a3e50 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20  x519c2973    /* 
a3e60 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74  VDBE has complet
a3e70 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a  ed execution */.
a3e80 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47  #define VDBE_MAG
a3e90 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36  IC_DEAD     0xb6
a3ea0 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65  06c3c8    /* The
a3eb0 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64   VDBE has been d
a3ec0 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f  eallocated */../
a3ed0 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72  *.** Function pr
a3ee0 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49  ototypes.*/.SQLI
a3ef0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a3f00 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
a3f10 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 64  ursor(Vdbe *, Vd
a3f20 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 64  beCursor*);.void
a3f30 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 74   sqliteVdbePopSt
a3f40 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a  ack(Vdbe*,int);.
a3f50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a3f60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  nt sqlite3VdbeCu
a3f70 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43  rsorMoveto(VdbeC
a3f80 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66  ursor*);.#if def
a3f90 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
a3fa0 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44  G) || defined(VD
a3fb0 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49  BE_PROFILE).SQLI
a3fc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a3fd0 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
a3fe0 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f  Op(FILE*, int, O
a3ff0 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  p*);.#endif.SQLI
a4000 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a4010 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
a4020 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51  TypeLen(u32);.SQ
a4030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
a4040 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
a4050 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74  alType(Mem*, int
a4060 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
a4070 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
a4080 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 67  eSerialPut(unsig
a4090 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20  ned char*, int, 
a40a0 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  Mem*, int);.SQLI
a40b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a40c0 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
a40d0 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  Get(const unsign
a40e0 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d  ed char*, u32, M
a40f0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
a4100 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a4110 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61  3VdbeDeleteAuxDa
a4120 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e  ta(VdbeFunc*, in
a4130 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32  t);..int sqlite2
a4140 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 28  BtreeKeyCompare(
a4150 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73  BtCursor *, cons
a4160 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69  t void *, int, i
a4170 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49  nt, int *);.SQLI
a4180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a4190 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79  qlite3VdbeIdxKey
a41a0 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 73  Compare(VdbeCurs
a41b0 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f  or*,UnpackedReco
a41c0 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  rd*,int*);.SQLIT
a41d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a41e0 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69  lite3VdbeIdxRowi
a41f0 64 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36  d(BtCursor *, i6
a4200 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  4 *);.SQLITE_PRI
a4210 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
a4220 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74  MemCompare(const
a4230 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d   Mem*, const Mem
a4240 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71  *, const CollSeq
a4250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
a4260 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
a4270 62 65 45 78 65 63 28 56 64 62 65 2a 29 3b 0a 53  beExec(Vdbe*);.S
a4280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a4290 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73  t sqlite3VdbeLis
a42a0 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  t(Vdbe*);.SQLITE
a42b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a42c0 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62  ite3VdbeHalt(Vdb
a42d0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
a42e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
a42f0 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
a4300 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53  g(Mem *, int);.S
a4310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a4320 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
a4330 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 3b 0a 53 51  TooBig(Mem*);.SQ
a4340 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
a4350 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43   sqlite3VdbeMemC
a4360 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20  opy(Mem*, const 
a4370 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
a4380 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a4390 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
a43a0 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74  Copy(Mem*, const
a43b0 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c   Mem*, int);.SQL
a43c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a43d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
a43e0 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b  ove(Mem*, Mem*);
a43f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a4400 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
a4410 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d  emNulTerminate(M
a4420 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
a4430 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
a4440 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 4d 65  VdbeMemSetStr(Me
a4450 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  m*, const char*,
a4460 20 69 6e 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a   int, u8, void(*
a4470 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
a4480 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a4490 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
a44a0 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29  Int64(Mem*, i64)
a44b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a44c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
a44d0 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65  eMemSetDouble(Me
a44e0 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c  m*, double);.SQL
a44f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a4500 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
a4510 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51  etNull(Mem*);.SQ
a4520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a4530 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
a4540 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a  SetZeroBlob(Mem*
a4550 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
a4560 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a4570 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53  e3VdbeMemSetRowS
a4580 65 74 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  et(Mem*);.SQLITE
a4590 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a45a0 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
a45b0 72 69 74 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a  riteable(Mem*);.
a45c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a45d0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
a45e0 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c  mStringify(Mem*,
a45f0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
a4600 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65  IVATE i64 sqlite
a4610 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65  3VdbeIntValue(Me
a4620 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
a4630 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
a4640 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
a4650 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
a4660 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71  RIVATE double sq
a4670 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
a4680 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ue(Mem*);.SQLITE
a4690 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a46a0 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
a46b0 41 66 66 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a  Affinity(Mem*);.
a46c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a46d0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
a46e0 6d 52 65 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a  mRealify(Mem*);.
a46f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a4700 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
a4710 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b  mNumerify(Mem*);
a4720 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a4730 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
a4740 65 6d 46 72 6f 6d 42 74 72 65 65 28 42 74 43 75  emFromBtree(BtCu
a4750 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e  rsor*,int,int,in
a4760 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  t,Mem*);.SQLITE_
a4770 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a4780 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
a4790 73 65 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49  se(Mem *p);.SQLI
a47a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a47b0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
a47c0 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65  leaseExternal(Me
a47d0 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52  m *p);.SQLITE_PR
a47e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a47f0 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65  3VdbeMemFinalize
a4800 28 4d 65 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29  (Mem*, FuncDef*)
a4810 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a4820 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
a4830 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69  ite3OpcodeName(i
a4840 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
a4850 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
a4860 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70  dbeOpcodeHasProp
a4870 65 72 74 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  erty(int, int);.
a4880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a4890 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
a48a0 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c  mGrow(Mem *pMem,
a48b0 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73   int n, int pres
a48c0 65 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  erve);.SQLITE_PR
a48d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a48e0 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d  3VdbeCloseStatem
a48f0 65 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29  ent(Vdbe *, int)
a4900 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
a4910 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
a4920 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f  NAGEMENT.SQLITE_
a4930 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a4940 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75  te3VdbeReleaseBu
a4950 66 66 65 72 73 28 56 64 62 65 20 2a 70 29 3b 0a  ffers(Vdbe *p);.
a4960 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
a4970 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a4980 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
a4990 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69  te(Mem*, u8);.#i
a49a0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
a49b0 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
a49c0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56     void sqlite3V
a49d0 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65  dbePrintSql(Vdbe
a49e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
a49f0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
a4a00 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72  3VdbeMemPrettyPr
a4a10 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63  int(Mem *pMem, c
a4a20 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64  har *zBuf);.#end
a4a30 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
a4a40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
a4a50 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65  eMemHandleBom(Me
a4a60 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64  m *pMem);..#ifnd
a4a70 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
a4a80 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50  NCRBLOB.SQLITE_P
a4a90 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
a4aa0 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e  ite3VdbeMemExpan
a4ab0 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65  dBlob(Mem *);.#e
a4ac0 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
a4ad0 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
a4ae0 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 45  ndBlob(x) SQLITE
a4af0 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  _OK.#endif..#end
a4b00 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
a4b10 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a  VDBEINT_H_) */..
a4b20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a4b30 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68  End of vdbeInt.h
a4b40 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
a4b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a4b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a4b70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a4b80 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
a4b90 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
a4ba0 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  utf.c **********
a4bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a4bc0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a4bd0 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a  AMALGAMATION./*.
a4be0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
a4bf0 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 20   constant value 
a4c00 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 53  is used by the S
a4c10 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
a4c20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49  and.** SQLITE_LI
a4c30 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f  TTLEENDIAN macro
a4c40 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
a4c50 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73  VATE const int s
a4c60 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23  qlite3one = 1;.#
a4c70 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
a4c80 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a  AMALGAMATION */.
a4c90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b  ./*.** This look
a4ca0 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 64  up table is used
a4cb0 20 74 6f 20 68 65 6c 70 20 64 65 63 6f 64 65 20   to help decode 
a4cc0 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f  the first byte o
a4cd0 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74  f.** a multi-byt
a4ce0 65 20 55 54 46 38 20 63 68 61 72 61 63 74 65 72  e UTF8 character
a4cf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
a4d00 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
a4d10 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73  sqlite3Utf8Trans
a4d20 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20  1[] = {.  0x00, 
a4d30 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
a4d40 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78  , 0x04, 0x05, 0x
a4d50 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38  06, 0x07,.  0x08
a4d60 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78  , 0x09, 0x0a, 0x
a4d70 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20  0b, 0x0c, 0x0d, 
a4d80 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78  0x0e, 0x0f,.  0x
a4d90 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32 2c 20  10, 0x11, 0x12, 
a4da0 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78 31 35  0x13, 0x14, 0x15
a4db0 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 20 20  , 0x16, 0x17,.  
a4dc0 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78 31 61  0x18, 0x19, 0x1a
a4dd0 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20 30 78  , 0x1b, 0x1c, 0x
a4de0 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66 2c 0a  1d, 0x1e, 0x1f,.
a4df0 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78    0x00, 0x01, 0x
a4e00 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20  02, 0x03, 0x04, 
a4e10 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37  0x05, 0x06, 0x07
a4e20 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20  ,.  0x08, 0x09, 
a4e30 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63  0x0a, 0x0b, 0x0c
a4e40 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78  , 0x0d, 0x0e, 0x
a4e50 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31  0f,.  0x00, 0x01
a4e60 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78  , 0x02, 0x03, 0x
a4e70 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20  04, 0x05, 0x06, 
a4e80 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 30 78  0x07,.  0x00, 0x
a4e90 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20  01, 0x02, 0x03, 
a4ea0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30  0x00, 0x01, 0x00
a4eb0 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65  , 0x00,.};...#de
a4ec0 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 38 28  fine WRITE_UTF8(
a4ed0 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20  zOut, c) {      
a4ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4ef0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 30 78      \.  if( c<0x
a4f00 30 30 30 38 30 20 29 7b 20 20 20 20 20 20 20 20  00080 ){        
a4f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4f20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a4f30 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
a4f40 29 28 63 26 30 78 46 46 29 3b 20 20 20 20 20 20  )(c&0xFF);      
a4f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4f60 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
a4f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a4fa0 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78  .  else if( c<0x
a4fb0 30 30 38 30 30 20 29 7b 20 20 20 20 20 20 20 20  00800 ){        
a4fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4fd0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
a4fe0 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28  Out++ = 0xC0 + (
a4ff0 75 38 29 28 28 63 3e 3e 36 29 26 30 78 31 46 29  u8)((c>>6)&0x1F)
a5000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a5010 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
a5020 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26   0x80 + (u8)(c &
a5030 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20   0x3F);         
a5040 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20            \.  } 
a5050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5080 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20     \.  else if( 
a5090 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20 20 20  c<0x10000 ){    
a50a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a50b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a50c0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30    *zOut++ = 0xE0
a50d0 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 26   + (u8)((c>>12)&
a50e0 30 78 30 46 29 3b 20 20 20 20 20 20 20 20 20 20  0x0F);          
a50f0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
a5100 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29  ++ = 0x80 + (u8)
a5110 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b  ((c>>6) & 0x3F);
a5120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a5130 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
a5140 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78  80 + (u8)(c & 0x
a5150 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
a5160 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65         \.  }else
a5170 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
a5180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a51a0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
a51b0 30 78 46 30 20 2b 20 28 75 38 29 28 28 63 3e 3e  0xF0 + (u8)((c>>
a51c0 31 38 29 20 26 20 30 78 30 37 29 3b 20 20 20 20  18) & 0x07);    
a51d0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
a51e0 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
a51f0 28 75 38 29 28 28 63 3e 3e 31 32 29 20 26 20 30  (u8)((c>>12) & 0
a5200 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
a5210 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
a5220 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63  = 0x80 + (u8)((c
a5230 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 20  >>6) & 0x3F);   
a5240 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a5250 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20   *zOut++ = 0x80 
a5260 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29  + (u8)(c & 0x3F)
a5270 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a5280 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
a5290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a52a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a52b0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
a52c0 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f  ..#define WRITE_
a52d0 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29  UTF16LE(zOut, c)
a52e0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
a52f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5300 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c        \.  if( c<
a5310 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20  =0xFFFF ){      
a5320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5340 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a5350 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
a5360 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  (c&0x00FF);     
a5370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5390 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
a53a0 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78  = (u8)((c>>8)&0x
a53b0 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  00FF);          
a53c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a53d0 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73          \.  }els
a53e0 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e{              
a53f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a5420 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
a5430 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30  8)(((c>>10)&0x00
a5440 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30  3F) + (((c-0x100
a5450 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29  00)>>10)&0x00C0)
a5460 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b  );  \.    *zOut+
a5470 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 20  + = (u8)(0x00D8 
a5480 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e  + (((c-0x10000)>
a5490 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 20  >18)&0x03));    
a54a0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
a54b0 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63  *zOut++ = (u8)(c
a54c0 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20  &0x00FF);       
a54d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a54e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a54f0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
a5500 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28 28  (u8)(0x00DC + ((
a5510 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20  c>>8)&0x03));   
a5520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5530 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
a5540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5570 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
a5580 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55  .#define WRITE_U
a5590 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20  TF16BE(zOut, c) 
a55a0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
a55b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a55c0 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d       \.  if( c<=
a55d0 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20  0xFFFF ){       
a55e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a55f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5600 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a5610 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
a5620 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20  (c>>8)&0x00FF); 
a5630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5650 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
a5660 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b   (u8)(c&0x00FF);
a5670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5690 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65         \.  }else
a56a0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
a56b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a56c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a56d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a56e0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
a56f0 29 28 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d  )(0x00D8 + (((c-
a5700 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78  0x10000)>>18)&0x
a5710 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20  03));           
a5720 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
a5730 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29   = (u8)(((c>>10)
a5740 26 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d  &0x003F) + (((c-
a5750 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78  0x10000)>>10)&0x
a5760 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a  00C0));  \.    *
a5770 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78  zOut++ = (u8)(0x
a5780 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30  00DC + ((c>>8)&0
a5790 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20  x03));          
a57a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a57b0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
a57c0 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20  u8)(c&0x00FF);  
a57d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a57e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a57f0 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
a5800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5830 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a             \.}..
a5840 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46  #define READ_UTF
a5850 31 36 4c 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20  16LE(zIn, c){   
a5860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5880 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a        \.  c = (*
a5890 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20  zIn++);         
a58a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a58b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a58c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a58d0 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29    c += ((*zIn++)
a58e0 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20  <<8);           
a58f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5910 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e        \.  if( c>
a5920 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 45  =0xD800 && c<0xE
a5930 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  000 ){          
a5940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5950 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a5960 20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49     int c2 = (*zI
a5970 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
a5980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a59a0 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d       \.    c2 +=
a59b0 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20   ((*zIn++)<<8); 
a59c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a59d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a59e0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a59f0 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46     c = (c2&0x03F
a5a00 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29  F) + ((c&0x003F)
a5a10 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30  <<10) + (((c&0x0
a5a20 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30  3C0)+0x0040)<<10
a5a30 29 3b 20 20 20 5c 0a 20 20 20 20 69 66 28 20 28  );   \.    if( (
a5a40 63 20 26 20 30 78 46 46 46 46 30 30 30 30 29 3d  c & 0xFFFF0000)=
a5a50 3d 30 20 29 20 63 20 3d 20 30 78 46 46 46 44 3b  =0 ) c = 0xFFFD;
a5a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5a70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a5a80 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
a5a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5ac0 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e       \.}..#defin
a5ad0 65 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a  e READ_UTF16BE(z
a5ae0 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20  In, c){         
a5af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b10 5c 0a 20 20 63 20 3d 20 28 28 2a 7a 49 6e 2b 2b  \.  c = ((*zIn++
a5b20 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20  )<<8);          
a5b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b50 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d          \.  c +=
a5b60 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20   (*zIn++);      
a5b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5ba0 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30  \.  if( c>=0xD80
a5bb0 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 29 7b  0 && c<0xE000 ){
a5bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5be0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74         \.    int
a5bf0 20 63 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c   c2 = ((*zIn++)<
a5c00 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  <8);            
a5c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a5c30 0a 20 20 20 20 63 32 20 2b 3d 20 28 2a 7a 49 6e  .    c2 += (*zIn
a5c40 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  ++);            
a5c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5c70 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d         \.    c =
a5c80 20 28 63 32 26 30 78 30 33 46 46 29 20 2b 20 28   (c2&0x03FF) + (
a5c90 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20  (c&0x003F)<<10) 
a5ca0 2b 20 28 28 28 63 26 30 78 30 33 43 30 29 2b 30  + (((c&0x03C0)+0
a5cb0 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c  x0040)<<10);   \
a5cc0 0a 20 20 20 20 69 66 28 20 28 63 20 26 20 30 78  .    if( (c & 0x
a5cd0 46 46 46 46 30 30 30 30 29 3d 3d 30 20 29 20 63  FFFF0000)==0 ) c
a5ce0 20 3d 20 30 78 46 46 46 44 3b 20 20 20 20 20 20   = 0xFFFD;      
a5cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5d00 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20         \.  }    
a5d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a5d50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c  .}../*.** Transl
a5d60 61 74 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46  ate a single UTF
a5d70 2d 38 20 63 68 61 72 61 63 74 65 72 2e 20 20 52  -8 character.  R
a5d80 65 74 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64  eturn the unicod
a5d90 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44  e value..**.** D
a5da0 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f  uring translatio
a5db0 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74  n, assume that t
a5dc0 68 65 20 62 79 74 65 20 74 68 61 74 20 7a 54 65  he byte that zTe
a5dd0 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20  rm points.** is 
a5de0 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72  a 0x00..**.** Wr
a5df0 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ite a pointer to
a5e00 20 74 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64   the next unread
a5e10 20 62 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20   byte back into 
a5e20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e  *pzNext..**.** N
a5e30 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20  otes On Invalid 
a5e40 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20  UTF-8:.**.**  * 
a5e50 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65   This routine ne
a5e60 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62  ver allows a 7-b
a5e70 69 74 20 63 68 61 72 61 63 74 65 72 20 28 30 78  it character (0x
a5e80 30 30 20 74 68 72 6f 75 67 68 20 30 78 37 66 29  00 through 0x7f)
a5e90 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e   to.**     be en
a5ea0 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69  coded as a multi
a5eb0 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 2e  -byte character.
a5ec0 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65    Any multi-byte
a5ed0 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 0a   character that.
a5ee0 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 74 73 20  **     attempts 
a5ef0 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75  to encode a valu
a5f00 65 20 62 65 74 77 65 65 6e 20 30 78 30 30 20 61  e between 0x00 a
a5f10 6e 64 20 30 78 37 66 20 69 73 20 72 65 6e 64 65  nd 0x7f is rende
a5f20 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a  red as 0xfffd..*
a5f30 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f  *.**  *  This ro
a5f40 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f  utine never allo
a5f50 77 73 20 61 20 55 54 46 31 36 20 73 75 72 72 6f  ws a UTF16 surro
a5f60 67 61 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65  gate value to be
a5f70 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20   encoded..**    
a5f80 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65   If a multi-byte
a5f90 20 63 68 61 72 61 63 74 65 72 20 61 74 74 65 6d   character attem
a5fa0 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20  pts to encode a 
a5fb0 76 61 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a  value between.**
a5fc0 20 20 20 20 20 30 78 64 38 30 30 20 61 6e 64 20       0xd800 and 
a5fd0 30 78 65 30 30 30 20 74 68 65 6e 20 69 74 20 69  0xe000 then it i
a5fe0 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 78  s rendered as 0x
a5ff0 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20  fffd..**.**  *  
a6000 42 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e  Bytes in the ran
a6010 67 65 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75  ge of 0x80 throu
a6020 67 68 20 30 78 62 66 20 77 68 69 63 68 20 6f 63  gh 0xbf which oc
a6030 63 75 72 20 61 73 20 74 68 65 20 66 69 72 73 74  cur as the first
a6040 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20  .**     byte of 
a6050 61 20 63 68 61 72 61 63 74 65 72 20 61 72 65 20  a character are 
a6060 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 73  interpreted as s
a6070 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61  ingle-byte chara
a6080 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64  cters.**     and
a6090 20 72 65 6e 64 65 72 65 64 20 61 73 20 74 68 65   rendered as the
a60a0 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f  mselves even tho
a60b0 75 67 68 20 74 68 65 79 20 61 72 65 20 74 65 63  ugh they are tec
a60c0 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20  hnically.**     
a60d0 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 65  invalid characte
a60e0 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68  rs..**.**  *  Th
a60f0 69 73 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70  is routine accep
a6100 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e  ts an infinite n
a6110 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65  umber of differe
a6120 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67  nt UTF8 encoding
a6130 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69  s.**     for uni
a6140 63 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38 30  code values 0x80
a6150 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49   and greater.  I
a6160 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20  t do not change 
a6170 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20  over-length.**  
a6180 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20     encodings to 
a6190 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73  0xfffd as some s
a61a0 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64  ystems recommend
a61b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41  ..*/.#define REA
a61c0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72  D_UTF8(zIn, zTer
a61d0 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20 20 20  m, c)           
a61e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a61f0 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29  \.  c = *(zIn++)
a6200 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a6210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6220 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a6230 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 20   if( c>=0xc0 ){ 
a6240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6260 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
a6270 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54  c = sqlite3Utf8T
a6280 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20  rans1[c-0xc0];  
a6290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a62a0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69         \.    whi
a62b0 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26  le( zIn!=zTerm &
a62c0 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d  & (*zIn & 0xc0)=
a62d0 3d 30 78 38 30 20 29 7b 20 20 20 20 20 20 20 20  =0x80 ){        
a62e0 20 20 20 20 5c 0a 20 20 20 20 20 20 63 20 3d 20      \.      c = 
a62f0 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26  (c<<6) + (0x3f &
a6300 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20   *(zIn++));     
a6310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6320 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20   \.    }        
a6330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a6360 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 20 20      if( c<0x80  
a6370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6390 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a63a0 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46       || (c&0xFFF
a63b0 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30 20 20  FF800)==0xD800  
a63c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a63d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20          \.      
a63e0 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46    || (c&0xFFFFFF
a63f0 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20  FE)==0xFFFE ){  
a6400 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 20  c = 0xFFFD; }   
a6410 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54       \.  }.SQLIT
a6420 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a6430 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 0a 20  lite3Utf8Read(. 
a6440 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
a6450 63 68 61 72 20 2a 7a 2c 20 20 20 20 20 20 20 20  char *z,        
a6460 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f   /* First byte o
a6470 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  f UTF-8 characte
a6480 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73  r */.  const uns
a6490 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72  igned char *zTer
a64a0 6d 2c 20 20 20 20 20 2f 2a 20 50 72 65 74 65 6e  m,     /* Preten
a64b0 64 20 74 68 69 73 20 62 79 74 65 20 69 73 20 30  d this byte is 0
a64c0 78 30 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75  x00 */.  const u
a64d0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 70  nsigned char **p
a64e0 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 69 74  zNext    /* Writ
a64f0 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73  e first byte pas
a6500 74 20 55 54 46 2d 38 20 63 68 61 72 20 68 65 72  t UTF-8 char her
a6510 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b  e */.){.  int c;
a6520 0a 20 20 52 45 41 44 5f 55 54 46 38 28 7a 2c 20  .  READ_UTF8(z, 
a6530 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 2a 70 7a  zTerm, c);.  *pz
a6540 4e 65 78 74 20 3d 20 7a 3b 0a 20 20 72 65 74 75  Next = z;.  retu
a6550 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a 2a  rn c;.}...../*.*
a6560 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 4c 41  * If the TRANSLA
a6570 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f 20 69  TE_TRACE macro i
a6580 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 20 76  s defined, the v
a6590 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d 65 6d  alue of each Mem
a65a0 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 20 6f   is.** printed o
a65b0 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 65 20  n stderr on the 
a65c0 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f 75 74  way into and out
a65d0 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 4d   of sqlite3VdbeM
a65e0 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e 0a 2a  emTranslate()..*
a65f0 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 54 52  / ./* #define TR
a6600 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 31 20  ANSLATE_TRACE 1 
a6610 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
a6620 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
a6630 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
a6640 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 65 20   transforms the 
a6650 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 65 6e  internal text en
a6660 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 70  coding used by p
a6670 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 72 65  Mem to.** desire
a6680 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e 20 65  dEnc. It is an e
a6690 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 72 69  rror if the stri
a66a0 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 6f 66  ng is already of
a66b0 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20   the desired.** 
a66c0 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 66 20  encoding, or if 
a66d0 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 20 63  *pMem does not c
a66e0 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e 67 20  ontain a string 
a66f0 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  value..*/.SQLITE
a6700 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a6710 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73  ite3VdbeMemTrans
a6720 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  late(Mem *pMem, 
a6730 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a  u8 desiredEnc){.
a6740 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20    int len;      
a6750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a6760 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   Maximum length 
a6770 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67  of output string
a6780 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75   in bytes */.  u
a6790 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f  nsigned char *zO
a67a0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
a67b0 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62       /* Output b
a67c0 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67  uffer */.  unsig
a67d0 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20  ned char *zIn;  
a67e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a67f0 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 61 74   /* Input iterat
a6800 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  or */.  unsigned
a6810 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20   char *zTerm;   
a6820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a6830 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a 2f   End of input */
a6840 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
a6850 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
a6860 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
a6870 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20  ut iterator */. 
a6880 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 3b   unsigned int c;
a6890 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ..  assert( pMem
a68a0 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
a68b0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
a68c0 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
a68d0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ;.  assert( pMem
a68e0 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20  ->flags&MEM_Str 
a68f0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
a6900 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45  m->enc!=desiredE
a6910 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  nc );.  assert( 
a6920 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b 0a  pMem->enc!=0 );.
a6930 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
a6940 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 65 66  n>=0 );..#if def
a6950 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 5f 54  ined(TRANSLATE_T
a6960 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 64  RACE) && defined
a6970 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 20  (SQLITE_DEBUG). 
a6980 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66   {.    char zBuf
a6990 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74  [100];.    sqlit
a69a0 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
a69b0 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29  rint(pMem, zBuf)
a69c0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74  ;.    fprintf(st
a69d0 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20 25  derr, "INPUT:  %
a69e0 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d  s\n", zBuf);.  }
a69f0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
a6a00 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e   the translation
a6a10 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d   is between UTF-
a6a20 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 62 69  16 little and bi
a6a30 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e 20 0a  g endian, then .
a6a40 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 73    ** all that is
a6a50 20 72 65 71 75 69 72 65 64 20 69 73 20 74 6f 20   required is to 
a6a60 73 77 61 70 20 74 68 65 20 62 79 74 65 20 6f 72  swap the byte or
a6a70 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 20 69  der. This case i
a6a80 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a 20 64  s handled.  ** d
a6a90 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20  ifferently from 
a6aa0 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 2a 2f  the others..  */
a6ab0 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63  .  if( pMem->enc
a6ac0 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26  !=SQLITE_UTF8 &&
a6ad0 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 51 4c   desiredEnc!=SQL
a6ae0 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20  ITE_UTF8 ){.    
a6af0 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 6e 74  u8 temp;.    int
a6b00 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71   rc;.    rc = sq
a6b10 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
a6b20 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b  Writeable(pMem);
a6b30 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
a6b40 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
a6b50 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
a6b60 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
a6b70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a6b80 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
a6b90 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d   zIn = (u8*)pMem
a6ba0 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d 20 3d  ->z;.    zTerm =
a6bb0 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e 31   &zIn[pMem->n&~1
a6bc0 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49  ];.    while( zI
a6bd0 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20  n<zTerm ){.     
a6be0 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20 20   temp = *zIn;.  
a6bf0 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49 6e      *zIn = *(zIn
a6c00 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b 2b  +1);.      zIn++
a6c10 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20 3d  ;.      *zIn++ =
a6c20 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20   temp;.    }.   
a6c30 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73   pMem->enc = des
a6c40 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f 74  iredEnc;.    got
a6c50 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3b  o translate_out;
a6c60 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 6c  .  }..  /* Set l
a6c70 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  en to the maximu
a6c80 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  m number of byte
a6c90 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68  s required in th
a6ca0 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e  e output buffer.
a6cb0 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72 65   */.  if( desire
a6cc0 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
a6cd0 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e  8 ){.    /* When
a6ce0 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d   converting from
a6cf0 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61 78   UTF-16, the max
a6d00 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73 75  imum growth resu
a6d10 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20  lts from.    ** 
a6d20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32 2d  translating a 2-
a6d30 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74  byte character t
a6d40 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d 38  o a 4-byte UTF-8
a6d50 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20   character..    
a6d60 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74 65  ** A single byte
a6d70 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
a6d80 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72 69   the output stri
a6d90 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65  ng.    ** nul-te
a6da0 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f  rminator..    */
a6db0 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d 20  .    pMem->n &= 
a6dc0 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d  ~1;.    len = pM
a6dd0 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a 20  em->n * 2 + 1;. 
a6de0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57   }else{.    /* W
a6df0 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66  hen converting f
a6e00 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46  rom UTF-8 to UTF
a6e10 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  -16 the maximum 
a6e20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65 64  growth is caused
a6e30 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20 31  .    ** when a 1
a6e40 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61 72  -byte UTF-8 char
a6e50 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c 61  acter is transla
a6e60 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79 74  ted into a 2-byt
a6e70 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a 20  e UTF-16.    ** 
a6e80 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20 62  character. Two b
a6e90 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72 65  ytes are require
a6ea0 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20  d in the output 
a6eb0 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a 20  buffer for the. 
a6ec0 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e     ** nul-termin
a6ed0 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ator..    */.   
a6ee0 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a   len = pMem->n *
a6ef0 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f   2 + 2;.  }..  /
a6f00 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69  * Set zIn to poi
a6f10 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20  nt at the start 
a6f20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 66  of the input buf
a6f30 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f  fer and zTerm to
a6f40 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62 79   point 1.  ** by
a6f50 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 2e  te past the end.
a6f60 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61  .  **.  ** Varia
a6f70 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74 20  ble zOut is set 
a6f80 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
a6f90 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 73  output buffer, s
a6fa0 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20 20  pace obtained.  
a6fb0 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ** from sqlite3_
a6fc0 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20  malloc()..  */. 
a6fd0 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d   zIn = (u8*)pMem
a6fe0 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26  ->z;.  zTerm = &
a6ff0 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20  zIn[pMem->n];.  
a7000 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44 62  zOut = sqlite3Db
a7010 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e  MallocRaw(pMem->
a7020 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28 20  db, len);.  if( 
a7030 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65 74  !zOut ){.    ret
a7040 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
a7050 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74  ;.  }.  z = zOut
a7060 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65  ;..  if( pMem->e
a7070 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
a7080 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69 72  ){.    if( desir
a7090 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
a70a0 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f  F16LE ){.      /
a70b0 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31  * UTF-8 -> UTF-1
a70c0 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20  6 Little-endian 
a70d0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
a70e0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
a70f0 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69       /* c = sqli
a7100 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c  te3Utf8Read(zIn,
a7110 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75   zTerm, (const u
a7120 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20  8**)&zIn); */.  
a7130 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28        READ_UTF8(
a7140 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a  zIn, zTerm, c);.
a7150 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
a7160 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16LE(z, c);.   
a7170 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
a7180 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 65        assert( de
a7190 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
a71a0 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20 20  _UTF16BE );.    
a71b0 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54    /* UTF-8 -> UT
a71c0 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20  F-16 Big-endian 
a71d0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
a71e0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
a71f0 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69       /* c = sqli
a7200 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c  te3Utf8Read(zIn,
a7210 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75   zTerm, (const u
a7220 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20  8**)&zIn); */.  
a7230 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28        READ_UTF8(
a7240 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a  zIn, zTerm, c);.
a7250 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
a7260 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16BE(z, c);.   
a7270 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
a7280 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a  Mem->n = (int)(z
a7290 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2a 7a   - zOut);.    *z
a72a0 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ++ = 0;.  }else{
a72b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65 73  .    assert( des
a72c0 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
a72d0 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28 20  UTF8 );.    if( 
a72e0 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  pMem->enc==SQLIT
a72f0 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20  E_UTF16LE ){.   
a7300 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 74     /* UTF-16 Lit
a7310 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54  tle-endian -> UT
a7320 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  F-8 */.      whi
a7330 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b  le( zIn<zTerm ){
a7340 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54  .        READ_UT
a7350 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b 20 0a  F16LE(zIn, c); .
a7360 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
a7370 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  F8(z, c);.      
a7380 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
a7390 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69 67     /* UTF-16 Big
a73a0 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38  -endian -> UTF-8
a73b0 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
a73c0 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
a73d0 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36        READ_UTF16
a73e0 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20 20  BE(zIn, c); .   
a73f0 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 28       WRITE_UTF8(
a7400 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20  z, c);.      }. 
a7410 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e     }.    pMem->n
a7420 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75   = (int)(z - zOu
a7430 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30  t);.  }.  *z = 0
a7440 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  ;.  assert( (pMe
a7450 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45 6e 63  m->n+(desiredEnc
a7460 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a  ==SQLITE_UTF8?1:
a7470 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73  2))<=len );..  s
a7480 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
a7490 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d  ease(pMem);.  pM
a74a0 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  em->flags &= ~(M
a74b0 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79  EM_Static|MEM_Dy
a74c0 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20  n|MEM_Ephem);.  
a74d0 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69  pMem->enc = desi
a74e0 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e  redEnc;.  pMem->
a74f0 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65  flags |= (MEM_Te
a7500 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 70  rm|MEM_Dyn);.  p
a7510 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29  Mem->z = (char*)
a7520 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d  zOut;.  pMem->zM
a7530 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b  alloc = pMem->z;
a7540 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a  ..translate_out:
a7550 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41  .#if defined(TRA
a7560 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26  NSLATE_TRACE) &&
a7570 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
a7580 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63  DEBUG).  {.    c
a7590 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20  har zBuf[100];. 
a75a0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
a75b0 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65  mPrettyPrint(pMe
a75c0 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70  m, zBuf);.    fp
a75d0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 4f  rintf(stderr, "O
a75e0 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42  UTPUT: %s\n", zB
a75f0 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  uf);.  }.#endif.
a7600 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a7610 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
a7620 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
a7630 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72 64 65   for a byte-orde
a7640 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20 62 65  r mark at the be
a7650 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a  ginning of the .
a7660 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  ** UTF-16 string
a7670 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d   stored in *pMem
a7680 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72 65 73  . If one is pres
a7690 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d 6f 76  ent, it is remov
a76a0 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e  ed and.** the en
a76b0 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65  coding of the Me
a76c0 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68 69 73  m adjusted. This
a76d0 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f   routine does no
a76e0 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65  t do any.** byte
a76f0 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 6a 75  -swapping, it ju
a7700 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20  st sets Mem.enc 
a7710 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a  appropriately..*
a7720 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74  *.** The allocat
a7730 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64 79 6e  ion (static, dyn
a7740 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64 20 65  amic etc.) and e
a7750 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d  ncoding of the M
a7760 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61  em may be.** cha
a7770 6e 67 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  nged by this fun
a7780 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
a7790 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a77a0 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c  ite3VdbeMemHandl
a77b0 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  eBom(Mem *pMem){
a77c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
a77d0 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20  TE_OK;.  u8 bom 
a77e0 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d  = 0;..  if( pMem
a77f0 2d 3e 6e 3c 30 20 7c 7c 20 70 4d 65 6d 2d 3e 6e  ->n<0 || pMem->n
a7800 3e 31 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20  >1 ){.    u8 b1 
a7810 3d 20 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a  = *(u8 *)pMem->z
a7820 3b 0a 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28  ;.    u8 b2 = *(
a7830 28 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20  ((u8 *)pMem->z) 
a7840 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31  + 1);.    if( b1
a7850 3d 3d 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78  ==0xFE && b2==0x
a7860 46 46 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20  FF ){.      bom 
a7870 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  = SQLITE_UTF16BE
a7880 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
a7890 62 31 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d  b1==0xFF && b2==
a78a0 30 78 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f  0xFE ){.      bo
a78b0 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  m = SQLITE_UTF16
a78c0 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  LE;.    }.  }.  
a78d0 0a 20 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20  .  if( bom ){.  
a78e0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
a78f0 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
a7900 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66  le(pMem);.    if
a7910 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
a7920 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e  ){.      pMem->n
a7930 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d   -= 2;.      mem
a7940 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70  move(pMem->z, &p
a7950 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d  Mem->z[2], pMem-
a7960 3e 6e 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  >n);.      pMem-
a7970 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c  >z[pMem->n] = '\
a7980 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  0';.      pMem->
a7990 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27  z[pMem->n+1] = '
a79a0 5c 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  \0';.      pMem-
a79b0 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65  >flags |= MEM_Te
a79c0 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  rm;.      pMem->
a79d0 65 6e 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d  enc = bom;.    }
a79e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
a79f0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
a7a00 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
a7a10 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20  */../*.** pZ is 
a7a20 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  a UTF-8 encoded 
a7a30 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20  unicode string. 
a7a40 49 66 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73  If nByte is less
a7a50 20 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72   than zero,.** r
a7a60 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
a7a70 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72   of unicode char
a7a80 61 63 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20  acters in pZ up 
a7a90 74 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c  to (but not incl
a7aa0 75 64 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69  uding).** the fi
a7ab0 72 73 74 20 30 78 30 30 20 62 79 74 65 2e 20 49  rst 0x00 byte. I
a7ac0 66 20 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c  f nByte is not l
a7ad0 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72  ess than zero, r
a7ae0 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d  eturn the.** num
a7af0 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63  ber of unicode c
a7b00 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65  haracters in the
a7b10 20 66 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20   first nByte of 
a7b20 70 5a 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a  pZ (or up to .**
a7b30 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30 2c   the first 0x00,
a7b40 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73   whichever comes
a7b50 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49   first)..*/.SQLI
a7b60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a7b70 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
a7b80 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49  n(const char *zI
a7b90 6e 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  n, int nByte){. 
a7ba0 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f   int r = 0;.  co
a7bb0 6e 73 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e  nst u8 *z = (con
a7bc0 73 74 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f  st u8*)zIn;.  co
a7bd0 6e 73 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20  nst u8 *zTerm;. 
a7be0 20 69 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b   if( nByte>=0 ){
a7bf0 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b  .    zTerm = &z[
a7c00 6e 42 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b  nByte];.  }else{
a7c10 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f  .    zTerm = (co
a7c20 6e 73 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20  nst u8*)(-1);.  
a7c30 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a  }.  assert( z<=z
a7c40 54 65 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28  Term );.  while(
a7c50 20 2a 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72   *z!=0 && z<zTer
a7c60 6d 20 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f  m ){.    SQLITE_
a7c70 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20  SKIP_UTF8(z);.  
a7c80 20 20 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74    r++;.  }.  ret
a7c90 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  urn r;.}../* Thi
a7ca0 73 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20  s test function 
a7cb0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
a7cc0 20 75 73 65 64 20 62 79 20 74 68 65 20 61 75 74   used by the aut
a7cd0 6f 6d 61 74 65 64 20 74 65 73 74 2d 73 75 69 74  omated test-suit
a7ce0 65 2e 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20  e. .** Hence it 
a7cf0 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
a7d00 65 20 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64  e in debug build
a7d10 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  s..*/.#if define
a7d20 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26  d(SQLITE_TEST) &
a7d30 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
a7d40 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72  _DEBUG)./*.** Tr
a7d50 61 6e 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f  anslate UTF-8 to
a7d60 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68   UTF-8..**.** Th
a7d70 69 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63  is has the effec
a7d80 74 20 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65  t of making sure
a7d90 20 74 68 61 74 20 74 68 65 20 73 74 72 69 6e 67   that the string
a7da0 20 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a   is well-formed.
a7db0 2a 2a 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f  ** UTF-8.  Misco
a7dc0 64 65 64 20 63 68 61 72 61 63 74 65 72 73 20 61  ded characters a
a7dd0 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a  re removed..**.*
a7de0 2a 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f  * The translatio
a7df0 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61  n is done in-pla
a7e00 63 65 20 28 73 69 6e 63 65 20 69 74 20 69 73 20  ce (since it is 
a7e10 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74  impossible for t
a7e20 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54  he.** correct UT
a7e30 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20  F-8 encoding to 
a7e40 62 65 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61  be longer than a
a7e50 20 6d 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64   malformed encod
a7e60 69 6e 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ing)..*/.SQLITE_
a7e70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a7e80 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67  te3Utf8To8(unsig
a7e90 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a  ned char *zIn){.
a7ea0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
a7eb0 2a 7a 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75  *zOut = zIn;.  u
a7ec0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53  nsigned char *zS
a7ed0 74 61 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e  tart = zIn;.  un
a7ee0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65  signed char *zTe
a7ef0 72 6d 20 3d 20 26 7a 49 6e 5b 73 71 6c 69 74 65  rm = &zIn[sqlite
a7f00 33 53 74 72 6c 65 6e 33 30 28 28 63 68 61 72 20  3Strlen30((char 
a7f10 2a 29 7a 49 6e 29 5d 3b 0a 20 20 75 33 32 20 63  *)zIn)];.  u32 c
a7f20 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b  ;..  while( zIn[
a7f30 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71  0] ){.    c = sq
a7f40 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
a7f50 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74  n, zTerm, (const
a7f60 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20 20 20   u8**)&zIn);.   
a7f70 20 69 66 28 20 63 21 3d 30 78 66 66 66 64 20 29   if( c!=0xfffd )
a7f80 7b 0a 20 20 20 20 20 20 57 52 49 54 45 5f 55 54  {.      WRITE_UT
a7f90 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a 20 20 20  F8(zOut, c);.   
a7fa0 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 74 20 3d   }.  }.  *zOut =
a7fb0 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e   0;.  return (in
a7fc0 74 29 28 7a 4f 75 74 20 2d 20 7a 53 74 61 72 74  t)(zOut - zStart
a7fd0 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  );.}.#endif..#if
a7fe0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
a7ff0 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f 6e  _UTF16./*.** Con
a8000 76 65 72 74 20 61 20 55 54 46 2d 31 36 20 73 74  vert a UTF-16 st
a8010 72 69 6e 67 20 69 6e 20 74 68 65 20 6e 61 74 69  ring in the nati
a8020 76 65 20 65 6e 63 6f 64 69 6e 67 20 69 6e 74 6f  ve encoding into
a8030 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e   a UTF-8 string.
a8040 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f  .** Memory to ho
a8050 6c 64 20 74 68 65 20 55 54 46 2d 38 20 73 74 72  ld the UTF-8 str
a8060 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ing is obtained 
a8070 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
a8080 6c 6f 63 20 61 6e 64 20 6d 75 73 74 0a 2a 2a 20  loc and must.** 
a8090 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20  be freed by the 
a80a0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
a80b0 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20  ..**.** NULL is 
a80c0 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 72  returned if ther
a80d0 65 20 69 73 20 61 6e 20 61 6c 6c 6f 63 61 74 69  e is an allocati
a80e0 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  on error..*/.SQL
a80f0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
a8100 20 2a 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f   *sqlite3Utf16to
a8110 38 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  8(sqlite3 *db, c
a8120 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e  onst void *z, in
a8130 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20  t nByte){.  Mem 
a8140 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20  m;.  memset(&m, 
a8150 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20  0, sizeof(m));. 
a8160 20 6d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 71   m.db = db;.  sq
a8170 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
a8180 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 79 74 65 2c  tr(&m, z, nByte,
a8190 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
a81a0 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  IVE, SQLITE_STAT
a81b0 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  IC);.  sqlite3Vd
a81c0 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
a81d0 28 26 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  (&m, SQLITE_UTF8
a81e0 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
a81f0 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
a8200 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
a8210 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 20 20  elease(&m);.    
a8220 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61  m.z = 0;.  }.  a
a8230 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 73 20  ssert( (m.flags 
a8240 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 7c  & MEM_Term)!=0 |
a8250 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
a8260 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ed );.  assert( 
a8270 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53  (m.flags & MEM_S
a8280 74 72 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61  tr)!=0 || db->ma
a8290 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
a82a0 72 65 74 75 72 6e 20 28 6d 2e 66 6c 61 67 73 20  return (m.flags 
a82b0 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 3f 20  & MEM_Dyn)!=0 ? 
a82c0 6d 2e 7a 20 3a 20 73 71 6c 69 74 65 33 44 62 53  m.z : sqlite3DbS
a82d0 74 72 44 75 70 28 64 62 2c 20 6d 2e 7a 29 3b 0a  trDup(db, m.z);.
a82e0 7d 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61  }../*.** pZ is a
a82f0 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20   UTF-16 encoded 
a8300 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20  unicode string. 
a8310 49 66 20 6e 43 68 61 72 20 69 73 20 6c 65 73 73  If nChar is less
a8320 20 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72   than zero,.** r
a8330 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
a8340 20 6f 66 20 62 79 74 65 73 20 75 70 20 74 6f 20   of bytes up to 
a8350 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69  (but not includi
a8360 6e 67 29 2c 20 74 68 65 20 66 69 72 73 74 20 70  ng), the first p
a8370 61 69 72 0a 2a 2a 20 6f 66 20 63 6f 6e 73 65 63  air.** of consec
a8380 75 74 69 76 65 20 30 78 30 30 20 62 79 74 65 73  utive 0x00 bytes
a8390 20 69 6e 20 70 5a 2e 20 49 66 20 6e 43 68 61 72   in pZ. If nChar
a83a0 20 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61   is not less tha
a83b0 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20  n zero,.** then 
a83c0 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
a83d0 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
a83e0 65 20 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e  e first nChar un
a83f0 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73  icode characters
a8400 0a 2a 2a 20 69 6e 20 70 5a 20 28 6f 72 20 75 70  .** in pZ (or up
a8410 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74   until the first
a8420 20 70 61 69 72 20 6f 66 20 30 78 30 30 20 62 79   pair of 0x00 by
a8430 74 65 73 2c 20 77 68 69 63 68 65 76 65 72 20 63  tes, whichever c
a8440 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a  omes first)..*/.
a8450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a8460 6e 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42  nt sqlite3Utf16B
a8470 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69  yteLen(const voi
a8480 64 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61  d *zIn, int nCha
a8490 72 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  r){.  unsigned i
a84a0 6e 74 20 63 20 3d 20 31 3b 0a 20 20 63 68 61 72  nt c = 1;.  char
a84b0 20 63 6f 6e 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b   const *z = zIn;
a84c0 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20  .  int n = 0;.  
a84d0 69 66 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36  if( SQLITE_UTF16
a84e0 4e 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55  NATIVE==SQLITE_U
a84f0 54 46 31 36 42 45 20 29 7b 0a 20 20 20 20 2f 2a  TF16BE ){.    /*
a8500 20 55 73 69 6e 67 20 61 6e 20 22 69 66 20 28 53   Using an "if (S
a8510 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
a8520 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  E==SQLITE_UTF16B
a8530 45 29 22 20 63 6f 6e 73 74 72 75 63 74 20 68 65  E)" construct he
a8540 72 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 6e  re.    ** and in
a8550 20 6f 74 68 65 72 20 70 61 72 74 73 20 6f 66 20   other parts of 
a8560 74 68 69 73 20 66 69 6c 65 20 6d 65 61 6e 73 20  this file means 
a8570 74 68 61 74 20 61 74 20 6f 6e 65 20 62 72 61 6e  that at one bran
a8580 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e  ch will.    ** n
a8590 6f 74 20 62 65 20 63 6f 76 65 72 65 64 20 62 79  ot be covered by
a85a0 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e   coverage testin
a85b0 67 20 6f 6e 20 61 6e 79 20 73 69 6e 67 6c 65 20  g on any single 
a85c0 68 6f 73 74 2e 20 42 75 74 20 63 6f 76 65 72 61  host. But covera
a85d0 67 65 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  ge.    ** will b
a85e0 65 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 74 68  e complete if th
a85f0 65 20 74 65 73 74 73 20 61 72 65 20 72 75 6e 20  e tests are run 
a8600 6f 6e 20 62 6f 74 68 20 61 20 6c 69 74 74 6c 65  on both a little
a8610 2d 65 6e 64 69 61 6e 20 61 6e 64 20 0a 20 20 20  -endian and .   
a8620 20 2a 2a 20 62 69 67 2d 65 6e 64 69 61 6e 20 68   ** big-endian h
a8630 6f 73 74 2e 20 42 65 63 61 75 73 65 20 62 6f 74  ost. Because bot
a8640 68 20 74 68 65 20 55 54 46 31 36 4e 41 54 49 56  h the UTF16NATIV
a8650 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 55 54 46  E and SQLITE_UTF
a8660 31 36 42 45 0a 20 20 20 20 2a 2a 20 6d 61 63 72  16BE.    ** macr
a8670 6f 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20  os are constant 
a8680 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
a8690 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e  the compiler can
a86a0 20 64 65 74 65 72 6d 69 6e 65 0a 20 20 20 20 2a   determine.    *
a86b0 2a 20 77 68 69 63 68 20 62 72 61 6e 63 68 20 77  * which branch w
a86c0 69 6c 6c 20 62 65 20 66 6f 6c 6c 6f 77 65 64 2e  ill be followed.
a86d0 20 49 74 20 69 73 20 74 68 65 72 65 66 6f 72 65   It is therefore
a86e0 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 6e 6f   assumed that no
a86f0 20 72 75 6e 74 69 6d 65 0a 20 20 20 20 2a 2a 20   runtime.    ** 
a8700 70 65 6e 61 6c 74 79 20 69 73 20 70 61 69 64 20  penalty is paid 
a8710 66 6f 72 20 74 68 69 73 20 22 69 66 22 20 73 74  for this "if" st
a8720 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a  atement..    */.
a8730 20 20 20 20 77 68 69 6c 65 28 20 63 20 26 26 20      while( c && 
a8740 28 28 6e 43 68 61 72 3c 30 29 20 7c 7c 20 6e 3c  ((nChar<0) || n<
a8750 6e 43 68 61 72 29 20 29 7b 0a 20 20 20 20 20 20  nChar) ){.      
a8760 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20  READ_UTF16BE(z, 
a8770 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20  c);.      n++;. 
a8780 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
a8790 20 20 77 68 69 6c 65 28 20 63 20 26 26 20 28 28    while( c && ((
a87a0 6e 43 68 61 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43  nChar<0) || n<nC
a87b0 68 61 72 29 20 29 7b 0a 20 20 20 20 20 20 52 45  har) ){.      RE
a87c0 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29  AD_UTF16LE(z, c)
a87d0 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20  ;.      n++;.   
a87e0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
a87f0 28 69 6e 74 29 28 7a 2d 28 63 68 61 72 20 63 6f  (int)(z-(char co
a8800 6e 73 74 20 2a 29 7a 49 6e 29 2d 28 28 63 3d 3d  nst *)zIn)-((c==
a8810 30 29 3f 32 3a 30 29 3b 0a 7d 0a 0a 23 69 66 20  0)?2:0);.}..#if 
a8820 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
a8830 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EST)./*.** This 
a8840 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
a8850 64 20 66 72 6f 6d 20 74 68 65 20 54 43 4c 20 74  d from the TCL t
a8860 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 22 74 72  est function "tr
a8870 61 6e 73 6c 61 74 65 5f 73 65 6c 66 74 65 73 74  anslate_selftest
a8880 22 2e 0a 2a 2a 20 49 74 20 63 68 65 63 6b 73 20  "..** It checks 
a8890 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69  that the primiti
a88a0 76 65 73 20 66 6f 72 20 73 65 72 69 61 6c 69 7a  ves for serializ
a88b0 69 6e 67 20 61 6e 64 20 64 65 73 65 72 69 61 6c  ing and deserial
a88c0 69 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74  izing.** charact
a88d0 65 72 73 20 69 6e 20 65 61 63 68 20 65 6e 63 6f  ers in each enco
a88e0 64 69 6e 67 20 61 72 65 20 69 6e 76 65 72 73 65  ding are inverse
a88f0 73 20 6f 66 20 65 61 63 68 20 6f 74 68 65 72 2e  s of each other.
a8900 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a8910 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
a8920 74 66 53 65 6c 66 54 65 73 74 28 76 6f 69 64 29  tfSelfTest(void)
a8930 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
a8940 20 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65   i, t;.  unsigne
a8950 64 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b  d char zBuf[20];
a8960 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
a8970 20 2a 7a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20   *z;.  unsigned 
a8980 63 68 61 72 20 2a 7a 54 65 72 6d 3b 0a 20 20 69  char *zTerm;.  i
a8990 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64  nt n;.  unsigned
a89a0 20 69 6e 74 20 63 3b 0a 0a 20 20 66 6f 72 28 69   int c;..  for(i
a89b0 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30  =0; i<0x00110000
a89c0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 20 3d 20  ; i++){.    z = 
a89d0 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f  zBuf;.    WRITE_
a89e0 55 54 46 38 28 7a 2c 20 69 29 3b 0a 20 20 20 20  UTF8(z, i);.    
a89f0 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66  n = (int)(z-zBuf
a8a00 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
a8a10 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20  >0 && n<=4 );.  
a8a20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20    z[0] = 0;.    
a8a30 7a 54 65 72 6d 20 3d 20 7a 3b 0a 20 20 20 20 7a  zTerm = z;.    z
a8a40 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d   = zBuf;.    c =
a8a50 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
a8a60 28 7a 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73  (z, zTerm, (cons
a8a70 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20  t u8**)&z);.    
a8a80 74 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69  t = i;.    if( i
a8a90 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30  >=0xD800 && i<=0
a8aa0 78 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46  xDFFF ) t = 0xFF
a8ab0 46 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30  FD;.    if( (i&0
a8ac0 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46  xFFFFFFFE)==0xFF
a8ad0 46 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b  FE ) t = 0xFFFD;
a8ae0 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d  .    assert( c==
a8af0 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
a8b00 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a   (z-zBuf)==n );.
a8b10 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
a8b20 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b  <0x00110000; i++
a8b30 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78  ){.    if( i>=0x
a8b40 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30  D800 && i<0xE000
a8b50 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
a8b60 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
a8b70 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20  RITE_UTF16LE(z, 
a8b80 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74  i);.    n = (int
a8b90 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61  )(z-zBuf);.    a
a8ba0 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c  ssert( n>0 && n<
a8bb0 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d  =4 );.    z[0] =
a8bc0 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66   0;.    z = zBuf
a8bd0 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36  ;.    READ_UTF16
a8be0 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73  LE(z, c);.    as
a8bf0 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20  sert( c==i );.  
a8c00 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75    assert( (z-zBu
a8c10 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66  f)==n );.  }.  f
a8c20 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31  or(i=0; i<0x0011
a8c30 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  0000; i++){.    
a8c40 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26  if( i>=0xD800 &&
a8c50 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74   i<0xE000 ) cont
a8c60 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42  inue;.    z = zB
a8c70 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54  uf;.    WRITE_UT
a8c80 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20  F16BE(z, i);.   
a8c90 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75   n = (int)(z-zBu
a8ca0 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  f);.    assert( 
a8cb0 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20  n>0 && n<=4 );. 
a8cc0 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20     z[0] = 0;.   
a8cd0 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52   z = zBuf;.    R
a8ce0 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63  EAD_UTF16BE(z, c
a8cf0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63  );.    assert( c
a8d00 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==i );.    asser
a8d10 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29  t( (z-zBuf)==n )
a8d20 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  ;.  }.}.#endif /
a8d30 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f  * SQLITE_TEST */
a8d40 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
a8d50 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
a8d60 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a8d70 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a   End of utf.c **
a8d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a8db0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a8dc0 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c   Begin file util
a8dd0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
a8de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a8e00 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
a8e10 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
a8e20 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
a8e30 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
a8e40 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
a8e50 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
a8e60 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
a8e70 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
a8e80 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
a8e90 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
a8ea0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
a8eb0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
a8ec0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
a8ed0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
a8ee0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
a8ef0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
a8f00 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
a8f10 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
a8f20 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
a8f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8f70 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69  *******.** Utili
a8f80 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  ty functions use
a8f90 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c  d throughout sql
a8fa0 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ite..**.** This 
a8fb0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75  file contains fu
a8fc0 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f  nctions for allo
a8fd0 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63  cating memory, c
a8fe0 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69  omparing.** stri
a8ff0 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c  ngs, and stuff l
a9000 69 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20  ike that..**.** 
a9010 24 49 64 3a 20 75 74 69 6c 2e 63 2c 76 20 31 2e  $Id: util.c,v 1.
a9020 32 34 39 20 32 30 30 39 2f 30 33 2f 30 31 20 32  249 2009/03/01 2
a9030 32 3a 32 39 3a 32 30 20 64 72 68 20 45 78 70 20  2:29:20 drh Exp 
a9040 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  $.*/../*.** Rout
a9050 69 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75  ine needed to su
a9060 70 70 6f 72 74 20 74 68 65 20 74 65 73 74 63 61  pport the testca
a9070 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23  se() macro..*/.#
a9080 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56  ifdef SQLITE_COV
a9090 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54  ERAGE_TEST.SQLIT
a90a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a90b0 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69  qlite3Coverage(i
a90c0 6e 74 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20  nt x){.  static 
a90d0 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20  int dummy = 0;. 
a90e0 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23   dummy += x;.}.#
a90f0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  endif../*.** Rou
a9100 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73  tine needed to s
a9110 75 70 70 6f 72 74 20 74 68 65 20 41 4c 57 41 59  upport the ALWAY
a9120 53 28 29 20 61 6e 64 20 4e 45 56 45 52 28 29 20  S() and NEVER() 
a9130 6d 61 63 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  macros..**.** Th
a9140 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c  e argument to AL
a9150 57 41 59 53 28 29 20 73 68 6f 75 6c 64 20 61 6c  WAYS() should al
a9160 77 61 79 73 20 62 65 20 74 72 75 65 20 61 6e 64  ways be true and
a9170 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   the argument.**
a9180 20 74 6f 20 4e 45 56 45 52 28 29 20 73 68 6f 75   to NEVER() shou
a9190 6c 64 20 61 6c 77 61 79 73 20 62 65 20 66 61 6c  ld always be fal
a91a0 73 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 69  se.  If either i
a91b0 73 20 6e 6f 74 20 74 68 65 20 63 61 73 65 0a 2a  s not the case.*
a91c0 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * then this rout
a91d0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e  ine is called in
a91e0 20 6f 72 64 65 72 20 74 6f 20 74 68 72 6f 77 20   order to throw 
a91f0 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  an error..**.** 
a9200 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c  This routine onl
a9210 79 20 65 78 69 73 74 73 20 69 66 20 61 73 73 65  y exists if asse
a9220 72 74 28 29 20 69 73 20 6f 70 65 72 61 74 69 6f  rt() is operatio
a9230 6e 61 6c 2e 20 20 49 74 20 61 6c 77 61 79 73 0a  nal.  It always.
a9240 2a 2a 20 74 68 72 6f 77 73 20 61 6e 20 61 73 73  ** throws an ass
a9250 65 72 74 20 6f 6e 20 69 74 73 20 66 69 72 73 74  ert on its first
a9260 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68   invocation.  Th
a9270 65 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 61  e variable has a
a9280 20 6c 6f 6e 67 0a 2a 2a 20 6e 61 6d 65 20 74 6f   long.** name to
a9290 20 68 65 6c 70 20 74 68 65 20 61 73 73 65 72 74   help the assert
a92a0 28 29 20 6d 65 73 73 61 67 65 20 62 65 20 6d 6f  () message be mo
a92b0 72 65 20 72 65 61 64 61 62 6c 65 2e 20 20 54 68  re readable.  Th
a92c0 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73  e variable.** is
a92d0 20 75 73 65 64 20 74 6f 20 70 72 65 76 65 6e 74   used to prevent
a92e0 20 61 20 74 6f 6f 2d 63 6c 65 76 65 72 20 6f 70   a too-clever op
a92f0 74 69 6d 69 7a 65 72 20 66 72 6f 6d 20 6f 70 74  timizer from opt
a9300 69 6d 69 7a 69 6e 67 20 6f 75 74 20 74 68 65 0a  imizing out the.
a9310 2a 2a 20 65 6e 74 69 72 65 20 63 61 6c 6c 2e 0a  ** entire call..
a9320 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
a9330 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
a9340 20 69 6e 74 20 73 71 6c 69 74 65 33 41 73 73 65   int sqlite3Asse
a9350 72 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  rt(void){.  stat
a9360 69 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20  ic volatile int 
a9370 41 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c 73 65  ALWAYS_was_false
a9380 5f 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f 74 72  _or_NEVER_was_tr
a9390 75 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ue = 0;.  assert
a93a0 28 20 41 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c  ( ALWAYS_was_fal
a93b0 73 65 5f 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f  se_or_NEVER_was_
a93c0 74 72 75 65 20 29 3b 20 20 20 20 20 20 2f 2a 20  true );      /* 
a93d0 41 6c 77 61 79 73 20 66 61 69 6c 73 20 2a 2f 0a  Always fails */.
a93e0 20 20 72 65 74 75 72 6e 20 41 4c 57 41 59 53 5f    return ALWAYS_
a93f0 77 61 73 5f 66 61 6c 73 65 5f 6f 72 5f 4e 45 56  was_false_or_NEV
a9400 45 52 5f 77 61 73 5f 74 72 75 65 2b 2b 3b 20 20  ER_was_true++;  
a9410 20 20 20 20 20 2f 2a 20 4e 6f 74 20 52 65 61 63       /* Not Reac
a9420 68 65 64 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 0a  hed */.}.#endif.
a9430 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
a9440 75 65 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69  ue if the floati
a9450 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69  ng point value i
a9460 73 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28  s Not a Number (
a9470 4e 61 4e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NaN)..*/.SQLITE_
a9480 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a9490 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20  te3IsNaN(double 
a94a0 78 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20 4e 61  x){.  /* This Na
a94b0 4e 20 74 65 73 74 20 73 6f 6d 65 74 69 6d 65 73  N test sometimes
a94c0 20 66 61 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c   fails if compil
a94d0 65 64 20 6f 6e 20 47 43 43 20 77 69 74 68 20 2d  ed on GCC with -
a94e0 66 66 61 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a  ffast-math..  **
a94f0 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61   On the other ha
a9500 6e 64 2c 20 74 68 65 20 75 73 65 20 6f 66 20 2d  nd, the use of -
a9510 66 66 61 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73  ffast-math comes
a9520 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77   with the follow
a9530 69 6e 67 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67  ing.  ** warning
a9540 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
a9550 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66   This option [-f
a9560 66 61 73 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c  fast-math] shoul
a9570 64 20 6e 65 76 65 72 20 62 65 20 74 75 72 6e 65  d never be turne
a9580 64 20 6f 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a  d on by any.  **
a9590 20 20 20 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20        -O option 
a95a0 73 69 6e 63 65 20 69 74 20 63 61 6e 20 72 65 73  since it can res
a95b0 75 6c 74 20 69 6e 20 69 6e 63 6f 72 72 65 63 74  ult in incorrect
a95c0 20 6f 75 74 70 75 74 20 66 6f 72 20 70 72 6f 67   output for prog
a95d0 72 61 6d 73 0a 20 20 2a 2a 20 20 20 20 20 20 77  rams.  **      w
a95e0 68 69 63 68 20 64 65 70 65 6e 64 20 6f 6e 20 61  hich depend on a
a95f0 6e 20 65 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e  n exact implemen
a9600 74 61 74 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f  tation of IEEE o
a9610 72 20 49 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20  r ISO .  **     
a9620 20 72 75 6c 65 73 2f 73 70 65 63 69 66 69 63 61   rules/specifica
a9630 74 69 6f 6e 73 20 66 6f 72 20 6d 61 74 68 20 66  tions for math f
a9640 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20  unctions..  **. 
a9650 20 2a 2a 20 55 6e 64 65 72 20 4d 53 56 43 2c 20   ** Under MSVC, 
a9660 74 68 69 73 20 4e 61 4e 20 74 65 73 74 20 6d 61  this NaN test ma
a9670 79 20 66 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c  y fail if compil
a9680 65 64 20 77 69 74 68 20 61 20 66 6c 6f 61 74 69  ed with a floati
a9690 6e 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70  ng-.  ** point p
a96a0 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74  recision mode ot
a96b0 68 65 72 20 74 68 61 6e 20 2f 66 70 3a 70 72 65  her than /fp:pre
a96c0 63 69 73 65 2e 20 20 46 72 6f 6d 20 74 68 65 20  cise.  From the 
a96d0 4d 53 44 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d  MSDN .  ** docum
a96e0 65 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20  entation:.  **. 
a96f0 20 2a 2a 20 20 20 20 20 20 54 68 65 20 63 6f 6d   **      The com
a9700 70 69 6c 65 72 20 5b 77 69 74 68 20 2f 66 70 3a  piler [with /fp:
a9710 70 72 65 63 69 73 65 5d 20 77 69 6c 6c 20 70 72  precise] will pr
a9720 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f  operly handle co
a9730 6d 70 61 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20  mparisons .  ** 
a9740 20 20 20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e       involving N
a9750 61 4e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  aN. For example,
a9760 20 78 20 21 3d 20 78 20 65 76 61 6c 75 61 74 65   x != x evaluate
a9770 73 20 74 6f 20 74 72 75 65 20 69 66 20 78 20 69  s to true if x i
a9780 73 20 4e 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20  s NaN .  **     
a9790 20 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66   ....  */.#ifdef
a97a0 20 5f 5f 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23   __FAST_MATH__.#
a97b0 20 65 72 72 6f 72 20 53 51 4c 69 74 65 20 77 69   error SQLite wi
a97c0 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72  ll not work corr
a97d0 65 63 74 6c 79 20 77 69 74 68 20 74 68 65 20 2d  ectly with the -
a97e0 66 66 61 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f  ffast-math optio
a97f0 6e 20 6f 66 20 47 43 43 2e 0a 23 65 6e 64 69 66  n of GCC..#endif
a9800 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62  .  volatile doub
a9810 6c 65 20 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61  le y = x;.  vola
a9820 74 69 6c 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20  tile double z = 
a9830 79 3b 0a 20 20 72 65 74 75 72 6e 20 79 21 3d 7a  y;.  return y!=z
a9840 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
a9850 74 65 20 61 20 73 74 72 69 6e 67 20 6c 65 6e 67  te a string leng
a9860 74 68 20 74 68 61 74 20 69 73 20 6c 69 6d 69 74  th that is limit
a9870 65 64 20 74 6f 20 77 68 61 74 20 63 61 6e 20 62  ed to what can b
a9880 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 6c  e stored in.** l
a9890 6f 77 65 72 20 33 30 20 62 69 74 73 20 6f 66 20  ower 30 bits of 
a98a0 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20  a 32-bit signed 
a98b0 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49  integer..*/.SQLI
a98c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a98d0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63  qlite3Strlen30(c
a98e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
a98f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20   const char *z2 
a9900 3d 20 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a  = z;.  while( *z
a9910 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72  2 ){ z2++; }.  r
a9920 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 66 66  eturn 0x3fffffff
a9930 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29   & (int)(z2 - z)
a9940 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
a9950 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  n the length of 
a9960 61 20 73 74 72 69 6e 67 2c 20 65 78 63 65 70 74  a string, except
a9970 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68   do not allow th
a9980 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 0a  e string length.
a9990 2a 2a 20 74 6f 20 65 78 63 65 65 64 20 74 68 65  ** to exceed the
a99a0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45   SQLITE_LIMIT_LE
a99b0 4e 47 54 48 20 73 65 74 74 69 6e 67 2e 0a 2a 2f  NGTH setting..*/
a99c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a99d0 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65  int sqlite3Strle
a99e0 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  n(sqlite3 *db, c
a99f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
a9a00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20   const char *z2 
a9a10 3d 20 7a 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a  = z;.  int len;.
a9a20 20 20 69 6e 74 20 78 3b 0a 20 20 77 68 69 6c 65    int x;.  while
a9a30 28 20 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d  ( *z2 ){ z2++; }
a9a40 0a 20 20 78 20 3d 20 28 69 6e 74 29 28 7a 32 20  .  x = (int)(z2 
a9a50 2d 20 7a 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 78  - z);.  len = 0x
a9a60 37 66 66 66 66 66 66 66 20 26 20 78 3b 0a 20 20  7fffffff & x;.  
a9a70 69 66 28 20 6c 65 6e 21 3d 78 20 7c 7c 20 6c 65  if( len!=x || le
a9a80 6e 20 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  n > db->aLimit[S
a9a90 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
a9aa0 54 48 5d 20 29 7b 0a 20 20 20 20 72 65 74 75 72  TH] ){.    retur
a9ab0 6e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  n db->aLimit[SQL
a9ac0 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
a9ad0 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
a9ae0 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a  return len;.  }.
a9af0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
a9b00 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   most recent err
a9b10 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f  or code and erro
a9b20 72 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  r string for the
a9b30 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c   sqlite.** handl
a9b40 65 20 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f  e "db". The erro
a9b50 72 20 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f  r code is set to
a9b60 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a   "err_code"..**.
a9b70 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  ** If it is not 
a9b80 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f  NULL, string zFo
a9b90 72 6d 61 74 20 73 70 65 63 69 66 69 65 73 20 74  rmat specifies t
a9ba0 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65  he format of the
a9bb0 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67  .** error string
a9bc0 20 69 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66   in the style of
a9bd0 20 74 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63   the printf func
a9be0 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f  tions: The follo
a9bf0 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63  wing.** format c
a9c00 68 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c  haracters are al
a9c10 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  lowed:.**.**    
a9c20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74    %s      Insert
a9c30 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20   a string.**    
a9c40 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69    %z      A stri
a9c50 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  ng that should b
a9c60 65 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73  e freed after us
a9c70 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20  e.**      %d    
a9c80 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65    Insert an inte
a9c90 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20  ger.**      %T  
a9ca0 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b      Insert a tok
a9cb0 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20  en.**      %S   
a9cc0 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69     Insert the fi
a9cd0 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61  rst element of a
a9ce0 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a   SrcList.**.** z
a9cf0 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73  Format and any s
a9d00 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61  tring tokens tha
a9d10 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20  t follow it are 
a9d20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a  assumed to be.**
a9d30 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
a9d40 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61  8..**.** To clea
a9d50 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  r the most recen
a9d60 74 20 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69  t error for sqli
a9d70 74 65 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20  te handle "db", 
a9d80 73 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20  sqlite3Error.** 
a9d90 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64  should be called
a9da0 20 77 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73   with err_code s
a9db0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  et to SQLITE_OK 
a9dc0 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a  and zFormat set.
a9dd0 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53  ** to NULL..*/.S
a9de0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a9df0 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  id sqlite3Error(
a9e00 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
a9e10 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74   err_code, const
a9e20 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
a9e30 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26  ...){.  if( db &
a9e40 26 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28  & (db->pErr || (
a9e50 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74  db->pErr = sqlit
a9e60 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21  e3ValueNew(db))!
a9e70 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65  =0) ){.    db->e
a9e80 72 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64  rrCode = err_cod
a9e90 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d  e;.    if( zForm
a9ea0 61 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72  at ){.      char
a9eb0 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69   *z;.      va_li
a9ec0 73 74 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f  st ap;.      va_
a9ed0 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
a9ee0 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  t);.      z = sq
a9ef0 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62  lite3VMPrintf(db
a9f00 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a9f10 20 20 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29        va_end(ap)
a9f20 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
a9f30 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
a9f40 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49  Err, -1, z, SQLI
a9f50 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
a9f60 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65  DYNAMIC);.    }e
a9f70 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
a9f80 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62  e3ValueSetStr(db
a9f90 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51  ->pErr, 0, 0, SQ
a9fa0 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
a9fb0 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d  E_STATIC);.    }
a9fc0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
a9fd0 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  d an error messa
a9fe0 67 65 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45  ge to pParse->zE
a9ff0 72 72 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d  rrMsg and increm
aa000 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  ent pParse->nErr
aa010 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
aa020 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68  ng formatting ch
aa030 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c  aracters are all
aa040 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  owed:.**.**     
aa050 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20   %s      Insert 
aa060 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20  a string.**     
aa070 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e   %z      A strin
aa080 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  g that should be
aa090 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65   freed after use
aa0a0 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20  .**      %d     
aa0b0 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67   Insert an integ
aa0c0 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20  er.**      %T   
aa0d0 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65     Insert a toke
aa0e0 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20  n.**      %S    
aa0f0 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72    Insert the fir
aa100 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20  st element of a 
aa110 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68  SrcList.**.** Th
aa120 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  is function shou
aa130 6c 64 20 62 65 20 75 73 65 64 20 74 6f 20 72 65  ld be used to re
aa140 70 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74  port any error t
aa150 68 61 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73  hat occurs whils
aa160 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61  t.** compiling a
aa170 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
aa180 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c  (i.e. within sql
aa190 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e  ite3_prepare()).
aa1a0 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69   The.** last thi
aa1b0 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  ng the sqlite3_p
aa1c0 72 65 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f  repare() functio
aa1d0 6e 20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74  n does is copy t
aa1e0 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72  he error.** stor
aa1f0 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  ed by this funct
aa200 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ion into the dat
aa210 61 62 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69  abase handle usi
aa220 6e 67 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  ng sqlite3Error(
aa230 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73  )..** Function s
aa240 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 73 68  qlite3Error() sh
aa250 6f 75 6c 64 20 62 65 20 75 73 65 64 20 64 75 72  ould be used dur
aa260 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78  ing statement ex
aa270 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69  ecution.** (sqli
aa280 74 65 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29  te3_step() etc.)
aa290 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
aa2a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
aa2b0 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a  ErrorMsg(Parse *
aa2c0 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
aa2d0 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
aa2e0 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
aa2f0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
aa300 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70   pParse->db;.  p
aa310 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
aa320 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
aa330 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  b, pParse->zErrM
aa340 73 67 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28  sg);.  va_start(
aa350 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
aa360 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
aa370 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
aa380 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  f(db, zFormat, a
aa390 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
aa3a0 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
aa3b0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
aa3c0 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
aa3d0 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
aa3e0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65    }.}../*.** Cle
aa3f0 61 72 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  ar the error mes
aa400 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20  sage in pParse, 
aa410 69 66 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45  if any.*/.SQLITE
aa420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
aa430 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28  lite3ErrorClear(
aa440 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
aa450 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
aa460 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72  pParse->db, pPar
aa470 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  se->zErrMsg);.  
aa480 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
aa490 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  = 0;.  pParse->n
aa4a0 45 72 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Err = 0;.}../*.*
aa4b0 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c  * Convert an SQL
aa4c0 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74  -style quoted st
aa4d0 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d  ring into a norm
aa4e0 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d  al string by rem
aa4f0 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f  oving.** the quo
aa500 74 65 20 63 68 61 72 61 63 74 65 72 73 2e 20 20  te characters.  
aa510 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  The conversion i
aa520 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e  s done in-place.
aa530 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75    If the.** inpu
aa540 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e  t does not begin
aa550 20 77 69 74 68 20 61 20 71 75 6f 74 65 20 63 68   with a quote ch
aa560 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68  aracter, then th
aa570 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73  is routine.** is
aa580 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
aa590 32 30 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69  2002-Feb-14: Thi
aa5a0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74  s routine is ext
aa5b0 65 6e 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20  ended to remove 
aa5c0 4d 53 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a  MS-Access style.
aa5d0 2a 2a 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d  ** brackets from
aa5e0 20 61 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65   around identife
aa5f0 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  rs.  For example
aa600 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63  :  "[a-b-c]" bec
aa610 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e  omes.** "a-b-c".
aa620 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
aa630 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
aa640 65 71 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b  equote(char *z){
aa650 0a 20 20 63 68 61 72 20 71 75 6f 74 65 3b 0a 20  .  char quote;. 
aa660 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28   int i, j;.  if(
aa670 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
aa680 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a    quote = z[0];.
aa690 20 20 73 77 69 74 63 68 28 20 71 75 6f 74 65 20    switch( quote 
aa6a0 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27  ){.    case '\''
aa6b0 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61  :  break;.    ca
aa6c0 73 65 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b  se '"':   break;
aa6d0 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 20 20  .    case '`':  
aa6e0 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20   break;         
aa6f0 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79         /* For My
aa700 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  SQL compatibilit
aa710 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b  y */.    case '[
aa720 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27  ':   quote = ']'
aa730 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f  ;  break;  /* Fo
aa740 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63  r MS SqlServer c
aa750 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a  ompatibility */.
aa760 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20      default:    
aa770 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f  return;.  }.  fo
aa780 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d  r(i=1, j=0; z[i]
aa790 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
aa7a0 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20  z[i]==quote ){. 
aa7b0 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d       if( z[i+1]=
aa7c0 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20  =quote ){.      
aa7d0 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65    z[j++] = quote
aa7e0 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20  ;.        i++;. 
aa7f0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
aa800 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a      z[j++] = 0;.
aa810 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
aa820 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
aa830 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d  {.      z[j++] =
aa840 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d   z[i];.    }.  }
aa850 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 6e  .}../* Convenien
aa860 74 20 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f 0a  t short-hand */.
aa870 23 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c  #define UpperToL
aa880 6f 77 65 72 20 73 71 6c 69 74 65 33 55 70 70 65  ower sqlite3Uppe
aa890 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20  rToLower../*.** 
aa8a0 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76  Some systems hav
aa8b0 65 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74  e stricmp().  Ot
aa8c0 68 65 72 73 20 68 61 76 65 20 73 74 72 63 61 73  hers have strcas
aa8d0 65 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65  ecmp().  Because
aa8e0 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20  .** there is no 
aa8f0 63 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20  consistency, we 
aa900 77 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20  will define our 
aa910 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  own..*/.SQLITE_P
aa920 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
aa930 65 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20  e3StrICmp(const 
aa940 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e  char *zLeft, con
aa950 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29  st char *zRight)
aa960 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73  {.  register uns
aa970 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a  igned char *a, *
aa980 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e  b;.  a = (unsign
aa990 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b  ed char *)zLeft;
aa9a0 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  .  b = (unsigned
aa9b0 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a   char *)zRight;.
aa9c0 20 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26    while( *a!=0 &
aa9d0 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  & UpperToLower[*
aa9e0 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72  a]==UpperToLower
aa9f0 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b  [*b]){ a++; b++;
aaa00 20 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65   }.  return Uppe
aaa10 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55  rToLower[*a] - U
aaa20 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b  pperToLower[*b];
aaa30 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
aaa40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72  E int sqlite3Str
aaa50 4e 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  NICmp(const char
aaa60 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63   *zLeft, const c
aaa70 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74  har *zRight, int
aaa80 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20   N){.  register 
aaa90 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61  unsigned char *a
aaaa0 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73  , *b;.  a = (uns
aaab0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65  igned char *)zLe
aaac0 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67  ft;.  b = (unsig
aaad0 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68  ned char *)zRigh
aaae0 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20  t;.  while( N-- 
aaaf0 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26 26 20  > 0 && *a!=0 && 
aab00 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d  UpperToLower[*a]
aab10 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  ==UpperToLower[*
aab20 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d  b]){ a++; b++; }
aab30 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20  .  return N<0 ? 
aab40 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  0 : UpperToLower
aab50 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f  [*a] - UpperToLo
aab60 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  wer[*b];.}../*.*
aab70 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
aab80 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e 75 6d   z is a pure num
aab90 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20 52 65  eric string.  Re
aaba0 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68  turn FALSE if th
aabb0 65 0a 2a 2a 20 73 74 72 69 6e 67 20 63 6f 6e 74  e.** string cont
aabc0 61 69 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74  ains any charact
aabd0 65 72 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20  er which is not 
aabe0 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62 65 72  part of a number
aabf0 2e 20 49 66 0a 2a 2a 20 74 68 65 20 73 74 72 69  . If.** the stri
aac00 6e 67 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e  ng is numeric an
aac10 64 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 27  d contains the '
aac20 2e 27 20 63 68 61 72 61 63 74 65 72 2c 20 73 65  .' character, se
aac30 74 20 2a 72 65 61 6c 6e 75 6d 0a 2a 2a 20 74 6f  t *realnum.** to
aac40 20 54 52 55 45 20 28 6f 74 68 65 72 77 69 73 65   TRUE (otherwise
aac50 20 46 41 4c 53 45 29 2e 0a 2a 2a 0a 2a 2a 20 41   FALSE)..**.** A
aac60 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 69  n empty string i
aac70 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e  s considered non
aac80 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c  -numeric..*/.SQL
aac90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
aaca0 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28  sqlite3IsNumber(
aacb0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
aacc0 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20  nt *realnum, u8 
aacd0 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72  enc){.  int incr
aace0 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f   = (enc==SQLITE_
aacf0 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28  UTF8?1:2);.  if(
aad00 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46   enc==SQLITE_UTF
aad10 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66  16BE ) z++;.  if
aad20 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d  ( *z=='-' || *z=
aad30 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72  ='+' ) z += incr
aad40 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
aad50 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20  Isdigit(*z) ){. 
aad60 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
aad70 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20  .  z += incr;.  
aad80 69 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72  if( realnum ) *r
aad90 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68  ealnum = 0;.  wh
aada0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
aadb0 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20  git(*z) ){ z += 
aadc0 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a 7a  incr; }.  if( *z
aadd0 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 2b  =='.' ){.    z +
aade0 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20  = incr;.    if( 
aadf0 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28  !sqlite3Isdigit(
aae00 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  *z) ) return 0;.
aae10 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
aae20 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b  e3Isdigit(*z) ){
aae30 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20   z += incr; }.  
aae40 20 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 29 20    if( realnum ) 
aae50 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20  *realnum = 1;.  
aae60 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20  }.  if( *z=='e' 
aae70 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20  || *z=='E' ){.  
aae80 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20    z += incr;.   
aae90 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20   if( *z=='+' || 
aaea0 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69  *z=='-' ) z += i
aaeb0 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 73 71  ncr;.    if( !sq
aaec0 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29  lite3Isdigit(*z)
aaed0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
aaee0 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
aaef0 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20  sdigit(*z) ){ z 
aaf00 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 69  += incr; }.    i
aaf10 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65  f( realnum ) *re
aaf20 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20  alnum = 1;.  }. 
aaf30 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d   return *z==0;.}
aaf40 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69  ../*.** The stri
aaf50 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20 61 73 63  ng z[] is an asc
aaf60 69 69 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ii representatio
aaf70 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d 62  n of a real numb
aaf80 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74  er..** Convert t
aaf90 68 69 73 20 73 74 72 69 6e 67 20 74 6f 20 61 20  his string to a 
aafa0 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  double..**.** Th
aafb0 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d  is routine assum
aafc0 65 73 20 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c  es that z[] real
aafd0 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 6e 75  ly is a valid nu
aafe0 6d 62 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20  mber.  If it.** 
aaff0 69 73 20 6e 6f 74 2c 20 74 68 65 20 72 65 73 75  is not, the resu
ab000 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  lt is undefined.
ab010 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
ab020 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 74  ine is used inst
ab030 65 61 64 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ead of the libra
ab040 72 79 20 61 74 6f 66 28 29 20 66 75 6e 63 74 69  ry atof() functi
ab050 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68  on because.** th
ab060 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29  e library atof()
ab070 20 6d 69 67 68 74 20 77 61 6e 74 20 74 6f 20 75   might want to u
ab080 73 65 20 22 2c 22 20 61 73 20 74 68 65 20 64 65  se "," as the de
ab090 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74  cimal point inst
ab0a0 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65  ead.** of "." de
ab0b0 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c  pending on how l
ab0c0 6f 63 61 6c 65 20 69 73 20 73 65 74 2e 20 20 42  ocale is set.  B
ab0d0 75 74 20 74 68 61 74 20 77 6f 75 6c 64 20 63 61  ut that would ca
ab0e0 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20  use problems.** 
ab0f0 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20 74 68 69  for SQL.  So thi
ab100 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73  s routine always
ab110 20 75 73 65 73 20 22 2e 22 20 72 65 67 61 72 64   uses "." regard
ab120 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a  less of locale..
ab130 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ab140 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f  E int sqlite3Ato
ab150 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  F(const char *z,
ab160 20 64 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74   double *pResult
ab170 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
ab180 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
ab190 50 4f 49 4e 54 0a 20 20 69 6e 74 20 73 69 67 6e  POINT.  int sign
ab1a0 20 3d 20 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68   = 1;.  const ch
ab1b0 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a  ar *zBegin = z;.
ab1c0 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50    LONGDOUBLE_TYP
ab1d0 45 20 76 31 20 3d 20 30 2e 30 3b 0a 20 20 69 6e  E v1 = 0.0;.  in
ab1e0 74 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 20 3d  t nSignificant =
ab1f0 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c   0;.  while( sql
ab200 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20  ite3Isspace(*z) 
ab210 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d  ) z++;.  if( *z=
ab220 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e  ='-' ){.    sign
ab230 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a   = -1;.    z++;.
ab240 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d    }else if( *z==
ab250 27 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a  '+' ){.    z++;.
ab260 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30    }.  while( z[0
ab270 5d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 2b  ]=='0' ){.    z+
ab280 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  +;.  }.  while( 
ab290 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
ab2a0 7a 29 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 76  z) ){.    v1 = v
ab2b0 31 2a 31 30 2e 30 20 2b 20 28 2a 7a 20 2d 20 27  1*10.0 + (*z - '
ab2c0 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20  0');.    z++;.  
ab2d0 20 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b    nSignificant++
ab2e0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ;.  }.  if( *z==
ab2f0 27 2e 27 20 29 7b 0a 20 20 20 20 4c 4f 4e 47 44  '.' ){.    LONGD
ab300 4f 55 42 4c 45 5f 54 59 50 45 20 64 69 76 69 73  OUBLE_TYPE divis
ab310 6f 72 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a 2b  or = 1.0;.    z+
ab320 2b 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 67 6e  +;.    if( nSign
ab330 69 66 69 63 61 6e 74 3d 3d 30 20 29 7b 0a 20 20  ificant==0 ){.  
ab340 20 20 20 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d      while( z[0]=
ab350 3d 27 30 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='0' ){.        
ab360 64 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b  divisor *= 10.0;
ab370 0a 20 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  .        z++;.  
ab380 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
ab390 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
ab3a0 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20  digit(*z) ){.   
ab3b0 20 20 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63     if( nSignific
ab3c0 61 6e 74 3c 31 38 20 29 7b 0a 20 20 20 20 20 20  ant<18 ){.      
ab3d0 20 20 76 31 20 3d 20 76 31 2a 31 30 2e 30 20 2b    v1 = v1*10.0 +
ab3e0 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20   (*z - '0');.   
ab3f0 20 20 20 20 20 64 69 76 69 73 6f 72 20 2a 3d 20       divisor *= 
ab400 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 6e 53  10.0;.        nS
ab410 69 67 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20  ignificant++;.  
ab420 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 2b 2b 3b      }.      z++;
ab430 0a 20 20 20 20 7d 0a 20 20 20 20 76 31 20 2f 3d  .    }.    v1 /=
ab440 20 64 69 76 69 73 6f 72 3b 0a 20 20 7d 0a 20 20   divisor;.  }.  
ab450 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a  if( *z=='e' || *
ab460 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 69 6e  z=='E' ){.    in
ab470 74 20 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 20  t esign = 1;.   
ab480 20 69 6e 74 20 65 76 61 6c 20 3d 20 30 3b 0a 20   int eval = 0;. 
ab490 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59     LONGDOUBLE_TY
ab4a0 50 45 20 73 63 61 6c 65 20 3d 20 31 2e 30 3b 0a  PE scale = 1.0;.
ab4b0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28      z++;.    if(
ab4c0 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20   *z=='-' ){.    
ab4d0 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20    esign = -1;.  
ab4e0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c      z++;.    }el
ab4f0 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29  se if( *z=='+' )
ab500 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  {.      z++;.   
ab510 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71   }.    while( sq
ab520 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29  lite3Isdigit(*z)
ab530 20 29 7b 0a 20 20 20 20 20 20 65 76 61 6c 20 3d   ){.      eval =
ab540 20 65 76 61 6c 2a 31 30 20 2b 20 2a 7a 20 2d 20   eval*10 + *z - 
ab550 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  '0';.      z++;.
ab560 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28      }.    while(
ab570 20 65 76 61 6c 3e 3d 36 34 20 29 7b 20 73 63 61   eval>=64 ){ sca
ab580 6c 65 20 2a 3d 20 31 2e 30 65 2b 36 34 3b 20 65  le *= 1.0e+64; e
ab590 76 61 6c 20 2d 3d 20 36 34 3b 20 7d 0a 20 20 20  val -= 64; }.   
ab5a0 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31 36   while( eval>=16
ab5b0 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30   ){ scale *= 1.0
ab5c0 65 2b 31 36 3b 20 65 76 61 6c 20 2d 3d 20 31 36  e+16; eval -= 16
ab5d0 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65  ; }.    while( e
ab5e0 76 61 6c 3e 3d 34 20 29 7b 20 73 63 61 6c 65 20  val>=4 ){ scale 
ab5f0 2a 3d 20 31 2e 30 65 2b 34 3b 20 65 76 61 6c 20  *= 1.0e+4; eval 
ab600 2d 3d 20 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c  -= 4; }.    whil
ab610 65 28 20 65 76 61 6c 3e 3d 31 20 29 7b 20 73 63  e( eval>=1 ){ sc
ab620 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65  ale *= 1.0e+1; e
ab630 76 61 6c 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20  val -= 1; }.    
ab640 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20  if( esign<0 ){. 
ab650 20 20 20 20 20 76 31 20 2f 3d 20 73 63 61 6c 65       v1 /= scale
ab660 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
ab670 20 20 20 76 31 20 2a 3d 20 73 63 61 6c 65 3b 0a     v1 *= scale;.
ab680 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65      }.  }.  *pRe
ab690 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65 29 28  sult = (double)(
ab6a0 73 69 67 6e 3c 30 20 3f 20 2d 76 31 20 3a 20 76  sign<0 ? -v1 : v
ab6b0 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e  1);.  return (in
ab6c0 74 29 28 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a  t)(z - zBegin);.
ab6d0 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73  #else.  return s
ab6e0 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20  qlite3Atoi64(z, 
ab6f0 70 52 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66  pResult);.#endif
ab700 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
ab710 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a  FLOATING_POINT *
ab720 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61  /.}../*.** Compa
ab730 72 65 20 74 68 65 20 31 39 2d 63 68 61 72 61 63  re the 19-charac
ab740 74 65 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20  ter string zNum 
ab750 61 67 61 69 6e 73 74 20 74 68 65 20 74 65 78 74  against the text
ab760 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a   representation.
ab770 2a 2a 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20  ** value 2^63:  
ab780 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
ab790 38 30 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67  808.  Return neg
ab7a0 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20  ative, zero, or 
ab7b0 70 6f 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a  positive.** if z
ab7c0 4e 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e  Num is less than
ab7d0 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  , equal to, or g
ab7e0 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
ab7f0 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e  string..**.** Un
ab800 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68  like memcmp() th
ab810 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75  is routine is gu
ab820 61 72 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75  aranteed to retu
ab830 72 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  rn the differenc
ab840 65 0a 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75  e.** in the valu
ab850 65 73 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64  es of the last d
ab860 69 67 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79  igit if the only
ab870 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 69   difference is i
ab880 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69  n the.** last di
ab890 67 69 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78  git.  So, for ex
ab8a0 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20  ample,.**.**    
ab8b0 20 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28    compare2pow63(
ab8c0 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37  "922337203685477
ab8d0 35 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c  5800").**.** wil
ab8e0 6c 20 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a  l return -8..*/.
ab8f0 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61  static int compa
ab900 72 65 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63  re2pow63(const c
ab910 68 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e  har *zNum){.  in
ab920 74 20 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d  t c;.  c = memcm
ab930 70 28 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30  p(zNum,"92233720
ab940 33 36 38 35 34 37 37 35 38 30 22 2c 31 38 29 3b  3685477580",18);
ab950 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20  .  if( c==0 ){. 
ab960 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20     c = zNum[18] 
ab970 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74  - '8';.  }.  ret
ab980 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn c;.}.../*.**
ab990 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
ab9a0 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74  zNum is a 64-bit
ab9b0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
ab9c0 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65  and write.** the
ab9d0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e   value of the in
ab9e0 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d  teger into *pNum
ab9f0 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f  .  If zNum is no
aba00 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  t an integer.** 
aba10 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  or is an integer
aba20 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72   that is too lar
aba30 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73  ge to be express
aba40 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c  ed with 64 bits,
aba50 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20  .** then return 
aba60 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  false..**.** Whe
aba70 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  n this routine w
aba80 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72  as originally wr
aba90 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77  itten it dealt w
abaa0 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62  ith only.** 32-b
abab0 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20  it numbers.  At 
abac0 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61  that time, it wa
abad0 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68  s much faster th
abae0 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29  an the.** atoi()
abaf0 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65   library routine
abb00 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a   in RedHat 7.2..
abb10 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
abb20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f  E int sqlite3Ato
abb30 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  i64(const char *
abb40 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29  zNum, i64 *pNum)
abb50 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20  {.  i64 v = 0;. 
abb60 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20   int neg;.  int 
abb70 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68  i, c;.  const ch
abb80 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68  ar *zStart;.  wh
abb90 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70  ile( sqlite3Issp
abba0 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75  ace(*zNum) ) zNu
abbb0 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d  m++;.  if( *zNum
abbc0 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67  =='-' ){.    neg
abbd0 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b   = 1;.    zNum++
abbe0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a  ;.  }else if( *z
abbf0 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20  Num=='+' ){.    
abc00 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75  neg = 0;.    zNu
abc10 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  m++;.  }else{.  
abc20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20    neg = 0;.  }. 
abc30 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a   zStart = zNum;.
abc40 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d    while( zNum[0]
abc50 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b  =='0' ){ zNum++;
abc60 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20   } /* Skip over 
abc70 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54  leading zeros. T
abc80 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20  icket #2454 */. 
abc90 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75   for(i=0; (c=zNu
abca0 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c  m[i])>='0' && c<
abcb0 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ='9'; i++){.    
abcc0 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27  v = v*10 + c - '
abcd0 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20  0';.  }.  *pNum 
abce0 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a  = neg ? -v : v;.
abcf0 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 69    if( c!=0 || (i
abd00 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a  ==0 && zStart==z
abd10 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a  Num) || i>19 ){.
abd20 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65      /* zNum is e
abd30 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73  mpty or contains
abd40 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78   non-numeric tex
abd50 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20  t or is longer. 
abd60 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69     ** than 19 di
abd70 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72 61  gits (thus guara
abd80 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 73  nting that it is
abd90 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20   too large) */. 
abda0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
abdb0 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b  else if( i<19 ){
abdc0 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61  .    /* Less tha
abdd0 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20  n 19 digits, so 
abde0 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20  we know that it 
abdf0 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 20  fits in 64 bits 
abe00 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  */.    return 1;
abe10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
abe20 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72   19-digit number
abe30 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72  s must be no lar
abe40 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32  ger than 9223372
abe50 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66 20  036854775807 if 
abe60 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20  positive.    ** 
abe70 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35 34  or 9223372036854
abe80 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 69  775808 if negati
abe90 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39  ve.  Note that 9
abea0 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35 38  2233720368546658
abeb0 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36  08.    ** is 2^6
abec0 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  3. */.    return
abed0 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a   compare2pow63(z
abee0 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a  Num)<neg;.  }.}.
abef0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e  ./*.** The strin
abf00 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74  g zNum represent
abf10 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 54  s an integer.  T
abf20 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 73 6f  here might be so
abf30 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f  me other.** info
abf40 72 6d 61 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e  rmation followin
abf50 67 20 74 68 65 20 69 6e 74 65 67 65 72 20 74 6f  g the integer to
abf60 6f 2c 20 62 75 74 20 74 68 61 74 20 70 61 72 74  o, but that part
abf70 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20   is ignored..** 
abf80 49 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 74  If the integer t
abf90 68 61 74 20 74 68 65 20 70 72 65 66 69 78 20 6f  hat the prefix o
abfa0 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74  f zNum represent
abfb0 73 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a  s will fit in a.
abfc0 2a 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  ** 64-bit signed
abfd0 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
abfe0 20 54 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73   TRUE.  Otherwis
abff0 65 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a  e return FALSE..
ac000 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
ac010 6e 65 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45  ne returns FALSE
ac020 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20   for the string 
ac030 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37  -922337203685477
ac040 35 38 30 38 20 65 76 65 6e 20 74 68 61 74 0a 2a  5808 even that.*
ac050 2a 20 74 68 61 74 20 6e 75 6d 62 65 72 20 77 69  * that number wi
ac060 6c 6c 2c 20 69 6e 20 74 68 65 6f 72 79 20 66 69  ll, in theory fi
ac070 74 20 69 6e 20 61 20 36 34 2d 62 69 74 20 69 6e  t in a 64-bit in
ac080 74 65 67 65 72 2e 20 20 50 6f 73 69 74 69 76 65  teger.  Positive
ac090 0a 2a 2a 20 39 32 32 33 33 37 33 30 33 36 38 35  .** 922337303685
ac0a0 34 37 37 35 38 30 38 20 77 69 6c 6c 20 6e 6f 74  4775808 will not
ac0b0 20 66 69 74 20 69 6e 20 36 34 20 62 69 74 73 2e   fit in 64 bits.
ac0c0 20 20 53 6f 20 69 74 20 73 65 65 6d 73 20 73 61    So it seems sa
ac0d0 66 65 72 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a  fer to return.**
ac0e0 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54   false..*/.SQLIT
ac0f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ac100 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74  lite3FitsIn64Bit
ac110 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  s(const char *zN
ac120 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 29  um, int negFlag)
ac130 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20  {.  int i, c;.  
ac140 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69  int neg = 0;.  i
ac150 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b  f( *zNum=='-' ){
ac160 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20  .    neg = 1;.  
ac170 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73    zNum++;.  }els
ac180 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27  e if( *zNum=='+'
ac190 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a   ){.    zNum++;.
ac1a0 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 46 6c 61    }.  if( negFla
ac1b0 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67 3b  g ) neg = 1-neg;
ac1c0 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d 3d  .  while( *zNum=
ac1d0 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d  ='0' ){.    zNum
ac1e0 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c 65  ++;   /* Skip le
ac1f0 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54 69  ading zeros.  Ti
ac200 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20  cket #2454 */.  
ac210 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d  }.  for(i=0; (c=
ac220 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26  zNum[i])>='0' &&
ac230 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 7d 0a   c<='9'; i++){}.
ac240 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20    if( i<19 ){.  
ac250 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20    /* Guaranteed 
ac260 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20 74  to fit if less t
ac270 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f  han 19 digits */
ac280 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
ac290 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39 20   }else if( i>19 
ac2a0 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e  ){.    /* Guaran
ac2b0 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 62  teed to be too b
ac2c0 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74 68  ig if greater th
ac2d0 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a  an 19 digits */.
ac2e0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
ac2f0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f  }else{.    /* Co
ac300 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32 5e  mpare against 2^
ac310 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  63. */.    retur
ac320 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28  n compare2pow63(
ac330 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d  zNum)<neg;.  }.}
ac340 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20  ../*.** If zNum 
ac350 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e  represents an in
ac360 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c 20  teger that will 
ac370 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c 20  fit in 32-bits, 
ac380 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61  then set.** *pVa
ac390 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74 65  lue to that inte
ac3a0 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ger and return t
ac3b0 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20  rue.  Otherwise 
ac3c0 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a  return false..**
ac3d0 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65  .** Any non-nume
ac3e0 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20 74  ric characters t
ac3f0 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e  hat following zN
ac400 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  um are ignored..
ac410 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66 65  ** This is diffe
ac420 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 65  rent from sqlite
ac430 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68 20  3Atoi64() which 
ac440 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20  requires the.** 
ac450 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f 20  input number to 
ac460 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  be zero-terminat
ac470 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
ac480 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ac490 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20  3GetInt32(const 
ac4a0 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20  char *zNum, int 
ac4b0 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69  *pValue){.  sqli
ac4c0 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a  te_int64 v = 0;.
ac4d0 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e    int i, c;.  in
ac4e0 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28  t neg = 0;.  if(
ac4f0 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b   zNum[0]=='-' ){
ac500 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20  .    neg = 1;.  
ac510 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73    zNum++;.  }els
ac520 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27  e if( zNum[0]=='
ac530 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b  +' ){.    zNum++
ac540 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a  ;.  }.  while( z
ac550 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e  Num[0]=='0' ) zN
ac560 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  um++;.  for(i=0;
ac570 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a 4e   i<11 && (c = zN
ac580 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20  um[i] - '0')>=0 
ac590 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20  && c<=9; i++){. 
ac5a0 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b     v = v*10 + c;
ac5b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c  .  }..  /* The l
ac5c0 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20 72  ongest decimal r
ac5d0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
ac5e0 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67 65   a 32 bit intege
ac5f0 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a 0a  r is 10 digits:.
ac600 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
ac610 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39 30        1234567890
ac620 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d  .  **     2^31 -
ac630 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20 2a  > 2147483648.  *
ac640 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b 0a  /.  if( i>10 ){.
ac650 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
ac660 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 31  }.  if( v-neg>21
ac670 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20 20  47483647 ){.    
ac680 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
ac690 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 76  if( neg ){.    v
ac6a0 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56   = -v;.  }.  *pV
ac6b0 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20  alue = (int)v;. 
ac6c0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
ac6d0 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c 65  .** The variable
ac6e0 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
ac6f0 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20 66  encoding is as f
ac700 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45  ollows:.**.** KE
ac710 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20  Y:.**         A 
ac720 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 20  = 0xxxxxxx    7 
ac730 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
ac740 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
ac750 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78           B = 1xx
ac760 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20  xxxxx    7 bits 
ac770 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20  of data and one 
ac780 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20  flag bit.**     
ac790 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78 78      C = xxxxxxxx
ac7a0 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64 61      8 bits of da
ac7b0 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73  ta.**.**  7 bits
ac7c0 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20   - A.** 14 bits 
ac7d0 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20  - BA.** 21 bits 
ac7e0 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 73  - BBA.** 28 bits
ac7f0 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 69   - BBBA.** 35 bi
ac800 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32  ts - BBBBA.** 42
ac810 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2a   bits - BBBBBA.*
ac820 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42 42  * 49 bits - BBBB
ac830 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d  BBA.** 56 bits -
ac840 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 20   BBBBBBBA.** 64 
ac850 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42 43  bits - BBBBBBBBC
ac860 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  .*/../*.** Write
ac870 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62   a 64-bit variab
ac880 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
ac890 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72  r to memory star
ac8a0 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a  ting at p[0]..**
ac8b0 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 64   The length of d
ac8c0 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20 62  ata write will b
ac8d0 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  e between 1 and 
ac8e0 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e 75  9 bytes.  The nu
ac8f0 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73  mber.** of bytes
ac900 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74 75   written is retu
ac910 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61  rned..**.** A va
ac920 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
ac930 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f  teger consists o
ac940 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69  f the lower 7 bi
ac950 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65 0a  ts of each byte.
ac960 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73  ** for all bytes
ac970 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20 38   that have the 8
ac980 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20 6f  th bit set and o
ac990 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68 65  ne byte with the
ac9a0 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61   8th.** bit clea
ac9b0 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 77  r.  Except, if w
ac9c0 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74 68  e get to the 9th
ac9d0 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65 73   byte, it stores
ac9e0 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62   the full.** 8 b
ac9f0 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20 6c  its and is the l
aca00 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c  ast byte..*/.SQL
aca10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
aca20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
aca30 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
aca40 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e 74  p, u64 v){.  int
aca50 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62   i, j, n;.  u8 b
aca60 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 20  uf[10];.  if( v 
aca70 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30 30  & (((u64)0xff000
aca80 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20  000)<<32) ){.   
aca90 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0a 20   p[8] = (u8)v;. 
acaa0 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20     v >>= 8;.    
acab0 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69  for(i=7; i>=0; i
acac0 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20  --){.      p[i] 
acad0 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37 66  = (u8)((v & 0x7f
acae0 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 20  ) | 0x80);.     
acaf0 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d 0a   v >>= 7;.    }.
acb00 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a 20 20      return 9;.  
acb10 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b 0a 20  }    .  n = 0;. 
acb20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e 2b 2b   do{.    buf[n++
acb30 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78  ] = (u8)((v & 0x
acb40 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20  7f) | 0x80);.   
acb50 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69   v >>= 7;.  }whi
acb60 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 75  le( v!=0 );.  bu
acb70 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20  f[0] &= 0x7f;.  
acb80 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a  assert( n<=9 );.
acb90 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31    for(i=0, j=n-1
acba0 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b  ; j>=0; j--, i++
acbb0 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 75  ){.    p[i] = bu
acbc0 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  f[j];.  }.  retu
acbd0 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn n;.}../*.** T
acbe0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
acbf0 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e 20   faster version 
acc00 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  of sqlite3PutVar
acc10 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a  int() that only.
acc20 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d  ** works for 32-
acc30 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e 74  bit positive int
acc40 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68 20  egers and which 
acc50 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72  is optimized for
acc60 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63  .** the common c
acc70 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74  ase of small int
acc80 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f 20  egers.  A MACRO 
acc90 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 69  version, putVari
acca0 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76  nt32,.** is prov
accb0 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e  ided which inlin
accc0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79  es the single-by
accd0 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f  te case.  All co
acce0 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a  de should use.**
accf0 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69   the MACRO versi
acd00 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63 74  on as this funct
acd10 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20  ion assumes the 
acd20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65  single-byte case
acd30 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20   has.** already 
acd40 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f  been handled..*/
acd50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acd60 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61  int sqlite3PutVa
acd70 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20  rint32(unsigned 
acd80 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b  char *p, u32 v){
acd90 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72 69  .#ifndef putVari
acda0 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26 20  nt32.  if( (v & 
acdb0 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20  ~0x7f)==0 ){.   
acdc0 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72   p[0] = v;.    r
acdd0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e  eturn 1;.  }.#en
acde0 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20 7e  dif.  if( (v & ~
acdf0 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20  0x3fff)==0 ){.  
ace00 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 28 76    p[0] = (u8)((v
ace10 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b 0a 20 20  >>7) | 0x80);.  
ace20 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 20    p[1] = (u8)(v 
ace30 26 20 30 78 37 66 29 3b 0a 20 20 20 20 72 65 74  & 0x7f);.    ret
ace40 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 2;.  }.  ret
ace50 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 56 61  urn sqlite3PutVa
ace60 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a 2f  rint(p, v);.}../
ace70 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d 62  *.** Read a 64-b
ace80 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67  it variable-leng
ace90 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20  th integer from 
acea0 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20  memory starting 
aceb0 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75  at p[0]..** Retu
acec0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
aced0 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68   bytes read.  Th
acee0 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65  e value is store
acef0 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49  d in *v..*/.SQLI
acf00 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71  TE_PRIVATE u8 sq
acf10 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 63  lite3GetVarint(c
acf20 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
acf30 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b 0a  ar *p, u64 *v){.
acf40 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 20    u32 a,b,s;..  
acf50 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20  a = *p;.  /* a: 
acf60 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p0 (unmasked) */
acf70 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29  .  if (!(a&0x80)
acf80 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 61  ).  {.    *v = a
acf90 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
acfa0 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20    }..  p++;.  b 
acfb0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31  = *p;.  /* b: p1
acfc0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
acfd0 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a   if (!(b&0x80)).
acfe0 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 37    {.    a &= 0x7
acff0 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b  f;.    a = a<<7;
ad000 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
ad010 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74   *v = a;.    ret
ad020 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b  urn 2;.  }..  p+
ad030 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a  +;.  a = a<<14;.
ad040 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    a |= *p;.  /* 
ad050 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28  a: p0<<14 | p2 (
ad060 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
ad070 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
ad080 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66  {.    a &= (0x7f
ad090 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
ad0a0 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20    b &= 0x7f;.   
ad0b0 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61   b = b<<7;.    a
ad0c0 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20   |= b;.    *v = 
ad0d0 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b  a;.    return 3;
ad0e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 20  .  }..  /* CSE1 
ad0f0 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  from below */.  
ad100 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
ad110 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20  (0x7f);.  p++;. 
ad120 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20   b = b<<14;.  b 
ad130 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70  |= *p;.  /* b: p
ad140 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61  1<<14 | p3 (unma
ad150 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
ad160 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (b&0x80)).  {.  
ad170 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    b &= (0x7f<<14
ad180 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a  )|(0x7f);.    /*
ad190 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a   moved CSE1 up *
ad1a0 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30  /.    /* a &= (0
ad1b0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
ad1c0 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37   */.    a = a<<7
ad1d0 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
ad1e0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
ad1f0 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 4;.  }..  /
ad200 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32  * a: p0<<14 | p2
ad210 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f   (masked) */.  /
ad220 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33  * b: p1<<14 | p3
ad230 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
ad240 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 66 20 70   /* 1:save off p
ad250 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c  0<<21 | p1<<14 |
ad260 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73   p2<<7 | p3 (mas
ad270 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76  ked) */.  /* mov
ad280 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20  ed CSE1 up */.  
ad290 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31  /* a &= (0x7f<<1
ad2a0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20  4)|(0x7f); */.  
ad2b0 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  b &= (0x7f<<14)|
ad2c0 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61 3b  (0x7f);.  s = a;
ad2d0 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 31 34 20  .  /* s: p0<<14 
ad2e0 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f  | p2 (masked) */
ad2f0 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61  ..  p++;.  a = a
ad300 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b  <<14;.  a |= *p;
ad310 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20  .  /* a: p0<<28 
ad320 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75  | p2<<14 | p4 (u
ad330 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
ad340 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b   (!(a&0x80)).  {
ad350 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73  .    /* we can s
ad360 6b 69 70 20 74 68 65 73 65 20 63 61 75 73 65 20  kip these cause 
ad370 74 68 65 79 20 77 65 72 65 20 28 65 66 66 65 63  they were (effec
ad380 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f  tively) done abo
ad390 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 73  ve in calc'ing s
ad3a0 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20   */.    /* a &= 
ad3b0 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66  (0x7f<<28)|(0x7f
ad3c0 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f  <<14)|(0x7f); */
ad3d0 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78  .    /* b &= (0x
ad3e0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20  7f<<14)|(0x7f); 
ad3f0 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b  */.    b = b<<7;
ad400 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
ad410 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20   s = s>>18;.    
ad420 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33  *v = ((u64)s)<<3
ad430 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72  2 | a;.    retur
ad440 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 32  n 5;.  }..  /* 2
ad450 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 31  :save off p0<<21
ad460 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c   | p1<<14 | p2<<
ad470 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20  7 | p3 (masked) 
ad480 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a 20  */.  s = s<<7;. 
ad490 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73 3a   s |= b;.  /* s:
ad4a0 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34   p0<<21 | p1<<14
ad4b0 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d   | p2<<7 | p3 (m
ad4c0 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b  asked) */..  p++
ad4d0 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20  ;.  b = b<<14;. 
ad4e0 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62   b |= *p;.  /* b
ad4f0 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c 31  : p1<<28 | p3<<1
ad500 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65 64  4 | p5 (unmasked
ad510 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30  ) */.  if (!(b&0
ad520 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a  x80)).  {.    /*
ad530 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 69   we can skip thi
ad540 73 20 63 61 75 73 65 20 69 74 20 77 61 73 20 28  s cause it was (
ad550 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e  effectively) don
ad560 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27  e above in calc'
ad570 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20  ing s */.    /* 
ad580 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c  b &= (0x7f<<28)|
ad590 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
ad5a0 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28  ); */.    a &= (
ad5b0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
ad5c0 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a  ;.    a = a<<7;.
ad5d0 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20      a |= b;.    
ad5e0 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a  s = s>>18;.    *
ad5f0 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32  v = ((u64)s)<<32
ad600 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   | a;.    return
ad610 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a   6;.  }..  p++;.
ad620 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61    a = a<<14;.  a
ad630 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20   |= *p;.  /* a: 
ad640 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c 31 34 20  p2<<28 | p4<<14 
ad650 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p6 (unmasked) 
ad660 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  */.  if (!(a&0x8
ad670 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d  0)).  {.    a &=
ad680 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37   (0x1f<<28)|(0x7
ad690 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
ad6a0 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31     b &= (0x7f<<1
ad6b0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
ad6c0 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = b<<7;.    a |
ad6d0 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e  = b;.    s = s>>
ad6e0 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75  11;.    *v = ((u
ad6f0 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20  64)s)<<32 | a;. 
ad700 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d     return 7;.  }
ad710 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f 6d  ..  /* CSE2 from
ad720 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d   below */.  a &=
ad730 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
ad740 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d  f);.  p++;.  b =
ad750 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a   b<<14;.  b |= *
ad760 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 3c 3c 32  p;.  /* b: p3<<2
ad770 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 70 37 20  8 | p5<<14 | p7 
ad780 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
ad790 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
ad7a0 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 31   {.    b &= (0x1
ad7b0 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34  f<<28)|(0x7f<<14
ad7c0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a  )|(0x7f);.    /*
ad7d0 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a   moved CSE2 up *
ad7e0 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30  /.    /* a &= (0
ad7f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
ad800 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37   */.    a = a<<7
ad810 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
ad820 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 20 20 20    s = s>>4;.    
ad830 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33  *v = ((u64)s)<<3
ad840 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72  2 | a;.    retur
ad850 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b  n 8;.  }..  p++;
ad860 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0a 20 20  .  a = a<<15;.  
ad870 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a  a |= *p;.  /* a:
ad880 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31 35   p4<<29 | p6<<15
ad890 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64 29   | p8 (unmasked)
ad8a0 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64 20   */..  /* moved 
ad8b0 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 2f 2a 20  CSE2 up */.  /* 
ad8c0 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 39 29 7c  a &= (0x7f<<29)|
ad8d0 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 78 66 66  (0x7f<<15)|(0xff
ad8e0 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78  ); */.  b &= (0x
ad8f0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
ad900 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 20 61 20    b = b<<8;.  a 
ad910 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 73 3c 3c  |= b;..  s = s<<
ad920 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b 0a  4;.  b = p[-4];.
ad930 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 62    b &= 0x7f;.  b
ad940 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d 20   = b>>3;.  s |= 
ad950 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 75 36 34  b;..  *v = ((u64
ad960 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20 20  )s)<<32 | a;..  
ad970 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a  return 9;.}../*.
ad980 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74  ** Read a 32-bit
ad990 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
ad9a0 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65   integer from me
ad9b0 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74  mory starting at
ad9c0 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e   p[0]..** Return
ad9d0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
ad9e0 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65 20  ytes read.  The 
ad9f0 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20  value is stored 
ada00 69 6e 20 2a 76 2e 0a 2a 2a 20 41 20 4d 41 43 52  in *v..** A MACR
ada10 4f 20 76 65 72 73 69 6f 6e 2c 20 67 65 74 56 61  O version, getVa
ada20 72 69 6e 74 33 32 2c 20 69 73 20 70 72 6f 76 69  rint32, is provi
ada30 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65  ded which inline
ada40 73 20 74 68 65 20 0a 2a 2a 20 73 69 6e 67 6c 65  s the .** single
ada50 2d 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c  -byte case.  All
ada60 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65   code should use
ada70 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69   the MACRO versi
ada80 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 69 73 20 66  on as .** this f
ada90 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20  unction assumes 
adaa0 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  the single-byte 
adab0 63 61 73 65 20 68 61 73 20 61 6c 72 65 61 64 79  case has already
adac0 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a   been handled..*
adad0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
adae0 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61   u8 sqlite3GetVa
adaf0 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73  rint32(const uns
adb00 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75  igned char *p, u
adb10 33 32 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c  32 *v){.  u32 a,
adb20 62 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20  b;..  a = *p;.  
adb30 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b  /* a: p0 (unmask
adb40 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67  ed) */.#ifndef g
adb50 65 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 20  etVarint32.  if 
adb60 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(a&0x80)).  {.
adb70 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20      *v = a;.    
adb80 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
adb90 6e 64 69 66 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62  ndif..  p++;.  b
adba0 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70   = *p;.  /* b: p
adbb0 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  1 (unmasked) */.
adbc0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
adbd0 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78  .  {.    a &= 0x
adbe0 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37  7f;.    a = a<<7
adbf0 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62  ;.    *v = a | b
adc00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a  ;.    return 2;.
adc10 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20    }..  p++;.  a 
adc20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20  = a<<14;.  a |= 
adc30 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c  *p;.  /* a: p0<<
adc40 31 34 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65  14 | p2 (unmaske
adc50 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26  d) */.  if (!(a&
adc60 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61  0x80)).  {.    a
adc70 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
adc80 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20  0x7f);.    b &= 
adc90 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c  0x7f;.    b = b<
adca0 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c  <7;.    *v = a |
adcb0 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33   b;.    return 3
adcc0 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20  ;.  }..  p++;.  
adcd0 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c  b = b<<14;.  b |
adce0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31  = *p;.  /* b: p1
adcf0 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73  <<14 | p3 (unmas
add00 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
add10 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
add20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   b &= (0x7f<<14)
add30 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 26  |(0x7f);.    a &
add40 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
add50 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c  7f);.    a = a<<
add60 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20  7;.    *v = a | 
add70 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b  b;.    return 4;
add80 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61  .  }..  p++;.  a
add90 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d   = a<<14;.  a |=
adda0 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c   *p;.  /* a: p0<
addb0 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70  <28 | p2<<14 | p
addc0 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  4 (unmasked) */.
addd0 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29    if (!(a&0x80))
adde0 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30  .  {.    a &= (0
addf0 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c  x1f<<28)|(0x7f<<
ade00 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
ade10 62 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c  b &= (0x1f<<28)|
ade20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
ade30 29 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b  );.    b = b<<7;
ade40 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b  .    *v = a | b;
ade50 0a 20 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20  .    return 5;. 
ade60 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20   }..  /* We can 
ade70 6f 6e 6c 79 20 72 65 61 63 68 20 74 68 69 73 20  only reach this 
ade80 70 6f 69 6e 74 20 77 68 65 6e 20 72 65 61 64 69  point when readi
ade90 6e 67 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  ng a corrupt dat
adea0 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e  abase.  ** file.
adeb0 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 77    In that case w
adec0 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79  e are not in any
aded0 20 68 75 72 72 79 2e 20 20 55 73 65 20 74 68 65   hurry.  Use the
adee0 20 28 72 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a   (relatively.  *
adef0 2a 20 73 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d  * slow) general-
adf00 70 75 72 70 6f 73 65 20 73 71 6c 69 74 65 33 47  purpose sqlite3G
adf10 65 74 56 61 72 69 6e 74 28 29 20 72 6f 75 74 69  etVarint() routi
adf20 6e 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68  ne to extract th
adf30 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f  e.  ** value. */
adf40 0a 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34  .  {.    u64 v64
adf50 3b 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20  ;.    u8 n;..   
adf60 20 70 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d   p -= 4;.    n =
adf70 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
adf80 74 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20  t(p, &v64);.    
adf90 61 73 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e  assert( n>5 && n
adfa0 3c 3d 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20  <=9 );.    *v = 
adfb0 28 75 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65  (u32)v64;.    re
adfc0 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn n;.  }.}../
adfd0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
adfe0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
adff0 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65  that will be nee
ae000 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ded to store the
ae010 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 74   given.** 64-bit
ae020 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c   integer..*/.SQL
ae030 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ae040 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
ae050 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69  (u64 v){.  int i
ae060 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20   = 0;.  do{.    
ae070 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37  i++;.    v >>= 7
ae080 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30  ;.  }while( v!=0
ae090 20 26 26 20 69 3c 39 20 29 3b 0a 20 20 72 65 74   && i<9 );.  ret
ae0a0 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn i;.}.../*.**
ae0b0 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61   Read or write a
ae0c0 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65   four-byte big-e
ae0d0 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61  ndian integer va
ae0e0 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  lue..*/.SQLITE_P
ae0f0 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
ae100 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74  e3Get4byte(const
ae110 20 75 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72   u8 *p){.  retur
ae120 6e 20 28 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28  n (p[0]<<24) | (
ae130 70 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32  p[1]<<16) | (p[2
ae140 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a  ]<<8) | p[3];.}.
ae150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
ae160 6f 69 64 20 73 71 6c 69 74 65 33 50 75 74 34 62  oid sqlite3Put4b
ae170 79 74 65 28 75 6e 73 69 67 6e 65 64 20 63 68 61  yte(unsigned cha
ae180 72 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 20 20  r *p, u32 v){.  
ae190 70 5b 30 5d 20 3d 20 28 75 38 29 28 76 3e 3e 32  p[0] = (u8)(v>>2
ae1a0 34 29 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 75 38  4);.  p[1] = (u8
ae1b0 29 28 76 3e 3e 31 36 29 3b 0a 20 20 70 5b 32 5d  )(v>>16);.  p[2]
ae1c0 20 3d 20 28 75 38 29 28 76 3e 3e 38 29 3b 0a 20   = (u8)(v>>8);. 
ae1d0 20 70 5b 33 5d 20 3d 20 28 75 38 29 76 3b 0a 7d   p[3] = (u8)v;.}
ae1e0 0a 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ....#if !defined
ae1f0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f  (SQLITE_OMIT_BLO
ae200 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65  B_LITERAL) || de
ae210 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53  fined(SQLITE_HAS
ae220 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72  _CODEC)./*.** Tr
ae230 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65  anslate a single
ae240 20 62 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74   byte of Hex int
ae250 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  o an integer..**
ae260 20 54 68 69 73 20 72 6f 75 74 69 6e 65 6e 20 6f   This routinen o
ae270 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 68 20 72  nly works if h r
ae280 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64  eally is a valid
ae290 20 68 65 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20   hexadecimal.** 
ae2a0 63 68 61 72 61 63 74 65 72 3a 20 20 30 2e 2e 39  character:  0..9
ae2b0 61 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74  a..fA..F.*/.stat
ae2c0 69 63 20 75 38 20 68 65 78 54 6f 49 6e 74 28 69  ic u8 hexToInt(i
ae2d0 6e 74 20 68 29 7b 0a 20 20 61 73 73 65 72 74 28  nt h){.  assert(
ae2e0 20 28 68 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27   (h>='0' && h<='
ae2f0 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20  9') ||  (h>='a' 
ae300 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28  && h<='f') ||  (
ae310 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d 27 46 27  h>='A' && h<='F'
ae320 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  ) );.#ifdef SQLI
ae330 54 45 5f 41 53 43 49 49 0a 20 20 68 20 2b 3d 20  TE_ASCII.  h += 
ae340 39 2a 28 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65  9*(1&(h>>6));.#e
ae350 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
ae360 54 45 5f 45 42 43 44 49 43 0a 20 20 68 20 2b 3d  TE_EBCDIC.  h +=
ae370 20 39 2a 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a   9*(1&~(h>>4));.
ae380 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
ae390 28 75 38 29 28 68 20 26 20 30 78 66 29 3b 0a 7d  (u8)(h & 0xf);.}
ae3a0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49  .#endif /* !SQLI
ae3b0 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
ae3c0 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48  ERAL || SQLITE_H
ae3d0 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 23 69 66  AS_CODEC */..#if
ae3e0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
ae3f0 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52  _OMIT_BLOB_LITER
ae400 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  AL) || defined(S
ae410 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29  QLITE_HAS_CODEC)
ae420 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
ae430 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c 20 6f 66   BLOB literal of
ae440 20 74 68 65 20 66 6f 72 6d 20 22 78 27 68 68 68   the form "x'hhh
ae450 68 68 68 27 22 20 69 6e 74 6f 20 69 74 73 20 62  hhh'" into its b
ae460 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75 65 2e 20  inary.** value. 
ae470 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
ae480 72 20 74 6f 20 69 74 73 20 62 69 6e 61 72 79 20  r to its binary 
ae490 76 61 6c 75 65 2e 20 20 53 70 61 63 65 20 74 6f  value.  Space to
ae4a0 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 62 69 6e   hold the.** bin
ae4b0 61 72 79 20 76 61 6c 75 65 20 68 61 73 20 62 65  ary value has be
ae4c0 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  en obtained from
ae4d0 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74   malloc and must
ae4e0 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20   be freed by.** 
ae4f0 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  the calling rout
ae500 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ine..*/.SQLITE_P
ae510 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
ae520 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71  ite3HexToBlob(sq
ae530 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
ae540 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
ae550 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b  {.  char *zBlob;
ae560 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c  .  int i;..  zBl
ae570 6f 62 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ob = (char *)sql
ae580 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
ae590 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b 0a 20 20  db, n/2 + 1);.  
ae5a0 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42 6c 6f 62  n--;.  if( zBlob
ae5b0 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
ae5c0 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20 20   i<n; i+=2){.   
ae5d0 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20     zBlob[i/2] = 
ae5e0 28 68 65 78 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c  (hexToInt(z[i])<
ae5f0 3c 34 29 20 7c 20 68 65 78 54 6f 49 6e 74 28 7a  <4) | hexToInt(z
ae600 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  [i+1]);.    }.  
ae610 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30    zBlob[i/2] = 0
ae620 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
ae630 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Blob;.}.#endif /
ae640 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  * !SQLITE_OMIT_B
ae650 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53  LOB_LITERAL || S
ae660 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20  QLITE_HAS_CODEC 
ae670 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  */.../*.** Chang
ae680 65 20 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67  e the sqlite.mag
ae690 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d  ic from SQLITE_M
ae6a0 41 47 49 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c  AGIC_OPEN to SQL
ae6b0 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a  ITE_MAGIC_BUSY..
ae6c0 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  ** Return an err
ae6d0 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66  or (non-zero) if
ae6e0 20 74 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e   the magic was n
ae6f0 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  ot SQLITE_MAGIC_
ae700 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69  OPEN.** when thi
ae710 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
ae720 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  led..**.** This 
ae730 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
ae740 64 20 77 68 65 6e 20 65 6e 74 65 72 69 6e 67 20  d when entering 
ae750 61 6e 20 53 51 4c 69 74 65 20 41 50 49 2e 20 20  an SQLite API.  
ae760 54 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  The SQLITE_MAGIC
ae770 5f 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69  _OPEN.** value i
ae780 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68  ndicates that th
ae790 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
ae7a0 63 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74  ction passed int
ae7b0 6f 20 74 68 65 20 41 50 49 20 69 73 0a 2a 2a 20  o the API is.** 
ae7c0 6f 70 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20  open and is not 
ae7d0 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 61 6e  being used by an
ae7e0 6f 74 68 65 72 20 74 68 72 65 61 64 2e 20 20 42  other thread.  B
ae7f0 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 76  y changing the v
ae800 61 6c 75 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54  alue.** to SQLIT
ae810 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 77 65 20  E_MAGIC_BUSY we 
ae820 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68  indicate that th
ae830 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
ae840 69 6e 20 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74  in use..** sqlit
ae850 65 33 53 61 66 65 74 79 4f 66 66 28 29 20 62 65  e3SafetyOff() be
ae860 6c 6f 77 20 77 69 6c 6c 20 63 68 61 6e 67 65 20  low will change 
ae870 74 68 65 20 76 61 6c 75 65 20 62 61 63 6b 20 74  the value back t
ae880 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  o SQLITE_MAGIC_O
ae890 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  PEN.** when the 
ae8a0 41 50 49 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a  API exits. .**.*
ae8b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
ae8c0 73 20 61 20 61 74 74 65 6d 70 74 20 74 6f 20 64  s a attempt to d
ae8d0 65 74 65 63 74 20 69 66 20 74 77 6f 20 74 68 72  etect if two thr
ae8e0 65 61 64 73 20 75 73 65 20 74 68 65 0a 2a 2a 20  eads use the.** 
ae8f0 73 61 6d 65 20 73 71 6c 69 74 65 2a 20 70 6f 69  same sqlite* poi
ae900 6e 74 65 72 20 61 74 20 74 68 65 20 73 61 6d 65  nter at the same
ae910 20 74 69 6d 65 2e 20 20 54 68 65 72 65 20 69 73   time.  There is
ae920 20 61 20 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64   a race .** cond
ae930 69 74 69 6f 6e 20 73 6f 20 69 74 20 69 73 20 70  ition so it is p
ae940 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65  ossible that the
ae950 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 64 65   error is not de
ae960 74 65 63 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75  tected..** But u
ae970 73 75 61 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c  sually the probl
ae980 65 6d 20 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e  em will be seen.
ae990 20 20 54 68 65 20 72 65 73 75 6c 74 20 77 69 6c    The result wil
ae9a0 6c 20 62 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  l be an.** error
ae9b0 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73   which can be us
ae9c0 65 64 20 74 6f 20 64 65 62 75 67 20 74 68 65 20  ed to debug the 
ae9d0 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 61 74  application that
ae9e0 20 69 73 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c   is.** using SQL
ae9f0 69 74 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e  ite incorrectly.
aea00 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32  .**.** Ticket #2
aea10 30 32 3a 20 20 49 66 20 64 62 2d 3e 6d 61 67 69  02:  If db->magi
aea20 63 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 64  c is not a valid
aea30 20 6f 70 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b   open value, tak
aea40 65 20 63 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f  e care not.** to
aea50 20 6d 6f 64 69 66 79 20 74 68 65 20 64 62 20 73   modify the db s
aea60 74 72 75 63 74 75 72 65 20 61 74 20 61 6c 6c 2e  tructure at all.
aea70 20 20 49 74 20 63 6f 75 6c 64 20 62 65 20 74 68    It could be th
aea80 61 74 20 64 62 20 69 73 20 61 20 73 74 61 6c 65  at db is a stale
aea90 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e  .** pointer.  In
aeaa0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74   other words, it
aeab0 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74   could be that t
aeac0 68 65 72 65 20 68 61 73 20 62 65 65 6e 20 61 20  here has been a 
aead0 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f  prior.** call to
aeae0 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64   sqlite3_close(d
aeaf0 62 29 20 61 6e 64 20 64 62 20 68 61 73 20 62 65  b) and db has be
aeb00 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20  en deallocated. 
aeb10 20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f   And we do.** no
aeb20 74 20 77 61 6e 74 20 74 6f 20 77 72 69 74 65 20  t want to write 
aeb30 69 6e 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64  into deallocated
aeb40 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64   memory..*/.#ifd
aeb50 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
aeb60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
aeb70 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
aeb80 4f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  On(sqlite3 *db){
aeb90 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63  .  if( db->magic
aeba0 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  ==SQLITE_MAGIC_O
aebb0 50 45 4e 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  PEN ){.    db->m
aebc0 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
aebd0 47 49 43 5f 42 55 53 59 3b 0a 20 20 20 20 61 73  GIC_BUSY;.    as
aebe0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
aebf0 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
aec00 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ex) );.    retur
aec10 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 0;.  }else if(
aec20 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49   db->magic==SQLI
aec30 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b  TE_MAGIC_BUSY ){
aec40 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  .    db->magic =
aec50 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52   SQLITE_MAGIC_ER
aec60 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e  ROR;.    db->u1.
aec70 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20  isInterrupted = 
aec80 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
aec90 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1;.}.#endif../*.
aeca0 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6d 61  ** Change the ma
aecb0 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f  gic from SQLITE_
aecc0 4d 41 47 49 43 5f 42 55 53 59 20 74 6f 20 53 51  MAGIC_BUSY to SQ
aecd0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e  LITE_MAGIC_OPEN.
aece0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72  .** Return an er
aecf0 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69  ror (non-zero) i
aed00 66 20 74 68 65 20 6d 61 67 69 63 20 77 61 73 20  f the magic was 
aed10 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  not SQLITE_MAGIC
aed20 5f 42 55 53 59 0a 2a 2a 20 77 68 65 6e 20 74 68  _BUSY.** when th
aed30 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
aed40 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  lled..*/.#ifdef 
aed50 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
aed60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
aed70 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
aed80 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
aed90 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d   if( db->magic==
aeda0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
aedb0 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67  Y ){.    db->mag
aedc0 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49  ic = SQLITE_MAGI
aedd0 43 5f 4f 50 45 4e 3b 0a 20 20 20 20 61 73 73 65  C_OPEN;.    asse
aede0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
aedf0 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
aee00 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) );.    return 
aee10 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
aee20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
aee30 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a  TE_MAGIC_ERROR;.
aee40 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74      db->u1.isInt
aee50 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20  errupted = 1;.  
aee60 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
aee70 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
aee80 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75  Check to make su
aee90 72 65 20 77 65 20 68 61 76 65 20 61 20 76 61 6c  re we have a val
aeea0 69 64 20 64 62 20 70 6f 69 6e 74 65 72 2e 20 20  id db pointer.  
aeeb0 54 68 69 73 20 74 65 73 74 20 69 73 20 6e 6f 74  This test is not
aeec0 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75  .** foolproof bu
aeed0 74 20 69 74 20 64 6f 65 73 20 70 72 6f 76 69 64  t it does provid
aeee0 65 20 73 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f  e some measure o
aeef0 66 20 70 72 6f 74 65 63 74 69 6f 6e 20 61 67 61  f protection aga
aef00 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f  inst.** misuse o
aef10 66 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  f the interface 
aef20 73 75 63 68 20 61 73 20 70 61 73 73 69 6e 67 20  such as passing 
aef30 69 6e 20 64 62 20 70 6f 69 6e 74 65 72 73 20 74  in db pointers t
aef40 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20  hat are.** NULL 
aef50 6f 72 20 77 68 69 63 68 20 68 61 76 65 20 62 65  or which have be
aef60 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c  en previously cl
aef70 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 72  osed.  If this r
aef80 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a  outine returns.*
aef90 2a 20 31 20 69 74 20 6d 65 61 6e 73 20 74 68 61  * 1 it means tha
aefa0 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72  t the db pointer
aefb0 20 69 73 20 76 61 6c 69 64 20 61 6e 64 20 30 20   is valid and 0 
aefc0 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74  if it should not
aefd0 20 62 65 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e   be.** dereferen
aefe0 63 65 64 20 66 6f 72 20 61 6e 79 20 72 65 61 73  ced for any reas
aeff0 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  on.  The calling
af000 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
af010 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54   invoke.** SQLIT
af020 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65 64 69 61  E_MISUSE immedia
af030 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  tely..**.** sqli
af040 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b  te3SafetyCheckOk
af050 28 29 20 72 65 71 75 69 72 65 73 20 74 68 61 74  () requires that
af060 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20   the db pointer 
af070 62 65 20 76 61 6c 69 64 20 66 6f 72 0a 2a 2a 20  be valid for.** 
af080 75 73 65 2e 20 20 73 71 6c 69 74 65 33 53 61 66  use.  sqlite3Saf
af090 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b  etyCheckSickOrOk
af0a0 28 29 20 61 6c 6c 6f 77 73 20 61 20 64 62 20 70  () allows a db p
af0b0 6f 69 6e 74 65 72 20 74 68 61 74 20 66 61 69 6c  ointer that fail
af0c0 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72  ed to.** open pr
af0d0 6f 70 65 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f  operly and is no
af0e0 74 20 66 69 74 20 66 6f 72 20 67 65 6e 65 72 61  t fit for genera
af0f0 6c 20 75 73 65 20 62 75 74 20 77 68 69 63 68 20  l use but which 
af100 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20 61  can be.** used a
af110 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f  s an argument to
af120 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
af130 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f  ) or sqlite3_clo
af140 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  se()..*/.SQLITE_
af150 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
af160 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b  te3SafetyCheckOk
af170 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
af180 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 69 66   u32 magic;.  if
af190 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( db==0 ) return
af1a0 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 62   0;.  magic = db
af1b0 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 6d  ->magic;.  if( m
af1c0 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47  agic!=SQLITE_MAG
af1d0 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20 20 20  IC_OPEN &&.     
af1e0 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d   magic!=SQLITE_M
af1f0 41 47 49 43 5f 42 55 53 59 20 29 20 72 65 74 75  AGIC_BUSY ) retu
af200 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31  rn 0;.  return 1
af210 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
af220 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61  TE int sqlite3Sa
af230 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f  fetyCheckSickOrO
af240 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  k(sqlite3 *db){.
af250 20 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 69    u32 magic;.  i
af260 66 28 20 64 62 3d 3d 30 20 29 20 72 65 74 75 72  f( db==0 ) retur
af270 6e 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64  n 0;.  magic = d
af280 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20  b->magic;.  if( 
af290 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41  magic!=SQLITE_MA
af2a0 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 20  GIC_SICK &&.    
af2b0 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f    magic!=SQLITE_
af2c0 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20  MAGIC_OPEN &&.  
af2d0 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54      magic!=SQLIT
af2e0 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72  E_MAGIC_BUSY ) r
af2f0 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72  eturn 0;.  retur
af300 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  n 1;.}../*******
af310 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75  ******* End of u
af320 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  til.c **********
af330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af350 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
af360 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
af370 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a  le hash.c ******
af380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af3a0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
af3b0 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a  01 September 22.
af3c0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
af3d0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
af3e0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
af3f0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
af400 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
af410 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
af420 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
af430 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
af440 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
af450 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
af460 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
af470 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
af480 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
af490 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
af4a0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
af4b0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
af4c0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
af4d0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
af4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
af520 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d  * This is the im
af530 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
af540 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62  generic hash-tab
af550 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53  les.** used in S
af560 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  QLite..**.** $Id
af570 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 33 20  : hash.c,v 1.33 
af580 32 30 30 39 2f 30 31 2f 30 39 20 30 31 3a 31 32  2009/01/09 01:12
af590 3a 32 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :28 drh Exp $.*/
af5a0 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d  ../* Turn bulk m
af5b0 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73  emory into a has
af5c0 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62  h table object b
af5d0 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  y initializing t
af5e0 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20  he.** fields of 
af5f0 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75  the Hash structu
af600 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22  re..**.** "pNew"
af610 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
af620 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
af630 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e  that is to be in
af640 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 22 63  itialized..** "c
af650 6f 70 79 4b 65 79 22 20 69 73 20 74 72 75 65 20  opyKey" is true 
af660 69 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  if the hash tabl
af670 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74  e should make it
af680 73 20 6f 77 6e 20 70 72 69 76 61 74 65 0a 2a 2a  s own private.**
af690 20 63 6f 70 79 20 6f 66 20 6b 65 79 73 20 61 6e   copy of keys an
af6a0 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 73 68  d false if it sh
af6b0 6f 75 6c 64 20 6a 75 73 74 20 75 73 65 20 74 68  ould just use th
af6c0 65 20 73 75 70 70 6c 69 65 64 20 70 6f 69 6e 74  e supplied point
af6d0 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
af6e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
af6f0 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 20  e3HashInit(Hash 
af700 2a 70 4e 65 77 2c 20 69 6e 74 20 63 6f 70 79 4b  *pNew, int copyK
af710 65 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  ey){.  assert( p
af720 4e 65 77 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77  New!=0 );.  pNew
af730 2d 3e 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79  ->copyKey = copy
af740 4b 65 79 21 3d 30 3b 0a 20 20 70 4e 65 77 2d 3e  Key!=0;.  pNew->
af750 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65  first = 0;.  pNe
af760 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20  w->count = 0;.  
af770 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30  pNew->htsize = 0
af780 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30  ;.  pNew->ht = 0
af790 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61  ;.}../* Remove a
af7a0 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ll entries from 
af7b0 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52  a hash table.  R
af7c0 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72  eclaim all memor
af7d0 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20  y..** Call this 
af7e0 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74  routine to delet
af7f0 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f  e a hash table o
af800 72 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73  r to reset a has
af810 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68  h table.** to th
af820 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a  e empty state..*
af830 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
af840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73   void sqlite3Has
af850 68 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29  hClear(Hash *pH)
af860 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  {.  HashElem *el
af870 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46  em;         /* F
af880 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
af890 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  all elements of 
af8a0 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20  the table */..  
af8b0 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b  assert( pH!=0 );
af8c0 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69  .  elem = pH->fi
af8d0 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74  rst;.  pH->first
af8e0 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
af8f0 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20  free(pH->ht);.  
af900 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48  pH->ht = 0;.  pH
af910 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20  ->htsize = 0;.  
af920 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20  while( elem ){. 
af930 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78     HashElem *nex
af940 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e  t_elem = elem->n
af950 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d  ext;.    if( pH-
af960 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 20  >copyKey ){.    
af970 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65    sqlite3_free(e
af980 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20  lem->pKey);.    
af990 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
af9a0 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c  ee(elem);.    el
af9b0 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a  em = next_elem;.
af9c0 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20    }.  pH->count 
af9d0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61  = 0;.}../*.** Ha
af9e0 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f  sh and compariso
af9f0 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e  n functions when
afa00 20 74 68 65 20 6d 6f 64 65 20 69 73 20 53 51 4c   the mode is SQL
afa10 49 54 45 5f 48 41 53 48 5f 53 54 52 49 4e 47 0a  ITE_HASH_STRING.
afa20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74  */.static int st
afa30 72 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69 64  rHash(const void
afa40 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
afa50 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
afa60 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  *z = (const char
afa70 20 2a 29 70 4b 65 79 3b 0a 20 20 69 6e 74 20 68   *)pKey;.  int h
afa80 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 4b 65 79   = 0;.  if( nKey
afa90 3c 3d 30 20 29 20 6e 4b 65 79 20 3d 20 73 71 6c  <=0 ) nKey = sql
afaa0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b  ite3Strlen30(z);
afab0 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e  .  while( nKey >
afac0 20 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28   0  ){.    h = (
afad0 68 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69  h<<3) ^ h ^ sqli
afae0 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
afaf0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a  (unsigned char)*
afb00 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d  z++];.    nKey--
afb10 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68  ;.  }.  return h
afb20 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d   & 0x7fffffff;.}
afb30 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 72 43  .static int strC
afb40 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69  ompare(const voi
afb50 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31  d *pKey1, int n1
afb60 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
afb70 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20  ey2, int n2){.  
afb80 69 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74  if( n1!=n2 ) ret
afb90 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20  urn 1;.  return 
afba0 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
afbb0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65  (const char*)pKe
afbc0 79 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  y1,(const char*)
afbd0 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 0a 2f  pKey2,n1);.}.../
afbe0 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e  * Link an elemen
afbf0 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20  t into the hash 
afc00 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20  table.*/.static 
afc10 76 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65  void insertEleme
afc20 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20  nt(.  Hash *pH, 
afc30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
afc40 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73  The complete has
afc50 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72  h table */.  str
afc60 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c  uct _ht *pEntry,
afc70 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79      /* The entry
afc80 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77   into which pNew
afc90 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   is inserted */.
afca0 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77    HashElem *pNew
afcb0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
afcc0 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e  element to be in
afcd0 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48  serted */.){.  H
afce0 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20  ashElem *pHead; 
afcf0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65        /* First e
afd00 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69  lement already i
afd10 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 70 48  n pEntry */.  pH
afd20 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68  ead = pEntry->ch
afd30 61 69 6e 3b 0a 20 20 69 66 28 20 70 48 65 61 64  ain;.  if( pHead
afd40 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65   ){.    pNew->ne
afd50 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20  xt = pHead;.    
afd60 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65  pNew->prev = pHe
afd70 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66  ad->prev;.    if
afd80 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b  ( pHead->prev ){
afd90 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65   pHead->prev->ne
afda0 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20  xt = pNew; }.   
afdb0 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20   else           
afdc0 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20    { pH->first = 
afdd0 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61  pNew; }.    pHea
afde0 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a  d->prev = pNew;.
afdf0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65    }else{.    pNe
afe00 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69  w->next = pH->fi
afe10 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d  rst;.    if( pH-
afe20 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69  >first ){ pH->fi
afe30 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77  rst->prev = pNew
afe40 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72  ; }.    pNew->pr
afe50 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e  ev = 0;.    pH->
afe60 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20  first = pNew;.  
afe70 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  }.  pEntry->coun
afe80 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 63  t++;.  pEntry->c
afe90 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a  hain = pNew;.}..
afea0 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68  ./* Resize the h
afeb0 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61  ash table so tha
afec0 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e  t it cantains "n
afed0 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73  ew_size" buckets
afee0 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20  ..** "new_size" 
afef0 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20  must be a power 
aff00 6f 66 20 32 2e 20 20 54 68 65 20 68 61 73 68 20  of 2.  The hash 
aff10 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c  table might fail
aff20 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20 69   .** to resize i
aff30 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  f sqlite3_malloc
aff40 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  () fails..*/.sta
aff50 74 69 63 20 76 6f 69 64 20 72 65 68 61 73 68 28  tic void rehash(
aff60 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65  Hash *pH, int ne
aff70 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63  w_size){.  struc
aff80 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20  t _ht *new_ht;  
aff90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
affa0 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20   new hash table 
affb0 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  */.  HashElem *e
affc0 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b  lem, *next_elem;
affd0 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
affe0 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67  ng over existing
afff0 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 69   elements */..#i
b0000 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c  fdef SQLITE_MALL
b0010 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 0a 20 20  OC_SOFT_LIMIT.  
b0020 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a  if( new_size*siz
b0030 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e  eof(struct _ht)>
b0040 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f  SQLITE_MALLOC_SO
b0050 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20  FT_LIMIT ){.    
b0060 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54  new_size = SQLIT
b0070 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
b0080 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63  MIT/sizeof(struc
b0090 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66  t _ht);.  }.  if
b00a0 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e  ( new_size==pH->
b00b0 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e 3b  htsize ) return;
b00c0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68  .#endif..  /* Th
b00d0 65 72 65 20 69 73 20 61 20 63 61 6c 6c 20 74 6f  ere is a call to
b00e0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
b00f0 29 20 69 6e 73 69 64 65 20 72 65 68 61 73 68 28  ) inside rehash(
b0100 29 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 20  ). If there is. 
b0110 20 2a 2a 20 61 6c 72 65 61 64 79 20 61 6e 20 61   ** already an a
b0120 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 20 70 48 2d  llocation at pH-
b0130 3e 68 74 2c 20 74 68 65 6e 20 69 66 20 74 68 69  >ht, then if thi
b0140 73 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  s malloc() fails
b0150 20 69 74 0a 20 20 2a 2a 20 69 73 20 62 65 6e 69   it.  ** is beni
b0160 67 6e 20 28 73 69 6e 63 65 20 66 61 69 6c 69 6e  gn (since failin
b0170 67 20 74 6f 20 72 65 73 69 7a 65 20 61 20 68 61  g to resize a ha
b0180 73 68 20 74 61 62 6c 65 20 69 73 20 61 20 70 65  sh table is a pe
b0190 72 66 6f 72 6d 61 6e 63 65 0a 20 20 2a 2a 20 68  rformance.  ** h
b01a0 69 74 20 6f 6e 6c 79 2c 20 6e 6f 74 20 61 20 66  it only, not a f
b01b0 61 74 61 6c 20 65 72 72 6f 72 29 2e 0a 20 20 2a  atal error)..  *
b01c0 2f 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69  /.  if( pH->htsi
b01d0 7a 65 3e 30 20 29 20 73 71 6c 69 74 65 33 42 65  ze>0 ) sqlite3Be
b01e0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
b01f0 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73  );.  new_ht = (s
b0200 74 72 75 63 74 20 5f 68 74 20 2a 29 73 71 6c 69  truct _ht *)sqli
b0210 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 6e  te3MallocZero( n
b0220 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73  ew_size*sizeof(s
b0230 74 72 75 63 74 20 5f 68 74 29 20 29 3b 0a 20 20  truct _ht) );.  
b0240 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30  if( pH->htsize>0
b0250 20 29 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e   ) sqlite3EndBen
b0260 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20  ignMalloc();..  
b0270 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20  if( new_ht==0 ) 
b0280 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65  return;.  sqlite
b0290 33 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a  3_free(pH->ht);.
b02a0 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68    pH->ht = new_h
b02b0 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20  t;.  pH->htsize 
b02c0 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 66 6f  = new_size;.  fo
b02d0 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74  r(elem=pH->first
b02e0 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65  , pH->first=0; e
b02f0 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74  lem; elem = next
b0300 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e 74 20  _elem){.    int 
b0310 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d  h = strHash(elem
b0320 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b  ->pKey, elem->nK
b0330 65 79 29 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d  ey) & (new_size-
b0340 31 29 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65  1);.    next_ele
b0350 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
b0360 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e      insertElemen
b0370 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d  t(pH, &new_ht[h]
b0380 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a  , elem);.  }.}..
b0390 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
b03a0 20 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75   (for internal u
b03b0 73 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73  se only) locates
b03c0 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61   an element in a
b03d0 6e 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20  n.** hash table 
b03e0 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65  that matches the
b03f0 20 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65   given key.  The
b0400 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b   hash for this k
b0410 65 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64  ey has.** alread
b0420 79 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20  y been computed 
b0430 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 73  and is passed as
b0440 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74   the 4th paramet
b0450 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61  er..*/.static Ha
b0460 73 68 45 6c 65 6d 20 2a 66 69 6e 64 45 6c 65 6d  shElem *findElem
b0470 65 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20  entGivenHash(.  
b0480 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20  const Hash *pH, 
b0490 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 74 6f      /* The pH to
b04a0 20 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a   be searched */.
b04b0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b    const void *pK
b04c0 65 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  ey,   /* The key
b04d0 20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e   we are searchin
b04e0 67 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  g for */.  int n
b04f0 4b 65 79 2c 0a 20 20 69 6e 74 20 68 20 20 20 20  Key,.  int h    
b0500 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
b0510 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20  e hash for this 
b0520 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 48 61 73  key. */.){.  Has
b0530 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
b0540 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
b0550 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75  sed to loop thru
b0560 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73   the element lis
b0570 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74  t */.  int count
b0580 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b0590 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
b05a0 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74  of elements left
b05b0 20 74 6f 20 74 65 73 74 20 2a 2f 0a 0a 20 20 69   to test */..  i
b05c0 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20  f( pH->ht ){.   
b05d0 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e   struct _ht *pEn
b05e0 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d  try = &pH->ht[h]
b05f0 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e  ;.    elem = pEn
b0600 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20  try->chain;.    
b0610 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e  count = pEntry->
b0620 63 6f 75 6e 74 3b 0a 20 20 20 20 77 68 69 6c 65  count;.    while
b0630 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65  ( count-- && ele
b0640 6d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  m ){.      if( s
b0650 74 72 43 6f 6d 70 61 72 65 28 65 6c 65 6d 2d 3e  trCompare(elem->
b0660 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c  pKey,elem->nKey,
b0670 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b  pKey,nKey)==0 ){
b0680 20 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e   .        return
b0690 20 65 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20   elem;.      }. 
b06a0 20 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d       elem = elem
b06b0 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  ->next;.    }.  
b06c0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
b06d0 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e  ./* Remove a sin
b06e0 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74  gle entry from t
b06f0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69  he hash table gi
b0700 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ven a pointer to
b0710 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74   that.** element
b0720 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74   and a hash on t
b0730 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79  he element's key
b0740 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
b0750 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69   removeElementGi
b0760 76 65 6e 48 61 73 68 28 0a 20 20 48 61 73 68 20  venHash(.  Hash 
b0770 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  *pH,         /* 
b0780 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e  The pH containin
b0790 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 48 61  g "elem" */.  Ha
b07a0 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20  shElem* elem,   
b07b0 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
b07c0 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f  o be removed fro
b07d0 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e  m the pH */.  in
b07e0 74 20 68 20 20 20 20 20 20 20 20 20 20 20 20 20  t h             
b07f0 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f  /* Hash value fo
b0800 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f  r the element */
b0810 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 68 74  .){.  struct _ht
b0820 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20   *pEntry;.  if( 
b0830 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20  elem->prev ){.  
b0840 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65    elem->prev->ne
b0850 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  xt = elem->next;
b0860 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70   .  }else{.    p
b0870 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d  H->first = elem-
b0880 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28  >next;.  }.  if(
b0890 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20   elem->next ){. 
b08a0 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70     elem->next->p
b08b0 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76  rev = elem->prev
b08c0 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d  ;.  }.  pEntry =
b08d0 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69   &pH->ht[h];.  i
b08e0 66 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e  f( pEntry->chain
b08f0 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45  ==elem ){.    pE
b0900 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c  ntry->chain = el
b0910 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20  em->next;.  }.  
b0920 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b  pEntry->count--;
b0930 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63  .  if( pEntry->c
b0940 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70  ount<=0 ){.    p
b0950 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30  Entry->chain = 0
b0960 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e  ;.  }.  if( pH->
b0970 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 20 73  copyKey ){.    s
b0980 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d  qlite3_free(elem
b0990 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 73  ->pKey);.  }.  s
b09a0 71 6c 69 74 65 33 5f 66 72 65 65 28 20 65 6c 65  qlite3_free( ele
b09b0 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74  m );.  pH->count
b09c0 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f  --;.  if( pH->co
b09d0 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73  unt<=0 ){.    as
b09e0 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73 74 3d  sert( pH->first=
b09f0 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
b0a00 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29  ( pH->count==0 )
b0a10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73  ;.    sqlite3Has
b0a20 68 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a  hClear(pH);.  }.
b0a30 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f  }../* Attempt to
b0a40 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65   locate an eleme
b0a50 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74  nt of the hash t
b0a60 61 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b  able pH with a k
b0a70 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68  ey.** that match
b0a80 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52  es pKey,nKey.  R
b0a90 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
b0aa0 74 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  to the correspon
b0ab0 64 69 6e 67 20 0a 2a 2a 20 48 61 73 68 45 6c 65  ding .** HashEle
b0ac0 6d 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  m structure for 
b0ad0 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20  this element if 
b0ae0 69 74 20 69 73 20 66 6f 75 6e 64 2c 20 6f 72 20  it is found, or 
b0af0 4e 55 4c 4c 0a 2a 2a 20 6f 74 68 65 72 77 69 73  NULL.** otherwis
b0b00 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
b0b10 56 41 54 45 20 48 61 73 68 45 6c 65 6d 20 2a 73  VATE HashElem *s
b0b20 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 45 6c  qlite3HashFindEl
b0b30 65 6d 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70  em(const Hash *p
b0b40 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  H, const void *p
b0b50 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a  Key, int nKey){.
b0b60 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20    int h;        
b0b70 20 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20 6f       /* A hash o
b0b80 6e 20 6b 65 79 20 2a 2f 0a 20 20 48 61 73 68 45  n key */.  HashE
b0b90 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a  lem *elem;    /*
b0ba0 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61   The element tha
b0bb0 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f  t matches key */
b0bc0 0a 0a 20 20 69 66 28 20 70 48 3d 3d 30 20 7c 7c  ..  if( pH==0 ||
b0bd0 20 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65 74   pH->ht==0 ) ret
b0be0 75 72 6e 20 30 3b 0a 20 20 68 20 3d 20 73 74 72  urn 0;.  h = str
b0bf0 48 61 73 68 28 70 4b 65 79 2c 6e 4b 65 79 29 3b  Hash(pKey,nKey);
b0c00 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c  .  elem = findEl
b0c10 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70  ementGivenHash(p
b0c20 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 25  H,pKey,nKey, h %
b0c30 20 70 48 2d 3e 68 74 73 69 7a 65 29 3b 0a 20 20   pH->htsize);.  
b0c40 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 7d 0a 0a  return elem;.}..
b0c50 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f  /* Attempt to lo
b0c60 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20  cate an element 
b0c70 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  of the hash tabl
b0c80 65 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a  e pH with a key.
b0c90 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  ** that matches 
b0ca0 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75  pKey,nKey.  Retu
b0cb0 72 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20  rn the data for 
b0cc0 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20  this element if 
b0cd0 69 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20  it is.** found, 
b0ce0 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65  or NULL if there
b0cf0 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f   is no match..*/
b0d00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0d10 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73  void *sqlite3Has
b0d20 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68  hFind(const Hash
b0d30 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *pH, const void
b0d40 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
b0d50 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  ){.  HashElem *e
b0d60 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65  lem;    /* The e
b0d70 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63  lement that matc
b0d80 68 65 73 20 6b 65 79 20 2a 2f 0a 20 20 65 6c 65  hes key */.  ele
b0d90 6d 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  m = sqlite3HashF
b0da0 69 6e 64 45 6c 65 6d 28 70 48 2c 20 70 4b 65 79  indElem(pH, pKey
b0db0 2c 20 6e 4b 65 79 29 3b 0a 20 20 72 65 74 75 72  , nKey);.  retur
b0dc0 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64  n elem ? elem->d
b0dd0 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49  ata : 0;.}../* I
b0de0 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74  nsert an element
b0df0 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74   into the hash t
b0e00 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20 6b 65  able pH.  The ke
b0e10 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a  y is pKey,nKey.*
b0e20 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69  * and the data i
b0e30 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20  s "data"..**.** 
b0e40 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78  If no element ex
b0e50 69 73 74 73 20 77 69 74 68 20 61 20 6d 61 74 63  ists with a matc
b0e60 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e 20 61  hing key, then a
b0e70 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20   new.** element 
b0e80 69 73 20 63 72 65 61 74 65 64 2e 20 20 41 20 63  is created.  A c
b0e90 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69  opy of the key i
b0ea0 73 20 6d 61 64 65 20 69 66 20 74 68 65 20 63 6f  s made if the co
b0eb0 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20 69 73  pyKey.** flag is
b0ec0 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73 20 72   set.  NULL is r
b0ed0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
b0ee0 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e  f another elemen
b0ef0 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
b0f00 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b   with the same k
b0f10 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  ey, then the.** 
b0f20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65  new data replace
b0f30 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61  s the old data a
b0f40 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20  nd the old data 
b0f50 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
b0f60 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63  The key is not c
b0f70 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e  opied in this in
b0f80 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61  stance.  If a ma
b0f90 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e  lloc fails, then
b0fa0 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61  .** the new data
b0fb0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
b0fc0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
b0fd0 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a  is unchanged..**
b0fe0 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61  .** If the "data
b0ff0 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  " parameter to t
b1000 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
b1010 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a  NULL, then the.*
b1020 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73  * element corres
b1030 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22  ponding to "key"
b1040 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   is removed from
b1050 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
b1060 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b1070 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
b1080 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 20  HashInsert(Hash 
b1090 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  *pH, const void 
b10a0 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c  *pKey, int nKey,
b10b0 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20   void *data){.  
b10c0 69 6e 74 20 68 72 61 77 3b 20 20 20 20 20 20 20  int hraw;       
b10d0 20 20 20 20 20 20 2f 2a 20 52 61 77 20 68 61 73        /* Raw has
b10e0 68 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b  h value of the k
b10f0 65 79 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20  ey */.  int h;  
b1100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
b1110 20 74 68 65 20 68 61 73 68 20 6f 66 20 74 68 65   the hash of the
b1120 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68   key modulo hash
b1130 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20   table size */. 
b1140 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b   HashElem *elem;
b1150 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
b1160 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20  o loop thru the 
b1170 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a  element list */.
b1180 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f    HashElem *new_
b1190 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65  elem;   /* New e
b11a0 6c 65 6d 65 6e 74 20 61 64 64 65 64 20 74 6f 20  lement added to 
b11b0 74 68 65 20 70 48 20 2a 2f 0a 0a 20 20 61 73 73  the pH */..  ass
b11c0 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20  ert( pH!=0 );.  
b11d0 68 72 61 77 20 3d 20 73 74 72 48 61 73 68 28 70  hraw = strHash(p
b11e0 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 69 66  Key, nKey);.  if
b11f0 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a  ( pH->htsize ){.
b1200 20 20 20 20 68 20 3d 20 68 72 61 77 20 25 20 70      h = hraw % p
b1210 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 65  H->htsize;.    e
b1220 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e  lem = findElemen
b1230 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 70 4b  tGivenHash(pH,pK
b1240 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 20 20  ey,nKey,h);.    
b1250 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20  if( elem ){.    
b1260 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61    void *old_data
b1270 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20   = elem->data;. 
b1280 20 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30       if( data==0
b1290 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6d 6f   ){.        remo
b12a0 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61  veElementGivenHa
b12b0 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20  sh(pH,elem,h);. 
b12c0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
b12d0 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d      elem->data =
b12e0 20 64 61 74 61 3b 0a 20 20 20 20 20 20 20 20 69   data;.        i
b12f0 66 28 20 21 70 48 2d 3e 63 6f 70 79 4b 65 79 20  f( !pH->copyKey 
b1300 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 65  ){.          ele
b1310 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 20  m->pKey = (void 
b1320 2a 29 70 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  *)pKey;.        
b1330 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
b1340 28 6e 4b 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65  (nKey==elem->nKe
b1350 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  y);.      }.    
b1360 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74    return old_dat
b1370 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  a;.    }.  }.  i
b1380 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74  f( data==0 ) ret
b1390 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65  urn 0;.  new_ele
b13a0 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73  m = (HashElem*)s
b13b0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69  qlite3Malloc( si
b13c0 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29  zeof(HashElem) )
b13d0 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d  ;.  if( new_elem
b13e0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74  ==0 ) return dat
b13f0 61 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70  a;.  if( pH->cop
b1400 79 4b 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20  yKey && pKey!=0 
b1410 29 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d  ){.    new_elem-
b1420 3e 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d  >pKey = sqlite3M
b1430 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20  alloc( nKey );. 
b1440 20 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d     if( new_elem-
b1450 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20  >pKey==0 ){.    
b1460 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6e    sqlite3_free(n
b1470 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20  ew_elem);.      
b1480 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20  return data;.   
b1490 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76   }.    memcpy((v
b14a0 6f 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70  oid*)new_elem->p
b14b0 4b 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29  Key, pKey, nKey)
b14c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
b14d0 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20  ew_elem->pKey = 
b14e0 28 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d  (void*)pKey;.  }
b14f0 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65  .  new_elem->nKe
b1500 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e  y = nKey;.  pH->
b1510 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70  count++;.  if( p
b1520 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a  H->htsize==0 ){.
b1530 20 20 20 20 72 65 68 61 73 68 28 70 48 2c 20 31      rehash(pH, 1
b1540 32 38 2f 73 69 7a 65 6f 66 28 70 48 2d 3e 68 74  28/sizeof(pH->ht
b1550 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 70  [0]));.    if( p
b1560 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a  H->htsize==0 ){.
b1570 20 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e 74 20        pH->count 
b1580 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 70  = 0;.      if( p
b1590 48 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20  H->copyKey ){.  
b15a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
b15b0 65 65 28 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65  ee(new_elem->pKe
b15c0 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  y);.      }.    
b15d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6e    sqlite3_free(n
b15e0 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20  ew_elem);.      
b15f0 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20  return data;.   
b1600 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d   }.  }.  if( pH-
b1610 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68 74 73  >count > pH->hts
b1620 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 68 61 73  ize ){.    rehas
b1630 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a  h(pH,pH->htsize*
b1640 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  2);.  }.  assert
b1650 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29  ( pH->htsize>0 )
b1660 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 25 20 70  ;.  h = hraw % p
b1670 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 69 6e 73  H->htsize;.  ins
b1680 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26  ertElement(pH, &
b1690 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65  pH->ht[h], new_e
b16a0 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d  lem);.  new_elem
b16b0 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20  ->data = data;. 
b16c0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
b16d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
b16e0 64 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a 2a 2a  d of hash.c ****
b16f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b1720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
b1730 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73  gin file opcodes
b1740 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
b1750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b1770 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67   Automatically g
b1780 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f  enerated.  Do no
b1790 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65  t edit */./* See
b17a0 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e 61   the mkopcodec.a
b17b0 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65  wk script for de
b17c0 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 21 64  tails. */.#if !d
b17d0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
b17e0 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21  IT_EXPLAIN) || !
b17f0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20  defined(NDEBUG) 
b1800 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f  || defined(VDBE_
b1810 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69  PROFILE) || defi
b1820 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
b1830 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
b1840 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
b1850 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69  ite3OpcodeName(i
b1860 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 63 20 63  nt i){. static c
b1870 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
b1880 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22 3f   azName[] = { "?
b1890 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 20 2a  ",.     /*   1 *
b18a0 2f 20 22 56 4e 65 78 74 22 2c 0a 20 20 20 20 20  / "VNext",.     
b18b0 2f 2a 20 20 20 32 20 2a 2f 20 22 41 66 66 69 6e  /*   2 */ "Affin
b18c0 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20  ity",.     /*   
b18d0 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22 2c 0a 20  3 */ "Column",. 
b18e0 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 53      /*   4 */ "S
b18f0 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20  etCookie",.     
b1900 2f 2a 20 20 20 35 20 2a 2f 20 22 53 65 65 6b 22  /*   5 */ "Seek"
b1910 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36 20 2a 2f  ,.     /*   6 */
b1920 20 22 53 65 71 75 65 6e 63 65 22 2c 0a 20 20 20   "Sequence",.   
b1930 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22 53 61 76    /*   7 */ "Sav
b1940 65 70 6f 69 6e 74 22 2c 0a 20 20 20 20 20 2f 2a  epoint",.     /*
b1950 20 20 20 38 20 2a 2f 20 22 52 6f 77 4b 65 79 22     8 */ "RowKey"
b1960 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39 20 2a 2f  ,.     /*   9 */
b1970 20 22 53 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f   "SCopy",.     /
b1980 2a 20 20 31 30 20 2a 2f 20 22 4f 70 65 6e 57 72  *  10 */ "OpenWr
b1990 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  ite",.     /*  1
b19a0 31 20 2a 2f 20 22 49 66 22 2c 0a 20 20 20 20 20  1 */ "If",.     
b19b0 2f 2a 20 20 31 32 20 2a 2f 20 22 56 52 6f 77 69  /*  12 */ "VRowi
b19c0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 33 20  d",.     /*  13 
b19d0 2a 2f 20 22 43 6f 6c 6c 53 65 71 22 2c 0a 20 20  */ "CollSeq",.  
b19e0 20 20 20 2f 2a 20 20 31 34 20 2a 2f 20 22 4f 70     /*  14 */ "Op
b19f0 65 6e 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a  enRead",.     /*
b1a00 20 20 31 35 20 2a 2f 20 22 45 78 70 69 72 65 22    15 */ "Expire"
b1a10 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 2a 2f  ,.     /*  16 */
b1a20 20 22 41 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20   "AutoCommit",. 
b1a30 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 50      /*  17 */ "P
b1a40 61 67 65 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20  agecount",.     
b1a50 2f 2a 20 20 31 38 20 2a 2f 20 22 49 6e 74 65 67  /*  18 */ "Integ
b1a60 72 69 74 79 43 6b 22 2c 0a 20 20 20 20 20 2f 2a  rityCk",.     /*
b1a70 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 2c 0a 20    19 */ "Not",. 
b1a80 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 22 53      /*  20 */ "S
b1a90 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32  ort",.     /*  2
b1aa0 31 20 2a 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20  1 */ "Copy",.   
b1ab0 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 54 72 61    /*  22 */ "Tra
b1ac0 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 33  ce",.     /*  23
b1ad0 20 2a 2f 20 22 46 75 6e 63 74 69 6f 6e 22 2c 0a   */ "Function",.
b1ae0 20 20 20 20 20 2f 2a 20 20 32 34 20 2a 2f 20 22       /*  24 */ "
b1af0 49 66 4e 65 67 22 2c 0a 20 20 20 20 20 2f 2a 20  IfNeg",.     /* 
b1b00 20 32 35 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20   25 */ "Noop",. 
b1b10 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 52      /*  26 */ "R
b1b20 65 74 75 72 6e 22 2c 0a 20 20 20 20 20 2f 2a 20  eturn",.     /* 
b1b30 20 32 37 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64   27 */ "NewRowid
b1b40 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a  ",.     /*  28 *
b1b50 2f 20 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20  / "Variable",.  
b1b60 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 22 53 74     /*  29 */ "St
b1b70 72 69 6e 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ring",.     /*  
b1b80 33 30 20 2a 2f 20 22 52 65 61 6c 41 66 66 69 6e  30 */ "RealAffin
b1b90 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  ity",.     /*  3
b1ba0 31 20 2a 2f 20 22 56 52 65 6e 61 6d 65 22 2c 0a  1 */ "VRename",.
b1bb0 20 20 20 20 20 2f 2a 20 20 33 32 20 2a 2f 20 22       /*  32 */ "
b1bc0 50 61 72 73 65 53 63 68 65 6d 61 22 2c 0a 20 20  ParseSchema",.  
b1bd0 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22 56 4f     /*  33 */ "VO
b1be0 70 65 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  pen",.     /*  3
b1bf0 34 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20  4 */ "Close",.  
b1c00 20 20 20 2f 2a 20 20 33 35 20 2a 2f 20 22 43 72     /*  35 */ "Cr
b1c10 65 61 74 65 49 6e 64 65 78 22 2c 0a 20 20 20 20  eateIndex",.    
b1c20 20 2f 2a 20 20 33 36 20 2a 2f 20 22 49 73 55 6e   /*  36 */ "IsUn
b1c30 69 71 75 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ique",.     /*  
b1c40 33 37 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e 64 22  37 */ "NotFound"
b1c50 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f  ,.     /*  38 */
b1c60 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 2f   "Int64",.     /
b1c70 2a 20 20 33 39 20 2a 2f 20 22 4d 75 73 74 42 65  *  39 */ "MustBe
b1c80 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  Int",.     /*  4
b1c90 30 20 2a 2f 20 22 48 61 6c 74 22 2c 0a 20 20 20  0 */ "Halt",.   
b1ca0 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 52 6f 77    /*  41 */ "Row
b1cb0 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 32  id",.     /*  42
b1cc0 20 2a 2f 20 22 49 64 78 4c 54 22 2c 0a 20 20 20   */ "IdxLT",.   
b1cd0 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 41 64 64    /*  43 */ "Add
b1ce0 49 6d 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  Imm",.     /*  4
b1cf0 34 20 2a 2f 20 22 53 74 61 74 65 6d 65 6e 74 22  4 */ "Statement"
b1d00 2c 0a 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f  ,.     /*  45 */
b1d10 20 22 52 6f 77 44 61 74 61 22 2c 0a 20 20 20 20   "RowData",.    
b1d20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4d 65 6d 4d   /*  46 */ "MemM
b1d30 61 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 37  ax",.     /*  47
b1d40 20 2a 2f 20 22 4e 6f 74 45 78 69 73 74 73 22 2c   */ "NotExists",
b1d50 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a 2f 20  .     /*  48 */ 
b1d60 22 47 6f 73 75 62 22 2c 0a 20 20 20 20 20 2f 2a  "Gosub",.     /*
b1d70 20 20 34 39 20 2a 2f 20 22 49 6e 74 65 67 65 72    49 */ "Integer
b1d80 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 2a  ",.     /*  50 *
b1d90 2f 20 22 50 72 65 76 22 2c 0a 20 20 20 20 20 2f  / "Prev",.     /
b1da0 2a 20 20 35 31 20 2a 2f 20 22 52 6f 77 53 65 74  *  51 */ "RowSet
b1db0 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Read",.     /*  
b1dc0 35 32 20 2a 2f 20 22 52 6f 77 53 65 74 41 64 64  52 */ "RowSetAdd
b1dd0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 33 20 2a  ",.     /*  53 *
b1de0 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20  / "VColumn",.   
b1df0 20 20 2f 2a 20 20 35 34 20 2a 2f 20 22 43 72 65    /*  54 */ "Cre
b1e00 61 74 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20  ateTable",.     
b1e10 2f 2a 20 20 35 35 20 2a 2f 20 22 4c 61 73 74 22  /*  55 */ "Last"
b1e20 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 20 2a 2f  ,.     /*  56 */
b1e30 20 22 53 65 65 6b 4c 65 22 2c 0a 20 20 20 20 20   "SeekLe",.     
b1e40 2f 2a 20 20 35 37 20 2a 2f 20 22 49 6e 63 72 56  /*  57 */ "IncrV
b1e50 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20  acuum",.     /* 
b1e60 20 35 38 20 2a 2f 20 22 49 64 78 52 6f 77 69 64   58 */ "IdxRowid
b1e70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 39 20 2a  ",.     /*  59 *
b1e80 2f 20 22 52 65 73 65 74 43 6f 75 6e 74 22 2c 0a  / "ResetCount",.
b1e90 20 20 20 20 20 2f 2a 20 20 36 30 20 2a 2f 20 22       /*  60 */ "
b1ea0 43 6f 6e 74 65 78 74 50 75 73 68 22 2c 0a 20 20  ContextPush",.  
b1eb0 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 59 69     /*  61 */ "Yi
b1ec0 65 6c 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  eld",.     /*  6
b1ed0 32 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65  2 */ "DropTrigge
b1ee0 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 33 20  r",.     /*  63 
b1ef0 2a 2f 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a  */ "DropIndex",.
b1f00 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20 22       /*  64 */ "
b1f10 49 64 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a 20  IdxGE",.     /* 
b1f20 20 36 35 20 2a 2f 20 22 49 64 78 44 65 6c 65 74   65 */ "IdxDelet
b1f30 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20  e",.     /*  66 
b1f40 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20 20 2f 2a  */ "Or",.     /*
b1f50 20 20 36 37 20 2a 2f 20 22 41 6e 64 22 2c 0a 20    67 */ "And",. 
b1f60 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f 20 22 56      /*  68 */ "V
b1f70 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20  acuum",.     /* 
b1f80 20 36 39 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a   69 */ "IfNot",.
b1f90 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 22       /*  70 */ "
b1fa0 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 20  DropTable",.    
b1fb0 20 2f 2a 20 20 37 31 20 2a 2f 20 22 49 73 4e 75   /*  71 */ "IsNu
b1fc0 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32  ll",.     /*  72
b1fd0 20 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20   */ "NotNull",. 
b1fe0 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f 20 22 4e      /*  73 */ "N
b1ff0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 34 20  e",.     /*  74 
b2000 2a 2f 20 22 45 71 22 2c 0a 20 20 20 20 20 2f 2a  */ "Eq",.     /*
b2010 20 20 37 35 20 2a 2f 20 22 47 74 22 2c 0a 20 20    75 */ "Gt",.  
b2020 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 4c 65     /*  76 */ "Le
b2030 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a  ",.     /*  77 *
b2040 2f 20 22 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20  / "Lt",.     /* 
b2050 20 37 38 20 2a 2f 20 22 47 65 22 2c 0a 20 20 20   78 */ "Ge",.   
b2060 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 53 65 65    /*  79 */ "See
b2070 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  kLt",.     /*  8
b2080 30 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20  0 */ "BitAnd",. 
b2090 20 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22 42      /*  81 */ "B
b20a0 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20  itOr",.     /*  
b20b0 38 32 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74  82 */ "ShiftLeft
b20c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a  ",.     /*  83 *
b20d0 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a  / "ShiftRight",.
b20e0 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 22       /*  84 */ "
b20f0 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  Add",.     /*  8
b2100 35 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c  5 */ "Subtract",
b2110 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20  .     /*  86 */ 
b2120 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20  "Multiply",.    
b2130 20 2f 2a 20 20 38 37 20 2a 2f 20 22 44 69 76 69   /*  87 */ "Divi
b2140 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 38  de",.     /*  88
b2150 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c   */ "Remainder",
b2160 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f 20  .     /*  89 */ 
b2170 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f  "Concat",.     /
b2180 2a 20 20 39 30 20 2a 2f 20 22 4d 61 6b 65 52 65  *  90 */ "MakeRe
b2190 63 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  cord",.     /*  
b21a0 39 31 20 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77  91 */ "ResultRow
b21b0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 2a  ",.     /*  92 *
b21c0 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20 20 20  / "Delete",.    
b21d0 20 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e   /*  93 */ "BitN
b21e0 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34  ot",.     /*  94
b21f0 20 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20   */ "String8",. 
b2200 20 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 41      /*  95 */ "A
b2210 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f  ggFinal",.     /
b2220 2a 20 20 39 36 20 2a 2f 20 22 43 6f 6d 70 61 72  *  96 */ "Compar
b2230 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37 20  e",.     /*  97 
b2240 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20  */ "Goto",.     
b2250 2f 2a 20 20 39 38 20 2a 2f 20 22 54 61 62 6c 65  /*  98 */ "Table
b2260 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Lock",.     /*  
b2270 39 39 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20  99 */ "Clear",. 
b2280 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 56      /* 100 */ "V
b2290 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20 20  erifyCookie",.  
b22a0 20 20 20 2f 2a 20 31 30 31 20 2a 2f 20 22 41 67     /* 101 */ "Ag
b22b0 67 53 74 65 70 22 2c 0a 20 20 20 20 20 2f 2a 20  gStep",.     /* 
b22c0 31 30 32 20 2a 2f 20 22 53 65 74 4e 75 6d 43 6f  102 */ "SetNumCo
b22d0 6c 75 6d 6e 73 22 2c 0a 20 20 20 20 20 2f 2a 20  lumns",.     /* 
b22e0 31 30 33 20 2a 2f 20 22 54 72 61 6e 73 61 63 74  103 */ "Transact
b22f0 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  ion",.     /* 10
b2300 34 20 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c 0a  4 */ "VFilter",.
b2310 20 20 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22       /* 105 */ "
b2320 56 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20  VDestroy",.     
b2330 2f 2a 20 31 30 36 20 2a 2f 20 22 43 6f 6e 74 65  /* 106 */ "Conte
b2340 78 74 50 6f 70 22 2c 0a 20 20 20 20 20 2f 2a 20  xtPop",.     /* 
b2350 31 30 37 20 2a 2f 20 22 4e 65 78 74 22 2c 0a 20  107 */ "Next",. 
b2360 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22 43      /* 108 */ "C
b2370 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  ount",.     /* 1
b2380 30 39 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74  09 */ "IdxInsert
b2390 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30 20 2a  ",.     /* 110 *
b23a0 2f 20 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20  / "SeekGe",.    
b23b0 20 2f 2a 20 31 31 31 20 2a 2f 20 22 49 6e 73 65   /* 111 */ "Inse
b23c0 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 32  rt",.     /* 112
b23d0 20 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a 20   */ "Destroy",. 
b23e0 20 20 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 52      /* 113 */ "R
b23f0 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20  eadCookie",.    
b2400 20 2f 2a 20 31 31 34 20 2a 2f 20 22 4c 6f 61 64   /* 114 */ "Load
b2410 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20  Analysis",.     
b2420 2f 2a 20 31 31 35 20 2a 2f 20 22 45 78 70 6c 61  /* 115 */ "Expla
b2430 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36  in",.     /* 116
b2440 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22   */ "HaltIfNull"
b2450 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f  ,.     /* 117 */
b2460 20 22 4f 70 65 6e 50 73 65 75 64 6f 22 2c 0a 20   "OpenPseudo",. 
b2470 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f 20 22 4f      /* 118 */ "O
b2480 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c 0a 20  penEphemeral",. 
b2490 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22 4e      /* 119 */ "N
b24a0 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32  ull",.     /* 12
b24b0 30 20 2a 2f 20 22 4d 6f 76 65 22 2c 0a 20 20 20  0 */ "Move",.   
b24c0 20 20 2f 2a 20 31 32 31 20 2a 2f 20 22 42 6c 6f    /* 121 */ "Blo
b24d0 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 32 20  b",.     /* 122 
b24e0 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20  */ "Rewind",.   
b24f0 20 20 2f 2a 20 31 32 33 20 2a 2f 20 22 53 65 65    /* 123 */ "See
b2500 6b 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32  kGt",.     /* 12
b2510 34 20 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20  4 */ "VBegin",. 
b2520 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f 20 22 56      /* 125 */ "V
b2530 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a  Update",.     /*
b2540 20 31 32 36 20 2a 2f 20 22 49 66 5a 65 72 6f 22   126 */ "IfZero"
b2550 2c 0a 20 20 20 20 20 2f 2a 20 31 32 37 20 2a 2f  ,.     /* 127 */
b2560 20 22 56 43 72 65 61 74 65 22 2c 0a 20 20 20 20   "VCreate",.    
b2570 20 2f 2a 20 31 32 38 20 2a 2f 20 22 46 6f 75 6e   /* 128 */ "Foun
b2580 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 39 20  d",.     /* 129 
b2590 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20 20 20 20  */ "IfPos",.    
b25a0 20 2f 2a 20 31 33 30 20 2a 2f 20 22 52 65 61 6c   /* 130 */ "Real
b25b0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a  ",.     /* 131 *
b25c0 2f 20 22 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20  / "NullRow",.   
b25d0 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 4a 75 6d    /* 132 */ "Jum
b25e0 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20  p",.     /* 133 
b25f0 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e 22  */ "Permutation"
b2600 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f  ,.     /* 134 */
b2610 20 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a   "NotUsed_134",.
b2620 20 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22       /* 135 */ "
b2630 4e 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20  NotUsed_135",.  
b2640 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f     /* 136 */ "No
b2650 74 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20  tUsed_136",.    
b2660 20 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55   /* 137 */ "NotU
b2670 73 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f  sed_137",.     /
b2680 2a 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65  * 138 */ "NotUse
b2690 64 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20  d_138",.     /* 
b26a0 31 33 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f  139 */ "NotUsed_
b26b0 31 33 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34  139",.     /* 14
b26c0 30 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34  0 */ "NotUsed_14
b26d0 30 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20  0",.     /* 141 
b26e0 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20  */ "ToText",.   
b26f0 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42    /* 142 */ "ToB
b2700 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34  lob",.     /* 14
b2710 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22  3 */ "ToNumeric"
b2720 2c 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f  ,.     /* 144 */
b2730 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f   "ToInt",.     /
b2740 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c  * 145 */ "ToReal
b2750 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e  ",.  };.  return
b2760 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65   azName[i];.}.#e
b2770 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
b2780 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63  ***** End of opc
b2790 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  odes.c *********
b27a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b27b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b27c0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
b27d0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
b27e0 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a   os_os2.c ******
b27f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2810 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
b2820 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68   Feb 14.**.** Th
b2830 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
b2840 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
b2850 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
b2860 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
b2870 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
b2880 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
b2890 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
b28a0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
b28b0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
b28c0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
b28d0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
b28e0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
b28f0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
b2900 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
b2910 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
b2920 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
b2930 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
b2940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
b2990 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
b29a0 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69  ains code that i
b29b0 73 20 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53  s specific to OS
b29c0 2f 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f  /2..**.** $Id: o
b29d0 73 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32  s_os2.c,v 1.63 2
b29e0 30 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a  008/12/10 19:26:
b29f0 32 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  24 drh Exp $.*/.
b2a00 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
b2a10 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74  OS2../*.** A Not
b2a20 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41  e About Memory A
b2a30 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a  llocation:.**.**
b2a40 20 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65   This driver use
b2a50 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28  s malloc()/free(
b2a60 29 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65  ) directly rathe
b2a70 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72  r than going thr
b2a80 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69  ough.** the SQLi
b2a90 74 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69  te-wrappers sqli
b2aa0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c  te3_malloc()/sql
b2ab0 69 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68  ite3_free().  Th
b2ac0 6f 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20  ose wrappers.** 
b2ad0 61 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72  are designed for
b2ae0 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64   use on embedded
b2af0 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d   systems where m
b2b00 65 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20  emory is scarce 
b2b10 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61  and.** malloc fa
b2b20 69 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72  ilures happen fr
b2b30 65 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20  equently.  OS/2 
b2b40 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c  does not typical
b2b50 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62  ly run on.** emb
b2b60 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61  edded systems, a
b2b70 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20  nd when it does 
b2b80 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e  the developers n
b2b90 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67  ormally have big
b2ba0 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20  ger.** problems 
b2bb0 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74  to worry about t
b2bc0 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20  han running out 
b2bd0 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74  of memory.  So t
b2be0 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61  here is not.** a
b2bf0 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64   compelling need
b2c00 20 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70   to use the wrap
b2c10 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20  pers..**.** But 
b2c20 74 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20  there is a good 
b2c30 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73  reason to not us
b2c40 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20  e the wrappers. 
b2c50 20 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a   If we use the.*
b2c60 2a 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20  * wrappers then 
b2c70 77 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75  we will get simu
b2c80 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66  lated malloc() f
b2c90 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74  ailures within t
b2ca0 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20  his.** driver.  
b2cb0 41 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20  And that causes 
b2cc0 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f  all kinds of pro
b2cd0 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65  blems for our te
b2ce0 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c  sts.  We.** coul
b2cf0 64 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65  d enhance SQLite
b2d00 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69   to deal with si
b2d10 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66  mulated malloc f
b2d20 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a  ailures within.*
b2d30 2a 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c  * the OS driver,
b2d40 20 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f   but the code to
b2d50 20 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65   deal with those
b2d60 20 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e   failure would n
b2d70 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73  ot.** be exercis
b2d80 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69  ed on Linux (whi
b2d90 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  ch does not need
b2da0 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20   to malloc() in 
b2db0 74 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61  the driver).** a
b2dc0 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68  nd so we would h
b2dd0 61 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77  ave difficulty w
b2de0 72 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20  riting coverage 
b2df0 74 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a  tests for that.*
b2e00 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20  * code.  Better 
b2e10 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64  to leave the cod
b2e20 65 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e  e out, we think.
b2e30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74  .**.** The point
b2e40 20 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73   of this discuss
b2e50 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  ion is as follow
b2e60 73 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e  s:  When creatin
b2e70 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61  g a new.** OS la
b2e80 79 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64  yer for an embed
b2e90 64 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79  ded system, if y
b2ea0 6f 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65  ou use this file
b2eb0 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a   as an example,.
b2ec0 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65  ** avoid the use
b2ed0 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65   of malloc()/fre
b2ee0 65 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74  e().  Those rout
b2ef0 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20  ines work ok on 
b2f00 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73  OS/2.** desktops
b2f10 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c   but not so well
b2f20 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73   in embedded sys
b2f30 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  tems..*/../*.** 
b2f40 4d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64  Macros used to d
b2f50 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
b2f60 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74   or not to use t
b2f70 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64  hreads..*/.#if d
b2f80 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48  efined(SQLITE_TH
b2f90 52 45 41 44 53 41 46 45 29 20 26 26 20 53 51 4c  READSAFE) && SQL
b2fa0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23  ITE_THREADSAFE.#
b2fb0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
b2fc0 53 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e  S2_THREADS 1.#en
b2fd0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75  dif../*.** Inclu
b2fe0 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  de code that is 
b2ff0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73  common to all os
b3000 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a  _*.c files.*/./*
b3010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
b3020 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  clude os_common.
b3030 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
b3040 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a  of os_os2.c ****
b3050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b3060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
b3070 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d  gin file os_comm
b3080 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
b3090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b30a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b30b0 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a  .** 2004 May 22.
b30c0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
b30d0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
b30e0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
b30f0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
b3100 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
b3110 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
b3120 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
b3130 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
b3140 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
b3150 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
b3160 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
b3170 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
b3180 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
b3190 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
b31a0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
b31b0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
b31c0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
b31d0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
b31e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b31f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3220 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
b3230 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63  ile contains mac
b3240 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65  ros and a little
b3250 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61   bit of code tha
b3260 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a  t is common to.*
b3270 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61  * all of the pla
b3280 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66  tform-specific f
b3290 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e  iles (os_*.c) an
b32a0 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69  d is #included i
b32b0 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c  nto those.** fil
b32c0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
b32d0 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69  ile should be #i
b32e0 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f  ncluded by the o
b32f0 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79  s_*.c files only
b3300 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a  .  It is not a.*
b3310 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  * general purpos
b3320 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a  e header file..*
b3330 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d  *.** $Id: os_com
b3340 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30  mon.h,v 1.38 200
b3350 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30  9/02/24 18:40:50
b3360 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
b3370 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f   $.*/.#ifndef _O
b3380 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66  S_COMMON_H_.#def
b3390 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ine _OS_COMMON_H
b33a0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  _../*.** At leas
b33b0 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
b33c0 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
b33d0 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
b33e0 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
b33f0 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
b3400 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
b3410 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
b3420 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
b3430 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
b3440 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
b3450 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
b3460 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
b3470 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
b3480 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
b3490 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
b34a0 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
b34b0 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
b34c0 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
b34d0 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
b34e0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
b34f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
b3500 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b3510 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d  sqlite3OSTrace =
b3520 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52   0;.#define OSTR
b3530 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20  ACE1(X)         
b3540 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
b3550 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
b3560 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69  gPrintf(X).#defi
b3570 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
b3580 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
b3590 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
b35a0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
b35b0 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
b35c0 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20  ACE3(X,Y,Z)     
b35d0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
b35e0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
b35f0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23  gPrintf(X,Y,Z).#
b3600 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28  define OSTRACE4(
b3610 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73  X,Y,Z,A)   if( s
b3620 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
b3630 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
b3640 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66  tf(X,Y,Z,A).#def
b3650 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59  ine OSTRACE5(X,Y
b3660 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69  ,Z,A,B) if( sqli
b3670 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
b3680 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
b3690 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
b36a0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
b36b0 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69  Z,A,B,C) \.    i
b36c0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
b36d0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
b36e0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
b36f0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
b3700 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
b3710 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
b3720 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
b3730 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
b3740 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c  Y,Z,A,B,C,D).#el
b3750 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  se.#define OSTRA
b3760 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f  CE1(X).#define O
b3770 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65  STRACE2(X,Y).#de
b3780 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
b3790 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Y,Z).#define OST
b37a0 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23  RACE4(X,Y,Z,A).#
b37b0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
b37c0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
b37d0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
b37e0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
b37f0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
b3800 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a  A,B,C,D).#endif.
b3810 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
b3820 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72  r performance tr
b3830 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79  acing.  Normally
b3840 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e   turned off.  On
b3850 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69  ly works.** on i
b3860 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f  486 hardware..*/
b3870 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50  .#ifdef SQLITE_P
b3880 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45  ERFORMANCE_TRACE
b3890 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e  ../* .** hwtime.
b38a0 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  h contains inlin
b38b0 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65  e assembler code
b38c0 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e   for implementin
b38d0 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f  g .** high-perfo
b38e0 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f  rmance timing ro
b38f0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  utines..*/./****
b3900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
b3910 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74  de hwtime.h in t
b3920 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
b3930 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
b3940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
b3950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
b3960 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a   file hwtime.h *
b3970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
b39a0 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a   2008 May 27.**.
b39b0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
b39c0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
b39d0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
b39e0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
b39f0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
b3a00 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
b3a10 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
b3a20 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
b3a30 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
b3a40 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
b3a50 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
b3a60 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
b3a70 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
b3a80 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
b3a90 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
b3aa0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
b3ab0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
b3ac0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
b3ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3b10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
b3b20 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
b3b30 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65   asm code for re
b3b40 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70  trieving "high-p
b3b50 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63  erformance".** c
b3b60 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20  ounters for x86 
b3b70 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a  class CPUs..**.*
b3b80 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c  * $Id: hwtime.h,
b3b90 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31  v 1.3 2008/08/01
b3ba0 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20   14:33:15 shane 
b3bb0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
b3bc0 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66   _HWTIME_H_.#def
b3bd0 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a  ine _HWTIME_H_..
b3be0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
b3bf0 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  ing routine only
b3c00 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75   works on pentiu
b3c10 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65  m-class (or newe
b3c20 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a  r) processors..*
b3c30 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44  * It uses the RD
b3c40 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65  TSC opcode to re
b3c50 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75  ad the cycle cou
b3c60 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20  nt value out of 
b3c70 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72  the.** processor
b3c80 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61   and returns tha
b3c90 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63  t value.  This c
b3ca0 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68  an be used for h
b3cb0 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69  igh-res.** profi
b3cc0 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65  ling..*/.#if (de
b3cd0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
b3ce0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  || defined(_MSC_
b3cf0 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20  VER)) && \.     
b3d00 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20   (defined(i386) 
b3d10 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38  || defined(__i38
b3d20 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  6__) || defined(
b3d30 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66  _M_IX86))..  #if
b3d40 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f   defined(__GNUC_
b3d50 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  _)..  __inline__
b3d60 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
b3d70 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
b3d80 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65  d){.     unsigne
b3d90 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20  d int lo, hi;.  
b3da0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
b3db0 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
b3dc0 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d   : "=a" (lo), "=
b3dd0 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72  d" (hi));.     r
b3de0 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69  eturn (sqlite_ui
b3df0 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20  nt64)hi << 32 | 
b3e00 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66  lo;.  }..  #elif
b3e10 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
b3e20 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63  R)..  __declspec
b3e30 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65  (naked) __inline
b3e40 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f   sqlite_uint64 _
b3e50 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77  _cdecl sqlite3Hw
b3e60 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
b3e70 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20   __asm {.       
b3e80 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72   rdtsc.        r
b3e90 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72  et       ; retur
b3ea0 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45  n value at EDX:E
b3eb0 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  AX.     }.  }.. 
b3ec0 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28   #endif..#elif (
b3ed0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
b3ee0 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78  ) && defined(__x
b3ef0 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69  86_64__))..  __i
b3f00 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
b3f10 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
b3f20 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
b3f30 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76   unsigned long v
b3f40 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  al;.      __asm_
b3f50 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
b3f60 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28  "rdtsc" : "=A" (
b3f70 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74  val));.      ret
b3f80 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23  urn val;.  }. .#
b3f90 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
b3fa0 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
b3fb0 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20  ed(__ppc__))..  
b3fc0 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
b3fd0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
b3fe0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
b3ff0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
b4000 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20  g long retval;. 
b4010 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
b4020 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f  ng junk;.      _
b4030 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
b4040 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20  e__ ("\n\.      
b4050 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62      1:      mftb
b4060 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20  u   %1\n\.      
b4070 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
b4080 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20      %L0\n\.     
b4090 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74               mft
b40a0 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20  bu   %0\n\.     
b40b0 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70               cmp
b40c0 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20  w    %0,%1\n\.  
b40d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b40e0 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20  bne     1b".    
b40f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20                : 
b4100 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22  "=r" (retval), "
b4110 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20  =r" (junk));.   
b4120 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c     return retval
b4130 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20  ;.  }..#else..  
b4140 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c  #error Need impl
b4150 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
b4160 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
b4170 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e  r your platform.
b4180 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63  ..  /*.  ** To c
b4190 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69  ompile without i
b41a0 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69  mplementing sqli
b41b0 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
b41c0 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20  your platform,. 
b41d0 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f   ** you can remo
b41e0 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72  ve the above #er
b41f0 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20  ror and use the 
b4200 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73  following.  ** s
b4210 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59  tub function.  Y
b4220 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d  ou will lose tim
b4230 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20  ing support for 
b4240 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65  many.  ** of the
b4250 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74   debugging and t
b4260 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73  esting utilities
b4270 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20  , but it should 
b4280 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f  at.  ** least co
b4290 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20  mpile and run.. 
b42a0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
b42b0 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74  TE   sqlite_uint
b42c0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
b42d0 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28  (void){ return (
b42e0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
b42f0 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65  ); }..#endif..#e
b4300 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
b4310 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a  (_HWTIME_H_) */.
b4320 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b4330 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68   End of hwtime.h
b4340 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
b4350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b4370 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b4380 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
b4390 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
b43a0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
b43b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b43c0 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  ..static sqlite_
b43d0 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a  uint64 g_start;.
b43e0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69  static sqlite_ui
b43f0 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a  nt64 g_elapsed;.
b4400 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54  #define TIMER_ST
b4410 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72  ART       g_star
b4420 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  t=sqlite3Hwtime(
b4430 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  ).#define TIMER_
b4440 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c  END         g_el
b4450 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74  apsed=sqlite3Hwt
b4460 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64  ime()-g_start.#d
b4470 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
b4480 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65  SED     g_elapse
b4490 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  d.#else.#define 
b44a0 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66  TIMER_START.#def
b44b0 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64  ine TIMER_END.#d
b44c0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
b44d0 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65  SED     ((sqlite
b44e0 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69  _uint64)0).#endi
b44f0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63  f../*.** If we c
b4500 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20  ompile with the 
b4510 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72  SQLITE_TEST macr
b4520 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  o set, then the 
b4530 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a  following block.
b4540 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20  ** of code will 
b4550 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c  give us the abil
b4560 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20  ity to simulate 
b4570 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72  a disk I/O error
b4580 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73  .  This.** is us
b4590 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74  ed for testing t
b45a0 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20  he I/O recovery 
b45b0 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66  logic..*/.#ifdef
b45c0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
b45d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
b45e0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
b45f0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
b4600 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
b4610 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a   of I/O Errors *
b4620 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
b4630 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
b4640 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20  r_hardhit = 0;  
b4650 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
b4660 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72  of non-benign er
b4670 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  rors */.SQLITE_A
b4680 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
b4690 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
b46a0 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  = 0;        /* C
b46b0 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72  ount down to fir
b46c0 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a  st I/O error */.
b46d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
b46e0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
b46f0 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20  persist = 0;    
b4700 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49      /* True if I
b4710 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73  /O errors persis
b4720 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  t */.SQLITE_API 
b4730 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
b4740 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b  rror_benign = 0;
b4750 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
b4760 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62   if errors are b
b4770 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f  enign */.SQLITE_
b4780 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
b4790 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
b47a0 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49   = 0;.SQLITE_API
b47b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73   int sqlite3_dis
b47c0 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69  kfull = 0;.#defi
b47d0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
b47e0 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69  orBenign(X) sqli
b47f0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
b4800 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20  ign=(X).#define 
b4810 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
b4820 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28  CODE)  \.  if( (
b4830 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
b4840 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69  _persist && sqli
b4850 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
b4860 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  ) \.       || sq
b4870 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
b4880 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20  ending-- == 1 ) 
b4890 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
b48a0 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29   { local_ioerr()
b48b0 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63  ; CODE; }.static
b48c0 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72   void local_ioer
b48d0 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28  r(){.  IOTRACE((
b48e0 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73  "IOERR\n"));.  s
b48f0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
b4900 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71  hit++;.  if( !sq
b4910 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
b4920 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f  enign ) sqlite3_
b4930 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
b4940 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69  ++;.}.#define Si
b4950 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
b4960 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69  ror(CODE) \.   i
b4970 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
b4980 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c  ull_pending ){ \
b4990 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  .     if( sqlite
b49a0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
b49b0 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20  ng == 1 ){ \.   
b49c0 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28      local_ioerr(
b49d0 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  ); \.       sqli
b49e0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31  te3_diskfull = 1
b49f0 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  ; \.       sqlit
b4a00 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20  e3_io_error_hit 
b4a10 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f  = 1; \.       CO
b4a20 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65  DE; \.     }else
b4a30 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  { \.       sqlit
b4a40 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
b4a50 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20  ing--; \.     } 
b4a60 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65  \.   }.#else.#de
b4a70 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
b4a80 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64  rrorBenign(X).#d
b4a90 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
b4aa0 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65  Error(A).#define
b4ab0 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
b4ac0 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66  lError(A).#endif
b4ad0 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73  ../*.** When tes
b4ae0 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75  ting, keep a cou
b4af0 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  nt of the number
b4b00 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a   of open files..
b4b10 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
b4b20 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
b4b30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
b4b40 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30  n_file_count = 0
b4b50 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f  ;.#define OpenCo
b4b60 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65  unter(X)  sqlite
b4b70 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e  3_open_file_coun
b4b80 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65  t+=(X).#else.#de
b4b90 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72  fine OpenCounter
b4ba0 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  (X).#endif..#end
b4bb0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
b4bc0 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f  OS_COMMON_H_) */
b4bd0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
b4be0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d  * End of os_comm
b4bf0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
b4c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4c20 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
b4c30 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
b4c40 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
b4c50 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a  n os_os2.c *****
b4c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4c70 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32  /../*.** The os2
b4c80 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69  File structure i
b4c90 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71  s subclass of sq
b4ca0 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69  lite3_file speci
b4cb0 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32  fic for the OS/2
b4cc0 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20  .** protability 
b4cd0 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65  layer..*/.typede
b4ce0 66 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65  f struct os2File
b4cf0 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74   os2File;.struct
b4d00 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e   os2File {.  con
b4d10 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
b4d20 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20  thods *pMethod; 
b4d30 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66   /* Always the f
b4d40 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20  irst entry */.  
b4d50 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20  HFILE h;        
b4d60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e            /* Han
b4d70 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e  dle for accessin
b4d80 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  g the file */.  
b4d90 63 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b  char* pathToDel;
b4da0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
b4db0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c  e of file to del
b4dc0 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55  ete on close, NU
b4dd0 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75  LL if not */.  u
b4de0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63  nsigned char loc
b4df0 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65  ktype;   /* Type
b4e00 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74   of lock current
b4e10 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20  ly held on this 
b4e20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66  file */.};..#def
b4e30 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ine LOCK_TIMEOUT
b4e40 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61   10L /* the defa
b4e50 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65  ult locking time
b4e60 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  out */../*******
b4e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4eb0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  ******.** The ne
b4ec0 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74  xt group of rout
b4ed0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74  ines implement t
b4ee0 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73  he I/O methods s
b4ef0 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74  pecified.** by t
b4f00 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
b4f10 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
b4f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
b4f70 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c  *.** Close a fil
b4f80 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
b4f90 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74   os2Close( sqlit
b4fa0 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20  e3_file *id ){. 
b4fb0 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
b4fc0 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65  ERROR;.  os2File
b4fd0 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69   *pFile;.  if( i
b4fe0 64 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f  d && (pFile = (o
b4ff0 73 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30  s2File*)id) != 0
b5000 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32   ){.    OSTRACE2
b5010 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20  ( "CLOSE %d\n", 
b5020 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20  pFile->h );.    
b5030 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70  rc = DosClose( p
b5040 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70  File->h );.    p
b5050 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
b5060 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66   NO_LOCK;.    if
b5070 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
b5080 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20  el != NULL ){.  
b5090 20 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63      rc = DosForc
b50a0 65 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46  eDelete( (PSZ)pF
b50b0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29  ile->pathToDel )
b50c0 3b 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46  ;.      free( pF
b50d0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29  ile->pathToDel )
b50e0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70  ;.      pFile->p
b50f0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
b5100 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20  .    }.    id = 
b5110 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74  0;.    OpenCount
b5120 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20  er( -1 );.  }.. 
b5130 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
b5140 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
b5150 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
b5160 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  R;.}../*.** Read
b5170 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c   data from a fil
b5180 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e  e into a buffer.
b5190 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
b51a0 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74  OK if all.** byt
b51b0 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63  es were read suc
b51c0 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51  cessfully and SQ
b51d0 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e  LITE_IOERR if an
b51e0 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77  ything goes.** w
b51f0 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rong..*/.static 
b5200 69 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73  int os2Read(.  s
b5210 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
b5220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
b5230 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66  * File to read f
b5240 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  rom */.  void *p
b5250 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
b5260 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
b5270 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74  e content into t
b5280 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20  his buffer */.  
b5290 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
b52a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b52b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
b52c0 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20  es to read */.  
b52d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66  sqlite3_int64 of
b52e0 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20  fset            
b52f0 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67  /* Begin reading
b5300 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
b5310 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69  */.){.  ULONG fi
b5320 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b  leLocation = 0L;
b5330 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20  .  ULONG got;.  
b5340 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  os2File *pFile =
b5350 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20   (os2File*)id;. 
b5360 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
b5370 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
b5380 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
b5390 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b  TE_IOERR_READ );
b53a0 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45  .  OSTRACE3( "RE
b53b0 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  AD %d lock=%d\n"
b53c0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
b53d0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
b53e0 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50   if( DosSetFileP
b53f0 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66  tr(pFile->h, off
b5400 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c  set, FILE_BEGIN,
b5410 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20   &fileLocation) 
b5420 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  != NO_ERROR ){. 
b5430 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
b5440 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66  _IOERR;.  }.  if
b5450 28 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65  ( DosRead( pFile
b5460 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20  ->h, pBuf, amt, 
b5470 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52  &got ) != NO_ERR
b5480 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
b5490 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45   SQLITE_IOERR_RE
b54a0 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f  AD;.  }.  if( go
b54b0 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20  t == (ULONG)amt 
b54c0 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ).    return SQL
b54d0 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b  ITE_OK;.  else {
b54e0 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70  .    /* Unread p
b54f0 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69  ortions of the i
b5500 6e 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74  nput buffer must
b5510 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20   be zero-filled 
b5520 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28  */.    memset(&(
b5530 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74  (char*)pBuf)[got
b5540 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a  ], 0, amt-got);.
b5550 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
b5560 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45  E_IOERR_SHORT_RE
b5570 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  AD;.  }.}../*.**
b5580 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d   Write data from
b5590 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61   a buffer into a
b55a0 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53   file.  Return S
b55b0 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
b55c0 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f  ess.** or some o
b55d0 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20  ther error code 
b55e0 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73  on failure..*/.s
b55f0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69  tatic int os2Wri
b5600 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  te(.  sqlite3_fi
b5610 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20  le *id,         
b5620 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f        /* File to
b5630 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20   write into */. 
b5640 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75   const void *pBu
b5650 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
b5660 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f   /* The bytes to
b5670 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20   be written */. 
b5680 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20   int amt,       
b5690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b56a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
b56b0 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
b56c0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
b56d0 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20  offset          
b56e0 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
b56f0 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67   the file to beg
b5700 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f  in writing at */
b5710 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65  .){.  ULONG file
b5720 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20  Location = 0L;. 
b5730 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
b5740 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77  ERROR;.  ULONG w
b5750 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20  rote;.  os2File 
b5760 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
b5770 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28  e*)id;.  assert(
b5780 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75   id!=0 );.  Simu
b5790 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
b57a0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
b57b0 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75  _WRITE );.  Simu
b57c0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
b57d0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
b57e0 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41  _FULL );.  OSTRA
b57f0 43 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c  CE3( "WRITE %d l
b5800 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
b5810 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
b5820 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f  type );.  if( Do
b5830 73 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c  sSetFilePtr(pFil
b5840 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49  e->h, offset, FI
b5850 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c  LE_BEGIN, &fileL
b5860 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45  ocation) != NO_E
b5870 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75  RROR ){.    retu
b5880 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
b5890 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61  .  }.  assert( a
b58a0 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  mt>0 );.  while(
b58b0 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20   amt > 0 &&.    
b58c0 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57       ( rc = DosW
b58d0 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20  rite( pFile->h, 
b58e0 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74  (PVOID)pBuf, amt
b58f0 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20  , &wrote ) ) == 
b5900 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20  NO_ERROR &&.    
b5910 20 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20       wrote > 0. 
b5920 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77   ){.    amt -= w
b5930 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d  rote;.    pBuf =
b5940 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b   &((char*)pBuf)[
b5950 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72  wrote];.  }..  r
b5960 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f  eturn ( rc != NO
b5970 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20  _ERROR || amt > 
b5980 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53  (int)wrote ) ? S
b5990 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c  QLITE_FULL : SQL
b59a0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
b59b0 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65   Truncate an ope
b59c0 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63  n file to a spec
b59d0 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74  ified size.*/.st
b59e0 61 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e  atic int os2Trun
b59f0 63 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69  cate( sqlite3_fi
b5a00 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74  le *id, i64 nByt
b5a10 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63  e ){.  APIRET rc
b5a20 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f   = NO_ERROR;.  o
b5a30 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
b5a40 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
b5a50 4f 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43  OSTRACE3( "TRUNC
b5a60 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20  ATE %d %lld\n", 
b5a70 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20  pFile->h, nByte 
b5a80 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
b5a90 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
b5aa0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
b5ab0 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73  TE );.  rc = Dos
b5ac0 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69  SetFileSize( pFi
b5ad0 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a  le->h, nByte );.
b5ae0 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e    return rc == N
b5af0 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45  O_ERROR ? SQLITE
b5b00 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
b5b10 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a  RR_TRUNCATE;.}..
b5b20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
b5b30 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74  ST./*.** Count t
b5b40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c  he number of ful
b5b50 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61  lsyncs and norma
b5b60 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69  l syncs.  This i
b5b70 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a  s used to test.*
b5b80 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64  * that syncs and
b5b90 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f   fullsyncs are o
b5ba0 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72  ccuring at the r
b5bb0 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53  ight times..*/.S
b5bc0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
b5bd0 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74  lite3_sync_count
b5be0 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49   = 0;.SQLITE_API
b5bf0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c   int sqlite3_ful
b5c00 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b  lsync_count = 0;
b5c10 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
b5c20 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69  ake sure all wri
b5c30 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75  tes to a particu
b5c40 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d  lar file are com
b5c50 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a  mitted to disk..
b5c60 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
b5c70 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66  2Sync( sqlite3_f
b5c80 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
b5c90 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20  gs ){.  os2File 
b5ca0 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
b5cb0 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45  e*)id;.  OSTRACE
b5cc0 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b  3( "SYNC %d lock
b5cd0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
b5ce0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
b5cf0 65 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  e );.#ifdef SQLI
b5d00 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c  TE_TEST.  if( fl
b5d10 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e  ags & SQLITE_SYN
b5d20 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c  C_FULL){.    sql
b5d30 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
b5d40 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c  unt++;.  }.  sql
b5d50 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b  ite3_sync_count+
b5d60 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49  +;.#endif.  /* I
b5d70 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  f we compiled wi
b5d80 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f  th the SQLITE_NO
b5d90 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e  _SYNC flag, then
b5da0 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20   syncing is a.  
b5db0 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69  ** no-op.  */.#i
b5dc0 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  fdef SQLITE_NO_S
b5dd0 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  YNC.  UNUSED_PAR
b5de0 41 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20  AMETER(pFile);. 
b5df0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
b5e00 4b 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72  K;.#else.  retur
b5e10 6e 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72  n DosResetBuffer
b5e20 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20  ( pFile->h ) == 
b5e30 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54  NO_ERROR ? SQLIT
b5e40 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f  E_OK : SQLITE_IO
b5e50 45 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  ERR;.#endif.}../
b5e60 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74  *.** Determine t
b5e70 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
b5e80 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74  of a file in byt
b5e90 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  es.*/.static int
b5ea0 20 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71   os2FileSize( sq
b5eb0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
b5ec0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
b5ed0 53 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54  Size ){.  APIRET
b5ee0 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
b5ef0 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73    FILESTATUS3 fs
b5f00 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d  ts3FileInfo;.  m
b5f10 65 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65  emset(&fsts3File
b5f20 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Info, 0, sizeof(
b5f30 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b  fsts3FileInfo));
b5f40 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
b5f50 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   );.  SimulateIO
b5f60 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
b5f70 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
b5f80 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75   );.  rc = DosQu
b5f90 65 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f  eryFileInfo( ((o
b5fa0 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  s2File*)id)->h, 
b5fb0 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66  FIL_STANDARD, &f
b5fc0 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69  sts3FileInfo, si
b5fd0 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33  zeof(FILESTATUS3
b5fe0 29 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d  ) );.  if( rc ==
b5ff0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
b6000 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46   *pSize = fsts3F
b6010 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a  ileInfo.cbFile;.
b6020 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
b6030 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
b6040 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
b6050 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20  _IOERR_FSTAT;.  
b6060 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69  }.}../*.** Acqui
b6070 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b  re a reader lock
b6080 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
b6090 67 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32  getReadLock( os2
b60a0 46 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20  File *pFile ){. 
b60b0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
b60c0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
b60d0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41   UnlockArea;.  A
b60e0 50 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d  PIRET res;.  mem
b60f0 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30  set(&LockArea, 0
b6100 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65  , sizeof(LockAre
b6110 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55  a));.  memset(&U
b6120 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  nlockArea, 0, si
b6130 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29  zeof(UnlockArea)
b6140 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  );.  LockArea.lO
b6150 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
b6160 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61  IRST;.  LockArea
b6170 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44  .lRange = SHARED
b6180 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41  _SIZE;.  UnlockA
b6190 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
b61a0 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c  ;.  UnlockArea.l
b61b0 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65  Range = 0L;.  re
b61c0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
b61d0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
b61e0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
b61f0 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
b6200 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54  OUT, 1L );.  OST
b6210 52 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c  RACE3( "GETREADL
b6220 4f 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22  OCK %d res=%d\n"
b6230 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
b6240 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b  );.  return res;
b6250 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61  .}../*.** Undo a
b6260 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61   readlock.*/.sta
b6270 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65  tic int unlockRe
b6280 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20  adLock( os2File 
b6290 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43  *id ){.  FILELOC
b62a0 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20  K  LockArea,.   
b62b0 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41           UnlockA
b62c0 72 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65  rea;.  APIRET re
b62d0 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  s;.  memset(&Loc
b62e0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
b62f0 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d  (LockArea));.  m
b6300 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65  emset(&UnlockAre
b6310 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c  a, 0, sizeof(Unl
b6320 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63  ockArea));.  Loc
b6330 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
b6340 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  0L;.  LockArea.l
b6350 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e  Range = 0L;.  Un
b6360 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
b6370 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
b6380 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52  .  UnlockArea.lR
b6390 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49  ange = SHARED_SI
b63a0 5a 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53  ZE;.  res = DosS
b63b0 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d  etFileLocks( id-
b63c0 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  >h, &UnlockArea,
b63d0 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b   &LockArea, LOCK
b63e0 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a  _TIMEOUT, 1L );.
b63f0 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c    OSTRACE3( "UNL
b6400 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c  OCK-READLOCK fil
b6410 65 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d  e handle=%d res=
b6420 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72  %d?\n", id->h, r
b6430 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  es );.  return r
b6440 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  es;.}../*.** Loc
b6450 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20  k the file with 
b6460 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  the lock specifi
b6470 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
b6480 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a  locktype - one.*
b6490 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
b64a0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
b64b0 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
b64c0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
b64d0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
b64e0 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
b64f0 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
b6500 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
b6510 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
b6520 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
b6530 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
b6540 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
b6550 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
b6560 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
b6570 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
b6580 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
b6590 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
b65a0 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
b65b0 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
b65c0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
b65d0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
b65e0 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
b65f0 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
b6600 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
b6610 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
b6620 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
b6630 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
b6640 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
b6650 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
b6660 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
b6670 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
b6680 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
b6690 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
b66a0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
b66b0 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
b66c0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
b66d0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
b66e0 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
b66f0 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54  XCLUSIVE.**.** T
b6700 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
b6710 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
b6720 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55   lock.  The os2U
b6730 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a  nlock() routine.
b6740 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f  ** erases all lo
b6750 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20  cks at once and 
b6760 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64  returns us immed
b6770 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e  iately to lockin
b6780 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74  g level 0..** It
b6790 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
b67a0 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f   to lower the lo
b67b0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20  cking level one 
b67c0 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20  step at a time. 
b67d0 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20   You.** must go 
b67e0 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b  straight to lock
b67f0 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a  ing level 0..*/.
b6800 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f  static int os2Lo
b6810 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck( sqlite3_file
b6820 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
b6830 70 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pe ){.  int rc =
b6840 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
b6850 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
b6860 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65   from subroutine
b6870 73 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65  s */.  APIRET re
b6880 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20  s = NO_ERROR;   
b6890 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e   /* Result of an
b68a0 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20   OS/2 lock call 
b68b0 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b  */.  int newLock
b68c0 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53  type;       /* S
b68d0 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  et pFile->lockty
b68e0 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65  pe to this value
b68f0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
b6900 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64  */.  int gotPend
b6910 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54  ingLock = 0;/* T
b6920 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72  rue if we acquir
b6930 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ed a PENDING loc
b6940 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20  k this time */. 
b6950 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
b6960 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
b6970 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f   UnlockArea;.  o
b6980 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
b6990 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
b69a0 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61  memset(&LockArea
b69b0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b  , 0, sizeof(Lock
b69c0 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Area));.  memset
b69d0 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
b69e0 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
b69f0 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ea));.  assert( 
b6a00 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53  pFile!=0 );.  OS
b6a10 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64  TRACE4( "LOCK %d
b6a20 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70   %d was %d\n", p
b6a30 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
b6a40 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  e, pFile->lockty
b6a50 70 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  pe );..  /* If t
b6a60 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
b6a70 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74  a lock of this t
b6a80 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74  ype or more rest
b6a90 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20  rictive on the. 
b6aa0 20 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20   ** os2File, do 
b6ab0 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75  nothing. Don't u
b6ac0 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a  se the end_lock:
b6ad0 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20   exit path, as. 
b6ae0 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   ** sqlite3_mute
b6af0 78 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74  x_enter() hasn't
b6b00 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74   been called yet
b6b10 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ..  */.  if( pFi
b6b20 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f  le->locktype>=lo
b6b30 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53  cktype ){.    OS
b6b40 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
b6b50 20 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20   %d ok (already 
b6b60 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  held)\n", pFile-
b6b70 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a  >h, locktype );.
b6b80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
b6b90 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
b6ba0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f  Make sure the lo
b6bb0 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  cking sequence i
b6bc0 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20  s correct.  */. 
b6bd0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
b6be0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
b6bf0 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  K || locktype==S
b6c00 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
b6c10 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
b6c20 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  !=PENDING_LOCK )
b6c30 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
b6c40 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c  type!=RESERVED_L
b6c50 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f  OCK || pFile->lo
b6c60 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
b6c70 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  OCK );..  /* Loc
b6c80 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f  k the PENDING_LO
b6c90 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65  CK byte if we ne
b6ca0 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20  ed to acquire a 
b6cb0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a  PENDING lock or.
b6cc0 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f    ** a SHARED lo
b6cd0 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61  ck.  If we are a
b6ce0 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45  cquiring a SHARE
b6cf0 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75  D lock, the acqu
b6d00 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20  isition of.  ** 
b6d10 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  the PENDING_LOCK
b6d20 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61   byte is tempora
b6d30 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f  ry..  */.  newLo
b6d40 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e  cktype = pFile->
b6d50 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20  locktype;.  if( 
b6d60 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
b6d70 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c  =NO_LOCK.      |
b6d80 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  | (locktype==EXC
b6d90 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
b6da0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
b6db0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20  RESERVED_LOCK). 
b6dc0 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61   ){.    LockArea
b6dd0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49  .lOffset = PENDI
b6de0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63  NG_BYTE;.    Loc
b6df0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31  kArea.lRange = 1
b6e00 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
b6e10 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
b6e20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
b6e30 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20  Range = 0L;..   
b6e40 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20   /* wait longer 
b6e50 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  than LOCK_TIMEOU
b6e60 54 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61  T here not to ha
b6e70 76 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70  ve to try multip
b6e80 6c 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20  le times */.    
b6e90 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
b6ea0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
b6eb0 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
b6ec0 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30  ockArea, 100L, 0
b6ed0 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73  L );.    if( res
b6ee0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
b6ef0 20 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67        gotPending
b6f00 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20  Lock = 1;.      
b6f10 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20  OSTRACE3( "LOCK 
b6f20 25 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20  %d pending lock 
b6f30 62 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65  boolean set.  re
b6f40 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
b6f50 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a  h, res );.    }.
b6f60 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
b6f70 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a  e a shared lock.
b6f80 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
b6f90 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
b6fa0 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52   && res == NO_ER
b6fb0 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ROR ){.    asser
b6fc0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
b6fd0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20  pe==NO_LOCK );. 
b6fe0 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64     res = getRead
b6ff0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
b7000 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45   if( res == NO_E
b7010 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65  RROR ){.      ne
b7020 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  wLocktype = SHAR
b7030 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20  ED_LOCK;.    }. 
b7040 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f     OSTRACE3( "LO
b7050 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68  CK %d acquire sh
b7060 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25  ared lock. res=%
b7070 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
b7080 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  res );.  }..  /*
b7090 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52   Acquire a RESER
b70a0 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20  VED lock.  */.  
b70b0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45  if( locktype==RE
b70c0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72  SERVED_LOCK && r
b70d0 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
b70e0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
b70f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
b7100 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
b7110 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
b7120 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  et = RESERVED_BY
b7130 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  TE;.    LockArea
b7140 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
b7150 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
b7160 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
b7170 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
b7180 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 0L;.    res =
b7190 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
b71a0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
b71b0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
b71c0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
b71d0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20  , 0L );.    if( 
b71e0 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
b71f0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
b7200 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f  type = RESERVED_
b7210 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  LOCK;.    }.    
b7220 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20  OSTRACE3( "LOCK 
b7230 25 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72  %d acquire reser
b7240 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64  ved lock. res=%d
b7250 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
b7260 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  es );.  }..  /* 
b7270 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e  Acquire a PENDIN
b7280 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  G lock.  */.  if
b7290 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c  ( locktype==EXCL
b72a0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65  USIVE_LOCK && re
b72b0 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
b72c0 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65  .    newLocktype
b72d0 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b   = PENDING_LOCK;
b72e0 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c  .    gotPendingL
b72f0 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54  ock = 0;.    OST
b7300 52 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20  RACE2( "LOCK %d 
b7310 61 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20  acquire pending 
b7320 6c 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f  lock. pending lo
b7330 63 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74  ck boolean unset
b7340 2e 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29  .\n", pFile->h )
b7350 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
b7360 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ire an EXCLUSIVE
b7370 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
b7380 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
b7390 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
b73a0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
b73b0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
b73c0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41  e->locktype>=SHA
b73d0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
b73e0 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64  res = unlockRead
b73f0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
b7400 20 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65   OSTRACE2( "unre
b7410 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20  adlock = %d\n", 
b7420 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41  res );.    LockA
b7430 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48  rea.lOffset = SH
b7440 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
b7450 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
b7460 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
b7470 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
b7480 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
b7490 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
b74a0 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20  e = 0L;.    res 
b74b0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
b74c0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
b74d0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
b74e0 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
b74f0 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28  T, 0L );.    if(
b7500 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
b7510 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63   ){.      newLoc
b7520 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56  ktype = EXCLUSIV
b7530 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73  E_LOCK;.    }els
b7540 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  e{.      OSTRACE
b7550 32 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63  2( "OS/2 error-c
b7560 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73  ode = %d\n", res
b7570 20 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61   );.      getRea
b7580 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20  dLock(pFile);.  
b7590 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33    }.    OSTRACE3
b75a0 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69  ( "LOCK %d acqui
b75b0 72 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  re exclusive loc
b75c0 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  k.  res=%d\n", p
b75d0 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
b75e0 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
b75f0 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45  are holding a PE
b7600 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20  NDING lock that 
b7610 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65  ought to be rele
b7620 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  ased, then.  ** 
b7630 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a  release it now..
b7640 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65    */.  if( gotPe
b7650 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63  ndingLock && loc
b7660 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
b7670 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b  CK ){.    int r;
b7680 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
b7690 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
b76a0 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
b76b0 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
b76c0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50  Area.lOffset = P
b76d0 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
b76e0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
b76f0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d  ge = 1L;.    r =
b7700 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
b7710 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
b7720 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
b7730 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
b7740 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
b7750 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75  ACE3( "LOCK %d u
b7760 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67  nlocking pending
b7770 2f 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64  /is shared. r=%d
b7780 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
b7790 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70   );.  }..  /* Up
b77a0 64 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f  date the state o
b77b0 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68  f the lock has h
b77c0 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20  eld in the file 
b77d0 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a  descriptor then.
b77e0 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20    ** return the 
b77f0 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75  appropriate resu
b7800 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  lt code..  */.  
b7810 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52  if( res == NO_ER
b7820 52 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ROR ){.    rc = 
b7830 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
b7840 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34  se{.    OSTRACE4
b7850 28 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25  ( "LOCK FAILED %
b7860 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20  d trying for %d 
b7870 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70  but got %d\n", p
b7880 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
b7890 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c         locktype,
b78a0 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a   newLocktype );.
b78b0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
b78c0 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c  BUSY;.  }.  pFil
b78d0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65  e->locktype = ne
b78e0 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54  wLocktype;.  OST
b78f0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20  RACE3( "LOCK %d 
b7900 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  now %d\n", pFile
b7910 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
b7920 74 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e  type );.  return
b7930 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
b7940 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
b7950 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
b7960 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
b7970 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
b7980 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
b7990 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
b79a0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
b79b0 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
b79c0 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d  , return.** non-
b79d0 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20  zero, otherwise 
b79e0 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  zero..*/.static 
b79f0 69 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65  int os2CheckRese
b7a00 72 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65  rvedLock( sqlite
b7a10 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
b7a20 2a 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72  *pOut ){.  int r
b7a30 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20   = 0;.  os2File 
b7a40 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
b7a50 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28  e*)id;.  assert(
b7a60 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69   pFile!=0 );.  i
b7a70 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
b7a80 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe>=RESERVED_LOC
b7a90 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a  K ){.    r = 1;.
b7aa0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54      OSTRACE3( "T
b7ab0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
b7ac0 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46  d (local)\n", pF
b7ad0 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d  ile->h, r );.  }
b7ae0 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f  else{.    FILELO
b7af0 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20  CK  LockArea,.  
b7b00 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f              Unlo
b7b10 63 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52  ckArea;.    APIR
b7b20 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52  ET rc = NO_ERROR
b7b30 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f  ;.    memset(&Lo
b7b40 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
b7b50 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  f(LockArea));.  
b7b60 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b    memset(&Unlock
b7b70 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
b7b80 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  UnlockArea));.  
b7b90 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
b7ba0 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  et = RESERVED_BY
b7bb0 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  TE;.    LockArea
b7bc0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
b7bd0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
b7be0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
b7bf0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
b7c00 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20   = 0L;.    rc = 
b7c10 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
b7c20 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
b7c30 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
b7c40 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
b7c50 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
b7c60 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f  CE3( "TEST WR-LO
b7c70 43 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72  CK %d lock reser
b7c80 76 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e  ved byte rc=%d\n
b7c90 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20  ", pFile->h, rc 
b7ca0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d  );.    if( rc ==
b7cb0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
b7cc0 20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20     APIRET rcu = 
b7cd0 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74  NO_ERROR; /* ret
b7ce0 75 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c  urn code for unl
b7cf0 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20  ocking */.      
b7d00 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
b7d10 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63   = 0L;.      Loc
b7d20 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
b7d30 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41  L;.      UnlockA
b7d40 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45  rea.lOffset = RE
b7d50 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20  SERVED_BYTE;.   
b7d60 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52     UnlockArea.lR
b7d70 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20  ange = 1L;.     
b7d80 20 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c   rcu = DosSetFil
b7d90 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
b7da0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
b7db0 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
b7dc0 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20  IMEOUT, 0L );.  
b7dd0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54      OSTRACE3( "T
b7de0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75  EST WR-LOCK %d u
b7df0 6e 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62  nlock reserved b
b7e00 79 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69  yte r=%d\n", pFi
b7e10 6c 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20  le->h, rcu );.  
b7e20 20 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63    }.    r = !(rc
b7e30 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20   == NO_ERROR);. 
b7e40 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45     OSTRACE3( "TE
b7e50 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
b7e60 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46   (remote)\n", pF
b7e70 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d  ile->h, r );.  }
b7e80 0a 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20  .  *pOut = r;.  
b7e90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
b7ea0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72  ;.}../*.** Lower
b7eb0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
b7ec0 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72  el on file descr
b7ed0 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b  iptor id to lock
b7ee0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
b7ef0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
b7f00 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
b7f10 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
b7f20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
b7f30 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
b7f40 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
b7f50 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
b7f60 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
b7f70 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
b7f80 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
b7f90 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
b7fa0 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
b7fb0 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f  ible for this ro
b7fc0 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66  utine to fail if
b7fd0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
b7fe0 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f  ment.** is NO_LO
b7ff0 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f  CK.  If the seco
b8000 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53  nd argument is S
b8010 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20  HARED_LOCK then 
b8020 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
b8030 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c  might return SQL
b8040 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74  ITE_IOERR;.*/.st
b8050 61 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f  atic int os2Unlo
b8060 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck( sqlite3_file
b8070 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
b8080 70 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65  pe ){.  int type
b8090 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ;.  os2File *pFi
b80a0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
b80b0 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  d;.  APIRET rc =
b80c0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50   SQLITE_OK;.  AP
b80d0 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52  IRET res = NO_ER
b80e0 52 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20  ROR;.  FILELOCK 
b80f0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20   LockArea,.     
b8100 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
b8110 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  a;.  memset(&Loc
b8120 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
b8130 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d  (LockArea));.  m
b8140 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65  emset(&UnlockAre
b8150 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c  a, 0, sizeof(Unl
b8160 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73  ockArea));.  ass
b8170 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b  ert( pFile!=0 );
b8180 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
b8190 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
b81a0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20   );.  OSTRACE4( 
b81b0 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64  "UNLOCK %d to %d
b81c0 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c   was %d\n", pFil
b81d0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20  e->h, locktype, 
b81e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
b81f0 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c  );.  type = pFil
b8200 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69  e->locktype;.  i
b8210 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49  f( type>=EXCLUSI
b8220 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c  VE_LOCK ){.    L
b8230 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
b8240 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  = 0L;.    LockAr
b8250 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
b8260 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
b8270 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f  Offset = SHARED_
b8280 46 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63  FIRST;.    Unloc
b8290 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53  kArea.lRange = S
b82a0 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20  HARED_SIZE;.    
b82b0 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
b82c0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
b82d0 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
b82e0 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49  ockArea, LOCK_TI
b82f0 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20  MEOUT, 0L );.   
b8300 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f   OSTRACE3( "UNLO
b8310 43 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20  CK %d exclusive 
b8320 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20  lock res=%d\n", 
b8330 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
b8340 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  .    if( locktyp
b8350 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
b8360 26 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46  & getReadLock(pF
b8370 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ile) != NO_ERROR
b8380 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
b8390 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
b83a0 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c  appen.  We shoul
b83b0 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65  d always be able
b83c0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61   to.      ** rea
b83d0 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
b83e0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53  lock */.      OS
b83f0 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20  TRACE3( "UNLOCK 
b8400 25 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64  %d to %d getRead
b8410 4c 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22  Lock() failed\n"
b8420 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
b8430 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63  type );.      rc
b8440 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
b8450 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  UNLOCK;.    }.  
b8460 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45  }.  if( type>=RE
b8470 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
b8480 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
b8490 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
b84a0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
b84b0 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
b84c0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53  ea.lOffset = RES
b84d0 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20  ERVED_BYTE;.    
b84e0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
b84f0 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20  e = 1L;.    res 
b8500 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
b8510 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
b8520 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
b8530 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
b8540 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54  T, 0L );.    OST
b8550 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE3( "UNLOCK %
b8560 64 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25  d reserved res=%
b8570 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
b8580 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28  res );.  }.  if(
b8590 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f   locktype==NO_LO
b85a0 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52  CK && type>=SHAR
b85b0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
b85c0 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c  es = unlockReadL
b85d0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
b85e0 4f 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43  OSTRACE5( "UNLOC
b85f0 4b 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20  K %d is %d want 
b8600 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  %d res=%d\n", pF
b8610 69 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f  ile->h, type, lo
b8620 63 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20  cktype, res );. 
b8630 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50   }.  if( type>=P
b8640 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20  ENDING_LOCK ){. 
b8650 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
b8660 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
b8670 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
b8680 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
b8690 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e  ea.lOffset = PEN
b86a0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55  DING_BYTE;.    U
b86b0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
b86c0 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 1L;.    res =
b86d0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
b86e0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
b86f0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
b8700 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
b8710 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
b8720 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
b8730 20 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c   pending res=%d\
b8740 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
b8750 73 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65  s );.  }.  pFile
b8760 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
b8770 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45  ktype;.  OSTRACE
b8780 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f  3( "UNLOCK %d no
b8790 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  w %d\n", pFile->
b87a0 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
b87b0 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pe );.  return r
b87c0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74  c;.}../*.** Cont
b87d0 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66  rol and query of
b87e0 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68   the open file h
b87f0 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  andle..*/.static
b8800 20 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74   int os2FileCont
b8810 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  rol(sqlite3_file
b8820 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f   *id, int op, vo
b8830 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69  id *pArg){.  swi
b8840 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
b8850 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
b8860 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20  _LOCKSTATE: {.  
b8870 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20      *(int*)pArg 
b8880 3d 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29  = ((os2File*)id)
b8890 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  ->locktype;.    
b88a0 20 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e    OSTRACE3( "FCN
b88b0 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20  TL_LOCKSTATE %d 
b88c0 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73  lock=%d\n", ((os
b88d0 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28  2File*)id)->h, (
b88e0 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (os2File*)id)->l
b88f0 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20  ocktype );.     
b8900 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
b8910 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  K;.    }.  }.  r
b8920 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
b8930 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OR;.}../*.** Ret
b8940 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73  urn the sector s
b8950 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
b8960 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62  the underlying b
b8970 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a  lock device for.
b8980 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ** the specified
b8990 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
b89a0 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32  lmost always 512
b89b0 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20   bytes, but may 
b89c0 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72  be.** larger for
b89d0 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a   some devices..*
b89e0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65  *.** SQLite code
b89f0 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75   assumes this fu
b8a00 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61  nction cannot fa
b8a10 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75  il. It also assu
b8a20 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74  mes that.** if t
b8a30 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65  wo files are cre
b8a40 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  ated in the same
b8a50 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72   file-system dir
b8a60 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20  ectory (i.e..** 
b8a70 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69  a database and i
b8a80 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29  ts journal file)
b8a90 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72   that the sector
b8aa0 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68   size will be th
b8ab0 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f  e.** same for bo
b8ac0 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th..*/.static in
b8ad0 74 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28  t os2SectorSize(
b8ae0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
b8af0 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
b8b00 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
b8b10 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  R_SIZE;.}../*.**
b8b20 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72   Return a vector
b8b30 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61   of device chara
b8b40 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73  cteristics..*/.s
b8b50 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76  tatic int os2Dev
b8b60 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
b8b70 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
b8b80 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30  *id){.  return 0
b8b90 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72  ;.}.../*.** Char
b8ba0 61 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72  acter set conver
b8bb0 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65  sion objects use
b8bc0 64 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20  d by conversion 
b8bd0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61  routines..*/.sta
b8be0 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20  tic UconvObject 
b8bf0 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f  ucUtf8 = NULL; /
b8c00 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65  * convert betwee
b8c10 6e 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d  n UTF-8 and UCS-
b8c20 32 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e  2 */.static Ucon
b8c30 76 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20  vObject uclCp = 
b8c40 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72  NULL;  /* conver
b8c50 74 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20  t between local 
b8c60 63 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53  codepage and UCS
b8c70 2d 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c  -2 */../*.** Hel
b8c80 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
b8c90 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63  initialize the c
b8ca0 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74  onversion object
b8cb0 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54  s from and to UT
b8cc0 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  F-8..*/.static v
b8cd0 6f 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a  oid initUconvObj
b8ce0 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20  ects( void ){.  
b8cf0 69 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f  if( UniCreateUco
b8d00 6e 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c  nvObject( UTF_8,
b8d10 20 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c   &ucUtf8 ) != UL
b8d20 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20  S_SUCCESS ).    
b8d30 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20  ucUtf8 = NULL;. 
b8d40 20 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55   if ( UniCreateU
b8d50 63 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69  convObject( (Uni
b8d60 43 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79  Char *)L"@path=y
b8d70 65 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d  es", &uclCp ) !=
b8d80 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20   ULS_SUCCESS ). 
b8d90 20 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b     uclCp = NULL;
b8da0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72  .}../*.** Helper
b8db0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65   function to fre
b8dc0 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  e the conversion
b8dd0 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e   objects from an
b8de0 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73  d to UTF-8..*/.s
b8df0 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55  tatic void freeU
b8e00 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69  convObjects( voi
b8e10 64 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74  d ){.  if ( ucUt
b8e20 66 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65  f8 ).    UniFree
b8e30 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55  UconvObject( ucU
b8e40 74 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63  tf8 );.  if ( uc
b8e50 6c 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65  lCp ).    UniFre
b8e60 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63  eUconvObject( uc
b8e70 6c 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20  lCp );.  ucUtf8 
b8e80 3d 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20  = NULL;.  uclCp 
b8e90 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  = NULL;.}../*.**
b8ea0 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
b8eb0 20 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d   to convert UTF-
b8ec0 38 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c  8 filenames to l
b8ed0 6f 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61  ocal OS/2 codepa
b8ee0 67 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73  ge..** The two-s
b8ef0 74 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72  tep process: fir
b8f00 73 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69  st convert the i
b8f10 6e 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74  ncoming UTF-8 st
b8f20 72 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53  ring.** into UCS
b8f30 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d  -2 and then from
b8f40 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75   UCS-2 to the cu
b8f50 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a  rrent codepage..
b8f60 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
b8f70 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73  char pointer has
b8f80 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f   to be freed..*/
b8f90 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f  .static char *co
b8fa0 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
b8fb0 70 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69  p( const char *i
b8fc0 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74  n ){.  UniChar t
b8fd0 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41  empPath[CCHMAXPA
b8fe0 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74  TH];.  char *out
b8ff0 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f   = (char *)callo
b9000 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31  c( CCHMAXPATH, 1
b9010 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20   );..  if( !out 
b9020 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c  ).    return NUL
b9030 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66  L;..  if( !ucUtf
b9040 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20  8 || !uclCp ).  
b9050 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63    initUconvObjec
b9060 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65  ts();..  /* dete
b9070 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72  rmine string for
b9080 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
b9090 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69  of UTF-8 which i
b90a0 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66  s CP1208 */.  if
b90b0 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75  ( UniStrToUcs( u
b90c0 63 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c  cUtf8, tempPath,
b90d0 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48   (char *)in, CCH
b90e0 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53  MAXPATH ) != ULS
b90f0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72  _SUCCESS ).    r
b9100 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66  eturn out; /* if
b9110 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c   conversion fail
b9120 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d  s, return the em
b9130 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20  pty string */.. 
b9140 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66   /* conversion f
b9150 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70  or current codep
b9160 61 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65  age which can be
b9170 20 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20   used for paths 
b9180 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55  */.  UniStrFromU
b9190 63 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20  cs( uclCp, out, 
b91a0 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58  tempPath, CCHMAX
b91b0 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72  PATH );..  retur
b91c0 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n out;.}../*.** 
b91d0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
b91e0 74 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e  to convert filen
b91f0 61 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20  ames from local 
b9200 63 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d  codepage to UTF-
b9210 38 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74  8..** The two-st
b9220 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73  ep process: firs
b9230 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e  t convert the in
b9240 63 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d  coming codepage-
b9250 73 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69  specific.** stri
b9260 6e 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e  ng into UCS-2 an
b9270 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d  d then from UCS-
b9280 32 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67  2 to the codepag
b9290 65 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54  e of UTF-8..** T
b92a0 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72  he returned char
b92b0 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20   pointer has to 
b92c0 62 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20  be freed..**.** 
b92d0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
b92e0 20 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62   non-static to b
b92f0 65 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68  e able to use th
b9300 69 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e  is in shell.c an
b9310 64 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70  d.** similar app
b9320 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74  lications that t
b9330 61 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65  ake command line
b9340 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63   arguments..*/.c
b9350 68 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61  har *convertCpPa
b9360 74 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20  thToUtf8( const 
b9370 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e  char *in ){.  Un
b9380 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43  iChar tempPath[C
b9390 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68  CHMAXPATH];.  ch
b93a0 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20  ar *out = (char 
b93b0 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58  *)calloc( CCHMAX
b93c0 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66  PATH, 1 );..  if
b93d0 28 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74  ( !out ).    ret
b93e0 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28  urn NULL;..  if(
b93f0 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c   !ucUtf8 || !ucl
b9400 43 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f  Cp ).    initUco
b9410 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20  nvObjects();..  
b9420 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f  /* conversion fo
b9430 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  r current codepa
b9440 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  ge which can be 
b9450 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a  used for paths *
b9460 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f  /.  if( UniStrTo
b9470 55 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70  Ucs( uclCp, temp
b9480 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e  Path, (char *)in
b9490 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21  , CCHMAXPATH ) !
b94a0 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a  = ULS_SUCCESS ).
b94b0 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20      return out; 
b94c0 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e  /* if conversion
b94d0 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74   fails, return t
b94e0 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20  he empty string 
b94f0 2a 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69  */..  /* determi
b9500 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68  ne string for th
b9510 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20  e conversion of 
b9520 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43  UTF-8 which is C
b9530 50 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74  P1208 */.  UniSt
b9540 72 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38  rFromUcs( ucUtf8
b9550 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c  , out, tempPath,
b9560 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a   CCHMAXPATH );..
b9570 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a    return out;.}.
b9580 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74  ./*.** This vect
b9590 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74  or defines all t
b95a0 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  he methods that 
b95b0 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61  can operate on a
b95c0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c  n.** sqlite3_fil
b95d0 65 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74  e for os2..*/.st
b95e0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
b95f0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73  e3_io_methods os
b9600 32 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20  2IoMethod = {.  
b9610 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
b9620 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
b9630 72 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c  rsion */.  os2Cl
b9640 6f 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a  ose,.  os2Read,.
b9650 20 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73    os2Write,.  os
b9660 32 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32  2Truncate,.  os2
b9670 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53  Sync,.  os2FileS
b9680 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a  ize,.  os2Lock,.
b9690 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f    os2Unlock,.  o
b96a0 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  s2CheckReservedL
b96b0 6f 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f  ock,.  os2FileCo
b96c0 6e 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74  ntrol,.  os2Sect
b96d0 6f 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76  orSize,.  os2Dev
b96e0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
b96f0 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  cs.};../********
b9700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9740 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73  ***.** Here ends
b9750 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
b9760 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73   that form the s
b9770 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
b9780 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  s object..**.** 
b9790 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f  The next block o
b97a0 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74  f code implement
b97b0 73 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64  s the VFS method
b97c0 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s..*************
b97d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b97f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b9810 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
b9820 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
b9830 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a  name in zBuf.  z
b9840 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20  Buf must be big 
b9850 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c  enough to.** hol
b9860 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74  d at pVfs->mxPat
b9870 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73  hname characters
b9880 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
b9890 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20  getTempname(int 
b98a0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
b98b0 20 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   ){.  static con
b98c0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
b98d0 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20   zChars[] =.    
b98e0 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f  "abcdefghijklmno
b98f0 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20  pqrstuvwxyz".   
b9900 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e   "ABCDEFGHIJKLMN
b9910 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20  OPQRSTUVWXYZ".  
b9920 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a    "0123456789";.
b9930 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68    int i, j;.  ch
b9940 61 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b  ar zTempPathBuf[
b9950 33 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50  3];.  PSZ zTempP
b9960 61 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d  ath = (PSZ)&zTem
b9970 70 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20  pPathBuf;.  if( 
b9980 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
b9990 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54  ectory ){.    zT
b99a0 65 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65  empPath = sqlite
b99b0 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
b99c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
b99d0 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28  f( DosScanEnv( (
b99e0 50 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65  PSZ)"TEMP", &zTe
b99f0 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20  mpPath ) ){.    
b9a00 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76    if( DosScanEnv
b9a10 28 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a  ( (PSZ)"TMP", &z
b9a20 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20  TempPath ) ){.  
b9a30 20 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61        if( DosSca
b9a40 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44  nEnv( (PSZ)"TMPD
b9a50 49 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20  IR", &zTempPath 
b9a60 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
b9a70 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d  ULONG ulDriveNum
b9a80 20 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70   = 0, ulDriveMap
b9a90 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
b9aa0 20 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74   DosQueryCurrent
b9ab0 44 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75  Disk( &ulDriveNu
b9ac0 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29  m, &ulDriveMap )
b9ad0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72  ;.           spr
b9ae0 69 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65  intf( (char*)zTe
b9af0 6d 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28  mpPath, "%c:", (
b9b00 63 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44  char)( 'A' + ulD
b9b10 72 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b  riveNum - 1 ) );
b9b20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
b9b30 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f   }.    }.  }.  /
b9b40 2a 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72  * Strip off a tr
b9b50 61 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f  ailing slashes o
b9b60 72 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f  r backslashes, o
b9b70 74 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c  therwise we woul
b9b80 64 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c  d get *.   * mul
b9b90 74 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73  tiple (back)slas
b9ba0 68 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73  hes which causes
b9bb0 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61   DosOpen() to fa
b9bc0 69 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20  il.             
b9bd0 20 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67   *.   * Trailing
b9be0 20 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20   spaces are not 
b9bf0 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e  allowed, either.
b9c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b9c10 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
b9c20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   j = sqlite3Strl
b9c30 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b  en30(zTempPath);
b9c40 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20  .  while( j > 0 
b9c50 26 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a  && ( zTempPath[j
b9c60 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a  -1] == '\\' || z
b9c70 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d  TempPath[j-1] ==
b9c80 20 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20   '/'.           
b9c90 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d           || zTem
b9ca0 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20  pPath[j-1] == ' 
b9cb0 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a  ' ) ){.    j--;.
b9cc0 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b    }.  zTempPath[
b9cd0 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28  j] = '\0';.  if(
b9ce0 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64   !sqlite3_temp_d
b9cf0 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
b9d00 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55  char *zTempPathU
b9d10 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61  TF = convertCpPa
b9d20 74 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50  thToUtf8( zTempP
b9d30 61 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ath );.    sqlit
b9d40 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75  e3_snprintf( nBu
b9d50 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20  f-30, zBuf,.    
b9d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b9d70 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54    "%s\\"SQLITE_T
b9d80 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c  EMP_FILE_PREFIX,
b9d90 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b   zTempPathUTF );
b9da0 0a 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70  .    free( zTemp
b9db0 50 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c  PathUTF );.  }el
b9dc0 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
b9dd0 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33  snprintf( nBuf-3
b9de0 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20  0, zBuf,.       
b9df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
b9e00 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50  %s\\"SQLITE_TEMP
b9e10 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54  _FILE_PREFIX, zT
b9e20 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20  empPath );.  }. 
b9e30 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   j = sqlite3Strl
b9e40 65 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20  en30( zBuf );.  
b9e50 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
b9e60 73 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d  ss( 20, &zBuf[j]
b9e70 20 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30   );.  for( i = 0
b9e80 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a  ; i < 20; i++, j
b9e90 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a  ++ ){.    zBuf[j
b9ea0 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73  ] = (char)zChars
b9eb0 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  [ ((unsigned cha
b9ec0 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65  r)zBuf[j])%(size
b9ed0 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b  of(zChars)-1) ];
b9ee0 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d  .  }.  zBuf[j] =
b9ef0 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20   0;.  OSTRACE2( 
b9f00 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20  "TEMP FILENAME: 
b9f10 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20  %s\n", zBuf );. 
b9f20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
b9f30 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72  K;.}.../*.** Tur
b9f40 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74  n a relative pat
b9f50 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c  hname into a ful
b9f60 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69  l pathname.  Wri
b9f70 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70  te the full.** p
b9f80 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75  athname into zFu
b9f90 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77  ll[].  zFull[] w
b9fa0 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20  ill be at least 
b9fb0 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
b9fc0 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a  .** bytes in siz
b9fd0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
b9fe0 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65   os2FullPathname
b9ff0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
ba000 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20  *pVfs,          
ba010 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66  /* Pointer to vf
ba020 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f  s object */.  co
ba030 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74  nst char *zRelat
ba040 69 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73  ive,      /* Pos
ba050 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69  sibly relative i
ba060 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69  nput path */.  i
ba070 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20  nt nFull,       
ba080 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
ba090 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
ba0a0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
ba0b0 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20    char *zFull   
ba0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ba0d0 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a   Output buffer *
ba0e0 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65  /.){.  char *zRe
ba0f0 6c 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65  lativeCp = conve
ba100 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20  rtUtf8PathToCp( 
ba110 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63  zRelative );.  c
ba120 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d  har zFullCp[CCHM
ba130 41 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a  AXPATH] = "\0";.
ba140 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46    char *zFullUTF
ba150 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
ba160 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f  DosQueryPathInfo
ba170 28 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46  ( zRelativeCp, F
ba180 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45  IL_QUERYFULLNAME
ba190 2c 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20  , zFullCp,.     
ba1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ba1b0 20 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41             CCHMA
ba1c0 58 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28  XPATH );.  free(
ba1d0 20 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a   zRelativeCp );.
ba1e0 20 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e    zFullUTF = con
ba1f0 76 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38  vertCpPathToUtf8
ba200 28 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73  ( zFullCp );.  s
ba210 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
ba220 20 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a   nFull, zFull, z
ba230 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65  FullUTF );.  fre
ba240 65 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20  e( zFullUTF );. 
ba250 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
ba260 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
ba270 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
ba280 52 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  R;.}.../*.** Ope
ba290 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  n a file..*/.sta
ba2a0 74 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28  tic int os2Open(
ba2b0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
ba2c0 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
ba2d0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a   /* Not used */.
ba2e0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
ba2f0 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
ba300 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
ba310 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ile */.  sqlite3
ba320 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20  _file *id,      
ba330 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
ba340 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20  the SQLite file 
ba350 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20  handle here */. 
ba360 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20   int flags,     
ba370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ba380 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67  * Open mode flag
ba390 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74  s */.  int *pOut
ba3a0 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  Flags           
ba3b0 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72       /* Status r
ba3c0 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29  eturn flags */.)
ba3d0 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55  {.  HFILE h;.  U
ba3e0 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69  LONG ulFileAttri
ba3f0 62 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d  bute = FILE_NORM
ba400 41 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70  AL;.  ULONG ulOp
ba410 65 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55  enFlags = 0;.  U
ba420 4c 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  LONG ulOpenMode 
ba430 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  = 0;.  os2File *
ba440 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
ba450 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72  *)id;.  APIRET r
ba460 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
ba470 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a  ULONG ulAction;.
ba480 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b    char *zNameCp;
ba490 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65  .  char zTmpname
ba4a0 5b 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20  [CCHMAXPATH+1]; 
ba4b0 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20     /* Buffer to 
ba4c0 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d  hold name of tem
ba4d0 70 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20  p file */..  /* 
ba4e0 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
ba4f0 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
ba500 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c  unction is NULL,
ba510 20 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a   generate a .  *
ba520 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  * temporary file
ba530 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20   name to use .  
ba540 2a 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  */.  if( !zName 
ba550 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
ba560 67 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d  getTempname(CCHM
ba570 41 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61  AXPATH+1, zTmpna
ba580 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  me);.    if( rc!
ba590 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
ba5a0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
ba5b0 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d     }.    zName =
ba5c0 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a   zTmpname;.  }..
ba5d0 0a 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65  .  memset( pFile
ba5e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69  , 0, sizeof(*pFi
ba5f0 6c 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43  le) );..  OSTRAC
ba600 45 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25  E2( "OPEN want %
ba610 64 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a  d\n", flags );..
ba620 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51    if( flags & SQ
ba630 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
ba640 49 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65  ITE ){.    ulOpe
ba650 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43  nMode |= OPEN_AC
ba660 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a  CESS_READWRITE;.
ba670 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f      OSTRACE1( "O
ba680 50 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e  PEN read/write\n
ba690 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  " );.  }else{.  
ba6a0 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20    ulOpenMode |= 
ba6b0 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44  OPEN_ACCESS_READ
ba6c0 4f 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43  ONLY;.    OSTRAC
ba6d0 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f  E1( "OPEN read o
ba6e0 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20  nly\n" );.  }.. 
ba6f0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
ba700 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
ba710 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61  ){.    ulOpenFla
ba720 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f  gs |= OPEN_ACTIO
ba730 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53  N_OPEN_IF_EXISTS
ba740 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43   | OPEN_ACTION_C
ba750 52 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20  REATE_IF_NEW;.  
ba760 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
ba770 4e 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74  N open new/creat
ba780 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  e\n" );.  }else{
ba790 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73  .    ulOpenFlags
ba7a0 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f   |= OPEN_ACTION_
ba7b0 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c  OPEN_IF_EXISTS |
ba7c0 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49   OPEN_ACTION_FAI
ba7d0 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53  L_IF_NEW;.    OS
ba7e0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70  TRACE1( "OPEN op
ba7f0 65 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29  en existing\n" )
ba800 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61  ;.  }..  if( fla
ba810 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
ba820 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20  _MAIN_DB ){.    
ba830 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50  ulOpenMode |= OP
ba840 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e  EN_SHARE_DENYNON
ba850 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28  E;.    OSTRACE1(
ba860 20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61   "OPEN share rea
ba870 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20  d/write\n" );.  
ba880 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65  }else{.    ulOpe
ba890 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48  nMode |= OPEN_SH
ba8a0 41 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20  ARE_DENYWRITE;. 
ba8b0 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50     OSTRACE1( "OP
ba8c0 45 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e  EN share read on
ba8d0 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20  ly\n" );.  }..  
ba8e0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
ba8f0 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
ba900 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61  CLOSE ){.    cha
ba910 72 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41  r pathUtf8[CCHMA
ba920 58 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e  XPATH];.#ifdef N
ba930 44 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65  DEBUG /* when de
ba940 62 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20  bugging we want 
ba950 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  to make sure it 
ba960 69 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  is deleted */.  
ba970 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74    ulFileAttribut
ba980 65 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b  e = FILE_HIDDEN;
ba990 0a 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46  .#endif.    os2F
ba9a0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66  ullPathname( pVf
ba9b0 73 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58  s, zName, CCHMAX
ba9c0 50 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29  PATH, pathUtf8 )
ba9d0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74  ;.    pFile->pat
ba9e0 68 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74  hToDel = convert
ba9f0 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61  Utf8PathToCp( pa
baa00 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53  thUtf8 );.    OS
baa10 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69  TRACE1( "OPEN hi
baa20 64 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63  dden/delete on c
baa30 6c 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62  lose file attrib
baa40 75 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c  utes\n" );.  }el
baa50 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70  se{.    pFile->p
baa60 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
baa70 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
baa80 4f 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65  OPEN normal file
baa90 20 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b   attribute\n" );
baaa0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79  .  }..  /* alway
baab0 73 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d  s open in random
baac0 20 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72   access mode for
baad0 20 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72   possibly better
baae0 20 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70   speed */.  ulOp
baaf0 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46  enMode |= OPEN_F
bab00 4c 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75  LAGS_RANDOM;.  u
bab10 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45  lOpenMode |= OPE
bab20 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f  N_FLAGS_FAIL_ON_
bab30 45 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d  ERROR;.  ulOpenM
bab40 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47  ode |= OPEN_FLAG
bab50 53 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20  S_NOINHERIT;..  
bab60 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72  zNameCp = conver
bab70 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a  tUtf8PathToCp( z
bab80 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44  Name );.  rc = D
bab90 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61  osOpen( (PSZ)zNa
baba0 6d 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20  meCp,.          
babb0 20 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20        &h,.      
babc0 20 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74            &ulAct
babd0 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ion,.           
babe0 20 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20       0L,.       
babf0 20 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41           ulFileA
bac00 74 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20  ttribute,.      
bac10 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e            ulOpen
bac20 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20  Flags,.         
bac30 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64         ulOpenMod
bac40 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
bac50 20 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20     (PEAOP2)NULL 
bac60 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65  );.  free( zName
bac70 43 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21  Cp );.  if( rc !
bac80 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
bac90 20 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45    OSTRACE7( "OPE
baca0 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65  N Invalid handle
bacb0 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73   rc=%d: zName=%s
bacc0 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c  , ulAction=%#lx,
bacd0 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c   ulAttr=%#lx, ul
bace0 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f  Flags=%#lx, ulMo
bacf0 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20  de=%#lx\n",.    
bad00 20 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e            rc, zN
bad10 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75  ame, ulAction, u
bad20 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20  lFileAttribute, 
bad30 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f  ulOpenFlags, ulO
bad40 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69  penMode );.    i
bad50 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f  f( pFile->pathTo
bad60 44 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65  Del ).      free
bad70 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
bad80 65 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  el );.    pFile-
bad90 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c  >pathToDel = NUL
bada0 4c 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  L;.    if( flags
badb0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
badc0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20  EADWRITE ){.    
badd0 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45    OSTRACE2( "OPE
bade0 4e 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e  N %d Invalid han
badf0 64 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20  dle\n", ((flags 
bae00 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  | SQLITE_OPEN_RE
bae10 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54  ADONLY) & ~SQLIT
bae20 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
bae30 29 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ) );.      retur
bae40 6e 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c  n os2Open( pVfs,
bae50 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20   zName, id,.    
bae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bae70 20 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49    ((flags | SQLI
bae80 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
bae90 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e  ) & ~SQLITE_OPEN
baea0 5f 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20  _READWRITE),.   
baeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
baec0 20 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a     pOutFlags );.
baed0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
baee0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
baef0 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20  ANTOPEN;.    }. 
baf00 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c   }..  if( pOutFl
baf10 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74  ags ){.    *pOut
baf20 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20  Flags = flags & 
baf30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
baf40 57 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f  WRITE ? SQLITE_O
baf50 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20  PEN_READWRITE : 
baf60 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
baf70 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69  ONLY;.  }..  pFi
baf80 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f  le->pMethod = &o
baf90 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46  s2IoMethod;.  pF
bafa0 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70  ile->h = h;.  Op
bafb0 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20  enCounter(+1);. 
bafc0 20 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e   OSTRACE3( "OPEN
bafd0 20 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64   %d pOutFlags=%d
bafe0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
baff0 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65  OutFlags );.  re
bb000 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
bb010 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
bb020 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a  the named file..
bb030 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
bb040 32 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74  2Delete(.  sqlit
bb050 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
bb060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb070 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e    /* Not used on
bb080 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   os2 */.  const 
bb090 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
bb0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb0b0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65   /* Name of file
bb0c0 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
bb0d0 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20  int syncDir     
bb0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb0f0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
bb100 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a  ed on os2 */.){.
bb110 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
bb120 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a  _ERROR;.  char *
bb130 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f  zFilenameCp = co
bb140 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
bb150 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a  p( zFilename );.
bb160 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
bb170 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
bb180 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b  _IOERR_DELETE );
bb190 0a 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74  .  rc = DosDelet
bb1a0 65 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d  e( (PSZ)zFilenam
bb1b0 65 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a  eCp );.  free( z
bb1c0 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20  FilenameCp );.  
bb1d0 4f 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54  OSTRACE2( "DELET
bb1e0 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69  E \"%s\"\n", zFi
bb1f0 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75  lename );.  retu
bb200 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  rn rc == NO_ERRO
bb210 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  R ? SQLITE_OK : 
bb220 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c  SQLITE_IOERR_DEL
bb230 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  ETE;.}../*.** Ch
bb240 65 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63  eck the existanc
bb250 65 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20  e and status of 
bb260 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
bb270 63 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28  c int os2Access(
bb280 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
bb290 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20  pVfs,        /* 
bb2a0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20  Not used on os2 
bb2b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
bb2c0 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f  *zFilename,    /
bb2d0 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
bb2e0 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74  o check */.  int
bb2f0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
bb300 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f         /* Type o
bb310 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f  f test to make o
bb320 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20  n this file */. 
bb330 20 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20   int *pOut      
bb340 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
bb350 69 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65  ite results here
bb360 20 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41   */.){.  FILESTA
bb370 54 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67  TUS3 fsts3Config
bb380 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72  Info;.  APIRET r
bb390 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
bb3a0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43  char *zFilenameC
bb3b0 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  p = convertUtf8P
bb3c0 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61  athToCp( zFilena
bb3d0 6d 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  me );..  memset(
bb3e0 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66   &fsts3ConfigInf
bb3f0 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74  o, 0, sizeof(fst
bb400 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b  s3ConfigInfo) );
bb410 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79  .  rc = DosQuery
bb420 50 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a  PathInfo( (PSZ)z
bb430 46 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f  FilenameCp, FIL_
bb440 53 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20  STANDARD,.      
bb450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb460 20 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49     &fsts3ConfigI
bb470 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45  nfo, sizeof(FILE
bb480 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72  STATUS3) );.  fr
bb490 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20  ee( zFilenameCp 
bb4a0 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22  );.  OSTRACE4( "
bb4b0 41 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66  ACCESS fsts3Conf
bb4c0 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d  igInfo.attrFile=
bb4d0 25 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25  %d flags=%d rc=%
bb4e0 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
bb4f0 20 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66    fsts3ConfigInf
bb500 6f 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67  o.attrFile, flag
bb510 73 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63  s, rc );.  switc
bb520 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20  h( flags ){.    
bb530 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
bb540 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73  SS_READ:.    cas
bb550 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
bb560 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63  EXISTS:.      rc
bb570 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52   = (rc == NO_ERR
bb580 4f 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41  OR);.      OSTRA
bb590 43 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20  CE3( "ACCESS %s 
bb5a0 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61  access of read a
bb5b0 6e 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64  nd exists  rc=%d
bb5c0 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  \n", zFilename, 
bb5d0 72 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  rc );.      brea
bb5e0 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
bb5f0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
bb600 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20  ITE:.      rc = 
bb610 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29  (rc == NO_ERROR)
bb620 20 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66   && ( (fsts3Conf
bb630 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20  igInfo.attrFile 
bb640 26 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29  & FILE_READONLY)
bb650 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f   == 0 );.      O
bb660 53 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53  STRACE3( "ACCESS
bb670 20 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65   %s access of re
bb680 61 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c  ad/write  rc=%d\
bb690 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72  n", zFilename, r
bb6a0 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  c );.      break
bb6b0 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20  ;.    default:. 
bb6c0 20 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49       assert( !"I
bb6d0 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67  nvalid flags arg
bb6e0 75 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20  ument" );.  }.  
bb6f0 2a 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65  *pOut = rc;.  re
bb700 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
bb710 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }...#ifndef SQLI
bb720 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
bb730 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74  ENSION./*.** Int
bb740 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
bb750 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
bb760 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
bb770 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69  try points.** wi
bb780 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  thin the shared 
bb790 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f  library, and clo
bb7a0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  sing the shared 
bb7b0 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a  library..*/./*.*
bb7c0 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72  * Interfaces for
bb7d0 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65   opening a share
bb7e0 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69  d library, findi
bb7f0 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a  ng entry points.
bb800 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68  ** within the sh
bb810 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e  ared library, an
bb820 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68  d closing the sh
bb830 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  ared library..*/
bb840 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73  .static void *os
bb850 32 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  2DlOpen(sqlite3_
bb860 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
bb870 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
bb880 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45  ){.  UCHAR loadE
bb890 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55  rr[256];.  HMODU
bb8a0 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45  LE hmod;.  APIRE
bb8b0 54 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46  T rc;.  char *zF
bb8c0 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76  ilenameCp = conv
bb8d0 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
bb8e0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63  zFilename);.  rc
bb8f0 20 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65   = DosLoadModule
bb900 28 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73  ((PSZ)loadErr, s
bb910 69 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20  izeof(loadErr), 
bb920 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d  zFilenameCp, &hm
bb930 6f 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c  od);.  free(zFil
bb940 65 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75  enameCp);.  retu
bb950 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f  rn rc != NO_ERRO
bb960 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68  R ? 0 : (void*)h
bb970 6d 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e  mod;.}./*.** A n
bb980 6f 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65  o-op since the e
bb990 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
bb9a0 75 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73  urned on the Dos
bb9b0 4c 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e  LoadModule call.
bb9c0 0a 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65  .** os2Dlopen re
bb9d0 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f  turns zero if Do
bb9e0 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e  sLoadModule is n
bb9f0 6f 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a  ot successful..*
bba00 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  /.static void os
bba10 32 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33  2DlError(sqlite3
bba20 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
bba30 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
bba40 4f 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a  Out){./* no-op *
bba50 2f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  /.}.static void 
bba60 2a 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65  *os2DlSym(sqlite
bba70 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
bba80 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73  d *pHandle, cons
bba90 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29  t char *zSymbol)
bbaa0 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41  {.  PFN pfn;.  A
bbab0 50 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d  PIRET rc;.  rc =
bbac0 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64   DosQueryProcAdd
bbad0 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64  r((HMODULE)pHand
bbae0 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c  le, 0L, zSymbol,
bbaf0 20 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63   &pfn);.  if( rc
bbb00 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
bbb10 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79      /* if the sy
bbb20 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20  mbol itself was 
bbb30 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63  not found, searc
bbb40 68 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20  h again for the 
bbb50 73 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62  same.     * symb
bbb60 6f 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61  ol with an extra
bbb70 20 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61   underscore, tha
bbb80 74 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65  t might be neede
bbb90 64 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20  d depending.    
bbba0 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e   * on the callin
bbbb0 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a  g convention */.
bbbc0 20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f      char _zSymbo
bbbd0 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20  l[256] = "_";.  
bbbe0 20 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62    strncat(_zSymb
bbbf0 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35  ol, zSymbol, 255
bbc00 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51  );.    rc = DosQ
bbc10 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d  ueryProcAddr((HM
bbc20 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30  ODULE)pHandle, 0
bbc30 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66  L, _zSymbol, &pf
bbc40 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
bbc50 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc != NO_ERROR 
bbc60 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e  ? 0 : (void*)pfn
bbc70 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
bbc80 6f 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74  os2DlClose(sqlit
bbc90 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f  e3_vfs *pVfs, vo
bbca0 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20  id *pHandle){.  
bbcb0 44 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48  DosFreeModule((H
bbcc0 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b  MODULE)pHandle);
bbcd0 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53  .}.#else /* if S
bbce0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
bbcf0 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66  EXTENSION is def
bbd00 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69  ined: */.  #defi
bbd10 6e 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20  ne os2DlOpen 0. 
bbd20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72   #define os2DlEr
bbd30 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20  ror 0.  #define 
bbd40 6f 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65  os2DlSym 0.  #de
bbd50 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20  fine os2DlClose 
bbd60 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
bbd70 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75   Write up to nBu
bbd80 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f  f bytes of rando
bbd90 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e  mness into zBuf.
bbda0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
bbdb0 73 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  s2Randomness(sql
bbdc0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
bbdd0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
bbde0 7a 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20  zBuf ){.  int n 
bbdf0 3d 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  = 0;.#if defined
bbe00 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20  (SQLITE_TEST).  
bbe10 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73  n = nBuf;.  mems
bbe20 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66  et(zBuf, 0, nBuf
bbe30 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73  );.#else.  int s
bbe40 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a  izeofULong = siz
bbe50 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66  eof(ULONG);.  if
bbe60 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41  ( (int)sizeof(DA
bbe70 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20  TETIME) <= nBuf 
bbe80 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54  - n ){.    DATET
bbe90 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65  IME x;.    DosGe
bbea0 74 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20  tDateTime(&x);. 
bbeb0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
bbec0 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78  n], &x, sizeof(x
bbed0 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ));.    n += siz
bbee0 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69  eof(x);.  }..  i
bbef0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c  f( sizeofULong <
bbf00 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20  = nBuf - n ){.  
bbf10 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20    PPIB ppib;.   
bbf20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
bbf30 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a  s(NULL, &ppib);.
bbf40 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
bbf50 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f  [n], &ppib->pib_
bbf60 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f  ulpid, sizeofULo
bbf70 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69  ng);.    n += si
bbf80 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a  zeofULong;.  }..
bbf90 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
bbfa0 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
bbfb0 0a 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a  .    PTIB ptib;.
bbfc0 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
bbfd0 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c  ocks(&ptib, NULL
bbfe0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
bbff0 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74  Buf[n], &ptib->t
bc000 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75  ib_ptib2->tib2_u
bc010 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  ltid, sizeofULon
bc020 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  g);.    n += siz
bc030 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20  eofULong;.  }.. 
bc040 20 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20   /* if we still 
bc050 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74  haven't filled t
bc060 68 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68  he buffer yet th
bc070 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c  e following will
bc080 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76   */.  /* grab ev
bc090 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e  erything once in
bc0a0 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20  stead of making 
bc0b0 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f  several calls fo
bc0c0 72 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20  r a single item 
bc0d0 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55  */.  if( sizeofU
bc0e0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
bc0f0 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c   ){.    ULONG ul
bc100 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d  SysInfo[QSV_MAX]
bc110 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79  ;.    DosQuerySy
bc120 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41  sInfo(1L, QSV_MA
bc130 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69  X, ulSysInfo, si
bc140 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f  zeofULong * QSV_
bc150 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  MAX);..    memcp
bc160 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53  y(&zBuf[n], &ulS
bc170 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f  ysInfo[QSV_MS_CO
bc180 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66  UNT - 1], sizeof
bc190 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d  ULong);.    n +=
bc1a0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20   sizeofULong;.. 
bc1b0 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f     if( sizeofULo
bc1c0 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  ng <= nBuf - n )
bc1d0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  {.      memcpy(&
bc1e0 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49  zBuf[n], &ulSysI
bc1f0 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e  nfo[QSV_TIMER_IN
bc200 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a  TERVAL - 1], siz
bc210 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20  eofULong);.     
bc220 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
bc230 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  g;.    }.    if(
bc240 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
bc250 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
bc260 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
bc270 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53  ], &ulSysInfo[QS
bc280 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c  V_TIME_LOW - 1],
bc290 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20   sizeofULong);. 
bc2a0 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66       n += sizeof
bc2b0 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20  ULong;.    }.   
bc2c0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
bc2d0 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
bc2e0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42        memcpy(&zB
bc2f0 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66  uf[n], &ulSysInf
bc300 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20  o[QSV_TIME_HIGH 
bc310 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  - 1], sizeofULon
bc320 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73  g);.      n += s
bc330 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20  izeofULong;.    
bc340 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66  }.    if( sizeof
bc350 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20  ULong <= nBuf - 
bc360 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  n ){.      memcp
bc370 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53  y(&zBuf[n], &ulS
bc380 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56  ysInfo[QSV_TOTAV
bc390 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a  AILMEM - 1], siz
bc3a0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20  eofULong);.     
bc3b0 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
bc3c0 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  g;.    }.  }.#en
bc3d0 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b  dif..  return n;
bc3e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20  .}../*.** Sleep 
bc3f0 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69  for a little whi
bc400 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  le.  Return the 
bc410 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73  amount of time s
bc420 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67  lept..** The arg
bc430 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d  ument is the num
bc440 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
bc450 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73  nds we want to s
bc460 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74  leep..** The ret
bc470 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
bc480 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f   number of micro
bc490 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
bc4a0 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71   actually.** req
bc4b0 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20  uested from the 
bc4c0 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61  underlying opera
bc4d0 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e  ting system, a n
bc4e0 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d  umber which.** m
bc4f0 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20  ight be greater 
bc500 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
bc510 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62   the argument, b
bc520 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74  ut not less.** t
bc530 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74  han the argument
bc540 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
bc550 6f 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65  os2Sleep( sqlite
bc560 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
bc570 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44   microsec ){.  D
bc580 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73  osSleep( (micros
bc590 65 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65  ec/1000) );.  re
bc5a0 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d  turn microsec;.}
bc5b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
bc5c0 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20  owing variable, 
bc5d0 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d  if set to a non-
bc5e0 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f  zero value, beco
bc5f0 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  mes the result.*
bc600 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  * returned from 
bc610 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
bc620 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73  Time().  This is
bc630 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
bc640 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  g..*/.#ifdef SQL
bc650 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
bc660 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
bc670 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30  current_time = 0
bc680 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
bc690 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74  Find the current
bc6a0 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72   time (in Univer
bc6b0 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20  sal Coordinated 
bc6c0 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68  Time).  Write th
bc6d0 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d  e.** current tim
bc6e0 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20  e and date as a 
bc6f0 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65  Julian Day numbe
bc700 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e  r into *prNow an
bc710 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20  d.** return 0.  
bc720 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
bc730 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61  time and date ca
bc740 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a  nnot be found..*
bc750 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74  /.int os2Current
bc760 54 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66  Time( sqlite3_vf
bc770 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20  s *pVfs, double 
bc780 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62  *prNow ){.  doub
bc790 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20  le now;.  SHORT 
bc7a0 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73  minute; /* needs
bc7b0 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63   to be able to c
bc7c0 6f 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76  ope with negativ
bc7d0 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65  e timezone offse
bc7e0 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65  t */.  USHORT se
bc7f0 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20  cond, hour,.    
bc800 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c       day, month,
bc810 20 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d   year;.  DATETIM
bc820 45 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61  E dt;.  DosGetDa
bc830 74 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20  teTime( &dt );. 
bc840 20 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52   second = (USHOR
bc850 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20  T)dt.seconds;.  
bc860 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29  minute = (SHORT)
bc870 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e  dt.minutes + dt.
bc880 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72  timezone;.  hour
bc890 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f   = (USHORT)dt.ho
bc8a0 75 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53  urs;.  day = (US
bc8b0 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d  HORT)dt.day;.  m
bc8c0 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64  onth = (USHORT)d
bc8d0 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20  t.month;.  year 
bc8e0 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61  = (USHORT)dt.yea
bc8f0 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61  r;..  /* Calcula
bc900 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a  tions from http:
bc910 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c  //www.astro.keel
bc920 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74  e.ac.uk/~rno/Ast
bc930 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a  ronomy/hjd.html.
bc940 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e       http://www.
bc950 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75  astro.keele.ac.u
bc960 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79  k/~rno/Astronomy
bc970 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20  /hjd-0.1.c */.  
bc980 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65  /* Calculate the
bc990 20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a   Julian days */.
bc9a0 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32    now = day - 32
bc9b0 30 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28  076 +.    1461*(
bc9c0 79 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d  year + 4800 + (m
bc9d0 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34  onth - 14)/12)/4
bc9e0 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74   +.    367*(mont
bc9f0 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d  h - 2 - (month -
bca00 20 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d   14)/12*12)/12 -
bca10 0a 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20  .    3*((year + 
bca20 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20  4900 + (month - 
bca30 31 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a  14)/12)/100)/4;.
bca40 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72  .  /* Add the fr
bca50 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20  actional hours, 
bca60 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73  mins and seconds
bca70 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f   */.  now += (ho
bca80 75 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b  ur + 12.0)/24.0;
bca90 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65  .  now += minute
bcaa0 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b  /1440.0;.  now +
bcab0 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30  = second/86400.0
bcac0 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77  ;.  *prNow = now
bcad0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
bcae0 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74  TEST.  if( sqlit
bcaf0 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
bcb00 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20  ){.    *prNow = 
bcb10 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
bcb20 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32  time/86400.0 + 2
bcb30 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65  440587.5;.  }.#e
bcb40 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b  ndif.  return 0;
bcb50 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .}..static int o
bcb60 73 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73  s2GetLastError(s
bcb70 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
bcb80 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
bcb90 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72   *zBuf){.  retur
bcba0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 0;.}../*.** In
bcbb0 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69  itialize and dei
bcbc0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70  nitialize the op
bcbd0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
bcbe0 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c  nterface..*/.SQL
bcbf0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
bcc00 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64  te3_os_init(void
bcc10 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
bcc20 74 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d  te3_vfs os2Vfs =
bcc30 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   {.    1,       
bcc40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
bcc50 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a  rsion */.    siz
bcc60 65 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20  eof(os2File),   
bcc70 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20  /* szOsFile */. 
bcc80 20 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20     CCHMAXPATH,  
bcc90 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e        /* mxPathn
bcca0 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
bccb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bccc0 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f   pNext */.    "o
bccd0 73 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  s2",            
bcce0 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20   /* zName */.   
bccf0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
bcd00 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20      /* pAppData 
bcd10 2a 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c  */..    os2Open,
bcd20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
bcd30 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65  pen */.    os2De
bcd40 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  lete,         /*
bcd50 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20   xDelete */.    
bcd60 6f 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20  os2Access,      
bcd70 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f     /* xAccess */
bcd80 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68  .    os2FullPath
bcd90 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c  name,   /* xFull
bcda0 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  Pathname */.    
bcdb0 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20  os2DlOpen,      
bcdc0 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f     /* xDlOpen */
bcdd0 0a 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c  .    os2DlError,
bcde0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72          /* xDlEr
bcdf0 72 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c  ror */.    os2Dl
bce00 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Sym,          /*
bce10 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f   xDlSym */.    o
bce20 73 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20  s2DlClose,      
bce30 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f    /* xDlClose */
bce40 0a 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65  .    os2Randomne
bce50 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64  ss,     /* xRand
bce60 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73  omness */.    os
bce70 32 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20  2Sleep,         
bce80 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20   /* xSleep */.  
bce90 20 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65    os2CurrentTime
bcea0 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74  ,    /* xCurrent
bceb0 54 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47  Time */.    os2G
bcec0 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f  etLastError    /
bced0 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20  * xGetLastError 
bcee0 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65  */.  };.  sqlite
bcef0 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26  3_vfs_register(&
bcf00 6f 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e  os2Vfs, 1);.  in
bcf10 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29  itUconvObjects()
bcf20 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
bcf30 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  E_OK;.}.SQLITE_A
bcf40 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
bcf50 73 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66  s_end(void){.  f
bcf60 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28  reeUconvObjects(
bcf70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
bcf80 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66  TE_OK;.}..#endif
bcf90 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53   /* SQLITE_OS_OS
bcfa0 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  2 */../*********
bcfb0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
bcfc0 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os2.c **********
bcfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcff0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
bd000 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
bd010 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a   os_unix.c *****
bd020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd040 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
bd050 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68   May 22.**.** Th
bd060 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
bd070 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
bd080 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
bd090 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
bd0a0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
bd0b0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
bd0c0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
bd0d0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
bd0e0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
bd0f0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
bd100 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
bd110 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
bd120 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
bd130 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
bd140 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
bd150 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
bd160 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
bd170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
bd1c0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
bd1d0 61 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70  ains the VFS imp
bd1e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  lementation for 
bd1f0 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74  unix-like operat
bd200 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69  ing systems.** i
bd210 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61  nclude Linux, Ma
bd220 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c  cOSX, *BSD, QNX,
bd230 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48   VxWorks, AIX, H
bd240 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e  PUX, and others.
bd250 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
bd260 20 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61   actually severa
bd270 6c 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20  l different VFS 
bd280 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
bd290 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a  in this file..**
bd2a0 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73   The differences
bd2b0 20 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20   are in the way 
bd2c0 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e  that file lockin
bd2d0 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20  g is done.  The 
bd2e0 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65  default.** imple
bd2f0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50  mentation uses P
bd300 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f  osix Advisory Lo
bd310 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76  cks.  Alternativ
bd320 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
bd330 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29  s.** use flock()
bd340 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72  , dot-files, var
bd350 69 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79  ious proprietary
bd360 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73   locking schemas
bd370 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73  , or simply.** s
bd380 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20  kip locking all 
bd390 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20  together..**.** 
bd3a0 54 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65  This source file
bd3b0 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e   is organized in
bd3c0 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65  to divisions whe
bd3d0 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72  re the logic for
bd3e0 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66   various.** subf
bd3f0 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74  unctions is cont
bd400 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65  ained within the
bd410 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76   appropriate div
bd420 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a  ision.  PLEASE.*
bd430 2a 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43  * KEEP THE STRUC
bd440 54 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c  TURE OF THIS FIL
bd450 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63  E INTACT.  New c
bd460 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c  ode should be pl
bd470 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63  aced.** in the c
bd480 6f 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20  orrect division 
bd490 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c  and should be cl
bd4a0 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a  early labeled..*
bd4b0 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20  *.** The layout 
bd4c0 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20  of divisions is 
bd4d0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
bd4e0 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70  *   *  General-p
bd4f0 75 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69  urpose declarati
bd500 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20  ons and utility 
bd510 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20  functions..**   
bd520 2a 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49  *  Unique file I
bd530 44 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20  D logic used by 
bd540 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20  VxWorks..**   * 
bd550 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67   Various locking
bd560 20 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65   primitive imple
bd570 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20  mentations (all 
bd580 65 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63  except proxy loc
bd590 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b  king):.**      +
bd5a0 20 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73   for Posix Advis
bd5b0 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20  ory Locks.**    
bd5c0 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f    + for no-op lo
bd5d0 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f  cks.**      + fo
bd5e0 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73  r dot-file locks
bd5f0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66  .**      + for f
bd600 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a  lock() locking.*
bd610 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d  *      + for nam
bd620 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ed semaphore loc
bd630 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79  ks (VxWorks only
bd640 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  ).**      + for 
bd650 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c  AFP filesystem l
bd660 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c  ocks (MacOSX onl
bd670 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74  y).**   *  sqlit
bd680 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20  e3_file methods 
bd690 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77  not associated w
bd6a0 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20  ith locking..** 
bd6b0 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73    *  Definitions
bd6c0 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   of sqlite3_io_m
bd6d0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66  ethods objects f
bd6e0 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a  or all locking.*
bd6f0 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70  *      methods p
bd700 6c 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e  lus "finder" fun
bd710 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20  ctions for each 
bd720 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a  locking method..
bd730 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f  **   *  sqlite3_
bd740 76 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65  vfs method imple
bd750 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20  mentations..**  
bd760 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d   *  Locking prim
bd770 69 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70  itives for the p
bd780 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e  roxy uber-lockin
bd790 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53  g-method. (MacOS
bd7a0 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20  X only).**   *  
bd7b0 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73  Definitions of s
bd7c0 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
bd7d0 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69  ts for all locki
bd7e0 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20  ng methods.**   
bd7f0 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e     plus implemen
bd800 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74  tations of sqlit
bd810 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64  e3_os_init() and
bd820 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
bd830 29 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73  )..**.** $Id: os
bd840 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 32 34 38 20  _unix.c,v 1.248 
bd850 32 30 30 39 2f 30 33 2f 33 30 20 30 37 3a 33 39  2009/03/30 07:39
bd860 3a 33 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :35 danielk1977 
bd870 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20 53 51 4c  Exp $.*/.#if SQL
bd880 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20  ITE_OS_UNIX     
bd890 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
bd8a0 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e   file is used on
bd8b0 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f   unix only */../
bd8c0 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 76  *.** There are v
bd8d0 61 72 69 6f 75 73 20 6d 65 74 68 6f 64 73 20 66  arious methods f
bd8e0 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  or file locking 
bd8f0 75 73 65 64 20 66 6f 72 20 63 6f 6e 63 75 72 72  used for concurr
bd900 65 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a  ency.** control:
bd910 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49  .**.**   1. POSI
bd920 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68 65 20 64  X locking (the d
bd930 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e  efault),.**   2.
bd940 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20   No locking,.** 
bd950 20 20 33 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f    3. Dot-file lo
bd960 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66  cking,.**   4. f
bd970 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a  lock() locking,.
bd980 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b  **   5. AFP lock
bd990 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a  ing (OSX only),.
bd9a0 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f  **   6. Named PO
bd9b0 53 49 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28  SIX semaphores (
bd9c0 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a  VXWorks only),.*
bd9d0 2a 20 20 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63  *   7. proxy loc
bd9e0 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29  king. (OSX only)
bd9f0 0a 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c  .**.** Styles 4,
bda00 20 35 2c 20 61 6e 64 20 37 20 61 72 65 20 6f 6e   5, and 7 are on
bda10 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20  ly available of 
bda20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
bda30 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69  CKING_STYLE.** i
bda40 73 20 64 65 66 69 6e 65 64 20 74 6f 20 31 2e 20  s defined to 1. 
bda50 20 54 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42   The SQLITE_ENAB
bda60 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
bda70 20 61 6c 73 6f 20 65 6e 61 62 6c 65 73 20 61 75   also enables au
bda80 74 6f 6d 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63  tomatic.** selec
bda90 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 70 70 72  tion of the appr
bdaa0 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20  opriate locking 
bdab0 73 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 74  style based on t
bdac0 68 65 20 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a  he filesystem.**
bdad0 20 77 68 65 72 65 20 74 68 65 20 64 61 74 61 62   where the datab
bdae0 61 73 65 20 69 73 20 6c 6f 63 61 74 65 64 2e 20  ase is located. 
bdaf0 20 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65   .*/.#if !define
bdb00 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
bdb10 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23  LOCKING_STYLE).#
bdb20 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41    if defined(__A
bdb30 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66  PPLE__).#    def
bdb40 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ine SQLITE_ENABL
bdb50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
bdb60 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64  1.#  else.#    d
bdb70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41  efine SQLITE_ENA
bdb80 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
bdb90 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e  E 0.#  endif.#en
bdba0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e  dif../*.** Defin
bdbb0 65 20 74 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53  e the OS_VXWORKS
bdbc0 20 70 72 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d   pre-processor m
bdbd0 61 63 72 6f 20 74 6f 20 31 20 69 66 20 62 75 69  acro to 1 if bui
bdbe0 6c 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77  lding on .** vxw
bdbf0 6f 72 6b 73 2c 20 6f 72 20 30 20 6f 74 68 65 72  orks, or 0 other
bdc00 77 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  wise..*/.#ifndef
bdc10 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69   OS_VXWORKS.#  i
bdc20 66 20 64 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f  f defined(__RTP_
bdc30 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57  _) || defined(_W
bdc40 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20  RS_KERNEL).#    
bdc50 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b  define OS_VXWORK
bdc60 53 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20  S 1.#  else.#   
bdc70 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52   define OS_VXWOR
bdc80 4b 53 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65  KS 0.#  endif.#e
bdc90 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  ndif../*.** Thes
bdca0 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c  e #defines shoul
bdcb0 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69  d enable >2GB fi
bdcc0 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f  le support on Po
bdcd0 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e  six if the.** un
bdce0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
bdcf0 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  ng system suppor
bdd00 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f  ts it.  If the O
bdd10 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65  S lacks.** large
bdd20 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 74   file support, t
bdd30 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e  hese should be n
bdd40 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72  o-ops..**.** Lar
bdd50 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20  ge file support 
bdd60 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20  can be disabled 
bdd70 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49  using the -DSQLI
bdd80 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73  TE_DISABLE_LFS s
bdd90 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20  witch.** on the 
bdda0 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64  compiler command
bddb0 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20   line.  This is 
bddc0 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75  necessary if you
bddd0 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a   are compiling.*
bdde0 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61  * on a recent ma
bddf0 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61  chine (ex: RedHa
bde00 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77  t 7.2) but you w
bde10 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f  ant your code to
bde20 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f   work.** on an o
bde30 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78  lder machine (ex
bde40 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20  : RedHat 6.0).  
bde50 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f  If you compile o
bde60 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20  n RedHat 7.2.** 
bde70 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74  without this opt
bde80 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62  ion, LFS is enab
bde90 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65  le.  But LFS doe
bdea0 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74  s not exist in t
bdeb0 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20  he kernel.** in 
bdec0 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74  RedHat 6.0, so t
bded0 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f  he code won't wo
bdee0 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20  rk.  Hence, for 
bdef0 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a  maximum binary.*
bdf00 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f  * portability yo
bdf10 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46  u should omit LF
bdf20 53 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65  S..**.** The pre
bdf30 76 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 20  vious paragraph 
bdf40 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 20 32  was written in 2
bdf50 30 30 35 2e 20 20 28 54 68 69 73 20 70 61 72 61  005.  (This para
bdf60 67 72 61 70 68 20 69 73 20 77 72 69 74 74 65 6e  graph is written
bdf70 0a 2a 2a 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32  .** on 2008-11-2
bdf80 38 2e 29 20 54 68 65 73 65 20 64 61 79 73 2c 20  8.) These days, 
bdf90 61 6c 6c 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c  all Linux kernel
bdfa0 73 20 73 75 70 70 6f 72 74 20 6c 61 72 67 65 20  s support large 
bdfb0 66 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75  files, so.** you
bdfc0 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79   should probably
bdfd0 20 6c 65 61 76 65 20 4c 46 53 20 65 6e 61 62 6c   leave LFS enabl
bdfe0 65 64 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 6d  ed.  But some em
bdff0 62 65 64 64 65 64 20 70 6c 61 74 66 6f 72 6d 73  bedded platforms
be000 20 6d 69 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c   might.** lack L
be010 46 53 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  FS in which case
be020 20 74 68 65 20 53 51 4c 49 54 45 5f 44 49 53 41   the SQLITE_DISA
be030 42 4c 45 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69  BLE_LFS macro mi
be040 67 68 74 20 73 74 69 6c 6c 20 62 65 20 75 73 65  ght still be use
be050 66 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ful..*/.#ifndef 
be060 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c  SQLITE_DISABLE_L
be070 46 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52  FS.# define _LAR
be080 47 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a  GE_FILE       1.
be090 23 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f  # ifndef _FILE_O
be0a0 46 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64  FFSET_BITS.#   d
be0b0 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53  efine _FILE_OFFS
be0c0 45 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64  ET_BITS 64.# end
be0d0 69 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52  if.# define _LAR
be0e0 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a  GEFILE_SOURCE 1.
be0f0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74  #endif../*.** st
be100 61 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66  andard include f
be110 69 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64  iles..*/.#includ
be120 65 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a  e <sys/types.h>.
be130 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74  #include <sys/st
be140 61 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  at.h>.#include <
be150 66 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64  fcntl.h>.#includ
be160 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e  e <unistd.h>.#in
be170 63 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e  clude <sys/time.
be180 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72  h>.#include <err
be190 6e 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54  no.h>..#if SQLIT
be1a0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
be1b0 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65  _STYLE.# include
be1c0 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23   <sys/ioctl.h>.#
be1d0 20 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23   if OS_VXWORKS.#
be1e0 20 20 69 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70    include <semap
be1f0 68 6f 72 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75  hore.h>.#  inclu
be200 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20  de <limits.h>.# 
be210 65 6c 73 65 0a 23 20 20 69 6e 63 6c 75 64 65 20  else.#  include 
be220 3c 73 79 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20  <sys/file.h>.#  
be230 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 61 72  include <sys/par
be240 61 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65  am.h>.#  include
be250 20 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23   <sys/mount.h>.#
be260 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a   endif.#endif /*
be270 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
be280 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
be290 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65  ./*.** If we are
be2a0 20 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73 61   to be thread-sa
be2b0 66 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65 20  fe, include the 
be2c0 70 74 68 72 65 61 64 73 20 68 65 61 64 65 72 20  pthreads header 
be2d0 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68  and define.** th
be2e0 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48  e SQLITE_UNIX_TH
be2f0 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a  READS macro..*/.
be300 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
be310 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53  DSAFE.# define S
be320 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41  QLITE_UNIX_THREA
be330 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  DS 1.#endif../*.
be340 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69  ** Default permi
be350 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61  ssions when crea
be360 74 69 6e 67 20 61 20 6e 65 77 20 66 69 6c 65 0a  ting a new file.
be370 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
be380 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50  E_DEFAULT_FILE_P
be390 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66  ERMISSIONS.# def
be3a0 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
be3b0 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49  LT_FILE_PERMISSI
be3c0 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64 69 66 0a  ONS 0644.#endif.
be3d0 0a 2f 2a 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20  ./*. ** Default 
be3e0 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e  permissions when
be3f0 20 63 72 65 61 74 69 6e 67 20 61 75 74 6f 20 70   creating auto p
be400 72 6f 78 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66  roxy dir. */.#if
be410 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
be420 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52  ULT_PROXYDIR_PER
be430 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e  MISSIONS.# defin
be440 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
be450 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53  _PROXYDIR_PERMIS
be460 53 49 4f 4e 53 20 30 37 35 35 0a 23 65 6e 64 69  SIONS 0755.#endi
be470 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d  f../*.** Maximum
be480 20 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 2d   supported path-
be490 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69  length..*/.#defi
be4a0 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20  ne MAX_PATHNAME 
be4b0 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20  512../*.** Only 
be4c0 73 65 74 20 74 68 65 20 6c 61 73 74 45 72 72 6e  set the lastErrn
be4d0 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 63  o if the error c
be4e0 6f 64 65 20 69 73 20 61 20 72 65 61 6c 20 65 72  ode is a real er
be4f0 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20  ror and not .** 
be500 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 65  a normal expecte
be510 64 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66  d return code of
be520 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20   SQLITE_BUSY or 
be530 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65  SQLITE_OK.*/.#de
be540 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52  fine IS_LOCK_ERR
be550 4f 52 28 78 29 20 20 28 28 78 20 21 3d 20 53 51  OR(x)  ((x != SQ
be560 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 21  LITE_OK) && (x !
be570 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 29 29 0a  = SQLITE_BUSY)).
be580 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69 78  ../*.** The unix
be590 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69  File structure i
be5a0 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71  s subclass of sq
be5b0 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69  lite3_file speci
be5c0 66 69 63 20 74 6f 20 74 68 65 20 75 6e 69 78 0a  fic to the unix.
be5d0 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74  ** VFS implement
be5e0 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64  ations..*/.typed
be5f0 65 66 20 73 74 72 75 63 74 20 75 6e 69 78 46 69  ef struct unixFi
be600 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73 74 72  le unixFile;.str
be610 75 63 74 20 75 6e 69 78 46 69 6c 65 20 7b 0a 20  uct unixFile {. 
be620 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
be630 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68  ods const *pMeth
be640 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74  od;  /* Always t
be650 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a  he first entry *
be660 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f  /.  struct unixO
be670 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20  penCnt *pOpen;  
be680 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f       /* Info abo
be690 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27 73  ut all open fd's
be6a0 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a   on this inode *
be6b0 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  /.  struct unixL
be6c0 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20  ockInfo *pLock; 
be6d0 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f       /* Info abo
be6e0 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73  ut locks on this
be6f0 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20   inode */.  int 
be700 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
be710 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
be720 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  The file descrip
be730 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72  tor */.  int dir
be740 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fd;             
be750 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
be760 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  e descriptor for
be770 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 2a   the directory *
be780 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
be790 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20  r locktype;     
be7a0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 79 70 65       /* The type
be7b0 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e   of lock held on
be7c0 20 74 68 69 73 20 66 64 20 2a 2f 0a 20 20 69 6e   this fd */.  in
be7d0 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 20  t lastErrno;    
be7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
be7f0 2a 20 54 68 65 20 75 6e 69 78 20 65 72 72 6e 6f  * The unix errno
be800 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49   from the last I
be810 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 76 6f  /O error */.  vo
be820 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  id *lockingConte
be830 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  xt;            /
be840 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20  * Locking style 
be850 73 70 65 63 69 66 69 63 20 73 74 61 74 65 20 2a  specific state *
be860 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  /.#if SQLITE_ENA
be870 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
be880 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67  E.  int openFlag
be890 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
be8a0 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 67       /* The flag
be8b0 73 20 73 70 65 63 69 66 69 65 64 20 61 74 20 6f  s specified at o
be8c0 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a  pen() */.#endif.
be8d0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
be8e0 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64  DSAFE && defined
be8f0 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74  (__linux__).  pt
be900 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20 20  hread_t tid;    
be910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
be920 2a 20 54 68 65 20 74 68 72 65 61 64 20 74 68 61  * The thread tha
be930 74 20 22 6f 77 6e 73 22 20 74 68 69 73 20 75 6e  t "owns" this un
be940 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66  ixFile */.#endif
be950 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
be960 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 3b 20    int isDelete; 
be970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be980 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20     /* Delete on 
be990 63 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f  close if true */
be9a0 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b  .  struct vxwork
be9b0 73 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 20  sFileId *pId;   
be9c0 20 20 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 69      /* Unique fi
be9d0 6c 65 20 49 44 20 2a 2f 0a 23 65 6e 64 69 66 0a  le ID */.#endif.
be9e0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
be9f0 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f   /* The next gro
bea00 75 70 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20  up of variables 
bea10 61 72 65 20 75 73 65 64 20 74 6f 20 74 72 61 63  are used to trac
bea20 6b 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  k whether or not
bea30 20 74 68 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61   the.  ** transa
bea40 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 6e  ction counter in
bea50 20 62 79 74 65 73 20 32 34 2d 32 37 20 6f 66 20   bytes 24-27 of 
bea60 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 61  database files a
bea70 72 65 20 75 70 64 61 74 65 64 0a 20 20 2a 2a 20  re updated.  ** 
bea80 77 68 65 6e 65 76 65 72 20 61 6e 79 20 70 61 72  whenever any par
bea90 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  t of the databas
beaa0 65 20 63 68 61 6e 67 65 73 2e 20 20 41 6e 20 61  e changes.  An a
beab0 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77  ssertion fault w
beac0 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69  ill.  ** occur i
bead0 66 20 61 20 66 69 6c 65 20 69 73 20 75 70 64 61  f a file is upda
beae0 74 65 64 20 77 69 74 68 6f 75 74 20 61 6c 73 6f  ted without also
beaf0 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 74 72   updating the tr
beb00 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63  ansaction.  ** c
beb10 6f 75 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65  ounter.  This te
beb20 73 74 20 69 73 20 6d 61 64 65 20 74 6f 20 61 76  st is made to av
beb30 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73  oid new problems
beb40 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65 0a   similar to the.
beb50 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63 72 69 62    ** one describ
beb60 65 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 35  ed by ticket #35
beb70 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 69  84. .  */.  unsi
beb80 67 6e 65 64 20 63 68 61 72 20 74 72 61 6e 73 43  gned char transC
beb90 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a 20 54 72  ntrChng;   /* Tr
beba0 75 65 20 69 66 20 74 68 65 20 74 72 61 6e 73 61  ue if the transa
bebb0 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 63 68  ction counter ch
bebc0 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67  anged */.  unsig
bebd0 6e 65 64 20 63 68 61 72 20 64 62 55 70 64 61 74  ned char dbUpdat
bebe0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  e;        /* Tru
bebf0 65 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66  e if any part of
bec00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63   database file c
bec10 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69  hanged */.  unsi
bec20 67 6e 65 64 20 63 68 61 72 20 69 6e 4e 6f 72 6d  gned char inNorm
bec30 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a 20 54 72  alWrite;   /* Tr
bec40 75 65 20 69 66 20 69 6e 20 61 20 6e 6f 72 6d 61  ue if in a norma
bec50 6c 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f  l write operatio
bec60 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 72  n */..  /* If tr
bec70 75 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 77  ue, that means w
bec80 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
bec90 74 68 20 61 20 64 61 74 61 62 61 73 65 20 66 69  th a database fi
beca0 6c 65 20 74 68 61 74 20 68 61 73 0a 20 20 2a 2a  le that has.  **
becb0 20 61 20 72 61 6e 67 65 20 6f 66 20 6c 6f 63 6b   a range of lock
becc0 69 6e 67 20 62 79 74 65 73 20 66 72 6f 6d 20 50  ing bytes from P
becd0 45 4e 44 49 4e 47 5f 42 59 54 45 20 74 68 72 6f  ENDING_BYTE thro
bece0 75 67 68 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  ugh PENDING_BYTE
becf0 2b 35 31 31 0a 20 20 2a 2a 20 77 68 69 63 68 20  +511.  ** which 
bed00 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20  should never be 
bed10 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e  read or written.
bed20 20 20 41 73 73 65 72 74 73 28 29 20 77 69 6c 6c    Asserts() will
bed30 20 76 65 72 69 66 79 20 74 68 69 73 20 2a 2f 0a   verify this */.
bed40 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
bed50 69 73 4c 6f 63 6b 61 62 6c 65 3b 20 20 20 20 20  isLockable;     
bed60 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 6c 65   /* True if file
bed70 20 6d 69 67 68 74 20 62 65 20 6c 6f 63 6b 65 64   might be locked
bed80 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65   */.#endif.#ifde
bed90 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
beda0 2f 2a 20 49 6e 20 74 65 73 74 20 6d 6f 64 65 2c  /* In test mode,
bedb0 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 69   increase the si
bedc0 7a 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ze of this struc
bedd0 74 75 72 65 20 61 20 62 69 74 20 73 6f 20 74 68  ture a bit so th
bede0 61 74 20 0a 20 20 2a 2a 20 69 74 20 69 73 20 6c  at .  ** it is l
bedf0 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 73  arger than the s
bee00 74 72 75 63 74 20 43 72 61 73 68 46 69 6c 65 20  truct CrashFile 
bee10 64 65 66 69 6e 65 64 20 69 6e 20 74 65 73 74 36  defined in test6
bee20 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20  .c..  */.  char 
bee30 61 50 61 64 64 69 6e 67 5b 33 32 5d 3b 0a 23 65  aPadding[32];.#e
bee40 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49  ndif.};../*.** I
bee50 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74  nclude code that
bee60 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c   is common to al
bee70 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a  l os_*.c files.*
bee80 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
bee90 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d  * Include os_com
beea0 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  mon.h in the mid
beeb0 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63  dle of os_unix.c
beec0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
beed0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
beee0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
beef0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
bef00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bef10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bef20 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
bef30 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
bef40 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
bef50 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
bef60 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
bef70 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
bef80 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
bef90 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
befa0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
befb0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
befc0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
befd0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
befe0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
beff0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
bf000 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
bf010 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
bf020 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
bf030 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
bf040 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
bf050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf090 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
bf0a0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
bf0b0 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69   macros and a li
bf0c0 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65  ttle bit of code
bf0d0 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
bf0e0 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65  to.** all of the
bf0f0 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66   platform-specif
bf100 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63  ic files (os_*.c
bf110 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64  ) and is #includ
bf120 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a  ed into those.**
bf130 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   files..**.** Th
bf140 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  is file should b
bf150 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74  e #included by t
bf160 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20  he os_*.c files 
bf170 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74  only.  It is not
bf180 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75   a.** general pu
bf190 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c  rpose header fil
bf1a0 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73  e..**.** $Id: os
bf1b0 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38  _common.h,v 1.38
bf1c0 20 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34   2009/02/24 18:4
bf1d0 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37  0:50 danielk1977
bf1e0 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
bf1f0 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a  f _OS_COMMON_H_.
bf200 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d  #define _OS_COMM
bf210 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20  ON_H_../*.** At 
bf220 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68  least two bugs h
bf230 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62  ave slipped in b
bf240 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65  ecause we change
bf250 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42  d the MEMORY_DEB
bf260 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53  UG.** macro to S
bf270 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20  QLITE_DEBUG and 
bf280 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66  some older makef
bf290 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65  iles have not ye
bf2a0 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77  t made the.** sw
bf2b0 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  itch.  The follo
bf2c0 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64  wing code should
bf2d0 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62   catch this prob
bf2e0 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lem at compile-t
bf2f0 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d  ime..*/.#ifdef M
bf300 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72  EMORY_DEBUG.# er
bf310 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f  ror "The MEMORY_
bf320 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f  DEBUG macro is o
bf330 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51  bsolete.  Use SQ
bf340 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65  LITE_DEBUG inste
bf350 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ad.".#endif..#if
bf360 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
bf370 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
bf380 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61  int sqlite3OSTra
bf390 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  ce = 0;.#define 
bf3a0 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20  OSTRACE1(X)     
bf3b0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
bf3c0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
bf3d0 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23  DebugPrintf(X).#
bf3e0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28  define OSTRACE2(
bf3f0 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73  X,Y)       if( s
bf400 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
bf410 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
bf420 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  tf(X,Y).#define 
bf430 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20  OSTRACE3(X,Y,Z) 
bf440 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
bf450 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
bf460 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
bf470 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
bf480 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69  CE4(X,Y,Z,A)   i
bf490 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
bf4a0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
bf4b0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a  Printf(X,Y,Z,A).
bf4c0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35  #define OSTRACE5
bf4d0 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20  (X,Y,Z,A,B) if( 
bf4e0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
bf4f0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
bf500 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  ntf(X,Y,Z,A,B).#
bf510 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
bf520 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20  X,Y,Z,A,B,C) \. 
bf530 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54     if(sqlite3OST
bf540 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
bf550 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
bf560 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,B,C).#define OS
bf570 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE7(X,Y,Z,A,B
bf580 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73  ,C,D) \.    if(s
bf590 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73  qlite3OSTrace) s
bf5a0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
bf5b0 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  f(X,Y,Z,A,B,C,D)
bf5c0 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
bf5d0 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69  STRACE1(X).#defi
bf5e0 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
bf5f0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bf600 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65  3(X,Y,Z).#define
bf610 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c   OSTRACE4(X,Y,Z,
bf620 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
bf630 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  CE5(X,Y,Z,A,B).#
bf640 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
bf650 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
bf660 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
bf670 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e  Y,Z,A,B,C,D).#en
bf680 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  dif../*.** Macro
bf690 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  s for performanc
bf6a0 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d  e tracing.  Norm
bf6b0 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e  ally turned off.
bf6c0 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20    Only works.** 
bf6d0 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65  on i486 hardware
bf6e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
bf6f0 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54  TE_PERFORMANCE_T
bf700 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74  RACE../* .** hwt
bf710 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69  ime.h contains i
bf720 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20  nline assembler 
bf730 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65  code for impleme
bf740 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70  nting .** high-p
bf750 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e  erformance timin
bf760 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f  g routines..*/./
bf770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
bf780 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20  nclude hwtime.h 
bf790 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
bf7a0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
bf7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
bf7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
bf7d0 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65  egin file hwtime
bf7e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
bf7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
bf810 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37  *.** 2008 May 27
bf820 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
bf830 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
bf840 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
bf850 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
bf860 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
bf870 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
bf880 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
bf890 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
bf8a0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
bf8b0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
bf8c0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
bf8d0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
bf8e0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
bf8f0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
bf900 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
bf910 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
bf920 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
bf930 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
bf940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf980 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
bf990 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e  file contains in
bf9a0 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f  line asm code fo
bf9b0 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69  r retrieving "hi
bf9c0 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a  gh-performance".
bf9d0 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20  ** counters for 
bf9e0 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a  x86 class CPUs..
bf9f0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d  **.** $Id: hwtim
bfa00 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30  e.h,v 1.3 2008/0
bfa10 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68  8/01 14:33:15 sh
bfa20 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  ane Exp $.*/.#if
bfa30 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a  ndef _HWTIME_H_.
bfa40 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f  #define _HWTIME_
bfa50 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  H_../*.** The fo
bfa60 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
bfa70 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65  only works on pe
bfa80 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20  ntium-class (or 
bfa90 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72  newer) processor
bfaa0 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68  s..** It uses th
bfab0 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74  e RDTSC opcode t
bfac0 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65  o read the cycle
bfad0 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74   count value out
bfae0 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65   of the.** proce
bfaf0 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73  ssor and returns
bfb00 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68   that value.  Th
bfb10 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  is can be used f
bfb20 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70  or high-res.** p
bfb30 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66  rofiling..*/.#if
bfb40 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
bfb50 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
bfb60 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20  MSC_VER)) && \. 
bfb70 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33       (defined(i3
bfb80 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  86) || defined(_
bfb90 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69  _i386__) || defi
bfba0 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20  ned(_M_IX86)).. 
bfbb0 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47   #if defined(__G
bfbc0 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69  NUC__)..  __inli
bfbd0 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
bfbe0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
bfbf0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73  (void){.     uns
bfc00 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69  igned int lo, hi
bfc10 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f  ;.     __asm__ _
bfc20 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64  _volatile__ ("rd
bfc30 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29  tsc" : "=a" (lo)
bfc40 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20  , "=d" (hi));.  
bfc50 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74     return (sqlit
bfc60 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33  e_uint64)hi << 3
bfc70 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23  2 | lo;.  }..  #
bfc80 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
bfc90 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c  C_VER)..  __decl
bfca0 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e  spec(naked) __in
bfcb0 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74  line sqlite_uint
bfcc0 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74  64 __cdecl sqlit
bfcd0 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
bfce0 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20       __asm {.   
bfcf0 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20       rdtsc.     
bfd00 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72     ret       ; r
bfd10 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45  eturn value at E
bfd20 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20  DX:EAX.     }.  
bfd30 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c  }..  #endif..#el
bfd40 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
bfd50 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
bfd60 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20  (__x86_64__)).. 
bfd70 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
bfd80 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
bfd90 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
bfda0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
bfdb0 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f  ng val;.      __
bfdc0 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
bfdd0 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
bfde0 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20  A" (val));.     
bfdf0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d   return val;.  }
bfe00 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65  . .#elif (define
bfe10 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
bfe20 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29  efined(__ppc__))
bfe30 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
bfe40 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
bfe50 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
bfe60 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
bfe70 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61   long long retva
bfe80 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  l;.      unsigne
bfe90 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20  d long junk;.   
bfea0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
bfeb0 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20  atile__ ("\n\.  
bfec0 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20          1:      
bfed0 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20  mftbu   %1\n\.  
bfee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfef0 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20  mftb    %L0\n\. 
bff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bff10 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20   mftbu   %0\n\. 
bff20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bff30 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e   cmpw    %0,%1\n
bff40 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
bff50 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a      bne     1b".
bff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bff70 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c    : "=r" (retval
bff80 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b  ), "=r" (junk));
bff90 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65  .      return re
bffa0 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65  tval;.  }..#else
bffb0 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20  ..  #error Need 
bffc0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
bffd0 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  f sqlite3Hwtime(
bffe0 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66  ) for your platf
bfff0 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  orm...  /*.  ** 
c0000 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f  To compile witho
c0010 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  ut implementing 
c0020 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
c0030 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72  for your platfor
c0040 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20  m,.  ** you can 
c0050 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65  remove the above
c0060 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20   #error and use 
c0070 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
c0080 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e  ** stub function
c0090 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65  .  You will lose
c00a0 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20   timing support 
c00b0 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66  for many.  ** of
c00c0 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61   the debugging a
c00d0 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69  nd testing utili
c00e0 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f  ties, but it sho
c00f0 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73  uld at.  ** leas
c0100 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75  t compile and ru
c0110 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  n..  */.SQLITE_P
c0120 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f  RIVATE   sqlite_
c0130 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
c0140 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75  time(void){ retu
c0150 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74  rn ((sqlite_uint
c0160 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66  64)0); }..#endif
c0170 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
c0180 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29  ined(_HWTIME_H_)
c0190 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
c01a0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69  **** End of hwti
c01b0 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  me.h ***********
c01c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c01d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c01e0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c01f0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
c0200 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
c0210 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  f in os_common.h
c0220 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c0230 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c  ***/..static sql
c0240 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61  ite_uint64 g_sta
c0250 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74  rt;.static sqlit
c0260 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73  e_uint64 g_elaps
c0270 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  ed;.#define TIME
c0280 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f  R_START       g_
c0290 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74  start=sqlite3Hwt
c02a0 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49  ime().#define TI
c02b0 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20  MER_END         
c02c0 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65  g_elapsed=sqlite
c02d0 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72  3Hwtime()-g_star
c02e0 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  t.#define TIMER_
c02f0 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c  ELAPSED     g_el
c0300 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66  apsed.#else.#def
c0310 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a  ine TIMER_START.
c0320 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e  #define TIMER_EN
c0330 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  D.#define TIMER_
c0340 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71  ELAPSED     ((sq
c0350 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23  lite_uint64)0).#
c0360 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
c0370 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  we compile with 
c0380 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20  the SQLITE_TEST 
c0390 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20  macro set, then 
c03a0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  the following bl
c03b0 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77  ock.** of code w
c03c0 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20  ill give us the 
c03d0 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c  ability to simul
c03e0 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65  ate a disk I/O e
c03f0 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69  rror.  This.** i
c0400 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
c0410 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76  ng the I/O recov
c0420 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69  ery logic..*/.#i
c0430 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
c0440 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
c0450 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c0460 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20  _hit = 0;       
c0470 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
c0480 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f  mber of I/O Erro
c0490 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rs */.SQLITE_API
c04a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
c04b0 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20  error_hardhit = 
c04c0 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  0;        /* Num
c04d0 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67  ber of non-benig
c04e0 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49  n errors */.SQLI
c04f0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c0500 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
c0510 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ing = 0;        
c0520 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f  /* Count down to
c0530 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72   first I/O error
c0540 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
c0550 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
c0560 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b  ror_persist = 0;
c0570 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
c0580 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65  if I/O errors pe
c0590 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f  rsist */.SQLITE_
c05a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c05b0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20  io_error_benign 
c05c0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
c05d0 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61  True if errors a
c05e0 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c  re benign */.SQL
c05f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c0600 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
c0610 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45  ding = 0;.SQLITE
c0620 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
c0630 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23  _diskfull = 0;.#
c0640 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
c0650 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20  OErrorBenign(X) 
c0660 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c0670 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66  _benign=(X).#def
c0680 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
c0690 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69  ror(CODE)  \.  i
c06a0 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65  f( (sqlite3_io_e
c06b0 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20  rror_persist && 
c06c0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c06d0 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c  _hit) \.       |
c06e0 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  | sqlite3_io_err
c06f0 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20  or_pending-- == 
c0700 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20  1 )  \.         
c0710 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65       { local_ioe
c0720 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74  rr(); CODE; }.st
c0730 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f  atic void local_
c0740 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41  ioerr(){.  IOTRA
c0750 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b  CE(("IOERR\n"));
c0760 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
c0770 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28  ror_hit++;.  if(
c0780 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72   !sqlite3_io_err
c0790 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69  or_benign ) sqli
c07a0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72  te3_io_error_har
c07b0 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e  dhit++;.}.#defin
c07c0 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75  e SimulateDiskfu
c07d0 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a  llError(CODE) \.
c07e0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64     if( sqlite3_d
c07f0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20  iskfull_pending 
c0800 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71  ){ \.     if( sq
c0810 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
c0820 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c  ending == 1 ){ \
c0830 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f  .       local_io
c0840 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20  err(); \.       
c0850 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
c0860 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73   = 1; \.       s
c0870 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c0880 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  hit = 1; \.     
c0890 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d    CODE; \.     }
c08a0 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73  else{ \.       s
c08b0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
c08c0 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20  pending--; \.   
c08d0 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65    } \.   }.#else
c08e0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
c08f0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58  eIOErrorBenign(X
c0900 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
c0910 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65  teIOError(A).#de
c0920 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
c0930 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65  kfullError(A).#e
c0940 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ndif../*.** When
c0950 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61   testing, keep a
c0960 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75   count of the nu
c0970 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c  mber of open fil
c0980 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  es..*/.#ifdef SQ
c0990 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
c09a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
c09b0 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74  _open_file_count
c09c0 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70   = 0;.#define Op
c09d0 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71  enCounter(X)  sq
c09e0 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
c09f0 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65  count+=(X).#else
c0a00 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
c0a10 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a  nter(X).#endif..
c0a20 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
c0a30 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ed(_OS_COMMON_H_
c0a40 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
c0a50 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
c0a60 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
c0a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0a90 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
c0aa0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
c0ab0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
c0ac0 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20  ff in os_unix.c 
c0ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0ae0 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  ****/../*.** Def
c0af0 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72  ine various macr
c0b00 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73  os that are miss
c0b10 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79  ing from some sy
c0b20 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  stems..*/.#ifnde
c0b30 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20  f O_LARGEFILE.# 
c0b40 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49  define O_LARGEFI
c0b50 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64  LE 0.#endif.#ifd
c0b60 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  ef SQLITE_DISABL
c0b70 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f  E_LFS.# undef O_
c0b80 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69  LARGEFILE.# defi
c0b90 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30  ne O_LARGEFILE 0
c0ba0 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
c0bb0 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66  O_NOFOLLOW.# def
c0bc0 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30  ine O_NOFOLLOW 0
c0bd0 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
c0be0 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e  O_BINARY.# defin
c0bf0 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e  e O_BINARY 0.#en
c0c00 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44  dif../*.** The D
c0c10 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e  JGPP compiler en
c0c20 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20  vironment looks 
c0c30 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78  mostly like Unix
c0c40 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b  , but it.** lack
c0c50 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79  s the fcntl() sy
c0c60 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72  stem call.  So r
c0c70 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20  edefine fcntl() 
c0c80 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a  to be something.
c0c90 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73  ** that always s
c0ca0 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d  ucceeds.  This m
c0cb0 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e  eans that lockin
c0cc0 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72  g does not occur
c0cd0 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e   under.** DJGPP.
c0ce0 20 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20    But it is DOS 
c0cf0 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65  - what did you e
c0d00 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66  xpect?.*/.#ifdef
c0d10 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66   __DJGPP__.# def
c0d20 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29  ine fcntl(A,B,C)
c0d30 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
c0d40 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61   The threadid ma
c0d50 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20  cro resolves to 
c0d60 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72  the thread-id or
c0d70 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72   to 0.  Used for
c0d80 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20  .** testing and 
c0d90 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a  debugging only..
c0da0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
c0db0 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65  READSAFE.#define
c0dc0 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61   threadid pthrea
c0dd0 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23  d_self().#else.#
c0de0 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20  define threadid 
c0df0 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
c0e00 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
c0e10 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20  s to obtain and 
c0e20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67  relinquish the g
c0e30 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 0a 2a 2f 0a  lobal mutex..*/.
c0e40 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78  static void unix
c0e50 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29  EnterMutex(void)
c0e60 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
c0e70 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d  x_enter(sqlite3M
c0e80 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
c0e90 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
c0ea0 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63  STER));.}.static
c0eb0 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 4d   void unixLeaveM
c0ec0 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71  utex(void){.  sq
c0ed0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
c0ee0 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  e(sqlite3MutexAl
c0ef0 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
c0f00 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
c0f10 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  ;.}...#ifdef SQL
c0f20 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
c0f30 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
c0f40 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74  for printing out
c0f50 20 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69   trace informati
c0f60 6f 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e  on from debuggin
c0f70 67 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54  g.** binaries. T
c0f80 68 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20  his returns the 
c0f90 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61  string represeta
c0fa0 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70  tion of the supp
c0fb0 6c 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20  lied.** integer 
c0fc0 6c 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74  lock-type..*/.st
c0fd0 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
c0fe0 2a 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e  *locktypeName(in
c0ff0 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73  t locktype){.  s
c1000 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20  witch( locktype 
c1010 29 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43  ){.  case NO_LOC
c1020 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22  K: return "NONE"
c1030 3b 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f  ;.  case SHARED_
c1040 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48  LOCK: return "SH
c1050 41 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45  ARED";.  case RE
c1060 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74  SERVED_LOCK: ret
c1070 75 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a  urn "RESERVED";.
c1080 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c    case PENDING_L
c1090 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e  OCK: return "PEN
c10a0 44 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58  DING";.  case EX
c10b0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65  CLUSIVE_LOCK: re
c10c0 74 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22  turn "EXCLUSIVE"
c10d0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22  ;.  }.  return "
c10e0 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66  ERROR";.}.#endif
c10f0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
c1100 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a  LOCK_TRACE./*.**
c1110 20 50 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72   Print out infor
c1120 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c  mation about all
c1130 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69   locking operati
c1140 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ons..**.** This 
c1150 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
c1160 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74  for troubleshoot
c1170 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c  ing locks on mul
c1180 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c  tithreaded.** pl
c1190 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65  atforms.  Enable
c11a0 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69   by compiling wi
c11b0 74 68 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f  th the -DSQLITE_
c11c0 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f  LOCK_TRACE.** co
c11d0 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f  mmand-line optio
c11e0 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  n on the compile
c11f0 72 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73  r.  This code is
c1200 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72   normally.** tur
c1210 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74  ned off..*/.stat
c1220 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65  ic int lockTrace
c1230 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c  (int fd, int op,
c1240 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70   struct flock *p
c1250 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61  ){.  char *zOpNa
c1260 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e  me, *zType;.  in
c1270 74 20 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64  t s;.  int saved
c1280 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d  Errno;.  if( op=
c1290 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20  =F_GETLK ){.    
c12a0 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b  zOpName = "GETLK
c12b0 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  ";.  }else if( o
c12c0 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20  p==F_SETLK ){.  
c12d0 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54    zOpName = "SET
c12e0 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  LK";.  }else{.  
c12f0 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20    s = fcntl(fd, 
c1300 6f 70 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69  op, p);.    sqli
c1310 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
c1320 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64  fcntl unknown %d
c1330 20 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f   %d %d\n", fd, o
c1340 70 2c 20 73 29 3b 0a 20 20 20 20 72 65 74 75 72  p, s);.    retur
c1350 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n s;.  }.  if( p
c1360 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43  ->l_type==F_RDLC
c1370 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d  K ){.    zType =
c1380 20 22 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73   "RDLCK";.  }els
c1390 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d  e if( p->l_type=
c13a0 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_WRLCK ){.    
c13b0 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b  zType = "WRLCK";
c13c0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
c13d0 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20  l_type==F_UNLCK 
c13e0 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22  ){.    zType = "
c13f0 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b  UNLCK";.  }else{
c1400 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29  .    assert( 0 )
c1410 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
c1420 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45  p->l_whence==SEE
c1430 4b 5f 53 45 54 20 29 3b 0a 20 20 73 20 3d 20 66  K_SET );.  s = f
c1440 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b  cntl(fd, op, p);
c1450 0a 20 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20  .  savedErrno = 
c1460 65 72 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33  errno;.  sqlite3
c1470 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e  DebugPrintf("fcn
c1480 74 6c 20 25 64 20 25 64 20 25 73 20 25 73 20 25  tl %d %d %s %s %
c1490 64 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20  d %d %d %d\n",. 
c14a0 20 20 20 20 74 68 72 65 61 64 69 64 2c 20 66 64      threadid, fd
c14b0 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65  , zOpName, zType
c14c0 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72  , (int)p->l_star
c14d0 74 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e  t, (int)p->l_len
c14e0 2c 0a 20 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c  ,.     (int)p->l
c14f0 5f 70 69 64 2c 20 73 29 3b 0a 20 20 69 66 28 20  _pid, s);.  if( 
c1500 73 3d 3d 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46  s==(-1) && op==F
c1510 5f 53 45 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f  _SETLK && (p->l_
c1520 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c  type==F_RDLCK ||
c1530 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52   p->l_type==F_WR
c1540 4c 43 4b 29 20 29 7b 0a 20 20 20 20 73 74 72 75  LCK) ){.    stru
c1550 63 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20  ct flock l2;.   
c1560 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63   l2 = *p;.    fc
c1570 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c  ntl(fd, F_GETLK,
c1580 20 26 6c 32 29 3b 0a 20 20 20 20 69 66 28 20 6c   &l2);.    if( l
c1590 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43  2.l_type==F_RDLC
c15a0 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65  K ){.      zType
c15b0 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20   = "RDLCK";.    
c15c0 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74  }else if( l2.l_t
c15d0 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a  ype==F_WRLCK ){.
c15e0 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57        zType = "W
c15f0 52 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65  RLCK";.    }else
c1600 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d   if( l2.l_type==
c1610 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20  F_UNLCK ){.     
c1620 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22   zType = "UNLCK"
c1630 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
c1640 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
c1650 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
c1660 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63  3DebugPrintf("fc
c1670 6e 74 6c 2d 66 61 69 6c 75 72 65 2d 72 65 61 73  ntl-failure-reas
c1680 6f 6e 3a 20 25 73 20 25 64 20 25 64 20 25 64 5c  on: %s %d %d %d\
c1690 6e 22 2c 0a 20 20 20 20 20 20 20 7a 54 79 70 65  n",.       zType
c16a0 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72  , (int)l2.l_star
c16b0 74 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e  t, (int)l2.l_len
c16c0 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29  , (int)l2.l_pid)
c16d0 3b 0a 20 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20  ;.  }.  errno = 
c16e0 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65  savedErrno;.  re
c16f0 74 75 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e  turn s;.}.#defin
c1700 65 20 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63  e fcntl lockTrac
c1710 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
c1720 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f  TE_LOCK_TRACE */
c1730 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  ..../*.** This r
c1740 6f 75 74 69 6e 65 20 74 72 61 6e 73 6c 61 74 65  outine translate
c1750 73 20 61 20 73 74 61 6e 64 61 72 64 20 50 4f 53  s a standard POS
c1760 49 58 20 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e  IX errno code in
c1770 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20  to something.** 
c1780 75 73 65 66 75 6c 20 74 6f 20 74 68 65 20 63 6c  useful to the cl
c1790 69 65 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c  ients of the sql
c17a0 69 74 65 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20  ite3 functions. 
c17b0 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 69   Specifically, i
c17c0 74 20 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64  t is.** intended
c17d0 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 61 20   to translate a 
c17e0 76 61 72 69 65 74 79 20 6f 66 20 22 74 72 79 20  variety of "try 
c17f0 61 67 61 69 6e 22 20 65 72 72 6f 72 73 20 69 6e  again" errors in
c1800 74 6f 20 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a  to SQLITE_BUSY.*
c1810 2a 20 61 6e 64 20 61 20 76 61 72 69 65 74 79 20  * and a variety 
c1820 6f 66 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65  of "please close
c1830 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
c1840 70 74 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73  ptor NOW" errors
c1850 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45   into .** SQLITE
c1860 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72  _IOERR.** .** Er
c1870 72 6f 72 73 20 64 75 72 69 6e 67 20 69 6e 69 74  rors during init
c1880 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f  ialization of lo
c1890 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20 73 79 73  cks, or file sys
c18a0 74 65 6d 20 73 75 70 70 6f 72 74 20 66 6f 72 20  tem support for 
c18b0 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64  locks,.** should
c18c0 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20   handle ENOLCK, 
c18d0 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53  ENOTSUP, EOPNOTS
c18e0 55 50 50 20 73 65 70 61 72 61 74 65 6c 79 2e 0a  UPP separately..
c18f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
c1900 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
c1910 69 78 45 72 72 6f 72 28 69 6e 74 20 70 6f 73 69  ixError(int posi
c1920 78 45 72 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69  xError, int sqli
c1930 74 65 49 4f 45 72 72 29 20 7b 0a 20 20 73 77 69  teIOErr) {.  swi
c1940 74 63 68 20 28 70 6f 73 69 78 45 72 72 6f 72 29  tch (posixError)
c1950 20 7b 0a 20 20 63 61 73 65 20 30 3a 20 0a 20 20   {.  case 0: .  
c1960 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c1970 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20  OK;.    .  case 
c1980 45 41 47 41 49 4e 3a 0a 20 20 63 61 73 65 20 45  EAGAIN:.  case E
c1990 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61 73 65  TIMEDOUT:.  case
c19a0 20 45 42 55 53 59 3a 0a 20 20 63 61 73 65 20 45   EBUSY:.  case E
c19b0 49 4e 54 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f  INTR:.  case ENO
c19c0 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61  LCK:  .    /* ra
c19d0 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72 79 20 65  ndom NFS retry e
c19e0 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64 75 72  rror, unless dur
c19f0 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20  ing file system 
c1a00 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20  support .     * 
c1a10 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69  introspection, i
c1a20 6e 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61  n which it actua
c1a30 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69  lly means what i
c1a40 74 20 73 61 79 73 20 2a 2f 0a 20 20 20 20 72 65  t says */.    re
c1a50 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
c1a60 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41  ;.    .  case EA
c1a70 43 43 45 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41  CCES: .    /* EA
c1a80 43 43 45 53 20 69 73 20 6c 69 6b 65 20 45 41 47  CCES is like EAG
c1a90 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69  AIN during locki
c1aa0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62  ng operations, b
c1ab0 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72  ut not any other
c1ac0 20 74 69 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20   time*/.    if( 
c1ad0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20  (sqliteIOErr == 
c1ae0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
c1af0 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49  K) || ..(sqliteI
c1b00 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49  OErr == SQLITE_I
c1b10 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 7c 7c 20  OERR_UNLOCK) || 
c1b20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d  ..(sqliteIOErr =
c1b30 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  = SQLITE_IOERR_R
c1b40 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73 71 6c 69  DLOCK) ||..(sqli
c1b50 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54  teIOErr == SQLIT
c1b60 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
c1b70 45 52 56 45 44 4c 4f 43 4b 29 20 29 7b 0a 20 20  ERVEDLOCK) ){.  
c1b80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
c1b90 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20  E_BUSY;.    }.  
c1ba0 20 20 2f 2a 20 65 6c 73 65 20 66 61 6c 6c 20 74    /* else fall t
c1bb0 68 72 6f 75 67 68 20 2a 2f 0a 20 20 63 61 73 65  hrough */.  case
c1bc0 20 45 50 45 52 4d 3a 20 0a 20 20 20 20 72 65 74   EPERM: .    ret
c1bd0 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b  urn SQLITE_PERM;
c1be0 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 44 45  .    .  case EDE
c1bf0 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74 75 72 6e  ADLK:.    return
c1c00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c   SQLITE_IOERR_BL
c1c10 4f 43 4b 45 44 3b 0a 20 20 20 20 0a 23 69 66 20  OCKED;.    .#if 
c1c20 45 4f 50 4e 4f 54 53 55 50 50 21 3d 45 4e 4f 54  EOPNOTSUPP!=ENOT
c1c30 53 55 50 0a 20 20 63 61 73 65 20 45 4f 50 4e 4f  SUP.  case EOPNO
c1c40 54 53 55 50 50 3a 20 0a 20 20 20 20 2f 2a 20 73  TSUPP: .    /* s
c1c50 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 74 65  omething went te
c1c60 72 72 69 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c  rribly awry, unl
c1c70 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20  ess during file 
c1c80 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a  system support .
c1c90 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63       * introspec
c1ca0 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69  tion, in which i
c1cb0 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73  t actually means
c1cc0 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f   what it says */
c1cd0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 45  .#endif.#ifdef E
c1ce0 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4e  NOTSUP.  case EN
c1cf0 4f 54 53 55 50 3a 20 0a 20 20 20 20 2f 2a 20 69  OTSUP: .    /* i
c1d00 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e 6c 65 73  nvalid fd, unles
c1d10 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79  s during file sy
c1d20 73 74 65 6d 20 73 75 70 70 6f 72 74 20 69 6e 74  stem support int
c1d30 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77  rospection, in w
c1d40 68 69 63 68 20 0a 20 20 20 20 20 2a 20 69 74 20  hich .     * it 
c1d50 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77  actually means w
c1d60 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23  hat it says */.#
c1d70 65 6e 64 69 66 0a 20 20 63 61 73 65 20 45 49 4f  endif.  case EIO
c1d80 3a 0a 20 20 63 61 73 65 20 45 42 41 44 46 3a 0a  :.  case EBADF:.
c1d90 20 20 63 61 73 65 20 45 49 4e 56 41 4c 3a 0a 20    case EINVAL:. 
c1da0 20 63 61 73 65 20 45 4e 4f 54 43 4f 4e 4e 3a 0a   case ENOTCONN:.
c1db0 20 20 63 61 73 65 20 45 4e 4f 44 45 56 3a 0a 20    case ENODEV:. 
c1dc0 20 63 61 73 65 20 45 4e 58 49 4f 3a 0a 20 20 63   case ENXIO:.  c
c1dd0 61 73 65 20 45 4e 4f 45 4e 54 3a 0a 20 20 63 61  ase ENOENT:.  ca
c1de0 73 65 20 45 53 54 41 4c 45 3a 0a 20 20 63 61 73  se ESTALE:.  cas
c1df0 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20 20 2f 2a  e ENOSYS:.    /*
c1e00 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 66 6f   these should fo
c1e10 72 63 65 20 74 68 65 20 63 6c 69 65 6e 74 20 74  rce the client t
c1e20 6f 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  o close the file
c1e30 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63 74 20 2a   and reconnect *
c1e40 2f 0a 20 20 20 20 0a 20 20 64 65 66 61 75 6c 74  /.    .  default
c1e50 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  : .    return sq
c1e60 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d  liteIOErr;.  }.}
c1e70 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..../***********
c1e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1ec0 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
c1ed0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69  ****** Begin Uni
c1ee0 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c  que File ID Util
c1ef0 69 74 79 20 55 73 65 64 20 42 79 20 56 78 57 6f  ity Used By VxWo
c1f00 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  rks ************
c1f10 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73  ***.**.** On mos
c1f20 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e  t versions of un
c1f30 69 78 2c 20 77 65 20 63 61 6e 20 67 65 74 20 61  ix, we can get a
c1f40 20 75 6e 69 71 75 65 20 49 44 20 66 6f 72 20 61   unique ID for a
c1f50 20 66 69 6c 65 20 62 79 20 63 6f 6e 63 61 74 65   file by concate
c1f60 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65  nating.** the de
c1f70 76 69 63 65 20 6e 75 6d 62 65 72 20 61 6e 64 20  vice number and 
c1f80 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72  the inode number
c1f90 2e 20 20 42 75 74 20 74 68 69 73 20 64 6f 65 73  .  But this does
c1fa0 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57   not work on VxW
c1fb0 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f  orks..** On VxWo
c1fc0 72 6b 73 2c 20 61 20 75 6e 69 71 75 65 20 66 69  rks, a unique fi
c1fd0 6c 65 20 69 64 20 6d 75 73 74 20 62 65 20 62 61  le id must be ba
c1fe0 73 65 64 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e  sed on the canon
c1ff0 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a  ical filename..*
c2000 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74  *.** A pointer t
c2010 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  o an instance of
c2020 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
c2030 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20  tructure can be 
c2040 75 73 65 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69  used as a.** uni
c2050 71 75 65 20 66 69 6c 65 20 49 44 20 69 6e 20 56  que file ID in V
c2060 78 57 6f 72 6b 73 2e 20 20 45 61 63 68 20 69 6e  xWorks.  Each in
c2070 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
c2080 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
c2090 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74  s.** a copy of t
c20a0 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c  he canonical fil
c20b0 65 6e 61 6d 65 2e 20 20 54 68 65 72 65 20 69 73  ename.  There is
c20c0 20 61 6c 73 6f 20 61 20 72 65 66 65 72 65 6e 63   also a referenc
c20d0 65 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68  e count.  .** Th
c20e0 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72  e structure is r
c20f0 65 63 6c 61 69 6d 65 64 20 77 68 65 6e 20 74 68  eclaimed when th
c2100 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e  e number of poin
c2110 74 65 72 73 20 74 6f 20 69 74 20 64 72 6f 70 73  ters to it drops
c2120 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a   to.** zero..**.
c2130 2a 2a 20 54 68 65 72 65 20 61 72 65 20 6e 65 76  ** There are nev
c2140 65 72 20 76 65 72 79 20 6d 61 6e 79 20 66 69 6c  er very many fil
c2150 65 73 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74  es open at one t
c2160 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20  ime and lookups 
c2170 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72  are not.** a per
c2180 66 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69 63 61  formance-critica
c2190 6c 20 70 61 74 68 2c 20 73 6f 20 69 74 20 69 73  l path, so it is
c21a0 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 70   sufficient to p
c21b0 75 74 20 74 68 65 73 65 0a 2a 2a 20 73 74 72 75  ut these.** stru
c21c0 63 74 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b  ctures on a link
c21d0 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75  ed list..*/.stru
c21e0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  ct vxworksFileId
c21f0 20 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f   {.  struct vxwo
c2200 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 78 74  rksFileId *pNext
c2210 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20  ;  /* Next in a 
c2220 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c  list of them all
c2230 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
c2240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2250 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
c2260 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
c2270 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74  his one */.  int
c2280 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20   nName;         
c2290 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
c22a0 6e 67 74 68 20 6f 66 20 74 68 65 20 7a 43 61 6e  ngth of the zCan
c22b0 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72  onicalName[] str
c22c0 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ing */.  char *z
c22d0 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20  CanonicalName;  
c22e0 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69         /* Canoni
c22f0 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a  cal filename */.
c2300 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  };..#if OS_VXWOR
c2310 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e  KS./* .** All un
c2320 69 71 75 65 20 66 69 6c 65 6e 61 6d 65 73 20 61  ique filenames a
c2330 72 65 20 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e  re held on a lin
c2340 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20  ked list headed 
c2350 62 79 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61  by this.** varia
c2360 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ble:.*/.static s
c2370 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c  truct vxworksFil
c2380 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65  eId *vxworksFile
c2390 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a  List = 0;../*.**
c23a0 20 53 69 6d 70 6c 69 66 79 20 61 20 66 69 6c 65   Simplify a file
c23b0 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73 20 63 61  name into its ca
c23c0 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20  nonical form.** 
c23d0 62 79 20 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f  by making the fo
c23e0 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 3a  llowing changes:
c23f0 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69  .**.**  * removi
c2400 6e 67 20 61 6e 79 20 74 72 61 69 6c 69 6e 67 20  ng any trailing 
c2410 61 6e 64 20 64 75 70 6c 69 63 61 74 65 20 2f 0a  and duplicate /.
c2420 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e  **  * convert /.
c2430 2f 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a  / into just /.**
c2440 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e    * convert /A/.
c2450 2e 2f 20 77 68 65 72 65 20 41 20 69 73 20 61 6e  ./ where A is an
c2460 79 20 73 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e  y simple name in
c2470 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20  to just /.**.** 
c2480 43 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65  Changes are made
c2490 20 69 6e 2d 70 6c 61 63 65 2e 20 20 52 65 74 75   in-place.  Retu
c24a0 72 6e 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 20  rn the new name 
c24b0 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68  length..**.** Th
c24c0 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e  e original filen
c24d0 61 6d 65 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e  ame is in z[0..n
c24e0 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 74 68 65  -1].  Return the
c24f0 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68   number of.** ch
c2500 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20  aracters in the 
c2510 73 69 6d 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e  simplified name.
c2520 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
c2530 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61  xworksSimplifyNa
c2540 6d 65 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  me(char *z, int 
c2550 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  n){.  int i, j;.
c2560 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20    while( n>1 && 
c2570 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e  z[n-1]=='/' ){ n
c2580 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d  --; }.  for(i=j=
c2590 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<n; i++){.  
c25a0 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20    if( z[i]=='/' 
c25b0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69  ){.      if( z[i
c25c0 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69  +1]=='/' ) conti
c25d0 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  nue;.      if( z
c25e0 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b  [i+1]=='.' && i+
c25f0 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27  2<n && z[i+2]=='
c2600 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20  /' ){.        i 
c2610 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 6f  += 1;.        co
c2620 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
c2630 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d        if( z[i+1]
c2640 3d 3d 27 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26  =='.' && i+3<n &
c2650 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26  & z[i+2]=='.' &&
c2660 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a   z[i+3]=='/' ){.
c2670 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a          while( j
c2680 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f  >0 && z[j-1]!='/
c2690 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20  ' ){ j--; }.    
c26a0 20 20 20 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a      if( j>0 ){ j
c26b0 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 20  --; }.        i 
c26c0 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 63 6f  += 2;.        co
c26d0 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
c26e0 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d      }.    z[j++]
c26f0 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a   = z[i];.  }.  z
c2700 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  [j] = 0;.  retur
c2710 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69  n j;.}../*.** Fi
c2720 6e 64 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65  nd a unique file
c2730 20 49 44 20 66 6f 72 20 74 68 65 20 67 69 76 65   ID for the give
c2740 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 6e  n absolute pathn
c2750 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  ame.  Return.** 
c2760 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
c2770 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f   vxworksFileId o
c2780 62 6a 65 63 74 2e 20 20 54 68 69 73 20 70 6f 69  bject.  This poi
c2790 6e 74 65 72 20 69 73 20 74 68 65 20 75 6e 69 71  nter is the uniq
c27a0 75 65 0a 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a  ue.** file ID..*
c27b0 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 66 20 66 69  *.** The nRef fi
c27c0 65 6c 64 20 6f 66 20 74 68 65 20 76 78 77 6f 72  eld of the vxwor
c27d0 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20  ksFileId object 
c27e0 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62  is incremented b
c27f0 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a  efore.** the obj
c2800 65 63 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ect is returned.
c2810 20 20 41 20 6e 65 77 20 76 78 77 6f 72 6b 73 46    A new vxworksF
c2820 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20  ileId object is 
c2830 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61  created.** and a
c2840 64 64 65 64 20 74 6f 20 74 68 65 20 67 6c 6f 62  dded to the glob
c2850 61 6c 20 6c 69 73 74 20 69 66 20 6e 65 63 65 73  al list if neces
c2860 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  sary..**.** If a
c2870 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
c2880 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  on error occurs,
c2890 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f   return NULL..*/
c28a0 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76  .static struct v
c28b0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78  xworksFileId *vx
c28c0 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28  worksFindFileId(
c28d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 62 73  const char *zAbs
c28e0 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74  oluteName){.  st
c28f0 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
c2900 49 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20  Id *pNew;       
c2910 20 20 2f 2a 20 73 65 61 72 63 68 20 6b 65 79 20    /* search key 
c2920 61 6e 64 20 6e 65 77 20 66 69 6c 65 20 49 44 20  and new file ID 
c2930 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f  */.  struct vxwo
c2940 72 6b 73 46 69 6c 65 49 64 20 2a 70 43 61 6e 64  rksFileId *pCand
c2950 69 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20  idate;   /* For 
c2960 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69  looping over exi
c2970 73 74 69 6e 67 20 66 69 6c 65 20 49 44 73 20 2a  sting file IDs *
c2980 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20  /.  int n;      
c2990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c29a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
c29b0 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61  h of zAbsoluteNa
c29c0 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20  me string */..  
c29d0 61 73 73 65 72 74 28 20 7a 41 62 73 6f 6c 75 74  assert( zAbsolut
c29e0 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b  eName[0]=='/' );
c29f0 0a 20 20 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c  .  n = (int)strl
c2a00 65 6e 28 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65  en(zAbsoluteName
c2a10 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  );.  pNew = sqli
c2a20 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
c2a30 6f 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31  of(*pNew) + (n+1
c2a40 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  ) );.  if( pNew=
c2a50 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
c2a60 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61   pNew->zCanonica
c2a70 6c 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26  lName = (char*)&
c2a80 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70  pNew[1];.  memcp
c2a90 79 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63  y(pNew->zCanonic
c2aa0 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74  alName, zAbsolut
c2ab0 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e  eName, n+1);.  n
c2ac0 20 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69   = vxworksSimpli
c2ad0 66 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61  fyName(pNew->zCa
c2ae0 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b  nonicalName, n);
c2af0 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f  ..  /* Search fo
c2b00 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e  r an existing en
c2b10 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 69 6e  try that matchin
c2b20 67 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20  g the canonical 
c2b30 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f  name..  ** If fo
c2b40 75 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74  und, increment t
c2b50 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
c2b60 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  nt and return a 
c2b70 70 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20  pointer to.  ** 
c2b80 74 68 65 20 65 78 69 73 74 69 6e 67 20 66 69 6c  the existing fil
c2b90 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69  e ID..  */.  uni
c2ba0 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
c2bb0 20 66 6f 72 28 70 43 61 6e 64 69 64 61 74 65 3d   for(pCandidate=
c2bc0 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b  vxworksFileList;
c2bd0 20 70 43 61 6e 64 69 64 61 74 65 3b 20 70 43 61   pCandidate; pCa
c2be0 6e 64 69 64 61 74 65 3d 70 43 61 6e 64 69 64 61  ndidate=pCandida
c2bf0 74 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  te->pNext){.    
c2c00 69 66 28 20 70 43 61 6e 64 69 64 61 74 65 2d 3e  if( pCandidate->
c2c10 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26  nName==n .     &
c2c20 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69 64  & memcmp(pCandid
c2c30 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e  ate->zCanonicalN
c2c40 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f  ame, pNew->zCano
c2c50 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30  nicalName, n)==0
c2c60 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 73  .    ){.       s
c2c70 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77  qlite3_free(pNew
c2c80 29 3b 0a 20 20 20 20 20 20 20 70 43 61 6e 64 69  );.       pCandi
c2c90 64 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  date->nRef++;.  
c2ca0 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75       unixLeaveMu
c2cb0 74 65 78 28 29 3b 0a 20 20 20 20 20 20 20 72 65  tex();.       re
c2cc0 74 75 72 6e 20 70 43 61 6e 64 69 64 61 74 65 3b  turn pCandidate;
c2cd0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
c2ce0 20 4e 6f 20 6d 61 74 63 68 20 77 61 73 20 66 6f   No match was fo
c2cf0 75 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61  und.  We will ma
c2d00 6b 65 20 61 20 6e 65 77 20 66 69 6c 65 20 49 44  ke a new file ID
c2d10 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66   */.  pNew->nRef
c2d20 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e   = 1;.  pNew->nN
c2d30 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d  ame = n;.  pNew-
c2d40 3e 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b 73  >pNext = vxworks
c2d50 46 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f  FileList;.  vxwo
c2d60 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e  rksFileList = pN
c2d70 65 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d  ew;.  unixLeaveM
c2d80 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e  utex();.  return
c2d90 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
c2da0 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65  Decrement the re
c2db0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e  ference count on
c2dc0 20 61 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64   a vxworksFileId
c2dd0 20 6f 62 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a   object.  Free.*
c2de0 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 77 68 65  * the object whe
c2df0 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  n the reference 
c2e00 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65  count reaches ze
c2e10 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ro..*/.static vo
c2e20 69 64 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73  id vxworksReleas
c2e30 65 46 69 6c 65 49 64 28 73 74 72 75 63 74 20 76  eFileId(struct v
c2e40 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49  xworksFileId *pI
c2e50 64 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d  d){.  unixEnterM
c2e60 75 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74  utex();.  assert
c2e70 28 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b  ( pId->nRef>0 );
c2e80 0a 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a  .  pId->nRef--;.
c2e90 20 20 69 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d    if( pId->nRef=
c2ea0 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  =0 ){.    struct
c2eb0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a   vxworksFileId *
c2ec0 2a 70 70 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d  *pp;.    for(pp=
c2ed0 26 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74  &vxworksFileList
c2ee0 3b 20 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 49  ; *pp && *pp!=pI
c2ef0 64 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 29 2d  d; pp = &((*pp)-
c2f00 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20 61  >pNext)){}.    a
c2f10 73 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 20  ssert( *pp==pId 
c2f20 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 49 64  );.    *pp = pId
c2f30 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c  ->pNext;.    sql
c2f40 69 74 65 33 5f 66 72 65 65 28 70 49 64 29 3b 0a  ite3_free(pId);.
c2f50 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d    }.  unixLeaveM
c2f60 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66  utex();.}.#endif
c2f70 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a   /* OS_VXWORKS *
c2f80 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c2f90 2a 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71 75 65  ** End of Unique
c2fa0 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79   File ID Utility
c2fb0 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73   Used By VxWorks
c2fc0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c2fd0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
c2fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3020 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /.../***********
c3030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3070 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
c3080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
c3090 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c  Posix Advisory L
c30a0 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ocking *********
c30b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c30c0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20  ***.**.** POSIX 
c30d0 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61  advisory locks a
c30e0 72 65 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 73  re broken by des
c30f0 69 67 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 31  ign.  ANSI STD 1
c3100 30 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20  003.1 (1996).** 
c3110 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20  section 6.5.2.2 
c3120 6c 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75 67  lines 483 throug
c3130 68 20 34 39 30 20 73 70 65 63 69 66 79 20 74 68  h 490 specify th
c3140 61 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65 73  at when a proces
c3150 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c 65  s.** sets or cle
c3160 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74  ars a lock, that
c3170 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 72   operation overr
c3180 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20 6c  ides any prior l
c3190 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20 74  ocks set.** by t
c31a0 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e  he same process.
c31b0 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65 78    It does not ex
c31c0 70 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f 2c  plicitly say so,
c31d0 20 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69 65   but this implie
c31e0 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f 76 65  s.** that it ove
c31f0 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 65 74  rrides locks set
c3200 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f   by the same pro
c3210 63 65 73 73 20 75 73 69 6e 67 20 61 20 64 69 66  cess using a dif
c3220 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64  ferent.** file d
c3230 65 73 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73  escriptor.  Cons
c3240 69 64 65 72 20 74 68 69 73 20 74 65 73 74 20 63  ider this test c
c3250 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ase:.**.**      
c3260 20 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e 28   int fd1 = open(
c3270 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57  "./file1", O_RDW
c3280 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29  R|O_CREAT, 0644)
c3290 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66  ;.**       int f
c32a0 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c  d2 = open("./fil
c32b0 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52  e2", O_RDWR|O_CR
c32c0 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a  EAT, 0644);.**.*
c32d0 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65  * Suppose ./file
c32e0 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72  1 and ./file2 ar
c32f0 65 20 72 65 61 6c 6c 79 20 74 68 65 20 73 61 6d  e really the sam
c3300 65 20 66 69 6c 65 20 28 62 65 63 61 75 73 65 0a  e file (because.
c3310 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 72 64  ** one is a hard
c3320 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e   or symbolic lin
c3330 6b 20 74 6f 20 74 68 65 20 6f 74 68 65 72 29 20  k to the other) 
c3340 74 68 65 6e 20 69 66 20 79 6f 75 20 73 65 74 0a  then if you set.
c3350 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ** an exclusive 
c3360 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65  lock on fd1, the
c3370 6e 20 74 72 79 20 74 6f 20 67 65 74 20 61 6e 20  n try to get an 
c3380 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a  exclusive lock.*
c3390 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f 72  * on fd2, it wor
c33a0 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 61 76  ks.  I would hav
c33b0 65 20 65 78 70 65 63 74 65 64 20 74 68 65 20 73  e expected the s
c33c0 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a  econd lock to.**
c33d0 20 66 61 69 6c 20 73 69 6e 63 65 20 74 68 65 72   fail since ther
c33e0 65 20 77 61 73 20 61 6c 72 65 61 64 79 20 61 20  e was already a 
c33f0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65  lock on the file
c3400 20 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20   due to fd1..** 
c3410 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e  But not so.  Sin
c3420 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61  ce both locks ca
c3430 6d 65 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65  me from the same
c3440 20 70 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a   process, the.**
c3450 20 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 65   second override
c3460 73 20 74 68 65 20 66 69 72 73 74 2c 20 65 76 65  s the first, eve
c3470 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20 77 65  n though they we
c3480 72 65 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a  re on different.
c3490 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ** file descript
c34a0 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69  ors opened on di
c34b0 66 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d  fferent file nam
c34c0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  es..**.** This m
c34d0 65 61 6e 73 20 74 68 61 74 20 77 65 20 63 61 6e  eans that we can
c34e0 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c 6f  not use POSIX lo
c34f0 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69  cks to synchroni
c3500 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73 0a 2a  ze file access.*
c3510 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e  * among competin
c3520 67 20 74 68 72 65 61 64 73 20 6f 66 20 74 68 65  g threads of the
c3530 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20   same process.  
c3540 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c  POSIX locks will
c3550 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f   work fine.** to
c3560 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63   synchronize acc
c3570 65 73 73 20 66 6f 72 20 74 68 72 65 61 64 73 20  ess for threads 
c3580 69 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f 63  in separate proc
c3590 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a  esses, but not.*
c35a0 2a 20 74 68 72 65 61 64 73 20 77 69 74 68 69 6e  * threads within
c35b0 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
c35c0 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b  s..**.** To work
c35d0 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f 62   around the prob
c35e0 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 20  lem, SQLite has 
c35f0 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c  to manage file l
c3600 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a  ocks internally.
c3610 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20  ** on its own.  
c3620 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 64  Whenever a new d
c3630 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
c3640 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 69  d, we have to fi
c3650 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66  nd the.** specif
c3660 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 20  ic inode of the 
c3670 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 74  database file (t
c3680 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74 65  he inode is dete
c3690 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a  rmined by the.**
c36a0 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f 69   st_dev and st_i
c36b0 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  no fields of the
c36c0 20 73 74 61 74 20 73 74 72 75 63 74 75 72 65 20   stat structure 
c36d0 74 68 61 74 20 66 73 74 61 74 28 29 20 66 69 6c  that fstat() fil
c36e0 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68  ls in).** and ch
c36f0 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c  eck for locks al
c3700 72 65 61 64 79 20 65 78 69 73 74 69 6e 67 20 6f  ready existing o
c3710 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 57  n that inode.  W
c3720 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a  hen locks are.**
c3730 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d 6f   created or remo
c3740 76 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20  ved, we have to 
c3750 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20  look at our own 
c3760 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20  internal record 
c3770 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20  of the.** locks 
c3780 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65  to see if anothe
c3790 72 20 74 68 72 65 61 64 20 68 61 73 20 70 72 65  r thread has pre
c37a0 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c 6f  viously set a lo
c37b0 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a  ck on that same.
c37c0 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ** inode..**.** 
c37d0 28 41 73 69 64 65 3a 20 54 68 65 20 75 73 65 20  (Aside: The use 
c37e0 6f 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73  of inode numbers
c37f0 20 61 73 20 75 6e 69 71 75 65 20 49 44 73 20 64   as unique IDs d
c3800 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20  oes not work on 
c3810 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20  VxWorks..** For 
c3820 56 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 65  VxWorks, we have
c3830 20 74 6f 20 75 73 65 20 74 68 65 20 61 6c 74 65   to use the alte
c3840 72 6e 61 74 69 76 65 20 75 6e 69 71 75 65 20 49  rnative unique I
c3850 44 20 73 79 73 74 65 6d 20 62 61 73 65 64 20 6f  D system based o
c3860 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66  n.** canonical f
c3870 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c  ilename and impl
c3880 65 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 70  emented in the p
c3890 72 65 76 69 6f 75 73 20 64 69 76 69 73 69 6f 6e  revious division
c38a0 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  .).**.** The sql
c38b0 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74  ite3_file struct
c38c0 75 72 65 20 66 6f 72 20 50 4f 53 49 58 20 69 73  ure for POSIX is
c38d0 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20   no longer just 
c38e0 61 6e 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a  an integer file.
c38f0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  ** descriptor.  
c3900 49 74 20 69 73 20 6e 6f 77 20 61 20 73 74 72 75  It is now a stru
c3910 63 74 75 72 65 20 74 68 61 74 20 68 6f 6c 64 73  cture that holds
c3920 20 74 68 65 20 69 6e 74 65 67 65 72 20 66 69 6c   the integer fil
c3930 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20  e.** descriptor 
c3940 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  and a pointer to
c3950 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61   a structure tha
c3960 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  t describes the 
c3970 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b  internal.** lock
c3980 73 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70  s on the corresp
c3990 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54  onding inode.  T
c39a0 68 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b  here is one lock
c39b0 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  ing structure.**
c39c0 20 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69   per inode, so i
c39d0 66 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65  f the same inode
c39e0 20 69 73 20 6f 70 65 6e 65 64 20 74 77 69 63 65   is opened twice
c39f0 2c 20 62 6f 74 68 20 75 6e 69 78 46 69 6c 65 20  , both unixFile 
c3a00 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f  structures.** po
c3a10 69 6e 74 20 74 6f 20 74 68 65 20 73 61 6d 65 20  int to the same 
c3a20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72  locking structur
c3a30 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20  e.  The locking 
c3a40 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 0a  structure keeps.
c3a50 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20 63  ** a reference c
c3a60 6f 75 6e 74 20 28 73 6f 20 77 65 20 77 69 6c 6c  ount (so we will
c3a70 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64 65   know when to de
c3a80 6c 65 74 65 20 69 74 29 20 61 6e 64 20 61 20 22  lete it) and a "
c3a90 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68  cnt".** field th
c3aa0 61 74 20 74 65 6c 6c 73 20 75 73 20 69 74 73 20  at tells us its 
c3ab0 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74  internal lock st
c3ac0 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65  atus.  cnt==0 me
c3ad0 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20  ans the.** file 
c3ae0 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e  is unlocked.  cn
c3af0 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65 20  t==-1 means the 
c3b00 66 69 6c 65 20 68 61 73 20 61 6e 20 65 78 63 6c  file has an excl
c3b10 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63  usive lock..** c
c3b20 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68 65 72 65  nt>0 means there
c3b30 20 61 72 65 20 63 6e 74 20 73 68 61 72 65 64 20   are cnt shared 
c3b40 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c  locks on the fil
c3b50 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74  e..**.** Any att
c3b60 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20  empt to lock or 
c3b70 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 69  unlock a file fi
c3b80 72 73 74 20 63 68 65 63 6b 73 20 74 68 65 20 6c  rst checks the l
c3b90 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74  ocking.** struct
c3ba0 75 72 65 2e 20 20 54 68 65 20 66 63 6e 74 6c 28  ure.  The fcntl(
c3bb0 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 73  ) system call is
c3bc0 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f   only invoked to
c3bd0 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58   set a .** POSIX
c3be0 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 6e 74   lock if the int
c3bf0 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63  ernal lock struc
c3c00 74 75 72 65 20 74 72 61 6e 73 69 74 69 6f 6e 73  ture transitions
c3c10 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f   between.** a lo
c3c20 63 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f  cked and an unlo
c3c30 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a  cked state..**.*
c3c40 2a 20 42 75 74 20 77 61 69 74 3a 20 20 74 68 65  * But wait:  the
c3c50 72 65 20 61 72 65 20 79 65 74 20 6d 6f 72 65 20  re are yet more 
c3c60 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 50 4f  problems with PO
c3c70 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63  SIX advisory loc
c3c80 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75  ks..**.** If you
c3c90 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65   close a file de
c3ca0 73 63 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f  scriptor that po
c3cb0 69 6e 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74  ints to a file t
c3cc0 68 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a  hat has locks,.*
c3cd0 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74  * all locks on t
c3ce0 68 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72  hat file that ar
c3cf0 65 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63  e owned by the c
c3d00 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61  urrent process a
c3d10 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20  re.** released. 
c3d20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20   To work around 
c3d30 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61  this problem, ea
c3d40 63 68 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75  ch unixFile stru
c3d50 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a  cture contains.*
c3d60 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
c3d70 6e 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74  n unixOpenCnt st
c3d80 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 20  ructure.  There 
c3d90 69 73 20 6f 6e 65 20 75 6e 69 78 4f 70 65 6e 43  is one unixOpenC
c3da0 6e 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  nt structure.** 
c3db0 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20  per open inode, 
c3dc0 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74  which means that
c3dd0 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69 78 46 69   multiple unixFi
c3de0 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20  le can point to 
c3df0 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 75 6e 69 78  a single.** unix
c3e00 4f 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61  OpenCnt.  When a
c3e10 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64  n attempt is mad
c3e20 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e  e to close an un
c3e30 69 78 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65  ixFile, if there
c3e40 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e   are.** other un
c3e50 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74  ixFile open on t
c3e60 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68  he same inode th
c3e70 61 74 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c  at are holding l
c3e80 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a  ocks, the call.*
c3e90 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65  * to close() the
c3ea0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
c3eb0 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74   is deferred unt
c3ec0 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f  il all of the lo
c3ed0 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68  cks clear..** Th
c3ee0 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74  e unixOpenCnt st
c3ef0 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 61 20  ructure keeps a 
c3f00 6c 69 73 74 20 6f 66 20 66 69 6c 65 20 64 65 73  list of file des
c3f10 63 72 69 70 74 6f 72 73 20 74 68 61 74 20 6e 65  criptors that ne
c3f20 65 64 20 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73  ed to.** be clos
c3f30 65 64 20 61 6e 64 20 74 68 61 74 20 6c 69 73 74  ed and that list
c3f40 20 69 73 20 77 61 6c 6b 65 64 20 28 61 6e 64 20   is walked (and 
c3f50 63 6c 65 61 72 65 64 29 20 77 68 65 6e 20 74 68  cleared) when th
c3f60 65 20 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63  e last lock.** c
c3f70 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 65 74  lears..**.** Yet
c3f80 20 61 6e 6f 74 68 65 72 20 70 72 6f 62 6c 65 6d   another problem
c3f90 3a 20 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20  :  LinuxThreads 
c3fa0 64 6f 20 6e 6f 74 20 70 6c 61 79 20 77 65 6c 6c  do not play well
c3fb0 20 77 69 74 68 20 70 6f 73 69 78 20 6c 6f 63 6b   with posix lock
c3fc0 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 6f 6c  s..**.** Many ol
c3fd0 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  der versions of 
c3fe0 6c 69 6e 75 78 20 75 73 65 20 74 68 65 20 4c 69  linux use the Li
c3ff0 6e 75 78 54 68 72 65 61 64 73 20 6c 69 62 72 61  nuxThreads libra
c4000 72 79 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6e  ry which is.** n
c4010 6f 74 20 70 6f 73 69 78 20 63 6f 6d 70 6c 69 61  ot posix complia
c4020 6e 74 2e 20 20 55 6e 64 65 72 20 4c 69 6e 75 78  nt.  Under Linux
c4030 54 68 72 65 61 64 73 2c 20 61 20 6c 6f 63 6b 20  Threads, a lock 
c4040 63 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61  created by threa
c4050 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f 74 20 62 65  d.** A cannot be
c4060 20 6d 6f 64 69 66 69 65 64 20 6f 72 20 6f 76 65   modified or ove
c4070 72 72 69 64 64 65 6e 20 62 79 20 61 20 64 69 66  rridden by a dif
c4080 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 42 2e  ferent thread B.
c4090 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72 65 61 64 20  .** Only thread 
c40a0 41 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 65  A can modify the
c40b0 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b 69 6e 67 20   lock.  Locking 
c40c0 62 65 68 61 76 69 6f 72 20 69 73 20 63 6f 72 72  behavior is corr
c40d0 65 63 74 0a 2a 2a 20 69 66 20 74 68 65 20 61 70  ect.** if the ap
c40e0 70 6c 69 61 74 69 6f 6e 20 75 73 65 73 20 74 68  pliation uses th
c40f0 65 20 6e 65 77 65 72 20 4e 61 74 69 76 65 20 50  e newer Native P
c4100 6f 73 69 78 20 54 68 72 65 61 64 20 4c 69 62 72  osix Thread Libr
c4110 61 72 79 20 28 4e 50 54 4c 29 0a 2a 2a 20 6f 6e  ary (NPTL).** on
c4120 20 6c 69 6e 75 78 20 2d 20 77 69 74 68 20 4e 50   linux - with NP
c4130 54 4c 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65  TL a lock create
c4140 64 20 62 79 20 74 68 72 65 61 64 20 41 20 63 61  d by thread A ca
c4150 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73  n override locks
c4160 0a 2a 2a 20 69 6e 20 74 68 72 65 61 64 20 42 2e  .** in thread B.
c4170 20 20 42 75 74 20 74 68 65 72 65 20 69 73 20 6e    But there is n
c4180 6f 20 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74  o way to know at
c4190 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68   compile-time wh
c41a0 69 63 68 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67  ich.** threading
c41b0 20 6c 69 62 72 61 72 79 20 69 73 20 62 65 69 6e   library is bein
c41c0 67 20 75 73 65 64 2e 20 20 53 6f 20 74 68 65 72  g used.  So ther
c41d0 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b  e is no way to k
c41e0 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c  now at.** compil
c41f0 65 2d 74 69 6d 65 20 77 68 65 74 68 65 72 20 6f  e-time whether o
c4200 72 20 6e 6f 74 20 74 68 72 65 61 64 20 41 20 63  r not thread A c
c4210 61 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b  an override lock
c4220 73 20 6f 6e 20 74 68 72 65 61 64 20 42 2e 0a 2a  s on thread B..*
c4230 2a 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20  * We have to do 
c4240 61 20 72 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b  a run-time check
c4250 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65   to discover the
c4260 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65   behavior of the
c4270 0a 2a 2a 20 63 75 72 72 65 6e 74 20 70 72 6f 63  .** current proc
c4280 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 79  ess..**.** On sy
c4290 73 74 65 6d 73 20 77 68 65 72 65 20 74 68 72 65  stems where thre
c42a0 61 64 20 41 20 69 73 20 75 6e 61 62 6c 65 20 74  ad A is unable t
c42b0 6f 20 6d 6f 64 69 66 79 20 6c 6f 63 6b 73 20 63  o modify locks c
c42c0 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 72  reated by.** thr
c42d0 65 61 64 20 42 2c 20 77 65 20 68 61 76 65 20 74  ead B, we have t
c42e0 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 20  o keep track of 
c42f0 77 68 69 63 68 20 74 68 72 65 61 64 20 63 72 65  which thread cre
c4300 61 74 65 64 20 65 61 63 68 0a 2a 2a 20 6c 6f 63  ated each.** loc
c4310 6b 2e 20 20 48 65 6e 63 65 20 74 68 65 72 65 20  k.  Hence there 
c4320 69 73 20 61 6e 20 65 78 74 72 61 20 66 69 65 6c  is an extra fiel
c4330 64 20 69 6e 20 74 68 65 20 6b 65 79 20 74 6f 20  d in the key to 
c4340 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  the unixLockInfo
c4350 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f  .** structure to
c4360 20 72 65 63 6f 72 64 20 74 68 69 73 20 69 6e 66   record this inf
c4370 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 6e 64 20 6f  ormation.  And o
c4380 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d 73 20  n those systems 
c4390 69 74 0a 2a 2a 20 69 73 20 69 6c 6c 65 67 61 6c  it.** is illegal
c43a0 20 74 6f 20 62 65 67 69 6e 20 61 20 74 72 61 6e   to begin a tran
c43b0 73 61 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 74  saction in one t
c43c0 68 72 65 61 64 20 61 6e 64 20 66 69 6e 69 73 68  hread and finish
c43d0 20 69 74 0a 2a 2a 20 69 6e 20 61 6e 6f 74 68 65   it.** in anothe
c43e0 72 2e 20 20 46 6f 72 20 74 68 69 73 20 6c 61 74  r.  For this lat
c43f0 74 65 72 20 72 65 73 74 72 69 63 74 69 6f 6e 2c  ter restriction,
c4400 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72   there is no wor
c4410 6b 2d 61 72 6f 75 6e 64 2e 0a 2a 2a 20 49 74 20  k-around..** It 
c4420 69 73 20 61 20 6c 69 6d 69 74 61 74 69 6f 6e 20  is a limitation 
c4430 6f 66 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e  of LinuxThreads.
c4440 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f  .*/../*.** Set o
c4450 72 20 63 68 65 63 6b 20 74 68 65 20 75 6e 69 78  r check the unix
c4460 46 69 6c 65 2e 74 69 64 20 66 69 65 6c 64 2e 20  File.tid field. 
c4470 20 54 68 69 73 20 66 69 65 6c 64 20 69 73 20 73   This field is s
c4480 65 74 20 77 68 65 6e 20 61 6e 20 75 6e 69 78 46  et when an unixF
c4490 69 6c 65 0a 2a 2a 20 69 73 20 66 69 72 73 74 20  ile.** is first 
c44a0 6f 70 65 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62  opened.  All sub
c44b0 73 65 71 75 65 6e 74 20 75 73 65 73 20 6f 66 20  sequent uses of 
c44c0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 76 65 72  the unixFile ver
c44d0 69 66 79 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ify that the.** 
c44e0 73 61 6d 65 20 74 68 72 65 61 64 20 69 73 20 6f  same thread is o
c44f0 70 65 72 61 74 69 6e 67 20 6f 6e 20 74 68 65 20  perating on the 
c4500 75 6e 69 78 46 69 6c 65 2e 20 20 53 6f 6d 65 20  unixFile.  Some 
c4510 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
c4520 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61 6c 6c 6f  s do.** not allo
c4530 77 20 6c 6f 63 6b 73 20 74 6f 20 62 65 20 6f 76  w locks to be ov
c4540 65 72 72 69 64 64 65 6e 20 62 79 20 6f 74 68 65  erridden by othe
c4550 72 20 74 68 72 65 61 64 73 20 61 6e 64 20 74 68  r threads and th
c4560 61 74 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a  at restriction.*
c4570 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 73 71 6c  * means that sql
c4580 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68  ite3* database h
c4590 61 6e 64 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65  andles cannot be
c45a0 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20   moved from one 
c45b0 74 68 72 65 61 64 0a 2a 2a 20 74 6f 20 61 6e 6f  thread.** to ano
c45c0 74 68 65 72 20 77 68 69 6c 65 20 6c 6f 63 6b 73  ther while locks
c45d0 20 61 72 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a   are held..**.**
c45e0 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e 31 20 28   Version 3.3.1 (
c45f0 32 30 30 36 2d 30 31 2d 31 35 29 3a 20 20 75 6e  2006-01-15):  un
c4600 69 78 46 69 6c 65 20 63 61 6e 20 62 65 20 6d 6f  ixFile can be mo
c4610 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72  ved from one thr
c4620 65 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65  ead to.** anothe
c4630 72 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20  r as long as we 
c4640 61 72 65 20 72 75 6e 6e 69 6e 67 20 6f 6e 20 61  are running on a
c4650 20 73 79 73 74 65 6d 20 74 68 61 74 20 73 75 70   system that sup
c4660 70 6f 72 74 73 20 74 68 72 65 61 64 73 0a 2a 2a  ports threads.**
c4670 20 6f 76 65 72 72 69 64 69 6e 67 20 65 61 63 68   overriding each
c4680 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 77   others locks (w
c4690 68 69 63 68 20 69 73 20 6e 6f 77 20 74 68 65 20  hich is now the 
c46a0 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61  most common beha
c46b0 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e  vior).** or if n
c46c0 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64  o locks are held
c46d0 2e 20 20 42 75 74 20 74 68 65 20 75 6e 69 78 46  .  But the unixF
c46e0 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20  ile.pLock field 
c46f0 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72  needs to be.** r
c4700 65 63 6f 6d 70 75 74 65 64 20 62 65 63 61 75 73  ecomputed becaus
c4710 65 20 69 74 73 20 6b 65 79 20 69 6e 63 6c 75 64  e its key includ
c4720 65 73 20 74 68 65 20 74 68 72 65 61 64 2d 69 64  es the thread-id
c4730 2e 20 20 53 65 65 20 74 68 65 20 0a 2a 2a 20 74  .  See the .** t
c4740 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70  ransferOwnership
c4750 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f  () function belo
c4760 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  w for additional
c4770 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a   information.*/.
c4780 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
c4790 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64  DSAFE && defined
c47a0 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65  (__linux__).# de
c47b0 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49  fine SET_THREADI
c47c0 44 28 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20  D(X)   (X)->tid 
c47d0 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  = pthread_self()
c47e0 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  .# define CHECK_
c47f0 54 48 52 45 41 44 49 44 28 58 29 20 28 74 68 72  THREADID(X) (thr
c4800 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
c4810 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26  OthersLocks==0 &
c4820 26 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  & \.            
c4830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4840 21 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28  !pthread_equal((
c4850 58 29 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64  X)->tid, pthread
c4860 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a  _self())).#else.
c4870 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52  # define SET_THR
c4880 45 41 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e  EADID(X).# defin
c4890 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44  e CHECK_THREADID
c48a0 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  (X) 0.#endif../*
c48b0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
c48c0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
c48d0 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65   structure serve
c48e0 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65  s as the key use
c48f0 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61  d.** to locate a
c4900 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78   particular unix
c4910 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72  OpenCnt structur
c4920 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64  e given its inod
c4930 65 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 74  e.  This.** is t
c4940 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 75  he same as the u
c4950 6e 69 78 4c 6f 63 6b 4b 65 79 20 65 78 63 65 70  nixLockKey excep
c4960 74 20 74 68 61 74 20 74 68 65 20 74 68 72 65 61  t that the threa
c4970 64 20 49 44 20 69 73 20 6f 6d 69 74 74 65 64 2e  d ID is omitted.
c4980 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 46  .*/.struct unixF
c4990 69 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f 74 20  ileId {.  dev_t 
c49a0 64 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  dev;            
c49b0 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20        /* Device 
c49c0 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53  number */.#if OS
c49d0 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63  _VXWORKS.  struc
c49e0 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
c49f0 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65  *pId;  /* Unique
c4a00 20 66 69 6c 65 20 49 44 20 66 6f 72 20 76 78 77   file ID for vxw
c4a10 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20  orks. */.#else. 
c4a20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20   ino_t ino;     
c4a30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c4a40 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  Inode number */.
c4a50 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
c4a60 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
c4a70 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
c4a80 72 75 63 74 75 72 65 20 73 65 72 76 65 73 20 61  ructure serves a
c4a90 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a  s the key used.*
c4aa0 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61  * to locate a pa
c4ab0 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4c 6f 63  rticular unixLoc
c4ac0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  kInfo structure 
c4ad0 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e  given its inode.
c4ae0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 72 65 61 64  .**.** If thread
c4af0 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64  s cannot overrid
c4b00 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  e each others lo
c4b10 63 6b 73 20 28 4c 69 6e 75 78 54 68 72 65 61 64  cks (LinuxThread
c4b20 73 29 2c 20 74 68 65 6e 20 77 65 0a 2a 2a 20 73  s), then we.** s
c4b30 65 74 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b  et the unixLockK
c4b40 65 79 2e 74 69 64 20 66 69 65 6c 64 20 74 6f 20  ey.tid field to 
c4b50 74 68 65 20 74 68 72 65 61 64 20 49 44 2e 20 20  the thread ID.  
c4b60 49 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f  If threads can o
c4b70 76 65 72 72 69 64 65 0a 2a 2a 20 65 61 63 68 20  verride.** each 
c4b80 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 50 6f  others locks (Po
c4b90 73 69 78 20 61 6e 64 20 4e 50 54 4c 29 20 74 68  six and NPTL) th
c4ba0 65 6e 20 74 69 64 20 69 73 20 61 6c 77 61 79 73  en tid is always
c4bb0 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a   set to zero..**
c4bc0 20 74 69 64 20 69 73 20 6f 6d 69 74 74 65 64 20   tid is omitted 
c4bd0 69 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  if we compile wi
c4be0 74 68 6f 75 74 20 74 68 72 65 61 64 69 6e 67 20  thout threading 
c4bf0 73 75 70 70 6f 72 74 20 6f 72 20 6f 6e 20 61 6e  support or on an
c4c00 20 4f 53 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61   OS.** other tha
c4c10 6e 20 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 74 72 75  n linux..*/.stru
c4c20 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 7b  ct unixLockKey {
c4c30 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69  .  struct unixFi
c4c40 6c 65 49 64 20 66 69 64 3b 20 20 2f 2a 20 55 6e  leId fid;  /* Un
c4c50 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20  ique identifier 
c4c60 66 6f 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  for the file */.
c4c70 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
c4c80 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64  DSAFE && defined
c4c90 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74  (__linux__).  pt
c4ca0 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 2f 2a  hread_t tid;  /*
c4cb0 20 54 68 72 65 61 64 20 49 44 20 6f 66 20 6c 6f   Thread ID of lo
c4cc0 63 6b 20 6f 77 6e 65 72 2e 20 5a 65 72 6f 20 69  ck owner. Zero i
c4cd0 66 20 6e 6f 74 20 75 73 69 6e 67 20 4c 69 6e 75  f not using Linu
c4ce0 78 54 68 72 65 61 64 73 20 2a 2f 0a 23 65 6e 64  xThreads */.#end
c4cf0 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  if.};../*.** An 
c4d00 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
c4d10 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
c4d20 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ure is allocated
c4d30 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a   for each open.*
c4d40 2a 20 69 6e 6f 64 65 2e 20 20 4f 72 2c 20 6f 6e  * inode.  Or, on
c4d50 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 74   LinuxThreads, t
c4d60 68 65 72 65 20 69 73 20 6f 6e 65 20 6f 66 20 74  here is one of t
c4d70 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 20  hese structures 
c4d80 66 6f 72 0a 2a 2a 20 65 61 63 68 20 69 6e 6f 64  for.** each inod
c4d90 65 20 6f 70 65 6e 65 64 20 62 79 20 65 61 63 68  e opened by each
c4da0 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41   thread..**.** A
c4db0 20 73 69 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61   single inode ca
c4dc0 6e 20 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20  n have multiple 
c4dd0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
c4de0 2c 20 73 6f 20 65 61 63 68 20 75 6e 69 78 46 69  , so each unixFi
c4df0 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  le.** structure 
c4e00 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74  contains a point
c4e10 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63  er to an instanc
c4e20 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
c4e30 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a   and this.** obj
c4e40 65 63 74 20 6b 65 65 70 73 20 61 20 63 6f 75 6e  ect keeps a coun
c4e50 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  t of the number 
c4e60 6f 66 20 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e  of unixFile poin
c4e70 74 69 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73  ting to it..*/.s
c4e80 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e  truct unixLockIn
c4e90 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e  fo {.  struct un
c4ea0 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65  ixLockKey lockKe
c4eb0 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f  y;     /* The lo
c4ec0 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  okup key */.  in
c4ed0 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20  t cnt;          
c4ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c4ef0 20 4e 75 6d 62 65 72 20 6f 66 20 53 48 41 52 45   Number of SHARE
c4f00 44 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a  D locks held */.
c4f10 20 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20    int locktype; 
c4f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4f30 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52    /* One of SHAR
c4f40 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45  ED_LOCK, RESERVE
c4f50 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20  D_LOCK etc. */. 
c4f60 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
c4f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4f80 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f   /* Number of po
c4f90 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73  inters to this s
c4fa0 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74  tructure */.  st
c4fb0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66  ruct unixLockInf
c4fc0 6f 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a  o *pNext;     /*
c4fd0 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69   List of all uni
c4fe0 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74  xLockInfo object
c4ff0 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e  s */.  struct un
c5000 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 50 72 65  ixLockInfo *pPre
c5010 76 3b 20 20 20 20 20 2f 2a 20 20 20 20 2e 2e 2e  v;     /*    ...
c5020 2e 20 64 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20  . doubly linked 
c5030 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
c5040 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
c5050 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
c5060 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ure is allocated
c5070 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a   for each open.*
c5080 2a 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73  * inode.  This s
c5090 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74  tructure keeps t
c50a0 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62  rack of the numb
c50b0 65 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74  er of locks on t
c50c0 68 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49  hat.** inode.  I
c50d0 66 20 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74  f a close is att
c50e0 65 6d 70 74 65 64 20 61 67 61 69 6e 73 74 20 61  empted against a
c50f0 6e 20 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20  n inode that is 
c5100 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73  holding.** locks
c5110 2c 20 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64  , the close is d
c5120 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c  eferred until al
c5130 6c 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79  l locks clear by
c5140 20 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66   adding the.** f
c5150 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
c5160 6f 20 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74  o be closed to t
c5170 68 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e  he pending list.
c5180 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 20 43 6f  .**.** TODO:  Co
c5190 6e 73 69 64 65 72 20 63 68 61 6e 67 69 6e 67 20  nsider changing 
c51a0 74 68 69 73 20 73 6f 20 74 68 61 74 20 74 68 65  this so that the
c51b0 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e  re is only a sin
c51c0 67 6c 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63  gle file.** desc
c51d0 72 69 70 74 6f 72 20 66 6f 72 20 65 61 63 68 20  riptor for each 
c51e0 6f 70 65 6e 20 66 69 6c 65 2c 20 65 76 65 6e 20  open file, even 
c51f0 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65  when it is opene
c5200 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
c5210 2e 0a 2a 2a 20 54 68 65 20 63 6c 6f 73 65 28 29  ..** The close()
c5220 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 77 6f 75   system call wou
c5230 6c 64 20 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68  ld only occur wh
c5240 65 6e 20 74 68 65 20 6c 61 73 74 20 64 61 74 61  en the last data
c5250 62 61 73 65 0a 2a 2a 20 75 73 69 6e 67 20 74 68  base.** using th
c5260 65 20 66 69 6c 65 20 63 6c 6f 73 65 73 2e 0a 2a  e file closes..*
c5270 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65  /.struct unixOpe
c5280 6e 43 6e 74 20 7b 0a 20 20 73 74 72 75 63 74 20  nCnt {.  struct 
c5290 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49  unixFileId fileI
c52a0 64 3b 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b  d;   /* The look
c52b0 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  up key */.  int 
c52c0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
c52d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
c52e0 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  r of pointers to
c52f0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
c5300 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20  */.  int nLock; 
c5310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5320 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75   /* Number of ou
c5330 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20  tstanding locks 
c5340 2a 2f 0a 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e  */.  int nPendin
c5350 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
c5360 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 65   /* Number of pe
c5370 6e 64 69 6e 67 20 63 6c 6f 73 65 28 29 20 6f 70  nding close() op
c5380 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  erations */.  in
c5390 74 20 2a 61 50 65 6e 64 69 6e 67 3b 20 20 20 20  t *aPending;    
c53a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f          /* Mallo
c53b0 63 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69 6e  ced space holdin
c53c0 67 20 66 64 27 73 20 61 77 61 69 74 69 6e 67 20  g fd's awaiting 
c53d0 61 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 23 69 66  a close() */.#if
c53e0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65   OS_VXWORKS.  se
c53f0 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20  m_t *pSem;      
c5400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c5410 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65  * Named POSIX se
c5420 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61  maphore */.  cha
c5430 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50  r aSemName[MAX_P
c5440 41 54 48 4e 41 4d 45 2b 31 5d 3b 20 20 20 2f 2a  ATHNAME+1];   /*
c5450 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65   Name of that se
c5460 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69  maphore */.#endi
c5470 66 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f  f.  struct unixO
c5480 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a  penCnt *pNext, *
c5490 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74  pPrev;   /* List
c54a0 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e   of all unixOpen
c54b0 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d  Cnt objects */.}
c54c0 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f  ;../*.** Lists o
c54d0 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e  f all unixLockIn
c54e0 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43  fo and unixOpenC
c54f0 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65  nt objects.  The
c5500 73 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 61  se used to be ha
c5510 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42  sh.** tables.  B
c5520 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ut the number of
c5530 20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 65   objects is rare
c5540 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64  ly more than a d
c5550 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65  ozen and.** neve
c5560 72 20 65 78 63 65 65 64 73 20 61 20 66 65 77 20  r exceeds a few 
c5570 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c  thousand.  And l
c5580 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20  ookup is not on 
c5590 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61  a critical.** pa
c55a0 74 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c  th so a simple l
c55b0 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c 20  inked list will 
c55c0 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74  suffice..*/.stat
c55d0 69 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  ic struct unixLo
c55e0 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74  ckInfo *lockList
c55f0 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72   = 0;.static str
c5600 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  uct unixOpenCnt 
c5610 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a  *openList = 0;..
c5620 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61  /*.** This varia
c5630 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 68  ble remembers wh
c5640 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72  ether or not thr
c5650 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64  eads can overrid
c5660 65 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a  e each others.**
c5670 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20   locks..**.**   
c5680 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64   0:  No.  Thread
c5690 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64  s cannot overrid
c56a0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  e each others lo
c56b0 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 65  cks.  (LinuxThre
c56c0 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59  ads).**    1:  Y
c56d0 65 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e  es.  Threads can
c56e0 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f   override each o
c56f0 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50  thers locks.  (P
c5700 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20  osix & NLPT).** 
c5710 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20    -1:  We don't 
c5720 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20  know yet..**.** 
c5730 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c  On some systems,
c5740 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70   we know at comp
c5750 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65  ile-time if thre
c5760 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65  ads can override
c5770 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20   each.** others 
c5780 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65  locks.  On those
c5790 20 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51   systems, the SQ
c57a0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52  LITE_THREAD_OVER
c57b0 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a  RIDE_LOCK macro.
c57c0 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61  ** will be set a
c57d0 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f  ppropriately.  O
c57e0 6e 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c  n other systems,
c57f0 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63   we have to chec
c5800 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e  k at.** runtime.
c5810 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65    On these latte
c5820 72 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49  r systems, SQLTI
c5830 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44  E_THREAD_OVERRID
c5840 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64  E_LOCK is.** und
c5850 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  efined..**.** Th
c5860 69 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d  is variable norm
c5870 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63  ally has file sc
c5880 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64  ope only.  But d
c5890 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77  uring testing, w
c58a0 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67  e make.** it a g
c58b0 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68  lobal so that th
c58c0 65 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20  e test code can 
c58d0 63 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65  change its value
c58e0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72   in order to ver
c58f0 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ify.** that the 
c5900 72 69 67 68 74 20 73 74 75 66 66 20 68 61 70 70  right stuff happ
c5910 65 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 61  ens in either ca
c5920 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  se..*/.#if SQLIT
c5930 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
c5940 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
c5950 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 4c  _).#  ifndef SQL
c5960 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52  ITE_THREAD_OVERR
c5970 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65  IDE_LOCK.#    de
c5980 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45  fine SQLITE_THRE
c5990 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b  AD_OVERRIDE_LOCK
c59a0 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 20   -1.#  endif.#  
c59b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
c59c0 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65  T.int threadsOve
c59d0 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
c59e0 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48  ocks = SQLITE_TH
c59f0 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
c5a00 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 74  CK;.#  else.stat
c5a10 69 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76  ic int threadsOv
c5a20 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
c5a30 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54  Locks = SQLITE_T
c5a40 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c  HREAD_OVERRIDE_L
c5a50 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65  OCK;.#  endif.#e
c5a60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
c5a70 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
c5a80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73   information pas
c5a90 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64  sed into individ
c5aa0 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65  ual test.** thre
c5ab0 61 64 73 20 62 79 20 74 68 65 20 74 65 73 74 54  ads by the testT
c5ac0 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61  hreadLockingBeha
c5ad0 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a  vior() routine..
c5ae0 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 64  */.struct thread
c5af0 54 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74  TestData {.  int
c5b00 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20   fd;            
c5b10 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62      /* File to b
c5b20 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74  e locked */.  st
c5b30 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b  ruct flock lock;
c5b40 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b       /* The lock
c5b50 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  ing operation */
c5b60 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20  .  int result;  
c5b70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
c5b80 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69  ult of the locki
c5b90 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ng operation */.
c5ba0 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  };..#if SQLITE_T
c5bb0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
c5bc0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
c5bd0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
c5be0 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74  ion is used as t
c5bf0 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20  he main routine 
c5c00 66 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 75  for a thread lau
c5c10 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 74  nched by.** test
c5c20 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
c5c30 61 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 74  avior(). It test
c5c40 73 20 77 68 65 74 68 65 72 20 74 68 65 20 73 68  s whether the sh
c5c50 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e  ared-lock obtain
c5c60 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 69  ed.** by the mai
c5c70 6e 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 74  n thread in test
c5c80 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
c5c90 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74  avior() conflict
c5ca0 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f  s with a.** hypo
c5cb0 74 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d 6c  thetical write-l
c5cc0 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 20  ock obtained by 
c5cd0 74 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 74  this thread on t
c5ce0 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a  he same file..**
c5cf0 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c 6f  .** The write-lo
c5d00 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c  ck is not actual
c5d10 6c 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 20  ly acquired, as 
c5d20 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73  this is not poss
c5d30 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 20  ible if .** the 
c5d40 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e 20  file is open in 
c5d50 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28  read-only mode (
c5d60 73 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 32  see ticket #3472
c5d70 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f  )..*/ .static vo
c5d80 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e  id *threadLockin
c5d90 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67  gTest(void *pArg
c5da0 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65  ){.  struct thre
c5db0 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74  adTestData *pDat
c5dc0 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65  a = (struct thre
c5dd0 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67  adTestData*)pArg
c5de0 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c  ;.  pData->resul
c5df0 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d  t = fcntl(pData-
c5e00 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70  >fd, F_GETLK, &p
c5e10 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72  Data->lock);.  r
c5e20 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65  eturn pArg;.}.#e
c5e30 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54  ndif /* SQLITE_T
c5e40 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
c5e50 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20  ined(__linux__) 
c5e60 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  */...#if SQLITE_
c5e70 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
c5e80 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
c5e90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63  ./*.** This proc
c5ea0 65 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74  edure attempts t
c5eb0 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74  o determine whet
c5ec0 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61  her or not threa
c5ed0 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69  ds.** can overri
c5ee0 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c  de each others l
c5ef0 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74  ocks then sets t
c5f00 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76  he .** threadsOv
c5f10 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
c5f20 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61  Locks variable a
c5f30 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f  ppropriately..*/
c5f40 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73  .static void tes
c5f50 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65  tThreadLockingBe
c5f60 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72  havior(int fd_or
c5f70 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20  ig){.  int fd;. 
c5f80 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63   int rc;.  struc
c5f90 74 20 74 68 72 65 61 64 54 65 73 74 44 61 74 61  t threadTestData
c5fa0 20 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f   d;.  struct flo
c5fb0 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f  ck l;.  pthread_
c5fc0 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70  t t;..  fd = dup
c5fd0 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28  (fd_orig);.  if(
c5fe0 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a   fd<0 ) return;.
c5ff0 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20    memset(&l, 0, 
c6000 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e  sizeof(l));.  l.
c6010 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b  l_type = F_RDLCK
c6020 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b  ;.  l.l_len = 1;
c6030 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30  .  l.l_start = 0
c6040 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d  ;.  l.l_whence =
c6050 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20   SEEK_SET;.  rc 
c6060 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c  = fcntl(fd_orig,
c6070 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20   F_SETLK, &l);. 
c6080 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74   if( rc!=0 ) ret
c6090 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64  urn;.  memset(&d
c60a0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b  , 0, sizeof(d));
c60b0 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20  .  d.fd = fd;.  
c60c0 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e  d.lock = l;.  d.
c60d0 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  lock.l_type = F_
c60e0 57 52 4c 43 4b 3b 0a 20 20 70 74 68 72 65 61 64  WRLCK;.  pthread
c60f0 5f 63 72 65 61 74 65 28 26 74 2c 20 30 2c 20 74  _create(&t, 0, t
c6100 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74  hreadLockingTest
c6110 2c 20 26 64 29 3b 0a 20 20 70 74 68 72 65 61 64  , &d);.  pthread
c6120 5f 6a 6f 69 6e 28 74 2c 20 30 29 3b 0a 20 20 63  _join(t, 0);.  c
c6130 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20  lose(fd);.  if( 
c6140 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65  d.result!=0 ) re
c6150 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f  turn;.  threadsO
c6160 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
c6170 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b  sLocks = (d.lock
c6180 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b  .l_type==F_UNLCK
c6190 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
c61a0 51 4c 49 54 45 5f 54 48 45 52 41 44 53 41 46 45  QLITE_THERADSAFE
c61b0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
c61c0 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  nux__) */../*.**
c61d0 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c   Release a unixL
c61e0 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72  ockInfo structur
c61f0 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  e previously all
c6200 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f  ocated by findLo
c6210 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a 73 74 61  ckInfo()..*/.sta
c6220 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65  tic void release
c6230 4c 6f 63 6b 49 6e 66 6f 28 73 74 72 75 63 74 20  LockInfo(struct 
c6240 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c  unixLockInfo *pL
c6250 6f 63 6b 29 7b 0a 20 20 69 66 28 20 70 4c 6f 63  ock){.  if( pLoc
c6260 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  k ){.    pLock->
c6270 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20  nRef--;.    if( 
c6280 70 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29  pLock->nRef==0 )
c6290 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63  {.      if( pLoc
c62a0 6b 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20  k->pPrev ){.    
c62b0 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
c62c0 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d  k->pPrev->pNext=
c62d0 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20  =pLock );.      
c62e0 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e    pLock->pPrev->
c62f0 70 4e 65 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70  pNext = pLock->p
c6300 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Next;.      }els
c6310 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e{.        asser
c6320 74 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f  t( lockList==pLo
c6330 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f  ck );.        lo
c6340 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e  ckList = pLock->
c6350 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
c6360 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e       if( pLock->
c6370 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20  pNext ){.       
c6380 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
c6390 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c  pNext->pPrev==pL
c63a0 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70  ock );.        p
c63b0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72  Lock->pNext->pPr
c63c0 65 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65  ev = pLock->pPre
c63d0 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  v;.      }.     
c63e0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c   sqlite3_free(pL
c63f0 6f 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ock);.    }.  }.
c6400 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  }../*.** Release
c6410 20 61 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73   a unixOpenCnt s
c6420 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75  tructure previou
c6430 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  sly allocated by
c6440 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e   findLockInfo().
c6450 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
c6460 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73  releaseOpenCnt(s
c6470 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
c6480 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 69 66 28  t *pOpen){.  if(
c6490 20 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f   pOpen ){.    pO
c64a0 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20  pen->nRef--;.   
c64b0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66   if( pOpen->nRef
c64c0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
c64d0 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b   pOpen->pPrev ){
c64e0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
c64f0 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70   pOpen->pPrev->p
c6500 4e 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20  Next==pOpen );. 
c6510 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50         pOpen->pP
c6520 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70  rev->pNext = pOp
c6530 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  en->pNext;.     
c6540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
c6550 61 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74  assert( openList
c6560 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20  ==pOpen );.     
c6570 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f     openList = pO
c6580 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pen->pNext;.    
c6590 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f    }.      if( pO
c65a0 70 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  pen->pNext ){.  
c65b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
c65c0 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  pen->pNext->pPre
c65d0 76 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20  v==pOpen );.    
c65e0 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74      pOpen->pNext
c65f0 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d  ->pPrev = pOpen-
c6600 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a  >pPrev;.      }.
c6610 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
c6620 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69  ee(pOpen->aPendi
c6630 6e 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ng);.      sqlit
c6640 65 33 5f 66 72 65 65 28 70 4f 70 65 6e 29 3b 0a  e3_free(pOpen);.
c6650 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
c6660 2a 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65 20  ** Given a file 
c6670 64 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61  descriptor, loca
c6680 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  te unixLockInfo 
c6690 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  and unixOpenCnt 
c66a0 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 0a  structures that.
c66b0 2a 2a 20 64 65 73 63 72 69 62 65 73 20 74 68 61  ** describes tha
c66c0 74 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  t file descripto
c66d0 72 2e 20 20 43 72 65 61 74 65 20 6e 65 77 20 6f  r.  Create new o
c66e0 6e 65 73 20 69 66 20 6e 65 63 65 73 73 61 72 79  nes if necessary
c66f0 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e  .  The.** return
c6700 20 76 61 6c 75 65 73 20 6d 69 67 68 74 20 62 65   values might be
c6710 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69   uninitialized i
c6720 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
c6730 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  s..**.** Return 
c6740 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65  an appropriate e
c6750 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
c6760 61 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63  atic int findLoc
c6770 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78 46 69 6c  kInfo(.  unixFil
c6780 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20  e *pFile,       
c6790 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 78 20          /* Unix 
c67a0 66 69 6c 65 20 77 69 74 68 20 66 69 6c 65 20 64  file with file d
c67b0 65 73 63 20 75 73 65 64 20 69 6e 20 74 68 65 20  esc used in the 
c67c0 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  key */.  struct 
c67d0 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a 70  unixLockInfo **p
c67e0 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 74 75 72  pLock,  /* Retur
c67f0 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e  n the unixLockIn
c6800 66 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 72  fo structure her
c6810 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e  e */.  struct un
c6820 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 70 4f 70  ixOpenCnt **ppOp
c6830 65 6e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20  en    /* Return 
c6840 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  the unixOpenCnt 
c6850 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a  structure here *
c6860 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  /.){.  int rc;  
c6870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6880 20 20 20 20 20 20 2f 2a 20 53 79 73 74 65 6d 20        /* System 
c6890 63 61 6c 6c 20 72 65 74 75 72 6e 20 63 6f 64 65  call return code
c68a0 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20   */.  int fd;   
c68b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c68c0 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65       /* The file
c68d0 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20   descriptor for 
c68e0 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63  pFile */.  struc
c68f0 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f  t unixLockKey lo
c6900 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 4c 6f 6f  ckKey;    /* Loo
c6910 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20  kup key for the 
c6920 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72  unixLockInfo str
c6930 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75  ucture */.  stru
c6940 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69  ct unixFileId fi
c6950 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  leId;      /* Lo
c6960 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65  okup key for the
c6970 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72   unixOpenCnt str
c6980 75 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  uct */.  struct 
c6990 73 74 61 74 20 73 74 61 74 62 75 66 3b 20 20 20  stat statbuf;   
c69a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c          /* Low-l
c69b0 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d  evel file inform
c69c0 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63  ation */.  struc
c69d0 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a  t unixLockInfo *
c69e0 70 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 43 61 6e  pLock;    /* Can
c69f0 64 69 64 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49  didate unixLockI
c6a00 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  nfo object */.  
c6a10 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
c6a20 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 2f  nt *pOpen;     /
c6a30 2a 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78  * Candidate unix
c6a40 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 20 2a  OpenCnt object *
c6a50 2f 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d  /..  /* Get low-
c6a60 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  level informatio
c6a70 6e 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65  n about the file
c6a80 20 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 65   that we can use
c6a90 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65  d to.  ** create
c6aa0 20 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66   a unique name f
c6ab0 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a  or the file..  *
c6ac0 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e  /.  fd = pFile->
c6ad0 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28  h;.  rc = fstat(
c6ae0 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20  fd, &statbuf);. 
c6af0 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20   if( rc!=0 ){.  
c6b00 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
c6b10 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64  no = errno;.#ifd
c6b20 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20  ef EOVERFLOW.   
c6b30 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74   if( pFile->last
c6b40 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57  Errno==EOVERFLOW
c6b50 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
c6b60 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20  _NOLFS;.#endif. 
c6b70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
c6b80 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66  _IOERR;.  }..#if
c6b90 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20  def __APPLE__.  
c6ba0 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e  /* On OS X on an
c6bb0 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65   msdos filesyste
c6bc0 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d  m, the inode num
c6bd0 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 0a  ber is reported.
c6be0 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79    ** incorrectly
c6bf0 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66   for zero-size f
c6c00 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b 65  iles.  See ticke
c6c10 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f 72  t #3260.  To wor
c6c20 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68  k.  ** around th
c6c30 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 63  is problem (we c
c6c40 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 67  onsider it a bug
c6c50 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51   in OS X, not SQ
c6c60 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c  Lite).  ** we al
c6c70 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 68  ways increase th
c6c80 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 31  e file size to 1
c6c90 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 69   by writing a si
c6ca0 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 70  ngle byte.  ** p
c6cb0 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 6e  rior to accessin
c6cc0 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62  g the inode numb
c6cd0 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 74  er.  The one byt
c6ce0 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 2a  e written is.  *
c6cf0 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 63  * an ASCII 'S' c
c6d00 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20 61  haracter which a
c6d10 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 62  lso happens to b
c6d20 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  e the first byte
c6d30 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 61  .  ** in the hea
c6d40 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 4c  der of every SQL
c6d50 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 49  ite database.  I
c6d60 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74  n this way, if t
c6d70 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 72  here.  ** is a r
c6d80 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75  ace condition su
c6d90 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20  ch that another 
c6da0 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 61  thread has alrea
c6db0 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a  dy populated.  *
c6dc0 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  * the first page
c6dd0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
c6de0 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64  , no damage is d
c6df0 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  one..  */.  if( 
c6e00 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d  statbuf.st_size=
c6e10 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 77  =0 ){.    rc = w
c6e20 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 29  rite(fd, "S", 1)
c6e30 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 20  ;.    if( rc!=1 
c6e40 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
c6e50 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20  SQLITE_IOERR;.  
c6e60 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73 74    }.    rc = fst
c6e70 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29  at(fd, &statbuf)
c6e80 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 20  ;.    if( rc!=0 
c6e90 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
c6ea0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
c6eb0 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  o;.      return 
c6ec0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20  SQLITE_IOERR;.  
c6ed0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
c6ee0 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65    memset(&lockKe
c6ef0 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63  y, 0, sizeof(loc
c6f00 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65  kKey));.  lockKe
c6f10 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61 74  y.fid.dev = stat
c6f20 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20  buf.st_dev;.#if 
c6f30 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63  OS_VXWORKS.  loc
c6f40 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 70  kKey.fid.pId = p
c6f50 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65  File->pId;.#else
c6f60 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69  .  lockKey.fid.i
c6f70 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f  no = statbuf.st_
c6f80 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  ino;.#endif.#if 
c6f90 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
c6fa0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c  E && defined(__l
c6fb0 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 68  inux__).  if( th
c6fc0 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63  readsOverrideEac
c6fd0 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29  hOthersLocks<0 )
c6fe0 7b 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 64  {.    testThread
c6ff0 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28  LockingBehavior(
c7000 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b  fd);.  }.  lockK
c7010 65 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64 73  ey.tid = threads
c7020 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
c7030 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74  rsLocks ? 0 : pt
c7040 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65  hread_self();.#e
c7050 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d 20  ndif.  fileId = 
c7060 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69  lockKey.fid;.  i
c7070 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a  f( ppLock!=0 ){.
c7080 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b      pLock = lock
c7090 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28  List;.    while(
c70a0 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70   pLock && memcmp
c70b0 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63  (&lockKey, &pLoc
c70c0 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65  k->lockKey, size
c70d0 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a  of(lockKey)) ){.
c70e0 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c        pLock = pL
c70f0 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ock->pNext;.    
c7100 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d  }.    if( pLock=
c7110 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63  =0 ){.      pLoc
c7120 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  k = sqlite3_mall
c7130 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63  oc( sizeof(*pLoc
c7140 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  k) );.      if( 
c7150 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20  pLock==0 ){.    
c7160 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
c7170 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67  NOMEM;.        g
c7180 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63  oto exit_findloc
c7190 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  kinfo;.      }. 
c71a0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b       pLock->lock
c71b0 4b 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a 20  Key = lockKey;. 
c71c0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66       pLock->nRef
c71d0 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f 63   = 1;.      pLoc
c71e0 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20  k->cnt = 0;.    
c71f0 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
c7200 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f  e = 0;.      pLo
c7210 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b  ck->pNext = lock
c7220 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f 63  List;.      pLoc
c7230 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20  k->pPrev = 0;.  
c7240 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 74      if( lockList
c7250 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 72   ) lockList->pPr
c7260 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20  ev = pLock;.    
c7270 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f    lockList = pLo
c7280 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ck;.    }else{. 
c7290 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66       pLock->nRef
c72a0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  ++;.    }.    *p
c72b0 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20  pLock = pLock;. 
c72c0 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e 21   }.  if( ppOpen!
c72d0 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 20  =0 ){.    pOpen 
c72e0 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20  = openList;.    
c72f0 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 20  while( pOpen && 
c7300 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c 20  memcmp(&fileId, 
c7310 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c 20  &pOpen->fileId, 
c7320 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20  sizeof(fileId)) 
c7330 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d  ){.      pOpen =
c7340 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20   pOpen->pNext;. 
c7350 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70     }.    if( pOp
c7360 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  en==0 ){.      p
c7370 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Open = sqlite3_m
c7380 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70  alloc( sizeof(*p
c7390 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 69  Open) );.      i
c73a0 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20  f( pOpen==0 ){. 
c73b0 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c 6f         releaseLo
c73c0 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20  ckInfo(pLock);. 
c73d0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
c73e0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
c73f0 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64    goto exit_find
c7400 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20  lockinfo;.      
c7410 7d 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66  }.      pOpen->f
c7420 69 6c 65 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a  ileId = fileId;.
c7430 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65        pOpen->nRe
c7440 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70  f = 1;.      pOp
c7450 65 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20  en->nLock = 0;. 
c7460 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e       pOpen->nPen
c7470 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  ding = 0;.      
c7480 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20  pOpen->aPending 
c7490 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e  = 0;.      pOpen
c74a0 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69  ->pNext = openLi
c74b0 73 74 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  st;.      pOpen-
c74c0 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  >pPrev = 0;.    
c74d0 20 20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29    if( openList )
c74e0 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76   openList->pPrev
c74f0 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20   = pOpen;.      
c7500 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e  openList = pOpen
c7510 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  ;.#if OS_VXWORKS
c7520 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 53  .      pOpen->pS
c7530 65 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20  em = NULL;.     
c7540 20 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65   pOpen->aSemName
c7550 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 23 65 6e 64  [0] = '\0';.#end
c7560 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  if.    }else{.  
c7570 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b      pOpen->nRef+
c7580 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70  +;.    }.    *pp
c7590 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20  Open = pOpen;.  
c75a0 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b  }..exit_findlock
c75b0 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72  info:.  return r
c75c0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77  c;.}../*.** If w
c75d0 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20  e are currently 
c75e0 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 74  in a different t
c75f0 68 72 65 61 64 20 74 68 61 6e 20 74 68 65 20 74  hread than the t
c7600 68 72 65 61 64 20 74 68 61 74 20 74 68 65 0a 2a  hread that the.*
c7610 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 67 75 6d  * unixFile argum
c7620 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20  ent belongs to, 
c7630 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 6f 77  then transfer ow
c7640 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 75  nership of the u
c7650 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20  nixFile.** over 
c7660 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  to the current t
c7670 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75  hread..**.** A u
c7680 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20  nixFile is only 
c7690 6f 77 6e 65 64 20 62 79 20 61 20 74 68 72 65 61  owned by a threa
c76a0 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 74 68 61  d on systems tha
c76b0 74 20 75 73 65 20 4c 69 6e 75 78 54 68 72 65 61  t use LinuxThrea
c76c0 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73  ds..**.** Owners
c76d0 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20  hip transfer is 
c76e0 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20  only allowed if 
c76f0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20  the unixFile is 
c7700 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b  currently unlock
c7710 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e  ed..** If the un
c7720 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64  ixFile is locked
c7730 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69   and an ownershi
c7740 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e  p is wrong, then
c7750 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   return.** SQLIT
c7760 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54  E_MISUSE.  SQLIT
c7770 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
c7780 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   if everything w
c7790 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  orks..*/.#if SQL
c77a0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
c77b0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75  & defined(__linu
c77c0 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e 74 20  x__).static int 
c77d0 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69  transferOwnershi
c77e0 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  p(unixFile *pFil
c77f0 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e){.  int rc;.  
c7800 70 74 68 72 65 61 64 5f 74 20 68 53 65 6c 66 3b  pthread_t hSelf;
c7810 0a 20 20 69 66 28 20 74 68 72 65 61 64 73 4f 76  .  if( threadsOv
c7820 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
c7830 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20  Locks ){.    /* 
c7840 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66  Ownership transf
c7850 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f  ers not needed o
c7860 6e 20 74 68 69 73 20 73 79 73 74 65 6d 20 2a 2f  n this system */
c7870 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
c7880 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65  TE_OK;.  }.  hSe
c7890 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  lf = pthread_sel
c78a0 66 28 29 3b 0a 20 20 69 66 28 20 70 74 68 72 65  f();.  if( pthre
c78b0 61 64 5f 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e  ad_equal(pFile->
c78c0 74 69 64 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20  tid, hSelf) ){. 
c78d0 20 20 20 2f 2a 20 57 65 20 61 72 65 20 73 74 69     /* We are sti
c78e0 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d 65 20 74  ll in the same t
c78f0 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54  hread */.    OST
c7900 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66  RACE1("No-transf
c7910 65 72 2c 20 73 61 6d 65 20 74 68 72 65 61 64 5c  er, same thread\
c7920 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  n");.    return 
c7930 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
c7940 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
c7950 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  type!=NO_LOCK ){
c7960 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f  .    /* We canno
c7970 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 72 73 68  t change ownersh
c7980 69 70 20 77 68 69 6c 65 20 77 65 20 61 72 65 20  ip while we are 
c7990 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20  holding a lock! 
c79a0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
c79b0 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
c79c0 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 72 61  .  OSTRACE4("Tra
c79d0 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20  nsfer ownership 
c79e0 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 20 74 6f  of %d from %d to
c79f0 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %d\n",.        
c7a00 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46      pFile->h, pF
c7a10 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29  ile->tid, hSelf)
c7a20 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d  ;.  pFile->tid =
c7a30 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28 70 46   hSelf;.  if (pF
c7a40 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55  ile->pLock != NU
c7a50 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65 61 73  LL) {.    releas
c7a60 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d  eLockInfo(pFile-
c7a70 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20  >pLock);.    rc 
c7a80 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70  = findLockInfo(p
c7a90 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e 70 4c  File, &pFile->pL
c7aa0 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54  ock, 0);.    OST
c7ab0 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25  RACE5("LOCK    %
c7ac0 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73 2c 25  d is now %s(%s,%
c7ad0 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  d)\n", pFile->h,
c7ae0 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b  .           lock
c7af0 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e  typeName(pFile->
c7b00 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20  locktype),.     
c7b10 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
c7b20 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d  me(pFile->pLock-
c7b30 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46 69 6c  >locktype), pFil
c7b40 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a  e->pLock->cnt);.
c7b50 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
c7b60 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65   } else {.    re
c7b70 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c7b80 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f 2a 20    }.}.#else  /* 
c7b90 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f 54 48  if not SQLITE_TH
c7ba0 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 20 2f 2a  READSAFE */.  /*
c7bb0 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65 61   On single-threa
c7bc0 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65  ded builds, owne
c7bd0 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69  rship transfer i
c7be0 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64  s a no-op */.# d
c7bf0 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72 4f 77  efine transferOw
c7c00 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c 49 54  nership(X) SQLIT
c7c10 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  E_OK.#endif /* S
c7c20 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
c7c30 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   */.../*.** This
c7c40 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
c7c50 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45  if there is a RE
c7c60 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64  SERVED lock held
c7c70 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   on the specifie
c7c80 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69  d.** file by thi
c7c90 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
c7ca0 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20  rocess. If such 
c7cb0 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20  a lock is held, 
c7cc0 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20  set *pResOut.** 
c7cd0 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  to a non-zero va
c7ce0 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70  lue otherwise *p
c7cf0 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f  ResOut is set to
c7d00 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75   zero.  The retu
c7d10 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73  rn value.** is s
c7d20 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  et to SQLITE_OK 
c7d30 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72  unless an I/O er
c7d40 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
c7d50 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e  g lock checking.
c7d60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
c7d70 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  nixCheckReserved
c7d80 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
c7d90 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
c7da0 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Out){.  int rc =
c7db0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
c7dc0 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a  t reserved = 0;.
c7dd0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
c7de0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
c7df0 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  d;..  SimulateIO
c7e00 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
c7e10 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
c7e20 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b  RESERVEDLOCK; );
c7e30 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
c7e40 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72  e );.  unixEnter
c7e50 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 63 61  Mutex(); /* Beca
c7e60 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  use pFile->pLock
c7e70 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73   is shared acros
c7e80 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20  s threads */..  
c7e90 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68  /* Check if a th
c7ea0 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f  read in this pro
c7eb0 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20  cess holds such 
c7ec0 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20  a lock */.  if( 
c7ed0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f  pFile->pLock->lo
c7ee0 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f  cktype>SHARED_LO
c7ef0 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76  CK ){.    reserv
c7f00 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f  ed = 1;.  }..  /
c7f10 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20  * Otherwise see 
c7f20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72  if some other pr
c7f30 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a  ocess holds it..
c7f40 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 5f 44    */.#ifndef __D
c7f50 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 21 72 65  JGPP__.  if( !re
c7f60 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 74  served ){.    st
c7f70 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b  ruct flock lock;
c7f80 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e  .    lock.l_when
c7f90 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
c7fa0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
c7fb0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b  = RESERVED_BYTE;
c7fc0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  .    lock.l_len 
c7fd0 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  = 1;.    lock.l_
c7fe0 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a  type = F_WRLCK;.
c7ff0 20 20 20 20 69 66 20 28 2d 31 20 3d 3d 20 66 63      if (-1 == fc
c8000 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f  ntl(pFile->h, F_
c8010 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b  GETLK, &lock)) {
c8020 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e  .      int tErrn
c8030 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
c8040 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
c8050 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
c8060 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
c8070 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
c8080 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 70  EDLOCK);.      p
c8090 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
c80a0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 20  = tErrno;.    } 
c80b0 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f  else if( lock.l_
c80c0 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b  type!=F_UNLCK ){
c80d0 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20  .      reserved 
c80e0 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  = 1;.    }.  }.#
c80f0 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69 78 4c  endif.  .  unixL
c8100 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
c8110 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52  STRACE4("TEST WR
c8120 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e  -LOCK %d %d %d\n
c8130 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c  ", pFile->h, rc,
c8140 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a   reserved);..  *
c8150 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76  pResOut = reserv
c8160 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ed;.  return rc;
c8170 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
c8180 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
c8190 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
c81a0 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
c81b0 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
c81c0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
c81d0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
c81e0 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
c81f0 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
c8200 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
c8210 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
c8220 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
c8230 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
c8240 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
c8250 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
c8260 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
c8270 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
c8280 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
c8290 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
c82a0 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
c82b0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
c82c0 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
c82d0 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
c82e0 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
c82f0 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
c8300 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
c8310 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
c8320 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
c8330 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
c8340 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
c8350 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
c8360 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
c8370 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
c8380 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
c8390 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
c83a0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
c83b0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
c83c0 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
c83d0 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
c83e0 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
c83f0 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
c8400 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
c8410 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
c8420 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
c8430 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
c8440 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
c8450 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
c8460 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a  ite3OsUnlock().*
c8470 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77  * routine to low
c8480 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  er a locking lev
c8490 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  el..*/.static in
c84a0 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74  t unixLock(sqlit
c84b0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
c84c0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a   locktype){.  /*
c84d0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64   The following d
c84e0 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70  escribes the imp
c84f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
c8500 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73  he various locks
c8510 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74   and.  ** lock t
c8520 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65  ransitions in te
c8530 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58  rms of the POSIX
c8540 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64   advisory shared
c8550 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20   and exclusive. 
c8560 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69   ** lock primiti
c8570 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64  ves (called read
c8580 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65  -locks and write
c8590 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f  -locks below, to
c85a0 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66   avoid.  ** conf
c85b0 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74  usion with SQLit
c85c0 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54  e lock names). T
c85d0 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72  he algorithms ar
c85e0 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20  e complicated.  
c85f0 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f  ** slightly in o
c8600 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61  rder to be compa
c8610 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f  tible with windo
c8620 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c  ws systems simul
c8630 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61  taneously.  ** a
c8640 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d  ccessing the sam
c8650 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
c8660 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73   in case that is
c8670 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a   ever required..
c8680 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c    **.  ** Symbol
c8690 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e  s defined in os.
c86a0 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20  h indentify the 
c86b0 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61  'pending byte' a
c86c0 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64  nd the 'reserved
c86d0 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63  .  ** byte', eac
c86e0 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61  h single bytes a
c86f0 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  t well known off
c8700 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73  sets, and the 's
c8710 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20  hared byte.  ** 
c8720 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20  range', a range 
c8730 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20  of 510 bytes at 
c8740 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  a well known off
c8750 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  set..  **.  ** T
c8760 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45  o obtain a SHARE
c8770 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c  D lock, a read-l
c8780 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
c8790 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a  on the 'pending.
c87a0 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20    ** byte'.  If 
c87b0 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66  this is successf
c87c0 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  ul, a random byt
c87d0 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72  e from the 'shar
c87e0 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e  ed byte.  ** ran
c87f0 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b  ge' is read-lock
c8800 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  ed and the lock 
c8810 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  on the 'pending 
c8820 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a  byte' released..
c8830 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63    **.  ** A proc
c8840 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74  ess may only obt
c8850 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c  ain a RESERVED l
c8860 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73  ock after it has
c8870 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a   a SHARED lock..
c8880 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20    ** A RESERVED 
c8890 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e  lock is implemen
c88a0 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20  ted by grabbing 
c88b0 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
c88c0 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76  the.  ** 'reserv
c88d0 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a  ed byte'. .  **.
c88e0 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d    ** A process m
c88f0 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61  ay only obtain a
c8900 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66   PENDING lock af
c8910 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69  ter it has obtai
c8920 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45  ned a.  ** SHARE
c8930 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e  D lock. A PENDIN
c8940 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d  G lock is implem
c8950 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69  ented by obtaini
c8960 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a  ng a write-lock.
c8970 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e    ** on the 'pen
c8980 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73  ding byte'. This
c8990 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f   ensures that no
c89a0 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b   new SHARED lock
c89b0 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62  s can be.  ** ob
c89c0 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73  tained, but exis
c89d0 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b  ting SHARED lock
c89e0 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f  s are allowed to
c89f0 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63   persist. A proc
c8a00 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f  ess.  ** does no
c8a10 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e  t have to obtain
c8a20 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
c8a30 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61   on the way to a
c8a40 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20   PENDING lock.. 
c8a50 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74   ** This propert
c8a60 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  y is used by the
c8a70 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72   algorithm for r
c8a80 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f  olling back a jo
c8a90 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20  urnal file.  ** 
c8aa0 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20  after a crash.. 
c8ab0 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c   **.  ** An EXCL
c8ac0 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61  USIVE lock, obta
c8ad0 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e  ined after a PEN
c8ae0 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c  DING lock is hel
c8af0 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65  d, is.  ** imple
c8b00 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e  mented by obtain
c8b10 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ing a write-lock
c8b20 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27   on the entire '
c8b30 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a  shared byte.  **
c8b40 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61   range'. Since a
c8b50 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72  ll other locks r
c8b60 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f  equire a read-lo
c8b70 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  ck on one of the
c8b80 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68   bytes.  ** with
c8b90 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74  in this range, t
c8ba0 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
c8bb0 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20   no other locks 
c8bc0 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a  are held on the.
c8bd0 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a    ** database. .
c8be0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65    **.  ** The re
c8bf0 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79  ason a single by
c8c00 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  te cannot be use
c8c10 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  d instead of the
c8c20 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20   'shared byte.  
c8c30 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61  ** range' is tha
c8c40 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20  t some versions 
c8c50 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f  of windows do no
c8c60 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c  t support read-l
c8c70 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f  ocks. By.  ** lo
c8c80 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62  cking a random b
c8c90 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65  yte from a range
c8ca0 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41  , concurrent SHA
c8cb0 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78  RED locks may ex
c8cc0 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66  ist.  ** even if
c8cd0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69   the locking pri
c8ce0 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61  mitive used is a
c8cf0 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f  lways a write-lo
c8d00 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72  ck..  */.  int r
c8d10 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
c8d20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
c8d30 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
c8d40 3b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  ;.  struct unixL
c8d50 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d  ockInfo *pLock =
c8d60 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20   pFile->pLock;. 
c8d70 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f   struct flock lo
c8d80 63 6b 3b 0a 20 20 69 6e 74 20 73 3b 0a 0a 20 20  ck;.  int s;..  
c8d90 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
c8da0 0a 20 20 4f 53 54 52 41 43 45 37 28 22 4c 4f 43  .  OSTRACE7("LOC
c8db0 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 25  K    %d %s was %
c8dc0 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 64 5c  s(%s,%d) pid=%d\
c8dd0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
c8de0 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
c8df0 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b  (locktype), lock
c8e00 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e  typeName(pFile->
c8e10 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20  locktype),.     
c8e20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c   locktypeName(pL
c8e30 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20  ock->locktype), 
c8e40 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74  pLock->cnt , get
c8e50 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  pid());..  /* If
c8e60 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64   there is alread
c8e70 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73  y a lock of this
c8e80 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65   type or more re
c8e90 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65  strictive on the
c8ea0 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20  .  ** unixFile, 
c8eb0 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27  do nothing. Don'
c8ec0 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f  t use the end_lo
c8ed0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
c8ee0 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72  s.  ** unixEnter
c8ef0 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62  Mutex() hasn't b
c8f00 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a  een called yet..
c8f10 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65    */.  if( pFile
c8f20 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b  ->locktype>=lock
c8f30 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52  type ){.    OSTR
c8f40 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE3("LOCK    %d
c8f50 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20   %s ok (already 
c8f60 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  held)\n", pFile-
c8f70 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  >h,.            
c8f80 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
c8f90 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74  ktype));.    ret
c8fa0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
c8fb0 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
c8fc0 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73  re the locking s
c8fd0 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65  equence is corre
c8fe0 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  ct.  */.  assert
c8ff0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
c9000 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f  e!=NO_LOCK || lo
c9010 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
c9020 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
c9030 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49   locktype!=PENDI
c9040 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  NG_LOCK );.  ass
c9050 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52  ert( locktype!=R
c9060 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20  ESERVED_LOCK || 
c9070 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
c9080 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
c9090 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78  .  /* This mutex
c90a0 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75   is needed becau
c90b0 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20  se pFile->pLock 
c90c0 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73  is shared across
c90d0 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20   threads.  */.  
c90e0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
c90f0 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  ;..  /* Make sur
c9100 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  e the current th
c9110 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46  read owns the pF
c9120 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ile..  */.  rc =
c9130 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68   transferOwnersh
c9140 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28  ip(pFile);.  if(
c9150 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
c9160 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d  {.    unixLeaveM
c9170 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75  utex();.    retu
c9180 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f  rn rc;.  }.  pLo
c9190 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63  ck = pFile->pLoc
c91a0 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65  k;..  /* If some
c91b0 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68   thread using th
c91c0 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63  is PID has a loc
c91d0 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e  k via a differen
c91e0 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a  t unixFile*.  **
c91f0 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65   handle that pre
c9200 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65  cludes the reque
c9210 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72  sted lock, retur
c9220 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69  n BUSY..  */.  i
c9230 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  f( (pFile->lockt
c9240 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  ype!=pLock->lock
c9250 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 20  type && .       
c9260 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74     (pLock->lockt
c9270 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype>=PENDING_LOC
c9280 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48  K || locktype>SH
c9290 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b  ARED_LOCK)).  ){
c92a0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
c92b0 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20  _BUSY;.    goto 
c92c0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20  end_lock;.  }.. 
c92d0 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20   /* If a SHARED 
c92e0 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65  lock is requeste
c92f0 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65  d, and some thre
c9300 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49  ad using this PI
c9310 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68  D already.  ** h
c9320 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52  as a SHARED or R
c9330 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68  ESERVED lock, th
c9340 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66  en increment ref
c9350 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e  erence counts an
c9360 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51  d.  ** return SQ
c9370 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20  LITE_OK..  */.  
c9380 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
c9390 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20  ARED_LOCK && .  
c93a0 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b      (pLock->lock
c93b0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
c93c0 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  K || pLock->lock
c93d0 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c  type==RESERVED_L
c93e0 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65  OCK) ){.    asse
c93f0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  rt( locktype==SH
c9400 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20  ARED_LOCK );.   
c9410 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
c9420 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20  locktype==0 );. 
c9430 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
c9440 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70  ->cnt>0 );.    p
c9450 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
c9460 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
c9470 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a    pLock->cnt++;.
c9480 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e      pFile->pOpen
c9490 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67  ->nLock++;.    g
c94a0 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  oto end_lock;.  
c94b0 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  }..  lock.l_len 
c94c0 3d 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f  = 1L;..  lock.l_
c94d0 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45  whence = SEEK_SE
c94e0 54 3b 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49  T;..  /* A PENDI
c94f0 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65  NG lock is neede
c9500 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69  d before acquiri
c9510 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ng a SHARED lock
c9520 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a   and before.  **
c9530 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58   acquiring an EX
c9540 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46  CLUSIVE lock.  F
c9550 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f  or the SHARED lo
c9560 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20  ck, the PENDING 
c9570 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c  will.  ** be rel
c9580 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  eased..  */.  if
c9590 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
c95a0 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c  ED_LOCK .      |
c95b0 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  | (locktype==EXC
c95c0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
c95d0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50  File->locktype<P
c95e0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29  ENDING_LOCK).  )
c95f0 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  {.    lock.l_typ
c9600 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53  e = (locktype==S
c9610 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c  HARED_LOCK?F_RDL
c9620 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20  CK:F_WRLCK);.   
c9630 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
c9640 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20  PENDING_BYTE;.  
c9650 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c    s = fcntl(pFil
c9660 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  e->h, F_SETLK, &
c9670 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73  lock);.    if( s
c9680 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
c9690 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
c96a0 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  no;.      rc = s
c96b0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
c96c0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
c96d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
c96e0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
c96f0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
c9700 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
c9710 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
c9720 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
c9730 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f       goto end_lo
c9740 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a  ck;.    }.  }...
c9750 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20    /* If control 
c9760 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69  gets to this poi
c9770 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c  nt, then actuall
c9780 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d  y go ahead and m
c9790 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69  ake.  ** operati
c97a0 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20  ng system calls 
c97b0 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
c97c0 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  d lock..  */.  i
c97d0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
c97e0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
c97f0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a  int tErrno = 0;.
c9800 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
c9810 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20  k->cnt==0 );.   
c9820 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
c9830 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a  locktype==0 );..
c9840 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74      /* Now get t
c9850 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a  he read-lock */.
c9860 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
c9870 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
c9880 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  .    lock.l_len 
c9890 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
c98a0 20 20 20 69 66 28 20 28 73 20 3d 20 66 63 6e 74     if( (s = fcnt
c98b0 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45  l(pFile->h, F_SE
c98c0 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 3d 3d 28 2d  TLK, &lock))==(-
c98d0 31 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72  1) ){.      tErr
c98e0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
c98f0 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68  }.    /* Drop th
c9900 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44  e temporary PEND
c9910 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ING lock */.    
c9920 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50  lock.l_start = P
c9930 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
c9940 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c   lock.l_len = 1L
c9950 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  ;.    lock.l_typ
c9960 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20  e = F_UNLCK;.   
c9970 20 69 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65   if( fcntl(pFile
c9980 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
c9990 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ock)!=0 ){.     
c99a0 20 69 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a   if( s != -1 ){.
c99b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
c99c0 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74  could happen wit
c99d0 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e  h a network moun
c99e0 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72  t */.        tEr
c99f0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20  rno = errno; .  
c9a00 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
c9a10 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
c9a20 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
c9a30 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
c9a40 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20  ); .        if( 
c9a50 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
c9a60 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
c9a70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
c9a80 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
c9a90 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f    }.        goto
c9aa0 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20   end_lock;.     
c9ab0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
c9ac0 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20   s==(-1) ){.    
c9ad0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
c9ae0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
c9af0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
c9b00 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20  IOERR_LOCK);.   
c9b10 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
c9b20 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
c9b30 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
c9b40 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
c9b50 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
c9b60 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
c9b70 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
c9b80 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69  _LOCK;.      pFi
c9b90 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b  le->pOpen->nLock
c9ba0 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d  ++;.      pLock-
c9bb0 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  >cnt = 1;.    }.
c9bc0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b    }else if( lock
c9bd0 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
c9be0 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63  LOCK && pLock->c
c9bf0 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57  nt>1 ){.    /* W
c9c00 65 20 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72  e are trying for
c9c10 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
c9c20 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74  ck but another t
c9c30 68 72 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20  hread in this.  
c9c40 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73    ** same proces
c9c50 73 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69  s is still holdi
c9c60 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  ng a shared lock
c9c70 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51  . */.    rc = SQ
c9c80 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c  LITE_BUSY;.  }el
c9c90 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72  se{.    /* The r
c9ca0 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61  equest was for a
c9cb0 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43   RESERVED or EXC
c9cc0 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74  LUSIVE lock.  It
c9cd0 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d   is.    ** assum
c9ce0 65 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73  ed that there is
c9cf0 20 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65   a SHARED or gre
c9d00 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ater lock on the
c9d10 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72   file.    ** alr
c9d20 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eady..    */.   
c9d30 20 61 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c   assert( 0!=pFil
c9d40 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
c9d50 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
c9d60 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77   F_WRLCK;.    sw
c9d70 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29  itch( locktype )
c9d80 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 45 53  {.      case RES
c9d90 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20  ERVED_LOCK:.    
c9da0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
c9db0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45   = RESERVED_BYTE
c9dc0 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
c9dd0 0a 20 20 20 20 20 20 63 61 73 65 20 45 58 43 4c  .      case EXCL
c9de0 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20  USIVE_LOCK:.    
c9df0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
c9e00 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
c9e10 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
c9e20 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a  len = SHARED_SIZ
c9e30 45 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  E;.        break
c9e40 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  ;.      default:
c9e50 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
c9e60 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 20  0);.    }.    s 
c9e70 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68  = fcntl(pFile->h
c9e80 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
c9e90 29 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d  );.    if( s==(-
c9ea0 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  1) ){.      int 
c9eb0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
c9ec0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
c9ed0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
c9ee0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
c9ef0 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b  ITE_IOERR_LOCK);
c9f00 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  .      if( IS_LO
c9f10 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
c9f20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
c9f30 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
c9f40 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  o;.      }.    }
c9f50 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66  .  }.  ..#ifndef
c9f60 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74   NDEBUG.  /* Set
c9f70 20 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74   up the transact
c9f80 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e  ion-counter chan
c9f90 67 65 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67  ge checking flag
c9fa0 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e  s when.  ** tran
c9fb0 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61  sitioning from a
c9fc0 20 53 48 41 52 45 44 20 74 6f 20 61 20 52 45 53   SHARED to a RES
c9fd0 45 52 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65  ERVED lock.  The
c9fe0 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f   change.  ** fro
c9ff0 6d 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45  m SHARED to RESE
ca000 52 56 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62  RVED marks the b
ca010 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f  eginning of a no
ca020 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20  rmal.  ** write 
ca030 6f 70 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61  operation (not a
ca040 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot journal rol
ca050 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69  lback)..  */.  i
ca060 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
ca070 0a 20 20 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f  .   && pFile->lo
ca080 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
ca090 4f 43 4b 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79  OCK.   && lockty
ca0a0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe==RESERVED_LOC
ca0b0 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  K.  ){.    pFile
ca0c0 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20  ->transCntrChng 
ca0d0 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  = 0;.    pFile->
ca0e0 64 62 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20  dbUpdate = 0;.  
ca0f0 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61    pFile->inNorma
ca100 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a  lWrite = 1;.  }.
ca110 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 72  #endif...  if( r
ca120 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
ca130 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
ca140 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
ca150 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74      pLock->lockt
ca160 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
ca170 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b    }else if( lock
ca180 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
ca190 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c  LOCK ){.    pFil
ca1a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45  e->locktype = PE
ca1b0 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20  NDING_LOCK;.    
ca1c0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20  pLock->locktype 
ca1d0 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a  = PENDING_LOCK;.
ca1e0 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20    }..end_lock:. 
ca1f0 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
ca200 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c  );.  OSTRACE4("L
ca210 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c  OCK    %d %s %s\
ca220 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
ca230 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74  cktypeName(lockt
ca240 79 70 65 29 2c 20 0a 20 20 20 20 20 20 72 63 3d  ype), .      rc=
ca250 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b  =SQLITE_OK ? "ok
ca260 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20  " : "failed");. 
ca270 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
ca280 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c  *.** Lower the l
ca290 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20  ocking level on 
ca2a0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
ca2b0 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70  pFile to locktyp
ca2c0 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20  e.  locktype.** 
ca2d0 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e  must be either N
ca2e0 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44  O_LOCK or SHARED
ca2f0 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  _LOCK..**.** If 
ca300 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
ca310 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65  l of the file de
ca320 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65  scriptor is alre
ca330 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a  ady at or below.
ca340 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64  ** the requested
ca350 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20   locking level, 
ca360 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
ca370 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
ca380 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f 63  ic int unixUnloc
ca390 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
ca3a0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
ca3b0 29 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  ){.  struct unix
ca3c0 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b  LockInfo *pLock;
ca3d0 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20  .  struct flock 
ca3e0 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 20 3d  lock;.  int rc =
ca3f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e   SQLITE_OK;.  un
ca400 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
ca410 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
ca420 20 69 6e 74 20 68 3b 0a 0a 20 20 61 73 73 65 72   int h;..  asser
ca430 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53  t( pFile );.  OS
ca440 54 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20  TRACE7("UNLOCK  
ca450 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c  %d %d was %d(%d,
ca460 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70  %d) pid=%d\n", p
ca470 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
ca480 65 2c 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  e,.      pFile->
ca490 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d  locktype, pFile-
ca4a0 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65  >pLock->locktype
ca4b0 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e  , pFile->pLock->
ca4c0 63 6e 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a  cnt, getpid());.
ca4d0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
ca4e0 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
ca4f0 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   );.  if( pFile-
ca500 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74  >locktype<=lockt
ca510 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
ca520 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
ca530 0a 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52  .  if( CHECK_THR
ca540 45 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a  EADID(pFile) ){.
ca550 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ca560 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
ca570 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
ca580 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68  ;.  h = pFile->h
ca590 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c  ;.  pLock = pFil
ca5a0 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65  e->pLock;.  asse
ca5b0 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d  rt( pLock->cnt!=
ca5c0 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  0 );.  if( pFile
ca5d0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45  ->locktype>SHARE
ca5e0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73  D_LOCK ){.    as
ca5f0 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63  sert( pLock->loc
ca600 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f  ktype==pFile->lo
ca610 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 53 69  cktype );.    Si
ca620 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
ca630 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d 75  ign(1);.    Simu
ca640 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28  lateIOError( h=(
ca650 2d 31 29 20 29 0a 20 20 20 20 53 69 6d 75 6c 61  -1) ).    Simula
ca660 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
ca670 30 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  0);..#ifndef NDE
ca680 42 55 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20  BUG.    /* When 
ca690 72 65 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20  reducing a lock 
ca6a0 73 75 63 68 20 74 68 61 74 20 6f 74 68 65 72 20  such that other 
ca6b0 70 72 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74  processes can st
ca6c0 61 72 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69  art.    ** readi
ca6d0 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
ca6e0 66 69 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65  file again, make
ca6f0 20 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 20   sure that the. 
ca700 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f     ** transactio
ca710 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70  n counter was up
ca720 64 61 74 65 64 20 69 66 20 61 6e 79 20 70 61 72  dated if any par
ca730 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  t of the databas
ca740 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68  e.    ** file ch
ca750 61 6e 67 65 64 2e 20 20 49 66 20 74 68 65 20 74  anged.  If the t
ca760 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
ca770 65 72 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65  er is not update
ca780 64 2c 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20  d,.    ** other 
ca790 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74  connections to t
ca7a0 68 65 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67  he same file mig
ca7b0 68 74 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74  ht not realize t
ca7c0 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66  hat.    ** the f
ca7d0 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 20  ile has changed 
ca7e0 61 6e 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20  and hence might 
ca7f0 6e 6f 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73  not know to flus
ca800 68 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63  h their.    ** c
ca810 61 63 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f  ache.  The use o
ca820 66 20 61 20 73 74 61 6c 65 20 63 61 63 68 65 20  f a stale cache 
ca830 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61  can lead to data
ca840 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e  base corruption.
ca850 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
ca860 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72  rt( pFile->inNor
ca870 6d 61 6c 57 72 69 74 65 3d 3d 30 0a 20 20 20 20  malWrite==0.    
ca880 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64       || pFile->d
ca890 62 55 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20  bUpdate==0.     
ca8a0 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72      || pFile->tr
ca8b0 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29  ansCntrChng==1 )
ca8c0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e  ;.    pFile->inN
ca8d0 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a  ormalWrite = 0;.
ca8e0 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20 69 66 28  #endif...    if(
ca8f0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
ca900 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20  D_LOCK ){.      
ca910 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  lock.l_type = F_
ca920 52 44 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63  RDLCK;.      loc
ca930 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  k.l_whence = SEE
ca940 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63  K_SET;.      loc
ca950 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52  k.l_start = SHAR
ca960 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20  ED_FIRST;.      
ca970 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41  lock.l_len = SHA
ca980 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20  RED_SIZE;.      
ca990 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53  if( fcntl(h, F_S
ca9a0 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d  ETLK, &lock)==(-
ca9b0 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  1) ){.        in
ca9c0 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  t tErrno = errno
ca9d0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
ca9e0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
ca9f0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
caa00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44   SQLITE_IOERR_RD
caa10 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69  LOCK);.        i
caa20 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
caa30 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (rc) ){.        
caa40 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
caa50 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
caa60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67       }.        g
caa70 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a  oto end_unlock;.
caa80 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
caa90 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
caaa0 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63  F_UNLCK;.    loc
caab0 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  k.l_whence = SEE
caac0 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e  K_SET;.    lock.
caad0 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e  l_start = PENDIN
caae0 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b  G_BYTE;.    lock
caaf0 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 73  .l_len = 2L;  as
cab00 73 65 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 59  sert( PENDING_BY
cab10 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f 42  TE+1==RESERVED_B
cab20 59 54 45 20 29 3b 0a 20 20 20 20 69 66 28 20 66  YTE );.    if( f
cab30 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c  cntl(h, F_SETLK,
cab40 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b   &lock)!=(-1) ){
cab50 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f  .      pLock->lo
cab60 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f  cktype = SHARED_
cab70 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
cab80 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e  .      int tErrn
cab90 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
caba0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
cabb0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
cabc0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
cabd0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20  OERR_UNLOCK);.  
cabe0 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
cabf0 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20  ERROR(rc) ){.   
cac00 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
cac10 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
cac20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f        }.      go
cac30 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20  to end_unlock;. 
cac40 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c     }.  }.  if( l
cac50 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
cac60 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75   ){.    struct u
cac70 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65  nixOpenCnt *pOpe
cac80 6e 3b 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d  n;.    int rc2 =
cac90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 20   SQLITE_OK;..   
caca0 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68   /* Decrement th
cacb0 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f  e shared lock co
cacc0 75 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20  unter.  Release 
cacd0 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61  the lock using a
cace0 6e 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c  n.    ** OS call
cacf0 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74   only when all t
cad00 68 72 65 61 64 73 20 69 6e 20 74 68 69 73 20 73  hreads in this s
cad10 61 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76 65  ame process have
cad20 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a   released.    **
cad30 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a   the lock..    *
cad40 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74  /.    pLock->cnt
cad50 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63  --;.    if( pLoc
cad60 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  k->cnt==0 ){.   
cad70 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
cad80 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20   F_UNLCK;.      
cad90 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
cada0 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20  SEEK_SET;.      
cadb0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c  lock.l_start = l
cadc0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a  ock.l_len = 0L;.
cadd0 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f        SimulateIO
cade0 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a  ErrorBenign(1);.
cadf0 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f        SimulateIO
cae00 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a  Error( h=(-1) ).
cae10 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f        SimulateIO
cae20 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a  ErrorBenign(0);.
cae30 20 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28        if( fcntl(
cae40 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63  h, F_SETLK, &loc
cae50 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20  k)!=(-1) ){.    
cae60 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74      pLock->lockt
cae70 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20  ype = NO_LOCK;. 
cae80 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
cae90 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
caea0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
caeb0 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
caec0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
caed0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
caee0 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20  ERR_UNLOCK);.   
caef0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
caf00 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
caf10 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
caf20 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
caf30 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  o;.        }.   
caf40 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b       pLock->lock
caf50 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
caf60 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
caf70 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
caf80 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  K;.      }.    }
caf90 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65  ..    /* Decreme
cafa0 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  nt the count of 
cafb0 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68  locks against th
cafc0 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57  is same file.  W
cafd0 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63  hen the.    ** c
cafe0 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72  ount reaches zer
caff0 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68  o, close any oth
cb000 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  er file descript
cb010 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a  ors whose close.
cb020 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72      ** was defer
cb030 72 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f  red because of o
cb040 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73  utstanding locks
cb050 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70  ..    */.    pOp
cb060 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  en = pFile->pOpe
cb070 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c  n;.    pOpen->nL
cb080 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72  ock--;.    asser
cb090 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e  t( pOpen->nLock>
cb0a0 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  =0 );.    if( pO
cb0b0 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26  pen->nLock==0 &&
cb0c0 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67   pOpen->nPending
cb0d0 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  >0 ){.      int 
cb0e0 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  i;.      for(i=0
cb0f0 3b 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64  ; i<pOpen->nPend
cb100 69 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ing; i++){.     
cb110 20 20 20 2f 2a 20 63 6c 6f 73 65 20 70 65 6e 64     /* close pend
cb120 69 6e 67 20 66 64 73 2c 20 62 75 74 20 69 66 20  ing fds, but if 
cb130 63 6c 6f 73 69 6e 67 20 66 61 69 6c 73 20 64 6f  closing fails do
cb140 6e 27 74 20 66 72 65 65 20 74 68 65 20 61 72 72  n't free the arr
cb150 61 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73  ay.        ** as
cb160 73 69 67 6e 20 2d 31 20 74 6f 20 74 68 65 20 73  sign -1 to the s
cb170 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6c 6f 73  uccessfully clos
cb180 65 64 20 64 65 73 63 72 69 70 74 6f 72 73 20 61  ed descriptors a
cb190 6e 64 20 72 65 63 6f 72 64 20 74 68 65 0a 20 20  nd record the.  
cb1a0 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 2e 20        ** error. 
cb1b0 20 54 68 65 20 6e 65 78 74 20 61 74 74 65 6d 70   The next attemp
cb1c0 74 20 74 6f 20 75 6e 6c 6f 63 6b 20 77 69 6c 6c  t to unlock will
cb1d0 20 74 72 79 20 61 67 61 69 6e 2e 20 2a 2f 0a 20   try again. */. 
cb1e0 20 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e         if( pOpen
cb1f0 2d 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3c 20  ->aPending[i] < 
cb200 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
cb210 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28        if( close(
cb220 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b  pOpen->aPending[
cb230 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  i]) ){.         
cb240 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
cb250 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
cb260 20 20 20 20 20 72 63 32 20 3d 20 53 51 4c 49 54       rc2 = SQLIT
cb270 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20  E_IOERR_CLOSE;. 
cb280 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
cb290 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61          pOpen->a
cb2a0 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d 31 3b  Pending[i] = -1;
cb2b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
cb2c0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 32   }.      if( rc2
cb2d0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
cb2e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
cb2f0 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64  ree(pOpen->aPend
cb300 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  ing);.        pO
cb310 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d 20  pen->nPending = 
cb320 30 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e  0;.        pOpen
cb330 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a  ->aPending = 0;.
cb340 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
cb350 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
cb360 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
cb370 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 20 20 7d  = rc2;.    }.  }
cb380 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20  ...end_unlock:. 
cb390 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
cb3a0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
cb3b0 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e  ITE_OK ) pFile->
cb3c0 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
cb3d0 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ype;.  return rc
cb3e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
cb3f0 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d  function perform
cb400 73 20 74 68 65 20 70 61 72 74 73 20 6f 66 20 74  s the parts of t
cb410 68 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20  he "close file" 
cb420 6f 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f  operation .** co
cb430 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b  mmon to all lock
cb440 69 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20  ing schemes. It 
cb450 63 6c 6f 73 65 73 20 74 68 65 20 64 69 72 65 63  closes the direc
cb460 74 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a  tory and file.**
cb470 20 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65   handles, if the
cb480 79 20 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64  y are valid, and
cb490 20 73 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73   sets all fields
cb4a0 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65   of the unixFile
cb4b0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f  .** structure to
cb4c0 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20   0..**.** It is 
cb4d0 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20  *not* necessary 
cb4e0 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65  to hold the mute
cb4f0 78 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  x when this rout
cb500 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a  ine is called,.*
cb510 2a 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b  * even on VxWork
cb520 73 2e 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c  s.  A mutex will
cb530 20 62 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20   be acquired on 
cb540 56 78 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a  VxWorks by the.*
cb550 2a 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65  * vxworksRelease
cb560 46 69 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65  FileId() routine
cb570 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
cb580 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71  closeUnixFile(sq
cb590 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
cb5a0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
cb5b0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
cb5c0 69 64 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20  id;.  if( pFile 
cb5d0 29 7b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65  ){.    if( pFile
cb5e0 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20  ->dirfd>=0 ){.  
cb5f0 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c      int err = cl
cb600 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64  ose(pFile->dirfd
cb610 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72  );.      if( err
cb620 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
cb630 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
cb640 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65  rrno;.        re
cb650 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
cb660 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20  R_DIR_CLOSE;.   
cb670 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
cb680 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d 2d    pFile->dirfd=-
cb690 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
cb6a0 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  .    if( pFile->
cb6b0 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  h>=0 ){.      in
cb6c0 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46  t err = close(pF
cb6d0 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 69  ile->h);.      i
cb6e0 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20  f( err ){.      
cb6f0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
cb700 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
cb710 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
cb720 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20  E_IOERR_CLOSE;. 
cb730 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66       }.    }.#if
cb740 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20   OS_VXWORKS.    
cb750 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 29  if( pFile->pId )
cb760 7b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c  {.      if( pFil
cb770 65 2d 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a 20  e->isDelete ){. 
cb780 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46         unlink(pF
cb790 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e  ile->pId->zCanon
cb7a0 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20  icalName);.     
cb7b0 20 7d 0a 20 20 20 20 20 20 76 78 77 6f 72 6b 73   }.      vxworks
cb7c0 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46  ReleaseFileId(pF
cb7d0 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 20  ile->pId);.     
cb7e0 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b   pFile->pId = 0;
cb7f0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
cb800 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53    OSTRACE2("CLOS
cb810 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69  E   %-3d\n", pFi
cb820 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 6e  le->h);.    Open
cb830 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20  Counter(-1);.   
cb840 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30   memset(pFile, 0
cb850 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c  , sizeof(unixFil
cb860 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  e));.  }.  retur
cb870 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
cb880 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
cb890 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
cb8a0 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69  t unixClose(sqli
cb8b0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
cb8c0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
cb8d0 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b  _OK;.  if( id ){
cb8e0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
cb8f0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
cb900 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e 69 78 55   *)id;.    unixU
cb910 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
cb920 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65  K);.    unixEnte
cb930 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66  rMutex();.    if
cb940 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26  ( pFile->pOpen &
cb950 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e  & pFile->pOpen->
cb960 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f  nLock ){.      /
cb970 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f  * If there are o
cb980 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73  utstanding locks
cb990 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c  , do not actuall
cb9a0 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  y close the file
cb9b0 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79   just.      ** y
cb9c0 65 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20  et because that 
cb9d0 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73  would clear thos
cb9e0 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61  e locks.  Instea
cb9f0 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a  d, add the file.
cba00 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70        ** descrip
cba10 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 50  tor to pOpen->aP
cba20 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c 6c  ending.  It will
cba30 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   be automaticall
cba40 79 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20 20  y closed when.  
cba50 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20      ** the last 
cba60 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e  lock is cleared.
cba70 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
cba80 69 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 20 20 20  int *aNew;.     
cba90 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e   struct unixOpen
cbaa0 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69  Cnt *pOpen = pFi
cbab0 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20  le->pOpen;.     
cbac0 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f   aNew = sqlite3_
cbad0 72 65 61 6c 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61  realloc(pOpen->a
cbae0 50 65 6e 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d  Pending, (pOpen-
cbaf0 3e 6e 50 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a  >nPending+1)*siz
cbb00 65 6f 66 28 69 6e 74 29 20 29 3b 0a 20 20 20 20  eof(int) );.    
cbb10 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b    if( aNew==0 ){
cbb20 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61  .        /* If a
cbb30 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a   malloc fails, j
cbb40 75 73 74 20 6c 65 61 6b 20 74 68 65 20 66 69 6c  ust leak the fil
cbb50 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a  e descriptor */.
cbb60 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
cbb70 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e       pOpen->aPen
cbb80 64 69 6e 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20  ding = aNew;.   
cbb90 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e       pOpen->aPen
cbba0 64 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e  ding[pOpen->nPen
cbbb0 64 69 6e 67 5d 20 3d 20 70 46 69 6c 65 2d 3e 68  ding] = pFile->h
cbbc0 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d  ;.        pOpen-
cbbd0 3e 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20  >nPending++;.   
cbbe0 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20       pFile->h = 
cbbf0 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
cbc00 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63  }.    releaseLoc
cbc10 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f  kInfo(pFile->pLo
cbc20 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65  ck);.    release
cbc30 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70  OpenCnt(pFile->p
cbc40 4f 70 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d 20  Open);.    rc = 
cbc50 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64  closeUnixFile(id
cbc60 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65  );.    unixLeave
cbc70 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72  Mutex();.  }.  r
cbc80 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a  eturn rc;.}../**
cbc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
cbca0 20 6f 66 20 74 68 65 20 70 6f 73 69 78 20 61 64   of the posix ad
cbcb0 76 69 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c  visory lock impl
cbcc0 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a  ementation *****
cbcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
cbce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
cbd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
cbd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d  ************ No-
cbda0 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a  op Locking *****
cbdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cbdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
cbdd0 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61 72 69 6f  .** Of the vario
cbde0 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65  us locking imple
cbdf0 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 6c  mentations avail
cbe00 61 62 6c 65 2c 20 74 68 69 73 20 69 73 20 62 79  able, this is by
cbe10 20 66 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70   far the.** simp
cbe20 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69  lest:  locking i
cbe30 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61  s ignored.  No a
cbe40 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
cbe50 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62  o lock the datab
cbe60 61 73 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20  ase.** file for 
cbe70 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
cbe80 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c  ng..**.** This l
cbe90 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 61  ocking mode is a
cbea0 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75  ppropriate for u
cbeb0 73 65 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20  se on read-only 
cbec0 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 78  databases.** (ex
cbed0 3a 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74  : databases that
cbee0 20 61 72 65 20 62 75 72 6e 65 64 20 69 6e 74 6f   are burned into
cbef0 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61   CD-ROM, for exa
cbf00 6d 70 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a  mple.)  It can.*
cbf10 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 69  * also be used i
cbf20 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
cbf30 6e 20 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65  n employs some e
cbf40 78 74 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73  xternal mechanis
cbf50 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20  m to.** prevent 
cbf60 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 63  simultaneous acc
cbf70 65 73 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ess of the same 
cbf80 64 61 74 61 62 61 73 65 20 62 79 20 74 77 6f 20  database by two 
cbf90 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62  or more.** datab
cbfa0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
cbfb0 20 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61    But there is a
cbfc0 20 73 65 72 69 6f 75 73 20 72 69 73 6b 20 6f 66   serious risk of
cbfd0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72   database.** cor
cbfe0 72 75 70 74 69 6f 6e 20 69 66 20 74 68 69 73 20  ruption if this 
cbff0 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20  locking mode is 
cc000 75 73 65 64 20 69 6e 20 73 69 74 75 61 74 69 6f  used in situatio
cc010 6e 73 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c  ns where multipl
cc020 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  e.** database co
cc030 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 61 63  nnections are ac
cc040 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65  cessing the same
cc050 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
cc060 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69  t the same.** ti
cc070 6d 65 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f  me and one or mo
cc080 72 65 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e  re of those conn
cc090 65 63 74 69 6f 6e 73 20 61 72 65 20 77 72 69 74  ections are writ
cc0a0 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20  ing..*/..static 
cc0b0 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52  int nolockCheckR
cc0c0 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
cc0d0 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65  te3_file *NotUse
cc0e0 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  d, int *pResOut)
cc0f0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
cc100 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
cc110 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20   *pResOut = 0;. 
cc120 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
cc130 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  K;.}.static int 
cc140 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74  nolockLock(sqlit
cc150 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64  e3_file *NotUsed
cc160 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b  , int NotUsed2){
cc170 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
cc180 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
cc190 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72  tUsed2);.  retur
cc1a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73  n SQLITE_OK;.}.s
cc1b0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b  tatic int nolock
cc1c0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
cc1d0 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  ile *NotUsed, in
cc1e0 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55  t NotUsed2){.  U
cc1f0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
cc200 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
cc210 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  d2);.  return SQ
cc220 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
cc230 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  * Close the file
cc240 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
cc250 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69  nolockClose(sqli
cc260 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a  te3_file *id) {.
cc270 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e    return closeUn
cc280 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f  ixFile(id);.}../
cc290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc2a0 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e  *** End of the n
cc2b0 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d  o-op lock implem
cc2c0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
cc2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
cc2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
cc330 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
cc340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
cc380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc390 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
cc3a0 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67  dot-file Locking
cc3b0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
cc3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
cc3d0 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c  **.** The dotfil
cc3e0 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d  e locking implem
cc3f0 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 74 68  entation uses th
cc400 65 20 65 78 69 73 74 69 6e 67 20 6f 66 20 73 65  e existing of se
cc410 70 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66  parate lock.** f
cc420 69 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f  iles in order to
cc430 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20   control access 
cc440 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
cc450 20 20 54 68 69 73 20 77 6f 72 6b 73 20 6f 6e 20    This works on 
cc460 6a 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 65 76  just.** about ev
cc470 65 72 79 20 66 69 6c 65 73 79 73 74 65 6d 20 69  ery filesystem i
cc480 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 74 20  maginable.  But 
cc490 74 68 65 72 65 20 61 72 65 20 73 65 72 69 6f 75  there are seriou
cc4a0 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a  s downsides:.**.
cc4b0 2a 2a 20 20 20 20 28 31 29 20 20 54 68 65 72 65  **    (1)  There
cc4c0 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 72 72   is zero concurr
cc4d0 65 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c 65 20  ency.  A single 
cc4e0 72 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 61 6c  reader blocks al
cc4f0 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20  l other.**      
cc500 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66     connections f
cc510 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 20 77  rom reading or w
cc520 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62  riting the datab
cc530 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32  ase..**.**    (2
cc540 29 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f  )  An applicatio
cc550 6e 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72  n crash or power
cc560 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20   loss can leave 
cc570 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73  stale lock files
cc580 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74  .**         sitt
cc590 69 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20  ing around that 
cc5a0 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72  need to be clear
cc5b0 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a  ed manually..**.
cc5c0 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c  ** Nevertheless,
cc5d0 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61 6e   a dotlock is an
cc5e0 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63   appropriate loc
cc5f0 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75 73  king mode for us
cc600 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72  e if no.** other
cc610 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
cc620 79 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  y is available..
cc630 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 6c 6f  **.** Dotfile lo
cc640 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 20 63  cking works by c
cc650 72 65 61 74 69 6e 67 20 61 20 66 69 6c 65 20 69  reating a file i
cc660 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63  n the same direc
cc670 74 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a 20 64  tory as the.** d
cc680 61 74 61 62 61 73 65 20 61 6e 64 20 77 69 74 68  atabase and with
cc690 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 62   the same name b
cc6a0 75 74 20 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b  ut with a ".lock
cc6b0 22 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 64 65  " extension adde
cc6c0 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61  d..** The exista
cc6d0 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 66 69  nce of a lock fi
cc6e0 6c 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58  le implies an EX
cc6f0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41  CLUSIVE lock.  A
cc700 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a  ll other lock.**
cc710 20 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20   types (SHARED, 
cc720 52 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e  RESERVED, PENDIN
cc730 47 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e  G) are mapped in
cc740 74 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f  to EXCLUSIVE..*/
cc750 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65  ../*.** The file
cc760 20 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f   suffix added to
cc770 20 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66   the data base f
cc780 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72  ilename in order
cc790 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a   to create the.*
cc7a0 2a 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a  * lock file..*/.
cc7b0 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f  #define DOTLOCK_
cc7c0 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a  SUFFIX ".lock"..
cc7d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
cc7e0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65  ne checks if the
cc7f0 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44  re is a RESERVED
cc800 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68   lock held on th
cc810 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66  e specified.** f
cc820 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61  ile by this or a
cc830 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
cc840 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b  . If such a lock
cc850 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70   is held, set *p
cc860 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e  ResOut.** to a n
cc870 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74  on-zero value ot
cc880 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74  herwise *pResOut
cc890 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e   is set to zero.
cc8a0 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
cc8b0 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  ue.** is set to 
cc8c0 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73  SQLITE_OK unless
cc8d0 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63   an I/O error oc
cc8e0 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b  curs during lock
cc8f0 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a   checking..**.**
cc900 20 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b   In dotfile lock
cc910 69 6e 67 2c 20 65 69 74 68 65 72 20 61 20 6c 6f  ing, either a lo
cc920 63 6b 20 65 78 69 73 74 73 20 6f 72 20 69 74 20  ck exists or it 
cc930 64 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 69 6e  does not.  So in
cc940 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 74 69   this.** variati
cc950 6f 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 65 72  on of CheckReser
cc960 76 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 65 73  vedLock(), *pRes
cc970 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 74 72  Out is set to tr
cc980 75 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a  ue if any lock.*
cc990 2a 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65  * is held on the
cc9a0 20 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 65 20   file and false 
cc9b0 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75  if the file is u
cc9c0 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74  nlocked..*/.stat
cc9d0 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 68  ic int dotlockCh
cc9e0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
cc9f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
cca00 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20  , int *pResOut) 
cca10 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
cca20 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65  ITE_OK;.  int re
cca30 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e  served = 0;.  un
cca40 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
cca50 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a  (unixFile*)id;..
cca60 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
cca70 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
cca80 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
cca90 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a  RVEDLOCK; );.  .
ccaa0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
ccab0 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69  );..  /* Check i
ccac0 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68  f a thread in th
ccad0 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  is process holds
ccae0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a   such a lock */.
ccaf0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
ccb00 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43  ktype>SHARED_LOC
ccb10 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68  K ){.    /* Eith
ccb20 65 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  er this connecti
ccb30 6f 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  on or some other
ccb40 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74   connection in t
ccb50 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 0a  he same process.
ccb60 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c      ** holds a l
ccb70 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e  ock on the file.
ccb80 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65    No need to che
ccb90 63 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20  ck further. */. 
ccba0 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b     reserved = 1;
ccbb0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
ccbc0 20 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c   The lock is hel
ccbd0 64 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66  d if and only if
ccbe0 20 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78   the lockfile ex
ccbf0 69 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73  ists */.    cons
ccc00 74 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c  t char *zLockFil
ccc10 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a  e = (const char*
ccc20 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
ccc30 6f 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 73 65  ontext;.    rese
ccc40 72 76 65 64 20 3d 20 61 63 63 65 73 73 28 7a 4c  rved = access(zL
ccc50 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a  ockFile, 0)==0;.
ccc60 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22    }.  OSTRACE4("
ccc70 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20  TEST WR-LOCK %d 
ccc80 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  %d %d\n", pFile-
ccc90 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64  >h, rc, reserved
ccca0 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20  );.  *pResOut = 
cccb0 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75  reserved;.  retu
cccc0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
cccd0 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69  Lock the file wi
ccce0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63  th the lock spec
cccf0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
ccd00 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e  er locktype - on
ccd10 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  e.** of the foll
ccd20 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
ccd30 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b   (1) SHARED_LOCK
ccd40 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45  .**     (2) RESE
ccd50 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  RVED_LOCK.**    
ccd60 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   (3) PENDING_LOC
ccd70 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43  K.**     (4) EXC
ccd80 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a  LUSIVE_LOCK.**.*
ccd90 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e  * Sometimes when
ccda0 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20   requesting one 
ccdb0 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69  lock state, addi
ccdc0 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74  tional lock stat
ccdd0 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74  es.** are insert
ccde0 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20  ed in between.  
ccdf0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68  The locking migh
cce00 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66  t fail on one of
cce10 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72   the later.** tr
cce20 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e  ansitions leavin
cce30 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65  g the lock state
cce40 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
cce50 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20  what it started 
cce60 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f  but.** still sho
cce70 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20  rt of its goal. 
cce80 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
cce90 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61  hart shows the a
ccea0 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69  llowed.** transi
cceb0 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e  tions and the in
ccec0 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69  serted intermedi
cced0 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a  ate states:.**.*
ccee0 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e  *    UNLOCKED ->
ccef0 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48   SHARED.**    SH
ccf00 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44  ARED -> RESERVED
ccf10 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
ccf20 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
ccf30 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45  CLUSIVE.**    RE
ccf40 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49  SERVED -> (PENDI
ccf50 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
ccf60 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d  .**    PENDING -
ccf70 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a  > EXCLUSIVE.**.*
ccf80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
ccf90 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73  ill only increas
ccfa0 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74  e a lock.  Use t
ccfb0 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  he sqlite3OsUnlo
ccfc0 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  ck().** routine 
ccfd0 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69  to lower a locki
ccfe0 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20  ng level..**.** 
ccff0 57 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f 63  With dotfile loc
cd000 6b 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 20  king, we really 
cd010 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 61  only support sta
cd020 74 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 56  te (4): EXCLUSIV
cd030 45 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 61  E..** But we tra
cd040 63 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f 63  ck the other loc
cd050 6b 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 65  king levels inte
cd060 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69  rnally..*/.stati
cd070 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63  c int dotlockLoc
cd080 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
cd090 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
cd0a0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
cd0b0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
cd0c0 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b  e*)id;.  int fd;
cd0d0 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69  .  char *zLockFi
cd0e0 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69  le = (char *)pFi
cd0f0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
cd100 78 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  xt;.  int rc = S
cd110 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a  QLITE_OK;...  /*
cd120 20 49 66 20 77 65 20 68 61 76 65 20 61 6e 79 20   If we have any 
cd130 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 6c  lock, then the l
cd140 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 64 79  ock file already
cd150 20 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 77 65   exists.  All we
cd160 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 6f   have.  ** to do
cd170 20 69 73 20 61 64 6a 75 73 74 20 6f 75 72 20 69   is adjust our i
cd180 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f  nternal record o
cd190 66 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 65 6c  f the lock level
cd1a0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ..  */.  if( pFi
cd1b0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e  le->locktype > N
cd1c0 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46  O_LOCK ){.    pF
cd1d0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
cd1e0 6c 6f 63 6b 74 79 70 65 3b 0a 23 69 66 20 21 4f  locktype;.#if !O
cd1f0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 2f 2a  S_VXWORKS.    /*
cd200 20 41 6c 77 61 79 73 20 75 70 64 61 74 65 20 74   Always update t
cd210 68 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e 20  he timestamp on 
cd220 74 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a  the old file */.
cd230 20 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b      utimes(zLock
cd240 46 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e  File, NULL);.#en
cd250 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53  dif.    return S
cd260 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
cd270 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78  .  /* grab an ex
cd280 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a  clusive lock */.
cd290 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63    fd = open(zLoc
cd2a0 6b 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f  kFile,O_RDONLY|O
cd2b0 5f 43 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36  _CREAT|O_EXCL,06
cd2c0 30 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20  00);.  if( fd<0 
cd2d0 29 7b 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64  ){.    /* failed
cd2e0 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20   to open/create 
cd2f0 74 68 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e  the file, someon
cd300 65 20 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20  e else may have 
cd310 73 74 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20  stolen the lock 
cd320 2a 2f 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e  */.    int tErrn
cd330 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69  o = errno;.    i
cd340 66 28 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72  f( EEXIST == tEr
cd350 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20  rno ){.      rc 
cd360 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
cd370 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
cd380 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
cd390 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
cd3a0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
cd3b0 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20  IOERR_LOCK);.   
cd3c0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
cd3d0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
cd3e0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
cd3f0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
cd400 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
cd410 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20   return rc;.  } 
cd420 0a 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29  .  if( close(fd)
cd430 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
cd440 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
cd450 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
cd460 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20  E_IOERR_CLOSE;. 
cd470 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69   }.  .  /* got i
cd480 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20  t, set the type 
cd490 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f  and return ok */
cd4a0 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  .  pFile->lockty
cd4b0 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
cd4c0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
cd4d0 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c  *.** Lower the l
cd4e0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20  ocking level on 
cd4f0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
cd500 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70  pFile to locktyp
cd510 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20  e.  locktype.** 
cd520 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e  must be either N
cd530 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44  O_LOCK or SHARED
cd540 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  _LOCK..**.** If 
cd550 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
cd560 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65  l of the file de
cd570 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65  scriptor is alre
cd580 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a  ady at or below.
cd590 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64  ** the requested
cd5a0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20   locking level, 
cd5b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
cd5c0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57  a no-op..**.** W
cd5d0 68 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  hen the locking 
cd5e0 6c 65 76 65 6c 20 72 65 61 63 68 65 73 20 4e 4f  level reaches NO
cd5f0 5f 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 74 68  _LOCK, delete th
cd600 65 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a  e lock file..*/.
cd610 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f  static int dotlo
cd620 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  ckUnlock(sqlite3
cd630 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
cd640 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69  ocktype) {.  uni
cd650 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
cd660 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
cd670 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20  char *zLockFile 
cd680 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d  = (char *)pFile-
cd690 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
cd6a0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
cd6b0 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28  e );.  OSTRACE5(
cd6c0 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77  "UNLOCK  %d %d w
cd6d0 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c  as %d pid=%d\n",
cd6e0 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
cd6f0 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e  ype,..   pFile->
cd700 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64  locktype, getpid
cd710 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  ());.  assert( l
cd720 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f  ocktype<=SHARED_
cd730 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20  LOCK );.  .  /* 
cd740 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c  no-op if possibl
cd750 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  e */.  if( pFile
cd760 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b  ->locktype==lock
cd770 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75  type ){.    retu
cd780 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
cd790 7d 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67  }..  /* To downg
cd7a0 72 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20  rade to shared, 
cd7b0 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75  simply update ou
cd7c0 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f  r internal notio
cd7d0 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f  n of the.  ** lo
cd7e0 63 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65  ck state.  No ne
cd7f0 65 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20  ed to mess with 
cd800 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b  the file on disk
cd810 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  ..  */.  if( loc
cd820 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
cd830 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK ){.    pFile-
cd840 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  >locktype = SHAR
cd850 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74  ED_LOCK;.    ret
cd860 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
cd870 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75   }.  .  /* To fu
cd880 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64  lly unlock the d
cd890 61 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20  atabase, delete 
cd8a0 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f  the lock file */
cd8b0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
cd8c0 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a  ype==NO_LOCK );.
cd8d0 20 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f    if( unlink(zLo
cd8e0 63 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69  ckFile) ){.    i
cd8f0 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20  nt rc, tErrno = 
cd900 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45  errno;.    if( E
cd910 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20  NOENT != tErrno 
cd920 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
cd930 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
cd940 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
cd950 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
cd960 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OCK);.    }.    
cd970 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
cd980 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70  R(rc) ){.      p
cd990 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
cd9a0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a  = tErrno;.    }.
cd9b0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a      return rc; .
cd9c0 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63    }.  pFile->loc
cd9d0 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b  ktype = NO_LOCK;
cd9e0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
cd9f0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
cda00 6f 73 65 20 61 20 66 69 6c 65 2e 20 20 4d 61 6b  ose a file.  Mak
cda10 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 20  e sure the lock 
cda20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65  has been release
cda30 64 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67  d before closing
cda40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
cda50 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c  dotlockClose(sql
cda60 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
cda70 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
cda80 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46   id ){.    unixF
cda90 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
cdaa0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20  ixFile*)id;.    
cdab0 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64  dotlockUnlock(id
cdac0 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  , NO_LOCK);.    
cdad0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69  sqlite3_free(pFi
cdae0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
cdaf0 78 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  xt);.  }.  rc = 
cdb00 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64  closeUnixFile(id
cdb10 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
cdb20 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }./*************
cdb30 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65  ***** End of the
cdb40 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20 69   dot-file lock i
cdb50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a  mplementation **
cdb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdb70 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
cdb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdbc0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
cdbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdc10 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
cdc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
cdc30 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b 69 6e  gin flock Lockin
cdc40 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  g **************
cdc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdc60 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65  **.**.** Use the
cdc70 20 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 6d 20   flock() system 
cdc80 63 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c 65 20  call to do file 
cdc90 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66  locking..**.** f
cdca0 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 69  lock() locking i
cdcb0 73 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c 65 20  s like dot-file 
cdcc0 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 74 20  locking in that 
cdcd0 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 66  the various.** f
cdce0 69 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b 69 6e  ine-grain lockin
cdcf0 67 20 6c 65 76 65 6c 73 20 73 75 70 70 6f 72 74  g levels support
cdd00 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 65  ed by SQLite are
cdd10 20 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 6f 0a   collapsed into.
cdd20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 63 6c  ** a single excl
cdd30 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 6e 20  usive lock.  In 
cdd40 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 53 48 41  other words, SHA
cdd50 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 61  RED, RESERVED, a
cdd60 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 6c 6f  nd.** PENDING lo
cdd70 63 6b 73 20 61 72 65 20 74 68 65 20 73 61 6d 65  cks are the same
cdd80 20 74 68 69 6e 67 20 61 73 20 61 6e 20 45 58 43   thing as an EXC
cdd90 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 53 51  LUSIVE lock.  SQ
cdda0 4c 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f  Lite.** still wo
cddb0 72 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 6f 20  rks when you do 
cddc0 74 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 75 72  this, but concur
cddd0 72 65 6e 63 79 20 69 73 20 72 65 64 75 63 65 64  rency is reduced
cdde0 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 20 61   since.** only a
cddf0 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20   single process 
cde00 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 74  can be reading t
cde10 68 65 20 64 61 74 61 62 61 73 65 20 61 74 20 61  he database at a
cde20 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69   time..**.** Omi
cde30 74 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20 69  t this section i
cde40 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
cde50 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73  LOCKING_STYLE is
cde60 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 69   turned off or i
cde70 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66  f.** compiling f
cde80 6f 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f 0a 23  or VXWORKS..*/.#
cde90 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
cdea0 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26  _LOCKING_STYLE &
cdeb0 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f  & !OS_VXWORKS../
cdec0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
cded0 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
cdee0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
cdef0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
cdf00 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
cdf10 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
cdf20 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
cdf30 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
cdf40 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52  is held, set *pR
cdf50 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f  esOut.** to a no
cdf60 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68  n-zero value oth
cdf70 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20  erwise *pResOut 
cdf80 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20  is set to zero. 
cdf90 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
cdfa0 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  e.** is set to S
cdfb0 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20  QLITE_OK unless 
cdfc0 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  an I/O error occ
cdfd0 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20  urs during lock 
cdfe0 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61  checking..*/.sta
cdff0 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65  tic int flockChe
ce000 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
ce010 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
ce020 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a   int *pResOut){.
ce030 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
ce040 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65  E_OK;.  int rese
ce050 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78  rved = 0;.  unix
ce060 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
ce070 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a  nixFile*)id;.  .
ce080 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
ce090 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
ce0a0 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
ce0b0 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a  RVEDLOCK; );.  .
ce0c0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
ce0d0 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b  );.  .  /* Check
ce0e0 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
ce0f0 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
ce100 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
ce110 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
ce120 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
ce130 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72  OCK ){.    reser
ce140 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a  ved = 1;.  }.  .
ce150 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73    /* Otherwise s
ce160 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  ee if some other
ce170 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69   process holds i
ce180 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73  t. */.  if( !res
ce190 65 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20  erved ){.    /* 
ce1a0 61 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74  attempt to get t
ce1b0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69  he lock */.    i
ce1c0 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70  nt lrc = flock(p
ce1d0 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58  File->h, LOCK_EX
ce1e0 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20   | LOCK_NB);.   
ce1f0 20 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 20 20   if( !lrc ){.   
ce200 20 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 6c 6f     /* got the lo
ce210 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f  ck, unlock it */
ce220 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 66 6c 6f  .      lrc = flo
ce230 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43  ck(pFile->h, LOC
ce240 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 66 20  K_UN);.      if 
ce250 28 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 20 20  ( lrc ) {.      
ce260 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
ce270 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a  rrno;.        /*
ce280 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77   unlock failed w
ce290 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ith an error */.
ce2a0 20 20 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71          lrc = sq
ce2b0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
ce2c0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
ce2d0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
ce2e0 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69  OCK); .        i
ce2f0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
ce300 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20  (lrc) ){.       
ce310 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
ce320 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
ce330 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63          rc = lrc
ce340 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
ce350 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b    }.    } else {
ce360 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e  .      int tErrn
ce370 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
ce380 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20   reserved = 1;. 
ce390 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20       /* someone 
ce3a0 65 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65 20  else might have 
ce3b0 69 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20  it reserved */. 
ce3c0 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74       lrc = sqlit
ce3d0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
ce3e0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
ce3f0 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b  ITE_IOERR_LOCK);
ce400 20 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c   .      if( IS_L
ce410 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29  OCK_ERROR(lrc) )
ce420 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
ce430 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
ce440 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20  rno;.        rc 
ce450 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  = lrc;.      }. 
ce460 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41     }.  }.  OSTRA
ce470 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43  CE4("TEST WR-LOC
ce480 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70  K %d %d %d\n", p
ce490 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73  File->h, rc, res
ce4a0 65 72 76 65 64 29 3b 0a 0a 23 69 66 64 65 66 20  erved);..#ifdef 
ce4b0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c  SQLITE_IGNORE_FL
ce4c0 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a  OCK_LOCK_ERRORS.
ce4d0 20 20 69 66 28 20 28 72 63 20 26 20 53 51 4c 49    if( (rc & SQLI
ce4e0 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c  TE_IOERR) == SQL
ce4f0 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20  ITE_IOERR ){.   
ce500 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
ce510 0a 20 20 20 20 72 65 73 65 72 76 65 64 3d 31 3b  .    reserved=1;
ce520 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
ce530 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
ce540 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a  CK_LOCK_ERRORS *
ce550 2f 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72  /.  *pResOut = r
ce560 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72  eserved;.  retur
ce570 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
ce580 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74  ock the file wit
ce590 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69  h the lock speci
ce5a0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
ce5b0 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65  r locktype - one
ce5c0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
ce5d0 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
ce5e0 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a  (1) SHARED_LOCK.
ce5f0 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52  **     (2) RESER
ce600 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  VED_LOCK.**     
ce610 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  (3) PENDING_LOCK
ce620 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c  .**     (4) EXCL
ce630 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a  USIVE_LOCK.**.**
ce640 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20   Sometimes when 
ce650 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c  requesting one l
ce660 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74  ock state, addit
ce670 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65  ional lock state
ce680 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65  s.** are inserte
ce690 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54  d in between.  T
ce6a0 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74  he locking might
ce6b0 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20   fail on one of 
ce6c0 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61  the later.** tra
ce6d0 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67  nsitions leaving
ce6e0 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20   the lock state 
ce6f0 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77  different from w
ce700 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62  hat it started b
ce710 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72  ut.** still shor
ce720 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20  t of its goal.  
ce730 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  The following ch
ce740 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c  art shows the al
ce750 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74  lowed.** transit
ce760 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73  ions and the ins
ce770 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61  erted intermedia
ce780 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  te states:.**.**
ce790 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20      UNLOCKED -> 
ce7a0 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41  SHARED.**    SHA
ce7b0 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a  RED -> RESERVED.
ce7c0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
ce7d0 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
ce7e0 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53  LUSIVE.**    RES
ce7f0 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  ERVED -> (PENDIN
ce800 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
ce810 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e  **    PENDING ->
ce820 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a   EXCLUSIVE.**.**
ce830 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 65   flock() only re
ce840 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 43  ally support EXC
ce850 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57  LUSIVE locks.  W
ce860 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 64  e track intermed
ce870 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61  iate.** lock sta
ce880 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74  tes in the sqlit
ce890 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72  e3_file structur
ce8a0 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73  e, but all locks
ce8b0 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62   SHARED or.** ab
ce8c0 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45  ove are really E
ce8d0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61  XCLUSIVE locks a
ce8e0 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f  nd exclude all o
ce8f0 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66  ther processes f
ce900 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68  rom.** access th
ce910 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
ce920 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
ce930 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
ce940 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73  lock.  Use the s
ce950 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29  qlite3OsUnlock()
ce960 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c  .** routine to l
ce970 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c  ower a locking l
ce980 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  evel..*/.static 
ce990 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73 71  int flockLock(sq
ce9a0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
ce9b0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
ce9c0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
ce9d0 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65  E_OK;.  unixFile
ce9e0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
ce9f0 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65  ile*)id;..  asse
cea00 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20  rt( pFile );..  
cea10 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79  /* if we already
cea20 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74   have a lock, it
cea30 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20   is exclusive.  
cea40 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73  .  ** Just adjus
cea50 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74  t level and punt
cea60 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20   on outta here. 
cea70 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  */.  if (pFile->
cea80 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f  locktype > NO_LO
cea90 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK) {.    pFile-
ceaa0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
ceab0 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e  type;.    return
ceac0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
cead0 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20    .  /* grab an 
ceae0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a  exclusive lock *
ceaf0 2f 0a 20 20 0a 20 20 69 66 20 28 66 6c 6f 63 6b  /.  .  if (flock
ceb00 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f  (pFile->h, LOCK_
ceb10 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b  EX | LOCK_NB)) {
ceb20 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20  .    int tErrno 
ceb30 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a 20  = errno;.    /* 
ceb40 64 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74  didn't get, must
ceb50 20 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20   be busy */.    
ceb60 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
ceb70 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
ceb80 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
ceb90 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69  ERR_LOCK);.    i
ceba0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
cebb0 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46  (rc) ){.      pF
cebc0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
cebd0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20   tErrno;.    }. 
cebe0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a   } else {.    /*
cebf0 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65   got it, set the
cec00 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e   type and return
cec10 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65   ok */.    pFile
cec20 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
cec30 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 4f 53 54  ktype;.  }.  OST
cec40 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25  RACE4("LOCK    %
cec50 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c  d %s %s\n", pFil
cec60 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61  e->h, locktypeNa
cec70 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20  me(locktype), . 
cec80 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51            rc==SQ
cec90 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a  LITE_OK ? "ok" :
ceca0 20 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 66 64   "failed");.#ifd
cecb0 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  ef SQLITE_IGNORE
cecc0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
cecd0 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53  RS.  if( (rc & S
cece0 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20  QLITE_IOERR) == 
cecf0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a  SQLITE_IOERR ){.
ced00 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
ced10 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  BUSY;.  }.#endif
ced20 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52   /* SQLITE_IGNOR
ced30 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52  E_FLOCK_LOCK_ERR
ced40 4f 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ORS */.  return 
ced50 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  rc;.}.../*.** Lo
ced60 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
ced70 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
ced80 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74  scriptor pFile t
ced90 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63  o locktype.  loc
ceda0 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65  ktype.** must be
cedb0 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20   either NO_LOCK 
cedc0 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a  or SHARED_LOCK..
cedd0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63  **.** If the loc
cede0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68  king level of th
cedf0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
cee00 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20  r is already at 
cee10 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20  or below.** the 
cee20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e  requested lockin
cee30 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f  g level, this ro
cee40 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
cee50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
cee60 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69  flockUnlock(sqli
cee70 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
cee80 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20  t locktype) {.  
cee90 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
ceea0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
ceeb0 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46  .  .  assert( pF
ceec0 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  ile );.  OSTRACE
ceed0 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64  5("UNLOCK  %d %d
ceee0 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e   was %d pid=%d\n
ceef0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
cef00 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20  ktype,.         
cef10 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
cef20 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20  e, getpid());.  
cef30 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
cef40 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
cef50 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69  .  .  /* no-op i
cef60 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20  f possible */.  
cef70 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
cef80 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype==locktype ){
cef90 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
cefa0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
cefb0 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75  /* shared can ju
cefc0 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73  st be set becaus
cefd0 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65  e we always have
cefe0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f   an exclusive */
ceff0 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d  .  if (locktype=
cf000 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a  =SHARED_LOCK) {.
cf010 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
cf020 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
cf030 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
cf040 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
cf050 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e  * no, really, un
cf060 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72  lock. */.  int r
cf070 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d  c = flock(pFile-
cf080 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20  >h, LOCK_UN);.  
cf090 69 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69 6e  if (rc) {.    in
cf0a0 74 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72  t r, tErrno = er
cf0b0 72 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71 6c  rno;.    r = sql
cf0c0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
cf0d0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
cf0e0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
cf0f0 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f  CK);.    if( IS_
cf100 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29 7b  LOCK_ERROR(r) ){
cf110 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
cf120 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
cf130 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  ;.    }.#ifdef S
cf140 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
cf150 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20  CK_LOCK_ERRORS. 
cf160 20 20 20 69 66 28 20 28 72 20 26 20 53 51 4c 49     if( (r & SQLI
cf170 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c  TE_IOERR) == SQL
cf180 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20  ITE_IOERR ){.   
cf190 20 20 20 72 20 3d 20 53 51 4c 49 54 45 5f 42 55     r = SQLITE_BU
cf1a0 53 59 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  SY;.    }.#endif
cf1b0 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52   /* SQLITE_IGNOR
cf1c0 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52  E_FLOCK_LOCK_ERR
cf1d0 4f 52 53 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20  ORS */.    .    
cf1e0 72 65 74 75 72 6e 20 72 3b 0a 20 20 7d 20 65 6c  return r;.  } el
cf1f0 73 65 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  se {.    pFile->
cf200 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f  locktype = NO_LO
cf210 43 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  CK;.    return S
cf220 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a  QLITE_OK;.  }.}.
cf230 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66  ./*.** Close a f
cf240 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
cf250 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71  nt flockClose(sq
cf260 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20  lite3_file *id) 
cf270 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20  {.  if( id ){.  
cf280 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64    flockUnlock(id
cf290 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a  , NO_LOCK);.  }.
cf2a0 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e    return closeUn
cf2b0 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23  ixFile(id);.}..#
cf2c0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
cf2d0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
cf2e0 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f  TYLE && !OS_VXWO
cf2f0 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  RK */../********
cf300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
cf310 6f 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f 63  of the flock loc
cf320 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
cf330 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
cf340 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
cf350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf390 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  *****/../*******
cf3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf3e0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
cf3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf400 20 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 6d   Begin Named Sem
cf410 61 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 2a  aphore Locking *
cf420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf430 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61  *******.**.** Na
cf440 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f  med semaphore lo
cf450 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 75  cking is only su
cf460 70 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f 72  pported on VxWor
cf470 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68  ks..**.** Semaph
cf480 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c  ore locking is l
cf490 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e 64  ike dot-lock and
cf4a0 20 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 69   flock in that i
cf4b0 74 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a  t really only.**
cf4c0 20 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 53   supports EXCLUS
cf4d0 49 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f 6e  IVE locking.  On
cf4e0 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63  ly a single proc
cf4f0 65 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 20  ess can read or 
cf500 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 74  write.** the dat
cf510 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 20  abase file at a 
cf520 74 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 75  time.  This redu
cf530 63 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 6f  ces potential co
cf540 6e 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a 2a  ncurrency, but.*
cf550 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 6b  * makes the lock
cf560 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
cf570 6d 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f 0a  much easier..*/.
cf580 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a  #if OS_VXWORKS..
cf590 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
cf5a0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65  ne checks if the
cf5b0 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44  re is a RESERVED
cf5c0 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68   lock held on th
cf5d0 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66  e specified.** f
cf5e0 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61  ile by this or a
cf5f0 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
cf600 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b  . If such a lock
cf610 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70   is held, set *p
cf620 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e  ResOut.** to a n
cf630 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74  on-zero value ot
cf640 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74  herwise *pResOut
cf650 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e   is set to zero.
cf660 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
cf670 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  ue.** is set to 
cf680 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73  SQLITE_OK unless
cf690 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63   an I/O error oc
cf6a0 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b  curs during lock
cf6b0 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74   checking..*/.st
cf6c0 61 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65 63  atic int semChec
cf6d0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
cf6e0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
cf6f0 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a  int *pResOut) {.
cf700 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
cf710 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65  E_OK;.  int rese
cf720 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78  rved = 0;.  unix
cf730 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
cf740 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20  nixFile*)id;..  
cf750 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
cf760 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
cf770 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
cf780 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20  EDLOCK; );.  .  
cf790 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
cf7a0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20  ..  /* Check if 
cf7b0 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  a thread in this
cf7c0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73   process holds s
cf7d0 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20  uch a lock */.  
cf7e0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
cf7f0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
cf800 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20  ){.    reserved 
cf810 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  = 1;.  }.  .  /*
cf820 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
cf830 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
cf840 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a  cess holds it. *
cf850 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65  /.  if( !reserve
cf860 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20 2a  d ){.    sem_t *
cf870 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f  pSem = pFile->pO
cf880 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 20 20 73  pen->pSem;.    s
cf890 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74 42  truct stat statB
cf8a0 75 66 3b 0a 0a 20 20 20 20 69 66 28 20 73 65 6d  uf;..    if( sem
cf8b0 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d  _trywait(pSem)==
cf8c0 2d 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  -1 ){.      int 
cf8d0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
cf8e0 20 20 20 20 20 20 69 66 28 20 45 41 47 41 49 4e        if( EAGAIN
cf8f0 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20   != tErrno ){.  
cf900 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
cf910 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
cf920 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
cf930 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
cf940 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20  ESERVEDLOCK);.  
cf950 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
cf960 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
cf970 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  .      } else {.
cf980 20 20 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f          /* someo
cf990 6e 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20  ne else has the 
cf9a0 6c 6f 63 6b 20 77 68 65 6e 20 77 65 20 61 72 65  lock when we are
cf9b0 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20   in NO_LOCK */. 
cf9c0 20 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20         reserved 
cf9d0 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  = (pFile->lockty
cf9e0 70 65 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43 4b  pe < SHARED_LOCK
cf9f0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
cfa00 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 77  else{.      /* w
cfa10 65 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74 20  e could have it 
cfa20 69 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a 2f  if we want it */
cfa30 0a 20 20 20 20 20 20 73 65 6d 5f 70 6f 73 74 28  .      sem_post(
cfa40 70 53 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pSem);.    }.  }
cfa50 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53  .  OSTRACE4("TES
cfa60 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
cfa70 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
cfa80 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a   rc, reserved);.
cfa90 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65  .  *pResOut = re
cfaa0 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e  served;.  return
cfab0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
cfac0 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68  ck the file with
cfad0 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66   the lock specif
cfae0 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
cfaf0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a   locktype - one.
cfb00 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
cfb10 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ing:.**.**     (
cfb20 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a  1) SHARED_LOCK.*
cfb30 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56  *     (2) RESERV
cfb40 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
cfb50 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a  3) PENDING_LOCK.
cfb60 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
cfb70 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20  SIVE_LOCK.**.** 
cfb80 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
cfb90 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
cfba0 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
cfbb0 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
cfbc0 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
cfbd0 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68   in between.  Th
cfbe0 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20  e locking might 
cfbf0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  fail on one of t
cfc00 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e  he later.** tran
cfc10 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
cfc20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
cfc30 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
cfc40 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
cfc50 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  t.** still short
cfc60 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
cfc70 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
cfc80 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
cfc90 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69  owed.** transiti
cfca0 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65  ons and the inse
cfcb0 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74  rted intermediat
cfcc0 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
cfcd0 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53     UNLOCKED -> S
cfce0 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  HARED.**    SHAR
cfcf0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a  ED -> RESERVED.*
cfd00 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28  *    SHARED -> (
cfd10 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
cfd20 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45  USIVE.**    RESE
cfd30 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  RVED -> (PENDING
cfd40 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
cfd50 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
cfd60 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20  EXCLUSIVE.**.** 
cfd70 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20  Semaphore locks 
cfd80 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70  only really supp
cfd90 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f  ort EXCLUSIVE lo
cfda0 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69  cks.  We track i
cfdb0 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c  ntermediate.** l
cfdc0 6f 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68  ock states in th
cfdd0 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73  e sqlite3_file s
cfde0 74 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c  tructure, but al
cfdf0 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f  l locks SHARED o
cfe00 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72  r.** above are r
cfe10 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20  eally EXCLUSIVE 
cfe20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64  locks and exclud
cfe30 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63  e all other proc
cfe40 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63  esses from.** ac
cfe50 63 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a  cess the file..*
cfe60 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
cfe70 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72  e will only incr
cfe80 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73  ease a lock.  Us
cfe90 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55  e the sqlite3OsU
cfea0 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69  nlock().** routi
cfeb0 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f  ne to lower a lo
cfec0 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a  cking level..*/.
cfed0 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c 6f  static int semLo
cfee0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
cfef0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
cff00 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  e) {.  unixFile 
cff10 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
cff20 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64  le*)id;.  int fd
cff30 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20  ;.  sem_t *pSem 
cff40 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e  = pFile->pOpen->
cff50 70 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pSem;.  int rc =
cff60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f   SQLITE_OK;..  /
cff70 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20  * if we already 
cff80 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20  have a lock, it 
cff90 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a  is exclusive.  .
cffa0 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74    ** Just adjust
cffb0 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20   level and punt 
cffc0 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a  on outta here. *
cffd0 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c  /.  if (pFile->l
cffe0 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43  ocktype > NO_LOC
cfff0 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K) {.    pFile->
d0000 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
d0010 79 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  ype;.    rc = SQ
d0020 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74  LITE_OK;.    got
d0030 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  o sem_end_lock;.
d0040 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 6b    }.  .  /* lock
d0050 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62   semaphore now b
d0060 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 6e  ut bail out when
d0070 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e   already locked.
d0080 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 72   */.  if( sem_tr
d0090 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20  ywait(pSem)==-1 
d00a0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
d00b0 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74  TE_BUSY;.    got
d00c0 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  o sem_end_lock;.
d00d0 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74    }..  /* got it
d00e0 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61  , set the type a
d00f0 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a  nd return ok */.
d0100 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d0110 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20  e = locktype;.. 
d0120 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20  sem_end_lock:.  
d0130 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d0140 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
d0150 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
d0160 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
d0170 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
d0180 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
d0190 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
d01a0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
d01b0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
d01c0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
d01d0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
d01e0 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
d01f0 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
d0200 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
d0210 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
d0220 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
d0230 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
d0240 63 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b 28  c int semUnlock(
d0250 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d0260 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
d0270 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
d0280 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d0290 29 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53  )id;.  sem_t *pS
d02a0 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  em = pFile->pOpe
d02b0 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 65  n->pSem;..  asse
d02c0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61  rt( pFile );.  a
d02d0 73 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a 20  ssert( pSem );. 
d02e0 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43   OSTRACE5("UNLOC
d02f0 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20  K  %d %d was %d 
d0300 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  pid=%d\n", pFile
d0310 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09  ->h, locktype,..
d0320 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d0330 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20  pe, getpid());. 
d0340 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d0350 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
d0360 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20  ;.  .  /* no-op 
d0370 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20  if possible */. 
d0380 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d0390 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29  type==locktype )
d03a0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
d03b0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20  ITE_OK;.  }.  . 
d03c0 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a   /* shared can j
d03d0 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75  ust be set becau
d03e0 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76  se we always hav
d03f0 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a  e an exclusive *
d0400 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65  /.  if (locktype
d0410 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b  ==SHARED_LOCK) {
d0420 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d0430 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d0440 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d0450 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d0460 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e  /* no, really un
d0470 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20  lock. */.  if ( 
d0480 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d  sem_post(pSem)==
d0490 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72  -1 ) {.    int r
d04a0 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  c, tErrno = errn
d04b0 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  o;.    rc = sqli
d04c0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d04d0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d04e0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
d04f0 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c  K);.    if( IS_L
d0500 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
d0510 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
d0520 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d0530 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
d0540 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46  rn rc; .  }.  pF
d0550 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
d0560 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72  NO_LOCK;.  retur
d0570 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d0580 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66  /*. ** Close a f
d0590 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20  ile.. */.static 
d05a0 69 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c  int semClose(sql
d05b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
d05c0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20  .  if( id ){.   
d05d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d05e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d05f0 3b 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28  ;.    semUnlock(
d0600 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20  id, NO_LOCK);.  
d0610 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
d0620 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72  );.    unixEnter
d0630 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c  Mutex();.    rel
d0640 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69  easeLockInfo(pFi
d0650 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20  le->pLock);.    
d0660 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70  releaseOpenCnt(p
d0670 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20  File->pOpen);.  
d0680 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28    closeUnixFile(
d0690 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61  id);.    unixLea
d06a0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20  veMutex();.  }. 
d06b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d06c0 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  K;.}..#endif /* 
d06d0 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a  OS_VXWORKS */./*
d06e0 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68  .** Named semaph
d06f0 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f  ore locking is o
d0700 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e  nly available on
d0710 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a   VxWorks..**.***
d0720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
d0730 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 65   of the named se
d0740 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d 70  maphore lock imp
d0750 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a  lementation ****
d0760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
d0770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d07a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d07b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f  ***********/.../
d07c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d07d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d07e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d07f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
d0810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
d0830 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a   AFP Locking ***
d0840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
d0860 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 20  *.** AFP is the 
d0870 41 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 6f  Apple Filing Pro
d0880 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 61  tocol.  AFP is a
d0890 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73   network filesys
d08a0 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e 20  tem found.** on 
d08b0 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20  Apple Macintosh 
d08c0 63 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 68  computers - both
d08d0 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a 2a   OS9 and OSX..**
d08e0 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20  .** Third-party 
d08f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
d0900 6f 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c  of AFP are avail
d0910 61 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20  able.  But this 
d0920 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e 6c  code here.** onl
d0930 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0a  y works on OSX..
d0940 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
d0950 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51  __APPLE__) && SQ
d0960 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
d0970 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20  ING_STYLE./*.** 
d0980 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  The afpLockingCo
d0990 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20  ntext structure 
d09a0 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 70  contains all afp
d09b0 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 73   lock specific s
d09c0 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20  tate.*/.typedef 
d09d0 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e  struct afpLockin
d09e0 67 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 6b  gContext afpLock
d09f0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75  ingContext;.stru
d0a00 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  ct afpLockingCon
d0a10 74 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e 65  text {.  unsigne
d0a20 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72  d long long shar
d0a30 65 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20  edByte;.  const 
d0a40 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20  char *dbPath;   
d0a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
d0a60 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69  e of the open fi
d0a70 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74  le */.};..struct
d0a80 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42   ByteRangeLockPB
d0a90 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c  2.{.  unsigned l
d0aa0 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b  ong long offset;
d0ab0 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65          /* offse
d0ac0 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20  t to first byte 
d0ad0 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73  to lock */.  uns
d0ae0 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
d0af0 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f  length;        /
d0b00 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74  * nbr of bytes t
d0b10 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69  o lock */.  unsi
d0b20 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72  gned long long r
d0b30 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a  etRangeStart; /*
d0b40 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65   nbr of 1st byte
d0b50 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65   locked if succe
d0b60 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67  ssful */.  unsig
d0b70 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46  ned char unLockF
d0b80 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lag;         /* 
d0b90 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20  1 = unlock, 0 = 
d0ba0 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  lock */.  unsign
d0bb0 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64  ed char startEnd
d0bc0 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31  Flag;       /* 1
d0bd0 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66  =rel to end of f
d0be0 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74  ork, 0=rel to st
d0bf0 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b  art */.  int fd;
d0c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d0c10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69             /* fi
d0c20 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63  le desc to assoc
d0c30 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20   this lock with 
d0c40 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61  */.};..#define a
d0c50 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63  fpfsByteRangeLoc
d0c60 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f  k2FSCTL        _
d0c70 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74  IOWR('z', 23, st
d0c80 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f  ruct ByteRangeLo
d0c90 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  ckPB2)../*.** Th
d0ca0 69 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20  is is a utility 
d0cb0 66 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63  for setting or c
d0cc0 6c 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61  learing a bit-ra
d0cd0 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a  nge lock on an.*
d0ce0 2a 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d  * AFP filesystem
d0cf0 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20  ..** .** Return 
d0d00 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
d0d10 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53  cess, SQLITE_BUS
d0d20 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f  Y on failure..*/
d0d30 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53  .static int afpS
d0d40 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20  etLock(.  const 
d0d50 63 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20  char *path,     
d0d60 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
d0d70 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20   of the file to 
d0d80 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c  be locked or unl
d0d90 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46  ocked */.  unixF
d0da0 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20  ile *pFile,     
d0db0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
d0dc0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
d0dd0 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75  r on path */.  u
d0de0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
d0df0 67 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a  g offset,     /*
d0e00 20 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62   First byte to b
d0e10 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e  e locked */.  un
d0e20 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
d0e30 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20   length,     /* 
d0e40 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
d0e50 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  to lock */.  int
d0e60 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20   setLockFlag    
d0e70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
d0e80 72 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e  rue to set lock.
d0e90 20 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72    False to clear
d0ea0 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74   lock */.){.  st
d0eb0 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f  ruct ByteRangeLo
d0ec0 63 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20  ckPB2 pb;.  int 
d0ed0 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c  err;.  .  pb.unL
d0ee0 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63  ockFlag = setLoc
d0ef0 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20  kFlag ? 0 : 1;. 
d0f00 20 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67   pb.startEndFlag
d0f10 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65   = 0;.  pb.offse
d0f20 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62  t = offset;.  pb
d0f30 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68  .length = length
d0f40 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69  ; .  pb.fd = pFi
d0f50 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52  le->h;.  .  OSTR
d0f60 41 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 4b  ACE6("AFPSETLOCK
d0f70 20 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69   [%s] for %d%s i
d0f80 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c  n range %llx:%ll
d0f90 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c  x\n", .    (setL
d0fa0 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46  ockFlag?"ON":"OF
d0fb0 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28  F"), pFile->h, (
d0fc0 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74  pb.fd==-1?"[test
d0fd0 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20  val-1]":""),.   
d0fe0 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29   offset, length)
d0ff0 3b 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28  ;.  err = fsctl(
d1000 70 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 52  path, afpfsByteR
d1010 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20  angeLock2FSCTL, 
d1020 26 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20  &pb, 0);.  if ( 
d1030 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20  err==-1 ) {.    
d1040 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 20  int rc;.    int 
d1050 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d1060 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 46      OSTRACE4("AF
d1070 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 20  PSETLOCK failed 
d1080 74 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 20  to fsctl() '%s' 
d1090 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  %d %s\n",.      
d10a0 20 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 72         path, tEr
d10b0 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 45  rno, strerror(tE
d10c0 72 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 53  rrno));.#ifdef S
d10d0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50  QLITE_IGNORE_AFP
d10e0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20  _LOCK_ERRORS.   
d10f0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
d1100 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20  Y;.#else.    rc 
d1110 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d1120 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d1130 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  no,.            
d1140 20 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b 46          setLockF
d1150 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45  lag ? SQLITE_IOE
d1160 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45  RR_LOCK : SQLITE
d1170 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a  _IOERR_UNLOCK);.
d1180 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
d1190 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b  _IGNORE_AFP_LOCK
d11a0 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 69  _ERRORS */.    i
d11b0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d11c0 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46  (rc) ){.      pF
d11d0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d11e0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20   tErrno;.    }. 
d11f0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
d1200 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74  } else {.    ret
d1210 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
d1220 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
d1230 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
d1240 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45  if there is a RE
d1250 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64  SERVED lock held
d1260 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   on the specifie
d1270 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69  d.** file by thi
d1280 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
d1290 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20  rocess. If such 
d12a0 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20  a lock is held, 
d12b0 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20  set *pResOut.** 
d12c0 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  to a non-zero va
d12d0 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70  lue otherwise *p
d12e0 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f  ResOut is set to
d12f0 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75   zero.  The retu
d1300 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73  rn value.** is s
d1310 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  et to SQLITE_OK 
d1320 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72  unless an I/O er
d1330 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
d1340 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e  g lock checking.
d1350 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
d1360 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  fpCheckReservedL
d1370 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d1380 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f   *id, int *pResO
d1390 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ut){.  int rc = 
d13a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
d13b0 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20   reserved = 0;. 
d13c0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d13d0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d13e0 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49  ;.  .  SimulateI
d13f0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
d1400 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
d1410 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29  KRESERVEDLOCK; )
d1420 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
d1430 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63  File );.  afpLoc
d1440 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e  kingContext *con
d1450 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69  text = (afpLocki
d1460 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
d1470 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
d1480 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63  xt;.  .  /* Chec
d1490 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e  k if a thread in
d14a0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f   this process ho
d14b0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20  lds such a lock 
d14c0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
d14d0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
d14e0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65  LOCK ){.    rese
d14f0 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  rved = 1;.  }.  
d1500 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  .  /* Otherwise 
d1510 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  see if some othe
d1520 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  r process holds 
d1530 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20  it..   */.  if( 
d1540 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20  !reserved ){.   
d1550 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53   /* lock the RES
d1560 45 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20  ERVED byte */.  
d1570 20 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 53    int lrc = afpS
d1580 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
d1590 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52  dbPath, pFile, R
d15a0 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c  ESERVED_BYTE, 1,
d15b0 31 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 51  1);  .    if( SQ
d15c0 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a  LITE_OK==lrc ){.
d15d0 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73        /* if we s
d15e0 75 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69  ucceeded in taki
d15f0 6e 67 20 74 68 65 20 72 65 73 65 72 76 65 64 20  ng the reserved 
d1600 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20  lock, unlock it 
d1610 74 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20  to restore.     
d1620 20 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   ** the original
d1630 20 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20   state */.      
d1640 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  lrc = afpSetLock
d1650 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
d1660 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45  , pFile, RESERVE
d1670 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20  D_BYTE, 1, 0);. 
d1680 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
d1690 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65    /* if we faile
d16a0 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63  d to get the loc
d16b0 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65  k then someone e
d16c0 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74  lse must have it
d16d0 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 76   */.      reserv
d16e0 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ed = 1;.    }.  
d16f0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d1700 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20  ROR(lrc) ){.    
d1710 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d 0a    rc=lrc;.    }.
d1720 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45    }.  .  OSTRACE
d1730 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  4("TEST WR-LOCK 
d1740 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69  %d %d %d\n", pFi
d1750 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72  le->h, rc, reser
d1760 76 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65 73  ved);.  .  *pRes
d1770 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a  Out = reserved;.
d1780 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
d1790 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66  /*.** Lock the f
d17a0 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63  ile with the loc
d17b0 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70  k specified by p
d17c0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70  arameter locktyp
d17d0 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68  e - one.** of th
d17e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
d17f0 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45  **     (1) SHARE
d1800 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32  D_LOCK.**     (2
d1810 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a  ) RESERVED_LOCK.
d1820 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49  **     (3) PENDI
d1830 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  NG_LOCK.**     (
d1840 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  4) EXCLUSIVE_LOC
d1850 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  K.**.** Sometime
d1860 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e  s when requestin
d1870 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65  g one lock state
d1880 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63  , additional loc
d1890 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20  k states.** are 
d18a0 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77  inserted in betw
d18b0 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e  een.  The lockin
d18c0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20  g might fail on 
d18d0 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72  one of the later
d18e0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
d18f0 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b  leaving the lock
d1900 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74   state different
d1910 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74   from what it st
d1920 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69  arted but.** sti
d1930 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20  ll short of its 
d1940 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  goal.  The follo
d1950 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73  wing chart shows
d1960 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20   the allowed.** 
d1970 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20  transitions and 
d1980 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74  the inserted int
d1990 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73  ermediate states
d19a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43  :.**.**    UNLOC
d19b0 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a  KED -> SHARED.**
d19c0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45      SHARED -> RE
d19d0 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41  SERVED.**    SHA
d19e0 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  RED -> (PENDING)
d19f0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
d1a00 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20      RESERVED -> 
d1a10 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
d1a20 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e  LUSIVE.**    PEN
d1a30 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56  DING -> EXCLUSIV
d1a40 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  E.**.** This rou
d1a50 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69  tine will only i
d1a60 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20  ncrease a lock. 
d1a70 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33   Use the sqlite3
d1a80 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f  OsUnlock().** ro
d1a90 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61  utine to lower a
d1aa0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a   locking level..
d1ab0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66  */.static int af
d1ac0 70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  pLock(sqlite3_fi
d1ad0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
d1ae0 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  type){.  int rc 
d1af0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75  = SQLITE_OK;.  u
d1b00 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d1b10 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d1b20 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74    afpLockingCont
d1b30 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28  ext *context = (
d1b40 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
d1b50 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  t *) pFile->lock
d1b60 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20  ingContext;.  . 
d1b70 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d1b80 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f  ;.  OSTRACE5("LO
d1b90 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20  CK    %d %s was 
d1ba0 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  %s pid=%d\n", pF
d1bb0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
d1bc0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f   locktypeName(lo
d1bd0 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70  cktype), locktyp
d1be0 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63  eName(pFile->loc
d1bf0 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64 28 29  ktype), getpid()
d1c00 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  );..  /* If ther
d1c10 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c  e is already a l
d1c20 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65  ock of this type
d1c30 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63   or more restric
d1c40 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a  tive on the.  **
d1c50 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f   unixFile, do no
d1c60 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65  thing. Don't use
d1c70 20 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f 63   the afp_end_loc
d1c80 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73  k: exit path, as
d1c90 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d  .  ** unixEnterM
d1ca0 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65  utex() hasn't be
d1cb0 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20  en called yet.. 
d1cc0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
d1cd0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74  >locktype>=lockt
d1ce0 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41  ype ){.    OSTRA
d1cf0 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20  CE3("LOCK    %d 
d1d00 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68  %s ok (already h
d1d10 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  eld)\n", pFile->
d1d20 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  h,.           lo
d1d30 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74  cktypeName(lockt
d1d40 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 72  ype));.    retur
d1d50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d1d60 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
d1d70 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71   the locking seq
d1d80 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74  uence is correct
d1d90 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
d1da0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21  pFile->locktype!
d1db0 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b  =NO_LOCK || lock
d1dc0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d1dd0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  K );.  assert( l
d1de0 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47  ocktype!=PENDING
d1df0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
d1e00 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53  t( locktype!=RES
d1e10 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46  ERVED_LOCK || pF
d1e20 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
d1e30 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
d1e40 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78  .  /* This mutex
d1e50 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75   is needed becau
d1e60 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20  se pFile->pLock 
d1e70 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73  is shared across
d1e80 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20   threads.  */.  
d1e90 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
d1ea0 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  ;..  /* Make sur
d1eb0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  e the current th
d1ec0 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46  read owns the pF
d1ed0 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ile..  */.  rc =
d1ee0 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68   transferOwnersh
d1ef0 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28  ip(pFile);.  if(
d1f00 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
d1f10 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d  {.    unixLeaveM
d1f20 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75  utex();.    retu
d1f30 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a  rn rc;.  }.    .
d1f40 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c    /* A PENDING l
d1f50 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65  ock is needed be
d1f60 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61  fore acquiring a
d1f70 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64   SHARED lock and
d1f80 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71   before.  ** acq
d1f90 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53  uiring an EXCLUS
d1fa0 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74  IVE lock.  For t
d1fb0 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20  he SHARED lock, 
d1fc0 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c  the PENDING will
d1fd0 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65  .  ** be release
d1fe0 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  d..  */.  if( lo
d1ff0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
d2000 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c  OCK .      || (l
d2010 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
d2020 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65  VE_LOCK && pFile
d2030 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49  ->locktype<PENDI
d2040 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20  NG_LOCK).  ){.  
d2050 20 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20    int failed;.  
d2060 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65    failed = afpSe
d2070 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
d2080 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45  bPath, pFile, PE
d2090 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 31  NDING_BYTE, 1, 1
d20a0 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c 65  );.    if (faile
d20b0 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  d) {.      rc = 
d20c0 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 67 6f  failed;.      go
d20d0 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b  to afp_end_lock;
d20e0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20  .    }.  }.  .  
d20f0 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65  /* If control ge
d2100 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74  ts to this point
d2110 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20  , then actually 
d2120 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b  go ahead and mak
d2130 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67  e.  ** operating
d2140 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f   system calls fo
d2150 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  r the specified 
d2160 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  lock..  */.  if(
d2170 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
d2180 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e  D_LOCK ){.    in
d2190 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 63 32  t lk, lrc1, lrc2
d21a0 2c 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20  , lrc1Errno;.   
d21b0 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74   .    /* Now get
d21c0 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53   the read-lock S
d21d0 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20  HARED_LOCK */.  
d21e0 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74    /* note that t
d21f0 68 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68  he quality of th
d2200 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65  e randomness doe
d2210 73 6e 27 74 20 6d 61 74 74 65 72 20 74 68 61 74  sn't matter that
d2220 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20   much */.    lk 
d2230 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20  = random(); .   
d2240 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64   context->shared
d2250 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37  Byte = (lk & 0x7
d2260 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44  fffffff)%(SHARED
d2270 5f 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20  _SIZE - 1);.    
d2280 6c 72 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63  lrc1 = afpSetLoc
d2290 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
d22a0 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20  h, pFile, .     
d22b0 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53       SHARED_FIRS
d22c0 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65  T+context->share
d22d0 64 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20  dByte, 1, 1);.  
d22e0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d22f0 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20  ROR(lrc1) ){.   
d2300 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70     lrc1Errno = p
d2310 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b  File->lastErrno;
d2320 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72  .    }.    /* Dr
d2330 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  op the temporary
d2340 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f   PENDING lock */
d2350 0a 20 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53  .    lrc2 = afpS
d2360 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
d2370 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50  dbPath, pFile, P
d2380 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20  ENDING_BYTE, 1, 
d2390 30 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28  0);.    .    if(
d23a0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
d23b0 72 63 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70  rc1) ) {.      p
d23c0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d23d0 3d 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20  = lrc1Errno;.   
d23e0 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20     rc = lrc1;.  
d23f0 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64      goto afp_end
d2400 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73  _lock;.    } els
d2410 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52  e if( IS_LOCK_ER
d2420 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20  ROR(lrc2) ){.   
d2430 20 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20     rc = lrc2;.  
d2440 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64      goto afp_end
d2450 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73  _lock;.    } els
d2460 65 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51  e if( lrc1 != SQ
d2470 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20  LITE_OK ) {.    
d2480 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20    rc = lrc1;.   
d2490 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
d24a0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d24b0 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
d24c0 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65       pFile->pOpe
d24d0 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20  n->nLock++;.    
d24e0 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  }.  }else{.    /
d24f0 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61  * The request wa
d2500 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44  s for a RESERVED
d2510 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   or EXCLUSIVE lo
d2520 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a  ck.  It is.    *
d2530 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  * assumed that t
d2540 68 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44  here is a SHARED
d2550 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
d2560 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20   on the file.   
d2570 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20   ** already..   
d2580 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c   */.    int fail
d2590 65 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65  ed = 0;.    asse
d25a0 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f  rt( 0!=pFile->lo
d25b0 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66  cktype );.    if
d25c0 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45   (locktype >= RE
d25d0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70  SERVED_LOCK && p
d25e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c  File->locktype <
d25f0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20   RESERVED_LOCK) 
d2600 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71  {.        /* Acq
d2610 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20  uire a RESERVED 
d2620 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20  lock */.        
d2630 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c  failed = afpSetL
d2640 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
d2650 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45  ath, pFile, RESE
d2660 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b  RVED_BYTE, 1,1);
d2670 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21  .    }.    if (!
d2680 66 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79  failed && lockty
d2690 70 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f  pe == EXCLUSIVE_
d26a0 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a  LOCK) {.      /*
d26b0 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c   Acquire an EXCL
d26c0 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20  USIVE lock */.  
d26d0 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20        .      /* 
d26e0 52 65 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65  Remove the share
d26f0 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72  d lock before tr
d2700 79 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20  ying the range. 
d2710 20 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a   we'll need to .
d2720 20 20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62        ** reestab
d2730 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20  lish the shared 
d2740 6c 6f 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74  lock if we can't
d2750 20 67 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c   get the  afpUnl
d2760 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ock.      */.   
d2770 20 20 20 69 66 28 20 21 28 66 61 69 6c 65 64 20     if( !(failed 
d2780 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e  = afpSetLock(con
d2790 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46  text->dbPath, pF
d27a0 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ile, SHARED_FIRS
d27b0 54 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20  T +.            
d27c0 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
d27d0 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65  text->sharedByte
d27e0 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20  , 1, 0)) ){.    
d27f0 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20      int failed2 
d2800 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
d2810 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65       /* now atte
d2820 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20  mmpt to get the 
d2830 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72  exclusive lock r
d2840 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  ange */.        
d2850 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c  failed = afpSetL
d2860 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
d2870 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52  ath, pFile, SHAR
d2880 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20  ED_FIRST, .     
d2890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d28a0 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44            SHARED
d28b0 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20  _SIZE, 1);.     
d28c0 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 26 26     if( failed &&
d28d0 20 28 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53   (failed2 = afpS
d28e0 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
d28f0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a  dbPath, pFile, .
d2900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d2910 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49         SHARED_FI
d2920 52 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73  RST + context->s
d2930 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29  haredByte, 1, 1)
d2940 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  ) ){.          /
d2950 2a 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c  * Can't reestabl
d2960 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c  ish the shared l
d2970 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e  ock.  Sqlite can
d2980 27 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73  't deal, this is
d2990 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20  .          ** a 
d29a0 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72  critical I/O err
d29b0 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  or.          */.
d29c0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 28            rc = (
d29d0 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45  (failed & SQLITE
d29e0 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54  _IOERR) == SQLIT
d29f0 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65  E_IOERR) ? faile
d2a00 64 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20  d2 : .          
d2a10 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52       SQLITE_IOER
d2a20 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20  R_LOCK;.        
d2a30 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c    goto afp_end_l
d2a40 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a  ock;.        } .
d2a50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
d2a60 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64       rc = failed
d2a70 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ; .      }.    }
d2a80 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20  .    if( failed 
d2a90 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61  ){.      rc = fa
d2aa0 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  iled;.    }.  }.
d2ab0 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c    .  if( rc==SQL
d2ac0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46  ITE_OK ){.    pF
d2ad0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
d2ae0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73  locktype;.  }els
d2af0 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d  e if( locktype==
d2b00 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
d2b10 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
d2b20 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f  ktype = PENDING_
d2b30 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70  LOCK;.  }.  .afp
d2b40 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69  _end_lock:.  uni
d2b50 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
d2b60 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20   OSTRACE4("LOCK 
d2b70 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20     %d %s %s\n", 
d2b80 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
d2b90 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29  peName(locktype)
d2ba0 2c 20 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d  , .         rc==
d2bb0 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22  SQLITE_OK ? "ok"
d2bc0 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20   : "failed");.  
d2bd0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d2be0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
d2bf0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
d2c00 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
d2c10 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
d2c20 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
d2c30 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
d2c40 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
d2c50 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
d2c60 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
d2c70 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
d2c80 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
d2c90 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
d2ca0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
d2cb0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
d2cc0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
d2cd0 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
d2ce0 63 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28  c int afpUnlock(
d2cf0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d2d00 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
d2d10 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
d2d20 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69  ITE_OK;.  unixFi
d2d30 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d2d40 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70  xFile*)id;.  afp
d2d50 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
d2d60 70 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69  pCtx = (afpLocki
d2d70 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
d2d80 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
d2d90 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  xt;..  assert( p
d2da0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
d2db0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25  E5("UNLOCK  %d %
d2dc0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c  d was %d pid=%d\
d2dd0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
d2de0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20  cktype,.        
d2df0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d2e00 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
d2e10 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
d2e20 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
d2e30 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d2e40 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65  cktype<=locktype
d2e50 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
d2e60 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
d2e70 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44  if( CHECK_THREAD
d2e80 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20  ID(pFile) ){.   
d2e90 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
d2ea0 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69  ISUSE;.  }.  uni
d2eb0 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
d2ec0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d2ed0 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
d2ee0 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28   ){.    .    if(
d2ef0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d2f00 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
d2f10 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
d2f20 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e  fpSetLock(pCtx->
d2f30 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53  dbPath, pFile, S
d2f40 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41  HARED_FIRST, SHA
d2f50 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20  RED_SIZE, 0);.  
d2f60 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
d2f70 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70  TE_OK && locktyp
d2f80 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
d2f90 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c  {.        /* onl
d2fa0 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74  y re-establish t
d2fb0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69  he shared lock i
d2fc0 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20  f necessary */. 
d2fd0 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65         int share
d2fe0 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52  dLockByte = SHAR
d2ff0 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73  ED_FIRST+pCtx->s
d3000 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20  haredByte;.     
d3010 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f     rc = afpSetLo
d3020 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c  ck(pCtx->dbPath,
d3030 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f   pFile, sharedLo
d3040 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20  ckByte, 1, 1);. 
d3050 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
d3060 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
d3070 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  OK && pFile->loc
d3080 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c  ktype>=PENDING_L
d3090 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  OCK ){.      rc 
d30a0 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74  = afpSetLock(pCt
d30b0 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
d30c0 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
d30d0 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20  1, 0);.    } .  
d30e0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
d30f0 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f  _OK && pFile->lo
d3100 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44  cktype>=RESERVED
d3110 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72  _LOCK ){.      r
d3120 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70  c = afpSetLock(p
d3130 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  Ctx->dbPath, pFi
d3140 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
d3150 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  E, 1, 0);.    }.
d3160 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b    }else if( lock
d3170 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  type==NO_LOCK ){
d3180 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68  .    /* clear th
d3190 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f  e shared lock */
d31a0 0a 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c  .    int sharedL
d31b0 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44  ockByte = SHARED
d31c0 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61  _FIRST+pCtx->sha
d31d0 72 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20  redByte;.    rc 
d31e0 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74  = afpSetLock(pCt
d31f0 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
d3200 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  , sharedLockByte
d3210 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  , 1, 0);.  }..  
d3220 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
d3230 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63  K ){.    if( loc
d3240 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29  ktype==NO_LOCK )
d3250 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75  {.      struct u
d3260 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65  nixOpenCnt *pOpe
d3270 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  n = pFile->pOpen
d3280 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e  ;.      pOpen->n
d3290 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73  Lock--;.      as
d32a0 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f  sert( pOpen->nLo
d32b0 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  ck>=0 );.      i
d32c0 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d  f( pOpen->nLock=
d32d0 3d 30 20 26 26 20 70 4f 70 65 6e 2d 3e 6e 50 65  =0 && pOpen->nPe
d32e0 6e 64 69 6e 67 3e 30 20 29 7b 0a 20 20 20 20 20  nding>0 ){.     
d32f0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
d3300 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70    for(i=0; i<pOp
d3310 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3b 20 69 2b  en->nPending; i+
d3320 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
d3330 28 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e  ( pOpen->aPendin
d3340 67 5b 69 5d 20 3c 20 30 20 29 20 63 6f 6e 74 69  g[i] < 0 ) conti
d3350 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 69  nue;.          i
d3360 66 28 20 63 6c 6f 73 65 28 70 4f 70 65 6e 2d 3e  f( close(pOpen->
d3370 61 50 65 6e 64 69 6e 67 5b 69 5d 29 20 29 7b 0a  aPending[i]) ){.
d3380 20 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c              pFil
d3390 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
d33a0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20  rrno;.          
d33b0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
d33c0 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20  ERR_CLOSE;.     
d33d0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
d33e0 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61          pOpen->a
d33f0 50 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d 31 3b  Pending[i] = -1;
d3400 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
d3410 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
d3420 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
d3430 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
d3440 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e  lite3_free(pOpen
d3450 2d 3e 61 50 65 6e 64 69 6e 67 29 3b 0a 20 20 20  ->aPending);.   
d3460 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50         pOpen->nP
d3470 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20  ending = 0;.    
d3480 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65        pOpen->aPe
d3490 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20  nding = 0;.     
d34a0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
d34b0 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61   }.  }.  unixLea
d34c0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  veMutex();.  if(
d34d0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
d34e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d34f0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72   = locktype;.  r
d3500 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d3510 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20  ** Close a file 
d3520 26 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70  & cleanup AFP sp
d3530 65 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63  ecific locking c
d3540 6f 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69  ontext .*/.stati
d3550 63 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73  c int afpClose(s
d3560 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
d3570 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20   {.  if( id ){. 
d3580 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69     unixFile *pFi
d3590 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d35a0 69 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63  id;.    afpUnloc
d35b0 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  k(id, NO_LOCK);.
d35c0 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74      unixEnterMut
d35d0 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46  ex();.    if( pF
d35e0 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46  ile->pOpen && pF
d35f0 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63  ile->pOpen->nLoc
d3600 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  k ){.      /* If
d3610 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74   there are outst
d3620 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f  anding locks, do
d3630 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c   not actually cl
d3640 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73  ose the file jus
d3650 74 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62  t.      ** yet b
d3660 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c  ecause that woul
d3670 64 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f  d clear those lo
d3680 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61  cks.  Instead, a
d3690 64 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20  dd the file.    
d36a0 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20    ** descriptor 
d36b0 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69  to pOpen->aPendi
d36c0 6e 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20  ng.  It will be 
d36d0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c  automatically cl
d36e0 6f 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20  osed when.      
d36f0 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b  ** the last lock
d3700 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20   is cleared..   
d3710 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20     */.      int 
d3720 2a 61 4e 65 77 3b 0a 20 20 20 20 20 20 73 74 72  *aNew;.      str
d3730 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  uct unixOpenCnt 
d3740 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e  *pOpen = pFile->
d3750 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 61 4e 65  pOpen;.      aNe
d3760 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  w = sqlite3_real
d3770 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64  loc(pOpen->aPend
d3780 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e 6e 50 65  ing, (pOpen->nPe
d3790 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65 6f 66 28  nding+1)*sizeof(
d37a0 69 6e 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66  int) );.      if
d37b0 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20  ( aNew==0 ){.   
d37c0 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c       /* If a mal
d37d0 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75 73 74 20  loc fails, just 
d37e0 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64 65  leak the file de
d37f0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 20 20  scriptor */.    
d3800 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
d3810 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67   pOpen->aPending
d3820 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20   = aNew;.       
d3830 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67   pOpen->aPending
d3840 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67  [pOpen->nPending
d3850 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20  ] = pFile->h;.  
d3860 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65        pOpen->nPe
d3870 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 20 20 20 20  nding++;.       
d3880 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a   pFile->h = -1;.
d3890 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
d38a0 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74    releaseOpenCnt
d38b0 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a  (pFile->pOpen);.
d38c0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
d38d0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  (pFile->lockingC
d38e0 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 63 6c 6f  ontext);.    clo
d38f0 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a  seUnixFile(id);.
d3900 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74      unixLeaveMut
d3910 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ex();.  }.  retu
d3920 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
d3930 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
d3940 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
d3950 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
d3960 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
d3970 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 61  /*.** The code a
d3980 62 6f 76 65 20 69 73 20 74 68 65 20 41 46 50 20  bove is the AFP 
d3990 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
d39a0 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64 65 20 69  ion.  The code i
d39b0 73 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f  s specific.** to
d39c0 20 4d 61 63 4f 53 58 20 61 6e 64 20 64 6f 65 73   MacOSX and does
d39d0 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 6f 74 68   not work on oth
d39e0 65 72 20 75 6e 69 78 20 70 6c 61 74 66 6f 72 6d  er unix platform
d39f0 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e 61 74 69  s.  No alternati
d3a00 76 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 62  ve.** is availab
d3a10 6c 65 2e 20 20 49 66 20 79 6f 75 20 64 6f 6e 27  le.  If you don'
d3a20 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72 20 61 20  t compile for a 
d3a30 6d 61 63 2c 20 74 68 65 6e 20 74 68 65 20 22 75  mac, then the "u
d3a40 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56 46 53 20  nix-afp".** VFS 
d3a50 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
d3a60 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
d3a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
d3a80 66 20 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69  f the AFP lock i
d3a90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a  mplementation **
d3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3ab0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
d3ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3b00 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ***/.../********
d3b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3b50 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
d3b60 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f 63 6b  ******* Non-lock
d3b70 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ing sqlite3_file
d3b80 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a   methods *******
d3b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d3ba0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  ******.**.** The
d3bb0 20 6e 65 78 74 20 64 69 76 69 73 69 6f 6e 20 63   next division c
d3bc0 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e  ontains implemen
d3bd0 74 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 20  tations for all 
d3be0 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 0a  methods of the .
d3bf0 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ** sqlite3_file 
d3c00 6f 62 6a 65 63 74 20 6f 74 68 65 72 20 74 68 61  object other tha
d3c10 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 65  n the locking me
d3c20 74 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f 63 6b  thods.  The lock
d3c30 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 77  ing.** methods w
d3c40 65 72 65 20 64 65 66 69 6e 65 64 20 69 6e 20 64  ere defined in d
d3c50 69 76 69 73 69 6f 6e 73 20 61 62 6f 76 65 20 28  ivisions above (
d3c60 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68  one locking meth
d3c70 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76 69 73 69  od per.** divisi
d3c80 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65 74 68  on).  Those meth
d3c90 6f 64 73 20 74 68 61 74 20 61 72 65 20 63 6f 6d  ods that are com
d3ca0 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69  mon to all locki
d3cb0 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 72 65 20  ng modes.** are 
d3cc0 67 61 74 68 65 72 20 74 6f 67 65 74 68 65 72 20  gather together 
d3cd0 69 6e 74 6f 20 74 68 69 73 20 64 69 76 69 73 69  into this divisi
d3ce0 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65  on..*/../*.** Se
d3cf0 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74  ek to the offset
d3d00 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
d3d10 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20  econd argument, 
d3d20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 0a 2a  then read cnt .*
d3d30 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 75  * bytes into pBu
d3d40 66 2e 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  f. Return the nu
d3d50 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63  mber of bytes ac
d3d60 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a 0a  tually read..**.
d3d70 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 20 64  ** NB:  If you d
d3d80 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 20  efine USE_PREAD 
d3d90 6f 72 20 55 53 45 5f 50 52 45 41 44 36 34 2c 20  or USE_PREAD64, 
d3da0 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 61 6c  then it might al
d3db0 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 61  so.** be necessa
d3dc0 72 79 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f  ry to define _XO
d3dd0 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 62 65  PEN_SOURCE to be
d3de0 20 35 30 30 2e 20 20 54 68 69 73 20 76 61 72 69   500.  This vari
d3df0 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 73  es from.** one s
d3e00 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 65 72  ystem to another
d3e10 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 74 65 20  .  Since SQLite 
d3e20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20  does not define 
d3e30 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 6e 79  USE_PREAD.** any
d3e40 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 65 66   any form by def
d3e50 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 6e 6f  ault, we will no
d3e60 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 66  t attempt to def
d3e70 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43  ine _XOPEN_SOURC
d3e80 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65 74  E..** See ticket
d3e90 73 20 23 32 37 34 31 20 61 6e 64 20 23 32 36 38  s #2741 and #268
d3ea0 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69  1..**.** To avoi
d3eb0 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65  d stomping the e
d3ec0 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20  rrno value on a 
d3ed0 66 61 69 6c 65 64 20 72 65 61 64 20 74 68 65 20  failed read the 
d3ee0 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a  lastErrno value.
d3ef0 2a 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65  ** is set before
d3f00 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73   returning..*/.s
d3f10 74 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e  tatic int seekAn
d3f20 64 52 65 61 64 28 75 6e 69 78 46 69 6c 65 20 2a  dRead(unixFile *
d3f30 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  id, sqlite3_int6
d3f40 34 20 6f 66 66 73 65 74 2c 20 76 6f 69 64 20 2a  4 offset, void *
d3f50 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a  pBuf, int cnt){.
d3f60 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34    int got;.  i64
d3f70 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 49   newOffset;.  TI
d3f80 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 64  MER_START;.#if d
d3f90 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44  efined(USE_PREAD
d3fa0 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 61 64 28  ).  got = pread(
d3fb0 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74  id->h, pBuf, cnt
d3fc0 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 53 69 6d  , offset);.  Sim
d3fd0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 67 6f  ulateIOError( go
d3fe0 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 69 66 20  t = -1 );.#elif 
d3ff0 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41  defined(USE_PREA
d4000 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 70 72 65  D64).  got = pre
d4010 61 64 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66  ad64(id->h, pBuf
d4020 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a  , cnt, offset);.
d4030 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
d4040 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23  r( got = -1 );.#
d4050 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74  else.  newOffset
d4060 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20   = lseek(id->h, 
d4070 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54  offset, SEEK_SET
d4080 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
d4090 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 74 2d  rror( newOffset-
d40a0 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66  - );.  if( newOf
d40b0 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a  fset!=offset ){.
d40c0 20 20 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65      if( newOffse
d40d0 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20  t == -1 ){.     
d40e0 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
d40f0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
d4100 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rno;.    }else{.
d4110 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65        ((unixFile
d4120 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  *)id)->lastErrno
d4130 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20   = 0;....    }. 
d4140 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20     return -1;.  
d4150 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61 64 28 69  }.  got = read(i
d4160 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29  d->h, pBuf, cnt)
d4170 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52  ;.#endif.  TIMER
d4180 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c  _END;.  if( got<
d4190 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46  0 ){.    ((unixF
d41a0 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
d41b0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d  rno = errno;.  }
d41c0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 52 45 41  .  OSTRACE5("REA
d41d0 44 20 20 20 20 25 2d 33 64 20 25 35 64 20 25 37  D    %-3d %5d %7
d41e0 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d  lld %llu\n", id-
d41f0 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c  >h, got, offset,
d4200 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b   TIMER_ELAPSED);
d4210 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d  .  return got;.}
d4220 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74  ../*.** Read dat
d4230 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e  a from a file in
d4240 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65  to a buffer.  Re
d4250 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
d4260 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77  f all.** bytes w
d4270 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73  ere read success
d4280 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45  fully and SQLITE
d4290 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69  _IOERR if anythi
d42a0 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67  ng goes.** wrong
d42b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d42c0 75 6e 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69  unixRead(.  sqli
d42d0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20  te3_file *id, . 
d42e0 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20   void *pBuf, .  
d42f0 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74  int amt,.  sqlit
d4300 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a  e3_int64 offset.
d4310 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20  ){.  int got;.  
d4320 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 0a 20  assert( id );.. 
d4330 20 2f 2a 20 4e 65 76 65 72 20 72 65 61 64 20 6f   /* Never read o
d4340 72 20 77 72 69 74 65 20 61 6e 79 20 6f 66 20 74  r write any of t
d4350 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  he bytes in the 
d4360 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 2a 2f  locking range */
d4370 0a 20 20 61 73 73 65 72 74 28 20 28 28 75 6e 69  .  assert( ((uni
d4380 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 73 4c 6f  xFile*)id)->isLo
d4390 63 6b 61 62 6c 65 3d 3d 30 0a 20 20 20 20 20 20  ckable==0.      
d43a0 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50      || offset>=P
d43b0 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a  ENDING_BYTE+512.
d43c0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6f 66 66            || off
d43d0 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47  set+amt<=PENDING
d43e0 5f 42 59 54 45 20 29 3b 0a 0a 20 20 67 6f 74 20  _BYTE );..  got 
d43f0 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 28 75  = seekAndRead((u
d4400 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20 6f 66 66  nixFile*)id, off
d4410 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 3b  set, pBuf, amt);
d4420 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 6d 74 20  .  if( got==amt 
d4430 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
d4440 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
d4450 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20   if( got<0 ){.  
d4460 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73    /* lastErrno s
d4470 65 74 20 62 79 20 73 65 65 6b 41 6e 64 52 65 61  et by seekAndRea
d4480 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  d */.    return 
d4490 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
d44a0 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  D;.  }else{.    
d44b0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
d44c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20  >lastErrno = 0; 
d44d0 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20  /* not a system 
d44e0 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20  error */.    /* 
d44f0 55 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20  Unread parts of 
d4500 74 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20  the buffer must 
d4510 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a  be zero-filled *
d4520 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28  /.    memset(&((
d4530 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d  char*)pBuf)[got]
d4540 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20  , 0, amt-got);. 
d4550 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d4560 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
d4570 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  D;.  }.}../*.** 
d4580 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73  Seek to the offs
d4590 65 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74  et in id->offset
d45a0 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62   then read cnt b
d45b0 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a  ytes into pBuf..
d45c0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
d45d0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63  mber of bytes ac
d45e0 74 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70  tually read.  Up
d45f0 64 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e  date the offset.
d4600 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20  .**.** To avoid 
d4610 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72  stomping the err
d4620 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61  no value on a fa
d4630 69 6c 65 64 20 77 72 69 74 65 20 74 68 65 20 6c  iled write the l
d4640 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a  astErrno value.*
d4650 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20  * is set before 
d4660 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  returning..*/.st
d4670 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64  atic int seekAnd
d4680 57 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a  Write(unixFile *
d4690 69 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20  id, i64 offset, 
d46a0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66  const void *pBuf
d46b0 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e  , int cnt){.  in
d46c0 74 20 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77  t got;.  i64 new
d46d0 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f  Offset;.  TIMER_
d46e0 53 54 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e  START;.#if defin
d46f0 65 64 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20  ed(USE_PREAD).  
d4700 67 6f 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d  got = pwrite(id-
d4710 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
d4720 66 66 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65  ffset);.#elif de
d4730 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36  fined(USE_PREAD6
d4740 34 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74  4).  got = pwrit
d4750 65 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c  e64(id->h, pBuf,
d4760 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23   cnt, offset);.#
d4770 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74  else.  newOffset
d4780 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20   = lseek(id->h, 
d4790 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54  offset, SEEK_SET
d47a0 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73  );.  if( newOffs
d47b0 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20  et!=offset ){.  
d47c0 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20    if( newOffset 
d47d0 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28  == -1 ){.      (
d47e0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
d47f0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
d4800 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  o;.    }else{.  
d4810 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
d4820 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  id)->lastErrno =
d4830 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20   0;....    }.   
d4840 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a   return -1;.  }.
d4850 20 20 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64    got = write(id
d4860 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b  ->h, pBuf, cnt);
d4870 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f  .#endif.  TIMER_
d4880 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30  END;.  if( got<0
d4890 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69   ){.    ((unixFi
d48a0 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72  le*)id)->lastErr
d48b0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a  no = errno;.  }.
d48c0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 57 52 49  .  OSTRACE5("WRI
d48d0 54 45 20 20 20 25 2d 33 64 20 25 35 64 20 25 37  TE   %-3d %5d %7
d48e0 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d  lld %llu\n", id-
d48f0 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c  >h, got, offset,
d4900 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b   TIMER_ELAPSED);
d4910 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d  .  return got;.}
d4920 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64  .../*.** Write d
d4930 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65  ata from a buffe
d4940 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20  r into a file.  
d4950 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
d4960 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f   on success.** o
d4970 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72  r some other err
d4980 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75  or code on failu
d4990 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
d49a0 74 20 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73  t unixWrite(.  s
d49b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d49c0 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
d49d0 70 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74  pBuf, .  int amt
d49e0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ,.  sqlite3_int6
d49f0 34 20 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 69  4 offset .){.  i
d4a00 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20  nt wrote = 0;.  
d4a10 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20  assert( id );.  
d4a20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b  assert( amt>0 );
d4a30 0a 0a 20 20 2f 2a 20 4e 65 76 65 72 20 72 65 61  ..  /* Never rea
d4a40 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 6f  d or write any o
d4a50 66 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74  f the bytes in t
d4a60 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65  he locking range
d4a70 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 28   */.  assert( ((
d4a80 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69  unixFile*)id)->i
d4a90 73 4c 6f 63 6b 61 62 6c 65 3d 3d 30 0a 20 20 20  sLockable==0.   
d4aa0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74         || offset
d4ab0 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35  >=PENDING_BYTE+5
d4ac0 31 32 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20  12.          || 
d4ad0 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44  offset+amt<=PEND
d4ae0 49 4e 47 5f 42 59 54 45 20 29 3b 0a 0a 23 69 66  ING_BYTE );..#if
d4af0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
d4b00 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67   If we are doing
d4b10 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20   a normal write 
d4b20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69  to a database fi
d4b30 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74  le (as opposed t
d4b40 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68  o.  ** doing a h
d4b50 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot-journal rollb
d4b60 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74  ack or a write t
d4b70 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65  o some file othe
d4b80 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f  r than a.  ** no
d4b90 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  rmal database fi
d4ba0 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20  le) then record 
d4bb0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68  the fact that th
d4bc0 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  e database.  ** 
d4bd0 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66  has changed.  If
d4be0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
d4bf0 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69   counter is modi
d4c00 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61  fied, record tha
d4c10 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e  t.  ** fact too.
d4c20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 28 75 6e  .  */.  if( ((un
d4c30 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 6e 4e  ixFile*)id)->inN
d4c40 6f 72 6d 61 6c 57 72 69 74 65 20 29 7b 0a 20 20  ormalWrite ){.  
d4c50 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d4c60 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d4c70 64 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62  d;.    pFile->db
d4c80 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a 20  Update = 1;  /* 
d4c90 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  The database has
d4ca0 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 2a   been modified *
d4cb0 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 74  /.    if( offset
d4cc0 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b 61  <=24 && offset+a
d4cd0 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 20  mt>=27 ){.      
d4ce0 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63 68  int rc;.      ch
d4cf0 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a 20  ar oldCntr[4];. 
d4d00 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
d4d10 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20  rrorBenign(1);. 
d4d20 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41 6e       rc = seekAn
d4d30 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34 2c  dRead(pFile, 24,
d4d40 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 20   oldCntr, 4);.  
d4d50 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
d4d60 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20  rorBenign(0);.  
d4d70 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c 7c      if( rc!=4 ||
d4d80 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72 2c   memcmp(oldCntr,
d4d90 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b   &((char*)pBuf)[
d4da0 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21 3d  24-offset], 4)!=
d4db0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  0 ){.        pFi
d4dc0 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e  le->transCntrChn
d4dd0 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20 74  g = 1;  /* The t
d4de0 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
d4df0 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a  er has changed *
d4e00 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  /.      }.    }.
d4e10 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77 68    }.#endif..  wh
d4e20 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77  ile( amt>0 && (w
d4e30 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72  rote = seekAndWr
d4e40 69 74 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 69  ite((unixFile*)i
d4e50 64 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c  d, offset, pBuf,
d4e60 20 61 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20   amt))>0 ){.    
d4e70 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20  amt -= wrote;.  
d4e80 20 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74    offset += wrot
d4e90 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28  e;.    pBuf = &(
d4ea0 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f  (char*)pBuf)[wro
d4eb0 74 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c  te];.  }.  Simul
d4ec0 61 74 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f  ateIOError(( wro
d4ed0 74 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29  te=(-1), amt=1 )
d4ee0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73  );.  SimulateDis
d4ef0 6b 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f  kfullError(( wro
d4f00 74 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a  te=0, amt=1 ));.
d4f10 20 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20    if( amt>0 ){. 
d4f20 20 20 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29     if( wrote<0 )
d4f30 7b 0a 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45  {.      /* lastE
d4f40 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b  rrno set by seek
d4f50 41 6e 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20  AndWrite */.    
d4f60 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d4f70 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20  IOERR_WRITE;.   
d4f80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28   }else{.      ((
d4f90 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
d4fa0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a  astErrno = 0; /*
d4fb0 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72   not a system er
d4fc0 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74  ror */.      ret
d4fd0 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  urn SQLITE_FULL;
d4fe0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
d4ff0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
d5000 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
d5010 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74  TEST./*.** Count
d5020 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
d5030 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72  ullsyncs and nor
d5040 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73  mal syncs.  This
d5050 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74   is used to test
d5060 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61  .** that syncs a
d5070 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65  nd fullsyncs are
d5080 20 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68   occurring at th
d5090 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a  e right times..*
d50a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
d50b0 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f   sqlite3_sync_co
d50c0 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f  unt = 0;.SQLITE_
d50d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
d50e0 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d  fullsync_count =
d50f0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
d5100 2a 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73  * We do not trus
d5110 74 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f  t systems to pro
d5120 76 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66  vide a working f
d5130 64 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d  datasync().  Som
d5140 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20  e do..** Others 
d5150 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61  do no.  To be sa
d5160 66 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63  fe, we will stic
d5170 6b 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77  k with the (slow
d5180 65 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20  er) fsync()..** 
d5190 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74  If you know that
d51a0 20 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65   your system doe
d51b0 73 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 73  s support fdatas
d51c0 79 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c  ync() correctly,
d51d0 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20  .** then simply 
d51e0 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66  compile with -Df
d51f0 64 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79  datasync=fdatasy
d5200 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  nc.*/.#if !defin
d5210 65 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 26  ed(fdatasync) &&
d5220 20 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75   !defined(__linu
d5230 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64  x__).# define fd
d5240 61 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65  atasync fsync.#e
d5250 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69  ndif../*.** Defi
d5260 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e  ne HAVE_FULLFSYN
d5270 43 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65  C to 0 or 1 depe
d5280 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72  nding on whether
d5290 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46   or not.** the F
d52a0 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f  _FULLFSYNC macro
d52b0 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f   is defined.  F_
d52c0 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72  FULLFSYNC is cur
d52d0 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61  rently.** only a
d52e0 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20  vailable on Mac 
d52f0 4f 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20  OS X.  But that 
d5300 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f  could change..*/
d5310 0a 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53  .#ifdef F_FULLFS
d5320 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56  YNC.# define HAV
d5330 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65  E_FULLFSYNC 1.#e
d5340 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56  lse.# define HAV
d5350 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65  E_FULLFSYNC 0.#e
d5360 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ndif.../*.** The
d5370 20 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20   fsync() system 
d5380 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f  call does not wo
d5390 72 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64  rk as advertised
d53a0 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78   on many.** unix
d53b0 20 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66   systems.  The f
d53c0 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75  ollowing procedu
d53d0 72 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74  re is an attempt
d53e0 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77   to make.** it w
d53f0 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a  ork better..**.*
d5400 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f  * The SQLITE_NO_
d5410 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62  SYNC macro disab
d5420 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73  les all fsync()s
d5430 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75  .  This is usefu
d5440 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67  l.** for testing
d5450 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f   when we want to
d5460 20 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65   run through the
d5470 20 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63   test suite quic
d5480 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20  kly..** You are 
d5490 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64  strongly advised
d54a0 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79   *not* to deploy
d54b0 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f   with SQLITE_NO_
d54c0 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c  SYNC.** enabled,
d54d0 20 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20   however, since 
d54e0 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  with SQLITE_NO_S
d54f0 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20  YNC enabled, an 
d5500 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70  OS crash.** or p
d5510 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c  ower failure wil
d5520 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74  l likely corrupt
d5530 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
d5540 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  le..**.** SQLite
d5550 20 73 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e   sets the dataOn
d5560 6c 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 73  ly flag if the s
d5570 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
d5580 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a  is unchanged..**
d5590 20 54 68 65 20 69 64 65 61 20 62 65 68 69 6e 64   The idea behind
d55a0 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61   dataOnly is tha
d55b0 74 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79  t it should only
d55c0 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20   write the file 
d55d0 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69  content.** to di
d55e0 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64  sk, not the inod
d55f0 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20  e.  We only set 
d5600 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20  dataOnly if the 
d5610 66 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a  file size is .**
d5620 20 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65   unchanged since
d5630 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69   the file size i
d5640 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e  s part of the in
d5650 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a  ode.  However, .
d5660 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c  ** Ted Ts'o tell
d5670 73 20 75 73 20 74 68 61 74 20 66 64 61 74 61 73  s us that fdatas
d5680 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20  ync() will also 
d5690 77 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20  write the inode 
d56a0 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73  if the.** file s
d56b0 69 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e  ize has changed.
d56c0 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20    The only real 
d56d0 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
d56e0 65 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a  en fdatasync().*
d56f0 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54  * and fsync(), T
d5700 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20  ed tells us, is 
d5710 74 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 29  that fdatasync()
d5720 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20   will not flush 
d5730 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20  the.** inode if 
d5740 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e  the mtime or own
d5750 65 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64  er or other inod
d5760 65 20 61 74 74 72 69 62 75 74 65 73 20 68 61 76  e attributes hav
d5770 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65  e changed..** We
d5780 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74   only care about
d5790 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20   the file size, 
d57a0 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 69  not the other fi
d57b0 6c 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 73  le attributes, s
d57c0 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53  o.** as far as S
d57d0 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e  QLite is concern
d57e0 65 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63  ed, an fdatasync
d57f0 28 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 65  () is always ade
d5800 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65  quate..** So, we
d5810 20 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 74   always use fdat
d5820 61 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 73  async() if it is
d5830 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61   available, rega
d5840 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65  rdless of.** the
d5850 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61   value of the da
d5860 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a  taOnly flag..*/.
d5870 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f  static int full_
d5880 66 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e  fsync(int fd, in
d5890 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20  t fullSync, int 
d58a0 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74  dataOnly){.  int
d58b0 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66   rc;..  /* The f
d58c0 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f  ollowing "ifdef/
d58d0 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63  elif/else/" bloc
d58e0 6b 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73  k has the same s
d58f0 74 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a  tructure as.  **
d5900 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20   the one below. 
d5910 49 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64  It is replicated
d5920 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20   here solely to 
d5930 61 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67  avoid cluttering
d5940 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65   .  ** up the re
d5950 61 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65  al code with the
d5960 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
d5970 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f  R() macros..  */
d5980 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e  .#ifdef SQLITE_N
d5990 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f  O_SYNC.  UNUSED_
d59a0 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20  PARAMETER(fd);. 
d59b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
d59c0 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55  R(fullSync);.  U
d59d0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
d59e0 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66  dataOnly);.#elif
d59f0 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a   HAVE_FULLFSYNC.
d5a00 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
d5a10 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65  ER(dataOnly);.#e
d5a20 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  lse.  UNUSED_PAR
d5a30 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29  AMETER(fullSync)
d5a40 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
d5a50 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a  ETER(dataOnly);.
d5a60 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63  #endif..  /* Rec
d5a70 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ord the number o
d5a80 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 20  f times that we 
d5a90 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e  do a normal fsyn
d5aa0 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55  c() and .  ** FU
d5ab0 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73  LLSYNC.  This is
d5ac0 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73   used during tes
d5ad0 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74  ting to verify t
d5ae0 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 75  hat this procedu
d5af0 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c  re.  ** gets cal
d5b00 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 72  led with the cor
d5b10 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a  rect arguments..
d5b20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
d5b30 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75  TE_TEST.  if( fu
d5b40 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33  llSync ) sqlite3
d5b50 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b  _fullsync_count+
d5b60 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e  +;.  sqlite3_syn
d5b70 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69  c_count++;.#endi
d5b80 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f  f..  /* If we co
d5b90 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20  mpiled with the 
d5ba0 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66  SQLITE_NO_SYNC f
d5bb0 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e  lag, then syncin
d5bc0 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f  g is a.  ** no-o
d5bd0 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  p.  */.#ifdef SQ
d5be0 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72  LITE_NO_SYNC.  r
d5bf0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  c = SQLITE_OK;.#
d5c00 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53  elif HAVE_FULLFS
d5c10 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79  YNC.  if( fullSy
d5c20 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66  nc ){.    rc = f
d5c30 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46  cntl(fd, F_FULLF
d5c40 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  SYNC, 0);.  }els
d5c50 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  e{.    rc = 1;. 
d5c60 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46   }.  /* If the F
d5c70 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c  ULLFSYNC failed,
d5c80 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74   fall back to at
d5c90 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e  tempting an fsyn
d5ca0 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f  c()..  ** It sho
d5cb0 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62  uldn't be possib
d5cc0 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63  le for fullfsync
d5cd0 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20   to fail on the 
d5ce0 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65  local .  ** file
d5cf0 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29   system (on OSX)
d5d00 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64  , so failure ind
d5d10 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c  icates that FULL
d5d20 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74  FSYNC.  ** isn't
d5d30 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74   supported for t
d5d40 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e  his file system.
d5d50 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20   So, attempt an 
d5d60 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20  fsync .  ** and 
d5d70 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65  (for now) ignore
d5d80 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66   the overhead of
d5d90 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66   a superfluous f
d5da0 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a  cntl call.  .  *
d5db0 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65 72  * It'd be better
d5dc0 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66   to detect fullf
d5dd0 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63  sync support onc
d5de0 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a  e and avoid .  *
d5df0 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c  * the fcntl call
d5e00 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63   every time sync
d5e10 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f   is called..  */
d5e20 0a 20 20 69 66 28 20 72 63 20 29 20 72 63 20 3d  .  if( rc ) rc =
d5e30 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c   fsync(fd);..#el
d5e40 73 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61  se .  rc = fdata
d5e50 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53  sync(fd);.#if OS
d5e60 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72  _VXWORKS.  if( r
d5e70 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d  c==-1 && errno==
d5e80 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72  ENOTSUP ){.    r
d5e90 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20  c = fsync(fd);. 
d5ea0 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f   }.#endif /* OS_
d5eb0 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69  VXWORKS */.#endi
d5ec0 66 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54  f /* ifdef SQLIT
d5ed0 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48  E_NO_SYNC elif H
d5ee0 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f  AVE_FULLFSYNC */
d5ef0 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52  ..  if( OS_VXWOR
d5f00 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b  KS && rc!= -1 ){
d5f10 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d  .    rc = 0;.  }
d5f20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d5f30 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
d5f40 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61   all writes to a
d5f50 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65   particular file
d5f60 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74   are committed t
d5f70 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  o disk..**.** If
d5f80 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65   dataOnly==0 the
d5f90 6e 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20  n both the file 
d5fa0 69 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d  itself and its m
d5fb0 65 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a  etadata (file.**
d5fc0 20 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69   size, access ti
d5fd0 6d 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e  me, etc) are syn
d5fe0 63 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c  ced.  If dataOnl
d5ff0 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74  y!=0 then only t
d6000 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20  he.** file data 
d6010 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a  is synced..**.**
d6020 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73   Under Unix, als
d6030 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
d6040 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 65   the directory e
d6050 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c  ntry for the fil
d6060 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72  e.** has been cr
d6070 65 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69  eated by fsync-i
d6080 6e 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  ng the directory
d6090 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
d60a0 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77  he file..** If w
d60b0 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73  e do not do this
d60c0 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65   and we encounte
d60d0 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  r a power failur
d60e0 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  e, the directory
d60f0 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68  .** entry for th
d6100 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20  e journal might 
d6110 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 20  not exist after 
d6120 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20  we reboot.  The 
d6130 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74  next.** SQLite t
d6140 6f 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c  o access the fil
d6150 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20  e will not know 
d6160 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c  that the journal
d6170 20 65 78 69 73 74 73 20 28 62 65 63 61 75 73 65   exists (because
d6180 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72  .** the director
d6190 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  y entry for the 
d61a0 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65  journal was neve
d61b0 72 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 74  r created) and t
d61c0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a  he transaction.*
d61d0 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20  * will not roll 
d61e0 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20  back - possibly 
d61f0 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62  leading to datab
d6200 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  ase corruption..
d6210 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
d6220 69 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  ixSync(sqlite3_f
d6230 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
d6240 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  gs){.  int rc;. 
d6250 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d6260 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d6270 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f  ;..  int isDataO
d6280 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c  nly = (flags&SQL
d6290 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c  ITE_SYNC_DATAONL
d62a0 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c  Y);.  int isFull
d62b0 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78  sync = (flags&0x
d62c0 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43  0F)==SQLITE_SYNC
d62d0 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65  _FULL;..  /* Che
d62e0 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53  ck that one of S
d62f0 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41  QLITE_SYNC_NORMA
d6300 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61  L or FULL was pa
d6310 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74  ssed */.  assert
d6320 28 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53  ((flags&0x0F)==S
d6330 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41  QLITE_SYNC_NORMA
d6340 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67  L.      || (flag
d6350 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f  s&0x0F)==SQLITE_
d6360 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a  SYNC_FULL.  );..
d6370 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74    /* Unix cannot
d6380 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65  , but some syste
d6390 6d 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51  ms may return SQ
d63a0 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68  LITE_FULL from h
d63b0 65 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c  ere. This.  ** l
d63c0 69 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 74  ine is to test t
d63d0 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65  hat doing so doe
d63e0 73 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20  s not cause any 
d63f0 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20  problems..  */. 
d6400 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
d6410 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  lError( return S
d6420 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20  QLITE_FULL );.. 
d6430 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d6440 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 59  ;.  OSTRACE2("SY
d6450 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70  NC    %-3d\n", p
d6460 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d  File->h);.  rc =
d6470 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c   full_fsync(pFil
d6480 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63  e->h, isFullsync
d6490 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20  , isDataOnly);. 
d64a0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d64b0 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20  ( rc=1 );.  if( 
d64c0 72 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  rc ){.    pFile-
d64d0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
d64e0 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  no;.    return S
d64f0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e  QLITE_IOERR_FSYN
d6500 43 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69  C;.  }.  if( pFi
d6510 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a  le->dirfd>=0 ){.
d6520 20 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20      int err;.   
d6530 20 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59   OSTRACE4("DIRSY
d6540 4e 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75  NC %-3d (have_fu
d6550 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73  llfsync=%d fulls
d6560 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c  ync=%d)\n", pFil
d6570 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20  e->dirfd,.      
d6580 20 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46        HAVE_FULLF
d6590 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63  SYNC, isFullsync
d65a0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
d65b0 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e  E_DISABLE_DIRSYN
d65c0 43 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72  C.    /* The dir
d65d0 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f  ectory sync is o
d65e0 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66  nly attempted if
d65f0 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20   full_fsync is. 
d6600 20 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66     ** turned off
d6610 20 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e   or unavailable.
d6620 20 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e    If a full_fsyn
d6630 63 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65  c occurred above
d6640 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68  ,.    ** then th
d6650 65 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63  e directory sync
d6660 20 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e   is superfluous.
d6670 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
d6680 28 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  (!HAVE_FULLFSYNC
d6690 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29   || !isFullsync)
d66a0 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70   && full_fsync(p
d66b0 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29  File->dirfd,0,0)
d66c0 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20   ){.       /*.  
d66d0 20 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20       ** We have 
d66e0 72 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c  received multipl
d66f0 65 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79  e reports of fsy
d6700 6e 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20  nc() returning. 
d6710 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20        ** errors 
d6720 77 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20  when applied to 
d6730 64 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63  directories on c
d6740 65 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74  ertain file syst
d6750 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41  ems..       ** A
d6760 20 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72   failed director
d6770 79 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20  y sync is not a 
d6780 62 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74  big deal.  So it
d6790 20 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a   seems.       **
d67a0 20 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72   better to ignor
d67b0 65 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69  e the error.  Ti
d67c0 63 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20  cket #1657.     
d67d0 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70    */.       /* p
d67e0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d67f0 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20  = errno; */.    
d6800 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c     /* return SQL
d6810 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20  ITE_IOERR; */.  
d6820 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65    }.#endif.    e
d6830 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65  rr = close(pFile
d6840 2d 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c  ->dirfd); /* Onl
d6850 79 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f  y need to sync o
d6860 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68  nce, so close th
d6870 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72  e */.    if( err
d6880 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  ==0 ){          
d6890 20 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79      /* directory
d68a0 20 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e   when we are don
d68b0 65 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65  e */.      pFile
d68c0 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20  ->dirfd = -1;.  
d68d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
d68e0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d68f0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72  = errno;.      r
d6900 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
d6910 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20  _DIR_CLOSE;.    
d6920 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
d6930 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e  c;.}../*.** Trun
d6940 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c  cate an open fil
d6950 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64  e to a specified
d6960 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20   size.*/.static 
d6970 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65  int unixTruncate
d6980 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d6990 64 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20  d, i64 nByte){. 
d69a0 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
d69b0 74 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c  t( id );.  Simul
d69c0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
d69d0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
d69e0 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63  TRUNCATE );.  rc
d69f0 20 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 75   = ftruncate(((u
d6a00 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c  nixFile*)id)->h,
d6a10 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a   (off_t)nByte);.
d6a20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
d6a30 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
d6a40 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
d6a50 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  no;.    return S
d6a60 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
d6a70 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  CATE;.  }else{. 
d6a80 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d6a90 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  _OK;.  }.}../*.*
d6aa0 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
d6ab0 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  current size of 
d6ac0 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a  a file in bytes.
d6ad0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
d6ae0 69 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74  ixFileSize(sqlit
d6af0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34  e3_file *id, i64
d6b00 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20   *pSize){.  int 
d6b10 72 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61  rc;.  struct sta
d6b20 74 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28  t buf;.  assert(
d6b30 20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73   id );.  rc = fs
d6b40 74 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29  tat(((unixFile*)
d6b50 69 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20  id)->h, &buf);. 
d6b60 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d6b70 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20  ( rc=1 );.  if( 
d6b80 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75  rc!=0 ){.    ((u
d6b90 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
d6ba0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
d6bb0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d6bc0 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a  TE_IOERR_FSTAT;.
d6bd0 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62    }.  *pSize = b
d6be0 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f  uf.st_size;..  /
d6bf0 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61  * When opening a
d6c00 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62   zero-size datab
d6c10 61 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63  ase, the findLoc
d6c20 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72  kInfo() procedur
d6c30 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20  e.  ** writes a 
d6c40 73 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f  single byte into
d6c50 20 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72   that file in or
d6c60 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75  der to work arou
d6c70 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e  nd a bug.  ** in
d6c80 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20   the OS-X msdos 
d6c90 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20  filesystem.  In 
d6ca0 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70  order to avoid p
d6cb0 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70  roblems with upp
d6cc0 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20  er.  ** layers, 
d6cd0 77 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72  we need to repor
d6ce0 74 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65  t this file size
d6cf0 20 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68   as zero even th
d6d00 6f 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20  ough it is.  ** 
d6d10 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b  really 1.   Tick
d6d20 65 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20  et #3260..  */. 
d6d30 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29   if( *pSize==1 )
d6d40 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20   *pSize = 0;... 
d6d50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d6d60 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45  K;.}..#if SQLITE
d6d70 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
d6d80 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64  STYLE && defined
d6d90 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a  (__APPLE__)./*.*
d6da0 2a 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72  * Handler for pr
d6db0 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65  oxy-locking file
d6dc0 2d 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20  -control verbs. 
d6dd0 20 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69   Defined below i
d6de0 6e 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e  n the.** proxyin
d6df0 67 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69  g locking divisi
d6e00 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
d6e10 74 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72  t proxyFileContr
d6e20 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ol(sqlite3_file*
d6e30 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e  ,int,void*);.#en
d6e40 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f  dif.../*.** Info
d6e50 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74  rmation and cont
d6e60 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66  rol of an open f
d6e70 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73  ile handle..*/.s
d6e80 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69  tatic int unixFi
d6e90 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
d6ea0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
d6eb0 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
d6ec0 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
d6ed0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
d6ee0 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
d6ef0 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a  : {.      *(int*
d6f00 29 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69  )pArg = ((unixFi
d6f10 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70  le*)id)->locktyp
d6f20 65 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e;.      return 
d6f30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
d6f40 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
d6f50 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20  _LAST_ERRNO: {. 
d6f60 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67       *(int*)pArg
d6f70 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69   = ((unixFile*)i
d6f80 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20  d)->lastErrno;. 
d6f90 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
d6fa0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66  TE_OK;.    }.#if
d6fb0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
d6fc0 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 6c  /* The pager cal
d6fd0 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74  ls this method t
d6fe0 6f 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74  o signal that it
d6ff0 20 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a   has done.    **
d7000 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20   a rollback and 
d7010 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
d7020 65 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75  e is therefore u
d7030 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20  nchanged and.   
d7040 20 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20   ** it hence it 
d7050 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72  is OK for the tr
d7060 61 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65  ansaction change
d7070 20 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20   counter to be. 
d7080 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e     ** unchanged.
d7090 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
d70a0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42   SQLITE_FCNTL_DB
d70b0 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20  _UNCHANGED: {.  
d70c0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
d70d0 69 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20  id)->dbUpdate = 
d70e0 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
d70f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
d7100 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
d7110 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
d7120 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e  G_STYLE && defin
d7130 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20  ed(__APPLE__).  
d7140 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45    case SQLITE_SE
d7150 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a  T_LOCKPROXYFILE:
d7160 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
d7170 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _GET_LOCKPROXYFI
d7180 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75  LE: {.      retu
d7190 72 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74  rn proxyFileCont
d71a0 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b  rol(id,op,pArg);
d71b0 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
d71c0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
d71d0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20  OCKING_STYLE && 
d71e0 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
d71f0 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75  _) */.  }.  retu
d7200 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
d7210 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
d7220 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
d7230 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65   in bytes of the
d7240 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63   underlying bloc
d7250 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20  k device for.** 
d7260 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69  the specified fi
d7270 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f  le. This is almo
d7280 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79  st always 512 by
d7290 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a  tes, but may be.
d72a0 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f  ** larger for so
d72b0 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a  me devices..**.*
d72c0 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73  * SQLite code as
d72d0 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74  sumes this funct
d72e0 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e  ion cannot fail.
d72f0 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73   It also assumes
d7300 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20   that.** if two 
d7310 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65  files are create
d7320 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69  d in the same fi
d7330 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74  le-system direct
d7340 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64  ory (i.e..** a d
d7350 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20  atabase and its 
d7360 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68  journal file) th
d7370 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  at the sector si
d7380 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a  ze will be the.*
d7390 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e  * same for both.
d73a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
d73b0 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71  nixSectorSize(sq
d73c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55  lite3_file *NotU
d73d0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
d73e0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
d73f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
d7400 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
d7410 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  R_SIZE;.}../*.**
d7420 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69   Return the devi
d7430 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69  ce characteristi
d7440 63 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e  cs for the file.
d7450 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20   This is always 
d7460 30 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73  0 for unix..*/.s
d7470 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65  tatic int unixDe
d7480 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
d7490 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ics(sqlite3_file
d74a0 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
d74b0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
d74c0 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
d74d0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65  n 0;.}../*.** He
d74e0 72 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c  re ends the impl
d74f0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c  ementation of al
d7500 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d  l sqlite3_file m
d7510 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  ethods..**.*****
d7520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7530 2a 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69  * End sqlite3_fi
d7540 6c 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a  le Methods *****
d7550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
d7570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d75a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d75b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
d75c0 2a 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20  * This division 
d75d0 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74  contains definit
d75e0 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  ions of sqlite3_
d75f0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  io_methods objec
d7600 74 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65  ts that.** imple
d7610 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c  ment various fil
d7620 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65  e locking strate
d7630 67 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63  gies.  It also c
d7640 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69  ontains definiti
d7650 6f 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65  ons.** of "finde
d7660 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41  r" functions.  A
d7670 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
d7680 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61   is used to loca
d7690 74 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  te the appropria
d76a0 74 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f  te.** sqlite3_io
d76b0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20  _methods object 
d76c0 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72  for a particular
d76d0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
d76e0 20 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a   The pAppData.**
d76f0 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71   field of the sq
d7700 6c 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62  lite3_vfs VFS ob
d7710 6a 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 61  jects are initia
d7720 6c 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e  lized to be poin
d7730 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63  ters to.** the c
d7740 6f 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75  orrect finder-fu
d7750 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20  nction for that 
d7760 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20  VFS..**.** Most 
d7770 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73  finder functions
d7780 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
d7790 72 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c  r to a fixed sql
d77a0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a  ite3_io_methods.
d77b0 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20  ** object.  The 
d77c0 6f 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67  only interesting
d77d0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
d77e0 20 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69   is autolockIoFi
d77f0 6e 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c  nder, which.** l
d7800 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65  ooks at the file
d7810 73 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20  system type and 
d7820 74 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 74  tries to guess t
d7830 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a  he best locking.
d7840 2a 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d  ** strategy from
d7850 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72   that..**.** For
d7860 20 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20   finder-funtion 
d7870 46 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61  F, two objects a
d7880 72 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a  re created:.**.*
d7890 2a 20 20 20 20 28 31 29 20 54 68 65 20 72 65 61  *    (1) The rea
d78a0 6c 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  l finder-functio
d78b0 6e 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29  n named "FImpt()
d78c0 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20  "..**.**    (2) 
d78d0 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74  A constant point
d78e0 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  er to this funct
d78f0 69 6f 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46  io named just "F
d7900 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f  "..**.**.** A po
d7910 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70  inter to the F p
d7920 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61  ointer is used a
d7930 73 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76  s the pAppData v
d7940 61 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20  alue for VFS.** 
d7950 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76  objects.  We hav
d7960 65 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73  e to do this ins
d7970 74 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20  tead of letting 
d7980 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a  pAppData point.*
d7990 2a 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68  * directly at th
d79a0 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  e finder-functio
d79b0 6e 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65  n since C90 rule
d79c0 73 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64  s prevent a void
d79d0 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73  *.** from be cas
d79e0 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f  t into a functio
d79f0 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  n pointer..**.**
d7a00 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63  .** Each instanc
d7a10 65 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20  e of this macro 
d7a20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62  generates two ob
d7a30 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  jects:.**.**   *
d7a40 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c    A constant sql
d7a50 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
d7a60 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48  object call METH
d7a70 4f 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b  OD that has lock
d7a80 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68  ing.**      meth
d7a90 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c  ods CLOSE, LOCK,
d7aa0 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f   UNLOCK, CKRESLO
d7ab0 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41  CK..**.**   *  A
d7ac0 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e  n I/O method fin
d7ad0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  der function cal
d7ae0 6c 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20  led FINDER that 
d7af0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
d7b00 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65  r.**      to the
d7b10 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69   METHOD object i
d7b20 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62  n the previous b
d7b30 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ullet..*/.#defin
d7b40 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44  e IOMETHODS(FIND
d7b50 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53  ER, METHOD, CLOS
d7b60 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c  E, LOCK, UNLOCK,
d7b70 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20   CKLOCK)        
d7b80 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20         \.static 
d7b90 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
d7ba0 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20  _methods METHOD 
d7bb0 3d 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  = {             
d7bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7bd0 20 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20        \.   1,   
d7be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7bf0 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
d7c00 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  on */           
d7c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7c20 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c       \.   CLOSE,
d7c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7c40 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20        /* xClose 
d7c50 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
d7c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7c70 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61      \.   unixRea
d7c80 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
d7c90 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f       /* xRead */
d7ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7cc0 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74     \.   unixWrit
d7cd0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
d7ce0 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f      /* xWrite */
d7cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7d10 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63    \.   unixTrunc
d7d20 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ate,            
d7d30 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20     /* xTruncate 
d7d40 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
d7d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7d60 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20   \.   unixSync, 
d7d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7d80 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20    /* xSync */   
d7d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7db0 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a  \.   unixFileSiz
d7dc0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
d7dd0 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f   /* xFileSize */
d7de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
d7e00 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20  .   LOCK,       
d7e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7e20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20  /* xLock */     
d7e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
d7e50 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20     UNLOCK,      
d7e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d7e70 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20  * xUnlock */    
d7e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7e90 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
d7ea0 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20    CKLOCK,       
d7eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d7ec0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
d7ed0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ock */          
d7ee0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
d7ef0 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c   unixFileControl
d7f00 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
d7f10 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20  xFileControl */ 
d7f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7f30 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
d7f40 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20  unixSectorSize, 
d7f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
d7f60 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20  SectorSize */   
d7f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7f80 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75            \.   u
d7f90 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74  nixDeviceCharact
d7fa0 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44  eristics   /* xD
d7fb0 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65  eviceCapabilitie
d7fc0 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  s */            
d7fd0 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20           \.};   
d7fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8020 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63          \.static
d8030 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
d8040 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45  o_methods *FINDE
d8050 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68  R##Impl(const ch
d8060 61 72 20 2a 7a 2c 20 69 6e 74 20 68 29 7b 20 20  ar *z, int h){  
d8070 20 20 20 20 20 20 20 5c 0a 20 20 55 4e 55 53 45         \.  UNUSE
d8080 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20  D_PARAMETER(z); 
d8090 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
d80a0 28 68 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  (h);            
d80b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d80c0 20 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e        \.  return
d80d0 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20   &METHOD;       
d80e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d80f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8110 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20       \.}        
d8120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d8160 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e      \.static con
d8170 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
d8180 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46  thods *(*const F
d8190 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61  INDER)(const cha
d81a0 72 2a 2c 69 6e 74 29 20 20 20 20 20 20 20 20 20  r*,int)         
d81b0 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45     \.    = FINDE
d81c0 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20  R##Impl;../*.** 
d81d0 48 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20  Here are all of 
d81e0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  the sqlite3_io_m
d81f0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66  ethods objects f
d8200 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a  or each of the.*
d8210 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65  * locking strate
d8220 67 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73  gies.  Functions
d8230 20 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69   that return poi
d8240 6e 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d  nters to these m
d8250 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c  ethods.** are al
d8260 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49  so created..*/.I
d8270 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69  OMETHODS(.  posi
d8280 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20  xIoFinder,      
d8290 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
d82a0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
d82b0 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64  .  posixIoMethod
d82c0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
d82d0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
d82e0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
d82f0 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20  /.  unixClose,  
d8300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d8310 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
d8320 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20  /.  unixLock,   
d8330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d8340 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
d8350 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20  .  unixUnlock,  
d8360 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d8370 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
d8380 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73  /.  unixCheckRes
d8390 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a  ervedLock     /*
d83a0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
d83b0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
d83c0 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c  IOMETHODS(.  nol
d83d0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  ockIoFinder,    
d83e0 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72         /* Finder
d83f0 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
d8400 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68  /.  nolockIoMeth
d8410 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ods,          /*
d8420 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
d8430 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ods object name 
d8440 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65  */.  nolockClose
d8450 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
d8460 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
d8470 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c  */.  nolockLock,
d8480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d8490 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  * xLock method *
d84a0 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b  /.  nolockUnlock
d84b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
d84c0 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20   xUnlock method 
d84d0 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b  */.  nolockCheck
d84e0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f  ReservedLock   /
d84f0 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
d8500 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29  Lock method */.)
d8510 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f  .IOMETHODS(.  do
d8520 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20  tlockIoFinder,  
d8530 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
d8540 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
d8550 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65  */.  dotlockIoMe
d8560 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f  thods,         /
d8570 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
d8580 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
d8590 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f   */.  dotlockClo
d85a0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
d85b0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
d85c0 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63   */.  dotlockLoc
d85d0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
d85e0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
d85f0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f  */.  dotlockUnlo
d8600 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ck,            /
d8610 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
d8620 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65   */.  dotlockChe
d8630 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20  ckReservedLock  
d8640 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
d8650 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
d8660 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  )..#if SQLITE_EN
d8670 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
d8680 4c 45 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20  LE.IOMETHODS(.  
d8690 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20  flockIoFinder,  
d86a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
d86b0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der function nam
d86c0 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65  e */.  flockIoMe
d86d0 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20  thods,          
d86e0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   /* sqlite3_io_m
d86f0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61  ethods object na
d8700 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f  me */.  flockClo
d8710 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
d8720 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68    /* xClose meth
d8730 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63  od */.  flockLoc
d8740 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
d8750 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f    /* xLock metho
d8760 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f  d */.  flockUnlo
d8770 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
d8780 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68   /* xUnlock meth
d8790 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65  od */.  flockChe
d87a0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20  ckReservedLock  
d87b0 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
d87c0 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  vedLock method *
d87d0 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  /.).#endif..#if 
d87e0 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54  OS_VXWORKS.IOMET
d87f0 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e  HODS(.  semIoFin
d8800 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  der,            
d8810 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63    /* Finder func
d8820 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73  tion name */.  s
d8830 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20  emIoMethods,    
d8840 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69           /* sqli
d8850 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
d8860 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20  bject name */.  
d8870 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  semClose,       
d8880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c            /* xCl
d8890 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ose method */.  
d88a0 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20  semLock,        
d88b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f            /* xLo
d88c0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73  ck method */.  s
d88d0 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20  emUnlock,       
d88e0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c           /* xUnl
d88f0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
d8900 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64  semCheckReserved
d8910 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68  Lock      /* xCh
d8920 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
d8930 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64  method */.).#end
d8940 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  if..#if defined(
d8950 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51  __APPLE__) && SQ
d8960 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
d8970 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48  ING_STYLE.IOMETH
d8980 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64  ODS(.  afpIoFind
d8990 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
d89a0 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74   /* Finder funct
d89b0 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66  ion name */.  af
d89c0 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20  pIoMethods,     
d89d0 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
d89e0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
d89f0 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61  ject name */.  a
d8a00 66 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  fpClose,        
d8a10 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
d8a20 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61  se method */.  a
d8a30 66 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  fpLock,         
d8a40 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
d8a50 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66  k method */.  af
d8a60 70 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  pUnlock,        
d8a70 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
d8a80 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61  ck method */.  a
d8a90 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  fpCheckReservedL
d8aa0 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65  ock      /* xChe
d8ab0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d  ckReservedLock m
d8ac0 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69  ethod */.).#endi
d8ad0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f  f../*.** The pro
d8ae0 78 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  xy locking metho
d8af0 64 20 69 73 20 61 20 22 73 75 70 65 72 2d 6d 65  d is a "super-me
d8b00 74 68 6f 64 22 20 69 6e 20 74 68 65 20 73 65 6e  thod" in the sen
d8b10 73 65 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70  se that it.** op
d8b20 65 6e 73 20 73 65 63 6f 6e 64 61 72 79 20 66 69  ens secondary fi
d8b30 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 66  le descriptors f
d8b40 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 61 6e 64  or the conch and
d8b50 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a   lock files and.
d8b60 2a 2a 20 69 74 20 75 73 65 73 20 70 72 6f 78 79  ** it uses proxy
d8b70 2c 20 64 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c  , dot-file, AFP,
d8b80 20 61 6e 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63   and flock() loc
d8b90 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20  king methods on 
d8ba0 74 68 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61  those.** seconda
d8bb0 72 79 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 74  ry files.  For t
d8bc0 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20  his reason, the 
d8bd0 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20 69 6d  division that im
d8be0 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78  plements.** prox
d8bf0 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63  y locking is loc
d8c00 61 74 65 64 20 6d 75 63 68 20 66 75 72 74 68 65  ated much furthe
d8c10 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 66 69  r down in the fi
d8c20 6c 65 2e 20 20 42 75 74 20 77 65 20 6e 65 65 64  le.  But we need
d8c30 0a 2a 2a 20 74 6f 20 67 6f 20 61 68 65 61 64 20  .** to go ahead 
d8c40 61 6e 64 20 64 65 66 69 6e 65 20 74 68 65 20 73  and define the s
d8c50 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
d8c60 73 20 61 6e 64 20 66 69 6e 64 65 72 20 66 75 6e  s and finder fun
d8c70 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f  ction.** for pro
d8c80 78 79 20 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e  xy locking here.
d8c90 20 20 53 6f 20 77 65 20 66 6f 72 77 61 72 64 20    So we forward 
d8ca0 64 65 63 6c 61 72 65 20 74 68 65 20 49 2f 4f 20  declare the I/O 
d8cb0 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20  methods..*/.#if 
d8cc0 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
d8cd0 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  _) && SQLITE_ENA
d8ce0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
d8cf0 45 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  E.static int pro
d8d00 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  xyClose(sqlite3_
d8d10 66 69 6c 65 2a 29 3b 0a 73 74 61 74 69 63 20 69  file*);.static i
d8d20 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c  nt proxyLock(sql
d8d30 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29  ite3_file*, int)
d8d40 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  ;.static int pro
d8d50 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  xyUnlock(sqlite3
d8d60 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74  _file*, int);.st
d8d70 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68  atic int proxyCh
d8d80 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
d8d90 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
d8da0 6e 74 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28  nt*);.IOMETHODS(
d8db0 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72  .  proxyIoFinder
d8dc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
d8dd0 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  Finder function 
d8de0 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49  name */.  proxyI
d8df0 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
d8e00 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69      /* sqlite3_i
d8e10 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
d8e20 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79   name */.  proxy
d8e30 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
d8e40 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d       /* xClose m
d8e50 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79  ethod */.  proxy
d8e60 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
d8e70 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65       /* xLock me
d8e80 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 55  thod */.  proxyU
d8e90 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  nlock,          
d8ea0 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d      /* xUnlock m
d8eb0 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79  ethod */.  proxy
d8ec0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d8ed0 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65  k    /* xCheckRe
d8ee0 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f  servedLock metho
d8ef0 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 0a  d */.).#endif...
d8f00 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
d8f10 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
d8f20 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
d8f30 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69  STYLE./* .** Thi
d8f40 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  s "finder" funct
d8f50 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
d8f60 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65  determine the be
d8f70 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74  st locking strat
d8f80 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20  egy .** for the 
d8f90 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 22 66  database file "f
d8fa0 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 74 68  ilePath".  It th
d8fb0 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  en returns the s
d8fc0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
d8fd0 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74  s.** object that
d8fe0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74   implements that
d8ff0 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a   strategy..**.**
d9000 20 54 68 69 73 20 69 73 20 66 6f 72 20 4d 61 63   This is for Mac
d9010 4f 53 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61  OSX only..*/.sta
d9020 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
d9030 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75  3_io_methods *au
d9040 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d  tolockIoFinderIm
d9050 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  pl(.  const char
d9060 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f   *filePath,    /
d9070 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * name of the da
d9080 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
d9090 20 69 6e 74 20 66 64 20 20 20 20 20 20 20 20 20   int fd         
d90a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c            /* fil
d90b0 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65  e descriptor ope
d90c0 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
d90d0 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73  e file */.){.  s
d90e0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
d90f0 63 74 20 4d 61 70 70 69 6e 67 20 7b 0a 20 20 20  ct Mapping {.   
d9100 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
d9110 6c 65 73 79 73 74 65 6d 3b 20 20 20 20 20 20 20  lesystem;       
d9120 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 73 79         /* Filesy
d9130 73 74 65 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a  stem type name *
d9140 2f 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69  /.    const sqli
d9150 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
d9160 70 4d 65 74 68 6f 64 73 3b 20 20 20 2f 2a 20 41  pMethods;   /* A
d9170 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69  ppropriate locki
d9180 6e 67 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7d  ng method */.  }
d9190 20 61 4d 61 70 5b 5d 20 3d 20 7b 0a 20 20 20 20   aMap[] = {.    
d91a0 7b 20 22 68 66 73 22 2c 20 20 20 20 26 70 6f 73  { "hfs",    &pos
d91b0 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20  ixIoMethods },. 
d91c0 20 20 20 7b 20 22 75 66 73 22 2c 20 20 20 20 26     { "ufs",    &
d91d0 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d  posixIoMethods }
d91e0 2c 0a 20 20 20 20 7b 20 22 61 66 70 66 73 22 2c  ,.    { "afpfs",
d91f0 20 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20    &afpIoMethods 
d9200 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  },.#ifdef SQLITE
d9210 5f 45 4e 41 42 4c 45 5f 41 46 50 5f 4c 4f 43 4b  _ENABLE_AFP_LOCK
d9220 49 4e 47 5f 53 4d 42 0a 20 20 20 20 7b 20 22 73  ING_SMB.    { "s
d9230 6d 62 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65  mbfs",  &afpIoMe
d9240 74 68 6f 64 73 20 7d 2c 0a 23 65 6c 73 65 0a 20  thods },.#else. 
d9250 20 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26     { "smbfs",  &
d9260 66 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d  flockIoMethods }
d9270 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 20 22  ,.#endif.    { "
d9280 77 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 6b  webdav", &nolock
d9290 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20  IoMethods },.   
d92a0 20 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a 20   { 0, 0 }.  };. 
d92b0 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
d92c0 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a   statfs fsInfo;.
d92d0 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
d92e0 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20  ockInfo;..  if( 
d92f0 21 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20  !filePath ){.   
d9300 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d   /* If filePath=
d9310 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73  =NULL that means
d9320 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
d9330 77 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74  with a transient
d9340 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61   file.    ** tha
d9350 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  t does not need 
d9360 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f  to be locked. */
d9370 0a 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c  .    return &nol
d9380 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20  ockIoMethods;.  
d9390 7d 0a 20 20 69 66 28 20 73 74 61 74 66 73 28 66  }.  if( statfs(f
d93a0 69 6c 65 50 61 74 68 2c 20 26 66 73 49 6e 66 6f  ilePath, &fsInfo
d93b0 29 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 69  ) != -1 ){.    i
d93c0 66 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67  f( fsInfo.f_flag
d93d0 73 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29  s & MNT_RDONLY )
d93e0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 26  {.      return &
d93f0 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b  nolockIoMethods;
d9400 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
d9410 3d 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c  =0; aMap[i].zFil
d9420 65 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a 20  esystem; i++){. 
d9430 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
d9440 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e  fsInfo.f_fstypen
d9450 61 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 69  ame, aMap[i].zFi
d9460 6c 65 73 79 73 74 65 6d 29 3d 3d 30 20 29 7b 0a  lesystem)==0 ){.
d9470 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 61          return a
d9480 4d 61 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 3b  Map[i].pMethods;
d9490 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d94a0 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74   }..  /* Default
d94b0 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20   case. Handles, 
d94c0 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20  amongst others, 
d94d0 22 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 65 73 74  "nfs"..  ** Test
d94e0 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b   byte-range lock
d94f0 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20   using fcntl(). 
d9500 49 66 20 74 68 65 20 63 61 6c 6c 20 73 75 63 63  If the call succ
d9510 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75  eeds, .  ** assu
d9520 6d 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65  me that the file
d9530 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73  -system supports
d9540 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63   POSIX style loc
d9550 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b  ks. .  */.  lock
d9560 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a  Info.l_len = 1;.
d9570 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61    lockInfo.l_sta
d9580 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e  rt = 0;.  lockIn
d9590 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45  fo.l_whence = SE
d95a0 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e  EK_SET;.  lockIn
d95b0 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44  fo.l_type = F_RD
d95c0 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c  LCK;.  if( fcntl
d95d0 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c  (fd, F_GETLK, &l
d95e0 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b  ockInfo)!=-1 ) {
d95f0 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73  .    return &pos
d9600 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  ixIoMethods;.  }
d9610 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
d9620 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f   &dotlockIoMetho
d9630 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  ds;.  }.}.static
d9640 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
d9650 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e  o_methods *(*con
d9660 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e  st autolockIoFin
d9670 64 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  der)(const char*
d9680 2c 69 6e 74 29 0a 20 20 20 20 20 20 20 20 3d 20  ,int).        = 
d9690 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  autolockIoFinder
d96a0 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a  Impl;..#endif /*
d96b0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
d96c0 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e  __) && SQLITE_EN
d96d0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
d96e0 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  LE */../*.** An 
d96f0 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66 6f  abstract type fo
d9700 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  r a pointer to a
d9710 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64 65   IO method finde
d9720 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f 0a 74  r function:.*/.t
d9730 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 71 6c  ypedef const sql
d9740 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
d9750 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 29 28  *(*finder_type)(
d9760 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29  const char*,int)
d9770 3b 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.../***********
d9780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d97a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d97b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d97c0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
d97d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73  ************** s
d97e0 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f  qlite3_vfs metho
d97f0 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ds *************
d9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
d9810 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 73  **.** This divis
d9820 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ion contains the
d9830 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
d9840 6f 66 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68  of methods on th
d9850 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73  e.** sqlite3_vfs
d9860 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a   object..*/../*.
d9870 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** Initialize th
d9880 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
d9890 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63  e unixFile struc
d98a0 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  ture pointed to 
d98b0 62 79 20 70 49 64 2e 0a 2a 2f 0a 73 74 61 74 69  by pId..*/.stati
d98c0 63 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e 69 78  c int fillInUnix
d98d0 46 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  File(.  sqlite3_
d98e0 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
d98f0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66  /* Pointer to vf
d9900 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  s object */.  in
d9910 74 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20  t h,            
d9920 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69        /* Open fi
d9930 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 66  le descriptor of
d9940 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e   file being open
d9950 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66  ed */.  int dirf
d9960 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
d9970 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c  /* Directory fil
d9980 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a  e descriptor */.
d9990 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
d99a0 70 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69  pId,      /* Wri
d99b0 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69  te to the unixFi
d99c0 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 65 72  le structure her
d99d0 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
d99e0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f  r *zFilename,  /
d99f0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69  * Name of the fi
d9a00 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20  le being opened 
d9a10 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 2c  */.  int noLock,
d9a20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d9a30 4f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 66 20  Omit locking if 
d9a40 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73  true */.  int is
d9a50 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20  Delete          
d9a60 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63    /* Delete on c
d9a70 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a  lose if true */.
d9a80 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  ){.  const sqlit
d9a90 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70  e3_io_methods *p
d9aa0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20  LockingStyle;.  
d9ab0 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d  unixFile *pNew =
d9ac0 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49 64   (unixFile *)pId
d9ad0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
d9ae0 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72  ITE_OK;..  asser
d9af0 74 28 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b 3d 3d  t( pNew->pLock==
d9b00 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74  NULL );.  assert
d9b10 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 3d 3d 4e  ( pNew->pOpen==N
d9b20 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 50 61 72  ULL );..  /* Par
d9b30 61 6d 65 74 65 72 20 69 73 44 65 6c 65 74 65 20  ameter isDelete 
d9b40 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 6f 6e 20  is only used on 
d9b50 76 78 77 6f 72 6b 73 2e 0a 20 20 2a 2a 20 45 78  vxworks..  ** Ex
d9b60 70 72 65 73 73 20 74 68 69 73 20 65 78 70 6c 69  press this expli
d9b70 63 69 74 6c 79 20 68 65 72 65 20 74 6f 20 70 72  citly here to pr
d9b80 65 76 65 6e 74 20 63 6f 6d 70 69 6c 65 72 20 77  event compiler w
d9b90 61 72 6e 69 6e 67 73 0a 20 20 2a 2a 20 61 62 6f  arnings.  ** abo
d9ba0 75 74 20 75 6e 75 73 65 64 20 70 61 72 61 6d 65  ut unused parame
d9bb0 74 65 72 73 2e 0a 20 20 2a 2f 0a 23 69 66 20 21  ters..  */.#if !
d9bc0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 55 4e 55  OS_VXWORKS.  UNU
d9bd0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 73  SED_PARAMETER(is
d9be0 44 65 6c 65 74 65 29 3b 0a 23 65 6e 64 69 66 0a  Delete);.#endif.
d9bf0 0a 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45  .  OSTRACE3("OPE
d9c00 4e 20 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c  N    %-3d %s\n",
d9c10 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20   h, zFilename); 
d9c20 20 20 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20     .  pNew->h = 
d9c30 68 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64  h;.  pNew->dirfd
d9c40 20 3d 20 64 69 72 66 64 3b 0a 20 20 53 45 54 5f   = dirfd;.  SET_
d9c50 54 48 52 45 41 44 49 44 28 70 4e 65 77 29 3b 0a  THREADID(pNew);.
d9c60 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
d9c70 20 20 70 4e 65 77 2d 3e 70 49 64 20 3d 20 76 78    pNew->pId = vx
d9c80 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28  worksFindFileId(
d9c90 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66  zFilename);.  if
d9ca0 28 20 70 4e 65 77 2d 3e 70 49 64 3d 3d 30 20 29  ( pNew->pId==0 )
d9cb0 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b 20 3d 20 31  {.    noLock = 1
d9cc0 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
d9cd0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e  E_NOMEM;.  }.#en
d9ce0 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f 4c 6f 63  dif..  if( noLoc
d9cf0 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e  k ){.    pLockin
d9d00 67 53 74 79 6c 65 20 3d 20 26 6e 6f 6c 6f 63 6b  gStyle = &nolock
d9d10 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c  IoMethods;.  }el
d9d20 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67  se{.    pLocking
d9d30 53 74 79 6c 65 20 3d 20 28 2a 2a 28 66 69 6e 64  Style = (**(find
d9d40 65 72 5f 74 79 70 65 2a 29 70 56 66 73 2d 3e 70  er_type*)pVfs->p
d9d50 41 70 70 44 61 74 61 29 28 7a 46 69 6c 65 6e 61  AppData)(zFilena
d9d60 6d 65 2c 20 68 29 3b 0a 23 69 66 20 53 51 4c 49  me, h);.#if SQLI
d9d70 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
d9d80 47 5f 53 54 59 4c 45 0a 20 20 20 20 2f 2a 20 43  G_STYLE.    /* C
d9d90 61 63 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 69  ache zFilename i
d9da0 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f  n the locking co
d9db0 6e 74 65 78 74 20 28 41 46 50 20 61 6e 64 20 64  ntext (AFP and d
d9dc0 6f 74 6c 6f 63 6b 20 6f 76 65 72 72 69 64 65 29  otlock override)
d9dd0 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 72 6f 78   for.    ** prox
d9de0 79 4c 6f 63 6b 20 61 63 74 69 76 61 74 69 6f 6e  yLock activation
d9df0 20 69 73 20 70 6f 73 73 69 62 6c 65 20 28 72 65   is possible (re
d9e00 6d 6f 74 65 20 70 72 6f 78 79 20 69 73 20 62 61  mote proxy is ba
d9e10 73 65 64 20 6f 6e 20 64 62 20 6e 61 6d 65 29 0a  sed on db name).
d9e20 20 20 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65      ** zFilename
d9e30 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75   remains valid u
d9e40 6e 74 69 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f  ntil file is clo
d9e50 73 65 64 2c 20 74 6f 20 73 75 70 70 6f 72 74 20  sed, to support 
d9e60 2a 2f 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63  */.    pNew->loc
d9e70 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 28 76  kingContext = (v
d9e80 6f 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 3b 0a  oid*)zFilename;.
d9e90 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 69 66  #endif.  }..  if
d9ea0 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  ( pLockingStyle 
d9eb0 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f  == &posixIoMetho
d9ec0 64 73 20 29 7b 0a 20 20 20 20 75 6e 69 78 45 6e  ds ){.    unixEn
d9ed0 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  terMutex();.    
d9ee0 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  rc = findLockInf
d9ef0 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70  o(pNew, &pNew->p
d9f00 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70  Lock, &pNew->pOp
d9f10 65 6e 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61  en);.    unixLea
d9f20 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 0a  veMutex();.  }..
d9f30 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
d9f40 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
d9f50 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  && defined(__APP
d9f60 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 66 28  LE__).  else if(
d9f70 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d   pLockingStyle =
d9f80 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20  = &afpIoMethods 
d9f90 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 6c 6f  ){.    /* AFP lo
d9fa0 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66  cking uses the f
d9fb0 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e  ile path so it n
d9fc0 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75  eeds to be inclu
d9fd0 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68  ded in.    ** th
d9fe0 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  e afpLockingCont
d9ff0 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ext..    */.    
da000 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
da010 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 4e 65  t *pCtx;.    pNe
da020 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  w->lockingContex
da030 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c 69 74  t = pCtx = sqlit
da040 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  e3_malloc( sizeo
da050 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 20 20  f(*pCtx) );.    
da060 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20  if( pCtx==0 ){. 
da070 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
da080 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73  _NOMEM;.    }els
da090 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a 20  e{.      /* NB: 
da0a0 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 73  zFilename exists
da0b0 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 6c   and remains val
da0c0 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 6c  id until the fil
da0d0 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 20  e is closed.    
da0e0 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74    ** according t
da0f0 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 31  o requirement F1
da100 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 20  1141.  So we do 
da110 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65  not need to make
da120 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79   a.      ** copy
da130 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65   of the filename
da140 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78 2d  . */.      pCtx-
da150 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65 6e  >dbPath = zFilen
da160 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 6e 64  ame;.      srand
da170 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 20 75  omdev();.      u
da180 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
da190 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e 64  .      rc = find
da1a0 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 4e  LockInfo(pNew, N
da1b0 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65  ULL, &pNew->pOpe
da1c0 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 4c 65  n);.      unixLe
da1d0 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20 20  aveMutex();     
da1e0 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65     .    }.  }.#e
da1f0 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 66 28  ndif..  else if(
da200 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d   pLockingStyle =
da210 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68  = &dotlockIoMeth
da220 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f  ods ){.    /* Do
da230 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73  tfile locking us
da240 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 68  es the file path
da250 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20   so it needs to 
da260 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20  be included in. 
da270 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f 63     ** the dotloc
da280 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  kLockingContext 
da290 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 72  .    */.    char
da2a0 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20   *zLockFile;.   
da2b0 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b 0a   int nFilename;.
da2c0 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 3d 20      nFilename = 
da2d0 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 69 6c  (int)strlen(zFil
da2e0 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 20 20  ename) + 6;.    
da2f0 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61  zLockFile = (cha
da300 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
da310 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  oc(nFilename);. 
da320 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 6c 65     if( zLockFile
da330 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
da340 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
da350 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
da360 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
da370 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a 4c 6f  f(nFilename, zLo
da380 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 4f 54  ckFile, "%s" DOT
da390 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a 46 69  LOCK_SUFFIX, zFi
da3a0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  lename);.    }. 
da3b0 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67     pNew->locking
da3c0 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 6b 46  Context = zLockF
da3d0 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 4f 53  ile;.  }..#if OS
da3e0 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 65 20  _VXWORKS.  else 
da3f0 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c  if( pLockingStyl
da400 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 68 6f  e == &semIoMetho
da410 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 61 6d  ds ){.    /* Nam
da420 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ed semaphore loc
da430 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69  king uses the fi
da440 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65  le path so it ne
da450 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a  eds to be.    **
da460 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65   included in the
da470 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   semLockingConte
da480 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 6e  xt.    */.    un
da490 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
da4a0 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63      rc = findLoc
da4b0 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65  kInfo(pNew, &pNe
da4c0 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d  w->pLock, &pNew-
da4d0 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 66 28  >pOpen);.    if(
da4e0 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29   (rc==SQLITE_OK)
da4f0 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 65 6e   && (pNew->pOpen
da500 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 29 7b  ->pSem==NULL) ){
da510 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53 65  .      char *zSe
da520 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e 70 4f  mName = pNew->pO
da530 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b 0a 20  pen->aSemName;. 
da540 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
da550 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
da560 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c  tf(MAX_PATHNAME,
da570 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 25 73 2e 73   zSemName, "%s.s
da580 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  em",.           
da590 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77              pNew
da5a0 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61  ->pId->zCanonica
da5b0 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 66 6f  lName);.      fo
da5c0 72 28 20 6e 3d 30 3b 20 7a 53 65 6d 4e 61 6d 65  r( n=0; zSemName
da5d0 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20 20 20  [n]; n++ ).     
da5e0 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d 65 5b     if( zSemName[
da5f0 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d 4e 61  n]=='/' ) zSemNa
da600 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 20 20  me[n] = '_';.   
da610 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e     pNew->pOpen->
da620 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 6e 28  pSem = sem_open(
da630 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 45 41  zSemName, O_CREA
da640 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20 20 20  T, 0666, 1);.   
da650 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 4f 70     if( pNew->pOp
da660 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 45 4d 5f  en->pSem == SEM_
da670 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 20 20 20  FAILED ){.      
da680 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
da690 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65  MEM;.        pNe
da6a0 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61  w->pOpen->aSemNa
da6b0 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  me[0] = '\0';.  
da6c0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
da6d0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
da6e0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a  ;.  }.#endif.  .
da6f0 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 72 72 6e    pNew->lastErrn
da700 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 5f 56 58  o = 0;.#if OS_VX
da710 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 21 3d  WORKS.  if( rc!=
da720 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
da730 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e 61 6d   unlink(zFilenam
da740 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65 74 65  e);.    isDelete
da750 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77   = 0;.  }.  pNew
da760 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 69 73 44  ->isDelete = isD
da770 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 0a 20 20  elete;.#endif.  
da780 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
da790 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 64 69 72  K ){.    if( dir
da7a0 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 64 69  fd>=0 ) close(di
da7b0 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 20  rfd); /* silent 
da7c0 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c  leak if fail, al
da7d0 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a  ready in error *
da7e0 2f 0a 20 20 20 20 63 6c 6f 73 65 28 68 29 3b 0a  /.    close(h);.
da7f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65    }else{.    pNe
da800 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f  w->pMethod = pLo
da810 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20  ckingStyle;.    
da820 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b  OpenCounter(+1);
da830 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
da840 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
da850 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  a file descripto
da860 72 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f  r to the directo
da870 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69  ry containing fi
da880 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a  le zFilename..**
da890 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
da8a0 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74  *pFd is set to t
da8b0 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64  he opened file d
da8c0 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a  escriptor and.**
da8d0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
da8e0 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72  turned. If an er
da8f0 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68  ror occurs, eith
da900 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a  er SQLITE_NOMEM.
da910 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e  ** or SQLITE_CAN
da920 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65  TOPEN is returne
da930 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65  d and *pFd is se
da940 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65  t to an undefine
da950 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  d.** value..**.*
da960 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69  * If SQLITE_OK i
da970 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20  s returned, the 
da980 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e  caller is respon
da990 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e  sible for closin
da9a0 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65  g.** the file de
da9b0 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73  scriptor *pFd us
da9c0 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a  ing close()..*/.
da9d0 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44  static int openD
da9e0 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63  irectory(const c
da9f0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
daa00 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74  int *pFd){.  int
daa10 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20   ii;.  int fd = 
daa20 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e  -1;.  char zDirn
daa30 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ame[MAX_PATHNAME
daa40 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  +1];..  sqlite3_
daa50 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
daa60 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c  HNAME, zDirname,
daa70 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65   "%s", zFilename
daa80 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69 6e 74  );.  for(ii=(int
daa90 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65  )strlen(zDirname
daaa0 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69 72 6e  ); ii>1 && zDirn
daab0 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69  ame[ii]!='/'; ii
daac0 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20  --);.  if( ii>0 
daad0 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b  ){.    zDirname[
daae0 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  ii] = '\0';.    
daaf0 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61  fd = open(zDirna
dab00 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42  me, O_RDONLY|O_B
dab10 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69  INARY, 0);.    i
dab20 66 28 20 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64  f( fd>=0 ){.#ifd
dab30 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20  ef FD_CLOEXEC.  
dab40 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f      fcntl(fd, F_
dab50 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c  SETFD, fcntl(fd,
dab60 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46   F_GETFD, 0) | F
dab70 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64  D_CLOEXEC);.#end
dab80 69 66 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  if.      OSTRACE
dab90 33 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20  3("OPENDIR %-3d 
daba0 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e  %s\n", fd, zDirn
dabb0 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ame);.    }.  }.
dabc0 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72    *pFd = fd;.  r
dabd0 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c  eturn (fd>=0?SQL
dabe0 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41  ITE_OK:SQLITE_CA
dabf0 4e 54 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  NTOPEN);.}../*.*
dac00 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f  * Create a tempo
dac10 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69  rary file name i
dac20 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75  n zBuf.  zBuf mu
dac30 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a  st be allocated.
dac40 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ** by the callin
dac50 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75  g process and mu
dac60 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68  st be big enough
dac70 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73   to hold at leas
dac80 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74  t.** pVfs->mxPat
dac90 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a  hname bytes..*/.
daca0 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 65  static int getTe
dacb0 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c  mpname(int nBuf,
dacc0 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20   char *zBuf){.  
dacd0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
dace0 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a  r *azDirs[] = {.
dacf0 20 20 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a       0,.     0,.
dad00 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c       "/var/tmp",
dad10 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22  .     "/usr/tmp"
dad20 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20  ,.     "/tmp",. 
dad30 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20      ".",.  };.  
dad40 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
dad50 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72  igned char zChar
dad60 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65  s[] =.    "abcde
dad70 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75  fghijklmnopqrstu
dad80 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44  vwxyz".    "ABCD
dad90 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54  EFGHIJKLMNOPQRST
dada0 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32  UVWXYZ".    "012
dadb0 33 34 35 36 37 38 39 22 3b 0a 20 20 75 6e 73 69  3456789";.  unsi
dadc0 67 6e 65 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  gned int i, j;. 
dadd0 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66   struct stat buf
dade0 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
dadf0 7a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f  zDir = ".";..  /
dae00 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69  * It's odd to si
dae10 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72  mulate an io-err
dae20 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61  or here, but rea
dae30 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74  lly this is just
dae40 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  .  ** using the 
dae50 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74  io-error infrast
dae60 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20  ructure to test 
dae70 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64  that SQLite hand
dae80 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75  les this.  ** fu
dae90 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20  nction failing. 
daea0 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65  .  */.  Simulate
daeb0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
daec0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a  SQLITE_IOERR );.
daed0 0a 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73  .  azDirs[0] = s
daee0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
daef0 63 74 6f 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c  ctory;.  if (NUL
daf00 4c 20 3d 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20  L == azDirs[1]) 
daf10 7b 0a 20 20 20 20 61 7a 44 69 72 73 5b 31 5d 20  {.    azDirs[1] 
daf20 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44 49 52  = getenv("TMPDIR
daf30 22 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72  ");.  }.  .  for
daf40 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61  (i=0; i<sizeof(a
daf50 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a  zDirs)/sizeof(az
daf60 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  Dirs[0]); i++){.
daf70 20 20 20 20 69 66 28 20 61 7a 44 69 72 73 5b 69      if( azDirs[i
daf80 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ]==0 ) continue;
daf90 0a 20 20 20 20 69 66 28 20 73 74 61 74 28 61 7a  .    if( stat(az
dafa0 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29 20 29  Dirs[i], &buf) )
dafb0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
dafc0 66 28 20 21 53 5f 49 53 44 49 52 28 62 75 66 2e  f( !S_ISDIR(buf.
dafd0 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69  st_mode) ) conti
dafe0 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61 63 63  nue;.    if( acc
daff0 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30  ess(azDirs[i], 0
db000 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  7) ) continue;. 
db010 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 73     zDir = azDirs
db020 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  [i];.    break;.
db030 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
db040 74 68 61 74 20 74 68 65 20 6f 75 74 70 75 74 20  that the output 
db050 62 75 66 66 65 72 20 69 73 20 6c 61 72 67 65 20  buffer is large 
db060 65 6e 6f 75 67 68 20 66 6f 72 20 74 68 65 20 74  enough for the t
db070 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 0a 20  emporary file . 
db080 20 2a 2a 20 6e 61 6d 65 2e 20 49 66 20 69 74 20   ** name. If it 
db090 69 73 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53  is not, return S
db0a0 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a  QLITE_ERROR..  *
db0b0 2f 0a 20 20 69 66 28 20 28 73 74 72 6c 65 6e 28  /.  if( (strlen(
db0c0 7a 44 69 72 29 20 2b 20 73 74 72 6c 65 6e 28 53  zDir) + strlen(S
db0d0 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f  QLITE_TEMP_FILE_
db0e0 50 52 45 46 49 58 29 20 2b 20 31 37 29 20 3e 3d  PREFIX) + 17) >=
db0f0 20 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b   (size_t)nBuf ){
db100 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
db110 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
db120 20 64 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33   do{.    sqlite3
db130 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31  _snprintf(nBuf-1
db140 37 2c 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51  7, zBuf, "%s/"SQ
db150 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
db160 52 45 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20  REFIX, zDir);.  
db170 20 20 6a 20 3d 20 28 69 6e 74 29 73 74 72 6c 65    j = (int)strle
db180 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c  n(zBuf);.    sql
db190 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
db1a0 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20  15, &zBuf[j]);. 
db1b0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35     for(i=0; i<15
db1c0 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20  ; i++, j++){.   
db1d0 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68     zBuf[j] = (ch
db1e0 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73  ar)zChars[ ((uns
db1f0 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b  igned char)zBuf[
db200 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61  j])%(sizeof(zCha
db210 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a  rs)-1) ];.    }.
db220 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b      zBuf[j] = 0;
db230 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73  .  }while( acces
db240 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a  s(zBuf,0)==0 );.
db250 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
db260 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54  OK;.}..#if SQLIT
db270 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
db280 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
db290 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a  d(__APPLE__)./*.
db2a0 2a 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72  ** Routine to tr
db2b0 61 6e 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69  ansform a unixFi
db2c0 6c 65 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d  le into a proxy-
db2d0 6c 6f 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65  locking unixFile
db2e0 2e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  ..** Implementat
db2f0 69 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79  ion in the proxy
db300 2d 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20  -lock division, 
db310 62 75 74 20 75 73 65 64 20 62 79 20 75 6e 69 78  but used by unix
db320 4f 70 65 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c  Open().** if SQL
db330 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59  ITE_PREFER_PROXY
db340 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69  _LOCKING is defi
db350 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
db360 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72  nt proxyTransfor
db370 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69  mUnixFile(unixFi
db380 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  le*, const char*
db390 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  );.#endif.../*.*
db3a0 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  * Open the file 
db3b0 7a 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72  zPath..** .** Pr
db3c0 65 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 53 51  eviously, the SQ
db3d0 4c 69 74 65 20 4f 53 20 6c 61 79 65 72 20 75 73  Lite OS layer us
db3e0 65 64 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f  ed three functio
db3f0 6e 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74  ns in place of t
db400 68 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a  his.** one:.**.*
db410 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f  *     sqlite3OsO
db420 70 65 6e 52 65 61 64 57 72 69 74 65 28 29 3b 0a  penReadWrite();.
db430 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
db440 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a  OpenReadOnly();.
db450 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
db460 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 3b  OpenExclusive();
db470 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c  .**.** These cal
db480 6c 73 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f  ls correspond to
db490 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
db4a0 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66  ombinations of f
db4b0 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lags:.**.**     
db4c0 52 65 61 64 57 72 69 74 65 28 29 20 2d 3e 20 20  ReadWrite() ->  
db4d0 20 20 20 28 52 45 41 44 57 52 49 54 45 20 7c 20     (READWRITE | 
db4e0 43 52 45 41 54 45 29 0a 2a 2a 20 20 20 20 20 52  CREATE).**     R
db4f0 65 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20  eadOnly()  ->   
db500 20 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a    (READONLY) .**
db510 20 20 20 20 20 4f 70 65 6e 45 78 63 6c 75 73 69       OpenExclusi
db520 76 65 28 29 20 2d 3e 20 28 52 45 41 44 57 52 49  ve() -> (READWRI
db530 54 45 20 7c 20 43 52 45 41 54 45 20 7c 20 45 58  TE | CREATE | EX
db540 43 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54  CLUSIVE).**.** T
db550 68 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75  he old OpenExclu
db560 73 69 76 65 28 29 20 61 63 63 65 70 74 65 64 20  sive() accepted 
db570 61 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65  a boolean argume
db580 6e 74 20 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20  nt - "delFlag". 
db590 49 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 20  If.** true, the 
db5a0 66 69 6c 65 20 77 61 73 20 63 6f 6e 66 69 67 75  file was configu
db5b0 72 65 64 20 74 6f 20 62 65 20 61 75 74 6f 6d 61  red to be automa
db5c0 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20  tically deleted 
db5d0 77 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65  when the.** file
db5e0 20 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20   handle closed. 
db5f0 54 6f 20 61 63 68 69 65 76 65 20 74 68 65 20 73  To achieve the s
db600 61 6d 65 20 65 66 66 65 63 74 20 75 73 69 6e 67  ame effect using
db610 20 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e   this new .** in
db620 74 65 72 66 61 63 65 2c 20 61 64 64 20 74 68 65  terface, add the
db630 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66   DELETEONCLOSE f
db640 6c 61 67 20 74 6f 20 74 68 6f 73 65 20 73 70 65  lag to those spe
db650 63 69 66 69 65 64 20 61 62 6f 76 65 20 66 6f 72  cified above for
db660 20 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69   .** OpenExclusi
db670 76 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ve()..*/.static 
db680 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20  int unixOpen(.  
db690 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
db6a0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
db6b0 54 68 65 20 56 46 53 20 66 6f 72 20 77 68 69 63  The VFS for whic
db6c0 68 20 74 68 69 73 20 69 73 20 74 68 65 20 78 4f  h this is the xO
db6d0 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  pen method */.  
db6e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
db6f0 68 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  h,           /* 
db700 50 61 74 68 6e 61 6d 65 20 6f 66 20 66 69 6c 65  Pathname of file
db710 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f   to be opened */
db720 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
db730 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20  *pFile,         
db740 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63  /* The file desc
db750 72 69 70 74 6f 72 20 74 6f 20 62 65 20 66 69 6c  riptor to be fil
db760 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  led in */.  int 
db770 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
db780 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
db790 74 20 66 6c 61 67 73 20 74 6f 20 63 6f 6e 74 72  t flags to contr
db7a0 6f 6c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 2a  ol the opening *
db7b0 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61  /.  int *pOutFla
db7c0 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  gs              
db7d0 20 2f 2a 20 4f 75 74 70 75 74 20 66 6c 61 67 73   /* Output flags
db7e0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 51 4c   returned to SQL
db7f0 69 74 65 20 63 6f 72 65 20 2a 2f 0a 29 7b 0a 20  ite core */.){. 
db800 20 69 6e 74 20 66 64 20 3d 20 30 3b 20 20 20 20   int fd = 0;    
db810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
db820 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74  /* File descript
db830 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 6f  or returned by o
db840 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 64  pen() */.  int d
db850 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20  irfd = -1;      
db860 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72            /* Dir
db870 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63  ectory file desc
db880 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  riptor */.  int 
db890 6f 70 65 6e 46 6c 61 67 73 20 3d 20 30 3b 20 20  openFlags = 0;  
db8a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
db8b0 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f 20 6f  ags to pass to o
db8c0 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65  pen() */.  int e
db8d0 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46  Type = flags&0xF
db8e0 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70  FFFFF00;  /* Typ
db8f0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65  e of file to ope
db900 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63  n */.  int noLoc
db910 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
db920 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
db930 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 70 72   omit locking pr
db940 69 6d 69 74 69 76 65 73 20 2a 2f 0a 20 20 69 6e  imitives */.  in
db950 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
db960 3b 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 6c 75  ;..  int isExclu
db970 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 20 26  sive  = (flags &
db980 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
db990 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 20 69  LUSIVE);.  int i
db9a0 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 28 66  sDelete     = (f
db9b0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
db9c0 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
db9d0 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74  );.  int isCreat
db9e0 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26  e     = (flags &
db9f0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
dba00 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 52 65  ATE);.  int isRe
dba10 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c 61 67  adonly   = (flag
dba20 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
dba30 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74  READONLY);.  int
dba40 20 69 73 52 65 61 64 57 72 69 74 65 20 20 3d 20   isReadWrite  = 
dba50 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
dba60 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 3b  OPEN_READWRITE);
dba70 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69  ..  /* If creati
dba80 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20 6d  ng a master or m
dba90 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61 6c  ain-file journal
dbaa0 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
dbab0 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a 20 61  will open.  ** a
dbac0 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72   file-descriptor
dbad0 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72   on the director
dbae0 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73 74  y too. The first
dbaf0 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28 29   time unixSync()
dbb00 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  .  ** is called 
dbb10 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 66 69  the directory fi
dbb20 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 69  le descriptor wi
dbb30 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64 20  ll be fsync()ed 
dbb40 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a 20 20  and close()d..  
dbb50 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e 44  */.  int isOpenD
dbb60 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 43 72  irectory = (isCr
dbb70 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 20 28  eate && .      (
dbb80 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
dbb90 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
dbba0 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49  L || eType==SQLI
dbbb0 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
dbbc0 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 2f 2a  RNAL).  );..  /*
dbbd0 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 50 61   If argument zPa
dbbe0 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  th is a NULL poi
dbbf0 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 74  nter, this funct
dbc00 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 20  ion is required 
dbc10 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 74  to open.  ** a t
dbc20 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 55  emporary file. U
dbc30 73 65 20 74 68 69 73 20 62 75 66 66 65 72 20 74  se this buffer t
dbc40 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65  o store the file
dbc50 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f 0a 20   name in..  */. 
dbc60 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d   char zTmpname[M
dbc70 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 0a  AX_PATHNAME+1];.
dbc80 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
dbc90 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a 20 20  ame = zPath;..  
dbca0 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 6f 6c  /* Check the fol
dbcb0 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
dbcc0 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 20 2a  s are true: .  *
dbcd0 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 78 61  *.  **   (a) Exa
dbce0 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 20  ctly one of the 
dbcf0 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 52 45  READWRITE and RE
dbd00 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d 75 73  ADONLY flags mus
dbd10 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 0a 20  t be set, and . 
dbd20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 52 45   **   (b) if CRE
dbd30 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e  ATE is set, then
dbd40 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 74 20   READWRITE must 
dbd50 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 6e 64  also be set, and
dbd60 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 20 45  .  **   (c) if E
dbd70 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 74 2c  XCLUSIVE is set,
dbd80 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 73   then CREATE mus
dbd90 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20  t also be set.. 
dbda0 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 45 4c   **   (d) if DEL
dbdb0 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 73 65  ETEONCLOSE is se
dbdc0 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d  t, then CREATE m
dbdd0 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e  ust also be set.
dbde0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 28  .  */.  assert((
dbdf0 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c 7c  isReadonly==0 ||
dbe00 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d 30 29   isReadWrite==0)
dbe10 20 26 26 20 28 69 73 52 65 61 64 57 72 69 74 65   && (isReadWrite
dbe20 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 29 29   || isReadonly))
dbe30 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 72 65  ;.  assert(isCre
dbe40 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64  ate==0 || isRead
dbe50 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 72 74  Write);.  assert
dbe60 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30 20  (isExclusive==0 
dbe70 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 20 20  || isCreate);.  
dbe80 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 3d  assert(isDelete=
dbe90 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b  =0 || isCreate);
dbea0 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20  ..  /* The main 
dbeb0 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  DB, main journal
dbec0 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a 6f 75  , and master jou
dbed0 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 20 61  rnal are never a
dbee0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 20 2a  utomatically.  *
dbef0 2a 20 64 65 6c 65 74 65 64 0a 20 20 2a 2f 0a 20  * deleted.  */. 
dbf00 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d   assert( eType!=
dbf10 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
dbf20 5f 44 42 20 7c 7c 20 21 69 73 44 65 6c 65 74 65  _DB || !isDelete
dbf30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 54   );.  assert( eT
dbf40 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype!=SQLITE_OPEN
dbf50 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c  _MAIN_JOURNAL ||
dbf60 20 21 69 73 44 65 6c 65 74 65 20 29 3b 0a 20 20   !isDelete );.  
dbf70 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53  assert( eType!=S
dbf80 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45  QLITE_OPEN_MASTE
dbf90 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21 69 73  R_JOURNAL || !is
dbfa0 44 65 6c 65 74 65 20 29 3b 0a 0a 20 20 2f 2a 20  Delete );..  /* 
dbfb0 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20  Assert that the 
dbfc0 75 70 70 65 72 20 6c 61 79 65 72 20 68 61 73 20  upper layer has 
dbfd0 73 65 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 22  set one of the "
dbfe0 66 69 6c 65 2d 74 79 70 65 22 20 66 6c 61 67 73  file-type" flags
dbff0 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 65  . */.  assert( e
dc000 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
dc010 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 20 7c  N_MAIN_DB      |
dc020 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
dc030 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 0a 20 20  OPEN_TEMP_DB .  
dc040 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53       || eType==S
dc050 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
dc060 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65  JOURNAL || eType
dc070 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45  ==SQLITE_OPEN_TE
dc080 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20  MP_JOURNAL .    
dc090 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c     || eType==SQL
dc0a0 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52  ITE_OPEN_SUBJOUR
dc0b0 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d  NAL   || eType==
dc0c0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
dc0d0 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20  ER_JOURNAL .    
dc0e0 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c     || eType==SQL
dc0f0 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45  ITE_OPEN_TRANSIE
dc100 4e 54 5f 44 42 0a 20 20 29 3b 0a 0a 20 20 6d 65  NT_DB.  );..  me
dc110 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73  mset(pFile, 0, s
dc120 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29  izeof(unixFile))
dc130 3b 0a 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  ;..  if( !zName 
dc140 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 69 73  ){.    assert(is
dc150 44 65 6c 65 74 65 20 26 26 20 21 69 73 4f 70 65  Delete && !isOpe
dc160 6e 44 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20  nDirectory);.   
dc170 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d   rc = getTempnam
dc180 65 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31  e(MAX_PATHNAME+1
dc190 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20  , zTmpname);.   
dc1a0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
dc1b0 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
dc1c0 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
dc1d0 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d   zName = zTmpnam
dc1e0 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73  e;.  }..  if( is
dc1f0 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f 70 65 6e  Readonly )  open
dc200 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c  Flags |= O_RDONL
dc210 59 3b 0a 20 20 69 66 28 20 69 73 52 65 61 64 57  Y;.  if( isReadW
dc220 72 69 74 65 20 29 20 6f 70 65 6e 46 6c 61 67 73  rite ) openFlags
dc230 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20 69 66   |= O_RDWR;.  if
dc240 28 20 69 73 43 72 65 61 74 65 20 29 20 20 20 20  ( isCreate )    
dc250 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 43  openFlags |= O_C
dc260 52 45 41 54 3b 0a 20 20 69 66 28 20 69 73 45 78  REAT;.  if( isEx
dc270 63 6c 75 73 69 76 65 20 29 20 6f 70 65 6e 46 6c  clusive ) openFl
dc280 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c 7c 4f  ags |= (O_EXCL|O
dc290 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 20 6f 70  _NOFOLLOW);.  op
dc2a0 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 4c 41  enFlags |= (O_LA
dc2b0 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41 52 59  RGEFILE|O_BINARY
dc2c0 29 3b 0a 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28  );..  fd = open(
dc2d0 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73  zName, openFlags
dc2e0 2c 20 69 73 44 65 6c 65 74 65 3f 30 36 30 30 3a  , isDelete?0600:
dc2f0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
dc300 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29  ILE_PERMISSIONS)
dc310 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4f 50  ;.  OSTRACE4("OP
dc320 45 4e 58 20 20 20 25 2d 33 64 20 25 73 20 30 25  ENX   %-3d %s 0%
dc330 6f 5c 6e 22 2c 20 66 64 2c 20 7a 4e 61 6d 65 2c  o\n", fd, zName,
dc340 20 6f 70 65 6e 46 6c 61 67 73 29 3b 0a 20 20 69   openFlags);.  i
dc350 66 28 20 66 64 3c 30 20 26 26 20 65 72 72 6e 6f  f( fd<0 && errno
dc360 21 3d 45 49 53 44 49 52 20 26 26 20 69 73 52 65  !=EISDIR && isRe
dc370 61 64 57 72 69 74 65 20 26 26 20 21 69 73 45 78  adWrite && !isEx
dc380 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 2f  clusive ){.    /
dc390 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e  * Failed to open
dc3a0 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 65   the file for re
dc3b0 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e  ad/write access.
dc3c0 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20   Try read-only. 
dc3d0 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20 26 3d 20  */.    flags &= 
dc3e0 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  ~(SQLITE_OPEN_RE
dc3f0 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f  ADWRITE|SQLITE_O
dc400 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 20  PEN_CREATE);.   
dc410 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45   flags |= SQLITE
dc420 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a  _OPEN_READONLY;.
dc430 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78 4f      return unixO
dc440 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c  pen(pVfs, zPath,
dc450 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70   pFile, flags, p
dc460 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 7d 0a 20  OutFlags);.  }. 
dc470 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20   if( fd<0 ){.   
dc480 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
dc490 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 69  ANTOPEN;.  }.  i
dc4a0 66 28 20 69 73 44 65 6c 65 74 65 20 29 7b 0a 23  f( isDelete ){.#
dc4b0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
dc4c0 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61 6d 65 3b    zPath = zName;
dc4d0 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e 6c 69 6e  .#else.    unlin
dc4e0 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e 64 69 66  k(zName);.#endif
dc4f0 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f  .  }.#if SQLITE_
dc500 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
dc510 54 59 4c 45 0a 20 20 65 6c 73 65 7b 0a 20 20 20  TYLE.  else{.   
dc520 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69   ((unixFile*)pFi
dc530 6c 65 29 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d  le)->openFlags =
dc540 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 20 7d 0a   openFlags;.  }.
dc550 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4f 75  #endif.  if( pOu
dc560 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70  tFlags ){.    *p
dc570 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73  OutFlags = flags
dc580 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 4e  ;.  }..#ifndef N
dc590 44 45 42 55 47 0a 20 20 69 66 28 20 28 66 6c 61  DEBUG.  if( (fla
dc5a0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
dc5b0 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20 29 7b 0a  _MAIN_DB)!=0 ){.
dc5c0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
dc5d0 70 46 69 6c 65 29 2d 3e 69 73 4c 6f 63 6b 61 62  pFile)->isLockab
dc5e0 6c 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64  le = 1;.  }.#end
dc5f0 69 66 0a 0a 20 20 61 73 73 65 72 74 28 66 64 21  if..  assert(fd!
dc600 3d 30 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65  =0);.  if( isOpe
dc610 6e 44 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20  nDirectory ){.  
dc620 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63    rc = openDirec
dc630 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72  tory(zPath, &dir
dc640 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  fd);.    if( rc!
dc650 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
dc660 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f      close(fd); /
dc670 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20  * silently leak 
dc680 69 66 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 79  if fail, already
dc690 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20   in error */.   
dc6a0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
dc6b0 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20    }.  }..#ifdef 
dc6c0 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e  FD_CLOEXEC.  fcn
dc6d0 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20  tl(fd, F_SETFD, 
dc6e0 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46  fcntl(fd, F_GETF
dc6f0 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58  D, 0) | FD_CLOEX
dc700 45 43 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e  EC);.#endif..  n
dc710 6f 4c 6f 63 6b 20 3d 20 65 54 79 70 65 21 3d 53  oLock = eType!=S
dc720 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
dc730 44 42 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  DB;..#if SQLITE_
dc740 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43  PREFER_PROXY_LOC
dc750 4b 49 4e 47 0a 20 20 69 66 28 20 7a 50 61 74 68  KING.  if( zPath
dc760 21 3d 4e 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63  !=NULL && !noLoc
dc770 6b 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 65  k ){.    char *e
dc780 6e 76 66 6f 72 63 65 20 3d 20 67 65 74 65 6e 76  nvforce = getenv
dc790 28 22 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50  ("SQLITE_FORCE_P
dc7a0 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a  ROXY_LOCKING");.
dc7b0 20 20 20 20 69 6e 74 20 75 73 65 50 72 6f 78 79      int useProxy
dc7c0 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51   = 0;..    /* SQ
dc7d0 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59  LITE_FORCE_PROXY
dc7e0 5f 4c 4f 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e  _LOCKING==1 mean
dc7f0 73 20 66 6f 72 63 65 20 61 6c 77 61 79 73 20 75  s force always u
dc800 73 65 20 70 72 6f 78 79 2c 20 0a 20 20 20 20 2a  se proxy, .    *
dc810 2a 20 30 20 6d 65 61 6e 73 20 6e 65 76 65 72 20  * 0 means never 
dc820 75 73 65 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20  use proxy, NULL 
dc830 6d 65 61 6e 73 20 75 73 65 20 70 72 6f 78 79 20  means use proxy 
dc840 66 6f 72 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69  for non-local fi
dc850 6c 65 73 20 6f 6e 6c 79 0a 20 20 20 20 2a 2f 0a  les only.    */.
dc860 20 20 20 20 69 66 28 20 65 6e 76 66 6f 72 63 65      if( envforce
dc870 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  !=NULL ){.      
dc880 75 73 65 50 72 6f 78 79 20 3d 20 61 74 6f 69 28  useProxy = atoi(
dc890 65 6e 76 66 6f 72 63 65 29 3e 30 3b 0a 20 20 20  envforce)>0;.   
dc8a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74   }else{.      st
dc8b0 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49 6e  ruct statfs fsIn
dc8c0 66 6f 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 73  fo;..      if( s
dc8d0 74 61 74 66 73 28 7a 50 61 74 68 2c 20 26 66 73  tatfs(zPath, &fs
dc8e0 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 7b 0a 09  Info) == -1 ){..
dc8f0 09 09 09 28 28 75 6e 69 78 46 69 6c 65 2a 29 70  ...((unixFile*)p
dc900 46 69 6c 65 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  File)->lastErrno
dc910 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
dc920 20 20 69 66 28 20 64 69 72 66 64 3e 3d 30 20 29    if( dirfd>=0 )
dc930 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f   close(dirfd); /
dc940 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20  * silently leak 
dc950 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f  if fail, in erro
dc960 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6c 6f  r */.        clo
dc970 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e  se(fd); /* silen
dc980 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c  tly leak if fail
dc990 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20  , in error */.  
dc9a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
dc9b0 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53  ITE_IOERR_ACCESS
dc9c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
dc9d0 75 73 65 50 72 6f 78 79 20 3d 20 21 28 66 73 49  useProxy = !(fsI
dc9e0 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f  nfo.f_flags&MNT_
dc9f0 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20  LOCAL);.    }.  
dca00 20 20 69 66 28 20 75 73 65 50 72 6f 78 79 20 29    if( useProxy )
dca10 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6c  {.      rc = fil
dca20 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73  lInUnixFile(pVfs
dca30 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69  , fd, dirfd, pFi
dca40 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63  le, zPath, noLoc
dca50 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a 20 20  k, isDelete);.  
dca60 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
dca70 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
dca80 20 72 63 20 3d 20 70 72 6f 78 79 54 72 61 6e 73   rc = proxyTrans
dca90 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 28 75 6e  formUnixFile((un
dcaa0 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 2c 20 22  ixFile*)pFile, "
dcab0 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 20 20 20 20  :auto:");.      
dcac0 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  }.      return r
dcad0 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  c;.    }.  }.#en
dcae0 64 69 66 0a 20 20 0a 20 20 72 65 74 75 72 6e 20  dif.  .  return 
dcaf0 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70  fillInUnixFile(p
dcb00 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20  Vfs, fd, dirfd, 
dcb10 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f  pFile, zPath, no
dcb20 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b  Lock, isDelete);
dcb30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
dcb40 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61   the file at zPa
dcb50 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 79  th. If the dirSy
dcb60 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nc argument is t
dcb70 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a 20  rue, fsync().** 
dcb80 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 66  the directory af
dcb90 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65  ter deleting the
dcba0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
dcbb0 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 28   int unixDelete(
dcbc0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
dcbd0 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a 20  NotUsed,     /* 
dcbe0 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  VFS containing t
dcbf0 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c 65  his as the xDele
dcc00 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63  te method */.  c
dcc10 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
dcc20 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
dcc30 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 64   of file to be d
dcc40 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  eleted */.  int 
dcc50 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20 20  dirSync         
dcc60 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
dcc70 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63 74  , fsync() direct
dcc80 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69  ory after deleti
dcc90 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20  ng file */.){.  
dcca0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
dccb0 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  OK;.  UNUSED_PAR
dccc0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
dccd0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
dcce0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
dccf0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a  _IOERR_DELETE);.
dcd00 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b    unlink(zPath);
dcd10 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
dcd20 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a  DISABLE_DIRSYNC.
dcd30 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b    if( dirSync ){
dcd40 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20  .    int fd;.   
dcd50 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74   rc = openDirect
dcd60 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b  ory(zPath, &fd);
dcd70 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
dcd80 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f 53  ITE_OK ){.#if OS
dcd90 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69  _VXWORKS.      i
dcda0 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31  f( fsync(fd)==-1
dcdb0 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69   ).#else.      i
dcdc0 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23  f( fsync(fd) ).#
dcdd0 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20  endif.      {.  
dcde0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
dcdf0 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e  E_IOERR_DIR_FSYN
dce00 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  C;.      }.     
dce10 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26 26   if( close(fd)&&
dce20 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72  !rc ){.        r
dce30 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
dce40 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20  _DIR_CLOSE;.    
dce50 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65    }.    }.  }.#e
dce60 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63  ndif.  return rc
dce70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  ;.}../*.** Test 
dce80 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66  the existance of
dce90 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69   or access permi
dcea0 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a  ssions of file z
dceb0 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73  Path. The.** tes
dcec0 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65  t performed depe
dced0 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  nds on the value
dcee0 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a   of flags:.**.**
dcef0 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45       SQLITE_ACCE
dcf00 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72  SS_EXISTS: Retur
dcf10 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20  n 1 if the file 
dcf20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51  exists.**     SQ
dcf30 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
dcf40 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20  WRITE: Return 1 
dcf50 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72  if the file is r
dcf60 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65  ead and writable
dcf70 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f  ..**     SQLITE_
dcf80 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a  ACCESS_READONLY:
dcf90 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
dcfa0 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c   file is readabl
dcfb0 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  e..**.** Otherwi
dcfc0 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  se return 0..*/.
dcfd0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41  static int unixA
dcfe0 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33  ccess(.  sqlite3
dcff0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20  _vfs *NotUsed,  
dd000 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e 74   /* The VFS cont
dd010 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63 63  aining this xAcc
dd020 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ess method */.  
dd030 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
dd040 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20  h,      /* Path 
dd050 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 65  of the file to e
dd060 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  xamine */.  int 
dd070 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
dd080 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 77      /* What do w
dd090 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e 20  e want to learn 
dd0a0 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68 20  about the zPath 
dd0b0 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 2a  file? */.  int *
dd0c0 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20  pResOut         
dd0d0 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75     /* Write resu
dd0e0 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 20  lt boolean here 
dd0f0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64  */.){.  int amod
dd100 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f  e = 0;.  UNUSED_
dd110 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
dd120 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  d);.  SimulateIO
dd130 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
dd140 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
dd150 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20  S; );.  switch( 
dd160 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73  flags ){.    cas
dd170 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
dd180 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d  EXISTS:.      am
dd190 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20  ode = F_OK;.    
dd1a0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
dd1b0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
dd1c0 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20  READWRITE:.     
dd1d0 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f   amode = W_OK|R_
dd1e0 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OK;.      break;
dd1f0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
dd200 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20  _ACCESS_READ:.  
dd210 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b      amode = R_OK
dd220 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  ;.      break;..
dd230 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
dd240 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61     assert(!"Inva
dd250 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65  lid flags argume
dd260 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65  nt");.  }.  *pRe
dd270 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a  sOut = (access(z
dd280 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29  Path, amode)==0)
dd290 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
dd2a0 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
dd2b0 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20  Turn a relative 
dd2c0 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20  pathname into a 
dd2d0 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54  full pathname. T
dd2e0 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68  he relative path
dd2f0 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73  .** is stored as
dd300 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65   a nul-terminate
dd310 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20  d string in the 
dd320 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
dd330 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a  o by.** zPath. .
dd340 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74  **.** zOut point
dd350 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66  s to a buffer of
dd360 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65   at least sqlite
dd370 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65  3_vfs.mxPathname
dd380 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74   bytes .** (in t
dd390 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41  his case, MAX_PA
dd3a0 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54  THNAME bytes). T
dd3b0 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20  he full-path is 
dd3c0 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68  written to.** th
dd3d0 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 65  is buffer before
dd3e0 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73   returning..*/.s
dd3f0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75  tatic int unixFu
dd400 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71  llPathname(.  sq
dd410 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
dd420 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
dd430 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62  ointer to vfs ob
dd440 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ject */.  const 
dd450 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20  char *zPath,    
dd460 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69          /* Possi
dd470 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70  bly relative inp
dd480 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74  ut path */.  int
dd490 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20   nOut,          
dd4a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
dd4b0 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
dd4c0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
dd4d0 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20    char *zOut    
dd4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd4f0 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72  /* Output buffer
dd500 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27   */.){..  /* It'
dd510 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74  s odd to simulat
dd520 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65  e an io-error he
dd530 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74  re, but really t
dd540 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a  his is just.  **
dd550 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72   using the io-er
dd560 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75  ror infrastructu
dd570 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  re to test that 
dd580 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74  SQLite handles t
dd590 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
dd5a0 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20  n failing. This 
dd5b0 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66  function could f
dd5c0 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d  ail if, for exam
dd5d0 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75  ple, the.  ** cu
dd5e0 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69  rrent working di
dd5f0 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e  rectory has been
dd600 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a   unlinked..  */.
dd610 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
dd620 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
dd630 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73  _ERROR );..  ass
dd640 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74  ert( pVfs->mxPat
dd650 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e  hname==MAX_PATHN
dd660 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  AME );.  UNUSED_
dd670 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
dd680 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d  ..  zOut[nOut-1]
dd690 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 7a   = '\0';.  if( z
dd6a0 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b 0a  Path[0]=='/' ){.
dd6b0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
dd6c0 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 2c  intf(nOut, zOut,
dd6d0 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20   "%s", zPath);. 
dd6e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
dd6f0 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67 65  nCwd;.    if( ge
dd700 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d  tcwd(zOut, nOut-
dd710 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  1)==0 ){.      r
dd720 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e  eturn SQLITE_CAN
dd730 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20  TOPEN;.    }.   
dd740 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74 72   nCwd = (int)str
dd750 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73  len(zOut);.    s
dd760 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
dd770 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74  nOut-nCwd, &zOut
dd780 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a  [nCwd], "/%s", z
dd790 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Path);.  }.  ret
dd7a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
dd7b0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
dd7c0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
dd7d0 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  NSION./*.** Inte
dd7e0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
dd7f0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
dd800 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
dd810 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
dd820 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
dd830 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
dd840 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
dd850 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c  ibrary..*/.#incl
dd860 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 74  ude <dlfcn.h>.st
dd870 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 44  atic void *unixD
dd880 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
dd890 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e 73  s *NotUsed, cons
dd8a0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
dd8b0 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e){.  UNUSED_PAR
dd8c0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
dd8d0 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 6e  .  return dlopen
dd8e0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c 44  (zFilename, RTLD
dd8f0 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f 42  _NOW | RTLD_GLOB
dd900 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51  AL);.}../*.** SQ
dd910 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20  Lite calls this 
dd920 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 61  function immedia
dd930 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61 6c  tely after a cal
dd940 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 29  l to unixDlSym()
dd950 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 65   or.** unixDlOpe
dd960 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75 72  n() fails (retur
dd970 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65  ns a null pointe
dd980 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 65  r). If a more de
dd990 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a 20  tailed error.** 
dd9a0 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 6c  message is avail
dd9b0 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69 74  able, it is writ
dd9c0 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e 20  ten to zBufOut. 
dd9d0 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73  If no error mess
dd9e0 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61  age.** is availa
dd9f0 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 20  ble, zBufOut is 
dda00 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 20  left unmodified 
dda10 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73 20  and SQLite uses 
dda20 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 72  a default.** err
dda30 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73  or message..*/.s
dda40 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44  tatic void unixD
dda50 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76  lError(sqlite3_v
dda60 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74  fs *NotUsed, int
dda70 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
dda80 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a  fOut){.  char *z
dda90 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  Err;.  UNUSED_PA
ddaa0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
ddab0 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  ;.  unixEnterMut
ddac0 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64  ex();.  zErr = d
ddad0 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20  lerror();.  if( 
ddae0 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  zErr ){.    sqli
ddaf0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
ddb00 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22  f, zBufOut, "%s"
ddb10 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75  , zErr);.  }.  u
ddb20 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
ddb30 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28  .}.static void (
ddb40 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74  *unixDlSym(sqlit
ddb50 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
ddb60 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20   void *p, const 
ddb70 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64  char*zSym))(void
ddb80 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43  ){.  /* .  ** GC
ddb90 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63  C with -pedantic
ddba0 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61  -errors says tha
ddbb0 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61  t C90 does not a
ddbc0 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20  llow a void* to 
ddbd0 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74  be.  ** cast int
ddbe0 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  o a pointer to a
ddbf0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20   function.  And 
ddc00 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  yet the library 
ddc10 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a  dlsym() routine.
ddc20 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76    ** returns a v
ddc30 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65  oid* which is re
ddc40 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74  ally a pointer t
ddc50 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53  o a function.  S
ddc60 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a  o how do we.  **
ddc70 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74   use dlsym() wit
ddc80 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f  h -pedantic-erro
ddc90 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61  rs?.  **.  ** Va
ddca0 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69  riable x below i
ddcb0 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20  s defined to be 
ddcc0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
ddcd0 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20  unction taking. 
ddce0 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76   ** parameters v
ddcf0 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63  oid* and const c
ddd00 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69  har* and returni
ddd10 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
ddd20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a  a function..  **
ddd30 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78   We initialize x
ddd40 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74   by assigning it
ddd50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
ddd60 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69  e dlsym() functi
ddd70 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61  on..  ** (That a
ddd80 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72  ssignment requir
ddd90 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65  es a cast.)  The
ddda0 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75  n we call the fu
dddb0 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a  nction that.  **
dddc0 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a   x points to.  .
dddd0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77    **.  ** This w
ddde0 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e  ork-around is un
dddf0 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63  likely to work c
dde00 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20  orrectly on any 
dde10 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a  system where.  *
dde20 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e  * you really can
dde30 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74  not cast a funct
dde40 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f  ion pointer into
dde50 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65   void*.  But the
dde60 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f  n, on the.  ** o
dde70 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d  ther hand, dlsym
dde80 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b  () will not work
dde90 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65   on such a syste
ddea0 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20  m either, so we 
ddeb0 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65  have.  ** not re
ddec0 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69  ally lost anythi
dded0 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20  ng..  */.  void 
ddee0 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e  (*(*x)(void*,con
ddef0 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29  st char*))(void)
ddf00 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
ddf10 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
ddf20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28   x = (void(*(*)(
ddf30 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
ddf40 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b  *))(void))dlsym;
ddf50 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70  .  return (*x)(p
ddf60 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69  , zSym);.}.stati
ddf70 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f  c void unixDlClo
ddf80 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  se(sqlite3_vfs *
ddf90 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70  NotUsed, void *p
ddfa0 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45  Handle){.  UNUSE
ddfb0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
ddfc0 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28  sed);.  dlclose(
ddfd0 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73  pHandle);.}.#els
ddfe0 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f  e /* if SQLITE_O
ddff0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
de000 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a  ON is defined: *
de010 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78  /.  #define unix
de020 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66  DlOpen  0.  #def
de030 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20  ine unixDlError 
de040 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78  0.  #define unix
de050 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66  DlSym   0.  #def
de060 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20  ine unixDlClose 
de070 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
de080 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73  Write nBuf bytes
de090 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20   of random data 
de0a0 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  to the supplied 
de0b0 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a  buffer zBuf..*/.
de0c0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52  static int unixR
de0d0 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65  andomness(sqlite
de0e0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
de0f0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
de100 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f  zBuf){.  UNUSED_
de110 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
de120 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69  d);.  assert((si
de130 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65  ze_t)nBuf>=(size
de140 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f  of(time_t)+sizeo
de150 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20  f(int)));..  /* 
de160 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69  We have to initi
de170 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72  alize zBuf to pr
de180 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66  event valgrind f
de190 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20  rom reporting.  
de1a0 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20  ** errors.  The 
de1b0 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62  reports issued b
de1c0 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69  y valgrind are i
de1d0 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f  ncorrect - we wo
de1e0 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20  uld.  ** prefer 
de1f0 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e  that the randomn
de200 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64  ess be increased
de210 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f   by making use o
de220 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69  f the.  ** unini
de230 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69  tialized space i
de240 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c  n zBuf - but val
de250 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e  grind errors ten
de260 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20  d to worry.  ** 
de270 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74  some users.  Rat
de280 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20  her than argue, 
de290 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20  it seems easier 
de2a0 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69  just to initiali
de2b0 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c  ze.  ** the whol
de2c0 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65  e array and sile
de2d0 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76  nce valgrind, ev
de2e0 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73  en if that means
de2f0 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73   less randomness
de300 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e  .  ** in the ran
de310 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20  dom seed..  **. 
de320 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67   ** When testing
de330 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a  , initializing z
de340 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73  Buf[] to zero is
de350 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61   all we do.  Tha
de360 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61  t means.  ** tha
de370 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20  t we always use 
de380 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20  the same random 
de390 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e  number sequence.
de3a0 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65    This makes the
de3b0 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65  .  ** tests repe
de3c0 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d  atable..  */.  m
de3d0 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e  emset(zBuf, 0, n
de3e0 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e  Buf);.#if !defin
de3f0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
de400 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c    {.    int pid,
de410 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70   fd;.    fd = op
de420 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d  en("/dev/urandom
de430 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20  ", O_RDONLY);.  
de440 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20    if( fd<0 ){.  
de450 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20      time_t t;.  
de460 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20      time(&t);.  
de470 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c      memcpy(zBuf,
de480 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b   &t, sizeof(t));
de490 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 74  .      pid = get
de4a0 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d  pid();.      mem
de4b0 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66  cpy(&zBuf[sizeof
de4c0 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65  (t)], &pid, size
de4d0 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20  of(pid));.      
de4e0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74  assert( sizeof(t
de4f0 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28  )+sizeof(pid)<=(
de500 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20  size_t)nBuf );. 
de510 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65       nBuf = size
de520 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70  of(t) + sizeof(p
de530 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  id);.    }else{.
de540 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65 61        nBuf = rea
de550 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66  d(fd, zBuf, nBuf
de560 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66  );.      close(f
de570 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  d);.    }.  }.#e
de580 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 42  ndif.  return nB
de590 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c  uf;.}.../*.** Sl
de5a0 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65  eep for a little
de5b0 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20   while.  Return 
de5c0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69  the amount of ti
de5d0 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65  me slept..** The
de5e0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
de5f0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f   number of micro
de600 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20  seconds we want 
de610 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65  to sleep..** The
de620 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
de630 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   the number of m
de640 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73  icroseconds of s
de650 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a  leep actually.**
de660 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20   requested from 
de670 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  the underlying o
de680 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c  perating system,
de690 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a   a number which.
de6a0 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 61  ** might be grea
de6b0 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
de6c0 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e  l to the argumen
de6d0 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a  t, but not less.
de6e0 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 75  ** than the argu
de6f0 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ment..*/.static 
de700 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73 71  int unixSleep(sq
de710 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73  lite3_vfs *NotUs
de720 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  ed, int microsec
de730 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 58  onds){.#if OS_VX
de740 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74  WORKS.  struct t
de750 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 73  imespec sp;..  s
de760 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f  p.tv_sec = micro
de770 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 30  seconds / 100000
de780 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 20  0;.  sp.tv_nsec 
de790 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  = (microseconds 
de7a0 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30 30  % 1000000) * 100
de7b0 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26  0;.  nanosleep(&
de7c0 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e 55  sp, NULL);.  UNU
de7d0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
de7e0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e  tUsed);.  return
de7f0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23   microseconds;.#
de800 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56  elif defined(HAV
de810 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56  E_USLEEP) && HAV
de820 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 65  E_USLEEP.  uslee
de830 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b  p(microseconds);
de840 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
de850 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
de860 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f  return microseco
de870 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74  nds;.#else.  int
de880 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72   seconds = (micr
de890 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29  oseconds+999999)
de8a0 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65  /1000000;.  slee
de8b0 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e  p(seconds);.  UN
de8c0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
de8d0 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
de8e0 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 30  n seconds*100000
de8f0 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  0;.#endif.}../*.
de900 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
de910 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65   variable, if se
de920 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  t to a non-zero 
de930 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 70  value, is interp
de940 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65 20  reted as.** the 
de950 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e 64  number of second
de960 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e 64  s since 1970 and
de970 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20   is used to set 
de980 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
de990 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
de9a0 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 74  tTime() during t
de9b0 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65  esting..*/.#ifde
de9c0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
de9d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
de9e0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d  ite3_current_tim
de9f0 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 20  e = 0;  /* Fake 
dea00 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 73  system time in s
dea10 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37  econds since 197
dea20 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a  0. */.#endif../*
dea30 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72  .** Find the cur
dea40 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e  rent time (in Un
dea50 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61  iversal Coordina
dea60 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74  ted Time).  Writ
dea70 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  e the.** current
dea80 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61   time and date a
dea90 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e  s a Julian Day n
deaa0 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f  umber into *prNo
deab0 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  w and.** return 
deac0 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  0.  Return 1 if 
dead0 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74  the time and dat
deae0 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  e cannot be foun
deaf0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
deb00 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65   unixCurrentTime
deb10 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f  (sqlite3_vfs *No
deb20 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a 70  tUsed, double *p
deb30 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69 6e  rNow){.#if defin
deb40 65 64 28 4e 4f 5f 47 45 54 54 4f 44 29 0a 20 20  ed(NO_GETTOD).  
deb50 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65  time_t t;.  time
deb60 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d  (&t);.  *prNow =
deb70 20 74 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34   t/86400.0 + 244
deb80 30 35 38 37 2e 35 3b 0a 23 65 6c 69 66 20 4f 53  0587.5;.#elif OS
deb90 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63  _VXWORKS.  struc
deba0 74 20 74 69 6d 65 73 70 65 63 20 73 4e 6f 77 3b  t timespec sNow;
debb0 0a 20 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d 65  .  clock_gettime
debc0 28 43 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45 2c  (CLOCK_REALTIME,
debd0 20 26 73 4e 6f 77 29 3b 0a 20 20 2a 70 72 4e 6f   &sNow);.  *prNo
debe0 77 20 3d 20 32 34 34 30 35 38 37 2e 35 20 2b 20  w = 2440587.5 + 
debf0 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 30  sNow.tv_sec/8640
dec00 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73  0.0 + sNow.tv_ns
dec10 65 63 2f 38 36 34 30 30 30 30 30 30 30 30 30 30  ec/8640000000000
dec20 30 2e 30 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72  0.0;.#else.  str
dec30 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e 6f 77  uct timeval sNow
dec40 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64 61 79  ;.  gettimeofday
dec50 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20 2a 70  (&sNow, 0);.  *p
dec60 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 2e 35  rNow = 2440587.5
dec70 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38   + sNow.tv_sec/8
dec80 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76  6400.0 + sNow.tv
dec90 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30 30 30  _usec/8640000000
deca0 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  0.0;.#endif..#if
decb0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
decc0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75    if( sqlite3_cu
decd0 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20  rrent_time ){.  
dece0 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74    *prNow = sqlit
decf0 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f  e3_current_time/
ded00 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38  86400.0 + 244058
ded10 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  7.5;.  }.#endif.
ded20 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
ded30 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
ded40 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
ded50 2a 20 57 65 20 61 64 64 65 64 20 74 68 65 20 78  * We added the x
ded60 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 6d  GetLastError() m
ded70 65 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 69  ethod with the i
ded80 6e 74 65 6e 74 69 6f 6e 20 6f 66 20 70 72 6f 76  ntention of prov
ded90 69 64 69 6e 67 0a 2a 2a 20 62 65 74 74 65 72 20  iding.** better 
deda0 6c 6f 77 2d 6c 65 76 65 6c 20 65 72 72 6f 72 20  low-level error 
dedb0 6d 65 73 73 61 67 65 73 20 77 68 65 6e 20 6f 70  messages when op
dedc0 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 70  erating-system p
dedd0 72 6f 62 6c 65 6d 73 20 63 6f 6d 65 20 75 70 0a  roblems come up.
dede0 2a 2a 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65  ** during SQLite
dedf0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74   operation.  But
dee00 20 73 6f 20 66 61 72 2c 20 6e 6f 6e 65 20 6f 66   so far, none of
dee10 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 69   that has been i
dee20 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e  mplemented.** in
dee30 20 74 68 65 20 63 6f 72 65 2e 20 20 53 6f 20 74   the core.  So t
dee40 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e  his routine is n
dee50 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 46 6f  ever called.  Fo
dee60 72 20 6e 6f 77 2c 20 69 74 20 69 73 20 6d 65 72  r now, it is mer
dee70 65 6c 79 0a 2a 2a 20 61 20 70 6c 61 63 65 2d 68  ely.** a place-h
dee80 6f 6c 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  older..*/.static
dee90 20 69 6e 74 20 75 6e 69 78 47 65 74 4c 61 73 74   int unixGetLast
deea0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
deeb0 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20  s *NotUsed, int 
deec0 4e 6f 74 55 73 65 64 32 2c 20 63 68 61 72 20 2a  NotUsed2, char *
deed0 4e 6f 74 55 73 65 64 33 29 7b 0a 20 20 55 4e 55  NotUsed3){.  UNU
deee0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
deef0 74 55 73 65 64 29 3b 0a 20 20 55 4e 55 53 45 44  tUsed);.  UNUSED
def00 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
def10 65 64 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ed2);.  UNUSED_P
def20 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
def30 33 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  3);.  return 0;.
def40 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../*.**********
def50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
def60 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66  nd of sqlite3_vf
def70 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a  s methods ******
def80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
def90 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
defa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
defb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
defc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
defd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
defe0 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
deff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
df000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
df010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
df020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
df030 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
df040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
df050 2a 20 42 65 67 69 6e 20 50 72 6f 78 79 20 4c 6f  * Begin Proxy Lo
df060 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cking **********
df070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
df080 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 72 6f  ******.**.** Pro
df090 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 20  xy locking is a 
df0a0 22 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65  "uber-locking-me
df0b0 74 68 6f 64 22 20 69 6e 20 74 68 69 73 20 73 65  thod" in this se
df0c0 6e 73 65 3a 20 20 49 74 20 75 73 65 73 20 74 68  nse:  It uses th
df0d0 65 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69  e.** other locki
df0e0 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 73 65  ng methods on se
df0f0 63 6f 6e 64 61 72 79 20 6c 6f 63 6b 20 66 69 6c  condary lock fil
df100 65 73 2e 20 20 50 72 6f 78 79 20 6c 6f 63 6b 69  es.  Proxy locki
df110 6e 67 20 69 73 20 61 0a 2a 2a 20 6d 65 74 61 2d  ng is a.** meta-
df120 6c 61 79 65 72 20 6f 76 65 72 20 74 6f 70 20 6f  layer over top o
df130 66 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20  f the primitive 
df140 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  locking implemen
df150 74 65 64 20 61 62 6f 76 65 2e 20 20 46 6f 72 0a  ted above.  For.
df160 2a 2a 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20  ** this reason, 
df170 74 68 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61  the division tha
df180 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6f 66 20  t implements of 
df190 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73  proxy locking is
df1a0 20 64 65 66 65 72 72 65 64 0a 2a 2a 20 75 6e 74   deferred.** unt
df1b0 69 6c 20 6c 61 74 65 20 69 6e 20 74 68 65 20 66  il late in the f
df1c0 69 6c 65 20 28 68 65 72 65 29 20 61 66 74 65 72  ile (here) after
df1d0 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65   all of the othe
df1e0 72 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 68 61  r I/O methods ha
df1f0 76 65 0a 2a 2a 20 62 65 65 6e 20 64 65 66 69 6e  ve.** been defin
df200 65 64 20 2d 20 73 6f 20 74 68 61 74 20 74 68 65  ed - so that the
df210 20 70 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69   primitive locki
df220 6e 67 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61  ng methods are a
df230 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 73  vailable.** as s
df240 65 72 76 69 63 65 73 20 74 6f 20 68 65 6c 70 20  ervices to help 
df250 77 69 74 68 20 74 68 65 20 69 6d 70 6c 65 6d 65  with the impleme
df260 6e 74 61 74 69 6f 6e 20 6f 66 20 70 72 6f 78 79  ntation of proxy
df270 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 2a   locking..**.***
df280 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  *.**.** The defa
df290 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65  ult locking sche
df2a0 6d 65 73 20 69 6e 20 53 51 4c 69 74 65 20 75 73  mes in SQLite us
df2b0 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63  e byte-range loc
df2c0 6b 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74  ks on the.** dat
df2d0 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 6f  abase file to co
df2e0 6f 72 64 69 6e 61 74 65 20 73 61 66 65 2c 20 63  ordinate safe, c
df2f0 6f 6e 63 75 72 72 65 6e 74 20 61 63 63 65 73 73  oncurrent access
df300 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 72 65 61   by multiple rea
df310 64 65 72 73 0a 2a 2a 20 61 6e 64 20 77 72 69 74  ders.** and writ
df320 65 72 73 20 5b 68 74 74 70 3a 2f 2f 73 71 6c 69  ers [http://sqli
df330 74 65 2e 6f 72 67 2f 6c 6f 63 6b 69 6e 67 76 33  te.org/lockingv3
df340 2e 68 74 6d 6c 5d 2e 20 20 54 68 65 20 66 69 76  .html].  The fiv
df350 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a  e file locking.*
df360 2a 20 73 74 61 74 65 73 20 28 55 4e 4c 4f 43 4b  * states (UNLOCK
df370 45 44 2c 20 50 45 4e 44 49 4e 47 2c 20 53 48 41  ED, PENDING, SHA
df380 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 45  RED, RESERVED, E
df390 58 43 4c 55 53 49 56 45 29 20 61 72 65 20 69 6d  XCLUSIVE) are im
df3a0 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 61 73 20  plemented.** as 
df3b0 50 4f 53 49 58 20 72 65 61 64 20 26 20 77 72 69  POSIX read & wri
df3c0 74 65 20 6c 6f 63 6b 73 20 6f 76 65 72 20 66 69  te locks over fi
df3d0 78 65 64 20 73 65 74 20 6f 66 20 6c 6f 63 61 74  xed set of locat
df3e0 69 6f 6e 73 20 28 76 69 61 20 66 73 63 74 6c 29  ions (via fsctl)
df3f0 2c 0a 2a 2a 20 6f 6e 20 41 46 50 20 61 6e 64 20  ,.** on AFP and 
df400 53 4d 42 20 6f 6e 6c 79 20 65 78 63 6c 75 73 69  SMB only exclusi
df410 76 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f  ve byte-range lo
df420 63 6b 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  cks are availabl
df430 65 20 76 69 61 20 66 73 63 74 6c 0a 2a 2a 20 77  e via fsctl.** w
df440 69 74 68 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32  ith _IOWR('z', 2
df450 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61  3, struct ByteRa
df460 6e 67 65 4c 6f 63 6b 50 42 32 29 20 74 6f 20 74  ngeLockPB2) to t
df470 72 61 63 6b 20 74 68 65 20 73 61 6d 65 20 35 20  rack the same 5 
df480 73 74 61 74 65 73 2e 0a 2a 2a 20 54 6f 20 73 69  states..** To si
df490 6d 75 6c 61 74 65 20 61 20 46 5f 52 44 4c 43 4b  mulate a F_RDLCK
df4a0 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 72   on the shared r
df4b0 61 6e 67 65 2c 20 6f 6e 20 41 46 50 20 61 20 72  ange, on AFP a r
df4c0 61 6e 64 6f 6d 6c 79 20 73 65 6c 65 63 74 65 64  andomly selected
df4d0 0a 2a 2a 20 61 64 64 72 65 73 73 20 69 6e 20 74  .** address in t
df4e0 68 65 20 73 68 61 72 65 64 20 72 61 6e 67 65 20  he shared range 
df4f0 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 20 53  is taken for a S
df500 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20  HARED lock, the 
df510 65 6e 74 69 72 65 0a 2a 2a 20 73 68 61 72 65 64  entire.** shared
df520 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20   range is taken 
df530 66 6f 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45  for an EXCLUSIVE
df540 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a 2a 2a 20 20 20   lock):.**.**   
df550 20 20 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20     PENDING_BYTE 
df560 20 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30         0x4000000
df570 30 09 09 20 20 20 09 0a 2a 2a 20 20 20 20 20 20  0..   ..**      
df580 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 20 20  RESERVED_BYTE   
df590 20 20 20 20 30 78 34 30 30 30 30 30 30 31 0a 2a      0x40000001.*
df5a0 2a 20 20 20 20 20 20 53 48 41 52 45 44 5f 52 41  *      SHARED_RA
df5b0 4e 47 45 20 20 20 20 20 20 20 20 30 78 34 30 30  NGE        0x400
df5c0 30 30 30 30 32 20 2d 3e 20 30 78 34 30 30 30 30  00002 -> 0x40000
df5d0 32 30 30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77  200.**.** This w
df5e0 6f 72 6b 73 20 77 65 6c 6c 20 6f 6e 20 74 68 65  orks well on the
df5f0 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74   local file syst
df600 65 6d 2c 20 62 75 74 20 73 68 6f 77 73 20 61 20  em, but shows a 
df610 6e 65 61 72 6c 79 20 31 30 30 78 0a 2a 2a 20 73  nearly 100x.** s
df620 6c 6f 77 64 6f 77 6e 20 69 6e 20 72 65 61 64 20  lowdown in read 
df630 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 6e 20 41  performance on A
df640 46 50 20 62 65 63 61 75 73 65 20 74 68 65 20 41  FP because the A
df650 46 50 20 63 6c 69 65 6e 74 20 64 69 73 61 62 6c  FP client disabl
df660 65 73 0a 2a 2a 20 74 68 65 20 72 65 61 64 20 63  es.** the read c
df670 61 63 68 65 20 77 68 65 6e 20 62 79 74 65 2d 72  ache when byte-r
df680 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 70  ange locks are p
df690 72 65 73 65 6e 74 2e 20 20 45 6e 61 62 6c 69 6e  resent.  Enablin
df6a0 67 20 74 68 65 20 72 65 61 64 0a 2a 2a 20 63 61  g the read.** ca
df6b0 63 68 65 20 65 78 70 6f 73 65 73 20 61 20 63 61  che exposes a ca
df6c0 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20 70 72  che coherency pr
df6d0 6f 62 6c 65 6d 20 74 68 61 74 20 69 73 20 70 72  oblem that is pr
df6e0 65 73 65 6e 74 20 6f 6e 20 61 6c 6c 20 4f 53 20  esent on all OS 
df6f0 58 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 20 6e  X.** supported n
df700 65 74 77 6f 72 6b 20 66 69 6c 65 20 73 79 73 74  etwork file syst
df710 65 6d 73 2e 20 20 4e 46 53 20 61 6e 64 20 41 46  ems.  NFS and AF
df720 50 20 62 6f 74 68 20 6f 62 73 65 72 76 65 20 74  P both observe t
df730 68 65 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f 2d 6f  he.** close-to-o
df740 70 65 6e 20 73 65 6d 61 6e 74 69 63 73 20 66 6f  pen semantics fo
df750 72 20 65 6e 73 75 72 69 6e 67 20 63 61 63 68 65  r ensuring cache
df760 20 63 6f 68 65 72 65 6e 63 79 0a 2a 2a 20 5b 68   coherency.** [h
df770 74 74 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72 63 65  ttp://nfs.source
df780 66 6f 72 67 65 2e 6e 65 74 2f 23 66 61 71 5f 61  forge.net/#faq_a
df790 38 5d 2c 20 77 68 69 63 68 20 64 6f 65 73 20 6e  8], which does n
df7a0 6f 74 20 65 66 66 65 63 74 69 76 65 6c 79 0a 2a  ot effectively.*
df7b0 2a 20 61 64 64 72 65 73 73 20 74 68 65 20 72 65  * address the re
df7c0 71 75 69 72 65 6d 65 6e 74 73 20 66 6f 72 20 63  quirements for c
df7d0 6f 6e 63 75 72 72 65 6e 74 20 64 61 74 61 62 61  oncurrent databa
df7e0 73 65 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c  se access by mul
df7f0 74 69 70 6c 65 0a 2a 2a 20 72 65 61 64 65 72 73  tiple.** readers
df800 20 61 6e 64 20 77 72 69 74 65 72 73 0a 2a 2a 20   and writers.** 
df810 5b 68 74 74 70 3a 2f 2f 77 77 77 2e 6e 61 62 62  [http://www.nabb
df820 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d 6f 6e  le.com/SQLite-on
df830 2d 4e 46 53 2d 63 61 63 68 65 2d 63 6f 68 65 72  -NFS-cache-coher
df840 65 6e 63 79 2d 74 64 31 35 36 35 35 37 30 31 2e  ency-td15655701.
df850 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  html]..**.** To 
df860 61 64 64 72 65 73 73 20 74 68 65 20 70 65 72 66  address the perf
df870 6f 72 6d 61 6e 63 65 20 61 6e 64 20 63 61 63 68  ormance and cach
df880 65 20 63 6f 68 65 72 65 6e 63 79 20 69 73 73 75  e coherency issu
df890 65 73 2c 20 70 72 6f 78 79 20 66 69 6c 65 20 6c  es, proxy file l
df8a0 6f 63 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65  ocking.** change
df8b0 73 20 74 68 65 20 77 61 79 20 64 61 74 61 62 61  s the way databa
df8c0 73 65 20 61 63 63 65 73 73 20 69 73 20 63 6f 6e  se access is con
df8d0 74 72 6f 6c 6c 65 64 20 62 79 20 6c 69 6d 69 74  trolled by limit
df8e0 69 6e 67 20 61 63 63 65 73 73 20 74 6f 20 61 0a  ing access to a.
df8f0 2a 2a 20 73 69 6e 67 6c 65 20 68 6f 73 74 20 61  ** single host a
df900 74 20 61 20 74 69 6d 65 20 61 6e 64 20 6d 6f 76  t a time and mov
df910 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73 20 6f  ing file locks o
df920 66 66 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ff of the databa
df930 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6f  se file.** and o
df940 6e 74 6f 20 61 20 70 72 6f 78 79 20 66 69 6c 65  nto a proxy file
df950 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69   on the local fi
df960 6c 65 20 73 79 73 74 65 6d 2e 20 20 0a 2a 2a 0a  le system.  .**.
df970 2a 2a 0a 2a 2a 20 55 73 69 6e 67 20 70 72 6f 78  **.** Using prox
df980 79 20 6c 6f 63 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d  y locks.** -----
df990 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a  ------------.**.
df9a0 2a 2a 20 43 20 41 50 49 73 0a 2a 2a 0a 2a 2a 20  ** C APIs.**.** 
df9b0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f   sqlite3_file_co
df9c0 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65  ntrol(db, dbname
df9d0 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43  , SQLITE_SET_LOC
df9e0 4b 50 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a 20 20  KPROXYFILE,.**  
df9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dfa00 20 20 20 20 20 3c 70 72 6f 78 79 5f 70 61 74 68       <proxy_path
dfa10 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 2a  > | ":auto:");.*
dfa20 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  *  sqlite3_file_
dfa30 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61  control(db, dbna
dfa40 6d 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c  me, SQLITE_GET_L
dfa50 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 26 3c  OCKPROXYFILE, &<
dfa60 70 72 6f 78 79 5f 70 61 74 68 3e 29 3b 0a 2a 2a  proxy_path>);.**
dfa70 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70 72 61 67 6d  .**.** SQL pragm
dfa80 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47 4d 41  as.**.**  PRAGMA
dfa90 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b   [database.]lock
dfaa0 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 3c 70 72 6f  _proxy_file=<pro
dfab0 78 79 5f 70 61 74 68 3e 20 7c 20 3a 61 75 74 6f  xy_path> | :auto
dfac0 3a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61  :.**  PRAGMA [da
dfad0 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f  tabase.]lock_pro
dfae0 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20 53 70  xy_file.**.** Sp
dfaf0 65 63 69 66 79 69 6e 67 20 22 3a 61 75 74 6f 3a  ecifying ":auto:
dfb00 22 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20  " means that if 
dfb10 74 68 65 72 65 20 69 73 20 61 20 63 6f 6e 63 68  there is a conch
dfb20 20 66 69 6c 65 20 77 69 74 68 20 61 20 6d 61 74   file with a mat
dfb30 63 68 69 6e 67 0a 2a 2a 20 68 6f 73 74 20 49 44  ching.** host ID
dfb40 20 69 6e 20 69 74 2c 20 74 68 65 20 70 72 6f 78   in it, the prox
dfb50 79 20 70 61 74 68 20 69 6e 20 74 68 65 20 63 6f  y path in the co
dfb60 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65  nch file will be
dfb70 20 75 73 65 64 2c 20 6f 74 68 65 72 77 69 73 65   used, otherwise
dfb80 0a 2a 2a 20 61 20 70 72 6f 78 79 20 70 61 74 68  .** a proxy path
dfb90 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 75 73   based on the us
dfba0 65 72 27 73 20 74 65 6d 70 20 64 69 72 0a 2a 2a  er's temp dir.**
dfbb0 20 28 76 69 61 20 63 6f 6e 66 73 74 72 28 5f 43   (via confstr(_C
dfbc0 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45  S_DARWIN_USER_TE
dfbd0 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29 20 77 69 6c  MP_DIR,...)) wil
dfbe0 6c 20 62 65 20 75 73 65 64 20 61 6e 64 20 74 68  l be used and th
dfbf0 65 0a 2a 2a 20 61 63 74 75 61 6c 20 70 72 6f 78  e.** actual prox
dfc00 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 67  y file name is g
dfc10 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 74 68  enerated from th
dfc20 65 20 6e 61 6d 65 20 61 6e 64 20 70 61 74 68 20  e name and path 
dfc30 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  of the.** databa
dfc40 73 65 20 66 69 6c 65 2e 20 20 46 6f 72 20 65 78  se file.  For ex
dfc50 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ample:.**.**    
dfc60 20 20 20 46 6f 72 20 64 61 74 61 62 61 73 65 20     For database 
dfc70 70 61 74 68 20 22 2f 55 73 65 72 73 2f 6d 65 2f  path "/Users/me/
dfc80 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20 20 20 20 20  foo.db" .**     
dfc90 20 20 54 68 65 20 6c 6f 63 6b 20 70 61 74 68 20    The lock path 
dfca0 77 69 6c 6c 20 62 65 20 22 3c 74 6d 70 64 69 72  will be "<tmpdir
dfcb0 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 6b 73 2f 5f  >/sqliteplocks/_
dfcc0 55 73 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 62 3a  Users_me_foo.db:
dfcd0 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e  auto:").**.** On
dfce0 63 65 20 61 20 6c 6f 63 6b 20 70 72 6f 78 79 20  ce a lock proxy 
dfcf0 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f  is configured fo
dfd00 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  r a database con
dfd10 6e 65 63 74 69 6f 6e 2c 20 69 74 20 63 61 6e 20  nection, it can 
dfd20 6e 6f 74 0a 2a 2a 20 62 65 20 72 65 6d 6f 76 65  not.** be remove
dfd30 64 2c 20 68 6f 77 65 76 65 72 20 69 74 20 6d 61  d, however it ma
dfd40 79 20 62 65 20 73 77 69 74 63 68 65 64 20 74 6f  y be switched to
dfd50 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f   a different pro
dfd60 78 79 20 70 61 74 68 20 76 69 61 0a 2a 2a 20 74  xy path via.** t
dfd70 68 65 20 61 62 6f 76 65 20 41 50 49 73 20 28 61  he above APIs (a
dfd80 73 73 75 6d 69 6e 67 20 74 68 65 20 63 6f 6e 63  ssuming the conc
dfd90 68 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 62 65  h file is not be
dfda0 69 6e 67 20 68 65 6c 64 20 62 79 20 61 6e 6f 74  ing held by anot
dfdb0 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  her.** connectio
dfdc0 6e 20 6f 72 20 70 72 6f 63 65 73 73 29 2e 20 0a  n or process). .
dfdd0 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70 72 6f  **.**.** How pro
dfde0 78 79 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73  xy locking works
dfdf0 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .** ------------
dfe00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a  -----------.**.*
dfe10 2a 20 50 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63  * Proxy file loc
dfe20 6b 69 6e 67 20 72 65 6c 69 65 73 20 70 72 69 6d  king relies prim
dfe30 61 72 69 6c 79 20 6f 6e 20 74 77 6f 20 6e 65 77  arily on two new
dfe40 20 73 75 70 70 6f 72 74 69 6e 67 20 66 69 6c 65   supporting file
dfe50 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 63  s: .**.**   *  c
dfe60 6f 6e 63 68 20 66 69 6c 65 20 74 6f 20 6c 69 6d  onch file to lim
dfe70 69 74 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  it access to the
dfe80 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
dfe90 6f 20 61 20 73 69 6e 67 6c 65 20 68 6f 73 74 0a  o a single host.
dfea0 2a 2a 20 20 20 20 20 20 61 74 20 61 20 74 69 6d  **      at a tim
dfeb0 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70 72 6f  e.**.**   *  pro
dfec0 78 79 20 66 69 6c 65 20 74 6f 20 61 63 74 20 61  xy file to act a
dfed0 73 20 61 20 70 72 6f 78 79 20 66 6f 72 20 74 68  s a proxy for th
dfee0 65 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  e advisory locks
dfef0 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20 20 20   normally.**    
dff00 20 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64    taken on the d
dff10 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 54 68  atabase.**.** Th
dff20 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2d 20 74  e conch file - t
dff30 6f 20 75 73 65 20 61 20 70 72 6f 78 79 20 66 69  o use a proxy fi
dff40 6c 65 2c 20 73 71 6c 69 74 65 20 6d 75 73 74 20  le, sqlite must 
dff50 66 69 72 73 74 20 22 68 6f 6c 64 20 74 68 65 20  first "hold the 
dff60 63 6f 6e 63 68 22 0a 2a 2a 20 62 79 20 74 61 6b  conch".** by tak
dff70 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 2d 73 74  ing an sqlite-st
dff80 79 6c 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  yle shared lock 
dff90 6f 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  on the conch fil
dffa0 65 2c 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a  e, reading the.*
dffb0 2a 20 63 6f 6e 74 65 6e 74 73 20 61 6e 64 20 63  * contents and c
dffc0 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 68 6f 73  omparing the hos
dffd0 74 27 73 20 75 6e 69 71 75 65 20 68 6f 73 74 20  t's unique host 
dffe0 49 44 20 28 73 65 65 20 62 65 6c 6f 77 29 20 61  ID (see below) a
dfff0 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f 78 79  nd lock.** proxy
e0000 20 70 61 74 68 20 61 67 61 69 6e 73 74 20 74 68   path against th
e0010 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  e values stored 
e0020 69 6e 20 74 68 65 20 63 6f 6e 63 68 2e 20 20 54  in the conch.  T
e0030 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73  he conch file is
e0040 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68  .** stored in th
e0050 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79  e same directory
e0060 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65   as the database
e0070 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69   file and the fi
e0080 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 70 61  le name.** is pa
e0090 74 74 65 72 6e 65 64 20 61 66 74 65 72 20 74 68  tterned after th
e00a0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
e00b0 6e 61 6d 65 20 61 73 20 22 2e 3c 64 61 74 61 62  name as ".<datab
e00c0 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68 22 2e  asename>-conch".
e00d0 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 63 68  .** If the conch
e00e0 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65   file does not e
e00f0 78 69 73 74 2c 20 6f 72 20 69 74 27 73 20 63 6f  xist, or it's co
e0100 6e 74 65 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61  ntents do not ma
e0110 74 63 68 20 74 68 65 0a 2a 2a 20 68 6f 73 74 20  tch the.** host 
e0120 49 44 20 61 6e 64 2f 6f 72 20 70 72 6f 78 79 20  ID and/or proxy 
e0130 70 61 74 68 2c 20 74 68 65 6e 20 74 68 65 20 6c  path, then the l
e0140 6f 63 6b 20 69 73 20 65 73 63 61 6c 61 74 65 64  ock is escalated
e0150 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65   to an exclusive
e0160 0a 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 74 68 65  .** lock and the
e0170 20 63 6f 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74   conch file cont
e0180 65 6e 74 73 20 69 73 20 75 70 64 61 74 65 64 20  ents is updated 
e0190 77 69 74 68 20 74 68 65 20 68 6f 73 74 20 49 44  with the host ID
e01a0 20 61 6e 64 20 70 72 6f 78 79 0a 2a 2a 20 70 61   and proxy.** pa
e01b0 74 68 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  th and the lock 
e01c0 69 73 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f  is downgraded to
e01d0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61   a shared lock a
e01e0 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20 63 6f  gain.  If the co
e01f0 6e 63 68 0a 2a 2a 20 69 73 20 68 65 6c 64 20 62  nch.** is held b
e0200 79 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73  y another proces
e0210 73 20 28 77 69 74 68 20 61 20 73 68 61 72 65 64  s (with a shared
e0220 20 6c 6f 63 6b 29 2c 20 74 68 65 20 65 78 63 6c   lock), the excl
e0230 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 77 69  usive lock.** wi
e0240 6c 6c 20 66 61 69 6c 20 61 6e 64 20 53 51 4c 49  ll fail and SQLI
e0250 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72  TE_BUSY is retur
e0260 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ned..**.** The p
e0270 72 6f 78 79 20 66 69 6c 65 20 2d 20 61 20 73 69  roxy file - a si
e0280 6e 67 6c 65 2d 62 79 74 65 20 66 69 6c 65 20 75  ngle-byte file u
e0290 73 65 64 20 66 6f 72 20 61 6c 6c 20 61 64 76 69  sed for all advi
e02a0 73 6f 72 79 20 66 69 6c 65 20 6c 6f 63 6b 73 0a  sory file locks.
e02b0 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 74 61 6b 65  ** normally take
e02c0 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
e02d0 65 20 66 69 6c 65 2e 20 20 20 54 68 69 73 20 61  e file.   This a
e02e0 6c 6c 6f 77 73 20 66 6f 72 20 73 61 66 65 20 73  llows for safe s
e02f0 68 61 72 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65  haring.** of the
e0300 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
e0310 6f 72 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64  or multiple read
e0320 65 72 73 20 61 6e 64 20 77 72 69 74 65 72 73 20  ers and writers 
e0330 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 68  on the same.** h
e0340 6f 73 74 20 28 74 68 65 20 63 6f 6e 63 68 20 65  ost (the conch e
e0350 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 65 79  nsures that they
e0360 20 61 6c 6c 20 75 73 65 20 74 68 65 20 73 61 6d   all use the sam
e0370 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c  e local lock fil
e0380 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  e)..**.** There 
e0390 69 73 20 61 20 74 68 69 72 64 20 66 69 6c 65 20  is a third file 
e03a0 2d 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69  - the host ID fi
e03b0 6c 65 20 2d 20 75 73 65 64 20 61 73 20 61 20 70  le - used as a p
e03c0 65 72 73 69 73 74 65 6e 74 20 72 65 63 6f 72 64  ersistent record
e03d0 0a 2a 2a 20 6f 66 20 61 20 75 6e 69 71 75 65 20  .** of a unique 
e03e0 69 64 65 6e 74 69 66 69 65 72 20 66 6f 72 20 74  identifier for t
e03f0 68 65 20 68 6f 73 74 2c 20 61 20 31 32 38 2d 62  he host, a 128-b
e0400 79 74 65 20 75 6e 69 71 75 65 20 68 6f 73 74 20  yte unique host 
e0410 69 64 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 74 68  id file.** in th
e0420 65 20 70 61 74 68 20 64 65 66 69 6e 65 64 20 62  e path defined b
e0430 79 20 74 68 65 20 48 4f 53 54 49 44 50 41 54 48  y the HOSTIDPATH
e0440 20 6d 61 63 72 6f 20 28 64 65 66 61 75 6c 74 20   macro (default 
e0450 76 61 6c 75 65 20 69 73 0a 2a 2a 20 2f 4c 69 62  value is.** /Lib
e0460 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d  rary/Caches/.com
e0470 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e  .apple.sqliteCon
e0480 63 68 48 6f 73 74 49 64 29 2e 0a 2a 2a 0a 2a 2a  chHostId)..**.**
e0490 20 52 65 71 75 65 73 74 69 6e 67 20 74 68 65 20   Requesting the 
e04a0 6c 6f 63 6b 20 70 72 6f 78 79 20 64 6f 65 73 20  lock proxy does 
e04b0 6e 6f 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  not immediately 
e04c0 74 61 6b 65 20 74 68 65 20 63 6f 6e 63 68 2c 20  take the conch, 
e04d0 69 74 20 69 73 0a 2a 2a 20 6f 6e 6c 79 20 74 61  it is.** only ta
e04e0 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 66 69 72  ken when the fir
e04f0 73 74 20 72 65 71 75 65 73 74 20 74 6f 20 6c 6f  st request to lo
e0500 63 6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ck database file
e0510 20 69 73 20 6d 61 64 65 2e 20 20 0a 2a 2a 20 54   is made.  .** T
e0520 68 69 73 20 6d 61 74 63 68 65 73 20 74 68 65 20  his matches the 
e0530 73 65 6d 61 6e 74 69 63 73 20 6f 66 20 74 68 65  semantics of the
e0540 20 74 72 61 64 69 74 69 6f 6e 61 6c 20 6c 6f 63   traditional loc
e0550 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 2c 20 77  king behavior, w
e0560 68 65 72 65 0a 2a 2a 20 6f 70 65 6e 69 6e 67 20  here.** opening 
e0570 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  a connection to 
e0580 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  a database file 
e0590 64 6f 65 73 20 6e 6f 74 20 74 61 6b 65 20 61 20  does not take a 
e05a0 6c 6f 63 6b 20 6f 6e 20 69 74 2e 0a 2a 2a 20 54  lock on it..** T
e05b0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61  he shared lock a
e05c0 6e 64 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  nd an open file 
e05d0 64 65 73 63 72 69 70 74 6f 72 20 61 72 65 20 6d  descriptor are m
e05e0 61 69 6e 74 61 69 6e 65 64 20 75 6e 74 69 6c 20  aintained until 
e05f0 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  .** the connecti
e0600 6f 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  on to the databa
e0610 73 65 20 69 73 20 63 6c 6f 73 65 64 2e 20 0a 2a  se is closed. .*
e0620 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66  *.** The proxy f
e0630 69 6c 65 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b  ile and the lock
e0640 20 66 69 6c 65 20 61 72 65 20 6e 65 76 65 72 20   file are never 
e0650 64 65 6c 65 74 65 64 20 73 6f 20 74 68 65 79 20  deleted so they 
e0660 6f 6e 6c 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20  only need.** to 
e0670 62 65 20 63 72 65 61 74 65 64 20 74 68 65 20 66  be created the f
e0680 69 72 73 74 20 74 69 6d 65 20 74 68 65 79 20 61  irst time they a
e0690 72 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43  re used..**.** C
e06a0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
e06b0 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d  ions.** --------
e06c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
e06d0 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 45 46  .**  SQLITE_PREF
e06e0 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47  ER_PROXY_LOCKING
e06f0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 44 61 74  .**.**       Dat
e0700 61 62 61 73 65 20 66 69 6c 65 73 20 61 63 63 65  abase files acce
e0710 73 73 65 64 20 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61  ssed on non-loca
e0720 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20 61  l file systems a
e0730 72 65 0a 2a 2a 20 20 20 20 20 20 20 61 75 74 6f  re.**       auto
e0740 6d 61 74 69 63 61 6c 6c 79 20 63 6f 6e 66 69 67  matically config
e0750 75 72 65 64 20 66 6f 72 20 70 72 6f 78 79 20 6c  ured for proxy l
e0760 6f 63 6b 69 6e 67 2c 20 6c 6f 63 6b 20 66 69 6c  ocking, lock fil
e0770 65 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20  es are.**       
e0780 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61  named automatica
e0790 6c 6c 79 20 75 73 69 6e 67 20 74 68 65 20 73 61  lly using the sa
e07a0 6d 65 20 6c 6f 67 69 63 20 61 73 0a 2a 2a 20 20  me logic as.**  
e07b0 20 20 20 20 20 50 52 41 47 4d 41 20 6c 6f 63 6b       PRAGMA lock
e07c0 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 22 3a 61 75  _proxy_file=":au
e07d0 74 6f 3a 22 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20  to:".**    .**  
e07e0 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42  SQLITE_PROXY_DEB
e07f0 55 47 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 45  UG.**.**       E
e0800 6e 61 62 6c 65 73 20 74 68 65 20 6c 6f 67 67 69  nables the loggi
e0810 6e 67 20 6f 66 20 65 72 72 6f 72 20 6d 65 73 73  ng of error mess
e0820 61 67 65 73 20 64 75 72 69 6e 67 20 68 6f 73 74  ages during host
e0830 20 69 64 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20   id file.**     
e0840 20 20 72 65 74 72 69 65 76 61 6c 20 61 6e 64 20    retrieval and 
e0850 63 72 65 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20  creation.**.**  
e0860 48 4f 53 54 49 44 50 41 54 48 0a 2a 2a 0a 2a 2a  HOSTIDPATH.**.**
e0870 20 20 20 20 20 20 20 4f 76 65 72 72 69 64 65 73         Overrides
e0880 20 74 68 65 20 64 65 66 61 75 6c 74 20 68 6f 73   the default hos
e0890 74 20 49 44 20 66 69 6c 65 20 70 61 74 68 20 6c  t ID file path l
e08a0 6f 63 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c  ocation.**.**  L
e08b0 4f 43 4b 50 52 4f 58 59 44 49 52 0a 2a 2a 0a 2a  OCKPROXYDIR.**.*
e08c0 2a 20 20 20 20 20 20 20 4f 76 65 72 72 69 64 65  *       Override
e08d0 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 64 69  s the default di
e08e0 72 65 63 74 6f 72 79 20 75 73 65 64 20 66 6f 72  rectory used for
e08f0 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65   lock proxy file
e0900 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20  s that.**       
e0910 61 72 65 20 6e 61 6d 65 64 20 61 75 74 6f 6d 61  are named automa
e0920 74 69 63 61 6c 6c 79 20 76 69 61 20 74 68 65 20  tically via the 
e0930 22 3a 61 75 74 6f 3a 22 20 73 65 74 74 69 6e 67  ":auto:" setting
e0940 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 44  .**.**  SQLITE_D
e0950 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
e0960 50 45 52 4d 49 53 53 49 4f 4e 53 0a 2a 2a 0a 2a  PERMISSIONS.**.*
e0970 2a 20 20 20 20 20 20 20 50 65 72 6d 69 73 73 69  *       Permissi
e0980 6f 6e 73 20 74 6f 20 75 73 65 20 77 68 65 6e 20  ons to use when 
e0990 63 72 65 61 74 69 6e 67 20 61 20 64 69 72 65 63  creating a direc
e09a0 74 6f 72 79 20 66 6f 72 20 73 74 6f 72 69 6e 67  tory for storing
e09b0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 6c 6f   the.**       lo
e09c0 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 2c 20  ck proxy files, 
e09d0 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 4c  only used when L
e09e0 4f 43 4b 50 52 4f 58 59 44 49 52 20 69 73 20 6e  OCKPROXYDIR is n
e09f0 6f 74 20 73 65 74 2e 0a 2a 2a 20 20 20 20 0a 2a  ot set..**    .*
e0a00 2a 20 20 20 20 0a 2a 2a 20 41 73 20 6d 65 6e 74  *    .** As ment
e0a10 69 6f 6e 65 64 20 61 62 6f 76 65 2c 20 77 68 65  ioned above, whe
e0a20 6e 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  n compiled with 
e0a30 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52  SQLITE_PREFER_PR
e0a40 4f 58 59 5f 4c 4f 43 4b 49 4e 47 2c 0a 2a 2a 20  OXY_LOCKING,.** 
e0a50 73 65 74 74 69 6e 67 20 74 68 65 20 65 6e 76 69  setting the envi
e0a60 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c 65  ronment variable
e0a70 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52   SQLITE_FORCE_PR
e0a80 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 74 6f 20 31  OXY_LOCKING to 1
e0a90 20 77 69 6c 6c 0a 2a 2a 20 66 6f 72 63 65 20 70   will.** force p
e0aa0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20  roxy locking to 
e0ab0 62 65 20 75 73 65 64 20 66 6f 72 20 65 76 65 72  be used for ever
e0ac0 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  y database file 
e0ad0 6f 70 65 6e 65 64 2c 20 61 6e 64 20 30 0a 2a 2a  opened, and 0.**
e0ae0 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 75 74 6f   will force auto
e0af0 6d 61 74 69 63 20 70 72 6f 78 79 20 6c 6f 63 6b  matic proxy lock
e0b00 69 6e 67 20 74 6f 20 62 65 20 64 69 73 61 62 6c  ing to be disabl
e0b10 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62  ed for all datab
e0b20 61 73 65 0a 2a 2a 20 66 69 6c 65 73 20 28 65 78  ase.** files (ex
e0b30 70 6c 69 63 69 74 79 20 63 61 6c 6c 69 6e 67 20  plicity calling 
e0b40 74 68 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c  the SQLITE_SET_L
e0b50 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 70 72 61  OCKPROXYFILE pra
e0b60 67 6d 61 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  gma or.** sqlite
e0b70 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 41 50  _file_control AP
e0b80 49 20 69 73 20 6e 6f 74 20 61 66 66 65 63 74 65  I is not affecte
e0b90 64 20 62 79 20 53 51 4c 49 54 45 5f 46 4f 52 43  d by SQLITE_FORC
e0ba0 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29  E_PROXY_LOCKING)
e0bb0 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78  ..*/../*.** Prox
e0bc0 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c  y locking is onl
e0bd0 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d  y available on M
e0be0 61 63 4f 53 58 20 0a 2a 2f 0a 23 69 66 20 64 65  acOSX .*/.#if de
e0bf0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e0c00 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
e0c10 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
e0c20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
e0c30 45 53 54 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20  EST./* simulate 
e0c40 6d 75 6c 74 69 70 6c 65 20 68 6f 73 74 73 20 62  multiple hosts b
e0c50 79 20 63 72 65 61 74 69 6e 67 20 75 6e 69 71 75  y creating uniqu
e0c60 65 20 68 6f 73 74 69 64 20 66 69 6c 65 20 70 61  e hostid file pa
e0c70 74 68 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ths */.SQLITE_AP
e0c80 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 68 6f  I int sqlite3_ho
e0c90 73 74 69 64 5f 6e 75 6d 20 3d 20 30 3b 0a 23 65  stid_num = 0;.#e
e0ca0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
e0cb0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
e0cc0 65 78 74 20 68 61 73 20 74 68 65 20 70 61 74 68  ext has the path
e0cd0 20 61 6e 64 20 66 69 6c 65 20 73 74 72 75 63 74   and file struct
e0ce0 75 72 65 73 20 66 6f 72 20 74 68 65 20 72 65 6d  ures for the rem
e0cf0 6f 74 65 20 0a 2a 2a 20 61 6e 64 20 6c 6f 63 61  ote .** and loca
e0d00 6c 20 70 72 6f 78 79 20 66 69 6c 65 73 20 69 6e  l proxy files in
e0d10 20 69 74 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73   it.*/.typedef s
e0d20 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69  truct proxyLocki
e0d30 6e 67 43 6f 6e 74 65 78 74 20 70 72 6f 78 79 4c  ngContext proxyL
e0d40 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73  ockingContext;.s
e0d50 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69  truct proxyLocki
e0d60 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e  ngContext {.  un
e0d70 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c  ixFile *conchFil
e0d80 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70  e;         /* Op
e0d90 65 6e 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f  en conch file */
e0da0 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 46 69  .  char *conchFi
e0db0 6c 65 50 61 74 68 3b 20 20 20 20 20 20 20 20 20  lePath;         
e0dc0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
e0dd0 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 75  onch file */.  u
e0de0 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f  nixFile *lockPro
e0df0 78 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  xy;         /* O
e0e00 70 65 6e 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66  pen proxy lock f
e0e10 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c  ile */.  char *l
e0e20 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 20 20 20  ockProxyPath;   
e0e30 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
e0e40 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20   the proxy lock 
e0e50 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  file */.  char *
e0e60 64 62 50 61 74 68 3b 20 20 20 20 20 20 20 20 20  dbPath;         
e0e70 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
e0e80 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20  f the open file 
e0e90 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 63 68 48 65  */.  int conchHe
e0ea0 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ld;             
e0eb0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
e0ec0 20 63 6f 6e 63 68 20 69 73 20 63 75 72 72 65 6e   conch is curren
e0ed0 74 6c 79 20 68 65 6c 64 20 2a 2f 0a 20 20 76 6f  tly held */.  vo
e0ee0 69 64 20 2a 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f  id *oldLockingCo
e0ef0 6e 74 65 78 74 3b 20 20 20 20 20 2f 2a 20 4f 72  ntext;     /* Or
e0f00 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 63 6f  iginal lockingco
e0f10 6e 74 65 78 74 20 74 6f 20 72 65 73 74 6f 72 65  ntext to restore
e0f20 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 73   on close */.  s
e0f30 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e0f40 73 20 63 6f 6e 73 74 20 2a 70 4f 6c 64 4d 65 74  s const *pOldMet
e0f50 68 6f 64 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67  hod;     /* Orig
e0f60 69 6e 61 6c 20 49 2f 4f 20 6d 65 74 68 6f 64 73  inal I/O methods
e0f70 20 66 6f 72 20 63 6c 6f 73 65 20 2a 2f 0a 7d 3b   for close */.};
e0f80 0a 0a 2f 2a 20 48 4f 53 54 49 44 4c 45 4e 20 61  ../* HOSTIDLEN a
e0f90 6e 64 20 43 4f 4e 43 48 4c 45 4e 20 62 6f 74 68  nd CONCHLEN both
e0fa0 20 69 6e 63 6c 75 64 65 20 73 70 61 63 65 20 66   include space f
e0fb0 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 0a 2a  or the string .*
e0fc0 2a 20 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75  * terminating nu
e0fd0 6c 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 48 4f  l .*/.#define HO
e0fe0 53 54 49 44 4c 45 4e 20 20 20 20 20 20 20 20 20  STIDLEN         
e0ff0 31 32 38 0a 23 64 65 66 69 6e 65 20 43 4f 4e 43  128.#define CONC
e1000 48 4c 45 4e 20 20 20 20 20 20 20 20 20 20 28 4d  HLEN          (M
e1010 41 58 50 41 54 48 4c 45 4e 2b 48 4f 53 54 49 44  AXPATHLEN+HOSTID
e1020 4c 45 4e 2b 31 29 0a 23 69 66 6e 64 65 66 20 48  LEN+1).#ifndef H
e1030 4f 53 54 49 44 50 41 54 48 0a 23 20 64 65 66 69  OSTIDPATH.# defi
e1040 6e 65 20 48 4f 53 54 49 44 50 41 54 48 20 20 20  ne HOSTIDPATH   
e1050 20 20 20 20 22 2f 4c 69 62 72 61 72 79 2f 43 61      "/Library/Ca
e1060 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e  ches/.com.apple.
e1070 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49  sqliteConchHostI
e1080 64 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 62 61  d".#endif../* ba
e1090 73 69 63 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f  sically a copy o
e10a0 66 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73  f unixRandomness
e10b0 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74 0a   with different.
e10c0 2a 2a 20 74 65 73 74 20 62 65 68 61 76 69 6f 72  ** test behavior
e10d0 20 62 75 69 6c 74 20 69 6e 20 2a 2f 0a 73 74 61   built in */.sta
e10e0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 6e  tic int proxyGen
e10f0 65 72 61 74 65 48 6f 73 74 49 44 28 63 68 61 72  erateHostID(char
e1100 20 2a 70 48 6f 73 74 49 44 29 7b 0a 20 20 69 6e   *pHostID){.  in
e1110 74 20 70 69 64 2c 20 66 64 2c 20 6c 65 6e 3b 0a  t pid, fd, len;.
e1120 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
e1130 2a 6b 65 79 20 3d 20 28 75 6e 73 69 67 6e 65 64  *key = (unsigned
e1140 20 63 68 61 72 20 2a 29 70 48 6f 73 74 49 44 3b   char *)pHostID;
e1150 0a 20 20 0a 20 20 6d 65 6d 73 65 74 28 6b 65 79  .  .  memset(key
e1160 2c 20 30 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b  , 0, HOSTIDLEN);
e1170 0a 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 66 64  .  len = 0;.  fd
e1180 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72   = open("/dev/ur
e1190 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59  andom", O_RDONLY
e11a0 29 3b 0a 20 20 69 66 28 20 66 64 3e 3d 30 20 29  );.  if( fd>=0 )
e11b0 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 72 65 61 64  {.    len = read
e11c0 28 66 64 2c 20 6b 65 79 2c 20 48 4f 53 54 49 44  (fd, key, HOSTID
e11d0 4c 45 4e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28  LEN);.    close(
e11e0 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79  fd); /* silently
e11f0 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20   leak the fd if 
e1200 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 7d 0a  it fails */.  }.
e1210 20 20 69 66 28 20 6c 65 6e 20 3c 20 48 4f 53 54    if( len < HOST
e1220 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 74 69 6d  IDLEN ){.    tim
e1230 65 5f 74 20 74 3b 0a 20 20 20 20 74 69 6d 65 28  e_t t;.    time(
e1240 26 74 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  &t);.    memcpy(
e1250 6b 65 79 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28  key, &t, sizeof(
e1260 74 29 29 3b 0a 20 20 20 20 70 69 64 20 3d 20 67  t));.    pid = g
e1270 65 74 70 69 64 28 29 3b 0a 20 20 20 20 6d 65 6d  etpid();.    mem
e1280 63 70 79 28 26 6b 65 79 5b 73 69 7a 65 6f 66 28  cpy(&key[sizeof(
e1290 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f  t)], &pid, sizeo
e12a0 66 28 70 69 64 29 29 3b 0a 20 20 7d 0a 20 20 0a  f(pid));.  }.  .
e12b0 23 69 66 64 65 66 20 4d 41 4b 45 5f 50 52 45 54  #ifdef MAKE_PRET
e12c0 54 59 5f 48 4f 53 54 49 44 0a 20 20 7b 0a 20 20  TY_HOSTID.  {.  
e12d0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 2f 2a 20    int i;.    /* 
e12e0 66 69 6c 74 65 72 20 74 68 65 20 62 79 74 65 73  filter the bytes
e12f0 20 69 6e 74 6f 20 70 72 69 6e 74 61 62 6c 65 20   into printable 
e1300 61 73 63 69 69 20 63 68 61 72 61 63 74 65 72 73  ascii characters
e1310 20 61 6e 64 20 4e 55 4c 20 74 65 72 6d 69 6e 61   and NUL termina
e1320 74 65 20 2a 2f 0a 20 20 20 20 6b 65 79 5b 28 48  te */.    key[(H
e1330 4f 53 54 49 44 4c 45 4e 2d 31 29 5d 20 3d 20 30  OSTIDLEN-1)] = 0
e1340 78 30 30 3b 0a 20 20 20 20 66 6f 72 28 20 69 3d  x00;.    for( i=
e1350 30 3b 20 69 3c 28 48 4f 53 54 49 44 4c 45 4e 2d  0; i<(HOSTIDLEN-
e1360 31 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20  1); i++ ){.     
e1370 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70   unsigned char p
e1380 61 20 3d 20 6b 65 79 5b 69 5d 26 30 78 37 46 3b  a = key[i]&0x7F;
e1390 0a 20 20 20 20 20 20 69 66 28 20 70 61 3c 30 78  .      if( pa<0x
e13a0 32 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65  20 ){.        ke
e13b0 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30  y[i] = (key[i]&0
e13c0 78 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70  x80 == 0x80) ? p
e13d0 61 2b 30 78 34 30 20 3a 20 70 61 2b 30 78 32 30  a+0x40 : pa+0x20
e13e0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
e13f0 28 20 70 61 3d 3d 30 78 37 46 20 29 7b 0a 20 20  ( pa==0x7F ){.  
e1400 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28        key[i] = (
e1410 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30  key[i]&0x80 == 0
e1420 78 38 30 29 20 3f 20 70 61 3d 30 78 32 30 20 3a  x80) ? pa=0x20 :
e1430 20 70 61 2b 30 78 37 45 3b 0a 20 20 20 20 20 20   pa+0x7E;.      
e1440 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  }.    }.  }.#end
e1450 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  if.  return SQLI
e1460 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 77 72 69  TE_OK;.}../* wri
e1470 74 65 73 20 74 68 65 20 68 6f 73 74 20 69 64 20  tes the host id 
e1480 70 61 74 68 20 74 6f 20 70 61 74 68 2c 20 70 61  path to path, pa
e1490 74 68 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20  th should be an 
e14a0 70 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 62 75  pre-allocated bu
e14b0 66 66 65 72 0a 2a 2a 20 77 69 74 68 20 65 6e 6f  ffer.** with eno
e14c0 75 67 68 20 73 70 61 63 65 20 66 6f 72 20 61 20  ugh space for a 
e14d0 70 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20  path .*/.static 
e14e0 76 6f 69 64 20 70 72 6f 78 79 47 65 74 48 6f 73  void proxyGetHos
e14f0 74 49 44 50 61 74 68 28 63 68 61 72 20 2a 70 61  tIDPath(char *pa
e1500 74 68 2c 20 73 69 7a 65 5f 74 20 6c 65 6e 29 7b  th, size_t len){
e1510 0a 20 20 73 74 72 6c 63 70 79 28 70 61 74 68 2c  .  strlcpy(path,
e1520 20 48 4f 53 54 49 44 50 41 54 48 2c 20 6c 65 6e   HOSTIDPATH, len
e1530 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
e1540 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69  _TEST.  if( sqli
e1550 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3e 30  te3_hostid_num>0
e1560 20 29 7b 0a 20 20 20 20 63 68 61 72 20 73 75 66   ){.    char suf
e1570 66 69 78 5b 32 5d 20 3d 20 22 31 22 3b 0a 20 20  fix[2] = "1";.  
e1580 20 20 73 75 66 66 69 78 5b 30 5d 20 3d 20 73 75    suffix[0] = su
e1590 66 66 69 78 5b 30 5d 20 2b 20 73 71 6c 69 74 65  ffix[0] + sqlite
e15a0 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3b 0a 20 20  3_hostid_num;.  
e15b0 20 20 73 74 72 6c 63 61 74 28 70 61 74 68 2c 20    strlcat(path, 
e15c0 73 75 66 66 69 78 2c 20 6c 65 6e 29 3b 0a 20 20  suffix, len);.  
e15d0 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 53 54 52 41  }.#endif.  OSTRA
e15e0 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 50 41  CE3("GETHOSTIDPA
e15f0 54 48 20 20 25 73 20 70 69 64 3d 25 64 5c 6e 22  TH  %s pid=%d\n"
e1600 2c 20 70 61 74 68 2c 20 67 65 74 70 69 64 28 29  , path, getpid()
e1610 29 3b 0a 7d 0a 0a 2f 2a 20 67 65 74 20 74 68 65  );.}../* get the
e1620 20 68 6f 73 74 20 49 44 20 66 72 6f 6d 20 61 20   host ID from a 
e1630 73 71 6c 69 74 65 20 68 6f 73 74 69 64 20 66 69  sqlite hostid fi
e1640 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  le stored in the
e1650 20 0a 2a 2a 20 75 73 65 72 2d 73 70 65 63 69 66   .** user-specif
e1660 69 63 20 74 6d 70 20 64 69 72 65 63 74 6f 72 79  ic tmp directory
e1670 2c 20 63 72 65 61 74 65 20 74 68 65 20 49 44 20  , create the ID 
e1680 69 66 20 69 74 27 73 20 6e 6f 74 20 74 68 65 72  if it's not ther
e1690 65 20 61 6c 72 65 61 64 79 20 0a 2a 2f 0a 73 74  e already .*/.st
e16a0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65  atic int proxyGe
e16b0 74 48 6f 73 74 49 44 28 63 68 61 72 20 2a 70 48  tHostID(char *pH
e16c0 6f 73 74 49 44 2c 20 69 6e 74 20 2a 70 45 72 72  ostID, int *pErr
e16d0 6f 72 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20  or){.  int fd;. 
e16e0 20 63 68 61 72 20 70 61 74 68 5b 4d 41 58 50 41   char path[MAXPA
e16f0 54 48 4c 45 4e 5d 3b 20 0a 20 20 73 69 7a 65 5f  THLEN]; .  size_
e1700 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3d  t len;.  int rc=
e1710 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 70 72  SQLITE_OK;..  pr
e1720 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 74 68  oxyGetHostIDPath
e1730 28 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45  (path, MAXPATHLE
e1740 4e 29 3b 0a 20 20 2f 2a 20 74 72 79 20 74 6f 20  N);.  /* try to 
e1750 63 72 65 61 74 65 20 74 68 65 20 68 6f 73 74 20  create the host 
e1760 49 44 20 66 69 6c 65 2c 20 69 66 20 69 74 20 61  ID file, if it a
e1770 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 72 65  lready exists re
e1780 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ad the contents 
e1790 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 70  */.  fd = open(p
e17a0 61 74 68 2c 20 4f 5f 43 52 45 41 54 7c 4f 5f 57  ath, O_CREAT|O_W
e17b0 52 4f 4e 4c 59 7c 4f 5f 45 58 43 4c 2c 20 30 36  RONLY|O_EXCL, 06
e17c0 34 34 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20  44);.  if( fd<0 
e17d0 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3d 65  ){.    int err=e
e17e0 72 72 6e 6f 3b 0a 09 09 0a 20 20 20 20 69 66 28  rrno;....    if(
e17f0 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b 0a   err!=EEXIST ){.
e1800 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52  #ifdef SQLITE_PR
e1810 4f 58 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74  OXY_DEBUG /* set
e1820 20 74 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f   the sqlite erro
e1830 72 20 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61  r message instea
e1840 64 20 2a 2f 0a 20 20 20 20 20 20 66 70 72 69 6e  d */.      fprin
e1850 74 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69  tf(stderr, "sqli
e1860 74 65 20 65 72 72 6f 72 20 63 72 65 61 74 69 6e  te error creatin
e1870 67 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25  g host ID file %
e1880 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  s: %s\n",.      
e1890 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 73 74          path, st
e18a0 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65  rerror(err));.#e
e18b0 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74 75 72  ndif.      retur
e18c0 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20  n SQLITE_PERM;. 
e18d0 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 75 6c     }.    /* coul
e18e0 64 6e 27 74 20 63 72 65 61 74 65 20 74 68 65 20  dn't create the 
e18f0 66 69 6c 65 2c 20 72 65 61 64 20 69 74 20 69 6e  file, read it in
e1900 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 66 64 20  stead */.    fd 
e1910 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 52  = open(path, O_R
e1920 44 4f 4e 4c 59 7c 4f 5f 45 58 43 4c 29 3b 0a 20  DONLY|O_EXCL);. 
e1930 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 23     if( fd<0 ){.#
e1940 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f  ifdef SQLITE_PRO
e1950 58 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 20  XY_DEBUG /* set 
e1960 74 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f 72  the sqlite error
e1970 20 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61 64   message instead
e1980 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 72   */.      int er
e1990 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  r = errno;.     
e19a0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
e19b0 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6f   "sqlite error o
e19c0 70 65 6e 69 6e 67 20 68 6f 73 74 20 49 44 20 66  pening host ID f
e19d0 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20  ile %s: %s\n",. 
e19e0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74               pat
e19f0 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29  h, strerror(err)
e1a00 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
e1a10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45  return SQLITE_PE
e1a20 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65  RM;.    }.    le
e1a30 6e 20 3d 20 70 72 65 61 64 28 66 64 2c 20 70 48  n = pread(fd, pH
e1a40 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e  ostID, HOSTIDLEN
e1a50 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65  , 0);.    if( le
e1a60 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45  n<0 ){.      *pE
e1a70 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rror = errno;.  
e1a80 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e1a90 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 20 20  IOERR_READ;.    
e1aa0 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f  }else if( len<HO
e1ab0 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20  STIDLEN ){.     
e1ac0 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20   *pError = 0;.  
e1ad0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e1ae0 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
e1af0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73  ;.    }.    clos
e1b00 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74  e(fd); /* silent
e1b10 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69  ly leak the fd i
e1b20 66 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20  f it fails */.  
e1b30 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48    OSTRACE3("GETH
e1b40 4f 53 54 49 44 20 20 72 65 61 64 20 25 73 20 70  OSTID  read %s p
e1b50 69 64 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49  id=%d\n", pHostI
e1b60 44 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20  D, getpid());.  
e1b70 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
e1b80 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 77 65 27  else{.    /* we'
e1b90 72 65 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  re creating the 
e1ba0 68 6f 73 74 20 49 44 20 66 69 6c 65 20 28 75 73  host ID file (us
e1bb0 65 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e  e a random strin
e1bc0 67 20 6f 66 20 62 79 74 65 73 29 20 2a 2f 0a 20  g of bytes) */. 
e1bd0 20 20 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65     proxyGenerate
e1be0 48 6f 73 74 49 44 28 70 48 6f 73 74 49 44 29 3b  HostID(pHostID);
e1bf0 0a 20 20 20 20 6c 65 6e 20 3d 20 70 77 72 69 74  .    len = pwrit
e1c00 65 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48  e(fd, pHostID, H
e1c10 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20  OSTIDLEN, 0);.  
e1c20 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20    if( len<0 ){. 
e1c30 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65       *pError = e
e1c40 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d  rrno;.      rc =
e1c50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52   SQLITE_IOERR_WR
e1c60 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ITE;.    }else i
e1c70 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e  f( len<HOSTIDLEN
e1c80 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f   ){.      *pErro
e1c90 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  r = 0;.      rc 
e1ca0 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  = SQLITE_IOERR_W
e1cb0 52 49 54 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20  RITE;.    }.    
e1cc0 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69  close(fd); /* si
e1cd0 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20  lently leak the 
e1ce0 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a  fd if it fails *
e1cf0 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22  /.    OSTRACE3("
e1d00 47 45 54 48 4f 53 54 49 44 20 20 77 72 6f 74 65  GETHOSTID  wrote
e1d10 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %s pid=%d\n", p
e1d20 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 29  HostID, getpid()
e1d30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
e1d40 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20  ;.  }.}..static 
e1d50 69 6e 74 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b  int proxyGetLock
e1d60 50 61 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20  Path(const char 
e1d70 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a 6c  *dbPath, char *l
e1d80 50 61 74 68 2c 20 73 69 7a 65 5f 74 20 6d 61 78  Path, size_t max
e1d90 4c 65 6e 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b  Len){.  int len;
e1da0 0a 20 20 69 6e 74 20 64 62 4c 65 6e 3b 0a 20 20  .  int dbLen;.  
e1db0 69 6e 74 20 69 3b 0a 0a 23 69 66 64 65 66 20 4c  int i;..#ifdef L
e1dc0 4f 43 4b 50 52 4f 58 59 44 49 52 0a 20 20 6c 65  OCKPROXYDIR.  le
e1dd0 6e 20 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74  n = strlcpy(lPat
e1de0 68 2c 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 2c  h, LOCKPROXYDIR,
e1df0 20 6d 61 78 4c 65 6e 29 3b 0a 23 65 6c 73 65 0a   maxLen);.#else.
e1e00 23 20 69 66 64 65 66 20 5f 43 53 5f 44 41 52 57  # ifdef _CS_DARW
e1e10 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52  IN_USER_TEMP_DIR
e1e20 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 66 73 74 72  .  {.    confstr
e1e30 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52  (_CS_DARWIN_USER
e1e40 5f 54 45 4d 50 5f 44 49 52 2c 20 6c 50 61 74 68  _TEMP_DIR, lPath
e1e50 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 20 6c  , maxLen);.    l
e1e60 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61  en = strlcat(lPa
e1e70 74 68 2c 20 22 73 71 6c 69 74 65 70 6c 6f 63 6b  th, "sqliteplock
e1e80 73 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20  s", maxLen);.   
e1e90 20 69 66 28 20 6d 6b 64 69 72 28 6c 50 61 74 68   if( mkdir(lPath
e1ea0 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  , SQLITE_DEFAULT
e1eb0 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53  _PROXYDIR_PERMIS
e1ec0 53 49 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20  SIONS) ){.      
e1ed0 2f 2a 20 69 66 20 6d 6b 64 69 72 20 66 61 69 6c  /* if mkdir fail
e1ee0 73 2c 20 68 61 6e 64 6c 65 20 61 73 20 6c 6f 63  s, handle as loc
e1ef0 6b 20 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20  k file creation 
e1f00 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 20 20 20  failure */.     
e1f10 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f   int err = errno
e1f20 3b 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54  ;.#  ifdef SQLIT
e1f30 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66  E_DEBUG.      if
e1f40 28 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b  ( err!=EEXIST ){
e1f50 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66  .        fprintf
e1f60 28 73 74 64 65 72 72 2c 20 22 70 72 6f 78 79 47  (stderr, "proxyG
e1f70 65 74 4c 6f 63 6b 50 61 74 68 3a 20 6d 6b 64 69  etLockPath: mkdi
e1f80 72 28 25 73 2c 30 25 6f 29 20 65 72 72 6f 72 20  r(%s,0%o) error 
e1f90 25 64 20 25 73 5c 6e 22 2c 20 6c 50 61 74 68 2c  %d %s\n", lPath,
e1fa0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
e1fb0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
e1fc0 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53  PROXYDIR_PERMISS
e1fd0 49 4f 4e 53 2c 20 65 72 72 2c 20 73 74 72 65 72  IONS, err, strer
e1fe0 72 6f 72 28 65 72 72 29 29 3b 0a 20 20 20 20 20  ror(err));.     
e1ff0 20 7d 0a 23 20 20 65 6e 64 69 66 0a 20 20 20 20   }.#  endif.    
e2000 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54  }else{.      OST
e2010 52 41 43 45 33 28 22 47 45 54 4c 4f 43 4b 50 41  RACE3("GETLOCKPA
e2020 54 48 20 20 6d 6b 64 69 72 20 25 73 20 70 69 64  TH  mkdir %s pid
e2030 3d 25 64 5c 6e 22 2c 20 6c 50 61 74 68 2c 20 67  =%d\n", lPath, g
e2040 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 7d 0a  etpid());.    }.
e2050 20 20 20 20 0a 20 20 7d 0a 23 20 65 6c 73 65 0a      .  }.# else.
e2060 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28    len = strlcpy(
e2070 6c 50 61 74 68 2c 20 22 2f 74 6d 70 2f 22 2c 20  lPath, "/tmp/", 
e2080 6d 61 78 4c 65 6e 29 3b 0a 23 20 65 6e 64 69 66  maxLen);.# endif
e2090 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6c  .#endif..  if( l
e20a0 50 61 74 68 5b 6c 65 6e 2d 31 5d 21 3d 27 2f 27  Path[len-1]!='/'
e20b0 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74   ){.    len = st
e20c0 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 2f 22  rlcat(lPath, "/"
e20d0 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 7d 0a 20  , maxLen);.  }. 
e20e0 20 0a 20 20 2f 2a 20 74 72 61 6e 73 66 6f 72 6d   .  /* transform
e20f0 20 74 68 65 20 64 62 20 70 61 74 68 20 74 6f 20   the db path to 
e2100 61 20 75 6e 69 71 75 65 20 63 61 63 68 65 20 6e  a unique cache n
e2110 61 6d 65 20 2a 2f 0a 20 20 64 62 4c 65 6e 20 3d  ame */.  dbLen =
e2120 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50   (int)strlen(dbP
e2130 61 74 68 29 3b 0a 20 20 66 6f 72 28 20 69 3d 30  ath);.  for( i=0
e2140 3b 20 69 3c 64 62 4c 65 6e 20 26 26 20 28 69 2b  ; i<dbLen && (i+
e2150 6c 65 6e 2b 37 29 3c 6d 61 78 4c 65 6e 3b 20 69  len+7)<maxLen; i
e2160 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 63 20  ++){.    char c 
e2170 3d 20 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20  = dbPath[i];.   
e2180 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d 20 3d 20   lPath[i+len] = 
e2190 28 63 3d 3d 27 2f 27 29 3f 27 5f 27 3a 63 3b 0a  (c=='/')?'_':c;.
e21a0 20 20 7d 0a 20 20 6c 50 61 74 68 5b 69 2b 6c 65    }.  lPath[i+le
e21b0 6e 5d 3d 27 5c 30 27 3b 0a 20 20 73 74 72 6c 63  n]='\0';.  strlc
e21c0 61 74 28 6c 50 61 74 68 2c 20 22 3a 61 75 74 6f  at(lPath, ":auto
e21d0 3a 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 72  :", maxLen);.  r
e21e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e21f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
e2200 20 61 20 6e 65 77 20 56 46 53 20 66 69 6c 65 20   a new VFS file 
e2210 64 65 73 63 72 69 70 74 6f 72 20 28 73 74 6f 72  descriptor (stor
e2220 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  ed in memory obt
e2230 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71  ained from.** sq
e2240 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 29 20 61 6e  lite3_malloc) an
e2250 64 20 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  d open the file 
e2260 6e 61 6d 65 64 20 22 70 61 74 68 22 20 69 6e 20  named "path" in 
e2270 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
e2280 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  tor..**.** The c
e2290 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73  aller is respons
e22a0 69 62 6c 65 20 6e 6f 74 20 6f 6e 6c 79 20 66 6f  ible not only fo
e22b0 72 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 66 69  r closing the fi
e22c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a  le descriptor.**
e22d0 20 62 75 74 20 61 6c 73 6f 20 66 6f 72 20 66 72   but also for fr
e22e0 65 65 69 6e 67 20 74 68 65 20 6d 65 6d 6f 72 79  eeing the memory
e22f0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
e2300 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
e2310 70 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ptor..*/.static 
e2320 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 55  int proxyCreateU
e2330 6e 69 78 46 69 6c 65 28 63 6f 6e 73 74 20 63 68  nixFile(const ch
e2340 61 72 20 2a 70 61 74 68 2c 20 75 6e 69 78 46 69  ar *path, unixFi
e2350 6c 65 20 2a 2a 70 70 46 69 6c 65 29 20 7b 0a 20  le **ppFile) {. 
e2360 20 69 6e 74 20 66 64 3b 0a 20 20 69 6e 74 20 64   int fd;.  int d
e2370 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 75 6e 69  irfd = -1;.  uni
e2380 78 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69  xFile *pNew;.  i
e2390 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
e23a0 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  K;.  sqlite3_vfs
e23b0 20 64 75 6d 6d 79 56 66 73 3b 0a 0a 20 20 66 64   dummyVfs;..  fd
e23c0 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f   = open(path, O_
e23d0 52 44 57 52 20 7c 20 4f 5f 43 52 45 41 54 2c 20  RDWR | O_CREAT, 
e23e0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
e23f0 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29  ILE_PERMISSIONS)
e2400 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a  ;.  if( fd<0 ){.
e2410 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e2420 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a  E_CANTOPEN;.  }.
e2430 20 20 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69    .  pNew = (uni
e2440 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f  xFile *)sqlite3_
e2450 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e  malloc(sizeof(un
e2460 69 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20  ixFile));.  if( 
e2470 70 4e 65 77 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20  pNew==NULL ){.  
e2480 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
e2490 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e  MEM;.    goto en
e24a0 64 5f 63 72 65 61 74 65 5f 70 72 6f 78 79 3b 0a  d_create_proxy;.
e24b0 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65    }.  memset(pNe
e24c0 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69  w, 0, sizeof(uni
e24d0 78 46 69 6c 65 29 29 3b 0a 0a 20 20 64 75 6d 6d  xFile));..  dumm
e24e0 79 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20  yVfs.pAppData = 
e24f0 28 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b  (void*)&autolock
e2500 49 6f 46 69 6e 64 65 72 3b 0a 20 20 72 63 20 3d  IoFinder;.  rc =
e2510 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28   fillInUnixFile(
e2520 26 64 75 6d 6d 79 56 66 73 2c 20 66 64 2c 20 64  &dummyVfs, fd, d
e2530 69 72 66 64 2c 20 28 73 71 6c 69 74 65 33 5f 66  irfd, (sqlite3_f
e2540 69 6c 65 2a 29 70 4e 65 77 2c 20 70 61 74 68 2c  ile*)pNew, path,
e2550 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63   0, 0);.  if( rc
e2560 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
e2570 20 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65     *ppFile = pNe
e2580 77 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  w;.    return SQ
e2590 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 65 6e 64  LITE_OK;.  }.end
e25a0 5f 63 72 65 61 74 65 5f 70 72 6f 78 79 3a 20 20  _create_proxy:  
e25b0 20 20 0a 20 20 63 6c 6f 73 65 28 66 64 29 3b 20    .  close(fd); 
e25c0 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b  /* silently leak
e25d0 20 66 64 20 69 66 20 65 72 72 6f 72 2c 20 77 65   fd if error, we
e25e0 27 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20 65  're already in e
e25f0 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rror */.  sqlite
e2600 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20  3_free(pNew);.  
e2610 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
e2620 20 74 61 6b 65 73 20 74 68 65 20 63 6f 6e 63 68   takes the conch
e2630 20 62 79 20 74 61 6b 69 6e 67 20 61 20 73 68 61   by taking a sha
e2640 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61  red lock and rea
e2650 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 63  d the contents c
e2660 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a 20 6c 6f 63  onch, if .** loc
e2670 6b 50 61 74 68 20 69 73 20 6e 6f 6e 2d 4e 55 4c  kPath is non-NUL
e2680 4c 2c 20 74 68 65 20 68 6f 73 74 20 49 44 20 61  L, the host ID a
e2690 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74  nd lock file pat
e26a0 68 20 6d 75 73 74 20 6d 61 74 63 68 2e 20 20 41  h must match.  A
e26b0 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f 63 6b 50 61   NULL .** lockPa
e26c0 74 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  th means that th
e26d0 65 20 6c 6f 63 6b 50 61 74 68 20 69 6e 20 74 68  e lockPath in th
e26e0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 6c  e conch file wil
e26f0 6c 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65  l be used if the
e2700 20 0a 2a 2a 20 68 6f 73 74 20 49 44 73 20 6d 61   .** host IDs ma
e2710 74 63 68 2c 20 6f 72 20 61 20 6e 65 77 20 6c 6f  tch, or a new lo
e2720 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20  ck path will be 
e2730 67 65 6e 65 72 61 74 65 64 20 61 75 74 6f 6d 61  generated automa
e2740 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 61 6e 64 20  tically .** and 
e2750 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 63  written to the c
e2760 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  onch file..*/.st
e2770 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 61  atic int proxyTa
e2780 6b 65 43 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65  keConch(unixFile
e2790 20 2a 70 46 69 6c 65 29 7b 0a 20 20 70 72 6f 78   *pFile){.  prox
e27a0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
e27b0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
e27c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70  ckingContext *)p
e27d0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
e27e0 74 65 78 74 3b 20 0a 20 20 0a 20 20 69 66 28 20  text; .  .  if( 
e27f0 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 3e  pCtx->conchHeld>
e2800 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
e2810 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
e2820 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65  se{.    unixFile
e2830 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43   *conchFile = pC
e2840 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20  tx->conchFile;. 
e2850 20 20 20 63 68 61 72 20 74 65 73 74 56 61 6c 75     char testValu
e2860 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20  e[CONCHLEN];.   
e2870 20 63 68 61 72 20 63 6f 6e 63 68 56 61 6c 75 65   char conchValue
e2880 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 20  [CONCHLEN];.    
e2890 63 68 61 72 20 6c 6f 63 6b 50 61 74 68 5b 4d 41  char lockPath[MA
e28a0 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20 20 20 63  XPATHLEN];.    c
e28b0 68 61 72 20 2a 74 4c 6f 63 6b 50 61 74 68 20 3d  har *tLockPath =
e28c0 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72   NULL;.    int r
e28d0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
e28e0 20 20 20 69 6e 74 20 72 65 61 64 52 63 20 3d 20     int readRc = 
e28f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69  SQLITE_OK;.    i
e2900 6e 74 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 30  nt syncPerms = 0
e2910 3b 0a 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28  ;..    OSTRACE4(
e2920 22 54 41 4b 45 43 4f 4e 43 48 20 20 25 64 20 66  "TAKECONCH  %d f
e2930 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c  or %s pid=%d\n",
e2940 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20   conchFile->h,. 
e2950 20 20 20 20 20 20 20 20 20 20 20 20 28 70 43 74              (pCt
e2960 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
e2970 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f   ? pCtx->lockPro
e2980 78 79 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a  xyPath : ":auto:
e2990 22 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a  "), getpid());..
e29a0 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69      rc = conchFi
e29b0 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f  le->pMethod->xLo
e29c0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
e29d0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41  *)conchFile, SHA
e29e0 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69  RED_LOCK);.    i
e29f0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
e2a00 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 45   ){.      int pE
e2a10 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  rror = 0;.      
e2a20 6d 65 6d 73 65 74 28 74 65 73 74 56 61 6c 75 65  memset(testValue
e2a30 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 4e 29 3b 20  , 0, CONCHLEN); 
e2a40 2f 2a 20 63 6f 6e 63 68 20 69 73 20 66 69 78 65  /* conch is fixe
e2a50 64 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20  d size */.      
e2a60 72 63 20 3d 20 70 72 6f 78 79 47 65 74 48 6f 73  rc = proxyGetHos
e2a70 74 49 44 28 74 65 73 74 56 61 6c 75 65 2c 20 26  tID(testValue, &
e2a80 70 45 72 72 6f 72 29 3b 0a 20 20 20 20 20 20 69  pError);.      i
e2a90 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51  f( (rc&0xff)==SQ
e2aa0 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20  LITE_IOERR ){.  
e2ab0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
e2ac0 74 45 72 72 6e 6f 20 3d 20 70 45 72 72 6f 72 3b  tErrno = pError;
e2ad0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
e2ae0 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f  f( pCtx->lockPro
e2af0 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20  xyPath ){.      
e2b00 20 20 73 74 72 6c 63 70 79 28 26 74 65 73 74 56    strlcpy(&testV
e2b10 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c  alue[HOSTIDLEN],
e2b20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
e2b30 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e  Path, MAXPATHLEN
e2b40 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
e2b50 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
e2b60 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
e2b70 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e  goto end_takecon
e2b80 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20  ch;.    }.    . 
e2b90 20 20 20 72 65 61 64 52 63 20 3d 20 75 6e 69 78     readRc = unix
e2ba0 52 65 61 64 28 28 73 71 6c 69 74 65 33 5f 66 69  Read((sqlite3_fi
e2bb0 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20  le *)conchFile, 
e2bc0 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43  conchValue, CONC
e2bd0 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66  HLEN, 0);.    if
e2be0 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 45  ( readRc!=SQLITE
e2bf0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
e2c00 44 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72  D ){.      if( r
e2c10 65 61 64 52 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  eadRc!=SQLITE_OK
e2c20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
e2c30 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54  (rc&0xff)==SQLIT
e2c40 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 20  E_IOERR ){.     
e2c50 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
e2c60 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 68 46 69 6c  Errno = conchFil
e2c70 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20  e->lastErrno;.  
e2c80 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
e2c90 72 63 20 3d 20 72 65 61 64 52 63 3b 0a 20 20 20  rc = readRc;.   
e2ca0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61       goto end_ta
e2cb0 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 7d  keconch;.      }
e2cc0 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 74 68 65  .      /* if the
e2cd0 20 63 6f 6e 63 68 20 68 61 73 20 64 61 74 61 20   conch has data 
e2ce0 63 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74  compare the cont
e2cf0 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 66  ents */.      if
e2d00 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f  ( !pCtx->lockPro
e2d10 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20  xyPath ){.      
e2d20 20 20 2f 2a 20 66 6f 72 20 61 75 74 6f 2d 6e 61    /* for auto-na
e2d30 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66  med local lock f
e2d40 69 6c 65 2c 20 6a 75 73 74 20 63 68 65 63 6b 20  ile, just check 
e2d50 74 68 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20  the host ID and 
e2d60 77 65 27 6c 6c 0a 20 20 20 20 20 20 20 20 20 2a  we'll.         *
e2d70 2a 20 75 73 65 20 74 68 65 20 6c 6f 63 61 6c 20  * use the local 
e2d80 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 20 74  lock file path t
e2d90 68 61 74 27 73 20 61 6c 72 65 61 64 79 20 69 6e  hat's already in
e2da0 20 74 68 65 72 65 20 2a 2f 0a 20 20 20 20 20 20   there */.      
e2db0 20 20 69 66 28 20 21 6d 65 6d 63 6d 70 28 74 65    if( !memcmp(te
e2dc0 73 74 56 61 6c 75 65 2c 20 63 6f 6e 63 68 56 61  stValue, conchVa
e2dd0 6c 75 65 2c 20 48 4f 53 54 49 44 4c 45 4e 29 20  lue, HOSTIDLEN) 
e2de0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 4c 6f  ){.          tLo
e2df0 63 6b 50 61 74 68 20 3d 20 28 63 68 61 72 20 2a  ckPath = (char *
e2e00 29 26 63 6f 6e 63 68 56 61 6c 75 65 5b 48 4f 53  )&conchValue[HOS
e2e10 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 20 20 20 20  TIDLEN];.       
e2e20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65     goto end_take
e2e30 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d  conch;.        }
e2e40 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
e2e50 20 20 20 20 20 20 2f 2a 20 77 65 27 76 65 20 67        /* we've g
e2e60 6f 74 20 74 68 65 20 63 6f 6e 63 68 20 69 66 20  ot the conch if 
e2e70 63 6f 6e 63 68 56 61 6c 75 65 20 6d 61 74 63 68  conchValue match
e2e80 65 73 20 6f 75 72 20 70 61 74 68 20 61 6e 64 20  es our path and 
e2e90 68 6f 73 74 20 49 44 20 2a 2f 0a 20 20 20 20 20  host ID */.     
e2ea0 20 20 20 69 66 28 20 21 6d 65 6d 63 6d 70 28 74     if( !memcmp(t
e2eb0 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e 63 68 56  estValue, conchV
e2ec0 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 29 20  alue, CONCHLEN) 
e2ed0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
e2ee0 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b  o end_takeconch;
e2ef0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
e2f00 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
e2f10 20 20 20 20 2f 2a 20 61 20 73 68 6f 72 74 20 72      /* a short r
e2f20 65 61 64 20 6d 65 61 6e 73 20 77 65 27 72 65 20  ead means we're 
e2f30 22 63 72 65 61 74 69 6e 67 22 20 74 68 65 20 63  "creating" the c
e2f40 6f 6e 63 68 20 28 65 76 65 6e 20 74 68 6f 75 67  onch (even thoug
e2f50 68 20 69 74 20 63 6f 75 6c 64 20 0a 20 20 20 20  h it could .    
e2f60 20 20 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 75    ** have been u
e2f70 73 65 72 2d 69 6e 74 65 72 76 65 6e 74 69 6f 6e  ser-intervention
e2f80 29 2c 20 69 66 20 77 65 20 61 63 71 75 69 72 65  ), if we acquire
e2f90 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c   the exclusive l
e2fa0 6f 63 6b 2c 0a 20 20 20 20 20 20 2a 2a 20 77 65  ock,.      ** we
e2fb0 27 6c 6c 20 74 72 79 20 74 6f 20 6d 61 74 63 68  'll try to match
e2fc0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 6e 2d   the current on-
e2fd0 64 69 73 6b 20 70 65 72 6d 69 73 73 69 6f 6e 73  disk permissions
e2fe0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
e2ff0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
e3000 73 79 6e 63 50 65 72 6d 73 20 3d 20 31 3b 0a 20  syncPerms = 1;. 
e3010 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a     }.    .    /*
e3020 20 65 69 74 68 65 72 20 63 6f 6e 63 68 20 77 61   either conch wa
e3030 73 20 65 6d 74 70 79 20 6f 72 20 64 69 64 6e 27  s emtpy or didn'
e3040 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 69  t match */.    i
e3050 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  f( !pCtx->lockPr
e3060 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20  oxyPath ){.     
e3070 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74   proxyGetLockPat
e3080 68 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20  h(pCtx->dbPath, 
e3090 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 50 41 54  lockPath, MAXPAT
e30a0 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 74 4c 6f  HLEN);.      tLo
e30b0 63 6b 50 61 74 68 20 3d 20 6c 6f 63 6b 50 61 74  ckPath = lockPat
e30c0 68 3b 0a 20 20 20 20 20 20 73 74 72 6c 63 70 79  h;.      strlcpy
e30d0 28 26 74 65 73 74 56 61 6c 75 65 5b 48 4f 53 54  (&testValue[HOST
e30e0 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b 50 61 74 68  IDLEN], lockPath
e30f0 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20  , MAXPATHLEN);. 
e3100 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a     }.    .    /*
e3110 20 75 70 64 61 74 65 20 63 6f 6e 63 68 20 77 69   update conch wi
e3120 74 68 20 68 6f 73 74 20 61 6e 64 20 70 61 74 68  th host and path
e3130 20 28 74 68 69 73 20 77 69 6c 6c 20 66 61 69 6c   (this will fail
e3140 20 69 66 20 6f 74 68 65 72 20 70 72 6f 63 65 73   if other proces
e3150 73 0a 20 20 20 20 20 2a 2a 20 68 61 73 20 61 20  s.     ** has a 
e3160 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6c 72 65  shared lock alre
e3170 61 64 79 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d  ady) */.    rc =
e3180 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74   conchFile->pMet
e3190 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69  hod->xLock((sqli
e31a0 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46  te3_file*)conchF
e31b0 69 6c 65 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c  ile, EXCLUSIVE_L
e31c0 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63  OCK);.    if( rc
e31d0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
e31e0 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 57 72       rc = unixWr
e31f0 69 74 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ite((sqlite3_fil
e3200 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 74  e *)conchFile, t
e3210 65 73 74 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c  estValue, CONCHL
e3220 45 4e 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  EN, 0);.      if
e3230 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
e3240 26 26 20 73 79 6e 63 50 65 72 6d 73 20 29 7b 0a  && syncPerms ){.
e3250 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 73          struct s
e3260 74 61 74 20 62 75 66 3b 0a 20 20 20 20 20 20 20  tat buf;.       
e3270 20 69 6e 74 20 65 72 72 20 3d 20 66 73 74 61 74   int err = fstat
e3280 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62 75 66 29  (pFile->h, &buf)
e3290 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 72  ;.        if( er
e32a0 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r==0 ){.        
e32b0 20 20 2f 2a 20 74 72 79 20 74 6f 20 6d 61 74 63    /* try to matc
e32c0 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  h the database f
e32d0 69 6c 65 20 70 65 72 6d 69 73 73 69 6f 6e 73 2c  ile permissions,
e32e0 20 69 67 6e 6f 72 65 20 66 61 69 6c 75 72 65 20   ignore failure 
e32f0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
e3300 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 0a 20 20  E_PROXY_DEBUG.  
e3310 20 20 20 20 20 20 20 20 66 63 68 6d 6f 64 28 63          fchmod(c
e3320 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66  onchFile->h, buf
e3330 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 65 6c 73 65  .st_mode);.#else
e3340 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66  .          if( f
e3350 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d  chmod(conchFile-
e3360 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29  >h, buf.st_mode)
e3370 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
e3380 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 65 72     int code = er
e3390 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20  rno;.           
e33a0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
e33b0 20 22 66 63 68 6d 6f 64 20 25 6f 20 46 41 49 4c   "fchmod %o FAIL
e33c0 45 44 20 77 69 74 68 20 25 64 20 25 73 5c 6e 22  ED with %d %s\n"
e33d0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
e33e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
e33f0 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 63 6f 64 65  uf.st_mode, code
e3400 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29  , strerror(code)
e3410 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20 65  );.          } e
e3420 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 20  lse {.          
e3430 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
e3440 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 53 55 43  , "fchmod %o SUC
e3450 43 45 44 45 44 5c 6e 22 2c 62 75 66 2e 73 74 5f  CEDED\n",buf.st_
e3460 6d 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 20  mode);.         
e3470 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
e3480 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
e3490 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  code = errno;.  
e34a0 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
e34b0 73 74 64 65 72 72 2c 20 22 53 54 41 54 20 46 41  stderr, "STAT FA
e34c0 49 4c 45 44 5b 25 64 5d 20 77 69 74 68 20 25 64  ILED[%d] with %d
e34d0 20 25 73 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20   %s\n", .       
e34e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e34f0 20 20 20 65 72 72 2c 20 63 6f 64 65 2c 20 73 74     err, code, st
e3500 72 65 72 72 6f 72 28 63 6f 64 65 29 29 3b 0a 23  rerror(code));.#
e3510 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7d 0a  endif.        }.
e3520 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
e3530 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65    conchFile->pMe
e3540 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73  thod->xUnlock((s
e3550 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
e3560 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c  chFile, SHARED_L
e3570 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 5f 74 61 6b  OCK);.  .end_tak
e3580 65 63 6f 6e 63 68 3a 0a 20 20 20 20 4f 53 54 52  econch:.    OSTR
e3590 41 43 45 32 28 22 54 52 41 4e 53 50 52 4f 58 59  ACE2("TRANSPROXY
e35a0 3a 20 43 4c 4f 53 45 20 20 25 64 5c 6e 22 2c 20  : CLOSE  %d\n", 
e35b0 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 69  pFile->h);.    i
e35c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
e35d0 20 26 26 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46   && pFile->openF
e35e0 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 69 66  lags ){.      if
e35f0 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b  ( pFile->h>=0 ){
e3600 0a 23 69 66 64 65 66 20 53 54 52 49 43 54 5f 43  .#ifdef STRICT_C
e3610 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 20 20 20 20  LOSE_ERROR.     
e3620 20 20 20 69 66 28 20 63 6c 6f 73 65 28 70 46 69     if( close(pFi
e3630 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 20 20  le->h) ){.      
e3640 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
e3650 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
e3660 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
e3670 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53  QLITE_IOERR_CLOS
e3680 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6c  E;.        }.#el
e3690 73 65 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65  se.        close
e36a0 28 70 46 69 6c 65 2d 3e 68 29 3b 20 2f 2a 20 73  (pFile->h); /* s
e36b0 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 66 64 20  ilently leak fd 
e36c0 69 66 20 66 61 69 6c 20 2a 2f 0a 23 65 6e 64 69  if fail */.#endi
e36d0 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  f.      }.      
e36e0 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20  pFile->h = -1;. 
e36f0 20 20 20 20 20 69 6e 74 20 66 64 20 3d 20 6f 70       int fd = op
e3700 65 6e 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c  en(pCtx->dbPath,
e3710 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67   pFile->openFlag
e3720 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
e3730 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45         SQLITE_DE
e3740 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
e3750 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 20 20 4f  SSIONS);.      O
e3760 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 52  STRACE2("TRANSPR
e3770 4f 58 59 3a 20 4f 50 45 4e 20 20 25 64 5c 6e 22  OXY: OPEN  %d\n"
e3780 2c 20 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28  , fd);.      if(
e3790 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20   fd>=0 ){.      
e37a0 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 66 64 3b    pFile->h = fd;
e37b0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
e37c0 20 20 20 20 20 20 72 63 3d 53 51 4c 49 54 45 5f        rc=SQLITE_
e37d0 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a 20 53 51 4c  CANTOPEN; /* SQL
e37e0 49 54 45 5f 42 55 53 59 3f 20 70 72 6f 78 79 54  ITE_BUSY? proxyT
e37f0 61 6b 65 43 6f 6e 63 68 20 63 61 6c 6c 65 64 0a  akeConch called.
e3800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
e3820 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f  uring locking */
e3830 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
e3840 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
e3850 45 5f 4f 4b 20 26 26 20 21 70 43 74 78 2d 3e 6c  E_OK && !pCtx->l
e3860 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20  ockProxy ){.    
e3870 20 20 63 68 61 72 20 2a 70 61 74 68 20 3d 20 74    char *path = t
e3880 4c 6f 63 6b 50 61 74 68 20 3f 20 74 4c 6f 63 6b  LockPath ? tLock
e3890 50 61 74 68 20 3a 20 70 43 74 78 2d 3e 6c 6f 63  Path : pCtx->loc
e38a0 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20  kProxyPath;.    
e38b0 20 20 2f 2a 20 41 43 53 3a 20 4e 65 65 64 20 74    /* ACS: Need t
e38c0 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  o make a copy of
e38d0 20 70 61 74 68 20 73 6f 6d 65 74 69 6d 65 73 20   path sometimes 
e38e0 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 72  */.      rc = pr
e38f0 6f 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c  oxyCreateUnixFil
e3900 65 28 70 61 74 68 2c 20 26 70 43 74 78 2d 3e 6c  e(path, &pCtx->l
e3910 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d  ockProxy);.    }
e3920 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
e3930 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
e3940 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20  pCtx->conchHeld 
e3950 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  = 1;..      if( 
e3960 74 4c 6f 63 6b 50 61 74 68 20 29 7b 0a 20 20 20  tLockPath ){.   
e3970 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50       pCtx->lockP
e3980 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74  roxyPath = sqlit
e3990 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 74 4c  e3DbStrDup(0, tL
e39a0 6f 63 6b 50 61 74 68 29 3b 0a 20 20 20 20 20 20  ockPath);.      
e39b0 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b    if( pCtx->lock
e39c0 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 20 3d  Proxy->pMethod =
e39d0 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20  = &afpIoMethods 
e39e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 28 28 61  ){.          ((a
e39f0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
e3a00 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f   *)pCtx->lockPro
e3a10 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  xy->lockingConte
e3a20 78 74 29 2d 3e 64 62 50 61 74 68 20 3d 0a 20 20  xt)->dbPath =.  
e3a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3a40 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f     pCtx->lockPro
e3a50 78 79 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20  xyPath;.        
e3a60 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20  }.      }.    } 
e3a70 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 63 6f 6e  else {.      con
e3a80 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d  chFile->pMethod-
e3a90 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65  >xUnlock((sqlite
e3aa0 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c  3_file*)conchFil
e3ab0 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20  e, NO_LOCK);.   
e3ac0 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28   }.    OSTRACE3(
e3ad0 22 54 41 4b 45 43 4f 4e 43 48 20 20 25 64 20 25  "TAKECONCH  %d %
e3ae0 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d  s\n", conchFile-
e3af0 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  >h, rc==SQLITE_O
e3b00 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c 65 64 22 29  K?"ok":"failed")
e3b10 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
e3b20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
e3b30 20 70 46 69 6c 65 20 68 6f 6c 64 73 20 61 20 6c   pFile holds a l
e3b40 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e 63 68 20 66  ock on a conch f
e3b50 69 6c 65 2c 20 74 68 65 6e 20 72 65 6c 65 61 73  ile, then releas
e3b60 65 20 74 68 61 74 20 6c 6f 63 6b 2e 0a 2a 2f 0a  e that lock..*/.
e3b70 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
e3b80 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 75 6e 69  ReleaseConch(uni
e3b90 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  xFile *pFile){. 
e3ba0 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
e3bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e3bc0 53 75 62 72 6f 75 74 69 6e 65 20 72 65 74 75 72  Subroutine retur
e3bd0 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 70 72 6f 78  n code */.  prox
e3be0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
e3bf0 2a 70 43 74 78 3b 20 20 2f 2a 20 54 68 65 20 6c  *pCtx;  /* The l
e3c00 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 66  ocking context f
e3c10 6f 72 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63  or the proxy loc
e3c20 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20  k */.  unixFile 
e3c30 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20  *conchFile;     
e3c40 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
e3c50 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a  e conch file */.
e3c60 0a 20 20 70 43 74 78 20 3d 20 28 70 72 6f 78 79  .  pCtx = (proxy
e3c70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
e3c80 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
e3c90 6f 6e 74 65 78 74 3b 0a 20 20 63 6f 6e 63 68 46  ontext;.  conchF
e3ca0 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63  ile = pCtx->conc
e3cb0 68 46 69 6c 65 3b 0a 20 20 4f 53 54 52 41 43 45  hFile;.  OSTRACE
e3cc0 34 28 22 52 45 4c 45 41 53 45 43 4f 4e 43 48 20  4("RELEASECONCH 
e3cd0 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25   %d for %s pid=%
e3ce0 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d  d\n", conchFile-
e3cf0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28  >h,.           (
e3d00 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
e3d10 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b  ath ? pCtx->lock
e3d20 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 75  ProxyPath : ":au
e3d30 74 6f 3a 22 29 2c 20 0a 20 20 20 20 20 20 20 20  to:"), .        
e3d40 20 20 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20     getpid());.  
e3d50 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20  pCtx->conchHeld 
e3d60 3d 20 30 3b 0a 20 20 72 63 20 3d 20 63 6f 6e 63  = 0;.  rc = conc
e3d70 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e  hFile->pMethod->
e3d80 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33  xUnlock((sqlite3
e3d90 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65  _file*)conchFile
e3da0 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 4f 53  , NO_LOCK);.  OS
e3db0 54 52 41 43 45 33 28 22 52 45 4c 45 41 53 45 43  TRACE3("RELEASEC
e3dc0 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e 22 2c 20  ONCH  %d %s\n", 
e3dd0 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20  conchFile->h,.  
e3de0 20 20 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51           (rc==SQ
e3df0 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a  LITE_OK ? "ok" :
e3e00 20 22 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72   "failed"));.  r
e3e10 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
e3e20 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d  ** Given the nam
e3e30 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  e of a database 
e3e40 66 69 6c 65 2c 20 63 6f 6d 70 75 74 65 20 74 68  file, compute th
e3e50 65 20 6e 61 6d 65 20 6f 66 20 69 74 73 20 63 6f  e name of its co
e3e60 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a 20 53 74 6f  nch file..** Sto
e3e70 72 65 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  re the conch fil
e3e80 65 6e 61 6d 65 20 69 6e 20 6d 65 6d 6f 72 79 20  ename in memory 
e3e90 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
e3ea0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a  lite3_malloc()..
e3eb0 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e 63 68 50  ** Make *pConchP
e3ec0 61 74 68 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  ath point to the
e3ed0 20 6e 65 77 20 6e 61 6d 65 2e 20 20 52 65 74 75   new name.  Retu
e3ee0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
e3ef0 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 53 51  success.** or SQ
e3f00 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 75 6e  LITE_NOMEM if un
e3f10 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 6d  able to obtain m
e3f20 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  emory..**.** The
e3f30 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f   caller is respo
e3f40 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72  nsible for ensur
e3f50 69 6e 67 20 74 68 61 74 20 74 68 65 20 61 6c 6c  ing that the all
e3f60 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a  ocated memory.**
e3f70 20 73 70 61 63 65 20 69 73 20 65 76 65 6e 74 75   space is eventu
e3f80 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a  ally freed..**.*
e3f90 2a 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 69 73  * *pConchPath is
e3fa0 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 69 66 20   set to NULL if 
e3fb0 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
e3fc0 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ion error occurs
e3fd0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e3fe0 70 72 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 68  proxyCreateConch
e3ff0 50 61 74 68 6e 61 6d 65 28 63 68 61 72 20 2a 64  Pathname(char *d
e4000 62 50 61 74 68 2c 20 63 68 61 72 20 2a 2a 70 43  bPath, char **pC
e4010 6f 6e 63 68 50 61 74 68 29 7b 0a 20 20 69 6e 74  onchPath){.  int
e4020 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
e4030 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
e4040 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
e4050 69 6e 74 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73  int len = (int)s
e4060 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 20 2f  trlen(dbPath); /
e4070 2a 20 4c 65 6e 67 74 68 20 6f 66 20 64 61 74 61  * Length of data
e4080 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 2d 20  base filename - 
e4090 64 62 50 61 74 68 20 2a 2f 0a 20 20 63 68 61 72  dbPath */.  char
e40a0 20 2a 63 6f 6e 63 68 50 61 74 68 3b 20 20 20 20   *conchPath;    
e40b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 62 75 66            /* buf
e40c0 66 65 72 20 69 6e 20 77 68 69 63 68 20 74 6f 20  fer in which to 
e40d0 63 6f 6e 73 74 72 75 63 74 20 63 6f 6e 63 68 20  construct conch 
e40e0 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c  name */..  /* Al
e40f0 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
e4100 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e   the conch filen
e4110 61 6d 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ame and initiali
e4120 7a 65 20 74 68 65 20 6e 61 6d 65 20 74 6f 0a 20  ze the name to. 
e4130 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   ** the name of 
e4140 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74  the original dat
e4150 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 20 20  abase file. */  
e4160 0a 20 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 3d  .  *pConchPath =
e4170 20 63 6f 6e 63 68 50 61 74 68 20 3d 20 28 63 68   conchPath = (ch
e4180 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar *)sqlite3_mal
e4190 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 3b 0a 20 20  loc(len + 8);.  
e41a0 69 66 28 20 63 6f 6e 63 68 50 61 74 68 3d 3d 30  if( conchPath==0
e41b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
e41c0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
e41d0 0a 20 20 6d 65 6d 63 70 79 28 63 6f 6e 63 68 50  .  memcpy(conchP
e41e0 61 74 68 2c 20 64 62 50 61 74 68 2c 20 6c 65 6e  ath, dbPath, len
e41f0 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 77  +1);.  .  /* now
e4200 20 69 6e 73 65 72 74 20 61 20 22 2e 22 20 62 65   insert a "." be
e4210 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 2f 20  fore the last / 
e4220 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 66  character */.  f
e4230 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 29 3b 20 69  or( i=(len-1); i
e4240 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a 20 20 20 20  >=0; i-- ){.    
e4250 69 66 28 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d  if( conchPath[i]
e4260 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69  =='/' ){.      i
e4270 2b 2b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ++;.      break;
e4280 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e  .    }.  }.  con
e4290 63 68 50 61 74 68 5b 69 5d 3d 27 2e 27 3b 0a 20  chPath[i]='.';. 
e42a0 20 77 68 69 6c 65 20 28 20 69 3c 6c 65 6e 20 29   while ( i<len )
e42b0 7b 0a 20 20 20 20 63 6f 6e 63 68 50 61 74 68 5b  {.    conchPath[
e42c0 69 2b 31 5d 3d 64 62 50 61 74 68 5b 69 5d 3b 0a  i+1]=dbPath[i];.
e42d0 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20      i++;.  }..  
e42e0 2f 2a 20 61 70 70 65 6e 64 20 74 68 65 20 22 2d  /* append the "-
e42f0 63 6f 6e 63 68 22 20 73 75 66 66 69 78 20 74 6f  conch" suffix to
e4300 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 6d   the file */.  m
e4310 65 6d 63 70 79 28 26 63 6f 6e 63 68 50 61 74 68  emcpy(&conchPath
e4320 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63 68 22 2c  [i+1], "-conch",
e4330 20 37 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28   7);.  assert( (
e4340 69 6e 74 29 73 74 72 6c 65 6e 28 63 6f 6e 63 68  int)strlen(conch
e4350 50 61 74 68 29 20 3d 3d 20 6c 65 6e 2b 37 20 29  Path) == len+7 )
e4360 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
e4370 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 20 54 61  TE_OK;.}.../* Ta
e4380 6b 65 73 20 61 20 66 75 6c 6c 79 20 63 6f 6e 66  kes a fully conf
e4390 69 67 75 72 65 64 20 70 72 6f 78 79 20 6c 6f 63  igured proxy loc
e43a0 6b 69 6e 67 2d 73 74 79 6c 65 20 75 6e 69 78 20  king-style unix 
e43b0 66 69 6c 65 20 61 6e 64 20 73 77 69 74 63 68 65  file and switche
e43c0 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 6c  s.** the local l
e43d0 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 20 0a 2a  ock file path .*
e43e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 77 69  /.static int swi
e43f0 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68  tchLockProxyPath
e4400 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
e4410 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61  , const char *pa
e4420 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63  th) {.  proxyLoc
e4430 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74  kingContext *pCt
e4440 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e  x = (proxyLockin
e4450 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d  gContext*)pFile-
e4460 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
e4470 0a 20 20 63 68 61 72 20 2a 6f 6c 64 50 61 74 68  .  char *oldPath
e4480 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f   = pCtx->lockPro
e4490 78 79 50 61 74 68 3b 0a 20 20 69 6e 74 20 72 63  xyPath;.  int rc
e44a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
e44b0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
e44c0 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  type!=NO_LOCK ){
e44d0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
e44e0 54 45 5f 42 55 53 59 3b 0a 20 20 7d 20 20 0a 0a  TE_BUSY;.  }  ..
e44f0 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20    /* nothing to 
e4500 64 6f 20 69 66 20 74 68 65 20 70 61 74 68 20 69  do if the path i
e4510 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 6f 3a 20 6f  s NULL, :auto: o
e4520 72 20 6d 61 74 63 68 65 73 20 74 68 65 20 65 78  r matches the ex
e4530 69 73 74 69 6e 67 20 70 61 74 68 20 2a 2f 0a 20  isting path */. 
e4540 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 70 61   if( !path || pa
e4550 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 21  th[0]=='\0' || !
e4560 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 3a 61  strcmp(path, ":a
e4570 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 20 20 28 6f  uto:") ||.    (o
e4580 6c 64 50 61 74 68 20 26 26 20 21 73 74 72 6e 63  ldPath && !strnc
e4590 6d 70 28 6f 6c 64 50 61 74 68 2c 20 70 61 74 68  mp(oldPath, path
e45a0 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 29 20 29  , MAXPATHLEN)) )
e45b0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
e45c0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
e45d0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c  .    unixFile *l
e45e0 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 78 2d  ockProxy = pCtx-
e45f0 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20  >lockProxy;.    
e4600 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3d  pCtx->lockProxy=
e4610 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 74 78 2d 3e  NULL;.    pCtx->
e4620 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20  conchHeld = 0;. 
e4630 20 20 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79     if( lockProxy
e4640 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  !=NULL ){.      
e4650 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d  rc=lockProxy->pM
e4660 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73  ethod->xClose((s
e4670 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 6c 6f  qlite3_file *)lo
e4680 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20  ckProxy);.      
e4690 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
e46a0 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  rc;.      sqlite
e46b0 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79  3_free(lockProxy
e46c0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
e46d0 69 74 65 33 5f 66 72 65 65 28 6f 6c 64 50 61 74  ite3_free(oldPat
e46e0 68 29 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f  h);.    pCtx->lo
e46f0 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71  ckProxyPath = sq
e4700 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
e4710 20 70 61 74 68 29 3b 0a 20 20 7d 0a 20 20 0a 20   path);.  }.  . 
e4720 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
e4730 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 73 20 61 20  *.** pFile is a 
e4740 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 62 65  file that has be
e4750 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70  en opened by a p
e4760 72 69 6f 72 20 78 4f 70 65 6e 20 63 61 6c 6c 2e  rior xOpen call.
e4770 20 20 64 62 50 61 74 68 0a 2a 2a 20 69 73 20 61    dbPath.** is a
e4780 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 61   string buffer a
e4790 74 20 6c 65 61 73 74 20 4d 41 58 50 41 54 48 4c  t least MAXPATHL
e47a0 45 4e 2b 31 20 63 68 61 72 61 63 74 65 72 73 20  EN+1 characters 
e47b0 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54  in size..**.** T
e47c0 68 69 73 20 72 6f 75 74 69 6e 65 20 66 69 6e 64  his routine find
e47d0 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 73   the filename as
e47e0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 46  sociated with pF
e47f0 69 6c 65 20 61 6e 64 20 77 72 69 74 65 73 20 69  ile and writes i
e4800 74 0a 2a 2a 20 69 6e 74 20 64 62 50 61 74 68 2e  t.** int dbPath.
e4810 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
e4820 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f 72  roxyGetDbPathFor
e4830 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c  UnixFile(unixFil
e4840 65 20 2a 70 46 69 6c 65 2c 20 63 68 61 72 20 2a  e *pFile, char *
e4850 64 62 50 61 74 68 29 7b 0a 23 69 66 20 64 65 66  dbPath){.#if def
e4860 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a  ined(__APPLE__).
e4870 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65    if( pFile->pMe
e4880 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65  thod == &afpIoMe
e4890 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
e48a0 61 66 70 20 73 74 79 6c 65 20 6b 65 65 70 73 20  afp style keeps 
e48b0 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
e48c0 68 65 20 64 62 20 70 61 74 68 20 69 6e 20 74 68  he db path in th
e48d0 65 20 66 69 6c 65 50 61 74 68 20 66 69 65 6c 64  e filePath field
e48e0 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20   .    ** of the 
e48f0 73 74 72 75 63 74 20 2a 2f 0a 20 20 20 20 61 73  struct */.    as
e4900 73 65 72 74 28 20 28 69 6e 74 29 73 74 72 6c 65  sert( (int)strle
e4910 6e 28 28 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e  n((char*)pFile->
e4920 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c  lockingContext)<
e4930 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20  =MAXPATHLEN );. 
e4940 20 20 20 73 74 72 63 70 79 28 64 62 50 61 74 68     strcpy(dbPath
e4950 2c 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  , ((afpLockingCo
e4960 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c  ntext *)pFile->l
e4970 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e  ockingContext)->
e4980 64 62 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65  dbPath);.  }else
e4990 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 46  .#endif.  if( pF
e49a0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20  ile->pMethod == 
e49b0 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64  &dotlockIoMethod
e49c0 73 20 29 7b 0a 20 20 20 20 2f 2a 20 64 6f 74 20  s ){.    /* dot 
e49d0 6c 6f 63 6b 20 73 74 79 6c 65 20 75 73 65 73 20  lock style uses 
e49e0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  the locking cont
e49f0 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ext to store the
e4a00 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 20 20 2a 2a   dot lock.    **
e4a10 20 66 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20   file path */.  
e4a20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 73 74 72 6c    int len = strl
e4a30 65 6e 28 28 63 68 61 72 20 2a 29 70 46 69 6c 65  en((char *)pFile
e4a40 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
e4a50 29 20 2d 20 73 74 72 6c 65 6e 28 44 4f 54 4c 4f  ) - strlen(DOTLO
e4a60 43 4b 5f 53 55 46 46 49 58 29 3b 0a 20 20 20 20  CK_SUFFIX);.    
e4a70 6d 65 6d 63 70 79 28 64 62 50 61 74 68 2c 20 28  memcpy(dbPath, (
e4a80 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f  char *)pFile->lo
e4a90 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c 20 6c 65  ckingContext, le
e4aa0 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  n + 1);.  }else{
e4ab0 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6f 74 68 65  .    /* all othe
e4ac0 72 20 73 74 79 6c 65 73 20 75 73 65 20 74 68 65  r styles use the
e4ad0 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
e4ae0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 64 62   to store the db
e4af0 20 66 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20   file path */.  
e4b00 20 20 61 73 73 65 72 74 28 20 73 74 72 6c 65 6e    assert( strlen
e4b10 28 28 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c  ((char*)pFile->l
e4b20 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d  ockingContext)<=
e4b30 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20 20  MAXPATHLEN );.  
e4b40 20 20 73 74 72 63 70 79 28 64 62 50 61 74 68 2c    strcpy(dbPath,
e4b50 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e   (char *)pFile->
e4b60 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
e4b70 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
e4b80 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
e4b90 2a 20 54 61 6b 65 73 20 61 6e 20 61 6c 72 65 61  * Takes an alrea
e4ba0 64 79 20 66 69 6c 6c 65 64 20 69 6e 20 75 6e 69  dy filled in uni
e4bb0 78 20 66 69 6c 65 20 61 6e 64 20 61 6c 74 65 72  x file and alter
e4bc0 73 20 69 74 20 73 6f 20 61 6c 6c 20 66 69 6c 65  s it so all file
e4bd0 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 77 69 6c   locking .** wil
e4be0 6c 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 6f  l be performed o
e4bf0 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 72 6f 78  n the local prox
e4c00 79 20 6c 6f 63 6b 20 66 69 6c 65 2e 20 20 54 68  y lock file.  Th
e4c10 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c  e following fiel
e4c20 64 73 0a 2a 2a 20 61 72 65 20 70 72 65 73 65 72  ds.** are preser
e4c30 76 65 64 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69  ved in the locki
e4c40 6e 67 20 63 6f 6e 74 65 78 74 20 73 6f 20 74 68  ng context so th
e4c50 61 74 20 74 68 65 79 20 63 61 6e 20 62 65 20 72  at they can be r
e4c60 65 73 74 6f 72 65 64 20 61 6e 64 20 0a 2a 2a 20  estored and .** 
e4c70 74 68 65 20 75 6e 69 78 20 73 74 72 75 63 74 75  the unix structu
e4c80 72 65 20 70 72 6f 70 65 72 6c 79 20 63 6c 65 61  re properly clea
e4c90 6e 65 64 20 75 70 20 61 74 20 63 6c 6f 73 65 20  ned up at close 
e4ca0 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e 6c 6f 63 6b  time:.**  ->lock
e4cb0 69 6e 67 43 6f 6e 74 65 78 74 0a 2a 2a 20 20 2d  ingContext.**  -
e4cc0 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a 73 74 61 74  >pMethod.*/.stat
e4cd0 69 63 20 69 6e 74 20 70 72 6f 78 79 54 72 61 6e  ic int proxyTran
e4ce0 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e  sformUnixFile(un
e4cf0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63  ixFile *pFile, c
e4d00 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 29  onst char *path)
e4d10 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e   {.  proxyLockin
e4d20 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a  gContext *pCtx;.
e4d30 20 20 63 68 61 72 20 64 62 50 61 74 68 5b 4d 41    char dbPath[MA
e4d40 58 50 41 54 48 4c 45 4e 2b 31 5d 3b 20 20 20 20  XPATHLEN+1];    
e4d50 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
e4d60 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
e4d70 2a 2f 0a 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50  */.  char *lockP
e4d80 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20  ath=NULL;.  int 
e4d90 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
e4da0 20 20 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e    .  if( pFile->
e4db0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
e4dc0 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
e4dd0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
e4de0 0a 20 20 70 72 6f 78 79 47 65 74 44 62 50 61 74  .  proxyGetDbPat
e4df0 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 70 46 69  hForUnixFile(pFi
e4e00 6c 65 2c 20 64 62 50 61 74 68 29 3b 0a 20 20 69  le, dbPath);.  i
e4e10 66 28 20 21 70 61 74 68 20 7c 7c 20 70 61 74 68  f( !path || path
e4e20 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 21 73 74  [0]=='\0' || !st
e4e30 72 63 6d 70 28 70 61 74 68 2c 20 22 3a 61 75 74  rcmp(path, ":aut
e4e40 6f 3a 22 29 20 29 7b 0a 20 20 20 20 6c 6f 63 6b  o:") ){.    lock
e4e50 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 7d 65 6c  Path=NULL;.  }el
e4e60 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68  se{.    lockPath
e4e70 3d 28 63 68 61 72 20 2a 29 70 61 74 68 3b 0a 20  =(char *)path;. 
e4e80 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 34   }.  .  OSTRACE4
e4e90 28 22 54 52 41 4e 53 50 52 4f 58 59 20 20 25 64  ("TRANSPROXY  %d
e4ea0 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e   for %s pid=%d\n
e4eb0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20  ", pFile->h,.   
e4ec0 20 20 20 20 20 20 20 20 28 6c 6f 63 6b 50 61 74          (lockPat
e4ed0 68 20 3f 20 6c 6f 63 6b 50 61 74 68 20 3a 20 22  h ? lockPath : "
e4ee0 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64  :auto:"), getpid
e4ef0 28 29 29 3b 0a 0a 20 20 70 43 74 78 20 3d 20 73  ());..  pCtx = s
e4f00 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73  qlite3_malloc( s
e4f10 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a  izeof(*pCtx) );.
e4f20 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b    if( pCtx==0 ){
e4f30 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
e4f40 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
e4f50 6d 65 6d 73 65 74 28 70 43 74 78 2c 20 30 2c 20  memset(pCtx, 0, 
e4f60 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 29 3b 0a  sizeof(*pCtx));.
e4f70 0a 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65  .  rc = proxyCre
e4f80 61 74 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65  ateConchPathname
e4f90 28 64 62 50 61 74 68 2c 20 26 70 43 74 78 2d 3e  (dbPath, &pCtx->
e4fa0 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 0a  conchFilePath);.
e4fb0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
e4fc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  _OK ){.    rc = 
e4fd0 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78 46  proxyCreateUnixF
e4fe0 69 6c 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46  ile(pCtx->conchF
e4ff0 69 6c 65 50 61 74 68 2c 20 26 70 43 74 78 2d 3e  ilePath, &pCtx->
e5000 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 7d 20  conchFile);.  } 
e5010 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49   .  if( rc==SQLI
e5020 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 50 61 74  TE_OK && lockPat
e5030 68 20 29 7b 0a 20 20 20 20 70 43 74 78 2d 3e 6c  h ){.    pCtx->l
e5040 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73  ockProxyPath = s
e5050 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30  qlite3DbStrDup(0
e5060 2c 20 6c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 7d  , lockPath);.  }
e5070 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
e5080 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20  TE_OK ){.    /* 
e5090 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c  all memory is al
e50a0 6c 6f 63 61 74 65 64 2c 20 70 72 6f 78 79 73 20  located, proxys 
e50b0 61 72 65 20 63 72 65 61 74 65 64 20 61 6e 64 20  are created and 
e50c0 61 73 73 69 67 6e 65 64 2c 20 0a 20 20 20 20 2a  assigned, .    *
e50d0 2a 20 73 77 69 74 63 68 20 74 68 65 20 6c 6f 63  * switch the loc
e50e0 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 61 6e 64  king context and
e50f0 20 70 4d 65 74 68 6f 64 20 74 68 65 6e 20 72 65   pMethod then re
e5100 74 75 72 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  turn..    */.   
e5110 20 70 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20   pCtx->dbPath = 
e5120 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
e5130 30 2c 20 64 62 50 61 74 68 29 3b 0a 20 20 20 20  0, dbPath);.    
e5140 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67  pCtx->oldLocking
e5150 43 6f 6e 74 65 78 74 20 3d 20 70 46 69 6c 65 2d  Context = pFile-
e5160 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
e5170 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
e5180 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74  ingContext = pCt
e5190 78 3b 0a 20 20 20 20 70 43 74 78 2d 3e 70 4f 6c  x;.    pCtx->pOl
e51a0 64 4d 65 74 68 6f 64 20 3d 20 70 46 69 6c 65 2d  dMethod = pFile-
e51b0 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 20 20 70 46  >pMethod;.    pF
e51c0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26  ile->pMethod = &
e51d0 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 3b 0a  proxyIoMethods;.
e51e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
e51f0 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65   pCtx->conchFile
e5200 20 29 7b 20 0a 20 20 20 20 20 20 72 63 20 3d 20   ){ .      rc = 
e5210 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 2d  pCtx->conchFile-
e5220 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65  >pMethod->xClose
e5230 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ((sqlite3_file *
e5240 29 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65  )pCtx->conchFile
e5250 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
e5260 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
e5270 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
e5280 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29  pCtx->conchFile)
e5290 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
e52a0 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63  te3_free(pCtx->c
e52b0 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 20 0a  onchFilePath); .
e52c0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
e52d0 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20 20 4f 53  (pCtx);.  }.  OS
e52e0 54 52 41 43 45 33 28 22 54 52 41 4e 53 50 52 4f  TRACE3("TRANSPRO
e52f0 58 59 20 20 25 64 20 25 73 5c 6e 22 2c 20 70 46  XY  %d %s\n", pF
e5300 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
e5310 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f     (rc==SQLITE_O
e5320 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c  K ? "ok" : "fail
e5330 65 64 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20  ed"));.  return 
e5340 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  rc;.}.../*.** Th
e5350 69 73 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c  is routine handl
e5360 65 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  es sqlite3_file_
e5370 63 6f 6e 74 72 6f 6c 28 29 20 63 61 6c 6c 73 20  control() calls 
e5380 74 68 61 74 20 61 72 65 20 73 70 65 63 69 66 69  that are specifi
e5390 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 79 20 6c 6f  c.** to proxy lo
e53a0 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  cking..*/.static
e53b0 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 6f   int proxyFileCo
e53c0 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69  ntrol(sqlite3_fi
e53d0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20  le *id, int op, 
e53e0 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73  void *pArg){.  s
e53f0 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
e5400 20 63 61 73 65 20 53 51 4c 49 54 45 5f 47 45 54   case SQLITE_GET
e5410 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20  _LOCKPROXYFILE: 
e5420 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c 65  {.      unixFile
e5430 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
e5440 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 69  ile*)id;.      i
e5450 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  f( pFile->pMetho
e5460 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74  d == &proxyIoMet
e5470 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20  hods ){.        
e5480 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
e5490 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
e54a0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
e54b0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
e54c0 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20  Context;.       
e54d0 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28   proxyTakeConch(
e54e0 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 20  pFile);.        
e54f0 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  if( pCtx->lockPr
e5500 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20  oxyPath ){.     
e5510 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61       *(const cha
e5520 72 20 2a 2a 29 70 41 72 67 20 3d 20 70 43 74 78  r **)pArg = pCtx
e5530 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b  ->lockProxyPath;
e5540 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
e5550 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73            *(cons
e5560 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d  t char **)pArg =
e5570 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74 20 68 65   ":auto: (not he
e5580 6c 64 29 22 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ld)";.        }.
e5590 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20        } else {. 
e55a0 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63         *(const c
e55b0 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 4e 55  har **)pArg = NU
e55c0 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  LL;.      }.    
e55d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e55e0 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  OK;.    }.    ca
e55f0 73 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f  se SQLITE_SET_LO
e5600 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20  CKPROXYFILE: {. 
e5610 20 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70       unixFile *p
e5620 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
e5630 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 6e 74 20  *)id;.      int 
e5640 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
e5650 20 20 20 20 20 20 69 6e 74 20 69 73 50 72 6f 78        int isProx
e5660 79 53 74 79 6c 65 20 3d 20 28 70 46 69 6c 65 2d  yStyle = (pFile-
e5670 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f  >pMethod == &pro
e5680 78 79 49 6f 4d 65 74 68 6f 64 73 29 3b 0a 20 20  xyIoMethods);.  
e5690 20 20 20 20 69 66 28 20 70 41 72 67 3d 3d 4e 55      if( pArg==NU
e56a0 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 20 63 68 61  LL || (const cha
e56b0 72 20 2a 29 70 41 72 67 3d 3d 30 20 29 7b 0a 20  r *)pArg==0 ){. 
e56c0 20 20 20 20 20 20 20 69 66 28 20 69 73 50 72 6f         if( isPro
e56d0 78 79 53 74 79 6c 65 20 29 7b 0a 20 20 20 20 20  xyStyle ){.     
e56e0 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66       /* turn off
e56f0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d   proxy locking -
e5700 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20 2a   not supported *
e5710 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  /.          rc =
e5720 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 2f 2a   SQLITE_ERROR /*
e5730 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 3f  SQLITE_PROTOCOL?
e5740 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3f 2a   SQLITE_MISUSE?*
e5750 2f 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  /;.        }else
e5760 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74  {.          /* t
e5770 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f  urn off proxy lo
e5780 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61 64 79 20  cking - already 
e5790 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f 0a 20 20  off - NOOP */.  
e57a0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
e57b0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20  ITE_OK;.        
e57c0 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
e57d0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
e57e0 72 20 2a 70 72 6f 78 79 50 61 74 68 20 3d 20 28  r *proxyPath = (
e57f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 41 72  const char *)pAr
e5800 67 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  g;.        if( i
e5810 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20  sProxyStyle ){. 
e5820 20 20 20 20 20 20 20 20 20 70 72 6f 78 79 4c 6f           proxyLo
e5830 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
e5840 74 78 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20  tx = .          
e5850 20 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43    (proxyLockingC
e5860 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c  ontext*)pFile->l
e5870 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
e5880 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73 74           if( !st
e5890 72 63 6d 70 28 70 41 72 67 2c 20 22 3a 61 75 74  rcmp(pArg, ":aut
e58a0 6f 3a 22 29 20 0a 20 20 20 20 20 20 20 20 20 20  o:") .          
e58b0 20 7c 7c 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50   || (pCtx->lockP
e58c0 72 6f 78 79 50 61 74 68 20 26 26 0a 20 20 20 20  roxyPath &&.    
e58d0 20 20 20 20 20 20 20 20 20 20 20 21 73 74 72 6e             !strn
e58e0 63 6d 70 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  cmp(pCtx->lockPr
e58f0 6f 78 79 50 61 74 68 2c 20 70 72 6f 78 79 50 61  oxyPath, proxyPa
e5900 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 29  th, MAXPATHLEN))
e5910 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20  .          ){.  
e5920 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
e5930 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
e5940 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
e5950 20 20 20 20 20 20 20 72 63 20 3d 20 73 77 69 74         rc = swit
e5960 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 28  chLockProxyPath(
e5970 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74 68  pFile, proxyPath
e5980 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
e5990 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
e59a0 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20          /* turn 
e59b0 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65 20 6c 6f  on proxy file lo
e59c0 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20  cking */.       
e59d0 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72 61     rc = proxyTra
e59e0 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 70  nsformUnixFile(p
e59f0 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74 68 29  File, proxyPath)
e5a00 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
e5a10 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
e5a20 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64   rc;.    }.    d
e5a30 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
e5a40 61 73 73 65 72 74 28 20 30 20 29 3b 20 20 2f 2a  assert( 0 );  /*
e5a50 20 54 68 65 20 63 61 6c 6c 20 61 73 73 75 72 65   The call assure
e5a60 73 20 74 68 61 74 20 6f 6e 6c 79 20 76 61 6c 69  s that only vali
e5a70 64 20 6f 70 63 6f 64 65 73 20 61 72 65 20 73 65  d opcodes are se
e5a80 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a  nt */.    }.  }.
e5a90 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f    /*NOTREACHED*/
e5aa0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
e5ab0 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _ERROR;.}../*.**
e5ac0 20 57 69 74 68 69 6e 20 74 68 69 73 20 64 69 76   Within this div
e5ad0 69 73 69 6f 6e 20 28 74 68 65 20 70 72 6f 78 79  ision (the proxy
e5ae0 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c  ing locking impl
e5af0 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74 68 65 20  ementation) the 
e5b00 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 61 62  procedures.** ab
e5b10 6f 76 65 20 74 68 69 73 20 70 6f 69 6e 74 20 61  ove this point a
e5b20 72 65 20 61 6c 6c 20 75 74 69 6c 69 74 69 65 73  re all utilities
e5b30 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72 65 6c 61  .  The lock-rela
e5b40 74 65 64 20 6d 65 74 68 6f 64 73 20 6f 66 20 74  ted methods of t
e5b50 68 65 0a 2a 2a 20 70 72 6f 78 79 2d 6c 6f 63 6b  he.** proxy-lock
e5b60 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  ing sqlite3_io_m
e5b70 65 74 68 6f 64 20 6f 62 6a 65 63 74 20 66 6f 6c  ethod object fol
e5b80 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  low..*/.../*.** 
e5b90 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
e5ba0 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20  cks if there is 
e5bb0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
e5bc0 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63  held on the spec
e5bd0 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79  ified.** file by
e5be0 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
e5bf0 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73  er process. If s
e5c00 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65  uch a lock is he
e5c10 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74  ld, set *pResOut
e5c20 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  .** to a non-zer
e5c30 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73  o value otherwis
e5c40 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65  e *pResOut is se
e5c50 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20  t to zero.  The 
e5c60 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
e5c70 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
e5c80 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f  _OK unless an I/
e5c90 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  O error occurs d
e5ca0 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b  uring lock check
e5cb0 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
e5cc0 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73  nt proxyCheckRes
e5cd0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
e5ce0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
e5cf0 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 75 6e  *pResOut) {.  un
e5d00 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
e5d10 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
e5d20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54   int rc = proxyT
e5d30 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b  akeConch(pFile);
e5d40 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
e5d50 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78  E_OK ){.    prox
e5d60 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
e5d70 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
e5d80 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70  ckingContext *)p
e5d90 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
e5da0 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69  text;.    unixFi
e5db0 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78  le *proxy = pCtx
e5dc0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20  ->lockProxy;.   
e5dd0 20 72 65 74 75 72 6e 20 70 72 6f 78 79 2d 3e 70   return proxy->p
e5de0 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63 6b 52 65  Method->xCheckRe
e5df0 73 65 72 76 65 64 4c 6f 63 6b 28 28 73 71 6c 69  servedLock((sqli
e5e00 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c  te3_file*)proxy,
e5e10 20 70 52 65 73 4f 75 74 29 3b 0a 20 20 7d 0a 20   pResOut);.  }. 
e5e20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
e5e30 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  *.** Lock the fi
e5e40 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
e5e50 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
e5e60 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65  rameter locktype
e5e70 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
e5e80 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
e5e90 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
e5ea0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
e5eb0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
e5ec0 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
e5ed0 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
e5ee0 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
e5ef0 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
e5f00 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
e5f10 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
e5f20 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
e5f30 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
e5f40 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
e5f50 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
e5f60 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
e5f70 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
e5f80 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
e5f90 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
e5fa0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
e5fb0 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
e5fc0 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
e5fd0 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
e5fe0 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
e5ff0 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
e6000 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
e6010 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
e6020 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
e6030 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
e6040 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
e6050 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
e6060 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
e6070 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
e6080 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
e6090 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
e60a0 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
e60b0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
e60c0 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
e60d0 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
e60e0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
e60f0 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
e6100 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
e6110 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f  Use the sqlite3O
e6120 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75  sUnlock().** rou
e6130 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20  tine to lower a 
e6140 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a  locking level..*
e6150 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
e6160 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  xyLock(sqlite3_f
e6170 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
e6180 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46  ktype) {.  unixF
e6190 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
e61a0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e  ixFile*)id;.  in
e61b0 74 20 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65  t rc = proxyTake
e61c0 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20  Conch(pFile);.  
e61d0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
e61e0 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f  K ){.    proxyLo
e61f0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
e6200 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
e6210 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
e6220 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
e6230 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  t;.    unixFile 
e6240 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c  *proxy = pCtx->l
e6250 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 63  ockProxy;.    rc
e6260 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f   = proxy->pMetho
e6270 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65  d->xLock((sqlite
e6280 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c  3_file*)proxy, l
e6290 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 70 46  ocktype);.    pF
e62a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
e62b0 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b  proxy->locktype;
e62c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
e62d0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65  ;.}.../*.** Lowe
e62e0 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  r the locking le
e62f0 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63  vel on file desc
e6300 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20  riptor pFile to 
e6310 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74  locktype.  lockt
e6320 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  ype.** must be e
e6330 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72  ither NO_LOCK or
e6340 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a   SHARED_LOCK..**
e6350 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69  .** If the locki
e6360 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  ng level of the 
e6370 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e6380 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72  is already at or
e6390 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65   below.** the re
e63a0 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20  quested locking 
e63b0 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74  level, this rout
e63c0 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
e63d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
e63e0 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  oxyUnlock(sqlite
e63f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
e6400 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e  locktype) {.  un
e6410 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
e6420 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
e6430 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54   int rc = proxyT
e6440 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b  akeConch(pFile);
e6450 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
e6460 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78  E_OK ){.    prox
e6470 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
e6480 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
e6490 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70  ckingContext *)p
e64a0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
e64b0 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69  text;.    unixFi
e64c0 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78  le *proxy = pCtx
e64d0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20  ->lockProxy;.   
e64e0 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65   rc = proxy->pMe
e64f0 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73  thod->xUnlock((s
e6500 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f  qlite3_file*)pro
e6510 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20  xy, locktype);. 
e6520 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
e6530 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b  pe = proxy->lock
e6540 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75  type;.  }.  retu
e6550 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
e6560 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 74 68 61  Close a file tha
e6570 74 20 75 73 65 73 20 70 72 6f 78 79 20 6c 6f 63  t uses proxy loc
e6580 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ks..*/.static in
e6590 74 20 70 72 6f 78 79 43 6c 6f 73 65 28 73 71 6c  t proxyClose(sql
e65a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
e65b0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20  .  if( id ){.   
e65c0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
e65d0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
e65e0 3b 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69  ;.    proxyLocki
e65f0 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20  ngContext *pCtx 
e6600 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43  = (proxyLockingC
e6610 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e  ontext *)pFile->
e6620 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
e6630 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f      unixFile *lo
e6640 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e  ckProxy = pCtx->
e6650 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 75  lockProxy;.    u
e6660 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69  nixFile *conchFi
e6670 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68  le = pCtx->conch
e6680 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 20 72 63  File;.    int rc
e6690 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
e66a0 20 20 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50    .    if( lockP
e66b0 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63  roxy ){.      rc
e66c0 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d   = lockProxy->pM
e66d0 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28  ethod->xUnlock((
e66e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f  sqlite3_file*)lo
e66f0 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f 4c 4f 43 4b  ckProxy, NO_LOCK
e6700 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
e6710 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
e6720 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78     rc = lockProx
e6730 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f  y->pMethod->xClo
e6740 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  se((sqlite3_file
e6750 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20  *)lockProxy);.  
e6760 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
e6770 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71  urn rc;.      sq
e6780 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50  lite3_free(lockP
e6790 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 70 43 74  roxy);.      pCt
e67a0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 30  x->lockProxy = 0
e67b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
e67c0 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0a 20 20 20  conchFile ){.   
e67d0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e     if( pCtx->con
e67e0 63 68 48 65 6c 64 20 29 7b 0a 20 20 20 20 20 20  chHeld ){.      
e67f0 20 20 72 63 20 3d 20 70 72 6f 78 79 52 65 6c 65    rc = proxyRele
e6800 61 73 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b  aseConch(pFile);
e6810 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20  .        if( rc 
e6820 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
e6830 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
e6840 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68  conchFile->pMeth
e6850 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69  od->xClose((sqli
e6860 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46  te3_file*)conchF
e6870 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ile);.      if( 
e6880 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
e6890 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
e68a0 65 65 28 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20  ee(conchFile);. 
e68b0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
e68c0 5f 66 72 65 65 28 70 43 74 78 2d 3e 6c 6f 63 6b  _free(pCtx->lock
e68d0 50 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20  ProxyPath);.    
e68e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
e68f0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68  x->conchFilePath
e6900 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
e6910 72 65 65 28 70 43 74 78 2d 3e 64 62 50 61 74 68  ree(pCtx->dbPath
e6920 29 3b 0a 20 20 20 20 2f 2a 20 72 65 73 74 6f 72  );.    /* restor
e6930 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6c  e the original l
e6940 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 61  ocking context a
e6950 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 6e 20  nd pMethod then 
e6960 63 6c 6f 73 65 20 69 74 20 2a 2f 0a 20 20 20 20  close it */.    
e6970 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
e6980 6e 74 65 78 74 20 3d 20 70 43 74 78 2d 3e 6f 6c  ntext = pCtx->ol
e6990 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  dLockingContext;
e69a0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74  .    pFile->pMet
e69b0 68 6f 64 20 3d 20 70 43 74 78 2d 3e 70 4f 6c 64  hod = pCtx->pOld
e69c0 4d 65 74 68 6f 64 3b 0a 20 20 20 20 73 71 6c 69  Method;.    sqli
e69d0 74 65 33 5f 66 72 65 65 28 70 43 74 78 29 3b 0a  te3_free(pCtx);.
e69e0 20 20 20 20 72 65 74 75 72 6e 20 70 46 69 6c 65      return pFile
e69f0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73  ->pMethod->xClos
e6a00 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74  e(id);.  }.  ret
e6a10 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
e6a20 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  ....#endif /* de
e6a30 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e6a40 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
e6a50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
e6a60 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f  */./*.** The pro
e6a70 78 79 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65  xy locking style
e6a80 20 69 73 20 69 6e 74 65 6e 64 65 64 20 66 6f 72   is intended for
e6a90 20 75 73 65 20 77 69 74 68 20 41 46 50 20 66 69   use with AFP fi
e6aa0 6c 65 73 79 73 74 65 6d 73 2e 0a 2a 2a 20 41 6e  lesystems..** An
e6ab0 64 20 73 69 6e 63 65 20 41 46 50 20 69 73 20 6f  d since AFP is o
e6ac0 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e  nly supported on
e6ad0 20 4d 61 63 4f 53 58 2c 20 74 68 65 20 70 72 6f   MacOSX, the pro
e6ae0 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 6c  xy locking is al
e6af0 73 6f 0a 2a 2a 20 72 65 73 74 72 69 63 74 65 64  so.** restricted
e6b00 20 74 6f 20 4d 61 63 4f 53 58 2e 0a 2a 2a 20 0a   to MacOSX..** .
e6b10 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
e6b20 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68  ****** End of th
e6b30 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 69 6d 70  e proxy lock imp
e6b40 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a  lementation ****
e6b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6b60 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
e6b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6bb0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  */../*.** Initia
e6bc0 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69  lize the operati
e6bd0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
e6be0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ace..**.** This 
e6bf0 72 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65 72  routine register
e6c00 73 20 61 6c 6c 20 56 46 53 20 69 6d 70 6c 65 6d  s all VFS implem
e6c10 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 75 6e  entations for un
e6c20 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e  ix-like operatin
e6c30 67 0a 2a 2a 20 73 79 73 74 65 6d 73 2e 20 20 54  g.** systems.  T
e6c40 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 64  his routine, and
e6c50 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f   the sqlite3_os_
e6c60 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 20 74 68  end() routine th
e6c70 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a 2a 2a 20 73  at follows,.** s
e6c80 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c  hould be the onl
e6c90 79 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  y routines in th
e6ca0 69 73 20 66 69 6c 65 20 74 68 61 74 20 61 72 65  is file that are
e6cb0 20 76 69 73 69 62 6c 65 20 66 72 6f 6d 20 6f 74   visible from ot
e6cc0 68 65 72 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a  her.** files..**
e6cd0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
e6ce0 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   is called once 
e6cf0 64 75 72 69 6e 67 20 53 51 4c 69 74 65 20 69 6e  during SQLite in
e6d00 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64  itialization and
e6d10 20 62 79 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20   by a.** single 
e6d20 74 68 72 65 61 64 2e 20 20 54 68 65 20 6d 65 6d  thread.  The mem
e6d30 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  ory allocation a
e6d40 6e 64 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  nd mutex subsyst
e6d50 65 6d 73 20 68 61 76 65 20 6e 6f 74 0a 2a 2a 20  ems have not.** 
e6d60 6e 65 63 65 73 73 61 72 69 6c 79 20 62 65 65 6e  necessarily been
e6d70 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65   initialized whe
e6d80 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
e6d90 73 20 63 61 6c 6c 65 64 2c 20 61 6e 64 20 73 6f  s called, and so
e6da0 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20   they.** should 
e6db0 6e 6f 74 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a  not be used..*/.
e6dc0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
e6dd0 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76  qlite3_os_init(v
e6de0 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 0a 20 20 2a  oid){ .  /* .  *
e6df0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
e6e00 6d 61 63 72 6f 20 64 65 66 69 6e 65 73 20 61 6e  macro defines an
e6e10 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 66 6f 72   initializer for
e6e20 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 66 73 20   an sqlite3_vfs 
e6e30 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 54 68 65  object..  ** The
e6e40 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 56 46 53   name of the VFS
e6e50 20 69 73 20 4e 41 4d 45 2e 20 20 54 68 65 20 70   is NAME.  The p
e6e60 41 70 70 44 61 74 61 20 69 73 20 61 20 70 6f 69  AppData is a poi
e6e70 6e 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74 65  nter to a pointe
e6e80 72 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 66  r.  ** to the "f
e6e90 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 2e  inder" function.
e6ea0 20 20 28 70 41 70 70 44 61 74 61 20 69 73 20 61    (pAppData is a
e6eb0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f   pointer to a po
e6ec0 69 6e 74 65 72 20 62 65 63 61 75 73 65 0a 20 20  inter because.  
e6ed0 2a 2a 20 73 69 6c 6c 79 20 43 39 30 20 72 75 6c  ** silly C90 rul
e6ee0 65 73 20 70 72 6f 68 69 62 69 74 20 61 20 76 6f  es prohibit a vo
e6ef0 69 64 2a 20 66 72 6f 6d 20 62 65 69 6e 67 20 63  id* from being c
e6f00 61 73 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ast to a functio
e6f10 6e 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 61  n pointer.  ** a
e6f20 6e 64 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f  nd so we have to
e6f30 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 65 20   go through the 
e6f40 69 6e 74 65 72 6d 65 64 69 61 74 65 20 70 6f 69  intermediate poi
e6f50 6e 74 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72  nter to avoid pr
e6f60 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 77 68 65 6e  oblems.  ** when
e6f70 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68 20   compiling with 
e6f80 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 73  -pedantic-errors
e6f90 20 6f 6e 20 47 43 43 2e 29 0a 20 20 2a 2a 0a 20   on GCC.).  **. 
e6fa0 20 2a 2a 20 54 68 65 20 46 49 4e 44 45 52 20 70   ** The FINDER p
e6fb0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73  arameter to this
e6fc0 20 6d 61 63 72 6f 20 69 73 20 74 68 65 20 6e 61   macro is the na
e6fd0 6d 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65  me of the pointe
e6fe0 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 66 69  r to the.  ** fi
e6ff0 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 2e 20 20  nder-function.  
e7000 54 68 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74  The finder-funct
e7010 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ion returns a po
e7020 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a  inter to the.  *
e7030 2a 20 73 71 6c 69 74 65 5f 69 6f 5f 6d 65 74 68  * sqlite_io_meth
e7040 6f 64 73 20 6f 62 6a 65 63 74 20 74 68 61 74 20  ods object that 
e7050 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64  implements the d
e7060 65 73 69 72 65 64 20 6c 6f 63 6b 69 6e 67 0a 20  esired locking. 
e7070 20 2a 2a 20 62 65 68 61 76 69 6f 72 73 2e 20 20   ** behaviors.  
e7080 53 65 65 20 74 68 65 20 64 69 76 69 73 69 6f 6e  See the division
e7090 20 61 62 6f 76 65 20 74 68 61 74 20 63 6f 6e 74   above that cont
e70a0 61 69 6e 73 20 74 68 65 20 49 4f 4d 45 54 48 4f  ains the IOMETHO
e70b0 44 53 0a 20 20 2a 2a 20 6d 61 63 72 6f 20 66 6f  DS.  ** macro fo
e70c0 72 20 61 64 64 69 74 69 6f 6e 20 69 6e 66 6f 72  r addition infor
e70d0 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 6e 64 65 72  mation on finder
e70e0 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a  -functions..  **
e70f0 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65  .  ** Most finde
e7100 72 73 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e  rs simply return
e7110 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
e7120 66 69 78 65 64 20 73 71 6c 69 74 65 33 5f 69 6f  fixed sqlite3_io
e7130 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 2a 20 6f 62  _methods.  ** ob
e7140 6a 65 63 74 2e 20 20 42 75 74 20 74 68 65 20 22  ject.  But the "
e7150 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  autolockIoFinder
e7160 22 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d  " available on M
e7170 61 63 4f 53 58 20 64 6f 65 73 20 61 20 6c 69 74  acOSX does a lit
e7180 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 65 20 74 68  tle.  ** more th
e7190 61 6e 20 74 68 61 74 3b 20 69 74 20 6c 6f 6f 6b  an that; it look
e71a0 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 79 73  s at the filesys
e71b0 74 65 6d 20 74 79 70 65 20 74 68 61 74 20 68 6f  tem type that ho
e71c0 73 74 73 20 74 68 65 20 0a 20 20 2a 2a 20 64 61  sts the .  ** da
e71d0 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
e71e0 74 72 69 65 73 20 74 6f 20 63 68 6f 6f 73 65 20  tries to choose 
e71f0 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  an locking metho
e7200 64 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  d appropriate fo
e7210 72 0a 20 20 2a 2a 20 74 68 61 74 20 66 69 6c 65  r.  ** that file
e7220 73 79 73 74 65 6d 20 74 69 6d 65 2e 0a 20 20 2a  system time..  *
e7230 2f 0a 20 20 23 64 65 66 69 6e 65 20 55 4e 49 58  /.  #define UNIX
e7240 56 46 53 28 56 46 53 4e 41 4d 45 2c 20 46 49 4e  VFS(VFSNAME, FIN
e7250 44 45 52 29 20 7b 20 20 20 20 20 20 20 20 20 20  DER) {          
e7260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e7270 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20      1,          
e7280 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
e7290 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20  rsion */        
e72a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e72b0 20 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c    sizeof(unixFil
e72c0 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a 4f 73 46  e),     /* szOsF
e72d0 69 6c 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ile */          
e72e0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
e72f0 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 20 20  MAX_PATHNAME,   
e7300 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e        /* mxPathn
e7310 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ame */          
e7320 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 30 2c          \.    0,
e7330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7340 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 20      /* pNext */ 
e7350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7360 20 20 20 20 20 20 5c 0a 20 20 20 20 56 46 53 4e        \.    VFSN
e7370 41 4d 45 2c 20 20 20 20 20 20 20 20 20 20 20 20  AME,            
e7380 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 20 20 20    /* zName */   
e7390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e73a0 20 20 20 20 5c 0a 20 20 20 20 28 76 6f 69 64 2a      \.    (void*
e73b0 29 26 46 49 4e 44 45 52 2c 20 20 20 20 20 20 20  )&FINDER,       
e73c0 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 20 20  /* pAppData */  
e73d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e73e0 20 20 5c 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e    \.    unixOpen
e73f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
e7400 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20   xOpen */       
e7410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7420 5c 0a 20 20 20 20 75 6e 69 78 44 65 6c 65 74 65  \.    unixDelete
e7430 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  ,           /* x
e7440 44 65 6c 65 74 65 20 2a 2f 20 20 20 20 20 20 20  Delete */       
e7450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e7460 20 20 20 20 75 6e 69 78 41 63 63 65 73 73 2c 20      unixAccess, 
e7470 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63            /* xAc
e7480 63 65 73 73 20 2a 2f 20 20 20 20 20 20 20 20 20  cess */         
e7490 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e74a0 20 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61    unixFullPathna
e74b0 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46 75 6c 6c  me,     /* xFull
e74c0 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20  Pathname */     
e74d0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
e74e0 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 20 20 20 20  unixDlOpen,     
e74f0 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e        /* xDlOpen
e7500 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e7510 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
e7520 69 78 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20  ixDlError,      
e7530 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20      /* xDlError 
e7540 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e7550 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
e7560 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20  DlSym,          
e7570 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 20 20    /* xDlSym */  
e7580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7590 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c      \.    unixDl
e75a0 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
e75b0 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 20 20  /* xDlClose */  
e75c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e75d0 20 20 5c 0a 20 20 20 20 75 6e 69 78 52 61 6e 64    \.    unixRand
e75e0 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 20 2f 2a  omness,       /*
e75f0 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 20   xRandomness */ 
e7600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7610 5c 0a 20 20 20 20 75 6e 69 78 53 6c 65 65 70 2c  \.    unixSleep,
e7620 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e7630 53 6c 65 65 70 20 2a 2f 20 20 20 20 20 20 20 20  Sleep */        
e7640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e7650 20 20 20 20 75 6e 69 78 43 75 72 72 65 6e 74 54      unixCurrentT
e7660 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20 78 43 75  ime,      /* xCu
e7670 72 72 65 6e 74 54 69 6d 65 20 2a 2f 20 20 20 20  rrentTime */    
e7680 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e7690 20 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72    unixGetLastErr
e76a0 6f 72 20 20 20 20 20 20 2f 2a 20 78 47 65 74 4c  or      /* xGetL
e76b0 61 73 74 45 72 72 6f 72 20 2a 2f 20 20 20 20 20  astError */     
e76c0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a            \.  }.
e76d0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 20 64  .  /*.  ** All d
e76e0 65 66 61 75 6c 74 20 56 46 53 65 73 20 66 6f 72  efault VFSes for
e76f0 20 75 6e 69 78 20 61 72 65 20 63 6f 6e 74 61 69   unix are contai
e7700 6e 65 64 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ned in the follo
e7710 77 69 6e 67 20 61 72 72 61 79 2e 0a 20 20 2a 2a  wing array..  **
e7720 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  .  ** Note that 
e7730 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 2e  the sqlite3_vfs.
e7740 70 4e 65 78 74 20 66 69 65 6c 64 20 6f 66 20 74  pNext field of t
e7750 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 69 73  he VFS object is
e7760 20 6d 6f 64 69 66 69 65 64 0a 20 20 2a 2a 20 62   modified.  ** b
e7770 79 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72  y the SQLite cor
e7780 65 20 77 68 65 6e 20 74 68 65 20 56 46 53 20 69  e when the VFS i
e7790 73 20 72 65 67 69 73 74 65 72 65 64 2e 20 20 53  s registered.  S
e77a0 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  o the following.
e77b0 20 20 2a 2a 20 61 72 72 61 79 20 63 61 6e 6e 6f    ** array canno
e77c0 74 20 62 65 20 63 6f 6e 73 74 2e 0a 20 20 2a 2f  t be const..  */
e77d0 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
e77e0 33 5f 76 66 73 20 61 56 66 73 5b 5d 20 3d 20 7b  3_vfs aVfs[] = {
e77f0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
e7800 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e7810 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   && defined(__AP
e7820 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 58 56  PLE__).    UNIXV
e7830 46 53 28 22 75 6e 69 78 22 2c 20 20 20 20 20 20  FS("unix",      
e7840 20 20 20 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69      autolockIoFi
e7850 6e 64 65 72 20 29 2c 0a 23 65 6c 73 65 0a 20 20  nder ),.#else.  
e7860 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22    UNIXVFS("unix"
e7870 2c 20 20 20 20 20 20 20 20 20 20 70 6f 73 69 78  ,          posix
e7880 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64  IoFinder ),.#end
e7890 69 66 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22  if.    UNIXVFS("
e78a0 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20 20 20 20  unix-none",     
e78b0 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29  nolockIoFinder )
e78c0 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  ,.    UNIXVFS("u
e78d0 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c 20 20 64  nix-dotfile",  d
e78e0 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29  otlockIoFinder )
e78f0 2c 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  ,.#if OS_VXWORKS
e7900 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e  .    UNIXVFS("un
e7910 69 78 2d 6e 61 6d 65 64 73 65 6d 22 2c 20 73 65  ix-namedsem", se
e7920 6d 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e  mIoFinder ),.#en
e7930 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  dif.#if SQLITE_E
e7940 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e7950 59 4c 45 0a 20 20 20 20 55 4e 49 58 56 46 53 28  YLE.    UNIXVFS(
e7960 22 75 6e 69 78 2d 70 6f 73 69 78 22 2c 20 20 20  "unix-posix",   
e7970 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29   posixIoFinder )
e7980 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  ,.    UNIXVFS("u
e7990 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 20 66  nix-flock",    f
e79a0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a  lockIoFinder ),.
e79b0 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
e79c0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
e79d0 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
e79e0 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20  d(__APPLE__).   
e79f0 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 61   UNIXVFS("unix-a
e7a00 66 70 22 2c 20 20 20 20 20 20 61 66 70 49 6f 46  fp",      afpIoF
e7a10 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49  inder ),.    UNI
e7a20 58 56 46 53 28 22 75 6e 69 78 2d 70 72 6f 78 79  XVFS("unix-proxy
e7a30 22 2c 20 20 20 20 70 72 6f 78 79 49 6f 46 69 6e  ",    proxyIoFin
e7a40 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20  der ),.#endif.  
e7a50 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  };.  unsigned in
e7a60 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  t i;          /*
e7a70 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
e7a80 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
e7a90 61 6c 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65  all VFSes define
e7aa0 64 20 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20  d in the aVfs[] 
e7ab0 61 72 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69  array */.  for(i
e7ac0 3d 30 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56  =0; i<(sizeof(aV
e7ad0 66 73 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74  fs)/sizeof(sqlit
e7ae0 65 33 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a  e3_vfs)); i++){.
e7af0 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f      sqlite3_vfs_
e7b00 72 65 67 69 73 74 65 72 28 26 61 56 66 73 5b 69  register(&aVfs[i
e7b10 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20  ], i==0);.  }.  
e7b20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e7b30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74  ; .}../*.** Shut
e7b40 64 6f 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69  down the operati
e7b50 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
e7b60 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20  ace..**.** Some 
e7b70 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
e7b80 73 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20  s might need to 
e7b90 64 6f 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20  do some cleanup 
e7ba0 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  in this routine,
e7bb0 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64  .** to release d
e7bc0 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
e7bd0 61 74 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42  ated objects.  B
e7be0 75 74 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a  ut not on unix..
e7bf0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
e7c00 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75  is a no-op for u
e7c10 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  nix..*/.SQLITE_A
e7c20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
e7c30 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20  s_end(void){ .  
e7c40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e7c50 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a  ; .}. .#endif /*
e7c60 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20   SQLITE_OS_UNIX 
e7c70 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
e7c80 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e  *** End of os_un
e7c90 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ix.c ***********
e7ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e7cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e7cc0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
e7cd0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
e7ce0 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_win.c ********
e7cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e7d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e7d10 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
e7d20 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 22.**.** The 
e7d30 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
e7d40 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
e7d50 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
e7d60 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
e7d70 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
e7d80 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
e7d90 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
e7da0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
e7db0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
e7dc0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
e7dd0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
e7de0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
e7df0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
e7e00 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
e7e10 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
e7e20 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
e7e30 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
e7e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e7e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e7e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e7e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e7e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
e7e90 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
e7ea0 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ns code that is 
e7eb0 73 70 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64  specific to wind
e7ec0 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ows..**.** $Id: 
e7ed0 6f 73 5f 77 69 6e 2e 63 2c 76 20 31 2e 31 35 33  os_win.c,v 1.153
e7ee0 20 32 30 30 39 2f 30 33 2f 33 31 20 30 33 3a 34   2009/03/31 03:4
e7ef0 31 3a 35 37 20 73 68 61 6e 65 20 45 78 70 20 24  1:57 shane Exp $
e7f00 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
e7f10 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20  S_WIN           
e7f20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65      /* This file
e7f30 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e   is used for win
e7f40 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f  dows only */.../
e7f50 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75  *.** A Note Abou
e7f60 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  t Memory Allocat
e7f70 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion:.**.** This 
e7f80 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c  driver uses mall
e7f90 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65  oc()/free() dire
e7fa0 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e  ctly rather than
e7fb0 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a   going through.*
e7fc0 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61  * the SQLite-wra
e7fd0 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61  ppers sqlite3_ma
e7fe0 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66  lloc()/sqlite3_f
e7ff0 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72  ree().  Those wr
e8000 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65  appers.** are de
e8010 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f  signed for use o
e8020 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
e8030 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20  ms where memory 
e8040 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a  is scarce and.**
e8050 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
e8060 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74   happen frequent
e8070 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20  ly.  Win32 does 
e8080 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75  not typically ru
e8090 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64  n on.** embedded
e80a0 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68   systems, and wh
e80b0 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64  en it does the d
e80c0 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c  evelopers normal
e80d0 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a  ly have bigger.*
e80e0 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f  * problems to wo
e80f0 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72  rry about than r
e8100 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65  unning out of me
e8110 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20  mory.  So there 
e8120 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70  is not.** a comp
e8130 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75  elling need to u
e8140 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e  se the wrappers.
e8150 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65  .**.** But there
e8160 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f   is a good reaso
e8170 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65  n to not use the
e8180 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77   wrappers.  If w
e8190 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61  e use the.** wra
e81a0 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69  ppers then we wi
e81b0 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64  ll get simulated
e81c0 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
e81d0 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a  es within this.*
e81e0 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74  * driver.  And t
e81f0 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b  hat causes all k
e8200 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73  inds of problems
e8210 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20   for our tests. 
e8220 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68   We.** could enh
e8230 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64  ance SQLite to d
e8240 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74  eal with simulat
e8250 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ed malloc failur
e8260 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65  es within.** the
e8270 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20   OS driver, but 
e8280 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c  the code to deal
e8290 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c   with those fail
e82a0 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a  ure would not.**
e82b0 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e   be exercised on
e82c0 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f   Linux (which do
e82d0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
e82e0 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64  alloc() in the d
e82f0 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f  river).** and so
e8300 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64   we would have d
e8310 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e  ifficulty writin
e8320 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73  g coverage tests
e8330 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64   for that.** cod
e8340 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65  e.  Better to le
e8350 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74  ave the code out
e8360 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a  , we think..**.*
e8370 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74  * The point of t
e8380 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69  his discussion i
e8390 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57  s as follows:  W
e83a0 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e  hen creating a n
e83b0 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66  ew.** OS layer f
e83c0 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73  or an embedded s
e83d0 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73  ystem, if you us
e83e0 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61  e this file as a
e83f0 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76  n example,.** av
e8400 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d  oid the use of m
e8410 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20  alloc()/free(). 
e8420 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20   Those routines 
e8430 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f  work ok on windo
e8440 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62  ws.** desktops b
e8450 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69  ut not so well i
e8460 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
e8470 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65  ms..*/..#include
e8480 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69   <winbase.h>..#i
e8490 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a  fdef __CYGWIN__.
e84a0 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63  # include <sys/c
e84b0 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a  ygwin.h>.#endif.
e84c0 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73  ./*.** Macros us
e84d0 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
e84e0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
e84f0 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a  o use threads..*
e8500 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  /.#if defined(TH
e8510 52 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52  READSAFE) && THR
e8520 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65  EADSAFE.# define
e8530 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45   SQLITE_W32_THRE
e8540 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ADS 1.#endif../*
e8550 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65  .** Include code
e8560 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
e8570 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69  to all os_*.c fi
e8580 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  les.*/./********
e8590 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f  ****** Include o
e85a0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68  s_common.h in th
e85b0 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77  e middle of os_w
e85c0 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
e85d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
e85e0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
e85f0 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  e os_common.h **
e8600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8620 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
e8630 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54  4 May 22.**.** T
e8640 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
e8650 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
e8660 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
e8670 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
e8680 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
e8690 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
e86a0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
e86b0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
e86c0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
e86d0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
e86e0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
e86f0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
e8700 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
e8710 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
e8720 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
e8730 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
e8740 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
e8750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
e87a0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
e87b0 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64  tains macros and
e87c0 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66   a little bit of
e87d0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
e87e0 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f  mmon to.** all o
e87f0 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73  f the platform-s
e8800 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f  pecific files (o
e8810 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69  s_*.c) and is #i
e8820 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f  ncluded into tho
e8830 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a  se.** files..**.
e8840 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f  ** This file sho
e8850 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64  uld be #included
e8860 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66   by the os_*.c f
e8870 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69  iles only.  It i
e8880 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72  s not a.** gener
e8890 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65  al purpose heade
e88a0 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  r file..**.** $I
e88b0 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76  d: os_common.h,v
e88c0 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34   1.38 2009/02/24
e88d0 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c   18:40:50 daniel
e88e0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23  k1977 Exp $.*/.#
e88f0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f  ifndef _OS_COMMO
e8900 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53  N_H_.#define _OS
e8910 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a  _COMMON_H_../*.*
e8920 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62  * At least two b
e8930 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64  ugs have slipped
e8940 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63   in because we c
e8950 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52  hanged the MEMOR
e8960 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f  Y_DEBUG.** macro
e8970 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47   to SQLITE_DEBUG
e8980 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20   and some older 
e8990 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e  makefiles have n
e89a0 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a  ot yet made the.
e89b0 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20  ** switch.  The 
e89c0 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73  following code s
e89d0 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73  hould catch this
e89e0 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70   problem at comp
e89f0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66  ile-time..*/.#if
e8a00 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  def MEMORY_DEBUG
e8a10 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45  .# error "The ME
e8a20 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f  MORY_DEBUG macro
e8a30 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55   is obsolete.  U
e8a40 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  se SQLITE_DEBUG 
e8a50 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66  instead.".#endif
e8a60 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
e8a70 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
e8a80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
e8a90 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65  OSTrace = 0;.#de
e8aa0 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29  fine OSTRACE1(X)
e8ab0 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
e8ac0 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
e8ad0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
e8ae0 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  (X).#define OSTR
e8af0 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20  ACE2(X,Y)       
e8b00 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
e8b10 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
e8b20 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65  gPrintf(X,Y).#de
e8b30 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
e8b40 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c  Y,Z)     if( sql
e8b50 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
e8b60 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
e8b70 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
e8b80 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
e8b90 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f  )   if( sqlite3O
e8ba0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
e8bb0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
e8bc0 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Z,A).#define OST
e8bd0 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29  RACE5(X,Y,Z,A,B)
e8be0 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
e8bf0 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
e8c00 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
e8c10 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
e8c20 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
e8c30 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
e8c40 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
e8c50 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
e8c60 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69  Y,Z,A,B,C).#defi
e8c70 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c  ne OSTRACE7(X,Y,
e8c80 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20  Z,A,B,C,D) \.   
e8c90 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61   if(sqlite3OSTra
e8ca0 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
e8cb0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
e8cc0 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66  ,C,D).#else.#def
e8cd0 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a  ine OSTRACE1(X).
e8ce0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
e8cf0 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53  (X,Y).#define OS
e8d00 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64  TRACE3(X,Y,Z).#d
e8d10 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58  efine OSTRACE4(X
e8d20 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20  ,Y,Z,A).#define 
e8d30 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41  OSTRACE5(X,Y,Z,A
e8d40 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
e8d50 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
e8d60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
e8d70 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
e8d80 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
e8d90 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f  Macros for perfo
e8da0 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20  rmance tracing. 
e8db0 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64   Normally turned
e8dc0 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b   off.  Only work
e8dd0 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72  s.** on i486 har
e8de0 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  dware..*/.#ifdef
e8df0 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41   SQLITE_PERFORMA
e8e00 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a  NCE_TRACE../* .*
e8e10 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61  * hwtime.h conta
e8e20 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d  ins inline assem
e8e30 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d  bler code for im
e8e40 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68  plementing .** h
e8e50 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20  igh-performance 
e8e60 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e  timing routines.
e8e70 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
e8e80 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
e8e90 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
e8ea0 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  le of os_common.
e8eb0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
e8ec0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
e8ed0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
e8ee0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
e8ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8f10 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d  **/./*.** 2008 M
e8f20 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 27.**.** The 
e8f30 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
e8f40 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
e8f50 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
e8f60 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
e8f70 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
e8f80 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
e8f90 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
e8fa0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
e8fb0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
e8fc0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
e8fd0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
e8fe0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
e8ff0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
e9000 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
e9010 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
e9020 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
e9030 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
e9040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9080 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
e9090 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
e90a0 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f  ns inline asm co
e90b0 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e  de for retrievin
e90c0 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61  g "high-performa
e90d0 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73  nce".** counters
e90e0 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43   for x86 class C
e90f0 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  PUs..**.** $Id: 
e9100 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32  hwtime.h,v 1.3 2
e9110 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a  008/08/01 14:33:
e9120 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  15 shane Exp $.*
e9130 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d  /.#ifndef _HWTIM
e9140 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57  E_H_.#define _HW
e9150 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54  TIME_H_../*.** T
e9160 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
e9170 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tine only works 
e9180 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73  on pentium-class
e9190 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63   (or newer) proc
e91a0 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73  essors..** It us
e91b0 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63  es the RDTSC opc
e91c0 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20  ode to read the 
e91d0 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75  cycle count valu
e91e0 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20  e out of the.** 
e91f0 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65  processor and re
e9200 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65  turns that value
e9210 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
e9220 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73  sed for high-res
e9230 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a  .** profiling..*
e9240 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f  /.#if (defined(_
e9250 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69  _GNUC__) || defi
e9260 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26  ned(_MSC_VER)) &
e9270 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e  & \.      (defin
e9280 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69  ed(i386) || defi
e9290 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c  ned(__i386__) ||
e92a0 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36   defined(_M_IX86
e92b0 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65  ))..  #if define
e92c0 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f  d(__GNUC__)..  _
e92d0 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
e92e0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
e92f0 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
e9300 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c    unsigned int l
e9310 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73  o, hi;.     __as
e9320 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
e9330 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22   ("rdtsc" : "=a"
e9340 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29   (lo), "=d" (hi)
e9350 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28  );.     return (
e9360 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69  sqlite_uint64)hi
e9370 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d   << 32 | lo;.  }
e9380 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65  ..  #elif define
e9390 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f  d(_MSC_VER)..  _
e93a0 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29  _declspec(naked)
e93b0 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65   __inline sqlite
e93c0 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20  _uint64 __cdecl 
e93d0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
e93e0 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20  id){.     __asm 
e93f0 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a  {.        rdtsc.
e9400 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20          ret     
e9410 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65    ; return value
e9420 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20   at EDX:EAX.    
e9430 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66   }.  }..  #endif
e9440 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64  ..#elif (defined
e9450 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
e9460 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f  fined(__x86_64__
e9470 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
e9480 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
e9490 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
e94a0 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
e94b0 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20  ed long val;.   
e94c0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
e94d0 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
e94e0 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a   : "=A" (val));.
e94f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c        return val
e9500 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64  ;.  }. .#elif (d
e9510 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
e9520 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70   && defined(__pp
e9530 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  c__))..  __inlin
e9540 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
e9550 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
e9560 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
e9570 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
e9580 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e  retval;.      un
e9590 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b  signed long junk
e95a0 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
e95b0 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c  __volatile__ ("\
e95c0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20  n\.          1: 
e95d0 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c       mftbu   %1\
e95e0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
e95f0 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30       mftb    %L0
e9600 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
e9610 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30        mftbu   %0
e9620 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
e9630 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30        cmpw    %0
e9640 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  ,%1\n\.         
e9650 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20           bne    
e9660 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20   1b".           
e9670 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72         : "=r" (r
e9680 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75  etval), "=r" (ju
e9690 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  nk));.      retu
e96a0 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a  rn retval;.  }..
e96b0 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20  #else..  #error 
e96c0 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74  Need implementat
e96d0 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77  ion of sqlite3Hw
e96e0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
e96f0 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a  platform...  /*.
e9700 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20    ** To compile 
e9710 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e  without implemen
e9720 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69  ting sqlite3Hwti
e9730 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
e9740 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75  atform,.  ** you
e9750 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20   can remove the 
e9760 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64  above #error and
e9770 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69   use the followi
e9780 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e  ng.  ** stub fun
e9790 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c  ction.  You will
e97a0 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70   lose timing sup
e97b0 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20  port for many.  
e97c0 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67  ** of the debugg
e97d0 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20  ing and testing 
e97e0 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69  utilities, but i
e97f0 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a  t should at.  **
e9800 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61   least compile a
e9810 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c  nd run..  */.SQL
e9820 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71  ITE_PRIVATE   sq
e9830 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
e9840 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
e9850 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65   return ((sqlite
e9860 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23  _uint64)0); }..#
e9870 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
e9880 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d   !defined(_HWTIM
e9890 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  E_H_) */../*****
e98a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
e98b0 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
e98c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e98d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e98e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
e98f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
e9900 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
e9910 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d  ft off in os_com
e9920 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mon.h **********
e9930 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69  ********/..stati
e9940 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
e9950 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20  g_start;.static 
e9960 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
e9970 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65  elapsed;.#define
e9980 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20   TIMER_START    
e9990 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74     g_start=sqlit
e99a0 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69  e3Hwtime().#defi
e99b0 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20  ne TIMER_END    
e99c0 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73       g_elapsed=s
e99d0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67  qlite3Hwtime()-g
e99e0 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54  _start.#define T
e99f0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20  IMER_ELAPSED    
e9a00 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65   g_elapsed.#else
e9a10 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53  .#define TIMER_S
e9a20 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d  TART.#define TIM
e9a30 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54  ER_END.#define T
e9a40 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20  IMER_ELAPSED    
e9a50 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34   ((sqlite_uint64
e9a60 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  )0).#endif../*.*
e9a70 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20  * If we compile 
e9a80 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
e9a90 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20  TEST macro set, 
e9aa0 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  then the followi
e9ab0 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63  ng block.** of c
e9ac0 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73  ode will give us
e9ad0 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20   the ability to 
e9ae0 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20  simulate a disk 
e9af0 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73  I/O error.  This
e9b00 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20  .** is used for 
e9b10 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20  testing the I/O 
e9b20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a  recovery logic..
e9b30 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
e9b40 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
e9b50 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
e9b60 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20  error_hit = 0;  
e9b70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
e9b80 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f  al number of I/O
e9b90 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54   Errors */.SQLIT
e9ba0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
e9bb0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68  3_io_error_hardh
e9bc0 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  it = 0;        /
e9bd0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d  * Number of non-
e9be0 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f  benign errors */
e9bf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
e9c00 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
e9c10 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20  _pending = 0;   
e9c20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f       /* Count do
e9c30 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20  wn to first I/O 
e9c40 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f  error */.SQLITE_
e9c50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
e9c60 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74  io_error_persist
e9c70 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
e9c80 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f  True if I/O erro
e9c90 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51  rs persist */.SQ
e9ca0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
e9cb0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65  ite3_io_error_be
e9cc0 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  nign = 0;       
e9cd0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72    /* True if err
e9ce0 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a  ors are benign *
e9cf0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
e9d00 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
e9d10 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53  l_pending = 0;.S
e9d20 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e9d30 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d  lite3_diskfull =
e9d40 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75   0;.#define Simu
e9d50 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
e9d60 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f  n(X) sqlite3_io_
e9d70 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29  error_benign=(X)
e9d80 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
e9d90 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20  eIOError(CODE)  
e9da0 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33  \.  if( (sqlite3
e9db0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73  _io_error_persis
e9dc0 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f  t && sqlite3_io_
e9dd0 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20  error_hit) \.   
e9de0 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69      || sqlite3_i
e9df0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d  o_error_pending-
e9e00 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20  - == 1 )  \.    
e9e10 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61            { loca
e9e20 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b  l_ioerr(); CODE;
e9e30 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c   }.static void l
e9e40 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20  ocal_ioerr(){.  
e9e50 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c  IOTRACE(("IOERR\
e9e60 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  n"));.  sqlite3_
e9e70 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a  io_error_hit++;.
e9e80 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69    if( !sqlite3_i
e9e90 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29  o_error_benign )
e9ea0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
e9eb0 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23  r_hardhit++;.}.#
e9ec0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
e9ed0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44  iskfullError(COD
e9ee0 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69  E) \.   if( sqli
e9ef0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
e9f00 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69  ding ){ \.     i
e9f10 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
e9f20 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31  ull_pending == 1
e9f30 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63   ){ \.       loc
e9f40 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20  al_ioerr(); \.  
e9f50 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
e9f60 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20  kfull = 1; \.   
e9f70 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65      sqlite3_io_e
e9f80 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a  rror_hit = 1; \.
e9f90 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20         CODE; \. 
e9fa0 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20      }else{ \.   
e9fb0 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b      sqlite3_disk
e9fc0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20  full_pending--; 
e9fd0 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a  \.     } \.   }.
e9fe0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69  #else.#define Si
e9ff0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
ea000 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53  ign(X).#define S
ea010 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41  imulateIOError(A
ea020 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
ea030 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
ea040 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  A).#endif../*.**
ea050 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b   When testing, k
ea060 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74  eep a count of t
ea070 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65  he number of ope
ea080 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64  n files..*/.#ifd
ea090 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
ea0a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
ea0b0 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
ea0c0 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69  count = 0;.#defi
ea0d0 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
ea0e0 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  )  sqlite3_open_
ea0f0 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a  file_count+=(X).
ea100 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70  #else.#define Op
ea110 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e  enCounter(X).#en
ea120 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
ea130 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d  defined(_OS_COMM
ea140 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ON_H_) */../****
ea150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
ea160 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
ea170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
ea1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
ea1b0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
ea1c0 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69  eft off in os_wi
ea1d0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
ea1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
ea1f0 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74  * Some microsoft
ea200 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20   compilers lack 
ea210 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e  this definition.
ea220 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41  .*/.#ifndef INVA
ea230 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
ea240 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56  TES.# define INV
ea250 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
ea260 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29  UTES ((DWORD)-1)
ea270 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20   .#endif../*.** 
ea280 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20  Determine if we 
ea290 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
ea2a0 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69   WindowsCE - whi
ea2b0 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a  ch has a much.**
ea2c0 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f   reduced API..*/
ea2d0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
ea2e0 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72  INCE.# define Ar
ea2f0 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20  eFileApisANSI() 
ea300 31 0a 23 20 64 65 66 69 6e 65 20 47 65 74 44 69  1.# define GetDi
ea310 73 6b 46 72 65 65 53 70 61 63 65 57 28 29 20 30  skFreeSpaceW() 0
ea320 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
ea330 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76  inCE lacks nativ
ea340 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69  e support for fi
ea350 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65  le locking so we
ea360 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74   have to fake it
ea370 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f  .** with some co
ea380 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a  de of our own..*
ea390 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
ea3a0 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74  WINCE.typedef st
ea3b0 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b  ruct winceLock {
ea3c0 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b  .  int nReaders;
ea3d0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
ea3e0 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73   of reader locks
ea3f0 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42   obtained */.  B
ea400 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20  OOL bPending;   
ea410 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20     /* Indicates 
ea420 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68  a pending lock h
ea430 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64  as been obtained
ea440 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65   */.  BOOL bRese
ea450 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64  rved;     /* Ind
ea460 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65  icates a reserve
ea470 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20  d lock has been 
ea480 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f  obtained */.  BO
ea490 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20  OL bExclusive;  
ea4a0 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61    /* Indicates a
ea4b0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
ea4c0 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e   has been obtain
ea4d0 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63  ed */.} winceLoc
ea4e0 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  k;.#endif../*.**
ea4f0 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72   The winFile str
ea500 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63  ucture is a subc
ea510 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  lass of sqlite3_
ea520 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74  file* specific t
ea530 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70  o the win32.** p
ea540 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72  ortability layer
ea550 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
ea560 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46  uct winFile winF
ea570 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46  ile;.struct winF
ea580 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  ile {.  const sq
ea590 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
ea5a0 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73   *pMethod;/* Mus
ea5b0 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20  t be first */.  
ea5c0 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20  HANDLE h;       
ea5d0 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
ea5e0 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  e for accessing 
ea5f0 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e  the file */.  un
ea600 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b  signed char lock
ea610 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66  type; /* Type of
ea620 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20   lock currently 
ea630 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c  held on this fil
ea640 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61  e */.  short sha
ea650 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f  redLockByte;   /
ea660 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65  * Randomly chose
ea670 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61  n byte used as a
ea680 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a   shared lock */.
ea690 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e    DWORD lastErrn
ea6a0 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  o;        /* The
ea6b0 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 66   Windows errno f
ea6c0 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f  rom the last I/O
ea6d0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f 52   error */.  DWOR
ea6e0 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20  D sectorSize;   
ea6f0 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69      /* Sector si
ea700 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65  ze of the device
ea710 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a 23   file is on */.#
ea720 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ea730 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c  CE.  WCHAR *zDel
ea740 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20  eteOnClose;  /* 
ea750 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
ea760 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73  delete when clos
ea770 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20  ing */.  HANDLE 
ea780 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  hMutex;         
ea790 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74   /* Mutex used t
ea7a0 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
ea7b0 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20   to shared lock 
ea7c0 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53  */  .  HANDLE hS
ea7d0 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f  hared;         /
ea7e0 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * Shared memory 
ea7f0 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72  segment used for
ea800 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69   locking */.  wi
ea810 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20  nceLock local;  
ea820 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f        /* Locks o
ea830 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20  btained by this 
ea840 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46  instance of winF
ea850 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f  ile */.  winceLo
ea860 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20  ck *shared;     
ea870 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65   /* Global share
ea880 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f  d lock memory fo
ea890 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23  r the file  */.#
ea8a0 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
ea8b0 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 70  Forward prototyp
ea8c0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
ea8d0 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28  t getSectorSize(
ea8e0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
ea8f0 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73   *pVfs,.    cons
ea900 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
ea910 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66  e     /* UTF-8 f
ea920 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a  ile name */.);..
ea930 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
ea940 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20  ing variable is 
ea950 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f  (normally) set o
ea960 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68  nce and never ch
ea970 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66  anges.** thereaf
ea980 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73  ter.  It records
ea990 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65   whether the ope
ea9a0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73  rating system is
ea9b0 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e   Win95.** or Win
ea9c0 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f  NT..**.** 0:   O
ea9d0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
ea9e0 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20  unknown..** 1:  
ea9f0 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   Operating syste
eaa00 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32  m is Win95..** 2
eaa10 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79  :   Operating sy
eaa20 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a  stem is WinNT..*
eaa30 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f  *.** In order to
eaa40 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74   facilitate test
eaa50 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73  ing on a WinNT s
eaa60 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20  ystem, the test 
eaa70 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d  fixture.** can m
eaa80 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73  anually set this
eaa90 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65   value to 1 to e
eaaa0 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68  mulate Win98 beh
eaab0 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66  avior..*/.#ifdef
eaac0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
eaad0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
eaae0 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b  te3_os_type = 0;
eaaf0 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e  .#else.static in
eab00 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70  t sqlite3_os_typ
eab10 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  e = 0;.#endif../
eab20 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
eab30 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77   (non-zero) if w
eab40 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e  e are running un
eab50 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b  der WinNT, Win2K
eab60 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57  , WinXP,.** or W
eab70 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61  inCE.  Return fa
eab80 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57  lse (zero) for W
eab90 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20  in95, Win98, or 
eaba0 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72  WinME..**.** Her
eabb0 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74  e is an interest
eabc0 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a  ing observation:
eabd0 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20    Win95, Win98, 
eabe0 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a  and WinME lack.*
eabf0 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78  * the LockFileEx
eac00 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20  () API.  But we 
eac10 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63  can still static
eac20 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73  ally link agains
eac30 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73  t that.** API as
eac40 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27   long as we don'
eac50 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 72  t call it when r
eac60 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f  unning Win95/98/
eac70 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a  ME.  A call to.*
eac80 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * this routine i
eac90 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
eaca0 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20  ine if the host 
eacb0 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f  is Win95/98/ME o
eacc0 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50  r.** WinNT/2K/XP
eacd0 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c   so that we will
eace0 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72   know whether or
eacf0 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65   not we can safe
ead00 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c  ly call.** the L
ead10 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
ead20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
ead30 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65  S_WINCE.# define
ead40 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c   isNT()  (1).#el
ead50 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  se.  static int 
ead60 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20  isNT(void){.    
ead70 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  if( sqlite3_os_t
ead80 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ype==0 ){.      
ead90 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49  OSVERSIONINFO sI
eada0 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f  nfo;.      sInfo
eadb0 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f  .dwOSVersionInfo
eadc0 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49  Size = sizeof(sI
eadd0 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56  nfo);.      GetV
eade0 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29  ersionEx(&sInfo)
eadf0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
eae00 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e  os_type = sInfo.
eae10 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45  dwPlatformId==VE
eae20 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32  R_PLATFORM_WIN32
eae30 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20  _NT ? 2 : 1;.   
eae40 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71   }.    return sq
eae50 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32  lite3_os_type==2
eae60 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
eae70 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
eae80 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  */../*.** Conver
eae90 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67  t a UTF-8 string
eaea0 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e   to microsoft un
eaeb0 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e  icode (UTF-16?).
eaec0 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f   .**.** Space to
eaed0 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
eaee0 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74  ed string is obt
eaef0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
eaf00 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48  c..*/.static WCH
eaf10 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64  AR *utf8ToUnicod
eaf20 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  e(const char *zF
eaf30 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20  ilename){.  int 
eaf40 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a  nChar;.  WCHAR *
eaf50 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a  zWideFilename;..
eaf60 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42    nChar = MultiB
eaf70 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50  yteToWideChar(CP
eaf80 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e  _UTF8, 0, zFilen
eaf90 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30  ame, -1, NULL, 0
eafa0 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61  );.  zWideFilena
eafb0 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68  me = malloc( nCh
eafc0 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46  ar*sizeof(zWideF
eafd0 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20  ilename[0]) );. 
eafe0 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61   if( zWideFilena
eaff0 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  me==0 ){.    ret
eb000 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68  urn 0;.  }.  nCh
eb010 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f  ar = MultiByteTo
eb020 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38  WideChar(CP_UTF8
eb030 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
eb040 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  -1, zWideFilenam
eb050 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28  e, nChar);.  if(
eb060 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20   nChar==0 ){.   
eb070 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e   free(zWideFilen
eb080 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46  ame);.    zWideF
eb090 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
eb0a0 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46  .  return zWideF
eb0b0 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ilename;.}../*.*
eb0c0 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73  * Convert micros
eb0d0 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55  oft unicode to U
eb0e0 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20  TF-8.  Space to 
eb0f0 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
eb100 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f  d string is.** o
eb110 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c  btained from mal
eb120 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
eb130 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f   char *unicodeTo
eb140 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52  Utf8(const WCHAR
eb150 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29   *zWideFilename)
eb160 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  {.  int nByte;. 
eb170 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
eb180 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64  ;..  nByte = Wid
eb190 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
eb1a0 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69  (CP_UTF8, 0, zWi
eb1b0 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20  deFilename, -1, 
eb1c0 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a  0, 0, 0, 0);.  z
eb1d0 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f  Filename = mallo
eb1e0 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66  c( nByte );.  if
eb1f0 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29  ( zFilename==0 )
eb200 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
eb210 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69    }.  nByte = Wi
eb220 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74  deCharToMultiByt
eb230 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57  e(CP_UTF8, 0, zW
eb240 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
eb250 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74   zFilename, nByt
eb260 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
eb270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb280 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42   0, 0);.  if( nB
eb290 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  yte == 0 ){.    
eb2a0 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  free(zFilename);
eb2b0 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d  .    zFilename =
eb2c0 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
eb2d0 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f   zFilename;.}../
eb2e0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20  *.** Convert an 
eb2f0 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d  ansi string to m
eb300 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
eb310 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a  , based on the.*
eb320 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  * current codepa
eb330 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20  ge settings for 
eb340 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a  file apis..** .*
eb350 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
eb360 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  the returned str
eb370 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a  ing is obtained.
eb380 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a  ** from malloc..
eb390 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20  */.static WCHAR 
eb3a0 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63  *mbcsToUnicode(c
eb3b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
eb3c0 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79  name){.  int nBy
eb3d0 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62  te;.  WCHAR *zMb
eb3e0 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e  csFilename;.  in
eb3f0 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65  t codepage = Are
eb400 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f  FileApisANSI() ?
eb410 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d   CP_ACP : CP_OEM
eb420 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d  CP;..  nByte = M
eb430 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68  ultiByteToWideCh
eb440 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20  ar(codepage, 0, 
eb450 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e  zFilename, -1, N
eb460 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43  ULL,0)*sizeof(WC
eb470 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c  HAR);.  zMbcsFil
eb480 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20  ename = malloc( 
eb490 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62  nByte*sizeof(zMb
eb4a0 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29  csFilename[0]) )
eb4b0 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c  ;.  if( zMbcsFil
eb4c0 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  ename==0 ){.    
eb4d0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
eb4e0 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74  nByte = MultiByt
eb4f0 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65  eToWideChar(code
eb500 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61  page, 0, zFilena
eb510 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c  me, -1, zMbcsFil
eb520 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20  ename, nByte);. 
eb530 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b   if( nByte==0 ){
eb540 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46  .    free(zMbcsF
eb550 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d  ilename);.    zM
eb560 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b  bcsFilename = 0;
eb570 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d  .  }.  return zM
eb580 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  bcsFilename;.}..
eb590 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69  /*.** Convert mi
eb5a0 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20  crosoft unicode 
eb5b0 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61  to multibyte cha
eb5c0 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62  racter string, b
eb5d0 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75  ased on the.** u
eb5e0 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70  ser's Ansi codep
eb5f0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  age..**.** Space
eb600 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
eb610 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20  urned string is 
eb620 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
eb630 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74   malloc()..*/.st
eb640 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f  atic char *unico
eb650 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57  deToMbcs(const W
eb660 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e  CHAR *zWideFilen
eb670 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ame){.  int nByt
eb680 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  e;.  char *zFile
eb690 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65  name;.  int code
eb6a0 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70  page = AreFileAp
eb6b0 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43  isANSI() ? CP_AC
eb6c0 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20  P : CP_OEMCP;.. 
eb6d0 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61   nByte = WideCha
eb6e0 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64  rToMultiByte(cod
eb6f0 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46  epage, 0, zWideF
eb700 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20  ilename, -1, 0, 
eb710 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c  0, 0, 0);.  zFil
eb720 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20  ename = malloc( 
eb730 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a  nByte );.  if( z
eb740 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20  Filename==0 ){. 
eb750 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
eb760 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43  .  nByte = WideC
eb770 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63  harToMultiByte(c
eb780 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64  odepage, 0, zWid
eb790 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a  eFilename, -1, z
eb7a0 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c  Filename, nByte,
eb7b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
eb7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
eb7d0 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  , 0);.  if( nByt
eb7e0 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72  e == 0 ){.    fr
eb7f0 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ee(zFilename);. 
eb800 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30     zFilename = 0
eb810 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
eb820 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
eb830 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69  ** Convert multi
eb840 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73  byte character s
eb850 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20  tring to UTF-8. 
eb860 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
eb870 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73  he.** returned s
eb880 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
eb890 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e  d from malloc().
eb8a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
eb8b0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e  har *sqlite3_win
eb8c0 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28  32_mbcs_to_utf8(
eb8d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
eb8e0 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  ename){.  char *
eb8f0 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20  zFilenameUtf8;. 
eb900 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65   WCHAR *zTmpWide
eb910 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20  ;..  zTmpWide = 
eb920 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46  mbcsToUnicode(zF
eb930 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ilename);.  if( 
eb940 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20  zTmpWide==0 ){. 
eb950 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
eb960 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38  .  zFilenameUtf8
eb970 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38   = unicodeToUtf8
eb980 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72  (zTmpWide);.  fr
eb990 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20  ee(zTmpWide);.  
eb9a0 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65  return zFilename
eb9b0 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Utf8;.}../*.** C
eb9c0 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20  onvert UTF-8 to 
eb9d0 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63  multibyte charac
eb9e0 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61  ter string.  Spa
eb9f0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a  ce to hold the .
eba00 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  ** returned stri
eba10 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ng is obtained f
eba20 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  rom malloc()..*/
eba30 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74  .static char *ut
eba40 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63  f8ToMbcs(const c
eba50 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
eba60 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
eba70 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20  meMbcs;.  WCHAR 
eba80 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54  *zTmpWide;..  zT
eba90 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55  mpWide = utf8ToU
ebaa0 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
ebab0 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64  );.  if( zTmpWid
ebac0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
ebad0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c  rn 0;.  }.  zFil
ebae0 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63  enameMbcs = unic
ebaf0 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69  odeToMbcs(zTmpWi
ebb00 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70  de);.  free(zTmp
ebb10 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Wide);.  return 
ebb20 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d  zFilenameMbcs;.}
ebb30 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
ebb40 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  WINCE./*********
ebb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebb90 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e  .** This section
ebba0 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66   contains code f
ebbb0 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a  or WinCE only..*
ebbc0 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43  /./*.** WindowsC
ebbd0 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  E does not have 
ebbe0 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75  a localtime() fu
ebbf0 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61  nction.  So crea
ebc00 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75  te a.** substitu
ebc10 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d  te..*/.struct tm
ebc20 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74   *__cdecl localt
ebc30 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74  ime(const time_t
ebc40 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20   *t).{.  static 
ebc50 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46  struct tm y;.  F
ebc60 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d  ILETIME uTm, lTm
ebc70 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70  ;.  SYSTEMTIME p
ebc80 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  Tm;.  sqlite3_in
ebc90 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d  t64 t64;.  t64 =
ebca0 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36   *t;.  t64 = (t6
ebcb0 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29  4 + 11644473600)
ebcc0 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d  *10000000;.  uTm
ebcd0 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d  .dwLowDateTime =
ebce0 20 74 36 34 20 26 20 30 78 46 46 46 46 46 46 46   t64 & 0xFFFFFFF
ebcf0 46 3b 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44  F;.  uTm.dwHighD
ebd00 61 74 65 54 69 6d 65 3d 20 74 36 34 20 3e 3e 20  ateTime= t64 >> 
ebd10 33 32 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f  32;.  FileTimeTo
ebd20 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28 26 75  LocalFileTime(&u
ebd30 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65  Tm,&lTm);.  File
ebd40 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65  TimeToSystemTime
ebd50 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79  (&lTm,&pTm);.  y
ebd60 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77  .tm_year = pTm.w
ebd70 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 20 20 79  Year - 1900;.  y
ebd80 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d  .tm_mon = pTm.wM
ebd90 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d  onth - 1;.  y.tm
ebda0 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79  _wday = pTm.wDay
ebdb0 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d  OfWeek;.  y.tm_m
ebdc0 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a  day = pTm.wDay;.
ebdd0 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54    y.tm_hour = pT
ebde0 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f  m.wHour;.  y.tm_
ebdf0 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74  min = pTm.wMinut
ebe00 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20  e;.  y.tm_sec = 
ebe10 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72  pTm.wSecond;.  r
ebe20 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20  eturn &y;.}../* 
ebe30 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 65 72 20  This will never 
ebe40 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 64  be called, but d
ebe50 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74  efined to make t
ebe60 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20  he code compile 
ebe70 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 74 54 65  */.#define GetTe
ebe80 6d 70 50 61 74 68 41 28 61 2c 62 29 0a 0a 23 64  mpPathA(a,b)..#d
ebe90 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61  efine LockFile(a
ebea0 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 20 20  ,b,c,d,e)       
ebeb0 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61  winceLockFile(&a
ebec0 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64  , b, c, d, e).#d
ebed0 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65  efine UnlockFile
ebee0 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20  (a,b,c,d,e)     
ebef0 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28  winceUnlockFile(
ebf00 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a  &a, b, c, d, e).
ebf10 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65  #define LockFile
ebf20 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20  Ex(a,b,c,d,e,f) 
ebf30 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45    winceLockFileE
ebf40 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65  x(&a, b, c, d, e
ebf50 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 20 48 41  , f)..#define HA
ebf60 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28  NDLE_TO_WINFILE(
ebf70 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 26 28 28  a) (winFile*)&((
ebf80 63 68 61 72 2a 29 61 29 5b 2d 6f 66 66 73 65 74  char*)a)[-offset
ebf90 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a  of(winFile,h)]..
ebfa0 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20  /*.** Acquire a 
ebfb0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 6e 64  lock on the hand
ebfc0 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63 20 76  le h.*/.static v
ebfd0 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 78 41 63  oid winceMutexAc
ebfe0 71 75 69 72 65 28 48 41 4e 44 4c 45 20 68 29 7b  quire(HANDLE h){
ebff0 0a 20 20 20 44 57 4f 52 44 20 64 77 45 72 72 3b  .   DWORD dwErr;
ec000 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 64 77  .   do {.     dw
ec010 45 72 72 20 3d 20 57 61 69 74 46 6f 72 53 69 6e  Err = WaitForSin
ec020 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49 4e 46  gleObject(h, INF
ec030 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77 68 69  INITE);.   } whi
ec040 6c 65 20 28 64 77 45 72 72 20 21 3d 20 57 41 49  le (dwErr != WAI
ec050 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20 64 77  T_OBJECT_0 && dw
ec060 45 72 72 20 21 3d 20 57 41 49 54 5f 41 42 41 4e  Err != WAIT_ABAN
ec070 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20  DONED);.}./*.** 
ec080 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b 20 61  Release a lock a
ec090 63 71 75 69 72 65 64 20 62 79 20 77 69 6e 63 65  cquired by wince
ec0a0 4d 75 74 65 78 41 63 71 75 69 72 65 28 29 0a 2a  MutexAcquire().*
ec0b0 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e 63 65 4d  /.#define winceM
ec0c0 75 74 65 78 52 65 6c 65 61 73 65 28 68 29 20 52  utexRelease(h) R
ec0d0 65 6c 65 61 73 65 4d 75 74 65 78 28 68 29 0a 0a  eleaseMutex(h)..
ec0e0 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65  /*.** Create the
ec0f0 20 6d 75 74 65 78 20 61 6e 64 20 73 68 61 72 65   mutex and share
ec100 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f  d memory used fo
ec110 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 65  r locking in the
ec120 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70   file.** descrip
ec130 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73 74 61  tor pFile.*/.sta
ec140 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 43 72  tic BOOL winceCr
ec150 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74 20 63  eateLock(const c
ec160 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
ec170 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b  winFile *pFile){
ec180 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a  .  WCHAR *zTok;.
ec190 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65 20 3d    WCHAR *zName =
ec1a0 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a   utf8ToUnicode(z
ec1b0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f  Filename);.  BOO
ec1c0 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a  L bInit = TRUE;.
ec1d0 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
ec1e0 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64   the local lockd
ec1f0 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d  ata */.  ZeroMem
ec200 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f 63 61  ory(&pFile->loca
ec210 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c 65 2d  l, sizeof(pFile-
ec220 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20  >local));..  /* 
ec230 52 65 70 6c 61 63 65 20 74 68 65 20 62 61 63 6b  Replace the back
ec240 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 74 68 65  slashes from the
ec250 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 6c 6f   filename and lo
ec260 77 65 72 63 61 73 65 20 69 74 0a 20 20 2a 2a 20  wercase it.  ** 
ec270 74 6f 20 64 65 72 69 76 65 20 61 20 6d 75 74 65  to derive a mute
ec280 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f  x name. */.  zTo
ec290 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 57 28 7a  k = CharLowerW(z
ec2a0 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28 3b 2a  Name);.  for (;*
ec2b0 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20  zTok;zTok++){.  
ec2c0 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27    if (*zTok == '
ec2d0 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27  \\') *zTok = '_'
ec2e0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61  ;.  }..  /* Crea
ec2f0 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61 6d 65  te/open the name
ec300 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70 46 69  d mutex */.  pFi
ec310 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43 72 65  le->hMutex = Cre
ec320 61 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c 2c 20  ateMutexW(NULL, 
ec330 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20  FALSE, zName);. 
ec340 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75   if (!pFile->hMu
ec350 74 65 78 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  tex){.    pFile-
ec360 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74  >lastErrno = Get
ec370 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
ec380 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 20 20   free(zName);.  
ec390 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a    return FALSE;.
ec3a0 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
ec3b0 65 20 74 68 65 20 6d 75 74 65 78 20 62 65 66 6f  e the mutex befo
ec3c0 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f  re continuing */
ec3d0 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71  .  winceMutexAcq
ec3e0 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  uire(pFile->hMut
ec3f0 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53 69 6e  ex);.  .  /* Sin
ec400 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  ce the names of 
ec410 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c 20 73  named mutexes, s
ec420 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c 65 20  emaphores, file 
ec430 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61 72 65  mappings etc are
ec440 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65 6e 73   .  ** case-sens
ec450 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64 76 61  itive, take adva
ec460 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20 62 79  ntage of that by
ec470 20 75 70 70 65 72 63 61 73 69 6e 67 20 74 68 65   uppercasing the
ec480 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20 2a 2a   mutex name.  **
ec490 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61 74 20   and using that 
ec4a0 61 73 20 74 68 65 20 73 68 61 72 65 64 20 66 69  as the shared fi
ec4b0 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65 2e 0a  lemapping name..
ec4c0 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70 65 72    */.  CharUpper
ec4d0 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46 69 6c  W(zName);.  pFil
ec4e0 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43 72 65  e->hShared = Cre
ec4f0 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 57 28  ateFileMappingW(
ec500 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56  INVALID_HANDLE_V
ec510 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20  ALUE, NULL,.    
ec520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec540 20 20 20 50 41 47 45 5f 52 45 41 44 57 52 49 54     PAGE_READWRIT
ec550 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e  E, 0, sizeof(win
ec560 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20 20 20  ceLock),.       
ec570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec590 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f 2a 20  zName);  ..  /* 
ec5a0 53 65 74 20 61 20 66 6c 61 67 20 74 68 61 74 20  Set a flag that 
ec5b0 69 6e 64 69 63 61 74 65 73 20 77 65 27 72 65 20  indicates we're 
ec5c0 74 68 65 20 66 69 72 73 74 20 74 6f 20 63 72 65  the first to cre
ec5d0 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73  ate the memory s
ec5e0 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73 74 20  o it .  ** must 
ec5f0 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61 6c 69  be zero-initiali
ec600 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47 65 74  zed */.  if (Get
ec610 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d 20 45  LastError() == E
ec620 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45 58 49  RROR_ALREADY_EXI
ec630 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69 74 20  STS){.    bInit 
ec640 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20  = FALSE;.  }..  
ec650 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20  free(zName);..  
ec660 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65 65 64  /* If we succeed
ec670 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74 68 65  ed in making the
ec680 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68   shared memory h
ec690 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e 20 2a  andle, map it. *
ec6a0 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68  /.  if (pFile->h
ec6b0 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70 46 69  Shared){.    pFi
ec6c0 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28 77 69  le->shared = (wi
ec6d0 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69 65 77  nceLock*)MapView
ec6e0 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 53  OfFile(pFile->hS
ec6f0 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20 20 20  hared, .        
ec700 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f 52 45       FILE_MAP_RE
ec710 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52 49 54  AD|FILE_MAP_WRIT
ec720 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28  E, 0, 0, sizeof(
ec730 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 20  winceLock));.   
ec740 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67 20 66   /* If mapping f
ec750 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74 68 65  ailed, close the
ec760 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68   shared memory h
ec770 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73 65 20  andle and erase 
ec780 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 21 70  it */.    if (!p
ec790 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b 0a 20  File->shared){. 
ec7a0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
ec7b0 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45  Errno = GetLastE
ec7c0 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 43 6c  rror();.      Cl
ec7d0 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d  oseHandle(pFile-
ec7e0 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20  >hShared);.     
ec7f0 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20   pFile->hShared 
ec800 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20  = NULL;.    }.  
ec810 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 72 65  }..  /* If share
ec820 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 20 6e  d memory could n
ec830 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c 20 74  ot be created, t
ec840 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 6d 75  hen close the mu
ec850 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a 2f 0a  tex and fail */.
ec860 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68    if (pFile->hSh
ec870 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20  ared == NULL){. 
ec880 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c     winceMutexRel
ec890 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  ease(pFile->hMut
ec8a0 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61  ex);.    CloseHa
ec8b0 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  ndle(pFile->hMut
ec8c0 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ex);.    pFile->
ec8d0 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20  hMutex = NULL;. 
ec8e0 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b     return FALSE;
ec8f0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 6e 69  .  }.  .  /* Ini
ec900 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68 61 72  tialize the shar
ec910 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 65 27  ed memory if we'
ec920 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f 20 2a  re supposed to *
ec930 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29 20 7b  /.  if (bInit) {
ec940 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28  .    ZeroMemory(
ec950 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c 20 73  pFile->shared, s
ec960 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29  izeof(winceLock)
ec970 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d  );.  }..  winceM
ec980 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
ec990 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65  e->hMutex);.  re
ec9a0 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a  turn TRUE;.}../*
ec9b0 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 65 20  .** Destroy the 
ec9c0 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c 65 20  part of winFile 
ec9d0 74 68 61 74 20 64 65 61 6c 73 20 77 69 74 68 20  that deals with 
ec9e0 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73  wince locks.*/.s
ec9f0 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65  tatic void wince
eca00 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 6e 46  DestroyLock(winF
eca10 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69  ile *pFile){.  i
eca20 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  f (pFile->hMutex
eca30 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 69 72  ){.    /* Acquir
eca40 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20  e the mutex */. 
eca50 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71     winceMutexAcq
eca60 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  uire(pFile->hMut
eca70 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  ex);..    /* The
eca80 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
eca90 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c  s should probabl
ecaa0 79 20 61 73 73 65 72 74 20 69 6e 20 64 65 62 75  y assert in debu
ecab0 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 65 79  g mode, but they
ecac0 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f 20 63  .       are to c
ecad0 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65 20 61  leanup in case a
ecae0 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 6e 65  ny locks remaine
ecaf0 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 66  d open */.    if
ecb00 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e   (pFile->local.n
ecb10 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20  Readers){.      
ecb20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e  pFile->shared->n
ecb30 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20  Readers --;.    
ecb40 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  }.    if (pFile-
ecb50 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64  >local.bReserved
ecb60 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
ecb70 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65  shared->bReserve
ecb80 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d  d = FALSE;.    }
ecb90 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
ecba0 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b  local.bPending){
ecbb0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
ecbc0 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d  ared->bPending =
ecbd0 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20   FALSE;.    }.  
ecbe0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
ecbf0 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a  al.bExclusive){.
ecc00 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
ecc10 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20  red->bExclusive 
ecc20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a  = FALSE;.    }..
ecc30 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 72 65      /* De-refere
ecc40 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 6f 75  nce and close ou
ecc50 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 68  r copy of the sh
ecc60 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64  ared memory hand
ecc70 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 70 56  le */.    UnmapV
ecc80 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d  iewOfFile(pFile-
ecc90 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20 43 6c  >shared);.    Cl
ecca0 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d  oseHandle(pFile-
eccb0 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 20 20  >hShared);..    
eccc0 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65  /* Done with the
eccd0 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69   mutex */.    wi
ecce0 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28  nceMutexRelease(
eccf0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20  pFile->hMutex); 
ecd00 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e     .    CloseHan
ecd10 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  dle(pFile->hMute
ecd20 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68  x);.    pFile->h
ecd30 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20  Mutex = NULL;.  
ecd40 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69  }.}../* .** An i
ecd50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
ecd60 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20   the LockFile() 
ecd70 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66  API of windows f
ecd80 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74  or wince.*/.stat
ecd90 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63  ic BOOL winceLoc
ecda0 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20  kFile(.  HANDLE 
ecdb0 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44  *phFile,.  DWORD
ecdc0 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77   dwFileOffsetLow
ecdd0 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65  ,.  DWORD dwFile
ecde0 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57  OffsetHigh,.  DW
ecdf0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
ece00 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44  esToLockLow,.  D
ece10 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79  WORD nNumberOfBy
ece20 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b  tesToLockHigh.){
ece30 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
ece40 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49  e = HANDLE_TO_WI
ece50 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20  NFILE(phFile);. 
ece60 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20   BOOL bReturn = 
ece70 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21 70  FALSE;..  if (!p
ece80 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65  File->hMutex) re
ece90 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e  turn TRUE;.  win
ecea0 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70  ceMutexAcquire(p
eceb0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a  File->hMutex);..
ecec0 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20    /* Wanting an 
eced0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20  exclusive lock? 
ecee0 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  */.  if (dwFileO
ecef0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 53 48 41 52  ffsetLow == SHAR
ecf00 45 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20  ED_FIRST.       
ecf10 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
ecf20 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48  sToLockLow == SH
ecf30 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20  ARED_SIZE){.    
ecf40 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65  if (pFile->share
ecf50 64 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30  d->nReaders == 0
ecf60 20 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65   && pFile->share
ecf70 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d  d->bExclusive ==
ecf80 20 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c   0){.       pFil
ecf90 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c  e->shared->bExcl
ecfa0 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20  usive = TRUE;.  
ecfb0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
ecfc0 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54  l.bExclusive = T
ecfd0 52 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74  RUE;.       bRet
ecfe0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
ecff0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
ed000 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63   a read-only loc
ed010 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20  k? */.  else if 
ed020 28 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f  ((dwFileOffsetLo
ed030 77 20 3e 3d 20 53 48 41 52 45 44 5f 46 49 52 53  w >= SHARED_FIRS
ed040 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  T &&.           
ed050 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77   dwFileOffsetLow
ed060 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20   < SHARED_FIRST 
ed070 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29 20 26  + SHARED_SIZE) &
ed080 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e  &.            nN
ed090 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
ed0a0 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20  ckLow == 1){.   
ed0b0 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72   if (pFile->shar
ed0c0 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d  ed->bExclusive =
ed0d0 3d 20 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  = 0){.      pFil
ed0e0 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
ed0f0 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28  s ++;.      if (
ed100 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65  pFile->local.nRe
ed110 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20  aders == 1){.   
ed120 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
ed130 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b  ed->nReaders ++;
ed140 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
ed150 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20  Return = TRUE;. 
ed160 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57     }.  }..  /* W
ed170 61 6e 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f  ant a pending lo
ed180 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ck? */.  else if
ed190 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f   (dwFileOffsetLo
ed1a0 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  w == PENDING_BYT
ed1b0 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
ed1c0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20  tesToLockLow == 
ed1d0 31 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f  1){.    /* If no
ed1e0 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61   pending lock ha
ed1f0 73 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c  s been acquired,
ed200 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74   then acquire it
ed210 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c   */.    if (pFil
ed220 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
ed230 69 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20  ing == 0) {.    
ed240 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
ed250 3e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45  >bPending = TRUE
ed260 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
ed270 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20  ocal.bPending = 
ed280 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74  TRUE;.      bRet
ed290 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
ed2a0 7d 0a 20 20 7d 0a 20 20 2f 2a 20 57 61 6e 74 20  }.  }.  /* Want 
ed2b0 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f  a reserved lock?
ed2c0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
ed2d0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
ed2e0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20  = RESERVED_BYTE 
ed2f0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
ed300 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29  sToLockLow == 1)
ed310 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  {.    if (pFile-
ed320 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76  >shared->bReserv
ed330 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20  ed == 0) {.     
ed340 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
ed350 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45  bReserved = TRUE
ed360 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
ed370 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d  ocal.bReserved =
ed380 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65   TRUE;.      bRe
ed390 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
ed3a0 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d   }.  }..  winceM
ed3b0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
ed3c0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65  e->hMutex);.  re
ed3d0 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a  turn bReturn;.}.
ed3e0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d  ./*.** An implem
ed3f0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
ed400 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f  UnlockFile API o
ed410 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69  f windows for wi
ed420 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  nce.*/.static BO
ed430 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69  OL winceUnlockFi
ed440 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68  le(.  HANDLE *ph
ed450 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77  File,.  DWORD dw
ed460 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20  FileOffsetLow,. 
ed470 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66   DWORD dwFileOff
ed480 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44  setHigh,.  DWORD
ed490 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
ed4a0 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57  oUnlockLow,.  DW
ed4b0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
ed4c0 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29  esToUnlockHigh.)
ed4d0 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  {.  winFile *pFi
ed4e0 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57  le = HANDLE_TO_W
ed4f0 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a  INFILE(phFile);.
ed500 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d    BOOL bReturn =
ed510 20 46 41 4c 53 45 3b 0a 0a 20 20 69 66 20 28 21   FALSE;..  if (!
ed520 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72  pFile->hMutex) r
ed530 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69  eturn TRUE;.  wi
ed540 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
ed550 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
ed560 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20  .  /* Releasing 
ed570 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72  a reader lock or
ed580 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
ed590 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69  ck */.  if (dwFi
ed5a0 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53  leOffsetLow >= S
ed5b0 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20  HARED_FIRST &&. 
ed5c0 20 20 20 20 20 20 64 77 46 69 6c 65 4f 66 66 73        dwFileOffs
ed5d0 65 74 4c 6f 77 20 3c 20 53 48 41 52 45 44 5f 46  etLow < SHARED_F
ed5e0 49 52 53 54 20 2b 20 53 48 41 52 45 44 5f 53 49  IRST + SHARED_SI
ed5f0 5a 45 29 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20  ZE){.    /* Did 
ed600 77 65 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75  we have an exclu
ed610 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  sive lock? */.  
ed620 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
ed630 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 7b 0a  al.bExclusive){.
ed640 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
ed650 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20  al.bExclusive = 
ed660 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69  FALSE;.      pFi
ed670 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63  le->shared->bExc
ed680 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a  lusive = FALSE;.
ed690 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
ed6a0 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  TRUE;.    }..   
ed6b0 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 74 20   /* Did we just 
ed6c0 68 61 76 65 20 61 20 72 65 61 64 65 72 20 6c 6f  have a reader lo
ed6d0 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20  ck? */.    else 
ed6e0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
ed6f0 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20  .nReaders){.    
ed700 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e    pFile->local.n
ed710 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20  Readers --;.    
ed720 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
ed730 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30  al.nReaders == 0
ed740 29 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  ).      {.      
ed750 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
ed760 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20  >nReaders --;.  
ed770 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74      }.      bRet
ed780 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
ed790 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65  }.  }..  /* Rele
ed7a0 61 73 69 6e 67 20 61 20 70 65 6e 64 69 6e 67 20  asing a pending 
ed7b0 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69  lock */.  else i
ed7c0 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  f (dwFileOffsetL
ed7d0 6f 77 20 3d 3d 20 50 45 4e 44 49 4e 47 5f 42 59  ow == PENDING_BY
ed7e0 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42  TE && nNumberOfB
ed7f0 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20  ytesToUnlockLow 
ed800 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70  == 1){.    if (p
ed810 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e  File->local.bPen
ed820 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69  ding){.      pFi
ed830 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69  le->local.bPendi
ed840 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ng = FALSE;.    
ed850 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
ed860 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53  >bPending = FALS
ed870 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
ed880 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
ed890 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e   }.  /* Releasin
ed8a0 67 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  g a reserved loc
ed8b0 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  k */.  else if (
ed8c0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
ed8d0 3d 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  == RESERVED_BYTE
ed8e0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74   && nNumberOfByt
ed8f0 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d  esToUnlockLow ==
ed900 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69   1){.    if (pFi
ed910 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72  le->local.bReser
ed920 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 69  ved) {.      pFi
ed930 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72  le->local.bReser
ed940 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ved = FALSE;.   
ed950 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
ed960 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41  ->bReserved = FA
ed970 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75  LSE;.      bRetu
ed980 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
ed990 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74  .  }..  winceMut
ed9a0 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d  exRelease(pFile-
ed9b0 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  >hMutex);.  retu
ed9c0 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  rn bReturn;.}../
ed9d0 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e  *.** An implemen
ed9e0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f  tation of the Lo
ed9f0 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 6f  ckFileEx() API o
eda00 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69  f windows for wi
eda10 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  nce.*/.static BO
eda20 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65  OL winceLockFile
eda30 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68  Ex(.  HANDLE *ph
eda40 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77  File,.  DWORD dw
eda50 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 64  Flags,.  DWORD d
eda60 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 4f  wReserved,.  DWO
eda70 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  RD nNumberOfByte
eda80 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57  sToLockLow,.  DW
eda90 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
edaa0 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 20  esToLockHigh,.  
edab0 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 4f  LPOVERLAPPED lpO
edac0 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 2f  verlapped.){.  /
edad0 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
edae0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72  wants a shared r
edaf0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72  ead lock, forwar
edb00 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a  d this call.  **
edb10 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c   to winceLockFil
edb20 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65  e */.  if (lpOve
edb30 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20  rlapped->Offset 
edb40 3d 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 20  == SHARED_FIRST 
edb50 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73  &&.      dwFlags
edb60 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e   == 1 &&.      n
edb70 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
edb80 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44  ockLow == SHARED
edb90 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75  _SIZE){.    retu
edba0 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65  rn winceLockFile
edbb0 28 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f  (phFile, SHARED_
edbc0 46 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b  FIRST, 0, 1, 0);
edbd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41  .  }.  return FA
edbe0 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64  LSE;.}./*.** End
edbf0 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20   of the special 
edc00 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a  code for wince.*
edc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
edc60 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
edc70 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a  S_WINCE */../***
edc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
edcd0 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20  e next group of 
edce0 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65  routines impleme
edcf0 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f  nt the I/O metho
edd00 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  ds specified.** 
edd10 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  by the sqlite3_i
edd20 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
edd30 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
edd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
edd80 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  /../*.** Close a
edd90 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20   file..**.** It 
edda0 69 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74  is reported that
eddb0 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63   an attempt to c
eddc0 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69  lose a handle mi
eddd0 67 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a  ght sometimes.**
edde0 20 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20   fail.  This is 
eddf0 61 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61  a very unreasona
ede00 62 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20  ble result, but 
ede10 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72  windows is notor
ede20 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e  ious.** for bein
ede30 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73  g unreasonable s
ede40 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74  o I do not doubt
ede50 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68   that it might h
ede60 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68  appen.  If.** th
ede70 65 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77  e close fails, w
ede80 65 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20  e pause for 100 
ede90 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64  milliseconds and
edea0 20 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a   try again.  As.
edeb0 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c  ** many as MX_CL
edec0 4f 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65  OSE_ATTEMPT atte
eded0 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68  mpts to close th
edee0 65 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64  e handle are mad
edef0 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69  e before.** givi
edf00 6e 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e  ng up and return
edf10 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  ing an error..*/
edf20 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53  .#define MX_CLOS
edf30 45 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74  E_ATTEMPT 3.stat
edf40 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28  ic int winClose(
edf50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
edf60 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74  ){.  int rc, cnt
edf70 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20   = 0;.  winFile 
edf80 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
edf90 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74  e*)id;..  assert
edfa0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54  ( id!=0 );.  OST
edfb0 52 41 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c  RACE2("CLOSE %d\
edfc0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20  n", pFile->h);. 
edfd0 20 64 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c   do{.    rc = Cl
edfe0 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d  oseHandle(pFile-
edff0 3e 68 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72  >h);.  }while( r
ee000 63 3d 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20  c==0 && ++cnt < 
ee010 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54  MX_CLOSE_ATTEMPT
ee020 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c   && (Sleep(100),
ee030 20 31 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54   1) );.#if SQLIT
ee040 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69  E_OS_WINCE.#defi
ee050 6e 65 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f  ne WINCE_DELETIO
ee060 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77  N_ATTEMPTS 3.  w
ee070 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28  inceDestroyLock(
ee080 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46  pFile);.  if( pF
ee090 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c  ile->zDeleteOnCl
ee0a0 6f 73 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63  ose ){.    int c
ee0b0 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c  nt = 0;.    whil
ee0c0 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 44 65  e(.           De
ee0d0 6c 65 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d  leteFileW(pFile-
ee0e0 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29  >zDeleteOnClose)
ee0f0 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 47  ==0.        && G
ee100 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
ee110 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65  W(pFile->zDelete
ee120 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66  OnClose)!=0xffff
ee130 66 66 66 66 20 0a 20 20 20 20 20 20 20 20 26 26  ffff .        &&
ee140 20 63 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44   cnt++ < WINCE_D
ee150 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53  ELETION_ATTEMPTS
ee160 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53  .    ){.       S
ee170 6c 65 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57  leep(100);  /* W
ee180 61 69 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66  ait a little bef
ee190 6f 72 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e  ore trying again
ee1a0 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72   */.    }.    fr
ee1b0 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74  ee(pFile->zDelet
ee1c0 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23  eOnClose);.  }.#
ee1d0 65 6e 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e  endif.  OpenCoun
ee1e0 74 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72  ter(-1);.  retur
ee1f0 6e 20 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  n rc ? SQLITE_OK
ee200 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
ee210 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d  .}../*.** Some m
ee220 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65  icrosoft compile
ee230 72 73 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66  rs lack this def
ee240 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e  inition..*/.#ifn
ee250 64 65 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f  def INVALID_SET_
ee260 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64  FILE_POINTER.# d
ee270 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45  efine INVALID_SE
ee280 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28  T_FILE_POINTER (
ee290 28 44 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69  (DWORD)-1).#endi
ee2a0 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61  f../*.** Read da
ee2b0 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69  ta from a file i
ee2c0 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52  nto a buffer.  R
ee2d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
ee2e0 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20  if all.** bytes 
ee2f0 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73  were read succes
ee300 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54  sfully and SQLIT
ee310 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68  E_IOERR if anyth
ee320 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e  ing goes.** wron
ee330 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
ee340 20 77 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69   winRead(.  sqli
ee350 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20  te3_file *id,   
ee360 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74         /* File t
ee370 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20  o read from */. 
ee380 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20   void *pBuf,    
ee390 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
ee3a0 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74  rite content int
ee3b0 6f 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f  o this buffer */
ee3c0 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20  .  int amt,     
ee3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ee3e0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
ee3f0 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71   to read */.  sq
ee400 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
ee410 65 74 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69  et       /* Begi
ee420 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69  n reading at thi
ee430 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20  s offset */.){. 
ee440 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20   LONG upperBits 
ee450 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74  = (LONG)((offset
ee460 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66  >>32) & 0x7fffff
ee470 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65  ff);.  LONG lowe
ee480 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f  rBits = (LONG)(o
ee490 66 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66  ffset & 0xffffff
ee4a0 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b  ff);.  DWORD rc;
ee4b0 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
ee4c0 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64  e = (winFile*)id
ee4d0 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b  ;.  DWORD error;
ee4e0 0a 20 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20  .  DWORD got;.. 
ee4f0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
ee500 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
ee510 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
ee520 45 5f 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20  E_IOERR_READ);. 
ee530 20 4f 53 54 52 41 43 45 33 28 22 52 45 41 44 20   OSTRACE3("READ 
ee540 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70  %d lock=%d\n", p
ee550 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
ee560 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20  locktype);.  rc 
ee570 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72  = SetFilePointer
ee580 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72  (pFile->h, lower
ee590 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73  Bits, &upperBits
ee5a0 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20  , FILE_BEGIN);. 
ee5b0 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44   if( rc==INVALID
ee5c0 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
ee5d0 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c  R && (error=GetL
ee5e0 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f  astError())!=NO_
ee5f0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69  ERROR ){.    pFi
ee600 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
ee610 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72  error;.    retur
ee620 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
ee630 20 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69   }.  if( !ReadFi
ee640 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75  le(pFile->h, pBu
ee650 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29  f, amt, &got, 0)
ee660 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
ee670 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61  astErrno = GetLa
ee680 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72  stError();.    r
ee690 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
ee6a0 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69  RR_READ;.  }.  i
ee6b0 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61  f( got==(DWORD)a
ee6c0 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mt ){.    return
ee6d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
ee6e0 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65  lse{.    /* Unre
ee6f0 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20  ad parts of the 
ee700 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a  buffer must be z
ee710 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20  ero-filled */.  
ee720 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72    memset(&((char
ee730 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c  *)pBuf)[got], 0,
ee740 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72   amt-got);.    r
ee750 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
ee760 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20  RR_SHORT_READ;. 
ee770 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74   }.}../*.** Writ
ee780 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75  e data from a bu
ee790 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65  ffer into a file
ee7a0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
ee7b0 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a  _OK on success.*
ee7c0 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  * or some other 
ee7d0 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61  error code on fa
ee7e0 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ilure..*/.static
ee7f0 20 69 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20   int winWrite(. 
ee800 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
ee810 64 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  d,         /* Fi
ee820 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f  le to write into
ee830 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
ee840 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20   *pBuf,         
ee850 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20  /* The bytes to 
ee860 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  be written */.  
ee870 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
ee880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ee890 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
ee8a0 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74  write */.  sqlit
ee8b0 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20  e3_int64 offset 
ee8c0 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
ee8d0 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20  nto the file to 
ee8e0 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74  begin writing at
ee8f0 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70   */.){.  LONG up
ee900 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29  perBits = (LONG)
ee910 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20  ((offset>>32) & 
ee920 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c  0x7fffffff);.  L
ee930 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20  ONG lowerBits = 
ee940 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20  (LONG)(offset & 
ee950 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44  0xffffffff);.  D
ee960 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69  WORD rc;.  winFi
ee970 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
ee980 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
ee990 44 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44  D error;.  DWORD
ee9a0 20 77 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61   wrote = 0;..  a
ee9b0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
ee9c0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
ee9d0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
ee9e0 49 4f 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20  IOERR_WRITE);.  
ee9f0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
eea00 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
eea10 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54  ITE_FULL);.  OST
eea20 52 41 43 45 33 28 22 57 52 49 54 45 20 25 64 20  RACE3("WRITE %d 
eea30 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c  lock=%d\n", pFil
eea40 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  e->h, pFile->loc
eea50 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53  ktype);.  rc = S
eea60 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46  etFilePointer(pF
eea70 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74  ile->h, lowerBit
eea80 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46  s, &upperBits, F
eea90 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66  ILE_BEGIN);.  if
eeaa0 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45  ( rc==INVALID_SE
eeab0 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26  T_FILE_POINTER &
eeac0 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74  & (error=GetLast
eead0 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52  Error())!=NO_ERR
eeae0 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  OR ){.    pFile-
eeaf0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
eeb00 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  or;.    return S
eeb10 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a  QLITE_FULL;.  }.
eeb20 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20    assert( amt>0 
eeb30 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20  );.  while(.    
eeb40 20 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28   amt>0.     && (
eeb50 72 63 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70  rc = WriteFile(p
eeb60 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61  File->h, pBuf, a
eeb70 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21  mt, &wrote, 0))!
eeb80 3d 30 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65  =0.     && wrote
eeb90 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20  >0.  ){.    amt 
eeba0 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42  -= wrote;.    pB
eebb0 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42  uf = &((char*)pB
eebc0 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a  uf)[wrote];.  }.
eebd0 20 20 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74    if( !rc || amt
eebe0 3e 28 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20  >(int)wrote ){. 
eebf0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
eec00 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72  rno = GetLastErr
eec10 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  or();.    return
eec20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20   SQLITE_FULL;.  
eec30 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
eec40 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
eec50 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20  runcate an open 
eec60 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66  file to a specif
eec70 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74  ied size.*/.stat
eec80 69 63 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61  ic int winTrunca
eec90 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
eeca0 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  *id, sqlite3_int
eecb0 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e  64 nByte){.  LON
eecc0 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c  G upperBits = (L
eecd0 4f 4e 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29  ONG)((nByte>>32)
eece0 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a   & 0x7fffffff);.
eecf0 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73    LONG lowerBits
eed00 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20   = (LONG)(nByte 
eed10 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20  & 0xffffffff);. 
eed20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e   DWORD rc;.  win
eed30 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
eed40 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57  inFile*)id;.  DW
eed50 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73  ORD error;..  as
eed60 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
eed70 20 4f 53 54 52 41 43 45 33 28 22 54 52 55 4e 43   OSTRACE3("TRUNC
eed80 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20  ATE %d %lld\n", 
eed90 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29  pFile->h, nByte)
eeda0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
eedb0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
eedc0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45  E_IOERR_TRUNCATE
eedd0 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c  );.  rc = SetFil
eede0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e  ePointer(pFile->
eedf0 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75  h, lowerBits, &u
eee00 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42  pperBits, FILE_B
eee10 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d  EGIN);.  if( rc=
eee20 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c  =INVALID_SET_FIL
eee30 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72  E_POINTER && (er
eee40 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72  ror=GetLastError
eee50 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b  ())!=NO_ERROR ){
eee60 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
eee70 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20  Errno = error;. 
eee80 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
eee90 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b  _IOERR_TRUNCATE;
eeea0 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64  .  }.  /* SetEnd
eeeb0 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c  OfFile will fail
eeec0 20 69 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67   if nByte is neg
eeed0 61 74 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21  ative */.  if( !
eeee0 53 65 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69  SetEndOfFile(pFi
eeef0 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46  le->h) ){.    pF
eef00 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
eef10 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b   GetLastError();
eef20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
eef30 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
eef40 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  E;.  }.  return 
eef50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69  SQLITE_OK;.}..#i
eef60 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
eef70 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65  ./*.** Count the
eef80 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73   number of fulls
eef90 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20  yncs and normal 
eefa0 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20  syncs.  This is 
eefb0 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20  used to test.** 
eefc0 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66  that syncs and f
eefd0 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63  ullsyncs are occ
eefe0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67  uring at the rig
eeff0 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c  ht times..*/.SQL
ef000 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
ef010 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d  te3_sync_count =
ef020 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
ef030 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73  nt sqlite3_fulls
ef040 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ync_count = 0;.#
ef050 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  endif../*.** Mak
ef060 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65  e sure all write
ef070 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61  s to a particula
ef080 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69  r file are commi
ef090 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f  tted to disk..*/
ef0a0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53  .static int winS
ef0b0 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ync(sqlite3_file
ef0c0 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29   *id, int flags)
ef0d0 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
ef0e0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69  _NO_SYNC.  winFi
ef0f0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
ef100 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73  File*)id;..  ass
ef110 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
ef120 4f 53 54 52 41 43 45 33 28 22 53 59 4e 43 20 25  OSTRACE3("SYNC %
ef130 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46  d lock=%d\n", pF
ef140 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c  ile->h, pFile->l
ef150 6f 63 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a  ocktype);.#else.
ef160 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
ef170 45 52 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23  ER(id);.#endif.#
ef180 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45  ifndef SQLITE_TE
ef190 53 54 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ST.  UNUSED_PARA
ef1a0 4d 45 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65  METER(flags);.#e
ef1b0 6c 73 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20  lse.  if( flags 
ef1c0 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  & SQLITE_SYNC_FU
ef1d0 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  LL ){.    sqlite
ef1e0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
ef1f0 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ++;.  }.  sqlite
ef200 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a  3_sync_count++;.
ef210 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77  #endif.  /* If w
ef220 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  e compiled with 
ef230 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  the SQLITE_NO_SY
ef240 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79  NC flag, then sy
ef250 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20  ncing is a.  ** 
ef260 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65  no-op.  */.#ifde
ef270 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  f SQLITE_NO_SYNC
ef280 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
ef290 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69  TE_OK;.#else.  i
ef2a0 66 28 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66  f( FlushFileBuff
ef2b0 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b  ers(pFile->h) ){
ef2c0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
ef2d0 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
ef2e0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
ef2f0 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72  rrno = GetLastEr
ef300 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72  ror();.    retur
ef310 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a  n SQLITE_IOERR;.
ef320 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a    }.#endif.}../*
ef330 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68  .** Determine th
ef340 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
ef350 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65  f a file in byte
ef360 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  s.*/.static int 
ef370 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69  winFileSize(sqli
ef380 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71  te3_file *id, sq
ef390 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69  lite3_int64 *pSi
ef3a0 7a 65 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70  ze){.  DWORD upp
ef3b0 65 72 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20  erBits;.  DWORD 
ef3c0 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e  lowerBits;.  win
ef3d0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
ef3e0 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57  inFile*)id;.  DW
ef3f0 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73  ORD error;..  as
ef400 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
ef410 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
ef420 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49  (return SQLITE_I
ef430 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c  OERR_FSTAT);.  l
ef440 6f 77 65 72 42 69 74 73 20 3d 20 47 65 74 46 69  owerBits = GetFi
ef450 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c  leSize(pFile->h,
ef460 20 26 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20   &upperBits);.  
ef470 69 66 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73  if(   (lowerBits
ef480 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45   == INVALID_FILE
ef490 5f 53 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28  _SIZE).     && (
ef4a0 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74  (error = GetLast
ef4b0 45 72 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45  Error()) != NO_E
ef4c0 52 52 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20  RROR) ).  {.    
ef4d0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
ef4e0 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65   = error;.    re
ef4f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
ef500 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a  R_FSTAT;.  }.  *
ef510 70 53 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74  pSize = (((sqlit
ef520 65 33 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69  e3_int64)upperBi
ef530 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72  ts)<<32) + lower
ef540 42 69 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53  Bits;.  return S
ef550 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
ef560 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c  ** LOCKFILE_FAIL
ef570 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20  _IMMEDIATELY is 
ef580 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d  undefined on som
ef590 65 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d  e Windows system
ef5a0 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f  s..*/.#ifndef LO
ef5b0 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45  CKFILE_FAIL_IMME
ef5c0 44 49 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65  DIATELY.# define
ef5d0 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
ef5e0 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e  MMEDIATELY 1.#en
ef5f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69  dif../*.** Acqui
ef600 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b  re a reader lock
ef610 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41  ..** Different A
ef620 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  PI routines are 
ef630 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67  called depending
ef640 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e   on whether or n
ef650 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69  ot this.** is Wi
ef660 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f  n95 or WinNT..*/
ef670 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52  .static int getR
ef680 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20  eadLock(winFile 
ef690 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72  *pFile){.  int r
ef6a0 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29  es;.  if( isNT()
ef6b0 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50   ){.    OVERLAPP
ef6c0 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c  ED ovlp;.    ovl
ef6d0 70 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45  p.Offset = SHARE
ef6e0 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c  D_FIRST;.    ovl
ef6f0 70 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30  p.OffsetHigh = 0
ef700 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e  ;.    ovlp.hEven
ef710 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d  t = 0;.    res =
ef720 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c   LockFileEx(pFil
ef730 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46  e->h, LOCKFILE_F
ef740 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c  AIL_IMMEDIATELY,
ef750 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ef760 20 20 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f        0, SHARED_
ef770 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b  SIZE, 0, &ovlp);
ef780 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
ef790 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ef7a0 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
ef7b0 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
ef7c0 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53  cuted. .*/.#if S
ef7d0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
ef7e0 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  0.  }else{.    i
ef7f0 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74  nt lk;.    sqlit
ef800 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69  e3_randomness(si
ef810 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a  zeof(lk), &lk);.
ef820 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
ef830 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f  dLockByte = (sho
ef840 72 74 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66  rt)((lk & 0x7fff
ef850 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49  ffff)%(SHARED_SI
ef860 5a 45 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65  ZE - 1));.    re
ef870 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69  s = LockFile(pFi
ef880 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
ef890 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65  RST+pFile->share
ef8a0 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c  dLockByte, 0, 1,
ef8b0 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a   0);.#endif.  }.
ef8c0 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29    if( res == 0 )
ef8d0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
ef8e0 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
ef8f0 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72  Error();.  }.  r
ef900 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a  eturn res;.}../*
ef910 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c  .** Undo a readl
ef920 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ock.*/.static in
ef930 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b  t unlockReadLock
ef940 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29  (winFile *pFile)
ef950 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69  {.  int res;.  i
ef960 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
ef970 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c   res = UnlockFil
ef980 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  e(pFile->h, SHAR
ef990 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41  ED_FIRST, 0, SHA
ef9a0 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a  RED_SIZE, 0);./*
ef9b0 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
ef9c0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
ef9d0 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
ef9e0 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
ef9f0 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  ed. .*/.#if SQLI
efa00 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20  TE_OS_WINCE==0. 
efa10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20   }else{.    res 
efa20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69  = UnlockFile(pFi
efa30 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
efa40 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61  RST + pFile->sha
efa50 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20  redLockByte, 0, 
efa60 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20  1, 0);.#endif.  
efa70 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30  }.  if( res == 0
efa80 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
efa90 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61  astErrno = GetLa
efaa0 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20  stError();.  }. 
efab0 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a   return res;.}..
efac0 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66  /*.** Lock the f
efad0 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63  ile with the loc
efae0 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70  k specified by p
efaf0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70  arameter locktyp
efb00 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68  e - one.** of th
efb10 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
efb20 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45  **     (1) SHARE
efb30 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32  D_LOCK.**     (2
efb40 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a  ) RESERVED_LOCK.
efb50 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49  **     (3) PENDI
efb60 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  NG_LOCK.**     (
efb70 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  4) EXCLUSIVE_LOC
efb80 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  K.**.** Sometime
efb90 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e  s when requestin
efba0 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65  g one lock state
efbb0 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63  , additional loc
efbc0 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20  k states.** are 
efbd0 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77  inserted in betw
efbe0 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e  een.  The lockin
efbf0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20  g might fail on 
efc00 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72  one of the later
efc10 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
efc20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b  leaving the lock
efc30 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74   state different
efc40 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74   from what it st
efc50 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69  arted but.** sti
efc60 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20  ll short of its 
efc70 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  goal.  The follo
efc80 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73  wing chart shows
efc90 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20   the allowed.** 
efca0 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20  transitions and 
efcb0 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74  the inserted int
efcc0 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73  ermediate states
efcd0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43  :.**.**    UNLOC
efce0 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a  KED -> SHARED.**
efcf0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45      SHARED -> RE
efd00 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41  SERVED.**    SHA
efd10 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  RED -> (PENDING)
efd20 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
efd30 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20      RESERVED -> 
efd40 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
efd50 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e  LUSIVE.**    PEN
efd60 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56  DING -> EXCLUSIV
efd70 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  E.**.** This rou
efd80 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69  tine will only i
efd90 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20  ncrease a lock. 
efda0 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29   The winUnlock()
efdb0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73   routine.** eras
efdc0 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20  es all locks at 
efdd0 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73  once and returns
efde0 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   us immediately 
efdf0 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  to locking level
efe00 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74   0..** It is not
efe10 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77   possible to low
efe20 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
efe30 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74  evel one step at
efe40 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a   a time.  You.**
efe50 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67 68   must go straigh
efe60 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  t to locking lev
efe70 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  el 0..*/.static 
efe80 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69  int winLock(sqli
efe90 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
efea0 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69  t locktype){.  i
efeb0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
efec0 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20  K;    /* Return 
efed0 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75  code from subrou
efee0 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72  tines */.  int r
efef0 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  es = 1;         
eff00 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61    /* Result of a
eff10 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61   windows lock ca
eff20 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c  ll */.  int newL
eff30 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f  ocktype;       /
eff40 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63  * Set pFile->loc
eff50 6b 74 79 70 65 20 74 6f 20 74 68 69 73 20 76 61  ktype to this va
eff60 6c 75 65 20 62 65 66 6f 72 65 20 65 78 69 74 69  lue before exiti
eff70 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50  ng */.  int gotP
eff80 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f  endingLock = 0;/
eff90 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 63 71  * True if we acq
effa0 75 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20  uired a PENDING 
effb0 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a  lock this time *
effc0 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  /.  winFile *pFi
effd0 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
effe0 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72  d;.  DWORD error
efff0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20   = NO_ERROR;..  
f0000 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
f0010 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43  .  OSTRACE5("LOC
f0020 4b 20 25 64 20 25 64 20 77 61 73 20 25 64 28 25  K %d %d was %d(%
f0030 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  d)\n",.         
f0040 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
f0050 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ype, pFile->lock
f0060 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61  type, pFile->sha
f0070 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20  redLockByte);.. 
f0080 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
f0090 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f  already a lock o
f00a0 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d  f this type or m
f00b0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20  ore restrictive 
f00c0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69  on the.  ** OsFi
f00d0 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20  le, do nothing. 
f00e0 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e  Don't use the en
f00f0 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74  d_lock: exit pat
f0100 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74  h, as.  ** sqlit
f0110 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29  e3OsEnterMutex()
f0120 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c   hasn't been cal
f0130 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20  led yet..  */.  
f0140 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
f0150 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype>=locktype ){
f0160 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f0170 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
f0180 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c   Make sure the l
f0190 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20  ocking sequence 
f01a0 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a  is correct.  */.
f01b0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
f01c0 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f  >locktype!=NO_LO
f01d0 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d  CK || locktype==
f01e0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
f01f0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
f0200 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  e!=PENDING_LOCK 
f0210 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63  );.  assert( loc
f0220 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f  ktype!=RESERVED_
f0230 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c  LOCK || pFile->l
f0240 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
f0250 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f  LOCK );..  /* Lo
f0260 63 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c  ck the PENDING_L
f0270 4f 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e  OCK byte if we n
f0280 65 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61  eed to acquire a
f0290 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72   PENDING lock or
f02a0 0a 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c  .  ** a SHARED l
f02b0 6f 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20  ock.  If we are 
f02c0 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52  acquiring a SHAR
f02d0 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71  ED lock, the acq
f02e0 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a  uisition of.  **
f02f0 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   the PENDING_LOC
f0300 4b 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72  K byte is tempor
f0310 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c  ary..  */.  newL
f0320 6f 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d  ocktype = pFile-
f0330 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28  >locktype;.  if(
f0340 20 20 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74     (pFile->lockt
f0350 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20  ype==NO_LOCK).  
f0360 20 20 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74     || (   (lockt
f0370 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
f0380 4f 43 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26  OCK).         &&
f0390 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
f03a0 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e==RESERVED_LOCK
f03b0 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  )).  ){.    int 
f03c0 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69  cnt = 3;.    whi
f03d0 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28  le( cnt-->0 && (
f03e0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70  res = LockFile(p
f03f0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
f0400 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29  _BYTE, 0, 1, 0))
f0410 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
f0420 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67  Try 3 times to g
f0430 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c  et the pending l
f0440 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e  ock.  The pendin
f0450 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a  g lock might be.
f0460 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79        ** held by
f0470 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20   another reader 
f0480 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c  process who will
f0490 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65   release it mome
f04a0 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a  ntarily..      *
f04b0 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  /.      OSTRACE2
f04c0 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20  ("could not get 
f04d0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20  a PENDING lock. 
f04e0 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b  cnt=%d\n", cnt);
f04f0 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b  .      Sleep(1);
f0500 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65  .    }.    gotPe
f0510 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b  ndingLock = res;
f0520 0a 20 20 20 20 69 66 28 20 21 72 65 73 20 29 7b  .    if( !res ){
f0530 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47  .      error = G
f0540 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
f0550 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
f0560 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64 20  cquire a shared 
f0570 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
f0580 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
f0590 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
f05a0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
f05b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  e->locktype==NO_
f05c0 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20  LOCK );.    res 
f05d0 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46  = getReadLock(pF
f05e0 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65  ile);.    if( re
f05f0 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f  s ){.      newLo
f0600 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f  cktype = SHARED_
f0610 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
f0620 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47  .      error = G
f0630 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
f0640 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
f0650 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45  cquire a RESERVE
f0660 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  D lock.  */.  if
f0670 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45  ( locktype==RESE
f0680 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73  RVED_LOCK && res
f0690 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
f06a0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
f06b0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
f06c0 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69      res = LockFi
f06d0 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53  le(pFile->h, RES
f06e0 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31  ERVED_BYTE, 0, 1
f06f0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65  , 0);.    if( re
f0700 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f  s ){.      newLo
f0710 63 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56 45  cktype = RESERVE
f0720 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73  D_LOCK;.    }els
f0730 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d  e{.      error =
f0740 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b   GetLastError();
f0750 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
f0760 20 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49   Acquire a PENDI
f0770 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  NG lock.  */.  i
f0780 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
f0790 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72  LUSIVE_LOCK && r
f07a0 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63  es ){.    newLoc
f07b0 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f  ktype = PENDING_
f07c0 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e  LOCK;.    gotPen
f07d0 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  dingLock = 0;.  
f07e0 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
f07f0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
f0800 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
f0810 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
f0820 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
f0830 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
f0840 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41  e->locktype>=SHA
f0850 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
f0860 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64  res = unlockRead
f0870 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
f0880 20 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61   OSTRACE2("unrea
f0890 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72  dlock = %d\n", r
f08a0 65 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c  es);.    res = L
f08b0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
f08c0 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20  , SHARED_FIRST, 
f08d0 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20  0, SHARED_SIZE, 
f08e0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20  0);.    if( res 
f08f0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
f0900 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45  type = EXCLUSIVE
f0910 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
f0920 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20  {.      error = 
f0930 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
f0940 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22        OSTRACE2("
f0950 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c  error-code = %d\
f0960 6e 22 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20  n", error);.    
f0970 20 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46    getReadLock(pF
f0980 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ile);.    }.  }.
f0990 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
f09a0 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e  holding a PENDIN
f09b0 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68  G lock that ough
f09c0 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64  t to be released
f09d0 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65  , then.  ** rele
f09e0 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f  ase it now..  */
f09f0 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e  .  if( gotPendin
f0a00 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70  gLock && locktyp
f0a10 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
f0a20 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65  {.    UnlockFile
f0a30 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49  (pFile->h, PENDI
f0a40 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30  NG_BYTE, 0, 1, 0
f0a50 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64  );.  }..  /* Upd
f0a60 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  ate the state of
f0a70 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65   the lock has he
f0a80 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64  ld in the file d
f0a90 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20  escriptor then. 
f0aa0 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61   ** return the a
f0ab0 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c  ppropriate resul
f0ac0 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69  t code..  */.  i
f0ad0 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63  f( res ){.    rc
f0ae0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f0af0 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41  }else{.    OSTRA
f0b00 43 45 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44  CE4("LOCK FAILED
f0b10 20 25 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25   %d trying for %
f0b20 64 20 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c  d but got %d\n",
f0b30 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
f0b40 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20        locktype, 
f0b50 6e 65 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20  newLocktype);.  
f0b60 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
f0b70 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20  no = error;.    
f0b80 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
f0b90 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c  ;.  }.  pFile->l
f0ba0 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65  ocktype = (u8)ne
f0bb0 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74  wLocktype;.  ret
f0bc0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
f0bd0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
f0be0 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73  ecks if there is
f0bf0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
f0c00 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65   held on the spe
f0c10 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62  cified.** file b
f0c20 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74  y this or any ot
f0c30 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20  her process. If 
f0c40 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68  such a lock is h
f0c50 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e  eld, return.** n
f0c60 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69  on-zero, otherwi
f0c70 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74  se zero..*/.stat
f0c80 69 63 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52  ic int winCheckR
f0c90 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
f0ca0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
f0cb0 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69  t *pResOut){.  i
f0cc0 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65  nt rc;.  winFile
f0cd0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
f0ce0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72  le*)id;..  asser
f0cf0 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66  t( id!=0 );.  if
f0d00 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
f0d10 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
f0d20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a   ){.    rc = 1;.
f0d30 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45      OSTRACE3("TE
f0d40 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
f0d50 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69   (local)\n", pFi
f0d60 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65  le->h, rc);.  }e
f0d70 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f  lse{.    rc = Lo
f0d80 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
f0d90 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
f0da0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 1, 0);.    if
f0db0 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e  ( rc ){.      Un
f0dc0 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
f0dd0 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  h, RESERVED_BYTE
f0de0 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  , 0, 1, 0);.    
f0df0 7d 0a 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a  }.    rc = !rc;.
f0e00 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45      OSTRACE3("TE
f0e10 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
f0e20 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46   (remote)\n", pF
f0e30 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d  ile->h, rc);.  }
f0e40 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63  .  *pResOut = rc
f0e50 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
f0e60 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  E_OK;.}../*.** L
f0e70 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
f0e80 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
f0e90 65 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20  escriptor id to 
f0ea0 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74  locktype.  lockt
f0eb0 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  ype.** must be e
f0ec0 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72  ither NO_LOCK or
f0ed0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a   SHARED_LOCK..**
f0ee0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69  .** If the locki
f0ef0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  ng level of the 
f0f00 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
f0f10 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72  is already at or
f0f20 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65   below.** the re
f0f30 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20  quested locking 
f0f40 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74  level, this rout
f0f50 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
f0f60 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20  **.** It is not 
f0f70 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69  possible for thi
f0f80 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69  s routine to fai
f0f90 6c 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20  l if the second 
f0fa0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e  argument.** is N
f0fb0 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20  O_LOCK.  If the 
f0fc0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
f0fd0 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74  is SHARED_LOCK t
f0fe0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
f0ff0 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e  .** might return
f1000 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a   SQLITE_IOERR;.*
f1010 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
f1020 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
f1030 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
f1040 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79  ktype){.  int ty
f1050 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  pe;.  winFile *p
f1060 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
f1070 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  )id;.  int rc = 
f1080 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
f1090 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b  ert( pFile!=0 );
f10a0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
f10b0 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
f10c0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22   );.  OSTRACE5("
f10d0 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20  UNLOCK %d to %d 
f10e0 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70  was %d(%d)\n", p
f10f0 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
f1100 65 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  e,.          pFi
f1110 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46  le->locktype, pF
f1120 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42  ile->sharedLockB
f1130 79 74 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70  yte);.  type = p
f1140 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a  File->locktype;.
f1150 20 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c    if( type>=EXCL
f1160 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20  USIVE_LOCK ){.  
f1170 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69    UnlockFile(pFi
f1180 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49  le->h, SHARED_FI
f1190 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53  RST, 0, SHARED_S
f11a0 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  IZE, 0);.    if(
f11b0 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
f11c0 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65  D_LOCK && !getRe
f11d0 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b  adLock(pFile) ){
f11e0 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73  .      /* This s
f11f0 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70  hould never happ
f1200 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61  en.  We should a
f1210 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f  lways be able to
f1220 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75  .      ** reacqu
f1230 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63  ire the read loc
f1240 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  k */.      rc = 
f1250 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
f1260 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  OCK;.    }.  }. 
f1270 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52   if( type>=RESER
f1280 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  VED_LOCK ){.    
f1290 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  UnlockFile(pFile
f12a0 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59  ->h, RESERVED_BY
f12b0 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  TE, 0, 1, 0);.  
f12c0 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  }.  if( locktype
f12d0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70  ==NO_LOCK && typ
f12e0 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e>=SHARED_LOCK )
f12f0 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64  {.    unlockRead
f1300 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d  Lock(pFile);.  }
f1310 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e  .  if( type>=PEN
f1320 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  DING_LOCK ){.   
f1330 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
f1340 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59  e->h, PENDING_BY
f1350 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  TE, 0, 1, 0);.  
f1360 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  }.  pFile->lockt
f1370 79 70 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79  ype = (u8)lockty
f1380 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  pe;.  return rc;
f1390 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f  .}../*.** Contro
f13a0 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74  l and query of t
f13b0 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e  he open file han
f13c0 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  dle..*/.static i
f13d0 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f  nt winFileContro
f13e0 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  l(sqlite3_file *
f13f0 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  id, int op, void
f1400 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63   *pArg){.  switc
f1410 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  h( op ){.    cas
f1420 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c  e SQLITE_FCNTL_L
f1430 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20  OCKSTATE: {.    
f1440 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20    *(int*)pArg = 
f1450 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e  ((winFile*)id)->
f1460 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20  locktype;.      
f1470 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f1480 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
f1490 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52   SQLITE_LAST_ERR
f14a0 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e  NO: {.      *(in
f14b0 74 2a 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28  t*)pArg = (int)(
f14c0 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (winFile*)id)->l
f14d0 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  astErrno;.      
f14e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f14f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
f1500 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
f1510 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  R;.}../*.** Retu
f1520 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  rn the sector si
f1530 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
f1540 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c  he underlying bl
f1550 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a  ock device for.*
f1560 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  * the specified 
f1570 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c  file. This is al
f1580 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20  most always 512 
f1590 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62  bytes, but may b
f15a0 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20  e.** larger for 
f15b0 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a  some devices..**
f15c0 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20  .** SQLite code 
f15d0 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e  assumes this fun
f15e0 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69  ction cannot fai
f15f0 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d  l. It also assum
f1600 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77  es that.** if tw
f1610 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61  o files are crea
f1620 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ted in the same 
f1630 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65  file-system dire
f1640 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61  ctory (i.e..** a
f1650 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74   database and it
f1660 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20  s journal file) 
f1670 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20  that the sector 
f1680 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65  size will be the
f1690 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74  .** same for bot
f16a0 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h..*/.static int
f16b0 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73   winSectorSize(s
f16c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
f16d0 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  {.  assert( id!=
f16e0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69  0 );.  return (i
f16f0 6e 74 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69  nt)(((winFile*)i
f1700 64 29 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b  d)->sectorSize);
f1710 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
f1720 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76   a vector of dev
f1730 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74  ice characterist
f1740 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ics..*/.static i
f1750 6e 74 20 77 69 6e 44 65 76 69 63 65 43 68 61 72  nt winDeviceChar
f1760 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69  acteristics(sqli
f1770 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
f1780 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f1790 52 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  R(id);.  return 
f17a0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
f17b0 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20   vector defines 
f17c0 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20  all the methods 
f17d0 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65  that can operate
f17e0 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65   on an.** sqlite
f17f0 33 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32  3_file for win32
f1800 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
f1810 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
f1820 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64  hods winIoMethod
f1830 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20   = {.  1,       
f1840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1850 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
f1860 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69    winClose,.  wi
f1870 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74  nRead,.  winWrit
f1880 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65  e,.  winTruncate
f1890 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77  ,.  winSync,.  w
f18a0 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69  inFileSize,.  wi
f18b0 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f  nLock,.  winUnlo
f18c0 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65  ck,.  winCheckRe
f18d0 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69  servedLock,.  wi
f18e0 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20  nFileControl,.  
f18f0 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20  winSectorSize,. 
f1900 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63   winDeviceCharac
f1910 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a  teristics.};../*
f1920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65  **********.** He
f1970 72 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20  re ends the I/O 
f1980 6d 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72  methods that for
f1990 6d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f  m the sqlite3_io
f19a0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e  _methods object.
f19b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  .**.** The next 
f19c0 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d  block of code im
f19d0 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53  plements the VFS
f19e0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a   methods..******
f19f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1a30 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  ******/../*.** C
f1a40 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 66  onvert a UTF-8 f
f1a50 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61  ilename into wha
f1a60 74 65 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75  tever form the u
f1a70 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65  nderlying.** ope
f1a80 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61  rating system wa
f1a90 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e  nts filenames in
f1aa0 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
f1ab0 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69   the result.** i
f1ac0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
f1ad0 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20  malloc and must 
f1ae0 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20  be freed by the 
f1af0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74  calling.** funct
f1b00 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
f1b10 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38  oid *convertUtf8
f1b20 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63  Filename(const c
f1b30 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
f1b40 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72  .  void *zConver
f1b50 74 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69  ted = 0;.  if( i
f1b60 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f  sNT() ){.    zCo
f1b70 6e 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f  nverted = utf8To
f1b80 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d  Unicode(zFilenam
f1b90 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73  e);./* isNT() is
f1ba0 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
f1bb0 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
f1bc0 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
f1bd0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69  executed. .*/.#i
f1be0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
f1bf0 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  E==0.  }else{.  
f1c00 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75    zConverted = u
f1c10 74 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e  tf8ToMbcs(zFilen
f1c20 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  ame);.#endif.  }
f1c30 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c  .  /* caller wil
f1c40 6c 20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20  l handle out of 
f1c50 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75  memory */.  retu
f1c60 72 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d  rn zConverted;.}
f1c70 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
f1c80 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
f1c90 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a  name in zBuf.  z
f1ca0 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20  Buf must be big 
f1cb0 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c  enough to.** hol
f1cc0 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74  d at pVfs->mxPat
f1cd0 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73  hname characters
f1ce0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f1cf0 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20  getTempname(int 
f1d00 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
f1d10 29 7b 0a 20 20 73 74 61 74 69 63 20 63 68 61 72  ){.  static char
f1d20 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20   zChars[] =.    
f1d30 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f  "abcdefghijklmno
f1d40 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20  pqrstuvwxyz".   
f1d50 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e   "ABCDEFGHIJKLMN
f1d60 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20  OPQRSTUVWXYZ".  
f1d70 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a    "0123456789";.
f1d80 20 20 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20    size_t i, j;. 
f1d90 20 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b   char zTempPath[
f1da0 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69  MAX_PATH+1];.  i
f1db0 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  f( sqlite3_temp_
f1dc0 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20  directory ){.   
f1dd0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
f1de0 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a  f(MAX_PATH-30, z
f1df0 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20  TempPath, "%s", 
f1e00 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
f1e10 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65  ectory);.  }else
f1e20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
f1e30 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b     char *zMulti;
f1e40 0a 20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65  .    WCHAR zWide
f1e50 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a  Path[MAX_PATH];.
f1e60 20 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 57      GetTempPathW
f1e70 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57  (MAX_PATH-30, zW
f1e80 69 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d  idePath);.    zM
f1e90 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f  ulti = unicodeTo
f1ea0 55 74 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b  Utf8(zWidePath);
f1eb0 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20  .    if( zMulti 
f1ec0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
f1ed0 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41  _snprintf(MAX_PA
f1ee0 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68  TH-30, zTempPath
f1ef0 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b  , "%s", zMulti);
f1f00 0a 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c  .      free(zMul
f1f10 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ti);.    }else{.
f1f20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
f1f30 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
f1f40 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
f1f50 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f1f60 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
f1f70 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
f1f80 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65  cuted. .** Since
f1f90 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69   the ASCII versi
f1fa0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
f1fb0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
f1fc0 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a  xist for WINCE,.
f1fd0 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e  ** it's importan
f1fe0 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e  t to not referen
f1ff0 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43  ce them for WINC
f2000 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  E builds..*/.#if
f2010 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f2020 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
f2030 20 63 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20   char *zUtf8;.  
f2040 20 20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68    char zMbcsPath
f2050 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20  [MAX_PATH];.    
f2060 47 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58  GetTempPathA(MAX
f2070 5f 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50  _PATH-30, zMbcsP
f2080 61 74 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20  ath);.    zUtf8 
f2090 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f  = sqlite3_win32_
f20a0 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62  mbcs_to_utf8(zMb
f20b0 63 73 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28  csPath);.    if(
f20c0 20 7a 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20   zUtf8 ){.      
f20d0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
f20e0 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54  (MAX_PATH-30, zT
f20f0 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a  empPath, "%s", z
f2100 55 74 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65  Utf8);.      fre
f2110 65 28 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65  e(zUtf8);.    }e
f2120 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
f2130 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
f2140 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
f2150 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33  .  for(i=sqlite3
f2160 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61  Strlen30(zTempPa
f2170 74 68 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d  th); i>0 && zTem
f2180 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27  pPath[i-1]=='\\'
f2190 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70  ; i--){}.  zTemp
f21a0 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73  Path[i] = 0;.  s
f21b0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
f21c0 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20  nBuf-30, zBuf,. 
f21d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f21e0 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54    "%s\\"SQLITE_T
f21f0 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c  EMP_FILE_PREFIX,
f2200 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a   zTempPath);.  j
f2210 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
f2220 33 30 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69  30(zBuf);.  sqli
f2230 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32  te3_randomness(2
f2240 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20  0, &zBuf[j]);.  
f2250 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69  for(i=0; i<20; i
f2260 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42  ++, j++){.    zB
f2270 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43  uf[j] = (char)zC
f2280 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64  hars[ ((unsigned
f2290 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28   char)zBuf[j])%(
f22a0 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31  sizeof(zChars)-1
f22b0 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b  ) ];.  }.  zBuf[
f22c0 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43  j] = 0;.  OSTRAC
f22d0 45 32 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d  E2("TEMP FILENAM
f22e0 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b  E: %s\n", zBuf);
f22f0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f2300 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  _OK; .}../*.** T
f2310 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
f2320 6f 66 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d  of getLastErrorM
f2330 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66  sg.** is zero if
f2340 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
f2350 67 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62  ge fits in the b
f2360 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65  uffer, or non-ze
f2370 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20  ro.** otherwise 
f2380 28 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20  (if the message 
f2390 77 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a  was truncated)..
f23a0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
f23b0 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e  tLastErrorMsg(in
f23c0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
f23d0 75 66 29 7b 0a 20 20 44 57 4f 52 44 20 65 72 72  uf){.  DWORD err
f23e0 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  or = GetLastErro
f23f0 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45  r();..#if SQLITE
f2400 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73 71 6c 69  _OS_WINCE.  sqli
f2410 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
f2420 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f  f, zBuf, "OsErro
f2430 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72  r 0x%x (%u)", er
f2440 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 23 65 6c  ror, error);.#el
f2450 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65  se.  /* FormatMe
f2460 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 20  ssage returns 0 
f2470 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68  on failure.  Oth
f2480 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72  erwise it.  ** r
f2490 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
f24a0 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 74  r of TCHARs writ
f24b0 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ten to the outpu
f24c0 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65  t.  ** buffer, e
f24d0 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72  xcluding the ter
f24e0 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68  minating null ch
f24f0 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 20 28 21  ar..  */.  if (!
f2500 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46  FormatMessageA(F
f2510 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52  ORMAT_MESSAGE_FR
f2520 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 20 20 20  OM_SYSTEM,.     
f2530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2540 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20   NULL,.         
f2550 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72               err
f2560 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  or,.            
f2570 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
f2580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2590 20 20 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20     zBuf,.       
f25a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e                 n
f25b0 42 75 66 2d 31 2c 0a 20 20 20 20 20 20 20 20 20  Buf-1,.         
f25c0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 29               0))
f25d0 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  .  {.    sqlite3
f25e0 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20  _snprintf(nBuf, 
f25f0 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30  zBuf, "OsError 0
f2600 78 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72  x%x (%u)", error
f2610 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d 0a 23 65  , error);.  }.#e
f2620 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 30  ndif..  return 0
f2630 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
f2640 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
f2650 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28 0a 20  c int winOpen(. 
f2660 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
f2670 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  fs,        /* No
f2680 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  t used */.  cons
f2690 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
f26a0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
f26b0 20 74 68 65 20 66 69 6c 65 20 28 55 54 46 2d 38   the file (UTF-8
f26c0 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  ) */.  sqlite3_f
f26d0 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20  ile *id,        
f26e0 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 53 51   /* Write the SQ
f26f0 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65  Lite file handle
f2700 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66   here */.  int f
f2710 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
f2720 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64       /* Open mod
f2730 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74  e flags */.  int
f2740 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20   *pOutFlags     
f2750 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73         /* Status
f2760 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f   return flags */
f2770 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a  .){.  HANDLE h;.
f2780 20 20 44 57 4f 52 44 20 64 77 44 65 73 69 72 65    DWORD dwDesire
f2790 64 41 63 63 65 73 73 3b 0a 20 20 44 57 4f 52 44  dAccess;.  DWORD
f27a0 20 64 77 53 68 61 72 65 4d 6f 64 65 3b 0a 20 20   dwShareMode;.  
f27b0 44 57 4f 52 44 20 64 77 43 72 65 61 74 69 6f 6e  DWORD dwCreation
f27c0 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 20 44  Disposition;.  D
f27d0 57 4f 52 44 20 64 77 46 6c 61 67 73 41 6e 64 41  WORD dwFlagsAndA
f27e0 74 74 72 69 62 75 74 65 73 20 3d 20 30 3b 0a 23  ttributes = 0;.#
f27f0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f2800 43 45 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 20  CE.  int isTemp 
f2810 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 77 69  = 0;.#endif.  wi
f2820 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
f2830 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 76  winFile*)id;.  v
f2840 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b  oid *zConverted;
f2850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2860 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20   /* Filename in 
f2870 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  OS encoding */. 
f2880 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 74   const char *zUt
f2890 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20  f8Name = zName; 
f28a0 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69     /* Filename i
f28b0 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67  n UTF-8 encoding
f28c0 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e   */.  char zTmpn
f28d0 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b  ame[MAX_PATH+1];
f28e0 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65          /* Buffe
f28f0 72 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  r used to create
f2900 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d 65 20 2a   temp filename *
f2910 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21  /..  assert( id!
f2920 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =0 );.  UNUSED_P
f2930 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
f2940 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63  .  /* If the sec
f2950 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
f2960 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
f2970 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20   NULL, generate 
f2980 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72  a .  ** temporar
f2990 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75  y file name to u
f29a0 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  se .  */.  if( !
f29b0 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20  zUtf8Name ){.   
f29c0 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d   int rc = getTem
f29d0 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b 31  pname(MAX_PATH+1
f29e0 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20  , zTmpname);.   
f29f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
f2a00 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
f2a10 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
f2a20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 6d   zUtf8Name = zTm
f2a30 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  pname;.  }..  /*
f2a40 20 43 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c   Convert the fil
f2a50 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73  ename to the sys
f2a60 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f  tem encoding. */
f2a70 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  .  zConverted = 
f2a80 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
f2a90 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a  ame(zUtf8Name);.
f2aa0 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64    if( zConverted
f2ab0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f2ac0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
f2ad0 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73    }..  if( flags
f2ae0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
f2af0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20  EADWRITE ){.    
f2b00 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20  dwDesiredAccess 
f2b10 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c  = GENERIC_READ |
f2b20 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 3b 0a   GENERIC_WRITE;.
f2b30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 44    }else{.    dwD
f2b40 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 47  esiredAccess = G
f2b50 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20 20 7d  ENERIC_READ;.  }
f2b60 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  .  if( flags & S
f2b70 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
f2b80 45 20 29 7b 0a 20 20 20 20 64 77 43 72 65 61 74  E ){.    dwCreat
f2b90 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d  ionDisposition =
f2ba0 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 20   OPEN_ALWAYS;.  
f2bb0 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 43 72 65  }else{.    dwCre
f2bc0 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e  ationDisposition
f2bd0 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47   = OPEN_EXISTING
f2be0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67  ;.  }.  if( flag
f2bf0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
f2c00 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 64  MAIN_DB ){.    d
f2c10 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c  wShareMode = FIL
f2c20 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46  E_SHARE_READ | F
f2c30 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b  ILE_SHARE_WRITE;
f2c40 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77  .  }else{.    dw
f2c50 53 68 61 72 65 4d 6f 64 65 20 3d 20 30 3b 0a 20  ShareMode = 0;. 
f2c60 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26   }.  if( flags &
f2c70 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
f2c80 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69  ETEONCLOSE ){.#i
f2c90 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
f2ca0 45 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64  E.    dwFlagsAnd
f2cb0 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c  Attributes = FIL
f2cc0 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44  E_ATTRIBUTE_HIDD
f2cd0 45 4e 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d  EN;.    isTemp =
f2ce0 20 31 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77   1;.#else.    dw
f2cf0 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74  FlagsAndAttribut
f2d00 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42  es = FILE_ATTRIB
f2d10 55 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20  UTE_TEMPORARY.  
f2d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2d30 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46               | F
f2d40 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49  ILE_ATTRIBUTE_HI
f2d50 44 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20  DDEN.           
f2d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2d70 20 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f      | FILE_FLAG_
f2d80 44 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b  DELETE_ON_CLOSE;
f2d90 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b  .#endif.  }else{
f2da0 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41  .    dwFlagsAndA
f2db0 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45  ttributes = FILE
f2dc0 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41  _ATTRIBUTE_NORMA
f2dd0 4c 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f  L;.  }.  /* Repo
f2de0 72 74 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74  rts from the int
f2df0 65 72 6e 65 74 20 61 72 65 20 74 68 61 74 20 70  ernet are that p
f2e00 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c  erformance is al
f2e10 77 61 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72  ways.  ** better
f2e20 20 69 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41   if FILE_FLAG_RA
f2e30 4e 44 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75  NDOM_ACCESS is u
f2e40 73 65 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36  sed.  Ticket #26
f2e50 39 39 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54  99. */.#if SQLIT
f2e60 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46  E_OS_WINCE.  dwF
f2e70 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
f2e80 73 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52  s |= FILE_FLAG_R
f2e90 41 4e 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65  ANDOM_ACCESS;.#e
f2ea0 6e 64 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28  ndif.  if( isNT(
f2eb0 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65  ) ){.    h = Cre
f2ec0 61 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a  ateFileW((WCHAR*
f2ed0 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20  )zConverted,.   
f2ee0 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63      dwDesiredAcc
f2ef0 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68  ess,.       dwSh
f2f00 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20  areMode,.       
f2f10 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43  NULL,.       dwC
f2f20 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69  reationDispositi
f2f30 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61  on,.       dwFla
f2f40 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c  gsAndAttributes,
f2f50 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20  .       NULL.   
f2f60 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73   );./* isNT() is
f2f70 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
f2f80 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
f2f90 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
f2fa0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69  executed. .** Si
f2fb0 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65  nce the ASCII ve
f2fc0 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57  rsion of these W
f2fd0 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f  indows API do no
f2fe0 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43  t exist for WINC
f2ff0 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72  E,.** it's impor
f3000 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65  tant to not refe
f3010 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57  rence them for W
f3020 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a  INCE builds..*/.
f3030 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
f3040 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a  NCE==0.  }else{.
f3050 20 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69      h = CreateFi
f3060 6c 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76  leA((char*)zConv
f3070 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77  erted,.       dw
f3080 44 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20  DesiredAccess,. 
f3090 20 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64        dwShareMod
f30a0 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a  e,.       NULL,.
f30b0 20 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f         dwCreatio
f30c0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20  nDisposition,.  
f30d0 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41       dwFlagsAndA
f30e0 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20  ttributes,.     
f30f0 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65    NULL.    );.#e
f3100 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68  ndif.  }.  if( h
f3110 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45  ==INVALID_HANDLE
f3120 5f 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72  _VALUE ){.    fr
f3130 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
f3140 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
f3150 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
f3160 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72  WRITE ){.      r
f3170 65 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56  eturn winOpen(pV
f3180 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a  fs, zName, id, .
f3190 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66               ((f
f31a0 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  lags|SQLITE_OPEN
f31b0 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49  _READONLY)&~SQLI
f31c0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
f31d0 45 29 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a  E), pOutFlags);.
f31e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
f31f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
f3200 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20  ANTOPEN;.    }. 
f3210 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61   }.  if( pOutFla
f3220 67 73 20 29 7b 0a 20 20 20 20 69 66 28 20 66 6c  gs ){.    if( fl
f3230 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
f3240 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20  N_READWRITE ){. 
f3250 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20       *pOutFlags 
f3260 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  = SQLITE_OPEN_RE
f3270 41 44 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c  ADWRITE;.    }el
f3280 73 65 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46  se{.      *pOutF
f3290 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50  lags = SQLITE_OP
f32a0 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20  EN_READONLY;.   
f32b0 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28   }.  }.  memset(
f32c0 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66  pFile, 0, sizeof
f32d0 28 2a 70 46 69 6c 65 29 29 3b 0a 20 20 70 46 69  (*pFile));.  pFi
f32e0 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77  le->pMethod = &w
f32f0 69 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46  inIoMethod;.  pF
f3300 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 46  ile->h = h;.  pF
f3310 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
f3320 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 70 46 69   NO_ERROR;.  pFi
f3330 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d  le->sectorSize =
f3340 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70   getSectorSize(p
f3350 56 66 73 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b  Vfs, zUtf8Name);
f3360 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
f3370 49 4e 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67  INCE.  if( (flag
f3380 73 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e  s & (SQLITE_OPEN
f3390 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
f33a0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29  E_OPEN_MAIN_DB))
f33b0 20 3d 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20   ==.            
f33c0 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f     (SQLITE_OPEN_
f33d0 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45  READWRITE|SQLITE
f33e0 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20  _OPEN_MAIN_DB). 
f33f0 20 20 20 20 20 20 26 26 20 21 77 69 6e 63 65 43        && !winceC
f3400 72 65 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c  reateLock(zName,
f3410 20 70 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20   pFile).  ){.   
f3420 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b   CloseHandle(h);
f3430 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65  .    free(zConve
f3440 72 74 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72  rted);.    retur
f3450 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  n SQLITE_CANTOPE
f3460 4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54  N;.  }.  if( isT
f3470 65 6d 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  emp ){.    pFile
f3480 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65  ->zDeleteOnClose
f3490 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20   = zConverted;. 
f34a0 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
f34b0 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76  {.    free(zConv
f34c0 65 72 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70  erted);.  }.  Op
f34d0 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20  enCounter(+1);. 
f34e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f34f0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  K;.}../*.** Dele
f3500 74 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c  te the named fil
f3510 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  e..**.** Note th
f3520 61 74 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20  at windows does 
f3530 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65  not allow a file
f3540 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69   to be deleted i
f3550 66 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  f some other.** 
f3560 70 72 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f  process has it o
f3570 70 65 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20  pen.  Sometimes 
f3580 61 20 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20  a virus scanner 
f3590 6f 72 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67  or indexing prog
f35a0 72 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e  ram.** will open
f35b0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20   a journal file 
f35c0 73 68 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74  shortly after it
f35d0 20 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f   is created in o
f35e0 72 64 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68  rder to do.** wh
f35f0 61 74 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20  atever it does. 
f3600 20 57 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65   While this othe
f3610 72 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c  r process is hol
f3620 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65  ding the.** file
f3630 20 6f 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62   open, we will b
f3640 65 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65  e unable to dele
f3650 74 65 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20  te it.  To work 
f3660 61 72 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70  around this.** p
f3670 72 6f 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79  roblem, we delay
f3680 20 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64   100 millisecond
f3690 73 20 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c  s and try to del
f36a0 65 74 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a  ete again.  Up.*
f36b0 2a 20 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e  * to MX_DELETION
f36c0 5f 41 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69  _ATTEMPTs deleti
f36d0 6f 6e 20 61 74 74 65 6d 70 74 73 20 61 72 65 20  on attempts are 
f36e0 72 75 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e  run before givin
f36f0 67 0a 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75  g.** up and retu
f3700 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a  rning an error..
f3710 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45  */.#define MX_DE
f3720 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20  LETION_ATTEMPTS 
f3730 35 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  5.static int win
f3740 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65  Delete(.  sqlite
f3750 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
f3760 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65        /* Not use
f3770 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20  d on win32 */.  
f3780 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
f3790 65 6e 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e  ename,      /* N
f37a0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64  ame of file to d
f37b0 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73  elete */.  int s
f37c0 79 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20  yncDir          
f37d0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
f37e0 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29  ed on win32 */.)
f37f0 7b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b  {.  int cnt = 0;
f3800 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44  .  DWORD rc;.  D
f3810 57 4f 52 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a  WORD error = 0;.
f3820 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
f3830 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  ed = convertUtf8
f3840 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61  Filename(zFilena
f3850 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  me);.  UNUSED_PA
f3860 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20  RAMETER(pVfs);. 
f3870 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f3880 52 28 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 66  R(syncDir);.  if
f3890 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20  ( zConverted==0 
f38a0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
f38b0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
f38c0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
f38d0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
f38e0 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20  IOERR_DELETE);. 
f38f0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
f3900 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c     do{.      Del
f3910 65 74 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72  eteFileW(zConver
f3920 74 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65  ted);.    }while
f3930 28 20 20 20 28 20 20 20 28 28 72 63 20 3d 20 47  (   (   ((rc = G
f3940 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
f3950 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21  W(zConverted)) !
f3960 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41  = INVALID_FILE_A
f3970 54 54 52 49 42 55 54 45 53 29 0a 20 20 20 20 20  TTRIBUTES).     
f3980 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65            || ((e
f3990 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72  rror = GetLastEr
f39a0 72 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f  ror()) == ERROR_
f39b0 41 43 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a  ACCESS_DENIED)).
f39c0 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2b             && (+
f39d0 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49  +cnt < MX_DELETI
f39e0 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20  ON_ATTEMPTS).   
f39f0 20 20 20 20 20 20 20 20 26 26 20 28 53 6c 65 65          && (Slee
f3a00 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a  p(100), 1) );./*
f3a10 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
f3a20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
f3a30 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
f3a40 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
f3a50 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68  ed. .** Since th
f3a60 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20  e ASCII version 
f3a70 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73  of these Windows
f3a80 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73   API do not exis
f3a90 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20  t for WINCE,.** 
f3aa0 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  it's important t
f3ab0 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20  o not reference 
f3ac0 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62  them for WINCE b
f3ad0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51  uilds..*/.#if SQ
f3ae0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
f3af0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f  .  }else{.    do
f3b00 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69  {.      DeleteFi
f3b10 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  leA(zConverted);
f3b20 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28  .    }while(   (
f3b30 20 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c     ((rc = GetFil
f3b40 65 41 74 74 72 69 62 75 74 65 73 41 28 7a 43 6f  eAttributesA(zCo
f3b50 6e 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56  nverted)) != INV
f3b60 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
f3b70 55 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20  UTES).          
f3b80 20 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20       || ((error 
f3b90 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
f3ba0 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53  ) == ERROR_ACCES
f3bb0 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20  S_DENIED)).     
f3bc0 20 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20        && (++cnt 
f3bd0 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54  < MX_DELETION_AT
f3be0 54 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20  TEMPTS).        
f3bf0 20 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30     && (Sleep(100
f3c00 29 2c 20 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a  ), 1) );.#endif.
f3c10 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76    }.  free(zConv
f3c20 65 72 74 65 64 29 3b 0a 20 20 4f 53 54 52 41 43  erted);.  OSTRAC
f3c30 45 32 28 22 44 45 4c 45 54 45 20 5c 22 25 73 5c  E2("DELETE \"%s\
f3c40 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29  "\n", zFilename)
f3c50 3b 0a 20 20 72 65 74 75 72 6e 20 28 20 20 20 28  ;.  return (   (
f3c60 72 63 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49  rc == INVALID_FI
f3c70 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 20 0a  LE_ATTRIBUTES) .
f3c80 20 20 20 20 20 20 20 20 20 20 26 26 20 28 65 72            && (er
f3c90 72 6f 72 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c  ror == ERROR_FIL
f3ca0 45 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20  E_NOT_FOUND)) ? 
f3cb0 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
f3cc0 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b  TE_IOERR_DELETE;
f3cd0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
f3ce0 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e  the existance an
f3cf0 64 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69  d status of a fi
f3d00 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
f3d10 74 20 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73  t winAccess(.  s
f3d20 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
f3d30 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74  ,         /* Not
f3d40 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a   used on win32 *
f3d50 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
f3d60 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f  zFilename,     /
f3d70 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
f3d80 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74  o check */.  int
f3d90 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
f3da0 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20          /* Type 
f3db0 6f 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20  of test to make 
f3dc0 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  on this file */.
f3dd0 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20    int *pResOut  
f3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f3df0 4f 55 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29  OUT: Result */.)
f3e00 7b 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a  {.  DWORD attr;.
f3e10 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20    int rc = 0;.  
f3e20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
f3e30 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69   = convertUtf8Fi
f3e40 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65  lename(zFilename
f3e50 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
f3e60 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69  METER(pVfs);.  i
f3e70 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30  f( zConverted==0
f3e80 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
f3e90 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
f3ea0 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
f3eb0 0a 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46  .    attr = GetF
f3ec0 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 28  ileAttributesW((
f3ed0 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65  WCHAR*)zConverte
f3ee0 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73  d);./* isNT() is
f3ef0 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
f3f00 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
f3f10 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
f3f20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69  executed. .** Si
f3f30 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65  nce the ASCII ve
f3f40 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57  rsion of these W
f3f50 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f  indows API do no
f3f60 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43  t exist for WINC
f3f70 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72  E,.** it's impor
f3f80 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65  tant to not refe
f3f90 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57  rence them for W
f3fa0 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a  INCE builds..*/.
f3fb0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
f3fc0 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a  NCE==0.  }else{.
f3fd0 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69      attr = GetFi
f3fe0 6c 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63  leAttributesA((c
f3ff0 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29  har*)zConverted)
f4000 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66  ;.#endif.  }.  f
f4010 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
f4020 0a 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73  .  switch( flags
f4030 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
f4040 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a  ITE_ACCESS_READ:
f4050 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
f4060 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a  _ACCESS_EXISTS:.
f4070 20 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21        rc = attr!
f4080 3d 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54  =INVALID_FILE_AT
f4090 54 52 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20  TRIBUTES;.      
f40a0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
f40b0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
f40c0 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72  ADWRITE:.      r
f40d0 63 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45  c = (attr & FILE
f40e0 5f 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f  _ATTRIBUTE_READO
f40f0 4e 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62  NLY)==0;.      b
f4100 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c  reak;.    defaul
f4110 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  t:.      assert(
f4120 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20  !"Invalid flags 
f4130 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a  argument");.  }.
f4140 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b    *pResOut = rc;
f4150 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f4160 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  _OK;.}.../*.** T
f4170 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70  urn a relative p
f4180 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66  athname into a f
f4190 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57  ull pathname.  W
f41a0 72 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a  rite the full.**
f41b0 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a   pathname into z
f41c0 4f 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77  Out[].  zOut[] w
f41d0 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20  ill be at least 
f41e0 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
f41f0 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a  .** bytes in siz
f4200 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
f4210 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65   winFullPathname
f4220 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
f4230 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20  *pVfs,          
f4240 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
f4250 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  vfs object */.  
f4260 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c  const char *zRel
f4270 61 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a  ative,        /*
f4280 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69   Possibly relati
f4290 76 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f  ve input path */
f42a0 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20  .  int nFull,   
f42b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f42c0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70   /* Size of outp
f42d0 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74  ut buffer in byt
f42e0 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46  es */.  char *zF
f42f0 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  ull             
f4300 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
f4310 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a  buffer */.){.  .
f4320 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 43 59  #if defined(__CY
f4330 47 57 49 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44  GWIN__).  UNUSED
f4340 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c  _PARAMETER(nFull
f4350 29 3b 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76  );.  cygwin_conv
f4360 5f 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70  _to_full_win32_p
f4370 61 74 68 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a  ath(zRelative, z
f4380 46 75 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20  Full);.  return 
f4390 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69  SQLITE_OK;.#endi
f43a0 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  f..#if SQLITE_OS
f43b0 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f  _WINCE.  UNUSED_
f43c0 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29  PARAMETER(nFull)
f43d0 3b 0a 20 20 2f 2a 20 57 69 6e 43 45 20 68 61 73  ;.  /* WinCE has
f43e0 20 6e 6f 20 63 6f 6e 63 65 70 74 20 6f 66 20 61   no concept of a
f43f0 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61   relative pathna
f4400 6d 65 2c 20 6f 72 20 73 6f 20 49 20 61 6d 20 74  me, or so I am t
f4410 6f 6c 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  old. */.  sqlite
f4420 33 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d  3_snprintf(pVfs-
f4430 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75  >mxPathname, zFu
f4440 6c 6c 2c 20 22 25 73 22 2c 20 7a 52 65 6c 61 74  ll, "%s", zRelat
f4450 69 76 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ive);.  return S
f4460 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66  QLITE_OK;.#endif
f4470 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53  ..#if !SQLITE_OS
f4480 5f 57 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e  _WINCE && !defin
f4490 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20  ed(__CYGWIN__). 
f44a0 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f   int nByte;.  vo
f44b0 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a  id *zConverted;.
f44c0 20 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20    char *zOut;.  
f44d0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f44e0 28 6e 46 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76  (nFull);.  zConv
f44f0 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55  erted = convertU
f4500 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c  tf8Filename(zRel
f4510 61 74 69 76 65 29 3b 0a 20 20 69 66 28 20 69 73  ative);.  if( is
f4520 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 43 48 41  NT() ){.    WCHA
f4530 52 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42  R *zTemp;.    nB
f4540 79 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74  yte = GetFullPat
f4550 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a  hNameW((WCHAR*)z
f4560 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c  Converted, 0, 0,
f4570 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65   0) + 3;.    zTe
f4580 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79  mp = malloc( nBy
f4590 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b  te*sizeof(zTemp[
f45a0 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a  0]) );.    if( z
f45b0 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Temp==0 ){.     
f45c0 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64   free(zConverted
f45d0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
f45e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
f45f0 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50    }.    GetFullP
f4600 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a  athNameW((WCHAR*
f4610 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79  )zConverted, nBy
f4620 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20  te, zTemp, 0);. 
f4630 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
f4640 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20  ed);.    zOut = 
f4650 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54  unicodeToUtf8(zT
f4660 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a  emp);.    free(z
f4670 54 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29  Temp);./* isNT()
f4680 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
f4690 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
f46a0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
f46b0 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a  er executed. .**
f46c0 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49   Since the ASCII
f46d0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73   version of thes
f46e0 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f  e Windows API do
f46f0 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57   not exist for W
f4700 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d  INCE,.** it's im
f4710 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72  portant to not r
f4720 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f  eference them fo
f4730 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a  r WINCE builds..
f4740 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f4750 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73  _WINCE==0.  }els
f4760 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65  e{.    char *zTe
f4770 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20  mp;.    nByte = 
f4780 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41  GetFullPathNameA
f4790 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74  ((char*)zConvert
f47a0 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33  ed, 0, 0, 0) + 3
f47b0 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61  ;.    zTemp = ma
f47c0 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65  lloc( nByte*size
f47d0 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a  of(zTemp[0]) );.
f47e0 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30      if( zTemp==0
f47f0 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a   ){.      free(z
f4800 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
f4810 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f4820 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
f4830 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65   GetFullPathName
f4840 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72  A((char*)zConver
f4850 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d  ted, nByte, zTem
f4860 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28  p, 0);.    free(
f4870 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
f4880 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f   zOut = sqlite3_
f4890 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74  win32_mbcs_to_ut
f48a0 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66  f8(zTemp);.    f
f48b0 72 65 65 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64  ree(zTemp);.#end
f48c0 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75  if.  }.  if( zOu
f48d0 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
f48e0 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e  _snprintf(pVfs->
f48f0 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c  mxPathname, zFul
f4900 6c 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a  l, "%s", zOut);.
f4910 20 20 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a      free(zOut);.
f4920 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
f4930 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
f4940 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f4950 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64  _NOMEM;.  }.#end
f4960 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  if.}../*.** Get 
f4970 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
f4980 6f 66 20 74 68 65 20 64 65 76 69 63 65 20 75 73  of the device us
f4990 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 66  ed to store.** f
f49a0 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
f49b0 6e 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65  nt getSectorSize
f49c0 28 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66  (.    sqlite3_vf
f49d0 73 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e  s *pVfs,.    con
f49e0 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69  st char *zRelati
f49f0 76 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20  ve     /* UTF-8 
f4a00 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a  file name */.){.
f4a10 20 20 44 57 4f 52 44 20 62 79 74 65 73 50 65 72    DWORD bytesPer
f4a20 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f  Sector = SQLITE_
f4a30 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53  DEFAULT_SECTOR_S
f4a40 49 5a 45 3b 0a 20 20 63 68 61 72 20 7a 46 75 6c  IZE;.  char zFul
f4a50 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31  lpath[MAX_PATH+1
f4a60 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44  ];.  int rc;.  D
f4a70 57 4f 52 44 20 64 77 52 65 74 20 3d 20 30 3b 0a  WORD dwRet = 0;.
f4a80 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 6e 65  .  /*.  ** We ne
f4a90 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 66 75  ed to get the fu
f4aa0 6c 6c 20 70 61 74 68 20 6e 61 6d 65 20 6f 66 20  ll path name of 
f4ab0 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 6f  the file.  ** to
f4ac0 20 67 65 74 20 74 68 65 20 64 72 69 76 65 20 6c   get the drive l
f4ad0 65 74 74 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70  etter to look up
f4ae0 20 74 68 65 20 73 65 63 74 6f 72 0a 20 20 2a 2a   the sector.  **
f4af0 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 72 63   size..  */.  rc
f4b00 20 3d 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61   = winFullPathna
f4b10 6d 65 28 70 56 66 73 2c 20 7a 52 65 6c 61 74 69  me(pVfs, zRelati
f4b20 76 65 2c 20 4d 41 58 5f 50 41 54 48 2c 20 7a 46  ve, MAX_PATH, zF
f4b30 75 6c 6c 70 61 74 68 29 3b 0a 20 20 69 66 28 20  ullpath);.  if( 
f4b40 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f 4b 20  rc == SQLITE_OK 
f4b50 29 0a 20 20 7b 0a 20 20 20 20 76 6f 69 64 20 2a  ).  {.    void *
f4b60 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
f4b70 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
f4b80 28 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 20  (zFullpath);.   
f4b90 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 20   if( zConverted 
f4ba0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 4e  ){.      if( isN
f4bb0 54 28 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  T() ){.        i
f4bc0 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 2f 2a  nt i;.        /*
f4bd0 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a 75   trim path to ju
f4be0 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 6e  st drive referen
f4bf0 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 57 43  ce */.        WC
f4c00 48 41 52 20 2a 70 20 3d 20 7a 43 6f 6e 76 65 72  HAR *p = zConver
f4c10 74 65 64 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  ted;.        for
f4c20 28 69 3d 30 3b 69 3c 4d 41 58 5f 50 41 54 48 3b  (i=0;i<MAX_PATH;
f4c30 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
f4c40 69 66 28 20 70 5b 69 5d 20 3d 3d 20 27 5c 5c 27  if( p[i] == '\\'
f4c50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
f4c60 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  i++;.           
f4c70 20 70 5b 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20   p[i] = '\0';.  
f4c80 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
f4c90 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
f4ca0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
f4cb0 77 52 65 74 20 3d 20 47 65 74 44 69 73 6b 46 72  wRet = GetDiskFr
f4cc0 65 65 53 70 61 63 65 57 28 28 57 43 48 41 52 2a  eeSpaceW((WCHAR*
f4cd0 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20  )zConverted,.   
f4ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e                 N
f4d00 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ULL,.           
f4d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4d20 20 20 20 20 20 20 20 26 62 79 74 65 73 50 65 72         &bytesPer
f4d30 53 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20  Sector,.        
f4d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4d50 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a            NULL,.
f4d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4d80 20 20 4e 55 4c 4c 29 3b 0a 23 69 66 20 53 51 4c    NULL);.#if SQL
f4d90 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
f4da0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
f4db0 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
f4dc0 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68      /* trim path
f4dd0 20 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72   to just drive r
f4de0 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20  eference */.    
f4df0 20 20 20 20 43 48 41 52 20 2a 70 20 3d 20 28 43      CHAR *p = (C
f4e00 48 41 52 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64  HAR *)zConverted
f4e10 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
f4e20 30 3b 69 3c 4d 41 58 5f 50 41 54 48 3b 69 2b 2b  0;i<MAX_PATH;i++
f4e30 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
f4e40 20 70 5b 69 5d 20 3d 3d 20 27 5c 5c 27 20 29 7b   p[i] == '\\' ){
f4e50 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
f4e60 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 5b  ;.            p[
f4e70 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  i] = '\0';.     
f4e80 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
f4e90 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
f4ea0 20 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65    }.        dwRe
f4eb0 74 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53  t = GetDiskFreeS
f4ec0 70 61 63 65 41 28 28 43 48 41 52 2a 29 7a 43 6f  paceA((CHAR*)zCo
f4ed0 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20  nverted,.       
f4ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4ef0 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c             NULL,
f4f00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f4f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4f20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 63 74     &bytesPerSect
f4f30 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  or,.            
f4f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4f50 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20        NULL,.    
f4f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55                NU
f4f80 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  LL);.#endif.    
f4f90 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65 28 7a    }.      free(z
f4fa0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
f4fb0 7d 0a 20 20 20 20 69 66 28 20 21 64 77 52 65 74  }.    if( !dwRet
f4fc0 20 29 7b 0a 20 20 20 20 20 20 62 79 74 65 73 50   ){.      bytesP
f4fd0 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54  erSector = SQLIT
f4fe0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
f4ff0 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _SIZE;.    }.  }
f5000 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 20  .  return (int) 
f5010 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 3b 20  bytesPerSector; 
f5020 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
f5030 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
f5040 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74  ENSION./*.** Int
f5050 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
f5060 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
f5070 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
f5080 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69  try points.** wi
f5090 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  thin the shared 
f50a0 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f  library, and clo
f50b0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  sing the shared 
f50c0 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a  library..*/./*.*
f50d0 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72  * Interfaces for
f50e0 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65   opening a share
f50f0 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69  d library, findi
f5100 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a  ng entry points.
f5110 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68  ** within the sh
f5120 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e  ared library, an
f5130 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68  d closing the sh
f5140 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  ared library..*/
f5150 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69  .static void *wi
f5160 6e 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  nDlOpen(sqlite3_
f5170 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
f5180 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f5190 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20  ){.  HANDLE h;. 
f51a0 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
f51b0 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46  d = convertUtf8F
f51c0 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d  ilename(zFilenam
f51d0 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e);.  UNUSED_PAR
f51e0 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
f51f0 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d  if( zConverted==
f5200 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
f5210 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4e  0;.  }.  if( isN
f5220 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 4c  T() ){.    h = L
f5230 6f 61 64 4c 69 62 72 61 72 79 57 28 28 57 43 48  oadLibraryW((WCH
f5240 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b  AR*)zConverted);
f5250 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
f5260 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f5270 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
f5280 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
f5290 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65  cuted. .** Since
f52a0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69   the ASCII versi
f52b0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
f52c0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
f52d0 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a  xist for WINCE,.
f52e0 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e  ** it's importan
f52f0 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e  t to not referen
f5300 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43  ce them for WINC
f5310 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  E builds..*/.#if
f5320 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f5330 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
f5340 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79   h = LoadLibrary
f5350 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72  A((char*)zConver
f5360 74 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  ted);.#endif.  }
f5370 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74  .  free(zConvert
f5380 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 76  ed);.  return (v
f5390 6f 69 64 2a 29 68 3b 0a 7d 0a 73 74 61 74 69 63  oid*)h;.}.static
f53a0 20 76 6f 69 64 20 77 69 6e 44 6c 45 72 72 6f 72   void winDlError
f53b0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
f53c0 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
f53d0 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20  ar *zBufOut){.  
f53e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f53f0 28 70 56 66 73 29 3b 0a 20 20 67 65 74 4c 61 73  (pVfs);.  getLas
f5400 74 45 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20  tErrorMsg(nBuf, 
f5410 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64  zBufOut);.}.void
f5420 20 28 2a 77 69 6e 44 6c 53 79 6d 28 73 71 6c 69   (*winDlSym(sqli
f5430 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76  te3_vfs *pVfs, v
f5440 6f 69 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f  oid *pHandle, co
f5450 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f  nst char *zSymbo
f5460 6c 29 29 28 76 6f 69 64 29 7b 0a 20 20 55 4e 55  l))(void){.  UNU
f5470 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
f5480 66 73 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  fs);.#if SQLITE_
f5490 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 54 68  OS_WINCE.  /* Th
f54a0 65 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73  e GetProcAddress
f54b0 41 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 6f  A() routine is o
f54c0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e  nly available on
f54d0 20 77 69 6e 63 65 2e 20 2a 2f 0a 20 20 72 65 74   wince. */.  ret
f54e0 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69  urn (void(*)(voi
f54f0 64 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73  d))GetProcAddres
f5500 73 41 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64  sA((HANDLE)pHand
f5510 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65  le, zSymbol);.#e
f5520 6c 73 65 0a 20 20 2f 2a 20 41 6c 6c 20 6f 74 68  lse.  /* All oth
f5530 65 72 20 77 69 6e 64 6f 77 73 20 70 6c 61 74 66  er windows platf
f5540 6f 72 6d 73 20 65 78 70 65 63 74 20 47 65 74 50  orms expect GetP
f5550 72 6f 63 41 64 64 72 65 73 73 28 29 20 74 6f 20  rocAddress() to 
f5560 74 61 6b 65 0a 20 20 2a 2a 20 61 6e 20 41 6e 73  take.  ** an Ans
f5570 69 20 73 74 72 69 6e 67 20 72 65 67 61 72 64 6c  i string regardl
f5580 65 73 73 20 6f 66 20 74 68 65 20 5f 55 4e 49 43  ess of the _UNIC
f5590 4f 44 45 20 73 65 74 74 69 6e 67 20 2a 2f 0a 20  ODE setting */. 
f55a0 20 72 65 74 75 72 6e 20 28 76 6f 69 64 28 2a 29   return (void(*)
f55b0 28 76 6f 69 64 29 29 47 65 74 50 72 6f 63 41 64  (void))GetProcAd
f55c0 64 72 65 73 73 28 28 48 41 4e 44 4c 45 29 70 48  dress((HANDLE)pH
f55d0 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b  andle, zSymbol);
f55e0 0a 23 65 6e 64 69 66 0a 7d 0a 76 6f 69 64 20 77  .#endif.}.void w
f55f0 69 6e 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  inDlClose(sqlite
f5600 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
f5610 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55  d *pHandle){.  U
f5620 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
f5630 70 56 66 73 29 3b 0a 20 20 46 72 65 65 4c 69 62  pVfs);.  FreeLib
f5640 72 61 72 79 28 28 48 41 4e 44 4c 45 29 70 48 61  rary((HANDLE)pHa
f5650 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f  ndle);.}.#else /
f5660 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  * if SQLITE_OMIT
f5670 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20  _LOAD_EXTENSION 
f5680 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20  is defined: */. 
f5690 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70   #define winDlOp
f56a0 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20  en  0.  #define 
f56b0 77 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 20 23  winDlError 0.  #
f56c0 64 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 6d 20  define winDlSym 
f56d0 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69    0.  #define wi
f56e0 6e 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69  nDlClose 0.#endi
f56f0 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  f.../*.** Write 
f5700 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73  up to nBuf bytes
f5710 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69   of randomness i
f5720 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61  nto zBuf..*/.sta
f5730 74 69 63 20 69 6e 74 20 77 69 6e 52 61 6e 64 6f  tic int winRando
f5740 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66  mness(sqlite3_vf
f5750 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
f5760 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
f5770 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 55    int n = 0;.  U
f5780 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
f5790 70 56 66 73 29 3b 0a 23 69 66 20 64 65 66 69 6e  pVfs);.#if defin
f57a0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
f57b0 20 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65    n = nBuf;.  me
f57c0 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42  mset(zBuf, 0, nB
f57d0 75 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28  uf);.#else.  if(
f57e0 20 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54 49   sizeof(SYSTEMTI
f57f0 4d 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20  ME)<=nBuf-n ){. 
f5800 20 20 20 53 59 53 54 45 4d 54 49 4d 45 20 78 3b     SYSTEMTIME x;
f5810 0a 20 20 20 20 47 65 74 53 79 73 74 65 6d 54 69  .    GetSystemTi
f5820 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63  me(&x);.    memc
f5830 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c  py(&zBuf[n], &x,
f5840 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20   sizeof(x));.   
f5850 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b   n += sizeof(x);
f5860 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f  .  }.  if( sizeo
f5870 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e  f(DWORD)<=nBuf-n
f5880 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 70 69   ){.    DWORD pi
f5890 64 20 3d 20 47 65 74 43 75 72 72 65 6e 74 50 72  d = GetCurrentPr
f58a0 6f 63 65 73 73 49 64 28 29 3b 0a 20 20 20 20 6d  ocessId();.    m
f58b0 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20  emcpy(&zBuf[n], 
f58c0 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64  &pid, sizeof(pid
f58d0 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ));.    n += siz
f58e0 65 6f 66 28 70 69 64 29 3b 0a 20 20 7d 0a 20 20  eof(pid);.  }.  
f58f0 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44  if( sizeof(DWORD
f5900 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20  )<=nBuf-n ){.   
f5910 20 44 57 4f 52 44 20 63 6e 74 20 3d 20 47 65 74   DWORD cnt = Get
f5920 54 69 63 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20  TickCount();.   
f5930 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
f5940 2c 20 26 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63  , &cnt, sizeof(c
f5950 6e 74 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73  nt));.    n += s
f5960 69 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a  izeof(cnt);.  }.
f5970 20 20 69 66 28 20 73 69 7a 65 6f 66 28 4c 41 52    if( sizeof(LAR
f5980 47 45 5f 49 4e 54 45 47 45 52 29 3c 3d 6e 42 75  GE_INTEGER)<=nBu
f5990 66 2d 6e 20 29 7b 0a 20 20 20 20 4c 41 52 47 45  f-n ){.    LARGE
f59a0 5f 49 4e 54 45 47 45 52 20 69 3b 0a 20 20 20 20  _INTEGER i;.    
f59b0 51 75 65 72 79 50 65 72 66 6f 72 6d 61 6e 63 65  QueryPerformance
f59c0 43 6f 75 6e 74 65 72 28 26 69 29 3b 0a 20 20 20  Counter(&i);.   
f59d0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
f59e0 2c 20 26 69 2c 20 73 69 7a 65 6f 66 28 69 29 29  , &i, sizeof(i))
f59f0 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ;.    n += sizeo
f5a00 66 28 69 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  f(i);.  }.#endif
f5a10 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
f5a20 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72  ./*.** Sleep for
f5a30 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e   a little while.
f5a40 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f    Return the amo
f5a50 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70  unt of time slep
f5a60 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
f5a70 20 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 74 65   winSleep(sqlite
f5a80 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
f5a90 20 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 53 6c   microsec){.  Sl
f5aa0 65 65 70 28 28 6d 69 63 72 6f 73 65 63 2b 39 39  eep((microsec+99
f5ab0 39 29 2f 31 30 30 30 29 3b 0a 20 20 55 4e 55 53  9)/1000);.  UNUS
f5ac0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
f5ad0 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6d  s);.  return ((m
f5ae0 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30  icrosec+999)/100
f5af0 30 29 2a 31 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  0)*1000;.}../*.*
f5b00 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
f5b10 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74  variable, if set
f5b20 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
f5b30 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 68  alue, becomes th
f5b40 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75  e result.** retu
f5b50 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  rned from sqlite
f5b60 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29  3OsCurrentTime()
f5b70 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
f5b80 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a  for testing..*/.
f5b90 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
f5ba0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
f5bb0 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  t sqlite3_curren
f5bc0 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64  t_time = 0;.#end
f5bd0 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  if../*.** Find t
f5be0 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  he current time 
f5bf0 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f  (in Universal Co
f5c00 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e  ordinated Time).
f5c10 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63    Write the.** c
f5c20 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20  urrent time and 
f5c30 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e  date as a Julian
f5c40 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f   Day number into
f5c50 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72   *prNow and.** r
f5c60 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e  eturn 0.  Return
f5c70 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61   1 if the time a
f5c80 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62  nd date cannot b
f5c90 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20  e found..*/.int 
f5ca0 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 28 73  winCurrentTime(s
f5cb0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
f5cc0 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29  , double *prNow)
f5cd0 7b 0a 20 20 46 49 4c 45 54 49 4d 45 20 66 74 3b  {.  FILETIME ft;
f5ce0 0a 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45 20 73  .  /* FILETIME s
f5cf0 74 72 75 63 74 75 72 65 20 69 73 20 61 20 36 34  tructure is a 64
f5d00 2d 62 69 74 20 76 61 6c 75 65 20 72 65 70 72 65  -bit value repre
f5d10 73 65 6e 74 69 6e 67 20 74 68 65 20 6e 75 6d 62  senting the numb
f5d20 65 72 20 6f 66 20 0a 20 20 20 20 20 31 30 30 2d  er of .     100-
f5d30 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72  nanosecond inter
f5d40 76 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e 75 61  vals since Janua
f5d50 72 79 20 31 2c 20 31 36 30 31 20 28 3d 20 4a 44  ry 1, 1601 (= JD
f5d60 20 32 33 30 35 38 31 33 2e 35 29 2e 20 0a 20 20   2305813.5). .  
f5d70 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
f5d80 36 34 20 74 69 6d 65 57 3b 20 20 20 2f 2a 20 57  64 timeW;   /* W
f5d90 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20 73  hole days */.  s
f5da0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 69 6d  qlite3_int64 tim
f5db0 65 46 3b 20 20 20 2f 2a 20 46 72 61 63 74 69 6f  eF;   /* Fractio
f5dc0 6e 61 6c 20 44 61 79 73 20 2a 2f 0a 0a 20 20 2f  nal Days */..  /
f5dd0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 30 2d  * Number of 100-
f5de0 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72  nanosecond inter
f5df0 76 61 6c 73 20 69 6e 20 61 20 73 69 6e 67 6c 65  vals in a single
f5e00 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 74 69 63   day */.  static
f5e10 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
f5e20 6e 74 36 34 20 6e 74 75 50 65 72 44 61 79 20 3d  nt64 ntuPerDay =
f5e30 20 0a 20 20 20 20 20 20 31 30 30 30 30 30 30 30   .      10000000
f5e40 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  *(sqlite3_int64)
f5e50 38 36 34 30 30 3b 0a 0a 20 20 2f 2a 20 4e 75 6d  86400;..  /* Num
f5e60 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f 73  ber of 100-nanos
f5e70 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20  econd intervals 
f5e80 69 6e 20 68 61 6c 66 20 6f 66 20 61 20 64 61 79  in half of a day
f5e90 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
f5ea0 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  st sqlite3_int64
f5eb0 20 6e 74 75 50 65 72 48 61 6c 66 44 61 79 20 3d   ntuPerHalfDay =
f5ec0 20 0a 20 20 20 20 20 20 31 30 30 30 30 30 30 30   .      10000000
f5ed0 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  *(sqlite3_int64)
f5ee0 34 33 32 30 30 3b 0a 0a 20 20 2f 2a 20 32 5e 33  43200;..  /* 2^3
f5ef0 32 20 2d 20 74 6f 20 61 76 6f 69 64 20 75 73 65  2 - to avoid use
f5f00 20 6f 66 20 4c 4c 20 61 6e 64 20 77 61 72 6e 69   of LL and warni
f5f10 6e 67 73 20 69 6e 20 67 63 63 20 2a 2f 0a 20 20  ngs in gcc */.  
f5f20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
f5f30 69 74 65 33 5f 69 6e 74 36 34 20 6d 61 78 33 32  ite3_int64 max32
f5f40 42 69 74 56 61 6c 75 65 20 3d 20 0a 20 20 20 20  BitValue = .    
f5f50 20 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34    (sqlite3_int64
f5f60 29 32 30 30 30 30 30 30 30 30 30 20 2b 20 28 73  )2000000000 + (s
f5f70 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30  qlite3_int64)200
f5f80 30 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74  0000000 + (sqlit
f5f90 65 33 5f 69 6e 74 36 34 29 32 39 34 39 36 37 32  e3_int64)2949672
f5fa0 39 36 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  96;..#if SQLITE_
f5fb0 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 53 54 45  OS_WINCE.  SYSTE
f5fc0 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20 47 65  MTIME time;.  Ge
f5fd0 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74 69 6d  tSystemTime(&tim
f5fe0 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 79 73 74  e);.  /* if Syst
f5ff0 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65  emTimeToFileTime
f6000 28 29 20 66 61 69 6c 73 2c 20 69 74 20 72 65 74  () fails, it ret
f6010 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20  urns zero. */.  
f6020 69 66 20 28 21 53 79 73 74 65 6d 54 69 6d 65 54  if (!SystemTimeT
f6030 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d 65 2c  oFileTime(&time,
f6040 26 66 74 29 29 7b 0a 20 20 20 20 72 65 74 75 72  &ft)){.    retur
f6050 6e 20 31 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  n 1;.  }.#else. 
f6060 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 41 73   GetSystemTimeAs
f6070 46 69 6c 65 54 69 6d 65 28 20 26 66 74 20 29 3b  FileTime( &ft );
f6080 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44  .#endif.  UNUSED
f6090 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
f60a0 3b 0a 20 20 74 69 6d 65 57 20 3d 20 28 28 28 73  ;.  timeW = (((s
f60b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e  qlite3_int64)ft.
f60c0 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 29 2a  dwHighDateTime)*
f60d0 6d 61 78 33 32 42 69 74 56 61 6c 75 65 29 20 2b  max32BitValue) +
f60e0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
f60f0 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65  ft.dwLowDateTime
f6100 3b 0a 20 20 74 69 6d 65 46 20 3d 20 74 69 6d 65  ;.  timeF = time
f6110 57 20 25 20 6e 74 75 50 65 72 44 61 79 3b 20 20  W % ntuPerDay;  
f6120 20 20 20 20 20 20 20 20 2f 2a 20 66 72 61 63 74          /* fract
f6130 69 6f 6e 61 6c 20 64 61 79 73 20 28 31 30 30 2d  ional days (100-
f6140 6e 61 6e 6f 73 65 63 6f 6e 64 73 29 20 2a 2f 0a  nanoseconds) */.
f6150 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20    timeW = timeW 
f6160 2f 20 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20  / ntuPerDay;    
f6170 20 20 20 20 20 20 2f 2a 20 77 68 6f 6c 65 20 64        /* whole d
f6180 61 79 73 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d  ays */.  timeW =
f6190 20 74 69 6d 65 57 20 2b 20 32 33 30 35 38 31 33   timeW + 2305813
f61a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
f61b0 61 64 64 20 77 68 6f 6c 65 20 64 61 79 73 20 28  add whole days (
f61c0 66 72 6f 6d 20 32 33 30 35 38 31 33 2e 35 29 20  from 2305813.5) 
f61d0 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 69 6d  */.  timeF = tim
f61e0 65 46 20 2b 20 6e 74 75 50 65 72 48 61 6c 66 44  eF + ntuPerHalfD
f61f0 61 79 3b 20 20 20 20 20 20 2f 2a 20 61 64 64 20  ay;      /* add 
f6200 68 61 6c 66 20 61 20 64 61 79 20 28 66 72 6f 6d  half a day (from
f6210 20 32 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20   2305813.5) */. 
f6220 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b   timeW = timeW +
f6230 20 28 74 69 6d 65 46 2f 6e 74 75 50 65 72 44 61   (timeF/ntuPerDa
f6240 79 29 3b 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c  y);  /* add whol
f6250 65 20 64 61 79 20 69 66 20 68 61 6c 66 20 64 61  e day if half da
f6260 79 20 6d 61 64 65 20 6f 6e 65 20 2a 2f 0a 20 20  y made one */.  
f6270 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 25 20  timeF = timeF % 
f6280 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20 20 20  ntuPerDay;      
f6290 20 20 20 20 2f 2a 20 63 6f 6d 70 75 74 65 20 6e      /* compute n
f62a0 65 77 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61  ew fractional da
f62b0 79 73 20 2a 2f 0a 20 20 2a 70 72 4e 6f 77 20 3d  ys */.  *prNow =
f62c0 20 28 64 6f 75 62 6c 65 29 74 69 6d 65 57 20 2b   (double)timeW +
f62d0 20 28 28 64 6f 75 62 6c 65 29 74 69 6d 65 46 20   ((double)timeF 
f62e0 2f 20 28 64 6f 75 62 6c 65 29 6e 74 75 50 65 72  / (double)ntuPer
f62f0 44 61 79 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  Day);.#ifdef SQL
f6300 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73  ITE_TEST.  if( s
f6310 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
f6320 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f  ime ){.    *prNo
f6330 77 20 3d 20 28 28 64 6f 75 62 6c 65 29 73 71 6c  w = ((double)sql
f6340 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d  ite3_current_tim
f6350 65 20 2b 20 28 64 6f 75 62 6c 65 29 34 33 32 30  e + (double)4320
f6360 30 29 20 2f 20 28 64 6f 75 62 6c 65 29 38 36 34  0) / (double)864
f6370 30 30 20 2b 20 28 64 6f 75 62 6c 65 29 32 34 34  00 + (double)244
f6380 30 35 38 37 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  0587;.  }.#endif
f6390 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
f63a0 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 69  /*.** The idea i
f63b0 73 20 74 68 61 74 20 74 68 69 73 20 66 75 6e 63  s that this func
f63c0 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20  tion works like 
f63d0 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  a combination of
f63e0 0a 2a 2a 20 47 65 74 4c 61 73 74 45 72 72 6f 72  .** GetLastError
f63f0 28 29 20 61 6e 64 20 46 6f 72 6d 61 74 4d 65 73  () and FormatMes
f6400 73 61 67 65 28 29 20 6f 6e 20 77 69 6e 64 6f 77  sage() on window
f6410 73 20 28 6f 72 20 65 72 72 6e 6f 20 61 6e 64 0a  s (or errno and.
f6420 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 29 20  ** strerror_r() 
f6430 6f 6e 20 75 6e 69 78 29 2e 20 41 66 74 65 72 20  on unix). After 
f6440 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  an error is retu
f6450 72 6e 65 64 20 62 79 20 61 6e 20 4f 53 0a 2a 2a  rned by an OS.**
f6460 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c 69 74   function, SQLit
f6470 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e  e calls this fun
f6480 63 74 69 6f 6e 20 77 69 74 68 20 7a 42 75 66 20  ction with zBuf 
f6490 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 61  pointing to.** a
f64a0 20 62 75 66 66 65 72 20 6f 66 20 6e 42 75 66 20   buffer of nBuf 
f64b0 62 79 74 65 73 2e 20 54 68 65 20 4f 53 20 6c 61  bytes. The OS la
f64c0 79 65 72 20 73 68 6f 75 6c 64 20 70 6f 70 75 6c  yer should popul
f64d0 61 74 65 20 74 68 65 0a 2a 2a 20 62 75 66 66 65  ate the.** buffe
f64e0 72 20 77 69 74 68 20 61 20 6e 75 6c 2d 74 65 72  r with a nul-ter
f64f0 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 65 6e  minated UTF-8 en
f6500 63 6f 64 65 64 20 65 72 72 6f 72 20 6d 65 73 73  coded error mess
f6510 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e  age.** describin
f6520 67 20 74 68 65 20 6c 61 73 74 20 49 4f 20 65 72  g the last IO er
f6530 72 6f 72 20 74 6f 20 68 61 76 65 20 6f 63 63 75  ror to have occu
f6540 72 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  rred within the 
f6550 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 72 65 61  calling.** threa
f6560 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
f6570 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73  error message is
f6580 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 74   too large for t
f6590 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66 66  he supplied buff
f65a0 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f 75 6c 64  er,.** it should
f65b0 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e 20 54   be truncated. T
f65c0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
f65d0 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  of xGetLastError
f65e0 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74  .** is zero if t
f65f0 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
f6600 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66   fits in the buf
f6610 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f  fer, or non-zero
f6620 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69  .** otherwise (i
f6630 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61  f the message wa
f6640 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20 49 66  s truncated). If
f6650 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74   non-zero is ret
f6660 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 69  urned,.** then i
f6670 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  t is not necessa
f6680 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68  ry to include th
f6690 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  e nul-terminator
f66a0 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69 6e   character.** in
f66b0 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
f66c0 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 75  er..**.** Not su
f66d0 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 72 6f 72  pplying an error
f66e0 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 68 61   message will ha
f66f0 76 65 20 6e 6f 20 61 64 76 65 72 73 65 20 65 66  ve no adverse ef
f6700 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 4c 69 74  fect.** on SQLit
f6710 65 2e 20 49 74 20 69 73 20 66 69 6e 65 20 74 6f  e. It is fine to
f6720 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 65 6d 65   have an impleme
f6730 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6e 65 76  ntation that nev
f6740 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 6e  er.** returns an
f6750 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 3a 0a   error message:.
f6760 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 47 65 74  **.**   int xGet
f6770 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65  LastError(sqlite
f6780 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
f6790 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
f67a0 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 73 65 72  f){.**     asser
f67b0 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c 30 27 29  t(zBuf[0]=='\0')
f67c0 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 72 6e 20  ;.**     return 
f67d0 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20  0;.**   }.**.** 
f67e0 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20 65 72  However if an er
f67f0 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 73  ror message is s
f6800 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69 6c 6c  upplied, it will
f6810 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 65 64   be incorporated
f6820 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20 69 6e  .** by sqlite in
f6830 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  to the error mes
f6840 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65 20 74  sage available t
f6850 6f 20 74 68 65 20 75 73 65 72 20 75 73 69 6e 67  o the user using
f6860 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  .** sqlite3_errm
f6870 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79 20 6d  sg(), possibly m
f6880 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 20  aking IO errors 
f6890 65 61 73 69 65 72 20 74 6f 20 64 65 62 75 67 2e  easier to debug.
f68a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
f68b0 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73  inGetLastError(s
f68c0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
f68d0 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
f68e0 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45   *zBuf){.  UNUSE
f68f0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
f6900 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 65 74 4c  );.  return getL
f6910 61 73 74 45 72 72 6f 72 4d 73 67 28 6e 42 75 66  astErrorMsg(nBuf
f6920 2c 20 7a 42 75 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , zBuf);.}../*.*
f6930 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  * Initialize and
f6940 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   deinitialize th
f6950 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
f6960 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  em interface..*/
f6970 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f6980 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
f6990 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
f69a0 73 71 6c 69 74 65 33 5f 76 66 73 20 77 69 6e 56  sqlite3_vfs winV
f69b0 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20  fs = {.    1,   
f69c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f69d0 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20   iVersion */.   
f69e0 20 73 69 7a 65 6f 66 28 77 69 6e 46 69 6c 65 29   sizeof(winFile)
f69f0 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20  ,   /* szOsFile 
f6a00 2a 2f 0a 20 20 20 20 4d 41 58 5f 50 41 54 48 2c  */.    MAX_PATH,
f6a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50            /* mxP
f6a20 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 30  athname */.    0
f6a30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
f6a40 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20    /* pNext */.  
f6a50 20 20 22 77 69 6e 33 32 22 2c 20 20 20 20 20 20    "win32",      
f6a60 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f       /* zName */
f6a70 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
f6a80 20 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70 44          /* pAppD
f6a90 61 74 61 20 2a 2f 0a 20 0a 20 20 20 20 77 69 6e  ata */. .    win
f6aa0 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
f6ab0 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20 20 20  /* xOpen */.    
f6ac0 77 69 6e 44 65 6c 65 74 65 2c 20 20 20 20 20 20  winDelete,      
f6ad0 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f     /* xDelete */
f6ae0 0a 20 20 20 20 77 69 6e 41 63 63 65 73 73 2c 20  .    winAccess, 
f6af0 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 63 65          /* xAcce
f6b00 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 46 75 6c  ss */.    winFul
f6b10 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f 2a 20  lPathname,   /* 
f6b20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f  xFullPathname */
f6b30 0a 20 20 20 20 77 69 6e 44 6c 4f 70 65 6e 2c 20  .    winDlOpen, 
f6b40 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f 70          /* xDlOp
f6b50 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 45  en */.    winDlE
f6b60 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  rror,        /* 
f6b70 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 20 20  xDlError */.    
f6b80 77 69 6e 44 6c 53 79 6d 2c 20 20 20 20 20 20 20  winDlSym,       
f6b90 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a     /* xDlSym */.
f6ba0 20 20 20 20 77 69 6e 44 6c 43 6c 6f 73 65 2c 20      winDlClose, 
f6bb0 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f         /* xDlClo
f6bc0 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e 52 61 6e  se */.    winRan
f6bd0 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a 20  domness,     /* 
f6be0 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20  xRandomness */. 
f6bf0 20 20 20 77 69 6e 53 6c 65 65 70 2c 20 20 20 20     winSleep,    
f6c00 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20        /* xSleep 
f6c10 2a 2f 0a 20 20 20 20 77 69 6e 43 75 72 72 65 6e  */.    winCurren
f6c20 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78 43 75  tTime,    /* xCu
f6c30 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20 20  rrentTime */.   
f6c40 20 77 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72   winGetLastError
f6c50 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45      /* xGetLastE
f6c60 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73  rror */.  };.  s
f6c70 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
f6c80 74 65 72 28 26 77 69 6e 56 66 73 2c 20 31 29 3b  ter(&winVfs, 1);
f6c90 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f6ca0 5f 4f 4b 3b 20 0a 7d 0a 53 51 4c 49 54 45 5f 41  _OK; .}.SQLITE_A
f6cb0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
f6cc0 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20  s_end(void){ .  
f6cd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f6ce0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
f6cf0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a  QLITE_OS_WIN */.
f6d00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
f6d10 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63   End of os_win.c
f6d20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
f6d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
f6d50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
f6d60 20 42 65 67 69 6e 20 66 69 6c 65 20 62 69 74 76   Begin file bitv
f6d70 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ec.c ***********
f6d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
f6da0 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46 65 62 72  ./*.** 2008 Febr
f6db0 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68  uary 16.**.** Th
f6dc0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
f6dd0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
f6de0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
f6df0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
f6e00 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
f6e10 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
f6e20 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
f6e30 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
f6e40 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
f6e50 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
f6e60 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
f6e70 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
f6e80 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
f6e90 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
f6ea0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
f6eb0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
f6ec0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
f6ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6f10 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
f6f20 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
f6f30 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 72 65  n object that re
f6f40 70 72 65 73 65 6e 74 73 20 61 20 66 69 78 65 64  presents a fixed
f6f50 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69 74 6d 61  -length.** bitma
f6f60 70 2e 20 20 42 69 74 73 20 61 72 65 20 6e 75 6d  p.  Bits are num
f6f70 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 77  bered starting w
f6f80 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62  ith 1..**.** A b
f6f90 69 74 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f  itmap is used to
f6fa0 20 72 65 63 6f 72 64 20 77 68 69 63 68 20 70 61   record which pa
f6fb0 67 65 73 20 6f 66 20 61 20 64 61 74 61 62 61 73  ges of a databas
f6fc0 65 20 66 69 6c 65 20 68 61 76 65 20 62 65 65 6e  e file have been
f6fd0 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64  .** journalled d
f6fe0 75 72 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74  uring a transact
f6ff0 69 6f 6e 2c 20 6f 72 20 77 68 69 63 68 20 70 61  ion, or which pa
f7000 67 65 73 20 68 61 76 65 20 74 68 65 20 22 64 6f  ges have the "do
f7010 6e 74 2d 77 72 69 74 65 22 0a 2a 2a 20 70 72 6f  nt-write".** pro
f7020 70 65 72 74 79 2e 20 20 55 73 75 61 6c 6c 79 20  perty.  Usually 
f7030 6f 6e 6c 79 20 61 20 66 65 77 20 70 61 67 65 73  only a few pages
f7040 20 61 72 65 20 6d 65 65 74 20 65 69 74 68 65 72   are meet either
f7050 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53   condition..** S
f7060 6f 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20  o the bitmap is 
f7070 75 73 75 61 6c 6c 79 20 73 70 61 72 73 65 20 61  usually sparse a
f7080 6e 64 20 68 61 73 20 6c 6f 77 20 63 61 72 64 69  nd has low cardi
f7090 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 73  nality..** But s
f70a0 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72 20 65 78  ometimes (for ex
f70b0 61 6d 70 6c 65 20 77 68 65 6e 20 64 75 72 69 6e  ample when durin
f70c0 67 20 61 20 44 52 4f 50 20 6f 66 20 61 20 6c 61  g a DROP of a la
f70d0 72 67 65 20 74 61 62 6c 65 29 20 6d 6f 73 74 0a  rge table) most.
f70e0 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  ** or all of the
f70f0 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61   pages in a data
f7100 62 61 73 65 20 63 61 6e 20 67 65 74 20 6a 6f 75  base can get jou
f7110 72 6e 61 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f  rnalled.  In tho
f7120 73 65 20 63 61 73 65 73 2c 20 0a 2a 2a 20 74 68  se cases, .** th
f7130 65 20 62 69 74 6d 61 70 20 62 65 63 6f 6d 65 73  e bitmap becomes
f7140 20 64 65 6e 73 65 20 77 69 74 68 20 68 69 67 68   dense with high
f7150 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 20 20 54   cardinality.  T
f7160 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65  he algorithm nee
f7170 64 73 20 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65  ds .** to handle
f7180 20 62 6f 74 68 20 63 61 73 65 73 20 77 65 6c 6c   both cases well
f7190 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65  ..**.** The size
f71a0 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 20 69   of the bitmap i
f71b0 73 20 66 69 78 65 64 20 77 68 65 6e 20 74 68 65  s fixed when the
f71c0 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74   object is creat
f71d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69  ed..**.** All bi
f71e0 74 73 20 61 72 65 20 63 6c 65 61 72 20 77 68 65  ts are clear whe
f71f0 6e 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20  n the bitmap is 
f7200 63 72 65 61 74 65 64 2e 20 20 49 6e 64 69 76 69  created.  Indivi
f7210 64 75 61 6c 20 62 69 74 73 0a 2a 2a 20 6d 61 79  dual bits.** may
f7220 20 62 65 20 73 65 74 20 6f 72 20 63 6c 65 61 72   be set or clear
f7230 65 64 20 6f 6e 65 20 61 74 20 61 20 74 69 6d 65  ed one at a time
f7240 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20 6f 70 65  ..**.** Test ope
f7250 72 61 74 69 6f 6e 73 20 61 72 65 20 61 62 6f 75  rations are abou
f7260 74 20 31 30 30 20 74 69 6d 65 73 20 6d 6f 72 65  t 100 times more
f7270 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 73 65 74   common that set
f7280 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20   operations..** 
f7290 43 6c 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73  Clear operations
f72a0 20 61 72 65 20 65 78 63 65 65 64 69 6e 67 6c 79   are exceedingly
f72b0 20 72 61 72 65 2e 20 20 54 68 65 72 65 20 61 72   rare.  There ar
f72c0 65 20 75 73 75 61 6c 6c 79 20 62 65 74 77 65 65  e usually betwee
f72d0 6e 0a 2a 2a 20 35 20 61 6e 64 20 35 30 30 20 73  n.** 5 and 500 s
f72e0 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 70 65  et operations pe
f72f0 72 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2c  r Bitvec object,
f7300 20 74 68 6f 75 67 68 20 74 68 65 20 6e 75 6d 62   though the numb
f7310 65 72 20 6f 66 20 73 65 74 73 20 63 61 6e 0a 2a  er of sets can.*
f7320 2a 20 73 6f 6d 65 74 69 6d 65 73 20 67 72 6f 77  * sometimes grow
f7330 20 69 6e 74 6f 20 74 65 6e 73 20 6f 66 20 74 68   into tens of th
f7340 6f 75 73 61 6e 64 73 20 6f 72 20 6c 61 72 67 65  ousands or large
f7350 72 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20  r.  The size of 
f7360 74 68 65 0a 2a 2a 20 42 69 74 76 65 63 20 6f 62  the.** Bitvec ob
f7370 6a 65 63 74 20 69 73 20 74 68 65 20 6e 75 6d 62  ject is the numb
f7380 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
f7390 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
f73a0 20 61 74 20 74 68 65 0a 2a 2a 20 73 74 61 72 74   at the.** start
f73b0 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   of a transactio
f73c0 6e 2c 20 61 6e 64 20 69 73 20 74 68 75 73 20 75  n, and is thus u
f73d0 73 75 61 6c 6c 79 20 6c 65 73 73 20 74 68 61 6e  sually less than
f73e0 20 61 20 66 65 77 20 74 68 6f 75 73 61 6e 64 2c   a few thousand,
f73f0 0a 2a 2a 20 62 75 74 20 63 61 6e 20 62 65 20 61  .** but can be a
f7400 73 20 6c 61 72 67 65 20 61 73 20 32 20 62 69 6c  s large as 2 bil
f7410 6c 69 6f 6e 20 66 6f 72 20 61 20 72 65 61 6c 6c  lion for a reall
f7420 79 20 62 69 67 20 64 61 74 61 62 61 73 65 2e 0a  y big database..
f7430 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
f7440 62 69 74 76 65 63 2e 63 2c 76 20 31 2e 31 33 20  bitvec.c,v 1.13 
f7450 32 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36  2009/01/20 17:06
f7460 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :27 danielk1977 
f7470 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a  Exp $.*/../* Siz
f7480 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20  e of the Bitvec 
f7490 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 74  structure in byt
f74a0 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  es. */.#define B
f74b0 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20  ITVEC_SZ        
f74c0 35 31 32 0a 0a 2f 2a 20 52 6f 75 6e 64 20 74 68  512../* Round th
f74d0 65 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77  e union size dow
f74e0 6e 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74  n to the nearest
f74f0 20 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72   pointer boundar
f7500 79 2c 20 73 69 6e 63 65 20 74 68 61 74 27 73 20  y, since that's 
f7510 68 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20  how .** it will 
f7520 62 65 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69  be aligned withi
f7530 6e 20 74 68 65 20 42 69 74 76 65 63 20 73 74 72  n the Bitvec str
f7540 75 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  uct. */.#define 
f7550 42 49 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20  BITVEC_USIZE    
f7560 20 28 28 28 42 49 54 56 45 43 5f 53 5a 2d 28 33   (((BITVEC_SZ-(3
f7570 2a 73 69 7a 65 6f 66 28 75 33 32 29 29 29 2f 73  *sizeof(u32)))/s
f7580 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a  izeof(Bitvec*))*
f7590 73 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29  sizeof(Bitvec*))
f75a0 0a 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65  ../* Type of the
f75b0 20 61 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22   array "element"
f75c0 20 66 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20   for the bitmap 
f75d0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20  representation. 
f75e0 0a 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20  .** Should be a 
f75f0 70 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20  power of 2, and 
f7600 69 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20  ideally, evenly 
f7610 64 69 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56  divide into BITV
f7620 45 43 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 53 65  EC_USIZE. .** Se
f7630 74 74 69 6e 67 20 74 68 69 73 20 74 6f 20 74 68  tting this to th
f7640 65 20 22 6e 61 74 75 72 61 6c 20 77 6f 72 64 22  e "natural word"
f7650 20 73 69 7a 65 20 6f 66 20 79 6f 75 72 20 43 50   size of your CP
f7660 55 20 6d 61 79 20 69 6d 70 72 6f 76 65 0a 2a 2a  U may improve.**
f7670 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 2a 2f   performance. */
f7680 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
f7690 54 45 4c 45 4d 20 20 20 20 20 75 38 0a 2f 2a 20  TELEM     u8./* 
f76a0 53 69 7a 65 2c 20 69 6e 20 62 69 74 73 2c 20 6f  Size, in bits, o
f76b0 66 20 74 68 65 20 62 69 74 6d 61 70 20 65 6c 65  f the bitmap ele
f76c0 6d 65 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  ment. */.#define
f76d0 20 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 20 20   BITVEC_SZELEM  
f76e0 20 20 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66    8./* Number of
f76f0 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 20 62   elements in a b
f7700 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a  itmap array. */.
f7710 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e  #define BITVEC_N
f7720 45 4c 45 4d 20 20 20 20 20 28 42 49 54 56 45 43  ELEM     (BITVEC
f7730 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 49  _USIZE/sizeof(BI
f7740 54 56 45 43 5f 54 45 4c 45 4d 29 29 0a 2f 2a 20  TVEC_TELEM))./* 
f7750 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69  Number of bits i
f7760 6e 20 74 68 65 20 62 69 74 6d 61 70 20 61 72 72  n the bitmap arr
f7770 61 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  ay. */.#define B
f7780 49 54 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20  ITVEC_NBIT      
f7790 28 42 49 54 56 45 43 5f 4e 45 4c 45 4d 2a 42 49  (BITVEC_NELEM*BI
f77a0 54 56 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a 2f 2a  TVEC_SZELEM)../*
f77b0 20 4e 75 6d 62 65 72 20 6f 66 20 75 33 32 20 76   Number of u32 v
f77c0 61 6c 75 65 73 20 69 6e 20 68 61 73 68 20 74 61  alues in hash ta
f77d0 62 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ble. */.#define 
f77e0 42 49 54 56 45 43 5f 4e 49 4e 54 20 20 20 20 20  BITVEC_NINT     
f77f0 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73   (BITVEC_USIZE/s
f7800 69 7a 65 6f 66 28 75 33 32 29 29 0a 2f 2a 20 4d  izeof(u32))./* M
f7810 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
f7820 20 65 6e 74 72 69 65 73 20 69 6e 20 68 61 73 68   entries in hash
f7830 20 74 61 62 6c 65 20 62 65 66 6f 72 65 20 0a 2a   table before .*
f7840 2a 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61  * sub-dividing a
f7850 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a  nd re-hashing. *
f7860 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
f7870 5f 4d 58 48 41 53 48 20 20 20 20 28 42 49 54 56  _MXHASH    (BITV
f7880 45 43 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 48 61  EC_NINT/2)./* Ha
f7890 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66  shing function f
f78a0 6f 72 20 74 68 65 20 61 48 61 73 68 20 72 65 70  or the aHash rep
f78b0 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20  resentation..** 
f78c0 45 6d 70 69 72 69 63 61 6c 20 74 65 73 74 69 6e  Empirical testin
f78d0 67 20 73 68 6f 77 65 64 20 74 68 61 74 20 74 68  g showed that th
f78e0 65 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65 72  e *37 multiplier
f78f0 20 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74 72 61   .** (an arbitra
f7900 72 79 20 70 72 69 6d 65 29 69 6e 20 74 68 65 20  ry prime)in the 
f7910 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 70 72  hash function pr
f7920 6f 76 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 66 65  ovided .** no fe
f7930 77 65 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 74  wer collisions t
f7940 68 61 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 2a 31  han the no-op *1
f7950 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54  . */.#define BIT
f7960 56 45 43 5f 48 41 53 48 28 58 29 20 20 20 28 28  VEC_HASH(X)   ((
f7970 28 58 29 2a 31 29 25 42 49 54 56 45 43 5f 4e 49  (X)*1)%BITVEC_NI
f7980 4e 54 29 0a 0a 23 64 65 66 69 6e 65 20 42 49 54  NT)..#define BIT
f7990 56 45 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42  VEC_NPTR      (B
f79a0 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65  ITVEC_USIZE/size
f79b0 6f 66 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 0a  of(Bitvec *))...
f79c0 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69  /*.** A bitmap i
f79d0 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
f79e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
f79f0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
f7a00 54 68 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f  This bitmap reco
f7a10 72 64 73 20 74 68 65 20 65 78 69 73 74 61 6e 63  rds the existanc
f7a20 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72  e of zero or mor
f7a30 65 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76  e bits.** with v
f7a40 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20  alues between 1 
f7a50 61 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75  and iSize, inclu
f7a60 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  sive..**.** Ther
f7a70 65 20 61 72 65 20 74 68 72 65 65 20 70 6f 73 73  e are three poss
f7a80 69 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74  ible representat
f7a90 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d  ions of the bitm
f7aa0 61 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c  ap..** If iSize<
f7ab0 3d 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68  =BITVEC_NBIT, th
f7ac0 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74  en Bitvec.u.aBit
f7ad0 6d 61 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69  map[] is a strai
f7ae0 67 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20  ght.** bitmap.  
f7af0 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66  The least signif
f7b00 69 63 61 6e 74 20 62 69 74 20 69 73 20 62 69 74  icant bit is bit
f7b10 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69   1..**.** If iSi
f7b20 7a 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61  ze>BITVEC_NBIT a
f7b30 6e 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74  nd iDivisor==0 t
f7b40 68 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61  hen Bitvec.u.aHa
f7b50 73 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73  sh[] is.** a has
f7b60 68 20 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c  h table that wil
f7b70 6c 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54  l hold up to BIT
f7b80 56 45 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69  VEC_MXHASH disti
f7b90 6e 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a  nct values..**.*
f7ba0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  * Otherwise, the
f7bb0 20 76 61 6c 75 65 20 69 20 69 73 20 72 65 64 69   value i is redi
f7bc0 72 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20  rected into one 
f7bd0 6f 66 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a  of BITVEC_NPTR.*
f7be0 2a 20 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f  * sub-bitmaps po
f7bf0 69 6e 74 65 64 20 74 6f 20 62 79 20 42 69 74 76  inted to by Bitv
f7c00 65 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45  ec.u.apSub[].  E
f7c10 61 63 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a  ach subbitmap.**
f7c20 20 68 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69   handles up to i
f7c30 44 69 76 69 73 6f 72 20 73 65 70 61 72 61 74 65  Divisor separate
f7c40 20 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61   values of i.  a
f7c50 70 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a  pSub[0] holds.**
f7c60 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20   values between 
f7c70 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20  1 and iDivisor. 
f7c80 20 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20   apSub[1] holds 
f7c90 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a  values between.*
f7ca0 2a 20 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64  * iDivisor+1 and
f7cb0 20 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70   2*iDivisor.  ap
f7cc0 53 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c  Sub[N] holds val
f7cd0 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e  ues between.** N
f7ce0 2a 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20  *iDivisor+1 and 
f7cf0 28 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20  (N+1)*iDivisor. 
f7d00 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 20   Each subbitmap 
f7d10 69 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a  is normalized.**
f7d20 20 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69   to hold deal wi
f7d30 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65  th values betwee
f7d40 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72  n 1 and iDivisor
f7d50 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76  ..*/.struct Bitv
f7d60 65 63 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65  ec {.  u32 iSize
f7d70 3b 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75  ;      /* Maximu
f7d80 6d 20 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61  m bit index.  Ma
f7d90 78 20 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34  x iSize is 4,294
f7da0 2c 39 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 20 75  ,967,296. */.  u
f7db0 33 32 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f  32 nSet;       /
f7dc0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73  * Number of bits
f7dd0 20 74 68 61 74 20 61 72 65 20 73 65 74 20 2d 20   that are set - 
f7de0 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61  only valid for a
f7df0 48 61 73 68 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  Hash element */.
f7e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7e10 20 20 2f 2a 20 4d 61 78 20 6e 53 65 74 20 69 73    /* Max nSet is
f7e20 20 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46   BITVEC_NINT.  F
f7e30 6f 72 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20  or BITVEC_SZ of 
f7e40 35 31 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20  512, this would 
f7e50 62 65 20 31 32 35 2e 20 2a 2f 0a 20 20 75 33 32  be 125. */.  u32
f7e60 20 69 44 69 76 69 73 6f 72 3b 20 20 20 2f 2a 20   iDivisor;   /* 
f7e70 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 68  Number of bits h
f7e80 61 6e 64 6c 65 64 20 62 79 20 65 61 63 68 20 61  andled by each a
f7e90 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e 20 2a 2f  pSub[] entry. */
f7ea0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f7eb0 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d 30     /* Should >=0
f7ec0 20 66 6f 72 20 61 70 53 75 62 20 65 6c 65 6d 65   for apSub eleme
f7ed0 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  nt. */.         
f7ee0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20           /* Max 
f7ef0 69 44 69 76 69 73 6f 72 20 69 73 20 6d 61 78 28  iDivisor is max(
f7f00 75 33 32 29 20 2f 20 42 49 54 56 45 43 5f 4e 50  u32) / BITVEC_NP
f7f10 54 52 20 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 20  TR + 1.  */.    
f7f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f7f30 20 46 6f 72 20 61 20 42 49 54 56 45 43 5f 53 5a   For a BITVEC_SZ
f7f40 20 6f 66 20 35 31 32 2c 20 74 68 69 73 20 77 6f   of 512, this wo
f7f50 75 6c 64 20 62 65 20 33 34 2c 33 35 39 2c 37 33  uld be 34,359,73
f7f60 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a  9. */.  union {.
f7f70 20 20 20 20 42 49 54 56 45 43 5f 54 45 4c 45 4d      BITVEC_TELEM
f7f80 20 61 42 69 74 6d 61 70 5b 42 49 54 56 45 43 5f   aBitmap[BITVEC_
f7f90 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 69  NELEM];    /* Bi
f7fa0 74 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74  tmap representat
f7fb0 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 33 32 20 61  ion */.    u32 a
f7fc0 48 61 73 68 5b 42 49 54 56 45 43 5f 4e 49 4e 54  Hash[BITVEC_NINT
f7fd0 5d 3b 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20  ];      /* Hash 
f7fe0 74 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61  table representa
f7ff0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 42 69 74 76  tion */.    Bitv
f8000 65 63 20 2a 61 70 53 75 62 5b 42 49 54 56 45 43  ec *apSub[BITVEC
f8010 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 52 65 63 75  _NPTR];  /* Recu
f8020 72 73 69 76 65 20 72 65 70 72 65 73 65 6e 74 61  rsive representa
f8030 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d  tion */.  } u;.}
f8040 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  ;../*.** Create 
f8050 61 20 6e 65 77 20 62 69 74 6d 61 70 20 6f 62 6a  a new bitmap obj
f8060 65 63 74 20 61 62 6c 65 20 74 6f 20 68 61 6e 64  ect able to hand
f8070 6c 65 20 62 69 74 73 20 62 65 74 77 65 65 6e 20  le bits between 
f8080 30 20 61 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 20  0 and iSize,.** 
f8090 69 6e 63 6c 75 73 69 76 65 2e 20 20 52 65 74 75  inclusive.  Retu
f80a0 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
f80b0 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20  the new object. 
f80c0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
f80d0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  .** malloc fails
f80e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f80f0 41 54 45 20 42 69 74 76 65 63 20 2a 73 71 6c 69  ATE Bitvec *sqli
f8100 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28  te3BitvecCreate(
f8110 75 33 32 20 69 53 69 7a 65 29 7b 0a 20 20 42 69  u32 iSize){.  Bi
f8120 74 76 65 63 20 2a 70 3b 0a 20 20 61 73 73 65 72  tvec *p;.  asser
f8130 74 28 20 73 69 7a 65 6f 66 28 2a 70 29 3d 3d 42  t( sizeof(*p)==B
f8140 49 54 56 45 43 5f 53 5a 20 29 3b 0a 20 20 70 20  ITVEC_SZ );.  p 
f8150 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
f8160 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20  ero( sizeof(*p) 
f8170 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
f8180 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 69 53 69    p->iSize = iSi
f8190 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ze;.  }.  return
f81a0 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65   p;.}../*.** Che
f81b0 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
f81c0 20 69 2d 74 68 20 62 69 74 20 69 73 20 73 65 74   i-th bit is set
f81d0 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 6f  .  Return true o
f81e0 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 70  r false..** If p
f81f0 20 69 73 20 4e 55 4c 4c 20 28 69 66 20 74 68 65   is NULL (if the
f8200 20 62 69 74 6d 61 70 20 68 61 73 20 6e 6f 74 20   bitmap has not 
f8210 62 65 65 6e 20 63 72 65 61 74 65 64 29 20 6f 72  been created) or
f8220 20 69 66 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20   if.** i is out 
f8230 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 72  of range, then r
f8240 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a  eturn false..*/.
f8250 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
f8260 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  nt sqlite3Bitvec
f8270 54 65 73 74 28 42 69 74 76 65 63 20 2a 70 2c 20  Test(Bitvec *p, 
f8280 75 33 32 20 69 29 7b 0a 20 20 69 66 28 20 70 3d  u32 i){.  if( p=
f8290 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
f82a0 20 69 66 28 20 69 3e 70 2d 3e 69 53 69 7a 65 20   if( i>p->iSize 
f82b0 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74 75 72 6e  || i==0 ) return
f82c0 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69   0;.  i--;.  whi
f82d0 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20  le( p->iDivisor 
f82e0 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d  ){.    u32 bin =
f82f0 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a   i/p->iDivisor;.
f8300 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69      i = i%p->iDi
f8310 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70  visor;.    p = p
f8320 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a  ->u.apSub[bin];.
f8330 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20      if (!p) {.  
f8340 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
f8350 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d    }.  }.  if( p-
f8360 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e  >iSize<=BITVEC_N
f8370 42 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72  BIT ){.    retur
f8380 6e 20 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b  n (p->u.aBitmap[
f8390 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d  i/BITVEC_SZELEM]
f83a0 20 26 20 28 31 3c 3c 28 69 26 28 42 49 54 56 45   & (1<<(i&(BITVE
f83b0 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d  C_SZELEM-1))))!=
f83c0 30 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a 20 20 20  0;.  } else{.   
f83d0 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f   u32 h = BITVEC_
f83e0 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 20 20 77  HASH(i++);.    w
f83f0 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68  hile( p->u.aHash
f8400 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 69 66 28  [h] ){.      if(
f8410 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d   p->u.aHash[h]==
f8420 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  i ) return 1;.  
f8430 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 69      h++;.      i
f8440 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e  f( h>=BITVEC_NIN
f8450 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 7d  T ) h = 0;.    }
f8460 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
f8470 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20   }.}../*.** Set 
f8480 74 68 65 20 69 2d 74 68 20 62 69 74 2e 20 20 52  the i-th bit.  R
f8490 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65  eturn 0 on succe
f84a0 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ss and an error 
f84b0 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68  code if.** anyth
f84c0 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a  ing goes wrong..
f84d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
f84e0 6e 65 20 6d 69 67 68 74 20 63 61 75 73 65 20 73  ne might cause s
f84f0 75 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62 65  ub-bitmaps to be
f8500 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61 69   allocated.  Fai
f8510 6c 69 6e 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74  ling.** to get t
f8520 68 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64  he memory needed
f8530 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62   to hold the sub
f8540 2d 62 69 74 6d 61 70 20 69 73 20 74 68 65 20 6f  -bitmap is the o
f8550 6e 6c 79 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  nly.** that can 
f8560 67 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61 6e  go wrong with an
f8570 20 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69 6e   insert, assumin
f8580 67 20 70 20 61 6e 64 20 69 20 61 72 65 20 76 61  g p and i are va
f8590 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  lid..**.** The c
f85a0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
f85b0 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74  must ensure that
f85c0 20 70 20 69 73 20 61 20 76 61 6c 69 64 20 42 69   p is a valid Bi
f85d0 74 76 65 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61  tvec object.** a
f85e0 6e 64 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  nd that the valu
f85f0 65 20 66 6f 72 20 22 69 22 20 69 73 20 77 69 74  e for "i" is wit
f8600 68 69 6e 20 72 61 6e 67 65 20 6f 66 20 74 68 65  hin range of the
f8610 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a   Bitvec object..
f8620 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 74 68 65  ** Otherwise the
f8630 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
f8640 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
f8650 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f8660 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 42  lite3BitvecSet(B
f8670 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29  itvec *p, u32 i)
f8680 7b 0a 20 20 75 33 32 20 68 3b 0a 20 20 61 73 73  {.  u32 h;.  ass
f8690 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61  ert( p!=0 );.  a
f86a0 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20  ssert( i>0 );.  
f86b0 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53  assert( i<=p->iS
f86c0 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20  ize );.  i--;.  
f86d0 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a 65 20  while((p->iSize 
f86e0 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 29 20 26  > BITVEC_NBIT) &
f86f0 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 29 20 7b  & p->iDivisor) {
f8700 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69  .    u32 bin = i
f8710 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  /p->iDivisor;.  
f8720 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69    i = i%p->iDivi
f8730 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  sor;.    if( p->
f8740 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20  u.apSub[bin]==0 
f8750 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 2e 61 70  ){.      p->u.ap
f8760 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74  Sub[bin] = sqlit
f8770 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 20  e3BitvecCreate( 
f8780 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b 0a 20  p->iDivisor );. 
f8790 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 70       if( p->u.ap
f87a0 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65  Sub[bin]==0 ) re
f87b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
f87c0 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d  M;.    }.    p =
f87d0 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d   p->u.apSub[bin]
f87e0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69  ;.  }.  if( p->i
f87f0 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49  Size<=BITVEC_NBI
f8800 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42  T ){.    p->u.aB
f8810 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53  itmap[i/BITVEC_S
f8820 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20 28  ZELEM] |= 1 << (
f8830 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d  i&(BITVEC_SZELEM
f8840 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  -1));.    return
f8850 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
f8860 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53    h = BITVEC_HAS
f8870 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66 20  H(i++);.  /* if 
f8880 74 68 65 72 65 20 77 61 73 6e 27 74 20 61 20 68  there wasn't a h
f8890 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 61  ash collision, a
f88a0 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27 74 20  nd this doesn't 
f88b0 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65 74 65  */.  /* complete
f88c0 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61 73 68  ly fill the hash
f88d0 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64 64 20  , then just add 
f88e0 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a 20 20  it without */.  
f88f0 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f 75 74  /* worring about
f8900 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e   sub-dividing an
f8910 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f  d re-hashing. */
f8920 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61 48 61  .  if( !p->u.aHa
f8930 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 66 20  sh[h] ){.    if 
f8940 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56 45 43  (p->nSet<(BITVEC
f8950 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20 20 20  _NINT-1)) {.    
f8960 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 65    goto bitvec_se
f8970 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65 6c 73  t_end;.    } els
f8980 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  e {.      goto b
f8990 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73 68  itvec_set_rehash
f89a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  ;.    }.  }.  /*
f89b0 20 74 68 65 72 65 20 77 61 73 20 61 20 63 6f 6c   there was a col
f89c0 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20 74 6f  lision, check to
f89d0 20 73 65 65 20 69 66 20 69 74 27 73 20 61 6c 72   see if it's alr
f89e0 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e 20  eady */.  /* in 
f89f0 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20 74 72  hash, if not, tr
f8a00 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70 6f 74  y to find a spot
f8a10 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64 6f 20   for it */.  do 
f8a20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61  {.    if( p->u.a
f8a30 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74  Hash[h]==i ) ret
f8a40 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
f8a50 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 28 20     h++;.    if( 
f8a60 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29  h>=BITVEC_NINT )
f8a70 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c   h = 0;.  } whil
f8a80 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  e( p->u.aHash[h]
f8a90 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69 64 6e   );.  /* we didn
f8aa0 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20 74 68  't find it in th
f8ab0 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69 6e 74  e hash.  h point
f8ac0 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 2a  s to the first *
f8ad0 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62 6c 65  /.  /* available
f8ae0 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68 65 63   free spot. chec
f8af0 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73  k to see if this
f8b00 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a 2f 0a   is going to */.
f8b10 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20 68 61    /* make our ha
f8b20 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e 20 20  sh too "full".  
f8b30 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f 72 65  */.bitvec_set_re
f8b40 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d 3e 6e  hash:.  if( p->n
f8b50 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58 48 41  Set>=BITVEC_MXHA
f8b60 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e  SH ){.    unsign
f8b70 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 69 6e  ed int j;.    in
f8b80 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20 61 69  t rc;.    u32 ai
f8b90 56 61 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e 49  Values[BITVEC_NI
f8ba0 4e 54 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  NT];.    memcpy(
f8bb0 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61  aiValues, p->u.a
f8bc0 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 61 69 56  Hash, sizeof(aiV
f8bd0 61 6c 75 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d  alues));.    mem
f8be0 73 65 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20  set(p->u.apSub, 
f8bf0 30 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75  0, sizeof(aiValu
f8c00 65 73 29 29 3b 0a 20 20 20 20 70 2d 3e 69 44 69  es));.    p->iDi
f8c10 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a  visor = (p->iSiz
f8c20 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52 20  e + BITVEC_NPTR 
f8c30 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54 52  - 1)/BITVEC_NPTR
f8c40 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
f8c50 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 69  e3BitvecSet(p, i
f8c60 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  );.    for(j=0; 
f8c70 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a  j<BITVEC_NINT; j
f8c80 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61  ++){.      if( a
f8c90 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63 20  iValues[j] ) rc 
f8ca0 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  |= sqlite3Bitvec
f8cb0 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73 5b  Set(p, aiValues[
f8cc0 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  j]);.    }.    r
f8cd0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 62 69  eturn rc;.  }.bi
f8ce0 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 20  tvec_set_end:.  
f8cf0 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d 3e  p->nSet++;.  p->
f8d00 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a  u.aHash[h] = i;.
f8d10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f8d20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  OK;.}../*.** Cle
f8d30 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e  ar the i-th bit.
f8d40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
f8d50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
f8d60 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 65  itvecClear(Bitve
f8d70 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20  c *p, u32 i){.  
f8d80 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a  assert( p!=0 );.
f8d90 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b    assert( i>0 );
f8da0 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28  .  i--;.  while(
f8db0 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a   p->iDivisor ){.
f8dc0 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f      u32 bin = i/
f8dd0 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20  p->iDivisor;.   
f8de0 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73   i = i%p->iDivis
f8df0 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75  or;.    p = p->u
f8e00 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20  .apSub[bin];.   
f8e10 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 20   if (!p) {.     
f8e20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
f8e30 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a   }.  if( p->iSiz
f8e40 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29  e<=BITVEC_NBIT )
f8e50 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d  {.    p->u.aBitm
f8e60 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c  ap[i/BITVEC_SZEL
f8e70 45 4d 5d 20 26 3d 20 7e 28 31 20 3c 3c 20 28 69  EM] &= ~(1 << (i
f8e80 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d  &(BITVEC_SZELEM-
f8e90 31 29 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  1)));.  }else{. 
f8ea0 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
f8eb0 6a 3b 0a 20 20 20 20 75 33 32 20 61 69 56 61 6c  j;.    u32 aiVal
f8ec0 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d  ues[BITVEC_NINT]
f8ed0 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56  ;.    memcpy(aiV
f8ee0 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73  alues, p->u.aHas
f8ef0 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75  h, sizeof(aiValu
f8f00 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  es));.    memset
f8f10 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c 20  (p->u.aHash, 0, 
f8f20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29  sizeof(aiValues)
f8f30 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d  );.    p->nSet =
f8f40 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b   0;.    for(j=0;
f8f50 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20   j<BITVEC_NINT; 
f8f60 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
f8f70 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20 61  aiValues[j] && a
f8f80 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b 31  iValues[j]!=(i+1
f8f90 29 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32  ) ){.        u32
f8fa0 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48   h = BITVEC_HASH
f8fb0 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d 31 29 3b  (aiValues[j]-1);
f8fc0 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 53 65 74  .        p->nSet
f8fd0 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ++;.        whil
f8fe0 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  e( p->u.aHash[h]
f8ff0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 68 2b   ){.          h+
f9000 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  +;.          if(
f9010 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20   h>=BITVEC_NINT 
f9020 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ) h = 0;.       
f9030 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 75 2e   }.        p->u.
f9040 61 48 61 73 68 5b 68 5d 20 3d 20 61 69 56 61 6c  aHash[h] = aiVal
f9050 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a  ues[j];.      }.
f9060 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
f9070 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69 74  ** Destroy a bit
f9080 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65 63  map object.  Rec
f9090 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  laim all memory 
f90a0 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  used..*/.SQLITE_
f90b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
f90c0 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
f90d0 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20  y(Bitvec *p){.  
f90e0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
f90f0 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 44 69 76  n;.  if( p->iDiv
f9100 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 6e 73 69  isor ){.    unsi
f9110 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20 20  gned int i;.    
f9120 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 54 56 45  for(i=0; i<BITVE
f9130 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20 20  C_NPTR; i++){.  
f9140 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
f9150 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61 70  cDestroy(p->u.ap
f9160 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  Sub[i]);.    }. 
f9170 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
f9180 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  e(p);.}../*.** R
f9190 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20  eturn the value 
f91a0 6f 66 20 74 68 65 20 69 53 69 7a 65 20 70 61 72  of the iSize par
f91b0 61 6d 65 74 65 72 20 73 70 65 63 69 66 69 65 64  ameter specified
f91c0 20 77 68 65 6e 20 42 69 74 76 65 63 20 2a 70 0a   when Bitvec *p.
f91d0 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a  ** was created..
f91e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
f91f0 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 69 74  E u32 sqlite3Bit
f9200 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 20 2a  vecSize(Bitvec *
f9210 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p){.  return p->
f9220 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65  iSize;.}..#ifnde
f9230 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
f9240 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  ILTIN_TEST./*.**
f9250 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 6e 20 61   Let V[] be an a
f9260 72 72 61 79 20 6f 66 20 75 6e 73 69 67 6e 65 64  rray of unsigned
f9270 20 63 68 61 72 61 63 74 65 72 73 20 73 75 66 66   characters suff
f9280 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a  icient to hold.*
f9290 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 73 2e 20  * up to N bits. 
f92a0 20 4c 65 74 20 49 20 62 65 20 61 6e 20 69 6e 74   Let I be an int
f92b0 65 67 65 72 20 62 65 74 77 65 65 6e 20 30 20 61  eger between 0 a
f92c0 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a  nd N.  0<=I<N..*
f92d0 2a 20 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  * Then the follo
f92e0 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e 20  wing macros can 
f92f0 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 2c 20  be used to set, 
f9300 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a  clear, or test.*
f9310 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 62 69 74  * individual bit
f9320 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23  s within V..*/.#
f9330 64 65 66 69 6e 65 20 53 45 54 42 49 54 28 56 2c  define SETBIT(V,
f9340 49 29 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20  I)      V[I>>3] 
f9350 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 0a 23 64  |= (1<<(I&7)).#d
f9360 65 66 69 6e 65 20 43 4c 45 41 52 42 49 54 28 56  efine CLEARBIT(V
f9370 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26  ,I)    V[I>>3] &
f9380 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23 64  = ~(1<<(I&7)).#d
f9390 65 66 69 6e 65 20 54 45 53 54 42 49 54 28 56 2c  efine TESTBIT(V,
f93a0 49 29 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26  I)     (V[I>>3]&
f93b0 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a  (1<<(I&7)))!=0..
f93c0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
f93d0 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 74 65 6e  ne runs an exten
f93e0 73 69 76 65 20 74 65 73 74 20 6f 66 20 74 68 65  sive test of the
f93f0 20 42 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a   Bitvec code..**
f9400 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73  .** The input is
f9410 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74   an array of int
f9420 65 67 65 72 73 20 74 68 61 74 20 61 63 74 73 20  egers that acts 
f9430 61 73 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  as a program.** 
f9440 74 6f 20 74 65 73 74 20 74 68 65 20 42 69 74 76  to test the Bitv
f9450 65 63 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72  ec.  The integer
f9460 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f  s are opcodes fo
f9470 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20  llowed.** by 0, 
f9480 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 6e 64 73  1, or 3 operands
f9490 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  , depending on t
f94a0 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74  he opcode.  Anot
f94b0 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f  her.** opcode fo
f94c0 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 74 65 6c  llows immediatel
f94d0 79 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74  y after the last
f94e0 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   operand..**.** 
f94f0 54 68 65 72 65 20 61 72 65 20 36 20 6f 70 63 6f  There are 6 opco
f9500 64 65 73 20 6e 75 6d 62 65 72 65 64 20 66 72 6f  des numbered fro
f9510 6d 20 30 20 74 68 72 6f 75 67 68 20 35 2e 20 20  m 0 through 5.  
f9520 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c  0 is the.** "hal
f9530 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 20 63 61  t" opcode and ca
f9540 75 73 65 73 20 74 68 65 20 74 65 73 74 20 74 6f  uses the test to
f9550 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30   end..**.**    0
f9560 20 20 20 20 20 20 20 20 20 20 48 61 6c 74 20 61            Halt a
f9570 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  nd return the nu
f9580 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a  mber of errors.*
f9590 2a 20 20 20 20 31 20 4e 20 53 20 58 20 20 20 20  *    1 N S X    
f95a0 53 65 74 20 4e 20 62 69 74 73 20 62 65 67 69 6e  Set N bits begin
f95b0 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20  ning with S and 
f95c0 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20  incrementing by 
f95d0 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 20 58 20  X.**    2 N S X 
f95e0 20 20 20 43 6c 65 61 72 20 4e 20 62 69 74 73 20     Clear N bits 
f95f0 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53  beginning with S
f9600 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e   and incrementin
f9610 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e  g by X.**    3 N
f9620 20 20 20 20 20 20 20 20 53 65 74 20 4e 20 72 61          Set N ra
f9630 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69  ndomly chosen bi
f9640 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 20  ts.**    4 N    
f9650 20 20 20 20 43 6c 65 61 72 20 4e 20 72 61 6e 64      Clear N rand
f9660 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73  omly chosen bits
f9670 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 58 20 20  .**    5 N S X  
f9680 20 20 53 65 74 20 4e 20 62 69 74 73 20 66 72 6f    Set N bits fro
f9690 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 20  m S increment X 
f96a0 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e  in array only, n
f96b0 6f 74 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a  ot in bitvec.**.
f96c0 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 73 20 31  ** The opcodes 1
f96d0 20 74 68 72 6f 75 67 68 20 34 20 70 65 72 66 6f   through 4 perfo
f96e0 72 6d 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72  rm set and clear
f96f0 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20   operations are 
f9700 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20  performed.** on 
f9710 62 6f 74 68 20 61 20 42 69 74 76 65 63 20 6f 62  both a Bitvec ob
f9720 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69  ject and on a li
f9730 6e 65 61 72 20 61 72 72 61 79 20 6f 66 20 62 69  near array of bi
f9740 74 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ts obtained from
f9750 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f   malloc..** Opco
f9760 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68  de 5 works on th
f9770 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f  e linear array o
f9780 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20  nly, not on the 
f9790 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64  Bitvec..** Opcod
f97a0 65 20 35 20 69 73 20 75 73 65 64 20 74 6f 20 64  e 5 is used to d
f97b0 65 6c 69 62 65 72 61 74 65 6c 79 20 69 6e 64 75  eliberately indu
f97c0 63 65 20 61 20 66 61 75 6c 74 20 69 6e 20 6f 72  ce a fault in or
f97d0 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72  der to.** confir
f97e0 6d 20 74 68 61 74 20 65 72 72 6f 72 20 64 65 74  m that error det
f97f0 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a  ection works..**
f9800 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f 6e 63 6c  .** At the concl
f9810 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 73  usion of the tes
f9820 74 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72  t the linear arr
f9830 61 79 20 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a  ay is compared.*
f9840 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 42 69  * against the Bi
f9850 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 49 66  tvec object.  If
f9860 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 64   there are any d
f9870 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61  ifferences,.** a
f9880 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
f9890 6e 65 64 2e 20 20 49 66 20 74 68 65 79 20 61 72  ned.  If they ar
f98a0 65 20 74 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f  e the same, zero
f98b0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
f98c0 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  .** If a memory 
f98d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
f98e0 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20   occurs, return 
f98f0 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  -1..*/.SQLITE_PR
f9900 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
f9910 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65  3BitvecBuiltinTe
f9920 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a  st(int sz, int *
f9930 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 63 20 2a  aOp){.  Bitvec *
f9940 70 42 69 74 76 65 63 20 3d 20 30 3b 0a 20 20 75  pBitvec = 0;.  u
f9950 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 56  nsigned char *pV
f9960 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 0;.  int rc =
f9970 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78   -1;.  int i, nx
f9980 2c 20 70 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a 20  , pc, op;..  /* 
f9990 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69 74  Allocate the Bit
f99a0 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65 64  vec to be tested
f99b0 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61 72   and a linear ar
f99c0 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73  ray of.  ** bits
f99d0 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72   to act as the r
f99e0 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42  eference */.  pB
f99f0 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33 42  itvec = sqlite3B
f9a00 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a 20  itvecCreate( sz 
f9a10 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74 65  );.  pV = sqlite
f9a20 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29  3_malloc( (sz+7)
f9a30 2f 38 20 2b 20 31 20 29 3b 0a 20 20 69 66 28 20  /8 + 1 );.  if( 
f9a40 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56  pBitvec==0 || pV
f9a50 3d 3d 30 20 29 20 67 6f 74 6f 20 62 69 74 76 65  ==0 ) goto bitve
f9a60 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28  c_end;.  memset(
f9a70 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20  pV, 0, (sz+7)/8 
f9a80 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20  + 1);..  /* Run 
f9a90 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  the program */. 
f9aa0 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65   pc = 0;.  while
f9ab0 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29  ( (op = aOp[pc])
f9ac0 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63  !=0 ){.    switc
f9ad0 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63  h( op ){.      c
f9ae0 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73  ase 1:.      cas
f9af0 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20  e 2:.      case 
f9b00 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20  5: {.        nx 
f9b10 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d  = 4;.        i =
f9b20 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a   aOp[pc+2] - 1;.
f9b30 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32          aOp[pc+2
f9b40 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a  ] += aOp[pc+3];.
f9b50 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
f9b60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
f9b70 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20  e 3:.      case 
f9b80 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c  4: .      defaul
f9b90 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20  t: {.        nx 
f9ba0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 2;.        sql
f9bb0 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
f9bc0 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a  sizeof(i), &i);.
f9bd0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
f9be0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
f9bf0 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31   if( (--aOp[pc+1
f9c00 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b  ]) > 0 ) nx = 0;
f9c10 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20  .    pc += nx;. 
f9c20 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 66     i = (i & 0x7f
f9c30 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20  ffffff)%sz;.    
f9c40 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20  if( (op & 1)!=0 
f9c50 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 28  ){.      SETBIT(
f9c60 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20  pV, (i+1));.    
f9c70 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20    if( op!=5 ){. 
f9c80 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
f9c90 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69 74  e3BitvecSet(pBit
f9ca0 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f  vec, i+1) ) goto
f9cb0 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20   bitvec_end;.   
f9cc0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
f9cd0 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 70        CLEARBIT(p
f9ce0 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20  V, (i+1));.     
f9cf0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c   sqlite3BitvecCl
f9d00 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31  ear(pBitvec, i+1
f9d10 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
f9d20 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20  /* Test to make 
f9d30 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20  sure the linear 
f9d40 61 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61  array exactly ma
f9d50 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42  tches the.  ** B
f9d60 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53  itvec object.  S
f9d70 74 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73  tart with the as
f9d80 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68  sumption that th
f9d90 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68  ey do.  ** match
f9da0 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67   (rc==0).  Chang
f9db0 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f  e rc to non-zero
f9dc0 20 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63   if a discrepanc
f9dd0 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e  y.  ** is found.
f9de0 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
f9df0 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30  ite3BitvecTest(0
f9e00 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74  ,0) + sqlite3Bit
f9e10 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c  vecTest(pBitvec,
f9e20 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20   sz+1).         
f9e30 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   + sqlite3Bitvec
f9e40 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29  Test(pBitvec, 0)
f9e50 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d  ;.  for(i=1; i<=
f9e60 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  sz; i++){.    if
f9e70 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69  (  (TESTBIT(pV,i
f9e80 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65  ))!=sqlite3Bitve
f9e90 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29  cTest(pBitvec,i)
f9ea0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69   ){.      rc = i
f9eb0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
f9ec0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
f9ed0 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74  ree allocated st
f9ee0 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65  ructure */.bitve
f9ef0 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33  c_end:.  sqlite3
f9f00 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 6c  _free(pV);.  sql
f9f10 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
f9f20 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 65  y(pBitvec);.  re
f9f30 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
f9f40 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
f9f50 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f  _BUILTIN_TEST */
f9f60 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
f9f70 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 2e  * End of bitvec.
f9f80 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
f9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9fb0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
f9fc0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 61  * Begin file pca
f9fd0 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  che.c **********
f9fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa000 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67  /./*.** 2008 Aug
fa010 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 05.**.** The
fa020 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
fa030 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
fa040 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
fa050 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
fa060 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
fa070 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
fa080 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
fa090 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
fa0a0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
fa0b0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
fa0c0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
fa0d0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
fa0e0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
fa0f0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
fa100 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
fa110 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
fa120 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
fa130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa170 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
fa180 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  le implements th
fa190 61 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a 2a  at page cache..*
fa1a0 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70  *.** @(#) $Id: p
fa1b0 63 61 63 68 65 2e 63 2c 76 20 31 2e 34 34 20 32  cache.c,v 1.44 2
fa1c0 30 30 39 2f 30 33 2f 33 31 20 30 31 3a 33 32 3a  009/03/31 01:32:
fa1d0 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  18 drh Exp $.*/.
fa1e0 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74  ./*.** A complet
fa1f0 65 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20  e page cache is 
fa200 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
fa210 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
fa220 2f 0a 73 74 72 75 63 74 20 50 43 61 63 68 65 20  /.struct PCache 
fa230 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74  {.  PgHdr *pDirt
fa240 79 2c 20 2a 70 44 69 72 74 79 54 61 69 6c 3b 20  y, *pDirtyTail; 
fa250 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
fa260 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 69  of dirty pages i
fa270 6e 20 4c 52 55 20 6f 72 64 65 72 20 2a 2f 0a 20  n LRU order */. 
fa280 20 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 3b   PgHdr *pSynced;
fa290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fa2a0 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e       /* Last syn
fa2b0 63 65 64 20 70 61 67 65 20 69 6e 20 64 69 72 74  ced page in dirt
fa2c0 79 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20  y page list */. 
fa2d0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
fa2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fa2f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
fa300 66 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67  f referenced pag
fa310 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78  es */.  int nMax
fa320 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
fa330 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
fa340 6f 6e 66 69 67 75 72 65 64 20 63 61 63 68 65 20  onfigured cache 
fa350 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  size */.  int sz
fa360 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
fa370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
fa380 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70   Size of every p
fa390 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 63 68  age in this cach
fa3a0 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74  e */.  int szExt
fa3b0 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ra;             
fa3c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
fa3d0 7a 65 20 6f 66 20 65 78 74 72 61 20 73 70 61 63  ze of extra spac
fa3e0 65 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  e for each page 
fa3f0 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61  */.  int bPurgea
fa400 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ble;            
fa410 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
fa420 20 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e   if pages are on
fa430 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a   backing store *
fa440 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73  /.  int (*xStres
fa450 73 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29  s)(void*,PgHdr*)
fa460 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20  ;       /* Call 
fa470 74 6f 20 74 72 79 20 6d 61 6b 65 20 61 20 70 61  to try make a pa
fa480 67 65 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f  ge clean */.  vo
fa490 69 64 20 2a 70 53 74 72 65 73 73 3b 20 20 20 20  id *pStress;    
fa4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fa4b0 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
fa4c0 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 73 71   xStress */.  sq
fa4d0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 43  lite3_pcache *pC
fa4e0 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20  ache;           
fa4f0 20 20 2f 2a 20 50 6c 75 67 67 61 62 6c 65 20 63    /* Pluggable c
fa500 61 63 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20  ache module */. 
fa510 20 50 67 48 64 72 20 2a 70 50 61 67 65 31 3b 20   PgHdr *pPage1; 
fa520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fa530 20 20 20 20 20 2f 2a 20 52 65 66 65 72 65 6e 63       /* Referenc
fa540 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 7d  e to page 1 */.}
fa550 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66  ;../*.** Some of
fa560 20 74 68 65 20 61 73 73 65 72 74 28 29 20 6d 61   the assert() ma
fa570 63 72 6f 73 20 69 6e 20 74 68 69 73 20 63 6f 64  cros in this cod
fa580 65 20 61 72 65 20 74 6f 6f 20 65 78 70 65 6e 73  e are too expens
fa590 69 76 65 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 76  ive to run.** ev
fa5a0 65 6e 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c  en during normal
fa5b0 20 64 65 62 75 67 67 69 6e 67 2e 20 20 55 73 65   debugging.  Use
fa5c0 20 74 68 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c   them only rarel
fa5d0 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e  y on long-runnin
fa5e0 67 0a 2a 2a 20 74 65 73 74 73 2e 20 20 45 6e 61  g.** tests.  Ena
fa5f0 62 6c 65 20 74 68 65 20 65 78 70 65 6e 73 69 76  ble the expensiv
fa600 65 20 61 73 73 65 72 74 73 20 75 73 69 6e 67 20  e asserts using 
fa610 74 68 65 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f  the.** -DSQLITE_
fa620 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45  ENABLE_EXPENSIVE
fa630 5f 41 53 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c  _ASSERT=1 compil
fa640 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a  e-time option..*
fa650 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
fa660 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45  ENABLE_EXPENSIVE
fa670 5f 41 53 53 45 52 54 0a 23 20 64 65 66 69 6e 65  _ASSERT.# define
fa680 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72   expensive_asser
fa690 74 28 58 29 20 20 61 73 73 65 72 74 28 58 29 0a  t(X)  assert(X).
fa6a0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 65  #else.# define e
fa6b0 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28  xpensive_assert(
fa6c0 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  X).#endif../****
fa6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c  ************** L
fa6f0 69 6e 6b 65 64 20 4c 69 73 74 20 4d 61 6e 61 67  inked List Manag
fa700 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ement **********
fa710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66  **********/..#if
fa720 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
fa730 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
fa740 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
fa750 53 49 56 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a  SIVE_ASSERT)./*.
fa760 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  ** Check that th
fa770 65 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65  e pCache->pSynce
fa780 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  d variable is se
fa790 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 49 66 20  t correctly. If 
fa7a0 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 65 69  it.** is not, ei
fa7b0 74 68 65 72 20 66 61 69 6c 20 61 6e 20 61 73 73  ther fail an ass
fa7c0 65 72 74 20 6f 72 20 72 65 74 75 72 6e 20 7a 65  ert or return ze
fa7d0 72 6f 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72  ro. Otherwise, r
fa7e0 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  eturn.** non-zer
fa7f0 6f 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20  o. This is only 
fa800 75 73 65 64 20 69 6e 20 64 65 62 75 67 67 69 6e  used in debuggin
fa810 67 20 62 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c  g builds, as fol
fa820 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 65 78  lows:.**.**   ex
fa830 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20  pensive_assert( 
fa840 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65  pcacheCheckSynce
fa850 64 28 70 43 61 63 68 65 29 20 29 3b 0a 2a 2f 0a  d(pCache) );.*/.
fa860 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68  static int pcach
fa870 65 43 68 65 63 6b 53 79 6e 63 65 64 28 50 43 61  eCheckSynced(PCa
fa880 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  che *pCache){.  
fa890 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28  PgHdr *p;.  for(
fa8a0 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  p=pCache->pDirty
fa8b0 54 61 69 6c 3b 20 70 21 3d 70 43 61 63 68 65 2d  Tail; p!=pCache-
fa8c0 3e 70 53 79 6e 63 65 64 3b 20 70 3d 70 2d 3e 70  >pSynced; p=p->p
fa8d0 44 69 72 74 79 50 72 65 76 29 7b 0a 20 20 20 20  DirtyPrev){.    
fa8e0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 20  assert( p->nRef 
fa8f0 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48  || (p->flags&PGH
fa900 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 3b  DR_NEED_SYNC) );
fa910 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70  .  }.  return (p
fa920 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66 20 7c  ==0 || p->nRef |
fa930 7c 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44  | (p->flags&PGHD
fa940 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d 30 29  R_NEED_SYNC)==0)
fa950 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e  ;.}.#endif /* !N
fa960 44 45 42 55 47 20 26 26 20 53 51 4c 49 54 45 5f  DEBUG && SQLITE_
fa970 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45  ENABLE_EXPENSIVE
fa980 5f 41 53 53 45 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a  _ASSERT */../*.*
fa990 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 70 50  * Remove page pP
fa9a0 61 67 65 20 66 72 6f 6d 20 74 68 65 20 6c 69 73  age from the lis
fa9b0 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73  t of dirty pages
fa9c0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
fa9d0 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f   pcacheRemoveFro
fa9e0 6d 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72  mDirtyList(PgHdr
fa9f0 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63   *pPage){.  PCac
faa00 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70  he *p = pPage->p
faa10 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74  Cache;..  assert
faa20 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e  ( pPage->pDirtyN
faa30 65 78 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d  ext || pPage==p-
faa40 3e 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20  >pDirtyTail );. 
faa50 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
faa60 70 44 69 72 74 79 50 72 65 76 20 7c 7c 20 70 50  pDirtyPrev || pP
faa70 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29  age==p->pDirty )
faa80 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74  ;..  /* Update t
faa90 68 65 20 50 43 61 63 68 65 31 2e 70 53 79 6e 63  he PCache1.pSync
faaa0 65 64 20 76 61 72 69 61 62 6c 65 20 69 66 20 6e  ed variable if n
faab0 65 63 65 73 73 61 72 79 2e 20 2a 2f 0a 20 20 69  ecessary. */.  i
faac0 66 28 20 70 2d 3e 70 53 79 6e 63 65 64 3d 3d 70  f( p->pSynced==p
faad0 50 61 67 65 20 29 7b 0a 20 20 20 20 50 67 48 64  Page ){.    PgHd
faae0 72 20 2a 70 53 79 6e 63 65 64 20 3d 20 70 50 61  r *pSynced = pPa
faaf0 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a  ge->pDirtyPrev;.
fab00 20 20 20 20 77 68 69 6c 65 28 20 70 53 79 6e 63      while( pSync
fab10 65 64 20 26 26 20 28 70 53 79 6e 63 65 64 2d 3e  ed && (pSynced->
fab20 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
fab30 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 20 20  _SYNC) ){.      
fab40 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65  pSynced = pSynce
fab50 64 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20  d->pDirtyPrev;. 
fab60 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 53 79 6e     }.    p->pSyn
fab70 63 65 64 20 3d 20 70 53 79 6e 63 65 64 3b 0a 20  ced = pSynced;. 
fab80 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d   }..  if( pPage-
fab90 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20  >pDirtyNext ){. 
faba0 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79     pPage->pDirty
fabb0 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76  Next->pDirtyPrev
fabc0 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79   = pPage->pDirty
fabd0 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Prev;.  }else{. 
fabe0 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
fabf0 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20  ==p->pDirtyTail 
fac00 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79  );.    p->pDirty
fac10 54 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44  Tail = pPage->pD
fac20 69 72 74 79 50 72 65 76 3b 0a 20 20 7d 0a 20 20  irtyPrev;.  }.  
fac30 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74  if( pPage->pDirt
fac40 79 50 72 65 76 20 29 7b 0a 20 20 20 20 70 50 61  yPrev ){.    pPa
fac50 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 2d 3e  ge->pDirtyPrev->
fac60 70 44 69 72 74 79 4e 65 78 74 20 3d 20 70 50 61  pDirtyNext = pPa
fac70 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a  ge->pDirtyNext;.
fac80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
fac90 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70  ert( pPage==p->p
faca0 44 69 72 74 79 20 29 3b 0a 20 20 20 20 70 2d 3e  Dirty );.    p->
facb0 70 44 69 72 74 79 20 3d 20 70 50 61 67 65 2d 3e  pDirty = pPage->
facc0 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a  pDirtyNext;.  }.
facd0 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e    pPage->pDirtyN
face0 65 78 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  ext = 0;.  pPage
facf0 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 30  ->pDirtyPrev = 0
fad00 3b 0a 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61  ;..  expensive_a
fad10 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65  ssert( pcacheChe
fad20 63 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d  ckSynced(p) );.}
fad30 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 70 61 67 65  ../*.** Add page
fad40 20 70 50 61 67 65 20 74 6f 20 74 68 65 20 68 65   pPage to the he
fad50 61 64 20 6f 66 20 74 68 65 20 64 69 72 74 79 20  ad of the dirty 
fad60 6c 69 73 74 20 28 50 43 61 63 68 65 31 2e 70 44  list (PCache1.pD
fad70 69 72 74 79 20 69 73 20 73 65 74 20 74 6f 0a 2a  irty is set to.*
fad80 2a 20 70 50 61 67 65 29 2e 0a 2a 2f 0a 73 74 61  * pPage)..*/.sta
fad90 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 41  tic void pcacheA
fada0 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28 50 67  ddToDirtyList(Pg
fadb0 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 50  Hdr *pPage){.  P
fadc0 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 65  Cache *p = pPage
fadd0 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73 73  ->pCache;..  ass
fade0 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72  ert( pPage->pDir
fadf0 74 79 4e 65 78 74 3d 3d 30 20 26 26 20 70 50 61  tyNext==0 && pPa
fae00 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d  ge->pDirtyPrev==
fae10 30 20 26 26 20 70 2d 3e 70 44 69 72 74 79 21 3d  0 && p->pDirty!=
fae20 70 50 61 67 65 20 29 3b 0a 0a 20 20 70 50 61 67  pPage );..  pPag
fae30 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20  e->pDirtyNext = 
fae40 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20 69 66 28  p->pDirty;.  if(
fae50 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65   pPage->pDirtyNe
fae60 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  xt ){.    assert
fae70 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e  ( pPage->pDirtyN
fae80 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 3d  ext->pDirtyPrev=
fae90 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d  =0 );.    pPage-
faea0 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44 69  >pDirtyNext->pDi
faeb0 72 74 79 50 72 65 76 20 3d 20 70 50 61 67 65 3b  rtyPrev = pPage;
faec0 0a 20 20 7d 0a 20 20 70 2d 3e 70 44 69 72 74 79  .  }.  p->pDirty
faed0 20 3d 20 70 50 61 67 65 3b 0a 20 20 69 66 28 20   = pPage;.  if( 
faee0 21 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29  !p->pDirtyTail )
faef0 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54  {.    p->pDirtyT
faf00 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d  ail = pPage;.  }
faf10 0a 20 20 69 66 28 20 21 70 2d 3e 70 53 79 6e 63  .  if( !p->pSync
faf20 65 64 20 26 26 20 30 3d 3d 28 70 50 61 67 65 2d  ed && 0==(pPage-
faf30 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
faf40 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70  D_SYNC) ){.    p
faf50 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67  ->pSynced = pPag
faf60 65 3b 0a 20 20 7d 0a 20 20 65 78 70 65 6e 73 69  e;.  }.  expensi
faf70 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68  ve_assert( pcach
faf80 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20  eCheckSynced(p) 
faf90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70  );.}../*.** Wrap
fafa0 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 70  per around the p
fafb0 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 73 20  luggable caches 
fafc0 78 55 6e 70 69 6e 20 6d 65 74 68 6f 64 2e 20 49  xUnpin method. I
fafd0 66 20 74 68 65 20 63 61 63 68 65 20 69 73 0a 2a  f the cache is.*
fafe0 2a 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72  * being used for
faff0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
fb000 74 61 62 61 73 65 2c 20 74 68 69 73 20 66 75 6e  tabase, this fun
fb010 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
fb020 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
fb030 20 70 63 61 63 68 65 55 6e 70 69 6e 28 50 67 48   pcacheUnpin(PgH
fb040 64 72 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65  dr *p){.  PCache
fb050 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43   *pCache = p->pC
fb060 61 63 68 65 3b 0a 20 20 69 66 28 20 70 43 61 63  ache;.  if( pCac
fb070 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29  he->bPurgeable )
fb080 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e  {.    if( p->pgn
fb090 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43  o==1 ){.      pC
fb0a0 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 30  ache->pPage1 = 0
fb0b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
fb0c0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
fb0d0 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43  pcache.xUnpin(pC
fb0e0 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c  ache->pCache, p,
fb0f0 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a   0);.  }.}../***
fb100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fb110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fb120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fb130 20 47 65 6e 65 72 61 6c 20 49 6e 74 65 72 66 61   General Interfa
fb140 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  ces ******.**.**
fb150 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
fb160 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67  shutdown the pag
fb170 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65  e cache subsyste
fb180 6d 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68  m. Neither of th
fb190 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ese .** function
fb1a0 73 20 61 72 65 20 74 68 72 65 61 64 73 61 66 65  s are threadsafe
fb1b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
fb1c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
fb1d0 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28  cacheInitialize(
fb1e0 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c  void){.  if( sql
fb1f0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
fb200 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30  .pcache.xInit==0
fb210 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   ){.    sqlite3P
fb220 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28  CacheSetDefault(
fb230 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
fb240 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
fb250 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 74  fig.pcache.xInit
fb260 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
fb270 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67  nfig.pcache.pArg
fb280 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
fb290 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
fb2a0 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76  PcacheShutdown(v
fb2b0 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69  oid){.  if( sqli
fb2c0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
fb2d0 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e  pcache.xShutdown
fb2e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   ){.    sqlite3G
fb2f0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
fb300 68 65 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c  he.xShutdown(sql
fb310 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
fb320 2e 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 20  .pcache.pArg);. 
fb330 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
fb340 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62  rn the size in b
fb350 79 74 65 73 20 6f 66 20 61 20 50 43 61 63 68 65  ytes of a PCache
fb360 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49   object..*/.SQLI
fb370 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
fb380 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65  qlite3PcacheSize
fb390 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73  (void){ return s
fb3a0 69 7a 65 6f 66 28 50 43 61 63 68 65 29 3b 20 7d  izeof(PCache); }
fb3b0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
fb3c0 20 6e 65 77 20 50 43 61 63 68 65 20 6f 62 6a 65   new PCache obje
fb3d0 63 74 2e 20 53 74 6f 72 61 67 65 20 73 70 61 63  ct. Storage spac
fb3e0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62  e to hold the ob
fb3f0 6a 65 63 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65  ject.** has alre
fb400 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ady been allocat
fb410 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64  ed and is passed
fb420 20 69 6e 20 61 73 20 74 68 65 20 70 20 70 6f 69   in as the p poi
fb430 6e 74 65 72 2e 20 0a 2a 2a 20 54 68 65 20 63 61  nter. .** The ca
fb440 6c 6c 65 72 20 64 69 73 63 6f 76 65 72 73 20 68  ller discovers h
fb450 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 6e 65  ow much space ne
fb460 65 64 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61  eds to be alloca
fb470 74 65 64 20 62 79 20 0a 2a 2a 20 63 61 6c 6c 69  ted by .** calli
fb480 6e 67 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  ng sqlite3Pcache
fb490 53 69 7a 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  Size()..*/.SQLIT
fb4a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
fb4b0 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e  qlite3PcacheOpen
fb4c0 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20  (.  int szPage, 
fb4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb4e0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72   /* Size of ever
fb4f0 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  y page */.  int 
fb500 73 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20 20  szExtra,        
fb510 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
fb520 61 20 73 70 61 63 65 20 61 73 73 6f 63 69 61 74  a space associat
fb530 65 64 20 77 69 74 68 20 65 61 63 68 20 70 61 67  ed with each pag
fb540 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67  e */.  int bPurg
fb550 65 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20  eable,          
fb560 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
fb570 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b  ages are on back
fb580 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69  ing store */.  i
fb590 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f  nt (*xStress)(vo
fb5a0 69 64 2a 2c 50 67 48 64 72 2a 29 2c 2f 2a 20 43  id*,PgHdr*),/* C
fb5b0 61 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61  all to try to ma
fb5c0 6b 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a  ke pages clean *
fb5d0 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73  /.  void *pStres
fb5e0 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
fb5f0 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
fb600 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 61  xStress */.  PCa
fb610 63 68 65 20 2a 70 20 20 20 20 20 20 20 20 20 20  che *p          
fb620 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
fb630 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20  allocated space 
fb640 66 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a  for the PCache *
fb650 2f 0a 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c  /.){.  memset(p,
fb660 20 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68   0, sizeof(PCach
fb670 65 29 29 3b 0a 20 20 70 2d 3e 73 7a 50 61 67 65  e));.  p->szPage
fb680 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 70 2d 3e   = szPage;.  p->
fb690 73 7a 45 78 74 72 61 20 3d 20 73 7a 45 78 74 72  szExtra = szExtr
fb6a0 61 3b 0a 20 20 70 2d 3e 62 50 75 72 67 65 61 62  a;.  p->bPurgeab
fb6b0 6c 65 20 3d 20 62 50 75 72 67 65 61 62 6c 65 3b  le = bPurgeable;
fb6c0 0a 20 20 70 2d 3e 78 53 74 72 65 73 73 20 3d 20  .  p->xStress = 
fb6d0 78 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53  xStress;.  p->pS
fb6e0 74 72 65 73 73 20 3d 20 70 53 74 72 65 73 73 3b  tress = pStress;
fb6f0 0a 20 20 70 2d 3e 6e 4d 61 78 20 3d 20 31 30 30  .  p->nMax = 100
fb700 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
fb710 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  e the page size 
fb720 66 6f 72 20 50 43 61 63 68 65 20 6f 62 6a 65 63  for PCache objec
fb730 74 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75  t. The caller mu
fb740 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  st ensure that t
fb750 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6f  here.** are no o
fb760 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20  utstanding page 
fb770 72 65 66 65 72 65 6e 63 65 73 20 77 68 65 6e 20  references when 
fb780 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
fb790 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49   called..*/.SQLI
fb7a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
fb7b0 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74  sqlite3PcacheSet
fb7c0 50 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 20  PageSize(PCache 
fb7d0 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 73 7a 50  *pCache, int szP
fb7e0 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  age){.  assert( 
fb7f0 70 43 61 63 68 65 2d 3e 6e 52 65 66 3d 3d 30 20  pCache->nRef==0 
fb800 26 26 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74  && pCache->pDirt
fb810 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43  y==0 );.  if( pC
fb820 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a  ache->pCache ){.
fb830 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
fb840 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
fb850 44 65 73 74 72 6f 79 28 70 43 61 63 68 65 2d 3e  Destroy(pCache->
fb860 70 43 61 63 68 65 29 3b 0a 20 20 20 20 70 43 61  pCache);.    pCa
fb870 63 68 65 2d 3e 70 43 61 63 68 65 20 3d 20 30 3b  che->pCache = 0;
fb880 0a 20 20 7d 0a 20 20 70 43 61 63 68 65 2d 3e 73  .  }.  pCache->s
fb890 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a  zPage = szPage;.
fb8a0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20  }../*.** Try to 
fb8b0 6f 62 74 61 69 6e 20 61 20 70 61 67 65 20 66 72  obtain a page fr
fb8c0 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f  om the cache..*/
fb8d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
fb8e0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
fb8f0 65 46 65 74 63 68 28 0a 20 20 50 43 61 63 68 65  eFetch(.  PCache
fb900 20 2a 70 43 61 63 68 65 2c 20 20 20 20 20 20 20   *pCache,       
fb910 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61  /* Obtain the pa
fb920 67 65 20 66 72 6f 6d 20 74 68 69 73 20 63 61 63  ge from this cac
fb930 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  he */.  Pgno pgn
fb940 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  o,            /*
fb950 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20   Page number to 
fb960 6f 62 74 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  obtain */.  int 
fb970 63 72 65 61 74 65 46 6c 61 67 2c 20 20 20 20 20  createFlag,     
fb980 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 63 72    /* If true, cr
fb990 65 61 74 65 20 70 61 67 65 20 69 66 20 69 74 20  eate page if it 
fb9a0 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 61  does not exist a
fb9b0 6c 72 65 61 64 79 20 2a 2f 0a 20 20 50 67 48 64  lready */.  PgHd
fb9c0 72 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 20  r **ppPage      
fb9d0 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70    /* Write the p
fb9e0 61 67 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  age here */.){. 
fb9f0 20 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20   PgHdr *pPage = 
fba00 30 3b 0a 20 20 69 6e 74 20 65 43 72 65 61 74 65  0;.  int eCreate
fba10 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61  ;..  assert( pCa
fba20 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  che!=0 );.  asse
fba30 72 74 28 20 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20  rt( pgno>0 );.. 
fba40 20 2f 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67   /* If the plugg
fba50 61 62 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69  able cache (sqli
fba60 74 65 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73  te3_pcache*) has
fba70 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61   not been alloca
fba80 74 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61  ted,.  ** alloca
fba90 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  te it now..  */.
fbaa0 20 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e 70    if( !pCache->p
fbab0 43 61 63 68 65 20 26 26 20 63 72 65 61 74 65 46  Cache && createF
fbac0 6c 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  lag ){.    sqlit
fbad0 65 33 5f 70 63 61 63 68 65 20 2a 70 3b 0a 20 20  e3_pcache *p;.  
fbae0 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20    int nByte;.   
fbaf0 20 6e 42 79 74 65 20 3d 20 70 43 61 63 68 65 2d   nByte = pCache-
fbb00 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 63 68 65  >szPage + pCache
fbb10 2d 3e 73 7a 45 78 74 72 61 20 2b 20 73 69 7a 65  ->szExtra + size
fbb20 6f 66 28 50 67 48 64 72 29 3b 0a 20 20 20 20 70  of(PgHdr);.    p
fbb30 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
fbb40 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43  Config.pcache.xC
fbb50 72 65 61 74 65 28 6e 42 79 74 65 2c 20 70 43 61  reate(nByte, pCa
fbb60 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29  che->bPurgeable)
fbb70 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a  ;.    if( !p ){.
fbb80 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
fbb90 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
fbba0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
fbbb0 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
fbbc0 78 43 61 63 68 65 73 69 7a 65 28 70 2c 20 70 43  xCachesize(p, pC
fbbd0 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20  ache->nMax);.   
fbbe0 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
fbbf0 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 43 72 65  = p;.  }..  eCre
fbc00 61 74 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67  ate = createFlag
fbc10 20 3f 20 31 20 3a 20 30 3b 0a 20 20 69 66 28 20   ? 1 : 0;.  if( 
fbc20 65 43 72 65 61 74 65 20 26 26 20 28 21 70 43 61  eCreate && (!pCa
fbc30 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20  che->bPurgeable 
fbc40 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 44 69 72  || !pCache->pDir
fbc50 74 79 29 20 29 7b 0a 20 20 20 20 65 43 72 65 61  ty) ){.    eCrea
fbc60 74 65 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66  te = 2;.  }.  if
fbc70 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  ( pCache->pCache
fbc80 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20   ){.    pPage = 
fbc90 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
fbca0 66 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63  fig.pcache.xFetc
fbcb0 68 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  h(pCache->pCache
fbcc0 2c 20 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29  , pgno, eCreate)
fbcd0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50  ;.  }..  if( !pP
fbce0 61 67 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d  age && eCreate==
fbcf0 31 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a  1 ){.    PgHdr *
fbd00 70 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e  pPg;..    /* Fin
fbd10 64 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74  d a dirty page t
fbd20 6f 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20  o write-out and 
fbd30 72 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74  recycle. First t
fbd40 72 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20  ry to find a .  
fbd50 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64    ** page that d
fbd60 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20  oes not require 
fbd70 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28  a journal-sync (
fbd80 6f 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e  one with PGHDR_N
fbd90 45 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20  EED_SYNC.    ** 
fbda0 63 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66  cleared), but if
fbdb0 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73   that is not pos
fbdc0 73 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72  sible settle for
fbdd0 20 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20   any other .    
fbde0 2a 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20  ** unreferenced 
fbdf0 64 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20  dirty page..    
fbe00 2a 2f 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65  */.    expensive
fbe10 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43  _assert( pcacheC
fbe20 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68  heckSynced(pCach
fbe30 65 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50  e) );.    for(pP
fbe40 67 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65  g=pCache->pSynce
fbe50 64 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 20  d; .        pPg 
fbe60 26 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c  && (pPg->nRef ||
fbe70 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48   (pPg->flags&PGH
fbe80 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20  DR_NEED_SYNC)); 
fbe90 0a 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67  .        pPg=pPg
fbea0 2d 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 20  ->pDirtyPrev.   
fbeb0 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 67   );.    if( !pPg
fbec0 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 50   ){.      for(pP
fbed0 67 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  g=pCache->pDirty
fbee0 54 61 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67  Tail; pPg && pPg
fbef0 2d 3e 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d  ->nRef; pPg=pPg-
fbf00 3e 70 44 69 72 74 79 50 72 65 76 29 3b 0a 20 20  >pDirtyPrev);.  
fbf10 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 67 20    }.    if( pPg 
fbf20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b  ){.      int rc;
fbf30 0a 20 20 20 20 20 20 72 63 20 3d 20 70 43 61 63  .      rc = pCac
fbf40 68 65 2d 3e 78 53 74 72 65 73 73 28 70 43 61 63  he->xStress(pCac
fbf50 68 65 2d 3e 70 53 74 72 65 73 73 2c 20 70 50 67  he->pStress, pPg
fbf60 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
fbf70 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63  =SQLITE_OK && rc
fbf80 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b  !=SQLITE_BUSY ){
fbf90 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
fbfa0 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
fbfb0 7d 0a 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73  }..    pPage = s
fbfc0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
fbfd0 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68  ig.pcache.xFetch
fbfe0 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
fbff0 20 70 67 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a   pgno, 2);.  }..
fc000 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20    if( pPage ){. 
fc010 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70     if( !pPage->p
fc020 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65  Data ){.      me
fc030 6d 73 65 74 28 70 50 61 67 65 2c 20 30 2c 20 73  mset(pPage, 0, s
fc040 69 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70  izeof(PgHdr) + p
fc050 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b  Cache->szExtra);
fc060 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 45  .      pPage->pE
fc070 78 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 70  xtra = (void*)&p
fc080 50 61 67 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70  Page[1];.      p
fc090 50 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76  Page->pData = (v
fc0a0 6f 69 64 20 2a 29 26 28 28 63 68 61 72 20 2a 29  oid *)&((char *)
fc0b0 70 50 61 67 65 29 5b 73 69 7a 65 6f 66 28 50 67  pPage)[sizeof(Pg
fc0c0 48 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73  Hdr) + pCache->s
fc0d0 7a 45 78 74 72 61 5d 3b 0a 20 20 20 20 20 20 70  zExtra];.      p
fc0e0 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70  Page->pCache = p
fc0f0 43 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50 61  Cache;.      pPa
fc100 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b  ge->pgno = pgno;
fc110 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
fc120 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65  t( pPage->pCache
fc130 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 20 20  ==pCache );.    
fc140 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
fc150 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20  gno==pgno );.   
fc160 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
fc170 70 45 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29  pExtra==(void *)
fc180 26 70 50 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20  &pPage[1] );..  
fc190 20 20 69 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e    if( 0==pPage->
fc1a0 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 70 43  nRef ){.      pC
fc1b0 61 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  ache->nRef++;.  
fc1c0 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e    }.    pPage->n
fc1d0 52 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70  Ref++;.    if( p
fc1e0 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  gno==1 ){.      
fc1f0 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d  pCache->pPage1 =
fc200 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20   pPage;.    }.  
fc210 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50  }.  *ppPage = pP
fc220 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  age;.  return (p
fc230 50 61 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61  Page==0 && eCrea
fc240 74 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d  te) ? SQLITE_NOM
fc250 45 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  EM : SQLITE_OK;.
fc260 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65  }../*.** Decreme
fc270 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  nt the reference
fc280 20 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65   count on a page
fc290 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73  . If the page is
fc2a0 20 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a   clean and the.*
fc2b0 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  * reference coun
fc2c0 74 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68  t drops to 0, th
fc2d0 65 6e 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c  en it is made el
fc2e0 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69  ible for recycli
fc2f0 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
fc300 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
fc310 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28  e3PcacheRelease(
fc320 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73  PgHdr *p){.  ass
fc330 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
fc340 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  ;.  p->nRef--;. 
fc350 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
fc360 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70  ){.    PCache *p
fc370 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68  Cache = p->pCach
fc380 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e  e;.    pCache->n
fc390 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28  Ref--;.    if( (
fc3a0 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  p->flags&PGHDR_D
fc3b0 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20  IRTY)==0 ){.    
fc3c0 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29    pcacheUnpin(p)
fc3d0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
fc3e0 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70     /* Move the p
fc3f0 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20  age to the head 
fc400 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73  of the dirty lis
fc410 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63  t. */.      pcac
fc420 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
fc430 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20  yList(p);.      
fc440 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79  pcacheAddToDirty
fc450 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20  List(p);.    }. 
fc460 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72   }.}../*.** Incr
fc470 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e  ease the referen
fc480 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75  ce count of a su
fc490 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31  pplied page by 1
fc4a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
fc4b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
fc4c0 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20  PcacheRef(PgHdr 
fc4d0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d  *p){.  assert(p-
fc4e0 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e  >nRef>0);.  p->n
fc4f0 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Ref++;.}../*.** 
fc500 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d  Drop a page from
fc510 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72   the cache. Ther
fc520 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c  e must be exactl
fc530 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20  y one reference 
fc540 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20  to the.** page. 
fc550 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65  This function de
fc560 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72  letes that refer
fc570 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69  ence, so after i
fc580 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  t returns the.**
fc590 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f   page pointed to
fc5a0 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64   by p is invalid
fc5b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
fc5c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
fc5d0 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72  PcacheDrop(PgHdr
fc5e0 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a   *p){.  PCache *
fc5f0 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74  pCache;.  assert
fc600 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a  ( p->nRef==1 );.
fc610 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50    if( p->flags&P
fc620 47 48 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20  GHDR_DIRTY ){.  
fc630 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72    pcacheRemoveFr
fc640 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a  omDirtyList(p);.
fc650 20 20 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70    }.  pCache = p
fc660 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63  ->pCache;.  pCac
fc670 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66  he->nRef--;.  if
fc680 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a  ( p->pgno==1 ){.
fc690 20 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67      pCache->pPag
fc6a0 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  e1 = 0;.  }.  sq
fc6b0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
fc6c0 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28  g.pcache.xUnpin(
fc6d0 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
fc6e0 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  p, 1);.}../*.** 
fc6f0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61  Make sure the pa
fc700 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  ge is marked as 
fc710 64 69 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e  dirty. If it isn
fc720 27 74 20 64 69 72 74 79 20 61 6c 72 65 61 64 79  't dirty already
fc730 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e  ,.** make it so.
fc740 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fc750 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
fc760 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50  cacheMakeDirty(P
fc770 67 48 64 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66  gHdr *p){.  p->f
fc780 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 44  lags &= ~PGHDR_D
fc790 4f 4e 54 5f 57 52 49 54 45 3b 0a 20 20 61 73 73  ONT_WRITE;.  ass
fc7a0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
fc7b0 3b 0a 20 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66  ;.  if( 0==(p->f
fc7c0 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52  lags & PGHDR_DIR
fc7d0 54 59 29 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c  TY) ){.    p->fl
fc7e0 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 49 52  ags |= PGHDR_DIR
fc7f0 54 59 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64  TY;.    pcacheAd
fc800 64 54 6f 44 69 72 74 79 4c 69 73 74 28 20 70 29  dToDirtyList( p)
fc810 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
fc820 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67  ake sure the pag
fc830 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63  e is marked as c
fc840 6c 65 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27  lean. If it isn'
fc850 74 20 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c  t clean already,
fc860 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a  .** make it so..
fc870 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
fc880 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
fc890 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67  acheMakeClean(Pg
fc8a0 48 64 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 28  Hdr *p){.  if( (
fc8b0 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52  p->flags & PGHDR
fc8c0 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70  _DIRTY) ){.    p
fc8d0 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44  cacheRemoveFromD
fc8e0 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20  irtyList(p);.   
fc8f0 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50   p->flags &= ~(P
fc900 47 48 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52  GHDR_DIRTY|PGHDR
fc910 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20  _NEED_SYNC);.   
fc920 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
fc930 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55  ){.      pcacheU
fc940 6e 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20  npin(p);.    }. 
fc950 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
fc960 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 74   every page in t
fc970 68 65 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a  he cache clean..
fc980 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
fc990 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
fc9a0 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61  acheCleanAll(PCa
fc9b0 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  che *pCache){.  
fc9c0 50 67 48 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c  PgHdr *p;.  whil
fc9d0 65 28 20 28 70 20 3d 20 70 43 61 63 68 65 2d 3e  e( (p = pCache->
fc9e0 70 44 69 72 74 79 29 21 3d 30 20 29 7b 0a 20 20  pDirty)!=0 ){.  
fc9f0 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
fca00 61 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d  akeClean(p);.  }
fca10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
fca20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  the PGHDR_NEED_S
fca30 59 4e 43 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c  YNC flag from al
fca40 6c 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a  l dirty pages..*
fca50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
fca60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
fca70 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67  cheClearSyncFlag
fca80 73 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  s(PCache *pCache
fca90 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20  ){.  PgHdr *p;. 
fcaa0 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70   for(p=pCache->p
fcab0 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70  Dirty; p; p=p->p
fcac0 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20  DirtyNext){.    
fcad0 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48  p->flags &= ~PGH
fcae0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
fcaf0 7d 0a 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e  }.  pCache->pSyn
fcb00 63 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44  ced = pCache->pD
fcb10 69 72 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a  irtyTail;.}../*.
fcb20 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61  ** Change the pa
fcb30 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ge number of pag
fcb40 65 20 70 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20  e p to newPgno. 
fcb50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fcb60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
fcb70 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 20  cacheMove(PgHdr 
fcb80 2a 70 2c 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f  *p, Pgno newPgno
fcb90 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  ){.  PCache *pCa
fcba0 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
fcbb0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
fcbc0 65 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ef>0 );.  assert
fcbd0 28 20 6e 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20  ( newPgno>0 );. 
fcbe0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
fcbf0 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 52 65 6b  nfig.pcache.xRek
fcc00 65 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  ey(pCache->pCach
fcc10 65 2c 20 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e  e, p, p->pgno, n
fcc20 65 77 50 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67  ewPgno);.  p->pg
fcc30 6e 6f 20 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20  no = newPgno;.  
fcc40 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47  if( (p->flags&PG
fcc50 48 44 52 5f 44 49 52 54 59 29 20 26 26 20 28 70  HDR_DIRTY) && (p
fcc60 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
fcc70 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20  ED_SYNC) ){.    
fcc80 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d  pcacheRemoveFrom
fcc90 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20  DirtyList(p);.  
fcca0 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72    pcacheAddToDir
fccb0 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d  tyList(p);.  }.}
fccc0 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65  ../*.** Drop eve
fccd0 72 79 20 63 61 63 68 65 20 65 6e 74 72 79 20 77  ry cache entry w
fcce0 68 6f 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72  hose page number
fccf0 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
fcd00 20 22 70 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20   "pgno". The.** 
fcd10 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75  caller must ensu
fcd20 72 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72  re that there ar
fcd30 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67  e no outstanding
fcd40 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61   references to a
fcd50 6e 79 20 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65  ny pages.** othe
fcd60 72 20 74 68 61 6e 20 70 61 67 65 20 31 20 77 69  r than page 1 wi
fcd70 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  th a page number
fcd80 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 67   greater than pg
fcd90 6e 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  no..**.** If the
fcda0 72 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63  re is a referenc
fcdb0 65 20 74 6f 20 70 61 67 65 20 31 20 61 6e 64 20  e to page 1 and 
fcdc0 74 68 65 20 70 67 6e 6f 20 70 61 72 61 6d 65 74  the pgno paramet
fcdd0 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  er passed to thi
fcde0 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  s.** function is
fcdf0 20 30 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74   0, then the dat
fce00 61 20 61 72 65 61 20 61 73 73 6f 63 69 61 74 65  a area associate
fce10 64 20 77 69 74 68 20 70 61 67 65 20 31 20 69 73  d with page 1 is
fce20 20 7a 65 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20   zeroed, but.** 
fce30 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20  the page object 
fce40 69 73 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a  is not dropped..
fce50 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
fce60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
fce70 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 61  acheTruncate(PCa
fce80 63 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e  che *pCache, Pgn
fce90 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70  o pgno){.  if( p
fcea0 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b  Cache->pCache ){
fceb0 0a 20 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20  .    PgHdr *p;. 
fcec0 20 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b     PgHdr *pNext;
fced0 0a 20 20 20 20 66 6f 72 28 70 3d 70 43 61 63 68  .    for(p=pCach
fcee0 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d  e->pDirty; p; p=
fcef0 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e  pNext){.      pN
fcf00 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e  ext = p->pDirtyN
fcf10 65 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ext;.      if( p
fcf20 2d 3e 70 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20  ->pgno>pgno ){. 
fcf30 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
fcf40 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
fcf50 52 54 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73  RTY );.        s
fcf60 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
fcf70 43 6c 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20  Clean(p);.      
fcf80 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
fcf90 70 67 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68  pgno==0 && pCach
fcfa0 65 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20  e->pPage1 ){.   
fcfb0 20 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65     memset(pCache
fcfc0 2d 3e 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c  ->pPage1->pData,
fcfd0 20 30 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61   0, pCache->szPa
fcfe0 67 65 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20  ge);.      pgno 
fcff0 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 1;.    }.    s
fd000 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
fd010 69 67 2e 70 63 61 63 68 65 2e 78 54 72 75 6e 63  ig.pcache.xTrunc
fd020 61 74 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63  ate(pCache->pCac
fd030 68 65 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d  he, pgno+1);.  }
fd040 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
fd050 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49  a cache..*/.SQLI
fd060 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
fd070 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f  sqlite3PcacheClo
fd080 73 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68  se(PCache *pCach
fd090 65 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65  e){.  if( pCache
fd0a0 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20  ->pCache ){.    
fd0b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
fd0c0 66 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74  fig.pcache.xDest
fd0d0 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63  roy(pCache->pCac
fd0e0 68 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a  he);.  }.}../* .
fd0f0 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63  ** Discard the c
fd100 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
fd110 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ache..*/.SQLITE_
fd120 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
fd130 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28  ite3PcacheClear(
fd140 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
fd150 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
fd160 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c  Truncate(pCache,
fd170 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65   0);.}../*.** Me
fd180 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66  rge two lists of
fd190 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64   pages connected
fd1a0 20 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69   by pDirty and i
fd1b0 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a  n pgno order..**
fd1c0 20 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78   Do not both fix
fd1d0 69 6e 67 20 74 68 65 20 70 44 69 72 74 79 50 72  ing the pDirtyPr
fd1e0 65 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a  ev pointers..*/.
fd1f0 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63  static PgHdr *pc
fd200 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69  acheMergeDirtyLi
fd210 73 74 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67  st(PgHdr *pA, Pg
fd220 48 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64  Hdr *pB){.  PgHd
fd230 72 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c  r result, *pTail
fd240 3b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73  ;.  pTail = &res
fd250 75 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41  ult;.  while( pA
fd260 20 26 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66   && pB ){.    if
fd270 28 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70  ( pA->pgno<pB->p
fd280 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61  gno ){.      pTa
fd290 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b  il->pDirty = pA;
fd2a0 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70  .      pTail = p
fd2b0 41 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41  A;.      pA = pA
fd2c0 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65  ->pDirty;.    }e
fd2d0 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c  lse{.      pTail
fd2e0 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20  ->pDirty = pB;. 
fd2f0 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b       pTail = pB;
fd300 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e  .      pB = pB->
fd310 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20  pDirty;.    }.  
fd320 7d 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20  }.  if( pA ){.  
fd330 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20    pTail->pDirty 
fd340 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66  = pA;.  }else if
fd350 28 20 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69  ( pB ){.    pTai
fd360 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a  l->pDirty = pB;.
fd370 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61    }else{.    pTa
fd380 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a  il->pDirty = 0;.
fd390 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73    }.  return res
fd3a0 75 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f  ult.pDirty;.}../
fd3b0 2a 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69  *.** Sort the li
fd3c0 73 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61  st of pages in a
fd3d0 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  ccending order b
fd3e0 79 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61  y pgno.  Pages a
fd3f0 72 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20  re.** connected 
fd400 62 79 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65  by pDirty pointe
fd410 72 73 2e 20 20 54 68 65 20 70 44 69 72 74 79 50  rs.  The pDirtyP
fd420 72 65 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65  rev pointers are
fd430 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79  .** corrupted by
fd440 20 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23   this sort..*/.#
fd450 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55  define N_SORT_BU
fd460 43 4b 45 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64  CKET_ALLOC 25.#d
fd470 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43  efine N_SORT_BUC
fd480 4b 45 54 20 20 20 20 20 20 20 32 35 0a 23 69 66  KET       25.#if
fd490 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
fd4a0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61    int sqlite3_pa
fd4b0 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65  ger_n_sort_bucke
fd4c0 74 20 3d 20 30 3b 0a 20 20 23 75 6e 64 65 66 20  t = 0;.  #undef 
fd4d0 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20  N_SORT_BUCKET.  
fd4e0 23 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42  #define N_SORT_B
fd4f0 55 43 4b 45 54 20 5c 0a 20 20 20 28 73 71 6c 69  UCKET \.   (sqli
fd500 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74  te3_pager_n_sort
fd510 5f 62 75 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f  _bucket?sqlite3_
fd520 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63  pager_n_sort_buc
fd530 6b 65 74 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45  ket:N_SORT_BUCKE
fd540 54 5f 41 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a  T_ALLOC).#endif.
fd550 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63  static PgHdr *pc
fd560 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73  acheSortDirtyLis
fd570 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20  t(PgHdr *pIn){. 
fd580 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54   PgHdr *a[N_SORT
fd590 5f 42 55 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20  _BUCKET_ALLOC], 
fd5a0 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d  *p;.  int i;.  m
fd5b0 65 6d 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65  emset(a, 0, size
fd5c0 6f 66 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28  of(a));.  while(
fd5d0 20 70 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20   pIn ){.    p = 
fd5e0 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70  pIn;.    pIn = p
fd5f0 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d  ->pDirty;.    p-
fd600 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20  >pDirty = 0;.   
fd610 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f   for(i=0; i<N_SO
fd620 52 54 5f 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b  RT_BUCKET-1; i++
fd630 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69  ){.      if( a[i
fd640 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==0 ){.        
fd650 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20  a[i] = p;.      
fd660 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
fd670 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20  else{.        p 
fd680 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72  = pcacheMergeDir
fd690 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b  tyList(a[i], p);
fd6a0 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20  .        a[i] = 
fd6b0 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
fd6c0 0a 20 20 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f  .    if( i==N_SO
fd6d0 52 54 5f 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20  RT_BUCKET-1 ){. 
fd6e0 20 20 20 20 20 2f 2a 20 43 6f 76 65 72 61 67 65       /* Coverage
fd6f0 3a 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74  : To get here, t
fd700 68 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20  here need to be 
fd710 32 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  2^(N_SORT_BUCKET
fd720 29 20 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d  ) .      ** elem
fd730 65 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 70 75  ents in the inpu
fd740 74 20 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20  t list. This is 
fd750 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20 69 6d  possible, but im
fd760 70 72 61 63 74 69 63 61 6c 2e 0a 20 20 20 20 20  practical..     
fd770 20 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 69 73   ** Testing this
fd780 20 6c 69 6e 65 20 69 73 20 74 68 65 20 70 6f 69   line is the poi
fd790 6e 74 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72  nt of global var
fd7a0 69 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73  iable.      ** s
fd7b0 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73  qlite3_pager_n_s
fd7c0 6f 72 74 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20  ort_bucket..    
fd7d0 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20    */.      a[i] 
fd7e0 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72  = pcacheMergeDir
fd7f0 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b  tyList(a[i], p);
fd800 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d  .    }.  }.  p =
fd810 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31   a[0];.  for(i=1
fd820 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45  ; i<N_SORT_BUCKE
fd830 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d  T; i++){.    p =
fd840 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74   pcacheMergeDirt
fd850 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a  yList(p, a[i]);.
fd860 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
fd870 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
fd880 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69  a list of all di
fd890 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65  rty pages in the
fd8a0 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62   cache, sorted b
fd8b0 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a  y page number..*
fd8c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
fd8d0 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50   PgHdr *sqlite3P
fd8e0 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 50  cacheDirtyList(P
fd8f0 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
fd900 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f    PgHdr *p;.  fo
fd910 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72  r(p=pCache->pDir
fd920 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72  ty; p; p=p->pDir
fd930 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e  tyNext){.    p->
fd940 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69 72  pDirty = p->pDir
fd950 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65  tyNext;.  }.  re
fd960 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74 44  turn pcacheSortD
fd970 69 72 74 79 4c 69 73 74 28 70 43 61 63 68 65 2d  irtyList(pCache-
fd980 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20  >pDirty);.}../* 
fd990 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74  .** Return the t
fd9a0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72  otal number of r
fd9b0 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20  eferenced pages 
fd9c0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 63 68  held by the cach
fd9d0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
fd9e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
fd9f0 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50  PcacheRefCount(P
fda00 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
fda10 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d    return pCache-
fda20 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  >nRef;.}../*.** 
fda30 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
fda40 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
fda50 74 6f 20 74 68 65 20 70 61 67 65 20 73 75 70 70  to the page supp
fda60 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d  lied as an argum
fda70 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
fda80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
fda90 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63  e3PcachePageRefc
fdaa0 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b 0a  ount(PgHdr *p){.
fdab0 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66    return p->nRef
fdac0 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75  ;.}../* .** Retu
fdad0 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  rn the total num
fdae0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
fdaf0 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51  the cache..*/.SQ
fdb00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
fdb10 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61   sqlite3PcachePa
fdb20 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20 2a  gecount(PCache *
fdb30 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e  pCache){.  int n
fdb40 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28 20  Page = 0;.  if( 
fdb50 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29  pCache->pCache )
fdb60 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73 71  {.    nPage = sq
fdb70 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
fdb80 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63 6f  g.pcache.xPageco
fdb90 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61 63  unt(pCache->pCac
fdba0 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  he);.  }.  retur
fdbb0 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 64  n nPage;.}..#ifd
fdbc0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f  ef SQLITE_TEST./
fdbd0 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75 67  *.** Get the sug
fdbe0 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a  gested cache-siz
fdbf0 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49  e value..*/.SQLI
fdc00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
fdc10 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 43  qlite3PcacheGetC
fdc20 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20  achesize(PCache 
fdc30 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 75  *pCache){.  retu
fdc40 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b  rn pCache->nMax;
fdc50 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
fdc60 20 53 65 74 20 74 68 65 20 73 75 67 67 65 73 74   Set the suggest
fdc70 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61  ed cache-size va
fdc80 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  lue..*/.SQLITE_P
fdc90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
fdca0 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63 68  te3PcacheSetCach
fdcb0 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43  esize(PCache *pC
fdcc0 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67 65  ache, int mxPage
fdcd0 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61  ){.  pCache->nMa
fdce0 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69 66  x = mxPage;.  if
fdcf0 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  ( pCache->pCache
fdd00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   ){.    sqlite3G
fdd10 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
fdd20 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 43  he.xCachesize(pC
fdd30 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d 78  ache->pCache, mx
fdd40 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  Page);.  }.}..#i
fdd50 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
fdd60 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 46 6f  K_PAGES./*.** Fo
fdd70 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  r all dirty page
fdd80 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74  s currently in t
fdd90 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65  he cache, invoke
fdda0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
fddb0 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73  * callback. This
fddc0 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66   is only used if
fddd0 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43   the SQLITE_CHEC
fdde0 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73  K_PAGES macro is
fddf0 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  .** defined..*/.
fde00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
fde10 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
fde20 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43  eIterateDirty(PC
fde30 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f  ache *pCache, vo
fde40 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64  id (*xIter)(PgHd
fde50 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a  r *)){.  PgHdr *
fde60 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44  pDirty;.  for(pD
fde70 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69  irty=pCache->pDi
fde80 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69  rty; pDirty; pDi
fde90 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72  rty=pDirty->pDir
fdea0 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74  tyNext){.    xIt
fdeb0 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a  er(pDirty);.  }.
fdec0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
fded0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
fdee0 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a   pcache.c ******
fdef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdf00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdf10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
fdf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
fdf30 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a  file pcache1.c *
fdf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdf50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdf60 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
fdf70 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35  2008 November 05
fdf80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
fdf90 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
fdfa0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
fdfb0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
fdfc0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
fdfd0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
fdfe0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
fdff0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
fe000 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
fe010 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
fe020 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
fe030 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
fe040 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
fe050 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
fe060 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
fe070 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
fe080 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
fe090 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
fe0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fe0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fe0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fe0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
fe0e0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
fe0f0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64  implements the d
fe100 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68  efault page cach
fe110 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
fe120 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33   (the.** sqlite3
fe130 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63  _pcache interfac
fe140 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74  e). It also cont
fe150 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65  ains part of the
fe160 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
fe170 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ** of the SQLITE
fe180 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
fe190 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  E and sqlite3_re
fe1a0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66  lease_memory() f
fe1b0 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74  eatures..** If t
fe1c0 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20  he default page 
fe1d0 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  cache implementa
fe1e0 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65  tion is override
fe1f0 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20  n, then neither 
fe200 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20  of.** these two 
fe210 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61  features are ava
fe220 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28  ilable..**.** @(
fe230 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e  #) $Id: pcache1.
fe240 63 2c 76 20 31 2e 31 30 20 32 30 30 39 2f 30 33  c,v 1.10 2009/03
fe250 2f 32 33 20 30 34 3a 33 33 3a 33 33 20 64 61 6e  /23 04:33:33 dan
fe260 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
fe270 2f 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  /...typedef stru
fe280 63 74 20 50 43 61 63 68 65 31 20 50 43 61 63 68  ct PCache1 PCach
fe290 65 31 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  e1;.typedef stru
fe2a0 63 74 20 50 67 48 64 72 31 20 50 67 48 64 72 31  ct PgHdr1 PgHdr1
fe2b0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
fe2c0 20 50 67 46 72 65 65 73 6c 6f 74 20 50 67 46 72   PgFreeslot PgFr
fe2d0 65 65 73 6c 6f 74 3b 0a 0a 2f 2a 20 50 6f 69 6e  eeslot;../* Poin
fe2e0 74 65 72 73 20 74 6f 20 73 74 72 75 63 74 75 72  ters to structur
fe2f0 65 73 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  es of this type 
fe300 61 72 65 20 63 61 73 74 20 61 6e 64 20 72 65 74  are cast and ret
fe310 75 72 6e 65 64 20 61 73 20 0a 2a 2a 20 6f 70 61  urned as .** opa
fe320 71 75 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  que sqlite3_pcac
fe330 68 65 2a 20 68 61 6e 64 6c 65 73 0a 2a 2f 0a 73  he* handles.*/.s
fe340 74 72 75 63 74 20 50 43 61 63 68 65 31 20 7b 0a  truct PCache1 {.
fe350 20 20 2f 2a 20 43 61 63 68 65 20 63 6f 6e 66 69    /* Cache confi
fe360 67 75 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74  guration paramet
fe370 65 72 73 2e 20 50 61 67 65 20 73 69 7a 65 20 28  ers. Page size (
fe380 73 7a 50 61 67 65 29 20 61 6e 64 20 74 68 65 20  szPage) and the 
fe390 70 75 72 67 65 61 62 6c 65 0a 20 20 2a 2a 20 66  purgeable.  ** f
fe3a0 6c 61 67 20 28 62 50 75 72 67 65 61 62 6c 65 29  lag (bPurgeable)
fe3b0 20 61 72 65 20 73 65 74 20 77 68 65 6e 20 74 68   are set when th
fe3c0 65 20 63 61 63 68 65 20 69 73 20 63 72 65 61 74  e cache is creat
fe3d0 65 64 2e 20 6e 4d 61 78 20 6d 61 79 20 62 65 20  ed. nMax may be 
fe3e0 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61  .  ** modified a
fe3f0 74 20 61 6e 79 20 74 69 6d 65 20 62 79 20 61 20  t any time by a 
fe400 63 61 6c 6c 20 74 6f 20 74 68 65 20 70 63 61 63  call to the pcac
fe410 68 65 31 43 61 63 68 65 53 69 7a 65 28 29 20 6d  he1CacheSize() m
fe420 65 74 68 6f 64 2e 0a 20 20 2a 2a 20 54 68 65 20  ethod..  ** The 
fe430 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73  global mutex mus
fe440 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 61  t be held when a
fe450 63 63 65 73 73 69 6e 67 20 6e 4d 61 78 2e 0a 20  ccessing nMax.. 
fe460 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65   */.  int szPage
fe470 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
fe480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
fe490 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 70  e of allocated p
fe4a0 61 67 65 73 20 69 6e 20 62 79 74 65 73 20 2a 2f  ages in bytes */
fe4b0 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c  .  int bPurgeabl
fe4c0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
fe4d0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
fe4e0 66 20 63 61 63 68 65 20 69 73 20 70 75 72 67 65  f cache is purge
fe4f0 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  able */.  unsign
fe500 65 64 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 20  ed int nMin;    
fe510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
fe520 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20   Minimum number 
fe530 6f 66 20 70 61 67 65 73 20 72 65 73 65 72 76 65  of pages reserve
fe540 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  d */.  unsigned 
fe550 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 20 20  int nMax;       
fe560 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
fe570 6e 66 69 67 75 72 65 64 20 22 63 61 63 68 65 5f  nfigured "cache_
fe580 73 69 7a 65 22 20 76 61 6c 75 65 20 2a 2f 0a 0a  size" value */..
fe590 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20    /* Hash table 
fe5a0 6f 66 20 61 6c 6c 20 70 61 67 65 73 2e 20 54 68  of all pages. Th
fe5b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
fe5c0 61 62 6c 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62  ables may only b
fe5d0 65 20 61 63 63 65 73 73 65 64 0a 20 20 2a 2a 20  e accessed.  ** 
fe5e0 77 68 65 6e 20 74 68 65 20 61 63 63 65 73 73 6f  when the accesso
fe5f0 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65  r is holding the
fe600 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 28 73   global mutex (s
fe610 65 65 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d  ee pcache1EnterM
fe620 75 74 65 78 28 29 20 0a 20 20 2a 2a 20 61 6e 64  utex() .  ** and
fe630 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
fe640 65 78 28 29 29 2e 0a 20 20 2a 2f 0a 20 20 75 6e  ex())..  */.  un
fe650 73 69 67 6e 65 64 20 69 6e 74 20 6e 52 65 63 79  signed int nRecy
fe660 63 6c 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  clable;         
fe670 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
fe680 61 67 65 73 20 69 6e 20 74 68 65 20 4c 52 55 20  ages in the LRU 
fe690 6c 69 73 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  list */.  unsign
fe6a0 65 64 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20  ed int nPage;   
fe6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
fe6c0 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
fe6d0 20 70 61 67 65 73 20 69 6e 20 61 70 48 61 73 68   pages in apHash
fe6e0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
fe6f0 6e 74 20 6e 48 61 73 68 3b 20 20 20 20 20 20 20  nt nHash;       
fe700 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
fe710 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20  ber of slots in 
fe720 61 70 48 61 73 68 5b 5d 20 2a 2f 0a 20 20 50 67  apHash[] */.  Pg
fe730 48 64 72 31 20 2a 2a 61 70 48 61 73 68 3b 20 20  Hdr1 **apHash;  
fe740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe750 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20    /* Hash table 
fe760 66 6f 72 20 66 61 73 74 20 6c 6f 6f 6b 75 70 20  for fast lookup 
fe770 62 79 20 6b 65 79 20 2a 2f 0a 0a 20 20 75 6e 73  by key */..  uns
fe780 69 67 6e 65 64 20 69 6e 74 20 69 4d 61 78 4b 65  igned int iMaxKe
fe790 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
fe7a0 20 2f 2a 20 4c 61 72 67 65 73 74 20 6b 65 79 20   /* Largest key 
fe7b0 73 65 65 6e 20 73 69 6e 63 65 20 78 54 72 75 6e  seen since xTrun
fe7c0 63 61 74 65 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  cate() */.};../*
fe7d0 0a 2a 2a 20 45 61 63 68 20 63 61 63 68 65 20 65  .** Each cache e
fe7e0 6e 74 72 79 20 69 73 20 72 65 70 72 65 73 65 6e  ntry is represen
fe7f0 74 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e  ted by an instan
fe800 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
fe810 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  ing .** structur
fe820 65 2e 20 41 20 62 75 66 66 65 72 20 6f 66 20 50  e. A buffer of P
fe830 67 48 64 72 31 2e 70 43 61 63 68 65 2d 3e 73 7a  gHdr1.pCache->sz
fe840 50 61 67 65 20 62 79 74 65 73 20 69 73 20 61 6c  Page bytes is al
fe850 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 64 69 72 65  located .** dire
fe860 63 74 6c 79 20 61 66 74 65 72 20 74 68 65 20 73  ctly after the s
fe870 74 72 75 63 74 75 72 65 20 69 6e 20 6d 65 6d 6f  tructure in memo
fe880 72 79 20 28 73 65 65 20 74 68 65 20 50 47 48 44  ry (see the PGHD
fe890 52 31 5f 54 4f 5f 50 41 47 45 28 29 20 0a 2a 2a  R1_TO_PAGE() .**
fe8a0 20 6d 61 63 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a   macro below)..*
fe8b0 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 31 20  /.struct PgHdr1 
fe8c0 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
fe8d0 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20   iKey;          
fe8e0 20 20 20 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20     /* Key value 
fe8f0 28 70 61 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f  (page number) */
fe900 0a 20 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74  .  PgHdr1 *pNext
fe910 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
fe920 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73    /* Next in has
fe930 68 20 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f  h table chain */
fe940 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63  .  PCache1 *pCac
fe950 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  he;             
fe960 20 20 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20    /* Cache that 
fe970 63 75 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74  currently owns t
fe980 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 67  his page */.  Pg
fe990 48 64 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20  Hdr1 *pLruNext; 
fe9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fe9b0 4e 65 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74  Next in LRU list
fe9c0 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67   of unpinned pag
fe9d0 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a  es */.  PgHdr1 *
fe9e0 70 4c 72 75 50 72 65 76 3b 20 20 20 20 20 20 20  pLruPrev;       
fe9f0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f         /* Previo
fea00 75 73 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f  us in LRU list o
fea10 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  f unpinned pages
fea20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72   */.};../*.** Fr
fea30 65 65 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20  ee slots in the 
fea40 61 6c 6c 6f 63 61 74 6f 72 20 75 73 65 64 20 74  allocator used t
fea50 6f 20 64 69 76 69 64 65 20 75 70 20 74 68 65 20  o divide up the 
fea60 62 75 66 66 65 72 20 70 72 6f 76 69 64 65 64 20  buffer provided 
fea70 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c  using.** the SQL
fea80 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
fea90 41 43 48 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a  ACHE mechanism..
feaa0 2a 2f 0a 73 74 72 75 63 74 20 50 67 46 72 65 65  */.struct PgFree
feab0 73 6c 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73  slot {.  PgFrees
feac0 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20  lot *pNext;  /* 
fead0 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a  Next free slot *
feae0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62  /.};../*.** Glob
feaf0 61 6c 20 64 61 74 61 20 75 73 65 64 20 62 79 20  al data used by 
feb00 74 68 69 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73  this cache..*/.s
feb10 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
feb20 20 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c   struct PCacheGl
feb30 6f 62 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33  obal {.  sqlite3
feb40 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20  _mutex *mutex;  
feb50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
feb60 73 74 61 74 69 63 20 6d 75 74 65 78 20 4d 55 54  static mutex MUT
feb70 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 2a 2f  EX_STATIC_LRU */
feb80 0a 0a 20 20 69 6e 74 20 6e 4d 61 78 50 61 67 65  ..  int nMaxPage
feb90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
feba0 20 20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f          /* Sum o
febb0 66 20 6e 4d 61 78 50 61 67 65 20 66 6f 72 20 70  f nMaxPage for p
febc0 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20  urgeable caches 
febd0 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e 50 61 67  */.  int nMinPag
febe0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
febf0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20           /* Sum 
fec00 6f 66 20 6e 4d 69 6e 50 61 67 65 20 66 6f 72 20  of nMinPage for 
fec10 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73  purgeable caches
fec20 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65   */.  int nCurre
fec30 6e 74 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  ntPage;         
fec40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
fec50 62 65 72 20 6f 66 20 70 75 72 67 65 61 62 6c 65  ber of purgeable
fec60 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64   pages allocated
fec70 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c   */.  PgHdr1 *pL
fec80 72 75 48 65 61 64 2c 20 2a 70 4c 72 75 54 61 69  ruHead, *pLruTai
fec90 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 52 55  l;        /* LRU
feca0 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65   list of unpinne
fecb0 64 20 70 61 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a  d pages */..  /*
fecc0 20 56 61 72 69 61 62 6c 65 73 20 72 65 6c 61 74   Variables relat
fecd0 65 64 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e  ed to SQLITE_CON
fece0 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 73 65  FIG_PAGECACHE se
fecf0 74 74 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 6e 74  ttings. */.  int
fed00 20 73 7a 53 6c 6f 74 3b 20 20 20 20 20 20 20 20   szSlot;        
fed10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fed20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68   /* Size of each
fed30 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20   free slot */.  
fed40 76 6f 69 64 20 2a 70 53 74 61 72 74 2c 20 2a 70  void *pStart, *p
fed50 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  End;            
fed60 20 20 20 20 2f 2a 20 42 6f 75 6e 64 73 20 6f 66      /* Bounds of
fed70 20 70 61 67 65 63 61 63 68 65 20 6d 61 6c 6c 6f   pagecache mallo
fed80 63 20 72 61 6e 67 65 20 2a 2f 0a 20 20 50 67 46  c range */.  PgF
fed90 72 65 65 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20  reeslot *pFree; 
feda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fedb0 20 2f 2a 20 46 72 65 65 20 70 61 67 65 20 62 6c   /* Free page bl
fedc0 6f 63 6b 73 20 2a 2f 0a 7d 20 70 63 61 63 68 65  ocks */.} pcache
fedd0 31 5f 67 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20  1_g;../*.** All 
fede0 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
fedf0 65 20 73 68 6f 75 6c 64 20 61 63 63 65 73 73 20  e should access 
fee00 74 68 65 20 67 6c 6f 62 61 6c 20 73 74 72 75 63  the global struc
fee10 74 75 72 65 20 61 62 6f 76 65 20 76 69 61 20 74  ture above via t
fee20 68 65 0a 2a 2a 20 61 6c 69 61 73 20 22 70 63 61  he.** alias "pca
fee30 63 68 65 31 22 2e 20 54 68 69 73 20 65 6e 73 75  che1". This ensu
fee40 72 65 73 20 74 68 61 74 20 74 68 65 20 57 53 44  res that the WSD
fee50 20 65 6d 75 6c 61 74 69 6f 6e 20 69 73 20 75 73   emulation is us
fee60 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69  ed when.** compi
fee70 6c 69 6e 67 20 66 6f 72 20 73 79 73 74 65 6d 73  ling for systems
fee80 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 73 75 70   that do not sup
fee90 70 6f 72 74 20 72 65 61 6c 20 57 53 44 2e 0a 2a  port real WSD..*
feea0 2f 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68 65  /.#define pcache
feeb0 31 20 28 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  1 (GLOBAL(struct
feec0 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70   PCacheGlobal, p
feed0 63 61 63 68 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a  cache1_g))../*.*
feee0 2a 20 57 68 65 6e 20 61 20 50 67 48 64 72 31 20  * When a PgHdr1 
feef0 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c  structure is all
fef00 6f 63 61 74 65 64 2c 20 74 68 65 20 61 73 73 6f  ocated, the asso
fef10 63 69 61 74 65 64 20 50 43 61 63 68 65 31 2e 73  ciated PCache1.s
fef20 7a 50 61 67 65 0a 2a 2a 20 62 79 74 65 73 20 6f  zPage.** bytes o
fef30 66 20 64 61 74 61 20 61 72 65 20 6c 6f 63 61 74  f data are locat
fef40 65 64 20 64 69 72 65 63 74 6c 79 20 61 66 74 65  ed directly afte
fef50 72 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28  r it in memory (
fef60 69 2e 65 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a  i.e. the total.*
fef70 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c  * size of the al
fef80 6c 6f 63 61 74 69 6f 6e 20 69 73 20 73 69 7a 65  location is size
fef90 6f 66 28 50 67 48 64 72 31 29 2b 50 43 61 63 68  of(PgHdr1)+PCach
fefa0 65 31 2e 73 7a 50 61 67 65 20 62 79 74 65 29 2e  e1.szPage byte).
fefb0 20 54 68 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54   The.** PGHDR1_T
fefc0 4f 5f 50 41 47 45 28 29 20 6d 61 63 72 6f 20 74  O_PAGE() macro t
fefd0 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  akes a pointer t
fefe0 6f 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63  o a PgHdr1 struc
feff0 74 75 72 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72  ture as.** an ar
ff000 67 75 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72  gument and retur
ff010 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
ff020 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 62  the associated b
ff030 6c 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a  lock of szPage.*
ff040 2a 20 62 79 74 65 73 2e 20 54 68 65 20 50 41 47  * bytes. The PAG
ff050 45 5f 54 4f 5f 50 47 48 44 52 31 28 29 20 6d 61  E_TO_PGHDR1() ma
ff060 63 72 6f 20 64 6f 65 73 20 74 68 65 20 6f 70 70  cro does the opp
ff070 6f 73 69 74 65 3a 20 69 74 73 20 61 72 67 75 6d  osite: its argum
ff080 65 6e 74 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e  ent is.** a poin
ff090 74 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f  ter to a block o
ff0a0 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 20 6f  f szPage bytes o
ff0b0 66 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 72  f data and the r
ff0c0 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a  eturn value is.*
ff0d0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  * a pointer to t
ff0e0 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 50 67  he associated Pg
ff0f0 48 64 72 31 20 73 74 72 75 63 74 75 72 65 2e 0a  Hdr1 structure..
ff100 2a 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20  **.**   assert( 
ff110 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 50  PGHDR1_TO_PAGE(P
ff120 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 58 29  AGE_TO_PGHDR1(X)
ff130 29 3d 3d 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69  )==X );.*/.#defi
ff140 6e 65 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47  ne PGHDR1_TO_PAG
ff150 45 28 70 29 20 28 76 6f 69 64 20 2a 29 28 26 28  E(p) (void *)(&(
ff160 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
ff170 29 70 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72  )p)[sizeof(PgHdr
ff180 31 29 5d 29 0a 23 64 65 66 69 6e 65 20 50 41 47  1)]).#define PAG
ff190 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 29 20 28  E_TO_PGHDR1(p) (
ff1a0 50 67 48 64 72 31 20 2a 29 28 26 28 28 75 6e 73  PgHdr1 *)(&((uns
ff1b0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 5b  igned char *)p)[
ff1c0 2d 31 2a 28 69 6e 74 29 73 69 7a 65 6f 66 28 50  -1*(int)sizeof(P
ff1d0 67 48 64 72 31 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20  gHdr1)])../*.** 
ff1e0 4d 61 63 72 6f 73 20 74 6f 20 65 6e 74 65 72 20  Macros to enter 
ff1f0 61 6e 64 20 6c 65 61 76 65 20 74 68 65 20 67 6c  and leave the gl
ff200 6f 62 61 6c 20 4c 52 55 20 6d 75 74 65 78 2e 0a  obal LRU mutex..
ff210 2a 2f 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68  */.#define pcach
ff220 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 20 73  e1EnterMutex() s
ff230 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
ff240 65 72 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78  er(pcache1.mutex
ff250 29 0a 23 64 65 66 69 6e 65 20 70 63 61 63 68 65  ).#define pcache
ff260 31 4c 65 61 76 65 4d 75 74 65 78 28 29 20 73 71  1LeaveMutex() sq
ff270 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
ff280 65 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29  e(pcache1.mutex)
ff290 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
ff2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ff2e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 50 61 67  */./******** Pag
ff2f0 65 20 41 6c 6c 6f 63 61 74 69 6f 6e 2f 53 51 4c  e Allocation/SQL
ff300 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
ff310 45 20 52 65 6c 61 74 65 64 20 46 75 6e 63 74 69  E Related Functi
ff320 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ons ************
ff330 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  **/../*.** This 
ff340 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
ff350 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61  ed during initia
ff360 6c 69 7a 61 74 69 6f 6e 20 69 66 20 61 20 73 74  lization if a st
ff370 61 74 69 63 20 62 75 66 66 65 72 20 69 73 20 0a  atic buffer is .
ff380 2a 2a 20 73 75 70 70 6c 69 65 64 20 74 6f 20 75  ** supplied to u
ff390 73 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d  se for the page-
ff3a0 63 61 63 68 65 20 62 79 20 70 61 73 73 69 6e 67  cache by passing
ff3b0 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46   the SQLITE_CONF
ff3c0 49 47 5f 50 41 47 45 43 41 43 48 45 0a 2a 2a 20  IG_PAGECACHE.** 
ff3d0 76 65 72 62 20 74 6f 20 73 71 6c 69 74 65 33 5f  verb to sqlite3_
ff3e0 63 6f 6e 66 69 67 28 29 2e 20 50 61 72 61 6d 65  config(). Parame
ff3f0 74 65 72 20 70 42 75 66 20 70 6f 69 6e 74 73 20  ter pBuf points 
ff400 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  to an allocation
ff410 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68   large.** enough
ff420 20 74 6f 20 63 6f 6e 74 61 69 6e 20 27 6e 27 20   to contain 'n' 
ff430 62 75 66 66 65 72 73 20 6f 66 20 27 73 7a 27 20  buffers of 'sz' 
ff440 62 79 74 65 73 20 65 61 63 68 2e 0a 2a 2f 0a 53  bytes each..*/.S
ff450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ff460 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 65  id sqlite3PCache
ff470 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 64  BufferSetup(void
ff480 20 2a 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20   *pBuf, int sz, 
ff490 69 6e 74 20 6e 29 7b 0a 20 20 50 67 46 72 65 65  int n){.  PgFree
ff4a0 73 6c 6f 74 20 2a 70 3b 0a 20 20 73 7a 20 3d 20  slot *p;.  sz = 
ff4b0 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a  ROUNDDOWN8(sz);.
ff4c0 20 20 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74    pcache1.szSlot
ff4d0 20 3d 20 73 7a 3b 0a 20 20 70 63 61 63 68 65 31   = sz;.  pcache1
ff4e0 2e 70 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a  .pStart = pBuf;.
ff4f0 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20    pcache1.pFree 
ff500 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d  = 0;.  while( n-
ff510 2d 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 50 67  - ){.    p = (Pg
ff520 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a  Freeslot*)pBuf;.
ff530 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70      p->pNext = p
ff540 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 20  cache1.pFree;.  
ff550 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20    pcache1.pFree 
ff560 3d 20 70 3b 0a 20 20 20 20 70 42 75 66 20 3d 20  = p;.    pBuf = 
ff570 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29  (void*)&((char*)
ff580 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 7d 0a 20  pBuf)[sz];.  }. 
ff590 20 70 63 61 63 68 65 31 2e 70 45 6e 64 20 3d 20   pcache1.pEnd = 
ff5a0 70 42 75 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  pBuf;.}../*.** M
ff5b0 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75  alloc function u
ff5c0 73 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 20  sed within this 
ff5d0 66 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  file to allocate
ff5e0 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20   space from the 
ff5f0 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67  buffer.** config
ff600 75 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  ured using sqlit
ff610 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45  e3_config(SQLITE
ff620 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
ff630 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f  E) option. If no
ff640 20 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 72   .** such buffer
ff650 20 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 65   exists or there
ff660 20 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 66   is no space lef
ff670 74 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 75  t in it, this fu
ff680 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a  nction falls .**
ff690 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33   back to sqlite3
ff6a0 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61  Malloc()..*/.sta
ff6b0 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65  tic void *pcache
ff6c0 31 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65  1Alloc(int nByte
ff6d0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20  ){.  void *p;.  
ff6e0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
ff6f0 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68  mutex_held(pcach
ff700 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  e1.mutex) );.  i
ff710 66 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 65  f( nByte<=pcache
ff720 31 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 63  1.szSlot && pcac
ff730 68 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 20  he1.pFree ){.   
ff740 20 70 20 3d 20 28 50 67 48 64 72 31 20 2a 29 70   p = (PgHdr1 *)p
ff750 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 20  cache1.pFree;.  
ff760 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20    pcache1.pFree 
ff770 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 2d  = pcache1.pFree-
ff780 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69  >pNext;.    sqli
ff790 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c  te3StatusSet(SQL
ff7a0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
ff7b0 41 43 48 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65  ACHE_SIZE, nByte
ff7c0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  );.    sqlite3St
ff7d0 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
ff7e0 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
ff7f0 55 53 45 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73  USED, 1);.  }els
ff800 65 7b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63  e{..    /* Alloc
ff810 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72  ate a new buffer
ff820 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 61   using sqlite3Ma
ff830 6c 6c 6f 63 2e 20 42 65 66 6f 72 65 20 64 6f 69  lloc. Before doi
ff840 6e 67 20 73 6f 2c 20 65 78 69 74 20 74 68 65 0a  ng so, exit the.
ff850 20 20 20 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63      ** global pc
ff860 61 63 68 65 20 6d 75 74 65 78 20 61 6e 64 20 75  ache mutex and u
ff870 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2d  nlock the pager-
ff880 63 61 63 68 65 20 6f 62 6a 65 63 74 20 70 43 61  cache object pCa
ff890 63 68 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20  che. This is .  
ff8a0 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 69 66 20    ** so that if 
ff8b0 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61  the attempt to a
ff8c0 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 62 75  llocate a new bu
ff8d0 66 66 65 72 20 63 61 75 73 65 73 20 74 68 65 20  ffer causes the 
ff8e0 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66  the .    ** conf
ff8f0 69 67 75 72 65 64 20 73 6f 66 74 2d 68 65 61 70  igured soft-heap
ff900 2d 6c 69 6d 69 74 20 74 6f 20 62 65 20 62 72 65  -limit to be bre
ff910 61 63 68 65 64 2c 20 69 74 20 77 69 6c 6c 20 62  ached, it will b
ff920 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20  e possible to.  
ff930 20 20 2a 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d    ** reclaim mem
ff940 6f 72 79 20 66 72 6f 6d 20 74 68 69 73 20 70 61  ory from this pa
ff950 67 65 72 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a  ger-cache..    *
ff960 2f 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61  /.    pcache1Lea
ff970 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 70  veMutex();.    p
ff980 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
ff990 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 63 61  (nByte);.    pca
ff9a0 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
ff9b0 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20  ;.    if( p ){. 
ff9c0 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 73 71       int sz = sq
ff9d0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
ff9e0 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
ff9f0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
ffa00 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
ffa10 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29  HE_OVERFLOW, sz)
ffa20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
ffa30 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
ffa40 20 46 72 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74   Free an allocat
ffa50 65 64 20 62 75 66 66 65 72 20 6f 62 74 61 69 6e  ed buffer obtain
ffa60 65 64 20 66 72 6f 6d 20 70 63 61 63 68 65 31 41  ed from pcache1A
ffa70 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lloc()..*/.stati
ffa80 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 46 72  c void pcache1Fr
ffa90 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61  ee(void *p){.  a
ffaa0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
ffab0 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65  utex_held(pcache
ffac0 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  1.mutex) );.  if
ffad0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( p==0 ) return;
ffae0 0a 20 20 69 66 28 20 70 3e 3d 70 63 61 63 68 65  .  if( p>=pcache
ffaf0 31 2e 70 53 74 61 72 74 20 26 26 20 70 3c 70 63  1.pStart && p<pc
ffb00 61 63 68 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20  ache1.pEnd ){.  
ffb10 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 53    PgFreeslot *pS
ffb20 6c 6f 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  lot;.    sqlite3
ffb30 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45  StatusAdd(SQLITE
ffb40 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
ffb50 45 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20  E_USED, -1);.   
ffb60 20 70 53 6c 6f 74 20 3d 20 28 50 67 46 72 65 65   pSlot = (PgFree
ffb70 73 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c  slot*)p;.    pSl
ffb80 6f 74 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63  ot->pNext = pcac
ffb90 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70  he1.pFree;.    p
ffba0 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70  cache1.pFree = p
ffbb0 53 6c 6f 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Slot;.  }else{. 
ffbc0 20 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73     int iSize = s
ffbd0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65  qlite3MallocSize
ffbe0 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  (p);.    sqlite3
ffbf0 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45  StatusAdd(SQLITE
ffc00 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
ffc10 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69  E_OVERFLOW, -iSi
ffc20 7a 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ze);.    sqlite3
ffc30 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  _free(p);.  }.}.
ffc40 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
ffc50 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63  a new page objec
ffc60 74 20 69 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f  t initially asso
ffc70 63 69 61 74 65 64 20 77 69 74 68 20 63 61 63 68  ciated with cach
ffc80 65 20 70 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61  e pCache..*/.sta
ffc90 74 69 63 20 50 67 48 64 72 31 20 2a 70 63 61 63  tic PgHdr1 *pcac
ffca0 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 50 43 61  he1AllocPage(PCa
ffcb0 63 68 65 31 20 2a 70 43 61 63 68 65 29 7b 0a 20  che1 *pCache){. 
ffcc0 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a   int nByte = siz
ffcd0 65 6f 66 28 50 67 48 64 72 31 29 20 2b 20 70 43  eof(PgHdr1) + pC
ffce0 61 63 68 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20  ache->szPage;.  
ffcf0 50 67 48 64 72 31 20 2a 70 20 3d 20 28 50 67 48  PgHdr1 *p = (PgH
ffd00 64 72 31 20 2a 29 70 63 61 63 68 65 31 41 6c 6c  dr1 *)pcache1All
ffd10 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28  oc(nByte);.  if(
ffd20 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43   p ){.    if( pC
ffd30 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
ffd40 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65   ){.      pcache
ffd50 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 2b 2b  1.nCurrentPage++
ffd60 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
ffd70 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
ffd80 20 46 72 65 65 20 61 20 70 61 67 65 20 6f 62 6a   Free a page obj
ffd90 65 63 74 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  ect allocated by
ffda0 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 67   pcache1AllocPag
ffdb0 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  e()..*/.static v
ffdc0 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 50  oid pcache1FreeP
ffdd0 61 67 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a  age(PgHdr1 *p){.
ffde0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69    if( p ){.    i
ffdf0 66 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 62 50  f( p->pCache->bP
ffe00 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20  urgeable ){.    
ffe10 20 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65    pcache1.nCurre
ffe20 6e 74 50 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a  ntPage--;.    }.
ffe30 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 28      pcache1Free(
ffe40 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
ffe50 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e   Malloc function
ffe60 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
ffe70 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20  to obtain space 
ffe80 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20  from the buffer 
ffe90 63 6f 6e 66 69 67 75 72 65 64 0a 2a 2a 20 75 73  configured.** us
ffea0 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ing sqlite3_conf
ffeb0 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
ffec0 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69  _PAGECACHE) opti
ffed0 6f 6e 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 62  on. If no such b
ffee0 75 66 66 65 72 0a 2a 2a 20 65 78 69 73 74 73 2c  uffer.** exists,
ffef0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   this function f
fff00 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20 73 71 6c  alls back to sql
fff10 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  ite3Malloc()..*/
fff20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
fff30 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
fff40 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a 29 7b  eMalloc(int sz){
fff50 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 70 63  .  void *p;.  pc
fff60 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
fff70 29 3b 0a 20 20 70 20 3d 20 70 63 61 63 68 65 31  );.  p = pcache1
fff80 41 6c 6c 6f 63 28 73 7a 29 3b 0a 20 20 70 63 61  Alloc(sz);.  pca
fff90 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
fffa0 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
fffb0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61  ./*.** Free an a
fffc0 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20  llocated buffer 
fffd0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
fffe0 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
ffff0 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
10000 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
10001 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 2a  3PageFree(void *
10002 70 29 7b 0a 20 20 70 63 61 63 68 65 31 45 6e 74  p){.  pcache1Ent
10003 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61  erMutex();.  pca
10004 63 68 65 31 46 72 65 65 28 70 29 3b 0a 20 20 70  che1Free(p);.  p
10005 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10006 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ();.}../********
10007 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10008 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10009 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1000a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1000b 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1000c 2a 20 47 65 6e 65 72 61 6c 20 49 6d 70 6c 65 6d  * General Implem
1000d 65 6e 74 61 74 69 6f 6e 20 46 75 6e 63 74 69 6f  entation Functio
1000e 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ns *************
1000f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10010 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
10011 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
10012 20 75 73 65 64 20 74 6f 20 72 65 73 69 7a 65 20   used to resize 
10013 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 75  the hash table u
10014 73 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65  sed by the cache
10015 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68   passed.** as th
10016 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
10017 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62  ..**.** The glob
10018 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  al mutex must be
10019 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20   held when this 
1001a 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1001b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
1001c 74 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 48  t pcache1ResizeH
1001d 61 73 68 28 50 43 61 63 68 65 31 20 2a 70 29 7b  ash(PCache1 *p){
1001e 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 70 4e 65  .  PgHdr1 **apNe
1001f 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  w;.  unsigned in
10020 74 20 6e 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e  t nNew;.  unsign
10021 65 64 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73  ed int i;..  ass
10022 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10023 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e  ex_held(pcache1.
10024 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 6e 4e 65  mutex) );..  nNe
10025 77 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a  w = p->nHash*2;.
10026 20 20 69 66 28 20 6e 4e 65 77 3c 32 35 36 20 29    if( nNew<256 )
10027 7b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 32 35 36  {.    nNew = 256
10028 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31  ;.  }..  pcache1
10029 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
1002a 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20  if( p->nHash ){ 
1002b 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
1002c 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20  gnMalloc(); }.  
1002d 61 70 4e 65 77 20 3d 20 28 50 67 48 64 72 31 20  apNew = (PgHdr1 
1002e 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  **)sqlite3_mallo
1002f 63 28 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20  c(sizeof(PgHdr1 
10030 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20  *)*nNew);.  if( 
10031 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69  p->nHash ){ sqli
10032 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
10033 6f 63 28 29 3b 20 7d 0a 20 20 70 63 61 63 68 65  oc(); }.  pcache
10034 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10035 20 69 66 28 20 61 70 4e 65 77 20 29 7b 0a 20 20   if( apNew ){.  
10036 20 20 6d 65 6d 73 65 74 28 61 70 4e 65 77 2c 20    memset(apNew, 
10037 30 2c 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31  0, sizeof(PgHdr1
10038 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 20 20 66   *)*nNew);.    f
10039 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61  or(i=0; i<p->nHa
1003a 73 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  sh; i++){.      
1003b 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20  PgHdr1 *pPage;. 
1003c 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 4e 65       PgHdr1 *pNe
1003d 78 74 20 3d 20 70 2d 3e 61 70 48 61 73 68 5b 69  xt = p->apHash[i
1003e 5d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ];.      while( 
1003f 28 70 50 61 67 65 20 3d 20 70 4e 65 78 74 29 21  (pPage = pNext)!
10040 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e  =0 ){.        un
10041 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 70  signed int h = p
10042 50 61 67 65 2d 3e 69 4b 65 79 20 25 20 6e 4e 65  Page->iKey % nNe
10043 77 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74  w;.        pNext
10044 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b   = pPage->pNext;
10045 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e  .        pPage->
10046 70 4e 65 78 74 20 3d 20 61 70 4e 65 77 5b 68 5d  pNext = apNew[h]
10047 3b 0a 20 20 20 20 20 20 20 20 61 70 4e 65 77 5b  ;.        apNew[
10048 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20  h] = pPage;.    
10049 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
1004a 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70  lite3_free(p->ap
1004b 48 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 61 70  Hash);.    p->ap
1004c 48 61 73 68 20 3d 20 61 70 4e 65 77 3b 0a 20 20  Hash = apNew;.  
1004d 20 20 70 2d 3e 6e 48 61 73 68 20 3d 20 6e 4e 65    p->nHash = nNe
1004e 77 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  w;.  }..  return
1004f 20 28 70 2d 3e 61 70 48 61 73 68 20 3f 20 53 51   (p->apHash ? SQ
10050 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
10051 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  _NOMEM);.}../*.*
10052 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
10053 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  is used internal
10054 6c 79 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65  ly to remove the
10055 20 70 61 67 65 20 70 50 61 67 65 20 66 72 6f 6d   page pPage from
10056 20 74 68 65 20 0a 2a 2a 20 67 6c 6f 62 61 6c 20   the .** global 
10057 4c 52 55 20 6c 69 73 74 2c 20 69 66 20 69 73 20  LRU list, if is 
10058 70 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 70  part of it. If p
10059 50 61 67 65 20 69 73 20 6e 6f 74 20 70 61 72 74  Page is not part
1005a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 0a 2a   of the global.*
1005b 2a 20 4c 52 55 20 6c 69 73 74 2c 20 74 68 65 6e  * LRU list, then
1005c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
1005d 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
1005e 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65   The global mute
1005f 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77  x must be held w
10060 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
10061 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  n is called..*/.
10062 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10063 68 65 31 50 69 6e 50 61 67 65 28 50 67 48 64 72  he1PinPage(PgHdr
10064 31 20 2a 70 50 61 67 65 29 7b 0a 20 20 61 73 73  1 *pPage){.  ass
10065 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10066 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e  ex_held(pcache1.
10067 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
10068 70 50 61 67 65 20 26 26 20 28 70 50 61 67 65 2d  pPage && (pPage-
10069 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 50 61  >pLruNext || pPa
1006a 67 65 3d 3d 70 63 61 63 68 65 31 2e 70 4c 72 75  ge==pcache1.pLru
1006b 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 69 66 28  Tail) ){.    if(
1006c 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76   pPage->pLruPrev
1006d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d   ){.      pPage-
1006e 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e  >pLruPrev->pLruN
1006f 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72  ext = pPage->pLr
10070 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  uNext;.    }.   
10071 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75   if( pPage->pLru
10072 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 50  Next ){.      pP
10073 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70  age->pLruNext->p
10074 4c 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 2d  LruPrev = pPage-
10075 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d  >pLruPrev;.    }
10076 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 65 31  .    if( pcache1
10077 2e 70 4c 72 75 48 65 61 64 3d 3d 70 50 61 67 65  .pLruHead==pPage
10078 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65   ){.      pcache
10079 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61  1.pLruHead = pPa
1007a 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20  ge->pLruNext;.  
1007b 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61 63    }.    if( pcac
1007c 68 65 31 2e 70 4c 72 75 54 61 69 6c 3d 3d 70 50  he1.pLruTail==pP
1007d 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61  age ){.      pca
1007e 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20  che1.pLruTail = 
1007f 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b  pPage->pLruPrev;
10080 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
10081 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a  ->pLruNext = 0;.
10082 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50      pPage->pLruP
10083 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  rev = 0;.    pPa
10084 67 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 65 63  ge->pCache->nRec
10085 79 63 6c 61 62 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d  yclable--;.  }.}
10086 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  .../*.** Remove 
10087 74 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65  the page supplie
10088 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
10089 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74   from the hash t
1008a 61 62 6c 65 20 0a 2a 2a 20 28 50 43 61 63 68 65  able .** (PCache
1008b 31 2e 61 70 48 61 73 68 20 73 74 72 75 63 74 75  1.apHash structu
1008c 72 65 29 20 74 68 61 74 20 69 74 20 69 73 20 63  re) that it is c
1008d 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20  urrently stored 
1008e 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c  in..**.** The gl
1008f 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20  obal mutex must 
10090 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69  be held when thi
10091 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
10092 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lled..*/.static 
10093 76 6f 69 64 20 70 63 61 63 68 65 31 52 65 6d 6f  void pcache1Remo
10094 76 65 46 72 6f 6d 48 61 73 68 28 50 67 48 64 72  veFromHash(PgHdr
10095 31 20 2a 70 50 61 67 65 29 7b 0a 20 20 75 6e 73  1 *pPage){.  uns
10096 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 50  igned int h;.  P
10097 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
10098 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a   pPage->pCache;.
10099 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 0a    PgHdr1 **pp;..
1009a 20 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65    h = pPage->iKe
1009b 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73  y % pCache->nHas
1009c 68 3b 0a 20 20 66 6f 72 28 70 70 3d 26 70 43 61  h;.  for(pp=&pCa
1009d 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20  che->apHash[h]; 
1009e 28 2a 70 70 29 21 3d 70 50 61 67 65 3b 20 70 70  (*pp)!=pPage; pp
1009f 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 3b  =&(*pp)->pNext);
100a0 0a 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e  .  *pp = (*pp)->
100a1 70 4e 65 78 74 3b 0a 0a 20 20 70 43 61 63 68 65  pNext;..  pCache
100a2 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a  ->nPage--;.}../*
100a3 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65  .** If there are
100a4 20 63 75 72 72 65 6e 74 6c 79 20 6d 6f 72 65 20   currently more 
100a5 74 68 61 6e 20 70 63 61 63 68 65 2e 6e 4d 61 78  than pcache.nMax
100a6 50 61 67 65 20 70 61 67 65 73 20 61 6c 6c 6f 63  Page pages alloc
100a7 61 74 65 64 2c 20 74 72 79 0a 2a 2a 20 74 6f 20  ated, try.** to 
100a8 72 65 63 79 63 6c 65 20 70 61 67 65 73 20 74 6f  recycle pages to
100a9 20 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62   reduce the numb
100aa 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  er allocated to 
100ab 70 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 2e  pcache.nMaxPage.
100ac 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
100ad 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61  pcache1EnforceMa
100ae 78 50 61 67 65 28 76 6f 69 64 29 7b 0a 20 20 61  xPage(void){.  a
100af 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
100b0 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65  utex_held(pcache
100b1 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 77 68  1.mutex) );.  wh
100b2 69 6c 65 28 20 70 63 61 63 68 65 31 2e 6e 43 75  ile( pcache1.nCu
100b3 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63 68 65  rrentPage>pcache
100b4 31 2e 6e 4d 61 78 50 61 67 65 20 26 26 20 70 63  1.nMaxPage && pc
100b5 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 29  ache1.pLruTail )
100b6 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 20  {.    PgHdr1 *p 
100b7 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  = pcache1.pLruTa
100b8 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50  il;.    pcache1P
100b9 69 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 70  inPage(p);.    p
100ba 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d  cache1RemoveFrom
100bb 48 61 73 68 28 70 29 3b 0a 20 20 20 20 70 63 61  Hash(p);.    pca
100bc 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b  che1FreePage(p);
100bd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69  .  }.}../*.** Di
100be 73 63 61 72 64 20 61 6c 6c 20 70 61 67 65 73 20  scard all pages 
100bf 66 72 6f 6d 20 63 61 63 68 65 20 70 43 61 63 68  from cache pCach
100c0 65 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75  e with a page nu
100c1 6d 62 65 72 20 28 6b 65 79 20 76 61 6c 75 65 29  mber (key value)
100c2 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61   .** greater tha
100c3 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69 4c  n or equal to iL
100c4 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64  imit. Any pinned
100c5 20 70 61 67 65 73 20 74 68 61 74 20 6d 65 65 74   pages that meet
100c6 20 74 68 69 73 20 0a 2a 2a 20 63 72 69 74 65 72   this .** criter
100c7 69 61 20 61 72 65 20 75 6e 70 69 6e 6e 65 64 20  ia are unpinned 
100c8 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20  before they are 
100c9 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a  discarded..**.**
100ca 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65   The global mute
100cb 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77  x must be held w
100cc 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
100cd 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  n is called..*/.
100ce 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
100cf 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66  he1TruncateUnsaf
100d0 65 28 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  e(.  PCache1 *pC
100d1 61 63 68 65 2c 20 0a 20 20 75 6e 73 69 67 6e 65  ache, .  unsigne
100d2 64 20 69 6e 74 20 69 4c 69 6d 69 74 20 0a 29 7b  d int iLimit .){
100d3 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
100d4 68 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  h;.  assert( sql
100d5 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
100d6 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
100d7 3b 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70  ;.  for(h=0; h<p
100d8 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b  Cache->nHash; h+
100d9 2b 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a  +){.    PgHdr1 *
100da 2a 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61  *pp = &pCache->a
100db 70 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50  pHash[h]; .    P
100dc 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20  gHdr1 *pPage;.  
100dd 20 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20    while( (pPage 
100de 3d 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 20  = *pp)!=0 ){.   
100df 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 4b     if( pPage->iK
100e0 65 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20  ey>=iLimit ){.  
100e1 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e        pcache1Pin
100e2 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
100e3 20 20 20 20 20 2a 70 70 20 3d 20 70 50 61 67 65       *pp = pPage
100e4 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
100e5 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65   pcache1FreePage
100e6 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d  (pPage);.      }
100e7 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 70  else{.        pp
100e8 20 3d 20 26 70 50 61 67 65 2d 3e 70 4e 65 78 74   = &pPage->pNext
100e9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
100ea 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
100eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100ef 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
100f0 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  * sqlite3_pcache
100f1 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a   Methods *******
100f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100f4 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
100f5 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
100f6 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
100f7 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 74 68 6f  ache.xInit metho
100f8 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
100f9 20 70 63 61 63 68 65 31 49 6e 69 74 28 76 6f 69   pcache1Init(voi
100fa 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
100fb 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
100fc 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 73  NotUsed);.  mems
100fd 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20  et(&pcache1, 0, 
100fe 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29  sizeof(pcache1))
100ff 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  ;.  if( sqlite3G
10100 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
10101 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 70 63  eMutex ){.    pc
10102 61 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 73 71  ache1.mutex = sq
10103 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
10104 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
10105 54 41 54 49 43 5f 4c 52 55 29 3b 0a 20 20 7d 0a  TATIC_LRU);.  }.
10106 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10107 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  OK;.}../*.** Imp
10108 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10109 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
1010a 65 2e 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68  e.xShutdown meth
1010b 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  od..*/.static vo
1010c 69 64 20 70 63 61 63 68 65 31 53 68 75 74 64 6f  id pcache1Shutdo
1010d 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  wn(void *NotUsed
1010e 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
1010f 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
10110 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a    /* no-op */.}.
10111 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10112 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10113 69 74 65 33 5f 70 63 61 63 68 65 2e 78 43 72 65  ite3_pcache.xCre
10114 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a  ate method..**.*
10115 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
10116 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69   cache..*/.stati
10117 63 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  c sqlite3_pcache
10118 20 2a 70 63 61 63 68 65 31 43 72 65 61 74 65 28   *pcache1Create(
10119 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20  int szPage, int 
1011a 62 50 75 72 67 65 61 62 6c 65 29 7b 0a 20 20 50  bPurgeable){.  P
1011b 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 0a  Cache1 *pCache;.
1011c 0a 20 20 70 43 61 63 68 65 20 3d 20 28 50 43 61  .  pCache = (PCa
1011d 63 68 65 31 20 2a 29 73 71 6c 69 74 65 33 5f 6d  che1 *)sqlite3_m
1011e 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 43 61  alloc(sizeof(PCa
1011f 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 70 43  che1));.  if( pC
10120 61 63 68 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73  ache ){.    mems
10121 65 74 28 70 43 61 63 68 65 2c 20 30 2c 20 73 69  et(pCache, 0, si
10122 7a 65 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a  zeof(PCache1));.
10123 20 20 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61      pCache->szPa
10124 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20  ge = szPage;.   
10125 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
10126 62 6c 65 20 3d 20 28 62 50 75 72 67 65 61 62 6c  ble = (bPurgeabl
10127 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20  e ? 1 : 0);.    
10128 69 66 28 20 62 50 75 72 67 65 61 62 6c 65 20 29  if( bPurgeable )
10129 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e  {.      pCache->
1012a 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 20 20 20  nMin = 10;.     
1012b 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
1012c 65 78 28 29 3b 0a 20 20 20 20 20 20 70 63 61 63  ex();.      pcac
1012d 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2b 3d 20  he1.nMinPage += 
1012e 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20  pCache->nMin;.  
1012f 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65      pcache1Leave
10130 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 7d 0a 20  Mutex();.    }. 
10131 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c   }.  return (sql
10132 69 74 65 33 5f 70 63 61 63 68 65 20 2a 29 70 43  ite3_pcache *)pC
10133 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ache;.}../*.** I
10134 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
10135 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
10136 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 20 6d  che.xCachesize m
10137 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f  ethod. .**.** Co
10138 6e 66 69 67 75 72 65 20 74 68 65 20 63 61 63 68  nfigure the cach
10139 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72  e_size limit for
1013a 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61   a cache..*/.sta
1013b 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
1013c 43 61 63 68 65 73 69 7a 65 28 73 71 6c 69 74 65  Cachesize(sqlite
1013d 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 69 6e 74  3_pcache *p, int
1013e 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61 63 68 65   nMax){.  PCache
1013f 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61  1 *pCache = (PCa
10140 63 68 65 31 20 2a 29 70 3b 0a 20 20 69 66 28 20  che1 *)p;.  if( 
10141 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
10142 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65  le ){.    pcache
10143 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10144 20 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50     pcache1.nMaxP
10145 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20 2d 20 70  age += (nMax - p
10146 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20  Cache->nMax);.  
10147 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d    pCache->nMax =
10148 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63 61 63 68   nMax;.    pcach
10149 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65  e1EnforceMaxPage
1014a 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 4c  ();.    pcache1L
1014b 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
1014c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
1014d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
1014e 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
1014f 50 61 67 65 63 6f 75 6e 74 20 6d 65 74 68 6f 64  Pagecount method
10150 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  . .*/.static int
10151 20 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e   pcache1Pagecoun
10152 74 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t(sqlite3_pcache
10153 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20   *p){.  int n;. 
10154 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
10155 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28 28 50 43  ex();.  n = ((PC
10156 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e 50 61 67  ache1 *)p)->nPag
10157 65 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  e;.  pcache1Leav
10158 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75  eMutex();.  retu
10159 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn n;.}../*.** I
1015a 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
1015b 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
1015c 63 68 65 2e 78 46 65 74 63 68 20 6d 65 74 68 6f  che.xFetch metho
1015d 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 63 68 20  d. .**.** Fetch 
1015e 61 20 70 61 67 65 20 62 79 20 6b 65 79 20 76 61  a page by key va
1015f 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68  lue..**.** Wheth
10160 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e 65 77 20  er or not a new 
10161 70 61 67 65 20 6d 61 79 20 62 65 20 61 6c 6c 6f  page may be allo
10162 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75  cated by this fu
10163 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f  nction depends o
10164 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f  n.** the value o
10165 66 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 67  f the createFlag
10166 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
10167 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65   There are three
10168 20 64 69 66 66 65 72 65 6e 74 20 61 70 70 72 6f   different appro
10169 61 63 68 65 73 20 74 6f 20 6f 62 74 61 69 6e 69  aches to obtaini
1016a 6e 67 20 73 70 61 63 65 20 66 6f 72 20 61 20 70  ng space for a p
1016b 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e  age,.** dependin
1016c 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  g on the value o
1016d 66 20 70 61 72 61 6d 65 74 65 72 20 63 72 65 61  f parameter crea
1016e 74 65 46 6c 61 67 20 28 77 68 69 63 68 20 6d 61  teFlag (which ma
1016f 79 20 62 65 20 30 2c 20 31 20 6f 72 20 32 29 2e  y be 0, 1 or 2).
10170 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52 65 67 61  .**.**   1. Rega
10171 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 61  rdless of the va
10172 6c 75 65 20 6f 66 20 63 72 65 61 74 65 46 6c 61  lue of createFla
10173 67 2c 20 74 68 65 20 63 61 63 68 65 20 69 73 20  g, the cache is 
10174 73 65 61 72 63 68 65 64 20 66 6f 72 20 61 20 0a  searched for a .
10175 2a 2a 20 20 20 20 20 20 63 6f 70 79 20 6f 66 20  **      copy of 
10176 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
10177 67 65 2e 20 49 66 20 6f 6e 65 20 69 73 20 66 6f  ge. If one is fo
10178 75 6e 64 2c 20 69 74 20 69 73 20 72 65 74 75 72  und, it is retur
10179 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20  ned..**.**   2. 
1017a 49 66 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30  If createFlag==0
1017b 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73   and the page is
1017c 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20   not already in 
1017d 74 68 65 20 63 61 63 68 65 2c 20 4e 55 4c 4c 20  the cache, NULL 
1017e 69 73 0a 2a 2a 20 20 20 20 20 20 72 65 74 75 72  is.**      retur
1017f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20  ned..**.**   3. 
10180 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73  If createFlag is
10181 20 31 2c 20 74 68 65 20 63 61 63 68 65 20 69 73   1, the cache is
10182 20 6d 61 72 6b 65 64 20 61 73 20 70 75 72 67 65   marked as purge
10183 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70 61 67  able and the pag
10184 65 20 69 73 20 0a 2a 2a 20 20 20 20 20 20 6e 6f  e is .**      no
10185 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  t already in the
10186 20 63 61 63 68 65 2c 20 61 6e 64 20 69 66 20 65   cache, and if e
10187 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f 6c  ither of the fol
10188 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 2c  lowing are true,
10189 20 0a 2a 2a 20 20 20 20 20 20 72 65 74 75 72 6e   .**      return
1018a 20 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   NULL:.**.**    
1018b 20 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65     (a) the numbe
1018c 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65  r of pages pinne
1018d 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69  d by the cache i
1018e 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a  s greater than.*
1018f 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63  *           PCac
10190 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20  he1.nMax, or.** 
10191 20 20 20 20 20 20 28 62 29 20 74 68 65 20 6e 75        (b) the nu
10192 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69  mber of pages pi
10193 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68  nned by the cach
10194 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  e is greater tha
10195 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  n.**           t
10196 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66  he sum of nMax f
10197 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65  or all purgeable
10198 20 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 68   caches, less th
10199 65 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20  e sum of .**    
1019a 20 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20         nMin for 
1019b 61 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 61  all other purgea
1019c 62 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a  ble caches. .**.
1019d 2a 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20  **   4. If none 
1019e 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72  of the first thr
1019f 65 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70  ee conditions ap
101a0 70 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 68  ply and the cach
101a1 65 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20  e is marked.**  
101a2 20 20 20 20 61 73 20 70 75 72 67 65 61 62 6c 65      as purgeable
101a3 2c 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20  , and if one of 
101a4 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73  the following is
101a5 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   true:.**.**    
101a6 20 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65     (a) The numbe
101a7 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63  r of pages alloc
101a8 61 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63  ated for the cac
101a9 68 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 2a  he is already .*
101aa 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63  *           PCac
101ab 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a  he1.nMax, or.**.
101ac 2a 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 65  **       (b) The
101ad 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
101ae 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61   allocated for a
101af 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63  ll purgeable cac
101b0 68 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 20  hes is.**       
101b1 20 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 61      already equa
101b2 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20  l to or greater 
101b3 74 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 20  than the sum of 
101b4 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20  nMax for all.** 
101b5 20 20 20 20 20 20 20 20 20 20 70 75 72 67 65 61            purgea
101b6 62 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a  ble caches,.**.*
101b7 2a 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 65  *      then atte
101b8 6d 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 61  mpt to recycle a
101b9 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 4c   page from the L
101ba 52 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 69  RU list. If it i
101bb 73 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 20  s the right.**  
101bc 20 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 6e      size, return
101bd 20 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 75   the recycled bu
101be 66 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c  ffer. Otherwise,
101bf 20 66 72 65 65 20 74 68 65 20 62 75 66 66 65 72   free the buffer
101c0 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f   and.**      pro
101c1 63 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e 20  ceed to step 5. 
101c2 0a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65  .**.**   5. Othe
101c3 72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20  rwise, allocate 
101c4 61 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 77  and return a new
101c5 20 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f   page buffer..*/
101c6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63  .static void *pc
101c7 61 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 74  ache1Fetch(sqlit
101c8 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e  e3_pcache *p, un
101c9 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c  signed int iKey,
101ca 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29   int createFlag)
101cb 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
101cc 20 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63   nPinned;.  PCac
101cd 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50  he1 *pCache = (P
101ce 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67  Cache1 *)p;.  Pg
101cf 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b  Hdr1 *pPage = 0;
101d0 0a 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ..  pcache1Enter
101d1 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63  Mutex();.  if( c
101d2 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73  reateFlag==1 ) s
101d3 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
101d4 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a  nMalloc();..  /*
101d5 20 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68   Search the hash
101d6 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78   table for an ex
101d7 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f  isting entry. */
101d8 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e  .  if( pCache->n
101d9 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e  Hash>0 ){.    un
101da 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69  signed int h = i
101db 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48  Key % pCache->nH
101dc 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61  ash;.    for(pPa
101dd 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73  ge=pCache->apHas
101de 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61  h[h]; pPage&&pPa
101df 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20  ge->iKey!=iKey; 
101e0 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65  pPage=pPage->pNe
101e1 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  xt);.  }..  if( 
101e2 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46  pPage || createF
101e3 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63  lag==0 ){.    pc
101e4 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61  ache1PinPage(pPa
101e5 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65  ge);.    goto fe
101e6 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  tch_out;.  }..  
101e7 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 65 61  /* Step 3 of hea
101e8 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a  der comment. */.
101e9 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63    nPinned = pCac
101ea 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63  he->nPage - pCac
101eb 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b  he->nRecyclable;
101ec 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61  .  if( createFla
101ed 67 3d 3d 31 20 26 26 20 70 43 61 63 68 65 2d 3e  g==1 && pCache->
101ee 62 50 75 72 67 65 61 62 6c 65 20 26 26 20 28 0a  bPurgeable && (.
101ef 20 20 20 20 20 20 20 20 6e 50 69 6e 6e 65 64 3e          nPinned>
101f0 3d 28 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61  =(pcache1.nMaxPa
101f1 67 65 2b 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 2d  ge+pCache->nMin-
101f2 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65  pcache1.nMinPage
101f3 29 0a 20 20 20 20 20 7c 7c 20 6e 50 69 6e 6e 65  ).     || nPinne
101f4 64 3e 3d 28 70 43 61 63 68 65 2d 3e 6e 4d 61 78  d>=(pCache->nMax
101f5 20 2a 20 39 20 2f 20 31 30 29 0a 20 20 29 29 7b   * 9 / 10).  )){
101f6 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f  .    goto fetch_
101f7 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  out;.  }..  if( 
101f8 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3e 3d 70  pCache->nPage>=p
101f9 43 61 63 68 65 2d 3e 6e 48 61 73 68 20 26 26 20  Cache->nHash && 
101fa 70 63 61 63 68 65 31 52 65 73 69 7a 65 48 61 73  pcache1ResizeHas
101fb 68 28 70 43 61 63 68 65 29 20 29 7b 0a 20 20 20  h(pCache) ){.   
101fc 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b   goto fetch_out;
101fd 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20  .  }..  /* Step 
101fe 34 2e 20 54 72 79 20 74 6f 20 72 65 63 79 63 6c  4. Try to recycl
101ff 65 20 61 20 70 61 67 65 20 62 75 66 66 65 72 20  e a page buffer 
10200 69 66 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20  if appropriate. 
10201 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  */.  if( pCache-
10202 3e 62 50 75 72 67 65 61 62 6c 65 20 26 26 20 70  >bPurgeable && p
10203 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20  cache1.pLruTail 
10204 26 26 20 28 0a 20 20 20 20 20 20 70 43 61 63 68  && (.      pCach
10205 65 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65  e->nPage>=pCache
10206 2d 3e 6e 4d 61 78 2d 31 20 7c 7c 20 70 63 61 63  ->nMax-1 || pcac
10207 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65  he1.nCurrentPage
10208 3e 3d 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61  >=pcache1.nMaxPa
10209 67 65 0a 20 20 29 29 7b 0a 20 20 20 20 70 50 61  ge.  )){.    pPa
1020a 67 65 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72  ge = pcache1.pLr
1020b 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 63 68  uTail;.    pcach
1020c 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  e1RemoveFromHash
1020d 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63 61  (pPage);.    pca
1020e 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67  che1PinPage(pPag
1020f 65 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67  e);.    if( pPag
10210 65 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67  e->pCache->szPag
10211 65 21 3d 70 43 61 63 68 65 2d 3e 73 7a 50 61 67  e!=pCache->szPag
10212 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68  e ){.      pcach
10213 65 31 46 72 65 65 50 61 67 65 28 70 50 61 67 65  e1FreePage(pPage
10214 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d  );.      pPage =
10215 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
10216 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43 75       pcache1.nCu
10217 72 72 65 6e 74 50 61 67 65 20 2d 3d 20 28 70 50  rrentPage -= (pP
10218 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e 62 50 75  age->pCache->bPu
10219 72 67 65 61 62 6c 65 20 2d 20 70 43 61 63 68 65  rgeable - pCache
1021a 2d 3e 62 50 75 72 67 65 61 62 6c 65 29 3b 0a 20  ->bPurgeable);. 
1021b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53     }.  }..  /* S
1021c 74 65 70 20 35 2e 20 49 66 20 61 20 75 73 61 62  tep 5. If a usab
1021d 6c 65 20 70 61 67 65 20 62 75 66 66 65 72 20 68  le page buffer h
1021e 61 73 20 73 74 69 6c 6c 20 6e 6f 74 20 62 65 65  as still not bee
1021f 6e 20 66 6f 75 6e 64 2c 20 0a 20 20 2a 2a 20 61  n found, .  ** a
10220 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61  ttempt to alloca
10221 74 65 20 61 20 6e 65 77 20 6f 6e 65 2e 20 0a 20  te a new one. . 
10222 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67 65   */.  if( !pPage
10223 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20   ){.    pPage = 
10224 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61 67 65  pcache1AllocPage
10225 28 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20  (pCache);.  }.. 
10226 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20   if( pPage ){.  
10227 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
10228 20 3d 20 69 4b 65 79 20 25 20 70 43 61 63 68 65   = iKey % pCache
10229 2d 3e 6e 48 61 73 68 3b 0a 20 20 20 20 2a 28 76  ->nHash;.    *(v
1022a 6f 69 64 20 2a 2a 29 28 50 47 48 44 52 31 5f 54  oid **)(PGHDR1_T
1022b 4f 5f 50 41 47 45 28 70 50 61 67 65 29 29 20 3d  O_PAGE(pPage)) =
1022c 20 30 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e   0;.    pCache->
1022d 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61  nPage++;.    pPa
1022e 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b  ge->iKey = iKey;
1022f 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78  .    pPage->pNex
10230 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61  t = pCache->apHa
10231 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65  sh[h];.    pPage
10232 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68  ->pCache = pCach
10233 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c  e;.    pPage->pL
10234 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  ruPrev = 0;.    
10235 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
10236 3d 20 30 3b 0a 20 20 20 20 70 43 61 63 68 65 2d  = 0;.    pCache-
10237 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61  >apHash[h] = pPa
10238 67 65 3b 0a 20 20 7d 0a 0a 66 65 74 63 68 5f 6f  ge;.  }..fetch_o
10239 75 74 3a 0a 20 20 69 66 28 20 70 50 61 67 65 20  ut:.  if( pPage 
1023a 26 26 20 69 4b 65 79 3e 70 43 61 63 68 65 2d 3e  && iKey>pCache->
1023b 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 70  iMaxKey ){.    p
1023c 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d  Cache->iMaxKey =
1023d 20 69 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28   iKey;.  }.  if(
1023e 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29   createFlag==1 )
1023f 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
10240 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 70 63 61  nMalloc();.  pca
10241 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10242 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67  ;.  return (pPag
10243 65 20 3f 20 50 47 48 44 52 31 5f 54 4f 5f 50 41  e ? PGHDR1_TO_PA
10244 47 45 28 70 50 61 67 65 29 20 3a 20 30 29 3b 0a  GE(pPage) : 0);.
10245 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  }.../*.** Implem
10246 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
10247 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
10248 55 6e 70 69 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a  Unpin method..**
10249 0a 2a 2a 20 4d 61 72 6b 20 61 20 70 61 67 65 20  .** Mark a page 
1024a 61 73 20 75 6e 70 69 6e 6e 65 64 20 28 65 6c 69  as unpinned (eli
1024b 67 69 62 6c 65 20 66 6f 72 20 61 73 79 6e 63 68  gible for asynch
1024c 72 6f 6e 6f 75 73 20 72 65 63 79 63 6c 69 6e 67  ronous recycling
1024d 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
1024e 64 20 70 63 61 63 68 65 31 55 6e 70 69 6e 28 73  d pcache1Unpin(s
1024f 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70  qlite3_pcache *p
10250 2c 20 76 6f 69 64 20 2a 70 50 67 2c 20 69 6e 74  , void *pPg, int
10251 20 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 29 7b   reuseUnlikely){
10252 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63  .  PCache1 *pCac
10253 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29  he = (PCache1 *)
10254 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61  p;.  PgHdr1 *pPa
10255 67 65 20 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48  ge = PAGE_TO_PGH
10256 44 52 31 28 70 50 67 29 3b 0a 0a 20 20 70 63 61  DR1(pPg);..  pca
10257 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10258 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 61 6e  ;..  /* It is an
10259 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74   error to call t
1025a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  his function if 
1025b 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65  the page is alre
1025c 61 64 79 20 0a 20 20 2a 2a 20 70 61 72 74 20 6f  ady .  ** part o
1025d 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55  f the global LRU
1025e 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 61 73   list..  */.  as
1025f 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 4c 72  sert( pPage->pLr
10260 75 50 72 65 76 3d 3d 30 20 26 26 20 70 50 61 67  uPrev==0 && pPag
10261 65 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d 30 20 29  e->pLruNext==0 )
10262 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63  ;.  assert( pcac
10263 68 65 31 2e 70 4c 72 75 48 65 61 64 21 3d 70 50  he1.pLruHead!=pP
10264 61 67 65 20 26 26 20 70 63 61 63 68 65 31 2e 70  age && pcache1.p
10265 4c 72 75 54 61 69 6c 21 3d 70 50 61 67 65 20 29  LruTail!=pPage )
10266 3b 0a 0a 20 20 69 66 28 20 72 65 75 73 65 55 6e  ;..  if( reuseUn
10267 6c 69 6b 65 6c 79 20 7c 7c 20 70 63 61 63 68 65  likely || pcache
10268 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70  1.nCurrentPage>p
10269 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20  cache1.nMaxPage 
1026a 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65  ){.    pcache1Re
1026b 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61  moveFromHash(pPa
1026c 67 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31  ge);.    pcache1
1026d 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  FreePage(pPage);
1026e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1026f 20 41 64 64 20 74 68 65 20 70 61 67 65 20 74 6f   Add the page to
10270 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20   the global LRU 
10271 6c 69 73 74 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20  list. Normally, 
10272 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65  the page is adde
10273 64 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  d to.    ** the 
10274 68 65 61 64 20 6f 66 20 74 68 65 20 6c 69 73 74  head of the list
10275 20 28 6c 61 73 74 20 70 61 67 65 20 74 6f 20 62   (last page to b
10276 65 20 72 65 63 79 63 6c 65 64 29 2e 20 48 6f 77  e recycled). How
10277 65 76 65 72 2c 20 69 66 20 74 68 65 20 0a 20 20  ever, if the .  
10278 20 20 2a 2a 20 72 65 75 73 65 55 6e 6c 69 6b 65    ** reuseUnlike
10279 6c 79 20 66 6c 61 67 20 70 61 73 73 65 64 20 74  ly flag passed t
1027a 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  o this function 
1027b 69 73 20 74 72 75 65 2c 20 74 68 65 20 70 61 67  is true, the pag
1027c 65 20 69 73 20 61 64 64 65 64 0a 20 20 20 20 2a  e is added.    *
1027d 2a 20 74 6f 20 74 68 65 20 74 61 69 6c 20 6f 66  * to the tail of
1027e 20 74 68 65 20 6c 69 73 74 20 28 66 69 72 73 74   the list (first
1027f 20 70 61 67 65 20 74 6f 20 62 65 20 72 65 63 79   page to be recy
10280 63 6c 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  cled)..    */.  
10281 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c    if( pcache1.pL
10282 72 75 48 65 61 64 20 29 7b 0a 20 20 20 20 20 20  ruHead ){.      
10283 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
10284 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50 61  ->pLruPrev = pPa
10285 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d  ge;.      pPage-
10286 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 63 61 63  >pLruNext = pcac
10287 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 0a 20 20  he1.pLruHead;.  
10288 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75      pcache1.pLru
10289 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20  Head = pPage;.  
1028a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
1028b 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20  cache1.pLruTail 
1028c 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70  = pPage;.      p
1028d 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20  cache1.pLruHead 
1028e 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20  = pPage;.    }. 
1028f 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 63 79     pCache->nRecy
10290 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20  clable++;.  }.. 
10291 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10292 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ex();.}../*.** I
10293 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
10294 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
10295 63 68 65 2e 78 52 65 6b 65 79 20 6d 65 74 68 6f  che.xRekey metho
10296 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  d. .*/.static vo
10297 69 64 20 70 63 61 63 68 65 31 52 65 6b 65 79 28  id pcache1Rekey(
10298 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  .  sqlite3_pcach
10299 65 20 2a 70 2c 0a 20 20 76 6f 69 64 20 2a 70 50  e *p,.  void *pP
1029a 67 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  g,.  unsigned in
1029b 74 20 69 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 6e  t iOld,.  unsign
1029c 65 64 20 69 6e 74 20 69 4e 65 77 0a 29 7b 0a 20  ed int iNew.){. 
1029d 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
1029e 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b   = (PCache1 *)p;
1029f 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65  .  PgHdr1 *pPage
102a0 20 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52   = PAGE_TO_PGHDR
102a1 31 28 70 50 67 29 3b 0a 20 20 50 67 48 64 72 31  1(pPg);.  PgHdr1
102a2 20 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65   **pp;.  unsigne
102a3 64 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 65  d int h; .  asse
102a4 72 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d  rt( pPage->iKey=
102a5 3d 69 4f 6c 64 20 29 3b 0a 0a 20 20 70 63 61 63  =iOld );..  pcac
102a6 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
102a7 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 43 61  ..  h = iOld%pCa
102a8 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 70  che->nHash;.  pp
102a9 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61   = &pCache->apHa
102aa 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 28 20  sh[h];.  while( 
102ab 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 7b 0a  (*pp)!=pPage ){.
102ac 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d      pp = &(*pp)-
102ad 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 2a 70  >pNext;.  }.  *p
102ae 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74  p = pPage->pNext
102af 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 70 43  ;..  h = iNew%pC
102b0 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70  ache->nHash;.  p
102b1 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4e 65  Page->iKey = iNe
102b2 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e 65 78  w;.  pPage->pNex
102b3 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61  t = pCache->apHa
102b4 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 65 2d  sh[h];.  pCache-
102b5 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61  >apHash[h] = pPa
102b6 67 65 3b 0a 0a 20 20 69 66 28 20 69 4e 65 77 3e  ge;..  if( iNew>
102b7 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
102b8 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69  ){.    pCache->i
102b9 4d 61 78 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20  MaxKey = iNew;. 
102ba 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61   }..  pcache1Lea
102bb 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a  veMutex();.}../*
102bc 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
102bd 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
102be 33 5f 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61  3_pcache.xTrunca
102bf 74 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a  te method. .**.*
102c0 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75 6e  * Discard all un
102c1 70 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e 20  pinned pages in 
102c2 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20 61  the cache with a
102c3 20 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71 75   page number equ
102c4 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 61  al to.** or grea
102c5 74 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65 74  ter than paramet
102c6 65 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70  er iLimit. Any p
102c7 69 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74 68  inned pages with
102c8 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a   a page number.*
102c9 2a 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67 72  * equal to or gr
102ca 65 61 74 65 72 20 74 68 61 6e 20 69 4c 69 6d 69  eater than iLimi
102cb 74 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79  t are implicitly
102cc 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 74   unpinned..*/.st
102cd 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
102ce 31 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65  1Truncate(sqlite
102cf 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73  3_pcache *p, uns
102d0 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74  igned int iLimit
102d1 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  ){.  PCache1 *pC
102d2 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20  ache = (PCache1 
102d3 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e  *)p;.  pcache1En
102d4 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66  terMutex();.  if
102d5 28 20 69 4c 69 6d 69 74 3c 3d 70 43 61 63 68 65  ( iLimit<=pCache
102d6 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20  ->iMaxKey ){.   
102d7 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65   pcache1Truncate
102d8 55 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20 69  Unsafe(pCache, i
102d9 4c 69 6d 69 74 29 3b 0a 20 20 20 20 70 43 61 63  Limit);.    pCac
102da 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4c  he->iMaxKey = iL
102db 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a 20 20 70 63  imit-1;.  }.  pc
102dc 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
102dd 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
102de 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
102df 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
102e0 2e 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64  .xDestroy method
102e1 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f 79  . .**.** Destroy
102e2 20 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74   a cache allocat
102e3 65 64 20 75 73 69 6e 67 20 70 63 61 63 68 65 31  ed using pcache1
102e4 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a 73 74 61  Create()..*/.sta
102e5 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
102e6 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f  Destroy(sqlite3_
102e7 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 50 43  pcache *p){.  PC
102e8 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20  ache1 *pCache = 
102e9 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20  (PCache1 *)p;.  
102ea 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
102eb 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 54 72  x();.  pcache1Tr
102ec 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61  uncateUnsafe(pCa
102ed 63 68 65 2c 20 30 29 3b 0a 20 20 70 63 61 63 68  che, 0);.  pcach
102ee 65 31 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20 70  e1.nMaxPage -= p
102ef 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 70  Cache->nMax;.  p
102f0 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20  cache1.nMinPage 
102f1 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b  -= pCache->nMin;
102f2 0a 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63  .  pcache1Enforc
102f3 65 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 70 63  eMaxPage();.  pc
102f4 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
102f5 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
102f6 65 28 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68  e(pCache->apHash
102f7 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
102f8 65 28 70 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a  e(pCache);.}../*
102f9 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
102fa 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69  n is called duri
102fb 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ng initializatio
102fc 6e 20 28 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  n (sqlite3_initi
102fd 61 6c 69 7a 65 28 29 29 20 74 6f 0a 2a 2a 20 69  alize()) to.** i
102fe 6e 73 74 61 6c 6c 20 74 68 65 20 64 65 66 61 75  nstall the defau
102ff 6c 74 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63  lt pluggable cac
10300 68 65 20 6d 6f 64 75 6c 65 2c 20 61 73 73 75 6d  he module, assum
10301 69 6e 67 20 74 68 65 20 75 73 65 72 20 68 61 73  ing the user has
10302 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20   not.** already 
10303 70 72 6f 76 69 64 65 64 20 61 6e 20 61 6c 74 65  provided an alte
10304 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49  rnative..*/.SQLI
10305 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10306 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74  sqlite3PCacheSet
10307 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20  Default(void){. 
10308 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
10309 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 64  pcache_methods d
1030a 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20  efaultMethods = 
1030b 7b 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  {.    0,        
1030c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1030d 2a 20 70 41 72 67 20 2a 2f 0a 20 20 20 20 70 63  * pArg */.    pc
1030e 61 63 68 65 31 49 6e 69 74 2c 20 20 20 20 20 20  ache1Init,      
1030f 20 20 20 20 20 20 20 2f 2a 20 78 49 6e 69 74 20         /* xInit 
10310 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 53 68  */.    pcache1Sh
10311 75 74 64 6f 77 6e 2c 20 20 20 20 20 20 20 20 20  utdown,         
10312 2f 2a 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f 0a  /* xShutdown */.
10313 20 20 20 20 70 63 61 63 68 65 31 43 72 65 61 74      pcache1Creat
10314 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
10315 78 43 72 65 61 74 65 20 2a 2f 0a 20 20 20 20 70  xCreate */.    p
10316 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 2c  cache1Cachesize,
10317 20 20 20 20 20 20 20 20 2f 2a 20 78 43 61 63 68          /* xCach
10318 65 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63 61  esize */.    pca
10319 63 68 65 31 50 61 67 65 63 6f 75 6e 74 2c 20 20  che1Pagecount,  
1031a 20 20 20 20 20 20 2f 2a 20 78 50 61 67 65 63 6f        /* xPageco
1031b 75 6e 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  unt */.    pcach
1031c 65 31 46 65 74 63 68 2c 20 20 20 20 20 20 20 20  e1Fetch,        
1031d 20 20 20 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f      /* xFetch */
1031e 0a 20 20 20 20 70 63 61 63 68 65 31 55 6e 70 69  .    pcache1Unpi
1031f 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n,            /*
10320 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 70   xUnpin */.    p
10321 63 61 63 68 65 31 52 65 6b 65 79 2c 20 20 20 20  cache1Rekey,    
10322 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6b 65          /* xReke
10323 79 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31  y */.    pcache1
10324 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20  Truncate,       
10325 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a    /* xTruncate *
10326 2f 0a 20 20 20 20 70 63 61 63 68 65 31 44 65 73  /.    pcache1Des
10327 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 2f  troy           /
10328 2a 20 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20 20  * xDestroy */.  
10329 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  };.  sqlite3_con
1032a 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49  fig(SQLITE_CONFI
1032b 47 5f 50 43 41 43 48 45 2c 20 26 64 65 66 61 75  G_PCACHE, &defau
1032c 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23  ltMethods);.}..#
1032d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
1032e 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
1032f 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73  EMENT./*.** This
10330 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
10331 6c 65 64 20 74 6f 20 66 72 65 65 20 73 75 70 65  led to free supe
10332 72 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63 61  rfluous dynamica
10333 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65  lly allocated me
10334 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20  mory.** held by 
10335 74 68 65 20 70 61 67 65 72 20 73 79 73 74 65 6d  the pager system
10336 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73 65 20  . Memory in use 
10337 62 79 20 61 6e 79 20 53 51 4c 69 74 65 20 70 61  by any SQLite pa
10338 67 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a  ger allocated.**
10339 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20   by the current 
1033a 74 68 72 65 61 64 20 6d 61 79 20 62 65 20 73 71  thread may be sq
1033b 6c 69 74 65 33 5f 66 72 65 65 28 29 65 64 2e 0a  lite3_free()ed..
1033c 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74 68  **.** nReq is th
1033d 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1033e 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71 75  s of memory requ
1033f 69 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73 20  ired. Once this 
10340 6d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65 6e  much has.** been
10341 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20 66   released, the f
10342 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e  unction returns.
10343 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
10344 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  e is the total n
10345 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 20 62 79 74  umber .** of byt
10346 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 6c  es of memory rel
10347 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eased..*/.SQLITE
10348 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10349 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
1034a 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65 71  eMemory(int nReq
1034b 29 7b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d  ){.  int nFree =
1034c 20 30 3b 0a 20 20 69 66 28 20 70 63 61 63 68 65   0;.  if( pcache
1034d 31 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b 0a 20  1.pStart==0 ){. 
1034e 20 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20     PgHdr1 *p;.  
1034f 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10350 74 65 78 28 29 3b 0a 20 20 20 20 77 68 69 6c 65  tex();.    while
10351 28 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 72  ( (nReq<0 || nFr
10352 65 65 3c 6e 52 65 71 29 20 26 26 20 28 70 3d 70  ee<nReq) && (p=p
10353 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 29  cache1.pLruTail)
10354 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 20   ){.      nFree 
10355 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  += sqlite3Malloc
10356 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 70  Size(p);.      p
10357 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29  cache1PinPage(p)
10358 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 52  ;.      pcache1R
10359 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 29  emoveFromHash(p)
1035a 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 46  ;.      pcache1F
1035b 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 20 20  reePage(p);.    
1035c 7d 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61  }.    pcache1Lea
1035d 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20  veMutex();.  }. 
1035e 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d   return nFree;.}
1035f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
10360 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
10361 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 23  MANAGEMENT */..#
10362 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
10363 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  T./*.** This fun
10364 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79  ction is used by
10365 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73   test procedures
10366 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20   to inspect the 
10367 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0a 2a  internal state.*
10368 2a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20  * of the global 
10369 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cache..*/.SQLITE
1036a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1036b 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74 73  lite3PcacheStats
1036c 28 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72 65  (.  int *pnCurre
1036d 6e 74 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  nt,      /* OUT:
1036e 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
1036f 20 70 61 67 65 73 20 63 61 63 68 65 64 20 2a 2f   pages cached */
10370 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c 20 20  .  int *pnMax,  
10371 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
10372 47 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 63  Global maximum c
10373 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69  ache size */.  i
10374 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 20 20 20 20  nt *pnMin,      
10375 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 75 6d 20      /* OUT: Sum 
10376 6f 66 20 50 43 61 63 68 65 31 2e 6e 4d 69 6e 20  of PCache1.nMin 
10377 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 61  for purgeable ca
10378 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ches */.  int *p
10379 6e 52 65 63 79 63 6c 61 62 6c 65 20 20 20 20 2f  nRecyclable    /
1037a 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d  * OUT: Total num
1037b 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 76 61  ber of pages ava
1037c 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 63 79 63  ilable for recyc
1037d 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 67 48  ling */.){.  PgH
1037e 64 72 31 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 52  dr1 *p;.  int nR
1037f 65 63 79 63 6c 61 62 6c 65 20 3d 20 30 3b 0a 20  ecyclable = 0;. 
10380 20 66 6f 72 28 70 3d 70 63 61 63 68 65 31 2e 70   for(p=pcache1.p
10381 4c 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d  LruHead; p; p=p-
10382 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 20  >pLruNext){.    
10383 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20  nRecyclable++;. 
10384 20 7d 0a 20 20 2a 70 6e 43 75 72 72 65 6e 74 20   }.  *pnCurrent 
10385 3d 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65  = pcache1.nCurre
10386 6e 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 78  ntPage;.  *pnMax
10387 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50   = pcache1.nMaxP
10388 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d 20  age;.  *pnMin = 
10389 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65  pcache1.nMinPage
1038a 3b 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c  ;.  *pnRecyclabl
1038b 65 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b  e = nRecyclable;
1038c 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  .}.#endif../****
1038d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1038e 66 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a  f pcache1.c ****
1038f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10391 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
10392 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
10393 20 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20 2a   file rowset.c *
10394 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10395 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10396 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
10397 20 32 30 30 38 20 44 65 63 65 6d 62 65 72 20 33   2008 December 3
10398 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
10399 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1039a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1039b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1039c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1039d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1039e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1039f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
103a0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
103a1 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
103a2 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
103a3 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
103a4 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
103a5 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
103a6 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
103a7 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
103a8 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
103a9 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
103aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
103ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
103ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
103ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
103ae 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c  **.** This modul
103af 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20  e implements an 
103b0 6f 62 6a 65 63 74 20 77 65 20 63 61 6c 6c 20 61  object we call a
103b1 20 22 52 6f 77 20 53 65 74 22 2e 0a 2a 2a 0a 2a   "Row Set"..**.*
103b2 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a  * The RowSet obj
103b3 65 63 74 20 69 73 20 61 20 62 61 67 20 6f 66 20  ect is a bag of 
103b4 72 6f 77 69 64 73 2e 20 20 52 6f 77 69 64 73 0a  rowids.  Rowids.
103b5 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
103b6 69 6e 74 6f 20 74 68 65 20 62 61 67 20 69 6e 20  into the bag in 
103b7 61 6e 20 61 72 62 69 74 72 61 72 79 20 6f 72 64  an arbitrary ord
103b8 65 72 2e 20 20 54 68 65 6e 20 74 68 65 79 20 61  er.  Then they a
103b9 72 65 0a 2a 2a 20 70 75 6c 6c 65 64 20 66 72 6f  re.** pulled fro
103ba 6d 20 74 68 65 20 62 61 67 20 69 6e 20 73 6f 72  m the bag in sor
103bb 74 65 64 20 6f 72 64 65 72 2e 20 20 52 6f 77 69  ted order.  Rowi
103bc 64 73 20 6f 6e 6c 79 20 61 70 70 65 61 72 20 69  ds only appear i
103bd 6e 20 74 68 65 0a 2a 2a 20 62 61 67 20 6f 6e 63  n the.** bag onc
103be 65 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20  e.  If the same 
103bf 72 6f 77 69 64 20 69 73 20 69 6e 73 65 72 74 65  rowid is inserte
103c0 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
103c1 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20  , the.** second 
103c2 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 69  and subsequent i
103c3 6e 73 65 72 74 73 20 6d 61 6b 65 20 6e 6f 20 64  nserts make no d
103c4 69 66 66 65 72 65 6e 63 65 20 6f 6e 20 74 68 65  ifference on the
103c5 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 54   output..**.** T
103c6 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
103c7 6f 6e 20 61 63 63 75 6d 75 6c 61 74 65 73 20 72  on accumulates r
103c8 6f 77 69 64 73 20 69 6e 20 61 20 6c 69 6e 6b 65  owids in a linke
103c9 64 20 6c 69 73 74 2e 20 20 46 6f 72 0a 2a 2a 20  d list.  For.** 
103ca 6f 75 74 70 75 74 2c 20 69 74 20 66 69 72 73 74  output, it first
103cb 20 73 6f 72 74 73 20 74 68 65 20 6c 69 6e 6b 65   sorts the linke
103cc 64 20 6c 69 73 74 20 28 72 65 6d 6f 76 69 6e 67  d list (removing
103cd 20 64 75 70 6c 69 63 61 74 65 73 20 64 75 72 69   duplicates duri
103ce 6e 67 0a 2a 2a 20 74 68 65 20 73 6f 72 74 29 20  ng.** the sort) 
103cf 74 68 65 6e 20 72 65 74 75 72 6e 73 20 65 6c 65  then returns ele
103d0 6d 65 6e 74 73 20 6f 6e 65 20 62 79 20 6f 6e 65  ments one by one
103d1 20 62 79 20 77 61 6c 6b 69 6e 67 20 74 68 65 20   by walking the 
103d2 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 42 69 67 20  list..**.** Big 
103d3 63 68 75 6e 6b 73 20 6f 66 20 72 6f 77 69 64 2f  chunks of rowid/
103d4 6e 65 78 74 2d 70 74 72 20 70 61 69 72 73 20 61  next-ptr pairs a
103d5 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20  re allocated at 
103d6 61 20 74 69 6d 65 2c 20 74 6f 0a 2a 2a 20 72 65  a time, to.** re
103d7 64 75 63 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20  duce the malloc 
103d8 6f 76 65 72 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  overhead..**.** 
103d9 24 49 64 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20  $Id: rowset.c,v 
103da 31 2e 33 20 32 30 30 39 2f 30 31 2f 31 33 20 32  1.3 2009/01/13 2
103db 30 3a 31 34 3a 31 36 20 64 72 68 20 45 78 70 20  0:14:16 drh Exp 
103dc 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
103dd 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74  number of rowset
103de 20 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c   entries per all
103df 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a  ocation chunk..*
103e0 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54  /.#define ROWSET
103e1 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b  _ENTRY_PER_CHUNK
103e2 20 20 36 33 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68    63../*.** Each
103e3 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f 77 53   entry in a RowS
103e4 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  et is an instanc
103e5 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
103e6 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 3a  ng.** structure:
103e7 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65  .*/.struct RowSe
103e8 74 45 6e 74 72 79 20 7b 20 20 20 20 20 20 20 20  tEntry {        
103e9 20 20 20 20 0a 20 20 69 36 34 20 76 3b 20 20 20      .  i64 v;   
103ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
103eb 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 76 61       /* ROWID va
103ec 6c 75 65 20 66 6f 72 20 74 68 69 73 20 65 6e 74  lue for this ent
103ed 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ry */.  struct R
103ee 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4e 65 78  owSetEntry *pNex
103ef 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e  t;    /* Next en
103f0 74 72 79 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66  try on a list of
103f1 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 2a 2f 0a   all entries */.
103f2 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 65 78 20  };../*.** Index 
103f3 65 6e 74 72 69 65 73 20 61 72 65 20 61 6c 6c 6f  entries are allo
103f4 63 61 74 65 64 20 69 6e 20 6c 61 72 67 65 20 63  cated in large c
103f5 68 75 6e 6b 73 20 28 69 6e 73 74 61 6e 63 65 73  hunks (instances
103f6 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f   of the.** follo
103f7 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 29 20  wing structure) 
103f8 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79  to reduce memory
103f9 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 76 65 72   allocation over
103fa 68 65 61 64 2e 20 20 54 68 65 0a 2a 2a 20 63 68  head.  The.** ch
103fb 75 6e 6b 73 20 61 72 65 20 6b 65 70 74 20 6f 6e  unks are kept on
103fc 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73   a linked list s
103fd 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20  o that they can 
103fe 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 0a 2a  be deallocated.*
103ff 2a 20 77 68 65 6e 20 74 68 65 20 52 6f 77 53 65  * when the RowSe
10400 74 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0a  t is destroyed..
10401 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74  */.struct RowSet
10402 43 68 75 6e 6b 20 7b 0a 20 20 73 74 72 75 63 74  Chunk {.  struct
10403 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e   RowSetChunk *pN
10404 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
10405 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 6f   /* Next chunk o
10406 6e 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61  n list of them a
10407 6c 6c 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ll */.  struct R
10408 6f 77 53 65 74 45 6e 74 72 79 20 61 45 6e 74 72  owSetEntry aEntr
10409 79 5b 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50  y[ROWSET_ENTRY_P
1040a 45 52 5f 43 48 55 4e 4b 5d 3b 20 2f 2a 20 41 6c  ER_CHUNK]; /* Al
1040b 6c 6f 63 61 74 65 64 20 65 6e 74 72 69 65 73 20  located entries 
1040c 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 52  */.};../*.** A R
1040d 6f 77 53 65 74 20 69 6e 20 61 6e 20 69 6e 73 74  owSet in an inst
1040e 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
1040f 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
10410 0a 2a 2a 0a 2a 2a 20 41 20 74 79 70 65 64 65 66  .**.** A typedef
10411 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
10412 72 65 20 69 66 20 66 6f 75 6e 64 20 69 6e 20 73  re if found in s
10413 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 73  qliteInt.h..*/.s
10414 74 72 75 63 74 20 52 6f 77 53 65 74 20 7b 0a 20  truct RowSet {. 
10415 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68   struct RowSetCh
10416 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20  unk *pChunk;    
10417 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 63  /* List of all c
10418 68 75 6e 6b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  hunk allocations
10419 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
1041a 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
1041b 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1041c 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1041d 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
1041e 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b  etEntry *pEntry;
1041f 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65      /* List of e
10420 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 6f  ntries in the ro
10421 77 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63 74  wset */.  struct
10422 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c   RowSetEntry *pL
10423 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74  ast;     /* Last
10424 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 45   entry on the pE
10425 6e 74 72 79 20 6c 69 73 74 20 2a 2f 0a 20 20 73  ntry list */.  s
10426 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
10427 79 20 2a 70 46 72 65 73 68 3b 20 20 20 20 2f 2a  y *pFresh;    /*
10428 20 53 6f 75 72 63 65 20 6f 66 20 6e 65 77 20 65   Source of new e
10429 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 2a 2f 0a  ntry objects */.
1042a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20 20 20    u16 nFresh;   
1042b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1042c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62   /* Number of ob
1042d 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73 68 20  jects on pFresh 
1042e 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74 65 64  */.  u8 isSorted
1042f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10430 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63      /* True if c
10431 6f 6e 74 65 6e 74 20 69 73 20 73 6f 72 74 65 64  ontent is sorted
10432 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75   */.};../*.** Tu
10433 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69  rn bulk memory i
10434 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f 62 6a  nto a RowSet obj
10435 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20 6f 66  ect.  N bytes of
10436 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61   memory.** are a
10437 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53 70 61  vailable at pSpa
10438 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f 69 6e  ce.  The db poin
10439 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20 61  ter is used as a
1043a 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a   memory context.
1043b 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 73 65  ** for any subse
1043c 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e  quent allocation
1043d 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 6f  s that need to o
1043e 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  ccur..** Return 
1043f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
10440 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 6a 65   new RowSet obje
10441 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73  ct..**.** It mus
10442 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  t be the case th
10443 61 74 20 4e 20 69 73 20 73 75 66 66 69 63 69 65  at N is sufficie
10444 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77  nt to make a Row
10445 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20  set.  If not.** 
10446 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75  an assertion fau
10447 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a  lt occurs..** .*
10448 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67 65 72  * If N is larger
10449 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75   than the minimu
1044a 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72 70 6c  m, use the surpl
1044b 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69 61 6c  us as an initial
1044c 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  .** allocation o
1044d 66 20 65 6e 74 72 69 65 73 20 61 76 61 69 6c 61  f entries availa
1044e 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c 65 64  ble to be filled
1044f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10450 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69  ATE RowSet *sqli
10451 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71  te3RowSetInit(sq
10452 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
10453 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67 6e 65  *pSpace, unsigne
10454 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53  d int N){.  RowS
10455 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  et *p;.  assert(
10456 20 4e 20 3e 3d 20 73 69 7a 65 6f 66 28 2a 70 29   N >= sizeof(*p)
10457 20 29 3b 0a 20 20 70 20 3d 20 70 53 70 61 63 65   );.  p = pSpace
10458 3b 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20  ;.  p->pChunk = 
10459 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  0;.  p->db = db;
1045a 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30  .  p->pEntry = 0
1045b 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30  ;.  p->pLast = 0
1045c 3b 0a 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20  ;.  p->pFresh = 
1045d 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e  (struct RowSetEn
1045e 74 72 79 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d  try*)&p[1];.  p-
1045f 3e 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 28  >nFresh = (u16)(
10460 28 4e 20 2d 20 73 69 7a 65 6f 66 28 2a 70 29 29  (N - sizeof(*p))
10461 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 52  /sizeof(struct R
10462 6f 77 53 65 74 45 6e 74 72 79 29 29 3b 0a 20 20  owSetEntry));.  
10463 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b  p->isSorted = 1;
10464 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
10465 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  /*.** Deallocate
10466 20 61 6c 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d   all chunks from
10467 20 61 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51   a RowSet..*/.SQ
10468 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10469 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43  d sqlite3RowSetC
1046a 6c 65 61 72 28 52 6f 77 53 65 74 20 2a 70 29 7b  lear(RowSet *p){
1046b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
1046c 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a  Chunk *pChunk, *
1046d 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 66 6f  pNextChunk;.  fo
1046e 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75  r(pChunk=p->pChu
1046f 6e 6b 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68 75  nk; pChunk; pChu
10470 6e 6b 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29  nk = pNextChunk)
10471 7b 0a 20 20 20 20 70 4e 65 78 74 43 68 75 6e 6b  {.    pNextChunk
10472 20 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74   = pChunk->pNext
10473 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
10474 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e  ree(p->db, pChun
10475 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68  k);.  }.  p->pCh
10476 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46  unk = 0;.  p->nF
10477 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70  resh = 0;.  p->p
10478 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e  Entry = 0;.  p->
10479 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e  pLast = 0;.  p->
1047a 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a  isSorted = 1;.}.
1047b 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
1047c 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61  new value into a
1047d 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54   RowSet..**.** T
1047e 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
1047f 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 61  flag of the data
10480 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
10481 69 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d  is set if a.** m
10482 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
10483 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54   fails..*/.SQLIT
10484 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
10485 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65  qlite3RowSetInse
10486 72 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36  rt(RowSet *p, i6
10487 34 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 75  4 rowid){.  stru
10488 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
10489 70 45 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74  pEntry;.  struct
1048a 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c   RowSetEntry *pL
1048b 61 73 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  ast;.  if( p==0 
1048c 29 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 4d 75  ) return;  /* Mu
1048d 73 74 20 68 61 76 65 20 62 65 65 6e 20 61 20 6d  st have been a m
1048e 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f  alloc failure */
1048f 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 65 73 68  .  if( p->nFresh
10490 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63  ==0 ){.    struc
10491 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70  t RowSetChunk *p
10492 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20  New;.    pNew = 
10493 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
10494 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a 65 6f 66  aw(p->db, sizeof
10495 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 69 66  (*pNew));.    if
10496 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20  ( pNew==0 ){.   
10497 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
10498 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74  .    pNew->pNext
10499 20 3d 20 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20   = p->pChunk;.  
1049a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e    p->pChunk = pN
1049b 65 77 3b 0a 20 20 20 20 70 2d 3e 70 46 72 65 73  ew;.    p->pFres
1049c 68 20 3d 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79  h = pNew->aEntry
1049d 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 65 73 68 20  ;.    p->nFresh 
1049e 3d 20 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50  = ROWSET_ENTRY_P
1049f 45 52 5f 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20  ER_CHUNK;.  }.  
104a0 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 46 72 65  pEntry = p->pFre
104a1 73 68 2b 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73  sh++;.  p->nFres
104a2 68 2d 2d 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76  h--;.  pEntry->v
104a3 20 3d 20 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74   = rowid;.  pEnt
104a4 72 79 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20  ry->pNext = 0;. 
104a5 20 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73   pLast = p->pLas
104a6 74 3b 0a 20 20 69 66 28 20 70 4c 61 73 74 20 29  t;.  if( pLast )
104a7 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 73 53  {.    if( p->isS
104a8 6f 72 74 65 64 20 26 26 20 72 6f 77 69 64 3c 3d  orted && rowid<=
104a9 70 4c 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20  pLast->v ){.    
104aa 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20    p->isSorted = 
104ab 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 61  0;.    }.    pLa
104ac 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 45 6e 74  st->pNext = pEnt
104ad 72 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ry;.  }else{.   
104ae 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74   assert( p->pEnt
104af 72 79 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e  ry==0 );.    p->
104b0 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 3b  pEntry = pEntry;
104b1 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20  .  }.  p->pLast 
104b2 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a  = pEntry;.}../*.
104b3 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 73  ** Merge two lis
104b4 74 73 20 6f 66 20 52 6f 77 53 65 74 20 65 6e 74  ts of RowSet ent
104b5 72 69 65 73 2e 20 20 52 65 6d 6f 76 65 20 64 75  ries.  Remove du
104b6 70 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  plicates..**.** 
104b7 54 68 65 20 69 6e 70 75 74 20 6c 69 73 74 73 20  The input lists 
104b8 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62  are assumed to b
104b9 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  e in sorted orde
104ba 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  r..*/.static str
104bb 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
104bc 2a 62 6f 6f 6c 69 64 78 4d 65 72 67 65 28 0a 20  *boolidxMerge(. 
104bd 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
104be 74 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46  try *pA,    /* F
104bf 69 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74  irst sorted list
104c0 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f   to be merged */
104c1 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
104c2 45 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a  Entry *pB     /*
104c3 20 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c   Second sorted l
104c4 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64  ist to be merged
104c5 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
104c6 52 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 64  RowSetEntry head
104c7 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  ;.  struct RowSe
104c8 74 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a  tEntry *pTail;..
104c9 20 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b    pTail = &head;
104ca 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20  .  while( pA && 
104cb 70 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  pB ){.    assert
104cc 28 20 70 41 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c  ( pA->pNext==0 |
104cd 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 4e 65  | pA->v<=pA->pNe
104ce 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73 73  xt->v );.    ass
104cf 65 72 74 28 20 70 42 2d 3e 70 4e 65 78 74 3d 3d  ert( pB->pNext==
104d0 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e  0 || pB->v<=pB->
104d1 70 4e 65 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20  pNext->v );.    
104d2 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20  if( pA->v<pB->v 
104d3 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e  ){.      pTail->
104d4 70 4e 65 78 74 20 3d 20 70 41 3b 0a 20 20 20 20  pNext = pA;.    
104d5 20 20 70 41 20 3d 20 70 41 2d 3e 70 4e 65 78 74    pA = pA->pNext
104d6 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20  ;.      pTail = 
104d7 70 54 61 69 6c 2d 3e 70 4e 65 78 74 3b 0a 20 20  pTail->pNext;.  
104d8 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d 3e    }else if( pB->
104d9 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 20 20 20  v<pA->v ){.     
104da 20 70 54 61 69 6c 2d 3e 70 4e 65 78 74 20 3d 20   pTail->pNext = 
104db 70 42 3b 0a 20 20 20 20 20 20 70 42 20 3d 20 70  pB;.      pB = p
104dc 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  B->pNext;.      
104dd 70 54 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70  pTail = pTail->p
104de 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Next;.    }else{
104df 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e  .      pA = pA->
104e0 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  pNext;.    }.  }
104e1 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20  .  if( pA ){.   
104e2 20 61 73 73 65 72 74 28 20 70 41 2d 3e 70 4e 65   assert( pA->pNe
104e3 78 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d  xt==0 || pA->v<=
104e4 70 41 2d 3e 70 4e 65 78 74 2d 3e 76 20 29 3b 0a  pA->pNext->v );.
104e5 20 20 20 20 70 54 61 69 6c 2d 3e 70 4e 65 78 74      pTail->pNext
104e6 20 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a   = pA;.  }else{.
104e7 20 20 20 20 61 73 73 65 72 74 28 20 70 42 3d 3d      assert( pB==
104e8 30 20 7c 7c 20 70 42 2d 3e 70 4e 65 78 74 3d 3d  0 || pB->pNext==
104e9 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e  0 || pB->v<=pB->
104ea 70 4e 65 78 74 2d 3e 76 20 29 3b 0a 20 20 20 20  pNext->v );.    
104eb 70 54 61 69 6c 2d 3e 70 4e 65 78 74 20 3d 20 70  pTail->pNext = p
104ec 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  B;.  }.  return 
104ed 68 65 61 64 2e 70 4e 65 78 74 3b 0a 7d 0a 0a 2f  head.pNext;.}../
104ee 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c  *.** Sort all el
104ef 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 52 6f  ements of the Ro
104f0 77 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e 64  wSet into ascend
104f1 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73  ing order..*/ .s
104f2 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
104f3 65 33 52 6f 77 53 65 74 53 6f 72 74 28 52 6f 77  e3RowSetSort(Row
104f4 53 65 74 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67  Set *p){.  unsig
104f5 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  ned int i;.  str
104f6 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
104f7 2a 70 45 6e 74 72 79 3b 0a 20 20 73 74 72 75 63  *pEntry;.  struc
104f8 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 61  t RowSetEntry *a
104f9 42 75 63 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61  Bucket[40];..  a
104fa 73 73 65 72 74 28 20 70 2d 3e 69 73 53 6f 72 74  ssert( p->isSort
104fb 65 64 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65  ed==0 );.  memse
104fc 74 28 61 42 75 63 6b 65 74 2c 20 30 2c 20 73 69  t(aBucket, 0, si
104fd 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 29 3b 0a  zeof(aBucket));.
104fe 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74    while( p->pEnt
104ff 72 79 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79  ry ){.    pEntry
10500 20 3d 20 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20   = p->pEntry;.  
10501 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45    p->pEntry = pE
10502 6e 74 72 79 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ntry->pNext;.   
10503 20 70 45 6e 74 72 79 2d 3e 70 4e 65 78 74 20 3d   pEntry->pNext =
10504 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   0;.    for(i=0;
10505 20 61 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b   aBucket[i]; i++
10506 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20  ){.      pEntry 
10507 3d 20 62 6f 6f 6c 69 64 78 4d 65 72 67 65 28 61  = boolidxMerge(a
10508 42 75 63 6b 65 74 5b 69 5d 2c 70 45 6e 74 72 79  Bucket[i],pEntry
10509 29 3b 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74  );.      aBucket
1050a 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  [i] = 0;.    }. 
1050b 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20     aBucket[i] = 
1050c 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45  pEntry;.  }.  pE
1050d 6e 74 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28  ntry = 0;.  for(
1050e 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42  i=0; i<sizeof(aB
1050f 75 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42  ucket)/sizeof(aB
10510 75 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b  ucket[0]); i++){
10511 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 62 6f  .    pEntry = bo
10512 6f 6c 69 64 78 4d 65 72 67 65 28 70 45 6e 74 72  olidxMerge(pEntr
10513 79 2c 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20  y,aBucket[i]);. 
10514 20 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d   }.  p->pEntry =
10515 20 70 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c   pEntry;.  p->pL
10516 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73  ast = 0;.  p->is
10517 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f  Sorted = 1;.}../
10518 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
10519 20 6e 65 78 74 20 28 73 6d 61 6c 6c 65 73 74 29   next (smallest)
1051a 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68   element from th
1051b 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69  e RowSet..** Wri
1051c 74 65 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 69  te the element i
1051d 6e 74 6f 20 2a 70 52 6f 77 69 64 2e 20 20 52 65  nto *pRowid.  Re
1051e 74 75 72 6e 20 31 20 6f 6e 20 73 75 63 63 65 73  turn 1 on succes
1051f 73 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20  s.  Return.** 0 
10520 69 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 73  if the RowSet is
10521 20 61 6c 72 65 61 64 79 20 65 6d 70 74 79 2e 0a   already empty..
10522 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
10523 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77  E int sqlite3Row
10524 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 20 2a  SetNext(RowSet *
10525 70 2c 20 69 36 34 20 2a 70 52 6f 77 69 64 29 7b  p, i64 *pRowid){
10526 0a 20 20 69 66 28 20 21 70 2d 3e 69 73 53 6f 72  .  if( !p->isSor
10527 74 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ted ){.    sqlit
10528 65 33 52 6f 77 53 65 74 53 6f 72 74 28 70 29 3b  e3RowSetSort(p);
10529 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 45  .  }.  if( p->pE
1052a 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a 70 52 6f  ntry ){.    *pRo
1052b 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d  wid = p->pEntry-
1052c 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72  >v;.    p->pEntr
1052d 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 70  y = p->pEntry->p
1052e 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 2d  Next;.    if( p-
1052f 3e 70 45 6e 74 72 79 3d 3d 30 20 29 7b 0a 20 20  >pEntry==0 ){.  
10530 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65      sqlite3RowSe
10531 74 43 6c 65 61 72 28 70 29 3b 0a 20 20 20 20 7d  tClear(p);.    }
10532 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
10533 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
10534 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a  rn 0;.  }.}../**
10535 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
10536 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a   of rowset.c ***
10537 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1053a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1053b 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63 20  in file pager.c 
1053c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1053d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1053e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1053f 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
10540 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
10541 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
10542 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
10543 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
10544 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
10545 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
10546 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
10547 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
10548 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
10549 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
1054a 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
1054b 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
1054c 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
1054d 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
1054e 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
1054f 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
10550 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
10551 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
10552 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10553 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10556 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ***.** This is t
10557 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
10558 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61  n of the page ca
10559 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f 72  che subsystem or
1055a 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a   "pager"..** .**
1055b 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75 73   The pager is us
1055c 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20 64  ed to access a d
1055d 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69 6c  atabase disk fil
1055e 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74  e.  It implement
1055f 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d  s.** atomic comm
10560 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20  it and rollback 
10561 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65 20  through the use 
10562 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  of a journal fil
10563 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65 70  e that.** is sep
10564 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 64  arate from the d
10565 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
10566 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69 6d  he pager also im
10567 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a  plements file.**
10568 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 76   locking to prev
10569 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73 65  ent two processe
1056a 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 74  s from writing t
1056b 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
1056c 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 61  .** file simulta
1056d 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20  neously, or one 
1056e 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 61  process from rea
1056f 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ding the databas
10570 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68  e while.** anoth
10571 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a  er is writing..*
10572 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70  *.** @(#) $Id: p
10573 61 67 65 72 2e 63 2c 76 20 31 2e 35 37 36 20 32  ager.c,v 1.576 2
10574 30 30 39 2f 30 33 2f 33 31 20 30 32 3a 35 34 3a  009/03/31 02:54:
10575 34 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  40 drh Exp $.*/.
10576 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
10577 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a  MIT_DISKIO../*.*
10578 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 72 6f  * Macros for tro
10579 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 20 4e  ubleshooting.  N
1057a 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f  ormally turned o
1057b 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20  ff.*/.#if 0.int 
1057c 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63  sqlite3PagerTrac
1057d 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f  e=1;  /* True to
1057e 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20   enable tracing 
1057f 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  */.#define sqlit
10580 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 70 72  e3DebugPrintf pr
10581 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41 47  intf.#define PAG
10582 45 52 54 52 41 43 45 28 58 29 20 20 20 20 20 69  ERTRACE(X)     i
10583 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 54  f( sqlite3PagerT
10584 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 44  race ){ sqlite3D
10585 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20 7d 0a  ebugPrintf X; }.
10586 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50 41  #else.#define PA
10587 47 45 52 54 52 41 43 45 28 58 29 0a 23 65 6e 64  GERTRACE(X).#end
10588 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
10589 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 72  llowing two macr
1058a 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 74 68  os are used with
1058b 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 41 43  in the PAGERTRAC
1058c 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76 65  E() macros above
1058d 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75 74  .** to print out
1058e 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72   file-descriptor
1058f 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52 49  s. .**.** PAGERI
10590 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f 69 6e  D() takes a poin
10591 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 73  ter to a Pager s
10592 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67  truct as its arg
10593 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 61 73  ument. The.** as
10594 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d 64 65  sociated file-de
10595 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 74 75  scriptor is retu
10596 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 4c 45  rned. FILEHANDLE
10597 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 73 71  ID() takes an sq
10598 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 73 74  lite3_file.** st
10599 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67 75  ruct as its argu
1059a 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ment..*/.#define
1059b 20 50 41 47 45 52 49 44 28 70 29 20 28 28 69 6e   PAGERID(p) ((in
1059c 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 66 69  t)(p->fd)).#defi
1059d 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28  ne FILEHANDLEID(
1059e 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a 0a 2f  fd) ((int)fd)../
1059f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63 61  *.** The page ca
105a0 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 20 69  che as a whole i
105a1 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e 65 20  s always in one 
105a2 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
105a3 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a  .** states:.**.*
105a4 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  *   PAGER_UNLOCK
105a5 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67 65          The page
105a6 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 63 75   cache is not cu
105a7 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e 67 20  rrently reading 
105a8 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  or .**          
105a9 20 20 20 20 20 20 20 20 20 20 20 20 20 77 72 69               wri
105aa 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ting the databas
105ab 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 20 69  e file.  There i
105ac 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  s no.**         
105ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
105ae 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72  ta held in memor
105af 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  y.  This is the 
105b0 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 20 20  initial.**      
105b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
105b2 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20   state..**.**   
105b3 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 20 20  PAGER_SHARED    
105b4 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63      The page cac
105b5 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 74 68  he is reading th
105b6 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20  e database..**  
105b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
105b8 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 73 20       Writing is 
105b9 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e 20 20  not permitted.  
105ba 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20  There can be.** 
105bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
105bc 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 20 72        multiple r
105bd 65 61 64 65 72 73 20 61 63 63 65 73 73 69 6e 67  eaders accessing
105be 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
105bf 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  se.**           
105c0 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65              file
105c1 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
105c2 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52  e..**.**   PAGER
105c3 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 54  _RESERVED      T
105c4 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 73 20  his process has 
105c5 72 65 73 65 72 76 65 64 20 74 68 65 20 64 61 74  reserved the dat
105c6 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 69 6e  abase for writin
105c7 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  g.**            
105c8 20 20 20 20 20 20 20 20 20 20 20 62 75 74 20 68             but h
105c9 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20  as not yet made 
105ca 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 4f 6e  any changes.  On
105cb 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 0a 2a  ly one process.*
105cc 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
105cd 20 20 20 20 20 20 20 20 61 74 20 61 20 74 69 6d          at a tim
105ce 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 74 68  e can reserve th
105cf 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
105d0 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 20   original.**    
105d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
105d2 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65     database file
105d3 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 6d 6f   has not been mo
105d4 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 72 0a  dified so other.
105d5 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
105d6 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 73 73           process
105d7 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20  es may still be 
105d8 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e 2d 64  reading the on-d
105d9 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  isk.**          
105da 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74               dat
105db 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
105dc 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c 55 53  *   PAGER_EXCLUS
105dd 49 56 45 20 20 20 20 20 54 68 65 20 70 61 67 65  IVE     The page
105de 20 63 61 63 68 65 20 69 73 20 77 72 69 74 69 6e   cache is writin
105df 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  g the database..
105e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
105e1 20 20 20 20 20 20 20 20 20 41 63 63 65 73 73 20           Access 
105e2 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 4e  is exclusive.  N
105e3 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  o other processe
105e4 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  s or.**         
105e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
105e6 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 65 61  reads can be rea
105e7 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20  ding or writing 
105e8 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20 20  while one.**    
105e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
105ea 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 77 72     process is wr
105eb 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50  iting..**.**   P
105ec 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20 20  AGER_SYNCED     
105ed 20 20 20 54 68 65 20 70 61 67 65 72 20 6d 6f 76     The pager mov
105ee 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 74 65  es to this state
105ef 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 43 4c   from PAGER_EXCL
105f0 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 20 20  USIVE.**        
105f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
105f2 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 20 70  fter all dirty p
105f3 61 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 77  ages have been w
105f4 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a  ritten to the.**
105f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
105f6 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20         database 
105f7 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 6c  file and the fil
105f8 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e 63 65  e has been synce
105f9 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  d to.**         
105fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69                di
105fb 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 65 6d  sk. All that rem
105fc 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 74 6f  ains to do is to
105fd 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20 20   remove or.**   
105fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
105ff 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 68 65      truncate the
10600 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e   journal file an
10601 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  d the transactio
10602 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n .**           
10603 20 20 20 20 20 20 20 20 20 20 20 20 77 69 6c 6c              will
10604 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a   be committed..*
10605 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63 61  *.** The page ca
10606 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 6e 20  che comes up in 
10607 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54  PAGER_UNLOCK.  T
10608 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 0a  he first time a.
10609 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  ** sqlite3PagerG
1060a 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 68 65  et() occurs, the
1060b 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f   state transitio
1060c 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52  ns to PAGER_SHAR
1060d 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c  ED..** After all
1060e 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e   pages have been
1060f 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20   released using 
10610 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e 72 65  sqlite_page_unre
10611 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 61 74  f(),.** the stat
10612 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 61  e transitions ba
10613 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e 4c 4f  ck to PAGER_UNLO
10614 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 20 74  CK.  The first t
10615 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69  ime.** that sqli
10616 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
10617 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 73  is called, the s
10618 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73  tate transitions
10619 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53   to.** PAGER_RES
1061a 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 74 68  ERVED.  (Note th
1061b 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  at sqlite3PagerW
1061c 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c 79 20  rite() can only 
1061d 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20  be.** called on 
1061e 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  an outstanding p
1061f 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e 73 20  age which means 
10620 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 6d  that the pager m
10621 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 41 47  ust.** be in PAG
10622 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f 72 65  ER_SHARED before
10623 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e 73 20   it transitions 
10624 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 56 45  to PAGER_RESERVE
10625 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 45 53  D.).** PAGER_RES
10626 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 61 74  ERVED means that
10627 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f 70 65   there is an ope
10628 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  n rollback journ
10629 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73  al..** The trans
1062a 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 5f 45  ition to PAGER_E
1062b 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 73 20  XCLUSIVE occurs 
1062c 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67  before any chang
1062d 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74  es.** are made t
1062e 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
1062f 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 69 74  ile, though writ
10630 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61  es to the rollba
10631 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63  ck.** journal oc
10632 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 20 50  curs with just P
10633 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 20  AGER_RESERVED.  
10634 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 65 33  After an sqlite3
10635 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 0a  PagerRollback().
10636 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 61 67  ** or sqlite3Pag
10637 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
10638 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 63 61  (), the state ca
10639 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 41 47  n go back to PAG
1063a 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 6f 72  ER_SHARED,.** or
1063b 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 74 20   it can stay at 
1063c 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
1063d 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 78 63  if we are in exc
1063e 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 6d 6f  lusive access mo
1063f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  de..*/.#define P
10640 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20  AGER_UNLOCK     
10641 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52   0.#define PAGER
10642 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 20 20  _SHARED      1  
10643 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 41 52   /* same as SHAR
10644 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69  ED_LOCK */.#defi
10645 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 56 45  ne PAGER_RESERVE
10646 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 6d 65  D    2   /* same
10647 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c 4f 43   as RESERVED_LOC
10648 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  K */.#define PAG
10649 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 34  ER_EXCLUSIVE   4
1064a 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 45 58     /* same as EX
1064b 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a  CLUSIVE_LOCK */.
1064c 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 59  #define PAGER_SY
1064d 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f 2a 0a  NCED      5../*.
1064e 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 20  ** A macro used 
1064f 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65  for invoking the
10650 20 63 6f 64 65 63 20 69 66 20 74 68 65 72 65 20   codec if there 
10651 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 66  is one.*/.#ifdef
10652 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
10653 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43  C.# define CODEC
10654 31 28 50 2c 44 2c 4e 2c 58 29 20 69 66 28 20 50  1(P,D,N,X) if( P
10655 2d 3e 78 43 6f 64 65 63 21 3d 30 20 29 7b 20 50  ->xCodec!=0 ){ P
10656 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64  ->xCodec(P->pCod
10657 65 63 41 72 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a  ecArg,D,N,X); }.
10658 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28  # define CODEC2(
10659 50 2c 44 2c 4e 2c 58 29 20 28 28 63 68 61 72 2a  P,D,N,X) ((char*
1065a 29 28 50 2d 3e 78 43 6f 64 65 63 21 3d 30 3f 50  )(P->xCodec!=0?P
1065b 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64  ->xCodec(P->pCod
1065c 65 63 41 72 67 2c 44 2c 4e 2c 58 29 3a 44 29 29  ecArg,D,N,X):D))
1065d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
1065e 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20  CODEC1(P,D,N,X) 
1065f 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65  /* NO-OP */.# de
10660 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c  fine CODEC2(P,D,
10661 4e 2c 58 29 20 28 28 63 68 61 72 2a 29 44 29 0a  N,X) ((char*)D).
10662 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
10663 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65  e maximum allowe
10664 64 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 31  d sector size. 1
10665 36 4d 42 2e 20 49 66 20 74 68 65 20 78 53 65 63  6MB. If the xSec
10666 74 6f 72 73 69 7a 65 28 29 20 6d 65 74 68 6f 64  torsize() method
10667 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76   .** returns a v
10668 61 6c 75 65 20 6c 61 72 67 65 72 20 74 68 61 6e  alue larger than
10669 20 74 68 69 73 2c 20 74 68 65 6e 20 4d 41 58 5f   this, then MAX_
1066a 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 73 20 75  SECTOR_SIZE is u
1066b 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20  sed instead..** 
1066c 54 68 69 73 20 63 6f 75 6c 64 20 63 6f 6e 63 65  This could conce
1066d 69 76 61 62 6c 79 20 63 61 75 73 65 20 63 6f 72  ivably cause cor
1066e 72 75 70 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e  ruption followin
1066f 67 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  g a power failur
10670 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 20 61 20 73  e on.** such a s
10671 79 73 74 65 6d 2e 20 54 68 69 73 20 69 73 20 63  ystem. This is c
10672 75 72 72 65 6e 74 6c 79 20 61 6e 20 75 6e 64 6f  urrently an undo
10673 63 75 6d 65 6e 74 65 64 20 6c 69 6d 69 74 2e 0a  cumented limit..
10674 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 53  */.#define MAX_S
10675 45 43 54 4f 52 5f 53 49 5a 45 20 30 78 30 31 30  ECTOR_SIZE 0x010
10676 30 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  0000../*.** An i
10677 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
10678 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
10679 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  re is allocated 
1067a 66 6f 72 20 65 61 63 68 20 61 63 74 69 76 65 0a  for each active.
1067b 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64  ** savepoint and
1067c 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1067d 61 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 79  action in the sy
1067e 73 74 65 6d 2e 20 41 6c 6c 20 73 75 63 68 20 73  stem. All such s
1067f 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 61 72 65  tructures.** are
10680 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 50   stored in the P
10681 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b  ager.aSavepoint[
10682 5d 20 61 72 72 61 79 2c 20 77 68 69 63 68 20 69  ] array, which i
10683 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a  s allocated and.
10684 2a 2a 20 72 65 73 69 7a 65 64 20 75 73 69 6e 67  ** resized using
10685 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28   sqlite3Realloc(
10686 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  )..**.** When a 
10687 73 61 76 65 70 6f 69 6e 74 20 69 73 20 63 72 65  savepoint is cre
10688 61 74 65 64 2c 20 74 68 65 20 50 61 67 65 72 53  ated, the PagerS
10689 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
1068a 73 65 74 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20  set field is.** 
1068b 73 65 74 20 74 6f 20 30 2e 20 49 66 20 61 20 6a  set to 0. If a j
1068c 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73  ournal-header is
1068d 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
1068e 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 77  e main journal w
1068f 68 69 6c 65 0a 2a 2a 20 74 68 65 20 73 61 76 65  hile.** the save
10690 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65 2c  point is active,
10691 20 74 68 65 6e 20 69 48 64 72 4f 66 66 73 65 74   then iHdrOffset
10692 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 62   is set to the b
10693 79 74 65 20 6f 66 66 73 65 74 20 0a 2a 2a 20 69  yte offset .** i
10694 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f  mmediately follo
10695 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 6a 6f  wing the last jo
10696 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 72 69  urnal record wri
10697 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61  tten into the ma
10698 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 62 65  in.** journal be
10699 66 6f 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  fore the journal
1069a 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20 69 73  -header. This is
1069b 20 72 65 71 75 69 72 65 64 20 64 75 72 69 6e 67   required during
1069c 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f   savepoint.** ro
1069d 6c 6c 62 61 63 6b 20 28 73 65 65 20 70 61 67 65  llback (see page
1069e 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69  rPlaybackSavepoi
1069f 6e 74 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 64 65  nt())..*/.typede
106a0 66 20 73 74 72 75 63 74 20 50 61 67 65 72 53 61  f struct PagerSa
106a1 76 65 70 6f 69 6e 74 20 50 61 67 65 72 53 61 76  vepoint PagerSav
106a2 65 70 6f 69 6e 74 3b 0a 73 74 72 75 63 74 20 50  epoint;.struct P
106a3 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 7b 0a  agerSavepoint {.
106a4 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20    i64 iOffset;  
106a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
106a6 2a 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65  * Starting offse
106a7 74 20 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61  t in main journa
106a8 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f  l */.  i64 iHdrO
106a9 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
106aa 20 20 20 20 2f 2a 20 53 65 65 20 61 62 6f 76 65      /* See above
106ab 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49   */.  Bitvec *pI
106ac 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20  nSavepoint;     
106ad 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67     /* Set of pag
106ae 65 73 20 69 6e 20 74 68 69 73 20 73 61 76 65 70  es in this savep
106af 6f 69 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e  oint */.  Pgno n
106b0 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 20 20  Orig;           
106b1 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e         /* Origin
106b2 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
106b3 65 73 20 69 6e 20 66 69 6c 65 20 2a 2f 0a 20 20  es in file */.  
106b4 50 67 6e 6f 20 69 53 75 62 52 65 63 3b 20 20 20  Pgno iSubRec;   
106b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
106b6 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 20 72  Index of first r
106b7 65 63 6f 72 64 20 69 6e 20 73 75 62 2d 6a 6f 75  ecord in sub-jou
106b8 72 6e 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  rnal */.};../*.*
106b9 2a 20 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61  * A open page ca
106ba 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  che is an instan
106bb 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
106bc 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
106bd 2a 0a 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a  *.** errCode.**.
106be 2a 2a 20 20 20 50 61 67 65 72 2e 65 72 72 43 6f  **   Pager.errCo
106bf 64 65 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f  de may be set to
106c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53   SQLITE_IOERR, S
106c1 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f  QLITE_CORRUPT, o
106c2 72 0a 2a 2a 20 20 20 6f 72 20 53 51 4c 49 54 45  r.**   or SQLITE
106c3 5f 46 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20  _FULL. Once one 
106c4 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72  of the first thr
106c5 65 65 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73  ee errors occurs
106c6 2c 20 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a  , it persists.**
106c7 20 20 20 61 6e 64 20 69 73 20 72 65 74 75 72 6e     and is return
106c8 65 64 20 61 73 20 74 68 65 20 72 65 73 75 6c 74  ed as the result
106c9 20 6f 66 20 65 76 65 72 79 20 6d 61 6a 6f 72 20   of every major 
106ca 70 61 67 65 72 20 41 50 49 20 63 61 6c 6c 2e 20  pager API call. 
106cb 20 54 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 45   The.**   SQLITE
106cc 5f 46 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64  _FULL return cod
106cd 65 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69  e is slightly di
106ce 66 66 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73  fferent. It pers
106cf 69 73 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20  ists only until 
106d0 74 68 65 0a 2a 2a 20 20 20 6e 65 78 74 20 73 75  the.**   next su
106d1 63 63 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63  ccessful rollbac
106d2 6b 20 69 73 20 70 65 72 66 6f 72 6d 65 64 20 6f  k is performed o
106d3 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  n the pager cach
106d4 65 2e 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51  e. Also,.**   SQ
106d5 4c 49 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e  LITE_FULL does n
106d6 6f 74 20 61 66 66 65 63 74 20 74 68 65 20 73 71  ot affect the sq
106d7 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20  lite3PagerGet() 
106d8 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  and sqlite3Pager
106d9 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 41 50  Lookup().**   AP
106da 49 73 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69  Is, they may sti
106db 6c 6c 20 62 65 20 75 73 65 64 20 73 75 63 63 65  ll be used succe
106dc 73 73 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64  ssfully..**.** d
106dd 62 53 69 7a 65 56 61 6c 69 64 2c 20 64 62 53 69  bSizeValid, dbSi
106de 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a 65 2c 20  ze, dbOrigSize, 
106df 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a  dbFileSize.**.**
106e0 20 20 20 4d 61 6e 61 67 69 6e 67 20 74 68 65 20     Managing the 
106e1 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
106e2 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67  base file in pag
106e3 65 73 20 69 73 20 61 20 6c 69 74 74 6c 65 20 63  es is a little c
106e4 6f 6d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 20 20  omplicated..**  
106e5 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 50 61   The variable Pa
106e6 67 65 72 2e 64 62 53 69 7a 65 20 63 6f 6e 74 61  ger.dbSize conta
106e7 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ins the number o
106e8 66 20 70 61 67 65 73 20 74 68 61 74 20 74 68 65  f pages that the
106e9 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 69   database.**   i
106ea 6d 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 63  mage currently c
106eb 6f 6e 74 61 69 6e 73 2e 20 41 73 20 74 68 65 20  ontains. As the 
106ec 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 67  database image g
106ed 72 6f 77 73 20 6f 72 20 73 68 72 69 6e 6b 73 20  rows or shrinks 
106ee 74 68 69 73 0a 2a 2a 20 20 20 76 61 72 69 61 62  this.**   variab
106ef 6c 65 20 69 73 20 75 70 64 61 74 65 64 2e 20 54  le is updated. T
106f0 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 67 65  he variable Page
106f1 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e  r.dbFileSize con
106f2 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  tains the number
106f3 0a 2a 2a 20 20 20 6f 66 20 70 61 67 65 73 20 69  .**   of pages i
106f4 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
106f5 69 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 62 65  ile. This may be
106f6 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
106f7 50 61 67 65 72 2e 64 62 53 69 7a 65 0a 2a 2a 20  Pager.dbSize.** 
106f8 20 20 69 66 20 73 6f 6d 65 20 70 61 67 65 73 20    if some pages 
106f9 68 61 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64  have been append
106fa 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ed to the databa
106fb 73 65 20 69 6d 61 67 65 20 62 75 74 20 6e 6f 74  se image but not
106fc 20 79 65 74 20 77 72 69 74 74 65 6e 0a 2a 2a 20   yet written.** 
106fd 20 20 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63    out from the c
106fe 61 63 68 65 20 74 6f 20 74 68 65 20 61 63 74 75  ache to the actu
106ff 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  al file on disk.
10700 20 4f 72 20 69 66 20 74 68 65 20 69 6d 61 67 65   Or if the image
10701 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 20 20 74   has been.**   t
10702 72 75 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69  runcated by an i
10703 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75  ncremental-vacuu
10704 6d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65  m operation. The
10705 20 50 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a   Pager.dbOrigSiz
10706 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 20 20  e variable.**   
10707 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d  contains the num
10708 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
10709 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61  the database ima
1070a 67 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 72  ge when the curr
1070b 65 6e 74 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63  ent.**   transac
1070c 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e  tion was opened.
1070d 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   The contents of
1070e 20 61 6c 6c 20 74 68 72 65 65 20 6f 66 20 74 68   all three of th
1070f 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 69 73  ese variables is
10710 0a 2a 2a 20 20 20 6f 6e 6c 79 20 67 75 61 72 61  .**   only guara
10711 6e 74 65 65 64 20 74 6f 20 62 65 20 63 6f 72 72  nteed to be corr
10712 65 63 74 20 69 66 20 74 68 65 20 62 6f 6f 6c 65  ect if the boole
10713 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 56  an Pager.dbSizeV
10714 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 2a 2a  alid is true..**
10715 0a 2a 2a 20 20 20 54 4f 44 4f 3a 20 55 6e 64 65  .**   TODO: Unde
10716 72 20 77 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e  r what condition
10717 73 20 69 73 20 64 62 53 69 7a 65 56 61 6c 69 64  s is dbSizeValid
10718 20 73 65 74 3f 20 43 6c 65 61 72 65 64 3f 0a 2a   set? Cleared?.*
10719 2a 0a 2a 2a 20 63 68 61 6e 67 65 43 6f 75 6e 74  *.** changeCount
1071a 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  Done.**.**   Thi
1071b 73 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62  s boolean variab
1071c 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61  le is used to ma
1071d 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
1071e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
1071f 0a 2a 2a 20 20 20 28 74 68 65 20 34 2d 62 79 74  .**   (the 4-byt
10720 65 20 68 65 61 64 65 72 20 66 69 65 6c 64 20 61  e header field a
10721 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34  t byte offset 24
10722 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
10723 20 66 69 6c 65 29 20 69 73 20 0a 2a 2a 20 20 20   file) is .**   
10724 6e 6f 74 20 75 70 64 61 74 65 64 20 6d 6f 72 65  not updated more
10725 20 6f 66 74 65 6e 20 74 68 61 6e 20 6e 65 63 65   often than nece
10726 73 73 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20  ssary. .**.**   
10727 49 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  It is set to tru
10728 65 20 77 68 65 6e 20 74 68 65 20 63 68 61 6e 67  e when the chang
10729 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20  e-counter field 
1072a 69 73 20 75 70 64 61 74 65 64 2c 20 77 68 69 63  is updated, whic
1072b 68 20 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c 79  h .**   can only
1072c 20 68 61 70 70 65 6e 20 69 66 20 61 6e 20 65 78   happen if an ex
1072d 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20  clusive lock is 
1072e 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61  held on the data
1072f 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 20  base file..**   
10730 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 28 73  It is cleared (s
10731 65 74 20 74 6f 20 66 61 6c 73 65 29 20 77 68 65  et to false) whe
10732 6e 65 76 65 72 20 61 6e 20 65 78 63 6c 75 73 69  never an exclusi
10733 76 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 20  ve lock is .**  
10734 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 6f 6e   relinquished on
10735 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
10736 6c 65 2e 20 45 61 63 68 20 74 69 6d 65 20 61 20  le. Each time a 
10737 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63  transaction is c
10738 6f 6d 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 20 54  ommitted,.**   T
10739 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  he changeCountDo
1073a 6e 65 20 66 6c 61 67 20 69 73 20 69 6e 73 70 65  ne flag is inspe
1073b 63 74 65 64 2e 20 49 66 20 69 74 20 69 73 20 74  cted. If it is t
1073c 72 75 65 2c 20 74 68 65 20 77 6f 72 6b 20 6f 66  rue, the work of
1073d 0a 2a 2a 20 20 20 75 70 64 61 74 69 6e 67 20 74  .**   updating t
1073e 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  he change-counte
1073f 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66 6f 72  r is omitted for
10740 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
10741 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
10742 20 20 54 68 69 73 20 6d 65 63 68 61 6e 69 73 6d    This mechanism
10743 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e   means that when
10744 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c   running in excl
10745 75 73 69 76 65 20 6d 6f 64 65 2c 20 61 20 63 6f  usive mode, a co
10746 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e  nnection .**   n
10747 65 65 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 20  eed only update 
10748 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
10749 65 72 20 6f 6e 63 65 2c 20 66 6f 72 20 74 68 65  er once, for the
1074a 20 66 69 72 73 74 20 74 72 61 6e 73 61 63 74 69   first transacti
1074b 6f 6e 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 74 65  on.**   committe
1074c 64 2e 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66  d..**.** dbModif
1074d 69 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20  ied.**.**   The 
1074e 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20  dbModified flag 
1074f 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 72 20  is set whenever 
10750 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  a database page 
10751 69 73 20 64 69 72 74 69 65 64 2e 0a 2a 2a 20 20  is dirtied..**  
10752 20 49 74 20 69 73 20 63 6c 65 61 72 65 64 20 61   It is cleared a
10753 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63  t the end of eac
10754 68 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  h transaction..*
10755 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 75 73 65  *.**   It is use
10756 64 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e  d when committin
10757 67 20 6f 72 20 6f 74 68 65 72 77 69 73 65 20 65  g or otherwise e
10758 6e 64 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74  nding a transact
10759 69 6f 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 68 65  ion. If.**   the
1075a 20 64 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67   dbModified flag
1075b 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e 20 6c   is clear then l
1075c 65 73 73 20 77 6f 72 6b 20 68 61 73 20 74 6f 20  ess work has to 
1075d 62 65 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a  be done..**.** j
1075e 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 0a 2a 2a  ournalStarted.**
1075f 0a 2a 2a 20 20 20 54 68 69 73 20 66 6c 61 67 20  .**   This flag 
10760 69 73 20 73 65 74 20 77 68 65 6e 65 76 65 72 20  is set whenever 
10761 74 68 65 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75  the the main jou
10762 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 2e 20  rnal is synced. 
10763 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 70 6f 69  .**.**   The poi
10764 6e 74 20 6f 66 20 74 68 69 73 20 66 6c 61 67 20  nt of this flag 
10765 69 73 20 74 68 61 74 20 69 74 20 6d 75 73 74 20  is that it must 
10766 62 65 20 73 65 74 20 61 66 74 65 72 20 74 68 65  be set after the
10767 20 0a 2a 2a 20 20 20 66 69 72 73 74 20 6a 6f 75   .**   first jou
10768 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 61  rnal header in a
10769 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
1076a 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f  s been synced to
1076b 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 41 66 74 65   disk..**   Afte
1076c 72 20 74 68 69 73 20 68 61 73 20 68 61 70 70 65  r this has happe
1076d 6e 65 64 2c 20 6e 65 77 20 70 61 67 65 73 20 61  ned, new pages a
1076e 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 64  ppended to the d
1076f 61 74 61 62 61 73 65 20 0a 2a 2a 20 20 20 64 6f  atabase .**   do
10770 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20 50 47   not need the PG
10771 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c  HDR_NEED_SYNC fl
10772 61 67 20 73 65 74 2c 20 61 73 20 74 68 65 79 20  ag set, as they 
10773 64 6f 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20  do not need.**  
10774 20 74 6f 20 77 61 69 74 20 66 6f 72 20 61 20 6a   to wait for a j
10775 6f 75 72 6e 61 6c 20 73 79 6e 63 20 62 65 66 6f  ournal sync befo
10776 72 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 77  re they can be w
10777 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 2a 2a  ritten out to.**
10778 20 20 20 74 68 65 20 64 61 74 61 62 61 73 65 20     the database 
10779 66 69 6c 65 20 28 73 65 65 20 66 75 6e 63 74 69  file (see functi
1077a 6f 6e 20 70 61 67 65 72 5f 77 72 69 74 65 28 29  on pager_write()
1077b 29 2e 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65 74 4d  )..**   .** setM
1077c 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 54 68  aster.**.**   Th
1077d 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 75  is variable is u
1077e 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68  sed to ensure th
1077f 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  at the master jo
10780 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 0a  urnal file name.
10781 2a 2a 20 20 20 28 69 66 20 61 6e 79 29 20 69 73  **   (if any) is
10782 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 69 6e   only written in
10783 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
10784 69 6c 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  ile once..**.** 
10785 20 20 57 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e    When committin
10786 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  g a transaction,
10787 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
10788 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 28 69  nal file name (i
10789 66 20 61 6e 79 29 0a 2a 2a 20 20 20 6d 61 79 20  f any).**   may 
1078a 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
1078b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1078c 20 77 68 69 6c 65 20 74 68 65 20 70 61 67 65 72   while the pager
1078d 20 69 73 20 73 74 69 6c 6c 20 69 6e 0a 2a 2a 20   is still in.** 
1078e 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44    PAGER_RESERVED
1078f 20 73 74 61 74 65 20 28 73 65 65 20 43 6f 6d 6d   state (see Comm
10790 69 74 50 68 61 73 65 4f 6e 65 28 29 20 66 6f 72  itPhaseOne() for
10791 20 74 68 65 20 61 63 74 69 6f 6e 29 2e 20 49 74   the action). It
10792 0a 2a 2a 20 20 20 74 68 65 6e 20 61 74 74 65 6d  .**   then attem
10793 70 74 73 20 74 6f 20 75 70 67 72 61 64 65 20 74  pts to upgrade t
10794 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  o an exclusive l
10795 6f 63 6b 2e 20 49 66 20 74 68 69 73 20 61 74 74  ock. If this att
10796 65 6d 70 74 0a 2a 2a 20 20 20 66 61 69 6c 73 2c  empt.**   fails,
10797 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 42 55 53   then SQLITE_BUS
10798 59 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65  Y may be returne
10799 64 20 74 6f 20 74 68 65 20 75 73 65 72 20 61 6e  d to the user an
1079a 64 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 20 20  d the user.**   
1079b 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63  may attempt to c
1079c 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61  ommit the transa
1079d 63 74 69 6f 6e 20 61 67 61 69 6e 20 6c 61 74 65  ction again late
1079e 72 20 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20 20 20  r (calling.**   
1079f 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29  CommitPhaseOne()
107a0 20 61 67 61 69 6e 29 2e 20 54 68 69 73 20 66 6c   again). This fl
107a1 61 67 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e  ag is used to en
107a2 73 75 72 65 20 74 68 61 74 20 74 68 65 20 0a 2a  sure that the .*
107a3 2a 20 20 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  *   master journ
107a4 61 6c 20 6e 61 6d 65 20 69 73 20 6f 6e 6c 79 20  al name is only 
107a5 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
107a6 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 20  ournal file the 
107a7 66 69 72 73 74 0a 2a 2a 20 20 20 74 69 6d 65 20  first.**   time 
107a8 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29  CommitPhaseOne()
107a9 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a   is called..**.*
107aa 2a 20 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a  * doNotSync.**.*
107ab 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c  *   This variabl
107ac 65 20 69 73 20 73 65 74 20 61 6e 64 20 63 6c 65  e is set and cle
107ad 61 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 50  ared by sqlite3P
107ae 61 67 65 72 57 72 69 74 65 28 29 2e 0a 2a 2a 0a  agerWrite()..**.
107af 2a 2a 20 6e 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a  ** needSync.**.*
107b0 2a 20 20 20 54 4f 44 4f 3a 20 49 74 20 6d 69 67  *   TODO: It mig
107b1 68 74 20 62 65 20 65 61 73 69 65 72 20 74 6f 20  ht be easier to 
107b2 73 65 74 20 74 68 69 73 20 76 61 72 69 61 62 6c  set this variabl
107b3 65 20 69 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61  e in writeJourna
107b4 6c 48 64 72 28 29 0a 2a 2a 20 20 20 61 6e 64 20  lHdr().**   and 
107b5 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e  writeMasterJourn
107b6 61 6c 28 29 20 6f 6e 6c 79 2e 20 43 68 61 6e 67  al() only. Chang
107b7 65 20 69 74 73 20 6d 65 61 6e 69 6e 67 20 74 6f  e its meaning to
107b8 20 22 75 6e 73 79 6e 63 65 64 20 64 61 74 61 0a   "unsynced data.
107b9 2a 2a 20 20 20 68 61 73 20 62 65 65 6e 20 77 72  **   has been wr
107ba 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
107bb 72 6e 61 6c 22 2e 0a 2a 2f 0a 73 74 72 75 63 74  rnal"..*/.struct
107bc 20 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69 74   Pager {.  sqlit
107bd 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20  e3_vfs *pVfs;   
107be 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e         /* OS fun
107bf 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f  ctions to use fo
107c0 72 20 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78 63  r IO */.  u8 exc
107c1 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 20  lusiveMode;     
107c2 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e        /* Boolean
107c3 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e  . True if lockin
107c4 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56  g_mode==EXCLUSIV
107c5 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61  E */.  u8 journa
107c6 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  lMode;          
107c7 20 20 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20     /* On of the 
107c8 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
107c9 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  E_* values */.  
107ca 75 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20  u8 useJournal;  
107cb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
107cc 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  se a rollback jo
107cd 75 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20 66 69  urnal on this fi
107ce 6c 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61  le */.  u8 noRea
107cf 64 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20  dlock;          
107d0 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f      /* Do not bo
107d1 74 68 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 72  ther to obtain r
107d2 65 61 64 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38  eadlocks */.  u8
107d3 20 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20 20 20   noSync;        
107d4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20            /* Do 
107d5 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 6a 6f 75  not sync the jou
107d6 72 6e 61 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a  rnal if true */.
107d7 20 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20    u8 fullSync;  
107d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
107d9 20 44 6f 20 65 78 74 72 61 20 73 79 6e 63 73 20   Do extra syncs 
107da 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
107db 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f  or robustness */
107dc 0a 20 20 75 38 20 73 79 6e 63 5f 66 6c 61 67 73  .  u8 sync_flags
107dd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
107de 2a 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f  * One of SYNC_NO
107df 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c  RMAL or SYNC_FUL
107e0 4c 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 46 69  L */.  u8 tempFi
107e1 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
107e2 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20     /* zFilename 
107e3 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  is a temporary f
107e4 69 6c 65 20 2a 2f 0a 20 20 75 38 20 72 65 61 64  ile */.  u8 read
107e5 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20  Only;           
107e6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
107e7 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74   a read-only dat
107e8 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 6d 65  abase */.  u8 me
107e9 6d 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  mDb;            
107ea 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
107eb 6f 20 69 6e 68 69 62 69 74 20 61 6c 6c 20 66 69  o inhibit all fi
107ec 6c 65 20 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a 20  le I/O */..  /* 
107ed 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  The following bl
107ee 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 6f  ock contains tho
107ef 73 65 20 63 6c 61 73 73 20 6d 65 6d 62 65 72 73  se class members
107f0 20 74 68 61 74 20 61 72 65 20 64 79 6e 61 6d 69   that are dynami
107f1 63 61 6c 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 66  cally.  ** modif
107f2 69 65 64 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61  ied during norma
107f3 6c 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 54 68  l operations. Th
107f4 65 20 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65  e other variable
107f5 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74  s in this struct
107f6 75 72 65 0a 20 20 2a 2a 20 61 72 65 20 65 69 74  ure.  ** are eit
107f7 68 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 72  her constant thr
107f8 6f 75 67 68 6f 75 74 20 74 68 65 20 6c 69 66 65  oughout the life
107f9 74 69 6d 65 20 6f 66 20 74 68 65 20 70 61 67 65  time of the page
107fa 72 2c 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20  r, or else.  ** 
107fb 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 63 6f  used to store co
107fc 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72 61  nfiguration para
107fd 6d 65 74 65 72 73 20 74 68 61 74 20 61 66 66 65  meters that affe
107fe 63 74 20 74 68 65 20 77 61 79 20 74 68 65 20 70  ct the way the p
107ff 61 67 65 72 20 0a 20 20 2a 2a 20 6f 70 65 72 61  ager .  ** opera
10800 74 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  tes..  **.  ** T
10801 68 65 20 27 73 74 61 74 65 27 20 76 61 72 69 61  he 'state' varia
10802 62 6c 65 20 69 73 20 64 65 73 63 72 69 62 65 64  ble is described
10803 20 69 6e 20 6d 6f 72 65 20 64 65 74 61 69 6c 20   in more detail 
10804 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 0a 20  along with the. 
10805 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 73   ** descriptions
10806 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69   of the values i
10807 74 20 6d 61 79 20 74 61 6b 65 20 2d 20 50 41 47  t may take - PAG
10808 45 52 5f 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 4d  ER_UNLOCK etc. M
10809 61 6e 79 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  any of the.  ** 
1080a 6f 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 20  other variables 
1080b 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b 20 61 72  in this block ar
1080c 65 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74  e described in t
1080d 68 65 20 63 6f 6d 6d 65 6e 74 20 64 69 72 65 63  he comment direc
1080e 74 6c 79 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20  tly .  ** above 
1080f 74 68 69 73 20 63 6c 61 73 73 20 64 65 66 69 6e  this class defin
10810 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 38  ition..  */.  u8
10811 20 73 74 61 74 65 3b 20 20 20 20 20 20 20 20 20   state;         
10812 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 41 47            /* PAG
10813 45 52 5f 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52  ER_UNLOCK, _SHAR
10814 45 44 2c 20 5f 52 45 53 45 52 56 45 44 2c 20 65  ED, _RESERVED, e
10815 74 63 2e 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f  tc. */.  u8 dbMo
10816 64 69 66 69 65 64 3b 20 20 20 20 20 20 20 20 20  dified;         
10817 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
10818 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 63 68  there are any ch
10819 61 6e 67 65 73 20 74 6f 20 74 68 65 20 44 62 20  anges to the Db 
1081a 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53 79 6e 63  */.  u8 needSync
1081b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1081c 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 66   /* True if an f
1081d 73 79 6e 63 28 29 20 69 73 20 6e 65 65 64 65 64  sync() is needed
1081e 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   on the journal 
1081f 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53  */.  u8 journalS
10820 74 61 72 74 65 64 3b 20 20 20 20 20 20 20 20 20  tarted;         
10821 20 2f 2a 20 54 72 75 65 20 69 66 20 68 65 61 64   /* True if head
10822 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73  er of journal is
10823 20 73 79 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20   synced */.  u8 
10824 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b  changeCountDone;
10825 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20           /* Set 
10826 61 66 74 65 72 20 69 6e 63 72 65 6d 65 6e 74 69  after incrementi
10827 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  ng the change-co
10828 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 73 65  unter */.  u8 se
10829 74 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 20  tMaster;        
1082a 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
1082b 66 20 61 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73  f a m-j name has
1082c 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
1082d 20 6a 72 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f   jrnl */.  u8 do
1082e 4e 6f 74 53 79 6e 63 3b 20 20 20 20 20 20 20 20  NotSync;        
1082f 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61         /* Boolea
10830 6e 2e 20 57 68 69 6c 65 20 74 72 75 65 2c 20 64  n. While true, d
10831 6f 20 6e 6f 74 20 73 70 69 6c 6c 20 74 68 65 20  o not spill the 
10832 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 64 62  cache */.  u8 db
10833 53 69 7a 65 56 61 6c 69 64 3b 20 20 20 20 20 20  SizeValid;      
10834 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 77 68         /* Set wh
10835 65 6e 20 64 62 53 69 7a 65 20 69 73 20 63 6f 72  en dbSize is cor
10836 72 65 63 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 64  rect */.  Pgno d
10837 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  bSize;          
10838 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
10839 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
1083a 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 67  database */.  Pg
1083b 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 20  no dbOrigSize;  
1083c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53            /* dbS
1083d 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 63  ize before the c
1083e 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
1083f 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46  on */.  Pgno dbF
10840 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  ileSize;        
10841 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
10842 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61   pages in the da
10843 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
10844 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20   int errCode;   
10845 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10846 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c 20 6b  One of several k
10847 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 2a  inds of errors *
10848 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 20  /.  int nRec;   
10849 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1084a 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c  /* Pages journal
1084b 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74 20 6a  led since last j
1084c 2d 68 65 61 64 65 72 20 77 72 69 74 74 65 6e 20  -header written 
1084d 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 6e  */.  u32 cksumIn
1084e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  it;             
1084f 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d   /* Quasi-random
10850 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f 20   value added to 
10851 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20 2a  every checksum *
10852 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65 63 3b  /.  u32 nSubRec;
10853 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10854 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63  /* Number of rec
10855 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74 6f 20  ords written to 
10856 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20  sub-journal */. 
10857 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72   Bitvec *pInJour
10858 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  nal;         /* 
10859 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68  One bit for each
1085a 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
1085b 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
1085c 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64  sqlite3_file *fd
1085d 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;           /* F
1085e 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66  ile descriptor f
1085f 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  or database */. 
10860 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a   sqlite3_file *j
10861 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fd;          /* 
10862 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  File descriptor 
10863 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  for main journal
10864 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
10865 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 20 20  le *sjfd;       
10866 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69    /* File descri
10867 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75  ptor for sub-jou
10868 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f  rnal */.  i64 jo
10869 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 20  urnalOff;       
1086a 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
1086b 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 69 6e   write offset in
1086c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1086d 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e  e */.  i64 journ
1086e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20 20 20  alHdr;          
1086f 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65     /* Byte offse
10870 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f  t to previous jo
10871 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a  urnal header */.
10872 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74    PagerSavepoint
10873 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a   *aSavepoint; /*
10874 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 76 65   Array of active
10875 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20   savepoints */. 
10876 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b   int nSavepoint;
10877 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10878 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  Number of elemen
10879 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e 74  ts in aSavepoint
1087a 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 46  [] */.  char dbF
1087b 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 20  ileVers[16];    
1087c 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 77      /* Changes w
1087d 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 73 65  henever database
1087e 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f   file changes */
1087f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53 69 7a  .  u32 sectorSiz
10880 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
10881 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f 72  * Assumed sector
10882 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c   size during rol
10883 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 69 6e 74 20  lback */..  int 
10884 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20  nExtra;         
10885 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74          /* Add t
10886 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74  his many bytes t
10887 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79  o each in-memory
10888 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 76   page */.  u32 v
10889 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  fsFlags;        
1088a 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
1088b 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 73 2e  for sqlite3_vfs.
1088c 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74  xOpen() */.  int
1088d 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20   pageSize;      
1088e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1088f 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61  er of bytes in a
10890 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20   page */.  Pgno 
10891 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20  mxPgno;         
10892 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
10893 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f  m allowed size o
10894 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  f the database *
10895 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  /.  char *zFilen
10896 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
10897 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
10898 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
10899 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c    char *zJournal
1089a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1089b 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75   Name of the jou
1089c 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69  rnal file */.  i
1089d 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65  nt (*xBusyHandle
1089e 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75  r)(void*); /* Fu
1089f 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77  nction to call w
108a0 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f  hen busy */.  vo
108a1 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72  id *pBusyHandler
108a2 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e  Arg;      /* Con
108a3 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f  text argument fo
108a4 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a  r xBusyHandler *
108a5 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
108a6 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c  TEST.  int nHit,
108a7 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20   nMiss;         
108a8 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73     /* Cache hits
108a9 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a   and missing */.
108aa 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72    int nRead, nWr
108ab 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ite;          /*
108ac 20 44 61 74 61 62 61 73 65 20 70 61 67 65 73 20   Database pages 
108ad 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a  read/written */.
108ae 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a  #endif.  void (*
108af 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 67  xReiniter)(DbPag
108b0 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69  e*); /* Call thi
108b1 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72  s routine when r
108b2 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a  eloading pages *
108b3 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
108b4 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64  HAS_CODEC.  void
108b5 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64   *(*xCodec)(void
108b6 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74  *,void*,Pgno,int
108b7 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f  ); /* Routine fo
108b8 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61  r en/decoding da
108b9 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43  ta */.  void *pC
108ba 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20 20 20  odecArg;        
108bb 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
108bc 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 28  ument to xCodec(
108bd 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68  ) */.#endif.  ch
108be 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20  ar *pTmpSpace;  
108bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
108c0 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65  er.pageSize byte
108c1 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74  s of space for t
108c2 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 34 20  mp use */.  i64 
108c3 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  journalSizeLimit
108c4 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  ;       /* Size 
108c5 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69 73  limit for persis
108c6 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  tent journal fil
108c7 65 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a  es */.  PCache *
108c8 70 50 43 61 63 68 65 3b 20 20 20 20 20 20 20 20  pPCache;        
108c9 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
108ca 6f 20 70 61 67 65 20 63 61 63 68 65 20 6f 62 6a  o page cache obj
108cb 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ect */.  sqlite3
108cc 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70  _backup *pBackup
108cd 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20  ;    /* Pointer 
108ce 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f 69  to list of ongoi
108cf 6e 67 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73  ng backup proces
108d0 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ses */.};../*.**
108d1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67   The following g
108d2 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20  lobal variables 
108d3 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73  hold counters us
108d4 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e  ed for.** testin
108d5 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e  g purposes only.
108d6 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65    These variable
108d7 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69  s do not exist i
108d8 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69  n.** a non-testi
108d9 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65  ng build.  These
108da 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e   variables are n
108db 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a  ot thread-safe..
108dc 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
108dd 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
108de 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67   int sqlite3_pag
108df 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20  er_readdb_count 
108e0 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65  = 0;    /* Numbe
108e1 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20  r of full pages 
108e2 72 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a  read from DB */.
108e3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
108e4 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69  qlite3_pager_wri
108e5 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20  tedb_count = 0; 
108e6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
108e7 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65  ull pages writte
108e8 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54  n to DB */.SQLIT
108e9 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
108ea 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63  3_pager_writej_c
108eb 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20  ount = 0;    /* 
108ec 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
108ed 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e  written to journ
108ee 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50  al */.# define P
108ef 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b  AGER_INCR(v)  v+
108f0 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  +.#else.# define
108f1 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23   PAGER_INCR(v).#
108f2 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a  endif..../*.** J
108f3 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67  ournal files beg
108f4 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c  in with the foll
108f5 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69  owing magic stri
108f6 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a  ng.  The data.**
108f7 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72   was obtained fr
108f8 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20  om /dev/random. 
108f9 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79   It is used only
108fa 20 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65   as a sanity che
108fb 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20  ck..**.** Since 
108fc 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74  version 2.8.0, t
108fd 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61  he journal forma
108fe 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74  t contains addit
108ff 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20  ional sanity.** 
10900 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61  checking informa
10901 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f  tion.  If the po
10902 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20  wer fails while 
10903 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62  the journal is b
10904 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c  eing.** written,
10905 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72   semi-random gar
10906 62 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20  bage data might 
10907 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f  appear in the jo
10908 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66  urnal.** file af
10909 74 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73  ter power is res
1090a 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74  tored.  If an at
1090b 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61  tempt is then ma
1090c 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68  de.** to roll th
1090d 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20  e journal back, 
1090e 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75  the database cou
1090f 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e  ld be corrupted.
10910 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c    The additional
10911 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b  .** sanity check
10912 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61  ing data is an a
10913 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76  ttempt to discov
10914 65 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69  er the garbage i
10915 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  n the.** journal
10916 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a   and ignore it..
10917 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79  **.** The sanity
10918 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d   checking inform
10919 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65  ation for the ne
1091a 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74  w journal format
1091b 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20   consists.** of 
1091c 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75  a 32-bit checksu
1091d 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f  m on each page o
1091e 66 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65  f data.  The che
1091f 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74  cksum covers bot
10920 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75  h.** the page nu
10921 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61  mber and the pPa
10922 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79  ger->pageSize by
10923 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20  tes of data for 
10924 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69  the page..** Thi
10925 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69  s cksum is initi
10926 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62  alized to a 32-b
10927 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20  it random value 
10928 74 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20  that appears in 
10929 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
1092a 69 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20  ile right after 
1092b 74 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65  the header.  The
1092c 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69   random initiali
1092d 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  zer is important
1092e 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72  ,.** because gar
1092f 62 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61  bage data that a
10930 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e  ppears at the en
10931 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69  d of a journal i
10932 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61  s likely.** data
10933 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69   that was once i
10934 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68  n other files th
10935 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e  at have now been
10936 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68   deleted.  If th
10937 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74  e.** garbage dat
10938 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f  a came from an o
10939 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20  bsolete journal 
1093a 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73  file, the checks
1093b 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20  ums might.** be 
1093c 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79  correct.  But by
1093d 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68   initializing th
1093e 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61  e checksum to ra
1093f 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68  ndom value which
10940 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74  .** is different
10941 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e   for every journ
10942 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20  al, we minimize 
10943 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74  that risk..*/.st
10944 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
10945 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61  ned char aJourna
10946 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30  lMagic[] = {.  0
10947 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c  xd9, 0xd5, 0x05,
10948 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61   0xf9, 0x20, 0xa
10949 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d  1, 0x63, 0xd7,.}
1094a 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a  ;../*.** The siz
1094b 65 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 68  e of the of each
1094c 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e 20   page record in 
1094d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67  the journal is g
1094e 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66  iven by.** the f
1094f 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a  ollowing macro..
10950 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e  */.#define JOURN
10951 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29  AL_PG_SZ(pPager)
10952 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65    ((pPager->page
10953 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a  Size) + 8)../*.*
10954 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  * The journal he
10955 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68  ader size for th
10956 69 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 69  is pager. This i
10957 73 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 61  s usually the sa
10958 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61  me .** size as a
10959 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63   single disk sec
1095a 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 65  tor. See also se
1095b 74 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a  tSectorSize()..*
1095c 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41  /.#define JOURNA
1095d 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
1095e 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72   (pPager->sector
1095f 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  Size)../*.** The
10960 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20   macro MEMDB is 
10961 74 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64  true if we are d
10962 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69  ealing with an i
10963 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
10964 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73  e..** We do this
10965 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74   as a macro so t
10966 68 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54  hat if the SQLIT
10967 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20  E_OMIT_MEMORYDB 
10968 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a  macro is set,.**
10969 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45   the value of ME
1096a 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f  MDB will be a co
1096b 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63  nstant and the c
1096c 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74  ompiler will opt
1096d 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64  imize.** out cod
1096e 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76  e that would nev
1096f 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23  er execute..*/.#
10970 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
10971 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66  T_MEMORYDB.# def
10972 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73  ine MEMDB 0.#els
10973 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42  e.# define MEMDB
10974 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23   pPager->memDb.#
10975 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
10976 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70   maximum legal p
10977 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32  age number is (2
10978 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65  ^31 - 1)..*/.#de
10979 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50  fine PAGER_MAX_P
1097a 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a  GNO 2147483647..
1097b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 0a  #ifndef NDEBUG .
1097c 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a  /*.** Usage:.**.
1097d 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 73  **   assert( ass
1097e 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28  ert_pager_state(
1097f 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 74  pPager) );.*/.st
10980 61 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 5f  atic int assert_
10981 70 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65  pager_state(Page
10982 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 2f  r *pPager){..  /
10983 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 73  * A temp-file is
10984 20 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 52   always in PAGER
10985 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 41  _EXCLUSIVE or PA
10986 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65  GER_SYNCED state
10987 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
10988 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d  Pager->tempFile=
10989 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74  =0 || pPager->st
1098a 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55  ate>=PAGER_EXCLU
1098b 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  SIVE );..  /* Th
1098c 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e  e changeCountDon
1098d 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73  e flag is always
1098e 20 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 69   set for temp-fi
1098f 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  les */.  assert(
10990 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
10991 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e  e==0 || pPager->
10992 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20  changeCountDone 
10993 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  );..  return 1;.
10994 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
10995 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69  Return true if i
10996 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74  t is necessary t
10997 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 50  o write page *pP
10998 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a  g into the sub-j
10999 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 67  ournal..** A pag
1099a 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72  e needs to be wr
1099b 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 73  itten into the s
1099c 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68  ub-journal if th
1099d 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a 2a  ere exists one.*
1099e 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 73  * or more open s
1099f 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 68  avepoints for wh
109a0 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54  ich:.**.**   * T
109a1 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69  he page-number i
109a2 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
109a3 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 76  qual to PagerSav
109a4 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e  epoint.nOrig, an
109a5 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 74  d.**   * The bit
109a6 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
109a7 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65  o the page-numbe
109a8 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e 0a  r is not set in.
109a9 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65  **     PagerSave
109aa 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69  point.pInSavepoi
109ab 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nt..*/.static in
109ac 74 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61  t subjRequiresPa
109ad 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  ge(PgHdr *pPg){.
109ae 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50    Pgno pgno = pP
109af 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72  g->pgno;.  Pager
109b0 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
109b1 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 3b  pPager;.  int i;
109b2 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
109b3 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
109b4 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65  ; i++){.    Page
109b5 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20  rSavepoint *p = 
109b6 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  &pPager->aSavepo
109b7 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20  int[i];.    if( 
109b8 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 26  p->nOrig>=pgno &
109b9 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 76  & 0==sqlite3Bitv
109ba 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 76  ecTest(p->pInSav
109bb 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 7b  epoint, pgno) ){
109bc 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
109bd 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
109be 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
109bf 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
109c0 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61  he page is alrea
109c1 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  dy in the journa
109c2 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  l file..*/.stati
109c3 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72  c int pageInJour
109c4 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b  nal(PgHdr *pPg){
109c5 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
109c6 33 42 69 74 76 65 63 54 65 73 74 28 70 50 67 2d  3BitvecTest(pPg-
109c7 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  >pPager->pInJour
109c8 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b  nal, pPg->pgno);
109c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61  .}../*.** Read a
109ca 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
109cb 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 66  from the given f
109cc 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20  ile descriptor. 
109cd 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67   Store the integ
109ce 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65  er.** that is re
109cf 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 65  ad in *pRes.  Re
109d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
109d1 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72  f everything wor
109d2 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 72  ked, or an.** er
109d3 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65  ror code is some
109d4 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
109d5 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75  ..**.** All valu
109d6 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e  es are stored on
109d7 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e 64   disk as big-end
109d8 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ian..*/.static i
109d9 6e 74 20 72 65 61 64 33 32 62 69 74 73 28 73 71  nt read32bits(sq
109da 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20  lite3_file *fd, 
109db 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20  i64 offset, u32 
109dc 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e  *pRes){.  unsign
109dd 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20  ed char ac[4];. 
109de 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
109df 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20  3OsRead(fd, ac, 
109e0 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 73  sizeof(ac), offs
109e1 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  et);.  if( rc==S
109e2 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
109e3 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47  *pRes = sqlite3G
109e4 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 7d  et4byte(ac);.  }
109e5 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
109e6 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33  ./*.** Write a 3
109e7 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e  2-bit integer in
109e8 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 66  to a string buff
109e9 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 6e  er in big-endian
109ea 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a   byte order..*/.
109eb 23 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 74  #define put32bit
109ec 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 50  s(A,B)  sqlite3P
109ed 75 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c 42  ut4byte((u8*)A,B
109ee 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61  )../*.** Write a
109ef 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
109f0 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 66  into the given f
109f1 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20  ile descriptor. 
109f2 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
109f3 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20  K.** on success 
109f4 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  or an error code
109f5 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   is something go
109f6 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61  es wrong..*/.sta
109f7 74 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62  tic int write32b
109f8 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  its(sqlite3_file
109f9 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74   *fd, i64 offset
109fa 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 68  , u32 val){.  ch
109fb 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 33  ar ac[4];.  put3
109fc 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 0a  2bits(ac, val);.
109fd 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
109fe 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 20  OsWrite(fd, ac, 
109ff 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f  4, offset);.}../
10a00 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  *.** The argumen
10a01 74 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20  t to this macro 
10a02 69 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 69  is a file descri
10a03 70 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74  ptor (type sqlit
10a04 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65  e3_file*)..** Re
10a05 74 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 20  turn 0 if it is 
10a06 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e  not open, or non
10a07 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31  -zero (but not 1
10a08 29 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a  ) if it is..**.*
10a09 2a 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61  * This is so tha
10a0a 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61  t expressions ca
10a0b 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a  n be written as:
10a0c 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f  .**.**   if( isO
10a0d 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
10a0e 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e   ){ ....**.** in
10a0f 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20  stead of.**.**  
10a10 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64   if( pPager->jfd
10a11 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e  ->pMethods ){ ..
10a12 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f  ..*/.#define isO
10a13 70 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d  pen(pFd) ((pFd)-
10a14 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a  >pMethods)../*.*
10a15 2a 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 73  * If file pFd is
10a16 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69   open, call sqli
10a17 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e  te3OsUnlock() on
10a18 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69   it..*/.static i
10a19 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69  nt osUnlock(sqli
10a1a 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69  te3_file *pFd, i
10a1b 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28  nt eLock){.  if(
10a1c 20 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 7b   !isOpen(pFd) ){
10a1d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
10a1e 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74  TE_OK;.  }.  ret
10a1f 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  urn sqlite3OsUnl
10a20 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b  ock(pFd, eLock);
10a21 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
10a22 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e  unction determin
10a23 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  es whether or no
10a24 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69  t the atomic-wri
10a25 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a  te optimization.
10a26 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77  ** can be used w
10a27 69 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20  ith this pager. 
10a28 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  The optimization
10a29 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a   can be used if:
10a2a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20  .**.**  (a) the 
10a2b 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
10a2c 79 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  y OsDeviceCharac
10a2d 74 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69  teristics() indi
10a2e 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20  cates that.**   
10a2f 20 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61     a database pa
10a30 67 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65  ge may be writte
10a31 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e  n atomically, an
10a32 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61  d.**  (b) the va
10a33 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
10a34 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69  OsSectorSize() i
10a35 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
10a36 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20  qual.**      to 
10a37 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a  the page size..*
10a38 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a  *.** The optimiz
10a39 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 6c  ation is also al
10a3a 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f 72  ways enabled for
10a3b 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
10a3c 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 72  . It is.** an er
10a3d 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  ror to call this
10a3e 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 61   function if pPa
10a3f 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e  ger is opened on
10a40 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a   an in-memory.**
10a41 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
10a42 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61   If the optimiza
10a43 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75  tion cannot be u
10a44 73 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e  sed, 0 is return
10a45 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65  ed. If it can be
10a46 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74   used,.** then t
10a47 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
10a48 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66  d is the size of
10a49 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
10a4a 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e  e when it.** con
10a4b 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64  tains rollback d
10a4c 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20  ata for exactly 
10a4d 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66  one page..*/.#if
10a4e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
10a4f 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73  E_ATOMIC_WRITE.s
10a50 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75  tatic int jrnlBu
10a51 66 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a  fferSize(Pager *
10a52 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72  pPager){.  asser
10a53 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69  t( !MEMDB );.  i
10a54 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  f( !pPager->temp
10a55 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20  File ){.    int 
10a56 64 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dc;             
10a57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10a58 20 44 65 76 69 63 65 20 63 68 61 72 61 63 74 65   Device characte
10a59 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 69  ristics */.    i
10a5a 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20  nt nSector;     
10a5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a5c 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20   /* Sector size 
10a5d 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67  */.    int szPag
10a5e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
10a5f 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
10a60 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 73   size */..    as
10a61 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61  sert( isOpen(pPa
10a62 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 20  ger->fd) );.    
10a63 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65  dc = sqlite3OsDe
10a64 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
10a65 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ics(pPager->fd);
10a66 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 70  .    nSector = p
10a67 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
10a68 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d 20  e;.    szPage = 
10a69 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
10a6a 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 51  ;..    assert(SQ
10a6b 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
10a6c 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b  C512==(512>>8));
10a6d 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49  .    assert(SQLI
10a6e 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36  TE_IOCAP_ATOMIC6
10a6f 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b  4K==(65536>>8));
10a70 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 26  .    if( 0==(dc&
10a71 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54  (SQLITE_IOCAP_AT
10a72 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 29  OMIC|(szPage>>8)
10a73 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a 50  ) || nSector>szP
10a74 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65  age) ){.      re
10a75 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
10a76 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 52  }..  return JOUR
10a77 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
10a78 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f  r) + JOURNAL_PG_
10a79 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 65  SZ(pPager);.}.#e
10a7a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53  ndif../*.** If S
10a7b 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
10a7c 53 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65  S is defined the
10a7d 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 6e  n we do some san
10a7e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20  ity checking.** 
10a7f 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 69  on the cache usi
10a80 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 69  ng a hash functi
10a81 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  on.  This is use
10a82 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a  d for testing.**
10a83 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f   and debugging o
10a84 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  nly..*/.#ifdef S
10a85 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
10a86 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  S./*.** Return a
10a87 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 20   32-bit hash of 
10a88 74 68 65 20 70 61 67 65 20 64 61 74 61 20 66 6f  the page data fo
10a89 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74  r pPage..*/.stat
10a8a 69 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 74  ic u32 pager_dat
10a8b 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 2c  ahash(int nByte,
10a8c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
10a8d 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 61  pData){.  u32 ha
10a8e 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b  sh = 0;.  int i;
10a8f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42  .  for(i=0; i<nB
10a90 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 68  yte; i++){.    h
10a91 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 39  ash = (hash*1039
10a92 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 20  ) + pData[i];.  
10a93 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 3b  }.  return hash;
10a94 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 61  .}.static u32 pa
10a95 67 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 48  ger_pagehash(PgH
10a96 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65  dr *pPage){.  re
10a97 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 68  turn pager_datah
10a98 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 65  ash(pPage->pPage
10a99 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 6e  r->pageSize, (un
10a9a 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 50  signed char *)pP
10a9b 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a 73  age->pData);.}.s
10a9c 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72  tatic void pager
10a9d 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 67  _set_pagehash(Pg
10a9e 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 70  Hdr *pPage){.  p
10a9f 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 3d  Page->pageHash =
10aa0 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28   pager_pagehash(
10aa1 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pPage);.}../*.**
10aa2 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 20   The CHECK_PAGE 
10aa3 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 67  macro takes a Pg
10aa4 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 6d  Hdr* as an argum
10aa5 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f 43  ent. If SQLITE_C
10aa6 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 73  HECK_PAGES.** is
10aa7 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e 44   defined, and ND
10aa8 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69  EBUG is not defi
10aa9 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29  ned, an assert()
10aaa 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 6b   statement check
10aab 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 61  s.** that the pa
10aac 67 65 20 69 73 20 65 69 74 68 65 72 20 64 69 72  ge is either dir
10aad 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 63  ty or still matc
10aae 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 74  hes the calculat
10aaf 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a 2f  ed page-hash..*/
10ab0 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50  .#define CHECK_P
10ab1 41 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 65  AGE(x) checkPage
10ab2 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20  (x).static void 
10ab3 63 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 20  checkPage(PgHdr 
10ab4 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  *pPg){.  Pager *
10ab5 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
10ab6 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20  ager;.  assert( 
10ab7 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 7c  !pPg->pageHash |
10ab8 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  | pPager->errCod
10ab9 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 2d  e.      || (pPg-
10aba 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
10abb 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65  TY) || pPg->page
10abc 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65  Hash==pager_page
10abd 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a  hash(pPg) );.}..
10abe 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61  #else.#define pa
10abf 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59  ger_datahash(X,Y
10ac0 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67  )  0.#define pag
10ac1 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20  er_pagehash(X)  
10ac2 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  0.#define CHECK_
10ac3 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20  PAGE(x).#endif  
10ac4 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  /* SQLITE_CHECK_
10ac5 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  PAGES */../*.** 
10ac6 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c  When this is cal
10ac7 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  led the journal 
10ac8 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70  file for pager p
10ac9 50 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70  Pager must be op
10aca 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  en..** This func
10acb 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f  tion attempts to
10acc 20 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a   read a master j
10acd 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
10ace 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e   from the .** en
10acf 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e  d of the file an
10ad0 64 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  d, if successful
10ad1 2c 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f  , copies it into
10ad2 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64   memory supplied
10ad3 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c   .** by the call
10ad4 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73  er. See comments
10ad5 20 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74   above writeMast
10ad6 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20  erJournal() for 
10ad7 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73  the format.** us
10ad8 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61  ed to store a ma
10ad9 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
10ada 65 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e  e name at the en
10adb 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66  d of a journal f
10adc 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74  ile..**.** zMast
10add 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f  er must point to
10ade 20 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20   a buffer of at 
10adf 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79  least nMaster by
10ae0 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  tes allocated by
10ae1 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20  .** the caller. 
10ae2 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73  This should be s
10ae3 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74  qlite3_vfs.mxPat
10ae4 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75  hname+1 (to ensu
10ae5 72 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65  re there is.** e
10ae6 6e 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77  nough space to w
10ae7 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  rite the master 
10ae8 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49  journal name). I
10ae9 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  f the master jou
10aea 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20  rnal.** name in 
10aeb 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c  the journal is l
10aec 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74  onger than nMast
10aed 65 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64  er bytes (includ
10aee 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72  ing a.** nul-ter
10aef 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74  minator), then t
10af0 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61  his is handled a
10af1 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a  s if no master j
10af2 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77  ournal name.** w
10af3 65 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74  ere present in t
10af4 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a  he journal..**.*
10af5 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f  * If a master jo
10af6 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
10af7 69 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 68  is present at th
10af8 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75  e end of the jou
10af9 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68  rnal.** file, th
10afa 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20  en it is copied 
10afb 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20  into the buffer 
10afc 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d  pointed to by zM
10afd 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d  aster. A.** nul-
10afe 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20  terminator byte 
10aff 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  is appended to t
10b00 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77  he buffer follow
10b01 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a  ing the master.*
10b02 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e  * journal file n
10b03 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74  ame..**.** If it
10b04 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74   is determined t
10b05 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f  hat no master jo
10b06 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
10b07 69 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a  is present .** z
10b08 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74  Master[0] is set
10b09 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45   to 0 and SQLITE
10b0a 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a  _OK returned..**
10b0b 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
10b0c 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 61  occurs while rea
10b0d 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f  ding from the jo
10b0e 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53  urnal file, an S
10b0f 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63  QLite.** error c
10b10 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
10b11 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
10b12 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  eadMasterJournal
10b13 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
10b14 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73  Jrnl, char *zMas
10b15 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72  ter, u32 nMaster
10b16 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
10b17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b18 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
10b19 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20  /.  u32 len;    
10b1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10b1b 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65  * Length in byte
10b1c 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72  s of master jour
10b1d 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36  nal name */.  i6
10b1e 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20  4 szJ;          
10b1f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
10b20 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  l size in bytes 
10b21 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  of journal file 
10b22 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63  pJrnl */.  u32 c
10b23 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  ksum;           
10b24 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63        /* MJ chec
10b25 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20  ksum value read 
10b26 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  from journal */.
10b27 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20    u32 u;        
10b28 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b29 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f  Unsigned loop co
10b2a 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67  unter */.  unsig
10b2b 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b  ned char aMagic[
10b2c 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65  8];   /* A buffe
10b2d 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61  r to hold the ma
10b2e 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  gic header */.  
10b2f 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30  zMaster[0] = '\0
10b30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45  ';..  if( SQLITE
10b31 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK!=(rc = sqlit
10b32 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72  e3OsFileSize(pJr
10b33 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c  nl, &szJ)).   ||
10b34 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51   szJ<16.   || SQ
10b35 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
10b36 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c  ead32bits(pJrnl,
10b37 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a   szJ-16, &len)).
10b38 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74     || len>=nMast
10b39 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45  er .   || SQLITE
10b3a 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33  _OK!=(rc = read3
10b3b 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a  2bits(pJrnl, szJ
10b3c 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20  -12, &cksum)).  
10b3d 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
10b3e 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
10b3f 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63  ad(pJrnl, aMagic
10b40 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20  , 8, szJ-8)).   
10b41 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63  || memcmp(aMagic
10b42 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c  , aJournalMagic,
10b43 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45   8).   || SQLITE
10b44 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK!=(rc = sqlit
10b45 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20  e3OsRead(pJrnl, 
10b46 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a  zMaster, len, sz
10b47 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a  J-16-len)).  ){.
10b48 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
10b49 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20   }..  /* See if 
10b4a 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74  the checksum mat
10b4b 63 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 20  ches the master 
10b4c 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a  journal name */.
10b4d 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e    for(u=0; u<len
10b4e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75  ; u++){.    cksu
10b4f 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b  m -= zMaster[u];
10b50 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d  .  }.  if( cksum
10b51 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
10b52 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e  e checksum doesn
10b53 27 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20  't add up, then 
10b54 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
10b55 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a  he disk sectors.
10b56 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e      ** containin
10b57 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  g the master jou
10b58 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73  rnal filename is
10b59 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73   corrupted. This
10b5a 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65   means.    ** de
10b5b 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61  finitely roll ba
10b5c 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75  ck, so just retu
10b5d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64  rn SQLITE_OK and
10b5e 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a   report a (nul).
10b5f 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f      ** master-jo
10b60 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a  urnal filename..
10b61 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d      */.    len =
10b62 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65   0;.  }.  zMaste
10b63 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20  r[len] = '\0';. 
10b64 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49    .  return SQLI
10b65 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
10b66 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65  Return the offse
10b67 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20  t of the sector 
10b68 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69  boundary at or i
10b69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66  mmediately .** f
10b6a 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c  ollowing the val
10b6b 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f  ue in pPager->jo
10b6c 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69  urnalOff, assumi
10b6d 6e 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20  ng a sector .** 
10b6e 73 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e  size of pPager->
10b6f 73 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73  sectorSize bytes
10b70 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20  ..**.** i.e for 
10b71 61 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66  a sector size of
10b72 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61   512:.**.**   Pa
10b73 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20  ger.journalOff  
10b74 20 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 76          Return v
10b75 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  alue.**   ------
10b76 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10b77 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10b78 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20  -.**   0        
10b79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b7a 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20   0.**   512     
10b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b7c 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20    512.**   100  
10b7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b7e 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30       512.**   20
10b7f 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
10b80 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20          2048.** 
10b81 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a  .*/.static i64 j
10b82 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28  ournalHdrOffset(
10b83 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
10b84 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30    i64 offset = 0
10b85 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67  ;.  i64 c = pPag
10b86 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a  er->journalOff;.
10b87 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f    if( c ){.    o
10b88 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a  ffset = ((c-1)/J
10b89 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
10b8a 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55  ager) + 1) * JOU
10b8b 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
10b8c 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  er);.  }.  asser
10b8d 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41  t( offset%JOURNA
10b8e 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
10b8f 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
10b90 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20   offset>=c );.  
10b91 61 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d  assert( (offset-
10b92 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  c)<JOURNAL_HDR_S
10b93 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72  Z(pPager) );.  r
10b94 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a  eturn offset;.}.
10b95 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e  ./*.** The journ
10b96 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20  al file must be 
10b97 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66  open when this f
10b98 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10b99 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  d..**.** This fu
10b9a 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
10b9b 70 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  p if the journal
10b9c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65   file has not be
10b9d 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a  en written to.**
10b9e 20 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 72   within the curr
10b9f 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
10ba0 28 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a  (i.e. if Pager.j
10ba1 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a  ournalOff==0)..*
10ba2 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61  *.** If doTrunca
10ba3 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f  te is non-zero o
10ba4 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72  r the Pager.jour
10ba5 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72  nalSizeLimit var
10ba6 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20  iable is.** set 
10ba7 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63  to 0, then trunc
10ba8 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ate the journal 
10ba9 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74  file to zero byt
10baa 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65  es in size. Othe
10bab 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74  rwise,.** zero t
10bac 68 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 65  he 28-byte heade
10bad 72 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  r at the start o
10bae 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
10baf 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61  le. In either ca
10bb0 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70  se, .** if the p
10bb1 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e  ager is not in n
10bb2 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e  o-sync mode, syn
10bb3 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  c the journal fi
10bb4 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a  le immediately .
10bb5 2a 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e 67  ** after writing
10bb6 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69   or truncating i
10bb7 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65  t..**.** If Page
10bb8 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  r.journalSizeLim
10bb9 69 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 70  it is set to a p
10bba 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72  ositive, non-zer
10bbb 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20  o value, and.** 
10bbc 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72  following the tr
10bbd 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f  uncation or zero
10bbe 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 62  ing described ab
10bbf 6f 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ove the size of 
10bc0 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  the .** journal 
10bc1 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73  file in bytes is
10bc2 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69   larger than thi
10bc3 73 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72  s value, then tr
10bc4 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f  uncate the.** jo
10bc5 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61  urnal file to Pa
10bc6 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c  ger.journalSizeL
10bc7 69 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 20  imit bytes. The 
10bc8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65  journal file doe
10bc9 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f  s.** not need to
10bca 20 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f   be synced follo
10bcb 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74  wing this operat
10bcc 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ion..**.** If an
10bcd 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
10bce 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73  , abandon proces
10bcf 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20  sing and return 
10bd0 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64  the IO error cod
10bd1 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  e..** Otherwise,
10bd2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
10bd3 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  K..*/.static int
10bd4 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28   zeroJournalHdr(
10bd5 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
10bd6 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a  nt doTruncate){.
10bd7 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
10bd8 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
10bd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10bda 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
10bdb 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  de */.  assert( 
10bdc 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
10bdd 66 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61  fd) );.  if( pPa
10bde 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
10bdf 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34  ){.    const i64
10be0 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72   iLimit = pPager
10be1 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  ->journalSizeLim
10be2 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20  it;    /* Local 
10be3 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a  cache of jsl */.
10be4 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a  .    IOTRACE(("J
10be5 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70  ZEROHDR %p\n", p
10be6 50 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20  Pager)).    if( 
10be7 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c  doTruncate || iL
10be8 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  imit==0 ){.     
10be9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
10bea 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
10beb 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c  jfd, 0);.    }el
10bec 73 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63  se{.      static
10bed 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f   const char zero
10bee 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20  Hdr[28] = {0};. 
10bef 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
10bf0 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
10bf1 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73  >jfd, zeroHdr, s
10bf2 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20  izeof(zeroHdr), 
10bf3 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  0);.    }.    if
10bf4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
10bf5 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79  && !pPager->noSy
10bf6 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  nc ){.      rc =
10bf7 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
10bf8 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49  Pager->jfd, SQLI
10bf9 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
10bfa 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c  |pPager->sync_fl
10bfb 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ags);.    }..   
10bfc 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
10bfd 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  t the transactio
10bfe 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62  n is committed b
10bff 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63  ut the write loc
10c00 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69  k .    ** is sti
10c01 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66  ll held on the f
10c02 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73  ile. If there is
10c03 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f   a size limit co
10c04 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20  nfigured for .  
10c05 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74    ** the persist
10c06 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  ent journal and 
10c07 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
10c08 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75   currently consu
10c09 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20  mes more.    ** 
10c0a 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20  space than that 
10c0b 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72  limit allows for
10c0c 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f  , truncate it no
10c0d 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e  w. There is no n
10c0e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79  eed.    ** to sy
10c0f 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c  nc the file foll
10c10 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61  owing this opera
10c11 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
10c12 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
10c13 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29  OK && iLimit>0 )
10c14 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a  {.      i64 sz;.
10c15 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
10c16 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
10c17 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a  ger->jfd, &sz);.
10c18 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
10c19 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c  LITE_OK && sz>iL
10c1a 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  imit ){.        
10c1b 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72  rc = sqlite3OsTr
10c1c 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a  uncate(pPager->j
10c1d 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  fd, iLimit);.   
10c1e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
10c1f 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
10c20 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c  *.** The journal
10c21 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70   file must be op
10c22 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  en when this rou
10c23 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20  tine is called. 
10c24 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61  A journal.** hea
10c25 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52  der (JOURNAL_HDR
10c26 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72  _SZ bytes) is wr
10c27 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
10c28 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74  ournal file at t
10c29 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f  he.** current lo
10c2a 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cation..**.** Th
10c2b 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65  e format for the
10c2c 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
10c2d 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
10c2e 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67  * - 8 bytes: Mag
10c2f 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a  ic identifying j
10c30 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a  ournal format..*
10c31 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d  * - 4 bytes: Num
10c32 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69  ber of records i
10c33 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31  n journal, or -1
10c34 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73   no-sync mode is
10c35 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65   on..** - 4 byte
10c36 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  s: Random number
10c37 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68   used for page h
10c38 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65  ash..** - 4 byte
10c39 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62  s: Initial datab
10c3a 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a  ase page count..
10c3b 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65  ** - 4 bytes: Se
10c3c 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62  ctor size used b
10c3d 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68  y the process th
10c3e 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f  at wrote this jo
10c3f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79  urnal..** - 4 by
10c40 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61  tes: Database pa
10c41 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20  ge size..** .** 
10c42 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55  Followed by (JOU
10c43 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38  RNAL_HDR_SZ - 28
10c44 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65  ) bytes of unuse
10c45 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74  d space..*/.stat
10c46 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72  ic int writeJour
10c47 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50  nalHdr(Pager *pP
10c48 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  ager){.  int rc 
10c49 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
10c4a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10c4b 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
10c4c 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d   char *zHeader =
10c4d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61   pPager->pTmpSpa
10c4e 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72  ce;  /* Temporar
10c4f 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20  y space used to 
10c50 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a  build header */.
10c51 20 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d 20    u32 nHeader = 
10c52 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
10c53 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66  ;     /* Size of
10c54 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20   buffer pointed 
10c55 74 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a 2f  to by zHeader */
10c56 0a 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 20  .  u32 nWrite;  
10c57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10c58 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
10c59 6f 66 20 68 65 61 64 65 72 20 73 65 63 74 6f 72  of header sector
10c5a 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e   written */.  in
10c5b 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20  t ii;           
10c5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10c5d 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
10c5e 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  r */..  assert( 
10c5f 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
10c60 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a  fd) );      /* J
10c61 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74  ournal file must
10c62 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20   be open. */..  
10c63 69 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52  if( nHeader>JOUR
10c64 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
10c65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65  r) ){.    nHeade
10c66 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  r = JOURNAL_HDR_
10c67 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  SZ(pPager);.  }.
10c68 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
10c69 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f  re active savepo
10c6a 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 20  ints and any of 
10c6b 74 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 65  them were create
10c6c 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 68  d .  ** since th
10c6d 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f  e most recent jo
10c6e 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 73  urnal header was
10c6f 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65   written, update
10c70 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72   the .  ** Pager
10c71 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66  Savepoint.iHdrOf
10c72 66 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e  fset fields now.
10c73 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30  .  */.  for(ii=0
10c74 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61  ; ii<pPager->nSa
10c75 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a  vepoint; ii++){.
10c76 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
10c77 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69  aSavepoint[ii].i
10c78 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a  HdrOffset==0 ){.
10c79 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53        pPager->aS
10c7a 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64  avepoint[ii].iHd
10c7b 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72  rOffset = pPager
10c7c 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20  ->journalOff;.  
10c7d 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65    }.  }..  pPage
10c7e 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20  r->journalHdr = 
10c7f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
10c80 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f  ff = journalHdrO
10c81 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  ffset(pPager);. 
10c82 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c   memcpy(zHeader,
10c83 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
10c84 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d  sizeof(aJournalM
10c85 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a 20 0a 20  agic));..  /* . 
10c86 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52   ** Write the nR
10c87 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e  ec Field - the n
10c88 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65  umber of page re
10c89 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f  cords that follo
10c8a 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72  w this.  ** jour
10c8b 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d  nal header. Norm
10c8c 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72  ally, zero is wr
10c8d 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61  itten to this va
10c8e 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 6d 65  lue at this time
10c8f 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65  ..  ** After the
10c90 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 64   records are add
10c91 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  ed to the journa
10c92 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e  l (and the journ
10c93 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a  al synced, .  **
10c94 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63   if in full-sync
10c95 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f   mode), the zero
10c96 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20   is overwritten 
10c97 77 69 74 68 20 74 68 65 20 74 72 75 65 20 6e 75  with the true nu
10c98 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63  mber.  ** of rec
10c99 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f  ords (see syncJo
10c9a 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20  urnal())..  **. 
10c9b 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74   ** A faster alt
10c9c 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77  ernative is to w
10c9d 72 69 74 65 20 30 78 46 46 46 46 46 46 46 46 20  rite 0xFFFFFFFF 
10c9e 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  to the nRec fiel
10c9f 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61  d. When.  ** rea
10ca0 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ding the journal
10ca1 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c   this value tell
10ca2 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75  s SQLite to assu
10ca3 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a  me that the.  **
10ca4 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75   rest of the jou
10ca5 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69  rnal file contai
10ca6 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 65  ns valid page re
10ca7 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 75  cords. This assu
10ca8 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64  mption.  ** is d
10ca9 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20  angerous, as if 
10caa 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72  a failure occurr
10cab 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 6e  ed whilst writin
10cac 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  g to the journal
10cad 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61  .  ** file it ma
10cae 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67  y contain some g
10caf 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 65  arbage data. The
10cb0 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e 61  re are two scena
10cb1 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 20  rios.  ** where 
10cb2 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 65  this risk can be
10cb3 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20   ignored:.  **. 
10cb4 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65   **   * When the
10cb5 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d   pager is in no-
10cb6 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75  sync mode. Corru
10cb7 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77  ption can follow
10cb8 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65   a.  **     powe
10cb9 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69  r failure in thi
10cba 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a 20  s case anyway.. 
10cbb 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65   **.  **   * Whe
10cbc 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43  n the SQLITE_IOC
10cbd 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 66  AP_SAFE_APPEND f
10cbe 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 73  lag is set. This
10cbf 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a   guarantees.  **
10cc0 20 20 20 20 20 74 68 61 74 20 67 61 72 62 61 67       that garbag
10cc1 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20  e data is never 
10cc2 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20  appended to the 
10cc3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20  journal file..  
10cc4 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  */.  assert( isO
10cc5 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
10cc6 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  || pPager->noSyn
10cc7 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67  c );.  if( (pPag
10cc8 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28  er->noSync) || (
10cc9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
10cca 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
10ccb 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20  ALMODE_MEMORY). 
10ccc 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44    || (sqlite3OsD
10ccd 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
10cce 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
10ccf 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41  &SQLITE_IOCAP_SA
10cd0 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b  FE_APPEND) .  ){
10cd1 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26  .    put32bits(&
10cd2 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
10cd3 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20  JournalMagic)], 
10cd4 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 7d  0xffffffff);.  }
10cd5 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33 32 62  else{.    put32b
10cd6 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a  its(&zHeader[siz
10cd7 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
10cd8 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  c)], 0);.  }..  
10cd9 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68  /* The random ch
10cda 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c  eck-hash initial
10cdb 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74  iser */ .  sqlit
10cdc 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69  e3_randomness(si
10cdd 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73  zeof(pPager->cks
10cde 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72  umInit), &pPager
10cdf 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20  ->cksumInit);.  
10ce0 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
10ce1 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
10ce2 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61  alMagic)+4], pPa
10ce3 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b  ger->cksumInit);
10ce4 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61  .  /* The initia
10ce5 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  l database size 
10ce6 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  */.  put32bits(&
10ce7 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
10ce8 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d  JournalMagic)+8]
10ce9 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  , pPager->dbOrig
10cea 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20  Size);.  /* The 
10ceb 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73  assumed sector s
10cec 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f  ize for this pro
10ced 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62  cess */.  put32b
10cee 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a  its(&zHeader[siz
10cef 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
10cf0 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e  c)+12], pPager->
10cf1 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20  sectorSize);..  
10cf2 2f 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65  /* The page size
10cf3 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28   */.  put32bits(
10cf4 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
10cf5 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31  aJournalMagic)+1
10cf6 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  6], pPager->page
10cf7 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69  Size);..  /* Ini
10cf8 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 61  tializing the ta
10cf9 69 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  il of the buffer
10cfa 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
10cfb 79 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a 20  y.  Everything. 
10cfc 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20 69   ** works find i
10cfd 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
10cfe 6d 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69 74  memset() is omit
10cff 74 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69 61  ted.  But initia
10d00 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  lizing.  ** the 
10d01 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73 20  memory prevents 
10d02 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63 6f  valgrind from co
10d03 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77 65  mplaining, so we
10d04 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f 0a   are willing to.
10d05 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70 65    ** take the pe
10d06 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a 20  rformance hit.. 
10d07 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a 48   */.  memset(&zH
10d08 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f  eader[sizeof(aJo
10d09 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d 2c  urnalMagic)+20],
10d0a 20 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48 65   0,.         nHe
10d0b 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a 6f  ader-(sizeof(aJo
10d0c 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29 29  urnalMagic)+20))
10d0d 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72  ;..  /* In theor
10d0e 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e 65  y, it is only ne
10d0f 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65  cessary to write
10d10 20 74 68 65 20 32 38 20 62 79 74 65 73 20 74 68   the 28 bytes th
10d11 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f 75  at the .  ** jou
10d12 72 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e 73  rnal header cons
10d13 75 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 72  umes to the jour
10d14 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20 54  nal file here. T
10d15 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 68  hen increment th
10d16 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a 6f  e .  ** Pager.jo
10d17 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62 6c  urnalOff variabl
10d18 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  e by JOURNAL_HDR
10d19 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65 20  _SZ so that the 
10d1a 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f 72  next .  ** recor
10d1b 64 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  d is written to 
10d1c 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65  the following se
10d1d 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61 20  ctor (leaving a 
10d1e 67 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65 0a  gap in the file.
10d1f 20 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20 62    ** that will b
10d20 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69 6c  e implicitly fil
10d21 6c 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f 53  led in by the OS
10d22 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77  )..  **.  ** How
10d23 65 76 65 72 20 69 74 20 68 61 73 20 62 65 65 6e  ever it has been
10d24 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74   discovered that
10d25 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73   on some systems
10d26 20 74 68 69 73 20 70 61 74 74 65 72 6e 20 63 61   this pattern ca
10d27 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e 69  n .  ** be signi
10d28 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 20  ficantly slower 
10d29 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 6c  than contiguousl
10d2a 79 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74  y writing data t
10d2b 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a 2a  o the file,.  **
10d2c 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65   even if that me
10d2d 61 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 77  ans explicitly w
10d2e 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74  riting data to t
10d2f 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a  he block of .  *
10d30 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  * (JOURNAL_HDR_S
10d31 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 74 68  Z - 28) bytes th
10d32 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75  at will not be u
10d33 73 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73 20  sed. So that is 
10d34 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f 6e  what.  ** is don
10d35 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  e. .  **.  ** Th
10d36 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69 72  e loop is requir
10d37 65 64 20 68 65 72 65 20 69 6e 20 63 61 73 65 20  ed here in case 
10d38 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20  the sector-size 
10d39 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
10d3a 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  he .  ** databas
10d3b 65 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69 6e  e page size. Sin
10d3c 63 65 20 74 68 65 20 7a 48 65 61 64 65 72 20 62  ce the zHeader b
10d3d 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50 61  uffer is only Pa
10d3e 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 2a  ger.pageSize.  *
10d3f 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c  * bytes in size,
10d40 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 63   more than one c
10d41 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73  all to sqlite3Os
10d42 57 72 69 74 65 28 29 20 6d 61 79 20 62 65 20 72  Write() may be r
10d43 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f 20  equired.  ** to 
10d44 70 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e 74  populate the ent
10d45 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ire journal head
10d46 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f 20  er sector..  */ 
10d47 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30 3b  .  for(nWrite=0;
10d48 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26   rc==SQLITE_OK&&
10d49 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48  nWrite<JOURNAL_H
10d4a 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20 6e  DR_SZ(pPager); n
10d4b 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29 7b  Write+=nHeader){
10d4c 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a  .    IOTRACE(("J
10d4d 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e  HDR %p %lld %d\n
10d4e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65  ", pPager, pPage
10d4f 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 6e  r->journalHdr, n
10d50 48 65 61 64 65 72 29 29 0a 20 20 20 20 72 63 20  Header)).    rc 
10d51 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
10d52 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48  (pPager->jfd, zH
10d53 65 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c 20  eader, nHeader, 
10d54 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
10d55 66 66 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  ff);.    pPager-
10d56 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 6e  >journalOff += n
10d57 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 72  Header;.  }..  r
10d58 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
10d59 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ** The journal f
10d5a 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ile must be open
10d5b 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61   when this is ca
10d5c 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 20  lled. A journal 
10d5d 68 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20 28  header file.** (
10d5e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62  JOURNAL_HDR_SZ b
10d5f 79 74 65 73 29 20 69 73 20 72 65 61 64 20 66 72  ytes) is read fr
10d60 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  om the current l
10d61 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a  ocation in the j
10d62 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20  ournal.** file. 
10d63 54 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61  The current loca
10d64 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72  tion in the jour
10d65 6e 61 6c 20 66 69 6c 65 20 69 73 20 67 69 76 65  nal file is give
10d66 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 2d 3e  n by.** pPager->
10d67 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65 20  journalOff. See 
10d68 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66  comments above f
10d69 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75  unction writeJou
10d6a 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a 2a 2a  rnalHdr() for.**
10d6b 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f   a description o
10d6c 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  f the journal he
10d6d 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a  ader format..**.
10d6e 2a 2a 20 49 66 20 74 68 65 20 68 65 61 64 65 72  ** If the header
10d6f 20 69 73 20 72 65 61 64 20 73 75 63 63 65 73 73   is read success
10d70 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 69 73  fully, *pNRec is
10d71 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62   set to the numb
10d72 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72 65  er of.** page re
10d73 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20  cords following 
10d74 74 68 69 73 20 68 65 61 64 65 72 20 61 6e 64 20  this header and 
10d75 2a 70 44 62 53 69 7a 65 20 69 73 20 73 65 74 20  *pDbSize is set 
10d76 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  to the size of t
10d77 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 62  he.** database b
10d78 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61  efore the transa
10d79 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e 20  ction began, in 
10d7a 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50 61  pages. Also, pPa
10d7b 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a  ger->cksumInit.*
10d7c 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  * is set to the 
10d7d 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20  value read from 
10d7e 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
10d7f 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  er. SQLITE_OK is
10d80 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e 20   returned.** in 
10d81 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  this case..**.**
10d82 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
10d83 68 65 61 64 65 72 20 66 69 6c 65 20 61 70 70 65  header file appe
10d84 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70  ars to be corrup
10d85 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45  ted, SQLITE_DONE
10d86 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20   is.** returned 
10d87 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 20 2a  and *pNRec and *
10d88 50 44 62 53 69 7a 65 20 61 72 65 20 75 6e 64 65  PDbSize are unde
10d89 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 52 4e  fined.  If JOURN
10d8a 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 0a  AL_HDR_SZ bytes.
10d8b 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 61  ** cannot be rea
10d8c 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  d from the journ
10d8d 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 6f 72  al file an error
10d8e 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
10d8f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
10d90 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28   readJournalHdr(
10d91 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
10d92 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
10d93 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
10d94 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c  */.  i64 journal
10d95 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20  Size,           
10d96 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
10d97 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   open journal fi
10d98 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  le in bytes */. 
10d99 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 20 20   u32 *pNRec,    
10d9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10d9b 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 61 64   OUT: Value read
10d9c 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 20 66   from the nRec f
10d9d 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 2a 70  ield */.  u32 *p
10d9e 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 20 20  DbSize          
10d9f 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56         /* OUT: V
10da0 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e 61 6c  alue of original
10da1 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 66   database size f
10da2 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ield */.){.  int
10da3 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
10da4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
10da5 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e  urn code */.  un
10da6 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67  signed char aMag
10da7 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41 20  ic[8];     /* A 
10da8 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
10da9 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 20  he magic header 
10daa 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66  */.  i64 iHdrOff
10dab 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10dac 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 6a    /* Offset of j
10dad 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 62 65  ournal header be
10dae 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61  ing read */..  a
10daf 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
10db0 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 20  ager->jfd) );   
10db1 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 69     /* Journal fi
10db2 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e  le must be open.
10db3 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 6e 63   */..  /* Advanc
10db4 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f  e Pager.journalO
10db5 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  ff to the start 
10db6 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 63 74  of the next sect
10db7 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a 20  or. If the.  ** 
10db8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
10db9 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68  too small for th
10dba 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 61 64  ere to be a head
10dbb 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 68 69  er stored at thi
10dbc 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 72 65  s.  ** point, re
10dbd 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
10dbe 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d  ..  */.  pPager-
10dbf 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f  >journalOff = jo
10dc0 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70  urnalHdrOffset(p
10dc1 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50  Pager);.  if( pP
10dc2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
10dc3 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  +JOURNAL_HDR_SZ(
10dc4 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61  pPager) > journa
10dc5 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74  lSize ){.    ret
10dc6 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
10dc7 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d  .  }.  iHdrOff =
10dc8 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
10dc9 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20  Off;..  /* Read 
10dca 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 62  in the first 8 b
10dcb 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  ytes of the jour
10dcc 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20 74  nal header. If t
10dcd 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68  hey do not match
10dce 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69 63  .  ** the  magic
10dcf 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61 74   string found at
10dd0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 61   the start of ea
10dd1 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  ch journal heade
10dd2 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 53  r, return.  ** S
10dd3 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 20 61  QLITE_DONE. If a
10dd4 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
10dd5 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  s, return an err
10dd6 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69  or code. Otherwi
10dd7 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 65 64  se,.  ** proceed
10dd8 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ..  */.  rc = sq
10dd9 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
10dda 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c  er->jfd, aMagic,
10ddb 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c   sizeof(aMagic),
10ddc 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 69 66 28   iHdrOff);.  if(
10ddd 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72   rc ){.    retur
10dde 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20  n rc;.  }.  if( 
10ddf 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61  memcmp(aMagic, a
10de0 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69  JournalMagic, si
10de1 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d 30  zeof(aMagic))!=0
10de2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
10de3 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a  QLITE_DONE;.  }.
10de4 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 66  .  /* Read the f
10de5 69 72 73 74 20 74 68 72 65 65 20 33 32 2d 62 69  irst three 32-bi
10de6 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  t fields of the 
10de7 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a 20  journal header: 
10de8 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 66 69  The nRec.  ** fi
10de9 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73 75  eld, the checksu
10dea 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61 6e  m-initializer an
10deb 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
10dec 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72 74  ize at the start
10ded 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 61  .  ** of the tra
10dee 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72 6e  nsaction. Return
10def 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
10df0 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20  f anything goes 
10df1 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66  wrong..  */.  if
10df2 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
10df3 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50   = read32bits(pP
10df4 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f  ager->jfd, iHdrO
10df5 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a 20 20  ff+8, pNRec)).  
10df6 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
10df7 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
10df8 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
10df9 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 72  rOff+12, &pPager
10dfa 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a 20 20  ->cksumInit)).  
10dfb 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
10dfc 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
10dfd 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
10dfe 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 7a 65  rOff+16, pDbSize
10dff 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  )).  ){.    retu
10e00 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66  rn rc;.  }..  if
10e01 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
10e02 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 75  lOff==0 ){.    u
10e03 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 20 20  32 iPageSize;   
10e04 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
10e05 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f  age-size field o
10e06 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  f journal header
10e07 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 65 63   */.    u32 iSec
10e08 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20  torSize;        
10e09 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 2d 73       /* Sector-s
10e0a 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75  ize field of jou
10e0b 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20  rnal header */. 
10e0c 20 20 20 75 31 36 20 69 50 61 67 65 53 69 7a 65     u16 iPageSize
10e0d 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  16;             
10e0e 2f 2a 20 43 6f 70 79 20 6f 66 20 69 50 61 67 65  /* Copy of iPage
10e0f 53 69 7a 65 20 69 6e 20 31 36 2d 62 69 74 20 76  Size in 16-bit v
10e10 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20  ariable */..    
10e11 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65  /* Read the page
10e12 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72  -size and sector
10e13 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20 68 65  -size journal he
10e14 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a 2f 0a  ader fields. */.
10e15 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
10e16 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62  K!=(rc = read32b
10e17 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
10e18 20 69 48 64 72 4f 66 66 2b 32 30 2c 20 26 69 53   iHdrOff+20, &iS
10e19 65 63 74 6f 72 53 69 7a 65 29 29 0a 20 20 20 20  ectorSize)).    
10e1a 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
10e1b 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
10e1c 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
10e1d 72 4f 66 66 2b 32 34 2c 20 26 69 50 61 67 65 53  rOff+24, &iPageS
10e1e 69 7a 65 29 29 0a 20 20 20 20 29 7b 0a 20 20 20  ize)).    ){.   
10e1f 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
10e20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63    }..    /* Chec
10e21 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  k that the value
10e22 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  s read from the 
10e23 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65  page-size and se
10e24 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 73  ctor-size fields
10e25 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 69 74 68  .    ** are with
10e26 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 62 65 20  in range. To be 
10e27 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 6f 74 68  'in range', both
10e28 20 76 61 6c 75 65 73 20 6e 65 65 64 20 74 6f 20   values need to 
10e29 62 65 20 61 20 70 6f 77 65 72 0a 20 20 20 20 2a  be a power.    *
10e2a 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 74 65 72  * of two greater
10e2b 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
10e2c 6f 20 35 31 32 2c 20 61 6e 64 20 6e 6f 74 20 67  o 512, and not g
10e2d 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 69  reater than thei
10e2e 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 65 63  r .    ** respec
10e2f 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69 6d  tive compile tim
10e30 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74 73  e maximum limits
10e31 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
10e32 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 20 20   iPageSize<512  
10e33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e34 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3c 35  || iSectorSize<5
10e35 31 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 65  12.     || iPage
10e36 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  Size>SQLITE_MAX_
10e37 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 65  PAGE_SIZE || iSe
10e38 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43  ctorSize>MAX_SEC
10e39 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c 7c  TOR_SIZE.     ||
10e3a 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 26   ((iPageSize-1)&
10e3b 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 20  iPageSize)!=0   
10e3c 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a 65  || ((iSectorSize
10e3d 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 29  -1)&iSectorSize)
10e3e 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  !=0 .    ){.    
10e3f 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 68    /* If the eith
10e40 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  er the page-size
10e41 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 20   or sector-size 
10e42 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68  in the journal-h
10e43 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 20  eader is .      
10e44 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 6e  ** invalid, then
10e45 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61   the process tha
10e46 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 72  t wrote the jour
10e47 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 20  nal-header must 
10e48 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 63  have .      ** c
10e49 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 68  rashed before th
10e4a 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 6e  e header was syn
10e4b 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ced. In this cas
10e4c 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 0a  e stop reading .
10e4d 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75        ** the jou
10e4e 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 0a  rnal file here..
10e4f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
10e50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
10e51 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  E;.    }..    /*
10e52 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 65   Update the page
10e53 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74  -size to match t
10e54 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
10e55 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  om the journal. 
10e56 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 65  .    ** Use a te
10e57 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 74  stcase() macro t
10e58 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
10e59 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
10e5a 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 50  within .    ** P
10e5b 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28  agerSetPagesize(
10e5c 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 20  ) is tested..   
10e5d 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69 7a   */.    iPageSiz
10e5e 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67 65  e16 = (u16)iPage
10e5f 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73  Size;.    rc = s
10e60 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
10e61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26  gesize(pPager, &
10e62 69 50 61 67 65 53 69 7a 65 31 36 29 3b 0a 20 20  iPageSize16);.  
10e63 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d    testcase( rc!=
10e64 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
10e65 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
10e66 49 54 45 5f 4f 4b 20 7c 7c 20 69 50 61 67 65 53  ITE_OK || iPageS
10e67 69 7a 65 31 36 3d 3d 28 75 31 36 29 69 50 61 67  ize16==(u16)iPag
10e68 65 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20 2f 2a  eSize );..    /*
10e69 20 55 70 64 61 74 65 20 74 68 65 20 61 73 73 75   Update the assu
10e6a 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20  med sector-size 
10e6b 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c  to match the val
10e6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20 20 20  ue used by .    
10e6d 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20 74  ** the process t
10e6e 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73  hat created this
10e6f 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 69   journal. If thi
10e70 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20  s journal was.  
10e71 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20    ** created by 
10e72 61 20 70 72 6f 63 65 73 73 20 6f 74 68 65 72 20  a process other 
10e73 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74  than this one, t
10e74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
10e75 0a 20 20 20 20 2a 2a 20 69 73 20 62 65 69 6e 67  .    ** is being
10e76 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
10e77 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 62 61  hin pager_playba
10e78 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c 20  ck(). The local 
10e79 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20  value.    ** of 
10e7a 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65  Pager.sectorSize
10e7b 20 69 73 20 72 65 73 74 6f 72 65 64 20 61 74 20   is restored at 
10e7c 74 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74 20  the end of that 
10e7d 72 6f 75 74 69 6e 65 2e 0a 20 20 20 20 2a 2f 0a  routine..    */.
10e7e 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74      pPager->sect
10e7f 6f 72 53 69 7a 65 20 3d 20 69 53 65 63 74 6f 72  orSize = iSector
10e80 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50 61  Size;.  }..  pPa
10e81 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
10e82 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  += JOURNAL_HDR_S
10e83 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74  Z(pPager);.  ret
10e84 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
10e85 2a 20 57 72 69 74 65 20 74 68 65 20 73 75 70 70  * Write the supp
10e86 6c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f 75 72  lied master jour
10e87 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68  nal name into th
10e88 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
10e89 6f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 61 67  or pager.** pPag
10e8a 65 72 20 61 74 20 74 68 65 20 63 75 72 72 65 6e  er at the curren
10e8b 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20  t location. The 
10e8c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
10e8d 61 6d 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  ame must be the 
10e8e 6c 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72  last.** thing wr
10e8f 69 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e  itten to a journ
10e90 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20  al file. If the 
10e91 70 61 67 65 72 20 69 73 20 69 6e 20 66 75 6c 6c  pager is in full
10e92 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a  -sync mode, the.
10e93 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
10e94 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 64  descriptor is ad
10e95 76 61 6e 63 65 64 20 74 6f 20 74 68 65 20 6e 65  vanced to the ne
10e96 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61  xt sector bounda
10e97 72 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79  ry before.** any
10e98 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e  thing is written
10e99 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 69 73 3a  . The format is:
10e9a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74  .**.**   + 4 byt
10e9b 65 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  es: PAGER_MJ_PGN
10e9c 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 74 65  O..**   + N byte
10e9d 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61  s: Master journa
10e9e 6c 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 75 74  l filename in ut
10e9f 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79  f-8..**   + 4 by
10ea0 74 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68 20 6f  tes: N (length o
10ea1 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  f master journal
10ea2 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 2c 20   name in bytes, 
10ea3 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  no nul-terminato
10ea4 72 29 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74  r)..**   + 4 byt
10ea5 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e  es: Master journ
10ea6 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75 6d  al name checksum
10ea7 2e 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74 65 73  ..**   + 8 bytes
10ea8 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b  : aJournalMagic[
10ea9 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73  ]..**.** The mas
10eaa 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65  ter journal page
10eab 20 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 65   checksum is the
10eac 20 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 65   sum of the byte
10ead 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 0a  s in the master.
10eae 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c  ** journal name,
10eaf 20 77 68 65 72 65 20 65 61 63 68 20 62 79 74 65   where each byte
10eb0 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20   is interpreted 
10eb1 61 73 20 61 20 73 69 67 6e 65 64 20 38 2d 62 69  as a signed 8-bi
10eb2 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a  t integer..**.**
10eb3 20 49 66 20 7a 4d 61 73 74 65 72 20 69 73 20 61   If zMaster is a
10eb4 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 6f   NULL pointer (o
10eb5 63 63 75 72 73 20 66 6f 72 20 61 20 73 69 6e 67  ccurs for a sing
10eb6 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e  le database tran
10eb7 73 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68  saction), .** th
10eb8 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
10eb9 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
10eba 74 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75  t writeMasterJou
10ebb 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67  rnal(Pager *pPag
10ebc 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  er, const char *
10ebd 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20  zMaster){.  int 
10ebe 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
10ebf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10ec0 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
10ec1 20 69 6e 74 20 6e 4d 61 73 74 65 72 3b 20 20 20   int nMaster;   
10ec2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ec3 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73    /* Length of s
10ec4 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f  tring zMaster */
10ec5 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20  .  i64 iHdrOff; 
10ec6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ec7 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66      /* Offset of
10ec8 20 68 65 61 64 65 72 20 69 6e 20 6a 6f 75 72 6e   header in journ
10ec9 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34  al file */.  i64
10eca 20 6a 72 6e 6c 53 69 7a 65 3b 20 20 20 20 20 20   jrnlSize;      
10ecb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10ecc 20 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c   Size of journal
10ecd 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 2a 2f   file on disk */
10ece 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 30  .  u32 cksum = 0
10ecf 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10ed0 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20      /* Checksum 
10ed1 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65  of string zMaste
10ed2 72 20 2a 2f 0a 0a 20 20 69 66 28 20 21 7a 4d 61  r */..  if( !zMa
10ed3 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e  ster || pPager->
10ed4 73 65 74 4d 61 73 74 65 72 0a 20 20 20 7c 7c 20  setMaster.   || 
10ed5 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
10ed6 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
10ed7 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20  ALMODE_MEMORY . 
10ed8 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75    || pPager->jou
10ed9 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
10eda 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20  JOURNALMODE_OFF 
10edb 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
10edc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
10edd 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73    pPager->setMas
10ede 74 65 72 20 3d 20 31 3b 0a 20 20 61 73 73 65 72  ter = 1;.  asser
10edf 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
10ee0 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20  ->jfd) );..  /* 
10ee1 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 6c 65  Calculate the le
10ee2 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 61 6e  ngth in bytes an
10ee3 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6f  d the checksum o
10ee4 66 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 66  f zMaster */.  f
10ee5 6f 72 28 6e 4d 61 73 74 65 72 3d 30 3b 20 7a 4d  or(nMaster=0; zM
10ee6 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 20  aster[nMaster]; 
10ee7 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 20 20 20  nMaster++){.    
10ee8 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72  cksum += zMaster
10ee9 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 7d 0a 0a  [nMaster];.  }..
10eea 20 20 2f 2a 20 49 66 20 69 6e 20 66 75 6c 6c 2d    /* If in full-
10eeb 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64 76 61 6e  sync mode, advan
10eec 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 64  ce to the next d
10eed 69 73 6b 20 73 65 63 74 6f 72 20 62 65 66 6f 72  isk sector befor
10eee 65 20 77 72 69 74 69 6e 67 0a 20 20 2a 2a 20 74  e writing.  ** t
10eef 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
10ef0 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20 69 73 20  l name. This is 
10ef1 69 6e 20 63 61 73 65 20 74 68 65 20 70 72 65 76  in case the prev
10ef2 69 6f 75 73 20 70 61 67 65 20 77 72 69 74 74 65  ious page writte
10ef3 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6a 6f  n to.  ** the jo
10ef4 75 72 6e 61 6c 20 68 61 73 20 61 6c 72 65 61 64  urnal has alread
10ef5 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a 20  y been synced.. 
10ef6 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72   */.  if( pPager
10ef7 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20  ->fullSync ){.  
10ef8 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
10ef9 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64  lOff = journalHd
10efa 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b  rOffset(pPager);
10efb 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d  .  }.  iHdrOff =
10efc 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
10efd 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65  Off;..  /* Write
10efe 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
10eff 6e 61 6c 20 64 61 74 61 20 74 6f 20 74 68 65 20  nal data to the 
10f00 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  end of the journ
10f01 61 6c 20 66 69 6c 65 2e 20 49 66 0a 20 20 2a 2a  al file. If.  **
10f02 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
10f03 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72  , return the err
10f04 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63  or code to the c
10f05 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  aller..  */.  if
10f06 28 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72  ( (0 != (rc = wr
10f07 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72  ite32bits(pPager
10f08 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2c 20  ->jfd, iHdrOff, 
10f09 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50  PAGER_MJ_PGNO(pP
10f0a 61 67 65 72 29 29 29 29 0a 20 20 20 7c 7c 20 28  ager)))).   || (
10f0b 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74  0 != (rc = sqlit
10f0c 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
10f0d 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20  ->jfd, zMaster, 
10f0e 6e 4d 61 73 74 65 72 2c 20 69 48 64 72 4f 66 66  nMaster, iHdrOff
10f0f 2b 34 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21  +4))).   || (0 !
10f10 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 62  = (rc = write32b
10f11 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
10f12 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74   iHdrOff+4+nMast
10f13 65 72 2c 20 6e 4d 61 73 74 65 72 29 29 29 0a 20  er, nMaster))). 
10f14 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d    || (0 != (rc =
10f15 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61   write32bits(pPa
10f16 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66  ger->jfd, iHdrOf
10f17 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 2c 20 63  f+4+nMaster+4, c
10f18 6b 73 75 6d 29 29 29 0a 20 20 20 7c 7c 20 28 30  ksum))).   || (0
10f19 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65   != (rc = sqlite
10f1a 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
10f1b 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61  >jfd, aJournalMa
10f1c 67 69 63 2c 20 38 2c 20 69 48 64 72 4f 66 66 2b  gic, 8, iHdrOff+
10f1d 34 2b 6e 4d 61 73 74 65 72 2b 38 29 29 29 0a 20  4+nMaster+8))). 
10f1e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
10f1f 63 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d  c;.  }.  pPager-
10f20 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28  >journalOff += (
10f21 6e 4d 61 73 74 65 72 2b 32 30 29 3b 0a 20 20 70  nMaster+20);.  p
10f22 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
10f23 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  = !pPager->noSyn
10f24 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  c;..  /* If the 
10f25 70 61 67 65 72 20 69 73 20 69 6e 20 70 65 72 69  pager is in peri
10f26 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f  stent-journal mo
10f27 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 79  de, then the phy
10f28 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72  sical .  ** jour
10f29 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78 74  nal-file may ext
10f2a 65 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e 64  end past the end
10f2b 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a   of the master-j
10f2c 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a  ournal name.  **
10f2d 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66 20   and 8 bytes of 
10f2e 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73 74 20  magic data just 
10f2f 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66  written to the f
10f30 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20  ile. This is .  
10f31 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65 63  ** dangerous bec
10f32 61 75 73 65 20 74 68 65 20 63 6f 64 65 20 74 6f  ause the code to
10f33 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d   rollback a hot-
10f34 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a  journal file.  *
10f35 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  * will not be ab
10f36 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d  le to find the m
10f37 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61  aster-journal na
10f38 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  me to determine 
10f39 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f 72  .  ** whether or
10f3a 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c   not the journal
10f3b 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20   is hot. .  **. 
10f3c 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69 6e   ** Easiest thin
10f3d 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73 20  g to do in this 
10f3e 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20 74  scenario is to t
10f3f 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72  runcate the jour
10f40 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74  nal .  ** file t
10f41 6f 20 74 68 65 20 72 65 71 75 69 72 65 64 20 73  o the required s
10f42 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28  ize..  */ .  if(
10f43 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
10f44 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
10f45 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ize(pPager->jfd,
10f46 20 26 6a 72 6e 6c 53 69 7a 65 29 29 0a 20 20 20   &jrnlSize)).   
10f47 26 26 20 6a 72 6e 6c 53 69 7a 65 3e 70 50 61 67  && jrnlSize>pPag
10f48 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20  er->journalOff. 
10f49 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
10f4a 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
10f4b 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67  Pager->jfd, pPag
10f4c 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b  er->journalOff);
10f4d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
10f4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  ;.}../*.** Find 
10f4f 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 68 61  a page in the ha
10f50 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 69  sh table given i
10f51 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20  ts page number. 
10f52 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e  Return.** a poin
10f53 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20  ter to the page 
10f54 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 72  or NULL if the r
10f55 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73  equested page is
10f56 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72 65 61 64 79   not .** already
10f57 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73   in memory..*/.s
10f58 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 61 67  tatic PgHdr *pag
10f59 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20  er_lookup(Pager 
10f5a 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67  *pPager, Pgno pg
10f5b 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b  no){.  PgHdr *p;
10f5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f5d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
10f5e 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f  rn value */..  /
10f5f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
10f60 69 62 6c 65 20 66 6f 72 20 61 20 63 61 6c 6c 20  ible for a call 
10f61 74 6f 20 50 63 61 63 68 65 46 65 74 63 68 28 29  to PcacheFetch()
10f62 20 77 69 74 68 20 63 72 65 61 74 65 46 6c 61 67   with createFlag
10f63 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20 66 61 69 6c  ==0 to.  ** fail
10f64 2c 20 73 69 6e 63 65 20 6e 6f 20 61 74 74 65 6d  , since no attem
10f65 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 64  pt to allocate d
10f66 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 77 69  ynamic memory wi
10f67 6c 6c 20 62 65 20 6d 61 64 65 2e 0a 20 20 2a 2f  ll be made..  */
10f68 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
10f69 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67  PcacheFetch(pPag
10f6a 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e  er->pPCache, pgn
10f6b 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 65 74  o, 0, &p);.  ret
10f6c 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
10f6d 55 6e 6c 65 73 73 20 74 68 65 20 70 61 67 65 72  Unless the pager
10f6e 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 61   is in error-sta
10f6f 74 65 2c 20 64 69 73 63 61 72 64 20 61 6c 6c 20  te, discard all 
10f70 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2e  in-memory pages.
10f71 20 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 72   If.** the pager
10f72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 61   is in error-sta
10f73 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61  te, then this ca
10f74 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ll is a no-op..*
10f75 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57 68 79 20 63  *.** TODO: Why c
10f76 61 6e 20 77 65 20 6e 6f 74 20 72 65 73 65 74 20  an we not reset 
10f77 74 68 65 20 70 61 67 65 72 20 77 68 69 6c 65 20  the pager while 
10f78 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 3f 0a  in error state?.
10f79 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10f7a 61 67 65 72 5f 72 65 73 65 74 28 50 61 67 65 72  ager_reset(Pager
10f7b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28   *pPager){.  if(
10f7c 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 50 61 67   SQLITE_OK==pPag
10f7d 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20  er->errCode ){. 
10f7e 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70     sqlite3Backup
10f7f 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e  Restart(pPager->
10f80 70 42 61 63 6b 75 70 29 3b 0a 20 20 20 20 73 71  pBackup);.    sq
10f81 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72  lite3PcacheClear
10f82 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
10f83 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
10f84 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 74 75  Free all structu
10f85 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 65 72  res in the Pager
10f86 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72  .aSavepoint[] ar
10f87 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f 74 68  ray and set both
10f88 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 65 70  .** Pager.aSavep
10f89 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72 2e 6e  oint and Pager.n
10f8a 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65 72  Savepoint to zer
10f8b 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 75 62  o. Close the sub
10f8c 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20 69  -journal.** if i
10f8d 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68  t is open and th
10f8e 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69  e pager is not i
10f8f 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
10f90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
10f91 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70   releaseAllSavep
10f92 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70 50 61  oints(Pager *pPa
10f93 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20  ger){.  int ii; 
10f94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10f95 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20 6c 6f   Iterator for lo
10f96 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 50 61  oping through Pa
10f97 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 2a  ger.aSavepoint *
10f98 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  /.  for(ii=0; ii
10f99 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f  <pPager->nSavepo
10f9a 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  int; ii++){.    
10f9b 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
10f9c 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 61  troy(pPager->aSa
10f9d 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53  vepoint[ii].pInS
10f9e 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 20  avepoint);.  }. 
10f9f 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78   if( !pPager->ex
10fa0 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20  clusiveMode ){. 
10fa1 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
10fa2 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b  e(pPager->sjfd);
10fa3 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
10fa4 72 65 65 28 70 50 61 67 65 72 2d 3e 61 53 61 76  ree(pPager->aSav
10fa5 65 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61 67 65  epoint);.  pPage
10fa6 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20  r->aSavepoint = 
10fa7 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 61  0;.  pPager->nSa
10fa8 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70  vepoint = 0;.  p
10fa9 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d  Pager->nSubRec =
10faa 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   0;.}../*.** Set
10fab 20 74 68 65 20 62 69 74 20 6e 75 6d 62 65 72 20   the bit number 
10fac 70 67 6e 6f 20 69 6e 20 74 68 65 20 50 61 67 65  pgno in the Page
10fad 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61  rSavepoint.pInSa
10fae 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 76  vepoint .** bitv
10faf 65 63 73 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20  ecs of all open 
10fb0 73 61 76 65 70 6f 69 6e 74 73 2e 20 52 65 74 75  savepoints. Retu
10fb1 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
10fb2 73 75 63 63 65 73 73 66 75 6c 0a 2a 2a 20 6f 72  successful.** or
10fb3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
10fb4 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
10fb5 65 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61  e occurs..*/.sta
10fb6 74 69 63 20 69 6e 74 20 61 64 64 54 6f 53 61 76  tic int addToSav
10fb7 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 50 61  epointBitvecs(Pa
10fb8 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e  ger *pPager, Pgn
10fb9 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 69  o pgno){.  int i
10fba 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
10fbb 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
10fbc 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63  nter */.  int rc
10fbd 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
10fbe 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
10fbf 64 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 3d  de */..  for(ii=
10fc0 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53  0; ii<pPager->nS
10fc1 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b  avepoint; ii++){
10fc2 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f  .    PagerSavepo
10fc3 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72  int *p = &pPager
10fc4 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d  ->aSavepoint[ii]
10fc5 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d  ;.    if( pgno<=
10fc6 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20  p->nOrig ){.    
10fc7 20 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42    rc |= sqlite3B
10fc8 69 74 76 65 63 53 65 74 28 70 2d 3e 70 49 6e 53  itvecSet(p->pInS
10fc9 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 3b  avepoint, pgno);
10fca 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
10fcb 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
10fcc 4d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  M );.      asser
10fcd 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
10fce 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   || rc==SQLITE_N
10fcf 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20  OMEM );.    }.  
10fd0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
10fd1 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74  ../*.** Unlock t
10fd2 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
10fd3 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
10fd4 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  is a no-op if th
10fd5 65 20 70 61 67 65 72 0a 2a 2a 20 69 73 20 69 6e  e pager.** is in
10fd6 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e   exclusive mode.
10fd7 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
10fd8 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ger is currently
10fd9 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c   in error state,
10fda 20 64 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e   discard the con
10fdb 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65  tents of .** the
10fdc 20 63 61 63 68 65 20 61 6e 64 20 72 65 73 65 74   cache and reset
10fdd 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63   the Pager struc
10fde 74 75 72 65 20 69 6e 74 65 72 6e 61 6c 20 73 74  ture internal st
10fdf 61 74 65 2e 20 49 66 20 74 68 65 72 65 20 69 73  ate. If there is
10fe0 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72  .** an open jour
10fe1 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74  nal-file, then t
10fe2 68 65 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73  he next time a s
10fe3 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62  hared-lock is ob
10fe4 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65  tained.** on the
10fe5 20 70 61 67 65 72 20 66 69 6c 65 20 28 62 79 20   pager file (by 
10fe6 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65  this or any othe
10fe7 72 20 70 72 6f 63 65 73 73 29 2c 20 69 74 20 77  r process), it w
10fe8 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61 74 65  ill be.** treate
10fe9 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  d as a hot-journ
10fea 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61  al and rolled ba
10feb 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ck..*/.static vo
10fec 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28  id pager_unlock(
10fed 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
10fee 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65    if( !pPager->e
10fef 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a  xclusiveMode ){.
10ff0 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20      int rc;     
10ff1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ff2 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
10ff3 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79  */..    /* Alway
10ff4 73 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72  s close the jour
10ff5 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64 72  nal file when dr
10ff6 6f 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 62  opping the datab
10ff7 61 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2a  ase lock..    **
10ff8 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 6f 74   Otherwise, anot
10ff9 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  her connection w
10ffa 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  ith journal_mode
10ffb 3d 64 65 6c 65 74 65 20 6d 69 67 68 74 0a 20 20  =delete might.  
10ffc 20 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20    ** delete the 
10ffd 66 69 6c 65 20 6f 75 74 20 66 72 6f 6d 20 75 6e  file out from un
10ffe 64 65 72 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20  der us..    */. 
10fff 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
11000 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  e(pPager->jfd);.
11001 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
11002 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d  cDestroy(pPager-
11003 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20  >pInJournal);.  
11004 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75    pPager->pInJou
11005 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65  rnal = 0;.    re
11006 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e  leaseAllSavepoin
11007 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 20  ts(pPager);..   
11008 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20   /* If the file 
11009 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f 6d  is unlocked, som
1100a 65 62 6f 64 79 20 65 6c 73 65 20 6d 69 67 68 74  ebody else might
1100b 20 63 68 61 6e 67 65 20 69 74 2e 20 54 68 65 0a   change it. The.
1100c 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 73 74      ** values st
1100d 6f 72 65 64 20 69 6e 20 50 61 67 65 72 2e 64 62  ored in Pager.db
1100e 53 69 7a 65 20 65 74 63 2e 20 6d 69 67 68 74 20  Size etc. might 
1100f 62 65 63 6f 6d 65 20 69 6e 76 61 6c 69 64 20 69  become invalid i
11010 66 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 68 61  f.    ** this ha
11011 70 70 65 6e 73 2e 20 54 4f 44 4f 3a 20 52 65 61  ppens. TODO: Rea
11012 6c 6c 79 2c 20 74 68 69 73 20 64 6f 65 73 6e 27  lly, this doesn'
11013 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 65  t need to be cle
11014 61 72 65 64 0a 20 20 20 20 2a 2a 20 75 6e 74 69  ared.    ** unti
11015 6c 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75  l the change-cou
11016 6e 74 65 72 20 63 68 65 63 6b 20 66 61 69 6c 73  nter check fails
11017 20 69 6e 20 70 61 67 65 72 53 68 61 72 65 64 4c   in pagerSharedL
11018 6f 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ock()..    */.  
11019 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
1101a 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20  Valid = 0;..    
1101b 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50  rc = osUnlock(pP
1101c 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43  ager->fd, NO_LOC
1101d 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  K);.    if( rc )
1101e 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
1101f 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20  errCode = rc;.  
11020 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 28    }.    IOTRACE(
11021 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20  ("UNLOCK %p\n", 
11022 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a  pPager))..    /*
11023 20 49 66 20 50 61 67 65 72 2e 65 72 72 43 6f 64   If Pager.errCod
11024 65 20 69 73 20 73 65 74 2c 20 74 68 65 20 63 6f  e is set, the co
11025 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
11026 67 65 72 20 63 61 63 68 65 20 63 61 6e 6e 6f 74  ger cache cannot
11027 20 62 65 0a 20 20 20 20 2a 2a 20 74 72 75 73 74   be.    ** trust
11028 65 64 2e 20 4e 6f 77 20 74 68 61 74 20 74 68 65  ed. Now that the
11029 20 70 61 67 65 72 20 66 69 6c 65 20 69 73 20 75   pager file is u
1102a 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 20 63 6f 6e  nlocked, the con
1102b 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 20  tents of the.   
1102c 20 2a 2a 20 63 61 63 68 65 20 63 61 6e 20 62 65   ** cache can be
1102d 20 64 69 73 63 61 72 64 65 64 20 61 6e 64 20 74   discarded and t
1102e 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 61  he error code sa
1102f 66 65 6c 79 20 63 6c 65 61 72 65 64 2e 0a 20 20  fely cleared..  
11030 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61    */.    if( pPa
11031 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a  ger->errCode ){.
11032 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
11033 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
11034 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f     pPager->errCo
11035 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  de = SQLITE_OK;.
11036 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 61        }.      pa
11037 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
11038 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50  );.    }..    pP
11039 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e  ager->changeCoun
1103a 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70  tDone = 0;.    p
1103b 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
1103c 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d  AGER_UNLOCK;.  }
1103d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
1103e 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62  unction should b
1103f 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e  e called when an
11040 20 49 4f 45 52 52 2c 20 43 4f 52 52 55 50 54 20   IOERR, CORRUPT 
11041 6f 72 20 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a  or FULL error.**
11042 20 6d 61 79 20 68 61 76 65 20 6f 63 63 75 72 72   may have occurr
11043 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72  ed. The first ar
11044 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e  gument is a poin
11045 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 72  ter to the pager
11046 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20   .** structure, 
11047 74 68 65 20 73 65 63 6f 6e 64 20 74 68 65 20 65  the second the e
11048 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f 75 74 20  rror-code about 
11049 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  to be returned b
1104a 79 20 61 20 70 61 67 65 72 20 0a 2a 2a 20 41 50  y a pager .** AP
1104b 49 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  I function. The 
1104c 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
1104d 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
1104e 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
1104f 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66 75 6e 63  .** to this func
11050 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  tion. .**.** If 
11051 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
11052 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 49 4f  ent is SQLITE_IO
11053 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52  ERR, SQLITE_CORR
11054 55 50 54 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46  UPT, or SQLITE_F
11055 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  ULL.** the error
11056 20 62 65 63 6f 6d 65 73 20 70 65 72 73 69 73 74   becomes persist
11057 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68 65 20 70  ent. Until the p
11058 65 72 73 69 73 74 65 6e 20 65 72 72 6f 72 20 69  ersisten error i
11059 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75  s cleared,.** su
1105a 62 73 65 71 75 65 6e 74 20 41 50 49 20 63 61 6c  bsequent API cal
1105b 6c 73 20 6f 6e 20 74 68 69 73 20 50 61 67 65 72  ls on this Pager
1105c 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c   will immediatel
1105d 79 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 6d  y return the sam
1105e 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  e .** error code
1105f 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73  ..**.** A persis
11060 74 65 6e 74 20 65 72 72 6f 72 20 69 6e 64 69 63  tent error indic
11061 61 74 65 73 20 74 68 61 74 20 74 68 65 20 63 6f  ates that the co
11062 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
11063 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61  ger-cache .** ca
11064 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e  nnot be trusted.
11065 20 54 68 69 73 20 73 74 61 74 65 20 63 61 6e 20   This state can 
11066 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 63 6f  be cleared by co
11067 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 61 72 64  mpletely discard
11068 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  ing .** the cont
11069 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
1106a 72 2d 63 61 63 68 65 2e 20 49 66 20 61 20 74 72  r-cache. If a tr
1106b 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 61 63  ansaction was ac
1106c 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65  tive when.** the
1106d 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f   persistent erro
1106e 72 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e  r occurred, then
1106f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
11070 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a  urnal may need.*
11071 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 64  * to be replayed
11072 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20   to restore the 
11073 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
11074 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61  database file (a
11075 73 20 69 66 0a 2a 2a 20 69 74 20 77 65 72 65 20  s if.** it were 
11076 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a  a hot-journal)..
11077 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
11078 67 65 72 5f 65 72 72 6f 72 28 50 61 67 65 72 20  ger_error(Pager 
11079 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 72 63 29  *pPager, int rc)
1107a 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 72 63  {.  int rc2 = rc
1107b 20 26 20 30 78 66 66 3b 0a 20 20 61 73 73 65 72   & 0xff;.  asser
1107c 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 65 72  t(.       pPager
1107d 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54  ->errCode==SQLIT
1107e 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20  E_FULL ||.      
1107f 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11080 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20  ==SQLITE_OK ||. 
11081 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 65        (pPager->e
11082 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 3d 3d  rrCode & 0xff)==
11083 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 29  SQLITE_IOERR.  )
11084 3b 0a 20 20 69 66 28 0a 20 20 20 20 72 63 32 3d  ;.  if(.    rc2=
11085 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a  =SQLITE_FULL ||.
11086 20 20 20 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f      rc2==SQLITE_
11087 49 4f 45 52 52 20 7c 7c 0a 20 20 20 20 72 63 32  IOERR ||.    rc2
11088 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  ==SQLITE_CORRUPT
11089 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  .  ){.    pPager
1108a 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a  ->errCode = rc;.
1108b 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
1108c 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c  state==PAGER_UNL
1108d 4f 43 4b 20 0a 20 20 20 20 20 26 26 20 73 71 6c  OCK .     && sql
1108e 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75  ite3PcacheRefCou
1108f 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
11090 68 65 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20  he)==0 .    ){. 
11091 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70       /* If the p
11092 61 67 65 72 20 69 73 20 61 6c 72 65 61 64 79 20  ager is already 
11093 75 6e 6c 6f 63 6b 65 64 2c 20 63 61 6c 6c 20 70  unlocked, call p
11094 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 6e 6f  ager_unlock() no
11095 77 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6c  w to.      ** cl
11096 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73 74  ear the error st
11097 61 74 65 20 61 6e 64 20 65 6e 73 75 72 65 20 74  ate and ensure t
11098 68 61 74 20 74 68 65 20 70 61 67 65 72 2d 63 61  hat the pager-ca
11099 63 68 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a  che is .      **
1109a 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74   completely empt
1109b 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  y..      */.    
1109c 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70    pager_unlock(p
1109d 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Pager);.    }.  
1109e 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1109f 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  ../*.** Execute 
110a0 61 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20  a rollback if a 
110a1 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
110a2 63 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b  ctive and unlock
110a3 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73   the .** databas
110a4 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49  e file. .**.** I
110a5 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20  f the pager has 
110a6 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20  already entered 
110a7 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c  the error state,
110a8 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20   do not attempt 
110a9 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  .** the rollback
110aa 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 49   at this time. I
110ab 6e 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 6e  nstead, pager_un
110ac 6c 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64  lock() is called
110ad 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f  . The.** call to
110ae 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20   pager_unlock() 
110af 77 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c 6c  will discard all
110b0 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
110b1 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20  , unlock.** the 
110b2 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
110b3 64 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f  d clear the erro
110b4 72 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 73  r state. If this
110b5 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74   means that.** t
110b6 68 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f  here is a hot-jo
110b7 75 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74 68  urnal left in th
110b8 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74  e file-system, t
110b9 68 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69  he next connecti
110ba 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20  on.** to obtain 
110bb 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e  a shared lock on
110bc 20 74 68 65 20 70 61 67 65 72 20 28 77 68 69 63   the pager (whic
110bd 68 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f 6e  h may be this on
110be 65 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20  e) will.** roll 
110bf 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49  it back..**.** I
110c0 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20  f the pager has 
110c1 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 65  not already ente
110c2 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74  red the error st
110c3 61 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f  ate, but an IO o
110c4 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f  r.** malloc erro
110c5 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
110c6 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e  a rollback, then
110c7 20 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 6c   this will itsel
110c8 66 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20  f cause .** the 
110c9 70 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74  pager to enter t
110ca 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20  he error state. 
110cb 57 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 6c  Which will be cl
110cc 65 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  eared by the.** 
110cd 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e  call to pager_un
110ce 6c 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 72  lock(), as descr
110cf 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73  ibed above..*/.s
110d0 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72  tatic void pager
110d1 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63  UnlockAndRollbac
110d2 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  k(Pager *pPager)
110d3 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  {.  if( pPager->
110d4 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f  errCode==SQLITE_
110d5 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74  OK && pPager->st
110d6 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52  ate>=PAGER_RESER
110d7 56 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  VED ){.    sqlit
110d8 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
110d9 6c 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74  loc();.    sqlit
110da 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
110db 70 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71 6c  pPager);.    sql
110dc 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
110dd 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67  loc();.  }.  pag
110de 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72  er_unlock(pPager
110df 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
110e0 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20   routine ends a 
110e1 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74  transaction. A t
110e2 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73  ransaction is us
110e3 75 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a  ually ended by .
110e4 2a 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d  ** either a COMM
110e5 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b  IT or a ROLLBACK
110e6 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73   operation. This
110e7 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20   routine may be 
110e8 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72  called .** after
110e9 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68   rollback of a h
110ea 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69  ot-journal, or i
110eb 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
110ec 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a  s while opening.
110ed 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ** the journal f
110ee 69 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74  ile or writing t
110ef 68 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f  he very first jo
110f0 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20  urnal-header of 
110f1 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72  a.** database tr
110f2 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a  ansaction..** .*
110f3 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
110f4 73 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45  s in PAGER_SHARE
110f5 44 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43  D or PAGER_UNLOC
110f6 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  K state when thi
110f7 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
110f8 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20  called, it is a 
110f9 6e 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20 53  no-op (returns S
110fa 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a  QLITE_OK)..**.**
110fb 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20   Otherwise, any 
110fc 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74  active savepoint
110fd 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e 0a  s are released..
110fe 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75  **.** If the jou
110ff 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65  rnal file is ope
11100 6e 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22 66  n, then it is "f
11101 69 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20  inalized". Once 
11102 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69  a journal .** fi
11103 6c 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61  le has been fina
11104 6c 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74 20  lized it is not 
11105 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20  possible to use 
11106 69 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20  it to roll back 
11107 61 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  a .** transactio
11108 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62  n. Nor will it b
11109 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  e considered to 
1110a 62 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  be a hot-journal
1110b 20 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61   by this.** or a
1110c 6e 79 20 6f 74 68 65 72 20 64 61 74 61 62 61 73  ny other databas
1110d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78  e connection. Ex
1110e 61 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72  actly how a jour
1110f 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  nal is finalized
11110 0a 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 77  .** depends on w
11111 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
11112 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69  e pager is runni
11113 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  ng in exclusive 
11114 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20  mode and.** the 
11115 63 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d  current journal-
11116 6d 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 72  mode (Pager.jour
11117 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20  nalMode value), 
11118 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
11119 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  *   journalMode=
1111a 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a  =MEMORY.**     J
1111b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63  ournal file desc
1111c 72 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79  riptor is simply
1111d 20 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64 65   closed. This de
1111e 73 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20  stroys an .**   
1111f 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72    in-memory jour
11120 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75  nal..**.**   jou
11121 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41  rnalMode==TRUNCA
11122 54 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61  TE.**     Journa
11123 6c 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61  l file is trunca
11124 74 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74 65  ted to zero byte
11125 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a  s in size..**.**
11126 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d     journalMode==
11127 50 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20 54  PERSIST.**     T
11128 68 65 20 66 69 72 73 74 20 32 38 20 62 79 74 65  he first 28 byte
11129 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  s of the journal
1112a 20 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65 64   file are zeroed
1112b 2e 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61 74  . This invalidat
1112c 65 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66 69  es.**     the fi
1112d 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  rst journal head
1112e 65 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c 20  er in the file, 
1112f 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65 6e  and hence the en
11130 74 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  tire journal.** 
11131 20 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76      file. An inv
11132 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  alid journal fil
11133 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c  e cannot be roll
11134 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20  ed back..**.**  
11135 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45   journalMode==DE
11136 4c 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65 20  LETE.**     The 
11137 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
11138 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74  closed and delet
11139 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
1113a 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a  OsDelete()..**.*
1113b 2a 20 20 20 20 20 49 66 20 74 68 65 20 70 61 67  *     If the pag
1113c 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e  er is running in
1113d 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c   exclusive mode,
1113e 20 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66 20   this method of 
1113f 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20  finalizing.**   
11140 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69    the journal fi
11141 6c 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64  le is never used
11142 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74 68  . Instead, if th
11143 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73  e journalMode is
11144 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 61  .**     DELETE a
11145 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20  nd the pager is 
11146 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  in exclusive mod
11147 65 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64 65  e, the method de
11148 73 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a  scribed under.**
11149 20 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65       journalMode
1114a 3d 3d 50 45 52 53 49 53 54 20 69 73 20 75 73 65  ==PERSIST is use
1114b 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a  d instead..**.**
1114c 20 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 6e   After the journ
1114d 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c  al is finalized,
1114e 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e   if running in n
1114f 6f 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  on-exclusive mod
11150 65 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20  e, the.** pager 
11151 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 53  moves to PAGER_S
11152 48 41 52 45 44 20 73 74 61 74 65 20 28 61 6e 64  HARED state (and
11153 20 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65 20   downgrades the 
11154 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64  lock on the.** d
11155 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 63 63  atabase file acc
11156 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a  ordingly)..**.**
11157 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73   If the pager is
11158 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c   running in excl
11159 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69  usive mode and i
1115a 73 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43 45  s in PAGER_SYNCE
1115b 44 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d  D state,.** it m
1115c 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45 58  oves to PAGER_EX
1115d 43 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b  CLUSIVE. No lock
1115e 73 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65 64  s are downgraded
1115f 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e   when running in
11160 0a 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d 6f  .** exclusive mo
11161 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  de..**.** SQLITE
11162 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
11163 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75  if no error occu
11164 72 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  rs. If an error 
11165 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a  occurs during.**
11166 20 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20 6f   any of the IO o
11167 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e  perations to fin
11168 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61  alize the journa
11169 6c 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b  l file or unlock
1116a 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
1116b 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72   then the IO err
1116c 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
1116d 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e  ned to the user.
1116e 20 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72   If the .** oper
1116f 61 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a  ation to finaliz
11170 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
11171 6c 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74  le fails, then t
11172 68 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a  he code still.**
11173 20 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b   tries to unlock
11174 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
11175 6c 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78 63  le if not in exc
11176 6c 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66 20  lusive mode. If 
11177 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70  the.** unlock op
11178 65 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61 73  eration fails as
11179 20 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20   well, then the 
1117a 66 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64 65  first error code
1117b 20 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74   related.** to t
1117c 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 65  he first error e
1117d 6e 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65 20  ncountered (the 
1117e 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61  journal finaliza
1117f 74 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20  tion one) is.** 
11180 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
11181 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e  tic int pager_en
11182 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61  d_transaction(Pa
11183 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
11184 20 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20 69   hasMaster){.  i
11185 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
11186 4b 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72  K;      /* Error
11187 20 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e   code from journ
11188 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20  al finalization 
11189 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69  operation */.  i
1118a 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f  nt rc2 = SQLITE_
1118b 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72  OK;     /* Error
1118c 20 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 69   code from db fi
1118d 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74  le unlock operat
1118e 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50  ion */..  if( pP
1118f 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45  ager->state<PAGE
11190 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20  R_RESERVED ){.  
11191 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11192 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  OK;.  }.  releas
11193 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70  eAllSavepoints(p
11194 50 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65 72  Pager);..  asser
11195 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
11196 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72  ->jfd) || pPager
11197 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
11198 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28  );.  if( isOpen(
11199 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a  pPager->jfd) ){.
1119a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 54 68  .    /* TODO: Th
1119b 65 72 65 27 73 20 61 20 70 72 6f 62 6c 65 6d 20  ere's a problem 
1119c 68 65 72 65 20 69 66 20 61 20 6a 6f 75 72 6e 61  here if a journa
1119d 6c 2d 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65  l-file was opene
1119e 64 20 69 6e 20 4d 45 4d 4f 52 59 0a 20 20 20 20  d in MEMORY.    
1119f 2a 2a 20 6d 6f 64 65 20 61 6e 64 20 74 68 65 6e  ** mode and then
111a0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64   the journal-mod
111a1 65 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20  e is changed to 
111a2 54 52 55 4e 43 41 54 45 20 6f 72 20 50 45 52 53  TRUNCATE or PERS
111a3 49 53 54 0a 20 20 20 20 2a 2a 20 64 75 72 69 6e  IST.    ** durin
111a4 67 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  g the transactio
111a5 6e 2e 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f  n. This code sho
111a6 75 6c 64 20 62 65 20 63 68 61 6e 67 65 64 20 74  uld be changed t
111a7 6f 20 61 73 73 75 6d 65 0a 20 20 20 20 2a 2a 20  o assume.    ** 
111a8 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c  that the journal
111a9 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 63 68   mode has not ch
111aa 61 6e 67 65 64 20 73 69 6e 63 65 20 74 68 65 20  anged since the 
111ab 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 0a  transaction was.
111ac 20 20 20 20 2a 2a 20 73 74 61 72 74 65 64 2e 20      ** started. 
111ad 41 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 50  And the sqlite3P
111ae 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  agerJournalMode(
111af 29 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  ) function shoul
111b0 64 20 62 65 0a 20 20 20 20 2a 2a 20 63 68 61 6e  d be.    ** chan
111b1 67 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ged to make sure
111b2 20 74 68 61 74 20 74 68 69 73 20 69 73 20 74 68   that this is th
111b3 65 20 63 61 73 65 20 74 6f 6f 2e 0a 20 20 20 20  e case too..    
111b4 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c  */..    /* Final
111b5 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ize the journal 
111b6 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28  file. */.    if(
111b7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
111b8 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
111b9 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29  NALMODE_MEMORY )
111ba 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 4d 65  {.      int isMe
111bb 6d 6f 72 79 4a 6f 75 72 6e 61 6c 20 3d 20 73 71  moryJournal = sq
111bc 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61  lite3IsMemJourna
111bd 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  l(pPager->jfd);.
111be 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
111bf 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  lose(pPager->jfd
111c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 69 73  );.      if( !is
111c1 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 6c 20 29 7b  MemoryJournal ){
111c2 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
111c3 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 50  lite3OsDelete(pP
111c4 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 67  ager->pVfs, pPag
111c5 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29  er->zJournal, 0)
111c6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
111c7 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e  lse if( pPager->
111c8 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
111c9 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54  ER_JOURNALMODE_T
111ca 52 55 4e 43 41 54 45 20 29 7b 0a 20 20 20 20 20  RUNCATE ){.     
111cb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
111cc 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
111cd 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 70  jfd, 0);.      p
111ce 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
111cf 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61  f = 0;.      pPa
111d0 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72  ger->journalStar
111d1 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ted = 0;.    }el
111d2 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  se if( pPager->e
111d3 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20  xclusiveMode .  
111d4 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f     || pPager->jo
111d5 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
111d6 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52  _JOURNALMODE_PER
111d7 53 49 53 54 0a 20 20 20 20 29 7b 0a 20 20 20 20  SIST.    ){.    
111d8 20 20 72 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e    rc = zeroJourn
111d9 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 68 61  alHdr(pPager, ha
111da 73 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  sMaster);.      
111db 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
111dc 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 70  er, rc);.      p
111dd 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
111de 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61  f = 0;.      pPa
111df 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72  ger->journalStar
111e0 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ted = 0;.    }el
111e1 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
111e2 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
111e3 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
111e4 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20  RNALMODE_DELETE 
111e5 7c 7c 20 72 63 20 29 3b 0a 20 20 20 20 20 20 73  || rc );.      s
111e6 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
111e7 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20  ager->jfd);.    
111e8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
111e9 5f 4f 4b 20 26 26 20 21 70 50 61 67 65 72 2d 3e  _OK && !pPager->
111ea 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  tempFile ){.    
111eb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
111ec 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d  OsDelete(pPager-
111ed 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  >pVfs, pPager->z
111ee 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20  Journal, 0);.   
111ef 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 64     }.    }..#ifd
111f0 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  ef SQLITE_CHECK_
111f1 50 41 47 45 53 0a 20 20 20 20 73 71 6c 69 74 65  PAGES.    sqlite
111f2 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44 69  3PcacheIterateDi
111f3 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61  rty(pPager->pPCa
111f4 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74 5f 70  che, pager_set_p
111f5 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64 69 66  agehash);.#endif
111f6 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ..    sqlite3Pca
111f7 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67  cheCleanAll(pPag
111f8 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
111f9 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
111fa 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70  estroy(pPager->p
111fb 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20  InJournal);.    
111fc 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
111fd 61 6c 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  al = 0;.    pPag
111fe 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20  er->nRec = 0;.  
111ff 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 72  }..  if( !pPager
11200 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
11201 29 7b 0a 20 20 20 20 72 63 32 20 3d 20 6f 73 55  ){.    rc2 = osU
11202 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64  nlock(pPager->fd
11203 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a  , SHARED_LOCK);.
11204 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
11205 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44  e = PAGER_SHARED
11206 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68  ;.    pPager->ch
11207 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20  angeCountDone = 
11208 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  0;.  }else if( p
11209 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
1120a 47 45 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20 20  GER_SYNCED ){.  
1120b 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
1120c 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  = PAGER_EXCLUSIV
1120d 45 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d  E;.  }.  pPager-
1120e 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a  >setMaster = 0;.
1120f 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79    pPager->needSy
11210 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  nc = 0;.  pPager
11211 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30  ->dbModified = 0
11212 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73  ;..  /* TODO: Is
11213 20 74 68 69 73 20 6f 70 74 69 6d 61 6c 3f 20 57   this optimal? W
11214 68 79 20 69 73 20 74 68 65 20 64 62 20 73 69 7a  hy is the db siz
11215 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 68 65  e invalidated he
11216 72 65 20 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68  re .  ** when th
11217 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
11218 69 73 20 6e 6f 74 20 75 6e 6c 6f 63 6b 65 64 3f  is not unlocked?
11219 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62   */.  pPager->db
1121a 4f 72 69 67 53 69 7a 65 20 3d 20 30 3b 0a 20 20  OrigSize = 0;.  
1121b 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75  sqlite3PcacheTru
1121c 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50  ncate(pPager->pP
1121d 43 61 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64  Cache, pPager->d
1121e 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 4d  bSize);.  if( !M
1121f 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 61 67  EMDB ){.    pPag
11220 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
11221 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  = 0;.  }..  retu
11222 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rn (rc==SQLITE_O
11223 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a 2f 2a  K?rc2:rc);.}../*
11224 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 61 44  .** Parameter aD
11225 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74  ata must point t
11226 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 70 50  o a buffer of pP
11227 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62  ager->pageSize b
11228 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 2e  ytes.** of data.
11229 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 72 65 74   Compute and ret
1122a 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d 20 62  urn a checksum b
1122b 61 73 65 64 20 6f 6e 74 20 74 68 65 20 63 6f 6e  ased ont the con
1122c 74 65 6e 74 73 20 6f 66 20 74 68 65 20 0a 2a 2a  tents of the .**
1122d 20 70 61 67 65 20 6f 66 20 64 61 74 61 20 61 6e   page of data an
1122e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  d the current va
1122f 6c 75 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 63  lue of pPager->c
11230 6b 73 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20  ksumInit..**.** 
11231 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 72 65  This is not a re
11232 61 6c 20 63 68 65 63 6b 73 75 6d 2e 20 49 74 20  al checksum. It 
11233 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74  is really just t
11234 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 0a 2a  he sum of the .*
11235 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c  * random initial
11236 20 76 61 6c 75 65 20 28 70 50 61 67 65 72 2d 3e   value (pPager->
11237 63 6b 73 75 6d 49 6e 69 74 29 20 61 6e 64 20 65  cksumInit) and e
11238 76 65 72 79 20 32 30 30 74 68 20 62 79 74 65 0a  very 200th byte.
11239 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 64  ** of the page d
1123a 61 74 61 2c 20 73 74 61 72 74 69 6e 67 20 77 69  ata, starting wi
1123b 74 68 20 62 79 74 65 20 6f 66 66 73 65 74 20 28  th byte offset (
1123c 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
1123d 25 32 30 30 29 2e 0a 2a 2a 20 45 61 63 68 20 62  %200)..** Each b
1123e 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 74  yte is interpret
1123f 65 64 20 61 73 20 61 6e 20 38 2d 62 69 74 20 75  ed as an 8-bit u
11240 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e  nsigned integer.
11241 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20  .**.** Changing 
11242 74 68 65 20 66 6f 72 6d 75 6c 61 20 75 73 65 64  the formula used
11243 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 69 73   to compute this
11244 20 63 68 65 63 6b 73 75 6d 20 72 65 73 75 6c 74   checksum result
11245 73 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6d  s in an.** incom
11246 70 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c 20  patible journal 
11247 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a  file format..**.
11248 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63 6f  ** If journal co
11249 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20  rruption occurs 
1124a 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66  due to a power f
1124b 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74  ailure, the most
1124c 20 6c 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 65 6e   likely .** scen
1124d 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f 6e 65  ario is that one
1124e 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65   end or the othe
1124f 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  r of the record 
11250 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e  will be changed.
11251 20 0a 2a 2a 20 49 74 20 69 73 20 6d 75 63 68 20   .** It is much 
11252 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74  less likely that
11253 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66   the two ends of
11254 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63   the journal rec
11255 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63  ord will be.** c
11256 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d  orrect and the m
11257 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74  iddle be corrupt
11258 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22 63  .  Thus, this "c
11259 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c  hecksum" scheme,
1125a 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20  .** though fast 
1125b 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63  and simple, catc
1125c 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c  hes the mostly l
1125d 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f  ikely kind of co
1125e 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  rruption..*/.sta
1125f 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 63 6b  tic u32 pager_ck
11260 73 75 6d 28 50 61 67 65 72 20 2a 70 50 61 67 65  sum(Pager *pPage
11261 72 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61  r, const u8 *aDa
11262 74 61 29 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d  ta){.  u32 cksum
11263 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d   = pPager->cksum
11264 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Init;         /*
11265 20 43 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20   Checksum value 
11266 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69  to return */.  i
11267 6e 74 20 69 20 3d 20 70 50 61 67 65 72 2d 3e 70  nt i = pPager->p
11268 61 67 65 53 69 7a 65 2d 32 30 30 3b 20 20 20 20  ageSize-200;    
11269 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1126a 75 6e 74 65 72 20 2a 2f 0a 20 20 77 68 69 6c 65  unter */.  while
1126b 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b 73  ( i>0 ){.    cks
1126c 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b 0a  um += aData[i];.
1126d 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20 20      i -= 200;.  
1126e 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 75 6d  }.  return cksum
1126f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
11270 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 66 72  a single page fr
11271 6f 6d 20 65 69 74 68 65 72 20 74 68 65 20 6a 6f  om either the jo
11272 75 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 20 69  urnal file (if i
11273 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 31 29 20 6f 72  sMainJrnl==1) or
11274 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 75 62  .** from the sub
11275 2d 6a 6f 75 72 6e 61 6c 20 28 69 66 20 69 73 4d  -journal (if isM
11276 61 69 6e 4a 72 6e 6c 3d 3d 30 29 20 61 6e 64 20  ainJrnl==0) and 
11277 70 6c 61 79 62 61 63 6b 20 74 68 61 74 20 70 61  playback that pa
11278 67 65 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  ge..** The page 
11279 62 65 67 69 6e 73 20 61 74 20 6f 66 66 73 65 74  begins at offset
1127a 20 2a 70 4f 66 66 73 65 74 20 69 6e 74 6f 20 74   *pOffset into t
1127b 68 65 20 66 69 6c 65 2e 20 54 68 65 20 2a 70 4f  he file. The *pO
1127c 66 66 73 65 74 0a 2a 2a 20 76 61 6c 75 65 20 69  ffset.** value i
1127d 73 20 69 6e 63 72 65 61 73 65 64 20 74 6f 20 74  s increased to t
1127e 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
1127f 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65  next page in the
11280 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20   journal..**.** 
11281 54 68 65 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 66  The isMainJrnl f
11282 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74  lag is true if t
11283 68 69 73 20 69 73 20 74 68 65 20 6d 61 69 6e 20  his is the main 
11284 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
11285 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 66 6f   and.** false fo
11286 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  r the statement 
11287 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61  journal.  The ma
11288 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  in rollback jour
11289 6e 61 6c 20 75 73 65 73 0a 2a 2a 20 63 68 65 63  nal uses.** chec
1128a 6b 73 75 6d 73 20 2d 20 74 68 65 20 73 74 61 74  ksums - the stat
1128b 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 6f  ement journal do
1128c 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  es not..**.** If
1128d 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
1128e 20 6f 66 20 74 68 65 20 70 61 67 65 20 72 65 63   of the page rec
1128f 6f 72 64 20 72 65 61 64 20 66 72 6f 6d 20 74 68  ord read from th
11290 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20  e (sub-)journal 
11291 66 69 6c 65 0a 2a 2a 20 69 73 20 67 72 65 61 74  file.** is great
11292 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72  er than the curr
11293 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61 67  ent value of Pag
11294 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 65 6e 20  er.dbSize, then 
11295 70 6c 61 79 62 61 63 6b 20 69 73 0a 2a 2a 20 73  playback is.** s
11296 6b 69 70 70 65 64 20 61 6e 64 20 53 51 4c 49 54  kipped and SQLIT
11297 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
11298 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f 6e 65  ..**.** If pDone
11299 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
1129a 65 6e 20 69 74 20 69 73 20 61 20 72 65 63 6f 72  en it is a recor
1129b 64 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 20  d of pages that 
1129c 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20  have already.** 
1129d 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b  been played back
1129e 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 61  .  If the page a
1129f 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73 20 61  t *pOffset has a
112a0 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c 61 79  lready been play
112a1 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66 20 74  ed back.** (if t
112a2 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
112a3 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 73 65   pDone bit is se
112a4 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65  t) then skip the
112a5 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 4d 61   playback..** Ma
112a6 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 6f 6e  ke sure the pDon
112a7 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64  e bit correspond
112a8 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f 66 66  ing to the *pOff
112a9 73 65 74 20 70 61 67 65 20 69 73 20 73 65 74 0a  set page is set.
112aa 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 75  ** prior to retu
112ab 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rning..**.** If 
112ac 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 64 20  the page record 
112ad 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  is successfully 
112ae 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73  read from the (s
112af 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ub-)journal file
112b0 0a 2a 2a 20 61 6e 64 20 70 6c 61 79 65 64 20 62  .** and played b
112b1 61 63 6b 2c 20 74 68 65 6e 20 53 51 4c 49 54 45  ack, then SQLITE
112b2 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
112b3 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
112b4 6f 63 63 75 72 73 0a 2a 2a 20 77 68 69 6c 65 20  occurs.** while 
112b5 72 65 61 64 69 6e 67 20 74 68 65 20 72 65 63 6f  reading the reco
112b6 72 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62  rd from the (sub
112b7 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  -)journal file o
112b8 72 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67 0a  r while writing.
112b9 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ** to the databa
112ba 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 68  se file, then th
112bb 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  e IO error code 
112bc 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
112bd 64 61 74 61 0a 2a 2a 20 69 73 20 73 75 63 63 65  data.** is succe
112be 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f  ssfully read fro
112bf 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72  m the (sub-)jour
112c0 6e 61 6c 20 66 69 6c 65 20 62 75 74 20 61 70 70  nal file but app
112c1 65 61 72 73 20 74 6f 20 62 65 0a 2a 2a 20 63 6f  ears to be.** co
112c2 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f  rrupted, SQLITE_
112c3 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64  DONE is returned
112c4 2e 20 44 61 74 61 20 69 73 20 63 6f 6e 73 69 64  . Data is consid
112c5 65 72 65 64 20 63 6f 72 72 75 70 74 65 64 20 69  ered corrupted i
112c6 6e 0a 2a 2a 20 74 77 6f 20 63 69 72 63 75 6d 73  n.** two circums
112c7 74 61 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a 20 20  tances:.** .**  
112c8 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64   * If the record
112c9 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20   page-number is 
112ca 69 6c 6c 65 67 61 6c 20 28 30 20 6f 72 20 50 41  illegal (0 or PA
112cb 47 45 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 6f 72  GER_MJ_PGNO), or
112cc 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 72  .**   * If the r
112cd 65 63 6f 72 64 20 69 73 20 62 65 69 6e 67 20 72  ecord is being r
112ce 6f 6c 6c 65 64 20 62 61 63 6b 20 66 72 6f 6d 20  olled back from 
112cf 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
112d0 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61 6e 64   file.**     and
112d1 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 66 69   the checksum fi
112d2 65 6c 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  eld does not mat
112d3 63 68 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f  ch the record co
112d4 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65 69  ntent..**.** Nei
112d5 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77  ther of these tw
112d6 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72 65 20  o scenarios are 
112d7 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e 67 20  possible during 
112d8 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c  a savepoint roll
112d9 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  back..**.** If t
112da 68 69 73 20 69 73 20 61 20 73 61 76 65 70 6f 69  his is a savepoi
112db 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65  nt rollback, the
112dc 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68 61 76  n memory may hav
112dd 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 61  e to be dynamica
112de 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  lly.** allocated
112df 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
112e0 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68  n. If this is th
112e1 65 20 63 61 73 65 20 61 6e 64 20 61 6e 20 61 6c  e case and an al
112e2 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a  location fails,.
112e3 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  ** SQLITE_NOMEM 
112e4 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
112e5 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
112e6 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61  _playback_one_pa
112e7 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  ge(.  Pager *pPa
112e8 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ger,            
112e9 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72      /* The pager
112ea 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20 62 61   being played ba
112eb 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 61  ck */.  int isMa
112ec 69 6e 4a 72 6e 6c 2c 20 20 20 20 20 20 20 20 20  inJrnl,         
112ed 20 20 20 20 20 20 2f 2a 20 31 20 2d 3e 20 6d 61        /* 1 -> ma
112ee 69 6e 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 2d 3e  in journal. 0 ->
112ef 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f   sub-journal. */
112f0 0a 20 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 2c  .  int isUnsync,
112f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112f2 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 61 64   /* True if read
112f3 69 6e 67 20 66 72 6f 6d 20 75 6e 73 79 6e 63 65  ing from unsynce
112f4 64 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a  d main journal *
112f5 2f 0a 20 20 69 36 34 20 2a 70 4f 66 66 73 65 74  /.  i64 *pOffset
112f6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
112f7 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 72    /* Offset of r
112f8 65 63 6f 72 64 20 74 6f 20 70 6c 61 79 62 61 63  ecord to playbac
112f9 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 61 76  k */.  int isSav
112fa 65 70 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20  epnt,           
112fb 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
112fc 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c   a savepoint rol
112fd 6c 62 61 63 6b 20 2a 2f 0a 20 20 42 69 74 76 65  lback */.  Bitve
112fe 63 20 2a 70 44 6f 6e 65 20 20 20 20 20 20 20 20  c *pDone        
112ff 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76           /* Bitv
11300 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65  ec of pages alre
11301 61 64 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20  ady played back 
11302 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
11303 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20 20 20    PgHdr *pPg;   
11304 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11305 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67 20 70  /* An existing p
11306 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65  age in the cache
11307 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b   */.  Pgno pgno;
11308 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11309 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20      /* The page 
1130a 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61 67 65  number of a page
1130b 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20   in journal */. 
1130c 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20   u32 cksum;     
1130d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1130e 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65 64 20  * Checksum used 
1130f 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63 6b  for sanity check
11310 69 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61  ing */.  u8 *aDa
11311 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
11312 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
11313 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f 72 20  ary storage for 
11314 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 73 71  the page */.  sq
11315 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b  lite3_file *jfd;
11316 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
11317 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
11318 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e  or for the journ
11319 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73  al file */..  as
1131a 73 65 72 74 28 20 28 69 73 4d 61 69 6e 4a 72 6e  sert( (isMainJrn
1131b 6c 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20  l&~1)==0 );     
1131c 20 2f 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 69   /* isMainJrnl i
1131d 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73  s 0 or 1 */.  as
1131e 73 65 72 74 28 20 28 69 73 53 61 76 65 70 6e 74  sert( (isSavepnt
1131f 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 20  &~1)==0 );      
11320 20 2f 2a 20 69 73 53 61 76 65 70 6e 74 20 69 73   /* isSavepnt is
11321 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 73   0 or 1 */.  ass
11322 65 72 74 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20  ert( isMainJrnl 
11323 7c 7c 20 70 44 6f 6e 65 20 29 3b 20 20 20 20 20  || pDone );     
11324 2f 2a 20 70 44 6f 6e 65 20 61 6c 77 61 79 73 20  /* pDone always 
11325 75 73 65 64 20 6f 6e 20 73 75 62 2d 6a 6f 75 72  used on sub-jour
11326 6e 61 6c 73 20 2a 2f 0a 20 20 61 73 73 65 72 74  nals */.  assert
11327 28 20 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 70  ( isSavepnt || p
11328 44 6f 6e 65 3d 3d 30 20 29 3b 20 20 20 2f 2a 20  Done==0 );   /* 
11329 70 44 6f 6e 65 20 6e 65 76 65 72 20 75 73 65 64  pDone never used
1132a 20 6f 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f 69 6e   on non-savepoin
1132b 74 20 2a 2f 0a 0a 20 20 61 44 61 74 61 20 3d 20  t */..  aData = 
1132c 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70 54 6d  (u8*)pPager->pTm
1132d 70 53 70 61 63 65 3b 0a 20 20 61 73 73 65 72 74  pSpace;.  assert
1132e 28 20 61 44 61 74 61 20 29 3b 20 20 20 20 20 20  ( aData );      
1132f 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61     /* Temp stora
11330 67 65 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72  ge must have alr
11331 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
11332 74 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61  ted */..  /* Rea
11333 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  d the page numbe
11334 72 20 61 6e 64 20 70 61 67 65 20 64 61 74 61 20  r and page data 
11335 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c  from the journal
11336 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a   or sub-journal.
11337 20 20 2a 2a 20 66 69 6c 65 2e 20 52 65 74 75 72    ** file. Retur
11338 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  n an error code 
11339 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 66  to the caller if
1133a 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
1133b 75 72 73 2e 0a 20 20 2a 2f 0a 20 20 6a 66 64 20  urs..  */.  jfd 
1133c 3d 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 3f 20 70  = isMainJrnl ? p
1133d 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20 70 50 61  Pager->jfd : pPa
1133e 67 65 72 2d 3e 73 6a 66 64 3b 0a 20 20 72 63 20  ger->sjfd;.  rc 
1133f 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 64  = read32bits(jfd
11340 2c 20 2a 70 4f 66 66 73 65 74 2c 20 26 70 67 6e  , *pOffset, &pgn
11341 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  o);.  if( rc!=SQ
11342 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
11343 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69   rc;.  rc = sqli
11344 74 65 33 4f 73 52 65 61 64 28 6a 66 64 2c 20 61  te3OsRead(jfd, a
11345 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61  Data, pPager->pa
11346 67 65 53 69 7a 65 2c 20 28 2a 70 4f 66 66 73 65  geSize, (*pOffse
11347 74 29 2b 34 29 3b 0a 20 20 69 66 28 20 72 63 21  t)+4);.  if( rc!
11348 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
11349 75 72 6e 20 72 63 3b 0a 20 20 2a 70 4f 66 66 73  urn rc;.  *pOffs
1134a 65 74 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61  et += pPager->pa
1134b 67 65 53 69 7a 65 20 2b 20 34 20 2b 20 69 73 4d  geSize + 4 + isM
1134c 61 69 6e 4a 72 6e 6c 2a 34 3b 0a 0a 20 20 2f 2a  ainJrnl*4;..  /*
1134d 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   Sanity checking
1134e 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20 20 54   on the page.  T
1134f 68 69 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 6f  his is more impo
11350 72 74 61 6e 74 20 74 68 61 74 20 49 20 6f 72 69  rtant that I ori
11351 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f  ginally.  ** tho
11352 75 67 68 74 2e 20 20 49 66 20 61 20 70 6f 77 65  ught.  If a powe
11353 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73  r failure occurs
11354 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e   while the journ
11355 61 6c 20 69 73 20 62 65 69 6e 67 20 77 72 69 74  al is being writ
11356 74 65 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f 75  ten,.  ** it cou
11357 6c 64 20 63 61 75 73 65 20 69 6e 76 61 6c 69 64  ld cause invalid
11358 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 74   data to be writ
11359 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ten into the jou
1135a 72 6e 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 74  rnal.  We need t
1135b 6f 0a 20 20 2a 2a 20 64 65 74 65 63 74 20 74 68  o.  ** detect th
1135c 69 73 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20  is invalid data 
1135d 28 77 69 74 68 20 68 69 67 68 20 70 72 6f 62 61  (with high proba
1135e 62 69 6c 69 74 79 29 20 61 6e 64 20 69 67 6e 6f  bility) and igno
1135f 72 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  re it..  */.  if
11360 28 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e  ( pgno==0 || pgn
11361 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  o==PAGER_MJ_PGNO
11362 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  (pPager) ){.    
11363 61 73 73 65 72 74 28 20 21 69 73 53 61 76 65 70  assert( !isSavep
11364 6e 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  nt );.    return
11365 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
11366 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 67  }.  if( pgno>(Pg
11367 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  no)pPager->dbSiz
11368 65 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76  e || sqlite3Bitv
11369 65 63 54 65 73 74 28 70 44 6f 6e 65 2c 20 70 67  ecTest(pDone, pg
1136a 6e 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  no) ){.    retur
1136b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1136c 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e  .  if( isMainJrn
1136d 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 65  l ){.    rc = re
1136e 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 28 2a  ad32bits(jfd, (*
1136f 70 4f 66 66 73 65 74 29 2d 34 2c 20 26 63 6b 73  pOffset)-4, &cks
11370 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  um);.    if( rc 
11371 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
11372 20 69 66 28 20 21 69 73 53 61 76 65 70 6e 74 20   if( !isSavepnt 
11373 26 26 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70  && pager_cksum(p
11374 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63  Pager, aData)!=c
11375 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 65  ksum ){.      re
11376 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
11377 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  ;.    }.  }..  i
11378 66 28 20 70 44 6f 6e 65 20 26 26 20 28 72 63 20  f( pDone && (rc 
11379 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  = sqlite3BitvecS
1137a 65 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29  et(pDone, pgno))
1137b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1137c 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1137d 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  }..  assert( pPa
1137e 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
1137f 52 5f 52 45 53 45 52 56 45 44 20 7c 7c 20 70 50  R_RESERVED || pP
11380 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
11381 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a  ER_EXCLUSIVE );.
11382 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67  .  /* If the pag
11383 65 72 20 69 73 20 69 6e 20 52 45 53 45 52 56 45  er is in RESERVE
11384 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68  D state, then th
11385 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 63 6f  ere must be a co
11386 70 79 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20  py of this.  ** 
11387 70 61 67 65 20 69 6e 20 74 68 65 20 70 61 67 65  page in the page
11388 72 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73  r cache. In this
11389 20 63 61 73 65 20 6a 75 73 74 20 75 70 64 61 74   case just updat
1138a 65 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  e the pager cach
1138b 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74 68 65 20  e,.  ** not the 
1138c 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54  database file. T
1138d 68 65 20 70 61 67 65 20 69 73 20 6c 65 66 74 20  he page is left 
1138e 6d 61 72 6b 65 64 20 64 69 72 74 79 20 69 6e 20  marked dirty in 
1138f 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a  this case..  **.
11390 20 20 2a 2a 20 41 6e 20 65 78 63 65 70 74 69 6f    ** An exceptio
11391 6e 20 74 6f 20 74 68 65 20 61 62 6f 76 65 20 72  n to the above r
11392 75 6c 65 3a 20 49 66 20 74 68 65 20 64 61 74 61  ule: If the data
11393 62 61 73 65 20 69 73 20 69 6e 20 6e 6f 2d 73 79  base is in no-sy
11394 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20 61 6e 64  nc mode.  ** and
11395 20 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64   a page is moved
11396 20 64 75 72 69 6e 67 20 61 6e 20 69 6e 63 72 65   during an incre
11397 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 74 68  mental vacuum th
11398 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 0a  en the page may.
11399 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69 6e 20 74    ** not be in t
1139a 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20  he pager cache. 
1139b 4c 61 74 65 72 3a 20 69 66 20 61 20 6d 61 6c 6c  Later: if a mall
1139c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72 72 6f 72  oc() or IO error
1139d 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 64 75 72   occurs.  ** dur
1139e 69 6e 67 20 61 20 4d 6f 76 65 70 61 67 65 28 29  ing a Movepage()
1139f 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20   call, then the 
113a0 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20  page may not be 
113a1 69 6e 20 74 68 65 20 63 61 63 68 65 0a 20 20 2a  in the cache.  *
113a2 2a 20 65 69 74 68 65 72 2e 20 53 6f 20 74 68 65  * either. So the
113a3 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 72   condition descr
113a4 69 62 65 64 20 69 6e 20 74 68 65 20 61 62 6f 76  ibed in the abov
113a5 65 20 70 61 72 61 67 72 61 70 68 20 69 73 20 6e  e paragraph is n
113a6 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72 74 28 29  ot.  ** assert()
113a7 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  able..  **.  ** 
113a8 49 66 20 69 6e 20 45 58 43 4c 55 53 49 56 45 20  If in EXCLUSIVE 
113a9 73 74 61 74 65 2c 20 74 68 65 6e 20 77 65 20 75  state, then we u
113aa 70 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20  pdate the pager 
113ab 63 61 63 68 65 20 69 66 20 69 74 20 65 78 69 73  cache if it exis
113ac 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20  ts.  ** and the 
113ad 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68 65 20 70  main file. The p
113ae 61 67 65 20 69 73 20 74 68 65 6e 20 6d 61 72 6b  age is then mark
113af 65 64 20 6e 6f 74 20 64 69 72 74 79 2e 0a 20 20  ed not dirty..  
113b0 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 23  **.  ** Ticket #
113b1 31 31 37 31 3a 20 20 54 68 65 20 73 74 61 74 65  1171:  The state
113b2 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 69 67  ment journal mig
113b3 68 74 20 63 6f 6e 74 61 69 6e 20 70 61 67 65 20  ht contain page 
113b4 63 6f 6e 74 65 6e 74 20 74 68 61 74 20 69 73 0a  content that is.
113b5 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 66    ** different f
113b6 72 6f 6d 20 74 68 65 20 70 61 67 65 20 63 6f 6e  rom the page con
113b7 74 65 6e 74 20 61 74 20 74 68 65 20 73 74 61 72  tent at the star
113b8 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  t of the transac
113b9 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 69 73 20  tion..  ** This 
113ba 6f 63 63 75 72 73 20 77 68 65 6e 20 61 20 70 61  occurs when a pa
113bb 67 65 20 69 73 20 63 68 61 6e 67 65 64 20 70 72  ge is changed pr
113bc 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 74  ior to the start
113bd 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 0a   of a statement.
113be 20 20 2a 2a 20 74 68 65 6e 20 63 68 61 6e 67 65    ** then change
113bf 64 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74  d again within t
113c0 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 57  he statement.  W
113c1 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b  hen rolling back
113c2 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 74 61   such a.  ** sta
113c3 74 65 6d 65 6e 74 20 77 65 20 6d 75 73 74 20 6e  tement we must n
113c4 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65 20  ot write to the 
113c5 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73  original databas
113c6 65 20 75 6e 6c 65 73 73 20 77 65 20 6b 6e 6f 77  e unless we know
113c7 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72 74 61 69  .  ** for certai
113c8 6e 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 20  n that original 
113c9 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 61 72  page contents ar
113ca 65 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68  e synced into th
113cb 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a  e main rollback.
113cc 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f    ** journal.  O
113cd 74 68 65 72 77 69 73 65 2c 20 61 20 70 6f 77 65  therwise, a powe
113ce 72 20 6c 6f 73 73 20 6d 69 67 68 74 20 6c 65 61  r loss might lea
113cf 76 65 20 6d 6f 64 69 66 69 65 64 20 64 61 74 61  ve modified data
113d0 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74   in the.  ** dat
113d1 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f  abase file witho
113d2 75 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74  ut an entry in t
113d3 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
113d4 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a 20 20 2a  nal that can.  *
113d5 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61  * restore the da
113d6 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72  tabase to its or
113d7 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20 20 54 77  iginal form.  Tw
113d8 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 73  o conditions mus
113d9 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74 20 62 65  t be.  ** met be
113da 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20  fore writing to 
113db 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
113dc 65 73 2e 20 28 31 29 20 74 68 65 20 64 61 74 61  es. (1) the data
113dd 62 61 73 65 20 6d 75 73 74 20 62 65 0a 20 20 2a  base must be.  *
113de 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32 29 20 77  * locked.  (2) w
113df 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20  e know that the 
113e0 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f  original page co
113e1 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c 79 20 73  ntent is fully s
113e2 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e 20 74 68  ynced.  ** in th
113e3 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 65  e main journal e
113e4 69 74 68 65 72 20 62 65 63 61 75 73 65 20 74 68  ither because th
113e5 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e  e page is not in
113e6 20 63 61 63 68 65 20 6f 72 20 65 6c 73 65 0a 20   cache or else. 
113e7 20 2a 2a 20 74 68 65 20 70 61 67 65 20 69 73 20   ** the page is 
113e8 6d 61 72 6b 65 64 20 61 73 20 6e 65 65 64 53 79  marked as needSy
113e9 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  nc==0..  **.  **
113ea 20 32 30 30 38 2d 30 34 2d 31 34 3a 20 20 57 68   2008-04-14:  Wh
113eb 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  en attempting to
113ec 20 76 61 63 75 75 6d 20 61 20 63 6f 72 72 75 70   vacuum a corrup
113ed 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  t database file,
113ee 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73   it.  ** is poss
113ef 69 62 6c 65 20 74 6f 20 66 61 69 6c 20 61 20 73  ible to fail a s
113f0 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61 20 64 61  tatement on a da
113f1 74 61 62 61 73 65 20 74 68 61 74 20 64 6f 65 73  tabase that does
113f2 20 6e 6f 74 20 79 65 74 20 65 78 69 73 74 2e 0a   not yet exist..
113f3 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61 74 74 65    ** Do not atte
113f4 6d 70 74 20 74 6f 20 77 72 69 74 65 20 69 66 20  mpt to write if 
113f5 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
113f6 73 20 6e 65 76 65 72 20 62 65 65 6e 20 6f 70 65  s never been ope
113f7 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 20  ned..  */.  pPg 
113f8 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70  = pager_lookup(p
113f9 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20  Pager, pgno);.  
113fa 61 73 73 65 72 74 28 20 70 50 67 20 7c 7c 20 21  assert( pPg || !
113fb 4d 45 4d 44 42 20 29 3b 0a 20 20 50 41 47 45 52  MEMDB );.  PAGER
113fc 54 52 41 43 45 28 28 22 50 4c 41 59 42 41 43 4b  TRACE(("PLAYBACK
113fd 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68   %d page %d hash
113fe 28 25 30 38 78 29 20 25 73 5c 6e 22 2c 0a 20 20  (%08x) %s\n",.  
113ff 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47               PAG
11400 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67  ERID(pPager), pg
11401 6e 6f 2c 20 70 61 67 65 72 5f 64 61 74 61 68 61  no, pager_dataha
11402 73 68 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53  sh(pPager->pageS
11403 69 7a 65 2c 20 61 44 61 74 61 29 2c 0a 20 20 20  ize, aData),.   
11404 20 20 20 20 20 20 20 20 20 20 20 20 28 69 73 4d              (isM
11405 61 69 6e 4a 72 6e 6c 3f 22 6d 61 69 6e 2d 6a 6f  ainJrnl?"main-jo
11406 75 72 6e 61 6c 22 3a 22 73 75 62 2d 6a 6f 75 72  urnal":"sub-jour
11407 6e 61 6c 22 29 0a 20 20 29 29 3b 0a 20 20 69 66  nal").  ));.  if
11408 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  ( (pPager->state
11409 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  >=PAGER_EXCLUSIV
1140a 45 29 0a 20 20 20 26 26 20 28 70 50 67 3d 3d 30  E).   && (pPg==0
1140b 20 7c 7c 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 61   || 0==(pPg->fla
1140c 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
1140d 4e 43 29 29 0a 20 20 20 26 26 20 69 73 4f 70 65  NC)).   && isOpe
1140e 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 0a 20 20  n(pPager->fd).  
1140f 20 26 26 20 21 69 73 55 6e 73 79 6e 63 0a 20 20   && !isUnsync.  
11410 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 73 74 20  ){.    i64 ofst 
11411 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29  = (pgno-1)*(i64)
11412 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11413 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
11414 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
11415 2d 3e 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61  ->fd, aData, pPa
11416 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f  ger->pageSize, o
11417 66 73 74 29 3b 0a 20 20 20 20 69 66 28 20 70 67  fst);.    if( pg
11418 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  no>pPager->dbFil
11419 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70  eSize ){.      p
1141a 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a  Pager->dbFileSiz
1141b 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a  e = pgno;.    }.
1141c 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75      sqlite3Backu
1141d 70 55 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e  pUpdate(pPager->
1141e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 61  pBackup, pgno, a
1141f 44 61 74 61 29 3b 0a 20 20 7d 65 6c 73 65 20 69  Data);.  }else i
11420 66 28 20 21 69 73 4d 61 69 6e 4a 72 6e 6c 20 26  f( !isMainJrnl &
11421 26 20 70 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20  & pPg==0 ){.    
11422 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
11423 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 73 61  rollback of a sa
11424 76 65 70 6f 69 6e 74 20 61 6e 64 20 64 61 74 61  vepoint and data
11425 20 77 61 73 20 6e 6f 74 20 77 72 69 74 74 65 6e   was not written
11426 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64   to.    ** the d
11427 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20  atabase and the 
11428 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 2d 6d  page is not in-m
11429 65 6d 6f 72 79 2c 20 74 68 65 72 65 20 69 73 20  emory, there is 
1142a 61 20 70 6f 74 65 6e 74 69 61 6c 0a 20 20 20 20  a potential.    
1142b 2a 2a 20 70 72 6f 62 6c 65 6d 2e 20 57 68 65 6e  ** problem. When
1142c 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 65 78   the page is nex
1142d 74 20 66 65 74 63 68 65 64 20 62 79 20 74 68 65  t fetched by the
1142e 20 62 2d 74 72 65 65 20 6c 61 79 65 72 2c 20 69   b-tree layer, i
1142f 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  t .    ** will b
11430 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  e read from the 
11431 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77  database file, w
11432 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 79 20  hich may or may 
11433 6e 6f 74 20 62 65 20 0a 20 20 20 20 2a 2a 20 63  not be .    ** c
11434 75 72 72 65 6e 74 2e 20 0a 20 20 20 20 2a 2a 0a  urrent. .    **.
11435 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 72 65      ** There are
11436 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66   a couple of dif
11437 66 65 72 65 6e 74 20 77 61 79 73 20 74 68 69 73  ferent ways this
11438 20 63 61 6e 20 68 61 70 70 65 6e 2e 20 41 6c 6c   can happen. All
11439 20 61 72 65 20 71 75 69 74 65 0a 20 20 20 20 2a   are quite.    *
1143a 2a 20 6f 62 73 63 75 72 65 2e 20 57 68 65 6e 20  * obscure. When 
1143b 72 75 6e 6e 69 6e 67 20 69 6e 20 73 79 6e 63 68  running in synch
1143c 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 74 68 69  ronous mode, thi
1143d 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65  s can only happe
1143e 6e 20 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65  n .    ** if the
1143f 20 70 61 67 65 20 69 73 20 6f 6e 20 74 68 65 20   page is on the 
11440 66 72 65 65 2d 6c 69 73 74 20 61 74 20 74 68 65  free-list at the
11441 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72   start of the tr
11442 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 0a  ansaction, then.
11443 20 20 20 20 2a 2a 20 70 6f 70 75 6c 61 74 65 64      ** populated
11444 2c 20 74 68 65 6e 20 6d 6f 76 65 64 20 75 73 69  , then moved usi
11445 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d  ng sqlite3PagerM
11446 6f 76 65 70 61 67 65 28 29 2e 0a 20 20 20 20 2a  ovepage()..    *
11447 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 6f 6c  *.    ** The sol
11448 75 74 69 6f 6e 20 69 73 20 74 6f 20 61 64 64 20  ution is to add 
11449 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  an in-memory pag
1144a 65 20 74 6f 20 74 68 65 20 63 61 63 68 65 20 63  e to the cache c
1144b 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 20 20 2a 2a  ontaining.    **
1144c 20 74 68 65 20 64 61 74 61 20 6a 75 73 74 20 72   the data just r
1144d 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73 75 62  ead from the sub
1144e 2d 6a 6f 75 72 6e 61 6c 2e 20 4d 61 72 6b 20 74  -journal. Mark t
1144f 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74 79  he page as dirty
11450 20 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 66 20   .    ** and if 
11451 74 68 65 20 70 61 67 65 72 20 72 65 71 75 69 72  the pager requir
11452 65 73 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e  es a journal-syn
11453 63 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 65  c, then mark the
11454 20 70 61 67 65 20 61 73 20 0a 20 20 20 20 2a 2a   page as .    **
11455 20 72 65 71 75 69 72 69 6e 67 20 61 20 6a 6f 75   requiring a jou
11456 72 6e 61 6c 2d 73 79 6e 63 20 62 65 66 6f 72 65  rnal-sync before
11457 20 69 74 20 69 73 20 77 72 69 74 74 65 6e 2e 0a   it is written..
11458 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
11459 74 28 20 69 73 53 61 76 65 70 6e 74 20 29 3b 0a  t( isSavepnt );.
1145a 20 20 20 20 69 66 28 20 28 72 63 20 3d 20 73 71      if( (rc = sq
1145b 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72  lite3PagerAcquir
1145c 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20  e(pPager, pgno, 
1145d 26 70 50 67 2c 20 31 29 29 21 3d 53 51 4c 49 54  &pPg, 1))!=SQLIT
1145e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
1145f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
11460 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d     pPg->flags &=
11461 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41   ~PGHDR_NEED_REA
11462 44 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  D;.    sqlite3Pc
11463 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50  acheMakeDirty(pP
11464 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  g);.  }.  if( pP
11465 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 70  g ){.    /* No p
11466 61 67 65 20 73 68 6f 75 6c 64 20 65 76 65 72 20  age should ever 
11467 62 65 20 65 78 70 6c 69 63 69 74 6c 79 20 72 6f  be explicitly ro
11468 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 74 20 69  lled back that i
11469 73 20 69 6e 20 75 73 65 2c 20 65 78 63 65 70 74  s in use, except
1146a 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 61 67 65  .    ** for page
1146b 20 31 20 77 68 69 63 68 20 69 73 20 68 65 6c 64   1 which is held
1146c 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 64 65 72   in use in order
1146d 20 74 6f 20 6b 65 65 70 20 74 68 65 20 6c 6f 63   to keep the loc
1146e 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  k on the.    ** 
1146f 64 61 74 61 62 61 73 65 20 61 63 74 69 76 65 2e  database active.
11470 20 48 6f 77 65 76 65 72 20 73 75 63 68 20 61 20   However such a 
11471 70 61 67 65 20 6d 61 79 20 62 65 20 72 6f 6c 6c  page may be roll
11472 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73  ed back as a res
11473 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 20 61 6e  ult.    ** of an
11474 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 6f 72 20   internal error 
11475 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 61 6e 20  resulting in an 
11476 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c 20 74  automatic call t
11477 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  o.    ** sqlite3
11478 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 2e  PagerRollback().
11479 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 6f 69 64  .    */.    void
1147a 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 70 44 61   *pData;.    pDa
1147b 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b  ta = pPg->pData;
1147c 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 44 61 74  .    memcpy(pDat
1147d 61 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72  a, aData, pPager
1147e 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
1147f 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78 52 65   if( pPager->xRe
11480 69 6e 69 74 65 72 20 29 7b 0a 20 20 20 20 20 20  initer ){.      
11481 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65  pPager->xReinite
11482 72 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20  r(pPg);.    }.  
11483 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c    if( isMainJrnl
11484 20 26 26 20 28 21 69 73 53 61 76 65 70 6e 74 20   && (!isSavepnt 
11485 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70 50 61  || *pOffset<=pPa
11486 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29  ger->journalHdr)
11487 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
11488 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
11489 74 68 69 73 20 70 61 67 65 20 77 65 72 65 20 6a  this page were j
1148a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66 72 6f  ust restored fro
1148b 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 20 20 20  m the main .    
1148c 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c    ** journal fil
1148d 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f 6e 74  e, then its cont
1148e 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73 20 74  ent must be as t
1148f 68 65 79 20 77 65 72 65 20 77 68 65 6e 20 74 68  hey were when th
11490 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72 61 6e  e .      ** tran
11491 73 61 63 74 69 6f 6e 20 77 61 73 20 66 69 72 73  saction was firs
11492 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 69  t opened. In thi
11493 73 20 63 61 73 65 20 77 65 20 63 61 6e 20 6d 61  s case we can ma
11494 72 6b 20 74 68 65 20 70 61 67 65 0a 20 20 20 20  rk the page.    
11495 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e 2c 20 73    ** as clean, s
11496 69 6e 63 65 20 74 68 65 72 65 20 77 69 6c 6c 20  ince there will 
11497 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 72  be no need to wr
11498 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 74 68  ite it out to th
11499 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  e..      **.    
1149a 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e    ** There is on
1149b 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74  e exception to t
1149c 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74 68 65  his rule. If the
1149d 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 72   page is being r
1149e 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a 20 62  olled.      ** b
1149f 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66 20 61  ack as part of a
114a0 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72 20 73   savepoint (or s
114a1 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c 62 61  tatement) rollba
114a2 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20 20 20  ck from an .    
114a3 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20 70 6f    ** unsynced po
114a4 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 61 69  rtion of the mai
114a5 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  n journal file, 
114a6 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 73  then it is not s
114a7 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  afe.      ** to 
114a8 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73  mark the page as
114a9 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69 73 20   clean. This is 
114aa 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e 67 20  because marking 
114ab 74 68 65 20 70 61 67 65 20 61 73 0a 20 20 20 20  the page as.    
114ac 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c 6c 20    ** clean will 
114ad 63 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 5f  clear the PGHDR_
114ae 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 2e 20  NEED_SYNC flag. 
114af 53 69 6e 63 65 20 74 68 65 20 70 61 67 65 20 69  Since the page i
114b0 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61  s.      ** alrea
114b1 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  dy in the journa
114b2 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64 65 64  l file (recorded
114b3 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75   in Pager.pInJou
114b4 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20 20 20  rnal) and.      
114b5 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45  ** the PGHDR_NEE
114b6 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 63  D_SYNC flag is c
114b7 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65 20 70  leared, if the p
114b8 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74  age is written t
114b9 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e  o.      ** again
114ba 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 61   within this tra
114bb 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77 69 6c  nsaction, it wil
114bc 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64  l be marked as d
114bd 69 72 74 79 20 62 75 74 0a 20 20 20 20 20 20 2a  irty but.      *
114be 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44  * the PGHDR_NEED
114bf 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c 6c 20  _SYNC flag will 
114c0 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74 20 63  not be set. It c
114c1 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65 6e 74  ould then potent
114c2 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 62  ially.      ** b
114c3 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 69 6e  e written out in
114c4 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
114c5 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74 73 20  file before its 
114c6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 20  journal file.   
114c7 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20 69 73     ** segment is
114c8 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20 63 72   synced. If a cr
114c9 61 73 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ash occurs durin
114ca 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  g or following t
114cb 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20 64 61  his,.      ** da
114cc 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
114cd 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20 20 20  n may ensue..   
114ce 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
114cf 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
114d0 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 23  an(pPg);.    }.#
114d1 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45  ifdef SQLITE_CHE
114d2 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67  CK_PAGES.    pPg
114d3 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67  ->pageHash = pag
114d4 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29  er_pagehash(pPg)
114d5 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20  ;.#endif.    /* 
114d6 49 66 20 74 68 69 73 20 77 61 73 20 70 61 67 65  If this was page
114d7 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65   1, then restore
114d8 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61   the value of Pa
114d9 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 2e 0a  ger.dbFileVers..
114da 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 62      ** Do this b
114db 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f 64 69  efore any decodi
114dc 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ng. */.    if( p
114dd 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  gno==1 ){.      
114de 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e  memcpy(&pPager->
114df 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75  dbFileVers, &((u
114e0 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c 73 69  8*)pData)[24],si
114e1 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46  zeof(pPager->dbF
114e2 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 7d  ileVers));.    }
114e3 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 65 20  ..    /* Decode 
114e4 74 68 65 20 70 61 67 65 20 6a 75 73 74 20 72 65  the page just re
114e5 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a  ad from disk */.
114e6 20 20 20 20 43 4f 44 45 43 31 28 70 50 61 67 65      CODEC1(pPage
114e7 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70  r, pData, pPg->p
114e8 67 6e 6f 2c 20 33 29 3b 0a 20 20 20 20 73 71 6c  gno, 3);.    sql
114e9 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
114ea 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65  e(pPg);.  }.  re
114eb 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20  turn rc;.}..#if 
114ec 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
114ed 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
114ee 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54  TE_COVERAGE_TEST
114ef 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  )./*.** This rou
114f0 74 69 6e 65 20 6c 6f 6f 6b 73 20 61 68 65 61 64  tine looks ahead
114f1 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a   into the main j
114f2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20  ournal file and 
114f3 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68  determines.** wh
114f4 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
114f5 20 6e 65 78 74 20 72 65 63 6f 72 64 20 28 74 68   next record (th
114f6 65 20 72 65 63 6f 72 64 20 74 68 61 74 20 62 65  e record that be
114f7 67 69 6e 73 20 61 74 20 66 69 6c 65 0a 2a 2a 20  gins at file.** 
114f8 6f 66 66 73 65 74 20 70 50 61 67 65 72 2d 3e 6a  offset pPager->j
114f9 6f 75 72 6e 61 6c 4f 66 66 29 20 69 73 20 61 20  ournalOff) is a 
114fa 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 70 61 67 65  well-formed page
114fb 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 69   record consisti
114fc 6e 67 0a 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64  ng.** of a valid
114fd 20 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 70 50   page number, pP
114fe 61 67 65 2d 3e 70 61 67 65 53 69 7a 65 20 62 79  age->pageSize by
114ff 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 2c 20  tes of content, 
11500 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 61  followed.** by a
11501 20 76 61 6c 69 64 20 63 68 65 63 6b 73 75 6d 2e   valid checksum.
11502 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
11503 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20   never needs to 
11504 6b 6e 6f 77 20 74 68 69 73 20 69 6e 20 6f 72 64  know this in ord
11505 65 72 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62  er to do its job
11506 2e 20 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74  .   This.** rout
11507 69 6e 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ine is only used
11508 20 66 72 6f 6d 20 77 69 74 68 20 61 73 73 65 72   from with asser
11509 74 28 29 20 61 6e 64 20 74 65 73 74 63 61 73 65  t() and testcase
1150a 28 29 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74  () macros..*/.st
1150b 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 4e 65  atic int pagerNe
1150c 78 74 4a 6f 75 72 6e 61 6c 50 61 67 65 49 73 56  xtJournalPageIsV
1150d 61 6c 69 64 28 50 61 67 65 72 20 2a 70 50 61 67  alid(Pager *pPag
1150e 65 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  er){.  Pgno pgno
1150f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
11510 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  he page number o
11511 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20  f the page */.  
11512 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 20  u32 cksum;      
11513 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
11514 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 69   checksum */.  i
11515 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
11516 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
11517 64 65 20 66 72 6f 6d 20 72 65 61 64 20 6f 70 65  de from read ope
11518 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c  rations */.  sql
11519 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 20 20  ite3_file *fd;  
1151a 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
1151b 73 63 72 69 70 74 6f 72 20 66 72 6f 6d 20 77 68  scriptor from wh
1151c 69 63 68 20 77 65 20 61 72 65 20 72 65 61 64 69  ich we are readi
1151d 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74  ng */.  u8 *aDat
1151e 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  a;           /* 
1151f 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70  Content of the p
11520 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61  age */..  /* Rea
11521 64 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  d the page numbe
11522 72 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 66 64  r header */.  fd
11523 20 3d 20 70 50 61 67 65 72 2d 3e 6a 66 64 3b 0a   = pPager->jfd;.
11524 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74    rc = read32bit
11525 73 28 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f  s(fd, pPager->jo
11526 75 72 6e 61 6c 4f 66 66 2c 20 26 70 67 6e 6f 29  urnalOff, &pgno)
11527 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
11528 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e 20  TE_OK ){ return 
11529 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  0; }            
1152a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1152b 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
1152c 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20  /.  if( pgno==0 
1152d 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d  || pgno==PAGER_M
1152e 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29  J_PGNO(pPager) )
1152f 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 20 20 20  { return 0; }   
11530 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
11531 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 67  /.  if( pgno>(Pg
11532 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  no)pPager->dbSiz
11533 65 20 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d  e ){ return 0; }
11534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11535 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
11536 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  /..  /* Read the
11537 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20 20 72   checksum */.  r
11538 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 66  c = read32bits(f
11539 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  d, pPager->journ
1153a 61 6c 4f 66 66 2b 70 50 61 67 65 72 2d 3e 70 61  alOff+pPager->pa
1153b 67 65 53 69 7a 65 2b 34 2c 20 26 63 6b 73 75 6d  geSize+4, &cksum
1153c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1153d 49 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e  ITE_OK ){ return
1153e 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20   0; }           
1153f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11540 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54         /*NO_TEST
11541 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68  */..  /* Read th
11542 65 20 64 61 74 61 20 61 6e 64 20 76 65 72 69 66  e data and verif
11543 79 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 2a  y the checksum *
11544 2f 0a 20 20 61 44 61 74 61 20 3d 20 28 75 38 2a  /.  aData = (u8*
11545 29 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61  )pPager->pTmpSpa
11546 63 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ce;.  rc = sqlit
11547 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 44 61  e3OsRead(fd, aDa
11548 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  ta, pPager->page
11549 53 69 7a 65 2c 20 70 50 61 67 65 72 2d 3e 6a 6f  Size, pPager->jo
1154a 75 72 6e 61 6c 4f 66 66 2b 34 29 3b 0a 20 20 69  urnalOff+4);.  i
1154b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1154c 20 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 20   ){ return 0; } 
1154d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1154e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1154f 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 69   /*NO_TEST*/.  i
11550 66 28 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70  f( pager_cksum(p
11551 50 61 67 65 72 2c 20 61 44 61 74 61 29 21 3d 63  Pager, aData)!=c
11552 6b 73 75 6d 20 29 7b 20 72 65 74 75 72 6e 20 30  ksum ){ return 0
11553 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20  ; }             
11554 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 20 20   /*NO_TEST*/..  
11555 2f 2a 20 52 65 61 63 68 20 74 68 69 73 20 70 6f  /* Reach this po
11556 69 6e 74 20 6f 6e 6c 79 20 69 66 20 74 68 65 20  int only if the 
11557 70 61 67 65 20 69 73 20 76 61 6c 69 64 20 2a 2f  page is valid */
11558 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
11559 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
1155a 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66  d(NDEBUG) || def
1155b 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45  ined(SQLITE_COVE
1155c 52 41 47 45 5f 54 45 53 54 29 20 2a 2f 0a 0a 2f  RAGE_TEST) */../
1155d 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
1155e 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e 61  Master is the na
1155f 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a  me of a master j
11560 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73  ournal file. A s
11561 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  ingle journal.**
11562 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 72   file that refer
11563 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65  red to the maste
11564 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  r journal file h
11565 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c  as just been rol
11566 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69  led back..** Thi
11567 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
11568 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62   if it is possib
11569 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  le to delete the
1156a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1156b 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65  file,.** and doe
1156c 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a  s so if it is..*
1156d 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d  *.** Argument zM
1156e 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20  aster may point 
1156f 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61  to Pager.pTmpSpa
11570 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66  ce. So that buff
11571 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76  er is not .** av
11572 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20  ailable for use 
11573 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63  within this func
11574 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  tion..**.** When
11575 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
11576 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65  l file is create
11577 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 74  d, it is populat
11578 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ed with the name
11579 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20  s .** of all of 
1157a 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  its child journa
1157b 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 6e  ls, one after an
1157c 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 64  other, formatted
1157d 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e   as utf-8 .** en
1157e 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 20  coded text. The 
1157f 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 6c  end of each chil
11580 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  d journal file i
11581 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 20  s marked with a 
11582 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  .** nul-terminat
11583 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e 20  or byte (0x00). 
11584 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 20  i.e. the entire 
11585 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61  contents of a ma
11586 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ster journal.** 
11587 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e 73  file for a trans
11588 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67  action involving
11589 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 6d   two databases m
1158a 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20  ight be:.**.**  
1158b 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64   "/home/bill/a.d
1158c 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f  b-journal\x00/ho
1158d 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75  me/bill/b.db-jou
1158e 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20  rnal\x00".**.** 
1158f 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  A master journal
11590 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62   file may only b
11591 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 61  e deleted once a
11592 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20  ll of its child 
11593 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76  .** journals hav
11594 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61  e been rolled ba
11595 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ck..**.** This f
11596 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 68  unction reads th
11597 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
11598 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  e master-journal
11599 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d   file into .** m
1159a 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20  emory and loops 
1159b 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 20  through each of 
1159c 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  the child journa
1159d 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20  l names. For.** 
1159e 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e  each child journ
1159f 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 66  al, it checks if
115a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74  :.**.**   * if t
115a1 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c  he child journal
115a2 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 20   exists, and if 
115a3 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65  so.**   * if the
115a4 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63   child journal c
115a5 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 65  ontains a refere
115a6 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f  nce to master jo
115a7 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69  urnal .**     fi
115a8 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a  le zMaster.**.**
115a9 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 72   If a child jour
115aa 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  nal can be found
115ab 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 6f   that matches bo
115ac 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 72  th of the criter
115ad 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69  ia.** above, thi
115ae 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
115af 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  ns without doing
115b0 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 72   anything. Other
115b1 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73  wise, if.** no s
115b2 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  uch child journa
115b3 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20  l can be found, 
115b4 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20  file zMaster is 
115b5 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20  deleted from.** 
115b6 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
115b7 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44  using sqlite3OsD
115b8 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  elete()..**.** I
115b9 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 69  f an IO error wi
115ba 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69  thin this functi
115bb 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  on, an error cod
115bc 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54  e is returned. T
115bd 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
115be 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79  allocates memory
115bf 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69   by calling sqli
115c0 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20  te3Malloc(). If 
115c1 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  an allocation.**
115c2 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e   fails, SQLITE_N
115c3 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
115c4 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
115c5 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20  no IO or malloc 
115c6 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72  errors .** occur
115c7 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
115c8 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
115c9 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 69  ODO: This functi
115ca 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73  on allocates a s
115cb 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d  ingle block of m
115cc 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a  emory to load.**
115cd 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74   the entire cont
115ce 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74  ents of the mast
115cf 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
115d0 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a   This could be.*
115d1 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69  * a couple of ki
115d2 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d 20  lobytes or so - 
115d3 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67  potentially larg
115d4 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65  er than the page
115d5 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74   .** size..*/.st
115d6 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64  atic int pager_d
115d7 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a  elmaster(Pager *
115d8 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68  pPager, const ch
115d9 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
115da 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
115db 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73  s = pPager->pVfs
115dc 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ;.  int rc;     
115dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
115de 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
115df 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
115e0 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d  pMaster;    /* M
115e1 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a  alloc'd master-j
115e2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63  ournal file desc
115e3 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69  riptor */.  sqli
115e4 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e  te3_file *pJourn
115e5 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27  al;   /* Malloc'
115e6 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20  d child-journal 
115e7 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
115e8 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  */.  char *zMast
115e9 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f  erJournal = 0; /
115ea 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61  * Contents of ma
115eb 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
115ec 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74  e */.  i64 nMast
115ed 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20  erJournal;      
115ee 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74   /* Size of mast
115ef 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
115f0 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  */..  /* Allocat
115f1 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 68  e space for both
115f2 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 6e   the pJournal an
115f3 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 64  d pMaster file d
115f4 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a 2a  escriptors..  **
115f5 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
115f6 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20  open the master 
115f7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
115f8 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20   reading..  */. 
115f9 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69   pMaster = (sqli
115fa 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74  te3_file *)sqlit
115fb 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66  e3MallocZero(pVf
115fc 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29  s->szOsFile * 2)
115fd 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28  ;.  pJournal = (
115fe 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28  sqlite3_file *)(
115ff 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20  ((u8 *)pMaster) 
11600 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  + pVfs->szOsFile
11601 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65  );.  if( !pMaste
11602 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  r ){.    rc = SQ
11603 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
11604 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  lse{.    const i
11605 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49  nt flags = (SQLI
11606 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
11607 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  |SQLITE_OPEN_MAS
11608 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20  TER_JOURNAL);.  
11609 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1160a 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74  Open(pVfs, zMast
1160b 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61  er, pMaster, fla
1160c 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  gs, 0);.  }.  if
1160d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1160e 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72  ) goto delmaster
1160f 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71  _out;..  rc = sq
11610 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
11611 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 65  pMaster, &nMaste
11612 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28  rJournal);.  if(
11613 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11614 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
11615 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73  out;..  if( nMas
11616 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a  terJournal>0 ){.
11617 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e      char *zJourn
11618 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d  al;.    char *zM
11619 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 20  asterPtr = 0;.  
1161a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72    int nMasterPtr
1161b 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e   = pVfs->mxPathn
1161c 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c  ame+1;..    /* L
1161d 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d  oad the entire m
1161e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1161f 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  le into space ob
11620 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 20  tained from.    
11621 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
11622 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 20  c() and pointed 
11623 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f 75  to by zMasterJou
11624 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  rnal. .    */.  
11625 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c    zMasterJournal
11626 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
11627 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d  e3Malloc((int)nM
11628 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e  asterJournal + n
11629 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20  MasterPtr);.    
1162a 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72  if( !zMasterJour
1162b 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  nal ){.      rc 
1162c 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1162d 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
1162e 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  ster_out;.    }.
1162f 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d      zMasterPtr =
11630 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c   &zMasterJournal
11631 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5d  [nMasterJournal]
11632 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
11633 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72  e3OsRead(pMaster
11634 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  , zMasterJournal
11635 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f  , (int)nMasterJo
11636 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69  urnal, 0);.    i
11637 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
11638 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65   ) goto delmaste
11639 72 5f 6f 75 74 3b 0a 0a 20 20 20 20 7a 4a 6f 75  r_out;..    zJou
1163a 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a 6f  rnal = zMasterJo
1163b 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c 65  urnal;.    while
1163c 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73  ( (zJournal-zMas
1163d 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73  terJournal)<nMas
1163e 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  terJournal ){.  
1163f 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a      int exists;.
11640 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
11641 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c  e3OsAccess(pVfs,
11642 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54   zJournal, SQLIT
11643 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c  E_ACCESS_EXISTS,
11644 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20 20   &exists);.     
11645 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
11646 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
11647 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  to delmaster_out
11648 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
11649 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 20  if( exists ){.  
1164a 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
1164b 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f 69  the journals poi
1164c 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 6d  nted to by the m
1164d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 78  aster journal ex
1164e 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a  ists..        **
1164f 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68 65   Open it and che
11650 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73 20  ck if it points 
11651 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  at the master jo
11652 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 20  urnal. If.      
11653 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e 20    ** so, return 
11654 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e 67  without deleting
11655 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11656 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20  nal file..      
11657 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
11658 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   c;.        int 
11659 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f  flags = (SQLITE_
1165a 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51  OPEN_READONLY|SQ
1165b 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
1165c 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 20  OURNAL);.       
1165d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
1165e 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e  pen(pVfs, zJourn
1165f 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c  al, pJournal, fl
11660 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ags, 0);.       
11661 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
11662 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
11663 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f  goto delmaster_o
11664 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ut;.        }.. 
11665 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64         rc = read
11666 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a  MasterJournal(pJ
11667 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50  ournal, zMasterP
11668 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b  tr, nMasterPtr);
11669 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1166a 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c  OsClose(pJournal
1166b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1166c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1166d 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64            goto d
1166e 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20  elmaster_out;.  
1166f 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
11670 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 5b   c = zMasterPtr[
11671 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70 28  0]!=0 && strcmp(
11672 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 73  zMasterPtr, zMas
11673 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 20  ter)==0;.       
11674 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20 20   if( c ){.      
11675 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61      /* We have a
11676 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 64   match. Do not d
11677 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72  elete the master
11678 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a   journal file. *
11679 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  /.          goto
1167a 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
1167b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1167c 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 6c  }.      zJournal
1167d 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c   += (sqlite3Strl
1167e 65 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31  en30(zJournal)+1
1167f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a  );.    }.  }.  .
11680 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11681 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61  Delete(pVfs, zMa
11682 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c 6d 61  ster, 0);..delma
11683 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20  ster_out:.  if( 
11684 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29  zMasterJournal )
11685 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
11686 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61  ee(zMasterJourna
11687 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20  l);.  }  .  if( 
11688 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 73  pMaster ){.    s
11689 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4d  qlite3OsClose(pM
1168a 61 73 74 65 72 29 3b 0a 20 20 20 20 61 73 73 65  aster);.    asse
1168b 72 74 28 20 21 69 73 4f 70 65 6e 28 70 4a 6f 75  rt( !isOpen(pJou
1168c 72 6e 61 6c 29 20 29 3b 0a 20 20 7d 0a 20 20 73  rnal) );.  }.  s
1168d 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 73  qlite3_free(pMas
1168e 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ter);.  return r
1168f 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  c;.}.../*.** Thi
11690 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
11691 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ed to change the
11692 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 20   actual size of 
11693 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a  the database .**
11694 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c   file in the fil
11695 65 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20 6f  e-system. This o
11696 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e  nly happens when
11697 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72   committing a tr
11698 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 6f 72  ansaction,.** or
11699 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20   rolling back a 
1169a 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e 63  transaction (inc
1169b 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20 62  luding rolling b
1169c 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  ack a hot-journa
1169d 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  l)..**.** If the
1169e 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
1169f 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c  ile is not open,
116a0 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65   or an exclusive
116a1 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 0a 2a 2a 20   lock is not.** 
116a2 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74  held, this funct
116a3 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  ion is a no-op. 
116a4 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73  Otherwise, the s
116a5 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
116a6 69 73 0a 2a 2a 20 63 68 61 6e 67 65 64 20 74 6f  is.** changed to
116a7 20 6e 50 61 67 65 20 70 61 67 65 73 20 28 6e 50   nPage pages (nP
116a8 61 67 65 2a 70 50 61 67 65 72 2d 3e 70 61 67 65  age*pPager->page
116a9 53 69 7a 65 20 62 79 74 65 73 29 2e 20 49 66 20  Size bytes). If 
116aa 74 68 65 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64  the file.** on d
116ab 69 73 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79  isk is currently
116ac 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6e 50 61   larger than nPa
116ad 67 65 20 70 61 67 65 73 2c 20 74 68 65 6e 20 75  ge pages, then u
116ae 73 65 20 74 68 65 20 56 46 53 0a 2a 2a 20 78 54  se the VFS.** xT
116af 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64  runcate() method
116b0 20 74 6f 20 74 72 75 6e 63 61 74 65 20 69 74 2e   to truncate it.
116b1 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 74 20 6d 69  .**.** Or, it mi
116b2 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68 65  ght might be the
116b3 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 66   case that the f
116b4 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 73  ile on disk is s
116b5 6d 61 6c 6c 65 72 20 74 68 61 6e 20 0a 2a 2a 20  maller than .** 
116b6 6e 50 61 67 65 20 70 61 67 65 73 2e 20 53 6f 6d  nPage pages. Som
116b7 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
116b8 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  em implementatio
116b9 6e 73 20 63 61 6e 20 67 65 74 20 63 6f 6e 66 75  ns can get confu
116ba 73 65 64 20 69 66 20 0a 2a 2a 20 79 6f 75 20 74  sed if .** you t
116bb 72 79 20 74 6f 20 74 72 75 6e 63 61 74 65 20 61  ry to truncate a
116bc 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69   file to some si
116bd 7a 65 20 74 68 61 74 20 69 73 20 6c 61 72 67 65  ze that is large
116be 72 20 74 68 61 6e 20 69 74 20 0a 2a 2a 20 63 75  r than it .** cu
116bf 72 72 65 6e 74 6c 79 20 69 73 2c 20 73 6f 20 64  rrently is, so d
116c0 65 74 65 63 74 20 74 68 69 73 20 63 61 73 65 20  etect this case 
116c1 61 6e 64 20 77 72 69 74 65 20 61 20 73 69 6e 67  and write a sing
116c2 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74 6f 20  le zero byte to 
116c3 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74  .** the end of t
116c4 68 65 20 6e 65 77 20 66 69 6c 65 20 69 6e 73 74  he new file inst
116c5 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  ead..**.** If su
116c6 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e  ccessful, return
116c7 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61   SQLITE_OK. If a
116c8 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
116c9 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 79 69 6e  s while modifyin
116ca 67 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  g.** the databas
116cb 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e 20 74  e file, return t
116cc 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f  he error code to
116cd 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a   the caller..*/.
116ce 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
116cf 5f 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 20  _truncate(Pager 
116d0 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50  *pPager, Pgno nP
116d1 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  age){.  int rc =
116d2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
116d3 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
116d4 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45  =PAGER_EXCLUSIVE
116d5 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
116d6 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 69 36  r->fd) ){.    i6
116d7 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20 6e  4 currentSize, n
116d8 65 77 53 69 7a 65 3b 0a 20 20 20 20 2f 2a 20 54  ewSize;.    /* T
116d9 4f 44 4f 3a 20 49 73 20 69 74 20 73 61 66 65 20  ODO: Is it safe 
116da 74 6f 20 75 73 65 20 50 61 67 65 72 2e 64 62 46  to use Pager.dbF
116db 69 6c 65 53 69 7a 65 20 68 65 72 65 3f 20 2a 2f  ileSize here? */
116dc 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
116dd 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67  3OsFileSize(pPag
116de 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e 74  er->fd, &current
116df 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53 69  Size);.    newSi
116e0 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67  ze = pPager->pag
116e1 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67 65  eSize*(i64)nPage
116e2 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
116e3 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 65  LITE_OK && curre
116e4 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 20  ntSize!=newSize 
116e5 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 72  ){.      if( cur
116e6 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a 65  rentSize>newSize
116e7 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
116e8 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
116e9 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6e  te(pPager->fd, n
116ea 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d  ewSize);.      }
116eb 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
116ec 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
116ed 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22 22  e(pPager->fd, ""
116ee 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29 3b  , 1, newSize-1);
116ef 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
116f0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
116f1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67   ){.        pPag
116f2 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d  er->dbFileSize =
116f3 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a   nPage;.      }.
116f4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
116f5 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
116f6 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Set the value of
116f7 20 74 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f   the Pager.secto
116f8 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 66  rSize variable f
116f9 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20  or the given.** 
116fa 70 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20 74  pager based on t
116fb 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
116fc 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72  d by the xSector
116fd 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f  Size method.** o
116fe 66 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62  f the open datab
116ff 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73 65  ase file. The se
11700 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62  ctor size will b
11701 65 20 75 73 65 64 20 75 73 65 64 20 0a 2a 2a 20  e used used .** 
11702 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
11703 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 67 6e 6d   size and alignm
11704 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68  ent of journal h
11705 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a 20 6d 61  eader and .** ma
11706 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69  ster journal poi
11707 6e 74 65 72 73 20 77 69 74 68 69 6e 20 63 72 65  nters within cre
11708 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ated journal fil
11709 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 65  es..**.** For te
1170a 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 74 68  mporary files th
1170b 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74  e effective sect
1170c 6f 72 20 73 69 7a 65 20 69 73 20 61 6c 77 61 79  or size is alway
1170d 73 20 35 31 32 20 62 79 74 65 73 2e 0a 2a 2a 0a  s 512 bytes..**.
1170e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f  ** Otherwise, fo
1170f 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 72 79 20  r non-temporary 
11710 66 69 6c 65 73 2c 20 74 68 65 20 65 66 66 65 63  files, the effec
11711 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65  tive sector size
11712 20 69 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65   is.** the value
11713 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
11714 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d   xSectorSize() m
11715 65 74 68 6f 64 20 72 6f 75 6e 64 65 64 20 75 70  ethod rounded up
11716 20 74 6f 20 35 31 32 20 69 66 0a 2a 2a 20 69 74   to 512 if.** it
11717 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 35 31   is less than 51
11718 32 2c 20 6f 72 20 72 6f 75 6e 64 65 64 20 64 6f  2, or rounded do
11719 77 6e 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f 52  wn to MAX_SECTOR
1171a 5f 53 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20 69  _SIZE if it.** i
1171b 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4d  s greater than M
1171c 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e 0a  AX_SECTOR_SIZE..
1171d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
1171e 65 74 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67  etSectorSize(Pag
1171f 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61  er *pPager){.  a
11720 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
11721 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61  ager->fd) || pPa
11722 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
11723 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d  ..  if( !pPager-
11724 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20  >tempFile ){.   
11725 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20   /* Sector size 
11726 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 66  doesn't matter f
11727 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  or temporary fil
11728 65 73 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66 69  es. Also, the fi
11729 6c 65 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f  le.    ** may no
1172a 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e  t have been open
1172b 65 64 20 79 65 74 2c 20 69 6e 20 77 68 69 63 68  ed yet, in which
1172c 20 63 61 73 65 20 74 68 65 20 4f 73 53 65 63 74   case the OsSect
1172d 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20  orSize().    ** 
1172e 63 61 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61 75  call will segfau
1172f 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  lt..    */.    p
11730 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
11731 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63  e = sqlite3OsSec
11732 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d 3e  torSize(pPager->
11733 66 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  fd);.  }.  if( p
11734 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
11735 65 3c 35 31 32 20 29 7b 0a 20 20 20 20 70 50 61  e<512 ){.    pPa
11736 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20  ger->sectorSize 
11737 3d 20 35 31 32 3b 0a 20 20 7d 0a 20 20 69 66 28  = 512;.  }.  if(
11738 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53   pPager->sectorS
11739 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53  ize>MAX_SECTOR_S
1173a 49 5a 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72  IZE ){.    asser
1173b 74 28 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49  t( MAX_SECTOR_SI
1173c 5a 45 3e 3d 35 31 32 20 29 3b 0a 20 20 20 20 70  ZE>=512 );.    p
1173d 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
1173e 65 20 3d 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53  e = MAX_SECTOR_S
1173f 49 5a 45 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  IZE;.  }.}../*.*
11740 2a 20 50 6c 61 79 62 61 63 6b 20 74 68 65 20 6a  * Playback the j
11741 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 75 73 20  ournal and thus 
11742 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61  restore the data
11743 62 61 73 65 20 66 69 6c 65 20 74 6f 0a 2a 2a 20  base file to.** 
11744 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61 73  the state it was
11745 20 69 6e 20 62 65 66 6f 72 65 20 77 65 20 73 74   in before we st
11746 61 72 74 65 64 20 6d 61 6b 69 6e 67 20 63 68 61  arted making cha
11747 6e 67 65 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  nges.  .**.** Th
11748 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
11749 6f 72 6d 61 74 20 69 73 20 61 73 20 66 6f 6c 6c  ormat is as foll
1174a 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29  ows: .**.**  (1)
1174b 20 20 38 20 62 79 74 65 20 70 72 65 66 69 78 2e    8 byte prefix.
1174c 20 20 41 20 63 6f 70 79 20 6f 66 20 61 4a 6f 75    A copy of aJou
1174d 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20  rnalMagic[]..** 
1174e 20 28 32 29 20 20 34 20 62 79 74 65 20 62 69 67   (2)  4 byte big
1174f 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
11750 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d  which is the num
11751 62 65 72 20 6f 66 20 76 61 6c 69 64 20 70 61 67  ber of valid pag
11752 65 20 72 65 63 6f 72 64 73 0a 2a 2a 20 20 20 20  e records.**    
11753 20 20 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61     in the journa
11754 6c 2e 20 20 49 66 20 74 68 69 73 20 76 61 6c 75  l.  If this valu
11755 65 20 69 73 20 30 78 66 66 66 66 66 66 66 66 2c  e is 0xffffffff,
11756 20 74 68 65 6e 20 63 6f 6d 70 75 74 65 20 74 68   then compute th
11757 65 0a 2a 2a 20 20 20 20 20 20 20 6e 75 6d 62 65  e.**       numbe
11758 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f 72 64  r of page record
11759 73 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  s from the journ
1175a 61 6c 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 33 29  al size..**  (3)
1175b 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64    4 byte big-end
1175c 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63  ian integer whic
1175d 68 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c  h is the initial
1175e 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 0a   value for the .
1175f 2a 2a 20 20 20 20 20 20 20 73 61 6e 69 74 79 20  **       sanity 
11760 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34  checksum..**  (4
11761 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67 65  )  4 byte intege
11762 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e  r which is the n
11763 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74  umber of pages t
11764 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 0a 2a  o truncate the.*
11765 2a 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65  *       database
11766 20 74 6f 20 64 75 72 69 6e 67 20 61 20 72 6f 6c   to during a rol
11767 6c 62 61 63 6b 2e 0a 2a 2a 20 20 28 35 29 20 20  lback..**  (5)  
11768 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61  4 byte big-endia
11769 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
1176a 69 73 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  is the sector si
1176b 7a 65 2e 20 20 54 68 65 20 68 65 61 64 65 72 0a  ze.  The header.
1176c 2a 2a 20 20 20 20 20 20 20 69 73 20 74 68 69 73  **       is this
1176d 20 6d 61 6e 79 20 62 79 74 65 73 20 69 6e 20 73   many bytes in s
1176e 69 7a 65 2e 0a 2a 2a 20 20 28 36 29 20 20 34 20  ize..**  (6)  4 
1176f 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
11770 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
11771 20 74 68 65 20 70 61 67 65 20 63 61 73 65 2e 0a   the page case..
11772 2a 2a 20 20 28 37 29 20 20 34 20 62 79 74 65 20  **  (7)  4 byte 
11773 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
11774 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
11775 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73 74  ytes in the mast
11776 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20  er journal.**   
11777 20 20 20 20 6e 61 6d 65 2e 20 20 54 68 65 20 76      name.  The v
11778 61 6c 75 65 20 6d 61 79 20 62 65 20 7a 65 72 6f  alue may be zero
11779 20 28 69 6e 64 69 63 61 74 65 20 74 68 61 74 20   (indicate that 
1177a 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 73 74  there is no mast
1177b 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72  er.**       jour
1177c 6e 61 6c 2e 29 0a 2a 2a 20 20 28 38 29 20 20 4e  nal.).**  (8)  N
1177d 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 6d 61   bytes of the ma
1177e 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
1177f 65 2e 20 20 54 68 65 20 6e 61 6d 65 20 77 69 6c  e.  The name wil
11780 6c 20 62 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  l be nul-termina
11781 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 61 6e 64  ted.**       and
11782 20 6d 69 67 68 74 20 62 65 20 73 68 6f 72 74 65   might be shorte
11783 72 20 74 68 61 6e 20 74 68 65 20 76 61 6c 75 65  r than the value
11784 20 72 65 61 64 20 66 72 6f 6d 20 28 35 29 2e 20   read from (5). 
11785 20 49 66 20 74 68 65 20 66 69 72 73 74 20 62 79   If the first by
11786 74 65 0a 2a 2a 20 20 20 20 20 20 20 6f 66 20 74  te.**       of t
11787 68 65 20 6e 61 6d 65 20 69 73 20 5c 30 30 30 20  he name is \000 
11788 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f  then there is no
11789 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e   master journal.
1178a 20 20 54 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20    The master.** 
1178b 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 6e 61        journal na
1178c 6d 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  me is stored in 
1178d 55 54 46 2d 38 2e 0a 2a 2a 20 20 28 39 29 20 20  UTF-8..**  (9)  
1178e 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67  Zero or more pag
1178f 65 73 20 69 6e 73 74 61 6e 63 65 73 2c 20 65 61  es instances, ea
11790 63 68 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ch as follows:.*
11791 2a 20 20 20 20 20 20 20 20 2b 20 20 34 20 62 79  *        +  4 by
11792 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a  te page number..
11793 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 70 50 61  **        +  pPa
11794 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79  ger->pageSize by
11795 74 65 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20  tes of data..** 
11796 20 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65         +  4 byte
11797 20 63 68 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20   checksum.**.** 
11798 57 68 65 6e 20 77 65 20 73 70 65 61 6b 20 6f 66  When we speak of
11799 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
1179a 64 65 72 2c 20 77 65 20 6d 65 61 6e 20 74 68 65  der, we mean the
1179b 20 66 69 72 73 74 20 38 20 69 74 65 6d 73 20 61   first 8 items a
1179c 62 6f 76 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e  bove..** Each en
1179d 74 72 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  try in the journ
1179e 61 6c 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  al is an instanc
1179f 65 20 6f 66 20 74 68 65 20 39 74 68 20 69 74 65  e of the 9th ite
117a0 6d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68  m..**.** Call th
117a1 65 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65  e value from the
117a2 20 73 65 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22   second bullet "
117a3 6e 52 65 63 22 2e 20 20 6e 52 65 63 20 69 73 20  nRec".  nRec is 
117a4 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
117a5 20 76 61 6c 69 64 20 70 61 67 65 20 65 6e 74 72   valid page entr
117a6 69 65 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ies in the journ
117a7 61 6c 2e 20 20 49 6e 20 6d 6f 73 74 20 63 61 73  al.  In most cas
117a8 65 73 2c 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70  es, you can comp
117a9 75 74 65 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65  ute the.** value
117aa 20 6f 66 20 6e 52 65 63 20 66 72 6f 6d 20 74 68   of nRec from th
117ab 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f  e size of the jo
117ac 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 42 75 74  urnal file.  But
117ad 20 69 66 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66   if a power.** f
117ae 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 20  ailure occurred 
117af 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61  while the journa
117b0 6c 20 77 61 73 20 62 65 69 6e 67 20 77 72 69 74  l was being writ
117b1 74 65 6e 2c 20 69 74 20 63 6f 75 6c 64 20 62 65  ten, it could be
117b2 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61   the.** case tha
117b3 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  t the size of th
117b4 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  e journal file h
117b5 61 64 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ad already been 
117b6 69 6e 63 72 65 61 73 65 64 20 62 75 74 0a 2a 2a  increased but.**
117b7 20 74 68 65 20 65 78 74 72 61 20 65 6e 74 72 69   the extra entri
117b8 65 73 20 68 61 64 20 6e 6f 74 20 79 65 74 20 6d  es had not yet m
117b9 61 64 65 20 69 74 20 73 61 66 65 6c 79 20 74 6f  ade it safely to
117ba 20 64 69 73 6b 2e 20 20 49 6e 20 73 75 63 68 20   disk.  In such 
117bb 61 20 63 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76  a case,.** the v
117bc 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d  alue of nRec com
117bd 70 75 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66  puted from the f
117be 69 6c 65 20 73 69 7a 65 20 77 6f 75 6c 64 20 62  ile size would b
117bf 65 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f  e too large.  Fo
117c0 72 0a 2a 2a 20 74 68 61 74 20 72 65 61 73 6f 6e  r.** that reason
117c1 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20  , we always use 
117c2 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69  the nRec value i
117c3 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a  n the header..**
117c4 0a 2a 2a 20 49 66 20 74 68 65 20 6e 52 65 63 20  .** If the nRec 
117c5 76 61 6c 75 65 20 69 73 20 30 78 66 66 66 66 66  value is 0xfffff
117c6 66 66 66 20 69 74 20 6d 65 61 6e 73 20 74 68 61  fff it means tha
117c7 74 20 6e 52 65 63 20 73 68 6f 75 6c 64 20 62 65  t nRec should be
117c8 20 63 6f 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f   computed.** fro
117c9 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2e  m the file size.
117ca 20 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20    This value is 
117cb 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 75 73  used when the us
117cc 65 72 20 73 65 6c 65 63 74 73 20 74 68 65 0a 2a  er selects the.*
117cd 2a 20 6e 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e  * no-sync option
117ce 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c   for the journal
117cf 2e 20 20 41 20 70 6f 77 65 72 20 66 61 69 6c 75  .  A power failu
117d0 72 65 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f  re could lead to
117d1 20 63 6f 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69   corruption.** i
117d2 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 42 75  n this case.  Bu
117d3 74 20 66 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b  t for things lik
117d4 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
117d5 65 20 28 77 68 69 63 68 20 77 69 6c 6c 20 62 65  e (which will be
117d6 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e  .** deleted when
117d7 20 74 68 65 20 70 6f 77 65 72 20 69 73 20 72 65   the power is re
117d8 73 74 6f 72 65 64 29 20 77 65 20 64 6f 6e 27 74  stored) we don't
117d9 20 63 61 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49   care.  .**.** I
117da 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e 65  f the file opene
117db 64 20 61 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d as the journal
117dc 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 20 77   file is not a w
117dd 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f  ell-formed.** jo
117de 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 6e 20  urnal file then 
117df 61 6c 6c 20 70 61 67 65 73 20 75 70 20 74 6f 20  all pages up to 
117e0 74 68 65 20 66 69 72 73 74 20 63 6f 72 72 75 70  the first corrup
117e1 74 65 64 20 70 61 67 65 20 61 72 65 20 72 6f 6c  ted page are rol
117e2 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20  led.** back (or 
117e3 6e 6f 20 70 61 67 65 73 20 69 66 20 74 68 65 20  no pages if the 
117e4 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69  journal header i
117e5 73 20 63 6f 72 72 75 70 74 65 64 29 2e 20 54 68  s corrupted). Th
117e6 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a  e journal file.*
117e7 2a 20 69 73 20 74 68 65 6e 20 64 65 6c 65 74 65  * is then delete
117e8 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  d and SQLITE_OK 
117e9 72 65 74 75 72 6e 65 64 2c 20 6a 75 73 74 20 61  returned, just a
117ea 73 20 69 66 20 6e 6f 20 63 6f 72 72 75 70 74 69  s if no corrupti
117eb 6f 6e 20 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65  on had.** been e
117ec 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a  ncountered..**.*
117ed 2a 20 49 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d  * If an I/O or m
117ee 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63  alloc() error oc
117ef 63 75 72 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61  curs, the journa
117f0 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74 20 64 65  l-file is not de
117f1 6c 65 74 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20  leted.** and an 
117f2 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
117f3 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
117f4 65 20 69 73 48 6f 74 20 70 61 72 61 6d 65 74 65  e isHot paramete
117f5 72 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  r indicates that
117f6 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74   we are trying t
117f7 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 6a 6f 75  o rollback a jou
117f8 72 6e 61 6c 0a 2a 2a 20 74 68 61 74 20 6d 69 67  rnal.** that mig
117f9 68 74 20 62 65 20 61 20 68 6f 74 20 6a 6f 75 72  ht be a hot jour
117fa 6e 61 6c 2e 20 20 4f 72 2c 20 69 74 20 63 6f 75  nal.  Or, it cou
117fb 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 20 6a  ld be that the j
117fc 6f 75 72 6e 61 6c 20 69 73 20 0a 2a 2a 20 70 72  ournal is .** pr
117fd 65 73 65 72 76 65 64 20 62 65 63 61 75 73 65 20  eserved because 
117fe 6f 66 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50  of JOURNALMODE_P
117ff 45 52 53 49 53 54 20 6f 72 20 4a 4f 55 52 4e 41  ERSIST or JOURNA
11800 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 2e 0a  LMODE_TRUNCATE..
11801 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  ** If the journa
11802 6c 20 72 65 61 6c 6c 79 20 69 73 20 68 6f 74 2c  l really is hot,
11803 20 72 65 73 65 74 20 74 68 65 20 70 61 67 65 72   reset the pager
11804 20 63 61 63 68 65 20 70 72 69 6f 72 20 72 6f 6c   cache prior rol
11805 6c 69 6e 67 0a 2a 2a 20 62 61 63 6b 20 61 6e 79  ling.** back any
11806 20 63 6f 6e 74 65 6e 74 2e 20 20 49 66 20 74 68   content.  If th
11807 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6d 65 72  e journal is mer
11808 65 6c 79 20 70 65 72 73 69 73 74 65 6e 74 2c 20  ely persistent, 
11809 6e 6f 20 72 65 73 65 74 20 69 73 0a 2a 2a 20 6e  no reset is.** n
1180a 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  eeded..*/.static
1180b 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62   int pager_playb
1180c 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65  ack(Pager *pPage
1180d 72 2c 20 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20  r, int isHot){. 
1180e 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
1180f 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66  fs = pPager->pVf
11810 73 3b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20  s;.  i64 szJ;   
11811 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11812 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75   Size of the jou
11813 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74  rnal file in byt
11814 65 73 20 2a 2f 0a 20 20 75 33 32 20 6e 52 65 63  es */.  u32 nRec
11815 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11816 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 52 65   /* Number of Re
11817 63 6f 72 64 73 20 69 6e 20 74 68 65 20 6a 6f 75  cords in the jou
11818 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b  rnal */.  u32 u;
11819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1181a 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c     /* Unsigned l
1181b 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1181c 20 50 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20   Pgno mxPg = 0; 
1181d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
1181e 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  e of the origina
1181f 6c 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20  l file in pages 
11820 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
11821 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11822 20 52 65 73 75 6c 74 20 63 6f 64 65 20 6f 66 20   Result code of 
11823 61 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  a subroutine */.
11824 20 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20    int res = 1;  
11825 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
11826 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
11827 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
11828 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61  ) */.  char *zMa
11829 73 74 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20  ster = 0;       
1182a 2f 2a 20 4e 61 6d 65 20 6f 66 20 6d 61 73 74 65  /* Name of maste
1182b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  r journal file i
1182c 66 20 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  f any */.  int n
1182d 65 65 64 50 61 67 65 72 52 65 73 65 74 3b 20 20  eedPagerReset;  
1182e 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 72      /* True to r
1182f 65 73 65 74 20 70 61 67 65 20 70 72 69 6f 72 20  eset page prior 
11830 74 6f 20 66 69 72 73 74 20 70 61 67 65 20 72 6f  to first page ro
11831 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 2f 2a 20  llback */..  /* 
11832 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  Figure out how m
11833 61 6e 79 20 72 65 63 6f 72 64 73 20 61 72 65 20  any records are 
11834 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  in the journal. 
11835 20 41 62 6f 72 74 20 65 61 72 6c 79 20 69 66 0a   Abort early if.
11836 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c    ** the journal
11837 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a   is empty..  */.
11838 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e    assert( isOpen
11839 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b  (pPager->jfd) );
1183a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
1183b 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72  sFileSize(pPager
1183c 2d 3e 6a 66 64 2c 20 26 73 7a 4a 29 3b 0a 20 20  ->jfd, &szJ);.  
1183d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1183e 4b 20 7c 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a 20  K || szJ==0 ){. 
1183f 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79     goto end_play
11840 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  back;.  }..  /* 
11841 52 65 61 64 20 74 68 65 20 6d 61 73 74 65 72 20  Read the master 
11842 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f  journal name fro
11843 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69  m the journal, i
11844 66 20 69 74 20 69 73 20 70 72 65 73 65 6e 74 2e  f it is present.
11845 0a 20 20 2a 2a 20 49 66 20 61 20 6d 61 73 74 65  .  ** If a maste
11846 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e  r journal file n
11847 61 6d 65 20 69 73 20 73 70 65 63 69 66 69 65 64  ame is specified
11848 2c 20 62 75 74 20 74 68 65 20 66 69 6c 65 20 69  , but the file i
11849 73 20 6e 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65  s not.  ** prese
1184a 6e 74 20 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e  nt on disk, then
1184b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
1184c 6e 6f 74 20 68 6f 74 20 61 6e 64 20 64 6f 65 73  not hot and does
1184d 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 0a   not need to be.
1184e 20 20 2a 2a 20 70 6c 61 79 65 64 20 62 61 63 6b    ** played back
1184f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f  ..  **.  ** TODO
11850 3a 20 54 65 63 68 6e 69 63 61 6c 6c 79 20 74 68  : Technically th
11851 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61  e following is a
11852 6e 20 65 72 72 6f 72 20 62 65 63 61 75 73 65 20  n error because 
11853 69 74 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a  it assumes that.
11854 20 20 2a 2a 20 62 75 66 66 65 72 20 50 61 67 65    ** buffer Page
11855 72 2e 70 54 6d 70 53 70 61 63 65 20 69 73 20 28  r.pTmpSpace is (
11856 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 20 62 79  mxPathname+1) by
11857 74 65 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 69  tes or larger. i
11858 2e 65 2e 20 74 68 61 74 0a 20 20 2a 2a 20 28 70  .e. that.  ** (p
11859 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20  Pager->pageSize 
1185a 3e 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d  >= pPager->pVfs-
1185b 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 2e 20  >mxPathname+1). 
1185c 55 73 69 6e 67 20 6f 73 5f 75 6e 69 78 2e 63 2c  Using os_unix.c,
1185d 0a 20 20 2a 2a 20 20 6d 78 50 61 74 68 6e 61 6d  .  **  mxPathnam
1185e 65 20 69 73 20 35 31 32 2c 20 77 68 69 63 68 20  e is 512, which 
1185f 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
11860 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77  he minimum allow
11861 61 62 6c 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20  able value.  ** 
11862 66 6f 72 20 70 61 67 65 53 69 7a 65 2e 0a 20 20  for pageSize..  
11863 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70  */.  zMaster = p
11864 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
11865 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73  ;.  rc = readMas
11866 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  terJournal(pPage
11867 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c  r->jfd, zMaster,
11868 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d   pPager->pVfs->m
11869 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20  xPathname+1);.  
1186a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1186b 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20  K && zMaster[0] 
1186c 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1186d 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73  te3OsAccess(pVfs
1186e 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54  , zMaster, SQLIT
1186f 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c  E_ACCESS_EXISTS,
11870 20 26 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d   &res);.  }.  zM
11871 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 28  aster = 0;.  if(
11872 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
11873 7c 20 21 72 65 73 20 29 7b 0a 20 20 20 20 67 6f  | !res ){.    go
11874 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b  to end_playback;
11875 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a  .  }.  pPager->j
11876 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20  ournalOff = 0;. 
11877 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20   needPagerReset 
11878 3d 20 69 73 48 6f 74 3b 0a 0a 20 20 2f 2a 20 54  = isHot;..  /* T
11879 68 69 73 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61  his loop termina
1187a 74 65 73 20 65 69 74 68 65 72 20 77 68 65 6e 20  tes either when 
1187b 61 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72  a readJournalHdr
1187c 28 29 20 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65  () or .  ** page
1187d 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
1187e 61 67 65 28 29 20 63 61 6c 6c 20 72 65 74 75 72  age() call retur
1187f 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f  ns SQLITE_DONE o
11880 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 20  r an IO error . 
11881 20 2a 2a 20 6f 63 63 75 72 73 2e 20 0a 20 20 2a   ** occurs. .  *
11882 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a  /.  while( 1 ){.
11883 20 20 20 20 69 6e 74 20 69 73 55 6e 73 79 6e 63      int isUnsync
11884 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 52 65   = 0;..    /* Re
11885 61 64 20 74 68 65 20 6e 65 78 74 20 6a 6f 75 72  ad the next jour
11886 6e 61 6c 20 68 65 61 64 65 72 20 66 72 6f 6d 20  nal header from 
11887 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11888 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 0a  .  If there are.
11889 20 20 20 20 2a 2a 20 6e 6f 74 20 65 6e 6f 75 67      ** not enoug
1188a 68 20 62 79 74 65 73 20 6c 65 66 74 20 69 6e 20  h bytes left in 
1188b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1188c 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 20   for a complete 
1188d 68 65 61 64 65 72 2c 20 6f 72 0a 20 20 20 20 2a  header, or.    *
1188e 2a 20 69 74 20 69 73 20 63 6f 72 72 75 70 74 65  * it is corrupte
1188f 64 2c 20 74 68 65 6e 20 61 20 70 72 6f 63 65 73  d, then a proces
11890 73 20 6d 75 73 74 20 6f 66 20 66 61 69 6c 65 64  s must of failed
11891 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67 20 69   while writing i
11892 74 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69  t..    ** This i
11893 6e 64 69 63 61 74 65 73 20 6e 6f 74 68 69 6e 67  ndicates nothing
11894 20 6d 6f 72 65 20 6e 65 65 64 73 20 74 6f 20 62   more needs to b
11895 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 20  e rolled back.. 
11896 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 72     */.    rc = r
11897 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  eadJournalHdr(pP
11898 61 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 52 65 63  ager, szJ, &nRec
11899 2c 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66  , &mxPg);.    if
1189a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1189b 29 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63  ){ .      if( rc
1189c 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
1189d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1189e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
1189f 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
118a0 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a  playback;.    }.
118a1 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20  .    /* If nRec 
118a2 69 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74  is 0xffffffff, t
118a3 68 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c  hen this journal
118a4 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20   was created by 
118a5 61 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a  a process.    **
118a6 20 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73   working in no-s
118a7 79 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d  ync mode. This m
118a8 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65  eans that the re
118a9 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  st of the journa
118aa 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f  l.    ** file co
118ab 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c  nsists of pages,
118ac 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f   there are no mo
118ad 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  re journal heade
118ae 72 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20  rs. Compute.    
118af 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
118b0 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68  nRec based on th
118b1 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20  is assumption.. 
118b2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52     */.    if( nR
118b3 65 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29  ec==0xffffffff )
118b4 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
118b5 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
118b6 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ff==JOURNAL_HDR_
118b7 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  SZ(pPager) );.  
118b8 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29      nRec = (int)
118b9 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f  ((szJ - JOURNAL_
118ba 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f  HDR_SZ(pPager))/
118bb 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
118bc 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20  ager));.    }.. 
118bd 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73     /* If nRec is
118be 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c   0 and this roll
118bf 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61  back is of a tra
118c0 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64  nsaction created
118c1 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   by this.    ** 
118c2 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74  process and if t
118c3 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c  his is the final
118c4 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a   header in the j
118c5 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20  ournal, then it 
118c6 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61  means.    ** tha
118c7 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74  t this part of t
118c8 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62  he journal was b
118c9 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20  eing filled but 
118ca 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
118cb 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74  .    ** synced t
118cc 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65  o disk.  Compute
118cd 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
118ce 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68  ages based on th
118cf 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20  e remaining.    
118d0 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  ** size of the f
118d1 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
118d2 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72  ** The third ter
118d3 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61  m of the test wa
118d4 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74  s added to fix t
118d5 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20  icket #2565..   
118d6 20 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67   ** When rolling
118d7 20 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72   back a hot jour
118d8 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77  nal, nRec==0 alw
118d9 61 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74  ays means that t
118da 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63  he next.    ** c
118db 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72  hunk of the jour
118dc 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72  nal contains zer
118dd 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f  o pages to be ro
118de 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a  lled back.  But.
118df 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e      ** when doin
118e0 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64  g a ROLLBACK and
118e1 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75   the nRec==0 chu
118e2 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 63  nk is the last c
118e3 68 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74  hunk in.    ** t
118e4 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d  he journal, it m
118e5 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f  eans that the jo
118e6 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74  urnal might cont
118e7 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20  ain additional. 
118e8 20 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74     ** pages that
118e9 20 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c   need to be roll
118ea 65 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74  ed back and that
118eb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
118ec 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f  ages .    ** sho
118ed 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20  uld be computed 
118ee 62 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75  based on the jou
118ef 72 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a  rnal file size..
118f0 20 20 20 20 2a 2f 0a 20 20 20 20 74 65 73 74 63      */.    testc
118f1 61 73 65 28 20 6e 52 65 63 3d 3d 30 20 26 26 20  ase( nRec==0 && 
118f2 21 69 73 48 6f 74 0a 20 20 20 20 20 20 20 20 20  !isHot.         
118f3 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
118f4 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44  alHdr+JOURNAL_HD
118f5 52 5f 53 5a 28 70 50 61 67 65 72 29 21 3d 70 50  R_SZ(pPager)!=pP
118f6 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
118f7 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 28 73  .         && ((s
118f8 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75  zJ - pPager->jou
118f9 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e  rnalOff) / JOURN
118fa 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29  AL_PG_SZ(pPager)
118fb 29 3e 30 0a 20 20 20 20 20 20 20 20 20 26 26 20  )>0.         && 
118fc 70 61 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c  pagerNextJournal
118fd 50 61 67 65 49 73 56 61 6c 69 64 28 70 50 61 67  PageIsValid(pPag
118fe 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  er).    );.    i
118ff 66 28 20 6e 52 65 63 3d 3d 30 20 26 26 20 21 69  f( nRec==0 && !i
11900 73 48 6f 74 20 26 26 0a 20 20 20 20 20 20 20 20  sHot &&.        
11901 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
11902 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  dr+JOURNAL_HDR_S
11903 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65  Z(pPager)==pPage
11904 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b  r->journalOff ){
11905 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20 28 69  .      nRec = (i
11906 6e 74 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65  nt)((szJ - pPage
11907 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f  r->journalOff) /
11908 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70   JOURNAL_PG_SZ(p
11909 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 69  Pager));.      i
1190a 73 55 6e 73 79 6e 63 20 3d 20 31 3b 0a 20 20 20  sUnsync = 1;.   
1190b 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
1190c 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
1190d 68 65 61 64 65 72 20 72 65 61 64 20 66 72 6f 6d  header read from
1190e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 72   the journal, tr
1190f 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20 20 2a  uncate the.    *
11910 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
11911 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72 69 67  back to its orig
11912 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 20 20 2a  inal size..    *
11913 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  /.    if( pPager
11914 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f  ->journalOff==JO
11915 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
11916 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63  ger) ){.      rc
11917 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74   = pager_truncat
11918 65 28 70 50 61 67 65 72 2c 20 6d 78 50 67 29 3b  e(pPager, mxPg);
11919 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1191a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1191b 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
1191c 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  yback;.      }. 
1191d 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53       pPager->dbS
1191e 69 7a 65 20 3d 20 6d 78 50 67 3b 0a 20 20 20 20  ize = mxPg;.    
1191f 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f  }..    /* Copy o
11920 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 6f 75  riginal pages ou
11921 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
11922 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 74   and back into t
11923 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  he .    ** datab
11924 61 73 65 20 66 69 6c 65 20 61 6e 64 2f 6f 72 20  ase file and/or 
11925 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 20 20  page cache..    
11926 2a 2f 0a 20 20 20 20 66 6f 72 28 75 3d 30 3b 20  */.    for(u=0; 
11927 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 7b 0a 20 20  u<nRec; u++){.  
11928 20 20 20 20 69 66 28 20 6e 65 65 64 50 61 67 65      if( needPage
11929 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20  rReset ){.      
1192a 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50    pager_reset(pP
1192b 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 6e  ager);.        n
1192c 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d 20  eedPagerReset = 
1192d 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
1192e 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79   rc = pager_play
1192f 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50  back_one_page(pP
11930 61 67 65 72 2c 31 2c 69 73 55 6e 73 79 6e 63 2c  ager,1,isUnsync,
11931 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  &pPager->journal
11932 4f 66 66 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  Off,0,0);.      
11933 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
11934 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  K ){.        if(
11935 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
11936 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
11937 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
11938 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
11939 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a  journalOff = szJ
1193a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
1193b 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  k;.        }else
1193c 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  {.          /* I
1193d 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20  f we are unable 
1193e 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65  to rollback, the
1193f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
11940 73 20 70 72 6f 62 61 62 6c 79 0a 20 20 20 20 20  s probably.     
11941 20 20 20 20 20 2a 2a 20 67 6f 69 6e 67 20 74 6f       ** going to
11942 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 63 6f   end up being co
11943 72 72 75 70 74 2e 20 20 49 74 20 69 73 20 63 6f  rrupt.  It is co
11944 72 72 75 70 74 20 74 6f 20 75 73 2c 20 61 6e 79  rrupt to us, any
11945 68 6f 77 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  how..          *
11946 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e 65  * Perhaps the ne
11947 78 74 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f  xt process to co
11948 6d 65 20 61 6c 6f 6e 67 20 63 61 6e 20 66 69 78  me along can fix
11949 20 69 74 2e 2e 2e 2e 0a 20 20 20 20 20 20 20 20   it.....        
1194a 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72    */.          r
1194b 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
1194c 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20  PT_BKPT;.       
1194d 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79     goto end_play
1194e 62 61 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  back;.        }.
1194f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
11950 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44  }.  /*NOTREACHED
11951 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 29  */.  assert( 0 )
11952 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3a  ;..end_playback:
11953 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20  .  /* Following 
11954 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20  a rollback, the 
11955 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 68  database file sh
11956 6f 75 6c 64 20 62 65 20 62 61 63 6b 20 69 6e 20  ould be back in 
11957 69 74 73 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a  its original.  *
11958 2a 20 73 74 61 74 65 20 70 72 69 6f 72 20 74 6f  * state prior to
11959 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
1195a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 73  e transaction, s
1195b 6f 20 69 6e 76 6f 6b 65 20 74 68 65 0a 20 20 2a  o invoke the.  *
1195c 2a 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44  * SQLITE_FCNTL_D
1195d 42 5f 55 4e 43 48 41 4e 47 45 44 20 66 69 6c 65  B_UNCHANGED file
1195e 2d 63 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20  -control method 
1195f 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 0a 20  to disable the. 
11960 20 2a 2a 20 61 73 73 65 72 74 69 6f 6e 20 74 68   ** assertion th
11961 61 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  at the transacti
11962 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 20 6d  on counter was m
11963 6f 64 69 66 69 65 64 2e 0a 20 20 2a 2f 0a 20 20  odified..  */.  
11964 61 73 73 65 72 74 28 0a 20 20 20 20 70 50 61 67  assert(.    pPag
11965 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73  er->fd->pMethods
11966 3d 3d 30 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74  ==0 ||.    sqlit
11967 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  e3OsFileControl(
11968 70 50 61 67 65 72 2d 3e 66 64 2c 53 51 4c 49 54  pPager->fd,SQLIT
11969 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41  E_FCNTL_DB_UNCHA
1196a 4e 47 45 44 2c 30 29 3e 3d 53 51 4c 49 54 45 5f  NGED,0)>=SQLITE_
1196b 4f 4b 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  OK.  );..  /* If
1196c 20 74 68 69 73 20 70 6c 61 79 62 61 63 6b 20 69   this playback i
1196d 73 20 68 61 70 70 65 6e 69 6e 67 20 61 75 74 6f  s happening auto
1196e 6d 61 74 69 63 61 6c 6c 79 20 61 73 20 61 20 72  matically as a r
1196f 65 73 75 6c 74 20 6f 66 20 61 6e 20 49 4f 20 6f  esult of an IO o
11970 72 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65  r .  ** malloc e
11971 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 72  rror that occurr
11972 65 64 20 61 66 74 65 72 20 74 68 65 20 63 68 61  ed after the cha
11973 6e 67 65 2d 63 6f 75 6e 74 65 72 20 77 61 73 20  nge-counter was 
11974 75 70 64 61 74 65 64 20 62 75 74 20 0a 20 20 2a  updated but .  *
11975 2a 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61  * before the tra
11976 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 63 6f 6d  nsaction was com
11977 6d 69 74 74 65 64 2c 20 74 68 65 6e 20 74 68 65  mitted, then the
11978 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
11979 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69  .  ** modificati
1197a 6f 6e 20 6d 61 79 20 6a 75 73 74 20 68 61 76 65  on may just have
1197b 20 62 65 65 6e 20 72 65 76 65 72 74 65 64 2e 20   been reverted. 
1197c 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 20  If this happens 
1197d 69 6e 20 65 78 63 6c 75 73 69 76 65 20 0a 20 20  in exclusive .  
1197e 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 73 75  ** mode, then su
1197f 62 73 65 71 75 65 6e 74 20 74 72 61 6e 73 61 63  bsequent transac
11980 74 69 6f 6e 73 20 70 65 72 66 6f 72 6d 65 64 20  tions performed 
11981 62 79 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  by the connectio
11982 6e 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20  n will not.  ** 
11983 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
11984 65 2d 63 6f 75 6e 74 65 72 20 61 74 20 61 6c 6c  e-counter at all
11985 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 64 20  . This may lead 
11986 74 6f 20 63 61 63 68 65 20 69 6e 63 6f 6e 73 69  to cache inconsi
11987 73 74 65 6e 63 79 0a 20 20 2a 2a 20 70 72 6f 62  stency.  ** prob
11988 6c 65 6d 73 20 66 6f 72 20 6f 74 68 65 72 20 70  lems for other p
11989 72 6f 63 65 73 73 65 73 20 61 74 20 73 6f 6d 65  rocesses at some
1198a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75   point in the fu
1198b 74 75 72 65 2e 20 53 6f 2c 20 6a 75 73 74 0a 20  ture. So, just. 
1198c 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 69 73   ** in case this
1198d 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20 63   has happened, c
1198e 6c 65 61 72 20 74 68 65 20 63 68 61 6e 67 65 43  lear the changeC
1198f 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 6e 6f  ountDone flag no
11990 77 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72  w..  */.  pPager
11991 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e  ->changeCountDon
11992 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  e = pPager->temp
11993 46 69 6c 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d  File;..  if( rc=
11994 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11995 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 67    zMaster = pPag
11996 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20  er->pTmpSpace;. 
11997 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74     rc = readMast
11998 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  erJournal(pPager
11999 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20  ->jfd, zMaster, 
1199a 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78  pPager->pVfs->mx
1199b 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 20  Pathname+1);.   
1199c 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
1199d 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a  QLITE_OK );.  }.
1199e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1199f 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  _OK ){.    rc = 
119a0 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
119a1 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d  ction(pPager, zM
119a2 61 73 74 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b  aster[0]!='\0');
119a3 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72  .    testcase( r
119a4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c!=SQLITE_OK );.
119a5 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
119a6 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74  LITE_OK && zMast
119a7 65 72 5b 30 5d 20 26 26 20 72 65 73 20 29 7b 0a  er[0] && res ){.
119a8 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
119a9 77 61 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  was a master jou
119aa 72 6e 61 6c 20 61 6e 64 20 74 68 69 73 20 72 6f  rnal and this ro
119ab 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72  utine will retur
119ac 6e 20 73 75 63 63 65 73 73 2c 0a 20 20 20 20 2a  n success,.    *
119ad 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20 70  * see if it is p
119ae 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74  ossible to delet
119af 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
119b0 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rnal..    */.   
119b1 20 72 63 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d   rc = pager_delm
119b2 61 73 74 65 72 28 70 50 61 67 65 72 2c 20 7a 4d  aster(pPager, zM
119b3 61 73 74 65 72 29 3b 0a 20 20 20 20 74 65 73 74  aster);.    test
119b4 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45  case( rc!=SQLITE
119b5 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  _OK );.  }..  /*
119b6 20 54 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f   The Pager.secto
119b7 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d  rSize variable m
119b8 61 79 20 68 61 76 65 20 62 65 65 6e 20 75 70 64  ay have been upd
119b9 61 74 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69  ated while rolli
119ba 6e 67 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a  ng.  ** back a j
119bb 6f 75 72 6e 61 6c 20 63 72 65 61 74 65 64 20 62  ournal created b
119bc 79 20 61 20 70 72 6f 63 65 73 73 20 77 69 74 68  y a process with
119bd 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 65 63   a different sec
119be 74 6f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61  tor size.  ** va
119bf 6c 75 65 2e 20 52 65 73 65 74 20 69 74 20 74 6f  lue. Reset it to
119c0 20 74 68 65 20 63 6f 72 72 65 63 74 20 76 61 6c   the correct val
119c1 75 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63  ue for this proc
119c2 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53  ess..  */.  setS
119c3 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72  ectorSize(pPager
119c4 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
119c5 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63  }../*.** Playbac
119c6 6b 20 73 61 76 65 70 6f 69 6e 74 20 70 53 61 76  k savepoint pSav
119c7 65 70 6f 69 6e 74 2e 20 4f 72 2c 20 69 66 20 70  epoint. Or, if p
119c8 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c  Savepoint==NULL,
119c9 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 0a 2a   then playback.*
119ca 2a 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73  * the entire mas
119cb 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
119cc 2e 20 54 68 65 20 63 61 73 65 20 70 53 61 76 65  . The case pSave
119cd 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75  point==NULL occu
119ce 72 73 20 77 68 65 6e 20 0a 2a 2a 20 61 20 52 4f  rs when .** a RO
119cf 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e  LLBACK TO comman
119d0 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20  d is invoked on 
119d1 61 20 53 41 56 45 50 4f 49 4e 54 20 74 68 61 74  a SAVEPOINT that
119d2 20 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f   is a transactio
119d3 6e 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e  n .** savepoint.
119d4 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 53 61 76  .**.** When pSav
119d5 65 70 6f 69 6e 74 20 69 73 20 6e 6f 74 20 4e 55  epoint is not NU
119d6 4c 4c 20 28 6d 65 61 6e 69 6e 67 20 61 20 6e 6f  LL (meaning a no
119d7 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61  n-transaction sa
119d8 76 65 70 6f 69 6e 74 20 69 73 20 0a 2a 2a 20 62  vepoint is .** b
119d9 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b  eing rolled back
119da 29 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c  ), then the roll
119db 62 61 63 6b 20 63 6f 6e 73 69 73 74 73 20 6f 66  back consists of
119dc 20 75 70 20 74 6f 20 74 68 72 65 65 20 73 74 61   up to three sta
119dd 67 65 73 2c 0a 2a 2a 20 70 65 72 66 6f 72 6d 65  ges,.** performe
119de 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 73  d in the order s
119df 70 65 63 69 66 69 65 64 3a 0a 2a 2a 0a 2a 2a 20  pecified:.**.** 
119e0 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 70 6c    * Pages are pl
119e1 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74  ayed back from t
119e2 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
119e3 73 74 61 72 74 69 6e 67 20 61 74 20 62 79 74 65  starting at byte
119e4 0a 2a 2a 20 20 20 20 20 6f 66 66 73 65 74 20 50  .**     offset P
119e5 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f  agerSavepoint.iO
119e6 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e  ffset and contin
119e7 75 69 6e 67 20 74 6f 20 0a 2a 2a 20 20 20 20 20  uing to .**     
119e8 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69  PagerSavepoint.i
119e9 48 64 72 4f 66 66 73 65 74 2c 20 6f 72 20 74 6f  HdrOffset, or to
119ea 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
119eb 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  main journal.** 
119ec 20 20 20 20 66 69 6c 65 20 69 66 20 50 61 67 65      file if Page
119ed 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f  rSavepoint.iHdrO
119ee 66 66 73 65 74 20 69 73 20 7a 65 72 6f 2e 0a 2a  ffset is zero..*
119ef 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 50 61 67 65  *.**   * If Page
119f0 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f  rSavepoint.iHdrO
119f1 66 66 73 65 74 20 69 73 20 6e 6f 74 20 7a 65 72  ffset is not zer
119f2 6f 2c 20 74 68 65 6e 20 70 61 67 65 73 20 61 72  o, then pages ar
119f3 65 20 70 6c 61 79 65 64 0a 2a 2a 20 20 20 20 20  e played.**     
119f4 62 61 63 6b 20 73 74 61 72 74 69 6e 67 20 66 72  back starting fr
119f5 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  om the journal h
119f6 65 61 64 65 72 20 69 6d 6d 65 64 69 61 74 65 6c  eader immediatel
119f7 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20  y following .** 
119f8 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69      PagerSavepoi
119f9 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 74 6f  nt.iHdrOffset to
119fa 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
119fb 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  main journal fil
119fc 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67  e..**.**   * Pag
119fd 65 73 20 61 72 65 20 74 68 65 6e 20 70 6c 61 79  es are then play
119fe 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65  ed back from the
119ff 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   sub-journal fil
11a00 65 2c 20 73 74 61 72 74 69 6e 67 0a 2a 2a 20 20  e, starting.**  
11a01 20 20 20 77 69 74 68 20 74 68 65 20 50 61 67 65     with the Page
11a02 72 53 61 76 65 70 6f 69 6e 74 2e 69 53 75 62 52  rSavepoint.iSubR
11a03 65 63 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e  ec and continuin
11a04 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 0a  g to the end of.
11a05 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e  **     the journ
11a06 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  al file..**.** T
11a07 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20 72 6f  hroughout the ro
11a08 6c 6c 62 61 63 6b 20 70 72 6f 63 65 73 73 2c 20  llback process, 
11a09 65 61 63 68 20 74 69 6d 65 20 61 20 70 61 67 65  each time a page
11a0a 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c   is rolled back,
11a0b 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f   the.** correspo
11a0c 6e 64 69 6e 67 20 62 69 74 20 69 73 20 73 65 74  nding bit is set
11a0d 20 69 6e 20 61 20 62 69 74 76 65 63 20 73 74 72   in a bitvec str
11a0e 75 63 74 75 72 65 20 28 76 61 72 69 61 62 6c 65  ucture (variable
11a0f 20 70 44 6f 6e 65 20 69 6e 20 74 68 65 0a 2a 2a   pDone in the.**
11a10 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
11a11 62 65 6c 6f 77 29 2e 20 54 68 69 73 20 69 73 20  below). This is 
11a12 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74  used to ensure t
11a13 68 61 74 20 61 20 70 61 67 65 20 69 73 20 6f 6e  hat a page is on
11a14 6c 79 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63  ly.** rolled bac
11a15 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  k the first time
11a16 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72   it is encounter
11a17 65 64 20 69 6e 20 65 69 74 68 65 72 20 6a 6f 75  ed in either jou
11a18 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  rnal..**.** If p
11a19 53 61 76 65 70 6f 69 6e 74 20 69 73 20 4e 55 4c  Savepoint is NUL
11a1a 4c 2c 20 74 68 65 6e 20 70 61 67 65 73 20 61 72  L, then pages ar
11a1b 65 20 6f 6e 6c 79 20 70 6c 61 79 65 64 20 62 61  e only played ba
11a1c 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ck from the main
11a1d 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
11a1e 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  . There is no ne
11a1f 65 64 20 66 6f 72 20 61 20 62 69 74 76 65 63 20  ed for a bitvec 
11a20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
11a21 0a 2a 2a 20 49 6e 20 65 69 74 68 65 72 20 63 61  .** In either ca
11a22 73 65 2c 20 62 65 66 6f 72 65 20 70 6c 61 79 62  se, before playb
11a23 61 63 6b 20 63 6f 6d 6d 65 6e 63 65 73 20 74 68  ack commences th
11a24 65 20 50 61 67 65 72 2e 64 62 53 69 7a 65 20 76  e Pager.dbSize v
11a25 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 72 65  ariable.** is re
11a26 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65  set to the value
11a27 20 74 68 61 74 20 69 74 20 68 65 6c 64 20 61 74   that it held at
11a28 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
11a29 65 20 73 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20  e savepoint .** 
11a2a 28 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 29  (or transaction)
11a2b 2e 20 4e 6f 20 70 61 67 65 20 77 69 74 68 20 61  . No page with a
11a2c 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 67 72 65   page-number gre
11a2d 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 20 76  ater than this v
11a2e 61 6c 75 65 0a 2a 2a 20 69 73 20 70 6c 61 79 65  alue.** is playe
11a2f 64 20 62 61 63 6b 2e 20 49 66 20 6f 6e 65 20 69  d back. If one i
11a30 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 74  s encountered it
11a31 20 69 73 20 73 69 6d 70 6c 79 20 73 6b 69 70 70   is simply skipp
11a32 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
11a33 74 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53  t pagerPlaybackS
11a34 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a  avepoint(Pager *
11a35 70 50 61 67 65 72 2c 20 50 61 67 65 72 53 61 76  pPager, PagerSav
11a36 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69  epoint *pSavepoi
11a37 6e 74 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20  nt){.  i64 szJ; 
11a38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a39 2f 2a 20 45 66 66 65 63 74 69 76 65 20 73 69 7a  /* Effective siz
11a3a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f  e of the main jo
11a3b 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69  urnal */.  i64 i
11a3c 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20  HdrOff;         
11a3d 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69      /* End of fi
11a3e 72 73 74 20 73 65 67 6d 65 6e 74 20 6f 66 20 6d  rst segment of m
11a3f 61 69 6e 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f  ain-journal reco
11a40 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  rds */.  int rc 
11a41 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
11a42 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
11a43 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 44   */.  Bitvec *pD
11a44 6f 6e 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  one = 0;       /
11a45 2a 20 42 69 74 76 65 63 20 74 6f 20 65 6e 73 75  * Bitvec to ensu
11a46 72 65 20 70 61 67 65 73 20 70 6c 61 79 65 64 20  re pages played 
11a47 62 61 63 6b 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a  back only once *
11a48 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  /..  assert( pPa
11a49 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
11a4a 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 2f  R_SHARED );..  /
11a4b 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 69 74  * Allocate a bit
11a4c 76 65 63 20 74 6f 20 75 73 65 20 74 6f 20 73 74  vec to use to st
11a4d 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 70  ore the set of p
11a4e 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  ages rolled back
11a4f 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70   */.  if( pSavep
11a50 6f 69 6e 74 20 29 7b 0a 20 20 20 20 70 44 6f 6e  oint ){.    pDon
11a51 65 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  e = sqlite3Bitve
11a52 63 43 72 65 61 74 65 28 70 53 61 76 65 70 6f 69  cCreate(pSavepoi
11a53 6e 74 2d 3e 6e 4f 72 69 67 29 3b 0a 20 20 20 20  nt->nOrig);.    
11a54 69 66 28 20 21 70 44 6f 6e 65 20 29 7b 0a 20 20  if( !pDone ){.  
11a55 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
11a56 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
11a57 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
11a58 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 62   database size b
11a59 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c 75 65  ack to the value
11a5a 20 69 74 20 77 61 73 20 62 65 66 6f 72 65 20 74   it was before t
11a5b 68 65 20 73 61 76 65 70 6f 69 6e 74 20 0a 20 20  he savepoint .  
11a5c 2a 2a 20 62 65 69 6e 67 20 72 65 76 65 72 74 65  ** being reverte
11a5d 64 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 20 20  d was opened..  
11a5e 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53  */.  pPager->dbS
11a5f 69 7a 65 20 3d 20 70 53 61 76 65 70 6f 69 6e 74  ize = pSavepoint
11a60 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e   ? pSavepoint->n
11a61 4f 72 69 67 20 3a 20 70 50 61 67 65 72 2d 3e 64  Orig : pPager->d
11a62 62 4f 72 69 67 53 69 7a 65 3b 0a 0a 20 20 2f 2a  bOrigSize;..  /*
11a63 20 55 73 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75   Use pPager->jou
11a64 72 6e 61 6c 4f 66 66 20 61 73 20 74 68 65 20 65  rnalOff as the e
11a65 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66  ffective size of
11a66 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61   the main rollba
11a67 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e  ck.  ** journal.
11a68 20 20 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c    The actual fil
11a69 65 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65  e might be large
11a6a 72 20 74 68 61 6e 20 74 68 69 73 20 69 6e 0a 20  r than this in. 
11a6b 20 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41   ** PAGER_JOURNA
11a6c 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 6f  LMODE_TRUNCATE o
11a6d 72 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  r PAGER_JOURNALM
11a6e 4f 44 45 5f 50 45 52 53 49 53 54 2e 20 20 42 75  ODE_PERSIST.  Bu
11a6f 74 20 61 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20  t anything.  ** 
11a70 70 61 73 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75  past pPager->jou
11a71 72 6e 61 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c  rnalOff is off-l
11a72 69 6d 69 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a  imits to us..  *
11a73 2f 0a 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 72  /.  szJ = pPager
11a74 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20  ->journalOff;.. 
11a75 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 72 6f 6c   /* Begin by rol
11a76 6c 69 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64  ling back record
11a77 73 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20  s from the main 
11a78 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67  journal starting
11a79 20 61 74 0a 20 20 2a 2a 20 50 61 67 65 72 53 61   at.  ** PagerSa
11a7a 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  vepoint.iOffset 
11a7b 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74  and continuing t
11a7c 6f 20 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e  o the next journ
11a7d 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20  al header..  ** 
11a7e 54 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 72  There might be r
11a7f 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 6d 61  ecords in the ma
11a80 69 6e 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20  in journal that 
11a81 68 61 76 65 20 61 20 70 61 67 65 20 6e 75 6d 62  have a page numb
11a82 65 72 0a 20 20 2a 2a 20 67 72 65 61 74 65 72 20  er.  ** greater 
11a83 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74  than the current
11a84 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 28   database size (
11a85 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 20  pPager->dbSize) 
11a86 62 75 74 20 74 68 6f 73 65 0a 20 20 2a 2a 20 77  but those.  ** w
11a87 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 20 61  ill be skipped a
11a88 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 50  utomatically.  P
11a89 61 67 65 73 20 61 72 65 20 61 64 64 65 64 20 74  ages are added t
11a8a 6f 20 70 44 6f 6e 65 20 61 73 20 74 68 65 79 0a  o pDone as they.
11a8b 20 20 2a 2a 20 61 72 65 20 70 6c 61 79 65 64 20    ** are played 
11a8c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  back..  */.  if(
11a8d 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20   pSavepoint ){. 
11a8e 20 20 20 69 48 64 72 4f 66 66 20 3d 20 70 53 61     iHdrOff = pSa
11a8f 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66  vepoint->iHdrOff
11a90 73 65 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74  set ? pSavepoint
11a91 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3a 20 73  ->iHdrOffset : s
11a92 7a 4a 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  zJ;.    pPager->
11a93 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 70 53 61  journalOff = pSa
11a94 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 66 73 65 74  vepoint->iOffset
11a95 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d  ;.    while( rc=
11a96 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50  =SQLITE_OK && pP
11a97 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11a98 3c 69 48 64 72 4f 66 66 20 29 7b 0a 20 20 20 20  <iHdrOff ){.    
11a99 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
11a9a 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70  yback_one_page(p
11a9b 50 61 67 65 72 2c 20 31 2c 20 30 2c 20 26 70 50  Pager, 1, 0, &pP
11a9c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11a9d 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20  , 1, pDone);.   
11a9e 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72   }.    assert( r
11a9f 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
11aa0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
11aa1 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11aa2 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  f = 0;.  }..  /*
11aa3 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e   Continue rollin
11aa4 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 6f  g back records o
11aa5 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  ut of the main j
11aa6 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20  ournal starting 
11aa7 61 74 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73  at.  ** the firs
11aa8 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  t journal header
11aa9 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e   seen and contin
11aaa 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 65  uing until the e
11aab 66 66 65 63 74 69 76 65 20 65 6e 64 0a 20 20 2a  ffective end.  *
11aac 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f  * of the main jo
11aad 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f 6e  urnal file.  Con
11aae 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f 75  tinue to skip ou
11aaf 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73  t-of-range pages
11ab0 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e   and.  ** contin
11ab1 75 65 20 61 64 64 69 6e 67 20 70 61 67 65 73 20  ue adding pages 
11ab2 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20 70  rolled back to p
11ab3 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 69  Done..  */.  whi
11ab4 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
11ab5 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75  K && pPager->jou
11ab6 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20  rnalOff<szJ ){. 
11ab7 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 20     u32 ii;      
11ab8 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
11ab9 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 33 32  unter */.    u32
11aba 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 20   nJRec = 0;     
11abb 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75  /* Number of Jou
11abc 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a  rnal Records */.
11abd 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20      u32 dummy;. 
11abe 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72     rc = readJour
11abf 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 73  nalHdr(pPager, s
11ac0 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d  zJ, &nJRec, &dum
11ac1 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  my);.    assert(
11ac2 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
11ac3 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   );..    /*.    
11ac4 2a 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e  ** The "pPager->
11ac5 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e  journalHdr+JOURN
11ac6 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
11ac7 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  )==pPager->journ
11ac8 61 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65  alOff".    ** te
11ac9 73 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f  st is related to
11aca 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20   ticket #2565.  
11acb 53 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69  See the discussi
11acc 6f 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a  on in the.    **
11acd 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
11ace 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61  ) function for a
11acf 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
11ad0 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
11ad1 20 20 61 73 73 65 72 74 28 20 21 28 6e 4a 52 65    assert( !(nJRe
11ad2 63 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26  c==0.         &&
11ad3 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11ad4 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  Hdr+JOURNAL_HDR_
11ad5 53 5a 28 70 50 61 67 65 72 29 21 3d 70 50 61 67  SZ(pPager)!=pPag
11ad6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20  er->journalOff. 
11ad7 20 20 20 20 20 20 20 20 26 26 20 28 28 73 7a 4a          && ((szJ
11ad8 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   - pPager->journ
11ad9 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c  alOff) / JOURNAL
11ada 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3e  _PG_SZ(pPager))>
11adb 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 61  0.         && pa
11adc 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50 61  gerNextJournalPa
11add 67 65 49 73 56 61 6c 69 64 28 70 50 61 67 65 72  geIsValid(pPager
11ade 29 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66  )).    );.    if
11adf 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20 20  ( nJRec==0 .    
11ae0 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   && pPager->jour
11ae1 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48  nalHdr+JOURNAL_H
11ae2 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70  DR_SZ(pPager)==p
11ae3 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11ae4 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e  f.    ){.      n
11ae5 4a 52 65 63 20 3d 20 28 75 33 32 29 28 28 73 7a  JRec = (u32)((sz
11ae6 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  J - pPager->jour
11ae7 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 4c 5f  nalOff)/JOURNAL_
11ae8 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a  PG_SZ(pPager));.
11ae9 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69      }.    for(ii
11aea 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
11aeb 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63 20 26 26  K && ii<nJRec &&
11aec 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11aed 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 7b 0a  Off<szJ; ii++){.
11aee 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
11aef 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61  _playback_one_pa
11af0 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20 30 2c  ge(pPager, 1, 0,
11af1 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61   &pPager->journa
11af2 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29 3b  lOff, 1, pDone);
11af3 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
11af4 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  t( rc!=SQLITE_DO
11af5 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  NE );.  }.  asse
11af6 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
11af7 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  K || pPager->jou
11af8 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 3b 0a  rnalOff==szJ );.
11af9 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 20  .  /* Finally,  
11afa 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 73 20 66  rollback pages f
11afb 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  rom the sub-jour
11afc 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 61 74 20  nal.  Page that 
11afd 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 76 69 6f  were.  ** previo
11afe 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b  usly rolled back
11aff 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e   out of the main
11b00 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61 72   journal (and ar
11b01 65 20 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e 65  e hence in pDone
11b02 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73  ).  ** will be s
11b03 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d 6f 66 2d  kipped.  Out-of-
11b04 72 61 6e 67 65 20 70 61 67 65 73 20 61 72 65 20  range pages are 
11b05 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e 0a 20 20  also skipped..  
11b06 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f  */.  if( pSavepo
11b07 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 32 20 69  int ){.    u32 i
11b08 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  i;            /*
11b09 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
11b0a 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20  .    i64 offset 
11b0b 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53  = pSavepoint->iS
11b0c 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d  ubRec*(4+pPager-
11b0d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20  >pageSize);.    
11b0e 66 6f 72 28 69 69 3d 70 53 61 76 65 70 6f 69 6e  for(ii=pSavepoin
11b0f 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 63 3d 3d  t->iSubRec; rc==
11b10 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c  SQLITE_OK && ii<
11b11 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b  pPager->nSubRec;
11b12 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73   ii++){.      as
11b13 73 65 72 74 28 20 6f 66 66 73 65 74 3d 3d 69 69  sert( offset==ii
11b14 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65  *(4+pPager->page
11b15 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 20 20 72  Size) );.      r
11b16 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61  c = pager_playba
11b17 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67  ck_one_page(pPag
11b18 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66 66 73 65  er, 0, 0, &offse
11b19 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20  t, 1, pDone);.  
11b1a 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
11b1b 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
11b1c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
11b1d 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
11b1e 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  Done);.  if( rc=
11b1f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11b20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11b21 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a  lOff = szJ;.  }.
11b22 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11b23 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
11b24 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
11b25 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  of in-memory pag
11b26 65 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f  es that are allo
11b27 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  wed..*/.SQLITE_P
11b28 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
11b29 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65  te3PagerSetCache
11b2a 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67  size(Pager *pPag
11b2b 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  er, int mxPage){
11b2c 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
11b2d 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61  SetCachesize(pPa
11b2e 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d 78  ger->pPCache, mx
11b2f 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Page);.}../*.** 
11b30 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73  Adjust the robus
11b31 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61 74  tness of the dat
11b32 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20  abase to damage 
11b33 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65  due to OS crashe
11b34 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61  s.** or power fa
11b35 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67 69  ilures by changi
11b36 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ng the number of
11b37 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77   syncs()s when w
11b38 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f  riting.** the ro
11b39 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20  llback journal. 
11b3a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65   There are three
11b3b 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20   levels:.**.**  
11b3c 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69    OFF       sqli
11b3d 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e  te3OsSync() is n
11b3e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68  ever called.  Th
11b3f 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  is is the defaul
11b40 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
11b41 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20    for temporary 
11b42 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69  and transient fi
11b43 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f  les..**.**    NO
11b44 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72  RMAL    The jour
11b45 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e  nal is synced on
11b46 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73  ce before writes
11b47 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a   begin on the.**
11b48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
11b49 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 69 73  tabase.  This is
11b4a 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61   normally adequa
11b4b 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62  te protection, b
11b4c 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
11b4d 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 65 74     it is theoret
11b4e 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c  ically possible,
11b4f 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c   though very unl
11b50 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20  ikely,.**       
11b51 20 20 20 20 20 20 20 74 68 61 74 20 61 6e 20 69         that an i
11b52 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20  nopertune power 
11b53 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65  failure could le
11b54 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a  ave the journal.
11b55 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
11b56 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 63 68  in a state which
11b57 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d   would cause dam
11b58 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  age to the datab
11b59 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ase.**          
11b5a 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 20 72      when it is r
11b5b 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a  olled back..**.*
11b5c 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54  *    FULL      T
11b5d 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79  he journal is sy
11b5e 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 6f 72  nced twice befor
11b5f 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f  e writes begin o
11b60 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  n the.**        
11b61 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28        database (
11b62 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69  with some additi
11b63 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
11b64 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c   - the nRec fiel
11b65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
11b66 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c    of the journal
11b67 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20   header - being 
11b68 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65  written in betwe
11b69 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20  en the two.**   
11b6a 20 20 20 20 20 20 20 20 20 20 20 73 79 6e 63 73             syncs
11b6b 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d 65  ).  If we assume
11b6c 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a   that writing a.
11b6d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
11b6e 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74  single disk sect
11b6f 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68  or is atomic, th
11b70 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f  en this mode pro
11b71 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  vides.**        
11b72 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 65 20        assurance 
11b73 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c  that the journal
11b74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72   will not be cor
11b75 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a  rupted to the.**
11b76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f                po
11b77 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64  int of causing d
11b78 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74  amage to the dat
11b79 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c  abase during rol
11b7a 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d  lback..**.** Num
11b7b 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f  eric values asso
11b7c 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 73  ciated with thes
11b7d 65 20 73 74 61 74 65 73 20 61 72 65 20 4f 46 46  e states are OFF
11b7e 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a  ==1, NORMAL=2,.*
11b7f 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f  * and FULL=3..*/
11b80 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
11b81 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
11b82 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  AS.SQLITE_PRIVAT
11b83 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
11b84 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65  gerSetSafetyLeve
11b85 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  l(Pager *pPager,
11b86 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20   int level, int 
11b87 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 70  bFullFsync){.  p
11b88 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20  Pager->noSync = 
11b89 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 50   (level==1 || pP
11b8a 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20  ager->tempFile) 
11b8b 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ?1:0;.  pPager->
11b8c 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76 65  fullSync = (leve
11b8d 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 72 2d  l==3 && !pPager-
11b8e 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b  >tempFile) ?1:0;
11b8f 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  .  pPager->sync_
11b90 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46 73  flags = (bFullFs
11b91 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f  ync?SQLITE_SYNC_
11b92 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e 43  FULL:SQLITE_SYNC
11b93 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 20  _NORMAL);.  if( 
11b94 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29  pPager->noSync )
11b95 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
11b96 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  c = 0;.}.#endif.
11b97 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
11b98 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69  wing global vari
11b99 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e  able is incremen
11b9a 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65  ted whenever the
11b9b 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 65   library.** atte
11b9c 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 74  mpts to open a t
11b9d 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20  emporary file.  
11b9e 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
11b9f 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20   is used for.** 
11ba0 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c  testing and anal
11ba1 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a  ysis only.  .*/.
11ba2 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
11ba3 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
11ba4 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65  t sqlite3_opente
11ba5 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  mp_count = 0;.#e
11ba6 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  ndif../*.** Open
11ba7 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
11ba8 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74  e..**.** Write t
11ba9 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
11baa 6f 72 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e 20  or into *pFile. 
11bab 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
11bac 20 6f 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a 20   on success .** 
11bad 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72  or some other er
11bae 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66  ror code if we f
11baf 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c  ail. The OS will
11bb0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a   automatically .
11bb1 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 74 65  ** delete the te
11bb2 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 77 68 65  mporary file whe
11bb3 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a  n it is closed..
11bb4 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20  **.** The flags 
11bb5 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 56 46  passed to the VF
11bb6 53 20 6c 61 79 65 72 20 78 4f 70 65 6e 28 29 20  S layer xOpen() 
11bb7 63 61 6c 6c 20 61 72 65 20 74 68 6f 73 65 20 73  call are those s
11bb8 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 70  pecified.** by p
11bb9 61 72 61 6d 65 74 65 72 20 76 66 73 46 6c 61 67  arameter vfsFlag
11bba 73 20 4f 52 65 64 20 77 69 74 68 20 74 68 65 20  s ORed with the 
11bbb 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
11bbc 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
11bbd 5f 52 45 41 44 57 52 49 54 45 0a 2a 2a 20 20 20  _READWRITE.**   
11bbe 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52    SQLITE_OPEN_CR
11bbf 45 41 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49  EATE.**     SQLI
11bc0 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
11bc1 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f  E.**     SQLITE_
11bc2 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
11bc3 53 45 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  SE.*/.static int
11bc4 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 0a   pagerOpentemp(.
11bc5 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c    Pager *pPager,
11bc6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
11bc7 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ager object */. 
11bc8 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
11bc9 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 20  File,  /* Write 
11bca 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
11bcb 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  tor here */.  in
11bcc 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 20  t vfsFlags      
11bcd 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73      /* Flags pas
11bce 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74  sed through to t
11bcf 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69  he VFS */.){.  i
11bd0 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
11bd1 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
11bd2 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  ode */..#ifdef S
11bd3 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c  QLITE_TEST.  sql
11bd4 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f  ite3_opentemp_co
11bd5 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 20  unt++;  /* Used 
11bd6 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20  for testing and 
11bd7 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a 2f  analysis only */
11bd8 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 46 6c  .#endif..  vfsFl
11bd9 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f  ags |=  SQLITE_O
11bda 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20  PEN_READWRITE | 
11bdb 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
11bdc 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20  TE |.           
11bdd 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
11bde 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45 5f  LUSIVE | SQLITE_
11bdf 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
11be0 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  SE;.  rc = sqlit
11be1 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  e3OsOpen(pPager-
11be2 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c  >pVfs, 0, pFile,
11be3 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a 20   vfsFlags, 0);. 
11be4 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
11be5 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e  ITE_OK || isOpen
11be6 28 70 46 69 6c 65 29 20 29 3b 0a 20 20 72 65 74  (pFile) );.  ret
11be7 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
11be8 20 53 65 74 20 74 68 65 20 62 75 73 79 20 68 61   Set the busy ha
11be9 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0a  ndler function..
11bea 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  **.** The pager 
11beb 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73 79  invokes the busy
11bec 2d 68 61 6e 64 6c 65 72 20 69 66 20 73 71 6c 69  -handler if sqli
11bed 74 65 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74 75  te3OsLock() retu
11bee 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 42  rns .** SQLITE_B
11bef 55 53 59 20 77 68 65 6e 20 74 72 79 69 6e 67 20  USY when trying 
11bf0 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20  to upgrade from 
11bf1 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48 41  no-lock to a SHA
11bf2 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72 20  RED lock,.** or 
11bf3 77 68 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 75  when trying to u
11bf4 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45  pgrade from a RE
11bf5 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61  SERVED lock to a
11bf6 6e 20 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a 20  n EXCLUSIVE .** 
11bf7 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a 6e  lock. It does *n
11bf8 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62  ot* invoke the b
11bf9 75 73 79 20 68 61 6e 64 6c 65 72 20 77 68 65 6e  usy handler when
11bfa 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 0a   upgrading from.
11bfb 2a 2a 20 53 48 41 52 45 44 20 74 6f 20 52 45 53  ** SHARED to RES
11bfc 45 52 56 45 44 2c 20 6f 72 20 77 68 65 6e 20 75  ERVED, or when u
11bfd 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 20 53 48  pgrading from SH
11bfe 41 52 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56  ARED to EXCLUSIV
11bff 45 0a 2a 2a 20 28 77 68 69 63 68 20 6f 63 63 75  E.** (which occu
11c00 72 73 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f  rs during hot-jo
11c01 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e  urnal rollback).
11c02 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a 20   Summary:.**.** 
11c03 20 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 20    Transition    
11c04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c05 20 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 42      | Invokes xB
11c06 75 73 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20 20  usyHandler.**   
11c07 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11c08 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11c09 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11c0a 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e 4f  --------.**   NO
11c0b 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20 53  _LOCK       -> S
11c0c 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 20  HARED_LOCK      
11c0d 7c 20 59 65 73 0a 2a 2a 20 20 20 53 48 41 52 45  | Yes.**   SHARE
11c0e 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53 45  D_LOCK   -> RESE
11c0f 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20 4e  RVED_LOCK    | N
11c10 6f 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f  o.**   SHARED_LO
11c11 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55 53 49 56  CK   -> EXCLUSIV
11c12 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a 2a  E_LOCK   | No.**
11c13 20 20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b     RESERVED_LOCK
11c14 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f   -> EXCLUSIVE_LO
11c15 43 4b 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a 2a  CK   | Yes.**.**
11c16 20 49 66 20 74 68 65 20 62 75 73 79 2d 68 61 6e   If the busy-han
11c17 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65  dler callback re
11c18 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  turns non-zero, 
11c19 74 68 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20  the lock is .** 
11c1a 72 65 74 72 69 65 64 2e 20 49 66 20 69 74 20 72  retried. If it r
11c1b 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65  eturns zero, the
11c1c 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55 53  n the SQLITE_BUS
11c1d 59 20 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72 65  Y error is.** re
11c1e 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61  turned to the ca
11c1f 6c 6c 65 72 20 6f 66 20 74 68 65 20 70 61 67 65  ller of the page
11c20 72 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a  r API function..
11c21 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11c22 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
11c23 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65  gerSetBusyhandle
11c24 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  r(.  Pager *pPag
11c25 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
11c26 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
11c27 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69  er object */.  i
11c28 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65  nt (*xBusyHandle
11c29 72 29 28 76 6f 69 64 20 2a 29 2c 20 20 20 20 20  r)(void *),     
11c2a 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
11c2b 6f 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66  o busy-handler f
11c2c 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
11c2d 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41  d *pBusyHandlerA
11c2e 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
11c2f 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
11c30 20 70 61 73 73 20 74 6f 20 78 42 75 73 79 48 61   pass to xBusyHa
11c31 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 20 0a 20 20  ndler */.){  .  
11c32 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e  pPager->xBusyHan
11c33 64 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e 64  dler = xBusyHand
11c34 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70  ler;.  pPager->p
11c35 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d  BusyHandlerArg =
11c36 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67   pBusyHandlerArg
11c37 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
11c38 68 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 72  he reinitializer
11c39 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e   for this pager.
11c3a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   If not NULL, th
11c3b 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 72 0a  e reinitializer.
11c3c 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  ** is called whe
11c3d 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  n the content of
11c3e 20 61 20 70 61 67 65 20 69 6e 20 63 61 63 68 65   a page in cache
11c3f 20 69 73 20 6d 6f 64 69 66 69 65 64 20 28 72 65   is modified (re
11c40 73 74 6f 72 65 64 29 0a 2a 2a 20 61 73 20 70 61  stored).** as pa
11c41 72 74 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74  rt of a transact
11c42 69 6f 6e 20 6f 72 20 73 61 76 65 70 6f 69 6e 74  ion or savepoint
11c43 20 72 6f 6c 6c 62 61 63 6b 2e 20 54 68 65 20 63   rollback. The c
11c44 61 6c 6c 62 61 63 6b 20 67 69 76 65 73 20 0a 2a  allback gives .*
11c45 2a 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 63  * higher-level c
11c46 6f 64 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69  ode an opportuni
11c47 74 79 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68  ty to restore th
11c48 65 20 45 58 54 52 41 20 73 65 63 74 69 6f 6e 20  e EXTRA section 
11c49 74 6f 20 0a 2a 2a 20 61 67 72 65 65 20 77 69 74  to .** agree wit
11c4a 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 70  h the restored p
11c4b 61 67 65 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c  age data..*/.SQL
11c4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
11c4d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
11c4e 52 65 69 6e 69 74 65 72 28 50 61 67 65 72 20 2a  Reiniter(Pager *
11c4f 70 50 61 67 65 72 2c 20 76 6f 69 64 20 28 2a 78  pPager, void (*x
11c50 52 65 69 6e 69 74 29 28 44 62 50 61 67 65 2a 29  Reinit)(DbPage*)
11c51 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65  ){.  pPager->xRe
11c52 69 6e 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74  initer = xReinit
11c53 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
11c54 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  e the page size 
11c55 75 73 65 64 20 62 79 20 74 68 65 20 50 61 67 65  used by the Page
11c56 72 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 65  r object. The ne
11c57 77 20 70 61 67 65 20 73 69 7a 65 20 0a 2a 2a 20  w page size .** 
11c58 69 73 20 70 61 73 73 65 64 20 69 6e 20 2a 70 50  is passed in *pP
11c59 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49  ageSize..**.** I
11c5a 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  f the pager is i
11c5b 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  n the error stat
11c5c 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  e when this func
11c5d 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20  tion is called, 
11c5e 69 74 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70  it.** is a no-op
11c5f 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  . The value retu
11c60 72 6e 65 64 20 69 73 20 74 68 65 20 65 72 72 6f  rned is the erro
11c61 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f  r state error co
11c62 64 65 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e 65  de (i.e. .** one
11c63 20 6f 66 20 53 51 4c 49 54 45 5f 49 4f 45 52 52   of SQLITE_IOERR
11c64 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  , SQLITE_CORRUPT
11c65 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29   or SQLITE_FULL)
11c66 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
11c67 65 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65  e, if all of the
11c68 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74   following are t
11c69 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74  rue:.**.**   * t
11c6a 68 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65  he new page size
11c6b 20 28 76 61 6c 75 65 20 6f 66 20 2a 70 50 61 67   (value of *pPag
11c6c 65 53 69 7a 65 29 20 69 73 20 76 61 6c 69 64 20  eSize) is valid 
11c6d 28 61 20 70 6f 77 65 72 20 0a 2a 2a 20 20 20 20  (a power .**    
11c6e 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20   of two between 
11c6f 35 31 32 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d  512 and SQLITE_M
11c70 41 58 5f 50 41 47 45 5f 53 49 5a 45 2c 20 69 6e  AX_PAGE_SIZE, in
11c71 63 6c 75 73 69 76 65 29 2c 20 61 6e 64 0a 2a 2a  clusive), and.**
11c72 0a 2a 2a 20 20 20 2a 20 74 68 65 72 65 20 61 72  .**   * there ar
11c73 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67  e no outstanding
11c74 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73   page references
11c75 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  , and.**.**   * 
11c76 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
11c77 65 69 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e  either not an in
11c78 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
11c79 20 6f 72 20 69 74 20 69 73 0a 2a 2a 20 20 20 20   or it is.**    
11c7a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
11c7b 74 61 62 61 73 65 20 74 68 61 74 20 63 75 72 72  tabase that curr
11c7c 65 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73 20 6f  ently consists o
11c7d 66 20 7a 65 72 6f 20 70 61 67 65 73 2e 0a 2a 2a  f zero pages..**
11c7e 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67  .** then the pag
11c7f 65 72 20 6f 62 6a 65 63 74 20 70 61 67 65 20 73  er object page s
11c80 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 2a 70  ize is set to *p
11c81 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20  PageSize..**.** 
11c82 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  If the page size
11c83 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65   is changed, the
11c84 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
11c85 75 73 65 73 20 73 71 6c 69 74 65 33 50 61 67 65  uses sqlite3Page
11c86 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 6f  rMalloc() .** to
11c87 20 6f 62 74 61 69 6e 20 61 20 6e 65 77 20 50 61   obtain a new Pa
11c88 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75  ger.pTmpSpace bu
11c89 66 66 65 72 2e 20 49 66 20 74 68 69 73 20 61 6c  ffer. If this al
11c8a 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74  location attempt
11c8b 20 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49   .** fails, SQLI
11c8c 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75  TE_NOMEM is retu
11c8d 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 67  rned and the pag
11c8e 65 20 73 69 7a 65 20 72 65 6d 61 69 6e 73 20 75  e size remains u
11c8f 6e 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 6e  nchanged. .** In
11c90 20 61 6c 6c 20 6f 74 68 65 72 20 63 61 73 65 73   all other cases
11c91 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
11c92 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
11c93 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  f the page size 
11c94 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2c 20  is not changed, 
11c95 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20 6f  either because o
11c96 6e 65 20 6f 66 20 74 68 65 20 65 6e 75 6d 65 72  ne of the enumer
11c97 61 74 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f  ated.** conditio
11c98 6e 73 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20  ns above is not 
11c99 74 72 75 65 2c 20 74 68 65 20 70 61 67 65 72 20  true, the pager 
11c9a 77 61 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61  was in error sta
11c9b 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20  te when this.** 
11c9c 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c  function was cal
11c9d 6c 65 64 2c 20 6f 72 20 62 65 63 61 75 73 65 20  led, or because 
11c9e 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
11c9f 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 66 61  ation attempt fa
11ca0 69 6c 65 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 2a  iled, .** then *
11ca1 70 50 61 67 65 53 69 7a 65 20 69 73 20 73 65 74  pPageSize is set
11ca2 20 74 6f 20 74 68 65 20 6f 6c 64 2c 20 72 65 74   to the old, ret
11ca3 61 69 6e 65 64 20 70 61 67 65 20 73 69 7a 65 20  ained page size 
11ca4 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
11ca5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11ca6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
11ca7 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28  agerSetPagesize(
11ca8 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 75  Pager *pPager, u
11ca9 31 36 20 2a 70 50 61 67 65 53 69 7a 65 29 7b 0a  16 *pPageSize){.
11caa 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 65    int rc = pPage
11cab 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 69 66  r->errCode;.  if
11cac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
11cad 29 7b 0a 20 20 20 20 75 31 36 20 70 61 67 65 53  ){.    u16 pageS
11cae 69 7a 65 20 3d 20 2a 70 50 61 67 65 53 69 7a 65  ize = *pPageSize
11caf 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61  ;.    assert( pa
11cb0 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 61  geSize==0 || (pa
11cb1 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70  geSize>=512 && p
11cb2 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f  ageSize<=SQLITE_
11cb3 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 20 29  MAX_PAGE_SIZE) )
11cb4 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 69  ;.    if( pageSi
11cb5 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 3d  ze && pageSize!=
11cb6 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11cb7 20 0a 20 20 20 20 20 26 26 20 28 70 50 61 67 65   .     && (pPage
11cb8 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 70  r->memDb==0 || p
11cb9 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30  Pager->dbSize==0
11cba 29 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65  ).     && sqlite
11cbb 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
11cbc 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
11cbd 3d 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  ==0 .    ){.    
11cbe 20 20 63 68 61 72 20 2a 70 4e 65 77 20 3d 20 28    char *pNew = (
11cbf 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61  char *)sqlite3Pa
11cc0 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 7a  geMalloc(pageSiz
11cc1 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  e);.      if( !p
11cc2 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72  New ){.        r
11cc3 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
11cc4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
11cc5 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73         pager_res
11cc6 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  et(pPager);.    
11cc7 20 20 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65      pPager->page
11cc8 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b  Size = pageSize;
11cc9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
11cca 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 2d  PageFree(pPager-
11ccb 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20  >pTmpSpace);.   
11ccc 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d       pPager->pTm
11ccd 70 53 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20  pSpace = pNew;. 
11cce 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63         sqlite3Pc
11ccf 61 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 28  acheSetPageSize(
11cd0 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c  pPager->pPCache,
11cd1 20 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20   pageSize);.    
11cd2 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70    }.    }.    *p
11cd3 50 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29  PageSize = (u16)
11cd4 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11cd5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
11cd6 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
11cd7 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
11cd8 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79 20 70  the "temporary p
11cd9 61 67 65 22 20 62 75 66 66 65 72 20 68 65 6c 64  age" buffer held
11cda 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62   internally.** b
11cdb 79 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68  y the pager.  Th
11cdc 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 74  is is a buffer t
11cdd 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67  hat is big enoug
11cde 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a  h to hold the.**
11cdf 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 20   entire content 
11ce0 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61  of a database pa
11ce1 67 65 2e 20 20 54 68 69 73 20 62 75 66 66 65 72  ge.  This buffer
11ce2 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61   is used interna
11ce3 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f  lly.** during ro
11ce4 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20  llback and will 
11ce5 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77  be overwritten w
11ce6 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61  henever a rollba
11ce7 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42  ck.** occurs.  B
11ce8 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73  ut other modules
11ce9 20 61 72 65 20 66 72 65 65 20 74 6f 20 75 73 65   are free to use
11cea 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67   it too, as long
11ceb 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61   as.** no rollba
11cec 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e  cks are happenin
11ced 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
11cee 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
11cef 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65  e3PagerTempSpace
11cf0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
11cf1 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72  .  return pPager
11cf2 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a  ->pTmpSpace;.}..
11cf3 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
11cf4 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d   set the maximum
11cf5 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63   database page c
11cf6 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69  ount if mxPage i
11cf7 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20  s positive. .** 
11cf8 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20  Make no changes 
11cf9 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72  if mxPage is zer
11cfa 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20  o or negative.  
11cfb 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75 63 65  And never reduce
11cfc 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20   the.** maximum 
11cfd 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77  page count below
11cfe 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a   the current siz
11cff 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
11d00 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c  e..**.** Regardl
11d01 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72  ess of mxPage, r
11d02 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e  eturn the curren
11d03 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  t maximum page c
11d04 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ount..*/.SQLITE_
11d05 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
11d06 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43  te3PagerMaxPageC
11d07 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  ount(Pager *pPag
11d08 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b  er, int mxPage){
11d09 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20  .  if( mxPage>0 
11d0a 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d  ){.    pPager->m
11d0b 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a  xPgno = mxPage;.
11d0c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67    }.  sqlite3Pag
11d0d 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
11d0e 65 72 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  er, 0);.  return
11d0f 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b   pPager->mxPgno;
11d10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
11d11 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72  llowing set of r
11d12 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
11d13 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20   to disable the 
11d14 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f  simulated.** I/O
11d15 20 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d   error mechanism
11d16 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
11d17 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 76  s are used to av
11d18 6f 69 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a  oid simulated.**
11d19 20 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65   errors in place
11d1a 73 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f  s where we do no
11d1b 74 20 63 61 72 65 20 61 62 6f 75 74 20 65 72 72  t care about err
11d1c 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73  ors..**.** Unles
11d1d 73 20 2d 44 53 51 4c 49 54 45 5f 54 45 53 54 3d  s -DSQLITE_TEST=
11d1e 31 20 69 73 20 75 73 65 64 2c 20 74 68 65 73 65  1 is used, these
11d1f 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c   routines are al
11d20 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20  l no-ops.** and 
11d21 67 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64 65  generate no code
11d22 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
11d23 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
11d24 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71  PI extern int sq
11d25 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
11d26 65 6e 64 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41  ending;.SQLITE_A
11d27 50 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71  PI extern int sq
11d28 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
11d29 69 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73  it;.static int s
11d2a 61 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64  aved_cnt;.void d
11d2b 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  isable_simulated
11d2c 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29  _io_errors(void)
11d2d 7b 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20  {.  saved_cnt = 
11d2e 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
11d2f 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69  _pending;.  sqli
11d30 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
11d31 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69  ding = -1;.}.voi
11d32 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74  d enable_simulat
11d33 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69  ed_io_errors(voi
11d34 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  d){.  sqlite3_io
11d35 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d  _error_pending =
11d36 20 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65   saved_cnt;.}.#e
11d37 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73  lse.# define dis
11d38 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
11d39 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66  o_errors().# def
11d3a 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c  ine enable_simul
11d3b 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
11d3c 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
11d3d 65 61 64 20 74 68 65 20 66 69 72 73 74 20 4e 20  ead the first N 
11d3e 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62  bytes from the b
11d3f 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
11d40 66 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  file into memory
11d41 0a 2a 2a 20 74 68 61 74 20 70 44 65 73 74 20 70  .** that pDest p
11d42 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a  oints to. .**.**
11d43 20 49 66 20 74 68 65 20 70 61 67 65 72 20 77 61   If the pager wa
11d44 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72  s opened on a tr
11d45 61 6e 73 69 65 6e 74 20 66 69 6c 65 20 28 7a 46  ansient file (zF
11d46 69 6c 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72  ilename==""), or
11d47 0a 2a 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20  .** opened on a 
11d48 66 69 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 4e  file less than N
11d49 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20   bytes in size, 
11d4a 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
11d4b 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61  r is.** zeroed a
11d4c 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
11d4d 75 72 6e 65 64 2e 20 54 68 65 20 72 61 74 69 6f  urned. The ratio
11d4e 6e 61 6c 65 20 66 6f 72 20 74 68 69 73 20 69 73  nale for this is
11d4f 20 74 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 66   that this .** f
11d50 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
11d51 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65  to read database
11d52 20 68 65 61 64 65 72 73 2c 20 61 6e 64 20 61 20   headers, and a 
11d53 6e 65 77 20 74 72 61 6e 73 69 65 6e 74 20 6f 72  new transient or
11d54 0a 2a 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 64  .** zero sized d
11d55 61 74 61 62 61 73 65 20 68 61 73 20 61 20 68 65  atabase has a he
11d56 61 64 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 73  ader than consis
11d57 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a  ts entirely of z
11d58 65 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  eroes..**.** If 
11d59 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61  any IO error apa
11d5a 72 74 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49  rt from SQLITE_I
11d5b 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20  OERR_SHORT_READ 
11d5c 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a  is encountered,.
11d5d 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ** the error cod
11d5e 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f  e is returned to
11d5f 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20   the caller and 
11d60 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
11d61 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75  the.** output bu
11d62 66 66 65 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a  ffer undefined..
11d63 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11d64 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
11d65 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72  erReadFileheader
11d66 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
11d67 69 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20  int N, unsigned 
11d68 63 68 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20  char *pDest){.  
11d69 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
11d6a 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65  OK;.  memset(pDe
11d6b 73 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73  st, 0, N);.  ass
11d6c 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
11d6d 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65  er->fd) || pPage
11d6e 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20  r->tempFile );. 
11d6f 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
11d70 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 49  er->fd) ){.    I
11d71 4f 54 52 41 43 45 28 28 22 44 42 48 44 52 20 25  OTRACE(("DBHDR %
11d72 70 20 30 20 25 64 5c 6e 22 2c 20 70 50 61 67 65  p 0 %d\n", pPage
11d73 72 2c 20 4e 29 29 0a 20 20 20 20 72 63 20 3d 20  r, N)).    rc = 
11d74 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50  sqlite3OsRead(pP
11d75 61 67 65 72 2d 3e 66 64 2c 20 70 44 65 73 74 2c  ager->fd, pDest,
11d76 20 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   N, 0);.    if( 
11d77 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
11d78 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20  _SHORT_READ ){. 
11d79 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
11d7a 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
11d7b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
11d7c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
11d7d 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
11d7e 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
11d7f 61 62 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63  abase file assoc
11d80 69 61 74 65 64 20 0a 2a 2a 20 77 69 74 68 20 70  iated .** with p
11d81 50 61 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c  Pager. Normally,
11d82 20 74 68 69 73 20 69 73 20 63 61 6c 63 75 6c 61   this is calcula
11d83 74 65 64 20 61 73 20 28 3c 64 62 20 66 69 6c 65  ted as (<db file
11d84 20 73 69 7a 65 3e 2f 3c 70 61 67 65 2d 73 69 7a   size>/<page-siz
11d85 65 3e 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  e>)..** However,
11d86 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   if the file is 
11d87 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 3c 70  between 1 and <p
11d88 61 67 65 2d 73 69 7a 65 3e 20 62 79 74 65 73 20  age-size> bytes 
11d89 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a  in size, then .*
11d8a 2a 20 74 68 69 73 20 69 73 20 63 6f 6e 73 69 64  * this is consid
11d8b 65 72 65 64 20 61 20 31 20 70 61 67 65 20 66 69  ered a 1 page fi
11d8c 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le..**.** If the
11d8d 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72   pager is in err
11d8e 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  or state when th
11d8f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
11d90 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a  alled, then the.
11d91 2a 2a 20 65 72 72 6f 72 20 73 74 61 74 65 20 65  ** error state e
11d92 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
11d93 75 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67  urned and *pnPag
11d94 65 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64  e left unchanged
11d95 2e 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20  . Or,.** if the 
11d96 66 69 6c 65 20 73 79 73 74 65 6d 20 68 61 73 20  file system has 
11d97 74 6f 20 62 65 20 71 75 65 72 69 65 64 20 66 6f  to be queried fo
11d98 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  r the size of th
11d99 65 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68  e file and.** th
11d9a 65 20 71 75 65 72 79 20 61 74 74 65 6d 70 74 20  e query attempt 
11d9b 72 65 74 75 72 6e 73 20 61 6e 20 49 4f 20 65 72  returns an IO er
11d9c 72 6f 72 2c 20 74 68 65 20 49 4f 20 65 72 72 6f  ror, the IO erro
11d9d 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
11d9e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67  ed.** and *pnPag
11d9f 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e  e is left unchan
11da0 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72  ged..**.** Other
11da1 77 69 73 65 2c 20 69 66 20 65 76 65 72 79 74 68  wise, if everyth
11da2 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75  ing is successfu
11da3 6c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f  l, then SQLITE_O
11da4 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a  K is returned.**
11da5 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20   and *pnPage is 
11da6 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
11da7 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
11da8 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53  e database..*/.S
11da9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
11daa 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  t sqlite3PagerPa
11dab 67 65 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70  gecount(Pager *p
11dac 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61  Pager, int *pnPa
11dad 67 65 29 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67  ge){.  Pgno nPag
11dae 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
11daf 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74   /* Value to ret
11db0 75 72 6e 20 76 69 61 20 2a 70 6e 50 61 67 65 20  urn via *pnPage 
11db1 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  */..  /* If the 
11db2 70 61 67 65 72 20 69 73 20 61 6c 72 65 61 64 79  pager is already
11db3 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74   in the error st
11db4 61 74 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  ate, return the 
11db5 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20  error code. */. 
11db6 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
11db7 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75  Code ){.    retu
11db8 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  rn pPager->errCo
11db9 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65  de;.  }..  /* De
11dba 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62  termine the numb
11dbb 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
11dbc 68 65 20 66 69 6c 65 2e 20 53 74 6f 72 65 20 74  he file. Store t
11dbd 68 69 73 20 69 6e 20 6e 50 61 67 65 2e 20 2a 2f  his in nPage. */
11dbe 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64  .  if( pPager->d
11dbf 62 53 69 7a 65 56 61 6c 69 64 20 29 7b 0a 20 20  bSizeValid ){.  
11dc0 20 20 6e 50 61 67 65 20 3d 20 70 50 61 67 65 72    nPage = pPager
11dc1 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 65 6c 73  ->dbSize;.  }els
11dc2 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20  e{.    int rc;  
11dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11dc4 2a 20 45 72 72 6f 72 20 72 65 74 75 72 6e 65 64  * Error returned
11dc5 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29   by OsFileSize()
11dc6 20 2a 2f 0a 20 20 20 20 69 36 34 20 6e 20 3d 20   */.    i64 n = 
11dc7 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
11dc8 2f 2a 20 46 69 6c 65 20 73 69 7a 65 20 69 6e 20  /* File size in 
11dc9 62 79 74 65 73 20 72 65 74 75 72 6e 65 64 20 62  bytes returned b
11dca 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a  y OsFileSize() *
11dcb 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  /..    assert( i
11dcc 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
11dcd 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d  ) || pPager->tem
11dce 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28  pFile );.    if(
11dcf 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
11dd0 66 64 29 20 26 26 20 28 30 20 21 3d 20 28 72 63  fd) && (0 != (rc
11dd1 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
11dd2 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c  Size(pPager->fd,
11dd3 20 26 6e 29 29 29 20 29 7b 0a 20 20 20 20 20 20   &n))) ){.      
11dd4 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
11dd5 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 72  er, rc);.      r
11dd6 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
11dd7 20 20 20 20 69 66 28 20 6e 3e 30 20 26 26 20 6e      if( n>0 && n
11dd8 3c 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  <pPager->pageSiz
11dd9 65 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65  e ){.      nPage
11dda 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 1;.    }else{
11ddb 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28  .      nPage = (
11ddc 50 67 6e 6f 29 28 6e 20 2f 20 70 50 61 67 65 72  Pgno)(n / pPager
11ddd 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
11dde 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65   }.    if( pPage
11ddf 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f  r->state!=PAGER_
11de0 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20  UNLOCK ){.      
11de1 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
11de2 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50   nPage;.      pP
11de3 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
11de4 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20   = nPage;.      
11de5 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
11de6 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  lid = 1;.    }. 
11de7 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
11de8 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f  current number o
11de9 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66  f pages in the f
11dea 69 6c 65 20 69 73 20 67 72 65 61 74 65 72 20 74  ile is greater t
11deb 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a 20 63 6f  han the .  ** co
11dec 6e 66 69 67 75 72 65 64 20 6d 61 78 69 6d 75 6d  nfigured maximum
11ded 20 70 61 67 65 72 20 6e 75 6d 62 65 72 2c 20 69   pager number, i
11dee 6e 63 72 65 61 73 65 20 74 68 65 20 61 6c 6c 6f  ncrease the allo
11def 77 65 64 20 6c 69 6d 69 74 20 73 6f 0a 20 20 2a  wed limit so.  *
11df0 2a 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20  * that the file 
11df1 63 61 6e 20 62 65 20 72 65 61 64 2e 0a 20 20 2a  can be read..  *
11df2 2f 0a 20 20 69 66 28 20 6e 50 61 67 65 3e 70 50  /.  if( nPage>pP
11df3 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a  ager->mxPgno ){.
11df4 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67      pPager->mxPg
11df5 6e 6f 20 3d 20 28 50 67 6e 6f 29 6e 50 61 67 65  no = (Pgno)nPage
11df6 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
11df7 74 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 61  the output varia
11df8 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53  ble and return S
11df9 51 4c 49 54 45 5f 4f 4b 20 2a 2f 0a 20 20 69 66  QLITE_OK */.  if
11dfa 28 20 70 6e 50 61 67 65 20 29 7b 0a 20 20 20 20  ( pnPage ){.    
11dfb 2a 70 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b  *pnPage = nPage;
11dfc 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
11dfd 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
11dfe 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e  ** Try to obtain
11dff 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20   a lock of type 
11e00 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 74 68 65 20  locktype on the 
11e01 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
11e02 66 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f  f.** a similar o
11e03 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69  r greater lock i
11e04 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20  s already held, 
11e05 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
11e06 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74   a no-op.** (ret
11e07 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b  urning SQLITE_OK
11e08 20 69 6d 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a   immediately)..*
11e09 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  *.** Otherwise, 
11e0a 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69  attempt to obtai
11e0b 6e 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67  n the lock using
11e0c 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29   sqlite3OsLock()
11e0d 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65  . Invoke .** the
11e0e 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69   busy callback i
11e0f 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 63 75  f the lock is cu
11e10 72 72 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69  rrently not avai
11e11 6c 61 62 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a  lable. Repeat .*
11e12 2a 20 75 6e 74 69 6c 20 74 68 65 20 62 75 73 79  * until the busy
11e13 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
11e14 73 20 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c  s false or until
11e15 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20   the attempt to 
11e16 0a 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6c  .** obtain the l
11e17 6f 63 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a  ock succeeds..**
11e18 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
11e19 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
11e1a 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
11e1b 65 20 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f  e if we cannot o
11e1c 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63  btain.** the loc
11e1d 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 69  k. If the lock i
11e1e 73 20 6f 62 74 61 69 6e 65 64 20 73 75 63 63 65  s obtained succe
11e1f 73 73 66 75 6c 6c 79 2c 20 73 65 74 20 74 68 65  ssfully, set the
11e20 20 50 61 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a   Pager.state .**
11e21 20 76 61 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63   variable to loc
11e22 6b 74 79 70 65 20 62 65 66 6f 72 65 20 72 65 74  ktype before ret
11e23 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  urning..*/.stati
11e24 63 20 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74  c int pager_wait
11e25 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a  _on_lock(Pager *
11e26 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b  pPager, int lock
11e27 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  type){.  int rc;
11e28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11e29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11e2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
11e2b 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63  .  /* The OS loc
11e2c 6b 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65  k values must be
11e2d 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
11e2e 20 50 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75   Pager lock valu
11e2f 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  es */.  assert( 
11e30 50 41 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48  PAGER_SHARED==SH
11e31 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
11e32 73 73 65 72 74 28 20 50 41 47 45 52 5f 52 45 53  ssert( PAGER_RES
11e33 45 52 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f  ERVED==RESERVED_
11e34 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
11e35 28 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  ( PAGER_EXCLUSIV
11e36 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  E==EXCLUSIVE_LOC
11e37 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  K );..  /* If th
11e38 65 20 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e  e file is curren
11e39 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65  tly unlocked the
11e3a 6e 20 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20  n the size must 
11e3b 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20  be unknown */.  
11e3c 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
11e3d 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41  state>=PAGER_SHA
11e3e 52 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64  RED || pPager->d
11e3f 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b  bSizeValid==0 );
11e40 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
11e41 74 20 74 68 69 73 20 69 73 20 65 69 74 68 65 72  t this is either
11e42 20 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61 75 73   a no-op (becaus
11e43 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  e the requested 
11e44 6c 6f 63 6b 20 69 73 20 0a 20 20 2a 2a 20 61 6c  lock is .  ** al
11e45 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 72 20 6f  ready held, or o
11e46 6e 65 20 6f 66 20 74 68 65 20 74 72 61 6e 73 69  ne of the transi
11e47 73 74 69 6f 6e 73 20 74 68 61 74 20 74 68 65 20  stions that the 
11e48 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 20 20 2a  busy-handler.  *
11e49 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b 65 64  * may be invoked
11e4a 20 64 75 72 69 6e 67 2c 20 61 63 63 6f 72 64 69   during, accordi
11e4b 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d 65 6e  ng to the commen
11e4c 74 20 61 62 6f 76 65 0a 20 20 2a 2a 20 73 71 6c  t above.  ** sql
11e4d 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79  ite3PagerSetBusy
11e4e 68 61 6e 64 6c 65 72 28 29 2e 0a 20 20 2a 2f 0a  handler()..  */.
11e4f 20 20 61 73 73 65 72 74 28 20 28 70 50 61 67 65    assert( (pPage
11e50 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79  r->state>=lockty
11e51 70 65 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70  pe).       || (p
11e52 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
11e53 47 45 52 5f 55 4e 4c 4f 43 4b 20 26 26 20 6c 6f  GER_UNLOCK && lo
11e54 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 53 48  cktype==PAGER_SH
11e55 41 52 45 44 29 0a 20 20 20 20 20 20 20 7c 7c 20  ARED).       || 
11e56 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d  (pPager->state==
11e57 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 26  PAGER_RESERVED &
11e58 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45  & locktype==PAGE
11e59 52 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 29  R_EXCLUSIVE).  )
11e5a 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  ;..  if( pPager-
11e5b 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65  >state>=locktype
11e5c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
11e5d 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
11e5e 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20  .    do {.      
11e5f 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f  rc = sqlite3OsLo
11e60 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c  ck(pPager->fd, l
11e61 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 7d 77  ocktype);.    }w
11e62 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
11e63 5f 42 55 53 59 20 26 26 20 70 50 61 67 65 72 2d  _BUSY && pPager-
11e64 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50  >xBusyHandler(pP
11e65 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
11e66 65 72 41 72 67 29 20 29 3b 0a 20 20 20 20 69 66  erArg) );.    if
11e67 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
11e68 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
11e69 3e 73 74 61 74 65 20 3d 20 28 75 38 29 6c 6f 63  >state = (u8)loc
11e6a 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 49 4f 54  ktype;.      IOT
11e6b 52 41 43 45 28 28 22 4c 4f 43 4b 20 25 70 20 25  RACE(("LOCK %p %
11e6c 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 6c 6f  d\n", pPager, lo
11e6d 63 6b 74 79 70 65 29 29 0a 20 20 20 20 7d 0a 20  cktype)).    }. 
11e6e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
11e6f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74  }../*.** Truncat
11e70 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  e the in-memory 
11e71 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6d  database file im
11e72 61 67 65 20 74 6f 20 6e 50 61 67 65 20 70 61 67  age to nPage pag
11e73 65 73 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e  es. This .** fun
11e74 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61  ction does not a
11e75 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74  ctually modify t
11e76 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11e77 20 6f 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a   on disk. It .**
11e78 20 6a 75 73 74 20 73 65 74 73 20 74 68 65 20 69   just sets the i
11e79 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66  nternal state of
11e7a 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63   the pager objec
11e7b 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 0a 2a  t so that the .*
11e7c 2a 20 74 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c  * truncation wil
11e7d 6c 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 74  l be done when t
11e7e 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
11e7f 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74  action is commit
11e80 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
11e81 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
11e82 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65  te3PagerTruncate
11e83 49 6d 61 67 65 28 50 61 67 65 72 20 2a 70 50 61  Image(Pager *pPa
11e84 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29  ger, Pgno nPage)
11e85 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  {.  assert( pPag
11e86 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
11e87 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
11e88 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61  ger->dbSize>=nPa
11e89 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ge );.  assert( 
11e8a 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
11e8b 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b  AGER_RESERVED );
11e8c 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  .  pPager->dbSiz
11e8d 65 20 3d 20 6e 50 61 67 65 3b 0a 7d 0a 0a 2f 2a  e = nPage;.}../*
11e8e 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65  .** Shutdown the
11e8f 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 46 72   page cache.  Fr
11e90 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e  ee all memory an
11e91 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65  d close all file
11e92 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72  s..**.** If a tr
11e93 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e  ansaction was in
11e94 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74   progress when t
11e95 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
11e96 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74  alled, that.** t
11e97 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
11e98 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20  lled back.  All 
11e99 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
11e9a 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65  s are invalidate
11e9b 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d  d.** and their m
11e9c 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20  emory is freed. 
11e9d 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
11e9e 75 73 65 20 61 20 70 61 67 65 20 61 73 73 6f 63  use a page assoc
11e9f 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  iated.** with th
11ea0 69 73 20 70 61 67 65 20 63 61 63 68 65 20 61 66  is page cache af
11ea1 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ter this functio
11ea2 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c  n returns will l
11ea3 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20  ikely.** result 
11ea4 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a  in a coredump..*
11ea5 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
11ea6 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65 65  on always succee
11ea7 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63  ds. If a transac
11ea8 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61  tion is active a
11ea9 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20  n attempt.** is 
11eaa 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20  made to roll it 
11eab 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f  back. If an erro
11eac 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
11ead 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a  the rollback .**
11eae 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d   a hot journal m
11eaf 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68  ay be left in th
11eb0 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74  e filesystem but
11eb1 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74   no error is ret
11eb2 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20  urned.** to the 
11eb3 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  caller..*/.SQLIT
11eb4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
11eb5 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28  lite3PagerClose(
11eb6 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11eb7 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61    disable_simula
11eb8 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b  ted_io_errors();
11eb9 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  .  sqlite3BeginB
11eba 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
11ebb 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11ebc 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   = 0;.  pPager->
11ebd 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20  exclusiveMode = 
11ebe 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65 74  0;.  pager_reset
11ebf 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20  (pPager);.  if( 
11ec0 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 61 67  MEMDB ){.    pag
11ec1 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72  er_unlock(pPager
11ec2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
11ec3 2f 2a 20 53 65 74 20 50 61 67 65 72 2e 6a 6f 75  /* Set Pager.jou
11ec4 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 20 66 6f  rnalHdr to -1 fo
11ec5 72 20 74 68 65 20 62 65 6e 65 66 69 74 20 6f 66  r the benefit of
11ec6 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 79 62   the pager_playb
11ec7 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a 20 63 61  ack() .    ** ca
11ec8 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20  ll which may be 
11ec9 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 68 69 6e  made from within
11eca 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52   pagerUnlockAndR
11ecb 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 20 69 74  ollback(). If it
11ecc 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 2d  .    ** is not -
11ecd 31 2c 20 74 68 65 6e 20 74 68 65 20 75 6e 73 79  1, then the unsy
11ece 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20  nced portion of 
11ecf 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20  an open journal 
11ed0 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 2a 2a 20  file may.    ** 
11ed1 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 69  be played back i
11ed2 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
11ed3 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69  . If a power fai
11ed4 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c  lure occurs whil
11ed5 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73  e.    ** this is
11ed6 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 68 65 20   happening, the 
11ed7 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65 63  database may bec
11ed8 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 20  ome corrupt..   
11ed9 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   */.    pPager->
11eda 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 2d 31 3b  journalHdr = -1;
11edb 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b  .    pagerUnlock
11edc 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67  AndRollback(pPag
11edd 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  er);.  }.  sqlit
11ede 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
11edf 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73 69  c();.  enable_si
11ee0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
11ee1 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43  s();.  PAGERTRAC
11ee2 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c  E(("CLOSE %d\n",
11ee3 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
11ee4 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22  ));.  IOTRACE(("
11ee5 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50 61  CLOSE %p\n", pPa
11ee6 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65 33 4f  ger)).  sqlite3O
11ee7 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66  sClose(pPager->f
11ee8 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67  d);.  sqlite3Pag
11ee9 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54  eFree(pPager->pT
11eea 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69  mpSpace);.  sqli
11eeb 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 70  te3PcacheClose(p
11eec 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b  Pager->pPCache);
11eed 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61  ..  assert( !pPa
11eee 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20  ger->aSavepoint 
11eef 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a  && !pPager->pInJ
11ef0 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65  ournal );.  asse
11ef1 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67  rt( !isOpen(pPag
11ef2 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 4f  er->jfd) && !isO
11ef3 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  pen(pPager->sjfd
11ef4 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  ) );..  sqlite3_
11ef5 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20  free(pPager);.  
11ef6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
11ef7 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  ;.}..#if !define
11ef8 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66  d(NDEBUG) || def
11ef9 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
11efa 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  )./*.** Return t
11efb 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  he page number f
11efc 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a  or page pPg..*/.
11efd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50  SQLITE_PRIVATE P
11efe 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72  gno sqlite3Pager
11eff 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67  Pagenumber(DbPag
11f00 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72  e *pPg){.  retur
11f01 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23  n pPg->pgno;.}.#
11f02 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  endif../*.** Inc
11f03 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72  rement the refer
11f04 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 70  ence count for p
11f05 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49  age pPg..*/.SQLI
11f06 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
11f07 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28  sqlite3PagerRef(
11f08 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
11f09 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
11f0a 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (pPg);.}../*.** 
11f0b 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Sync the journal
11f0c 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  . In other words
11f0d 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  , make sure all 
11f0e 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68  the pages that h
11f0f 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74  ave.** been writ
11f10 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
11f11 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79  al have actually
11f12 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72   reached the sur
11f13 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64  face of the.** d
11f14 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 20 72  isk and can be r
11f15 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 65  estored in the e
11f16 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d 6a 6f  vent of a hot-jo
11f17 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a  urnal rollback..
11f18 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 61 67  **.** If the Pag
11f19 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67  er.needSync flag
11f1a 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 68 65   is not set, the
11f1b 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
11f1c 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f  is a.** no-op. O
11f1d 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 63  therwise, the ac
11f1e 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 64  tions required d
11f1f 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75  epend on the jou
11f20 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64  rnal-mode.** and
11f21 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61 72   the device char
11f22 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74  acteristics of t
11f23 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  he the file-syst
11f24 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  em, as follows:.
11f25 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65  **.**   * If the
11f26 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
11f27 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f   an in-memory jo
11f28 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61  urnal file, no a
11f29 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20  ction need.**   
11f2a 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a    be taken..**.*
11f2b 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65 2c  *   * Otherwise,
11f2c 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20 64   if the device d
11f2d 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
11f2e 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44 20  the SAFE_APPEND 
11f2f 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20  property,.**    
11f30 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66   then the nRec f
11f31 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74  ield of the most
11f32 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74 65   recently writte
11f33 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  n journal header
11f34 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61 74  .**     is updat
11f35 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  ed to contain th
11f36 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72  e number of jour
11f37 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61 74  nal records that
11f38 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 65   have.**     bee
11f39 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77  n written follow
11f3a 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20 70  ing it. If the p
11f3b 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69 6e  ager is operatin
11f3c 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a  g in full-sync.*
11f3d 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e  *     mode, then
11f3e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11f3f 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66 6f  e is synced befo
11f40 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69 73  re this field is
11f41 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   updated..**.** 
11f42 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69 63    * If the devic
11f43 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f  e does not suppo
11f44 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49 41  rt the SEQUENTIA
11f45 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65 6e  L property, then
11f46 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c   .**     journal
11f47 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e   file is synced.
11f48 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73  .**.** Or, in ps
11f49 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a  eudo-code:.**.**
11f4a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d     if( NOT <in-m
11f4b 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29  emory journal> )
11f4c 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54  {.**     if( NOT
11f4d 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a   SAFE_APPEND ){.
11f4e 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66 75  **       if( <fu
11f4f 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20  ll-sync mode> ) 
11f50 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66  xSync(<journal f
11f51 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20  ile>);.**       
11f52 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69 65  <update nRec fie
11f53 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a  ld>.**     } .**
11f54 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45 51       if( NOT SEQ
11f55 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 28  UENTIAL ) xSync(
11f56 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b  <journal file>);
11f57 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68  .**   }.**.** Th
11f58 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63  e Pager.needSync
11f59 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 20 62   flag is never b
11f5a 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 6f 72  e set for tempor
11f5b 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 61 6e  ary files, or an
11f5c 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 61 74  y.** file operat
11f5d 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d  ing in no-sync m
11f5e 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 79 6e  ode (Pager.noSyn
11f5f 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72  c set to non-zer
11f60 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63  o)..**.** If suc
11f61 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 72 6f  cessful, this ro
11f62 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65  utine clears the
11f63 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
11f64 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 20 0a   flag of every .
11f65 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c  ** page currentl
11f66 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  y held in memory
11f67 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
11f68 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20  g SQLITE_OK. If 
11f69 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 69  an IO.** error i
11f6a 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74  s encountered, t
11f6b 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72  hen the IO error
11f6c 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
11f6d 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e  d to the caller.
11f6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
11f6f 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72  yncJournal(Pager
11f70 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28   *pPager){.  if(
11f71 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
11f72 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
11f73 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69   !pPager->tempFi
11f74 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  le );.    if( pP
11f75 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
11f76 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e!=PAGER_JOURNAL
11f77 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20  MODE_MEMORY ){. 
11f78 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20       int rc;    
11f79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11f7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
11f7b 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20  urn code */.    
11f7c 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 63 20    const int iDc 
11f7d 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63  = sqlite3OsDevic
11f7e 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
11f7f 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20  (pPager->fd);.  
11f80 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70      assert( isOp
11f81 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
11f82 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d  );..      if( 0=
11f83 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43  =(iDc&SQLITE_IOC
11f84 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20  AP_SAFE_APPEND) 
11f85 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61  ){.        /* Va
11f86 72 69 61 62 6c 65 20 69 4e 52 65 63 4f 66 66 73  riable iNRecOffs
11f87 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  et is set to the
11f88 20 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20 6a   offset in the j
11f89 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 20 20  ournal file.    
11f8a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 52      ** of the nR
11f8b 65 63 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  ec field of the 
11f8c 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 77 72  most recently wr
11f8d 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65  itten journal he
11f8e 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2a  ader..        **
11f8f 20 54 68 69 73 20 66 69 65 6c 64 20 77 69 6c 6c   This field will
11f90 20 62 65 20 75 70 64 61 74 65 64 20 66 6f 6c 6c   be updated foll
11f91 6f 77 69 6e 67 20 74 68 65 20 78 53 79 6e 63 28  owing the xSync(
11f92 29 20 6f 70 65 72 61 74 69 6f 6e 0a 20 20 20 20  ) operation.    
11f93 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6a 6f      ** on the jo
11f94 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20  urnal file. */. 
11f95 20 20 20 20 20 20 20 69 36 34 20 69 4e 52 65 63         i64 iNRec
11f96 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d  Offset = pPager-
11f97 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 2b 20 73 69  >journalHdr + si
11f98 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11f99 69 63 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  ic);..        /*
11f9a 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c   This block deal
11f9b 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72  s with an obscur
11f9c 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68  e problem. If th
11f9d 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f  e last connectio
11f9e 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61  n.        ** tha
11f9f 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20  t wrote to this 
11fa0 64 61 74 61 62 61 73 65 20 77 61 73 20 6f 70 65  database was ope
11fa1 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73  rating in persis
11fa2 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20  tent-journal.   
11fa3 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68       ** mode, th
11fa4 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  en the journal f
11fa5 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73 20  ile may at this 
11fa6 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62  point actually b
11fa7 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20  e larger.       
11fa8 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a   ** than Pager.j
11fa9 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e  ournalOff bytes.
11faa 20 49 66 20 74 68 65 20 6e 65 78 74 20 74 68 69   If the next thi
11fab 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ng in the journa
11fac 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c  l.        ** fil
11fad 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20  e happens to be 
11fae 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72  a journal-header
11faf 20 28 77 72 69 74 74 65 6e 20 61 73 20 70 61 72   (written as par
11fb0 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20  t of the.       
11fb1 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e   ** previous con
11fb2 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63  nections transac
11fb3 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 61  tion), and a cra
11fb4 73 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c  sh or power-fail
11fb5 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ure .        ** 
11fb6 6f 63 63 75 72 73 20 61 66 74 65 72 20 6e 52 65  occurs after nRe
11fb7 63 20 69 73 20 75 70 64 61 74 65 64 20 62 75 74  c is updated but
11fb8 20 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f 6e   before this con
11fb9 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 0a  nection writes .
11fba 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 68          ** anyth
11fbb 69 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65 20  ing else to the 
11fbc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72  journal file (or
11fbd 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62   commits/rolls b
11fbe 61 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20 20  ack its .       
11fbf 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29   ** transaction)
11fc0 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61  , then SQLite ma
11fc1 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65  y become confuse
11fc2 64 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65  d when doing the
11fc3 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74   .        ** hot
11fc4 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63  -journal rollbac
11fc5 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f  k following reco
11fc6 76 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c  very. It may rol
11fc7 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20  l back all.     
11fc8 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f     ** of this co
11fc9 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20  nnections data, 
11fca 74 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20  then proceed to 
11fcb 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65  rolling back the
11fcc 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a   old,.        **
11fcd 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 74   out-of-date dat
11fce 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69  a that follows i
11fcf 74 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72 72  t. Database corr
11fd0 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20  uption..        
11fd1 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 6f  **.        ** To
11fd2 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69   work around thi
11fd3 73 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61  s, if the journa
11fd4 6c 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70 65  l file does appe
11fd5 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20  ar to contain.  
11fd6 20 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 64        ** a valid
11fd7 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e   header followin
11fd8 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f  g Pager.journalO
11fd9 66 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  ff, then write a
11fda 20 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a 2a   0x00.        **
11fdb 20 62 79 74 65 20 74 6f 20 74 68 65 20 73 74 61   byte to the sta
11fdc 72 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65 76  rt of it to prev
11fdd 65 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69 6e  ent it from bein
11fde 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20  g recognized..  
11fdf 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
11fe0 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 65   ** Variable iNe
11fe1 78 74 48 64 72 4f 66 66 73 65 74 20 69 73 20 73  xtHdrOffset is s
11fe2 65 74 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74  et to the offset
11fe3 20 61 74 20 77 68 69 63 68 20 74 68 69 73 0a 20   at which this. 
11fe4 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65         ** proble
11fe5 6d 61 74 69 63 20 68 65 61 64 65 72 20 77 69 6c  matic header wil
11fe6 6c 20 6f 63 63 75 72 2c 20 69 66 20 69 74 20 65  l occur, if it e
11fe7 78 69 73 74 73 2e 20 61 4d 61 67 69 63 20 69 73  xists. aMagic is
11fe8 20 75 73 65 64 20 0a 20 20 20 20 20 20 20 20 2a   used .        *
11fe9 2a 20 61 73 20 61 20 74 65 6d 70 6f 72 61 72 79  * as a temporary
11fea 20 62 75 66 66 65 72 20 74 6f 20 69 6e 73 70 65   buffer to inspe
11feb 63 74 20 74 68 65 20 66 69 72 73 74 20 63 6f 75  ct the first cou
11fec 70 6c 65 20 6f 66 20 62 79 74 65 73 20 6f 66 0a  ple of bytes of.
11fed 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70          ** the p
11fee 6f 74 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c  otential journal
11fef 20 68 65 61 64 65 72 2e 0a 20 20 20 20 20 20 20   header..       
11ff0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34 20   */.        i64 
11ff1 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20 3d  iNextHdrOffset =
11ff2 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65   journalHdrOffse
11ff3 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  t(pPager);.     
11ff4 20 20 20 75 38 20 61 4d 61 67 69 63 5b 38 5d 3b     u8 aMagic[8];
11ff5 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
11ff6 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
11ff7 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c  er->jfd, aMagic,
11ff8 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 73   8, iNextHdrOffs
11ff9 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  et);.        if(
11ffa 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
11ffb 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67  & 0==memcmp(aMag
11ffc 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  ic, aJournalMagi
11ffd 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20 20  c, 8) ){.       
11ffe 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
11fff 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b  u8 zerobyte = 0;
12000 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
12001 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
12002 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72  Pager->jfd, &zer
12003 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 48  obyte, 1, iNextH
12004 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20  drOffset);.     
12005 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
12006 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
12007 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45  & rc!=SQLITE_IOE
12008 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b  RR_SHORT_READ ){
12009 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1200a 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
1200b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74  .        /* Writ
1200c 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65  e the nRec value
1200d 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61   into the journa
1200e 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49  l file header. I
1200f 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  f in.        ** 
12010 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73  full-synchronous
12011 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20   mode, sync the 
12012 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54  journal first. T
12013 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
12014 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20  .        ** all 
12015 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20  data has really 
12016 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65 66  hit the disk bef
12017 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64 61  ore nRec is upda
12018 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20  ted to mark.    
12019 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63      ** it as a c
1201a 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c  andidate for rol
1201b 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a  lback..        *
1201c 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69  *.        ** Thi
1201d 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  s is not require
1201e 64 20 69 66 20 74 68 65 20 70 65 72 73 69 73 74  d if the persist
1201f 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72  ent media suppor
12020 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ts the.        *
12021 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72  * SAFE_APPEND pr
12022 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 65 20  operty. Because 
12023 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 20  in this case it 
12024 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
12025 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20  .        ** for 
12026 67 61 72 62 61 67 65 20 64 61 74 61 20 74 6f 20  garbage data to 
12027 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  be appended to t
12028 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65  he file, the nRe
12029 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20  c field.        
1202a 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20  ** is populated 
1202b 77 69 74 68 20 30 78 46 46 46 46 46 46 46 46 20  with 0xFFFFFFFF 
1202c 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  when the journal
1202d 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74   header is writt
1202e 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  en.        ** an
1202f 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f  d never needs to
12030 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20   be updated..   
12031 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
12032 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c  if( pPager->full
12033 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26  Sync && 0==(iDc&
12034 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
12035 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20  UENTIAL) ){.    
12036 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
12037 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20  (("SYNC journal 
12038 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  of %d\n", PAGERI
12039 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20  D(pPager)));.   
1203a 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28         IOTRACE((
1203b 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50  "JSYNC %p\n", pP
1203c 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20  ager)).         
1203d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
1203e 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ync(pPager->jfd,
1203f 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
12040 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ags);.          
12041 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12042 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
12043 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12044 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20   IOTRACE(("JHDR 
12045 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70  %p %lld %d\n", p
12046 50 61 67 65 72 2c 20 69 4e 52 65 63 4f 66 66 73  Pager, iNRecOffs
12047 65 74 2c 20 34 29 29 3b 0a 20 20 20 20 20 20 20  et, 4));.       
12048 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74   rc = write32bit
12049 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69  s(pPager->jfd, i
1204a 4e 52 65 63 4f 66 66 73 65 74 2c 20 70 50 61 67  NRecOffset, pPag
1204b 65 72 2d 3e 6e 52 65 63 29 3b 0a 20 20 20 20 20  er->nRec);.     
1204c 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1204d 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
1204e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1204f 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49  if( 0==(iDc&SQLI
12050 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
12051 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20  IAL) ){.        
12052 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59 4e  PAGERTRACE(("SYN
12053 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c  C journal of %d\
12054 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
12055 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20 49  er)));.        I
12056 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25  OTRACE(("JSYNC %
12057 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20  p\n", pPager)). 
12058 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
12059 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72  te3OsSync(pPager
1205a 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73  ->jfd, pPager->s
1205b 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20 20 20 20  ync_flags| .    
1205c 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 73        (pPager->s
1205d 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 54  ync_flags==SQLIT
1205e 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c 49  E_SYNC_FULL?SQLI
1205f 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
12060 3a 30 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  :0).        );. 
12061 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
12062 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
12063 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
12064 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20    }..    /* The 
12065 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 61 73  journal file was
12066 20 6a 75 73 74 20 73 75 63 63 65 73 73 66 75 6c   just successful
12067 6c 79 20 73 79 6e 63 65 64 2e 20 53 65 74 20 50  ly synced. Set P
12068 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20  ager.needSync . 
12069 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f 20 61 6e     ** to zero an
1206a 64 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 44  d clear the PGHD
1206b 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
1206c 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 73 2e 0a   on all pagess..
1206d 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65      */.    pPage
1206e 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  r->needSync = 0;
1206f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
12070 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 31 3b  rnalStarted = 1;
12071 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
12072 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73  heClearSyncFlags
12073 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
12074 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
12075 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
12076 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  *.** The argumen
12077 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69  t is the first i
12078 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
12079 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 63  of dirty pages c
1207a 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 62 79 20 74  onnected.** by t
1207b 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79 20  he PgHdr.pDirty 
1207c 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20 66 75  pointer. This fu
1207d 6e 63 74 69 6f 6e 20 77 72 69 74 65 73 20 65 61  nction writes ea
1207e 63 68 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a  ch one of the.**
1207f 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
12080 20 69 6e 20 74 68 65 20 6c 69 73 74 20 74 6f 20   in the list to 
12081 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12082 65 2e 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  e. The argument 
12083 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2c 20  may.** be NULL, 
12084 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61 6e 20  representing an 
12085 65 6d 70 74 79 20 6c 69 73 74 2e 20 49 6e 20 74  empty list. In t
12086 68 69 73 20 63 61 73 65 20 74 68 69 73 20 66 75  his case this fu
12087 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 20 6e  nction is.** a n
12088 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  o-op..**.** The 
12089 70 61 67 65 72 20 6d 75 73 74 20 68 6f 6c 64 20  pager must hold 
1208a 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52  at least a RESER
1208b 56 45 44 20 6c 6f 63 6b 20 77 68 65 6e 20 74 68  VED lock when th
1208c 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69  is function.** i
1208d 73 20 63 61 6c 6c 65 64 2e 20 42 65 66 6f 72 65  s called. Before
1208e 20 77 72 69 74 69 6e 67 20 61 6e 79 74 68 69 6e   writing anythin
1208f 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  g to the databas
12090 65 20 66 69 6c 65 2c 20 74 68 69 73 20 6c 6f 63  e file, this loc
12091 6b 0a 2a 2a 20 69 73 20 75 70 67 72 61 64 65 64  k.** is upgraded
12092 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45   to an EXCLUSIVE
12093 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f   lock. If the lo
12094 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74  ck cannot be obt
12095 61 69 6e 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45  ained,.** SQLITE
12096 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65  _BUSY is returne
12097 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73  d and no data is
12098 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
12099 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1209a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  * .** If the pag
1209b 65 72 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c  er is a temp-fil
1209c 65 20 70 61 67 65 72 20 61 6e 64 20 74 68 65 20  e pager and the 
1209d 61 63 74 75 61 6c 20 66 69 6c 65 2d 73 79 73 74  actual file-syst
1209e 65 6d 20 66 69 6c 65 0a 2a 2a 20 69 73 20 6e 6f  em file.** is no
1209f 74 20 79 65 74 20 6f 70 65 6e 2c 20 69 74 20 69  t yet open, it i
120a0 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 6f 70  s created and op
120a1 65 6e 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20  ened before any 
120a2 64 61 74 61 20 69 73 20 0a 2a 2a 20 77 72 69 74  data is .** writ
120a3 74 65 6e 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 4f  ten out..**.** O
120a4 6e 63 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73  nce the lock has
120a5 20 62 65 65 6e 20 75 70 67 72 61 64 65 64 20 61   been upgraded a
120a6 6e 64 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79  nd, if necessary
120a7 2c 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e 65  , the file opene
120a8 64 2c 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 20  d,.** the pages 
120a9 61 72 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20  are written out 
120aa 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
120ab 66 69 6c 65 20 69 6e 20 6c 69 73 74 20 6f 72 64  file in list ord
120ac 65 72 2e 20 57 72 69 74 69 6e 67 0a 2a 2a 20 61  er. Writing.** a
120ad 20 70 61 67 65 20 69 73 20 73 6b 69 70 70 65 64   page is skipped
120ae 20 69 66 20 69 74 20 6d 65 65 74 73 20 65 69 74   if it meets eit
120af 68 65 72 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  her of the follo
120b0 77 69 6e 67 20 63 72 69 74 65 72 69 61 3a 0a 2a  wing criteria:.*
120b1 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67  *.**   * The pag
120b2 65 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61  e number is grea
120b3 74 65 72 20 74 68 61 6e 20 50 61 67 65 72 2e 64  ter than Pager.d
120b4 62 53 69 7a 65 2c 20 6f 72 0a 2a 2a 20 20 20 2a  bSize, or.**   *
120b5 20 54 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f   The PGHDR_DONT_
120b6 57 52 49 54 45 20 66 6c 61 67 20 69 73 20 73 65  WRITE flag is se
120b7 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a  t on the page..*
120b8 2a 0a 2a 2a 20 49 66 20 77 72 69 74 69 6e 67 20  *.** If writing 
120b9 6f 75 74 20 61 20 70 61 67 65 20 63 61 75 73 65  out a page cause
120ba 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
120bb 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 50 61 67  ile to grow, Pag
120bc 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 0a 2a 2a  er.dbFileSize.**
120bd 20 69 73 20 75 70 64 61 74 65 64 20 61 63 63 6f   is updated acco
120be 72 64 69 6e 67 6c 79 2e 20 49 66 20 70 61 67 65  rdingly. If page
120bf 20 31 20 69 73 20 77 72 69 74 74 65 6e 20 6f 75   1 is written ou
120c0 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  t, then the valu
120c1 65 20 63 61 63 68 65 64 0a 2a 2a 20 69 6e 20 50  e cached.** in P
120c2 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b  ager.dbFileVers[
120c3 5d 20 69 73 20 75 70 64 61 74 65 64 20 74 6f 20  ] is updated to 
120c4 6d 61 74 63 68 20 74 68 65 20 6e 65 77 20 76 61  match the new va
120c5 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a  lue stored in.**
120c6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
120c7 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76 65  le..**.** If eve
120c8 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65  rything is succe
120c9 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b  ssful, SQLITE_OK
120ca 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
120cb 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 2a 2a   an IO error .**
120cc 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20 65   occurs, an IO e
120cd 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
120ce 75 72 6e 65 64 2e 20 4f 72 2c 20 69 66 20 74 68  urned. Or, if th
120cf 65 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  e EXCLUSIVE lock
120d0 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 6f 62   cannot.** be ob
120d1 74 61 69 6e 65 64 2c 20 53 51 4c 49 54 45 5f 42  tained, SQLITE_B
120d2 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2e  USY is returned.
120d3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
120d4 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c  ager_write_pagel
120d5 69 73 74 28 50 67 48 64 72 20 2a 70 4c 69 73 74  ist(PgHdr *pList
120d6 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
120d7 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
120d8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
120d9 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69  er object */.  i
120da 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
120db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
120dc 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
120dd 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4c 69  de */..  if( pLi
120de 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53  st==0 ) return S
120df 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67  QLITE_OK;.  pPag
120e0 65 72 20 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67  er = pList->pPag
120e1 65 72 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69  er;..  /* At thi
120e2 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 61  s point there ma
120e3 79 20 62 65 20 65 69 74 68 65 72 20 61 20 52 45  y be either a RE
120e4 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53  SERVED or EXCLUS
120e5 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a  IVE lock on the.
120e6 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69    ** database fi
120e7 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  le. If there is 
120e8 61 6c 72 65 61 64 79 20 61 6e 20 45 58 43 4c 55  already an EXCLU
120e9 53 49 56 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66  SIVE lock, the f
120ea 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61  ollowing.  ** ca
120eb 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20  ll is a no-op.. 
120ec 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20   **.  ** Moving 
120ed 74 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45  the lock from RE
120ee 53 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53  SERVED to EXCLUS
120ef 49 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76  IVE actually inv
120f0 6f 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a  olves going.  **
120f1 20 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65   through an inte
120f2 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 20 50  rmediate state P
120f3 45 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44  ENDING.   A PEND
120f4 49 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74  ING lock prevent
120f5 73 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65  s new.  ** reade
120f6 72 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e  rs from attachin
120f7 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  g to the databas
120f8 65 20 62 75 74 20 69 73 20 75 6e 73 75 66 66 69  e but is unsuffi
120f9 63 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a  cient for us to.
120fa 20 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65    ** write.  The
120fb 20 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49   idea of a PENDI
120fc 4e 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72  NG lock is to pr
120fd 65 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72  event new reader
120fe 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69  s from.  ** comi
120ff 6e 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77  ng in while we w
12100 61 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67  ait for existing
12101 20 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61   readers to clea
12102 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69  r..  **.  ** Whi
12103 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20  le the pager is 
12104 69 6e 20 74 68 65 20 52 45 53 45 52 56 45 44 20  in the RESERVED 
12105 73 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69  state, the origi
12106 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  nal database fil
12107 65 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e  e.  ** is unchan
12108 67 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72  ged and we can r
12109 6f 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20  ollback without 
1210a 68 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61  having to playba
1210b 63 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72  ck the.  ** jour
1210c 6e 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69  nal into the ori
1210d 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66  ginal database f
1210e 69 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72  ile.  Once we tr
1210f 61 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a  ansition to.  **
12110 20 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d   EXCLUSIVE, it m
12111 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73  eans the databas
12112 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  e file has been 
12113 63 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20  changed and any 
12114 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69  rollback.  ** wi
12115 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75  ll require a jou
12116 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20  rnal playback.. 
12117 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
12118 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
12119 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20  ER_RESERVED );. 
1211a 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74   rc = pager_wait
1211b 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c  _on_lock(pPager,
1211c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29   EXCLUSIVE_LOCK)
1211d 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66  ;..  /* If the f
1211e 69 6c 65 20 69 73 20 61 20 74 65 6d 70 2d 66 69  ile is a temp-fi
1211f 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  le has not yet b
12120 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e  een opened, open
12121 20 69 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a   it now. It.  **
12122 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
12123 20 66 6f 72 20 72 63 20 74 6f 20 62 65 20 6f 74   for rc to be ot
12124 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
12125 4f 4b 20 69 66 20 74 68 69 73 20 62 72 61 6e 63  OK if this branc
12126 68 0a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c  h.  ** is taken,
12127 20 61 73 20 70 61 67 65 72 5f 77 61 69 74 5f 6f   as pager_wait_o
12128 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f  n_lock() is a no
12129 2d 6f 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c  -op for temp-fil
1212a 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  es..  */.  if( !
1212b 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
1212c 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  d) ){.    assert
1212d 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  ( pPager->tempFi
1212e 6c 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  le && rc==SQLITE
1212f 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  _OK );.    rc = 
12130 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50  pagerOpentemp(pP
12131 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64  ager, pPager->fd
12132 2c 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61  , pPager->vfsFla
12133 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c  gs);.  }..  whil
12134 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
12135 20 26 26 20 70 4c 69 73 74 20 29 7b 0a 20 20 20   && pList ){.   
12136 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69   Pgno pgno = pLi
12137 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f  st->pgno;..    /
12138 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 64  * If there are d
12139 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68  irty pages in th
1213a 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74  e page cache wit
1213b 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67  h page numbers g
1213c 72 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 68  reater.    ** th
1213d 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c  an Pager.dbSize,
1213e 20 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c 69   this means sqli
1213f 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65  te3PagerTruncate
12140 49 6d 61 67 65 28 29 20 77 61 73 20 63 61 6c 6c  Image() was call
12141 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b  ed to.    ** mak
12142 65 20 74 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c  e the file small
12143 65 72 20 28 70 72 65 73 75 6d 61 62 6c 79 20 62  er (presumably b
12144 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f  y auto-vacuum co
12145 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74  de). Do not writ
12146 65 0a 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 63  e.    ** any suc
12147 68 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 66  h pages to the f
12148 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
12149 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20  ** Also, do not 
1214a 77 72 69 74 65 20 6f 75 74 20 61 6e 79 20 70 61  write out any pa
1214b 67 65 20 74 68 61 74 20 68 61 73 20 74 68 65 20  ge that has the 
1214c 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45  PGHDR_DONT_WRITE
1214d 20 66 6c 61 67 0a 20 20 20 20 2a 2a 20 73 65 74   flag.    ** set
1214e 20 28 73 65 74 20 62 79 20 73 71 6c 69 74 65 33   (set by sqlite3
1214f 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 29  PagerDontWrite()
12150 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  )..    */.    if
12151 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e  ( pgno<=pPager->
12152 64 62 53 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c  dbSize && 0==(pL
12153 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  ist->flags&PGHDR
12154 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a  _DONT_WRITE) ){.
12155 20 20 20 20 20 20 69 36 34 20 6f 66 66 73 65 74        i64 offset
12156 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34   = (pgno-1)*(i64
12157 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  )pPager->pageSiz
12158 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66  e;         /* Of
12159 66 73 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f  fset to write */
1215a 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 44 61  .      char *pDa
1215b 74 61 20 3d 20 43 4f 44 45 43 32 28 70 50 61 67  ta = CODEC2(pPag
1215c 65 72 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61  er, pList->pData
1215d 2c 20 70 67 6e 6f 2c 20 36 29 3b 20 2f 2a 20 44  , pgno, 6); /* D
1215e 61 74 61 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  ata to write */.
1215f 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  .      /* Write 
12160 6f 75 74 20 74 68 65 20 70 61 67 65 20 64 61 74  out the page dat
12161 61 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d  a. */.      rc =
12162 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
12163 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61 74  pPager->fd, pDat
12164 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
12165 69 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20  ize, offset);.. 
12166 20 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20       /* If page 
12167 31 20 77 61 73 20 6a 75 73 74 20 77 72 69 74 74  1 was just writt
12168 65 6e 2c 20 75 70 64 61 74 65 20 50 61 67 65 72  en, update Pager
12169 2e 64 62 46 69 6c 65 56 65 72 73 20 74 6f 20 6d  .dbFileVers to m
1216a 61 74 63 68 0a 20 20 20 20 20 20 2a 2a 20 74 68  atch.      ** th
1216b 65 20 76 61 6c 75 65 20 6e 6f 77 20 73 74 6f 72  e value now stor
1216c 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ed in the databa
1216d 73 65 20 66 69 6c 65 2e 20 49 66 20 77 72 69 74  se file. If writ
1216e 69 6e 67 20 74 68 69 73 20 0a 20 20 20 20 20 20  ing this .      
1216f 2a 2a 20 70 61 67 65 20 63 61 75 73 65 64 20 74  ** page caused t
12170 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12171 20 74 6f 20 67 72 6f 77 2c 20 75 70 64 61 74 65   to grow, update
12172 20 64 62 46 69 6c 65 53 69 7a 65 2e 20 0a 20 20   dbFileSize. .  
12173 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
12174 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20   pgno==1 ){.    
12175 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67      memcpy(&pPag
12176 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20  er->dbFileVers, 
12177 26 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a 65  &pData[24], size
12178 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  of(pPager->dbFil
12179 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d  eVers));.      }
1217a 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e  .      if( pgno>
1217b 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69  pPager->dbFileSi
1217c 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50  ze ){.        pP
1217d 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
1217e 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d   = pgno;.      }
1217f 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74  ..      /* Updat
12180 65 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 6a  e any backup obj
12181 65 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 65  ects copying the
12182 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69   contents of thi
12183 73 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 20  s pager. */.    
12184 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55    sqlite3BackupU
12185 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42  pdate(pPager->pB
12186 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38  ackup, pgno, (u8
12187 20 2a 29 70 44 61 74 61 29 3b 0a 0a 20 20 20 20   *)pData);..    
12188 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53    PAGERTRACE(("S
12189 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 20  TORE %d page %d 
1218a 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20  hash(%08x)\n",. 
1218b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1218c 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72    PAGERID(pPager
1218d 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70  ), pgno, pager_p
1218e 61 67 65 68 61 73 68 28 70 4c 69 73 74 29 29 29  agehash(pList)))
1218f 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28  ;.      IOTRACE(
12190 28 22 50 47 4f 55 54 20 25 70 20 25 64 5c 6e 22  ("PGOUT %p %d\n"
12191 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29  , pPager, pgno))
12192 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e  ;.      PAGER_IN
12193 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72  CR(sqlite3_pager
12194 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 29 3b  _writedb_count);
12195 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43  .      PAGER_INC
12196 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65  R(pPager->nWrite
12197 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
12198 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28      PAGERTRACE((
12199 22 4e 4f 53 54 4f 52 45 20 25 64 20 70 61 67 65  "NOSTORE %d page
1219a 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
1219b 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 29 29 3b  pPager), pgno));
1219c 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51  .    }.#ifdef SQ
1219d 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
1219e 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61 67 65  .    pList->page
1219f 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67  Hash = pager_pag
121a0 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a 23 65  ehash(pList);.#e
121a1 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74 20 3d  ndif.    pList =
121a2 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b 0a   pList->pDirty;.
121a3 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
121a4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e  ;.}../*.** Appen
121a5 64 20 61 20 72 65 63 6f 72 64 20 6f 66 20 74 68  d a record of th
121a6 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  e current state 
121a7 6f 66 20 70 61 67 65 20 70 50 67 20 74 6f 20 74  of page pPg to t
121a8 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20  he sub-journal. 
121a9 0a 2a 2a 20 49 74 20 69 73 20 74 68 65 20 63 61  .** It is the ca
121aa 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69  llers responsibi
121ab 6c 69 74 79 20 74 6f 20 75 73 65 20 73 75 62 6a  lity to use subj
121ac 52 65 71 75 69 72 65 73 50 61 67 65 28 29 20 74  RequiresPage() t
121ad 6f 20 63 68 65 63 6b 20 0a 2a 2a 20 74 68 61 74  o check .** that
121ae 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65   it is really re
121af 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 63 61  quired before ca
121b0 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74  lling this funct
121b1 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  ion..**.** If su
121b2 63 63 65 73 73 66 75 6c 2c 20 73 65 74 20 74 68  ccessful, set th
121b3 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64  e bit correspond
121b4 69 6e 67 20 74 6f 20 70 50 67 2d 3e 70 67 6e 6f  ing to pPg->pgno
121b5 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 73 0a   in the bitvecs.
121b6 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f 70 65 6e 20  ** for all open 
121b7 73 61 76 65 70 6f 69 6e 74 73 20 62 65 66 6f 72  savepoints befor
121b8 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a  e returning..**.
121b9 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
121ba 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
121bb 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67  OK if everything
121bc 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
121bd 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 63  an IO.** error c
121be 6f 64 65 20 69 66 20 74 68 65 20 61 74 74 65 6d  ode if the attem
121bf 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74  pt to write to t
121c0 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66  he sub-journal f
121c1 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c  ails, or .** SQL
121c2 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d  ITE_NOMEM if a m
121c3 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c  alloc fails whil
121c4 65 20 73 65 74 74 69 6e 67 20 61 20 62 69 74 20  e setting a bit 
121c5 69 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 0a 2a  in a savepoint.*
121c6 2a 20 62 69 74 76 65 63 2e 0a 2a 2f 0a 73 74 61  * bitvec..*/.sta
121c7 74 69 63 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e  tic int subjourn
121c8 61 6c 50 61 67 65 28 50 67 48 64 72 20 2a 70 50  alPage(PgHdr *pP
121c9 67 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  g){.  int rc = S
121ca 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65  QLITE_OK;.  Page
121cb 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
121cc 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20 69  >pPager;.  if( i
121cd 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a  sOpen(pPager->sj
121ce 66 64 29 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  fd) ){.    void 
121cf 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44  *pData = pPg->pD
121d0 61 74 61 3b 0a 20 20 20 20 69 36 34 20 6f 66 66  ata;.    i64 off
121d1 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53  set = pPager->nS
121d2 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d  ubRec*(4+pPager-
121d3 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20  >pageSize);.    
121d4 63 68 61 72 20 2a 70 44 61 74 61 32 20 3d 20 43  char *pData2 = C
121d5 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44  ODEC2(pPager, pD
121d6 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  ata, pPg->pgno, 
121d7 37 29 3b 0a 20 20 0a 20 20 20 20 50 41 47 45 52  7);.  .    PAGER
121d8 54 52 41 43 45 28 28 22 53 54 4d 54 2d 4a 4f 55  TRACE(("STMT-JOU
121d9 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 5c  RNAL %d page %d\
121da 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
121db 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29  er), pPg->pgno))
121dc 3b 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28  ;.  .    assert(
121dd 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70   pageInJournal(p
121de 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f  Pg) || pPg->pgno
121df 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  >pPager->dbOrigS
121e0 69 7a 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ize );.    rc = 
121e1 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
121e2 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 73 65 74  er->sjfd, offset
121e3 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
121e4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
121e5 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
121e6 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
121e7 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 70  (pPager->sjfd, p
121e8 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70  Data2, pPager->p
121e9 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b  ageSize, offset+
121ea 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  4);.    }.  }.  
121eb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
121ec 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  K ){.    pPager-
121ed 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20  >nSubRec++;.    
121ee 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
121ef 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a  nSavepoint>0 );.
121f0 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 53 61      rc = addToSa
121f1 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70  vepointBitvecs(p
121f2 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
121f3 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
121f4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
121f5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
121f6 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  c;.}.../*.** Thi
121f7 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
121f8 6c 6c 65 64 20 62 79 20 74 68 65 20 70 63 61 63  lled by the pcac
121f9 68 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69 74  he layer when it
121fa 20 68 61 73 20 72 65 61 63 68 65 64 20 73 6f 6d   has reached som
121fb 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79  e.** soft memory
121fc 20 6c 69 6d 69 74 2e 20 54 68 65 20 66 69 72 73   limit. The firs
121fd 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  t argument is a 
121fe 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67  pointer to a Pag
121ff 65 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61  er object.** (ca
12200 73 74 20 61 73 20 61 20 76 6f 69 64 2a 29 2e 20  st as a void*). 
12201 54 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 77  The pager is alw
12202 61 79 73 20 27 70 75 72 67 65 61 62 6c 65 27 20  ays 'purgeable' 
12203 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  (not an in-memor
12204 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 2e 20  y.** database). 
12205 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
12206 65 6e 74 20 69 73 20 61 20 72 65 66 65 72 65 6e  ent is a referen
12207 63 65 20 74 6f 20 61 20 70 61 67 65 20 74 68 61  ce to a page tha
12208 74 20 69 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74  t is .** current
12209 6c 79 20 64 69 72 74 79 20 62 75 74 20 68 61 73  ly dirty but has
1220a 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
1220b 72 65 66 65 72 65 6e 63 65 73 2e 20 54 68 65 20  references. The 
1220c 70 61 67 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79  page.** is alway
1220d 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
1220e 68 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a 65  h the Pager obje
1220f 63 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ct passed as the
12210 20 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d   first .** argum
12211 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a  ent..**.** The j
12212 6f 62 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74  ob of this funct
12213 69 6f 6e 20 69 73 20 74 6f 20 6d 61 6b 65 20 70  ion is to make p
12214 50 67 20 63 6c 65 61 6e 20 62 79 20 77 72 69 74  Pg clean by writ
12215 69 6e 67 20 69 74 73 20 63 6f 6e 74 65 6e 74 73  ing its contents
12216 0a 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 64  .** out to the d
12217 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 66  atabase file, if
12218 20 70 6f 73 73 69 62 6c 65 2e 20 54 68 69 73 20   possible. This 
12219 6d 61 79 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63  may involve sync
1221a 69 6e 67 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e  ing the.** journ
1221b 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20  al file. .**.** 
1221c 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73  If successful, s
1221d 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
1221e 43 6c 65 61 6e 28 29 20 69 73 20 63 61 6c 6c 65  Clean() is calle
1221f 64 20 6f 6e 20 74 68 65 20 70 61 67 65 20 61 6e  d on the page an
12220 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72  d.** SQLITE_OK r
12221 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49  eturned. If an I
12222 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  O error occurs w
12223 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 6d  hile trying to m
12224 61 6b 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20  ake the.** page 
12225 63 6c 65 61 6e 2c 20 74 68 65 20 49 4f 20 65 72  clean, the IO er
12226 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
12227 72 6e 65 64 2e 20 49 66 20 74 68 65 20 70 61 67  rned. If the pag
12228 65 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d  e cannot be.** m
12229 61 64 65 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f  ade clean for so
1222a 6d 65 20 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c  me other reason,
1222b 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63   but no error oc
1222c 63 75 72 73 2c 20 74 68 65 6e 20 53 51 4c 49 54  curs, then SQLIT
1222d 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72  E_OK.** is retur
1222e 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 63  ned by sqlite3Pc
1222f 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20  acheMakeClean() 
12230 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a  is not called..*
12231 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
12232 65 72 53 74 72 65 73 73 28 76 6f 69 64 20 2a 70  erStress(void *p
12233 2c 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  , PgHdr *pPg){. 
12234 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
12235 20 28 50 61 67 65 72 20 2a 29 70 3b 0a 20 20 69   (Pager *)p;.  i
12236 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
12237 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  K;..  assert( pP
12238 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65  g->pPager==pPage
12239 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1223a 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
1223b 44 49 52 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 54  DIRTY );..  /* T
1223c 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61  he doNotSync fla
1223d 67 20 69 73 20 73 65 74 20 62 79 20 74 68 65 20  g is set by the 
1223e 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1223f 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 68 69  e() function whi
12240 6c 65 20 69 74 0a 20 20 2a 2a 20 69 73 20 6a 6f  le it.  ** is jo
12241 75 72 6e 61 6c 6c 69 6e 67 20 61 20 73 65 74 20  urnalling a set 
12242 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 64  of two or more d
12243 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68  atabase pages th
12244 61 74 20 61 72 65 20 73 74 6f 72 65 64 0a 20 20  at are stored.  
12245 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64  ** on the same d
12246 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 79 6e 63  isk sector. Sync
12247 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ing the journal 
12248 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 77  is not allowed w
12249 68 69 6c 65 0a 20 20 2a 2a 20 74 68 69 73 20 69  hile.  ** this i
1224a 73 20 68 61 70 70 65 6e 69 6e 67 20 61 73 20 69  s happening as i
1224b 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  t is important t
1224c 68 61 74 20 61 6c 6c 20 6d 65 6d 62 65 72 73 20  hat all members 
1224d 6f 66 20 73 75 63 68 20 61 0a 20 20 2a 2a 20 73  of such a.  ** s
1224e 65 74 20 6f 66 20 70 61 67 65 73 20 61 72 65 20  et of pages are 
1224f 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 74  synced to disk t
12250 6f 67 65 74 68 65 72 2e 20 53 6f 2c 20 69 66 20  ogether. So, if 
12251 74 68 65 20 70 61 67 65 20 74 68 69 73 20 66 75  the page this fu
12252 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74  nction.  ** is t
12253 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 63 6c  rying to make cl
12254 65 61 6e 20 77 69 6c 6c 20 72 65 71 75 69 72 65  ean will require
12255 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20   a journal sync 
12256 61 6e 64 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e  and the doNotSyn
12257 63 0a 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73  c.  ** flag is s
12258 65 74 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f  et, return witho
12259 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e  ut doing anythin
1225a 67 2e 20 54 68 65 20 70 63 61 63 68 65 20 6c 61  g. The pcache la
1225b 79 65 72 20 77 69 6c 6c 0a 20 20 2a 2a 20 6a 75  yer will.  ** ju
1225c 73 74 20 68 61 76 65 20 74 6f 20 67 6f 20 61 68  st have to go ah
1225d 65 61 64 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65  ead and allocate
1225e 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66 66   a new page buff
1225f 65 72 20 69 6e 73 74 65 61 64 20 6f 66 0a 20 20  er instead of.  
12260 2a 2a 20 72 65 75 73 69 6e 67 20 70 50 67 2e 0a  ** reusing pPg..
12261 20 20 2a 2a 0a 20 20 2a 2a 20 53 69 6d 69 6c 61    **.  ** Simila
12262 72 6c 79 2c 20 69 66 20 74 68 65 20 70 61 67 65  rly, if the page
12263 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e  r has already en
12264 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 20  tered the error 
12265 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 0a 20 20  state, do not.  
12266 2a 2a 20 74 72 79 20 74 6f 20 77 72 69 74 65 20  ** try to write 
12267 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
12268 70 50 67 20 74 6f 20 64 69 73 6b 2e 0a 20 20 2a  pPg to disk..  *
12269 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  /.  if( pPager->
1226a 65 72 72 43 6f 64 65 20 7c 7c 20 28 70 50 61 67  errCode || (pPag
1226b 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 26 26  er->doNotSync &&
1226c 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
1226d 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a  R_NEED_SYNC) ){.
1226e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1226f 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
12270 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Sync the journal
12271 20 66 69 6c 65 20 69 66 20 72 65 71 75 69 72 65   file if require
12272 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d  d. */.  if( pPg-
12273 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
12274 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 72 63  D_SYNC ){.    rc
12275 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70   = syncJournal(p
12276 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
12277 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
12278 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
12279 63 20 26 26 20 0a 20 20 20 20 20 20 21 28 70 50  c && .      !(pP
1227a 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
1227b 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
1227c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26 26 0a  MODE_MEMORY) &&.
1227d 20 20 20 20 20 20 21 28 73 71 6c 69 74 65 33 4f        !(sqlite3O
1227e 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
1227f 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
12280 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  d)&SQLITE_IOCAP_
12281 53 41 46 45 5f 41 50 50 45 4e 44 29 0a 20 20 20  SAFE_APPEND).   
12282 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
12283 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20  ->nRec = 0;.    
12284 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72    rc = writeJour
12285 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a  nalHdr(pPager);.
12286 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
12287 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  If the page numb
12288 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  er of this page 
12289 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
1228a 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
1228b 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  of.  ** the data
1228c 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74 20 6d  base image, it m
1228d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 77 72  ay need to be wr
1228e 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62  itten to the sub
1228f 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54  -journal..  ** T
12290 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 74  his is because t
12291 68 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72  he call to pager
12292 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28  _write_pagelist(
12293 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74  ) below will not
12294 0a 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 77  .  ** actually w
12295 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65  rite data to the
12296 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61   file in this ca
12297 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f  se..  **.  ** Co
12298 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f  nsider the follo
12299 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66  wing sequence of
1229a 20 65 76 65 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20   events:.  **.  
1229b 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a  **   BEGIN;.  **
1229c 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61       <journal pa
1229d 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 3c  ge X>.  **     <
1229e 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e 0a 20  modify page X>. 
1229f 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e   **     SAVEPOIN
122a0 54 20 73 70 3b 0a 20 20 2a 2a 20 20 20 20 20 20  T sp;.  **      
122a1 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62 61 73   <shrink databas
122a2 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61 67 65  e file to Y page
122a3 73 3e 0a 20 20 2a 2a 20 20 20 20 20 20 20 70 61  s>.  **       pa
122a4 67 65 72 53 74 72 65 73 73 28 70 61 67 65 20 58  gerStress(page X
122a5 29 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42  ).  **     ROLLB
122a6 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a  ACK TO sp;.  **.
122a7 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 74    ** If (X>Y), t
122a8 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72 53 74  hen when pagerSt
122a9 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64 20 70  ress is called p
122aa 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62  age X will not b
122ab 65 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 6f  e written.  ** o
122ac 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
122ad 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c  se file, but wil
122ae 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f  l be dropped fro
122af 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65  m the cache. The
122b0 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  n,.  ** followin
122b1 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20  g the "ROLLBACK 
122b2 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65 6e 74  TO sp" statement
122b3 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 58  , reading page X
122b4 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 2a 2a 20   will read.  ** 
122b5 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61  data from the da
122b6 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69  tabase file. Thi
122b7 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 63 6f  s will be the co
122b8 70 79 20 6f 66 20 70 61 67 65 20 58 20 61 73 20  py of page X as 
122b9 69 74 0a 20 20 2a 2a 20 77 61 73 20 77 68 65 6e  it.  ** was when
122ba 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
122bb 20 73 74 61 72 74 65 64 2c 20 6e 6f 74 20 61 73   started, not as
122bc 20 69 74 20 77 61 73 20 77 68 65 6e 20 22 53 41   it was when "SA
122bd 56 45 50 4f 49 4e 54 20 73 70 22 0a 20 20 2a 2a  VEPOINT sp".  **
122be 20 77 61 73 20 65 78 65 63 75 74 65 64 2e 0a 20   was executed.. 
122bf 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 6f 6c   **.  ** The sol
122c0 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 69 74  ution is to writ
122c1 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61  e the current da
122c2 74 61 20 66 6f 72 20 70 61 67 65 20 58 20 69 6e  ta for page X in
122c3 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62  to the .  ** sub
122c4 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f  -journal file no
122c5 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20  w (if it is not 
122c6 61 6c 72 65 61 64 79 20 74 68 65 72 65 29 2c 20  already there), 
122c7 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 0a  so that it will.
122c8 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 65 64    ** be restored
122c9 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20   to its current 
122ca 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 22  value when the "
122cb 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20  ROLLBACK TO sp" 
122cc 69 73 20 0a 20 20 2a 2a 20 65 78 65 63 75 74 65  is .  ** execute
122cd 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  d..  */.  if( rc
122ce 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
122cf 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d  Pg->pgno>pPager-
122d0 3e 64 62 53 69 7a 65 20 26 26 20 73 75 62 6a 52  >dbSize && subjR
122d1 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29  equiresPage(pPg)
122d2 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 75 62   ){.    rc = sub
122d3 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29  journalPage(pPg)
122d4 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74  ;.  }..  /* Writ
122d5 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  e the contents o
122d6 66 20 74 68 65 20 70 61 67 65 20 6f 75 74 20 74  f the page out t
122d7 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
122d8 69 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63  ile. */.  if( rc
122d9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
122da 20 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d     pPg->pDirty =
122db 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   0;.    rc = pag
122dc 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73  er_write_pagelis
122dd 74 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f  t(pPg);.  }..  /
122de 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20  * Mark the page 
122df 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 69  as clean. */.  i
122e0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
122e1 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41   ){.    PAGERTRA
122e2 43 45 28 28 22 53 54 52 45 53 53 20 25 64 20 70  CE(("STRESS %d p
122e3 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  age %d\n", PAGER
122e4 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
122e5 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c  >pgno));.    sql
122e6 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c  ite3PcacheMakeCl
122e7 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20  ean(pPg);.  }.. 
122e8 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72   return pager_er
122e9 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b  ror(pPager, rc);
122ea 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .}.../*.** Alloc
122eb 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ate and initiali
122ec 7a 65 20 61 20 6e 65 77 20 50 61 67 65 72 20 6f  ze a new Pager o
122ed 62 6a 65 63 74 20 61 6e 64 20 70 75 74 20 61 20  bject and put a 
122ee 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a 2a  pointer to it.**
122ef 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 20 54 68   in *ppPager. Th
122f0 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64 20 65  e pager should e
122f1 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 66 72 65  ventually be fre
122f2 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 69 74  ed by passing it
122f3 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61  .** to sqlite3Pa
122f4 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a 2a  gerClose()..**.*
122f5 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20  * The zFilename 
122f6 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
122f7 70 61 74 68 20 74 6f 20 74 68 65 20 64 61 74 61  path to the data
122f8 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65  base file to ope
122f9 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61  n..** If zFilena
122fa 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  me is NULL then 
122fb 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65 64  a randomly-named
122fc 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
122fd 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e  is created.** an
122fe 64 20 75 73 65 64 20 61 73 20 74 68 65 20 66 69  d used as the fi
122ff 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64 2e  le to be cached.
12300 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   Temporary files
12301 20 61 72 65 20 62 65 20 64 65 6c 65 74 65 64 0a   are be deleted.
12302 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ** automatically
12303 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 63   when they are c
12304 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 6c 65 6e  losed. If zFilen
12305 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a  ame is ":memory:
12306 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20 69  " then .** all i
12307 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 65  nformation is he
12308 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 49 74 20  ld in cache. It 
12309 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e  is never written
1230a 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54 68   to disk. .** Th
1230b 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  is can be used t
1230c 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69  o implement an i
1230d 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
1230e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 78  e..**.** The nEx
1230f 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 73 70  tra parameter sp
12310 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62  ecifies the numb
12311 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
12312 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a  pace allocated.*
12313 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65 61 63  * along with eac
12314 68 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65  h page reference
12315 2e 20 54 68 69 73 20 73 70 61 63 65 20 69 73 20  . This space is 
12316 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65  available to the
12317 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 74 68 65   user.** via the
12318 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
12319 45 78 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a 0a  Extra() API..**.
1231a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67  ** The flags arg
1231b 75 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f  ument is used to
1231c 20 73 70 65 63 69 66 79 20 70 72 6f 70 65 72 74   specify propert
1231d 69 65 73 20 74 68 61 74 20 61 66 66 65 63 74 20  ies that affect 
1231e 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e  the.** operation
1231f 20 6f 66 20 74 68 65 20 70 61 67 65 72 2e 20 49   of the pager. I
12320 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73  t should be pass
12321 65 64 20 73 6f 6d 65 20 62 69 74 77 69 73 65 20  ed some bitwise 
12322 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f  combination.** o
12323 66 20 74 68 65 20 50 41 47 45 52 5f 4f 4d 49 54  f the PAGER_OMIT
12324 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41 47  _JOURNAL and PAG
12325 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 66  ER_NO_READLOCK f
12326 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lags..**.** The 
12327 76 66 73 46 6c 61 67 73 20 70 61 72 61 6d 65 74  vfsFlags paramet
12328 65 72 20 69 73 20 61 20 62 69 74 6d 61 73 6b 20  er is a bitmask 
12329 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 66  to pass to the f
1232a 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a 2a  lags parameter.*
1232b 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 28 29  * of the xOpen()
1232c 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 73   method of the s
1232d 75 70 70 6c 69 65 64 20 56 46 53 20 77 68 65 6e  upplied VFS when
1232e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e 20   opening files. 
1232f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
12330 67 65 72 20 6f 62 6a 65 63 74 20 69 73 20 61 6c  ger object is al
12331 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20  located and the 
12332 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 20 6f  specified file o
12333 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65 73  pened .** succes
12334 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f 4f  sfully, SQLITE_O
12335 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  K is returned an
12336 64 20 2a 70 70 50 61 67 65 72 20 73 65 74 20 74  d *ppPager set t
12337 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 68  o point to.** th
12338 65 20 6e 65 77 20 70 61 67 65 72 20 6f 62 6a 65  e new pager obje
12339 63 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ct. If an error 
1233a 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 67 65 72  occurs, *ppPager
1233b 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 0a   is set to NULL.
1233c 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 63 6f 64  ** and error cod
1233d 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73  e returned. This
1233e 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65   function may re
1233f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
12340 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 6c  M.** (sqlite3Mal
12341 6c 6f 63 28 29 20 69 73 20 75 73 65 64 20 74 6f  loc() is used to
12342 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   allocate memory
12343 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  ), SQLITE_CANTOP
12344 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f 75  EN or .** variou
12345 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 20  s SQLITE_IO_XXX 
12346 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  errors..*/.SQLIT
12347 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12348 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 0a  lite3PagerOpen(.
12349 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
1234a 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  Vfs,       /* Th
1234b 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73  e virtual file s
1234c 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f 0a  ystem to use */.
1234d 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65    Pager **ppPage
1234e 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  r,         /* OU
1234f 54 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50 61  T: Return the Pa
12350 67 65 72 20 73 74 72 75 63 74 75 72 65 20 68 65  ger structure he
12351 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  re */.  const ch
12352 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
12353 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
12354 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
12355 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e   open */.  int n
12356 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20  Extra,          
12357 20 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79 74      /* Extra byt
12358 65 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61 63  es append to eac
12359 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  h in-memory page
1235a 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c   */.  int flags,
1235b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1235c 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 6c  * flags controll
1235d 69 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a 2f  ing this file */
1235e 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20  .  int vfsFlags 
1235f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
12360 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f  lags passed thro
12361 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76  ugh to sqlite3_v
12362 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b  fs.xOpen() */.){
12363 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 50  .  u8 *pPtr;.  P
12364 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 30  ager *pPager = 0
12365 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72  ;       /* Pager
12366 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f 63   object to alloc
12367 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 2a  ate and return *
12368 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
12369 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20  ITE_OK;      /* 
1236a 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
1236b 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20   int tempFile = 
1236c 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  0;        /* Tru
1236d 65 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 73  e for temp files
1236e 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f 72   (incl. in-memor
1236f 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 6e  y files) */.  in
12370 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 20  t memDb = 0;    
12371 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
12372 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d  f this is an in-
12373 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a 20  memory file */. 
12374 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20   int readOnly = 
12375 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  0;        /* Tru
12376 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 72  e if this is a r
12377 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a 2f  ead-only file */
12378 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 69  .  int journalFi
12379 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 42  leSize;     /* B
1237a 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65  ytes to allocate
1237b 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e 61   for each journa
1237c 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  l fd */.  char *
1237d 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20  zPathname = 0;  
1237e 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 20     /* Full path 
1237f 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  to database file
12380 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 6e   */.  int nPathn
12381 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  ame = 0;       /
12382 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
12383 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 2a  s in zPathname *
12384 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e  /.  int useJourn
12385 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41  al = (flags & PA
12386 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c  GER_OMIT_JOURNAL
12387 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 74  )==0; /* False t
12388 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 2a  o omit journal *
12389 2f 0a 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c 6f  /.  int noReadlo
1238a 63 6b 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41  ck = (flags & PA
1238b 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 29  GER_NO_READLOCK)
1238c 21 3d 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f  !=0;  /* True to
1238d 20 6f 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b 20   omit read-lock 
1238e 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65 53  */.  int pcacheS
1238f 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63 61  ize = sqlite3Pca
12390 63 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20 20  cheSize();      
12391 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c   /* Bytes to all
12392 6f 63 61 74 65 20 66 6f 72 20 50 43 61 63 68 65  ocate for PCache
12393 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50 61 67 65   */.  u16 szPage
12394 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 45  Dflt = SQLITE_DE
12395 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b  FAULT_PAGE_SIZE;
12396 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 67    /* Default pag
12397 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a 20  e size */..  /* 
12398 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  Figure out how m
12399 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 71  uch space is req
1239a 75 69 72 65 64 20 66 6f 72 20 65 61 63 68 20 6a  uired for each j
1239b 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64  ournal file-hand
1239c 6c 65 0a 20 20 2a 2a 20 28 74 68 65 72 65 20 61  le.  ** (there a
1239d 72 65 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c 20  re two of them, 
1239e 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
1239f 20 61 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f 75   and the sub-jou
123a0 72 6e 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a 2a  rnal). This.  **
123a1 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
123a2 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66  space required f
123a3 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  or an in-memory 
123a4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e  journal file han
123a5 64 6c 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61 20  dle .  ** and a 
123a6 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 20  regular journal 
123a7 66 69 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f 74  file-handle. Not
123a8 65 20 74 68 61 74 20 61 20 22 72 65 67 75 6c 61  e that a "regula
123a9 72 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c 65  r journal-handle
123aa 22 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 61 20  ".  ** may be a 
123ab 77 72 61 70 70 65 72 20 63 61 70 61 62 6c 65 20  wrapper capable 
123ac 6f 66 20 63 61 63 68 69 6e 67 20 74 68 65 20 66  of caching the f
123ad 69 72 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20  irst portion of 
123ae 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a  the journal.  **
123af 20 66 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79 20   file in memory 
123b0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
123b1 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70   atomic-write op
123b2 74 69 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65 20  timization (see 
123b3 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20 66 69 6c  .  ** source fil
123b4 65 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20 20  e journal.c)..  
123b5 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
123b6 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73  JournalSize(pVfs
123b7 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72  )>sqlite3MemJour
123b8 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 20  nalSize() ){.   
123b9 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65   journalFileSize
123ba 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61   = sqlite3Journa
123bb 6c 53 69 7a 65 28 70 56 66 73 29 3b 0a 20 20 7d  lSize(pVfs);.  }
123bc 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61  else{.    journa
123bd 6c 46 69 6c 65 53 69 7a 65 20 3d 20 73 71 6c 69  lFileSize = sqli
123be 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a  te3MemJournalSiz
123bf 65 28 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  e();.  }..  /* S
123c0 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 61  et the output va
123c1 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20 69  riable to NULL i
123c2 6e 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72 20  n case an error 
123c3 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70 70  occurs. */.  *pp
123c4 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a  Pager = 0;..  /*
123c5 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f   Compute and sto
123c6 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  re the full path
123c7 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63  name in an alloc
123c8 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e  ated buffer poin
123c9 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a  ted.  ** to by z
123ca 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68  Pathname, length
123cb 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20   nPathname. Or, 
123cc 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d  if this is a tem
123cd 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a  porary file,.  *
123ce 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61  * leave both nPa
123cf 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68  thname and zPath
123d0 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20  name set to 0.. 
123d1 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e   */.  if( zFilen
123d2 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65  ame && zFilename
123d3 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68  [0] ){.    nPath
123d4 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50  name = pVfs->mxP
123d5 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a  athname+1;.    z
123d6 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74  Pathname = sqlit
123d7 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61  e3Malloc(nPathna
123d8 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a  me*2);.    if( z
123d9 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20  Pathname==0 ){. 
123da 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
123db 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
123dc 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
123dd 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20  MIT_MEMORYDB.   
123de 20 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c   if( strcmp(zFil
123df 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22  ename,":memory:"
123e0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65  )==0 ){.      me
123e1 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a  mDb = 1;.      z
123e2 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b  Pathname[0] = 0;
123e3 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  .    }else.#endi
123e4 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 7a 50  f.    {.      zP
123e5 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 20  athname[0] = 0; 
123e6 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 6e 69  /* Make sure ini
123e7 74 69 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 66  tialized even if
123e8 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20   FullPathname() 
123e9 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20 72  fails */.      r
123ea 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c  c = sqlite3OsFul
123eb 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20  lPathname(pVfs, 
123ec 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68  zFilename, nPath
123ed 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29  name, zPathname)
123ee 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50 61  ;.    }..    nPa
123ef 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  thname = sqlite3
123f0 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61  Strlen30(zPathna
123f1 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  me);.    if( rc=
123f2 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 50  =SQLITE_OK && nP
123f3 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d 3e  athname+8>pVfs->
123f4 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20  mxPathname ){.  
123f5 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e      /* This bran
123f6 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65 6e  ch is taken when
123f7 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 74   the journal pat
123f8 68 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 20  h required by.  
123f9 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62      ** the datab
123fa 61 73 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64  ase being opened
123fb 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 68   will be more th
123fc 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  an pVfs->mxPathn
123fd 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74  ame.      ** byt
123fe 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 68  es in length. Th
123ff 69 73 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74  is means the dat
12400 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20  abase cannot be 
12401 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a  opened,.      **
12402 20 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20   as it will not 
12403 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f  be possible to o
12404 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  pen the journal 
12405 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 20  file or even.   
12406 20 20 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 20     ** check for 
12407 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 65  a hot-journal be
12408 66 6f 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 20  fore reading..  
12409 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20      */.      rc 
1240a 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  = SQLITE_CANTOPE
1240b 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  N;.    }.    if(
1240c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1240d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1240e 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b  free(zPathname);
1240f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
12410 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
12411 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
12412 79 20 66 6f 72 20 74 68 65 20 50 61 67 65 72 20  y for the Pager 
12413 73 74 72 75 63 74 75 72 65 2c 20 50 43 61 63 68  structure, PCach
12414 65 20 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20 20  e object, the.  
12415 2a 2a 20 74 68 72 65 65 20 66 69 6c 65 20 64 65  ** three file de
12416 73 63 72 69 70 74 6f 72 73 2c 20 74 68 65 20 64  scriptors, the d
12417 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d  atabase file nam
12418 65 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61  e and the journa
12419 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61 6d  l .  ** file nam
1241a 65 2e 20 54 68 65 20 6c 61 79 6f 75 74 20 69 6e  e. The layout in
1241b 20 6d 65 6d 6f 72 79 20 69 73 20 61 73 20 66 6f   memory is as fo
1241c 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  llows:.  **.  **
1241d 20 20 20 20 20 50 61 67 65 72 20 6f 62 6a 65 63       Pager objec
1241e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
1241f 20 20 20 20 20 28 73 69 7a 65 6f 66 28 50 61 67       (sizeof(Pag
12420 65 72 29 20 62 79 74 65 73 29 0a 20 20 2a 2a 20  er) bytes).  ** 
12421 20 20 20 20 50 43 61 63 68 65 20 6f 62 6a 65 63      PCache objec
12422 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
12423 20 20 20 20 28 73 71 6c 69 74 65 33 50 63 61 63      (sqlite3Pcac
12424 68 65 53 69 7a 65 28 29 20 62 79 74 65 73 29 0a  heSize() bytes).
12425 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61 73    **     Databas
12426 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20  e file handle   
12427 20 20 20 20 20 20 20 20 20 28 70 56 66 73 2d 3e           (pVfs->
12428 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 29 0a  szOsFile bytes).
12429 20 20 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f 75    **     Sub-jou
1242a 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65  rnal file handle
1242b 20 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61           (journa
1242c 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29  lFileSize bytes)
1242d 0a 20 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20 6a  .  **     Main j
1242e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64  ournal file hand
1242f 6c 65 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e  le        (journ
12430 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73  alFileSize bytes
12431 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62  ).  **     Datab
12432 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 20 20  ase file name   
12433 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61 74             (nPat
12434 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 29 0a 20  hname+1 bytes). 
12435 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20   **     Journal 
12436 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20  file name       
12437 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e 61          (nPathna
12438 6d 65 2b 38 2b 31 20 62 79 74 65 73 29 0a 20 20  me+8+1 bytes).  
12439 2a 2f 0a 20 20 70 50 74 72 20 3d 20 28 75 38 20  */.  pPtr = (u8 
1243a 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  *)sqlite3MallocZ
1243b 65 72 6f 28 0a 20 20 20 20 73 69 7a 65 6f 66 28  ero(.    sizeof(
1243c 2a 70 50 61 67 65 72 29 20 2b 20 20 20 20 20 20  *pPager) +      
1243d 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73 74       /* Pager st
1243e 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 70  ructure */.    p
1243f 63 61 63 68 65 53 69 7a 65 20 20 20 20 20 20 2b  cacheSize      +
12440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43             /* PC
12441 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ache object */. 
12442 20 20 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c     pVfs->szOsFil
12443 65 20 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f  e  +           /
12444 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69  * The main db fi
12445 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61  le */.    journa
12446 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20  lFileSize * 2 + 
12447 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 77 6f        /* The two
12448 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a   journal files *
12449 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65  / .    nPathname
1244a 20 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20   + 1 +          
1244b 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20     /* zFilename 
1244c 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65  */.    nPathname
1244d 20 2b 20 38 20 2b 20 31 20 20 20 20 20 20 20 20   + 8 + 1        
1244e 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61 6c 20 2a     /* zJournal *
1244f 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 70 50  /.  );.  if( !pP
12450 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  tr ){.    sqlite
12451 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65  3_free(zPathname
12452 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
12453 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
12454 20 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20    pPager =      
12455 20 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29          (Pager*)
12456 28 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72  (pPtr);.  pPager
12457 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28  ->pPCache =    (
12458 50 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d  PCache*)(pPtr +=
12459 20 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29   sizeof(*pPager)
1245a 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64 20  );.  pPager->fd 
1245b 3d 20 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c  =   (sqlite3_fil
1245c 65 2a 29 28 70 50 74 72 20 2b 3d 20 70 63 61 63  e*)(pPtr += pcac
1245d 68 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65  heSize);.  pPage
1245e 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74  r->sjfd = (sqlit
1245f 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b  e3_file*)(pPtr +
12460 3d 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  = pVfs->szOsFile
12461 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 64  );.  pPager->jfd
12462 20 3d 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c   =  (sqlite3_fil
12463 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72  e*)(pPtr += jour
12464 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20  nalFileSize);.  
12465 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
12466 65 20 3d 20 20 20 20 28 63 68 61 72 2a 29 28 70  e =    (char*)(p
12467 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69  Ptr += journalFi
12468 6c 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 46  leSize);..  /* F
12469 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 67 65 72  ill in the Pager
1246a 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50  .zFilename and P
1246b 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75  ager.zJournal bu
1246c 66 66 65 72 73 2c 20 69 66 20 72 65 71 75 69 72  ffers, if requir
1246d 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61  ed. */.  if( zPa
1246e 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 70 50  thname ){.    pP
1246f 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d  ager->zJournal =
12470 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 72 20     (char*)(pPtr 
12471 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31  += nPathname + 1
12472 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50  );.    memcpy(pP
12473 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
12474 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74   zPathname, nPat
12475 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63  hname);.    memc
12476 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  py(pPager->zJour
12477 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20  nal, zPathname, 
12478 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  nPathname);.    
12479 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e  memcpy(&pPager->
1247a 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61  zJournal[nPathna
1247b 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c  me], "-journal",
1247c 20 38 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   8);.    sqlite3
1247d 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29  _free(zPathname)
1247e 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e  ;.  }.  pPager->
1247f 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 70  pVfs = pVfs;.  p
12480 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 20  Pager->vfsFlags 
12481 3d 20 76 66 73 46 6c 61 67 73 3b 0a 0a 20 20 2f  = vfsFlags;..  /
12482 2a 20 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72  * Open the pager
12483 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   file..  */.  if
12484 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a  ( zFilename && z
12485 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21  Filename[0] && !
12486 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 69 6e 74  memDb ){.    int
12487 20 66 6f 75 74 20 3d 20 30 3b 20 20 20 20 20 20   fout = 0;      
12488 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12489 20 56 46 53 20 66 6c 61 67 73 20 72 65 74 75 72   VFS flags retur
1248a 6e 65 64 20 62 79 20 78 4f 70 65 6e 28 29 20 2a  ned by xOpen() *
1248b 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
1248c 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70  e3OsOpen(pVfs, p
1248d 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65  Pager->zFilename
1248e 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 76 66  , pPager->fd, vf
1248f 73 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a  sFlags, &fout);.
12490 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28      readOnly = (
12491 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e  fout&SQLITE_OPEN
12492 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20 20 20  _READONLY);..   
12493 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20   /* If the file 
12494 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79  was successfully
12495 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
12496 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c 0a 20  /write access,. 
12497 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61 20 64     ** choose a d
12498 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65  efault page size
12499 20 69 6e 20 63 61 73 65 20 77 65 20 68 61 76 65   in case we have
1249a 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 20   to create the. 
1249b 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66     ** database f
1249c 69 6c 65 2e 20 54 68 65 20 64 65 66 61 75 6c 74  ile. The default
1249d 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 74 68   page size is th
1249e 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20  e maximum of:.  
1249f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b    **.    **    +
124a0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
124a1 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 2a  PAGE_SIZE,.    *
124a2 2a 20 20 20 20 2b 20 54 68 65 20 76 61 6c 75 65  *    + The value
124a3 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
124a4 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65  ite3OsSectorSize
124a5 28 29 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54  ().    **    + T
124a6 68 65 20 6c 61 72 67 65 73 74 20 70 61 67 65 20  he largest page 
124a7 73 69 7a 65 20 74 68 61 74 20 63 61 6e 20 62 65  size that can be
124a8 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61   written atomica
124a9 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  lly..    */.    
124aa 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
124ab 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c 79 20 29  K && !readOnly )
124ac 7b 0a 20 20 20 20 20 20 73 65 74 53 65 63 74 6f  {.      setSecto
124ad 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20  rSize(pPager);. 
124ae 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49       assert(SQLI
124af 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
124b0 53 49 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d 41 58  SIZE<=SQLITE_MAX
124b1 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
124b2 5a 45 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  ZE);.      if( s
124b3 7a 50 61 67 65 44 66 6c 74 3c 70 50 61 67 65 72  zPageDflt<pPager
124b4 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 29 7b 0a  ->sectorSize ){.
124b5 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67          if( pPag
124b6 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3e 53  er->sectorSize>S
124b7 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c  QLITE_MAX_DEFAUL
124b8 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20  T_PAGE_SIZE ){. 
124b9 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44           szPageD
124ba 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58  flt = SQLITE_MAX
124bb 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
124bc 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ZE;.        }els
124bd 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50  e{.          szP
124be 61 67 65 44 66 6c 74 20 3d 20 28 75 31 36 29 70  ageDflt = (u16)p
124bf 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
124c0 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
124c1 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
124c2 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43  TE_ENABLE_ATOMIC
124c3 5f 57 52 49 54 45 0a 20 20 20 20 20 20 7b 0a 20  _WRITE.      {. 
124c4 20 20 20 20 20 20 20 69 6e 74 20 69 44 63 20 3d         int iDc =
124c5 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65   sqlite3OsDevice
124c6 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
124c7 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20  pPager->fd);.   
124c8 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20       int ii;.   
124c9 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49       assert(SQLI
124ca 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35  TE_IOCAP_ATOMIC5
124cb 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20  12==(512>>8));. 
124cc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51         assert(SQ
124cd 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
124ce 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29  C64K==(65536>>8)
124cf 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
124d0 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  t(SQLITE_MAX_DEF
124d1 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d  AULT_PAGE_SIZE<=
124d2 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20 20  65536);.        
124d3 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 44 66 6c  for(ii=szPageDfl
124d4 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 5f 4d 41  t; ii<=SQLITE_MA
124d5 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
124d6 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 7b 0a 20  IZE; ii=ii*2){. 
124d7 20 20 20 20 20 20 20 20 20 69 66 28 20 69 44 63           if( iDc
124d8 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  &(SQLITE_IOCAP_A
124d9 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 29 20 29  TOMIC|(ii>>8)) )
124da 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 7a  {.            sz
124db 50 61 67 65 44 66 6c 74 20 3d 20 69 69 3b 0a 20  PageDflt = ii;. 
124dc 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
124dd 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e     }.      }.#en
124de 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  dif.    }.  }els
124df 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74  e{.    /* If a t
124e0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73  emporary file is
124e1 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69   requested, it i
124e2 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d  s not opened imm
124e3 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a  ediately..    **
124e4 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65   In this case we
124e5 20 61 63 63 65 70 74 20 74 68 65 20 64 65 66 61   accept the defa
124e6 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e  ult page size an
124e7 64 20 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79  d delay actually
124e8 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20  .    ** opening 
124e9 74 68 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74  the file until t
124ea 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f  he first call to
124eb 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20   OsWrite()..    
124ec 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62  **.    ** This b
124ed 72 61 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75  ranch is also ru
124ee 6e 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  n for an in-memo
124ef 72 79 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20  ry database. An 
124f0 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a  in-memory.    **
124f1 20 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65   database is the
124f2 20 73 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d   same as a temp-
124f3 66 69 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76  file that is nev
124f4 65 72 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74  er written out t
124f5 6f 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e  o.    ** disk an
124f6 64 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d  d uses an in-mem
124f7 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  ory rollback jou
124f8 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20  rnal..    */ .  
124f9 20 20 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a    tempFile = 1;.
124fa 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
124fb 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53  e = PAGER_EXCLUS
124fc 49 56 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  IVE;.  }..  /* T
124fd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c  he following cal
124fe 6c 20 74 6f 20 50 61 67 65 72 53 65 74 50 61 67  l to PagerSetPag
124ff 65 73 69 7a 65 28 29 20 73 65 72 76 65 73 20 74  esize() serves t
12500 6f 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  o set the value 
12501 6f 66 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 70  of .  ** Pager.p
12502 61 67 65 53 69 7a 65 20 61 6e 64 20 74 6f 20 61  ageSize and to a
12503 6c 6c 6f 63 61 74 65 20 74 68 65 20 50 61 67 65  llocate the Page
12504 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 66  r.pTmpSpace buff
12505 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  er..  */.  if( r
12506 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12507 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
12508 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29 3b 0a  er->memDb==0 );.
12509 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1250a 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
1250b 28 70 50 61 67 65 72 2c 20 26 73 7a 50 61 67 65  (pPager, &szPage
1250c 44 66 6c 74 29 3b 0a 20 20 20 20 74 65 73 74 63  Dflt);.    testc
1250d 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
1250e 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  OK );.  }..  /* 
1250f 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
12510 72 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6f  rred in either o
12511 66 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62 6f  f the blocks abo
12512 76 65 2c 20 66 72 65 65 20 74 68 65 20 0a 20 20  ve, free the .  
12513 2a 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75  ** Pager structu
12514 72 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65  re and close the
12515 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   file..  */.  if
12516 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
12517 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
12518 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
12519 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  e );.    sqlite3
1251a 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
1251b 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  fd);.    sqlite3
1251c 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20  _free(pPager);. 
1251d 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1251e 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  }..  /* Initiali
1251f 7a 65 20 74 68 65 20 50 43 61 63 68 65 20 6f 62  ze the PCache ob
12520 6a 65 63 74 2e 20 2a 2f 0a 20 20 6e 45 78 74 72  ject. */.  nExtr
12521 61 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 74 72  a = ROUND8(nExtr
12522 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61  a);.  sqlite3Pca
12523 63 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 44 66  cheOpen(szPageDf
12524 6c 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d 65 6d  lt, nExtra, !mem
12525 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Db,.            
12526 20 20 20 20 20 20 20 20 21 6d 65 6d 44 62 3f 70          !memDb?p
12527 61 67 65 72 53 74 72 65 73 73 3a 30 2c 20 28 76  agerStress:0, (v
12528 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 50  oid *)pPager, pP
12529 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
1252a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
1252b 4f 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20 46  OPEN %d %s\n", F
1252c 49 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 61 67  ILEHANDLEID(pPag
1252d 65 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 72 2d  er->fd), pPager-
1252e 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 20  >zFilename));.  
1252f 49 4f 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25  IOTRACE(("OPEN %
12530 70 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %s\n", pPager,
12531 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
12532 6d 65 29 29 0a 0a 20 20 70 50 61 67 65 72 2d 3e  me))..  pPager->
12533 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38  useJournal = (u8
12534 29 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70  )useJournal;.  p
12535 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63  Pager->noReadloc
12536 6b 20 3d 20 28 6e 6f 52 65 61 64 6c 6f 63 6b 20  k = (noReadlock 
12537 26 26 20 72 65 61 64 4f 6e 6c 79 29 20 3f 31 3a  && readOnly) ?1:
12538 30 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  0;.  /* pPager->
12539 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f  stmtOpen = 0; */
1253a 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74  .  /* pPager->st
1253b 6d 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0a  mtInUse = 0; */.
1253c 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65    /* pPager->nRe
1253d 66 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  f = 0; */.  pPag
1253e 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
1253f 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 2f  = (u8)memDb;.  /
12540 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69  * pPager->stmtSi
12541 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20  ze = 0; */.  /* 
12542 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a  pPager->stmtJSiz
12543 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70  e = 0; */.  /* p
12544 50 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30  Pager->nPage = 0
12545 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6d  ; */.  pPager->m
12546 78 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d  xPgno = SQLITE_M
12547 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20  AX_PAGE_COUNT;. 
12548 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74   /* pPager->stat
12549 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  e = PAGER_UNLOCK
1254a 3b 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  ; */.  assert( p
1254b 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20  Pager->state == 
1254c 28 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45  (tempFile ? PAGE
1254d 52 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41  R_EXCLUSIVE : PA
1254e 47 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20  GER_UNLOCK) );. 
1254f 20 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d   /* pPager->errM
12550 61 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50  ask = 0; */.  pP
12551 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d  ager->tempFile =
12552 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a 20   (u8)tempFile;. 
12553 20 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c   assert( tempFil
12554 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
12555 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20  MODE_NORMAL .   
12556 20 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69         || tempFi
12557 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  le==PAGER_LOCKIN
12558 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
12559 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
1255a 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
1255b 58 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20  XCLUSIVE==1 );. 
1255c 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
1255d 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 6d  veMode = (u8)tem
1255e 70 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 72  pFile; .  pPager
1255f 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e  ->changeCountDon
12560 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  e = pPager->temp
12561 46 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e  File;.  pPager->
12562 6d 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d 44  memDb = (u8)memD
12563 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 61  b;.  pPager->rea
12564 64 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61 64  dOnly = (u8)read
12565 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65  Only;.  /* pPage
12566 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  r->needSync = 0;
12567 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f   */.  pPager->no
12568 53 79 6e 63 20 3d 20 28 70 50 61 67 65 72 2d 3e  Sync = (pPager->
12569 74 65 6d 70 46 69 6c 65 20 7c 7c 20 21 75 73 65  tempFile || !use
1256a 4a 6f 75 72 6e 61 6c 29 20 3f 31 3a 30 3b 0a 20  Journal) ?1:0;. 
1256b 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
1256c 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79  c = pPager->noSy
1256d 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 65  nc ?0:1;.  pPage
1256e 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20  r->sync_flags = 
1256f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
12570 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  AL;.  /* pPager-
12571 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a  >pFirst = 0; */.
12572 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69    /* pPager->pFi
12573 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a  rstSynced = 0; *
12574 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70  /.  /* pPager->p
12575 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70  Last = 0; */.  p
12576 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20  Pager->nExtra = 
12577 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 67 65 72  nExtra;.  pPager
12578 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  ->journalSizeLim
12579 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  it = SQLITE_DEFA
1257a 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45  ULT_JOURNAL_SIZE
1257b 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 65 72 74  _LIMIT;.  assert
1257c 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
1257d 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46 69 6c 65  >fd) || tempFile
1257e 20 29 3b 0a 20 20 73 65 74 53 65 63 74 6f 72 53   );.  setSectorS
1257f 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69  ize(pPager);.  i
12580 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20  f( memDb ){.    
12581 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
12582 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52  ode = PAGER_JOUR
12583 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a  NALMODE_MEMORY;.
12584 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d    }.  /* pPager-
12585 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20  >xBusyHandler = 
12586 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
12587 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41  r->pBusyHandlerA
12588 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20  rg = 0; */.  /* 
12589 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61  memset(pPager->a
1258a 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Hash, 0, sizeof(
1258b 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b  pPager->aHash));
1258c 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d   */.  *ppPager =
1258d 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72   pPager;.  retur
1258e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1258f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
12590 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
12591 61 66 74 65 72 20 74 72 61 6e 73 69 74 69 6f 6e  after transition
12592 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52 5f 55  ing from PAGER_U
12593 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 47 45  NLOCK to.** PAGE
12594 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 2e 20  R_SHARED state. 
12595 49 74 20 74 65 73 74 73 20 69 66 20 74 68 65 72  It tests if ther
12596 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e  e is a hot journ
12597 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a  al present in.**
12598 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
12599 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70   for the given p
1259a 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72  ager. A hot jour
1259b 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20  nal is one that 
1259c 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20  .** needs to be 
1259d 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63  played back. Acc
1259e 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66  ording to this f
1259f 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a  unction, a hot-j
125a0 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 65  ournal.** file e
125a1 78 69 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c  xists if the fol
125a2 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 20  lowing criteria 
125a3 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a 20 20  are met:.**.**  
125a4 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66   * The journal f
125a5 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20 74 68  ile exists in th
125a6 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 61  e file system, a
125a7 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 72 6f  nd.**   * No pro
125a8 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53  cess holds a RES
125a9 45 52 56 45 44 20 6f 72 20 67 72 65 61 74 65 72  ERVED or greater
125aa 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
125ab 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 0a  abase file, and.
125ac 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62  **   * The datab
125ad 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20  ase file itself 
125ae 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
125af 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c  0 bytes in size,
125b0 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20   and.**   * The 
125b1 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
125b2 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  e journal file e
125b3 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74  xists and is not
125b4 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   0x00..**.** If 
125b5 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65  the current size
125b6 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
125b7 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 20 61   file is 0 but a
125b8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a   journal file.**
125b9 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 69 73   exists, that is
125ba 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64   probably an old
125bb 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76   journal left ov
125bc 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a  er from a prior.
125bd 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ** database with
125be 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20   the same name. 
125bf 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
125c0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
125c1 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74 65 64  .** just deleted
125c2 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74 65 2c   using OsDelete,
125c3 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65 74   *pExists is set
125c4 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45   to 0 and SQLITE
125c5 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  _OK.** is return
125c6 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
125c7 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20  outine does not 
125c8 63 68 65 63 6b 20 69 66 20 74 68 65 72 65 20 69  check if there i
125c9 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  s a master journ
125ca 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 61  al filename.** a
125cb 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
125cc 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20   file. If there 
125cd 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73  is, and that mas
125ce 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
125cf 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  .** does not exi
125d0 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75  st, then the jou
125d1 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74  rnal file is not
125d2 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20   really hot. In 
125d3 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 69  this.** case thi
125d4 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72  s routine will r
125d5 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f  eturn a false-po
125d6 73 69 74 69 76 65 2e 20 54 68 65 20 70 61 67 65  sitive. The page
125d7 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a 2a 20  r_playback().** 
125d8 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73  routine will dis
125d9 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 20 6a  cover that the j
125da 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
125db 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20 61 6e  ot really hot an
125dc 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72  d .** will not r
125dd 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a  oll it back. .**
125de 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a 6f 75  .** If a hot-jou
125df 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 6f 75  rnal file is fou
125e0 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a 70 45  nd to exist, *pE
125e1 78 69 73 74 73 20 69 73 20 73 65 74 20 74 6f 20  xists is set to 
125e2 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 54 45  1 and .** SQLITE
125e3 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66  _OK returned. If
125e4 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20   no hot-journal 
125e5 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c  file is present,
125e6 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a 2a 20   *pExists is.** 
125e7 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c  set to 0 and SQL
125e8 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e  ITE_OK returned.
125e9 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
125ea 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79  occurs while try
125eb 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d  ing.** to determ
125ec 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
125ed 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  ot a hot-journal
125ee 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 74 68   file exists, th
125ef 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 63 6f  e IO error.** co
125f0 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  de is returned a
125f1 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  nd the value of 
125f2 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e 64 65  *pExists is unde
125f3 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  fined..*/.static
125f4 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 6e   int hasHotJourn
125f5 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  al(Pager *pPager
125f6 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 29 7b  , int *pExists){
125f7 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
125f8 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50   const pVfs = pP
125f9 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e  ager->pVfs;.  in
125fa 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
125fb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
125fc 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
125fd 69 6e 74 20 65 78 69 73 74 73 3b 20 20 20 20 20  int exists;     
125fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
125ff 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75 72 6e   True if a journ
12600 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65  al file is prese
12601 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  nt */..  assert(
12602 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20   pPager!=0 );.  
12603 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
12604 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20  useJournal );.  
12605 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
12606 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20  Pager->fd) );.  
12607 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28  assert( !isOpen(
12608 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a  pPager->jfd) );.
12609 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b  .  *pExists = 0;
1260a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
1260b 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 50  sAccess(pVfs, pP
1260c 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
1260d 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
1260e 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a  ISTS, &exists);.
1260f 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12610 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 7b  _OK && exists ){
12611 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65 64 3b  .    int locked;
12612 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12613 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f 6d 65   /* True if some
12614 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61   process holds a
12615 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a   RESERVED lock *
12616 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
12617 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
12618 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64  dLock(pPager->fd
12619 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20  , &locked);.    
1261a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1261b 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a  K && !locked ){.
1261c 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b        int nPage;
1261d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b  ..      /* Check
1261e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1261f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
12620 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 20 6f  If it consists o
12621 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 20 20  f 0 pages,.     
12622 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 65 20   ** then delete 
12623 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12624 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72  . See the header
12625 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 66   comment above f
12626 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  or .      ** the
12627 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 65 2e   reasoning here.
12628 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
12629 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1262a 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  rPagecount(pPage
1262b 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20  r, &nPage);.    
1262c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1262d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69  _OK ){.        i
1262e 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20  f( nPage==0 ){. 
1262f 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
12630 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56  lite3OsDelete(pV
12631 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  fs, pPager->zJou
12632 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  rnal, 0);.      
12633 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
12634 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61     /* The journa
12635 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e  l file exists an
12636 64 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65  d no other conne
12637 63 74 69 6f 6e 20 68 61 73 20 61 20 72 65 73 65  ction has a rese
12638 72 76 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a  rved.          *
12639 2a 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63  * or greater loc
1263a 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
1263b 65 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68 65 63  e file. Now chec
1263c 6b 20 74 68 61 74 20 74 68 65 72 65 20 69 73 0a  k that there is.
1263d 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20            ** at 
1263e 6c 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a 65  least one non-ze
1263f 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20  ro bytes at the 
12640 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75  start of the jou
12641 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20  rnal file..     
12642 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65       ** If there
12643 20 69 73 2c 20 74 68 65 6e 20 77 65 20 63 6f 6e   is, then we con
12644 73 69 64 65 72 20 74 68 69 73 20 6a 6f 75 72 6e  sider this journ
12645 61 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20 49 66  al to be hot. If
12646 20 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20 20 20   not, .         
12647 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 69 67   ** it can be ig
12648 6e 6f 72 65 64 2e 0a 20 20 20 20 20 20 20 20 20  nored..         
12649 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e   */.          in
1264a 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45  t f = SQLITE_OPE
1264b 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54  N_READONLY|SQLIT
1264c 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
1264d 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72  NAL;.          r
1264e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
1264f 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  n(pVfs, pPager->
12650 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
12651 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b 0a 20  ->jfd, f, &f);. 
12652 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d           if( rc=
12653 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12654 20 20 20 20 20 20 20 20 20 20 75 38 20 66 69 72            u8 fir
12655 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  st = 0;.        
12656 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12657 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a  OsRead(pPager->j
12658 66 64 2c 20 28 76 6f 69 64 20 2a 29 26 66 69 72  fd, (void *)&fir
12659 73 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20  st, 1, 0);.     
1265a 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
1265b 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
1265c 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20  T_READ ){.      
1265d 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1265e 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20  ITE_OK;.        
1265f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
12660 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
12661 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
12662 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 78 69             *pExi
12663 73 74 73 20 3d 20 28 66 69 72 73 74 21 3d 30 29  sts = (first!=0)
12664 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
12665 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
12666 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
12667 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
12668 20 52 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e   Read the conten
12669 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 20 6f  t for page pPg o
1266a 75 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ut of the databa
1266b 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 74 6f  se file and into
1266c 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 61 74 61 2e   .** pPg->pData.
1266d 20 41 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f   A shared lock o
1266e 72 20 67 72 65 61 74 65 72 20 6d 75 73 74 20 62  r greater must b
1266f 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61  e held on the da
12670 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 62  tabase.** file b
12671 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
12672 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ion is called..*
12673 2a 0a 2a 2a 20 49 66 20 70 61 67 65 20 31 20 69  *.** If page 1 i
12674 73 20 72 65 61 64 2c 20 74 68 65 6e 20 74 68 65  s read, then the
12675 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e   value of Pager.
12676 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20  dbFileVers[] is 
12677 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61  set to.** the va
12678 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68  lue read from th
12679 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1267a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20  .**.** If an IO 
1267b 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
1267c 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20  en the IO error 
1267d 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
1267e 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 4f 74  he caller..** Ot
1267f 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f  herwise, SQLITE_
12680 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
12681 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
12682 61 64 44 62 50 61 67 65 28 50 67 48 64 72 20 2a  adDbPage(PgHdr *
12683 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  pPg){.  Pager *p
12684 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
12685 67 65 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62  ger; /* Pager ob
12686 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 20  ject associated 
12687 77 69 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f  with page pPg */
12688 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70  .  Pgno pgno = p
12689 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20  Pg->pgno;       
1268a 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74  /* Page number t
1268b 6f 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20  o read */.  int 
1268c 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
1268d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
1268e 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 36 34  rn code */.  i64
1268f 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
12690 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
12691 65 20 6f 66 66 73 65 74 20 6f 66 20 66 69 6c 65  e offset of file
12692 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f   to read from */
12693 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
12694 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
12695 5f 53 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44  _SHARED && !MEMD
12696 42 20 29 3b 0a 0a 20 20 69 66 28 20 21 69 73 4f  B );..  if( !isO
12697 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
12698 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
12699 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
1269a 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50  );.    memset(pP
1269b 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61  g->pData, 0, pPa
1269c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  ger->pageSize);.
1269d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1269e 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f 66 66  E_OK;.  }.  iOff
1269f 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28  set = (pgno-1)*(
126a0 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65  i64)pPager->page
126a1 53 69 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Size;.  rc = sql
126a2 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65  ite3OsRead(pPage
126a3 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 74  r->fd, pPg->pDat
126a4 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
126a5 69 7a 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20  ize, iOffset);. 
126a6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
126a7 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
126a8 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
126a9 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
126aa 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20  ( pgno==1 ){.   
126ab 20 75 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20   u8 *dbFileVers 
126ac 3d 20 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44  = &((u8*)pPg->pD
126ad 61 74 61 29 5b 32 34 5d 3b 0a 20 20 20 20 6d 65  ata)[24];.    me
126ae 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62  mcpy(&pPager->db
126af 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65  FileVers, dbFile
126b0 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61  Vers, sizeof(pPa
126b1 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29  ger->dbFileVers)
126b2 29 3b 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28  );.  }.  CODEC1(
126b3 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61  pPager, pPg->pDa
126b4 74 61 2c 20 70 67 6e 6f 2c 20 33 29 3b 0a 0a 20  ta, pgno, 3);.. 
126b5 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69   PAGER_INCR(sqli
126b6 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62  te3_pager_readdb
126b7 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 52  _count);.  PAGER
126b8 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 52  _INCR(pPager->nR
126b9 65 61 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 28  ead);.  IOTRACE(
126ba 28 22 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 2c  ("PGIN %p %d\n",
126bb 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b   pPager, pgno));
126bc 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
126bd 46 45 54 43 48 20 25 64 20 70 61 67 65 20 25 64  FETCH %d page %d
126be 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a   hash(%08x)\n",.
126bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
126c0 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
126c1 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65  pgno, pager_page
126c2 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 20  hash(pPg)));..  
126c3 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
126c4 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
126c5 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
126c6 65 76 65 72 20 74 68 65 20 75 70 70 65 72 20 6c  ever the upper l
126c7 61 79 65 72 20 72 65 71 75 65 73 74 73 20 61 20  ayer requests a 
126c8 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 65  database.** page
126c9 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 62   is requested, b
126ca 65 66 6f 72 65 20 74 68 65 20 63 61 63 68 65 20  efore the cache 
126cb 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72 20 61  is checked for a
126cc 20 73 75 69 74 61 62 6c 65 20 70 61 67 65 0a 2a   suitable page.*
126cd 2a 20 6f 72 20 61 6e 79 20 64 61 74 61 20 69 73  * or any data is
126ce 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64   read from the d
126cf 61 74 61 62 61 73 65 2e 20 49 74 20 70 65 72 66  atabase. It perf
126d0 6f 72 6d 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69  orms the followi
126d1 6e 67 0a 2a 2a 20 74 77 6f 20 66 75 6e 63 74 69  ng.** two functi
126d2 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  ons:.**.**   1) 
126d3 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
126d4 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 50 41 47  currently in PAG
126d5 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20  ER_UNLOCK state 
126d6 28 6e 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a  (no lock held.**
126d7 20 20 20 20 20 20 6f 6e 20 74 68 65 20 64 61 74        on the dat
126d8 61 62 61 73 65 20 66 69 6c 65 29 2c 20 74 68 65  abase file), the
126d9 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  n an attempt is 
126da 6d 61 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61  made to obtain a
126db 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 44 20  .**      SHARED 
126dc 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
126dd 62 61 73 65 20 66 69 6c 65 2e 20 49 6d 6d 65 64  base file. Immed
126de 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f 62 74  iately after obt
126df 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74  aining.**      t
126e0 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20  he SHARED lock, 
126e1 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
126e2 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72 20 61  is checked for a
126e3 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a   hot-journal,.**
126e4 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 70        which is p
126e5 6c 61 79 65 64 20 62 61 63 6b 20 69 66 20 70 72  layed back if pr
126e6 65 73 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67  esent. Following
126e7 20 61 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c   any hot-journal
126e8 20 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61   .**      rollba
126e9 63 6b 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ck, the contents
126ea 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 61 72   of the cache ar
126eb 65 20 76 61 6c 69 64 61 74 65 64 20 62 79 20 63  e validated by c
126ec 68 65 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20  hecking.**      
126ed 74 68 65 20 27 63 68 61 6e 67 65 2d 63 6f 75 6e  the 'change-coun
126ee 74 65 72 27 20 66 69 65 6c 64 20 6f 66 20 74 68  ter' field of th
126ef 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
126f0 68 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20 20 20  header and.**   
126f1 20 20 20 64 69 73 63 61 72 64 65 64 20 69 66 20     discarded if 
126f2 74 68 65 79 20 61 72 65 20 66 6f 75 6e 64 20 74  they are found t
126f3 6f 20 62 65 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a  o be invalid..**
126f4 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 65 20  .**   2) If the 
126f5 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67  pager is running
126f6 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f   in exclusive-mo
126f7 64 65 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72  de, and there ar
126f8 65 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 20  e currently.**  
126f9 20 20 20 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69      no outstandi
126fa 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ng references to
126fb 20 61 6e 79 20 70 61 67 65 73 2c 20 61 6e 64 20   any pages, and 
126fc 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20  is in the error 
126fd 73 74 61 74 65 2c 0a 2a 2a 20 20 20 20 20 20 74  state,.**      t
126fe 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69  hen an attempt i
126ff 73 20 6d 61 64 65 20 74 6f 20 63 6c 65 61 72 20  s made to clear 
12700 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20  the error state 
12701 62 79 20 64 69 73 63 61 72 64 69 6e 67 0a 2a 2a  by discarding.**
12702 20 20 20 20 20 20 74 68 65 20 63 6f 6e 74 65 6e        the conten
12703 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 63  ts of the page c
12704 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 69 6e 67  ache and rolling
12705 20 62 61 63 6b 20 61 6e 79 20 6f 70 65 6e 20 6a   back any open j
12706 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 66  ournal.**      f
12707 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ile..**.** If th
12708 65 20 6f 70 65 72 61 74 69 6f 6e 20 64 65 73 63  e operation desc
12709 72 69 62 65 64 20 62 79 20 28 32 29 20 61 62 6f  ribed by (2) abo
1270a 76 65 20 69 73 20 6e 6f 74 20 61 74 74 65 6d 70  ve is not attemp
1270b 74 65 64 2c 20 61 6e 64 20 69 66 20 74 68 65 0a  ted, and if the.
1270c 2a 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20 61  ** pager is in a
1270d 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74  n error state ot
1270e 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
1270f 46 55 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 69  FULL when this i
12710 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65  s called,.** the
12711 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72   error state err
12712 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
12713 6e 65 64 2e 20 49 74 20 69 73 20 70 65 72 6d 69  ned. It is permi
12714 74 74 65 64 20 74 6f 20 72 65 61 64 20 74 68 65  tted to read the
12715 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 68 65  .** database whe
12716 6e 20 69 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  n in SQLITE_FULL
12717 20 65 72 72 6f 72 20 73 74 61 74 65 2e 0a 2a 2a   error state..**
12718 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69  .** Otherwise, i
12719 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20  f everything is 
1271a 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49  successful, SQLI
1271b 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
1271c 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 49 4f 20 65  d. If an.** IO e
1271d 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
1271e 65 20 6c 6f 63 6b 69 6e 67 20 74 68 65 20 64 61  e locking the da
1271f 74 61 62 61 73 65 2c 20 63 68 65 63 6b 69 6e 67  tabase, checking
12720 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e   for a hot-journ
12721 61 6c 0a 2a 2a 20 66 69 6c 65 20 6f 72 20 72 6f  al.** file or ro
12722 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75  lling back a jou
12723 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 49  rnal file, the I
12724 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
12725 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
12726 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 68 61  tic int pagerSha
12727 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70  redLock(Pager *p
12728 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
12729 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
1272a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1272b 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
1272c 20 69 6e 74 20 69 73 45 72 72 6f 72 52 65 73 65   int isErrorRese
1272d 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
1272e 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72      /* True if r
1272f 65 63 6f 76 65 72 69 6e 67 20 66 72 6f 6d 20 65  ecovering from e
12730 72 72 6f 72 20 73 74 61 74 65 20 2a 2f 0a 0a 20  rror state */.. 
12731 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61   /* If this data
12732 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66  base is opened f
12733 6f 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63  or exclusive acc
12734 65 73 73 2c 20 68 61 73 20 6e 6f 20 6f 75 74 73  ess, has no outs
12735 74 61 6e 64 69 6e 67 20 0a 20 20 2a 2a 20 70 61  tanding .  ** pa
12736 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e  ge references an
12737 64 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72  d is in an error
12738 2d 73 74 61 74 65 2c 20 74 68 69 73 20 69 73 20  -state, this is 
12739 61 20 63 68 61 6e 63 65 20 74 6f 20 63 6c 65 61  a chance to clea
1273a 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72  r.  ** the error
1273b 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f  . Discard the co
1273c 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
1273d 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20 74 72  ger-cache and tr
1273e 65 61 74 20 61 6e 79 0a 20 20 2a 2a 20 6f 70 65  eat any.  ** ope
1273f 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  n journal file a
12740 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2e  s a hot-journal.
12741 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4d 45 4d  .  */.  if( !MEM
12742 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78  DB && pPager->ex
12743 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20  clusiveMode .   
12744 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  && sqlite3Pcache
12745 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
12746 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 26 26 20  >pPCache)==0 && 
12747 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
12748 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73  .  ){.    if( is
12749 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
1274a 29 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 72  ) ){.      isErr
1274b 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 20  orReset = 1;.   
1274c 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65   }.    pPager->e
1274d 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  rrCode = SQLITE_
1274e 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 65  OK;.    pager_re
1274f 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  set(pPager);.  }
12750 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
12751 67 65 72 20 69 73 20 73 74 69 6c 6c 20 69 6e 20  ger is still in 
12752 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  an error state, 
12753 64 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64 2e 20  do not proceed. 
12754 54 68 65 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20  The error .  ** 
12755 73 74 61 74 65 20 77 69 6c 6c 20 62 65 20 63 6c  state will be cl
12756 65 61 72 65 64 20 61 74 20 73 6f 6d 65 20 70 6f  eared at some po
12757 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72  int in the futur
12758 65 20 77 68 65 6e 20 61 6c 6c 20 70 61 67 65 20  e when all page 
12759 0a 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73  .  ** references
1275a 20 61 72 65 20 64 72 6f 70 70 65 64 20 61 6e 64   are dropped and
1275b 20 74 68 65 20 63 61 63 68 65 20 63 61 6e 20 62   the cache can b
1275c 65 20 64 69 73 63 61 72 64 65 64 2e 0a 20 20 2a  e discarded..  *
1275d 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  /.  if( pPager->
1275e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65  errCode && pPage
1275f 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49  r->errCode!=SQLI
12760 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72  TE_FULL ){.    r
12761 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
12762 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 69 66  rCode;.  }..  if
12763 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  ( pPager->state=
12764 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c  =PAGER_UNLOCK ||
12765 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b   isErrorReset ){
12766 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
12767 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20   * const pVfs = 
12768 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20  pPager->pVfs;.  
12769 20 20 69 6e 74 20 69 73 48 6f 74 4a 6f 75 72 6e    int isHotJourn
1276a 61 6c 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65  al = 0;.    asse
1276b 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20  rt( !MEMDB );.  
1276c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1276d 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
1276e 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
1276f 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21  ==0 );.    if( !
12770 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f  pPager->noReadlo
12771 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ck ){.      rc =
12772 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
12773 6f 63 6b 28 70 50 61 67 65 72 2c 20 53 48 41 52  ock(pPager, SHAR
12774 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  ED_LOCK);.      
12775 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12776 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  K ){.        ass
12777 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
12778 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  te==PAGER_UNLOCK
12779 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   );.        retu
1277a 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70  rn pager_error(p
1277b 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20  Pager, rc);.    
1277c 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
1277d 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  ( pPager->state=
1277e 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b  =PAGER_UNLOCK ){
1277f 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73  .      pPager->s
12780 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41  tate = PAGER_SHA
12781 52 45 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  RED;.    }.    a
12782 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
12783 74 61 74 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43  tate>=SHARED_LOC
12784 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  K );..    /* If 
12785 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  a journal file e
12786 78 69 73 74 73 2c 20 61 6e 64 20 74 68 65 72 65  xists, and there
12787 20 69 73 20 6e 6f 20 52 45 53 45 52 56 45 44 20   is no RESERVED 
12788 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20  lock on the.    
12789 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
1278a 2c 20 74 68 65 6e 20 69 74 20 65 69 74 68 65 72  , then it either
1278b 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61   needs to be pla
1278c 79 65 64 20 62 61 63 6b 20 6f 72 20 64 65 6c 65  yed back or dele
1278d 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
1278e 69 66 28 20 21 69 73 45 72 72 6f 72 52 65 73 65  if( !isErrorRese
1278f 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  t ){.      rc = 
12790 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50  hasHotJournal(pP
12791 61 67 65 72 2c 20 26 69 73 48 6f 74 4a 6f 75 72  ager, &isHotJour
12792 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nal);.      if( 
12793 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
12794 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61  .        goto fa
12795 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20  iled;.      }.  
12796 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 45 72    }.    if( isEr
12797 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 73 48 6f  rorReset || isHo
12798 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  tJournal ){.    
12799 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c    /* Get an EXCL
1279a 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
1279b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1279c 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69   At this point i
1279d 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 6d  t is.      ** im
1279e 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 20 52  portant that a R
1279f 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20  ESERVED lock is 
127a0 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20  not obtained on 
127a1 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 0a 20  the way to the. 
127a2 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56       ** EXCLUSIV
127a3 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 77 65  E lock. If it we
127a4 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 6f 63  re, another proc
127a5 65 73 73 20 6d 69 67 68 74 20 6f 70 65 6e 20 74  ess might open t
127a6 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61  he.      ** data
127a7 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74 65 63  base file, detec
127a8 74 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c  t the RESERVED l
127a9 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c 75 64  ock, and conclud
127aa 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 20  e that the.     
127ab 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20   ** database is 
127ac 73 61 66 65 20 74 6f 20 72 65 61 64 20 77 68 69  safe to read whi
127ad 6c 65 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  le this process 
127ae 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c 69 6e 67  is still rolling
127af 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 68   the .      ** h
127b0 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2e  ot-journal back.
127b1 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20  .      ** .     
127b2 20 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20   ** Because the 
127b3 69 6e 74 65 72 6d 65 64 69 61 74 65 20 52 45 53  intermediate RES
127b4 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f  ERVED lock is no
127b5 74 20 72 65 71 75 65 73 74 65 64 2c 20 61 6e 79  t requested, any
127b6 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20  .      ** other 
127b7 70 72 6f 63 65 73 73 20 61 74 74 65 6d 70 74 69  process attempti
127b8 6e 67 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ng to access the
127b9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
127ba 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20 20 20 20  ill get to .    
127bb 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 6e 74 20    ** this point 
127bc 69 6e 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20  in the code and 
127bd 66 61 69 6c 20 74 6f 20 6f 62 74 61 69 6e 20 69  fail to obtain i
127be 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 49 56 45  ts own EXCLUSIVE
127bf 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20 2a 2a 20   lock .      ** 
127c0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
127c1 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  file..      */. 
127c2 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d       if( pPager-
127c3 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 49 56 45  >state<EXCLUSIVE
127c4 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 20  _LOCK ){.       
127c5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c   rc = sqlite3OsL
127c6 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ock(pPager->fd, 
127c7 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
127c8 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
127c9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
127ca 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67          rc = pag
127cb 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
127cc 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20   rc);.          
127cd 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
127ce 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
127cf 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
127d0 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a  AGER_EXCLUSIVE;.
127d1 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20 20 20        }. .      
127d2 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72  /* Open the jour
127d3 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f 77 72 69  nal for read/wri
127d4 74 65 20 61 63 63 65 73 73 2e 20 54 68 69 73 20  te access. This 
127d5 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 0a 20  is because in . 
127d6 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69 76       ** exclusiv
127d7 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68  e-access mode th
127d8 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
127d9 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74 20 6f  r will be kept o
127da 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a  pen and.      **
127db 20 70 6f 73 73 69 62 6c 79 20 75 73 65 64 20 66   possibly used f
127dc 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  or a transaction
127dd 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e 20 73 6f   later on. On so
127de 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 0a  me systems, the.
127df 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 75 6e 63        ** OsTrunc
127e0 61 74 65 28 29 20 63 61 6c 6c 20 75 73 65 64 20  ate() call used 
127e1 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63  in exclusive-acc
127e2 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f 20 72 65  ess mode also re
127e3 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a 2a 20  quires.      ** 
127e4 61 20 72 65 61 64 2f 77 72 69 74 65 20 66 69 6c  a read/write fil
127e5 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 20 20  e handle..      
127e6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 69 73  */.      if( !is
127e7 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
127e8 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ) ){.        int
127e9 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72 63   res;.        rc
127ea 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65   = sqlite3OsAcce
127eb 73 73 28 70 56 66 73 2c 70 50 61 67 65 72 2d 3e  ss(pVfs,pPager->
127ec 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 54 45 5f  zJournal,SQLITE_
127ed 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 26 72  ACCESS_EXISTS,&r
127ee 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  es);.        if(
127ef 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
127f0 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
127f1 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  res ){.         
127f2 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b     int fout = 0;
127f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
127f4 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e   f = SQLITE_OPEN
127f5 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
127f6 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
127f7 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 20  NAL;.           
127f8 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
127f9 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20  ->tempFile );.  
127fa 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
127fb 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
127fc 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
127fd 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
127fe 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20  , f, &fout);.   
127ff 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
12800 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
12801 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  | isOpen(pPager-
12802 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 20 20 20  >jfd) );.       
12803 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
12804 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 74 26 53  ITE_OK && fout&S
12805 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
12806 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  NLY ){.         
12807 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
12808 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20  _CANTOPEN;.     
12809 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1280a 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
1280b 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  jfd);.          
1280c 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65    }.          }e
1280d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
1280e 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e   /* If the journ
1280f 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  al does not exis
12810 74 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 73 6f  t, that means so
12811 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
12812 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
12813 68 61 73 20 61 6c 72 65 61 64 79 20 72 6f 6c 6c  has already roll
12814 65 64 20 69 74 20 62 61 63 6b 20 2a 2f 0a 20 20  ed it back */.  
12815 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
12816 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
12817 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
12818 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
12819 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1281a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
1281b 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20  o failed;.      
1281c 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f  }..      /* TODO
1281d 3a 20 57 68 79 20 61 72 65 20 74 68 65 73 65 20  : Why are these 
1281e 63 6c 65 61 72 65 64 20 68 65 72 65 3f 20 49 73  cleared here? Is
1281f 20 69 74 20 6e 65 63 65 73 73 61 72 79 3f 20 2a   it necessary? *
12820 2f 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  /.      pPager->
12821 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
12822 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
12823 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
12824 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ;.      pPager->
12825 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20  setMaster = 0;. 
12826 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
12827 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a 20  rnalHdr = 0;. . 
12828 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63 6b       /* Playback
12829 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20   and delete the 
1282a 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20 74  journal.  Drop t
1282b 68 65 20 64 61 74 61 62 61 73 65 20 77 72 69 74  he database writ
1282c 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 20  e.      ** lock 
1282d 61 6e 64 20 72 65 61 63 71 75 69 72 65 20 74 68  and reacquire th
1282e 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 75 72  e read lock. Pur
1282f 67 65 20 74 68 65 20 63 61 63 68 65 20 62 65 66  ge the cache bef
12830 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 6c 61  ore.      ** pla
12831 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20 68 6f  ying back the ho
12832 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 68 61  t-journal so tha
12833 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 20 75  t we don't end u
12834 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a 2a 20  p with.      ** 
12835 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20  an inconsistent 
12836 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  cache..      */.
12837 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
12838 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72  _playback(pPager
12839 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 1);.      if( 
1283a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1283b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61  .        rc = pa
1283c 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
1283d 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 67  , rc);.        g
1283e 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
1283f 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
12840 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  ( (pPager->state
12841 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a  ==PAGER_SHARED).
12842 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70             || (p
12843 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
12844 4d 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e  Mode && pPager->
12845 73 74 61 74 65 3e 50 41 47 45 52 5f 53 48 41 52  state>PAGER_SHAR
12846 45 44 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ED).      );.   
12847 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69   }..    if( sqli
12848 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75  te3PcachePagecou
12849 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
1284a 68 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f  he)>0 ){.      /
1284b 2a 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63  * The shared-loc
1284c 6b 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20  k has just been 
1284d 61 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20  acquired on the 
1284e 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20  database file.  
1284f 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65      ** and there
12850 20 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67   are already pag
12851 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  es in the cache 
12852 28 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73  (from a previous
12853 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f  .      ** read o
12854 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  r write transact
12855 69 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20  ion).  Check to 
12856 73 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62  see if the datab
12857 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73  ase.      ** has
12858 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20   been modified. 
12859 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
1285a 20 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c   has changed, fl
1285b 75 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  ush the.      **
1285c 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a   cache..      **
1285d 0a 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 61  .      ** Databa
1285e 73 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 65  se changes is de
1285f 74 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e  tected by lookin
12860 67 20 61 74 20 31 35 20 62 79 74 65 73 20 62 65  g at 15 bytes be
12861 67 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a  ginning.      **
12862 20 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 6e   at offset 24 in
12863 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68  to the file.  Th
12864 65 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 65  e first 4 of the
12865 73 65 20 31 36 20 62 79 74 65 73 20 61 72 65 0a  se 16 bytes are.
12866 20 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69        ** a 32-bi
12867 74 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69  t counter that i
12868 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69  s incremented wi
12869 74 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20  th each change. 
1286a 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74   The.      ** ot
1286b 68 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65  her bytes change
1286c 20 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65   randomly with e
1286d 61 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20  ach file change 
1286e 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20  when.      ** a 
1286f 63 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e  codec is in use.
12870 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20  .      ** .     
12871 20 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 76   ** There is a v
12872 61 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c  anishingly small
12873 20 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 63   chance that a c
12874 68 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62  hange will not b
12875 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 65  e .      ** dete
12876 63 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e 63  cted.  The chanc
12877 65 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 74  e of an undetect
12878 65 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f 20  ed change is so 
12879 73 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 20  small that.     
1287a 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e 65   ** it can be ne
1287b 67 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 2a  glected..      *
1287c 2f 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 46  /.      char dbF
1287d 69 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70  ileVers[sizeof(p
1287e 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72  Pager->dbFileVer
1287f 73 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  s)];.      sqlit
12880 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
12881 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20  (pPager, 0);..  
12882 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
12883 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20  errCode ){.     
12884 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e     rc = pPager->
12885 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20  errCode;.       
12886 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20   goto failed;.  
12887 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73      }..      ass
12888 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53  ert( pPager->dbS
12889 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20  izeValid );.    
1288a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62    if( pPager->db
1288b 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20  Size>0 ){.      
1288c 20 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45    IOTRACE(("CKVE
1288d 52 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61  RS %p %d\n", pPa
1288e 67 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69  ger, sizeof(dbFi
1288f 6c 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20  leVers)));.     
12890 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
12891 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64  sRead(pPager->fd
12892 2c 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73  , &dbFileVers, s
12893 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73  izeof(dbFileVers
12894 29 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20  ), 24);.        
12895 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12896 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  K ){.          g
12897 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
12898 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
12899 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65  e{.        memse
1289a 74 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c  t(dbFileVers, 0,
1289b 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65   sizeof(dbFileVe
1289c 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  rs));.      }.. 
1289d 20 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28       if( memcmp(
1289e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65  pPager->dbFileVe
1289f 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20  rs, dbFileVers, 
128a0 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72  sizeof(dbFileVer
128a1 73 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  s))!=0 ){.      
128a2 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50    pager_reset(pP
128a3 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ager);.      }. 
128a4 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
128a5 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
128a6 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72  veMode || pPager
128a7 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53  ->state==PAGER_S
128a8 48 41 52 45 44 20 29 3b 0a 20 20 7d 0a 0a 20 66  HARED );.  }.. f
128a9 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21  ailed:.  if( rc!
128aa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
128ab 20 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63    /* pager_unloc
128ac 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66  k() is a no-op f
128ad 6f 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  or exclusive mod
128ae 65 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20  e and in-memory 
128af 64 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20  databases. */.  
128b0 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70    pager_unlock(p
128b1 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65  Pager);.  }.  re
128b2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
128b3 2a 20 49 66 20 74 68 65 20 72 65 66 65 72 65 6e  * If the referen
128b4 63 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 61  ce count has rea
128b5 63 68 65 64 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62  ched zero, rollb
128b6 61 63 6b 20 61 6e 79 20 61 63 74 69 76 65 0a 2a  ack any active.*
128b7 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e  * transaction an
128b8 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67  d unlock the pag
128b9 65 72 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76  er..*/ .static v
128ba 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49  oid pagerUnlockI
128bb 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a 70  fUnused(Pager *p
128bc 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 73 71  Pager){.  if( sq
128bd 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
128be 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
128bf 63 68 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70  che)==0 ){.    p
128c0 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c  agerUnlockAndRol
128c1 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20  lback(pPager);. 
128c2 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70   }.}../*.** Drop
128c3 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
128c4 20 63 61 63 68 65 20 75 73 69 6e 67 20 73 71 6c   cache using sql
128c5 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 29  ite3PcacheDrop()
128c6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20  ..**.** If this 
128c7 6d 65 61 6e 73 20 74 68 65 72 65 20 61 72 65 20  means there are 
128c8 6e 6f 77 20 6e 6f 20 70 61 67 65 73 20 77 69 74  now no pages wit
128c9 68 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  h references to 
128ca 74 68 65 6d 2c 20 61 20 72 6f 6c 6c 62 61 63 6b  them, a rollback
128cb 0a 2a 2a 20 6f 63 63 75 72 73 20 61 6e 64 20 74  .** occurs and t
128cc 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  he lock on the d
128cd 61 74 61 62 61 73 65 20 69 73 20 72 65 6d 6f 76  atabase is remov
128ce 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
128cf 69 64 20 70 61 67 65 72 44 72 6f 70 50 61 67 65  id pagerDropPage
128d0 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20  (DbPage *pPg){. 
128d1 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
128d2 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
128d3 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f  sqlite3PcacheDro
128d4 70 28 70 50 67 29 3b 0a 20 20 70 61 67 65 72 55  p(pPg);.  pagerU
128d5 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50  nlockIfUnused(pP
128d6 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ager);.}../*.** 
128d7 41 63 71 75 69 72 65 20 61 20 72 65 66 65 72 65  Acquire a refere
128d8 6e 63 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62  nce to page numb
128d9 65 72 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72  er pgno in pager
128da 20 70 50 61 67 65 72 20 28 61 20 70 61 67 65 0a   pPager (a page.
128db 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 68 61 73  ** reference has
128dc 20 74 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20   type DbPage*). 
128dd 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
128de 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a   reference is .*
128df 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f  * successfully o
128e0 62 74 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63  btained, it is c
128e1 6f 70 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65  opied to *ppPage
128e2 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72   and SQLITE_OK r
128e3 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
128e4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  his function cal
128e5 6c 73 20 70 61 67 65 72 53 68 61 72 65 64 4c 6f  ls pagerSharedLo
128e6 63 6b 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 61  ck() to obtain a
128e7 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 0a   SHARED lock on.
128e8 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
128e9 66 69 6c 65 20 69 66 20 73 75 63 68 20 61 20 6c  file if such a l
128ea 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 69  ock or greater i
128eb 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65  s not already he
128ec 6c 64 2e 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20  ld..** This may 
128ed 63 61 75 73 65 20 68 6f 74 2d 6a 6f 75 72 6e 61  cause hot-journa
128ee 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20  l rollback or a 
128ef 63 61 63 68 65 20 70 75 72 67 65 2e 20 53 65 65  cache purge. See
128f0 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 61 62 6f   comments.** abo
128f1 76 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65  ve function page
128f2 72 53 68 61 72 65 64 4c 6f 63 6b 28 29 20 66 6f  rSharedLock() fo
128f3 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a  r details..**.**
128f4 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65   If the requeste
128f5 64 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64  d page is alread
128f6 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20  y in the cache, 
128f7 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  it is returned. 
128f8 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61  .** Otherwise, a
128f9 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74   new page object
128fa 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
128fb 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  d populated with
128fc 20 64 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 72   data.** read fr
128fd 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
128fe 66 69 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 61  file. In some ca
128ff 73 65 73 2c 20 74 68 65 20 70 63 61 63 68 65 20  ses, the pcache 
12900 6d 6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 68  module may.** ch
12901 6f 6f 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f  oose not to allo
12902 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  cate a new page 
12903 6f 62 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 72  object and may r
12904 65 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67  euse an existing
12905 0a 2a 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 20  .** object with 
12906 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  no outstanding r
12907 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a  eferences..**.**
12908 20 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20   The extra data 
12909 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61  appended to a pa
1290a 67 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69  ge is always ini
1290b 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f  tialized to zero
1290c 73 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20  s the .** first 
1290d 74 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c  time a page is l
1290e 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72  oaded into memor
1290f 79 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 72  y. If the page r
12910 65 71 75 65 73 74 65 64 20 69 73 20 0a 2a 2a 20  equested is .** 
12911 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63  already in the c
12912 61 63 68 65 20 77 68 65 6e 20 74 68 69 73 20 66  ache when this f
12913 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
12914 64 2c 20 74 68 65 6e 20 74 68 65 20 65 78 74 72  d, then the extr
12915 61 0a 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 66  a.** data is lef
12916 74 20 61 73 20 69 74 20 77 61 73 20 77 68 65 6e  t as it was when
12917 20 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74   the page object
12918 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 0a   was last used..
12919 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74  **.** If the dat
1291a 61 62 61 73 65 20 69 6d 61 67 65 20 69 73 20 73  abase image is s
1291b 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20  maller than the 
1291c 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6f  requested page o
1291d 72 20 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a  r if a .** non-z
1291e 65 72 6f 20 76 61 6c 75 65 20 69 73 20 70 61 73  ero value is pas
1291f 73 65 64 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e  sed as the noCon
12920 74 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 61  tent parameter a
12921 6e 64 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 65  nd the .** reque
12922 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74  sted page is not
12923 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20   already stored 
12924 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68  in the cache, th
12925 65 6e 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 6c  en no .** actual
12926 20 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72   disk read occur
12927 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  s. In this case 
12928 74 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65  the memory image
12929 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65   of the .** page
1292a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
1292b 74 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a  to all zeros. .*
1292c 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e  *.** If noConten
1292d 74 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65  t is true, it me
1292e 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e  ans that we do n
1292f 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 68  ot care about th
12930 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66  e contents.** of
12931 20 74 68 65 20 70 61 67 65 2e 20 54 68 69 73 20   the page. This 
12932 6f 63 63 75 72 73 20 69 6e 20 74 77 6f 20 73 65  occurs in two se
12933 70 65 72 61 74 65 20 73 63 65 6e 61 72 69 6f 73  perate scenarios
12934 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 65  :.**.**   a) Whe
12935 6e 20 72 65 61 64 69 6e 67 20 61 20 66 72 65 65  n reading a free
12936 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20  -list leaf page 
12937 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
12938 65 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62  e, and.**.**   b
12939 29 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69  ) When a savepoi
1293a 6e 74 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c  nt is being roll
1293b 65 64 20 62 61 63 6b 20 61 6e 64 20 77 65 20 6e  ed back and we n
1293c 65 65 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20  eed to load.**  
1293d 20 20 20 20 61 20 6e 65 77 20 70 61 67 65 20 69      a new page i
1293e 6e 74 6f 20 74 68 65 20 63 61 63 68 65 20 74 6f  nto the cache to
1293f 20 70 6f 70 75 6c 61 74 65 20 77 69 74 68 20 74   populate with t
12940 68 65 20 64 61 74 61 20 72 65 61 64 0a 2a 2a 20  he data read.** 
12941 20 20 20 20 20 66 72 6f 6d 20 74 68 65 20 73 61       from the sa
12942 76 65 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e  vepoint journal.
12943 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74  .**.** If noCont
12944 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 65  ent is true, the
12945 6e 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72  n the data retur
12946 6e 65 64 20 69 73 20 7a 65 72 6f 65 64 20 69 6e  ned is zeroed in
12947 73 74 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e  stead of.** bein
12948 67 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  g read from the 
12949 64 61 74 61 62 61 73 65 2e 20 41 64 64 69 74 69  database. Additi
1294a 6f 6e 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 73  onally, the bits
1294b 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a   corresponding.*
1294c 2a 20 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67  * to pgno in Pag
1294d 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62  er.pInJournal (b
1294e 69 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 61  itvec of pages a
1294f 6c 72 65 61 64 79 20 77 72 69 74 74 65 6e 20 74  lready written t
12950 6f 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  o the.** journal
12951 20 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50   file) and the P
12952 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49  agerSavepoint.pI
12953 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65  nSavepoint bitve
12954 63 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a  cs of any open.*
12955 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65  * savepoints are
12956 20 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73   set. This means
12957 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
12958 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 61 74  made writable at
12959 20 61 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e   any.** point in
1295a 20 74 68 65 20 66 75 74 75 72 65 2c 20 75 73 69   the future, usi
1295b 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ng a call to sql
1295c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
1295d 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a  , its contents.*
1295e 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f  * will not be jo
1295f 75 72 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 61  urnaled. This sa
12960 76 65 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68  ves IO..**.** Th
12961 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69  e acquisition mi
12962 67 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76  ght fail for sev
12963 65 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49  eral reasons.  I
12964 6e 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20  n all cases,.** 
12965 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65  an appropriate e
12966 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
12967 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67  urned and *ppPag
12968 65 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  e is set to NULL
12969 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1296a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f   sqlite3PagerLoo
1296b 6b 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69  kup().  Both thi
1296c 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f  s routine and Lo
1296d 6f 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a  okup() attempt.*
1296e 2a 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65  * to find a page
1296f 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   in the in-memor
12970 79 20 63 61 63 68 65 20 66 69 72 73 74 2e 20 20  y cache first.  
12971 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  If the page is n
12972 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e  ot already.** in
12973 20 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f   memory, this ro
12974 75 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69  utine goes to di
12975 73 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e  sk to read it in
12976 20 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28   whereas Lookup(
12977 29 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e  ).** just return
12978 73 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69  s 0.  This routi
12979 6e 65 20 61 63 71 75 69 72 65 73 20 61 20 72 65  ne acquires a re
1297a 61 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73  ad-lock the firs
1297b 74 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73  t time it.** has
1297c 20 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20   to go to disk, 
1297d 61 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70  and could also p
1297e 6c 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a  layback an old j
1297f 6f 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73  ournal if necess
12980 61 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f  ary..** Since Lo
12981 6f 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65  okup() never goe
12982 73 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65  s to disk, it ne
12983 76 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20  ver has to deal 
12984 77 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72  with locks.** or
12985 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a   journal files..
12986 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12987 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
12988 65 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 67  erAcquire(.  Pag
12989 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
1298a 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70   /* The pager op
1298b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  en on the databa
1298c 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e  se file */.  Pgn
1298d 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20  o pgno,         
1298e 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
1298f 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62  to fetch */.  Db
12990 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20  Page **ppPage,  
12991 20 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69    /* Write a poi
12992 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65  nter to the page
12993 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   here */.  int n
12994 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f  oContent       /
12995 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20  * Do not bother 
12996 72 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20  reading content 
12997 66 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75  from disk if tru
12998 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20  e */.){.  PgHdr 
12999 2a 70 50 67 20 3d 20 30 3b 0a 20 20 69 6e 74 20  *pPg = 0;.  int 
1299a 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61  rc;..  assert( a
1299b 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74  ssert_pager_stat
1299c 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61  e(pPager) );.  a
1299d 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
1299e 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
1299f 43 4b 20 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  CK .       || sq
129a0 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
129a1 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
129a2 63 68 65 29 3e 30 20 0a 20 20 20 20 20 20 20 7c  che)>0 .       |
129a3 7c 20 70 67 6e 6f 3d 3d 31 0a 20 20 29 3b 0a 0a  | pgno==1.  );..
129a4 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d    /* The maximum
129a5 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
129a6 32 5e 33 31 2e 20 52 65 74 75 72 6e 20 53 51 4c  2^31. Return SQL
129a7 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20 61  ITE_CORRUPT if a
129a8 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d 62 65   page.  ** numbe
129a9 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  r greater than t
129aa 68 69 73 2c 20 6f 72 20 7a 65 72 6f 2c 20 69 73  his, or zero, is
129ab 20 72 65 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f   requested..  */
129ac 0a 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45  .  if( pgno>PAGE
129ad 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67  R_MAX_PGNO || pg
129ae 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50  no==0 || pgno==P
129af 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
129b0 67 65 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ger) ){.    retu
129b1 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
129b2 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f  T_BKPT;.  }..  /
129b3 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68  * Make sure we h
129b4 61 76 65 20 6e 6f 74 20 68 69 74 20 61 6e 79 20  ave not hit any 
129b5 63 72 69 74 69 63 61 6c 20 65 72 72 6f 72 73 2e  critical errors.
129b6 0a 20 20 2a 2f 20 0a 20 20 61 73 73 65 72 74 28  .  */ .  assert(
129b7 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20   pPager!=0 );.  
129b8 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20  *ppPage = 0;..  
129b9 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68  /* If this is th
129ba 65 20 66 69 72 73 74 20 70 61 67 65 20 61 63 63  e first page acc
129bb 65 73 73 65 64 2c 20 74 68 65 6e 20 67 65 74 20  essed, then get 
129bc 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 0a 20 20  a SHARED lock.  
129bd 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ** on the databa
129be 73 65 20 66 69 6c 65 2e 20 70 61 67 65 72 53 68  se file. pagerSh
129bf 61 72 65 64 4c 6f 63 6b 28 29 20 69 73 20 61 20  aredLock() is a 
129c0 6e 6f 2d 6f 70 20 69 66 20 0a 20 20 2a 2a 20 61  no-op if .  ** a
129c1 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 69   database lock i
129c2 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a  s already held..
129c3 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65    */.  rc = page
129c4 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 50 61 67  rSharedLock(pPag
129c5 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  er);.  if( rc!=S
129c6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
129c7 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
129c8 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
129c9 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e  >state!=PAGER_UN
129ca 4c 4f 43 4b 20 29 3b 0a 0a 20 20 72 63 20 3d 20  LOCK );..  rc = 
129cb 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
129cc 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ch(pPager->pPCac
129cd 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 26 70 50  he, pgno, 1, &pP
129ce 67 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  g);.  if( rc!=SQ
129cf 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
129d0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
129d1 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e  assert( pPg->pgn
129d2 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73  o==pgno );.  ass
129d3 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72  ert( pPg->pPager
129d4 3d 3d 70 50 61 67 65 72 20 7c 7c 20 70 50 67 2d  ==pPager || pPg-
129d5 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b 0a 20 20  >pPager==0 );.  
129d6 69 66 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d  if( pPg->pPager=
129d7 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  =0 ){.    /* The
129d8 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73   pager cache has
129d9 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70   created a new p
129da 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  age. Its content
129db 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a   needs to .    *
129dc 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  * be initialized
129dd 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
129de 20 6e 4d 61 78 3b 0a 20 20 20 20 50 41 47 45 52   nMax;.    PAGER
129df 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d  _INCR(pPager->nM
129e0 69 73 73 29 3b 0a 20 20 20 20 70 50 67 2d 3e 70  iss);.    pPg->p
129e1 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a  Pager = pPager;.
129e2 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
129e3 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
129e4 70 50 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b 0a  pPager, &nMax);.
129e5 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
129e6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
129e7 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
129e8 28 70 50 67 29 3b 0a 20 20 20 20 20 20 72 65 74  (pPg);.      ret
129e9 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20  urn rc;.    }.. 
129ea 20 20 20 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74     if( nMax<(int
129eb 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c  )pgno || MEMDB |
129ec 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20  | noContent ){. 
129ed 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50       if( pgno>pP
129ee 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a  ager->mxPgno ){.
129ef 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
129f0 61 67 65 72 55 6e 72 65 66 28 70 50 67 29 3b 0a  agerUnref(pPg);.
129f1 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
129f2 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20  QLITE_FULL;.    
129f3 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f    }.      if( no
129f4 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20  Content ){.     
129f5 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f     /* Failure to
129f6 20 73 65 74 20 74 68 65 20 62 69 74 73 20 69 6e   set the bits in
129f7 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62   the InJournal b
129f8 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20 62 65  it-vectors is be
129f9 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a  nign..        **
129fa 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73   It merely means
129fb 20 74 68 61 74 20 77 65 20 6d 69 67 68 74 20 64   that we might d
129fc 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77 6f 72  o some extra wor
129fd 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a  k to journal a .
129fe 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20          ** page 
129ff 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65  that does not ne
12a00 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c  ed to be journal
12a01 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73  ed.  Nevertheles
12a02 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20 20 20  s, be sure .    
12a03 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74      ** to test t
12a04 68 65 20 63 61 73 65 20 77 68 65 72 65 20 61 20  he case where a 
12a05 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63  malloc error occ
12a06 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67  urs while trying
12a07 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20 20 20   to set .       
12a08 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61 20 62   ** a bit in a b
12a09 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20 20 20  it vector..     
12a0a 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71     */.        sq
12a0b 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
12a0c 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
12a0d 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67    if( pgno<=pPag
12a0e 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29  er->dbOrigSize )
12a0f 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45 53 54  {.          TEST
12a10 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c  ONLY( rc = ) sql
12a11 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50  ite3BitvecSet(pP
12a12 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
12a13 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  , pgno);.       
12a14 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d     testcase( rc=
12a15 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
12a16 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
12a17 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20     TESTONLY( rc 
12a18 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69  = ) addToSavepoi
12a19 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72  ntBitvecs(pPager
12a1a 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  , pgno);.       
12a1b 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53   testcase( rc==S
12a1c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
12a1d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e         sqlite3En
12a1e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
12a1f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
12a20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67        memset(pPg
12a21 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67  ->pData, 0, pPag
12a22 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
12a23 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54       }.      IOT
12a24 52 41 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25  RACE(("ZERO %p %
12a25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67  d\n", pPager, pg
12a26 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  no));.    }else{
12a27 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
12a28 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67  Pg->pPager==pPag
12a29 65 72 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  er );.      rc =
12a2a 20 72 65 61 64 44 62 50 61 67 65 28 70 50 67 29   readDbPage(pPg)
12a2b 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
12a2c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12a2d 20 20 20 20 20 70 61 67 65 72 44 72 6f 70 50 61       pagerDropPa
12a2e 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20  ge(pPg);.       
12a2f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
12a30 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 66    }.    }.#ifdef
12a31 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
12a32 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 67  GES.    pPg->pag
12a33 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61  eHash = pager_pa
12a34 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 6e  gehash(pPg);.#en
12a35 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  dif.  }else{.   
12a36 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 65   /* The requeste
12a37 64 20 70 61 67 65 20 69 73 20 69 6e 20 74 68 65  d page is in the
12a38 20 70 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a   page cache. */.
12a39 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70      PAGER_INCR(p
12a3a 50 61 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20  Pager->nHit);.  
12a3b 7d 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70  }..  *ppPage = p
12a3c 50 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  Pg;.  return SQL
12a3d 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
12a3e 20 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20   Acquire a page 
12a3f 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79  if it is already
12a40 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   in the in-memor
12a41 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20  y cache.  Do.** 
12a42 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67  not read the pag
12a43 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65  e from disk.  Re
12a44 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
12a45 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f  o the page,.** o
12a46 72 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20  r 0 if the page 
12a47 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e  is not in cache.
12a48 20 41 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20   Also, return 0 
12a49 69 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72  if the .** pager
12a4a 20 69 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c   is in PAGER_UNL
12a4b 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74  OCK state when t
12a4c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
12a4d 63 61 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66  called,.** or if
12a4e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
12a4f 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20   an error state 
12a50 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54  other than SQLIT
12a51 45 5f 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65  E_FULL..**.** Se
12a52 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61  e also sqlite3Pa
12a53 67 65 72 47 65 74 28 29 2e 20 20 54 68 65 20 64  gerGet().  The d
12a54 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
12a55 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
12a56 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67  * and sqlite3Pag
12a57 65 72 47 65 74 28 29 20 69 73 20 74 68 61 74 20  erGet() is that 
12a58 5f 67 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74  _get() will go t
12a59 6f 20 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72  o the disk and r
12a5a 65 61 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61  ead.** in the pa
12a5b 67 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69  ge if the page i
12a5c 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e  s not already in
12a5d 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f   cache.  This ro
12a5e 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73  utine.** returns
12a5f 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67   NULL if the pag
12a60 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68  e is not in cach
12a61 65 20 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49  e or if a disk I
12a62 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73  /O error .** has
12a63 20 65 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a   ever happened..
12a64 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12a65 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65  E DbPage *sqlite
12a66 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67  3PagerLookup(Pag
12a67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
12a68 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20   pgno){.  PgHdr 
12a69 2a 70 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65  *pPg = 0;.  asse
12a6a 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b  rt( pPager!=0 );
12a6b 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21  .  assert( pgno!
12a6c 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 70 50  =0 );..  if( (pP
12a6d 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
12a6e 45 52 5f 55 4e 4c 4f 43 4b 29 0a 20 20 20 26 26  ER_UNLOCK).   &&
12a6f 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64   (pPager->errCod
12a70 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  e==SQLITE_OK || 
12a71 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d  pPager->errCode=
12a72 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29 0a 20 20  =SQLITE_FULL).  
12a73 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  ){.    sqlite3Pc
12a74 61 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72  acheFetch(pPager
12a75 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c  ->pPCache, pgno,
12a76 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 7d 0a 0a   0, &pPg);.  }..
12a77 20 20 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a    return pPg;.}.
12a78 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
12a79 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e   page reference.
12a7a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75  .**.** If the nu
12a7b 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63  mber of referenc
12a7c 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 64  es to the page d
12a7d 72 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65  rop to zero, the
12a7e 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73  n the.** page is
12a7f 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52   added to the LR
12a80 55 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c  U list.  When al
12a81 6c 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  l references to 
12a82 61 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65  all pages.** are
12a83 20 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c   released, a rol
12a84 6c 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64  lback occurs and
12a85 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65   the lock on the
12a86 20 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20   database is.** 
12a87 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49  removed..*/.SQLI
12a88 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12a89 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
12a8a 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  f(DbPage *pPg){.
12a8b 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20    if( pPg ){.   
12a8c 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
12a8d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
12a8e 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52    sqlite3PcacheR
12a8f 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20  elease(pPg);.   
12a90 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e   pagerUnlockIfUn
12a91 75 73 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20  used(pPager);.  
12a92 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  }.}../*.** If th
12a93 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66  e main journal f
12a94 69 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20  ile has already 
12a95 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73  been opened, ens
12a96 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ure that the.** 
12a97 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  sub-journal file
12a98 20 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66   is open too. If
12a99 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
12a9a 6c 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a  l is not open,.*
12a9b 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
12a9c 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
12a9d 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
12a9e 65 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 79  eturned if every
12a9f 74 68 69 6e 67 20 67 6f 65 73 20 61 63 63 6f 72  thing goes accor
12aa0 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a  ding to plan. .*
12aa1 2a 20 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  * An SQLITE_IOER
12aa2 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65  R_XXX error code
12aa3 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
12aa4 61 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71  a call to .** sq
12aa5 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 20 66 61  lite3OsOpen() fa
12aa6 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ils..*/.static i
12aa7 6e 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61  nt openSubJourna
12aa8 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  l(Pager *pPager)
12aa9 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
12aaa 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73  ITE_OK;.  if( is
12aab 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
12aac 29 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61  ) && !isOpen(pPa
12aad 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20  ger->sjfd) ){.  
12aae 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f    if( pPager->jo
12aaf 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
12ab0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
12ab1 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ORY ){.      sql
12ab2 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70  ite3MemJournalOp
12ab3 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29  en(pPager->sjfd)
12ab4 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
12ab5 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65     rc = pagerOpe
12ab6 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50  ntemp(pPager, pP
12ab7 61 67 65 72 2d 3e 73 6a 66 64 2c 20 53 51 4c 49  ager->sjfd, SQLI
12ab8 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e  TE_OPEN_SUBJOURN
12ab9 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  AL);.    }.  }. 
12aba 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
12abb 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
12abc 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 74 20  on is called at 
12abd 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 76 65  the start of eve
12abe 72 79 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  ry write transac
12abf 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 6d  tion..** There m
12ac0 75 73 74 20 61 6c 72 65 61 64 79 20 62 65 20 61  ust already be a
12ac1 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43   RESERVED or EXC
12ac2 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74  LUSIVE lock on t
12ac3 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20  he database .** 
12ac4 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72  file when this r
12ac5 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
12ac6 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  ..**.** Open the
12ac7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f   journal file fo
12ac8 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20 61  r pager pPager a
12ac9 6e 64 20 77 72 69 74 65 20 61 20 6a 6f 75 72 6e  nd write a journ
12aca 61 6c 20 68 65 61 64 65 72 0a 2a 2a 20 74 6f 20  al header.** to 
12acb 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 2e  the start of it.
12acc 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 63   If there are ac
12acd 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 2c  tive savepoints,
12ace 20 6f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f   open the sub-jo
12acf 75 72 6e 61 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c  urnal.** as well
12ad0 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
12ad1 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65  is only used whe
12ad2 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
12ad3 6c 65 20 69 73 20 62 65 69 6e 67 20 0a 2a 2a 20  le is being .** 
12ad4 6f 70 65 6e 65 64 20 74 6f 20 77 72 69 74 65 20  opened to write 
12ad5 61 20 72 6f 6c 6c 62 61 63 6b 20 6c 6f 67 20 66  a rollback log f
12ad6 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  or a transaction
12ad7 2e 20 49 74 20 69 73 20 6e 6f 74 20 75 73 65 64  . It is not used
12ad8 20 0a 2a 2a 20 77 68 65 6e 20 6f 70 65 6e 69 6e   .** when openin
12ad9 67 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  g a hot journal 
12ada 66 69 6c 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20  file to roll it 
12adb 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  back..**.** If t
12adc 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
12add 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20  is already open 
12ade 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 69 6e  (as it may be in
12adf 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29   exclusive mode)
12ae0 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 66  ,.** then this f
12ae1 75 6e 63 74 69 6f 6e 20 6a 75 73 74 20 77 72 69  unction just wri
12ae2 74 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 68 65  tes a journal he
12ae3 61 64 65 72 20 74 6f 20 74 68 65 20 73 74 61 72  ader to the star
12ae4 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 72 65  t of the.** alre
12ae5 61 64 79 20 6f 70 65 6e 20 66 69 6c 65 2e 20 0a  ady open file. .
12ae6 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72  **.** Whether or
12ae7 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c   not the journal
12ae8 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20   file is opened 
12ae9 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
12aea 2c 20 74 68 65 0a 2a 2a 20 50 61 67 65 72 2e 70  , the.** Pager.p
12aeb 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63  InJournal bitvec
12aec 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c   structure is al
12aed 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52  located..**.** R
12aee 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
12aef 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  if everything is
12af0 20 73 75 63 63 65 73 73 66 75 6c 2e 20 4f 74 68   successful. Oth
12af1 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 0a  erwise, return .
12af2 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  ** SQLITE_NOMEM 
12af3 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74  if the attempt t
12af4 6f 20 61 6c 6c 6f 63 61 74 65 20 50 61 67 65 72  o allocate Pager
12af5 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 66 61 69 6c  .pInJournal fail
12af6 73 2c 20 6f 72 20 0a 2a 2a 20 61 6e 20 49 4f 20  s, or .** an IO 
12af7 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 6f 70  error code if op
12af8 65 6e 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67  ening or writing
12af9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12afa 65 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  e fails..*/.stat
12afb 69 63 20 69 6e 74 20 70 61 67 65 72 5f 6f 70 65  ic int pager_ope
12afc 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20  n_journal(Pager 
12afd 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20  *pPager){.  int 
12afe 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
12aff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12b00 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
12b01 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74   code */.  sqlit
12b02 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70  e3_vfs * const p
12b03 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56  Vfs = pPager->pV
12b04 66 73 3b 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63  fs;   /* Local c
12b05 61 63 68 65 20 6f 66 20 76 66 73 20 70 6f 69 6e  ache of vfs poin
12b06 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ter */..  assert
12b07 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
12b08 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  =PAGER_RESERVED 
12b09 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
12b0a 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
12b0b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
12b0c 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d  ger->pInJournal=
12b0d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66  =0 );.  .  /* If
12b0e 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
12b0f 65 72 72 6f 72 20 73 74 61 74 65 2c 20 74 68 69  error state, thi
12b10 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
12b11 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20  no-op. */.  if( 
12b12 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
12b13 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50  ){.    return pP
12b14 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
12b15 20 7d 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49   }..  /* TODO: I
12b16 73 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73 73  s it really poss
12b17 69 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72 65  ible to get here
12b18 20 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c 69   with dbSizeVali
12b19 64 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20 20  d==0? If not,.  
12b1a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 50  ** the call to P
12b1b 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20  agerPagecount() 
12b1c 63 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e 0a  can be removed..
12b1d 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28    */.  testcase(
12b1e 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
12b1f 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  alid==0 );.  sql
12b20 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
12b21 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a  nt(pPager, 0);..
12b22 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75    pPager->pInJou
12b23 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69  rnal = sqlite3Bi
12b24 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65  tvecCreate(pPage
12b25 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 66  r->dbSize);.  if
12b26 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ( pPager->pInJou
12b27 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72  rnal==0 ){.    r
12b28 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
12b29 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70  EM;.  }..  /* Op
12b2a 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  en the journal f
12b2b 69 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f 74  ile if it is not
12b2c 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 2a   already open. *
12b2d 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28  /.  if( !isOpen(
12b2e 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a  pPager->jfd) ){.
12b2f 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
12b30 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
12b31 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
12b32 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 73  EMORY ){.      s
12b33 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
12b34 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
12b35 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
12b36 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c      const int fl
12b37 61 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 20  ags =           
12b38 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66          /* VFS f
12b39 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f 75  lags to open jou
12b3a 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 20  rnal file */.   
12b3b 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
12b3c 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
12b3d 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a 20  E_OPEN_CREATE|. 
12b3e 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e         (pPager->
12b3f 74 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 20  tempFile ? .    
12b40 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50        (SQLITE_OP
12b41 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
12b42 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d  |SQLITE_OPEN_TEM
12b43 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 20  P_JOURNAL):.    
12b44 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50        (SQLITE_OP
12b45 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29  EN_MAIN_JOURNAL)
12b46 0a 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66 64  .        );.#ifd
12b47 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
12b48 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20  _ATOMIC_WRITE.  
12b49 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12b4a 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20  JournalOpen(.   
12b4b 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61         pVfs, pPa
12b4c 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70  ger->zJournal, p
12b4d 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67  Pager->jfd, flag
12b4e 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a  s, jrnlBufferSiz
12b4f 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20  e(pPager).      
12b50 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 72  );.#else.      r
12b51 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
12b52 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  n(pVfs, pPager->
12b53 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
12b54 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29  ->jfd, flags, 0)
12b55 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20  ;.#endif.    }. 
12b56 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
12b57 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70  QLITE_OK || isOp
12b58 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
12b59 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72  );.  }...  /* Wr
12b5a 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f  ite the first jo
12b5b 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20  urnal header to 
12b5c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12b5d 20 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a 20   and open .  ** 
12b5e 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  the sub-journal 
12b5f 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20  if necessary..  
12b60 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
12b61 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a  ITE_OK ){.    /*
12b62 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66 20   TODO: Check if 
12b63 61 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 65  all of these are
12b64 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64   really required
12b65 2e 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d  . */.    pPager-
12b66 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50  >dbOrigSize = pP
12b67 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20  ager->dbSize;.  
12b68 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
12b69 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20  lStarted = 0;.  
12b6a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79    pPager->needSy
12b6b 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  nc = 0;.    pPag
12b6c 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20  er->nRec = 0;.  
12b6d 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
12b6e 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 50  lOff = 0;.    pP
12b6f 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20  ager->setMaster 
12b70 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  = 0;.    pPager-
12b71 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b  >journalHdr = 0;
12b72 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4a  .    rc = writeJ
12b73 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72  ournalHdr(pPager
12b74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
12b75 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50  =SQLITE_OK && pP
12b76 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
12b77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65   ){.    rc = ope
12b78 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67  nSubJournal(pPag
12b79 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  er);.  }..  if( 
12b7a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
12b7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76  .    sqlite3Bitv
12b7c 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72  ecDestroy(pPager
12b7d 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20  ->pInJournal);. 
12b7e 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f     pPager->pInJo
12b7f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20  urnal = 0;.  }. 
12b80 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
12b81 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 69  *.** Begin a wri
12b82 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te-transaction o
12b83 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  n the specified 
12b84 70 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66  pager object. If
12b85 20 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61   a .** write-tra
12b86 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72  nsaction has alr
12b87 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64  eady been opened
12b88 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
12b89 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
12b8a 2a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20  * If the exFlag 
12b8b 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73  argument is fals
12b8c 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 20  e, then acquire 
12b8d 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52  at least a RESER
12b8e 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74  VED.** lock on t
12b8f 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12b90 2e 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74  . If exFlag is t
12b91 72 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72  rue, then acquir
12b92 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 6e  e at least.** an
12b93 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
12b94 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
12b95 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c  is already held,
12b96 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20   no locking .** 
12b97 66 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 62  functions need b
12b98 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  e called..**.** 
12b99 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61  If this is not a
12b9a 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 6e   temporary or in
12b9b 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e 64  -memory file and
12b9c 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  , the journal fi
12b9d 6c 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65 64  le is .** opened
12b9e 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62   if it has not b
12b9f 65 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f 72  een already. For
12ba0 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
12ba1 65 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 0a  e, the opening .
12ba2 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ** of the journa
12ba3 6c 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 72  l file is deferr
12ba4 65 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69  ed until there i
12ba5 73 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65 64  s an actual need
12ba6 20 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74 6f   to .** write to
12ba7 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 4f   the journal. TO
12ba8 44 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20 74  DO: Why handle t
12ba9 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 64  emporary files d
12baa 69 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a 2a  ifferently?.**.*
12bab 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * If the journal
12bac 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20   file is opened 
12bad 28 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c 72  (or if it is alr
12bae 65 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65 6e  eady open), then
12baf 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68 65   a.** journal-he
12bb0 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20  ader is written 
12bb1 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
12bb2 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  it..*/.SQLITE_PR
12bb3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
12bb4 33 50 61 67 65 72 42 65 67 69 6e 28 50 61 67 65  3PagerBegin(Page
12bb5 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65  r *pPager, int e
12bb6 78 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 72 63  xFlag){.  int rc
12bb7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
12bb8 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
12bb9 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c  state!=PAGER_UNL
12bba 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 61  OCK );.  if( pPa
12bbb 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
12bbc 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20  R_SHARED ){.    
12bbd 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
12bbe 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b  pInJournal==0 );
12bbf 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45  .    assert( !ME
12bc0 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 2d 3e  MDB && !pPager->
12bc1 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 20  tempFile );..   
12bc2 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 53   /* Obtain a RES
12bc3 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68  ERVED lock on th
12bc4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
12bc5 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 70   If the exFlag p
12bc6 61 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a 20  arameter.    ** 
12bc7 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69 6d  is true, then im
12bc8 6d 65 64 69 61 74 65 6c 79 20 75 70 67 72 61 64  mediately upgrad
12bc9 65 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58 43  e this to an EXC
12bca 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65  LUSIVE lock. The
12bcb 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e  .    ** busy-han
12bcc 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 61  dler callback ca
12bcd 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 75  n be used when u
12bce 70 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 20  pgrading to the 
12bcf 45 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a 2a  EXCLUSIVE.    **
12bd0 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77   lock, but not w
12bd1 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 68  hen obtaining th
12bd2 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e  e RESERVED lock.
12bd3 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
12bd4 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70   sqlite3OsLock(p
12bd5 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52  Pager->fd, RESER
12bd6 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69  VED_LOCK);.    i
12bd7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12bd8 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
12bd9 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
12bda 52 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20 20  RESERVED;.      
12bdb 69 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20  if( exFlag ){.  
12bdc 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
12bdd 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50  _wait_on_lock(pP
12bde 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f  ager, EXCLUSIVE_
12bdf 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20  LOCK);.      }. 
12be0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
12be1 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63  the required loc
12be2 6b 73 20 77 65 72 65 20 73 75 63 63 65 73 73 66  ks were successf
12be3 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 6f  ully obtained, o
12be4 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a  pen the journal.
12be5 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 20      ** file and 
12be6 77 72 69 74 65 20 74 68 65 20 66 69 72 73 74 20  write the first 
12be7 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 74  journal-header t
12be8 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  o it..    */.   
12be9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
12bea 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73  OK && pPager->us
12beb 65 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 26 26  eJournal.     &&
12bec 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12bed 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode!=PAGER_JOUR
12bee 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20  NALMODE_OFF .   
12bef 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
12bf0 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61  ager_open_journa
12bf1 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d  l(pPager);.    }
12bf2 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4f  .  }else if( isO
12bf3 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
12bf4 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   && pPager->jour
12bf5 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20  nalOff==0 ){.   
12bf6 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73   /* This happens
12bf7 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20   when the pager 
12bf8 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65  was in exclusive
12bf9 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65  -access mode the
12bfa 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d   last.    ** tim
12bfb 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72 69  e a (read or wri
12bfc 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  te) transaction 
12bfd 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79  was successfully
12bfe 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a   concluded.    *
12bff 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 63  * by this connec
12c00 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66  tion. Instead of
12c01 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f   deleting the jo
12c02 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 61  urnal file it wa
12c03 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f  s .    ** kept o
12c04 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20 77  pen and either w
12c05 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20  as truncated to 
12c06 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20 68  0 bytes or its h
12c07 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a 2a  eader was.    **
12c08 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74   overwritten wit
12c09 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a  h zeros..    */.
12c0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
12c0b 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20  er->nRec==0 );. 
12c0c 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
12c0d 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30  r->dbOrigSize==0
12c0e 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
12c0f 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
12c10 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20  al==0 );.    rc 
12c11 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75  = pager_open_jou
12c12 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20  rnal(pPager);.  
12c13 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28  }..  PAGERTRACE(
12c14 28 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 64  ("TRANSACTION %d
12c15 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
12c16 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74  ger)));.  assert
12c17 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  ( !isOpen(pPager
12c18 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72  ->jfd) || pPager
12c19 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c  ->journalOff>0 |
12c1a 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  | rc!=SQLITE_OK 
12c1b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
12c1c 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20  }../*.** Mark a 
12c1d 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65  single data page
12c1e 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54   as writeable. T
12c1f 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74  he page is writt
12c20 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20  en into the .** 
12c21 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20  main journal or 
12c22 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72  sub-journal as r
12c23 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20  equired. If the 
12c24 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20  page is written 
12c25 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74  into.** one of t
12c26 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65  he journals, the
12c27 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62   corresponding b
12c28 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65  it is set in the
12c29 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f   .** Pager.pInJo
12c2a 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64  urnal bitvec and
12c2b 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f   the PagerSavepo
12c2c 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  int.pInSavepoint
12c2d 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61   bitvecs.** of a
12c2e 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e  ny open savepoin
12c2f 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  ts as appropriat
12c30 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
12c31 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48   pager_write(PgH
12c32 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64  dr *pPg){.  void
12c33 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70   *pData = pPg->p
12c34 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70  Data;.  Pager *p
12c35 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
12c36 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ger;.  int rc = 
12c37 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a  SQLITE_OK;..  /*
12c38 20 43 68 65 63 6b 20 66 6f 72 20 65 72 72 6f 72   Check for error
12c39 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  s.  */.  if( pPa
12c3a 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 20  ger->errCode ){ 
12c3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67  .    return pPag
12c3c 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d  er->errCode;.  }
12c3d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 72  .  if( pPager->r
12c3e 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72  eadOnly ){.    r
12c3f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52  eturn SQLITE_PER
12c40 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  M;.  }..  assert
12c41 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  ( !pPager->setMa
12c42 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b  ster );..  CHECK
12c43 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f  _PAGE(pPg);..  /
12c44 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20  * Mark the page 
12c45 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68  as dirty.  If th
12c46 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61  e page has alrea
12c47 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a  dy been written.
12c48 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72    ** to the jour
12c49 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20  nal then we can 
12c4a 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61  return right awa
12c4b 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  y..  */.  sqlite
12c4c 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
12c4d 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67  (pPg);.  if( pag
12c4e 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20  eInJournal(pPg) 
12c4f 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73  && !subjRequires
12c50 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20  Page(pPg) ){.   
12c51 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66   pPager->dbModif
12c52 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  ied = 1;.  }else
12c53 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  {..    /* If we 
12c54 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74  get this far, it
12c55 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
12c56 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65  page needs to be
12c57 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20  .    ** written 
12c58 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  to the transacti
12c59 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68  on journal or th
12c5a 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75  e ckeckpoint jou
12c5b 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62  rnal.    ** or b
12c5c 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  oth..    **.    
12c5d 2a 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74  ** First check t
12c5e 6f 20 73 65 65 20 74 68 61 74 20 74 68 65 20 74  o see that the t
12c5f 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e  ransaction journ
12c60 61 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20 20  al exists and.  
12c61 20 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20 69    ** create it i
12c62 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20  f it does not.. 
12c63 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
12c64 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21  ( pPager->state!
12c65 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b  =PAGER_UNLOCK );
12c66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
12c67 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 61 67  3PagerBegin(pPag
12c68 65 72 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  er, 0);.    if( 
12c69 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
12c6a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
12c6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
12c6c 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
12c6d 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45  e>=PAGER_RESERVE
12c6e 44 20 29 3b 0a 20 20 20 20 69 66 28 20 21 69 73  D );.    if( !is
12c6f 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
12c70 29 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65  ) && pPager->use
12c71 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20  Journal.        
12c72 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75    && pPager->jou
12c73 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f  rnalMode!=PAGER_
12c74 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20  JOURNALMODE_OFF 
12c75 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  ){.      rc = pa
12c76 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
12c77 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
12c78 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12c79 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
12c7a 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d     }.    pPager-
12c7b 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b  >dbModified = 1;
12c7c 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68 65 20 74  .  .    /* The t
12c7d 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e  ransaction journ
12c7e 61 6c 20 6e 6f 77 20 65 78 69 73 74 73 20 61 6e  al now exists an
12c7f 64 20 77 65 20 68 61 76 65 20 61 20 52 45 53 45  d we have a RESE
12c80 52 56 45 44 20 6f 72 20 61 6e 0a 20 20 20 20 2a  RVED or an.    *
12c81 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  * EXCLUSIVE lock
12c82 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   on the main dat
12c83 61 62 61 73 65 20 66 69 6c 65 2e 20 20 57 72 69  abase file.  Wri
12c84 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  te the current p
12c85 61 67 65 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68  age to.    ** th
12c86 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f  e transaction jo
12c87 75 72 6e 61 6c 20 69 66 20 69 74 20 69 73 20 6e  urnal if it is n
12c88 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79  ot there already
12c89 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
12c8a 20 21 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28   !pageInJournal(
12c8b 70 50 67 29 20 26 26 20 69 73 4f 70 65 6e 28 70  pPg) && isOpen(p
12c8c 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20  Pager->jfd) ){. 
12c8d 20 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67       if( pPg->pg
12c8e 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72  no<=pPager->dbOr
12c8f 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  igSize ){.      
12c90 20 20 75 33 32 20 63 6b 73 75 6d 3b 0a 20 20 20    u32 cksum;.   
12c91 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61       char *pData
12c92 32 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57  2;..        /* W
12c93 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 77  e should never w
12c94 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72  rite to the jour
12c95 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 70 61 67  nal file the pag
12c96 65 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a  e that.        *
12c97 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64  * contains the d
12c98 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 2e 20 20  atabase locks.  
12c99 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73  The following as
12c9a 73 65 72 74 20 76 65 72 69 66 69 65 73 0a 20 20  sert verifies.  
12c9b 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65        ** that we
12c9c 20 64 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20   do not. */.    
12c9d 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d      assert( pPg-
12c9e 3e 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f  >pgno!=PAGER_MJ_
12c9f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a  PGNO(pPager) );.
12ca0 20 20 20 20 20 20 20 20 70 44 61 74 61 32 20 3d          pData2 =
12ca1 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20   CODEC2(pPager, 
12ca2 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f  pData, pPg->pgno
12ca3 2c 20 37 29 3b 0a 20 20 20 20 20 20 20 20 63 6b  , 7);.        ck
12ca4 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75  sum = pager_cksu
12ca5 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70  m(pPager, (u8*)p
12ca6 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20  Data2);.        
12ca7 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
12ca8 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
12ca9 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
12caa 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
12cab 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
12cac 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12cad 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12cae 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
12caf 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50  >jfd, pData2, pP
12cb0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a  ager->pageSize,.
12cb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12cb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
12cb3 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
12cb4 20 2b 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20   + 4);.         
12cb5 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12cb6 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70  Off += pPager->p
12cb7 61 67 65 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20  ageSize+4;.     
12cb8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
12cb9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
12cba 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
12cbb 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61   write32bits(pPa
12cbc 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72  ger->jfd, pPager
12cbd 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 63 6b  ->journalOff, ck
12cbe 73 75 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  sum);.          
12cbf 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12cc0 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20  ff += 4;.       
12cc1 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41   }.        IOTRA
12cc2 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64 20  CE(("JOUT %p %d 
12cc3 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67  %lld %d\n", pPag
12cc4 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a  er, pPg->pgno, .
12cc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12cc6 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12cc7 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  Off, pPager->pag
12cc8 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20 20  eSize));.       
12cc9 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69   PAGER_INCR(sqli
12cca 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a  te3_pager_writej
12ccb 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20  _count);.       
12ccc 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4a 4f   PAGERTRACE(("JO
12ccd 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64  URNAL %d page %d
12cce 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73   needSync=%d has
12ccf 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20  h(%08x)\n",.    
12cd0 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44           PAGERID
12cd1 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70  (pPager), pPg->p
12cd2 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20  gno, .          
12cd3 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26     ((pPg->flags&
12cd4 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
12cd5 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61 67  ?1:0), pager_pag
12cd6 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20  ehash(pPg)));.. 
12cd7 20 20 20 20 20 20 20 2f 2a 20 45 76 65 6e 20 69         /* Even i
12cd8 66 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 6b 66  f an IO or diskf
12cd9 75 6c 6c 20 65 72 72 6f 72 20 6f 63 63 75 72 72  ull error occurr
12cda 65 64 20 77 68 69 6c 65 20 6a 6f 75 72 6e 61 6c  ed while journal
12cdb 6c 69 6e 67 20 74 68 65 0a 20 20 20 20 20 20 20  ling the.       
12cdc 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20   ** page in the 
12cdd 62 6c 6f 63 6b 20 61 62 6f 76 65 2c 20 73 65 74  block above, set
12cde 20 74 68 65 20 6e 65 65 64 2d 73 79 6e 63 20 66   the need-sync f
12cdf 6c 61 67 20 66 6f 72 20 74 68 65 20 70 61 67 65  lag for the page
12ce0 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68  ..        ** Oth
12ce1 65 72 77 69 73 65 2c 20 77 68 65 6e 20 74 68 65  erwise, when the
12ce2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
12ce3 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65  rolled back, the
12ce4 20 6c 6f 67 69 63 20 69 6e 0a 20 20 20 20 20 20   logic in.      
12ce5 20 20 2a 2a 20 70 6c 61 79 62 61 63 6b 5f 6f 6e    ** playback_on
12ce6 65 5f 70 61 67 65 28 29 20 77 69 6c 6c 20 74 68  e_page() will th
12ce7 69 6e 6b 20 74 68 61 74 20 74 68 65 20 70 61 67  ink that the pag
12ce8 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  e needs to be re
12ce9 73 74 6f 72 65 64 0a 20 20 20 20 20 20 20 20 2a  stored.        *
12cea 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  * in the databas
12ceb 65 20 66 69 6c 65 2e 20 41 6e 64 20 69 66 20 61  e file. And if a
12cec 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
12ced 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f  s while doing so
12cee 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  ,.        ** the
12cef 6e 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79  n corruption may
12cf0 20 66 6f 6c 6c 6f 77 2e 0a 20 20 20 20 20 20 20   follow..       
12cf1 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
12cf2 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20  !pPager->noSync 
12cf3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67  ){.          pPg
12cf4 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
12cf5 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20  _NEED_SYNC;.    
12cf6 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65        pPager->ne
12cf7 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
12cf8 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f      }..        /
12cf9 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  * An error has o
12cfa 63 63 75 72 72 65 64 20 77 72 69 74 69 6e 67 20  ccurred writing 
12cfb 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
12cfc 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20 20 20  ile. The .      
12cfd 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
12cfe 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20   will be rolled 
12cff 62 61 63 6b 20 62 79 20 74 68 65 20 6c 61 79 65  back by the laye
12d00 72 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20  r above..       
12d01 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
12d02 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
12d03 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
12d04 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
12d05 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
12d06 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20  >nRec++;.       
12d07 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
12d08 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29  >pInJournal!=0 )
12d09 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
12d0a 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
12d0b 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
12d0c 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  al, pPg->pgno);.
12d0d 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
12d0e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
12d0f 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  EM );.        as
12d10 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
12d11 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  _OK || rc==SQLIT
12d12 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
12d13 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f 53 61     rc |= addToSa
12d14 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70  vepointBitvecs(p
12d15 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
12d16 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
12d17 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
12d18 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
12d19 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
12d1a 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  EM );.          
12d1b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
12d1c 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
12d1d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  {.        if( !p
12d1e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74  Pager->journalSt
12d1f 61 72 74 65 64 20 26 26 20 21 70 50 61 67 65 72  arted && !pPager
12d20 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20  ->noSync ){.    
12d21 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73        pPg->flags
12d22 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53   |= PGHDR_NEED_S
12d23 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70  YNC;.          p
12d24 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
12d25 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
12d26 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
12d27 45 28 28 22 41 50 50 45 4e 44 20 25 64 20 70 61  E(("APPEND %d pa
12d28 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d 25  ge %d needSync=%
12d29 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
12d2a 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
12d2b 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f  ager), pPg->pgno
12d2c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
12d2d 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47   ((pPg->flags&PG
12d2e 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31  HDR_NEED_SYNC)?1
12d2f 3a 30 29 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  :0)));.      }. 
12d30 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
12d31 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
12d32 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20  journal is open 
12d33 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20  and the page is 
12d34 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a  not in it,.    *
12d35 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65  * then write the
12d36 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f   current page to
12d37 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   the statement j
12d38 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68  ournal.  Note th
12d39 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74  at.    ** the st
12d3a 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
12d3b 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73 20 66  format differs f
12d3c 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64  rom the standard
12d3d 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a   journal format.
12d3e 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74 20 69      ** in that i
12d3f 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68 65 63  t omits the chec
12d40 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20 68 65  ksums and the he
12d41 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ader..    */.   
12d42 20 69 66 28 20 73 75 62 6a 52 65 71 75 69 72 65   if( subjRequire
12d43 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20  sPage(pPg) ){.  
12d44 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72      rc = subjour
12d45 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 20  nalPage(pPg);.  
12d46 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70    }.  }..  /* Up
12d47 64 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73  date the databas
12d48 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72  e size and retur
12d49 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  n..  */.  assert
12d4a 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
12d4b 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b  =PAGER_SHARED );
12d4c 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64  .  if( pPager->d
12d4d 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20  bSize<pPg->pgno 
12d4e 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64  ){.    pPager->d
12d4f 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e  bSize = pPg->pgn
12d50 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  o;.  }.  return 
12d51 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72  rc;.}../*.** Mar
12d52 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 61 73  k a data page as
12d53 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 69 73   writeable. This
12d54 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65   routine must be
12d55 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a   called before .
12d56 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65  ** making change
12d57 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 68 65  s to a page. The
12d58 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 68 65   caller must che
12d59 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ck the return va
12d5a 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20  lue .** of this 
12d5b 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 65 20  function and be 
12d5c 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63  careful not to c
12d5d 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64  hange any page d
12d5e 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74  ata unless .** t
12d5f 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
12d60 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  rns SQLITE_OK..*
12d61 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65  *.** The differe
12d62 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73  nce between this
12d63 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61   function and pa
12d64 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 20 74  ger_write() is t
12d65 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63  hat this.** func
12d66 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20  tion also deals 
12d67 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c  with the special
12d68 20 63 61 73 65 20 77 68 65 72 65 20 32 20 6f 72   case where 2 or
12d69 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a 20 66   more pages.** f
12d6a 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64  it on a single d
12d6b 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e 20 74  isk sector. In t
12d6c 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d  his case all co-
12d6d 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 0a 2a  resident pages.*
12d6e 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e  * must have been
12d6f 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
12d70 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66  journal file bef
12d71 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
12d72 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
12d73 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f   occurs, SQLITE_
12d74 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65  NOMEM or an IO e
12d75 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
12d76 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 70 72  urned.** as appr
12d77 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 77 69  opriate. Otherwi
12d78 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  se, SQLITE_OK..*
12d79 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
12d7a 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
12d7b 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70  rWrite(DbPage *p
12d7c 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72  DbPage){.  int r
12d7d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
12d7e 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70    PgHdr *pPg = p
12d7f 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20  DbPage;.  Pager 
12d80 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
12d81 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50  Pager;.  Pgno nP
12d82 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d 20 28  agePerSector = (
12d83 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
12d84 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 53  ze/pPager->pageS
12d85 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61  ize);..  if( nPa
12d86 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20 29 7b  gePerSector>1 ){
12d87 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 43  .    Pgno nPageC
12d88 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f  ount;          /
12d89 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
12d8a 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62  f pages in datab
12d8b 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  ase file */.    
12d8c 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 20 20  Pgno pg1;       
12d8d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
12d8e 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73  st page of the s
12d8f 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63  ector pPg is loc
12d90 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20  ated on. */.    
12d91 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20  int nPage;      
12d92 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
12d93 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 61  ber of pages sta
12d94 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 6f 20  rting at pg1 to 
12d95 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69  journal */.    i
12d96 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
12d97 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
12d98 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
12d99 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30  int needSync = 0
12d9a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
12d9b 65 20 69 66 20 61 6e 79 20 70 61 67 65 20 68 61  e if any page ha
12d9c 73 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s PGHDR_NEED_SYN
12d9d 43 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74  C */..    /* Set
12d9e 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66   the doNotSync f
12d9f 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69  lag to 1. This i
12da0 73 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e  s because we can
12da1 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72  not allow a jour
12da2 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 65  nal.    ** heade
12da3 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  r to be written 
12da4 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 65  between the page
12da5 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 74  s journaled by t
12da6 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  his function..  
12da7 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
12da8 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61   !MEMDB );.    a
12da9 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
12daa 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20  oNotSync==0 );. 
12dab 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74     pPager->doNot
12dac 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f  Sync = 1;..    /
12dad 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 73  * This trick ass
12dae 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 74  umes that both t
12daf 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64  he page-size and
12db0 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 65   sector-size are
12db1 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67  .    ** an integ
12db2 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 49  er power of 2. I
12db3 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20  t sets variable 
12db4 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e 74  pg1 to the ident
12db5 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20  ifier.    ** of 
12db6 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f  the first page o
12db7 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67  f the sector pPg
12db8 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a   is located on..
12db9 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d      */.    pg1 =
12dba 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20   ((pPg->pgno-1) 
12dbb 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 74  & ~(nPagePerSect
12dbc 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20  or-1)) + 1;..   
12dbd 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12dbe 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28  ecount(pPager, (
12dbf 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e  int *)&nPageCoun
12dc0 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d  t);.    if( pPg-
12dc1 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74  >pgno>nPageCount
12dc2 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20   ){.      nPage 
12dc3 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70  = (pPg->pgno - p
12dc4 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65  g1)+1;.    }else
12dc5 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50   if( (pg1+nPageP
12dc6 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67  erSector-1)>nPag
12dc7 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20  eCount ){.      
12dc8 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75  nPage = nPageCou
12dc9 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65  nt+1-pg1;.    }e
12dca 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65  lse{.      nPage
12dcb 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f   = nPagePerSecto
12dcc 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  r;.    }.    ass
12dcd 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20  ert(nPage>0);.  
12dce 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50    assert(pg1<=pP
12dcf 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73  g->pgno);.    as
12dd0 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29  sert((pg1+nPage)
12dd1 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20  >pPg->pgno);..  
12dd2 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
12dd3 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49  Page && rc==SQLI
12dd4 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20  TE_OK; ii++){.  
12dd5 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67      Pgno pg = pg
12dd6 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64  1+ii;.      PgHd
12dd7 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20  r *pPage;.      
12dd8 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e  if( pg==pPg->pgn
12dd9 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74  o || !sqlite3Bit
12dda 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e  vecTest(pPager->
12ddb 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20  pInJournal, pg) 
12ddc 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
12ddd 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  g!=PAGER_MJ_PGNO
12dde 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  (pPager) ){.    
12ddf 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12de0 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65  e3PagerGet(pPage
12de1 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a  r, pg, &pPage);.
12de2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
12de3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
12de4 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
12de5 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67  pager_write(pPag
12de6 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
12de7 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73  if( pPage->flags
12de8 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
12de9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
12dea 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a    needSync = 1;.
12deb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73                as
12dec 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65  sert(pPager->nee
12ded 64 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  dSync);.        
12dee 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
12def 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
12df0 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ref(pPage);.    
12df1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
12df2 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
12df3 28 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72  ( (pPage = pager
12df4 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  _lookup(pPager, 
12df5 70 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  pg))!=0 ){.     
12df6 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c     if( pPage->fl
12df7 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
12df8 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20  YNC ){.         
12df9 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20   needSync = 1;. 
12dfa 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12dfb 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
12dfc 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ef(pPage);.     
12dfd 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
12dfe 20 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45   If the PGHDR_NE
12dff 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20  ED_SYNC flag is 
12e00 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74  set for any of t
12e01 68 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a  he nPage pages .
12e02 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20      ** starting 
12e03 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20  at pg1, then it 
12e04 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20  needs to be set 
12e05 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e  for all of them.
12e06 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20   Because.    ** 
12e07 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f  writing to any o
12e08 66 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61  f these nPage pa
12e09 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74  ges may damage t
12e0a 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20  he others, the. 
12e0b 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69     ** journal fi
12e0c 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20  le must contain 
12e0d 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20  sync()ed copies 
12e0e 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20  of all of them. 
12e0f 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79     ** before any
12e10 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20   of them can be 
12e11 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74  written out to t
12e12 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12e13 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
12e14 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20   needSync ){.   
12e15 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44     assert( !MEMD
12e16 42 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f 53  B && pPager->noS
12e17 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ync==0 );.      
12e18 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61  for(ii=0; ii<nPa
12e19 67 65 20 26 26 20 6e 65 65 64 53 79 6e 63 3b 20  ge && needSync; 
12e1a 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50  ii++){.        P
12e1b 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61  gHdr *pPage = pa
12e1c 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65  ger_lookup(pPage
12e1d 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20  r, pg1+ii);.    
12e1e 20 20 20 20 69 66 28 20 70 50 61 67 65 20 29 7b      if( pPage ){
12e1f 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65  .          pPage
12e20 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
12e21 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20  _NEED_SYNC;.    
12e22 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
12e23 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a  erUnref(pPage);.
12e24 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12e25 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70  }.      assert(p
12e26 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29  Pager->needSync)
12e27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73  ;.    }..    ass
12e28 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e  ert( pPager->doN
12e29 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20  otSync==1 );.   
12e2a 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79   pPager->doNotSy
12e2b 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  nc = 0;.  }else{
12e2c 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
12e2d 77 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a  write(pDbPage);.
12e2e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
12e2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
12e30 20 54 52 55 45 20 69 66 20 74 68 65 20 70 61 67   TRUE if the pag
12e31 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61  e given in the a
12e32 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76  rgument was prev
12e33 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a  iously passed.**
12e34 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72   to sqlite3Pager
12e35 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68  Write().  In oth
12e36 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e  er words, return
12e37 20 54 52 55 45 20 69 66 20 69 74 20 69 73 20 6f   TRUE if it is o
12e38 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74  k.** to change t
12e39 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
12e3a 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64  e page..*/.#ifnd
12e3b 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45  ef NDEBUG.SQLITE
12e3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
12e3d 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
12e3e 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67  able(DbPage *pPg
12e3f 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d  ){.  return pPg-
12e40 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
12e41 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  TY;.}.#endif../*
12e42 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68  .** A call to th
12e43 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73  is routine tells
12e44 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74 20   the pager that 
12e45 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  it is not necess
12e46 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20  ary to.** write 
12e47 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
12e48 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61 63 6b  on page pPg back
12e49 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76   to the disk, ev
12e4a 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61  en though.** tha
12e4b 74 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20  t page might be 
12e4c 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e  marked as dirty.
12e4d 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20    This happens, 
12e4e 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65  for example, whe
12e4f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61  n.** the page ha
12e50 73 20 62 65 65 6e 20 61 64 64 65 64 20 61 73 20  s been added as 
12e51 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72  a leaf of the fr
12e52 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74  eelist and so it
12e53 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20  s.** content no 
12e54 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a  longer matters..
12e55 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79  **.** The overly
12e56 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79  ing software lay
12e57 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
12e58 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f  utine when all o
12e59 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e  f the data.** on
12e5a 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 20   the given page 
12e5b 69 73 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70  is unused. The p
12e5c 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70  ager marks the p
12e5d 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a  age as clean so.
12e5e 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20  ** that it does 
12e5f 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 6e 20  not get written 
12e60 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54  to disk..**.** T
12e61 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 20 74  ests show that t
12e62 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
12e63 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65 20 74   can quadruple t
12e64 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67  he speed of larg
12e65 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65  e .** DELETE ope
12e66 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  rations..*/.SQLI
12e67 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12e68 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
12e69 57 72 69 74 65 28 50 67 48 64 72 20 2a 70 50 67  Write(PgHdr *pPg
12e6a 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
12e6b 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
12e6c 3b 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c  ;.  if( (pPg->fl
12e6d 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29  ags&PGHDR_DIRTY)
12e6e 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76   && pPager->nSav
12e6f 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  epoint==0 ){.   
12e70 20 50 41 47 45 52 54 52 41 43 45 28 28 22 44 4f   PAGERTRACE(("DO
12e71 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 25 64  NT_WRITE page %d
12e72 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e   of %d\n", pPg->
12e73 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50  pgno, PAGERID(pP
12e74 61 67 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54  ager)));.    IOT
12e75 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20  RACE(("CLEAN %p 
12e76 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
12e77 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70  Pg->pgno)).    p
12e78 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48  Pg->flags |= PGH
12e79 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23  DR_DONT_WRITE;.#
12e7a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45  ifdef SQLITE_CHE
12e7b 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67  CK_PAGES.    pPg
12e7c 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67  ->pageHash = pag
12e7d 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29  er_pagehash(pPg)
12e7e 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a  ;.#endif.  }.}..
12e7f 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
12e80 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
12e81 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61  increment the va
12e82 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  lue of the datab
12e83 61 73 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61  ase file .** cha
12e84 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f  nge-counter, sto
12e85 72 65 64 20 61 73 20 61 20 34 2d 62 79 74 65 20  red as a 4-byte 
12e86 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67  big-endian integ
12e87 65 72 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a  er starting at .
12e88 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74 20 32  ** byte offset 2
12e89 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 66  4 of the pager f
12e8a 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ile..**.** If th
12e8b 65 20 69 73 44 69 72 65 63 74 20 66 6c 61 67 20  e isDirect flag 
12e8c 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  is zero, then th
12e8d 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20 63 61  is is done by ca
12e8e 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65  lling .** sqlite
12e8f 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e  3PagerWrite() on
12e90 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 6d 6f   page 1, then mo
12e91 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74  difying the cont
12e92 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 70  ents of the.** p
12e93 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74 68 69  age data. In thi
12e94 73 20 63 61 73 65 20 74 68 65 20 66 69 6c 65 20  s case the file 
12e95 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 64 20  will be updated 
12e96 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74  when the current
12e97 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
12e98 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a  is committed..**
12e99 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65 63 74  .** The isDirect
12e9a 20 66 6c 61 67 20 6d 61 79 20 6f 6e 6c 79 20 62   flag may only b
12e9b 65 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68  e non-zero if th
12e9c 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f  e library was co
12e9d 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74  mpiled.** with t
12e9e 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  he SQLITE_ENABLE
12e9f 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 20 6d 61  _ATOMIC_WRITE ma
12ea0 63 72 6f 20 64 65 66 69 6e 65 64 2e 20 49 6e 20  cro defined. In 
12ea1 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 66  this case,.** if
12ea2 20 69 73 44 69 72 65 63 74 20 69 73 20 6e 6f 6e   isDirect is non
12ea3 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20  -zero, then the 
12ea4 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
12ea5 20 75 70 64 61 74 65 64 20 64 69 72 65 63 74 6c   updated directl
12ea6 79 0a 2a 2a 20 62 79 20 77 72 69 74 69 6e 67 20  y.** by writing 
12ea7 61 6e 20 75 70 64 61 74 65 64 20 76 65 72 73 69  an updated versi
12ea8 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 75 73 69  on of page 1 usi
12ea9 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65  ng a call to the
12eaa 20 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 57 72   .** sqlite3OsWr
12eab 69 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a  ite() function..
12eac 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
12ead 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63  ger_incr_changec
12eae 6f 75 6e 74 65 72 28 50 61 67 65 72 20 2a 70 50  ounter(Pager *pP
12eaf 61 67 65 72 2c 20 69 6e 74 20 69 73 44 69 72 65  ager, int isDire
12eb0 63 74 4d 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72  ctMode){.  int r
12eb1 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
12eb2 20 20 2f 2a 20 44 65 63 6c 61 72 65 20 61 6e 64    /* Declare and
12eb3 20 69 6e 69 74 69 61 6c 69 7a 65 20 63 6f 6e 73   initialize cons
12eb4 74 61 6e 74 20 69 6e 74 65 67 65 72 20 27 69 73  tant integer 'is
12eb5 44 69 72 65 63 74 27 2e 20 49 66 20 74 68 65 0a  Direct'. If the.
12eb6 20 20 2a 2a 20 61 74 6f 6d 69 63 2d 77 72 69 74    ** atomic-writ
12eb7 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
12eb8 73 20 65 6e 61 62 6c 65 64 20 69 6e 20 74 68 69  s enabled in thi
12eb9 73 20 62 75 69 6c 64 2c 20 74 68 65 6e 20 69 73  s build, then is
12eba 44 69 72 65 63 74 0a 20 20 2a 2a 20 69 73 20 69  Direct.  ** is i
12ebb 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 74 68  nitialized to th
12ebc 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  e value passed a
12ebd 73 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f  s the isDirectMo
12ebe 64 65 20 70 61 72 61 6d 65 74 65 72 0a 20 20 2a  de parameter.  *
12ebf 2a 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  * to this functi
12ec0 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  on. Otherwise, i
12ec1 74 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 20  t is always set 
12ec2 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20  to zero..  **.  
12ec3 2a 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74  ** The idea is t
12ec4 68 61 74 20 69 66 20 74 68 65 20 61 74 6f 6d 69  hat if the atomi
12ec5 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
12ec6 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 2a 2a  tion is not.  **
12ec7 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70   enabled at comp
12ec8 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 20 63 6f  ile time, the co
12ec9 6d 70 69 6c 65 72 20 63 61 6e 20 6f 6d 69 74 20  mpiler can omit 
12eca 74 68 65 20 74 65 73 74 73 20 6f 66 0a 20 20 2a  the tests of.  *
12ecb 2a 20 27 69 73 44 69 72 65 63 74 27 20 62 65 6c  * 'isDirect' bel
12ecc 6f 77 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74  ow, as well as t
12ecd 68 65 20 62 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65  he block enclose
12ece 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 22 69  d in the.  ** "i
12ecf 66 28 20 69 73 44 69 72 65 63 74 20 29 22 20 63  f( isDirect )" c
12ed0 6f 6e 64 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23  ondition..  */.#
12ed1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  ifndef SQLITE_EN
12ed2 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
12ed3 45 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73  E.  const int is
12ed4 44 69 72 65 63 74 20 3d 20 30 3b 0a 20 20 61 73  Direct = 0;.  as
12ed5 73 65 72 74 28 20 69 73 44 69 72 65 63 74 4d 6f  sert( isDirectMo
12ed6 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45  de==0 );.  UNUSE
12ed7 44 5f 50 41 52 41 4d 45 54 45 52 28 69 73 44 69  D_PARAMETER(isDi
12ed8 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65 6c 73 65  rectMode);.#else
12ed9 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 44  .  const int isD
12eda 69 72 65 63 74 20 3d 20 69 73 44 69 72 65 63 74  irect = isDirect
12edb 4d 6f 64 65 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  Mode;.#endif..  
12edc 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
12edd 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
12ede 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21  ERVED );.  if( !
12edf 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
12ee0 75 6e 74 44 6f 6e 65 20 26 26 20 70 50 61 67 65  untDone && pPage
12ee1 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20  r->dbSize>0 ){. 
12ee2 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72     PgHdr *pPgHdr
12ee3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12ee4 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f   /* Reference to
12ee5 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 20 20 75   page 1 */.    u
12ee6 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65  32 change_counte
12ee7 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
12ee8 49 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66  Initial value of
12ee9 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
12eea 66 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 61 73  field */..    as
12eeb 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74  sert( !pPager->t
12eec 65 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65  empFile && isOpe
12eed 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b  n(pPager->fd) );
12eee 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61  ..    /* Open pa
12eef 67 65 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65  ge 1 of the file
12ef0 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f   for writing. */
12ef1 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
12ef2 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72  3PagerGet(pPager
12ef3 2c 20 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20  , 1, &pPgHdr);. 
12ef4 20 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64     assert( pPgHd
12ef5 72 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  r==0 || rc==SQLI
12ef6 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a  TE_OK );..    /*
12ef7 20 49 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73   If page one was
12ef8 20 66 65 74 63 68 65 64 20 73 75 63 63 65 73 73   fetched success
12ef9 66 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20  fully, and this 
12efa 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a  function is not.
12efb 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67      ** operating
12efc 20 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c   in direct-mode,
12efd 20 6d 61 6b 65 20 70 61 67 65 20 31 20 77 72 69   make page 1 wri
12efe 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  table..    */.  
12eff 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12f00 5f 4f 4b 20 26 26 20 21 69 73 44 69 72 65 63 74  _OK && !isDirect
12f01 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
12f02 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
12f03 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 7d 0a  (pPgHdr);.    }.
12f04 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
12f05 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12f06 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  /* Increment the
12f07 20 76 61 6c 75 65 20 6a 75 73 74 20 72 65 61 64   value just read
12f08 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 62 61   and write it ba
12f09 63 6b 20 74 6f 20 62 79 74 65 20 32 34 2e 20 2a  ck to byte 24. *
12f0a 2f 0a 20 20 20 20 20 20 63 68 61 6e 67 65 5f 63  /.      change_c
12f0b 6f 75 6e 74 65 72 20 3d 20 73 71 6c 69 74 65 33  ounter = sqlite3
12f0c 47 65 74 34 62 79 74 65 28 28 75 38 2a 29 70 50  Get4byte((u8*)pP
12f0d 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
12f0e 29 3b 0a 20 20 20 20 20 20 63 68 61 6e 67 65 5f  );.      change_
12f0f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20 20 20 20 20  counter++;.     
12f10 20 70 75 74 33 32 62 69 74 73 28 28 28 63 68 61   put32bits(((cha
12f11 72 2a 29 70 50 67 48 64 72 2d 3e 70 44 61 74 61  r*)pPgHdr->pData
12f12 29 2b 32 34 2c 20 63 68 61 6e 67 65 5f 63 6f 75  )+24, change_cou
12f13 6e 74 65 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  nter);..      /*
12f14 20 49 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 64   If running in d
12f15 69 72 65 63 74 20 6d 6f 64 65 2c 20 77 72 69 74  irect mode, writ
12f16 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  e the contents o
12f17 66 20 70 61 67 65 20 31 20 74 6f 20 74 68 65 20  f page 1 to the 
12f18 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69  file. */.      i
12f19 66 28 20 69 73 44 69 72 65 63 74 20 29 7b 0a 20  f( isDirect ){. 
12f1a 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69         const voi
12f1b 64 20 2a 7a 42 75 66 20 3d 20 70 50 67 48 64 72  d *zBuf = pPgHdr
12f1c 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 20 20 20  ->pData;.       
12f1d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
12f1e 3e 64 62 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b  >dbFileSize>0 );
12f1f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
12f20 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
12f21 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70  ger->fd, zBuf, p
12f22 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
12f23 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   0);.      }..  
12f24 20 20 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74      /* If everyt
12f25 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 73 65 74  hing worked, set
12f26 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74   the changeCount
12f27 44 6f 6e 65 20 66 6c 61 67 2e 20 2a 2f 0a 20 20  Done flag. */.  
12f28 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12f29 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
12f2a 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43   pPager->changeC
12f2b 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20  ountDone = 1;.  
12f2c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
12f2d 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20   /* Release the 
12f2e 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20  page reference. 
12f2f 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  */.    sqlite3Pa
12f30 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29  gerUnref(pPgHdr)
12f31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
12f32 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63  c;.}../*.** Sync
12f33 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20   the pager file 
12f34 74 6f 20 64 69 73 6b 2e 20 54 68 69 73 20 69 73  to disk. This is
12f35 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d   a no-op for in-
12f36 6d 65 6d 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20  memory files.** 
12f37 6f 72 20 70 61 67 65 73 20 77 69 74 68 20 74 68  or pages with th
12f38 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66  e Pager.noSync f
12f39 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49  lag set..**.** I
12f3a 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  f successful, or
12f3b 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67   called on a pag
12f3c 65 72 20 66 6f 72 20 77 68 69 63 68 20 69 74 20  er for which it 
12f3d 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73  is a no-op, this
12f3e 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  .** function ret
12f3f 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  urns SQLITE_OK. 
12f40 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f  Otherwise, an IO
12f41 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
12f42 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
12f43 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
12f44 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28  qlite3PagerSync(
12f45 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
12f46 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
12f47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f48 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
12f49 20 63 6f 64 65 20 2a 2f 0a 20 20 69 66 28 20 4d   code */.  if( M
12f4a 45 4d 44 42 20 7c 7c 20 70 50 61 67 65 72 2d 3e  EMDB || pPager->
12f4b 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63  noSync ){.    rc
12f4c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
12f4d 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
12f4e 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50  sqlite3OsSync(pP
12f4f 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72  ager->fd, pPager
12f50 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20  ->sync_flags);. 
12f51 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
12f52 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68  }../*.** Sync th
12f53 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
12f54 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 50  for the pager pP
12f55 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f  ager. zMaster po
12f56 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d 65  ints to the name
12f57 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 20  .** of a master 
12f58 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61  journal file tha
12f59 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74  t should be writ
12f5a 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e 64  ten into the ind
12f5b 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e  ividual.** journ
12f5c 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72  al file. zMaster
12f5d 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68   may be NULL, wh
12f5e 69 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 74  ich is interpret
12f5f 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a  ed as no master.
12f60 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69  ** journal (a si
12f61 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
12f62 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a  ansaction)..**.*
12f63 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65  * This routine e
12f64 6e 73 75 72 65 73 20 74 68 61 74 3a 0a 2a 2a 0a  nsures that:.**.
12f65 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62  **   * The datab
12f66 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 2d  ase file change-
12f67 63 6f 75 6e 74 65 72 20 69 73 20 75 70 64 61 74  counter is updat
12f68 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6a  ed,.**   * the j
12f69 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64  ournal is synced
12f6a 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 74 6f   (unless the ato
12f6b 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69  mic-write optimi
12f6c 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 29 2c  zation is used),
12f6d 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64 69 72 74  .**   * all dirt
12f6e 79 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74  y pages are writ
12f6f 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62  ten to the datab
12f70 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a 20 20 20  ase file, .**   
12f71 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
12f72 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64  ile is truncated
12f73 20 28 69 66 20 72 65 71 75 69 72 65 64 29 2c 20   (if required), 
12f74 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64  and.**   * the d
12f75 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79 6e  atabase file syn
12f76 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ced. .**.** The 
12f77 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 61 74 20  only thing that 
12f78 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69  remains to commi
12f79 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  t the transactio
12f7a 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65  n is to finalize
12f7b 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c 20 74 72   .** (delete, tr
12f7c 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 74  uncate or zero t
12f7d 68 65 20 66 69 72 73 74 20 70 61 72 74 20 6f 66  he first part of
12f7e 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  ) the journal fi
12f7f 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65 6c 65 74  le (or .** delet
12f80 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
12f81 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70 65  rnal file if spe
12f82 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e  cified)..**.** N
12f83 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61 73  ote that if zMas
12f84 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20  ter==NULL, this 
12f85 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 69  does not overwri
12f86 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76 61  te a previous va
12f87 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f  lue.** passed to
12f88 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72   an sqlite3Pager
12f89 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29  CommitPhaseOne()
12f8a 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   call..**.** If 
12f8b 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65  the final parame
12f8c 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 69  ter - noSync - i
12f8d 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65  s true, then the
12f8e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
12f8f 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74 20  tself.** is not 
12f90 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 6c 6c  synced. The call
12f91 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c  er must call sql
12f92 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 29 20  ite3PagerSync() 
12f93 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a 20 73  directly to.** s
12f94 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65  ync the database
12f95 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63 61 6c   file before cal
12f96 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61 73 65  ling CommitPhase
12f97 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74 65 20  Two() to delete 
12f98 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
12f99 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  ile in this case
12f9a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
12f9b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
12f9c 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f  agerCommitPhaseO
12f9d 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  ne(.  Pager *pPa
12f9e 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ger,            
12f9f 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f        /* Pager o
12fa0 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  bject */.  const
12fa1 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20   char *zMaster, 
12fa2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
12fa3 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 6d   not NULL, the m
12fa4 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
12fa5 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53 79  me */.  int noSy
12fa6 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nc              
12fa7 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
12fa8 74 6f 20 6f 6d 69 74 20 74 68 65 20 78 53 79 6e  to omit the xSyn
12fa9 63 20 6f 6e 20 74 68 65 20 64 62 20 66 69 6c 65  c on the db file
12faa 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
12fab 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
12fac 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
12fad 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66  rn code */..  if
12fae 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
12faf 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
12fb0 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
12fb1 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41  .  }..  PAGERTRA
12fb2 43 45 28 28 22 44 41 54 41 42 41 53 45 20 53 59  CE(("DATABASE SY
12fb3 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73  NC: File=%s zMas
12fb4 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d 25 64 5c  ter=%s nSize=%d\
12fb5 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 61 67 65  n", .      pPage
12fb6 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 4d  r->zFilename, zM
12fb7 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 64  aster, pPager->d
12fb8 62 53 69 7a 65 29 29 3b 0a 0a 20 20 2f 2a 20 49  bSize));..  /* I
12fb9 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d  f this is an in-
12fba 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 6e 6f  memory db, or no
12fbb 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e   pages have been
12fbc 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f 72 20   written to, or 
12fbd 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69  this.  ** functi
12fbe 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  on has already b
12fbf 65 65 6e 20 63 61 6c 6c 65 64 2c 20 69 74 20 69  een called, it i
12fc0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a  s a no-op..  */.
12fc1 20 20 69 66 28 20 4d 45 4d 44 42 20 26 26 20 70    if( MEMDB && p
12fc2 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65  Pager->dbModifie
12fc3 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
12fc4 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50  BackupRestart(pP
12fc5 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a  ager->pBackup);.
12fc6 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67    }else if( pPag
12fc7 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52  er->state!=PAGER
12fc8 5f 53 59 4e 43 45 44 20 26 26 20 70 50 61 67 65  _SYNCED && pPage
12fc9 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b  r->dbModified ){
12fca 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c  ..    /* The fol
12fcb 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64  lowing block upd
12fcc 61 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d  ates the change-
12fcd 63 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79  counter. Exactly
12fce 20 68 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20 64   how it.    ** d
12fcf 6f 65 73 20 74 68 69 73 20 64 65 70 65 6e 64 73  oes this depends
12fd0 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e   on whether or n
12fd1 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70  ot the atomic-up
12fd2 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  date optimizatio
12fd3 6e 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 6e 61  n.    ** was ena
12fd4 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20  bled at compile 
12fd5 74 69 6d 65 2c 20 61 6e 64 20 69 66 20 74 68 69  time, and if thi
12fd6 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 65  s transaction me
12fd7 65 74 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20  ets the .    ** 
12fd8 72 75 6e 74 69 6d 65 20 63 72 69 74 65 72 69 61  runtime criteria
12fd9 20 74 6f 20 75 73 65 20 74 68 65 20 6f 70 65 72   to use the oper
12fda 61 74 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a 20  ation: .    **. 
12fdb 20 20 20 2a 2a 20 20 20 20 2a 20 54 68 65 20 66     **    * The f
12fdc 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f  ile-system suppo
12fdd 72 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77  rts the atomic-w
12fde 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f  rite property fo
12fdf 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c  r.    **      bl
12fe0 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67  ocks of size pag
12fe1 65 2d 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 20  e-size, and .   
12fe2 20 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f   **    * This co
12fe3 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74  mmit is not part
12fe4 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65   of a multi-file
12fe5 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e   transaction, an
12fe6 64 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78  d.    **    * Ex
12fe7 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68  actly one page h
12fe8 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  as been modified
12fe9 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68   and store in th
12fea 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
12feb 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66      **.    ** If
12fec 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f   the optimizatio
12fed 6e 20 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c 65  n was not enable
12fee 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d  d at compile tim
12fef 65 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20  e, then the.    
12ff0 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68  ** pager_incr_ch
12ff1 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66 75  angecounter() fu
12ff2 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
12ff3 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63   to update the c
12ff4 68 61 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f 75  hange.    ** cou
12ff5 6e 74 65 72 20 69 6e 20 27 69 6e 64 69 72 65 63  nter in 'indirec
12ff6 74 2d 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 20  t-mode'. If the 
12ff7 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20  optimization is 
12ff8 63 6f 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 0a  compiled in but.
12ff9 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 70      ** is not ap
12ffa 70 6c 69 63 61 62 6c 65 20 74 6f 20 74 68 69 73  plicable to this
12ffb 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63 61   transaction, ca
12ffc 6c 6c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  ll sqlite3Journa
12ffd 6c 43 72 65 61 74 65 28 29 0a 20 20 20 20 2a 2a  lCreate().    **
12ffe 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
12fff 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  e journal file h
13000 61 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65 6e  as actually been
13001 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63   created, then c
13002 61 6c 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 72  all.    ** pager
13003 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
13004 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20  ter() to update 
13005 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
13006 65 72 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20  er in indirect. 
13007 20 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20     ** mode. .   
13008 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72   **.    ** Other
13009 77 69 73 65 2c 20 69 66 20 74 68 65 20 6f 70 74  wise, if the opt
1300a 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f 74  imization is bot
1300b 68 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 61 70  h enabled and ap
1300c 70 6c 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a 2a  plicable,.    **
1300d 20 74 68 65 6e 20 63 61 6c 6c 20 70 61 67 65 72   then call pager
1300e 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
1300f 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20  ter() to update 
13010 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
13011 65 72 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64 69  er.    ** in 'di
13012 72 65 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20 74  rect' mode. In t
13013 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75  his case the jou
13014 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e  rnal file will n
13015 65 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 63  ever be.    ** c
13016 72 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20  reated for this 
13017 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20  transaction..   
13018 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
13019 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
1301a 57 52 49 54 45 0a 20 20 20 20 50 67 48 64 72 20  WRITE.    PgHdr 
1301b 2a 70 50 67 3b 0a 20 20 20 20 61 73 73 65 72 74  *pPg;.    assert
1301c 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
1301d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  >jfd) || pPager-
1301e 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
1301f 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
13020 4f 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20 21  OFF );.    if( !
13021 7a 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70 65  zMaster && isOpe
13022 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 0a  n(pPager->jfd) .
13023 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e       && pPager->
13024 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c  journalOff==jrnl
13025 42 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 65  BufferSize(pPage
13026 72 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 67  r) .     && pPag
13027 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 67  er->dbSize>=pPag
13028 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a 20  er->dbFileSize. 
13029 20 20 20 20 26 26 20 28 30 3d 3d 28 70 50 67 20      && (0==(pPg 
1302a 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44  = sqlite3PcacheD
1302b 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d  irtyList(pPager-
1302c 3e 70 50 43 61 63 68 65 29 29 20 7c 7c 20 30 3d  >pPCache)) || 0=
1302d 3d 70 50 67 2d 3e 70 44 69 72 74 79 29 0a 20 20  =pPg->pDirty).  
1302e 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70    ){.      /* Up
1302f 64 61 74 65 20 74 68 65 20 64 62 20 66 69 6c 65  date the db file
13030 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
13031 76 69 61 20 74 68 65 20 64 69 72 65 63 74 2d 77  via the direct-w
13032 72 69 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 65  rite method. The
13033 20 0a 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f   .      ** follo
13034 77 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d  wing call will m
13035 6f 64 69 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d  odify the in-mem
13036 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69  ory representati
13037 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 0a 20 20  on of page 1 .  
13038 20 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64      ** to includ
13039 65 20 74 68 65 20 75 70 64 61 74 65 64 20 63 68  e the updated ch
1303a 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64  ange counter and
1303b 20 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 65   then write page
1303c 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 72   1 .      ** dir
1303d 65 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74  ectly to the dat
1303e 61 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63 61  abase file. Beca
1303f 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d 69  use of the atomi
13040 63 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a  c-write .      *
13041 2a 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68  * property of th
13042 65 20 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74  e host file-syst
13043 65 6d 2c 20 74 68 69 73 20 69 73 20 73 61 66 65  em, this is safe
13044 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
13045 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72   rc = pager_incr
13046 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70  _changecounter(p
13047 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 7d  Pager, 1);.    }
13048 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
13049 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43   sqlite3JournalC
1304a 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  reate(pPager->jf
1304b 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  d);.      if( rc
1304c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1304d 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65         rc = page
1304e 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75  r_incr_changecou
1304f 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b  nter(pPager, 0);
13050 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
13051 65 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 70 61  else.    rc = pa
13052 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63  ger_incr_changec
13053 6f 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30  ounter(pPager, 0
13054 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66  );.#endif.    if
13055 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13056 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68  ) goto commit_ph
13057 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20  ase_one_exit;.. 
13058 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72     /* If this tr
13059 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61  ansaction has ma
1305a 64 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  de the database 
1305b 73 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c  smaller, then al
1305c 6c 20 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 62  l pages.    ** b
1305d 65 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 62  eing discarded b
1305e 79 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e  y the truncation
1305f 20 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 6e   must be written
13060 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   to the journal.
13061 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68 69      ** file. Thi
13062 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65  s can only happe
13063 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  n in auto-vacuum
13064 20 6d 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20   mode..    **.  
13065 20 20 2a 2a 20 42 65 66 6f 72 65 20 72 65 61 64    ** Before read
13066 69 6e 67 20 74 68 65 20 70 61 67 65 73 20 77 69  ing the pages wi
13067 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20  th page numbers 
13068 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
13069 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20  .    ** current 
1306a 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64  value of Pager.d
1306b 62 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a  bSize, set dbSiz
1306c 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61  e back to the va
1306d 6c 75 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  lue.    ** that 
1306e 69 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 73  it took at the s
1306f 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e  tart of the tran
13070 73 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69  saction. Otherwi
13071 73 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 63  se, the.    ** c
13072 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50  alls to sqlite3P
13073 61 67 65 72 47 65 74 28 29 20 72 65 74 75 72 6e  agerGet() return
13074 20 7a 65 72 6f 65 64 20 70 61 67 65 73 20 69 6e   zeroed pages in
13075 73 74 65 61 64 20 6f 66 20 0a 20 20 20 20 2a 2a  stead of .    **
13076 20 72 65 61 64 69 6e 67 20 64 61 74 61 20 66 72   reading data fr
13077 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
13078 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  file..    */.#if
13079 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1307a 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
1307b 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  if( pPager->dbSi
1307c 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  ze<pPager->dbOri
1307d 67 53 69 7a 65 0a 20 20 20 20 20 26 26 20 70 50  gSize.     && pP
1307e 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
1307f 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e!=PAGER_JOURNAL
13080 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20 29 7b  MODE_OFF .    ){
13081 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20  .      Pgno i;  
13082 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13083 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13084 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
13085 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 63  iable */.      c
13086 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 70 20  onst Pgno iSkip 
13087 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  = PAGER_MJ_PGNO(
13088 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 6e 64  pPager); /* Pend
13089 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 2a 2f  ing lock page */
1308a 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e  .      const Pgn
1308b 6f 20 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65  o dbSize = pPage
1308c 72 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 20 20  r->dbSize;      
1308d 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6d 61   /* Database ima
1308e 67 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20 20 20  ge size */ .    
1308f 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
13090 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69   = pPager->dbOri
13091 67 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 6f 72  gSize;.      for
13092 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 3c  ( i=dbSize+1; i<
13093 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  =pPager->dbOrigS
13094 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20  ize; i++ ){.    
13095 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
13096 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65  BitvecTest(pPage
13097 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69  r->pInJournal, i
13098 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b  ) && i!=iSkip ){
13099 0a 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72  .          PgHdr
1309a 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20   *pPage;        
1309b 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20       /* Page to 
1309c 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 20  journal */.     
1309d 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1309e 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72  3PagerGet(pPager
1309f 2c 20 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20  , i, &pPage);.  
130a0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
130a1 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
130a2 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e   commit_phase_on
130a3 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20  e_exit;.        
130a4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
130a5 67 65 72 57 72 69 74 65 28 70 50 61 67 65 29 3b  gerWrite(pPage);
130a6 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
130a7 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
130a8 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ge);.          i
130a9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
130aa 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70   ) goto commit_p
130ab 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20  hase_one_exit;. 
130ac 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
130ad 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e   .      pPager->
130ae 64 62 53 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b  dbSize = dbSize;
130af 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
130b0 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
130b1 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
130b2 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ame into the jou
130b3 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 20  rnal file. If a 
130b4 6d 61 73 74 65 72 20 0a 20 20 20 20 2a 2a 20 6a  master .    ** j
130b5 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
130b6 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
130b7 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  n written to the
130b8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a   journal file, .
130b9 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61      ** or if zMa
130ba 73 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f  ster is NULL (no
130bb 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29   master journal)
130bc 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c  , then this call
130bd 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20   is a no-op..   
130be 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 72 69   */.    rc = wri
130bf 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  teMasterJournal(
130c0 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29  pPager, zMaster)
130c1 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
130c2 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63  LITE_OK ) goto c
130c3 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f  ommit_phase_one_
130c4 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 79  exit;..    /* Sy
130c5 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nc the journal f
130c6 69 6c 65 2e 20 49 66 20 74 68 65 20 61 74 6f 6d  ile. If the atom
130c7 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69  ic-update optimi
130c8 7a 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 0a  zation is being.
130c9 20 20 20 20 2a 2a 20 75 73 65 64 2c 20 74 68 69      ** used, thi
130ca 73 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20  s call will not 
130cb 63 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e  create the journ
130cc 61 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f  al file or perfo
130cd 72 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20 72 65  rm any.    ** re
130ce 61 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20  al IO..    */.  
130cf 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e    rc = syncJourn
130d0 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
130d1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
130d2 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f  K ) goto commit_
130d3 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a  phase_one_exit;.
130d4 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6c  .    /* Write al
130d5 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 74 6f  l dirty pages to
130d6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
130d7 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  le. */.    rc = 
130d8 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65  pager_write_page
130d9 6c 69 73 74 28 73 71 6c 69 74 65 33 50 63 61 63  list(sqlite3Pcac
130da 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67  heDirtyList(pPag
130db 65 72 2d 3e 70 50 43 61 63 68 65 29 29 3b 0a 20  er->pPCache));. 
130dc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
130dd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73  E_OK ){.      as
130de 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
130df 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29  _IOERR_BLOCKED )
130e0 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6d  ;.      goto com
130e1 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78  mit_phase_one_ex
130e2 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  it;.    }.    sq
130e3 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
130e4 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61  All(pPager->pPCa
130e5 63 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  che);..    /* If
130e6 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73   the file on dis
130e7 6b 20 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d  k is not the sam
130e8 65 20 73 69 7a 65 20 61 73 20 74 68 65 20 64 61  e size as the da
130e9 74 61 62 61 73 65 20 69 6d 61 67 65 2c 0a 20 20  tabase image,.  
130ea 20 20 2a 2a 20 74 68 65 6e 20 75 73 65 20 70 61    ** then use pa
130eb 67 65 72 5f 74 72 75 6e 63 61 74 65 20 74 6f 20  ger_truncate to 
130ec 67 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b 20 74  grow or shrink t
130ed 68 65 20 66 69 6c 65 20 68 65 72 65 2e 0a 20 20  he file here..  
130ee 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61    */.    if( pPa
130ef 67 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61  ger->dbSize!=pPa
130f0 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20  ger->dbFileSize 
130f1 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e  ){.      Pgno nN
130f2 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ew = pPager->dbS
130f3 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64  ize - (pPager->d
130f4 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f  bSize==PAGER_MJ_
130f5 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0a 20  PGNO(pPager));. 
130f6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
130f7 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
130f8 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20  R_EXCLUSIVE );. 
130f9 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
130fa 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c  truncate(pPager,
130fb 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66   nNew);.      if
130fc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
130fd 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68  ) goto commit_ph
130fe 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20  ase_one_exit;.  
130ff 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61    }..    /* Fina
13100 6c 6c 79 2c 20 73 79 6e 63 20 74 68 65 20 64 61  lly, sync the da
13101 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a  tabase file. */.
13102 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d      if( !pPager-
13103 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 79  >noSync && !noSy
13104 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  nc ){.      rc =
13105 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
13106 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65  Pager->fd, pPage
13107 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a  r->sync_flags);.
13108 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43      }.    IOTRAC
13109 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e 22  E(("DBSYNC %p\n"
1310a 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20  , pPager))..    
1310b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
1310c 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20 20  PAGER_SYNCED;.  
1310d 7d 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f  }..commit_phase_
1310e 6f 6e 65 5f 65 78 69 74 3a 0a 20 20 69 66 28 20  one_exit:.  if( 
1310f 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
13110 5f 42 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20  _BLOCKED ){.    
13111 2f 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68  /* pager_incr_ch
13112 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 6d 61  angecounter() ma
13113 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62 74  y attempt to obt
13114 61 69 6e 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ain an exclusive
13115 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 74 6f 20  .    ** lock to 
13116 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20  spill the cache 
13117 61 6e 64 20 72 65 74 75 72 6e 20 49 4f 45 52 52  and return IOERR
13118 5f 42 4c 4f 43 4b 45 44 2e 20 42 75 74 20 73 69  _BLOCKED. But si
13119 6e 63 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 72  nce .    ** ther
1311a 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74  e is no chance t
1311b 68 65 20 63 61 63 68 65 20 69 73 20 69 6e 63 6f  he cache is inco
1311c 6e 73 69 73 74 65 6e 74 2c 20 69 74 20 69 73 0a  nsistent, it is.
1311d 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f      ** better to
1311e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
1311f 55 53 59 2e 0a 20 20 20 20 2a 2a 2f 0a 20 20 20  USY..    **/.   
13120 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
13121 59 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  Y;.  }.  return 
13122 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68  rc;.}.../*.** Wh
13123 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
13124 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20   is called, the 
13125 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
13126 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c  s been completel
13127 79 0a 2a 2a 20 75 70 64 61 74 65 64 20 74 6f 20  y.** updated to 
13128 72 65 66 6c 65 63 74 20 74 68 65 20 63 68 61 6e  reflect the chan
13129 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20  ges made by the 
1312a 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
1312b 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 6e 63 65  ion and.** synce
1312c 64 20 74 6f 20 64 69 73 6b 2e 20 54 68 65 20 6a  d to disk. The j
1312d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c  ournal file stil
1312e 6c 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  l exists in the 
1312f 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a 2a 2a 20  file-system .** 
13130 74 68 6f 75 67 68 2c 20 61 6e 64 20 69 66 20 61  though, and if a
13131 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20   failure occurs 
13132 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 74  at this point it
13133 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79   will eventually
13134 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 73 20 61  .** be used as a
13135 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64   hot-journal and
13136 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
13137 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 20  nsaction rolled 
13138 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  back..**.** This
13139 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69   function finali
1313a 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  zes the journal 
1313b 66 69 6c 65 2c 20 65 69 74 68 65 72 20 62 79 20  file, either by 
1313c 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a 20 74 72  deleting, .** tr
1313d 75 6e 63 61 74 69 6e 67 20 6f 72 20 70 61 72 74  uncating or part
1313e 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 20 69 74  ially zeroing it
1313f 2c 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  , so that it can
13140 6e 6f 74 20 62 65 20 75 73 65 64 20 0a 2a 2a 20  not be used .** 
13141 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  for hot-journal 
13142 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 65 20 74  rollback. Once t
13143 68 69 73 20 69 73 20 64 6f 6e 65 20 74 68 65 20  his is done the 
13144 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
13145 2a 20 69 72 72 65 76 6f 63 61 62 6c 79 20 63 6f  * irrevocably co
13146 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  mmitted..**.** I
13147 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
13148 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  s, an IO error c
13149 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
1314a 61 6e 64 20 74 68 65 20 70 61 67 65 72 0a 2a 2a  and the pager.**
1314b 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74 68 65 20   moves into the 
1314c 65 72 72 6f 72 20 73 74 61 74 65 2e 20 4f 74 68  error state. Oth
1314d 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f  erwise, SQLITE_O
1314e 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  K is returned..*
1314f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13150 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13151 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
13152 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
13153 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
13154 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
13155 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
13156 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 44   code */..  /* D
13157 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64 20 69 66  o not proceed if
13158 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 6c   the pager is al
13159 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72  ready in the err
1315a 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69  or state. */.  i
1315b 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
1315c 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  de ){.    return
1315d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
1315e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73  ;.  }..  /* This
1315f 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
13160 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69   not be called i
13161 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6e  f the pager is n
13162 6f 74 20 69 6e 20 61 74 20 6c 65 61 73 74 0a 20  ot in at least. 
13163 20 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56   ** PAGER_RESERV
13164 45 44 20 73 74 61 74 65 2e 20 41 6e 64 20 69 6e  ED state. And in
13165 64 65 65 64 20 53 51 4c 69 74 65 20 6e 65 76 65  deed SQLite neve
13166 72 20 64 6f 65 73 20 74 68 69 73 2e 20 42 75 74  r does this. But
13167 20 69 74 20 69 73 0a 20 20 2a 2a 20 6e 69 63 65   it is.  ** nice
13168 20 74 6f 20 68 61 76 65 20 74 68 69 73 20 64 65   to have this de
13169 66 65 6e 73 69 76 65 20 62 6c 6f 63 6b 20 68 65  fensive block he
1316a 72 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2f 0a  re anyway..  */.
1316b 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67    if( NEVER(pPag
1316c 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f  er->state<PAGER_
1316d 52 45 53 45 52 56 45 44 29 20 29 7b 0a 20 20 20  RESERVED) ){.   
1316e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1316f 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
13170 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  An optimization.
13171 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
13172 20 77 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c   was not actuall
13173 79 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e  y modified durin
13174 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 72 61 6e  g.  ** this tran
13175 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 70 61 67  saction, the pag
13176 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e  er is running in
13177 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20   exclusive-mode 
13178 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 73 69 6e  and is.  ** usin
13179 67 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  g persistent jou
1317a 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 68 69 73  rnals, then this
1317b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
1317c 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  o-op..  **.  ** 
1317d 54 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  The start of the
1317e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75   journal file cu
1317f 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73  rrently contains
13180 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61   a single journa
13181 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77  l .  ** header w
13182 69 74 68 20 74 68 65 20 6e 52 65 63 20 66 69 65  ith the nRec fie
13183 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20  ld set to 0. If 
13184 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c 20 69  such a journal i
13185 73 20 75 73 65 64 20 61 73 0a 20 20 2a 2a 20 61  s used as.  ** a
13186 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64 75 72   hot-journal dur
13187 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  ing hot-journal 
13188 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68 61 6e  rollback, 0 chan
13189 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61 64 65  ges will be made
1318a 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 61 74  .  ** to the dat
1318b 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f 20 74  abase file. So t
1318c 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
1318d 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f 75 72  to zero the jour
1318e 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72  nal .  ** header
1318f 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65  . Since the page
13190 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76  r is in exclusiv
13191 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20 69 73  e mode, there is
13192 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20 74 6f   no need.  ** to
13193 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 6b 73 20   drop any locks 
13194 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  either..  */.  i
13195 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64  f( pPager->dbMod
13196 69 66 69 65 64 3d 3d 30 20 26 26 20 70 50 61 67  ified==0 && pPag
13197 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
13198 65 20 0a 20 20 20 26 26 20 70 50 61 67 65 72 2d  e .   && pPager-
13199 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
1319a 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
1319b 50 45 52 53 49 53 54 0a 20 20 29 7b 0a 20 20 20  PERSIST.  ){.   
1319c 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1319d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55  >journalOff==JOU
1319e 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
1319f 65 72 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72  er) );.    retur
131a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
131a1 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ..  PAGERTRACE((
131a2 22 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50  "COMMIT %d\n", P
131a3 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29  AGERID(pPager)))
131a4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
131a5 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
131a6 5f 53 59 4e 43 45 44 20 7c 7c 20 4d 45 4d 44 42  _SYNCED || MEMDB
131a7 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 64 62 4d   || !pPager->dbM
131a8 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 72 63 20  odified );.  rc 
131a9 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e  = pager_end_tran
131aa 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20  saction(pPager, 
131ab 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65  pPager->setMaste
131ac 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 61 67  r);.  return pag
131ad 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
131ae 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   rc);.}../*.** R
131af 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e  ollback all chan
131b0 67 65 73 2e 20 54 68 65 20 64 61 74 61 62 61 73  ges. The databas
131b1 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20  e falls back to 
131b2 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d 6f 64  PAGER_SHARED mod
131b3 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  e..**.** This fu
131b4 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20  nction performs 
131b5 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a 2a 2a  two tasks:.**.**
131b6 20 20 20 31 29 20 49 74 20 72 6f 6c 6c 73 20 62     1) It rolls b
131b7 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ack the journal 
131b8 66 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e 67 20  file, restoring 
131b9 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
131ba 65 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 69  e and .**      i
131bb 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 70  n-memory cache p
131bc 61 67 65 73 20 74 6f 20 74 68 65 20 73 74 61 74  ages to the stat
131bd 65 20 74 68 65 79 20 77 65 72 65 20 69 6e 20 77  e they were in w
131be 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
131bf 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 77 61 73 20  ion.**      was 
131c0 6f 70 65 6e 65 64 2c 20 61 6e 64 0a 2a 2a 20 20  opened, and.**  
131c1 20 32 29 20 49 74 20 66 69 6e 61 6c 69 7a 65 73   2) It finalizes
131c2 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
131c3 65 2c 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  e, so that it is
131c4 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 20 68 6f   not used for ho
131c5 74 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61  t.**      rollba
131c6 63 6b 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20  ck at any point 
131c7 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 2a  in the future..*
131c8 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74 6f 20  *.** subject to 
131c9 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75  the following qu
131ca 61 6c 69 66 69 63 61 74 69 6f 6e 73 3a 0a 2a 2a  alifications:.**
131cb 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 6a 6f 75  .** * If the jou
131cc 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74  rnal file is not
131cd 20 79 65 74 20 6f 70 65 6e 20 77 68 65 6e 20 74   yet open when t
131ce 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
131cf 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 74 68 65  called,.**   the
131d0 6e 20 6f 6e 6c 79 20 28 32 29 20 69 73 20 70 65  n only (2) is pe
131d1 72 66 6f 72 6d 65 64 2e 20 49 6e 20 74 68 69 73  rformed. In this
131d2 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e   case there is n
131d3 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a  o journal file.*
131d4 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b  *   to roll back
131d5 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 69 6e 20  ..**.** * If in 
131d6 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f  an error state o
131d7 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
131d8 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 74 61 73 6b  _FULL, then task
131d9 20 28 31 29 20 69 73 20 0a 2a 2a 20 20 20 70 65   (1) is .**   pe
131da 72 66 6f 72 6d 65 64 2e 20 49 66 20 73 75 63 63  rformed. If succ
131db 65 73 73 66 75 6c 2c 20 74 61 73 6b 20 28 32 29  essful, task (2)
131dc 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20  . Regardless of 
131dd 74 68 65 20 6f 75 74 63 6f 6d 65 0a 2a 2a 20 20  the outcome.**  
131de 20 6f 66 20 65 69 74 68 65 72 2c 20 74 68 65 20   of either, the 
131df 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f  error state erro
131e0 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
131e1 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
131e2 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 65 69 74 68  .**   (i.e. eith
131e3 65 72 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  er SQLITE_IOERR 
131e4 6f 72 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  or SQLITE_CORRUP
131e5 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74  T)..**.** * If t
131e6 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 50  he pager is in P
131e7 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74  AGER_RESERVED st
131e8 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 6d 70  ate, then attemp
131e9 74 20 28 31 29 2e 20 57 68 65 74 68 65 72 0a 2a  t (1). Whether.*
131ea 2a 20 20 20 6f 72 20 6e 6f 74 20 28 31 29 20 69  *   or not (1) i
131eb 73 20 73 75 63 63 75 73 73 66 75 6c 2c 20 61 6c  s succussful, al
131ec 73 6f 20 61 74 74 65 6d 70 74 20 28 32 29 2e 20  so attempt (2). 
131ed 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  If successful, r
131ee 65 74 75 72 6e 0a 2a 2a 20 20 20 53 51 4c 49 54  eturn.**   SQLIT
131ef 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c  E_OK. Otherwise,
131f0 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72   enter the error
131f1 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 75 72   state and retur
131f2 6e 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20  n the first .** 
131f3 20 20 65 72 72 6f 72 20 63 6f 64 65 20 65 6e 63    error code enc
131f4 6f 75 6e 74 65 72 65 64 2e 20 0a 2a 2a 0a 2a 2a  ountered. .**.**
131f5 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20     In this case 
131f6 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e  there is no chan
131f7 63 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ce that the data
131f8 62 61 73 65 20 77 61 73 20 77 72 69 74 74 65 6e  base was written
131f9 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 6f 20 69 73   to. .**   So is
131fa 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a   safe to finaliz
131fb 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
131fc 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 70  le even if the p
131fd 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 20 20 28 6f  layback .**   (o
131fe 70 65 72 61 74 69 6f 6e 20 31 29 20 66 61 69 6c  peration 1) fail
131ff 65 64 2e 20 48 6f 77 65 76 65 72 20 74 68 65 20  ed. However the 
13200 70 61 67 65 72 20 6d 75 73 74 20 65 6e 74 65 72  pager must enter
13201 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
13202 0a 2a 2a 20 20 20 61 73 20 74 68 65 20 63 6f 6e  .**   as the con
13203 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69 6e 2d  tents of the in-
13204 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 61 72 65  memory cache are
13205 20 6e 6f 77 20 73 75 73 70 65 63 74 2e 0a 2a 2a   now suspect..**
13206 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c 79 2c 20 69  .** * Finally, i
13207 66 20 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55  f in PAGER_EXCLU
13208 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e  SIVE state, then
13209 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 4f 6e   attempt (1). On
1320a 6c 79 0a 2a 2a 20 20 20 61 74 74 65 6d 70 74 20  ly.**   attempt 
1320b 28 32 29 20 69 66 20 28 31 29 20 69 73 20 73 75  (2) if (1) is su
1320c 63 63 65 73 73 66 75 6c 2e 20 52 65 74 75 72 6e  ccessful. Return
1320d 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75   SQLITE_OK if su
1320e 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 20 20 6f  ccessful,.**   o
1320f 74 68 65 72 77 69 73 65 20 65 6e 74 65 72 20 74  therwise enter t
13210 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 61  he error state a
13211 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  nd return the er
13212 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 74 68  ror code from th
13213 65 20 0a 2a 2a 20 20 20 66 61 69 6c 69 6e 67 20  e .**   failing 
13214 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  operation..**.**
13215 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20     In this case 
13216 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13217 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20  e may have been 
13218 77 72 69 74 74 65 6e 20 74 6f 2e 20 53 6f 20 69  written to. So i
13219 66 20 74 68 65 0a 2a 2a 20 20 20 70 6c 61 79 62  f the.**   playb
1321a 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 64 69  ack operation di
1321b 64 20 6e 6f 74 20 73 75 63 63 65 65 64 20 69 74  d not succeed it
1321c 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61   would not be sa
1321d 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65 0a 2a  fe to finalize.*
1321e 2a 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  *   the journal 
1321f 66 69 6c 65 2e 20 49 74 20 6e 65 65 64 73 20 74  file. It needs t
13220 6f 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65  o be left in the
13221 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 6f 20   file-system so 
13222 74 68 61 74 0a 2a 2a 20 20 20 73 6f 6d 65 20 6f  that.**   some o
13223 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e  ther process can
13224 20 75 73 65 20 69 74 20 74 6f 20 72 65 73 74 6f   use it to resto
13225 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
13226 73 74 61 74 65 20 28 62 79 0a 2a 2a 20 20 20 68  state (by.**   h
13227 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot-journal rollb
13228 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ack)..*/.SQLITE_
13229 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1322a 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
1322b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
1322c 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1322d 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
1322e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
1322f 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41 47 45  n code */.  PAGE
13230 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42 41 43  RTRACE(("ROLLBAC
13231 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  K %d\n", PAGERID
13232 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 69 66  (pPager)));.  if
13233 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64  ( !pPager->dbMod
13234 69 66 69 65 64 20 7c 7c 20 21 69 73 4f 70 65 6e  ified || !isOpen
13235 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
13236 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
13237 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  end_transaction(
13238 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e  pPager, pPager->
13239 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 7d 65  setMaster);.  }e
1323a 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e  lse if( pPager->
1323b 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65  errCode && pPage
1323c 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49  r->errCode!=SQLI
1323d 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 69  TE_FULL ){.    i
1323e 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
1323f 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  >=PAGER_EXCLUSIV
13240 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72  E ){.      pager
13241 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72  _playback(pPager
13242 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
13243 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72  rc = pPager->err
13244 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Code;.  }else{. 
13245 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73     if( pPager->s
13246 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45  tate==PAGER_RESE
13247 52 56 45 44 20 29 7b 0a 20 20 20 20 20 20 69 6e  RVED ){.      in
13248 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20  t rc2;.      rc 
13249 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
1324a 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20  (pPager, 0);.   
1324b 20 20 20 72 63 32 20 3d 20 70 61 67 65 72 5f 65     rc2 = pager_e
1324c 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70  nd_transaction(p
1324d 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73  Pager, pPager->s
1324e 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  etMaster);.     
1324f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13250 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
13251 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a   = rc2;.      }.
13252 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13253 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79   rc = pager_play
13254 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b  back(pPager, 0);
13255 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
13256 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20  !MEMDB ){.      
13257 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
13258 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  lid = 0;.    }..
13259 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72      /* If an err
1325a 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
1325b 20 61 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 65 20   a ROLLBACK, we 
1325c 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72  can no longer tr
1325d 75 73 74 20 74 68 65 20 70 61 67 65 72 0a 20 20  ust the pager.  
1325e 20 20 2a 2a 20 63 61 63 68 65 2e 20 53 6f 20 63    ** cache. So c
1325f 61 6c 6c 20 70 61 67 65 72 5f 65 72 72 6f 72 28  all pager_error(
13260 29 20 6f 6e 20 74 68 65 20 77 61 79 20 6f 75 74  ) on the way out
13261 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 65 72 72   to make any err
13262 6f 72 20 0a 20 20 20 20 2a 2a 20 70 65 72 73 69  or .    ** persi
13263 73 74 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  stent..    */.  
13264 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72    rc = pager_err
13265 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
13266 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13267 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
13268 20 54 52 55 45 20 69 66 20 74 68 65 20 64 61 74   TRUE if the dat
13269 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70  abase file is op
1326a 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20  ened read-only. 
1326b 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a   Return FALSE.**
1326c 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
1326d 20 69 73 20 28 69 6e 20 74 68 65 6f 72 79 29 20   is (in theory) 
1326e 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  writable..*/.SQL
1326f 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73  ITE_PRIVATE u8 s
13270 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61  qlite3PagerIsrea
13271 64 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 50 61  donly(Pager *pPa
13272 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ger){.  return p
13273 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b  Pager->readOnly;
13274 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
13275 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
13276 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
13277 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54   pager..*/.SQLIT
13278 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13279 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
1327a 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nt(Pager *pPager
1327b 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1327c 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e  te3PcacheRefCoun
1327d 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
1327e 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  e);.}../*.** Ret
1327f 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
13280 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
13281 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61  the specified pa
13282 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ge..*/.SQLITE_PR
13283 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
13284 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75  3PagerPageRefcou
13285 6e 74 28 44 62 50 61 67 65 20 2a 70 50 61 67 65  nt(DbPage *pPage
13286 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
13287 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66  te3PcachePageRef
13288 63 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a 7d 0a  count(pPage);.}.
13289 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1328a 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  EST./*.** This r
1328b 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66  outine is used f
1328c 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61  or testing and a
1328d 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f  nalysis only..*/
1328e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1328f 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65  int *sqlite3Page
13290 72 53 74 61 74 73 28 50 61 67 65 72 20 2a 70 50  rStats(Pager *pP
13291 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69 63 20  ager){.  static 
13292 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 5b 30  int a[11];.  a[0
13293 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68  ] = sqlite3Pcach
13294 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72  eRefCount(pPager
13295 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b  ->pPCache);.  a[
13296 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63  1] = sqlite3Pcac
13297 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  hePagecount(pPag
13298 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
13299 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 50 63  a[2] = sqlite3Pc
1329a 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65  acheGetCachesize
1329b 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
1329c 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 61 67  );.  a[3] = pPag
1329d 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
1329e 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72 2d 3e  ? (int) pPager->
1329f 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 20 61  dbSize : -1;.  a
132a0 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e 73 74  [4] = pPager->st
132a1 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 70 50  ate;.  a[5] = pP
132a2 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
132a3 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[6] = pPager->
132a4 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d 20 70  nHit;.  a[7] = p
132a5 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20  Pager->nMiss;.  
132a6 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 55 73  a[8] = 0;  /* Us
132a7 65 64 20 74 6f 20 62 65 20 70 50 61 67 65 72 2d  ed to be pPager-
132a8 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d  >nOvfl */.  a[9]
132a9 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 61 64   = pPager->nRead
132aa 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 61 67  ;.  a[10] = pPag
132ab 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 72 65  er->nWrite;.  re
132ac 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64 69 66  turn a;.}.#endif
132ad 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
132ae 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61  rue if this is a
132af 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  n in-memory page
132b0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
132b1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
132b2 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67  PagerIsMemdb(Pag
132b3 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
132b4 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 0a  eturn MEMDB;.}..
132b5 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74  /*.** Check that
132b6 20 74 68 65 72 65 20 61 72 65 20 61 74 20 6c 65   there are at le
132b7 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 20 73  ast nSavepoint s
132b8 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2e 20  avepoints open. 
132b9 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20  If there are.** 
132ba 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 20 74  currently less t
132bb 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 73 20  han nSavepoints 
132bc 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 6e 20  open, then open 
132bd 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65  one or more save
132be 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d 61 6b  points.** to mak
132bf 65 20 75 70 20 74 68 65 20 64 69 66 66 65 72 65  e up the differe
132c0 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75 6d 62  nce. If the numb
132c1 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73  er of savepoints
132c2 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65   is already.** e
132c3 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 6f 69  qual to nSavepoi
132c4 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  nt, then this fu
132c5 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
132c6 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65  p..**.** If a me
132c7 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
132c8 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f  fails, SQLITE_NO
132c9 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  MEM is returned.
132ca 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a   If an error .**
132cb 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70   occurs while op
132cc 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d 6a 6f  ening the sub-jo
132cd 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e  urnal file, then
132ce 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64   an IO error cod
132cf 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  e is.** returned
132d0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
132d1 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54  ITE_OK..*/.SQLIT
132d2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
132d3 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61  lite3PagerOpenSa
132d4 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
132d5 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 76 65  Pager, int nSave
132d6 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63  point){.  int rc
132d7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
132d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132d9 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
132da 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72  de */.  int nCur
132db 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e  rent = pPager->n
132dc 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20  Savepoint;      
132dd 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d    /* Current num
132de 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74  ber of savepoint
132df 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53 61 76  s */..  if( nSav
132e0 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e 74 20  epoint>nCurrent 
132e1 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  && pPager->useJo
132e2 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74  urnal ){.    int
132e3 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
132e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132e5 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
132e6 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20   variable */.   
132e7 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
132e8 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20  *aNew;          
132e9 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20           /* New 
132ea 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74  Pager.aSavepoint
132eb 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20 20 2f   array */..    /
132ec 2a 20 45 69 74 68 65 72 20 74 68 65 72 65 20 69  * Either there i
132ed 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f 75 72  s no active jour
132ee 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62 2d 6a  nal or the sub-j
132ef 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 6f  ournal is open o
132f0 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f  r .    ** the jo
132f1 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79 73 20  urnal is always 
132f2 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
132f3 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
132f4 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69  pPager->nSavepoi
132f5 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65 6e 28  nt==0 || isOpen(
132f6 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 7c 7c  pPager->sjfd) ||
132f7 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61  .            pPa
132f8 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
132f9 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
132fa 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 0a 20  ODE_MEMORY );.. 
132fb 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20 50     /* Grow the P
132fc 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20  ager.aSavepoint 
132fd 61 72 72 61 79 20 75 73 69 6e 67 20 72 65 61 6c  array using real
132fe 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20 53 51  loc(). Return SQ
132ff 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20 20 2a  LITE_NOMEM.    *
13300 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  * if the allocat
13301 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68 65 72  ion fails. Other
13302 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65 20 6e  wise, zero the n
13303 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20 63 61  ew portion in ca
13304 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d 61 6c  se a .    ** mal
13305 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75  loc failure occu
13306 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c 61 74  rs while populat
13307 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20 66 6f  ing it in the fo
13308 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65 6c 6f  r(...) loop belo
13309 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 4e  w..    */.    aN
1330a 65 77 20 3d 20 28 50 61 67 65 72 53 61 76 65 70  ew = (PagerSavep
1330b 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33 52 65  oint *)sqlite3Re
1330c 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20 20 70  alloc(.        p
1330d 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
1330e 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65 72 53  t, sizeof(PagerS
1330f 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76 65 70  avepoint)*nSavep
13310 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20 20 20  oint.    );.    
13311 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 20 20  if( !aNew ){.   
13312 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13313 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
13314 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77 5b 6e    memset(&aNew[n
13315 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28 6e 53  Current], 0, (nS
13316 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72 65 6e  avepoint-nCurren
13317 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61 67 65  t) * sizeof(Page
13318 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a 20 20  rSavepoint));.  
13319 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70    pPager->aSavep
1331a 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20 20 20  oint = aNew;.   
1331b 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f   pPager->nSavepo
1331c 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69 6e 74  int = nSavepoint
1331d 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61  ;..    /* Popula
1331e 74 65 20 74 68 65 20 50 61 67 65 72 53 61 76 65  te the PagerSave
1331f 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 73  point structures
13320 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 64 2e   just allocated.
13321 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e   */.    for(ii=n
13322 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 61 76  Current; ii<nSav
13323 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  epoint; ii++){. 
13324 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
13325 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
13326 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 5b 69   );.      aNew[i
13327 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61 67 65  i].nOrig = pPage
13328 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 20  r->dbSize;.     
13329 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
1332a 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67  er->jfd) && pPag
1332b 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30  er->journalOff>0
1332c 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 77   ){.        aNew
1332d 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 70  [ii].iOffset = p
1332e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
1332f 66 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  f;.      }else{.
13330 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d          aNew[ii]
13331 2e 69 4f 66 66 73 65 74 20 3d 20 4a 4f 55 52 4e  .iOffset = JOURN
13332 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
13333 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
13334 20 61 4e 65 77 5b 69 69 5d 2e 69 53 75 62 52 65   aNew[ii].iSubRe
13335 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62  c = pPager->nSub
13336 52 65 63 3b 0a 20 20 20 20 20 20 61 4e 65 77 5b  Rec;.      aNew[
13337 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  ii].pInSavepoint
13338 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   = sqlite3Bitvec
13339 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 64  Create(pPager->d
1333a 62 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66  bSize);.      if
1333b 28 20 21 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53  ( !aNew[ii].pInS
1333c 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20  avepoint ){.    
1333d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1333e 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
1333f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f  .    }..    /* O
13340 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  pen the sub-jour
13341 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 6e 6f  nal, if it is no
13342 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64  t already opened
13343 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6f 70  . */.    rc = op
13344 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61  enSubJournal(pPa
13345 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  ger);.  }..  ret
13346 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
13347 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
13348 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c  s called to roll
13349 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20  back or release 
1334a 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70  (commit) a savep
1334b 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76  oint..** The sav
1334c 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73  epoint to releas
1334d 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65  e or rollback ne
1334e 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f  ed not be the mo
1334f 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20  st recently .** 
13350 63 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e  created savepoin
13351 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  t..**.** Paramet
13352 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20  er op is always 
13353 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54  either SAVEPOINT
13354 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56  _ROLLBACK or SAV
13355 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a  EPOINT_RELEASE..
13356 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56 45  ** If it is SAVE
13357 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74  POINT_RELEASE, t
13358 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20  hen release and 
13359 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65  destroy the save
1335a 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e  point with.** in
1335b 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20  dex iSavepoint. 
1335c 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49  If it is SAVEPOI
1335d 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65  NT_ROLLBACK, the
1335e 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63  n rollback all c
1335f 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68  hanges.** that h
13360 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e  ave occurred sin
13361 63 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ce the specified
13362 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63   savepoint was c
13363 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  reated..**.** Th
13364 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72  e savepoint to r
13365 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61  ollback or relea
13366 73 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  se is identified
13367 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a   by parameter .*
13368 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20  * iSavepoint. A 
13369 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73  value of 0 means
1336a 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74   to operate on t
1336b 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76  he outermost sav
1336c 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66  epoint.** (the f
1336d 69 72 73 74 20 63 72 65 61 74 65 64 29 2e 20 41  irst created). A
1336e 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72   value of (Pager
1336f 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d  .nSavepoint-1) m
13370 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20  eans operate.** 
13371 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  on the most rece
13372 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 61 76  ntly created sav
13373 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65  epoint. If iSave
13374 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72  point is greater
13375 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e   than.** (Pager.
13376 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74  nSavepoint-1), t
13377 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
13378 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  n is a no-op..**
13379 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76  .** If a negativ
1337a 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65  e value is passe
1337b 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  d to this functi
1337c 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  on, then the cur
1337d 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74  rent.** transact
1337e 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
1337f 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66  ck. This is diff
13380 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67  erent to calling
13381 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65   .** sqlite3Page
13382 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61  rRollback() beca
13383 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  use this functio
13384 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69  n does not termi
13385 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e  nate.** the tran
13386 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63  saction or unloc
13387 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  k the database, 
13388 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73  it just restores
13389 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74   the .** content
1338a 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
1338b 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61  e to its origina
1338c 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20  l state. .**.** 
1338d 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c  In any case, all
1338e 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68   savepoints with
1338f 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65   an index greate
13390 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e  r than iSavepoin
13391 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f  t .** are destro
13392 79 65 64 2e 20 49 66 20 74 68 69 73 20 69 73 20  yed. If this is 
13393 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74  a release operat
13394 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49  ion (op==SAVEPOI
13395 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20  NT_RELEASE),.** 
13396 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  then savepoint i
13397 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73  Savepoint is als
13398 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a  o destroyed..**.
13399 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1339a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49   may return SQLI
1339b 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65  TE_NOMEM if a me
1339c 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1339d 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20  fails,.** or an 
1339e 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  IO error code if
1339f 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
133a0 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e  urs while rollin
133a1 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76  g back a .** sav
133a2 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72  epoint. If no er
133a3 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49  rors occur, SQLI
133a4 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
133a5 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  d..*/ .SQLITE_PR
133a6 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
133a7 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28  3PagerSavepoint(
133a8 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
133a9 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65  nt op, int iSave
133aa 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63  point){.  int rc
133ab 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
133ac 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56   assert( op==SAV
133ad 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c  EPOINT_RELEASE |
133ae 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  | op==SAVEPOINT_
133af 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 73  ROLLBACK );.  as
133b0 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74  sert( iSavepoint
133b1 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50  >=0 || op==SAVEP
133b2 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b  OINT_ROLLBACK );
133b3 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70 6f 69  ..  if( iSavepoi
133b4 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65  nt<pPager->nSave
133b5 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74  point ){.    int
133b6 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
133b7 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
133b8 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  able */.    int 
133b9 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 2f  nNew;          /
133ba 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d 61  * Number of rema
133bb 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 73  ining savepoints
133bc 20 61 66 74 65 72 20 74 68 69 73 20 6f 70 2e 20   after this op. 
133bd 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  */..    /* Figur
133be 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 73  e out how many s
133bf 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 73  avepoints will s
133c0 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65 20 61  till be active a
133c1 66 74 65 72 20 74 68 69 73 0a 20 20 20 20 2a 2a  fter this.    **
133c2 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f 72   operation. Stor
133c3 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 6e 20  e this value in 
133c4 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65 65 20  nNew. Then free 
133c5 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69  resources associ
133c6 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 74  ated .    ** wit
133c7 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73  h any savepoints
133c8 20 74 68 61 74 20 61 72 65 20 64 65 73 74 72 6f   that are destro
133c9 79 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72  yed by this oper
133ca 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
133cb 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f    nNew = iSavepo
133cc 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 50  int + (op==SAVEP
133cd 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a  OINT_ROLLBACK);.
133ce 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 3b      for(ii=nNew;
133cf 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76   ii<pPager->nSav
133d0 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  epoint; ii++){. 
133d1 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
133d2 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72  ecDestroy(pPager
133d3 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d  ->aSavepoint[ii]
133d4 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a  .pInSavepoint);.
133d5 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72      }.    pPager
133d6 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e  ->nSavepoint = n
133d7 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  New;..    /* If 
133d8 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61  this is a rollba
133d9 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c  ck operation, pl
133da 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 69  ayback the speci
133db 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a  fied savepoint..
133dc 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69      ** If this i
133dd 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69  s a temp-file, i
133de 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68  t is possible th
133df 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  at the journal f
133e0 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e  ile has.    ** n
133e1 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e  ot yet been open
133e2 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
133e3 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 6e   there have been
133e4 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20   no changes to. 
133e5 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61     ** the databa
133e6 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20  se file, so the 
133e7 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69  playback operati
133e8 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65  on can be skippe
133e9 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
133ea 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  ( op==SAVEPOINT_
133eb 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f 70  ROLLBACK && isOp
133ec 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
133ed 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 53 61  ){.      PagerSa
133ee 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f  vepoint *pSavepo
133ef 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 3f  int = (nNew==0)?
133f0 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65  0:&pPager->aSave
133f1 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a 20  point[nNew-1];. 
133f2 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50       rc = pagerP
133f3 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74  laybackSavepoint
133f4 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 70 6f  (pPager, pSavepo
133f5 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65  int);.      asse
133f6 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  rt(rc!=SQLITE_DO
133f7 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  NE);.    }.  .  
133f8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
133f9 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65  a release of the
133fa 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70   outermost savep
133fb 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a  oint, truncate .
133fc 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a      ** the sub-j
133fd 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62  ournal to zero b
133fe 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f  ytes in size. */
133ff 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30  .    if( nNew==0
13400 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e   && op==SAVEPOIN
13401 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69 73 4f  T_RELEASE && isO
13402 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  pen(pPager->sjfd
13403 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ) ){.      asser
13404 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
13405 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   );.      rc = s
13406 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
13407 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30  (pPager->sjfd, 0
13408 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  );.      pPager-
13409 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 20  >nSubRec = 0;.  
1340a 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1340b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
1340c 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61  turn the full pa
1340d 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  thname of the da
1340e 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a  tabase file..*/.
1340f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
13410 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
13411 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28  e3PagerFilename(
13412 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
13413 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
13414 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f  >zFilename;.}../
13415 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
13416 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f  VFS structure fo
13417 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a  r the pager..*/.
13418 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
13419 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73  onst sqlite3_vfs
1341a 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66   *sqlite3PagerVf
1341b 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  s(Pager *pPager)
1341c 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
1341d 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  r->pVfs;.}../*.*
1341e 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c  * Return the fil
1341f 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65  e handle for the
13420 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
13421 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74  ssociated.** wit
13422 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68  h the pager.  Th
13423 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  is might return 
13424 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65  NULL if the file
13425 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20   has.** not yet 
13426 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a  been opened..*/.
13427 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
13428 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c  qlite3_file *sql
13429 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61  ite3PagerFile(Pa
1342a 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
1342b 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66  return pPager->f
1342c 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  d;.}../*.** Retu
1342d 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  rn the full path
1342e 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  name of the jour
1342f 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c  nal file..*/.SQL
13430 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
13431 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50  t char *sqlite3P
13432 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  agerJournalname(
13433 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
13434 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
13435 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a  >zJournal;.}../*
13436 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
13437 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c 6c 73  if fsync() calls
13438 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 66 6f   are disabled fo
13439 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 52  r this pager.  R
1343a 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69  eturn FALSE.** i
1343b 66 20 66 73 79 6e 63 28 29 73 20 61 72 65 20 65  f fsync()s are e
1343c 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79  xecuted normally
1343d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1343e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
1343f 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72  agerNosync(Pager
13440 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
13441 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79  urn pPager->noSy
13442 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  nc;.}..#ifdef SQ
13443 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f  LITE_HAS_CODEC./
13444 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 64  *.** Set the cod
13445 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  ec for this page
13446 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  r.*/.SQLITE_PRIV
13447 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
13448 50 61 67 65 72 53 65 74 43 6f 64 65 63 28 0a 20  PagerSetCodec(. 
13449 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 0a   Pager *pPager,.
1344a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63    void *(*xCodec
1344b 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67  )(void*,void*,Pg
1344c 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20  no,int),.  void 
1344d 2a 70 43 6f 64 65 63 41 72 67 0a 29 7b 0a 20 20  *pCodecArg.){.  
1344e 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d  pPager->xCodec =
1344f 20 78 43 6f 64 65 63 3b 0a 20 20 70 50 61 67 65   xCodec;.  pPage
13450 72 2d 3e 70 43 6f 64 65 63 41 72 67 20 3d 20 70  r->pCodecArg = p
13451 43 6f 64 65 63 41 72 67 3b 0a 7d 0a 23 65 6e 64  CodecArg;.}.#end
13452 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
13453 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
13454 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68  UM./*.** Move th
13455 65 20 70 61 67 65 20 70 50 67 20 74 6f 20 6c 6f  e page pPg to lo
13456 63 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74  cation pgno in t
13457 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  he file..**.** T
13458 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20  here must be no 
13459 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
1345a 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c  e page previousl
1345b 79 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20  y located at.** 
1345c 70 67 6e 6f 20 28 77 68 69 63 68 20 77 65 20 63  pgno (which we c
1345d 61 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68 6f 75  all pPgOld) thou
1345e 67 68 20 74 68 61 74 20 70 61 67 65 20 69 73 20  gh that page is 
1345f 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a  allowed to be.**
13460 20 69 6e 20 63 61 63 68 65 2e 20 20 49 66 20 74   in cache.  If t
13461 68 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 73  he page previous
13462 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 20 70 67  ly located at pg
13463 6e 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  no is not alread
13464 79 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c  y.** in the roll
13465 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74  back journal, it
13466 20 69 73 20 6e 6f 74 20 70 75 74 20 74 68 65 72   is not put ther
13467 65 20 62 79 20 62 79 20 74 68 69 73 20 72 6f 75  e by by this rou
13468 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65  tine..**.** Refe
13469 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61  rences to the pa
1346a 67 65 20 70 50 67 20 72 65 6d 61 69 6e 20 76 61  ge pPg remain va
1346b 6c 69 64 2e 20 55 70 64 61 74 69 6e 67 20 61 6e  lid. Updating an
1346c 79 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61 20 61  y.** meta-data a
1346d 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70  ssociated with p
1346e 50 67 20 28 69 2e 65 2e 20 64 61 74 61 20 73 74  Pg (i.e. data st
1346f 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 45 78 74  ored in the nExt
13470 72 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f  ra bytes.** allo
13471 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68  cated along with
13472 20 74 68 65 20 70 61 67 65 29 20 69 73 20 74 68   the page) is th
13473 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  e responsibility
13474 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   of the caller..
13475 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74  **.** A transact
13476 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 63 74 69  ion must be acti
13477 76 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  ve when this rou
13478 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20  tine is called. 
13479 49 74 20 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a  It used to be.**
1347a 20 72 65 71 75 69 72 65 64 20 74 68 61 74 20 61   required that a
1347b 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1347c 61 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61  action was not a
1347d 63 74 69 76 65 2c 20 62 75 74 20 74 68 69 73 20  ctive, but this 
1347e 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68  restriction.** h
1347f 61 73 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20  as been removed 
13480 28 43 52 45 41 54 45 20 49 4e 44 45 58 20 6e 65  (CREATE INDEX ne
13481 65 64 73 20 74 6f 20 6d 6f 76 65 20 61 20 70 61  eds to move a pa
13482 67 65 20 77 68 65 6e 20 61 20 73 74 61 74 65 6d  ge when a statem
13483 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  ent.** transacti
13484 6f 6e 20 69 73 20 61 63 74 69 76 65 29 2e 0a 2a  on is active)..*
13485 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 75 72  *.** If the four
13486 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 73 43  th argument, isC
13487 6f 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65  ommit, is non-ze
13488 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61  ro, then this pa
13489 67 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d  ge is being.** m
1348a 6f 76 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  oved as part of 
1348b 61 20 64 61 74 61 62 61 73 65 20 72 65 6f 72 67  a database reorg
1348c 61 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62  anization just b
1348d 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61  efore the transa
1348e 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69  ction .** is bei
1348f 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e  ng committed. In
13490 20 74 68 69 73 20 63 61 73 65 2c 20 69 74 20 69   this case, it i
13491 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61  s guaranteed tha
13492 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70  t the database p
13493 61 67 65 20 0a 2a 2a 20 70 50 67 20 72 65 66 65  age .** pPg refe
13494 72 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62  rs to will not b
13495 65 20 77 72 69 74 74 65 6e 20 74 6f 20 61 67 61  e written to aga
13496 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74  in within this t
13497 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ransaction..**.*
13498 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
13499 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54  may return SQLIT
1349a 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f  E_NOMEM or an IO
1349b 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61   error code if a
1349c 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72  n error.** occur
1349d 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74  s. Otherwise, it
1349e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
1349f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  OK..*/.SQLITE_PR
134a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
134a1 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50  3PagerMovepage(P
134a2 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 44 62  ager *pPager, Db
134a3 50 61 67 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20  Page *pPg, Pgno 
134a4 70 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d  pgno, int isComm
134a5 69 74 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50  it){.  PgHdr *pP
134a6 67 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20  gOld;           
134a7 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20      /* The page 
134a8 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65  being overwritte
134a9 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65  n. */.  Pgno nee
134aa 64 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b 20 20  dSyncPgno = 0;  
134ab 20 20 20 20 20 2f 2a 20 4f 6c 64 20 76 61 6c 75       /* Old valu
134ac 65 20 6f 66 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  e of pPg->pgno, 
134ad 69 66 20 73 79 6e 63 20 69 73 20 72 65 71 75 69  if sync is requi
134ae 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  red */.  int rc;
134af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
134b0 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
134b1 63 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f  code */.  Pgno o
134b2 72 69 67 50 67 6e 6f 3b 20 20 20 20 20 20 20 20  rigPgno;        
134b3 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72         /* The or
134b4 69 67 69 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62  iginal page numb
134b5 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  er */..  assert(
134b6 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a   pPg->nRef>0 );.
134b7 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67  .  /* If the pag
134b8 65 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69 73  e being moved is
134b9 20 64 69 72 74 79 20 61 6e 64 20 68 61 73 20 6e   dirty and has n
134ba 6f 74 20 62 65 65 6e 20 73 61 76 65 64 20 62 79  ot been saved by
134bb 20 74 68 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a   the latest.  **
134bc 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e   savepoint, then
134bd 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65 6e   save the curren
134be 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  t contents of th
134bf 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  e page into the 
134c0 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61  .  ** sub-journa
134c1 6c 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 72  l now. This is r
134c2 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c  equired to handl
134c3 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
134c4 73 63 65 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20  scenario:.  **. 
134c5 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a   **   BEGIN;.  *
134c6 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70  *     <journal p
134c7 61 67 65 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69  age X, then modi
134c8 66 79 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e  fy it in memory>
134c9 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f  .  **     SAVEPO
134ca 49 4e 54 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20  INT one;.  **   
134cb 20 20 20 20 3c 4d 6f 76 65 20 70 61 67 65 20 58      <Move page X
134cc 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a   to location Y>.
134cd 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43    **     ROLLBAC
134ce 4b 20 54 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20  K TO one;.  **. 
134cf 20 2a 2a 20 49 66 20 70 61 67 65 20 58 20 77 65   ** If page X we
134d0 72 65 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74  re not written t
134d1 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  o the sub-journa
134d2 6c 20 68 65 72 65 2c 20 69 74 20 77 6f 75 6c 64  l here, it would
134d3 20 6e 6f 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73   not.  ** be pos
134d4 73 69 62 6c 65 20 74 6f 20 72 65 73 74 6f 72 65  sible to restore
134d5 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 68   its contents wh
134d6 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b  en the "ROLLBACK
134d7 20 54 4f 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74   TO one".  ** st
134d8 61 74 65 6d 65 6e 74 20 77 65 72 65 20 69 73 20  atement were is 
134d9 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2a 0a  processed..  **.
134da 20 20 2a 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50    ** subjournalP
134db 61 67 65 28 29 20 6d 61 79 20 6e 65 65 64 20 74  age() may need t
134dc 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  o allocate space
134dd 20 74 6f 20 73 74 6f 72 65 20 70 50 67 2d 3e 70   to store pPg->p
134de 67 6e 6f 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f 6e  gno into.  ** on
134df 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 6f  e or more savepo
134e0 69 6e 74 20 62 69 74 76 65 63 73 2e 20 54 68 69  int bitvecs. Thi
134e1 73 20 69 73 20 74 68 65 20 72 65 61 73 6f 6e 20  s is the reason 
134e2 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20  this function.  
134e3 2a 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51  ** may return SQ
134e4 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f  LITE_NOMEM..  */
134e5 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67  .  if( pPg->flag
134e6 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 0a 20  s&PGHDR_DIRTY . 
134e7 20 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65    && subjRequire
134e8 73 50 61 67 65 28 70 50 67 29 0a 20 20 20 26 26  sPage(pPg).   &&
134e9 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
134ea 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65  = subjournalPage
134eb 28 70 50 67 29 29 0a 20 20 29 7b 0a 20 20 20 20  (pPg)).  ){.    
134ec 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
134ed 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4d    PAGERTRACE(("M
134ee 4f 56 45 20 25 64 20 70 61 67 65 20 25 64 20 28  OVE %d page %d (
134ef 6e 65 65 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76  needSync=%d) mov
134f0 65 73 20 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20  es to %d\n", .  
134f1 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67      PAGERID(pPag
134f2 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  er), pPg->pgno, 
134f3 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
134f4 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30  R_NEED_SYNC)?1:0
134f5 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52  , pgno));.  IOTR
134f6 41 43 45 28 28 22 4d 4f 56 45 20 25 70 20 25 64  ACE(("MOVE %p %d
134f7 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
134f8 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29  pPg->pgno, pgno)
134f9 29 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a  )..  /* If the j
134fa 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20  ournal needs to 
134fb 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f  be sync()ed befo
134fc 72 65 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e  re page pPg->pgn
134fd 6f 20 63 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72  o can.  ** be wr
134fe 69 74 74 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20  itten to, store 
134ff 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63  pPg->pgno in loc
13500 61 6c 20 76 61 72 69 61 62 6c 65 20 6e 65 65 64  al variable need
13501 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20  SyncPgno..  **. 
13502 20 2a 2a 20 49 66 20 74 68 65 20 69 73 43 6f 6d   ** If the isCom
13503 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c  mit flag is set,
13504 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
13505 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68  d to remember th
13506 61 74 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72  at.  ** the jour
13507 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20  nal needs to be 
13508 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20  sync()ed before 
13509 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 50  database page pP
1350a 67 2d 3e 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61  g->pgno .  ** ca
1350b 6e 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e  n be written to.
1350c 20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20   The caller has 
1350d 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64  already promised
1350e 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f   not to write to
1350f 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
13510 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
13511 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 26 26 20  R_NEED_SYNC) && 
13512 21 69 73 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  !isCommit ){.   
13513 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20   needSyncPgno = 
13514 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61  pPg->pgno;.    a
13515 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75  ssert( pageInJou
13516 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67  rnal(pPg) || pPg
13517 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64  ->pgno>pPager->d
13518 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20  bOrigSize );.   
13519 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c   assert( pPg->fl
1351a 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20  ags&PGHDR_DIRTY 
1351b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1351c 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
1351d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
1351e 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69  the cache contai
1351f 6e 73 20 61 20 70 61 67 65 20 77 69 74 68 20 70  ns a page with p
13520 61 67 65 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c  age-number pgno,
13521 20 72 65 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20   remove it.  ** 
13522 66 72 6f 6d 20 69 74 73 20 68 61 73 68 20 63 68  from its hash ch
13523 61 69 6e 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68  ain. Also, if th
13524 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63  e PgHdr.needSync
13525 20 77 61 73 20 73 65 74 20 66 6f 72 20 0a 20 20   was set for .  
13526 2a 2a 20 70 61 67 65 20 70 67 6e 6f 20 62 65 66  ** page pgno bef
13527 6f 72 65 20 74 68 65 20 27 6d 6f 76 65 27 20 6f  ore the 'move' o
13528 70 65 72 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65  peration, it nee
13529 64 73 20 74 6f 20 62 65 20 72 65 74 61 69 6e 65  ds to be retaine
1352a 64 20 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  d .  ** for the 
1352b 70 61 67 65 20 6d 6f 76 65 64 20 74 68 65 72 65  page moved there
1352c 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c  ..  */.  pPg->fl
1352d 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45  ags &= ~PGHDR_NE
1352e 45 44 5f 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c  ED_SYNC;.  pPgOl
1352f 64 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70  d = pager_lookup
13530 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a  (pPager, pgno);.
13531 20 20 61 73 73 65 72 74 28 20 21 70 50 67 4f 6c    assert( !pPgOl
13532 64 20 7c 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65  d || pPgOld->nRe
13533 66 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 50  f==1 );.  if( pP
13534 67 4f 6c 64 20 29 7b 0a 20 20 20 20 70 50 67 2d  gOld ){.    pPg-
13535 3e 66 6c 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c  >flags |= (pPgOl
13536 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  d->flags&PGHDR_N
13537 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 7d 0a 0a  EED_SYNC);.  }..
13538 20 20 6f 72 69 67 50 67 6e 6f 20 3d 20 70 50 67    origPgno = pPg
13539 2d 3e 70 67 6e 6f 3b 0a 20 20 73 71 6c 69 74 65  ->pgno;.  sqlite
1353a 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 2c  3PcacheMove(pPg,
1353b 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 50   pgno);.  if( pP
1353c 67 4f 6c 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  gOld ){.    sqli
1353d 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50  te3PcacheDrop(pP
1353e 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 73 71  gOld);.  }..  sq
1353f 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
13540 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50 61  irty(pPg);.  pPa
13541 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20  ger->dbModified 
13542 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65 64  = 1;..  if( need
13543 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20 20  SyncPgno ){.    
13544 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50 67  /* If needSyncPg
13545 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  no is non-zero, 
13546 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
13547 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 62   file needs to b
13548 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28 29  e .    ** sync()
13549 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 61  ed before any da
1354a 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ta is written to
1354b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 70   database file p
1354c 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f  age needSyncPgno
1354d 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e 74  ..    ** Current
1354e 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67 65  ly, no such page
1354f 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 70   exists in the p
13550 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74 68  age-cache and th
13551 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a 6f  e .    ** "is jo
13552 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63 20  urnaled" bitvec 
13553 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73 65  flag has been se
13554 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74 6f  t. This needs to
13555 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79 0a   be remedied by.
13556 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20 74      ** loading t
13557 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65  he page into the
13558 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64   pager-cache and
13559 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67 48   setting the PgH
1355a 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 20  dr.needSync .   
1355b 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a 2a   ** flag..    **
1355c 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 61  .    ** If the a
1355d 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 74  ttempt to load t
1355e 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65  he page into the
1355f 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69 6c   page-cache fail
13560 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20 74  s, (due.    ** t
13561 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20  o a malloc() or 
13562 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c 65  IO failure), cle
13563 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74 68  ar the bit in th
13564 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a 20  e pInJournal[]. 
13565 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74 68     ** array. Oth
13566 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 70  erwise, if the p
13567 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61 6e  age is loaded an
13568 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e 20  d written again 
13569 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 74  in.    ** this t
1356a 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 6d  ransaction, it m
1356b 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  ay be written to
1356c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1356d 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a  le before.    **
1356e 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69 6e   it is synced in
1356f 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
13570 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20 69  ile. This way, i
13571 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e 0a  t may end up in.
13572 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e      ** the journ
13573 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20 62  al file twice, b
13574 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61  ut that is not a
13575 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a 2a   problem..    **
13576 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c 69  .    ** The sqli
13577 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63 61  te3PagerGet() ca
13578 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68 65  ll may cause the
13579 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e 63   journal to sync
1357a 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a  . So make.    **
1357b 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72 2e   sure the Pager.
1357c 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73  needSync flag is
1357d 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a 2f   set too..    */
1357e 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 48  .    PgHdr *pPgH
1357f 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  dr;.    assert( 
13580 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
13581 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
13582 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61  ite3PagerGet(pPa
13583 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e  ger, needSyncPgn
13584 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20  o, &pPgHdr);.   
13585 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13586 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  OK ){.      if( 
13587 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
13588 61 6c 20 26 26 20 6e 65 65 64 53 79 6e 63 50 67  al && needSyncPg
13589 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72  no<=pPager->dbOr
1358a 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  igSize ){.      
1358b 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43    sqlite3BitvecC
1358c 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 49 6e  lear(pPager->pIn
1358d 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53 79 6e  Journal, needSyn
1358e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 7d 0a  cPgno);.      }.
1358f 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
13590 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
13591 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b  r->needSync = 1;
13592 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13593 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26  ger->noSync==0 &
13594 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20  & !MEMDB );.    
13595 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d  pPgHdr->flags |=
13596 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
13597 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ;.    sqlite3Pca
13598 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67  cheMakeDirty(pPg
13599 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Hdr);.    sqlite
1359a 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48  3PagerUnref(pPgH
1359b 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  dr);.  }..  /*. 
1359c 20 2a 2a 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65   ** For an in-me
1359d 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 6d  mory database, m
1359e 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72 69  ake sure the ori
1359f 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 69  ginal page conti
135a0 6e 75 65 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69  nues.  ** to exi
135a1 73 74 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20  st, in case the 
135a2 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64  transaction need
135a3 73 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20  s to roll back. 
135a4 20 57 65 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a   We allocate.  *
135a5 2a 20 74 68 65 20 70 61 67 65 20 6e 6f 77 2c 20  * the page now, 
135a6 69 6e 73 74 65 61 64 20 6f 66 20 61 74 20 72 6f  instead of at ro
135a7 6c 6c 62 61 63 6b 2c 20 62 65 63 61 75 73 65 20  llback, because 
135a8 77 65 20 63 61 6e 20 62 65 74 74 65 72 20 64 65  we can better de
135a9 61 6c 0a 20 20 2a 2a 20 77 69 74 68 20 61 6e 20  al.  ** with an 
135aa 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72  out-of-memory er
135ab 72 6f 72 20 6e 6f 77 2e 20 20 54 69 63 6b 65 74  ror now.  Ticket
135ac 20 23 33 37 36 31 2e 0a 20 20 2a 2f 0a 20 20 69   #3761..  */.  i
135ad 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20  f( MEMDB ){.    
135ae 44 62 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20  DbPage *pNew;.  
135af 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
135b0 67 65 72 41 63 71 75 69 72 65 28 70 50 61 67 65  gerAcquire(pPage
135b1 72 2c 20 6f 72 69 67 50 67 6e 6f 2c 20 26 70 4e  r, origPgno, &pN
135b2 65 77 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20  ew, 1);.    if( 
135b3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
135b4 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 73  return rc;.    s
135b5 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
135b6 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72  (pNew);.  }..  r
135b7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
135b8 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
135b9 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
135ba 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 66 6f  r to the data fo
135bb 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  r the specified 
135bc 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  page..*/.SQLITE_
135bd 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
135be 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
135bf 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  a(DbPage *pPg){.
135c0 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e    assert( pPg->n
135c1 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50  Ref>0 || pPg->pP
135c2 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20  ager->memDb );. 
135c3 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 44 61   return pPg->pDa
135c4 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ta;.}../*.** Ret
135c5 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
135c6 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72   the Pager.nExtr
135c7 61 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72  a bytes of "extr
135c8 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c  a" space .** all
135c9 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74  ocated along wit
135ca 68 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  h the specified 
135cb 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  page..*/.SQLITE_
135cc 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
135cd 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
135ce 72 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  ra(DbPage *pPg){
135cf 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
135d0 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
135d1 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 72    return (pPager
135d2 3f 70 50 67 2d 3e 70 45 78 74 72 61 3a 30 29 3b  ?pPg->pExtra:0);
135d3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65  .}../*.** Get/se
135d4 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f  t the locking-mo
135d5 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  de for this page
135d6 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f  r. Parameter eMo
135d7 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a  de must be one.*
135d8 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49  * of PAGER_LOCKI
135d9 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41  NGMODE_QUERY, PA
135da 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
135db 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41  NORMAL or .** PA
135dc 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
135dd 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68  EXCLUSIVE. If th
135de 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  e parameter is n
135df 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a  ot _QUERY, then.
135e0 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  ** the locking-m
135e1 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68  ode is set to th
135e2 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65  e value specifie
135e3 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  d..**.** The ret
135e4 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65  urned value is e
135e5 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b  ither PAGER_LOCK
135e6 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f  INGMODE_NORMAL o
135e7 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49  r.** PAGER_LOCKI
135e8 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
135e9 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65  , indicating the
135ea 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62   current (possib
135eb 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c  ly updated).** l
135ec 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a  ocking-mode..*/.
135ed 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
135ee 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c  nt sqlite3PagerL
135ef 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72  ockingMode(Pager
135f0 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d   *pPager, int eM
135f1 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ode){.  assert( 
135f2 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
135f3 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20  KINGMODE_QUERY. 
135f4 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
135f5 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
135f6 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20  NGMODE_NORMAL.  
135f7 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
135f8 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  de==PAGER_LOCKIN
135f9 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
135fa 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
135fb 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51  ER_LOCKINGMODE_Q
135fc 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65  UERY<0 );.  asse
135fd 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  rt( PAGER_LOCKIN
135fe 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20  GMODE_NORMAL>=0 
135ff 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  && PAGER_LOCKING
13600 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d  MODE_EXCLUSIVE>=
13601 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65  0 );.  if( eMode
13602 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e  >=0 && !pPager->
13603 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  tempFile ){.    
13604 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
13605 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64  eMode = (u8)eMod
13606 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
13607 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63  (int)pPager->exc
13608 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f  lusiveMode;.}../
13609 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65  *.** Get/set the
1360a 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f   journal-mode fo
1360b 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61  r this pager. Pa
1360c 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75  rameter eMode mu
1360d 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a  st be one of:.**
1360e 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55  .**    PAGER_JOU
1360f 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 2a  RNALMODE_QUERY.*
13610 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
13611 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a  ALMODE_DELETE.**
13612 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
13613 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a  LMODE_TRUNCATE.*
13614 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
13615 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a  ALMODE_PERSIST.*
13616 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
13617 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 20  ALMODE_OFF.**   
13618 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
13619 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a 20  DE_MEMORY.**.** 
1361a 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  If the parameter
1361b 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20   is not _QUERY, 
1361c 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
1361d 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20  -mode is set to 
1361e 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 65  the.** value spe
1361f 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  cified..**.** Th
13620 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63  e returned indic
13621 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
13622 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65  (possibly update
13623 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e  d) journal-mode.
13624 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13625 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
13626 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  gerJournalMode(P
13627 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
13628 74 20 65 4d 6f 64 65 29 7b 0a 20 20 69 66 28 20  t eMode){.  if( 
13629 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 61 73  !MEMDB ){.    as
1362a 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47  sert( eMode==PAG
1362b 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51  ER_JOURNALMODE_Q
1362c 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20  UERY.           
1362d 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
1362e 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44  ER_JOURNALMODE_D
1362f 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20 20  ELETE.          
13630 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41      || eMode==PA
13631 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
13632 54 52 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20  TRUNCATE.       
13633 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d         || eMode=
13634 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
13635 44 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20  DE_PERSIST.     
13636 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64           || eMod
13637 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
13638 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20 20 20  MODE_OFF .      
13639 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
1363a 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
1363b 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20  ODE_MEMORY );.  
1363c 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
1363d 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
1363e 59 3c 30 20 29 3b 0a 20 20 20 20 69 66 28 20 65  Y<0 );.    if( e
1363f 4d 6f 64 65 3e 3d 30 20 29 7b 0a 20 20 20 20 20  Mode>=0 ){.     
13640 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13641 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65  Mode = (u8)eMode
13642 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
13643 20 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65     assert( eMode
13644 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
13645 4f 44 45 5f 51 55 45 52 59 20 29 3b 0a 20 20 20  ODE_QUERY );.   
13646 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
13647 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75  (int)pPager->jou
13648 72 6e 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a  rnalMode;.}../*.
13649 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 73  ** Get/set the s
1364a 69 7a 65 2d 6c 69 6d 69 74 20 75 73 65 64 20 66  ize-limit used f
1364b 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f  or persistent jo
1364c 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a  urnal files..*/.
1364d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1364e 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  64 sqlite3PagerJ
1364f 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
13650 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
13651 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66  64 iLimit){.  if
13652 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a  ( iLimit>=-1 ){.
13653 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
13654 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69  nalSizeLimit = i
13655 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74  Limit;.  }.  ret
13656 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  urn pPager->jour
13657 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a  nalSizeLimit;.}.
13658 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
13659 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
1365a 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76  Pager->pBackup v
1365b 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63  ariable. The bac
1365c 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e  kup module.** in
1365d 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61   backup.c mainta
1365e 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ins the content 
1365f 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  of this variable
13660 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a  . This module.**
13661 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c   uses it opaquel
13662 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  y as an argument
13663 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75   to sqlite3Backu
13664 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a  pRestart() and.*
13665 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55  * sqlite3BackupU
13666 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f  pdate() only..*/
13667 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13668 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
13669 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63  *sqlite3PagerBac
1366a 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50  kupPtr(Pager *pP
1366b 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
1366c 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70  &pPager->pBackup
1366d 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
1366e 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
1366f 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  O */../*********
13670 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67  ***** End of pag
13671 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
13672 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13674 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
13675 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
13676 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
13677 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13678 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13679 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
1367a 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a   August 27.**.**
1367b 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1367c 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1367d 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1367e 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1367f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
13680 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
13681 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
13682 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
13683 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
13684 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
13685 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
13686 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
13687 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
13688 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
13689 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1368a 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1368b 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1368c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1368d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1368e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1368f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13690 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
13691 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76  $Id: btmutex.c,v
13692 20 31 2e 31 33 20 32 30 30 39 2f 30 33 2f 30 35   1.13 2009/03/05
13693 20 30 34 3a 32 30 3a 33 32 20 73 68 61 6e 65 20   04:20:32 shane 
13694 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73  Exp $.**.** This
13695 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
13696 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
13697 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e  ement mutexes on
13698 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a   Btree objects..
13699 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 65 61  ** This code rea
1369a 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62  lly belongs in b
1369b 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72  tree.c.  But btr
1369c 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20  ee.c is getting 
1369d 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77  too.** big and w
1369e 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20  e want to break 
1369f 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54  it down some.  T
136a0 68 69 73 20 70 61 63 6b 61 67 65 64 20 73 65 65  his packaged see
136a1 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f  med like.** a go
136a2 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a  od breakout..*/.
136a3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
136a4 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74  Include btreeInt
136a5 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
136a6 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a   of btmutex.c **
136a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
136a8 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
136a9 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65  Begin file btree
136aa 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
136ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
136ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
136ad 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c  /*.** 2004 April
136ae 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   6.**.** The aut
136af 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
136b0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
136b1 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
136b2 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
136b3 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
136b4 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
136b5 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
136b6 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
136b7 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
136b8 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
136b9 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
136ba 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
136bb 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
136bc 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
136bd 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
136be 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
136bf 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
136c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
136c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
136c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
136c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
136c4 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49  *.** $Id: btreeI
136c5 6e 74 2e 68 2c 76 20 31 2e 34 36 20 32 30 30 39  nt.h,v 1.46 2009
136c6 2f 30 33 2f 32 30 20 31 34 3a 31 38 3a 35 32 20  /03/20 14:18:52 
136c7 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
136c8 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  $.**.** This fil
136c9 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65  e implements a e
136ca 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61  xternal (disk-ba
136cb 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75 73  sed) database us
136cc 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 46  ing BTrees..** F
136cd 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 69  or a detailed di
136ce 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72 65  scussion of BTre
136cf 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a 0a  es, refer to.**.
136d0 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45 2e  **     Donald E.
136d1 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54 20   Knuth, THE ART 
136d2 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f 47  OF COMPUTER PROG
136d3 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 20  RAMMING, Volume 
136d4 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74 69  3:.**     "Sorti
136d5 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e 67  ng And Searching
136d6 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38 30  ", pages 473-480
136d7 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65 79  . Addison-Wesley
136d8 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68 69  .**     Publishi
136d9 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 64  ng Company, Read
136da 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65 74  ing, Massachuset
136db 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61  ts..**.** The ba
136dc 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61 74  sic idea is that
136dd 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68   each page of th
136de 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  e file contains 
136df 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65 6e  N database.** en
136e0 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70 6f  tries and N+1 po
136e1 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61 67  inters to subpag
136e2 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d  es..**.**   ----
136e3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
136e4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
136e5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
136e6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
136e7 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b 65    |  Ptr(0) | Ke
136e8 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c 20  y(0) | Ptr(1) | 
136e9 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b  Key(1) | ... | K
136ea 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e 29  ey(N-1) | Ptr(N)
136eb 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d   |.**   --------
136ec 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
136ed 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
136ee 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
136ef 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41  --------.**.** A
136f0 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f  ll of the keys o
136f1 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  n the page that 
136f2 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74 6f  Ptr(0) points to
136f3 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65 73   have values les
136f4 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30 29  s.** than Key(0)
136f5 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65  .  All of the ke
136f6 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28 31  ys on page Ptr(1
136f7 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67  ) and its subpag
136f8 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65  es have.** value
136f9 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4b  s greater than K
136fa 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20 74  ey(0) and less t
136fb 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c 6c  han Key(1).  All
136fc 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a 20   of the keys.** 
136fd 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69 74  on Ptr(N) and it
136fe 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65 20  s subpages have 
136ff 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 74  values greater t
13700 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20 41  han Key(N-1).  A
13701 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e 0a  nd.** so forth..
13702 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61 20  **.** Finding a 
13703 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20 72  particular key r
13704 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67 20  equires reading 
13705 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73 20  O(log(M)) pages 
13706 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69 73  from the .** dis
13707 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68 65  k where M is the
13708 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
13709 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a  es in the tree..
1370a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d  **.** In this im
1370b 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 20  plementation, a 
1370c 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e 20  single file can 
1370d 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  hold one or more
1370e 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42 54   separate .** BT
1370f 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72 65  rees.  Each BTre
13710 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20  e is identified 
13711 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  by the index of 
13712 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 20  its root page.  
13713 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64  The.** key and d
13714 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74 72  ata for any entr
13715 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74  y are combined t
13716 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79 6c  o form the "payl
13717 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78 65  oad".  A.** fixe
13718 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c  d amount of payl
13719 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72 69  oad can be carri
1371a 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20 74  ed directly on t
1371b 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70  he database.** p
1371c 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 79  age.  If the pay
1371d 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20 74  load is larger t
1371e 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20 61  han the preset a
1371f 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70 6c  mount then surpl
13720 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65 20  us.** bytes are 
13721 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66 6c  stored on overfl
13722 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 70  ow pages.  The p
13723 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65 6e  ayload for an en
13724 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70  try.** and the p
13725 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65 72  receding pointer
13726 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f   are combined to
13727 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e 20   form a "Cell". 
13728 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20 68   Each .** page h
13729 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64 65  as a small heade
1372a 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73  r which contains
1372b 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69 6e   the Ptr(N) poin
1372c 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a 2a  ter and other.**
1372d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 63   information suc
1372e 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f 66  h as the size of
1372f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a   key and data..*
13730 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54 41  *.** FORMAT DETA
13731 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ILS.**.** The fi
13732 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69 6e  le is divided in
13733 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20 66  to pages.  The f
13734 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61 6c  irst page is cal
13735 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20 74  led page 1,.** t
13736 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61 67  he second is pag
13737 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74  e 2, and so fort
13738 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62 65  h.  A page numbe
13739 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63 61  r of zero indica
1373a 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 20  tes.** "no such 
1373b 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67 65  page".  The page
1373c 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e 79   size can be any
1373d 74 68 69 6e 67 20 62 65 74 77 65 65 6e 20 35 31  thing between 51
1373e 32 20 61 6e 64 20 36 35 35 33 36 2e 0a 2a 2a 20  2 and 65536..** 
1373f 45 61 63 68 20 70 61 67 65 20 63 61 6e 20 62 65  Each page can be
13740 20 65 69 74 68 65 72 20 61 20 62 74 72 65 65 20   either a btree 
13741 70 61 67 65 2c 20 61 20 66 72 65 65 6c 69 73 74  page, a freelist
13742 20 70 61 67 65 20 6f 72 20 61 6e 20 6f 76 65 72   page or an over
13743 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a  flow.** page..**
13744 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
13745 67 65 20 69 73 20 61 6c 77 61 79 73 20 61 20 62  ge is always a b
13746 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20  tree page.  The 
13747 66 69 72 73 74 20 31 30 30 20 62 79 74 65 73 20  first 100 bytes 
13748 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  of the first.** 
13749 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 61 20 73  page contain a s
1374a 70 65 63 69 61 6c 20 68 65 61 64 65 72 20 28 74  pecial header (t
1374b 68 65 20 22 66 69 6c 65 20 68 65 61 64 65 72 22  he "file header"
1374c 29 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  ) that describes
1374d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68   the file..** Th
1374e 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20  e format of the 
1374f 66 69 6c 65 20 68 65 61 64 65 72 20 69 73 20 61  file header is a
13750 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
13751 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45     OFFSET   SIZE
13752 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a      DESCRIPTION.
13753 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 31  **      0      1
13754 36 20 20 20 20 20 48 65 61 64 65 72 20 73 74 72  6     Header str
13755 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66 6f 72  ing: "SQLite for
13756 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a 20 20 20  mat 3\000".**   
13757 20 20 31 36 20 20 20 20 20 20 20 32 20 20 20 20    16       2    
13758 20 50 61 67 65 20 73 69 7a 65 20 69 6e 20 62 79   Page size in by
13759 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 20 31 38  tes.  .**     18
1375a 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 6c         1     Fil
1375b 65 20 66 6f 72 6d 61 74 20 77 72 69 74 65 20 76  e format write v
1375c 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 31 39  ersion.**     19
1375d 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 6c         1     Fil
1375e 65 20 66 6f 72 6d 61 74 20 72 65 61 64 20 76 65  e format read ve
1375f 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 30 20  rsion.**     20 
13760 20 20 20 20 20 20 31 20 20 20 20 20 42 79 74 65        1     Byte
13761 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63  s of unused spac
13762 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  e at the end of 
13763 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 20 20 20  each page.**    
13764 20 32 31 20 20 20 20 20 20 20 31 20 20 20 20 20   21       1     
13765 4d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79  Max embedded pay
13766 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
13767 20 20 20 20 20 32 32 20 20 20 20 20 20 20 31 20       22       1 
13768 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 64 65 64      Min embedded
13769 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
1376a 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 20 20 20  n.**     23     
1376b 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65 61 66    1     Min leaf
1376c 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
1376d 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 20 20 20  n.**     24     
1376e 20 20 34 20 20 20 20 20 46 69 6c 65 20 63 68 61    4     File cha
1376f 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 20  nge counter.**  
13770 20 20 20 32 38 20 20 20 20 20 20 20 34 20 20 20     28       4   
13771 20 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66    Reserved for f
13772 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 20 20 20  uture use.**    
13773 20 33 32 20 20 20 20 20 20 20 34 20 20 20 20 20   32       4     
13774 46 69 72 73 74 20 66 72 65 65 6c 69 73 74 20 70  First freelist p
13775 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 20 20 20  age.**     36   
13776 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72      4     Number
13777 20 6f 66 20 66 72 65 65 6c 69 73 74 20 70 61 67   of freelist pag
13778 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a  es in the file.*
13779 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 36 30  *     40      60
1377a 20 20 20 20 20 31 35 20 34 2d 62 79 74 65 20 6d       15 4-byte m
1377b 65 74 61 20 76 61 6c 75 65 73 20 70 61 73 73 65  eta values passe
1377c 64 20 74 6f 20 68 69 67 68 65 72 20 6c 61 79 65  d to higher laye
1377d 72 73 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  rs.**.** All of 
1377e 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  the integer valu
1377f 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69 61  es are big-endia
13780 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69 63  n (most signific
13781 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29 2e  ant byte first).
13782 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  .**.** The file 
13783 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69  change counter i
13784 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68  s incremented wh
13785 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
13786 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68  is changed.** Th
13787 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77  is counter allow
13788 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  s other processe
13789 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74  s to know when t
1378a 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e  he file has chan
1378b 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20  ged.** and thus 
1378c 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20 74  when they need t
1378d 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63 61  o flush their ca
1378e 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  che..**.** The m
1378f 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c  ax embedded payl
13790 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20  oad fraction is 
13791 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68  the amount of th
13792 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a  e total usable.*
13793 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61 67  * space in a pag
13794 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f  e that can be co
13795 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e 67  nsumed by a sing
13796 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e  le cell for stan
13797 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28  dard.** B-tree (
13798 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74 61  non-LEAFDATA) ta
13799 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20 6f  bles.  A value o
1379a 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30 25  f 255 means 100%
1379b 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a  .  The default.*
1379c 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74 68  * is to limit th
1379d 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73  e maximum cell s
1379e 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20 6c  ize so that at l
1379f 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69 6c  east 4 cells wil
137a0 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20  l fit.** on one 
137a1 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65 20  page.  Thus the 
137a2 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62 65  default max embe
137a3 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61  dded payload fra
137a4 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a  ction is 64..**.
137a5 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61  ** If the payloa
137a6 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 20  d for a cell is 
137a7 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
137a8 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 65  max payload, the
137a9 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f  n extra.** paylo
137aa 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 6f  ad is spilled to
137ab 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
137ac 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c    Once an overfl
137ad 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63  ow page is alloc
137ae 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79  ated,.** as many
137af 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 62   bytes as possib
137b0 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e 74  le are moved int
137b1 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  o the overflow p
137b2 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 74  ages without let
137b3 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c  ting.** the cell
137b4 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77   size drop below
137b5 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65   the min embedde
137b6 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
137b7 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69  on..**.** The mi
137b8 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66  n leaf payload f
137b9 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20  raction is like 
137ba 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64  the min embedded
137bb 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
137bc 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74  n.** except that
137bd 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20 6c   it applies to l
137be 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c  eaf nodes in a L
137bf 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20 54  EAFDATA tree.  T
137c0 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61  he maximum.** pa
137c1 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 66  yload fraction f
137c2 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74 72  or a LEAFDATA tr
137c3 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30 30  ee is always 100
137c4 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20 69  % (or 255) and i
137c5 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66 69  t.** not specifi
137c6 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  ed in the header
137c7 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72  ..**.** Each btr
137c8 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76 69  ee pages is divi
137c9 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20 73  ded into three s
137ca 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68 65  ections:  The he
137cb 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c  ader, the.** cel
137cc 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 2c  l pointer array,
137cd 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f   and the cell co
137ce 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61 67  ntent area.  Pag
137cf 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20 31  e 1 also has a 1
137d0 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20  00-byte.** file 
137d1 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 75  header that occu
137d2 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70 61  rs before the pa
137d3 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a  ge header..**.**
137d4 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d        |---------
137d5 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20  -------|.**     
137d6 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20 20   | file header  
137d7 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73 2e    |   100 bytes.
137d8 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a    Page 1 only..*
137d9 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
137da 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
137db 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 20    | page header 
137dc 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 66     |   8 bytes f
137dd 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 62  or leaves.  12 b
137de 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 6f  ytes for interio
137df 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20  r nodes.**      
137e0 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  |---------------
137e1 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c  -|.**      | cel
137e2 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20 20  l pointer   |   
137e3 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20 63  |  2 bytes per c
137e4 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72 64  ell.  Sorted ord
137e5 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72  er..**      | ar
137e6 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20 20  ray          |  
137e7 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77 61   |  Grows downwa
137e8 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20 20  rd.**      |    
137e9 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20              |   
137ea 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d  v.**      |-----
137eb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20  -----------|.** 
137ec 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74       | unallocat
137ed 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20  ed    |.**      
137ee 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20 20  | space         
137ef 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d   |.**      |----
137f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20  ------------|   
137f1 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64 73  ^  Grows upwards
137f2 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20  .**      | cell 
137f3 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c 20  content   |   | 
137f4 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65 72   Arbitrary order
137f5 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77 69   interspersed wi
137f6 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a  th freeblocks..*
137f7 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20 20  *      | area   
137f8 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 61          |   |  a
137f9 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66 72  nd free space fr
137fa 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20  agments..**     
137fb 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
137fc 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  --|.**.** The pa
137fd 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73  ge headers looks
137fe 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
137ff 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a  *   OFFSET   SIZ
13800 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49 4f  E     DESCRIPTIO
13801 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20  N.**      0     
13802 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 20    1      Flags. 
13803 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65  1: intkey, 2: ze
13804 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 64  rodata, 4: leafd
13805 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20  ata, 8: leaf.** 
13806 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20 20       1       2  
13807 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 20      byte offset 
13808 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65  to the first fre
13809 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33  eblock.**      3
1380a 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e 75         2      nu
1380b 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e  mber of cells on
1380c 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20   this page.**   
1380d 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20 20     5       2    
1380e 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20    first byte of 
1380f 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
13810 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37 20   area.**      7 
13811 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75 6d        1      num
13812 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65  ber of fragmente
13813 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a 20  d free bytes.** 
13814 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20 20       8       4  
13815 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64 20      Right child 
13816 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c 75  (the Ptr(N) valu
13817 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20  e).  Omitted on 
13818 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  leaves..**.** Th
13819 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20 74  e flags define t
1381a 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 69  he format of thi
1381b 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 54  s btree page.  T
1381c 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65 61  he leaf flag mea
1381d 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 20  ns that.** this 
1381e 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69 6c  page has no chil
1381f 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64  dren.  The zerod
13820 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20 74  ata flag means t
13821 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63 61  hat this page ca
13822 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65  rries.** only ke
13823 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20  ys and no data. 
13824 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61 67   The intkey flag
13825 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
13826 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65 72  key is a integer
13827 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74 6f  .** which is sto
13828 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 73  red in the key s
13829 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68 65  ize entry of the
1382a 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61 74   cell header rat
1382b 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 74  her than in.** t
1382c 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 2e  he payload area.
1382d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20  .**.** The cell 
1382e 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62 65  pointer array be
1382f 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72 73  gins on the firs
13830 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65  t byte after the
13831 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a   page header..**
13832 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65   The cell pointe
13833 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73  r array contains
13834 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d   zero or more 2-
13835 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68 69  byte numbers whi
13836 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65 74  ch are.** offset
13837 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e  s from the begin
13838 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65  ning of the page
13839 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   to the cell con
1383a 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c 6c  tent in the cell
1383b 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  .** content area
1383c 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e  .  The cell poin
1383d 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 6f  ters occur in so
1383e 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 65  rted order.  The
1383f 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 0a   system strives.
13840 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 20  ** to keep free 
13841 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65 20  space after the 
13842 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  last cell pointe
13843 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 65  r so that new ce
13844 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65 61  lls can.** be ea
13845 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68 6f  sily added witho
13846 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65 66  ut having to def
13847 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65  ragment the page
13848 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e  ..**.** Cell con
13849 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 61  tent is stored a
1384a 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 6f  t the very end o
1384b 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 67  f the page and g
1384c 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65 0a  rows toward the.
1384d 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  ** beginning of 
1384e 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  the page..**.** 
1384f 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69 74  Unused space wit
13850 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e  hin the cell con
13851 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f 6c  tent area is col
13852 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c 69  lected into a li
13853 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20  nked list of.** 
13854 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61 63  freeblocks.  Eac
13855 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 61  h freeblock is a
13856 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 20  t least 4 bytes 
13857 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62 79  in size.  The by
13858 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f 20  te offset.** to 
13859 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c  the first freebl
1385a 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e 20  ock is given in 
1385b 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72 65  the header.  Fre
1385c 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69 6e  eblocks occur in
1385d 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20 6f  .** increasing o
1385e 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20 61  rder.  Because a
1385f 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74 20   freeblock must 
13860 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79  be at least 4 by
13861 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20  tes in size,.** 
13862 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20 6f  any group of 3 o
13863 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20 62  r fewer unused b
13864 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c  ytes in the cell
13865 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63 61   content area ca
13866 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f 6e  nnot.** exist on
13867 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 63   the freeblock c
13868 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20 6f  hain.  A group o
13869 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72 65  f 3 or fewer fre
1386a 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c 65  e bytes is calle
1386b 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74 2e  d.** a fragment.
1386c 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62    The total numb
1386d 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61  er of bytes in a
1386e 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73 20  ll fragments is 
1386f 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e 20  recorded..** in 
13870 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20  the page header 
13871 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a  at offset 7..**.
13872 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45  **    SIZE    DE
13873 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20  SCRIPTION.**    
13874 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 66    2     Byte off
13875 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20  set of the next 
13876 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20  freeblock.**    
13877 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69 6e    2     Bytes in
13878 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b 0a   this freeblock.
13879 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65 20  **.** Cells are 
1387a 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67  of variable leng
1387b 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20 73  th.  Cells are s
1387c 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65 6c  tored in the cel
1387d 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 61  l content area a
1387e 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20  t.** the end of 
1387f 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e 74  the page.  Point
13880 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c 73  ers to the cells
13881 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c 6c   are in the cell
13882 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 2a   pointer array.*
13883 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 65  * that immediate
13884 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 70  ly follows the p
13885 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65 6c  age header.  Cel
13886 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  ls is not necess
13887 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 75  arily.** contigu
13888 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72 2c  ous or in order,
13889 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65   but cell pointe
1388a 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75  rs are contiguou
1388b 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e 0a  s and in order..
1388c 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65  **.** Cell conte
1388d 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 20  nt makes use of 
1388e 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20  variable length 
1388f 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61 72  integers.  A var
13890 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 20  iable.** length 
13891 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f 20  integer is 1 to 
13892 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74 68  9 bytes where th
13893 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f  e lower 7 bits o
13894 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65 20  f each .** byte 
13895 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20 69  are used.  The i
13896 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20  nteger consists 
13897 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61  of all bytes tha
13898 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65 74  t have bit 8 set
13899 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72 73   and.** the firs
1389a 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74 20  t byte with bit 
1389b 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d 6f  8 clear.  The mo
1389c 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62  st significant b
1389d 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67  yte of the integ
1389e 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66 69  er.** appears fi
1389f 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c 65  rst.  A variable
138a0 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
138a1 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65 20  may not be more 
138a2 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f 6e  than 9 bytes lon
138a3 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63 69  g..** As a speci
138a4 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20 62  al case, all 8 b
138a5 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68 20  ytes of the 9th 
138a6 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61 73  byte are used as
138a7 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a 20   data.  This.** 
138a8 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 20  allows a 64-bit 
138a9 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65 6e  integer to be en
138aa 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65 73  coded in 9 bytes
138ab 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30 20  ..**.**    0x00 
138ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
138ad 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78       becomes  0x
138ae 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 30  00000000.**    0
138af 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 20  x7f             
138b0 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73           becomes
138b1 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20    0x0000007f.** 
138b2 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20 20     0x81 0x00    
138b3 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63               bec
138b4 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38 30  omes  0x00000080
138b5 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30 30  .**    0x82 0x00
138b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
138b7 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30   becomes  0x0000
138b8 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30 20  0100.**    0x80 
138b9 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20  0x7f            
138ba 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78       becomes  0x
138bb 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 30  0000007f.**    0
138bc 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30 78  x8a 0x91 0xd1 0x
138bd 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65 73  ac 0x78  becomes
138be 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a 20    0x12345678.** 
138bf 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78 38     0x81 0x81 0x8
138c0 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65 63  1 0x81 0x01  bec
138c1 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38 31  omes  0x10204081
138c2 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20  .**.** Variable 
138c3 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20  length integers 
138c4 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f 77  are used for row
138c5 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64 20  ids and to hold 
138c6 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
138c7 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61 6e   bytes of key an
138c8 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72 65  d data in a btre
138c9 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68  e cell..**.** Th
138ca 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63  e content of a c
138cb 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74  ell looks like t
138cc 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49  his:.**.**    SI
138cd 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f  ZE    DESCRIPTIO
138ce 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20  N.**      4     
138cf 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  Page number of t
138d0 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f  he left child. O
138d1 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20 66  mitted if leaf f
138d2 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20  lag is set..**  
138d3 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72     var    Number
138d4 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74   of bytes of dat
138d5 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 68  a. Omitted if th
138d6 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20  e zerodata flag 
138d7 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76  is set..**     v
138d8 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  ar    Number of 
138d9 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72  bytes of key. Or
138da 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 20   the key itself 
138db 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69  if intkey flag i
138dc 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20 2a  s set..**      *
138dd 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a 20       Payload.** 
138de 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 74       4     First
138df 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76 65   page of the ove
138e0 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f 6d  rflow chain.  Om
138e1 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65 72  itted if no over
138e2 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66  flow.**.** Overf
138e3 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20 61  low pages form a
138e4 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 45   linked list.  E
138e5 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74 20  ach page except 
138e6 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d 70  the last is comp
138e7 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64  letely.** filled
138e8 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67 65   with data (page
138e9 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29 2e  size - 4 bytes).
138ea 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65 20    The last page 
138eb 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74 74  can have as litt
138ec 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65 20  le.** as 1 byte 
138ed 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20  of data..**.**  
138ee 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49    SIZE    DESCRI
138ef 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20  PTION.**      4 
138f0 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20      Page number 
138f1 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77  of next overflow
138f2 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20   page.**      * 
138f3 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46      Data.**.** F
138f4 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63 6f  reelist pages co
138f5 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79 70  me in two subtyp
138f6 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73 20  es: trunk pages 
138f7 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e 20  and leaf pages. 
138f8 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61   The.** file hea
138f9 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  der points to th
138fa 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e  e first in a lin
138fb 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75 6e  ked list of trun
138fc 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74 72  k page.  Each tr
138fd 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e  unk.** page poin
138fe 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c  ts to multiple l
138ff 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 20  eaf pages.  The 
13900 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 61  content of a lea
13901 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e 73  f page is.** uns
13902 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 75  pecified.  A tru
13903 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69  nk page looks li
13904 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
13905 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49    SIZE    DESCRI
13906 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20  PTION.**      4 
13907 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20      Page number 
13908 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70 61  of next trunk pa
13909 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ge.**      4    
1390a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20   Number of leaf 
1390b 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69 73  pointers on this
1390c 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20   page.**      * 
1390d 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65      zero or more
1390e 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 6f   pages numbers o
1390f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f 2a  f leaves.*/.../*
13910 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
13911 61 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 69  alue is the maxi
13912 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 73  mum cell size as
13913 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d  suming a maximum
13914 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69   page.** size gi
13915 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65  ve above..*/.#de
13916 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a  fine MX_CELL_SIZ
13917 45 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 61  E(pBt)  (pBt->pa
13918 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68  geSize-8)../* Th
13919 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1391a 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73   of cells on a s
1391b 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68  ingle page of th
1391c 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
1391d 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d  s.** assumes a m
1391e 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  inimum cell size
1391f 20 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 20   of 6 bytes  (4 
13920 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 65  bytes for the ce
13921 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75  ll itself.** plu
13922 73 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 68  s 2 bytes for th
13923 65 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 63  e index to the c
13924 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 20  ell in the page 
13925 68 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a 2a  header).  Such.*
13926 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69  * small cells wi
13927 6c 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 20  ll be rare, but 
13928 74 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c  they are possibl
13929 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58  e..*/.#define MX
1392a 5f 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74  _CELL(pBt) ((pBt
1392b 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 29  ->pageSize-8)/6)
1392c 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
1392d 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70  larations */.typ
1392e 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50  edef struct MemP
1392f 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70  age MemPage;.typ
13930 65 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f  edef struct BtLo
13931 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a  ck BtLock;../*.*
13932 2a 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69  * This is a magi
13933 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70  c string that ap
13934 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67  pears at the beg
13935 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a  inning of every.
13936 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  ** SQLite databa
13937 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69  se in order to i
13938 64 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c 65  dentify the file
13939 20 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 62   as a real datab
1393a 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63  ase..**.** You c
1393b 61 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 76  an change this v
1393c 61 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d  alue at compile-
1393d 74 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 69  time by specifyi
1393e 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45  ng a.** -DSQLITE
1393f 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e  _FILE_HEADER="..
13940 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c  ." on the compil
13941 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e  er command-line.
13942 20 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 20    The.** header 
13943 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20  must be exactly 
13944 31 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 69  16 bytes includi
13945 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d  ng the zero-term
13946 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65  inator so.** the
13947 20 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 73   string itself s
13948 68 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 72  hould be 15 char
13949 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66  acters long.  If
1394a 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74   you change.** t
1394b 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e 20  he header, then 
1394c 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72  your custom libr
1394d 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  ary will not be 
1394e 61 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a  able to read .**
1394f 20 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 72   databases gener
13950 61 74 65 64 20 62 79 20 74 68 65 20 73 74 61 6e  ated by the stan
13951 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74  dard tools and t
13952 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c  he standard tool
13953 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65  s.** will not be
13954 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 61   able to read da
13955 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20  tabases created 
13956 62 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c  by your custom l
13957 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64  ibrary..*/.#ifnd
13958 65 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48  ef SQLITE_FILE_H
13959 45 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 37  EADER /* 1234567
1395a 38 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 20  89 123456 */.#  
1395b 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 49  define SQLITE_FI
1395c 4c 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 74  LE_HEADER "SQLit
1395d 65 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64  e format 3".#end
1395e 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74  if../*.** Page t
1395f 79 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f  ype flags.  An O
13960 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  Red combination 
13961 6f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61  of these flags a
13962 70 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a 20  ppear as the.** 
13963 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f 6e  first byte of on
13964 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 65  -disk image of e
13965 76 65 72 79 20 42 54 72 65 65 20 70 61 67 65 2e  very BTree page.
13966 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  .*/.#define PTF_
13967 49 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a 23  INTKEY    0x01.#
13968 64 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44  define PTF_ZEROD
13969 41 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 6e  ATA  0x02.#defin
1396a 65 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 20  e PTF_LEAFDATA  
1396b 30 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 46  0x04.#define PTF
1396c 5f 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 0a  _LEAF      0x08.
1396d 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 70  ./*.** As each p
1396e 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  age of the file 
1396f 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d  is loaded into m
13970 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e  emory, an instan
13971 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
13972 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ing.** structure
13973 20 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e 64   is appended and
13974 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
13975 7a 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 75  zero.  This stru
13976 63 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20  cture stores.** 
13977 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
13978 74 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  t the page that 
13979 69 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d 20  is decoded from 
1397a 74 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 67  the raw file pag
1397b 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61  e..**.** The pPa
1397c 72 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e 74  rent field point
1397d 73 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 61  s back to the pa
1397e 72 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 73  rent page.  This
1397f 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a   allows us to.**
13980 20 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72   walk up the BTr
13981 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66  ee from any leaf
13982 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43   to the root.  C
13983 61 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65  are must be take
13984 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20  n to.** unref() 
13985 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
13986 70 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69  pointer when thi
13987 73 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e  s page is no lon
13988 67 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0a  ger referenced..
13989 2a 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 72  ** The pageDestr
1398a 75 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20  uctor() routine 
1398b 68 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f  handles that cho
1398c 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73  re..**.** Access
1398d 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f   to all fields o
1398e 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
1398f 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62   is controlled b
13990 79 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73  y the mutex.** s
13991 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65  tored in MemPage
13992 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a  .pBt->mutex..*/.
13993 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 7b  struct MemPage {
13994 0a 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 20  .  u8 isInit;   
13995 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
13996 69 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e  if previously in
13997 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20  itialized. MUST 
13998 42 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 75  BE FIRST! */.  u
13999 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20  8 nOverflow;    
1399a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1399b 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62   overflow cell b
1399c 6f 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d  odies in aCell[]
1399d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b   */.  u8 intKey;
1399e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1399f 75 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61  ue if intkey fla
139a0 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38  g is set */.  u8
139a1 20 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20   leaf;          
139a2 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 65     /* True if le
139a3 61 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a  af flag is set *
139a4 2f 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b 20  /.  u8 hasData; 
139a5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
139a6 20 69 66 20 74 68 69 73 20 70 61 67 65 20 73 74   if this page st
139a7 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20 75  ores data */.  u
139a8 38 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20  8 hdrOffset;    
139a9 20 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20 70      /* 100 for p
139aa 61 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72 77  age 1.  0 otherw
139ab 69 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c  ise */.  u8 chil
139ac 64 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a  dPtrSize;     /*
139ad 20 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20   0 if leaf==1.  
139ae 34 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a  4 if leaf==0 */.
139af 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20    u16 maxLocal; 
139b0 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
139b1 66 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f  f BtShared.maxLo
139b2 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e  cal or BtShared.
139b3 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36  maxLeaf */.  u16
139b4 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20   minLocal;      
139b5 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53    /* Copy of BtS
139b6 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f  hared.minLocal o
139b7 72 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65  r BtShared.minLe
139b8 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c  af */.  u16 cell
139b9 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  Offset;      /* 
139ba 49 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 6f  Index in aData o
139bb 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69  f first cell poi
139bc 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46  nter */.  u16 nF
139bd 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ree;           /
139be 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  * Number of free
139bf 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61   bytes on the pa
139c0 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c  ge */.  u16 nCel
139c1 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l;           /* 
139c2 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  Number of cells 
139c3 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c 6f  on this page, lo
139c4 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a  cal and ovfl */.
139c5 20 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b 20    u16 maskPage; 
139c6 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66         /* Mask f
139c7 6f 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 2a  or page offset *
139c8 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c  /.  struct _Ovfl
139c9 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c  Cell {   /* Cell
139ca 73 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20  s that will not 
139cb 66 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a  fit on aData[] *
139cc 2f 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b  /.    u8 *pCell;
139cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
139ce 6e 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f 64  nters to the bod
139cf 79 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f  y of the overflo
139d0 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31  w cell */.    u1
139d1 36 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  6 idx;          
139d2 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 73    /* Insert this
139d3 20 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 78   cell before idx
139d4 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77  -th non-overflow
139d5 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76   cell */.  } aOv
139d6 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 65  fl[5];.  BtShare
139d7 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f 2a  d *pBt;       /*
139d8 20 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 68   Pointer to BtSh
139d9 61 72 65 64 20 74 68 61 74 20 74 68 69 73 20 70  ared that this p
139da 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a  age is part of *
139db 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20  /.  u8 *aData;  
139dc 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
139dd 74 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 67  ter to disk imag
139de 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 61  e of the page da
139df 74 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a  ta */.  DbPage *
139e0 70 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a 20  pDbPage;     /* 
139e1 50 61 67 65 72 20 70 61 67 65 20 68 61 6e 64 6c  Pager page handl
139e2 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
139e3 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ;           /* P
139e4 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
139e5 68 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a  his page */.};..
139e6 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d  /*.** The in-mem
139e7 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 64  ory image of a d
139e8 69 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 65  isk page has the
139e9 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72   auxiliary infor
139ea 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a  mation appended.
139eb 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 20  ** to the end.  
139ec 45 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 68  EXTRA_SIZE is th
139ed 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
139ee 73 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65  s of space neede
139ef 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61  d to hold.** tha
139f0 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74  t extra informat
139f1 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ion..*/.#define 
139f2 45 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f  EXTRA_SIZE sizeo
139f3 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 20 41  f(MemPage)../* A
139f4 20 42 74 72 65 65 20 68 61 6e 64 6c 65 0a 2a 2a   Btree handle.**
139f5 0a 2a 2a 20 41 20 64 61 74 61 62 61 73 65 20 63  .** A database c
139f6 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69  onnection contai
139f7 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
139f8 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a  an instance of.*
139f9 2a 20 74 68 69 73 20 6f 62 6a 65 63 74 20 66 6f  * this object fo
139fa 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65  r every database
139fb 20 66 69 6c 65 20 74 68 61 74 20 69 74 20 68 61   file that it ha
139fc 73 20 6f 70 65 6e 2e 20 20 54 68 69 73 20 73 74  s open.  This st
139fd 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6f 70  ructure.** is op
139fe 61 71 75 65 20 74 6f 20 74 68 65 20 64 61 74 61  aque to the data
139ff 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
13a00 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63    The database c
13a01 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e 6f 74  onnection cannot
13a02 0a 2a 2a 20 73 65 65 20 74 68 65 20 69 6e 74 65  .** see the inte
13a03 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74  rnals of this st
13a04 72 75 63 74 75 72 65 20 61 6e 64 20 6f 6e 6c 79  ructure and only
13a05 20 64 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e   deals with poin
13a06 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20  ters to.** this 
13a07 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
13a08 20 46 6f 72 20 73 6f 6d 65 20 64 61 74 61 62 61   For some databa
13a09 73 65 20 66 69 6c 65 73 2c 20 74 68 65 20 73 61  se files, the sa
13a0a 6d 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61  me underlying da
13a0b 74 61 62 61 73 65 20 63 61 63 68 65 20 6d 69 67  tabase cache mig
13a0c 68 74 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64  ht be .** shared
13a0d 20 62 65 74 77 65 65 6e 20 6d 75 6c 74 69 70 6c   between multipl
13a0e 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20  e connections.  
13a0f 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 65 61  In that case, ea
13a10 63 68 20 63 6f 6e 74 65 63 74 69 6f 6e 0a 2a 2a  ch contection.**
13a11 20 68 61 73 20 69 74 20 6f 77 6e 20 70 6f 69 6e   has it own poin
13a12 74 65 72 20 74 6f 20 74 68 69 73 20 6f 62 6a 65  ter to this obje
13a13 63 74 2e 20 20 42 75 74 20 65 61 63 68 20 69 6e  ct.  But each in
13a14 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
13a15 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20  bject.** points 
13a16 74 6f 20 74 68 65 20 73 61 6d 65 20 42 74 53 68  to the same BtSh
13a17 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 54 68  ared object.  Th
13a18 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65  e database cache
13a19 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 63 68 65   and the.** sche
13a1a 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ma associated wi
13a1b 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
13a1c 66 69 6c 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e  file are all con
13a1d 74 61 69 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a  tained within.**
13a1e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62   the BtShared ob
13a1f 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  ject..**.** All 
13a20 66 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73  fields in this s
13a21 74 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63  tructure are acc
13a22 65 73 73 65 64 20 75 6e 64 65 72 20 73 71 6c 69  essed under sqli
13a23 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68  te3.mutex..** Th
13a24 65 20 70 42 74 20 70 6f 69 6e 74 65 72 20 69 74  e pBt pointer it
13a25 73 65 6c 66 20 6d 61 79 20 6e 6f 74 20 62 65 20  self may not be 
13a26 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74 68  changed while th
13a27 65 72 65 20 65 78 69 73 74 73 20 63 75 72 73 6f  ere exists curso
13a28 72 73 20 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65  rs .** in the re
13a29 66 65 72 65 6e 63 65 64 20 42 74 53 68 61 72 65  ferenced BtShare
13a2a 64 20 74 68 61 74 20 70 6f 69 6e 74 20 62 61 63  d that point bac
13a2b 6b 20 74 6f 20 74 68 69 73 20 42 74 72 65 65 20  k to this Btree 
13a2c 73 69 6e 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63  since those.** c
13a2d 75 72 73 6f 72 73 20 68 61 76 65 20 74 6f 20 64  ursors have to d
13a2e 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69  o go through thi
13a2f 73 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20  s Btree to find 
13a30 74 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61  their BtShared a
13a31 6e 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e  nd.** they often
13a32 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68   do so without h
13a33 6f 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d  olding sqlite3.m
13a34 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  utex..*/.struct 
13a35 42 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65  Btree {.  sqlite
13a36 33 20 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20  3 *db;       /* 
13a37 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
13a38 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20  nection holding 
13a39 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20  this btree */.  
13a3a 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
13a3b 20 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63     /* Sharable c
13a3c 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62  ontent of this b
13a3d 74 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54  tree */.  u8 inT
13a3e 72 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20  rans;        /* 
13a3f 54 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e  TRANS_NONE, TRAN
13a40 53 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f  S_READ or TRANS_
13a41 57 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68  WRITE */.  u8 sh
13a42 61 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a  arable;       /*
13a43 20 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20   True if we can 
13a44 73 68 61 72 65 20 70 42 74 20 77 69 74 68 20 61  share pBt with a
13a45 6e 6f 74 68 65 72 20 64 62 20 2a 2f 0a 20 20 75  nother db */.  u
13a46 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20  8 locked;       
13a47 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20    /* True if db 
13a48 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42  currently has pB
13a49 74 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e  t locked */.  in
13a4a 74 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20  t wantToLock;   
13a4b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65   /* Number of ne
13a4c 73 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71  sted calls to sq
13a4d 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
13a4e 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b  ) */.  int nBack
13a4f 75 70 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  up;       /* Num
13a50 62 65 72 20 6f 66 20 62 61 63 6b 75 70 20 6f 70  ber of backup op
13a51 65 72 61 74 69 6f 6e 73 20 72 65 61 64 69 6e 67  erations reading
13a52 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20   this btree */. 
13a53 20 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20   Btree *pNext;  
13a54 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f      /* List of o
13a55 74 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74  ther sharable Bt
13a56 72 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61  rees from the sa
13a57 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65  me db */.  Btree
13a58 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a   *pPrev;      /*
13a59 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66   Back pointer of
13a5a 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a   the same list *
13a5b 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65  /.};../*.** Btre
13a5c 65 2e 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 61  e.inTrans may ta
13a5d 6b 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  ke one of the fo
13a5e 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a  llowing values..
13a5f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61  **.** If the sha
13a60 72 65 64 2d 64 61 74 61 20 65 78 74 65 6e 73 69  red-data extensi
13a61 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74  on is enabled, t
13a62 68 65 72 65 20 6d 61 79 20 62 65 20 6d 75 6c 74  here may be mult
13a63 69 70 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66  iple users.** of
13a64 20 74 68 65 20 42 74 72 65 65 20 73 74 72 75 63   the Btree struc
13a65 74 75 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e  ture. At most on
13a66 65 20 6f 66 20 74 68 65 73 65 20 6d 61 79 20 6f  e of these may o
13a67 70 65 6e 20 61 20 77 72 69 74 65 20 74 72 61 6e  pen a write tran
13a68 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20  saction,.** but 
13a69 61 6e 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 68  any number may h
13a6a 61 76 65 20 61 63 74 69 76 65 20 72 65 61 64 20  ave active read 
13a6b 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f  transactions..*/
13a6c 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e  .#define TRANS_N
13a6d 4f 4e 45 20 20 30 0a 23 64 65 66 69 6e 65 20 54  ONE  0.#define T
13a6e 52 41 4e 53 5f 52 45 41 44 20 20 31 0a 23 64 65  RANS_READ  1.#de
13a6f 66 69 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 45  fine TRANS_WRITE
13a70 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73   2../*.** An ins
13a71 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
13a72 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20  ject represents 
13a73 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  a single databas
13a74 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41  e file..** .** A
13a75 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
13a76 20 66 69 6c 65 20 63 61 6e 20 62 65 20 69 6e 20   file can be in 
13a77 75 73 65 20 61 73 20 74 68 65 20 73 61 6d 65 20  use as the same 
13a78 74 69 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f  time by two.** o
13a79 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20  r more database 
13a7a 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68  connections.  Wh
13a7b 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63  en two or more c
13a7c 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a  onnections are.*
13a7d 2a 20 73 68 61 72 69 6e 67 20 74 68 65 20 73 61  * sharing the sa
13a7e 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  me database file
13a7f 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f  , each connectio
13a80 6e 20 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20  n has it own.** 
13a81 70 72 69 76 61 74 65 20 42 74 72 65 65 20 6f 62  private Btree ob
13a82 6a 65 63 74 20 66 6f 72 20 74 68 65 20 66 69 6c  ject for the fil
13a83 65 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68  e and each of th
13a84 6f 73 65 20 42 74 72 65 65 73 20 70 6f 69 6e 74  ose Btrees point
13a85 73 0a 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65  s.** to this one
13a86 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
13a87 2e 20 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66  .  BtShared.nRef
13a88 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
13a89 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  f.** connections
13a8a 20 63 75 72 72 65 6e 74 6c 79 20 73 68 61 72 69   currently shari
13a8b 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73 65  ng this database
13a8c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65   file..**.** Fie
13a8d 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75  lds in this stru
13a8e 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73  cture are access
13a8f 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 53  ed under the BtS
13a90 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d  hared.mutex.** m
13a91 75 74 65 78 2c 20 65 78 63 65 70 74 20 66 6f 72  utex, except for
13a92 20 6e 52 65 66 20 61 6e 64 20 70 4e 65 78 74 20   nRef and pNext 
13a93 77 68 69 63 68 20 61 72 65 20 61 63 63 65 73 73  which are access
13a94 65 64 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20  ed under the.** 
13a95 67 6c 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55  global SQLITE_MU
13a96 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
13a97 52 20 6d 75 74 65 78 2e 20 20 54 68 65 20 70 50  R mutex.  The pP
13a98 61 67 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61  ager field.** ma
13a99 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65  y not be modifie
13a9a 64 20 6f 6e 63 65 20 69 74 20 69 73 20 69 6e 69  d once it is ini
13a9b 74 69 61 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f  tially set as lo
13a9c 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a  ng as nRef>0..**
13a9d 20 54 68 65 20 70 53 63 68 65 6d 61 20 66 69 65   The pSchema fie
13a9e 6c 64 20 6d 61 79 20 62 65 20 73 65 74 20 6f 6e  ld may be set on
13a9f 63 65 20 75 6e 64 65 72 20 42 74 53 68 61 72 65  ce under BtShare
13aa0 64 2e 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74  d.mutex and.** t
13aa1 68 65 72 65 61 66 74 65 72 20 69 73 20 75 6e 63  hereafter is unc
13aa2 68 61 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 61  hanged as long a
13aa3 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20  s nRef>0..**.** 
13aa4 69 73 50 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a  isPending:.**.**
13aa5 20 20 20 49 66 20 61 20 42 74 53 68 61 72 65 64     If a BtShared
13aa6 20 63 6c 69 65 6e 74 20 66 61 69 6c 73 20 74 6f   client fails to
13aa7 20 6f 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d   obtain a write-
13aa8 6c 6f 63 6b 20 6f 6e 20 61 20 64 61 74 61 62 61  lock on a databa
13aa9 73 65 0a 2a 2a 20 20 20 74 61 62 6c 65 20 28 62  se.**   table (b
13aaa 65 63 61 75 73 65 20 74 68 65 72 65 20 65 78 69  ecause there exi
13aab 73 74 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  sts one or more 
13aac 72 65 61 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74 68  read-locks on th
13aad 65 20 74 61 62 6c 65 29 2c 0a 2a 2a 20 20 20 74  e table),.**   t
13aae 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  he shared-cache 
13aaf 65 6e 74 65 72 73 20 27 70 65 6e 64 69 6e 67 2d  enters 'pending-
13ab0 6c 6f 63 6b 27 20 73 74 61 74 65 20 61 6e 64 20  lock' state and 
13ab1 69 73 50 65 6e 64 69 6e 67 20 69 73 0a 2a 2a 20  isPending is.** 
13ab2 20 20 73 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a    set to true..*
13ab3 2a 0a 2a 2a 20 20 20 54 68 65 20 73 68 61 72 65  *.**   The share
13ab4 64 2d 63 61 63 68 65 20 6c 65 61 76 65 73 20 74  d-cache leaves t
13ab5 68 65 20 27 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  he 'pending lock
13ab6 27 20 73 74 61 74 65 20 77 68 65 6e 20 65 69 74  ' state when eit
13ab7 68 65 72 20 6f 66 0a 2a 2a 20 20 20 74 68 65 20  her of.**   the 
13ab8 66 6f 6c 6c 6f 77 69 6e 67 20 6f 63 63 75 72 3a  following occur:
13ab9 0a 2a 2a 0a 2a 2a 20 20 20 20 20 31 29 20 54 68  .**.**     1) Th
13aba 65 20 63 75 72 72 65 6e 74 20 77 72 69 74 65 72  e current writer
13abb 20 28 42 74 53 68 61 72 65 64 2e 70 57 72 69 74   (BtShared.pWrit
13abc 65 72 29 20 63 6f 6e 63 6c 75 64 65 73 20 69 74  er) concludes it
13abd 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 4f  s transaction, O
13abe 52 0a 2a 2a 20 20 20 20 20 32 29 20 54 68 65 20  R.**     2) The 
13abf 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20  number of locks 
13ac0 68 65 6c 64 20 62 79 20 6f 74 68 65 72 20 63 6f  held by other co
13ac1 6e 6e 65 63 74 69 6f 6e 73 20 64 72 6f 70 73 20  nnections drops 
13ac2 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20  to zero..**.**  
13ac3 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 27 70   while in the 'p
13ac4 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61  ending-lock' sta
13ac5 74 65 2c 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f  te, no connectio
13ac6 6e 20 6d 61 79 20 73 74 61 72 74 20 61 20 6e 65  n may start a ne
13ac7 77 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69  w.**   transacti
13ac8 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73  on..**.**   This
13ac9 20 66 65 61 74 75 72 65 20 69 73 20 69 6e 63 6c   feature is incl
13aca 75 64 65 64 20 74 6f 20 68 65 6c 70 20 70 72 65  uded to help pre
13acb 76 65 6e 74 20 77 72 69 74 65 72 2d 73 74 61 72  vent writer-star
13acc 76 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63  vation..*/.struc
13acd 74 20 42 74 53 68 61 72 65 64 20 7b 0a 20 20 50  t BtShared {.  P
13ace 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20  ager *pPager;   
13acf 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
13ad0 20 63 61 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69   cache */.  sqli
13ad1 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
13ad2 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
13ad3 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74  nnection current
13ad4 6c 79 20 75 73 69 6e 67 20 74 68 69 73 20 42 74  ly using this Bt
13ad5 72 65 65 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f  ree */.  BtCurso
13ad6 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f  r *pCursor;    /
13ad7 2a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  * A list of all 
13ad8 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a  open cursors */.
13ad9 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
13ada 31 3b 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74  1;      /* First
13adb 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74   page of the dat
13adc 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 72 65  abase */.  u8 re
13add 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20  adOnly;         
13ade 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
13adf 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20  underlying file 
13ae0 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 20  is readonly */. 
13ae1 20 75 38 20 70 61 67 65 53 69 7a 65 46 69 78 65   u8 pageSizeFixe
13ae2 64 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  d;     /* True i
13ae3 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  f the page size 
13ae4 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65  can no longer be
13ae5 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 69 66 6e   changed */.#ifn
13ae6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
13ae7 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 75 38 20  AUTOVACUUM.  u8 
13ae8 61 75 74 6f 56 61 63 75 75 6d 3b 20 20 20 20 20  autoVacuum;     
13ae9 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 75     /* True if au
13aea 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61  to-vacuum is ena
13aeb 62 6c 65 64 20 2a 2f 0a 20 20 75 38 20 69 6e 63  bled */.  u8 inc
13aec 72 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20  rVacuum;        
13aed 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 63 72 2d  /* True if incr-
13aee 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65  vacuum is enable
13aef 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31  d */.#endif.  u1
13af0 36 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20  6 pageSize;     
13af1 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
13af2 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 6e 20  ber of bytes on 
13af3 61 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20  a page */.  u16 
13af4 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20  usableSize;     
13af5 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75    /* Number of u
13af6 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 65  sable bytes on e
13af7 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 31  ach page */.  u1
13af8 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20  6 maxLocal;     
13af9 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
13afa 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20  ocal payload in 
13afb 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62  non-LEAFDATA tab
13afc 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e  les */.  u16 min
13afd 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f  Local;         /
13afe 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20  * Minimum local 
13aff 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c  payload in non-L
13b00 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a  EAFDATA tables *
13b01 2f 0a 20 20 75 31 36 20 6d 61 78 4c 65 61 66 3b  /.  u16 maxLeaf;
13b02 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
13b03 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f  imum local paylo
13b04 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41  ad in a LEAFDATA
13b05 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20   table */.  u16 
13b06 6d 69 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20  minLeaf;        
13b07 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63    /* Minimum loc
13b08 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20  al payload in a 
13b09 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a  LEAFDATA table *
13b0a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 61 63  /.  u8 inTransac
13b0b 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61  tion;     /* Tra
13b0c 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 2a  nsaction state *
13b0d 2f 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 61 63  /.  int nTransac
13b0e 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  tion;     /* Num
13b0f 62 65 72 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e  ber of open tran
13b10 73 61 63 74 69 6f 6e 73 20 28 72 65 61 64 20 2b  sactions (read +
13b11 20 77 72 69 74 65 29 20 2a 2f 0a 20 20 76 6f 69   write) */.  voi
13b12 64 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20  d *pSchema;     
13b13 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
13b14 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
13b15 20 62 79 20 73 71 6c 69 74 65 33 42 74 72 65 65   by sqlite3Btree
13b16 53 63 68 65 6d 61 28 29 20 2a 2f 0a 20 20 76 6f  Schema() */.  vo
13b17 69 64 20 28 2a 78 46 72 65 65 53 63 68 65 6d 61  id (*xFreeSchema
13b18 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65  )(void*);  /* De
13b19 73 74 72 75 63 74 6f 72 20 66 6f 72 20 42 74 53  structor for BtS
13b1a 68 61 72 65 64 2e 70 53 63 68 65 6d 61 20 2a 2f  hared.pSchema */
13b1b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
13b1c 20 2a 6d 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e 2d   *mutex; /* Non-
13b1d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
13b1e 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 63 65  required to acce
13b1f 73 73 20 74 68 69 73 20 73 74 72 75 63 74 20 2a  ss this struct *
13b20 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 48 61 73  /.  Bitvec *pHas
13b21 43 6f 6e 74 65 6e 74 3b 20 20 2f 2a 20 53 65 74  Content;  /* Set
13b22 20 6f 66 20 70 61 67 65 73 20 6d 6f 76 65 64 20   of pages moved 
13b23 74 6f 20 66 72 65 65 2d 6c 69 73 74 20 74 68 69  to free-list thi
13b24 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  s transaction */
13b25 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
13b26 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
13b27 45 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  E.  int nRef;   
13b28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
13b29 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
13b2a 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  s to this struct
13b2b 75 72 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65  ure */.  BtShare
13b2c 64 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f  d *pNext;      /
13b2d 2a 20 4e 65 78 74 20 6f 6e 20 61 20 6c 69 73 74  * Next on a list
13b2e 20 6f 66 20 73 68 61 72 61 62 6c 65 20 42 74 53   of sharable BtS
13b2f 68 61 72 65 64 20 73 74 72 75 63 74 73 20 2a 2f  hared structs */
13b30 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b  .  BtLock *pLock
13b31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  ;        /* List
13b32 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f   of locks held o
13b33 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74  n this shared-bt
13b34 72 65 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20  ree struct */.  
13b35 42 74 72 65 65 20 2a 70 57 72 69 74 65 72 3b 20  Btree *pWriter; 
13b36 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 77        /* Btree w
13b37 69 74 68 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  ith currently op
13b38 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  en write transac
13b39 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 69 73 45  tion */.  u8 isE
13b3a 78 63 6c 75 73 69 76 65 3b 20 20 20 20 20 20 20  xclusive;       
13b3b 2f 2a 20 54 72 75 65 20 69 66 20 70 57 72 69 74  /* True if pWrit
13b3c 65 72 20 68 61 73 20 61 6e 20 45 58 43 4c 55 53  er has an EXCLUS
13b3d 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  IVE lock on the 
13b3e 64 62 20 2a 2f 0a 20 20 75 38 20 69 73 50 65 6e  db */.  u8 isPen
13b3f 64 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  ding;         /*
13b40 20 49 66 20 77 61 69 74 69 6e 67 20 66 6f 72 20   If waiting for 
13b41 72 65 61 64 2d 6c 6f 63 6b 73 20 74 6f 20 63 6c  read-locks to cl
13b42 65 61 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ear */.#endif.  
13b43 75 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20  u8 *pTmpSpace;  
13b44 20 20 20 20 20 20 2f 2a 20 42 74 53 68 61 72 65        /* BtShare
13b45 64 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  d.pageSize bytes
13b46 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d   of space for tm
13b47 70 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  p use */.};../*.
13b48 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
13b49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
13b4a 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
13b4b 64 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d  d to hold inform
13b4c 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61  ation.** about a
13b4d 20 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72 73   cell.  The pars
13b4e 65 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63 74  eCellPtr() funct
13b4f 69 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68 69  ion fills in thi
13b50 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 62  s structure.** b
13b51 61 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74  ased on informat
13b52 69 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f 6d  ion extract from
13b53 20 74 68 65 20 72 61 77 20 64 69 73 6b 20 70 61   the raw disk pa
13b54 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ge..*/.typedef s
13b55 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43  truct CellInfo C
13b56 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20  ellInfo;.struct 
13b57 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 75 38 20  CellInfo {.  u8 
13b58 2a 70 43 65 6c 6c 3b 20 20 20 20 20 2f 2a 20 50  *pCell;     /* P
13b59 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 74  ointer to the st
13b5a 61 72 74 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74  art of cell cont
13b5b 65 6e 74 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65  ent */.  i64 nKe
13b5c 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6b  y;      /* The k
13b5d 65 79 20 66 6f 72 20 49 4e 54 4b 45 59 20 74 61  ey for INTKEY ta
13b5e 62 6c 65 73 2c 20 6f 72 20 6e 75 6d 62 65 72 20  bles, or number 
13b5f 6f 66 20 62 79 74 65 73 20 69 6e 20 6b 65 79 20  of bytes in key 
13b60 2a 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b 20  */.  u32 nData; 
13b61 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
13b62 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 2a   bytes of data *
13b63 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64  /.  u32 nPayload
13b64 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75  ;  /* Total amou
13b65 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f  nt of payload */
13b66 0a 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b 20  .  u16 nHeader; 
13b67 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
13b68 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
13b69 61 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f  ader in bytes */
13b6a 0a 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20  .  u16 nLocal;  
13b6b 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70    /* Amount of p
13b6c 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61  ayload held loca
13b6d 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76  lly */.  u16 iOv
13b6e 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65  erflow; /* Offse
13b6f 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  t to overflow pa
13b70 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f  ge number.  Zero
13b71 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20   if no overflow 
13b72 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b 20  */.  u16 nSize; 
13b73 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
13b74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
13b75 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72  on the main b-tr
13b76 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f  ee page */.};../
13b77 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70  *.** Maximum dep
13b78 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 20  th of an SQLite 
13b79 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65  B-Tree structure
13b7a 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65 65  . Any B-Tree dee
13b7b 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69 73  per than.** this
13b7c 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72 65   will be declare
13b7d 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 20  d corrupt. This 
13b7e 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c 61  value is calcula
13b7f 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a 2a  ted based on a.*
13b80 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61  * maximum databa
13b81 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31 20  se size of 2^31 
13b82 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20  pages a minimum 
13b83 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72 20  fanout of 2 for 
13b84 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 61  a.** root-node a
13b85 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74 68  nd 3 for all oth
13b86 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  er internal node
13b87 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72  s..**.** If a tr
13b88 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ee that appears 
13b89 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68 61  to be taller tha
13b8a 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75 6e  n this is encoun
13b8b 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a 20  tered, it is.** 
13b8c 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
13b8d 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
13b8e 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rupt..*/.#define
13b8f 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45   BTCURSOR_MAX_DE
13b90 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 20  PTH 20../*.** A 
13b91 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e  cursor is a poin
13b92 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 75  ter to a particu
13b93 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 6e  lar entry within
13b94 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
13b95 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20 61   b-tree within a
13b96 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
13b97 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 20  **.** The entry 
13b98 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  is identified by
13b99 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e 64   its MemPage and
13b9a 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a   the index in.**
13b9b 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d   MemPage.aCell[]
13b9c 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a   of the entry..*
13b9d 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 69 6e 67  *.** When a sing
13b9e 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
13b9f 20 63 61 6e 20 73 68 61 72 65 64 20 62 79 20 74   can shared by t
13ba0 77 6f 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65  wo more database
13ba1 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a   connections,.**
13ba2 20 62 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e   but cursors can
13ba3 6e 6f 74 20 62 65 20 73 68 61 72 65 64 2e 20 20  not be shared.  
13ba4 45 61 63 68 20 63 75 72 73 6f 72 20 69 73 20 61  Each cursor is a
13ba5 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
13ba6 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64  .** particular d
13ba7 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
13ba8 6f 6e 20 69 64 65 6e 74 69 66 69 65 64 20 42 74  on identified Bt
13ba9 43 75 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62  Cursor.pBtree.db
13baa 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69  ..**.** Fields i
13bab 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
13bac 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
13bad 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64  der the BtShared
13bae 2e 6d 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20  .mutex.** found 
13baf 61 74 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75  at self->pBt->mu
13bb0 74 65 78 2e 20 0a 2a 2f 0a 73 74 72 75 63 74 20  tex. .*/.struct 
13bb1 42 74 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72  BtCursor {.  Btr
13bb2 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20  ee *pBtree;     
13bb3 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74         /* The Bt
13bb4 72 65 65 20 74 6f 20 77 68 69 63 68 20 74 68 69  ree to which thi
13bb5 73 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73  s cursor belongs
13bb6 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
13bb7 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  pBt;            
13bb8 2f 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 20  /* The BtShared 
13bb9 74 68 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e  this cursor poin
13bba 74 73 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72  ts to */.  BtCur
13bbb 73 6f 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72  sor *pNext, *pPr
13bbc 65 76 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20  ev;  /* Forms a 
13bbd 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61  linked list of a
13bbe 6c 6c 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20  ll cursors */.  
13bbf 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a  struct KeyInfo *
13bc0 70 4b 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67  pKeyInfo; /* Arg
13bc1 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20  ument passed to 
13bc2 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
13bc3 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ion */.  Pgno pg
13bc4 6e 6f 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20  noRoot;         
13bc5 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70     /* The root p
13bc6 61 67 65 20 6f 66 20 74 68 69 73 20 74 72 65 65  age of this tree
13bc7 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
13bc8 74 36 34 20 63 61 63 68 65 64 52 6f 77 69 64 3b  t64 cachedRowid;
13bc9 20 2f 2a 20 4e 65 78 74 20 72 6f 77 69 64 20 63   /* Next rowid c
13bca 61 63 68 65 2e 20 20 30 20 6d 65 61 6e 73 20 6e  ache.  0 means n
13bcb 6f 74 20 76 61 6c 69 64 20 2a 2f 0a 20 20 43 65  ot valid */.  Ce
13bcc 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 20 20  llInfo info;    
13bcd 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72          /* A par
13bce 73 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 77  se of the cell w
13bcf 65 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61  e are pointing a
13bd0 74 20 2a 2f 0a 20 20 75 38 20 77 72 46 6c 61 67  t */.  u8 wrFlag
13bd1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13bd2 20 2f 2a 20 54 72 75 65 20 69 66 20 77 72 69 74   /* True if writ
13bd3 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 61 74 4c  able */.  u8 atL
13bd4 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ast;            
13bd5 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f      /* Cursor po
13bd6 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61  inting to the la
13bd7 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 38  st entry */.  u8
13bd8 20 76 61 6c 69 64 4e 4b 65 79 3b 20 20 20 20 20   validNKey;     
13bd9 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
13bda 69 66 20 69 6e 66 6f 2e 6e 4b 65 79 20 69 73 20  if info.nKey is 
13bdb 76 61 6c 69 64 20 2a 2f 0a 20 20 75 38 20 65 53  valid */.  u8 eS
13bdc 74 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  tate;           
13bdd 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
13bde 68 65 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f  he CURSOR_XXX co
13bdf 6e 73 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c  nstants (see bel
13be0 6f 77 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ow) */.  void *p
13be1 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76  Key;      /* Sav
13be2 65 64 20 6b 65 79 20 74 68 61 74 20 77 61 73 20  ed key that was 
13be3 63 75 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e  cursor's last kn
13be4 6f 77 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a  own position */.
13be5 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20    i64 nKey;     
13be6 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b     /* Size of pK
13be7 65 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 65  ey, or last inte
13be8 67 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  ger key */.  int
13be9 20 73 6b 69 70 3b 20 20 20 20 20 20 20 20 2f 2a   skip;        /*
13bea 20 28 73 6b 69 70 3c 30 29 20 2d 3e 20 50 72 65   (skip<0) -> Pre
13beb 76 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  v() is a no-op. 
13bec 28 73 6b 69 70 3e 30 29 20 2d 3e 20 4e 65 78 74  (skip>0) -> Next
13bed 28 29 20 69 73 20 2a 2f 0a 23 69 66 6e 64 65 66  () is */.#ifndef
13bee 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
13bef 52 42 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e 63  RBLOB.  u8 isInc
13bf0 72 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 20  rblobHandle;    
13bf1 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
13bf2 73 20 63 75 72 73 6f 72 20 69 73 20 61 6e 20 69  s cursor is an i
13bf3 6e 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 2a  ncr. io handle *
13bf4 2f 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66  /.  Pgno *aOverf
13bf5 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  low;          /*
13bf6 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c   Cache of overfl
13bf7 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e  ow page location
13bf8 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e  s */.#endif.#ifn
13bf9 64 65 66 20 4e 44 45 42 55 47 0a 20 20 75 38 20  def NDEBUG.  u8 
13bfa 70 61 67 65 73 53 68 75 66 66 6c 65 64 3b 20 20  pagesShuffled;  
13bfb 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
13bfc 66 20 42 74 72 65 65 20 70 61 67 65 73 20 61 72  f Btree pages ar
13bfd 65 20 72 65 61 72 72 61 6e 67 65 64 20 62 79 20  e rearranged by 
13bfe 62 61 6c 61 6e 63 65 28 29 2a 2f 0a 23 65 6e 64  balance()*/.#end
13bff 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b 20  if.  i16 iPage; 
13c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c01 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
13c02 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 70  dex of current p
13c03 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a 2f  age in apPage */
13c04 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 61  .  MemPage *apPa
13c05 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f  ge[BTCURSOR_MAX_
13c06 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 65  DEPTH];  /* Page
13c07 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 63  s from root to c
13c08 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20  urrent page */. 
13c09 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 52   u16 aiIdx[BTCUR
13c0a 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20  SOR_MAX_DEPTH]; 
13c0b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
13c0c 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 67  t index in apPag
13c0d 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  e[i] */.};../*.*
13c0e 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75  * Potential valu
13c0f 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 2e  es for BtCursor.
13c10 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 55  eState..**.** CU
13c11 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20  RSOR_VALID:.**  
13c12 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74   Cursor points t
13c13 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e  o a valid entry.
13c14 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65 74   getPayload() et
13c15 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64  c. may be called
13c16 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 49  ..**.** CURSOR_I
13c17 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72  NVALID:.**   Cur
13c18 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f 69  sor does not poi
13c19 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e  nt to a valid en
13c1a 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68 61  try. This can ha
13c1b 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 6c  ppen (for exampl
13c1c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 65  e) .**   because
13c1d 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d   the table is em
13c1e 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 20 42  pty or because B
13c1f 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 74 28  treeCursorFirst(
13c20 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a  ) has not been.*
13c21 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a  *   called..**.*
13c22 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  * CURSOR_REQUIRE
13c23 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 74  SEEK:.**   The t
13c24 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 63  able that this c
13c25 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64  ursor was opened
13c26 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 73   on still exists
13c27 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20 0a  , but has been .
13c28 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73 69  **   modified si
13c29 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 77  nce the cursor w
13c2a 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54 68  as last used. Th
13c2b 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f  e cursor positio
13c2c 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 20  n is saved.**   
13c2d 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74 43  in variables BtC
13c2e 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 42  ursor.pKey and B
13c2f 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 68  tCursor.nKey. Wh
13c30 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20 69  en a cursor is i
13c31 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 61  n .**   this sta
13c32 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73 6f  te, restoreCurso
13c33 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20  rPosition() can 
13c34 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 74  be called to att
13c35 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65  empt to.**   see
13c36 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  k the cursor to 
13c37 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 69  the saved positi
13c38 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52  on..**.** CURSOR
13c39 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75  _FAULT:.**   A u
13c3a 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72 72  nrecoverable err
13c3b 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72  or (an I/O error
13c3c 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   or a malloc fai
13c3d 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72 72  lure) has occurr
13c3e 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66  ed.**   on a dif
13c3f 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f  ferent connectio
13c40 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68  n that shares th
13c41 65 20 42 74 53 68 61 72 65 64 20 63 61 63 68 65  e BtShared cache
13c42 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 20   with this.**   
13c43 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72 72  cursor.  The err
13c44 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 65 20  or has left the 
13c45 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f  cache in an inco
13c46 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a  nsistent state..
13c47 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20  **   Do nothing 
13c48 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 20 63  else with this c
13c49 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 65  ursor.  Any atte
13c4a 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 63  mpt to use the c
13c4b 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c  ursor.**   shoul
13c4c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72  d return the err
13c4d 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69  or code stored i
13c4e 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a  n BtCursor.skip.
13c4f 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f  */.#define CURSO
13c50 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 20  R_INVALID       
13c51 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 55      0.#define CU
13c52 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 20  RSOR_VALID      
13c53 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
13c54 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
13c55 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65 66  EEK       2.#def
13c56 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54  ine CURSOR_FAULT
13c57 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 0a               3..
13c58 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61 62  /* .** The datab
13c59 61 73 65 20 70 61 67 65 20 74 68 65 20 50 45 4e  ase page the PEN
13c5a 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 69  DING_BYTE occupi
13c5b 65 73 2e 20 54 68 69 73 20 70 61 67 65 20 69 73  es. This page is
13c5c 20 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f 0a   never used..*/.
13c5d 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47  # define PENDING
13c5e 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
13c5f 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 42  PAGER_MJ_PGNO(pB
13c60 74 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b  t)../*.** A link
13c61 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66  ed list of the f
13c62 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
13c63 72 65 73 20 69 73 20 73 74 6f 72 65 64 20 61 74  res is stored at
13c64 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e   BtShared.pLock.
13c65 0a 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 61 64  .** Locks are ad
13c66 64 65 64 20 28 6f 72 20 75 70 67 72 61 64 65 64  ded (or upgraded
13c67 20 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20   from READ_LOCK 
13c68 74 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77  to WRITE_LOCK) w
13c69 68 65 6e 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a  hen a cursor .**
13c6a 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68   is opened on th
13c6b 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f  e table with roo
13c6c 74 20 70 61 67 65 20 42 74 53 68 61 72 65 64 2e  t page BtShared.
13c6d 69 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72  iTable. Locks ar
13c6e 65 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f  e removed.** fro
13c6f 6d 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e  m this list when
13c70 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
13c71 73 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72  s committed or r
13c72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 20 77  olled back, or w
13c73 68 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 20 68  hen.** a btree h
13c74 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e  andle is closed.
13c75 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c 6f 63  .*/.struct BtLoc
13c76 6b 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74  k {.  Btree *pBt
13c77 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42  ree;        /* B
13c78 74 72 65 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64  tree handle hold
13c79 69 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f  ing this lock */
13c7a 0a 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20  .  Pgno iTable; 
13c7b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
13c7c 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 2a   page of table *
13c7d 2f 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20  /.  u8 eLock;   
13c7e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41            /* REA
13c7f 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f  D_LOCK or WRITE_
13c80 4c 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b  LOCK */.  BtLock
13c81 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20   *pNext;        
13c82 2f 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 68 61  /* Next in BtSha
13c83 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a  red.pLock list *
13c84 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61  /.};../* Candida
13c85 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74  te values for Bt
13c86 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64  Lock.eLock */.#d
13c87 65 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20  efine READ_LOCK 
13c88 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 52      1.#define WR
13c89 49 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f  ITE_LOCK    2../
13c8a 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f  *.** These macro
13c8b 73 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63  s define the loc
13c8c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 69  ation of the poi
13c8d 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66  nter-map entry f
13c8e 6f 72 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73  or a .** databas
13c8f 65 20 70 61 67 65 2e 20 54 68 65 20 66 69 72 73  e page. The firs
13c90 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 65 61  t argument to ea
13c91 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ch is the number
13c92 20 6f 66 20 75 73 61 62 6c 65 0a 2a 2a 20 62 79   of usable.** by
13c93 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65  tes on each page
13c94 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
13c95 20 28 6f 66 74 65 6e 20 31 30 32 34 29 2e 20 54   (often 1024). T
13c96 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65  he second is the
13c97 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20  .** page number 
13c98 74 6f 20 6c 6f 6f 6b 20 75 70 20 69 6e 20 74 68  to look up in th
13c99 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a  e pointer map..*
13c9a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 50 41 47 45  *.** PTRMAP_PAGE
13c9b 4e 4f 20 72 65 74 75 72 6e 73 20 74 68 65 20 64  NO returns the d
13c9c 61 74 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d  atabase page num
13c9d 62 65 72 20 6f 66 20 74 68 65 20 70 6f 69 6e 74  ber of the point
13c9e 65 72 2d 6d 61 70 0a 2a 2a 20 70 61 67 65 20 74  er-map.** page t
13c9f 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 72  hat stores the r
13ca0 65 71 75 69 72 65 64 20 70 6f 69 6e 74 65 72 2e  equired pointer.
13ca1 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45   PTRMAP_PTROFFSE
13ca2 54 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65  T returns.** the
13ca3 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 72   offset of the r
13ca4 65 71 75 65 73 74 65 64 20 6d 61 70 20 65 6e 74  equested map ent
13ca5 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ry..**.** If the
13ca6 20 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70   pgno argument p
13ca7 61 73 73 65 64 20 74 6f 20 50 54 52 4d 41 50 5f  assed to PTRMAP_
13ca8 50 41 47 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e  PAGENO is a poin
13ca9 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 0a 2a 2a  ter-map page,.**
13caa 20 74 68 65 6e 20 70 67 6e 6f 20 69 73 20 72 65   then pgno is re
13cab 74 75 72 6e 65 64 2e 20 53 6f 20 28 70 67 6e 6f  turned. So (pgno
13cac 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  ==PTRMAP_PAGENO(
13cad 70 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63 61 6e  pgsz, pgno)) can
13cae 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 74   be.** used to t
13caf 65 73 74 20 69 66 20 70 67 6e 6f 20 69 73 20 61  est if pgno is a
13cb0 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
13cb1 65 2e 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  e. PTRMAP_ISPAGE
13cb2 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74   implements.** t
13cb3 68 69 73 20 74 65 73 74 2e 0a 2a 2f 0a 23 64 65  his test..*/.#de
13cb4 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 41 47 45  fine PTRMAP_PAGE
13cb5 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 29 20 70 74  NO(pBt, pgno) pt
13cb6 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20  rmapPageno(pBt, 
13cb7 70 67 6e 6f 29 0a 23 64 65 66 69 6e 65 20 50 54  pgno).#define PT
13cb8 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 70  RMAP_PTROFFSET(p
13cb9 67 70 74 72 6d 61 70 2c 20 70 67 6e 6f 29 20 28  gptrmap, pgno) (
13cba 35 2a 28 70 67 6e 6f 2d 70 67 70 74 72 6d 61 70  5*(pgno-pgptrmap
13cbb 2d 31 29 29 0a 23 64 65 66 69 6e 65 20 50 54 52  -1)).#define PTR
13cbc 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
13cbd 70 67 6e 6f 29 20 28 50 54 52 4d 41 50 5f 50 41  pgno) (PTRMAP_PA
13cbe 47 45 4e 4f 28 28 70 42 74 29 2c 28 70 67 6e 6f  GENO((pBt),(pgno
13cbf 29 29 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a  ))==(pgno))../*.
13cc0 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d  ** The pointer m
13cc1 61 70 20 69 73 20 61 20 6c 6f 6f 6b 75 70 20 74  ap is a lookup t
13cc2 61 62 6c 65 20 74 68 61 74 20 69 64 65 6e 74 69  able that identi
13cc3 66 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20  fies the parent 
13cc4 70 61 67 65 20 66 6f 72 0a 2a 2a 20 65 61 63 68  page for.** each
13cc5 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 20 74   child page in t
13cc6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13cc7 2e 20 20 54 68 65 20 70 61 72 65 6e 74 20 70 61  .  The parent pa
13cc8 67 65 20 69 73 20 74 68 65 20 70 61 67 65 20 74  ge is the page t
13cc9 68 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20  hat.** contains 
13cca 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
13ccb 20 63 68 69 6c 64 2e 20 20 45 76 65 72 79 20 70   child.  Every p
13ccc 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
13ccd 61 73 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  ase contains.** 
13cce 30 20 6f 72 20 31 20 70 61 72 65 6e 74 20 70 61  0 or 1 parent pa
13ccf 67 65 73 2e 20 20 28 49 6e 20 74 68 69 73 20 63  ges.  (In this c
13cd0 6f 6e 74 65 78 74 20 27 64 61 74 61 62 61 73 65  ontext 'database
13cd1 20 70 61 67 65 27 20 72 65 66 65 72 73 0a 2a 2a   page' refers.**
13cd2 20 74 6f 20 61 6e 79 20 70 61 67 65 20 74 68 61   to any page tha
13cd3 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
13cd4 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
13cd5 20 69 74 73 65 6c 66 2e 29 20 20 45 61 63 68 20   itself.)  Each 
13cd6 70 6f 69 6e 74 65 72 20 6d 61 70 0a 2a 2a 20 65  pointer map.** e
13cd7 6e 74 72 79 20 63 6f 6e 73 69 73 74 73 20 6f 66  ntry consists of
13cd8 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 27   a single byte '
13cd9 74 79 70 65 27 20 61 6e 64 20 61 20 34 20 62 79  type' and a 4 by
13cda 74 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e  te parent page n
13cdb 75 6d 62 65 72 2e 0a 2a 2a 20 54 68 65 20 50 54  umber..** The PT
13cdc 52 4d 41 50 5f 58 58 58 20 69 64 65 6e 74 69 66  RMAP_XXX identif
13cdd 69 65 72 73 20 62 65 6c 6f 77 20 61 72 65 20 74  iers below are t
13cde 68 65 20 76 61 6c 69 64 20 74 79 70 65 73 2e 0a  he valid types..
13cdf 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f 73  **.** The purpos
13ce0 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72  e of the pointer
13ce1 20 6d 61 70 20 69 73 20 74 6f 20 66 61 63 69 6c   map is to facil
13ce2 69 74 79 20 6d 6f 76 69 6e 67 20 70 61 67 65 73  ity moving pages
13ce3 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73   from one.** pos
13ce4 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c  ition in the fil
13ce5 65 20 74 6f 20 61 6e 6f 74 68 65 72 20 61 73 20  e to another as 
13ce6 70 61 72 74 20 6f 66 20 61 75 74 6f 76 61 63 75  part of autovacu
13ce7 75 6d 2e 20 20 57 68 65 6e 20 61 20 70 61 67 65  um.  When a page
13ce8 0a 2a 2a 20 69 73 20 6d 6f 76 65 64 2c 20 74 68  .** is moved, th
13ce9 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 69 74 73  e pointer in its
13cea 20 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20   parent must be 
13ceb 75 70 64 61 74 65 64 20 74 6f 20 70 6f 69 6e 74  updated to point
13cec 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c   to the.** new l
13ced 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 6f  ocation.  The po
13cee 69 6e 74 65 72 20 6d 61 70 20 69 73 20 75 73 65  inter map is use
13cef 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  d to locate the 
13cf0 70 61 72 65 6e 74 20 70 61 67 65 20 71 75 69 63  parent page quic
13cf1 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  kly..**.** PTRMA
13cf2 50 5f 52 4f 4f 54 50 41 47 45 3a 20 54 68 65 20  P_ROOTPAGE: The 
13cf3 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
13cf4 20 61 20 72 6f 6f 74 2d 70 61 67 65 2e 20 54 68   a root-page. Th
13cf5 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73  e page-number is
13cf6 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   not.**         
13cf7 20 20 20 20 20 20 20 20 20 75 73 65 64 20 69 6e           used in
13cf8 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a   this case..**.*
13cf9 2a 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  * PTRMAP_FREEPAG
13cfa 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  E: The database 
13cfb 70 61 67 65 20 69 73 20 61 6e 20 75 6e 75 73 65  page is an unuse
13cfc 64 20 28 66 72 65 65 29 20 70 61 67 65 2e 20 54  d (free) page. T
13cfd 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 0a  he page-number .
13cfe 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
13cff 20 20 20 20 69 73 20 6e 6f 74 20 75 73 65 64 20      is not used 
13d00 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
13d01 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46  .** PTRMAP_OVERF
13d02 4c 4f 57 31 3a 20 54 68 65 20 64 61 74 61 62 61  LOW1: The databa
13d03 73 65 20 70 61 67 65 20 69 73 20 74 68 65 20 66  se page is the f
13d04 69 72 73 74 20 70 61 67 65 20 69 6e 20 61 20 6c  irst page in a l
13d05 69 73 74 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20  ist of .**      
13d06 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65               ove
13d07 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65  rflow pages. The
13d08 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 64 65   page number ide
13d09 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 67 65  ntifies the page
13d0a 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
13d0b 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61             conta
13d0c 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 77 69 74  ins the cell wit
13d0d 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  h a pointer to t
13d0e 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  his overflow pag
13d0f 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  e..**.** PTRMAP_
13d10 4f 56 45 52 46 4c 4f 57 32 3a 20 54 68 65 20 64  OVERFLOW2: The d
13d11 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20  atabase page is 
13d12 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 20 6c 61  the second or la
13d13 74 65 72 20 70 61 67 65 20 69 6e 20 61 20 6c 69  ter page in a li
13d14 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  st of.**        
13d15 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66             overf
13d16 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70  low pages. The p
13d17 61 67 65 2d 6e 75 6d 62 65 72 20 69 64 65 6e 74  age-number ident
13d18 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f  ifies the previo
13d19 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  us.**           
13d1a 20 20 20 20 20 20 20 20 70 61 67 65 20 69 6e 20          page in 
13d1b 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
13d1c 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54  e list..**.** PT
13d1d 52 4d 41 50 5f 42 54 52 45 45 3a 20 54 68 65 20  RMAP_BTREE: The 
13d1e 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
13d1f 20 61 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74 72 65   a non-root btre
13d20 65 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65  e page. The page
13d21 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20   number.**      
13d22 20 20 20 20 20 20 20 20 20 69 64 65 6e 74 69 66           identif
13d23 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70  ies the parent p
13d24 61 67 65 20 69 6e 20 74 68 65 20 62 74 72 65 65  age in the btree
13d25 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52  ..*/.#define PTR
13d26 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 31 0a 23  MAP_ROOTPAGE 1.#
13d27 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 46 52  define PTRMAP_FR
13d28 45 45 50 41 47 45 20 32 0a 23 64 65 66 69 6e 65  EEPAGE 2.#define
13d29 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
13d2a 31 20 33 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  1 3.#define PTRM
13d2b 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 34 0a 23  AP_OVERFLOW2 4.#
13d2c 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 42 54  define PTRMAP_BT
13d2d 52 45 45 20 35 0a 0a 2f 2a 20 41 20 62 75 6e 63  REE 5../* A bunc
13d2e 68 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74  h of assert() st
13d2f 61 74 65 6d 65 6e 74 73 20 74 6f 20 63 68 65 63  atements to chec
13d30 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
13d31 6e 20 73 74 61 74 65 20 76 61 72 69 61 62 6c 65  n state variable
13d32 73 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 20 70  s.** of handle p
13d33 20 28 74 79 70 65 20 42 74 72 65 65 2a 29 20 61   (type Btree*) a
13d34 72 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f  re internally co
13d35 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65  nsistent..*/.#de
13d36 66 69 6e 65 20 62 74 72 65 65 49 6e 74 65 67 72  fine btreeIntegr
13d37 69 74 79 28 70 29 20 5c 0a 20 20 61 73 73 65 72  ity(p) \.  asser
13d38 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61  t( p->pBt->inTra
13d39 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f  nsaction!=TRANS_
13d3a 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e  NONE || p->pBt->
13d3b 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 30 20  nTransaction==0 
13d3c 29 3b 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70  ); \.  assert( p
13d3d 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ->pBt->inTransac
13d3e 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73  tion>=p->inTrans
13d3f 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   ); .../*.** The
13d40 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 6d 61   ISAUTOVACUUM ma
13d41 63 72 6f 20 69 73 20 75 73 65 64 20 77 69 74 68  cro is used with
13d42 69 6e 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f  in balance_nonro
13d43 6f 74 28 29 20 74 6f 20 64 65 74 65 72 6d 69 6e  ot() to determin
13d44 65 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61  e.** if the data
13d45 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75  base supports au
13d46 74 6f 2d 76 61 63 75 75 6d 20 6f 72 20 6e 6f 74  to-vacuum or not
13d47 2e 20 42 65 63 61 75 73 65 20 69 74 20 69 73 20  . Because it is 
13d48 75 73 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61  used.** within a
13d49 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  n expression tha
13d4a 74 20 69 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  t is an argument
13d4b 20 74 6f 20 61 6e 6f 74 68 65 72 20 6d 61 63 72   to another macr
13d4c 6f 20 0a 2a 2a 20 28 73 71 6c 69 74 65 4d 61 6c  o .** (sqliteMal
13d4d 6c 6f 63 52 61 77 29 2c 20 69 74 20 69 73 20 6e  locRaw), it is n
13d4e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75  ot possible to u
13d4f 73 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 63  se conditional c
13d50 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53  ompilation..** S
13d51 6f 2c 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73  o, this macro is
13d52 20 64 65 66 69 6e 65 64 20 69 6e 73 74 65 61 64   defined instead
13d53 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
13d54 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
13d55 55 55 4d 0a 23 64 65 66 69 6e 65 20 49 53 41 55  UUM.#define ISAU
13d56 54 4f 56 41 43 55 55 4d 20 28 70 42 74 2d 3e 61  TOVACUUM (pBt->a
13d57 75 74 6f 56 61 63 75 75 6d 29 0a 23 65 6c 73 65  utoVacuum).#else
13d58 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56  .#define ISAUTOV
13d59 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a  ACUUM 0.#endif..
13d5a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  ./*.** This stru
13d5b 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20  cture is passed 
13d5c 61 72 6f 75 6e 64 20 74 68 72 6f 75 67 68 20 61  around through a
13d5d 6c 6c 20 74 68 65 20 73 61 6e 69 74 79 20 63 68  ll the sanity ch
13d5e 65 63 6b 69 6e 67 20 72 6f 75 74 69 6e 65 73 0a  ecking routines.
13d5f 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  ** in order to k
13d60 65 65 70 20 74 72 61 63 6b 20 6f 66 20 73 6f 6d  eep track of som
13d61 65 20 67 6c 6f 62 61 6c 20 73 74 61 74 65 20 69  e global state i
13d62 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  nformation..*/.t
13d63 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e  ypedef struct In
13d64 74 65 67 72 69 74 79 43 6b 20 49 6e 74 65 67 72  tegrityCk Integr
13d65 69 74 79 43 6b 3b 0a 73 74 72 75 63 74 20 49 6e  ityCk;.struct In
13d66 74 65 67 72 69 74 79 43 6b 20 7b 0a 20 20 42 74  tegrityCk {.  Bt
13d67 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20  Shared *pBt;    
13d68 2f 2a 20 54 68 65 20 74 72 65 65 20 62 65 69 6e  /* The tree bein
13d69 67 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f  g checked out */
13d6a 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
13d6b 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f  ;    /* The asso
13d6c 63 69 61 74 65 64 20 70 61 67 65 72 2e 20 20 41  ciated pager.  A
13d6d 6c 73 6f 20 61 63 63 65 73 73 69 62 6c 65 20 62  lso accessible b
13d6e 79 20 70 42 74 2d 3e 70 50 61 67 65 72 20 2a 2f  y pBt->pPager */
13d6f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20  .  Pgno nPage;  
13d70 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
13d71 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
13d72 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
13d73 20 2a 61 6e 52 65 66 3b 20 20 20 20 20 20 20 2f   *anRef;       /
13d74 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  * Number of time
13d75 73 20 65 61 63 68 20 70 61 67 65 20 69 73 20 72  s each page is r
13d76 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69  eferenced */.  i
13d77 6e 74 20 6d 78 45 72 72 3b 20 20 20 20 20 20 20  nt mxErr;       
13d78 20 2f 2a 20 53 74 6f 70 20 61 63 63 75 6d 75 6c   /* Stop accumul
13d79 61 74 69 6e 67 20 65 72 72 6f 72 73 20 77 68 65  ating errors whe
13d7a 6e 20 74 68 69 73 20 72 65 61 63 68 65 73 20 7a  n this reaches z
13d7b 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72  ero */.  int nEr
13d7c 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  r;         /* Nu
13d7d 6d 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65 73  mber of messages
13d7e 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72 72   written to zErr
13d7f 4d 73 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  Msg so far */.  
13d80 69 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  int mallocFailed
13d81 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c  ; /* A memory al
13d82 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68  location error h
13d83 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 20  as occurred */. 
13d84 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73 67   StrAccum errMsg
13d85 3b 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65  ;  /* Accumulate
13d86 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
13d87 67 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f 0a  ge text here */.
13d88 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f  };../*.** Read o
13d89 72 20 77 72 69 74 65 20 61 20 74 77 6f 2d 20 61  r write a two- a
13d8a 6e 64 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67  nd four-byte big
13d8b 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
13d8c 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69  values..*/.#defi
13d8d 6e 65 20 67 65 74 32 62 79 74 65 28 78 29 20 20  ne get2byte(x)  
13d8e 20 28 28 78 29 5b 30 5d 3c 3c 38 20 7c 20 28 78   ((x)[0]<<8 | (x
13d8f 29 5b 31 5d 29 0a 23 64 65 66 69 6e 65 20 70 75  )[1]).#define pu
13d90 74 32 62 79 74 65 28 70 2c 76 29 20 28 28 70 29  t2byte(p,v) ((p)
13d91 5b 30 5d 20 3d 20 28 75 38 29 28 28 76 29 3e 3e  [0] = (u8)((v)>>
13d92 38 29 2c 20 28 70 29 5b 31 5d 20 3d 20 28 75 38  8), (p)[1] = (u8
13d93 29 28 76 29 29 0a 23 64 65 66 69 6e 65 20 67 65  )(v)).#define ge
13d94 74 34 62 79 74 65 20 73 71 6c 69 74 65 33 47 65  t4byte sqlite3Ge
13d95 74 34 62 79 74 65 0a 23 64 65 66 69 6e 65 20 70  t4byte.#define p
13d96 75 74 34 62 79 74 65 20 73 71 6c 69 74 65 33 50  ut4byte sqlite3P
13d97 75 74 34 62 79 74 65 0a 0a 2f 2a 0a 2a 2a 20 49  ut4byte../*.** I
13d98 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e 65 73  nternal routines
13d99 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
13d9a 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20  accessed by the 
13d9b 62 74 72 65 65 20 6c 61 79 65 72 20 6f 6e 6c 79  btree layer only
13d9c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
13d9d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
13d9e 74 72 65 65 47 65 74 50 61 67 65 28 42 74 53 68  treeGetPage(BtSh
13d9f 61 72 65 64 2a 2c 20 50 67 6e 6f 2c 20 4d 65 6d  ared*, Pgno, Mem
13da0 50 61 67 65 2a 2a 2c 20 69 6e 74 29 3b 0a 53 51  Page**, int);.SQ
13da1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13da2 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69   sqlite3BtreeIni
13da3 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  tPage(MemPage *p
13da4 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Page);.SQLITE_PR
13da5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
13da6 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
13da7 50 74 72 28 4d 65 6d 50 61 67 65 2a 2c 20 75 38  Ptr(MemPage*, u8
13da8 2a 2c 20 43 65 6c 6c 49 6e 66 6f 2a 29 3b 0a 53  *, CellInfo*);.S
13da9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
13daa 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  id sqlite3BtreeP
13dab 61 72 73 65 43 65 6c 6c 28 4d 65 6d 50 61 67 65  arseCell(MemPage
13dac 2a 2c 20 69 6e 74 2c 20 43 65 6c 6c 49 6e 66 6f  *, int, CellInfo
13dad 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
13dae 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
13daf 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72  reeRestoreCursor
13db0 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f  Position(BtCurso
13db1 72 20 2a 70 43 75 72 29 3b 0a 53 51 4c 49 54 45  r *pCur);.SQLITE
13db2 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
13db3 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 65 6d  lite3BtreeGetTem
13db4 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  pCursor(BtCursor
13db5 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 6f 72   *pCur, BtCursor
13db6 20 2a 70 54 65 6d 70 43 75 72 29 3b 0a 53 51 4c   *pTempCur);.SQL
13db7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
13db8 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 6c   sqlite3BtreeRel
13db9 65 61 73 65 54 65 6d 70 43 75 72 73 6f 72 28 42  easeTempCursor(B
13dba 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a  tCursor *pCur);.
13dbb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
13dbc 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
13dbd 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43  MoveToParent(BtC
13dbe 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a 0a 2f  ursor *pCur);../
13dbf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
13dc0 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68  nd of btreeInt.h
13dc1 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
13dc2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13dc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
13dc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
13dc5 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
13dc6 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62  we left off in b
13dc7 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
13dc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  *************/.#
13dc9 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
13dca 53 41 46 45 20 26 26 20 21 64 65 66 69 6e 65 64  SAFE && !defined
13dcb 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  (SQLITE_OMIT_SHA
13dcc 52 45 44 5f 43 41 43 48 45 29 0a 0a 0a 2f 2a 0a  RED_CACHE).../*.
13dcd 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78  ** Enter a mutex
13dce 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54   on the given BT
13dcf 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  ree object..**.*
13dd0 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20  * If the object 
13dd1 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c  is not sharable,
13dd2 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69   then no mutex i
13dd3 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 0a  s ever required.
13dd4 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  ** and this rout
13dd5 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  ine is a no-op. 
13dd6 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   The underlying 
13dd7 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63  mutex is non-rec
13dd8 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77  ursive..** But w
13dd9 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e  e keep a referen
13dda 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65  ce count in Btre
13ddb 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20  e.wantToLock so 
13ddc 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  the behavior.** 
13ddd 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  of this interfac
13dde 65 20 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a  e is recursive..
13ddf 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64  **.** To avoid d
13de0 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70  eadlocks, multip
13de1 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f  le Btrees are lo
13de2 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  cked in the same
13de3 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c   order.** by all
13de4 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
13de5 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70  tions.  The p->p
13de6 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f  Next is a list o
13de7 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65  f other.** Btree
13de8 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74  s belonging to t
13de9 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
13dea 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74   connection as t
13deb 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68  he p Btree.** wh
13dec 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c  ich need to be l
13ded 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20  ocked after p.  
13dee 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74  If we cannot get
13def 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c   a lock on.** p,
13df0 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f   then first unlo
13df1 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74  ck all of the ot
13df2 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74  hers on p->pNext
13df3 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66  , then wait.** f
13df4 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62  or the lock to b
13df5 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20  ecome available 
13df6 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63  on p, then reloc
13df7 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20  k all of the.** 
13df8 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 65  subsequent Btree
13df9 73 20 74 68 61 74 20 64 65 73 69 72 65 20 61 20  s that desire a 
13dfa 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lock..*/.SQLITE_
13dfb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
13dfc 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42  ite3BtreeEnter(B
13dfd 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65  tree *p){.  Btre
13dfe 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a  e *pLater;..  /*
13dff 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69   Some basic sani
13e00 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74  ty checking on t
13e01 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c  he Btree.  The l
13e02 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20  ist of Btrees.  
13e03 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ** connected by 
13e04 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 20  pNext and pPrev 
13e05 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72  should be in sor
13e06 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a  ted order by.  *
13e07 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75  * Btree.pBt valu
13e08 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20  e. All elements 
13e09 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75  of the list shou
13e0a 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a  ld belong to.  *
13e0b 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65  * the same conne
13e0c 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72  ction. Only shar
13e0d 65 64 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e  ed Btrees are on
13e0e 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20   the list. */.  
13e0f 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74  assert( p->pNext
13e10 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d  ==0 || p->pNext-
13e11 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20  >pBt>p->pBt );. 
13e12 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65   assert( p->pPre
13e13 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76  v==0 || p->pPrev
13e14 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a  ->pBt<p->pBt );.
13e15 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65    assert( p->pNe
13e16 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78  xt==0 || p->pNex
13e17 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a  t->db==p->db );.
13e18 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72    assert( p->pPr
13e19 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65  ev==0 || p->pPre
13e1a 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a  v->db==p->db );.
13e1b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61    assert( p->sha
13e1c 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65  rable || (p->pNe
13e1d 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65  xt==0 && p->pPre
13e1e 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43  v==0) );..  /* C
13e1f 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67  heck for locking
13e20 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a   consistency */.
13e21 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f    assert( !p->lo
13e22 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54  cked || p->wantT
13e23 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73  oLock>0 );.  ass
13e24 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65  ert( p->sharable
13e25 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63   || p->wantToLoc
13e26 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65  k==0 );..  /* We
13e27 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20   should already 
13e28 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  hold a lock on t
13e29 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
13e2a 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65  ection */.  asse
13e2b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
13e2c 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
13e2d 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 28 20 21  tex) );..  if( !
13e2e 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 20 72 65  p->sharable ) re
13e2f 74 75 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54  turn;.  p->wantT
13e30 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70  oLock++;.  if( p
13e31 2d 3e 6c 6f 63 6b 65 64 20 29 20 72 65 74 75 72  ->locked ) retur
13e32 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6d 6f 73 74  n;..  /* In most
13e33 20 63 61 73 65 73 2c 20 77 65 20 73 68 6f 75 6c   cases, we shoul
13e34 64 20 62 65 20 61 62 6c 65 20 74 6f 20 61 63 71  d be able to acq
13e35 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 77 65  uire the lock we
13e36 0a 20 20 2a 2a 20 77 61 6e 74 20 77 69 74 68 6f  .  ** want witho
13e37 75 74 20 68 61 76 69 6e 67 20 74 6f 20 67 6f 20  ut having to go 
13e38 74 68 72 6f 75 67 68 74 20 74 68 65 20 61 73 63  throught the asc
13e39 65 6e 64 69 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a  ending lock.  **
13e3a 20 70 72 6f 63 65 64 75 72 65 20 74 68 61 74 20   procedure that 
13e3b 66 6f 6c 6c 6f 77 73 2e 20 20 4a 75 73 74 20 62  follows.  Just b
13e3c 65 20 73 75 72 65 20 6e 6f 74 20 74 6f 20 62 6c  e sure not to bl
13e3d 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ock..  */.  if( 
13e3e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
13e3f 79 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  y(p->pBt->mutex)
13e40 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13e41 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31     p->locked = 1
13e42 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
13e43 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64  }..  /* To avoid
13e44 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74   deadlock, first
13e45 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63   release all loc
13e46 6b 73 20 77 69 74 68 20 61 20 6c 61 72 67 65 72  ks with a larger
13e47 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61  .  ** BtShared a
13e48 64 64 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63  ddress.  Then ac
13e49 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20  quire our lock. 
13e4a 20 54 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a   Then reacquire.
13e4b 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42    ** the other B
13e4c 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68  tShared locks th
13e4d 61 74 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f  at we used to ho
13e4e 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a  ld in ascending.
13e4f 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f    ** order..  */
13e50 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d  .  for(pLater=p-
13e51 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20  >pNext; pLater; 
13e52 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70  pLater=pLater->p
13e53 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72  Next){.    asser
13e54 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61  t( pLater->shara
13e55 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ble );.    asser
13e56 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74  t( pLater->pNext
13e57 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70  ==0 || pLater->p
13e58 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72  Next->pBt>pLater
13e59 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73  ->pBt );.    ass
13e5a 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f  ert( !pLater->lo
13e5b 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e  cked || pLater->
13e5c 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a  wantToLock>0 );.
13e5d 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e      if( pLater->
13e5e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20  locked ){.      
13e5f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
13e60 61 76 65 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d  ave(pLater->pBt-
13e61 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70  >mutex);.      p
13e62 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20  Later->locked = 
13e63 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  0;.    }.  }.  s
13e64 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
13e65 65 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  er(p->pBt->mutex
13e66 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d  );.  p->locked =
13e67 20 31 3b 0a 20 20 66 6f 72 28 70 4c 61 74 65 72   1;.  for(pLater
13e68 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65  =p->pNext; pLate
13e69 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72  r; pLater=pLater
13e6a 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
13e6b 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f  ( pLater->wantTo
13e6c 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71  Lock ){.      sq
13e6d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
13e6e 72 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d 3e 6d  r(pLater->pBt->m
13e6f 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 4c 61  utex);.      pLa
13e70 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b  ter->locked = 1;
13e71 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
13e72 0a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 65 63  .** Exit the rec
13e73 75 72 73 69 76 65 20 6d 75 74 65 78 20 6f 6e 20  ursive mutex on 
13e74 61 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49  a Btree..*/.SQLI
13e75 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
13e76 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
13e77 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69  e(Btree *p){.  i
13e78 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  f( p->sharable )
13e79 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  {.    assert( p-
13e7a 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b  >wantToLock>0 );
13e7b 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  .    p->wantToLo
13e7c 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d  ck--;.    if( p-
13e7d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29  >wantToLock==0 )
13e7e 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
13e7f 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20  p->locked );.   
13e80 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
13e81 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d  _leave(p->pBt->m
13e82 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 2d 3e  utex);.      p->
13e83 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20  locked = 0;.    
13e84 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66  }.  }.}..#ifndef
13e85 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65   NDEBUG./*.** Re
13e86 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
13e87 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20   BtShared mutex 
13e88 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 62  is held on the b
13e89 74 72 65 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  tree.  .**.** Th
13e8a 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73  is routine makes
13e8b 20 6e 6f 20 64 65 74 65 72 6d 69 6e 61 74 69 6f   no determinatio
13e8c 6e 20 6f 6e 65 20 77 61 79 20 6f 72 20 61 6e 6f  n one way or ano
13e8d 74 68 65 72 20 69 66 20 74 68 65 0a 2a 2a 20 64  ther if the.** d
13e8e 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
13e8f 6f 6e 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  on mutex is held
13e90 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
13e91 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c  tine is used onl
13e92 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 73  y from within as
13e93 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
13e94 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
13e95 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
13e96 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
13e97 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74  Btree *p){.  ret
13e98 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65  urn (p->sharable
13e99 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  ==0 ||.         
13e9a 20 20 20 20 28 70 2d 3e 6c 6f 63 6b 65 64 20 26      (p->locked &
13e9b 26 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20  & p->wantToLock 
13e9c 26 26 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  && sqlite3_mutex
13e9d 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75  _held(p->pBt->mu
13e9e 74 65 78 29 29 29 3b 0a 7d 0a 23 65 6e 64 69 66  tex)));.}.#endif
13e9f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
13ea0 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
13ea1 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20  /*.** Enter and 
13ea2 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20 6f 6e  leave a mutex on
13ea3 20 61 20 42 74 72 65 65 20 67 69 76 65 6e 20 61   a Btree given a
13ea4 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 79   cursor owned by
13ea5 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20   that.** Btree. 
13ea6 20 54 68 65 73 65 20 65 6e 74 72 79 20 70 6f 69   These entry poi
13ea7 6e 74 73 20 61 72 65 20 75 73 65 64 20 62 79 20  nts are used by 
13ea8 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20  incremental I/O 
13ea9 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d  and can be.** om
13eaa 69 74 74 65 64 20 69 66 20 74 68 61 74 20 6d 6f  itted if that mo
13eab 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 64  dule is not used
13eac 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
13ead 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
13eae 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72  BtreeEnterCursor
13eaf 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
13eb0 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  {.  sqlite3Btree
13eb1 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72  Enter(pCur->pBtr
13eb2 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ee);.}.SQLITE_PR
13eb3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
13eb4 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
13eb5 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
13eb6 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  r){.  sqlite3Btr
13eb7 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 42  eeLeave(pCur->pB
13eb8 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  tree);.}.#endif 
13eb9 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  /* SQLITE_OMIT_I
13eba 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a  NCRBLOB */.../*.
13ebb 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74  ** Enter the mut
13ebc 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74 72 65  ex on every Btre
13ebd 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
13ebe 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  h a database.** 
13ebf 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69  connection.  Thi
13ec0 73 20 69 73 20 6e 65 65 64 65 64 20 28 66 6f 72  s is needed (for
13ec1 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 20   example) prior 
13ec2 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20  to parsing.** a 
13ec3 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 20  statement since 
13ec4 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61  we will be compa
13ec5 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 63  ring table and c
13ec6 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61  olumn names.** a
13ec7 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d  gainst all schem
13ec8 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74  as and we do not
13ec9 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63 68 65   want those sche
13eca 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73  mas being.** res
13ecb 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65  et out from unde
13ecc 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  r us..**.** Ther
13ecd 65 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e  e is a correspon
13ece 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70  ding leave-all p
13ecf 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a  rocedures..**.**
13ed0 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78   Enter the mutex
13ed1 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20  es in accending 
13ed2 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 72 65  order by BtShare
13ed3 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65 73  d pointer addres
13ed4 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 68  s.** to avoid th
13ed5 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66  e possibility of
13ed6 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74   deadlock when t
13ed7 77 6f 20 74 68 72 65 61 64 73 20 77 69 74 68 0a  wo threads with.
13ed8 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62  ** two or more b
13ed9 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20  trees in common 
13eda 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b  both try to lock
13edb 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72 65 65   all their btree
13edc 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65  s.** at the same
13edd 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c   instant..*/.SQL
13ede 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
13edf 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
13ee0 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64  erAll(sqlite3 *d
13ee1 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42  b){.  int i;.  B
13ee2 74 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72  tree *p, *pLater
13ee3 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
13ee4 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
13ee5 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  b->mutex) );.  f
13ee6 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
13ee7 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d  b; i++){.    p =
13ee8 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
13ee9 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d  .    if( p && p-
13eea 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
13eeb 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b     p->wantToLock
13eec 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  ++;.      if( !p
13eed 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20  ->locked ){.    
13eee 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77      assert( p->w
13eef 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b 0a  antToLock==1 );.
13ef0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
13ef1 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70 2d  ->pPrev ) p = p-
13ef2 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20 20  >pPrev;.        
13ef3 77 68 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b 65 64  while( p->locked
13ef4 20 26 26 20 70 2d 3e 70 4e 65 78 74 20 29 20 70   && p->pNext ) p
13ef5 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   = p->pNext;.   
13ef6 20 20 20 20 20 66 6f 72 28 70 4c 61 74 65 72 20       for(pLater 
13ef7 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74  = p->pNext; pLat
13ef8 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65  er; pLater=pLate
13ef9 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  r->pNext){.     
13efa 20 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d       if( pLater-
13efb 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20  >locked ){.     
13efc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d         sqlite3_m
13efd 75 74 65 78 5f 6c 65 61 76 65 28 70 4c 61 74 65  utex_leave(pLate
13efe 72 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  r->pBt->mutex);.
13eff 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 61 74              pLat
13f00 65 72 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a  er->locked = 0;.
13f01 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
13f02 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68      }.        wh
13f03 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20  ile( p ){.      
13f04 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
13f05 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 2d 3e  x_enter(p->pBt->
13f06 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20  mutex);.        
13f07 20 20 70 2d 3e 6c 6f 63 6b 65 64 2b 2b 3b 0a 20    p->locked++;. 
13f08 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e           p = p->
13f09 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d  pNext;.        }
13f0a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
13f0b 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56   }.}.SQLITE_PRIV
13f0c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
13f0d 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71  BtreeLeaveAll(sq
13f0e 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
13f0f 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b  t i;.  Btree *p;
13f10 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
13f11 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
13f12 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f  ->mutex) );.  fo
13f13 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
13f14 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20  ; i++){.    p = 
13f15 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
13f16 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e      if( p && p->
13f17 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
13f18 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e    assert( p->wan
13f19 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20  tToLock>0 );.   
13f1a 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b     p->wantToLock
13f1b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  --;.      if( p-
13f1c 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29  >wantToLock==0 )
13f1d 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
13f1e 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20  ( p->locked );. 
13f1f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d         sqlite3_m
13f20 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42  utex_leave(p->pB
13f21 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  t->mutex);.     
13f22 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30     p->locked = 0
13f23 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
13f24 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e    }.}..#ifndef N
13f25 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75  DEBUG./*.** Retu
13f26 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 63  rn true if the c
13f27 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f  urrent thread ho
13f28 6c 64 73 20 74 68 65 20 64 61 74 61 62 61 73 65  lds the database
13f29 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d   connection.** m
13f2a 75 74 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71  utex and all req
13f2b 75 69 72 65 64 20 42 74 53 68 61 72 65 64 20 6d  uired BtShared m
13f2c 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  utexes..**.** Th
13f2d 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
13f2e 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ed inside assert
13f2f 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e  () statements on
13f30 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
13f31 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
13f32 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
13f33 74 65 78 65 73 28 73 71 6c 69 74 65 33 20 2a 64  texes(sqlite3 *d
13f34 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  b){.  int i;.  i
13f35 66 28 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65  f( !sqlite3_mute
13f36 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
13f37 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
13f38 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  0;.  }.  for(i=0
13f39 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
13f3a 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 3b  ){.    Btree *p;
13f3b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62  .    p = db->aDb
13f3c 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
13f3d 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c   p && p->sharabl
13f3e 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 28 70  e &&.         (p
13f3f 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
13f40 7c 7c 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65  || !sqlite3_mute
13f41 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d  x_held(p->pBt->m
13f42 75 74 65 78 29 29 20 29 7b 0a 20 20 20 20 20 20  utex)) ){.      
13f43 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
13f44 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
13f45 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  }.#endif /* NDEB
13f46 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  UG */../*.** Add
13f47 20 61 20 6e 65 77 20 42 74 72 65 65 20 70 6f 69   a new Btree poi
13f48 6e 74 65 72 20 74 6f 20 61 20 42 74 72 65 65 4d  nter to a BtreeM
13f49 75 74 65 78 41 72 72 61 79 2e 20 0a 2a 2a 20 69  utexArray. .** i
13f4a 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 63 61  f the pointer ca
13f4b 6e 20 70 6f 73 73 69 62 6c 79 20 62 65 20 73 68  n possibly be sh
13f4c 61 72 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 6f  ared with.** ano
13f4d 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f  ther database co
13f4e 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nnection..**.** 
13f4f 54 68 65 20 70 6f 69 6e 74 65 72 73 20 61 72 65  The pointers are
13f50 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 65 64 20   kept in sorted 
13f51 6f 72 64 65 72 20 62 79 20 70 42 74 72 65 65 2d  order by pBtree-
13f52 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a 2a 20 77  >pBt.  That.** w
13f53 61 79 20 77 68 65 6e 20 77 65 20 67 6f 20 74 6f  ay when we go to
13f54 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 65 20 6d   enter all the m
13f55 75 74 65 78 65 73 2c 20 77 65 20 63 61 6e 20 65  utexes, we can e
13f56 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 69 6e 20  nter them.** in 
13f57 6f 72 64 65 72 20 77 69 74 68 6f 75 74 20 65 76  order without ev
13f58 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 62 61  ery having to ba
13f59 63 6b 75 70 20 61 6e 64 20 72 65 74 72 79 20 61  ckup and retry a
13f5a 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20 77 6f  nd without.** wo
13f5b 72 72 79 69 6e 67 20 61 62 6f 75 74 20 64 65 61  rrying about dea
13f5c 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  dlock..**.** The
13f5d 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 61 72 65   number of share
13f5e 64 20 62 74 72 65 65 73 20 77 69 6c 6c 20 61 6c  d btrees will al
13f5f 77 61 79 73 20 62 65 20 73 6d 61 6c 6c 20 28 75  ways be small (u
13f60 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 29 0a 2a  sually 0 or 1).*
13f61 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 74 69 6f  * so an insertio
13f62 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 61 64 65  n sort is an ade
13f63 71 75 61 74 65 20 61 6c 67 6f 72 69 74 68 6d 20  quate algorithm 
13f64 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  here..*/.SQLITE_
13f65 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
13f66 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
13f67 72 61 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d  rayInsert(BtreeM
13f68 75 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61  utexArray *pArra
13f69 79 2c 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  y, Btree *pBtree
13f6a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ){.  int i, j;. 
13f6b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a   BtShared *pBt;.
13f6c 20 20 69 66 28 20 70 42 74 72 65 65 3d 3d 30 20    if( pBtree==0 
13f6d 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 61 72 61  || pBtree->shara
13f6e 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ble==0 ) return;
13f6f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
13f70 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    {.    for(i=0;
13f71 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65   i<pArray->nMute
13f72 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61  x; i++){.      a
13f73 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61  ssert( pArray->a
13f74 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65  Btree[i]!=pBtree
13f75 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65   );.    }.  }.#e
13f76 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70  ndif.  assert( p
13f77 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30  Array->nMutex>=0
13f78 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41   );.  assert( pA
13f79 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c 41 72 72  rray->nMutex<Arr
13f7a 61 79 53 69 7a 65 28 70 41 72 72 61 79 2d 3e 61  aySize(pArray->a
13f7b 42 74 72 65 65 29 2d 31 20 29 3b 0a 20 20 70 42  Btree)-1 );.  pB
13f7c 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b  t = pBtree->pBt;
13f7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41  .  for(i=0; i<pA
13f7e 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b  rray->nMutex; i+
13f7f 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
13f80 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69  pArray->aBtree[i
13f81 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20  ]!=pBtree );.   
13f82 20 69 66 28 20 70 41 72 72 61 79 2d 3e 61 42 74   if( pArray->aBt
13f83 72 65 65 5b 69 5d 2d 3e 70 42 74 3e 70 42 74 20  ree[i]->pBt>pBt 
13f84 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70  ){.      for(j=p
13f85 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a  Array->nMutex; j
13f86 3e 69 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20  >i; j--){.      
13f87 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65    pArray->aBtree
13f88 5b 6a 5d 20 3d 20 70 41 72 72 61 79 2d 3e 61 42  [j] = pArray->aB
13f89 74 72 65 65 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20  tree[j-1];.     
13f8a 20 7d 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d   }.      pArray-
13f8b 3e 61 42 74 72 65 65 5b 69 5d 20 3d 20 70 42 74  >aBtree[i] = pBt
13f8c 72 65 65 3b 0a 20 20 20 20 20 20 70 41 72 72 61  ree;.      pArra
13f8d 79 2d 3e 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20  y->nMutex++;.   
13f8e 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
13f8f 0a 20 20 7d 0a 20 20 70 41 72 72 61 79 2d 3e 61  .  }.  pArray->a
13f90 42 74 72 65 65 5b 70 41 72 72 61 79 2d 3e 6e 4d  Btree[pArray->nM
13f91 75 74 65 78 2b 2b 5d 20 3d 20 70 42 74 72 65 65  utex++] = pBtree
13f92 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72  ;.}../*.** Enter
13f93 20 74 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76   the mutex of ev
13f94 65 72 79 20 62 74 72 65 65 20 69 6e 20 74 68 65  ery btree in the
13f95 20 61 72 72 61 79 2e 20 20 54 68 69 73 20 72 6f   array.  This ro
13f96 75 74 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c  utine is.** call
13f97 65 64 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  ed at the beginn
13f98 69 6e 67 20 6f 66 20 73 71 6c 69 74 65 33 56 64  ing of sqlite3Vd
13f99 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 6d  beExec().  The m
13f9a 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 65 78  utexes are.** ex
13f9b 69 74 65 64 20 61 74 20 74 68 65 20 65 6e 64 20  ited at the end 
13f9c 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63  of the same func
13f9d 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
13f9e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
13f9f 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
13fa0 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75  rayEnter(BtreeMu
13fa1 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79  texArray *pArray
13fa2 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
13fa3 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d  r(i=0; i<pArray-
13fa4 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20  >nMutex; i++){. 
13fa5 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41     Btree *p = pA
13fa6 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b  rray->aBtree[i];
13fa7 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73  .    /* Some bas
13fa8 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  ic sanity checki
13fa9 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  ng */.    assert
13faa 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79  ( i==0 || pArray
13fab 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70  ->aBtree[i-1]->p
13fac 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20  Bt<p->pBt );.   
13fad 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63   assert( !p->loc
13fae 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f  ked || p->wantTo
13faf 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f  Lock>0 );..    /
13fb0 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65  * We should alre
13fb1 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20  ady hold a lock 
13fb2 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
13fb3 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
13fb4 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
13fb5 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
13fb6 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
13fb7 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b     p->wantToLock
13fb8 2b 2b 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e  ++;.    if( !p->
13fb9 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 73 68 61  locked && p->sha
13fba 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73  rable ){.      s
13fbb 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
13fbc 65 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  er(p->pBt->mutex
13fbd 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b  );.      p->lock
13fbe 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ed = 1;.    }.  
13fbf 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 61 76 65  }.}../*.** Leave
13fc0 20 74 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76   the mutex of ev
13fc1 65 72 79 20 62 74 72 65 65 20 69 6e 20 74 68 65  ery btree in the
13fc2 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53 51 4c 49 54   group..*/.SQLIT
13fc3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
13fc4 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
13fc5 41 72 72 61 79 4c 65 61 76 65 28 42 74 72 65 65  ArrayLeave(Btree
13fc6 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 72 72  MutexArray *pArr
13fc7 61 79 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ay){.  int i;.  
13fc8 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61  for(i=0; i<pArra
13fc9 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b  y->nMutex; i++){
13fca 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20  .    Btree *p = 
13fcb 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69  pArray->aBtree[i
13fcc 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62  ];.    /* Some b
13fcd 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 63  asic sanity chec
13fce 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73 65  king */.    asse
13fcf 72 74 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72  rt( i==0 || pArr
13fd0 61 79 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d  ay->aBtree[i-1]-
13fd1 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20  >pBt<p->pBt );. 
13fd2 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f     assert( p->lo
13fd3 63 6b 65 64 20 7c 7c 20 21 70 2d 3e 73 68 61 72  cked || !p->shar
13fd4 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65  able );.    asse
13fd5 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  rt( p->wantToLoc
13fd6 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57  k>0 );..    /* W
13fd7 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79  e should already
13fd8 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20   hold a lock on 
13fd9 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
13fda 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61  nection */.    a
13fdb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
13fdc 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
13fdd 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20  >mutex) );..    
13fde 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b  p->wantToLock--;
13fdf 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74  .    if( p->want
13fe0 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d 3e  ToLock==0 && p->
13fe1 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20  locked ){.      
13fe2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
13fe3 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  ave(p->pBt->mute
13fe4 78 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 6f 63  x);.      p->loc
13fe5 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ked = 0;.    }. 
13fe6 20 7d 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 20 2f   }.}...#endif  /
13fe7 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  * SQLITE_THREADS
13fe8 41 46 45 20 26 26 20 21 53 51 4c 49 54 45 5f 4f  AFE && !SQLITE_O
13fe9 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
13fea 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
13feb 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d 75  **** End of btmu
13fec 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tex.c **********
13fed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13fee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13fef 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
13ff0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
13ff1 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  btree.c ********
13ff2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13ff3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13ff4 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
13ff5 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68  April 6.**.** Th
13ff6 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
13ff7 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
13ff8 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
13ff9 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
13ffa 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
13ffb 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
13ffc 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
13ffd 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
13ffe 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
13fff 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
14000 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
14001 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
14002 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
14003 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
14004 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
14005 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
14006 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
14007 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14008 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14009 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1400a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1400b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62  ******.** $Id: b
1400c 74 72 65 65 2e 63 2c 76 20 31 2e 35 38 32 20 32  tree.c,v 1.582 2
1400d 30 30 39 2f 30 33 2f 33 30 20 31 38 3a 35 30 3a  009/03/30 18:50:
1400e 30 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  05 danielk1977 E
1400f 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  xp $.**.** This 
14010 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
14011 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b  a external (disk
14012 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 73 65  -based) database
14013 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a   using BTrees..*
14014 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72  * See the header
14015 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 74 72   comment on "btr
14016 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 64 64  eeInt.h" for add
14017 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
14018 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e  ion..** Includin
14019 67 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20  g a description 
1401a 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61  of file format a
1401b 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 20 6f  nd an overview o
1401c 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a  f operation..*/.
1401d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65  ./*.** The heade
1401e 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70  r string that ap
1401f 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67  pears at the beg
14020 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a  inning of every.
14021 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  ** SQLite databa
14022 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  se..*/.static co
14023 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 48  nst char zMagicH
14024 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 45  eader[] = SQLITE
14025 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a 2f  _FILE_HEADER;../
14026 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 6c  *.** Set this gl
14027 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 6f  obal variable to
14028 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61   1 to enable tra
14029 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 54  cing using the T
1402a 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a  RACE.** macro..*
1402b 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69  /.#if 0.int sqli
1402c 74 65 33 42 74 72 65 65 54 72 61 63 65 3d 30 3b  te3BtreeTrace=0;
1402d 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61    /* True to ena
1402e 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23  ble tracing */.#
1402f 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29   define TRACE(X)
14030 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 65    if(sqlite3Btre
14031 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 58  eTrace){printf X
14032 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b  ;fflush(stdout);
14033 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
14034 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66   TRACE(X).#endif
14035 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
14036 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
14037 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73  ACHE./*.** A lis
14038 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62  t of BtShared ob
14039 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65  jects that are e
1403a 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74  ligible for part
1403b 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20  icipation.** in 
1403c 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54  shared cache.  T
1403d 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73  his variable has
1403e 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69   file scope duri
1403f 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73  ng normal builds
14040 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73  ,.** but the tes
14041 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20  t harness needs 
14042 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20  to access it so 
14043 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61  we make it globa
14044 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62  l for .** test b
14045 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63  uilds..**.** Acc
14046 65 73 73 20 74 6f 20 74 68 69 73 20 76 61 72 69  ess to this vari
14047 61 62 6c 65 20 69 73 20 70 72 6f 74 65 63 74 65  able is protecte
14048 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45  d by SQLITE_MUTE
14049 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 2e  X_STATIC_MASTER.
1404a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1404b 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
1404c 49 56 41 54 45 20 42 74 53 68 61 72 65 64 20 2a  IVATE BtShared *
1404d 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74  SQLITE_WSD sqlit
1404e 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73  e3SharedCacheLis
1404f 74 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61  t = 0;.#else.sta
14050 74 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 51  tic BtShared *SQ
14051 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33  LITE_WSD sqlite3
14052 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20  SharedCacheList 
14053 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64  = 0;.#endif.#end
14054 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
14055 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a  T_SHARED_CACHE *
14056 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
14057 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
14058 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65  CHE./*.** Enable
14059 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20   or disable the 
1405a 73 68 61 72 65 64 20 70 61 67 65 72 20 61 6e 64  shared pager and
1405b 20 73 63 68 65 6d 61 20 66 65 61 74 75 72 65 73   schema features
1405c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1405d 74 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 66 65  tine has no effe
1405e 63 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 20 64  ct on existing d
1405f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14060 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72  ons..** The shar
14061 65 64 20 63 61 63 68 65 20 73 65 74 74 69 6e 67  ed cache setting
14062 20 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 66 75   effects only fu
14063 74 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  ture calls to.**
14064 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2c   sqlite3_open(),
14065 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
14066 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  ), or sqlite3_op
14067 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 53 51 4c 49  en_v2()..*/.SQLI
14068 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
14069 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64  e3_enable_shared
1406a 5f 63 61 63 68 65 28 69 6e 74 20 65 6e 61 62 6c  _cache(int enabl
1406b 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f  e){.  sqlite3Glo
1406c 62 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72 65 64  balConfig.shared
1406d 43 61 63 68 65 45 6e 61 62 6c 65 64 20 3d 20 65  CacheEnabled = e
1406e 6e 61 62 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20  nable;.  return 
1406f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
14070 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77  dif.../*.** Forw
14071 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a  ard declaration.
14072 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68  */.static int ch
14073 65 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69  eckForReadConfli
14074 63 74 73 28 42 74 72 65 65 2a 2c 20 50 67 6e 6f  cts(Btree*, Pgno
14075 2c 20 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 34  , BtCursor*, i64
14076 29 3b 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  );...#ifdef SQLI
14077 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
14078 41 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  ACHE.  /*.  ** T
14079 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 65  he functions que
1407a 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
1407b 6c 65 4c 6f 63 6b 28 29 2c 20 73 65 74 53 68 61  leLock(), setSha
1407c 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
1407d 6b 28 29 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c  k(),.  ** and cl
1407e 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68  earAllSharedCach
1407f 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20  eTableLocks().  
14080 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e  ** manipulate en
14081 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 74 53  tries in the BtS
14082 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b  hared.pLock link
14083 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 6f 20  ed list used to 
14084 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 72 65  store.  ** share
14085 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 6c 65  d-cache table le
14086 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68  vel locks. If th
14087 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d  e library is com
14088 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 20  piled with the. 
14089 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65   ** shared-cache
1408a 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c 65   feature disable
1408b 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73  d, then there is
1408c 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 20 75   only ever one u
1408d 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68  ser.  ** of each
1408e 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74   BtShared struct
1408f 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 73 20  ure and so this 
14090 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e  locking is not n
14091 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20  ecessary. .  ** 
14092 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f  So define the lo
14093 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e 63 74  ck related funct
14094 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a  ions as no-ops..
14095 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 71    */.  #define q
14096 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54  uerySharedCacheT
14097 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20  ableLock(a,b,c) 
14098 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66  SQLITE_OK.  #def
14099 69 6e 65 20 73 65 74 53 68 61 72 65 64 43 61 63  ine setSharedCac
1409a 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c  heTableLock(a,b,
1409b 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23  c) SQLITE_OK.  #
1409c 64 65 66 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53  define clearAllS
1409d 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
1409e 6f 63 6b 73 28 61 29 0a 23 65 6e 64 69 66 0a 0a  ocks(a).#endif..
1409f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
140a0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
140a1 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20  ./*.** Query to 
140a2 73 65 65 20 69 66 20 62 74 72 65 65 20 68 61 6e  see if btree han
140a3 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 61 69 6e  dle p may obtain
140a4 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20   a lock of type 
140a5 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f  eLock .** (READ_
140a6 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f  LOCK or WRITE_LO
140a7 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  CK) on the table
140a8 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20   with root-page 
140a9 69 54 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20  iTab. Return.** 
140aa 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65  SQLITE_OK if the
140ab 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 74   lock may be obt
140ac 61 69 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e  ained (by callin
140ad 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 64 43 61  g.** setSharedCa
140ae 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c  cheTableLock()),
140af 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   or SQLITE_LOCKE
140b0 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61  D if not..*/.sta
140b1 74 69 63 20 69 6e 74 20 71 75 65 72 79 53 68 61  tic int querySha
140b2 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
140b3 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  k(Btree *p, Pgno
140b4 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29   iTab, u8 eLock)
140b5 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
140b6 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74  t = p->pBt;.  Bt
140b7 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20  Lock *pIter;..  
140b8 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
140b9 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
140ba 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65  ) );.  assert( e
140bb 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20  Lock==READ_LOCK 
140bc 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  || eLock==WRITE_
140bd 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
140be 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20  ( p->db!=0 );.  
140bf 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20  .  /* This is a 
140c0 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61  no-op if the sha
140c1 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74  red-cache is not
140c2 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66   enabled */.  if
140c3 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  ( !p->sharable )
140c4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
140c5 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f  ITE_OK;.  }..  /
140c6 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  * If some other 
140c7 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f  connection is ho
140c8 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69  lding an exclusi
140c9 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a  ve lock, the.  *
140ca 2a 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  * requested lock
140cb 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61   may not be obta
140cc 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ined..  */.  if(
140cd 20 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70   pBt->pWriter!=p
140ce 20 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75   && pBt->isExclu
140cf 73 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  sive ){.    sqli
140d0 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
140d1 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d  cked(p->db, pBt-
140d2 3e 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20  >pWriter->db);. 
140d3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
140d4 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41  _LOCKED_SHAREDCA
140d5 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  CHE;.  }..  /* T
140d6 68 69 73 20 28 61 6c 6f 6e 67 20 77 69 74 68 20  his (along with 
140d7 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61  setSharedCacheTa
140d8 62 6c 65 4c 6f 63 6b 28 29 29 20 69 73 20 77 68  bleLock()) is wh
140d9 65 72 65 0a 20 20 2a 2a 20 74 68 65 20 52 65 61  ere.  ** the Rea
140da 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61  dUncommitted fla
140db 67 20 69 73 20 64 65 61 6c 74 20 77 69 74 68 2e  g is dealt with.
140dc 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 63 61 6c  .  ** If the cal
140dd 6c 65 72 20 69 73 20 71 75 65 72 79 69 6e 67 20  ler is querying 
140de 66 6f 72 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  for a read-lock 
140df 6f 6e 20 61 6e 79 20 74 61 62 6c 65 0a 20 20 2a  on any table.  *
140e0 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65  * other than the
140e1 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
140e2 61 62 6c 65 20 28 74 61 62 6c 65 20 31 29 20 61  able (table 1) a
140e3 6e 64 20 69 66 20 74 68 65 20 52 65 61 64 55 6e  nd if the ReadUn
140e4 63 6f 6d 6d 69 74 74 65 64 0a 20 20 2a 2a 20 66  committed.  ** f
140e5 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e  lag is set, then
140e6 20 74 68 65 20 6c 6f 63 6b 20 67 72 61 6e 74 65   the lock grante
140e7 64 20 65 76 65 6e 20 69 66 20 74 68 65 72 65 20  d even if there 
140e8 61 72 65 20 77 72 69 74 65 2d 6c 6f 63 6b 73 0a  are write-locks.
140e9 20 20 2a 2a 20 6f 6e 20 74 68 65 20 74 61 62 6c    ** on the tabl
140ea 65 2e 20 49 66 20 61 20 77 72 69 74 65 2d 6c 6f  e. If a write-lo
140eb 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ck is requested,
140ec 20 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69   the ReadUncommi
140ed 74 74 65 64 20 66 6c 61 67 0a 20 20 2a 2a 20 69  tted flag.  ** i
140ee 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64  s not considered
140ef 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 66  ..  **.  ** In f
140f0 75 6e 63 74 69 6f 6e 20 73 65 74 53 68 61 72 65  unction setShare
140f1 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
140f2 29 2c 20 69 66 20 61 20 72 65 61 64 2d 6c 6f 63  ), if a read-loc
140f3 6b 20 69 73 20 64 65 6d 61 6e 64 65 64 20 61 6e  k is demanded an
140f4 64 20 74 68 65 20 0a 20 20 2a 2a 20 52 65 61 64  d the .  ** Read
140f5 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67  Uncommitted flag
140f6 20 69 73 20 73 65 74 2c 20 6e 6f 20 65 6e 74 72   is set, no entr
140f7 79 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  y is added to th
140f8 65 20 6c 6f 63 6b 73 20 6c 69 73 74 20 0a 20 20  e locks list .  
140f9 2a 2a 20 28 42 74 53 68 61 72 65 64 2e 70 4c 6f  ** (BtShared.pLo
140fa 63 6b 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ck)..  **.  ** T
140fb 6f 20 73 75 6d 6d 61 72 69 7a 65 3a 20 49 66 20  o summarize: If 
140fc 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74  the ReadUncommit
140fd 74 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 2c  ted flag is set,
140fe 20 74 68 65 6e 20 72 65 61 64 20 63 75 72 73 6f   then read curso
140ff 72 73 0a 20 20 2a 2a 20 6f 6e 20 6e 6f 6e 2d 73  rs.  ** on non-s
14100 63 68 65 6d 61 20 74 61 62 6c 65 73 20 64 6f 20  chema tables do 
14101 6e 6f 74 20 63 72 65 61 74 65 20 6f 72 20 72 65  not create or re
14102 73 70 65 63 74 20 74 61 62 6c 65 20 6c 6f 63 6b  spect table lock
14103 73 2e 20 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 20  s. The locking. 
14104 20 2a 2a 20 70 72 6f 63 65 64 75 72 65 20 66 6f   ** procedure fo
14105 72 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72  r a write-cursor
14106 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65   does not change
14107 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20 20  ..  */.  if( .  
14108 20 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61    0==(p->db->fla
14109 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e  gs&SQLITE_ReadUn
1410a 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20 0a 20  committed) || . 
1410b 20 20 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f     eLock==WRITE_
1410c 4c 4f 43 4b 20 7c 7c 0a 20 20 20 20 69 54 61 62  LOCK ||.    iTab
1410d 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a 20 20  ==MASTER_ROOT.  
1410e 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72  ){.    for(pIter
1410f 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74  =pBt->pLock; pIt
14110 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d  er; pIter=pIter-
14111 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69  >pNext){.      i
14112 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65  f( pIter->pBtree
14113 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 54  !=p && pIter->iT
14114 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 0a 20  able==iTab && . 
14115 20 20 20 20 20 20 20 20 20 28 70 49 74 65 72 2d           (pIter-
14116 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20 7c 7c  >eLock!=eLock ||
14117 20 65 4c 6f 63 6b 21 3d 52 45 41 44 5f 4c 4f 43   eLock!=READ_LOC
14118 4b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  K) ){.        sq
14119 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
1411a 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49  locked(p->db, pI
1411b 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29  ter->pBtree->db)
1411c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 4c  ;.        if( eL
1411d 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20  ock==WRITE_LOCK 
1411e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  ){.          ass
1411f 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72  ert( p==pBt->pWr
14120 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20  iter );.        
14121 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67    pBt->isPending
14122 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
14123 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
14124 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41  QLITE_LOCKED_SHA
14125 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 20 20  REDCACHE;.      
14126 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
14127 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
14128 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
14129 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1412a 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
1412b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
1412c 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
1412d 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74   Add a lock on t
1412e 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  he table with ro
1412f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74  ot-page iTable t
14130 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  o the shared-btr
14131 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74  ee used.** by Bt
14132 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61  ree handle p. Pa
14133 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75  rameter eLock mu
14134 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41  st be either REA
14135 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52  D_LOCK or .** WR
14136 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  ITE_LOCK..**.** 
14137 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
14138 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63  urned if the loc
14139 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65  k is added succe
1413a 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f  ssfully. SQLITE_
1413b 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49  BUSY and.** SQLI
1413c 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73  TE_NOMEM may als
1413d 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a  o be returned..*
1413e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74  /.static int set
1413f 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
14140 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50  Lock(Btree *p, P
14141 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65  gno iTable, u8 e
14142 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65  Lock){.  BtShare
14143 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
14144 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b  .  BtLock *pLock
14145 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a   = 0;.  BtLock *
14146 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  pIter;..  assert
14147 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
14148 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
14149 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d   assert( eLock==
1414a 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f  READ_LOCK || eLo
1414b 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  ck==WRITE_LOCK )
1414c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  ;.  assert( p->d
1414d 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  b!=0 );..  /* Th
1414e 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  is is a no-op if
1414f 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
14150 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64  e is not enabled
14151 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68   */.  if( !p->sh
14152 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65  arable ){.    re
14153 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
14154 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 53    }..  assert( S
14155 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53  QLITE_OK==queryS
14156 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
14157 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65  ock(p, iTable, e
14158 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  Lock) );..  /* I
14159 66 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d  f the read-uncom
1415a 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 20 73  mitted flag is s
1415b 65 74 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f  et and a read-lo
1415c 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 20  ck is requested 
1415d 6f 6e 0a 20 20 2a 2a 20 61 20 6e 6f 6e 2d 73 63  on.  ** a non-sc
1415e 68 65 6d 61 20 74 61 62 6c 65 2c 20 74 68 65 6e  hema table, then
1415f 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 6c 77   the lock is alw
14160 61 79 73 20 67 72 61 6e 74 65 64 2e 20 20 52 65  ays granted.  Re
14161 74 75 72 6e 20 65 61 72 6c 79 0a 20 20 2a 2a 20  turn early.  ** 
14162 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20 61  without adding a
14163 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 42  n entry to the B
14164 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69  tShared.pLock li
14165 73 74 2e 20 53 65 65 0a 20 20 2a 2a 20 63 6f 6d  st. See.  ** com
14166 6d 65 6e 74 20 69 6e 20 66 75 6e 63 74 69 6f 6e  ment in function
14167 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68   querySharedCach
14168 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66 6f 72  eTableLock() for
14169 20 6d 6f 72 65 20 69 6e 66 6f 0a 20 20 2a 2a 20   more info.  ** 
1416a 6f 6e 20 68 61 6e 64 6c 69 6e 67 20 74 68 65 20  on handling the 
1416b 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
1416c 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  flag..  */.  if(
1416d 20 0a 20 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c   .    (p->db->fl
1416e 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55  ags&SQLITE_ReadU
1416f 6e 63 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a  ncommitted) && .
14170 20 20 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44      (eLock==READ
14171 5f 4c 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54  _LOCK) &&.    iT
14172 61 62 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f  able!=MASTER_ROO
14173 54 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  T.  ){.    retur
14174 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
14175 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61  ..  /* First sea
14176 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72  rch the list for
14177 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63   an existing loc
14178 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  k on this table.
14179 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d   */.  for(pIter=
1417a 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65  pBt->pLock; pIte
1417b 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e  r; pIter=pIter->
1417c 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
1417d 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69  pIter->iTable==i
1417e 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e  Table && pIter->
1417f 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20  pBtree==p ){.   
14180 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72     pLock = pIter
14181 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
14182 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
14183 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 61 72  f the above sear
14184 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20  ch did not find 
14185 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20  a BtLock struct 
14186 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65  associating Btre
14187 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61  e p.  ** with ta
14188 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f  ble iTable, allo
14189 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e  cate one and lin
1418a 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69  k it into the li
1418b 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  st..  */.  if( !
1418c 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f  pLock ){.    pLo
1418d 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73  ck = (BtLock *)s
1418e 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
1418f 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29  (sizeof(BtLock))
14190 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b  ;.    if( !pLock
14191 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
14192 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
14193 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e     }.    pLock->
14194 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b  iTable = iTable;
14195 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72  .    pLock->pBtr
14196 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63  ee = p;.    pLoc
14197 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e  k->pNext = pBt->
14198 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e  pLock;.    pBt->
14199 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20  pLock = pLock;. 
1419a 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
1419b 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61   BtLock.eLock va
1419c 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61  riable to the ma
1419d 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72  ximum of the cur
1419e 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61  rent lock.  ** a
1419f 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 65 64  nd the requested
141a0 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e   lock. This mean
141a1 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63  s if a write-loc
141a2 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 68 65  k was already he
141a3 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65  ld.  ** and a re
141a4 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65  ad-lock requeste
141a5 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f  d, we don't inco
141a6 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64  rrectly downgrad
141a7 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f  e the lock..  */
141a8 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 54 45  .  assert( WRITE
141a9 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20  _LOCK>READ_LOCK 
141aa 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70  );.  if( eLock>p
141ab 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20  Lock->eLock ){. 
141ac 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20     pLock->eLock 
141ad 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20  = eLock;.  }..  
141ae 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
141af 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
141b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
141b1 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e  D_CACHE */..#ifn
141b2 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
141b3 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
141b4 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74  ** Release all t
141b5 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28  he table locks (
141b6 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76  locks obtained v
141b7 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74  ia calls to.** t
141b8 68 65 20 73 65 74 53 68 61 72 65 64 43 61 63 68  he setSharedCach
141b9 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f  eTableLock() pro
141ba 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20  cedure) held by 
141bb 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a  Btree handle p..
141bc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
141bd 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63  learAllSharedCac
141be 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72  heTableLocks(Btr
141bf 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72  ee *p){.  BtShar
141c0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
141c1 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49  ;.  BtLock **ppI
141c2 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63  ter = &pBt->pLoc
141c3 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  k;..  assert( sq
141c4 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
141c5 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73  utex(p) );.  ass
141c6 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65  ert( p->sharable
141c7 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 29   || 0==*ppIter )
141c8 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 70 70 49  ;..  while( *ppI
141c9 74 65 72 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63  ter ){.    BtLoc
141ca 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 74  k *pLock = *ppIt
141cb 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  er;.    assert( 
141cc 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65  pBt->isExclusive
141cd 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69  ==0 || pBt->pWri
141ce 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 74 72  ter==pLock->pBtr
141cf 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  ee );.    if( pL
141d0 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29  ock->pBtree==p )
141d1 7b 0a 20 20 20 20 20 20 2a 70 70 49 74 65 72 20  {.      *ppIter 
141d2 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a  = pLock->pNext;.
141d3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
141d4 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d  ee(pLock);.    }
141d5 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 70 49 74  else{.      ppIt
141d6 65 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e 70 4e 65  er = &pLock->pNe
141d7 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  xt;.    }.  }.. 
141d8 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73   assert( pBt->is
141d9 50 65 6e 64 69 6e 67 3d 3d 30 20 7c 7c 20 70 42  Pending==0 || pB
141da 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20  t->pWriter );.  
141db 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72  if( pBt->pWriter
141dc 3d 3d 70 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e  ==p ){.    pBt->
141dd 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20  pWriter = 0;.   
141de 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76   pBt->isExclusiv
141df 65 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e  e = 0;.    pBt->
141e0 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20  isPending = 0;. 
141e1 20 7d 65 6c 73 65 20 69 66 28 20 70 42 74 2d 3e   }else if( pBt->
141e2 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 32 20  nTransaction==2 
141e3 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66  ){.    /* This f
141e4 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
141e5 64 20 77 68 65 6e 20 63 6f 6e 6e 65 63 74 69 6f  d when connectio
141e6 6e 20 70 20 69 73 20 63 6f 6e 63 6c 75 64 69 6e  n p is concludin
141e7 67 20 69 74 73 20 0a 20 20 20 20 2a 2a 20 74 72  g its .    ** tr
141e8 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 74 68  ansaction. If th
141e9 65 72 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78  ere currently ex
141ea 69 73 74 73 20 61 20 77 72 69 74 65 72 2c 20 61  ists a writer, a
141eb 6e 64 20 70 20 69 73 20 6e 6f 74 0a 20 20 20 20  nd p is not.    
141ec 2a 2a 20 74 68 61 74 20 77 72 69 74 65 72 2c 20  ** that writer, 
141ed 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  then the number 
141ee 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79  of locks held by
141ef 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 74 68   connections oth
141f0 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 74  er.    ** than t
141f1 68 65 20 77 72 69 74 65 72 20 6d 75 73 74 20 62  he writer must b
141f2 65 20 61 62 6f 75 74 20 74 6f 20 64 72 6f 70 20  e about to drop 
141f3 74 6f 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73  to zero. In this
141f4 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 73 65 74   case.    ** set
141f5 20 74 68 65 20 69 73 50 65 6e 64 69 6e 67 20 66   the isPending f
141f6 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20 20 2a 2a  lag to 0..    **
141f7 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65  .    ** If there
141f8 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
141f9 79 20 61 20 77 72 69 74 65 72 2c 20 74 68 65 6e  y a writer, then
141fa 20 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e 64   BtShared.isPend
141fb 69 6e 67 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20  ing must.    ** 
141fc 62 65 20 7a 65 72 6f 20 61 6c 72 65 61 64 79 2e  be zero already.
141fd 20 53 6f 20 74 68 69 73 20 6e 65 78 74 20 6c 69   So this next li
141fe 6e 65 20 69 73 20 68 61 72 6d 6c 65 73 73 20 69  ne is harmless i
141ff 6e 20 74 68 61 74 20 63 61 73 65 2e 0a 20 20 20  n that case..   
14200 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 69 73 50   */.    pBt->isP
14201 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 7d 0a  ending = 0;.  }.
14202 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
14203 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
14204 41 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20  ACHE */..static 
14205 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65  void releasePage
14206 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29  (MemPage *pPage)
14207 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  ;  /* Forward re
14208 66 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a  ference */../*.*
14209 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
1420a 65 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 61  e cursor holds a
1420b 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74   mutex on the Bt
1420c 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 6e 64 65  Shared.*/.#ifnde
1420d 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20  f NDEBUG.static 
1420e 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  int cursorHoldsM
1420f 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70  utex(BtCursor *p
14210 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
14211 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14212 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d  ->pBt->mutex);.}
14213 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65  .#endif...#ifnde
14214 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
14215 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76  CRBLOB./*.** Inv
14216 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72  alidate the over
14217 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63  flow page-list c
14218 61 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 20  ache for cursor 
14219 70 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f  pCur, if any..*/
1421a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76  .static void inv
1421b 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43  alidateOverflowC
1421c 61 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a 70  ache(BtCursor *p
1421d 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Cur){.  assert( 
1421e 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1421f 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69  (pCur) );.  sqli
14220 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61  te3_free(pCur->a
14221 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75  Overflow);.  pCu
14222 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30  r->aOverflow = 0
14223 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c  ;.}../*.** Inval
14224 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c  idate the overfl
14225 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63  ow page-list cac
14226 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f  he for all curso
14227 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20  rs opened.** on 
14228 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65  the shared btree
14229 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a   structure pBt..
1422a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
1422b 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72  nvalidateAllOver
1422c 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 72  flowCache(BtShar
1422d 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75  ed *pBt){.  BtCu
1422e 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72  rsor *p;.  asser
1422f 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14230 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
14231 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74  ) );.  for(p=pBt
14232 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d  ->pCursor; p; p=
14233 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  p->pNext){.    i
14234 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f  nvalidateOverflo
14235 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d  wCache(p);.  }.}
14236 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
14237 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66   invalidateOverf
14238 6c 6f 77 43 61 63 68 65 28 78 29 0a 20 20 23 64  lowCache(x).  #d
14239 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65  efine invalidate
1423a 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
1423b 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (x).#endif../*.*
1423c 2a 20 53 65 74 20 62 69 74 20 70 67 6e 6f 20 6f  * Set bit pgno o
1423d 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70  f the BtShared.p
1423e 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65  HasContent bitve
1423f 63 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  c. This is calle
14240 64 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70 61 67  d .** when a pag
14241 65 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 6c  e that previousl
14242 79 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 74 61  y contained data
14243 20 62 65 63 6f 6d 65 73 20 61 20 66 72 65 65 2d   becomes a free-
14244 6c 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20 70 61  list leaf .** pa
14245 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 74  ge..**.** The Bt
14246 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65  Shared.pHasConte
14247 6e 74 20 62 69 74 76 65 63 20 65 78 69 73 74 73  nt bitvec exists
14248 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20   to work around 
14249 61 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20 62 75  an obscure.** bu
1424a 67 20 63 61 75 73 65 64 20 62 79 20 74 68 65 20  g caused by the 
1424b 69 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66 20 74  interaction of t
1424c 77 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f 70 74  wo useful IO opt
1424d 69 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72 72 6f  imizations surro
1424e 75 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65 2d 6c  unding.** free-l
1424f 69 73 74 20 6c 65 61 66 20 70 61 67 65 73 3a 0a  ist leaf pages:.
14250 2a 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 6e 20  **.**   1) When 
14251 61 6c 6c 20 64 61 74 61 20 69 73 20 64 65 6c 65  all data is dele
14252 74 65 64 20 66 72 6f 6d 20 61 20 70 61 67 65 20  ted from a page 
14253 61 6e 64 20 74 68 65 20 70 61 67 65 20 62 65 63  and the page bec
14254 6f 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61 20 66  omes.**      a f
14255 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61  ree-list leaf pa
14256 67 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 20  ge, the page is 
14257 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74  not written to t
14258 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20  he database.**  
14259 20 20 20 20 28 61 73 20 66 72 65 65 2d 6c 69 73      (as free-lis
1425a 74 20 6c 65 61 66 20 70 61 67 65 73 20 63 6f 6e  t leaf pages con
1425b 74 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 66  tain no meaningf
1425c 75 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65 74 69  ul data). Someti
1425d 6d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75 63 68  mes.**      such
1425e 20 61 20 70 61 67 65 20 69 73 20 6e 6f 74 20 65   a page is not e
1425f 76 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 28  ven journalled (
14260 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62  as it will not b
14261 65 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20  e modified,.**  
14262 20 20 20 20 77 68 79 20 62 6f 74 68 65 72 20 6a      why bother j
14263 6f 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 2e  ournalling it?).
14264 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e  .**.**   2) When
14265 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61   a free-list lea
14266 66 20 70 61 67 65 20 69 73 20 72 65 75 73 65 64  f page is reused
14267 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 69 73  , its content is
14268 20 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20 20 20   not read.**    
14269 20 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62    from the datab
1426a 61 73 65 20 6f 72 20 77 72 69 74 74 65 6e 20 74  ase or written t
1426b 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
1426c 6c 65 20 28 77 68 79 20 73 68 6f 75 6c 64 20 69  le (why should i
1426d 74 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 69 66  t.**      be, if
1426e 20 69 74 20 69 73 20 6e 6f 74 20 61 74 20 61 6c   it is not at al
1426f 6c 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0a  l meaningful?)..
14270 2a 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73 65 6c  **.** By themsel
14271 76 65 73 2c 20 74 68 65 73 65 20 6f 70 74 69 6d  ves, these optim
14272 69 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 66 69  izations work fi
14273 6e 65 20 61 6e 64 20 70 72 6f 76 69 64 65 20 61  ne and provide a
14274 20 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66 6f 72   handy.** perfor
14275 6d 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f 20 62  mance boost to b
14276 75 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20 69 6e  ulk delete or in
14277 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e  sert operations.
14278 20 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a 2a 20   However, if.** 
14279 61 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 20  a page is moved 
1427a 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  to the free-list
1427b 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64   and then reused
1427c 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
1427d 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  .** transaction,
1427e 20 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 73   a problem comes
1427f 20 75 70 2e 20 49 66 20 74 68 65 20 70 61 67 65   up. If the page
14280 20 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c   is not journall
14281 65 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20 69 73  ed when.** it is
14282 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72   moved to the fr
14283 65 65 2d 6c 69 73 74 20 61 6e 64 20 69 74 20 69  ee-list and it i
14284 73 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 6e  s also not journ
14285 61 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a 2a 2a  alled when it.**
14286 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72   is extracted fr
14287 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  om the free-list
14288 20 61 6e 64 20 72 65 75 73 65 64 2c 20 74 68 65   and reused, the
14289 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  n the original d
1428a 61 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f  ata.** may be lo
1428b 73 74 2e 20 49 6e 20 74 68 65 20 65 76 65 6e 74  st. In the event
1428c 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20   of a rollback, 
1428d 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 6f  it may not be po
1428e 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 73  ssible.** to res
1428f 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73  tore the databas
14290 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61  e to its origina
14291 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e  l configuration.
14292 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 74  .**.** The solut
14293 69 6f 6e 20 69 73 20 74 68 65 20 42 74 53 68 61  ion is the BtSha
14294 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20  red.pHasContent 
14295 62 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72  bitvec. Whenever
14296 20 61 20 70 61 67 65 20 69 73 20 0a 2a 2a 20 6d   a page is .** m
14297 6f 76 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 61  oved to become a
14298 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
14299 70 61 67 65 2c 20 74 68 65 20 63 6f 72 72 65 73  page, the corres
1429a 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 0a 2a  ponding bit is.*
1429b 2a 20 73 65 74 20 69 6e 20 74 68 65 20 62 69 74  * set in the bit
1429c 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20  vec. Whenever a 
1429d 6c 65 61 66 20 70 61 67 65 20 69 73 20 65 78 74  leaf page is ext
1429e 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20  racted from the 
1429f 66 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f 70  free-list,.** op
142a0 74 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f  timization 2 abo
142a1 76 65 20 69 73 20 6f 6d 6d 69 74 74 65 64 20 69  ve is ommitted i
142a2 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  f the correspond
142a3 69 6e 67 20 62 69 74 20 69 73 20 61 6c 72 65 61  ing bit is alrea
142a4 64 79 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74 53  dy.** set in BtS
142a5 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e  hared.pHasConten
142a6 74 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20  t. The contents 
142a7 6f 66 20 74 68 65 20 62 69 74 76 65 63 20 61 72  of the bitvec ar
142a8 65 20 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74 20  e cleared.** at 
142a9 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79  the end of every
142aa 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f   transaction..*/
142ab 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
142ac 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42  eSetHasContent(B
142ad 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67  tShared *pBt, Pg
142ae 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20  no pgno){.  int 
142af 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
142b0 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73    if( !pBt->pHas
142b1 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 69  Content ){.    i
142b2 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 72 63  nt nPage;.    rc
142b3 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50   = sqlite3PagerP
142b4 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  agecount(pBt->pP
142b5 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20  ager, &nPage);. 
142b6 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
142b7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 42  E_OK ){.      pB
142b8 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d  t->pHasContent =
142b9 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72   sqlite3BitvecCr
142ba 65 61 74 65 28 28 75 33 32 29 6e 50 61 67 65 29  eate((u32)nPage)
142bb 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 42 74  ;.      if( !pBt
142bc 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 29 7b  ->pHasContent ){
142bd 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
142be 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
142bf 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
142c0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
142c1 4b 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74  K && pgno<=sqlit
142c2 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 74  e3BitvecSize(pBt
142c3 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29  ->pHasContent) )
142c4 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
142c5 65 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d  e3BitvecSet(pBt-
142c6 3e 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67  >pHasContent, pg
142c7 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  no);.  }.  retur
142c8 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  n rc;.}../*.** Q
142c9 75 65 72 79 20 74 68 65 20 42 74 53 68 61 72 65  uery the BtShare
142ca 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 65  d.pHasContent ve
142cb 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ctor..**.** This
142cc 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
142cd 6c 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 2d  led when a free-
142ce 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69  list leaf page i
142cf 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  s removed from t
142d0 68 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20  he.** free-list 
142d1 66 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 65  for reuse. It re
142d2 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 69  turns false if i
142d3 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 74  t is safe to ret
142d4 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 67  rieve the.** pag
142d5 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72  e from the pager
142d6 20 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20   layer with the 
142d7 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61  'no-content' fla
142d8 67 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 65  g set. True othe
142d9 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  rwise..*/.static
142da 20 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 73   int btreeGetHas
142db 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64  Content(BtShared
142dc 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f   *pBt, Pgno pgno
142dd 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 3d  ){.  Bitvec *p =
142de 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e   pBt->pHasConten
142df 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26  t;.  return (p &
142e0 26 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42  & (pgno>sqlite3B
142e1 69 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c 20  itvecSize(p) || 
142e2 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
142e3 74 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a  t(p, pgno)));.}.
142e4 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65  ./*.** Clear (de
142e5 73 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 61  stroy) the BtSha
142e6 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20  red.pHasContent 
142e7 62 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 6f  bitvec. This sho
142e8 75 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65  uld be.** invoke
142e9 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73  d at the conclus
142ea 69 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 74  ion of each writ
142eb 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  e-transaction..*
142ec 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74  /.static void bt
142ed 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65  reeClearHasConte
142ee 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  nt(BtShared *pBt
142ef 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76  ){.  sqlite3Bitv
142f0 65 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e 70  ecDestroy(pBt->p
142f1 48 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70  HasContent);.  p
142f2 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20  Bt->pHasContent 
142f3 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61  = 0;.}../*.** Sa
142f4 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ve the current c
142f5 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69  ursor position i
142f6 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20  n the variables 
142f7 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a  BtCursor.nKey .*
142f8 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70  * and BtCursor.p
142f9 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27  Key. The cursor'
142fa 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74  s state is set t
142fb 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  o CURSOR_REQUIRE
142fc 53 45 45 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  SEEK..*/.static 
142fd 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f  int saveCursorPo
142fe 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20  sition(BtCursor 
142ff 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63  *pCur){.  int rc
14300 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 52  ;..  assert( CUR
14301 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d  SOR_VALID==pCur-
14302 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73  >eState );.  ass
14303 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b  ert( 0==pCur->pK
14304 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ey );.  assert( 
14305 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
14306 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 20  (pCur) );..  rc 
14307 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
14308 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 75  ySize(pCur, &pCu
14309 72 2d 3e 6e 4b 65 79 29 3b 0a 0a 20 20 2f 2a 20  r->nKey);..  /* 
1430a 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e  If this is an in
1430b 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e  tKey table, then
1430c 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c 6c 20   the above call 
1430d 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a 65 28  to BtreeKeySize(
1430e 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68  ).  ** stores th
1430f 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69 6e  e integer key in
14310 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20   pCur->nKey. In 
14311 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20 76  this case this v
14312 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c  alue is.  ** all
14313 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65   that is require
14314 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  d. Otherwise, if
14315 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 65   pCur is not ope
14316 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20  n on an intKey. 
14317 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e 20   ** table, then 
14318 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f 72  malloc space for
14319 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 70   and store the p
1431a 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20  Cur->nKey bytes 
1431b 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74  of key .  ** dat
1431c 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  a..  */.  if( rc
1431d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30  ==SQLITE_OK && 0
1431e 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  ==pCur->apPage[0
1431f 5d 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20 20 20  ]->intKey){.    
14320 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c  void *pKey = sql
14321 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74  ite3Malloc( (int
14322 29 70 43 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20  )pCur->nKey );. 
14323 20 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20     if( pKey ){. 
14324 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
14325 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20  3BtreeKey(pCur, 
14326 30 2c 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b  0, (int)pCur->nK
14327 65 79 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 20  ey, pKey);.     
14328 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
14329 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  OK ){.        pC
1432a 75 72 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b  ur->pKey = pKey;
1432b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1432c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1432d 65 65 28 70 4b 65 79 29 3b 0a 20 20 20 20 20 20  ee(pKey);.      
1432e 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
1432f 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
14330 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  OMEM;.    }.  }.
14331 20 20 61 73 73 65 72 74 28 20 21 70 43 75 72 2d    assert( !pCur-
14332 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b  >apPage[0]->intK
14333 65 79 20 7c 7c 20 21 70 43 75 72 2d 3e 70 4b 65  ey || !pCur->pKe
14334 79 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  y );..  if( rc==
14335 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
14336 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
14337 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50  i=0; i<=pCur->iP
14338 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  age; i++){.     
14339 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75   releasePage(pCu
1433a 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20  r->apPage[i]);. 
1433b 20 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67       pCur->apPag
1433c 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  e[i] = 0;.    }.
1433d 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20      pCur->iPage 
1433e 3d 20 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e  = -1;.    pCur->
1433f 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
14340 52 45 51 55 49 52 45 53 45 45 4b 3b 0a 20 20 7d  REQUIRESEEK;.  }
14341 0a 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76  ..  invalidateOv
14342 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72  erflowCache(pCur
14343 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
14344 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68  }../*.** Save th
14345 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61  e positions of a
14346 6c 6c 20 63 75 72 73 6f 72 73 20 65 78 63 65 70  ll cursors excep
14347 74 20 70 45 78 63 65 70 74 20 6f 70 65 6e 20 6f  t pExcept open o
14348 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20  n the table .** 
14349 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69  with root-page i
1434a 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79 2c 20 74  Root. Usually, t
1434b 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6a 75  his is called ju
1434c 73 74 20 62 65 66 6f 72 65 20 63 75 72 73 6f 72  st before cursor
1434d 0a 2a 2a 20 70 45 78 63 65 70 74 20 69 73 20 75  .** pExcept is u
1434e 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  sed to modify th
1434f 65 20 74 61 62 6c 65 20 28 42 74 72 65 65 44 65  e table (BtreeDe
14350 6c 65 74 65 28 29 20 6f 72 20 42 74 72 65 65 49  lete() or BtreeI
14351 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a 73 74 61  nsert())..*/.sta
14352 74 69 63 20 69 6e 74 20 73 61 76 65 41 6c 6c 43  tic int saveAllC
14353 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 20  ursors(BtShared 
14354 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 6f 6f 74  *pBt, Pgno iRoot
14355 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 45 78 63  , BtCursor *pExc
14356 65 70 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72  ept){.  BtCursor
14357 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73   *p;.  assert( s
14358 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14359 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1435a 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 63 65  .  assert( pExce
1435b 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 65 70 74  pt==0 || pExcept
1435c 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 20 20  ->pBt==pBt );.  
1435d 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73  for(p=pBt->pCurs
1435e 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  or; p; p=p->pNex
1435f 74 29 7b 0a 20 20 20 20 69 66 28 20 70 21 3d 70  t){.    if( p!=p
14360 45 78 63 65 70 74 20 26 26 20 28 30 3d 3d 69 52  Except && (0==iR
14361 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f  oot || p->pgnoRo
14362 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26 20 0a 20  ot==iRoot) && . 
14363 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65         p->eState
14364 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
14365 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d  {.      int rc =
14366 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74   saveCursorPosit
14367 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 20 69 66  ion(p);.      if
14368 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20  ( SQLITE_OK!=rc 
14369 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
1436a 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
1436b 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1436c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1436d 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63  *.** Clear the c
1436e 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f  urrent cursor po
1436f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  sition..*/.SQLIT
14370 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
14371 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72  qlite3BtreeClear
14372 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20  Cursor(BtCursor 
14373 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74  *pCur){.  assert
14374 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
14375 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71  ex(pCur) );.  sq
14376 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d  lite3_free(pCur-
14377 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75 72 2d 3e  >pKey);.  pCur->
14378 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70 43 75 72  pKey = 0;.  pCur
14379 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
1437a 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a  R_INVALID;.}../*
1437b 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20  .** Restore the 
1437c 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 70 6f  cursor to the po
1437d 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 69 6e  sition it was in
1437e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 74 6f   (or as close to
1437f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a 2a   as possible).**
14380 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 6f 72   when saveCursor
14381 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 20 63  Position() was c
14382 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61 74  alled. Note that
14383 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c 65 74   this call delet
14384 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 65 64  es the .** saved
14385 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20 73   position info s
14386 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 75 72  tored by saveCur
14387 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20 73  sorPosition(), s
14388 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a  o there can be.*
14389 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 66  * at most one ef
1438a 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 65 43  fective restoreC
1438b 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20  ursorPosition() 
1438c 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 68 20  call after each 
1438d 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 50 6f  .** saveCursorPo
1438e 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 53 51 4c  sition()..*/.SQL
1438f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
14390 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 73 74  sqlite3BtreeRest
14391 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
14392 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  n(BtCursor *pCur
14393 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  a
14394 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
14395 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
14396 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
14397 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f  >eState>=CURSOR_
14398 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20  REQUIRESEEK );. 
14399 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
1439a 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  e==CURSOR_FAULT 
1439b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43  ){.    return pC
1439c 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a 20 20  ur->skip;.  }.  
1439d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
1439e 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20  URSOR_INVALID;. 
1439f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
143a0 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70  eeMoveto(pCur, p
143a1 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d  Cur->pKey, pCur-
143a2 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d  >nKey, 0, &pCur-
143a3 3e 73 6b 69 70 29 3b 0a 20 20 69 66 28 20 72 63  >skip);.  if( rc
143a4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
143a5 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
143a6 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20  pCur->pKey);.   
143a7 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b   pCur->pKey = 0;
143a8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
143a9 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
143aa 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d  R_VALID || pCur-
143ab 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
143ac 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20  INVALID );.  }. 
143ad 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
143ae 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65 43 75  define restoreCu
143af 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20  rsorPosition(p) 
143b0 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 65 3e 3d  \.  (p->eState>=
143b1 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
143b2 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 20 20 20  EK ? \.         
143b3 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 73 74  sqlite3BtreeRest
143b4 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
143b5 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 20  n(p) : \.       
143b6 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a    SQLITE_OK)../*
143b7 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68  .** Determine wh
143b8 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 63  ether or not a c
143b9 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20  ursor has moved 
143ba 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 69 6f  from the positio
143bb 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74  n it.** was last
143bc 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43 75 72   placed at.  Cur
143bd 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 68  sors can move wh
143be 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 20  en the row they 
143bf 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20  are pointing.** 
143c0 61 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 75  at is deleted ou
143c1 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65  t from under the
143c2 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  m..**.** This ro
143c3 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 6e  utine returns an
143c4 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 73   error code if s
143c5 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
143c6 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74  ong.  The.** int
143c7 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65 64 20  eger *pHasMoved 
143c8 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 69 66  is set to one if
143c9 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20   the cursor has 
143ca 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 66 20 6e  moved and 0 if n
143cb 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ot..*/.SQLITE_PR
143cc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
143cd 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d  3BtreeCursorHasM
143ce 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20 2a 70  oved(BtCursor *p
143cf 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f  Cur, int *pHasMo
143d0 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ved){.  int rc;.
143d1 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43  .  rc = restoreC
143d2 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43  ursorPosition(pC
143d3 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  ur);.  if( rc ){
143d4 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20  .    *pHasMoved 
143d5 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
143d6 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  rc;.  }.  if( pC
143d7 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  ur->eState!=CURS
143d8 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72  OR_VALID || pCur
143d9 2d 3e 73 6b 69 70 21 3d 30 20 29 7b 0a 20 20 20  ->skip!=0 ){.   
143da 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b   *pHasMoved = 1;
143db 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
143dc 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 20  HasMoved = 0;.  
143dd 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
143de 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  E_OK;.}..#ifndef
143df 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
143e0 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69  OVACUUM./*.** Gi
143e1 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65  ven a page numbe
143e2 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 64  r of a regular d
143e3 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 65  atabase page, re
143e4 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a  turn the page.**
143e5 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
143e6 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
143e7 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
143e8 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  he entry for the
143e9 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 6e  .** input page n
143ea 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  umber..*/.static
143eb 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 65   Pgno ptrmapPage
143ec 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  no(BtShared *pBt
143ed 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20  , Pgno pgno){.  
143ee 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 70  int nPagesPerMap
143ef 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74  Page;.  Pgno iPt
143f0 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73 73  rMap, ret;.  ass
143f1 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
143f2 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
143f3 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73 50  ex) );.  nPagesP
143f4 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42 74  erMapPage = (pBt
143f5 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b  ->usableSize/5)+
143f6 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 28  1;.  iPtrMap = (
143f7 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65  pgno-2)/nPagesPe
143f8 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74 20  rMapPage;.  ret 
143f9 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 65  = (iPtrMap*nPage
143fa 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20 32  sPerMapPage) + 2
143fb 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 45  ; .  if( ret==PE
143fc 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
143fd 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 2b  pBt) ){.    ret+
143fe 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  +;.  }.  return 
143ff 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  ret;.}../*.** Wr
14400 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74  ite an entry int
14401 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  o the pointer ma
14402 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  p..**.** This ro
14403 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74 68  utine updates th
14404 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e  e pointer map en
14405 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75 6d  try for page num
14406 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20  ber 'key'.** so 
14407 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f 20  that it maps to 
14408 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e 64  type 'eType' and
14409 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d   parent page num
1440a 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 20 41  ber 'pgno'..** A
1440b 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  n error code is 
1440c 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65  returned if some
1440d 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
1440e 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49  , otherwise SQLI
1440f 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63  TE_OK..*/.static
14410 20 69 6e 74 20 70 74 72 6d 61 70 50 75 74 28 42   int ptrmapPut(B
14411 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67  tShared *pBt, Pg
14412 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 70 65  no key, u8 eType
14413 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 29 7b 0a  , Pgno parent){.
14414 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
14415 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74  e;  /* The point
14416 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20  er map page */. 
14417 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20   u8 *pPtrmap;   
14418 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65     /* The pointe
14419 72 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20  r map data */.  
1441a 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20  Pgno iPtrmap;   
1441b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72    /* The pointer
1441c 20 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72   map page number
1441d 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74   */.  int offset
1441e 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65  ;       /* Offse
1441f 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70  t in pointer map
14420 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72   page */.  int r
14421 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  c;..  assert( sq
14422 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14423 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
14424 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 72 2d    /* The master-
14425 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d  journal page num
14426 62 65 72 20 6d 75 73 74 20 6e 65 76 65 72 20 62  ber must never b
14427 65 20 75 73 65 64 20 61 73 20 61 20 70 6f 69 6e  e used as a poin
14428 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a  ter map page */.
14429 20 20 61 73 73 65 72 74 28 20 30 3d 3d 50 54 52    assert( 0==PTR
1442a 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
1442b 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1442c 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 61 73  E(pBt)) );..  as
1442d 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56  sert( pBt->autoV
1442e 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 20 6b  acuum );.  if( k
1442f 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ey==0 ){.    ret
14430 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
14431 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 69  PT_BKPT;.  }.  i
14432 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f  Ptrmap = PTRMAP_
14433 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29  PAGENO(pBt, key)
14434 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
14435 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50  PagerGet(pBt->pP
14436 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26  ager, iPtrmap, &
14437 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20  pDbPage);.  if( 
14438 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
14439 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
1443a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 50    }.  offset = P
1443b 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28  TRMAP_PTROFFSET(
1443c 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20  iPtrmap, key);. 
1443d 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a   pPtrmap = (u8 *
1443e 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  )sqlite3PagerGet
1443f 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a  Data(pDbPage);..
14440 20 20 69 66 28 20 65 54 79 70 65 21 3d 70 50 74    if( eType!=pPt
14441 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 7c 7c 20  rmap[offset] || 
14442 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d 61  get4byte(&pPtrma
14443 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21 3d 70 61  p[offset+1])!=pa
14444 72 65 6e 74 20 29 7b 0a 20 20 20 20 54 52 41 43  rent ){.    TRAC
14445 45 28 28 22 50 54 52 4d 41 50 5f 55 50 44 41 54  E(("PTRMAP_UPDAT
14446 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64 29 5c 6e  E: %d->(%d,%d)\n
14447 22 2c 20 6b 65 79 2c 20 65 54 79 70 65 2c 20 70  ", key, eType, p
14448 61 72 65 6e 74 29 29 3b 0a 20 20 20 20 72 63 20  arent));.    rc 
14449 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1444a 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ite(pDbPage);.  
1444b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1444c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 74  _OK ){.      pPt
1444d 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65  rmap[offset] = e
1444e 54 79 70 65 3b 0a 20 20 20 20 20 20 70 75 74 34  Type;.      put4
1444f 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66  byte(&pPtrmap[of
14450 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29  fset+1], parent)
14451 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73  ;.    }.  }..  s
14452 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
14453 28 70 44 62 50 61 67 65 29 3b 0a 20 20 72 65 74  (pDbPage);.  ret
14454 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
14455 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79 20 66   Read an entry f
14456 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65 72 20  rom the pointer 
14457 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  map..**.** This 
14458 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65 76 65  routine retrieve
14459 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  s the pointer ma
1445a 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65  p entry for page
1445b 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e 67 0a   'key', writing.
1445c 2a 2a 20 74 68 65 20 74 79 70 65 20 61 6e 64 20  ** the type and 
1445d 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62  parent page numb
1445e 65 72 20 74 6f 20 2a 70 45 54 79 70 65 20 61 6e  er to *pEType an
1445f 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 65 63 74  d *pPgno respect
14460 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72  ively..** An err
14461 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
14462 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e 67  ned if something
14463 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68   goes wrong, oth
14464 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b  erwise SQLITE_OK
14465 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
14466 70 74 72 6d 61 70 47 65 74 28 42 74 53 68 61 72  ptrmapGet(BtShar
14467 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65  ed *pBt, Pgno ke
14468 79 2c 20 75 38 20 2a 70 45 54 79 70 65 2c 20 50  y, u8 *pEType, P
14469 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44  gno *pPgno){.  D
1446a 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20  bPage *pDbPage; 
1446b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72    /* The pointer
1446c 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69   map page */.  i
1446d 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20  nt iPtrmap;     
1446e 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70    /* Pointer map
1446f 20 70 61 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20   page index */. 
14470 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20   u8 *pPtrmap;   
14471 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d      /* Pointer m
14472 61 70 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a  ap page data */.
14473 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20    int offset;   
14474 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f       /* Offset o
14475 66 20 65 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74  f entry in point
14476 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20  er map */.  int 
14477 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  rc;..  assert( s
14478 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14479 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1447a 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54  ..  iPtrmap = PT
1447b 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c  RMAP_PAGENO(pBt,
1447c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71   key);.  rc = sq
1447d 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42  lite3PagerGet(pB
1447e 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d  t->pPager, iPtrm
1447f 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20  ap, &pDbPage);. 
14480 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20   if( rc!=0 ){.  
14481 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
14482 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38  .  pPtrmap = (u8
14483 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
14484 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b  etData(pDbPage);
14485 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52  ..  offset = PTR
14486 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50  MAP_PTROFFSET(iP
14487 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61  trmap, key);.  a
14488 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d 30  ssert( pEType!=0
14489 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d 20   );.  *pEType = 
1448a 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b  pPtrmap[offset];
1448b 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20 2a  .  if( pPgno ) *
1448c 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65  pPgno = get4byte
1448d 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  (&pPtrmap[offset
1448e 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  +1]);..  sqlite3
1448f 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
14490 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54 79  ge);.  if( *pETy
14491 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e  pe<1 || *pEType>
14492 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  5 ) return SQLIT
14493 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
14494 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
14495 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20  OK;.}..#else /* 
14496 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c 49 54  if defined SQLIT
14497 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
14498 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 70  M */.  #define p
14499 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a  trmapPut(w,x,y,z
1449a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64  ) SQLITE_OK.  #d
1449b 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65 74 28  efine ptrmapGet(
1449c 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f  w,x,y,z) SQLITE_
1449d 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72  OK.  #define ptr
1449e 6d 61 70 50 75 74 4f 76 66 6c 28 79 2c 7a 29 20  mapPutOvfl(y,z) 
1449f 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66  SQLITE_OK.#endif
144a0 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20  ../*.** Given a 
144a1 62 74 72 65 65 20 70 61 67 65 20 61 6e 64 20 61  btree page and a
144a2 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d   cell index (0 m
144a3 65 61 6e 73 20 74 68 65 20 66 69 72 73 74 20 63  eans the first c
144a4 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61  ell on.** the pa
144a5 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20  ge, 1 means the 
144a6 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64  second cell, and
144a7 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72   so forth) retur
144a8 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  n a pointer.** t
144a9 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  o the cell conte
144aa 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  nt..**.** This r
144ab 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c  outine works onl
144ac 79 20 66 6f 72 20 70 61 67 65 73 20 74 68 61 74  y for pages that
144ad 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20   do not contain 
144ae 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a  overflow cells..
144af 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43  */.#define findC
144b0 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28 28 50  ell(P,I) \.  ((P
144b1 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d  )->aData + ((P)-
144b2 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65 74 32  >maskPage & get2
144b3 62 79 74 65 28 26 28 50 29 2d 3e 61 44 61 74 61  byte(&(P)->aData
144b4 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 66 73 65 74  [(P)->cellOffset
144b5 2b 32 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a 0a 2a  +2*(I)])))../*.*
144b6 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 63 6f  * This a more co
144b7 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f 66  mplex version of
144b8 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68 61 74   findCell() that
144b9 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 70 61   works for.** pa
144ba 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f 6e 74  ges that do cont
144bb 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  ain overflow cel
144bc 6c 73 2e 20 20 53 65 65 20 69 6e 73 65 72 74 0a  ls.  See insert.
144bd 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2a 66 69  */.static u8 *fi
144be 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 4d  ndOverflowCell(M
144bf 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
144c0 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 69 6e 74  nt iCell){.  int
144c1 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   i;.  assert( sq
144c2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
144c3 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
144c4 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 70  ex) );.  for(i=p
144c5 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2d  Page->nOverflow-
144c6 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20  1; i>=0; i--){. 
144c7 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 73 74     int k;.    st
144c8 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 2a  ruct _OvflCell *
144c9 70 4f 76 66 6c 3b 0a 20 20 20 20 70 4f 76 66 6c  pOvfl;.    pOvfl
144ca 20 3d 20 26 70 50 61 67 65 2d 3e 61 4f 76 66 6c   = &pPage->aOvfl
144cb 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d 20 70 4f 76  [i];.    k = pOv
144cc 66 6c 2d 3e 69 64 78 3b 0a 20 20 20 20 69 66 28  fl->idx;.    if(
144cd 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20   k<=iCell ){.   
144ce 20 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c 6c 20     if( k==iCell 
144cf 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
144d0 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 6c 6c 3b 0a  n pOvfl->pCell;.
144d1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 43        }.      iC
144d2 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d  ell--;.    }.  }
144d3 0a 20 20 72 65 74 75 72 6e 20 66 69 6e 64 43 65  .  return findCe
144d4 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29  ll(pPage, iCell)
144d5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65  ;.}../*.** Parse
144d6 20 61 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20   a cell content 
144d7 62 6c 6f 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69  block and fill i
144d8 6e 20 74 68 65 20 43 65 6c 6c 49 6e 66 6f 20 73  n the CellInfo s
144d9 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65  tructure.  There
144da 0a 2a 2a 20 61 72 65 20 74 77 6f 20 76 65 72 73  .** are two vers
144db 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 66 75 6e  ions of this fun
144dc 63 74 69 6f 6e 2e 20 20 73 71 6c 69 74 65 33 42  ction.  sqlite3B
144dd 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20  treeParseCell() 
144de 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c 6c  takes a .** cell
144df 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 65   index as the se
144e0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 6e  cond argument an
144e1 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  d sqlite3BtreePa
144e2 72 73 65 43 65 6c 6c 50 74 72 28 29 20 0a 2a 2a  rseCellPtr() .**
144e3 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72   takes a pointer
144e4 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20   to the body of 
144e5 74 68 65 20 63 65 6c 6c 20 61 73 20 69 74 73 20  the cell as its 
144e6 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e  second argument.
144e7 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68  .**.** Within th
144e8 69 73 20 66 69 6c 65 2c 20 74 68 65 20 70 61 72  is file, the par
144e9 73 65 43 65 6c 6c 28 29 20 6d 61 63 72 6f 20 63  seCell() macro c
144ea 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 73  an be called ins
144eb 74 65 61 64 20 6f 66 0a 2a 2a 20 73 71 6c 69 74  tead of.** sqlit
144ec 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
144ed 50 74 72 28 29 2e 20 55 73 69 6e 67 20 73 6f 6d  Ptr(). Using som
144ee 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 74 68 69  e compilers, thi
144ef 73 20 77 69 6c 6c 20 62 65 20 66 61 73 74 65 72  s will be faster
144f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
144f1 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
144f2 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  BtreeParseCellPt
144f3 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  r(.  MemPage *pP
144f4 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  age,         /* 
144f5 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  Page containing 
144f6 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38  the cell */.  u8
144f7 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20   *pCell,        
144f8 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
144f9 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74 65 78   to the cell tex
144fa 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f  t. */.  CellInfo
144fb 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20   *pInfo         
144fc 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20  /* Fill in this 
144fd 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a  structure */.){.
144fe 20 20 75 31 36 20 6e 3b 20 20 20 20 20 20 20 20    u16 n;        
144ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
14500 62 65 72 20 62 79 74 65 73 20 69 6e 20 63 65 6c  ber bytes in cel
14501 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72  l content header
14502 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f   */.  u32 nPaylo
14503 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ad;           /*
14504 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
14505 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f 61 64   of cell payload
14506 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
14507 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14508 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
14509 74 65 78 29 20 29 3b 0a 0a 20 20 70 49 6e 66 6f  tex) );..  pInfo
1450a 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b  ->pCell = pCell;
1450b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1450c 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 50 61  ->leaf==0 || pPa
1450d 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b 0a 20  ge->leaf==1 );. 
1450e 20 6e 20 3d 20 70 50 61 67 65 2d 3e 63 68 69 6c   n = pPage->chil
1450f 64 50 74 72 53 69 7a 65 3b 0a 20 20 61 73 73 65  dPtrSize;.  asse
14510 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 50 61 67 65  rt( n==4-4*pPage
14511 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 69 66 28 20  ->leaf );.  if( 
14512 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
14513 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
14514 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20  hasData ){.     
14515 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33   n += getVarint3
14516 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61  2(&pCell[n], nPa
14517 79 6c 6f 61 64 29 3b 0a 20 20 20 20 7d 65 6c 73  yload);.    }els
14518 65 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c 6f 61  e{.      nPayloa
14519 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  d = 0;.    }.   
1451a 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 28   n += getVarint(
1451b 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a  &pCell[n], (u64*
1451c 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a  )&pInfo->nKey);.
1451d 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61      pInfo->nData
1451e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d   = nPayload;.  }
1451f 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 2d  else{.    pInfo-
14520 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20  >nData = 0;.    
14521 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32  n += getVarint32
14522 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79  (&pCell[n], nPay
14523 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 6e 66 6f  load);.    pInfo
14524 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 79 6c 6f 61  ->nKey = nPayloa
14525 64 3b 0a 20 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e  d;.  }.  pInfo->
14526 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61 79 6c  nPayload = nPayl
14527 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48  oad;.  pInfo->nH
14528 65 61 64 65 72 20 3d 20 6e 3b 0a 20 20 69 66 28  eader = n;.  if(
14529 20 6c 69 6b 65 6c 79 28 6e 50 61 79 6c 6f 61 64   likely(nPayload
1452a 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  <=pPage->maxLoca
1452b 6c 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  l) ){.    /* Thi
1452c 73 20 69 73 20 74 68 65 20 28 65 61 73 79 29 20  s is the (easy) 
1452d 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72  common case wher
1452e 65 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 79  e the entire pay
1452f 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 20 2a 2a  load fits.    **
14530 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61   on the local pa
14531 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c 6f 77  ge.  No overflow
14532 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20   is required..  
14533 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 69    */.    int nSi
14534 7a 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ze;          /* 
14535 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 63 65  Total size of ce
14536 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 62 79  ll content in by
14537 74 65 73 20 2a 2f 0a 20 20 20 20 6e 53 69 7a 65  tes */.    nSize
14538 20 3d 20 6e 50 61 79 6c 6f 61 64 20 2b 20 6e 3b   = nPayload + n;
14539 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63  .    pInfo->nLoc
1453a 61 6c 20 3d 20 28 75 31 36 29 6e 50 61 79 6c 6f  al = (u16)nPaylo
1453b 61 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69  ad;.    pInfo->i
1453c 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20  Overflow = 0;.  
1453d 20 20 69 66 28 20 28 6e 53 69 7a 65 20 26 20 7e    if( (nSize & ~
1453e 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e  3)==0 ){.      n
1453f 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20 20 20  Size = 4;       
14540 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c   /* Minimum cell
14541 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20   size is 4 */.  
14542 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e    }.    pInfo->n
14543 53 69 7a 65 20 3d 20 28 75 31 36 29 6e 53 69 7a  Size = (u16)nSiz
14544 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
14545 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f 61  /* If the payloa
14546 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 63  d will not fit c
14547 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74 68 65  ompletely on the
14548 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77 65 20   local page, we 
14549 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64  have.    ** to d
1454a 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68 20 74  ecide how much t
1454b 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20  o store locally 
1454c 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74 6f 20  and how much to 
1454d 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20 20 2a  spill onto.    *
1454e 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  * overflow pages
1454f 2e 20 20 54 68 65 20 73 74 72 61 74 65 67 79 20  .  The strategy 
14550 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 20 74  is to minimize t
14551 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75 6e 75  he amount of unu
14552 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61 63 65  sed.    ** space
14553 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   on overflow pag
14554 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69 6e 67  es while keeping
14555 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6c   the amount of l
14556 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20 20 20  ocal storage.   
14557 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 6d   ** in between m
14558 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61 78 4c  inLocal and maxL
14559 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ocal..    **.   
1455a 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20 63 68   ** Warning:  ch
1455b 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79 20 6f  anging the way o
1455c 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20  verflow payload 
1455d 69 73 20 64 69 73 74 72 69 62 75 74 65 64 20 69  is distributed i
1455e 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77 61 79  n any.    ** way
1455f 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20   will result in 
14560 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  an incompatible 
14561 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20 20 20  file format..   
14562 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69 6e 4c   */.    int minL
14563 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75  ocal;  /* Minimu
14564 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c  m amount of payl
14565 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79  oad held locally
14566 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 78 4c   */.    int maxL
14567 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69 6d 75  ocal;  /* Maximu
14568 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c  m amount of payl
14569 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79  oad held locally
1456a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75 72 70   */.    int surp
1456b 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72 66 6c  lus;   /* Overfl
1456c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61 69 6c  ow payload avail
1456d 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c 20 73  able for local s
1456e 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20 20 6d  torage */..    m
1456f 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d  inLocal = pPage-
14570 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 6d  >minLocal;.    m
14571 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d  axLocal = pPage-
14572 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 73  >maxLocal;.    s
14573 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f 63 61  urplus = minLoca
14574 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20 2d 20  l + (nPayload - 
14575 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61 67 65  minLocal)%(pPage
14576 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
14577 65 20 2d 20 34 29 3b 0a 20 20 20 20 69 66 28 20  e - 4);.    if( 
14578 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f  surplus <= maxLo
14579 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  cal ){.      pIn
1457a 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31  fo->nLocal = (u1
1457b 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d  6)surplus;.    }
1457c 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66  else{.      pInf
1457d 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36  o->nLocal = (u16
1457e 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d  )minLocal;.    }
1457f 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  .    pInfo->iOve
14580 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49  rflow = (u16)(pI
14581 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29  nfo->nLocal + n)
14582 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69  ;.    pInfo->nSi
14583 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  ze = pInfo->iOve
14584 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d  rflow + 4;.  }.}
14585 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65  .#define parseCe
14586 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c  ll(pPage, iCell,
14587 20 70 49 6e 66 6f 29 20 5c 0a 20 20 73 71 6c 69   pInfo) \.  sqli
14588 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
14589 6c 50 74 72 28 28 70 50 61 67 65 29 2c 20 66 69  lPtr((pPage), fi
1458a 6e 64 43 65 6c 6c 28 28 70 50 61 67 65 29 2c 20  ndCell((pPage), 
1458b 28 69 43 65 6c 6c 29 29 2c 20 28 70 49 6e 66 6f  (iCell)), (pInfo
1458c 29 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  )).SQLITE_PRIVAT
1458d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
1458e 72 65 65 50 61 72 73 65 43 65 6c 6c 28 0a 20 20  reeParseCell(.  
1458f 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
14590 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
14591 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63  containing the c
14592 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65  ell */.  int iCe
14593 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ll,             
14594 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69 6e 64   /* The cell ind
14595 65 78 2e 20 20 46 69 72 73 74 20 63 65 6c 6c 20  ex.  First cell 
14596 69 73 20 30 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e  is 0 */.  CellIn
14597 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20  fo *pInfo       
14598 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69    /* Fill in thi
14599 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29  s structure */.)
1459a 7b 0a 20 20 70 61 72 73 65 43 65 6c 6c 28 70 50  {.  parseCell(pP
1459b 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66  age, iCell, pInf
1459c 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  o);.}../*.** Com
1459d 70 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20 6e  pute the total n
1459e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1459f 68 61 74 20 61 20 43 65 6c 6c 20 6e 65 65 64 73  hat a Cell needs
145a0 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20   in the cell.** 
145a1 64 61 74 61 20 61 72 65 61 20 6f 66 20 74 68 65  data area of the
145a2 20 62 74 72 65 65 2d 70 61 67 65 2e 20 20 54 68   btree-page.  Th
145a3 65 20 72 65 74 75 72 6e 20 6e 75 6d 62 65 72 20  e return number 
145a4 69 6e 63 6c 75 64 65 73 20 74 68 65 20 63 65 6c  includes the cel
145a5 6c 0a 2a 2a 20 64 61 74 61 20 68 65 61 64 65 72  l.** data header
145a6 20 61 6e 64 20 74 68 65 20 6c 6f 63 61 6c 20 70   and the local p
145a7 61 79 6c 6f 61 64 2c 20 62 75 74 20 6e 6f 74 20  ayload, but not 
145a8 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  any overflow pag
145a9 65 20 6f 72 0a 2a 2a 20 74 68 65 20 73 70 61 63  e or.** the spac
145aa 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 65  e used by the ce
145ab 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23  ll pointer..*/.#
145ac 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
145ad 61 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a  atic u16 cellSiz
145ae 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
145af 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20  , int iCell){.  
145b0 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
145b1 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
145b2 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  seCell(pPage, iC
145b3 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 72  ell, &info);.  r
145b4 65 74 75 72 6e 20 69 6e 66 6f 2e 6e 53 69 7a 65  eturn info.nSize
145b5 3b 0a 7d 0a 23 65 6e 64 69 66 0a 73 74 61 74 69  ;.}.#endif.stati
145b6 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74  c u16 cellSizePt
145b7 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  r(MemPage *pPage
145b8 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20  , u8 *pCell){.  
145b9 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
145ba 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
145bb 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
145bc 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
145bd 20 20 72 65 74 75 72 6e 20 69 6e 66 6f 2e 6e 53    return info.nS
145be 69 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ize;.}..#ifndef 
145bf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
145c0 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20  VACUUM./*.** If 
145c1 74 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20  the cell pCell, 
145c2 70 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 61  part of page pPa
145c3 67 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  ge contains a po
145c4 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f  inter.** to an o
145c5 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e  verflow page, in
145c6 73 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 6e  sert an entry in
145c7 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  to the pointer-m
145c8 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76  ap.** for the ov
145c9 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a  erflow page..*/.
145ca 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61  static int ptrma
145cb 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50  pPutOvflPtr(MemP
145cc 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a  age *pPage, u8 *
145cd 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 6e  pCell){.  CellIn
145ce 66 6f 20 69 6e 66 6f 3b 0a 20 20 61 73 73 65 72  fo info;.  asser
145cf 74 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20  t( pCell!=0 );. 
145d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
145d1 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
145d2 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
145d3 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f 2e    assert( (info.
145d4 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e  nData+(pPage->in
145d5 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79  tKey?0:info.nKey
145d6 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61  ))==info.nPayloa
145d7 64 20 29 3b 0a 20 20 69 66 28 20 28 69 6e 66 6f  d );.  if( (info
145d8 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69  .nData+(pPage->i
145d9 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65  ntKey?0:info.nKe
145da 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20  y))>info.nLocal 
145db 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c  ){.    Pgno ovfl
145dc 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65   = get4byte(&pCe
145dd 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
145de 77 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  w]);.    return 
145df 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d  ptrmapPut(pPage-
145e0 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d  >pBt, ovfl, PTRM
145e1 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50  AP_OVERFLOW1, pP
145e2 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a  age->pgno);.  }.
145e3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
145e4 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 49 66 20 74  OK;.}./*.** If t
145e5 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69 6e 64  he cell with ind
145e6 65 78 20 69 43 65 6c 6c 20 6f 6e 20 70 61 67 65  ex iCell on page
145e7 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20   pPage contains 
145e8 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
145e9 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
145ea 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72  , insert an entr
145eb 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74  y into the point
145ec 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68  er-map.** for th
145ed 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  e overflow page.
145ee 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
145ef 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 4d 65 6d  trmapPutOvfl(Mem
145f0 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74  Page *pPage, int
145f1 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70   iCell){.  u8 *p
145f2 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20  Cell;.  assert( 
145f3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
145f4 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
145f5 75 74 65 78 29 20 29 3b 0a 20 20 70 43 65 6c 6c  utex) );.  pCell
145f6 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43   = findOverflowC
145f7 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c  ell(pPage, iCell
145f8 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 74 72 6d  );.  return ptrm
145f9 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61  apPutOvflPtr(pPa
145fa 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 7d 0a 23 65  ge, pCell);.}.#e
145fb 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  ndif.../*.** Def
145fc 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65  ragment the page
145fd 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c   given.  All Cel
145fe 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20  ls are moved to 
145ff 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68  the.** end of th
14600 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 66  e page and all f
14601 72 65 65 20 73 70 61 63 65 20 69 73 20 63 6f 6c  ree space is col
14602 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a  lected into one.
14603 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b 20 74  ** big FreeBlk t
14604 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 62 65  hat occurs in be
14605 74 77 65 65 6e 20 74 68 65 20 68 65 61 64 65 72  tween the header
14606 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69   and cell.** poi
14607 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64 20 74  nter array and t
14608 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
14609 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  area..*/.static 
1460a 69 6e 74 20 64 65 66 72 61 67 6d 65 6e 74 50 61  int defragmentPa
1460b 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
1460c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  e){.  int i;    
1460d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1460e 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
1460f 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20   */.  int pc;   
14610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14611 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61   /* Address of a
14612 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20   i-th cell */.  
14613 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20  int addr;       
14614 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
14615 66 73 65 74 20 6f 66 20 66 69 72 73 74 20 62 79  fset of first by
14616 74 65 20 61 66 74 65 72 20 63 65 6c 6c 20 70 6f  te after cell po
14617 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20  inter array */. 
14618 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20   int hdr;       
14619 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1461a 66 66 73 65 74 20 74 6f 20 74 68 65 20 70 61 67  ffset to the pag
1461b 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e  e header */.  in
1461c 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20  t size;         
1461d 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
1461e 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 20 20   of a cell */.  
1461f 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20  int usableSize; 
14620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
14621 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62  mber of usable b
14622 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a  ytes on a page *
14623 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73  /.  int cellOffs
14624 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
14625 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20  * Offset to the 
14626 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72  cell pointer arr
14627 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b  ay */.  int cbrk
14628 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
14629 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20     /* Offset to 
1462a 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
1462b 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20 6e   area */.  int n
1462c 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
1462d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1462e 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20  of cells on the 
1462f 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  page */.  unsign
14630 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 20 20  ed char *data;  
14631 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
14632 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 69 67   data */.  unsig
14633 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 3b 20  ned char *temp; 
14634 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 61 72        /* Temp ar
14635 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f 6e 74  ea for cell cont
14636 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ent */..  assert
14637 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
14638 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
14639 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61  >pDbPage) );.  a
1463a 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42  ssert( pPage->pB
1463b 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t!=0 );.  assert
1463c 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  ( pPage->pBt->us
1463d 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c 49  ableSize <= SQLI
1463e 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
1463f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
14640 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  age->nOverflow==
14641 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
14642 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14643 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
14644 74 65 78 29 20 29 3b 0a 20 20 74 65 6d 70 20 3d  tex) );.  temp =
14645 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d   sqlite3PagerTem
14646 70 53 70 61 63 65 28 70 50 61 67 65 2d 3e 70 42  pSpace(pPage->pB
14647 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 64 61  t->pPager);.  da
14648 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
14649 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65  a;.  hdr = pPage
1464a 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63  ->hdrOffset;.  c
1464b 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67  ellOffset = pPag
1464c 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20  e->cellOffset;. 
1464d 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e   nCell = pPage->
1464e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28  nCell;.  assert(
1464f 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74 65   nCell==get2byte
14650 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 20 29  (&data[hdr+3]) )
14651 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d  ;.  usableSize =
14652 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61   pPage->pBt->usa
14653 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 72 6b 20  bleSize;.  cbrk 
14654 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
14655 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63  [hdr+5]);.  memc
14656 70 79 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c 20  py(&temp[cbrk], 
14657 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61  &data[cbrk], usa
14658 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 6b 29 3b  bleSize - cbrk);
14659 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 62 6c 65  .  cbrk = usable
1465a 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  Size;.  for(i=0;
1465b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a   i<nCell; i++){.
1465c 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20 20      u8 *pAddr;  
1465d 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 63     /* The i-th c
1465e 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ell pointer */. 
1465f 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 74 61     pAddr = &data
14660 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69 2a  [cellOffset + i*
14661 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65 74  2];.    pc = get
14662 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20 20  2byte(pAddr);.  
14663 20 20 69 66 28 20 70 63 3e 3d 75 73 61 62 6c 65    if( pc>=usable
14664 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65  Size ){.      re
14665 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
14666 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
14667 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53      size = cellS
14668 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 74  izePtr(pPage, &t
14669 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62  emp[pc]);.    cb
1466a 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20  rk -= size;.    
1466b 69 66 28 20 63 62 72 6b 3c 63 65 6c 6c 4f 66 66  if( cbrk<cellOff
1466c 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 7c 7c 20 70  set+2*nCell || p
1466d 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a  c+size>usableSiz
1466e 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  e ){.      retur
1466f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
14670 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _BKPT;.    }.   
14671 20 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73 69   assert( cbrk+si
14672 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 26  ze<=usableSize &
14673 26 20 63 62 72 6b 3e 3d 30 20 29 3b 0a 20 20 20  & cbrk>=0 );.   
14674 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 62   memcpy(&data[cb
14675 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d 2c 20  rk], &temp[pc], 
14676 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 32 62  size);.    put2b
14677 79 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29  yte(pAddr, cbrk)
14678 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
14679 63 62 72 6b 3e 3d 63 65 6c 6c 4f 66 66 73 65 74  cbrk>=cellOffset
1467a 2b 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20 20 70 75  +2*nCell );.  pu
1467b 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
1467c 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 64 61  +5], cbrk);.  da
1467d 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 3b 0a 20  ta[hdr+1] = 0;. 
1467e 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 3d 20 30   data[hdr+2] = 0
1467f 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20  ;.  data[hdr+7] 
14680 3d 20 30 3b 0a 20 20 61 64 64 72 20 3d 20 63 65  = 0;.  addr = ce
14681 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c  llOffset+2*nCell
14682 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61  ;.  memset(&data
14683 5b 61 64 64 72 5d 2c 20 30 2c 20 63 62 72 6b 2d  [addr], 0, cbrk-
14684 61 64 64 72 29 3b 0a 20 20 61 73 73 65 72 74 28  addr);.  assert(
14685 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
14686 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
14687 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66  pDbPage) );.  if
14688 28 20 63 62 72 6b 2d 61 64 64 72 21 3d 70 50 61  ( cbrk-addr!=pPa
14689 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20  ge->nFree ){.   
1468a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1468b 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
1468c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1468d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  _OK;.}../*.** Al
1468e 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74  locate nByte byt
1468f 65 73 20 6f 66 20 73 70 61 63 65 20 6f 6e 20 61  es of space on a
14690 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74   page..**.** Ret
14691 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e  urn the index in
14692 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  to pPage->aData[
14693 5d 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 62  ] of the first b
14694 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65  yte of.** the ne
14695 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54  w allocation.  T
14696 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e  he caller guaran
14697 74 65 65 73 20 74 68 61 74 20 74 68 65 72 65 20  tees that there 
14698 69 73 20 65 6e 6f 75 67 68 0a 2a 2a 20 73 70 61  is enough.** spa
14699 63 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ce.  This routin
1469a 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 66 61 69  e will never fai
1469b 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  l..**.** If the 
1469c 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 6e 42  page contains nB
1469d 79 74 65 73 20 6f 66 20 66 72 65 65 20 73 70 61  ytes of free spa
1469e 63 65 20 62 75 74 20 64 6f 65 73 20 6e 6f 74 20  ce but does not 
1469f 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 42 79 74 65  contain.** nByte
146a0 73 20 6f 66 20 63 6f 6e 74 69 67 75 6f 75 73 20  s of contiguous 
146a1 66 72 65 65 20 73 70 61 63 65 2c 20 74 68 65 6e  free space, then
146a2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 75   this routine au
146a3 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 63  tomatically.** c
146a4 61 6c 6c 73 20 64 65 66 72 61 67 6d 65 6e 74 50  alls defragmentP
146a5 61 67 65 28 29 20 74 6f 20 63 6f 6e 73 6f 6c 69  age() to consoli
146a6 64 61 74 65 20 61 6c 6c 20 66 72 65 65 20 73 70  date all free sp
146a7 61 63 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 61  ace before .** a
146a8 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20 6e 65  llocating the ne
146a9 77 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 73 74 61 74  w chunk..*/.stat
146aa 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 53  ic int allocateS
146ab 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  pace(MemPage *pP
146ac 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  age, int nByte){
146ad 0a 20 20 69 6e 74 20 61 64 64 72 2c 20 70 63 2c  .  int addr, pc,
146ae 20 68 64 72 3b 0a 20 20 69 6e 74 20 73 69 7a 65   hdr;.  int size
146af 3b 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b 0a 20  ;.  int nFrag;. 
146b0 20 69 6e 74 20 74 6f 70 3b 0a 20 20 69 6e 74 20   int top;.  int 
146b1 6e 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 63 65 6c  nCell;.  int cel
146b2 6c 4f 66 66 73 65 74 3b 0a 20 20 75 6e 73 69 67  lOffset;.  unsig
146b3 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 0a  ned char *data;.
146b4 20 20 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67    .  data = pPag
146b5 65 2d 3e 61 44 61 74 61 3b 0a 20 20 61 73 73 65  e->aData;.  asse
146b6 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
146b7 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
146b8 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
146b9 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
146ba 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  pBt );.  assert(
146bb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
146bc 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
146bd 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
146be 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29 3b 20  rt( nByte>=0 ); 
146bf 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c   /* Minimum cell
146c0 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20   size is 4 */.  
146c1 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
146c2 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b 0a 20  Free>=nByte );. 
146c3 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
146c4 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a  nOverflow==0 );.
146c5 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d    pPage->nFree -
146c6 3d 20 28 75 31 36 29 6e 42 79 74 65 3b 0a 20 20  = (u16)nByte;.  
146c7 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
146c8 4f 66 66 73 65 74 3b 0a 0a 20 20 6e 46 72 61 67  Offset;..  nFrag
146c9 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a   = data[hdr+7];.
146ca 20 20 69 66 28 20 6e 46 72 61 67 3c 36 30 20 29    if( nFrag<60 )
146cb 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20  {.    /* Search 
146cc 74 68 65 20 66 72 65 65 6c 69 73 74 20 6c 6f 6f  the freelist loo
146cd 6b 69 6e 67 20 66 6f 72 20 61 20 73 6c 6f 74 20  king for a slot 
146ce 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61  big enough to sa
146cf 74 69 73 66 79 20 74 68 65 0a 20 20 20 20 2a 2a  tisfy the.    **
146d0 20 73 70 61 63 65 20 72 65 71 75 65 73 74 2e 20   space request. 
146d1 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 68 64  */.    addr = hd
146d2 72 2b 31 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  r+1;.    while( 
146d3 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26  (pc = get2byte(&
146d4 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29  data[addr]))>0 )
146d5 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67  {.      size = g
146d6 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  et2byte(&data[pc
146d7 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +2]);.      if( 
146d8 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b 0a 20  size>=nByte ){. 
146d9 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 73         int x = s
146da 69 7a 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20  ize - nByte;.   
146db 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c 6e 42       if( size<nB
146dc 79 74 65 2b 34 20 29 7b 0a 20 20 20 20 20 20 20  yte+4 ){.       
146dd 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b     memcpy(&data[
146de 61 64 64 72 5d 2c 20 26 64 61 74 61 5b 70 63 5d  addr], &data[pc]
146df 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 2);.          
146e0 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 28 75  data[hdr+7] = (u
146e1 38 29 28 6e 46 72 61 67 20 2b 20 78 29 3b 0a 20  8)(nFrag + x);. 
146e2 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
146e3 70 63 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  pc;.        }els
146e4 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74  e{.          put
146e5 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32  2byte(&data[pc+2
146e6 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 20 20 20  ], x);.         
146e7 20 72 65 74 75 72 6e 20 70 63 20 2b 20 78 3b 0a   return pc + x;.
146e8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
146e9 7d 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 70  }.      addr = p
146ea 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
146eb 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  /* Allocate memo
146ec 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20  ry from the gap 
146ed 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63  in between the c
146ee 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
146ef 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63  y.  ** and the c
146f0 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
146f1 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 3d 20 67  ..  */.  top = g
146f2 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
146f3 72 2b 35 5d 29 3b 0a 20 20 6e 43 65 6c 6c 20 3d  r+5]);.  nCell =
146f4 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
146f5 68 64 72 2b 33 5d 29 3b 0a 20 20 63 65 6c 6c 4f  hdr+3]);.  cellO
146f6 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63  ffset = pPage->c
146f7 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 69 66 28  ellOffset;.  if(
146f8 20 6e 46 72 61 67 3e 3d 36 30 20 7c 7c 20 63 65   nFrag>=60 || ce
146f9 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 65  llOffset + 2*nCe
146fa 6c 6c 20 3e 20 74 6f 70 20 2d 20 6e 42 79 74 65  ll > top - nByte
146fb 20 29 7b 0a 20 20 20 20 64 65 66 72 61 67 6d 65   ){.    defragme
146fc 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  ntPage(pPage);. 
146fd 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74     top = get2byt
146fe 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  e(&data[hdr+5]);
146ff 0a 20 20 7d 0a 20 20 74 6f 70 20 2d 3d 20 6e 42  .  }.  top -= nB
14700 79 74 65 3b 0a 20 20 61 73 73 65 72 74 28 20 63  yte;.  assert( c
14701 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43  ellOffset + 2*nC
14702 65 6c 6c 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 20  ell <= top );.  
14703 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  put2byte(&data[h
14704 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61  dr+5], top);.  a
14705 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
14706 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
14707 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
14708 3b 0a 20 20 72 65 74 75 72 6e 20 74 6f 70 3b 0a  ;.  return top;.
14709 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1470a 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65  a section of the
1470b 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f   pPage->aData to
1470c 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a   the freelist..*
1470d 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65  * The first byte
1470e 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65   of the new free
1470f 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d   block is pPage-
14710 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a  >aDisk[start].**
14711 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66   and the size of
14712 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73   the block is "s
14713 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  ize" bytes..**.*
14714 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66  * Most of the ef
14715 66 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76  fort here is inv
14716 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69  olved in coalesi
14717 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66  ng adjacent.** f
14718 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20  ree blocks into 
14719 61 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65  a single big fre
1471a 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74  e block..*/.stat
1471b 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65  ic int freeSpace
1471c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1471d 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20   int start, int 
1471e 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64  size){.  int add
1471f 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a  r, pbegin, hdr;.
14720 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
14721 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61  *data = pPage->a
14722 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28  Data;..  assert(
14723 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29   pPage->pBt!=0 )
14724 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
14725 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
14726 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
14727 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
14728 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68   start>=pPage->h
14729 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 50 61 67  drOffset+6+(pPag
1472a 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a  e->leaf?0:4) );.
1472b 20 20 61 73 73 65 72 74 28 20 28 73 74 61 72 74    assert( (start
1472c 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d   + size)<=pPage-
1472d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
1472e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
1472f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14730 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
14731 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
14732 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a   size>=0 );   /*
14733 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69   Minimum cell si
14734 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64  ze is 4 */..#ifd
14735 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45  ef SQLITE_SECURE
14736 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65  _DELETE.  /* Ove
14737 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69  rwrite deleted i
14738 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20  nformation with 
14739 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53  zeros when the S
1473a 45 43 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20  ECURE_DELETE .  
1473b 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61  ** option is ena
1473c 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d  bled at compile-
1473d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  time */.  memset
1473e 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30  (&data[start], 0
1473f 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a  , size);.#endif.
14740 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70  .  /* Add the sp
14741 61 63 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  ace back into th
14742 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  e linked list of
14743 20 66 72 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20   freeblocks */. 
14744 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64   hdr = pPage->hd
14745 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20  rOffset;.  addr 
14746 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69  = hdr + 1;.  whi
14747 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65  le( (pbegin = ge
14748 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64  t2byte(&data[add
14749 72 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 62  r]))<start && pb
1474a 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73  egin>0 ){.    as
1474b 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50  sert( pbegin<=pP
1474c 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
1474d 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 69 66  Size-4 );.    if
1474e 28 20 70 62 65 67 69 6e 3c 3d 61 64 64 72 20 29  ( pbegin<=addr )
1474f 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
14750 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
14751 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  KPT;.    }.    a
14752 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20  ddr = pbegin;.  
14753 7d 0a 20 20 69 66 20 28 20 70 62 65 67 69 6e 3e  }.  if ( pbegin>
14754 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
14755 6c 65 53 69 7a 65 2d 34 20 29 20 7b 0a 20 20 20  leSize-4 ) {.   
14756 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
14757 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
14758 0a 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69  .  assert( pbegi
14759 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67 69 6e  n>addr || pbegin
1475a 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62 79 74  ==0 );.  put2byt
1475b 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 73  e(&data[addr], s
1475c 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62 79 74  tart);.  put2byt
1475d 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20  e(&data[start], 
1475e 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74 32 62  pbegin);.  put2b
1475f 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 2b  yte(&data[start+
14760 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50 61  2], size);.  pPa
14761 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 28 75 31  ge->nFree += (u1
14762 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f  6)size;..  /* Co
14763 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20  alesce adjacent 
14764 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20  free blocks */. 
14765 20 61 64 64 72 20 3d 20 70 50 61 67 65 2d 3e 68   addr = pPage->h
14766 64 72 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20 20  drOffset + 1;.  
14767 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d  while( (pbegin =
14768 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
14769 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20  addr]))>0 ){.   
1476a 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 69 7a   int pnext, psiz
1476b 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 65 72 74  e, x;.    assert
1476c 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b  ( pbegin>addr );
1476d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65  .    assert( pbe
1476e 67 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d  gin<=pPage->pBt-
1476f 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b  >usableSize-4 );
14770 0a 20 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74  .    pnext = get
14771 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67  2byte(&data[pbeg
14772 69 6e 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20  in]);.    psize 
14773 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
14774 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20  [pbegin+2]);.   
14775 20 69 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73   if( pbegin + ps
14776 69 7a 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74  ize + 3 >= pnext
14777 20 26 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20   && pnext>0 ){. 
14778 20 20 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20       int frag = 
14779 70 6e 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b  pnext - (pbegin+
1477a 70 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66  psize);.      if
1477b 28 20 28 66 72 61 67 3c 30 29 20 7c 7c 20 28 66  ( (frag<0) || (f
1477c 72 61 67 3e 28 69 6e 74 29 64 61 74 61 5b 70 50  rag>(int)data[pP
1477d 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37  age->hdrOffset+7
1477e 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ]) ){.        re
1477f 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
14780 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
14781 7d 0a 20 20 20 20 20 20 64 61 74 61 5b 70 50 61  }.      data[pPa
14782 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d  ge->hdrOffset+7]
14783 20 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20   -= (u8)frag;.  
14784 20 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65      x = get2byte
14785 28 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a  (&data[pnext]);.
14786 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26        put2byte(&
14787 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29  data[pbegin], x)
14788 3b 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78  ;.      x = pnex
14789 74 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61  t + get2byte(&da
1478a 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70  ta[pnext+2]) - p
1478b 62 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74  begin;.      put
1478c 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67  2byte(&data[pbeg
1478d 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d  in+2], x);.    }
1478e 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72  else{.      addr
1478f 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d   = pbegin;.    }
14790 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
14791 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
14792 72 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20  rea begins with 
14793 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d  a freeblock, rem
14794 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28  ove it. */.  if(
14795 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61   data[hdr+1]==da
14796 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74  ta[hdr+5] && dat
14797 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68  a[hdr+2]==data[h
14798 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74  dr+6] ){.    int
14799 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e   top;.    pbegin
1479a 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
1479b 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d  a[hdr+1]);.    m
1479c 65 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b  emcpy(&data[hdr+
1479d 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e  1], &data[pbegin
1479e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d  ], 2);.    top =
1479f 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
147a0 68 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79  hdr+5]) + get2by
147a1 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b  te(&data[pbegin+
147a2 32 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74  2]);.    put2byt
147a3 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
147a4 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  top);.  }.  asse
147a5 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
147a6 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
147a7 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
147a8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
147a9 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f  K;.}../*.** Deco
147aa 64 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74  de the flags byt
147ab 65 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74  e (the first byt
147ac 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29  e of the header)
147ad 20 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61   for a page.** a
147ae 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69  nd initialize fi
147af 65 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50  elds of the MemP
147b0 61 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63  age structure ac
147b1 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a  cordingly..**.**
147b2 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77   Only the follow
147b3 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  ing combinations
147b4 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20   are supported. 
147b5 20 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72   Anything differ
147b6 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73  ent.** indicates
147b7 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
147b8 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a  ase files:.**.**
147b9 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52           PTF_ZER
147ba 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20  ODATA.**        
147bb 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20   PTF_ZERODATA | 
147bc 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20  PTF_LEAF.**     
147bd 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41      PTF_LEAFDATA
147be 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a   | PTF_INTKEY.**
147bf 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41           PTF_LEA
147c0 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b  FDATA | PTF_INTK
147c1 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f  EY | PTF_LEAF.*/
147c2 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f  .static int deco
147c3 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20  deFlags(MemPage 
147c4 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67  *pPage, int flag
147c5 42 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65  Byte){.  BtShare
147c6 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41  d *pBt;     /* A
147c7 20 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e   copy of pPage->
147c8 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  pBt */..  assert
147c9 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  ( pPage->hdrOffs
147ca 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f  et==(pPage->pgno
147cb 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29  ==1 ? 100 : 0) )
147cc 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
147cd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
147ce 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
147cf 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65  ) );.  pPage->le
147d0 61 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79  af = (u8)(flagBy
147d1 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28  te>>3);  assert(
147d2 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c   PTF_LEAF == 1<<
147d3 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20  3 );.  flagByte 
147d4 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20  &= ~PTF_LEAF;.  
147d5 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53  pPage->childPtrS
147d6 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d  ize = 4-4*pPage-
147d7 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70  >leaf;.  pBt = p
147d8 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28  Page->pBt;.  if(
147d9 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f   flagByte==(PTF_
147da 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49  LEAFDATA | PTF_I
147db 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50  NTKEY) ){.    pP
147dc 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b  age->intKey = 1;
147dd 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44  .    pPage->hasD
147de 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61  ata = pPage->lea
147df 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61  f;.    pPage->ma
147e0 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61  xLocal = pBt->ma
147e1 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65  xLeaf;.    pPage
147e2 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74  ->minLocal = pBt
147e3 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c  ->minLeaf;.  }el
147e4 73 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d  se if( flagByte=
147e5 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b  =PTF_ZERODATA ){
147e6 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b  .    pPage->intK
147e7 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  ey = 0;.    pPag
147e8 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a  e->hasData = 0;.
147e9 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f      pPage->maxLo
147ea 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f  cal = pBt->maxLo
147eb 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  cal;.    pPage->
147ec 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e  minLocal = pBt->
147ed 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73  minLocal;.  }els
147ee 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e{.    return SQ
147ef 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
147f0 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  T;.  }.  return 
147f1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
147f2 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  .** Initialize t
147f3 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66  he auxiliary inf
147f4 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64  ormation for a d
147f5 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  isk block..**.**
147f6 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
147f7 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49  K on success.  I
147f8 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68  f we see that th
147f9 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e  e page does.** n
147fa 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c  ot contain a wel
147fb 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73  l-formed databas
147fc 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74  e page, then ret
147fd 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43  urn .** SQLITE_C
147fe 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68  ORRUPT.  Note th
147ff 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53  at a return of S
14800 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f  QLITE_OK does no
14801 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74  t.** guarantee t
14802 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20  hat the page is 
14803 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74  well-formed.  It
14804 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74   only shows that
14805 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f  .** we failed to
14806 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72   detect any corr
14807 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  uption..*/.SQLIT
14808 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14809 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
1480a 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
1480b 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  e){..  assert( p
1480c 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a  Page->pBt!=0 );.
1480d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1480e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
1480f 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
14810 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
14811 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65  ge->pgno==sqlite
14812 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72  3PagerPagenumber
14813 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
14814 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
14815 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61  age == sqlite3Pa
14816 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67  gerGetExtra(pPag
14817 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
14818 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
14819 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33  aData == sqlite3
1481a 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61  PagerGetData(pPa
1481b 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
1481c 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69  .  if( !pPage->i
1481d 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36  sInit ){.    u16
1481e 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   pc;            
1481f 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20  /* Address of a 
14820 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e  freeblock within
14821 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20   pPage->aData[] 
14822 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20  */.    u8 hdr;  
14823 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
14824 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67  set to beginning
14825 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20   of page header 
14826 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b  */.    u8 *data;
14827 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75            /* Equ
14828 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61  al to pPage->aDa
14829 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72  ta */.    BtShar
1482a 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20  ed *pBt;        
1482b 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65  /* The main btre
1482c 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
1482d 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a     u16 usableSiz
1482e 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20  e;    /* Amount 
1482f 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20  of usable space 
14830 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  on each page */.
14831 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73      u16 cellOffs
14832 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74  et;    /* Offset
14833 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70   from start of p
14834 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c  age to first cel
14835 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20  l pointer */.   
14836 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20   u16 nFree;     
14837 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14838 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e   unused bytes on
14839 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20   the page */.   
1483a 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20   u16 top;       
1483b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
1483c 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  e of the cell co
1483d 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 0a 20  ntent area */.. 
1483e 20 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e     pBt = pPage->
1483f 70 42 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20  pBt;..    hdr = 
14840 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
14841 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61  ;.    data = pPa
14842 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69  ge->aData;.    i
14843 66 28 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70  f( decodeFlags(p
14844 50 61 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29  Page, data[hdr])
14845 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
14846 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
14847 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
14848 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26  pageSize>=512 &&
14849 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d   pBt->pageSize<=
1484a 33 32 37 36 38 20 29 3b 0a 20 20 20 20 70 50 61  32768 );.    pPa
1484b 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70  ge->maskPage = p
1484c 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31  Bt->pageSize - 1
1484d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76  ;.    pPage->nOv
1484e 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20  erflow = 0;.    
1484f 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74  usableSize = pBt
14850 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  ->usableSize;.  
14851 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66    pPage->cellOff
14852 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  set = cellOffset
14853 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a   = hdr + 12 - 4*
14854 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20  pPage->leaf;.   
14855 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28   top = get2byte(
14856 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20  &data[hdr+5]);. 
14857 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20     pPage->nCell 
14858 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
14859 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66  [hdr+3]);.    if
1485a 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d  ( pPage->nCell>M
1485b 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20  X_CELL(pBt) ){. 
1485c 20 20 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20       /* To many 
1485d 63 65 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67  cells for a sing
1485e 6c 65 20 70 61 67 65 2e 20 20 54 68 65 20 70 61  le page.  The pa
1485f 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75  ge must be corru
14860 70 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  pt */.      retu
14861 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
14862 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20  T_BKPT;.    }.  
14863 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  .    /* Compute 
14864 74 68 65 20 74 6f 74 61 6c 20 66 72 65 65 20 73  the total free s
14865 70 61 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65  pace on the page
14866 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 67 65 74   */.    pc = get
14867 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
14868 31 5d 29 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d  1]);.    nFree =
14869 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74   data[hdr+7] + t
1486a 6f 70 20 2d 20 28 63 65 6c 6c 4f 66 66 73 65 74  op - (cellOffset
1486b 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c   + 2*pPage->nCel
1486c 6c 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  l);.    while( p
1486d 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 31 36  c>0 ){.      u16
1486e 20 6e 65 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20   next, size;.   
1486f 20 20 20 69 66 28 20 70 63 3e 75 73 61 62 6c 65     if( pc>usable
14870 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20  Size-4 ){.      
14871 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 20    /* Free block 
14872 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20  is off the page 
14873 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
14874 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
14875 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a  _BKPT; .      }.
14876 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74        next = get
14877 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29  2byte(&data[pc])
14878 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67  ;.      size = g
14879 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  et2byte(&data[pc
1487a 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +2]);.      if( 
1487b 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c 3d  next>0 && next<=
1487c 70 63 2b 73 69 7a 65 2b 33 20 29 7b 0a 20 20 20  pc+size+3 ){.   
1487d 20 20 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f       /* Free blo
1487e 63 6b 73 20 6d 75 73 74 20 62 65 20 69 6e 20 61  cks must be in a
1487f 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  ccending order *
14880 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
14881 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
14882 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20  BKPT; .      }. 
14883 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 69       nFree += si
14884 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 6e  ze;.      pc = n
14885 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ext;.    }.    p
14886 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75  Page->nFree = (u
14887 31 36 29 6e 46 72 65 65 3b 0a 20 20 20 20 69 66  16)nFree;.    if
14888 28 20 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65 53  ( nFree>=usableS
14889 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ize ){.      /* 
1488a 46 72 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f  Free space canno
1488b 74 20 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70  t exceed total p
1488c 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20  age size */.    
1488d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1488e 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20  CORRUPT_BKPT; . 
1488f 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 2f 2a     }..#if 0.  /*
14890 20 43 68 65 63 6b 20 74 68 61 74 20 61 6c 6c 20   Check that all 
14891 74 68 65 20 6f 66 66 73 65 74 73 20 69 6e 20 74  the offsets in t
14892 68 65 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61  he cell offset a
14893 72 72 61 79 20 61 72 65 20 77 69 74 68 69 6e 20  rray are within 
14894 72 61 6e 67 65 2e 20 0a 20 20 2a 2a 20 0a 20 20  range. .  ** .  
14895 2a 2a 20 4f 6d 69 74 74 69 6e 67 20 74 68 69 73  ** Omitting this
14896 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65   consistency che
14897 63 6b 20 61 6e 64 20 75 73 69 6e 67 20 74 68 65  ck and using the
14898 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65   pPage->maskPage
14899 20 6d 61 73 6b 0a 20 20 2a 2a 20 74 6f 20 70 72   mask.  ** to pr
1489a 65 76 65 6e 74 20 6f 76 65 72 72 75 6e 6e 69 6e  event overrunnin
1489b 67 20 74 68 65 20 70 61 67 65 20 62 75 66 66 65  g the page buffe
1489c 72 20 69 6e 20 66 69 6e 64 43 65 6c 6c 28 29 20  r in findCell() 
1489d 72 65 73 75 6c 74 73 20 69 6e 20 61 0a 20 20 2a  results in a.  *
1489e 2a 20 32 2e 35 25 20 70 65 72 66 6f 72 6d 61 6e  * 2.5% performan
1489f 63 65 20 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20  ce gain..  */.  
148a0 7b 0a 20 20 20 20 75 38 20 2a 70 4f 66 66 3b 20  {.    u8 *pOff; 
148a1 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
148a2 6f 72 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b  or used to check
148a3 20 61 6c 6c 20 63 65 6c 6c 20 6f 66 66 73 65 74   all cell offset
148a4 73 20 61 72 65 20 69 6e 20 72 61 6e 67 65 20 2a  s are in range *
148a5 2f 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 20  /.    u8 *pEnd; 
148a6 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
148a7 72 20 74 6f 20 65 6e 64 20 6f 66 20 63 65 6c 6c  r to end of cell
148a8 20 6f 66 66 73 65 74 20 61 72 72 61 79 20 2a 2f   offset array */
148a9 0a 20 20 20 20 75 38 20 6d 61 73 6b 3b 20 20 20  .    u8 mask;   
148aa 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
148ab 20 62 69 74 73 20 74 68 61 74 20 6d 75 73 74 20   bits that must 
148ac 62 65 20 7a 65 72 6f 20 69 6e 20 4d 53 42 20 6f  be zero in MSB o
148ad 66 20 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 2a  f cell offsets *
148ae 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28  /.    mask = ~((
148af 28 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69  (u8)(pBt->pageSi
148b0 7a 65 3e 3e 38 29 29 2d 31 29 3b 0a 20 20 20 20  ze>>8))-1);.    
148b1 70 45 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c  pEnd = &data[cel
148b2 6c 4f 66 66 73 65 74 20 2b 20 70 50 61 67 65 2d  lOffset + pPage-
148b3 3e 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 20 20 66  >nCell*2];.    f
148b4 6f 72 28 70 4f 66 66 3d 26 64 61 74 61 5b 63 65  or(pOff=&data[ce
148b5 6c 6c 4f 66 66 73 65 74 5d 3b 20 70 4f 66 66 21  llOffset]; pOff!
148b6 3d 70 45 6e 64 20 26 26 20 21 28 28 2a 70 4f 66  =pEnd && !((*pOf
148b7 66 29 26 6d 61 73 6b 29 3b 20 70 4f 66 66 2b 3d  f)&mask); pOff+=
148b8 32 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 66 66  2);.    if( pOff
148b9 21 3d 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20  !=pEnd ){.      
148ba 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
148bb 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
148bc 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
148bd 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
148be 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
148bf 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
148c0 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72  /*.** Set up a r
148c1 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 74 20  aw page so that 
148c2 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20  it looks like a 
148c3 64 61 74 61 62 61 73 65 20 70 61 67 65 20 68 6f  database page ho
148c4 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72  lding.** no entr
148c5 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ies..*/.static v
148c6 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d  oid zeroPage(Mem
148c7 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74  Page *pPage, int
148c8 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67   flags){.  unsig
148c9 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d  ned char *data =
148ca 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20   pPage->aData;. 
148cb 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
148cc 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75   pPage->pBt;.  u
148cd 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68  8 hdr = pPage->h
148ce 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 20  drOffset;.  u16 
148cf 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74  first;..  assert
148d0 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  ( sqlite3PagerPa
148d1 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e  genumber(pPage->
148d2 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d  pDbPage)==pPage-
148d3 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72  >pgno );.  asser
148d4 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  t( sqlite3PagerG
148d5 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70  etExtra(pPage->p
148d6 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64  DbPage) == (void
148d7 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73  *)pPage );.  ass
148d8 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
148d9 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e  rGetData(pPage->
148da 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61  pDbPage) == data
148db 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
148dc 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
148dd 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
148de 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
148df 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
148e0 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
148e1 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28  ) );.  /*memset(
148e2 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70  &data[hdr], 0, p
148e3 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
148e4 20 68 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b   hdr);*/.  data[
148e5 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c 61  hdr] = (char)fla
148e6 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 64  gs;.  first = hd
148e7 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 67  r + 8 + 4*((flag
148e8 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f  s&PTF_LEAF)==0 ?
148e9 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26  1:0);.  memset(&
148ea 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20  data[hdr+1], 0, 
148eb 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37  4);.  data[hdr+7
148ec 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 74  ] = 0;.  put2byt
148ed 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
148ee 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29  pBt->usableSize)
148ef 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  ;.  pPage->nFree
148f0 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
148f1 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 65  ze - first;.  de
148f2 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c  codeFlags(pPage,
148f3 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65   flags);.  pPage
148f4 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68 64  ->hdrOffset = hd
148f5 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c  r;.  pPage->cell
148f6 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a  Offset = first;.
148f7 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
148f8 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ow = 0;.  assert
148f9 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e  ( pBt->pageSize>
148fa 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67  =512 && pBt->pag
148fb 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a  eSize<=32768 );.
148fc 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67    pPage->maskPag
148fd 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  e = pBt->pageSiz
148fe 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e  e - 1;.  pPage->
148ff 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61  nCell = 0;.  pPa
14900 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a  ge->isInit = 1;.
14901 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  }.../*.** Conver
14902 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 69  t a DbPage obtai
14903 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67  ned from the pag
14904 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67  er into a MemPag
14905 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 65  e used by.** the
14906 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f   btree layer..*/
14907 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 20  .static MemPage 
14908 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62  *btreePageFromDb
14909 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 62  Page(DbPage *pDb
1490a 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c  Page, Pgno pgno,
1490b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b   BtShared *pBt){
1490c 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
1490d 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71  e = (MemPage*)sq
1490e 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
1490f 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70  ra(pDbPage);.  p
14910 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71  Page->aData = sq
14911 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
14912 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50  a(pDbPage);.  pP
14913 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 70  age->pDbPage = p
14914 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 2d  DbPage;.  pPage-
14915 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 50  >pBt = pBt;.  pP
14916 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f  age->pgno = pgno
14917 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  ;.  pPage->hdrOf
14918 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 67  fset = pPage->pg
14919 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b  no==1 ? 100 : 0;
1491a 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b  .  return pPage;
1491b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61   .}../*.** Get a
1491c 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70   page from the p
1491d 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a  ager.  Initializ
1491e 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42  e the MemPage.pB
1491f 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65  t and.** MemPage
14920 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20  .aData elements 
14921 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a  if needed..**.**
14922 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e   If the noConten
14923 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69  t flag is set, i
14924 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20  t means that we 
14925 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75  do not care abou
14926 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  t.** the content
14927 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 20   of the page at 
14928 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 64  this time.  So d
14929 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 20  o not go to the 
1492a 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68  disk.** to fetch
1492b 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a   the content.  J
1492c 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 20  ust fill in the 
1492d 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72  content with zer
1492e 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49  os for now..** I
1492f 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 20  f in the future 
14930 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50  we call sqlite3P
14931 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74  agerWrite() on t
14932 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a 2a  his page, that.*
14933 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 20  * means we have 
14934 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 6f  started to be co
14935 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 6f  ncerned about co
14936 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 69  ntent and the di
14937 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c  sk.** read shoul
14938 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 20  d occur at that 
14939 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  point..*/.SQLITE
1493a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1493b 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
1493c 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  (.  BtShared *pB
1493d 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  t,       /* The 
1493e 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  btree */.  Pgno 
1493f 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  pgno,           
14940 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65  /* Number of the
14941 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a   page to fetch *
14942 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70  /.  MemPage **pp
14943 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75  Page,    /* Retu
14944 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e 20 74  rn the page in t
14945 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  his parameter */
14946 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74  .  int noContent
14947 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
14948 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74  t load page cont
14949 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  ent if true */.)
1494a 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62  {.  int rc;.  Db
1494b 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a  Page *pDbPage;..
1494c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1494d 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
1494e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63  ->mutex) );.  rc
1494f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 41   = sqlite3PagerA
14950 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67  cquire(pBt->pPag
14951 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67  er, pgno, (DbPag
14952 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f  e**)&pDbPage, no
14953 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20  Content);.  if( 
14954 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
14955 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65    *ppPage = btre
14956 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28  ePageFromDbPage(
14957 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70  pDbPage, pgno, p
14958 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Bt);.  return SQ
14959 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1495a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 61 67  * Retrieve a pag
1495b 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72  e from the pager
1495c 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 20 72   cache. If the r
1495d 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73  equested page is
1495e 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20   not.** already 
1495f 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63  in the pager cac
14960 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  he return NULL. 
14961 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d  Initialize the M
14962 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a  emPage.pBt and.*
14963 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20  * MemPage.aData 
14964 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64  elements if need
14965 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65  ed..*/.static Me
14966 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 67 65  mPage *btreePage
14967 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20  Lookup(BtShared 
14968 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  *pBt, Pgno pgno)
14969 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  {.  DbPage *pDbP
1496a 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73  age;.  assert( s
1496b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1496c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1496d 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c  .  pDbPage = sql
1496e 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
1496f 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e  pBt->pPager, pgn
14970 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67  o);.  if( pDbPag
14971 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
14972 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50  btreePageFromDbP
14973 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e  age(pDbPage, pgn
14974 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72  o, pBt);.  }.  r
14975 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
14976 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
14977 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
14978 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e  e file in pages.
14979 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 79   If there is any
1497a 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f   kind of.** erro
1497b 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69  r, return ((unsi
1497c 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f  gned int)-1)..*/
1497d 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67  .static Pgno pag
1497e 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68  erPagecount(BtSh
1497f 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e  ared *pBt){.  in
14980 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20  t nPage = -1;.  
14981 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
14982 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b  ( pBt->pPage1 );
14983 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
14984 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
14985 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67  t->pPager, &nPag
14986 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  e);.  assert( rc
14987 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e  ==SQLITE_OK || n
14988 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65  Page==-1 );.  re
14989 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65  turn (Pgno)nPage
1498a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61  ;.}../*.** Get a
1498b 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70   page from the p
1498c 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c  ager and initial
1498d 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f  ize it.  This ro
1498e 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74  utine.** is just
1498f 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77   a convenience w
14990 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65  rapper around se
14991 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a  parate calls to.
14992 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  ** sqlite3BtreeG
14993 65 74 50 61 67 65 28 29 20 61 6e 64 20 73 71 6c  etPage() and sql
14994 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
14995 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  e()..*/.static i
14996 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67  nt getAndInitPag
14997 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
14998 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Bt,          /* 
14999 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
1499a 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
1499b 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
1499c 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
1499d 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65  e to get */.  Me
1499e 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20  mPage **ppPage  
1499f 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
149a0 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72  page pointer her
149a1 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
149a2 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
149a3 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ge;..  assert( s
149a4 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
149a5 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
149a6 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29  .  if( pgno==0 )
149a7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
149a8 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
149a9 3b 20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20  ; .  }..  /* It 
149aa 69 73 20 6f 66 74 65 6e 20 74 68 65 20 63 61 73  is often the cas
149ab 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
149ac 77 65 20 77 61 6e 74 20 69 73 20 61 6c 72 65 61  we want is alrea
149ad 64 79 20 69 6e 20 63 61 63 68 65 2e 0a 20 20 2a  dy in cache..  *
149ae 2a 20 49 66 20 73 6f 2c 20 67 65 74 20 69 74 20  * If so, get it 
149af 64 69 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20  directly.  This 
149b0 73 61 76 65 73 20 75 73 20 66 72 6f 6d 20 68 61  saves us from ha
149b1 76 69 6e 67 20 74 6f 20 63 61 6c 6c 0a 20 20 2a  ving to call.  *
149b2 2a 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  * pagerPagecount
149b3 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  () to make sure 
149b4 70 67 6e 6f 20 69 73 20 77 69 74 68 69 6e 20 6c  pgno is within l
149b5 69 6d 69 74 73 2c 20 77 68 69 63 68 20 72 65 73  imits, which res
149b6 75 6c 74 73 0a 20 20 2a 2a 20 69 6e 20 61 20 6d  ults.  ** in a m
149b7 65 61 73 75 72 65 61 62 6c 65 20 70 65 72 66 6f  easureable perfo
149b8 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65  rmance improveme
149b9 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 2a 70 70 50  nts..  */.  *ppP
149ba 61 67 65 20 3d 20 70 50 61 67 65 20 3d 20 62 74  age = pPage = bt
149bb 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42  reePageLookup(pB
149bc 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20  t, pgno);.  if( 
149bd 70 50 61 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20  pPage ){.    /* 
149be 50 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20  Page is already 
149bf 69 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20  in cache */.    
149c0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
149c1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
149c2 50 61 67 65 20 6e 6f 74 20 69 6e 20 63 61 63 68  Page not in cach
149c3 65 2e 20 20 41 63 71 75 69 72 65 20 69 74 2e 20  e.  Acquire it. 
149c4 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e  */.    if( pgno>
149c5 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
149c6 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  Bt) ){.      ret
149c7 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
149c8 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a  PT_BKPT; .    }.
149c9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
149ca 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
149cb 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20  , pgno, ppPage, 
149cc 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  0);.    if( rc )
149cd 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
149ce 70 50 61 67 65 20 3d 20 2a 70 70 50 61 67 65 3b  pPage = *ppPage;
149cf 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67  .  }.  if( !pPag
149d0 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  e->isInit ){.   
149d1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
149d2 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65  eeInitPage(pPage
149d3 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21  );.  }.  if( rc!
149d4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
149d5 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
149d6 61 67 65 29 3b 0a 20 20 20 20 2a 70 70 50 61 67  age);.    *ppPag
149d7 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  e = 0;.  }.  ret
149d8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
149d9 20 52 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61   Release a MemPa
149da 67 65 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64  ge.  This should
149db 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   be called once 
149dc 66 6f 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a  for each prior.*
149dd 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  * call to sqlite
149de 33 42 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a  3BtreeGetPage..*
149df 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
149e0 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67  leasePage(MemPag
149e1 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28  e *pPage){.  if(
149e2 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73   pPage ){.    as
149e3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
149e4 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c  erflow==0 || sql
149e5 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
149e6 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62  count(pPage->pDb
149e7 50 61 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61  Page)>1 );.    a
149e8 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44  ssert( pPage->aD
149e9 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ata );.    asser
149ea 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b  t( pPage->pBt );
149eb 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
149ec 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
149ed 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  a(pPage->pDbPage
149ee 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67  ) == (void*)pPag
149ef 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  e );.    assert(
149f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
149f1 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50  Data(pPage->pDbP
149f2 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61  age)==pPage->aDa
149f3 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ta );.    assert
149f4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
149f5 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
149f6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73  >mutex) );.    s
149f7 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
149f8 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
149f9 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
149fa 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b  uring a rollback
149fb 2c 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72  , when the pager
149fc 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61   reloads informa
149fd 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61  tion into the ca
149fe 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74  che.** so that t
149ff 68 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74  he cache is rest
14a00 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67  ored to its orig
14a01 69 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68  inal state at th
14a02 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68  e start of.** th
14a03 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66  e transaction, f
14a04 6f 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73  or each page res
14a05 74 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69  tored this routi
14a06 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
14a07 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14a08 20 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20   needs to reset 
14a09 74 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73  the extra data s
14a0a 65 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e  ection at the en
14a0b 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65  d of the.** page
14a0c 20 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74   to agree with t
14a0d 68 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61  he restored data
14a0e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14a0f 20 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61   pageReinit(DbPa
14a10 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65  ge *pData){.  Me
14a11 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20  mPage *pPage;.  
14a12 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65  pPage = (MemPage
14a13 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
14a14 65 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a  etExtra(pData);.
14a15 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49    if( pPage->isI
14a16 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  nit ){.    asser
14a17 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14a18 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
14a19 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20  ->mutex) );.    
14a1a 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
14a1b 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  0;.    if( sqlit
14a1c 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f  e3PagerPageRefco
14a1d 75 6e 74 28 70 44 61 74 61 29 3e 30 20 29 7b 0a  unt(pData)>0 ){.
14a1e 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d        /* pPage m
14a1f 69 67 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74  ight not be a bt
14a20 72 65 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69  ree page;  it mi
14a21 67 68 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c  ght be an overfl
14a22 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a  ow page.      **
14a23 20 6f 72 20 70 74 72 6d 61 70 20 70 61 67 65 20   or ptrmap page 
14a24 6f 72 20 61 20 66 72 65 65 20 70 61 67 65 2e 20  or a free page. 
14a25 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c   In those cases,
14a26 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
14a27 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20       ** call to 
14a28 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
14a29 50 61 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65  Page() will like
14a2a 6c 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ly return SQLITE
14a2b 5f 43 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20  _CORRUPT..      
14a2c 2a 2a 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69  ** But no harm i
14a2d 73 20 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20  s done by this. 
14a2e 20 41 6e 64 20 69 74 20 69 73 20 76 65 72 79 20   And it is very 
14a2f 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20  important that. 
14a30 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42       ** sqlite3B
14a31 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62  treeInitPage() b
14a32 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72  e called on ever
14a33 79 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20  y btree page so 
14a34 77 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a  we make.      **
14a35 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76   the call for ev
14a36 65 72 79 20 70 61 67 65 20 74 68 61 74 20 63 6f  ery page that co
14a37 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e  mes in for re-in
14a38 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20  iting. */.      
14a39 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
14a3a 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
14a3b 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
14a3c 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  Invoke the busy 
14a3d 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74  handler for a bt
14a3e 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ree..*/.static i
14a3f 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75  nt btreeInvokeBu
14a40 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a  syHandler(void *
14a41 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65  pArg){.  BtShare
14a42 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72  d *pBt = (BtShar
14a43 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65  ed*)pArg;.  asse
14a44 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20  rt( pBt->db );. 
14a45 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
14a46 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
14a47 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
14a48 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49   return sqlite3I
14a49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72  nvokeBusyHandler
14a4a 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48  (&pBt->db->busyH
14a4b 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  andler);.}../*.*
14a4c 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73  * Open a databas
14a4d 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a  e file..** .** z
14a4e 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20  Filename is the 
14a4f 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
14a50 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a  base file.  If z
14a51 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c  Filename is NULL
14a52 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61  .** a new databa
14a53 73 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d  se with a random
14a54 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64   name is created
14a55 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79  .  This randomly
14a56 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61   named.** databa
14a57 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20  se file will be 
14a58 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c  deleted when sql
14a59 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29  ite3BtreeClose()
14a5a 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49   is called..** I
14a5b 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22  f zFilename is "
14a5c 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61  :memory:" then a
14a5d 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
14a5e 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 0a  base is created.
14a5f 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d  ** that is autom
14a60 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79  atically destroy
14a61 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c  ed when it is cl
14a62 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  osed..*/.SQLITE_
14a63 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14a64 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20  te3BtreeOpen(.  
14a65 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
14a66 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20  ename,  /* Name 
14a67 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74  of the file cont
14a68 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65  aining the BTree
14a69 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73   database */.  s
14a6a 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
14a6b 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69         /* Associ
14a6c 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 61  ated database ha
14a6d 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20  ndle */.  Btree 
14a6e 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 20  **ppBtree,      
14a6f 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
14a70 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74  new Btree object
14a71 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
14a72 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
14a73 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
14a74 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76  tions */.  int v
14a75 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20  fsFlags         
14a76 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73     /* Flags pass
14a77 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71  ed through to sq
14a78 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28  lite3_vfs.xOpen(
14a79 29 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  ) */.){.  sqlite
14a7a 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20  3_vfs *pVfs;    
14a7b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
14a7c 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 74  VFS to use for t
14a7d 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42  his btree */.  B
14a7e 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30  tShared *pBt = 0
14a7f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
14a80 20 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 20   Shared part of 
14a81 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  btree structure 
14a82 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20  */.  Btree *p;  
14a83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a84 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f      /* Handle to
14a85 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c   return */.  sql
14a86 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
14a87 78 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50  xOpen = 0;  /* P
14a88 72 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 63  revents a race c
14a89 6f 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74  ondition. Ticket
14a8a 20 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20   #3537 */.  int 
14a8b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
14a8c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
14a8d 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74  sult code from t
14a8e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  his function */.
14a8f 20 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20    u8 nReserve;  
14a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a91 20 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73   /* Byte of unus
14a92 65 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68  ed space on each
14a93 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67   page */.  unsig
14a94 6e 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 64  ned char zDbHead
14a95 65 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74  er[100];  /* Dat
14a96 61 62 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e  abase header con
14a97 74 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65  tent */..  /* Se
14a98 74 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 69  t the variable i
14a99 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66  sMemdb to true f
14a9a 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  or an in-memory 
14a9b 64 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20  database, or .  
14a9c 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66  ** false for a f
14a9d 69 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62 61  ile-based databa
14a9e 73 65 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20  se. This symbol 
14a9f 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
14aa0 20 69 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20   if.  ** either 
14aa1 6f 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61  of the shared-da
14aa2 74 61 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d  ta or autovacuum
14aa3 20 66 65 61 74 75 72 65 73 20 61 72 65 20 63 6f   features are co
14aa4 6d 70 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74  mpiled .  ** int
14aa5 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20  o the library.. 
14aa6 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64   */.#if !defined
14aa7 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  (SQLITE_OMIT_SHA
14aa8 52 45 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64  RED_CACHE) || !d
14aa9 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
14aaa 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20  IT_AUTOVACUUM). 
14aab 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f   #ifdef SQLITE_O
14aac 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20  MIT_MEMORYDB.   
14aad 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d   const int isMem
14aae 64 62 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a  db = 0;.  #else.
14aaf 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73      const int is
14ab0 4d 65 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d  Memdb = zFilenam
14ab1 65 20 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69  e && !strcmp(zFi
14ab2 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79  lename, ":memory
14ab3 3a 22 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65  :");.  #endif.#e
14ab4 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20  ndif..  assert( 
14ab5 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  db!=0 );.  asser
14ab6 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14ab7 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
14ab8 20 29 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 62   );..  pVfs = db
14ab9 2d 3e 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 71  ->pVfs;.  p = sq
14aba 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
14abb 73 69 7a 65 6f 66 28 42 74 72 65 65 29 29 3b 0a  sizeof(Btree));.
14abc 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20    if( !p ){.    
14abd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
14abe 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e  MEM;.  }.  p->in
14abf 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f  Trans = TRANS_NO
14ac0 4e 45 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62  NE;.  p->db = db
14ac1 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  ;..#if !defined(
14ac2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
14ac3 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65  ED_CACHE) && !de
14ac4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
14ac5 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20  T_DISKIO).  /*. 
14ac6 20 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65   ** If this Btre
14ac7 65 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65  e is a candidate
14ac8 20 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68   for shared cach
14ac9 65 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61  e, try to find a
14aca 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20  n.  ** existing 
14acb 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20  BtShared object 
14acc 74 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72  that we can shar
14acd 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66  e with.  */.  if
14ace 28 20 69 73 4d 65 6d 64 62 3d 3d 30 0a 20 20 20  ( isMemdb==0.   
14acf 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20  && (db->flags & 
14ad0 53 51 4c 49 54 45 5f 56 74 61 62 29 3d 3d 30 0a  SQLITE_Vtab)==0.
14ad1 20 20 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20     && zFilename 
14ad2 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 0a  && zFilename[0].
14ad3 20 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c    ){.    if( sql
14ad4 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
14ad5 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62  .sharedCacheEnab
14ad6 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  led ){.      int
14ad7 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d   nFullPathname =
14ad8 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
14ad9 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20  e+1;.      char 
14ada 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d  *zFullPathname =
14adb 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e   sqlite3Malloc(n
14adc 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
14add 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
14ade 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b  ex *mutexShared;
14adf 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62  .      p->sharab
14ae0 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 64 62  le = 1;.      db
14ae1 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
14ae2 45 5f 53 68 61 72 65 64 43 61 63 68 65 3b 0a 20  E_SharedCache;. 
14ae3 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c 6c 50       if( !zFullP
14ae4 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20  athname ){.     
14ae5 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
14ae6 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  p);.        retu
14ae7 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
14ae8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
14ae9 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68  qlite3OsFullPath
14aea 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65  name(pVfs, zFile
14aeb 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e  name, nFullPathn
14aec 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61  ame, zFullPathna
14aed 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78  me);.      mutex
14aee 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75  Open = sqlite3Mu
14aef 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
14af0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45  MUTEX_STATIC_OPE
14af1 4e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  N);.      sqlite
14af2 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
14af3 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20  texOpen);.      
14af4 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73 71  mutexShared = sq
14af5 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
14af6 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
14af7 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20  TIC_MASTER);.   
14af8 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
14af9 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72  _enter(mutexShar
14afa 65 64 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  ed);.      for(p
14afb 42 74 3d 47 4c 4f 42 41 4c 28 42 74 53 68 61 72  Bt=GLOBAL(BtShar
14afc 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65  ed*,sqlite3Share
14afd 64 43 61 63 68 65 4c 69 73 74 29 3b 20 70 42 74  dCacheList); pBt
14afe 3b 20 70 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74  ; pBt=pBt->pNext
14aff 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
14b00 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29  t( pBt->nRef>0 )
14b01 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d  ;.        if( 0=
14b02 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74  =strcmp(zFullPat
14b03 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61  hname, sqlite3Pa
14b04 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d  gerFilename(pBt-
14b05 3e 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20  >pPager)).      
14b06 20 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71             && sq
14b07 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 70 42  lite3PagerVfs(pB
14b08 74 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73  t->pPager)==pVfs
14b09 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d   ){.          p-
14b0a 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20  >pBt = pBt;.    
14b0b 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b        pBt->nRef+
14b0c 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  +;.          bre
14b0d 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
14b0e 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
14b0f 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
14b10 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20  mutexShared);.  
14b11 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
14b12 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b  (zFullPathname);
14b13 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51  .    }.#ifdef SQ
14b14 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65  LITE_DEBUG.    e
14b15 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e  lse{.      /* In
14b16 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20   debug mode, we 
14b17 6d 61 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74  mark all persist
14b18 65 6e 74 20 64 61 74 61 62 61 73 65 73 20 61 73  ent databases as
14b19 20 73 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20   sharable.      
14b1a 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65  ** even when the
14b1b 79 20 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73  y are not.  This
14b1c 20 65 78 65 72 63 69 73 65 73 20 74 68 65 20 6c   exercises the l
14b1d 6f 63 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a  ocking code and.
14b1e 20 20 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d        ** gives m
14b1f 6f 72 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20  ore opportunity 
14b20 66 6f 72 20 61 73 73 65 72 74 73 28 73 71 6c 69  for asserts(sqli
14b21 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
14b22 29 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65  ).      ** state
14b23 6d 65 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f  ments to find lo
14b24 63 6b 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a  cking problems..
14b25 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70        */.      p
14b26 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a  ->sharable = 1;.
14b27 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
14b28 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42  .#endif.  if( pB
14b29 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20  t==0 ){.    /*. 
14b2a 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77     ** The follow
14b2b 69 6e 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65  ing asserts make
14b2c 20 73 75 72 65 20 74 68 61 74 20 73 74 72 75 63   sure that struc
14b2d 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68  tures used by th
14b2e 65 20 62 74 72 65 65 20 61 72 65 0a 20 20 20 20  e btree are.    
14b2f 2a 2a 20 74 68 65 20 72 69 67 68 74 20 73 69 7a  ** the right siz
14b30 65 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 67  e.  This is to g
14b31 75 61 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a  uard against siz
14b32 65 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 72  e changes that r
14b33 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65  esult.    ** whe
14b34 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61  n compiling on a
14b35 20 64 69 66 66 65 72 65 6e 74 20 61 72 63 68 69   different archi
14b36 74 65 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a  tecture..    */.
14b37 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
14b38 6f 66 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69  of(i64)==8 || si
14b39 7a 65 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a  zeof(i64)==4 );.
14b3a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
14b3b 6f 66 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69  of(u64)==8 || si
14b3c 7a 65 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a  zeof(u64)==4 );.
14b3d 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
14b3e 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20  of(u32)==4 );.  
14b3f 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
14b40 28 75 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20  (u16)==2 );.    
14b41 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50  assert( sizeof(P
14b42 67 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20  gno)==4 );.  .  
14b43 20 20 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d    pBt = sqlite3M
14b44 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
14b45 66 28 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69  f(*pBt) );.    i
14b46 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20  f( pBt==0 ){.   
14b47 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
14b48 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f  OMEM;.      goto
14b49 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b   btree_open_out;
14b4a 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
14b4b 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
14b4c 28 70 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61  (pVfs, &pBt->pPa
14b4d 67 65 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a  ger, zFilename,.
14b4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b4f 20 20 20 20 20 20 20 20 20 20 45 58 54 52 41 5f            EXTRA_
14b50 53 49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73  SIZE, flags, vfs
14b51 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20  Flags);.    if( 
14b52 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
14b53 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
14b54 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c 65  te3PagerReadFile
14b55 68 65 61 64 65 72 28 70 42 74 2d 3e 70 50 61 67  header(pBt->pPag
14b56 65 72 2c 73 69 7a 65 6f 66 28 7a 44 62 48 65 61  er,sizeof(zDbHea
14b57 64 65 72 29 2c 7a 44 62 48 65 61 64 65 72 29 3b  der),zDbHeader);
14b58 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
14b59 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
14b5a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65        goto btree
14b5b 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d  _open_out;.    }
14b5c 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
14b5d 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28  rSetBusyhandler(
14b5e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 62 74 72  pBt->pPager, btr
14b5f 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64  eeInvokeBusyHand
14b60 6c 65 72 2c 20 70 42 74 29 3b 0a 20 20 20 20 70  ler, pBt);.    p
14b61 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a  ->pBt = pBt;.  .
14b62 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
14b63 53 65 74 52 65 69 6e 69 74 65 72 28 70 42 74 2d  SetReiniter(pBt-
14b64 3e 70 50 61 67 65 72 2c 20 70 61 67 65 52 65 69  >pPager, pageRei
14b65 6e 69 74 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70  nit);.    pBt->p
14b66 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20  Cursor = 0;.    
14b67 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b  pBt->pPage1 = 0;
14b68 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e  .    pBt->readOn
14b69 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ly = sqlite3Page
14b6a 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d  rIsreadonly(pBt-
14b6b 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42  >pPager);.    pB
14b6c 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65  t->pageSize = ge
14b6d 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65  t2byte(&zDbHeade
14b6e 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20  r[16]);.    if( 
14b6f 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31  pBt->pageSize<51
14b70 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69  2 || pBt->pageSi
14b71 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ze>SQLITE_MAX_PA
14b72 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20  GE_SIZE.        
14b73 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53   || ((pBt->pageS
14b74 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65  ize-1)&pBt->page
14b75 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Size)!=0 ){.    
14b76 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
14b77 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
14b78 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
14b79 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ze(pBt->pPager, 
14b7a 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b  &pBt->pageSize);
14b7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14b7c 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
14b7d 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
14b7e 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d  magic name ":mem
14b7f 6f 72 79 3a 22 20 77 69 6c 6c 20 63 72 65 61 74  ory:" will creat
14b80 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  e an in-memory d
14b81 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 20 20  atabase, then.  
14b82 20 20 20 20 2a 2a 20 6c 65 61 76 65 20 74 68 65      ** leave the
14b83 20 61 75 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65   autoVacuum mode
14b84 20 61 74 20 30 20 28 64 6f 20 6e 6f 74 20 61 75   at 0 (do not au
14b85 74 6f 2d 76 61 63 75 75 6d 29 2c 20 65 76 65 6e  to-vacuum), even
14b86 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c   if.      ** SQL
14b87 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
14b88 56 41 43 55 55 4d 20 69 73 20 74 72 75 65 2e 20  VACUUM is true. 
14b89 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
14b8a 64 2c 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 53  d, if.      ** S
14b8b 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
14b8c 59 44 42 20 68 61 73 20 62 65 65 6e 20 64 65 66  YDB has been def
14b8d 69 6e 65 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d  ined, then ":mem
14b8e 6f 72 79 3a 22 20 69 73 20 6a 75 73 74 20 61 0a  ory:" is just a.
14b8f 20 20 20 20 20 20 2a 2a 20 72 65 67 75 6c 61 72        ** regular
14b90 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74   file-name. In t
14b91 68 69 73 20 63 61 73 65 20 74 68 65 20 61 75 74  his case the aut
14b92 6f 2d 76 61 63 75 75 6d 20 61 70 70 6c 69 65 73  o-vacuum applies
14b93 20 61 73 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a   as per normal..
14b94 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
14b95 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20  f( zFilename && 
14b96 21 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20  !isMemdb ){.    
14b97 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63      pBt->autoVac
14b98 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45  uum = (SQLITE_DE
14b99 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d  FAULT_AUTOVACUUM
14b9a 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20   ? 1 : 0);.     
14b9b 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75     pBt->incrVacu
14b9c 75 6d 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46  um = (SQLITE_DEF
14b9d 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d  AULT_AUTOVACUUM=
14b9e 3d 32 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20  =2 ? 1 : 0);.   
14b9f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
14ba0 20 20 6e 52 65 73 65 72 76 65 20 3d 20 30 3b 0a    nReserve = 0;.
14ba1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
14ba2 20 6e 52 65 73 65 72 76 65 20 3d 20 7a 44 62 48   nReserve = zDbH
14ba3 65 61 64 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20  eader[20];.     
14ba4 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69   pBt->pageSizeFi
14ba5 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66  xed = 1;.#ifndef
14ba6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
14ba7 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 70 42  OVACUUM.      pB
14ba8 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20  t->autoVacuum = 
14ba9 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 48 65  (get4byte(&zDbHe
14baa 61 64 65 72 5b 33 36 20 2b 20 34 2a 34 5d 29 3f  ader[36 + 4*4])?
14bab 31 3a 30 29 3b 0a 20 20 20 20 20 20 70 42 74 2d  1:0);.      pBt-
14bac 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67  >incrVacuum = (g
14bad 65 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 64  et4byte(&zDbHead
14bae 65 72 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a  er[36 + 7*4])?1:
14baf 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  0);.#endif.    }
14bb0 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65  .    pBt->usable
14bb1 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65  Size = pBt->page
14bb2 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b  Size - nReserve;
14bb3 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42  .    assert( (pB
14bb4 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29  t->pageSize & 7)
14bb5 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74  ==0 );  /* 8-byt
14bb6 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70  e alignment of p
14bb7 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 20 73  ageSize */.    s
14bb8 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
14bb9 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67  gesize(pBt->pPag
14bba 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69  er, &pBt->pageSi
14bbb 7a 65 29 3b 0a 20 20 20 0a 23 69 66 20 21 64 65  ze);.   .#if !de
14bbc 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
14bbd 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20  T_SHARED_CACHE) 
14bbe 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
14bbf 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a  TE_OMIT_DISKIO).
14bc0 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e      /* Add the n
14bc1 65 77 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  ew BtShared obje
14bc2 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64  ct to the linked
14bc3 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65 20 42   list sharable B
14bc4 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f  tShareds..    */
14bc5 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72  .    if( p->shar
14bc6 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71  able ){.      sq
14bc7 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
14bc8 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20  exShared;.      
14bc9 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  pBt->nRef = 1;. 
14bca 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64       mutexShared
14bcb 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
14bcc 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
14bcd 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
14bce 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  ;.      if( SQLI
14bcf 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
14bd0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
14bd1 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20  nfig.bCoreMutex 
14bd2 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  ){.        pBt->
14bd3 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
14bd4 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
14bd5 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20  _MUTEX_FAST);.  
14bd6 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d        if( pBt->m
14bd7 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20  utex==0 ){.     
14bd8 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
14bd9 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
14bda 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
14bdb 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ed = 0;.        
14bdc 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65    goto btree_ope
14bdd 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d  n_out;.        }
14bde 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
14bdf 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
14be0 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b  er(mutexShared);
14be1 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78  .      pBt->pNex
14be2 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61  t = GLOBAL(BtSha
14be3 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
14be4 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20  edCacheList);.  
14be5 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61      GLOBAL(BtSha
14be6 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
14be7 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70  edCacheList) = p
14be8 42 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  Bt;.      sqlite
14be9 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
14bea 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20  texShared);.    
14beb 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69  }.#endif.  }..#i
14bec 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
14bed 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
14bee 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64  CHE) && !defined
14bef 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53  (SQLITE_OMIT_DIS
14bf0 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65  KIO).  /* If the
14bf1 20 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20   new Btree uses 
14bf2 61 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68  a sharable pBtSh
14bf3 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20  ared, then link 
14bf4 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72  the new.  ** Btr
14bf5 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74  ee into the list
14bf6 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65   of all sharable
14bf7 20 42 74 72 65 65 73 20 66 6f 72 20 74 68 65 20   Btrees for the 
14bf8 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  same connection.
14bf9 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69  .  ** The list i
14bfa 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64  s kept in ascend
14bfb 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 42 74  ing order by pBt
14bfc 20 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20   address..  */. 
14bfd 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65   if( p->sharable
14bfe 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
14bff 20 20 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a     Btree *pSib;.
14c00 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
14c01 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
14c02 20 20 20 20 69 66 28 20 28 70 53 69 62 20 3d 20      if( (pSib = 
14c03 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21  db->aDb[i].pBt)!
14c04 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72  =0 && pSib->shar
14c05 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
14c06 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72  while( pSib->pPr
14c07 65 76 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69  ev ){ pSib = pSi
14c08 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20  b->pPrev; }.    
14c09 20 20 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70      if( p->pBt<p
14c0a 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20  Sib->pBt ){.    
14c0b 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
14c0c 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20   pSib;.         
14c0d 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20   p->pPrev = 0;. 
14c0e 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70           pSib->p
14c0f 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20  Prev = p;.      
14c10 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
14c11 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e     while( pSib->
14c12 70 4e 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70  pNext && pSib->p
14c13 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74  Next->pBt<p->pBt
14c14 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
14c15 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65  pSib = pSib->pNe
14c16 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  xt;.          }.
14c17 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65            p->pNe
14c18 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74  xt = pSib->pNext
14c19 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  ;.          p->p
14c1a 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20  Prev = pSib;.   
14c1b 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e         if( p->pN
14c1c 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ext ){.         
14c1d 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72     p->pNext->pPr
14c1e 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  ev = p;.        
14c1f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53    }.          pS
14c20 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20  ib->pNext = p;. 
14c21 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14c22 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
14c23 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
14c24 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b  .  *ppBtree = p;
14c25 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74  ..btree_open_out
14c26 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  :.  if( rc!=SQLI
14c27 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
14c28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61   pBt && pBt->pPa
14c29 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ger ){.      sql
14c2a 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70  ite3PagerClose(p
14c2b 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
14c2c 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
14c2d 72 65 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71  ree(pBt);.    sq
14c2e 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
14c2f 20 20 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b     *ppBtree = 0;
14c30 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 74 65 78  .  }.  if( mutex
14c31 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 73 73 65  Open ){.    asse
14c32 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14c33 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f 70 65 6e  x_held(mutexOpen
14c34 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) );.    sqlite3
14c35 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
14c36 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 20 72  exOpen);.  }.  r
14c37 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
14c38 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  ** Decrement the
14c39 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63   BtShared.nRef c
14c3a 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 74  ounter.  When it
14c3b 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a 2a   reaches zero,.*
14c3c 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 74 53  * remove the BtS
14c3d 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20  hared structure 
14c3e 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 6e 67  from the sharing
14c3f 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 0a 2a   list.  Return.*
14c40 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 42 74  * true if the Bt
14c41 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e  Shared.nRef coun
14c42 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 72 6f  ter reaches zero
14c43 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 66   and return.** f
14c44 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 74  alse if it is st
14c45 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2f  ill positive..*/
14c46 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f  .static int remo
14c47 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73  veFromSharingLis
14c48 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  t(BtShared *pBt)
14c49 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
14c4a 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
14c4b 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  HE.  sqlite3_mut
14c4c 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 42  ex *pMaster;.  B
14c4d 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a  tShared *pList;.
14c4e 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20    int removed = 
14c4f 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  0;..  assert( sq
14c50 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
14c51 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
14c52 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73  );.  pMaster = s
14c53 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
14c54 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
14c55 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20  ATIC_MASTER);.  
14c56 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
14c57 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ter(pMaster);.  
14c58 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69  pBt->nRef--;.  i
14c59 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20  f( pBt->nRef<=0 
14c5a 29 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41  ){.    if( GLOBA
14c5b 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69  L(BtShared*,sqli
14c5c 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
14c5d 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20  st)==pBt ){.    
14c5e 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65    GLOBAL(BtShare
14c5f 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
14c60 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74  CacheList) = pBt
14c61 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c  ->pNext;.    }el
14c62 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20  se{.      pList 
14c63 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65  = GLOBAL(BtShare
14c64 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
14c65 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20  CacheList);.    
14c66 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28    while( ALWAYS(
14c67 70 4c 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d  pList) && pList-
14c68 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20  >pNext!=pBt ){. 
14c69 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69         pList=pLi
14c6a 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  st->pNext;.     
14c6b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57   }.      if( ALW
14c6c 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20  AYS(pList) ){.  
14c6d 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65        pList->pNe
14c6e 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b  xt = pBt->pNext;
14c6f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
14c70 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48     if( SQLITE_TH
14c71 52 45 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20  READSAFE ){.    
14c72 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
14c73 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29  free(pBt->mutex)
14c74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f  ;.    }.    remo
14c75 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73  ved = 1;.  }.  s
14c76 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
14c77 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72  ve(pMaster);.  r
14c78 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23  eturn removed;.#
14c79 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b  else.  return 1;
14c7a 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
14c7b 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e   Make sure pBt->
14c7c 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73  pTmpSpace points
14c7d 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f   to an allocatio
14c7e 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c  n of .** MX_CELL
14c7f 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74 65 73  _SIZE(pBt) bytes
14c80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14c81 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61   allocateTempSpa
14c82 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ce(BtShared *pBt
14c83 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70  ){.  if( !pBt->p
14c84 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20  TmpSpace ){.    
14c85 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d  pBt->pTmpSpace =
14c86 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c   sqlite3PageMall
14c87 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  oc( pBt->pageSiz
14c88 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  e );.  }.}../*.*
14c89 2a 20 46 72 65 65 20 74 68 65 20 70 42 74 2d 3e  * Free the pBt->
14c8a 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 61  pTmpSpace alloca
14c8b 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tion.*/.static v
14c8c 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70 61 63  oid freeTempSpac
14c8d 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  e(BtShared *pBt)
14c8e 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46  {.  sqlite3PageF
14c8f 72 65 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70  ree( pBt->pTmpSp
14c90 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e 70 54 6d  ace);.  pBt->pTm
14c91 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f  pSpace = 0;.}../
14c92 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70  *.** Close an op
14c93 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  en database and 
14c94 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 63  invalidate all c
14c95 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ursors..*/.SQLIT
14c96 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14c97 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
14c98 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53  Btree *p){.  BtS
14c99 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
14c9a 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  pBt;.  BtCursor 
14c9b 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f  *pCur;..  /* Clo
14c9c 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f  se all cursors o
14c9d 70 65 6e 65 64 20 76 69 61 20 74 68 69 73 20 68  pened via this h
14c9e 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73  andle.  */.  ass
14c9f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14ca0 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
14ca1 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
14ca2 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
14ca3 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e  .  pBt->db = p->
14ca4 64 62 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74  db;.  pCur = pBt
14ca5 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69  ->pCursor;.  whi
14ca6 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20  le( pCur ){.    
14ca7 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d  BtCursor *pTmp =
14ca8 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20   pCur;.    pCur 
14ca9 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20  = pCur->pNext;. 
14caa 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74     if( pTmp->pBt
14cab 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20  ree==p ){.      
14cac 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
14cad 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20  eCursor(pTmp);. 
14cae 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52     }.  }..  /* R
14caf 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69  ollback any acti
14cb0 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  ve transaction a
14cb1 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64  nd free the hand
14cb2 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  le structure..  
14cb3 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73  ** The call to s
14cb4 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
14cb5 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20  ack() drops any 
14cb6 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64  table-locks held
14cb7 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61   by.  ** this ha
14cb8 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ndle..  */.  sql
14cb9 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63  ite3BtreeRollbac
14cba 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  k(p);.  sqlite3B
14cbb 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20  treeLeave(p);.. 
14cbc 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
14cbd 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74   still other out
14cbe 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e  standing referen
14cbf 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65  ces to the share
14cc0 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72  d-btree.  ** str
14cc1 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e  ucture, return n
14cc2 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65  ow. The remainde
14cc3 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64  r of this proced
14cc4 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a  ure cleans .  **
14cc5 20 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62   up the shared-b
14cc6 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  tree..  */.  ass
14cc7 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  ert( p->wantToLo
14cc8 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b  ck==0 && p->lock
14cc9 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21  ed==0 );.  if( !
14cca 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72  p->sharable || r
14ccb 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67  emoveFromSharing
14ccc 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20  List(pBt) ){.   
14ccd 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e   /* The pBt is n
14cce 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20  o longer on the 
14ccf 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f  sharing list, so
14cd0 20 77 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20   we can access. 
14cd1 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74     ** it without
14cd2 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20   having to hold 
14cd3 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a  the mutex..    *
14cd4 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f  *.    ** Clean o
14cd5 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68  ut and delete th
14cd6 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
14cd7 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  t..    */.    as
14cd8 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72  sert( !pBt->pCur
14cd9 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  sor );.    sqlit
14cda 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74  e3PagerClose(pBt
14cdb 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  ->pPager);.    i
14cdc 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68  f( pBt->xFreeSch
14cdd 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68  ema && pBt->pSch
14cde 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74  ema ){.      pBt
14cdf 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42  ->xFreeSchema(pB
14ce0 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  t->pSchema);.   
14ce1 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
14ce2 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65 6d 61  ree(pBt->pSchema
14ce3 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53  );.    freeTempS
14ce4 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73  pace(pBt);.    s
14ce5 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29  qlite3_free(pBt)
14ce6 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
14ce7 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
14ce8 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74  D_CACHE.  assert
14ce9 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
14cea 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
14ceb 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a  p->locked==0 );.
14cec 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29    if( p->pPrev )
14ced 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74   p->pPrev->pNext
14cee 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69   = p->pNext;.  i
14cef 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d  f( p->pNext ) p-
14cf0 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20  >pNext->pPrev = 
14cf1 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66  p->pPrev;.#endif
14cf2 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
14cf3 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
14cf4 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
14cf5 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d  * Change the lim
14cf6 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72  it on the number
14cf7 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65   of pages allowe
14cf8 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a  d in the cache..
14cf9 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  **.** The maximu
14cfa 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68  m number of cach
14cfb 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74  e pages is set t
14cfc 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a  o the absolute.*
14cfd 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67  * value of mxPag
14cfe 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73  e.  If mxPage is
14cff 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70   negative, the p
14d00 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65  ager will.** ope
14d01 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75  rate asynchronou
14d02 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f  sly - it will no
14d03 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79  t stop to do fsy
14d04 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75  nc()s.** to insu
14d05 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74  re data is writt
14d06 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73  en to the disk s
14d07 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a  urface before.**
14d08 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72   continuing.  Tr
14d09 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c  ansactions still
14d0a 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f   work if synchro
14d0b 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20  nous is off,.** 
14d0c 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
14d0d 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75   cannot be corru
14d0e 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f  pted if this pro
14d0f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e  gram.** crashes.
14d10 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65    But if the ope
14d11 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72  rating system cr
14d12 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69  ashes or there i
14d13 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70  s.** an abrupt p
14d14 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65  ower failure whe
14d15 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  n synchronous is
14d16 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61   off, the databa
14d17 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c  se.** could be l
14d18 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73  eft in an incons
14d19 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63  istent and unrec
14d1a 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a  overable state..
14d1b 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69  ** Synchronous i
14d1c 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20  s on by default 
14d1d 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  so database corr
14d1e 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a  uption is not.**
14d1f 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72   normally a worr
14d20 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
14d21 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14d22 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
14d23 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  e(Btree *p, int 
14d24 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61  mxPage){.  BtSha
14d25 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
14d26 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  t;.  assert( sql
14d27 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14d28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
14d29 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
14d2a 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74  nter(p);.  sqlit
14d2b 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73  e3PagerSetCaches
14d2c 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ize(pBt->pPager,
14d2d 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69   mxPage);.  sqli
14d2e 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
14d2f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
14d30 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
14d31 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 61  hange the way da
14d32 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20  ta is synced to 
14d33 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f  disk in order to
14d34 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63   increase or dec
14d35 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c  rease.** how wel
14d36 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72  l the database r
14d37 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 75  esists damage du
14d38 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20  e to OS crashes 
14d39 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69  and power.** fai
14d3a 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20  lures.  Level 1 
14d3b 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61  is the same as a
14d3c 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20  synchronous (no 
14d3d 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e  syncs() occur an
14d3e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20  d.** there is a 
14d3f 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79  high probability
14d40 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76   of damage)  Lev
14d41 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 61  el 2 is the defa
14d42 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69  ult.  There.** i
14d43 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74  s a very low but
14d44 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62   non-zero probab
14d45 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e  ility of damage.
14d46 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65    Level 3 reduce
14d47 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69  s the.** probabi
14d48 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74  lity of damage t
14d49 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20  o near zero but 
14d4a 77 69 74 68 20 61 20 77 72 69 74 65 20 70 65 72  with a write per
14d4b 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69  formance reducti
14d4c 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
14d4d 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
14d4e 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f  _PRAGMAS.SQLITE_
14d4f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14d50 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74  te3BtreeSetSafet
14d51 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c  yLevel(Btree *p,
14d52 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20   int level, int 
14d53 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53  fullSync){.  BtS
14d54 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
14d55 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73  pBt;.  assert( s
14d56 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14d57 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
14d58 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
14d59 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c  eEnter(p);.  sql
14d5a 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65  ite3PagerSetSafe
14d5b 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61  tyLevel(pBt->pPa
14d5c 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c  ger, level, full
14d5d 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  Sync);.  sqlite3
14d5e 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
14d5f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
14d60 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  K;.}.#endif../*.
14d61 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
14d62 66 20 74 68 65 20 67 69 76 65 6e 20 62 74 72 65  f the given btre
14d63 65 20 69 73 20 73 65 74 20 74 6f 20 73 61 66 65  e is set to safe
14d64 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20  ty level 1.  In 
14d65 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20  other.** words, 
14d66 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e  return TRUE if n
14d67 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20  o sync() occurs 
14d68 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65  on the disk file
14d69 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
14d6a 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14d6b 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65  BtreeSyncDisable
14d6c 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42  d(Btree *p){.  B
14d6d 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
14d6e 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b  ->pBt;.  int rc;
14d6f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
14d70 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
14d71 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20  >db->mutex) );  
14d72 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
14d73 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72  nter(p);.  asser
14d74 74 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70  t( pBt && pBt->p
14d75 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20  Pager );.  rc = 
14d76 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79  sqlite3PagerNosy
14d77 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  nc(pBt->pPager);
14d78 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
14d79 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
14d7a 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65  n rc;.}..#if !de
14d7b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
14d7c 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29  T_PAGER_PRAGMAS)
14d7d 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
14d7e 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29  ITE_OMIT_VACUUM)
14d7f 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
14d80 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20  e default pages 
14d81 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d  size and the num
14d82 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20  ber of reserved 
14d83 62 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a  bytes per page..
14d84 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 73  **.** The page s
14d85 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f  ize must be a po
14d86 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e  wer of 2 between
14d87 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e 20   512 and 65536. 
14d88 20 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20   If the page.** 
14d89 73 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64 6f  size supplied do
14d8a 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 73  es not meet this
14d8b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 6e   constraint then
14d8c 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69   the page size i
14d8d 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64  s not.** changed
14d8e 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a  ..**.** Page siz
14d8f 65 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e  es are constrain
14d90 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72  ed to be a power
14d91 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20   of two so that 
14d92 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66  the region.** of
14d93 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
14d94 6c 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  le used for lock
14d95 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61  ing (beginning a
14d96 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a  t PENDING_BYTE,.
14d97 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74  ** the first byt
14d98 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62  e past the 1GB b
14d99 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30 30  oundary, 0x40000
14d9a 30 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 63  000) needs to oc
14d9b 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 65  cur.** at the be
14d9c 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 67  ginning of a pag
14d9d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61  e..**.** If para
14d9e 6d 65 74 65 72 20 6e 52 65 73 65 72 76 65 20 69  meter nReserve i
14d9f 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f  s less than zero
14da0 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65  , then the numbe
14da1 72 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a 2a  r of reserved.**
14da2 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65 20   bytes per page 
14da3 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65  is left unchange
14da4 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
14da5 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14da6 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65  BtreeSetPageSize
14da7 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70  (Btree *p, int p
14da8 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65  ageSize, int nRe
14da9 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63  serve){.  int rc
14daa 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
14dab 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
14dac 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74  p->pBt;.  assert
14dad 28 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 26  ( nReserve>=-1 &
14dae 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20  & nReserve<=255 
14daf 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
14db0 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28  eEnter(p);.  if(
14db1 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69   pBt->pageSizeFi
14db2 78 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  xed ){.    sqlit
14db3 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
14db4 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
14db5 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d  TE_READONLY;.  }
14db6 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c  .  if( nReserve<
14db7 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 72 76  0 ){.    nReserv
14db8 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  e = pBt->pageSiz
14db9 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e - pBt->usableS
14dba 69 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ize;.  }.  asser
14dbb 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26  t( nReserve>=0 &
14dbc 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 20  & nReserve<=255 
14dbd 29 3b 0a 20 20 69 66 28 20 70 61 67 65 53 69 7a  );.  if( pageSiz
14dbe 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69  e>=512 && pageSi
14dbf 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50  ze<=SQLITE_MAX_P
14dc0 41 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 20 20  AGE_SIZE &&.    
14dc1 20 20 20 20 28 28 70 61 67 65 53 69 7a 65 2d 31      ((pageSize-1
14dc2 29 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 20 29  )&pageSize)==0 )
14dc3 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  {.    assert( (p
14dc4 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20  ageSize & 7)==0 
14dc5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
14dc6 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20 21  pBt->pPage1 && !
14dc7 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a  pBt->pCursor );.
14dc8 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a      pBt->pageSiz
14dc9 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a  e = (u16)pageSiz
14dca 65 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53  e;.    freeTempS
14dcb 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 72  pace(pBt);.    r
14dcc 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
14dcd 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d  SetPagesize(pBt-
14dce 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70  >pPager, &pBt->p
14dcf 61 67 65 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20  ageSize);.  }.  
14dd0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
14dd1 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  = pBt->pageSize 
14dd2 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b  - (u16)nReserve;
14dd3 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
14dd4 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
14dd5 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
14dd6 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e  eturn the curren
14dd7 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67 65  tly defined page
14dd8 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f   size.*/.SQLITE_
14dd9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14dda 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
14ddb 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ize(Btree *p){. 
14ddc 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e   return p->pBt->
14ddd 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a  pageSize;.}../*.
14dde 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
14ddf 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
14de0 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 6e   space at the en
14de1 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20  d of every page 
14de2 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74 65  that.** are inte
14de3 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e 75  ntually left unu
14de4 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 68  sed.  This is th
14de5 65 20 22 72 65 73 65 72 76 65 64 22 20 73 70 61  e "reserved" spa
14de6 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 73 6f  ce that is.** so
14de7 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 79 20  metimes used by 
14de8 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
14de9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14dea 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
14deb 74 52 65 73 65 72 76 65 28 42 74 72 65 65 20 2a  tReserve(Btree *
14dec 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73  p){.  int n;.  s
14ded 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
14dee 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42  (p);.  n = p->pB
14def 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d  t->pageSize - p-
14df0 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
14df1 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
14df2 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
14df3 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  rn n;.}../*.** S
14df4 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70  et the maximum p
14df5 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20  age count for a 
14df6 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 50 61  database if mxPa
14df7 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a  ge is positive..
14df8 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72  ** No changes ar
14df9 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65  e made if mxPage
14dfa 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76   is 0 or negativ
14dfb 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73  e..** Regardless
14dfc 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66   of the value of
14dfd 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20   mxPage, return 
14dfe 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65  the maximum page
14dff 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54   count..*/.SQLIT
14e00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14e01 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67  lite3BtreeMaxPag
14e02 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c  eCount(Btree *p,
14e03 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20   int mxPage){.  
14e04 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33  int n;.  sqlite3
14e05 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
14e06 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65   n = sqlite3Page
14e07 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d  rMaxPageCount(p-
14e08 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78  >pBt->pPager, mx
14e09 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Page);.  sqlite3
14e0a 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
14e0b 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e   return n;.}.#en
14e0c 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
14e0d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45  SQLITE_OMIT_PAGE
14e0e 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64  R_PRAGMAS) || !d
14e0f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
14e10 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f  IT_VACUUM) */../
14e11 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
14e12 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72  'auto-vacuum' pr
14e13 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 64 61  operty of the da
14e14 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 27  tabase. If the '
14e15 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70  autoVacuum'.** p
14e16 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d  arameter is non-
14e17 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d  zero, then auto-
14e18 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65  vacuum mode is e
14e19 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c  nabled. If zero,
14e1a 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c   it.** is disabl
14e1b 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20  ed. The default 
14e1c 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75  value for the au
14e1d 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72  to-vacuum proper
14e1e 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d  ty is .** determ
14e1f 69 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c 49  ined by the SQLI
14e20 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56  TE_DEFAULT_AUTOV
14e21 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a  ACUUM macro..*/.
14e22 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
14e23 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
14e24 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72  etAutoVacuum(Btr
14e25 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56  ee *p, int autoV
14e26 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53  acuum){.#ifdef S
14e27 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
14e28 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53  ACUUM.  return S
14e29 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a  QLITE_READONLY;.
14e2a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64  #else.  BtShared
14e2b 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
14e2c 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
14e2d 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20  E_OK;.  u8 av = 
14e2e 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a  (u8)autoVacuum;.
14e2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
14e30 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70  nter(p);.  if( p
14e31 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
14e32 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29 21 3d  d && (av ?1:0)!=
14e33 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
14e34 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
14e35 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d  TE_READONLY;.  }
14e36 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61  else{.    pBt->a
14e37 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 3f  utoVacuum = av ?
14e38 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e  1:0;.    pBt->in
14e39 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d 32  crVacuum = av==2
14e3a 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 6c   ?1:0;.  }.  sql
14e3b 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
14e3c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
14e3d 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
14e3e 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65  Return the value
14e3f 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d 76 61   of the 'auto-va
14e40 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 2e 20  cuum' property. 
14e41 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69  If auto-vacuum i
14e42 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 20  s .** enabled 1 
14e43 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  is returned. Oth
14e44 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51 4c  erwise 0..*/.SQL
14e45 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
14e46 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41  sqlite3BtreeGetA
14e47 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20  utoVacuum(Btree 
14e48 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  *p){.#ifdef SQLI
14e49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
14e4a 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 52 45  UM.  return BTRE
14e4b 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e  E_AUTOVACUUM_NON
14e4c 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 72  E;.#else.  int r
14e4d 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  c;.  sqlite3Btre
14e4e 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20  eEnter(p);.  rc 
14e4f 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 74  = (.    (!p->pBt
14e50 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 54  ->autoVacuum)?BT
14e51 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e  REE_AUTOVACUUM_N
14e52 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 42  ONE:.    (!p->pB
14e53 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f 42  t->incrVacuum)?B
14e54 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
14e55 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 5f  FULL:.    BTREE_
14e56 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 0a  AUTOVACUUM_INCR.
14e57 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74    );.  sqlite3Bt
14e58 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
14e59 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66  eturn rc;.#endif
14e5a 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61  .}.../*.** Get a
14e5b 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 50   reference to pP
14e5c 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 74 61  age1 of the data
14e5d 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73  base file.  This
14e5e 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63   will.** also ac
14e5f 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f 63 6b  quire a readlock
14e60 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a 2a   on that file..*
14e61 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  *.** SQLITE_OK i
14e62 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75  s returned on su
14e63 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 20 66  ccess.  If the f
14e64 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20  ile is not a.** 
14e65 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61  well-formed data
14e66 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20  base file, then 
14e67 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69  SQLITE_CORRUPT i
14e68 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 53  s returned..** S
14e69 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65  QLITE_BUSY is re
14e6a 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 64 61  turned if the da
14e6b 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64  tabase is locked
14e6c 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a  .  SQLITE_NOMEM.
14e6d 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ** is returned i
14e6e 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20  f we run out of 
14e6f 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 74  memory. .*/.stat
14e70 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65  ic int lockBtree
14e71 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b  (BtShared *pBt){
14e72 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d  .  int rc;.  Mem
14e73 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20  Page *pPage1;.  
14e74 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73  int nPage;..  as
14e75 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14e76 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
14e77 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 42  tex) );.  if( pB
14e78 74 2d 3e 70 50 61 67 65 31 20 29 20 72 65 74 75  t->pPage1 ) retu
14e79 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
14e7a 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
14e7b 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c  eGetPage(pBt, 1,
14e7c 20 26 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 20   &pPage1, 0);.  
14e7d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
14e7e 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
14e7f 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65    /* Do some che
14e80 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e  cking to help in
14e81 73 75 72 65 20 74 68 65 20 66 69 6c 65 20 77 65  sure the file we
14e82 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c 79 20 69   opened really i
14e83 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 64  s.  ** a valid d
14e84 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 20  atabase file. . 
14e85 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
14e86 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
14e87 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e  (pBt->pPager, &n
14e88 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21  Page);.  if( rc!
14e89 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14e8a 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69    goto page1_ini
14e8b 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 65 6c 73  t_failed;.  }els
14e8c 65 20 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b  e if( nPage>0 ){
14e8d 0a 20 20 20 20 69 6e 74 20 70 61 67 65 53 69 7a  .    int pageSiz
14e8e 65 3b 0a 20 20 20 20 69 6e 74 20 75 73 61 62 6c  e;.    int usabl
14e8f 65 53 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70  eSize;.    u8 *p
14e90 61 67 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61  age1 = pPage1->a
14e91 44 61 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53  Data;.    rc = S
14e92 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20  QLITE_NOTADB;.  
14e93 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67    if( memcmp(pag
14e94 65 31 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72  e1, zMagicHeader
14e95 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20  , 16)!=0 ){.    
14e96 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69    goto page1_ini
14e97 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a  t_failed;.    }.
14e98 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 38      if( page1[18
14e99 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 70 42 74  ]>1 ){.      pBt
14e9a 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a  ->readOnly = 1;.
14e9b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 61      }.    if( pa
14e9c 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20  ge1[19]>1 ){.   
14e9d 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e     goto page1_in
14e9e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d  it_failed;.    }
14e9f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78  ..    /* The max
14ea0 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 20 66 72  imum embedded fr
14ea1 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 65  action must be e
14ea2 78 61 63 74 6c 79 20 32 35 25 2e 20 20 41 6e 64  xactly 25%.  And
14ea3 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 20   the minimum.   
14ea4 20 2a 2a 20 65 6d 62 65 64 64 65 64 20 66 72 61   ** embedded fra
14ea5 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 31 32  ction must be 12
14ea6 2e 35 25 20 66 6f 72 20 62 6f 74 68 20 6c 65 61  .5% for both lea
14ea7 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d 6c  f-data and non-l
14ea8 65 61 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a 2a  eaf-data..    **
14ea9 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 65   The original de
14eaa 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65  sign allowed the
14eab 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61  se amounts to va
14eac 72 79 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 20  ry, but as of.  
14ead 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36    ** version 3.6
14eae 2e 30 2c 20 77 65 20 72 65 71 75 69 72 65 20 74  .0, we require t
14eaf 68 65 6d 20 74 6f 20 62 65 20 66 69 78 65 64 2e  hem to be fixed.
14eb0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
14eb1 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 31  memcmp(&page1[21
14eb2 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 30  ], "\100\040\040
14eb3 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ",3)!=0 ){.     
14eb4 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74   goto page1_init
14eb5 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20  _failed;.    }. 
14eb6 20 20 20 70 61 67 65 53 69 7a 65 20 3d 20 67 65     pageSize = ge
14eb7 74 32 62 79 74 65 28 26 70 61 67 65 31 5b 31 36  t2byte(&page1[16
14eb8 5d 29 3b 0a 20 20 20 20 69 66 28 20 28 28 70 61  ]);.    if( ((pa
14eb9 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69  geSize-1)&pageSi
14eba 7a 65 29 21 3d 30 20 7c 7c 20 70 61 67 65 53 69  ze)!=0 || pageSi
14ebb 7a 65 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20 20  ze<512 ||.      
14ebc 20 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41    (SQLITE_MAX_PA
14ebd 47 45 5f 53 49 5a 45 3c 33 32 37 36 38 20 26 26  GE_SIZE<32768 &&
14ebe 20 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45   pageSize>SQLITE
14ebf 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a  _MAX_PAGE_SIZE).
14ec0 20 20 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74      ){.      got
14ec1 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69  o page1_init_fai
14ec2 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  led;.    }.    a
14ec3 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65  ssert( (pageSize
14ec4 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20   & 7)==0 );.    
14ec5 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 61 67  usableSize = pag
14ec6 65 53 69 7a 65 20 2d 20 70 61 67 65 31 5b 32 30  eSize - page1[20
14ec7 5d 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 53  ];.    if( pageS
14ec8 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 69  ize!=pBt->pageSi
14ec9 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41  ze ){.      /* A
14eca 66 74 65 72 20 72 65 61 64 69 6e 67 20 74 68 65  fter reading the
14ecb 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   first page of t
14ecc 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73 75  he database assu
14ecd 6d 69 6e 67 20 61 20 70 61 67 65 20 73 69 7a 65  ming a page size
14ece 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 53  .      ** of BtS
14ecf 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 2c 20  hared.pageSize, 
14ed0 77 65 20 68 61 76 65 20 64 69 73 63 6f 76 65 72  we have discover
14ed1 65 64 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ed that the page
14ed2 2d 73 69 7a 65 20 69 73 0a 20 20 20 20 20 20 2a  -size is.      *
14ed3 2a 20 61 63 74 75 61 6c 6c 79 20 70 61 67 65 53  * actually pageS
14ed4 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20  ize. Unlock the 
14ed5 64 61 74 61 62 61 73 65 2c 20 6c 65 61 76 65 20  database, leave 
14ed6 70 42 74 2d 3e 70 50 61 67 65 31 20 61 74 0a 20  pBt->pPage1 at. 
14ed7 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64       ** zero and
14ed8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
14ed9 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69  K. The caller wi
14eda 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e  ll call this fun
14edb 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61  ction.      ** a
14edc 67 61 69 6e 20 77 69 74 68 20 74 68 65 20 63 6f  gain with the co
14edd 72 72 65 63 74 20 70 61 67 65 2d 73 69 7a 65 2e  rrect page-size.
14ede 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
14edf 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
14ee0 65 31 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e  e1);.      pBt->
14ee1 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 31  usableSize = (u1
14ee2 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  6)usableSize;.  
14ee3 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a      pBt->pageSiz
14ee4 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a  e = (u16)pageSiz
14ee5 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 65 6d  e;.      freeTem
14ee6 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20  pSpace(pBt);.   
14ee7 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53     sqlite3PagerS
14ee8 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e  etPagesize(pBt->
14ee9 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61  pPager, &pBt->pa
14eea 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 72  geSize);.      r
14eeb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
14eec 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75  .    }.    if( u
14eed 73 61 62 6c 65 53 69 7a 65 3c 35 30 30 20 29 7b  sableSize<500 ){
14eee 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65  .      goto page
14eef 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20  1_init_failed;. 
14ef0 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61     }.    pBt->pa
14ef1 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61  geSize = (u16)pa
14ef2 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d  geSize;.    pBt-
14ef3 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75  >usableSize = (u
14ef4 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23  16)usableSize;.#
14ef5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14ef6 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
14ef7 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75    pBt->autoVacuu
14ef8 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70  m = (get4byte(&p
14ef9 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f  age1[36 + 4*4])?
14efa 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69  1:0);.    pBt->i
14efb 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74  ncrVacuum = (get
14efc 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20  4byte(&page1[36 
14efd 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65  + 7*4])?1:0);.#e
14efe 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d  ndif.  }..  /* m
14eff 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d  axLocal is the m
14f00 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66  aximum amount of
14f01 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72   payload to stor
14f02 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20  e locally for.  
14f03 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65  ** a cell.  Make
14f04 20 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c   sure it is smal
14f05 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74  l enough so that
14f06 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e   at least minFan
14f07 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63  out.  ** cells c
14f08 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f  an will fit on o
14f09 6e 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73  ne page.  We ass
14f0a 75 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61  ume a 10-byte pa
14f0b 67 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20  ge header..  ** 
14f0c 42 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c  Besides the payl
14f0d 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75  oad, the cell mu
14f0e 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20  st store:.  **  
14f0f 20 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65     2-byte pointe
14f10 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20  r to the cell.  
14f11 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68  **     4-byte ch
14f12 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a  ild pointer.  **
14f13 20 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79       9-byte nKey
14f14 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20   value.  **     
14f15 34 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c  4-byte nData val
14f16 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79  ue.  **     4-by
14f17 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  te overflow page
14f18 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f   pointer.  ** So
14f19 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73   a cell consists
14f1a 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69   of a 2-byte poi
14f1b 6e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68  ner, a header wh
14f1c 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61  ich is as much a
14f1d 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20  s.  ** 17 bytes 
14f1e 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74  long, 0 to N byt
14f1f 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61  es of payload, a
14f20 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34  nd an optional 4
14f21 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20   byte overflow. 
14f22 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72   ** page pointer
14f23 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61  ..  */.  pBt->ma
14f24 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75  xLocal = (pBt->u
14f25 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34  sableSize-12)*64
14f26 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74  /255 - 23;.  pBt
14f27 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42  ->minLocal = (pB
14f28 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32  t->usableSize-12
14f29 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20  )*32/255 - 23;. 
14f2a 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20   pBt->maxLeaf = 
14f2b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
14f2c 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e  - 35;.  pBt->min
14f2d 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61  Leaf = (pBt->usa
14f2e 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32  bleSize-12)*32/2
14f2f 35 35 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72  55 - 23;.  asser
14f30 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20  t( pBt->maxLeaf 
14f31 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f  + 23 <= MX_CELL_
14f32 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70  SIZE(pBt) );.  p
14f33 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61  Bt->pPage1 = pPa
14f34 67 65 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ge1;.  return SQ
14f35 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f  LITE_OK;..page1_
14f36 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72  init_failed:.  r
14f37 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
14f38 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65  1);.  pBt->pPage
14f39 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  1 = 0;.  return 
14f3a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
14f3b 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20  s routine works 
14f3c 6c 69 6b 65 20 6c 6f 63 6b 42 74 72 65 65 28 29  like lockBtree()
14f3d 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
14f3e 61 6c 73 6f 20 69 6e 76 6f 6b 65 73 20 74 68 65  also invokes the
14f3f 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61 63  .** busy callbac
14f40 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6c 6f  k if there is lo
14f41 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 0a 2a  ck contention..*
14f42 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63  /.static int loc
14f43 6b 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28  kBtreeWithRetry(
14f44 42 74 72 65 65 20 2a 70 52 65 66 29 7b 0a 20 20  Btree *pRef){.  
14f45 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
14f46 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  OK;..  assert( s
14f47 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
14f48 4d 75 74 65 78 28 70 52 65 66 29 20 29 3b 0a 20  Mutex(pRef) );. 
14f49 20 69 66 28 20 70 52 65 66 2d 3e 69 6e 54 72 61   if( pRef->inTra
14f4a 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ns==TRANS_NONE )
14f4b 7b 0a 20 20 20 20 75 38 20 69 6e 54 72 61 6e 73  {.    u8 inTrans
14f4c 61 63 74 69 6f 6e 20 3d 20 70 52 65 66 2d 3e 70  action = pRef->p
14f4d 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
14f4e 6e 3b 0a 20 20 20 20 62 74 72 65 65 49 6e 74 65  n;.    btreeInte
14f4f 67 72 69 74 79 28 70 52 65 66 29 3b 0a 20 20 20  grity(pRef);.   
14f50 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
14f51 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 52 65  eeBeginTrans(pRe
14f52 66 2c 20 30 29 3b 0a 20 20 20 20 70 52 65 66 2d  f, 0);.    pRef-
14f53 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  >pBt->inTransact
14f54 69 6f 6e 20 3d 20 69 6e 54 72 61 6e 73 61 63 74  ion = inTransact
14f55 69 6f 6e 3b 0a 20 20 20 20 70 52 65 66 2d 3e 69  ion;.    pRef->i
14f56 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e  nTrans = TRANS_N
14f57 4f 4e 45 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ONE;.    if( rc=
14f58 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14f59 20 20 20 20 70 52 65 66 2d 3e 70 42 74 2d 3e 6e      pRef->pBt->n
14f5a 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20  Transaction--;. 
14f5b 20 20 20 7d 0a 20 20 20 20 62 74 72 65 65 49 6e     }.    btreeIn
14f5c 74 65 67 72 69 74 79 28 70 52 65 66 29 3b 0a 20  tegrity(pRef);. 
14f5d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
14f5e 7d 0a 20 20 20 20 20 20 20 0a 0a 2f 2a 0a 2a 2a  }.       ../*.**
14f5f 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
14f60 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72   outstanding cur
14f61 73 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20  sors and we are 
14f62 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  not in the middl
14f63 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61  e.** of a transa
14f64 63 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20  ction but there 
14f65 69 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f  is a read lock o
14f66 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
14f67 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75  then.** this rou
14f68 74 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20  tine unrefs the 
14f69 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68  first page of th
14f6a 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
14f6b 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68  which .** has th
14f6c 65 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65  e effect of rele
14f6d 61 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c  asing the read l
14f6e 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ock..**.** If th
14f6f 65 72 65 20 61 72 65 20 61 6e 79 20 6f 75 74 73  ere are any outs
14f70 74 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 2c  tanding cursors,
14f71 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
14f72 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
14f73 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74 72  If there is a tr
14f74 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f  ansaction in pro
14f75 67 72 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74  gress, this rout
14f76 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
14f77 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
14f78 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
14f79 65 64 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ed(BtShared *pBt
14f7a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
14f7b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14f7c 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
14f7d 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e   if( pBt->inTran
14f7e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e  saction==TRANS_N
14f7f 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70 43 75 72  ONE && pBt->pCur
14f80 73 6f 72 3d 3d 30 20 26 26 20 70 42 74 2d 3e 70  sor==0 && pBt->p
14f81 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20  Page1!=0 ){.    
14f82 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  if( sqlite3Pager
14f83 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  Refcount(pBt->pP
14f84 61 67 65 72 29 3e 3d 31 20 29 7b 0a 20 20 20 20  ager)>=1 ){.    
14f85 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70    assert( pBt->p
14f86 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a  Page1->aData );.
14f87 23 69 66 20 30 0a 20 20 20 20 20 20 69 66 28 20  #if 0.      if( 
14f88 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
14f89 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ta==0 ){.       
14f8a 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
14f8b 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20  = pBt->pPage1;. 
14f8c 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 44         pPage->aD
14f8d 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ata = sqlite3Pag
14f8e 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d  erGetData(pPage-
14f8f 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
14f90 20 20 20 70 50 61 67 65 2d 3e 70 42 74 20 3d 20     pPage->pBt = 
14f91 70 42 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61  pBt;.        pPa
14f92 67 65 2d 3e 70 67 6e 6f 20 3d 20 31 3b 0a 20 20  ge->pgno = 1;.  
14f93 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
14f94 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
14f95 42 74 2d 3e 70 50 61 67 65 31 29 3b 0a 20 20 20  Bt->pPage1);.   
14f96 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67   }.    pBt->pPag
14f97 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  e1 = 0;.  }.}../
14f98 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
14f99 77 20 64 61 74 61 62 61 73 65 20 62 79 20 69 6e  w database by in
14f9a 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 66  itializing the f
14f9b 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
14f9c 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  .** file..*/.sta
14f9d 74 69 63 20 69 6e 74 20 6e 65 77 44 61 74 61 62  tic int newDatab
14f9e 61 73 65 28 42 74 53 68 61 72 65 64 20 2a 70 42  ase(BtShared *pB
14f9f 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  t){.  MemPage *p
14fa0 50 31 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  P1;.  unsigned c
14fa1 68 61 72 20 2a 64 61 74 61 3b 0a 20 20 69 6e 74  har *data;.  int
14fa2 20 72 63 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65   rc;.  int nPage
14fa3 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
14fa4 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14fa5 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
14fa6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
14fa7 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
14fa8 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  >pPager, &nPage)
14fa9 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
14faa 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 67 65 3e 30  TE_OK || nPage>0
14fab 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
14fac 63 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20 70  c;.  }.  pP1 = p
14fad 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61 73  Bt->pPage1;.  as
14fae 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 3b 0a  sert( pP1!=0 );.
14faf 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e 61 44    data = pP1->aD
14fb0 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ata;.  rc = sqli
14fb1 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
14fb2 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69  1->pDbPage);.  i
14fb3 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
14fb4 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 74 61  c;.  memcpy(data
14fb5 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20  , zMagicHeader, 
14fb6 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 61  sizeof(zMagicHea
14fb7 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74 28  der));.  assert(
14fb8 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65   sizeof(zMagicHe
14fb9 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20 70  ader)==16 );.  p
14fba 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 31 36  ut2byte(&data[16
14fbb 5d 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  ], pBt->pageSize
14fbc 29 3b 0a 20 20 64 61 74 61 5b 31 38 5d 20 3d 20  );.  data[18] = 
14fbd 31 3b 0a 20 20 64 61 74 61 5b 31 39 5d 20 3d 20  1;.  data[19] = 
14fbe 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  1;.  assert( pBt
14fbf 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3c 3d 70 42  ->usableSize<=pB
14fc0 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 26 20 70  t->pageSize && p
14fc1 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2b 32  Bt->usableSize+2
14fc2 35 35 3e 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a  55>=pBt->pageSiz
14fc3 65 29 3b 0a 20 20 64 61 74 61 5b 32 30 5d 20 3d  e);.  data[20] =
14fc4 20 28 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53   (u8)(pBt->pageS
14fc5 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c  ize - pBt->usabl
14fc6 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32  eSize);.  data[2
14fc7 31 5d 20 3d 20 36 34 3b 0a 20 20 64 61 74 61 5b  1] = 64;.  data[
14fc8 32 32 5d 20 3d 20 33 32 3b 0a 20 20 64 61 74 61  22] = 32;.  data
14fc9 5b 32 33 5d 20 3d 20 33 32 3b 0a 20 20 6d 65 6d  [23] = 32;.  mem
14fca 73 65 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30  set(&data[24], 0
14fcb 2c 20 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72  , 100-24);.  zer
14fcc 6f 50 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49  oPage(pP1, PTF_I
14fcd 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50  NTKEY|PTF_LEAF|P
14fce 54 46 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20  TF_LEAFDATA );. 
14fcf 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69   pBt->pageSizeFi
14fd0 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66  xed = 1;.#ifndef
14fd1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
14fd2 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74  OVACUUM.  assert
14fd3 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
14fd4 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74  m==1 || pBt->aut
14fd5 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20  oVacuum==0 );.  
14fd6 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63  assert( pBt->inc
14fd7 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42  rVacuum==1 || pB
14fd8 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30  t->incrVacuum==0
14fd9 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26   );.  put4byte(&
14fda 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20  data[36 + 4*4], 
14fdb 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
14fdc 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61  ;.  put4byte(&da
14fdd 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42  ta[36 + 7*4], pB
14fde 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a  t->incrVacuum);.
14fdf 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
14fe0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
14fe1 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73  .** Attempt to s
14fe2 74 61 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73  tart a new trans
14fe3 61 63 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d  action. A write-
14fe4 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69  transaction.** i
14fe5 73 20 73 74 61 72 74 65 64 20 69 66 20 74 68 65  s started if the
14fe6 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
14fe7 20 69 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68   is nonzero, oth
14fe8 65 72 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a  erwise a read-.*
14fe9 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  * transaction.  
14fea 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
14feb 67 75 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d  gument is 2 or m
14fec 6f 72 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76  ore and exclusiv
14fed 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  e.** transaction
14fee 20 69 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61   is started, mea
14fef 6e 69 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68  ning that no oth
14ff0 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c  er process is al
14ff1 6c 6f 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65  lowed.** to acce
14ff2 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ss the database.
14ff3 20 20 41 20 70 72 65 65 78 69 73 74 69 6e 67 20    A preexisting 
14ff4 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20  transaction may 
14ff5 6e 6f 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64  not be.** upgrad
14ff6 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20  ed to exclusive 
14ff7 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  by calling this 
14ff8 72 6f 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64  routine a second
14ff9 20 74 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65   time - the.** e
14ffa 78 63 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20  xclusivity flag 
14ffb 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61  only works for a
14ffc 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e   new transaction
14ffd 2e 0a 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d  ..**.** A write-
14ffe 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74  transaction must
14fff 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f   be started befo
15000 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e  re attempting an
15001 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f  y .** changes to
15002 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
15003 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  None of the foll
15004 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a  owing routines .
15005 2a 2a 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c  ** will work unl
15006 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ess a transactio
15007 6e 20 69 73 20 73 74 61 72 74 65 64 20 66 69 72  n is started fir
15008 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73  st:.**.**      s
15009 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
1500a 65 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20  eTable().**     
1500b 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65   sqlite3BtreeCre
1500c 61 74 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20  ateIndex().**   
1500d 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
1500e 6c 65 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20  learTable().**  
1500f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15010 44 72 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20  DropTable().**  
15011 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15012 49 6e 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20  Insert().**     
15013 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c   sqlite3BtreeDel
15014 65 74 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71  ete().**      sq
15015 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
15016 4d 65 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20  Meta().**.** If 
15017 61 6e 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d  an initial attem
15018 70 74 20 74 6f 20 61 63 71 75 69 72 65 20 74 68  pt to acquire th
15019 65 20 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63  e lock fails bec
1501a 61 75 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e  ause of lock con
1501b 74 65 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74  tention.** and t
1501c 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20  he database was 
1501d 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63  previously unloc
1501e 6b 65 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65  ked, then invoke
1501f 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
15020 72 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73  r.** if there is
15021 20 6f 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68   one.  But if th
15022 65 72 65 20 77 61 73 20 70 72 65 76 69 6f 75 73  ere was previous
15023 6c 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20  ly a read-lock, 
15024 64 6f 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65  do not.** invoke
15025 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
15026 72 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20  r - just return 
15027 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51  SQLITE_BUSY.  SQ
15028 4c 49 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a  LITE_BUSY is .**
15029 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74   returned when t
1502a 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
1502b 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f  a read-lock in o
1502c 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20  rder to avoid a 
1502d 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  deadlock..**.** 
1502e 53 75 70 70 6f 73 65 20 74 68 65 72 65 20 61 72  Suppose there ar
1502f 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20  e two processes 
15030 41 20 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20  A and B.  A has 
15031 61 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20  a read lock and 
15032 42 20 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72  B has.** a reser
15033 76 65 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69  ved lock.  B tri
15034 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f  es to promote to
15035 20 65 78 63 6c 75 73 69 76 65 20 62 75 74 20 69   exclusive but i
15036 73 20 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73  s blocked becaus
15037 65 0a 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64  e.** of A's read
15038 20 6c 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20   lock.  A tries 
15039 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65  to promote to re
1503a 73 65 72 76 65 64 20 62 75 74 20 69 73 20 62 6c  served but is bl
1503b 6f 63 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f  ocked by B..** O
1503c 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  ne or the other 
1503d 6f 66 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65  of the two proce
1503e 73 73 65 73 20 6d 75 73 74 20 67 69 76 65 20 77  sses must give w
1503f 61 79 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20  ay or there can 
15040 62 65 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73  be.** no progres
15041 73 2e 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67  s.  By returning
15042 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64   SQLITE_BUSY and
15043 20 6e 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68   not invoking th
15044 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a  e busy callback.
15045 2a 2a 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64  ** when A alread
15046 79 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63  y has a read loc
15047 6b 2c 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20  k, we encourage 
15048 41 20 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64  A to give up and
15049 20 6c 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65   let B.** procee
1504a 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1504b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1504c 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28  BtreeBeginTrans(
1504d 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72  Btree *p, int wr
1504e 66 6c 61 67 29 7b 0a 20 20 73 71 6c 69 74 65 33  flag){.  sqlite3
1504f 20 2a 70 42 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20   *pBlock = 0;.  
15050 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
15051 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63  p->pBt;.  int rc
15052 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
15053 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
15054 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62  er(p);.  pBt->db
15055 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 62 74 72 65   = p->db;.  btre
15056 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a  eIntegrity(p);..
15057 20 20 2f 2a 20 49 66 20 74 68 65 20 62 74 72 65    /* If the btre
15058 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  e is already in 
15059 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  a write-transact
1505a 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 20 2a 2a 20  ion, or it.  ** 
1505b 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20  is already in a 
1505c 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e  read-transaction
1505d 20 61 6e 64 20 61 20 72 65 61 64 2d 74 72 61 6e   and a read-tran
1505e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20  saction.  ** is 
1505f 72 65 71 75 65 73 74 65 64 2c 20 74 68 69 73 20  requested, this 
15060 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f  is a no-op..  */
15061 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
15062 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c  s==TRANS_WRITE |
15063 7c 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  | (p->inTrans==T
15064 52 41 4e 53 5f 52 45 41 44 20 26 26 20 21 77 72  RANS_READ && !wr
15065 66 6c 61 67 29 20 29 7b 0a 20 20 20 20 67 6f 74  flag) ){.    got
15066 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20  o trans_begun;. 
15067 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74   }..  /* Write t
15068 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20  ransactions are 
15069 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 6f 6e 20  not possible on 
1506a 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61  a read-only data
1506b 62 61 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 42  base */.  if( pB
1506c 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 77  t->readOnly && w
1506d 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 72 63 20  rflag ){.    rc 
1506e 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  = SQLITE_READONL
1506f 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e  Y;.    goto tran
15070 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69  s_begun;.  }..#i
15071 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15072 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
15073 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 64   /* If another d
15074 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 68  atabase handle h
15075 61 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65  as already opene
15076 64 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61  d a write transa
15077 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74  ction .  ** on t
15078 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 65  his shared-btree
15079 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61   structure and a
1507a 20 73 65 63 6f 6e 64 20 77 72 69 74 65 20 74 72   second write tr
1507b 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a  ansaction is.  *
1507c 2a 20 72 65 71 75 65 73 74 65 64 2c 20 72 65 74  * requested, ret
1507d 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
1507e 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 77  D..  */.  if( (w
1507f 72 66 6c 61 67 20 26 26 20 70 42 74 2d 3e 69 6e  rflag && pBt->in
15080 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
15081 4e 53 5f 57 52 49 54 45 29 20 7c 7c 20 70 42 74  NS_WRITE) || pBt
15082 2d 3e 69 73 50 65 6e 64 69 6e 67 20 29 7b 0a 20  ->isPending ){. 
15083 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 42 74 2d     pBlock = pBt-
15084 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b 0a 20 20  >pWriter->db;.  
15085 7d 65 6c 73 65 20 69 66 28 20 77 72 66 6c 61 67  }else if( wrflag
15086 3e 31 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b  >1 ){.    BtLock
15087 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 66 6f 72   *pIter;.    for
15088 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63  (pIter=pBt->pLoc
15089 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d  k; pIter; pIter=
1508a 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  pIter->pNext){. 
1508b 20 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e       if( pIter->
1508c 70 42 74 72 65 65 21 3d 70 20 29 7b 0a 20 20 20  pBtree!=p ){.   
1508d 20 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 49       pBlock = pI
1508e 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3b  ter->pBtree->db;
1508f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
15090 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
15091 7d 0a 20 20 69 66 28 20 70 42 6c 6f 63 6b 20 29  }.  if( pBlock )
15092 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  {.    sqlite3Con
15093 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70  nectionBlocked(p
15094 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 3b 0a 20  ->db, pBlock);. 
15095 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c     rc = SQLITE_L
15096 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
15097 45 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e  E;.    goto tran
15098 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 23 65 6e  s_begun;.  }.#en
15099 64 69 66 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20  dif..  do {.    
1509a 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d  if( pBt->pPage1=
1509b 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 7b 0a  =0 ){.      do{.
1509c 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63          rc = loc
1509d 6b 42 74 72 65 65 28 70 42 74 29 3b 0a 20 20 20  kBtree(pBt);.   
1509e 20 20 20 7d 77 68 69 6c 65 28 20 70 42 74 2d 3e     }while( pBt->
1509f 70 50 61 67 65 31 3d 3d 30 20 26 26 20 72 63 3d  pPage1==0 && rc=
150a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
150a1 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d    }..    if( rc=
150a2 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72  =SQLITE_OK && wr
150a3 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 69 66  flag ){.      if
150a4 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  ( pBt->readOnly 
150a5 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
150a6 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
150a7 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
150a8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
150a9 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 42 74  e3PagerBegin(pBt
150aa 2d 3e 70 50 61 67 65 72 2c 20 77 72 66 6c 61 67  ->pPager, wrflag
150ab 3e 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  >1);.        if(
150ac 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
150ad 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
150ae 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74   newDatabase(pBt
150af 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
150b0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
150b1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
150b2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c  _OK ){.      unl
150b3 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
150b4 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  (pBt);.    }.  }
150b5 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
150b6 45 5f 42 55 53 59 20 26 26 20 70 42 74 2d 3e 69  E_BUSY && pBt->i
150b7 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
150b8 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20  ANS_NONE &&.    
150b9 20 20 20 20 20 20 62 74 72 65 65 49 6e 76 6f 6b        btreeInvok
150ba 65 42 75 73 79 48 61 6e 64 6c 65 72 28 70 42 74  eBusyHandler(pBt
150bb 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  ) );..  if( rc==
150bc 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
150bd 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d   if( p->inTrans=
150be 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20  =TRANS_NONE ){. 
150bf 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73       pBt->nTrans
150c0 61 63 74 69 6f 6e 2b 2b 3b 0a 20 20 20 20 7d 0a  action++;.    }.
150c1 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d      p->inTrans =
150c2 20 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f 57   (wrflag?TRANS_W
150c3 52 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44 29  RITE:TRANS_READ)
150c4 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54  ;.    if( p->inT
150c5 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61 6e  rans>pBt->inTran
150c6 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20  saction ){.     
150c7 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
150c8 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e 73  ion = p->inTrans
150c9 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
150ca 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
150cb 45 44 5f 43 41 43 48 45 0a 20 20 20 20 69 66 28  ED_CACHE.    if(
150cc 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20   wrflag ){.     
150cd 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70   assert( !pBt->p
150ce 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20  Writer );.      
150cf 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 70  pBt->pWriter = p
150d0 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 73 45  ;.      pBt->isE
150d1 78 63 6c 75 73 69 76 65 20 3d 20 28 75 38 29 28  xclusive = (u8)(
150d2 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 7d  wrflag>1);.    }
150d3 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a 74 72  .#endif.  }...tr
150d4 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66 28  ans_begun:.  if(
150d5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
150d6 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20  & wrflag ){.    
150d7 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61 6b  /* This call mak
150d8 65 73 20 73 75 72 65 20 74 68 61 74 20 74 68 65  es sure that the
150d9 20 70 61 67 65 72 20 68 61 73 20 74 68 65 20 63   pager has the c
150da 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66  orrect number of
150db 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 76  .    ** open sav
150dc 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 20  epoints. If the 
150dd 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
150de 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
150df 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68   0 and.    ** th
150e0 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73  e sub-journal is
150e1 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65   not already ope
150e2 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20  n, then it will 
150e3 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e 0a  be opened here..
150e4 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
150e5 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
150e6 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70  Savepoint(pBt->p
150e7 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e 53  Pager, p->db->nS
150e8 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 0a  avepoint);.  }..
150e9 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79    btreeIntegrity
150ea 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  (p);.  sqlite3Bt
150eb 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
150ec 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
150ed 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
150ee 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a  _AUTOVACUUM../*.
150ef 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74  ** Set the point
150f0 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66  er-map entries f
150f1 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20  or all children 
150f2 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 41  of page pPage. A
150f3 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65  lso, if.** pPage
150f4 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20   contains cells 
150f5 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76  that point to ov
150f6 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65  erflow pages, se
150f7 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a  t the pointer.**
150f8 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72   map entries for
150f9 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
150fa 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a  ges as well..*/.
150fb 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 68  static int setCh
150fc 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61  ildPtrmaps(MemPa
150fd 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e  ge *pPage){.  in
150fe 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
150ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15100 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69   /* Counter vari
15101 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  able */.  int nC
15102 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
15103 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15104 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  Number of cells 
15105 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f  in page pPage */
15106 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
15107 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15108 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
15109 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72  code */.  BtShar
1510a 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d  ed *pBt = pPage-
1510b 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e 69  >pBt;.  u8 isIni
1510c 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69  tOrig = pPage->i
1510d 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 67  sInit;.  Pgno pg
1510e 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f  no = pPage->pgno
1510f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
15110 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15111 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
15112 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  x) );.  rc = sql
15113 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
15114 65 28 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20  e(pPage);.  if( 
15115 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
15116 0a 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68  .    goto set_ch
15117 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b  ild_ptrmaps_out;
15118 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70  .  }.  nCell = p
15119 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20  Page->nCell;..  
1511a 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c  for(i=0; i<nCell
1511b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a  ; i++){.    u8 *
1511c 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
1511d 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20  (pPage, i);..   
1511e 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 4f   rc = ptrmapPutO
1511f 76 66 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  vflPtr(pPage, pC
15120 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ell);.    if( rc
15121 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
15122 20 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68       goto set_ch
15123 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b  ild_ptrmaps_out;
15124 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
15125 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
15126 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64        Pgno child
15127 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  Pgno = get4byte(
15128 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 72 63  pCell);.      rc
15129 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74   = ptrmapPut(pBt
1512a 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52  , childPgno, PTR
1512b 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 29  MAP_BTREE, pgno)
1512c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
1512d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
1512e 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61   set_child_ptrma
1512f 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  ps_out;.    }.  
15130 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d  }..  if( !pPage-
15131 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67 6e  >leaf ){.    Pgn
15132 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65  o childPgno = ge
15133 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
15134 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
15135 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 72  ffset+8]);.    r
15136 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42  c = ptrmapPut(pB
15137 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54  t, childPgno, PT
15138 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f  RMAP_BTREE, pgno
15139 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 69 6c  );.  }..set_chil
1513a 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a 0a 20  d_ptrmaps_out:. 
1513b 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
1513c 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 72   isInitOrig;.  r
1513d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1513e 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e 20  ** Somewhere on 
1513f 70 50 61 67 65 2c 20 77 68 69 63 68 20 69 73 20  pPage, which is 
15140 67 75 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65  guarenteed to be
15141 20 61 20 62 74 72 65 65 20 70 61 67 65 2c 20 6e   a btree page, n
15142 6f 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a  ot an overflow.*
15143 2a 20 70 61 67 65 2c 20 69 73 20 61 20 70 6f 69  * page, is a poi
15144 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 72  nter to page iFr
15145 6f 6d 2e 20 4d 6f 64 69 66 79 20 74 68 69 73 20  om. Modify this 
15146 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 20  pointer so that 
15147 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  it points to.** 
15148 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65  iTo. Parameter e
15149 54 79 70 65 20 64 65 73 63 72 69 62 65 73 20 74  Type describes t
1514a 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74  he type of point
1514b 65 72 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65  er to be modifie
1514c 64 2c 20 61 73 20 0a 2a 2a 20 66 6f 6c 6c 6f 77  d, as .** follow
1514d 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  s:.**.** PTRMAP_
1514e 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 67 65  BTREE:     pPage
1514f 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67 65   is a btree-page
15150 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f  . The pointer po
15151 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c 64 20  ints at a child 
15152 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
15153 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 70 50        page of pP
15154 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  age..**.** PTRMA
15155 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50 61  P_OVERFLOW1: pPa
15156 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61  ge is a btree-pa
15157 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  ge. The pointer 
15158 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f 76 65  points at an ove
15159 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  rflow.**        
1515a 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20             page 
1515b 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6f 6e  pointed to by on
1515c 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f  e of the cells o
1515d 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  n pPage..**.** P
1515e 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a  TRMAP_OVERFLOW2:
1515f 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65   pPage is an ove
15160 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65 20  rflow-page. The 
15161 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61  pointer points a
15162 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20  t the next.**   
15163 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15164 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e  overflow page in
15165 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74   the list..*/.st
15166 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 79 50  atic int modifyP
15167 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50 61  agePointer(MemPa
15168 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f 20  ge *pPage, Pgno 
15169 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c  iFrom, Pgno iTo,
1516a 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 61 73   u8 eType){.  as
1516b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1516c 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1516d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1516e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1516f 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
15170 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
15171 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d   );.  if( eType=
15172 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  =PTRMAP_OVERFLOW
15173 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  2 ){.    /* The 
15174 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79  pointer is alway
15175 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79  s the first 4 by
15176 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20  tes of the page 
15177 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a  in this case.  *
15178 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 62 79  /.    if( get4by
15179 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29  te(pPage->aData)
1517a 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  !=iFrom ){.     
1517b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1517c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1517d 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 65 28   }.    put4byte(
1517e 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54  pPage->aData, iT
1517f 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  o);.  }else{.   
15180 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d   u8 isInitOrig =
15181 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a   pPage->isInit;.
15182 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69      int i;.    i
15183 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 73  nt nCell;..    s
15184 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
15185 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
15186 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e  nCell = pPage->n
15187 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69  Cell;..    for(i
15188 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b  =0; i<nCell; i++
15189 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65  ){.      u8 *pCe
1518a 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
1518b 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69  age, i);.      i
1518c 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  f( eType==PTRMAP
1518d 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20  _OVERFLOW1 ){.  
1518e 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69        CellInfo i
1518f 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nfo;.        sql
15190 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
15191 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
15192 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  ll, &info);.    
15193 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76      if( info.iOv
15194 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
15195 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 67      if( iFrom==g
15196 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  et4byte(&pCell[i
15197 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 20  nfo.iOverflow]) 
15198 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
15199 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  ut4byte(&pCell[i
1519a 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 2c 20  nfo.iOverflow], 
1519b 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iTo);.          
1519c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1519d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1519e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1519f 20 20 20 20 69 66 28 20 67 65 74 34 62 79 74 65      if( get4byte
151a0 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29  (pCell)==iFrom )
151a1 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34  {.          put4
151a2 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 6f 29  byte(pCell, iTo)
151a3 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
151a4 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
151a5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
151a6 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c 20 29    if( i==nCell )
151a7 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70  {.      if( eTyp
151a8 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20  e!=PTRMAP_BTREE 
151a9 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 67 65  || .          ge
151aa 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
151ab 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
151ac 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72 6f 6d  ffset+8])!=iFrom
151ad 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
151ae 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
151af 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a  T_BKPT;.      }.
151b0 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
151b1 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
151b2 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
151b3 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20  , iTo);.    }.. 
151b4 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74     pPage->isInit
151b5 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20   = isInitOrig;. 
151b6 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
151b7 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
151b8 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e 20 64   Move the open d
151b9 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62  atabase page pDb
151ba 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69 6f 6e  Page to location
151bb 20 69 46 72 65 65 50 61 67 65 20 69 6e 20 74 68   iFreePage in th
151bc 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  e .** database. 
151bd 54 68 65 20 70 44 62 50 61 67 65 20 72 65 66 65  The pDbPage refe
151be 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20 76 61  rence remains va
151bf 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lid..*/.static i
151c0 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28  nt relocatePage(
151c1 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
151c2 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ,           /* B
151c3 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  tree */.  MemPag
151c4 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20  e *pDbPage,     
151c5 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20     /* Open page 
151c6 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20  to move */.  u8 
151c7 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 20  eType,          
151c8 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
151c9 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74 72   map 'type' entr
151ca 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f  y for pDbPage */
151cb 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65  .  Pgno iPtrPage
151cc 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ,           /* P
151cd 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65  ointer map 'page
151ce 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70  -no' entry for p
151cf 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f  DbPage */.  Pgno
151d0 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 20 20   iFreePage,     
151d1 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61       /* The loca
151d2 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62  tion to move pDb
151d3 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74  Page to */.  int
151d4 20 69 73 43 6f 6d 6d 69 74 0a 29 7b 0a 20 20 4d   isCommit.){.  M
151d5 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67 65  emPage *pPtrPage
151d6 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20  ;   /* The page 
151d7 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20  that contains a 
151d8 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50 61  pointer to pDbPa
151d9 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62  ge */.  Pgno iDb
151da 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d 3e  Page = pDbPage->
151db 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70  pgno;.  Pager *p
151dc 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61  Pager = pBt->pPa
151dd 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ger;.  int rc;..
151de 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d    assert( eType=
151df 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  =PTRMAP_OVERFLOW
151e0 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d  2 || eType==PTRM
151e1 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20  AP_OVERFLOW1 || 
151e2 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 54  .      eType==PT
151e3 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54  RMAP_BTREE || eT
151e4 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54  ype==PTRMAP_ROOT
151e5 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74  PAGE );.  assert
151e6 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
151e7 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
151e8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   );.  assert( pD
151e9 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20  bPage->pBt==pBt 
151ea 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61  );..  /* Move pa
151eb 67 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d 20  ge iDbPage from 
151ec 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 61  its current loca
151ed 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d  tion to page num
151ee 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a 2f  ber iFreePage */
151ef 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f 56  .  TRACE(("AUTOV
151f0 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64  ACUUM: Moving %d
151f1 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25 64   to free page %d
151f2 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74 79   (ptr page %d ty
151f3 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20  pe %d)\n", .    
151f4 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65    iDbPage, iFree
151f5 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c 20  Page, iPtrPage, 
151f6 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d 20  eType));.  rc = 
151f7 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65  sqlite3PagerMove
151f8 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44 62  page(pPager, pDb
151f9 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 69  Page->pDbPage, i
151fa 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d  FreePage, isComm
151fb 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  it);.  if( rc!=S
151fc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
151fd 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
151fe 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d   pDbPage->pgno =
151ff 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f   iFreePage;..  /
15200 2a 20 49 66 20 70 44 62 50 61 67 65 20 77 61 73  * If pDbPage was
15201 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20 74   a btree-page, t
15202 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65 20  hen it may have 
15203 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64 2f  child pages and/
15204 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68  or cells.  ** th
15205 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72  at point to over
15206 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20  flow pages. The 
15207 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72  pointer map entr
15208 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 73  ies for all thes
15209 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 65  e.  ** pages nee
1520a 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 2e  d to be changed.
1520b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44  .  **.  ** If pD
1520c 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72  bPage is an over
1520d 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20  flow page, then 
1520e 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65  the first 4 byte
1520f 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 20  s may store a.  
15210 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20  ** pointer to a 
15211 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66  subsequent overf
15212 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68 69  low page. If thi
15213 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  s is the case, t
15214 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69  hen.  ** the poi
15215 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20 74  nter map needs t
15216 6f 20 62 65 20 75 70 64 61 74 65 64 20 66 6f 72  o be updated for
15217 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 20   the subsequent 
15218 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20  overflow page.. 
15219 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 3d   */.  if( eType=
1521a 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c  =PTRMAP_BTREE ||
1521b 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52   eType==PTRMAP_R
1521c 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72  OOTPAGE ){.    r
1521d 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d  c = setChildPtrm
1521e 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20 20  aps(pDbPage);.  
1521f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15220 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
15221 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
15222 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20  }else{.    Pgno 
15223 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 62  nextOvfl = get4b
15224 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44 61  yte(pDbPage->aDa
15225 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78  ta);.    if( nex
15226 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20  tOvfl!=0 ){.    
15227 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
15228 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c 2c 20  (pBt, nextOvfl, 
15229 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
1522a 2c 20 69 46 72 65 65 50 61 67 65 29 3b 0a 20 20  , iFreePage);.  
1522b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1522c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1522d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1522e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
1522f 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74 61   /* Fix the data
15230 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20  base pointer on 
15231 70 61 67 65 20 69 50 74 72 50 61 67 65 20 74 68  page iPtrPage th
15232 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69 44  at pointed at iD
15233 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68  bPage so.  ** th
15234 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20  at it points at 
15235 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f 20  iFreePage. Also 
15236 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 20  fix the pointer 
15237 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20  map entry for.  
15238 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20 2a  ** iPtrPage..  *
15239 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 50  /.  if( eType!=P
1523a 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29  TRMAP_ROOTPAGE )
1523b 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1523c 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
1523d 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 70  Bt, iPtrPage, &p
1523e 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 20  PtrPage, 0);.   
1523f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
15240 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
15241 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
15242 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
15243 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67 65  erWrite(pPtrPage
15244 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
15245 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15246 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  K ){.      relea
15247 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65 29  sePage(pPtrPage)
15248 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
15249 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  c;.    }.    rc 
1524a 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e  = modifyPagePoin
1524b 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 44  ter(pPtrPage, iD
1524c 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65  bPage, iFreePage
1524d 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 65  , eType);.    re
1524e 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61  leasePage(pPtrPa
1524f 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ge);.    if( rc=
15250 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15251 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50      rc = ptrmapP
15252 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 67  ut(pBt, iFreePag
15253 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61  e, eType, iPtrPa
15254 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ge);.    }.  }. 
15255 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
15256 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
15257 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 62  ation required b
15258 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70  y incrVacuumStep
15259 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  (). */.static in
1525a 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  t allocateBtreeP
1525b 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c 20  age(BtShared *, 
1525c 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e 6f  MemPage **, Pgno
1525d 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a 0a   *, Pgno, u8);..
1525e 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20  /*.** Perform a 
1525f 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 61  single step of a
15260 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61  n incremental-va
15261 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 73  cuum. If success
15262 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53  ful,.** return S
15263 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65  QLITE_OK. If the
15264 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 6f  re is no work to
15265 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 6f   do (and therefo
15266 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20 69  re no.** point i
15267 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66  n calling this f
15268 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c 20  unction again), 
15269 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
1526a 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 73  NE..**.** More s
1526b 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73 20  pecificly, this 
1526c 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74  function attempt
1526d 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a 65  s to re-organize
1526e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73   the .** databas
1526f 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c 61  e so that the la
15270 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 66  st page of the f
15271 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  ile currently in
15272 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c 6f   use.** is no lo
15273 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a  nger in use..**.
15274 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 70  ** If the nFin p
15275 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d  arameter is non-
15276 7a 65 72 6f 2c 20 74 68 65 20 69 6d 70 6c 65 6d  zero, the implem
15277 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73  entation assumes
15278 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c  .** that the cal
15279 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61  ler will keep ca
1527a 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d  lling incrVacuum
1527b 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20  Step() until.** 
1527c 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  it returns SQLIT
1527d 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72  E_DONE or an err
1527e 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69  or, and that nFi
1527f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  n is the.** numb
15280 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 20  er of pages the 
15281 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69  database file wi
15282 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72  ll contain after
15283 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73   this .** proces
15284 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a  s is complete..*
15285 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63  /.static int inc
15286 72 56 61 63 75 75 6d 53 74 65 70 28 42 74 53 68  rVacuumStep(BtSh
15287 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
15288 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74  nFin, Pgno iLast
15289 50 67 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 72 65  Pg){.  Pgno nFre
1528a 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  eList;          
1528b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
1528c 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65  ges still on the
1528d 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20   free-list */.. 
1528e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1528f 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
15290 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 66  >mutex) );..  if
15291 28 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47 45  ( !PTRMAP_ISPAGE
15292 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 26  (pBt, iLastPg) &
15293 26 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 49  & iLastPg!=PENDI
15294 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
15295 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b  ) ){.    int rc;
15296 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20  .    u8 eType;. 
15297 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65     Pgno iPtrPage
15298 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 74  ;..    nFreeList
15299 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74   = get4byte(&pBt
1529a 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
1529b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 46  36]);.    if( nF
1529c 72 65 65 4c 69 73 74 3d 3d 30 20 7c 7c 20 6e 46  reeList==0 || nF
1529d 69 6e 3d 3d 69 4c 61 73 74 50 67 20 29 7b 0a 20  in==iLastPg ){. 
1529e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1529f 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a  TE_DONE;.    }..
152a0 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
152a1 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c  et(pBt, iLastPg,
152a2 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61   &eType, &iPtrPa
152a3 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
152a4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
152a5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
152a6 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79     }.    if( eTy
152a7 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe==PTRMAP_ROOTP
152a8 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74  AGE ){.      ret
152a9 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
152aa 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a  PT_BKPT;.    }..
152ab 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50      if( eType==P
152ac 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29  TRMAP_FREEPAGE )
152ad 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e  {.      if( nFin
152ae 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
152af 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67  * Remove the pag
152b0 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73  e from the files
152b1 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73   free-list. This
152b2 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
152b3 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e  .        ** if n
152b4 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e  Fin is non-zero.
152b5 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74   In that case, t
152b6 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c  he free-list wil
152b7 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  l be.        ** 
152b8 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72  truncated to zer
152b9 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e  o after this fun
152ba 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73  ction returns, s
152bb 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20  o it doesn't .  
152bc 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20        ** matter 
152bd 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74  if it still cont
152be 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67  ains some garbag
152bf 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20  e entries..     
152c0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67     */.        Pg
152c1 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20  no iFreePg;.    
152c2 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72      MemPage *pFr
152c3 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63  eePg;.        rc
152c4 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
152c5 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65  Page(pBt, &pFree
152c6 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c  Pg, &iFreePg, iL
152c7 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20  astPg, 1);.     
152c8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
152c9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
152ca 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
152cb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
152cc 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d  ssert( iFreePg==
152cd 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20  iLastPg );.     
152ce 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
152cf 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d  FreePg);.      }
152d0 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
152d1 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67      Pgno iFreePg
152d2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
152d3 20 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70   Index of free p
152d4 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73  age to move pLas
152d5 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20  tPg to */.      
152d6 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67  MemPage *pLastPg
152d7 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ;..      rc = sq
152d8 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
152d9 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20  e(pBt, iLastPg, 
152da 26 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20  &pLastPg, 0);.  
152db 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
152dc 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
152dd 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
152de 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66    }..      /* If
152df 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74   nFin is zero, t
152e0 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78  his loop runs ex
152e1 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70  actly once and p
152e2 61 67 65 20 70 4c 61 73 74 50 67 0a 20 20 20 20  age pLastPg.    
152e3 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65 64 20    ** is swapped 
152e4 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20 66  with the first f
152e5 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65 64 20  ree page pulled 
152e6 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c 69 73  off the free lis
152e7 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  t..      **.    
152e8 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65    ** On the othe
152e9 72 20 68 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20  r hand, if nFin 
152ea 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
152eb 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a  zero, then keep.
152ec 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67        ** looping
152ed 20 75 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61   until a free-pa
152ee 67 65 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69  ge located withi
152ef 6e 20 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e  n the first nFin
152f0 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20   pages.      ** 
152f1 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 66  of the file is f
152f2 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ound..      */. 
152f3 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20       do {.      
152f4 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65    MemPage *pFree
152f5 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  Pg;.        rc =
152f6 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
152f7 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67  ge(pBt, &pFreePg
152f8 2c 20 26 69 46 72 65 65 50 67 2c 20 30 2c 20 30  , &iFreePg, 0, 0
152f9 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
152fa 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
152fb 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73            releas
152fc 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a  ePage(pLastPg);.
152fd 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
152fe 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
152ff 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
15300 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20  ge(pFreePg);.   
15301 20 20 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21     }while( nFin!
15302 3d 30 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46  =0 && iFreePg>nF
15303 69 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  in );.      asse
15304 72 74 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73  rt( iFreePg<iLas
15305 74 50 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20  tPg );.      .  
15306 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
15307 50 61 67 65 72 57 72 69 74 65 28 70 4c 61 73 74  PagerWrite(pLast
15308 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  Pg->pDbPage);.  
15309 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1530a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1530b 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61   rc = relocatePa
1530c 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c  ge(pBt, pLastPg,
1530d 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65   eType, iPtrPage
1530e 2c 20 69 46 72 65 65 50 67 2c 20 6e 46 69 6e 21  , iFreePg, nFin!
1530f 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  =0);.      }.   
15310 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
15311 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69  LastPg);.      i
15312 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
15313 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
15314 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
15315 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
15316 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69  nFin==0 ){.    i
15317 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68  LastPg--;.    wh
15318 69 6c 65 28 20 69 4c 61 73 74 50 67 3d 3d 50 45  ile( iLastPg==PE
15319 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
1531a 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50  pBt)||PTRMAP_ISP
1531b 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67  AGE(pBt, iLastPg
1531c 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 50  ) ){.      if( P
1531d 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
1531e 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20  , iLastPg) ){.  
1531f 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
15320 50 67 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  Pg;.        int 
15321 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
15322 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c  eGetPage(pBt, iL
15323 61 73 74 50 67 2c 20 26 70 50 67 2c 20 30 29 3b  astPg, &pPg, 0);
15324 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
15325 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15326 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
15327 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
15328 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
15329 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 2d  3PagerWrite(pPg-
1532a 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
1532b 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1532c 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Pg);.        if(
1532d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1532e 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
1532f 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
15330 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
15331 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 7d 0a  LastPg--;.    }.
15332 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
15333 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42  TruncateImage(pB
15334 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c 61 73 74  t->pPager, iLast
15335 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Pg);.  }.  retur
15336 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
15337 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72  /*.** A write-tr
15338 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
15339 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20  e opened before 
1533a 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e  calling this fun
1533b 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 70 65 72  ction..** It per
1533c 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c 65 20 75  forms a single u
1533d 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 6f 77 61  nit of work towa
1533e 72 64 73 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74  rds an increment
1533f 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a  al vacuum..**.**
15340 20 49 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e   If the incremen
15341 74 61 6c 20 76 61 63 75 75 6d 20 69 73 20 66 69  tal vacuum is fi
15342 6e 69 73 68 65 64 20 61 66 74 65 72 20 74 68 69  nished after thi
15343 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 72  s function has r
15344 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 4f  un,.** SQLITE_DO
15345 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  NE is returned. 
15346 49 66 20 69 74 20 69 73 20 6e 6f 74 20 66 69 6e  If it is not fin
15347 69 73 68 65 64 2c 20 62 75 74 20 6e 6f 20 65 72  ished, but no er
15348 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a  ror occurred,.**
15349 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
1534a 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
1534b 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  e an SQLite erro
1534c 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51 4c 49  r code. .*/.SQLI
1534d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1534e 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56  qlite3BtreeIncrV
1534f 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 29 7b  acuum(Btree *p){
15350 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53  .  int rc;.  BtS
15351 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
15352 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  pBt;..  sqlite3B
15353 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
15354 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  pBt->db = p->db;
15355 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
15356 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
15357 52 41 4e 53 5f 57 52 49 54 45 20 26 26 20 70 2d  RANS_WRITE && p-
15358 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
15359 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 21  WRITE );.  if( !
1535a 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
1535b 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
1535c 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65  TE_DONE;.  }else
1535d 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65  {.    invalidate
1535e 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
1535f 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20  (pBt);.    rc = 
15360 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70  incrVacuumStep(p
15361 42 74 2c 20 30 2c 20 70 61 67 65 72 50 61 67 65  Bt, 0, pagerPage
15362 63 6f 75 6e 74 28 70 42 74 29 29 3b 0a 20 20 7d  count(pBt));.  }
15363 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
15364 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
15365 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
15366 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
15367 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73  alled prior to s
15368 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
15369 74 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63  t when a transac
1536a 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69  tion.** is commi
1536b 74 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d  ted for an auto-
1536c 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e  vacuum database.
1536d 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  .**.** If SQLITE
1536e 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c  _OK is returned,
1536f 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69   then *pnTrunc i
15370 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d  s set to the num
15371 62 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20  ber of pages.** 
15372 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
15373 65 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e  e should be trun
15374 63 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20  cated to during 
15375 74 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65  the commit proce
15376 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65  ss. .** i.e. the
15377 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65   database has be
15378 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73  en reorganized s
15379 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20  o that only the 
1537a 66 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a  first *pnTrunc.*
1537b 2a 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 75  * pages are in u
1537c 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
1537d 74 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d  t autoVacuumComm
1537e 69 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  it(BtShared *pBt
1537f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
15380 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72  LITE_OK;.  Pager
15381 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e   *pPager = pBt->
15382 70 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e  pPager;.  VVA_ON
15383 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73  LY( int nRef = s
15384 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
15385 75 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 0a  unt(pPager) );..
15386 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15387 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
15388 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e  ->mutex) );.  in
15389 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66  validateAllOverf
1538a 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20  lowCache(pBt);. 
1538b 20 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75 74   assert(pBt->aut
1538c 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 20  oVacuum);.  if( 
1538d 21 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d  !pBt->incrVacuum
1538e 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69   ){.    Pgno nFi
1538f 6e 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72 65  n;.    Pgno nFre
15390 65 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 50 74 72  e;.    Pgno nPtr
15391 6d 61 70 3b 0a 20 20 20 20 50 67 6e 6f 20 69 46  map;.    Pgno iF
15392 72 65 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69  ree;.    const i
15393 6e 74 20 70 67 73 7a 20 3d 20 70 42 74 2d 3e 70  nt pgsz = pBt->p
15394 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 50 67 6e  ageSize;.    Pgn
15395 6f 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 72 50  o nOrig = pagerP
15396 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 0a  agecount(pBt);..
15397 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49      if( PTRMAP_I
15398 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67  SPAGE(pBt, nOrig
15399 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
1539a 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
1539b 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _BKPT;.    }.   
1539c 20 69 66 28 20 6e 4f 72 69 67 3d 3d 50 45 4e 44   if( nOrig==PEND
1539d 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
1539e 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 4f 72 69  t) ){.      nOri
1539f 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  g--;.    }.    n
153a0 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 28  Free = get4byte(
153a1 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44  &pBt->pPage1->aD
153a2 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e 50  ata[36]);.    nP
153a3 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e  trmap = (nFree-n
153a4 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 45  Orig+PTRMAP_PAGE
153a5 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b 70  NO(pBt, nOrig)+p
153a6 67 73 7a 2f 35 29 2f 28 70 67 73 7a 2f 35 29 3b  gsz/5)/(pgsz/5);
153a7 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69  .    nFin = nOri
153a8 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72  g - nFree - nPtr
153a9 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f 72  map;.    if( nOr
153aa 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  ig>PENDING_BYTE_
153ab 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69  PAGE(pBt) && nFi
153ac 6e 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  n<=PENDING_BYTE_
153ad 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
153ae 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d     nFin--;.    }
153af 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d  .    while( PTRM
153b0 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e  AP_ISPAGE(pBt, n
153b1 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45  Fin) || nFin==PE
153b2 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
153b3 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46  pBt) ){.      nF
153b4 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  in--;.    }..   
153b5 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 67   for(iFree=nOrig
153b6 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 20  ; iFree>nFin && 
153b7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69  rc==SQLITE_OK; i
153b8 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 72  Free--){.      r
153b9 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74  c = incrVacuumSt
153ba 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 46  ep(pBt, nFin, iF
153bb 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ree);.    }.    
153bc 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  if( (rc==SQLITE_
153bd 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  DONE || rc==SQLI
153be 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 3e  TE_OK) && nFree>
153bf 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
153c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
153c1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
153c2 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61  erWrite(pBt->pPa
153c3 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge1->pDbPage);. 
153c4 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
153c5 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
153c6 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20  a[32], 0);.     
153c7 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e   put4byte(&pBt->
153c8 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
153c9 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  ], 0);.      sql
153ca 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
153cb 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67  eImage(pBt->pPag
153cc 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 7d  er, nFin);.    }
153cd 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
153ce 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
153cf 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
153d0 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  back(pPager);.  
153d1 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72    }.  }..  asser
153d2 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 33  t( nRef==sqlite3
153d3 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50  PagerRefcount(pP
153d4 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ager) );.  retur
153d5 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  n rc;.}..#endif 
153d6 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
153d7 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
153d8 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   */../*.** This 
153d9 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65  routine does the
153da 20 66 69 72 73 74 20 70 68 61 73 65 20 6f 66 20   first phase of 
153db 61 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d 6d  a two-phase comm
153dc 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  it.  This routin
153dd 65 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72 6f  e.** causes a ro
153de 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74  llback journal t
153df 6f 20 62 65 20 63 72 65 61 74 65 64 20 28 69 66  o be created (if
153e0 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72   it does not alr
153e1 65 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20 61  eady exist).** a
153e2 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  nd populated wit
153e3 68 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61  h enough informa
153e4 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66 20  tion so that if 
153e5 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63 63  a power loss occ
153e6 75 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  urs.** the datab
153e7 61 73 65 20 63 61 6e 20 62 65 20 72 65 73 74 6f  ase can be resto
153e8 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69  red to its origi
153e9 6e 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c 61  nal state by pla
153ea 79 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68 65  ying back.** the
153eb 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e 20   journal.  Then 
153ec 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
153ed 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20  the journal are 
153ee 66 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a 2a  flushed out to.*
153ef 2a 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66 74  * the disk.  Aft
153f0 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  er the journal i
153f1 73 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69 64  s safely on oxid
153f2 65 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20 74  e, the changes t
153f3 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  o the.** databas
153f4 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e  e are written in
153f5 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
153f6 66 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65 64  file and flushed
153f7 20 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 74   to oxide..** At
153f8 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73   the end of this
153f9 20 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c 62   call, the rollb
153fa 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69 6c  ack journal stil
153fb 6c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65 0a  l exists on the.
153fc 2a 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20 61  ** disk and we a
153fd 72 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67  re still holding
153fe 20 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 74   all locks, so t
153ff 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68  he transaction h
15400 61 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74  as not.** commit
15401 74 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74 65  ted.  See sqlite
15402 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 20 66  3BtreeCommit() f
15403 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68  or the second ph
15404 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ase of the.** co
15405 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a  mmit process..**
15406 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73  .** This call is
15407 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77   a no-op if no w
15408 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
15409 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63   is currently ac
1540a 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a  tive on pBt..**.
1540b 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79  ** Otherwise, sy
1540c 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nc the database 
1540d 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72  file for the btr
1540e 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20  ee pBt. zMaster 
1540f 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65  points to.** the
15410 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65   name of a maste
15411 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  r journal file t
15412 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72  hat should be wr
15413 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a  itten into the.*
15414 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75  * individual jou
15415 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73  rnal file, or is
15416 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e   NULL, indicatin
15417 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72  g no master jour
15418 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69  nal file .** (si
15419 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
1541a 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a  ansaction)..**.*
1541b 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63  * When this is c
1541c 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65  alled, the maste
1541d 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64  r journal should
1541e 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65   already have be
1541f 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70  en.** created, p
15420 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68  opulated with th
15421 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  is journal point
15422 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f  er and synced to
15423 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63   disk..**.** Onc
15424 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e  e this is routin
15425 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20  e has returned, 
15426 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72  the only thing r
15427 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69  equired to commi
15428 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74  t.** the write-t
15429 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74  ransaction for t
1542a 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c  his database fil
1542b 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74  e is to delete t
1542c 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53  he journal..*/.S
1542d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1542e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
1542f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72  mmitPhaseOne(Btr
15430 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  ee *p, const cha
15431 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69  r *zMaster){.  i
15432 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
15433 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  K;.  if( p->inTr
15434 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
15435 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64   ){.    BtShared
15436 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
15437 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15438 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 70 42  Enter(p);.    pB
15439 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 23  t->db = p->db;.#
1543a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1543b 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1543c 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
1543d 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72  acuum ){.      r
1543e 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f  c = autoVacuumCo
1543f 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20  mmit(pBt);.     
15440 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
15441 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  OK ){.        sq
15442 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15443 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  p);.        retu
15444 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
15445 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
15446 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15447 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
15448 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61  pBt->pPager, zMa
15449 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71  ster, 0);.    sq
1544a 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
1544b 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p);.  }.  return
1544c 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   rc;.}../*.** Co
1544d 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63  mmit the transac
1544e 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69  tion currently i
1544f 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a  n progress..**.*
15450 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
15451 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65  mplements the se
15452 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20  cond phase of a 
15453 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20  2-phase commit. 
15454 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42   The.** sqlite3B
15455 74 72 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69  treeSync() routi
15456 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73  ne does the firs
15457 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75  t phase and shou
15458 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a  ld be invoked.**
15459 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
1545a 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  g this routine. 
1545b 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 65   The sqlite3Btre
1545c 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 20  eSync() routine 
1545d 64 69 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 77  did.** all the w
1545e 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69  ork of writing i
1545f 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74  nformation out t
15460 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68  o disk and flush
15461 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65  ing the.** conte
15462 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79  nts so that they
15463 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74   are written ont
15464 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74  o the disk platt
15465 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a  er.  All this.**
15466 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20   routine has to 
15467 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20  do is delete or 
15468 74 72 75 6e 63 61 74 65 20 74 68 65 20 72 6f 6c  truncate the rol
15469 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  lback journal.**
1546a 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 74   (which causes t
1546b 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  he transaction t
1546c 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 72  o commit) and dr
1546d 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  op locks..**.** 
1546e 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73  This will releas
1546f 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b  e the write lock
15470 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
15471 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65   file.  If there
15472 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76  .** are no activ
15473 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c  e cursors, it al
15474 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20  so releases the 
15475 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51  read lock..*/.SQ
15476 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15477 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
15478 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65  mitPhaseTwo(Btre
15479 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65  e *p){.  BtShare
1547a 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
1547b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
1547c 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d  Enter(p);.  pBt-
1547d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 62  >db = p->db;.  b
1547e 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29  treeIntegrity(p)
1547f 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68  ;..  /* If the h
15480 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69 74  andle has a writ
15481 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  e-transaction op
15482 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73  en, commit the s
15483 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20  hared-btrees .  
15484 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  ** transaction a
15485 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72 65  nd set the share
15486 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53  d state to TRANS
15487 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66  _READ..  */.  if
15488 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
15489 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20  ANS_WRITE ){.   
1548a 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73   int rc;.    ass
1548b 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
1548c 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
1548d 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65  RITE );.    asse
1548e 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61  rt( pBt->nTransa
1548f 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72  ction>0 );.    r
15490 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
15491 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
15492 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
15493 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
15494 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
15495 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
15496 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
15497 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  c;.    }.    pBt
15498 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20  ->inTransaction 
15499 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20  = TRANS_READ;.  
1549a 7d 0a 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 72  }.  clearAllShar
1549b 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
1549c 73 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  s(p);..  /* If t
1549d 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6e  he handle has an
1549e 79 20 6b 69 6e 64 20 6f 66 20 74 72 61 6e 73 61  y kind of transa
1549f 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 64 65 63 72  ction open, decr
154a0 65 6d 65 6e 74 20 74 68 65 20 74 72 61 6e 73 61  ement the transa
154a1 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e 74  ction.  ** count
154a2 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 62   of the shared b
154a3 74 72 65 65 2e 20 49 66 20 74 68 65 20 74 72 61  tree. If the tra
154a4 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 72  nsaction count r
154a5 65 61 63 68 65 73 20 30 2c 20 73 65 74 0a 20 20  eaches 0, set.  
154a6 2a 2a 20 74 68 65 20 73 68 61 72 65 64 20 73 74  ** the shared st
154a7 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e  ate to TRANS_NON
154a8 45 2e 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72  E. The unlockBtr
154a9 65 65 49 66 55 6e 75 73 65 64 28 29 20 63 61 6c  eeIfUnused() cal
154aa 6c 20 62 65 6c 6f 77 0a 20 20 2a 2a 20 77 69 6c  l below.  ** wil
154ab 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67  l unlock the pag
154ac 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  er..  */.  if( p
154ad 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53  ->inTrans!=TRANS
154ae 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 70 42 74  _NONE ){.    pBt
154af 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d  ->nTransaction--
154b0 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74  ;.    if( 0==pBt
154b1 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29  ->nTransaction )
154b2 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54  {.      pBt->inT
154b3 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41  ransaction = TRA
154b4 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20  NS_NONE;.    }. 
154b5 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
154b6 20 68 61 6e 64 6c 65 73 20 63 75 72 72 65 6e 74   handles current
154b7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61   transaction sta
154b8 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45  te to TRANS_NONE
154b9 20 61 6e 64 20 75 6e 6c 6f 63 6b 0a 20 20 2a 2a   and unlock.  **
154ba 20 74 68 65 20 70 61 67 65 72 20 69 66 20 74 68   the pager if th
154bb 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74  is call closed t
154bc 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20  he only read or 
154bd 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
154be 6e 2e 0a 20 20 2a 2f 0a 20 20 62 74 72 65 65 43  n..  */.  btreeC
154bf 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 70  learHasContent(p
154c0 42 74 29 3b 0a 20 20 70 2d 3e 69 6e 54 72 61 6e  Bt);.  p->inTran
154c1 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  s = TRANS_NONE;.
154c2 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55    unlockBtreeIfU
154c3 6e 75 73 65 64 28 70 42 74 29 3b 0a 0a 20 20 62  nused(pBt);..  b
154c4 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29  treeIntegrity(p)
154c5 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
154c6 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
154c7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
154c8 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70  ./*.** Do both p
154c9 68 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69  hases of a commi
154ca 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
154cb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
154cc 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65  BtreeCommit(Btre
154cd 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
154ce 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
154cf 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
154d0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
154d1 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29  itPhaseOne(p, 0)
154d2 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
154d3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
154d4 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
154d5 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29 3b  mmitPhaseTwo(p);
154d6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
154d7 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
154d8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
154d9 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a  ndef NDEBUG./*.*
154da 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
154db 62 65 72 20 6f 66 20 77 72 69 74 65 2d 63 75 72  ber of write-cur
154dc 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69  sors open on thi
154dd 73 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 69  s handle. This i
154de 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20  s for use.** in 
154df 61 73 73 65 72 74 28 29 20 65 78 70 72 65 73 73  assert() express
154e0 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 20 6f  ions, so it is o
154e1 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 20  nly compiled if 
154e2 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a  NDEBUG is not.**
154e3 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
154e4 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
154e5 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
154e6 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72  , a write-cursor
154e7 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74   is any cursor t
154e8 68 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c  hat.** is capabl
154e9 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20  e of writing to 
154ea 74 68 65 20 64 61 74 61 62 73 65 2e 20 20 54 68  the databse.  Th
154eb 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75 72  at means the cur
154ec 73 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69  sor was.** origi
154ed 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72  nally opened for
154ee 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74 68 65   writing and the
154ef 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f 74 20   cursor has not 
154f0 62 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 62  be disabled.** b
154f1 79 20 68 61 76 69 6e 67 20 69 74 73 20 73 74 61  y having its sta
154f2 74 65 20 63 68 61 6e 67 65 64 20 74 6f 20 43 55  te changed to CU
154f3 52 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73  RSOR_FAULT..*/.s
154f4 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 57  tatic int countW
154f5 72 69 74 65 43 75 72 73 6f 72 73 28 42 74 53 68  riteCursors(BtSh
154f6 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74  ared *pBt){.  Bt
154f7 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20  Cursor *pCur;.  
154f8 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 6f 72  int r = 0;.  for
154f9 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 72 73  (pCur=pBt->pCurs
154fa 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72 3d 70  or; pCur; pCur=p
154fb 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Cur->pNext){.   
154fc 20 69 66 28 20 70 43 75 72 2d 3e 77 72 46 6c 61   if( pCur->wrFla
154fd 67 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74  g && pCur->eStat
154fe 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  e!=CURSOR_FAULT 
154ff 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65  ) r++; .  }.  re
15500 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66  turn r;.}.#endif
15501 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
15502 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74  tine sets the st
15503 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41  ate to CURSOR_FA
15504 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f  ULT and the erro
15505 72 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72  r.** code to err
15506 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 63  Code for every c
15507 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65  ursor on BtShare
15508 64 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a  d that pBtree.**
15509 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a   references..**.
1550a 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20  ** Every cursor 
1550b 69 73 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c  is tripped, incl
1550c 75 64 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68  uding cursors th
1550d 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20  at belong.** to 
1550e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
1550f 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20  onnections that 
15510 68 61 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61  happen to be sha
15511 72 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68  ring.** the cach
15512 65 20 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a  e with pBtree..*
15513 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
15514 65 20 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68  e gets called wh
15515 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63  en a rollback oc
15516 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72  curs..** All cur
15517 73 6f 72 73 20 75 73 69 6e 67 20 74 68 65 20 73  sors using the s
15518 61 6d 65 20 63 61 63 68 65 20 6d 75 73 74 20 62  ame cache must b
15519 65 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20  e tripped.** to 
1551a 70 72 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f  prevent them fro
1551b 6d 20 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20  m trying to use 
1551c 74 68 65 20 62 74 72 65 65 20 61 66 74 65 72 0a  the btree after.
1551d 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e  ** the rollback.
1551e 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d    The rollback m
1551f 61 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20  ay have deleted 
15520 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76  tables.** or mov
15521 65 64 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73  ed root pages, s
15522 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66  o it is not suff
15523 69 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76  icient to.** sav
15524 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
15525 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20  he cursor.  The 
15526 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a  cursor must be.*
15527 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a  * invalidated..*
15528 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15529 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
1552a 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73  eeTripAllCursors
1552b 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20  (Btree *pBtree, 
1552c 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20  int errCode){.  
1552d 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 73  BtCursor *p;.  s
1552e 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1552f 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 28  (pBtree);.  for(
15530 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70  p=pBtree->pBt->p
15531 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e  Cursor; p; p=p->
15532 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20  pNext){.    int 
15533 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  i;.    sqlite3Bt
15534 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70  reeClearCursor(p
15535 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 65  );.    p->eState
15536 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b   = CURSOR_FAULT;
15537 0a 20 20 20 20 70 2d 3e 73 6b 69 70 20 3d 20 65  .    p->skip = e
15538 72 72 43 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28  rrCode;.    for(
15539 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65  i=0; i<=p->iPage
1553a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65  ; i++){.      re
1553b 6c 65 61 73 65 50 61 67 65 28 70 2d 3e 61 70 50  leasePage(p->apP
1553c 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70  age[i]);.      p
1553d 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b  ->apPage[i] = 0;
1553e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
1553f 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
15540 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Btree);.}../*.**
15541 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72   Rollback the tr
15542 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f  ansaction in pro
15543 67 72 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73  gress.  All curs
15544 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69  ors will be.** i
15545 6e 76 61 6c 69 64 65 64 20 62 79 20 74 68 69 73  nvalided by this
15546 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79   operation.  Any
15547 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
15548 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74  a cursor.** that
15549 20 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65   was open at the
1554a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
1554b 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c  is operation wil
1554c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61  l result.** in a
1554d 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  n error..**.** T
1554e 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65  his will release
1554f 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20   the write lock 
15550 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
15551 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a  file.  If there.
15552 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  ** are no active
15553 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73   cursors, it als
15554 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72  o releases the r
15555 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c  ead lock..*/.SQL
15556 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15557 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
15558 62 61 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a  back(Btree *p){.
15559 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68    int rc;.  BtSh
1555a 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
1555b 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  Bt;.  MemPage *p
1555c 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65  Page1;..  sqlite
1555d 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
1555e 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64    pBt->db = p->d
1555f 62 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c  b;.  rc = saveAl
15560 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c  lCursors(pBt, 0,
15561 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   0);.#ifndef SQL
15562 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
15563 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21 3d  CACHE.  if( rc!=
15564 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15565 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68 6f   /* This is a ho
15566 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f 6e  rrible situation
15567 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f  . An IO or mallo
15568 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 72  c() error occurr
15569 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a  ed whilst.    **
1556a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65 20   trying to save 
1556b 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73  cursor positions
1556c 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  . If this is an 
1556d 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61  automatic rollba
1556e 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74 68  ck (as.    ** th
1556f 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63 6f  e result of a co
15570 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63  nstraint, malloc
15571 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49 4f  () failure or IO
15572 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20 20   error) then .  
15573 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 6d    ** the cache m
15574 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79  ay be internally
15575 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e   inconsistent (n
15576 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64  ot contain valid
15577 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20 2a   trees) so.    *
15578 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70  * we cannot simp
15579 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  ly return the er
1557a 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ror to the calle
1557b 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f 72  r. Instead, abor
1557c 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75  t .    ** all qu
1557d 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20 62  eries that may b
1557e 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20 74  e using any of t
1557f 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  he cursors that 
15580 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a  failed to save..
15581 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
15582 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75  e3BtreeTripAllCu
15583 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20 20  rsors(p, rc);.  
15584 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65 65  }.#endif.  btree
15585 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20  Integrity(p);.  
15586 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61  clearAllSharedCa
15587 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29  cheTableLocks(p)
15588 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  ;..  if( p->inTr
15589 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
1558a 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b   ){.    int rc2;
1558b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 52  ..    assert( TR
1558c 41 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e  ANS_WRITE==pBt->
1558d 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b  inTransaction );
1558e 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74  .    rc2 = sqlit
1558f 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
15590 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
15591 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54    if( rc2!=SQLIT
15592 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
15593 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20   = rc2;.    }.. 
15594 20 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61     /* The rollba
15595 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 74  ck may have dest
15596 72 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 31  royed the pPage1
15597 2d 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 20  ->aData value.  
15598 53 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 73  So.    ** call s
15599 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
1559a 67 65 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61  ge() on page 1 a
1559b 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20  gain to make.   
1559c 20 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d   ** sure pPage1-
1559d 3e 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f  >aData is set co
1559e 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20  rrectly. */.    
1559f 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  if( sqlite3Btree
155a0 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20  GetPage(pBt, 1, 
155a1 26 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c  &pPage1, 0)==SQL
155a2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
155a3 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
155a4 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  e1);.    }.    a
155a5 73 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74  ssert( countWrit
155a6 65 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30  eCursors(pBt)==0
155a7 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54   );.    pBt->inT
155a8 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41  ransaction = TRA
155a9 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20  NS_READ;.  }..  
155aa 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d  if( p->inTrans!=
155ab 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20  TRANS_NONE ){.  
155ac 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e    assert( pBt->n
155ad 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b  Transaction>0 );
155ae 0a 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73  .    pBt->nTrans
155af 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66  action--;.    if
155b0 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73  ( 0==pBt->nTrans
155b1 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
155b2 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
155b3 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b  on = TRANS_NONE;
155b4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 74  .    }.  }..  bt
155b5 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65  reeClearHasConte
155b6 6e 74 28 70 42 74 29 3b 0a 20 20 70 2d 3e 69 6e  nt(pBt);.  p->in
155b7 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f  Trans = TRANS_NO
155b8 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65  NE;.  unlockBtre
155b9 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
155ba 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74  .  btreeIntegrit
155bb 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  y(p);.  sqlite3B
155bc 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
155bd 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
155be 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 74  .** Start a stat
155bf 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63  ement subtransac
155c0 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 61  tion. The subtra
155c1 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 63 61 6e  nsaction can can
155c2 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61   be rolled.** ba
155c3 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79  ck independently
155c4 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61   of the main tra
155c5 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75  nsaction. You mu
155c6 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73  st start a trans
155c7 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72  action .** befor
155c8 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 62  e starting a sub
155c9 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65  transaction. The
155ca 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20   subtransaction 
155cb 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 74  is ended automat
155cc 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68  ically .** if th
155cd 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69  e main transacti
155ce 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f  on commits or ro
155cf 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  lls back..**.** 
155d0 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61  Statement subtra
155d1 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73  nsactions are us
155d2 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69  ed around indivi
155d3 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65  dual SQL stateme
155d4 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  nts.** that are 
155d5 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
155d6 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49   a BEGIN...COMMI
155d7 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63  T block.  If a c
155d8 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72  onstraint.** err
155d9 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  or occurs within
155da 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20   the statement, 
155db 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68  the effect of th
155dc 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74  at one statement
155dd 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65  .** can be rolle
155de 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68  d back without h
155df 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63  aving to rollbac
155e0 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61  k the entire tra
155e1 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
155e2 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 2d  A statement sub-
155e3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 69  transaction is i
155e4 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 6e  mplemented as an
155e5 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70   anonymous savep
155e6 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c  oint. The.** val
155e7 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ue passed as the
155e8 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
155e9 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  r is the total n
155ea 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69  umber of savepoi
155eb 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e  nts,.** includin
155ec 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d  g the new anonym
155ed 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f  ous savepoint, o
155ee 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72 65  pen on the B-Tre
155ef 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72 65  e. i.e. if there
155f0 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76  .** are no activ
155f1 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64  e savepoints and
155f2 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d   no other statem
155f3 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73  ent-transactions
155f4 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65   open,.** iState
155f5 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73 20  ment is 1. This 
155f6 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f  anonymous savepo
155f7 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65 61  int can be relea
155f8 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61  sed or rolled ba
155f9 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  ck.** using the 
155fa 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65  sqlite3BtreeSave
155fb 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e  point() function
155fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
155fd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
155fe 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74  treeBeginStmt(Bt
155ff 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74 61  ree *p, int iSta
15600 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72  tement){.  int r
15601 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  c;.  BtShared *p
15602 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73  Bt = p->pBt;.  s
15603 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
15604 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d  (p);.  pBt->db =
15605 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74   p->db;.  assert
15606 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
15607 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61  ANS_WRITE );.  a
15608 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64  ssert( pBt->read
15609 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Only==0 );.  ass
1560a 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e  ert( iStatement>
1560b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
1560c 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d  Statement>p->db-
1560d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20  >nSavepoint );. 
1560e 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e   if( NEVER(p->in
1560f 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49  Trans!=TRANS_WRI
15610 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f  TE || pBt->readO
15611 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  nly) ){.    rc =
15612 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c   SQLITE_INTERNAL
15613 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
15614 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72  ssert( pBt->inTr
15615 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
15616 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a  _WRITE );.    /*
15617 20 41 74 20 74 68 65 20 70 61 67 65 72 20 6c 65   At the pager le
15618 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74  vel, a statement
15619 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1561a 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68  a savepoint with
1561b 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78  .    ** an index
1561c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c   greater than al
1561d 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65  l savepoints cre
1561e 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20  ated explicitly 
1561f 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c  using.    ** SQL
15620 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20   statements. It 
15621 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70  is illegal to op
15622 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72  en, release or r
15623 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20  ollback any.    
15624 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e  ** such savepoin
15625 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 74 61  ts while the sta
15626 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
15627 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20  on savepoint is 
15628 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20  active..    */. 
15629 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1562a 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e  agerOpenSavepoin
1562b 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
1562c 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a  Statement);.  }.
1562d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1562e 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1562f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
15630 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
15631 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
15632 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79  on, op, is alway
15633 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  s SAVEPOINT_ROLL
15634 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50  BACK.** or SAVEP
15635 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68  OINT_RELEASE. Th
15636 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68  is function eith
15637 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72  er releases or r
15638 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a  olls back the.**
15639 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74   savepoint ident
1563a 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
1563b 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64  er iSavepoint, d
1563c 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
1563d 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e  value .** of op.
1563e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c  .**.** Normally,
1563f 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67   iSavepoint is g
15640 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
15641 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f  qual to zero. Ho
15642 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a  wever, if op is.
15643 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  ** SAVEPOINT_ROL
15644 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76  LBACK, then iSav
15645 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20  epoint may also 
15646 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63  be -1. In this c
15647 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74  ase the .** cont
15648 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69  ents of the enti
15649 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  re transaction a
1564a 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  re rolled back. 
1564b 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e  This is differen
1564c 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d  t.** from a norm
1564d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72  al transaction r
1564e 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c  ollback, as no l
1564f 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65  ocks are release
15650 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61  d and the.** tra
15651 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73  nsaction remains
15652 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45   open..*/.SQLITE
15653 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15654 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
15655 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  nt(Btree *p, int
15656 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f   op, int iSavepo
15657 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  int){.  int rc =
15658 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
15659 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e  ( p && p->inTran
1565a 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
1565b 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a  {.    BtShared *
1565c 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1565d 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41    assert( op==SA
1565e 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20  VEPOINT_RELEASE 
1565f 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  || op==SAVEPOINT
15660 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20  _ROLLBACK );.   
15661 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f   assert( iSavepo
15662 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65  int>=0 || (iSave
15663 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d  point==-1 && op=
15664 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
15665 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  ACK) );.    sqli
15666 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
15667 3b 0a 20 20 20 20 70 42 74 2d 3e 64 62 20 3d 20  ;.    pBt->db = 
15668 70 2d 3e 64 62 3b 0a 20 20 20 20 72 63 20 3d 20  p->db;.    rc = 
15669 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65  sqlite3PagerSave
1566a 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65  point(pBt->pPage
1566b 72 2c 20 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e  r, op, iSavepoin
1566c 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  t);.    if( rc==
1566d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1566e 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 62     rc = newDatab
1566f 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a  ase(pBt);.    }.
15670 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15671 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20  Leave(p);.  }.  
15672 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
15673 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
15674 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20   cursor for the 
15675 42 54 72 65 65 20 77 68 6f 73 65 20 72 6f 6f 74  BTree whose root
15676 20 69 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a   is on the page.
15677 2a 2a 20 69 54 61 62 6c 65 2e 20 20 54 68 65 20  ** iTable.  The 
15678 61 63 74 20 6f 66 20 61 63 71 75 69 72 69 6e 67  act of acquiring
15679 20 61 20 63 75 72 73 6f 72 20 67 65 74 73 20 61   a cursor gets a
1567a 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a   read lock on .*
1567b 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
1567c 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72  ile..**.** If wr
1567d 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68  Flag==0, then th
1567e 65 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c  e cursor can onl
1567f 79 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65  y be used for re
15680 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46  ading..** If wrF
15681 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65  lag==1, then the
15682 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 75   cursor can be u
15683 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20  sed for reading 
15684 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e  or for.** writin
15685 67 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 69  g if other condi
15686 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 6e  tions for writin
15687 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20  g are also met. 
15688 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 68   These.** are th
15689 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61  e conditions tha
1568a 74 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e  t must be met in
1568b 20 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 69   order for writi
1568c 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f  ng to.** be allo
1568d 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54  wed:.**.** 1:  T
1568e 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68  he cursor must h
1568f 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20  ave been opened 
15690 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a  with wrFlag==1.*
15691 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64  *.** 2:  Other d
15692 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
15693 6f 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74  ons that share t
15694 68 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61  he same pager ca
15695 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77  che.**     but w
15696 68 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20  hich are not in 
15697 74 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49  the READ_UNCOMMI
15698 54 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e  TTED state may n
15699 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63  ot have.**     c
1569a 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68  ursors open with
1569b 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68   wrFlag==0 on th
1569c 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f  e same table.  O
1569d 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20  therwise.**     
1569e 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65  the changes made
1569f 20 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63   by this write c
156a0 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76  ursor would be v
156a1 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20  isible to.**    
156a2 20 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72   the read cursor
156a3 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64  s in the other d
156a4 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
156a5 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68  on..**.** 3:  Th
156a6 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20  e database must 
156a7 62 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74  be writable (not
156a8 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65   on read-only me
156a9 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54  dia).**.** 4:  T
156aa 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20  here must be an 
156ab 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69  active transacti
156ac 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65  on..**.** No che
156ad 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f  cking is done to
156ae 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
156af 70 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 6c  page iTable real
156b0 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f  ly is the.** roo
156b1 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72  t page of a b-tr
156b2 65 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f  ee.  If it is no
156b3 74 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73  t, then the curs
156b4 6f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 77  or acquired.** w
156b5 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72  ill not work cor
156b6 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74  rectly..**.** It
156b7 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
156b8 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65   the sqlite3Btre
156b9 65 43 75 72 73 6f 72 53 69 7a 65 28 29 20 62 79  eCursorSize() by
156ba 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a  tes of memory .*
156bb 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  * pointed to by 
156bc 70 43 75 72 20 68 61 76 65 20 62 65 65 6e 20 7a  pCur have been z
156bd 65 72 6f 65 64 20 62 79 20 74 68 65 20 63 61 6c  eroed by the cal
156be 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ler..*/.static i
156bf 6e 74 20 62 74 72 65 65 43 75 72 73 6f 72 28 0a  nt btreeCursor(.
156c0 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20    Btree *p,     
156c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156c2 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
156c3 62 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69  btree */.  int i
156c4 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20  Table,          
156c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156c6 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
156c7 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  f table to open 
156c8 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c  */.  int wrFlag,
156c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
156cb 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61   to write. 0 rea
156cc 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75  d-only */.  stru
156cd 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  ct KeyInfo *pKey
156ce 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20  Info,           
156cf 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20     /* First arg 
156d0 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75  to comparison fu
156d1 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75  nction */.  BtCu
156d2 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20  rsor *pCur      
156d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
156d4 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20     /* Space for 
156d5 6e 65 77 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b  new cursor */.){
156d6 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 6e  .  int rc;.  Pgn
156d7 6f 20 6e 50 61 67 65 3b 0a 20 20 42 74 53 68 61  o nPage;.  BtSha
156d8 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
156d9 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  t;..  assert( sq
156da 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
156db 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73  utex(p) );.  ass
156dc 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c  ert( wrFlag==0 |
156dd 7c 20 77 72 46 6c 61 67 3d 3d 31 20 29 3b 0a 20  | wrFlag==1 );. 
156de 20 69 66 28 20 77 72 46 6c 61 67 20 29 7b 0a 20   if( wrFlag ){. 
156df 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d     assert( !pBt-
156e0 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 20  >readOnly );.   
156e1 20 69 66 28 20 4e 45 56 45 52 28 70 42 74 2d 3e   if( NEVER(pBt->
156e2 72 65 61 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20  readOnly) ){.   
156e3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
156e4 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d  _READONLY;.    }
156e5 0a 20 20 20 20 72 63 20 3d 20 63 68 65 63 6b 46  .    rc = checkF
156e6 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28  orReadConflicts(
156e7 70 2c 20 69 54 61 62 6c 65 2c 20 30 2c 20 30 29  p, iTable, 0, 0)
156e8 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
156e9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
156ea 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
156eb 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45  ITE_LOCKED_SHARE
156ec 44 43 41 43 48 45 20 29 3b 0a 20 20 20 20 20 20  DCACHE );.      
156ed 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
156ee 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d  .  }..  if( pBt-
156ef 3e 70 50 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20  >pPage1==0 ){.  
156f0 20 20 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65    rc = lockBtree
156f1 57 69 74 68 52 65 74 72 79 28 70 29 3b 0a 20 20  WithRetry(p);.  
156f2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
156f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
156f4 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
156f5 7d 0a 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f  }.  pCur->pgnoRo
156f6 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 61 62 6c  ot = (Pgno)iTabl
156f7 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  e;.  rc = sqlite
156f8 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
156f9 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 28 69 6e  pBt->pPager, (in
156fa 74 20 2a 29 26 6e 50 61 67 65 29 3b 20 0a 20 20  t *)&nPage); .  
156fb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
156fc 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
156fd 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 54  rc;.  }.  if( iT
156fe 61 62 6c 65 3d 3d 31 20 26 26 20 6e 50 61 67 65  able==1 && nPage
156ff 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
15700 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20  SQLITE_EMPTY;.  
15701 20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 63 75    goto create_cu
15702 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3b 0a  rsor_exception;.
15703 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 41 6e    }.  rc = getAn
15704 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70  dInitPage(pBt, p
15705 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26  Cur->pgnoRoot, &
15706 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29  pCur->apPage[0])
15707 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
15708 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
15709 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f 72 5f  o create_cursor_
1570a 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a  exception;.  }..
1570b 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f    /* Now that no
1570c 20 6f 74 68 65 72 20 65 72 72 6f 72 73 20 63 61   other errors ca
1570d 6e 20 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20  n occur, finish 
1570e 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42  filling in the B
1570f 74 43 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72  tCursor.  ** var
15710 69 61 62 6c 65 73 2c 20 6c 69 6e 6b 20 74 68 65  iables, link the
15711 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65   cursor into the
15712 20 42 74 53 68 61 72 65 64 20 6c 69 73 74 20 61   BtShared list a
15713 6e 64 20 73 65 74 20 2a 70 70 43 75 72 20 28 74  nd set *ppCur (t
15714 68 65 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 61  he.  ** output a
15715 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
15716 66 75 6e 63 74 69 6f 6e 29 2e 0a 20 20 2a 2f 0a  function)..  */.
15717 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f    pCur->pKeyInfo
15718 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70   = pKeyInfo;.  p
15719 43 75 72 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b  Cur->pBtree = p;
1571a 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 3d 20 70  .  pCur->pBt = p
1571b 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c  Bt;.  pCur->wrFl
1571c 61 67 20 3d 20 28 75 38 29 77 72 46 6c 61 67 3b  ag = (u8)wrFlag;
1571d 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d  .  pCur->pNext =
1571e 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20   pBt->pCursor;. 
1571f 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74   if( pCur->pNext
15720 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e   ){.    pCur->pN
15721 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75  ext->pPrev = pCu
15722 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43  r;.  }.  pBt->pC
15723 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20  ursor = pCur;.  
15724 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
15725 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20  URSOR_INVALID;. 
15726 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77   pCur->cachedRow
15727 69 64 20 3d 20 30 3b 0a 0a 20 20 72 65 74 75 72  id = 0;..  retur
15728 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 63 72  n SQLITE_OK;..cr
15729 65 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65  eate_cursor_exce
1572a 70 74 69 6f 6e 3a 0a 20 20 72 65 6c 65 61 73 65  ption:.  release
1572b 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67  Page(pCur->apPag
1572c 65 5b 30 5d 29 3b 0a 20 20 75 6e 6c 6f 63 6b 42  e[0]);.  unlockB
1572d 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74  treeIfUnused(pBt
1572e 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1572f 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
15730 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
15731 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65  eCursor(.  Btree
15732 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
15733 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15734 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74         /* The bt
15735 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ree */.  int iTa
15736 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ble,            
15737 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15738 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
15739 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70  e of table to op
1573a 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c  en */.  int wrFl
1573b 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ag,             
1573c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1573d 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74      /* 1 to writ
1573e 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a  e. 0 read-only *
1573f 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e  /.  struct KeyIn
15740 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20  fo *pKeyInfo,   
15741 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15742 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20  /* First arg to 
15743 78 43 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20  xCompare() */.  
15744 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20  BtCursor *pCur  
15745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15746 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
15747 72 69 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20  rite new cursor 
15748 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
15749 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
1574a 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
1574b 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64  ->pBt->db = p->d
1574c 62 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43  b;.  rc = btreeC
1574d 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c  ursor(p, iTable,
1574e 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66   wrFlag, pKeyInf
1574f 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69  o, pCur);.  sqli
15750 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
15751 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
15752 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
15753 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74 43  he size of a BtC
15754 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20  ursor object in 
15755 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  bytes..**.** Thi
15756 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20  s interfaces is 
15757 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 75  needed so that u
15758 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 20  sers of cursors 
15759 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a  can preallocate.
1575a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 74  ** sufficient st
1575b 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 20  orage to hold a 
1575c 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 43  cursor.  The BtC
1575d 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 20  ursor object is 
1575e 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65  opaque.** to use
1575f 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f  rs so they canno
15760 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66 28  t do the sizeof(
15761 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20 74  ) themselves - t
15762 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a  hey must call.**
15763 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
15764 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15765 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
15766 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64  eCursorSize(void
15767 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65  ){.  return size
15768 6f 66 28 42 74 43 75 72 73 6f 72 29 3b 0a 7d 0a  of(BtCursor);.}.
15769 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63  ./*.** Set the c
1576a 61 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75  ached rowid valu
1576b 65 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f  e of every curso
1576c 72 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61  r in the same da
1576d 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61  tabase file.** a
1576e 73 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e  s pCur and havin
1576f 67 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20  g the same root 
15770 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70  page number as p
15771 43 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20  Cur.  The value 
15772 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f  is.** set to iRo
15773 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  wid..**.** Only 
15774 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 76  positive rowid v
15775 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64  alues are consid
15776 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74  ered valid for t
15777 68 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68  his cache..** Th
15778 65 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 69  e cache is initi
15779 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20  alized to zero, 
1577a 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e  indicating an in
1577b 76 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20  valid cache..** 
1577c 41 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72  A btree will wor
1577d 6b 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f  k fine with zero
1577e 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77   or negative row
1577f 69 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61  ids.  We just ca
15780 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65  nnot.** cache ze
15781 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72  ro or negative r
15782 6f 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61  owids, which mea
15783 6e 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 75  ns tables that u
15784 73 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65  se zero or.** ne
15785 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69  gative rowids mi
15786 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65  ght run a little
15787 20 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e   slower.  But in
15788 20 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a   practice, zero.
15789 2a 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72  ** or negative r
1578a 6f 77 69 64 73 20 61 72 65 20 76 65 72 79 20 75  owids are very u
1578b 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20  ncommon so this 
1578c 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20  should not be a 
1578d 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49  problem..*/.SQLI
1578e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1578f 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
15790 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72  achedRowid(BtCur
15791 73 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74  sor *pCur, sqlit
15792 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29  e3_int64 iRowid)
15793 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  {.  BtCursor *p;
15794 0a 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70  .  for(p=pCur->p
15795 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
15796 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
15797 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74   if( p->pgnoRoot
15798 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  ==pCur->pgnoRoot
15799 20 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69   ) p->cachedRowi
1579a 64 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a  d = iRowid;.  }.
1579b 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1579c 63 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f  cachedRowid==iRo
1579d 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  wid );.}../*.** 
1579e 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 65  Return the cache
1579f 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  d rowid for the 
157a0 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41  given cursor.  A
157a1 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72   negative or zer
157a2 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75  o.** return valu
157a3 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  e indicates that
157a4 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65   the rowid cache
157a5 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20   is invalid and 
157a6 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e  should be.** ign
157a7 6f 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f  ored.  If the ro
157a8 77 69 64 20 63 61 63 68 65 20 68 61 73 20 6e 65  wid cache has ne
157a9 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20  ver before been 
157aa 73 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a  set, then a.** z
157ab 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ero is returned.
157ac 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
157ad 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  TE sqlite3_int64
157ae 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
157af 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75  CachedRowid(BtCu
157b0 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72  rsor *pCur){.  r
157b1 65 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68  eturn pCur->cach
157b2 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  edRowid;.}../*.*
157b3 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72  * Close a cursor
157b4 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b  .  The read lock
157b5 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
157b6 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65   file is release
157b7 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61  d.** when the la
157b8 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f  st cursor is clo
157b9 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sed..*/.SQLITE_P
157ba 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
157bb 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73  e3BtreeCloseCurs
157bc 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
157bd 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74  r){.  Btree *pBt
157be 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72  ree = pCur->pBtr
157bf 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65  ee;.  if( pBtree
157c0 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
157c1 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74     BtShared *pBt
157c2 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20   = pCur->pBt;.  
157c3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
157c4 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20  ter(pBtree);.   
157c5 20 70 42 74 2d 3e 64 62 20 3d 20 70 42 74 72 65   pBt->db = pBtre
157c6 65 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74  e->db;.    sqlit
157c7 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
157c8 6f 72 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66  or(pCur);.    if
157c9 28 20 70 43 75 72 2d 3e 70 50 72 65 76 20 29 7b  ( pCur->pPrev ){
157ca 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 50 72  .      pCur->pPr
157cb 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75 72  ev->pNext = pCur
157cc 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c  ->pNext;.    }el
157cd 73 65 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70  se{.      pBt->p
157ce 43 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70  Cursor = pCur->p
157cf 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
157d0 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20  if( pCur->pNext 
157d1 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70  ){.      pCur->p
157d2 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43  Next->pPrev = pC
157d3 75 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d  ur->pPrev;.    }
157d4 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
157d5 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b  =pCur->iPage; i+
157d6 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73  +){.      releas
157d7 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61  ePage(pCur->apPa
157d8 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  ge[i]);.    }.  
157d9 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55    unlockBtreeIfU
157da 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20  nused(pBt);.    
157db 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c  invalidateOverfl
157dc 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20  owCache(pCur);. 
157dd 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72     /* sqlite3_fr
157de 65 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20  ee(pCur); */.   
157df 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
157e0 76 65 28 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a  ve(pBtree);.  }.
157e1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
157e2 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  OK;.}../*.** Mak
157e3 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75  e a temporary cu
157e4 72 73 6f 72 20 62 79 20 66 69 6c 6c 69 6e 67 20  rsor by filling 
157e5 69 6e 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66  in the fields of
157e6 20 70 54 65 6d 70 43 75 72 2e 0a 2a 2a 20 54 68   pTempCur..** Th
157e7 65 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73  e temporary curs
157e8 6f 72 20 69 73 20 6e 6f 74 20 6f 6e 20 74 68 65  or is not on the
157e9 20 63 75 72 73 6f 72 20 6c 69 73 74 20 66 6f 72   cursor list for
157ea 20 74 68 65 20 42 74 72 65 65 2e 0a 2a 2f 0a 53   the Btree..*/.S
157eb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
157ec 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  id sqlite3BtreeG
157ed 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43  etTempCursor(BtC
157ee 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 43  ursor *pCur, BtC
157ef 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 29  ursor *pTempCur)
157f0 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  {.  int i;.  ass
157f1 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
157f2 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
157f3 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 43 75 72   memcpy(pTempCur
157f4 2c 20 70 43 75 72 2c 20 73 69 7a 65 6f 66 28 42  , pCur, sizeof(B
157f5 74 43 75 72 73 6f 72 29 29 3b 0a 20 20 70 54 65  tCursor));.  pTe
157f6 6d 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 30  mpCur->pNext = 0
157f7 3b 0a 20 20 70 54 65 6d 70 43 75 72 2d 3e 70 50  ;.  pTempCur->pP
157f8 72 65 76 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  rev = 0;.  for(i
157f9 3d 30 3b 20 69 3c 3d 70 54 65 6d 70 43 75 72 2d  =0; i<=pTempCur-
157fa 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20  >iPage; i++){.  
157fb 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65    sqlite3PagerRe
157fc 66 28 70 54 65 6d 70 43 75 72 2d 3e 61 70 50 61  f(pTempCur->apPa
157fd 67 65 5b 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b  ge[i]->pDbPage);
157fe 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
157ff 54 65 6d 70 43 75 72 2d 3e 70 4b 65 79 3d 3d 30  TempCur->pKey==0
15800 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c   );.}../*.** Del
15801 65 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ete a temporary 
15802 63 75 72 73 6f 72 20 73 75 63 68 20 61 73 20 77  cursor such as w
15803 61 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 43  as made by the C
15804 72 65 61 74 65 54 65 6d 70 6f 72 61 72 79 43 75  reateTemporaryCu
15805 72 73 6f 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69  rsor().** functi
15806 6f 6e 20 61 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c  on above..*/.SQL
15807 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
15808 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 65 6c   sqlite3BtreeRel
15809 65 61 73 65 54 65 6d 70 43 75 72 73 6f 72 28 42  easeTempCursor(B
1580a 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
1580b 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72    int i;.  asser
1580c 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
1580d 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 66  tex(pCur) );.  f
1580e 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d  or(i=0; i<=pCur-
1580f 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20  >iPage; i++){.  
15810 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
15811 72 65 66 28 70 43 75 72 2d 3e 61 70 50 61 67 65  ref(pCur->apPage
15812 5b 69 5d 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  [i]->pDbPage);. 
15813 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
15814 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 7d  e(pCur->pKey);.}
15815 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  ..../*.** Make s
15816 75 72 65 20 74 68 65 20 42 74 43 75 72 73 6f 72  ure the BtCursor
15817 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61  * given in the a
15818 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20 76 61  rgument has a va
15819 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e  lid.** BtCursor.
1581a 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20  info structure. 
1581b 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c   If it is not al
1581c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63 61 6c  ready valid, cal
1581d 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65  l.** sqlite3Btre
1581e 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20  eParseCell() to 
1581f 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a  fill it in..**.*
15820 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20  * BtCursor.info 
15821 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 68  is a cache of th
15822 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  e information in
15823 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c   the current cel
15824 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73  l..** Using this
15825 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20 74   cache reduces t
15826 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c  he number of cal
15827 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ls to sqlite3Btr
15828 65 65 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a  eeParseCell()..*
15829 2a 0a 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a  *.** 2007-06-25:
1582a 20 20 54 68 65 72 65 20 69 73 20 61 20 62 75 67    There is a bug
1582b 20 69 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e   in some version
1582c 73 20 6f 66 20 4d 53 56 43 20 74 68 61 74 20 63  s of MSVC that c
1582d 61 75 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70  ause the.** comp
1582e 69 6c 65 72 20 74 6f 20 63 72 61 73 68 20 77 68  iler to crash wh
1582f 65 6e 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29  en getCellInfo()
15830 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
15831 61 73 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42  as a macro..** B
15832 75 74 20 74 68 65 72 65 20 69 73 20 61 20 6d 65  ut there is a me
15833 61 73 75 72 65 61 62 6c 65 20 73 70 65 65 64 20  asureable speed 
15834 61 64 76 61 6e 74 61 67 65 20 74 6f 20 75 73 69  advantage to usi
15835 6e 67 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20  ng the macro on 
15836 67 63 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73  gcc.** (when les
15837 73 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d  s compiler optim
15838 69 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f  izations like -O
15839 73 20 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65  s or -O0 are use
1583a 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d  d and the.** com
1583b 70 69 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69  piler is not doi
1583c 6e 67 20 61 67 72 65 73 73 69 76 65 20 69 6e 6c  ng agressive inl
1583d 69 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75  ining.)  So we u
1583e 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69  se a real functi
1583f 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61  on.** for MSVC a
15840 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65  nd a macro for e
15841 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20  verything else. 
15842 20 54 69 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a   Ticket #2457..*
15843 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
15844 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 61  .  static void a
15845 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74  ssertCellInfo(Bt
15846 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
15847 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f     CellInfo info
15848 3b 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65 20  ;.    int iPage 
15849 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20  = pCur->iPage;. 
1584a 20 20 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c     memset(&info,
1584b 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29   0, sizeof(info)
1584c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
1584d 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75  reeParseCell(pCu
1584e 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d  r->apPage[iPage]
1584f 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50  , pCur->aiIdx[iP
15850 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  age], &info);.  
15851 20 20 61 73 73 65 72 74 28 20 6d 65 6d 63 6d 70    assert( memcmp
15852 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69  (&info, &pCur->i
15853 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f  nfo, sizeof(info
15854 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c  ))==0 );.  }.#el
15855 73 65 0a 20 20 23 64 65 66 69 6e 65 20 61 73 73  se.  #define ass
15856 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23  ertCellInfo(x).#
15857 65 6e 64 69 66 0a 23 69 66 64 65 66 20 5f 4d 53  endif.#ifdef _MS
15858 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 65 20 61  C_VER.  /* Use a
15859 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69   real function i
1585a 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 6b 20 61  n MSVC to work a
1585b 72 6f 75 6e 64 20 62 75 67 73 20 69 6e 20 74 68  round bugs in th
1585c 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a  at compiler. */.
1585d 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 67 65    static void ge
1585e 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73  tCellInfo(BtCurs
1585f 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 69  or *pCur){.    i
15860 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53  f( pCur->info.nS
15861 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ize==0 ){.      
15862 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72  int iPage = pCur
15863 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 20 20 73  ->iPage;.      s
15864 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
15865 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67  Cell(pCur->apPag
15866 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61  e[iPage],pCur->a
15867 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75  iIdx[iPage],&pCu
15868 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20  r->info);.      
15869 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1586a 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 1;.    }else{.
1586b 20 20 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c        assertCell
1586c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20  Info(pCur);.    
1586d 7d 0a 20 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69  }.  }.#else /* i
1586e 66 20 6e 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a  f not _MSC_VER *
1586f 2f 0a 20 20 2f 2a 20 55 73 65 20 61 20 6d 61 63  /.  /* Use a mac
15870 72 6f 20 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20  ro in all other 
15871 63 6f 6d 70 69 6c 65 72 73 20 73 6f 20 74 68 61  compilers so tha
15872 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  t the function i
15873 73 20 69 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65  s inlined */.#de
15874 66 69 6e 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f  fine getCellInfo
15875 28 70 43 75 72 29 20 20 20 20 20 20 20 20 20 20  (pCur)          
15876 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15878 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
15879 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  if( pCur->info.n
1587a 53 69 7a 65 3d 3d 30 20 29 7b 20 20 20 20 20 20  Size==0 ){      
1587b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1587c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1587d 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
1587e 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70     int iPage = p
1587f 43 75 72 2d 3e 69 50 61 67 65 3b 20 20 20 20 20  Cur->iPage;     
15880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15882 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
15883 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15884 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
15885 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43  apPage[iPage],pC
15886 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d  ur->aiIdx[iPage]
15887 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c  ,&pCur->info); \
15888 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64  .    pCur->valid
15889 4e 4b 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20  NKey = 1;       
1588a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1588b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1588c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1588d 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20  \.  }else{      
1588e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1588f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15891 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15892 20 5c 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c   \.    assertCel
15893 6c 49 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20  lInfo(pCur);    
15894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15896 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15897 20 20 5c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f    \.  }.#endif /
15898 2a 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 2f  * _MSC_VER */../
15899 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20  *.** Set *pSize 
1589a 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  to the size of t
1589b 68 65 20 62 75 66 66 65 72 20 6e 65 65 64 65 64  he buffer needed
1589c 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76 61 6c   to hold the val
1589d 75 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79  ue of.** the key
1589e 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
1589f 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68 65 20   entry.  If the 
158a0 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f  cursor is not po
158a1 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76  inting.** to a v
158a2 61 6c 69 64 20 65 6e 74 72 79 2c 20 2a 70 53 69  alid entry, *pSi
158a3 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 2e 20  ze is set to 0. 
158a4 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62  .**.** For a tab
158a5 6c 65 20 77 69 74 68 20 74 68 65 20 49 4e 54 4b  le with the INTK
158a6 45 59 20 66 6c 61 67 20 73 65 74 2c 20 74 68 69  EY flag set, thi
158a7 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
158a8 73 20 74 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73  s the key.** its
158a9 65 6c 66 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d  elf, not the num
158aa 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
158ab 74 68 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49  the key..*/.SQLI
158ac 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
158ad 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
158ae 7a 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ze(BtCursor *pCu
158af 72 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a  r, i64 *pSize){.
158b0 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
158b1 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
158b2 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
158b3 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72   rc = restoreCur
158b4 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72  sorPosition(pCur
158b5 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
158b6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73  ITE_OK ){.    as
158b7 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
158b8 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
158b9 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61  ID || pCur->eSta
158ba 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
158bb 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72   );.    if( pCur
158bc 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
158bd 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20  _INVALID ){.    
158be 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20    *pSize = 0;.  
158bf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 67    }else{.      g
158c0 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  etCellInfo(pCur)
158c1 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d  ;.      *pSize =
158c2 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79   pCur->info.nKey
158c3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
158c4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
158c5 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20  * Set *pSize to 
158c6 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
158c7 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74  tes of data in t
158c8 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20  he entry the.** 
158c9 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79  cursor currently
158ca 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77   points to.  Alw
158cb 61 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ays return SQLIT
158cc 45 5f 4f 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65  E_OK..** Failure
158cd 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
158ce 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  .  If the cursor
158cf 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
158d0 79 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f  y.** pointing to
158d1 20 61 6e 20 65 6e 74 72 79 20 28 77 68 69 63 68   an entry (which
158d2 20 63 61 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72   can happen, for
158d3 20 65 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20   example, if.** 
158d4 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
158d5 65 6d 70 74 79 29 20 74 68 65 6e 20 2a 70 53 69  empty) then *pSi
158d6 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a  ze is set to 0..
158d7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
158d8 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
158d9 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72  eeDataSize(BtCur
158da 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a  sor *pCur, u32 *
158db 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63  pSize){.  int rc
158dc 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
158dd 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
158de 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65  ur) );.  rc = re
158df 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
158e0 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28  ion(pCur);.  if(
158e1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
158e2 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  {.    assert( pC
158e3 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
158e4 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43  OR_INVALID || pC
158e5 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
158e6 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20  OR_VALID );.    
158e7 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
158e8 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
158e9 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74   ){.      /* Not
158ea 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 76   pointing at a v
158eb 61 6c 69 64 20 65 6e 74 72 79 20 2d 20 73 65 74  alid entry - set
158ec 20 2a 70 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f   *pSize to 0. */
158ed 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20  .      *pSize = 
158ee 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
158ef 20 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28      getCellInfo(
158f0 70 43 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 53  pCur);.      *pS
158f1 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  ize = pCur->info
158f2 2e 6e 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20  .nData;.    }.  
158f3 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
158f4 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68  ../*.** Given th
158f5 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  e page number of
158f6 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
158f7 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
158f8 65 20 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  e (parameter.** 
158f9 6f 76 66 6c 29 2c 20 74 68 69 73 20 66 75 6e 63  ovfl), this func
158fa 74 69 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 70  tion finds the p
158fb 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
158fc 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74  e next page in t
158fd 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69  he .** linked li
158fe 73 74 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70  st of overflow p
158ff 61 67 65 73 2e 20 49 66 20 70 6f 73 73 69 62 6c  ages. If possibl
15900 65 2c 20 69 74 20 75 73 65 73 20 74 68 65 20 61  e, it uses the a
15901 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f  uto-vacuum.** po
15902 69 6e 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69  inter-map data i
15903 6e 73 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e  nstead of readin
15904 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  g the content of
15905 20 70 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f   page ovfl to do
15906 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61   so. .**.** If a
15907 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61  n error occurs a
15908 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
15909 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
1590a 20 4f 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a   Otherwise:.**.*
1590b 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  * The page numbe
1590c 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76  r of the next ov
1590d 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74  erflow page in t
1590e 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69  he linked list i
1590f 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f  s .** written to
15910 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20   *pPgnoNext. If 
15911 70 61 67 65 20 6f 76 66 6c 20 69 73 20 74 68 65  page ovfl is the
15912 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 69 74   last page in it
15913 73 20 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 73  s linked .** lis
15914 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73  t, *pPgnoNext is
15915 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a   set to zero. .*
15916 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69  *.** If ppPage i
15917 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 20  s not NULL, and 
15918 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
15919 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63  he MemPage objec
1591a 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a  t corresponding.
1591b 2a 2a 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65  ** to page numbe
1591c 72 20 70 4f 76 66 6c 20 77 61 73 20 6f 62 74 61  r pOvfl was obta
1591d 69 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70 50 61  ined, then *ppPa
1591e 67 65 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  ge is set to poi
1591f 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 72 65  nt to that.** re
15920 66 65 72 65 6e 63 65 2e 20 49 74 20 69 73 20 74  ference. It is t
15921 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  he responsibilit
15922 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20  y of the caller 
15923 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50  to call releaseP
15924 61 67 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 50  age().** on *ppP
15925 61 67 65 20 74 6f 20 66 72 65 65 20 74 68 65 20  age to free the 
15926 72 65 66 65 72 65 6e 63 65 2e 20 49 6e 20 6e 6f  reference. In no
15927 20 72 65 66 65 72 65 6e 63 65 20 77 61 73 20 6f   reference was o
15928 62 74 61 69 6e 65 64 20 28 62 65 63 61 75 73 65  btained (because
15929 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d  .** the pointer-
1592a 6d 61 70 20 77 61 73 20 75 73 65 64 20 74 6f 20  map was used to 
1592b 6f 62 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65  obtain the value
1592c 20 66 6f 72 20 2a 70 50 67 6e 6f 4e 65 78 74 29   for *pPgnoNext)
1592d 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 67  , then.** *ppPag
1592e 65 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  e is set to zero
1592f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15930 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28  getOverflowPage(
15931 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
15932 2c 20 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20  , .  Pgno ovfl, 
15933 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15934 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61    /* Overflow pa
15935 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ge */.  MemPage 
15936 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 20 20 20  **ppPage,       
15937 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d       /* OUT: Mem
15938 50 61 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79  Page handle (may
15939 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50   be NULL) */.  P
1593a 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20  gno *pPgnoNext  
1593b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1593c 55 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f  UT: Next overflo
1593d 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  w page number */
1593e 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20  .){.  Pgno next 
1593f 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  = 0;.  MemPage *
15940 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74  pPage = 0;.  int
15941 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
15942 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
15943 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
15944 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15945 61 73 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74  assert(pPgnoNext
15946 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
15947 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
15948 55 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66  UM.  /* Try to f
15949 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 61 67  ind the next pag
1594a 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f  e in the overflo
1594b 77 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65  w list using the
1594c 0a 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d  .  ** autovacuum
1594d 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
1594e 65 73 2e 20 47 75 65 73 73 20 74 68 61 74 20 74  es. Guess that t
1594f 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
15950 0a 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c  .  ** the overfl
15951 6f 77 20 6c 69 73 74 20 69 73 20 70 61 67 65 20  ow list is page 
15952 6e 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e  number (ovfl+1).
15953 20 49 66 20 74 68 61 74 20 67 75 65 73 73 20 74   If that guess t
15954 75 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74  urns .  ** out t
15955 6f 20 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c  o be wrong, fall
15956 20 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67   back to loading
15957 20 74 68 65 20 64 61 74 61 20 6f 66 20 70 61 67   the data of pag
15958 65 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f  e .  ** number o
15959 76 66 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  vfl to determine
1595a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e   the next page n
1595b 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  umber..  */.  if
1595c 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
1595d 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67  m ){.    Pgno pg
1595e 6e 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75  no;.    Pgno iGu
1595f 65 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20  ess = ovfl+1;.  
15960 20 20 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20    u8 eType;..   
15961 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49   while( PTRMAP_I
15962 53 50 41 47 45 28 70 42 74 2c 20 69 47 75 65 73  SPAGE(pBt, iGues
15963 73 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45  s) || iGuess==PE
15964 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
15965 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47  pBt) ){.      iG
15966 75 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  uess++;.    }.. 
15967 20 20 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70     if( iGuess<=p
15968 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
15969 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  t) ){.      rc =
1596a 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20   ptrmapGet(pBt, 
1596b 69 47 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20  iGuess, &eType, 
1596c 26 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66  &pgno);.      if
1596d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1596e 26 26 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  && eType==PTRMAP
1596f 5f 4f 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67  _OVERFLOW2 && pg
15970 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20  no==ovfl ){.    
15971 20 20 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73      next = iGues
15972 73 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  s;.        rc = 
15973 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
15974 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
15975 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 72 63 3d  endif..  if( rc=
15976 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15977 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
15978 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
15979 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29  ovfl, &pPage, 0)
1597a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 72 63 3d  ;.    assert(rc=
1597b 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50  =SQLITE_OK || pP
1597c 61 67 65 3d 3d 30 29 3b 0a 20 20 20 20 69 66 28  age==0);.    if(
1597d 20 6e 65 78 74 3d 3d 30 20 26 26 20 72 63 3d 3d   next==0 && rc==
1597e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1597f 20 20 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79     next = get4by
15980 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29  te(pPage->aData)
15981 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a  ;.    }.  }..  *
15982 70 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74  pPgnoNext = next
15983 3b 0a 20 20 69 66 28 20 70 70 50 61 67 65 20 29  ;.  if( ppPage )
15984 7b 0a 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20  {.    *ppPage = 
15985 70 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pPage;.  }else{.
15986 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
15987 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65  pPage);.  }.  re
15988 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45  turn (rc==SQLITE
15989 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f  _DONE ? SQLITE_O
1598a 4b 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  K : rc);.}../*.*
1598b 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d  * Copy data from
1598c 20 61 20 62 75 66 66 65 72 20 74 6f 20 61 20 70   a buffer to a p
1598d 61 67 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70  age, or from a p
1598e 61 67 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e  age to a buffer.
1598f 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20  .**.** pPayload 
15990 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
15991 64 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64  data stored on d
15992 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62  atabase page pDb
15993 50 61 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75  Page..** If argu
15994 6d 65 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73  ment eOp is fals
15995 65 2c 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79  e, then nByte by
15996 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20  tes of data are 
15997 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70  copied.** from p
15998 50 61 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62  Payload to the b
15999 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74  uffer pointed at
1599a 20 62 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70   by pBuf. If eOp
1599b 20 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65   is true,.** the
1599c 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  n sqlite3PagerWr
1599d 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ite() is called 
1599e 6f 6e 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e  on pDbPage and n
1599f 42 79 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66  Byte bytes.** of
159a0 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64   data are copied
159a1 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72   from the buffer
159a2 20 70 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61   pBuf to pPayloa
159a3 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  d..**.** SQLITE_
159a4 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  OK is returned o
159a5 6e 20 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72  n success, other
159a6 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f  wise an error co
159a7 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  de..*/.static in
159a8 74 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20  t copyPayload(. 
159a9 20 76 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c   void *pPayload,
159aa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
159ab 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 64 61  inter to page da
159ac 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42  ta */.  void *pB
159ad 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  uf,             
159ae 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
159af 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20  buffer */.  int 
159b0 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20  nByte,          
159b1 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
159b2 6f 66 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79  of bytes to copy
159b3 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20   */.  int eOp,  
159b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
159b5 2f 2a 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f  /* 0 -> copy fro
159b6 6d 20 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70  m page, 1 -> cop
159b7 79 20 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44  y to page */.  D
159b8 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 20  bPage *pDbPage  
159b9 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
159ba 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79   containing pPay
159bb 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  load */.){.  if(
159bc 20 65 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43   eOp ){.    /* C
159bd 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75  opy data from bu
159be 66 66 65 72 20 74 6f 20 70 61 67 65 20 28 61 20  ffer to page (a 
159bf 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29  write operation)
159c0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d   */.    int rc =
159c1 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
159c2 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20  te(pDbPage);.   
159c3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
159c4 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
159c5 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
159c6 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64   memcpy(pPayload
159c7 2c 20 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a  , pBuf, nByte);.
159c8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
159c9 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70  Copy data from p
159ca 61 67 65 20 74 6f 20 62 75 66 66 65 72 20 28 61  age to buffer (a
159cb 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29   read operation)
159cc 20 2a 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70   */.    memcpy(p
159cd 42 75 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e  Buf, pPayload, n
159ce 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Byte);.  }.  ret
159cf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
159d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
159d1 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
159d2 20 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69   read or overwri
159d3 74 65 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72  te payload infor
159d4 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68  mation.** for th
159d5 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65  e entry that the
159d6 20 70 43 75 72 20 63 75 72 73 6f 72 20 69 73 20   pCur cursor is 
159d7 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20  pointing to. If 
159d8 74 68 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d  the eOp.** param
159d9 65 74 65 72 20 69 73 20 30 2c 20 74 68 69 73 20  eter is 0, this 
159da 69 73 20 61 20 72 65 61 64 20 6f 70 65 72 61 74  is a read operat
159db 69 6f 6e 20 28 64 61 74 61 20 63 6f 70 69 65 64  ion (data copied
159dc 20 69 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20   into.** buffer 
159dd 70 42 75 66 29 2e 20 49 66 20 69 74 20 69 73 20  pBuf). If it is 
159de 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74  non-zero, a writ
159df 65 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 66  e (data copied f
159e0 72 6f 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70 42  rom.** buffer pB
159e1 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74  uf)..**.** A tot
159e2 61 6c 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65  al of "amt" byte
159e3 73 20 61 72 65 20 72 65 61 64 20 6f 72 20 77 72  s are read or wr
159e4 69 74 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20  itten beginning 
159e5 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20  at "offset"..** 
159e6 44 61 74 61 20 69 73 20 72 65 61 64 20 74 6f 20  Data is read to 
159e7 6f 72 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66  or from the buff
159e8 65 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54  er pBuf..**.** T
159e9 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  his routine does
159ea 20 6e 6f 74 20 6d 61 6b 65 20 61 20 64 69 73 74   not make a dist
159eb 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20  inction between 
159ec 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a  key and data..**
159ed 20 49 74 20 6a 75 73 74 20 72 65 61 64 73 20 6f   It just reads o
159ee 72 20 77 72 69 74 65 73 20 62 79 74 65 73 20 66  r writes bytes f
159ef 72 6f 6d 20 74 68 65 20 70 61 79 6c 6f 61 64 20  rom the payload 
159f0 61 72 65 61 2e 20 20 44 61 74 61 20 6d 69 67 68  area.  Data migh
159f1 74 20 0a 2a 2a 20 61 70 70 65 61 72 20 6f 6e 20  t .** appear on 
159f2 74 68 65 20 6d 61 69 6e 20 70 61 67 65 20 6f 72  the main page or
159f3 20 62 65 20 73 63 61 74 74 65 72 65 64 20 6f 75   be scattered ou
159f4 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76  t on multiple ov
159f5 65 72 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 73  erflow .** pages
159f6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42  ..**.** If the B
159f7 74 43 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c  tCursor.isIncrbl
159f8 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73  obHandle flag is
159f9 20 73 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75   set, and the cu
159fa 72 72 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20  rrent.** cursor 
159fb 65 6e 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f  entry uses one o
159fc 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20  r more overflow 
159fd 70 61 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63  pages, this func
159fe 74 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  tion.** allocate
159ff 73 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20  s space for and 
15a00 6c 61 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65 73  lazily popluates
15a01 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
15a02 67 65 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68  ge-list .** cach
15a03 65 20 61 72 72 61 79 20 28 42 74 43 75 72 73 6f  e array (BtCurso
15a04 72 2e 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75  r.aOverflow). Su
15a05 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75  bsequent calls u
15a06 73 65 20 74 68 69 73 0a 2a 2a 20 63 61 63 68 65  se this.** cache
15a07 20 74 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67   to make seeking
15a08 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64   to the supplied
15a09 20 6f 66 66 73 65 74 20 6d 6f 72 65 20 65 66 66   offset more eff
15a0a 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  icient..**.** On
15a0b 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  ce an overflow p
15a0c 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68  age-list cache h
15a0d 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  as been allocate
15a0e 64 2c 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20  d, it may be.** 
15a0f 69 6e 76 61 6c 69 64 61 74 65 64 20 69 66 20 73  invalidated if s
15a10 6f 6d 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72  ome other cursor
15a11 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 73   writes to the s
15a12 61 6d 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66  ame table, or if
15a13 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69  .** the cursor i
15a14 73 20 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66  s moved to a dif
15a15 66 65 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69  ferent row. Addi
15a16 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74  tionally, in aut
15a17 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65  o-vacuum.** mode
15a18 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
15a19 65 76 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c  events may inval
15a1a 69 64 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f  idate an overflo
15a1b 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  w page-list cach
15a1c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20  e..**.**   * An 
15a1d 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  incremental vacu
15a1e 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d  um,.**   * A com
15a1f 6d 69 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75  mit in auto_vacu
15a20 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a  um="full" mode,.
15a21 2a 2a 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20  **   * Creating 
15a22 61 20 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71  a table (may req
15a23 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f  uire moving an o
15a24 76 65 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a  verflow page)..*
15a25 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 63 63  /.static int acc
15a26 65 73 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74  essPayload(.  Bt
15a27 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20  Cursor *pCur,   
15a28 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69     /* Cursor poi
15a29 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74  nting to entry t
15a2a 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20  o read from */. 
15a2b 20 75 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20   u32 offset,    
15a2c 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72        /* Begin r
15a2d 65 61 64 69 6e 67 20 74 68 69 73 20 66 61 72 20  eading this far 
15a2e 69 6e 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a  into payload */.
15a2f 20 20 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20    u32 amt,      
15a30 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74         /* Read t
15a31 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a  his many bytes *
15a32 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
15a33 72 20 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74  r *pBuf, /* Writ
15a34 65 20 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f  e the bytes into
15a35 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20   this buffer */ 
15a36 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 2c 20  .  int skipKey, 
15a37 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65          /* offse
15a38 74 20 62 65 67 69 6e 73 20 61 74 20 64 61 74 61  t begins at data
15a39 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65   if this is true
15a3a 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 20 20 20   */.  int eOp   
15a3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 65             /* ze
15a3c 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d  ro to read. non-
15a3d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a  zero to write. *
15a3e 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  /.){.  unsigned 
15a3f 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a  char *aPayload;.
15a40 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
15a41 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79  E_OK;.  u32 nKey
15a42 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30  ;.  int iIdx = 0
15a43 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
15a44 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
15a45 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20  e[pCur->iPage]; 
15a46 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20 6f 66  /* Btree page of
15a47 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20 2a   current entry *
15a48 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
15a49 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20 20  t = pCur->pBt;  
15a4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15a4b 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20 63 75  /* Btree this cu
15a4c 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20  rsor belongs to 
15a4d 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
15a4e 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  age );.  assert(
15a4f 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
15a50 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
15a51 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
15a52 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
15a53 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  ]<pPage->nCell )
15a54 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ;.  assert( curs
15a55 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
15a56 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c  r) );..  getCell
15a57 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50  Info(pCur);.  aP
15a58 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69  ayload = pCur->i
15a59 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72  nfo.pCell + pCur
15a5a 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a  ->info.nHeader;.
15a5b 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d    nKey = (pPage-
15a5c 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 28 69  >intKey ? 0 : (i
15a5d 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b  nt)pCur->info.nK
15a5e 65 79 29 3b 0a 0a 20 20 69 66 28 20 73 6b 69 70  ey);..  if( skip
15a5f 4b 65 79 20 29 7b 0a 20 20 20 20 6f 66 66 73 65  Key ){.    offse
15a60 74 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 7d 0a 20  t += nKey;.  }. 
15a61 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 20   if( offset+amt 
15a62 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66  > nKey+pCur->inf
15a63 6f 2e 6e 44 61 74 61 20 0a 20 20 20 7c 7c 20 26  o.nData .   || &
15a64 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69  aPayload[pCur->i
15a65 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70  nfo.nLocal] > &p
15a66 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d  Page->aData[pBt-
15a67 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29  >usableSize].  )
15a68 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20  {.    /* Trying 
15a69 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  to read or write
15a6a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
15a6b 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e 20   the data is an 
15a6c 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74  error */.    ret
15a6d 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15a6e 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20  PT_BKPT;.  }..  
15a6f 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 61  /* Check if data
15a70 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 72   must be read/wr
15a71 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68  itten to/from th
15a72 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74 73  e btree page its
15a73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66  elf. */.  if( of
15a74 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e  fset<pCur->info.
15a75 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e  nLocal ){.    in
15a76 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69  t a = amt;.    i
15a77 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72  f( a+offset>pCur
15a78 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b  ->info.nLocal ){
15a79 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d  .      a = pCur-
15a7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f  >info.nLocal - o
15a7b 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20  ffset;.    }.   
15a7c 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61   rc = copyPayloa
15a7d 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73  d(&aPayload[offs
15a7e 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f  et], pBuf, a, eO
15a7f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67  p, pPage->pDbPag
15a80 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d  e);.    offset =
15a81 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20   0;.    pBuf += 
15a82 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b  a;.    amt -= a;
15a83 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66  .  }else{.    of
15a84 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e  fset -= pCur->in
15a85 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a  fo.nLocal;.  }..
15a86 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
15a87 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a  _OK && amt>0 ){.
15a88 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76      const u32 ov
15a89 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  flSize = pBt->us
15a8a 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f  ableSize - 4;  /
15a8b 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20  * Bytes content 
15a8c 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f  per ovfl page */
15a8d 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61  .    Pgno nextPa
15a8e 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67  ge;..    nextPag
15a8f 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 50  e = get4byte(&aP
15a90 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66  ayload[pCur->inf
15a91 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66  o.nLocal]);..#if
15a92 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15a93 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a  _INCRBLOB.    /*
15a94 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c   If the isIncrbl
15a95 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73  obHandle flag is
15a96 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 43   set and the BtC
15a97 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b  ursor.aOverflow[
15a98 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74  ].    ** has not
15a99 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c   been allocated,
15a9a 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77   allocate it now
15a9b 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20 73  . The array is s
15a9c 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f  ized at.    ** o
15a9d 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ne entry for eac
15a9e 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  h overflow page 
15a9f 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  in the overflow 
15aa0 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a  chain. The.    *
15aa1 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  * page number of
15aa2 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66   the first overf
15aa3 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72  low page is stor
15aa4 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b  ed in aOverflow[
15aa5 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20  0],.    ** etc. 
15aa6 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20  A value of 0 in 
15aa7 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20  the aOverflow[] 
15aa8 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74  array means "not
15aa9 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20   yet known".    
15aaa 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 73  ** (the cache is
15aab 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65   lazily populate
15aac 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  d)..    */.    i
15aad 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62  f( pCur->isIncrb
15aae 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43  lobHandle && !pC
15aaf 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b  ur->aOverflow ){
15ab0 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c  .      int nOvfl
15ab1 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e   = (pCur->info.n
15ab2 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e  Payload-pCur->in
15ab3 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69  fo.nLocal+ovflSi
15ab4 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a  ze-1)/ovflSize;.
15ab5 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65        pCur->aOve
15ab6 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29  rflow = (Pgno *)
15ab7 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
15ab8 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e  o(sizeof(Pgno)*n
15ab9 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 69 66 28  Ovfl);.      if(
15aba 20 6e 4f 76 66 6c 20 26 26 20 21 70 43 75 72 2d   nOvfl && !pCur-
15abb 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  >aOverflow ){.  
15abc 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
15abd 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
15abe 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
15abf 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  f the overflow p
15ac0 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68  age-list cache h
15ac1 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  as been allocate
15ac2 64 20 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a  d and the.    **
15ac3 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66   entry for the f
15ac4 69 72 73 74 20 72 65 71 75 69 72 65 64 20 6f 76  irst required ov
15ac5 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 76  erflow page is v
15ac6 61 6c 69 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a  alid, skip.    *
15ac7 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 69 74  * directly to it
15ac8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
15ac9 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
15aca 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66   && pCur->aOverf
15acb 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53  low[offset/ovflS
15acc 69 7a 65 5d 20 29 7b 0a 20 20 20 20 20 20 69 49  ize] ){.      iI
15acd 64 78 20 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66  dx = (offset/ovf
15ace 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65  lSize);.      ne
15acf 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  xtPage = pCur->a
15ad0 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a  Overflow[iIdx];.
15ad1 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 28        offset = (
15ad2 6f 66 66 73 65 74 25 6f 76 66 6c 53 69 7a 65 29  offset%ovflSize)
15ad3 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
15ad4 20 20 20 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53      for( ; rc==S
15ad5 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e  QLITE_OK && amt>
15ad6 30 20 26 26 20 6e 65 78 74 50 61 67 65 3b 20 69  0 && nextPage; i
15ad7 49 64 78 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66  Idx++){..#ifndef
15ad8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
15ad9 52 42 4c 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49  RBLOB.      /* I
15ada 66 20 72 65 71 75 69 72 65 64 2c 20 70 6f 70 75  f required, popu
15adb 6c 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f  late the overflo
15adc 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  w page-list cach
15add 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  e. */.      if( 
15ade 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
15adf 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
15ae0 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  t(!pCur->aOverfl
15ae1 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 70 43 75 72  ow[iIdx] || pCur
15ae2 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
15ae3 5d 3d 3d 6e 65 78 74 50 61 67 65 29 3b 0a 20 20  ]==nextPage);.  
15ae4 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65        pCur->aOve
15ae5 72 66 6c 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65  rflow[iIdx] = ne
15ae6 78 74 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a  xtPage;.      }.
15ae7 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 69 66  #endif..      if
15ae8 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69  ( offset>=ovflSi
15ae9 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ze ){.        /*
15aea 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e   The only reason
15aeb 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61   to read this pa
15aec 67 65 20 69 73 20 74 6f 20 6f 62 74 61 69 6e 20  ge is to obtain 
15aed 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20  the page.       
15aee 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74   ** number for t
15aef 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
15af0 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61  the overflow cha
15af1 69 6e 2e 20 54 68 65 20 70 61 67 65 0a 20 20 20  in. The page.   
15af2 20 20 20 20 20 2a 2a 20 64 61 74 61 20 69 73 20       ** data is 
15af3 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 20 53 6f  not required. So
15af4 20 66 69 72 73 74 20 74 72 79 20 74 6f 20 6c 6f   first try to lo
15af5 6f 6b 75 70 20 74 68 65 20 6f 76 65 72 66 6c 6f  okup the overflo
15af6 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67  w.        ** pag
15af7 65 2d 6c 69 73 74 20 63 61 63 68 65 2c 20 69 66  e-list cache, if
15af8 20 61 6e 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20   any, then fall 
15af9 62 61 63 6b 20 74 6f 20 74 68 65 20 67 65 74 4f  back to the getO
15afa 76 65 72 66 6c 6f 77 50 61 67 65 28 29 0a 20 20  verflowPage().  
15afb 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f        ** functio
15afc 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23 69  n..        */.#i
15afd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15afe 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20  T_INCRBLOB.     
15aff 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76     if( pCur->aOv
15b00 65 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e  erflow && pCur->
15b01 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31  aOverflow[iIdx+1
15b02 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ] ){.          n
15b03 65 78 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e  extPage = pCur->
15b04 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31  aOverflow[iIdx+1
15b05 5d 3b 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73  ];.        } els
15b06 65 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  e .#endif.      
15b07 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72      rc = getOver
15b08 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6e 65  flowPage(pBt, ne
15b09 78 74 50 61 67 65 2c 20 30 2c 20 26 6e 65 78 74  xtPage, 0, &next
15b0a 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 6f  Page);.        o
15b0b 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a  ffset -= ovflSiz
15b0c 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  e;.      }else{.
15b0d 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20          /* Need 
15b0e 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 67  to read this pag
15b0f 65 20 70 72 6f 70 65 72 6c 79 2e 20 49 74 20 63  e properly. It c
15b10 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20  ontains some of 
15b11 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72  the.        ** r
15b12 61 6e 67 65 20 6f 66 20 64 61 74 61 20 74 68 61  ange of data tha
15b13 74 20 69 73 20 62 65 69 6e 67 20 72 65 61 64 20  t is being read 
15b14 28 65 4f 70 3d 3d 30 29 20 6f 72 20 77 72 69 74  (eOp==0) or writ
15b15 74 65 6e 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20  ten (eOp!=0)..  
15b16 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
15b17 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
15b18 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 20  ;.        int a 
15b19 3d 20 61 6d 74 3b 0a 20 20 20 20 20 20 20 20 72  = amt;.        r
15b1a 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
15b1b 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  Get(pBt->pPager,
15b1c 20 6e 65 78 74 50 61 67 65 2c 20 26 70 44 62 50   nextPage, &pDbP
15b1d 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  age);.        if
15b1e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15b1f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 50 61  ){.          aPa
15b20 79 6c 6f 61 64 20 3d 20 73 71 6c 69 74 65 33 50  yload = sqlite3P
15b21 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
15b22 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
15b23 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74 34 62  nextPage = get4b
15b24 79 74 65 28 61 50 61 79 6c 6f 61 64 29 3b 0a 20  yte(aPayload);. 
15b25 20 20 20 20 20 20 20 20 20 69 66 28 20 61 20 2b           if( a +
15b26 20 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c 53 69   offset > ovflSi
15b27 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ze ){.          
15b28 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d    a = ovflSize -
15b29 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20   offset;.       
15b2a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72     }.          r
15b2b 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64 28  c = copyPayload(
15b2c 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74  &aPayload[offset
15b2d 2b 34 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f  +4], pBuf, a, eO
15b2e 70 2c 20 70 44 62 50 61 67 65 29 3b 0a 20 20 20  p, pDbPage);.   
15b2f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
15b30 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65  gerUnref(pDbPage
15b31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 66 66  );.          off
15b32 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  set = 0;.       
15b33 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20     amt -= a;.   
15b34 20 20 20 20 20 20 20 70 42 75 66 20 2b 3d 20 61         pBuf += a
15b35 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
15b36 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
15b37 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
15b38 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20  OK && amt>0 ){. 
15b39 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15b3a 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
15b3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
15b3c 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61  }../*.** Read pa
15b3d 72 74 20 6f 66 20 74 68 65 20 6b 65 79 20 61 73  rt of the key as
15b3e 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 75  sociated with cu
15b3f 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78 61 63  rsor pCur.  Exac
15b40 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74  tly.** "amt" byt
15b41 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73  es will be trans
15b42 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75 66 5b  fered into pBuf[
15b43 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66 65 72  ].  The transfer
15b44 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20 22 6f  .** begins at "o
15b45 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65  ffset"..**.** Re
15b46 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
15b47 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20  n success or an 
15b48 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e  error code if an
15b49 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77  ything goes.** w
15b4a 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20  rong.  An error 
15b4b 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 22  is returned if "
15b4c 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c  offset+amt" is l
15b4d 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68  arger than.** th
15b4e 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c  e available payl
15b4f 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  oad..*/.SQLITE_P
15b50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15b51 65 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72  e3BtreeKey(BtCur
15b52 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f  sor *pCur, u32 o
15b53 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
15b54 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 69  void *pBuf){.  i
15b55 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
15b56 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
15b57 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
15b58 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
15b59 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
15b5a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
15b5b 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
15b5c 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
15b5d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
15b5e 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
15b5f 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70  r->iPage>=0 && p
15b60 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
15b61 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20  ->iPage] );.    
15b62 69 66 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65  if( pCur->apPage
15b63 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  [0]->intKey ){. 
15b64 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
15b65 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
15b66 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
15b67 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  t( pCur->aiIdx[p
15b68 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72  Cur->iPage]<pCur
15b69 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
15b6a 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  Page]->nCell );.
15b6b 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73 50      rc = accessP
15b6c 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66  ayload(pCur, off
15b6d 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67  set, amt, (unsig
15b6e 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20  ned char*)pBuf, 
15b6f 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  0, 0);.  }.  ret
15b70 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
15b71 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 68   Read part of th
15b72 65 20 64 61 74 61 20 61 73 73 6f 63 69 61 74 65  e data associate
15b73 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43  d with cursor pC
15b74 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20  ur.  Exactly.** 
15b75 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c  "amt" bytes will
15b76 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69   be transfered i
15b77 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65  nto pBuf[].  The
15b78 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67   transfer.** beg
15b79 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e  ins at "offset".
15b7a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
15b7b 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
15b7c 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63  ss or an error c
15b7d 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ode if anything 
15b7e 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20  goes.** wrong.  
15b7f 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  An error is retu
15b80 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b  rned if "offset+
15b81 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74  amt" is larger t
15b82 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c  han.** the avail
15b83 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f  able payload..*/
15b84 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15b85 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
15b86 44 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70  Data(BtCursor *p
15b87 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c  Cur, u32 offset,
15b88 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a   u32 amt, void *
15b89 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pBuf){.  int rc;
15b8a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
15b8b 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
15b8c 20 69 66 20 28 20 70 43 75 72 2d 3e 65 53 74 61   if ( pCur->eSta
15b8d 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
15b8e 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ID ){.    return
15b8f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20   SQLITE_ABORT;. 
15b90 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73   }.#endif..  ass
15b91 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
15b92 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
15b93 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72   rc = restoreCur
15b94 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72  sorPosition(pCur
15b95 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
15b96 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73  ITE_OK ){.    as
15b97 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
15b98 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
15b99 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15b9a 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26  pCur->iPage>=0 &
15b9b 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  & pCur->apPage[p
15b9c 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20  Cur->iPage] );. 
15b9d 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
15b9e 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
15b9f 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65  ge]<pCur->apPage
15ba0 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
15ba1 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d  Cell );.    rc =
15ba2 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70   accessPayload(p
15ba3 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74  Cur, offset, amt
15ba4 2c 20 70 42 75 66 2c 20 31 2c 20 30 29 3b 0a 20  , pBuf, 1, 0);. 
15ba5 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
15ba6 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
15ba7 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 79  a pointer to pay
15ba8 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  load information
15ba9 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20   from the entry 
15baa 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 70 43 75  that the .** pCu
15bab 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e  r cursor is poin
15bac 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 70 6f  ting to.  The po
15bad 69 6e 74 65 72 20 69 73 20 74 6f 20 74 68 65 20  inter is to the 
15bae 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20  beginning of.** 
15baf 74 68 65 20 6b 65 79 20 69 66 20 73 6b 69 70 4b  the key if skipK
15bb0 65 79 3d 3d 30 20 61 6e 64 20 69 74 20 70 6f 69  ey==0 and it poi
15bb1 6e 74 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e  nts to the begin
15bb2 6e 69 6e 67 20 6f 66 20 64 61 74 61 20 69 66 0a  ning of data if.
15bb3 2a 2a 20 73 6b 69 70 4b 65 79 3d 3d 31 2e 20 20  ** skipKey==1.  
15bb4 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  The number of by
15bb5 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65  tes of available
15bb6 20 6b 65 79 2f 64 61 74 61 20 69 73 20 77 72 69   key/data is wri
15bb7 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 41  tten.** into *pA
15bb8 6d 74 2e 20 20 49 66 20 2a 70 41 6d 74 3d 3d 30  mt.  If *pAmt==0
15bb9 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
15bba 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e   returned will n
15bbb 6f 74 20 62 65 0a 2a 2a 20 61 20 76 61 6c 69 64  ot be.** a valid
15bbc 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
15bbd 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
15bbe 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  an optimization.
15bbf 20 20 49 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 66    It is common f
15bc0 6f 72 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65  or the entire ke
15bc1 79 0a 2a 2a 20 61 6e 64 20 64 61 74 61 20 74 6f  y.** and data to
15bc2 20 66 69 74 20 6f 6e 20 74 68 65 20 6c 6f 63 61   fit on the loca
15bc3 6c 20 70 61 67 65 20 61 6e 64 20 66 6f 72 20 74  l page and for t
15bc4 68 65 72 65 20 74 6f 20 62 65 20 6e 6f 20 6f 76  here to be no ov
15bc5 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 73 2e  erflow.** pages.
15bc6 20 20 57 68 65 6e 20 74 68 61 74 20 69 73 20 73    When that is s
15bc7 6f 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  o, this routine 
15bc8 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 61  can be used to a
15bc9 63 63 65 73 73 20 74 68 65 0a 2a 2a 20 6b 65 79  ccess the.** key
15bca 20 61 6e 64 20 64 61 74 61 20 77 69 74 68 6f 75   and data withou
15bcb 74 20 6d 61 6b 69 6e 67 20 61 20 63 6f 70 79 2e  t making a copy.
15bcc 20 20 49 66 20 74 68 65 20 6b 65 79 20 61 6e 64    If the key and
15bcd 2f 6f 72 20 64 61 74 61 20 73 70 69 6c 6c 73 0a  /or data spills.
15bce 2a 2a 20 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77  ** onto overflow
15bcf 20 70 61 67 65 73 2c 20 74 68 65 6e 20 61 63 63   pages, then acc
15bd0 65 73 73 50 61 79 6c 6f 61 64 28 29 20 6d 75 73  essPayload() mus
15bd1 74 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61  t be used to rea
15bd2 73 73 65 6d 62 6c 65 0a 2a 2a 20 74 68 65 20 6b  ssemble.** the k
15bd3 65 79 2f 64 61 74 61 20 61 6e 64 20 63 6f 70 79  ey/data and copy
15bd4 20 69 74 20 69 6e 74 6f 20 61 20 70 72 65 61 6c   it into a preal
15bd5 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 2e 0a  located buffer..
15bd6 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65  **.** The pointe
15bd7 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  r returned by th
15bd8 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73  is routine looks
15bd9 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 74   directly into t
15bda 68 65 20 63 61 63 68 65 64 0a 2a 2a 20 70 61 67  he cached.** pag
15bdb 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
15bdc 65 2e 20 20 54 68 65 20 64 61 74 61 20 6d 69 67  e.  The data mig
15bdd 68 74 20 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76  ht change or mov
15bde 65 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 0a  e the next time.
15bdf 2a 2a 20 61 6e 79 20 62 74 72 65 65 20 72 6f 75  ** any btree rou
15be0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
15be1 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
15be2 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 66  unsigned char *f
15be3 65 74 63 68 50 61 79 6c 6f 61 64 28 0a 20 20 42  etchPayload(.  B
15be4 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20  tCursor *pCur,  
15be5 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f      /* Cursor po
15be6 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20  inting to entry 
15be7 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a  to read from */.
15be8 20 20 69 6e 74 20 2a 70 41 6d 74 2c 20 20 20 20    int *pAmt,    
15be9 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
15bea 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76  the number of av
15beb 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 68 65  ailable bytes he
15bec 72 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70  re */.  int skip
15bed 4b 65 79 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Key          /* 
15bee 72 65 61 64 20 62 65 67 69 6e 6e 69 6e 67 20 61  read beginning a
15bef 74 20 64 61 74 61 20 69 66 20 74 68 69 73 20 69  t data if this i
15bf0 73 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 75  s true */.){.  u
15bf1 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 50  nsigned char *aP
15bf2 61 79 6c 6f 61 64 3b 0a 20 20 4d 65 6d 50 61 67  ayload;.  MemPag
15bf3 65 20 2a 70 50 61 67 65 3b 0a 20 20 75 33 32 20  e *pPage;.  u32 
15bf4 6e 4b 65 79 3b 0a 20 20 75 33 32 20 6e 4c 6f 63  nKey;.  u32 nLoc
15bf5 61 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  al;..  assert( p
15bf6 43 75 72 21 3d 30 20 26 26 20 70 43 75 72 2d 3e  Cur!=0 && pCur->
15bf7 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72  iPage>=0 && pCur
15bf8 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
15bf9 50 61 67 65 5d 29 3b 0a 20 20 61 73 73 65 72 74  Page]);.  assert
15bfa 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
15bfb 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
15bfc 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
15bfd 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
15bfe 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43   );.  pPage = pC
15bff 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
15c00 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72  >iPage];.  asser
15c01 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  t( pCur->aiIdx[p
15c02 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67  Cur->iPage]<pPag
15c03 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 67 65  e->nCell );.  ge
15c04 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b  tCellInfo(pCur);
15c05 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43  .  aPayload = pC
15c06 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 3b 0a  ur->info.pCell;.
15c07 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 70 43    aPayload += pC
15c08 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72  ur->info.nHeader
15c09 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69  ;.  if( pPage->i
15c0a 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 6e 4b 65  ntKey ){.    nKe
15c0b 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  y = 0;.  }else{.
15c0c 20 20 20 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29      nKey = (int)
15c0d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b  pCur->info.nKey;
15c0e 0a 20 20 7d 0a 20 20 69 66 28 20 73 6b 69 70 4b  .  }.  if( skipK
15c0f 65 79 20 29 7b 0a 20 20 20 20 61 50 61 79 6c 6f  ey ){.    aPaylo
15c10 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20 20  ad += nKey;.    
15c11 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69  nLocal = pCur->i
15c12 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6e 4b 65  nfo.nLocal - nKe
15c13 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  y;.  }else{.    
15c14 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69  nLocal = pCur->i
15c15 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20  nfo.nLocal;.    
15c16 69 66 28 20 6e 4c 6f 63 61 6c 3e 6e 4b 65 79 20  if( nLocal>nKey 
15c17 29 7b 0a 20 20 20 20 20 20 6e 4c 6f 63 61 6c 20  ){.      nLocal 
15c18 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 7d 0a 20 20  = nKey;.    }.  
15c19 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63  }.  *pAmt = nLoc
15c1a 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61  al;.  return aPa
15c1b 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  yload;.}.../*.**
15c1c 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74   For the entry t
15c1d 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20  hat cursor pCur 
15c1e 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74  is point to, ret
15c1f 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62  urn as.** many b
15c20 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20  ytes of the key 
15c21 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61  or data as are a
15c22 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
15c23 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20  local.** b-tree 
15c24 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65  page.  Write the
15c25 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c   number of avail
15c26 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20  able bytes into 
15c27 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  *pAmt..**.** The
15c28 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
15c29 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20  d is ephemeral. 
15c2a 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61   The key/data ma
15c2b 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20  y move.** or be 
15c2c 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65  destroyed on the
15c2d 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e   next call to an
15c2e 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c  y Btree routine,
15c2f 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61  .** including ca
15c30 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74  lls from other t
15c31 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74  hreads against t
15c32 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a  he same cache..*
15c33 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78  * Hence, a mutex
15c34 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64   on the BtShared
15c35 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20   should be held 
15c36 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67  prior to calling
15c37 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
15c38 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
15c39 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74  utines is used t
15c3a 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65  o get quick acce
15c3b 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61  ss to key and da
15c3c 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d  ta.** in the com
15c3d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e  mon case where n
15c3e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
15c3f 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51   are used..*/.SQ
15c40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
15c41 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
15c42 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74  BtreeKeyFetch(Bt
15c43 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
15c44 74 20 2a 70 41 6d 74 29 7b 0a 20 20 61 73 73 65  t *pAmt){.  asse
15c45 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
15c46 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
15c47 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
15c48 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
15c49 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 63 6f  {.    return (co
15c4a 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50  nst void*)fetchP
15c4b 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d  ayload(pCur, pAm
15c4c 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  t, 0);.  }.  ret
15c4d 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f  urn 0;.}.SQLITE_
15c4e 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f  PRIVATE const vo
15c4f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
15c50 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73  DataFetch(BtCurs
15c51 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
15c52 41 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Amt){.  assert( 
15c53 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
15c54 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20  (pCur) );.  if( 
15c55 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
15c56 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20  RSOR_VALID ){.  
15c57 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20    return (const 
15c58 76 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f  void*)fetchPaylo
15c59 61 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 31  ad(pCur, pAmt, 1
15c5a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
15c5b 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76  0;.}.../*.** Mov
15c5c 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77  e the cursor dow
15c5d 6e 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64  n to a new child
15c5e 20 70 61 67 65 2e 20 20 54 68 65 20 6e 65 77 50   page.  The newP
15c5f 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 69 73 20  gno argument is 
15c60 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62  the.** page numb
15c61 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  er of the child 
15c62 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e  page to move to.
15c63 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
15c64 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75 72  oveToChild(BtCur
15c65 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6e  sor *pCur, u32 n
15c66 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72  ewPgno){.  int r
15c67 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43 75  c;.  int i = pCu
15c68 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50  r->iPage;.  MemP
15c69 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20  age *pNewPage;. 
15c6a 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15c6b 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61   pCur->pBt;..  a
15c6c 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
15c6d 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
15c6e 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
15c6f 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15c70 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
15c71 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 42  t( pCur->iPage<B
15c72 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54  TCURSOR_MAX_DEPT
15c73 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  H );.  if( pCur-
15c74 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 53 4f  >iPage>=(BTCURSO
15c75 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 20 29  R_MAX_DEPTH-1) )
15c76 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
15c77 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15c78 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74  ;.  }.  rc = get
15c79 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c  AndInitPage(pBt,
15c7a 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50   newPgno, &pNewP
15c7b 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
15c7c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 43   return rc;.  pC
15c7d 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 5d 20  ur->apPage[i+1] 
15c7e 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 70 43  = pNewPage;.  pC
15c7f 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d 20 3d  ur->aiIdx[i+1] =
15c80 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67   0;.  pCur->iPag
15c81 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e  e++;..  pCur->in
15c82 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
15c83 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
15c84 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 77 50  = 0;.  if( pNewP
15c85 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 29 7b 0a  age->nCell<1 ){.
15c86 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
15c87 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
15c88 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
15c89 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
15c8a 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ef NDEBUG./*.** 
15c8b 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 20  Page pParent is 
15c8c 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e  an internal (non
15c8d 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 65  -leaf) tree page
15c8e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
15c8f 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 74  .** asserts that
15c90 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68   page number iCh
15c91 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d  ild is the left-
15c92 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 64  child if the iId
15c93 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20  x'th.** cell in 
15c94 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72  page pParent. Or
15c95 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 71 75  , if iIdx is equ
15c96 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20  al to the total 
15c97 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c  number of.** cel
15c98 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74  ls in pParent, t
15c99 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  hat page number 
15c9a 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 69  iChild is the ri
15c9b 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20  ght-child of.** 
15c9c 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61  the page..*/.sta
15c9d 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50  tic void assertP
15c9e 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61  arentIndex(MemPa
15c9f 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74  ge *pParent, int
15ca0 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69   iIdx, Pgno iChi
15ca1 6c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  ld){.  assert( i
15ca2 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43  Idx<=pParent->nC
15ca3 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64  ell );.  if( iId
15ca4 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c  x==pParent->nCel
15ca5 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  l ){.    assert(
15ca6 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65   get4byte(&pPare
15ca7 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e  nt->aData[pParen
15ca8 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  t->hdrOffset+8])
15ca9 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65  ==iChild );.  }e
15caa 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
15cab 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65   get4byte(findCe
15cac 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78  ll(pParent, iIdx
15cad 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20  ))==iChild );.  
15cae 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66  }.}.#else.#  def
15caf 69 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e 74  ine assertParent
15cb0 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65  Index(x,y,z) .#e
15cb1 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  ndif../*.** Move
15cb2 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74   the cursor up t
15cb3 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  o the parent pag
15cb4 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69  e..**.** pCur->i
15cb5 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  dx is set to the
15cb6 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74   cell index that
15cb7 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f   contains the po
15cb8 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  inter.** to the 
15cb9 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69  page we are comi
15cba 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20  ng from.  If we 
15cbb 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20  are coming from 
15cbc 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73  the.** right-mos
15cbd 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 65  t child page the
15cbe 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73  n pCur->idx is s
15cbf 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74  et to one more t
15cc0 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65  han.** the large
15cc1 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a  st cell index..*
15cc2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15cc3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
15cc4 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42  eeMoveToParent(B
15cc5 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
15cc6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
15cc7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
15cc8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
15cc9 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
15cca 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73  OR_VALID );.  as
15ccb 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67  sert( pCur->iPag
15ccc 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  e>0 );.  assert(
15ccd 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
15cce 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20  ur->iPage] );.  
15ccf 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65  assertParentInde
15cd0 78 28 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50  x(.    pCur->apP
15cd1 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d  age[pCur->iPage-
15cd2 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61  1], .    pCur->a
15cd3 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
15cd4 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e  -1], .    pCur->
15cd5 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
15cd6 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20  ge]->pgno.  );. 
15cd7 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75   releasePage(pCu
15cd8 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
15cd9 69 50 61 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d  iPage]);.  pCur-
15cda 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72  >iPage--;.  pCur
15cdb 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
15cdc 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e  ;.  pCur->validN
15cdd 4b 65 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Key = 0;.}../*.*
15cde 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
15cdf 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61  r to the root pa
15ce0 67 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ge.*/.static int
15ce1 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 42 74 43 75   moveToRoot(BtCu
15ce2 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d  rsor *pCur){.  M
15ce3 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20  emPage *pRoot;. 
15ce4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
15ce5 5f 4f 4b 3b 0a 20 20 42 74 72 65 65 20 2a 70 20  _OK;.  Btree *p 
15ce6 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a  = pCur->pBtree;.
15ce7 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
15ce8 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73  = p->pBt;..  ass
15ce9 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
15cea 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
15ceb 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f   assert( CURSOR_
15cec 49 4e 56 41 4c 49 44 20 3c 20 43 55 52 53 4f 52  INVALID < CURSOR
15ced 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a  _REQUIRESEEK );.
15cee 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52    assert( CURSOR
15cef 5f 56 41 4c 49 44 20 20 20 3c 20 43 55 52 53 4f  _VALID   < CURSO
15cf0 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b  R_REQUIRESEEK );
15cf1 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f  .  assert( CURSO
15cf2 52 5f 46 41 55 4c 54 20 20 20 3e 20 43 55 52 53  R_FAULT   > CURS
15cf3 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
15cf4 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53  ;.  if( pCur->eS
15cf5 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51  tate>=CURSOR_REQ
15cf6 55 49 52 45 53 45 45 4b 20 29 7b 0a 20 20 20 20  UIRESEEK ){.    
15cf7 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
15cf8 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29  ==CURSOR_FAULT )
15cf9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  {.      return p
15cfa 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 20 20 7d  Cur->skip;.    }
15cfb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
15cfc 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75  eClearCursor(pCu
15cfd 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  r);.  }..  if( p
15cfe 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 29 7b  Cur->iPage>=0 ){
15cff 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
15d00 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 43 75 72  for(i=1; i<=pCur
15d01 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20  ->iPage; i++){. 
15d02 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
15d03 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d  (pCur->apPage[i]
15d04 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
15d05 7b 0a 20 20 20 20 69 66 28 20 0a 20 20 20 20 20  {.    if( .     
15d06 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
15d07 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65  = getAndInitPage
15d08 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f  (pBt, pCur->pgno
15d09 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50  Root, &pCur->apP
15d0a 61 67 65 5b 30 5d 29 29 0a 20 20 20 20 29 7b 0a  age[0])).    ){.
15d0b 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61        pCur->eSta
15d0c 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41  te = CURSOR_INVA
15d0d 4c 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72  LID;.      retur
15d0e 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
15d0f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75 72 2d  .  pRoot = pCur-
15d10 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 61 73  >apPage[0];.  as
15d11 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e  sert( pRoot->pgn
15d12 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  o==pCur->pgnoRoo
15d13 74 20 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61  t );.  pCur->iPa
15d14 67 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e  ge = 0;.  pCur->
15d15 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20  aiIdx[0] = 0;.  
15d16 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
15d17 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 74   = 0;.  pCur->at
15d18 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 75 72  Last = 0;.  pCur
15d19 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
15d1a 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e  ..  if( pRoot->n
15d1b 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 6f 6f  Cell==0 && !pRoo
15d1c 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50  t->leaf ){.    P
15d1d 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 20 20  gno subpage;.   
15d1e 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e   assert( pRoot->
15d1f 70 67 6e 6f 3d 3d 31 20 29 3b 0a 20 20 20 20 73  pgno==1 );.    s
15d20 75 62 70 61 67 65 20 3d 20 67 65 74 34 62 79 74  ubpage = get4byt
15d21 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b  e(&pRoot->aData[
15d22 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74  pRoot->hdrOffset
15d23 2b 38 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +8]);.    assert
15d24 28 20 73 75 62 70 61 67 65 3e 30 20 29 3b 0a 20  ( subpage>0 );. 
15d25 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
15d26 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a  = CURSOR_VALID;.
15d27 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
15d28 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70 61  hild(pCur, subpa
15d29 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ge);.  }else{.  
15d2a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
15d2b 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e   ((pRoot->nCell>
15d2c 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a  0)?CURSOR_VALID:
15d2d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b  CURSOR_INVALID);
15d2e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
15d2f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
15d30 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20  the cursor down 
15d31 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  to the left-most
15d32 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65   leaf entry bene
15d33 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79  ath the.** entry
15d34 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20   to which it is 
15d35 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
15d36 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65  ng..**.** The le
15d37 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20  ft-most leaf is 
15d38 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65  the one with the
15d39 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20   smallest key - 
15d3a 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20  the first.** in 
15d3b 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e  ascending order.
15d3c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
15d3d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74  oveToLeftmost(Bt
15d3e 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
15d3f 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e   Pgno pgno;.  in
15d40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
15d41 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
15d42 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63  ge;..  assert( c
15d43 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
15d44 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
15d45 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
15d46 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
15d47 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
15d48 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61  LITE_OK && !(pPa
15d49 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
15d4a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d  e[pCur->iPage])-
15d4b 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73 73  >leaf ){.    ass
15d4c 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
15d4d 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50  [pCur->iPage]<pP
15d4e 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
15d4f 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74    pgno = get4byt
15d50 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  e(findCell(pPage
15d51 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  , pCur->aiIdx[pC
15d52 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20  ur->iPage]));.  
15d53 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69    rc = moveToChi
15d54 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a  ld(pCur, pgno);.
15d55 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
15d56 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74  .}../*.** Move t
15d57 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74  he cursor down t
15d58 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  o the right-most
15d59 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65   leaf entry bene
15d5a 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20  ath the.** page 
15d5b 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63  to which it is c
15d5c 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
15d5d 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20 64  g.  Notice the d
15d5e 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74  ifference.** bet
15d5f 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d  ween moveToLeftm
15d60 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f  ost() and moveTo
15d61 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f  Rightmost().  mo
15d62 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a  veToLeftmost().*
15d63 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74  * finds the left
15d64 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65  -most entry bene
15d65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20  ath the *entry* 
15d66 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69  whereas moveToRi
15d67 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e  ghtmost().** fin
15d68 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  ds the right-mos
15d69 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20  t entry beneath 
15d6a 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a  the *page*..**.*
15d6b 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  * The right-most
15d6c 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f 6e   entry is the on
15d6d 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65  e with the large
15d6e 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 73  st key - the las
15d6f 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 65  t.** key in asce
15d70 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a  nding order..*/.
15d71 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54  static int moveT
15d72 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75 72  oRightmost(BtCur
15d73 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67  sor *pCur){.  Pg
15d74 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72  no pgno;.  int r
15d75 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
15d76 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
15d77 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
15d78 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
15d79 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
15d7a 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
15d7b 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
15d7c 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  ;.  while( rc==S
15d7d 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50  QLITE_OK && !(pP
15d7e 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
15d7f 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
15d80 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67  ->leaf ){.    pg
15d81 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
15d82 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
15d83 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
15d84 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64  ;.    pCur->aiId
15d85 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d  x[pCur->iPage] =
15d86 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20   pPage->nCell;. 
15d87 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
15d88 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b  ild(pCur, pgno);
15d89 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
15d8a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
15d8b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
15d8c 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65  ->iPage] = pPage
15d8d 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70  ->nCell-1;.    p
15d8e 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
15d8f 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76  = 0;.    pCur->v
15d90 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20  alidNKey = 0;.  
15d91 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
15d92 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  ../* Move the cu
15d93 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73  rsor to the firs
15d94 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74  t entry in the t
15d95 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51  able.  Return SQ
15d96 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75  LITE_OK.** on su
15d97 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65  ccess.  Set *pRe
15d98 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75  s to 0 if the cu
15d99 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f  rsor actually po
15d9a 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e  ints to somethin
15d9b 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65  g.** or set *pRe
15d9c 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61  s to 1 if the ta
15d9d 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f  ble is empty..*/
15d9e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15d9f 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
15da0 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a  First(BtCursor *
15da1 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29  pCur, int *pRes)
15da2 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  {.  int rc;..  a
15da3 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
15da4 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
15da5 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15da6 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
15da7 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e  ur->pBtree->db->
15da8 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
15da9 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
15daa 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
15dab 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
15dac 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
15dad 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29  CURSOR_INVALID )
15dae 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
15daf 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
15db0 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
15db1 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52  ==0 );.      *pR
15db2 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63  es = 1;.      rc
15db3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
15db4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
15db5 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
15db6 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
15db7 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20  ->nCell>0 );.   
15db8 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
15db9 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c      rc = moveToL
15dba 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20  eftmost(pCur);. 
15dbb 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
15dbc 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65  n rc;.}../* Move
15dbd 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
15dbe 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
15dbf 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74   the table.  Ret
15dc0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  urn SQLITE_OK.**
15dc1 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65   on success.  Se
15dc2 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20  t *pRes to 0 if 
15dc3 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61  the cursor actua
15dc4 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f  lly points to so
15dc5 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65  mething.** or se
15dc6 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20  t *pRes to 1 if 
15dc7 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
15dc8 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ty..*/.SQLITE_PR
15dc9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15dca 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72  3BtreeLast(BtCur
15dcb 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
15dcc 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pRes){.  int rc;
15dcd 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  . .  assert( cur
15dce 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
15dcf 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
15dd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15dd1 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65  eld(pCur->pBtree
15dd2 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
15dd3 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f    rc = moveToRoo
15dd4 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  t(pCur);.  if( r
15dd5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
15dd6 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49      if( CURSOR_I
15dd7 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53  NVALID==pCur->eS
15dd8 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73  tate ){.      as
15dd9 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61  sert( pCur->apPa
15dda 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
15ddb 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  >nCell==0 );.   
15ddc 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20     *pRes = 1;.  
15ddd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
15dde 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
15ddf 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
15de0 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73  D );.      *pRes
15de1 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
15de2 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
15de3 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 67 65  (pCur);.      ge
15de4 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b  tCellInfo(pCur);
15de5 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 74 4c  .      pCur->atL
15de6 61 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49 54 45  ast = rc==SQLITE
15de7 5f 4f 4b 20 3f 31 3a 30 3b 0a 20 20 20 20 7d 0a  _OK ?1:0;.    }.
15de8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
15de9 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20  .}../* Move the 
15dea 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69  cursor so that i
15deb 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65  t points to an e
15dec 6e 74 72 79 20 6e 65 61 72 20 74 68 65 20 6b 65  ntry near the ke
15ded 79 20 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  y .** specified 
15dee 62 79 20 70 49 64 78 4b 65 79 20 6f 72 20 69 6e  by pIdxKey or in
15def 74 4b 65 79 2e 20 20 20 52 65 74 75 72 6e 20 61  tKey.   Return a
15df0 20 73 75 63 63 65 73 73 20 63 6f 64 65 2e 0a 2a   success code..*
15df1 2a 0a 2a 2a 20 46 6f 72 20 49 4e 54 4b 45 59 20  *.** For INTKEY 
15df2 74 61 62 6c 65 73 2c 20 74 68 65 20 69 6e 74 4b  tables, the intK
15df3 65 79 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ey parameter is 
15df4 75 73 65 64 2e 20 20 70 49 64 78 4b 65 79 20 0a  used.  pIdxKey .
15df5 2a 2a 20 6d 75 73 74 20 62 65 20 4e 55 4c 4c 2e  ** must be NULL.
15df6 20 20 46 6f 72 20 69 6e 64 65 78 20 74 61 62 6c    For index tabl
15df7 65 73 2c 20 70 49 64 78 4b 65 79 20 69 73 20 75  es, pIdxKey is u
15df8 73 65 64 20 61 6e 64 20 69 6e 74 4b 65 79 0a 2a  sed and intKey.*
15df9 2a 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a  * is ignored..**
15dfa 0a 2a 2a 20 49 66 20 61 6e 20 65 78 61 63 74 20  .** If an exact 
15dfb 6d 61 74 63 68 20 69 73 20 6e 6f 74 20 66 6f 75  match is not fou
15dfc 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  nd, then the cur
15dfd 73 6f 72 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a  sor is always.**
15dfe 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
15dff 74 20 61 20 6c 65 61 66 20 70 61 67 65 20 77 68  t a leaf page wh
15e00 69 63 68 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74  ich would hold t
15e01 68 65 20 65 6e 74 72 79 20 69 66 20 69 74 0a 2a  he entry if it.*
15e02 2a 20 77 65 72 65 20 70 72 65 73 65 6e 74 2e 20  * were present. 
15e03 20 54 68 65 20 63 75 72 73 6f 72 20 6d 69 67 68   The cursor migh
15e04 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e  t point to an en
15e05 74 72 79 20 74 68 61 74 20 63 6f 6d 65 73 0a 2a  try that comes.*
15e06 2a 20 62 65 66 6f 72 65 20 6f 72 20 61 66 74 65  * before or afte
15e07 72 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a  r the key..**.**
15e08 20 41 6e 20 69 6e 74 65 67 65 72 20 69 73 20 77   An integer is w
15e09 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 52 65  ritten into *pRe
15e0a 73 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72  s which is the r
15e0b 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 63 6f 6d 70  esult of.** comp
15e0c 61 72 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69  aring the key wi
15e0d 74 68 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20  th the entry to 
15e0e 77 68 69 63 68 20 74 68 65 20 63 75 72 73 6f 72  which the cursor
15e0f 20 69 73 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67   is .** pointing
15e10 2e 20 20 54 68 65 20 6d 65 61 6e 69 6e 67 20 6f  .  The meaning o
15e11 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 77 72  f the integer wr
15e12 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 2a 70  itten into.** *p
15e13 52 65 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  Res is as follow
15e14 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52  s:.**.**     *pR
15e15 65 73 3c 30 20 20 20 20 20 20 54 68 65 20 63 75  es<0      The cu
15e16 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69  rsor is left poi
15e17 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72  nting at an entr
15e18 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20  y that.**       
15e19 20 20 20 20 20 20 20 20 20 20 20 69 73 20 73 6d             is sm
15e1a 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e 74 4b 65  aller than intKe
15e1b 79 2f 70 49 64 78 4b 65 79 20 6f 72 20 69 66 20  y/pIdxKey or if 
15e1c 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
15e1d 74 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ty.**           
15e1e 20 20 20 20 20 20 20 61 6e 64 20 74 68 65 20 63         and the c
15e1f 75 72 73 6f 72 20 69 73 20 74 68 65 72 65 66 6f  ursor is therefo
15e20 72 65 20 6c 65 66 74 20 70 6f 69 6e 74 20 74 6f  re left point to
15e21 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   nothing..**.** 
15e22 20 20 20 20 2a 70 52 65 73 3d 3d 30 20 20 20 20      *pRes==0    
15e23 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c   The cursor is l
15e24 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
15e25 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a  an entry that.**
15e26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15e27 20 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65    exactly matche
15e28 73 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79  s intKey/pIdxKey
15e29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65  ..**.**     *pRe
15e2a 73 3e 30 20 20 20 20 20 20 54 68 65 20 63 75 72  s>0      The cur
15e2b 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  sor is left poin
15e2c 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79  ting at an entry
15e2d 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
15e2e 20 20 20 20 20 20 20 20 20 20 69 73 20 6c 61 72            is lar
15e2f 67 65 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f  ger than intKey/
15e30 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53  pIdxKey..**.*/.S
15e31 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
15e32 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  t sqlite3BtreeMo
15e33 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20  vetoUnpacked(.  
15e34 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
15e35 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
15e36 63 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f 76  cursor to be mov
15e37 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64  ed */.  Unpacked
15e38 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 2c  Record *pIdxKey,
15e39 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e 64   /* Unpacked ind
15e3a 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 36 34 20  ex key */.  i64 
15e3b 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 20 20 20  intKey,         
15e3c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
15e3d 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 62  e key */.  int b
15e3e 69 61 73 52 69 67 68 74 2c 20 20 20 20 20 20 20  iasRight,       
15e3f 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20      /* If true, 
15e40 62 69 61 73 20 74 68 65 20 73 65 61 72 63 68 20  bias the search 
15e41 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20  to the high end 
15e42 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20  */.  int *pRes  
15e43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15e44 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 65   Write search re
15e45 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b  sults here */.){
15e46 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
15e47 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
15e48 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
15e49 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15e4a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75  3_mutex_held(pCu
15e4b 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d  r->pBtree->db->m
15e4c 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  utex) );..  /* I
15e4d 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
15e4e 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e  already position
15e4f 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20  ed at the point 
15e50 77 65 20 61 72 65 20 74 72 79 69 6e 67 0a 20 20  we are trying.  
15e51 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c 20 74  ** to move to, t
15e52 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20  hen just return 
15e53 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e  without doing an
15e54 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 66 28 20  y work */.  if( 
15e55 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
15e56 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 20 70 43  RSOR_VALID && pC
15e57 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 0a 20  ur->validNKey . 
15e58 20 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67    && pCur->apPag
15e59 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 0a 20 20  e[0]->intKey .  
15e5a 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
15e5b 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b  >info.nKey==intK
15e5c 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65  ey ){.      *pRe
15e5d 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74  s = 0;.      ret
15e5e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
15e5f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75     }.    if( pCu
15e60 72 2d 3e 61 74 4c 61 73 74 20 26 26 20 70 43 75  r->atLast && pCu
15e61 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74  r->info.nKey<int
15e62 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52  Key ){.      *pR
15e63 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72  es = -1;.      r
15e64 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15e65 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 63  .    }.  }..  rc
15e66 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43   = moveToRoot(pC
15e67 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  ur);.  if( rc ){
15e68 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
15e69 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
15e6a 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
15e6b 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73  >iPage] );.  ass
15e6c 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
15e6d 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
15e6e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20  isInit );.  if( 
15e6f 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
15e70 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a  RSOR_INVALID ){.
15e71 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a      *pRes = -1;.
15e72 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
15e73 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
15e74 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20  Page]->nCell==0 
15e75 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
15e76 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61  LITE_OK;.  }.  a
15e77 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
15e78 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c  age[0]->intKey |
15e79 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a 20 20 66  | pIdxKey );.  f
15e7a 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69 6e 74 20  or(;;){.    int 
15e7b 6c 77 72 2c 20 75 70 72 3b 0a 20 20 20 20 50 67  lwr, upr;.    Pg
15e7c 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d  no chldPg;.    M
15e7d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
15e7e 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
15e7f 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69  r->iPage];.    i
15e80 6e 74 20 63 20 3d 20 2d 31 3b 20 20 2f 2a 20 70  nt c = -1;  /* p
15e81 52 65 73 20 72 65 74 75 72 6e 20 69 66 20 74 61  Res return if ta
15e82 62 6c 65 20 69 73 20 65 6d 70 74 79 20 6d 75 73  ble is empty mus
15e83 74 20 62 65 20 2d 31 20 2a 2f 0a 20 20 20 20 6c  t be -1 */.    l
15e84 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 20  wr = 0;.    upr 
15e85 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31  = pPage->nCell-1
15e86 3b 0a 20 20 20 20 69 66 28 20 28 21 70 50 61 67  ;.    if( (!pPag
15e87 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 70 49 64  e->intKey && pId
15e88 78 4b 65 79 3d 3d 30 29 20 7c 7c 20 75 70 72 3c  xKey==0) || upr<
15e89 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
15e8a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
15e8b 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
15e8c 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
15e8d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 69 61     }.    if( bia
15e8e 73 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  sRight ){.      
15e8f 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
15e90 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29  ->iPage] = (u16)
15e91 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  upr;.    }else{.
15e92 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64        pCur->aiId
15e93 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d  x[pCur->iPage] =
15e94 20 28 75 31 36 29 28 28 75 70 72 2b 6c 77 72 29   (u16)((upr+lwr)
15e95 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  /2);.    }.    f
15e96 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20 20 76 6f  or(;;){.      vo
15e97 69 64 20 2a 70 43 65 6c 6c 4b 65 79 3b 0a 20 20  id *pCellKey;.  
15e98 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79      i64 nCellKey
15e99 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 20  ;.      int idx 
15e9a 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  = pCur->aiIdx[pC
15e9b 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
15e9c 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
15e9d 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43  ze = 0;.      pC
15e9e 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
15e9f 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  1;.      if( pPa
15ea0 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
15ea1 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b        u8 *pCell;
15ea2 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d  .        pCell =
15ea3 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
15ea4 20 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e 63   idx) + pPage->c
15ea5 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 20  hildPtrSize;.   
15ea6 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
15ea7 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20  hasData ){.     
15ea8 20 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a       u32 dummy;.
15ea9 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20            pCell 
15eaa 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70  += getVarint32(p
15eab 43 65 6c 6c 2c 20 64 75 6d 6d 79 29 3b 0a 20 20  Cell, dummy);.  
15eac 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
15ead 67 65 74 56 61 72 69 6e 74 28 70 43 65 6c 6c 2c  getVarint(pCell,
15eae 20 28 75 36 34 2a 29 26 6e 43 65 6c 6c 4b 65 79   (u64*)&nCellKey
15eaf 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e  );.        if( n
15eb0 43 65 6c 6c 4b 65 79 3d 3d 69 6e 74 4b 65 79 20  CellKey==intKey 
15eb1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d  ){.          c =
15eb2 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   0;.        }els
15eb3 65 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 69  e if( nCellKey<i
15eb4 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  ntKey ){.       
15eb5 20 20 20 63 20 3d 20 2d 31 3b 0a 20 20 20 20 20     c = -1;.     
15eb6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
15eb7 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c      assert( nCel
15eb8 6c 4b 65 79 3e 69 6e 74 4b 65 79 20 29 3b 0a 20  lKey>intKey );. 
15eb9 20 20 20 20 20 20 20 20 20 63 20 3d 20 2b 31 3b           c = +1;
15eba 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
15ebb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
15ebc 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 3b 0a 20  int available;. 
15ebd 20 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 20         pCellKey 
15ebe 3d 20 28 76 6f 69 64 20 2a 29 66 65 74 63 68 50  = (void *)fetchP
15ebf 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 26 61 76  ayload(pCur, &av
15ec0 61 69 6c 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20  ailable, 0);.   
15ec1 20 20 20 20 20 6e 43 65 6c 6c 4b 65 79 20 3d 20       nCellKey = 
15ec2 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b  pCur->info.nKey;
15ec3 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 76 61  .        if( ava
15ec4 69 6c 61 62 6c 65 3e 3d 6e 43 65 6c 6c 4b 65 79  ilable>=nCellKey
15ec5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20   ){.          c 
15ec6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
15ec7 6f 72 64 43 6f 6d 70 61 72 65 28 28 69 6e 74 29  ordCompare((int)
15ec8 6e 43 65 6c 6c 4b 65 79 2c 20 70 43 65 6c 6c 4b  nCellKey, pCellK
15ec9 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20  ey, pIdxKey);.  
15eca 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
15ecb 20 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 20         pCellKey 
15ecc 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
15ecd 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 20 29   (int)nCellKey )
15ece 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
15ecf 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20  pCellKey==0 ){. 
15ed0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
15ed1 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15ed2 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d            goto m
15ed3 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20  oveto_finish;.  
15ed4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15ed5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
15ed6 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30  BtreeKey(pCur, 0
15ed7 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c  , (int)nCellKey,
15ed8 20 28 76 6f 69 64 2a 29 70 43 65 6c 6c 4b 65 79   (void*)pCellKey
15ed9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d  );.          c =
15eda 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f   sqlite3VdbeReco
15edb 72 64 43 6f 6d 70 61 72 65 28 28 69 6e 74 29 6e  rdCompare((int)n
15edc 43 65 6c 6c 4b 65 79 2c 20 70 43 65 6c 6c 4b 65  CellKey, pCellKe
15edd 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20  y, pIdxKey);.   
15ede 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
15edf 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20  ree(pCellKey);. 
15ee0 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20           if( rc 
15ee1 29 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69  ) goto moveto_fi
15ee2 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nish;.        }.
15ee3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
15ee4 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
15ee5 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65    pCur->info.nKe
15ee6 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20  y = nCellKey;.  
15ee7 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d        if( pPage-
15ee8 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67  >intKey && !pPag
15ee9 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  e->leaf ){.     
15eea 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 3b 0a       lwr = idx;.
15eeb 20 20 20 20 20 20 20 20 20 20 75 70 72 20 3d 20            upr = 
15eec 6c 77 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  lwr - 1;.       
15eed 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
15eee 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
15eef 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
15ef0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
15ef1 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20  ITE_OK;.        
15ef2 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69    goto moveto_fi
15ef3 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nish;.        }.
15ef4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
15ef5 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ( c<0 ){.       
15ef6 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 20   lwr = idx+1;.  
15ef7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
15ef8 20 20 20 75 70 72 20 3d 20 69 64 78 2d 31 3b 0a     upr = idx-1;.
15ef9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
15efa 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a 20 20 20  ( lwr>upr ){.   
15efb 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e       pCur->info.
15efc 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79 3b  nKey = nCellKey;
15efd 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
15efe 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
15eff 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
15f00 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28  iPage] = (u16)((
15f01 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20  lwr+upr)/2);.   
15f02 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c   }.    assert( l
15f03 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20  wr==upr+1 );.   
15f04 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15f05 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66  isInit );.    if
15f06 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b  ( pPage->leaf ){
15f07 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20  .      chldPg = 
15f08 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
15f09 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65   lwr>=pPage->nCe
15f0a 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64  ll ){.      chld
15f0b 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  Pg = get4byte(&p
15f0c 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
15f0d 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
15f0e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
15f0f 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34     chldPg = get4
15f10 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50  byte(findCell(pP
15f11 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20  age, lwr));.    
15f12 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67  }.    if( chldPg
15f13 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
15f14 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
15f15 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43  [pCur->iPage]<pC
15f16 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
15f17 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29  >iPage]->nCell )
15f18 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 65 73  ;.      if( pRes
15f19 20 29 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20 20   ) *pRes = c;.  
15f1a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
15f1b 4f 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d  OK;.      goto m
15f1c 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20  oveto_finish;.  
15f1d 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69    }.    pCur->ai
15f1e 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
15f1f 20 3d 20 28 75 31 36 29 6c 77 72 3b 0a 20 20 20   = (u16)lwr;.   
15f20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
15f21 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d  e = 0;.    pCur-
15f22 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
15f23 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
15f24 68 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50  hild(pCur, chldP
15f25 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  g);.    if( rc )
15f26 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e   goto moveto_fin
15f27 69 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f 5f  ish;.  }.moveto_
15f28 66 69 6e 69 73 68 3a 0a 20 20 72 65 74 75 72 6e  finish:.  return
15f29 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e   rc;.}../*.** In
15f2a 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   this version of
15f2b 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b   BtreeMoveto, pK
15f2c 65 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69  ey is a packed i
15f2d 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73  ndex record.** s
15f2e 75 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61  uch as is genera
15f2f 74 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61  ted by the OP_Ma
15f30 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e  keRecord opcode.
15f31 20 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20    Unpack the.** 
15f32 72 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20  record and then 
15f33 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f  call BtreeMoveto
15f34 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f  Unpacked() to do
15f35 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 53 51   the work..*/.SQ
15f36 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15f37 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
15f38 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20  eto(.  BtCursor 
15f39 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75  *pCur,     /* Cu
15f3a 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65  rsor open on the
15f3b 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61   btree to be sea
15f3c 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  rched */.  const
15f3d 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f   void *pKey,   /
15f3e 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20  * Packed key if 
15f3f 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20  the btree is an 
15f40 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e  index */.  i64 n
15f41 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Key,           /
15f42 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f  * Integer key fo
15f43 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20  r tables.  Size 
15f44 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69  of pKey for indi
15f45 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61  ces */.  int bia
15f46 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
15f47 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74  Bias search to t
15f48 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20  he high end */. 
15f49 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20   int *pRes      
15f4a 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65       /* Write se
15f4b 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72  arch results her
15f4c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
15f4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15f4e 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63       /* Status c
15f4f 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65  ode */.  Unpacke
15f50 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79  dRecord *pIdxKey
15f51 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  ;   /* Unpacked 
15f52 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 55  index key */.  U
15f53 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 61 53  npackedRecord aS
15f54 70 61 63 65 5b 31 36 5d 3b 20 2f 2a 20 54 65 6d  pace[16]; /* Tem
15f55 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78  p space for pIdx
15f56 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61  Key - to avoid a
15f57 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66   malloc */..  if
15f58 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73  ( pKey ){.    as
15f59 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34  sert( nKey==(i64
15f5a 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20  )(int)nKey );.  
15f5b 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69    pIdxKey = sqli
15f5c 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
15f5d 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e  ack(pCur->pKeyIn
15f5e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70  fo, (int)nKey, p
15f5f 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
15f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15f61 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63             aSpac
15f62 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65  e, sizeof(aSpace
15f63 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78  ));.    if( pIdx
15f64 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Key==0 ) return 
15f65 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15f66 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b  }else{.    pIdxK
15f67 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63  ey = 0;.  }.  rc
15f68 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
15f69 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43  ovetoUnpacked(pC
15f6a 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65  ur, pIdxKey, nKe
15f6b 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a  y, bias, pRes);.
15f6c 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20    if( pKey ){.  
15f6d 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
15f6e 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
15f6f 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a  d(pIdxKey);.  }.
15f70 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
15f71 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
15f72 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72  UE if the cursor
15f73 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67   is not pointing
15f74 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20   at an entry of 
15f75 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  the table..**.**
15f76 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65   TRUE will be re
15f77 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63  turned after a c
15f78 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74  all to sqlite3Bt
15f79 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a  reeNext() moves.
15f7a 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74  ** past the last
15f7b 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61   entry in the ta
15f7c 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74  ble or sqlite3Bt
15f7d 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20  reePrev() moves 
15f7e 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73  past.** the firs
15f7f 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69  t entry.  TRUE i
15f80 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20  s also returned 
15f81 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
15f82 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  empty..*/.SQLITE
15f83 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15f84 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43  ite3BtreeEof(BtC
15f85 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
15f86 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66  /* TODO: What if
15f87 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69   the cursor is i
15f88 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  n CURSOR_REQUIRE
15f89 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62  SEEK but all tab
15f8a 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20  le entries.  ** 
15f8b 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65  have been delete
15f8c 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c  d? This API will
15f8d 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20   need to change 
15f8e 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  to return an err
15f8f 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20  or code.  ** as 
15f90 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c  well as the bool
15f91 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65  ean result value
15f92 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ..  */.  return 
15f93 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70  (CURSOR_VALID!=p
15f94 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a  Cur->eState);.}.
15f95 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
15f96 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
15f97 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72  ction handle for
15f98 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51   a cursor..*/.SQ
15f99 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
15f9a 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42 74 72  ite3 *sqlite3Btr
15f9b 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e 73 74  eeCursorDb(const
15f9c 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29   BtCursor *pCur)
15f9d 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
15f9e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
15f9f 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d  Cur->pBtree->db-
15fa0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74  >mutex) );.  ret
15fa1 75 72 6e 20 70 43 75 72 2d 3e 70 42 74 72 65 65  urn pCur->pBtree
15fa2 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ->db;.}../*.** A
15fa3 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f  dvance the curso
15fa4 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e  r to the next en
15fa5 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
15fa6 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63  ase.  If.** succ
15fa7 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20  essful then set 
15fa8 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65  *pRes=0.  If the
15fa9 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61   cursor.** was a
15faa 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20  lready pointing 
15fab 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  to the last entr
15fac 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
15fad 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73  e before.** this
15fae 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
15faf 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70  led, then set *p
15fb0 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Res=1..*/.SQLITE
15fb1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15fb2 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74  ite3BtreeNext(Bt
15fb3 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
15fb4 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20  t *pRes){.  int 
15fb5 72 63 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20  rc;.  int idx;. 
15fb6 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
15fb7 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
15fb8 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
15fb9 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73  r) );.  rc = res
15fba 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
15fbb 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20  on(pCur);.  if( 
15fbc 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
15fbd 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
15fbe 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 52    }.  assert( pR
15fbf 65 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 43  es!=0 );.  if( C
15fc0 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70  URSOR_INVALID==p
15fc1 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20  Cur->eState ){. 
15fc2 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20     *pRes = 1;.  
15fc3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15fc4 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  OK;.  }.  if( pC
15fc5 75 72 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a 20 20  ur->skip>0 ){.  
15fc6 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30    pCur->skip = 0
15fc7 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b  ;.    *pRes = 0;
15fc8 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
15fc9 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75  TE_OK;.  }.  pCu
15fca 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a 20 20  r->skip = 0;..  
15fcb 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
15fcc 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
15fcd 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 75  ];.  idx = ++pCu
15fce 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
15fcf 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28  Page];.  assert(
15fd0 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
15fd1 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c  ;.  assert( idx<
15fd2 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b  =pPage->nCell );
15fd3 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  ..  pCur->info.n
15fd4 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72  Size = 0;.  pCur
15fd5 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
15fd6 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67  .  if( idx>=pPag
15fd7 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20  e->nCell ){.    
15fd8 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
15fd9 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d   ){.      rc = m
15fda 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c  oveToChild(pCur,
15fdb 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
15fdc 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
15fdd 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20  drOffset+8]));. 
15fde 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
15fdf 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72  turn rc;.      r
15fe0 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  c = moveToLeftmo
15fe1 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20  st(pCur);.      
15fe2 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20  *pRes = 0;.     
15fe3 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
15fe4 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20  }.    do{.      
15fe5 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d  if( pCur->iPage=
15fe6 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  =0 ){.        *p
15fe7 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Res = 1;.       
15fe8 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
15fe9 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
15fea 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
15feb 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
15fec 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  }.      sqlite3B
15fed 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74  treeMoveToParent
15fee 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50  (pCur);.      pP
15fef 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
15ff0 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
15ff1 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75  .    }while( pCu
15ff2 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
15ff3 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43  Page]>=pPage->nC
15ff4 65 6c 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73  ell );.    *pRes
15ff5 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50   = 0;.    if( pP
15ff6 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
15ff7 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
15ff8 33 42 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c  3BtreeNext(pCur,
15ff9 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73   pRes);.    }els
15ffa 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  e{.      rc = SQ
15ffb 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
15ffc 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
15ffd 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20  }.  *pRes = 0;. 
15ffe 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66   if( pPage->leaf
15fff 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16000 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
16001 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d  rc = moveToLeftm
16002 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74  ost(pCur);.  ret
16003 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
16004 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 73 6f  * Step the curso
16005 72 20 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f  r to the back to
16006 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e   the previous en
16007 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
16008 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63  ase.  If.** succ
16009 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20  essful then set 
1600a 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65  *pRes=0.  If the
1600b 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61   cursor.** was a
1600c 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20  lready pointing 
1600d 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74  to the first ent
1600e 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ry in the databa
1600f 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69  se before.** thi
16010 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61  s routine was ca
16011 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a  lled, then set *
16012 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54  pRes=1..*/.SQLIT
16013 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
16014 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f  lite3BtreePrevio
16015 75 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  us(BtCursor *pCu
16016 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20  r, int *pRes){. 
16017 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61   int rc;.  MemPa
16018 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73  ge *pPage;..  as
16019 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
1601a 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
1601b 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75    rc = restoreCu
1601c 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75  rsorPosition(pCu
1601d 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  r);.  if( rc!=SQ
1601e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
1601f 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
16020 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30  pCur->atLast = 0
16021 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49  ;.  if( CURSOR_I
16022 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53  NVALID==pCur->eS
16023 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65  tate ){.    *pRe
16024 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  s = 1;.    retur
16025 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
16026 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69  .  if( pCur->ski
16027 70 3c 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  p<0 ){.    pCur-
16028 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a  >skip = 0;.    *
16029 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65  pRes = 0;.    re
1602a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1602b 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70    }.  pCur->skip
1602c 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d   = 0;..  pPage =
1602d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1602e 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73  ur->iPage];.  as
1602f 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
16030 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50  nit );.  if( !pP
16031 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
16032 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d   int idx = pCur-
16033 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
16034 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f  ge];.    rc = mo
16035 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
16036 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c  get4byte(findCel
16037 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b  l(pPage, idx)));
16038 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
16039 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1603a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d      }.    rc = m
1603b 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70  oveToRightmost(p
1603c 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Cur);.  }else{. 
1603d 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e     while( pCur->
1603e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1603f 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  e]==0 ){.      i
16040 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d  f( pCur->iPage==
16041 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75  0 ){.        pCu
16042 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
16043 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20  OR_INVALID;.    
16044 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20      *pRes = 1;. 
16045 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
16046 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
16047 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
16048 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28  reeMoveToParent(
16049 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pCur);.    }.   
1604a 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
1604b 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d  e = 0;.    pCur-
1604c 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
1604d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78  .    pCur->aiIdx
1604e 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b  [pCur->iPage]--;
1604f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75  .    pPage = pCu
16050 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16051 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20  iPage];.    if( 
16052 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26  pPage->intKey &&
16053 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
16054 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
16055 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
16056 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20  (pCur, pRes);.  
16057 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
16058 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
16059 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73     }.  }.  *pRes
1605a 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72   = 0;.  return r
1605b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  c;.}../*.** Allo
1605c 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  cate a new page 
1605d 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1605e 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
1605f 65 20 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61  e new page is ma
16060 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20  rked as dirty.  
16061 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
16062 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
16063 74 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65  te().** has alre
16064 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  ady been called 
16065 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e  on the new page.
16066 29 20 20 54 68 65 20 6e 65 77 20 70 61 67 65 20  )  The new page 
16067 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e  has also.** been
16068 20 72 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20   referenced and 
16069 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  the calling rout
1606a 69 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ine is responsib
1606b 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a  le for calling.*
1606c 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e  * sqlite3PagerUn
1606d 72 65 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77  ref() on the new
1606e 20 70 61 67 65 20 77 68 65 6e 20 69 74 20 69 73   page when it is
1606f 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   done..**.** SQL
16070 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
16071 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  ed on success.  
16072 41 6e 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e  Any other return
16073 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
16074 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a  .** an error.  *
16075 70 70 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e  ppPage and *pPgn
16076 6f 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20  o are undefined 
16077 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20  in the event of 
16078 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20  an error..** Do 
16079 6e 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  not invoke sqlit
1607a 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f  e3PagerUnref() o
1607b 6e 20 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20  n *ppPage if an 
1607c 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
1607d 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1607e 22 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 74  "nearby" paramet
1607f 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65  er is not 0, the
16080 6e 20 61 20 28 66 65 65 62 6c 65 29 20 65 66 66  n a (feeble) eff
16081 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a  ort is made to .
16082 2a 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65  ** locate a page
16083 20 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61   close to the pa
16084 67 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62  ge number "nearb
16085 79 22 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  y".  This can be
16086 20 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61   used in an.** a
16087 74 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72  ttempt to keep r
16088 65 6c 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f  elated pages clo
16089 73 65 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72  se to each other
1608a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1608b 20 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20   file,.** which 
1608c 69 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65  in turn can make
1608d 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73   database access
1608e 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49   faster..**.** I
1608f 66 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61  f the "exact" pa
16090 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30  rameter is not 0
16091 2c 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e  , and the page-n
16092 75 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69  umber nearby exi
16093 73 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65  sts .** anywhere
16094 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   on the free-lis
16095 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75  t, then it is gu
16096 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72  arenteed to be r
16097 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a  eturned. This.**
16098 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79   is only used by
16099 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
1609a 61 62 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f  abases when allo
1609b 63 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62  cating a new tab
1609c 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1609d 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  t allocateBtreeP
1609e 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20  age(.  BtShared 
1609f 2a 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65  *pBt, .  MemPage
160a0 20 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67   **ppPage, .  Pg
160a1 6e 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67  no *pPgno, .  Pg
160a2 6e 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20  no nearby,.  u8 
160a3 65 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61  exact.){.  MemPa
160a4 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e  ge *pPage1;.  in
160a5 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 20 20  t rc;.  int n;  
160a6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
160a7 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65  pages on the fre
160a8 65 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6b  elist */.  int k
160a9 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
160aa 6f 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65  of leaves on the
160ab 20 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72   trunk of the fr
160ac 65 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50  eelist */.  MemP
160ad 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b  age *pTrunk = 0;
160ae 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65  .  MemPage *pPre
160af 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61  vTrunk = 0;..  a
160b0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
160b1 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
160b2 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65  utex) );.  pPage
160b3 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b  1 = pBt->pPage1;
160b4 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28  .  n = get4byte(
160b5 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
160b6 36 5d 29 3b 0a 20 20 69 66 28 20 6e 3e 30 20 29  6]);.  if( n>0 )
160b7 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61  {.    /* There a
160b8 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20  re pages on the 
160b9 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65  freelist.  Reuse
160ba 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61   one of those pa
160bb 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f  ges. */.    Pgno
160bc 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20   iTrunk;.    u8 
160bd 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20  searchList = 0; 
160be 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c  /* If the free-l
160bf 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72  ist must be sear
160c0 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79  ched for 'nearby
160c1 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a  ' */.    .    /*
160c2 20 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20   If the 'exact' 
160c3 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72  parameter was tr
160c4 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f  ue and a query o
160c5 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  f the pointer-ma
160c6 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74  p.    ** shows t
160c7 68 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65  hat the page 'ne
160c8 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65  arby' is somewhe
160c9 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c  re on the free-l
160ca 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  ist, then.    **
160cb 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74   the entire-list
160cc 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65   will be searche
160cd 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e  d for that page.
160ce 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  .    */.#ifndef 
160cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
160d0 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65  VACUUM.    if( e
160d1 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d  xact && nearby<=
160d2 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
160d3 42 74 29 20 29 7b 0a 20 20 20 20 20 20 75 38 20  Bt) ){.      u8 
160d4 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61 73 73  eType;.      ass
160d5 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20 29 3b  ert( nearby>0 );
160d6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
160d7 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
160d8 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  ;.      rc = ptr
160d9 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65 61 72  mapGet(pBt, near
160da 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29 3b 0a  by, &eType, 0);.
160db 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
160dc 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
160dd 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
160de 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20  P_FREEPAGE ){.  
160df 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74        searchList
160e0 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
160e1 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 65 61      *pPgno = nea
160e2 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  rby;.    }.#endi
160e3 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d  f..    /* Decrem
160e4 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73  ent the free-lis
160e5 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65  t count by 1. Se
160e6 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20  t iTrunk to the 
160e7 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20  index of the.   
160e8 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c   ** first free-l
160e9 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20  ist trunk page. 
160ea 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e  iPrevTrunk is in
160eb 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a  itially 1..    *
160ec 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
160ed 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
160ee 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge1->pDbPage);. 
160ef 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
160f0 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 34 62  rn rc;.    put4b
160f1 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
160f2 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20  ta[36], n-1);.. 
160f3 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 77     /* The code w
160f4 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20  ithin this loop 
160f5 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65  is run only once
160f6 20 69 66 20 74 68 65 20 27 73 65 61 72 63 68 4c   if the 'searchL
160f7 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a 20 20  ist' variable.  
160f8 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 75 65    ** is not true
160f9 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20  . Otherwise, it 
160fa 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61  runs once for ea
160fb 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e  ch trunk-page on
160fc 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 65 65   the.    ** free
160fd 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 65 20  -list until the 
160fe 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 73  page 'nearby' is
160ff 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20 2a 2f   located..    */
16100 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20  .    do {.      
16101 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 54 72  pPrevTrunk = pTr
16102 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70  unk;.      if( p
16103 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20  PrevTrunk ){.   
16104 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65       iTrunk = ge
16105 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75  t4byte(&pPrevTru
16106 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20  nk->aData[0]);. 
16107 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
16108 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74      iTrunk = get
16109 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
1610a 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20  Data[32]);.     
1610b 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   }.      rc = sq
1610c 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
1610d 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26  e(pBt, iTrunk, &
1610e 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20  pTrunk, 0);.    
1610f 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
16110 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a      pTrunk = 0;.
16111 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
16112 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
16113 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 6b        }..      k
16114 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72   = get4byte(&pTr
16115 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a  unk->aData[4]);.
16116 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 26        if( k==0 &
16117 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 29 7b  & !searchList ){
16118 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
16119 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61  trunk has no lea
1611a 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 73 74  ves and the list
1611b 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 73 65   is not being se
1611c 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 20 20  arched. .       
1611d 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 20 74   ** So extract t
1611e 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 74  he trunk page it
1611f 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 74 20  self and use it 
16120 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a 20 20  as the newly .  
16121 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74        ** allocat
16122 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20  ed page */.     
16123 20 20 20 61 73 73 65 72 74 28 20 70 50 72 65 76     assert( pPrev
16124 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20  Trunk==0 );.    
16125 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16126 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
16127 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
16128 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
16129 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
1612a 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
1612b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1612c 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75     *pPgno = iTru
1612d 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  nk;.        memc
1612e 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  py(&pPage1->aDat
1612f 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  a[32], &pTrunk->
16130 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20  aData[0], 4);.  
16131 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20        *ppPage = 
16132 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20  pTrunk;.        
16133 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20  pTrunk = 0;.    
16134 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f      TRACE(("ALLO
16135 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d  CATE: %d trunk -
16136 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c   %d free pages l
16137 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20  eft\n", *pPgno, 
16138 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c  n-1));.      }el
16139 73 65 20 69 66 28 20 6b 3e 70 42 74 2d 3e 75 73  se if( k>pBt->us
1613a 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 29  ableSize/4 - 2 )
1613b 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c  {.        /* Val
1613c 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 74 20 6f  ue of k is out o
1613d 66 20 72 61 6e 67 65 2e 20 20 44 61 74 61 62 61  f range.  Databa
1613e 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f  se corruption */
1613f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
16140 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
16141 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  T;.        goto 
16142 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
16143 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e;.#ifndef SQLIT
16144 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
16145 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  M.      }else if
16146 28 20 73 65 61 72 63 68 4c 69 73 74 20 26 26 20  ( searchList && 
16147 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b 20 29  nearby==iTrunk )
16148 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
16149 20 6c 69 73 74 20 69 73 20 62 65 69 6e 67 20 73   list is being s
1614a 65 61 72 63 68 65 64 20 61 6e 64 20 74 68 69 73  earched and this
1614b 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 74   trunk page is t
1614c 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20  he page.        
1614d 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2c 20  ** to allocate, 
1614e 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
1614f 65 74 68 65 72 20 69 74 20 68 61 73 20 6c 65 61  ether it has lea
16150 76 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ves..        */.
16151 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
16152 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29  *pPgno==iTrunk )
16153 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67  ;.        *ppPag
16154 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20  e = pTrunk;.    
16155 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d      searchList =
16156 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   0;.        rc =
16157 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
16158 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61  te(pTrunk->pDbPa
16159 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ge);.        if(
1615a 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20   rc ){.         
1615b 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
1615c 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20  te_page;.       
1615d 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6b   }.        if( k
1615e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1615f 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b   if( !pPrevTrunk
16160 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
16161 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e  memcpy(&pPage1->
16162 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75  aData[32], &pTru
16163 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29  nk->aData[0], 4)
16164 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
16165 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d  e{.            m
16166 65 6d 63 70 79 28 26 70 50 72 65 76 54 72 75 6e  emcpy(&pPrevTrun
16167 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54  k->aData[0], &pT
16168 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20  runk->aData[0], 
16169 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  4);.          }.
1616a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1616b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1616c 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65  trunk page is re
1616d 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63 61  quired by the ca
1616e 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74  ller but it cont
1616f 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20 20 20  ains .          
16170 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66  ** pointers to f
16171 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e  ree-list leaves.
16172 20 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 20   The first leaf 
16173 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0a  becomes a trunk.
16174 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67            ** pag
16175 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  e in this case..
16176 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
16177 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a         MemPage *
16178 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 20  pNewTrunk;.     
16179 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 72       Pgno iNewTr
1617a 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26  unk = get4byte(&
1617b 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d  pTrunk->aData[8]
1617c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
1617d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
1617e 74 50 61 67 65 28 70 42 74 2c 20 69 4e 65 77 54  tPage(pBt, iNewT
1617f 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b  runk, &pNewTrunk
16180 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
16181 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16182 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
16183 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
16184 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20  te_page;.       
16185 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72     }.          r
16186 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
16187 57 72 69 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d  Write(pNewTrunk-
16188 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
16189 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1618a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1618b 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
1618c 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20  e(pNewTrunk);.  
1618d 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
1618e 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
1618f 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
16190 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
16191 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61  pNewTrunk->aData
16192 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44  [0], &pTrunk->aD
16193 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20  ata[0], 4);.    
16194 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
16195 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61  pNewTrunk->aData
16196 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20  [4], k-1);.     
16197 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65       memcpy(&pNe
16198 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d  wTrunk->aData[8]
16199 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  , &pTrunk->aData
1619a 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a  [12], (k-1)*4);.
1619b 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73            releas
1619c 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29  ePage(pNewTrunk)
1619d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1619e 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20  !pPrevTrunk ){. 
1619f 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
161a0 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
161a1 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
161a2 31 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  1->pDbPage) );. 
161a3 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62             put4b
161a4 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
161a5 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e  ta[32], iNewTrun
161a6 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  k);.          }e
161a7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
161a8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
161a9 65 72 57 72 69 74 65 28 70 50 72 65 76 54 72 75  erWrite(pPrevTru
161aa 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  nk->pDbPage);.  
161ab 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
161ac 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
161ad 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63    goto end_alloc
161ae 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20  ate_page;.      
161af 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
161b0 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50      put4byte(&pP
161b1 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  revTrunk->aData[
161b2 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a  0], iNewTrunk);.
161b3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
161b4 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 54      }.        pT
161b5 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20  runk = 0;.      
161b6 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
161b7 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25  TE: %d trunk - %
161b8 64 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 66  d free pages lef
161b9 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d  t\n", *pPgno, n-
161ba 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  1));.#endif.    
161bb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
161bc 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65   /* Extract a le
161bd 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e  af from the trun
161be 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74  k */.        int
161bf 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20   closest;.      
161c0 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20    Pgno iPage;.  
161c1 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63        unsigned c
161c2 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72  har *aData = pTr
161c3 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  unk->aData;.    
161c4 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
161c5 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
161c6 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
161c7 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
161c8 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
161c9 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
161ca 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
161cb 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20     if( nearby>0 
161cc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
161cd 20 69 2c 20 64 69 73 74 3b 0a 20 20 20 20 20 20   i, dist;.      
161ce 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b      closest = 0;
161cf 0a 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20  .          dist 
161d0 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74  = get4byte(&aDat
161d1 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a  a[8]) - nearby;.
161d2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64 69            if( di
161d3 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64  st<0 ) dist = -d
161d4 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66  ist;.          f
161d5 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b  or(i=1; i<k; i++
161d6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
161d7 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79 74 65  nt d2 = get4byte
161d8 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20  (&aData[8+i*4]) 
161d9 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20  - nearby;.      
161da 20 20 20 20 20 20 69 66 28 20 64 32 3c 30 20 29        if( d2<0 )
161db 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20   d2 = -d2;.     
161dc 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 64 69         if( d2<di
161dd 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  st ){.          
161de 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b      closest = i;
161df 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64  .              d
161e0 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20  ist = d2;.      
161e1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
161e2 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
161e3 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f  e{.          clo
161e4 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  sest = 0;.      
161e5 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61    }..        iPa
161e6 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61  ge = get4byte(&a
161e7 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34  Data[8+closest*4
161e8 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]);.        if( 
161e9 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69  !searchList || i
161ea 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a  Page==nearby ){.
161eb 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f            int no
161ec 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20  Content;.       
161ed 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 0a 20     Pgno nPage;. 
161ee 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20           *pPgno 
161ef 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20  = iPage;.       
161f0 20 20 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72     nPage = pager
161f1 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a  Pagecount(pBt);.
161f2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70            if( *p
161f3 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b 0a 20 20  Pgno>nPage ){.  
161f4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65            /* Fre
161f5 65 20 70 61 67 65 20 6f 66 66 20 74 68 65 20 65  e page off the e
161f6 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  nd of the file *
161f7 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  /.            rc
161f8 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
161f9 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20  T_BKPT;.        
161fa 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
161fb 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
161fc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
161fd 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
161fe 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20  TE: %d was leaf 
161ff 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e  %d of %d on trun
16200 6b 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20  k %d".          
16201 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72         ": %d mor
16202 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c  e free pages\n",
16203 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
16204 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73    *pPgno, closes
16205 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e  t+1, k, pTrunk->
16206 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20  pgno, n-1));.   
16207 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65         if( close
16208 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20  st<k-1 ){.      
16209 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44        memcpy(&aD
1620a 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d  ata[8+closest*4]
1620b 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c  , &aData[4+k*4],
1620c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   4);.          }
1620d 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62  .          put4b
1620e 79 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b  yte(&aData[4], k
1620f 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  -1);.          a
16210 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
16211 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
16212 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 20  Trunk->pDbPage) 
16213 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43  );.          noC
16214 6f 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47  ontent = !btreeG
16215 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74  etHasContent(pBt
16216 2c 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 20 20  , *pPgno);.     
16217 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
16218 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
16219 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67  t, *pPgno, ppPag
1621a 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20  e, noContent);. 
1621b 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d           if( rc=
1621c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1621d 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
1621e 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1621f 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50  ((*ppPage)->pDbP
16220 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
16221 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16222 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
16223 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
16224 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20  (*ppPage);.     
16225 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
16226 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73     }.          s
16227 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20  earchList = 0;. 
16228 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
16229 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1622a 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a  ge(pPrevTrunk);.
1622b 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b        pPrevTrunk
1622c 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65   = 0;.    }while
1622d 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a  ( searchList );.
1622e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1622f 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67  There are no pag
16230 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  es on the freeli
16231 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 20  st, so create a 
16232 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65 0a  new page at the.
16233 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68      ** end of th
16234 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 6e  e file */.    in
16235 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50  t nPage = pagerP
16236 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20  agecount(pBt);. 
16237 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 61 67     *pPgno = nPag
16238 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66 28 20  e + 1;..    if( 
16239 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f  *pPgno==PENDING_
1623a 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
1623b 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29  {.      (*pPgno)
1623c 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64  ++;.    }..#ifnd
1623d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1623e 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
1623f 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
16240 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50 41  m && PTRMAP_ISPA
16241 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29 20  GE(pBt, *pPgno) 
16242 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 2a  ){.      /* If *
16243 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f 20  pPgno refers to 
16244 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  a pointer-map pa
16245 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77 6f  ge, allocate two
16246 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20 20   new pages.     
16247 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f   ** at the end o
16248 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74 65  f the file inste
16249 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20 66  ad of one. The f
1624a 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 70  irst allocated p
1624b 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 63  age.      ** bec
1624c 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e 74  omes a new point
1624d 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68 65  er-map page, the
1624e 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64 20   second is used 
1624f 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20  by the caller.. 
16250 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4d 65       */.      Me
16251 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b 0a  mPage *pPg = 0;.
16252 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c        TRACE(("AL
16253 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20  LOCATE: %d from 
16254 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f 69  end of file (poi
16255 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c 6e  nter-map page)\n
16256 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 20  ", *pPgno));.   
16257 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e     assert( *pPgn
16258 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  o!=PENDING_BYTE_
16259 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20  PAGE(pBt) );.   
1625a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1625b 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
1625c 20 2a 70 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30   *pPgno, &pPg, 0
1625d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
1625e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1625f 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
16260 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67  e3PagerWrite(pPg
16261 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
16262 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
16263 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pPg);.      }.  
16264 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
16265 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 28 2a  urn rc;.      (*
16266 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20  pPgno)++;.      
16267 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44  if( *pPgno==PEND
16268 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
16269 74 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b  t) ){ (*pPgno)++
1626a 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ; }.    }.#endif
1626b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70  ..    assert( *p
1626c 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno!=PENDING_BY
1626d 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a  TE_PAGE(pBt) );.
1626e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1626f 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
16270 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65  , *pPgno, ppPage
16271 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
16272 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
16273 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
16274 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67  gerWrite((*ppPag
16275 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  e)->pDbPage);.  
16276 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16277 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c  _OK ){.      rel
16278 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65  easePage(*ppPage
16279 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 52 41  );.    }.    TRA
1627a 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25  CE(("ALLOCATE: %
1627b 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69  d from end of fi
1627c 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b  le\n", *pPgno));
1627d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
1627e 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f  *pPgno!=PENDING_
1627f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
16280 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  ;..end_allocate_
16281 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73 65 50  page:.  releaseP
16282 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72  age(pTrunk);.  r
16283 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76  eleasePage(pPrev
16284 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 20 72 63  Trunk);.  if( rc
16285 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
16286 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61     if( sqlite3Pa
16287 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
16288 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61  (*ppPage)->pDbPa
16289 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 72  ge)>1 ){.      r
1628a 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61  eleasePage(*ppPa
1628b 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ge);.      retur
1628c 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
1628d 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _BKPT;.    }.   
1628e 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 6e   (*ppPage)->isIn
1628f 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  it = 0;.  }.  re
16290 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
16291 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
16292 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70  is used to add p
16293 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65  age iPage to the
16294 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
16295 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74  ree-list. .** It
16296 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
16297 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
16298 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20   already a part 
16299 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  of the free-list
1629a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ..**.** The valu
1629b 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
1629c 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
1629d 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
1629e 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a   is optional..**
1629f 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68   If the caller h
162a0 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61  appens to have a
162a1 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
162a2 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a  MemPage object .
162a3 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
162a4 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68   to page iPage h
162a5 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73  andy, it may pas
162a6 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f  s it as the seco
162a7 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74  nd value. .** Ot
162a8 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20  herwise, it may 
162a9 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  pass NULL..**.**
162aa 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   If a pointer to
162ab 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63   a MemPage objec
162ac 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  t is passed as t
162ad 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
162ae 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72  nt,.** its refer
162af 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f  ence count is no
162b0 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69  t altered by thi
162b1 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  s function..*/.s
162b2 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61  tatic int freePa
162b3 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42  ge2(BtShared *pB
162b4 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d  t, MemPage *pMem
162b5 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65  Page, Pgno iPage
162b6 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54  ){.  MemPage *pT
162b7 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20  runk = 0;       
162b8 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65           /* Free
162b9 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
162ba 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e   */.  Pgno iTrun
162bb 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  k = 0;          
162bc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
162bd 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  e number of free
162be 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
162bf 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a   */ .  MemPage *
162c0 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50  pPage1 = pBt->pP
162c1 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  age1;      /* Lo
162c2 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f  cal reference to
162c3 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d   page 1 */.  Mem
162c4 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20  Page *pPage;    
162c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
162c6 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66   /* Page being f
162c7 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c  reed. May be NUL
162c8 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  L. */.  int rc; 
162c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
162ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
162cb 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20  eturn Code */.  
162cc 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20  int nFree;      
162cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
162ce 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e      /* Initial n
162cf 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f  umber of pages o
162d0 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a  n free-list */..
162d1 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
162d2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
162d3 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
162d4 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b  sert( iPage>1 );
162d5 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d  .  assert( !pMem
162d6 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65  Page || pMemPage
162d7 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b  ->pgno==iPage );
162d8 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65  ..  if( pMemPage
162d9 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20   ){.    pPage = 
162da 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71  pMemPage;.    sq
162db 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50  lite3PagerRef(pP
162dc 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
162dd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67   }else{.    pPag
162de 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f  e = btreePageLoo
162df 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b  kup(pBt, iPage);
162e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65  .  }..  /* Incre
162e1 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61  ment the free pa
162e2 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67  ge count on pPag
162e3 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  e1 */.  rc = sql
162e4 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
162e5 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b  Page1->pDbPage);
162e6 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f  .  if( rc ) goto
162e7 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20   freepage_out;. 
162e8 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74   nFree = get4byt
162e9 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
162ea 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74  [36]);.  put4byt
162eb 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
162ec 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a  [36], nFree+1);.
162ed 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
162ee 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f  ECURE_DELETE.  /
162ef 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f  * If the SQLITE_
162f0 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f  SECURE_DELETE co
162f1 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
162f2 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  n is enabled, th
162f3 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66  en.  ** always f
162f4 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64  ully overwrite d
162f5 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69  eleted informati
162f6 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20  on with zeros.. 
162f7 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67   */.  if( (!pPag
162f8 65 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74  e && (rc = sqlit
162f9 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
162fa 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 67  Bt, iPage, &pPag
162fb 65 2c 20 30 29 29 29 0a 20 20 20 7c 7c 20 20 20  e, 0))).   ||   
162fc 20 20 20 20 20 20 20 20 20 28 72 63 20 3d 20 73           (rc = s
162fd 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
162fe 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
162ff 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ).  ){.    goto 
16300 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
16301 7d 0a 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65  }.  memset(pPage
16302 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 67  ->aData, 0, pPag
16303 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e->pBt->pageSize
16304 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
16305 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
16306 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
16307 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e 20 65  cuum, write an e
16308 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e  ntry in the poin
16309 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20  ter-map.  ** to 
1630a 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68  indicate that th
1630b 65 20 70 61 67 65 20 69 73 20 66 72 65 65 2e 0a  e page is free..
1630c 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 54    */.  if( ISAUT
1630d 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 72  OVACUUM ){.    r
1630e 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42  c = ptrmapPut(pB
1630f 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50  t, iPage, PTRMAP
16310 5f 46 52 45 45 50 41 47 45 2c 20 30 29 3b 0a 20  _FREEPAGE, 0);. 
16311 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
16312 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20   freepage_out;. 
16313 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e   }..  /* Now man
16314 69 70 75 6c 61 74 65 20 74 68 65 20 61 63 74 75  ipulate the actu
16315 61 6c 20 64 61 74 61 62 61 73 65 20 66 72 65 65  al database free
16316 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72 65 2e  -list structure.
16317 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 0a 20   There are two. 
16318 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 69 65   ** possibilitie
16319 73 2e 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c  s. If the free-l
1631a 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ist is currently
1631b 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20 74 68   empty, or if th
1631c 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74 72 75  e first.  ** tru
1631d 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66  nk page in the f
1631e 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c  ree-list is full
1631f 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65  , then this page
16320 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 0a 20   will become a. 
16321 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c 69 73   ** new free-lis
16322 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 4f 74  t trunk page. Ot
16323 68 65 72 77 69 73 65 2c 20 69 74 20 77 69 6c 6c  herwise, it will
16324 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66 20 6f   become a leaf o
16325 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74  f the.  ** first
16326 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74   trunk page in t
16327 68 65 20 63 75 72 72 65 6e 74 20 66 72 65 65 2d  he current free-
16328 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f 63 6b  list. This block
16329 20 74 65 73 74 73 20 69 66 20 69 74 0a 20 20 2a   tests if it.  *
1632a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  * is possible to
1632b 20 61 64 64 20 74 68 65 20 70 61 67 65 20 61 73   add the page as
1632c 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 74   a new free-list
1632d 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 66   leaf..  */.  if
1632e 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a 20 20  ( nFree!=0 ){.  
1632f 20 20 69 6e 74 20 6e 4c 65 61 66 3b 20 20 20 20    int nLeaf;    
16330 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
16331 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66  nitial number of
16332 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74   leaf cells on t
16333 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 20  runk page */..  
16334 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62    iTrunk = get4b
16335 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
16336 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72 63 20  ta[32]);.    rc 
16337 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
16338 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e  tPage(pBt, iTrun
16339 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a  k, &pTrunk, 0);.
1633a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1633b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67  TE_OK ){.      g
1633c 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74  oto freepage_out
1633d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 65  ;.    }..    nLe
1633e 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  af = get4byte(&p
1633f 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29  Trunk->aData[4])
16340 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 3c  ;.    if( nLeaf<
16341 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
16342 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
16343 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
16344 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
16345 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61    }.    if( nLea
16346 66 3c 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  f<pBt->usableSiz
16347 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 20 20 20  e/4 - 8 ){.     
16348 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   /* In this case
16349 20 74 68 65 72 65 20 69 73 20 72 6f 6f 6d 20 6f   there is room o
1634a 6e 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65  n the trunk page
1634b 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 70   to insert the p
1634c 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 69  age.      ** bei
1634d 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6e 65  ng freed as a ne
1634e 77 20 6c 65 61 66 2e 0a 20 20 20 20 20 20 2a 2a  w leaf..      **
1634f 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74  .      ** Note t
16350 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61  hat the trunk pa
16351 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79  ge is not really
16352 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63   full until it c
16353 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a  ontains.      **
16354 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20   usableSize/4 - 
16355 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75  2 entries, not u
16356 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20  sableSize/4 - 8 
16357 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68 61  entries as we ha
16358 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 65  ve.      ** code
16359 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20 61  d.  But due to a
1635a 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 6e   coding error in
1635b 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
1635c 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 20  ite prior to.   
1635d 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 74     ** 3.6.0, dat
1635e 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65 65  abases with free
1635f 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 73  list trunk pages
16360 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 68   holding more th
16361 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62  an.      ** usab
16362 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74  leSize/4 - 8 ent
16363 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65 70  ries will be rep
16364 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70 74  orted as corrupt
16365 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 20  .  In order.    
16366 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 6e    ** to maintain
16367 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
16368 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f 6c  tibility with ol
16369 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  der versions of 
1636a 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a 2a  SQLite,.      **
1636b 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e   we will contain
1636c 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 65   to restrict the
1636d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
1636e 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a 65  es to usableSize
1636f 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a 20  /4 - 8.      ** 
16370 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f 6d  for now.  At som
16371 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66  e point in the f
16372 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 72  uture (once ever
16373 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 65  yone has upgrade
16374 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 2e  d.      ** to 3.
16375 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 65  6.0 or later) we
16376 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 72   should consider
16377 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 64   fixing the cond
16378 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 20  itional above.  
16379 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 22      ** to read "
1637a 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 20  usableSize/4-2" 
1637b 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 62  instead of "usab
1637c 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 20  leSize/4-8"..   
1637d 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d     */.      rc =
1637e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1637f 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61  te(pTrunk->pDbPa
16380 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
16381 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16382 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
16383 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
16384 34 5d 2c 20 6e 4c 65 61 66 2b 31 29 3b 0a 20 20  4], nLeaf+1);.  
16385 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
16386 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b  pTrunk->aData[8+
16387 6e 4c 65 61 66 2a 34 5d 2c 20 69 50 61 67 65 29  nLeaf*4], iPage)
16388 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
16389 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20  _SECURE_DELETE. 
1638a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
1638b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
1638c 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
1638d 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
1638e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23  ge);.        }.#
1638f 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 63  endif.        rc
16390 20 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f   = btreeSetHasCo
16391 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65  ntent(pBt, iPage
16392 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
16393 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41   TRACE(("FREE-PA
16394 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74  GE: %d leaf on t
16395 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c  runk page %d\n",
16396 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75  pPage->pgno,pTru
16397 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  nk->pgno));.    
16398 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f    goto freepage_
16399 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  out;.    }.  }..
1639a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20    /* If control 
1639b 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f  flows to this po
1639c 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73  int, then it was
1639d 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
1639e 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68   add the.  ** th
1639f 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65  e page being fre
163a0 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67  ed as a leaf pag
163a1 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74  e of the first t
163a2 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65  runk in the free
163a3 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73  -list..  ** Poss
163a4 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65  ibly because the
163a5 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d   free-list is em
163a6 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79  pty, or possibly
163a7 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20   because the .  
163a8 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69  ** first trunk i
163a9 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
163aa 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20  is full. Either 
163ab 77 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65  way, the page be
163ac 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77  ing freed.  ** w
163ad 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e  ill become the n
163ae 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70  ew first trunk p
163af 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d  age in the free-
163b0 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  list..  */.  if(
163b1 20 20 20 28 28 21 70 50 61 67 65 29 20 26 26 20     ((!pPage) && 
163b2 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69  (0 != (rc = sqli
163b3 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
163b4 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61  pBt, iPage, &pPa
163b5 67 65 2c 20 30 29 29 29 29 0a 20 20 20 20 20 7c  ge, 0)))).     |
163b6 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71  | (0 != (rc = sq
163b7 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
163b8 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29  pPage->pDbPage))
163b9 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ).  ){.    goto 
163ba 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
163bb 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 61  }.  put4byte(pPa
163bc 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e  ge->aData, iTrun
163bd 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26  k);.  put4byte(&
163be 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c  pPage->aData[4],
163bf 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28   0);.  put4byte(
163c0 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
163c1 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52  2], iPage);.  TR
163c2 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a  ACE(("FREE-PAGE:
163c3 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61   %d new trunk pa
163c4 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c  ge replacing %d\
163c5 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  n", pPage->pgno,
163c6 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65   iTrunk));..free
163c7 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20  page_out:.  if( 
163c8 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61  pPage ){.    pPa
163c9 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  ge->isInit = 0;.
163ca 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67    }.  releasePag
163cb 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65  e(pPage);.  rele
163cc 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b  asePage(pTrunk);
163cd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
163ce 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50  static int freeP
163cf 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
163d0 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 66 72  ge){.  return fr
163d1 65 65 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70  eePage2(pPage->p
163d2 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65  Bt, pPage, pPage
163d3 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->pgno);.}../*.*
163d4 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66  * Free any overf
163d5 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69  low pages associ
163d6 61 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69  ated with the gi
163d7 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  ven Cell..*/.sta
163d8 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c  tic int clearCel
163d9 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  l(MemPage *pPage
163da 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  , unsigned char 
163db 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61  *pCell){.  BtSha
163dc 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
163dd 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66  ->pBt;.  CellInf
163de 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f  o info;.  Pgno o
163df 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72  vflPgno;.  int r
163e0 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a  c;.  int nOvfl;.
163e1 20 20 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69    u16 ovflPageSi
163e2 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ze;..  assert( s
163e3 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
163e4 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
163e5 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
163e6 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
163e7 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
163e8 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69   &info);.  if( i
163e9 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30  nfo.iOverflow==0
163ea 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
163eb 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f  QLITE_OK;  /* No
163ec 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
163ed 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20   Return without 
163ee 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a  doing anything *
163ef 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f  /.  }.  ovflPgno
163f0 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65   = get4byte(&pCe
163f1 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
163f2 77 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  w]);.  assert( p
163f3 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3e  Bt->usableSize >
163f4 20 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65   4 );.  ovflPage
163f5 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62  Size = pBt->usab
163f6 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f  leSize - 4;.  nO
163f7 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79  vfl = (info.nPay
163f8 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63  load - info.nLoc
163f9 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a  al + ovflPageSiz
163fa 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53  e - 1)/ovflPageS
163fb 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  ize;.  assert( o
163fc 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f  vflPgno==0 || nO
163fd 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65  vfl>0 );.  while
163fe 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20  ( nOvfl-- ){.   
163ff 20 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 30 3b   Pgno iNext = 0;
16400 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f  .    MemPage *pO
16401 76 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  vfl = 0;.    if(
16402 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20   ovflPgno==0 || 
16403 6f 76 66 6c 50 67 6e 6f 3e 70 61 67 65 72 50 61  ovflPgno>pagerPa
16404 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a  gecount(pBt) ){.
16405 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
16406 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
16407 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
16408 6e 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 72  nOvfl ){.      r
16409 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50  c = getOverflowP
1640a 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e  age(pBt, ovflPgn
1640b 6f 2c 20 26 70 4f 76 66 6c 2c 20 26 69 4e 65 78  o, &pOvfl, &iNex
1640c 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  t);.      if( rc
1640d 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
1640e 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 72 65    }.    rc = fre
1640f 65 50 61 67 65 32 28 70 42 74 2c 20 70 4f 76 66  ePage2(pBt, pOvf
16410 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20  l, ovflPgno);.  
16411 20 20 69 66 28 20 70 4f 76 66 6c 20 29 7b 0a 20    if( pOvfl ){. 
16412 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
16413 72 55 6e 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44  rUnref(pOvfl->pD
16414 62 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  bPage);.    }.  
16415 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
16416 6e 20 72 63 3b 0a 20 20 20 20 6f 76 66 6c 50 67  n rc;.    ovflPg
16417 6e 6f 20 3d 20 69 4e 65 78 74 3b 0a 20 20 7d 0a  no = iNext;.  }.
16418 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
16419 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  OK;.}../*.** Cre
1641a 61 74 65 20 74 68 65 20 62 79 74 65 20 73 65 71  ate the byte seq
1641b 75 65 6e 63 65 20 75 73 65 64 20 74 6f 20 72 65  uence used to re
1641c 70 72 65 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f  present a cell o
1641d 6e 20 70 61 67 65 20 70 50 61 67 65 0a 2a 2a 20  n page pPage.** 
1641e 61 6e 64 20 77 72 69 74 65 20 74 68 61 74 20 62  and write that b
1641f 79 74 65 20 73 65 71 75 65 6e 63 65 20 69 6e 74  yte sequence int
16420 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72  o pCell[].  Over
16421 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 0a 2a  flow pages are.*
16422 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  * allocated and 
16423 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65 63  filled in as nec
16424 65 73 73 61 72 79 2e 20 20 54 68 65 20 63 61 6c  essary.  The cal
16425 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 0a 2a  ling procedure.*
16426 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  * is responsible
16427 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65   for making sure
16428 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63   sufficient spac
16429 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63  e has been alloc
1642a 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c  ated.** for pCel
1642b 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  l[]..**.** Note 
1642c 74 68 61 74 20 70 43 65 6c 6c 20 64 6f 65 73 20  that pCell does 
1642d 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 6e 65  not necessary ne
1642e 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  ed to point to t
1642f 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a  he pPage->aData.
16430 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20  ** area.  pCell 
16431 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73  might point to s
16432 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74  ome temporary st
16433 6f 72 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c  orage.  The cell
16434 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73   will.** be cons
16435 74 72 75 63 74 65 64 20 69 6e 20 74 68 69 73 20  tructed in this 
16436 74 65 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74  temporary area t
16437 68 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  hen copied into 
16438 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20  pPage->aData.** 
16439 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  later..*/.static
1643a 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28   int fillInCell(
1643b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
1643c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
1643d 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68    /* The page th
1643e 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
1643f 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  cell */.  unsign
16440 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20  ed char *pCell, 
16441 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70           /* Comp
16442 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65  lete text of the
16443 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74   cell */.  const
16444 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34   void *pKey, i64
16445 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65   nKey,    /* The
16446 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   key */.  const 
16447 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 6e 74 20  void *pData,int 
16448 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 68 65 20  nData,   /* The 
16449 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a  data */.  int nZ
1644a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  ero,            
1644b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
1644c 61 20 7a 65 72 6f 20 62 79 74 65 73 20 74 6f 20  a zero bytes to 
1644d 61 70 70 65 6e 64 20 74 6f 20 70 44 61 74 61 20  append to pData 
1644e 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65  */.  int *pnSize
1644f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16450 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 65 6c      /* Write cel
16451 6c 20 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29  l size here */.)
16452 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64  {.  int nPayload
16453 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 70 53  ;.  const u8 *pS
16454 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20  rc;.  int nSrc, 
16455 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 73 70 61  n, rc;.  int spa
16456 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67  ceLeft;.  MemPag
16457 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20  e *pOvfl = 0;.  
16458 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c 65  MemPage *pToRele
16459 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67  ase = 0;.  unsig
1645a 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72  ned char *pPrior
1645b 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
1645c 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42  r *pPayload;.  B
1645d 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1645e 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e  Page->pBt;.  Pgn
1645f 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a  o pgnoOvfl = 0;.
16460 20 20 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20    int nHeader;. 
16461 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
16462 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16463 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
16464 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
16465 20 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 67 65 20   );..  /* pPage 
16466 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  is not necessari
16467 6c 79 20 77 72 69 74 65 61 62 6c 65 20 73 69 6e  ly writeable sin
16468 63 65 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 62  ce pCell might b
16469 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a  e auxiliary.  **
1646a 20 62 75 66 66 65 72 20 73 70 61 63 65 20 74 68   buffer space th
1646b 61 74 20 69 73 20 73 65 70 61 72 61 74 65 20 66  at is separate f
1646c 72 6f 6d 20 74 68 65 20 70 50 61 67 65 20 62 75  rom the pPage bu
1646d 66 66 65 72 20 61 72 65 61 20 2a 2f 0a 20 20 61  ffer area */.  a
1646e 73 73 65 72 74 28 20 70 43 65 6c 6c 3c 70 50 61  ssert( pCell<pPa
1646f 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 43 65  ge->aData || pCe
16470 6c 6c 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74  ll>=&pPage->aDat
16471 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d  a[pBt->pageSize]
16472 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
16473 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
16474 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
16475 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a  DbPage) );..  /*
16476 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 68 65 61   Fill in the hea
16477 64 65 72 2e 20 2a 2f 0a 20 20 6e 48 65 61 64 65  der. */.  nHeade
16478 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 21 70 50  r = 0;.  if( !pP
16479 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
1647a 20 6e 48 65 61 64 65 72 20 2b 3d 20 34 3b 0a 20   nHeader += 4;. 
1647b 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e   }.  if( pPage->
1647c 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 6e  hasData ){.    n
1647d 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72  Header += putVar
1647e 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64  int(&pCell[nHead
1647f 65 72 5d 2c 20 6e 44 61 74 61 2b 6e 5a 65 72 6f  er], nData+nZero
16480 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
16481 6e 44 61 74 61 20 3d 20 6e 5a 65 72 6f 20 3d 20  nData = nZero = 
16482 30 3b 0a 20 20 7d 0a 20 20 6e 48 65 61 64 65 72  0;.  }.  nHeader
16483 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70   += putVarint(&p
16484 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 2a  Cell[nHeader], *
16485 28 75 36 34 2a 29 26 6e 4b 65 79 29 3b 0a 20 20  (u64*)&nKey);.  
16486 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
16487 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20  eCellPtr(pPage, 
16488 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20  pCell, &info);. 
16489 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 48   assert( info.nH
1648a 65 61 64 65 72 3d 3d 6e 48 65 61 64 65 72 20 29  eader==nHeader )
1648b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f  ;.  assert( info
1648c 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 29 3b 0a 20  .nKey==nKey );. 
1648d 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 44   assert( info.nD
1648e 61 74 61 3d 3d 28 75 33 32 29 28 6e 44 61 74 61  ata==(u32)(nData
1648f 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 20 0a 20 20  +nZero) );.  .  
16490 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70  /* Fill in the p
16491 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 6e 50 61 79  ayload */.  nPay
16492 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20 2b 20 6e  load = nData + n
16493 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70 50 61 67  Zero;.  if( pPag
16494 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  e->intKey ){.   
16495 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20   pSrc = pData;. 
16496 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b     nSrc = nData;
16497 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a  .    nData = 0;.
16498 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20 69 66    }else{ .    if
16499 28 20 6e 4b 65 79 3e 30 78 37 66 66 66 66 66 66  ( nKey>0x7ffffff
1649a 66 20 7c 7c 20 70 4b 65 79 3d 3d 30 20 29 7b 0a  f || pKey==0 ){.
1649b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1649c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20  ITE_CORRUPT;.   
1649d 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20   }.    nPayload 
1649e 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20  += (int)nKey;.  
1649f 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20    pSrc = pKey;. 
164a0 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e     nSrc = (int)n
164a1 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69  Key;.  }.  *pnSi
164a2 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b  ze = info.nSize;
164a3 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69  .  spaceLeft = i
164a4 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50  nfo.nLocal;.  pP
164a5 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b  ayload = &pCell[
164a6 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69  nHeader];.  pPri
164a7 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f  or = &pCell[info
164a8 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20  .iOverflow];..  
164a9 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e  while( nPayload>
164aa 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61  0 ){.    if( spa
164ab 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66  ceLeft==0 ){.#if
164ac 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
164ad 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
164ae 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61    Pgno pgnoPtrma
164af 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a  p = pgnoOvfl; /*
164b0 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70   Overflow page p
164b1 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79  ointer-map entry
164b2 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69   page */.      i
164b3 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
164b4 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f  um ){.        do
164b5 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f  {.          pgno
164b6 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20  Ovfl++;.        
164b7 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20  } while( .      
164b8 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47      PTRMAP_ISPAG
164b9 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29  E(pBt, pgnoOvfl)
164ba 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45   || pgnoOvfl==PE
164bb 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
164bc 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b  pBt) .        );
164bd 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
164be 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63        rc = alloc
164bf 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
164c0 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f  , &pOvfl, &pgnoO
164c1 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30  vfl, pgnoOvfl, 0
164c2 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
164c3 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
164c4 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  M.      /* If th
164c5 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
164c6 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c  rts auto-vacuum,
164c7 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
164c8 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20  or subsequent.  
164c9 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20      ** overflow 
164ca 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c  page is being al
164cb 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20  located, add an 
164cc 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69  entry to the poi
164cd 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a  nter-map.      *
164ce 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 20  * for that page 
164cf 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20  now. .      **. 
164d0 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20       ** If this 
164d1 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 65  is the first ove
164d2 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e  rflow page, then
164d3 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 6c   write a partial
164d4 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a   entry .      **
164d5 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   to the pointer-
164d6 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 65  map. If we write
164d7 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73   nothing to this
164d8 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f   pointer-map slo
164d9 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  t,.      ** then
164da 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20   the optimistic 
164db 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70  overflow chain p
164dc 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65  rocessing in cle
164dd 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a  arCell().      *
164de 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72  * may misinterpr
164df 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c  et the uninitial
164e0 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20  ised values and 
164e1 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20  delete the.     
164e2 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20   ** wrong pages 
164e3 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
164e4 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
164e5 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
164e6 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c  acuum && rc==SQL
164e7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
164e8 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67    u8 eType = (pg
164e9 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f  noPtrmap?PTRMAP_
164ea 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50  OVERFLOW2:PTRMAP
164eb 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20  _OVERFLOW1);.   
164ec 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
164ed 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66  Put(pBt, pgnoOvf
164ee 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74  l, eType, pgnoPt
164ef 72 6d 61 70 29 3b 0a 20 20 20 20 20 20 20 20 69  rmap);.        i
164f0 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
164f1 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
164f2 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d  Ovfl);.        }
164f3 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
164f4 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
164f5 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
164f6 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b  age(pToRelease);
164f7 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
164f8 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rc;.      }..   
164f9 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65     /* If pToRele
164fa 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20  ase is not zero 
164fb 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e  than pPrior poin
164fc 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ts into the data
164fd 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f   area.      ** o
164fe 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d  f pToRelease.  M
164ff 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65  ake sure pToRele
16500 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69  ase is still wri
16501 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20  teable. */.     
16502 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65   assert( pToRele
16503 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ase==0 || sqlite
16504 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
16505 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44  e(pToRelease->pD
16506 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20  bPage) );..     
16507 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73   /* If pPrior is
16508 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
16509 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c  a area of pPage,
1650a 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20   then make sure 
1650b 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69  pPage.      ** i
1650c 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c  s still writeabl
1650d 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  e */.      asser
1650e 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d  t( pPrior<pPage-
1650f 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72  >aData || pPrior
16510 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  >=&pPage->aData[
16511 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20  pBt->pageSize]. 
16512 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71             || sq
16513 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
16514 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
16515 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20  Page) );..      
16516 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c  put4byte(pPrior,
16517 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20   pgnoOvfl);.    
16518 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
16519 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20  oRelease);.     
1651a 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f   pToRelease = pO
1651b 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f  vfl;.      pPrio
1651c 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61  r = pOvfl->aData
1651d 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  ;.      put4byte
1651e 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20  (pPrior, 0);.   
1651f 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70     pPayload = &p
16520 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a  Ovfl->aData[4];.
16521 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20        spaceLeft 
16522 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
16523 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20  e - 4;.    }.   
16524 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20   n = nPayload;. 
16525 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65     if( n>spaceLe
16526 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65  ft ) n = spaceLe
16527 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70  ft;..    /* If p
16528 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74  ToRelease is not
16529 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c   zero than pPayl
1652a 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20  oad points into 
1652b 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20  the data area.  
1652c 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61    ** of pToRelea
1652d 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70  se.  Make sure p
1652e 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69  ToRelease is sti
1652f 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f  ll writeable. */
16530 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f  .    assert( pTo
16531 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71  Release==0 || sq
16532 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
16533 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65  eable(pToRelease
16534 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20  ->pDbPage) );.. 
16535 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61     /* If pPayloa
16536 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65  d is part of the
16537 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 50   data area of pP
16538 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73  age, then make s
16539 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a  ure pPage.    **
1653a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61   is still writea
1653b 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ble */.    asser
1653c 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67  t( pPayload<pPag
1653d 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79  e->aData || pPay
1653e 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44  load>=&pPage->aD
1653f 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a  ata[pBt->pageSiz
16540 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  e].            |
16541 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  | sqlite3PagerIs
16542 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
16543 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20  >pDbPage) );..  
16544 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a    if( nSrc>0 ){.
16545 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63        if( n>nSrc
16546 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20   ) n = nSrc;.   
16547 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20     assert( pSrc 
16548 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
16549 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20  pPayload, pSrc, 
1654a 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  n);.    }else{. 
1654b 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79       memset(pPay
1654c 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20  load, 0, n);.   
1654d 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20   }.    nPayload 
1654e 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f  -= n;.    pPaylo
1654f 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72  ad += n;.    pSr
16550 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63  c += n;.    nSrc
16551 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65   -= n;.    space
16552 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69  Left -= n;.    i
16553 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20  f( nSrc==0 ){.  
16554 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61      nSrc = nData
16555 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70  ;.      pSrc = p
16556 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Data;.    }.  }.
16557 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
16558 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74  oRelease);.  ret
16559 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1655a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  ../*.** Remove t
1655b 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f  he i-th cell fro
1655c 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72  m pPage.  This r
1655d 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70  outine effects p
1655e 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68  Page only..** Th
1655f 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  e cell content i
16560 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64  s not freed or d
16561 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20  eallocated.  It 
16562 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a  is assumed that.
16563 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  ** the cell cont
16564 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70  ent has been cop
16565 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c  ied someplace el
16566 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  se.  This routin
16567 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65  e just.** remove
16568 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  s the reference 
16569 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d  to the cell from
1656a 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73   pPage..**.** "s
1656b 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e  z" must be the n
1656c 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1656d 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73  n the cell..*/.s
1656e 74 61 74 69 63 20 69 6e 74 20 64 72 6f 70 43 65  tatic int dropCe
1656f 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ll(MemPage *pPag
16570 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20  e, int idx, int 
16571 73 7a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  sz){.  int i;   
16572 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
16573 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
16574 70 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  pc;         /* O
16575 66 66 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f  ffset to cell co
16576 6e 74 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65  ntent of cell be
16577 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20  ing deleted */. 
16578 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20   u8 *data;      
16579 20 2f 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61   /* pPage->aData
1657a 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20   */.  u8 *ptr;  
1657b 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
1657c 20 6d 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75   move bytes arou
1657d 6e 64 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d  nd within data[]
1657e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
1657f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74        /* The ret
16580 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 61  urn code */..  a
16581 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26  ssert( idx>=0 &&
16582 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c   idx<pPage->nCel
16583 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  l );.  assert( s
16584 7a 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67  z==cellSize(pPag
16585 65 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73  e, idx) );.  ass
16586 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
16587 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
16588 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
16589 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1658a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
1658b 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
1658c 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67  );.  data = pPag
1658d 65 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20  e->aData;.  ptr 
1658e 3d 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63  = &data[pPage->c
1658f 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64  ellOffset + 2*id
16590 78 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62  x];.  pc = get2b
16591 79 74 65 28 70 74 72 29 3b 0a 20 20 69 66 28 20  yte(ptr);.  if( 
16592 28 70 63 3c 70 50 61 67 65 2d 3e 68 64 72 4f 66  (pc<pPage->hdrOf
16593 66 73 65 74 2b 36 2b 28 70 50 61 67 65 2d 3e 6c  fset+6+(pPage->l
16594 65 61 66 3f 30 3a 34 29 29 0a 20 20 20 20 20 7c  eaf?0:4)).     |
16595 7c 20 28 70 63 2b 73 7a 3e 70 50 61 67 65 2d 3e  | (pc+sz>pPage->
16596 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29  pBt->usableSize)
16597 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16598 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
16599 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66  PT;.  }.  rc = f
1659a 72 65 65 53 70 61 63 65 28 70 50 61 67 65 2c 20  reeSpace(pPage, 
1659b 70 63 2c 20 73 7a 29 3b 0a 20 20 69 66 28 20 72  pc, sz);.  if( r
1659c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1659d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1659e 20 7d 0a 20 20 66 6f 72 28 69 3d 69 64 78 2b 31   }.  for(i=idx+1
1659f 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ; i<pPage->nCell
165a0 3b 20 69 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a  ; i++, ptr+=2){.
165a1 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 74 72      ptr[0] = ptr
165a2 5b 32 5d 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20  [2];.    ptr[1] 
165a3 3d 20 70 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20  = ptr[3];.  }.  
165a4 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a  pPage->nCell--;.
165a5 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
165a6 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
165a7 74 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65  t+3], pPage->nCe
165a8 6c 6c 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46  ll);.  pPage->nF
165a9 72 65 65 20 2b 3d 20 32 3b 0a 20 20 72 65 74 75  ree += 2;.  retu
165aa 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
165ab 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
165ac 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67  new cell on pPag
165ad 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20  e at cell index 
165ae 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e  "i".  pCell poin
165af 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e  ts to the.** con
165b0 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  tent of the cell
165b1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  ..**.** If the c
165b2 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c  ell content will
165b3 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65   fit on the page
165b4 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74 68  , then put it th
165b5 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77  ere.  If it.** w
165b6 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65  ill not fit, the
165b7 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  n make a copy of
165b8 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
165b9 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a  t into pTemp if.
165ba 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20  ** pTemp is not 
165bb 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73  null.  Regardles
165bc 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f  s of pTemp, allo
165bd 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79  cate a new entry
165be 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 4f  .** in pPage->aO
165bf 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69  vfl[] and make i
165c0 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63  t point to the c
165c1 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74  ell content (eit
165c2 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20  her.** in pTemp 
165c3 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  or the original 
165c4 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20  pCell) and also 
165c5 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78  record its index
165c6 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67  . .** Allocating
165c7 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20   a new entry in 
165c8 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69  pPage->aCell[] i
165c9 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20  mplies that .** 
165ca 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
165cb 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e   is incremented.
165cc 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 20  .**.** If nSkip 
165cd 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
165ce 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68  n do not copy th
165cf 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79  e first nSkip by
165d0 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 65  tes of the.** ce
165d1 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77  ll. The caller w
165d2 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68  ill overwrite th
165d3 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 66 75  em after this fu
165d4 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20  nction returns. 
165d5 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 6e  If.** nSkip is n
165d6 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 43  on-zero, then pC
165d7 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 6e  ell may not poin
165d8 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20  t to an invalid 
165d9 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  memory location 
165da 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e  .** (but pCell+n
165db 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 20 76  Skip is always v
165dc 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  alid)..*/.static
165dd 20 69 6e 74 20 69 6e 73 65 72 74 43 65 6c 6c 28   int insertCell(
165de 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
165df 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 6e 74  e,   /* Page int
165e0 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 63  o which we are c
165e1 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  opying */.  int 
165e2 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  i,            /*
165e3 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f 6d 65   New cell become
165e4 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20  s the i-th cell 
165e5 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20  of the page */. 
165e6 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20   u8 *pCell,     
165e7 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
165e8 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 2a 2f   the new cell */
165e9 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20 20 20  .  int sz,      
165ea 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66       /* Bytes of
165eb 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43 65 6c   content in pCel
165ec 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65 6d 70  l */.  u8 *pTemp
165ed 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70  ,        /* Temp
165ee 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 66   storage space f
165ef 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e 65 65  or pCell, if nee
165f0 64 65 64 20 2a 2f 0a 20 20 75 38 20 6e 53 6b 69  ded */.  u8 nSki
165f1 70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  p          /* Do
165f2 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20 66   not write the f
165f3 69 72 73 74 20 6e 53 6b 69 70 20 62 79 74 65 73  irst nSkip bytes
165f4 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a   of the cell */.
165f5 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20  ){.  int idx;   
165f6 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
165f7 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63 65 6c  to write new cel
165f8 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74  l content in dat
165f9 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20  a[] */.  int j; 
165fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
165fb 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
165fc 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 20  int top;        
165fd 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
165fe 6f 66 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 61  of content for a
165ff 6e 79 20 63 65 6c 6c 20 69 6e 20 64 61 74 61 5b  ny cell in data[
16600 5d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20  ] */.  int end; 
16601 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
16602 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20  t byte past the 
16603 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  last cell pointe
16604 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20  r in data[] */. 
16605 20 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20   int ins;       
16606 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64     /* Index in d
16607 61 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20  ata[] where new 
16608 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20  cell pointer is 
16609 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e  inserted */.  in
1660a 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20  t hdr;          
1660b 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 64  /* Offset into d
1660c 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 70 61 67  ata[] of the pag
1660d 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e  e header */.  in
1660e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20  t cellOffset;   
1660f 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 66 69  /* Address of fi
16610 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  rst cell pointer
16611 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20   in data[] */.  
16612 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20  u8 *data;       
16613 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74    /* The content
16614 20 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61   of the whole pa
16615 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b  ge */.  u8 *ptr;
16616 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
16617 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66  d for moving inf
16618 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20  ormation around 
16619 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20  in data[] */..  
1661a 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
1661b 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b  i<=pPage->nCell+
1661c 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
1661d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
1661e 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43  age->nCell<=MX_C
1661f 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20  ELL(pPage->pBt) 
16620 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65  && MX_CELL(pPage
16621 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a  ->pBt)<=5460 );.
16622 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
16623 3e 6e 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61  >nOverflow<=Arra
16624 79 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 4f 76  ySize(pPage->aOv
16625 66 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  fl) );.  assert(
16626 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72   sz==cellSizePtr
16627 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29  (pPage, pCell) )
16628 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
16629 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1662a 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
1662b 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ) );.  if( pPage
1662c 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73  ->nOverflow || s
1662d 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65  z+2>pPage->nFree
1662e 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d   ){.    if( pTem
1662f 70 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  p ){.      memcp
16630 79 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70  y(pTemp+nSkip, p
16631 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e  Cell+nSkip, sz-n
16632 53 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65  Skip);.      pCe
16633 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20  ll = pTemp;.    
16634 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d  }.    j = pPage-
16635 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20  >nOverflow++;.  
16636 20 20 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74    assert( j<(int
16637 29 28 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e  )(sizeof(pPage->
16638 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50  aOvfl)/sizeof(pP
16639 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20  age->aOvfl[0])) 
1663a 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f  );.    pPage->aO
1663b 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70  vfl[j].pCell = p
1663c 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d  Cell;.    pPage-
1663d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20  >aOvfl[j].idx = 
1663e 28 75 31 36 29 69 3b 0a 20 20 20 20 70 50 61 67  (u16)i;.    pPag
1663f 65 2d 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20  e->nFree = 0;.  
16640 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72  }else{.    int r
16641 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
16642 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
16643 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
16644 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
16645 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
16646 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
16647 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
16648 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
16649 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
1664a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e    data = pPage->
1664b 61 44 61 74 61 3b 0a 20 20 20 20 68 64 72 20 3d  aData;.    hdr =
1664c 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
1664d 74 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74  t;.    top = get
1664e 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
1664f 35 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66  5]);.    cellOff
16650 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c  set = pPage->cel
16651 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 65 6e 64  lOffset;.    end
16652 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20   = cellOffset + 
16653 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 2b  2*pPage->nCell +
16654 20 32 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63 65   2;.    ins = ce
16655 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a  llOffset + 2*i;.
16656 20 20 20 20 69 66 28 20 65 6e 64 20 3e 20 74 6f      if( end > to
16657 70 20 2d 20 73 7a 20 29 7b 0a 20 20 20 20 20 20  p - sz ){.      
16658 72 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50  rc = defragmentP
16659 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
1665a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1665b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
1665c 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
1665d 7d 0a 20 20 20 20 20 20 74 6f 70 20 3d 20 67 65  }.      top = ge
1665e 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
1665f 2b 35 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65  +5]);.      asse
16660 72 74 28 20 65 6e 64 20 2b 20 73 7a 20 3c 3d 20  rt( end + sz <= 
16661 74 6f 70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  top );.    }.   
16662 20 69 64 78 20 3d 20 61 6c 6c 6f 63 61 74 65 53   idx = allocateS
16663 70 61 63 65 28 70 50 61 67 65 2c 20 73 7a 29 3b  pace(pPage, sz);
16664 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78  .    assert( idx
16665 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
16666 28 20 65 6e 64 20 3c 3d 20 67 65 74 32 62 79 74  ( end <= get2byt
16667 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20  e(&data[hdr+5]) 
16668 29 3b 0a 20 20 20 20 69 66 20 28 69 64 78 2b 73  );.    if (idx+s
16669 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e  z > pPage->pBt->
1666a 75 73 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 20  usableSize) {.  
1666b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1666c 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1666d 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d      }.    pPage-
1666e 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50  >nCell++;.    pP
1666f 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 3b  age->nFree -= 2;
16670 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74  .    memcpy(&dat
16671 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43  a[idx+nSkip], pC
16672 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53  ell+nSkip, sz-nS
16673 6b 69 70 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  kip);.    for(j=
16674 65 6e 64 2d 32 2c 20 70 74 72 3d 26 64 61 74 61  end-2, ptr=&data
16675 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32  [j]; j>ins; j-=2
16676 2c 20 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20  , ptr-=2){.     
16677 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32   ptr[0] = ptr[-2
16678 5d 3b 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20  ];.      ptr[1] 
16679 3d 20 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d  = ptr[-1];.    }
1667a 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64  .    put2byte(&d
1667b 61 74 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a  ata[ins], idx);.
1667c 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
1667d 74 61 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65  ta[hdr+3], pPage
1667e 2d 3e 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65  ->nCell);.#ifnde
1667f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
16680 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
16681 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74   pPage->pBt->aut
16682 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
16683 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61 79   /* The cell may
16684 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74   contain a point
16685 65 72 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f  er to an overflo
16686 77 20 70 61 67 65 2e 20 49 66 20 73 6f 2c 20 77  w page. If so, w
16687 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68  rite.      ** th
16688 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  e entry for the 
16689 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e  overflow page in
1668a 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  to the pointer m
1668b 61 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ap..      */.   
1668c 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f     CellInfo info
1668d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
1668e 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
1668f 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26  (pPage, pCell, &
16690 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 61 73 73  info);.      ass
16691 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61  ert( (info.nData
16692 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f  +(pPage->intKey?
16693 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69  0:info.nKey))==i
16694 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a  nfo.nPayload );.
16695 20 20 20 20 20 20 69 66 28 20 28 69 6e 66 6f 2e        if( (info.
16696 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e  nData+(pPage->in
16697 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79  tKey?0:info.nKey
16698 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29  ))>info.nLocal )
16699 7b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 70  {.        Pgno p
1669a 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79  gnoOvfl = get4by
1669b 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  te(&pCell[info.i
1669c 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20  Overflow]);.    
1669d 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50      rc = ptrmapP
1669e 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70  ut(pPage->pBt, p
1669f 67 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f  gnoOvfl, PTRMAP_
166a0 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65  OVERFLOW1, pPage
166a1 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
166a2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
166a3 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
166a4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
166a5 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ndif.  }..  retu
166a6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
166a7 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 73  ./*.** Add a lis
166a8 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 20  t of cells to a 
166a9 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20  page.  The page 
166aa 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61  should be initia
166ab 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 68  lly empty..** Th
166ac 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61 72  e cells are guar
166ad 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f 6e  anteed to fit on
166ae 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74   the page..*/.st
166af 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62  atic void assemb
166b0 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67  lePage(.  MemPag
166b1 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54  e *pPage,   /* T
166b2 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61 73  he page to be as
166b3 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 6e  semblied */.  in
166b4 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20  t nCell,        
166b5 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  /* The number of
166b6 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f   cells to add to
166b7 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20   this page */.  
166b8 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20  u8 **apCell,    
166b9 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f    /* Pointers to
166ba 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f 0a   cell bodies */.
166bb 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20 20    u16 *aSize    
166bc 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20      /* Sizes of 
166bd 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a  the cells */.){.
166be 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
166bf 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
166c0 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 74  ter */.  int tot
166c1 61 6c 53 69 7a 65 3b 20 20 20 20 2f 2a 20 54 6f  alSize;    /* To
166c2 74 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20  tal size of all 
166c3 63 65 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 68  cells */.  int h
166c4 64 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dr;          /* 
166c5 49 6e 64 65 78 20 6f 66 20 70 61 67 65 20 68 65  Index of page he
166c6 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65  ader */.  int ce
166c7 6c 6c 70 74 72 3b 20 20 20 20 20 20 2f 2a 20 41  llptr;      /* A
166c8 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63  ddress of next c
166c9 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ell pointer */. 
166ca 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20   int cellbody;  
166cb 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
166cc 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20   next cell body 
166cd 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20  */.  u8 *data;  
166ce 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66         /* Data f
166cf 6f 72 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a  or the page */..
166d0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
166d1 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b  >nOverflow==0 );
166d2 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
166d3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
166d4 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
166d5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43   );.  assert( nC
166d6 65 6c 6c 3e 3d 30 20 26 26 20 6e 43 65 6c 6c 3c  ell>=0 && nCell<
166d7 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e  =MX_CELL(pPage->
166d8 70 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28  pBt) && MX_CELL(
166d9 70 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36  pPage->pBt)<=546
166da 30 20 29 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65  0 );.  totalSize
166db 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   = 0;.  for(i=0;
166dc 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a   i<nCell; i++){.
166dd 20 20 20 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d      totalSize +=
166de 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 7d 0a 20   aSize[i];.  }. 
166df 20 61 73 73 65 72 74 28 20 74 6f 74 61 6c 53 69   assert( totalSi
166e0 7a 65 2b 32 2a 6e 43 65 6c 6c 3c 3d 70 50 61 67  ze+2*nCell<=pPag
166e1 65 2d 3e 6e 46 72 65 65 20 29 3b 0a 20 20 61 73  e->nFree );.  as
166e2 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65  sert( pPage->nCe
166e3 6c 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ll==0 );.  asser
166e4 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
166e5 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
166e6 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
166e7 63 65 6c 6c 70 74 72 20 3d 20 70 50 61 67 65 2d  cellptr = pPage-
166e8 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 64  >cellOffset;.  d
166e9 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
166ea 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  ta;.  hdr = pPag
166eb 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
166ec 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  put2byte(&data[h
166ed 64 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20  dr+3], nCell);. 
166ee 20 69 66 28 20 6e 43 65 6c 6c 20 29 7b 0a 20 20   if( nCell ){.  
166ef 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 61 6c 6c    cellbody = all
166f0 6f 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65  ocateSpace(pPage
166f1 2c 20 74 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20  , totalSize);.  
166f2 20 20 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f    assert( cellbo
166f3 64 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  dy>0 );.    asse
166f4 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  rt( pPage->nFree
166f5 20 3e 3d 20 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20   >= 2*nCell );. 
166f6 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20     pPage->nFree 
166f7 2d 3d 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 20 20  -= 2*nCell;.    
166f8 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c  for(i=0; i<nCell
166f9 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 75  ; i++){.      pu
166fa 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c  t2byte(&data[cel
166fb 6c 70 74 72 5d 2c 20 63 65 6c 6c 62 6f 64 79 29  lptr], cellbody)
166fc 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ;.      memcpy(&
166fd 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20  data[cellbody], 
166fe 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65  apCell[i], aSize
166ff 5b 69 5d 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c  [i]);.      cell
16700 70 74 72 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20  ptr += 2;.      
16701 63 65 6c 6c 62 6f 64 79 20 2b 3d 20 61 53 69 7a  cellbody += aSiz
16702 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  e[i];.    }.    
16703 61 73 73 65 72 74 28 20 63 65 6c 6c 62 6f 64 79  assert( cellbody
16704 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  ==pPage->pBt->us
16705 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a  ableSize );.  }.
16706 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d    pPage->nCell =
16707 20 28 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a   (u16)nCell;.}..
16708 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
16709 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 64  ing parameters d
1670a 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e  etermine how man
1670b 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73  y adjacent pages
1670c 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a   get involved.**
1670d 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20   in a balancing 
1670e 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69  operation.  NN i
1670f 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
16710 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74  neighbors on eit
16711 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74  her side.** of t
16712 68 65 20 70 61 67 65 20 74 68 61 74 20 70 61 72  he page that par
16713 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20  ticipate in the 
16714 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74  balancing operat
16715 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a  ion.  NB is the.
16716 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  ** total number 
16717 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 70 61  of pages that pa
16718 72 74 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75  rticipate, inclu
16719 64 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 20  ding the target 
1671a 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e  page and.** NN n
1671b 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68  eighbors on eith
1671c 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54  er side..**.** T
1671d 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65  he minimum value
1671e 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20   of NN is 1 (of 
1671f 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61  course).  Increa
16720 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a  sing NN above 1.
16721 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67  ** (to 2 or 3) g
16722 69 76 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d  ives a modest im
16723 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c  provement in SEL
16724 45 43 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70  ECT and DELETE p
16725 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e  erformance.** in
16726 20 65 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20   exchange for a 
16727 6c 61 72 67 65 72 20 64 65 67 72 61 64 61 74 69  larger degradati
16728 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64  on in INSERT and
16729 20 55 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61   UPDATE performa
1672a 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75  nce..** The valu
1672b 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20  e of NN appears 
1672c 74 6f 20 67 69 76 65 20 74 68 65 20 62 65 73 74  to give the best
1672d 20 72 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c   results overall
1672e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20  ..*/.#define NN 
1672f 31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  1             /*
16730 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68   Number of neigh
16731 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73  bors on either s
16732 69 64 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a  ide of pPage */.
16733 23 64 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32  #define NB (NN*2
16734 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61  +1)      /* Tota
16735 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64  l pages involved
16736 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20   in the balance 
16737 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72  */../* Forward r
16738 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74  eference */.stat
16739 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42  ic int balance(B
1673a 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 29 3b 0a  tCursor*, int);.
1673b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1673c 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43  OMIT_QUICKBALANC
1673d 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  E./*.** This ver
1673e 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65 28  sion of balance(
1673f 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63 6f  ) handles the co
16740 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61 73  mmon special cas
16741 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65 77  e where.** a new
16742 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67 20   entry is being 
16743 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65 20  inserted on the 
16744 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65 6e  extreme right-en
16745 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65 65  d of the.** tree
16746 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  , in other words
16747 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20 65  , when the new e
16748 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d 65  ntry will become
16749 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20   the largest.** 
1674a 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 65  entry in the tre
1674b 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64  e..**.** Instead
1674c 20 6f 66 20 74 72 79 69 6e 67 20 62 61 6c 61 6e   of trying balan
1674d 63 65 20 74 68 65 20 33 20 72 69 67 68 74 2d 6d  ce the 3 right-m
1674e 6f 73 74 20 6c 65 61 66 20 70 61 67 65 73 2c 20  ost leaf pages, 
1674f 6a 75 73 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65  just add.** a ne
16750 77 20 70 61 67 65 20 74 6f 20 74 68 65 20 72 69  w page to the ri
16751 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 61 6e  ght-hand side an
16752 64 20 70 75 74 20 74 68 65 20 6f 6e 65 20 6e 65  d put the one ne
16753 77 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68  w entry in.** th
16754 61 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 6c  at page.  This l
16755 65 61 76 65 73 20 74 68 65 20 72 69 67 68 74 20  eaves the right 
16756 73 69 64 65 20 6f 66 20 74 68 65 20 74 72 65 65  side of the tree
16757 20 73 6f 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62   somewhat.** unb
16758 61 6c 61 6e 63 65 64 2e 20 20 42 75 74 20 6f 64  alanced.  But od
16759 64 73 20 61 72 65 20 74 68 61 74 20 77 65 20 77  ds are that we w
1675a 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67  ill be inserting
1675b 20 6e 65 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20   new entries.** 
1675c 61 74 20 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20  at the end soon 
1675d 61 66 74 65 72 77 61 72 64 73 20 73 6f 20 74 68  afterwards so th
1675e 65 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 20 70  e nearly empty p
1675f 61 67 65 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79  age will quickly
16760 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e  .** fill up.  On
16761 20 61 76 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20   average..**.** 
16762 70 50 61 67 65 20 69 73 20 74 68 65 20 6c 65 61  pPage is the lea
16763 66 20 70 61 67 65 20 77 68 69 63 68 20 69 73 20  f page which is 
16764 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 70  the right-most p
16765 61 67 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e  age in the tree.
16766 0a 2a 2a 20 70 50 61 72 65 6e 74 20 69 73 20 69  .** pParent is i
16767 74 73 20 70 61 72 65 6e 74 2e 20 20 70 50 61 67  ts parent.  pPag
16768 65 20 6d 75 73 74 20 68 61 76 65 20 61 20 73 69  e must have a si
16769 6e 67 6c 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e  ngle overflow en
1676a 74 72 79 0a 2a 2a 20 77 68 69 63 68 20 69 73 20  try.** which is 
1676b 61 6c 73 6f 20 74 68 65 20 72 69 67 68 74 2d 6d  also the right-m
1676c 6f 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65  ost entry on the
1676d 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   page..*/.static
1676e 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69   int balance_qui
1676f 63 6b 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ck(BtCursor *pCu
16770 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  r){.  int rc;.  
16771 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d 20  MemPage *pNew = 
16772 30 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65  0;.  Pgno pgnoNe
16773 77 3b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a  w;.  u8 *pCell;.
16774 20 20 75 31 36 20 73 7a 43 65 6c 6c 3b 0a 20 20    u16 szCell;.  
16775 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
16776 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
16777 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
16778 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 4d  Cur->iPage];.  M
16779 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 20  emPage *pParent 
1677a 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
1677b 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20  Cur->iPage-1];. 
1677c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1677d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69   pPage->pBt;.  i
1677e 6e 74 20 70 61 72 65 6e 74 49 64 78 20 3d 20 70  nt parentIdx = p
1677f 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 20 20  Parent->nCell;  
16780 20 2f 2a 20 70 50 61 72 65 6e 74 20 6e 65 77 20   /* pParent new 
16781 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 64  divider cell ind
16782 65 78 20 2a 2f 0a 20 20 69 6e 74 20 70 61 72 65  ex */.  int pare
16783 6e 74 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  ntSize;         
16784 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
16785 65 20 6f 66 20 6e 65 77 20 64 69 76 69 64 65 72  e of new divider
16786 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 70 61   cell */.  u8 pa
16787 72 65 6e 74 43 65 6c 6c 5b 36 34 5d 3b 20 20 20  rentCell[64];   
16788 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16789 53 70 61 63 65 20 66 6f 72 20 74 68 65 20 6e 65  Space for the ne
1678a 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a  w divider cell *
1678b 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  /..  assert( sql
1678c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1678d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
1678e 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  x) );..  /* Allo
1678f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e  cate a new page.
16790 20 49 6e 73 65 72 74 20 74 68 65 20 6f 76 65 72   Insert the over
16791 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f 6d 20 70  flow cell from p
16792 50 61 67 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 69  Page.  ** into i
16793 74 2e 20 54 68 65 6e 20 72 65 6d 6f 76 65 20 74  t. Then remove t
16794 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  he overflow cell
16795 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 20 20 2a   from pPage..  *
16796 2f 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74  /.  rc = allocat
16797 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
16798 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c  &pNew, &pgnoNew,
16799 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63   0, 0);.  if( rc
1679a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1679b 20 20 20 70 43 65 6c 6c 20 3d 20 70 50 61 67 65     pCell = pPage
1679c 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c  ->aOvfl[0].pCell
1679d 3b 0a 20 20 20 20 73 7a 43 65 6c 6c 20 3d 20 63  ;.    szCell = c
1679e 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
1679f 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73  , pCell);.    as
167a0 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
167a1 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e  erIswriteable(pN
167a2 65 77 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ew->pDbPage) );.
167a3 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65      zeroPage(pNe
167a4 77 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  w, pPage->aData[
167a5 30 5d 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c  0]);.    assembl
167a6 65 50 61 67 65 28 70 4e 65 77 2c 20 31 2c 20 26  ePage(pNew, 1, &
167a7 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b  pCell, &szCell);
167a8 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65  .    pPage->nOve
167a9 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 0a 20 20  rflow = 0;.  .  
167aa 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 63 75    /* pPage is cu
167ab 72 72 65 6e 74 6c 79 20 74 68 65 20 72 69 67 68  rrently the righ
167ac 74 2d 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65  t-child of pPare
167ad 6e 74 2e 20 43 68 61 6e 67 65 20 74 68 69 73 0a  nt. Change this.
167ae 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 74      ** so that t
167af 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69  he right-child i
167b0 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 61  s the new page a
167b1 6c 6c 6f 63 61 74 65 64 20 61 62 6f 76 65 20 61  llocated above a
167b2 6e 64 0a 20 20 20 20 2a 2a 20 70 50 61 67 65 20  nd.    ** pPage 
167b3 69 73 20 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72  is the next-to-r
167b4 69 67 68 74 20 63 68 69 6c 64 2e 20 0a 20 20 20  ight child. .   
167b5 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 67 6e 6f 72   **.    ** Ignor
167b6 65 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  e the return val
167b7 75 65 20 6f 66 20 74 68 65 20 63 61 6c 6c 20 74  ue of the call t
167b8 6f 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 2e 20  o fillInCell(). 
167b9 66 69 6c 6c 49 6e 43 65 6c 6c 28 29 0a 20 20 20  fillInCell().   
167ba 20 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 72 65 74   ** may only ret
167bb 75 72 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 53  urn other than S
167bc 51 4c 49 54 45 5f 4f 4b 20 69 66 20 69 74 20 69  QLITE_OK if it i
167bd 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 6c  s required to al
167be 6c 6f 63 61 74 65 0a 20 20 20 20 2a 2a 20 6f 6e  locate.    ** on
167bf 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c  e or more overfl
167c0 6f 77 20 70 61 67 65 73 2e 20 53 69 6e 63 65 20  ow pages. Since 
167c1 61 6e 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c  an internal tabl
167c2 65 20 42 2d 54 72 65 65 20 63 65 6c 6c 20 0a 20  e B-Tree cell . 
167c3 20 20 20 2a 2a 20 6d 61 79 20 6e 65 76 65 72 20     ** may never 
167c4 73 70 69 6c 6c 20 6f 76 65 72 20 6f 6e 74 6f 20  spill over onto 
167c5 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
167c6 20 28 69 74 20 69 73 20 61 20 6d 61 78 69 6d 75   (it is a maximu
167c7 6d 20 6f 66 20 0a 20 20 20 20 2a 2a 20 31 33 20  m of .    ** 13 
167c8 62 79 74 65 73 20 69 6e 20 73 69 7a 65 29 2c 20  bytes in size), 
167c9 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 63 65 73  it is not necces
167ca 73 61 72 79 20 74 6f 20 63 68 65 63 6b 20 74 68  sary to check th
167cb 65 20 72 65 74 75 72 6e 20 63 6f 64 65 2e 0a 20  e return code.. 
167cc 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d     **.    ** Sim
167cd 69 6c 61 72 6c 79 2c 20 74 68 65 20 69 6e 73 65  ilarly, the inse
167ce 72 74 43 65 6c 6c 28 29 20 66 75 6e 63 74 69 6f  rtCell() functio
167cf 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 69 66  n cannot fail if
167d0 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 2a 2a   the page.    **
167d1 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20   being inserted 
167d2 69 6e 74 6f 20 69 73 20 61 6c 72 65 61 64 79 20  into is already 
167d3 77 72 69 74 61 62 6c 65 20 61 6e 64 20 74 68 65  writable and the
167d4 20 63 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 0a   cell does not .
167d5 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 61      ** contain a
167d6 6e 20 6f 76 65 72 66 6c 6f 77 20 70 6f 69 6e 74  n overflow point
167d7 65 72 2e 20 53 6f 20 69 67 6e 6f 72 65 20 74 68  er. So ignore th
167d8 69 73 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74  is return code t
167d9 6f 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  oo..    */.    a
167da 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43  ssert( pPage->nC
167db 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 70 43 65  ell>0 );.    pCe
167dc 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
167dd 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c  age, pPage->nCel
167de 6c 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  l-1);.    sqlite
167df 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
167e0 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
167e1 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 66 69 6c   &info);.    fil
167e2 6c 49 6e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c  lInCell(pParent,
167e3 20 70 61 72 65 6e 74 43 65 6c 6c 2c 20 30 2c 20   parentCell, 0, 
167e4 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c  info.nKey, 0, 0,
167e5 20 30 2c 20 26 70 61 72 65 6e 74 53 69 7a 65 29   0, &parentSize)
167e6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61  ;.    assert( pa
167e7 72 65 6e 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20  rentSize<64 );. 
167e8 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
167e9 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
167ea 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50  le(pParent->pDbP
167eb 61 67 65 29 20 29 3b 0a 20 20 20 20 69 6e 73 65  age) );.    inse
167ec 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  rtCell(pParent, 
167ed 70 61 72 65 6e 74 49 64 78 2c 20 70 61 72 65 6e  parentIdx, paren
167ee 74 43 65 6c 6c 2c 20 70 61 72 65 6e 74 53 69 7a  tCell, parentSiz
167ef 65 2c 20 30 2c 20 34 29 3b 0a 20 20 20 20 70 75  e, 0, 4);.    pu
167f0 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66  t4byte(findOverf
167f1 6c 6f 77 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c  lowCell(pParent,
167f2 70 61 72 65 6e 74 49 64 78 29 2c 20 70 50 61 67  parentIdx), pPag
167f3 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75  e->pgno);.    pu
167f4 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d  t4byte(&pParent-
167f5 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e  >aData[pParent->
167f6 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67  hdrOffset+8], pg
167f7 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f  noNew);.  .    /
167f8 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
167f9 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
167fa 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65  base, update the
167fb 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20   pointer map.   
167fc 20 2a 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73   ** with entries
167fd 20 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67   for the new pag
167fe 65 2c 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74  e, and any point
167ff 65 72 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20  er from the .   
16800 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20   ** cell on the 
16801 70 61 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66  page to an overf
16802 6c 6f 77 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  low page..    */
16803 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56  .    if( ISAUTOV
16804 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72  ACUUM ){.      r
16805 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42  c = ptrmapPut(pB
16806 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d  t, pgnoNew, PTRM
16807 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e  AP_BTREE, pParen
16808 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  t->pgno);.      
16809 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1680a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
1680b 3d 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28  = ptrmapPutOvfl(
1680c 70 4e 65 77 2c 20 30 29 3b 0a 20 20 20 20 20 20  pNew, 0);.      
1680d 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
1680e 52 65 6c 65 61 73 65 20 74 68 65 20 72 65 66 65  Release the refe
1680f 72 65 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 77  rence to the new
16810 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65   page. */.    re
16811 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 29 3b  leasePage(pNew);
16812 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68  .  }..  /* At th
16813 69 73 20 70 6f 69 6e 74 20 74 68 65 20 70 50 61  is point the pPa
16814 67 65 2d 3e 6e 46 72 65 65 20 76 61 72 69 61 62  ge->nFree variab
16815 6c 65 20 69 73 20 6e 6f 74 20 73 65 74 20 63 6f  le is not set co
16816 72 72 65 63 74 6c 79 20 77 69 74 68 0a 20 20 2a  rrectly with.  *
16817 2a 20 72 65 73 70 65 63 74 20 74 6f 20 74 68 65  * respect to the
16818 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
16819 70 61 67 65 20 28 62 65 63 61 75 73 65 20 69 74  page (because it
1681a 20 77 61 73 20 73 65 74 20 74 6f 20 30 20 62 79   was set to 0 by
1681b 20 0a 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c   .  ** insertCel
1681c 6c 29 2e 20 53 6f 20 63 61 6c 6c 20 73 71 6c 69  l). So call sqli
1681d 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65  te3BtreeInitPage
1681e 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  () to make sure 
1681f 69 74 20 69 73 0a 20 20 2a 2a 20 63 6f 72 72 65  it is.  ** corre
16820 63 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ct..  **.  ** Th
16821 69 73 20 68 61 73 20 74 6f 20 62 65 20 64 6f 6e  is has to be don
16822 65 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72  e even if an err
16823 6f 72 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  or will be retur
16824 6e 65 64 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69  ned. Normally, i
16825 66 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20  f.  ** an error 
16826 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 72  occurs during tr
16827 65 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68  ee balancing, th
16828 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65  e contents of Me
16829 6d 50 61 67 65 20 61 72 65 0a 20 20 2a 2a 20 6e  mPage are.  ** n
1682a 6f 74 20 69 6d 70 6f 72 74 61 6e 74 2c 20 61 73  ot important, as
1682b 20 74 68 65 79 20 77 69 6c 6c 20 62 65 20 72 65   they will be re
1682c 63 61 6c 63 75 6c 61 74 65 64 20 77 68 65 6e 20  calculated when 
1682d 74 68 65 20 70 61 67 65 20 69 73 20 72 6f 6c 6c  the page is roll
1682e 65 64 0a 20 20 2a 2a 20 62 61 63 6b 2e 20 42 75  ed.  ** back. Bu
1682f 74 20 68 65 72 65 2c 20 69 6e 20 62 61 6c 61 6e  t here, in balan
16830 63 65 5f 71 75 69 63 6b 28 29 2c 20 69 74 20 69  ce_quick(), it i
16831 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20  s possible that 
16832 70 50 61 67 65 20 68 61 73 20 0a 20 20 2a 2a 20  pPage has .  ** 
16833 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6d 61 72  not yet been mar
16834 6b 65 64 20 64 69 72 74 79 20 6f 72 20 77 72 69  ked dirty or wri
16835 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f  tten into the jo
16836 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72  urnal file. Ther
16837 65 66 6f 72 65 0a 20 20 2a 2a 20 69 74 20 77 69  efore.  ** it wi
16838 6c 6c 20 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64  ll not be rolled
16839 20 62 61 63 6b 20 61 6e 64 20 73 6f 20 69 74 20   back and so it 
1683a 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  is important to 
1683b 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20  make sure that. 
1683c 20 2a 2a 20 74 68 65 20 70 61 67 65 20 64 61 74   ** the page dat
1683d 61 20 61 6e 64 20 63 6f 6e 74 65 6e 74 73 20 6f  a and contents o
1683e 66 20 4d 65 6d 50 61 67 65 20 61 72 65 20 63 6f  f MemPage are co
1683f 6e 73 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20  nsistent..  */. 
16840 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
16841 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   0;.  sqlite3Btr
16842 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65  eeInitPage(pPage
16843 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
16844 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  ge->nOverflow==0
16845 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65   );..  /* If eve
16846 72 79 74 68 69 6e 67 20 65 6c 73 65 20 73 75 63  rything else suc
16847 63 65 65 64 65 64 2c 20 62 61 6c 61 6e 63 65 20  ceeded, balance 
16848 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c  the parent page,
16849 20 69 6e 20 0a 20 20 2a 2a 20 63 61 73 65 20 74   in .  ** case t
1684a 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  he divider cell 
1684b 69 6e 73 65 72 74 65 64 20 63 61 75 73 65 64 20  inserted caused 
1684c 69 74 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65  it to become ove
1684d 72 66 75 6c 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66  rfull..  */.  if
1684e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1684f 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
16850 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70  ge(pPage);.    p
16851 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20  Cur->iPage--;.  
16852 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70    rc = balance(p
16853 43 75 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  Cur, 0);.  }.  r
16854 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
16855 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
16856 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a  T_QUICKBALANCE *
16857 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  /../*.** This ro
16858 75 74 69 6e 65 20 72 65 64 69 73 74 72 69 62 75  utine redistribu
16859 74 65 73 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61  tes Cells on pPa
1685a 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 4e 2a  ge and up to NN*
1685b 32 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66  2 siblings.** of
1685c 20 70 50 61 67 65 20 73 6f 20 74 68 61 74 20 61   pPage so that a
1685d 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 61 62  ll pages have ab
1685e 6f 75 74 20 74 68 65 20 73 61 6d 65 20 61 6d 6f  out the same amo
1685f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63  unt of free spac
16860 65 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 4e 4e  e..** Usually NN
16861 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74   siblings on eit
16862 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 67  her side of pPag
16863 65 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65  e is used in the
16864 20 62 61 6c 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74   balancing,.** t
16865 68 6f 75 67 68 20 6d 6f 72 65 20 73 69 62 6c 69  hough more sibli
16866 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66  ngs might come f
16867 72 6f 6d 20 6f 6e 65 20 73 69 64 65 20 69 66 20  rom one side if 
16868 70 50 61 67 65 20 69 73 20 74 68 65 20 66 69 72  pPage is the fir
16869 73 74 0a 2a 2a 20 6f 72 20 6c 61 73 74 20 63 68  st.** or last ch
1686a 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65 6e  ild of its paren
1686b 74 2e 20 20 49 66 20 70 50 61 67 65 20 68 61 73  t.  If pPage has
1686c 20 66 65 77 65 72 20 74 68 61 6e 20 32 2a 4e 4e   fewer than 2*NN
1686d 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f   siblings.** (so
1686e 6d 65 74 68 69 6e 67 20 77 68 69 63 68 20 63 61  mething which ca
1686f 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66  n only happen if
16870 20 70 50 61 67 65 20 69 73 20 74 68 65 20 72 6f   pPage is the ro
16871 6f 74 20 70 61 67 65 20 6f 72 20 61 20 0a 2a 2a  ot page or a .**
16872 20 63 68 69 6c 64 20 6f 66 20 72 6f 6f 74 29 20   child of root) 
16873 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62  then all availab
16874 6c 65 20 73 69 62 6c 69 6e 67 73 20 70 61 72 74  le siblings part
16875 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62  icipate in the b
16876 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  alancing..**.** 
16877 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69  The number of si
16878 62 6c 69 6e 67 73 20 6f 66 20 70 50 61 67 65 20  blings of pPage 
16879 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73  might be increas
1687a 65 64 20 6f 72 20 64 65 63 72 65 61 73 65 64 20  ed or decreased 
1687b 62 79 20 6f 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f  by one or.** two
1687c 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f   in an effort to
1687d 20 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 72   keep pages near
1687e 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20  ly full but not 
1687f 6f 76 65 72 20 66 75 6c 6c 2e 20 54 68 65 20 72  over full. The r
16880 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 73  oot page.** is s
16881 70 65 63 69 61 6c 20 61 6e 64 20 69 73 20 61 6c  pecial and is al
16882 6c 6f 77 65 64 20 74 6f 20 62 65 20 6e 65 61 72  lowed to be near
16883 6c 79 20 65 6d 70 74 79 2e 20 49 66 20 70 50 61  ly empty. If pPa
16884 67 65 20 69 73 20 0a 2a 2a 20 74 68 65 20 72 6f  ge is .** the ro
16885 6f 74 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68  ot page, then th
16886 65 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 74  e depth of the t
16887 72 65 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63  ree might be inc
16888 72 65 61 73 65 64 0a 2a 2a 20 6f 72 20 64 65 63  reased.** or dec
16889 72 65 61 73 65 64 20 62 79 20 6f 6e 65 2c 20 61  reased by one, a
1688a 73 20 6e 65 63 65 73 73 61 72 79 2c 20 74 6f 20  s necessary, to 
1688b 6b 65 65 70 20 74 68 65 20 72 6f 6f 74 20 70 61  keep the root pa
1688c 67 65 20 66 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a  ge from being.**
1688d 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 63 6f 6d   overfull or com
1688e 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 0a 2a  pletely empty..*
1688f 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77  *.** Note that w
16890 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
16891 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65   is called, some
16892 20 6f 66 20 74 68 65 20 43 65 6c 6c 73 20 6f 6e   of the Cells on
16893 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20   pPage.** might 
16894 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20  not actually be 
16895 73 74 6f 72 65 64 20 69 6e 20 70 50 61 67 65 2d  stored in pPage-
16896 3e 61 44 61 74 61 5b 5d 2e 20 20 54 68 69 73 20  >aData[].  This 
16897 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66  can happen.** if
16898 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 76 65   the page is ove
16899 72 66 75 6c 6c 2e 20 20 50 61 72 74 20 6f 66 20  rfull.  Part of 
1689a 74 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20  the job of this 
1689b 72 6f 75 74 69 6e 65 20 69 73 20 74 6f 0a 2a 2a  routine is to.**
1689c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 43   make sure all C
1689d 65 6c 6c 73 20 66 6f 72 20 70 50 61 67 65 20 6f  ells for pPage o
1689e 6e 63 65 20 61 67 61 69 6e 20 66 69 74 20 69 6e  nce again fit in
1689f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 2e   pPage->aData[].
168a0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f  .**.** In the co
168a1 75 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e  urse of balancin
168a2 67 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 6f  g the siblings o
168a3 66 20 70 50 61 67 65 2c 20 74 68 65 20 70 61 72  f pPage, the par
168a4 65 6e 74 20 6f 66 20 70 50 61 67 65 0a 2a 2a 20  ent of pPage.** 
168a5 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 6f 76 65  might become ove
168a6 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75  rfull or underfu
168a7 6c 6c 2e 20 20 49 66 20 74 68 61 74 20 68 61 70  ll.  If that hap
168a8 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 69 73 20  pens, then this 
168a9 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61  routine.** is ca
168aa 6c 6c 65 64 20 72 65 63 75 72 73 69 76 65 6c 79  lled recursively
168ab 20 6f 6e 20 74 68 65 20 70 61 72 65 6e 74 2e 0a   on the parent..
168ac 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f  **.** If this ro
168ad 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20  utine fails for 
168ae 61 6e 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d  any reason, it m
168af 69 67 68 74 20 6c 65 61 76 65 20 74 68 65 20 64  ight leave the d
168b0 61 74 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20  atabase.** in a 
168b1 63 6f 72 72 75 70 74 65 64 20 73 74 61 74 65 2e  corrupted state.
168b2 20 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75    So if this rou
168b3 74 69 6e 65 20 66 61 69 6c 73 2c 20 74 68 65 20  tine fails, the 
168b4 64 61 74 61 62 61 73 65 20 73 68 6f 75 6c 64 0a  database should.
168b5 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  ** be rolled bac
168b6 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  k..*/.static int
168b7 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74   balance_nonroot
168b8 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
168b9 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
168ba 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
168bb 20 2f 2a 20 54 68 65 20 6f 76 65 72 20 6f 72 20   /* The over or 
168bc 75 6e 64 65 72 66 75 6c 6c 20 70 61 67 65 20 74  underfull page t
168bd 6f 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 4d  o balance */.  M
168be 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 3b  emPage *pParent;
168bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
168c0 68 65 20 70 61 72 65 6e 74 20 6f 66 20 70 50 61  he parent of pPa
168c1 67 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64  ge */.  BtShared
168c2 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20   *pBt;          
168c3 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c       /* The whol
168c4 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
168c5 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20  int nCell = 0;  
168c6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
168c7 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  Number of cells 
168c8 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20  in apCell[] */. 
168c9 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d   int nMaxCells =
168ca 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
168cb 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   Allocated size 
168cc 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c  of apCell, szCel
168cd 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69  l, aFrom. */.  i
168ce 6e 74 20 6e 4f 6c 64 20 3d 20 30 3b 20 20 20 20  nt nOld = 0;    
168cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
168d0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
168d1 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69  n apOld[] */.  i
168d2 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20  nt nNew = 0;    
168d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
168d4 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
168d5 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69  n apNew[] */.  i
168d6 6e 74 20 6e 44 69 76 3b 20 20 20 20 20 20 20 20  nt nDiv;        
168d7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
168d8 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69  umber of cells i
168d9 6e 20 61 70 44 69 76 5b 5d 20 2a 2f 0a 20 20 69  n apDiv[] */.  i
168da 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20  nt i, j, k;     
168db 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
168dc 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
168dd 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20    int idx;      
168de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
168df 2a 20 49 6e 64 65 78 20 6f 66 20 70 50 61 67 65  * Index of pPage
168e0 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65   in pParent->aCe
168e1 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 78  ll[] */.  int nx
168e2 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  Div;            
168e3 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64         /* Next d
168e4 69 76 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70  ivider slot in p
168e5 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20  Parent->aCell[] 
168e6 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
168e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
168e8 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20    /* The return 
168e9 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6c 65  code */.  int le
168ea 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20  afCorrection;   
168eb 20 20 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70         /* 4 if p
168ec 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20  Page is a leaf. 
168ed 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69   0 if not */.  i
168ee 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20  nt leafData;    
168ef 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
168f0 72 75 65 20 69 66 20 70 50 61 67 65 20 69 73 20  rue if pPage is 
168f1 61 20 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46  a leaf of a LEAF
168f2 44 41 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69  DATA tree */.  i
168f3 6e 74 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20  nt usableSpace; 
168f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
168f5 79 74 65 73 20 69 6e 20 70 50 61 67 65 20 62 65  ytes in pPage be
168f6 79 6f 6e 64 20 74 68 65 20 68 65 61 64 65 72 20  yond the header 
168f7 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61  */.  int pageFla
168f8 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
168f9 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50    /* Value of pP
168fa 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f  age->aData[0] */
168fb 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b  .  int subtotal;
168fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
168fd 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62  /* Subtotal of b
168fe 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e  ytes in cells on
168ff 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69   one page */.  i
16900 6e 74 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20  nt iSpace1 = 0; 
16901 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
16902 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65  irst unused byte
16903 20 6f 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f   of aSpace1[] */
16904 0a 20 20 69 6e 74 20 69 53 70 61 63 65 32 20 3d  .  int iSpace2 =
16905 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
16906 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20  /* First unused 
16907 62 79 74 65 20 6f 66 20 61 53 70 61 63 65 32 5b  byte of aSpace2[
16908 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72  ] */.  int szScr
16909 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
1690a 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73      /* Size of s
1690b 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 72 65  cratch memory re
1690c 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d  quested */.  Mem
1690d 50 61 67 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b  Page *apOld[NB];
1690e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61            /* pPa
1690f 67 65 20 61 6e 64 20 75 70 20 74 6f 20 74 77 6f  ge and up to two
16910 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 50   siblings */.  P
16911 67 6e 6f 20 70 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b  gno pgnoOld[NB];
16912 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
16913 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20  age numbers for 
16914 65 61 63 68 20 70 61 67 65 20 69 6e 20 61 70 4f  each page in apO
16915 6c 64 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ld[] */.  MemPag
16916 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20  e *apCopy[NB];  
16917 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74         /* Privat
16918 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c  e copies of apOl
16919 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d  d[] pages */.  M
1691a 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42  emPage *apNew[NB
1691b 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70  +2];        /* p
1691c 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e  Page and up to N
1691d 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72  B siblings after
1691e 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20   balancing */.  
1691f 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 5b 4e 42 2b  Pgno pgnoNew[NB+
16920 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  2];          /* 
16921 50 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72  Page numbers for
16922 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 61 70   each page in ap
16923 4e 65 77 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61  New[] */.  u8 *a
16924 70 44 69 76 5b 4e 42 5d 3b 20 20 20 20 20 20 20  pDiv[NB];       
16925 20 20 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64          /* Divid
16926 65 72 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72  er cells in pPar
16927 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  ent */.  int cnt
16928 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20  New[NB+2];      
16929 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
1692a 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c  n aCell[] of cel
1692b 6c 20 61 66 74 65 72 20 69 2d 74 68 20 70 61 67  l after i-th pag
1692c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77  e */.  int szNew
1692d 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20  [NB+2];         
1692e 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20      /* Combined 
1692f 73 69 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c  size of cells pl
16930 61 63 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 65  ace on i-th page
16931 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c   */.  u8 **apCel
16932 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  l = 0;          
16933 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20     /* All cells 
16934 62 65 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a  begin balanced *
16935 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b  /.  u16 *szCell;
16936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16937 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f   /* Local size o
16938 66 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61  f all cells in a
16939 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20  pCell[] */.  u8 
1693a 2a 61 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20  *aCopy[NB];     
1693b 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
1693c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 6f 66   holding data of
1693d 20 61 70 43 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75   apCopy[] */.  u
1693e 38 20 2a 61 53 70 61 63 65 31 3b 20 20 20 20 20  8 *aSpace1;     
1693f 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66        /* Space f
16940 6f 72 20 63 6f 70 69 65 73 20 6f 66 20 64 69 76  or copies of div
16941 69 64 65 72 73 20 63 65 6c 6c 73 20 62 65 66 6f  iders cells befo
16942 72 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20  re balance */.  
16943 75 38 20 2a 61 53 70 61 63 65 32 20 3d 20 30 3b  u8 *aSpace2 = 0;
16944 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
16945 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 64 69 76  for overflow div
16946 69 64 65 72 73 20 63 65 6c 6c 73 20 61 66 74 65  iders cells afte
16947 72 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75  r balance */.  u
16948 38 20 2a 61 46 72 6f 6d 20 3d 20 30 3b 0a 0a 20  8 *aFrom = 0;.. 
16949 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
1694a 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
1694b 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  e];.  assert( sq
1694c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1694d 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
1694e 65 78 29 20 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c  ex) );.  VVA_ONL
1694f 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68  Y( pCur->pagesSh
16950 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 0a 20  uffled = 1 );.. 
16951 20 2f 2a 20 0a 20 20 2a 2a 20 46 69 6e 64 20 74   /* .  ** Find t
16952 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a  he parent page..
16953 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
16954 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b 0a  Cur->iPage>0 );.
16955 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
16956 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73  >isInit );.  ass
16957 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
16958 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
16959 67 65 2d 3e 70 44 62 50 61 67 65 29 20 7c 7c 20  ge->pDbPage) || 
1695a 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
1695b 3d 3d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70  ==1 );.  pBt = p
1695c 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 70 50 61  Page->pBt;.  pPa
1695d 72 65 6e 74 20 3d 20 70 43 75 72 2d 3e 61 70 50  rent = pCur->apP
1695e 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d  age[pCur->iPage-
1695f 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  1];.  assert( pP
16960 61 72 65 6e 74 20 29 3b 0a 20 20 69 66 28 20 53  arent );.  if( S
16961 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
16962 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
16963 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61  e(pParent->pDbPa
16964 67 65 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ge)) ){.    goto
16965 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
16966 3b 0a 20 20 7d 0a 0a 20 20 54 52 41 43 45 28 28  ;.  }..  TRACE((
16967 22 42 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20  "BALANCE: begin 
16968 70 61 67 65 20 25 64 20 63 68 69 6c 64 20 6f 66  page %d child of
16969 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70   %d\n", pPage->p
1696a 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67  gno, pParent->pg
1696b 6e 6f 29 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  no));..#ifndef S
1696c 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b  QLITE_OMIT_QUICK
1696d 42 41 4c 41 4e 43 45 0a 20 20 2f 2a 0a 20 20 2a  BALANCE.  /*.  *
1696e 2a 20 41 20 73 70 65 63 69 61 6c 20 63 61 73 65  * A special case
1696f 3a 20 20 49 66 20 61 20 6e 65 77 20 65 6e 74 72  :  If a new entr
16970 79 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20  y has just been 
16971 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 0a  inserted into a.
16972 20 20 2a 2a 20 74 61 62 6c 65 20 28 74 68 61 74    ** table (that
16973 20 69 73 2c 20 61 20 62 74 72 65 65 20 77 69 74   is, a btree wit
16974 68 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 61  h integer keys a
16975 6e 64 20 61 6c 6c 20 64 61 74 61 20 61 74 20 74  nd all data at t
16976 68 65 20 6c 65 61 76 65 73 29 0a 20 20 2a 2a 20  he leaves).  ** 
16977 61 6e 64 20 74 68 65 20 6e 65 77 20 65 6e 74 72  and the new entr
16978 79 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d  y is the right-m
16979 6f 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ost entry in the
1697a 20 74 72 65 65 20 28 69 74 20 68 61 73 20 74 68   tree (it has th
1697b 65 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b  e.  ** largest k
1697c 65 79 29 20 74 68 65 6e 20 75 73 65 20 74 68 65  ey) then use the
1697d 20 73 70 65 63 69 61 6c 20 62 61 6c 61 6e 63 65   special balance
1697e 5f 71 75 69 63 6b 28 29 20 72 6f 75 74 69 6e 65  _quick() routine
1697f 20 66 6f 72 0a 20 20 2a 2a 20 62 61 6c 61 6e 63   for.  ** balanc
16980 69 6e 67 2e 20 20 62 61 6c 61 6e 63 65 5f 71 75  ing.  balance_qu
16981 69 63 6b 28 29 20 69 73 20 6d 75 63 68 20 66 61  ick() is much fa
16982 73 74 65 72 20 61 6e 64 20 72 65 73 75 6c 74 73  ster and results
16983 20 69 6e 20 61 20 74 69 67 68 74 65 72 0a 20 20   in a tighter.  
16984 2a 2a 20 70 61 63 6b 69 6e 67 20 6f 66 20 64 61  ** packing of da
16985 74 61 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e  ta in the common
16986 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   case..  */.  if
16987 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 26 26  ( pPage->leaf &&
16988 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69 6e  .      pPage->in
16989 74 4b 65 79 20 26 26 0a 20 20 20 20 20 20 70 50  tKey &&.      pP
1698a 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  age->nOverflow==
1698b 31 20 26 26 0a 20 20 20 20 20 20 70 50 61 67 65  1 &&.      pPage
1698c 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d  ->aOvfl[0].idx==
1698d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 0a  pPage->nCell &&.
1698e 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
1698f 67 6e 6f 21 3d 31 20 26 26 0a 20 20 20 20 20 20  gno!=1 &&.      
16990 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e  get4byte(&pParen
16991 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74  t->aData[pParent
16992 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d  ->hdrOffset+8])=
16993 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 0a 20 20 29  =pPage->pgno.  )
16994 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
16995 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20  age->intKey );. 
16996 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 4f 44     /*.    ** TOD
16997 4f 3a 20 43 68 65 63 6b 20 74 68 65 20 73 69 62  O: Check the sib
16998 6c 69 6e 67 73 20 74 6f 20 74 68 65 20 6c 65 66  lings to the lef
16999 74 20 6f 66 20 70 50 61 67 65 2e 20 49 74 20 6d  t of pPage. It m
1699a 61 79 20 62 65 20 74 68 61 74 0a 20 20 20 20 2a  ay be that.    *
1699b 2a 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 66  * they are not f
1699c 75 6c 6c 20 61 6e 64 20 6e 6f 20 6e 65 77 20 70  ull and no new p
1699d 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 2e  age is required.
1699e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75  .    */.    retu
1699f 72 6e 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  rn balance_quick
169a0 28 70 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64  (pCur);.  }.#end
169a1 69 66 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45  if..  if( SQLITE
169a2 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK!=(rc = sqlit
169a3 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
169a4 67 65 2d 3e 70 44 62 50 61 67 65 29 29 20 29 7b  ge->pDbPage)) ){
169a5 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63  .    goto balanc
169a6 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  e_cleanup;.  }..
169a7 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74    /*.  ** Find t
169a8 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70  he cell in the p
169a9 61 72 65 6e 74 20 70 61 67 65 20 77 68 6f 73 65  arent page whose
169aa 20 6c 65 66 74 20 63 68 69 6c 64 20 70 6f 69 6e   left child poin
169ab 74 73 20 62 61 63 6b 0a 20 20 2a 2a 20 74 6f 20  ts back.  ** to 
169ac 70 50 61 67 65 2e 20 20 54 68 65 20 22 69 64 78  pPage.  The "idx
169ad 22 20 76 61 72 69 61 62 6c 65 20 69 73 20 74 68  " variable is th
169ae 65 20 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20  e index of that 
169af 63 65 6c 6c 2e 20 20 49 66 20 70 50 61 67 65 0a  cell.  If pPage.
169b0 20 20 2a 2a 20 69 73 20 74 68 65 20 72 69 67 68    ** is the righ
169b1 74 6d 6f 73 74 20 63 68 69 6c 64 20 6f 66 20 70  tmost child of p
169b2 50 61 72 65 6e 74 20 74 68 65 6e 20 73 65 74 20  Parent then set 
169b3 69 64 78 20 74 6f 20 70 50 61 72 65 6e 74 2d 3e  idx to pParent->
169b4 6e 43 65 6c 6c 20 0a 20 20 2a 2f 0a 20 20 69 64  nCell .  */.  id
169b5 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  x = pCur->aiIdx[
169b6 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a  pCur->iPage-1];.
169b7 20 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e    assertParentIn
169b8 64 65 78 28 70 50 61 72 65 6e 74 2c 20 69 64 78  dex(pParent, idx
169b9 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a  , pPage->pgno);.
169ba 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20  .  /*.  ** Find 
169bb 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f  sibling pages to
169bc 20 70 50 61 67 65 20 61 6e 64 20 74 68 65 20 63   pPage and the c
169bd 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20  ells in pParent 
169be 74 68 61 74 20 64 69 76 69 64 65 0a 20 20 2a 2a  that divide.  **
169bf 20 74 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 20   the siblings.  
169c0 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  An attempt is ma
169c1 64 65 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69  de to find NN si
169c2 62 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72  blings on either
169c3 0a 20 20 2a 2a 20 73 69 64 65 20 6f 66 20 70 50  .  ** side of pP
169c4 61 67 65 2e 20 20 4d 6f 72 65 20 73 69 62 6c 69  age.  More sibli
169c5 6e 67 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72  ngs are taken fr
169c6 6f 6d 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77  om one side, how
169c7 65 76 65 72 2c 20 69 66 0a 20 20 2a 2a 20 70 50  ever, if.  ** pP
169c8 61 67 65 20 74 68 65 72 65 20 61 72 65 20 66 65  age there are fe
169c9 77 65 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c  wer than NN sibl
169ca 69 6e 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 65  ings on the othe
169cb 72 20 73 69 64 65 2e 20 20 49 66 20 70 50 61 72  r side.  If pPar
169cc 65 6e 74 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20  ent.  ** has NB 
169cd 6f 72 20 66 65 77 65 72 20 63 68 69 6c 64 72 65  or fewer childre
169ce 6e 20 74 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64  n then all child
169cf 72 65 6e 20 6f 66 20 70 50 61 72 65 6e 74 20 61  ren of pParent a
169d0 72 65 20 74 61 6b 65 6e 2e 0a 20 20 2a 2f 0a 20  re taken..  */. 
169d1 20 6e 78 44 69 76 20 3d 20 69 64 78 20 2d 20 4e   nxDiv = idx - N
169d2 4e 3b 0a 20 20 69 66 28 20 6e 78 44 69 76 20 2b  N;.  if( nxDiv +
169d3 20 4e 42 20 3e 20 70 50 61 72 65 6e 74 2d 3e 6e   NB > pParent->n
169d4 43 65 6c 6c 20 29 7b 0a 20 20 20 20 6e 78 44 69  Cell ){.    nxDi
169d5 76 20 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65  v = pParent->nCe
169d6 6c 6c 20 2d 20 4e 42 20 2b 20 31 3b 0a 20 20 7d  ll - NB + 1;.  }
169d7 0a 20 20 69 66 28 20 6e 78 44 69 76 3c 30 20 29  .  if( nxDiv<0 )
169d8 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b  {.    nxDiv = 0;
169d9 0a 20 20 7d 0a 20 20 6e 44 69 76 20 3d 20 30 3b  .  }.  nDiv = 0;
169da 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6b 3d 6e 78  .  for(i=0, k=nx
169db 44 69 76 3b 20 69 3c 4e 42 3b 20 69 2b 2b 2c 20  Div; i<NB; i++, 
169dc 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6b 3c  k++){.    if( k<
169dd 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29  pParent->nCell )
169de 7b 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d  {.      apDiv[i]
169df 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72   = findCell(pPar
169e0 65 6e 74 2c 20 6b 29 3b 0a 20 20 20 20 20 20 6e  ent, k);.      n
169e1 44 69 76 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73  Div++;.      ass
169e2 65 72 74 28 20 21 70 50 61 72 65 6e 74 2d 3e 6c  ert( !pParent->l
169e3 65 61 66 20 29 3b 0a 20 20 20 20 20 20 70 67 6e  eaf );.      pgn
169e4 6f 4f 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62 79  oOld[i] = get4by
169e5 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20  te(apDiv[i]);.  
169e6 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3d 3d 70    }else if( k==p
169e7 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b  Parent->nCell ){
169e8 0a 20 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69  .      pgnoOld[i
169e9 5d 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  ] = get4byte(&pP
169ea 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61  arent->aData[pPa
169eb 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b  rent->hdrOffset+
169ec 38 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  8]);.    }else{.
169ed 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
169ee 20 7d 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41   }.    rc = getA
169ef 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20  ndInitPage(pBt, 
169f0 70 67 6e 6f 4f 6c 64 5b 69 5d 2c 20 26 61 70 4f  pgnoOld[i], &apO
169f1 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20  ld[i]);.    if( 
169f2 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63  rc ) goto balanc
169f3 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 2f  e_cleanup;.    /
169f4 2a 20 61 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50  * apOld[i]->idxP
169f5 61 72 65 6e 74 20 3d 20 6b 3b 20 2a 2f 0a 20 20  arent = k; */.  
169f6 20 20 61 70 43 6f 70 79 5b 69 5d 20 3d 20 30 3b    apCopy[i] = 0;
169f7 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d  .    assert( i==
169f8 6e 4f 6c 64 20 29 3b 0a 20 20 20 20 6e 4f 6c 64  nOld );.    nOld
169f9 2b 2b 3b 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c  ++;.    nMaxCell
169fa 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d  s += 1+apOld[i]-
169fb 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d  >nCell+apOld[i]-
169fc 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a  >nOverflow;.  }.
169fd 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43  .  /* Make nMaxC
169fe 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20  ells a multiple 
169ff 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f  of 4 in order to
16a00 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74 65   preserve 8-byte
16a01 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20  .  ** alignment 
16a02 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d  */.  nMaxCells =
16a03 20 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29   (nMaxCells + 3)
16a04 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  &~3;..  /*.  ** 
16a05 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  Allocate space f
16a06 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74  or memory struct
16a07 75 72 65 73 0a 20 20 2a 2f 0a 20 20 73 7a 53 63  ures.  */.  szSc
16a08 72 61 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e  ratch =.       n
16a09 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28  MaxCells*sizeof(
16a0a 75 38 2a 29 20 20 20 20 20 20 20 20 20 20 20 20  u8*)            
16a0b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70             /* ap
16a0c 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e  Cell */.     + n
16a0d 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28  MaxCells*sizeof(
16a0e 75 31 36 29 20 20 20 20 20 20 20 20 20 20 20 20  u16)            
16a0f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a             /* sz
16a10 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 28  Cell */.     + (
16a11 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65  ROUND8(sizeof(Me
16a12 6d 50 61 67 65 29 29 2b 70 42 74 2d 3e 70 61 67  mPage))+pBt->pag
16a13 65 53 69 7a 65 29 2a 4e 42 20 20 2f 2a 20 61 43  eSize)*NB  /* aC
16a14 6f 70 79 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42  opy */.     + pB
16a15 74 2d 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20  t->pageSize     
16a16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a17 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70            /* aSp
16a18 61 63 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 28  ace1 */.     + (
16a19 49 53 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 6e  ISAUTOVACUUM ? n
16a1a 4d 61 78 43 65 6c 6c 73 20 3a 20 30 29 3b 20 20  MaxCells : 0);  
16a1b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 46             /* aF
16a1c 72 6f 6d 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20  rom */.  apCell 
16a1d 3d 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68  = sqlite3Scratch
16a1e 4d 61 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63  Malloc( szScratc
16a1f 68 20 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65  h ); .  if( apCe
16a20 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  ll==0 ){.    rc 
16a21 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
16a22 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65      goto balance
16a23 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
16a24 73 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26  szCell = (u16*)&
16a25 61 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73  apCell[nMaxCells
16a26 5d 3b 0a 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20  ];.  aCopy[0] = 
16a27 28 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61  (u8*)&szCell[nMa
16a28 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72  xCells];.  asser
16a29 74 28 20 28 28 61 43 6f 70 79 5b 30 5d 20 2d 20  t( ((aCopy[0] - 
16a2a 28 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20  (u8*)0) & 7)==0 
16a2b 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69  ); /* 8-byte ali
16a2c 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 64 20  gnment required 
16a2d 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  */.  for(i=1; i<
16a2e 4e 42 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 43  NB; i++){.    aC
16a2f 6f 70 79 5b 69 5d 20 3d 20 26 61 43 6f 70 79 5b  opy[i] = &aCopy[
16a30 69 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69  i-1][pBt->pageSi
16a31 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66  ze+ROUND8(sizeof
16a32 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 20  (MemPage))];.   
16a33 20 61 73 73 65 72 74 28 20 28 28 61 43 6f 70 79   assert( ((aCopy
16a34 5b 69 5d 20 2d 20 28 75 38 2a 29 30 29 20 26 20  [i] - (u8*)0) & 
16a35 37 29 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79  7)==0 ); /* 8-by
16a36 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71  te alignment req
16a37 75 69 72 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61  uired */.  }.  a
16a38 53 70 61 63 65 31 20 3d 20 26 61 43 6f 70 79 5b  Space1 = &aCopy[
16a39 4e 42 2d 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53  NB-1][pBt->pageS
16a3a 69 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f  ize+ROUND8(sizeo
16a3b 66 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20  f(MemPage))];.  
16a3c 61 73 73 65 72 74 28 20 28 28 61 53 70 61 63 65  assert( ((aSpace
16a3d 31 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29  1 - (u8*)0) & 7)
16a3e 3d 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65  ==0 ); /* 8-byte
16a3f 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69   alignment requi
16a40 72 65 64 20 2a 2f 0a 20 20 69 66 28 20 49 53 41  red */.  if( ISA
16a41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
16a42 20 61 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 65   aFrom = &aSpace
16a43 31 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d  1[pBt->pageSize]
16a44 3b 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 32 20  ;.  }.  aSpace2 
16a45 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c  = sqlite3PageMal
16a46 6c 6f 63 28 70 42 74 2d 3e 70 61 67 65 53 69 7a  loc(pBt->pageSiz
16a47 65 29 3b 0a 20 20 69 66 28 20 61 53 70 61 63 65  e);.  if( aSpace
16a48 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  2==0 ){.    rc =
16a49 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
16a4a 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f     goto balance_
16a4b 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 0a  cleanup;.  }.  .
16a4c 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63    /*.  ** Make c
16a4d 6f 70 69 65 73 20 6f 66 20 74 68 65 20 63 6f 6e  opies of the con
16a4e 74 65 6e 74 20 6f 66 20 70 50 61 67 65 20 61 6e  tent of pPage an
16a4f 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20 69  d its siblings i
16a50 6e 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a  nto aOld[]..  **
16a51 20 54 68 65 20 72 65 73 74 20 6f 66 20 74 68 69   The rest of thi
16a52 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20  s function will 
16a53 75 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68  use data from th
16a54 65 20 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a  e copies rather.
16a55 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72    ** that the or
16a56 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e  iginal pages sin
16a57 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  ce the original 
16a58 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e  pages will be in
16a59 20 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73   the.  ** proces
16a5a 73 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77  s of being overw
16a5b 72 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66  ritten..  */.  f
16a5c 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20  or(i=0; i<nOld; 
16a5d 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67  i++){.    MemPag
16a5e 65 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d  e *p = apCopy[i]
16a5f 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f   = (MemPage*)aCo
16a60 70 79 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70  py[i];.    memcp
16a61 79 28 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73  y(p, apOld[i], s
16a62 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b  izeof(MemPage));
16a63 0a 20 20 20 20 70 2d 3e 61 44 61 74 61 20 3d 20  .    p->aData = 
16a64 28 76 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20  (void*)&p[1];.  
16a65 20 20 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61 74    memcpy(p->aDat
16a66 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61  a, apOld[i]->aDa
16a67 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  ta, pBt->pageSiz
16a68 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20  e);.  }..  /*.  
16a69 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73  ** Load pointers
16a6a 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e   to all cells on
16a6b 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61   sibling pages a
16a6c 6e 64 20 74 68 65 20 64 69 76 69 64 65 72 20 63  nd the divider c
16a6d 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74  ells.  ** into t
16a6e 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b  he local apCell[
16a6f 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63  ] array.  Make c
16a70 6f 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 76  opies of the div
16a71 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20  ider cells.  ** 
16a72 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69  into space obtai
16a73 6e 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65 31  ned form aSpace1
16a74 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68  [] and remove th
16a75 65 20 74 68 65 20 64 69 76 69 64 65 72 20 43 65  e the divider Ce
16a76 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50  lls.  ** from pP
16a77 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  arent..  **.  **
16a78 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73   If the siblings
16a79 20 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67   are on leaf pag
16a7a 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69  es, then the chi
16a7b 6c 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74  ld pointers of t
16a7c 68 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20  he.  ** divider 
16a7d 63 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70  cells are stripp
16a7e 65 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c  ed from the cell
16a7f 73 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72  s before they ar
16a80 65 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e  e copied.  ** in
16a81 74 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49  to aSpace1[].  I
16a82 6e 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20  n this way, all 
16a83 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b  cells in apCell[
16a84 5d 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20  ] are without.  
16a85 2a 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72  ** child pointer
16a86 73 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20  s.  If siblings 
16a87 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20  are not leaves, 
16a88 74 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e  then all cell in
16a89 0a 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69  .  ** apCell[] i
16a8a 6e 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69  nclude child poi
16a8b 6e 74 65 72 73 2e 20 20 45 69 74 68 65 72 20 77  nters.  Either w
16a8c 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e  ay, all cells in
16a8d 20 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61   apCell[].  ** a
16a8e 72 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20  re alike..  **. 
16a8f 20 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69   ** leafCorrecti
16a90 6f 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65 20  on:  4 if pPage 
16a91 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66  is a leaf.  0 if
16a92 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20   pPage is not a 
16a93 6c 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20  leaf..  **      
16a94 20 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69 66   leafData:  1 if
16a95 20 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79   pPage holds key
16a96 2b 64 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e  +data and pParen
16a97 74 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79  t holds only key
16a98 73 2e 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20  s..  */.  nCell 
16a99 3d 20 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65  = 0;.  leafCorre
16a9a 63 74 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c  ction = pPage->l
16a9b 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74  eaf*4;.  leafDat
16a9c 61 20 3d 20 70 50 61 67 65 2d 3e 68 61 73 44 61  a = pPage->hasDa
16a9d 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ta;.  for(i=0; i
16a9e 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nOld; i++){.   
16a9f 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d   MemPage *pOld =
16aa0 20 61 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20   apCopy[i];.    
16aa1 69 6e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64  int limit = pOld
16aa2 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f  ->nCell+pOld->nO
16aa3 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72  verflow;.    for
16aa4 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a  (j=0; j<limit; j
16aa5 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
16aa6 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c  t( nCell<nMaxCel
16aa7 6c 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43 65  ls );.      apCe
16aa8 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64  ll[nCell] = find
16aa9 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c  OverflowCell(pOl
16aaa 64 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43  d, j);.      szC
16aab 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c  ell[nCell] = cel
16aac 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61  lSizePtr(pOld, a
16aad 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20  pCell[nCell]);. 
16aae 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56       if( ISAUTOV
16aaf 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20  ACUUM ){.       
16ab0 20 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 20   int a;.        
16ab1 61 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 28  aFrom[nCell] = (
16ab2 75 38 29 69 3b 20 20 20 61 73 73 65 72 74 28 20  u8)i;   assert( 
16ab3 69 3e 3d 30 20 26 26 20 69 3c 36 20 29 3b 0a 20  i>=0 && i<6 );. 
16ab4 20 20 20 20 20 20 20 66 6f 72 28 61 3d 30 3b 20         for(a=0; 
16ab5 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  a<pOld->nOverflo
16ab6 77 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 20  w; a++){.       
16ab7 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f 76     if( pOld->aOv
16ab8 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 43  fl[a].pCell==apC
16ab9 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 20  ell[nCell] ){.  
16aba 20 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b            aFrom[
16abb 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20  nCell] = 0xFF;. 
16abc 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
16abd 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
16abe 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
16abf 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20        nCell++;. 
16ac0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e     }.    if( i<n
16ac1 4f 6c 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 75  Old-1 ){.      u
16ac2 31 36 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65  16 sz = cellSize
16ac3 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44  Ptr(pParent, apD
16ac4 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66  iv[i]);.      if
16ac5 28 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20  ( leafData ){.  
16ac6 20 20 20 20 20 20 2f 2a 20 57 69 74 68 20 74 68        /* With th
16ac7 65 20 4c 45 41 46 44 41 54 41 20 66 6c 61 67 2c  e LEAFDATA flag,
16ac8 20 70 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 68   pParent cells h
16ac9 6f 6c 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 73  old only INTKEYs
16aca 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a   that.        **
16acb 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 20   are duplicates 
16acc 6f 66 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 63  of keys on the c
16acd 68 69 6c 64 20 70 61 67 65 73 2e 20 20 57 65 20  hild pages.  We 
16ace 6e 65 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a 20  need to remove. 
16acf 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 69         ** the di
16ad0 76 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d  vider cells from
16ad1 20 70 50 61 72 65 6e 74 2c 20 62 75 74 20 74 68   pParent, but th
16ad2 65 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73  e dividers cells
16ad3 20 61 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 20   are not.       
16ad4 20 2a 2a 20 61 64 64 65 64 20 74 6f 20 61 70 43   ** added to apC
16ad5 65 6c 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 68  ell[] because th
16ad6 65 79 20 61 72 65 20 64 75 70 6c 69 63 61 74 65  ey are duplicate
16ad7 73 20 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c 73  s of child cells
16ad8 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
16ad9 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50       dropCell(pP
16ada 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a  arent, nxDiv, sz
16adb 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
16adc 20 20 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d          u8 *pTem
16add 70 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  p;.        asser
16ade 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c  t( nCell<nMaxCel
16adf 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a  ls );.        sz
16ae0 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a  Cell[nCell] = sz
16ae1 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 20  ;.        pTemp 
16ae2 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 63  = &aSpace1[iSpac
16ae3 65 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 70  e1];.        iSp
16ae4 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20  ace1 += sz;.    
16ae5 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d      assert( sz<=
16ae6 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20  pBt->pageSize/4 
16ae7 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
16ae8 74 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d  t( iSpace1<=pBt-
16ae9 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20  >pageSize );.   
16aea 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d       memcpy(pTem
16aeb 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29  p, apDiv[i], sz)
16aec 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c  ;.        apCell
16aed 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b  [nCell] = pTemp+
16aee 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a  leafCorrection;.
16aef 20 20 20 20 20 20 20 20 69 66 28 20 49 53 41 55          if( ISAU
16af0 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
16af1 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c        aFrom[nCel
16af2 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 20  l] = 0xFF;.     
16af3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 72 6f     }.        dro
16af4 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e  pCell(pParent, n
16af5 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20  xDiv, sz);.     
16af6 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43     assert( leafC
16af7 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20  orrection==0 || 
16af8 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d  leafCorrection==
16af9 34 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43  4 );.        szC
16afa 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 28 75  ell[nCell] -= (u
16afb 31 36 29 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  16)leafCorrectio
16afc 6e 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  n;.        asser
16afd 74 28 20 67 65 74 34 62 79 74 65 28 70 54 65 6d  t( get4byte(pTem
16afe 70 29 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20 29  p)==pgnoOld[i] )
16aff 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  ;.        if( !p
16b00 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  Old->leaf ){.   
16b01 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c         assert( l
16b02 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30  eafCorrection==0
16b03 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   );.          /*
16b04 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e 74   The right point
16b05 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  er of the child 
16b06 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65  page pOld become
16b07 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20 20  s the left.     
16b08 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20       ** pointer 
16b09 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63  of the divider c
16b0a 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ell */.         
16b0b 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e   memcpy(apCell[n
16b0c 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44  Cell], &pOld->aD
16b0d 61 74 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66 66  ata[pOld->hdrOff
16b0e 73 65 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20 20  set+8], 4);.    
16b0f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
16b10 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61       assert( lea
16b11 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29  fCorrection==4 )
16b12 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
16b13 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20  szCell[nCell]<4 
16b14 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  ){.            /
16b15 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61  * Do not allow a
16b16 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 72  ny cells smaller
16b17 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20 2a   than 4 bytes. *
16b18 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 7a  /.            sz
16b19 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b  Cell[nCell] = 4;
16b1a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
16b1b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e       }.        n
16b1c 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  Cell++;.      }.
16b1d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a      }.  }..  /*.
16b1e 20 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20    ** Figure out 
16b1f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
16b20 67 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68 6f  ges needed to ho
16b21 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c  ld all nCell cel
16b22 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 74  ls..  ** Store t
16b23 68 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 6b  his number in "k
16b24 22 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 65  ".  Also compute
16b25 20 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 69   szNew[] which i
16b26 73 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a 2a  s the total.  **
16b27 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c   size of all cel
16b28 6c 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 70  ls on the i-th p
16b29 61 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b 5d  age and cntNew[]
16b2a 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 6e   which is the in
16b2b 64 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65  dex.  ** in apCe
16b2c 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c  ll[] of the cell
16b2d 20 74 68 61 74 20 64 69 76 69 64 65 73 20 70 61   that divides pa
16b2e 67 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20 69  ge i from page i
16b2f 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e 65  +1.  .  ** cntNe
16b30 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 61  w[k] should equa
16b31 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20  l nCell..  **.  
16b32 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 74  ** Values comput
16b33 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 6b  ed by this block
16b34 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
16b35 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f 74        k: The tot
16b36 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62  al number of sib
16b37 6c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a 20  ling pages.  ** 
16b38 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 61     szNew[i]: Spa
16b39 63 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65 20  ced used on the 
16b3a 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67  i-th sibling pag
16b3b 65 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 77  e..  **   cntNew
16b3c 5b 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 70  [i]: Index in ap
16b3d 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 6c  Cell[] and szCel
16b3e 6c 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73  l[] for the firs
16b3f 74 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20  t cell to.  **  
16b40 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
16b41 72 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d 74  right of the i-t
16b42 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a  h sibling page..
16b43 20 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 65    ** usableSpace
16b44 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  : Number of byte
16b45 73 20 6f 66 20 73 70 61 63 65 20 61 76 61 69 6c  s of space avail
16b46 61 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 62  able on each sib
16b47 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f  ling..  ** .  */
16b48 0a 20 20 75 73 61 62 6c 65 53 70 61 63 65 20 3d  .  usableSpace =
16b49 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
16b4a 20 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 72   - 12 + leafCorr
16b4b 65 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 75  ection;.  for(su
16b4c 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c  btotal=k=i=0; i<
16b4d 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
16b4e 20 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 43   assert( i<nMaxC
16b4f 65 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 74  ells );.    subt
16b50 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69  otal += szCell[i
16b51 5d 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 73  ] + 2;.    if( s
16b52 75 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65  ubtotal > usable
16b53 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20 73  Space ){.      s
16b54 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74  zNew[k] = subtot
16b55 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a  al - szCell[i];.
16b56 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20        cntNew[k] 
16b57 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20 6c  = i;.      if( l
16b58 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20  eafData ){ i--; 
16b59 7d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61 6c  }.      subtotal
16b5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b   = 0;.      k++;
16b5b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e  .    }.  }.  szN
16b5c 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c  ew[k] = subtotal
16b5d 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20  ;.  cntNew[k] = 
16b5e 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20  nCell;.  k++;.. 
16b5f 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63   /*.  ** The pac
16b60 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79  king computed by
16b61 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c   the previous bl
16b62 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f  ock is biased to
16b63 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67  ward the sibling
16b64 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65  s.  ** on the le
16b65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65  ft side.  The le
16b66 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20  ft siblings are 
16b67 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75  always nearly fu
16b68 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20  ll, while the.  
16b69 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69  ** right-most si
16b6a 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e  bling might be n
16b6b 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68  early empty.  Th
16b6c 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65  is block of code
16b6d 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74   attempts.  ** t
16b6e 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70 61 63  o adjust the pac
16b6f 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73  king of siblings
16b70 20 74 6f 20 67 65 74 20 61 20 62 65 74 74 65 72   to get a better
16b71 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20   balance..  **. 
16b72 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d   ** This adjustm
16b73 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e  ent is more than
16b74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
16b75 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61  .  The packing a
16b76 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20  bove might.  ** 
16b77 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c  be so out of bal
16b78 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c  ance as to be il
16b79 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d  legal.  For exam
16b7a 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d  ple, the right-m
16b7b 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67  ost.  ** sibling
16b7c 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65   might be comple
16b7d 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69  tely empty.  Thi
16b7e 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20  s adjustment is 
16b7f 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20  not optional..  
16b80 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20  */.  for(i=k-1; 
16b81 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69  i>0; i--){.    i
16b82 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e  nt szRight = szN
16b83 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20  ew[i];  /* Size 
16b84 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68  of sibling on th
16b85 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69  e right */.    i
16b86 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65  nt szLeft = szNe
16b87 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20  w[i-1]; /* Size 
16b88 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68  of sibling on th
16b89 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e  e left */.    in
16b8a 74 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  t r;            
16b8b 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69    /* Index of ri
16b8c 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e  ght-most cell in
16b8d 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f   left sibling */
16b8e 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20  .    int d;     
16b8f 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
16b90 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20  x of first cell 
16b91 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72  to the left of r
16b92 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a  ight sibling */.
16b93 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b  .    r = cntNew[
16b94 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20  i-1] - 1;.    d 
16b95 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61  = r + 1 - leafDa
16b96 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ta;.    assert( 
16b97 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  d<nMaxCells );. 
16b98 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61     assert( r<nMa
16b99 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68  xCells );.    wh
16b9a 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20  ile( szRight==0 
16b9b 7c 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 6c  || szRight+szCel
16b9c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28  l[d]+2<=szLeft-(
16b9d 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a  szCell[r]+2) ){.
16b9e 20 20 20 20 20 20 73 7a 52 69 67 68 74 20 2b 3d        szRight +=
16b9f 20 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a   szCell[d] + 2;.
16ba0 20 20 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d 20        szLeft -= 
16ba1 73 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20  szCell[r] + 2;. 
16ba2 20 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 5d       cntNew[i-1]
16ba3 2d 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 6e  --;.      r = cn
16ba4 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20  tNew[i-1] - 1;. 
16ba5 20 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d       d = r + 1 -
16ba6 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 7d   leafData;.    }
16ba7 0a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20  .    szNew[i] = 
16ba8 73 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 7a 4e  szRight;.    szN
16ba9 65 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 74  ew[i-1] = szLeft
16baa 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 68  ;.  }..  /* Eith
16bab 65 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20  er we found one 
16bac 6f 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63  or more cells (c
16bad 6e 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20  ntnew[0])>0) or 
16bae 77 65 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20  we are the.  ** 
16baf 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70  a virtual root p
16bb0 61 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20  age.  A virtual 
16bb1 72 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68 65  root page is whe
16bb2 6e 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0a  n the real root.
16bb3 20 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67    ** page is pag
16bb4 65 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 74  e 1 and we are t
16bb5 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66  he only child of
16bb6 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a 2f   that page..  */
16bb7 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e 65  .  assert( cntNe
16bb8 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 65  w[0]>0 || (pPare
16bb9 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 70  nt->pgno==1 && p
16bba 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30  Parent->nCell==0
16bbb 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ) );..  /*.  ** 
16bbc 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70  Allocate k new p
16bbd 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64  ages.  Reuse old
16bbe 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f 73   pages where pos
16bbf 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73  sible..  */.  as
16bc0 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e  sert( pPage->pgn
16bc1 6f 3e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c 61  o>1 );.  pageFla
16bc2 67 73 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  gs = pPage->aDat
16bc3 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  a[0];.  for(i=0;
16bc4 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<k; i++){.    
16bc5 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20  MemPage *pNew;. 
16bc6 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b     if( i<nOld ){
16bc7 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70  .      pNew = ap
16bc8 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69  New[i] = apOld[i
16bc9 5d 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77  ];.      pgnoNew
16bca 5b 69 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69 5d  [i] = pgnoOld[i]
16bcb 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d  ;.      apOld[i]
16bcc 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
16bcd 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
16bce 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65  te(pNew->pDbPage
16bcf 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b  );.      nNew++;
16bd0 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
16bd1 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
16bd2 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  anup;.    }else{
16bd3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
16bd4 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  >0 );.      rc =
16bd5 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
16bd6 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26  ge(pBt, &pNew, &
16bd7 70 67 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e 6f  pgnoNew[i], pgno
16bd8 4e 65 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20 20  New[i-1], 0);.  
16bd9 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
16bda 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
16bdb 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69  p;.      apNew[i
16bdc 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20  ] = pNew;.      
16bdd 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  nNew++;.    }.  
16bde 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79  }..  /* Free any
16bdf 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74 20   old pages that 
16be0 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64 20  were not reused 
16be1 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20  as new pages..  
16be2 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f  */.  while( i<nO
16be3 6c 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66  ld ){.    rc = f
16be4 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d  reePage(apOld[i]
16be5 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
16be6 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
16be7 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 73  anup;.    releas
16be8 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b  ePage(apOld[i]);
16be9 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20  .    apOld[i] = 
16bea 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a  0;.    i++;.  }.
16beb 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74  .  /*.  ** Put t
16bec 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e 20  he new pages in 
16bed 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e  accending order.
16bee 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f 0a    This helps to.
16bef 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 65    ** keep entrie
16bf0 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69  s in the disk fi
16bf1 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 74  le in order so t
16bf2 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20  hat a scan.  ** 
16bf3 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  of the table is 
16bf4 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68  a linear scan th
16bf5 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e 20  rough the file. 
16bf6 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75   That.  ** in tu
16bf7 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70 65  rn helps the ope
16bf8 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74 6f  rating system to
16bf9 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a 20   deliver pages. 
16bfa 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 73   ** from the dis
16bfb 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a  k more rapidly..
16bfc 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e    **.  ** An O(n
16bfd 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f  ^2) insertion so
16bfe 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20  rt algorithm is 
16bff 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65 0a  used, but since.
16c00 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 20    ** n is never 
16c01 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61 20  more than NB (a 
16c02 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c  small constant),
16c03 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a   that should.  *
16c04 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c  * not be a probl
16c05 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68  em..  **.  ** Wh
16c06 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f  en NB==3, this o
16c07 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  ne optimization 
16c08 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 61  makes the databa
16c09 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 35  se.  ** about 25
16c0a 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 72  % faster for lar
16c0b 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e  ge insertions an
16c0c 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a  d deletions..  *
16c0d 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b  /.  for(i=0; i<k
16c0e 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  -1; i++){.    in
16c0f 74 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77  t minV = pgnoNew
16c10 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e  [i];.    int min
16c11 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a  I = i;.    for(j
16c12 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b  =i+1; j<k; j++){
16c13 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 4e  .      if( pgnoN
16c14 65 77 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64 29  ew[j]<(unsigned)
16c15 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20  minV ){.        
16c16 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20  minI = j;.      
16c17 20 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77    minV = pgnoNew
16c18 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [j];.      }.   
16c19 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 3e   }.    if( minI>
16c1a 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74  i ){.      int t
16c1b 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20  ;.      MemPage 
16c1c 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 70  *pT;.      t = p
16c1d 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20  gnoNew[i];.     
16c1e 20 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a   pT = apNew[i];.
16c1f 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d        pgnoNew[i]
16c20 20 3d 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d   = pgnoNew[minI]
16c21 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d  ;.      apNew[i]
16c22 20 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b 0a   = apNew[minI];.
16c23 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d 69        pgnoNew[mi
16c24 6e 49 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20 61  nI] = t;.      a
16c25 70 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b  pNew[minI] = pT;
16c26 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41  .    }.  }.  TRA
16c27 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c  CE(("BALANCE: ol
16c28 64 3a 20 25 64 20 25 64 20 25 64 20 20 6e 65 77  d: %d %d %d  new
16c29 3a 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20  : %d(%d) %d(%d) 
16c2a 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64  %d(%d) %d(%d) %d
16c2b 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67 6e  (%d)\n",.    pgn
16c2c 6f 4f 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e 4f  oOld[0], .    nO
16c2d 6c 64 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64 5b  ld>=2 ? pgnoOld[
16c2e 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64  1] : 0,.    nOld
16c2f 3e 3d 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32 5d  >=3 ? pgnoOld[2]
16c30 20 3a 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e 65   : 0,.    pgnoNe
16c31 77 5b 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a  w[0], szNew[0],.
16c32 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70 67      nNew>=2 ? pg
16c33 6e 6f 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e 4e  noNew[1] : 0, nN
16c34 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d  ew>=2 ? szNew[1]
16c35 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d   : 0,.    nNew>=
16c36 33 20 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20 3a  3 ? pgnoNew[2] :
16c37 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a   0, nNew>=3 ? sz
16c38 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20  New[2] : 0,.    
16c39 6e 4e 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e 65  nNew>=4 ? pgnoNe
16c3a 77 5b 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d  w[3] : 0, nNew>=
16c3b 34 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30  4 ? szNew[3] : 0
16c3c 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20  ,.    nNew>=5 ? 
16c3d 70 67 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c 20  pgnoNew[4] : 0, 
16c3e 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b  nNew>=5 ? szNew[
16c3f 34 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a 0a  4] : 0));..  /*.
16c40 20 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74    ** Evenly dist
16c41 72 69 62 75 74 65 20 74 68 65 20 64 61 74 61 20  ribute the data 
16c42 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f  in apCell[] acro
16c43 73 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 73  ss the new pages
16c44 2e 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69  ..  ** Insert di
16c45 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f  vider cells into
16c46 20 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65   pParent as nece
16c47 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20  ssary..  */.  j 
16c48 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
16c49 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20  i<nNew; i++){.  
16c4a 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68    /* Assemble th
16c4b 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61  e new sibling pa
16c4c 67 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61  ge. */.    MemPa
16c4d 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77  ge *pNew = apNew
16c4e 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  [i];.    assert(
16c4f 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a   j<nMaxCells );.
16c50 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77      assert( pNew
16c51 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77 5b  ->pgno==pgnoNew[
16c52 69 5d 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61  i] );.    zeroPa
16c53 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c 61  ge(pNew, pageFla
16c54 67 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c  gs);.    assembl
16c55 65 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e  ePage(pNew, cntN
16c56 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c  ew[i]-j, &apCell
16c57 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29  [j], &szCell[j])
16c58 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  ;.    assert( pN
16c59 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28  ew->nCell>0 || (
16c5a 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65  nNew==1 && cntNe
16c5b 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 20  w[0]==0) );.    
16c5c 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f  assert( pNew->nO
16c5d 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20  verflow==0 );.. 
16c5e 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
16c5f 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   an auto-vacuum 
16c60 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65  database, update
16c61 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
16c62 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20   entries.    ** 
16c63 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68  that point to th
16c64 65 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20  e siblings that 
16c65 77 65 72 65 20 72 65 61 72 72 61 6e 67 65 64 2e  were rearranged.
16c66 20 54 68 65 73 65 20 63 61 6e 20 62 65 3a 20 6c   These can be: l
16c67 65 66 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64  eft.    ** child
16c68 72 65 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74 68  ren of cells, th
16c69 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66  e right-child of
16c6a 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 6f 76   the page, or ov
16c6b 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20  erflow pages.   
16c6c 20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62   ** pointed to b
16c6d 79 20 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f 0a  y cells..    */.
16c6e 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
16c6f 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 66 6f  CUUM ){.      fo
16c70 72 28 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b  r(k=j; k<cntNew[
16c71 69 5d 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; k++){.      
16c72 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 78    assert( k<nMax
16c73 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20  Cells );.       
16c74 20 69 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30   if( aFrom[k]==0
16c75 78 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 46  xFF || apCopy[aF
16c76 72 6f 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70  rom[k]]->pgno!=p
16c77 4e 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20  New->pgno ){.   
16c78 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d         rc = ptrm
16c79 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c 20  apPutOvfl(pNew, 
16c7a 6b 2d 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 20  k-j);.          
16c7b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16c7c 4b 20 26 26 20 6c 65 61 66 43 6f 72 72 65 63 74  K && leafCorrect
16c7d 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ion==0 ){.      
16c7e 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
16c7f 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62 79  pPut(pBt, get4by
16c80 74 65 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20 50  te(apCell[k]), P
16c81 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65  TRMAP_BTREE, pNe
16c82 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  w->pgno);.      
16c83 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
16c84 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16c85 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
16c86 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
16c87 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 20  eanup;.         
16c88 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
16c89 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
16c8a 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a  j = cntNew[i];..
16c8b 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 69      /* If the si
16c8c 62 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65 6d  bling page assem
16c8d 62 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20 6e  bled above was n
16c8e 6f 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  ot the right-mos
16c8f 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 2a  t sibling,.    *
16c90 2a 20 69 6e 73 65 72 74 20 61 20 64 69 76 69 64  * insert a divid
16c91 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65  er cell into the
16c92 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20   parent page..  
16c93 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c 6e    */.    if( i<n
16c94 4e 65 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c 6c  New-1 && j<nCell
16c95 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43   ){.      u8 *pC
16c96 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70  ell;.      u8 *p
16c97 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20  Temp;.      int 
16c98 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72  sz;..      asser
16c99 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( j<nMaxCells )
16c9a 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20  ;.      pCell = 
16c9b 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20  apCell[j];.     
16c9c 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20   sz = szCell[j] 
16c9d 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  + leafCorrection
16c9e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20  ;.      pTemp = 
16c9f 26 61 53 70 61 63 65 32 5b 69 53 70 61 63 65 32  &aSpace2[iSpace2
16ca0 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e  ];.      if( !pN
16ca1 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ew->leaf ){.    
16ca2 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77      memcpy(&pNew
16ca3 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 6c  ->aData[8], pCel
16ca4 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 69  l, 4);.        i
16ca5 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  f( ISAUTOVACUUM 
16ca6 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 61 46  .         && (aF
16ca7 72 6f 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c 20  rom[j]==0xFF || 
16ca8 61 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d 5d  apCopy[aFrom[j]]
16ca9 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67  ->pgno!=pNew->pg
16caa 6e 6f 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20  no).        ){. 
16cab 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74           rc = pt
16cac 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 74  rmapPut(pBt, get
16cad 34 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 50 54  4byte(pCell), PT
16cae 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77  RMAP_BTREE, pNew
16caf 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
16cb0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
16cb1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
16cb2 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65      goto balance
16cb3 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20  _cleanup;.      
16cb4 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
16cb5 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
16cb6 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20  leafData ){.    
16cb7 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72      /* If the tr
16cb8 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74  ee is a leaf-dat
16cb9 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20  a tree, and the 
16cba 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61  siblings are lea
16cbb 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a  ves, .        **
16cbc 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e   then there is n
16cbd 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69  o divider cell i
16cbe 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74  n apCell[]. Inst
16cbf 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 72  ead, the divider
16cc0 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c   .        ** cel
16cc1 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68  l consists of th
16cc2 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f  e integer key fo
16cc3 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  r the right-most
16cc4 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20   cell of .      
16cc5 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67    ** the sibling
16cc6 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20  -page assembled 
16cc7 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20  above only..    
16cc8 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43      */.        C
16cc9 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
16cca 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20        j--;.     
16ccb 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50     sqlite3BtreeP
16ccc 61 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 77  arseCellPtr(pNew
16ccd 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e  , apCell[j], &in
16cce 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43 65  fo);.        pCe
16ccf 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20  ll = pTemp;.    
16cd0 20 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43      rc = fillInC
16cd1 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 43 65  ell(pParent, pCe
16cd2 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79  ll, 0, info.nKey
16cd3 2c 20 30 2c 20 30 2c 20 30 2c 20 26 73 7a 29 3b  , 0, 0, 0, &sz);
16cd4 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
16cd5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16cd6 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c          goto bal
16cd7 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
16cd8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16cd9 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20  pTemp = 0;.     
16cda 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
16cdb 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20  pCell -= 4;.    
16cdc 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63      /* Obscure c
16cdd 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66  ase for non-leaf
16cde 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20  -data trees: If 
16cdf 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c  the cell at pCel
16ce0 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a  l was.        **
16ce1 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72   previously stor
16ce2 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64  ed on a leaf nod
16ce3 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72  e, and its repor
16ce4 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20  ted size was 4. 
16ce5 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c         ** bytes,
16ce6 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74   then it may act
16ce7 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72  ually be smaller
16ce8 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20   than this .    
16ce9 20 20 20 20 2a 2a 20 28 73 65 65 20 73 71 6c 69      ** (see sqli
16cea 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
16ceb 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65 73 20  lPtr(), 4 bytes 
16cec 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 73  is the minimum s
16ced 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20 2a  ize of.        *
16cee 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75 74  * any cell). But
16cef 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   it is important
16cf0 20 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f 72   to pass the cor
16cf1 72 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20 20  rect size to .  
16cf2 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 43        ** insertC
16cf3 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72 73  ell(), so repars
16cf4 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a  e the cell now..
16cf5 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
16cf6 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20     ** Note that 
16cf7 74 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20 68  this can never h
16cf8 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c 69  appen in an SQLi
16cf9 74 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61 73  te data file, as
16cfa 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20   all.        ** 
16cfb 63 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65 61  cells are at lea
16cfc 73 74 20 34 20 62 79 74 65 73 2e 20 49 74 20 6f  st 4 bytes. It o
16cfd 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20 62  nly happens in b
16cfe 2d 74 72 65 65 73 20 75 73 65 64 0a 20 20 20 20  -trees used.    
16cff 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75 61      ** to evalua
16d00 74 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e  te "IN (SELECT .
16d01 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61 72  ..)" and similar
16d02 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20 20   clauses..      
16d03 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28    */.        if(
16d04 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b   szCell[j]==4 ){
16d05 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
16d06 74 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  t(leafCorrection
16d07 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ==4);.          
16d08 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  sz = cellSizePtr
16d09 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29  (pParent, pCell)
16d0a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
16d0b 20 20 7d 0a 20 20 20 20 20 20 69 53 70 61 63 65    }.      iSpace
16d0c 32 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61  2 += sz;.      a
16d0d 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e  ssert( sz<=pBt->
16d0e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 20  pageSize/4 );.  
16d0f 20 20 20 20 61 73 73 65 72 74 28 20 69 53 70 61      assert( iSpa
16d10 63 65 32 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69  ce2<=pBt->pageSi
16d11 7a 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ze );.      rc =
16d12 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72   insertCell(pPar
16d13 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c  ent, nxDiv, pCel
16d14 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 34 29  l, sz, pTemp, 4)
16d15 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
16d16 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
16d17 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
16d18 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
16d19 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
16d1a 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d  iteable(pParent-
16d1b 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20  >pDbPage) );.   
16d1c 20 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64     put4byte(find
16d1d 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61  OverflowCell(pPa
16d1e 72 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e 65  rent,nxDiv), pNe
16d1f 77 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 20  w->pgno);..     
16d20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
16d21 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61  n auto-vacuum da
16d22 74 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74 20  tabase, and not 
16d23 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65  a leaf-data tree
16d24 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20  ,.      ** then 
16d25 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74  update the point
16d26 65 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20 65  er map with an e
16d27 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65  ntry for the ove
16d28 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20  rflow page.     
16d29 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65 6c   ** that the cel
16d2a 6c 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20  l just inserted 
16d2b 70 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61 6e  points to (if an
16d2c 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  y)..      */.   
16d2d 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
16d2e 55 55 4d 20 26 26 20 21 6c 65 61 66 44 61 74 61  UUM && !leafData
16d2f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
16d30 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70   ptrmapPutOvfl(p
16d31 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b 0a  Parent, nxDiv);.
16d32 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
16d33 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16d34 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61         goto bala
16d35 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
16d36 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
16d37 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20       j++;.      
16d38 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  nxDiv++;.    }..
16d39 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 70      /* Set the p
16d3a 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79  ointer-map entry
16d3b 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 62   for the new sib
16d3c 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 20  ling page. */.  
16d3d 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
16d3e 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  UM ){.      rc =
16d3f 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
16d40 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d  pNew->pgno, PTRM
16d41 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e  AP_BTREE, pParen
16d42 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  t->pgno);.      
16d43 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16d44 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
16d45 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
16d46 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  p;.      }.    }
16d47 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a  .  }.  assert( j
16d48 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73  ==nCell );.  ass
16d49 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20  ert( nOld>0 );. 
16d4a 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 20   assert( nNew>0 
16d4b 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 6c  );.  if( (pageFl
16d4c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 3d  ags & PTF_LEAF)=
16d4d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43  =0 ){.    u8 *zC
16d4e 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b 6e  hild = &apCopy[n
16d4f 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d  Old-1]->aData[8]
16d50 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 70  ;.    memcpy(&ap
16d51 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61  New[nNew-1]->aDa
16d52 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 34  ta[8], zChild, 4
16d53 29 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55 54  );.    if( ISAUT
16d54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20  OVACUUM ){.     
16d55 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28   rc = ptrmapPut(
16d56 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 7a 43  pBt, get4byte(zC
16d57 68 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f 42 54  hild), PTRMAP_BT
16d58 52 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d  REE, apNew[nNew-
16d59 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  1]->pgno);.     
16d5a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16d5b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
16d5c 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
16d5d 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  up;.      }.    
16d5e 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
16d5f 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
16d60 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d  iteable(pParent-
16d61 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69  >pDbPage) );.  i
16d62 66 28 20 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e  f( nxDiv==pParen
16d63 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65 6e 74  t->nCell+pParent
16d64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20  ->nOverflow ){. 
16d65 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74     /* Right-most
16d66 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20   sibling is the 
16d67 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64  right-most child
16d68 20 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20   of pParent */. 
16d69 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61     put4byte(&pPa
16d6a 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72  rent->aData[pPar
16d6b 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  ent->hdrOffset+8
16d6c 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d  ], pgnoNew[nNew-
16d6d 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  1]);.  }else{.  
16d6e 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20    /* Right-most 
16d6f 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 6c  sibling is the l
16d70 65 66 74 20 63 68 69 6c 64 20 6f 66 20 74 68 65  eft child of the
16d71 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20   first entry in 
16d72 70 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a 20 70  pParent.    ** p
16d73 61 73 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f  ast the right-mo
16d74 73 74 20 64 69 76 69 64 65 72 20 65 6e 74 72 79  st divider entry
16d75 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65   */.    put4byte
16d76 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c  (findOverflowCel
16d77 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76  l(pParent, nxDiv
16d78 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d  ), pgnoNew[nNew-
16d79 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  1]);.  }..  /*. 
16d7a 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68 65 20   ** Balance the 
16d7b 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20 4e 6f  parent page.  No
16d7c 74 65 20 74 68 61 74 20 74 68 65 20 63 75 72 72  te that the curr
16d7d 65 6e 74 20 70 61 67 65 20 28 70 50 61 67 65 29  ent page (pPage)
16d7e 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61 76 65   might.  ** have
16d7f 20 62 65 65 6e 20 61 64 64 65 64 20 74 6f 20 74   been added to t
16d80 68 65 20 66 72 65 65 6c 69 73 74 20 73 6f 20 69  he freelist so i
16d81 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e 67 65  t might no longe
16d82 72 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  r be initialized
16d83 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 70  ..  ** But the p
16d84 61 72 65 6e 74 20 70 61 67 65 20 77 69 6c 6c 20  arent page will 
16d85 61 6c 77 61 79 73 20 62 65 20 69 6e 69 74 69 61  always be initia
16d86 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73  lized..  */.  as
16d87 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 69  sert( pParent->i
16d88 73 49 6e 69 74 20 29 3b 0a 20 20 73 71 6c 69 74  sInit );.  sqlit
16d89 65 33 53 63 72 61 74 63 68 46 72 65 65 28 61 70  e3ScratchFree(ap
16d8a 43 65 6c 6c 29 3b 0a 20 20 61 70 43 65 6c 6c 20  Cell);.  apCell 
16d8b 3d 20 30 3b 0a 20 20 54 52 41 43 45 28 28 22 42  = 0;.  TRACE(("B
16d8c 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 64  ALANCE: finished
16d8d 20 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d 25 64   with %d: old=%d
16d8e 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64   new=%d cells=%d
16d8f 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  \n",.          p
16d90 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c 64  Page->pgno, nOld
16d91 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b  , nNew, nCell));
16d92 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  .  pPage->nOverf
16d93 6c 6f 77 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61  low = 0;.  relea
16d94 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
16d95 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a   pCur->iPage--;.
16d96 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70    rc = balance(p
16d97 43 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a  Cur, 0);.  .  /*
16d98 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65  .  ** Cleanup be
16d99 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
16d9a 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65    */.balance_cle
16d9b 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 50  anup:.  sqlite3P
16d9c 61 67 65 46 72 65 65 28 61 53 70 61 63 65 32 29  ageFree(aSpace2)
16d9d 3b 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 74  ;.  sqlite3Scrat
16d9e 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a  chFree(apCell);.
16d9f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c    for(i=0; i<nOl
16da0 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c  d; i++){.    rel
16da1 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69  easePage(apOld[i
16da2 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  ]);.  }.  for(i=
16da3 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b  0; i<nNew; i++){
16da4 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
16da5 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a  (apNew[i]);.  }.
16da6 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70    pCur->apPage[p
16da7 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76  Cur->iPage]->nOv
16da8 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 72  erflow = 0;..  r
16da9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
16daa 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
16dab 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 68  is called for th
16dac 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61  e root page of a
16dad 20 62 74 72 65 65 20 77 68 65 6e 20 74 68 65 20   btree when the 
16dae 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e  root.** page con
16daf 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20  tains no cells. 
16db0 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 70 6f   This is an oppo
16db1 72 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20  rtunity to make 
16db2 74 68 65 20 74 72 65 65 0a 2a 2a 20 73 68 61 6c  the tree.** shal
16db3 6c 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c 65 76  lower by one lev
16db4 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  el..*/.static in
16db5 74 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f  t balance_shallo
16db6 77 65 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  wer(BtCursor *pC
16db7 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ur){.  MemPage *
16db8 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  pPage;          
16db9 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65      /* Root page
16dba 20 6f 66 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20   of B-Tree */.  
16dbb 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b  MemPage *pChild;
16dbc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16dbd 54 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 70  The only child p
16dbe 61 67 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a  age of pPage */.
16dbf 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64    Pgno pgnoChild
16dc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
16dc1 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  * Page number fo
16dc2 72 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69 6e  r pChild */.  in
16dc3 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
16dc4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  ;          /* Re
16dc5 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73  turn code from s
16dc6 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a  ubprocedures */.
16dc7 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b    BtShared *pBt;
16dc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16dc9 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42 54    /* The main BT
16dca 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ree structure */
16dcb 0a 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65 72  .  int mxCellPer
16dcc 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
16dcd 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  /* Maximum numbe
16dce 72 20 6f 66 20 63 65 6c 6c 73 20 70 65 72 20 70  r of cells per p
16dcf 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70  age */.  u8 **ap
16dd0 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
16dd1 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c        /* All cel
16dd2 6c 73 20 66 72 6f 6d 20 70 61 67 65 73 20 62 65  ls from pages be
16dd3 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a  ing balanced */.
16dd4 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20    u16 *szCell;  
16dd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16dd6 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20  * Local size of 
16dd7 61 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20 20  all cells */..  
16dd8 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50  assert( pCur->iP
16dd9 61 67 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67  age==0 );.  pPag
16dda 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
16ddb 5b 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  [0];..  assert( 
16ddc 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20  pPage->nCell==0 
16ddd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
16dde 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
16ddf 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
16de0 78 29 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50  x) );.  pBt = pP
16de1 61 67 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 43 65  age->pBt;.  mxCe
16de2 6c 6c 50 65 72 50 61 67 65 20 3d 20 4d 58 5f 43  llPerPage = MX_C
16de3 45 4c 4c 28 70 42 74 29 3b 0a 20 20 61 70 43 65  ELL(pBt);.  apCe
16de4 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ll = sqlite3Mall
16de5 6f 63 28 20 6d 78 43 65 6c 6c 50 65 72 50 61 67  oc( mxCellPerPag
16de6 65 2a 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b 73  e*(sizeof(u8*)+s
16de7 69 7a 65 6f 66 28 75 31 36 29 29 20 29 3b 0a 20  izeof(u16)) );. 
16de8 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29   if( apCell==0 )
16de9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
16dea 4f 4d 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20 3d  OMEM;.  szCell =
16deb 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6d   (u16*)&apCell[m
16dec 78 43 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a 20  xCellPerPage];. 
16ded 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66   if( pPage->leaf
16dee 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74   ){.    /* The t
16def 61 62 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74 65  able is complete
16df0 6c 79 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20  ly empty */.    
16df1 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a  TRACE(("BALANCE:
16df2 20 65 6d 70 74 79 20 74 61 62 6c 65 20 25 64 5c   empty table %d\
16df3 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29  n", pPage->pgno)
16df4 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
16df5 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65  /* The root page
16df6 20 69 73 20 65 6d 70 74 79 20 62 75 74 20 68 61   is empty but ha
16df7 73 20 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54 72  s one child.  Tr
16df8 61 6e 73 66 65 72 20 74 68 65 0a 20 20 20 20 2a  ansfer the.    *
16df9 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  * information fr
16dfa 6f 6d 20 74 68 61 74 20 6f 6e 65 20 63 68 69 6c  om that one chil
16dfb 64 20 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74 20  d into the root 
16dfc 70 61 67 65 20 69 66 20 69 74 20 0a 20 20 20 20  page if it .    
16dfd 2a 2a 20 77 69 6c 6c 20 66 69 74 2e 20 20 54 68  ** will fit.  Th
16dfe 69 73 20 72 65 64 75 63 65 73 20 74 68 65 20 64  is reduces the d
16dff 65 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65  epth of the tree
16e00 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a   by one..    **.
16e01 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 72 6f      ** If the ro
16e02 6f 74 20 70 61 67 65 20 69 73 20 70 61 67 65 20  ot page is page 
16e03 31 2c 20 69 74 20 68 61 73 20 6c 65 73 73 20 73  1, it has less s
16e04 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 74  pace available t
16e05 68 61 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20 63  han.    ** its c
16e06 68 69 6c 64 20 28 64 75 65 20 74 6f 20 74 68 65  hild (due to the
16e07 20 31 30 30 20 62 79 74 65 20 68 65 61 64 65 72   100 byte header
16e08 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 20   that occurs at 
16e09 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20  the beginning.  
16e0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61    ** of the data
16e0b 62 61 73 65 20 66 6c 65 29 2c 20 73 6f 20 69 74  base fle), so it
16e0c 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 62   might not be ab
16e0d 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6f  le to hold all o
16e0e 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 69 6e  f the .    ** in
16e0f 66 6f 72 6d 61 74 69 6f 6e 20 63 75 72 72 65 6e  formation curren
16e10 74 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  tly contained in
16e11 20 74 68 65 20 63 68 69 6c 64 2e 20 20 49 66 20   the child.  If 
16e12 74 68 69 73 20 69 73 20 74 68 65 20 0a 20 20 20  this is the .   
16e13 20 2a 2a 20 63 61 73 65 2c 20 74 68 65 6e 20 64   ** case, then d
16e14 6f 20 6e 6f 74 20 64 6f 20 74 68 65 20 74 72 61  o not do the tra
16e15 6e 73 66 65 72 2e 20 20 4c 65 61 76 65 20 70 61  nsfer.  Leave pa
16e16 67 65 20 31 20 65 6d 70 74 79 20 65 78 63 65 70  ge 1 empty excep
16e17 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65  t.    ** for the
16e18 20 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 74   right-pointer t
16e19 6f 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65  o the child page
16e1a 2e 20 20 54 68 65 20 63 68 69 6c 64 20 70 61 67  .  The child pag
16e1b 65 20 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a 2a  e becomes.    **
16e1c 20 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f 6f   the virtual roo
16e1d 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 20  t of the tree.. 
16e1e 20 20 20 2a 2f 0a 20 20 20 20 56 56 41 5f 4f 4e     */.    VVA_ON
16e1f 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53  LY( pCur->pagesS
16e20 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 20  huffled = 1 );. 
16e21 20 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 67     pgnoChild = g
16e22 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
16e23 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
16e24 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
16e25 61 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c  assert( pgnoChil
16e26 64 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  d>0 );.    asser
16e27 74 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 70 61  t( pgnoChild<=pa
16e28 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61  gerPagecount(pPa
16e29 67 65 2d 3e 70 42 74 29 20 29 3b 0a 20 20 20 20  ge->pBt) );.    
16e2a 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
16e2b 65 47 65 74 50 61 67 65 28 70 50 61 67 65 2d 3e  eGetPage(pPage->
16e2c 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20  pBt, pgnoChild, 
16e2d 26 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20  &pChild, 0);.   
16e2e 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65   if( rc ) goto e
16e2f 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e  nd_shallow_balan
16e30 63 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67  ce;.    if( pPag
16e31 65 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20  e->pgno==1 ){.  
16e32 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16e33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 43  BtreeInitPage(pC
16e34 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 69 66 28  hild);.      if(
16e35 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73   rc ) goto end_s
16e36 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a  hallow_balance;.
16e37 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43        assert( pC
16e38 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  hild->nOverflow=
16e39 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
16e3a 70 43 68 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d 31  pChild->nFree>=1
16e3b 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  00 ){.        /*
16e3c 20 54 68 65 20 63 68 69 6c 64 20 69 6e 66 6f 72   The child infor
16e3d 6d 61 74 69 6f 6e 20 77 69 6c 6c 20 66 69 74 20  mation will fit 
16e3e 6f 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  on the root page
16e3f 2c 20 73 6f 20 64 6f 20 74 68 65 0a 20 20 20 20  , so do the.    
16e40 20 20 20 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a 20      ** copy */. 
16e41 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20         int i;.  
16e42 20 20 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70        zeroPage(p
16e43 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44  Page, pChild->aD
16e44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20  ata[0]);.       
16e45 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 69   for(i=0; i<pChi
16e46 6c 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ld->nCell; i++){
16e47 0a 20 20 20 20 20 20 20 20 20 20 61 70 43 65 6c  .          apCel
16e48 6c 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28  l[i] = findCell(
16e49 70 43 68 69 6c 64 2c 69 29 3b 0a 20 20 20 20 20  pChild,i);.     
16e4a 20 20 20 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 3d       szCell[i] =
16e4b 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 43 68   cellSizePtr(pCh
16e4c 69 6c 64 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 3b  ild, apCell[i]);
16e4d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
16e4e 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28     assemblePage(
16e4f 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 6e  pPage, pChild->n
16e50 43 65 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 7a  Cell, apCell, sz
16e51 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 2f  Cell);.        /
16e52 2a 20 43 6f 70 79 20 74 68 65 20 72 69 67 68 74  * Copy the right
16e53 2d 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20  -pointer of the 
16e54 63 68 69 6c 64 20 74 6f 20 74 68 65 20 70 61 72  child to the par
16e55 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ent. */.        
16e56 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
16e57 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
16e58 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
16e59 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62  );.        put4b
16e5a 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
16e5b 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
16e5c 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 20 20  et+8], .        
16e5d 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70 43      get4byte(&pC
16e5e 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 68 69  hild->aData[pChi
16e5f 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ld->hdrOffset+8]
16e60 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ));.        rc =
16e61 20 66 72 65 65 50 61 67 65 28 70 43 68 69 6c 64   freePage(pChild
16e62 29 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 45  );.        TRACE
16e63 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c  (("BALANCE: chil
16e64 64 20 25 64 20 74 72 61 6e 73 66 65 72 20 74 6f  d %d transfer to
16e65 20 70 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 69   page 1\n", pChi
16e66 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  ld->pgno));.    
16e67 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
16e68 20 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 68 61   /* The child ha
16e69 73 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69  s more informati
16e6a 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74  on that will fit
16e6b 20 6f 6e 20 74 68 65 20 72 6f 6f 74 2e 0a 20 20   on the root..  
16e6c 20 20 20 20 20 20 2a 2a 20 54 68 65 20 74 72 65        ** The tre
16e6d 65 20 69 73 20 61 6c 72 65 61 64 79 20 62 61 6c  e is already bal
16e6e 61 6e 63 65 64 2e 20 20 44 6f 20 6e 6f 74 68 69  anced.  Do nothi
16e6f 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 54  ng. */.        T
16e70 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20  RACE(("BALANCE: 
16e71 63 68 69 6c 64 20 25 64 20 77 69 6c 6c 20 6e 6f  child %d will no
16e72 74 20 66 69 74 20 6f 6e 20 70 61 67 65 20 31 5c  t fit on page 1\
16e73 6e 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f  n", pChild->pgno
16e74 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
16e75 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
16e76 63 70 79 28 70 50 61 67 65 2d 3e 61 44 61 74 61  cpy(pPage->aData
16e77 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 2c  , pChild->aData,
16e78 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61   pPage->pBt->usa
16e79 62 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  bleSize);.      
16e7a 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
16e7b 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  0;.      rc = sq
16e7c 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
16e7d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ge(pPage);.     
16e7e 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
16e7f 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20  ITE_OK );.      
16e80 66 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 29  freePage(pChild)
16e81 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22  ;.      TRACE(("
16e82 42 41 4c 41 4e 43 45 3a 20 74 72 61 6e 73 66 65  BALANCE: transfe
16e83 72 20 63 68 69 6c 64 20 25 64 20 69 6e 74 6f 20  r child %d into 
16e84 72 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 20 20  root %d\n",.    
16e85 20 20 20 20 20 20 20 20 20 20 70 43 68 69 6c 64            pChild
16e86 2d 3e 70 67 6e 6f 2c 20 70 50 61 67 65 2d 3e 70  ->pgno, pPage->p
16e87 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20  gno));.    }.   
16e88 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
16e89 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a  nOverflow==0 );.
16e8a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16e8b 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
16e8c 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
16e8d 55 55 4d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  UUM && rc==SQLIT
16e8e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
16e8f 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61   = setChildPtrma
16e90 70 73 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d  ps(pPage);.    }
16e91 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 6c 65  .#endif.    rele
16e92 61 73 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b  asePage(pChild);
16e93 0a 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c 6f 77  .  }.end_shallow
16e94 5f 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71 6c 69  _balance:.  sqli
16e95 74 65 33 5f 66 72 65 65 28 61 70 43 65 6c 6c 29  te3_free(apCell)
16e96 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
16e97 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f  .../*.** The roo
16e98 74 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 75  t page is overfu
16e99 6c 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  ll.**.** When th
16e9a 69 73 20 68 61 70 70 65 6e 73 2c 20 43 72 65 61  is happens, Crea
16e9b 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70  te a new child p
16e9c 61 67 65 20 61 6e 64 20 63 6f 70 79 20 74 68 65  age and copy the
16e9d 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  .** contents of 
16e9e 74 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20 74 68  the root into th
16e9f 65 20 63 68 69 6c 64 2e 20 20 54 68 65 6e 20 6d  e child.  Then m
16ea0 61 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20  ake the root.** 
16ea1 70 61 67 65 20 61 6e 20 65 6d 70 74 79 20 70 61  page an empty pa
16ea2 67 65 20 77 69 74 68 20 72 69 67 68 74 43 68 69  ge with rightChi
16ea3 6c 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  ld pointing to t
16ea4 68 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c 64 2e  he new.** child.
16ea5 20 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61 6c 6c     Finally, call
16ea6 20 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72 6e 61   balance_interna
16ea7 6c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 63  l() on the new c
16ea8 68 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75 73 65  hild.** to cause
16ea9 20 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a 2a 2f   it to split..*/
16eaa 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61  .static int bala
16eab 6e 63 65 5f 64 65 65 70 65 72 28 42 74 43 75 72  nce_deeper(BtCur
16eac 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e  sor *pCur){.  in
16ead 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
16eae 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75    /* Return valu
16eaf 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 64  e from subproced
16eb0 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ures */.  MemPag
16eb1 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 2f 2a  e *pPage;     /*
16eb2 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
16eb3 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 4d  root page */.  M
16eb4 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20  emPage *pChild; 
16eb5 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
16eb6 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67   a new child pag
16eb7 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
16eb8 43 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20 50 61  Child;     /* Pa
16eb9 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
16eba 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20   new child page 
16ebb 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
16ebc 42 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  Bt;         /* T
16ebd 68 65 20 42 54 72 65 65 20 2a 2f 0a 20 20 69 6e  he BTree */.  in
16ebe 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20  t usableSize;   
16ebf 20 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61 62 6c    /* Total usabl
16ec0 65 20 73 69 7a 65 20 6f 66 20 61 20 70 61 67 65  e size of a page
16ec1 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20   */.  u8 *data; 
16ec2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
16ec3 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 65  tent of the pare
16ec4 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20  nt page */.  u8 
16ec5 2a 63 64 61 74 61 3b 20 20 20 20 20 20 20 20 20  *cdata;         
16ec6 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74   /* Content of t
16ec7 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f  he child page */
16ec8 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20  .  int hdr;     
16ec9 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
16eca 20 74 6f 20 70 61 67 65 20 68 65 61 64 65 72 20   to page header 
16ecb 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 69  in parent */.  i
16ecc 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20  nt cbrk;        
16ecd 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20     /* Offset to 
16ece 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74  content of first
16ecf 20 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e 74 20   cell in parent 
16ed0 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43  */..  assert( pC
16ed1 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 3b 0a  ur->iPage==0 );.
16ed2 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16ed3 61 70 50 61 67 65 5b 30 5d 2d 3e 6e 4f 76 65 72  apPage[0]->nOver
16ed4 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 56 56 41  flow>0 );..  VVA
16ed5 5f 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67  _ONLY( pCur->pag
16ed6 65 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29  esShuffled = 1 )
16ed7 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72  ;.  pPage = pCur
16ed8 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 70  ->apPage[0];.  p
16ed9 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b  Bt = pPage->pBt;
16eda 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16edb 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
16edc 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
16edd 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
16ede 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
16edf 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
16ee0 3b 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74  ;.  rc = allocat
16ee1 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
16ee2 26 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43 68  &pChild, &pgnoCh
16ee3 69 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ild, pPage->pgno
16ee4 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29  , 0);.  if( rc )
16ee5 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73   return rc;.  as
16ee6 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
16ee7 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43  erIswriteable(pC
16ee8 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29  hild->pDbPage) )
16ee9 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d  ;.  usableSize =
16eea 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
16eeb 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65  ;.  data = pPage
16eec 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d  ->aData;.  hdr =
16eed 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
16eee 74 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32  t;.  cbrk = get2
16eef 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
16ef0 5d 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70 43  ]);.  cdata = pC
16ef1 68 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20 6d  hild->aData;.  m
16ef2 65 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64 61  emcpy(cdata, &da
16ef3 74 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d 3e  ta[hdr], pPage->
16ef4 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50 61  cellOffset+2*pPa
16ef5 67 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b 0a  ge->nCell-hdr);.
16ef6 20 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61 5b    memcpy(&cdata[
16ef7 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72  cbrk], &data[cbr
16ef8 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63  k], usableSize-c
16ef9 62 72 6b 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  brk);..  assert(
16efa 20 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69 74 3d   pChild->isInit=
16efb 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  =0 );.  rc = sql
16efc 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
16efd 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 28  e(pChild);.  if(
16efe 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16eff 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20  {.    int nCopy 
16f00 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  = pPage->nOverfl
16f01 6f 77 2a 73 69 7a 65 6f 66 28 70 50 61 67 65 2d  ow*sizeof(pPage-
16f02 3e 61 4f 76 66 6c 5b 30 5d 29 3b 0a 20 20 20 20  >aOvfl[0]);.    
16f03 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61  memcpy(pChild->a
16f04 4f 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61 4f 76  Ovfl, pPage->aOv
16f05 66 6c 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20  fl, nCopy);.    
16f06 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  pChild->nOverflo
16f07 77 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  w = pPage->nOver
16f08 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 70 43  flow;.    if( pC
16f09 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  hild->nOverflow 
16f0a 29 7b 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d  ){.      pChild-
16f0b 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20  >nFree = 0;.    
16f0c 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  }.    assert( pC
16f0d 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 50 61  hild->nCell==pPa
16f0e 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  ge->nCell );.   
16f0f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16f10 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
16f11 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
16f12 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65   );.    zeroPage
16f13 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e  (pPage, pChild->
16f14 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f  aData[0] & ~PTF_
16f15 4c 45 41 46 29 3b 0a 20 20 20 20 70 75 74 34 62  LEAF);.    put4b
16f16 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
16f17 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
16f18 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64  et+8], pgnoChild
16f19 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22 42  );.    TRACE(("B
16f1a 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f  ALANCE: copy roo
16f1b 74 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c  t %d into %d\n",
16f1c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 43   pPage->pgno, pC
16f1d 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20  hild->pgno));.  
16f1e 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
16f1f 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  UM ){.      rc =
16f20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
16f21 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 54  pChild->pgno, PT
16f22 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 67  RMAP_BTREE, pPag
16f23 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 65  e->pgno);.#ifnde
16f24 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
16f25 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69  TOVACUUM.      i
16f26 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16f27 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
16f28 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73   setChildPtrmaps
16f29 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20  (pChild);.      
16f2a 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  }.      if( rc )
16f2b 7b 0a 20 20 20 20 20 20 20 20 70 43 68 69 6c 64  {.        pChild
16f2c 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b  ->nOverflow = 0;
16f2d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
16f2e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
16f2f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16f30 7b 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67  {.    pCur->iPag
16f31 65 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61  e++;.    pCur->a
16f32 70 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 69 6c  pPage[1] = pChil
16f33 64 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49  d;.    pCur->aiI
16f34 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 72  dx[0] = 0;.    r
16f35 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72  c = balance_nonr
16f36 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c  oot(pCur);.  }el
16f37 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  se{.    releaseP
16f38 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 7d  age(pChild);.  }
16f39 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
16f3a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65  ../*.** The page
16f3b 20 74 68 61 74 20 70 43 75 72 20 63 75 72 72 65   that pCur curre
16f3c 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 68  ntly points to h
16f3d 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64  as just been mod
16f3e 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65  ified in.** some
16f3f 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74   way. This funct
16f40 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20  ion figures out 
16f41 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61  if this modifica
16f42 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a 2a  tion means the.*
16f43 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20  * tree needs to 
16f44 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64  be balanced, and
16f45 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65   if so calls the
16f46 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c   appropriate bal
16f47 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 69  ancing .** routi
16f48 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 61 6d  ne..** .** Param
16f49 65 74 65 72 20 69 73 49 6e 73 65 72 74 20 69 73  eter isInsert is
16f4a 20 74 72 75 65 20 69 66 20 61 20 6e 65 77 20 63   true if a new c
16f4b 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73  ell was just ins
16f4c 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a  erted into the.*
16f4d 2a 20 70 61 67 65 2c 20 6f 72 20 66 61 6c 73 65  * page, or false
16f4e 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73   otherwise..*/.s
16f4f 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63  tatic int balanc
16f50 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
16f51 2c 20 69 6e 74 20 69 73 49 6e 73 65 72 74 29 7b  , int isInsert){
16f52 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
16f53 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65  TE_OK;.  MemPage
16f54 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e   *pPage = pCur->
16f55 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
16f56 67 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ge];..  assert( 
16f57 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
16f58 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
16f59 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
16f5a 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b  Cur->iPage==0 ){
16f5b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
16f5c 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
16f5d 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
16f5e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16f5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76  OK && pPage->nOv
16f60 65 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20 20  erflow>0 ){.    
16f61 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64    rc = balance_d
16f62 65 65 70 65 72 28 70 43 75 72 29 3b 0a 20 20 20  eeper(pCur);.   
16f63 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
16f64 3e 61 70 50 61 67 65 5b 30 5d 3d 3d 70 50 61 67  >apPage[0]==pPag
16f65 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  e );.      asser
16f66 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
16f67 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 3d 53 51  low==0 || rc!=SQ
16f68 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d  LITE_OK );.    }
16f69 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
16f6a 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d  ITE_OK && pPage-
16f6b 3e 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20  >nCell==0 ){.   
16f6c 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f     rc = balance_
16f6d 73 68 61 6c 6c 6f 77 65 72 28 70 43 75 72 29 3b  shallower(pCur);
16f6e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
16f6f 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3d 3d  Cur->apPage[0]==
16f70 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 61  pPage );.      a
16f71 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f  ssert( pPage->nO
16f72 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63  verflow==0 || rc
16f73 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
16f74 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
16f75 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76    if( pPage->nOv
16f76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20 20  erflow>0 || .   
16f77 20 20 20 20 20 28 21 69 73 49 6e 73 65 72 74 20       (!isInsert 
16f78 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e  && pPage->nFree>
16f79 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
16f7a 6c 65 53 69 7a 65 2a 32 2f 33 29 20 29 7b 0a 20  leSize*2/3) ){. 
16f7b 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63       rc = balanc
16f7c 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 29 3b  e_nonroot(pCur);
16f7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
16f7e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
16f7f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
16f80 65 63 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72 73  ecks all cursors
16f81 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74   that point to t
16f82 61 62 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a  able pgnoRoot..*
16f83 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 6f 73  * If any of thos
16f84 65 20 63 75 72 73 6f 72 73 20 77 65 72 65 20 6f  e cursors were o
16f85 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61  pened with wrFla
16f86 67 3d 3d 30 20 69 6e 20 61 20 64 69 66 66 65 72  g==0 in a differ
16f87 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  ent.** database 
16f88 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64 61  connection (a da
16f89 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
16f8a 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68  n that shares th
16f8b 65 20 70 61 67 65 72 0a 2a 2a 20 63 61 63 68 65  e pager.** cache
16f8c 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e   with the curren
16f8d 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61 6e  t connection) an
16f8e 64 20 74 68 61 74 20 6f 74 68 65 72 20 63 6f 6e  d that other con
16f8f 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 6e  nection .** is n
16f90 6f 74 20 69 6e 20 74 68 65 20 52 65 61 64 55 6e  ot in the ReadUn
16f91 63 6f 6d 6d 6d 69 74 74 65 64 20 73 74 61 74 65  commmitted state
16f92 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
16f93 69 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20  ine returns .** 
16f94 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a  SQLITE_LOCKED..*
16f95 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20  *.** As well as 
16f96 63 75 72 73 6f 72 73 20 77 69 74 68 20 77 72 46  cursors with wrF
16f97 6c 61 67 3d 3d 30 2c 20 63 75 72 73 6f 72 73 20  lag==0, cursors 
16f98 77 69 74 68 20 0a 2a 2a 20 69 73 49 6e 63 72 62  with .** isIncrb
16f99 6c 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 72 65  lobHandle==1 are
16f9a 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64   also considered
16f9b 20 27 72 65 61 64 27 20 63 75 72 73 6f 72 73 20   'read' cursors 
16f9c 62 65 63 61 75 73 65 0a 2a 2a 20 69 6e 63 72 65  because.** incre
16f9d 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73  mental blob curs
16f9e 6f 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72  ors are used for
16f9f 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e   both reading an
16fa0 64 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  d writing..**.**
16fa1 20 57 68 65 6e 20 70 67 6e 6f 52 6f 6f 74 20 69   When pgnoRoot i
16fa2 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  s the root page 
16fa3 6f 66 20 61 6e 20 69 6e 74 6b 65 79 20 74 61 62  of an intkey tab
16fa4 6c 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  le, this functio
16fa5 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73  n is also.** res
16fa6 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76  ponsible for inv
16fa7 61 6c 69 64 61 74 69 6e 67 20 69 6e 63 72 65 6d  alidating increm
16fa8 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f  ental blob curso
16fa9 72 73 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c  rs when the tabl
16faa 65 20 72 6f 77 0a 2a 2a 20 6f 6e 20 77 68 69 63  e row.** on whic
16fab 68 20 74 68 65 79 20 61 72 65 20 6f 70 65 6e 65  h they are opene
16fac 64 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20  d is deleted or 
16fad 6d 6f 64 69 66 69 65 64 2e 20 43 75 72 73 6f 72  modified. Cursor
16fae 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65  s are invalidate
16faf 64 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74  d.** according t
16fb0 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
16fb1 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  rules:.**.**   1
16fb2 29 20 57 68 65 6e 20 42 74 72 65 65 43 6c 65 61  ) When BtreeClea
16fb3 72 54 61 62 6c 65 28 29 20 69 73 20 63 61 6c 6c  rTable() is call
16fb4 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79  ed to completely
16fb5 20 64 65 6c 65 74 65 20 74 68 65 20 63 6f 6e 74   delete the cont
16fb6 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 6f 66 20  ents.**      of 
16fb7 61 20 42 2d 54 72 65 65 20 74 61 62 6c 65 2c 20  a B-Tree table, 
16fb8 70 45 78 63 6c 75 64 65 20 69 73 20 73 65 74 20  pExclude is set 
16fb9 74 6f 20 7a 65 72 6f 20 61 6e 64 20 70 61 72 61  to zero and para
16fba 6d 65 74 65 72 20 69 52 6f 77 20 69 73 20 0a 2a  meter iRow is .*
16fbb 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 6e 6f  *      set to no
16fbc 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20  n-zero. In this 
16fbd 63 61 73 65 20 61 6c 6c 20 69 6e 63 72 65 6d 65  case all increme
16fbe 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72  ntal blob cursor
16fbf 73 20 6f 70 65 6e 0a 2a 2a 20 20 20 20 20 20 6f  s open.**      o
16fc0 6e 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74  n the table root
16fc1 65 64 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 61  ed at pgnoRoot a
16fc2 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a  re invalidated..
16fc3 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20  **.**   2) When 
16fc4 42 74 72 65 65 49 6e 73 65 72 74 28 29 2c 20 42  BtreeInsert(), B
16fc5 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20  treeDelete() or 
16fc6 42 74 72 65 65 50 75 74 44 61 74 61 28 29 20 69  BtreePutData() i
16fc7 73 20 63 61 6c 6c 65 64 20 74 6f 20 0a 2a 2a 20  s called to .** 
16fc8 20 20 20 20 20 6d 6f 64 69 66 79 20 61 20 74 61       modify a ta
16fc9 62 6c 65 20 72 6f 77 20 76 69 61 20 61 6e 20 53  ble row via an S
16fca 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 70 45  QL statement, pE
16fcb 78 63 6c 75 64 65 20 69 73 20 73 65 74 20 74 6f  xclude is set to
16fcc 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 77 72   the .**      wr
16fcd 69 74 65 20 63 75 72 73 6f 72 20 75 73 65 64 20  ite cursor used 
16fce 74 6f 20 64 6f 20 74 68 65 20 6d 6f 64 69 66 69  to do the modifi
16fcf 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 72 61 6d  cation and param
16fd0 65 74 65 72 20 69 52 6f 77 20 69 73 20 73 65 74  eter iRow is set
16fd1 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20  .**      to the 
16fd2 69 6e 74 65 67 65 72 20 72 6f 77 20 69 64 20 6f  integer row id o
16fd3 66 20 74 68 65 20 42 2d 54 72 65 65 20 65 6e 74  f the B-Tree ent
16fd4 72 79 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65  ry being modifie
16fd5 64 2e 20 55 6e 6c 65 73 73 0a 2a 2a 20 20 20 20  d. Unless.**    
16fd6 20 20 70 45 78 63 6c 75 64 65 20 69 73 20 69 74    pExclude is it
16fd7 73 65 6c 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e  self an incremen
16fd8 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 2c  tal blob cursor,
16fd9 20 74 68 65 6e 20 61 6c 6c 20 69 6e 63 72 65 6d   then all increm
16fda 65 6e 74 61 6c 0a 2a 2a 20 20 20 20 20 20 62 6c  ental.**      bl
16fdb 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20  ob cursors open 
16fdc 6f 6e 20 72 6f 77 20 69 52 6f 77 20 6f 66 20 74  on row iRow of t
16fdd 68 65 20 42 2d 54 72 65 65 20 61 72 65 20 69 6e  he B-Tree are in
16fde 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  validated..**.**
16fdf 20 20 20 33 29 20 49 66 20 62 6f 74 68 20 70 45     3) If both pE
16fe0 78 63 6c 75 64 65 20 61 6e 64 20 69 52 6f 77 20  xclude and iRow 
16fe1 61 72 65 20 73 65 74 20 74 6f 20 7a 65 72 6f 2c  are set to zero,
16fe2 20 6e 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20   no incremental 
16fe3 62 6c 6f 62 20 0a 2a 2a 20 20 20 20 20 20 63 75  blob .**      cu
16fe4 72 73 6f 72 73 20 61 72 65 20 69 6e 76 61 6c 69  rsors are invali
16fe5 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  dated..*/.static
16fe6 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 52 65 61   int checkForRea
16fe7 64 43 6f 6e 66 6c 69 63 74 73 28 0a 20 20 42 74  dConflicts(.  Bt
16fe8 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20  ree *pBtree,    
16fe9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
16fea 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68  abase file to ch
16feb 65 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  eck */.  Pgno pg
16fec 6e 6f 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20  noRoot,         
16fed 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 65 61   /* Look for rea
16fee 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 69  d cursors on thi
16fef 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 43  s btree */.  BtC
16ff0 75 72 73 6f 72 20 2a 70 45 78 63 6c 75 64 65 2c  ursor *pExclude,
16ff1 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74       /* Ignore t
16ff2 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  his cursor */.  
16ff3 69 36 34 20 69 52 6f 77 20 20 20 20 20 20 20 20  i64 iRow        
16ff4 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
16ff5 6f 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 20  owid that might 
16ff6 62 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 29  be changing */.)
16ff7 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  {.  BtCursor *p;
16ff8 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
16ff9 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a   = pBtree->pBt;.
16ffa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
16ffb 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 61 73  pBtree->db;.  as
16ffc 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
16ffd 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74  eeHoldsMutex(pBt
16ffe 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d  ree) );.  for(p=
16fff 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b  pBt->pCursor; p;
17000 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
17001 20 20 69 66 28 20 70 3d 3d 70 45 78 63 6c 75 64    if( p==pExclud
17002 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  e ) continue;.  
17003 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f    if( p->pgnoRoo
17004 74 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63 6f  t!=pgnoRoot ) co
17005 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20  ntinue;.#ifndef 
17006 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
17007 42 4c 4f 42 0a 20 20 20 20 69 66 28 20 70 2d 3e  BLOB.    if( p->
17008 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65  isIncrblobHandle
17009 20 26 26 20 28 20 0a 20 20 20 20 20 20 20 20 20   && ( .         
1700a 28 21 70 45 78 63 6c 75 64 65 20 26 26 20 69 52  (!pExclude && iR
1700b 6f 77 29 0a 20 20 20 20 20 20 7c 7c 20 28 70 45  ow).      || (pE
1700c 78 63 6c 75 64 65 20 26 26 20 21 70 45 78 63 6c  xclude && !pExcl
1700d 75 64 65 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48  ude->isIncrblobH
1700e 61 6e 64 6c 65 20 26 26 20 70 2d 3e 69 6e 66 6f  andle && p->info
1700f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20 20  .nKey==iRow).   
17010 20 29 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53   )){.      p->eS
17011 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e  tate = CURSOR_IN
17012 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 23 65 6e  VALID;.    }.#en
17013 64 69 66 0a 20 20 20 20 69 66 28 20 70 2d 3e 65  dif.    if( p->e
17014 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41  State!=CURSOR_VA
17015 4c 49 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  LID ) continue;.
17016 20 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c 61      if( p->wrFla
17017 67 3d 3d 30 20 0a 23 69 66 6e 64 65 66 20 53 51  g==0 .#ifndef SQ
17018 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
17019 4f 42 0a 20 20 20 20 20 7c 7c 20 70 2d 3e 69 73  OB.     || p->is
1701a 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 0a 23  IncrblobHandle.#
1701b 65 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20 20 20  endif.    ){.   
1701c 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f 74     sqlite3 *dbOt
1701d 68 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65 2d  her = p->pBtree-
1701e 3e 64 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72  >db;.      asser
1701f 74 28 64 62 4f 74 68 65 72 29 3b 0a 20 20 20 20  t(dbOther);.    
17020 20 20 69 66 28 20 64 62 4f 74 68 65 72 21 3d 64    if( dbOther!=d
17021 62 20 26 26 20 28 64 62 4f 74 68 65 72 2d 3e 66  b && (dbOther->f
17022 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65  lags & SQLITE_Re
17023 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 3d 3d  adUncommitted)==
17024 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
17025 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c  ite3ConnectionBl
17026 6f 63 6b 65 64 28 64 62 2c 20 64 62 4f 74 68 65  ocked(db, dbOthe
17027 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  r);.        retu
17028 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
17029 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20  _SHAREDCACHE;.  
1702a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1702b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1702c 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73  OK;.}../*.** Ins
1702d 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64  ert a new record
1702e 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e   into the BTree.
1702f 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76    The key is giv
17030 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79  en by (pKey,nKey
17031 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74  ).** and the dat
17032 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70  a is given by (p
17033 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68  Data,nData).  Th
17034 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64  e cursor is used
17035 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69   only to.** defi
17036 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68  ne what table th
17037 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20  e record should 
17038 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  be inserted into
17039 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a  .  The cursor.**
1703a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
1703b 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f  g at a random lo
1703c 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f  cation..**.** Fo
1703d 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c  r an INTKEY tabl
1703e 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79  e, only the nKey
1703f 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65   value of the ke
17040 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79  y is used.  pKey
17041 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20   is.** ignored. 
17042 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20   For a ZERODATA 
17043 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61  table, the pData
17044 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62   and nData are b
17045 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a  oth ignored..*/.
17046 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
17047 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
17048 6e 73 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f  nsert(.  BtCurso
17049 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20  r *pCur,        
1704a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72          /* Inser
1704b 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20  t data into the 
1704c 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75  table of this cu
1704d 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rsor */.  const 
1704e 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20  void *pKey, i64 
1704f 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20  nKey,    /* The 
17050 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72  key of the new r
17051 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  ecord */.  const
17052 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e   void *pData, in
17053 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65  t nData,  /* The
17054 20 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77   data of the new
17055 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
17056 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20   nZero,         
17057 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17058 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 30  umber of extra 0
17059 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64   bytes to append
1705a 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e   to data */.  in
1705b 74 20 61 70 70 65 6e 64 42 69 61 73 20 20 20 20  t appendBias    
1705c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1705d 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
1705e 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64  likely an append
1705f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
17060 0a 20 20 69 6e 74 20 6c 6f 63 3b 0a 20 20 69 6e  .  int loc;.  in
17061 74 20 73 7a 4e 65 77 3b 0a 20 20 69 6e 74 20 69  t szNew;.  int i
17062 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  dx;.  MemPage *p
17063 50 61 67 65 3b 0a 20 20 42 74 72 65 65 20 2a 70  Page;.  Btree *p
17064 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b   = pCur->pBtree;
17065 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
17066 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73   = p->pBt;.  uns
17067 69 67 6e 65 64 20 63 68 61 72 20 2a 6f 6c 64 43  igned char *oldC
17068 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  ell;.  unsigned 
17069 63 68 61 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20  char *newCell = 
1706a 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  0;..  assert( cu
1706b 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
1706c 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
1706d 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ( pBt->inTransac
1706e 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54  tion==TRANS_WRIT
1706f 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  E );.  assert( !
17070 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b  pBt->readOnly );
17071 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
17072 3e 77 72 46 6c 61 67 20 29 3b 0a 20 20 72 63 20  >wrFlag );.  rc 
17073 3d 20 63 68 65 63 6b 46 6f 72 52 65 61 64 43 6f  = checkForReadCo
17074 6e 66 6c 69 63 74 73 28 70 43 75 72 2d 3e 70 42  nflicts(pCur->pB
17075 74 72 65 65 2c 20 70 43 75 72 2d 3e 70 67 6e 6f  tree, pCur->pgno
17076 52 6f 6f 74 2c 20 70 43 75 72 2c 20 6e 4b 65 79  Root, pCur, nKey
17077 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 20 20  );.  if( rc ){  
17078 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20             .    
17079 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70 43 75  /* The table pCu
1707a 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20  r points to has 
1707b 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20  a read lock */. 
1707c 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
1707d 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41  QLITE_LOCKED_SHA
1707e 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20 20 20  REDCACHE );.    
1707f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
17080 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
17081 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  e==CURSOR_FAULT 
17082 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43  ){.    return pC
17083 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a 0a 20  ur->skip;.  }.. 
17084 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73   /* Save the pos
17085 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74  itions of any ot
17086 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e  her cursors open
17087 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   on this table *
17088 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  /.  sqlite3Btree
17089 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75 72  ClearCursor(pCur
1708a 29 3b 0a 20 20 69 66 28 20 0a 20 20 20 20 53 51  );.  if( .    SQ
1708b 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
1708c 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42  aveAllCursors(pB
1708d 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  t, pCur->pgnoRoo
1708e 74 2c 20 70 43 75 72 29 29 20 7c 7c 0a 20 20 20  t, pCur)) ||.   
1708f 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
17090 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
17091 76 65 74 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c  veto(pCur, pKey,
17092 20 6e 4b 65 79 2c 20 61 70 70 65 6e 64 42 69 61   nKey, appendBia
17093 73 2c 20 26 6c 6f 63 29 29 0a 20 20 29 7b 0a 20  s, &loc)).  ){. 
17094 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
17095 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75  }..  pPage = pCu
17096 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
17097 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74  iPage];.  assert
17098 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
17099 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20  || nKey>=0 );.  
1709a 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c  assert( pPage->l
1709b 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69  eaf || !pPage->i
1709c 6e 74 4b 65 79 20 29 3b 0a 20 20 54 52 41 43 45  ntKey );.  TRACE
1709d 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65  (("INSERT: table
1709e 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64  =%d nkey=%lld nd
1709f 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25  ata=%d page=%d %
170a0 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
170a1 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20  pCur->pgnoRoot, 
170a2 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61  nKey, nData, pPa
170a3 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20  ge->pgno,.      
170a4 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76      loc==0 ? "ov
170a5 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20  erwrite" : "new 
170a6 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65  entry"));.  asse
170a7 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  rt( pPage->isIni
170a8 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54  t );.  allocateT
170a9 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20  empSpace(pBt);. 
170aa 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e   newCell = pBt->
170ab 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28  pTmpSpace;.  if(
170ac 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65   newCell==0 ) re
170ad 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
170ae 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e  M;.  rc = fillIn
170af 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43  Cell(pPage, newC
170b0 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c  ell, pKey, nKey,
170b1 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e   pData, nData, n
170b2 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20  Zero, &szNew);. 
170b3 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65   if( rc ) goto e
170b4 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73  nd_insert;.  ass
170b5 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c  ert( szNew==cell
170b6 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e  SizePtr(pPage, n
170b7 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73  ewCell) );.  ass
170b8 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43  ert( szNew<=MX_C
170b9 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b  ELL_SIZE(pBt) );
170ba 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61  .  idx = pCur->a
170bb 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
170bc 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20  ];.  if( loc==0 
170bd 26 26 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d  && CURSOR_VALID=
170be 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b  =pCur->eState ){
170bf 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a  .    u16 szOld;.
170c0 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c      assert( idx<
170c1 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pPage->nCell );.
170c2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
170c3 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
170c4 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
170c5 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
170c6 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b  goto end_insert;
170c7 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65  .    }.    oldCe
170c8 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
170c9 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69  age, idx);.    i
170ca 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
170cb 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
170cc 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c  newCell, oldCell
170cd 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 4);.    }.    
170ce 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65  szOld = cellSize
170cf 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65  Ptr(pPage, oldCe
170d0 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c  ll);.    rc = cl
170d1 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f  earCell(pPage, o
170d2 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28  ldCell);.    if(
170d3 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69   rc ) goto end_i
170d4 6e 73 65 72 74 3b 0a 20 20 20 20 72 63 20 3d 20  nsert;.    rc = 
170d5 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20  dropCell(pPage, 
170d6 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20 20 20  idx, szOld);.   
170d7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
170d8 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 67 6f 74  OK ) {.      got
170d9 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20  o end_insert;.  
170da 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
170db 6c 6f 63 3c 30 20 26 26 20 70 50 61 67 65 2d 3e  loc<0 && pPage->
170dc 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 61  nCell>0 ){.    a
170dd 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65  ssert( pPage->le
170de 61 66 20 29 3b 0a 20 20 20 20 69 64 78 20 3d 20  af );.    idx = 
170df 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ++pCur->aiIdx[pC
170e0 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
170e1 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
170e2 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e   = 0;.    pCur->
170e3 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
170e4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
170e5 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  rt( pPage->leaf 
170e6 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 69 6e  );.  }.  rc = in
170e7 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20  sertCell(pPage, 
170e8 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a  idx, newCell, sz
170e9 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66  New, 0, 0);.  if
170ea 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
170eb 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61  ){.    rc = bala
170ec 6e 63 65 28 70 43 75 72 2c 20 31 29 3b 0a 20 20  nce(pCur, 1);.  
170ed 7d 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b  }..  /* Must mak
170ee 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77  e sure nOverflow
170ef 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72   is reset to zer
170f0 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20 62 61  o even if the ba
170f1 6c 61 6e 63 65 28 29 0a 20 20 2a 2a 20 66 61 69  lance().  ** fai
170f2 6c 73 2e 20 20 49 6e 74 65 72 6e 61 6c 20 64 61  ls.  Internal da
170f3 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72  ta structure cor
170f4 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73  ruption will res
170f5 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 2a  ult otherwise. *
170f6 2f 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65  /.  pCur->apPage
170f7 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
170f8 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20  Overflow = 0;.. 
170f9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
170fa 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f  OK ){.    moveTo
170fb 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a  Root(pCur);.  }.
170fc 65 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65  end_insert:.  re
170fd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
170fe 2a 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74  * Delete the ent
170ff 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73  ry that the curs
17100 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
17101 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a  o.  The cursor.*
17102 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  * is left pointi
17103 6e 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72  ng at a arbitrar
17104 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  y location..*/.S
17105 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
17106 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  t sqlite3BtreeDe
17107 6c 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70  lete(BtCursor *p
17108 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  Cur){.  MemPage 
17109 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  *pPage = pCur->a
1710a 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
1710b 65 5d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20  e];.  int idx;. 
1710c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
1710d 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63 3b  pCell;.  int rc;
1710e 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c  .  Pgno pgnoChil
1710f 64 20 3d 20 30 3b 0a 20 20 42 74 72 65 65 20 2a  d = 0;.  Btree *
17110 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65  p = pCur->pBtree
17111 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
17112 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61  t = p->pBt;..  a
17113 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
17114 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
17115 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
17116 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73  ->isInit );.  as
17117 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61  sert( pBt->inTra
17118 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
17119 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72  WRITE );.  asser
1711a 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  t( !pBt->readOnl
1711b 79 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  y );.  if( pCur-
1711c 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1711d 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74  FAULT ){.    ret
1711e 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a  urn pCur->skip;.
1711f 20 20 7d 0a 20 20 69 66 28 20 4e 45 56 45 52 28    }.  if( NEVER(
17120 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
17121 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d  ->iPage]>=pPage-
17122 3e 6e 43 65 6c 6c 29 20 29 7b 0a 20 20 20 20 72  >nCell) ){.    r
17123 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
17124 4f 52 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73  OR;  /* The curs
17125 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69  or is not pointi
17126 6e 67 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 2a  ng to anything *
17127 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  /.  }.  assert( 
17128 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b 0a  pCur->wrFlag );.
17129 20 20 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52    rc = checkForR
1712a 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20  eadConflicts(p, 
1712b 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20  pCur->pgnoRoot, 
1712c 70 43 75 72 2c 20 70 43 75 72 2d 3e 69 6e 66 6f  pCur, pCur->info
1712d 2e 6e 4b 65 79 29 3b 0a 20 20 69 66 28 20 72 63  .nKey);.  if( rc
1712e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1712f 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
17130 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 68  pCur points to h
17131 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a  as a read lock *
17132 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  /.    assert( rc
17133 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f  ==SQLITE_LOCKED_
17134 53 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a 20  SHAREDCACHE );. 
17135 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
17136 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20  }..  /* Restore 
17137 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73  the current curs
17138 6f 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20 6e  or position (a n
17139 6f 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72 73  o-op if the curs
1713a 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20 20  or is not in .  
1713b 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52  ** CURSOR_REQUIR
1713c 45 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e 64  ESEEK state) and
1713d 20 73 61 76 65 20 74 68 65 20 70 6f 73 69 74 69   save the positi
1713e 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72  ons of any other
1713f 20 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20 6f   cursors .  ** o
17140 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  pen on the same 
17141 74 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c 6c  table. Then call
17142 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
17143 74 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67 65  te() on the page
17144 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 65  .  ** that the e
17145 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65 6c  ntry will be del
17146 65 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f 0a  eted from..  */.
17147 20 20 69 66 28 20 0a 20 20 20 20 28 72 63 20 3d    if( .    (rc =
17148 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
17149 73 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d 30  sition(pCur))!=0
1714a 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 61   ||.    (rc = sa
1714b 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74  veAllCursors(pBt
1714c 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCur->pgnoRoot
1714d 2c 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a 20  , pCur))!=0 ||. 
1714e 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33     (rc = sqlite3
1714f 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
17150 2d 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a 20  ->pDbPage))!=0. 
17151 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
17152 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63  c;.  }..  /* Loc
17153 61 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74  ate the cell wit
17154 68 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e 64  hin its page and
17155 20 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f 69   leave pCell poi
17156 6e 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 2a  nting to the.  *
17157 2a 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 61  * data. The clea
17158 72 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 65  rCell() call fre
17159 65 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20  es any overflow 
1715a 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64  pages associated
1715b 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 63   with the.  ** c
1715c 65 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 74  ell. The cell it
1715d 73 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 6e  self is still in
1715e 74 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64 78  tact..  */.  idx
1715f 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70   = pCur->aiIdx[p
17160 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 70  Cur->iPage];.  p
17161 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
17162 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 69  pPage, idx);.  i
17163 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
17164 29 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64  ){.    pgnoChild
17165 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c   = get4byte(pCel
17166 6c 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63  l);.  }.  rc = c
17167 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20  learCell(pPage, 
17168 70 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 63  pCell);.  if( rc
17169 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
1716a 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70  c;.  }..  if( !p
1716b 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
1716c 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    /*.    ** The 
1716d 65 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 6f  entry we are abo
1716e 75 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20  ut to delete is 
1716f 6e 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 66  not a leaf so if
17170 20 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 2a   we do not.    *
17171 2a 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77  * do something w
17172 65 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 68  e will leave a h
17173 6f 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e  ole on an intern
17174 61 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 20  al page..    ** 
17175 57 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c 20  We have to fill 
17176 74 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 69  the hole by movi
17177 6e 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 6f  ng in a cell fro
17178 6d 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a 20  m a leaf.  The. 
17179 20 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c 20     ** next Cell 
1717a 61 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 6f  after the one to
1717b 20 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 67   be deleted is g
1717c 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69  uaranteed to exi
1717d 73 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 6f  st and.    ** to
1717e 20 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 65   be a leaf so we
1717f 20 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 20   can use it..   
17180 20 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f 72   */.    BtCursor
17181 20 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d 65   leafCur;.    Me
17182 6d 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67 65  mPage *pLeafPage
17183 20 3d 20 30 3b 0a 0a 20 20 20 20 75 6e 73 69 67   = 0;..    unsig
17184 6e 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74 3b  ned char *pNext;
17185 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64  .    int notUsed
17186 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
17187 68 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20 3d 20  har *tempCell = 
17188 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  0;.    assert( !
17189 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b  pPage->intKey );
1718a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1718b 65 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 70  eGetTempCursor(p
1718c 43 75 72 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a  Cur, &leafCur);.
1718d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1718e 42 74 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43  BtreeNext(&leafC
1718f 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20  ur, &notUsed);. 
17190 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
17191 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73  E_OK ){.      as
17192 73 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61 69  sert( leafCur.ai
17193 49 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61 67  Idx[leafCur.iPag
17194 65 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  e]==0 );.      p
17195 4c 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 43  LeafPage = leafC
17196 75 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 75  ur.apPage[leafCu
17197 72 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 20  r.iPage];.      
17198 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
17199 72 57 72 69 74 65 28 70 4c 65 61 66 50 61 67 65  rWrite(pLeafPage
1719a 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
1719b 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
1719c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1719d 20 69 6e 74 20 6c 65 61 66 43 75 72 73 6f 72 49   int leafCursorI
1719e 6e 76 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20  nvalid = 0;.    
1719f 20 20 75 31 36 20 73 7a 4e 65 78 74 3b 0a 20 20    u16 szNext;.  
171a0 20 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45      TRACE(("DELE
171a1 54 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c  TE: table=%d del
171a2 65 74 65 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f  ete internal fro
171a3 6d 20 25 64 20 72 65 70 6c 61 63 65 20 66 72 6f  m %d replace fro
171a4 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20  m leaf %d\n",.  
171a5 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e         pCur->pgn
171a6 6f 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67  oRoot, pPage->pg
171a7 6e 6f 2c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70  no, pLeafPage->p
171a8 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72 6f  gno));.      dro
171a9 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78  pCell(pPage, idx
171aa 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  , cellSizePtr(pP
171ab 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20  age, pCell));.  
171ac 20 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e 64      pNext = find
171ad 43 65 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c 20  Cell(pLeafPage, 
171ae 30 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78 74  0);.      szNext
171af 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
171b0 4c 65 61 66 50 61 67 65 2c 20 70 4e 65 78 74 29  LeafPage, pNext)
171b1 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
171b2 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74  MX_CELL_SIZE(pBt
171b3 29 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a 20  )>=szNext+4 );. 
171b4 20 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d       allocateTem
171b5 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20  pSpace(pBt);.   
171b6 20 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70 42     tempCell = pB
171b7 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20  t->pTmpSpace;.  
171b8 20 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c 6c      if( tempCell
171b9 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
171ba 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
171bb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
171bc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
171bd 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
171be 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61  = insertCell(pPa
171bf 67 65 2c 20 69 64 78 2c 20 70 4e 65 78 74 2d 34  ge, idx, pNext-4
171c0 2c 20 73 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 70  , szNext+4, temp
171c1 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  Cell, 0);.      
171c2 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  }...      /* The
171c3 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 20   "if" statement 
171c4 69 6e 20 74 68 65 20 6e 65 78 74 20 63 6f 64 65  in the next code
171c5 20 62 6c 6f 63 6b 20 69 73 20 63 72 69 74 69 63   block is critic
171c6 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a  al.  The.      *
171c7 2a 20 73 6c 69 67 68 74 65 73 74 20 65 72 72 6f  * slightest erro
171c8 72 20 69 6e 20 74 68 61 74 20 73 74 61 74 65 6d  r in that statem
171c9 65 6e 74 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 20  ent would allow 
171ca 53 51 4c 69 74 65 20 74 6f 20 6f 70 65 72 61 74  SQLite to operat
171cb 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65  e.      ** corre
171cc 63 74 6c 79 20 6d 6f 73 74 20 6f 66 20 74 68 65  ctly most of the
171cd 20 74 69 6d 65 20 62 75 74 20 70 72 6f 64 75 63   time but produc
171ce 65 20 76 65 72 79 20 72 61 72 65 20 66 61 69 6c  e very rare fail
171cf 75 72 65 73 2e 20 20 54 6f 0a 20 20 20 20 20 20  ures.  To.      
171d0 2a 2a 20 67 75 61 72 64 20 61 67 61 69 6e 73 74  ** guard against
171d1 20 74 68 69 73 2c 20 74 68 65 20 66 6f 6c 6c 6f   this, the follo
171d2 77 69 6e 67 20 6d 61 63 72 6f 73 20 68 65 6c 70  wing macros help
171d3 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 0a   to verify that.
171d4 20 20 20 20 20 20 2a 2a 20 74 68 65 20 22 69 66        ** the "if
171d5 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 77  " statement is w
171d6 65 6c 6c 20 74 65 73 74 65 64 2e 0a 20 20 20 20  ell tested..    
171d7 20 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63    */.      testc
171d8 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ase( pPage->nOve
171d9 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67  rflow==0 && pPag
171da 65 2d 3e 6e 46 72 65 65 3c 70 42 74 2d 3e 75 73  e->nFree<pBt->us
171db 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20  ableSize*2/3 .  
171dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
171dd 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72  & pLeafPage->nFr
171de 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42  ee+2+szNext > pB
171df 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f  t->usableSize*2/
171e0 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  3 );.      testc
171e1 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ase( pPage->nOve
171e2 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67  rflow==0 && pPag
171e3 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75  e->nFree==pBt->u
171e4 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20  sableSize*2/3 . 
171e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
171e6 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46  && pLeafPage->nF
171e7 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70  ree+2+szNext > p
171e8 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
171e9 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  /3 );.      test
171ea 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76  case( pPage->nOv
171eb 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61  erflow==0 && pPa
171ec 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e  ge->nFree==pBt->
171ed 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31  usableSize*2/3+1
171ee 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
171ef 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d     && pLeafPage-
171f0 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20  >nFree+2+szNext 
171f1 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  > pBt->usableSiz
171f2 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74  e*2/3 );.      t
171f3 65 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e  estcase( pPage->
171f4 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 26 26 20 70  nOverflow>0 && p
171f5 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 74  Page->nFree<=pBt
171f6 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33  ->usableSize*2/3
171f7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
171f8 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e    && pLeafPage->
171f9 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e  nFree+2+szNext >
171fa 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
171fb 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65  *2/3 );.      te
171fc 73 74 63 61 73 65 28 20 28 70 50 61 67 65 2d 3e  stcase( (pPage->
171fd 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28  nOverflow>0 || (
171fe 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70  pPage->nFree > p
171ff 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
17200 2f 33 29 29 0a 20 20 20 20 20 20 20 20 20 20 20  /3)).           
17201 20 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61        && pLeafPa
17202 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65  ge->nFree+2+szNe
17203 78 74 20 3d 3d 20 70 42 74 2d 3e 75 73 61 62 6c  xt == pBt->usabl
17204 65 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a 20  eSize*2/3 );... 
17205 20 20 20 20 20 69 66 28 20 28 70 50 61 67 65 2d       if( (pPage-
17206 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20  >nOverflow>0 || 
17207 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20  (pPage->nFree > 
17208 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a  pBt->usableSize*
17209 32 2f 33 29 29 20 26 26 0a 20 20 20 20 20 20 20  2/3)) &&.       
1720a 20 20 20 28 70 4c 65 61 66 50 61 67 65 2d 3e 6e     (pLeafPage->n
1720b 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20  Free+2+szNext > 
1720c 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a  pBt->usableSize*
1720d 32 2f 33 29 0a 20 20 20 20 20 20 29 7b 0a 20 20  2/3).      ){.  
1720e 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72        /* This br
1720f 61 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 69 66  anch is taken if
17210 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   the internal no
17211 64 65 20 69 73 20 6e 6f 77 20 65 69 74 68 65 72  de is now either
17212 20 6f 76 65 72 66 6c 6f 77 69 6e 67 0a 20 20 20   overflowing.   
17213 20 20 20 20 20 2a 2a 20 6f 72 20 75 6e 64 65 72       ** or under
17214 66 75 6c 6c 20 61 6e 64 20 74 68 65 20 6c 65 61  full and the lea
17215 66 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65 20 75  f node will be u
17216 6e 64 65 72 66 75 6c 6c 20 61 66 74 65 72 20 74  nderfull after t
17217 68 65 20 6a 75 73 74 20 63 65 6c 6c 20 0a 20 20  he just cell .  
17218 20 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20        ** copied 
17219 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  to the internal 
1721a 6e 6f 64 65 20 69 73 20 64 65 6c 65 74 65 64 20  node is deleted 
1721b 66 72 6f 6d 20 69 74 2e 20 54 68 69 73 20 69 73  from it. This is
1721c 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20   a special.     
1721d 20 20 20 2a 2a 20 63 61 73 65 20 62 65 63 61 75     ** case becau
1721e 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 62  se the call to b
1721f 61 6c 61 6e 63 65 28 29 20 74 6f 20 63 6f 72 72  alance() to corr
17220 65 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ect the internal
17221 20 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a 2a   node.        **
17222 20 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20   may change the 
17223 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61  tree structure a
17224 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68  nd invalidate th
17225 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 20  e contents of.  
17226 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 61        ** the lea
17227 66 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e  fCur.apPage[] an
17228 64 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b  d leafCur.aiIdx[
17229 5d 20 61 72 72 61 79 73 2c 20 77 68 69 63 68 20  ] arrays, which 
1722a 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20  will be.        
1722b 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 62  ** used by the b
1722c 61 6c 61 6e 63 65 28 29 20 72 65 71 75 69 72 65  alance() require
1722d 64 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 65  d to correct the
1722e 20 75 6e 64 65 72 66 75 6c 6c 20 6c 65 61 66 0a   underfull leaf.
1722f 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 2e          ** node.
17230 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
17231 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 72 6d 75      ** The formu
17232 6c 61 20 75 73 65 64 20 69 6e 20 74 68 65 20 65  la used in the e
17233 78 70 72 65 73 73 69 6f 6e 20 61 62 6f 76 65 20  xpression above 
17234 61 72 65 20 62 61 73 65 64 20 6f 6e 20 66 61 63  are based on fac
17235 65 74 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a  ets of.        *
17236 2a 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c  * the SQLite fil
17237 65 2d 66 6f 72 6d 61 74 20 74 68 61 74 20 64 6f  e-format that do
17238 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72   not change over
17239 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 2a   time..        *
1723a 2f 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  /.        testca
1723b 73 65 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  se( pPage->nFree
1723c 3d 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ==pBt->usableSiz
1723d 65 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 20 20 20  e*2/3+1 );.     
1723e 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65     testcase( pLe
1723f 61 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b  afPage->nFree+2+
17240 73 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e 75 73 61  szNext==pBt->usa
17241 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b  bleSize*2/3+1 );
17242 0a 20 20 20 20 20 20 20 20 6c 65 61 66 43 75 72  .        leafCur
17243 73 6f 72 49 6e 76 61 6c 69 64 20 3d 20 31 3b 0a  sorInvalid = 1;.
17244 20 20 20 20 20 20 7d 20 20 20 20 20 20 20 20 0a        }        .
17245 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
17246 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17247 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
17248 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
17249 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
1724a 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70  ge) );.        p
1724b 75 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72  ut4byte(findOver
1724c 66 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20  flowCell(pPage, 
1724d 69 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64 29  idx), pgnoChild)
1724e 3b 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e  ;.        VVA_ON
1724f 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53  LY( pCur->pagesS
17250 68 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a 20  huffled = 0 );. 
17251 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61         rc = bala
17252 6e 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 20  nce(pCur, 0);.  
17253 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
17254 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
17255 26 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61  & leafCursorInva
17256 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  lid ){.        /
17257 2a 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 65 20  * The leaf-node 
17258 69 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 6c 6c  is now underfull
17259 20 61 6e 64 20 73 6f 20 74 68 65 20 74 72 65 65   and so the tree
1725a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20   needs to be .  
1725b 20 20 20 20 20 20 2a 2a 20 72 65 62 61 6c 61 6e        ** rebalan
1725c 63 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68  ced. However, th
1725d 65 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 65 72  e balance() oper
1725e 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e 74  ation on the int
1725f 65 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a  ernal.        **
17260 20 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 79 20   node above may 
17261 68 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74 68  have modified th
17262 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74  e structure of t
17263 68 65 20 42 2d 54 72 65 65 20 61 6e 64 0a 20 20  he B-Tree and.  
17264 20 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 65 20        ** so the 
17265 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73  current contents
17266 20 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 50 61   of leafCur.apPa
17267 67 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 75 72  ge[] and leafCur
17268 2e 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20  .aiIdx[].       
17269 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 74   ** may not be t
1726a 72 75 73 74 65 64 2e 0a 20 20 20 20 20 20 20 20  rusted..        
1726b 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74  **.        ** It
1726c 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1726d 20 74 6f 20 63 6f 70 79 20 74 68 65 20 61 6e 63   to copy the anc
1726e 65 73 74 72 79 20 66 72 6f 6d 20 70 43 75 72 2c  estry from pCur,
1726f 20 61 73 20 74 68 65 20 73 61 6d 65 0a 20 20 20   as the same.   
17270 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 28       ** balance(
17271 29 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 61 6c  ) call has inval
17272 69 64 61 74 65 64 20 74 68 65 20 70 43 75 72 2d  idated the pCur-
17273 3e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 69  >apPage[] and ai
17274 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 2a 2a  Idx[].        **
17275 20 61 72 72 61 79 73 2e 20 0a 20 20 20 20 20 20   arrays. .      
17276 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
17277 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 76 65  The call to save
17278 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
17279 20 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 6c 6c   below internall
1727a 79 20 73 61 76 65 73 20 74 68 65 20 0a 20 20 20  y saves the .   
1727b 20 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 61 74       ** key that
1727c 20 6c 65 61 66 43 75 72 20 69 73 20 63 75 72 72   leafCur is curr
1727d 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74  ently pointing t
1727e 6f 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68  o. Currently, th
1727f 65 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ere.        ** a
17280 72 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66  re two copies of
17281 20 74 68 61 74 20 6b 65 79 20 69 6e 20 74 68 65   that key in the
17282 20 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 72 65   tree - one here
17283 20 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 20 20   on the leaf.   
17284 20 20 20 20 20 2a 2a 20 70 61 67 65 20 61 6e 64       ** page and
17285 20 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 6e 74   one on some int
17286 65 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 74 68  ernal node in th
17287 65 20 74 72 65 65 2e 20 54 68 65 20 63 6f 70 79  e tree. The copy
17288 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74   on.        ** t
17289 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 20  he leaf node is 
1728a 61 6c 77 61 79 73 20 74 68 65 20 6e 65 78 74 20  always the next 
1728b 6b 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 64 65  key in tree-orde
1728c 72 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 20  r after the .   
1728d 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 6e 20       ** copy on 
1728e 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  the internal nod
1728f 65 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c 6c 20  e. So, the call 
17290 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e  to sqlite3BtreeN
17291 65 78 74 28 29 0a 20 20 20 20 20 20 20 20 2a 2a  ext().        **
17292 20 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 43 75   calls restoreCu
17293 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 74  rsorPosition() t
17294 6f 20 70 6f 69 6e 74 20 74 68 65 20 63 75 72 73  o point the curs
17295 6f 72 20 74 6f 20 74 68 65 20 63 6f 70 79 0a 20  or to the copy. 
17296 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64         ** stored
17297 20 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   on the internal
17298 20 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 76 61   node, then adva
17299 6e 63 65 73 20 74 6f 20 74 68 65 20 6e 65 78 74  nces to the next
1729a 20 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 20 20   entry,.        
1729b 2a 2a 20 77 68 69 63 68 20 68 61 70 70 65 6e 73  ** which happens
1729c 20 74 6f 20 62 65 20 74 68 65 20 63 6f 70 79 20   to be the copy 
1729d 6f 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 74 68  of the key on th
1729e 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e  e internal node.
1729f 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 74 20  .        ** Net 
172a0 65 66 66 65 63 74 3a 20 6c 65 61 66 43 75 72 20  effect: leafCur 
172a1 69 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 63 6b  is pointing back
172a2 20 74 6f 20 74 68 65 20 64 75 70 6c 69 63 61 74   to the duplicat
172a3 65 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 2a  e cell.        *
172a4 2a 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20  * that needs to 
172a5 62 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e 64 20  be removed, and 
172a6 74 68 65 20 6c 65 61 66 43 75 72 2e 61 70 50 61  the leafCur.apPa
172a7 67 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 20 20  ge[] and.       
172a8 20 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 49 64   ** leafCur.aiId
172a9 78 5b 5d 20 61 72 72 61 79 73 20 61 72 65 20 63  x[] arrays are c
172aa 6f 72 72 65 63 74 2e 0a 20 20 20 20 20 20 20 20  orrect..        
172ab 2a 2f 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f  */.        VVA_O
172ac 4e 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 50 67  NLY( Pgno leafPg
172ad 6e 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 2d 3e  no = pLeafPage->
172ae 70 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 20  pgno );.        
172af 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50  rc = saveCursorP
172b0 6f 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 75 72  osition(&leafCur
172b1 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
172b2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
172b3 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
172b4 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28  qlite3BtreeNext(
172b5 26 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 55 73  &leafCur, &notUs
172b6 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ed);.        }. 
172b7 20 20 20 20 20 20 20 70 4c 65 61 66 50 61 67 65         pLeafPage
172b8 20 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67   = leafCur.apPag
172b9 65 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d  e[leafCur.iPage]
172ba 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
172bb 28 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 6e  ( pLeafPage->pgn
172bc 6f 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b 0a 20  o==leafPgno );. 
172bd 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c         assert( l
172be 65 61 66 43 75 72 2e 61 69 49 64 78 5b 6c 65 61  eafCur.aiIdx[lea
172bf 66 43 75 72 2e 69 50 61 67 65 5d 3d 3d 30 20 29  fCur.iPage]==0 )
172c0 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
172c1 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
172c2 72 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c  rc.       && SQL
172c3 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
172c4 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
172c5 70 4c 65 61 66 50 61 67 65 2d 3e 70 44 62 50 61  pLeafPage->pDbPa
172c6 67 65 29 29 20 0a 20 20 20 20 20 20 29 7b 0a 20  ge)) .      ){. 
172c7 20 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28         dropCell(
172c8 70 4c 65 61 66 50 61 67 65 2c 20 30 2c 20 73 7a  pLeafPage, 0, sz
172c9 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 56  Next);.        V
172ca 56 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 43 75 72  VA_ONLY( leafCur
172cb 2e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d  .pagesShuffled =
172cc 20 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63   0 );.        rc
172cd 20 3d 20 62 61 6c 61 6e 63 65 28 26 6c 65 61 66   = balance(&leaf
172ce 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Cur, 0);.       
172cf 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 72   assert( leafCur
172d0 73 6f 72 49 6e 76 61 6c 69 64 20 7c 7c 20 21 6c  sorInvalid || !l
172d1 65 61 66 43 75 72 2e 70 61 67 65 73 53 68 75 66  eafCur.pagesShuf
172d2 66 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20  fled.           
172d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
172d4 20 20 20 20 20 20 20 20 7c 7c 20 21 70 43 75 72          || !pCur
172d5 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20  ->pagesShuffled 
172d6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
172d7 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
172d8 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73  eReleaseTempCurs
172d9 6f 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20 20  or(&leafCur);.  
172da 7d 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43 45  }else{.    TRACE
172db 28 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c 65  (("DELETE: table
172dc 3d 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d 20  =%d delete from 
172dd 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20  leaf %d\n",.    
172de 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f     pCur->pgnoRoo
172df 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29  t, pPage->pgno))
172e0 3b 0a 20 20 20 20 72 63 20 3d 20 64 72 6f 70 43  ;.    rc = dropC
172e1 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20  ell(pPage, idx, 
172e2 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67  cellSizePtr(pPag
172e3 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 20  e, pCell));.    
172e4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
172e5 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
172e6 62 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 29  balance(pCur, 0)
172e7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
172e8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
172e9 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f  ){.    moveToRoo
172ea 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72  t(pCur);.  }.  r
172eb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
172ec 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
172ed 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 72  BTree table.  Wr
172ee 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c  ite into *piTabl
172ef 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75  e the page.** nu
172f0 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f  mber for the roo
172f1 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65  t page of the ne
172f2 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  w table..**.** T
172f3 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 20  he type of type 
172f4 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
172f5 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   the flags param
172f6 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a  eter.  Only the.
172f7 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  ** following val
172f8 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 65  ues of flags are
172f9 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73   currently in us
172fa 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 73  e.  Other values
172fb 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69   for.** flags mi
172fc 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a  ght not work:.**
172fd 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 4e  .**     BTREE_IN
172fe 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 44  TKEY|BTREE_LEAFD
172ff 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f 72  ATA     Used for
17300 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 68   SQL tables with
17301 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20   rowid keys.**  
17302 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54     BTREE_ZERODAT
17303 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  A               
17304 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20     Used for SQL 
17305 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69  indices.*/.stati
17306 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 74  c int btreeCreat
17307 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  eTable(Btree *p,
17308 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69   int *piTable, i
17309 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 53  nt flags){.  BtS
1730a 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
1730b 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  pBt;.  MemPage *
1730c 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67  pRoot;.  Pgno pg
1730d 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63  noRoot;.  int rc
1730e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1730f 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
17310 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
17311 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73  rt( pBt->inTrans
17312 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
17313 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ITE );.  assert(
17314 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20   !pBt->readOnly 
17315 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
17316 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
17317 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74  M.  rc = allocat
17318 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
17319 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f  &pRoot, &pgnoRoo
1731a 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20  t, 1, 0);.  if( 
1731b 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  rc ){.    return
1731c 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20   rc;.  }.#else. 
1731d 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
1731e 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f  cuum ){.    Pgno
1731f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20   pgnoMove;      
17320 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 68  /* Move a page h
17321 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d  ere to make room
17322 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61   for the root-pa
17323 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67  ge */.    MemPag
17324 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a  e *pPageMove; /*
17325 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f 76   The page to mov
17326 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a  e to. */..    /*
17327 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20   Creating a new 
17328 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 62  table may probab
17329 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e  ly require movin
1732a 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 61  g an existing da
1732b 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f  tabase.    ** to
1732c 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74   make room for t
1732d 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 6f  he new tables ro
1732e 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 65  ot page. In case
1732f 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e 73   this page turns
17330 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62  .    ** out to b
17331 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  e an overflow pa
17332 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f  ge, delete all o
17333 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70  verflow page-map
17334 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 68   caches.    ** h
17335 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73  eld by open curs
17336 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ors..    */.    
17337 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
17338 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b  rflowCache(pBt);
17339 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68  ..    /* Read th
1733a 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b  e value of meta[
1733b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  3] from the data
1733c 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 6e  base to determin
1733d 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 20  e where the.    
1733e 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  ** root page of 
1733f 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 68  the new table sh
17340 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d  ould go. meta[3]
17341 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20   is the largest 
17342 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a  root-page.    **
17343 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 2c   created so far,
17344 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74   so the new root
17345 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b 33  -page is (meta[3
17346 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ]+1)..    */.   
17347 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
17348 65 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20  eeGetMeta(p, 4, 
17349 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20  &pgnoRoot);.    
1734a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1734b 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
1734c 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
1734d 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20  pgnoRoot++;..   
1734e 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f 74   /* The new root
1734f 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65  -page may not be
17350 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20   allocated on a 
17351 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
17352 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  , or the.    ** 
17353 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 67  PENDING_BYTE pag
17354 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68  e..    */.    wh
17355 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50  ile( pgnoRoot==P
17356 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
17357 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20  , pgnoRoot) ||. 
17358 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d         pgnoRoot=
17359 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
1735a 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  GE(pBt) ){.     
1735b 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20   pgnoRoot++;.   
1735c 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
1735d 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20  gnoRoot>=3 );.. 
1735e 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61     /* Allocate a
1735f 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20   page. The page 
17360 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 72  that currently r
17361 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52 6f  esides at pgnoRo
17362 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62  ot will.    ** b
17363 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 61  e moved to the a
17364 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28 75  llocated page (u
17365 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 61  nless the alloca
17366 74 65 64 20 70 61 67 65 20 68 61 70 70 65 6e 73  ted page happens
17367 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 64  .    ** to resid
17368 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a  e at pgnoRoot)..
17369 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
1736a 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
1736b 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f 76  e(pBt, &pPageMov
1736c 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67  e, &pgnoMove, pg
1736d 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20  noRoot, 1);.    
1736e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1736f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
17370 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n rc;.    }..   
17371 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70   if( pgnoMove!=p
17372 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20  gnoRoot ){.     
17373 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20   /* pgnoRoot is 
17374 74 68 65 20 70 61 67 65 20 74 68 61 74 20 77 69  the page that wi
17375 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ll be used for t
17376 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a  he root-page of.
17377 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77        ** the new
17378 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67   table (assuming
17379 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f   an error did no
1737a 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65  t occur). But we
1737b 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 61   were.      ** a
1737c 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76  llocated pgnoMov
1737d 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 28  e. If required (
1737e 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e  i.e. if it was n
1737f 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20  ot allocated.   
17380 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69     ** by extendi
17381 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68  ng the file), th
17382 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 61  e current page a
17383 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d  t position pgnoM
17384 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  ove.      ** is 
17385 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65  already journale
17386 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
17387 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20    u8 eType;.    
17388 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b    Pgno iPtrPage;
17389 0a 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  ..      releaseP
1738a 61 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a  age(pPageMove);.
1738b 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74  .      /* Move t
1738c 68 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c  he page currentl
1738d 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f  y at pgnoRoot to
1738e 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20   pgnoMove. */.  
1738f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17390 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
17391 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f  , pgnoRoot, &pRo
17392 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  ot, 0);.      if
17393 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17394 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
17395 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
17396 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
17397 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74  et(pBt, pgnoRoot
17398 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50  , &eType, &iPtrP
17399 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
1739a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
1739b 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52   eType==PTRMAP_R
1739c 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65  OOTPAGE || eType
1739d 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  ==PTRMAP_FREEPAG
1739e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  E ){.        rel
1739f 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
173a0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
173a1 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
173a2 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21    assert( eType!
173a3 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45  =PTRMAP_ROOTPAGE
173a4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
173a5 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f  ( eType!=PTRMAP_
173a6 46 52 45 45 50 41 47 45 20 29 3b 0a 20 20 20 20  FREEPAGE );.    
173a7 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50    rc = relocateP
173a8 61 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 2c 20  age(pBt, pRoot, 
173a9 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c  eType, iPtrPage,
173aa 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b 0a 20   pgnoMove, 0);. 
173ab 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
173ac 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20 20  (pRoot);..      
173ad 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61  /* Obtain the pa
173ae 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 2a  ge at pgnoRoot *
173af 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  /.      if( rc!=
173b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
173b1 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
173b2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
173b3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
173b4 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f  etPage(pBt, pgno
173b5 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29  Root, &pRoot, 0)
173b6 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
173b7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
173b8 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
173b9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
173ba 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
173bb 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50  rite(pRoot->pDbP
173bc 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
173bd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
173be 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
173bf 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20  Page(pRoot);.   
173c0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
173c1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
173c2 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d  e{.      pRoot =
173c3 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20   pPageMove;.    
173c4 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74  } ..    /* Updat
173c5 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  e the pointer-ma
173c6 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20  p and meta-data 
173c7 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f  with the new roo
173c8 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a  t-page number. *
173c9 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61  /.    rc = ptrma
173ca 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f  pPut(pBt, pgnoRo
173cb 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50  ot, PTRMAP_ROOTP
173cc 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  AGE, 0);.    if(
173cd 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c   rc ){.      rel
173ce 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
173cf 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
173d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
173d1 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
173d2 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67  ateMeta(p, 4, pg
173d3 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28  noRoot);.    if(
173d4 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c   rc ){.      rel
173d5 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
173d6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
173d7 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65  ;.    }..  }else
173d8 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63  {.    rc = alloc
173d9 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
173da 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52  , &pRoot, &pgnoR
173db 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  oot, 1, 0);.    
173dc 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
173dd 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  rc;.  }.#endif. 
173de 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
173df 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
173e0 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
173e1 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70   );.  zeroPage(p
173e2 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54  Root, flags | PT
173e3 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74  F_LEAF);.  sqlit
173e4 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f  e3PagerUnref(pRo
173e5 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ot->pDbPage);.  
173e6 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29  *piTable = (int)
173e7 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75  pgnoRoot;.  retu
173e8 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
173e9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
173ea 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
173eb 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65  reateTable(Btree
173ec 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c   *p, int *piTabl
173ed 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  e, int flags){. 
173ee 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
173ef 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
173f0 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20  .  p->pBt->db = 
173f1 70 2d 3e 64 62 3b 0a 20 20 72 63 20 3d 20 62 74  p->db;.  rc = bt
173f2 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70  reeCreateTable(p
173f3 2c 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73  , piTable, flags
173f4 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
173f5 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
173f6 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
173f7 20 45 72 61 73 65 20 74 68 65 20 67 69 76 65 6e   Erase the given
173f8 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 61   database page a
173f9 6e 64 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64  nd all its child
173fa 72 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  ren.  Return.** 
173fb 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20  the page to the 
173fc 66 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61  freelist..*/.sta
173fd 74 69 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74  tic int clearDat
173fe 61 62 61 73 65 50 61 67 65 28 0a 20 20 42 74 53  abasePage(.  BtS
173ff 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20  hared *pBt,     
17400 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72        /* The BTr
17401 65 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ee that contains
17402 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
17403 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20  Pgno pgno,      
17404 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
17405 6d 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f  mber to clear */
17406 0a 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46  .  int freePageF
17407 6c 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c  lag,     /* Deal
17408 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74  locate page if t
17409 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  rue */.  int *pn
1740a 43 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50  Change.){.  MemP
1740b 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  age *pPage = 0;.
1740c 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69    int rc;.  unsi
1740d 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c  gned char *pCell
1740e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73  ;.  int i;..  as
1740f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
17410 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
17411 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67  tex) );.  if( pg
17412 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 6e  no>pagerPagecoun
17413 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65  t(pBt) ){.    re
17414 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
17415 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20  UPT_BKPT;.  }.. 
17416 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74   rc = getAndInit
17417 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20  Page(pBt, pgno, 
17418 26 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  &pPage);.  if( r
17419 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61  c ) goto clearda
1741a 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a  tabasepage_out;.
1741b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
1741c 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ge->nCell; i++){
1741d 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e  .    pCell = fin
1741e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b  dCell(pPage, i);
1741f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
17420 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72  >leaf ){.      r
17421 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73  c = clearDatabas
17422 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62  ePage(pBt, get4b
17423 79 74 65 28 70 43 65 6c 6c 29 2c 20 31 2c 20 70  yte(pCell), 1, p
17424 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20  nChange);.      
17425 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c  if( rc ) goto cl
17426 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f  eardatabasepage_
17427 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  out;.    }.    r
17428 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50  c = clearCell(pP
17429 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  age, pCell);.   
1742a 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63   if( rc ) goto c
1742b 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65  leardatabasepage
1742c 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  _out;.  }.  if( 
1742d 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
1742e 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61      rc = clearDa
1742f 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20  tabasePage(pBt, 
17430 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
17431 3e 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70  >aData[8]), 1, p
17432 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 69 66  nChange);.    if
17433 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61  ( rc ) goto clea
17434 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
17435 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  t;.  }else if( p
17436 6e 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 61  nChange ){.    a
17437 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e  ssert( pPage->in
17438 74 4b 65 79 20 29 3b 0a 20 20 20 20 2a 70 6e 43  tKey );.    *pnC
17439 68 61 6e 67 65 20 2b 3d 20 70 50 61 67 65 2d 3e  hange += pPage->
1743a 6e 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28  nCell;.  }.  if(
1743b 20 66 72 65 65 50 61 67 65 46 6c 61 67 20 29 7b   freePageFlag ){
1743c 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61  .    rc = freePa
1743d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c  ge(pPage);.  }el
1743e 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c  se if( (rc = sql
1743f 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
17440 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 3d  Page->pDbPage))=
17441 3d 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61  =0 ){.    zeroPa
17442 67 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d  ge(pPage, pPage-
17443 3e 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f  >aData[0] | PTF_
17444 4c 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61  LEAF);.  }..clea
17445 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
17446 74 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  t:.  releasePage
17447 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72  (pPage);.  retur
17448 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  n rc;.}../*.** D
17449 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d  elete all inform
1744a 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e  ation from a sin
1744b 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  gle table in the
1744c 20 64 61 74 61 62 61 73 65 2e 20 20 69 54 61 62   database.  iTab
1744d 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61 67  le is.** the pag
1744e 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
1744f 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c  root of the tabl
17450 65 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72  e.  After this r
17451 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a  outine returns,.
17452 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  ** the root page
17453 20 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20 73   is empty, but s
17454 74 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a  till exists..**.
17455 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
17456 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53  will fail with S
17457 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20  QLITE_LOCKED if 
17458 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70  there are any op
17459 65 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73 6f  en.** read curso
1745a 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e  rs on the table.
1745b 20 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75 72    Open write cur
1745c 73 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20 74  sors are moved t
1745d 6f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66  o the.** root of
1745e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
1745f 2a 20 49 66 20 70 6e 43 68 61 6e 67 65 20 69 73  * If pnChange is
17460 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
17461 74 61 62 6c 65 20 69 54 61 62 6c 65 20 6d 75 73  table iTable mus
17462 74 20 62 65 20 61 6e 20 69 6e 74 6b 65 79 20 74  t be an intkey t
17463 61 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 69 6e 74  able. The.** int
17464 65 67 65 72 20 76 61 6c 75 65 20 70 6f 69 6e 74  eger value point
17465 65 64 20 74 6f 20 62 79 20 70 6e 43 68 61 6e 67  ed to by pnChang
17466 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  e is incremented
17467 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
17468 66 0a 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e 20  f.** entries in 
17469 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  the table..*/.SQ
1746a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1746b 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
1746c 61 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  arTable(Btree *p
1746d 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e  , int iTable, in
1746e 74 20 2a 70 6e 43 68 61 6e 67 65 29 7b 0a 20 20  t *pnChange){.  
1746f 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72  int rc;.  BtShar
17470 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
17471 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
17472 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d  Enter(p);.  pBt-
17473 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61  >db = p->db;.  a
17474 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
17475 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
17476 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 63 68  ;.  if( (rc = ch
17477 65 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69  eckForReadConfli
17478 63 74 73 28 70 2c 20 69 54 61 62 6c 65 2c 20 30  cts(p, iTable, 0
17479 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b  , 1))!=SQLITE_OK
1747a 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f 74 68 69   ){.    /* nothi
1747b 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65  ng to do */.  }e
1747c 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  lse if( SQLITE_O
1747d 4b 21 3d 28 72 63 20 3d 20 73 61 76 65 41 6c 6c  K!=(rc = saveAll
1747e 43 75 72 73 6f 72 73 28 70 42 74 2c 20 69 54 61  Cursors(pBt, iTa
1747f 62 6c 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 20  ble, 0)) ){.    
17480 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f  /* nothing to do
17481 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   */.  }else{.   
17482 20 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62   rc = clearDatab
17483 61 73 65 50 61 67 65 28 70 42 74 2c 20 28 50 67  asePage(pBt, (Pg
17484 6e 6f 29 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e  no)iTable, 0, pn
17485 43 68 61 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73  Change);.  }.  s
17486 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
17487 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
17488 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65  ;.}../*.** Erase
17489 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   all information
1748a 20 69 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20   in a table and 
1748b 61 64 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20  add the root of 
1748c 74 68 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20  the table to.** 
1748d 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45  the freelist.  E
1748e 78 63 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20  xcept, the root 
1748f 6f 66 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65  of the principle
17490 20 74 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20   table (the one 
17491 6f 6e 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73  on.** page 1) is
17492 20 6e 65 76 65 72 20 61 64 64 65 64 20 74 6f 20   never added to 
17493 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a  the freelist..**
17494 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
17495 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20   will fail with 
17496 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66  SQLITE_LOCKED if
17497 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f   there are any o
17498 70 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f  pen.** cursors o
17499 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  n the table..**.
1749a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d  ** If AUTOVACUUM
1749b 20 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20   is enabled and 
1749c 74 68 65 20 70 61 67 65 20 61 74 20 69 54 61 62  the page at iTab
1749d 6c 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61  le is not the la
1749e 73 74 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20  st.** root page 
1749f 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
174a0 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c  file, then the l
174a1 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a  ast root page .*
174a2 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  * in the databas
174a3 65 20 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20  e file is moved 
174a4 69 6e 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f  into the slot fo
174a5 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20  rmerly occupied 
174a6 62 79 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64  by.** iTable and
174a7 20 74 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20   that last slot 
174a8 66 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65  formerly occupie
174a9 64 20 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f  d by the last ro
174aa 6f 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64  ot page.** is ad
174ab 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c  ded to the freel
174ac 69 73 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69  ist instead of i
174ad 54 61 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20  Table.  In this 
174ae 73 61 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74  say, all.** root
174af 20 70 61 67 65 73 20 61 72 65 20 6b 65 70 74 20   pages are kept 
174b0 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
174b1 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
174b2 20 66 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20   file, which.** 
174b3 69 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72  is necessary for
174b4 20 41 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77   AUTOVACUUM to w
174b5 6f 72 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d  ork right.  *piM
174b6 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 74  oved is set to t
174b7 68 65 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62  he .** page numb
174b8 65 72 20 74 68 61 74 20 75 73 65 64 20 74 6f 20  er that used to 
174b9 62 65 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74  be the last root
174ba 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c   page in the fil
174bb 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20  e before.** the 
174bc 6d 6f 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67  move.  If no pag
174bd 65 20 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70  e gets moved, *p
174be 69 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f  iMoved is set to
174bf 20 30 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20   0..** The last 
174c0 72 6f 6f 74 20 70 61 67 65 20 69 73 20 72 65 63  root page is rec
174c1 6f 72 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d  orded in meta[3]
174c2 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f   and the value o
174c3 66 0a 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20  f.** meta[3] is 
174c4 75 70 64 61 74 65 64 20 62 79 20 74 68 69 73 20  updated by this 
174c5 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74  procedure..*/.st
174c6 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 44 72  atic int btreeDr
174c7 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  opTable(Btree *p
174c8 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 69  , Pgno iTable, i
174c9 6e 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20  nt *piMoved){.  
174ca 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67  int rc;.  MemPag
174cb 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20  e *pPage = 0;.  
174cc 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
174cd 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72  p->pBt;..  asser
174ce 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
174cf 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a  oldsMutex(p) );.
174d0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54    assert( p->inT
174d1 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
174d2 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73  E );..  /* It is
174d3 20 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70   illegal to drop
174d4 20 61 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20   a table if any 
174d5 63 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e  cursors are open
174d6 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74   on the.  ** dat
174d7 61 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 62  abase. This is b
174d8 65 63 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76  ecause in auto-v
174d9 61 63 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62  acuum mode the b
174da 61 63 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20  ackend may.  ** 
174db 6e 65 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f  need to move ano
174dc 74 68 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74  ther root-page t
174dd 6f 20 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66  o fill a gap lef
174de 74 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64  t by the deleted
174df 0a 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e  .  ** root page.
174e0 20 49 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73   If an open curs
174e1 6f 72 20 77 61 73 20 75 73 69 6e 67 20 74 68 69  or was using thi
174e2 73 20 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d  s page a problem
174e3 20 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63   would .  ** occ
174e4 75 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ur..  */.  if( p
174e5 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20  Bt->pCursor ){. 
174e6 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63     sqlite3Connec
174e7 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64  tionBlocked(p->d
174e8 62 2c 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 2d  b, pBt->pCursor-
174e9 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20  >pBtree->db);.  
174ea 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
174eb 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
174ec 48 45 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  HE;.  }..  rc = 
174ed 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
174ee 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69  age(pBt, (Pgno)i
174ef 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c 20 30  Table, &pPage, 0
174f0 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
174f1 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20  turn rc;.  rc = 
174f2 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61  sqlite3BtreeClea
174f3 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65  rTable(p, iTable
174f4 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29  , 0);.  if( rc )
174f5 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  {.    releasePag
174f6 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65  e(pPage);.    re
174f7 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
174f8 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20  *piMoved = 0;.. 
174f9 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29 7b   if( iTable>1 ){
174fa 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
174fb 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
174fc 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65     rc = freePage
174fd 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 6c  (pPage);.    rel
174fe 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
174ff 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 70  .#else.    if( p
17500 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
17501 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61 78  {.      Pgno max
17502 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20  RootPgno;.      
17503 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
17504 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20 26  eGetMeta(p, 4, &
17505 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20  maxRootPgno);.  
17506 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
17507 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
17508 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
17509 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ge);.        ret
1750a 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
1750b 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 6c  .      if( iTabl
1750c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29  e==maxRootPgno )
1750d 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
1750e 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
1750f 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 74  dropped is the t
17510 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61  able with the la
17511 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a  rgest root-page.
17512 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65          ** numbe
17513 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  r in the databas
17514 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20  e, put the root 
17515 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65  page on the free
17516 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20   list. .        
17517 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  */.        rc = 
17518 66 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  freePage(pPage);
17519 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
1751a 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
1751b 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1751c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1751d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1751e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1751f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
17520 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   The table being
17521 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f   dropped does no
17522 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 65  t have the large
17523 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20  st root-page.   
17524 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69       ** number i
17525 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
17526 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65  So move the page
17527 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20   that does into 
17528 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
17529 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20  gap left by the 
1752a 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67  deleted root-pag
1752b 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
1752c 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
1752d 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65  Move;.        re
1752e 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
1752f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
17530 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
17531 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50  ge(pBt, maxRootP
17532 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b  gno, &pMove, 0);
17533 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
17534 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17535 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
17536 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
17537 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61       rc = reloca
17538 74 65 50 61 67 65 28 70 42 74 2c 20 70 4d 6f 76  tePage(pBt, pMov
17539 65 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  e, PTRMAP_ROOTPA
1753a 47 45 2c 20 30 2c 20 69 54 61 62 6c 65 2c 20 30  GE, 0, iTable, 0
1753b 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  );.        relea
1753c 73 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20  sePage(pMove);. 
1753d 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
1753e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1753f 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
17540 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
17541 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
17542 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
17543 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70   maxRootPgno, &p
17544 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Move, 0);.      
17545 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
17546 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
17547 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
17548 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
17549 20 3d 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76   = freePage(pMov
1754a 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65  e);.        rele
1754b 61 73 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a  asePage(pMove);.
1754c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1754d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1754e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1754f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
17550 20 20 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d      *piMoved = m
17551 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20  axRootPgno;.    
17552 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65    }..      /* Se
17553 74 20 74 68 65 20 6e 65 77 20 27 6d 61 78 2d 72  t the new 'max-r
17554 6f 6f 74 2d 70 61 67 65 27 20 76 61 6c 75 65 20  oot-page' value 
17555 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
17556 68 65 61 64 65 72 2e 20 54 68 69 73 0a 20 20 20  header. This.   
17557 20 20 20 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64     ** is the old
17558 20 76 61 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c   value less one,
17559 20 6c 65 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69   less one more i
1755a 66 20 74 68 61 74 20 68 61 70 70 65 6e 73 20 74  f that happens t
1755b 6f 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 61 20  o.      ** be a 
1755c 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72  root-page number
1755d 2c 20 6c 65 73 73 20 6f 6e 65 20 61 67 61 69 6e  , less one again
1755e 20 69 66 20 74 68 61 74 20 69 73 20 74 68 65 0a   if that is the.
1755f 20 20 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47        ** PENDING
17560 5f 42 59 54 45 5f 50 41 47 45 2e 0a 20 20 20 20  _BYTE_PAGE..    
17561 20 20 2a 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f    */.      maxRo
17562 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20  otPgno--;.      
17563 69 66 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d  if( maxRootPgno=
17564 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
17565 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  GE(pBt) ){.     
17566 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d     maxRootPgno--
17567 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17568 69 66 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d  if( maxRootPgno=
17569 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70  =PTRMAP_PAGENO(p
1756a 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29  Bt, maxRootPgno)
1756b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52   ){.        maxR
1756c 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20  ootPgno--;.     
1756d 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
1756e 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45   maxRootPgno!=PE
1756f 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
17570 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 20 20 72  pBt) );..      r
17571 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
17572 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c  UpdateMeta(p, 4,
17573 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20   maxRootPgno);. 
17574 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17575 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50  rc = freePage(pP
17576 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 6c 65  age);.      rele
17577 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
17578 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
17579 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20  else{.    /* If 
1757a 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70  sqlite3BtreeDrop
1757b 54 61 62 6c 65 20 77 61 73 20 63 61 6c 6c 65 64  Table was called
1757c 20 6f 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a 20   on page 1. */. 
1757d 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67     zeroPage(pPag
1757e 65 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54  e, PTF_INTKEY|PT
1757f 46 5f 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65  F_LEAF );.    re
17580 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
17581 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
17582 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  c;  .}.SQLITE_PR
17583 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
17584 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28  3BtreeDropTable(
17585 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54  Btree *p, int iT
17586 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76  able, int *piMov
17587 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ed){.  int rc;. 
17588 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
17589 65 72 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d  er(p);.  p->pBt-
1758a 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72  >db = p->db;.  r
1758b 63 20 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62  c = btreeDropTab
1758c 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69  le(p, iTable, pi
1758d 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65  Moved);.  sqlite
1758e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1758f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
17590 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20  ./*.** Read the 
17591 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta-information
17592 20 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61   out of a databa
17593 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30  se file.  Meta[0
17594 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62  ].** is the numb
17595 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73  er of free pages
17596 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68   currently in th
17597 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74  e database.  Met
17598 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20  a[1].** through 
17599 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61  meta[15] are ava
1759a 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62  ilable for use b
1759b 79 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e  y higher layers.
1759c 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20    Meta[0].** is 
1759d 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f  read-only, the o
1759e 74 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77  thers are read/w
1759f 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65  rite..** .** The
175a0 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75   schema layer nu
175a1 6d 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65  mbers meta value
175a2 73 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20  s differently.  
175a3 41 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a  At the schema.**
175a4 20 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20   layer (and the 
175a5 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65  SetCookie and Re
175a6 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73  adCookie opcodes
175a7 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  ) the number of.
175a8 2a 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73  ** free pages is
175a9 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53   not visible.  S
175aa 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74  o Cookie[0] is t
175ab 68 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b  he same as Meta[
175ac 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  1]..*/.SQLITE_PR
175ad 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
175ae 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74  3BtreeGetMeta(Bt
175af 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c  ree *p, int idx,
175b0 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20   u32 *pMeta){.  
175b1 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20  DbPage *pDbPage 
175b2 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  = 0;.  int rc;. 
175b3 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
175b4 70 50 31 3b 0a 20 20 42 74 53 68 61 72 65 64 20  pP1;.  BtShared 
175b5 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a  *pBt = p->pBt;..
175b6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
175b7 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64  ter(p);.  pBt->d
175b8 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a  b = p->db;..  /*
175b9 20 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61 2d   Reading a meta-
175ba 64 61 74 61 20 76 61 6c 75 65 20 72 65 71 75 69  data value requi
175bb 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  res a read-lock 
175bc 6f 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20 68  on page 1 (and h
175bd 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73 71  ence.  ** the sq
175be 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
175bf 65 2e 20 57 65 20 67 72 61 62 20 74 68 69 73 20  e. We grab this 
175c0 6c 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73 20  lock regardless 
175c1 6f 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20 20  of whether or.  
175c2 2a 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49 54  ** not the SQLIT
175c3 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  E_ReadUncommitte
175c4 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 28 74  d flag is set (t
175c5 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20  he table rooted 
175c6 61 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20 69  at page.  ** 1 i
175c7 73 20 74 72 65 61 74 65 64 20 61 73 20 61 20 73  s treated as a s
175c8 70 65 63 69 61 6c 20 63 61 73 65 20 62 79 20 71  pecial case by q
175c9 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54  uerySharedCacheT
175ca 61 62 6c 65 4c 6f 63 6b 28 29 0a 20 20 2a 2a 20  ableLock().  ** 
175cb 61 6e 64 20 73 65 74 53 68 61 72 65 64 43 61 63  and setSharedCac
175cc 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a  heTableLock())..
175cd 20 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72    */.  rc = quer
175ce 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
175cf 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41 44  eLock(p, 1, READ
175d0 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 63  _LOCK);.  if( rc
175d1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
175d2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
175d3 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74  eave(p);.    ret
175d4 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61  urn rc;.  }..  a
175d5 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26  ssert( idx>=0 &&
175d6 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 69 66   idx<=15 );.  if
175d7 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 7b  ( pBt->pPage1 ){
175d8 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74 72  .    /* The b-tr
175d9 65 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 6f  ee is already ho
175da 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63  lding a referenc
175db 65 20 74 6f 20 70 61 67 65 20 31 20 6f 66 20 74  e to page 1 of t
175dc 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
175dd 2a 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69 73  ** file. In this
175de 20 63 61 73 65 20 74 68 65 20 72 65 71 75 69 72   case the requir
175df 65 64 20 6d 65 74 61 2d 64 61 74 61 20 76 61 6c  ed meta-data val
175e0 75 65 20 63 61 6e 20 62 65 20 72 65 61 64 20 64  ue can be read d
175e1 69 72 65 63 74 6c 79 0a 20 20 20 20 2a 2a 20 66  irectly.    ** f
175e2 72 6f 6d 20 74 68 65 20 70 61 67 65 20 64 61 74  rom the page dat
175e3 61 20 6f 66 20 74 68 69 73 20 72 65 66 65 72 65  a of this refere
175e4 6e 63 65 2e 20 54 68 69 73 20 69 73 20 73 6c 69  nce. This is sli
175e5 67 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 61  ghtly faster tha
175e6 6e 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 73 74  n.    ** request
175e7 69 6e 67 20 61 20 6e 65 77 20 72 65 66 65 72 65  ing a new refere
175e8 6e 63 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  nce from the pag
175e9 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 2a 2f  er layer..    */
175ea 0a 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73 69  .    pP1 = (unsi
175eb 67 6e 65 64 20 63 68 61 72 20 2a 29 70 42 74 2d  gned char *)pBt-
175ec 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a  >pPage1->aData;.
175ed 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
175ee 54 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73 20  The b-tree does 
175ef 6e 6f 74 20 68 61 76 65 20 61 20 72 65 66 65 72  not have a refer
175f0 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 6f  ence to page 1 o
175f1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
175f2 69 6c 65 2e 0a 20 20 20 20 2a 2a 20 4f 62 74 61  ile..    ** Obta
175f3 69 6e 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 20  in one from the 
175f4 70 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 20  pager layer..   
175f5 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
175f6 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74  ite3PagerGet(pBt
175f7 2d 3e 70 50 61 67 65 72 2c 20 31 2c 20 26 70 44  ->pPager, 1, &pD
175f8 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
175f9 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rc ){.      sqli
175fa 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
175fb 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
175fc 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 31  c;.    }.    pP1
175fd 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
175fe 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72  r *)sqlite3Pager
175ff 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29  GetData(pDbPage)
17600 3b 0a 20 20 7d 0a 20 20 2a 70 4d 65 74 61 20 3d  ;.  }.  *pMeta =
17601 20 67 65 74 34 62 79 74 65 28 26 70 50 31 5b 33   get4byte(&pP1[3
17602 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 20  6 + idx*4]);..  
17603 2f 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65  /* If the b-tree
17604 20 69 73 20 6e 6f 74 20 68 6f 6c 64 69 6e 67 20   is not holding 
17605 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70  a reference to p
17606 61 67 65 20 31 2c 20 74 68 65 6e 20 6f 6e 65 20  age 1, then one 
17607 77 61 73 20 0a 20 20 2a 2a 20 72 65 71 75 65 73  was .  ** reques
17608 74 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67  ted from the pag
17609 65 72 20 6c 61 79 65 72 20 69 6e 20 74 68 65 20  er layer in the 
1760a 61 62 6f 76 65 20 62 6c 6f 63 6b 2e 20 52 65 6c  above block. Rel
1760b 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a  ease it now..  *
1760c 2f 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 50  /.  if( !pBt->pP
1760d 61 67 65 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  age1 ){.    sqli
1760e 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44  te3PagerUnref(pD
1760f 62 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  bPage);.  }..  /
17610 2a 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d 65  * If autovacuume
17611 64 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e  d is disabled in
17612 20 74 68 69 73 20 62 75 69 6c 64 20 62 75 74 20   this build but 
17613 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f  we are trying to
17614 20 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61 6e   .  ** access an
17615 20 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64 61   autovacuumed da
17616 74 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61 6b  tabase, then mak
17617 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72  e the database r
17618 65 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a 23  eadonly. .  */.#
17619 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1761a 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69  T_AUTOVACUUM.  i
1761b 66 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70 4d  f( idx==4 && *pM
1761c 65 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 61  eta>0 ) pBt->rea
1761d 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 69  dOnly = 1;.#endi
1761e 66 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65  f..  /* Grab the
1761f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 70 61   read-lock on pa
17620 67 65 20 31 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  ge 1. */.  rc = 
17621 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61  setSharedCacheTa
17622 62 6c 65 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45  bleLock(p, 1, RE
17623 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 73 71 6c 69  AD_LOCK);.  sqli
17624 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
17625 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
17626 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65  ../*.** Write me
17627 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62  ta-information b
17628 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ack into the dat
17629 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20  abase.  Meta[0] 
1762a 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20  is.** read-only 
1762b 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77  and may not be w
1762c 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ritten..*/.SQLIT
1762d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1762e 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
1762f 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69  Meta(Btree *p, i
17630 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74  nt idx, u32 iMet
17631 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  a){.  BtShared *
17632 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
17633 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
17634 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  P1;.  int rc;.  
17635 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26  assert( idx>=1 &
17636 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73  & idx<=15 );.  s
17637 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
17638 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d  (p);.  pBt->db =
17639 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74   p->db;.  assert
1763a 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
1763b 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61  ANS_WRITE );.  a
1763c 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67  ssert( pBt->pPag
1763d 65 31 21 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d  e1!=0 );.  pP1 =
1763e 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44   pBt->pPage1->aD
1763f 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ata;.  rc = sqli
17640 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42  te3PagerWrite(pB
17641 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61  t->pPage1->pDbPa
17642 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ge);.  if( rc==S
17643 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17644 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33 36  put4byte(&pP1[36
17645 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61   + idx*4], iMeta
17646 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
17647 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
17648 4d 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d 37  M.    if( idx==7
17649 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1764a 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
1764b 6d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b  m || iMeta==0 );
1764c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
1764d 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61  Meta==0 || iMeta
1764e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 42 74  ==1 );.      pBt
1764f 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28  ->incrVacuum = (
17650 75 38 29 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a  u8)iMeta;.    }.
17651 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c  #endif.  }.  sql
17652 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
17653 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
17654 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
17655 74 68 65 20 66 6c 61 67 20 62 79 74 65 20 61 74  the flag byte at
17656 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
17657 66 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  f the page that 
17658 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73  the cursor.** is
17659 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1765a 69 6e 67 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54  ing to..*/.SQLIT
1765b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1765c 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 28  lite3BtreeFlags(
1765d 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
1765e 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74  .  /* TODO: What
1765f 20 61 62 6f 75 74 20 43 55 52 53 4f 52 5f 52 45   about CURSOR_RE
17660 51 55 49 52 45 53 45 45 4b 20 73 74 61 74 65 3f  QUIRESEEK state?
17661 20 50 72 6f 62 61 62 6c 79 20 6e 65 65 64 20 74   Probably need t
17662 6f 20 63 61 6c 6c 0a 20 20 2a 2a 20 72 65 73 74  o call.  ** rest
17663 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
17664 6e 28 29 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20  n() here..  */. 
17665 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
17666 0a 20 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72  .  restoreCursor
17667 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
17668 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
17669 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1766a 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 63  ge];.  assert( c
1766b 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1766c 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
1766d 74 28 20 70 50 61 67 65 21 3d 30 20 29 3b 0a 20  t( pPage!=0 );. 
1766e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1766f 70 42 74 3d 3d 70 43 75 72 2d 3e 70 42 74 20 29  pBt==pCur->pBt )
17670 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  ;.  return pPage
17671 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
17672 64 72 4f 66 66 73 65 74 5d 3b 0a 7d 0a 0a 23 69  drOffset];.}..#i
17673 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17674 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f 2a 0a  T_BTREECOUNT./*.
17675 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
17676 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69 73 20  ument, pCur, is 
17677 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20  a cursor opened 
17678 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65 2e 20  on some b-tree. 
17679 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e 75 6d  Count the.** num
1767a 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
1767b 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61 6e 64  n the b-tree and
1767c 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   write the resul
1767d 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e 0a 2a  t to *pnEntry..*
1767e 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  *.** SQLITE_OK i
1767f 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
17680 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 73  e operation is s
17681 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78 65 63  uccessfully exec
17682 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77  uted. .** Otherw
17683 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72  ise, if an error
17684 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20   is encountered 
17685 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72 72 6f  (i.e. an IO erro
17686 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a 2a 2a  r or database.**
17687 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61 6e 20   corruption) an 
17688 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
17689 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
1768a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1768b 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1768c 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20  eCount(BtCursor 
1768d 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e 45 6e  *pCur, i64 *pnEn
1768e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45 6e 74  try){.  i64 nEnt
1768f 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ry = 0;         
17690 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17691 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
17692 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f 0a 20  in *pnEntry */. 
17693 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
17694 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17695 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
17696 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 3d 20 6d  code */.  rc = m
17697 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b  oveToRoot(pCur);
17698 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 61 6e  ..  /* Unless an
17699 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
1769a 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f  he following loo
1769b 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65 72 61  p runs one itera
1769c 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a 20 20  tion for each.  
1769d 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 42  ** page in the B
1769e 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20  -Tree structure 
1769f 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 6f  (not including o
176a0 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29 2e 20  verflow pages). 
176a1 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72  .  */.  while( r
176a2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
176a3 20 20 20 20 69 6e 74 20 69 49 64 78 3b 20 20 20      int iIdx;   
176a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
176a5 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
176a6 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20 69 6e  of child node in
176a7 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 20 20 4d   parent */.    M
176a8 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20  emPage *pPage;  
176a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
176aa 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70 61 67    /* Current pag
176ab 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20  e of the b-tree 
176ac 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  */..    /* If th
176ad 69 73 20 69 73 20 61 20 6c 65 61 66 20 70 61 67  is is a leaf pag
176ae 65 20 6f 72 20 74 68 65 20 74 72 65 65 20 69 73  e or the tree is
176af 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b 65 79 20   not an int-key 
176b0 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 20 20 20  tree, then .    
176b1 2a 2a 20 74 68 69 73 20 70 61 67 65 20 63 6f 6e  ** this page con
176b2 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 6c 65 20  tains countable 
176b3 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 65 6d 65  entries. Increme
176b4 6e 74 20 74 68 65 20 65 6e 74 72 79 20 63 6f 75  nt the entry cou
176b5 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 63 63 6f  nter.    ** acco
176b6 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 2a 2f 0a  rdingly..    */.
176b7 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72      pPage = pCur
176b8 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
176b9 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70  Page];.    if( p
176ba 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 70  Page->leaf || !p
176bb 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a  Page->intKey ){.
176bc 20 20 20 20 20 20 6e 45 6e 74 72 79 20 2b 3d 20        nEntry += 
176bd 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20  pPage->nCell;.  
176be 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67    }..    /* pPag
176bf 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65  e is a leaf node
176c0 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61 76 69  . This loop navi
176c1 67 61 74 65 73 20 74 68 65 20 63 75 72 73 6f 72  gates the cursor
176c2 20 73 6f 20 74 68 61 74 20 69 74 20 0a 20 20 20   so that it .   
176c3 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   ** points to th
176c4 65 20 66 69 72 73 74 20 69 6e 74 65 72 69 6f 72  e first interior
176c5 20 63 65 6c 6c 20 74 68 61 74 20 69 74 20 70 6f   cell that it po
176c6 69 6e 74 73 20 74 6f 20 74 68 65 20 70 61 72 65  ints to the pare
176c7 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65  nt of.    ** the
176c8 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68   next page in th
176c9 65 20 74 72 65 65 20 74 68 61 74 20 68 61 73 20  e tree that has 
176ca 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 76 69 73  not yet been vis
176cb 69 74 65 64 2e 20 54 68 65 0a 20 20 20 20 2a 2a  ited. The.    **
176cc 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
176cd 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c 75 65 20  r->iPage] value 
176ce 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e  is set to the in
176cf 64 65 78 20 6f 66 20 74 68 65 20 70 61 72 65 6e  dex of the paren
176d0 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 6f 66  t cell.    ** of
176d1 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 74 6f   the page, or to
176d2 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
176d3 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61 67 65  ells in the page
176d4 20 69 66 20 74 68 65 20 6e 65 78 74 20 70 61 67   if the next pag
176d5 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 69 73 69  e.    ** to visi
176d6 74 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 63  t is the right-c
176d7 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65  hild of its pare
176d8 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  nt..    **.    *
176d9 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 73 20 69  * If all pages i
176da 6e 20 74 68 65 20 74 72 65 65 20 68 61 76 65 20  n the tree have 
176db 62 65 65 6e 20 76 69 73 69 74 65 64 2c 20 72 65  been visited, re
176dc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 74  turn SQLITE_OK t
176dd 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c  o the.    ** cal
176de 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ler..    */.    
176df 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  if( pPage->leaf 
176e0 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20  ){.      do {.  
176e1 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e        if( pCur->
176e2 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20  iPage==0 ){.    
176e3 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70 61 67        /* All pag
176e4 65 73 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65  es of the b-tree
176e5 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74   have been visit
176e6 65 64 2e 20 52 65 74 75 72 6e 20 73 75 63 63 65  ed. Return succe
176e7 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20 20 20  ssfully. */.    
176e8 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79 20 3d        *pnEntry =
176e9 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 20 20 20   nEntry;.       
176ea 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
176eb 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  _OK;.        }. 
176ec 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
176ed 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28  reeMoveToParent(
176ee 70 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 77 68  pCur);.      }wh
176ef 69 6c 65 20 28 20 70 43 75 72 2d 3e 61 69 49 64  ile ( pCur->aiId
176f0 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d  x[pCur->iPage]>=
176f1 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
176f2 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
176f3 20 29 3b 0a 0a 20 20 20 20 20 20 70 43 75 72 2d   );..      pCur-
176f4 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
176f5 67 65 5d 2b 2b 3b 0a 20 20 20 20 20 20 70 50 61  ge]++;.      pPa
176f6 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
176f7 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
176f8 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
176f9 73 63 65 6e 64 20 74 6f 20 74 68 65 20 63 68 69  scend to the chi
176fa 6c 64 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 63  ld node of the c
176fb 65 6c 6c 20 74 68 61 74 20 74 68 65 20 63 75 72  ell that the cur
176fc 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 0a 20  sor currently . 
176fd 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 61 74 2e     ** points at.
176fe 20 54 68 69 73 20 69 73 20 74 68 65 20 72 69 67   This is the rig
176ff 68 74 2d 63 68 69 6c 64 20 69 66 20 28 69 49 64  ht-child if (iId
17700 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29  x==pPage->nCell)
17701 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 49 64  ..    */.    iId
17702 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  x = pCur->aiIdx[
17703 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
17704 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50 61 67    if( iIdx==pPag
17705 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20  e->nCell ){.    
17706 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69    rc = moveToChi
17707 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74  ld(pCur, get4byt
17708 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e(&pPage->aData[
17709 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
1770a 2b 38 5d 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65  +8]));.    }else
1770b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76  {.      rc = mov
1770c 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67  eToChild(pCur, g
1770d 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c  et4byte(findCell
1770e 28 70 50 61 67 65 2c 20 69 49 64 78 29 29 29 3b  (pPage, iIdx)));
1770f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
17710 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63   An error has oc
17711 63 75 72 72 65 64 2e 20 52 65 74 75 72 6e 20 61  curred. Return a
17712 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f  n error code. */
17713 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
17714 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
17715 74 75 72 6e 20 74 68 65 20 70 61 67 65 72 20 61  turn the pager a
17716 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
17717 20 42 54 72 65 65 2e 20 20 54 68 69 73 20 72 6f   BTree.  This ro
17718 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
17719 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64  r.** testing and
1771a 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
1771b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1771c 54 45 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65  TE Pager *sqlite
1771d 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 65  3BtreePager(Btre
1771e 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  e *p){.  return 
1771f 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a  p->pBt->pPager;.
17720 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
17721 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
17722 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 70 70  _CHECK./*.** App
17723 65 6e 64 20 61 20 6d 65 73 73 61 67 65 20 74 6f  end a message to
17724 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
17725 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74  ge string..*/.st
17726 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41  atic void checkA
17727 70 70 65 6e 64 4d 73 67 28 0a 20 20 49 6e 74 65  ppendMsg(.  Inte
17728 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c  grityCk *pCheck,
17729 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 31 2c 0a  .  char *zMsg1,.
1772a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
1772b 6f 72 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a  ormat,.  ....){.
1772c 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
1772d 69 66 28 20 21 70 43 68 65 63 6b 2d 3e 6d 78 45  if( !pCheck->mxE
1772e 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  rr ) return;.  p
1772f 43 68 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b 0a  Check->mxErr--;.
17730 20 20 70 43 68 65 63 6b 2d 3e 6e 45 72 72 2b 2b    pCheck->nErr++
17731 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
17732 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 69 66 28   zFormat);.  if(
17733 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e   pCheck->errMsg.
17734 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 73 71 6c  nChar ){.    sql
17735 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
17736 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d  nd(&pCheck->errM
17737 73 67 2c 20 22 5c 6e 22 2c 20 31 29 3b 0a 20 20  sg, "\n", 1);.  
17738 7d 0a 20 20 69 66 28 20 7a 4d 73 67 31 20 29 7b  }.  if( zMsg1 ){
17739 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41  .    sqlite3StrA
1773a 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65  ccumAppend(&pChe
1773b 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 7a 4d 73 67  ck->errMsg, zMsg
1773c 31 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71  1, -1);.  }.  sq
1773d 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 70  lite3VXPrintf(&p
1773e 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 31  Check->errMsg, 1
1773f 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
17740 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
17741 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d  if( pCheck->errM
17742 73 67 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  sg.mallocFailed 
17743 29 7b 0a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d  ){.    pCheck->m
17744 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
17745 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
17746 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
17747 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a  EGRITY_CHECK */.
17748 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
17749 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
1774a 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 64 64 20 31  HECK./*.** Add 1
1774b 20 74 6f 20 74 68 65 20 72 65 66 65 72 65 6e 63   to the referenc
1774c 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 61 67 65  e count for page
1774d 20 69 50 61 67 65 2e 20 20 49 66 20 74 68 69 73   iPage.  If this
1774e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a   is the second.*
1774f 2a 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  * reference to t
17750 68 65 20 70 61 67 65 2c 20 61 64 64 20 61 6e 20  he page, add an 
17751 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f  error message to
17752 20 70 43 68 65 63 6b 2d 3e 7a 45 72 72 4d 73 67   pCheck->zErrMsg
17753 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66  ..** Return 1 if
17754 20 74 68 65 72 65 20 61 72 65 20 32 20 6f 72 65   there are 2 ore
17755 20 6d 6f 72 65 20 72 65 66 65 72 65 6e 63 65 73   more references
17756 20 74 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64   to the page and
17757 20 30 20 69 66 0a 2a 2a 20 69 66 20 74 68 69 73   0 if.** if this
17758 20 69 73 20 74 68 65 20 66 69 72 73 74 20 72 65   is the first re
17759 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70  ference to the p
1775a 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20  age..**.** Also 
1775b 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70  check that the p
1775c 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 69 6e  age number is in
1775d 20 62 6f 75 6e 64 73 2e 0a 2a 2f 0a 73 74 61 74   bounds..*/.stat
1775e 69 63 20 69 6e 74 20 63 68 65 63 6b 52 65 66 28  ic int checkRef(
1775f 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68  IntegrityCk *pCh
17760 65 63 6b 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c  eck, Pgno iPage,
17761 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 29   char *zContext)
17762 7b 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30  {.  if( iPage==0
17763 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69   ) return 1;.  i
17764 66 28 20 69 50 61 67 65 3e 70 43 68 65 63 6b 2d  f( iPage>pCheck-
17765 3e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 63 68  >nPage ){.    ch
17766 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
17767 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  eck, zContext, "
17768 69 6e 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d  invalid page num
17769 62 65 72 20 25 64 22 2c 20 69 50 61 67 65 29 3b  ber %d", iPage);
1776a 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
1776b 20 7d 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d   }.  if( pCheck-
1776c 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 3d 3d 31  >anRef[iPage]==1
1776d 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70   ){.    checkApp
1776e 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
1776f 43 6f 6e 74 65 78 74 2c 20 22 32 6e 64 20 72 65  Context, "2nd re
17770 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20  ference to page 
17771 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20  %d", iPage);.   
17772 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
17773 20 72 65 74 75 72 6e 20 20 28 70 43 68 65 63 6b   return  (pCheck
17774 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 2b 2b  ->anRef[iPage]++
17775 29 3e 31 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  )>1;.}..#ifndef 
17776 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
17777 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 68 65  VACUUM./*.** Che
17778 63 6b 20 74 68 61 74 20 74 68 65 20 65 6e 74 72  ck that the entr
17779 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72  y in the pointer
1777a 2d 6d 61 70 20 66 6f 72 20 70 61 67 65 20 69 43  -map for page iC
1777b 68 69 6c 64 20 6d 61 70 73 20 74 6f 20 0a 2a 2a  hild maps to .**
1777c 20 70 61 67 65 20 69 50 61 72 65 6e 74 2c 20 70   page iParent, p
1777d 6f 69 6e 74 65 72 20 74 79 70 65 20 70 74 72 54  ointer type ptrT
1777e 79 70 65 2e 20 49 66 20 6e 6f 74 2c 20 61 70 70  ype. If not, app
1777f 65 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  end an error mes
17780 73 61 67 65 0a 2a 2a 20 74 6f 20 70 43 68 65 63  sage.** to pChec
17781 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
17782 64 20 63 68 65 63 6b 50 74 72 6d 61 70 28 0a 20  d checkPtrmap(. 
17783 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43   IntegrityCk *pC
17784 68 65 63 6b 2c 20 20 20 2f 2a 20 49 6e 74 65 67  heck,   /* Integ
17785 72 69 74 79 20 63 68 65 63 6b 20 63 6f 6e 74 65  rity check conte
17786 78 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 43 68  xt */.  Pgno iCh
17787 69 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ild,           /
17788 2a 20 43 68 69 6c 64 20 70 61 67 65 20 6e 75 6d  * Child page num
17789 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 54 79 70  ber */.  u8 eTyp
1778a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
1778b 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e  /* Expected poin
1778c 74 65 72 20 6d 61 70 20 74 79 70 65 20 2a 2f 0a  ter map type */.
1778d 20 20 50 67 6e 6f 20 69 50 61 72 65 6e 74 2c 20    Pgno iParent, 
1778e 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65           /* Expe
1778f 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70  cted pointer map
17790 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d   parent page num
17791 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ber */.  char *z
17792 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20 20  Context         
17793 2f 2a 20 43 6f 6e 74 65 78 74 20 64 65 73 63 72  /* Context descr
17794 69 70 74 69 6f 6e 20 28 75 73 65 64 20 66 6f 72  iption (used for
17795 20 65 72 72 6f 72 20 6d 73 67 29 20 2a 2f 0a 29   error msg) */.)
17796 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 38  {.  int rc;.  u8
17797 20 65 50 74 72 6d 61 70 54 79 70 65 3b 0a 20 20   ePtrmapType;.  
17798 50 67 6e 6f 20 69 50 74 72 6d 61 70 50 61 72 65  Pgno iPtrmapPare
17799 6e 74 3b 0a 0a 20 20 72 63 20 3d 20 70 74 72 6d  nt;..  rc = ptrm
1779a 61 70 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 42  apGet(pCheck->pB
1779b 74 2c 20 69 43 68 69 6c 64 2c 20 26 65 50 74 72  t, iChild, &ePtr
1779c 6d 61 70 54 79 70 65 2c 20 26 69 50 74 72 6d 61  mapType, &iPtrma
1779d 70 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20  pParent);.  if( 
1779e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1779f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
177a0 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65  ITE_NOMEM ) pChe
177a1 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ck->mallocFailed
177a2 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41   = 1;.    checkA
177a3 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
177a4 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c   zContext, "Fail
177a5 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61  ed to read ptrma
177a6 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c  p key=%d", iChil
177a7 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  d);.    return;.
177a8 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d    }..  if( ePtrm
177a9 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c  apType!=eType ||
177aa 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d   iPtrmapParent!=
177ab 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63  iParent ){.    c
177ac 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
177ad 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
177ae 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20  .      "Bad ptr 
177af 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64  map entry key=%d
177b0 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64   expected=(%d,%d
177b1 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20  ) got=(%d,%d)", 
177b2 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65  .      iChild, e
177b3 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65  Type, iParent, e
177b4 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72  PtrmapType, iPtr
177b5 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a  mapParent);.  }.
177b6 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
177b7 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
177b8 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c  ity of the freel
177b9 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65  ist or of an ove
177ba 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e  rflow page list.
177bb 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20  .** Verify that 
177bc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
177bd 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ges on the list 
177be 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  is N..*/.static 
177bf 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a  void checkList(.
177c0 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70    IntegrityCk *p
177c1 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67  Check,  /* Integ
177c2 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f  rity checking co
177c3 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
177c4 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20  sFreeList,      
177c5 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66   /* True for a f
177c6 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20  reelist.  False 
177c7 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  for overflow pag
177c8 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  e list */.  int 
177c9 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  iPage,          
177ca 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
177cb 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20   for first page 
177cc 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  in the list */. 
177cd 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20   int N,         
177ce 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74         /* Expect
177cf 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ed number of pag
177d0 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a  es in the list *
177d1 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  /.  char *zConte
177d2 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  xt        /* Con
177d3 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d  text for error m
177d4 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20  essages */.){.  
177d5 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70  int i;.  int exp
177d6 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74  ected = N;.  int
177d7 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b   iFirst = iPage;
177d8 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20  .  while( N-- > 
177d9 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45  0 && pCheck->mxE
177da 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65  rr ){.    DbPage
177db 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20   *pOvflPage;.   
177dc 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
177dd 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69  pOvflData;.    i
177de 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20  f( iPage<1 ){.  
177df 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
177e0 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
177e1 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25  ext,.         "%
177e2 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69  d of %d pages mi
177e3 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66  ssing from overf
177e4 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e  low list startin
177e5 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20  g at %d",.      
177e6 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65      N+1, expecte
177e7 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20  d, iFirst);.    
177e8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
177e9 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28     if( checkRef(
177ea 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a  pCheck, iPage, z
177eb 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b  Context) ) break
177ec 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
177ed 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b  3PagerGet(pCheck
177ee 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29  ->pPager, (Pgno)
177ef 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67  iPage, &pOvflPag
177f0 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  e) ){.      chec
177f1 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
177f2 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61  k, zContext, "fa
177f3 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65  iled to get page
177f4 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20   %d", iPage);.  
177f5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
177f6 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d  .    pOvflData =
177f7 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
177f8 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
177f9 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29  tData(pOvflPage)
177fa 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65  ;.    if( isFree
177fb 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  List ){.      in
177fc 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26  t n = get4byte(&
177fd 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23  pOvflData[4]);.#
177fe 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
177ff 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
17800 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e      if( pCheck->
17801 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
17802 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
17803 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69  Ptrmap(pCheck, i
17804 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
17805 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65  EPAGE, 0, zConte
17806 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
17807 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e  dif.      if( n>
17808 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61  pCheck->pBt->usa
17809 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20  bleSize/4-2 ){. 
1780a 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65         checkAppe
1780b 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
1780c 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
1780d 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61     "freelist lea
1780e 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20  f count too big 
1780f 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61  on page %d", iPa
17810 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d  ge);.        N--
17811 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
17812 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
17813 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<n; i++){.     
17814 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50       Pgno iFreeP
17815 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26  age = get4byte(&
17816 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d  pOvflData[8+i*4]
17817 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
17818 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
17819 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  M.          if( 
1781a 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74  pCheck->pBt->aut
1781b 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
1781c 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
1781d 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65  ap(pCheck, iFree
1781e 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
1781f 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65  EPAGE, 0, zConte
17820 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  xt);.          }
17821 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
17822 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63    checkRef(pChec
17823 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43  k, iFreePage, zC
17824 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20  ontext);.       
17825 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20   }.        N -= 
17826 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
17827 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
17828 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
17829 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20      else{.      
1782a 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62  /* If this datab
1782b 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
1782c 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61  o-vacuum and iPa
1782d 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61  ge is not the la
1782e 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65  st.      ** page
1782f 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   in this overflo
17830 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68  w list, check th
17831 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  at the pointer-m
17832 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20  ap entry for.   
17833 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77     ** the follow
17834 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73  ing page matches
17835 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f   iPage..      */
17836 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63  .      if( pChec
17837 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  k->pBt->autoVacu
17838 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20  um && N>0 ){.   
17839 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74       i = get4byt
1783a 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20  e(pOvflData);.  
1783b 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
1783c 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52  p(pCheck, i, PTR
1783d 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69  MAP_OVERFLOW2, i
1783e 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  Page, zContext);
1783f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
17840 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20  endif.    iPage 
17841 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c  = get4byte(pOvfl
17842 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74  Data);.    sqlit
17843 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76  e3PagerUnref(pOv
17844 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23  flPage);.  }.}.#
17845 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
17846 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
17847 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  HECK */..#ifndef
17848 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
17849 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a  EGRITY_CHECK./*.
1784a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61  ** Do various sa
1784b 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61  nity checks on a
1784c 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20   single page of 
1784d 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a  a tree.  Return.
1784e 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74  ** the tree dept
1784f 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72  h.  Root pages r
17850 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74  eturn 0.  Parent
17851 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a  s of root pages.
17852 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64  ** return 1, and
17853 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a   so forth..** .*
17854 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61  * These checks a
17855 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20  re done:.**.**  
17856 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72      1.  Make sur
17857 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64  e that cells and
17858 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e   freeblocks do n
17859 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20  ot overlap.**   
1785a 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69         but combi
1785b 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79  ne to completely
1785c 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e   cover the page.
1785d 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b  .**  NO  2.  Mak
1785e 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73  e sure cell keys
1785f 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a   are in order..*
17860 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20  *  NO  3.  Make 
17861 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c  sure no key is l
17862 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
17863 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64  l to zLowerBound
17864 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61  ..**  NO  4.  Ma
17865 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69  ke sure no key i
17866 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
17867 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65  r equal to zUppe
17868 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20  rBound..**      
17869 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e  5.  Check the in
1786a 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66  tegrity of overf
1786b 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20  low pages..**   
1786c 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65     6.  Recursive
1786d 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65  ly call checkTre
1786e 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69  ePage on all chi
1786f 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37  ldren..**      7
17870 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74  .  Verify that t
17871 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20  he depth of all 
17872 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20  children is the 
17873 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e  same..**      8.
17874 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73    Make sure this
17875 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73   page is at leas
17876 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c  t 33% full or el
17877 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20  se it is.**     
17878 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66       the root of
17879 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74   the tree..*/.st
1787a 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72  atic int checkTr
1787b 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72  eePage(.  Integr
1787c 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20  ityCk *pCheck,  
1787d 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74  /* Context for t
1787e 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20  he sanity check 
1787f 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20  */.  int iPage, 
17880 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
17881 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
17882 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a   page to check *
17883 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e  /.  char *zParen
17884 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72  tContext  /* Par
17885 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29  ent context */.)
17886 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
17887 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c  ge;.  int i, rc,
17888 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f   depth, d2, pgno
17889 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72  , cnt;.  int hdr
1788a 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69  , cellStart;.  i
1788b 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a  nt nCell;.  u8 *
1788c 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64  data;.  BtShared
1788d 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61   *pBt;.  int usa
1788e 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20  bleSize;.  char 
1788f 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20  zContext[100];. 
17890 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a   char *hit = 0;.
17891 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
17892 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74  ntf(sizeof(zCont
17893 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  ext), zContext, 
17894 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61  "Page %d: ", iPa
17895 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ge);..  /* Check
17896 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65   that the page e
17897 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74  xists.  */.  pBt
17898 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a   = pCheck->pBt;.
17899 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
1789a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
1789b 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29    if( iPage==0 )
1789c 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
1789d 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b   checkRef(pCheck
1789e 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74  , iPage, zParent
1789f 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72  Context) ) retur
178a0 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d  n 0;.  if( (rc =
178a1 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
178a2 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29  Page(pBt, (Pgno)
178a3 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30  iPage, &pPage, 0
178a4 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ))!=0 ){.    if(
178a5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
178a6 4d 20 29 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c  M ) pCheck->mall
178a7 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
178a8 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
178a9 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
178aa 74 2c 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c  t,.       "unabl
178ab 65 20 74 6f 20 67 65 74 20 74 68 65 20 70 61 67  e to get the pag
178ac 65 2e 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64  e. error code=%d
178ad 22 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75  ", rc);.    retu
178ae 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
178af 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72  (rc = sqlite3Btr
178b0 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65  eeInitPage(pPage
178b1 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  ))!=0 ){.    ass
178b2 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
178b3 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a 20 54  CORRUPT );  /* T
178b4 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65  he only possible
178b5 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e 69 74   error from Init
178b6 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 65 63  Page */.    chec
178b7 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
178b8 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20  k, zContext, .  
178b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
178ba 20 22 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e   "sqlite3BtreeIn
178bb 69 74 50 61 67 65 28 29 20 72 65 74 75 72 6e 73  itPage() returns
178bc 20 65 72 72 6f 72 20 63 6f 64 65 20 25 64 22 2c   error code %d",
178bd 20 72 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73   rc);.    releas
178be 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
178bf 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
178c0 0a 20 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20  .  /* Check out 
178c1 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20  all the cells.. 
178c2 20 2a 2f 0a 20 20 64 65 70 74 68 20 3d 20 30 3b   */.  depth = 0;
178c3 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
178c4 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43  age->nCell && pC
178c5 68 65 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b  heck->mxErr; i++
178c6 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c  ){.    u8 *pCell
178c7 3b 0a 20 20 20 20 75 33 32 20 73 7a 3b 0a 20 20  ;.    u32 sz;.  
178c8 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
178c9 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 70  ..    /* Check p
178ca 61 79 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20  ayload overflow 
178cb 70 61 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20  pages.    */.   
178cc 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
178cd 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78  f(sizeof(zContex
178ce 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20  t), zContext,.  
178cf 20 20 20 20 20 20 20 20 20 20 20 22 4f 6e 20 74             "On t
178d0 72 65 65 20 70 61 67 65 20 25 64 20 63 65 6c 6c  ree page %d cell
178d1 20 25 64 3a 20 22 2c 20 69 50 61 67 65 2c 20 69   %d: ", iPage, i
178d2 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66  );.    pCell = f
178d3 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 69 29  indCell(pPage,i)
178d4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
178d5 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
178d6 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e  Page, pCell, &in
178d7 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e  fo);.    sz = in
178d8 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 20 69 66  fo.nData;.    if
178d9 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  ( !pPage->intKey
178da 20 29 20 73 7a 20 2b 3d 20 28 69 6e 74 29 69 6e   ) sz += (int)in
178db 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 61 73 73  fo.nKey;.    ass
178dc 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f 2e 6e 50  ert( sz==info.nP
178dd 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20 20 69 66  ayload );.    if
178de 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  ( (sz>info.nLoca
178df 6c 29 20 0a 20 20 20 20 20 26 26 20 28 26 70 43  l) .     && (&pC
178e0 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c  ell[info.iOverfl
178e1 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e 61 44 61  ow]<=&pPage->aDa
178e2 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  ta[pBt->usableSi
178e3 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  ze]).    ){.    
178e4 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 28 73    int nPage = (s
178e5 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20  z - info.nLocal 
178e6 2b 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 35  + usableSize - 5
178e7 29 2f 28 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  )/(usableSize - 
178e8 34 29 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70  4);.      Pgno p
178e9 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79  gnoOvfl = get4by
178ea 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  te(&pCell[info.i
178eb 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66 6e  Overflow]);.#ifn
178ec 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
178ed 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
178ee 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
178ef 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  cuum ){.        
178f0 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65  checkPtrmap(pChe
178f1 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 54  ck, pgnoOvfl, PT
178f2 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20  RMAP_OVERFLOW1, 
178f3 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29  iPage, zContext)
178f4 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
178f5 0a 20 20 20 20 20 20 63 68 65 63 6b 4c 69 73 74  .      checkList
178f6 28 70 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e 6f  (pCheck, 0, pgno
178f7 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43 6f  Ovfl, nPage, zCo
178f8 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a 20  ntext);.    }.. 
178f9 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e 69     /* Check sani
178fa 74 79 20 6f 66 20 6c 65 66 74 20 63 68 69 6c 64  ty of left child
178fb 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   page..    */.  
178fc 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
178fd 61 66 20 29 7b 0a 20 20 20 20 20 20 70 67 6e 6f  af ){.      pgno
178fe 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c   = get4byte(pCel
178ff 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  l);.#ifndef SQLI
17900 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
17901 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  UM.      if( pBt
17902 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
17903 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72          checkPtr
17904 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f  map(pCheck, pgno
17905 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20  , PTRMAP_BTREE, 
17906 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29  iPage, zContext)
17907 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
17908 0a 20 20 20 20 20 20 64 32 20 3d 20 63 68 65 63  .      d2 = chec
17909 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 63 6b  kTreePage(pCheck
1790a 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 74  , pgno, zContext
1790b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3e 30  );.      if( i>0
1790c 20 26 26 20 64 32 21 3d 64 65 70 74 68 20 29 7b   && d2!=depth ){
1790d 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70  .        checkAp
1790e 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
1790f 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 69 6c 64  zContext, "Child
17910 20 70 61 67 65 20 64 65 70 74 68 20 64 69 66 66   page depth diff
17911 65 72 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ers");.      }. 
17912 20 20 20 20 20 64 65 70 74 68 20 3d 20 64 32 3b       depth = d2;
17913 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
17914 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
17915 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34  .    pgno = get4
17916 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
17917 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
17918 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 73 71 6c  set+8]);.    sql
17919 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1791a 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20  zeof(zContext), 
1791b 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20  zContext, .     
1791c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1791d 22 4f 6e 20 70 61 67 65 20 25 64 20 61 74 20 72  "On page %d at r
1791e 69 67 68 74 20 63 68 69 6c 64 3a 20 22 2c 20 69  ight child: ", i
1791f 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53  Page);.#ifndef S
17920 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
17921 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42  ACUUM.    if( pB
17922 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
17923 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d  .      checkPtrm
17924 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c  ap(pCheck, pgno,
17925 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69   PTRMAP_BTREE, i
17926 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  Page, 0);.    }.
17927 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b  #endif.    check
17928 54 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c  TreePage(pCheck,
17929 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 74 29   pgno, zContext)
1792a 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 43 68 65  ;.  }. .  /* Che
1792b 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 20  ck for complete 
1792c 63 6f 76 65 72 61 67 65 20 6f 66 20 74 68 65 20  coverage of the 
1792d 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64 61 74 61  page.  */.  data
1792e 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
1792f 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
17930 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 68 69 74  hdrOffset;.  hit
17931 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61   = sqlite3PageMa
17932 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53  lloc( pBt->pageS
17933 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 68 69 74  ize );.  if( hit
17934 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 68 65 63  ==0 ){.    pChec
17935 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  k->mallocFailed 
17936 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 1;.  }else{.  
17937 20 20 75 31 36 20 63 6f 6e 74 65 6e 74 4f 66 66    u16 contentOff
17938 73 65 74 20 3d 20 67 65 74 32 62 79 74 65 28 26  set = get2byte(&
17939 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20  data[hdr+5]);.  
1793a 20 20 69 66 20 28 63 6f 6e 74 65 6e 74 4f 66 66    if (contentOff
1793b 73 65 74 20 3e 20 75 73 61 62 6c 65 53 69 7a 65  set > usableSize
1793c 29 20 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41  ) {.      checkA
1793d 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
1793e 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
1793f 20 20 20 20 20 20 20 20 20 20 22 43 6f 72 72 75            "Corru
17940 70 74 69 6f 6e 20 64 65 74 65 63 74 65 64 20 69  ption detected i
17941 6e 20 68 65 61 64 65 72 20 6f 6e 20 70 61 67 65  n header on page
17942 20 25 64 22 2c 69 50 61 67 65 2c 30 29 3b 0a 20   %d",iPage,0);. 
17943 20 20 20 20 20 67 6f 74 6f 20 63 68 65 63 6b 5f       goto check_
17944 70 61 67 65 5f 61 62 6f 72 74 3b 0a 20 20 20 20  page_abort;.    
17945 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74  }.    memset(hit
17946 2b 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20  +contentOffset, 
17947 30 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f  0, usableSize-co
17948 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20  ntentOffset);.  
17949 20 20 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c    memset(hit, 1,
1794a 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b   contentOffset);
1794b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74  .    nCell = get
1794c 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
1794d 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61  3]);.    cellSta
1794e 72 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20  rt = hdr + 12 - 
1794f 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20  4*pPage->leaf;. 
17950 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
17951 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ell; i++){.     
17952 20 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79   int pc = get2by
17953 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61  te(&data[cellSta
17954 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20  rt+i*2]);.      
17955 75 31 36 20 73 69 7a 65 20 3d 20 31 30 32 34 3b  u16 size = 1024;
17956 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  .      int j;.  
17957 20 20 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62      if( pc<=usab
17958 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  leSize ){.      
17959 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a    size = cellSiz
1795a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 74  ePtr(pPage, &dat
1795b 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  a[pc]);.      }.
1795c 20 20 20 20 20 20 69 66 28 20 28 70 63 2b 73 69        if( (pc+si
1795d 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a  ze-1)>=usableSiz
1795e 65 20 7c 7c 20 70 63 3c 30 20 29 7b 0a 20 20 20  e || pc<0 ){.   
1795f 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
17960 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a  Msg(pCheck, 0, .
17961 20 20 20 20 20 20 20 20 20 20 20 20 22 43 6f 72              "Cor
17962 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 65 64  ruption detected
17963 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f 6e 20 70   in cell %d on p
17964 61 67 65 20 25 64 22 2c 69 2c 69 50 61 67 65 2c  age %d",i,iPage,
17965 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  0);.      }else{
17966 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 70  .        for(j=p
17967 63 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 70 63 3b  c+size-1; j>=pc;
17968 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a   j--) hit[j]++;.
17969 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1796a 20 20 66 6f 72 28 63 6e 74 3d 30 2c 20 69 3d 67    for(cnt=0, i=g
1796b 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
1796c 72 2b 31 5d 29 3b 20 69 3e 30 20 26 26 20 69 3c  r+1]); i>0 && i<
1796d 75 73 61 62 6c 65 53 69 7a 65 20 26 26 20 63 6e  usableSize && cn
1796e 74 3c 31 30 30 30 30 3b 20 0a 20 20 20 20 20 20  t<10000; .      
1796f 20 20 20 20 20 63 6e 74 2b 2b 29 7b 0a 20 20 20       cnt++){.   
17970 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 67 65     int size = ge
17971 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32  t2byte(&data[i+2
17972 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  ]);.      int j;
17973 0a 20 20 20 20 20 20 69 66 28 20 28 69 2b 73 69  .      if( (i+si
17974 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a  ze-1)>=usableSiz
17975 65 20 7c 7c 20 69 3c 30 20 29 7b 0a 20 20 20 20  e || i<0 ){.    
17976 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
17977 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 20 0a  sg(pCheck, 0,  .
17978 20 20 20 20 20 20 20 20 20 20 20 20 22 43 6f 72              "Cor
17979 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 65 64  ruption detected
1797a 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f 6e 20 70   in cell %d on p
1797b 61 67 65 20 25 64 22 2c 69 2c 69 50 61 67 65 2c  age %d",i,iPage,
1797c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  0);.      }else{
1797d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 69  .        for(j=i
1797e 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 69 3b 20 6a  +size-1; j>=i; j
1797f 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20  --) hit[j]++;.  
17980 20 20 20 20 7d 0a 20 20 20 20 20 20 69 20 3d 20      }.      i = 
17981 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69  get2byte(&data[i
17982 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  ]);.    }.    fo
17983 72 28 69 3d 63 6e 74 3d 30 3b 20 69 3c 75 73 61  r(i=cnt=0; i<usa
17984 62 6c 65 53 69 7a 65 3b 20 69 2b 2b 29 7b 0a 20  bleSize; i++){. 
17985 20 20 20 20 20 69 66 28 20 68 69 74 5b 69 5d 3d       if( hit[i]=
17986 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6e  =0 ){.        cn
17987 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  t++;.      }else
17988 20 69 66 28 20 68 69 74 5b 69 5d 3e 31 20 29 7b   if( hit[i]>1 ){
17989 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70  .        checkAp
1798a 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
1798b 30 2c 0a 20 20 20 20 20 20 20 20 20 20 22 4d 75  0,.          "Mu
1798c 6c 74 69 70 6c 65 20 75 73 65 73 20 66 6f 72 20  ltiple uses for 
1798d 62 79 74 65 20 25 64 20 6f 66 20 70 61 67 65 20  byte %d of page 
1798e 25 64 22 2c 20 69 2c 20 69 50 61 67 65 29 3b 0a  %d", i, iPage);.
1798f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
17990 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
17991 20 69 66 28 20 63 6e 74 21 3d 64 61 74 61 5b 68   if( cnt!=data[h
17992 64 72 2b 37 5d 20 29 7b 0a 20 20 20 20 20 20 63  dr+7] ){.      c
17993 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
17994 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20  heck, 0, .      
17995 20 20 20 20 22 46 72 61 67 6d 65 6e 74 65 64 20      "Fragmented 
17996 73 70 61 63 65 20 69 73 20 25 64 20 62 79 74 65  space is %d byte
17997 20 72 65 70 6f 72 74 65 64 20 61 73 20 25 64 20   reported as %d 
17998 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a 20 20 20  on page %d",.   
17999 20 20 20 20 20 20 20 63 6e 74 2c 20 64 61 74 61         cnt, data
1799a 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67 65 29 3b  [hdr+7], iPage);
1799b 0a 20 20 20 20 7d 0a 20 20 7d 0a 63 68 65 63 6b  .    }.  }.check
1799c 5f 70 61 67 65 5f 61 62 6f 72 74 3a 0a 20 20 69  _page_abort:.  i
1799d 66 20 28 68 69 74 29 20 73 71 6c 69 74 65 33 50  f (hit) sqlite3P
1799e 61 67 65 46 72 65 65 28 68 69 74 29 3b 0a 0a 20  ageFree(hit);.. 
1799f 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
179a0 67 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 65  ge);.  return de
179a1 70 74 68 2b 31 3b 0a 7d 0a 23 65 6e 64 69 66 20  pth+1;.}.#endif 
179a2 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  /* SQLITE_OMIT_I
179a3 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a  NTEGRITY_CHECK *
179a4 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
179a5 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
179a6 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69  _CHECK./*.** Thi
179a7 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61  s routine does a
179a8 20 63 6f 6d 70 6c 65 74 65 20 63 68 65 63 6b 20   complete check 
179a9 6f 66 20 74 68 65 20 67 69 76 65 6e 20 42 54 72  of the given BTr
179aa 65 65 20 66 69 6c 65 2e 20 20 61 52 6f 6f 74 5b  ee file.  aRoot[
179ab 5d 20 69 73 0a 2a 2a 20 61 6e 20 61 72 72 61 79  ] is.** an array
179ac 20 6f 66 20 70 61 67 65 73 20 6e 75 6d 62 65 72   of pages number
179ad 73 20 77 65 72 65 20 65 61 63 68 20 70 61 67 65  s were each page
179ae 20 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 72   number is the r
179af 6f 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20 61  oot page of.** a
179b0 20 74 61 62 6c 65 2e 20 20 6e 52 6f 6f 74 20 69   table.  nRoot i
179b1 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
179b2 65 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74  entries in aRoot
179b3 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  ..**.** Write th
179b4 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
179b5 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e 45 72 72  r seen in *pnErr
179b6 2e 20 20 45 78 63 65 70 74 20 66 6f 72 20 73 6f  .  Except for so
179b7 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c  me memory.** all
179b8 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 2c 20  ocation errors, 
179b9 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
179ba 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  e held in memory
179bb 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
179bc 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 72 65 74 75  * malloc is retu
179bd 72 6e 65 64 20 69 66 20 2a 70 6e 45 72 72 20 69  rned if *pnErr i
179be 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20  s non-zero.  If 
179bf 2a 70 6e 45 72 72 3d 3d 30 20 74 68 65 6e 20 4e  *pnErr==0 then N
179c0 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  ULL is.** return
179c1 65 64 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79  ed.  If a memory
179c2 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
179c3 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69  r occurs, NULL i
179c4 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
179c5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
179c6 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
179c7 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 0a  IntegrityCheck(.
179c8 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20    Btree *p,     
179c9 2f 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20  /* The btree to 
179ca 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20  be checked */.  
179cb 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20 2f 2a  int *aRoot,   /*
179cc 20 41 6e 20 61 72 72 61 79 20 6f 66 20 72 6f 6f   An array of roo
179cd 74 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20  t pages numbers 
179ce 66 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74  for individual t
179cf 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  rees */.  int nR
179d0 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65  oot,    /* Numbe
179d1 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
179d2 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74  aRoot[] */.  int
179d3 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20 53 74   mxErr,    /* St
179d4 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 65 72 72  op reporting err
179d5 6f 72 73 20 61 66 74 65 72 20 74 68 69 73 20 6d  ors after this m
179d6 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  any */.  int *pn
179d7 45 72 72 20 20 20 20 2f 2a 20 57 72 69 74 65 20  Err    /* Write 
179d8 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
179d9 20 73 65 65 6e 20 74 6f 20 74 68 69 73 20 76 61   seen to this va
179da 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 50  riable */.){.  P
179db 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65  gno i;.  int nRe
179dc 66 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b  f;.  IntegrityCk
179dd 20 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68 61   sCheck;.  BtSha
179de 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
179df 74 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31  t;.  char zErr[1
179e0 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  00];..  sqlite3B
179e1 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
179e2 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  pBt->db = p->db;
179e3 0a 20 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65  .  nRef = sqlite
179e4 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
179e5 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69  Bt->pPager);.  i
179e6 66 28 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68  f( lockBtreeWith
179e7 52 65 74 72 79 28 70 29 21 3d 53 51 4c 49 54 45  Retry(p)!=SQLITE
179e8 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 6e 45 72  _OK ){.    *pnEr
179e9 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74  r = 1;.    sqlit
179ea 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
179eb 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
179ec 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 22  te3DbStrDup(0, "
179ed 63 61 6e 6e 6f 74 20 61 63 71 75 69 72 65 20 61  cannot acquire a
179ee 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68   read lock on th
179ef 65 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20  e database");.  
179f0 7d 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d  }.  sCheck.pBt =
179f1 20 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70   pBt;.  sCheck.p
179f2 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61  Pager = pBt->pPa
179f3 67 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50  ger;.  sCheck.nP
179f4 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63  age = pagerPagec
179f5 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29  ount(sCheck.pBt)
179f6 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72  ;.  sCheck.mxErr
179f7 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65   = mxErr;.  sChe
179f8 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73  ck.nErr = 0;.  s
179f9 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c  Check.mallocFail
179fa 65 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72  ed = 0;.  *pnErr
179fb 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65   = 0;.  if( sChe
179fc 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20  ck.nPage==0 ){. 
179fd 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66     unlockBtreeIf
179fe 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20  Unused(pBt);.   
179ff 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
17a00 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72  ve(p);.    retur
17a01 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63  n 0;.  }.  sChec
17a02 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74 65  k.anRef = sqlite
17a03 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63 6b  3Malloc( (sCheck
17a04 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f 66  .nPage+1)*sizeof
17a05 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 5d  (sCheck.anRef[0]
17a06 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68 65  ) );.  if( !sChe
17a07 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20 20  ck.anRef ){.    
17a08 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
17a09 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 2a 70  sed(pBt);.    *p
17a0a 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73 71  nErr = 1;.    sq
17a0b 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
17a0c 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  p);.    return 0
17a0d 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
17a0e 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65   i<=sCheck.nPage
17a0f 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63 6b 2e 61  ; i++){ sCheck.a
17a10 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20 7d 0a 20  nRef[i] = 0; }. 
17a11 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54   i = PENDING_BYT
17a12 45 5f 50 41 47 45 28 70 42 74 29 3b 0a 20 20 69  E_PAGE(pBt);.  i
17a13 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61  f( i<=sCheck.nPa
17a14 67 65 20 29 7b 0a 20 20 20 20 73 43 68 65 63 6b  ge ){.    sCheck
17a15 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31 3b 0a 20  .anRef[i] = 1;. 
17a16 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41   }.  sqlite3StrA
17a17 63 63 75 6d 49 6e 69 74 28 26 73 43 68 65 63 6b  ccumInit(&sCheck
17a18 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72 2c 20 73  .errMsg, zErr, s
17a19 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 32 30 30  izeof(zErr), 200
17a1a 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  00);..  /* Check
17a1b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f   the integrity o
17a1c 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 0a 20  f the freelist. 
17a1d 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 73 74 28   */.  checkList(
17a1e 26 73 43 68 65 63 6b 2c 20 31 2c 20 67 65 74 34  &sCheck, 1, get4
17a1f 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65  byte(&pBt->pPage
17a20 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 2c 0a 20  1->aData[32]),. 
17a21 20 20 20 20 20 20 20 20 20 20 20 67 65 74 34 62             get4b
17a22 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31  yte(&pBt->pPage1
17a23 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c 20 22 4d  ->aData[36]), "M
17a24 61 69 6e 20 66 72 65 65 6c 69 73 74 3a 20 22 29  ain freelist: ")
17a25 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 61 6c  ;..  /* Check al
17a26 6c 20 74 68 65 20 74 61 62 6c 65 73 2e 0a 20 20  l the tables..  
17a27 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 69  */.  for(i=0; (i
17a28 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26 20 73 43  nt)i<nRoot && sC
17a29 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29  heck.mxErr; i++)
17a2a 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f 74 5b  {.    if( aRoot[
17a2b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  i]==0 ) continue
17a2c 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
17a2d 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
17a2e 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75  .    if( pBt->au
17a2f 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52 6f 6f  toVacuum && aRoo
17a30 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  t[i]>1 ){.      
17a31 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73 43 68  checkPtrmap(&sCh
17a32 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 50  eck, aRoot[i], P
17a33 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20  TRMAP_ROOTPAGE, 
17a34 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  0, 0);.    }.#en
17a35 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65  dif.    checkTre
17a36 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c 20 61  ePage(&sCheck, a
17a37 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 74 20 6f  Root[i], "List o
17a38 66 20 74 72 65 65 20 72 6f 6f 74 73 3a 20 22 29  f tree roots: ")
17a39 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
17a3a 20 73 75 72 65 20 65 76 65 72 79 20 70 61 67 65   sure every page
17a3b 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20   in the file is 
17a3c 72 65 66 65 72 65 6e 63 65 64 0a 20 20 2a 2f 0a  referenced.  */.
17a3d 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 43    for(i=1; i<=sC
17a3e 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 20 73 43  heck.nPage && sC
17a3f 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29  heck.mxErr; i++)
17a40 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
17a41 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
17a42 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61      if( sCheck.a
17a43 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20  nRef[i]==0 ){.  
17a44 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
17a45 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22  sg(&sCheck, 0, "
17a46 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72  Page %d is never
17a47 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20   used", i);.    
17a48 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 49  }.#else.    /* I
17a49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  f the database s
17a4a 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63  upports auto-vac
17a4b 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72 65 20 6e  uum, make sure n
17a4c 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 6e  o tables contain
17a4d 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63  .    ** referenc
17a4e 65 73 20 74 6f 20 70 6f 69 6e 74 65 72 2d 6d 61  es to pointer-ma
17a4f 70 20 70 61 67 65 73 2e 0a 20 20 20 20 2a 2f 0a  p pages..    */.
17a50 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61      if( sCheck.a
17a51 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26 20 0a 20  nRef[i]==0 && . 
17a52 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41        (PTRMAP_PA
17a53 47 45 4e 4f 28 70 42 74 2c 20 69 29 21 3d 69 20  GENO(pBt, i)!=i 
17a54 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63  || !pBt->autoVac
17a55 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68  uum) ){.      ch
17a56 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43  eckAppendMsg(&sC
17a57 68 65 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 25  heck, 0, "Page %
17a58 64 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 22  d is never used"
17a59 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , i);.    }.    
17a5a 69 66 28 20 73 43 68 65 63 6b 2e 61 6e 52 65 66  if( sCheck.anRef
17a5b 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20 20 20 20  [i]!=0 && .     
17a5c 20 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f    (PTRMAP_PAGENO
17a5d 28 70 42 74 2c 20 69 29 3d 3d 69 20 26 26 20 70  (pBt, i)==i && p
17a5e 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 20  Bt->autoVacuum) 
17a5f 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
17a60 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c  pendMsg(&sCheck,
17a61 20 30 2c 20 22 50 6f 69 6e 74 65 72 20 6d 61 70   0, "Pointer map
17a62 20 70 61 67 65 20 25 64 20 69 73 20 72 65 66 65   page %d is refe
17a63 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a 20 20 20  renced", i);.   
17a64 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20   }.#endif.  }.. 
17a65 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
17a66 69 73 20 61 6e 61 6c 79 73 69 73 20 64 69 64 20  is analysis did 
17a67 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79 20 75 6e  not leave any un
17a68 72 65 66 28 29 20 70 61 67 65 73 2e 0a 20 20 2a  ref() pages..  *
17a69 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74  * This is an int
17a6a 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74 65 6e 63  ernal consistenc
17a6b 79 20 63 68 65 63 6b 3b 20 61 6e 20 69 6e 74 65  y check; an inte
17a6c 67 72 69 74 79 20 63 68 65 63 6b 0a 20 20 2a 2a  grity check.  **
17a6d 20 6f 66 20 74 68 65 20 69 6e 74 65 67 72 69 74   of the integrit
17a6e 79 20 63 68 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20  y check..  */.  
17a6f 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
17a70 73 65 64 28 70 42 74 29 3b 0a 20 20 69 66 28 20  sed(pBt);.  if( 
17a71 4e 45 56 45 52 28 6e 52 65 66 20 21 3d 20 73 71  NEVER(nRef != sq
17a72 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
17a73 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29  nt(pBt->pPager))
17a74 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70   ){.    checkApp
17a75 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20  endMsg(&sCheck, 
17a76 30 2c 20 0a 20 20 20 20 20 20 22 4f 75 74 73 74  0, .      "Outst
17a77 61 6e 64 69 6e 67 20 70 61 67 65 20 63 6f 75 6e  anding page coun
17a78 74 20 67 6f 65 73 20 66 72 6f 6d 20 25 64 20 74  t goes from %d t
17a79 6f 20 25 64 20 64 75 72 69 6e 67 20 74 68 69 73  o %d during this
17a7a 20 61 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20   analysis",.    
17a7b 20 20 6e 52 65 66 2c 20 73 71 6c 69 74 65 33 50    nRef, sqlite3P
17a7c 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74  agerRefcount(pBt
17a7d 2d 3e 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b  ->pPager).    );
17a7e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e  .  }..  /* Clean
17a7f 20 20 75 70 20 61 6e 64 20 72 65 70 6f 72 74 20    up and report 
17a80 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 73  errors..  */.  s
17a81 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
17a82 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  (p);.  sqlite3_f
17a83 72 65 65 28 73 43 68 65 63 6b 2e 61 6e 52 65 66  ree(sCheck.anRef
17a84 29 3b 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e  );.  if( sCheck.
17a85 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
17a86 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
17a87 63 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b  cumReset(&sCheck
17a88 2e 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 2a 70  .errMsg);.    *p
17a89 6e 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45  nErr = sCheck.nE
17a8a 72 72 2b 31 3b 0a 20 20 20 20 72 65 74 75 72 6e  rr+1;.    return
17a8b 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 6e 45 72 72   0;.  }.  *pnErr
17a8c 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 3b 0a   = sCheck.nErr;.
17a8d 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 45 72    if( sCheck.nEr
17a8e 72 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 53 74  r==0 ) sqlite3St
17a8f 72 41 63 63 75 6d 52 65 73 65 74 28 26 73 43 68  rAccumReset(&sCh
17a90 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 72  eck.errMsg);.  r
17a91 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72  eturn sqlite3Str
17a92 41 63 63 75 6d 46 69 6e 69 73 68 28 26 73 43 68  AccumFinish(&sCh
17a93 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 7d 0a 23  eck.errMsg);.}.#
17a94 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
17a95 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
17a96 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  HECK */../*.** R
17a97 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70  eturn the full p
17a98 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 75  athname of the u
17a99 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61  nderlying databa
17a9a 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  se file..**.** T
17a9b 68 65 20 70 61 67 65 72 20 66 69 6c 65 6e 61 6d  he pager filenam
17a9c 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61  e is invariant a
17a9d 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61  s long as the pa
17a9e 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73  ger is.** open s
17a9f 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20  o it is safe to 
17aa0 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74  access without t
17aa1 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65  he BtShared mute
17aa2 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  x..*/.SQLITE_PRI
17aa3 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
17aa4 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  *sqlite3BtreeGet
17aa5 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a  Filename(Btree *
17aa6 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
17aa7 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20  >pBt->pPager!=0 
17aa8 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
17aa9 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65  te3PagerFilename
17aaa 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29  (p->pBt->pPager)
17aab 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
17aac 6e 20 74 68 65 20 70 61 74 68 6e 61 6d 65 20 6f  n the pathname o
17aad 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
17aae 6c 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61  le for this data
17aaf 62 61 73 65 2e 20 54 68 65 20 72 65 74 75 72 6e  base. The return
17ab0 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 69  .** value of thi
17ab1 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65  s routine is the
17ab2 20 73 61 6d 65 20 72 65 67 61 72 64 6c 65 73 73   same regardless
17ab3 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 65 20   of whether the 
17ab4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20  journal file.** 
17ab5 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64  has been created
17ab6 20 6f 72 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54   or not..**.** T
17ab7 68 65 20 70 61 67 65 72 20 6a 6f 75 72 6e 61 6c  he pager journal
17ab8 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76   filename is inv
17ab9 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61  ariant as long a
17aba 73 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a  s the pager is.*
17abb 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20  * open so it is 
17abc 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77  safe to access w
17abd 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61  ithout the BtSha
17abe 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  red mutex..*/.SQ
17abf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
17ac0 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
17ac1 42 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e  BtreeGetJournaln
17ac2 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ame(Btree *p){. 
17ac3 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d   assert( p->pBt-
17ac4 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20  >pPager!=0 );.  
17ac5 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61  return sqlite3Pa
17ac6 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70  gerJournalname(p
17ac7 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  ->pBt->pPager);.
17ac8 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
17ac9 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 74 72  non-zero if a tr
17aca 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
17acb 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ive..*/.SQLITE_P
17acc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
17acd 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
17ace 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73  (Btree *p){.  as
17acf 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 73 71  sert( p==0 || sq
17ad0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
17ad1 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
17ad2 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 26  ;.  return (p &&
17ad3 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52   (p->inTrans==TR
17ad4 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a 7d 0a 0a  ANS_WRITE));.}..
17ad5 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e  /*.** Return non
17ad6 2d 7a 65 72 6f 20 69 66 20 61 20 72 65 61 64 20  -zero if a read 
17ad7 28 6f 72 20 77 72 69 74 65 29 20 74 72 61 6e 73  (or write) trans
17ad8 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
17ad9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
17ada 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
17adb 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e  treeIsInReadTran
17adc 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61  s(Btree *p){.  a
17add 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73  ssert( p );.  as
17ade 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
17adf 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
17ae0 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75  mutex) );.  retu
17ae1 72 6e 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54  rn p->inTrans!=T
17ae2 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 7d 0a 0a 53 51  RANS_NONE;.}..SQ
17ae3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
17ae4 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
17ae5 6e 42 61 63 6b 75 70 28 42 74 72 65 65 20 2a 70  nBackup(Btree *p
17ae6 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29  ){.  assert( p )
17ae7 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
17ae8 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
17ae9 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
17aea 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 42 61 63    return p->nBac
17aeb 6b 75 70 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  kup!=0;.}../*.**
17aec 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
17aed 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
17aee 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 6d 65   to a blob of me
17aef 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
17af0 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c 65  with.** a single
17af1 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e 20 54   shared-btree. T
17af2 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 75 73 65  he memory is use
17af3 64 20 62 79 20 63 6c 69 65 6e 74 20 63 6f 64 65  d by client code
17af4 20 66 6f 72 20 69 74 73 20 6f 77 6e 0a 2a 2a 20   for its own.** 
17af5 70 75 72 70 6f 73 65 73 20 28 66 6f 72 20 65 78  purposes (for ex
17af6 61 6d 70 6c 65 2c 20 74 6f 20 73 74 6f 72 65 20  ample, to store 
17af7 61 20 68 69 67 68 2d 6c 65 76 65 6c 20 73 63 68  a high-level sch
17af8 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ema associated w
17af9 69 74 68 20 0a 2a 2a 20 74 68 65 20 73 68 61 72  ith .** the shar
17afa 65 64 2d 62 74 72 65 65 29 2e 20 54 68 65 20 62  ed-btree). The b
17afb 74 72 65 65 20 6c 61 79 65 72 20 6d 61 6e 61 67  tree layer manag
17afc 65 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  es reference cou
17afd 6e 74 69 6e 67 20 69 73 73 75 65 73 2e 0a 2a 2a  nting issues..**
17afe 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69  .** The first ti
17aff 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  me this is calle
17b00 64 20 6f 6e 20 61 20 73 68 61 72 65 64 2d 62 74  d on a shared-bt
17b01 72 65 65 2c 20 6e 42 79 74 65 73 20 62 79 74 65  ree, nBytes byte
17b02 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  s of memory.** a
17b03 72 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 7a 65  re allocated, ze
17b04 72 6f 65 64 2c 20 61 6e 64 20 72 65 74 75 72 6e  roed, and return
17b05 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
17b06 2e 20 46 6f 72 20 65 61 63 68 20 73 75 62 73 65  . For each subse
17b07 71 75 65 6e 74 20 0a 2a 2a 20 63 61 6c 6c 20 74  quent .** call t
17b08 68 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65  he nBytes parame
17b09 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 61  ter is ignored a
17b0a 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  nd a pointer to 
17b0b 74 68 65 20 73 61 6d 65 20 62 6c 6f 62 0a 2a 2a  the same blob.**
17b0c 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 74 75 72   of memory retur
17b0d 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ned. .**.** If t
17b0e 68 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65  he nBytes parame
17b0f 74 65 72 20 69 73 20 30 20 61 6e 64 20 74 68 65  ter is 0 and the
17b10 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20   blob of memory 
17b11 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
17b12 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61  .** allocated, a
17b13 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73   null pointer is
17b14 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68   returned. If th
17b15 65 20 62 6c 6f 62 20 68 61 73 20 61 6c 72 65 61  e blob has alrea
17b16 64 79 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63  dy been.** alloc
17b17 61 74 65 64 2c 20 69 74 20 69 73 20 72 65 74 75  ated, it is retu
17b18 72 6e 65 64 20 61 73 20 6e 6f 72 6d 61 6c 2e 0a  rned as normal..
17b19 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62 65 66 6f 72  **.** Just befor
17b1a 65 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  e the shared-btr
17b1b 65 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 68  ee is closed, th
17b1c 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65  e function passe
17b1d 64 20 61 73 20 74 68 65 20 0a 2a 2a 20 78 46 72  d as the .** xFr
17b1e 65 65 20 61 72 67 75 6d 65 6e 74 20 77 68 65 6e  ee argument when
17b1f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
17b20 63 61 74 69 6f 6e 20 77 61 73 20 6d 61 64 65 20  cation was made 
17b21 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68  is invoked on th
17b22 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 6c  e .** blob of al
17b23 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20  located memory. 
17b24 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68  This function sh
17b25 6f 75 6c 64 20 6e 6f 74 20 63 61 6c 6c 20 73 71  ould not call sq
17b26 6c 69 74 65 33 5f 66 72 65 65 28 29 0a 2a 2a 20  lite3_free().** 
17b27 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74  on the memory, t
17b28 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 64  he btree layer d
17b29 6f 65 73 20 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c  oes that..*/.SQL
17b2a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
17b2b 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 53 63   *sqlite3BtreeSc
17b2c 68 65 6d 61 28 42 74 72 65 65 20 2a 70 2c 20 69  hema(Btree *p, i
17b2d 6e 74 20 6e 42 79 74 65 73 2c 20 76 6f 69 64 28  nt nBytes, void(
17b2e 2a 78 46 72 65 65 29 28 76 6f 69 64 20 2a 29 29  *xFree)(void *))
17b2f 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
17b30 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71  t = p->pBt;.  sq
17b31 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
17b32 70 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e  p);.  if( !pBt->
17b33 70 53 63 68 65 6d 61 20 26 26 20 6e 42 79 74 65  pSchema && nByte
17b34 73 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 53  s ){.    pBt->pS
17b35 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 4d  chema = sqlite3M
17b36 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 79 74 65 73  allocZero(nBytes
17b37 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78 46 72 65  );.    pBt->xFre
17b38 65 53 63 68 65 6d 61 20 3d 20 78 46 72 65 65 3b  eSchema = xFree;
17b39 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
17b3a 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
17b3b 65 74 75 72 6e 20 70 42 74 2d 3e 70 53 63 68 65  eturn pBt->pSche
17b3c 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ma;.}../*.** Ret
17b3d 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
17b3e 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 69 66  D_SHAREDCACHE if
17b3f 20 61 6e 6f 74 68 65 72 20 75 73 65 72 20 6f 66   another user of
17b40 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64   the same shared
17b41 20 0a 2a 2a 20 62 74 72 65 65 20 61 73 20 74 68   .** btree as th
17b42 65 20 61 72 67 75 6d 65 6e 74 20 68 61 6e 64 6c  e argument handl
17b43 65 20 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75  e holds an exclu
17b44 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  sive lock on the
17b45 20 0a 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74   .** sqlite_mast
17b46 65 72 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77  er table. Otherw
17b47 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  ise SQLITE_OK..*
17b48 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17b49 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
17b4a 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74  eSchemaLocked(Bt
17b4b 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ree *p){.  int r
17b4c 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  c;.  assert( sql
17b4d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17b4e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
17b4f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
17b50 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
17b51 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65  querySharedCache
17b52 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53  TableLock(p, MAS
17b53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c  TER_ROOT, READ_L
17b54 4f 43 4b 29 3b 0a 20 20 61 73 73 65 72 74 28 20  OCK);.  assert( 
17b55 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
17b56 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b   rc==SQLITE_LOCK
17b57 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 29  ED_SHAREDCACHE )
17b58 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
17b59 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
17b5a 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64  rn rc;.}...#ifnd
17b5b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
17b5c 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a  HARED_CACHE./*.*
17b5d 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20  * Obtain a lock 
17b5e 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 68 6f  on the table who
17b5f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20  se root page is 
17b60 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a 20 6c 6f  iTab.  The.** lo
17b61 63 6b 20 69 73 20 61 20 77 72 69 74 65 20 6c 6f  ck is a write lo
17b62 63 6b 20 69 66 20 69 73 57 72 69 74 65 6c 6f 63  ck if isWriteloc
17b63 6b 20 69 73 20 74 72 75 65 20 6f 72 20 61 20 72  k is true or a r
17b64 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69 66 20 69  ead lock.** if i
17b65 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53  t is false..*/.S
17b66 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
17b67 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f  t sqlite3BtreeLo
17b68 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  ckTable(Btree *p
17b69 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 38 20 69  , int iTab, u8 i
17b6a 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a 20 20 69  sWriteLock){.  i
17b6b 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
17b6c 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72  K;.  if( p->shar
17b6d 61 62 6c 65 20 29 7b 0a 20 20 20 20 75 38 20 6c  able ){.    u8 l
17b6e 6f 63 6b 54 79 70 65 20 3d 20 52 45 41 44 5f 4c  ockType = READ_L
17b6f 4f 43 4b 20 2b 20 69 73 57 72 69 74 65 4c 6f 63  OCK + isWriteLoc
17b70 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 52  k;.    assert( R
17b71 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54  EAD_LOCK+1==WRIT
17b72 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73  E_LOCK );.    as
17b73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63  sert( isWriteLoc
17b74 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c  k==0 || isWriteL
17b75 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71  ock==1 );.    sq
17b76 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
17b77 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 75 65  p);.    rc = que
17b78 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
17b79 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20  leLock(p, iTab, 
17b7a 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 69  lockType);.    i
17b7b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
17b7c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
17b7d 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62  etSharedCacheTab
17b7e 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20  leLock(p, iTab, 
17b7f 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d  lockType);.    }
17b80 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
17b81 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20  eLeave(p);.  }. 
17b82 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
17b83 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
17b84 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
17b85 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  OB./*.** Argumen
17b86 74 20 70 43 73 72 20 6d 75 73 74 20 62 65 20 61  t pCsr must be a
17b87 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66   cursor opened f
17b88 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e  or writing on an
17b89 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c   .** INTKEY tabl
17b8a 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  e currently poin
17b8b 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20  ting at a valid 
17b8c 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a  table entry. .**
17b8d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
17b8e 6f 64 69 66 69 65 73 20 74 68 65 20 64 61 74 61  odifies the data
17b8f 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20   stored as part 
17b90 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a  of that entry..*
17b91 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20  * Only the data 
17b92 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79  content may only
17b93 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74   be modified, it
17b94 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
17b95 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68  .** to change th
17b96 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
17b97 64 61 74 61 20 73 74 6f 72 65 64 2e 0a 2a 2f 0a  data stored..*/.
17b98 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
17b99 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  nt sqlite3BtreeP
17b9a 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 20  utData(BtCursor 
17b9b 2a 70 43 73 72 2c 20 75 33 32 20 6f 66 66 73 65  *pCsr, u32 offse
17b9c 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64  t, u32 amt, void
17b9d 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a   *z){.  int rc;.
17b9e 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
17b9f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 73 72  rHoldsMutex(pCsr
17ba0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
17ba1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
17ba2 64 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2d 3e  d(pCsr->pBtree->
17ba3 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
17ba4 61 73 73 65 72 74 28 70 43 73 72 2d 3e 69 73 49  assert(pCsr->isI
17ba5 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a  ncrblobHandle);.
17ba6 0a 20 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72  .  restoreCursor
17ba7 50 6f 73 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a  Position(pCsr);.
17ba8 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e    assert( pCsr->
17ba9 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 52  eState!=CURSOR_R
17baa 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20  EQUIRESEEK );.  
17bab 69 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65  if( pCsr->eState
17bac 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  !=CURSOR_VALID )
17bad 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
17bae 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 0a  ITE_ABORT;.  }..
17baf 20 20 2f 2a 20 43 68 65 63 6b 20 73 6f 6d 65 20    /* Check some 
17bb0 70 72 65 63 6f 6e 64 69 74 69 6f 6e 73 3a 20 0a  preconditions: .
17bb1 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65 20 63    **   (a) the c
17bb2 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20 66 6f  ursor is open fo
17bb3 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a 2a 20  r writing,.  ** 
17bb4 20 20 28 62 29 20 74 68 65 72 65 20 69 73 20 6e    (b) there is n
17bb5 6f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74  o read-lock on t
17bb6 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6d  he table being m
17bb7 6f 64 69 66 69 65 64 20 61 6e 64 0a 20 20 2a 2a  odified and.  **
17bb8 20 20 20 28 63 29 20 74 68 65 20 63 75 72 73 6f     (c) the curso
17bb9 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76 61  r points at a va
17bba 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69 6e  lid row of an in
17bbb 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  tKey table..  */
17bbc 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77 72  .  if( !pCsr->wr
17bbd 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75  Flag ){.    retu
17bbe 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e  rn SQLITE_READON
17bbf 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  LY;.  }.  assert
17bc0 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72 65  ( !pCsr->pBt->re
17bc1 61 64 4f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20  adOnly .        
17bc2 20 20 26 26 20 70 43 73 72 2d 3e 70 42 74 2d 3e    && pCsr->pBt->
17bc3 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
17bc4 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20  RANS_WRITE );.  
17bc5 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 65 61  rc = checkForRea
17bc6 64 43 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d  dConflicts(pCsr-
17bc7 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70  >pBtree, pCsr->p
17bc8 67 6e 6f 52 6f 6f 74 2c 20 70 43 73 72 2c 20 30  gnoRoot, pCsr, 0
17bc9 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
17bca 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a  ITE_OK ){.    /*
17bcb 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20   The table pCur 
17bcc 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20  points to has a 
17bcd 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  read lock */.   
17bce 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
17bcf 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45  ITE_LOCKED_SHARE
17bd0 44 43 41 43 48 45 20 29 3b 0a 20 20 20 20 72 65  DCACHE );.    re
17bd1 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69  turn rc;.  }.  i
17bd2 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 3d  f( pCsr->eState=
17bd3 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
17bd4 7c 7c 20 21 70 43 73 72 2d 3e 61 70 50 61 67 65  || !pCsr->apPage
17bd5 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69  [pCsr->iPage]->i
17bd6 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74  ntKey ){.    ret
17bd7 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
17bd8 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
17bd9 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43  accessPayload(pC
17bda 73 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c  sr, offset, amt,
17bdb 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
17bdc 2a 29 7a 2c 20 30 2c 20 31 29 3b 0a 7d 0a 0a 2f  *)z, 0, 1);.}../
17bdd 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67  * .** Set a flag
17bde 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20   on this cursor 
17bdf 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63  to cache the loc
17be0 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20  ations of pages 
17be1 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65  from the .** ove
17be2 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74  rflow list for t
17be3 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20  he current row. 
17be4 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
17be5 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a  cursors opened.*
17be6 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61  * for incrementa
17be7 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a  l blob IO only..
17be8 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
17be9 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20  ion sets a flag 
17bea 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c  only. The actual
17beb 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63   page location c
17bec 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20  ache.** (stored 
17bed 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65  in BtCursor.aOve
17bee 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f  rflow[]) is allo
17bef 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62  cated and used b
17bf0 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63  y function.** ac
17bf1 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74  cessPayload() (t
17bf2 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69  he worker functi
17bf3 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74  on for sqlite3Bt
17bf4 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a  reeData() and.**
17bf5 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
17bf6 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49  Data())..*/.SQLI
17bf7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
17bf8 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68  sqlite3BtreeCach
17bf9 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73  eOverflow(BtCurs
17bfa 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
17bfb 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
17bfc 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
17bfd 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17bfe 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
17bff 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
17c00 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
17c01 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c  (!pCur->isIncrbl
17c02 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73  obHandle);.  ass
17c03 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72  ert(!pCur->aOver
17c04 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69  flow);.  pCur->i
17c05 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
17c06 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  = 1;.}.#endif../
17c07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
17c08 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a  nd of btree.c **
17c09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
17c0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
17c0d 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70  egin file backup
17c0e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
17c0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
17c11 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72  *.** 2009 Januar
17c12 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 28.**.** The a
17c13 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
17c14 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
17c15 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
17c16 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
17c17 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
17c18 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
17c19 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
17c1a 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
17c1b 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
17c1c 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
17c1d 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
17c1e 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
17c1f 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
17c20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
17c21 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
17c22 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
17c23 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
17c24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17c28 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
17c29 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d   contains the im
17c2a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
17c2b 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
17c2c 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49  up_XXX() .** API
17c2d 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74   functions and t
17c2e 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75  he related featu
17c2f 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  res..**.** $Id: 
17c30 62 61 63 6b 75 70 2e 63 2c 76 20 31 2e 31 33 20  backup.c,v 1.13 
17c31 32 30 30 39 2f 30 33 2f 31 36 20 31 33 3a 31 39  2009/03/16 13:19
17c32 3a 33 36 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :36 danielk1977 
17c33 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 4d 61 63  Exp $.*/../* Mac
17c34 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d  ro to find the m
17c35 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75  inimum of two nu
17c36 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f  meric values..*/
17c37 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64  .#ifndef MIN.# d
17c38 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28  efine MIN(x,y) (
17c39 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29  (x)<(y)?(x):(y))
17c3a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
17c3b 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74  tructure allocat
17c3c 65 64 20 66 6f 72 20 65 61 63 68 20 62 61 63 6b  ed for each back
17c3d 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f  up operation..*/
17c3e 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
17c3f 62 61 63 6b 75 70 20 7b 0a 20 20 73 71 6c 69 74  backup {.  sqlit
17c40 65 33 2a 20 70 44 65 73 74 44 62 3b 20 20 20 20  e3* pDestDb;    
17c41 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69      /* Destinati
17c42 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  on database hand
17c43 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70  le */.  Btree *p
17c44 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Dest;           
17c45 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20   /* Destination 
17c46 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 20  b-tree file */. 
17c47 20 75 33 32 20 69 44 65 73 74 53 63 68 65 6d 61   u32 iDestSchema
17c48 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69  ;         /* Ori
17c49 67 69 6e 61 6c 20 73 63 68 65 6d 61 20 63 6f 6f  ginal schema coo
17c4a 6b 69 65 20 69 6e 20 64 65 73 74 69 6e 61 74 69  kie in destinati
17c4b 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 44 65 73  on */.  int bDes
17c4c 74 4c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20  tLocked;        
17c4d 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 61 20   /* True once a 
17c4e 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
17c4f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e 20 70 44 65  n is open on pDe
17c50 73 74 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 69 4e  st */..  Pgno iN
17c51 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
17c52 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
17c53 20 6f 66 20 74 68 65 20 6e 65 78 74 20 73 6f 75   of the next sou
17c54 72 63 65 20 70 61 67 65 20 74 6f 20 63 6f 70 79  rce page to copy
17c55 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70   */.  sqlite3* p
17c56 53 72 63 44 62 3b 20 20 20 20 20 20 20 20 20 2f  SrcDb;         /
17c57 2a 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73  * Source databas
17c58 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74  e handle */.  Bt
17c59 72 65 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20  ree *pSrc;      
17c5a 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
17c5b 20 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a   b-tree file */.
17c5c 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
17c5d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
17c5e 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 65 72  ackup process er
17c5f 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f  ror code */..  /
17c60 2a 20 54 68 65 73 65 20 74 77 6f 20 76 61 72 69  * These two vari
17c61 61 62 6c 65 73 20 61 72 65 20 73 65 74 20 62 79  ables are set by
17c62 20 65 76 65 72 79 20 63 61 6c 6c 20 74 6f 20 62   every call to b
17c63 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68  ackup_step(). Th
17c64 65 79 20 61 72 65 0a 20 20 2a 2a 20 72 65 61 64  ey are.  ** read
17c65 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 62 61 63   by calls to bac
17c66 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20  kup_remaining() 
17c67 61 6e 64 20 62 61 63 6b 75 70 5f 70 61 67 65 63  and backup_pagec
17c68 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f 0a 20 20 50  ount()..  */.  P
17c69 67 6e 6f 20 6e 52 65 6d 61 69 6e 69 6e 67 3b 20  gno nRemaining; 
17c6a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
17c6b 72 20 6f 66 20 70 61 67 65 73 20 6c 65 66 74 20  r of pages left 
17c6c 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 50 67 6e  to copy */.  Pgn
17c6d 6f 20 6e 50 61 67 65 63 6f 75 6e 74 3b 20 20 20  o nPagecount;   
17c6e 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
17c6f 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74  umber of pages t
17c70 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 73 71 6c  o copy */..  sql
17c71 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 4e 65  ite3_backup *pNe
17c72 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 62 61  xt;   /* Next ba
17c73 63 6b 75 70 20 61 73 73 6f 63 69 61 74 65 64 20  ckup associated 
17c74 77 69 74 68 20 73 6f 75 72 63 65 20 70 61 67 65  with source page
17c75 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  r */.};../*.** T
17c76 48 52 45 41 44 20 53 41 46 45 54 59 20 4e 4f 54  HREAD SAFETY NOT
17c77 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 6e 63 65  ES:.**.**   Once
17c78 20 69 74 20 68 61 73 20 62 65 65 6e 20 63 72 65   it has been cre
17c79 61 74 65 64 20 75 73 69 6e 67 20 62 61 63 6b 75  ated using backu
17c7a 70 5f 69 6e 69 74 28 29 2c 20 61 20 73 69 6e 67  p_init(), a sing
17c7b 6c 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  le sqlite3_backu
17c7c 70 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65  p.**   structure
17c7d 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64   may be accessed
17c7e 20 76 69 61 20 74 77 6f 20 67 72 6f 75 70 73 20   via two groups 
17c7f 6f 66 20 74 68 72 65 61 64 2d 73 61 66 65 20 65  of thread-safe e
17c80 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0a 2a 2a 0a  ntry points:.**.
17c81 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65  **     * Via the
17c82 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
17c83 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 69  XXX() API functi
17c84 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29  on backup_step()
17c85 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20 62   and .**       b
17c86 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20  ackup_finish(). 
17c87 42 6f 74 68 20 74 68 65 73 65 20 66 75 6e 63 74  Both these funct
17c88 69 6f 6e 73 20 6f 62 74 61 69 6e 20 74 68 65 20  ions obtain the 
17c89 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 0a  source database.
17c8a 2a 2a 20 20 20 20 20 20 20 68 61 6e 64 6c 65 20  **       handle 
17c8b 6d 75 74 65 78 20 61 6e 64 20 74 68 65 20 6d 75  mutex and the mu
17c8c 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  tex associated w
17c8d 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 42  ith the source B
17c8e 74 53 68 61 72 65 64 20 0a 2a 2a 20 20 20 20 20  tShared .**     
17c8f 20 20 73 74 72 75 63 74 75 72 65 2c 20 69 6e 20    structure, in 
17c90 74 68 61 74 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a  that order..**.*
17c91 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 20  *     * Via the 
17c92 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 61  BackupUpdate() a
17c93 6e 64 20 42 61 63 6b 75 70 52 65 73 74 61 72 74  nd BackupRestart
17c94 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 68  () functions, wh
17c95 69 63 68 20 61 72 65 0a 2a 2a 20 20 20 20 20 20  ich are.**      
17c96 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20   invoked by the 
17c97 70 61 67 65 72 20 6c 61 79 65 72 20 74 6f 20 72  pager layer to r
17c98 65 70 6f 72 74 20 76 61 72 69 6f 75 73 20 73 74  eport various st
17c99 61 74 65 20 63 68 61 6e 67 65 73 20 69 6e 0a 2a  ate changes in.*
17c9a 2a 20 20 20 20 20 20 20 74 68 65 20 70 61 67 65  *       the page
17c9b 20 63 61 63 68 65 20 61 73 73 6f 63 69 61 74 65   cache associate
17c9c 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 63  d with the sourc
17c9d 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  e database. The 
17c9e 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 20 61  mutex.**       a
17c9f 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
17ca0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
17ca1 73 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75  se BtShared stru
17ca2 63 74 75 72 65 20 77 69 6c 6c 20 61 6c 77 61 79  cture will alway
17ca3 73 20 0a 2a 2a 20 20 20 20 20 20 20 62 65 20 68  s .**       be h
17ca4 65 6c 64 20 77 68 65 6e 20 65 69 74 68 65 72 20  eld when either 
17ca5 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  of these functio
17ca6 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 0a  ns are invoked..
17ca7 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 6f 74 68 65  **.**   The othe
17ca8 72 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  r sqlite3_backup
17ca9 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74  _XXX() API funct
17caa 69 6f 6e 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d  ions, backup_rem
17cab 61 69 6e 69 6e 67 28 29 20 61 6e 64 0a 2a 2a 20  aining() and.** 
17cac 20 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75    backup_pagecou
17cad 6e 74 28 29 20 61 72 65 20 6e 6f 74 20 74 68 72  nt() are not thr
17cae 65 61 64 2d 73 61 66 65 20 66 75 6e 63 74 69 6f  ead-safe functio
17caf 6e 73 2e 20 49 66 20 74 68 65 79 20 61 72 65 20  ns. If they are 
17cb0 63 61 6c 6c 65 64 0a 2a 2a 20 20 20 77 68 69 6c  called.**   whil
17cb1 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72  e some other thr
17cb2 65 61 64 20 69 73 20 63 61 6c 6c 69 6e 67 20 62  ead is calling b
17cb3 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 72 20  ackup_step() or 
17cb4 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c  backup_finish(),
17cb5 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c 75 65 73  .**   the values
17cb6 20 72 65 74 75 72 6e 65 64 20 6d 61 79 20 62 65   returned may be
17cb7 20 69 6e 76 61 6c 69 64 2e 20 54 68 65 72 65 20   invalid. There 
17cb8 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20  is no way for a 
17cb9 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 42 61 63  call to.**   Bac
17cba 6b 75 70 55 70 64 61 74 65 28 29 20 6f 72 20 42  kupUpdate() or B
17cbb 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 74  ackupRestart() t
17cbc 6f 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68  o interfere with
17cbd 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e   backup_remainin
17cbe 67 28 29 0a 2a 2a 20 20 20 6f 72 20 62 61 63 6b  g().**   or back
17cbf 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a  up_pagecount()..
17cc0 2a 2a 0a 2a 2a 20 20 20 44 65 70 65 6e 64 69 6e  **.**   Dependin
17cc1 67 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20  g on the SQLite 
17cc2 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 74  configuration, t
17cc3 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
17cc4 6c 65 73 20 61 6e 64 2f 6f 72 0a 2a 2a 20 20 20  les and/or.**   
17cc5 74 68 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74  the Btree object
17cc6 73 20 6d 61 79 20 68 61 76 65 20 74 68 65 69 72  s may have their
17cc7 20 6f 77 6e 20 6d 75 74 65 78 65 73 20 74 68 61   own mutexes tha
17cc8 74 20 72 65 71 75 69 72 65 20 6c 6f 63 6b 69 6e  t require lockin
17cc9 67 2e 0a 2a 2a 20 20 20 4e 6f 6e 2d 73 68 61 72  g..**   Non-shar
17cca 61 62 6c 65 20 42 74 72 65 65 73 20 28 69 6e 2d  able Btrees (in-
17ccb 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73  memory databases
17ccc 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 2c 20 64   for example), d
17ccd 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20  o not have.**   
17cce 61 73 73 6f 63 69 61 74 65 64 20 6d 75 74 65 78  associated mutex
17ccf 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  es..*/../*.** Re
17cd0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 63  turn a pointer c
17cd1 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
17cd2 64 61 74 61 62 61 73 65 20 7a 44 62 20 28 69 2e  database zDb (i.
17cd3 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70  e. "main", "temp
17cd4 22 29 0a 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74  ").** in connect
17cd5 69 6f 6e 20 68 61 6e 64 6c 65 20 70 44 62 2e 20  ion handle pDb. 
17cd6 49 66 20 73 75 63 68 20 61 20 64 61 74 61 62 61  If such a databa
17cd7 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  se cannot be fou
17cd8 6e 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  nd, return.** a 
17cd9 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64  NULL pointer and
17cda 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20   write an error 
17cdb 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f  message to pErro
17cdc 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  rDb..**.** If th
17cdd 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 73  e "temp" databas
17cde 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  e is requested, 
17cdf 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62  it may need to b
17ce0 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73  e opened by this
17ce1 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49   .** function. I
17ce2 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
17ce3 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f  s while doing so
17ce4 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 77  , return 0 and w
17ce5 72 69 74 65 20 61 6e 20 0a 2a 2a 20 65 72 72 6f  rite an .** erro
17ce6 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72  r message to pEr
17ce7 72 6f 72 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 63  rorDb..*/.static
17ce8 20 42 74 72 65 65 20 2a 66 69 6e 64 42 74 72 65   Btree *findBtre
17ce9 65 28 73 71 6c 69 74 65 33 20 2a 70 45 72 72 6f  e(sqlite3 *pErro
17cea 72 44 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 44  rDb, sqlite3 *pD
17ceb 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
17cec 44 62 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 73  Db){.  int i = s
17ced 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65  qlite3FindDbName
17cee 28 70 44 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 69  (pDb, zDb);..  i
17cef 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 50  f( i==1 ){.    P
17cf0 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 20  arse sParse;.   
17cf1 20 6d 65 6d 73 65 74 28 26 73 50 61 72 73 65 2c   memset(&sParse,
17cf2 20 30 2c 20 73 69 7a 65 6f 66 28 73 50 61 72 73   0, sizeof(sPars
17cf3 65 29 29 3b 0a 20 20 20 20 73 50 61 72 73 65 2e  e));.    sParse.
17cf4 64 62 20 3d 20 70 44 62 3b 0a 20 20 20 20 69 66  db = pDb;.    if
17cf5 28 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d  ( sqlite3OpenTem
17cf6 70 44 61 74 61 62 61 73 65 28 26 73 50 61 72 73  pDatabase(&sPars
17cf7 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  e) ){.      sqli
17cf8 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 26 73  te3ErrorClear(&s
17cf9 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71  Parse);.      sq
17cfa 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f  lite3Error(pErro
17cfb 72 44 62 2c 20 73 50 61 72 73 65 2e 72 63 2c 20  rDb, sParse.rc, 
17cfc 22 25 73 22 2c 20 73 50 61 72 73 65 2e 7a 45 72  "%s", sParse.zEr
17cfd 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 72 65 74  rMsg);.      ret
17cfe 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 0;.    }.   
17cff 20 61 73 73 65 72 74 28 20 73 50 61 72 73 65 2e   assert( sParse.
17d00 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20  zErrMsg==0 );.  
17d01 7d 0a 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a  }..  if( i<0 ){.
17d02 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
17d03 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54  (pErrorDb, SQLIT
17d04 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77  E_ERROR, "unknow
17d05 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20  n database %s", 
17d06 7a 44 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  zDb);.    return
17d07 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72   0;.  }..  retur
17d08 6e 20 70 44 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  n pDb->aDb[i].pB
17d09 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  t;.}../*.** Crea
17d0a 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61  te an sqlite3_ba
17d0b 63 6b 75 70 20 70 72 6f 63 65 73 73 20 74 6f 20  ckup process to 
17d0c 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74  copy the content
17d0d 73 20 6f 66 20 7a 53 72 63 44 62 20 66 72 6f 6d  s of zSrcDb from
17d0e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  .** connection h
17d0f 61 6e 64 6c 65 20 70 53 72 63 44 62 20 74 6f 20  andle pSrcDb to 
17d10 7a 44 65 73 74 44 62 20 69 6e 20 70 44 65 73 74  zDestDb in pDest
17d11 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  Db. If successfu
17d12 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  l, return.** a p
17d13 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
17d14 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  w sqlite3_backup
17d15 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49   object..**.** I
17d16 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
17d17 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  s, NULL is retur
17d18 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ned and an error
17d19 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20   code and error 
17d1a 6d 65 73 73 61 67 65 0a 2a 2a 20 73 74 6f 72 65  message.** store
17d1b 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 68 61  d in database ha
17d1c 6e 64 6c 65 20 70 44 65 73 74 44 62 2e 0a 2a 2f  ndle pDestDb..*/
17d1d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
17d1e 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69  te3_backup *sqli
17d1f 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
17d20 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73  .  sqlite3* pDes
17d21 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  tDb,            
17d22 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
17d23 62 61 73 65 20 74 6f 20 77 72 69 74 65 20 74 6f  base to write to
17d24 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
17d25 20 2a 7a 44 65 73 74 44 62 2c 20 20 20 20 20 20   *zDestDb,      
17d26 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17d27 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
17d28 77 69 74 68 69 6e 20 70 44 65 73 74 44 62 20 2a  within pDestDb *
17d29 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72  /.  sqlite3* pSr
17d2a 63 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  cDb,            
17d2b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
17d2c 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
17d2d 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f   to read from */
17d2e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
17d2f 53 72 63 44 62 20 20 20 20 20 20 20 20 20 20 20  SrcDb           
17d30 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
17d31 20 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74   of database wit
17d32 68 69 6e 20 70 53 72 63 44 62 20 2a 2f 0a 29 7b  hin pSrcDb */.){
17d33 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
17d34 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  p *p;           
17d35 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
17d36 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a  e to return */..
17d37 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 73 6f    /* Lock the so
17d38 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61  urce database ha
17d39 6e 64 6c 65 2e 20 54 68 65 20 64 65 73 74 69 6e  ndle. The destin
17d3a 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 20  ation database. 
17d3b 20 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f   ** handle is no
17d3c 74 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 69 73  t locked in this
17d3d 20 72 6f 75 74 69 6e 65 2c 20 62 75 74 20 69 74   routine, but it
17d3e 20 69 73 20 6c 6f 63 6b 65 64 20 69 6e 0a 20 20   is locked in.  
17d3f 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
17d40 70 5f 73 74 65 70 28 29 2e 20 54 68 65 20 75 73  p_step(). The us
17d41 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20 74  er is required t
17d42 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 6e 6f  o ensure that no
17d43 0a 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 72 65  .  ** other thre
17d44 61 64 20 61 63 63 65 73 73 65 73 20 74 68 65 20  ad accesses the 
17d45 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 6e 64  destination hand
17d46 6c 65 20 66 6f 72 20 74 68 65 20 64 75 72 61 74  le for the durat
17d47 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  ion.  ** of the 
17d48 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
17d49 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74  .  Any attempt t
17d4a 6f 20 75 73 65 20 74 68 65 20 64 65 73 74 69 6e  o use the destin
17d4b 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 61 74 61 62  ation.  ** datab
17d4c 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  ase connection w
17d4d 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73  hile a backup is
17d4e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 61 79   in progress may
17d4f 20 63 61 75 73 65 0a 20 20 2a 2a 20 61 20 6d 61   cause.  ** a ma
17d50 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20 64  lfunction or a d
17d51 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  eadlock..  */.  
17d52 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
17d53 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74 65  ter(pSrcDb->mute
17d54 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  x);.  sqlite3_mu
17d55 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73 74 44  tex_enter(pDestD
17d56 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69 66  b->mutex);..  if
17d57 28 20 70 53 72 63 44 62 3d 3d 70 44 65 73 74 44  ( pSrcDb==pDestD
17d58 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
17d59 45 72 72 6f 72 28 0a 20 20 20 20 20 20 20 20 70  Error(.        p
17d5a 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 45  DestDb, SQLITE_E
17d5b 52 52 4f 52 2c 20 22 73 6f 75 72 63 65 20 61 6e  RROR, "source an
17d5c 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 75  d destination mu
17d5d 73 74 20 62 65 20 64 69 73 74 69 6e 63 74 22 0a  st be distinct".
17d5e 20 20 20 20 29 3b 0a 20 20 20 20 70 20 3d 20 30      );.    p = 0
17d5f 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20  ;.  }else {.    
17d60 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  /* Allocate spac
17d61 65 20 66 6f 72 20 61 20 6e 65 77 20 73 71 6c 69  e for a new sqli
17d62 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63  te3_backup objec
17d63 74 20 2a 2f 0a 20 20 20 20 70 20 3d 20 28 73 71  t */.    p = (sq
17d64 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 73  lite3_backup *)s
17d65 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
17d66 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63  zeof(sqlite3_bac
17d67 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 21  kup));.    if( !
17d68 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
17d69 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 2c  e3Error(pDestDb,
17d6a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30   SQLITE_NOMEM, 0
17d6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
17d6c 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61  /* If the alloca
17d6d 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c 20  tion succeeded, 
17d6e 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77  populate the new
17d6f 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 66   object. */.  if
17d70 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
17d71 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  t(p, 0, sizeof(s
17d72 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 3b  qlite3_backup));
17d73 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 66  .    p->pSrc = f
17d74 69 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62  indBtree(pDestDb
17d75 2c 20 70 53 72 63 44 62 2c 20 7a 53 72 63 44 62  , pSrcDb, zSrcDb
17d76 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 20  );.    p->pDest 
17d77 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65 73  = findBtree(pDes
17d78 74 44 62 2c 20 70 44 65 73 74 44 62 2c 20 7a 44  tDb, pDestDb, zD
17d79 65 73 74 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70  estDb);.    p->p
17d7a 44 65 73 74 44 62 20 3d 20 70 44 65 73 74 44 62  DestDb = pDestDb
17d7b 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 44 62 20  ;.    p->pSrcDb 
17d7c 3d 20 70 53 72 63 44 62 3b 0a 20 20 20 20 70 2d  = pSrcDb;.    p-
17d7d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a 20 20 20  >iNext = 1;..   
17d7e 20 69 66 28 20 30 3d 3d 70 2d 3e 70 53 72 63 20   if( 0==p->pSrc 
17d7f 7c 7c 20 30 3d 3d 70 2d 3e 70 44 65 73 74 20 29  || 0==p->pDest )
17d80 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 28  {.      /* One (
17d81 6f 72 20 62 6f 74 68 29 20 6f 66 20 74 68 65 20  or both) of the 
17d82 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 73 20  named databases 
17d83 64 69 64 20 6e 6f 74 20 65 78 69 73 74 2e 20 41  did not exist. A
17d84 6e 20 65 72 72 6f 72 20 68 61 73 0a 20 20 20 20  n error has.    
17d85 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65    ** already bee
17d86 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  n written into t
17d87 68 65 20 70 44 65 73 74 44 62 20 68 61 6e 64 6c  he pDestDb handl
17d88 65 2e 20 41 6c 6c 20 74 68 61 74 20 69 73 20 6c  e. All that is l
17d89 65 66 74 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  eft.      ** to 
17d8a 64 6f 20 68 65 72 65 20 69 73 20 66 72 65 65 20  do here is free 
17d8b 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
17d8c 75 70 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  up structure..  
17d8d 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
17d8e 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
17d8f 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 20 20 7d      p = 0;.    }
17d90 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 65 76  .  }..  /* If ev
17d91 65 72 79 74 68 69 6e 67 20 68 61 73 20 67 6f 6e  erything has gon
17d92 65 20 61 73 20 70 6c 61 6e 6e 65 64 2c 20 61 74  e as planned, at
17d93 74 61 63 68 20 74 68 65 20 62 61 63 6b 75 70 20  tach the backup 
17d94 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 0a 20 20  object to the.  
17d95 2a 2a 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e  ** source pager.
17d96 20 54 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   The source page
17d97 72 20 63 61 6c 6c 73 20 42 61 63 6b 75 70 55 70  r calls BackupUp
17d98 64 61 74 65 28 29 20 61 6e 64 20 42 61 63 6b 75  date() and Backu
17d99 70 52 65 73 74 61 72 74 28 29 0a 20 20 2a 2a 20  pRestart().  ** 
17d9a 74 6f 20 6e 6f 74 69 66 79 20 74 68 69 73 20 6d  to notify this m
17d9b 6f 64 75 6c 65 20 69 66 20 74 68 65 20 73 6f 75  odule if the sou
17d9c 72 63 65 20 66 69 6c 65 20 69 73 20 6d 6f 64 69  rce file is modi
17d9d 66 69 65 64 20 6d 69 64 2d 62 61 63 6b 75 70 2e  fied mid-backup.
17d9e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b  .  */.  if( p ){
17d9f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 61 63  .    sqlite3_bac
17da0 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20 20 20  kup **pp;       
17da1 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
17da2 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61 67 65   to head of page
17da3 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74 20 2a  rs backup list *
17da4 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  /.    sqlite3Btr
17da5 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 63 29  eeEnter(p->pSrc)
17da6 3b 0a 20 20 20 20 70 70 20 3d 20 73 71 6c 69 74  ;.    pp = sqlit
17da7 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 72  e3PagerBackupPtr
17da8 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67  (sqlite3BtreePag
17da9 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20  er(p->pSrc));.  
17daa 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 70    p->pNext = *pp
17dab 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20  ;.    *pp = p;. 
17dac 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
17dad 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20  eave(p->pSrc);. 
17dae 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 63     p->pSrc->nBac
17daf 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 73 71  kup++;.  }..  sq
17db0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
17db1 65 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78  e(pDestDb->mutex
17db2 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
17db3 65 78 5f 6c 65 61 76 65 28 70 53 72 63 44 62 2d  ex_leave(pSrcDb-
17db4 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
17db5 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72  n p;.}../*.** Ar
17db6 67 75 6d 65 6e 74 20 72 63 20 69 73 20 61 6e 20  gument rc is an 
17db7 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
17db8 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  e. Return true i
17db9 66 20 74 68 69 73 20 65 72 72 6f 72 20 69 73 20  f this error is 
17dba 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 66  .** considered f
17dbb 61 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e 74 65  atal if encounte
17dbc 72 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63  red during a bac
17dbd 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 41  kup operation. A
17dbe 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a 20 61 72 65  ll errors.** are
17dbf 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61   considered fata
17dc0 6c 20 65 78 63 65 70 74 20 66 6f 72 20 53 51 4c  l except for SQL
17dc1 49 54 45 5f 42 55 53 59 20 61 6e 64 20 53 51 4c  ITE_BUSY and SQL
17dc2 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 73  ITE_LOCKED..*/.s
17dc3 74 61 74 69 63 20 69 6e 74 20 69 73 46 61 74 61  tatic int isFata
17dc4 6c 45 72 72 6f 72 28 69 6e 74 20 72 63 29 7b 0a  lError(int rc){.
17dc5 20 20 72 65 74 75 72 6e 20 28 72 63 21 3d 53 51    return (rc!=SQ
17dc6 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53  LITE_OK && rc!=S
17dc7 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72 63  QLITE_BUSY && rc
17dc8 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29  !=SQLITE_LOCKED)
17dc9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d  ;.}../*.** Param
17dca 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70 6f  eter zSrcData po
17dcb 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72  ints to a buffer
17dcc 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
17dcd 64 61 74 61 20 66 6f 72 20 0a 2a 2a 20 70 61 67  data for .** pag
17dce 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20 74 68  e iSrcPg from th
17dcf 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
17dd0 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64 61 74  e. Copy this dat
17dd1 61 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 64  a into the .** d
17dd2 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
17dd3 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ase..*/.static i
17dd4 6e 74 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65  nt backupOnePage
17dd5 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
17dd6 2a 70 2c 20 50 67 6e 6f 20 69 53 72 63 50 67 2c  *p, Pgno iSrcPg,
17dd7 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 72 63 44   const u8 *zSrcD
17dd8 61 74 61 29 7b 0a 20 20 50 61 67 65 72 20 2a 20  ata){.  Pager * 
17dd9 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72  const pDestPager
17dda 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
17ddb 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 0a  ager(p->pDest);.
17ddc 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63    const int nSrc
17ddd 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74  Pgsz = sqlite3Bt
17dde 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
17ddf 2d 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e  ->pSrc);.  int n
17de0 44 65 73 74 50 67 73 7a 20 3d 20 73 71 6c 69 74  DestPgsz = sqlit
17de1 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
17de2 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20  ze(p->pDest);.  
17de3 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f 70 79 20  const int nCopy 
17de4 3d 20 4d 49 4e 28 6e 53 72 63 50 67 73 7a 2c 20  = MIN(nSrcPgsz, 
17de5 6e 44 65 73 74 50 67 73 7a 29 3b 0a 20 20 63 6f  nDestPgsz);.  co
17de6 6e 73 74 20 69 36 34 20 69 45 6e 64 20 3d 20 28  nst i64 iEnd = (
17de7 69 36 34 29 69 53 72 63 50 67 2a 28 69 36 34 29  i64)iSrcPg*(i64)
17de8 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 20 69 6e 74  nSrcPgsz;..  int
17de9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
17dea 0a 20 20 69 36 34 20 69 4f 66 66 3b 0a 0a 20 20  .  i64 iOff;..  
17deb 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 73 74  assert( p->bDest
17dec 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 73 65  Locked );.  asse
17ded 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f  rt( !isFatalErro
17dee 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 61 73  r(p->rc) );.  as
17def 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50 45  sert( iSrcPg!=PE
17df0 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
17df1 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 3b  p->pSrc->pBt) );
17df2 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63 44  .  assert( zSrcD
17df3 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 74  ata );..  /* Cat
17df4 63 68 20 74 68 65 20 63 61 73 65 20 77 68 65 72  ch the case wher
17df5 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  e the destinatio
17df6 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  n is an in-memor
17df7 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74  y database and t
17df8 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 69 7a  he.  ** page siz
17df9 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  es of the source
17dfa 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e   and destination
17dfb 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f 0a 20   differ. .  */. 
17dfc 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 3d 6e   if( nSrcPgsz!=n
17dfd 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 6c 69  DestPgsz && sqli
17dfe 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28  te3PagerIsMemdb(
17dff 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
17e00 72 28 70 2d 3e 70 44 65 73 74 29 29 20 29 7b 0a  r(p->pDest)) ){.
17e01 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
17e02 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20  READONLY;.  }.. 
17e03 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72 75   /* This loop ru
17e04 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ns once for each
17e05 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67   destination pag
17e06 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68 65  e spanned by the
17e07 20 73 6f 75 72 63 65 20 0a 20 20 2a 2a 20 70 61   source .  ** pa
17e08 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69 74 65  ge. For each ite
17e09 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62 6c 65  ration, variable
17e0a 20 69 4f 66 66 20 69 73 20 73 65 74 20 74 6f 20   iOff is set to 
17e0b 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a  the byte offset.
17e0c 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 73 74    ** of the dest
17e0d 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20  ination page..  
17e0e 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 66 3d 69 45  */.  for(iOff=iE
17e0f 6e 64 2d 28 69 36 34 29 6e 53 72 63 50 67 73 7a  nd-(i64)nSrcPgsz
17e10 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
17e11 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 69 4f  && iOff<iEnd; iO
17e12 66 66 2b 3d 6e 44 65 73 74 50 67 73 7a 29 7b 0a  ff+=nDestPgsz){.
17e13 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 65 73      DbPage *pDes
17e14 74 50 67 20 3d 20 30 3b 0a 20 20 20 20 50 67 6e  tPg = 0;.    Pgn
17e15 6f 20 69 44 65 73 74 20 3d 20 28 50 67 6e 6f 29  o iDest = (Pgno)
17e16 28 69 4f 66 66 2f 6e 44 65 73 74 50 67 73 7a 29  (iOff/nDestPgsz)
17e17 2b 31 3b 0a 20 20 20 20 69 66 28 20 69 44 65 73  +1;.    if( iDes
17e18 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t==PENDING_BYTE_
17e19 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70  PAGE(p->pDest->p
17e1a 42 74 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  Bt) ) continue;.
17e1b 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
17e1c 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
17e1d 50 61 67 65 72 47 65 74 28 70 44 65 73 74 50 61  PagerGet(pDestPa
17e1e 67 65 72 2c 20 69 44 65 73 74 2c 20 26 70 44 65  ger, iDest, &pDe
17e1f 73 74 50 67 29 29 0a 20 20 20 20 20 26 26 20 53  stPg)).     && S
17e20 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
17e21 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
17e22 65 28 70 44 65 73 74 50 67 29 29 0a 20 20 20 20  e(pDestPg)).    
17e23 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75  ){.      const u
17e24 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 72 63 44 61  8 *zIn = &zSrcDa
17e25 74 61 5b 69 4f 66 66 25 6e 53 72 63 50 67 73 7a  ta[iOff%nSrcPgsz
17e26 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 44 65  ];.      u8 *zDe
17e27 73 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33  stData = sqlite3
17e28 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 65  PagerGetData(pDe
17e29 73 74 50 67 29 3b 0a 20 20 20 20 20 20 75 38 20  stPg);.      u8 
17e2a 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 73 74 44 61  *zOut = &zDestDa
17e2b 74 61 5b 69 4f 66 66 25 6e 44 65 73 74 50 67 73  ta[iOff%nDestPgs
17e2c 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  z];..      /* Co
17e2d 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d  py the data from
17e2e 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   the source page
17e2f 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e   into the destin
17e30 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 20 20  ation page..    
17e31 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72 20    ** Then clear 
17e32 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 20  the Btree layer 
17e33 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 66  MemPage.isInit f
17e34 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20 6d  lag. Both this m
17e35 6f 64 75 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61  odule.      ** a
17e36 6e 64 20 74 68 65 20 70 61 67 65 72 20 63 6f 64  nd the pager cod
17e37 65 20 75 73 65 20 74 68 69 73 20 74 72 69 63 6b  e use this trick
17e38 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65 20 66   (clearing the f
17e39 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 20 20  irst byte.      
17e3a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 27  ** of the page '
17e3b 65 78 74 72 61 27 20 73 70 61 63 65 20 74 6f 20  extra' space to 
17e3c 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 42  invalidate the B
17e3d 74 72 65 65 20 6c 61 79 65 72 73 0a 20 20 20 20  tree layers.    
17e3e 20 20 2a 2a 20 63 61 63 68 65 64 20 70 61 72 73    ** cached pars
17e3f 65 20 6f 66 20 74 68 65 20 70 61 67 65 29 2e 20  e of the page). 
17e40 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 69  MemPage.isInit i
17e41 73 20 6d 61 72 6b 65 64 20 0a 20 20 20 20 20 20  s marked .      
17e42 2a 2a 20 22 4d 55 53 54 20 42 45 20 46 49 52 53  ** "MUST BE FIRS
17e43 54 22 20 66 6f 72 20 74 68 69 73 20 70 75 72 70  T" for this purp
17e44 6f 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ose..      */.  
17e45 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c      memcpy(zOut,
17e46 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 20   zIn, nCopy);.  
17e47 20 20 20 20 28 28 75 38 20 2a 29 73 71 6c 69 74      ((u8 *)sqlit
17e48 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
17e49 70 44 65 73 74 50 67 29 29 5b 30 5d 20 3d 20 30  pDestPg))[0] = 0
17e4a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
17e4b 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44  te3PagerUnref(pD
17e4c 65 73 74 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72  estPg);.  }..  r
17e4d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
17e4e 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20 63  ** If pFile is c
17e4f 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20  urrently larger 
17e50 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 73  than iSize bytes
17e51 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20  , then truncate 
17e52 69 74 20 74 6f 0a 2a 2a 20 65 78 61 63 74 6c 79  it to.** exactly
17e53 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20 49 66   iSize bytes. If
17e54 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20 6c 61   pFile is not la
17e55 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20  rger than iSize 
17e56 62 79 74 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74  bytes, then.** t
17e57 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
17e58 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52  a no-op..**.** R
17e59 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
17e5a 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  if everything is
17e5b 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20   successful, or 
17e5c 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
17e5d 0a 2a 2a 20 63 6f 64 65 20 69 66 20 61 6e 20 65  .** code if an e
17e5e 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a  rror occurs..*/.
17e5f 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75  static int backu
17e60 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 73 71  pTruncateFile(sq
17e61 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
17e62 65 2c 20 69 36 34 20 69 53 69 7a 65 29 7b 0a 20  e, i64 iSize){. 
17e63 20 69 36 34 20 69 43 75 72 72 65 6e 74 3b 0a 20   i64 iCurrent;. 
17e64 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
17e65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 46 69 6c  3OsFileSize(pFil
17e66 65 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20  e, &iCurrent);. 
17e67 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
17e68 4f 4b 20 26 26 20 69 43 75 72 72 65 6e 74 3e 69  OK && iCurrent>i
17e69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 20 3d  Size ){.    rc =
17e6a 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
17e6b 74 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29  te(pFile, iSize)
17e6c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
17e6d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79  c;.}../*.** Copy
17e6e 20 6e 50 61 67 65 20 70 61 67 65 73 20 66 72 6f   nPage pages fro
17e6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74  m the source b-t
17e70 72 65 65 20 74 6f 20 74 68 65 20 64 65 73 74 69  ree to the desti
17e71 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  nation..*/.SQLIT
17e72 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
17e73 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71  3_backup_step(sq
17e74 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c  lite3_backup *p,
17e75 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69   int nPage){.  i
17e76 6e 74 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74 65  nt rc;..  sqlite
17e77 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
17e78 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b  >pSrcDb->mutex);
17e79 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
17e7a 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20  nter(p->pSrc);. 
17e7b 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20   if( p->pDestDb 
17e7c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
17e7d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44  utex_enter(p->pD
17e7e 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  estDb->mutex);. 
17e7f 20 7d 0a 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63   }..  rc = p->rc
17e80 3b 0a 20 20 69 66 28 20 21 69 73 46 61 74 61 6c  ;.  if( !isFatal
17e81 45 72 72 6f 72 28 72 63 29 20 29 7b 0a 20 20 20  Error(rc) ){.   
17e82 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70   Pager * const p
17e83 53 72 63 50 61 67 65 72 20 3d 20 73 71 6c 69 74  SrcPager = sqlit
17e84 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e  e3BtreePager(p->
17e85 70 53 72 63 29 3b 20 20 20 20 20 2f 2a 20 53 6f  pSrc);     /* So
17e86 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 20 20  urce pager */.  
17e87 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20    Pager * const 
17e88 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c  pDestPager = sql
17e89 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
17e8a 2d 3e 70 44 65 73 74 29 3b 20 20 20 2f 2a 20 44  ->pDest);   /* D
17e8b 65 73 74 20 70 61 67 65 72 20 2a 2f 0a 20 20 20  est pager */.   
17e8c 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20   int ii;        
17e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e8e 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
17e8f 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20  variable */.    
17e90 69 6e 74 20 6e 53 72 63 50 61 67 65 20 3d 20 2d  int nSrcPage = -
17e91 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
17e92 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 6f     /* Size of so
17e93 75 72 63 65 20 64 62 20 69 6e 20 70 61 67 65 73  urce db in pages
17e94 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 43 6c 6f   */.    int bClo
17e95 73 65 54 72 61 6e 73 20 3d 20 30 3b 20 20 20 20  seTrans = 0;    
17e96 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
17e97 75 65 20 69 66 20 73 72 63 20 64 62 20 72 65 71  ue if src db req
17e98 75 69 72 65 73 20 75 6e 6c 6f 63 6b 69 6e 67 20  uires unlocking 
17e99 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  */..    /* If th
17e9a 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 69  e source pager i
17e9b 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61  s currently in a
17e9c 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
17e9d 6f 6e 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 2a  on, return.    *
17e9e 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 6d  * SQLITE_BUSY im
17e9f 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a  mediately..    *
17ea0 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65  /.    if( p->pDe
17ea1 73 74 44 62 20 26 26 20 70 2d 3e 70 53 72 63 2d  stDb && p->pSrc-
17ea2 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  >pBt->inTransact
17ea3 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
17ea4 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
17ea5 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
17ea6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
17ea7 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
17ea8 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20   }..    /* Lock 
17ea9 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
17eaa 64 61 74 61 62 61 73 65 2c 20 69 66 20 69 74 20  database, if it 
17eab 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c  is not locked al
17eac 72 65 61 64 79 2e 20 2a 2f 0a 20 20 20 20 69 66  ready. */.    if
17ead 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
17eae 26 26 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65  && p->bDestLocke
17eaf 64 3d 3d 30 0a 20 20 20 20 20 26 26 20 53 51 4c  d==0.     && SQL
17eb0 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
17eb1 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
17eb2 72 61 6e 73 28 70 2d 3e 70 44 65 73 74 2c 20 32  rans(p->pDest, 2
17eb3 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  )) .    ){.     
17eb4 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20   p->bDestLocked 
17eb5 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 1;.      rc = 
17eb6 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
17eb7 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 20 31 2c  eta(p->pDest, 1,
17eb8 20 26 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61   &p->iDestSchema
17eb9 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
17eba 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
17ebb 6f 70 65 6e 20 72 65 61 64 2d 74 72 61 6e 73 61  open read-transa
17ebc 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75  ction on the sou
17ebd 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 6f 70  rce database, op
17ebe 65 6e 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f  en.    ** one no
17ebf 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74  w. If a transact
17ec0 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 68 65  ion is opened he
17ec1 72 65 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c  re, then it will
17ec2 20 62 65 20 63 6c 6f 73 65 64 0a 20 20 20 20 2a   be closed.    *
17ec3 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  * before this fu
17ec4 6e 63 74 69 6f 6e 20 65 78 69 74 73 2e 0a 20 20  nction exits..  
17ec5 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
17ec6 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d  =SQLITE_OK && 0=
17ec7 3d 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49  =sqlite3BtreeIsI
17ec8 6e 52 65 61 64 54 72 61 6e 73 28 70 2d 3e 70 53  nReadTrans(p->pS
17ec9 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  rc) ){.      rc 
17eca 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  = sqlite3BtreeBe
17ecb 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 53 72 63  ginTrans(p->pSrc
17ecc 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 43 6c 6f  , 0);.      bClo
17ecd 73 65 54 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20  seTrans = 1;.   
17ece 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77   }.  .    /* Now
17ecf 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61   that there is a
17ed0 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
17ed1 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
17ed2 65 2c 20 71 75 65 72 79 20 74 68 65 0a 20 20 20  e, query the.   
17ed3 20 2a 2a 20 73 6f 75 72 63 65 20 70 61 67 65 72   ** source pager
17ed4 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20   for the number 
17ed5 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
17ed6 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f  database..    */
17ed7 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
17ed8 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17ed9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
17eda 72 50 61 67 65 63 6f 75 6e 74 28 70 53 72 63 50  rPagecount(pSrcP
17edb 61 67 65 72 2c 20 26 6e 53 72 63 50 61 67 65 29  ager, &nSrcPage)
17edc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
17edd 69 69 3d 30 3b 20 28 6e 50 61 67 65 3c 30 20 7c  ii=0; (nPage<0 |
17ede 7c 20 69 69 3c 6e 50 61 67 65 29 20 26 26 20 70  | ii<nPage) && p
17edf 2d 3e 69 4e 65 78 74 3c 3d 28 50 67 6e 6f 29 6e  ->iNext<=(Pgno)n
17ee0 53 72 63 50 61 67 65 20 26 26 20 21 72 63 3b 20  SrcPage && !rc; 
17ee1 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e  ii++){.      con
17ee2 73 74 20 50 67 6e 6f 20 69 53 72 63 50 67 20 3d  st Pgno iSrcPg =
17ee3 20 70 2d 3e 69 4e 65 78 74 3b 20 20 20 20 20 20   p->iNext;      
17ee4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
17ee5 75 72 63 65 20 70 61 67 65 20 6e 75 6d 62 65 72  urce page number
17ee6 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 53   */.      if( iS
17ee7 72 63 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59  rcPg!=PENDING_BY
17ee8 54 45 5f 50 41 47 45 28 70 2d 3e 70 53 72 63 2d  TE_PAGE(p->pSrc-
17ee9 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  >pBt) ){.       
17eea 20 44 62 50 61 67 65 20 2a 70 53 72 63 50 67 3b   DbPage *pSrcPg;
17eeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17eec 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17eed 53 6f 75 72 63 65 20 70 61 67 65 20 6f 62 6a 65  Source page obje
17eee 63 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63  ct */.        rc
17eef 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
17ef0 65 74 28 70 53 72 63 50 61 67 65 72 2c 20 69 53  et(pSrcPager, iS
17ef1 72 63 50 67 2c 20 26 70 53 72 63 50 67 29 3b 0a  rcPg, &pSrcPg);.
17ef2 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
17ef3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
17ef4 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 63 6b         rc = back
17ef5 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 53 72  upOnePage(p, iSr
17ef6 63 50 67 2c 20 73 71 6c 69 74 65 33 50 61 67 65  cPg, sqlite3Page
17ef7 72 47 65 74 44 61 74 61 28 70 53 72 63 50 67 29  rGetData(pSrcPg)
17ef8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
17ef9 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
17efa 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20  SrcPg);.        
17efb 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
17efc 70 2d 3e 69 4e 65 78 74 2b 2b 3b 0a 20 20 20 20  p->iNext++;.    
17efd 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
17efe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17eff 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 20 3d   p->nPagecount =
17f00 20 6e 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20   nSrcPage;.     
17f01 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 20 3d   p->nRemaining =
17f02 20 6e 53 72 63 50 61 67 65 2b 31 2d 70 2d 3e 69   nSrcPage+1-p->i
17f03 4e 65 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20  Next;.      if( 
17f04 70 2d 3e 69 4e 65 78 74 3e 28 50 67 6e 6f 29 6e  p->iNext>(Pgno)n
17f05 53 72 63 50 61 67 65 20 29 7b 0a 20 20 20 20 20  SrcPage ){.     
17f06 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
17f07 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ONE;.      }.   
17f08 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63   }.  .    if( rc
17f09 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
17f0a 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  .      const int
17f0b 20 6e 53 72 63 50 61 67 65 73 69 7a 65 20 3d 20   nSrcPagesize = 
17f0c 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
17f0d 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 29  ageSize(p->pSrc)
17f0e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e  ;.      const in
17f0f 74 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20  t nDestPagesize 
17f10 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
17f11 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65  tPageSize(p->pDe
17f12 73 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  st);.      int n
17f13 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20 20  DestTruncate;.  
17f14 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65  .      /* Update
17f15 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73   the schema vers
17f16 69 6f 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 65  ion field in the
17f17 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
17f18 61 62 61 73 65 2e 20 54 68 69 73 0a 20 20 20 20  abase. This.    
17f19 20 20 2a 2a 20 69 73 20 74 6f 20 6d 61 6b 65 20    ** is to make 
17f1a 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73 63  sure that the sc
17f1b 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 72 65 61  hema-version rea
17f1c 6c 6c 79 20 64 6f 65 73 20 63 68 61 6e 67 65 20  lly does change 
17f1d 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  in.      ** the 
17f1e 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 73  case where the s
17f1f 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
17f20 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 20  ation databases 
17f21 68 61 76 65 20 74 68 65 0a 20 20 20 20 20 20 2a  have the.      *
17f22 2a 20 73 61 6d 65 20 73 63 68 65 6d 61 20 76 65  * same schema ve
17f23 72 73 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a  rsion..      */.
17f24 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
17f25 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2d 3e  eeUpdateMeta(p->
17f26 70 44 65 73 74 2c 20 31 2c 20 70 2d 3e 69 44 65  pDest, 1, p->iDe
17f27 73 74 53 63 68 65 6d 61 2b 31 29 3b 0a 20 20 20  stSchema+1);.   
17f28 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44     if( p->pDestD
17f29 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  b ){.        sql
17f2a 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
17f2b 6c 53 63 68 65 6d 61 28 70 2d 3e 70 44 65 73 74  lSchema(p->pDest
17f2c 44 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  Db, 0);.      }.
17f2d 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 44  .      /* Set nD
17f2e 65 73 74 54 72 75 6e 63 61 74 65 20 74 6f 20 74  estTruncate to t
17f2f 68 65 20 66 69 6e 61 6c 20 6e 75 6d 62 65 72 20  he final number 
17f30 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
17f31 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20  destination.    
17f32 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54    ** database. T
17f33 68 65 20 63 6f 6d 70 6c 69 63 61 74 69 6f 6e 20  he complication 
17f34 68 65 72 65 20 69 73 20 74 68 61 74 20 74 68 65  here is that the
17f35 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67   destination pag
17f36 65 0a 20 20 20 20 20 20 2a 2a 20 73 69 7a 65 20  e.      ** size 
17f37 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74  may be different
17f38 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 70   to the source p
17f39 61 67 65 20 73 69 7a 65 2e 20 0a 20 20 20 20 20  age size. .     
17f3a 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20   **.      ** If 
17f3b 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 20  the source page 
17f3c 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20  size is smaller 
17f3d 74 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61  than the destina
17f3e 74 69 6f 6e 20 70 61 67 65 20 73 69 7a 65 2c 20  tion page size, 
17f3f 0a 20 20 20 20 20 20 2a 2a 20 72 6f 75 6e 64 20  .      ** round 
17f40 75 70 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  up. In this case
17f41 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c   the call to sql
17f42 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 29  ite3OsTruncate()
17f43 20 62 65 6c 6f 77 20 77 69 6c 6c 0a 20 20 20 20   below will.    
17f44 20 20 2a 2a 20 66 69 78 20 74 68 65 20 73 69 7a    ** fix the siz
17f45 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 48  e of the file. H
17f46 6f 77 65 76 65 72 20 69 74 20 69 73 20 69 6d 70  owever it is imp
17f47 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 0a 20  ortant to call. 
17f48 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 50       ** sqlite3P
17f49 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67  agerTruncateImag
17f4a 65 28 29 20 68 65 72 65 20 73 6f 20 74 68 61 74  e() here so that
17f4b 20 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74 68   any pages in th
17f4c 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 74  e .      ** dest
17f4d 69 6e 61 74 69 6f 6e 20 66 69 6c 65 20 74 68 61  ination file tha
17f4e 74 20 6c 69 65 20 62 65 79 6f 6e 64 20 74 68 65  t lie beyond the
17f4f 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 70   nDestTruncate p
17f50 61 67 65 20 6d 61 72 6b 20 61 72 65 0a 20 20 20  age mark are.   
17f51 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64     ** journalled
17f52 20 62 79 20 50 61 67 65 72 43 6f 6d 6d 69 74 50   by PagerCommitP
17f53 68 61 73 65 4f 6e 65 28 29 20 62 65 66 6f 72 65  haseOne() before
17f54 20 74 68 65 79 20 61 72 65 20 64 65 73 74 72 6f   they are destro
17f55 79 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20  yed.      ** by 
17f56 74 68 65 20 66 69 6c 65 20 74 72 75 6e 63 61 74  the file truncat
17f57 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion..      */.  
17f58 20 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65      if( nSrcPage
17f59 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69  size<nDestPagesi
17f5a 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ze ){.        in
17f5b 74 20 72 61 74 69 6f 20 3d 20 6e 44 65 73 74 50  t ratio = nDestP
17f5c 61 67 65 73 69 7a 65 2f 6e 53 72 63 50 61 67 65  agesize/nSrcPage
17f5d 73 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 6e 44  size;.        nD
17f5e 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 28 6e  estTruncate = (n
17f5f 53 72 63 50 61 67 65 2b 72 61 74 69 6f 2d 31 29  SrcPage+ratio-1)
17f60 2f 72 61 74 69 6f 3b 0a 20 20 20 20 20 20 20 20  /ratio;.        
17f61 69 66 28 20 6e 44 65 73 74 54 72 75 6e 63 61 74  if( nDestTruncat
17f62 65 3d 3d 28 69 6e 74 29 50 45 4e 44 49 4e 47 5f  e==(int)PENDING_
17f63 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65  BYTE_PAGE(p->pDe
17f64 73 74 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20  st->pBt) ){.    
17f65 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63        nDestTrunc
17f66 61 74 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d  ate--;.        }
17f67 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
17f68 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63        nDestTrunc
17f69 61 74 65 20 3d 20 6e 53 72 63 50 61 67 65 20 2a  ate = nSrcPage *
17f6a 20 28 6e 53 72 63 50 61 67 65 73 69 7a 65 2f 6e   (nSrcPagesize/n
17f6b 44 65 73 74 50 61 67 65 73 69 7a 65 29 3b 0a 20  DestPagesize);. 
17f6c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
17f6d 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
17f6e 65 49 6d 61 67 65 28 70 44 65 73 74 50 61 67 65  eImage(pDestPage
17f6f 72 2c 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65  r, nDestTruncate
17f70 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 6e 53  );..      if( nS
17f71 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74  rcPagesize<nDest
17f72 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20  Pagesize ){.    
17f73 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f      /* If the so
17f74 75 72 63 65 20 70 61 67 65 2d 73 69 7a 65 20 69  urce page-size i
17f75 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74  s smaller than t
17f76 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70  he destination p
17f77 61 67 65 2d 73 69 7a 65 2c 0a 20 20 20 20 20 20  age-size,.      
17f78 20 20 2a 2a 20 74 77 6f 20 65 78 74 72 61 20 74    ** two extra t
17f79 68 69 6e 67 73 20 6d 61 79 20 6e 65 65 64 20 74  hings may need t
17f7a 6f 20 68 61 70 70 65 6e 3a 0a 20 20 20 20 20 20  o happen:.      
17f7b 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
17f7c 20 20 2a 20 54 68 65 20 64 65 73 74 69 6e 61 74    * The destinat
17f7d 69 6f 6e 20 6d 61 79 20 6e 65 65 64 20 74 6f 20  ion may need to 
17f7e 62 65 20 74 72 75 6e 63 61 74 65 64 2c 20 61 6e  be truncated, an
17f7f 64 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  d.        **.   
17f80 20 20 20 20 20 2a 2a 20 20 20 2a 20 44 61 74 61       **   * Data
17f81 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 70   stored on the p
17f82 61 67 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  ages immediately
17f83 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 0a   following the .
17f84 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 70          **     p
17f85 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65  ending-byte page
17f86 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
17f87 61 74 61 62 61 73 65 20 6d 61 79 20 6e 65 65 64  atabase may need
17f88 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20 2a   to be.        *
17f89 2a 20 20 20 20 20 63 6f 70 69 65 64 20 69 6e 74  *     copied int
17f8a 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
17f8b 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  n database..    
17f8c 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63      */.        c
17f8d 6f 6e 73 74 20 69 36 34 20 69 53 69 7a 65 20 3d  onst i64 iSize =
17f8e 20 28 69 36 34 29 6e 53 72 63 50 61 67 65 73 69   (i64)nSrcPagesi
17f8f 7a 65 20 2a 20 28 69 36 34 29 6e 53 72 63 50 61  ze * (i64)nSrcPa
17f90 67 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ge;.        sqli
17f91 74 65 33 5f 66 69 6c 65 20 2a 20 63 6f 6e 73 74  te3_file * const
17f92 20 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33   pFile = sqlite3
17f93 50 61 67 65 72 46 69 6c 65 28 70 44 65 73 74 50  PagerFile(pDestP
17f94 61 67 65 72 29 3b 0a 0a 20 20 20 20 20 20 20 20  ager);..        
17f95 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
17f96 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
17f97 20 28 69 36 34 29 6e 44 65 73 74 54 72 75 6e 63   (i64)nDestTrunc
17f98 61 74 65 2a 28 69 36 34 29 6e 44 65 73 74 50 61  ate*(i64)nDestPa
17f99 67 65 73 69 7a 65 20 3e 3d 20 69 53 69 7a 65 20  gesize >= iSize 
17f9a 7c 7c 20 28 0a 20 20 20 20 20 20 20 20 20 20 20  || (.           
17f9b 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65     nDestTruncate
17f9c 3d 3d 28 69 6e 74 29 28 50 45 4e 44 49 4e 47 5f  ==(int)(PENDING_
17f9d 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65  BYTE_PAGE(p->pDe
17f9e 73 74 2d 3e 70 42 74 29 2d 31 29 0a 20 20 20 20  st->pBt)-1).    
17f9f 20 20 20 20 20 20 20 26 26 20 69 53 69 7a 65 3e         && iSize>
17fa0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26  =PENDING_BYTE &&
17fa1 20 69 53 69 7a 65 3c 3d 50 45 4e 44 49 4e 47 5f   iSize<=PENDING_
17fa2 42 59 54 45 2b 6e 44 65 73 74 50 61 67 65 73 69  BYTE+nDestPagesi
17fa3 7a 65 0a 20 20 20 20 20 20 20 20 29 29 3b 0a 20  ze.        ));. 
17fa4 20 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54         if( SQLIT
17fa5 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
17fa6 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68  te3PagerCommitPh
17fa7 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65  aseOne(pDestPage
17fa8 72 2c 20 30 2c 20 31 29 29 0a 20 20 20 20 20 20  r, 0, 1)).      
17fa9 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
17faa 3d 28 72 63 20 3d 20 62 61 63 6b 75 70 54 72 75  =(rc = backupTru
17fab 6e 63 61 74 65 46 69 6c 65 28 70 46 69 6c 65 2c  ncateFile(pFile,
17fac 20 69 53 69 7a 65 29 29 0a 20 20 20 20 20 20 20   iSize)).       
17fad 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
17fae 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
17faf 65 72 53 79 6e 63 28 70 44 65 73 74 50 61 67 65  erSync(pDestPage
17fb0 72 29 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20  r)).        ){. 
17fb1 20 20 20 20 20 20 20 20 20 69 36 34 20 69 4f 66           i64 iOf
17fb2 66 3b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34  f;.          i64
17fb3 20 69 45 6e 64 20 3d 20 4d 49 4e 28 50 45 4e 44   iEnd = MIN(PEND
17fb4 49 4e 47 5f 42 59 54 45 20 2b 20 6e 44 65 73 74  ING_BYTE + nDest
17fb5 50 61 67 65 73 69 7a 65 2c 20 69 53 69 7a 65 29  Pagesize, iSize)
17fb6 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
17fb7 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66  .            iOf
17fb8 66 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e  f=PENDING_BYTE+n
17fb9 53 72 63 50 61 67 65 73 69 7a 65 3b 20 0a 20 20  SrcPagesize; .  
17fba 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51            rc==SQ
17fbb 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c  LITE_OK && iOff<
17fbc 69 45 6e 64 3b 20 0a 20 20 20 20 20 20 20 20 20  iEnd; .         
17fbd 20 20 20 69 4f 66 66 2b 3d 6e 53 72 63 50 61 67     iOff+=nSrcPag
17fbe 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20 20 20  esize.          
17fbf 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 50  ){.            P
17fc0 67 48 64 72 20 2a 70 53 72 63 50 67 20 3d 20 30  gHdr *pSrcPg = 0
17fc1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6f  ;.            co
17fc2 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50 67 20  nst Pgno iSrcPg 
17fc3 3d 20 28 50 67 6e 6f 29 28 28 69 4f 66 66 2f 6e  = (Pgno)((iOff/n
17fc4 53 72 63 50 61 67 65 73 69 7a 65 29 2b 31 29 3b  SrcPagesize)+1);
17fc5 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
17fc6 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
17fc7 74 28 70 53 72 63 50 61 67 65 72 2c 20 69 53 72  t(pSrcPager, iSr
17fc8 63 50 67 2c 20 26 70 53 72 63 50 67 29 3b 0a 20  cPg, &pSrcPg);. 
17fc9 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72             if( r
17fca 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
17fcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 38                u8
17fcc 20 2a 7a 44 61 74 61 20 3d 20 73 71 6c 69 74 65   *zData = sqlite
17fcd 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 53  3PagerGetData(pS
17fce 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20  rcPg);.         
17fcf 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
17fd0 33 4f 73 57 72 69 74 65 28 70 46 69 6c 65 2c 20  3OsWrite(pFile, 
17fd1 7a 44 61 74 61 2c 20 6e 53 72 63 50 61 67 65 73  zData, nSrcPages
17fd2 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20  ize, iOff);.    
17fd3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17fd4 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
17fd5 65 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b  erUnref(pSrcPg);
17fd6 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
17fd7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
17fd8 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  se{.        rc =
17fd9 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
17fda 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73  mitPhaseOne(pDes
17fdb 74 50 61 67 65 72 2c 20 30 2c 20 30 29 3b 0a 20  tPager, 0, 0);. 
17fdc 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20       }.  .      
17fdd 2f 2a 20 46 69 6e 69 73 68 20 63 6f 6d 6d 69 74  /* Finish commit
17fde 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63  ting the transac
17fdf 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 65 73 74  tion to the dest
17fe0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
17fe1 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 53  . */.      if( S
17fe2 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20  QLITE_OK==rc.   
17fe3 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b      && SQLITE_OK
17fe4 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42  ==(rc = sqlite3B
17fe5 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
17fe6 77 6f 28 70 2d 3e 70 44 65 73 74 29 29 0a 20 20  wo(p->pDest)).  
17fe7 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 72      ){.        r
17fe8 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
17fe9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
17fea 20 0a 20 20 20 20 2f 2a 20 49 66 20 62 43 6c 6f   .    /* If bClo
17feb 73 65 54 72 61 6e 73 20 69 73 20 74 72 75 65 2c  seTrans is true,
17fec 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
17fed 69 6f 6e 20 6f 70 65 6e 65 64 20 61 20 72 65 61  ion opened a rea
17fee 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  d transaction.  
17fef 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 6f 75 72    ** on the sour
17ff0 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 6c 6f  ce database. Clo
17ff1 73 65 20 74 68 65 20 72 65 61 64 20 74 72 61 6e  se the read tran
17ff2 73 61 63 74 69 6f 6e 20 68 65 72 65 2e 20 54 68  saction here. Th
17ff3 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f  ere is.    ** no
17ff4 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 74   need to check t
17ff5 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73  he return values
17ff6 20 6f 66 20 74 68 65 20 62 74 72 65 65 20 6d 65   of the btree me
17ff7 74 68 6f 64 73 20 68 65 72 65 2c 20 61 73 0a 20  thods here, as. 
17ff8 20 20 20 2a 2a 20 22 63 6f 6d 6d 69 74 74 69 6e     ** "committin
17ff9 67 22 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74  g" a read-only t
17ffa 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f  ransaction canno
17ffb 74 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20  t fail..    */. 
17ffc 20 20 20 69 66 28 20 62 43 6c 6f 73 65 54 72 61     if( bCloseTra
17ffd 6e 73 20 29 7b 0a 20 20 20 20 20 20 54 45 53 54  ns ){.      TEST
17ffe 4f 4e 4c 59 28 20 69 6e 74 20 72 63 32 20 29 3b  ONLY( int rc2 );
17fff 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28  .      TESTONLY(
18000 20 72 63 32 20 20 3d 20 29 20 73 71 6c 69 74 65   rc2  = ) sqlite
18001 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
18002 65 4f 6e 65 28 70 2d 3e 70 53 72 63 2c 20 30 29  eOne(p->pSrc, 0)
18003 3b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59  ;.      TESTONLY
18004 28 20 72 63 32 20 7c 3d 20 29 20 73 71 6c 69 74  ( rc2 |= ) sqlit
18005 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
18006 73 65 54 77 6f 28 70 2d 3e 70 53 72 63 29 3b 0a  seTwo(p->pSrc);.
18007 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
18008 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  2==SQLITE_OK );.
18009 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 70 2d 3e      }.  .    p->
1800a 72 63 20 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 69  rc = rc;.  }.  i
1800b 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b  f( p->pDestDb ){
1800c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1800d 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73  ex_leave(p->pDes
1800e 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  tDb->mutex);.  }
1800f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
18010 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20  eave(p->pSrc);. 
18011 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
18012 65 61 76 65 28 70 2d 3e 70 53 72 63 44 62 2d 3e  eave(p->pSrcDb->
18013 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
18014 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
18015 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72  lease all resour
18016 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
18017 69 74 68 20 61 6e 20 73 71 6c 69 74 65 33 5f 62  ith an sqlite3_b
18018 61 63 6b 75 70 2a 20 68 61 6e 64 6c 65 2e 0a 2a  ackup* handle..*
18019 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1801a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
1801b 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62  finish(sqlite3_b
1801c 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c  ackup *p){.  sql
1801d 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70  ite3_backup **pp
1801e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1801f 20 20 2f 2a 20 50 74 72 20 74 6f 20 68 65 61 64    /* Ptr to head
18020 20 6f 66 20 70 61 67 65 72 73 20 62 61 63 6b 75   of pagers backu
18021 70 20 6c 69 73 74 20 2a 2f 0a 20 20 73 71 6c 69  p list */.  sqli
18022 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
18023 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
18024 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 70 72 6f   /* Mutex to pro
18025 74 65 63 74 20 73 6f 75 72 63 65 20 64 61 74 61  tect source data
18026 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  base */.  int rc
18027 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
18028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18029 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
1802a 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 45 6e 74 65 72  n */..  /* Enter
1802b 20 74 68 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a   the mutexes */.
1802c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1802d 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d  enter(p->pSrcDb-
1802e 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74  >mutex);.  sqlit
1802f 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e  e3BtreeEnter(p->
18030 70 53 72 63 29 3b 0a 20 20 6d 75 74 65 78 20 3d  pSrc);.  mutex =
18031 20 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65   p->pSrcDb->mute
18032 78 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73  x;.  if( p->pDes
18033 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tDb ){.    sqlit
18034 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
18035 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78  ->pDestDb->mutex
18036 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74  );.  }..  /* Det
18037 61 63 68 20 74 68 69 73 20 62 61 63 6b 75 70 20  ach this backup 
18038 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20  from the source 
18039 70 61 67 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20  pager. */.  if( 
1803a 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20  p->pDestDb ){.  
1803b 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 61    pp = sqlite3Pa
1803c 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 6c  gerBackupPtr(sql
1803d 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
1803e 2d 3e 70 53 72 63 29 29 3b 0a 20 20 20 20 77 68  ->pSrc));.    wh
1803f 69 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b 0a 20  ile( *pp!=p ){. 
18040 20 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29       pp = &(*pp)
18041 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  ->pNext;.    }. 
18042 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78     *pp = p->pNex
18043 74 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e  t;.    p->pSrc->
18044 6e 42 61 63 6b 75 70 2d 2d 3b 0a 20 20 7d 0a 0a  nBackup--;.  }..
18045 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61    /* If a transa
18046 63 74 69 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f  ction is still o
18047 70 65 6e 20 6f 6e 20 74 68 65 20 42 74 72 65 65  pen on the Btree
18048 2c 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20  , roll it back. 
18049 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  */.  sqlite3Btre
1804a 65 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e 70 44 65  eRollback(p->pDe
1804b 73 74 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74  st);..  /* Set t
1804c 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66  he error code of
1804d 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
1804e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1804f 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e  . */.  rc = (p->
18050 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29  rc==SQLITE_DONE)
18051 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70   ? SQLITE_OK : p
18052 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 45  ->rc;.  sqlite3E
18053 72 72 6f 72 28 70 2d 3e 70 44 65 73 74 44 62 2c  rror(p->pDestDb,
18054 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45   rc, 0);..  /* E
18055 78 69 74 20 74 68 65 20 6d 75 74 65 78 65 73 20  xit the mutexes 
18056 61 6e 64 20 66 72 65 65 20 74 68 65 20 62 61 63  and free the bac
18057 6b 75 70 20 63 6f 6e 74 65 78 74 20 73 74 72 75  kup context stru
18058 63 74 75 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20  cture. */.  if( 
18059 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20  p->pDestDb ){.  
1805a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1805b 6c 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62  leave(p->pDestDb
1805c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
1805d 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1805e 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66  e(p->pSrc);.  if
1805f 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a  ( p->pDestDb ){.
18060 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
18061 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  (p);.  }.  sqlit
18062 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
18063 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
18064 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
18065 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
18066 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f  f pages still to
18067 20 62 65 20 62 61 63 6b 65 64 20 75 70 20 61 73   be backed up as
18068 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
18069 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73  ent.** call to s
1806a 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
1806b 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ep()..*/.SQLITE_
1806c 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1806d 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67  backup_remaining
1806e 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
1806f 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
18070 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a 7d 0a 0a  >nRemaining;.}..
18071 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
18072 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
18073 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f   pages in the so
18074 75 72 63 65 20 64 61 74 61 62 61 73 65 20 61 73  urce database as
18075 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 0a 2a 2a   of the most .**
18076 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20   recent call to 
18077 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
18078 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tep()..*/.SQLITE
18079 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1807a 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e  _backup_pagecoun
1807b 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t(sqlite3_backup
1807c 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
1807d 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 3b 0a 7d 0a  ->nPagecount;.}.
1807e 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1807f 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61  tion is called a
18080 66 74 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74  fter the content
18081 73 20 6f 66 20 70 61 67 65 20 69 50 61 67 65 20  s of page iPage 
18082 6f 66 20 74 68 65 0a 2a 2a 20 73 6f 75 72 63 65  of the.** source
18083 20 64 61 74 61 62 61 73 65 20 68 61 76 65 20 62   database have b
18084 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 49 66  een modified. If
18085 20 70 61 67 65 20 69 50 61 67 65 20 68 61 73 20   page iPage has 
18086 61 6c 72 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a  already been .**
18087 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
18088 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
18089 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20  abase, then the 
1808a 64 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f 20  data written to 
1808b 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69  the.** destinati
1808c 6f 6e 20 69 73 20 6e 6f 77 20 69 6e 76 61 6c 69  on is now invali
1808d 64 61 74 65 64 2e 20 54 68 65 20 64 65 73 74 69  dated. The desti
1808e 6e 61 74 69 6f 6e 20 63 6f 70 79 20 6f 66 20 69  nation copy of i
1808f 50 61 67 65 20 6e 65 65 64 73 0a 2a 2a 20 74 6f  Page needs.** to
18090 20 62 65 20 75 70 64 61 74 65 64 20 77 69 74 68   be updated with
18091 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 62 65   the new data be
18092 66 6f 72 65 20 74 68 65 20 62 61 63 6b 75 70 20  fore the backup 
18093 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20  operation is.** 
18094 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20  complete..**.** 
18095 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68  It is assumed th
18096 61 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73  at the mutex ass
18097 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
18098 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
18099 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  .** correspondin
1809a 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20  g to the source 
1809b 64 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64  database is held
1809c 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
1809d 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64  ion is.** called
1809e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1809f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
180a0 42 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c  BackupUpdate(sql
180a1 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61  ite3_backup *pBa
180a2 63 6b 75 70 2c 20 50 67 6e 6f 20 69 50 61 67 65  ckup, Pgno iPage
180a3 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74  , const u8 *aDat
180a4 61 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61  a){.  sqlite3_ba
180a5 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20  ckup *p;        
180a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
180a7 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
180a8 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b  */.  for(p=pBack
180a9 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  up; p; p=p->pNex
180aa 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  t){.    assert( 
180ab 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
180ac 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d  ld(p->pSrc->pBt-
180ad 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 69  >mutex) );.    i
180ae 66 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72  f( !isFatalError
180af 28 70 2d 3e 72 63 29 20 26 26 20 69 50 61 67 65  (p->rc) && iPage
180b0 3c 70 2d 3e 69 4e 65 78 74 20 29 7b 0a 20 20 20  <p->iNext ){.   
180b1 20 20 20 2f 2a 20 54 68 65 20 62 61 63 6b 75 70     /* The backup
180b2 20 70 72 6f 63 65 73 73 20 70 20 68 61 73 20 61   process p has a
180b3 6c 72 65 61 64 79 20 63 6f 70 69 65 64 20 70 61  lready copied pa
180b4 67 65 20 69 50 61 67 65 2e 20 42 75 74 20 6e 6f  ge iPage. But no
180b5 77 20 69 74 0a 20 20 20 20 20 20 2a 2a 20 68 61  w it.      ** ha
180b6 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  s been modified 
180b7 62 79 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  by a transaction
180b8 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 70   on the source p
180b9 61 67 65 72 2e 20 43 6f 70 79 0a 20 20 20 20 20  ager. Copy.     
180ba 20 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61   ** the new data
180bb 20 69 6e 74 6f 20 74 68 65 20 62 61 63 6b 75 70   into the backup
180bc 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
180bd 20 69 6e 74 20 72 63 20 3d 20 62 61 63 6b 75 70   int rc = backup
180be 4f 6e 65 50 61 67 65 28 70 2c 20 69 50 61 67 65  OnePage(p, iPage
180bf 2c 20 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20  , aData);.      
180c0 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
180c1 54 45 5f 42 55 53 59 20 26 26 20 72 63 21 3d 53  TE_BUSY && rc!=S
180c2 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 3b 0a  QLITE_LOCKED );.
180c3 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
180c4 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
180c5 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20     p->rc = rc;. 
180c6 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
180c7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72  .}../*.** Restar
180c8 74 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f  t the backup pro
180c9 63 65 73 73 2e 20 54 68 69 73 20 69 73 20 63 61  cess. This is ca
180ca 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 70 61  lled when the pa
180cb 67 65 72 20 6c 61 79 65 72 0a 2a 2a 20 64 65 74  ger layer.** det
180cc 65 63 74 73 20 74 68 61 74 20 74 68 65 20 64 61  ects that the da
180cd 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20  tabase has been 
180ce 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 65  modified by an e
180cf 78 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65  xternal database
180d0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  .** connection. 
180d1 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
180d2 72 65 20 69 73 20 6e 6f 20 77 61 79 20 6f 66 20  re is no way of 
180d3 6b 6e 6f 77 69 6e 67 20 77 68 69 63 68 20 6f 66  knowing which of
180d4 20 74 68 65 0a 2a 2a 20 70 61 67 65 73 20 74 68   the.** pages th
180d5 61 74 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70  at have been cop
180d6 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73  ied into the des
180d7 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
180d8 65 20 61 72 65 20 73 74 69 6c 6c 20 0a 2a 2a 20  e are still .** 
180d9 76 61 6c 69 64 20 61 6e 64 20 77 68 69 63 68 20  valid and which 
180da 61 72 65 20 6e 6f 74 2c 20 73 6f 20 74 68 65 20  are not, so the 
180db 65 6e 74 69 72 65 20 70 72 6f 63 65 73 73 20 6e  entire process n
180dc 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61  eeds to be resta
180dd 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  rted..**.** It i
180de 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  s assumed that t
180df 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61  he mutex associa
180e0 74 65 64 20 77 69 74 68 20 74 68 65 20 42 74 53  ted with the BtS
180e1 68 61 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20  hared object.** 
180e2 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
180e3 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
180e4 62 61 73 65 20 69 73 20 68 65 6c 64 20 77 68 65  base is held whe
180e5 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
180e6 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f  is.** called..*/
180e7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
180e8 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b  void sqlite3Back
180e9 75 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65  upRestart(sqlite
180ea 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75  3_backup *pBacku
180eb 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61  p){.  sqlite3_ba
180ec 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20  ckup *p;        
180ed 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
180ee 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
180ef 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b  */.  for(p=pBack
180f0 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  up; p; p=p->pNex
180f1 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  t){.    assert( 
180f2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
180f3 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d  ld(p->pSrc->pBt-
180f4 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70  >mutex) );.    p
180f5 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 7d  ->iNext = 1;.  }
180f6 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
180f7 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f  TE_OMIT_VACUUM./
180f8 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 6f  *.** Copy the co
180f9 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f  mplete content o
180fa 66 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70  f pBtFrom into p
180fb 42 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61 63  BtTo.  A transac
180fc 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20  tion.** must be 
180fd 61 63 74 69 76 65 20 66 6f 72 20 62 6f 74 68 20  active for both 
180fe 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  files..**.** The
180ff 20 73 69 7a 65 20 6f 66 20 66 69 6c 65 20 70 54   size of file pT
18100 6f 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64  o may be reduced
18101 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69   by this operati
18102 6f 6e 2e 20 49 66 20 61 6e 79 74 68 69 6e 67 20  on. If anything 
18103 0a 2a 2a 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20  .** goes wrong, 
18104 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
18105 6f 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64  on pTo is rolled
18106 20 62 61 63 6b 2e 20 49 66 20 73 75 63 63 65 73   back. If succes
18107 73 66 75 6c 2c 20 74 68 65 20 0a 2a 2a 20 74 72  sful, the .** tr
18108 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
18109 6d 69 74 74 65 64 20 62 65 66 6f 72 65 20 72 65  mitted before re
1810a 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  turning..*/.SQLI
1810b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1810c 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46  qlite3BtreeCopyF
1810d 69 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20  ile(Btree *pTo, 
1810e 42 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20  Btree *pFrom){. 
1810f 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
18110 65 33 5f 62 61 63 6b 75 70 20 62 3b 0a 20 20 73  e3_backup b;.  s
18111 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
18112 28 70 54 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33  (pTo);.  sqlite3
18113 42 74 72 65 65 45 6e 74 65 72 28 70 46 72 6f 6d  BtreeEnter(pFrom
18114 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 20  );..  /* Set up 
18115 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  an sqlite3_backu
18116 70 20 6f 62 6a 65 63 74 2e 20 73 71 6c 69 74 65  p object. sqlite
18117 33 5f 62 61 63 6b 75 70 2e 70 44 65 73 74 44 62  3_backup.pDestDb
18118 20 6d 75 73 74 20 62 65 20 73 65 74 0a 20 20 2a   must be set.  *
18119 2a 20 74 6f 20 30 2e 20 54 68 69 73 20 69 73 20  * to 0. This is 
1811a 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c  used by the impl
1811b 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73  ementations of s
1811c 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
1811d 65 70 28 29 0a 20 20 2a 2a 20 61 6e 64 20 73 71  ep().  ** and sq
1811e 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
1811f 69 73 68 28 29 20 74 6f 20 64 65 74 65 63 74 20  ish() to detect 
18120 74 68 61 74 20 74 68 65 79 20 61 72 65 20 62 65  that they are be
18121 69 6e 67 20 63 61 6c 6c 65 64 0a 20 20 2a 2a 20  ing called.  ** 
18122 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69  from this functi
18123 6f 6e 2c 20 6e 6f 74 20 64 69 72 65 63 74 6c 79  on, not directly
18124 20 62 79 20 74 68 65 20 75 73 65 72 2e 0a 20 20   by the user..  
18125 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 62 2c 20  */.  memset(&b, 
18126 30 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 0a 20  0, sizeof(b));. 
18127 20 62 2e 70 53 72 63 44 62 20 3d 20 70 46 72 6f   b.pSrcDb = pFro
18128 6d 2d 3e 64 62 3b 0a 20 20 62 2e 70 53 72 63 20  m->db;.  b.pSrc 
18129 3d 20 70 46 72 6f 6d 3b 0a 20 20 62 2e 70 44 65  = pFrom;.  b.pDe
1812a 73 74 20 3d 20 70 54 6f 3b 0a 20 20 62 2e 69 4e  st = pTo;.  b.iN
1812b 65 78 74 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 30  ext = 1;..  /* 0
1812c 78 37 46 46 46 46 46 46 46 20 69 73 20 74 68 65  x7FFFFFFF is the
1812d 20 68 61 72 64 20 6c 69 6d 69 74 20 66 6f 72 20   hard limit for 
1812e 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
1812f 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73  ges in a databas
18130 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 42 79 20  e.  ** file. By 
18131 70 61 73 73 69 6e 67 20 74 68 69 73 20 61 73 20  passing this as 
18132 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
18133 67 65 73 20 74 6f 20 63 6f 70 79 20 74 6f 0a 20  ges to copy to. 
18134 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   ** sqlite3_back
18135 75 70 5f 73 74 65 70 28 29 2c 20 77 65 20 63 61  up_step(), we ca
18136 6e 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74  n guarantee that
18137 20 74 68 65 20 63 6f 70 79 20 66 69 6e 69 73 68   the copy finish
18138 65 73 20 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20  es .  ** within 
18139 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 28 75  a single call (u
1813a 6e 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f  nless an error o
1813b 63 63 75 72 73 29 2e 20 54 68 65 20 61 73 73 65  ccurs). The asse
1813c 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 0a 20  rt() statement. 
1813d 20 2a 2a 20 63 68 65 63 6b 73 20 74 68 69 73 20   ** checks this 
1813e 61 73 73 75 6d 70 74 69 6f 6e 20 2d 20 28 70 2d  assumption - (p-
1813f 3e 72 63 29 20 73 68 6f 75 6c 64 20 62 65 20 73  >rc) should be s
18140 65 74 20 74 6f 20 65 69 74 68 65 72 20 53 51 4c  et to either SQL
18141 49 54 45 5f 44 4f 4e 45 20 0a 20 20 2a 2a 20 6f  ITE_DONE .  ** o
18142 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e  r an error code.
18143 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
18144 62 61 63 6b 75 70 5f 73 74 65 70 28 26 62 2c 20  backup_step(&b, 
18145 30 78 37 46 46 46 46 46 46 46 29 3b 0a 20 20 61  0x7FFFFFFF);.  a
18146 73 73 65 72 74 28 20 62 2e 72 63 21 3d 53 51 4c  ssert( b.rc!=SQL
18147 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d  ITE_OK );.  rc =
18148 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
18149 66 69 6e 69 73 68 28 26 62 29 3b 0a 20 20 69 66  finish(&b);.  if
1814a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1814b 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 70 42 74 2d  ){.    pTo->pBt-
1814c 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d  >pageSizeFixed =
1814d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74   0;.  }..  sqlit
1814e 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 46 72  e3BtreeLeave(pFr
1814f 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  om);.  sqlite3Bt
18150 72 65 65 4c 65 61 76 65 28 70 54 6f 29 3b 0a 20  reeLeave(pTo);. 
18151 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
18152 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
18153 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f  MIT_VACUUM */../
18154 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
18155 6e 64 20 6f 66 20 62 61 63 6b 75 70 2e 63 20 2a  nd of backup.c *
18156 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18157 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
18159 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1815a 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 6d 65  egin file vdbeme
1815b 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m.c ************
1815c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1815d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1815e 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36  *.** 2004 May 26
1815f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
18160 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
18161 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
18162 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
18163 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
18164 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
18165 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
18166 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
18167 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
18168 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
18169 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1816a 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1816b 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1816c 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1816d 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1816e 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1816f 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
18170 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
18171 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18172 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18173 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
18175 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
18176 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
18177 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20  e to manipulate 
18178 22 4d 65 6d 22 20 73 74 72 75 63 74 75 72 65 2e  "Mem" structure.
18179 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f    A "Mem".** sto
1817a 72 65 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c  res a single val
1817b 75 65 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20  ue in the VDBE. 
1817c 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 71 75   Mem is an opaqu
1817d 65 20 73 74 72 75 63 74 75 72 65 20 76 69 73 69  e structure visi
1817e 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68  ble.** only with
1817f 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 49 6e  in the VDBE.  In
18180 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73  terface routines
18181 20 72 65 66 65 72 20 74 6f 20 61 20 4d 65 6d 20   refer to a Mem 
18182 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d  using the.** nam
18183 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a  e sqlite_value.*
18184 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 6d 65  *.** $Id: vdbeme
18185 6d 2e 63 2c 76 20 31 2e 31 33 39 20 32 30 30 39  m.c,v 1.139 2009
18186 2f 30 33 2f 32 39 20 31 35 3a 31 32 3a 31 30 20  /03/29 15:12:10 
18187 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
18188 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  .** Call sqlite3
18189 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
1818a 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c  b() on the suppl
1818b 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20  ied value (type 
1818c 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65  Mem*).** P if re
1818d 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69  quired..*/.#defi
1818e 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50 29  ne expandBlob(P)
1818f 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45   (((P)->flags&ME
18190 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56  M_Zero)?sqlite3V
18191 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
18192 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 66  (P):0)../*.** If
18193 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65   pMem is an obje
18194 63 74 20 77 69 74 68 20 61 20 76 61 6c 69 64 20  ct with a valid 
18195 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
18196 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74  ation, this rout
18197 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74  ine.** ensures t
18198 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f  he internal enco
18199 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74 72  ding for the str
1819a 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
1819b 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72 65  on is.** 'desire
1819c 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51  dEnc', one of SQ
1819d 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
1819e 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c  E_UTF16LE or SQL
1819f 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a  ITE_UTF16BE..**.
181a0 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f  ** If pMem is no
181a1 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65 63  t a string objec
181a2 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64 69  t, or the encodi
181a3 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  ng of the string
181a4 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69  .** representati
181a5 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73 74  on is already st
181a6 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 72  ored using the r
181a7 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69 6e  equested encodin
181a8 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20  g, then this.** 
181a9 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
181aa 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  op..**.** SQLITE
181ab 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
181ac 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  if the conversio
181ad 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20  n is successful 
181ae 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64  (or not required
181af 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d  )..** SQLITE_NOM
181b0 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e  EM may be return
181b1 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  ed if a malloc()
181b2 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f   fails during co
181b3 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77  nversion.** betw
181b4 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a  een formats..*/.
181b5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
181b6 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  nt sqlite3VdbeCh
181b7 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d  angeEncoding(Mem
181b8 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69   *pMem, int desi
181b9 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72  redEnc){.  int r
181ba 63 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  c;.  assert( (pM
181bb 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f  em->flags&MEM_Ro
181bc 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73  wSet)==0 );.  as
181bd 73 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63  sert( desiredEnc
181be 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c  ==SQLITE_UTF8 ||
181bf 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
181c0 49 54 45 5f 55 54 46 31 36 4c 45 0a 20 20 20 20  ITE_UTF16LE.    
181c1 20 20 20 20 20 20 20 7c 7c 20 64 65 73 69 72 65         || desire
181c2 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
181c3 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20 21 28  16BE );.  if( !(
181c4 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
181c5 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e  Str) || pMem->en
181c6 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 20 29 7b  c==desiredEnc ){
181c7 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
181c8 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73  TE_OK;.  }.  ass
181c9 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30  ert( pMem->db==0
181ca 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
181cb 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d  x_held(pMem->db-
181cc 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 64 65  >mutex) );.#ifde
181cd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
181ce 46 31 36 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  F16.  return SQL
181cf 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65  ITE_ERROR;.#else
181d0 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c  ..  /* MemTransl
181d1 61 74 65 28 29 20 6d 61 79 20 72 65 74 75 72 6e  ate() may return
181d2 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51   SQLITE_OK or SQ
181d3 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e  LITE_NOMEM. If N
181d4 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
181d5 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20  ,.  ** then the 
181d6 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20  encoding of the 
181d7 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20 68 61  value may not ha
181d8 76 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f  ve changed..  */
181d9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
181da 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28  dbeMemTranslate(
181db 70 4d 65 6d 2c 20 28 75 38 29 64 65 73 69 72 65  pMem, (u8)desire
181dc 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28  dEnc);.  assert(
181dd 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20  rc==SQLITE_OK   
181de 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   || rc==SQLITE_N
181df 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72 74 28  OMEM);.  assert(
181e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20  rc==SQLITE_OK   
181e1 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64   || pMem->enc!=d
181e2 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73  esiredEnc);.  as
181e3 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f  sert(rc==SQLITE_
181e4 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65  NOMEM || pMem->e
181e5 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 29 3b  nc==desiredEnc);
181e6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65  .  return rc;.#e
181e7 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  ndif.}../*.** Ma
181e8 6b 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20  ke sure pMem->z 
181e9 70 6f 69 6e 74 73 20 74 6f 20 61 20 77 72 69 74  points to a writ
181ea 61 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  able allocation 
181eb 6f 66 20 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20  of at least .** 
181ec 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  n bytes..**.** I
181ed 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c  f the memory cel
181ee 6c 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74  l currently cont
181ef 61 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 20 62  ains string or b
181f0 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20  lob data.** and 
181f1 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  the third argume
181f2 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  nt passed to thi
181f3 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72  s function is tr
181f4 75 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75 72 72  ue, the .** curr
181f5 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  ent content of t
181f6 68 65 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65  he cell is prese
181f7 72 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rved. Otherwise,
181f8 20 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20 64 69   it may.** be di
181f9 73 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a 2a 2a  scarded.  .**.**
181fa 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
181fb 65 74 73 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20  ets the MEM_Dyn 
181fc 66 6c 61 67 20 61 6e 64 20 63 6c 65 61 72 73 20  flag and clears 
181fd 61 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62 61 63  any xDel callbac
181fe 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c  k..** It also cl
181ff 65 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61  ears MEM_Ephem a
18200 6e 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49  nd MEM_Static. I
18201 66 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66  f the preserve f
18202 6c 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73  lag is .** not s
18203 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72  et, Mem.n is zer
18204 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  oed..*/.SQLITE_P
18205 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18206 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65  e3VdbeMemGrow(Me
18207 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20  m *pMem, int n, 
18208 69 6e 74 20 70 72 65 73 65 72 76 65 29 7b 0a 20  int preserve){. 
18209 20 61 73 73 65 72 74 28 20 31 20 3e 3d 0a 20 20   assert( 1 >=.  
1820a 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f    ((pMem->zMallo
1820b 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  c && pMem->zMall
1820c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31  oc==pMem->z) ? 1
1820d 20 3a 20 30 29 20 2b 0a 20 20 20 20 28 28 28 70   : 0) +.    (((p
1820e 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44  Mem->flags&MEM_D
1820f 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29  yn)&&pMem->xDel)
18210 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20   ? 1 : 0) + .   
18211 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d   ((pMem->flags&M
18212 45 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20 3a 20  EM_Ephem) ? 1 : 
18213 30 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d  0) + .    ((pMem
18214 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74  ->flags&MEM_Stat
18215 69 63 29 20 3f 20 31 20 3a 20 30 29 0a 20 20 29  ic) ? 1 : 0).  )
18216 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  ;.  assert( (pMe
18217 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77  m->flags&MEM_Row
18218 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66  Set)==0 );..  if
18219 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b  ( n<32 ) n = 32;
1821a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 44 62  .  if( sqlite3Db
1821b 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d  MallocSize(pMem-
1821c 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  >db, pMem->zMall
1821d 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28  oc)<n ){.    if(
1821e 20 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65   preserve && pMe
1821f 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c  m->z==pMem->zMal
18220 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65  loc ){.      pMe
18221 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61  m->z = pMem->zMa
18222 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
18223 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d  ReallocOrFree(pM
18224 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c  em->db, pMem->z,
18225 20 6e 29 3b 0a 20 20 20 20 20 20 70 72 65 73 65   n);.      prese
18226 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  rve = 0;.    }el
18227 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
18228 33 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62  3DbFree(pMem->db
18229 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29  , pMem->zMalloc)
1822a 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d  ;.      pMem->zM
1822b 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  alloc = sqlite3D
1822c 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d  bMallocRaw(pMem-
1822d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20  >db, n);.    }. 
1822e 20 7d 0a 0a 20 20 69 66 28 20 70 72 65 73 65 72   }..  if( preser
1822f 76 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 20 26 26  ve && pMem->z &&
18230 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26   pMem->zMalloc &
18231 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d  & pMem->z!=pMem-
18232 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >zMalloc ){.    
18233 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61  memcpy(pMem->zMa
18234 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70  lloc, pMem->z, p
18235 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69  Mem->n);.  }.  i
18236 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  f( pMem->flags&M
18237 45 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e  EM_Dyn && pMem->
18238 78 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d  xDel ){.    pMem
18239 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28  ->xDel((void *)(
1823a 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a  pMem->z));.  }..
1823b 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d    pMem->z = pMem
1823c 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28  ->zMalloc;.  if(
1823d 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20   pMem->z==0 ){. 
1823e 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1823f 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c   MEM_Null;.  }el
18240 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  se{.    pMem->fl
18241 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68  ags &= ~(MEM_Eph
18242 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a  em|MEM_Static);.
18243 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c    }.  pMem->xDel
18244 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28   = 0;.  return (
18245 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45  pMem->z ? SQLITE
18246 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d  _OK : SQLITE_NOM
18247 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  EM);.}../*.** Ma
18248 6b 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d  ke the given Mem
18249 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e   object MEM_Dyn.
1824a 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
1824b 2c 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20  , make it so.** 
1824c 74 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72  that any TEXT or
1824d 20 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73   BLOB content is
1824e 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72   stored in memor
1824f 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a  y obtained from.
18250 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e  ** malloc().  In
18251 20 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e   this way, we kn
18252 6f 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f  ow that the memo
18253 72 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65  ry is safe to be
18254 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20  .** overwritten 
18255 6f 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a  or altered..**.*
18256 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
18257 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72  OK on success or
18258 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
18259 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a   malloc fails..*
1825a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1825b 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1825c 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
1825d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69  (Mem *pMem){.  i
1825e 6e 74 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20  nt f;.  assert( 
1825f 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
18260 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
18261 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
18262 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
18263 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
18264 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
18265 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d   expandBlob(pMem
18266 29 3b 0a 20 20 66 20 3d 20 70 4d 65 6d 2d 3e 66  );.  f = pMem->f
18267 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 66 26 28  lags;.  if( (f&(
18268 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
18269 29 29 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70  )) && pMem->z!=p
1826a 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a  Mem->zMalloc ){.
1826b 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
1826c 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
1826d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29   pMem->n + 2, 1)
1826e 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1826f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
18270 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a     }.    pMem->z
18271 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20  [pMem->n] = 0;. 
18272 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d     pMem->z[pMem-
18273 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 70  >n+1] = 0;.    p
18274 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
18275 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 20 72  M_Term;.  }..  r
18276 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
18277 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
18278 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61 73 20   given Mem* has 
18279 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  a zero-filled ta
1827a 69 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e 74 6f  il, turn it into
1827b 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a 2a 2a 20   an ordinary.** 
1827c 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e 20 64  blob stored in d
1827d 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
1827e 61 74 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 23  ated space..*/.#
1827f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18280 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49  IT_INCRBLOB.SQLI
18281 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18282 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
18283 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65  andBlob(Mem *pMe
18284 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e  m){.  if( pMem->
18285 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
18286 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74   ){.    int nByt
18287 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e;.    assert( p
18288 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42  Mem->flags&MEM_B
18289 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72  lob );.    asser
1828a 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
1828b 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
1828c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  ;.    assert( pM
1828d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
1828e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1828f 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
18290 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20   );..    /* Set 
18291 6e 42 79 74 65 20 74 6f 20 74 68 65 20 6e 75 6d  nByte to the num
18292 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71  ber of bytes req
18293 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74  uired to store t
18294 68 65 20 65 78 70 61 6e 64 65 64 20 62 6c 6f 62  he expanded blob
18295 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74 65 20 3d  . */.    nByte =
18296 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d   pMem->n + pMem-
18297 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 69 66  >u.nZero;.    if
18298 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b 0a 20 20  ( nByte<=0 ){.  
18299 20 20 20 20 6e 42 79 74 65 20 3d 20 31 3b 0a 20      nByte = 1;. 
1829a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1829b 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1829c 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31 29 20  pMem, nByte, 1) 
1829d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1829e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1829f 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28    }..    memset(
182a0 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e  &pMem->z[pMem->n
182a1 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 2e 6e 5a  ], 0, pMem->u.nZ
182a2 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  ero);.    pMem->
182a3 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65  n += pMem->u.nZe
182a4 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ro;.    pMem->fl
182a5 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72  ags &= ~(MEM_Zer
182a6 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d  o|MEM_Term);.  }
182a7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
182a8 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  _OK;.}.#endif...
182a9 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
182aa 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73  the given Mem is
182ab 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74   \u0000 terminat
182ac 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
182ad 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
182ae 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
182af 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  nate(Mem *pMem){
182b0 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
182b1 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
182b2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65  3_mutex_held(pMe
182b3 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
182b4 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c  .  if( (pMem->fl
182b5 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21  ags & MEM_Term)!
182b6 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61  =0 || (pMem->fla
182b7 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30  gs & MEM_Str)==0
182b8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
182b9 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e  QLITE_OK;   /* N
182ba 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a  othing to do */.
182bb 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
182bc 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
182bd 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29  m, pMem->n+2, 1)
182be 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
182bf 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
182c0 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d  .  pMem->z[pMem-
182c1 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d  >n] = 0;.  pMem-
182c2 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20  >z[pMem->n+1] = 
182c3 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  0;.  pMem->flags
182c4 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20   |= MEM_Term;.  
182c5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
182c6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d  ;.}../*.** Add M
182c7 45 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65  EM_Str to the se
182c8 74 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74  t of representat
182c9 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76  ions for the giv
182ca 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73  en Mem.  Numbers
182cb 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65  .** are converte
182cc 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  d using sqlite3_
182cd 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e  snprintf().  Con
182ce 76 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74  verting a BLOB t
182cf 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73  o a string.** is
182d0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
182d1 45 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65  Existing represe
182d2 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74  ntations MEM_Int
182d3 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72   and MEM_Real ar
182d4 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61  e *not* invalida
182d5 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d  ted..**.** A MEM
182d6 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c  _Null value will
182d7 20 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64   never be passed
182d8 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
182d9 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  n. This function
182da 20 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20   is.** used for 
182db 63 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65  converting value
182dc 73 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65  s to text for re
182dd 74 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75  turning to the u
182de 73 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a  ser (i.e. via.**
182df 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
182e0 65 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65  ext()), or for e
182e1 6e 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c  nsuring that val
182e2 75 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61  ues to be used a
182e3 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20  s btree.** keys 
182e4 61 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20  are strings. In 
182e5 74 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20  the former case 
182e6 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69  a NULL pointer i
182e7 73 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a  s returned the.*
182e8 2a 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c  * user and the l
182e9 61 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72  ater is an inter
182ea 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20  nal programming 
182eb 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
182ec 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
182ed 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e  ite3VdbeMemStrin
182ee 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  gify(Mem *pMem, 
182ef 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20  int enc){.  int 
182f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
182f1 20 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d    int fg = pMem-
182f2 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20  >flags;.  const 
182f3 69 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a  int nByte = 32;.
182f4 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
182f5 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
182f6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65  3_mutex_held(pMe
182f7 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
182f8 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26  .  assert( !(fg&
182f9 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61  MEM_Zero) );.  a
182fa 73 73 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d  ssert( !(fg&(MEM
182fb 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20  _Str|MEM_Blob)) 
182fc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67 26  );.  assert( fg&
182fd 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
182fe 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  l) );.  assert( 
182ff 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
18300 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a  _RowSet)==0 );..
18301 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
18302 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
18303 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20  nByte, 0) ){.   
18304 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
18305 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  OMEM;.  }..  /* 
18306 46 6f 72 20 61 20 52 65 61 6c 20 6f 72 20 49 6e  For a Real or In
18307 74 65 67 65 72 2c 20 75 73 65 20 73 71 6c 69 74  teger, use sqlit
18308 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20  e3_mprintf() to 
18309 70 72 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d  produce the UTF-
1830a 38 0a 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65  8.  ** string re
1830b 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1830c 74 68 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c  the value. Then,
1830d 20 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64   if the required
1830e 20 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69   encoding.  ** i
1830f 73 20 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54  s UTF-16le or UT
18310 46 2d 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e  F-16be do a tran
18311 73 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20  slation..  ** . 
18312 20 2a 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77   ** FIX ME: It w
18313 6f 75 6c 64 20 62 65 20 62 65 74 74 65 72 20 69  ould be better i
18314 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e  f sqlite3_snprin
18315 74 66 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54  tf() could do UT
18316 46 2d 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  F-16..  */.  if(
18317 20 66 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b   fg & MEM_Int ){
18318 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
18319 72 69 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65  rintf(nByte, pMe
1831a 6d 2d 3e 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d  m->z, "%lld", pM
1831b 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73  em->u.i);.  }els
1831c 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 66  e{.    assert( f
1831d 67 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a  g & MEM_Real );.
1831e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1831f 69 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d  intf(nByte, pMem
18320 2d 3e 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70  ->z, "%!.15g", p
18321 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70  Mem->r);.  }.  p
18322 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33  Mem->n = sqlite3
18323 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a  Strlen30(pMem->z
18324 29 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  );.  pMem->enc =
18325 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20   SQLITE_UTF8;.  
18326 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pMem->flags |= M
18327 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b  EM_Str|MEM_Term;
18328 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
18329 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65  angeEncoding(pMe
1832a 6d 2c 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72  m, enc);.  retur
1832b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
1832c 65 6d 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20  emory cell pMem 
1832d 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e  contains the con
1832e 74 65 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65  text of an aggre
1832f 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  gate function..*
18330 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
18331 61 6c 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a  alls the finaliz
18332 65 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61  e method for tha
18333 74 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  t function.  The
18334 0a 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68  .** result of th
18335 65 20 61 67 67 72 65 67 61 74 65 20 69 73 20 73  e aggregate is s
18336 74 6f 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20  tored back into 
18337 70 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  pMem..**.** Retu
18338 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  rn SQLITE_ERROR 
18339 69 66 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72  if the finalizer
1833a 20 72 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f   reports an erro
1833b 72 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  r.  SQLITE_OK.**
1833c 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53   otherwise..*/.S
1833d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1833e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
1833f 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d  Finalize(Mem *pM
18340 65 6d 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75  em, FuncDef *pFu
18341 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  nc){.  int rc = 
18342 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
18343 20 70 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d   pFunc && pFunc-
18344 3e 78 46 69 6e 61 6c 69 7a 65 20 29 7b 0a 20 20  >xFinalize ){.  
18345 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
18346 74 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72  t ctx;.    asser
18347 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
18348 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c  & MEM_Null)!=0 |
18349 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75  | pFunc==pMem->u
1834a 2e 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73  .pDef );.    ass
1834b 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30  ert( pMem->db==0
1834c 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
1834d 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d  x_held(pMem->db-
1834e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d  >mutex) );.    m
1834f 65 6d 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73  emset(&ctx, 0, s
18350 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20  izeof(ctx));.   
18351 20 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d   ctx.s.flags = M
18352 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78  EM_Null;.    ctx
18353 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62  .s.db = pMem->db
18354 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d  ;.    ctx.pMem =
18355 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70   pMem;.    ctx.p
18356 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20  Func = pFunc;.  
18357 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69    pFunc->xFinali
18358 7a 65 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73  ze(&ctx);.    as
18359 73 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e  sert( 0==(pMem->
1835a 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26  flags&MEM_Dyn) &
1835b 26 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b  & !pMem->xDel );
1835c 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1835d 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65  ee(pMem->db, pMe
1835e 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20  m->zMalloc);.   
1835f 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63   memcpy(pMem, &c
18360 74 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78  tx.s, sizeof(ctx
18361 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 28  .s));.    rc = (
18362 63 74 78 2e 69 73 45 72 72 6f 72 3f 53 51 4c 49  ctx.isError?SQLI
18363 54 45 5f 45 52 52 4f 52 3a 53 51 4c 49 54 45 5f  TE_ERROR:SQLITE_
18364 4f 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  OK);.  }.  retur
18365 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
18366 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c  f the memory cel
18367 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 74 72  l contains a str
18368 69 6e 67 20 76 61 6c 75 65 20 74 68 61 74 20 6d  ing value that m
18369 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a  ust be freed by.
1836a 2a 2a 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65  ** invoking an e
1836b 78 74 65 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b  xternal callback
1836c 2c 20 66 72 65 65 20 69 74 20 6e 6f 77 2e 20 43  , free it now. C
1836d 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63  alling this func
1836e 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74  tion.** does not
1836f 20 66 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d   free any Mem.zM
18370 61 6c 6c 6f 63 20 62 75 66 66 65 72 2e 0a 2a 2f  alloc buffer..*/
18371 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18372 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
18373 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
18374 61 6c 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73  al(Mem *p){.  as
18375 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c  sert( p->db==0 |
18376 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
18377 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
18378 78 29 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66  x) );.  if( p->f
18379 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a  lags&MEM_Agg ){.
1837a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1837b 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 70 2d  emFinalize(p, p-
1837c 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 61 73  >u.pDef);.    as
1837d 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 20  sert( (p->flags 
1837e 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 3b  & MEM_Agg)==0 );
1837f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
18380 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20  MemRelease(p);. 
18381 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c   }else if( p->fl
18382 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70  ags&MEM_Dyn && p
18383 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 61 73  ->xDel ){.    as
18384 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26  sert( (p->flags&
18385 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
18386 3b 0a 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76  ;.    p->xDel((v
18387 6f 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20  oid *)p->z);.   
18388 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20   p->xDel = 0;.  
18389 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61  }else if( p->fla
1838a 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b  gs&MEM_RowSet ){
1838b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53  .    sqlite3RowS
1838c 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 52 6f  etClear(p->u.pRo
1838d 77 53 65 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  wSet);.  }.}../*
1838e 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20  .** Release any 
1838f 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74  memory held by t
18390 68 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79  he Mem. This may
18391 20 6c 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69   leave the Mem i
18392 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73  n an.** inconsis
18393 74 65 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20  tent state, for 
18394 65 78 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65  example with (Me
18395 6d 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28  m.z==0) and.** (
18396 4d 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45  Mem.type==SQLITE
18397 5f 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54  _TEXT)..*/.SQLIT
18398 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
18399 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1839a 65 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20  ease(Mem *p){.  
1839b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1839c 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29  leaseExternal(p)
1839d 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1839e 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c  e(p->db, p->zMal
1839f 6c 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30  loc);.  p->z = 0
183a0 3b 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d  ;.  p->zMalloc =
183a1 20 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20   0;.  p->xDel = 
183a2 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  0;.}../*.** Conv
183a3 65 72 74 20 61 20 36 34 2d 62 69 74 20 49 45 45  ert a 64-bit IEE
183a4 45 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20  E double into a 
183a5 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
183a6 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65  teger..** If the
183a7 20 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c   double is too l
183a8 61 72 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38  arge, return 0x8
183a9 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2e  000000000000000.
183aa 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74  .**.** Most syst
183ab 65 6d 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f  ems appear to do
183ac 20 74 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20   this simply by 
183ad 61 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72  assigning.** var
183ae 69 61 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f  iables and witho
183af 75 74 20 74 68 65 20 65 78 74 72 61 20 72 61 6e  ut the extra ran
183b0 67 65 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a  ge tests.  But.*
183b1 2a 20 74 68 65 72 65 20 61 72 65 20 72 65 70 6f  * there are repo
183b2 72 74 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73  rts that windows
183b3 20 74 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63   throws an expec
183b4 74 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66  tion.** if the f
183b5 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
183b6 6c 75 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61  lue is out of ra
183b7 6e 67 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74  nge. (See ticket
183b8 20 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61   #2880.).** Beca
183b9 75 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f  use we do not co
183ba 6d 70 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74  mpletely underst
183bb 61 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c  and the problem,
183bc 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65   we will.** take
183bd 20 74 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76   the conservativ
183be 65 20 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61  e approach and a
183bf 6c 77 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74  lways do range t
183c0 65 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61  ests.** before a
183c1 74 74 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f  ttempting the co
183c2 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  nversion..*/.sta
183c3 74 69 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f  tic i64 doubleTo
183c4 49 6e 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b  Int64(double r){
183c5 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20  .  /*.  ** Many 
183c6 63 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63  compilers we enc
183c7 6f 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65  ounter do not de
183c8 66 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66  fine constants f
183c9 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69  or the.  ** mini
183ca 6d 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20  mum and maximum 
183cb 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c  64-bit integers,
183cc 20 6f 72 20 74 68 65 79 20 64 65 66 69 6e 65 20   or they define 
183cd 74 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73  them.  ** incons
183ce 69 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d  istently.  And m
183cf 61 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72  any do not under
183d0 73 74 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e  stand the "LL" n
183d1 6f 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f  otation..  ** So
183d2 20 77 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f   we define our o
183d3 77 6e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61  wn static consta
183d4 6e 74 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e  nts here using n
183d5 6f 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67  othing.  ** larg
183d6 65 72 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74  er than a 32-bit
183d7 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
183d8 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63  t..  */.  static
183d9 20 63 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e   const i64 maxIn
183da 74 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36  t = LARGEST_INT6
183db 34 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  4;.  static cons
183dc 74 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53  t i64 minInt = S
183dd 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a  MALLEST_INT64;..
183de 20 20 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29    if( r<(double)
183df 6d 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65  minInt ){.    re
183e0 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d  turn minInt;.  }
183e1 65 6c 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62  else if( r>(doub
183e2 6c 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20  le)maxInt ){.   
183e3 20 2f 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f   /* minInt is co
183e4 72 72 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74  rrect here - not
183e5 20 6d 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72   maxInt.  It tur
183e6 6e 73 20 6f 75 74 20 74 68 61 74 20 61 73 73 69  ns out that assi
183e7 67 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76  gning.    ** a v
183e8 65 72 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69  ery large positi
183e9 76 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20  ve number to an 
183ea 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 73 20  integer results 
183eb 69 6e 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a  in a very large.
183ec 20 20 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20      ** negative 
183ed 69 6e 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d  integer.  This m
183ee 61 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62  akes no sense, b
183ef 75 74 20 69 74 20 69 73 20 77 68 61 74 20 78 38  ut it is what x8
183f0 36 20 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a  6 hardware.    *
183f1 2a 20 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f  * does so for co
183f2 6d 70 61 74 69 62 69 6c 69 74 79 20 77 65 20 77  mpatibility we w
183f3 69 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20  ill do the same 
183f4 69 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a  in software. */.
183f5 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e      return minIn
183f6 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
183f7 72 65 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20  return (i64)r;. 
183f8 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
183f9 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20  rn some kind of 
183fa 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 68  integer value wh
183fb 69 63 68 20 69 73 20 74 68 65 20 62 65 73 74 20  ich is the best 
183fc 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20  we can do.** at 
183fd 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65  representing the
183fe 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65   value that *pMe
183ff 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 20 61  m describes as a
18400 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66  n integer..** If
18401 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65   pMem is an inte
18402 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61  ger, then the va
18403 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20 20 49  lue is exact.  I
18404 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66  f pMem is.** a f
18405 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68  loating-point th
18406 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  en the value ret
18407 75 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74  urned is the int
18408 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66  eger part..** If
18409 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e   pMem is a strin
1840a 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20  g or blob, then 
1840b 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d  we make an attem
1840c 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a  pt to convert.**
1840d 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67   it into a integ
1840e 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  er and return th
1840f 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70  at.  If pMem rep
18410 72 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e  resents an.** an
18411 20 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c   SQL-NULL value,
18412 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a   return 0..**.**
18413 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65   If pMem represe
18414 6e 74 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c  nts a string val
18415 75 65 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67  ue, its encoding
18416 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65   might be change
18417 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
18418 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33  VATE i64 sqlite3
18419 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d  VdbeIntValue(Mem
1841a 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66   *pMem){.  int f
1841b 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20  lags;.  assert( 
1841c 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
1841d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1841e 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
1841f 78 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20  x) );.  flags = 
18420 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69  pMem->flags;.  i
18421 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  f( flags & MEM_I
18422 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  nt ){.    return
18423 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65   pMem->u.i;.  }e
18424 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
18425 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
18426 72 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 6f 49  return doubleToI
18427 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 20  nt64(pMem->r);. 
18428 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73   }else if( flags
18429 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f   & (MEM_Str|MEM_
1842a 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 36 34  Blob) ){.    i64
1842b 20 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 65 6d   value;.    pMem
1842c 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53  ->flags |= MEM_S
1842d 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  tr;.    if( sqli
1842e 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1842f 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49  oding(pMem, SQLI
18430 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20  TE_UTF8).       
18431 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  || sqlite3VdbeMe
18432 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d  mNulTerminate(pM
18433 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  em) ){.      ret
18434 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 0;.    }.   
18435 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a   assert( pMem->z
18436 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   );.    sqlite3A
18437 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 20 26  toi64(pMem->z, &
18438 76 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 74 75  value);.    retu
18439 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 6c 73  rn value;.  }els
1843a 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  e{.    return 0;
1843b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
1843c 74 75 72 6e 20 74 68 65 20 62 65 73 74 20 72 65  turn the best re
1843d 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1843e 70 4d 65 6d 20 74 68 61 74 20 77 65 20 63 61 6e  pMem that we can
1843f 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a 20 64   get into a.** d
18440 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 6d 20  ouble.  If pMem 
18441 69 73 20 61 6c 72 65 61 64 79 20 61 20 64 6f 75  is already a dou
18442 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 67 65  ble or an intege
18443 72 2c 20 72 65 74 75 72 6e 20 69 74 73 0a 2a 2a  r, return its.**
18444 20 76 61 6c 75 65 2e 20 20 49 66 20 69 74 20 69   value.  If it i
18445 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c  s a string or bl
18446 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65  ob, try to conve
18447 72 74 20 69 74 20 74 6f 20 61 20 64 6f 75 62 6c  rt it to a doubl
18448 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 61  e..** If it is a
18449 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e   NULL, return 0.
1844a 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  0..*/.SQLITE_PRI
1844b 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69  VATE double sqli
1844c 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65  te3VdbeRealValue
1844d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61  (Mem *pMem){.  a
1844e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
1844f 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
18450 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
18451 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  b->mutex) );.  i
18452 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
18453 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20   MEM_Real ){.   
18454 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b   return pMem->r;
18455 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65  .  }else if( pMe
18456 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  m->flags & MEM_I
18457 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  nt ){.    return
18458 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75   (double)pMem->u
18459 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  .i;.  }else if( 
1845a 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pMem->flags & (M
1845b 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
1845c 20 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62   ){.    /* (doub
1845d 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20  le)0 In case of 
1845e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
1845f 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f  TING_POINT... */
18460 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20  .    double val 
18461 3d 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20  = (double)0;.   
18462 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20   pMem->flags |= 
18463 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28  MEM_Str;.    if(
18464 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
18465 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c  geEncoding(pMem,
18466 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20   SQLITE_UTF8).  
18467 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56       || sqlite3V
18468 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
18469 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20  te(pMem) ){.    
1846a 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49    /* (double)0 I
1846b 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45  n case of SQLITE
1846c 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
1846d 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20  OINT... */.     
1846e 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29   return (double)
1846f 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  0;.    }.    ass
18470 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a  ert( pMem->z );.
18471 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28      sqlite3AtoF(
18472 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a  pMem->z, &val);.
18473 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
18474 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
18475 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73  (double)0 In cas
18476 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  e of SQLITE_OMIT
18477 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e  _FLOATING_POINT.
18478 2e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  .. */.    return
18479 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a   (double)0;.  }.
1847a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d  }../*.** The MEM
1847b 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c   structure is al
1847c 72 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c  ready a MEM_Real
1847d 2e 20 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d  .  Try to also m
1847e 61 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f  ake it a.** MEM_
1847f 49 6e 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a  Int if we can..*
18480 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18481 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
18482 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79  eIntegerAffinity
18483 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61  (Mem *pMem){.  a
18484 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61  ssert( pMem->fla
18485 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b  gs & MEM_Real );
18486 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
18487 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
18488 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73  wSet)==0 );.  as
18489 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
1848a 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
1848b 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
1848c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
1848d 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 62 6c  Mem->u.i = doubl
1848e 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72  eToInt64(pMem->r
1848f 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72  );.  if( pMem->r
18490 3d 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e  ==(double)pMem->
18491 75 2e 69 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  u.i ){.    pMem-
18492 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e  >flags |= MEM_In
18493 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t;.  }.}../*.** 
18494 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20  Convert pMem to 
18495 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49  type integer.  I
18496 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
18497 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ior representati
18498 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
18499 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1849a 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1849b 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ify(Mem *pMem){.
1849c 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1849d 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
1849e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
1849f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
184a0 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
184a1 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
184a2 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 70 4d 65  Set)==0 );.  pMe
184a3 6d 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65 33  m->u.i = sqlite3
184a4 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 4d 65  VdbeIntValue(pMe
184a5 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65  m);.  MemSetType
184a6 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49  Flag(pMem, MEM_I
184a7 6e 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  nt);.  return SQ
184a8 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
184a9 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73  * Convert pMem s
184aa 6f 20 74 68 61 74 20 69 74 20 69 73 20 6f 66 20  o that it is of 
184ab 74 79 70 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a  type MEM_Real..*
184ac 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79  * Invalidate any
184ad 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74   prior represent
184ae 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ations..*/.SQLIT
184af 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
184b0 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c  lite3VdbeMemReal
184b1 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ify(Mem *pMem){.
184b2 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
184b3 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
184b4 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
184b5 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
184b6 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69    pMem->r = sqli
184b7 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65  te3VdbeRealValue
184b8 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74  (pMem);.  MemSet
184b9 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d  TypeFlag(pMem, M
184ba 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 75  EM_Real);.  retu
184bb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
184bc 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70  ./*.** Convert p
184bd 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 68  Mem so that it h
184be 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 61  as types MEM_Rea
184bf 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 20  l or MEM_Int or 
184c0 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64  both..** Invalid
184c1 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65  ate any prior re
184c2 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  presentations..*
184c3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
184c4 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
184c5 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 20  MemNumerify(Mem 
184c6 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c 65  *pMem){.  double
184c7 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 20 69   r1, r2;.  i64 i
184c8 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  ;.  assert( (pMe
184c9 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  m->flags & (MEM_
184ca 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d  Int|MEM_Real|MEM
184cb 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20 20  _Null))==0 );.  
184cc 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
184cd 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62  lags & (MEM_Blob
184ce 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 29 3b  |MEM_Str))!=0 );
184cf 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
184d0 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
184d1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65  3_mutex_held(pMe
184d2 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
184d3 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 56  .  r1 = sqlite3V
184d4 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d 65  dbeRealValue(pMe
184d5 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c 65  m);.  i = double
184d6 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 20 72  ToInt64(r1);.  r
184d7 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a 20  2 = (double)i;. 
184d8 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a 20   if( r1==r2 ){. 
184d9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
184da 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d  mIntegerify(pMem
184db 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
184dc 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20 20  pMem->r = r1;.  
184dd 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
184de 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29  (pMem, MEM_Real)
184df 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
184e0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
184e1 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72  ** Delete any pr
184e2 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64  evious value and
184e3 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73   set the value s
184e4 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74  tored in *pMem t
184e5 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54  o NULL..*/.SQLIT
184e6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
184e7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
184e8 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  Null(Mem *pMem){
184e9 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  .  if( pMem->fla
184ea 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20  gs & MEM_RowSet 
184eb 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f  ){.    sqlite3Ro
184ec 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e  wSetClear(pMem->
184ed 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 7d 0a  u.pRowSet);.  }.
184ee 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
184ef 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  (pMem, MEM_Null)
184f0 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d  ;.  pMem->type =
184f1 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d 0a   SQLITE_NULL;.}.
184f2 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
184f3 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
184f4 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
184f5 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20  ue to be a BLOB 
184f6 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20 63  of length.** n c
184f7 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65  ontaining all ze
184f8 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ros..*/.SQLITE_P
184f9 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
184fa 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72  te3VdbeMemSetZer
184fb 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 2c  oBlob(Mem *pMem,
184fc 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74   int n){.  sqlit
184fd 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
184fe 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e  (pMem);.  pMem->
184ff 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62  flags = MEM_Blob
18500 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d 65  |MEM_Zero;.  pMe
18501 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
18502 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e 6e  _BLOB;.  pMem->n
18503 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30 20   = 0;.  if( n<0 
18504 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d  ) n = 0;.  pMem-
18505 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a 20 20  >u.nZero = n;.  
18506 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
18507 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a 0a 2a  TE_UTF8;.}../*.*
18508 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65  * Delete any pre
18509 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20  vious value and 
1850a 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74  set the value st
1850b 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f  ored in *pMem to
1850c 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73   val,.** manifes
1850d 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e 0a  t type INTEGER..
1850e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1850f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
18510 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65  beMemSetInt64(Me
18511 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 6c  m *pMem, i64 val
18512 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ){.  sqlite3Vdbe
18513 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
18514 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20  ;.  pMem->u.i = 
18515 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  val;.  pMem->fla
18516 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
18517 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
18518 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a 0a  ITE_INTEGER;.}..
18519 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79  /*.** Delete any
1851a 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
1851b 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
1851c 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65  e stored in *pMe
1851d 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e  m to val,.** man
1851e 69 66 65 73 74 20 74 79 70 65 20 52 45 41 4c 2e  ifest type REAL.
1851f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18520 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
18521 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28  dbeMemSetDouble(
18522 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62 6c  Mem *pMem, doubl
18523 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 73 71  e val){.  if( sq
18524 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 29 20  lite3IsNaN(val) 
18525 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
18526 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65  beMemSetNull(pMe
18527 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  m);.  }else{.   
18528 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
18529 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
1852a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c 3b    pMem->r = val;
1852b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
1852c 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20   = MEM_Real;.   
1852d 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
1852e 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a  LITE_FLOAT;.  }.
1852f 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
18530 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c  any previous val
18531 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  ue and set the v
18532 61 6c 75 65 20 6f 66 20 70 4d 65 6d 20 74 6f 20  alue of pMem to 
18533 62 65 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 20 62  be an.** empty b
18534 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 2e 0a 2a 2f  oolean index..*/
18535 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18536 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
18537 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 6d  MemSetRowSet(Mem
18538 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 71 6c 69 74   *pMem){.  sqlit
18539 65 33 20 2a 64 62 20 3d 20 70 4d 65 6d 2d 3e 64  e3 *db = pMem->d
1853a 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21  b;.  assert( db!
1853b 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d  =0 );.  if( pMem
1853c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
1853d 77 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  wSet ){.    sqli
1853e 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70  te3RowSetClear(p
1853f 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b  Mem->u.pRowSet);
18540 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
18541 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
18542 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70  ase(pMem);.    p
18543 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73  Mem->zMalloc = s
18544 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
18545 77 28 64 62 2c 20 36 34 29 3b 0a 20 20 7d 0a 20  w(db, 64);.  }. 
18546 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
18547 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 4d 65  ailed ){.    pMe
18548 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  m->flags = MEM_N
18549 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ull;.  }else{.  
1854a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1854b 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 70  zMalloc );.    p
1854c 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 20 3d  Mem->u.pRowSet =
1854d 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e   sqlite3RowSetIn
1854e 69 74 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61  it(db, pMem->zMa
1854f 6c 6c 6f 63 2c 20 0a 20 20 20 20 20 20 20 20 20  lloc, .         
18550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
18552 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
18553 65 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c  e(db, pMem->zMal
18554 6c 6f 63 29 29 3b 0a 20 20 20 20 61 73 73 65 72  loc));.    asser
18555 74 28 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53  t( pMem->u.pRowS
18556 65 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 4d 65  et!=0 );.    pMe
18557 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52  m->flags = MEM_R
18558 6f 77 53 65 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  owSet;.  }.}../*
18559 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
1855a 69 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a 65 63  if the Mem objec
1855b 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 54 45 58  t contains a TEX
1855c 54 20 6f 72 20 42 4c 4f 42 20 74 68 61 74 20 69  T or BLOB that i
1855d 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65 20 2d  s.** too large -
1855e 20 77 68 6f 73 65 20 73 69 7a 65 20 65 78 63 65   whose size exce
1855f 65 64 73 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  eds SQLITE_MAX_L
18560 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ENGTH..*/.SQLITE
18561 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
18562 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69  ite3VdbeMemTooBi
18563 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73  g(Mem *p){.  ass
18564 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b  ert( p->db!=0 );
18565 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  .  if( p->flags 
18566 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
18567 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  lob) ){.    int 
18568 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 20 20 69 66  n = p->n;.    if
18569 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
1856a 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 6e  _Zero ){.      n
1856b 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a   += p->u.nZero;.
1856c 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
1856d 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74   n>p->db->aLimit
1856e 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
1856f 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72 65 74  NGTH];.  }.  ret
18570 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn 0; .}../*.**
18571 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63 74 20   Size of struct 
18572 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e  Mem not includin
18573 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f  g the Mem.zMallo
18574 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65  c member..*/.#de
18575 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 5a 45  fine MEMCELLSIZE
18576 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28 4d 65   (size_t)(&(((Me
18577 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 29  m *)0)->zMalloc)
18578 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e  )../*.** Make an
18579 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66   shallow copy of
1857a 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e   pFrom into pTo.
1857b 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73    Prior contents
1857c 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20 66   of.** pTo are f
1857d 72 65 65 64 2e 20 20 54 68 65 20 70 46 72 6f 6d  reed.  The pFrom
1857e 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74  ->z field is not
1857f 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20 49 66   duplicated.  If
18580 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 20  .** pFrom->z is 
18581 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d 3e  used, then pTo->
18582 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  z points to the 
18583 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 70 46  same thing as pF
18584 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66 6c  rom->z.** and fl
18585 61 67 73 20 67 65 74 73 20 73 72 63 54 79 70 65  ags gets srcType
18586 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 70 68   (either MEM_Eph
18587 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63  em or MEM_Static
18588 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
18589 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1858a 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43  3VdbeMemShallowC
1858b 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f  opy(Mem *pTo, co
1858c 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c 20  nst Mem *pFrom, 
1858d 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a 20 20  int srcType){.  
1858e 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e  assert( (pFrom->
1858f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
18590 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69  et)==0 );.  sqli
18591 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
18592 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a  eExternal(pTo);.
18593 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
18594 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45  rom, MEMCELLSIZE
18595 29 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c 20 3d  );.  pTo->xDel =
18596 20 30 3b 0a 20 20 69 66 28 20 28 70 46 72 6f 6d   0;.  if( (pFrom
18597 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29  ->flags&MEM_Dyn)
18598 21 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 7a 3d  !=0 || pFrom->z=
18599 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  =pFrom->zMalloc 
1859a 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67  ){.    pTo->flag
1859b 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e 7c 4d  s &= ~(MEM_Dyn|M
1859c 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70  EM_Static|MEM_Ep
1859d 68 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74  hem);.    assert
1859e 28 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 45  ( srcType==MEM_E
1859f 70 68 65 6d 20 7c 7c 20 73 72 63 54 79 70 65 3d  phem || srcType=
185a0 3d 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b 0a 20  =MEM_Static );. 
185a1 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d     pTo->flags |=
185a2 20 73 72 63 54 79 70 65 3b 0a 20 20 7d 0a 7d 0a   srcType;.  }.}.
185a3 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 66 75  ./*.** Make a fu
185a4 6c 6c 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d  ll copy of pFrom
185a5 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f   into pTo.  Prio
185a6 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 54  r contents of pT
185a7 6f 20 61 72 65 0a 2a 2a 20 66 72 65 65 64 20 62  o are.** freed b
185a8 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79 20 69  efore the copy i
185a9 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54  s made..*/.SQLIT
185aa 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
185ab 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79  lite3VdbeMemCopy
185ac 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74  (Mem *pTo, const
185ad 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20   Mem *pFrom){.  
185ae 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
185af 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 28  OK;..  assert( (
185b0 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d  pFrom->flags & M
185b1 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
185b2 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
185b3 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c  mReleaseExternal
185b4 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28  (pTo);.  memcpy(
185b5 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43  pTo, pFrom, MEMC
185b6 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d  ELLSIZE);.  pTo-
185b7 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44  >flags &= ~MEM_D
185b8 79 6e 3b 0a 0a 20 20 69 66 28 20 70 54 6f 2d 3e  yn;..  if( pTo->
185b9 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d  flags&(MEM_Str|M
185ba 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20  EM_Blob) ){.    
185bb 69 66 28 20 30 3d 3d 28 70 46 72 6f 6d 2d 3e 66  if( 0==(pFrom->f
185bc 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 29  lags&MEM_Static)
185bd 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 2d 3e 66   ){.      pTo->f
185be 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65  lags |= MEM_Ephe
185bf 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  m;.      rc = sq
185c0 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
185c1 57 72 69 74 65 61 62 6c 65 28 70 54 6f 29 3b 0a  Writeable(pTo);.
185c2 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
185c3 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
185c4 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 63 6f   Transfer the co
185c5 6e 74 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20  ntents of pFrom 
185c6 74 6f 20 70 54 6f 2e 20 41 6e 79 20 65 78 69 73  to pTo. Any exis
185c7 74 69 6e 67 20 76 61 6c 75 65 20 69 6e 20 70 54  ting value in pT
185c8 6f 20 69 73 0a 2a 2a 20 66 72 65 65 64 2e 20 49  o is.** freed. I
185c9 66 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73  f pFrom contains
185ca 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 2c   ephemeral data,
185cb 20 61 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e   a copy is made.
185cc 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d 20 63 6f 6e  .**.** pFrom con
185cd 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c  tains an SQL NUL
185ce 4c 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  L when this rout
185cf 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a  ine returns..*/.
185d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
185d1 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
185d2 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a 70 54 6f 2c  emMove(Mem *pTo,
185d3 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20   Mem *pFrom){.  
185d4 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 64  assert( pFrom->d
185d5 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
185d6 6d 75 74 65 78 5f 68 65 6c 64 28 70 46 72 6f 6d  mutex_held(pFrom
185d7 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
185d8 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 64    assert( pTo->d
185d9 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
185da 6d 75 74 65 78 5f 68 65 6c 64 28 70 54 6f 2d 3e  mutex_held(pTo->
185db 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
185dc 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 64  assert( pFrom->d
185dd 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d 3e 64 62 3d  b==0 || pTo->db=
185de 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 64 62 3d  =0 || pFrom->db=
185df 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a 0a 20 20 73  =pTo->db );..  s
185e0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
185e1 65 61 73 65 28 70 54 6f 29 3b 0a 20 20 6d 65 6d  ease(pTo);.  mem
185e2 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20  cpy(pTo, pFrom, 
185e3 73 69 7a 65 6f 66 28 4d 65 6d 29 29 3b 0a 20 20  sizeof(Mem));.  
185e4 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d  pFrom->flags = M
185e5 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70 46 72 6f 6d  EM_Null;.  pFrom
185e6 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 70 46  ->xDel = 0;.  pF
185e7 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30  rom->zMalloc = 0
185e8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
185e9 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  e the value of a
185ea 20 4d 65 6d 20 74 6f 20 62 65 20 61 20 73 74 72   Mem to be a str
185eb 69 6e 67 20 6f 72 20 61 20 42 4c 4f 42 2e 0a 2a  ing or a BLOB..*
185ec 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20  *.** The memory 
185ed 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 61 74  management strat
185ee 65 67 79 20 64 65 70 65 6e 64 73 20 6f 6e 20 74  egy depends on t
185ef 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
185f0 78 44 65 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65  xDel.** paramete
185f1 72 2e 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  r. If the value 
185f2 70 61 73 73 65 64 20 69 73 20 53 51 4c 49 54 45  passed is SQLITE
185f3 5f 54 52 41 4e 53 49 45 4e 54 2c 20 74 68 65 6e  _TRANSIENT, then
185f4 20 74 68 65 20 0a 2a 2a 20 73 74 72 69 6e 67 20   the .** string 
185f5 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 61  is copied into a
185f6 20 28 70 6f 73 73 69 62 6c 79 20 65 78 69 73 74   (possibly exist
185f7 69 6e 67 29 20 62 75 66 66 65 72 20 6d 61 6e 61  ing) buffer mana
185f8 67 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 4d  ged by the .** M
185f9 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 4f 74  em structure. Ot
185fa 68 65 72 77 69 73 65 2c 20 61 6e 79 20 65 78 69  herwise, any exi
185fb 73 74 69 6e 67 20 62 75 66 66 65 72 20 69 73 20  sting buffer is 
185fc 66 72 65 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a  freed and the.**
185fd 20 70 6f 69 6e 74 65 72 20 63 6f 70 69 65 64 2e   pointer copied.
185fe 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
185ff 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
18600 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 20 4d  beMemSetStr(.  M
18601 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 20 20  em *pMem,       
18602 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c     /* Memory cel
18603 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 72 69  l to set to stri
18604 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 6f  ng value */.  co
18605 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20 20  nst char *z,    
18606 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69 6e    /* String poin
18607 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20  ter */.  int n, 
18608 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18609 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67 2c  Bytes in string,
1860a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a   or negative */.
1860b 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20    u8 enc,       
1860c 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e        /* Encodin
1860d 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 42  g of z.  0 for B
1860e 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28  LOBs */.  void (
1860f 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f 2a  *xDel)(void*) /*
18610 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   Destructor func
18611 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  tion */.){.  int
18612 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20 20   nByte = n;     
18613 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66 6f   /* New value fo
18614 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 20 69  r pMem->n */.  i
18615 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20 20  nt iLimit;      
18616 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c     /* Maximum al
18617 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72 20  lowed string or 
18618 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 20 75  blob size */.  u
18619 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 20 20  16 flags = 0;   
1861a 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20     /* New value 
1861b 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  for pMem->flags 
1861c 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d  */..  assert( pM
1861d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
1861e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1861f 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
18620 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
18621 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
18622 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a  _RowSet)==0 );..
18623 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 4e    /* If z is a N
18624 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 74  ULL pointer, set
18625 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 6e   pMem to contain
18626 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a 2f   an SQL NULL. */
18627 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20  .  if( !z ){.   
18628 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
18629 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20  etNull(pMem);.  
1862a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1862b 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  OK;.  }..  if( p
1862c 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 69  Mem->db ){.    i
1862d 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 62  Limit = pMem->db
1862e 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1862f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20  LIMIT_LENGTH];. 
18630 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d   }else{.    iLim
18631 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f  it = SQLITE_MAX_
18632 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 6c  LENGTH;.  }.  fl
18633 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d 45  ags = (enc==0?ME
18634 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 3b  M_Blob:MEM_Str);
18635 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20 29  .  if( nByte<0 )
18636 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 6e  {.    assert( en
18637 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  c!=0 );.    if( 
18638 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc==SQLITE_UTF8
18639 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 42   ){.      for(nB
1863a 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 4c  yte=0; nByte<=iL
1863b 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 5d  imit && z[nByte]
1863c 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 20  ; nByte++){}.   
1863d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f   }else{.      fo
1863e 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65  r(nByte=0; nByte
1863f 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b 6e  <=iLimit && (z[n
18640 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 2b  Byte] | z[nByte+
18641 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b 7d  1]); nByte+=2){}
18642 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 73  .    }.    flags
18643 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20   |= MEM_Term;.  
18644 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  }..  /* The foll
18645 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 73  owing block sets
18646 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73 20   the new values 
18647 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 6d  of Mem.z and Mem
18648 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 61  .xDel. It.  ** a
18649 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67 20  lso sets a flag 
1864a 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  in local variabl
1864b 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e 64  e "flags" to ind
1864c 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79  icate the memory
1864d 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e 74  .  ** management
1864e 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 6e   (one of MEM_Dyn
1864f 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e   or MEM_Static).
18650 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 6c  .  */.  if( xDel
18651 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  ==SQLITE_TRANSIE
18652 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41  NT ){.    int nA
18653 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 20  lloc = nByte;.  
18654 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f    if( flags&MEM_
18655 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e 41  Term ){.      nA
18656 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 51  lloc += (enc==SQ
18657 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a  LITE_UTF8?1:2);.
18658 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 42      }.    if( nB
18659 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20  yte>iLimit ){.  
1865a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1865b 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d 0a  E_TOOBIG;.    }.
1865c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
1865d 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
1865e 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a 20   nAlloc, 0) ){. 
1865f 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
18660 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
18661 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d      memcpy(pMem-
18662 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b 0a  >z, z, nAlloc);.
18663 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65 6c    }else if( xDel
18664 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  ==SQLITE_DYNAMIC
18665 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
18666 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d  dbeMemRelease(pM
18667 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  em);.    pMem->z
18668 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a  Malloc = pMem->z
18669 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20   = (char *)z;.  
1866a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30    pMem->xDel = 0
1866b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
1866c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1866d 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
1866e 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20  pMem->z = (char 
1866f 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78  *)z;.    pMem->x
18670 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20  Del = xDel;.    
18671 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c 3d  flags |= ((xDel=
18672 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3f  =SQLITE_STATIC)?
18673 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f 44  MEM_Static:MEM_D
18674 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  yn);.  }.  if( n
18675 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20  Byte>iLimit ){. 
18676 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
18677 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20  _TOOBIG;.  }..  
18678 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b  pMem->n = nByte;
18679 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d  .  pMem->flags =
1867a 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e   flags;.  pMem->
1867b 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20  enc = (enc==0 ? 
1867c 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e  SQLITE_UTF8 : en
1867d 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65  c);.  pMem->type
1867e 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c   = (enc==0 ? SQL
1867f 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54  ITE_BLOB : SQLIT
18680 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65  E_TEXT);..#ifnde
18681 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
18682 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e  F16.  if( pMem->
18683 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc!=SQLITE_UTF8
18684 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d   && sqlite3VdbeM
18685 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d  emHandleBom(pMem
18686 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
18687 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
18688 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75  }.#endif..  retu
18689 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1868a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74  ./*.** Compare t
1868b 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 61 69  he values contai
1868c 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f 20 6d  ned by the two m
1868d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 65 74  emory cells, ret
1868e 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 74 69  urning.** negati
1868f 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69  ve, zero or posi
18690 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20 69 73  tive if pMem1 is
18691 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61   less than, equa
18692 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72  l to, or greater
18693 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e 20  .** than pMem2. 
18694 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 69 73  Sorting order is
18695 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c 20 66   NULL's first, f
18696 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62 65  ollowed by numbe
18697 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a 2a 20  rs (integers.** 
18698 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 74 65  and reals) sorte
18699 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20 66  d numerically, f
1869a 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 74 20  ollowed by text 
1869b 6f 72 64 65 72 65 64 20 62 79 20 74 68 65 20 63  ordered by the c
1869c 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75  ollating.** sequ
1869d 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20 66  ence pColl and f
1869e 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f 72  inally blob's or
1869f 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d 70 28  dered by memcmp(
186a0 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e 55 4c  )..**.** Two NUL
186a1 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e  L values are con
186a2 73 69 64 65 72 65 64 20 65 71 75 61 6c 20 62 79  sidered equal by
186a3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
186a4 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
186a5 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
186a6 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65  Compare(const Me
186a7 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 74 20  m *pMem1, const 
186a8 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f 6e 73  Mem *pMem2, cons
186a9 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c  t CollSeq *pColl
186aa 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
186ab 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 6e 74  nt f1, f2;.  int
186ac 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 3b   combined_flags;
186ad 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68 61 6e  ..  /* Interchan
186ae 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70 4d 65  ge pMem1 and pMe
186af 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74  m2 if the collat
186b0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70 65  ing sequence spe
186b1 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45 53 43  cifies.  ** DESC
186b2 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66   order..  */.  f
186b3 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67 73  1 = pMem1->flags
186b4 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d 3e  ;.  f2 = pMem2->
186b5 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69 6e 65  flags;.  combine
186b6 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 32 3b  d_flags = f1|f2;
186b7 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f 6d 62  .  assert( (comb
186b8 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d 45 4d  ined_flags & MEM
186b9 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
186ba 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c  .  /* If one val
186bb 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20 69  ue is NULL, it i
186bc 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
186bd 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 20 76  other. If both v
186be 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 20 4e  alues.  ** are N
186bf 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a 20  ULL, return 0.. 
186c0 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e   */.  if( combin
186c1 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c  ed_flags&MEM_Nul
186c2 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
186c3 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 2d 20  (f2&MEM_Null) - 
186c4 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20  (f1&MEM_Null);. 
186c5 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20   }..  /* If one 
186c6 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d 62 65  value is a numbe
186c7 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20  r and the other 
186c8 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 6d 62  is not, the numb
186c9 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a  er is less..  **
186ca 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e 75 6d   If both are num
186cb 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 61 73  bers, compare as
186cc 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 69 73   reals if one is
186cd 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 20 69   a real, or as i
186ce 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 66 20  ntegers.  ** if 
186cf 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 20  both values are 
186d0 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a 20  integers..  */. 
186d1 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c   if( combined_fl
186d2 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d  ags&(MEM_Int|MEM
186d3 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 69 66  _Real) ){.    if
186d4 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74 7c  ( !(f1&(MEM_Int|
186d5 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20  MEM_Real)) ){.  
186d6 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
186d7 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 66 32    }.    if( !(f2
186d8 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65  &(MEM_Int|MEM_Re
186d9 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65  al)) ){.      re
186da 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  turn -1;.    }. 
186db 20 20 20 69 66 28 20 28 66 31 20 26 20 66 32 20     if( (f1 & f2 
186dc 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b  & MEM_Int)==0 ){
186dd 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 31  .      double r1
186de 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 28 20  , r2;.      if( 
186df 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30  (f1&MEM_Real)==0
186e0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d   ){.        r1 =
186e1 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 31 2d 3e   (double)pMem1->
186e2 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  u.i;.      }else
186e3 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 70  {.        r1 = p
186e4 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20 20 7d  Mem1->r;.      }
186e5 0a 20 20 20 20 20 20 69 66 28 20 28 66 32 26 4d  .      if( (f2&M
186e6 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20  EM_Real)==0 ){. 
186e7 20 20 20 20 20 20 20 72 32 20 3d 20 28 64 6f 75         r2 = (dou
186e8 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 2e 69 3b 0a  ble)pMem2->u.i;.
186e9 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
186ea 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32 2d       r2 = pMem2-
186eb 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  >r;.      }.    
186ec 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72 65    if( r1<r2 ) re
186ed 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69  turn -1;.      i
186ee 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 75 72  f( r1>r2 ) retur
186ef 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  n 1;.      retur
186f0 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  n 0;.    }else{.
186f1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 31        assert( f1
186f2 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20  &MEM_Int );.    
186f3 20 20 61 73 73 65 72 74 28 20 66 32 26 4d 45 4d    assert( f2&MEM
186f4 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 69 66  _Int );.      if
186f5 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c 20 70  ( pMem1->u.i < p
186f6 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75  Mem2->u.i ) retu
186f7 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 66 28  rn -1;.      if(
186f8 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 70 4d   pMem1->u.i > pM
186f9 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75 72  em2->u.i ) retur
186fa 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  n 1;.      retur
186fb 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  n 0;.    }.  }..
186fc 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75    /* If one valu
186fd 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 61 6e  e is a string an
186fe 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20 61  d the other is a
186ff 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 69 6e   blob, the strin
18700 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a 20  g is less..  ** 
18701 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 72 69  If both are stri
18702 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 73 69  ngs, compare usi
18703 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  ng the collating
18704 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f   functions..  */
18705 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f  .  if( combined_
18706 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b  flags&MEM_Str ){
18707 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 4d  .    if( (f1 & M
18708 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20  EM_Str)==0 ){.  
18709 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1870a 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 32 20    }.    if( (f2 
1870b 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b  & MEM_Str)==0 ){
1870c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31  .      return -1
1870d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73  ;.    }..    ass
1870e 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d  ert( pMem1->enc=
1870f 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b 0a 20  =pMem2->enc );. 
18710 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31     assert( pMem1
18711 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
18712 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20  F8 || .         
18713 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53     pMem1->enc==S
18714 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c  QLITE_UTF16LE ||
18715 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c   pMem1->enc==SQL
18716 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 0a  ITE_UTF16BE );..
18717 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61      /* The colla
18718 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6d 75  tion sequence mu
18719 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 74  st be defined at
1871a 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 76 65   this point, eve
1871b 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 65 20  n if.    ** the 
1871c 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 68 65  user deletes the
1871d 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
1871e 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 76 64  nce after the vd
1871f 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a 20 20  be program is.  
18720 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28 74    ** compiled (t
18721 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c 77 61  his was not alwa
18722 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a 20 20  ys the case)..  
18723 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
18724 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c 6c   !pColl || pColl
18725 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 20 69  ->xCmp );..    i
18726 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
18727 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 6e 63    if( pMem1->enc
18728 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a  ==pColl->enc ){.
18729 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
1872a 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 65 61  trings are alrea
1872b 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63  dy in the correc
1872c 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 61 6c  t encoding.  Cal
1872d 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  l the.        **
1872e 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
1872f 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 2a 2f  tion directly */
18730 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
18731 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c  pColl->xCmp(pCol
18732 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 2d 3e  l->pUser,pMem1->
18733 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 6d 32  n,pMem1->z,pMem2
18734 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b 0a 20  ->n,pMem2->z);. 
18735 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
18736 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a      const void *
18737 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 20 20  v1, *v2;.       
18738 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 20 20   int n1, n2;.   
18739 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 20 20       Mem c1;.   
1873a 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 20 20       Mem c2;.   
1873b 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 31 2c       memset(&c1,
1873c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 29 3b   0, sizeof(c1));
1873d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
1873e 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63  &c2, 0, sizeof(c
1873f 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  2));.        sql
18740 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c  ite3VdbeMemShall
18741 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d 65 6d  owCopy(&c1, pMem
18742 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  1, MEM_Ephem);. 
18743 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
18744 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
18745 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45 4d  (&c2, pMem2, MEM
18746 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20  _Ephem);.       
18747 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61 6c   v1 = sqlite3Val
18748 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f  ueText((sqlite3_
18749 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f 6c  value*)&c1, pCol
1874a 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20  l->enc);.       
1874b 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30 20   n1 = v1==0 ? 0 
1874c 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 20 20  : c1.n;.        
1874d 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  v2 = sqlite3Valu
1874e 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f 76  eText((sqlite3_v
1874f 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f 6c 6c  alue*)&c2, pColl
18750 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  ->enc);.        
18751 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20 3a  n2 = v2==0 ? 0 :
18752 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 20 72   c2.n;.        r
18753 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28  c = pColl->xCmp(
18754 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e 31  pColl->pUser, n1
18755 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a 20  , v1, n2, v2);. 
18756 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
18757 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 31  beMemRelease(&c1
18758 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
18759 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1875a 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 20 72  (&c2);.        r
1875b 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
1875c 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 49  }.    }.    /* I
1875d 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  f a NULL pointer
1875e 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74   was passed as t
1875f 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63 74  he collate funct
18760 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  ion, fall throug
18761 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  h.    ** to the 
18762 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75 73  blob case and us
18763 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a  e memcmp().  */.
18764 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68 20    }. .  /* Both 
18765 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 62  values must be b
18766 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20 75  lobs.  Compare u
18767 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 20  sing memcmp().  
18768 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70  */.  rc = memcmp
18769 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d 32  (pMem1->z, pMem2
1876a 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e 70  ->z, (pMem1->n>p
1876b 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d 3e  Mem2->n)?pMem2->
1876c 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20 69  n:pMem1->n);.  i
1876d 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20  f( rc==0 ){.    
1876e 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d 20  rc = pMem1->n - 
1876f 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 20  pMem2->n;.  }.  
18770 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
18771 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f 75  .** Move data ou
18772 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65 79  t of a btree key
18773 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20 61   or data field a
18774 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73 74  nd into a Mem st
18775 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20  ructure..** The 
18776 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20 74  data or key is t
18777 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e  aken from the en
18778 74 72 79 20 74 68 61 74 20 70 43 75 72 20 69 73  try that pCur is
18779 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1877a 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66 73  ing.** to.  offs
1877b 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65 72  et and amt deter
1877c 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69 6f  mine what portio
1877d 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f 72  n of the data or
1877e 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76 65   key to retrieve
1877f 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75 65  ..** key is true
18780 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79 20   to get the key 
18781 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74 20  or false to get 
18782 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 75 6c  data.  The resul
18783 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  t is written.** 
18784 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65 6c  into the pMem el
18785 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ement..**.** The
18786 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20   pMem structure 
18787 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65  is assumed to be
18788 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20   uninitialized. 
18789 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74 65   Any prior conte
1878a 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72 69  nt.** is overwri
1878b 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65 69  tten without bei
1878c 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20  ng freed..**.** 
1878d 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
1878e 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65  fails for any re
1878f 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65 74  ason (malloc ret
18790 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e 61  urns NULL or una
18791 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20 66  ble.** to read f
18792 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74 68  rom the disk) th
18793 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20 6c  en the pMem is l
18794 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73  eft in an incons
18795 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2f  istent state..*/
18796 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18797 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
18798 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20 42  emFromBtree(.  B
18799 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20  tCursor *pCur,  
1879a 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74   /* Cursor point
1879b 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74 6f  ing at record to
1879c 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20 20   retrieve. */.  
1879d 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20 20  int offset,     
1879e 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d    /* Offset from
1879f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 64 61   the start of da
187a0 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79 74  ta to return byt
187a1 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e  es from. */.  in
187a2 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20  t amt,          
187a3 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
187a4 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a 2f  es to return. */
187a5 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20 20  .  int key,     
187a6 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c       /* If true,
187a7 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20 74   retrieve from t
187a8 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e 6f  he btree key, no
187a9 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65 6d  t data. */.  Mem
187aa 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 2f   *pMem         /
187ab 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64 61  * OUT: Return da
187ac 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20 73  ta in this Mem s
187ad 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b 0a  tructure. */.){.
187ae 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20    char *zData;  
187af 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72 6f       /* Data fro
187b0 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65  m the btree laye
187b1 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 6c  r */.  int avail
187b2 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75 6d  able = 0; /* Num
187b3 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 61  ber of bytes ava
187b4 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f  ilable on the lo
187b5 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 20 2a  cal btree page *
187b6 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
187b7 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
187b8 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
187b9 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
187ba 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20 73  TE_OK;..  db = s
187bb 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
187bc 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73 73  rDb(pCur);.  ass
187bd 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
187be 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
187bf 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
187c0 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d  (pMem->flags & M
187c1 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
187c2 0a 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20  .  if( key ){.  
187c3 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20    zData = (char 
187c4 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  *)sqlite3BtreeKe
187c5 79 46 65 74 63 68 28 70 43 75 72 2c 20 26 61 76  yFetch(pCur, &av
187c6 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73  ailable);.  }els
187c7 65 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28  e{.    zData = (
187c8 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74  char *)sqlite3Bt
187c9 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43 75  reeDataFetch(pCu
187ca 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a  r, &available);.
187cb 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 44    }.  assert( zD
187cc 61 74 61 21 3d 30 20 29 3b 0a 0a 20 20 69 66 28  ata!=0 );..  if(
187cd 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 61 76 61   offset+amt<=ava
187ce 69 6c 61 62 6c 65 20 26 26 20 28 28 70 4d 65 6d  ilable && ((pMem
187cf 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29  ->flags&MEM_Dyn)
187d0 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d 3e 78 44 65  ==0 || pMem->xDe
187d1 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  l) ){.    sqlite
187d2 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
187d3 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d  pMem);.    pMem-
187d4 3e 7a 20 3d 20 26 7a 44 61 74 61 5b 6f 66 66 73  >z = &zData[offs
187d5 65 74 5d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66  et];.    pMem->f
187d6 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c  lags = MEM_Blob|
187d7 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 6c  MEM_Ephem;.  }el
187d8 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  se if( SQLITE_OK
187d9 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56  ==(rc = sqlite3V
187da 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
187db 20 61 6d 74 2b 32 2c 20 30 29 29 20 29 7b 0a 20   amt+2, 0)) ){. 
187dc 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
187dd 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79   MEM_Blob|MEM_Dy
187de 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  n|MEM_Term;.    
187df 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20  pMem->enc = 0;. 
187e0 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
187e1 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 20  SQLITE_BLOB;.   
187e2 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20   if( key ){.    
187e3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
187e4 72 65 65 4b 65 79 28 70 43 75 72 2c 20 6f 66 66  reeKey(pCur, off
187e5 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e  set, amt, pMem->
187e6 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  z);.    }else{. 
187e7 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
187e8 33 42 74 72 65 65 44 61 74 61 28 70 43 75 72 2c  3BtreeData(pCur,
187e9 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d   offset, amt, pM
187ea 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  em->z);.    }.  
187eb 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d    pMem->z[amt] =
187ec 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b   0;.    pMem->z[
187ed 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20  amt+1] = 0;.    
187ee 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
187ef 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
187f0 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
187f1 28 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20  (pMem);.    }.  
187f2 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d  }.  pMem->n = am
187f3 74 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  t;..  return rc;
187f4 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63  .}../* This func
187f5 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 76 61  tion is only ava
187f6 69 6c 61 62 6c 65 20 69 6e 74 65 72 6e 61 6c 6c  ilable internall
187f7 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72  y, it is not par
187f8 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 74 65  t of the.** exte
187f9 72 6e 61 6c 20 41 50 49 2e 20 49 74 20 77 6f 72  rnal API. It wor
187fa 6b 73 20 69 6e 20 61 20 73 69 6d 69 6c 61 72 20  ks in a similar 
187fb 77 61 79 20 74 6f 20 73 71 6c 69 74 65 33 5f 76  way to sqlite3_v
187fc 61 6c 75 65 5f 74 65 78 74 28 29 2c 0a 2a 2a 20  alue_text(),.** 
187fd 65 78 63 65 70 74 20 74 68 65 20 64 61 74 61 20  except the data 
187fe 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e 20 74  returned is in t
187ff 68 65 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63  he encoding spec
18800 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63  ified by the sec
18801 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ond.** parameter
18802 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62 65 20  , which must be 
18803 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54  one of SQLITE_UT
18804 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54  F16BE, SQLITE_UT
18805 46 31 36 4c 45 20 6f 72 0a 2a 2a 20 53 51 4c 49  F16LE or.** SQLI
18806 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28  TE_UTF8..**.** (
18807 32 30 30 36 2d 30 32 2d 31 36 3a 29 20 20 54 68  2006-02-16:)  Th
18808 65 20 65 6e 63 20 76 61 6c 75 65 20 63 61 6e 20  e enc value can 
18809 62 65 20 6f 72 2d 65 64 20 77 69 74 68 20 53 51  be or-ed with SQ
1880a 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
1880b 45 44 2e 0a 2a 2a 20 49 66 20 74 68 61 74 20 69  ED..** If that i
1880c 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
1880d 20 74 68 65 20 72 65 73 75 6c 74 20 6d 75 73 74   the result must
1880e 20 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61   be aligned on a
1880f 6e 20 65 76 65 6e 20 62 79 74 65 0a 2a 2a 20 62  n even byte.** b
18810 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49  oundary..*/.SQLI
18811 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
18812 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61   void *sqlite3Va
18813 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f  lueText(sqlite3_
18814 76 61 6c 75 65 2a 20 70 56 61 6c 2c 20 75 38 20  value* pVal, u8 
18815 65 6e 63 29 7b 0a 20 20 69 66 28 20 21 70 56 61  enc){.  if( !pVa
18816 6c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  l ) return 0;.. 
18817 20 61 73 73 65 72 74 28 20 70 56 61 6c 2d 3e 64   assert( pVal->d
18818 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
18819 6d 75 74 65 78 5f 68 65 6c 64 28 70 56 61 6c 2d  mutex_held(pVal-
1881a 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
1881b 20 61 73 73 65 72 74 28 20 28 65 6e 63 26 33 29   assert( (enc&3)
1881c 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 54 45 5f 55  ==(enc&~SQLITE_U
1881d 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 3b  TF16_ALIGNED) );
1881e 0a 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c  .  assert( (pVal
1881f 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
18820 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69  wSet)==0 );..  i
18821 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d  f( pVal->flags&M
18822 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72  EM_Null ){.    r
18823 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61  eturn 0;.  }.  a
18824 73 73 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f 62  ssert( (MEM_Blob
18825 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 20  >>3) == MEM_Str 
18826 29 3b 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67 73  );.  pVal->flags
18827 20 7c 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73   |= (pVal->flags
18828 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b   & MEM_Blob)>>3;
18829 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 56  .  expandBlob(pV
1882a 61 6c 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d  al);.  if( pVal-
1882b 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29  >flags&MEM_Str )
1882c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1882d 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1882e 70 56 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51 4c  pVal, enc & ~SQL
1882f 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45  ITE_UTF16_ALIGNE
18830 44 29 3b 0a 20 20 20 20 69 66 28 20 28 65 6e 63  D);.    if( (enc
18831 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f   & SQLITE_UTF16_
18832 41 4c 49 47 4e 45 44 29 21 3d 30 20 26 26 20 31  ALIGNED)!=0 && 1
18833 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f  ==(1&SQLITE_PTR_
18834 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29  TO_INT(pVal->z))
18835 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
18836 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26  ( (pVal->flags &
18837 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f   (MEM_Ephem|MEM_
18838 53 74 61 74 69 63 29 29 21 3d 30 20 29 3b 0a 20  Static))!=0 );. 
18839 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1883a 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
1883b 61 62 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c 49  able(pVal)!=SQLI
1883c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1883d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
1883e 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
1883f 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
18840 72 6d 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20  rminate(pVal);. 
18841 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
18842 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73  rt( (pVal->flags
18843 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b  &MEM_Blob)==0 );
18844 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
18845 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 56 61  MemStringify(pVa
18846 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 61 73 73  l, enc);.    ass
18847 65 72 74 28 20 30 3d 3d 28 31 26 53 51 4c 49 54  ert( 0==(1&SQLIT
18848 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 61  E_PTR_TO_INT(pVa
18849 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20 7d 0a 20 20  l->z)) );.  }.  
1884a 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63  assert(pVal->enc
1884b 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45  ==(enc & ~SQLITE
1884c 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20  _UTF16_ALIGNED) 
1884d 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20  || pVal->db==0. 
1884e 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
1884f 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  pVal->db->malloc
18850 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
18851 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20  pVal->enc==(enc 
18852 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  & ~SQLITE_UTF16_
18853 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20  ALIGNED) ){.    
18854 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a  return pVal->z;.
18855 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
18856 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 0;.  }.}../*
18857 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
18858 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
18859 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  bject..*/.SQLITE
1885a 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
1885b 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56  _value *sqlite3V
1885c 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20  alueNew(sqlite3 
1885d 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d  *db){.  Mem *p =
1885e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1885f 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
18860 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  *p));.  if( p ){
18861 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20  .    p->flags = 
18862 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d  MEM_Null;.    p-
18863 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e  >type = SQLITE_N
18864 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d  ULL;.    p->db =
18865 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   db;.  }.  retur
18866 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  n p;.}../*.** Cr
18867 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74  eate a new sqlit
18868 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c  e3_value object,
18869 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1886a 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a  value of pExpr..
1886b 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20  **.** This only 
1886c 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73  works for very s
1886d 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e  imple expression
1886e 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f  s that consist o
1886f 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a  f one constant.*
18870 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35  * token (i.e. "5
18871 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74  ", "5.1", "'a st
18872 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20  ring'"). If the 
18873 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a  expression can.*
18874 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64  * be converted d
18875 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76  irectly into a v
18876 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76  alue, then the v
18877 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  alue is allocate
18878 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74  d and.** a point
18879 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70  er written to *p
1887a 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72  pVal. The caller
1887b 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
1887c 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67  for deallocating
1887d 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79  .** the value by
1887e 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73   passing it to s
1887f 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
18880 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74  ) later on. If t
18881 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
18882 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65   cannot be conve
18883 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c  rted to a value,
18884 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20   then *ppVal is 
18885 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  set to NULL..*/.
18886 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18887 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  nt sqlite3ValueF
18888 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74  romExpr(.  sqlit
18889 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
1888a 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1888b 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1888c 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
1888d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1888e 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
1888f 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a   to evaluate */.
18890 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20    u8 enc,       
18891 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
18892 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a  ncoding to use *
18893 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c  /.  u8 affinity,
18894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18895 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65   Affinity to use
18896 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61   */.  sqlite3_va
18897 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20  lue **ppVal     
18898 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77  /* Write the new
18899 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29   value here */.)
1889a 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68  {.  int op;.  ch
1889b 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20  ar *zVal = 0;.  
1889c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1889d 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  Val = 0;..  if( 
1889e 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70  !pExpr ){.    *p
1889f 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65  pVal = 0;.    re
188a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
188a1 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72    }.  op = pExpr
188a2 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70 3d  ->op;..  if( op=
188a3 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70  =TK_STRING || op
188a4 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70  ==TK_FLOAT || op
188a5 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a  ==TK_INTEGER ){.
188a6 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74      zVal = sqlit
188a7 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
188a8 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f  (char*)pExpr->to
188a9 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f  ken.z, pExpr->to
188aa 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61 6c  ken.n);.    pVal
188ab 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e   = sqlite3ValueN
188ac 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20  ew(db);.    if( 
188ad 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 29  !zVal || !pVal )
188ae 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
188af 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65    sqlite3Dequote
188b0 28 7a 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69  (zVal);.    sqli
188b1 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70  te3ValueSetStr(p
188b2 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53  Val, -1, zVal, S
188b3 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
188b4 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  TE_DYNAMIC);.   
188b5 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54   if( (op==TK_INT
188b6 45 47 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46  EGER || op==TK_F
188b7 4c 4f 41 54 20 29 20 26 26 20 61 66 66 69 6e 69  LOAT ) && affini
188b8 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ty==SQLITE_AFF_N
188b9 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ONE ){.      sql
188ba 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66  ite3ValueApplyAf
188bb 66 69 6e 69 74 79 28 70 56 61 6c 2c 20 53 51 4c  finity(pVal, SQL
188bc 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c  ITE_AFF_NUMERIC,
188bd 20 65 6e 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65   enc);.    }else
188be 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
188bf 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74  alueApplyAffinit
188c0 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79  y(pVal, affinity
188c1 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20  , enc);.    }.  
188c2 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b  }else if( op==TK
188c3 5f 55 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20  _UMINUS ) {.    
188c4 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73  if( SQLITE_OK==s
188c5 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45  qlite3ValueFromE
188c6 78 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c  xpr(db,pExpr->pL
188c7 65 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79  eft,enc,affinity
188c8 2c 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20  ,&pVal) ){.     
188c9 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20   pVal->u.i = -1 
188ca 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20  * pVal->u.i;.   
188cb 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 2d 31     /* (double)-1
188cc 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
188cd 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
188ce 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20  _POINT... */.   
188cf 20 20 20 70 56 61 6c 2d 3e 72 20 3d 20 28 64 6f     pVal->r = (do
188d0 75 62 6c 65 29 2d 31 20 2a 20 70 56 61 6c 2d 3e  uble)-1 * pVal->
188d1 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66  r;.    }.  }.#if
188d2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
188d3 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20  _BLOB_LITERAL.  
188d4 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  else if( op==TK_
188d5 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69 6e 74 20  BLOB ){.    int 
188d6 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73 65 72 74  nVal;.    assert
188d7 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  ( pExpr->token.n
188d8 3e 3d 33 20 29 3b 0a 20 20 20 20 61 73 73 65 72  >=3 );.    asser
188d9 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  t( pExpr->token.
188da 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78  z[0]=='x' || pEx
188db 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d  pr->token.z[0]==
188dc 27 58 27 20 29 3b 0a 20 20 20 20 61 73 73 65 72  'X' );.    asser
188dd 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  t( pExpr->token.
188de 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20  z[1]=='\'' );.  
188df 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
188e0 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78 70 72 2d 3e  >token.z[pExpr->
188e1 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d 27 5c 27 27  token.n-1]=='\''
188e2 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73   );.    pVal = s
188e3 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64  qlite3ValueNew(d
188e4 62 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 61  b);.    if( !pVa
188e5 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  l ) goto no_mem;
188e6 0a 20 20 20 20 6e 56 61 6c 20 3d 20 70 45 78 70  .    nVal = pExp
188e7 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20 33 3b 0a  r->token.n - 3;.
188e8 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 68 61 72      zVal = (char
188e9 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
188ea 20 2b 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65   + 2;.    sqlite
188eb 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
188ec 56 61 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 54  Val, sqlite3HexT
188ed 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c 20  oBlob(db, zVal, 
188ee 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a 20  nVal), nVal/2,. 
188ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
188f0 20 20 20 20 20 20 20 20 30 2c 20 53 51 4c 49 54          0, SQLIT
188f1 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a  E_DYNAMIC);.  }.
188f2 23 65 6e 64 69 66 0a 0a 20 20 2a 70 70 56 61 6c  #endif..  *ppVal
188f3 20 3d 20 70 56 61 6c 3b 0a 20 20 72 65 74 75 72   = pVal;.  retur
188f4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f  n SQLITE_OK;..no
188f5 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c  _mem:.  db->mall
188f6 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
188f7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
188f8 2c 20 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74  , zVal);.  sqlit
188f9 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c  e3ValueFree(pVal
188fa 29 3b 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b  );.  *ppVal = 0;
188fb 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
188fc 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _NOMEM;.}../*.**
188fd 20 43 68 61 6e 67 65 20 74 68 65 20 73 74 72 69   Change the stri
188fe 6e 67 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73  ng value of an s
188ff 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
18900 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ect.*/.SQLITE_PR
18901 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
18902 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20  e3ValueSetStr(. 
18903 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
18904 76 2c 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20  v,     /* Value 
18905 74 6f 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69  to be set */.  i
18906 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20  nt n,           
18907 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
18908 66 20 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20  f string z */.  
18909 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20  const void *z,  
1890a 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66        /* Text of
1890b 20 74 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20   the new string 
1890c 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20  */.  u8 enc,    
1890d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
1890e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f  coding to use */
1890f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
18910 76 6f 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74  void*)   /* Dest
18911 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73  ructor for the s
18912 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66  tring */.){.  if
18913 28 20 76 20 29 20 73 71 6c 69 74 65 33 56 64 62  ( v ) sqlite3Vdb
18914 65 4d 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20  eMemSetStr((Mem 
18915 2a 29 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20  *)v, z, n, enc, 
18916 78 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  xDel);.}../*.** 
18917 46 72 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f  Free an sqlite3_
18918 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a  value object.*/.
18919 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1891a 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
1891b 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  Free(sqlite3_val
1891c 75 65 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76  ue *v){.  if( !v
1891d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c   ) return;.  sql
1891e 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1891f 73 65 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20  se((Mem *)v);.  
18920 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 28 28  sqlite3DbFree(((
18921 4d 65 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 29 3b  Mem*)v)->db, v);
18922 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
18923 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
18924 79 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69  ytes in the sqli
18925 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
18926 20 61 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61   assuming.** tha
18927 74 20 69 74 20 75 73 65 73 20 74 68 65 20 65 6e  t it uses the en
18928 63 6f 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a  coding "enc".*/.
18929 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1892a 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42  nt sqlite3ValueB
1892b 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c  ytes(sqlite3_val
1892c 75 65 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63  ue *pVal, u8 enc
1892d 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d  ){.  Mem *p = (M
1892e 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20  em*)pVal;.  if( 
1892f 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  (p->flags & MEM_
18930 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69  Blob)!=0 || sqli
18931 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
18932 6c 2c 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69  l, enc) ){.    i
18933 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45  f( p->flags & ME
18934 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
18935 72 65 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 70 2d  return p->n + p-
18936 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65  >u.nZero;.    }e
18937 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
18938 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20  n p->n;.    }.  
18939 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1893a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1893b 20 45 6e 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e   End of vdbemem.
1893c 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1893d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1893e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1893f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
18940 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
18941 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  aux.c **********
18942 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18943 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
18944 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74  ./*.** 2003 Sept
18945 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68  ember 6.**.** Th
18946 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
18947 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
18948 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
18949 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1894a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1894b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1894c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1894d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1894e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1894f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
18950 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
18951 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
18952 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
18953 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
18954 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
18955 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
18956 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
18957 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18958 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18959 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1895a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1895b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
1895c 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
1895d 65 20 75 73 65 64 20 66 6f 72 20 63 72 65 61 74  e used for creat
1895e 69 6e 67 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c  ing, destroying,
1895f 20 61 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a   and populating.
18960 2a 2a 20 61 20 56 44 42 45 20 28 6f 72 20 61 6e  ** a VDBE (or an
18961 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20   "sqlite3_stmt" 
18962 61 73 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74  as it is known t
18963 6f 20 74 68 65 20 6f 75 74 73 69 64 65 20 77 6f  o the outside wo
18964 72 6c 64 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20  rld.)  Prior.** 
18965 74 6f 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37  to version 2.8.7
18966 2c 20 61 6c 6c 20 74 68 69 73 20 63 6f 64 65 20  , all this code 
18967 77 61 73 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74  was combined int
18968 6f 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75  o the vdbe.c sou
18969 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74  rce file..** But
1896a 20 74 68 61 74 20 66 69 6c 65 20 77 61 73 20 67   that file was g
1896b 65 74 74 69 6e 67 20 74 6f 6f 20 62 69 67 20 73  etting too big s
1896c 6f 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e  o this subroutin
1896d 65 73 20 77 65 72 65 20 73 70 6c 69 74 20 6f 75  es were split ou
1896e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64  t..**.** $Id: vd
1896f 62 65 61 75 78 2e 63 2c 76 20 31 2e 34 34 36 20  beaux.c,v 1.446 
18970 32 30 30 39 2f 30 33 2f 32 35 20 31 35 3a 34 33  2009/03/25 15:43
18971 3a 30 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :09 danielk1977 
18972 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a  Exp $.*/..../*.*
18973 2a 20 57 68 65 6e 20 64 65 62 75 67 67 69 6e 67  * When debugging
18974 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
18975 74 6f 72 20 69 6e 20 61 20 73 79 6d 62 6f 6c 69  tor in a symboli
18976 63 20 64 65 62 75 67 67 65 72 2c 20 6f 6e 65 20  c debugger, one 
18977 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68 65 20 73  can.** set the s
18978 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54  qlite3VdbeAddopT
18979 72 61 63 65 20 74 6f 20 31 20 61 6e 64 20 61 6c  race to 1 and al
1897a 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c 6c 20 62  l opcodes will b
1897b 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20 61 73 20  e printed.** as 
1897c 74 68 65 79 20 61 72 65 20 61 64 64 65 64 20 74  they are added t
1897d 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f  o the instructio
1897e 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66  n stream..*/.#if
1897f 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
18980 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18981 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
18982 64 64 6f 70 54 72 61 63 65 20 3d 20 30 3b 0a 23  ddopTrace = 0;.#
18983 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72  endif.../*.** Cr
18984 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75  eate a new virtu
18985 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  al database engi
18986 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ne..*/.SQLITE_PR
18987 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69  IVATE Vdbe *sqli
18988 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71  te3VdbeCreate(sq
18989 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64  lite3 *db){.  Vd
1898a 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  be *p;.  p = sql
1898b 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
1898c 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 64 62 65  (db, sizeof(Vdbe
1898d 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  ) );.  if( p==0 
1898e 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 2d  ) return 0;.  p-
1898f 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20  >db = db;.  if( 
18990 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20  db->pVdbe ){.   
18991 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70 50 72 65   db->pVdbe->pPre
18992 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e  v = p;.  }.  p->
18993 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70 56 64 62  pNext = db->pVdb
18994 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76 20 3d 20  e;.  p->pPrev = 
18995 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62 65 20 3d  0;.  db->pVdbe =
18996 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d   p;.  p->magic =
18997 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54   VDBE_MAGIC_INIT
18998 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
18999 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20  ./*.** Remember 
1899a 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 66  the SQL string f
1899b 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74  or a prepared st
1899c 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
1899d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1899e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71  sqlite3VdbeSetSq
1899f 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74  l(Vdbe *p, const
189a0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
189a1 20 69 6e 74 20 69 73 50 72 65 70 61 72 65 56 32   int isPrepareV2
189a2 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  ){.  if( p==0 ) 
189a3 72 65 74 75 72 6e 3b 0a 23 69 66 64 65 66 20 53  return;.#ifdef S
189a4 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
189a5 0a 20 20 69 66 28 20 21 69 73 50 72 65 70 61 72  .  if( !isPrepar
189a6 65 56 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 65  eV2 ) return;.#e
189a7 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70  ndif.  assert( p
189a8 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70  ->zSql==0 );.  p
189a9 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33  ->zSql = sqlite3
189aa 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c  DbStrNDup(p->db,
189ab 20 7a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 50   z, n);.  p->isP
189ac 72 65 70 61 72 65 56 32 20 3d 20 69 73 50 72 65  repareV2 = isPre
189ad 70 61 72 65 56 32 20 3f 20 31 20 3a 20 30 3b 0a  pareV2 ? 1 : 0;.
189ae 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
189af 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74  the SQL associat
189b0 65 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72  ed with a prepar
189b1 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a  ed statement.*/.
189b2 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
189b3 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73   char *sqlite3_s
189b4 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ql(sqlite3_stmt 
189b5 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
189b6 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
189b7 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  mt;.  return (p-
189b8 3e 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 70  >isPrepareV2 ? p
189b9 2d 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a  ->zSql : 0);.}..
189ba 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63  /*.** Swap all c
189bb 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74  ontent between t
189bc 77 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72  wo VDBE structur
189bd 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
189be 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
189bf 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20  e3VdbeSwap(Vdbe 
189c0 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a  *pA, Vdbe *pB){.
189c1 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d    Vdbe tmp, *pTm
189c2 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b  p;.  char *zTmp;
189c3 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20  .  tmp = *pA;.  
189c4 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42  *pA = *pB;.  *pB
189c5 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d   = tmp;.  pTmp =
189c6 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41   pA->pNext;.  pA
189c7 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e  ->pNext = pB->pN
189c8 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74  ext;.  pB->pNext
189c9 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20   = pTmp;.  pTmp 
189ca 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70  = pA->pPrev;.  p
189cb 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70  A->pPrev = pB->p
189cc 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65  Prev;.  pB->pPre
189cd 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70  v = pTmp;.  zTmp
189ce 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70   = pA->zSql;.  p
189cf 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53  A->zSql = pB->zS
189d0 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d  ql;.  pB->zSql =
189d1 20 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50   zTmp;.  pB->isP
189d2 72 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69  repareV2 = pA->i
189d3 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23  sPrepareV2;.}..#
189d4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
189d5 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72  UG./*.** Turn tr
189d6 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a  acing on or off.
189d7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
189d8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
189d9 62 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c  beTrace(Vdbe *p,
189da 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20   FILE *trace){. 
189db 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63   p->trace = trac
189dc 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e;.}.#endif../*.
189dd 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64  ** Resize the Vd
189de 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20  be.aOp array so 
189df 74 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65  that it is at le
189e0 61 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65  ast one op large
189e1 72 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61  r than .** it wa
189e2 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f  s..**.** If an o
189e3 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72  ut-of-memory err
189e4 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
189e5 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72  resizing the arr
189e6 61 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  ay, return.** SQ
189e7 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74  LITE_NOMEM. In t
189e8 68 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f  his case Vdbe.aO
189e9 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c  p and Vdbe.nOpAl
189ea 6c 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75  loc remain .** u
189eb 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69  nchanged (this i
189ec 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70  s so that any op
189ed 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c  codes already al
189ee 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a  located can be .
189ef 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61  ** correctly dea
189f0 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77  llocated along w
189f1 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20  ith the rest of 
189f2 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74  the Vdbe)..*/.st
189f3 61 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41  atic int growOpA
189f4 72 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20  rray(Vdbe *p){. 
189f5 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20   VdbeOp *pNew;. 
189f6 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e   int nNew = (p->
189f7 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f  nOpAlloc ? p->nO
189f8 70 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29  pAlloc*2 : (int)
189f9 28 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29  (1024/sizeof(Op)
189fa 29 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  ));.  pNew = sql
189fb 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d  ite3DbRealloc(p-
189fc 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65  >db, p->aOp, nNe
189fd 77 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20  w*sizeof(Op));. 
189fe 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
189ff 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73   p->nOpAlloc = s
18a00 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
18a01 7a 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f  ze(p->db, pNew)/
18a02 73 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20  sizeof(Op);.    
18a03 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20  p->aOp = pNew;. 
18a04 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65   }.  return (pNe
18a05 77 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  w ? SQLITE_OK : 
18a06 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d  SQLITE_NOMEM);.}
18a07 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
18a08 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  w instruction to
18a09 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73   the list of ins
18a0a 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  tructions curren
18a0b 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45  t in the.** VDBE
18a0c 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64  .  Return the ad
18a0d 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77  dress of the new
18a0e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
18a0f 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a  .** Parameters:.
18a10 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20  **.**    p      
18a11 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72           Pointer
18a12 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a   to the VDBE.**.
18a13 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20  **    op        
18a14 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65        The opcode
18a15 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75   for this instru
18a16 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70  ction.**.**    p
18a17 31 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f  1, p2, p3      O
18a18 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73  perands.**.** Us
18a19 65 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62  e the sqlite3Vdb
18a1a 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20  eResolveLabel() 
18a1b 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20  function to fix 
18a1c 61 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a  an address and.*
18a1d 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62  * the sqlite3Vdb
18a1e 65 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63  eChangeP4() func
18a1f 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74  tion to change t
18a20 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
18a21 50 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a  P4.** operand..*
18a22 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18a23 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
18a24 41 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20  AddOp3(Vdbe *p, 
18a25 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20  int op, int p1, 
18a26 69 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b  int p2, int p3){
18a27 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65  .  int i;.  Vdbe
18a28 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20  Op *pOp;..  i = 
18a29 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74  p->nOp;.  assert
18a2a 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
18a2b 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
18a2c 20 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26   assert( op>0 &&
18a2d 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66   op<0xff );.  if
18a2e 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69  ( p->nOpAlloc<=i
18a2f 20 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77   ){.    if( grow
18a30 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20  OpArray(p) ){.  
18a31 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
18a32 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70    }.  }.  p->nOp
18a33 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e  ++;.  pOp = &p->
18a34 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f  aOp[i];.  pOp->o
18a35 70 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a  pcode = (u8)op;.
18a36 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20    pOp->p5 = 0;. 
18a37 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20   pOp->p1 = p1;. 
18a38 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20   pOp->p2 = p2;. 
18a39 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20   pOp->p3 = p3;. 
18a3a 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a   pOp->p4.p = 0;.
18a3b 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
18a3c 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d  P4_NOTUSED;.  p-
18a3d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23 69  >expired = 0;.#i
18a3e 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
18a3f 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e  G.  pOp->zCommen
18a40 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c  t = 0;.  if( sql
18a41 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61  ite3VdbeAddopTra
18a42 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65  ce ) sqlite3Vdbe
18a43 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70  PrintOp(0, i, &p
18a44 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69  ->aOp[i]);.#endi
18a45 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  f.#ifdef VDBE_PR
18a46 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63  OFILE.  pOp->cyc
18a47 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e  les = 0;.  pOp->
18a48 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  cnt = 0;.#endif.
18a49 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51    return i;.}.SQ
18a4a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18a4b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
18a4c 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  p0(Vdbe *p, int 
18a4d 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  op){.  return sq
18a4e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
18a4f 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b  p, op, 0, 0, 0);
18a50 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
18a51 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
18a52 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70 2c  eAddOp1(Vdbe *p,
18a53 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 29   int op, int p1)
18a54 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
18a55 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
18a56 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d  op, p1, 0, 0);.}
18a57 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18a58 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
18a59 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20 69  ddOp2(Vdbe *p, i
18a5a 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69  nt op, int p1, i
18a5b 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72 6e  nt p2){.  return
18a5c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
18a5d 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32  p3(p, op, p1, p2
18a5e 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  , 0);.}.../*.** 
18a5f 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68  Add an opcode th
18a60 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20  at includes the 
18a61 70 34 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f  p4 value as a po
18a62 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
18a63 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
18a64 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a  ite3VdbeAddOp4(.
18a65 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20    Vdbe *p,      
18a66 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65        /* Add the
18a67 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73 20   opcode to this 
18a68 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20  VM */.  int op, 
18a69 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
18a6a 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f  he new opcode */
18a6b 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20  .  int p1,      
18a6c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31         /* The P1
18a6d 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
18a6e 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20  t p2,           
18a6f 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72    /* The P2 oper
18a70 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c  and */.  int p3,
18a71 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18a72 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 2a  The P3 operand *
18a73 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
18a74 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20 50  zP4,    /* The P
18a75 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  4 operand */.  i
18a76 6e 74 20 70 34 74 79 70 65 20 20 20 20 20 20 20  nt p4type       
18a77 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64     /* P4 operand
18a78 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   type */.){.  in
18a79 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33  t addr = sqlite3
18a7a 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70  VdbeAddOp3(p, op
18a7b 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20  , p1, p2, p3);. 
18a7c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
18a7d 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 50  geP4(p, addr, zP
18a7e 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72 65  4, p4type);.  re
18a7f 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a  turn addr;.}../*
18a80 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
18a81 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20   symbolic label 
18a82 66 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69  for an instructi
18a83 6f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20  on that has yet 
18a84 74 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20  to be.** coded. 
18a85 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61   The symbolic la
18a86 62 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75  bel is really ju
18a87 73 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75  st a negative nu
18a88 6d 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61  mber.  The.** la
18a89 62 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20  bel can be used 
18a8a 61 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20  as the P2 value 
18a8b 6f 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e  of an operation.
18a8c 20 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a    Later, when.**
18a8d 20 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65   the label is re
18a8e 73 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63  solved to a spec
18a8f 69 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68  ific address, th
18a90 65 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e  e VDBE will scan
18a91 0a 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20  .** through its 
18a92 6f 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61  operation list a
18a93 6e 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61  nd change all va
18a94 6c 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68  lues of P2 which
18a95 20 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61   match.** the la
18a96 62 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73  bel into the res
18a97 6f 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a  olved address..*
18a98 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e  *.** The VDBE kn
18a99 6f 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61  ows that a P2 va
18a9a 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62  lue is a label b
18a9b 65 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72  ecause labels ar
18a9c 65 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61  e.** always nega
18a9d 74 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75  tive and P2 valu
18a9e 65 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74  es are suppose t
18a9f 6f 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  o be non-negativ
18aa0 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e  e..** Hence, a n
18aa1 65 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65  egative P2 value
18aa2 20 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74   is a label that
18aa3 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72   has yet to be r
18aa4 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a  esolved..**.** Z
18aa5 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20  ero is returned 
18aa6 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
18aa7 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
18aa8 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18aa9 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
18aaa 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
18aab 69 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62  i;.  i = p->nLab
18aac 65 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20  el++;.  assert( 
18aad 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
18aae 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
18aaf 66 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41  f( i>=p->nLabelA
18ab0 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20  lloc ){.    int 
18ab1 6e 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c  n = p->nLabelAll
18ab2 6f 63 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d  oc*2 + 5;.    p-
18ab3 3e 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65  >aLabel = sqlite
18ab4 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
18ab5 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65  (p->db, p->aLabe
18ab6 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
18ab7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18ab8 20 20 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65            n*size
18ab9 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29  of(p->aLabel[0])
18aba 29 3b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c  );.    p->nLabel
18abb 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  Alloc = sqlite3D
18abc 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64  bMallocSize(p->d
18abd 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69  b, p->aLabel)/si
18abe 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30  zeof(p->aLabel[0
18abf 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ]);.  }.  if( p-
18ac0 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70  >aLabel ){.    p
18ac1 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31  ->aLabel[i] = -1
18ac2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d  ;.  }.  return -
18ac3 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  1-i;.}../*.** Re
18ac4 73 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20  solve label "x" 
18ac5 74 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73  to be the addres
18ac6 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e  s of the next in
18ac7 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20  struction to.** 
18ac8 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68  be inserted.  Th
18ac9 65 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20  e parameter "x" 
18aca 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f  must have been o
18acb 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
18acc 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
18acd 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
18ace 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  abel()..*/.SQLIT
18acf 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
18ad0 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
18ad1 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20  eLabel(Vdbe *p, 
18ad2 69 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20  int x){.  int j 
18ad3 3d 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74  = -1-x;.  assert
18ad4 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
18ad5 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
18ad6 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26   assert( j>=0 &&
18ad7 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a   j<p->nLabel );.
18ad8 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20    if( p->aLabel 
18ad9 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c  ){.    p->aLabel
18ada 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20  [j] = p->nOp;.  
18adb 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20  }.}../*.** Loop 
18adc 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67  through the prog
18add 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  ram looking for 
18ade 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61  P2 values that a
18adf 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f  re negative.** o
18ae0 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69  n jump instructi
18ae1 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20  ons.  Each such 
18ae2 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c  value is a label
18ae3 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a  .  Resolve the.*
18ae4 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69  * label by setti
18ae5 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20  ng the P2 value 
18ae6 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e  to its correct n
18ae7 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a  on-zero value..*
18ae8 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
18ae9 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65  e is called once
18aea 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64   after all opcod
18aeb 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73  es have been ins
18aec 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72  erted..**.** Var
18aed 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41  iable *pMaxFuncA
18aee 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68  rgs is set to th
18aef 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20  e maximum value 
18af0 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65  of any P2 argume
18af1 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f  nt .** to an OP_
18af2 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67  Function, OP_Agg
18af3 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74  Step or OP_VFilt
18af4 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20  er opcode. This 
18af5 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73  is used by .** s
18af6 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65  qlite3VdbeMakeRe
18af7 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68  ady() to size th
18af8 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61  e Vdbe.apArg[] a
18af9 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rray..**.** This
18afa 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f   routine also do
18afb 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
18afc 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20   optimization:  
18afd 49 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20  It scans for.** 
18afe 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61  instructions tha
18aff 74 20 6d 69 67 68 74 20 63 61 75 73 65 20 61 20  t might cause a 
18b00 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61  statement rollba
18b01 63 6b 2e 20 20 53 75 63 68 20 69 6e 73 74 72 75  ck.  Such instru
18b02 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 3a 0a 2a  ctions.** are:.*
18b03 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c  *.**   *  OP_Hal
18b04 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45  t with P1=SQLITE
18b05 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20  _CONSTRAINT and 
18b06 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20  P2=OE_Abort..** 
18b07 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a    *  OP_Destroy.
18b08 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61  **   *  OP_VUpda
18b09 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52  te.**   *  OP_VR
18b0a 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  ename.**.** If n
18b0b 6f 20 73 75 63 68 20 69 6e 73 74 72 75 63 74 69  o such instructi
18b0c 6f 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65  on is found, the
18b0d 6e 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e  n every Statemen
18b0e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 2a  t instruction .*
18b0f 2a 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20  * is changed to 
18b10 61 20 4e 6f 6f 70 2e 20 20 49 6e 20 74 68 69 73  a Noop.  In this
18b11 20 77 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63   way, we avoid c
18b12 72 65 61 74 69 6e 67 20 74 68 65 20 73 74 61 74  reating the stat
18b13 65 6d 65 6e 74 20 0a 2a 2a 20 6a 6f 75 72 6e 61  ement .** journa
18b14 6c 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61  l file unnecessa
18b15 72 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rily..*/.static 
18b16 76 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61  void resolveP2Va
18b17 6c 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e  lues(Vdbe *p, in
18b18 74 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29  t *pMaxFuncArgs)
18b19 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
18b1a 20 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20   nMaxArgs = 0;. 
18b1b 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20   Op *pOp;.  int 
18b1c 2a 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61  *aLabel = p->aLa
18b1d 62 65 6c 3b 0a 20 20 69 6e 74 20 64 6f 65 73 53  bel;.  int doesS
18b1e 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b  tatementRollback
18b1f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 53   = 0;.  int hasS
18b20 74 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20  tatementBegin = 
18b21 30 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79  0;.  p->readOnly
18b22 20 3d 20 31 3b 0a 20 20 70 2d 3e 75 73 65 73 53   = 1;.  p->usesS
18b23 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a  tmtJournal = 0;.
18b24 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70    for(pOp=p->aOp
18b25 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e  , i=p->nOp-1; i>
18b26 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b  =0; i--, pOp++){
18b27 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d  .    u8 opcode =
18b28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20   pOp->opcode;.. 
18b29 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f     if( opcode==O
18b2a 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70  P_Function || op
18b2b 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70  code==OP_AggStep
18b2c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   ){.      if( pO
18b2d 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29  p->p5>nMaxArgs )
18b2e 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d   nMaxArgs = pOp-
18b2f 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  >p5;.#ifndef SQL
18b30 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
18b31 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20  TABLE.    }else 
18b32 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56  if( opcode==OP_V
18b33 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Update ){.      
18b34 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78  if( pOp->p2>nMax
18b35 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20  Args ) nMaxArgs 
18b36 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69  = pOp->p2;.#endi
18b37 66 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  f.    }.    if( 
18b38 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20  opcode==OP_Halt 
18b39 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  ){.      if( pOp
18b3a 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e  ->p1==SQLITE_CON
18b3b 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e  STRAINT && pOp->
18b3c 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a  p2==OE_Abort ){.
18b3d 20 20 20 20 20 20 20 20 64 6f 65 73 53 74 61 74          doesStat
18b3e 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20  ementRollback = 
18b3f 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
18b40 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d  else if( opcode=
18b41 3d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b  =OP_Statement ){
18b42 0a 20 20 20 20 20 20 68 61 73 53 74 61 74 65 6d  .      hasStatem
18b43 65 6e 74 42 65 67 69 6e 20 3d 20 31 3b 0a 20 20  entBegin = 1;.  
18b44 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a      p->usesStmtJ
18b45 6f 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20  ournal = 1;.    
18b46 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65  }else if( opcode
18b47 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 29 7b 0a  ==OP_Destroy ){.
18b48 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d        doesStatem
18b49 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b  entRollback = 1;
18b4a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  .    }else if( o
18b4b 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61  pcode==OP_Transa
18b4c 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32  ction && pOp->p2
18b4d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  !=0 ){.      p->
18b4e 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69  readOnly = 0;.#i
18b4f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18b50 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
18b51 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63     }else if( opc
18b52 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20  ode==OP_VUpdate 
18b53 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52  || opcode==OP_VR
18b54 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 64  ename ){.      d
18b55 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c  oesStatementRoll
18b56 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65  back = 1;.    }e
18b57 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d  lse if( opcode==
18b58 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a 20 20  OP_VFilter ){.  
18b59 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20      int n;.     
18b5a 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 20   assert( p->nOp 
18b5b 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 20 20  - i >= 3 );.    
18b5c 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
18b5d 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 74  ].opcode==OP_Int
18b5e 65 67 65 72 20 29 3b 0a 20 20 20 20 20 20 6e 20  eger );.      n 
18b5f 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20  = pOp[-1].p1;.  
18b60 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 41 72      if( n>nMaxAr
18b61 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20  gs ) nMaxArgs = 
18b62 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  n;.#endif.    }.
18b63 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
18b64 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f  VdbeOpcodeHasPro
18b65 70 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 4f 50  perty(opcode, OP
18b66 46 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 4f 70  FLG_JUMP) && pOp
18b67 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20  ->p2<0 ){.      
18b68 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d 3e  assert( -1-pOp->
18b69 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a  p2<p->nLabel );.
18b6a 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20        pOp->p2 = 
18b6b 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70  aLabel[-1-pOp->p
18b6c 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  2];.    }.  }.  
18b6d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d  sqlite3DbFree(p-
18b6e 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b  >db, p->aLabel);
18b6f 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 30  .  p->aLabel = 0
18b70 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 72  ;..  *pMaxFuncAr
18b71 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a 0a  gs = nMaxArgs;..
18b72 20 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72    /* If we never
18b73 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74   rollback a stat
18b74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
18b75 6e 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e  n, then statemen
18b76 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  t.  ** transacti
18b77 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64  ons are not need
18b78 65 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65  ed.  So change e
18b79 76 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e  very OP_Statemen
18b7a 74 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e  t.  ** opcode in
18b7b 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20  to an OP_Noop.  
18b7c 54 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c  This avoid a cal
18b7d 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70  l to sqlite3OsOp
18b7e 65 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20  enExclusive().  
18b7f 2a 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  ** which can be 
18b80 65 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d  expensive on som
18b81 65 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a  e platforms..  *
18b82 2f 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65  /.  if( hasState
18b83 6d 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f  mentBegin && !do
18b84 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62  esStatementRollb
18b85 61 63 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 75 73  ack ){.    p->us
18b86 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20  esStmtJournal = 
18b87 30 3b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70  0;.    for(pOp=p
18b88 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d  ->aOp, i=p->nOp-
18b89 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f  1; i>=0; i--, pO
18b8a 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  p++){.      if( 
18b8b 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
18b8c 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20  Statement ){.   
18b8d 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65       pOp->opcode
18b8e 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20   = OP_Noop;.    
18b8f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
18b90 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
18b91 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
18b92 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
18b93 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  n to be inserted
18b94 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18b95 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
18b96 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56  dbeCurrentAddr(V
18b97 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  dbe *p){.  asser
18b98 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
18b99 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
18b9a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b    return p->nOp;
18b9b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
18b9c 77 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70  whole list of op
18b9d 65 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20  erations to the 
18b9e 6f 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e  operation stack.
18b9f 20 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20    Return the.** 
18ba0 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66  address of the f
18ba1 69 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61  irst operation a
18ba2 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dded..*/.SQLITE_
18ba3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18ba4 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
18ba5 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f  (Vdbe *p, int nO
18ba6 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f  p, VdbeOpList co
18ba7 6e 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74  nst *aOp){.  int
18ba8 20 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28   addr;.  assert(
18ba9 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
18baa 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
18bab 69 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70  if( p->nOp + nOp
18bac 20 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26   > p->nOpAlloc &
18bad 26 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29  & growOpArray(p)
18bae 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
18baf 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70  ;.  }.  addr = p
18bb0 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70  ->nOp;.  if( nOp
18bb1 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  >0 ){.    int i;
18bb2 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20  .    VdbeOpList 
18bb3 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70  const *pIn = aOp
18bb4 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
18bb5 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b  <nOp; i++, pIn++
18bb6 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20  ){.      int p2 
18bb7 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20  = pIn->p2;.     
18bb8 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20   VdbeOp *pOut = 
18bb9 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b  &p->aOp[i+addr];
18bba 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63  .      pOut->opc
18bbb 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64  ode = pIn->opcod
18bbc 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  e;.      pOut->p
18bbd 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20  1 = pIn->p1;.   
18bbe 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73     if( p2<0 && s
18bbf 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65  qlite3VdbeOpcode
18bc0 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74  HasProperty(pOut
18bc1 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f  ->opcode, OPFLG_
18bc2 4a 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20  JUMP) ){.       
18bc3 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72   pOut->p2 = addr
18bc4 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20   + ADDR(p2);.   
18bc5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
18bc6 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b    pOut->p2 = p2;
18bc7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
18bc8 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70  Out->p3 = pIn->p
18bc9 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  3;.      pOut->p
18bca 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53  4type = P4_NOTUS
18bcb 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  ED;.      pOut->
18bcc 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  p4.p = 0;.      
18bcd 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69  pOut->p5 = 0;.#i
18bce 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
18bcf 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43  G.      pOut->zC
18bd0 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20  omment = 0;.    
18bd1 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
18bd2 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20  eAddopTrace ){. 
18bd3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
18bd4 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61  bePrintOp(0, i+a
18bd5 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61  ddr, &p->aOp[i+a
18bd6 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23  ddr]);.      }.#
18bd7 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20  endif.    }.    
18bd8 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20  p->nOp += nOp;. 
18bd9 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72   }.  return addr
18bda 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
18bdb 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
18bdc 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f  he P1 operand fo
18bdd 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73  r a specific ins
18bde 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  truction..** Thi
18bdf 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
18be0 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65  ful when a large
18be1 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64   program is load
18be2 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61  ed from a.** sta
18be3 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20  tic array using 
18be4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
18be5 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74  List but we want
18be6 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65   to make a.** fe
18be7 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20  w minor changes 
18be8 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a  to the program..
18be9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18bea 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
18beb 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20  beChangeP1(Vdbe 
18bec 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  *p, int addr, in
18bed 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74  t val){.  assert
18bee 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67  ( p==0 || p->mag
18bef 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
18bf0 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26  NIT );.  if( p &
18bf1 26 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e  & addr>=0 && p->
18bf2 6e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61  nOp>addr && p->a
18bf3 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70  Op ){.    p->aOp
18bf4 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b  [addr].p1 = val;
18bf5 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  .  }.}../*.** Ch
18bf6 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
18bf7 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64  f the P2 operand
18bf8 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20   for a specific 
18bf9 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
18bfa 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
18bfb 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69  useful for setti
18bfc 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e  ng a jump destin
18bfd 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
18bfe 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
18bff 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
18c00 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  2(Vdbe *p, int a
18c01 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20  ddr, int val){. 
18c02 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c   assert( p==0 ||
18c03 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
18c04 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
18c05 69 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30  if( p && addr>=0
18c06 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20   && p->nOp>addr 
18c07 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20  && p->aOp ){.   
18c08 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32   p->aOp[addr].p2
18c09 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f   = val;.  }.}../
18c0a 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
18c0b 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20  value of the P3 
18c0c 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70  operand for a sp
18c0d 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69  ecific instructi
18c0e 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
18c0f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
18c10 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56  e3VdbeChangeP3(V
18c11 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
18c12 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73  , int val){.  as
18c13 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d  sert( p==0 || p-
18c14 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
18c15 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
18c16 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20 26 26   p && addr>=0 &&
18c17 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26 20   p->nOp>addr && 
18c18 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70 2d  p->aOp ){.    p-
18c19 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20  >aOp[addr].p3 = 
18c1a 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  val;.  }.}../*.*
18c1b 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
18c1c 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65  ue of the P5 ope
18c1d 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73  rand for the mos
18c1e 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64  t recently.** ad
18c1f 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a  ded operation..*
18c20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18c21 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
18c22 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a  eChangeP5(Vdbe *
18c23 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73  p, u8 val){.  as
18c24 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d  sert( p==0 || p-
18c25 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
18c26 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
18c27 20 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a   p && p->aOp ){.
18c28 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
18c29 4f 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61  Op>0 );.    p->a
18c2a 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20  Op[p->nOp-1].p5 
18c2b 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  = val;.  }.}../*
18c2c 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50  .** Change the P
18c2d 32 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73  2 operand of ins
18c2e 74 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f  truction addr so
18c2f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
18c30 74 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73  to.** the addres
18c31 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e  s of the next in
18c32 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20  struction to be 
18c33 63 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  coded..*/.SQLITE
18c34 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
18c35 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
18c36 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  e(Vdbe *p, int a
18c37 64 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 56  ddr){.  sqlite3V
18c38 64 62 65 43 68 61 6e 67 65 50 32 28 70 2c 20 61  dbeChangeP2(p, a
18c39 64 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a  ddr, p->nOp);.}.
18c3a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ../*.** If the i
18c3b 6e 70 75 74 20 46 75 6e 63 44 65 66 20 73 74 72  nput FuncDef str
18c3c 75 63 74 75 72 65 20 69 73 20 65 70 68 65 6d 65  ucture is epheme
18c3d 72 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 20 69  ral, then free i
18c3e 74 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 46 75  t.  If.** the Fu
18c3f 6e 63 44 65 66 20 69 73 20 6e 6f 74 20 65 70 68  ncDef is not eph
18c40 65 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e  ermal, then do n
18c41 6f 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  othing..*/.stati
18c42 63 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d  c void freeEphem
18c43 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c  eralFunction(sql
18c44 69 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65  ite3 *db, FuncDe
18c45 66 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 28 20  f *pDef){.  if( 
18c46 70 44 65 66 20 26 26 20 28 70 44 65 66 2d 3e 66  pDef && (pDef->f
18c47 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
18c48 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a  NC_EPHEM)!=0 ){.
18c49 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
18c4a 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d  e(db, pDef);.  }
18c4b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
18c4c 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e   a P4 value if n
18c4d 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61  ecessary..*/.sta
18c4e 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28  tic void freeP4(
18c4f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
18c50 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70   p4type, void *p
18c51 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a  4){.  if( p4 ){.
18c52 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79      switch( p4ty
18c53 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  pe ){.      case
18c54 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20   P4_REAL:.      
18c55 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20  case P4_INT64:. 
18c56 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52       case P4_MPR
18c57 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65  INTF:.      case
18c58 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20   P4_DYNAMIC:.   
18c59 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
18c5a 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  FO:.      case P
18c5b 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20  4_INTARRAY:.    
18c5c 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46    case P4_KEYINF
18c5d 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20  O_HANDOFF: {.   
18c5e 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
18c5f 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20  ee(db, p4);.    
18c60 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
18c61 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34   }.      case P4
18c62 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20  _VDBEFUNC: {.   
18c63 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70       VdbeFunc *p
18c64 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65  VdbeFunc = (Vdbe
18c65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20  Func *)p4;.     
18c66 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c     freeEphemeral
18c67 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64  Function(db, pVd
18c68 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a  beFunc->pFunc);.
18c69 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
18c6a 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61  dbeDeleteAuxData
18c6b 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a  (pVdbeFunc, 0);.
18c6c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
18c6d 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46  bFree(db, pVdbeF
18c6e 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72  unc);.        br
18c6f 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
18c70 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44     case P4_FUNCD
18c71 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72  EF: {.        fr
18c72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74  eeEphemeralFunct
18c73 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66  ion(db, (FuncDef
18c74 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62  *)p4);.        b
18c75 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
18c76 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a      case P4_MEM:
18c77 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
18c78 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c  e3ValueFree((sql
18c79 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b  ite3_value*)p4);
18c7a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
18c7b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
18c7c 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  }.}.../*.** Chan
18c7d 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61  ge N opcodes sta
18c7e 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f  rting at addr to
18c7f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49   No-ops..*/.SQLI
18c80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
18c81 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
18c82 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c  eToNoop(Vdbe *p,
18c83 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e   int addr, int N
18c84 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  ){.  if( p && p-
18c85 3e 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65  >aOp ){.    Vdbe
18c86 4f 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f  Op *pOp = &p->aO
18c87 70 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c  p[addr];.    sql
18c88 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
18c89 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d  ;.    while( N--
18c8a 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34   ){.      freeP4
18c8b 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65  (db, pOp->p4type
18c8c 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20  , pOp->p4.p);.  
18c8d 20 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20      memset(pOp, 
18c8e 30 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d  0, sizeof(pOp[0]
18c8f 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f  ));.      pOp->o
18c90 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b  pcode = OP_Noop;
18c91 0a 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20  .      pOp++;.  
18c92 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
18c93 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
18c94 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72  e of the P4 oper
18c95 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66  and for a specif
18c96 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ic instruction..
18c97 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
18c98 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61  is useful when a
18c99 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69   large program i
18c9a 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a  s loaded from a.
18c9b 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20  ** static array 
18c9c 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62  using sqlite3Vdb
18c9d 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77  eAddOpList but w
18c9e 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61  e want to make a
18c9f 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68  .** few minor ch
18ca0 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f  anges to the pro
18ca1 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  gram..**.** If n
18ca2 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 20  >=0 then the P4 
18ca3 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 6d  operand is dynam
18ca4 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74  ic, meaning that
18ca5 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68   a copy of.** th
18ca6 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 65  e string is made
18ca7 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74   into memory obt
18ca8 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
18ca9 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20  e3_malloc()..** 
18caa 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20  A value of n==0 
18cab 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 73  means copy bytes
18cac 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 6e   of zP4 up to an
18cad 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a  d including the.
18cae 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 79  ** first null by
18caf 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 6e  te.  If n>0 then
18cb0 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 20   copy n+1 bytes 
18cb1 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66  of zP4..**.** If
18cb2 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69   n==P4_KEYINFO i
18cb3 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 34  t means that zP4
18cb4 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
18cb5 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
18cb6 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20  ture..** A copy 
18cb7 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 4b  is made of the K
18cb8 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
18cb9 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74   into memory obt
18cba 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71  ained from.** sq
18cbb 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f  lite3_malloc, to
18cbc 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 74   be freed when t
18cbd 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c  he Vdbe is final
18cbe 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b  ized..** n==P4_K
18cbf 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69  EYINFO_HANDOFF i
18cc0 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a 50  ndicates that zP
18cc1 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65  4 points to a Ke
18cc2 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a  yInfo structure.
18cc3 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d  ** stored in mem
18cc4 6f 72 79 20 74 68 61 74 20 74 68 65 20 63 61 6c  ory that the cal
18cc5 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64  ler has obtained
18cc6 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
18cc7 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61  lloc. The .** ca
18cc8 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ller should not 
18cc9 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74  free the allocat
18cca 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20  ion, it will be 
18ccb 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
18ccc 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69  dbe is.** finali
18ccd 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65  zed..** .** Othe
18cce 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50  r values of n (P
18ccf 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c  4_STATIC, P4_COL
18cd0 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63  LSEQ etc.) indic
18cd1 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69  ate that zP4 poi
18cd2 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69  nts.** to a stri
18cd3 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20  ng or structure 
18cd4 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65  that is guarante
18cd5 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20  ed to exist for 
18cd6 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a  the lifetime of.
18cd7 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20  ** the Vdbe. In 
18cd8 74 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63  these cases we c
18cd9 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65  an just copy the
18cda 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
18cdb 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63  If addr<0 then c
18cdc 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 20  hange P4 on the 
18cdd 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e  most recently in
18cde 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69  serted instructi
18cdf 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
18ce0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
18ce1 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56  e3VdbeChangeP4(V
18ce2 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
18ce3 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50  , const char *zP
18ce4 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20  4, int n){.  Op 
18ce5 2a 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 20  *pOp;.  sqlite3 
18ce6 2a 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  *db;.  assert( p
18ce7 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d  !=0 );.  db = p-
18ce8 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  >db;.  assert( p
18ce9 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
18cea 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66  GIC_INIT );.  if
18ceb 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64  ( p->aOp==0 || d
18cec 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
18ced 29 7b 0a 20 20 20 20 69 66 20 28 6e 20 21 3d 20  ){.    if (n != 
18cee 50 34 5f 4b 45 59 49 4e 46 4f 29 20 7b 0a 20 20  P4_KEYINFO) {.  
18cef 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 6e      freeP4(db, n
18cf0 2c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a  , (void*)*(char*
18cf1 2a 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20  *)&zP4);.    }. 
18cf2 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
18cf3 20 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d   assert( addr<p-
18cf4 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64  >nOp );.  if( ad
18cf5 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72  dr<0 ){.    addr
18cf6 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20   = p->nOp - 1;. 
18cf7 20 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 20     if( addr<0 ) 
18cf8 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 4f  return;.  }.  pO
18cf9 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72  p = &p->aOp[addr
18cfa 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 62 2c 20  ];.  freeP4(db, 
18cfb 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70  pOp->p4type, pOp
18cfc 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d 3e  ->p4.p);.  pOp->
18cfd 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  p4.p = 0;.  if( 
18cfe 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20  n==P4_INT32 ){. 
18cff 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69 73     /* Note: this
18d00 20 63 61 73 74 20 69 73 20 73 61 66 65 2c 20 62   cast is safe, b
18d01 65 63 61 75 73 65 20 74 68 65 20 6f 72 69 67 69  ecause the origi
18d02 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61 73  n data point was
18d03 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20 74   an int.    ** t
18d04 68 61 74 20 77 61 73 20 63 61 73 74 20 74 6f 20  hat was cast to 
18d05 61 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  a (const char *)
18d06 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70 34  . */.    pOp->p4
18d07 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f  .i = SQLITE_PTR_
18d08 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 20 20  TO_INT(zP4);.   
18d09 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
18d0a 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 6c 73 65  4_INT32;.  }else
18d0b 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20   if( zP4==0 ){. 
18d0c 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30     pOp->p4.p = 0
18d0d 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
18d0e 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a  e = P4_NOTUSED;.
18d0f 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50    }else if( n==P
18d10 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20  4_KEYINFO ){.   
18d11 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
18d12 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 65  fo;.    int nFie
18d13 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 20  ld, nByte;..    
18d14 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 6e  nField = ((KeyIn
18d15 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c 64  fo*)zP4)->nField
18d16 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69  ;.    nByte = si
18d17 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 20  zeof(*pKeyInfo) 
18d18 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 7a  + (nField-1)*siz
18d19 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  eof(pKeyInfo->aC
18d1a 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c 64  oll[0]) + nField
18d1b 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d  ;.    pKeyInfo =
18d1c 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20   sqlite3Malloc( 
18d1d 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f 70  nByte );.    pOp
18d1e 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20  ->p4.pKeyInfo = 
18d1f 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66  pKeyInfo;.    if
18d20 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20  ( pKeyInfo ){.  
18d21 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64      u8 *aSortOrd
18d22 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  er;.      memcpy
18d23 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c 20  (pKeyInfo, zP4, 
18d24 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 53  nByte);.      aS
18d25 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49  ortOrder = pKeyI
18d26 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b  nfo->aSortOrder;
18d27 0a 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 74  .      if( aSort
18d28 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20  Order ){.       
18d29 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
18d2a 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e 65  Order = (unsigne
18d2b 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e 66  d char*)&pKeyInf
18d2c 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 5d  o->aColl[nField]
18d2d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
18d2e 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74  (pKeyInfo->aSort
18d2f 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64 65  Order, aSortOrde
18d30 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20  r, nField);.    
18d31 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70    }.      pOp->p
18d32 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e  4type = P4_KEYIN
18d33 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  FO;.    }else{. 
18d34 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c       p->db->mall
18d35 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
18d36 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
18d37 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20  = P4_NOTUSED;.  
18d38 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
18d39 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  n==P4_KEYINFO_HA
18d3a 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f 70  NDOFF ){.    pOp
18d3b 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29  ->p4.p = (void*)
18d3c 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  zP4;.    pOp->p4
18d3d 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 46  type = P4_KEYINF
18d3e 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  O;.  }else if( n
18d3f 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70  <0 ){.    pOp->p
18d40 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34  4.p = (void*)zP4
18d41 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
18d42 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72  e = (signed char
18d43 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  )n;.  }else{.   
18d44 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20   if( n==0 ) n = 
18d45 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
18d46 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  zP4);.    pOp->p
18d47 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53  4.z = sqlite3DbS
18d48 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50  trNDup(p->db, zP
18d49 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  4, n);.    pOp->
18d4a 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41  p4type = P4_DYNA
18d4b 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  MIC;.  }.}..#ifn
18d4c 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a  def NDEBUG./*.**
18d4d 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d   Change the comm
18d4e 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 20 6d  ent on the the m
18d4f 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64  ost recently cod
18d50 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  ed instruction. 
18d51 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20   Or.** insert a 
18d52 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68  No-op and add th
18d53 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61  e comment to tha
18d54 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f  t new instructio
18d55 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 6b 65  n.  This.** make
18d56 73 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65  s the code easie
18d57 72 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67  r to read during
18d58 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e   debugging.  Non
18d59 65 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e  e of this happen
18d5a 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63  s.** in a produc
18d5b 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f 0a 53  tion build..*/.S
18d5c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
18d5d 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f  id sqlite3VdbeCo
18d5e 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63  mment(Vdbe *p, c
18d5f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
18d60 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
18d61 69 73 74 20 61 70 3b 0a 20 20 61 73 73 65 72 74  ist ap;.  assert
18d62 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d  ( p->nOp>0 || p-
18d63 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73  >aOp==0 );.  ass
18d64 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c  ert( p->aOp==0 |
18d65 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d  | p->aOp[p->nOp-
18d66 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c  1].zComment==0 |
18d67 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  | p->db->mallocF
18d68 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70  ailed );.  if( p
18d69 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61  ->nOp ){.    cha
18d6a 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70  r **pz = &p->aOp
18d6b 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d  [p->nOp-1].zComm
18d6c 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72  ent;.    va_star
18d6d 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
18d6e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
18d6f 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20  e(p->db, *pz);. 
18d70 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33     *pz = sqlite3
18d71 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20  VMPrintf(p->db, 
18d72 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
18d73 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
18d74 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  }.}.SQLITE_PRIVA
18d75 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
18d76 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56  dbeNoopComment(V
18d77 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  dbe *p, const ch
18d78 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
18d79 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
18d7a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
18d7b 64 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29  dOp0(p, OP_Noop)
18d7c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
18d7d 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d  Op>0 || p->aOp==
18d7e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
18d7f 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61  ->aOp==0 || p->a
18d80 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f  Op[p->nOp-1].zCo
18d81 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64  mment==0 || p->d
18d82 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
18d83 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20  );.  if( p->nOp 
18d84 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a  ){.    char **pz
18d85 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f   = &p->aOp[p->nO
18d86 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20  p-1].zComment;. 
18d87 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20     va_start(ap, 
18d88 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71  zFormat);.    sq
18d89 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64  lite3DbFree(p->d
18d8a 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a  b, *pz);.    *pz
18d8b 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
18d8c 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61  tf(p->db, zForma
18d8d 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65  t, ap);.    va_e
18d8e 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65  nd(ap);.  }.}.#e
18d8f 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20  ndif  /* NDEBUG 
18d90 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
18d91 20 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20   the opcode for 
18d92 61 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e  a given address.
18d93 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18d94 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74  TE VdbeOp *sqlit
18d95 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65  e3VdbeGetOp(Vdbe
18d96 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a   *p, int addr){.
18d97 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
18d98 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
18d99 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28  NIT );.  assert(
18d9a 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64   (addr>=0 && add
18d9b 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e  r<p->nOp) || p->
18d9c 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
18d9d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 61   );.  return ((a
18d9e 64 64 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70  ddr>=0 && addr<p
18d9f 2d 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61 4f 70 5b  ->nOp)?(&p->aOp[
18da0 61 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a 0a 23 69  addr]):0);.}..#i
18da1 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
18da2 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20  E_OMIT_EXPLAIN) 
18da3 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  || !defined(NDEB
18da4 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65  UG) \.     || de
18da5 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49  fined(VDBE_PROFI
18da6 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  LE) || defined(S
18da7 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a  QLITE_DEBUG)./*.
18da8 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72  ** Compute a str
18da9 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62  ing that describ
18daa 65 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  es the P4 parame
18dab 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64  ter for an opcod
18dac 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20  e..** Use zTemp 
18dad 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64  for any required
18dae 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65   temporary buffe
18daf 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74  r space..*/.stat
18db0 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79  ic char *display
18db1 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72  P4(Op *pOp, char
18db2 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65   *zTemp, int nTe
18db3 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34  mp){.  char *zP4
18db4 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65   = zTemp;.  asse
18db5 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b  rt( nTemp>=20 );
18db6 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e  .  switch( pOp->
18db7 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61  p4type ){.    ca
18db8 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54  se P4_KEYINFO_ST
18db9 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50  ATIC:.    case P
18dba 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20  4_KEYINFO: {.   
18dbb 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20     int i, j;.   
18dbc 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
18dbd 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Info = pOp->p4.p
18dbe 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73  KeyInfo;.      s
18dbf 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
18dc0 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b  nTemp, zTemp, "k
18dc1 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79  eyinfo(%d", pKey
18dc2 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20  Info->nField);. 
18dc3 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33       i = sqlite3
18dc4 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b  Strlen30(zTemp);
18dc5 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
18dc6 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  j<pKeyInfo->nFie
18dc7 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ld; j++){.      
18dc8 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
18dc9 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f   = pKeyInfo->aCo
18dca 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69  ll[j];.        i
18dcb 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
18dcc 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71        int n = sq
18dcd 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43  lite3Strlen30(pC
18dce 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  oll->zName);.   
18dcf 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e         if( i+n>n
18dd0 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20  Temp-6 ){.      
18dd1 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54        memcpy(&zT
18dd2 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29  emp[i],",...",4)
18dd3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
18dd4 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
18dd5 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70  .          zTemp
18dd6 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20  [i++] = ',';.   
18dd7 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49         if( pKeyI
18dd8 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
18dd9 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f  && pKeyInfo->aSo
18dda 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20  rtOrder[j] ){.  
18ddb 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b            zTemp[
18ddc 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20  i++] = '-';.    
18ddd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
18dde 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b    memcpy(&zTemp[
18ddf 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  i], pColl->zName
18de0 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  ,n+1);.         
18de1 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20   i += n;.       
18de2 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e   }else if( i+4<n
18de3 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20  Temp-6 ){.      
18de4 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d      memcpy(&zTem
18de5 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a  p[i],",nil",4);.
18de6 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34            i += 4
18de7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
18de8 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b    }.      zTemp[
18de9 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20  i++] = ')';.    
18dea 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a    zTemp[i] = 0;.
18deb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c        assert( i<
18dec 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62  nTemp );.      b
18ded 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
18dee 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a  case P4_COLLSEQ:
18def 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71   {.      CollSeq
18df0 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70   *pColl = pOp->p
18df1 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73  4.pColl;.      s
18df2 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
18df3 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63  nTemp, zTemp, "c
18df4 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20  ollseq(%.20s)", 
18df5 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
18df6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
18df7 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55  }.    case P4_FU
18df8 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46  NCDEF: {.      F
18df9 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70  uncDef *pDef = p
18dfa 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20  Op->p4.pFunc;.  
18dfb 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
18dfc 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
18dfd 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65  p, "%s(%d)", pDe
18dfe 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e  f->zName, pDef->
18dff 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65  nArg);.      bre
18e00 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
18e01 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20  se P4_INT64: {. 
18e02 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
18e03 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
18e04 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70  mp, "%lld", *pOp
18e05 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20  ->p4.pI64);.    
18e06 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
18e07 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32     case P4_INT32
18e08 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
18e09 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
18e0a 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70  , zTemp, "%d", p
18e0b 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20  Op->p4.i);.     
18e0c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
18e0d 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20    case P4_REAL: 
18e0e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
18e0f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
18e10 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20  zTemp, "%.16g", 
18e11 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b  *pOp->p4.pReal);
18e12 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
18e13 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f    }.    case P4_
18e14 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d  MEM: {.      Mem
18e15 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34   *pMem = pOp->p4
18e16 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73  .pMem;.      ass
18e17 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  ert( (pMem->flag
18e18 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
18e19 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d   );.      if( pM
18e1a 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
18e1b 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  Str ){.        z
18e1c 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20  P4 = pMem->z;.  
18e1d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d      }else if( pM
18e1e 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
18e1f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Int ){.        s
18e20 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
18e21 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25  nTemp, zTemp, "%
18e22 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29  lld", pMem->u.i)
18e23 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
18e24 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
18e25 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
18e26 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
18e27 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
18e28 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65 6d  p, "%.16g", pMem
18e29 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ->r);.      }.  
18e2a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
18e2b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18e2c 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
18e2d 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 54  E.    case P4_VT
18e2e 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  AB: {.      sqli
18e2f 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20  te3_vtab *pVtab 
18e30 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b  = pOp->p4.pVtab;
18e31 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
18e32 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
18e33 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a 25  Temp, "vtab:%p:%
18e34 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61 62  p", pVtab, pVtab
18e35 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20  ->pModule);.    
18e36 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
18e37 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 50  endif.    case P
18e38 34 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20  4_INTARRAY: {.  
18e39 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
18e3a 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
18e3b 70 2c 20 22 69 6e 74 61 72 72 61 79 22 29 3b 0a  p, "intarray");.
18e3c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
18e3d 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
18e3e 7b 0a 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4f  {.      zP4 = pO
18e3f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69  p->p4.z;.      i
18e40 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20  f( zP4==0 ){.   
18e41 20 20 20 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70       zP4 = zTemp
18e42 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b  ;.        zTemp[
18e43 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  0] = 0;.      }.
18e44 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
18e45 72 74 28 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20  rt( zP4!=0 );.  
18e46 72 65 74 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65  return zP4;.}.#e
18e47 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c  ndif../*.** Decl
18e48 61 72 65 20 74 6f 20 74 68 65 20 56 64 62 65 20  are to the Vdbe 
18e49 74 68 61 74 20 74 68 65 20 42 54 72 65 65 20 6f  that the BTree o
18e4a 62 6a 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62  bject at db->aDb
18e4b 5b 69 5d 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a  [i] is used..**.
18e4c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18e4d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
18e4e 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65  beUsesBtree(Vdbe
18e4f 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69   *p, int i){.  i
18e50 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 73 65 72  nt mask;.  asser
18e51 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
18e52 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
18e53 65 72 74 28 20 69 3c 28 69 6e 74 29 73 69 7a 65  ert( i<(int)size
18e54 6f 66 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 29  of(p->btreeMask)
18e55 2a 38 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 31  *8 );.  mask = 1
18e56 3c 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62  <<i;.  if( (p->b
18e57 74 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29  treeMask & mask)
18e58 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74  ==0 ){.    p->bt
18e59 72 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b  reeMask |= mask;
18e5a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
18e5b 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72  eMutexArrayInser
18e5c 74 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d  t(&p->aMutex, p-
18e5d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29  >db->aDb[i].pBt)
18e5e 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65  ;.  }.}...#if de
18e5f 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49  fined(VDBE_PROFI
18e60 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  LE) || defined(S
18e61 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a  QLITE_DEBUG)./*.
18e62 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c  ** Print a singl
18e63 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20  e opcode.  This 
18e64 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
18e65 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e  for debugging on
18e66 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
18e67 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
18e68 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49  e3VdbePrintOp(FI
18e69 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63  LE *pOut, int pc
18e6a 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68  , Op *pOp){.  ch
18e6b 61 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20  ar *zP4;.  char 
18e6c 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74  zPtr[50];.  stat
18e6d 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ic const char *z
18e6e 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25  Format1 = "%4d %
18e6f 2d 31 33 73 20 25 34 64 20 25 34 64 20 25 34 64  -13s %4d %4d %4d
18e70 20 25 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22   %-4s %.2X %s\n"
18e71 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20  ;.  if( pOut==0 
18e72 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b  ) pOut = stdout;
18e73 0a 20 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79  .  zP4 = display
18e74 50 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69  P4(pOp, zPtr, si
18e75 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66  zeof(zPtr));.  f
18e76 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f  printf(pOut, zFo
18e77 72 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20  rmat1, pc, .    
18e78 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e    sqlite3OpcodeN
18e79 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29  ame(pOp->opcode)
18e7a 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
18e7b 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34  p2, pOp->p3, zP4
18e7c 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65  , pOp->p5,.#ifde
18e7d 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
18e7e 20 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65       pOp->zComme
18e7f 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65  nt ? pOp->zComme
18e80 6e 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20  nt : "".#else.  
18e81 20 20 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20      "".#endif.  
18e82 29 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74  );.  fflush(pOut
18e83 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
18e84 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72  ** Release an ar
18e85 72 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65  ray of N Mem ele
18e86 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20  ments.*/.static 
18e87 76 6f 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41  void releaseMemA
18e88 72 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74  rray(Mem *p, int
18e89 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20   N){.  if( p && 
18e8a 4e 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45  N ){.    Mem *pE
18e8b 6e 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  nd;.    sqlite3 
18e8c 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20  *db = p->db;.   
18e8d 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65   u8 malloc_faile
18e8e 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  d = db->mallocFa
18e8f 69 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 45  iled;.    for(pE
18e90 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64  nd=&p[N]; p<pEnd
18e91 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73  ; p++){.      as
18e92 73 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70  sert( (&p[1])==p
18e93 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d  End || p[0].db==
18e94 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20  p[1].db );..    
18e95 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20    /* This block 
18e96 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c  is really an inl
18e97 69 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ined version of 
18e98 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
18e99 6c 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a  lease().      **
18e9a 20 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61   that takes adva
18e9b 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63  ntage of the fac
18e9c 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72  t that the memor
18e9d 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20  y cell value is 
18e9e 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20  .      ** being 
18e9f 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65  set to NULL afte
18ea0 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20  r releasing any 
18ea1 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65  dynamic resource
18ea2 73 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  s..      **.    
18ea3 20 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69    ** The justifi
18ea4 63 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69  cation for dupli
18ea5 63 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74  cating code is t
18ea6 68 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  hat according to
18ea7 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67   .      ** callg
18ea8 72 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65  rind, this cause
18ea9 73 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 74  s a certain test
18eaa 20 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 65   case to hit the
18eab 20 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20   CPU 4.7 .      
18eac 2a 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20  ** percent less 
18ead 28 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20  (x86 linux, gcc 
18eae 76 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d  version 4.1.2, -
18eaf 4f 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20  O6) than if .   
18eb0 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d     ** sqlite3Mem
18eb1 52 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 63  Release() were c
18eb2 61 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e  alled from here.
18eb3 20 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20   With -O2, this 
18eb4 6a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74  jumps.      ** t
18eb5 6f 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54  o 6.6 percent. T
18eb6 68 65 20 74 65 73 74 20 63 61 73 65 20 69 73 20  he test case is 
18eb7 69 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 72  inserting 1000 r
18eb8 6f 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65  ows into a table
18eb9 20 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20   .      ** with 
18eba 6e 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67  no indexes using
18ebb 20 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72   a single prepar
18ebc 65 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  ed INSERT statem
18ebd 65 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20  ent, bind() .   
18ebe 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28     ** and reset(
18ebf 29 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 67  ). Inserts are g
18ec0 72 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72  rouped into a tr
18ec1 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20  ansaction..     
18ec2 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d   */.      if( p-
18ec3 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c  >flags&(MEM_Agg|
18ec4 4d 45 4d 5f 44 79 6e 29 20 29 7b 0a 20 20 20 20  MEM_Dyn) ){.    
18ec5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
18ec6 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 20  emRelease(p);.  
18ec7 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d      }else if( p-
18ec8 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >zMalloc ){.    
18ec9 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
18eca 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63  e(db, p->zMalloc
18ecb 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 4d  );.        p->zM
18ecc 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20  alloc = 0;.     
18ecd 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66 6c 61   }..      p->fla
18ece 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
18ecf 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c     }.    db->mal
18ed0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 6c 6c  locFailed = mall
18ed1 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 0a 7d  oc_failed;.  }.}
18ed2 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
18ed3 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
18ed4 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f  NAGEMENT.SQLITE_
18ed5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18ed6 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 42 75  te3VdbeReleaseBu
18ed7 66 66 65 72 73 28 56 64 62 65 20 2a 70 29 7b 0a  ffers(Vdbe *p){.
18ed8 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
18ed9 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 61 73 73  nFree = 0;.  ass
18eda 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
18edb 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
18edc 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69  utex) );.  for(i
18edd 69 3d 31 3b 20 69 69 3c 3d 70 2d 3e 6e 4d 65 6d  i=1; ii<=p->nMem
18ede 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d  ; ii++){.    Mem
18edf 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65   *pMem = &p->aMe
18ee0 6d 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  m[ii];.    if( p
18ee1 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
18ee2 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 20  _RowSet ){.     
18ee3 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c   sqlite3RowSetCl
18ee4 65 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77  ear(pMem->u.pRow
18ee5 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Set);.    }.    
18ee6 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70  if( pMem->z && p
18ee7 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44  Mem->flags&MEM_D
18ee8 79 6e 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  yn ){.      asse
18ee9 72 74 28 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20  rt( !pMem->xDel 
18eea 29 3b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b  );.      nFree +
18eeb 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
18eec 63 53 69 7a 65 28 70 4d 65 6d 2d 3e 64 62 2c 20  cSize(pMem->db, 
18eed 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20  pMem->z);.      
18eee 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
18eef 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20  lease(pMem);.   
18ef0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
18ef1 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a  nFree;.}.#endif.
18ef2 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18ef3 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a  OMIT_EXPLAIN./*.
18ef4 2a 2a 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e  ** Give a listin
18ef5 67 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d  g of the program
18ef6 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20   in the virtual 
18ef7 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54  machine..**.** T
18ef8 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  he interface is 
18ef9 74 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69  the same as sqli
18efa 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20  te3VdbeExec().  
18efb 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a  But instead of.*
18efc 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f  * running the co
18efd 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20 74  de, it invokes t
18efe 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65  he callback once
18eff 20 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72 75   for each instru
18f00 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66  ction..** This f
18f01 65 61 74 75 72 65 20 69 73 20 75 73 65 64 20 74  eature is used t
18f02 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50  o implement "EXP
18f03 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  LAIN"..**.** Whe
18f04 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c  n p->explain==1,
18f05 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f   each instructio
18f06 6e 20 69 73 20 6c 69 73 74 65 64 2e 20 20 57 68  n is listed.  Wh
18f07 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e  en.** p->explain
18f08 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70  ==2, only OP_Exp
18f09 6c 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e  lain instruction
18f0a 73 20 61 72 65 20 6c 69 73 74 65 64 20 61 6e 64  s are listed and
18f0b 20 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 73 68   these.** are sh
18f0c 6f 77 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65  own in a differe
18f0d 6e 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65  nt format.  p->e
18f0e 78 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 73 65  xplain==2 is use
18f0f 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a  d to implement.*
18f10 2a 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20  * EXPLAIN QUERY 
18f11 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  PLAN..*/.SQLITE_
18f12 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18f13 74 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56  te3VdbeList(.  V
18f14 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20  dbe *p          
18f15 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
18f16 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  VDBE */.){.  sql
18f17 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
18f18 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
18f19 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
18f1a 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70  .  Mem *pMem = p
18f1b 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26  ->pResultSet = &
18f1c 70 2d 3e 61 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61  p->aMem[1];..  a
18f1d 73 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69  ssert( p->explai
18f1e 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61  n );.  if( p->ma
18f1f 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic!=VDBE_MAGIC_
18f20 52 55 4e 20 29 20 72 65 74 75 72 6e 20 53 51 4c  RUN ) return SQL
18f21 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 61 73  ITE_MISUSE;.  as
18f22 73 65 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d  sert( db->magic=
18f23 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55  =SQLITE_MAGIC_BU
18f24 53 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  SY );.  assert( 
18f25 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->rc==SQLITE_OK
18f26 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54   || p->rc==SQLIT
18f27 45 5f 42 55 53 59 20 7c 7c 20 70 2d 3e 72 63 3d  E_BUSY || p->rc=
18f28 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
18f29 0a 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75  ..  /* Even thou
18f2a 67 68 20 74 68 69 73 20 6f 70 63 6f 64 65 20 64  gh this opcode d
18f2b 6f 65 73 20 6e 6f 74 20 75 73 65 20 64 79 6e 61  oes not use dyna
18f2c 6d 69 63 20 73 74 72 69 6e 67 73 20 66 6f 72 0a  mic strings for.
18f2d 20 20 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c    ** the result,
18f2e 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
18f2f 6d 61 79 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d  may become dynam
18f30 69 63 20 69 66 20 74 68 65 20 75 73 65 72 20 63  ic if the user c
18f31 61 6c 6c 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65  alls.  ** sqlite
18f32 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
18f33 29 2c 20 63 61 75 73 69 6e 67 20 61 20 74 72 61  ), causing a tra
18f34 6e 73 6c 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d  nslation to UTF-
18f35 31 36 20 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a  16 encoding..  *
18f36 2f 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72  /.  releaseMemAr
18f37 72 61 79 28 70 4d 65 6d 2c 20 70 2d 3e 6e 4d 65  ray(pMem, p->nMe
18f38 6d 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 63  m);..  if( p->rc
18f39 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
18f3a 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61  {.    /* This ha
18f3b 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f  ppens if a mallo
18f3c 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c  c() inside a cal
18f3d 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  l to sqlite3_col
18f3e 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20  umn_text() or.  
18f3f 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c    ** sqlite3_col
18f40 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69  umn_text16() fai
18f41 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 64 62 2d  led.  */.    db-
18f42 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
18f43 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  1;.    return SQ
18f44 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
18f45 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20 3d 20 70  .  do{.    i = p
18f46 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65  ->pc++;.  }while
18f47 28 20 69 3c 70 2d 3e 6e 4f 70 20 26 26 20 70 2d  ( i<p->nOp && p-
18f48 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 20 70  >explain==2 && p
18f49 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 21  ->aOp[i].opcode!
18f4a 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b 0a 20  =OP_Explain );. 
18f4b 20 69 66 28 20 69 3e 3d 70 2d 3e 6e 4f 70 20 29   if( i>=p->nOp )
18f4c 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
18f4d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20  LITE_OK;.    rc 
18f4e 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
18f4f 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75   }else if( db->u
18f50 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
18f51 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53  ){.    p->rc = S
18f52 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
18f53 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
18f54 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69  _ERROR;.    sqli
18f55 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
18f56 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
18f57 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  s", sqlite3ErrSt
18f58 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c  r(p->rc));.  }el
18f59 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b  se{.    char *z;
18f5a 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d 20 26  .    Op *pOp = &
18f5b 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 69  p->aOp[i];.    i
18f5c 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31  f( p->explain==1
18f5d 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   ){.      pMem->
18f5e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
18f5f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  .      pMem->typ
18f60 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  e = SQLITE_INTEG
18f61 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ER;.      pMem->
18f62 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20 20 20  u.i = i;        
18f63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18f64 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72          /* Progr
18f65 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  am counter */.  
18f66 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20      pMem++;.  . 
18f67 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
18f68 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45   = MEM_Static|ME
18f69 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
18f6a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20        pMem->z = 
18f6b 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 4f 70  (char*)sqlite3Op
18f6c 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70  codeName(pOp->op
18f6d 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64  code);  /* Opcod
18f6e 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  e */.      asser
18f6f 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b  t( pMem->z!=0 );
18f70 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
18f71 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
18f72 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20  (pMem->z);.     
18f73 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
18f74 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20  LITE_TEXT;.     
18f75 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
18f76 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20  ITE_UTF8;.      
18f77 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  pMem++;.    }.. 
18f78 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
18f79 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d   MEM_Int;.    pM
18f7a 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70  em->u.i = pOp->p
18f7b 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
18f7c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
18f7d 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  1 */.    pMem->t
18f7e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
18f7f 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b  EGER;.    pMem++
18f80 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ;..    pMem->fla
18f81 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
18f82 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f    pMem->u.i = pO
18f83 70 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20  p->p2;          
18f84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18f85 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65  /* P2 */.    pMe
18f86 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
18f87 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d  _INTEGER;.    pM
18f88 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70  em++;..    if( p
18f89 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a  ->explain==1 ){.
18f8a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
18f8b 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
18f8c 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70     pMem->u.i = p
18f8d 4f 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20  Op->p3;         
18f8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18f8f 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20   /* P3 */.      
18f90 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
18f91 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  ITE_INTEGER;.   
18f92 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d     pMem++;.    }
18f93 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
18f94 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
18f95 6d 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20  m, 32, 0) ){    
18f96 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f          /* P4 */
18f97 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
18f98 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
18f99 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ed );.      retu
18f9a 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
18f9b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
18f9c 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e  >flags = MEM_Dyn
18f9d 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72  |MEM_Str|MEM_Ter
18f9e 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c  m;.    z = displ
18f9f 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e  ayP4(pOp, pMem->
18fa0 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20  z, 32);.    if( 
18fa1 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20  z!=pMem->z ){.  
18fa2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
18fa3 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a  emSetStr(pMem, z
18fa4 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
18fa5 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  8, 0);.    }else
18fa6 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
18fa7 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20  pMem->z!=0 );.  
18fa8 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71      pMem->n = sq
18fa9 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d  lite3Strlen30(pM
18faa 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d  em->z);.      pM
18fab 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  em->enc = SQLITE
18fac 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20  _UTF8;.    }.   
18fad 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
18fae 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70  LITE_TEXT;.    p
18faf 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20  Mem++;..    if( 
18fb0 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b  p->explain==1 ){
18fb1 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
18fb2 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
18fb3 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20  em, 4, 0) ){.   
18fb4 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
18fb5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
18fb6 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   );.        retu
18fb7 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
18fb8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
18fb9 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
18fba 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d  _Dyn|MEM_Str|MEM
18fbb 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65  _Term;.      pMe
18fbc 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20  m->n = 2;.      
18fbd 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
18fbe 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e  (3, pMem->z, "%.
18fbf 32 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20  2x", pOp->p5);  
18fc0 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20   /* P5 */.      
18fc1 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
18fc2 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20  ITE_TEXT;.      
18fc3 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
18fc4 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70  TE_UTF8;.      p
18fc5 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66  Mem++;.  .#ifdef
18fc6 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
18fc7 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f      if( pOp->zCo
18fc8 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20  mment ){.       
18fc9 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
18fca 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b  EM_Str|MEM_Term;
18fcb 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a  .        pMem->z
18fcc 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74   = pOp->zComment
18fcd 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
18fce 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
18fcf 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  n30(pMem->z);.  
18fd0 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20        pMem->enc 
18fd1 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20  = SQLITE_UTF8;. 
18fd2 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70         pMem->typ
18fd3 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b  e = SQLITE_TEXT;
18fd4 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  .      }else.#en
18fd5 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  dif.      {.    
18fd6 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
18fd7 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20  = MEM_Null;     
18fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18fd9 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a    /* Comment */.
18fda 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79          pMem->ty
18fdb 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  pe = SQLITE_NULL
18fdc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
18fdd 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75  .    p->nResColu
18fde 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65  mn = 8 - 5*(p->e
18fdf 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70  xplain-1);.    p
18fe0 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
18fe1 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
18fe2 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74  E_ROW;.  }.  ret
18fe3 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
18fe4 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
18fe5 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64  EXPLAIN */..#ifd
18fe6 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
18fe7 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20  /*.** Print the 
18fe8 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65  SQL that was use
18fe9 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  d to generate a 
18fea 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f  VDBE program..*/
18feb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18fec 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
18fed 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70  PrintSql(Vdbe *p
18fee 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70  ){.  int nOp = p
18fef 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20  ->nOp;.  VdbeOp 
18ff0 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c  *pOp;.  if( nOp<
18ff1 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f  1 ) return;.  pO
18ff2 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a  p = &p->aOp[0];.
18ff3 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
18ff4 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70  e==OP_Trace && p
18ff5 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20  Op->p4.z!=0 ){. 
18ff6 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
18ff7 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20   = pOp->p4.z;.  
18ff8 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
18ff9 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b  Isspace(*z) ) z+
18ffa 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53  +;.    printf("S
18ffb 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b  QL: [%s]\n", z);
18ffc 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  .  }.}.#endif..#
18ffd 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
18ffe 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26  TE_OMIT_TRACE) &
18fff 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
19000 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29  _ENABLE_IOTRACE)
19001 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20  ./*.** Print an 
19002 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20  IOTRACE message 
19003 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74  showing SQL cont
19004 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
19005 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
19006 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
19007 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  l(Vdbe *p){.  in
19008 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a  t nOp = p->nOp;.
19009 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
1900a 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72   if( sqlite3IoTr
1900b 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ace==0 ) return;
1900c 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72  .  if( nOp<1 ) r
1900d 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26  eturn;.  pOp = &
1900e 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28  p->aOp[0];.  if(
1900f 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
19010 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70  _Trace && pOp->p
19011 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  4.z!=0 ){.    in
19012 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72  t i, j;.    char
19013 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71   z[1000];.    sq
19014 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
19015 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73  izeof(z), z, "%s
19016 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  ", pOp->p4.z);. 
19017 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69     for(i=0; sqli
19018 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29  te3Isspace(z[i])
19019 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72  ; i++){}.    for
1901a 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29  (j=0; z[i]; i++)
1901b 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
1901c 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29  te3Isspace(z[i])
1901d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1901e 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20  z[i-1]!=' ' ){. 
1901f 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
19020 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d  = ' ';.        }
19021 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19022 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
19023 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [i];.      }.   
19024 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b   }.    z[j] = 0;
19025 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72  .    sqlite3IoTr
19026 61 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20  ace("SQL %s\n", 
19027 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  z);.  }.}.#endif
19028 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
19029 5f 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45  _TRACE && SQLITE
1902a 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20  _ENABLE_IOTRACE 
1902b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  */../*.** Alloca
1902c 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20  te space from a 
1902d 66 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65  fixed size buffe
1902e 72 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69  r.  Make *pp poi
1902f 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c  nt to the.** all
19030 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20 20 28  ocated space.  (
19031 4e 6f 74 65 3a 20 20 70 70 20 69 73 20 61 20 63  Note:  pp is a c
19032 68 61 72 2a 20 72 61 74 68 65 72 20 74 68 61 6e  har* rather than
19033 20 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20   a void** to.** 
19034 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20  work around the 
19035 70 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 6e 67  pointer aliasing
19036 20 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a   rules of C.)  *
19037 70 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 69 61  pp should initia
19038 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20  lly.** be zero. 
19039 20 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a   If *pp is not z
1903a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  ero, that means 
1903b 74 68 61 74 20 74 68 65 20 73 70 61 63 65 20 68  that the space h
1903c 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65  as already.** be
1903d 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  en allocated and
1903e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1903f 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e   a noop..**.** n
19040 42 79 74 65 20 69 73 20 74 68 65 20 6e 75 6d 62  Byte is the numb
19041 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
19042 70 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a  pace needed..**.
19043 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74  ** *ppFrom point
19044 20 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 73 70   to available sp
19045 61 63 65 20 61 6e 64 20 70 45 6e 64 20 70 6f 69  ace and pEnd poi
19046 6e 74 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  nts to the end o
19047 66 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62  f the.** availab
19048 6c 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  le space..**.** 
19049 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75  *pnByte is a cou
1904a 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62  nter of the numb
1904b 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
1904c 70 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66  pace that have f
1904d 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f  ailed.** to allo
1904e 63 61 74 65 2e 20 20 49 66 20 74 68 65 72 65 20  cate.  If there 
1904f 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 20  is insufficient 
19050 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d  space in *ppFrom
19051 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a   to satisfy the.
19052 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68 65 6e  ** request, then
19053 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79   increment *pnBy
19054 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75 6e 74  te by the amount
19055 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 2e   of the request.
19056 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
19057 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 63 68  allocSpace(.  ch
19058 61 72 20 2a 70 70 2c 20 20 20 20 20 20 20 20 20  ar *pp,         
19059 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65     /* IN/OUT: Se
1905a 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74  t *pp to point t
1905b 6f 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  o allocated buff
1905c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  er */.  int nByt
1905d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
1905e 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1905f 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20  to allocate */. 
19060 20 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20   u8 **ppFrom,   
19061 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
19062 20 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a   Allocate from *
19063 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a  ppFrom */.  u8 *
19064 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20  pEnd,           
19065 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31   /* Pointer to 1
19066 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65   byte past the e
19067 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75  nd of *ppFrom bu
19068 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ffer */.  int *p
19069 6e 42 79 74 65 20 20 20 20 20 20 20 20 20 20 2f  nByte          /
1906a 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  * If allocation 
1906b 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20  cannot be made, 
1906c 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74  increment *pnByt
1906d 65 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 28 2a  e */.){.  if( (*
1906e 28 76 6f 69 64 2a 2a 29 70 70 29 3d 3d 30 20 29  (void**)pp)==0 )
1906f 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 52 4f  {.    nByte = RO
19070 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 20  UND8(nByte);.   
19071 20 69 66 28 20 28 70 45 6e 64 20 2d 20 2a 70 70   if( (pEnd - *pp
19072 46 72 6f 6d 29 3e 3d 6e 42 79 74 65 20 29 7b 0a  From)>=nByte ){.
19073 20 20 20 20 20 20 2a 28 76 6f 69 64 2a 2a 29 70        *(void**)p
19074 70 20 3d 20 28 76 6f 69 64 20 2a 29 2a 70 70 46  p = (void *)*ppF
19075 72 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 70 46 72  rom;.      *ppFr
19076 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20  om += nByte;.   
19077 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70   }else{.      *p
19078 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74 65 3b 0a  nByte += nByte;.
19079 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1907a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72  ** Prepare a vir
1907b 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72  tual machine for
1907c 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69   execution.  Thi
1907d 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67  s involves thing
1907e 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c  s such.** as all
1907f 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70  ocating stack sp
19080 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ace and initiali
19081 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d  zing the program
19082 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74   counter..** Aft
19083 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20  er the VDBE has 
19084 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63  be prepped, it c
19085 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62  an be executed b
19086 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  y one or more.**
19087 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
19088 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a  3VdbeExec().  .*
19089 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
1908a 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76   only way to mov
1908b 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44  e a VDBE from VD
1908c 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f  BE_MAGIC_INIT to
1908d 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52  .** VDBE_MAGIC_R
1908e 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  UN..**.** This f
1908f 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
19090 61 6c 6c 65 64 20 6d 6f 72 65 20 74 68 61 6e 20  alled more than 
19091 6f 6e 63 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65  once on a single
19092 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
19093 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 63  ..** The first c
19094 61 6c 6c 20 69 73 20 6d 61 64 65 20 77 68 69 6c  all is made whil
19095 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20  e compiling the 
19096 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 53  SQL statement. S
19097 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c  ubsequent.** cal
19098 6c 73 20 61 72 65 20 6d 61 64 65 20 61 73 20 70  ls are made as p
19099 61 72 74 20 6f 66 20 74 68 65 20 70 72 6f 63 65  art of the proce
1909a 73 73 20 6f 66 20 72 65 73 65 74 74 69 6e 67 20  ss of resetting 
1909b 61 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  a statement to b
1909c 65 0a 2a 2a 20 72 65 2d 65 78 65 63 75 74 65 64  e.** re-executed
1909d 20 28 66 72 6f 6d 20 61 20 63 61 6c 6c 20 74 6f   (from a call to
1909e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29   sqlite3_reset()
1909f 29 2e 20 54 68 65 20 6e 56 61 72 2c 20 6e 4d 65  ). The nVar, nMe
190a0 6d 2c 20 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 61  m, nCursor .** a
190a1 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 70 61 72  nd isExplain par
190a2 61 6d 65 74 65 72 73 20 61 72 65 20 6f 6e 6c 79  ameters are only
190a3 20 70 61 73 73 65 64 20 63 6f 72 72 65 63 74 20   passed correct 
190a4 76 61 6c 75 65 73 20 74 68 65 20 66 69 72 73 74  values the first
190a5 20 74 69 6d 65 0a 2a 2a 20 74 68 65 20 66 75 6e   time.** the fun
190a6 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
190a7 20 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63   On subsequent c
190a8 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 6c 69 74  alls, from sqlit
190a9 65 33 5f 72 65 73 65 74 28 29 2c 20 6e 56 61 72  e3_reset(), nVar
190aa 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 2d 31  .** is passed -1
190ab 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 73   and nMem, nCurs
190ac 6f 72 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e  or and isExplain
190ad 20 61 72 65 20 61 6c 6c 20 70 61 73 73 65 64 20   are all passed 
190ae 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  zero..*/.SQLITE_
190af 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
190b0 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64  ite3VdbeMakeRead
190b1 79 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20  y(.  Vdbe *p,   
190b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
190b3 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
190b4 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20  */.  int nVar,  
190b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
190b6 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
190b7 20 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20   '?' see in the 
190b8 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  SQL statement */
190b9 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20  .  int nMem,    
190ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
190bb 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
190bc 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61  emory cells to a
190bd 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74  llocate */.  int
190be 20 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20   nCursor,       
190bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
190c0 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73  umber of cursors
190c1 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a   to allocate */.
190c2 20 20 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 20    int isExplain 
190c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
190c4 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
190c5 45 58 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73  EXPLAIN keywords
190c6 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29   is present */.)
190c7 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c  {.  int n;.  sql
190c8 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
190c9 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d  ;..  assert( p!=
190ca 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
190cb 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
190cc 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f  GIC_INIT );..  /
190cd 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62  * There should b
190ce 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f  e at least one o
190cf 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73  pcode..  */.  as
190d0 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29  sert( p->nOp>0 )
190d1 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  ;..  /* Set the 
190d2 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41  magic to VDBE_MA
190d3 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72  GIC_RUN sooner r
190d4 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72  ather than later
190d5 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20  . */.  p->magic 
190d6 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  = VDBE_MAGIC_RUN
190d7 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68  ;..  /* For each
190d8 20 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 64   cursor required
190d9 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20  , also allocate 
190da 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d  a memory cell. M
190db 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73  emory.  ** cells
190dc 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72   (nMem+1-nCursor
190dd 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69  )..nMem, inclusi
190de 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  ve, will never b
190df 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74  e used by.  ** t
190e0 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e  he vdbe program.
190e1 20 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 72   Instead they ar
190e2 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61  e used to alloca
190e3 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a  te space for.  *
190e4 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43  * VdbeCursor/BtC
190e5 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 73  ursor structures
190e6 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65  . The blob of me
190e7 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
190e8 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f  with .  ** curso
190e9 72 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e  r 0 is stored in
190ea 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65   memory cell nMe
190eb 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28  m. Memory cell (
190ec 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f  nMem-1).  ** sto
190ed 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20  res the blob of 
190ee 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
190ef 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c  d with cursor 1,
190f0 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20   etc..  **.  ** 
190f1 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61  See also: alloca
190f2 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f  teCursor()..  */
190f3 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73  .  nMem += nCurs
190f4 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  or;..  /* Alloca
190f5 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d  te space for mem
190f6 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c 20 53  ory registers, S
190f7 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 56 44  QL variables, VD
190f8 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64 20 0a  BE cursors and .
190f9 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 74 6f    ** an array to
190fa 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 75 6e   marshal SQL fun
190fb 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ction arguments 
190fc 69 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79  in. This is only
190fd 20 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a 20 66   done the.  ** f
190fe 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 66  irst time this f
190ff 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
19100 64 20 66 6f 72 20 61 20 67 69 76 65 6e 20 56 44  d for a given VD
19101 42 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69 74 20  BE, not when it 
19102 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 63 61  is.  ** being ca
19103 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  lled from sqlite
19104 33 5f 72 65 73 65 74 28 29 20 74 6f 20 72 65 73  3_reset() to res
19105 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  et the virtual m
19106 61 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69  achine..  */.  i
19107 66 28 20 6e 56 61 72 3e 3d 30 20 26 26 20 21 64  f( nVar>=0 && !d
19108 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
19109 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 73 72 20  ){.    u8 *zCsr 
1910a 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b  = (u8 *)&p->aOp[
1910b 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 38 20  p->nOp];.    u8 
1910c 2a 7a 45 6e 64 20 3d 20 28 75 38 20 2a 29 26 70  *zEnd = (u8 *)&p
1910d 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f  ->aOp[p->nOpAllo
1910e 63 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74  c];.    int nByt
1910f 65 3b 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b  e;.    int nArg;
19110 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
19111 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 73  m number of args
19112 20 70 61 73 73 65 64 20 74 6f 20 61 20 75 73 65   passed to a use
19113 72 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20  r function. */. 
19114 20 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75     resolveP2Valu
19115 65 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20  es(p, &nArg);.  
19116 20 20 69 66 28 20 69 73 45 78 70 6c 61 69 6e 20    if( isExplain 
19117 26 26 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 20  && nMem<10 ){.  
19118 20 20 20 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20      nMem = 10;. 
19119 20 20 20 7d 0a 0a 20 20 20 20 64 6f 20 7b 0a 20     }..    do {. 
1911a 20 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72       memset(zCsr
1911b 2c 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b  , 0, zEnd-zCsr);
1911c 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 30  .      nByte = 0
1911d 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61  ;.      allocSpa
1911e 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 4d  ce((char*)&p->aM
1911f 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 28  em, nMem*sizeof(
19120 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e  Mem), &zCsr, zEn
19121 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20  d, &nByte);.    
19122 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68    allocSpace((ch
19123 61 72 2a 29 26 70 2d 3e 61 56 61 72 2c 20 6e 56  ar*)&p->aVar, nV
19124 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20  ar*sizeof(Mem), 
19125 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42  &zCsr, zEnd, &nB
19126 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f  yte);.      allo
19127 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70  cSpace((char*)&p
19128 2d 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a 73 69  ->apArg, nArg*si
19129 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a 43 73  zeof(Mem*), &zCs
1912a 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29  r, zEnd, &nByte)
1912b 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61  ;.      allocSpa
1912c 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 7a  ce((char*)&p->az
1912d 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66  Var, nVar*sizeof
1912e 28 63 68 61 72 2a 29 2c 20 26 7a 43 73 72 2c 20  (char*), &zCsr, 
1912f 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20  zEnd, &nByte);. 
19130 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28       allocSpace(
19131 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 43 73 72  (char*)&p->apCsr
19132 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
19133 20 20 20 20 6e 43 75 72 73 6f 72 2a 73 69 7a 65      nCursor*size
19134 6f 66 28 56 64 62 65 43 75 72 73 6f 72 2a 29 2c  of(VdbeCursor*),
19135 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e   &zCsr, zEnd, &n
19136 42 79 74 65 0a 20 20 20 20 20 20 29 3b 0a 20 20  Byte.      );.  
19137 20 20 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b      if( nByte ){
19138 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 72 65  .        p->pFre
19139 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  e = sqlite3DbMal
1913a 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65  locRaw(db, nByte
1913b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1913c 20 7a 43 73 72 20 3d 20 70 2d 3e 70 46 72 65 65   zCsr = p->pFree
1913d 3b 0a 20 20 20 20 20 20 7a 45 6e 64 20 3d 20 26  ;.      zEnd = &
1913e 7a 43 73 72 5b 6e 42 79 74 65 5d 3b 0a 20 20 20  zCsr[nByte];.   
1913f 20 7d 77 68 69 6c 65 28 20 6e 42 79 74 65 20 26   }while( nByte &
19140 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  & !db->mallocFai
19141 6c 65 64 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 6e  led );..    p->n
19142 43 75 72 73 6f 72 20 3d 20 6e 43 75 72 73 6f 72  Cursor = nCursor
19143 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61  ;.    if( p->aVa
19144 72 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56  r ){.      p->nV
19145 61 72 20 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20  ar = nVar;.     
19146 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72   for(n=0; n<nVar
19147 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; n++){.        
19148 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73  p->aVar[n].flags
19149 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1914a 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e       p->aVar[n].
1914b 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d  db = db;.      }
1914c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1914d 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20  ->aMem ){.      
1914e 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20  p->aMem--;      
1914f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19150 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66  /* aMem[] goes f
19151 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20  rom 1..nMem */. 
19152 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e       p->nMem = n
19153 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
19154 20 20 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f       /*       no
19155 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31  t from 0..nMem-1
19156 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d   */.      for(n=
19157 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29  1; n<=nMem; n++)
19158 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65  {.        p->aMe
19159 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d  m[n].flags = MEM
1915a 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70  _Null;.        p
1915b 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64  ->aMem[n].db = d
1915c 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  b;.      }.    }
1915d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49  .  }.#ifdef SQLI
1915e 54 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 6e  TE_DEBUG.  for(n
1915f 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e  =1; n<p->nMem; n
19160 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
19161 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d   p->aMem[n].db==
19162 64 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  db );.  }.#endif
19163 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a  ..  p->pc = -1;.
19164 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
19165 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41  _OK;.  p->errorA
19166 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74  ction = OE_Abort
19167 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c  ;.  p->explain |
19168 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70  = isExplain;.  p
19169 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d  ->magic = VDBE_M
1916a 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e  AGIC_RUN;.  p->n
1916b 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d  Change = 0;.  p-
1916c 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20  >cacheCtr = 1;. 
1916d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65   p->minWriteFile
1916e 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20  Format = 255;.  
1916f 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20  p->iStatement = 
19170 30 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50  0;.#ifdef VDBE_P
19171 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69  ROFILE.  {.    i
19172 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
19173 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b  0; i<p->nOp; i++
19174 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b  ){.      p->aOp[
19175 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20  i].cnt = 0;.    
19176 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c    p->aOp[i].cycl
19177 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  es = 0;.    }.  
19178 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
19179 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 45 20 63  * Close a VDBE c
1917a 75 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 73  ursor and releas
1917b 65 20 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 72  e all the resour
1917c 63 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20  ces that cursor 
1917d 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 68  .** happens to h
1917e 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  old..*/.SQLITE_P
1917f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
19180 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f  te3VdbeFreeCurso
19181 72 28 56 64 62 65 20 2a 70 2c 20 56 64 62 65 43  r(Vdbe *p, VdbeC
19182 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69  ursor *pCx){.  i
19183 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20  f( pCx==0 ){.   
19184 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
19185 66 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20  f( pCx->pBt ){. 
19186 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
19187 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a  lose(pCx->pBt);.
19188 20 20 20 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e      /* The pCx->
19189 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20  pCursor will be 
1918a 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 61  close automatica
1918b 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 69 73 74  lly, if it exist
1918c 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65  s, by.    ** the
1918d 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a   call above. */.
1918e 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 78 2d    }else if( pCx-
1918f 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  >pCursor ){.    
19190 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
19191 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 75  eCursor(pCx->pCu
19192 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64  rsor);.  }.#ifnd
19193 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
19194 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
19195 28 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73  ( pCx->pVtabCurs
19196 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  or ){.    sqlite
19197 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
19198 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43 78  VtabCursor = pCx
19199 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20  ->pVtabCursor;. 
1919a 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33     const sqlite3
1919b 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
1919c 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b   = pCx->pModule;
1919d 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65  .    p->inVtabMe
1919e 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 76  thod = 1;.    (v
1919f 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
191a0 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 20  yOff(p->db);.   
191a1 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65   pModule->xClose
191a2 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  (pVtabCursor);. 
191a3 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
191a4 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3b  SafetyOn(p->db);
191a5 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65  .    p->inVtabMe
191a6 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 65  thod = 0;.  }.#e
191a7 6e 64 69 66 0a 20 20 69 66 28 20 21 70 43 78 2d  ndif.  if( !pCx-
191a8 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c  >ephemPseudoTabl
191a9 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
191aa 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43  DbFree(p->db, pC
191ab 78 2d 3e 70 44 61 74 61 29 3b 0a 20 20 7d 0a 7d  x->pData);.  }.}
191ac 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c  ../*.** Close al
191ad 6c 20 63 75 72 73 6f 72 73 20 65 78 63 65 70 74  l cursors except
191ae 20 66 6f 72 20 56 54 61 62 20 63 75 72 73 6f 72   for VTab cursor
191af 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65  s that are curre
191b0 6e 74 6c 79 0a 2a 2a 20 69 6e 20 75 73 65 2e 0a  ntly.** in use..
191b1 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
191b2 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78  loseAllCursorsEx
191b3 63 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28  ceptActiveVtabs(
191b4 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
191b5 69 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73  i;.  if( p->apCs
191b6 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
191b7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
191b8 43 75 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20  Cursor; i++){.  
191b9 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
191ba 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a   = p->apCsr[i];.
191bb 20 20 20 20 69 66 28 20 70 43 20 26 26 20 28 21      if( pC && (!
191bc 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20  p->inVtabMethod 
191bd 7c 7c 20 21 70 43 2d 3e 70 56 74 61 62 43 75 72  || !pC->pVtabCur
191be 73 6f 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71  sor) ){.      sq
191bf 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72  lite3VdbeFreeCur
191c0 73 6f 72 28 70 2c 20 70 43 29 3b 0a 20 20 20 20  sor(p, pC);.    
191c1 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20    p->apCsr[i] = 
191c2 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
191c3 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74  /*.** Clean up t
191c4 68 65 20 56 4d 20 61 66 74 65 72 20 65 78 65 63  he VM after exec
191c5 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ution..**.** Thi
191c6 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61  s routine will a
191c7 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f  utomatically clo
191c8 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20  se any cursors, 
191c9 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a  lists, and/or.**
191ca 20 73 6f 72 74 65 72 73 20 74 68 61 74 20 77 65   sorters that we
191cb 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49  re left open.  I
191cc 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74  t also deletes t
191cd 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20  he values of.** 
191ce 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65  variables in the
191cf 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a   aVar[] array..*
191d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c  /.static void Cl
191d1 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a  eanup(Vdbe *p){.
191d2 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74    int i;.  sqlit
191d3 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  e3 *db = p->db;.
191d4 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 63    Mem *pMem;.  c
191d5 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78  loseAllCursorsEx
191d6 63 65 70 74 41 63 74 69 76 65 56 74 61 62 73 28  ceptActiveVtabs(
191d7 70 29 3b 0a 20 20 66 6f 72 28 70 4d 65 6d 3d 26  p);.  for(pMem=&
191d8 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 69 3d 31 3b  p->aMem[1], i=1;
191d9 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b   i<=p->nMem; i++
191da 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 69  , pMem++){.    i
191db 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
191dc 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20   MEM_RowSet ){. 
191dd 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53       sqlite3RowS
191de 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e  etClear(pMem->u.
191df 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a  pRowSet);.    }.
191e0 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
191e1 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c  ag(pMem, MEM_Nul
191e2 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  l);.  }.  releas
191e3 65 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d  eMemArray(&p->aM
191e4 65 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b  em[1], p->nMem);
191e5 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78  .  if( p->contex
191e6 74 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71  tStack ){.    sq
191e7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
191e8 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 29  p->contextStack)
191e9 3b 0a 20 20 7d 0a 20 20 70 2d 3e 63 6f 6e 74 65  ;.  }.  p->conte
191ea 78 74 53 74 61 63 6b 20 3d 20 30 3b 0a 20 20 70  xtStack = 0;.  p
191eb 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65  ->contextStackDe
191ec 70 74 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f  pth = 0;.  p->co
191ed 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 20 3d 20  ntextStackTop = 
191ee 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  0;.  sqlite3DbFr
191ef 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
191f0 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67  g);.  p->zErrMsg
191f1 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75   = 0;.  p->pResu
191f2 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  ltSet = 0;.}../*
191f3 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62  .** Set the numb
191f4 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c  er of result col
191f5 75 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62  umns that will b
191f6 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  e returned by th
191f7 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d  is SQL.** statem
191f8 65 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77  ent. This is now
191f9 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20   set at compile 
191fa 74 69 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61  time, rather tha
191fb 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63  n during.** exec
191fc 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62  ution of the vdb
191fd 65 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61  e program so tha
191fe 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
191ff 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20  _count() can.** 
19200 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20  be called on an 
19201 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65  SQL statement be
19202 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65  fore sqlite3_ste
19203 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  p()..*/.SQLITE_P
19204 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
19205 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
19206 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e  s(Vdbe *p, int n
19207 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65  ResColumn){.  Me
19208 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69  m *pColName;.  i
19209 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20  nt n;.  sqlite3 
1920a 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20  *db = p->db;..  
1920b 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28  releaseMemArray(
1920c 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e  p->aColName, p->
1920d 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41  nResColumn*COLNA
1920e 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33  ME_N);.  sqlite3
1920f 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43  DbFree(db, p->aC
19210 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e  olName);.  n = n
19211 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d  ResColumn*COLNAM
19212 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 6f  E_N;.  p->nResCo
19213 6c 75 6d 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d  lumn = nResColum
19214 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65  n;.  p->aColName
19215 20 3d 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28 4d   = pColName = (M
19216 65 6d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  em*)sqlite3DbMal
19217 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
19218 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b 0a 20 20 69  of(Mem)*n );.  i
19219 66 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d 3d  f( p->aColName==
1921a 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68  0 ) return;.  wh
1921b 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b 0a  ile( n-- > 0 ){.
1921c 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c      pColName->fl
1921d 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
1921e 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 64 62      pColName->db
1921f 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70 43   = p->db;.    pC
19220 6f 6c 4e 61 6d 65 2b 2b 3b 0a 20 20 7d 0a 7d 0a  olName++;.  }.}.
19221 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e  ./*.** Set the n
19222 61 6d 65 20 6f 66 20 74 68 65 20 69 64 78 27 74  ame of the idx't
19223 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 72  h column to be r
19224 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 53  eturned by the S
19225 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  QL statement..**
19226 20 7a 4e 61 6d 65 20 6d 75 73 74 20 62 65 20 61   zName must be a
19227 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 75   pointer to a nu
19228 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  l terminated str
19229 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ing..**.** This 
1922a 63 61 6c 6c 20 6d 75 73 74 20 62 65 20 6d 61 64  call must be mad
1922b 65 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74  e after a call t
1922c 6f 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  o sqlite3VdbeSet
1922d 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a 2a 0a 2a 2a  NumCols()..**.**
1922e 20 54 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d   The final param
1922f 65 74 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73 74  eter, xDel, must
19230 20 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54   be one of SQLIT
19231 45 5f 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49 54  E_DYNAMIC, SQLIT
19232 45 5f 53 54 41 54 49 43 0a 2a 2a 20 6f 72 20 53  E_STATIC.** or S
19233 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e  QLITE_TRANSIENT.
19234 20 49 66 20 69 74 20 69 73 20 53 51 4c 49 54 45   If it is SQLITE
19235 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 74  _DYNAMIC, then t
19236 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  he buffer pointe
19237 64 0a 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d 65  d.** to by zName
19238 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 62   will be freed b
19239 79 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  y sqlite3DbFree(
1923a 29 20 77 68 65 6e 20 74 68 65 20 76 64 62 65 20  ) when the vdbe 
1923b 69 73 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2f  is destroyed..*/
1923c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1923d 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53  int sqlite3VdbeS
1923e 65 74 43 6f 6c 4e 61 6d 65 28 0a 20 20 56 64 62  etColName(.  Vdb
1923f 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
19240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19241 20 56 64 62 65 20 62 65 69 6e 67 20 63 6f 6e 66   Vdbe being conf
19242 69 67 75 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  igured */.  int 
19243 69 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  idx,            
19244 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19245 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
19246 7a 4e 61 6d 65 20 61 70 70 6c 69 65 73 20 74 6f  zName applies to
19247 20 2a 2f 0a 20 20 69 6e 74 20 76 61 72 2c 20 20   */.  int var,  
19248 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19249 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
1924a 20 74 68 65 20 43 4f 4c 4e 41 4d 45 5f 2a 20 63   the COLNAME_* c
1924b 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 63 6f  onstants */.  co
1924c 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
1924d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1924e 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66  * Pointer to buf
1924f 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e  fer containing n
19250 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ame */.  void (*
19251 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20  xDel)(void*)    
19252 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
19253 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73  ory management s
19254 74 72 61 74 65 67 79 20 66 6f 72 20 7a 4e 61 6d  trategy for zNam
19255 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
19256 3b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d  ;.  Mem *pColNam
19257 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78  e;.  assert( idx
19258 3c 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 29  <p->nResColumn )
19259 3b 0a 20 20 61 73 73 65 72 74 28 20 76 61 72 3c  ;.  assert( var<
1925a 43 4f 4c 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20 69  COLNAME_N );.  i
1925b 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  f( p->db->malloc
1925c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 61 73  Failed ){.    as
1925d 73 65 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c 20  sert( !zName || 
1925e 78 44 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e  xDel!=SQLITE_DYN
1925f 41 4d 49 43 20 29 3b 0a 20 20 20 20 72 65 74 75  AMIC );.    retu
19260 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
19261 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
19262 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29 3b  ->aColName!=0 );
19263 0a 20 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26 28  .  pColName = &(
19264 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78 2b  p->aColName[idx+
19265 76 61 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d  var*p->nResColum
19266 6e 5d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  n]);.  rc = sqli
19267 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
19268 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d 65  (pColName, zName
19269 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
1926a 38 2c 20 78 44 65 6c 29 3b 0a 20 20 61 73 73 65  8, xDel);.  asse
1926b 72 74 28 20 72 63 21 3d 30 20 7c 7c 20 21 7a 4e  rt( rc!=0 || !zN
1926c 61 6d 65 20 7c 7c 20 28 70 43 6f 6c 4e 61 6d 65  ame || (pColName
1926d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 54 65 72 6d  ->flags&MEM_Term
1926e 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  )!=0 );.  return
1926f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20   rc;.}../*.** A 
19270 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72  read or write tr
19271 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6f 72  ansaction may or
19272 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 63 74 69   may not be acti
19273 76 65 20 6f 6e 20 64 61 74 61 62 61 73 65 20 68  ve on database h
19274 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 49 66 20  andle.** db. If 
19275 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
19276 20 61 63 74 69 76 65 2c 20 63 6f 6d 6d 69 74 20   active, commit 
19277 69 74 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  it. If there is 
19278 61 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e 73  a.** write-trans
19279 61 63 74 69 6f 6e 20 73 70 61 6e 6e 69 6e 67 20  action spanning 
1927a 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61  more than one da
1927b 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69  tabase file, thi
1927c 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 61 6b  s routine.** tak
1927d 65 73 20 63 61 72 65 20 6f 66 20 74 68 65 20 6d  es care of the m
1927e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 74 72  aster journal tr
1927f 69 63 6b 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  ickery..*/.stati
19280 63 20 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69 74  c int vdbeCommit
19281 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64  (sqlite3 *db, Vd
19282 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  be *p){.  int i;
19283 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d 20  .  int nTrans = 
19284 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  0;  /* Number of
19285 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 20   databases with 
19286 61 6e 20 61 63 74 69 76 65 20 77 72 69 74 65 2d  an active write-
19287 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
19288 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
19289 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 65 65 64 58  _OK;.  int needX
1928a 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a 0a 20 20 2f  commit = 0;..  /
1928b 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61  * Before doing a
1928c 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61  nything else, ca
1928d 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 63  ll the xSync() c
1928e 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a  allback for any.
1928f 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64    ** virtual mod
19290 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 74  ule tables writt
19291 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73  en in this trans
19292 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73  action. This has
19293 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65   to.  ** be done
19294 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e   before determin
19295 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d 61  ing whether a ma
19296 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
19297 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69  e is .  ** requi
19298 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63  red, as an xSync
19299 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20  () callback may 
1929a 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 20  add an attached 
1929b 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f  database.  ** to
1929c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1929d 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ..  */.  rc = sq
1929e 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 62  lite3VtabSync(db
1929f 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  , &p->zErrMsg);.
192a0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
192a1 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
192a2 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
192a3 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d  This loop determ
192a4 69 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 20  ines (a) if the 
192a5 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75  commit hook shou
192a6 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e  ld be invoked an
192a7 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d  d.  ** (b) how m
192a8 61 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  any database fil
192a9 65 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69  es have open wri
192aa 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c  te transactions,
192ab 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75   not .  ** inclu
192ac 64 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61  ding the temp da
192ad 74 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 69  tabase. (b) is i
192ae 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65  mportant because
192af 20 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20   if more than . 
192b0 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65   ** one database
192b1 20 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65   file has an ope
192b2 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  n write transact
192b3 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f  ion, a master jo
192b4 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20  urnal.  ** file 
192b5 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  is required for 
192b6 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74  an atomic commit
192b7 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d  ..  */ .  for(i=
192b8 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
192b9 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a  +){ .    Btree *
192ba 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
192bb 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71  .pBt;.    if( sq
192bc 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
192bd 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20  ans(pBt) ){.    
192be 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20    needXcommit = 
192bf 31 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d  1;.      if( i!=
192c0 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20  1 ) nTrans++;.  
192c1 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
192c2 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 77   there are any w
192c3 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
192c4 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65  s at all, invoke
192c5 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b   the commit hook
192c6 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63   */.  if( needXc
192c7 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f  ommit && db->xCo
192c8 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a  mmitCallback ){.
192c9 20 20 20 20 61 73 73 65 72 74 28 20 28 64 62 2d      assert( (db-
192ca 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
192cb 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 20 29  CommitBusy)==0 )
192cc 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  ;.    db->flags 
192cd 7c 3d 20 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 74  |= SQLITE_Commit
192ce 42 75 73 79 3b 0a 20 20 20 20 28 76 6f 69 64 29  Busy;.    (void)
192cf 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
192d0 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 64  (db);.    rc = d
192d1 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61  b->xCommitCallba
192d2 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72  ck(db->pCommitAr
192d3 67 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  g);.    (void)sq
192d4 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
192d5 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  );.    db->flags
192d6 20 26 3d 20 7e 53 51 4c 49 54 45 5f 43 6f 6d 6d   &= ~SQLITE_Comm
192d7 69 74 42 75 73 79 3b 0a 20 20 20 20 69 66 28 20  itBusy;.    if( 
192d8 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  rc ){.      retu
192d9 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  rn SQLITE_CONSTR
192da 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  AINT;.    }.  }.
192db 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65  .  /* The simple
192dc 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20   case - no more 
192dd 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  than one databas
192de 65 20 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e  e file (not coun
192df 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45  ting the.  ** TE
192e0 4d 50 20 64 61 74 61 62 61 73 65 29 20 68 61 73  MP database) has
192e1 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61   a transaction a
192e2 63 74 69 76 65 2e 20 20 20 54 68 65 72 65 20 69  ctive.   There i
192e3 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68  s no need for th
192e4 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f  e.  ** master-jo
192e5 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  urnal..  **.  **
192e6 20 49 66 20 74 68 65 20 72 65 74 75 72 6e 20 76   If the return v
192e7 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42  alue of sqlite3B
192e8 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
192e9 29 20 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67  ) is a zero leng
192ea 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20  th.  ** string, 
192eb 69 74 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69  it means the mai
192ec 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d  n database is :m
192ed 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70  emory: or a temp
192ee 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a   file.  In .  **
192ef 20 74 68 61 74 20 63 61 73 65 20 77 65 20 64 6f   that case we do
192f0 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f   not support ato
192f1 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63  mic multi-file c
192f2 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74  ommits, so use t
192f3 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20  he .  ** simple 
192f4 63 61 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20  case then too.. 
192f5 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c   */.  if( 0==sql
192f6 69 74 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c  ite3Strlen30(sql
192f7 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65  ite3BtreeGetFile
192f8 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e  name(db->aDb[0].
192f9 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 61  pBt)).   || nTra
192fa 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 66  ns<=1.  ){.    f
192fb 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
192fc 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e  TE_OK && i<db->n
192fd 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
192fe 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
192ff 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
19300 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20     if( pBt ){.  
19301 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
19302 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
19303 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20  seOne(pBt, 0);. 
19304 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
19305 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d    /* Do the comm
19306 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64  it only if all d
19307 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73 73  atabases success
19308 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70  fully complete p
19309 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20  hase 1. .    ** 
1930a 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74  If one of the Bt
1930b 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  reeCommitPhaseOn
1930c 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c  e() calls fails,
1930d 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   this indicates 
1930e 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72  an.    ** IO err
1930f 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e  or while deletin
19310 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20  g or truncating 
19311 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  a journal file. 
19312 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a  It is unlikely,.
19313 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64      ** but could
19314 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73   happen. In this
19315 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72   case abandon pr
19316 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74  ocessing and ret
19317 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20  urn the error.. 
19318 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d     */.    for(i=
19319 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
1931a 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69   && i<db->nDb; i
1931b 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65  ++){.      Btree
1931c 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
1931d 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
1931e 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ( pBt ){.       
1931f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
19320 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
19321 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  (pBt);.      }. 
19322 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
19323 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
19324 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43      sqlite3VtabC
19325 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d  ommit(db);.    }
19326 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63  .  }..  /* The c
19327 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68  omplex case - Th
19328 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66  ere is a multi-f
19329 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61  ile write-transa
1932a 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20  ction active..  
1932b 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65 73  ** This requires
1932c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
1932d 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65  l file to ensure
1932e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1932f 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74   is.  ** committ
19330 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a  ed atomicly..  *
19331 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
19332 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65  _OMIT_DISKIO.  e
19333 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
19334 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d  _vfs *pVfs = db-
19335 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e  >pVfs;.    int n
19336 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20  eedSync = 0;.   
19337 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d   char *zMaster =
19338 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61   0;   /* File-na
19339 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65  me for the maste
1933a 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20  r journal */.   
1933b 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61   char const *zMa
1933c 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33  inFile = sqlite3
1933d 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
1933e 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29  (db->aDb[0].pBt)
1933f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
19340 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b  le *pMaster = 0;
19341 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20  .    i64 offset 
19342 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73  = 0;.    int res
19343 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74  ;..    /* Select
19344 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
19345 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20  l file name */. 
19346 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33     do {.      u3
19347 32 20 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 20  2 iRandom;.     
19348 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
19349 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  b, zMaster);.   
1934a 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f     sqlite3_rando
1934b 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61  mness(sizeof(iRa
1934c 6e 64 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29  ndom), &iRandom)
1934d 3b 0a 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20  ;.      zMaster 
1934e 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
1934f 28 64 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22  (db, "%s-mj%08X"
19350 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 61  , zMainFile, iRa
19351 6e 64 6f 6d 26 30 78 37 66 66 66 66 66 66 66 29  ndom&0x7fffffff)
19352 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4d 61  ;.      if( !zMa
19353 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20  ster ){.        
19354 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
19355 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
19356 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
19357 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d  sAccess(pVfs, zM
19358 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43  aster, SQLITE_AC
19359 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65  CESS_EXISTS, &re
1935a 73 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20  s);.    }while( 
1935b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1935c 20 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20   res );.    if( 
1935d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1935e 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74  .      /* Open t
1935f 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
19360 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d  l. */.      rc =
19361 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61   sqlite3OsOpenMa
19362 6c 6c 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74  lloc(pVfs, zMast
19363 65 72 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20  er, &pMaster, . 
19364 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
19365 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53  OPEN_READWRITE|S
19366 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
19367 45 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c  E|.          SQL
19368 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
19369 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  VE|SQLITE_OPEN_M
1936a 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30  ASTER_JOURNAL, 0
1936b 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a  .      );.    }.
1936c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1936d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
1936e 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1936f 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20   zMaster);.     
19370 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
19371 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65  }. .    /* Write
19372 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63   the name of eac
19373 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  h database file 
19374 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  in the transacti
19375 6f 6e 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a  on into the new.
19376 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f      ** master jo
19377 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61  urnal file. If a
19378 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61  n error occurs a
19379 74 20 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f  t this point clo
1937a 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65  se.    ** and de
1937b 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  lete the master 
1937c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c  journal file. Al
1937d 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c  l the individual
1937e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20   journal files. 
1937f 20 20 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65     ** still have
19380 20 27 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d   'null' as the m
19381 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f  aster journal po
19382 69 6e 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77  inter, so they w
19383 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20  ill roll.    ** 
19384 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74  back independent
19385 6c 79 20 69 66 20 61 20 66 61 69 6c 75 72 65 20  ly if a failure 
19386 6f 63 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20  occurs..    */. 
19387 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
19388 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
19389 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
1938a 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1938b 20 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29        if( i==1 )
1938c 20 63 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20   continue;   /* 
1938d 49 67 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 20  Ignore the TEMP 
1938e 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20  database */.    
1938f 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72    if( sqlite3Btr
19390 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29  eeIsInTrans(pBt)
19391 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72   ){.        char
19392 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20   const *zFile = 
19393 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a  sqlite3BtreeGetJ
19394 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b  ournalname(pBt);
19395 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 69  .        if( zFi
19396 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  le[0]==0 ) conti
19397 6e 75 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20  nue;  /* Ignore 
19398 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73  :memory: databas
19399 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  es */.        if
1939a 28 20 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21  ( !needSync && !
1939b 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63  sqlite3BtreeSync
1939c 44 69 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b  Disabled(pBt) ){
1939d 0a 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53  .          needS
1939e 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ync = 1;.       
1939f 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   }.        rc = 
193a0 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
193a1 4d 61 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73  Master, zFile, s
193a2 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
193a3 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29  File)+1, offset)
193a4 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74  ;.        offset
193a5 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65   += sqlite3Strle
193a6 6e 33 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 20  n30(zFile)+1;.  
193a7 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
193a8 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
193a9 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c       sqlite3OsCl
193aa 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29  oseFree(pMaster)
193ab 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
193ac 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
193ad 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20  , zMaster, 0);. 
193ae 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
193af 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74  DbFree(db, zMast
193b0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  er);.          r
193b1 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
193b2 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
193b3 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74  }..    /* Sync t
193b4 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
193b5 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 49  l file. If the I
193b6 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20  OCAP_SEQUENTIAL 
193b7 64 65 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 6c  device.    ** fl
193b8 61 67 20 69 73 20 73 65 74 20 74 68 69 73 20 69  ag is set this i
193b9 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a  s not required..
193ba 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
193bb 65 65 64 53 79 6e 63 20 0a 20 20 20 20 20 26 26  eedSync .     &&
193bc 20 30 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65   0==(sqlite3OsDe
193bd 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
193be 69 63 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c  ics(pMaster)&SQL
193bf 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e  ITE_IOCAP_SEQUEN
193c0 54 49 41 4c 29 0a 20 20 20 20 20 26 26 20 53 51  TIAL).     && SQ
193c1 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
193c2 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61  qlite3OsSync(pMa
193c3 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e  ster, SQLITE_SYN
193c4 43 5f 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 29  C_NORMAL)).    )
193c5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  {.      sqlite3O
193c6 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74  sCloseFree(pMast
193c7 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  er);.      sqlit
193c8 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c  e3OsDelete(pVfs,
193c9 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20   zMaster, 0);.  
193ca 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
193cb 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  e(db, zMaster);.
193cc 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
193cd 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
193ce 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66  ync all the db f
193cf 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e  iles involved in
193d0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
193d1 2e 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a  . The same call.
193d2 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20      ** sets the 
193d3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70  master journal p
193d4 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69  ointer in each i
193d5 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61  ndividual journa
193d6 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20  l. If.    ** an 
193d7 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72  error occurs her
193d8 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65  e, do not delete
193d9 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
193da 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a  nal file..    **
193db 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65  .    ** If the e
193dc 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
193dd 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  ng the first cal
193de 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69  l to.    ** sqli
193df 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
193e0 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74  aseOne(), then t
193e1 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65  here is a chance
193e2 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a   that the.    **
193e3 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
193e4 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70  file will be orp
193e5 68 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61  haned. But we ca
193e6 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a  nnot delete it,.
193e7 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74      ** in case t
193e8 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
193e9 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20  l file name was 
193ea 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
193eb 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20   journal.    ** 
193ec 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20  file before the 
193ed 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64  failure occurred
193ee 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
193ef 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
193f0 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  _OK && i<db->nDb
193f1 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42  ; i++){ .      B
193f2 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
193f3 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
193f4 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
193f5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
193f6 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
193f7 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65  eOne(pBt, zMaste
193f8 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
193f9 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43  }.    sqlite3OsC
193fa 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72  loseFree(pMaster
193fb 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
193fc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
193fd 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
193fe 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  db, zMaster);.  
193ff 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
19400 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c     }..    /* Del
19401 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ete the master j
19402 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69  ournal file. Thi
19403 73 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72  s commits the tr
19404 61 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72  ansaction. After
19405 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68  .    ** doing th
19406 69 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  is the directory
19407 20 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e   is synced again
19408 20 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69   before any indi
19409 76 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72  vidual.    ** tr
1940a 61 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20  ansaction files 
1940b 61 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20  are deleted..   
1940c 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
1940d 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
1940e 73 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a  s, zMaster, 1);.
1940f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
19410 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  e(db, zMaster);.
19411 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b      zMaster = 0;
19412 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
19413 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
19414 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c      }..    /* Al
19415 6c 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65  l files and dire
19416 63 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72  ctories have alr
19417 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64  eady been synced
19418 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  , so the followi
19419 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20  ng.    ** calls 
1941a 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  to sqlite3BtreeC
1941b 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20  ommitPhaseTwo() 
1941c 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67  are only closing
1941d 20 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a   files and.    *
1941e 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72  * deleting or tr
1941f 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c  uncating journal
19420 73 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20  s. If something 
19421 67 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65  goes wrong while
19422 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20  .    ** this is 
19423 68 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e  happening we don
19424 27 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20  't really care. 
19425 54 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66  The integrity of
19426 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e   the.    ** tran
19427 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61  saction is alrea
19428 64 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62  dy guaranteed, b
19429 75 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63  ut some stray 'c
1942a 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20  old' journals.  
1942b 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e    ** may be lyin
1942c 67 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e  g around. Return
1942d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  ing an error cod
1942e 65 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74  e won't help mat
1942f 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ters..    */.   
19430 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74   disable_simulat
19431 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a  ed_io_errors();.
19432 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
19433 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
19434 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
19435 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20  b->nDb; i++){ . 
19436 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
19437 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
19438 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20  ;.      if( pBt 
19439 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1943a 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
1943b 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20  seTwo(pBt);.    
1943c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
1943d 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
1943e 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62  lloc();.    enab
1943f 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f  le_simulated_io_
19440 65 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73  errors();..    s
19441 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
19442 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  (db);.  }.#endif
19443 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
19444 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f  ../* .** This ro
19445 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61  utine checks tha
19446 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63  t the sqlite3.ac
19447 74 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e  tiveVdbeCnt coun
19448 74 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61  t variable.** ma
19449 74 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72  tches the number
1944a 20 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 68   of vdbe's in th
1944b 65 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70  e list sqlite3.p
1944c 56 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a  Vdbe that are.**
1944d 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76   currently activ
1944e 65 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20  e. An assertion 
1944f 66 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f  fails if the two
19450 20 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d   counts do not m
19451 61 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73  atch..** This is
19452 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c   an internal sel
19453 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69  f-check only - i
19454 74 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65  t is not an esse
19455 6e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67  ntial processing
19456 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20  .** step..**.** 
19457 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20  This is a no-op 
19458 69 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 66  if NDEBUG is def
19459 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ined..*/.#ifndef
1945a 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 76   NDEBUG.static v
1945b 6f 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 56  oid checkActiveV
1945c 64 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a  dbeCnt(sqlite3 *
1945d 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a  db){.  Vdbe *p;.
1945e 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
1945f 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b   int nWrite = 0;
19460 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65  .  p = db->pVdbe
19461 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a  ;.  while( p ){.
19462 20 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63      if( p->magic
19463 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  ==VDBE_MAGIC_RUN
19464 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a   && p->pc>=0 ){.
19465 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
19466 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e     if( p->readOn
19467 6c 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b  ly==0 ) nWrite++
19468 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20  ;.    }.    p = 
19469 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  p->pNext;.  }.  
1946a 61 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d  assert( cnt==db-
1946b 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29  >activeVdbeCnt )
1946c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 69  ;.  assert( nWri
1946d 74 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62  te==db->writeVdb
1946e 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a  eCnt );.}.#else.
1946f 23 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74  #define checkAct
19470 69 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65  iveVdbeCnt(x).#e
19471 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  ndif../*.** For 
19472 65 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74  every Btree that
19473 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e   in database con
19474 6e 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68  nection db which
19475 20 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f   .** has been mo
19476 64 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f  dified, "trip" o
19477 72 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63  r invalidate eac
19478 68 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74  h cursor in.** t
19479 68 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 20  hat Btree might 
1947a 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69  have been modifi
1947b 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  ed so that the c
1947c 75 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76  ursor.** can nev
1947d 65 72 20 62 65 20 75 73 65 64 20 61 67 61 69 6e  er be used again
1947e 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  .  This happens 
1947f 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a  when a rollback.
19480 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20  *** occurs.  We 
19481 68 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c  have to trip all
19482 20 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f   the other curso
19483 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73  rs, even.** curs
19484 6f 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d  or from other VM
19485 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64  s in different d
19486 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
19487 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20  ons,.** so that 
19488 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79  none of them try
19489 20 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 61   to use the data
1948a 20 61 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a   at which they.*
1948b 2a 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20  * were pointing 
1948c 61 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61  and which now ma
1948d 79 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e  y have been chan
1948e 67 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68  ged due.** to th
1948f 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  e rollback..**.*
19490 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20  * Remember that 
19491 61 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64  a rollback can d
19492 65 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d  elete tables com
19493 70 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f  plete and.** reo
19494 72 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20  rder rootpages. 
19495 20 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75   So it is not su
19496 66 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f  fficient just to
19497 20 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61   save.** the sta
19498 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72  te of the cursor
19499 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e  .  We have to in
1949a 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72  validate the cur
1949b 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69  sor.** so that i
1949c 74 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20  t is never used 
1949d 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  again..*/.static
1949e 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65   void invalidate
1949f 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65  CursorsOnModifie
194a0 64 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20  dBtrees(sqlite3 
194a1 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
194a2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
194a3 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42  nDb; i++){.    B
194a4 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44  tree *p = db->aD
194a5 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
194a6 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 74  ( p && sqlite3Bt
194a7 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20  reeIsInTrans(p) 
194a8 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
194a9 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73  BtreeTripAllCurs
194aa 6f 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42  ors(p, SQLITE_AB
194ab 4f 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ORT);.    }.  }.
194ac 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
194ad 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 74  Vdbe passed as t
194ae 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
194af 74 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65  t opened a state
194b0 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e  ment-transaction
194b1 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e 6f  ,.** close it no
194b2 77 2e 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 20  w. Argument eOp 
194b3 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 53  must be either S
194b4 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
194b5 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 4e  K or.** SAVEPOIN
194b6 54 5f 52 45 4c 45 41 53 45 2e 20 49 66 20 69 74  T_RELEASE. If it
194b7 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f   is SAVEPOINT_RO
194b8 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 65  LLBACK, then the
194b9 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72   statement.** tr
194ba 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c  ansaction is rol
194bb 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 65 4f 70  led back. If eOp
194bc 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45   is SAVEPOINT_RE
194bd 4c 45 41 53 45 2c 20 74 68 65 6e 20 74 68 65 20  LEASE, then the 
194be 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72  .** statement tr
194bf 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
194c0 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  mtted..**.** If 
194c1 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
194c2 72 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f  rs, an SQLITE_IO
194c3 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f  ERR_XXX error co
194c4 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  de is returned. 
194c5 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53 51  .** Otherwise SQ
194c6 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49  LITE_OK..*/.SQLI
194c7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
194c8 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53  qlite3VdbeCloseS
194c9 74 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 70  tatement(Vdbe *p
194ca 2c 20 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 71  , int eOp){.  sq
194cb 6c 69 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 20  lite3 *const db 
194cc 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72  = p->db;.  int r
194cd 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
194ce 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65   if( p->iStateme
194cf 6e 74 20 26 26 20 64 62 2d 3e 6e 53 74 61 74 65  nt && db->nState
194d0 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  ment ){.    int 
194d1 69 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  i;.    const int
194d2 20 69 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d   iSavepoint = p-
194d3 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0a 0a  >iStatement-1;..
194d4 20 20 20 20 61 73 73 65 72 74 28 20 65 4f 70 3d      assert( eOp=
194d5 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
194d6 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41 56 45  ACK || eOp==SAVE
194d7 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a  POINT_RELEASE);.
194d8 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
194d9 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a  nStatement>0 );.
194da 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69      assert( p->i
194db 53 74 61 74 65 6d 65 6e 74 3d 3d 28 64 62 2d 3e  Statement==(db->
194dc 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e  nStatement+db->n
194dd 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a 0a 20  Savepoint) );.. 
194de 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
194df 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
194e0 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51      int rc2 = SQ
194e1 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 42  LITE_OK;.      B
194e2 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
194e3 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
194e4 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
194e5 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41       if( eOp==SA
194e6 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
194e7 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
194e8 32 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  2 = sqlite3Btree
194e9 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53  Savepoint(pBt, S
194ea 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
194eb 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a  K, iSavepoint);.
194ec 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
194ed 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54    if( rc2==SQLIT
194ee 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
194ef 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42    rc2 = sqlite3B
194f0 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42  treeSavepoint(pB
194f1 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  t, SAVEPOINT_REL
194f2 45 41 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74  EASE, iSavepoint
194f3 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
194f4 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
194f5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
194f6 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
194f7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
194f8 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6e 53      }.    db->nS
194f9 74 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20 20 20 20  tatement--;.    
194fa 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20  p->iStatement = 
194fb 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
194fc 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
194fd 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
194fe 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20 56  led the when a V
194ff 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c  DBE tries to hal
19500 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45 0a  t.  If the VDBE.
19501 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61 6e  ** has made chan
19502 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 75  ges and is in au
19503 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 74  tocommit mode, t
19504 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 65  hen commit those
19505 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 66  .** changes.  If
19506 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6e   a rollback is n
19507 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 74  eeded, then do t
19508 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  he rollback..**.
19509 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1950a 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20  is the only way 
1950b 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 61 74  to move the stat
1950c 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a 2a  e of a VM from.*
1950d 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 52  * SQLITE_MAGIC_R
1950e 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47  UN to SQLITE_MAG
1950f 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 20  IC_HALT.  It is 
19510 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 63  harmless to.** c
19511 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 4d  all this on a VM
19512 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20   that is in the 
19513 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c  SQLITE_MAGIC_HAL
19514 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52  T state..**.** R
19515 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
19516 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 6f 6d  ode.  If the com
19517 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 6f  mit could not co
19518 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 20 6f  mplete because o
19519 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e  f.** lock conten
1951a 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 4c  tion, return SQL
1951b 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 53 51  ITE_BUSY.  If SQ
1951c 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
1951d 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 61  urned, it.** mea
1951e 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 69 64  ns the close did
1951f 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 20   not happen and 
19520 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 70 65  needs to be repe
19521 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
19522 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19523 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 65  te3VdbeHalt(Vdbe
19524 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20   *p){.  int rc; 
19525 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19526 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
19527 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 69 65  to store transie
19528 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 73 20  nt return codes 
19529 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
1952a 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20   = p->db;..  /* 
1952b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
1952c 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63  ntains the logic
1952d 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
1952e 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   if a statement 
1952f 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  or.  ** transact
19530 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d  ion will be comm
19531 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20  itted or rolled 
19532 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74  back as a result
19533 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 65   of the.  ** exe
19534 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 76  cution of this v
19535 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20  irtual machine. 
19536 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6e  .  **.  ** If an
19537 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  y of the followi
19538 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72 3a  ng errors occur:
19539 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53  .  **.  **     S
1953a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a  QLITE_NOMEM.  **
1953b 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52       SQLITE_IOER
1953c 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54  R.  **     SQLIT
1953d 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  E_FULL.  **     
1953e 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
1953f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20  .  **.  ** Then 
19540 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63  the internal cac
19541 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65  he might have be
19542 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e  en left in an in
19543 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20  consistent.  ** 
19544 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64 20  state.  We need 
19545 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  to rollback the 
19546 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
19547 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 20  ction, if there 
19548 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20  is.  ** one, or 
19549 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 61  the complete tra
1954a 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 72  nsaction if ther
1954b 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e  e is no statemen
1954c 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20  t transaction.. 
1954d 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 62   */..  if( p->db
1954e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1954f 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
19550 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
19551 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72    closeAllCursor
19552 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74 61  sExceptActiveVta
19553 62 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e  bs(p);.  if( p->
19554 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49  magic!=VDBE_MAGI
19555 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74  C_RUN ){.    ret
19556 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
19557 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65   }.  checkActive
19558 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20 20  VdbeCnt(db);..  
19559 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 20  /* No commit or 
1955a 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64 20  rollback needed 
1955b 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 6e  if the program n
1955c 65 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f 0a  ever started */.
1955d 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29    if( p->pc>=0 )
1955e 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20 20  {.    int mrc;  
1955f 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72 6f   /* Primary erro
19560 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e 72  r code from p->r
19561 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 53 74  c */.    int eSt
19562 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a 20  atementOp = 0;. 
19563 20 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c     int isSpecial
19564 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20  Error;          
19565 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65    /* Set to true
19566 20 69 66 20 61 20 27 73 70 65 63 69 61 6c 27 20   if a 'special' 
19567 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a  error */..    /*
19568 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 73   Lock all btrees
19569 20 75 73 65 64 20 62 79 20 74 68 65 20 73 74 61   used by the sta
1956a 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71  tement */.    sq
1956b 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
1956c 72 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d  rrayEnter(&p->aM
1956d 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  utex);..    /* C
1956e 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20  heck for one of 
1956f 74 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f  the special erro
19570 72 73 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20  rs */.    mrc = 
19571 70 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20  p->rc & 0xff;.  
19572 20 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72    isSpecialError
19573 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   = mrc==SQLITE_N
19574 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c  OMEM || mrc==SQL
19575 49 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20  ITE_IOERR.      
19576 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
19577 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e  | mrc==SQLITE_IN
19578 54 45 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d  TERRUPT || mrc==
19579 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20  SQLITE_FULL;.   
1957a 20 69 66 28 20 69 73 53 70 65 63 69 61 6c 45 72   if( isSpecialEr
1957b 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ror ){.      /* 
1957c 49 66 20 74 68 65 20 71 75 65 72 79 20 77 61 73  If the query was
1957d 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e   read-only, we n
1957e 65 65 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61  eed do no rollba
1957f 63 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72  ck at all. Other
19580 77 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70  wise,.      ** p
19581 72 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20  roceed with the 
19582 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67  special handling
19583 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
19584 20 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c   if( !p->readOnl
19585 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45  y || mrc!=SQLITE
19586 5f 49 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20  _INTERRUPT ){.  
19587 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d        if( p->rc=
19588 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c  =SQLITE_IOERR_BL
19589 4f 43 4b 45 44 20 26 26 20 70 2d 3e 75 73 65 73  OCKED && p->uses
1958a 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  StmtJournal ){. 
1958b 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d           eStatem
1958c 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e  entOp = SAVEPOIN
1958d 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20  T_ROLLBACK;.    
1958e 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51        p->rc = SQ
1958f 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20  LITE_BUSY;.     
19590 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6d 72     }else if( (mr
19591 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
19592 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46  || mrc==SQLITE_F
19593 55 4c 4c 29 0a 20 20 20 20 20 20 20 20 20 20 20  ULL).           
19594 20 20 20 20 20 20 20 20 26 26 20 70 2d 3e 75 73          && p->us
19595 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b  esStmtJournal ){
19596 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74  .          eStat
19597 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f  ementOp = SAVEPO
19598 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20  INT_ROLLBACK;.  
19599 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1959a 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65         /* We are
1959b 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20   forced to roll 
1959c 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20  back the active 
1959d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66  transaction. Bef
1959e 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20  ore doing.      
1959f 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74      ** so, abort
195a0 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65   any other state
195a1 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c  ments this handl
195a2 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20  e currently has 
195a3 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20  active..        
195a4 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69    */.          i
195a5 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73  nvalidateCursors
195a6 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73  OnModifiedBtrees
195a7 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
195a8 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41  sqlite3RollbackA
195a9 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ll(db);.        
195aa 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61    sqlite3CloseSa
195ab 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20  vepoints(db);.  
195ac 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f          db->auto
195ad 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  Commit = 1;.    
195ae 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
195af 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66    }.  .    /* If
195b0 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74   the auto-commit
195b1 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64   flag is set and
195b2 20 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c   this is the onl
195b3 79 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20  y active writer 
195b4 0a 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e  .    ** VM, then
195b5 20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20   we do either a 
195b6 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61  commit or rollba
195b7 63 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ck of the curren
195b8 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a  t transaction. .
195b9 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f      **.    ** No
195ba 74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61  te: This block a
195bb 6c 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20  lso runs if one 
195bc 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65  of the special e
195bd 72 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20  rrors handled . 
195be 20 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20     ** above has 
195bf 6f 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a  occurred. .    *
195c0 2f 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74  /.    if( !sqlit
195c1 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29  e3VtabInSync(db)
195c2 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75   .     && db->au
195c3 74 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26  toCommit .     &
195c4 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  & db->writeVdbeC
195c5 6e 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79  nt==(p->readOnly
195c6 3d 3d 30 29 20 0a 20 20 20 20 20 26 26 20 28 64  ==0) .     && (d
195c7 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
195c8 45 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30  E_CommitBusy)==0
195c9 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
195ca 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
195cb 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41  OK || (p->errorA
195cc 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26  ction==OE_Fail &
195cd 26 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f  & !isSpecialErro
195ce 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  r) ){.        /*
195cf 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74   The auto-commit
195d0 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61   flag is true, a
195d1 6e 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  nd the vdbe prog
195d2 72 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20  ram was .       
195d3 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f   ** successful o
195d4 72 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49  r hit an 'OR FAI
195d5 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54  L' constraint. T
195d6 68 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d  his means a comm
195d7 69 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  it .        ** i
195d8 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  s required..    
195d9 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72      */.        r
195da 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64  c = vdbeCommit(d
195db 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69  b, p);.        i
195dc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  f( rc==SQLITE_BU
195dd 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  SY ){.          
195de 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
195df 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e  xArrayLeave(&p->
195e0 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20  aMutex);.       
195e1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
195e2 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d  _BUSY;.        }
195e3 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c  else if( rc!=SQL
195e4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
195e5 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a      p->rc = rc;.
195e6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
195e7 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
195e8 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
195e9 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
195ea 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c  e3CommitInternal
195eb 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20  Changes(db);.   
195ec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
195ed 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
195ee 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64  te3RollbackAll(d
195ef 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  b);.      }.    
195f0 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74    db->nStatement
195f1 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
195f2 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70  if( eStatementOp
195f3 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
195f4 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
195f5 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74  K || p->errorAct
195f6 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a  ion==OE_Fail ){.
195f7 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65          eStateme
195f8 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54  ntOp = SAVEPOINT
195f9 5f 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20  _RELEASE;.      
195fa 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72  }else if( p->err
195fb 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f  orAction==OE_Abo
195fc 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53  rt ){.        eS
195fd 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56  tatementOp = SAV
195fe 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b  EPOINT_ROLLBACK;
195ff 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19600 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65        invalidate
19601 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65  CursorsOnModifie
19602 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20  dBtrees(db);.   
19603 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c       sqlite3Roll
19604 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20  backAll(db);.   
19605 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73       sqlite3Clos
19606 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b  eSavepoints(db);
19607 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74  .        db->aut
19608 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  oCommit = 1;.   
19609 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
1960a 20 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65    /* If eStateme
1960b 6e 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  ntOp is non-zero
1960c 2c 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65  , then a stateme
1960d 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e  nt transaction n
1960e 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62  eeds to.    ** b
1960f 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72  e committed or r
19610 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c  olled back. Call
19611 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73   sqlite3VdbeClos
19612 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a  eStatement() to.
19613 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66      ** do so. If
19614 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
19615 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
19616 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  , and the curren
19617 74 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20  t statement.    
19618 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ** error code is
19619 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51   SQLITE_OK or SQ
1961a 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c  LITE_CONSTRAINT,
1961b 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72   then set the er
1961c 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20  ror.    ** code 
1961d 74 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65  to the new value
1961e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1961f 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b   eStatementOp ){
19620 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
19621 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74  te3VdbeCloseStat
19622 65 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d  ement(p, eStatem
19623 65 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66  entOp);.      if
19624 28 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d  ( rc && (p->rc==
19625 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
19626 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54  rc==SQLITE_CONST
19627 52 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20  RAINT) ){.      
19628 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
19629 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1962a 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1962b 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  sg);.        p->
1962c 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
1962d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
1962e 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73    /* If this was
1962f 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41   an INSERT, UPDA
19630 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64  TE or DELETE and
19631 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72   no statement tr
19632 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a  ansaction.    **
19633 20 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64   has been rolled
19634 20 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68   back, update th
19635 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
19636 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75  ction change-cou
19637 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  nter. .    */.  
19638 20 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43    if( p->changeC
19639 6e 74 4f 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30  ntOn && p->pc>=0
1963a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53   ){.      if( eS
1963b 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45  tatementOp!=SAVE
1963c 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29  POINT_ROLLBACK )
1963d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1963e 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
1963f 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b  db, p->nChange);
19640 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19641 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
19642 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20  eSetChanges(db, 
19643 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
19644 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30    p->nChange = 0
19645 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
19646 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 6f  * Rollback or co
19647 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 20  mmit any schema 
19648 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 63  changes that occ
19649 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66  urred. */.    if
1964a 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  ( p->rc!=SQLITE_
1964b 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26  OK && db->flags&
1964c 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
1964d 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71  nges ){.      sq
1964e 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
1964f 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b  alSchema(db, 0);
19650 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  .      db->flags
19651 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20   = (db->flags | 
19652 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
19653 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  nges);.    }..  
19654 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65    /* Release the
19655 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 71   locks */.    sq
19656 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
19657 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d  rrayLeave(&p->aM
19658 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  utex);.  }..  /*
19659 20 57 65 20 68 61 76 65 20 73 75 63 63 65 73 73   We have success
1965a 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e 64  fully halted and
1965b 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e 20   closed the VM. 
1965c 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 63   Record this fac
1965d 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  t. */.  if( p->p
1965e 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  c>=0 ){.    db->
1965f 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d 3b  activeVdbeCnt--;
19660 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61  .    if( !p->rea
19661 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 64  dOnly ){.      d
19662 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2d  b->writeVdbeCnt-
19663 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  -;.    }.    ass
19664 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56  ert( db->activeV
19665 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74  dbeCnt>=db->writ
19666 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d 0a  eVdbeCnt );.  }.
19667 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
19668 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 20  E_MAGIC_HALT;.  
19669 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43  checkActiveVdbeC
1966a 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 2d  nt(db);.  if( p-
1966b 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1966c 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  d ){.    p->rc =
1966d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1966e 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
1966f 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
19670 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 2c   is set to true,
19671 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 20   then any locks 
19672 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a 20  that were held. 
19673 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f   ** by connectio
19674 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 65  n db have now be
19675 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 6c  en released. Cal
19676 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  l sqlite3Connect
19677 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a 20  ionUnlocked() . 
19678 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 6e   ** to invoke an
19679 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f 63  y required unloc
1967a 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
1967b 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ks..  */.  if( d
1967c 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
1967d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  .    sqlite3Conn
1967e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64  ectionUnlocked(d
1967f 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  b);.  }..  asser
19680 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  t( db->activeVdb
19681 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75  eCnt>0 || db->au
19682 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64  toCommit==0 || d
19683 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30  b->nStatement==0
19684 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   );.  return SQL
19685 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ITE_OK;.}.../*.*
19686 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64  * Each VDBE hold
19687 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  s the result of 
19688 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
19689 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63  sqlite3_step() c
1968a 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e  all.** in p->rc.
1968b 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73    This routine s
1968c 65 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20  ets that result 
1968d 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f  back to SQLITE_O
1968e 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  K..*/.SQLITE_PRI
1968f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
19690 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65  3VdbeResetStepRe
19691 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20  sult(Vdbe *p){. 
19692 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
19693 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  OK;.}../*.** Cle
19694 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74  an up a VDBE aft
19695 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74  er execution but
19696 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74   do not delete t
19697 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74  he VDBE just yet
19698 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65  ..** Write any e
19699 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e  rror messages in
1969a 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52  to *pzErrMsg.  R
1969b 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74  eturn the result
1969c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74   code..**.** Aft
1969d 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  er this routine 
1969e 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45  is run, the VDBE
1969f 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79   should be ready
196a0 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a   to be executed.
196a1 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20  ** again..**.** 
196a2 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e  To look at it an
196a3 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20  other way, this 
196a4 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74  routine resets t
196a5 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a  he state of the.
196a6 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ** virtual machi
196a7 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47  ne from VDBE_MAG
196a8 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d  IC_RUN or VDBE_M
196a9 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74  AGIC_HALT back t
196aa 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f  o.** VDBE_MAGIC_
196ab 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  INIT..*/.SQLITE_
196ac 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
196ad 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62  te3VdbeReset(Vdb
196ae 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  e *p){.  sqlite3
196af 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e   *db;.  db = p->
196b0 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  db;..  /* If the
196b1 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20   VM did not run 
196b2 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72  to completion or
196b3 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72   if it encounter
196b4 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72  ed an.  ** error
196b5 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20  , then it might 
196b6 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61  not have been ha
196b7 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20  lted properly.  
196b8 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20  So halt.  ** it 
196b9 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69  now..  */.  (voi
196ba 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
196bb 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  n(db);.  sqlite3
196bc 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 28  VdbeHalt(p);.  (
196bd 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
196be 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a  tyOff(db);..  /*
196bf 20 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73   If the VDBE has
196c0 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72   be run even par
196c1 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61  tially, then tra
196c2 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20  nsfer the error 
196c3 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72  code.  ** and er
196c4 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d  ror message from
196c5 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74   the VDBE into t
196c6 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
196c7 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74   structure.  But
196c8 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42  .  ** if the VDB
196c9 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20  E has just been 
196ca 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68  set to run but h
196cb 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20  as not actually 
196cc 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a  executed any.  *
196cd 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79  * instructions y
196ce 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61  et, leave the ma
196cf 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f  in database erro
196d0 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e  r information un
196d1 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20  changed..  */.  
196d2 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a  if( p->pc>=0 ){.
196d3 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d      if( p->zErrM
196d4 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  sg ){.      sqli
196d5 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
196d6 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71  lloc();.      sq
196d7 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
196d8 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e  (db->pErr,-1,p->
196d9 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55  zErrMsg,SQLITE_U
196da 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53  TF8,SQLITE_TRANS
196db 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 6c  IENT);.      sql
196dc 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
196dd 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 2d  loc();.      db-
196de 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63  >errCode = p->rc
196df 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
196e0 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
196e1 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d 3e  rMsg);.      p->
196e2 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
196e3 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63   }else if( p->rc
196e4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
196e5 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63  3Error(db, p->rc
196e6 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
196e7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
196e8 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
196e9 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  K, 0);.    }.  }
196ea 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 26  else if( p->rc &
196eb 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a  & p->expired ){.
196ec 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 72      /* The expir
196ed 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 20  ed flag was set 
196ee 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 6f  on the VDBE befo
196ef 72 65 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  re the first cal
196f0 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69  l.    ** to sqli
196f1 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 20  te3_step(). For 
196f2 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 6e  consistency (sin
196f3 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ce sqlite3_step(
196f4 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c  ) was.    ** cal
196f5 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 61  led), set the da
196f6 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 20  tabase error in 
196f7 74 68 69 73 20 63 61 73 65 20 61 73 20 77 65 6c  this case as wel
196f8 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  l..    */.    sq
196f9 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70  lite3Error(db, p
196fa 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71  ->rc, 0);.    sq
196fb 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
196fc 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 70  (db->pErr, -1, p
196fd 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 54  ->zErrMsg, SQLIT
196fe 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54  E_UTF8, SQLITE_T
196ff 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73  RANSIENT);.    s
19700 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
19701 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
19702 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30    p->zErrMsg = 0
19703 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c  ;.  }..  /* Recl
19704 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75  aim all memory u
19705 73 65 64 20 62 79 20 74 68 65 20 56 44 42 45 0a  sed by the VDBE.
19706 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 70    */.  Cleanup(p
19707 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 72  );..  /* Save pr
19708 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 74  ofiling informat
19709 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 44  ion from this VD
1970a 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 66  BE run..  */.#if
1970b 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45  def VDBE_PROFILE
1970c 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a 6f  .  {.    FILE *o
1970d 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 65  ut = fopen("vdbe
1970e 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 22  _profile.out", "
1970f 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74  a");.    if( out
19710 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
19711 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
19712 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 20  ut, "---- ");.  
19713 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
19714 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nOp; i++){.   
19715 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
19716 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f 70  , "%02x", p->aOp
19717 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 20  [i].opcode);.   
19718 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 6e     }.      fprin
19719 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20  tf(out, "\n");. 
1971a 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
1971b 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20  p->nOp; i++){.  
1971c 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
1971d 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 25  t, "%6d %10lld %
1971e 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 20  8lld ",.        
1971f 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74     p->aOp[i].cnt
19720 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ,.           p->
19721 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a 20  aOp[i].cycles,. 
19722 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70            p->aOp
19723 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e 61  [i].cnt>0 ? p->a
19724 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d 3e  Op[i].cycles/p->
19725 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a 20  aOp[i].cnt : 0. 
19726 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
19727 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
19728 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 2d  ntOp(out, i, &p-
19729 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 20  >aOp[i]);.      
1972a 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f  }.      fclose(o
1972b 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  ut);.    }.  }.#
1972c 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 63  endif.  p->magic
1972d 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e   = VDBE_MAGIC_IN
1972e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  IT;.  return p->
1972f 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b  rc & db->errMask
19730 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  ;.}. ./*.** Clea
19731 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20  n up and delete 
19732 61 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65  a VDBE after exe
19733 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20  cution.  Return 
19734 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
19735 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c   is.** the resul
19736 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61  t code.  Write a
19737 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ny error message
19738 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72   text into *pzEr
19739 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rMsg..*/.SQLITE_
1973a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1973b 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
1973c 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
1973d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1973e 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d    if( p->magic==
1973f 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c  VDBE_MAGIC_RUN |
19740 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  | p->magic==VDBE
19741 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20  _MAGIC_HALT ){. 
19742 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
19743 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20  dbeReset(p);.   
19744 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70   assert( (rc & p
19745 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d  ->db->errMask)==
19746 72 63 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rc );.  }else if
19747 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45  ( p->magic!=VDBE
19748 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 7b 0a 20  _MAGIC_INIT ){. 
19749 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1974a 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73  _MISUSE;.  }.  s
1974b 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
1974c 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
1974d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
1974e 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  the destructor f
1974f 6f 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20  or each auxdata 
19750 65 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75  entry in pVdbeFu
19751 6e 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20  nc for which.** 
19752 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
19753 67 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73  g bit in mask is
19754 20 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61   clear.  Auxdata
19755 20 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20   entries beyond 
19756 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73  31.** are always
19757 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20   destroyed.  To 
19758 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64  destroy all auxd
19759 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c  ata entries, cal
1975a 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  l this.** routin
1975b 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a  e with mask==0..
1975c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1975d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1975e 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28  beDeleteAuxData(
1975f 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46  VdbeFunc *pVdbeF
19760 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a  unc, int mask){.
19761 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
19762 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d  =0; i<pVdbeFunc-
19763 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nAux; i++){.   
19764 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20   struct AuxData 
19765 2a 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75  *pAux = &pVdbeFu
19766 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20  nc->apAux[i];.  
19767 20 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21    if( (i>31 || !
19768 28 6d 61 73 6b 26 28 31 3c 3c 69 29 29 29 20 26  (mask&(1<<i))) &
19769 26 20 70 41 75 78 2d 3e 70 41 75 78 20 29 7b 0a  & pAux->pAux ){.
1976a 20 20 20 20 20 20 69 66 28 20 70 41 75 78 2d 3e        if( pAux->
1976b 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20  xDelete ){.     
1976c 20 20 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65     pAux->xDelete
1976d 28 70 41 75 78 2d 3e 70 41 75 78 29 3b 0a 20 20  (pAux->pAux);.  
1976e 20 20 20 20 7d 0a 20 20 20 20 20 20 70 41 75 78      }.      pAux
1976f 2d 3e 70 41 75 78 20 3d 20 30 3b 0a 20 20 20 20  ->pAux = 0;.    
19770 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  }.  }.}../*.** D
19771 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20  elete an entire 
19772 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  VDBE..*/.SQLITE_
19773 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19774 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 56  ite3VdbeDelete(V
19775 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  dbe *p){.  int i
19776 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
19777 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ..  if( p==0 ) r
19778 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 2d  eturn;.  db = p-
19779 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50  >db;.  if( p->pP
1977a 72 65 76 20 29 7b 0a 20 20 20 20 70 2d 3e 70 50  rev ){.    p->pP
1977b 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e  rev->pNext = p->
1977c 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pNext;.  }else{.
1977d 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1977e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 20 20 20  pVdbe==p );.    
1977f 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 2d 3e 70  db->pVdbe = p->p
19780 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Next;.  }.  if( 
19781 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  p->pNext ){.    
19782 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20  p->pNext->pPrev 
19783 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a  = p->pPrev;.  }.
19784 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a    if( p->aOp ){.
19785 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d 20 70 2d      Op *pOp = p-
19786 3e 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d  >aOp;.    for(i=
19787 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b  0; i<p->nOp; i++
19788 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20  , pOp++){.      
19789 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e  freeP4(db, pOp->
1978a 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e  p4type, pOp->p4.
1978b 70 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  p);.#ifdef SQLIT
1978c 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 73 71  E_DEBUG.      sq
1978d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1978e 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b 0a  pOp->zComment);.
1978f 23 65 6e 64 69 66 20 20 20 20 20 0a 20 20 20 20  #endif     .    
19790 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d  }.  }.  releaseM
19791 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c  emArray(p->aVar,
19792 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 73 71 6c   p->nVar);.  sql
19793 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
19794 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 72 65 6c  ->aLabel);.  rel
19795 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e  easeMemArray(p->
19796 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65  aColName, p->nRe
19797 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f  sColumn*COLNAME_
19798 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  N);.  sqlite3DbF
19799 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e  ree(db, p->aColN
1979a 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
1979b 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 53 71  bFree(db, p->zSq
1979c 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d  l);.  p->magic =
1979d 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 44   VDBE_MAGIC_DEAD
1979e 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1979f 65 28 64 62 2c 20 70 2d 3e 61 4f 70 29 3b 0a 20  e(db, p->aOp);. 
197a0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
197a1 62 2c 20 70 2d 3e 70 46 72 65 65 29 3b 0a 20 20  b, p->pFree);.  
197a2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
197a3 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  , p);.}../*.** I
197a4 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61  f a MoveTo opera
197a5 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20  tion is pending 
197a6 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72  on the given cur
197a7 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61  sor, then do tha
197a8 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e  t.** MoveTo now.
197a9 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f    Return an erro
197aa 72 20 63 6f 64 65 2e 20 20 49 66 20 6e 6f 20 4d  r code.  If no M
197ab 6f 76 65 54 6f 20 69 73 20 70 65 6e 64 69 6e 67  oveTo is pending
197ac 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  , this.** routin
197ad 65 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67 20 61  e does nothing a
197ae 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  nd returns SQLIT
197af 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
197b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
197b1 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
197b2 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 2a  eto(VdbeCursor *
197b3 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 66  p){.  if( p->def
197b4 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20  erredMoveto ){. 
197b5 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b 0a     int res, rc;.
197b6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
197b7 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e  ST.    extern in
197b8 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68  t sqlite3_search
197b9 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a 20  _count;.#endif. 
197ba 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73     assert( p->is
197bb 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 20  Table );.    rc 
197bc 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
197bd 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e  vetoUnpacked(p->
197be 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d  pCursor, 0, p->m
197bf 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20  ovetoTarget, 0, 
197c0 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72  &res);.    if( r
197c1 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
197c2 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20     p->lastRowid 
197c3 3d 20 6b 65 79 54 6f 49 6e 74 28 70 2d 3e 6d 6f  = keyToInt(p->mo
197c4 76 65 74 6f 54 61 72 67 65 74 29 3b 0a 20 20 20  vetoTarget);.   
197c5 20 70 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64   p->rowidIsValid
197c6 20 3d 20 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a   = res==0 ?1:0;.
197c7 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 29 7b      if( res<0 ){
197c8 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
197c9 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 2d 3e  te3BtreeNext(p->
197ca 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a  pCursor, &res);.
197cb 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
197cc 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
197cd 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
197ce 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ST.    sqlite3_s
197cf 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  earch_count++;.#
197d0 65 6e 64 69 66 0a 20 20 20 20 70 2d 3e 64 65 66  endif.    p->def
197d1 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b  erredMoveto = 0;
197d2 0a 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61  .    p->cacheSta
197d3 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
197d4 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  E;.  }else if( p
197d5 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  ->pCursor ){.   
197d6 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20   int hasMoved;. 
197d7 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
197d8 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61  te3BtreeCursorHa
197d9 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f  sMoved(p->pCurso
197da 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20  r, &hasMoved);. 
197db 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
197dc 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68  rn rc;.    if( h
197dd 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20  asMoved ){.     
197de 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20   p->cacheStatus 
197df 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
197e0 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20       p->nullRow 
197e1 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 1;.    }.  }. 
197e2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
197e3 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K;.}../*.** The 
197e4 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69  following functi
197e5 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74  ons:.**.** sqlit
197e6 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
197e7 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  ().** sqlite3Vdb
197e8 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29  eSerialTypeLen()
197e9 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53  .** sqlite3VdbeS
197ea 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71  erialLen().** sq
197eb 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50  lite3VdbeSerialP
197ec 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56  ut().** sqlite3V
197ed 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a  dbeSerialGet().*
197ee 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65  *.** encapsulate
197ef 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73   the code that s
197f0 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73  erializes values
197f1 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20   for storage in 
197f2 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61  SQLite.** data a
197f3 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73  nd index records
197f4 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65  . Each serialize
197f5 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73  d value consists
197f6 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c   of a.** 'serial
197f7 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f  -type' and a blo
197f8 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73  b of data. The s
197f9 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e  erial type is an
197fa 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   8-byte unsigned
197fb 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f  .** integer, sto
197fc 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e  red as a varint.
197fd 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c  .**.** In an SQL
197fe 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64  ite index record
197ff 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70  , the serial typ
19800 65 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65  e is stored dire
19801 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74  ctly before.** t
19802 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20  he blob of data 
19803 74 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f  that it correspo
19804 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62  nds to. In a tab
19805 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73  le record, all s
19806 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61  erial.** types a
19807 72 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65  re stored at the
19808 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65   start of the re
19809 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c  cord, and the bl
1980a 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a  obs of data at.*
1980b 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65  * the end. Hence
1980c 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
1980d 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65   allow the calle
1980e 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a  r to handle the.
1980f 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61  ** serial-type a
19810 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70  nd data blob sep
19811 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  erately..**.** T
19812 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
19813 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65  le describes the
19814 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65   various storage
19815 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74   classes for dat
19816 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61  a:.**.**   seria
19817 6c 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79  l type        by
19818 74 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20  tes of data     
19819 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d   type.**   -----
1981a 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d  ---------     --
1981b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20  -------------   
1981c 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
1981d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20  .**      0      
1981e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1981f 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
19820 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20  .**      1      
19821 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
19822 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e              sign
19823 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  ed integer.**   
19824 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
19825 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20           2      
19826 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74        signed int
19827 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20  eger.**      3  
19828 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19829 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20     3            
1982a 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
1982b 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20  *      4        
1982c 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20               4  
1982d 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64            signed
1982e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
1982f 20 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20   5              
19830 20 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20         6        
19831 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67      signed integ
19832 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20  er.**      6    
19833 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19834 20 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69   8            si
19835 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20  gned integer.** 
19836 20 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20       7          
19837 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20             8    
19838 20 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f          IEEE flo
19839 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20  at.**      8    
1983a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1983b 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e   0            In
1983c 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30  teger constant 0
1983d 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20  .**      9      
1983e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1983f 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65              Inte
19840 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a  ger constant 1.*
19841 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20  *     10,11     
19842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19843 20 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76            reserv
19844 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e  ed for expansion
19845 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64  .**    N>=12 and
19846 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31   even       (N-1
19847 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42  2)/2        BLOB
19848 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64  .**    N>=13 and
19849 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31   odd        (N-1
1984a 33 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74  3)/2        text
1984b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64  .**.** The 8 and
1984c 20 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64   9 types were ad
1984d 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69  ded in 3.3.0, fi
1984e 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72  le format 4.  Pr
1984f 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20  ior versions.** 
19850 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  of SQLite will n
19851 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68  ot understand th
19852 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73  ose serial types
19853 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ..*/../*.** Retu
19854 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79  rn the serial-ty
19855 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65  pe for the value
19856 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e   stored in pMem.
19857 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19858 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64  TE u32 sqlite3Vd
19859 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d  beSerialType(Mem
1985a 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65   *pMem, int file
1985b 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20  _format){.  int 
1985c 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c  flags = pMem->fl
1985d 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20  ags;.  int n;.. 
1985e 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e   if( flags&MEM_N
1985f 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ull ){.    retur
19860 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66  n 0;.  }.  if( f
19861 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a  lags&MEM_Int ){.
19862 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
19863 74 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65  t whether to use
19864 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38   1, 2, 4, 6 or 8
19865 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64   bytes. */.#   d
19866 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20  efine MAX_6BYTE 
19867 28 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30  ((((i64)0x000080
19868 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20  00)<<32)-1).    
19869 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e  i64 i = pMem->u.
1986a 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20  i;.    u64 u;.  
1986b 20 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61    if( file_forma
1986c 74 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69  t>=4 && (i&1)==i
1986d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1986e 20 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d   8+(u32)i;.    }
1986f 0a 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d  .    u = i<0 ? -
19870 69 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75  i : i;.    if( u
19871 3c 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 31  <=127 ) return 1
19872 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37  ;.    if( u<=327
19873 36 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20  67 ) return 2;. 
19874 20 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30     if( u<=838860
19875 37 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20  7 ) return 3;.  
19876 20 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33    if( u<=2147483
19877 36 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a  647 ) return 4;.
19878 20 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36      if( u<=MAX_6
19879 42 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b  BYTE ) return 5;
1987a 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20  .    return 6;. 
1987b 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d   }.  if( flags&M
1987c 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  EM_Real ){.    r
1987d 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61  eturn 7;.  }.  a
1987e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d  ssert( pMem->db-
1987f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
19880 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c   flags&(MEM_Str|
19881 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e  MEM_Blob) );.  n
19882 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66   = pMem->n;.  if
19883 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ( flags & MEM_Ze
19884 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70  ro ){.    n += p
19885 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20  Mem->u.nZero;.  
19886 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30  }.  assert( n>=0
19887 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e   );.  return ((n
19888 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61  *2) + 12 + ((fla
19889 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29  gs&MEM_Str)!=0))
1988a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1988b 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  n the length of 
1988c 74 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 70  the data corresp
1988d 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75  onding to the su
1988e 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79  pplied serial-ty
1988f 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pe..*/.SQLITE_PR
19890 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19891 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
19892 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79  en(u32 serial_ty
19893 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61  pe){.  if( seria
19894 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20  l_type>=12 ){.  
19895 20 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c    return (serial
19896 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d  _type-12)/2;.  }
19897 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63  else{.    static
19898 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b   const u8 aSize[
19899 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33  ] = { 0, 1, 2, 3
1989a 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c  , 4, 6, 8, 8, 0,
1989b 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20   0, 0, 0 };.    
1989c 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72  return aSize[ser
1989d 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d  ial_type];.  }.}
1989e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72  ../*.** If we ar
1989f 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63  e on an architec
198a0 74 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d  ture with mixed-
198a1 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20  endian floating 
198a2 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20  .** points (ex: 
198a3 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20  ARM7) then swap 
198a4 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65  the lower 4 byte
198a5 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75  s with the .** u
198a6 70 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 52  pper 4 bytes.  R
198a7 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74  eturn the result
198a8 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74  ..**.** For most
198a9 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20   architectures, 
198aa 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  this is a no-op.
198ab 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20  .**.** (later): 
198ac 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20   It is reported 
198ad 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d  to me that the m
198ae 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62  ixed-endian prob
198af 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69  lem.** on ARM7 i
198b0 73 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 20  s an issue with 
198b1 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68  GCC, not with th
198b2 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74  e ARM7 chip.  It
198b3 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65   seems.** that e
198b4 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66  arly versions of
198b5 20 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 20   GCC stored the 
198b6 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36  two words of a 6
198b7 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69  4-bit.** float i
198b8 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65  n the wrong orde
198b9 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 72  r.  And that err
198ba 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70  or has been prop
198bb 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73  agated.** ever s
198bc 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65  ince.  The blame
198bd 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
198be 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 68  ily with GCC, th
198bf 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67  ough..** GCC mig
198c0 68 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70  ht have just cop
198c1 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d  ying the problem
198c2 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f   from a prior co
198c3 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20  mpiler..** I am 
198c4 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e  also told that n
198c5 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  ewer versions of
198c6 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77   GCC that follow
198c7 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20   a different.** 
198c8 41 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65  ABI get the byte
198c9 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a   order right..**
198ca 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75  .** Developers u
198cb 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61  sing SQLite on a
198cc 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f  n ARM7 should co
198cd 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68  mpile and run th
198ce 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  eir.** applicati
198cf 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54  on using -DSQLIT
198d0 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61  E_DEBUG=1 at lea
198d1 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44  st once.  With D
198d2 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c  EBUG.** enabled,
198d3 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65   some asserts be
198d4 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20  low will ensure 
198d5 74 68 61 74 20 74 68 65 20 62 79 74 65 20 6f 72  that the byte or
198d6 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69  der of.** floati
198d7 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20  ng point values 
198d8 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a  is correct..**.*
198d9 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20  * (2007-08-30)  
198da 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68  Frank van Vugt h
198db 61 73 20 73 74 75 64 69 65 64 20 74 68 69 73 20  as studied this 
198dc 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a  problem closely.
198dd 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20  ** and has send 
198de 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20  his findings to 
198df 74 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c  the SQLite devel
198e0 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a  opers.  Frank.**
198e1 20 77 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d   writes that som
198e2 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20  e Linux kernels 
198e3 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70  offer floating p
198e4 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a  oint hardware.**
198e5 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20   emulation that 
198e6 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74  uses only 32-bit
198e7 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65   mantissas inste
198e8 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a  ad of a full .**
198e9 20 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 75   48-bits as requ
198ea 69 72 65 64 20 62 79 20 74 68 65 20 49 45 45 45  ired by the IEEE
198eb 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69   standard.  (Thi
198ec 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46  s is the.** CONF
198ed 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f  IG_FPE_FASTFPE o
198ee 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68  ption.)  On such
198ef 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69   systems, floati
198f0 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65  ng point.** byte
198f1 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65   swapping become
198f2 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74  s very complicat
198f3 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72  ed.  To avoid pr
198f4 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e  oblems,.** the n
198f5 65 63 65 73 73 61 72 79 20 62 79 74 65 20 73 77  ecessary byte sw
198f6 61 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 65  apping is carrie
198f7 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34  d out using a 64
198f8 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20  -bit integer.** 
198f9 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34  rather than a 64
198fa 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61  -bit float.  Fra
198fb 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68  nk assures us th
198fc 61 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65  at the code here
198fd 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69  .** works for hi
198fe 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65  m.  We, the deve
198ff 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20  lopers, have no 
19900 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65  way to independe
19901 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74  ntly.** verify t
19902 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73  his, but Frank s
19903 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61  eems to know wha
19904 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20  t he is talking 
19905 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74  about.** so we t
19906 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66  rust him..*/.#if
19907 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44  def SQLITE_MIXED
19908 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c  _ENDIAN_64BIT_FL
19909 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 66  OAT.static u64 f
1990a 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e 29  loatSwap(u64 in)
1990b 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  {.  union {.    
1990c 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69  u64 r;.    u32 i
1990d 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33  [2];.  } u;.  u3
1990e 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e  2 t;..  u.r = in
1990f 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a  ;.  t = u.i[0];.
19910 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31    u.i[0] = u.i[1
19911 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b  ];.  u.i[1] = t;
19912 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d  .  return u.r;.}
19913 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69  .# define swapMi
19914 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58  xedEndianFloat(X
19915 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70  )  X = floatSwap
19916 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  (X).#else.# defi
19917 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69  ne swapMixedEndi
19918 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69  anFloat(X).#endi
19919 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74  f../*.** Write t
1991a 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61  he serialized da
1991b 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20  ta blob for the 
1991c 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20  value stored in 
1991d 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75  pMem into .** bu
1991e 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  f. It is assumed
1991f 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72   that the caller
19920 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73   has allocated s
19921 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e  ufficient space.
19922 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
19923 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77  umber of bytes w
19924 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42  ritten..**.** nB
19925 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74  uf is the amount
19926 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 69   of space left i
19927 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d  n buf[].  nBuf m
19928 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a  ust always be.**
19929 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f   large enough to
1992a 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65   hold the entire
1992b 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c   field.  Except,
1992c 20 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 73   if the field is
1992d 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20  .** a blob with 
1992e 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  a zero-filled ta
1992f 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d  il, then buf[] m
19930 69 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65  ight be just the
19931 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74   right.** size t
19932 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e  o hold everythin
19933 67 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65  g except for the
19934 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69   zero-filled tai
19935 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20  l.  If buf[].** 
19936 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75  is only big enou
19937 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e  gh to hold the n
19938 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20  on-zero prefix, 
19939 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20  then only write 
1993a 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69  that.** prefix i
1993b 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20  nto buf[].  But 
1993c 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67  if buf[] is larg
1993d 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  e enough to hold
1993e 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65   both the.** pre
1993f 66 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c  fix and the tail
19940 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 20   then write the 
19941 70 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 74  prefix and set t
19942 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a  he tail to all.*
19943 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52  * zeros..**.** R
19944 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
19945 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c   of bytes actual
19946 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  ly written into 
19947 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62  buf[].  The numb
19948 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69  er.** of bytes i
19949 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65  n the zero-fille
1994a 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64  d tail is includ
1994b 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e  ed in the return
1994c 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69   value only.** i
1994d 66 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 65  f those bytes we
1994e 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66  re zeroed in buf
1994f 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50  []..*/ .SQLITE_P
19950 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19951 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28  e3VdbeSerialPut(
19952 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75  u8 *buf, int nBu
19953 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e  f, Mem *pMem, in
19954 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a  t file_format){.
19955 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
19956 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  e = sqlite3VdbeS
19957 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20  erialType(pMem, 
19958 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
19959 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49  int len;..  /* I
1995a 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20  nteger and Real 
1995b 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f  */.  if( serial_
1995c 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61  type<=7 && seria
1995d 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20  l_type>0 ){.    
1995e 75 36 34 20 76 3b 0a 20 20 20 20 69 6e 74 20 69  u64 v;.    int i
1995f 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 6c  ;.    if( serial
19960 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20  _type==7 ){.    
19961 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
19962 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d  (v)==sizeof(pMem
19963 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65  ->r) );.      me
19964 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e  mcpy(&v, &pMem->
19965 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20  r, sizeof(v));. 
19966 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e       swapMixedEn
19967 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20  dianFloat(v);.  
19968 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76    }else{.      v
19969 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20   = pMem->u.i;.  
1996a 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20    }.    len = i 
1996b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
1996c 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61  ialTypeLen(seria
1996d 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73  l_type);.    ass
1996e 65 72 74 28 20 6c 65 6e 3c 3d 6e 42 75 66 20 29  ert( len<=nBuf )
1996f 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d  ;.    while( i--
19970 20 29 7b 0a 20 20 20 20 20 20 62 75 66 5b 69 5d   ){.      buf[i]
19971 20 3d 20 28 75 38 29 28 76 26 30 78 46 46 29 3b   = (u8)(v&0xFF);
19972 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a  .      v >>= 8;.
19973 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
19974 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   len;.  }..  /* 
19975 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 2a  String or blob *
19976 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74  /.  if( serial_t
19977 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 61  ype>=12 ){.    a
19978 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 20 2b  ssert( pMem->n +
19979 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26   ((pMem->flags &
1997a 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65 6d 2d   MEM_Zero)?pMem-
1997b 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20 20 20 20  >u.nZero:0).    
1997c 20 20 20 20 20 20 20 20 20 3d 3d 20 73 71 6c 69           == sqli
1997d 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1997e 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65  eLen(serial_type
1997f 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
19980 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29   pMem->n<=nBuf )
19981 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d  ;.    len = pMem
19982 2d 3e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  ->n;.    memcpy(
19983 62 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65  buf, pMem->z, le
19984 6e 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d  n);.    if( pMem
19985 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ->flags & MEM_Ze
19986 72 6f 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20  ro ){.      len 
19987 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f  += pMem->u.nZero
19988 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3e  ;.      if( len>
19989 6e 42 75 66 20 29 7b 0a 20 20 20 20 20 20 20 20  nBuf ){.        
1998a 6c 65 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 20 20  len = nBuf;.    
1998b 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74    }.      memset
1998c 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20  (&buf[pMem->n], 
1998d 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b  0, len-pMem->n);
1998e 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
1998f 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  n len;.  }..  /*
19990 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e   NULL or constan
19991 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72  ts 0 or 1 */.  r
19992 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
19993 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68  * Deserialize th
19994 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e  e data blob poin
19995 74 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73  ted to by buf as
19996 20 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72   serial type ser
19997 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20  ial_type.** and 
19998 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
19999 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72   in pMem.  Retur
1999a 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1999b 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a  bytes read..*/ .
1999c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1999d 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  nt sqlite3VdbeSe
1999e 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74  rialGet(.  const
1999f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
199a0 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66  buf,     /* Buff
199a1 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a  er to deserializ
199a2 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20  e from */.  u32 
199a3 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20  serial_type,    
199a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72            /* Ser
199a5 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65  ial type to dese
199a6 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d  rialize */.  Mem
199a7 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20   *pMem          
199a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
199a9 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69  mory cell to wri
199aa 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f  te value into */
199ab 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65  .){.  switch( se
199ac 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20  rial_type ){.   
199ad 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52   case 10:   /* R
199ae 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75  eserved for futu
199af 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61  re use */.    ca
199b0 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65  se 11:   /* Rese
199b1 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20  rved for future 
199b2 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  use */.    case 
199b3 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f  0: {  /* NULL */
199b4 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
199b5 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
199b6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
199b7 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20  }.    case 1: { 
199b8 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 1-byte signed
199b9 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
199ba 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73    pMem->u.i = (s
199bb 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30  igned char)buf[0
199bc 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ];.      pMem->f
199bd 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
199be 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
199bf 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32      }.    case 2
199c0 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69  : { /* 2-byte si
199c1 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
199c2 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
199c3 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72  = (((signed char
199c4 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62  )buf[0])<<8) | b
199c5 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65  uf[1];.      pMe
199c6 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
199c7 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  nt;.      return
199c8 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   2;.    }.    ca
199c9 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74  se 3: { /* 3-byt
199ca 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
199cb 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
199cc 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20  u.i = (((signed 
199cd 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36  char)buf[0])<<16
199ce 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20  ) | (buf[1]<<8) 
199cf 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20  | buf[2];.      
199d0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
199d1 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74  M_Int;.      ret
199d2 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 3;.    }.   
199d3 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d   case 4: { /* 4-
199d4 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
199d5 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ger */.      pMe
199d6 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d  m->u.i = (buf[0]
199d7 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c  <<24) | (buf[1]<
199d8 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c  <16) | (buf[2]<<
199d9 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20  8) | buf[3];.   
199da 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
199db 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
199dc 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a  return 4;.    }.
199dd 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a      case 5: { /*
199de 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   6-byte signed i
199df 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
199e0 75 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65  u64 x = (((signe
199e1 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c  d char)buf[0])<<
199e2 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20  8) | buf[1];.   
199e3 20 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b     u32 y = (buf[
199e4 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33  2]<<24) | (buf[3
199e5 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d  ]<<16) | (buf[4]
199e6 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20  <<8) | buf[5];. 
199e7 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29       x = (x<<32)
199e8 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d   | y;.      pMem
199e9 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26  ->u.i = *(i64*)&
199ea 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  x;.      pMem->f
199eb 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
199ec 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a        return 6;.
199ed 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36      }.    case 6
199ee 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69  :   /* 8-byte si
199ef 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
199f0 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a      case 7: { /*
199f1 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70   IEEE floating p
199f2 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36  oint */.      u6
199f3 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79  4 x;.      u32 y
199f4 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  ;.#if !defined(N
199f5 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e  DEBUG) && !defin
199f6 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
199f7 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20  LOATING_POINT). 
199f8 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74       /* Verify t
199f9 68 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64  hat integers and
199fa 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
199fb 76 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73  values use the s
199fc 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74  ame.      ** byt
199fd 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68  e order.  Or, th
199fe 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58  at if SQLITE_MIX
199ff 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f  ED_ENDIAN_64BIT_
19a00 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a  FLOAT is.      *
19a01 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36  * defined that 6
19a02 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70  4-bit floating p
19a03 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c  oint values real
19a04 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20  ly are mixed.   
19a05 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20     ** endian..  
19a06 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61      */.      sta
19a07 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31  tic const u64 t1
19a08 20 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30   = ((u64)0x3ff00
19a09 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20  000)<<32;.      
19a0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75  static const dou
19a0b 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20  ble r1 = 1.0;.  
19a0c 20 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b      u64 t2 = t1;
19a0d 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64  .      swapMixed
19a0e 45 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b  EndianFloat(t2);
19a0f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
19a10 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f  izeof(r1)==sizeo
19a11 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28  f(t2) && memcmp(
19a12 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66  &r1, &t2, sizeof
19a13 28 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64  (r1))==0 );.#end
19a14 69 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62  if..      x = (b
19a15 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75  uf[0]<<24) | (bu
19a16 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66  f[1]<<16) | (buf
19a17 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d  [2]<<8) | buf[3]
19a18 3b 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66  ;.      y = (buf
19a19 5b 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b  [4]<<24) | (buf[
19a1a 35 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36  5]<<16) | (buf[6
19a1b 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a  ]<<8) | buf[7];.
19a1c 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32        x = (x<<32
19a1d 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28  ) | y;.      if(
19a1e 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20   serial_type==6 
19a1f 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  ){.        pMem-
19a20 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78  >u.i = *(i64*)&x
19a21 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
19a22 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
19a23 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19a24 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69        assert( si
19a25 7a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69  zeof(x)==8 && si
19a26 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38  zeof(pMem->r)==8
19a27 20 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70   );.        swap
19a28 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74  MixedEndianFloat
19a29 28 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  (x);.        mem
19a2a 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78  cpy(&pMem->r, &x
19a2b 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20  , sizeof(x));.  
19a2c 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
19a2d 73 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  s = sqlite3IsNaN
19a2e 28 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f  (pMem->r) ? MEM_
19a2f 4e 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b  Null : MEM_Real;
19a30 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
19a31 65 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20  eturn 8;.    }. 
19a32 20 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a     case 8:    /*
19a33 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20   Integer 0 */.  
19a34 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20    case 9: {  /* 
19a35 49 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20  Integer 1 */.   
19a36 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73     pMem->u.i = s
19a37 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20  erial_type-8;.  
19a38 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
19a39 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
19a3a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
19a3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
19a3c 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20        int len = 
19a3d 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29  (serial_type-12)
19a3e 2f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  /2;.      pMem->
19a3f 7a 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b  z = (char *)buf;
19a40 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
19a41 20 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d   len;.      pMem
19a42 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20  ->xDel = 0;.    
19a43 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
19a44 65 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20  e&0x01 ){.      
19a45 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19a46 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70  MEM_Str | MEM_Ep
19a47 68 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  hem;.      }else
19a48 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
19a49 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62  flags = MEM_Blob
19a4a 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20   | MEM_Ephem;.  
19a4b 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
19a4c 72 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20  rn len;.    }.  
19a4d 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
19a4e 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68  ../*.** Given th
19a4f 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f  e nKey-byte enco
19a50 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64  ding of a record
19a51 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73   in pKey[], pars
19a52 65 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20  e the.** record 
19a53 69 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52  into a UnpackedR
19a54 65 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e  ecord structure.
19a55 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
19a56 65 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74  er to.** that st
19a57 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
19a58 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
19a59 69 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64  ion might provid
19a5a 65 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20  e szSpace bytes 
19a5b 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61  of memory.** spa
19a5c 63 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54  ce at pSpace.  T
19a5d 68 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65  his space can be
19a5e 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68   used to hold th
19a5f 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44  e returned.** VD
19a60 62 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73  beParsedRecord s
19a61 74 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69  tructure if it i
19a62 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20  s large enough. 
19a63 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74   If it is.** not
19a64 20 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61   big enough, spa
19a65 63 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ce is obtained f
19a66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
19a67 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oc()..**.** The 
19a68 72 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75  returned structu
19a69 72 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f  re should be clo
19a6a 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  sed by a call to
19a6b 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44  .** sqlite3VdbeD
19a6c 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
19a6d 6f 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54  ord()..*/ .SQLIT
19a6e 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b  E_PRIVATE Unpack
19a6f 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65  edRecord *sqlite
19a70 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63  3VdbeRecordUnpac
19a71 6b 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  k(.  KeyInfo *pK
19a72 65 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49  eyInfo,     /* I
19a73 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
19a74 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d   the record form
19a75 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79  at */.  int nKey
19a76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
19a77 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69  * Size of the bi
19a78 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20  nary record */. 
19a79 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
19a7a 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62  y,      /* The b
19a7b 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a  inary record */.
19a7c 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
19a7d 20 2a 70 53 70 61 63 65 2c 2f 2a 20 53 70 61 63   *pSpace,/* Spac
19a7e 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68  e available to h
19a7f 6f 6c 64 20 72 65 73 75 6c 74 69 6e 67 20 6f 62  old resulting ob
19a80 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  ject */.  int sz
19a81 53 70 61 63 65 20 20 20 20 20 20 20 20 20 20 20  Space           
19a82 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 61   /* Size of pSpa
19a83 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f  ce[] in bytes */
19a84 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  .){.  const unsi
19a85 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 20  gned char *aKey 
19a86 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  = (const unsigne
19a87 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20  d char *)pKey;. 
19a88 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
19a89 2a 70 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c  *p;.  int nByte,
19a8a 20 64 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20   d;.  u32 idx;. 
19a8b 20 75 31 36 20 75 3b 20 20 20 20 20 20 20 20 20   u16 u;         
19a8c 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67          /* Unsig
19a8d 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72  ned loop counter
19a8e 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 3b   */.  u32 szHdr;
19a8f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20  .  Mem *pMem;.  
19a90 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
19a91 66 28 4d 65 6d 29 3e 73 69 7a 65 6f 66 28 2a 70  f(Mem)>sizeof(*p
19a92 29 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73  ) );.  nByte = s
19a93 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 4b 65 79  izeof(Mem)*(pKey
19a94 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 32 29 3b  Info->nField+2);
19a95 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 73 7a 53  .  if( nByte>szS
19a96 70 61 63 65 20 29 7b 0a 20 20 20 20 70 20 3d 20  pace ){.    p = 
19a97 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
19a98 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c  aw(pKeyInfo->db,
19a99 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28   nByte);.    if(
19a9a 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
19a9b 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d  ;.    p->flags =
19a9c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 46   UNPACKED_NEED_F
19a9d 52 45 45 20 7c 20 55 4e 50 41 43 4b 45 44 5f 4e  REE | UNPACKED_N
19a9e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 7d  EED_DESTROY;.  }
19a9f 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 70 53  else{.    p = pS
19aa0 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 66 6c 61  pace;.    p->fla
19aa1 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45  gs = UNPACKED_NE
19aa2 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20 7d 0a  ED_DESTROY;.  }.
19aa3 20 20 70 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20    p->pKeyInfo = 
19aa4 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e  pKeyInfo;.  p->n
19aa5 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f  Field = pKeyInfo
19aa6 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20  ->nField + 1;.  
19aa7 70 2d 3e 61 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d  p->aMem = pMem =
19aa8 20 26 28 28 4d 65 6d 2a 29 70 29 5b 31 5d 3b 0a   &((Mem*)p)[1];.
19aa9 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e    idx = getVarin
19aaa 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29  t32(aKey, szHdr)
19aab 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20  ;.  d = szHdr;. 
19aac 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28   u = 0;.  while(
19aad 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c   idx<szHdr && u<
19aae 70 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20  p->nField ){.   
19aaf 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
19ab0 3b 0a 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65  ;..    idx += ge
19ab1 74 56 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b  tVarint32(&aKey[
19ab2 69 64 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70  idx], serial_typ
19ab3 65 29 3b 0a 20 20 20 20 69 66 28 20 64 3e 3d 6e  e);.    if( d>=n
19ab4 4b 65 79 20 26 26 20 73 71 6c 69 74 65 33 56 64  Key && sqlite3Vd
19ab5 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
19ab6 73 65 72 69 61 6c 5f 74 79 70 65 29 3e 30 20 29  serial_type)>0 )
19ab7 20 62 72 65 61 6b 3b 0a 20 20 20 20 70 4d 65 6d   break;.    pMem
19ab8 2d 3e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f  ->enc = pKeyInfo
19ab9 2d 3e 65 6e 63 3b 0a 20 20 20 20 70 4d 65 6d 2d  ->enc;.    pMem-
19aba 3e 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  >db = pKeyInfo->
19abb 64 62 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  db;.    pMem->fl
19abc 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65  ags = 0;.    pMe
19abd 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  m->zMalloc = 0;.
19abe 20 20 20 20 64 20 2b 3d 20 73 71 6c 69 74 65 33      d += sqlite3
19abf 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61  VdbeSerialGet(&a
19ac0 4b 65 79 5b 64 5d 2c 20 73 65 72 69 61 6c 5f 74  Key[d], serial_t
19ac1 79 70 65 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20  ype, pMem);.    
19ac2 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b  pMem++;.    u++;
19ac3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75  .  }.  assert( u
19ac4 3c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  <=pKeyInfo->nFie
19ac5 6c 64 20 2b 20 31 20 29 3b 0a 20 20 70 2d 3e 6e  ld + 1 );.  p->n
19ac6 46 69 65 6c 64 20 3d 20 75 3b 0a 20 20 72 65 74  Field = u;.  ret
19ac7 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a  urn (void*)p;.}.
19ac8 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
19ac9 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 55  ine destroys a U
19aca 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 6f 62  npackedRecord ob
19acb 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ject.*/.SQLITE_P
19acc 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
19acd 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70  te3VdbeDeleteUnp
19ace 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70 61  ackedRecord(Unpa
19acf 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 29 7b 0a  ckedRecord *p){.
19ad0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69    if( p ){.    i
19ad1 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e  f( p->flags & UN
19ad2 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54  PACKED_NEED_DEST
19ad3 52 4f 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ROY ){.      int
19ad4 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   i;.      Mem *p
19ad5 4d 65 6d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  Mem;.      for(i
19ad6 3d 30 2c 20 70 4d 65 6d 3d 70 2d 3e 61 4d 65 6d  =0, pMem=p->aMem
19ad7 3b 20 69 3c 70 2d 3e 6e 46 69 65 6c 64 3b 20 69  ; i<p->nField; i
19ad8 2b 2b 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20  ++, pMem++){.   
19ad9 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a       if( pMem->z
19ada 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20  Malloc ){.      
19adb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19adc 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
19add 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
19ade 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
19adf 20 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41   p->flags & UNPA
19ae0 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 29  CKED_NEED_FREE )
19ae1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
19ae2 62 46 72 65 65 28 70 2d 3e 70 4b 65 79 49 6e 66  bFree(p->pKeyInf
19ae3 6f 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d  o->db, p);.    }
19ae4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
19ae5 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70  is function comp
19ae6 61 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 62  ares the two tab
19ae7 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78  le rows or index
19ae8 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63   records.** spec
19ae9 69 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c  ified by {nKey1,
19aea 20 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65   pKey1} and pPKe
19aeb 79 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20  y2.  It returns 
19aec 61 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f  a negative, zero
19aed 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20  .** or positive 
19aee 69 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20  integer if key1 
19aef 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71  is less than, eq
19af0 75 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72  ual to or .** gr
19af1 65 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e  eater than key2.
19af2 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b    The {nKey1, pK
19af3 65 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65  ey1} key must be
19af4 20 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74   a blob.** creat
19af5 65 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65  ed by th OP_Make
19af6 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66  Record opcode of
19af7 20 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 20   the VDBE.  The 
19af8 70 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75  pPKey2.** key mu
19af9 73 74 20 62 65 20 61 20 70 61 72 73 65 64 20 6b  st be a parsed k
19afa 65 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 69  ey such as obtai
19afb 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69  ned from.** sqli
19afc 74 65 33 56 64 62 65 50 61 72 73 65 52 65 63 6f  te3VdbeParseReco
19afd 72 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61  rd..**.** Key1 a
19afe 6e 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68  nd Key2 do not h
19aff 61 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74  ave to contain t
19b00 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f  he same number o
19b01 66 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65  f fields..** The
19b02 20 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 20   key with fewer 
19b03 66 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c  fields is usuall
19b04 79 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20  y compares less 
19b05 74 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e  than the .** lon
19b06 67 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65  ger key.  Howeve
19b07 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45  r if the UNPACKE
19b08 44 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20  D_INCRKEY flags 
19b09 69 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 74  in pPKey2 is set
19b0a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d  .** and the comm
19b0b 6f 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 20  on prefixes are 
19b0c 65 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31  equal, then key1
19b0d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65   is less than ke
19b0e 79 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65  y2..** Or if the
19b0f 20 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f   UNPACKED_MATCH_
19b10 50 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 73  PREFIX flag is s
19b11 65 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 69  et and the prefi
19b12 78 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c  xes are.** equal
19b13 2c 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20  , then the keys 
19b14 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74  are considered t
19b15 6f 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a  o be equal and.*
19b16 2a 20 74 68 65 20 70 61 72 74 73 20 62 65 79 6f  * the parts beyo
19b17 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72  nd the common pr
19b18 65 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64  efix are ignored
19b19 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55  ..**.** If the U
19b1a 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52  NPACKED_IGNORE_R
19b1b 4f 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 74  OWID flag is set
19b1c 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20  , then the last 
19b1d 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68  byte of.** the h
19b1e 65 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69  eader of pKey1 i
19b1f 73 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69  s ignored.  It i
19b20 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 70  s assumed that p
19b21 4b 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e  Key1 is.** an in
19b22 64 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75  dex key, and thu
19b23 73 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f  s ends with a ro
19b24 77 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20  wid value.  The 
19b25 6c 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20  last byte.** of 
19b26 74 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20  the header will 
19b27 74 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 65  therefore be the
19b28 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20   serial type of 
19b29 74 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e  the rowid:.** on
19b2a 65 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c  e of 1, 2, 3, 4,
19b2b 20 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d   5, 6, 8, or 9 -
19b2c 20 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 72   the integer ser
19b2d 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68  ial types..** Th
19b2e 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66  e serial type of
19b2f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64   the final rowid
19b30 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20   will always be 
19b31 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a  a single byte..*
19b32 2a 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68  * By ignoring th
19b33 69 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20  is last byte of 
19b34 74 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 66  the header, we f
19b35 6f 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69  orce the compari
19b36 73 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65  son.** to ignore
19b37 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68   the rowid at th
19b38 65 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a  e end of key1..*
19b39 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19b3a 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
19b3b 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20  RecordCompare(. 
19b3c 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73   int nKey1, cons
19b3d 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f  t void *pKey1, /
19b3e 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20  * Left key */.  
19b3f 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
19b40 70 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a  pPKey2        /*
19b41 20 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b   Right key */.){
19b42 0a 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20  .  int d1;      
19b43 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
19b44 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e  into aKey[] of n
19b45 65 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74  ext data element
19b46 20 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20   */.  u32 idx1; 
19b47 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
19b48 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f  et into aKey[] o
19b49 66 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c  f next header el
19b4a 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73  ement */.  u32 s
19b4b 7a 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a  zHdr1;        /*
19b4c 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
19b4d 20 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20   in header */.  
19b4e 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74  int i = 0;.  int
19b4f 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72   nField;.  int r
19b50 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75  c = 0;.  const u
19b51 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b  nsigned char *aK
19b52 65 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73  ey1 = (const uns
19b53 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65  igned char *)pKe
19b54 79 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70  y1;.  KeyInfo *p
19b55 4b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d  KeyInfo;.  Mem m
19b56 65 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f  em1;..  pKeyInfo
19b57 20 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49   = pPKey2->pKeyI
19b58 6e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20  nfo;.  mem1.enc 
19b59 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b  = pKeyInfo->enc;
19b5a 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65  .  mem1.db = pKe
19b5b 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d  yInfo->db;.  mem
19b5c 31 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d  1.flags = 0;.  m
19b5d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b  em1.zMalloc = 0;
19b5e 0a 20 20 0a 20 20 69 64 78 31 20 3d 20 67 65 74  .  .  idx1 = get
19b5f 56 61 72 69 6e 74 33 32 28 61 4b 65 79 31 2c 20  Varint32(aKey1, 
19b60 73 7a 48 64 72 31 29 3b 0a 20 20 64 31 20 3d 20  szHdr1);.  d1 = 
19b61 73 7a 48 64 72 31 3b 0a 20 20 69 66 28 20 70 50  szHdr1;.  if( pP
19b62 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e  Key2->flags & UN
19b63 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f  PACKED_IGNORE_RO
19b64 57 49 44 20 29 7b 0a 20 20 20 20 73 7a 48 64 72  WID ){.    szHdr
19b65 31 2d 2d 3b 0a 20 20 7d 0a 20 20 6e 46 69 65 6c  1--;.  }.  nFiel
19b66 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  d = pKeyInfo->nF
19b67 69 65 6c 64 3b 0a 20 20 77 68 69 6c 65 28 20 69  ield;.  while( i
19b68 64 78 31 3c 73 7a 48 64 72 31 20 26 26 20 69 3c  dx1<szHdr1 && i<
19b69 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 20 29  pPKey2->nField )
19b6a 7b 0a 20 20 20 20 75 33 32 20 73 65 72 69 61 6c  {.    u32 serial
19b6b 5f 74 79 70 65 31 3b 0a 0a 20 20 20 20 2f 2a 20  _type1;..    /* 
19b6c 52 65 61 64 20 74 68 65 20 73 65 72 69 61 6c 20  Read the serial 
19b6d 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 6e 65  types for the ne
19b6e 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 65 61  xt element in ea
19b6f 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 69  ch key. */.    i
19b70 64 78 31 20 2b 3d 20 67 65 74 56 61 72 69 6e 74  dx1 += getVarint
19b71 33 32 28 20 61 4b 65 79 31 2b 69 64 78 31 2c 20  32( aKey1+idx1, 
19b72 73 65 72 69 61 6c 5f 74 79 70 65 31 20 29 3b 0a  serial_type1 );.
19b73 20 20 20 20 69 66 28 20 64 31 3e 3d 6e 4b 65 79      if( d1>=nKey
19b74 31 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65  1 && sqlite3Vdbe
19b75 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65  SerialTypeLen(se
19b76 72 69 61 6c 5f 74 79 70 65 31 29 3e 30 20 29 20  rial_type1)>0 ) 
19b77 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45  break;..    /* E
19b78 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65  xtract the value
19b79 73 20 74 6f 20 62 65 20 63 6f 6d 70 61 72 65 64  s to be compared
19b7a 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 31 20  ..    */.    d1 
19b7b 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  += sqlite3VdbeSe
19b7c 72 69 61 6c 47 65 74 28 26 61 4b 65 79 31 5b 64  rialGet(&aKey1[d
19b7d 31 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31  1], serial_type1
19b7e 2c 20 26 6d 65 6d 31 29 3b 0a 0a 20 20 20 20 2f  , &mem1);..    /
19b7f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 70 61 72 69  * Do the compari
19b80 73 6f 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  son.    */.    r
19b81 63 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f  c = sqlite3MemCo
19b82 6d 70 61 72 65 28 26 6d 65 6d 31 2c 20 26 70 50  mpare(&mem1, &pP
19b83 4b 65 79 32 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20  Key2->aMem[i],. 
19b84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19b85 20 20 20 20 20 20 20 20 20 20 69 3c 6e 46 69 65            i<nFie
19b86 6c 64 20 3f 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ld ? pKeyInfo->a
19b87 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29 3b 0a 20 20  Coll[i] : 0);.  
19b88 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20    if( rc!=0 ){. 
19b89 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
19b8a 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20  }.    i++;.  }. 
19b8b 20 69 66 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f   if( mem1.zMallo
19b8c 63 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c ) sqlite3VdbeM
19b8d 65 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29  emRelease(&mem1)
19b8e 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29  ;..  if( rc==0 )
19b8f 7b 0a 20 20 20 20 2f 2a 20 72 63 3d 3d 30 20 68  {.    /* rc==0 h
19b90 65 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20 6f  ere means that o
19b91 6e 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 72  ne of the keys r
19b92 61 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 73  an out of fields
19b93 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61 6c 6c 20   and.    ** all 
19b94 74 68 65 20 66 69 65 6c 64 73 20 75 70 20 74 6f  the fields up to
19b95 20 74 68 61 74 20 70 6f 69 6e 74 20 77 65 72 65   that point were
19b96 20 65 71 75 61 6c 2e 20 49 66 20 74 68 65 20 55   equal. If the U
19b97 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a  NPACKED_INCRKEY.
19b98 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73      ** flag is s
19b99 65 74 2c 20 74 68 65 6e 20 62 72 65 61 6b 20 74  et, then break t
19b9a 68 65 20 74 69 65 20 62 79 20 74 72 65 61 74 69  he tie by treati
19b9b 6e 67 20 6b 65 79 32 20 61 73 20 6c 61 72 67 65  ng key2 as large
19b9c 72 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65  r..    ** If the
19b9d 20 55 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f   UPACKED_PREFIX_
19b9e 4d 41 54 43 48 20 66 6c 61 67 20 69 73 20 73 65  MATCH flag is se
19b9f 74 2c 20 74 68 65 6e 20 6b 65 79 73 20 77 69 74  t, then keys wit
19ba0 68 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65  h common prefixe
19ba1 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 63 6f 6e  s.    ** are con
19ba2 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71  sidered to be eq
19ba3 75 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c  ual.  Otherwise,
19ba4 20 74 68 65 20 6c 6f 6e 67 65 72 20 6b 65 79 20   the longer key 
19ba5 69 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 6c  is the .    ** l
19ba6 61 72 67 65 72 2e 20 20 41 73 20 69 74 20 68 61  arger.  As it ha
19ba7 70 70 65 6e 73 2c 20 74 68 65 20 70 50 4b 65 79  ppens, the pPKey
19ba8 32 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65  2 will always be
19ba9 20 74 68 65 20 6c 6f 6e 67 65 72 0a 20 20 20 20   the longer.    
19baa 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ** if there is a
19bab 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20 20   difference..   
19bac 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 4b 65   */.    if( pPKe
19bad 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41  y2->flags & UNPA
19bae 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 7b 0a  CKED_INCRKEY ){.
19baf 20 20 20 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20        rc = -1;. 
19bb0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 4b     }else if( pPK
19bb1 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50  ey2->flags & UNP
19bb2 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
19bb3 43 48 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c  CH ){.      /* L
19bb4 65 61 76 65 20 72 63 3d 3d 30 20 2a 2f 0a 20 20  eave rc==0 */.  
19bb5 20 20 7d 65 6c 73 65 20 69 66 28 20 69 64 78 31    }else if( idx1
19bb6 3c 73 7a 48 64 72 31 20 29 7b 0a 20 20 20 20 20  <szHdr1 ){.     
19bb7 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20   rc = 1;.    }. 
19bb8 20 7d 65 6c 73 65 20 69 66 28 20 70 4b 65 79 49   }else if( pKeyI
19bb9 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
19bba 26 26 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e  && i<pKeyInfo->n
19bbb 46 69 65 6c 64 0a 20 20 20 20 20 20 20 20 20 20  Field.          
19bbc 20 20 20 20 20 26 26 20 70 4b 65 79 49 6e 66 6f       && pKeyInfo
19bbd 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20  ->aSortOrder[i] 
19bbe 29 7b 0a 20 20 20 20 72 63 20 3d 20 2d 72 63 3b  ){.    rc = -rc;
19bbf 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
19bc0 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43  c;.}. ../*.** pC
19bc1 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20  ur points at an 
19bc2 69 6e 64 65 78 20 65 6e 74 72 79 20 63 72 65 61  index entry crea
19bc3 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f 50  ted using the OP
19bc4 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f  _MakeRecord opco
19bc5 64 65 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20  de..** Read the 
19bc6 72 6f 77 69 64 20 28 74 68 65 20 6c 61 73 74 20  rowid (the last 
19bc7 66 69 65 6c 64 20 69 6e 20 74 68 65 20 72 65 63  field in the rec
19bc8 6f 72 64 29 20 61 6e 64 20 73 74 6f 72 65 20 69  ord) and store i
19bc9 74 20 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20  t in *rowid..** 
19bca 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
19bcb 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   if everything w
19bcc 6f 72 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f  orks, or an erro
19bcd 72 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65  r code otherwise
19bce 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 67  ..**.** pCur mig
19bcf 68 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74  ht be pointing t
19bd0 6f 20 74 65 78 74 20 6f 62 74 61 69 6e 65 64 20  o text obtained 
19bd1 66 72 6f 6d 20 61 20 63 6f 72 72 75 70 74 20 64  from a corrupt d
19bd2 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
19bd3 20 53 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   So the content 
19bd4 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65  cannot be truste
19bd5 64 2e 20 20 44 6f 20 61 70 70 72 6f 70 72 69 61  d.  Do appropria
19bd6 74 65 20 63 68 65 63 6b 73 20 6f 6e 20 74 68 65  te checks on the
19bd7 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c   content..*/.SQL
19bd8 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19bd9 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f  sqlite3VdbeIdxRo
19bda 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43  wid(BtCursor *pC
19bdb 75 72 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b  ur, i64 *rowid){
19bdc 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20  .  i64 nCellKey 
19bdd 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  = 0;.  int rc;. 
19bde 20 75 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20   u32 szHdr;     
19bdf 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
19be0 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33  e header */.  u3
19be1 32 20 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20  2 typeRowid;    
19be2 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f  /* Serial type o
19be3 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20  f the rowid */. 
19be4 20 75 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20   u32 lenRowid;  
19be5 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
19be6 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d  e rowid */.  Mem
19be7 20 6d 2c 20 76 3b 0a 0a 20 20 2f 2a 20 47 65 74   m, v;..  /* Get
19be8 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
19be9 20 69 6e 64 65 78 20 65 6e 74 72 79 2e 20 20 4f   index entry.  O
19bea 6e 6c 79 20 69 6e 64 69 63 65 73 20 65 6e 74 72  nly indices entr
19beb 69 65 73 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a  ies of less.  **
19bec 20 74 68 61 6e 20 32 47 69 42 20 61 72 65 20 73   than 2GiB are s
19bed 75 70 70 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e  upport - anythin
19bee 67 20 6c 61 72 67 65 20 6d 75 73 74 20 62 65 20  g large must be 
19bef 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
19bf0 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ion */.  sqlite3
19bf1 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75  BtreeKeySize(pCu
19bf2 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20  r, &nCellKey);. 
19bf3 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 6e 43   if( unlikely(nC
19bf4 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65  ellKey<=0 || nCe
19bf5 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66  llKey>0x7fffffff
19bf6 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
19bf7 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
19bf8 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  KPT;.  }..  /* R
19bf9 65 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70 6c  ead in the compl
19bfa 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  ete content of t
19bfb 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a  he index entry *
19bfc 2f 0a 20 20 6d 2e 66 6c 61 67 73 20 3d 20 30 3b  /.  m.flags = 0;
19bfd 0a 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 6d  .  m.db = 0;.  m
19bfe 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  .zMalloc = 0;.  
19bff 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
19c00 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 75  MemFromBtree(pCu
19c01 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c  r, 0, (int)nCell
19c02 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69  Key, 1, &m);.  i
19c03 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
19c04 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
19c05 2a 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74 72  * The index entr
19c06 79 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69 74  y must begin wit
19c07 68 20 61 20 68 65 61 64 65 72 20 73 69 7a 65 20  h a header size 
19c08 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61  */.  (void)getVa
19c09 72 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c  rint32((u8*)m.z,
19c0a 20 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74 63   szHdr);.  testc
19c0b 61 73 65 28 20 73 7a 48 64 72 3d 3d 32 20 29 3b  ase( szHdr==2 );
19c0c 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 48  .  testcase( szH
19c0d 64 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 28  dr==m.n );.  if(
19c0e 20 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 3c   unlikely(szHdr<
19c0f 32 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72 3e  2 || (int)szHdr>
19c10 6d 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  m.n) ){.    goto
19c11 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75   idx_rowid_corru
19c12 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ption;.  }..  /*
19c13 20 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20   The last field 
19c14 6f 66 20 74 68 65 20 69 6e 64 65 78 20 73 68 6f  of the index sho
19c15 75 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67 65  uld be an intege
19c16 72 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a 20  r - the ROWID.. 
19c17 20 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20   ** Verify that 
19c18 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 72  the last entry r
19c19 65 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74 65  eally is an inte
19c1a 67 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 29  ger. */.  (void)
19c1b 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a  getVarint32((u8*
19c1c 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20  )&m.z[szHdr-1], 
19c1d 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65  typeRowid);.  te
19c1e 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69  stcase( typeRowi
19c1f 64 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63 61  d==1 );.  testca
19c20 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 32  se( typeRowid==2
19c21 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
19c22 74 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0a  typeRowid==3 );.
19c23 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65    testcase( type
19c24 52 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74 65  Rowid==4 );.  te
19c25 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69  stcase( typeRowi
19c26 64 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63 61  d==5 );.  testca
19c27 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 36  se( typeRowid==6
19c28 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
19c29 74 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b 0a  typeRowid==8 );.
19c2a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65    testcase( type
19c2b 52 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69 66  Rowid==9 );.  if
19c2c 28 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65 52  ( unlikely(typeR
19c2d 6f 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52 6f  owid<1 || typeRo
19c2e 77 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f 77  wid>9 || typeRow
19c2f 69 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67 6f  id==7) ){.    go
19c30 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72  to idx_rowid_cor
19c31 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c  ruption;.  }.  l
19c32 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  enRowid = sqlite
19c33 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
19c34 65 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 20  en(typeRowid);. 
19c35 20 74 65 73 74 63 61 73 65 28 20 6d 2e 6e 2d 6c   testcase( m.n-l
19c36 65 6e 52 6f 77 69 64 3d 3d 73 7a 48 64 72 20 29  enRowid==szHdr )
19c37 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79  ;.  if( unlikely
19c38 28 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 3c 73 7a  (m.n-lenRowid<sz
19c39 48 64 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Hdr) ){.    goto
19c3a 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75   idx_rowid_corru
19c3b 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ption;.  }..  /*
19c3c 20 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 67   Fetch the integ
19c3d 65 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f  er off the end o
19c3e 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f  f the index reco
19c3f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  rd */.  sqlite3V
19c40 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38  dbeSerialGet((u8
19c41 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f  *)&m.z[m.n-lenRo
19c42 77 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 2c  wid], typeRowid,
19c43 20 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 3d   &v);.  *rowid =
19c44 20 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 65   v.u.i;.  sqlite
19c45 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
19c46 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  &m);.  return SQ
19c47 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a  LITE_OK;..  /* J
19c48 75 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 61  ump here if data
19c49 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
19c4a 69 73 20 64 65 74 65 63 74 65 64 20 61 66 74 65  is detected afte
19c4b 72 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 2a  r m has been.  *
19c4c 2a 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 72  * allocated.  Fr
19c4d 65 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 20  ee the m object 
19c4e 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
19c4f 45 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 64  E_CORRUPT. */.id
19c50 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69  x_rowid_corrupti
19c51 6f 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 20  on:.  testcase( 
19c52 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b 0a  m.zMalloc!=0 );.
19c53 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19c54 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72  Release(&m);.  r
19c55 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
19c56 52 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a  RUPT_BKPT;.}../*
19c57 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
19c58 6b 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78  key of the index
19c59 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73   entry that curs
19c5a 6f 72 20 70 43 20 69 73 20 70 6f 69 6e 74 20 74  or pC is point t
19c5b 6f 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65  o against.** the
19c5c 20 6b 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70   key string in p
19c5d 4b 65 79 20 28 6f 66 20 6c 65 6e 67 74 68 20 6e  Key (of length n
19c5e 4b 65 79 29 2e 20 20 57 72 69 74 65 20 69 6e 74  Key).  Write int
19c5f 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72  o *pRes a number
19c60 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61  .** that is nega
19c61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70  tive, zero, or p
19c62 6f 73 69 74 69 76 65 20 69 66 20 70 43 20 69 73  ositive if pC is
19c63 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61   less than, equa
19c64 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61  l to,.** or grea
19c65 74 65 72 20 74 68 61 6e 20 70 4b 65 79 2e 20 20  ter than pKey.  
19c66 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
19c67 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a   on success..**.
19c68 2a 2a 20 70 4b 65 79 20 69 73 20 65 69 74 68 65  ** pKey is eithe
19c69 72 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75  r created withou
19c6a 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20  t a rowid or is 
19c6b 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61  truncated so tha
19c6c 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68  t it.** omits th
19c6d 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65  e rowid at the e
19c6e 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61  nd.  The rowid a
19c6f 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
19c70 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20   index entry.** 
19c71 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65  is ignored as we
19c72 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73  ll.  Hence, this
19c73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f   routine only co
19c74 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69  mpares the prefi
19c75 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b  xes .** of the k
19c76 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65  eys prior to the
19c77 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f   final rowid, no
19c78 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79  t the entire key
19c79 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65  ..**.** pUnpacke
19c7a 64 20 6d 61 79 20 62 65 20 61 6e 20 75 6e 70 61  d may be an unpa
19c7b 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  cked version of 
19c7c 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 49 66 20 70  pKey,nKey.  If p
19c7d 55 6e 70 61 63 6b 65 64 20 69 73 0a 2a 2a 20 73  Unpacked is.** s
19c7e 75 70 70 6c 69 65 64 20 69 74 20 69 73 20 75 73  upplied it is us
19c7f 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 70  ed in place of p
19c80 4b 65 79 2c 6e 4b 65 79 2e 0a 2a 2f 0a 53 51 4c  Key,nKey..*/.SQL
19c81 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19c82 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65  sqlite3VdbeIdxKe
19c83 79 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65  yCompare(.  Vdbe
19c84 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20  Cursor *pC,     
19c85 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
19c86 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65  ursor to compare
19c87 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e   against */.  Un
19c88 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55  packedRecord *pU
19c89 6e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70  npacked,  /* Unp
19c8a 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  acked version of
19c8b 20 70 4b 65 79 20 61 6e 64 20 6e 4b 65 79 20 2a   pKey and nKey *
19c8c 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 20  /.  int *res    
19c8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c8e 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d  /* Write the com
19c8f 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 68  parison result h
19c90 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20  ere */.){.  i64 
19c91 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20  nCellKey = 0;.  
19c92 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 73  int rc;.  BtCurs
19c93 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70  or *pCur = pC->p
19c94 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b  Cursor;.  Mem m;
19c95 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
19c96 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e  KeySize(pCur, &n
19c97 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 69 66 28 20  CellKey);.  if( 
19c98 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e  nCellKey<=0 || n
19c99 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66  CellKey>0x7fffff
19c9a 66 66 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d  ff ){.    *res =
19c9b 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0;.    return S
19c9c 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
19c9d 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 6d 2e 66 6c  m.db = 0;.  m.fl
19c9e 61 67 73 20 3d 20 30 3b 0a 20 20 6d 2e 7a 4d 61  ags = 0;.  m.zMa
19c9f 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 72 63 20 3d  lloc = 0;.  rc =
19ca0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
19ca1 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75  romBtree(pC->pCu
19ca2 72 73 6f 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43  rsor, 0, (int)nC
19ca3 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a  ellKey, 1, &m);.
19ca4 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
19ca5 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
19ca6 20 61 73 73 65 72 74 28 20 70 55 6e 70 61 63 6b   assert( pUnpack
19ca7 65 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41  ed->flags & UNPA
19ca8 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49  CKED_IGNORE_ROWI
19ca9 44 20 29 3b 0a 20 20 2a 72 65 73 20 3d 20 73 71  D );.  *res = sq
19caa 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
19cab 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c  ompare(m.n, m.z,
19cac 20 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20 20 73   pUnpacked);.  s
19cad 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
19cae 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75  ease(&m);.  retu
19caf 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
19cb0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
19cb1 69 6e 65 20 73 65 74 73 20 74 68 65 20 76 61 6c  ine sets the val
19cb2 75 65 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  ue to be returne
19cb3 64 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20  d by subsequent 
19cb4 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69  calls to.** sqli
19cb5 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e  te3_changes() on
19cb6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
19cb7 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53  ndle 'db'. .*/.S
19cb8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
19cb9 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
19cba 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33  tChanges(sqlite3
19cbb 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67   *db, int nChang
19cbc 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  e){.  assert( sq
19cbd 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
19cbe 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
19cbf 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e   db->nChange = n
19cc0 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54  Change;.  db->nT
19cc1 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43  otalChange += nC
19cc2 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  hange;.}../*.** 
19cc3 53 65 74 20 61 20 66 6c 61 67 20 69 6e 20 74 68  Set a flag in th
19cc4 65 20 76 64 62 65 20 74 6f 20 75 70 64 61 74 65  e vdbe to update
19cc5 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e   the change coun
19cc6 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 20 66  ter when it is f
19cc7 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72 20 72  inalised.** or r
19cc8 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eset..*/.SQLITE_
19cc9 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19cca 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61  ite3VdbeCountCha
19ccb 6e 67 65 73 28 56 64 62 65 20 2a 76 29 7b 0a 20  nges(Vdbe *v){. 
19ccc 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20   v->changeCntOn 
19ccd 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  = 1;.}../*.** Ma
19cce 72 6b 20 65 76 65 72 79 20 70 72 65 70 61 72 65  rk every prepare
19ccf 64 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 6f  d statement asso
19cd0 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61  ciated with a da
19cd1 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
19cd2 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72 65 64 2e  n.** as expired.
19cd3 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65  .**.** An expire
19cd4 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e  d statement mean
19cd5 73 20 74 68 61 74 20 72 65 63 6f 6d 70 69 6c 61  s that recompila
19cd6 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74  tion of the stat
19cd7 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65 63 6f  ement is.** reco
19cd8 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e  mmend.  Statemen
19cd9 74 73 20 65 78 70 69 72 65 20 77 68 65 6e 20 74  ts expire when t
19cda 68 69 6e 67 73 20 68 61 70 70 65 6e 20 74 68 61  hings happen tha
19cdb 74 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a 2a 20  t make their.** 
19cdc 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74  programs obsolet
19cdd 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75 73 65  e.  Removing use
19cde 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  r-defined functi
19cdf 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67  ons or collating
19ce0 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c 20 6f  .** sequences, o
19ce1 72 20 63 68 61 6e 67 69 6e 67 20 61 6e 20 61 75  r changing an au
19ce2 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63  thorization func
19ce3 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 79 70  tion are the typ
19ce4 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67 73 20  es of.** things 
19ce5 74 68 61 74 20 6d 61 6b 65 20 70 72 65 70 61 72  that make prepar
19ce6 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 62  ed statements ob
19ce7 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54  solete..*/.SQLIT
19ce8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19ce9 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
19cea 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73  aredStatements(s
19ceb 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56  qlite3 *db){.  V
19cec 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 70 20  dbe *p;.  for(p 
19ced 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20 70 3b 20  = db->pVdbe; p; 
19cee 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
19cef 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b   p->expired = 1;
19cf0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
19cf1 74 75 72 6e 20 74 68 65 20 64 61 74 61 62 61 73  turn the databas
19cf2 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
19cf3 68 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f 0a 53  h the Vdbe..*/.S
19cf4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
19cf5 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64  lite3 *sqlite3Vd
19cf6 62 65 44 62 28 56 64 62 65 20 2a 76 29 7b 0a 20  beDb(Vdbe *v){. 
19cf7 20 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b 0a 7d   return v->db;.}
19cf8 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
19cf9 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 75 78  * End of vdbeaux
19cfa 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
19cfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19cfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19cfd 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
19cfe 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
19cff 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eapi.c *********
19d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d02 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
19d03 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   26.**.** The au
19d04 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
19d05 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
19d06 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
19d07 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
19d08 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
19d09 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
19d0a 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
19d0b 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
19d0c 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
19d0d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
19d0e 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
19d0f 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
19d10 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
19d11 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
19d12 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
19d13 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
19d14 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
19d15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d19 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
19d1a 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
19d1b 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e   use to implemen
19d1c 74 20 41 50 49 73 20 74 68 61 74 20 61 72 65 20  t APIs that are 
19d1d 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56  part of the.** V
19d1e 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  DBE..**.** $Id: 
19d1f 76 64 62 65 61 70 69 2e 63 2c 76 20 31 2e 31 35  vdbeapi.c,v 1.15
19d20 36 20 32 30 30 39 2f 30 33 2f 32 35 20 31 35 3a  6 2009/03/25 15:
19d21 34 33 3a 30 39 20 64 61 6e 69 65 6c 6b 31 39 37  43:09 danielk197
19d22 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20  7 Exp $.*/..#if 
19d23 30 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  0 && defined(SQL
19d24 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
19d25 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 29 0a 2f 2a  Y_MANAGEMENT)./*
19d26 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
19d27 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  g structure cont
19d28 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f  ains pointers to
19d29 20 74 68 65 20 65 6e 64 20 70 6f 69 6e 74 73 20   the end points 
19d2a 6f 66 20 61 0a 2a 2a 20 64 6f 75 62 6c 79 2d 6c  of a.** doubly-l
19d2b 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c  inked list of al
19d2c 6c 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  l compiled SQL s
19d2d 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 6d  tatements that m
19d2e 61 79 20 62 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a  ay be holding.**
19d2f 20 62 75 66 66 65 72 73 20 65 6c 69 67 69 62 6c   buffers eligibl
19d30 65 20 66 6f 72 20 72 65 6c 65 61 73 65 20 77 68  e for release wh
19d31 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  en the sqlite3_r
19d32 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
19d33 69 6e 74 65 72 66 61 63 65 20 69 73 0a 2a 2a 20  interface is.** 
19d34 69 6e 76 6f 6b 65 64 2e 20 41 63 63 65 73 73 20  invoked. Access 
19d35 74 6f 20 74 68 69 73 20 6c 69 73 74 20 69 73 20  to this list is 
19d36 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65  protected by the
19d37 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
19d38 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 6d 75 74  ATIC_LRU2.** mut
19d39 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d  ex..**.** Statem
19d3a 65 6e 74 73 20 61 72 65 20 61 64 64 65 64 20 74  ents are added t
19d3b 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69  o the end of thi
19d3c 73 20 6c 69 73 74 20 77 68 65 6e 20 73 71 6c 69  s list when sqli
19d3d 74 65 33 5f 72 65 73 65 74 28 29 20 69 73 0a 2a  te3_reset() is.*
19d3e 2a 20 63 61 6c 6c 65 64 2e 20 54 68 65 79 20 61  * called. They a
19d3f 72 65 20 72 65 6d 6f 76 65 64 20 65 69 74 68 65  re removed eithe
19d40 72 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 73  r when sqlite3_s
19d41 74 65 70 28 29 20 6f 72 20 73 71 6c 69 74 65 33  tep() or sqlite3
19d42 5f 66 69 6e 61 6c 69 7a 65 28 29 0a 2a 2a 20 69  _finalize().** i
19d43 73 20 63 61 6c 6c 65 64 2e 20 57 68 65 6e 20 73  s called. When s
19d44 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 61 64  tatements are ad
19d45 64 65 64 20 74 6f 20 74 68 69 73 20 6c 69 73 74  ded to this list
19d46 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  , the associated
19d47 20 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 72   .** register ar
19d48 72 61 79 20 28 70 2d 3e 61 4d 65 6d 5b 31 2e 2e  ray (p->aMem[1..
19d49 70 2d 3e 6e 4d 65 6d 5d 29 20 6d 61 79 20 63 6f  p->nMem]) may co
19d4a 6e 74 61 69 6e 20 64 79 6e 61 6d 69 63 20 62 75  ntain dynamic bu
19d4b 66 66 65 72 73 20 74 68 61 74 0a 2a 2a 20 63 61  ffers that.** ca
19d4c 6e 20 62 65 20 66 72 65 65 64 20 75 73 69 6e 67  n be freed using
19d4d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65   sqlite3VdbeRele
19d4e 61 73 65 4d 65 6d 6f 72 79 28 29 2e 0a 2a 2a 0a  aseMemory()..**.
19d4f 2a 2a 20 57 68 65 6e 20 73 74 61 74 65 6d 65 6e  ** When statemen
19d50 74 73 20 61 72 65 20 61 64 64 65 64 20 6f 72 20  ts are added or 
19d51 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 69  removed from thi
19d52 73 20 6c 69 73 74 2c 20 74 68 65 20 6d 75 74 65  s list, the mute
19d53 78 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  x.** associated 
19d54 77 69 74 68 20 74 68 65 20 56 64 62 65 20 62 65  with the Vdbe be
19d55 69 6e 67 20 61 64 64 65 64 20 6f 72 20 72 65 6d  ing added or rem
19d56 6f 76 65 64 20 28 56 64 62 65 2e 64 62 2d 3e 6d  oved (Vdbe.db->m
19d57 75 74 65 78 29 20 69 73 0a 2a 2a 20 61 6c 72 65  utex) is.** alre
19d58 61 64 79 20 68 65 6c 64 2e 20 54 68 65 20 4c 52  ady held. The LR
19d59 55 32 20 6d 75 74 65 78 20 69 73 20 74 68 65 6e  U2 mutex is then
19d5a 20 6f 62 74 61 69 6e 65 64 2c 20 62 6c 6f 63 6b   obtained, block
19d5b 69 6e 67 20 69 66 20 6e 65 63 65 73 73 61 72 79  ing if necessary
19d5c 2c 0a 2a 2a 20 74 68 65 20 6c 69 6e 6b 65 64 2d  ,.** the linked-
19d5d 6c 69 73 74 20 70 6f 69 6e 74 65 72 73 20 6d 61  list pointers ma
19d5e 6e 69 70 75 6c 61 74 65 64 20 61 6e 64 20 74 68  nipulated and th
19d5f 65 20 4c 52 55 32 20 6d 75 74 65 78 20 72 65 6c  e LRU2 mutex rel
19d60 69 6e 71 75 69 73 68 65 64 2e 0a 2a 2f 0a 73 74  inquished..*/.st
19d61 72 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c 72  ruct StatementLr
19d62 75 4c 69 73 74 20 7b 0a 20 20 56 64 62 65 20 2a  uList {.  Vdbe *
19d63 70 46 69 72 73 74 3b 0a 20 20 56 64 62 65 20 2a  pFirst;.  Vdbe *
19d64 70 4c 61 73 74 3b 0a 7d 3b 0a 73 74 61 74 69 63  pLast;.};.static
19d65 20 73 74 72 75 63 74 20 53 74 61 74 65 6d 65 6e   struct Statemen
19d66 74 4c 72 75 4c 69 73 74 20 73 71 6c 69 74 65 33  tLruList sqlite3
19d67 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 3b 0a 0a  LruStatements;..
19d68 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74  /*.** Check that
19d69 20 74 68 65 20 6c 69 73 74 20 6c 6f 6f 6b 73 20   the list looks 
19d6a 74 6f 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79  to be internally
19d6b 20 63 6f 6e 73 69 73 74 65 6e 74 2e 20 54 68 69   consistent. Thi
19d6c 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 61 73 20  s is used.** as 
19d6d 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 72  part of an asser
19d6e 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73  t() statement as
19d6f 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
19d70 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72    assert( stmtLr
19d71 75 43 68 65 63 6b 28 29 20 29 3b 0a 2a 2f 0a 23  uCheck() );.*/.#
19d72 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
19d73 61 74 69 63 20 69 6e 74 20 73 74 6d 74 4c 72 75  atic int stmtLru
19d74 43 68 65 63 6b 28 29 7b 0a 20 20 56 64 62 65 20  Check(){.  Vdbe 
19d75 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69  *p;.  for(p=sqli
19d76 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
19d77 2e 70 46 69 72 73 74 3b 20 70 3b 20 70 3d 70 2d  .pFirst; p; p=p-
19d78 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 20  >pLruNext){.    
19d79 61 73 73 65 72 74 28 70 2d 3e 70 4c 72 75 4e 65  assert(p->pLruNe
19d7a 78 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33  xt || p==sqlite3
19d7b 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c  LruStatements.pL
19d7c 61 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ast);.    assert
19d7d 28 21 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c  (!p->pLruNext ||
19d7e 20 70 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 4c   p->pLruNext->pL
19d7f 72 75 50 72 65 76 3d 3d 70 29 3b 0a 20 20 20 20  ruPrev==p);.    
19d80 61 73 73 65 72 74 28 70 2d 3e 70 4c 72 75 50 72  assert(p->pLruPr
19d81 65 76 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33  ev || p==sqlite3
19d82 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46  LruStatements.pF
19d83 69 72 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72  irst);.    asser
19d84 74 28 21 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c  t(!p->pLruPrev |
19d85 7c 20 70 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70  | p->pLruPrev->p
19d86 4c 72 75 4e 65 78 74 3d 3d 70 29 3b 0a 20 20 7d  LruNext==p);.  }
19d87 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
19d88 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  endif../*.** Add
19d89 20 76 64 62 65 20 70 20 74 6f 20 74 68 65 20 65   vdbe p to the e
19d8a 6e 64 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  nd of the statem
19d8b 65 6e 74 20 6c 72 75 20 6c 69 73 74 2e 20 49 74  ent lru list. It
19d8c 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
19d8d 0a 2a 2a 20 70 20 69 73 20 6e 6f 74 20 61 6c 72  .** p is not alr
19d8e 65 61 64 79 20 70 61 72 74 20 6f 66 20 74 68 65  eady part of the
19d8f 20 6c 69 73 74 20 77 68 65 6e 20 74 68 69 73 20   list when this 
19d90 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20 6c  is called. The l
19d91 72 75 20 6c 69 73 74 0a 2a 2a 20 69 73 20 70 72  ru list.** is pr
19d92 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53  otected by the S
19d93 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
19d94 49 43 5f 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f  IC_LRU mutex..*/
19d95 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 6d  .static void stm
19d96 74 4c 72 75 41 64 64 28 56 64 62 65 20 2a 70 29  tLruAdd(Vdbe *p)
19d97 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
19d98 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d  x_enter(sqlite3M
19d99 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
19d9a 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
19d9b 55 32 29 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e  U2));..  if( p->
19d9c 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70  pLruPrev || p->p
19d9d 4c 72 75 4e 65 78 74 20 7c 7c 20 73 71 6c 69 74  LruNext || sqlit
19d9e 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e  e3LruStatements.
19d9f 70 46 69 72 73 74 3d 3d 70 20 29 7b 0a 20 20 20  pFirst==p ){.   
19da0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
19da1 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65  eave(sqlite3Mute
19da2 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
19da3 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29  TEX_STATIC_LRU2)
19da4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
19da5 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 73 74   }..  assert( st
19da6 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a  mtLruCheck() );.
19da7 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 4c  .  if( !sqlite3L
19da8 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69  ruStatements.pFi
19da9 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  rst ){.    asser
19daa 74 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53 74  t( !sqlite3LruSt
19dab 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20 29  atements.pLast )
19dac 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 75  ;.    sqlite3Lru
19dad 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73  Statements.pFirs
19dae 74 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74  t = p;.    sqlit
19daf 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e  e3LruStatements.
19db0 70 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d 65 6c  pLast = p;.  }el
19db1 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
19db2 21 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65  !sqlite3LruState
19db3 6d 65 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c 72  ments.pLast->pLr
19db4 75 4e 65 78 74 20 29 3b 0a 20 20 20 20 70 2d 3e  uNext );.    p->
19db5 70 4c 72 75 50 72 65 76 20 3d 20 73 71 6c 69 74  pLruPrev = sqlit
19db6 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e  e3LruStatements.
19db7 70 4c 61 73 74 3b 0a 20 20 20 20 73 71 6c 69 74  pLast;.    sqlit
19db8 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e  e3LruStatements.
19db9 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65 78 74 20  pLast->pLruNext 
19dba 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = p;.    sqlite3
19dbb 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c  LruStatements.pL
19dbc 61 73 74 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20  ast = p;.  }..  
19dbd 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43  assert( stmtLruC
19dbe 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20 73 71 6c  heck() );..  sql
19dbf 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
19dc0 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
19dc1 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
19dc2 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 7d  STATIC_LRU2));.}
19dc3 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67  ../*.** Assuming
19dc4 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45   the SQLITE_MUTE
19dc5 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 6d 75  X_STATIC_LRU2 mu
19dc6 74 65 78 74 20 69 73 20 61 6c 72 65 61 64 79 20  text is already 
19dc7 68 65 6c 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20  held, remove.** 
19dc8 73 74 61 74 65 6d 65 6e 74 20 70 20 66 72 6f 6d  statement p from
19dc9 20 74 68 65 20 6c 65 61 73 74 2d 72 65 63 65 6e   the least-recen
19dca 74 6c 79 2d 75 73 65 64 20 73 74 61 74 65 6d 65  tly-used stateme
19dcb 6e 74 20 6c 69 73 74 2e 20 49 66 20 74 68 65 20  nt list. If the 
19dcc 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73  .** statement is
19dcd 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70   not currently p
19dce 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2c  art of the list,
19dcf 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20   this call is a 
19dd0 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
19dd1 20 76 6f 69 64 20 73 74 6d 74 4c 72 75 52 65 6d   void stmtLruRem
19dd2 6f 76 65 4e 6f 6d 75 74 65 78 28 56 64 62 65 20  oveNomutex(Vdbe 
19dd3 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4c  *p){.  if( p->pL
19dd4 72 75 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72  ruPrev || p->pLr
19dd5 75 4e 65 78 74 20 7c 7c 20 70 3d 3d 73 71 6c 69  uNext || p==sqli
19dd6 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
19dd7 2e 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20 61  .pFirst ){.    a
19dd8 73 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68  ssert( stmtLruCh
19dd9 65 63 6b 28 29 20 29 3b 0a 20 20 20 20 69 66 28  eck() );.    if(
19dda 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a   p->pLruNext ){.
19ddb 20 20 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65 78        p->pLruNex
19ddc 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 2d  t->pLruPrev = p-
19ddd 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d  >pLruPrev;.    }
19dde 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
19ddf 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
19de0 2e 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 72 75  .pLast = p->pLru
19de1 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Prev;.    }.    
19de2 69 66 28 20 70 2d 3e 70 4c 72 75 50 72 65 76 20  if( p->pLruPrev 
19de3 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72 75  ){.      p->pLru
19de4 50 72 65 76 2d 3e 70 4c 72 75 4e 65 78 74 20 3d  Prev->pLruNext =
19de5 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20   p->pLruNext;.  
19de6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
19de7 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65  qlite3LruStateme
19de8 6e 74 73 2e 70 46 69 72 73 74 20 3d 20 70 2d 3e  nts.pFirst = p->
19de9 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  pLruNext;.    }.
19dea 20 20 20 20 70 2d 3e 70 4c 72 75 4e 65 78 74 20      p->pLruNext 
19deb 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 72 75  = 0;.    p->pLru
19dec 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 61 73  Prev = 0;.    as
19ded 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65  sert( stmtLruChe
19dee 63 6b 28 29 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ck() );.  }.}../
19def 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68  *.** Assuming th
19df0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
19df1 54 41 54 49 43 5f 4c 52 55 32 20 6d 75 74 65 78  TATIC_LRU2 mutex
19df2 74 20 69 73 20 6e 6f 74 20 68 65 6c 64 2c 20 72  t is not held, r
19df3 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 74 65 6d 65  emove.** stateme
19df4 6e 74 20 70 20 66 72 6f 6d 20 74 68 65 20 6c 65  nt p from the le
19df5 61 73 74 2d 72 65 63 65 6e 74 6c 79 2d 75 73 65  ast-recently-use
19df6 64 20 73 74 61 74 65 6d 65 6e 74 20 6c 69 73 74  d statement list
19df7 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 73 74 61  . If the .** sta
19df8 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 63 75  tement is not cu
19df9 72 72 65 6e 74 6c 79 20 70 61 72 74 20 6f 66 20  rrently part of 
19dfa 74 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20 63  the list, this c
19dfb 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  all is a no-op..
19dfc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
19dfd 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28 56 64 62  tmtLruRemove(Vdb
19dfe 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  e *p){.  sqlite3
19dff 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c  _mutex_enter(sql
19e00 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
19e01 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
19e02 49 43 5f 4c 52 55 32 29 29 3b 0a 20 20 73 74 6d  IC_LRU2));.  stm
19e03 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65  tLruRemoveNomute
19e04 78 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  x(p);.  sqlite3_
19e05 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69  mutex_leave(sqli
19e06 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
19e07 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
19e08 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a 0a 2f 2a 0a  C_LRU2));.}../*.
19e09 2a 2a 20 54 72 79 20 74 6f 20 72 65 6c 65 61 73  ** Try to releas
19e0a 65 20 6e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  e n bytes of mem
19e0b 6f 72 79 20 62 79 20 66 72 65 65 69 6e 67 20 62  ory by freeing b
19e0c 75 66 66 65 72 73 20 61 73 73 6f 63 69 61 74 65  uffers associate
19e0d 64 20 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6d  d .** with the m
19e0e 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20  emory registers 
19e0f 6f 66 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 75  of currently unu
19e10 73 65 64 20 76 64 62 65 73 2e 0a 2a 2f 0a 53 51  sed vdbes..*/.SQ
19e11 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19e12 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65   sqlite3VdbeRele
19e13 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 29  aseMemory(int n)
19e14 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 56  {.  Vdbe *p;.  V
19e15 64 62 65 20 2a 70 4e 65 78 74 3b 0a 20 20 69 6e  dbe *pNext;.  in
19e16 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20  t nFree = 0;..  
19e17 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
19e18 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ter(sqlite3Mutex
19e19 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
19e1a 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29  EX_STATIC_LRU2))
19e1b 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65  ;.  for(p=sqlite
19e1c 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70  3LruStatements.p
19e1d 46 69 72 73 74 3b 20 70 20 26 26 20 6e 46 72 65  First; p && nFre
19e1e 65 3c 6e 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20  e<n; p=pNext){. 
19e1f 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4c     pNext = p->pL
19e20 72 75 4e 65 78 74 3b 0a 0a 20 20 20 20 2f 2a 20  ruNext;..    /* 
19e21 46 6f 72 20 65 61 63 68 20 73 74 61 74 65 6d 65  For each stateme
19e22 6e 74 20 68 61 6e 64 6c 65 20 69 6e 20 74 68 65  nt handle in the
19e23 20 6c 72 75 20 6c 69 73 74 2c 20 61 74 74 65 6d   lru list, attem
19e24 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  pt to obtain the
19e25 0a 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74  .    ** associat
19e26 65 64 20 64 61 74 61 62 61 73 65 20 6d 75 74 65  ed database mute
19e27 78 2e 20 49 66 20 69 74 20 63 61 6e 6e 6f 74 20  x. If it cannot 
19e28 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 63 6f 6e  be obtained, con
19e29 74 69 6e 75 65 0a 20 20 20 20 2a 2a 20 74 6f 20  tinue.    ** to 
19e2a 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65  the next stateme
19e2b 6e 74 20 68 61 6e 64 6c 65 2e 20 49 74 20 69 73  nt handle. It is
19e2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
19e2d 20 62 6c 6f 63 6b 20 6f 6e 0a 20 20 20 20 2a 2a   block on.    **
19e2e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75   the database mu
19e2f 74 65 78 20 2d 20 74 68 61 74 20 63 6f 75 6c 64  tex - that could
19e30 20 63 61 75 73 65 20 64 65 61 64 6c 6f 63 6b 2e   cause deadlock.
19e31 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
19e32 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74  SQLITE_OK==sqlit
19e33 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e  e3_mutex_try(p->
19e34 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20  db->mutex) ){.  
19e35 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c      nFree += sql
19e36 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 42  ite3VdbeReleaseB
19e37 75 66 66 65 72 73 28 70 29 3b 0a 20 20 20 20 20  uffers(p);.     
19e38 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e 6f   stmtLruRemoveNo
19e39 6d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 20 20  mutex(p);.      
19e3a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
19e3b 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
19e3c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
19e3d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
19e3e 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ve(sqlite3MutexA
19e3f 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
19e40 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b  X_STATIC_LRU2));
19e41 0a 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65  ..  return nFree
19e42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
19e43 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65  sqlite3Reprepare
19e44 28 29 20 6f 6e 20 74 68 65 20 73 74 61 74 65 6d  () on the statem
19e45 65 6e 74 2e 20 52 65 6d 6f 76 65 20 69 74 20 66  ent. Remove it f
19e46 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c 72 75 20 6c  rom the.** lru l
19e47 69 73 74 20 62 65 66 6f 72 65 20 64 6f 69 6e 67  ist before doing
19e48 20 73 6f 2c 20 61 73 20 52 65 70 72 65 70 61 72   so, as Reprepar
19e49 65 28 29 20 77 69 6c 6c 20 66 72 65 65 20 61 6c  e() will free al
19e4a 6c 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  l the.** memory 
19e4b 72 65 67 69 73 74 65 72 20 62 75 66 66 65 72 73  register buffers
19e4c 20 61 6e 79 77 61 79 2e 0a 2a 2f 0a 69 6e 74 20   anyway..*/.int 
19e4d 76 64 62 65 52 65 70 72 65 70 61 72 65 28 56 64  vdbeReprepare(Vd
19e4e 62 65 20 2a 70 29 7b 0a 20 20 73 74 6d 74 4c 72  be *p){.  stmtLr
19e4f 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 72 65  uRemove(p);.  re
19e50 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 70 72  turn sqlite3Repr
19e51 65 70 61 72 65 28 70 29 3b 0a 7d 0a 0a 23 65 6c  epare(p);.}..#el
19e52 73 65 20 20 20 20 20 20 20 2f 2a 20 21 53 51 4c  se       /* !SQL
19e53 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
19e54 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a  Y_MANAGEMENT */.
19e55 20 20 23 64 65 66 69 6e 65 20 73 74 6d 74 4c 72    #define stmtLr
19e56 75 52 65 6d 6f 76 65 28 78 29 0a 20 20 23 64 65  uRemove(x).  #de
19e57 66 69 6e 65 20 73 74 6d 74 4c 72 75 41 64 64 28  fine stmtLruAdd(
19e58 78 29 0a 20 20 23 64 65 66 69 6e 65 20 76 64 62  x).  #define vdb
19e59 65 52 65 70 72 65 70 61 72 65 28 78 29 20 73 71  eReprepare(x) sq
19e5a 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 78  lite3Reprepare(x
19e5b 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64  ).#endif...#ifnd
19e5c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
19e5d 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20  EPRECATED./*.** 
19e5e 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e  Return TRUE (non
19e5f 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20 73 74  -zero) of the st
19e60 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69 65 64  atement supplied
19e61 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
19e62 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 72  needs.** to be r
19e63 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41 20 73 74  ecompiled.  A st
19e64 61 74 65 6d 65 6e 74 20 6e 65 65 64 73 20 74 6f  atement needs to
19e65 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 77   be recompiled w
19e66 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 65  henever the.** e
19e67 78 65 63 75 74 69 6f 6e 20 65 6e 76 69 72 6f 6e  xecution environ
19e68 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 69 6e 20  ment changes in 
19e69 61 20 77 61 79 20 74 68 61 74 20 77 6f 75 6c 64  a way that would
19e6a 20 61 6c 74 65 72 20 74 68 65 20 70 72 6f 67 72   alter the progr
19e6b 61 6d 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74  am.** that sqlit
19e6c 65 33 5f 70 72 65 70 61 72 65 28 29 20 67 65 6e  e3_prepare() gen
19e6d 65 72 61 74 65 73 2e 20 20 46 6f 72 20 65 78 61  erates.  For exa
19e6e 6d 70 6c 65 2c 20 69 66 20 6e 65 77 20 66 75 6e  mple, if new fun
19e6f 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c  ctions or.** col
19e70 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
19e71 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 20   are registered 
19e72 6f 72 20 69 66 20 61 6e 20 61 75 74 68 6f 72 69  or if an authori
19e73 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a  zer function is.
19e74 2a 2a 20 61 64 64 65 64 20 6f 72 20 63 68 61 6e  ** added or chan
19e75 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ged..*/.SQLITE_A
19e76 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
19e77 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f 73  xpired(sqlite3_s
19e78 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56  tmt *pStmt){.  V
19e79 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29  dbe *p = (Vdbe*)
19e7a 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20  pStmt;.  return 
19e7b 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 78 70 69 72  p==0 || p->expir
19e7c 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ed;.}.#endif../*
19e7d 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
19e7e 67 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f  g routine destro
19e7f 79 73 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63  ys a virtual mac
19e80 68 69 6e 65 20 74 68 61 74 20 69 73 20 63 72 65  hine that is cre
19e81 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73  ated by.** the s
19e82 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29  qlite3_compile()
19e83 20 72 6f 75 74 69 6e 65 2e 20 54 68 65 20 69 6e   routine. The in
19e84 74 65 67 65 72 20 72 65 74 75 72 6e 65 64 20 69  teger returned i
19e85 73 20 61 6e 20 53 51 4c 49 54 45 5f 0a 2a 2a 20  s an SQLITE_.** 
19e86 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65 20  success/failure 
19e87 63 6f 64 65 20 74 68 61 74 20 64 65 73 63 72 69  code that descri
19e88 62 65 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f  bes the result o
19e89 66 20 65 78 65 63 75 74 69 6e 67 20 74 68 65 20  f executing the 
19e8a 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69  virtual.** machi
19e8b 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ne..**.** This r
19e8c 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
19e8d 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 73  error code and s
19e8e 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 62  tring returned b
19e8f 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72  y.** sqlite3_err
19e90 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 33 5f  code(), sqlite3_
19e91 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c  errmsg() and sql
19e92 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 2e  ite3_errmsg16().
19e93 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
19e94 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  nt sqlite3_final
19e95 69 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ize(sqlite3_stmt
19e96 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20   *pStmt){.  int 
19e97 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d  rc;.  if( pStmt=
19e98 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
19e99 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
19e9a 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  e{.    Vdbe *v =
19e9b 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
19e9c 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
19e9d 20 76 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49   v->db;.#if SQLI
19e9e 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20  TE_THREADSAFE.  
19e9f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
19ea0 2a 6d 75 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e  *mutex = v->db->
19ea1 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20  mutex;.#endif.  
19ea2 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
19ea3 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
19ea4 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 28    stmtLruRemove(
19ea5 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  v);.    rc = sql
19ea6 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
19ea7 28 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  (v);.    rc = sq
19ea8 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
19ea9 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   rc);.    sqlite
19eaa 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
19eab 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
19eac 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
19ead 54 65 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75  Terminate the cu
19eae 72 72 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 20  rrent execution 
19eaf 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  of an SQL statem
19eb0 65 6e 74 20 61 6e 64 20 72 65 73 65 74 20 69 74  ent and reset it
19eb1 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20  .** back to its 
19eb2 73 74 61 72 74 69 6e 67 20 73 74 61 74 65 20 73  starting state s
19eb3 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65  o that it can be
19eb4 20 72 65 75 73 65 64 2e 20 41 20 73 75 63 63 65   reused. A succe
19eb5 73 73 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20  ss code from.** 
19eb6 74 68 65 20 70 72 69 6f 72 20 65 78 65 63 75 74  the prior execut
19eb7 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ion is returned.
19eb8 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
19eb9 69 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72  ine sets the err
19eba 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69  or code and stri
19ebb 6e 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  ng returned by.*
19ebc 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  * sqlite3_errcod
19ebd 65 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72  e(), sqlite3_err
19ebe 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  msg() and sqlite
19ebf 33 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f  3_errmsg16()..*/
19ec0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
19ec1 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71  sqlite3_reset(sq
19ec2 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
19ec3 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
19ec4 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a  if( pStmt==0 ){.
19ec5 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
19ec6 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
19ec7 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65   Vdbe *v = (Vdbe
19ec8 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c  *)pStmt;.    sql
19ec9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
19eca 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (v->db->mutex);.
19ecb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
19ecc 56 64 62 65 52 65 73 65 74 28 76 29 3b 0a 20 20  VdbeReset(v);.  
19ecd 20 20 73 74 6d 74 4c 72 75 41 64 64 28 76 29 3b    stmtLruAdd(v);
19ece 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19ecf 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 2d 31 2c  MakeReady(v, -1,
19ed0 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 61   0, 0, 0);.    a
19ed1 73 73 65 72 74 28 20 28 72 63 20 26 20 28 76 2d  ssert( (rc & (v-
19ed2 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d  >db->errMask))==
19ed3 72 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  rc );.    rc = s
19ed4 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 76 2d  qlite3ApiExit(v-
19ed5 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71  >db, rc);.    sq
19ed6 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
19ed7 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(v->db->mutex);
19ed8 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
19ed9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61  ;.}../*.** Set a
19eda 6c 6c 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  ll the parameter
19edb 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  s in the compile
19edc 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  d SQL statement 
19edd 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49  to NULL..*/.SQLI
19ede 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
19edf 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67  e3_clear_binding
19ee0 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  s(sqlite3_stmt *
19ee1 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pStmt){.  int i;
19ee2 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
19ee3 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 65 20 2a 70  TE_OK;.  Vdbe *p
19ee4 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
19ee5 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
19ee6 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33  ADSAFE.  sqlite3
19ee7 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
19ee8 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e  ((Vdbe*)pStmt)->
19ee9 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 69  db->mutex;.#endi
19eea 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  f.  sqlite3_mute
19eeb 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a  x_enter(mutex);.
19eec 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
19eed 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nVar; i++){.    
19eee 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
19eef 6c 65 61 73 65 28 26 70 2d 3e 61 56 61 72 5b 69  lease(&p->aVar[i
19ef0 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56 61 72 5b  ]);.    p->aVar[
19ef1 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  i].flags = MEM_N
19ef2 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ull;.  }.  sqlit
19ef3 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
19ef4 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
19ef5 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  rc;.}.../*******
19ef6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ef7 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  ***** sqlite3_va
19ef8 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  lue_  **********
19ef9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19efa 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  *****.** The fol
19efb 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20  lowing routines 
19efc 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74  extract informat
19efd 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 6d 20 6f  ion from a Mem o
19efe 72 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 0a  r sqlite3_value.
19eff 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ** structure..*/
19f00 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
19f01 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
19f02 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 74  value_blob(sqlit
19f03 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
19f04 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d  .  Mem *p = (Mem
19f05 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 70 2d  *)pVal;.  if( p-
19f06 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c  >flags & (MEM_Bl
19f07 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20  ob|MEM_Str) ){. 
19f08 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19f09 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 29 3b 0a  mExpandBlob(p);.
19f0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20      p->flags &= 
19f0b 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 70 2d  ~MEM_Str;.    p-
19f0c 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c  >flags |= MEM_Bl
19f0d 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70  ob;.    return p
19f0e 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ->z;.  }else{.  
19f0f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
19f10 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56 61 6c  _value_text(pVal
19f11 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f  );.  }.}.SQLITE_
19f12 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
19f13 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71 6c 69  value_bytes(sqli
19f14 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
19f15 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
19f16 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 61  e3ValueBytes(pVa
19f17 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
19f18 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
19f19 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
19f1a 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f  bytes16(sqlite3_
19f1b 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
19f1c 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61  return sqlite3Va
19f1d 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53  lueBytes(pVal, S
19f1e 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
19f1f 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  E);.}.SQLITE_API
19f20 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f   double sqlite3_
19f21 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 6c  value_double(sql
19f22 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
19f23 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
19f24 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65  te3VdbeRealValue
19f25 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a  ((Mem*)pVal);.}.
19f26 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
19f27 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
19f28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
19f29 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
19f2a 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65  (int)sqlite3Vdbe
19f2b 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70  IntValue((Mem*)p
19f2c 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  Val);.}.SQLITE_A
19f2d 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  PI sqlite_int64 
19f2e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
19f2f 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  t64(sqlite3_valu
19f30 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
19f31 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  rn sqlite3VdbeIn
19f32 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61  tValue((Mem*)pVa
19f33 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
19f34 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
19f35 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61  char *sqlite3_va
19f36 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33  lue_text(sqlite3
19f37 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
19f38 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 75   return (const u
19f39 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73  nsigned char *)s
19f3a 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
19f3b 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
19f3c 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  8);.}.#ifndef SQ
19f3d 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
19f3e 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
19f3f 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
19f40 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c 69  alue_text16(sqli
19f41 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29  te3_value* pVal)
19f42 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
19f43 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c  e3ValueText(pVal
19f44 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
19f45 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  TIVE);.}.SQLITE_
19f46 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
19f47 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
19f48 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76  xt16be(sqlite3_v
19f49 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
19f4a 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c  eturn sqlite3Val
19f4b 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
19f4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b 0a 7d 0a  ITE_UTF16BE);.}.
19f4d 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
19f4e 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
19f4f 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71  alue_text16le(sq
19f50 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
19f51 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
19f52 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56  ite3ValueText(pV
19f53 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  al, SQLITE_UTF16
19f54 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  LE);.}.#endif /*
19f55 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
19f56 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  16 */.SQLITE_API
19f57 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
19f58 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ue_type(sqlite3_
19f59 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20  value* pVal){.  
19f5a 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 74 79 70  return pVal->typ
19f5b 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  e;.}../*********
19f5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f5d 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75  *** sqlite3_resu
19f5e 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  lt_  ***********
19f5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f60 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ****.** The foll
19f61 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61  owing routines a
19f62 72 65 20 75 73 65 64 20 62 79 20 75 73 65 72 2d  re used by user-
19f63 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
19f64 73 20 74 6f 20 73 70 65 63 69 66 79 0a 2a 2a 20  s to specify.** 
19f65 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 73  the function res
19f66 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ult..*/.SQLITE_A
19f67 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
19f68 72 65 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73  result_blob(.  s
19f69 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
19f6a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76  pCtx, .  const v
19f6b 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  oid *z, .  int n
19f6c 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
19f6d 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61  )(void *).){.  a
19f6e 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20  ssert( n>=0 );. 
19f6f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
19f70 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
19f71 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
19f72 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
19f73 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
19f74 73 2c 20 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c  s, z, n, 0, xDel
19f75 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
19f76 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
19f77 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74  ult_double(sqlit
19f78 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
19f79 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a  , double rVal){.
19f7a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
19f7b 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
19f7c 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
19f7d 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
19f7e 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 43  MemSetDouble(&pC
19f7f 74 78 2d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a  tx->s, rVal);.}.
19f80 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
19f81 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
19f82 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e  rror(sqlite3_con
19f83 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73  text *pCtx, cons
19f84 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  t char *z, int n
19f85 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
19f86 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19f87 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
19f88 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73  x) );.  pCtx->is
19f89 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45  Error = SQLITE_E
19f8a 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56  RROR;.  sqlite3V
19f8b 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43  dbeMemSetStr(&pC
19f8c 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c  tx->s, z, n, SQL
19f8d 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
19f8e 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23  _TRANSIENT);.}.#
19f8f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
19f90 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
19f91 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
19f92 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
19f93 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
19f94 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69  *pCtx, const voi
19f95 64 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  d *z, int n){.  
19f96 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
19f97 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
19f98 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
19f99 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72  .  pCtx->isError
19f9a 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
19f9b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
19f9c 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73  mSetStr(&pCtx->s
19f9d 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
19f9e 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49  TF16NATIVE, SQLI
19f9f 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d  TE_TRANSIENT);.}
19fa0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41  .#endif.SQLITE_A
19fa1 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
19fa2 72 65 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74  result_int(sqlit
19fa3 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
19fa4 2c 20 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61  , int iVal){.  a
19fa5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
19fa6 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
19fa7 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
19fa8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19fa9 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e  SetInt64(&pCtx->
19faa 73 2c 20 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d  s, (i64)iVal);.}
19fab 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
19fac 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
19fad 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f  int64(sqlite3_co
19fae 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 36 34  ntext *pCtx, i64
19faf 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74   iVal){.  assert
19fb0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
19fb1 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
19fb2 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
19fb3 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e  ite3VdbeMemSetIn
19fb4 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 69 56  t64(&pCtx->s, iV
19fb5 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  al);.}.SQLITE_AP
19fb6 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
19fb7 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74  esult_null(sqlit
19fb8 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
19fb9 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
19fba 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19fbb 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
19fbc 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
19fbd 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70  dbeMemSetNull(&p
19fbe 43 74 78 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54  Ctx->s);.}.SQLIT
19fbf 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
19fc0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 0a  e3_result_text(.
19fc1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
19fc2 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73  t *pCtx, .  cons
19fc3 74 20 63 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e  t char *z, .  in
19fc4 74 20 6e 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44  t n,.  void (*xD
19fc5 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20  el)(void *).){. 
19fc6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
19fc7 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
19fc8 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
19fc9 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
19fca 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
19fcb 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  s, z, n, SQLITE_
19fcc 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23  UTF8, xDel);.}.#
19fcd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
19fce 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
19fcf 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
19fd0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0a  _result_text16(.
19fd1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
19fd2 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73  t *pCtx, .  cons
19fd3 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e  t void *z, .  in
19fd4 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  t n, .  void (*x
19fd5 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a  Del)(void *).){.
19fd6 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
19fd7 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
19fd8 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
19fd9 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
19fda 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d  MemSetStr(&pCtx-
19fdb 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  >s, z, n, SQLITE
19fdc 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44  _UTF16NATIVE, xD
19fdd 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  el);.}.SQLITE_AP
19fde 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
19fdf 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a  esult_text16be(.
19fe0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
19fe1 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73  t *pCtx, .  cons
19fe2 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e  t void *z, .  in
19fe3 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  t n, .  void (*x
19fe4 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a  Del)(void *).){.
19fe5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
19fe6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
19fe7 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
19fe8 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
19fe9 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d  MemSetStr(&pCtx-
19fea 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  >s, z, n, SQLITE
19feb 5f 55 54 46 31 36 42 45 2c 20 78 44 65 6c 29 3b  _UTF16BE, xDel);
19fec 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
19fed 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
19fee 74 5f 74 65 78 74 31 36 6c 65 28 0a 20 20 73 71  t_text16le(.  sq
19fef 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
19ff0 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f  Ctx, .  const vo
19ff1 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  id *z, .  int n,
19ff2 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29   .  void (*xDel)
19ff3 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73  (void *).){.  as
19ff4 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
19ff5 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
19ff6 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
19ff7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19ff8 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
19ff9 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
19ffa 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23  16LE, xDel);.}.#
19ffb 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
19ffc 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51  OMIT_UTF16 */.SQ
19ffd 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
19ffe 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
19fff 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ue(sqlite3_conte
1a000 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 74 65  xt *pCtx, sqlite
1a001 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29  3_value *pValue)
1a002 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1a003 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1a004 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1a005 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
1a006 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 78 2d  beMemCopy(&pCtx-
1a007 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a 53  >s, pValue);.}.S
1a008 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1a009 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65  qlite3_result_ze
1a00a 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63  roblob(sqlite3_c
1a00b 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e  ontext *pCtx, in
1a00c 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20  t n){.  assert( 
1a00d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1a00e 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1a00f 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1a010 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f  e3VdbeMemSetZero
1a011 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20 6e  Blob(&pCtx->s, n
1a012 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1a013 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1a014 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73  ult_error_code(s
1a015 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a016 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 6f 64  pCtx, int errCod
1a017 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72  e){.  pCtx->isEr
1a018 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a 7d  ror = errCode;.}
1a019 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51  ../* Force an SQ
1a01a 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f  LITE_TOOBIG erro
1a01b 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r. */.SQLITE_API
1a01c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1a01d 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
1a01e 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  g(sqlite3_contex
1a01f 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65  t *pCtx){.  asse
1a020 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1a021 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1a022 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
1a023 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53  Ctx->isError = S
1a024 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20  QLITE_TOOBIG;.  
1a025 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1a026 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22  tStr(&pCtx->s, "
1a027 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74  string or blob t
1a028 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20  oo big", -1, .  
1a029 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a02a 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38       SQLITE_UTF8
1a02b 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
1a02c 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54  ;.}../* An SQLIT
1a02d 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a  E_NOMEM error. *
1a02e 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
1a02f 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1a030 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c  _error_nomem(sql
1a031 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1a032 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  tx){.  assert( s
1a033 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1a034 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1a035 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
1a036 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1a037 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74  &pCtx->s);.  pCt
1a038 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c  x->isError = SQL
1a039 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74  ITE_NOMEM;.  pCt
1a03a 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  x->s.db->mallocF
1a03b 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  ailed = 1;.}../*
1a03c 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 20  .** Execute the 
1a03d 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c  statement pStmt,
1a03e 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61 20   either until a 
1a03f 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 72  row of data is r
1a040 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61  eady, the.** sta
1a041 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65  tement is comple
1a042 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72  tely executed or
1a043 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1a044 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1a045 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  tine implements 
1a046 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20  the bulk of the 
1a047 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68 65  logic behind the
1a048 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a   sqlite_step().*
1a049 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79  * API.  The only
1a04a 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 69   thing omitted i
1a04b 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20  s the automatic 
1a04c 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a  recompile if a .
1a04d 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ** schema change
1a04e 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20   has occurred.  
1a04f 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20 68  That detail is h
1a050 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a  andled by the.**
1a051 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f 73   outer sqlite3_s
1a052 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70 72  tep() wrapper pr
1a053 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  ocedure..*/.stat
1a054 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  ic int sqlite3St
1a055 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73  ep(Vdbe *p){.  s
1a056 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e  qlite3 *db;.  in
1a057 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
1a058 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67  p);.  if( p->mag
1a059 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic!=VDBE_MAGIC_R
1a05a 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  UN ){.    return
1a05b 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
1a05c 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74    }..  /* Assert
1a05d 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68   that malloc() h
1a05e 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f  as not failed */
1a05f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  .  db = p->db;. 
1a060 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1a061 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74  ailed ){.    ret
1a062 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1a063 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e  ;.  }..  if( p->
1a064 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 69  pc<=0 && p->expi
1a065 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 20 70  red ){.    if( p
1a066 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1a067 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d  ){.      p->rc =
1a068 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a   SQLITE_SCHEMA;.
1a069 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53      }.    rc = S
1a06a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1a06b 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65   goto end_of_ste
1a06c 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  p;.  }.  if( sql
1a06d 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1a06e 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1a06f 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1a070 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1a071 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69  _MISUSE;.  }.  i
1a072 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20  f( p->pc<0 ){.  
1a073 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
1a074 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65  e no other state
1a075 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20  ments currently 
1a076 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20  running, then.  
1a077 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69    ** reset the i
1a078 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20  nterrupt flag.  
1a079 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20  This prevents a 
1a07a 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
1a07b 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a 2a  interrupt.    **
1a07c 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69   from interrupti
1a07d 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  ng a statement t
1a07e 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20  hat has not yet 
1a07f 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  started..    */.
1a080 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69      if( db->acti
1a081 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a  veVdbeCnt==0 ){.
1a082 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49        db->u1.isI
1a083 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a  nterrupted = 0;.
1a084 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
1a085 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
1a086 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72  .    if( db->xPr
1a087 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e  ofile && !db->in
1a088 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20  it.busy ){.     
1a089 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20   double rNow;.  
1a08a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72      sqlite3OsCur
1a08b 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66  rentTime(db->pVf
1a08c 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20  s, &rNow);.     
1a08d 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d 20   p->startTime = 
1a08e 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69  (u64)((rNow - (i
1a08f 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a  nt)rNow)*3600.0*
1a090 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e  24.0*1000000000.
1a091 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  0);.    }.#endif
1a092 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65  ..    db->active
1a093 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 69  VdbeCnt++;.    i
1a094 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d  f( p->readOnly==
1a095 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 62  0 ) db->writeVdb
1a096 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70  eCnt++;.    p->p
1a097 63 20 3d 20 30 3b 0a 20 20 20 20 73 74 6d 74 4c  c = 0;.    stmtL
1a098 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 7d  ruRemove(p);.  }
1a099 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a09a 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69  OMIT_EXPLAIN.  i
1a09b 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b  f( p->explain ){
1a09c 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1a09d 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20  3VdbeList(p);.  
1a09e 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
1a09f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
1a0a0 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72  AIN */.  {.    r
1a0a1 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45  c = sqlite3VdbeE
1a0a2 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69  xec(p);.  }..  i
1a0a3 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1a0a4 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72  Off(db) ){.    r
1a0a5 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
1a0a6 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  E;.  }..#ifndef 
1a0a7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1a0a8 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  E.  /* Invoke th
1a0a9 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61  e profile callba
1a0aa 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ck if there is o
1a0ab 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  ne.  */.  if( rc
1a0ac 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20  !=SQLITE_ROW && 
1a0ad 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20  db->xProfile && 
1a0ae 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26  !db->init.busy &
1a0af 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20  & p->zSql ){.   
1a0b0 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20   double rNow;.  
1a0b1 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65    u64 elapseTime
1a0b2 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ;..    sqlite3Os
1a0b3 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e  CurrentTime(db->
1a0b4 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20  pVfs, &rNow);.  
1a0b5 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28    elapseTime = (
1a0b6 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e  u64)((rNow - (in
1a0b7 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32  t)rNow)*3600.0*2
1a0b8 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30  4.0*1000000000.0
1a0b9 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d  );.    elapseTim
1a0ba 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d  e -= p->startTim
1a0bb 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66  e;.    db->xProf
1a0bc 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65  ile(db->pProfile
1a0bd 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c  Arg, p->zSql, el
1a0be 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23  apseTime);.  }.#
1a0bf 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72  endif..  db->err
1a0c0 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28  Code = rc;.  if(
1a0c1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73   SQLITE_NOMEM==s
1a0c2 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d  qlite3ApiExit(p-
1a0c3 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20  >db, p->rc) ){. 
1a0c4 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
1a0c5 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64  E_NOMEM;.  }.end
1a0c6 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41  _of_step:.  /* A
1a0c7 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63  t this point loc
1a0c8 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68  al variable rc h
1a0c9 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74  olds the value t
1a0ca 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20  hat should be . 
1a0cb 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20   ** returned if 
1a0cc 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77  this statement w
1a0cd 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e  as compiled usin
1a0ce 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20  g the legacy .  
1a0cf 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ** sqlite3_prepa
1a0d0 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20  re() interface. 
1a0d1 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  According to the
1a0d2 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20   docs, this can 
1a0d3 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65  only.  ** be one
1a0d4 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69   of the values i
1a0d5 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 65  n the first asse
1a0d6 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69  rt() below. Vari
1a0d7 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a  able p->rc .  **
1a0d8 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61   contains the va
1a0d9 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62  lue that would b
1a0da 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 71  e returned if sq
1a0db 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
1a0dc 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c   .  ** were call
1a0dd 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20  ed on statement 
1a0de 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  p..  */.  assert
1a0df 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
1a0e0 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f    || rc==SQLITE_
1a0e1 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51  DONE   || rc==SQ
1a0e2 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20  LITE_ERROR .    
1a0e3 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45     || rc==SQLITE
1a0e4 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c  _BUSY || rc==SQL
1a0e5 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a  ITE_MISUSE.  );.
1a0e6 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21    assert( p->rc!
1a0e7 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70  =SQLITE_ROW && p
1a0e8 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ->rc!=SQLITE_DON
1a0e9 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73  E );.  if( p->is
1a0ea 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 21  PrepareV2 && rc!
1a0eb 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72  =SQLITE_ROW && r
1a0ec 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
1a0ed 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
1a0ee 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70   statement was p
1a0ef 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 71  repared using sq
1a0f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a0f1 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a  (), and an.    *
1a0f2 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  * error has occu
1a0f3 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  red, then return
1a0f4 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
1a0f5 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a  in p->rc to the.
1a0f6 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53      ** caller. S
1a0f7 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  et the error cod
1a0f8 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
1a0f9 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20  e handle to the 
1a0fa 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20  same value..    
1a0fb 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d  */ .    rc = db-
1a0fc 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63  >errCode = p->rc
1a0fd 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
1a0fe 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b  rc&db->errMask);
1a0ff 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
1a100 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20  s the top-level 
1a101 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1a102 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  f sqlite3_step()
1a103 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74  .  Call.** sqlit
1a104 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d  e3Step() to do m
1a105 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e  ost of the work.
1a106 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72    If a schema er
1a107 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63  ror occurs,.** c
1a108 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65  all sqlite3Repre
1a109 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61  pare() and try a
1a10a 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  gain..*/.#ifdef 
1a10b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53  SQLITE_OMIT_PARS
1a10c 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ER.SQLITE_API in
1a10d 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  t sqlite3_step(s
1a10e 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1a10f 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  mt){.  int rc = 
1a110 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1a111 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20   if( pStmt ){.  
1a112 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 76    Vdbe *v;.    v
1a113 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
1a114 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1a115 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e  ex_enter(v->db->
1a116 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d  mutex);.    rc =
1a117 20 73 71 6c 69 74 65 33 53 74 65 70 28 76 29 3b   sqlite3Step(v);
1a118 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1a119 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e  ex_leave(v->db->
1a11a 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65  mutex);.  }.  re
1a11b 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65  turn rc;.}.#else
1a11c 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1a11d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c  sqlite3_step(sql
1a11e 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1a11f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1a120 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69  LITE_MISUSE;.  i
1a121 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20  f( pStmt ){.    
1a122 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20  int cnt = 0;.   
1a123 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65   Vdbe *v = (Vdbe
1a124 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c  *)pStmt;.    sql
1a125 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62  ite3 *db = v->db
1a126 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1a127 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
1a128 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  tex);.    while(
1a129 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74   (rc = sqlite3St
1a12a 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53  ep(v))==SQLITE_S
1a12b 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20  CHEMA.          
1a12c 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20   && cnt++ < 5.  
1a12d 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63 20           && (rc 
1a12e 3d 20 76 64 62 65 52 65 70 72 65 70 61 72 65 28  = vdbeReprepare(
1a12f 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  v))==SQLITE_OK )
1a130 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1a131 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20  reset(pStmt);.  
1a132 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20 3d      v->expired =
1a133 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
1a134 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48  ( rc==SQLITE_SCH
1a135 45 4d 41 20 26 26 20 76 2d 3e 69 73 50 72 65 70  EMA && v->isPrep
1a136 61 72 65 56 32 20 26 26 20 64 62 2d 3e 70 45 72  areV2 && db->pEr
1a137 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  r ){.      /* Th
1a138 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 61  is case occurs a
1a139 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f 20  fter failing to 
1a13a 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71 6c  recompile an sql
1a13b 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20 20   statement. .   
1a13c 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 20     ** The error 
1a13d 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65  message from the
1a13e 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 61   SQL compiler ha
1a13f 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c  s already been l
1a140 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a 20  oaded .      ** 
1a141 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
1a142 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 62  e handle. This b
1a143 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 20  lock copies the 
1a144 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a 20  error message . 
1a145 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65       ** from the
1a146 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1a147 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65 6d   into the statem
1a148 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68 65  ent and sets the
1a149 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20   statement.     
1a14a 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e   ** program coun
1a14b 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73 75  ter to 0 to ensu
1a14c 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 65  re that when the
1a14d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a 20   statement is . 
1a14e 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a 65       ** finalize
1a14f 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 70  d or reset the p
1a150 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73 73  arser error mess
1a151 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65  age is available
1a152 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73 71   via.      ** sq
1a153 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61  lite3_errmsg() a
1a154 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nd sqlite3_errco
1a155 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  de()..      */. 
1a156 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1a157 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20 63  *zErr = (const c
1a158 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
1a159 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72  lue_text(db->pEr
1a15a 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69 74  r); .      sqlit
1a15b 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d 3e  e3DbFree(db, v->
1a15c 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
1a15d 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
1a15e 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
1a15f 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71   v->zErrMsg = sq
1a160 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
1a161 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d  , zErr);.      }
1a162 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20   else {.        
1a163 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  v->zErrMsg = 0;.
1a164 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20          v->rc = 
1a165 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1a166 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1a167 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
1a168 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20  xit(db, rc);.   
1a169 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1a16a 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
1a16b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1a16c 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1a16d 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73  * Extract the us
1a16e 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73  er data from a s
1a16f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73  qlite3_context s
1a170 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74  tructure and ret
1a171 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72  urn a.** pointer
1a172 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54   to it..*/.SQLIT
1a173 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
1a174 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71  te3_user_data(sq
1a175 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1a176 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  ){.  assert( p &
1a177 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20  & p->pFunc );.  
1a178 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d  return p->pFunc-
1a179 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f  >pUserData;.}../
1a17a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
1a17b 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20   user data from 
1a17c 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a sqlite3_contex
1a17d 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  t structure and 
1a17e 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e  return a.** poin
1a17f 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51  ter to it..*/.SQ
1a180 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
1a181 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   *sqlite3_contex
1a182 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69  t_db_handle(sqli
1a183 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b  te3_context *p){
1a184 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20  .  assert( p && 
1a185 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65  p->pFunc );.  re
1a186 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a  turn p->s.db;.}.
1a187 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
1a188 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c  wing is the impl
1a189 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e  ementation of an
1a18a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68   SQL function th
1a18b 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69  at always.** fai
1a18c 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ls with an error
1a18d 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67   message stating
1a18e 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
1a18f 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68  on is used in th
1a190 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65  e.** wrong conte
1a191 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  xt.  The sqlite3
1a192 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
1a193 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63  on() API might c
1a194 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20  onstruct.** SQL 
1a195 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73  function that us
1a196 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  e this routine s
1a197 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74  o that the funct
1a198 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a  ions will exist.
1a199 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f  ** for name reso
1a19a 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61  lution but are a
1a19b 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64  ctually overload
1a19c 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46  ed by the xFindF
1a19d 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f  unction.** metho
1a19e 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62  d of virtual tab
1a19f 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  les..*/.SQLITE_P
1a1a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a1a1 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69  te3InvalidFuncti
1a1a2 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  on(.  sqlite3_co
1a1a3 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
1a1a4 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e   /* The function
1a1a5 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74   calling context
1a1a6 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65   */.  int NotUse
1a1a7 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
1a1a8 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
1a1a9 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
1a1aa 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  unction */.  sql
1a1ab 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
1a1ac 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65  Used2   /* Value
1a1ad 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e   of each argumen
1a1ae 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  t */.){.  const 
1a1af 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f  char *zName = co
1a1b0 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e  ntext->pFunc->zN
1a1b1 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72  ame;.  char *zEr
1a1b2 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  r;.  UNUSED_PARA
1a1b3 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
1a1b4 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72  NotUsed2);.  zEr
1a1b5 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  r = sqlite3MPrin
1a1b6 74 66 28 30 2c 0a 20 20 20 20 20 20 22 75 6e 61  tf(0,.      "una
1a1b7 62 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74  ble to use funct
1a1b8 69 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65  ion %s in the re
1a1b9 71 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22  quested context"
1a1ba 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  , zName);.  sqli
1a1bb 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
1a1bc 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20  (context, zErr, 
1a1bd 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  -1);.  sqlite3_f
1a1be 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a  ree(zErr);.}../*
1a1bf 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20  .** Allocate or 
1a1c0 72 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65  return the aggre
1a1c1 67 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  gate context for
1a1c2 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
1a1c3 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74  .  A new.** cont
1a1c4 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ext is allocated
1a1c5 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61   on the first ca
1a1c6 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20  ll.  Subsequent 
1a1c7 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65  calls return the
1a1c8 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74  .** same context
1a1c9 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e   that was return
1a1ca 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c  ed on prior call
1a1cb 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
1a1cc 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61   void *sqlite3_a
1a1cd 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
1a1ce 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1a1cf 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b   *p, int nByte){
1a1d0 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20  .  Mem *pMem;.  
1a1d1 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e  assert( p && p->
1a1d2 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e  pFunc && p->pFun
1a1d3 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73  c->xStep );.  as
1a1d4 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1a1d5 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62  tex_held(p->s.db
1a1d6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d  ->mutex) );.  pM
1a1d7 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20  em = p->pMem;.  
1a1d8 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  if( (pMem->flags
1a1d9 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29   & MEM_Agg)==0 )
1a1da 7b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d  {.    if( nByte=
1a1db 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1a1dc 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1a1dd 65 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b  eExternal(pMem);
1a1de 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
1a1df 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1a1e0 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30       pMem->z = 0
1a1e1 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1a1e2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1a1e3 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74  mGrow(pMem, nByt
1a1e4 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65  e, 0);.      pMe
1a1e5 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41  m->flags = MEM_A
1a1e6 67 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  gg;.      pMem->
1a1e7 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e  u.pDef = p->pFun
1a1e8 63 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65  c;.      if( pMe
1a1e9 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  m->z ){.        
1a1ea 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20  memset(pMem->z, 
1a1eb 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  0, nByte);.     
1a1ec 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
1a1ed 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65  eturn (void*)pMe
1a1ee 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  m->z;.}../*.** R
1a1ef 65 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61  eturn the auxila
1a1f0 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c  ry data pointer,
1a1f1 20 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65   if any, for the
1a1f2 20 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e   iArg'th argumen
1a1f3 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72  t to.** the user
1a1f4 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65  -function define
1a1f5 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51  d by pCtx..*/.SQ
1a1f6 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
1a1f7 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
1a1f8 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
1a1f9 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41  xt *pCtx, int iA
1a1fa 72 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20  rg){.  VdbeFunc 
1a1fb 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61  *pVdbeFunc;..  a
1a1fc 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1a1fd 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1a1fe 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1a1ff 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43    pVdbeFunc = pC
1a200 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20  tx->pVdbeFunc;. 
1a201 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20   if( !pVdbeFunc 
1a202 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75  || iArg>=pVdbeFu
1a203 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67  nc->nAux || iArg
1a204 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  <0 ){.    return
1a205 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
1a206 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75   pVdbeFunc->apAu
1a207 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a  x[iArg].pAux;.}.
1a208 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61  ./*.** Set the a
1a209 75 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69  uxilary data poi
1a20a 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20  nter and delete 
1a20b 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68  function, for th
1a20c 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67  e iArg'th.** arg
1a20d 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65  ument to the use
1a20e 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  r-function defin
1a20f 65 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20  ed by pCtx. Any 
1a210 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69  previous value i
1a211 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20  s.** deleted by 
1a212 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65  calling the dele
1a213 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63  te function spec
1a214 69 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61  ified when it wa
1a215 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s set..*/.SQLITE
1a216 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1a217 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20  3_set_auxdata(. 
1a218 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1a219 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69   *pCtx, .  int i
1a21a 41 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41  Arg, .  void *pA
1a21b 75 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44  ux, .  void (*xD
1a21c 65 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b  elete)(void*).){
1a21d 0a 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74  .  struct AuxDat
1a21e 61 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56  a *pAuxData;.  V
1a21f 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75  dbeFunc *pVdbeFu
1a220 6e 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30  nc;.  if( iArg<0
1a221 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a   ) goto failed;.
1a222 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1a223 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1a224 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1a225 20 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20   );.  pVdbeFunc 
1a226 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e  = pCtx->pVdbeFun
1a227 63 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46  c;.  if( !pVdbeF
1a228 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63  unc || pVdbeFunc
1a229 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a  ->nAux<=iArg ){.
1a22a 20 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28      int nAux = (
1a22b 70 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62  pVdbeFunc ? pVdb
1a22c 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29  eFunc->nAux : 0)
1a22d 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f  ;.    int nMallo
1a22e 63 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46  c = sizeof(VdbeF
1a22f 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74  unc) + sizeof(st
1a230 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41  ruct AuxData)*iA
1a231 72 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e  rg;.    pVdbeFun
1a232 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  c = sqlite3DbRea
1a233 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c  lloc(pCtx->s.db,
1a234 20 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c   pVdbeFunc, nMal
1a235 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70  loc);.    if( !p
1a236 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20  VdbeFunc ){.    
1a237 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20    goto failed;. 
1a238 20 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70     }.    pCtx->p
1a239 56 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65  VdbeFunc = pVdbe
1a23a 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74  Func;.    memset
1a23b 28 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41  (&pVdbeFunc->apA
1a23c 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a  ux[nAux], 0, siz
1a23d 65 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61  eof(struct AuxDa
1a23e 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78  ta)*(iArg+1-nAux
1a23f 29 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e  ));.    pVdbeFun
1a240 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31  c->nAux = iArg+1
1a241 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d  ;.    pVdbeFunc-
1a242 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70  >pFunc = pCtx->p
1a243 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75  Func;.  }..  pAu
1a244 78 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75  xData = &pVdbeFu
1a245 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b  nc->apAux[iArg];
1a246 0a 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d  .  if( pAuxData-
1a247 3e 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74  >pAux && pAuxDat
1a248 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20  a->xDelete ){.  
1a249 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c    pAuxData->xDel
1a24a 65 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41  ete(pAuxData->pA
1a24b 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44  ux);.  }.  pAuxD
1a24c 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78  ata->pAux = pAux
1a24d 3b 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44  ;.  pAuxData->xD
1a24e 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b  elete = xDelete;
1a24f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c  .  return;..fail
1a250 65 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74  ed:.  if( xDelet
1a251 65 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65  e ){.    xDelete
1a252 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23  (pAux);.  }.}..#
1a253 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a254 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
1a255 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1a256 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74  umber of times t
1a257 68 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e  he Step function
1a258 20 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20   of a aggregate 
1a259 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c  has been .** cal
1a25a 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  led..**.** This 
1a25b 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72  function is depr
1a25c 65 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20  ecated.  Do not 
1a25d 75 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63  use it for new c
1a25e 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70  ode.  It is.** p
1a25f 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61  rovide only to a
1a260 76 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65  void breaking le
1a261 67 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20  gacy code.  New 
1a262 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
1a263 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  on.** implementa
1a264 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65  tions should kee
1a265 70 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e  p their own coun
1a266 74 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20  ts within their 
1a267 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e  aggregate.** con
1a268 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  text..*/.SQLITE_
1a269 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1a26a 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28  aggregate_count(
1a26b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1a26c 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
1a26d 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70   && p->pMem && p
1a26e 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46  ->pFunc && p->pF
1a26f 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20  unc->xStep );.  
1a270 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e  return p->pMem->
1a271 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  n;.}.#endif../*.
1a272 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1a273 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1a274 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
1a275 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d  t for the statem
1a276 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51  ent pStmt..*/.SQ
1a277 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1a278 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
1a279 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
1a27a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a  pStmt){.  Vdbe *
1a27b 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53  pVm = (Vdbe *)pS
1a27c 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56  tmt;.  return pV
1a27d 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c  m ? pVm->nResCol
1a27e 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  umn : 0;.}../*.*
1a27f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1a280 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76  ber of values av
1a281 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65  ailable from the
1a282 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
1a283 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  the.** currently
1a284 20 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65   executing state
1a285 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53  ment pStmt..*/.S
1a286 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1a287 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
1a288 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1a289 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  Stmt){.  Vdbe *p
1a28a 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  Vm = (Vdbe *)pSt
1a28b 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30  mt;.  if( pVm==0
1a28c 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74   || pVm->pResult
1a28d 53 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Set==0 ) return 
1a28e 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d  0;.  return pVm-
1a28f 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a  >nResColumn;.}..
1a290 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
1a291 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43  see if column iC
1a292 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20  ol of the given 
1a293 73 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c  statement is val
1a294 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73  id.  If.** it is
1a295 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  , return a point
1a296 65 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f  er to the Mem fo
1a297 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  r the value of t
1a298 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49  hat column..** I
1a299 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61  f iCol is not va
1a29a 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f  lid, return a po
1a29b 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77  inter to a Mem w
1a29c 68 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 65  hich has a value
1a29d 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a  .** of NULL..*/.
1a29e 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75  static Mem *colu
1a29f 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74  mnMem(sqlite3_st
1a2a0 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1a2a1 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a  ){.  Vdbe *pVm;.
1a2a2 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65    int vals;.  Me
1a2a3 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20  m *pOut;..  pVm 
1a2a4 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
1a2a5 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56  .  if( pVm && pV
1a2a6 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30  m->pResultSet!=0
1a2a7 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43   && i<pVm->nResC
1a2a8 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b  olumn && i>=0 ){
1a2a9 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1a2aa 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62  ex_enter(pVm->db
1a2ab 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61  ->mutex);.    va
1a2ac 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74  ls = sqlite3_dat
1a2ad 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a  a_count(pStmt);.
1a2ae 20 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d      pOut = &pVm-
1a2af 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a  >pResultSet[i];.
1a2b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1a2b1 28 28 64 6f 75 62 6c 65 29 30 29 20 49 6e 20 63  ((double)0) In c
1a2b2 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
1a2b3 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
1a2b4 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 73 74 61 74  T... */.    stat
1a2b5 69 63 20 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c  ic const Mem nul
1a2b6 6c 4d 65 6d 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f  lMem = {{0}, (do
1a2b7 75 62 6c 65 29 30 2c 20 30 2c 20 22 22 2c 20 30  uble)0, 0, "", 0
1a2b8 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49  , MEM_Null, SQLI
1a2b9 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30  TE_NULL, 0, 0, 0
1a2ba 20 7d 3b 0a 20 20 20 20 69 66 28 20 70 56 6d 20   };.    if( pVm 
1a2bb 26 26 20 70 56 6d 2d 3e 64 62 20 29 7b 0a 20 20  && pVm->db ){.  
1a2bc 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1a2bd 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d  x_enter(pVm->db-
1a2be 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 73  >mutex);.      s
1a2bf 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d 2d  qlite3Error(pVm-
1a2c0 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47  >db, SQLITE_RANG
1a2c1 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  E, 0);.    }.   
1a2c2 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26 6e   pOut = (Mem*)&n
1a2c3 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65  ullMem;.  }.  re
1a2c4 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a  turn pOut;.}../*
1a2c5 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1a2c6 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  n is called afte
1a2c7 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73 71  r invoking an sq
1a2c8 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58 20  lite3_value_XXX 
1a2c9 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a  function on a .*
1a2ca 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 28  * column value (
1a2cb 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65 74  i.e. a value ret
1a2cc 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61 74  urned by evaluat
1a2cd 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72 65  ing an SQL expre
1a2ce 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a 20  ssion in the.** 
1a2cf 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20 61  select list of a
1a2d0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1a2d1 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75 73  t) that may caus
1a2d2 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  e a malloc() fai
1a2d3 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c  lure. If .** mal
1a2d4 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64  loc() has failed
1a2d5 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d 61  , the threads ma
1a2d6 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
1a2d7 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20 74  is cleared and t
1a2d8 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 64  he result.** cod
1a2d9 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 70  e of statement p
1a2da 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c 49  Stmt set to SQLI
1a2db 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20  TE_NOMEM..**.** 
1a2dc 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 68  Specifically, th
1a2dd 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f  is is called fro
1a2de 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20  m within:.**.** 
1a2df 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
1a2e0 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20 20  mn_int().**     
1a2e1 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
1a2e2 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73 71  nt64().**     sq
1a2e3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
1a2e4 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  t().**     sqlit
1a2e5 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
1a2e6 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
1a2e7 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a  3_column_real().
1a2e8 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
1a2e9 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a  olumn_bytes().**
1a2ea 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
1a2eb 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a 2a  umn_bytes16().**
1a2ec 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f 72 20  .** But not for 
1a2ed 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
1a2ee 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e 65 76  lob(), which nev
1a2ef 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28  er calls malloc(
1a2f0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
1a2f1 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61  d columnMallocFa
1a2f2 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f 73 74  ilure(sqlite3_st
1a2f3 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 20 2f  mt *pStmt).{.  /
1a2f4 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61  * If malloc() fa
1a2f5 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e 20 65  iled during an e
1a2f6 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69  ncoding conversi
1a2f7 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 20 2a  on within an.  *
1a2f8 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
1a2f9 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20 73  _XXX API, then s
1a2fa 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f  et the return co
1a2fb 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  de of the statem
1a2fc 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 4c 49  ent to.  ** SQLI
1a2fd 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e 65  TE_NOMEM. The ne
1a2fe 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65 70  xt call to _step
1a2ff 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c 6c  () (if any) will
1a300 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1a301 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 5f 66  RROR.  ** and _f
1a302 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 72  inalize() will r
1a303 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a  eturn NOMEM..  *
1a304 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  /.  Vdbe *p = (V
1a305 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69  dbe *)pStmt;.  i
1a306 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72  f( p ){.    p->r
1a307 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
1a308 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29  it(p->db, p->rc)
1a309 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1a30a 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
1a30b 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a  >mutex);.  }.}..
1a30c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
1a30d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71  ************* sq
1a30e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a  lite3_column_  *
1a30f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1a311 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1a312 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
1a313 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c 65 6d  d to access elem
1a314 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72 72  ents of the curr
1a315 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68  ent row.** in th
1a316 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f  e result set..*/
1a317 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1a318 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
1a319 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69  column_blob(sqli
1a31a 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1a31b 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74   int i){.  const
1a31c 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76 61   void *val;.  va
1a31d 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
1a31e 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65  e_blob( columnMe
1a31f 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20  m(pStmt,i) );.  
1a320 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74  /* Even though t
1a321 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f 64  here is no encod
1a322 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 20  ing conversion, 
1a323 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69 67  value_blob() mig
1a324 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20  ht.  ** need to 
1a325 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 6f  call malloc() to
1a326 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 73 75   expand the resu
1a327 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f 62  lt of a zeroblob
1a328 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73  () .  ** express
1a329 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c  ion. .  */.  col
1a32a 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1a32b 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1a32c 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1a32d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1a32e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c  column_bytes(sql
1a32f 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1a330 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
1a331 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
1a332 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c 75 6d  lue_bytes( colum
1a333 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1a334 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1a335 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1a336 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1a337 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1a338 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
1a339 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  es16(sqlite3_stm
1a33a 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1a33b 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71  {.  int val = sq
1a33c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1a33d 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  s16( columnMem(p
1a33e 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c  Stmt,i) );.  col
1a33f 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1a340 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1a341 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1a342 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
1a343 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65  e3_column_double
1a344 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1a345 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
1a346 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 71 6c  double val = sql
1a347 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
1a348 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  e( columnMem(pSt
1a349 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1a34a 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1a34b 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1a34c 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
1a34d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
1a34e 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33  lumn_int(sqlite3
1a34f 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1a350 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20  t i){.  int val 
1a351 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1a352 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  int( columnMem(p
1a353 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c  Stmt,i) );.  col
1a354 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1a355 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1a356 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1a357 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  API sqlite_int64
1a358 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1a359 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74  int64(sqlite3_st
1a35a 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1a35b 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  ){.  sqlite_int6
1a35c 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f  4 val = sqlite3_
1a35d 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 6f 6c  value_int64( col
1a35e 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
1a35f 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
1a360 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
1a361 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
1a362 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1a363 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1a364 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1a365 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  text(sqlite3_stm
1a366 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1a367 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
1a368 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d 20 73  ed char *val = s
1a369 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1a36a 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  t( columnMem(pSt
1a36b 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1a36c 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1a36d 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1a36e 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
1a36f 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  I sqlite3_value 
1a370 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1a371 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74  value(sqlite3_st
1a372 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1a373 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d  ){.  Mem *pOut =
1a374 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1a375 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f 75 74  , i);.  if( pOut
1a376 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74  ->flags&MEM_Stat
1a377 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e  ic ){.    pOut->
1a378 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74  flags &= ~MEM_St
1a379 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  atic;.    pOut->
1a37a 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68  flags |= MEM_Eph
1a37b 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d 6e  em;.  }.  column
1a37c 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53  MallocFailure(pS
1a37d 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  tmt);.  return (
1a37e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 29  sqlite3_value *)
1a37f 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  pOut;.}.#ifndef 
1a380 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1a381 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
1a382 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1a383 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 73  _column_text16(s
1a384 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1a385 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f  mt, int i){.  co
1a386 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d 20  nst void *val = 
1a387 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1a388 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  xt16( columnMem(
1a389 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f  pStmt,i) );.  co
1a38a 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
1a38b 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
1a38c 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69 66  rn val;.}.#endif
1a38d 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1a38e 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f  UTF16 */.SQLITE_
1a38f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1a390 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c 69  column_type(sqli
1a391 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1a392 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 69   int i){.  int i
1a393 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  Type = sqlite3_v
1a394 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75 6d  alue_type( colum
1a395 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1a396 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1a397 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1a398 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b 0a 7d   return iType;.}
1a399 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
1a39a 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65  ng function is e
1a39b 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20  xperimental and 
1a39c 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
1a39d 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20  e or.** removal 
1a39e 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65 33  */./*int sqlite3
1a39f 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f  _column_numeric_
1a3a0 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  type(sqlite3_stm
1a3a1 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1a3a2 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71 6c  {.**  return sql
1a3a3 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72  ite3_value_numer
1a3a4 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d  ic_type( columnM
1a3a5 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 2a  em(pStmt,i) );.*
1a3a6 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  *}.*/../*.** Con
1a3a7 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 65 6c  vert the N-th el
1a3a8 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 2d 3e  ement of pStmt->
1a3a9 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f 20  pColName[] into 
1a3aa 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 0a 2a  a string using.*
1a3ab 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e 20 72  * xFunc() then r
1a3ac 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 69 6e  eturn that strin
1a3ad 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 74 20  g.  If N is out 
1a3ae 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e  of range, return
1a3af 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20   0..**.** There 
1a3b0 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 6d 65  are up to 5 name
1a3b1 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  s for each colum
1a3b2 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 74 65  n.  useType dete
1a3b3 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a 2a 20  rmines which.** 
1a3b4 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e 65 64  name is returned
1a3b5 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 65 20  .  Here are the 
1a3b6 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  names:.**.**    
1a3b7 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 75 6d  0      The colum
1a3b8 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 68 6f  n name as it sho
1a3b9 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 65 64  uld be displayed
1a3ba 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20   for output.**  
1a3bb 20 20 31 20 20 20 20 20 20 54 68 65 20 64 61 74    1      The dat
1a3bc 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 20 74  atype name for t
1a3bd 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20  he column.**    
1a3be 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20  2      The name 
1a3bf 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1a3c0 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20  that the column 
1a3c1 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20  derives from.** 
1a3c2 20 20 20 33 20 20 20 20 20 20 54 68 65 20 6e 61     3      The na
1a3c3 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1a3c4 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20  that the column 
1a3c5 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20  derives from.** 
1a3c6 20 20 20 34 20 20 20 20 20 20 54 68 65 20 6e 61     4      The na
1a3c7 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1a3c8 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20  column that the 
1a3c9 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65  result column de
1a3ca 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a  rives from.**.**
1a3cb 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   If the result i
1a3cc 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 63  s not a simple c
1a3cd 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
1a3ce 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78 70  (if it is an exp
1a3cf 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61 20  ression.** or a 
1a3d0 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20 75  constant) then u
1a3d1 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61 6e  seTypes 2, 3, an
1a3d2 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  d 4 return NULL.
1a3d3 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
1a3d4 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61 6d   void *columnNam
1a3d5 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  e(.  sqlite3_stm
1a3d6 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e 74 20  t *pStmt,.  int 
1a3d7 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  N,.  const void 
1a3d8 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a 29 2c  *(*xFunc)(Mem*),
1a3d9 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 0a 29  .  int useType.)
1a3da 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  {.  const void *
1a3db 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 65 20  ret = 0;.  Vdbe 
1a3dc 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
1a3dd 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 0a  mt;.  int n;.  .
1a3de 0a 20 20 69 66 28 20 70 21 3d 30 20 29 7b 0a 20  .  if( p!=0 ){. 
1a3df 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63     n = sqlite3_c
1a3e0 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d  olumn_count(pStm
1a3e1 74 29 3b 0a 20 20 20 20 69 66 28 20 4e 3c 6e 20  t);.    if( N<n 
1a3e2 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 20  && N>=0 ){.     
1a3e3 20 4e 20 2b 3d 20 75 73 65 54 79 70 65 2a 6e 3b   N += useType*n;
1a3e4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
1a3e5 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62  utex_enter(p->db
1a3e6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
1a3e7 72 65 74 20 3d 20 78 46 75 6e 63 28 26 70 2d 3e  ret = xFunc(&p->
1a3e8 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 0a 20  aColName[N]);.. 
1a3e9 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63       /* A malloc
1a3ea 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 64   may have failed
1a3eb 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 78   inside of the x
1a3ec 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66 20  Func() call. If 
1a3ed 74 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 73  this.      ** is
1a3ee 20 74 68 65 20 63 61 73 65 2c 20 63 6c 65 61 72   the case, clear
1a3ef 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65   the mallocFaile
1a3f0 64 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72  d flag and retur
1a3f1 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a 2f  n NULL..      */
1a3f2 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 64 62  .      if( p->db
1a3f3 20 26 26 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f   && p->db->mallo
1a3f4 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
1a3f5 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63     p->db->malloc
1a3f6 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20  Failed = 0;.    
1a3f7 20 20 20 20 72 65 74 20 3d 20 30 3b 0a 20 20 20      ret = 0;.   
1a3f8 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1a3f9 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1a3fa 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1a3fb 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1a3fc 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   ret;.}../*.** R
1a3fd 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f  eturn the name o
1a3fe 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
1a3ff 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
1a400 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 53  et returned by S
1a401 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  QL.** statement 
1a402 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pStmt..*/.SQLITE
1a403 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
1a404 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1a405 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
1a406 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
1a407 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
1a408 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
1a409 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
1a40a 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
1a40b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c  ite3_value_text,
1a40c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a   COLNAME_NAME);.
1a40d 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1a40e 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1a40f 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1a410 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
1a411 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33  n_name16(sqlite3
1a412 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1a413 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  t N){.  return c
1a414 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20  olumnName(.     
1a415 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73   pStmt, N, (cons
1a416 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29  t void*(*)(Mem*)
1a417 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1a418 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  ext16, COLNAME_N
1a419 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  AME);.}.#endif..
1a41a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74  /*.** Constraint
1a41b 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 45  :  If you have E
1a41c 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54  NABLE_COLUMN_MET
1a41d 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 6d  ADATA then you m
1a41e 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e  ust.** not defin
1a41f 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e  e OMIT_DECLTYPE.
1a420 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
1a421 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
1a422 54 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 64  TYPE) && defined
1a423 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43  (SQLITE_ENABLE_C
1a424 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a  OLUMN_METADATA).
1a425 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f  # error "Must no
1a426 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51  t define both SQ
1a427 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59  LITE_OMIT_DECLTY
1a428 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 6e  PE \.         an
1a429 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d SQLITE_ENABLE_
1a42a 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22  COLUMN_METADATA"
1a42b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1a42c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
1a42d 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75  LTYPE./*.** Retu
1a42e 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65  rn the column de
1a42f 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 28  claration type (
1a430 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 6f  if applicable) o
1a431 66 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c 75  f the 'i'th colu
1a432 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73  mn.** of the res
1a433 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 73  ult set of SQL s
1a434 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a  tatement pStmt..
1a435 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1a436 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1a437 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
1a438 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1a439 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1a43a 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1a43b 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1a43c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1a43d 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1a43e 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f  3_value_text, CO
1a43f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b  LNAME_DECLTYPE);
1a440 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1a441 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1a442 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1a443 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
1a444 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71  mn_decltype16(sq
1a445 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1a446 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1a447 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1a448 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1a449 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1a44a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1a44b 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e  lue_text16, COLN
1a44c 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d  AME_DECLTYPE);.}
1a44d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1a44e 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
1a44f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1a450 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a  _OMIT_DECLTYPE *
1a451 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
1a452 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
1a453 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65  ETADATA./*.** Re
1a454 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
1a455 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72   the database fr
1a456 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c  om which a resul
1a457 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73  t column derives
1a458 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74  ..** NULL is ret
1a459 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73  urned if the res
1a45a 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e  ult column is an
1a45b 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63   expression or c
1a45c 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e  onstant or.** an
1a45d 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63  ything else whic
1a45e 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62  h is not an unab
1a45f 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65  iguous reference
1a460 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63   to a database c
1a461 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  olumn..*/.SQLITE
1a462 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
1a463 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1a464 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71  database_name(sq
1a465 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1a466 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1a467 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1a468 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1a469 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1a46a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1a46b 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d  lue_text, COLNAM
1a46c 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23  E_DATABASE);.}.#
1a46d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a46e 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1a46f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1a470 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
1a471 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73  atabase_name16(s
1a472 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1a473 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1a474 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1a475 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1a476 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1a477 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1a478 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
1a479 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a  NAME_DATABASE);.
1a47a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1a47b 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1a47c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1a47d 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
1a47e 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20  able from which 
1a47f 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20  a result column 
1a480 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c  derives..** NULL
1a481 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
1a482 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  the result colum
1a483 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69  n is an expressi
1a484 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f  on or constant o
1a485 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c  r.** anything el
1a486 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20  se which is not 
1a487 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65  an unabiguous re
1a488 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74  ference to a dat
1a489 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f  abase column..*/
1a48a 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1a48b 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
1a48c 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
1a48d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1a48e 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1a48f 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1a490 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1a491 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1a492 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1a493 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f  3_value_text, CO
1a494 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a  LNAME_TABLE);.}.
1a495 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a496 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
1a497 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
1a498 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1a499 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c  table_name16(sql
1a49a 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1a49b 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1a49c 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1a49d 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1a49e 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1a49f 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1a4a0 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41  ue_text16, COLNA
1a4a1 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e  ME_TABLE);.}.#en
1a4a2 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1a4a3 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a  IT_UTF16 */../*.
1a4a4 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61  ** Return the na
1a4a5 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1a4a6 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63  column from whic
1a4a7 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  h a result colum
1a4a8 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55  n derives..** NU
1a4a9 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  LL is returned i
1a4aa 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c  f the result col
1a4ab 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73  umn is an expres
1a4ac 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74  sion or constant
1a4ad 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20   or.** anything 
1a4ae 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f  else which is no
1a4af 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20  t an unabiguous 
1a4b0 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64  reference to a d
1a4b1 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a  atabase column..
1a4b2 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1a4b3 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1a4b4 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  3_column_origin_
1a4b5 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
1a4b6 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
1a4b7 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
1a4b8 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
1a4b9 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
1a4ba 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
1a4bb 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c  ite3_value_text,
1a4bc 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29   COLNAME_COLUMN)
1a4bd 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1a4be 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1a4bf 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1a4c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
1a4c1 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
1a4c2 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
1a4c3 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1a4c4 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1a4c5 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1a4c6 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1a4c7 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1a4c8 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20  3_value_text16, 
1a4c9 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b  COLNAME_COLUMN);
1a4ca 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1a4cb 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1a4cc 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  /.#endif /* SQLI
1a4cd 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
1a4ce 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f  _METADATA */.../
1a4cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
1a4d1 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a  sqlite3_bind_  *
1a4d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a4d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a  **********.** .*
1a4d4 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20  * Routines used 
1a4d5 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 73  to attach values
1a4d6 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 6e   to wildcards in
1a4d7 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20   a compiled SQL 
1a4d8 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a  statement..*/./*
1a4d9 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 76  .** Unbind the v
1a4da 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 61  alue bound to va
1a4db 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 74  riable i in virt
1a4dc 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 54  ual machine p. T
1a4dd 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 74  his is the .** t
1a4de 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 69  he same as bindi
1a4df 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20  ng a NULL value 
1a4e0 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49  to the column. I
1a4e1 66 20 74 68 65 20 22 69 22 20 70 61 72 61 6d 65  f the "i" parame
1a4e2 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66  ter is.** out of
1a4e3 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c   range, then SQL
1a4e4 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 74  ITE_RANGE is ret
1a4e5 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 20  urned. Othewise 
1a4e6 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a  SQLITE_OK..**.**
1a4e7 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76   A successful ev
1a4e8 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  aluation of this
1a4e9 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65   routine acquire
1a4ea 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70  s the mutex on p
1a4eb 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69  ..** the mutex i
1a4ec 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e  s released if an
1a4ed 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20  y kind of error 
1a4ee 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  occurs..**.** Th
1a4ef 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f  e error code sto
1a4f0 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20  red in database 
1a4f1 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69  p->db is overwri
1a4f2 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65  tten with the re
1a4f3 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e  turn.** value in
1a4f4 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74   any case..*/.st
1a4f5 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62  atic int vdbeUnb
1a4f6 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ind(Vdbe *p, int
1a4f7 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72   i){.  Mem *pVar
1a4f8 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
1a4f9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1a4fa 55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  USE;.  sqlite3_m
1a4fb 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62  utex_enter(p->db
1a4fc 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
1a4fd 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  p->magic!=VDBE_M
1a4fe 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70  AGIC_RUN || p->p
1a4ff 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  c>=0 ){.    sqli
1a500 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20  te3Error(p->db, 
1a501 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30  SQLITE_MISUSE, 0
1a502 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1a503 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
1a504 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65  ->mutex);.    re
1a505 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
1a506 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c  SE;.  }.  if( i<
1a507 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29  1 || i>p->nVar )
1a508 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1a509 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45  or(p->db, SQLITE
1a50a 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20  _RANGE, 0);.    
1a50b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1a50c 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
1a50d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
1a50e 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a  LITE_RANGE;.  }.
1a50f 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20    i--;.  pVar = 
1a510 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73  &p->aVar[i];.  s
1a511 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1a512 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56  ease(pVar);.  pV
1a513 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ar->flags = MEM_
1a514 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45  Null;.  sqlite3E
1a515 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49  rror(p->db, SQLI
1a516 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74  TE_OK, 0);.  ret
1a517 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1a518 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74  ../*.** Bind a t
1a519 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75  ext or BLOB valu
1a51a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1a51b 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 6c   bindText(.  sql
1a51c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1a51d 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65  ,   /* The state
1a51e 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 61  ment to bind aga
1a51f 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c  inst */.  int i,
1a520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a521 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
1a522 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69   parameter to bi
1a523 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nd */.  const vo
1a524 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f  id *zData,     /
1a525 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
1a526 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e   data to be boun
1a527 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61  d */.  int nData
1a528 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
1a529 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1a52a 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62   of data to be b
1a52b 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28  ound */.  void (
1a52c 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20  *xDel)(void*),  
1a52d 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
1a52e 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  or the data */. 
1a52f 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 20 20   u8 encoding    
1a530 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64          /* Encod
1a531 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61  ing for the data
1a532 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70   */.){.  Vdbe *p
1a533 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
1a534 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20  ;.  Mem *pVar;. 
1a535 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d   int rc;..  rc =
1a536 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
1a537 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1a538 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
1a539 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20 20  ( zData!=0 ){.  
1a53a 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61      pVar = &p->a
1a53b 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20  Var[i-1];.      
1a53c 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1a53d 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c 20  MemSetStr(pVar, 
1a53e 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e  zData, nData, en
1a53f 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20  coding, xDel);. 
1a540 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1a541 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69  ITE_OK && encodi
1a542 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ng!=0 ){.       
1a543 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1a544 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1a545 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29  pVar, ENC(p->db)
1a546 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1a547 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d   sqlite3Error(p-
1a548 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20  >db, rc, 0);.   
1a549 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
1a54a 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72 63  piExit(p->db, rc
1a54b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
1a54c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1a54d 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1a54e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1a54f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20  .}.../*.** Bind 
1a550 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f 20  a blob value to 
1a551 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
1a552 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 51   variable..*/.SQ
1a553 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1a554 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 0a  ite3_bind_blob(.
1a555 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1a556 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c  pStmt, .  int i,
1a557 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
1a558 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44  zData, .  int nD
1a559 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  ata, .  void (*x
1a55a 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20  Del)(void*).){. 
1a55b 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74   return bindText
1a55c 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61  (pStmt, i, zData
1a55d 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 30  , nData, xDel, 0
1a55e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1a55f 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1a560 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
1a561 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1a562 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 75   i, double rValu
1a563 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e){.  int rc;.  
1a564 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
1a565 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20  *)pStmt;.  rc = 
1a566 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29  vdbeUnbind(p, i)
1a567 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1a568 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
1a569 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f  ite3VdbeMemSetDo
1a56a 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69 2d  uble(&p->aVar[i-
1a56b 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20 20  1], rValue);.   
1a56c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1a56d 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
1a56e 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  x);.  }.  return
1a56f 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   rc;.}.SQLITE_AP
1a570 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1a571 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73  nd_int(sqlite3_s
1a572 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20 69  tmt *p, int i, i
1a573 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72 65  nt iValue){.  re
1a574 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e  turn sqlite3_bin
1a575 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28 69  d_int64(p, i, (i
1a576 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 51  64)iValue);.}.SQ
1a577 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1a578 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
1a579 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1a57a 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69  tmt, int i, sqli
1a57b 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 29  te_int64 iValue)
1a57c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64  {.  int rc;.  Vd
1a57d 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
1a57e 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64  pStmt;.  rc = vd
1a57f 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
1a580 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1a581 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
1a582 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1a583 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c  4(&p->aVar[i-1],
1a584 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71   iValue);.    sq
1a585 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1a586 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1a587 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1a588 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
1a589 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1a58a 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d  null(sqlite3_stm
1a58b 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1a58c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64  {.  int rc;.  Vd
1a58d 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70  be *p = (Vdbe*)p
1a58e 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62  Stmt;.  rc = vdb
1a58f 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20  eUnbind(p, i);. 
1a590 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1a591 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1a592 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
1a593 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
1a594 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1a595 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1a596 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
1a597 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ( .  sqlite3_stm
1a598 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74  t *pStmt, .  int
1a599 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61   i, .  const cha
1a59a 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74  r *zData, .  int
1a59b 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20   nData, .  void 
1a59c 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
1a59d 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54  {.  return bindT
1a59e 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44  ext(pStmt, i, zD
1a59f 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c  ata, nData, xDel
1a5a0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
1a5a1 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1a5a2 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1a5a3 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1a5a4 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 0a  e3_bind_text16(.
1a5a5 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1a5a6 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c  pStmt, .  int i,
1a5a7 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
1a5a8 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44  zData, .  int nD
1a5a9 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  ata, .  void (*x
1a5aa 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20  Del)(void*).){. 
1a5ab 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74   return bindText
1a5ac 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61  (pStmt, i, zData
1a5ad 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53  , nData, xDel, S
1a5ae 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
1a5af 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  E);.}.#endif /* 
1a5b0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1a5b1 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  6 */.SQLITE_API 
1a5b2 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1a5b3 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73  _value(sqlite3_s
1a5b4 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1a5b5 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  i, const sqlite3
1a5b6 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b  _value *pValue){
1a5b7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62  .  int rc;.  Vdb
1a5b8 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
1a5b9 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62  Stmt;.  rc = vdb
1a5ba 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20  eUnbind(p, i);. 
1a5bb 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1a5bc 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  OK ){.    rc = s
1a5bd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70  qlite3VdbeMemCop
1a5be 79 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c  y(&p->aVar[i-1],
1a5bf 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 69 66   pValue);.    if
1a5c0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1a5c1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1a5c2 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
1a5c3 6e 63 6f 64 69 6e 67 28 26 70 2d 3e 61 56 61 72  ncoding(&p->aVar
1a5c4 5b 69 2d 31 5d 2c 20 45 4e 43 28 70 2d 3e 64 62  [i-1], ENC(p->db
1a5c5 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ));.    }.    sq
1a5c6 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1a5c7 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1a5c8 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
1a5c9 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62  te3ApiExit(p->db
1a5ca 2c 20 72 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  , rc);.  return 
1a5cb 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  rc;.}.SQLITE_API
1a5cc 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1a5cd 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74  d_zeroblob(sqlit
1a5ce 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1a5cf 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b 0a 20  int i, int n){. 
1a5d0 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20   int rc;.  Vdbe 
1a5d1 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
1a5d2 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55  mt;.  rc = vdbeU
1a5d3 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69  nbind(p, i);.  i
1a5d4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1a5d5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1a5d6 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f  dbeMemSetZeroBlo
1a5d7 62 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c  b(&p->aVar[i-1],
1a5d8 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   n);.    sqlite3
1a5d9 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
1a5da 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  db->mutex);.  }.
1a5db 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1a5dc 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1a5dd 20 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64 63   number of wildc
1a5de 61 72 64 73 20 74 68 61 74 20 63 61 6e 20 62 65  ards that can be
1a5df 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f 75   potentially bou
1a5e0 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20 72  nd to..** This r
1a5e1 6f 75 74 69 6e 65 20 69 73 20 61 64 64 65 64 20  outine is added 
1a5e2 74 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a 3a  to support DBD::
1a5e3 53 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 53 51 4c  SQLite.  .*/.SQL
1a5e4 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1a5e5 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
1a5e6 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  er_count(sqlite3
1a5e7 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
1a5e8 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
1a5e9 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72  *)pStmt;.  retur
1a5ea 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 72 20 3a 20  n p ? p->nVar : 
1a5eb 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  0;.}../*.** Crea
1a5ec 74 65 20 61 20 6d 61 70 70 69 6e 67 20 66 72 6f  te a mapping fro
1a5ed 6d 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65  m variable numbe
1a5ee 72 73 20 74 6f 20 76 61 72 69 61 62 6c 65 20 6e  rs to variable n
1a5ef 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56  ames.** in the V
1a5f0 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 61  dbe.azVar[] arra
1a5f1 79 2c 20 69 66 20 73 75 63 68 20 61 20 6d 61 70  y, if such a map
1a5f2 70 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61 6c  ping does not al
1a5f3 72 65 61 64 79 0a 2a 2a 20 65 78 69 73 74 2e 0a  ready.** exist..
1a5f4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
1a5f5 72 65 61 74 65 56 61 72 4d 61 70 28 56 64 62 65  reateVarMap(Vdbe
1a5f6 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e   *p){.  if( !p->
1a5f7 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c  okVar ){.    sql
1a5f8 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1a5f9 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1a5fa 20 20 20 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61      if( !p->okVa
1a5fb 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a  r ){.      int j
1a5fc 3b 0a 20 20 20 20 20 20 4f 70 20 2a 70 4f 70 3b  ;.      Op *pOp;
1a5fd 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20  .      for(j=0, 
1a5fe 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70 2d  pOp=p->aOp; j<p-
1a5ff 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b 2b  >nOp; j++, pOp++
1a600 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
1a601 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56  Op->opcode==OP_V
1a602 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  ariable ){.     
1a603 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1a604 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p1>0 && pOp->p
1a605 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20  1<=p->nVar );.  
1a606 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 72          p->azVar
1a607 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f  [pOp->p1-1] = pO
1a608 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 20  p->p4.z;.       
1a609 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1a60a 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31 3b 0a 20   p->okVar = 1;. 
1a60b 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1a60c 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
1a60d 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  db->mutex);.  }.
1a60e 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1a60f 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77 69  the name of a wi
1a610 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72  ldcard parameter
1a611 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
1a612 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 69  f the index.** i
1a613 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f  s out of range o
1a614 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 61 72  r if the wildcar
1a615 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a 2a  d is unnamed..**
1a616 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69  .** The result i
1a617 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e 0a  s always UTF-8..
1a618 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1a619 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1a61a 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
1a61b 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
1a61c 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1a61d 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
1a61e 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69  Vdbe*)pStmt;.  i
1a61f 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c  f( p==0 || i<1 |
1a620 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20  | i>p->nVar ){. 
1a621 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1a622 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28  .  createVarMap(
1a623 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p);.  return p->
1a624 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f  azVar[i-1];.}../
1a625 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69 6c  *.** Given a wil
1a626 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 20  dcard parameter 
1a627 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68 65  name, return the
1a628 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 61   index of the va
1a629 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20 74  riable.** with t
1a62a 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74 68  hat name.  If th
1a62b 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61 62  ere is no variab
1a62c 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76 65  le with the give
1a62d 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 72  n name,.** retur
1a62e 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  n 0..*/.SQLITE_A
1a62f 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1a630 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
1a631 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  dex(sqlite3_stmt
1a632 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63   *pStmt, const c
1a633 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56  har *zName){.  V
1a634 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29  dbe *p = (Vdbe*)
1a635 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a  pStmt;.  int i;.
1a636 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20    if( p==0 ){.  
1a637 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1a638 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 70    createVarMap(p
1a639 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20  ); .  if( zName 
1a63a 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
1a63b 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b  i<p->nVar; i++){
1a63c 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
1a63d 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 5b  r *z = p->azVar[
1a63e 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20  i];.      if( z 
1a63f 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 6d  && strcmp(z,zNam
1a640 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)==0 ){.       
1a641 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20   return i+1;.   
1a642 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1a643 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
1a644 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c  .** Transfer all
1a645 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74   bindings from t
1a646 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65  he first stateme
1a647 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 73  nt over to the s
1a648 65 63 6f 6e 64 2e 0a 2a 2a 20 49 66 20 74 68 65  econd..** If the
1a649 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73 20   two statements 
1a64a 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65 72  contain a differ
1a64b 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  ent number of bi
1a64c 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a 20  ndings, then.** 
1a64d 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  an SQLITE_ERROR 
1a64e 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
1a64f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1a650 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66  nt sqlite3Transf
1a651 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  erBindings(sqlit
1a652 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74  e3_stmt *pFromSt
1a653 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  mt, sqlite3_stmt
1a654 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64   *pToStmt){.  Vd
1a655 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62  be *pFrom = (Vdb
1a656 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20  e*)pFromStmt;.  
1a657 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62  Vdbe *pTo = (Vdb
1a658 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 6e  e*)pToStmt;.  in
1a659 74 20 69 2c 20 72 63 20 3d 20 53 51 4c 49 54 45  t i, rc = SQLITE
1a65a 5f 4f 4b 3b 0a 20 20 69 66 28 20 28 70 46 72 6f  _OK;.  if( (pFro
1a65b 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  m->magic!=VDBE_M
1a65c 41 47 49 43 5f 52 55 4e 20 26 26 20 70 46 72 6f  AGIC_RUN && pFro
1a65d 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  m->magic!=VDBE_M
1a65e 41 47 49 43 5f 48 41 4c 54 29 0a 20 20 20 20 7c  AGIC_HALT).    |
1a65f 7c 20 28 70 54 6f 2d 3e 6d 61 67 69 63 21 3d 56  | (pTo->magic!=V
1a660 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26  DBE_MAGIC_RUN &&
1a661 20 70 54 6f 2d 3e 6d 61 67 69 63 21 3d 56 44 42   pTo->magic!=VDB
1a662 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20 20  E_MAGIC_HALT).  
1a663 20 20 7c 7c 20 70 54 6f 2d 3e 64 62 21 3d 70 46    || pTo->db!=pF
1a664 72 6f 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 72  rom->db ){.    r
1a665 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1a666 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  USE;.  }.  if( p
1a667 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d  From->nVar!=pTo-
1a668 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65 74  >nVar ){.    ret
1a669 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1a66a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1a66b 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54 6f 2d  mutex_enter(pTo-
1a66c 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66  >db->mutex);.  f
1a66d 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
1a66e 54 45 5f 4f 4b 20 26 26 20 69 3c 70 46 72 6f 6d  TE_OK && i<pFrom
1a66f 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20  ->nVar; i++){.  
1a670 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1a671 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 72 5b  Move(&pTo->aVar[
1a672 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 61 72  i], &pFrom->aVar
1a673 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  [i]);.  }.  sqli
1a674 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1a675 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  pTo->db->mutex);
1a676 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
1a677 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d  QLITE_OK || rc==
1a678 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
1a679 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1a67a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a67b 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f  MIT_DEPRECATED./
1a67c 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 20  *.** Deprecated 
1a67d 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61  external interfa
1a67e 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f  ce.  Internal/co
1a67f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a 2a  re SQLite code.*
1a680 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71  * should call sq
1a681 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e  lite3TransferBin
1a682 64 69 6e 67 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  dings..*/.SQLITE
1a683 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1a684 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e  _transfer_bindin
1a685 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  gs(sqlite3_stmt 
1a686 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69  *pFromStmt, sqli
1a687 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d  te3_stmt *pToStm
1a688 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  t){.  return sql
1a689 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64  ite3TransferBind
1a68a 69 6e 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20  ings(pFromStmt, 
1a68b 70 54 6f 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64  pToStmt);.}.#end
1a68c 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
1a68d 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61   the sqlite3* da
1a68e 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 6f  tabase handle to
1a68f 20 77 68 69 63 68 20 74 68 65 20 70 72 65 70 61   which the prepa
1a690 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67 69  red statement gi
1a691 76 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72  ven.** in the ar
1a692 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20  gument belongs. 
1a693 20 54 68 69 73 20 69 73 20 74 68 65 20 73 61 6d   This is the sam
1a694 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1a695 65 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 74 68  e that was.** th
1a696 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
1a697 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f   to the sqlite3_
1a698 70 72 65 70 61 72 65 28 29 20 74 68 61 74 20 77  prepare() that w
1a699 61 73 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  as used to creat
1a69a 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65  e.** the stateme
1a69b 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  nt in the first 
1a69c 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  place..*/.SQLITE
1a69d 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71  _API sqlite3 *sq
1a69e 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28  lite3_db_handle(
1a69f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1a6a0 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  tmt){.  return p
1a6a1 53 74 6d 74 20 3f 20 28 28 56 64 62 65 2a 29 70  Stmt ? ((Vdbe*)p
1a6a2 53 74 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d  Stmt)->db : 0;.}
1a6a3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
1a6a4 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1a6a5 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74  next prepared st
1a6a6 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 70 53  atement after pS
1a6a7 74 6d 74 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  tmt associated.*
1a6a8 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20  * with database 
1a6a9 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62 2e 20  connection pDb. 
1a6aa 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c   If pStmt is NUL
1a6ab 4c 2c 20 72 65 74 75 72 6e 20 74 68 65 20 66 69  L, return the fi
1a6ac 72 73 74 0a 2a 2a 20 70 72 65 70 61 72 65 64 20  rst.** prepared 
1a6ad 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 68  statement for th
1a6ae 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1a6af 63 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 4e  ction.  Return N
1a6b0 55 4c 4c 20 69 66 20 74 68 65 72 65 0a 2a 2a 20  ULL if there.** 
1a6b1 61 72 65 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a  are no more..*/.
1a6b2 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
1a6b3 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65 33  e3_stmt *sqlite3
1a6b4 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71 6c 69 74  _next_stmt(sqlit
1a6b5 65 33 20 2a 70 44 62 2c 20 73 71 6c 69 74 65 33  e3 *pDb, sqlite3
1a6b6 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
1a6b7 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
1a6b8 4e 65 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Next;.  sqlite3_
1a6b9 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 62 2d  mutex_enter(pDb-
1a6ba 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70  >mutex);.  if( p
1a6bb 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  Stmt==0 ){.    p
1a6bc 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33 5f  Next = (sqlite3_
1a6bd 73 74 6d 74 2a 29 70 44 62 2d 3e 70 56 64 62 65  stmt*)pDb->pVdbe
1a6be 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1a6bf 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33 5f  Next = (sqlite3_
1a6c0 73 74 6d 74 2a 29 28 28 56 64 62 65 2a 29 70 53  stmt*)((Vdbe*)pS
1a6c1 74 6d 74 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  tmt)->pNext;.  }
1a6c2 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1a6c3 5f 6c 65 61 76 65 28 70 44 62 2d 3e 6d 75 74 65  _leave(pDb->mute
1a6c4 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65  x);.  return pNe
1a6c5 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  xt;.}../*.** Ret
1a6c6 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66  urn the value of
1a6c7 20 61 20 73 74 61 74 75 73 20 63 6f 75 6e 74 65   a status counte
1a6c8 72 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 64  r for a prepared
1a6c9 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51   statement.*/.SQ
1a6ca 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1a6cb 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
1a6cc 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1a6cd 53 74 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  Stmt, int op, in
1a6ce 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20  t resetFlag){.  
1a6cf 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 28 56  Vdbe *pVdbe = (V
1a6d0 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e  dbe*)pStmt;.  in
1a6d1 74 20 76 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f  t v = pVdbe->aCo
1a6d2 75 6e 74 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69  unter[op-1];.  i
1a6d3 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 20 70  f( resetFlag ) p
1a6d4 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f  Vdbe->aCounter[o
1a6d5 70 2d 31 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75  p-1] = 0;.  retu
1a6d6 72 6e 20 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  rn v;.}../******
1a6d7 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1a6d8 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a  vdbeapi.c ******
1a6d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a6da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a6db 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1a6dc 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1a6dd 69 6c 65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a  ile vdbe.c *****
1a6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a6df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a6e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1a6e1 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
1a6e2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1a6e3 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1a6e4 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1a6e5 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1a6e6 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1a6e7 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1a6e8 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1a6e9 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1a6ea 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1a6eb 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1a6ec 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1a6ed 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1a6ee 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1a6ef 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1a6f0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1a6f1 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1a6f2 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1a6f3 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1a6f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a6f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a6f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a6f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1a6f8 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
1a6f9 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
1a6fa 6e 74 73 20 65 78 65 63 75 74 69 6f 6e 20 6d 65  nts execution me
1a6fb 74 68 6f 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20  thod of the .** 
1a6fc 56 69 72 74 75 61 6c 20 44 61 74 61 62 61 73 65  Virtual Database
1a6fd 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 2e 20   Engine (VDBE). 
1a6fe 20 41 20 73 65 70 61 72 61 74 65 20 66 69 6c 65   A separate file
1a6ff 20 28 22 76 64 62 65 61 75 78 2e 63 22 29 0a 2a   ("vdbeaux.c").*
1a700 2a 20 68 61 6e 64 6c 65 73 20 68 6f 75 73 65 6b  * handles housek
1a701 65 65 70 69 6e 67 20 64 65 74 61 69 6c 73 20 73  eeping details s
1a702 75 63 68 20 61 73 20 63 72 65 61 74 69 6e 67 20  uch as creating 
1a703 61 6e 64 20 64 65 6c 65 74 69 6e 67 0a 2a 2a 20  and deleting.** 
1a704 56 44 42 45 20 69 6e 73 74 61 6e 63 65 73 2e 20  VDBE instances. 
1a705 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 73 6f   This file is so
1a706 6c 65 6c 79 20 69 6e 74 65 72 65 73 74 65 64 20  lely interested 
1a707 69 6e 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20  in executing.** 
1a708 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d  the VDBE program
1a709 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65  ..**.** In the e
1a70a 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63  xternal interfac
1a70b 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73  e, an "sqlite3_s
1a70c 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70 61 71  tmt*" is an opaq
1a70d 75 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f  ue pointer.** to
1a70e 20 61 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54   a VDBE..**.** T
1a70f 68 65 20 53 51 4c 20 70 61 72 73 65 72 20 67 65  he SQL parser ge
1a710 6e 65 72 61 74 65 73 20 61 20 70 72 6f 67 72 61  nerates a progra
1a711 6d 20 77 68 69 63 68 20 69 73 20 74 68 65 6e 20  m which is then 
1a712 65 78 65 63 75 74 65 64 20 62 79 0a 2a 2a 20 74  executed by.** t
1a713 68 65 20 56 44 42 45 20 74 6f 20 64 6f 20 74 68  he VDBE to do th
1a714 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 20 53 51  e work of the SQ
1a715 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 56 44  L statement.  VD
1a716 42 45 20 70 72 6f 67 72 61 6d 73 20 61 72 65 20  BE programs are 
1a717 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e 20 66  .** similar in f
1a718 6f 72 6d 20 74 6f 20 61 73 73 65 6d 62 6c 79 20  orm to assembly 
1a719 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 20 70  language.  The p
1a71a 72 6f 67 72 61 6d 20 63 6f 6e 73 69 73 74 73 20  rogram consists 
1a71b 6f 66 0a 2a 2a 20 61 20 6c 69 6e 65 61 72 20 73  of.** a linear s
1a71c 65 71 75 65 6e 63 65 20 6f 66 20 6f 70 65 72 61  equence of opera
1a71d 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 6f 70 65  tions.  Each ope
1a71e 72 61 74 69 6f 6e 20 68 61 73 20 61 6e 20 6f 70  ration has an op
1a71f 63 6f 64 65 20 0a 2a 2a 20 61 6e 64 20 35 20 6f  code .** and 5 o
1a720 70 65 72 61 6e 64 73 2e 20 20 4f 70 65 72 61 6e  perands.  Operan
1a721 64 73 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 50  ds P1, P2, and P
1a722 33 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e 20  3 are integers. 
1a723 20 4f 70 65 72 61 6e 64 20 50 34 20 0a 2a 2a 20   Operand P4 .** 
1a724 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e  is a null-termin
1a725 61 74 65 64 20 73 74 72 69 6e 67 2e 20 20 4f 70  ated string.  Op
1a726 65 72 61 6e 64 20 50 35 20 69 73 20 61 6e 20 75  erand P5 is an u
1a727 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65  nsigned characte
1a728 72 2e 0a 2a 2a 20 46 65 77 20 6f 70 63 6f 64 65  r..** Few opcode
1a729 73 20 75 73 65 20 61 6c 6c 20 35 20 6f 70 65 72  s use all 5 oper
1a72a 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70  ands..**.** Comp
1a72b 75 74 61 74 69 6f 6e 20 72 65 73 75 6c 74 73 20  utation results 
1a72c 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20  are stored on a 
1a72d 73 65 74 20 6f 66 20 72 65 67 69 73 74 65 72 73  set of registers
1a72e 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e   numbered beginn
1a72f 69 6e 67 0a 2a 2a 20 77 69 74 68 20 31 20 61 6e  ing.** with 1 an
1a730 64 20 67 6f 69 6e 67 20 75 70 20 74 6f 20 56 64  d going up to Vd
1a731 62 65 2e 6e 4d 65 6d 2e 20 20 45 61 63 68 20 72  be.nMem.  Each r
1a732 65 67 69 73 74 65 72 20 63 61 6e 20 73 74 6f 72  egister can stor
1a733 65 0a 2a 2a 20 65 69 74 68 65 72 20 61 6e 20 69  e.** either an i
1a734 6e 74 65 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74  nteger, a null-t
1a735 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67  erminated string
1a736 2c 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  , a floating poi
1a737 6e 74 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72  nt.** number, or
1a738 20 74 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20   the SQL "NULL" 
1a739 76 61 6c 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69  value.  An impli
1a73a 63 69 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66  cit conversion f
1a73b 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20  rom one.** type 
1a73c 74 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63 63  to the other occ
1a73d 75 72 73 20 61 73 20 6e 65 63 65 73 73 61 72 79  urs as necessary
1a73e 2e 0a 2a 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66  ..** .** Most of
1a73f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   the code in thi
1a740 73 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e 20  s file is taken 
1a741 75 70 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  up by the sqlite
1a742 33 56 64 62 65 45 78 65 63 28 29 0a 2a 2a 20 66  3VdbeExec().** f
1a743 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 64 6f  unction which do
1a744 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 69  es the work of i
1a745 6e 74 65 72 70 72 65 74 69 6e 67 20 61 20 56 44  nterpreting a VD
1a746 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42  BE program..** B
1a747 75 74 20 6f 74 68 65 72 20 72 6f 75 74 69 6e 65  ut other routine
1a748 73 20 61 72 65 20 61 6c 73 6f 20 70 72 6f 76 69  s are also provi
1a749 64 65 64 20 74 6f 20 68 65 6c 70 20 69 6e 20 62  ded to help in b
1a74a 75 69 6c 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20  uilding up.** a 
1a74b 70 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74  program instruct
1a74c 69 6f 6e 20 62 79 20 69 6e 73 74 72 75 63 74 69  ion by instructi
1a74d 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75  on..**.** Variou
1a74e 73 20 73 63 72 69 70 74 73 20 73 63 61 6e 20 74  s scripts scan t
1a74f 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20  his source file 
1a750 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65  in order to gene
1a751 72 61 74 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63  rate HTML.** doc
1a752 75 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64  umentation, head
1a753 65 72 73 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74  ers files, or ot
1a754 68 65 72 20 64 65 72 69 76 65 64 20 66 69 6c 65  her derived file
1a755 73 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 74 69  s.  The formatti
1a756 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64  ng.** of the cod
1a757 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
1a758 73 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 69 6d  s, therefore, im
1a759 70 6f 72 74 61 6e 74 2e 20 20 53 65 65 20 6f 74  portant.  See ot
1a75a 68 65 72 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20  her comments.** 
1a75b 69 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72  in this file for
1a75c 20 64 65 74 61 69 6c 73 2e 20 20 49 66 20 69 6e   details.  If in
1a75d 20 64 6f 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64   doubt, do not d
1a75e 65 76 69 61 74 65 20 66 72 6f 6d 20 65 78 69 73  eviate from exis
1a75f 74 69 6e 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69  ting.** commenti
1a760 6e 67 20 61 6e 64 20 69 6e 64 65 6e 74 61 74 69  ng and indentati
1a761 6f 6e 20 70 72 61 63 74 69 63 65 73 20 77 68 65  on practices whe
1a762 6e 20 63 68 61 6e 67 69 6e 67 20 6f 72 20 61 64  n changing or ad
1a763 64 69 6e 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  ding code..**.**
1a764 20 24 49 64 3a 20 76 64 62 65 2e 63 2c 76 20 31   $Id: vdbe.c,v 1
1a765 2e 38 32 38 20 32 30 30 39 2f 30 33 2f 32 33 20  .828 2009/03/23 
1a766 31 37 3a 31 31 3a 32 37 20 64 61 6e 69 65 6c 6b  17:11:27 danielk
1a767 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f  1977 Exp $.*/../
1a768 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1a769 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  ng global variab
1a76a 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  le is incremente
1a76b 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20 63  d every time a c
1a76c 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20  ursor.** moves, 
1a76d 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f 50  either by the OP
1a76e 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74  _SeekXX, OP_Next
1a76f 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70 63  , or OP_Prev opc
1a770 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74 0a  odes.  The test.
1a771 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75 73  ** procedures us
1a772 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  e this informati
1a773 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  on to make sure 
1a774 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 65  that indices are
1a775 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72  .** working corr
1a776 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61 72  ectly.  This var
1a777 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e  iable has no fun
1a778 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e  ction other than
1a779 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69   to.** help veri
1a77a 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  fy the correct o
1a77b 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  peration of the 
1a77c 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64  library..*/.#ifd
1a77d 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
1a77e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1a77f 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
1a780 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
1a781 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  /*.** When this 
1a782 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1a783 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74 20  is positive, it 
1a784 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65 64  gets decremented
1a785 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20   once before.** 
1a786 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e  each instruction
1a787 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 57   in the VDBE.  W
1a788 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 72 6f  hen reaches zero
1a789 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 65 72  , the u1.isInter
1a78a 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20  rupted.** field 
1a78b 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 20 73  of the sqlite3 s
1a78c 74 72 75 63 74 75 72 65 20 69 73 20 73 65 74 20  tructure is set 
1a78d 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d 75  in order to simu
1a78e 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 72 75  late and interru
1a78f 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  pt..**.** This f
1a790 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 64 20  acility is used 
1a791 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70  for testing purp
1a792 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64  oses only.  It d
1a793 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e  oes not function
1a794 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61  .** in an ordina
1a795 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66  ry build..*/.#if
1a796 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1a797 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1a798 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1a799 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64  _count = 0;.#end
1a79a 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65  if../*.** The ne
1a79b 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  xt global variab
1a79c 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  le is incremente
1a79d 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65 20  d each type the 
1a79e 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a  OP_Sort opcode.*
1a79f 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 20  * is executed.  
1a7a0 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64 75  The test procedu
1a7a1 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e 66  res use this inf
1a7a2 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65  ormation to make
1a7a3 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f   sure that.** so
1a7a4 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72 69  rting is occurri
1a7a5 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72  ng or not occurr
1a7a6 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69 61  ing at appropria
1a7a7 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69 73  te times.   This
1a7a8 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73   variable.** has
1a7a9 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68   no function oth
1a7aa 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70 20  er than to help 
1a7ab 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 65  verify the corre
1a7ac 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  ct operation of 
1a7ad 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a  the.** library..
1a7ae 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1a7af 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
1a7b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f 72   int sqlite3_sor
1a7b1 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  t_count = 0;.#en
1a7b2 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  dif../*.** The n
1a7b3 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ext global varia
1a7b4 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 20  ble records the 
1a7b5 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67  size of the larg
1a7b6 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20  est MEM_Blob.** 
1a7b7 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74 20  or MEM_Str that 
1a7b8 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 79  has been used by
1a7b9 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e 20   a VDBE opcode. 
1a7ba 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64   The test proced
1a7bb 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69 73  ures.** use this
1a7bc 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
1a7bd 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
1a7be 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e  he zero-blob fun
1a7bf 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73  ctionality.** is
1a7c0 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74   working correct
1a7c1 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69 61  ly.   This varia
1a7c2 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74  ble has no funct
1a7c3 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74  ion other than t
1a7c4 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79  o.** help verify
1a7c5 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65   the correct ope
1a7c6 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  ration of the li
1a7c7 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66  brary..*/.#ifdef
1a7c8 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
1a7c9 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1a7ca 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65  te3_max_blobsize
1a7cb 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f 69   = 0;.static voi
1a7cc 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73  d updateMaxBlobs
1a7cd 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69  ize(Mem *p){.  i
1a7ce 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28  f( (p->flags & (
1a7cf 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
1a7d0 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71  ))!=0 && p->n>sq
1a7d1 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69  lite3_max_blobsi
1a7d2 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ze ){.    sqlite
1a7d3 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d  3_max_blobsize =
1a7d4 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e   p->n;.  }.}.#en
1a7d5 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  dif../*.** Test 
1a7d6 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 73 65  a register to se
1a7d7 65 20 69 66 20 69 74 20 65 78 63 65 65 64 73 20  e if it exceeds 
1a7d8 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69  the current maxi
1a7d9 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a  mum blob size..*
1a7da 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65  * If it does, re
1a7db 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78  cord the new max
1a7dc 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a  imum blob size..
1a7dd 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
1a7de 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 21  QLITE_TEST) && !
1a7df 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1a7e0 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
1a7e1 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41 54  ).# define UPDAT
1a7e2 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50  E_MAX_BLOBSIZE(P
1a7e3 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62  )  updateMaxBlob
1a7e4 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20  size(P).#else.# 
1a7e5 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41  define UPDATE_MA
1a7e6 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65  X_BLOBSIZE(P).#e
1a7e7 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  ndif../*.** Conv
1a7e8 65 72 74 20 74 68 65 20 67 69 76 65 6e 20 72 65  ert the given re
1a7e9 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74  gister into a st
1a7ea 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74  ring if it isn't
1a7eb 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e   one.** already.
1a7ec 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   Return non-zero
1a7ed 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   if a malloc() f
1a7ee 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ails..*/.#define
1a7ef 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e   Stringify(P, en
1a7f0 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d  c) \.   if(((P)-
1a7f1 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c  >flags&(MEM_Str|
1a7f2 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26  MEM_Blob))==0 &&
1a7f3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a7f4 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29  tringify(P,enc))
1a7f5 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e   \.     { goto n
1a7f6 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20  o_mem; }../*.** 
1a7f7 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72  An ephemeral str
1a7f8 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69  ing value (signi
1a7f9 66 69 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f  fied by the MEM_
1a7fa 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74  Ephem flag) cont
1a7fb 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  ains.** a pointe
1a7fc 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c  r to a dynamical
1a7fd 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72  ly allocated str
1a7fe 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f  ing where some o
1a7ff 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69  ther entity.** i
1a800 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
1a801 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74  r deallocating t
1a802 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63  hat string.  Bec
1a803 61 75 73 65 20 74 68 65 20 72 65 67 69 73 74 65  ause the registe
1a804 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f  r.** does not co
1a805 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67  ntrol the string
1a806 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 65  , it might be de
1a807 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20 74 68  leted without th
1a808 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e  e register.** kn
1a809 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  owing it..**.** 
1a80a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e  This routine con
1a80b 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72  verts an ephemer
1a80c 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61  al string into a
1a80d 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
1a80e 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67  ocated.** string
1a80f 20 74 68 61 74 20 74 68 65 20 72 65 67 69 73 74   that the regist
1a810 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f  er itself contro
1a811 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ls.  In other wo
1a812 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65  rds, it.** conve
1a813 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d  rts an MEM_Ephem
1a814 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20   string into an 
1a815 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a  MEM_Dyn string..
1a816 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68  */.#define Deeph
1a817 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20  emeralize(P) \. 
1a818 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67    if( ((P)->flag
1a819 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20  s&MEM_Ephem)!=0 
1a81a 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69  \.       && sqli
1a81b 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72  te3VdbeMemMakeWr
1a81c 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f  iteable(P) ){ go
1a81d 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a  to no_mem;}../*.
1a81e 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56  ** Call sqlite3V
1a81f 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
1a820 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69  () on the suppli
1a821 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d  ed value (type M
1a822 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71  em*).** P if req
1a823 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  uired..*/.#defin
1a824 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20  e ExpandBlob(P) 
1a825 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d  (((P)->flags&MEM
1a826 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64  _Zero)?sqlite3Vd
1a827 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28  beMemExpandBlob(
1a828 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67  P):0)../*.** Arg
1a829 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74  ument pMem point
1a82a 73 20 61 74 20 61 20 72 65 67 69 73 74 65 72 20  s at a register 
1a82b 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73  that will be pas
1a82c 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72  sed to a.** user
1a82d 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
1a82e 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f  n or returned to
1a82f 20 74 68 65 20 75 73 65 72 20 61 73 20 74 68 65   the user as the
1a830 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65   result of a que
1a831 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  ry..** The secon
1a832 64 20 61 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f  d argument, 'db_
1a833 65 6e 63 27 20 69 73 20 74 68 65 20 74 65 78 74  enc' is the text
1a834 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62   encoding used b
1a835 79 20 74 68 65 20 76 64 62 65 20 66 6f 72 0a 2a  y the vdbe for.*
1a836 2a 20 72 65 67 69 73 74 65 72 20 76 61 72 69 61  * register varia
1a837 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f 75 74  bles.  This rout
1a838 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 4d 65  ine sets the pMe
1a839 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d  m->enc and pMem-
1a83a 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c  >type.** variabl
1a83b 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73  es used by the s
1a83c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29  qlite3_value_*()
1a83d 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64   routines..*/.#d
1a83e 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70 65 49  efine storeTypeI
1a83f 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65 54  nfo(A,B) _storeT
1a840 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61 74 69  ypeInfo(A).stati
1a841 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54 79 70  c void _storeTyp
1a842 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29  eInfo(Mem *pMem)
1a843 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20  {.  int flags = 
1a844 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69  pMem->flags;.  i
1a845 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  f( flags & MEM_N
1a846 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  ull ){.    pMem-
1a847 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e  >type = SQLITE_N
1a848 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20  ULL;.  }.  else 
1a849 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f  if( flags & MEM_
1a84a 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  Int ){.    pMem-
1a84b 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49  >type = SQLITE_I
1a84c 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c  NTEGER;.  }.  el
1a84d 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  se if( flags & M
1a84e 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 70  EM_Real ){.    p
1a84f 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a850 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20  TE_FLOAT;.  }.  
1a851 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
1a852 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20   MEM_Str ){.    
1a853 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a854 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73  ITE_TEXT;.  }els
1a855 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  e{.    pMem->typ
1a856 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b  e = SQLITE_BLOB;
1a857 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  .  }.}../*.** Pr
1a858 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f  operties of opco
1a859 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f  des.  The OPFLG_
1a85a 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72  INITIALIZER macr
1a85b 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20  o is.** created 
1a85c 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b  by mkopcodeh.awk
1a85d 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74   during compilat
1a85e 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62  ion.  Data is ob
1a85f 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74  tained.** from t
1a860 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c  he comments foll
1a861 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 20  owing the "case 
1a862 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d  OP_xxxx:" statem
1a863 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20  ents in.** this 
1a864 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69  file.  .*/.stati
1a865 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
1a866 20 63 68 61 72 20 6f 70 63 6f 64 65 50 72 6f 70   char opcodeProp
1a867 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49  erty[] = OPFLG_I
1a868 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 0a  NITIALIZER;../*.
1a869 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
1a86a 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 61 73 20  f an opcode has 
1a86b 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 46 4c 47  any of the OPFLG
1a86c 5f 78 78 78 20 70 72 6f 70 65 72 74 69 65 73 0a  _xxx properties.
1a86d 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ** specified by 
1a86e 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mask..*/.SQLITE_
1a86f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1a870 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61 73  te3VdbeOpcodeHas
1a871 50 72 6f 70 65 72 74 79 28 69 6e 74 20 6f 70 63  Property(int opc
1a872 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a  ode, int mask){.
1a873 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65    assert( opcode
1a874 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c 28 69 6e  >0 && opcode<(in
1a875 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f 64 65 50  t)sizeof(opcodeP
1a876 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 20 72 65  roperty) );.  re
1a877 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 72 6f 70  turn (opcodeProp
1a878 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 6d 61 73  erty[opcode]&mas
1a879 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  k)!=0;.}../*.** 
1a87a 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 75 72  Allocate VdbeCur
1a87b 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2e  sor number iCur.
1a87c 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
1a87d 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75 72  er to it.  Retur
1a87e 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20  n NULL.** if we 
1a87f 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  run out of memor
1a880 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 64 62  y..*/.static Vdb
1a881 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 74  eCursor *allocat
1a882 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 65 20  eCursor(.  Vdbe 
1a883 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
1a884 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
1a885 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  machine */.  int
1a886 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20   iCur,          
1a887 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
1a888 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 73 6f  he new VdbeCurso
1a889 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c  r */.  int nFiel
1a88a 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d,           /* 
1a88b 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73  Number of fields
1a88c 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72   in the table or
1a88d 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
1a88e 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  iDb,            
1a88f 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 61 62 61    /* When databa
1a890 73 65 20 74 68 65 20 63 75 72 73 6f 72 20 62 65  se the cursor be
1a891 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d 31 20  longs to, or -1 
1a892 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 65 65  */.  int isBtree
1a893 43 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 2a 2f  Cursor     /* */
1a894 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68  .){.  /* Find th
1a895 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68  e memory cell th
1a896 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  at will be used 
1a897 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f  to store the blo
1a898 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a  b of memory.  **
1a899 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68   required for th
1a89a 69 73 20 56 64 62 65 43 75 72 73 6f 72 20 73 74  is VdbeCursor st
1a89b 72 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 63  ructure. It is c
1a89c 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65  onvenient to use
1a89d 20 61 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65   a .  ** vdbe me
1a89e 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e  mory cell to man
1a89f 61 67 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  age the memory a
1a8a0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72  llocation requir
1a8a1 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64  ed for a.  ** Vd
1a8a2 62 65 43 75 72 73 6f 72 20 73 74 72 75 63 74 75  beCursor structu
1a8a3 72 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f  re for the follo
1a8a4 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20  wing reasons:.  
1a8a5 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65  **.  **   * Some
1a8a6 74 69 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d  times cursor num
1a8a7 62 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f  bers are used fo
1a8a8 72 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69  r a couple of di
1a8a9 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20  fferent.  **    
1a8aa 20 70 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76   purposes in a v
1a8ab 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65  dbe program. The
1a8ac 20 64 69 66 66 65 72 65 6e 74 20 75 73 65 73 20   different uses 
1a8ad 6d 69 67 68 74 20 72 65 71 75 69 72 65 0a 20 20  might require.  
1a8ae 2a 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74  **     different
1a8af 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f   sized allocatio
1a8b0 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73  ns. Memory cells
1a8b1 20 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c   provide growabl
1a8b2 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63  e.  **     alloc
1a8b3 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ations..  **.  *
1a8b4 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67  *   * When using
1a8b5 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d   ENABLE_MEMORY_M
1a8b6 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72  ANAGEMENT, memor
1a8b7 79 20 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63  y cell buffers c
1a8b8 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66  an.  **     be f
1a8b9 72 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20  reed lazily via 
1a8ba 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  the sqlite3_rele
1a8bb 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49  ase_memory() API
1a8bc 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20  . This.  **     
1a8bd 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75  minimizes the nu
1a8be 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63  mber of malloc c
1a8bf 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65  alls made by the
1a8c0 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20   system..  **.  
1a8c1 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  ** Memory cells 
1a8c2 66 6f 72 20 63 75 72 73 6f 72 73 20 61 72 65 20  for cursors are 
1a8c3 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65  allocated at the
1a8c4 20 74 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72   top of the addr
1a8c5 65 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20  ess.  ** space. 
1a8c6 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e  Memory cell (p->
1a8c7 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64  nMem) correspond
1a8c8 73 20 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53  s to cursor 0. S
1a8c9 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75  pace for.  ** cu
1a8ca 72 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65  rsor 1 is manage
1a8cb 64 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  d by memory cell
1a8cc 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74   (p->nMem-1), et
1a8cd 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  c..  */.  Mem *p
1a8ce 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Mem = &p->aMem[p
1a8cf 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20  ->nMem-iCur];.. 
1a8d0 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64   int nByte;.  Vd
1a8d1 62 65 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20  beCursor *pCx = 
1a8d2 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 0a 20 20  0;.  nByte = .  
1a8d3 20 20 20 20 73 69 7a 65 6f 66 28 56 64 62 65 43      sizeof(VdbeC
1a8d4 75 72 73 6f 72 29 20 2b 20 0a 20 20 20 20 20 20  ursor) + .      
1a8d5 28 69 73 42 74 72 65 65 43 75 72 73 6f 72 3f 73  (isBtreeCursor?s
1a8d6 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
1a8d7 72 53 69 7a 65 28 29 3a 30 29 20 2b 20 0a 20 20  rSize():0) + .  
1a8d8 20 20 20 20 32 2a 6e 46 69 65 6c 64 2a 73 69 7a      2*nField*siz
1a8d9 65 6f 66 28 75 33 32 29 3b 0a 0a 20 20 61 73 73  eof(u32);..  ass
1a8da 65 72 74 28 20 69 43 75 72 3c 70 2d 3e 6e 43 75  ert( iCur<p->nCu
1a8db 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 2d  rsor );.  if( p-
1a8dc 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 29 7b 0a  >apCsr[iCur] ){.
1a8dd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
1a8de 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e  reeCursor(p, p->
1a8df 61 70 43 73 72 5b 69 43 75 72 5d 29 3b 0a 20 20  apCsr[iCur]);.  
1a8e0 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d    p->apCsr[iCur]
1a8e1 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
1a8e2 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74  SQLITE_OK==sqlit
1a8e3 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
1a8e4 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 7b  em, nByte, 0) ){
1a8e5 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43  .    p->apCsr[iC
1a8e6 75 72 5d 20 3d 20 70 43 78 20 3d 20 28 56 64 62  ur] = pCx = (Vdb
1a8e7 65 43 75 72 73 6f 72 2a 29 70 4d 65 6d 2d 3e 7a  eCursor*)pMem->z
1a8e8 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4d 65  ;.    memset(pMe
1a8e9 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b  m->z, 0, nByte);
1a8ea 0a 20 20 20 20 70 43 78 2d 3e 69 44 62 20 3d 20  .    pCx->iDb = 
1a8eb 69 44 62 3b 0a 20 20 20 20 70 43 78 2d 3e 6e 46  iDb;.    pCx->nF
1a8ec 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a 20  ield = nField;. 
1a8ed 20 20 20 69 66 28 20 6e 46 69 65 6c 64 20 29 7b     if( nField ){
1a8ee 0a 20 20 20 20 20 20 70 43 78 2d 3e 61 54 79 70  .      pCx->aTyp
1a8ef 65 20 3d 20 28 75 33 32 20 2a 29 26 70 4d 65 6d  e = (u32 *)&pMem
1a8f0 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65 43  ->z[sizeof(VdbeC
1a8f1 75 72 73 6f 72 29 5d 3b 0a 20 20 20 20 7d 0a 20  ursor)];.    }. 
1a8f2 20 20 20 69 66 28 20 69 73 42 74 72 65 65 43 75     if( isBtreeCu
1a8f3 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 70 43  rsor ){.      pC
1a8f4 78 2d 3e 70 43 75 72 73 6f 72 20 3d 20 28 42 74  x->pCursor = (Bt
1a8f5 43 75 72 73 6f 72 2a 29 0a 20 20 20 20 20 20 20  Cursor*).       
1a8f6 20 20 20 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65     &pMem->z[size
1a8f7 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 2b 32  of(VdbeCursor)+2
1a8f8 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75  *nField*sizeof(u
1a8f9 33 32 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  32)];.    }.  }.
1a8fa 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a    return pCx;.}.
1a8fb 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f  ./*.** Try to co
1a8fc 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20 69 6e  nvert a value in
1a8fd 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70  to a numeric rep
1a8fe 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 77  resentation if w
1a8ff 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77  e can.** do so w
1a900 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69  ithout loss of i
1a901 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20  nformation.  In 
1a902 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
1a903 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f  the string.** lo
1a904 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65  oks like a numbe
1a905 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e  r, convert it in
1a906 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66  to a number.  If
1a907 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20   it does not.** 
1a908 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62  look like a numb
1a909 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61 6c 6f  er, leave it alo
1a90a 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ne..*/.static vo
1a90b 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41  id applyNumericA
1a90c 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65  ffinity(Mem *pRe
1a90d 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65 63 2d  c){.  if( (pRec-
1a90e 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65  >flags & (MEM_Re
1a90f 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20  al|MEM_Int))==0 
1a910 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61 6c 6e  ){.    int realn
1a911 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  um;.    sqlite3V
1a912 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
1a913 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66  te(pRec);.    if
1a914 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d  ( (pRec->flags&M
1a915 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20 20 20  EM_Str).        
1a916 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e 75 6d   && sqlite3IsNum
1a917 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65  ber(pRec->z, &re
1a918 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63  alnum, pRec->enc
1a919 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76  ) ){.      i64 v
1a91a 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  alue;.      sqli
1a91b 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1a91c 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51 4c 49  oding(pRec, SQLI
1a91d 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20  TE_UTF8);.      
1a91e 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26 26 20  if( !realnum && 
1a91f 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 52  sqlite3Atoi64(pR
1a920 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29  ec->z, &value) )
1a921 7b 0a 20 20 20 20 20 20 20 20 70 52 65 63 2d 3e  {.        pRec->
1a922 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20  u.i = value;.   
1a923 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46       MemSetTypeF
1a924 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e  lag(pRec, MEM_In
1a925 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
1a926 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a927 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70  VdbeMemRealify(p
1a928 52 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Rec);.      }.  
1a929 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1a92a 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64   Processing is d
1a92b 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20  etermine by the 
1a92c 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74  affinity paramet
1a92d 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  er:.**.** SQLITE
1a92e 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a  _AFF_INTEGER:.**
1a92f 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
1a930 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  :.** SQLITE_AFF_
1a931 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54  NUMERIC:.**    T
1a932 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52  ry to convert pR
1a933 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  ec to an integer
1a934 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1a935 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61  or a .**    floa
1a936 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65  ting-point repre
1a937 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20  sentation if an 
1a938 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e  integer represen
1a939 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20  tation.**    is 
1a93a 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e  not possible.  N
1a93b 6f 74 65 20 74 68 61 74 20 74 68 65 20 69 6e 74  ote that the int
1a93c 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74  eger representat
1a93d 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77  ion is.**    alw
1a93e 61 79 73 20 70 72 65 66 65 72 72 65 64 2c 20 65  ays preferred, e
1a93f 76 65 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e  ven if the affin
1a940 69 74 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63  ity is REAL, bec
1a941 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e  ause.**    an in
1a942 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61  teger representa
1a943 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61  tion is more spa
1a944 63 65 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20  ce efficient on 
1a945 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  disk..**.** SQLI
1a946 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20  TE_AFF_TEXT:.** 
1a947 20 20 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20     Convert pRec 
1a948 74 6f 20 61 20 74 65 78 74 20 72 65 70 72 65 73  to a text repres
1a949 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
1a94a 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a  SQLITE_AFF_NONE:
1a94b 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70  .**    No-op.  p
1a94c 52 65 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64  Rec is unchanged
1a94d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1a94e 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a   applyAffinity(.
1a94f 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20    Mem *pRec,    
1a950 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c        /* The val
1a951 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69  ue to apply affi
1a952 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61  nity to */.  cha
1a953 72 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20  r affinity,     
1a954 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
1a955 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a   to be applied *
1a956 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 20 20 20  /.  u8 enc      
1a957 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74          /* Use t
1a958 68 69 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  his text encodin
1a959 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66  g */.){.  if( af
1a95a 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
1a95b 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f  FF_TEXT ){.    /
1a95c 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74  * Only attempt t
1a95d 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f  he conversion to
1a95e 20 54 45 58 54 20 69 66 20 74 68 65 72 65 20 69   TEXT if there i
1a95f 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20  s an integer or 
1a960 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72  real.    ** repr
1a961 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62  esentation (blob
1a962 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74   and NULL do not
1a963 20 67 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20   get converted) 
1a964 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20  but no string.  
1a965 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74    ** representat
1a966 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
1a967 69 66 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c  if( 0==(pRec->fl
1a968 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20  ags&MEM_Str) && 
1a969 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45  (pRec->flags&(ME
1a96a 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29  M_Real|MEM_Int))
1a96b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1a96c 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66  3VdbeMemStringif
1a96d 79 28 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20  y(pRec, enc);.  
1a96e 20 20 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c    }.    pRec->fl
1a96f 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61  ags &= ~(MEM_Rea
1a970 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65  l|MEM_Int);.  }e
1a971 6c 73 65 20 69 66 28 20 61 66 66 69 6e 69 74 79  lse if( affinity
1a972 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  !=SQLITE_AFF_NON
1a973 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  E ){.    assert(
1a974 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
1a975 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c  E_AFF_INTEGER ||
1a976 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
1a977 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20  E_AFF_REAL.     
1a978 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e          || affin
1a979 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
1a97a 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61  NUMERIC );.    a
1a97b 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
1a97c 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69  ity(pRec);.    i
1a97d 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26  f( pRec->flags &
1a97e 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20   MEM_Real ){.   
1a97f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e     sqlite3VdbeIn
1a980 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 52  tegerAffinity(pR
1a981 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ec);.    }.  }.}
1a982 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63  ../*.** Try to c
1a983 6f 6e 76 65 72 74 20 74 68 65 20 74 79 70 65 20  onvert the type 
1a984 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72  of a function ar
1a985 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75  gument or a resu
1a986 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74  lt column.** int
1a987 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72  o a numeric repr
1a988 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65  esentation.  Use
1a989 20 65 69 74 68 65 72 20 49 4e 54 45 47 45 52 20   either INTEGER 
1a98a 6f 72 20 52 45 41 4c 20 77 68 69 63 68 65 76 65  or REAL whicheve
1a98b 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69  r.** is appropri
1a98c 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64  ate.  But only d
1a98d 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  o the conversion
1a98e 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62   if it is possib
1a98f 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f  le without.** lo
1a990 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f  ss of informatio
1a991 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  n and return the
1a992 20 72 65 76 69 73 65 64 20 74 79 70 65 20 6f 66   revised type of
1a993 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   the argument..*
1a994 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20  *.** This is an 
1a995 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 61 70 69  EXPERIMENTAL api
1a996 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20   and is subject 
1a997 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 72 65 6d  to change or rem
1a998 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oval..*/.SQLITE_
1a999 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1a99a 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
1a99b 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  pe(sqlite3_value
1a99c 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a   *pVal){.  Mem *
1a99d 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61  pMem = (Mem*)pVa
1a99e 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69  l;.  applyNumeri
1a99f 63 41 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b  cAffinity(pMem);
1a9a0 0a 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f  .  storeTypeInfo
1a9a1 28 70 4d 65 6d 2c 20 30 29 3b 0a 20 20 72 65 74  (pMem, 0);.  ret
1a9a2 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 3b 0a  urn pMem->type;.
1a9a3 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72 74 65  }../*.** Exporte
1a9a4 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 70 70  d version of app
1a9a5 6c 79 41 66 66 69 6e 69 74 79 28 29 2e 20 54 68  lyAffinity(). Th
1a9a6 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20 6f 6e 20  is one works on 
1a9a7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20  sqlite3_value*, 
1a9a8 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69 6e 74 65  .** not the inte
1a9a9 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70 65 2e 0a  rnal Mem* type..
1a9aa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a9ab 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
1a9ac 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79  lueApplyAffinity
1a9ad 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  (.  sqlite3_valu
1a9ae 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75 38 20 61  e *pVal, .  u8 a
1a9af 66 66 69 6e 69 74 79 2c 20 0a 20 20 75 38 20 65  ffinity, .  u8 e
1a9b0 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79 41 66 66  nc.){.  applyAff
1a9b1 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29 70 56 61  inity((Mem *)pVa
1a9b2 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63  l, affinity, enc
1a9b3 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  );.}..#ifdef SQL
1a9b4 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
1a9b5 57 72 69 74 65 20 61 20 6e 69 63 65 20 73 74 72  Write a nice str
1a9b6 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
1a9b7 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e  on of the conten
1a9b8 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d 65 6d 0a  ts of cell pMem.
1a9b9 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65 72 20 7a  ** into buffer z
1a9ba 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e 42 75 66  Buf, length nBuf
1a9bb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a9bc 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a9bd 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69  VdbeMemPrettyPri
1a9be 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68  nt(Mem *pMem, ch
1a9bf 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 63 68 61  ar *zBuf){.  cha
1a9c0 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75 66 3b 0a  r *zCsr = zBuf;.
1a9c1 20 20 69 6e 74 20 66 20 3d 20 70 4d 65 6d 2d 3e    int f = pMem->
1a9c2 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61 74 69 63  flags;..  static
1a9c3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
1a9c4 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20  st encnames[] = 
1a9c5 7b 22 28 58 29 22 2c 20 22 28 38 29 22 2c 20 22  {"(X)", "(8)", "
1a9c6 28 31 36 4c 45 29 22 2c 20 22 28 31 36 42 45 29  (16LE)", "(16BE)
1a9c7 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26 4d 45 4d  "};..  if( f&MEM
1a9c8 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20 69 6e 74  _Blob ){.    int
1a9c9 20 69 3b 0a 20 20 20 20 63 68 61 72 20 63 3b 0a   i;.    char c;.
1a9ca 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f      if( f & MEM_
1a9cb 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 63 20 3d  Dyn ){.      c =
1a9cc 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 65   'z';.      asse
1a9cd 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74  rt( (f & (MEM_St
1a9ce 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29  atic|MEM_Ephem))
1a9cf 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ==0 );.    }else
1a9d0 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61   if( f & MEM_Sta
1a9d1 74 69 63 20 29 7b 0a 20 20 20 20 20 20 63 20 3d  tic ){.      c =
1a9d2 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65   't';.      asse
1a9d3 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79  rt( (f & (MEM_Dy
1a9d4 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30  n|MEM_Ephem))==0
1a9d5 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   );.    }else if
1a9d6 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20  ( f & MEM_Ephem 
1a9d7 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 65 27  ){.      c = 'e'
1a9d8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a9d9 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63  (f & (MEM_Static
1a9da 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b  |MEM_Dyn))==0 );
1a9db 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1a9dc 20 20 63 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d    c = 's';.    }
1a9dd 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
1a9de 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
1a9df 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20 20 20 20  , "%c", c);.    
1a9e0 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53  zCsr += sqlite3S
1a9e1 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20  trlen30(zCsr);. 
1a9e2 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1a9e3 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22  ntf(100, zCsr, "
1a9e4 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a  %d[", pMem->n);.
1a9e5 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69      zCsr += sqli
1a9e6 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72  te3Strlen30(zCsr
1a9e7 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
1a9e8 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e  i<16 && i<pMem->
1a9e9 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  n; i++){.      s
1a9ea 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a9eb 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30 32 58  100, zCsr, "%02X
1a9ec 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e 7a  ", ((int)pMem->z
1a9ed 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a 20 20  [i] & 0xFF));.  
1a9ee 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69      zCsr += sqli
1a9ef 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72  te3Strlen30(zCsr
1a9f0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
1a9f1 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c  (i=0; i<16 && i<
1a9f2 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20  pMem->n; i++){. 
1a9f3 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d       char z = pM
1a9f4 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20  em->z[i];.      
1a9f5 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32  if( z<32 || z>12
1a9f6 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e  6 ) *zCsr++ = '.
1a9f7 27 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a  ';.      else *z
1a9f8 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d  Csr++ = z;.    }
1a9f9 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
1a9fa 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
1a9fb 2c 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65  , "]%s", encname
1a9fc 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20  s[pMem->enc]);. 
1a9fd 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74     zCsr += sqlit
1a9fe 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29  e3Strlen30(zCsr)
1a9ff 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45  ;.    if( f & ME
1aa00 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
1aa01 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1aa02 28 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 64 7a  (100, zCsr,"+%dz
1aa03 22 2c 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 29  ",pMem->u.nZero)
1aa04 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20  ;.      zCsr += 
1aa05 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1aa06 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  zCsr);.    }.   
1aa07 20 2a 7a 43 73 72 20 3d 20 27 5c 30 27 3b 0a 20   *zCsr = '\0';. 
1aa08 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d   }else if( f & M
1aa09 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 6e  EM_Str ){.    in
1aa0a 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 7a 42 75 66  t j, k;.    zBuf
1aa0b 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 69  [0] = ' ';.    i
1aa0c 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29  f( f & MEM_Dyn )
1aa0d 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20  {.      zBuf[1] 
1aa0e 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73  = 'z';.      ass
1aa0f 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53  ert( (f & (MEM_S
1aa10 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29  tatic|MEM_Ephem)
1aa11 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73  )==0 );.    }els
1aa12 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74  e if( f & MEM_St
1aa13 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 7a 42  atic ){.      zB
1aa14 75 66 5b 31 5d 20 3d 20 27 74 27 3b 0a 20 20 20  uf[1] = 't';.   
1aa15 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1aa16 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68  (MEM_Dyn|MEM_Eph
1aa17 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  em))==0 );.    }
1aa18 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
1aa19 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20  _Ephem ){.      
1aa1a 7a 42 75 66 5b 31 5d 20 3d 20 27 65 27 3b 0a 20  zBuf[1] = 'e';. 
1aa1b 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20       assert( (f 
1aa1c 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45  & (MEM_Static|ME
1aa1d 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20  M_Dyn))==0 );.  
1aa1e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
1aa1f 42 75 66 5b 31 5d 20 3d 20 27 73 27 3b 0a 20 20  Buf[1] = 's';.  
1aa20 20 20 7d 0a 20 20 20 20 6b 20 3d 20 32 3b 0a 20    }.    k = 2;. 
1aa21 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1aa22 6e 74 66 28 31 30 30 2c 20 26 7a 42 75 66 5b 6b  ntf(100, &zBuf[k
1aa23 5d 2c 20 22 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e  ], "%d", pMem->n
1aa24 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69  );.    k += sqli
1aa25 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75  te3Strlen30(&zBu
1aa26 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b  f[k]);.    zBuf[
1aa27 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20 20 20  k++] = '[';.    
1aa28 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 20 26 26  for(j=0; j<15 &&
1aa29 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29   j<pMem->n; j++)
1aa2a 7b 0a 20 20 20 20 20 20 75 38 20 63 20 3d 20 70  {.      u8 c = p
1aa2b 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20 20 20  Mem->z[j];.     
1aa2c 20 69 66 28 20 63 3e 3d 30 78 32 30 20 26 26 20   if( c>=0x20 && 
1aa2d 63 3c 30 78 37 66 20 29 7b 0a 20 20 20 20 20 20  c<0x7f ){.      
1aa2e 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 63 3b    zBuf[k++] = c;
1aa2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1aa30 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20        zBuf[k++] 
1aa31 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 7d 0a 20  = '.';.      }. 
1aa32 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6b 2b     }.    zBuf[k+
1aa33 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20 20 73 71  +] = ']';.    sq
1aa34 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
1aa35 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65 6e 63  00,&zBuf[k], enc
1aa36 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d  names[pMem->enc]
1aa37 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69  );.    k += sqli
1aa38 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75  te3Strlen30(&zBu
1aa39 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b  f[k]);.    zBuf[
1aa3a 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  k++] = 0;.  }.}.
1aa3b 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
1aa3c 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a  QLITE_DEBUG./*.*
1aa3d 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61 6c 75  * Print the valu
1aa3e 65 20 6f 66 20 61 20 72 65 67 69 73 74 65 72 20  e of a register 
1aa3f 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75 72 70  for tracing purp
1aa40 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 63 20  oses:.*/.static 
1aa41 76 6f 69 64 20 6d 65 6d 54 72 61 63 65 50 72 69  void memTracePri
1aa42 6e 74 28 46 49 4c 45 20 2a 6f 75 74 2c 20 4d 65  nt(FILE *out, Me
1aa43 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  m *p){.  if( p->
1aa44 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1aa45 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28   ){.    fprintf(
1aa46 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 29 3b 0a 20  out, " NULL");. 
1aa47 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d 3e 66   }else if( (p->f
1aa48 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c  lags & (MEM_Int|
1aa49 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 4d 5f  MEM_Str))==(MEM_
1aa4a 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a  Int|MEM_Str) ){.
1aa4b 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
1aa4c 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e   " si:%lld", p->
1aa4d 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  u.i);.  }else if
1aa4e 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
1aa4f 5f 49 6e 74 20 29 7b 0a 20 20 20 20 66 70 72 69  _Int ){.    fpri
1aa50 6e 74 66 28 6f 75 74 2c 20 22 20 69 3a 25 6c 6c  ntf(out, " i:%ll
1aa51 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d  d", p->u.i);.  }
1aa52 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67  else if( p->flag
1aa53 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a  s & MEM_Real ){.
1aa54 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
1aa55 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72 29 3b   " r:%g", p->r);
1aa56 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68  .  }else{.    ch
1aa57 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a 20 20  ar zBuf[200];.  
1aa58 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1aa59 50 72 65 74 74 79 50 72 69 6e 74 28 70 2c 20 7a  PrettyPrint(p, z
1aa5a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  Buf);.    fprint
1aa5b 66 28 6f 75 74 2c 20 22 20 22 29 3b 0a 20 20 20  f(out, " ");.   
1aa5c 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
1aa5d 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 7d  s", zBuf);.  }.}
1aa5e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 67  .static void reg
1aa5f 69 73 74 65 72 54 72 61 63 65 28 46 49 4c 45 20  isterTrace(FILE 
1aa60 2a 6f 75 74 2c 20 69 6e 74 20 69 52 65 67 2c 20  *out, int iReg, 
1aa61 4d 65 6d 20 2a 70 29 7b 0a 20 20 66 70 72 69 6e  Mem *p){.  fprin
1aa62 74 66 28 6f 75 74 2c 20 22 52 45 47 5b 25 64 5d  tf(out, "REG[%d]
1aa63 20 3d 20 22 2c 20 69 52 65 67 29 3b 0a 20 20 6d   = ", iReg);.  m
1aa64 65 6d 54 72 61 63 65 50 72 69 6e 74 28 6f 75 74  emTracePrint(out
1aa65 2c 20 70 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  , p);.  fprintf(
1aa66 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 7d 0a 23 65  out, "\n");.}.#e
1aa67 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
1aa68 49 54 45 5f 44 45 42 55 47 0a 23 20 20 64 65 66  ITE_DEBUG.#  def
1aa69 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52 41  ine REGISTER_TRA
1aa6a 43 45 28 52 2c 4d 29 20 69 66 28 70 2d 3e 74 72  CE(R,M) if(p->tr
1aa6b 61 63 65 29 72 65 67 69 73 74 65 72 54 72 61 63  ace)registerTrac
1aa6c 65 28 70 2d 3e 74 72 61 63 65 2c 52 2c 4d 29 0a  e(p->trace,R,M).
1aa6d 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20  #else.#  define 
1aa6e 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 52  REGISTER_TRACE(R
1aa6f 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66  ,M).#endif...#if
1aa70 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45  def VDBE_PROFILE
1aa71 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e  ../* .** hwtime.
1aa72 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  h contains inlin
1aa73 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65  e assembler code
1aa74 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e   for implementin
1aa75 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f  g .** high-perfo
1aa76 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f  rmance timing ro
1aa77 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  utines..*/./****
1aa78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
1aa79 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74  de hwtime.h in t
1aa7a 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 64 62  he middle of vdb
1aa7b 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1aa7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1aa7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1aa7e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a   file hwtime.h *
1aa7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1aa82 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a   2008 May 27.**.
1aa83 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1aa84 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1aa85 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1aa86 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1aa87 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1aa88 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1aa89 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1aa8a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1aa8b 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1aa8c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1aa8d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1aa8e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1aa8f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1aa90 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1aa91 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1aa92 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1aa93 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1aa94 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1aa95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aa99 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
1aa9a 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
1aa9b 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65   asm code for re
1aa9c 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70  trieving "high-p
1aa9d 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63  erformance".** c
1aa9e 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20  ounters for x86 
1aa9f 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a  class CPUs..**.*
1aaa0 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c  * $Id: hwtime.h,
1aaa1 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31  v 1.3 2008/08/01
1aaa2 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20   14:33:15 shane 
1aaa3 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
1aaa4 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66   _HWTIME_H_.#def
1aaa5 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a  ine _HWTIME_H_..
1aaa6 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
1aaa7 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  ing routine only
1aaa8 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75   works on pentiu
1aaa9 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65  m-class (or newe
1aaaa 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a  r) processors..*
1aaab 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44  * It uses the RD
1aaac 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65  TSC opcode to re
1aaad 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75  ad the cycle cou
1aaae 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20  nt value out of 
1aaaf 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72  the.** processor
1aab0 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61   and returns tha
1aab1 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63  t value.  This c
1aab2 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68  an be used for h
1aab3 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69  igh-res.** profi
1aab4 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65  ling..*/.#if (de
1aab5 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
1aab6 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  || defined(_MSC_
1aab7 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20  VER)) && \.     
1aab8 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20   (defined(i386) 
1aab9 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38  || defined(__i38
1aaba 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  6__) || defined(
1aabb 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66  _M_IX86))..  #if
1aabc 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f   defined(__GNUC_
1aabd 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  _)..  __inline__
1aabe 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
1aabf 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
1aac0 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65  d){.     unsigne
1aac1 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20  d int lo, hi;.  
1aac2 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
1aac3 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
1aac4 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d   : "=a" (lo), "=
1aac5 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72  d" (hi));.     r
1aac6 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69  eturn (sqlite_ui
1aac7 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20  nt64)hi << 32 | 
1aac8 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66  lo;.  }..  #elif
1aac9 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
1aaca 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63  R)..  __declspec
1aacb 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65  (naked) __inline
1aacc 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f   sqlite_uint64 _
1aacd 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77  _cdecl sqlite3Hw
1aace 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
1aacf 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20   __asm {.       
1aad0 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72   rdtsc.        r
1aad1 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72  et       ; retur
1aad2 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45  n value at EDX:E
1aad3 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  AX.     }.  }.. 
1aad4 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28   #endif..#elif (
1aad5 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
1aad6 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78  ) && defined(__x
1aad7 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69  86_64__))..  __i
1aad8 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
1aad9 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
1aada 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
1aadb 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76   unsigned long v
1aadc 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  al;.      __asm_
1aadd 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
1aade 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28  "rdtsc" : "=A" (
1aadf 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74  val));.      ret
1aae0 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23  urn val;.  }. .#
1aae1 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
1aae2 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
1aae3 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20  ed(__ppc__))..  
1aae4 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
1aae5 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
1aae6 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
1aae7 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
1aae8 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20  g long retval;. 
1aae9 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
1aaea 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f  ng junk;.      _
1aaeb 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
1aaec 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20  e__ ("\n\.      
1aaed 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62      1:      mftb
1aaee 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20  u   %1\n\.      
1aaef 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
1aaf0 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20      %L0\n\.     
1aaf1 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74               mft
1aaf2 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20  bu   %0\n\.     
1aaf3 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70               cmp
1aaf4 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20  w    %0,%1\n\.  
1aaf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aaf6 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20  bne     1b".    
1aaf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20                : 
1aaf8 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22  "=r" (retval), "
1aaf9 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20  =r" (junk));.   
1aafa 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c     return retval
1aafb 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20  ;.  }..#else..  
1aafc 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c  #error Need impl
1aafd 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
1aafe 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
1aaff 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e  r your platform.
1ab00 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63  ..  /*.  ** To c
1ab01 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69  ompile without i
1ab02 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69  mplementing sqli
1ab03 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
1ab04 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20  your platform,. 
1ab05 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f   ** you can remo
1ab06 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72  ve the above #er
1ab07 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20  ror and use the 
1ab08 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73  following.  ** s
1ab09 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59  tub function.  Y
1ab0a 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d  ou will lose tim
1ab0b 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20  ing support for 
1ab0c 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65  many.  ** of the
1ab0d 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74   debugging and t
1ab0e 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73  esting utilities
1ab0f 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20  , but it should 
1ab10 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f  at.  ** least co
1ab11 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20  mpile and run.. 
1ab12 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
1ab13 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74  TE   sqlite_uint
1ab14 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
1ab15 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28  (void){ return (
1ab16 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
1ab17 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65  ); }..#endif..#e
1ab18 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
1ab19 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a  (_HWTIME_H_) */.
1ab1a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1ab1b 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68   End of hwtime.h
1ab1c 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1ab1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ab1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1ab1f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1ab20 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
1ab21 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
1ab22 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.c ********
1ab23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1ab24 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ..#endif../*.** 
1ab25 54 68 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e  The CHECK_FOR_IN
1ab26 54 45 52 52 55 50 54 20 6d 61 63 72 6f 20 64 65  TERRUPT macro de
1ab27 66 69 6e 65 64 20 68 65 72 65 20 6c 6f 6f 6b 73  fined here looks
1ab28 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 0a 2a   to see if the.*
1ab29 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  * sqlite3_interr
1ab2a 75 70 74 28 29 20 72 6f 75 74 69 6e 65 20 68 61  upt() routine ha
1ab2b 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 20 20  s been called.  
1ab2c 49 66 20 69 74 20 68 61 73 20 62 65 65 6e 2c 20  If it has been, 
1ab2d 74 68 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 69  then.** processi
1ab2e 6e 67 20 6f 66 20 74 68 65 20 56 44 42 45 20 70  ng of the VDBE p
1ab2f 72 6f 67 72 61 6d 20 69 73 20 69 6e 74 65 72 72  rogram is interr
1ab30 75 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  upted..**.** Thi
1ab31 73 20 6d 61 63 72 6f 20 61 64 64 65 64 20 74 6f  s macro added to
1ab32 20 65 76 65 72 79 20 69 6e 73 74 72 75 63 74 69   every instructi
1ab33 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 61 20 6a  on that does a j
1ab34 75 6d 70 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a  ump in order to.
1ab35 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6c  ** implement a l
1ab36 6f 6f 70 2e 20 20 54 68 69 73 20 74 65 73 74 20  oop.  This test 
1ab37 75 73 65 64 20 74 6f 20 62 65 20 6f 6e 20 65 76  used to be on ev
1ab38 65 72 79 20 73 69 6e 67 6c 65 20 69 6e 73 74 72  ery single instr
1ab39 75 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 74  uction,.** but t
1ab3a 68 61 74 20 6d 65 61 6e 74 20 77 65 20 6d 6f 72  hat meant we mor
1ab3b 65 20 74 65 73 74 69 6e 67 20 74 68 61 74 20 77  e testing that w
1ab3c 65 20 6e 65 65 64 65 64 2e 20 20 42 79 20 6f 6e  e needed.  By on
1ab3d 6c 79 20 74 65 73 74 69 6e 67 20 74 68 65 0a 2a  ly testing the.*
1ab3e 2a 20 66 6c 61 67 20 6f 6e 20 6a 75 6d 70 20 69  * flag on jump i
1ab3f 6e 73 74 72 75 63 74 69 6f 6e 73 2c 20 77 65 20  nstructions, we 
1ab40 67 65 74 20 61 20 28 73 6d 61 6c 6c 29 20 73 70  get a (small) sp
1ab41 65 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e  eed improvement.
1ab42 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43  .*/.#define CHEC
1ab43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20  K_FOR_INTERRUPT 
1ab44 5c 0a 20 20 20 69 66 28 20 64 62 2d 3e 75 31 2e  \.   if( db->u1.
1ab45 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 20  isInterrupted ) 
1ab46 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1ab47 6f 5f 69 6e 74 65 72 72 75 70 74 3b 0a 0a 23 69  o_interrupt;..#i
1ab48 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1ab49 47 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c  G.static int fil
1ab4a 65 45 78 69 73 74 73 28 73 71 6c 69 74 65 33 20  eExists(sqlite3 
1ab4b 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
1ab4c 2a 7a 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72  *zFile){.  int r
1ab4d 65 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  es = 0;.  int rc
1ab4e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69   = SQLITE_OK;.#i
1ab4f 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1ab50 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
1ab51 63 75 72 72 65 6e 74 6c 79 20 74 65 73 74 69 6e  currently testin
1ab52 67 20 49 4f 20 65 72 72 6f 72 73 2c 20 74 68 65  g IO errors, the
1ab53 6e 20 64 6f 20 6e 6f 74 20 63 61 6c 6c 20 4f 73  n do not call Os
1ab54 41 63 63 65 73 73 28 29 20 74 6f 0a 20 20 2a 2a  Access() to.  **
1ab55 20 74 65 73 74 20 66 6f 72 20 74 68 65 20 70 72   test for the pr
1ab56 65 73 65 6e 63 65 20 6f 66 20 7a 46 69 6c 65 2e  esence of zFile.
1ab57 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
1ab58 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 74 68   any IO error th
1ab59 61 74 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20 68  at.  ** occurs h
1ab5a 65 72 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  ere will not be 
1ab5b 72 65 70 6f 72 74 65 64 2c 20 63 61 75 73 69 6e  reported, causin
1ab5c 67 20 74 68 65 20 74 65 73 74 20 74 6f 20 66 61  g the test to fa
1ab5d 69 6c 2e 0a 20 20 2a 2f 0a 20 20 65 78 74 65 72  il..  */.  exter
1ab5e 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  n int sqlite3_io
1ab5f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a  _error_pending;.
1ab60 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6f    if( sqlite3_io
1ab61 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3c 3d  _error_pending<=
1ab62 30 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 72  0 ).#endif.    r
1ab63 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63  c = sqlite3OsAcc
1ab64 65 73 73 28 64 62 2d 3e 70 56 66 73 2c 20 7a 46  ess(db->pVfs, zF
1ab65 69 6c 65 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  ile, SQLITE_ACCE
1ab66 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29  SS_EXISTS, &res)
1ab67 3b 0a 20 20 72 65 74 75 72 6e 20 28 72 65 73 20  ;.  return (res 
1ab68 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
1ab69 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  );.}.#endif..#if
1ab6a 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a  ndef NDEBUG./*.*
1ab6b 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1ab6c 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66  is only called f
1ab6d 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 61 73  rom within an as
1ab6e 73 65 72 74 28 29 20 65 78 70 72 65 73 73 69 6f  sert() expressio
1ab6f 6e 2e 20 49 74 0a 2a 2a 20 63 68 65 63 6b 73 20  n. It.** checks 
1ab70 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33  that the sqlite3
1ab71 2e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 76 61  .nTransaction va
1ab72 72 69 61 62 6c 65 20 69 73 20 63 6f 72 72 65 63  riable is correc
1ab73 74 6c 79 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68  tly set to.** th
1ab74 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d  e number of non-
1ab75 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65  transaction save
1ab76 70 6f 69 6e 74 73 20 63 75 72 72 65 6e 74 6c 79  points currently
1ab77 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b   in the .** link
1ab78 65 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67  ed list starting
1ab79 20 61 74 20 73 71 6c 69 74 65 33 2e 70 53 61 76   at sqlite3.pSav
1ab7a 65 70 6f 69 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 55  epoint..** .** U
1ab7b 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  sage:.**.**     
1ab7c 61 73 73 65 72 74 28 20 63 68 65 63 6b 53 61 76  assert( checkSav
1ab7d 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20  epointCount(db) 
1ab7e 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  );.*/.static int
1ab7f 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43   checkSavepointC
1ab80 6f 75 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62  ount(sqlite3 *db
1ab81 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a  ){.  int n = 0;.
1ab82 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 3b 0a    Savepoint *p;.
1ab83 20 20 66 6f 72 28 70 3d 64 62 2d 3e 70 53 61 76    for(p=db->pSav
1ab84 65 70 6f 69 6e 74 3b 20 70 3b 20 70 3d 70 2d 3e  epoint; p; p=p->
1ab85 70 4e 65 78 74 29 20 6e 2b 2b 3b 0a 20 20 61 73  pNext) n++;.  as
1ab86 73 65 72 74 28 20 6e 3d 3d 28 64 62 2d 3e 6e 53  sert( n==(db->nS
1ab87 61 76 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 69  avepoint + db->i
1ab88 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65  sTransactionSave
1ab89 70 6f 69 6e 74 29 20 29 3b 0a 20 20 72 65 74 75  point) );.  retu
1ab8a 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 1;.}.#endif..
1ab8b 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 73  /*.** Execute as
1ab8c 20 6d 75 63 68 20 6f 66 20 61 20 56 44 42 45 20   much of a VDBE 
1ab8d 70 72 6f 67 72 61 6d 20 61 73 20 77 65 20 63 61  program as we ca
1ab8e 6e 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 2a  n then return..*
1ab8f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  *.** sqlite3Vdbe
1ab90 4d 61 6b 65 52 65 61 64 79 28 29 20 6d 75 73 74  MakeReady() must
1ab91 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72   be called befor
1ab92 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
1ab93 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6c  n order to.** cl
1ab94 6f 73 65 20 74 68 65 20 70 72 6f 67 72 61 6d 20  ose the program 
1ab95 77 69 74 68 20 61 20 66 69 6e 61 6c 20 4f 50 5f  with a final OP_
1ab96 48 61 6c 74 20 61 6e 64 20 74 6f 20 73 65 74 20  Halt and to set 
1ab97 75 70 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73  up the callbacks
1ab98 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 72 72 6f  .** and the erro
1ab99 72 20 6d 65 73 73 61 67 65 20 70 6f 69 6e 74 65  r message pointe
1ab9a 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65 76 65  r..**.** Wheneve
1ab9b 72 20 61 20 72 6f 77 20 6f 72 20 72 65 73 75 6c  r a row or resul
1ab9c 74 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61  t data is availa
1ab9d 62 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ble, this routin
1ab9e 65 20 77 69 6c 6c 20 65 69 74 68 65 72 0a 2a 2a  e will either.**
1ab9f 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 65 73 75   invoke the resu
1aba0 6c 74 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20  lt callback (if 
1aba1 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72  there is one) or
1aba2 20 72 65 74 75 72 6e 20 77 69 74 68 0a 2a 2a 20   return with.** 
1aba3 53 51 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a  SQLITE_ROW..**.*
1aba4 2a 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20  * If an attempt 
1aba5 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20  is made to open 
1aba6 61 20 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73  a locked databas
1aba7 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  e, then this rou
1aba8 74 69 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65 69 74  tine.** will eit
1aba9 68 65 72 20 69 6e 76 6f 6b 65 20 74 68 65 20 62  her invoke the b
1abaa 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 28 69 66  usy callback (if
1abab 20 74 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f   there is one) o
1abac 72 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 72 65 74  r it will.** ret
1abad 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  urn SQLITE_BUSY.
1abae 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
1abaf 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72  or occurs, an er
1abb0 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 77  ror message is w
1abb1 72 69 74 74 65 6e 20 74 6f 20 6d 65 6d 6f 72 79  ritten to memory
1abb2 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
1abb3 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
1abb4 28 29 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d 73  () and p->zErrMs
1abb5 67 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69  g is made to poi
1abb6 6e 74 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f 72  nt to that memor
1abb7 79 2e 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20  y..** The error 
1abb8 63 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20 69  code is stored i
1abb9 6e 20 70 2d 3e 72 63 20 61 6e 64 20 74 68 69 73  n p->rc and this
1abba 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
1abbb 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a   SQLITE_ERROR..*
1abbc 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c  *.** If the call
1abbd 62 61 63 6b 20 65 76 65 72 20 72 65 74 75 72 6e  back ever return
1abbe 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1abbf 20 74 68 65 20 70 72 6f 67 72 61 6d 20 65 78 69   the program exi
1abc0 74 73 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  ts.** immediatel
1abc1 79 2e 20 20 54 68 65 72 65 20 77 69 6c 6c 20 62  y.  There will b
1abc2 65 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61  e no error messa
1abc3 67 65 20 62 75 74 20 74 68 65 20 70 2d 3e 72 63  ge but the p->rc
1abc4 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74   field is.** set
1abc5 20 74 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54   to SQLITE_ABORT
1abc6 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
1abc7 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51  e will return SQ
1abc8 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a  LITE_ERROR..**.*
1abc9 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  * A memory alloc
1abca 61 74 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73  ation error caus
1abcb 65 73 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73  es p->rc to be s
1abcc 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d  et to SQLITE_NOM
1abcd 45 4d 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72  EM and this.** r
1abce 6f 75 74 69 6e 65 20 74 6f 20 72 65 74 75 72 6e  outine to return
1abcf 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a   SQLITE_ERROR..*
1abd0 2a 0a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61 6c  *.** Other fatal
1abd1 20 65 72 72 6f 72 73 20 72 65 74 75 72 6e 20 53   errors return S
1abd2 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a  QLITE_ERROR..**.
1abd3 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  ** After this ro
1abd4 75 74 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68  utine has finish
1abd5 65 64 2c 20 73 71 6c 69 74 65 33 56 64 62 65 46  ed, sqlite3VdbeF
1abd6 69 6e 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64  inalize() should
1abd7 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63   be.** used to c
1abd8 6c 65 61 6e 20 75 70 20 74 68 65 20 6d 65 73 73  lean up the mess
1abd9 20 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 62   that was left b
1abda 65 68 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ehind..*/.SQLITE
1abdb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1abdc 69 74 65 33 56 64 62 65 45 78 65 63 28 0a 20 20  ite3VdbeExec(.  
1abdd 56 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20  Vdbe *p         
1abde 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1abdf 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69  e VDBE */.){.  i
1abe0 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  nt pc;          
1abe1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1abe2 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   program counter
1abe3 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20   */.  Op *pOp;  
1abe4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1abe5 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72   /* Current oper
1abe6 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72  ation */.  int r
1abe7 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
1abe8 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
1abe9 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71  o return */.  sq
1abea 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
1abeb 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  b;       /* The 
1abec 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38  database */.  u8
1abed 20 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28   encoding = ENC(
1abee 64 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  db);     /* The 
1abef 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e  database encodin
1abf0 67 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31  g */.  Mem *pIn1
1abf1 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1abf2 20 20 2f 2a 20 31 73 74 20 69 6e 70 75 74 20 6f    /* 1st input o
1abf3 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20  perand */.  Mem 
1abf4 2a 70 49 6e 32 20 3d 20 30 3b 20 20 20 20 20 20  *pIn2 = 0;      
1abf5 20 20 20 20 20 20 20 2f 2a 20 32 6e 64 20 69 6e         /* 2nd in
1abf6 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  put operand */. 
1abf7 20 4d 65 6d 20 2a 70 49 6e 33 20 3d 20 30 3b 20   Mem *pIn3 = 0; 
1abf8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33              /* 3
1abf9 72 64 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64  rd input operand
1abfa 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20   */.  Mem *pOut 
1abfb 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1abfc 20 2f 2a 20 4f 75 74 70 75 74 20 6f 70 65 72 61   /* Output opera
1abfd 6e 64 20 2a 2f 0a 20 20 75 38 20 6f 70 50 72 6f  nd */.  u8 opPro
1abfe 70 65 72 74 79 3b 0a 20 20 69 6e 74 20 69 43 6f  perty;.  int iCo
1abff 6d 70 61 72 65 20 3d 20 30 3b 20 20 20 20 20 20  mpare = 0;      
1ac00 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66      /* Result of
1ac01 20 6c 61 73 74 20 4f 50 5f 43 6f 6d 70 61 72 65   last OP_Compare
1ac02 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
1ac03 69 6e 74 20 2a 61 50 65 72 6d 75 74 65 20 3d 20  int *aPermute = 
1ac04 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 65  0;         /* Pe
1ac05 72 6d 75 74 61 74 69 6f 6e 20 6f 66 20 63 6f 6c  rmutation of col
1ac06 75 6d 6e 73 20 66 6f 72 20 4f 50 5f 43 6f 6d 70  umns for OP_Comp
1ac07 61 72 65 20 2a 2f 0a 23 69 66 64 65 66 20 56 44  are */.#ifdef VD
1ac08 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 75 36 34  BE_PROFILE.  u64
1ac09 20 73 74 61 72 74 3b 20 20 20 20 20 20 20 20 20   start;         
1ac0a 20 20 20 20 20 20 20 20 2f 2a 20 43 50 55 20 63          /* CPU c
1ac0b 6c 6f 63 6b 20 63 6f 75 6e 74 20 61 74 20 73 74  lock count at st
1ac0c 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f  art of opcode */
1ac0d 0a 20 20 69 6e 74 20 6f 72 69 67 50 63 3b 20 20  .  int origPc;  
1ac0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ac0f 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72   Program counter
1ac10 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63   at start of opc
1ac11 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ode */.#endif.#i
1ac12 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1ac13 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
1ac14 41 43 4b 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72  ACK.  int nProgr
1ac15 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20 20 20  essOps = 0;     
1ac16 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78 65 63   /* Opcodes exec
1ac17 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f 67 72  uted since progr
1ac18 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f  ess callback. */
1ac19 0a 23 65 6e 64 69 66 0a 20 20 55 6e 70 61 63 6b  .#endif.  Unpack
1ac1a 65 64 52 65 63 6f 72 64 20 61 54 65 6d 70 52 65  edRecord aTempRe
1ac1b 63 5b 31 36 5d 3b 20 2f 2a 20 53 70 61 63 65 20  c[16]; /* Space 
1ac1c 74 6f 20 68 6f 6c 64 20 61 20 74 72 61 6e 73 69  to hold a transi
1ac1d 65 6e 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  ent UnpackedReco
1ac1e 72 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  rd */..  assert(
1ac1f 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
1ac20 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20 20 2f 2a  MAGIC_RUN );  /*
1ac21 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
1ac22 76 65 72 69 66 69 65 73 20 74 68 69 73 20 2a 2f  verifies this */
1ac23 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d  .  assert( db->m
1ac24 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47  agic==SQLITE_MAG
1ac25 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 73 71 6c  IC_BUSY );.  sql
1ac26 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
1ac27 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75  rayEnter(&p->aMu
1ac28 74 65 78 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72  tex);.  if( p->r
1ac29 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
1ac2a 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68  ){.    /* This h
1ac2b 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c  appens if a mall
1ac2c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 61  oc() inside a ca
1ac2d 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ll to sqlite3_co
1ac2e 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20  lumn_text() or. 
1ac2f 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f     ** sqlite3_co
1ac30 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61  lumn_text16() fa
1ac31 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f  iled.  */.    go
1ac32 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
1ac33 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d   assert( p->rc==
1ac34 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
1ac35 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
1ac36 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  );.  p->rc = SQL
1ac37 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74  ITE_OK;.  assert
1ac38 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20  ( p->explain==0 
1ac39 29 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53  );.  p->pResultS
1ac3a 65 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75  et = 0;.  db->bu
1ac3b 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20  syHandler.nBusy 
1ac3c 3d 20 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52  = 0;.  CHECK_FOR
1ac3d 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71  _INTERRUPT;.  sq
1ac3e 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65  lite3VdbeIOTrace
1ac3f 53 71 6c 28 70 29 3b 0a 23 69 66 64 65 66 20 53  Sql(p);.#ifdef S
1ac40 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71  QLITE_DEBUG.  sq
1ac41 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
1ac42 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20  Malloc();.  if( 
1ac43 70 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20  p->pc==0 .   && 
1ac44 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26  ((p->db->flags &
1ac45 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74   SQLITE_VdbeList
1ac46 69 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73  ing) || fileExis
1ac47 74 73 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70  ts(db, "vdbe_exp
1ac48 6c 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20  lain")).  ){.   
1ac49 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e   int i;.    prin
1ac4a 74 66 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d  tf("VDBE Program
1ac4b 20 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20   Listing:\n");. 
1ac4c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72     sqlite3VdbePr
1ac4d 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66  intSql(p);.    f
1ac4e 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70  or(i=0; i<p->nOp
1ac4f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
1ac50 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70  lite3VdbePrintOp
1ac51 28 73 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d 3e  (stdout, i, &p->
1ac52 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  aOp[i]);.    }. 
1ac53 20 7d 0a 20 20 69 66 28 20 66 69 6c 65 45 78 69   }.  if( fileExi
1ac54 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f 74 72  sts(db, "vdbe_tr
1ac55 61 63 65 22 29 20 29 7b 0a 20 20 20 20 70 2d 3e  ace") ){.    p->
1ac56 74 72 61 63 65 20 3d 20 73 74 64 6f 75 74 3b 0a  trace = stdout;.
1ac57 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64    }.  sqlite3End
1ac58 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
1ac59 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 70 63 3d  #endif.  for(pc=
1ac5a 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49 54  p->pc; rc==SQLIT
1ac5b 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 20 20  E_OK; pc++){.   
1ac5c 20 61 73 73 65 72 74 28 20 70 63 3e 3d 30 20 26   assert( pc>=0 &
1ac5d 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20  & pc<p->nOp );. 
1ac5e 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
1ac5f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e  cFailed ) goto n
1ac60 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56 44  o_mem;.#ifdef VD
1ac61 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 6f  BE_PROFILE.    o
1ac62 72 69 67 50 63 20 3d 20 70 63 3b 0a 20 20 20 20  rigPc = pc;.    
1ac63 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 48  start = sqlite3H
1ac64 77 74 69 6d 65 28 29 3b 0a 23 65 6e 64 69 66 0a  wtime();.#endif.
1ac65 20 20 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f      pOp = &p->aO
1ac66 70 5b 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a 20 4f  p[pc];..    /* O
1ac67 6e 6c 79 20 61 6c 6c 6f 77 20 74 72 61 63 69 6e  nly allow tracin
1ac68 67 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55  g if SQLITE_DEBU
1ac69 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20  G is defined..  
1ac6a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
1ac6b 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28  TE_DEBUG.    if(
1ac6c 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20   p->trace ){.   
1ac6d 20 20 20 69 66 28 20 70 63 3d 3d 30 20 29 7b 0a     if( pc==0 ){.
1ac6e 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 28 22          printf("
1ac6f 56 44 42 45 20 45 78 65 63 75 74 69 6f 6e 20 54  VDBE Execution T
1ac70 72 61 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 20 20  race:\n");.     
1ac71 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72     sqlite3VdbePr
1ac72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20  intSql(p);.     
1ac73 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1ac74 56 64 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e 74  VdbePrintOp(p->t
1ac75 72 61 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b 0a  race, pc, pOp);.
1ac76 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d      }.    if( p-
1ac77 3e 74 72 61 63 65 3d 3d 30 20 26 26 20 70 63 3d  >trace==0 && pc=
1ac78 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1ac79 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
1ac7a 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 69 66  lloc();.      if
1ac7b 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c  ( fileExists(db,
1ac7c 20 22 76 64 62 65 5f 73 71 6c 74 72 61 63 65 22   "vdbe_sqltrace"
1ac7d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
1ac7e 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c  ite3VdbePrintSql
1ac7f 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  (p);.      }.   
1ac80 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e     sqlite3EndBen
1ac81 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20  ignMalloc();.   
1ac82 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
1ac83 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74  ..    /* Check t
1ac84 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 65 64  o see if we need
1ac85 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20   to simulate an 
1ac86 69 6e 74 65 72 72 75 70 74 2e 20 20 54 68 69 73  interrupt.  This
1ac87 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 0a 20 20   only happens.  
1ac88 20 20 2a 2a 20 69 66 20 77 65 20 68 61 76 65 20    ** if we have 
1ac89 61 20 73 70 65 63 69 61 6c 20 74 65 73 74 20 62  a special test b
1ac8a 75 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  uild..    */.#if
1ac8b 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1ac8c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
1ac8d 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3e  interrupt_count>
1ac8e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1ac8f 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75  e3_interrupt_cou
1ac90 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20  nt--;.      if( 
1ac91 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
1ac92 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 20  t_count==0 ){.  
1ac93 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
1ac94 74 65 72 72 75 70 74 28 64 62 29 3b 0a 20 20 20  terrupt(db);.   
1ac95 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
1ac96 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
1ac97 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f  E_OMIT_PROGRESS_
1ac98 43 41 4c 4c 42 41 43 4b 0a 20 20 20 20 2f 2a 20  CALLBACK.    /* 
1ac99 43 61 6c 6c 20 74 68 65 20 70 72 6f 67 72 65 73  Call the progres
1ac9a 73 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 69 74  s callback if it
1ac9b 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 61   is configured a
1ac9c 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20  nd the required 
1ac9d 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f 66  number.    ** of
1ac9e 20 56 44 42 45 20 6f 70 73 20 68 61 76 65 20 62   VDBE ops have b
1ac9f 65 65 6e 20 65 78 65 63 75 74 65 64 20 28 65 69  een executed (ei
1aca0 74 68 65 72 20 73 69 6e 63 65 20 74 68 69 73 20  ther since this 
1aca1 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a 20 20  invocation of.  
1aca2 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65    ** sqlite3Vdbe
1aca3 45 78 65 63 28 29 20 6f 72 20 73 69 6e 63 65 20  Exec() or since 
1aca4 6c 61 73 74 20 74 69 6d 65 20 74 68 65 20 70 72  last time the pr
1aca5 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
1aca6 77 61 73 20 63 61 6c 6c 65 64 29 2e 0a 20 20 20  was called)..   
1aca7 20 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72   ** If the progr
1aca8 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  ess callback ret
1aca9 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 65  urns non-zero, e
1acaa 78 69 74 20 74 68 65 20 76 69 72 74 75 61 6c 20  xit the virtual 
1acab 6d 61 63 68 69 6e 65 20 77 69 74 68 0a 20 20 20  machine with.   
1acac 20 2a 2a 20 61 20 72 65 74 75 72 6e 20 63 6f 64   ** a return cod
1acad 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0a  e SQLITE_ABORT..
1acae 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64      */.    if( d
1acaf 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 29 7b 0a  b->xProgress ){.
1acb0 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 50        if( db->nP
1acb1 72 6f 67 72 65 73 73 4f 70 73 3d 3d 6e 50 72 6f  rogressOps==nPro
1acb2 67 72 65 73 73 4f 70 73 20 29 7b 0a 20 20 20 20  gressOps ){.    
1acb3 20 20 20 20 69 6e 74 20 70 72 63 3b 0a 20 20 20      int prc;.   
1acb4 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1acb5 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
1acb6 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1acb7 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20  o_misuse;.      
1acb8 20 20 70 72 63 20 3d 64 62 2d 3e 78 50 72 6f 67    prc =db->xProg
1acb9 72 65 73 73 28 64 62 2d 3e 70 50 72 6f 67 72 65  ress(db->pProgre
1acba 73 73 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20  ssArg);.        
1acbb 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1acbc 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61  yOn(db) ) goto a
1acbd 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1acbe 73 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  se;.        if( 
1acbf 70 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  prc!=0 ){.      
1acc0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1acc1 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 20  INTERRUPT;.     
1acc2 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 65       goto vdbe_e
1acc3 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 20 20  rror_halt;.     
1acc4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 50 72     }.        nPr
1acc5 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a 20  ogressOps = 0;. 
1acc6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 50 72       }.      nPr
1acc7 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20 20  ogressOps++;.   
1acc8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
1acc9 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75  * Do common setu
1acca 70 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  p processing for
1accb 20 61 6e 79 20 6f 70 63 6f 64 65 20 74 68 61 74   any opcode that
1accc 20 69 73 20 6d 61 72 6b 65 64 0a 20 20 20 20 2a   is marked.    *
1accd 2a 20 77 69 74 68 20 74 68 65 20 22 6f 75 74 32  * with the "out2
1acce 2d 70 72 65 72 65 6c 65 61 73 65 22 20 74 61 67  -prerelease" tag
1accf 2e 20 20 53 75 63 68 20 6f 70 63 6f 64 65 73 20  .  Such opcodes 
1acd0 68 61 76 65 20 61 20 73 69 6e 67 6c 65 0a 20 20  have a single.  
1acd1 20 20 2a 2a 20 6f 75 74 70 75 74 20 77 68 69 63    ** output whic
1acd2 68 20 69 73 20 73 70 65 63 69 66 69 65 64 20 62  h is specified b
1acd3 79 20 74 68 65 20 50 32 20 70 61 72 61 6d 65 74  y the P2 paramet
1acd4 65 72 2e 20 20 54 68 65 20 50 32 20 72 65 67 69  er.  The P2 regi
1acd5 73 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 69  ster.    ** is i
1acd6 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20  nitialized to a 
1acd7 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  NULL..    */.   
1acd8 20 6f 70 50 72 6f 70 65 72 74 79 20 3d 20 6f 70   opProperty = op
1acd9 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 70 4f 70  codeProperty[pOp
1acda 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 69  ->opcode];.    i
1acdb 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26  f( (opProperty &
1acdc 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52   OPFLG_OUT2_PRER
1acdd 45 4c 45 41 53 45 29 21 3d 30 20 29 7b 0a 20 20  ELEASE)!=0 ){.  
1acde 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1acdf 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  >p2>0 );.      a
1ace0 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d  ssert( pOp->p2<=
1ace1 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
1ace2 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
1ace3 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20  [pOp->p2];.     
1ace4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1ace5 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70  eleaseExternal(p
1ace6 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74  Out);.      pOut
1ace7 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  ->flags = MEM_Nu
1ace8 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 20 0a  ll;.    }else. .
1ace9 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e      /* Do common
1acea 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63 6f 64   setup for opcod
1aceb 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 6f  es marked with o
1acec 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
1aced 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d 62 69  ing.    ** combi
1acee 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f 70 65  nations of prope
1acef 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20  rties..    **.  
1acf0 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69    **           i
1acf1 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  n1.    **       
1acf2 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20 20 20      in1 in2.    
1acf3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31  **           in1
1acf4 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20 2a 2a   in2 out3.    **
1acf5 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 20 69             in1 i
1acf6 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  n3.    **.    **
1acf7 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e 31 2c   Variables pIn1,
1acf8 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e 33 20   pIn2, and pIn3 
1acf9 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e  are made to poin
1acfa 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  t to appropriate
1acfb 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72  .    ** register
1acfc 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20 20 56  s for inputs.  V
1acfd 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70 6f 69  ariable pOut poi
1acfe 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74 70 75  nts to the outpu
1acff 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20 20 20  t register..    
1ad00 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70 50 72  */.    if( (opPr
1ad01 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49  operty & OPFLG_I
1ad02 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  N1)!=0 ){.      
1ad03 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1ad04 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
1ad05 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e  t( pOp->p1<=p->n
1ad06 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49 6e  Mem );.      pIn
1ad07 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  1 = &p->aMem[pOp
1ad08 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52 45 47  ->p1];.      REG
1ad09 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1ad0a 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20  >p1, pIn1);.    
1ad0b 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74    if( (opPropert
1ad0c 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21 3d  y & OPFLG_IN2)!=
1ad0d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  0 ){.        ass
1ad0e 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29  ert( pOp->p2>0 )
1ad0f 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1ad10 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d  ( pOp->p2<=p->nM
1ad11 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70 49  em );.        pI
1ad12 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  n2 = &p->aMem[pO
1ad13 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 20  p->p2];.        
1ad14 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1ad15 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a 20  Op->p2, pIn2);. 
1ad16 20 20 20 20 20 20 20 69 66 28 20 28 6f 70 50 72         if( (opPr
1ad17 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f  operty & OPFLG_O
1ad18 55 54 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  UT3)!=0 ){.     
1ad19 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1ad1a 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20  ->p3>0 );.      
1ad1b 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1ad1c 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1ad1d 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 20 3d            pOut =
1ad1e 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1ad1f 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  3];.        }.  
1ad20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f      }else if( (o
1ad21 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
1ad22 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20  G_IN3)!=0 ){.   
1ad23 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1ad24 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20  ->p3>0 );.      
1ad25 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ad26 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1ad27 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 70 2d        pIn3 = &p-
1ad28 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1ad29 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52          REGISTER
1ad2a 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20  _TRACE(pOp->p3, 
1ad2b 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pIn3);.      }. 
1ad2c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70     }else if( (op
1ad2d 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
1ad2e 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20 20  _IN2)!=0 ){.    
1ad2f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ad30 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  2>0 );.      ass
1ad31 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d  ert( pOp->p2<=p-
1ad32 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70  >nMem );.      p
1ad33 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In2 = &p->aMem[p
1ad34 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 52  Op->p2];.      R
1ad35 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1ad36 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a 20 20  p->p2, pIn2);.  
1ad37 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 50    }else if( (opP
1ad38 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
1ad39 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  IN3)!=0 ){.     
1ad3a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1ad3b 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  >0 );.      asse
1ad3c 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e  rt( pOp->p3<=p->
1ad3d 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49  nMem );.      pI
1ad3e 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  n3 = &p->aMem[pO
1ad3f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 52 45  p->p3];.      RE
1ad40 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1ad41 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 20  ->p3, pIn3);.   
1ad42 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20   }..    switch( 
1ad43 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 0a  pOp->opcode ){..
1ad44 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
1ad45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1ad49 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69  * What follows i
1ad4a 73 20 61 20 6d 61 73 73 69 76 65 20 73 77 69 74  s a massive swit
1ad4b 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65  ch statement whe
1ad4c 72 65 20 65 61 63 68 20 63 61 73 65 20 69 6d 70  re each case imp
1ad4d 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 70  lements a.** sep
1ad4e 61 72 61 74 65 20 69 6e 73 74 72 75 63 74 69 6f  arate instructio
1ad4f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c  n in the virtual
1ad50 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20 77 65   machine.  If we
1ad51 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 61   follow the usua
1ad52 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f 6e  l.** indentation
1ad53 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 61   conventions, ea
1ad54 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64 20 62  ch case should b
1ad55 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20 36 20  e indented by 6 
1ad56 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a 20  spaces.  But.** 
1ad57 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20 6f 66  that is a lot of
1ad58 20 77 61 73 74 65 64 20 73 70 61 63 65 20 6f 6e   wasted space on
1ad59 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67 69 6e   the left margin
1ad5a 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65 20 77  .  So the code w
1ad5b 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 69  ithin.** the swi
1ad5c 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 69  tch statement wi
1ad5d 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20 63 6f  ll break with co
1ad5e 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 20  nvention and be 
1ad5f 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f 74  flush-left. Anot
1ad60 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d 65  her.** big comme
1ad61 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 74  nt (similar to t
1ad62 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d 61  his one) will ma
1ad63 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 20  rk the point in 
1ad64 74 68 65 20 63 6f 64 65 20 77 68 65 72 65 0a 2a  the code where.*
1ad65 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20  * we transition 
1ad66 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 69  back to normal i
1ad67 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ndentation..**.*
1ad68 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67  * The formatting
1ad69 20 6f 66 20 65 61 63 68 20 63 61 73 65 20 69 73   of each case is
1ad6a 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 65   important.  The
1ad6b 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 51   makefile for SQ
1ad6c 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65  Lite.** generate
1ad6d 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20 22 6f  s two C files "o
1ad6e 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 6f  pcodes.h" and "o
1ad6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73 63 61  pcodes.c" by sca
1ad70 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 69  nning this.** fi
1ad71 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c  le looking for l
1ad72 69 6e 65 73 20 74 68 61 74 20 62 65 67 69 6e 20  ines that begin 
1ad73 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f 22 2e  with "case OP_".
1ad74 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 20    The opcodes.h 
1ad75 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65  files.** will be
1ad76 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65   filled with #de
1ad77 66 69 6e 65 73 20 74 68 61 74 20 67 69 76 65 20  fines that give 
1ad78 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72 20 76  unique integer v
1ad79 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a 2a 2a  alues to each.**
1ad7a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20   opcode and the 
1ad7b 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69  opcodes.c file i
1ad7c 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e  s filled with an
1ad7d 20 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67   array of string
1ad7e 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20  s where.** each 
1ad7f 73 74 72 69 6e 67 20 69 73 20 74 68 65 20 73 79  string is the sy
1ad80 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20  mbolic name for 
1ad81 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1ad82 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68  g opcode.  If th
1ad83 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 65 6d  e.** case statem
1ad84 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20  ent is followed 
1ad85 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 20  by a comment of 
1ad86 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 6d  the form "/# sam
1ad87 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a 20  e as ... #/".** 
1ad88 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 20  that comment is 
1ad89 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
1ad8a 65 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72  e the particular
1ad8b 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70   value of the op
1ad8c 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  code..**.** Othe
1ad8d 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 68  r keywords in th
1ad8e 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 66  e comment that f
1ad8f 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61 73 65  ollows each case
1ad90 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20   are used to.** 
1ad91 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 4f 50  construct the OP
1ad92 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20  FLG_INITIALIZER 
1ad93 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69 74 69  value that initi
1ad94 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 6f  alizes opcodePro
1ad95 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 77  perty[]..** Keyw
1ad96 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 6e  ords include: in
1ad97 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 74  1, in2, in3, out
1ad98 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20 6f 75  2_prerelease, ou
1ad99 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a 2a  t2, out3.  See.*
1ad9a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e  * the mkopcodeh.
1ad9b 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 61  awk script for a
1ad9c 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1ad9d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63  ation..**.** Doc
1ad9e 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74  umentation about
1ad9f 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73   VDBE opcodes is
1ada0 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 63   generated by sc
1ada1 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65  anning this file
1ada2 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66  .** for lines of
1ada3 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f   that contain "O
1ada4 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c  pcode:".  That l
1ada5 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73  ine and all subs
1ada6 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e  equent.** commen
1ada7 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64  t lines are used
1ada8 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69   in the generati
1ada9 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  on of the opcode
1adaa 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74  .html documentat
1adab 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a  ion.** file..**.
1adac 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a  ** SUMMARY:.**.*
1adad 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67  *     Formatting
1adae 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f   is important to
1adaf 20 73 63 72 69 70 74 73 20 74 68 61 74 20 73 63   scripts that sc
1adb0 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a  an this file..**
1adb1 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69       Do not devi
1adb2 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72  ate from the for
1adb3 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75  matting style cu
1adb4 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a  rrently in use..
1adb5 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1adb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adba 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47  /../* Opcode:  G
1adbb 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a  oto * P2 * * *.*
1adbc 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 74  *.** An uncondit
1adbd 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 64  ional jump to ad
1adbe 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 65  dress P2..** The
1adbf 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1adc0 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c 6c 20  n executed will 
1adc1 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 61  be .** the one a
1adc2 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f 6d 20  t index P2 from 
1adc3 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
1adc4 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e  .** the program.
1adc5 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 6f  .*/.case OP_Goto
1adc6 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1adc7 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 45  /* jump */.  CHE
1adc8 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54  CK_FOR_INTERRUPT
1adc9 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  ;.  pc = pOp->p2
1adca 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   - 1;.  break;.}
1adcb 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f  ../* Opcode:  Go
1adcc 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  sub P1 P2 * * *.
1adcd 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
1adce 63 75 72 72 65 6e 74 20 61 64 64 72 65 73 73 20  current address 
1adcf 6f 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 31  onto register P1
1add0 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 6d  .** and then jum
1add1 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e  p to address P2.
1add2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 75  .*/.case OP_Gosu
1add3 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  b: {            
1add4 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 73 73  /* jump */.  ass
1add5 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29  ert( pOp->p1>0 )
1add6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1add7 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
1add8 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65    pIn1 = &p->aMe
1add9 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  m[pOp->p1];.  as
1adda 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61  sert( (pIn1->fla
1addb 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30  gs & MEM_Dyn)==0
1addc 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67   );.  pIn1->flag
1addd 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70  s = MEM_Int;.  p
1adde 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20  In1->u.i = pc;. 
1addf 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1ade0 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a  pOp->p1, pIn1);.
1ade1 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1ade2 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   1;.  break;.}..
1ade3 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 74 75  /* Opcode:  Retu
1ade4 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  rn P1 * * * *.**
1ade5 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20  .** Jump to the 
1ade6 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
1ade7 20 61 66 74 65 72 20 74 68 65 20 61 64 64 72 65   after the addre
1ade8 73 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ss in register P
1ade9 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65  1..*/.case OP_Re
1adea 74 75 72 6e 3a 20 7b 20 20 20 20 20 20 20 20 20  turn: {         
1adeb 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73    /* in1 */.  as
1adec 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1aded 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  s & MEM_Int );. 
1adee 20 70 63 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d   pc = (int)pIn1-
1adef 3e 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  >u.i;.  break;.}
1adf0 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59 69  ../* Opcode:  Yi
1adf1 65 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  eld P1 * * * *.*
1adf2 2a 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 70 72  *.** Swap the pr
1adf3 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 77 69  ogram counter wi
1adf4 74 68 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  th the value in 
1adf5 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a  register P1..*/.
1adf6 63 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20 7b  case OP_Yield: {
1adf7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
1adf8 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 65  n1 */.  int pcDe
1adf9 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  st;.  assert( (p
1adfa 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1adfb 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49  _Dyn)==0 );.  pI
1adfc 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  n1->flags = MEM_
1adfd 49 6e 74 3b 0a 20 20 70 63 44 65 73 74 20 3d 20  Int;.  pcDest = 
1adfe 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a  (int)pIn1->u.i;.
1adff 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63    pIn1->u.i = pc
1ae00 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  ;.  REGISTER_TRA
1ae01 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31  CE(pOp->p1, pIn1
1ae02 29 3b 0a 20 20 70 63 20 3d 20 70 63 44 65 73 74  );.  pc = pcDest
1ae03 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1ae04 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 66   Opcode:  HaltIf
1ae05 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 33 20 50  Null  P1 P2 P3 P
1ae06 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20  4 *.**.** Check 
1ae07 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1ae08 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 69 73  ister P3.  If is
1ae09 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 61   is NULL then Ha
1ae0a 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 61  lt using.** para
1ae0b 6d 65 74 65 72 20 50 31 2c 20 50 32 2c 20 61 6e  meter P1, P2, an
1ae0c 64 20 50 34 20 61 73 20 69 66 20 74 68 69 73 20  d P4 as if this 
1ae0d 77 65 72 65 20 61 20 48 61 6c 74 20 69 6e 73 74  were a Halt inst
1ae0e 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65  ruction.  If the
1ae0f 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 67  .** value in reg
1ae10 69 73 74 65 72 20 50 33 20 69 73 20 6e 6f 74 20  ister P3 is not 
1ae11 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20  NULL, then this 
1ae12 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
1ae13 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48  op..*/.case OP_H
1ae14 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 20  altIfNull: {    
1ae15 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 69 66    /* in3 */.  if
1ae16 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26  ( (pIn3->flags &
1ae17 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 20   MEM_Null)==0 ) 
1ae18 62 72 65 61 6b 3b 0a 20 20 2f 2a 20 46 61 6c 6c  break;.  /* Fall
1ae19 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50   through into OP
1ae1a 5f 48 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f  _Halt */.}../* O
1ae1b 70 63 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 20  pcode:  Halt P1 
1ae1c 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  P2 * P4 *.**.** 
1ae1d 45 78 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79  Exit immediately
1ae1e 2e 20 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73  .  All open curs
1ae1f 6f 72 73 2c 20 65 74 63 20 61 72 65 20 63 6c 6f  ors, etc are clo
1ae20 73 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63  sed.** automatic
1ae21 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69  ally..**.** P1 i
1ae22 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64  s the result cod
1ae23 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
1ae24 6c 69 74 65 33 5f 65 78 65 63 28 29 2c 20 73 71  lite3_exec(), sq
1ae25 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 0a 2a  lite3_reset(),.*
1ae26 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e  * or sqlite3_fin
1ae27 61 6c 69 7a 65 28 29 2e 20 20 46 6f 72 20 61 20  alize().  For a 
1ae28 6e 6f 72 6d 61 6c 20 68 61 6c 74 2c 20 74 68 69  normal halt, thi
1ae29 73 20 73 68 6f 75 6c 64 20 62 65 20 53 51 4c 49  s should be SQLI
1ae2a 54 45 5f 4f 4b 20 28 30 29 2e 0a 2a 2a 20 46 6f  TE_OK (0)..** Fo
1ae2b 72 20 65 72 72 6f 72 73 2c 20 69 74 20 63 61 6e  r errors, it can
1ae2c 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 76   be some other v
1ae2d 61 6c 75 65 2e 20 20 49 66 20 50 31 21 3d 30 20  alue.  If P1!=0 
1ae2e 74 68 65 6e 20 50 32 20 77 69 6c 6c 20 64 65 74  then P2 will det
1ae2f 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65  ermine.** whethe
1ae30 72 20 6f 72 20 6e 6f 74 20 74 6f 20 72 6f 6c 6c  r or not to roll
1ae31 62 61 63 6b 20 74 68 65 20 63 75 72 72 65 6e 74  back the current
1ae32 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 44   transaction.  D
1ae33 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 0a 2a  o not rollback.*
1ae34 2a 20 69 66 20 50 32 3d 3d 4f 45 5f 46 61 69 6c  * if P2==OE_Fail
1ae35 2e 20 44 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63  . Do the rollbac
1ae36 6b 20 69 66 20 50 32 3d 3d 4f 45 5f 52 6f 6c 6c  k if P2==OE_Roll
1ae37 62 61 63 6b 2e 20 20 49 66 20 50 32 3d 3d 4f 45  back.  If P2==OE
1ae38 5f 41 62 6f 72 74 2c 0a 2a 2a 20 74 68 65 6e 20  _Abort,.** then 
1ae39 62 61 63 6b 20 6f 75 74 20 61 6c 6c 20 63 68 61  back out all cha
1ae3a 6e 67 65 73 20 74 68 61 74 20 68 61 76 65 20 6f  nges that have o
1ae3b 63 63 75 72 72 65 64 20 64 75 72 69 6e 67 20 74  ccurred during t
1ae3c 68 69 73 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  his execution of
1ae3d 20 74 68 65 0a 2a 2a 20 56 44 42 45 2c 20 62 75   the.** VDBE, bu
1ae3e 74 20 64 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63  t do not rollbac
1ae3f 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
1ae40 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20  n. .**.** If P4 
1ae41 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e  is not null then
1ae42 20 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   it is an error 
1ae43 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a  message string..
1ae44 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61  **.** There is a
1ae45 6e 20 69 6d 70 6c 69 65 64 20 22 48 61 6c 74 20  n implied "Halt 
1ae46 30 20 30 20 30 22 20 69 6e 73 74 72 75 63 74 69  0 0 0" instructi
1ae47 6f 6e 20 69 6e 73 65 72 74 65 64 20 61 74 20 74  on inserted at t
1ae48 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 0a 2a  he very end of.*
1ae49 2a 20 65 76 65 72 79 20 70 72 6f 67 72 61 6d 2e  * every program.
1ae4a 20 20 53 6f 20 61 20 6a 75 6d 70 20 70 61 73 74    So a jump past
1ae4b 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75   the last instru
1ae4c 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 72 6f  ction of the pro
1ae4d 67 72 61 6d 0a 2a 2a 20 69 73 20 74 68 65 20 73  gram.** is the s
1ae4e 61 6d 65 20 61 73 20 65 78 65 63 75 74 69 6e 67  ame as executing
1ae4f 20 48 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f   Halt..*/.case O
1ae50 50 5f 48 61 6c 74 3a 20 7b 0a 20 20 70 2d 3e 72  P_Halt: {.  p->r
1ae51 63 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70  c = pOp->p1;.  p
1ae52 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 70 2d 3e  ->pc = pc;.  p->
1ae53 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 70 4f  errorAction = pO
1ae54 70 2d 3e 70 32 3b 0a 20 20 69 66 28 20 70 4f 70  p->p2;.  if( pOp
1ae55 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71  ->p4.z ){.    sq
1ae56 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1ae57 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1ae58 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  "%s", pOp->p4.z)
1ae59 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
1ae5a 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b  ite3VdbeHalt(p);
1ae5b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
1ae5c 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63  QLITE_BUSY || rc
1ae5d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
1ae5e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1ae5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72  BUSY ){.    p->r
1ae60 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  c = rc = SQLITE_
1ae61 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  BUSY;.  }else{. 
1ae62 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20     rc = p->rc ? 
1ae63 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53  SQLITE_ERROR : S
1ae64 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a  QLITE_DONE;.  }.
1ae65 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75    goto vdbe_retu
1ae66 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  rn;.}../* Opcode
1ae67 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 20  : Integer P1 P2 
1ae68 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  * * *.**.** The 
1ae69 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  32-bit integer v
1ae6a 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 74  alue P1 is writt
1ae6b 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  en into register
1ae6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P2..*/.case OP_
1ae6d 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20  Integer: {      
1ae6e 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1ae6f 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d  lease */.  pOut-
1ae70 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1ae71 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
1ae72 70 4f 70 2d 3e 70 31 3b 0a 20 20 62 72 65 61 6b  pOp->p1;.  break
1ae73 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1ae74 49 6e 74 36 34 20 2a 20 50 32 20 2a 20 50 34 20  Int64 * P2 * P4 
1ae75 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  *.**.** P4 is a 
1ae76 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d  pointer to a 64-
1ae77 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75  bit integer valu
1ae78 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74  e..** Write that
1ae79 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69   value into regi
1ae7a 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  ster P2..*/.case
1ae7b 20 4f 50 5f 49 6e 74 36 34 3a 20 7b 20 20 20 20   OP_Int64: {    
1ae7c 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1ae7d 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61  rerelease */.  a
1ae7e 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70  ssert( pOp->p4.p
1ae7f 49 36 34 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74  I64!=0 );.  pOut
1ae80 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1ae81 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  t;.  pOut->u.i =
1ae82 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a   *pOp->p4.pI64;.
1ae83 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1ae84 70 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32  pcode: Real * P2
1ae85 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34   * P4 *.**.** P4
1ae86 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1ae87 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69   a 64-bit floati
1ae88 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a  ng point value..
1ae89 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61  ** Write that va
1ae8a 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65  lue into registe
1ae8b 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P2..*/.case OP
1ae8c 5f 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20  _Real: {        
1ae8d 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1ae8e 4b 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72  K_FLOAT, out2-pr
1ae8f 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f  erelease */.  pO
1ae90 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
1ae91 52 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20  Real;.  assert( 
1ae92 21 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70  !sqlite3IsNaN(*p
1ae93 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b  Op->p4.pReal) );
1ae94 0a 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f  .  pOut->r = *pO
1ae95 70 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62  p->p4.pReal;.  b
1ae96 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1ae97 64 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32  de: String8 * P2
1ae98 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34   * P4 *.**.** P4
1ae99 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c   points to a nul
1ae9a 20 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d   terminated UTF-
1ae9b 38 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f  8 string. This o
1ae9c 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f  pcode is transfo
1ae9d 72 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e  rmed .** into an
1ae9e 20 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72   OP_String befor
1ae9f 65 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64  e it is executed
1aea0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74   for the first t
1aea1 69 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ime..*/.case OP_
1aea2 53 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20  String8: {      
1aea3 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1aea4 5f 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72  _STRING, out2-pr
1aea5 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73  erelease */.  as
1aea6 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21  sert( pOp->p4.z!
1aea7 3d 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63  =0 );.  pOp->opc
1aea8 6f 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b  ode = OP_String;
1aea9 0a 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c  .  pOp->p1 = sql
1aeaa 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70  ite3Strlen30(pOp
1aeab 2d 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65  ->p4.z);..#ifnde
1aeac 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
1aead 46 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69  F16.  if( encodi
1aeae 6e 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  ng!=SQLITE_UTF8 
1aeaf 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1aeb0 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74  beMemSetStr(pOut
1aeb1 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31 2c  , pOp->p4.z, -1,
1aeb2 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
1aeb3 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
1aeb4 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
1aeb5 3d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e  =sqlite3VdbeChan
1aeb6 67 65 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c  geEncoding(pOut,
1aeb7 20 65 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74   encoding) ) got
1aeb8 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66  o no_mem;.    if
1aeb9 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
1aeba 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
1aebb 72 69 74 65 61 62 6c 65 28 70 4f 75 74 29 20 29  riteable(pOut) )
1aebc 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1aebd 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20    pOut->zMalloc 
1aebe 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66  = 0;.    pOut->f
1aebf 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 61 74  lags |= MEM_Stat
1aec0 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c  ic;.    pOut->fl
1aec1 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b  ags &= ~MEM_Dyn;
1aec2 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34  .    if( pOp->p4
1aec3 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41 4d 49 43  type==P4_DYNAMIC
1aec4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1aec5 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d  3DbFree(db, pOp-
1aec6 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  >p4.z);.    }.  
1aec7 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
1aec8 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 20 20  P4_DYNAMIC;.    
1aec9 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75 74  pOp->p4.z = pOut
1aeca 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 31  ->z;.    pOp->p1
1aecb 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 20 20   = pOut->n;.    
1aecc 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e  if( pOp->p1>db->
1aecd 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1aece 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
1aecf 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69       goto too_bi
1aed0 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 55 50 44  g;.    }.    UPD
1aed1 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1aed2 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61  (pOut);.    brea
1aed3 6b 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  k;.  }.#endif.  
1aed4 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e  if( pOp->p1>db->
1aed5 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1aed6 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
1aed7 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1aed8 0a 20 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74  .  }.  /* Fall t
1aed9 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
1aeda 78 74 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69  xt case, OP_Stri
1aedb 6e 67 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70  ng */.}.  ./* Op
1aedc 63 6f 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20  code: String P1 
1aedd 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  P2 * P4 *.**.** 
1aede 54 68 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65  The string value
1aedf 20 50 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31   P4 of length P1
1aee0 20 28 62 79 74 65 73 29 20 69 73 20 73 74 6f 72   (bytes) is stor
1aee1 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1aee2 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74  2..*/.case OP_St
1aee3 72 69 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20  ring: {         
1aee4 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1aee5 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ase */.  assert(
1aee6 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b   pOp->p4.z!=0 );
1aee7 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d  .  pOut->flags =
1aee8 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61   MEM_Str|MEM_Sta
1aee9 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20  tic|MEM_Term;.  
1aeea 70 4f 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70  pOut->z = pOp->p
1aeeb 34 2e 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d  4.z;.  pOut->n =
1aeec 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74   pOp->p1;.  pOut
1aeed 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67  ->enc = encoding
1aeee 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ;.  UPDATE_MAX_B
1aeef 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20  LOBSIZE(pOut);. 
1aef0 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1aef1 63 6f 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20  code: Null * P2 
1aef2 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  * * *.**.** Writ
1aef3 65 20 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65  e a NULL into re
1aef4 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61  gister P2..*/.ca
1aef5 73 65 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20  se OP_Null: {   
1aef6 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1aef7 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1aef8 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70  break;.}.../* Op
1aef9 63 6f 64 65 3a 20 42 6c 6f 62 20 50 31 20 50 32  code: Blob P1 P2
1aefa 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70   * P4.**.** P4 p
1aefb 6f 69 6e 74 73 20 74 6f 20 61 20 62 6c 6f 62 20  oints to a blob 
1aefc 6f 66 20 64 61 74 61 20 50 31 20 62 79 74 65 73  of data P1 bytes
1aefd 20 6c 6f 6e 67 2e 20 20 53 74 6f 72 65 20 74 68   long.  Store th
1aefe 69 73 0a 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65  is.** blob in re
1aeff 67 69 73 74 65 72 20 50 32 2e 20 54 68 69 73 20  gister P2. This 
1af00 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6e  instruction is n
1af01 6f 74 20 63 6f 64 65 64 20 64 69 72 65 63 74 6c  ot coded directl
1af02 79 0a 2a 2a 20 62 79 20 74 68 65 20 63 6f 6d 70  y.** by the comp
1af03 69 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 74  iler. Instead, t
1af04 68 65 20 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65  he compiler laye
1af05 72 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61  r specifies.** a
1af06 6e 20 4f 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63  n OP_HexBlob opc
1af07 6f 64 65 2c 20 77 69 74 68 20 74 68 65 20 68 65  ode, with the he
1af08 78 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  x string represe
1af09 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68  ntation of.** th
1af0a 65 20 62 6c 6f 62 20 61 73 20 50 34 2e 20 54 68  e blob as P4. Th
1af0b 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72 61  is opcode is tra
1af0c 6e 73 66 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f  nsformed to an O
1af0d 50 5f 42 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69  P_Blob.** the fi
1af0e 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65  rst time it is e
1af0f 78 65 63 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65  xecuted..*/.case
1af10 20 4f 50 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20   OP_Blob: {     
1af11 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
1af12 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
1af13 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1af14 70 31 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58  p1 <= SQLITE_MAX
1af15 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c  _LENGTH );.  sql
1af16 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
1af17 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e  r(pOut, pOp->p4.
1af18 7a 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30  z, pOp->p1, 0, 0
1af19 29 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d  );.  pOut->enc =
1af1a 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44   encoding;.  UPD
1af1b 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1af1c 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b  (pOut);.  break;
1af1d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  .}../* Opcode: V
1af1e 61 72 69 61 62 6c 65 20 50 31 20 50 32 20 50 33  ariable P1 P2 P3
1af1f 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 61 6e   P4 *.**.** Tran
1af20 73 66 65 72 20 74 68 65 20 76 61 6c 75 65 73 20  sfer the values 
1af21 6f 66 20 62 6f 75 6e 64 20 70 61 72 61 6d 65 74  of bound paramet
1af22 65 72 73 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20  ers P1..P1+P3-1 
1af23 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a  into registers.*
1af24 2a 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 0a 2a  * P2..P2+P3-1..*
1af25 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61  *.** If the para
1af26 6d 65 74 65 72 20 69 73 20 6e 61 6d 65 64 2c 20  meter is named, 
1af27 74 68 65 6e 20 69 74 73 20 6e 61 6d 65 20 61 70  then its name ap
1af28 70 65 61 72 73 20 69 6e 20 50 34 20 61 6e 64 20  pears in P4 and 
1af29 50 33 3d 3d 31 2e 0a 2a 2a 20 54 68 65 20 50 34  P3==1..** The P4
1af2a 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 62   value is used b
1af2b 79 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  y sqlite3_bind_p
1af2c 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 2e  arameter_name().
1af2d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 61 72 69  .*/.case OP_Vari
1af2e 61 62 6c 65 3a 20 7b 0a 20 20 69 6e 74 20 6a 20  able: {.  int j 
1af2f 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a 20  = pOp->p1 - 1;. 
1af30 20 69 6e 74 20 6b 20 3d 20 70 4f 70 2d 3e 70 32   int k = pOp->p2
1af31 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20  ;.  Mem *pVar;. 
1af32 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33   int n = pOp->p3
1af33 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3e 3d 30  ;.  assert( j>=0
1af34 20 26 26 20 6a 2b 6e 3c 3d 70 2d 3e 6e 56 61 72   && j+n<=p->nVar
1af35 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b 3e   );.  assert( k>
1af36 3d 31 20 26 26 20 6b 2b 6e 2d 31 3c 3d 70 2d 3e  =1 && k+n-1<=p->
1af37 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74  nMem );.  assert
1af38 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c  ( pOp->p4.z==0 |
1af39 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 29 3b 0a  | pOp->p3==1 );.
1af3a 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e 20  .  while( n-- > 
1af3b 30 20 29 7b 0a 20 20 20 20 70 56 61 72 20 3d 20  0 ){.    pVar = 
1af3c 26 70 2d 3e 61 56 61 72 5b 6a 2b 2b 5d 3b 0a 20  &p->aVar[j++];. 
1af3d 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
1af3e 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 56 61 72  beMemTooBig(pVar
1af3f 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
1af40 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20  too_big;.    }. 
1af41 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d     pOut = &p->aM
1af42 65 6d 5b 6b 2b 2b 5d 3b 0a 20 20 20 20 73 71 6c  em[k++];.    sql
1af43 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1af44 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74 29  seExternal(pOut)
1af45 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67  ;.    pOut->flag
1af46 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1af47 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1af48 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74  ShallowCopy(pOut
1af49 2c 20 70 56 61 72 2c 20 4d 45 4d 5f 53 74 61 74  , pVar, MEM_Stat
1af4a 69 63 29 3b 0a 20 20 20 20 55 50 44 41 54 45 5f  ic);.    UPDATE_
1af4b 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75  MAX_BLOBSIZE(pOu
1af4c 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  t);.  }.  break;
1af4d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  .}../* Opcode: M
1af4e 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ove P1 P2 P3 * *
1af4f 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  .**.** Move the 
1af50 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74  values in regist
1af51 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 6f  er P1..P1+P3-1 o
1af52 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69  ver into.** regi
1af53 73 74 65 72 73 20 50 32 2e 2e 50 32 2b 50 33 2d  sters P2..P2+P3-
1af54 31 2e 20 20 52 65 67 69 73 74 65 72 73 20 50 31  1.  Registers P1
1af55 2e 2e 50 31 2b 50 31 2d 31 20 61 72 65 0a 2a 2a  ..P1+P1-1 are.**
1af56 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 20   left holding a 
1af57 4e 55 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e 20  NULL.  It is an 
1af58 65 72 72 6f 72 20 66 6f 72 20 72 65 67 69 73 74  error for regist
1af59 65 72 20 72 61 6e 67 65 73 0a 2a 2a 20 50 31 2e  er ranges.** P1.
1af5a 2e 50 31 2b 50 33 2d 31 20 61 6e 64 20 50 32 2e  .P1+P3-1 and P2.
1af5b 2e 50 32 2b 50 33 2d 31 20 74 6f 20 6f 76 65 72  .P2+P3-1 to over
1af5c 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  lap..*/.case OP_
1af5d 4d 6f 76 65 3a 20 7b 0a 20 20 63 68 61 72 20 2a  Move: {.  char *
1af5e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20 6e  zMalloc;.  int n
1af5f 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e   = pOp->p3;.  in
1af60 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  t p1 = pOp->p1;.
1af61 20 20 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e    int p2 = pOp->
1af62 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  p2;.  assert( n>
1af63 30 20 26 26 20 70 31 3e 30 20 26 26 20 70 32 3e  0 && p1>0 && p2>
1af64 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1af65 31 2b 6e 3c 3d 70 32 20 7c 7c 20 70 32 2b 6e 3c  1+n<=p2 || p2+n<
1af66 3d 70 31 20 29 3b 0a 0a 20 20 70 49 6e 31 20 3d  =p1 );..  pIn1 =
1af67 20 26 70 2d 3e 61 4d 65 6d 5b 70 31 5d 3b 0a 20   &p->aMem[p1];. 
1af68 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
1af69 5b 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 6e  [p2];.  while( n
1af6a 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  -- ){.    assert
1af6b 28 20 70 4f 75 74 3c 3d 26 70 2d 3e 61 4d 65 6d  ( pOut<=&p->aMem
1af6c 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20  [p->nMem] );.   
1af6d 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d 26   assert( pIn1<=&
1af6e 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d  p->aMem[p->nMem]
1af6f 20 29 3b 0a 20 20 20 20 7a 4d 61 6c 6c 6f 63 20   );.    zMalloc 
1af70 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b  = pOut->zMalloc;
1af71 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c  .    pOut->zMall
1af72 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  oc = 0;.    sqli
1af73 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70  te3VdbeMemMove(p
1af74 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20  Out, pIn1);.    
1af75 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pIn1->zMalloc = 
1af76 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 52 45 47  zMalloc;.    REG
1af77 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b 2b  ISTER_TRACE(p2++
1af78 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e  , pOut);.    pIn
1af79 31 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b  1++;.    pOut++;
1af7a 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1af7b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79  ./* Opcode: Copy
1af7c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1af7d 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f  ** Make a copy o
1af7e 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 6e  f register P1 in
1af7f 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1af80 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72  **.** This instr
1af81 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64  uction makes a d
1af82 65 65 70 20 63 6f 70 79 20 6f 66 20 74 68 65 20  eep copy of the 
1af83 76 61 6c 75 65 2e 20 20 41 20 64 75 70 6c 69 63  value.  A duplic
1af84 61 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f  ate.** is made o
1af85 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 20  f any string or 
1af86 62 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20  blob constant.  
1af87 53 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70  See also OP_SCop
1af88 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f  y..*/.case OP_Co
1af89 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  py: {           
1af8a 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73    /* in1 */.  as
1af8b 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1af8c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1af8d 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p2<=p->nMem );
1af8e 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1af8f 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61  em[pOp->p2];.  a
1af90 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e  ssert( pOut!=pIn
1af91 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  1 );.  sqlite3Vd
1af92 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
1af93 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d  (pOut, pIn1, MEM
1af94 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 70 68  _Ephem);.  Deeph
1af95 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29 3b  emeralize(pOut);
1af96 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1af97 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29  E(pOp->p2, pOut)
1af98 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1af99 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 20 50   Opcode: SCopy P
1af9a 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1af9b 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20   Make a shallow 
1af9c 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65 72  copy of register
1af9d 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74 65   P1 into registe
1af9e 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  r P2..**.** This
1af9f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b   instruction mak
1afa0 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70  es a shallow cop
1afa1 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20  y of the value. 
1afa2 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a   If the value.**
1afa3 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20   is a string or 
1afa4 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20 63  blob, then the c
1afa5 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f  opy is only a po
1afa6 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20  inter to the.** 
1afa7 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e  original and hen
1afa8 63 65 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e  ce if the origin
1afa9 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77 69  al changes so wi
1afaa 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20  ll the copy..** 
1afab 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f 72  Worse, if the or
1afac 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f  iginal is deallo
1afad 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79 20  cated, the copy 
1afae 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e  becomes invalid.
1afaf 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70 72 6f  .** Thus the pro
1afb0 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72 61 6e  gram must guaran
1afb1 74 65 65 20 74 68 61 74 20 74 68 65 20 6f 72 69  tee that the ori
1afb2 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63  ginal will not c
1afb3 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20  hange.** during 
1afb4 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20  the lifetime of 
1afb5 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65 20 4f  the copy.  Use O
1afb6 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61  P_Copy to make a
1afb7 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70   complete.** cop
1afb8 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43  y..*/.case OP_SC
1afb9 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20  opy: {          
1afba 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 52 45    /* in1 */.  RE
1afbb 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1afbc 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61  ->p1, pIn1);.  a
1afbd 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
1afbe 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1afbf 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p2<=p->nMem )
1afc0 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ;.  pOut = &p->a
1afc1 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1afc2 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49  assert( pOut!=pI
1afc3 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  n1 );.  sqlite3V
1afc4 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
1afc5 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45  y(pOut, pIn1, ME
1afc6 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47 49  M_Ephem);.  REGI
1afc7 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1afc8 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65  p2, pOut);.  bre
1afc9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1afca 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 31 20 50  : ResultRow P1 P
1afcb 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  2 * * *.**.** Th
1afcc 65 20 72 65 67 69 73 74 65 72 73 20 50 31 20 74  e registers P1 t
1afcd 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 20 63  hrough P1+P2-1 c
1afce 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 20  ontain a single 
1afcf 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74  row of.** result
1afd0 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 63  s. This opcode c
1afd1 61 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65  auses the sqlite
1afd2 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74 6f  3_step() call to
1afd3 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69   terminate.** wi
1afd4 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57  th an SQLITE_ROW
1afd5 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64   return code and
1afd6 20 69 74 20 73 65 74 73 20 75 70 20 74 68 65 20   it sets up the 
1afd7 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20  sqlite3_stmt.** 
1afd8 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 72 6f  structure to pro
1afd9 76 69 64 65 20 61 63 63 65 73 73 20 74 6f 20 74  vide access to t
1afda 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65 73  he top P1 values
1afdb 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a   as the result.*
1afdc 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f  * row..*/.case O
1afdd 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 20  P_ResultRow: {. 
1afde 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e   Mem *pMem;.  in
1afdf 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t i;.  assert( p
1afe0 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f  ->nResColumn==pO
1afe1 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65 72  p->p2 );.  asser
1afe2 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a  t( pOp->p1>0 );.
1afe3 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1afe4 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d  1+pOp->p2<=p->nM
1afe5 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  em+1 );..  /* If
1afe6 20 74 68 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e   the SQLITE_Coun
1afe7 74 52 6f 77 73 20 66 6c 61 67 20 69 73 20 73 65  tRows flag is se
1afe8 74 20 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61  t in sqlite3.fla
1afe9 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 0a 20  gs mask, then . 
1afea 20 2a 2a 20 44 4d 4c 20 73 74 61 74 65 6d 65 6e   ** DML statemen
1afeb 74 73 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f  ts invoke this o
1afec 70 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e 20  pcode to return 
1afed 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
1afee 77 73 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65  ws .  ** modifie
1afef 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 54  d to the user. T
1aff0 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  his is the only 
1aff1 77 61 79 20 74 68 61 74 20 61 20 56 4d 20 74 68  way that a VM th
1aff2 61 74 0a 20 20 2a 2a 20 6f 70 65 6e 73 20 61 20  at.  ** opens a 
1aff3 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1aff4 63 74 69 6f 6e 20 6d 61 79 20 69 6e 76 6f 6b 65  ction may invoke
1aff5 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 20 20   this opcode..  
1aff6 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 61 73 65 20  **.  ** In case 
1aff7 74 68 69 73 20 69 73 20 73 75 63 68 20 61 20 73  this is such a s
1aff8 74 61 74 65 6d 65 6e 74 2c 20 63 6c 6f 73 65 20  tatement, close 
1aff9 61 6e 79 20 73 74 61 74 65 6d 65 6e 74 20 74 72  any statement tr
1affa 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f  ansaction.  ** o
1affb 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 56 4d  pened by this VM
1affc 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
1affd 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65  g control to the
1affe 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20 74   user. This is t
1afff 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 65 20 74 68  o.  ** ensure th
1b000 61 74 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61  at statement-tra
1b001 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 61 6c  nsactions are al
1b002 77 61 79 73 20 6e 65 73 74 65 64 2c 20 6e 6f 74  ways nested, not
1b003 20 6f 76 65 72 6c 61 70 70 69 6e 67 2e 0a 20 20   overlapping..  
1b004 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 6e 20 73  ** If the open s
1b005 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63  tatement-transac
1b006 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6c 6f 73  tion is not clos
1b007 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 74 68  ed here, then th
1b008 65 20 75 73 65 72 0a 20 20 2a 2a 20 6d 61 79 20  e user.  ** may 
1b009 73 74 65 70 20 61 6e 6f 74 68 65 72 20 56 4d 20  step another VM 
1b00a 74 68 61 74 20 6f 70 65 6e 73 20 69 74 73 20 6f  that opens its o
1b00b 77 6e 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  wn statement tra
1b00c 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 0a 20  nsaction. This. 
1b00d 20 2a 2a 20 6d 61 79 20 6c 65 61 64 20 74 6f 20   ** may lead to 
1b00e 6f 76 65 72 6c 61 70 70 69 6e 67 20 73 74 61 74  overlapping stat
1b00f 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1b010 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ns..  */.  asser
1b011 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74  t( p->iStatement
1b012 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 6c 61 67 73  ==0 || db->flags
1b013 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77  &SQLITE_CountRow
1b014 73 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  s );.  if( SQLIT
1b015 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
1b016 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74  te3VdbeCloseStat
1b017 65 6d 65 6e 74 28 70 2c 20 53 41 56 45 50 4f 49  ement(p, SAVEPOI
1b018 4e 54 5f 52 45 4c 45 41 53 45 29 29 20 29 7b 0a  NT_RELEASE)) ){.
1b019 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
1b01a 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20    /* Invalidate 
1b01b 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75  all ephemeral cu
1b01c 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20  rsor row caches 
1b01d 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72  */.  p->cacheCtr
1b01e 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20   = (p->cacheCtr 
1b01f 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61  + 2)|1;..  /* Ma
1b020 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75  ke sure the resu
1b021 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65  lts of the curre
1b022 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20  nt row are \000 
1b023 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20  terminated.  ** 
1b024 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69  and have an assi
1b025 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20  gned type.  The 
1b026 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65  results are de-e
1b027 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a  phemeralized as.
1b028 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66    ** as side eff
1b029 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 70 4d 65 6d  ect..  */.  pMem
1b02a 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74   = p->pResultSet
1b02b 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1b02c 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  >p1];.  for(i=0;
1b02d 20 69 3c 70 4f 70 2d 3e 70 32 3b 20 69 2b 2b 29   i<pOp->p2; i++)
1b02e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1b02f 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
1b030 28 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 20 20  (&pMem[i]);.    
1b031 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 26 70  storeTypeInfo(&p
1b032 4d 65 6d 5b 69 5d 2c 20 65 6e 63 6f 64 69 6e 67  Mem[i], encoding
1b033 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  );.    REGISTER_
1b034 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 69 2c  TRACE(pOp->p1+i,
1b035 20 26 70 4d 65 6d 5b 69 5d 29 3b 0a 20 20 7d 0a   &pMem[i]);.  }.
1b036 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
1b037 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f  Failed ) goto no
1b038 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 75  _mem;..  /* Retu
1b039 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 20  rn SQLITE_ROW.  
1b03a 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 20  */.  p->pc = pc 
1b03b 2b 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49  + 1;.  rc = SQLI
1b03c 54 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 76  TE_ROW;.  goto v
1b03d 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  dbe_return;.}../
1b03e 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 74  * Opcode: Concat
1b03f 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1b040 0a 2a 2a 20 41 64 64 20 74 68 65 20 74 65 78 74  .** Add the text
1b041 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1b042 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  onto the end of 
1b043 74 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 72  the text in.** r
1b044 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73  egister P2 and s
1b045 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1b046 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a  in register P3..
1b047 2a 2a 20 49 66 20 65 69 74 68 65 72 20 74 68 65  ** If either the
1b048 20 50 31 20 6f 72 20 50 32 20 74 65 78 74 20 61   P1 or P2 text a
1b049 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f  re NULL then sto
1b04a 72 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a  re NULL in P3..*
1b04b 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 7c  *.**   P3 = P2 |
1b04c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  | P1.**.** It is
1b04d 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 20   illegal for P1 
1b04e 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68 65  and P3 to be the
1b04f 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e 20   same register. 
1b050 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 66  Sometimes,.** if
1b051 20 50 33 20 69 73 20 74 68 65 20 73 61 6d 65 20   P3 is the same 
1b052 72 65 67 69 73 74 65 72 20 61 73 20 50 32 2c 20  register as P2, 
1b053 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
1b054 6f 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 6f  on is able.** to
1b055 20 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 28   avoid a memcpy(
1b056 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f  )..*/.case OP_Co
1b057 6e 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20 20  ncat: {         
1b058 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1b059 43 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e 32  CONCAT, in1, in2
1b05a 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69 36 34 20  , out3 */.  i64 
1b05b 6e 42 79 74 65 3b 0a 0a 20 20 61 73 73 65 72 74  nByte;..  assert
1b05c 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b 0a  ( pIn1!=pOut );.
1b05d 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1b05e 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73  gs | pIn2->flags
1b05f 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  ) & MEM_Null ){.
1b060 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1b061 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b  emSetNull(pOut);
1b062 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1b063 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e    ExpandBlob(pIn
1b064 31 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28  1);.  Stringify(
1b065 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  pIn1, encoding);
1b066 0a 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49  .  ExpandBlob(pI
1b067 6e 32 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79  n2);.  Stringify
1b068 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e 67 29  (pIn2, encoding)
1b069 3b 0a 20 20 6e 42 79 74 65 20 3d 20 70 49 6e 31  ;.  nByte = pIn1
1b06a 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20  ->n + pIn2->n;. 
1b06b 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61   if( nByte>db->a
1b06c 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1b06d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1b06e 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1b06f 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65    }.  MemSetType
1b070 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 53  Flag(pOut, MEM_S
1b071 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  tr);.  if( sqlit
1b072 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f  e3VdbeMemGrow(pO
1b073 75 74 2c 20 28 69 6e 74 29 6e 42 79 74 65 2b 32  ut, (int)nByte+2
1b074 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29 7b  , pOut==pIn2) ){
1b075 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  .    goto no_mem
1b076 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74  ;.  }.  if( pOut
1b077 21 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d 65  !=pIn2 ){.    me
1b078 6d 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70 49  mcpy(pOut->z, pI
1b079 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29 3b  n2->z, pIn2->n);
1b07a 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70  .  }.  memcpy(&p
1b07b 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d 2c  Out->z[pIn2->n],
1b07c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d 3e   pIn1->z, pIn1->
1b07d 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e 42  n);.  pOut->z[nB
1b07e 79 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74  yte] = 0;.  pOut
1b07f 2d 3e 7a 5b 6e 42 79 74 65 2b 31 5d 20 3d 20 30  ->z[nByte+1] = 0
1b080 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20  ;.  pOut->flags 
1b081 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70  |= MEM_Term;.  p
1b082 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 6e 42  Out->n = (int)nB
1b083 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63  yte;.  pOut->enc
1b084 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55   = encoding;.  U
1b085 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1b086 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61  ZE(pOut);.  brea
1b087 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1b088 20 41 64 64 20 50 31 20 50 32 20 50 33 20 2a 20   Add P1 P2 P3 * 
1b089 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  *.**.** Add the 
1b08a 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1b08b 72 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75  r P1 to the valu
1b08c 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
1b08d 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68  .** and store th
1b08e 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
1b08f 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
1b090 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
1b091 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
1b092 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
1b093 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20  pcode: Multiply 
1b094 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1b095 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74  **.** Multiply t
1b096 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1b097 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76  ster P1 by the v
1b098 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1b099 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65   P2.** and store
1b09a 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1b09b 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1b09c 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1b09d 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1b09e 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1b09f 2a 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61  * Opcode: Subtra
1b0a0 63 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ct P1 P2 P3 * *.
1b0a1 2a 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74  **.** Subtract t
1b0a2 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1b0a3 73 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65  ster P1 from the
1b0a4 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1b0a5 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f  er P2.** and sto
1b0a6 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1b0a7 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
1b0a8 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74   If either input
1b0a9 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
1b0aa 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1b0ab 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69  ./* Opcode: Divi
1b0ac 64 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  de P1 P2 P3 * *.
1b0ad 2a 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65  **.** Divide the
1b0ae 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1b0af 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c  er P1 by the val
1b0b0 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1b0b1 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74  2.** and store t
1b0b2 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
1b0b3 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 74 68  ister P3.  If th
1b0b4 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1b0b5 74 65 72 20 50 32 0a 2a 2a 20 69 73 20 7a 65 72  ter P2.** is zer
1b0b6 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  o, then the resu
1b0b7 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49  lt is NULL..** I
1b0b8 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1b0b9 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1b0ba 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1b0bb 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69 6e  * Opcode: Remain
1b0bc 64 65 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a  der P1 P2 P3 * *
1b0bd 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  .**.** Compute t
1b0be 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61 66 74  he remainder aft
1b0bf 65 72 20 69 6e 74 65 67 65 72 20 64 69 76 69 73  er integer divis
1b0c0 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ion of the value
1b0c1 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20   in.** register 
1b0c2 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65 20  P1 by the value 
1b0c3 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61  in register P2 a
1b0c4 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1b0c5 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a 20 49  ult in P3. .** I
1b0c6 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  f the value in r
1b0c7 65 67 69 73 74 65 72 20 50 32 20 69 73 20 7a 65  egister P2 is ze
1b0c8 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  ro the result is
1b0c9 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 74   NULL..** If eit
1b0ca 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e  her operand is N
1b0cb 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
1b0cc 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65  is NULL..*/.case
1b0cd 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20   OP_Add:        
1b0ce 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1b0cf 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c 20 69  me as TK_PLUS, i
1b0d0 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1b0d1 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63  .case OP_Subtrac
1b0d2 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t:              
1b0d3 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49  /* same as TK_MI
1b0d4 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  NUS, in1, in2, o
1b0d5 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d  ut3 */.case OP_M
1b0d6 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 20 20  ultiply:        
1b0d7 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1b0d8 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c 20 69   TK_STAR, in1, i
1b0d9 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1b0da 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 20 20   OP_Divide:     
1b0db 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1b0dc 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 2c 20  me as TK_SLASH, 
1b0dd 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1b0de 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 69 6e  /.case OP_Remain
1b0df 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20  der: {          
1b0e0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52   /* same as TK_R
1b0e1 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  EM, in1, in2, ou
1b0e2 74 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  t3 */.  int flag
1b0e3 73 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69  s;.  applyNumeri
1b0e4 63 41 66 66 69 6e 69 74 79 28 70 49 6e 31 29 3b  cAffinity(pIn1);
1b0e5 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41  .  applyNumericA
1b0e6 66 66 69 6e 69 74 79 28 70 49 6e 32 29 3b 0a 20  ffinity(pIn2);. 
1b0e7 20 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66   flags = pIn1->f
1b0e8 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61  lags | pIn2->fla
1b0e9 67 73 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 73  gs;.  if( (flags
1b0ea 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20   & MEM_Null)!=0 
1b0eb 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69  ) goto arithmeti
1b0ec 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c  c_result_is_null
1b0ed 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  ;.  if( (pIn1->f
1b0ee 6c 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c 61  lags & pIn2->fla
1b0ef 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d  gs & MEM_Int)==M
1b0f0 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 69 36  EM_Int ){.    i6
1b0f1 34 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20  4 a, b;.    a = 
1b0f2 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 62  pIn1->u.i;.    b
1b0f3 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20   = pIn2->u.i;.  
1b0f4 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f    switch( pOp->o
1b0f5 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 63  pcode ){.      c
1b0f6 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20  ase OP_Add:     
1b0f7 20 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20 20      b += a;     
1b0f8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1b0f9 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a  ase OP_Subtract:
1b0fa 20 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20 20      b -= a;     
1b0fb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1b0fc 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a  ase OP_Multiply:
1b0fd 20 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20 20      b *= a;     
1b0fe 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1b0ff 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b  ase OP_Divide: {
1b100 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d  .        if( a==
1b101 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65  0 ) goto arithme
1b102 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75  tic_result_is_nu
1b103 6c 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44  ll;.        /* D
1b104 69 76 69 64 69 6e 67 20 74 68 65 20 6c 61 72 67  ividing the larg
1b105 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 67  est possible neg
1b106 61 74 69 76 65 20 36 34 2d 62 69 74 20 69 6e 74  ative 64-bit int
1b107 65 67 65 72 20 28 31 3c 3c 36 33 29 20 62 79 20  eger (1<<63) by 
1b108 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 72  .        ** -1 r
1b109 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65  eturns an intege
1b10a 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 73  r too large to s
1b10b 74 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69 74  tore in a 64-bit
1b10c 20 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a 20   data-type. On. 
1b10d 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 61         ** some a
1b10e 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 68  rchitectures, th
1b10f 65 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f 77  e value overflow
1b110 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f 6e  s to (1<<63). On
1b111 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20 20   others,.       
1b112 20 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73 20   ** a SIGFPE is 
1b113 69 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c 6c  issued. The foll
1b114 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20  owing statement 
1b115 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 0a  normalizes this.
1b116 20 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61 76          ** behav
1b117 69 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c 20  ior so that all 
1b118 61 72 63 68 69 74 65 63 74 75 72 65 73 20 62 65  architectures be
1b119 68 61 76 65 20 61 73 20 69 66 20 69 6e 74 65 67  have as if integ
1b11a 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  er .        ** o
1b11b 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 64  verflow occurred
1b11c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
1b11d 20 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20 26       if( a==-1 &
1b11e 26 20 62 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e  & b==SMALLEST_IN
1b11f 54 36 34 20 29 20 61 20 3d 20 31 3b 0a 20 20 20  T64 ) a = 1;.   
1b120 20 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 20 20       b /= a;.   
1b121 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1b122 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c    }.      defaul
1b123 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28  t: {.        if(
1b124 20 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69   a==0 ) goto ari
1b125 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1b126 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  s_null;.        
1b127 69 66 28 20 61 3d 3d 2d 31 20 29 20 61 20 3d 20  if( a==-1 ) a = 
1b128 31 3b 0a 20 20 20 20 20 20 20 20 62 20 25 3d 20  1;.        b %= 
1b129 61 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  a;.        break
1b12a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1b12b 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20      pOut->u.i = 
1b12c 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70  b;.    MemSetTyp
1b12d 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1b12e 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Int);.  }else{. 
1b12f 20 20 20 64 6f 75 62 6c 65 20 61 2c 20 62 3b 0a     double a, b;.
1b130 20 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33 56      a = sqlite3V
1b131 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e  dbeRealValue(pIn
1b132 31 29 3b 0a 20 20 20 20 62 20 3d 20 73 71 6c 69  1);.    b = sqli
1b133 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65  te3VdbeRealValue
1b134 28 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69 74  (pIn2);.    swit
1b135 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20  ch( pOp->opcode 
1b136 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50  ){.      case OP
1b137 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 62 20  _Add:         b 
1b138 2b 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61  += a;       brea
1b139 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50  k;.      case OP
1b13a 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 62 20  _Subtract:    b 
1b13b 2d 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61  -= a;       brea
1b13c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50  k;.      case OP
1b13d 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 62 20  _Multiply:    b 
1b13e 2a 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61  *= a;       brea
1b13f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50  k;.      case OP
1b140 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20  _Divide: {.     
1b141 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20     /* (double)0 
1b142 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54  In case of SQLIT
1b143 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
1b144 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20  POINT... */.    
1b145 20 20 20 20 69 66 28 20 61 3d 3d 28 64 6f 75 62      if( a==(doub
1b146 6c 65 29 30 20 29 20 67 6f 74 6f 20 61 72 69 74  le)0 ) goto arit
1b147 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73  hmetic_result_is
1b148 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 62  _null;.        b
1b149 20 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 62   /= a;.        b
1b14a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1b14b 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
1b14c 20 20 20 20 20 20 20 69 36 34 20 69 61 20 3d 20         i64 ia = 
1b14d 28 69 36 34 29 61 3b 0a 20 20 20 20 20 20 20 20  (i64)a;.        
1b14e 69 36 34 20 69 62 20 3d 20 28 69 36 34 29 62 3b  i64 ib = (i64)b;
1b14f 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 61 3d  .        if( ia=
1b150 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d  =0 ) goto arithm
1b151 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e  etic_result_is_n
1b152 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ull;.        if(
1b153 20 69 61 3d 3d 2d 31 20 29 20 69 61 20 3d 20 31   ia==-1 ) ia = 1
1b154 3b 0a 20 20 20 20 20 20 20 20 62 20 3d 20 28 64  ;.        b = (d
1b155 6f 75 62 6c 65 29 28 69 62 20 25 20 69 61 29 3b  ouble)(ib % ia);
1b156 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1b157 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1b158 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e    if( sqlite3IsN
1b159 61 4e 28 62 29 20 29 7b 0a 20 20 20 20 20 20 67  aN(b) ){.      g
1b15a 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72  oto arithmetic_r
1b15b 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20  esult_is_null;. 
1b15c 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 72     }.    pOut->r
1b15d 20 3d 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74   = b;.    MemSet
1b15e 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1b15f 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66  EM_Real);.    if
1b160 28 20 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  ( (flags & MEM_R
1b161 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  eal)==0 ){.     
1b162 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65   sqlite3VdbeInte
1b163 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74  gerAffinity(pOut
1b164 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  );.    }.  }.  b
1b165 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69  reak;..arithmeti
1b166 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c  c_result_is_null
1b167 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  :.  sqlite3VdbeM
1b168 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b  emSetNull(pOut);
1b169 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1b16a 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20  Opcode: CollSeq 
1b16b 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20  * * P4.**.** P4 
1b16c 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1b16d 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74  a CollSeq struct
1b16e 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 61  . If the next ca
1b16f 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e  ll to a user fun
1b170 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72  ction.** or aggr
1b171 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69  egate calls sqli
1b172 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65  te3GetFuncCollSe
1b173 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74  q(), this collat
1b174 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c  ion sequence wil
1b175 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 64  l.** be returned
1b176 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62  . This is used b
1b177 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d  y the built-in m
1b178 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 20  in(), max() and 
1b179 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63  nullif().** func
1b17a 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tions..**.** The
1b17b 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 64 20   interface used 
1b17c 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  by the implement
1b17d 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f  ation of the afo
1b17e 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63  rementioned func
1b17f 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72  tions.** to retr
1b180 69 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 69  ieve the collati
1b181 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74 20  on sequence set 
1b182 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69  by this opcode i
1b183 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a  s not available.
1b184 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c  ** publicly, onl
1b185 79 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69  y to user functi
1b186 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20 66  ons defined in f
1b187 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f  unc.c..*/.case O
1b188 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61  P_CollSeq: {.  a
1b189 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1b18a 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29  pe==P4_COLLSEQ )
1b18b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1b18c 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f   Opcode: Functio
1b18d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  n P1 P2 P3 P4 P5
1b18e 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20  .**.** Invoke a 
1b18f 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50  user function (P
1b190 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
1b191 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72  o a Function str
1b192 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64  ucture that.** d
1b193 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74  efines the funct
1b194 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67  ion) with P5 arg
1b195 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f  uments taken fro
1b196 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e  m register P2 an
1b197 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e  d.** successors.
1b198 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20    The result of 
1b199 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
1b19a 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1b19b 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74  er P3..** Regist
1b19c 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 62  er P3 must not b
1b19d 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 6e  e one of the fun
1b19e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a  ction inputs..**
1b19f 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62  .** P1 is a 32-b
1b1a0 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63  it bitmask indic
1b1a1 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72  ating whether or
1b1a2 20 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65   not each argume
1b1a3 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75  nt to the .** fu
1b1a4 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72  nction was deter
1b1a5 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73  mined to be cons
1b1a6 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20  tant at compile 
1b1a7 74 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72  time. If the fir
1b1a8 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77  st.** argument w
1b1a9 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e  as constant then
1b1aa 20 62 69 74 20 30 20 6f 66 20 50 31 20 69 73 20   bit 0 of P1 is 
1b1ab 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 65  set. This is use
1b1ac 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a  d to determine.*
1b1ad 2a 20 77 68 65 74 68 65 72 20 6d 65 74 61 20 64  * whether meta d
1b1ae 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
1b1af 69 74 68 20 61 20 75 73 65 72 20 66 75 6e 63 74  ith a user funct
1b1b0 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69  ion argument usi
1b1b1 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  ng the.** sqlite
1b1b2 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20  3_set_auxdata() 
1b1b3 41 50 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c  API may be safel
1b1b4 79 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c  y retained until
1b1b5 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76   the next.** inv
1b1b6 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ocation of this 
1b1b7 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65  opcode..**.** Se
1b1b8 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20  e also: AggStep 
1b1b9 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a  and AggFinal.*/.
1b1ba 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  case OP_Function
1b1bb 3a 20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d  : {.  int i;.  M
1b1bc 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69  em *pArg;.  sqli
1b1bd 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b  te3_context ctx;
1b1be 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1b1bf 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20   **apVal;.  int 
1b1c0 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 0a 20 20  n = pOp->p5;..  
1b1c1 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67  apVal = p->apArg
1b1c2 3b 0a 20 20 61 73 73 65 72 74 28 20 61 70 56 61  ;.  assert( apVa
1b1c3 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 20  l || n==0 );..  
1b1c4 61 73 73 65 72 74 28 20 6e 3d 3d 30 20 7c 7c 20  assert( n==0 || 
1b1c5 28 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f  (pOp->p2>0 && pO
1b1c6 70 2d 3e 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d  p->p2+n<=p->nMem
1b1c7 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  +1) );.  assert(
1b1c8 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32   pOp->p3<pOp->p2
1b1c9 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70   || pOp->p3>=pOp
1b1ca 2d 3e 70 32 2b 6e 20 29 3b 0a 20 20 70 41 72 67  ->p2+n );.  pArg
1b1cb 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1b1cc 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  >p2];.  for(i=0;
1b1cd 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 41 72 67 2b   i<n; i++, pArg+
1b1ce 2b 29 7b 0a 20 20 20 20 61 70 56 61 6c 5b 69 5d  +){.    apVal[i]
1b1cf 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 73 74 6f   = pArg;.    sto
1b1d0 72 65 54 79 70 65 49 6e 66 6f 28 70 41 72 67 2c  reTypeInfo(pArg,
1b1d1 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20   encoding);.    
1b1d2 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1b1d3 4f 70 2d 3e 70 32 2c 20 70 41 72 67 29 3b 0a 20  Op->p2, pArg);. 
1b1d4 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f   }..  assert( pO
1b1d5 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55  p->p4type==P4_FU
1b1d6 4e 43 44 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 34  NCDEF || pOp->p4
1b1d7 74 79 70 65 3d 3d 50 34 5f 56 44 42 45 46 55 4e  type==P4_VDBEFUN
1b1d8 43 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  C );.  if( pOp->
1b1d9 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44  p4type==P4_FUNCD
1b1da 45 46 20 29 7b 0a 20 20 20 20 63 74 78 2e 70 46  EF ){.    ctx.pF
1b1db 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46  unc = pOp->p4.pF
1b1dc 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70 56 64  unc;.    ctx.pVd
1b1dd 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65  beFunc = 0;.  }e
1b1de 6c 73 65 7b 0a 20 20 20 20 63 74 78 2e 70 56 64  lse{.    ctx.pVd
1b1df 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 46 75  beFunc = (VdbeFu
1b1e0 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62  nc*)pOp->p4.pVdb
1b1e1 65 46 75 6e 63 3b 0a 20 20 20 20 63 74 78 2e 70  eFunc;.    ctx.p
1b1e2 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 64 62 65  Func = ctx.pVdbe
1b1e3 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d  Func->pFunc;.  }
1b1e4 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1b1e5 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33  >p3>0 && pOp->p3
1b1e6 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
1b1e7 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
1b1e8 4f 70 2d 3e 70 33 5d 3b 0a 20 20 63 74 78 2e 73  Op->p3];.  ctx.s
1b1e9 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  .flags = MEM_Nul
1b1ea 6c 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20 3d 20  l;.  ctx.s.db = 
1b1eb 64 62 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c  db;.  ctx.s.xDel
1b1ec 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d   = 0;.  ctx.s.zM
1b1ed 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a  alloc = 0;..  /*
1b1ee 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c   The output cell
1b1ef 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 76   may already hav
1b1f0 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63  e a buffer alloc
1b1f1 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20  ated. Move.  ** 
1b1f2 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 63  the pointer to c
1b1f3 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20  tx.s so in case 
1b1f4 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f  the user-functio
1b1f5 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 74  n can use.  ** t
1b1f6 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  he already alloc
1b1f7 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 74  ated buffer inst
1b1f8 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e  ead of allocatin
1b1f9 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a  g a new one..  *
1b1fa 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  /.  sqlite3VdbeM
1b1fb 65 6d 4d 6f 76 65 28 26 63 74 78 2e 73 2c 20 70  emMove(&ctx.s, p
1b1fc 4f 75 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79  Out);.  MemSetTy
1b1fd 70 65 46 6c 61 67 28 26 63 74 78 2e 73 2c 20 4d  peFlag(&ctx.s, M
1b1fe 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 63 74 78  EM_Null);..  ctx
1b1ff 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20  .isError = 0;.  
1b200 69 66 28 20 63 74 78 2e 70 46 75 6e 63 2d 3e 66  if( ctx.pFunc->f
1b201 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
1b202 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20  NC_NEEDCOLL ){. 
1b203 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70     assert( pOp>p
1b204 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 73  ->aOp );.    ass
1b205 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74  ert( pOp[-1].p4t
1b206 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20  ype==P4_COLLSEQ 
1b207 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1b208 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f  Op[-1].opcode==O
1b209 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20  P_CollSeq );.   
1b20a 20 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70   ctx.pColl = pOp
1b20b 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20  [-1].p4.pColl;. 
1b20c 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1b20d 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
1b20e 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1b20f 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 63 74  o_misuse;.  (*ct
1b210 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28  x.pFunc->xFunc)(
1b211 26 63 74 78 2c 20 6e 2c 20 61 70 56 61 6c 29 3b  &ctx, n, apVal);
1b212 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1b213 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20  fetyOn(db) ){.  
1b214 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b215 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29 3b  Release(&ctx.s);
1b216 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f  .    goto abort_
1b217 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1b218 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c   }.  if( db->mal
1b219 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1b21a 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20   /* Even though 
1b21b 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66  a malloc() has f
1b21c 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 6c 65  ailed, the imple
1b21d 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
1b21e 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 75 6e  .    ** user fun
1b21f 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 20 63  ction may have c
1b220 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 65 33  alled an sqlite3
1b221 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 66 75  _result_XXX() fu
1b222 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f  nction.    ** to
1b223 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 65 2e   return a value.
1b224 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
1b225 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 6e 79  all releases any
1b226 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 20 2a   resources.    *
1b227 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
1b228 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 2e 0a  h such a value..
1b229 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f      **.    ** No
1b22a 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 65 6c  te: Maybe MemRel
1b22b 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 62 65  ease() should be
1b22c 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c 69 74   called if sqlit
1b22d 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 20 20  e3SafetyOn().   
1b22e 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f 20 28   ** fails also (
1b22f 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 61 74  the if(...) stat
1b230 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 42 75  ement above). Bu
1b231 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 65 0a  t if people are.
1b232 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e 67 20      ** misusing 
1b233 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 61 76  sqlite, they hav
1b234 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c 65 6d  e bigger problem
1b235 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 64 20  s than a leaked 
1b236 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  value..    */.  
1b237 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b238 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29 3b  Release(&ctx.s);
1b239 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  .    goto no_mem
1b23a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61  ;.  }..  /* If a
1b23b 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  ny auxiliary dat
1b23c 61 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76 65  a functions have
1b23d 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 62 79 20   been called by 
1b23e 74 68 69 73 20 75 73 65 72 20 66 75 6e 63 74 69  this user functi
1b23f 6f 6e 2c 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61  on,.  ** immedia
1b240 74 65 6c 79 20 63 61 6c 6c 20 74 68 65 20 64 65  tely call the de
1b241 73 74 72 75 63 74 6f 72 20 66 6f 72 20 61 6e 79  structor for any
1b242 20 6e 6f 6e 2d 73 74 61 74 69 63 20 76 61 6c 75   non-static valu
1b243 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  es..  */.  if( c
1b244 74 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b 0a  tx.pVdbeFunc ){.
1b245 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44      sqlite3VdbeD
1b246 65 6c 65 74 65 41 75 78 44 61 74 61 28 63 74 78  eleteAuxData(ctx
1b247 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 2d  .pVdbeFunc, pOp-
1b248 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  >p1);.    pOp->p
1b249 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 63 74  4.pVdbeFunc = ct
1b24a 78 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20  x.pVdbeFunc;.   
1b24b 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
1b24c 34 5f 56 44 42 45 46 55 4e 43 3b 0a 20 20 7d 0a  4_VDBEFUNC;.  }.
1b24d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 75 6e  .  /* If the fun
1b24e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61  ction returned a
1b24f 6e 20 65 72 72 6f 72 2c 20 74 68 72 6f 77 20 61  n error, throw a
1b250 6e 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20  n exception */. 
1b251 20 69 66 28 20 63 74 78 2e 69 73 45 72 72 6f 72   if( ctx.isError
1b252 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
1b253 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1b254 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20  rMsg, db, "%s", 
1b255 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1b256 78 74 28 26 63 74 78 2e 73 29 29 3b 0a 20 20 20  xt(&ctx.s));.   
1b257 20 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72 6f   rc = ctx.isErro
1b258 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70  r;.  }..  /* Cop
1b259 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  y the result of 
1b25a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74  the function int
1b25b 6f 20 72 65 67 69 73 74 65 72 20 50 33 20 2a 2f  o register P3 */
1b25c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
1b25d 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 63 74  angeEncoding(&ct
1b25e 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  x.s, encoding);.
1b25f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b260 4d 6f 76 65 28 70 4f 75 74 2c 20 26 63 74 78 2e  Move(pOut, &ctx.
1b261 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  s);.  if( sqlite
1b262 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70  3VdbeMemTooBig(p
1b263 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Out) ){.    goto
1b264 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20   too_big;.  }.  
1b265 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1b266 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20  Op->p3, pOut);. 
1b267 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1b268 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72  SIZE(pOut);.  br
1b269 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1b26a 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20  e: BitAnd P1 P2 
1b26b 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b  P3 * *.**.** Tak
1b26c 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41  e the bit-wise A
1b26d 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73  ND of the values
1b26e 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1b26f 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74  and P2 and.** st
1b270 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1b271 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1b272 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1b273 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1b274 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1b275 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74  /./* Opcode: Bit
1b276 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  Or P1 P2 P3 * *.
1b277 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62  **.** Take the b
1b278 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68  it-wise OR of th
1b279 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1b27a 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
1b27b 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
1b27c 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1b27d 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1b27e 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1b27f 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1b280 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1b281 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50  ode: ShiftLeft P
1b282 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1b283 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65  * Shift the inte
1b284 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67  ger value in reg
1b285 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20  ister P2 to the 
1b286 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e  left by the.** n
1b287 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70  umber of bits sp
1b288 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69  ecified by the i
1b289 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65  nteger in regise
1b28a 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74  r P1..** Store t
1b28b 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
1b28c 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20  ister P3..** If 
1b28d 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
1b28e 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
1b28f 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
1b290 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67  Opcode: ShiftRig
1b291 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ht P1 P2 P3 * *.
1b292 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20  **.** Shift the 
1b293 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e  integer value in
1b294 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20   register P2 to 
1b295 74 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65  the right by the
1b296 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  .** number of bi
1b297 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ts specified by 
1b298 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72  the integer in r
1b299 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53  egister P1..** S
1b29a 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1b29b 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a  in register P3..
1b29c 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70  ** If either inp
1b29d 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20  ut is NULL, the 
1b29e 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a  result is NULL..
1b29f 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e  */.case OP_BitAn
1b2a0 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d:              
1b2a1 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1b2a2 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e  _BITAND, in1, in
1b2a3 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20  2, out3 */.case 
1b2a4 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20  OP_BitOr:       
1b2a5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1b2a6 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20  me as TK_BITOR, 
1b2a7 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1b2a8 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c  /.case OP_ShiftL
1b2a9 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  eft:            
1b2aa 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1b2ab 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32  LSHIFT, in1, in2
1b2ac 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1b2ad 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20  P_ShiftRight: { 
1b2ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1b2af 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20  e as TK_RSHIFT, 
1b2b0 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1b2b1 2f 0a 20 20 69 36 34 20 61 2c 20 62 3b 0a 0a 20  /.  i64 a, b;.. 
1b2b2 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
1b2b3 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29  s | pIn2->flags)
1b2b4 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1b2b5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1b2b6 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a  mSetNull(pOut);.
1b2b7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
1b2b8 20 61 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65   a = sqlite3Vdbe
1b2b9 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a  IntValue(pIn2);.
1b2ba 20 20 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62    b = sqlite3Vdb
1b2bb 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 3b  eIntValue(pIn1);
1b2bc 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e  .  switch( pOp->
1b2bd 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61  opcode ){.    ca
1b2be 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20  se OP_BitAnd:   
1b2bf 20 20 20 61 20 26 3d 20 62 3b 20 20 20 20 20 62     a &= b;     b
1b2c0 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f  reak;.    case O
1b2c1 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 61  P_BitOr:       a
1b2c2 20 7c 3d 20 62 3b 20 20 20 20 20 62 72 65 61 6b   |= b;     break
1b2c3 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 53 68  ;.    case OP_Sh
1b2c4 69 66 74 4c 65 66 74 3a 20 20 20 61 20 3c 3c 3d  iftLeft:   a <<=
1b2c5 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20   b;    break;.  
1b2c6 20 20 64 65 66 61 75 6c 74 3a 20 20 61 73 73 65    default:  asse
1b2c7 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  rt( pOp->opcode=
1b2c8 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29  =OP_ShiftRight )
1b2c9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1b2ca 20 20 20 20 20 20 20 20 20 20 20 61 20 3e 3e 3d             a >>=
1b2cb 20 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20   b;    break;.  
1b2cc 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  }.  pOut->u.i = 
1b2cd 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  a;.  MemSetTypeF
1b2ce 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1b2cf 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1b2d0 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 49 6d  /* Opcode: AddIm
1b2d1 6d 20 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  m  P1 P2 * * *.*
1b2d2 2a 20 0a 2a 2a 20 41 64 64 20 74 68 65 20 63 6f  * .** Add the co
1b2d3 6e 73 74 61 6e 74 20 50 32 20 74 6f 20 74 68 65  nstant P2 to the
1b2d4 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1b2d5 65 72 20 50 31 2e 0a 2a 2a 20 54 68 65 20 72 65  er P1..** The re
1b2d6 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 61  sult is always a
1b2d7 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a  n integer..**.**
1b2d8 20 54 6f 20 66 6f 72 63 65 20 61 6e 79 20 72 65   To force any re
1b2d9 67 69 73 74 65 72 20 74 6f 20 62 65 20 61 6e 20  gister to be an 
1b2da 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 61 64  integer, just ad
1b2db 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  d 0..*/.case OP_
1b2dc 41 64 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20 20  AddImm: {       
1b2dd 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20       /* in1 */. 
1b2de 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1b2df 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b  ntegerify(pIn1);
1b2e0 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20  .  pIn1->u.i += 
1b2e1 70 4f 70 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b  pOp->p2;.  break
1b2e2 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1b2e3 4d 75 73 74 42 65 49 6e 74 20 50 31 20 50 32 20  MustBeInt P1 P2 
1b2e4 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72  * * *.** .** For
1b2e5 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ce the value in 
1b2e6 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62  register P1 to b
1b2e7 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49  e an integer.  I
1b2e8 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69  f the value.** i
1b2e9 6e 20 50 31 20 69 73 20 6e 6f 74 20 61 6e 20 69  n P1 is not an i
1b2ea 6e 74 65 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f  nteger and canno
1b2eb 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69  t be converted i
1b2ec 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a  nto an integer.*
1b2ed 2a 20 77 69 74 68 6f 75 74 20 64 61 74 61 20 6c  * without data l
1b2ee 6f 73 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69  oss, then jump i
1b2ef 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32  mmediately to P2
1b2f0 2c 20 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a  , or if P2==0.**
1b2f1 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45   raise an SQLITE
1b2f2 5f 4d 49 53 4d 41 54 43 48 20 65 78 63 65 70 74  _MISMATCH except
1b2f3 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
1b2f4 4d 75 73 74 42 65 49 6e 74 3a 20 7b 20 20 20 20  MustBeInt: {    
1b2f5 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1b2f6 20 69 6e 31 20 2a 2f 0a 20 20 61 70 70 6c 79 41   in1 */.  applyA
1b2f7 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51  ffinity(pIn1, SQ
1b2f8 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
1b2f9 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 69  , encoding);.  i
1b2fa 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
1b2fb 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b  & MEM_Int)==0 ){
1b2fc 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32  .    if( pOp->p2
1b2fd 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
1b2fe 3d 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43  = SQLITE_MISMATC
1b2ff 48 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62  H;.      goto ab
1b300 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1b301 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1b302 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1b303 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  - 1;.    }.  }el
1b304 73 65 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  se{.    MemSetTy
1b305 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d  peFlag(pIn1, MEM
1b306 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65  _Int);.  }.  bre
1b307 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1b308 3a 20 52 65 61 6c 41 66 66 69 6e 69 74 79 20 50  : RealAffinity P
1b309 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1b30a 49 66 20 72 65 67 69 73 74 65 72 20 50 31 20 68  If register P1 h
1b30b 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72 20  olds an integer 
1b30c 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20  convert it to a 
1b30d 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  real value..**.*
1b30e 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73  * This opcode is
1b30f 20 75 73 65 64 20 77 68 65 6e 20 65 78 74 72 61   used when extra
1b310 63 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  cting informatio
1b311 6e 20 66 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20  n from a column 
1b312 74 68 61 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c  that.** has REAL
1b313 20 61 66 66 69 6e 69 74 79 2e 20 20 53 75 63 68   affinity.  Such
1b314 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d   column values m
1b315 61 79 20 73 74 69 6c 6c 20 62 65 20 73 74 6f 72  ay still be stor
1b316 65 64 20 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72  ed as.** integer
1b317 73 2c 20 66 6f 72 20 73 70 61 63 65 20 65 66 66  s, for space eff
1b318 69 63 69 65 6e 63 79 2c 20 62 75 74 20 61 66 74  iciency, but aft
1b319 65 72 20 65 78 74 72 61 63 74 69 6f 6e 20 77 65  er extraction we
1b31a 20 77 61 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f   want them.** to
1b31b 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 72 65 61   have only a rea
1b31c 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65  l value..*/.case
1b31d 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79   OP_RealAffinity
1b31e 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1b31f 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20       /* in1 */. 
1b320 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73   if( pIn1->flags
1b321 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20   & MEM_Int ){.  
1b322 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b323 52 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20  Realify(pIn1);. 
1b324 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23   }.  break;.}..#
1b325 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b326 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64  IT_CAST./* Opcod
1b327 65 3a 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a  e: ToText P1 * *
1b328 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65   * *.**.** Force
1b329 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1b32a 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20  gister P1 to be 
1b32b 74 65 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  text..** If the 
1b32c 76 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63  value is numeric
1b32d 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20  , convert it to 
1b32e 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74  a string using t
1b32f 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74  he.** equivalent
1b330 20 6f 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42   of printf().  B
1b331 6c 6f 62 20 76 61 6c 75 65 73 20 61 72 65 20 75  lob values are u
1b332 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20  nchanged and.** 
1b333 61 72 65 20 61 66 74 65 72 77 61 72 64 73 20 73  are afterwards s
1b334 69 6d 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65  imply interprete
1b335 64 20 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a  d as text..**.**
1b336 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73   A NULL value is
1b337 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20   not changed by 
1b338 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49  this routine.  I
1b339 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a  t remains NULL..
1b33a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78  */.case OP_ToTex
1b33b 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  t: {            
1b33c 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1b33d 20 54 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31   TK_TO_TEXT, in1
1b33e 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   */.  if( pIn1->
1b33f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1b340 20 29 20 62 72 65 61 6b 3b 0a 20 20 61 73 73 65   ) break;.  asse
1b341 72 74 28 20 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45  rt( MEM_Str==(ME
1b342 4d 5f 42 6c 6f 62 3e 3e 33 29 20 29 3b 0a 20 20  M_Blob>>3) );.  
1b343 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 3d 20 28  pIn1->flags |= (
1b344 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pIn1->flags&MEM_
1b345 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 61 70 70 6c  Blob)>>3;.  appl
1b346 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20  yAffinity(pIn1, 
1b347 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c  SQLITE_AFF_TEXT,
1b348 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 72 63   encoding);.  rc
1b349 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49   = ExpandBlob(pI
1b34a 6e 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n1);.  assert( p
1b34b 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1b34c 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  _Str || db->mall
1b34d 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 70 49  ocFailed );.  pI
1b34e 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  n1->flags &= ~(M
1b34f 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c  EM_Int|MEM_Real|
1b350 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72  MEM_Blob|MEM_Zer
1b351 6f 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  o);.  UPDATE_MAX
1b352 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b  _BLOBSIZE(pIn1);
1b353 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1b354 4f 70 63 6f 64 65 3a 20 54 6f 42 6c 6f 62 20 50  Opcode: ToBlob P
1b355 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1b356 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20  Force the value 
1b357 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74  in register P1 t
1b358 6f 20 62 65 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20  o be a BLOB..** 
1b359 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
1b35a 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74  numeric, convert
1b35b 20 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20   it to a string 
1b35c 66 69 72 73 74 2e 0a 2a 2a 20 53 74 72 69 6e 67  first..** String
1b35d 73 20 61 72 65 20 73 69 6d 70 6c 79 20 72 65 69  s are simply rei
1b35e 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 62 6c  nterpreted as bl
1b35f 6f 62 73 20 77 69 74 68 20 6e 6f 20 63 68 61 6e  obs with no chan
1b360 67 65 0a 2a 2a 20 74 6f 20 74 68 65 20 75 6e 64  ge.** to the und
1b361 65 72 6c 79 69 6e 67 20 64 61 74 61 2e 0a 2a 2a  erlying data..**
1b362 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65  .** A NULL value
1b363 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20   is not changed 
1b364 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  by this routine.
1b365 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c    It remains NUL
1b366 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f  L..*/.case OP_To
1b367 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 20  Blob: {         
1b368 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1b369 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20   as TK_TO_BLOB, 
1b36a 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e  in1 */.  if( pIn
1b36b 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  1->flags & MEM_N
1b36c 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 69  ull ) break;.  i
1b36d 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
1b36e 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29  & MEM_Blob)==0 )
1b36f 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e  {.    applyAffin
1b370 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45  ity(pIn1, SQLITE
1b371 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64  _AFF_TEXT, encod
1b372 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ing);.    assert
1b373 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
1b374 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d  MEM_Str || db->m
1b375 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
1b376 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1b377 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62  g(pIn1, MEM_Blob
1b378 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1b379 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pIn1->flags &= ~
1b37a 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e 4d  (MEM_TypeMask&~M
1b37b 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20  EM_Blob);.  }.  
1b37c 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1b37d 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65  IZE(pIn1);.  bre
1b37e 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1b37f 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 2a  : ToNumeric P1 *
1b380 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72   * * *.**.** For
1b381 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ce the value in 
1b382 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62  register P1 to b
1b383 65 20 6e 75 6d 65 72 69 63 20 28 65 69 74 68 65  e numeric (eithe
1b384 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20  r an.** integer 
1b385 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f  or a floating-po
1b386 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20  int number.).** 
1b387 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
1b388 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72  text or blob, tr
1b389 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20  y to convert it 
1b38a 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 68 65 0a  to an using the.
1b38b 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66  ** equivalent of
1b38c 20 61 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 28   atoi() or atof(
1b38d 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66  ) and store 0 if
1b38e 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73   no such convers
1b38f 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 69  ion .** is possi
1b390 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c  ble..**.** A NUL
1b391 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63  L value is not c
1b392 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72  hanged by this r
1b393 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61  outine.  It rema
1b394 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73  ins NULL..*/.cas
1b395 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a 20  e OP_ToNumeric: 
1b396 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
1b397 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1b398 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e 31  _TO_NUMERIC, in1
1b399 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d   */.  if( (pIn1-
1b39a 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 4e 75  >flags & (MEM_Nu
1b39b 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  ll|MEM_Int|MEM_R
1b39c 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  eal))==0 ){.    
1b39d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
1b39e 6d 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  merify(pIn1);.  
1b39f 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  }.  break;.}.#en
1b3a0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1b3a1 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f  IT_CAST */../* O
1b3a2 70 63 6f 64 65 3a 20 54 6f 49 6e 74 20 50 31 20  pcode: ToInt P1 
1b3a3 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f  * * * *.**.** Fo
1b3a4 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  rce the value in
1b3a5 20 72 65 67 69 73 74 65 72 20 50 31 20 62 65 20   register P1 be 
1b3a6 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 0a  an integer.  If.
1b3a7 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73 20  ** The value is 
1b3a8 63 75 72 72 65 6e 74 6c 79 20 61 20 72 65 61 6c  currently a real
1b3a9 20 6e 75 6d 62 65 72 2c 20 64 72 6f 70 20 69 74   number, drop it
1b3aa 73 20 66 72 61 63 74 69 6f 6e 61 6c 20 70 61 72  s fractional par
1b3ab 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  t..** If the val
1b3ac 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c  ue is text or bl
1b3ad 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65  ob, try to conve
1b3ae 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74 65  rt it to an inte
1b3af 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  ger using the.**
1b3b0 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61   equivalent of a
1b3b1 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65 20  toi() and store 
1b3b2 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e  0 if no such con
1b3b3 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69  version is possi
1b3b4 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c  ble..**.** A NUL
1b3b5 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63  L value is not c
1b3b6 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72  hanged by this r
1b3b7 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61  outine.  It rema
1b3b8 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73  ins NULL..*/.cas
1b3b9 65 20 4f 50 5f 54 6f 49 6e 74 3a 20 7b 20 20 20  e OP_ToInt: {   
1b3ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b3bb 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
1b3bc 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66  INT, in1 */.  if
1b3bd 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1b3be 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b   MEM_Null)==0 ){
1b3bf 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b3c0 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49  MemIntegerify(pI
1b3c1 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  n1);.  }.  break
1b3c2 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
1b3c3 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a  ITE_OMIT_CAST./*
1b3c4 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20   Opcode: ToReal 
1b3c5 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1b3c6 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
1b3c7 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1b3c8 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67  to be a floating
1b3c9 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a   point number..*
1b3ca 2a 20 49 66 20 54 68 65 20 76 61 6c 75 65 20 69  * If The value i
1b3cb 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 69  s currently an i
1b3cc 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74 20  nteger, convert 
1b3cd 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  it..** If the va
1b3ce 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62  lue is text or b
1b3cf 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76  lob, try to conv
1b3d0 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74  ert it to an int
1b3d1 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a  eger using the.*
1b3d2 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20  * equivalent of 
1b3d3 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65  atoi() and store
1b3d4 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 63 68 20   0.0 if no such 
1b3d5 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f  conversion is po
1b3d6 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  ssible..**.** A 
1b3d7 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f  NULL value is no
1b3d8 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  t changed by thi
1b3d9 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72  s routine.  It r
1b3da 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  emains NULL..*/.
1b3db 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20  case OP_ToReal: 
1b3dc 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
1b3dd 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1b3de 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a 2f  _TO_REAL, in1 */
1b3df 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
1b3e0 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d  ags & MEM_Null)=
1b3e1 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1b3e2 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28  3VdbeMemRealify(
1b3e3 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65  pIn1);.  }.  bre
1b3e4 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1b3e5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
1b3e6 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20   */../* Opcode: 
1b3e7 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Lt P1 P2 P3 P4 P
1b3e8 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  5.**.** Compare 
1b3e9 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65  the values in re
1b3ea 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 33  gister P1 and P3
1b3eb 2e 20 20 49 66 20 72 65 67 28 50 33 29 3c 72 65  .  If reg(P3)<re
1b3ec 67 28 50 31 29 20 74 68 65 6e 0a 2a 2a 20 6a 75  g(P1) then.** ju
1b3ed 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 32  mp to address P2
1b3ee 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  .  .**.** If the
1b3ef 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
1b3f0 4c 4c 20 62 69 74 20 6f 66 20 50 35 20 69 73 20  LL bit of P5 is 
1b3f1 73 65 74 20 61 6e 64 20 65 69 74 68 65 72 20 72  set and either r
1b3f2 65 67 28 50 31 29 20 6f 72 0a 2a 2a 20 72 65 67  eg(P1) or.** reg
1b3f3 28 50 33 29 20 69 73 20 4e 55 4c 4c 20 74 68 65  (P3) is NULL the
1b3f4 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 2e  n take the jump.
1b3f5 20 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f    If the SQLITE_
1b3f6 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62  JUMPIFNULL .** b
1b3f7 69 74 20 69 73 20 63 6c 65 61 72 20 74 68 65 6e  it is clear then
1b3f8 20 66 61 6c 6c 20 74 68 72 75 20 69 66 20 65 69   fall thru if ei
1b3f9 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
1b3fa 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  NULL..**.** The 
1b3fb 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20  SQLITE_AFF_MASK 
1b3fc 70 6f 72 74 69 6f 6e 20 6f 66 20 50 35 20 6d 75  portion of P5 mu
1b3fd 73 74 20 62 65 20 61 6e 20 61 66 66 69 6e 69 74  st be an affinit
1b3fe 79 20 63 68 61 72 61 63 74 65 72 20 2d 0a 2a 2a  y character -.**
1b3ff 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1b400 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  , SQLITE_AFF_INT
1b401 45 47 45 52 2c 20 61 6e 64 20 73 6f 20 66 6f 72  EGER, and so for
1b402 74 68 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 69  th. An attempt i
1b403 73 20 6d 61 64 65 20 0a 2a 2a 20 74 6f 20 63 6f  s made .** to co
1b404 65 72 63 65 20 62 6f 74 68 20 69 6e 70 75 74 73  erce both inputs
1b405 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
1b406 69 73 20 61 66 66 69 6e 69 74 79 20 62 65 66 6f  is affinity befo
1b407 72 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 72  re the.** compar
1b408 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e 20 49 66  ison is made. If
1b409 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f   the SQLITE_AFF_
1b40a 4d 41 53 4b 20 69 73 20 30 78 30 30 2c 20 74 68  MASK is 0x00, th
1b40b 65 6e 20 6e 75 6d 65 72 69 63 0a 2a 2a 20 61 66  en numeric.** af
1b40c 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 2e 20  finity is used. 
1b40d 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 66  Note that the af
1b40e 66 69 6e 69 74 79 20 63 6f 6e 76 65 72 73 69 6f  finity conversio
1b40f 6e 73 20 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a  ns are stored.**
1b410 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 69   back into the i
1b411 6e 70 75 74 20 72 65 67 69 73 74 65 72 73 20 50  nput registers P
1b412 31 20 61 6e 64 20 50 33 2e 20 20 53 6f 20 74 68  1 and P3.  So th
1b413 69 73 20 6f 70 63 6f 64 65 20 63 61 6e 20 63 61  is opcode can ca
1b414 75 73 65 0a 2a 2a 20 70 65 72 73 69 73 74 65 6e  use.** persisten
1b415 74 20 63 68 61 6e 67 65 73 20 74 6f 20 72 65 67  t changes to reg
1b416 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33  isters P1 and P3
1b417 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79  ..**.** Once any
1b418 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61 76   conversions hav
1b419 65 20 74 61 6b 65 6e 20 70 6c 61 63 65 2c 20 61  e taken place, a
1b41a 6e 64 20 6e 65 69 74 68 65 72 20 76 61 6c 75 65  nd neither value
1b41b 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68   is NULL, .** th
1b41c 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6d  e values are com
1b41d 70 61 72 65 64 2e 20 49 66 20 62 6f 74 68 20 76  pared. If both v
1b41e 61 6c 75 65 73 20 61 72 65 20 62 6c 6f 62 73 20  alues are blobs 
1b41f 74 68 65 6e 20 6d 65 6d 63 6d 70 28 29 20 69 73  then memcmp() is
1b420 0a 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65  .** used to dete
1b421 72 6d 69 6e 65 20 74 68 65 20 72 65 73 75 6c 74  rmine the result
1b422 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69  s of the compari
1b423 73 6f 6e 2e 20 20 49 66 20 62 6f 74 68 20 76 61  son.  If both va
1b424 6c 75 65 73 0a 2a 2a 20 61 72 65 20 74 65 78 74  lues.** are text
1b425 2c 20 74 68 65 6e 20 74 68 65 20 61 70 70 72 6f  , then the appro
1b426 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67  priate collating
1b427 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66   function specif
1b428 69 65 64 20 69 6e 0a 2a 2a 20 50 34 20 69 73 20  ied in.** P4 is 
1b429 20 75 73 65 64 20 74 6f 20 64 6f 20 74 68 65 20   used to do the 
1b42a 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 20  comparison.  If 
1b42b 50 34 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  P4 is not specif
1b42c 69 65 64 20 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63  ied then.** memc
1b42d 6d 70 28 29 20 69 73 20 75 73 65 64 20 74 6f 20  mp() is used to 
1b42e 63 6f 6d 70 61 72 65 20 74 65 78 74 20 73 74 72  compare text str
1b42f 69 6e 67 2e 20 20 49 66 20 62 6f 74 68 20 76 61  ing.  If both va
1b430 6c 75 65 73 20 61 72 65 0a 2a 2a 20 6e 75 6d 65  lues are.** nume
1b431 72 69 63 2c 20 74 68 65 6e 20 61 20 6e 75 6d 65  ric, then a nume
1b432 72 69 63 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69  ric comparison i
1b433 73 20 75 73 65 64 2e 20 49 66 20 74 68 65 20 74  s used. If the t
1b434 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65  wo values.** are
1b435 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 74 79   of different ty
1b436 70 65 73 2c 20 74 68 65 6e 20 6e 75 6d 62 65 72  pes, then number
1b437 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64  s are considered
1b438 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 73 74   less than.** st
1b439 72 69 6e 67 73 20 61 6e 64 20 73 74 72 69 6e 67  rings and string
1b43a 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64  s are considered
1b43b 20 6c 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73   less than blobs
1b43c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
1b43d 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 62 69  QLITE_STOREP2 bi
1b43e 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20  t of P5 is set, 
1b43f 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 75 6d 70  then do not jump
1b440 2e 20 20 49 6e 73 74 65 61 64 2c 0a 2a 2a 20 73  .  Instead,.** s
1b441 74 6f 72 65 20 61 20 62 6f 6f 6c 65 61 6e 20 72  tore a boolean r
1b442 65 73 75 6c 74 20 28 65 69 74 68 65 72 20 30 2c  esult (either 0,
1b443 20 6f 72 20 31 2c 20 6f 72 20 4e 55 4c 4c 29 20   or 1, or NULL) 
1b444 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  in register P2..
1b445 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65  */./* Opcode: Ne
1b446 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1b447 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73  **.** This works
1b448 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c   just like the L
1b449 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20  t opcode except 
1b44a 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73  that the jump is
1b44b 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65   taken if.** the
1b44c 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67   operands in reg
1b44d 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33  isters P1 and P3
1b44e 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 2e 20   are not equal. 
1b44f 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f   See the Lt opco
1b450 64 65 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69  de for.** additi
1b451 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1b452 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1b453 45 71 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Eq P1 P2 P3 P4 P
1b454 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  5.**.** This wor
1b455 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  ks just like the
1b456 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70   Lt opcode excep
1b457 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20  t that the jump 
1b458 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74  is taken if.** t
1b459 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72  he operands in r
1b45a 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20  egisters P1 and 
1b45b 50 33 20 61 72 65 20 65 71 75 61 6c 2e 0a 2a 2a  P3 are equal..**
1b45c 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f   See the Lt opco
1b45d 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  de for additiona
1b45e 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
1b45f 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20  /./* Opcode: Le 
1b460 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1b461 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
1b462 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74  just like the Lt
1b463 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
1b464 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
1b465 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20  taken if.** the 
1b466 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73  content of regis
1b467 74 65 72 20 50 33 20 69 73 20 6c 65 73 73 20 74  ter P3 is less t
1b468 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
1b469 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a  the content of.*
1b46a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20  * register P1.  
1b46b 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64  See the Lt opcod
1b46c 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
1b46d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
1b46e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 50  ./* Opcode: Gt P
1b46f 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1b470 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a  .** This works j
1b471 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20  ust like the Lt 
1b472 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68  opcode except th
1b473 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74  at the jump is t
1b474 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63  aken if.** the c
1b475 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74  ontent of regist
1b476 65 72 20 50 33 20 69 73 20 67 72 65 61 74 65 72  er P3 is greater
1b477 20 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e   than the conten
1b478 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72  t of.** register
1b479 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74   P1.  See the Lt
1b47a 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69   opcode for addi
1b47b 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
1b47c 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  on..*/./* Opcode
1b47d 3a 20 47 65 20 50 31 20 50 32 20 50 33 20 50 34  : Ge P1 P2 P3 P4
1b47e 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77   P5.**.** This w
1b47f 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74  orks just like t
1b480 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63  he Lt opcode exc
1b481 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d  ept that the jum
1b482 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a  p is taken if.**
1b483 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
1b484 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 67  register P3 is g
1b485 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
1b486 71 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74  qual to the cont
1b487 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74  ent of.** regist
1b488 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20  er P1.  See the 
1b489 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64  Lt opcode for ad
1b48a 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
1b48b 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
1b48c 5f 45 71 3a 20 20 20 20 20 20 20 20 20 20 20 20  _Eq:            
1b48d 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1b48e 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20  _EQ, jump, in1, 
1b48f 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e  in3 */.case OP_N
1b490 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e:              
1b491 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
1b492 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e  E, jump, in1, in
1b493 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a  3 */.case OP_Lt:
1b494 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b495 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c  * same as TK_LT,
1b496 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
1b497 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20  */.case OP_Le:  
1b498 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b499 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a  same as TK_LE, j
1b49a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
1b49b 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20  .case OP_Gt:    
1b49c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1b49d 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d  me as TK_GT, jum
1b49e 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63  p, in1, in3 */.c
1b49f 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20  ase OP_Ge: {    
1b4a0 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1b4a1 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c   as TK_GE, jump,
1b4a2 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 20 20 69   in1, in3 */.  i
1b4a3 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20  nt flags;.  int 
1b4a4 72 65 73 3b 0a 20 20 63 68 61 72 20 61 66 66 69  res;.  char affi
1b4a5 6e 69 74 79 3b 0a 0a 20 20 66 6c 61 67 73 20 3d  nity;..  flags =
1b4a6 20 70 49 6e 31 2d 3e 66 6c 61 67 73 7c 70 49 6e   pIn1->flags|pIn
1b4a7 33 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 69 66 28  3->flags;..  if(
1b4a8 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20   flags&MEM_Null 
1b4a9 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 65 69 74  ){.    /* If eit
1b4aa 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e  her operand is N
1b4ab 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ULL then the res
1b4ac 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 4e 55  ult is always NU
1b4ad 4c 4c 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6a  LL..    ** The j
1b4ae 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 20  ump is taken if 
1b4af 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  the SQLITE_JUMPI
1b4b0 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 73 65 74  FNULL bit is set
1b4b1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1b4b2 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54   pOp->p5 & SQLIT
1b4b3 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20  E_STOREP2 ){.   
1b4b4 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d     pOut = &p->aM
1b4b5 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20  em[pOp->p2];.   
1b4b6 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1b4b7 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c  g(pOut, MEM_Null
1b4b8 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  );.      REGISTE
1b4b9 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c  R_TRACE(pOp->p2,
1b4ba 20 70 4f 75 74 29 3b 0a 20 20 20 20 7d 65 6c 73   pOut);.    }els
1b4bb 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20  e if( pOp->p5 & 
1b4bc 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
1b4bd 4c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  L ){.      pc = 
1b4be 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 7d  pOp->p2-1;.    }
1b4bf 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1b4c0 0a 20 20 61 66 66 69 6e 69 74 79 20 3d 20 70 4f  .  affinity = pO
1b4c1 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 41  p->p5 & SQLITE_A
1b4c2 46 46 5f 4d 41 53 4b 3b 0a 20 20 69 66 28 20 61  FF_MASK;.  if( a
1b4c3 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 61  ffinity ){.    a
1b4c4 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e  pplyAffinity(pIn
1b4c5 31 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63  1, affinity, enc
1b4c6 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 70 70 6c  oding);.    appl
1b4c7 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c 20  yAffinity(pIn3, 
1b4c8 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69  affinity, encodi
1b4c9 6e 67 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  ng);.    if( db-
1b4ca 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
1b4cb 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
1b4cc 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1b4cd 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c  >p4type==P4_COLL
1b4ce 53 45 51 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e 70  SEQ || pOp->p4.p
1b4cf 43 6f 6c 6c 3d 3d 30 20 29 3b 0a 20 20 45 78 70  Coll==0 );.  Exp
1b4d0 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20  andBlob(pIn1);. 
1b4d1 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33   ExpandBlob(pIn3
1b4d2 29 3b 0a 20 20 72 65 73 20 3d 20 73 71 6c 69 74  );.  res = sqlit
1b4d3 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 49 6e  e3MemCompare(pIn
1b4d4 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e 70 34  3, pIn1, pOp->p4
1b4d5 2e 70 43 6f 6c 6c 29 3b 0a 20 20 73 77 69 74 63  .pColl);.  switc
1b4d6 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29  h( pOp->opcode )
1b4d7 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 45 71  {.    case OP_Eq
1b4d8 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3d 3d  :    res = res==
1b4d9 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  0;     break;.  
1b4da 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20    case OP_Ne:   
1b4db 20 72 65 73 20 3d 20 72 65 73 21 3d 30 3b 20 20   res = res!=0;  
1b4dc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
1b4dd 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20 72 65 73  se OP_Lt:    res
1b4de 20 3d 20 72 65 73 3c 30 3b 20 20 20 20 20 20 62   = res<0;      b
1b4df 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f  reak;.    case O
1b4e0 50 5f 4c 65 3a 20 20 20 20 72 65 73 20 3d 20 72  P_Le:    res = r
1b4e1 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 65 61 6b  es<=0;     break
1b4e2 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 47 74  ;.    case OP_Gt
1b4e3 3a 20 20 20 20 72 65 73 20 3d 20 72 65 73 3e 30  :    res = res>0
1b4e4 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;      break;.  
1b4e5 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20    default:      
1b4e6 20 72 65 73 20 3d 20 72 65 73 3e 3d 30 3b 20 20   res = res>=0;  
1b4e7 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
1b4e8 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53   if( pOp->p5 & S
1b4e9 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b  QLITE_STOREP2 ){
1b4ea 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e  .    pOut = &p->
1b4eb 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1b4ec 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1b4ed 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1b4ee 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20  ;.    pOut->u.i 
1b4ef 3d 20 72 65 73 3b 0a 20 20 20 20 52 45 47 49 53  = res;.    REGIS
1b4f0 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1b4f1 32 2c 20 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73  2, pOut);.  }els
1b4f2 65 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20  e if( res ){.   
1b4f3 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b   pc = pOp->p2-1;
1b4f4 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1b4f5 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d  ./* Opcode: Perm
1b4f6 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34  utation * * * P4
1b4f7 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65   *.**.** Set the
1b4f8 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73 65   permutation use
1b4f9 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f 6d 70  d by the OP_Comp
1b4fa 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  are operator to 
1b4fb 62 65 20 74 68 65 20 61 72 72 61 79 0a 2a 2a 20  be the array.** 
1b4fc 6f 66 20 69 6e 74 65 67 65 72 73 20 69 6e 20 50  of integers in P
1b4fd 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72  4..**.** The per
1b4fe 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  mutation is only
1b4ff 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65   valid until the
1b500 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d 75 74 61   next OP_Permuta
1b501 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 65  tion, OP_Compare
1b502 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f 72  ,.** OP_Halt, or
1b503 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 20 20   OP_ResultRow.  
1b504 54 79 70 69 63 61 6c 6c 79 20 74 68 65 20 4f 50  Typically the OP
1b505 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 73 68 6f  _Permutation sho
1b506 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d  uld occur.** imm
1b507 65 64 69 61 74 65 6c 79 20 70 72 69 6f 72 20 74  ediately prior t
1b508 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65  o the OP_Compare
1b509 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 65 72  ..*/.case OP_Per
1b50a 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 73  mutation: {.  as
1b50b 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70  sert( pOp->p4typ
1b50c 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 59 20 29  e==P4_INTARRAY )
1b50d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1b50e 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 50 65 72  >p4.ai );.  aPer
1b50f 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 61  mute = pOp->p4.a
1b510 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  i;.  break;.}../
1b511 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61 72  * Opcode: Compar
1b512 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  e P1 P2 P3 P4 *.
1b513 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 6f  **.** Compare to
1b514 20 76 65 63 74 6f 72 73 20 6f 66 20 72 65 67 69   vectors of regi
1b515 73 74 65 72 73 20 69 6e 20 72 65 67 28 50 31 29  sters in reg(P1)
1b516 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 29 20 28  ..reg(P1+P3-1) (
1b517 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 20  all this.** one 
1b518 22 41 22 29 20 61 6e 64 20 69 6e 20 72 65 67 28  "A") and in reg(
1b519 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 33 2d 31  P2)..reg(P2+P3-1
1b51a 29 20 28 22 42 22 29 2e 20 20 53 61 76 65 20 74  ) ("B").  Save t
1b51b 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20  he result of.** 
1b51c 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  the comparison f
1b51d 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 6e 65  or use by the ne
1b51e 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 72  xt OP_Jump instr
1b51f 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  uct..**.** P4 is
1b520 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
1b521 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e 65  ture that define
1b522 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  s collating sequ
1b523 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 74 0a 2a  ences and sort.*
1b524 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 74 68 65  * orders for the
1b525 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68   comparison.  Th
1b526 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 70  e permutation ap
1b527 70 6c 69 65 73 20 74 6f 20 72 65 67 69 73 74 65  plies to registe
1b528 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65  rs.** only.  The
1b529 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d 65 6e 74   KeyInfo element
1b52a 73 20 61 72 65 20 75 73 65 64 20 73 65 71 75 65  s are used seque
1b52b 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  ntially..**.** T
1b52c 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  he comparison is
1b52d 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69 73   a sort comparis
1b52e 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d  on, so NULLs com
1b52f 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e  pare equal,.** N
1b530 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74 68  ULLs are less th
1b531 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d 62  an numbers, numb
1b532 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68 61  ers are less tha
1b533 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e  n strings,.** an
1b534 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c 65  d strings are le
1b535 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a  ss than blobs..*
1b536 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d 70 61 72  /.case OP_Compar
1b537 65 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 70  e: {.  int n = p
1b538 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 20 69 2c  Op->p3;.  int i,
1b539 20 70 31 2c 20 70 32 3b 0a 20 20 63 6f 6e 73 74   p1, p2;.  const
1b53a 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
1b53b 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65  fo = pOp->p4.pKe
1b53c 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72 74 28  yInfo;.  assert(
1b53d 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74   n>0 );.  assert
1b53e 28 20 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b  ( pKeyInfo!=0 );
1b53f 0a 20 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b  .  p1 = pOp->p1;
1b540 0a 20 20 61 73 73 65 72 74 28 20 70 31 3e 30 20  .  assert( p1>0 
1b541 26 26 20 70 31 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d  && p1+n<=p->nMem
1b542 2b 31 20 29 3b 0a 20 20 70 32 20 3d 20 70 4f 70  +1 );.  p2 = pOp
1b543 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p2;.  assert( 
1b544 70 32 3e 30 20 26 26 20 70 32 2b 6e 3c 3d 70 2d  p2>0 && p2+n<=p-
1b545 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 66 6f 72  >nMem+1 );.  for
1b546 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b  (i=0; i<n; i++){
1b547 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 61  .    int idx = a
1b548 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d 75  Permute ? aPermu
1b549 74 65 5b 69 5d 20 3a 20 69 3b 0a 20 20 20 20 43  te[i] : i;.    C
1b54a 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20  ollSeq *pColl;  
1b54b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73    /* Collating s
1b54c 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 20 6f  equence to use o
1b54d 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20  n this term */. 
1b54e 20 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20     int bRev;    
1b54f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
1b550 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f 72  r DESCENDING sor
1b551 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 52  t order */.    R
1b552 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 31  EGISTER_TRACE(p1
1b553 2b 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70  +idx, &p->aMem[p
1b554 31 2b 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47  1+idx]);.    REG
1b555 49 53 54 45 52 5f 54 52 41 43 45 28 70 32 2b 69  ISTER_TRACE(p2+i
1b556 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 2b  dx, &p->aMem[p2+
1b557 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65 72  idx]);.    asser
1b558 74 28 20 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e  t( i<pKeyInfo->n
1b559 46 69 65 6c 64 20 29 3b 0a 20 20 20 20 70 43 6f  Field );.    pCo
1b55a 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ll = pKeyInfo->a
1b55b 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 62 52 65  Coll[i];.    bRe
1b55c 76 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53  v = pKeyInfo->aS
1b55d 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20  ortOrder[i];.   
1b55e 20 69 43 6f 6d 70 61 72 65 20 3d 20 73 71 6c 69   iCompare = sqli
1b55f 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 70  te3MemCompare(&p
1b560 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d 2c 20  ->aMem[p1+idx], 
1b561 26 70 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78 5d  &p->aMem[p2+idx]
1b562 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66  , pColl);.    if
1b563 28 20 69 43 6f 6d 70 61 72 65 20 29 7b 0a 20 20  ( iCompare ){.  
1b564 20 20 20 20 69 66 28 20 62 52 65 76 20 29 20 69      if( bRev ) i
1b565 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d 70  Compare = -iComp
1b566 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  are;.      break
1b567 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 50  ;.    }.  }.  aP
1b568 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 72  ermute = 0;.  br
1b569 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1b56a 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50 33  e: Jump P1 P2 P3
1b56b 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20   * *.**.** Jump 
1b56c 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69  to the instructi
1b56d 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 31  on at address P1
1b56e 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 65  , P2, or P3 depe
1b56f 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72  nding on whether
1b570 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 20  .** in the most 
1b571 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 72  recent OP_Compar
1b572 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  e instruction th
1b573 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 20  e P1 vector was 
1b574 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 75  less than.** equ
1b575 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65  al to, or greate
1b576 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76 65  r than the P2 ve
1b577 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76 65  ctor, respective
1b578 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4a  ly..*/.case OP_J
1b579 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ump: {          
1b57a 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20     /* jump */.  
1b57b 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 29  if( iCompare<0 )
1b57c 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  {.    pc = pOp->
1b57d 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 20  p1 - 1;.  }else 
1b57e 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 20  if( iCompare==0 
1b57f 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1b580 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p2 - 1;.  }else
1b581 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  {.    pc = pOp->
1b582 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p3 - 1;.  }.  br
1b583 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1b584 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33 20  e: And P1 P2 P3 
1b585 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74  * *.**.** Take t
1b586 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 6f  he logical AND o
1b587 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20  f the values in 
1b588 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64  registers P1 and
1b589 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 65   P2 and.** write
1b58a 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f   the result into
1b58b 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
1b58c 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 31  .** If either P1
1b58d 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61 6c   or P2 is 0 (fal
1b58e 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 73  se) then the res
1b58f 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69 66  ult is 0 even if
1b590 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 6e  .** the other in
1b591 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20  put is NULL.  A 
1b592 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f 72  NULL and true or
1b593 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 0a   two NULLs give.
1b594 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74  ** a NULL output
1b595 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1b596 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  Or P1 P2 P3 * *.
1b597 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c  **.** Take the l
1b598 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 65  ogical OR of the
1b599 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73   values in regis
1b59a 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e  ter P1 and P2 an
1b59b 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 61  d.** store the a
1b59c 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74 65  nswer in registe
1b59d 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65  r P3..**.** If e
1b59e 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 69  ither P1 or P2 i
1b59f 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 29  s nonzero (true)
1b5a0 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
1b5a1 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a 20   is 1 (true).** 
1b5a2 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68 65  even if the othe
1b5a3 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e  r input is NULL.
1b5a4 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 6c    A NULL and fal
1b5a5 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 0a  se or two NULLs.
1b5a6 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 6f  ** give a NULL o
1b5a7 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f  utput..*/.case O
1b5a8 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20  P_And:          
1b5a9 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1b5aa 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c  K_AND, in1, in2,
1b5ab 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50   out3 */.case OP
1b5ac 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20  _Or: {          
1b5ad 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1b5ae 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  _OR, in1, in2, o
1b5af 75 74 33 20 2a 2f 0a 20 20 69 6e 74 20 76 31 2c  ut3 */.  int v1,
1b5b0 20 76 32 3b 20 20 20 20 2f 2a 20 30 3d 3d 46 41   v2;    /* 0==FA
1b5b1 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d  LSE, 1==TRUE, 2=
1b5b2 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c  =UNKNOWN or NULL
1b5b3 20 2a 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 2d   */..  if( pIn1-
1b5b4 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1b5b5 6c 20 29 7b 0a 20 20 20 20 76 31 20 3d 20 32 3b  l ){.    v1 = 2;
1b5b6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 31  .  }else{.    v1
1b5b7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
1b5b8 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b  tValue(pIn1)!=0;
1b5b9 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 2d  .  }.  if( pIn2-
1b5ba 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1b5bb 6c 20 29 7b 0a 20 20 20 20 76 32 20 3d 20 32 3b  l ){.    v2 = 2;
1b5bc 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 32  .  }else{.    v2
1b5bd 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
1b5be 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d 30 3b  tValue(pIn2)!=0;
1b5bf 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e  .  }.  if( pOp->
1b5c0 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20 29  opcode==OP_And )
1b5c1 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
1b5c2 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1b5c3 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b   and_logic[] = {
1b5c4 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20   0, 0, 0, 0, 1, 
1b5c5 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20 20  2, 0, 2, 2 };.  
1b5c6 20 20 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63    v1 = and_logic
1b5c7 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 65 6c  [v1*3+v2];.  }el
1b5c8 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63  se{.    static c
1b5c9 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1b5ca 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d 20  ar or_logic[] = 
1b5cb 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 2c  { 0, 1, 2, 1, 1,
1b5cc 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a 20   1, 2, 1, 2 };. 
1b5cd 20 20 20 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63     v1 = or_logic
1b5ce 5b 76 31 2a 33 2b 76 32 5d 3b 0a 20 20 7d 0a 20  [v1*3+v2];.  }. 
1b5cf 20 69 66 28 20 76 31 3d 3d 32 20 29 7b 0a 20 20   if( v1==2 ){.  
1b5d0 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1b5d1 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  (pOut, MEM_Null)
1b5d2 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1b5d3 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 31 3b 0a 20  Out->u.i = v1;. 
1b5d4 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1b5d5 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1b5d6 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1b5d7 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74  ../* Opcode: Not
1b5d8 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1b5d9 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65  ** Interpret the
1b5da 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1b5db 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f 6c 65  er P1 as a boole
1b5dc 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f 72 65  an value.  Store
1b5dd 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 20   the.** boolean 
1b5de 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20 72 65  complement in re
1b5df 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 74  gister P2.  If t
1b5e0 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1b5e1 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a 20 4e  ster P1 is .** N
1b5e2 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c  ULL, then a NULL
1b5e3 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 50 32   is stored in P2
1b5e4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74  ..*/.case OP_Not
1b5e5 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1b5e6 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1b5e7 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 70  _NOT, in1 */.  p
1b5e8 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
1b5e9 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 70  Op->p2];.  if( p
1b5ea 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1b5eb 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c  _Null ){.    sql
1b5ec 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1b5ed 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73  ll(pOut);.  }els
1b5ee 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  e{.    sqlite3Vd
1b5ef 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f  beMemSetInt64(pO
1b5f0 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62 65  ut, !sqlite3Vdbe
1b5f1 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3b  IntValue(pIn1));
1b5f2 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1b5f3 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4e  ./* Opcode: BitN
1b5f4 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ot P1 P2 * * *.*
1b5f5 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
1b5f6 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1b5f7 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e 20  gister P1 as an 
1b5f8 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 65 20  integer.  Store 
1b5f9 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d 70  the.** ones-comp
1b5fa 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 50 31  lement of the P1
1b5fb 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69   value into regi
1b5fc 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 31 20  ster P2.  If P1 
1b5fd 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20  holds.** a NULL 
1b5fe 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e 55 4c  then store a NUL
1b5ff 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  L in P2..*/.case
1b600 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 20   OP_BitNot: {   
1b601 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1b602 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c 20  e as TK_BITNOT, 
1b603 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d 20  in1 */.  pOut = 
1b604 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1b605 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  ];.  if( pIn1->f
1b606 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1b607 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1b608 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75  beMemSetNull(pOu
1b609 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
1b60a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1b60b 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 7e 73  etInt64(pOut, ~s
1b60c 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1b60d 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20  ue(pIn1));.  }. 
1b60e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1b60f 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 20 50  code: If P1 P2 P
1b610 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1b611 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61   to P2 if the va
1b612 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1b613 50 31 20 69 73 20 74 72 75 65 2e 20 20 54 68 65  P1 is true.  The
1b614 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20   value is.** is 
1b615 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 65 20  considered true 
1b616 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72 69 63  if it is numeric
1b617 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20   and non-zero.  
1b618 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  If the value.** 
1b619 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 68  in P1 is NULL th
1b61a 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  en take the jump
1b61b 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e 0a   if P3 is true..
1b61c 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66  */./* Opcode: If
1b61d 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a  Not P1 P2 P3 * *
1b61e 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50  .**.** Jump to P
1b61f 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69  2 if the value i
1b620 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  n register P1 is
1b621 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76 61 6c   False.  The val
1b622 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73  ue is.** is cons
1b623 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20 69  idered true if i
1b624 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69 63 20  t has a numeric 
1b625 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e 20 20  value of zero.  
1b626 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  If the value.** 
1b627 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 68  in P1 is NULL th
1b628 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  en take the jump
1b629 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e 0a   if P3 is true..
1b62a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a 20 20  */.case OP_If:  
1b62b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b62c 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 63  * jump, in1 */.c
1b62d 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20  ase OP_IfNot: { 
1b62e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
1b62f 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74  mp, in1 */.  int
1b630 20 63 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   c;.  if( pIn1->
1b631 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1b632 20 29 7b 0a 20 20 20 20 63 20 3d 20 70 4f 70 2d   ){.    c = pOp-
1b633 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69  >p3;.  }else{.#i
1b634 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1b635 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
1b636 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
1b637 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31  dbeIntValue(pIn1
1b638 29 21 3d 30 3b 0a 23 65 6c 73 65 0a 20 20 20 20  )!=0;.#else.    
1b639 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  c = sqlite3VdbeR
1b63a 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 3d  ealValue(pIn1)!=
1b63b 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  0.0;.#endif.    
1b63c 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1b63d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 63 20 3d 20  =OP_IfNot ) c = 
1b63e 21 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 20  !c;.  }.  if( c 
1b63f 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1b640 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65  >p2-1;.  }.  bre
1b641 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1b642 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50 32 20 50  : IsNull P1 P2 P
1b643 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1b644 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61   to P2 if the va
1b645 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1b646 50 31 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20  P1 is NULL.  If 
1b647 50 33 20 69 73 20 67 72 65 61 74 65 72 0a 2a 2a  P3 is greater.**
1b648 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e   than zero, then
1b649 20 63 68 65 63 6b 20 61 6c 6c 20 76 61 6c 75 65   check all value
1b64a 73 20 72 65 67 28 50 31 29 2c 20 72 65 67 28 50  s reg(P1), reg(P
1b64b 31 2b 31 29 2c 20 0a 2a 2a 20 72 65 67 28 50 31  1+1), .** reg(P1
1b64c 2b 32 29 2c 20 2e 2e 2e 2c 20 72 65 67 28 50 31  +2), ..., reg(P1
1b64d 2b 50 33 2d 31 29 2e 0a 2a 2f 0a 63 61 73 65 20  +P3-1)..*/.case 
1b64e 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20  OP_IsNull: {    
1b64f 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1b650 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75  as TK_ISNULL, ju
1b651 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74  mp, in1 */.  int
1b652 20 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20   n = pOp->p3;.  
1b653 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d  assert( pOp->p3=
1b654 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 31 3e 30 20  =0 || pOp->p1>0 
1b655 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 66 28  );.  do{.    if(
1b656 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1b657 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a  MEM_Null)!=0 ){.
1b658 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1b659 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 72  p2 - 1;.      br
1b65a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  eak;.    }.    p
1b65b 49 6e 31 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28  In1++;.  }while(
1b65c 20 2d 2d 6e 20 3e 20 30 20 29 3b 0a 20 20 62 72   --n > 0 );.  br
1b65d 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1b65e 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 20 50 32  e: NotNull P1 P2
1b65f 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d   * * *.**.** Jum
1b660 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76  p to P2 if the v
1b661 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1b662 20 50 31 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e   P1 is not NULL.
1b663 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f    .*/.case OP_No
1b664 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20  tNull: {        
1b665 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1b666 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70 2c  K_NOTNULL, jump,
1b667 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70   in1 */.  if( (p
1b668 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1b669 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20  _Null)==0 ){.   
1b66a 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1b66b 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1b66c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  }../* Opcode: Se
1b66d 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 2a 20 50 32  tNumColumns * P2
1b66e 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69   * * *.**.** Thi
1b66f 73 20 6f 70 63 6f 64 65 20 73 65 74 73 20 74 68  s opcode sets th
1b670 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
1b671 6d 6e 73 20 66 6f 72 20 74 68 65 20 63 75 72 73  mns for the curs
1b672 6f 72 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65  or opened by the
1b673 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  .** following in
1b674 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 50 32 2e  struction to P2.
1b675 0a 2a 2a 0a 2a 2a 20 41 6e 20 4f 50 5f 53 65 74  .**.** An OP_Set
1b676 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 73 20 6f 6e  NumColumns is on
1b677 6c 79 20 75 73 65 66 75 6c 20 69 66 20 69 74 20  ly useful if it 
1b678 6f 63 63 75 72 73 20 69 6d 6d 65 64 69 61 74 65  occurs immediate
1b679 6c 79 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6f 6e  ly before .** on
1b67a 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
1b67b 6e 67 20 6f 70 63 6f 64 65 73 3a 0a 2a 2a 0a 2a  ng opcodes:.**.*
1b67c 2a 20 20 20 20 20 4f 70 65 6e 52 65 61 64 0a 2a  *     OpenRead.*
1b67d 2a 20 20 20 20 20 4f 70 65 6e 57 72 69 74 65 0a  *     OpenWrite.
1b67e 2a 2a 20 20 20 20 20 4f 70 65 6e 50 73 65 75 64  **     OpenPseud
1b67f 6f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f  o.**.** If the O
1b680 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 20  P_Column opcode 
1b681 69 73 20 74 6f 20 62 65 20 65 78 65 63 75 74 65  is to be execute
1b682 64 20 6f 6e 20 61 20 63 75 72 73 6f 72 2c 20 74  d on a cursor, t
1b683 68 65 6e 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f  hen.** this opco
1b684 64 65 20 6d 75 73 74 20 62 65 20 70 72 65 73 65  de must be prese
1b685 6e 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62  nt immediately b
1b686 65 66 6f 72 65 20 74 68 65 20 6f 70 63 6f 64 65  efore the opcode
1b687 20 74 68 61 74 0a 2a 2a 20 6f 70 65 6e 73 20 74   that.** opens t
1b688 68 65 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 23 69  he cursor..*/.#i
1b689 66 20 30 0a 63 61 73 65 20 4f 50 5f 53 65 74 4e  f 0.case OP_SetN
1b68a 75 6d 43 6f 6c 75 6d 6e 73 3a 20 7b 0a 20 20 62  umColumns: {.  b
1b68b 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  reak;.}.#endif..
1b68c 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d  /* Opcode: Colum
1b68d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  n P1 P2 P3 P4 *.
1b68e 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  **.** Interpret 
1b68f 74 68 65 20 64 61 74 61 20 74 68 61 74 20 63 75  the data that cu
1b690 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74  rsor P1 points t
1b691 6f 20 61 73 20 61 20 73 74 72 75 63 74 75 72 65  o as a structure
1b692 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20   built using.** 
1b693 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69  the MakeRecord i
1b694 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65  nstruction.  (Se
1b695 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64  e the MakeRecord
1b696 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69   opcode for addi
1b697 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  tional.** inform
1b698 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
1b699 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61  format of the da
1b69a 74 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68  ta.)  Extract th
1b69b 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a  e P2-th column.*
1b69c 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f  * from this reco
1b69d 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  rd.  If there ar
1b69e 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b  e less that (P2+
1b69f 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e  1) .** values in
1b6a0 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 74   the record, ext
1b6a1 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ract a NULL..**.
1b6a2 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 74  ** The value ext
1b6a3 72 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 64  racted is stored
1b6a4 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1b6a5 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f  .**.** If the co
1b6a6 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65  lumn contains fe
1b6a7 77 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 6c  wer than P2 fiel
1b6a8 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 74  ds, then extract
1b6a9 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a   a NULL.  Or,.**
1b6aa 20 69 66 20 74 68 65 20 50 34 20 61 72 67 75 6d   if the P4 argum
1b6ab 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d 20  ent is a P4_MEM 
1b6ac 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  use the value of
1b6ad 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74   the P4 argument
1b6ae 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c   as.** the resul
1b6af 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f  t..*/.case OP_Co
1b6b0 6c 75 6d 6e 3a 20 7b 0a 20 20 69 6e 74 20 70 61  lumn: {.  int pa
1b6b1 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20  yloadSize;   /* 
1b6b2 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1b6b3 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
1b6b4 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d  .  int p1 = pOp-
1b6b5 3e 70 31 3b 20 20 2f 2a 20 50 31 20 76 61 6c 75  >p1;  /* P1 valu
1b6b6 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20  e of the opcode 
1b6b7 2a 2f 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 4f  */.  int p2 = pO
1b6b8 70 2d 3e 70 32 3b 20 20 2f 2a 20 63 6f 6c 75 6d  p->p2;  /* colum
1b6b9 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72  n number to retr
1b6ba 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 75  ieve */.  VdbeCu
1b6bb 72 73 6f 72 20 2a 70 43 20 3d 20 30 3b 2f 2a 20  rsor *pC = 0;/* 
1b6bc 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
1b6bd 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 3b  */.  char *zRec;
1b6be 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
1b6bf 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72  er to complete r
1b6c0 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20  ecord-data */.  
1b6c1 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1b6c2 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20     /* The BTree 
1b6c3 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 20  cursor */.  u32 
1b6c4 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f  *aType;        /
1b6c5 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73  * aType[i] holds
1b6c6 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70   the numeric typ
1b6c7 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f  e of the i-th co
1b6c8 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61  lumn */.  u32 *a
1b6c9 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  Offset;      /* 
1b6ca 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66  aOffset[i] is of
1b6cb 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66  fset to start of
1b6cc 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63   data for i-th c
1b6cd 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  olumn */.  int n
1b6ce 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a  Field;        /*
1b6cf 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64   number of field
1b6d0 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
1b6d1 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20  */.  int len;   
1b6d2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c          /* The l
1b6d3 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 72  ength of the ser
1b6d4 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72  ialized data for
1b6d5 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20   the column */. 
1b6d6 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1b6d7 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1b6d8 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ter */.  char *z
1b6d9 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50  Data;       /* P
1b6da 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72  art of the recor
1b6db 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20  d being decoded 
1b6dc 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b  */.  Mem *pDest;
1b6dd 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65          /* Where
1b6de 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 78   to write the ex
1b6df 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f  tracted value */
1b6e0 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20  .  Mem sMem;    
1b6e1 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f        /* For sto
1b6e2 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20  ring the record 
1b6e3 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f  being decoded */
1b6e4 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4d 65 6d  ..  memset(&sMem
1b6e5 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4d 65 6d  , 0, sizeof(sMem
1b6e6 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31  ));.  assert( p1
1b6e7 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1b6e8 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1b6e9 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70  >0 && pOp->p3<=p
1b6ea 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 44 65 73  ->nMem );.  pDes
1b6eb 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
1b6ec 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53 65 74 54  ->p3];.  MemSetT
1b6ed 79 70 65 46 6c 61 67 28 70 44 65 73 74 2c 20 4d  ypeFlag(pDest, M
1b6ee 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 2f 2a 20  EM_Null);..  /* 
1b6ef 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74 73 20  This block sets 
1b6f0 74 68 65 20 76 61 72 69 61 62 6c 65 20 70 61 79  the variable pay
1b6f1 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74  loadSize to be t
1b6f2 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
1b6f3 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e  of.  ** bytes in
1b6f4 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a   the record..  *
1b6f5 2a 0a 20 20 2a 2a 20 7a 52 65 63 20 69 73 20 73  *.  ** zRec is s
1b6f6 65 74 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6d  et to be the com
1b6f7 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
1b6f8 65 20 72 65 63 6f 72 64 20 69 66 20 69 74 20 69  e record if it i
1b6f9 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a  s available..  *
1b6fa 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 72  * The complete r
1b6fb 65 63 6f 72 64 20 74 65 78 74 20 69 73 20 61 6c  ecord text is al
1b6fc 77 61 79 73 20 61 76 61 69 6c 61 62 6c 65 20 66  ways available f
1b6fd 6f 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73  or pseudo-tables
1b6fe 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 63  .  ** If the rec
1b6ff 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ord is stored in
1b700 20 61 20 63 75 72 73 6f 72 2c 20 74 68 65 20 63   a cursor, the c
1b701 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 20 74  omplete record t
1b702 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62  ext.  ** might b
1b703 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74  e available in t
1b704 68 65 20 20 70 43 2d 3e 61 52 6f 77 20 63 61 63  he  pC->aRow cac
1b705 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68 74  he.  Or it might
1b706 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49 66   not be..  ** If
1b707 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e 61   the data is una
1b708 76 61 69 6c 61 62 6c 65 2c 20 20 7a 52 65 63 20  vailable,  zRec 
1b709 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a  is set to NULL..
1b70a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73    **.  ** We als
1b70b 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 75  o compute the nu
1b70c 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1b70d 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20 20  in the record.  
1b70e 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a  For cursors,.  *
1b70f 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
1b710 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 65  columns is store
1b711 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75 72  d in the VdbeCur
1b712 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65  sor.nField eleme
1b713 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 20 3d 20  nt..  */.  pC = 
1b714 70 2d 3e 61 70 43 73 72 5b 70 31 5d 3b 0a 20 20  p->apCsr[p1];.  
1b715 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b  assert( pC!=0 );
1b716 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b717 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1b718 45 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e  E.  assert( pC->
1b719 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20 29  pVtabCursor==0 )
1b71a 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  ;.#endif.  if( p
1b71b 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b  C->pCursor!=0 ){
1b71c 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f  .    /* The reco
1b71d 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  rd is stored in 
1b71e 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20 20  a B-Tree */.    
1b71f 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1b720 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29  CursorMoveto(pC)
1b721 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
1b722 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1b723 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7a 52 65 63  _error;.    zRec
1b724 20 3d 20 30 3b 0a 20 20 20 20 70 43 72 73 72 20   = 0;.    pCrsr 
1b725 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  = pC->pCursor;. 
1b726 20 20 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52     if( pC->nullR
1b727 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 61 79 6c  ow ){.      payl
1b728 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20  oadSize = 0;.   
1b729 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 63   }else if( pC->c
1b72a 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63  acheStatus==p->c
1b72b 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20  acheCtr ){.     
1b72c 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70   payloadSize = p
1b72d 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a  C->payloadSize;.
1b72e 20 20 20 20 20 20 7a 52 65 63 20 3d 20 28 63 68        zRec = (ch
1b72f 61 72 2a 29 70 43 2d 3e 61 52 6f 77 3b 0a 20 20  ar*)pC->aRow;.  
1b730 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e    }else if( pC->
1b731 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20  isIndex ){.     
1b732 20 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a 65   i64 payloadSize
1b733 36 34 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  64;.      sqlite
1b734 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43  3BtreeKeySize(pC
1b735 72 73 72 2c 20 26 70 61 79 6c 6f 61 64 53 69 7a  rsr, &payloadSiz
1b736 65 36 34 29 3b 0a 20 20 20 20 20 20 70 61 79 6c  e64);.      payl
1b737 6f 61 64 53 69 7a 65 20 3d 20 28 69 6e 74 29 70  oadSize = (int)p
1b738 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20  ayloadSize64;.  
1b739 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
1b73a 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53  qlite3BtreeDataS
1b73b 69 7a 65 28 70 43 72 73 72 2c 20 28 75 33 32 20  ize(pCrsr, (u32 
1b73c 2a 29 26 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b  *)&payloadSize);
1b73d 0a 20 20 20 20 7d 0a 20 20 20 20 6e 46 69 65 6c  .    }.    nFiel
1b73e 64 20 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a  d = pC->nField;.
1b73f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1b740 65 72 74 28 20 70 43 2d 3e 70 73 65 75 64 6f 54  ert( pC->pseudoT
1b741 61 62 6c 65 20 29 3b 0a 20 20 20 20 2f 2a 20 54  able );.    /* T
1b742 68 65 20 72 65 63 6f 72 64 20 69 73 20 74 68 65  he record is the
1b743 20 73 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20 61   sole entry of a
1b744 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f   pseudo-table */
1b745 0a 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65  .    payloadSize
1b746 20 3d 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20 20   = pC->nData;.  
1b747 20 20 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44 61    zRec = pC->pDa
1b748 74 61 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68  ta;.    pC->cach
1b749 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1b74a 53 54 41 4c 45 3b 0a 20 20 20 20 61 73 73 65 72  STALE;.    asser
1b74b 74 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d  t( payloadSize==
1b74c 30 20 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b 0a  0 || zRec!=0 );.
1b74d 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 2d      nField = pC-
1b74e 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 43 72  >nField;.    pCr
1b74f 73 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  sr = 0;.  }..  /
1b750 2a 20 49 66 20 70 61 79 6c 6f 61 64 53 69 7a 65  * If payloadSize
1b751 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 74   is 0, then just
1b752 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a 2f   store a NULL */
1b753 0a 20 20 69 66 28 20 70 61 79 6c 6f 61 64 53 69  .  if( payloadSi
1b754 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  ze==0 ){.    ass
1b755 65 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67  ert( pDest->flag
1b756 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20  s&MEM_Null );.  
1b757 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e    goto op_column
1b758 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  _out;.  }.  if( 
1b759 70 61 79 6c 6f 61 64 53 69 7a 65 3e 64 62 2d 3e  payloadSize>db->
1b75a 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1b75b 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
1b75c 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1b75d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
1b75e 70 32 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20  p2<nField );..  
1b75f 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 72 73  /* Read and pars
1b760 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 61 64  e the table head
1b761 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72  er.  Store the r
1b762 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 70 61  esults of the pa
1b763 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  rse.  ** into th
1b764 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20  e record header 
1b765 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f 66 20  cache fields of 
1b766 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f  the cursor..  */
1b767 0a 20 20 61 54 79 70 65 20 3d 20 70 43 2d 3e 61  .  aType = pC->a
1b768 54 79 70 65 3b 0a 20 20 69 66 28 20 70 43 2d 3e  Type;.  if( pC->
1b769 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e  cacheStatus==p->
1b76a 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20  cacheCtr ){.    
1b76b 61 4f 66 66 73 65 74 20 3d 20 70 43 2d 3e 61 4f  aOffset = pC->aO
1b76c 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ffset;.  }else{.
1b76d 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20      u8 *zIdx;   
1b76e 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
1b76f 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20  to header */.   
1b770 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20   u8 *zEndHdr;   
1b771 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
1b772 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72  first byte after
1b773 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20   the header */. 
1b774 20 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20     int offset;  
1b775 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
1b776 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  to the data */. 
1b777 20 20 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b 20     int szHdrSz; 
1b778 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
1b779 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 66  he header size f
1b77a 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66  ield at start of
1b77b 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69   record */.    i
1b77c 6e 74 20 61 76 61 69 6c 20 3d 20 30 3b 20 20 20  nt avail = 0;   
1b77d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1b77e 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20  es of available 
1b77f 64 61 74 61 20 2a 2f 0a 0a 20 20 20 20 61 73 73  data */..    ass
1b780 65 72 74 28 61 54 79 70 65 29 3b 0a 20 20 20 20  ert(aType);.    
1b781 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 4f  pC->aOffset = aO
1b782 66 66 73 65 74 20 3d 20 26 61 54 79 70 65 5b 6e  ffset = &aType[n
1b783 46 69 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d 3e  Field];.    pC->
1b784 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61  payloadSize = pa
1b785 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 70  yloadSize;.    p
1b786 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1b787 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20   p->cacheCtr;.. 
1b788 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
1b789 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20   how many bytes 
1b78a 61 72 65 20 69 6e 20 74 68 65 20 68 65 61 64 65  are in the heade
1b78b 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 65  r */.    if( zRe
1b78c 63 20 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61  c ){.      zData
1b78d 20 3d 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c   = zRec;.    }el
1b78e 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43  se{.      if( pC
1b78f 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20  ->isIndex ){.   
1b790 20 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68       zData = (ch
1b791 61 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65  ar*)sqlite3Btree
1b792 4b 65 79 46 65 74 63 68 28 70 43 72 73 72 2c 20  KeyFetch(pCrsr, 
1b793 26 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d  &avail);.      }
1b794 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 44  else{.        zD
1b795 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  ata = (char*)sql
1b796 69 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74  ite3BtreeDataFet
1b797 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c  ch(pCrsr, &avail
1b798 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1b799 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28   /* If KeyFetch(
1b79a 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61  )/DataFetch() ma
1b79b 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65  naged to get the
1b79c 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c   entire payload,
1b79d 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74  .      ** save t
1b79e 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68  he payload in th
1b79f 65 20 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65  e pC->aRow cache
1b7a0 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76  .  That will sav
1b7a1 65 20 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20  e us from.      
1b7a2 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b  ** having to mak
1b7a3 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c  e additional cal
1b7a4 6c 73 20 74 6f 20 66 65 74 63 68 20 74 68 65 20  ls to fetch the 
1b7a5 63 6f 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20  content portion 
1b7a6 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  of.      ** the 
1b7a7 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f  record..      */
1b7a8 0a 20 20 20 20 20 20 69 66 28 20 61 76 61 69 6c  .      if( avail
1b7a9 3e 3d 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b  >=payloadSize ){
1b7aa 0a 20 20 20 20 20 20 20 20 7a 52 65 63 20 3d 20  .        zRec = 
1b7ab 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70  zData;.        p
1b7ac 43 2d 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 7a  C->aRow = (u8*)z
1b7ad 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Data;.      }els
1b7ae 65 7b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61  e{.        pC->a
1b7af 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  Row = 0;.      }
1b7b0 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68  .    }.    /* Th
1b7b1 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
1b7b2 72 74 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c  rt is true in al
1b7b3 6c 20 63 61 73 65 73 20 61 63 63 65 70 74 20 77  l cases accept w
1b7b4 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64  hen.    ** the d
1b7b5 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
1b7b6 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20   been corrupted 
1b7b7 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20  externally..    
1b7b8 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 7a 52  **    assert( zR
1b7b9 65 63 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e 3d  ec!=0 || avail>=
1b7ba 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 61  payloadSize || a
1b7bb 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20  vail>=9 ); */.  
1b7bc 20 20 73 7a 48 64 72 53 7a 20 3d 20 67 65 74 56    szHdrSz = getV
1b7bd 61 72 69 6e 74 33 32 28 28 75 38 2a 29 7a 44 61  arint32((u8*)zDa
1b7be 74 61 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20  ta, offset);..  
1b7bf 20 20 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63    /* The KeyFetc
1b7c0 68 28 29 20 6f 72 20 44 61 74 61 46 65 74 63 68  h() or DataFetch
1b7c1 28 29 20 61 62 6f 76 65 20 61 72 65 20 66 61 73  () above are fas
1b7c2 74 20 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74  t and will get t
1b7c3 68 65 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a  he entire.    **
1b7c4 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69   record header i
1b7c5 6e 20 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42  n most cases.  B
1b7c6 75 74 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69  ut they will fai
1b7c7 6c 20 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d  l to get the com
1b7c8 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63  plete.    ** rec
1b7c9 6f 72 64 20 68 65 61 64 65 72 20 69 66 20 74 68  ord header if th
1b7ca 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20  e record header 
1b7cb 64 6f 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20  does not fit on 
1b7cc 61 20 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20  a single page.  
1b7cd 20 20 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72    ** in the B-Tr
1b7ce 65 65 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68  ee.  When that h
1b7cf 61 70 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69  appens, use sqli
1b7d0 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1b7d1 72 65 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20  ree() to.    ** 
1b7d2 61 63 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70  acquire the comp
1b7d3 6c 65 74 65 20 68 65 61 64 65 72 20 74 65 78 74  lete header text
1b7d4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1b7d5 20 21 7a 52 65 63 20 26 26 20 61 76 61 69 6c 3c   !zRec && avail<
1b7d6 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20  offset ){.      
1b7d7 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a  sMem.flags = 0;.
1b7d8 20 20 20 20 20 20 73 4d 65 6d 2e 64 62 20 3d 20        sMem.db = 
1b7d9 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  0;.      rc = sq
1b7da 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d  lite3VdbeMemFrom
1b7db 42 74 72 65 65 28 70 43 72 73 72 2c 20 30 2c 20  Btree(pCrsr, 0, 
1b7dc 6f 66 66 73 65 74 2c 20 70 43 2d 3e 69 73 49 6e  offset, pC->isIn
1b7dd 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20  dex, &sMem);.   
1b7de 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1b7df 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1b7e0 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f  goto op_column_o
1b7e1 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
1b7e2 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a    zData = sMem.z
1b7e3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 6e 64  ;.    }.    zEnd
1b7e4 48 64 72 20 3d 20 28 75 38 20 2a 29 26 7a 44 61  Hdr = (u8 *)&zDa
1b7e5 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20  ta[offset];.    
1b7e6 7a 49 64 78 20 3d 20 28 75 38 20 2a 29 26 7a 44  zIdx = (u8 *)&zD
1b7e7 61 74 61 5b 73 7a 48 64 72 53 7a 5d 3b 0a 0a 20  ata[szHdrSz];.. 
1b7e8 20 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68     /* Scan the h
1b7e9 65 61 64 65 72 20 61 6e 64 20 75 73 65 20 69 74  eader and use it
1b7ea 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20   to fill in the 
1b7eb 61 54 79 70 65 5b 5d 20 61 6e 64 20 61 4f 66 66  aType[] and aOff
1b7ec 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  set[].    ** arr
1b7ed 61 79 73 2e 20 20 61 54 79 70 65 5b 69 5d 20 77  ays.  aType[i] w
1b7ee 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  ill contain the 
1b7ef 74 79 70 65 20 69 6e 74 65 67 65 72 20 66 6f 72  type integer for
1b7f0 20 74 68 65 20 69 2d 74 68 0a 20 20 20 20 2a 2a   the i-th.    **
1b7f1 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 61 4f 66 66   column and aOff
1b7f2 73 65 74 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e 74  set[i] will cont
1b7f3 61 69 6e 20 74 68 65 20 6f 66 66 73 65 74 20 66  ain the offset f
1b7f4 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
1b7f5 67 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  g.    ** of the 
1b7f6 72 65 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74  record to the st
1b7f7 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20  art of the data 
1b7f8 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 6f 6c  for the i-th col
1b7f9 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  umn.    */.    f
1b7fa 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 69 65 6c 64  or(i=0; i<nField
1b7fb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
1b7fc 28 20 7a 49 64 78 3c 7a 45 6e 64 48 64 72 20 29  ( zIdx<zEndHdr )
1b7fd 7b 0a 20 20 20 20 20 20 20 20 61 4f 66 66 73 65  {.        aOffse
1b7fe 74 5b 69 5d 20 3d 20 6f 66 66 73 65 74 3b 0a 20  t[i] = offset;. 
1b7ff 20 20 20 20 20 20 20 7a 49 64 78 20 2b 3d 20 67         zIdx += g
1b800 65 74 56 61 72 69 6e 74 33 32 28 7a 49 64 78 2c  etVarint32(zIdx,
1b801 20 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20   aType[i]);.    
1b802 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 71      offset += sq
1b803 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1b804 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d 29  ypeLen(aType[i])
1b805 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1b806 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 20 69         /* If i i
1b807 73 20 6c 65 73 73 20 74 68 61 74 20 6e 46 69 65  s less that nFie
1b808 6c 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61  ld, then there a
1b809 72 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69  re less fields i
1b80a 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a  n this.        *
1b80b 2a 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65  * record than Se
1b80c 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69  tNumColumns indi
1b80d 63 61 74 65 64 20 74 68 65 72 65 20 61 72 65 20  cated there are 
1b80e 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20  columns in the. 
1b80f 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e         ** table.
1b810 20 53 65 74 20 74 68 65 20 6f 66 66 73 65 74 20   Set the offset 
1b811 66 6f 72 20 61 6e 79 20 65 78 74 72 61 20 63 6f  for any extra co
1b812 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 6e  lumns not presen
1b813 74 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  t in.        ** 
1b814 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 30 2e  the record to 0.
1b815 20 54 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 65   This tells code
1b816 20 62 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20   below to store 
1b817 61 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 2a  a NULL.        *
1b818 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73  * instead of des
1b819 65 72 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c  erializing a val
1b81a 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f  ue from the reco
1b81b 72 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  rd..        */. 
1b81c 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69         aOffset[i
1b81d 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  ] = 0;.      }. 
1b81e 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1b81f 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1b820 73 4d 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d 2e  sMem);.    sMem.
1b821 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1b822 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  ;..    /* If we 
1b823 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68  have read more h
1b824 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20  eader data than 
1b825 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  was contained in
1b826 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20   the header,.   
1b827 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e   ** or if the en
1b828 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69  d of the last fi
1b829 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62  eld appears to b
1b82a 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
1b82b 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63  f the.    ** rec
1b82c 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65  ord, or if the e
1b82d 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66  nd of the last f
1b82e 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20  ield appears to 
1b82f 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e  be before the en
1b830 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  d.    ** of the 
1b831 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c  record (when all
1b832 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29   fields present)
1b833 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62  , then we must b
1b834 65 20 64 65 61 6c 69 6e 67 20 0a 20 20 20 20 2a  e dealing .    *
1b835 2a 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74  * with a corrupt
1b836 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a   database..    *
1b837 2f 0a 20 20 20 20 69 66 28 20 7a 49 64 78 3e 7a  /.    if( zIdx>z
1b838 45 6e 64 48 64 72 20 7c 7c 20 6f 66 66 73 65 74  EndHdr || offset
1b839 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 0a 20 20  >payloadSize .  
1b83a 20 20 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45 6e     || (zIdx==zEn
1b83b 64 48 64 72 20 26 26 20 6f 66 66 73 65 74 21 3d  dHdr && offset!=
1b83c 70 61 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a  payloadSize) ){.
1b83d 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1b83e 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1b83f 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
1b840 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  lumn_out;.    }.
1b841 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68    }..  /* Get th
1b842 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61  e column informa
1b843 74 69 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65 74  tion. If aOffset
1b844 5b 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  [p2] is non-zero
1b845 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65 73  , then .  ** des
1b846 65 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c  erialize the val
1b847 75 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f  ue from the reco
1b848 72 64 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70  rd. If aOffset[p
1b849 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a  2] is zero,.  **
1b84a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65 20   then there are 
1b84b 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64  not enough field
1b84c 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
1b84d 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a 20  to satisfy the. 
1b84e 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e   ** request.  In
1b84f 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 20   this case, set 
1b850 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f  the value NULL o
1b851 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69 73  r to P4 if P4 is
1b852 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  .  ** a pointer 
1b853 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e  to a Mem object.
1b854 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66 66  .  */.  if( aOff
1b855 73 65 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20 61  set[p2] ){.    a
1b856 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1b857 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20  E_OK );.    if( 
1b858 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71  zRec ){.      sq
1b859 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1b85a 61 73 65 45 78 74 65 72 6e 61 6c 28 70 44 65 73  aseExternal(pDes
1b85b 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
1b85c 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28  3VdbeSerialGet((
1b85d 75 38 20 2a 29 26 7a 52 65 63 5b 61 4f 66 66 73  u8 *)&zRec[aOffs
1b85e 65 74 5b 70 32 5d 5d 2c 20 61 54 79 70 65 5b 70  et[p2]], aType[p
1b85f 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20  2], pDest);.    
1b860 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 65 6e  }else{.      len
1b861 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1b862 72 69 61 6c 54 79 70 65 4c 65 6e 28 61 54 79 70  rialTypeLen(aTyp
1b863 65 5b 70 32 5d 29 3b 0a 20 20 20 20 20 20 73 71  e[p2]);.      sq
1b864 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
1b865 28 26 73 4d 65 6d 2c 20 70 44 65 73 74 29 3b 0a  (&sMem, pDest);.
1b866 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1b867 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72  e3VdbeMemFromBtr
1b868 65 65 28 70 43 72 73 72 2c 20 61 4f 66 66 73 65  ee(pCrsr, aOffse
1b869 74 5b 70 32 5d 2c 20 6c 65 6e 2c 20 70 43 2d 3e  t[p2], len, pC->
1b86a 69 73 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b  isIndex, &sMem);
1b86b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1b86c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1b86d 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75      goto op_colu
1b86e 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  mn_out;.      }.
1b86f 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 73 4d        zData = sM
1b870 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69  em.z;.      sqli
1b871 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74  te3VdbeSerialGet
1b872 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 61 54 79  ((u8*)zData, aTy
1b873 70 65 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a  pe[p2], pDest);.
1b874 20 20 20 20 7d 0a 20 20 20 20 70 44 65 73 74 2d      }.    pDest-
1b875 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b  >enc = encoding;
1b876 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
1b877 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1b878 34 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 73  4_MEM ){.      s
1b879 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
1b87a 6c 6c 6f 77 43 6f 70 79 28 70 44 65 73 74 2c 20  llowCopy(pDest, 
1b87b 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45  pOp->p4.pMem, ME
1b87c 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 7d  M_Static);.    }
1b87d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
1b87e 72 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73  rt( pDest->flags
1b87f 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20  &MEM_Null );.   
1b880 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
1b881 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  we dynamically a
1b882 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74  llocated space t
1b883 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20  o hold the data 
1b884 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c  (in the.  ** sql
1b885 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42  ite3VdbeMemFromB
1b886 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76  tree() call abov
1b887 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72  e) then transfer
1b888 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74   control of that
1b889 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c  .  ** dynamicall
1b88a 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63  y allocated spac
1b88b 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20 70 44  e over to the pD
1b88c 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20  est structure.. 
1b88d 20 2a 2a 20 54 68 69 73 20 70 72 65 76 65 6e 74   ** This prevent
1b88e 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79 2e  s a memory copy.
1b88f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 4d 65 6d  .  */.  if( sMem
1b890 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  .zMalloc ){.    
1b891 61 73 73 65 72 74 28 20 73 4d 65 6d 2e 7a 3d 3d  assert( sMem.z==
1b892 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a  sMem.zMalloc );.
1b893 20 20 20 20 61 73 73 65 72 74 28 20 21 28 70 44      assert( !(pD
1b894 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  est->flags & MEM
1b895 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73  _Dyn) );.    ass
1b896 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e 66 6c  ert( !(pDest->fl
1b897 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c  ags & (MEM_Blob|
1b898 4d 45 4d 5f 53 74 72 29 29 20 7c 7c 20 70 44 65  MEM_Str)) || pDe
1b899 73 74 2d 3e 7a 3d 3d 73 4d 65 6d 2e 7a 20 29 3b  st->z==sMem.z );
1b89a 0a 20 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 67  .    pDest->flag
1b89b 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d  s &= ~(MEM_Ephem
1b89c 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20  |MEM_Static);.  
1b89d 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c    pDest->flags |
1b89e 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  = MEM_Term;.    
1b89f 70 44 65 73 74 2d 3e 7a 20 3d 20 73 4d 65 6d 2e  pDest->z = sMem.
1b8a0 7a 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 7a 4d  z;.    pDest->zM
1b8a1 61 6c 6c 6f 63 20 3d 20 73 4d 65 6d 2e 7a 4d 61  alloc = sMem.zMa
1b8a2 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  lloc;.  }..  rc 
1b8a3 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
1b8a4 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 44  MakeWriteable(pD
1b8a5 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e  est);..op_column
1b8a6 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45 5f 4d  _out:.  UPDATE_M
1b8a7 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 73  AX_BLOBSIZE(pDes
1b8a8 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  t);.  REGISTER_T
1b8a9 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44  RACE(pOp->p3, pD
1b8aa 65 73 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  est);.  break;.}
1b8ab 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66 66  ../* Opcode: Aff
1b8ac 69 6e 69 74 79 20 50 31 20 50 32 20 2a 20 50 34  inity P1 P2 * P4
1b8ad 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20 61   *.**.** Apply a
1b8ae 66 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20 72  ffinities to a r
1b8af 61 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69 73  ange of P2 regis
1b8b0 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69  ters starting wi
1b8b1 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20  th P1..**.** P4 
1b8b2 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  is a string that
1b8b3 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72   is P2 character
1b8b4 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68 20  s long. The nth 
1b8b5 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65  character of the
1b8b6 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63  .** string indic
1b8b7 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  ates the column 
1b8b8 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
1b8b9 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
1b8ba 20 74 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f   the nth.** memo
1b8bb 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72  ry cell in the r
1b8bc 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  ange..*/.case OP
1b8bd 5f 41 66 66 69 6e 69 74 79 3a 20 7b 0a 20 20 63  _Affinity: {.  c
1b8be 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 20 3d  har *zAffinity =
1b8bf 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 4d 65   pOp->p4.z;.  Me
1b8c0 6d 20 2a 70 44 61 74 61 30 20 3d 20 26 70 2d 3e  m *pData0 = &p->
1b8c1 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1b8c2 20 4d 65 6d 20 2a 70 4c 61 73 74 20 3d 20 26 70   Mem *pLast = &p
1b8c3 44 61 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d  Data0[pOp->p2-1]
1b8c4 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a 0a  ;.  Mem *pRec;..
1b8c5 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61    for(pRec=pData
1b8c6 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20  0; pRec<=pLast; 
1b8c7 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 70  pRec++){.    Exp
1b8c8 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20  andBlob(pRec);. 
1b8c9 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79     applyAffinity
1b8ca 28 70 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 79  (pRec, zAffinity
1b8cb 5b 70 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 65  [pRec-pData0], e
1b8cc 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20  ncoding);.  }.  
1b8cd 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1b8ce 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 20  ode: MakeRecord 
1b8cf 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
1b8d0 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32 20 72  .** Convert P2 r
1b8d1 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
1b8d2 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74 6f 20  ng with P1 into 
1b8d3 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a  a single entry.*
1b8d4 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75  * suitable for u
1b8d5 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65 63  se as a data rec
1b8d6 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61 73  ord in a databas
1b8d7 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61 20  e table or as a 
1b8d8 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e 64  key.** in an ind
1b8d9 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 6c 73  ex.  The details
1b8da 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 61   of the format a
1b8db 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20 61 73  re irrelevant as
1b8dc 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65 20   long as.** the 
1b8dd 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
1b8de 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 65 20   can decode the 
1b8df 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a 2a 2a  record later..**
1b8e0 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72 63 65   Refer to source
1b8e1 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66   code comments f
1b8e2 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f  or the details o
1b8e3 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20  f the record.** 
1b8e4 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 50 34  format..**.** P4
1b8e5 20 6d 61 79 20 62 65 20 61 20 73 74 72 69 6e 67   may be a string
1b8e6 20 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72   that is P2 char
1b8e7 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68  acters long.  Th
1b8e8 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72 20  e nth character 
1b8e9 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67  of the.** string
1b8ea 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 63   indicates the c
1b8eb 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74  olumn affinity t
1b8ec 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73  hat should be us
1b8ed 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a  ed for the nth.*
1b8ee 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69  * field of the i
1b8ef 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20  ndex key..**.** 
1b8f0 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d  The mapping from
1b8f1 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 66   character to af
1b8f2 66 69 6e 69 74 79 20 69 73 20 67 69 76 65 6e 20  finity is given 
1b8f3 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46  by the SQLITE_AF
1b8f4 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65 66  F_.** macros def
1b8f5 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e  ined in sqliteIn
1b8f6 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34  t.h..**.** If P4
1b8f7 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 6c   is NULL then al
1b8f8 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73 20 68  l index fields h
1b8f9 61 76 65 20 74 68 65 20 61 66 66 69 6e 69 74 79  ave the affinity
1b8fa 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NONE..*/.case O
1b8fb 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b 0a  P_MakeRecord: {.
1b8fc 20 20 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74 68    /* Assuming th
1b8fd 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 61 69 6e  e record contain
1b8fe 73 20 4e 20 66 69 65 6c 64 73 2c 20 74 68 65 20  s N fields, the 
1b8ff 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c 6f  record format lo
1b900 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74 68  oks.  ** like th
1b901 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d  is:.  **.  ** --
1b902 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b903 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b904 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b905 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b906 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64  ------.  ** | hd
1b907 72 2d 73 69 7a 65 20 7c 20 74 79 70 65 20 30 20  r-size | type 0 
1b908 7c 20 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20 7c  | type 1 | ... |
1b909 20 74 79 70 65 20 4e 2d 31 20 7c 20 64 61 74 61   type N-1 | data
1b90a 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20 4e  0 | ... | data N
1b90b 2d 31 20 7c 20 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d  -1 | .  ** -----
1b90c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b90d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b90e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b90f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1b910 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 61  ---.  **.  ** Da
1b911 74 61 28 30 29 20 69 73 20 74 61 6b 65 6e 20 66  ta(0) is taken f
1b912 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 2e  rom register P1.
1b913 20 20 44 61 74 61 28 31 29 20 63 6f 6d 65 73 20    Data(1) comes 
1b914 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31  from register P1
1b915 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66  +1.  ** and so f
1b916 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  roth..  **.  ** 
1b917 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64 20  Each type field 
1b918 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70 72  is a varint repr
1b919 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72  esenting the ser
1b91a 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20  ial type of the 
1b91b 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  .  ** correspond
1b91c 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74  ing data element
1b91d 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 64 62   (see sqlite3Vdb
1b91e 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20  eSerialType()). 
1b91f 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a  The.  ** hdr-siz
1b920 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20  e field is also 
1b921 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 20 69  a varint which i
1b922 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f  s the offset fro
1b923 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
1b924 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f    ** of the reco
1b925 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 2a  rd to data0..  *
1b926 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f  /.  u8 *zNewReco
1b927 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20  rd;        /* A 
1b928 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
1b929 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  he data for the 
1b92a 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
1b92b 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20  Mem *pRec;      
1b92c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
1b92d 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36  w record */.  u6
1b92e 34 20 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 20  4 nData = 0;    
1b92f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1b930 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  f bytes of data 
1b931 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  space */.  int n
1b932 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Hdr = 0;        
1b933 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1b934 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73  ytes of header s
1b935 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42  pace */.  i64 nB
1b936 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  yte = 0;        
1b937 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72   /* Data space r
1b938 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73  equired for this
1b939 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
1b93a 20 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20   nZero = 0;     
1b93b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1b93c 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74   zero bytes at t
1b93d 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65  he end of the re
1b93e 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56  cord */.  int nV
1b93f 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  arint;          
1b940 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1b941 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20  tes in a varint 
1b942 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f  */.  u32 serial_
1b943 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54  type;       /* T
1b944 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d  ype field */.  M
1b945 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20  em *pData0;     
1b946 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66        /* First f
1b947 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69  ield to be combi
1b948 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63  ned into the rec
1b949 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c  ord */.  Mem *pL
1b94a 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ast;            
1b94b 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66  /* Last field of
1b94c 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1b94d 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20   int nField;    
1b94e 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1b94f 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
1b950 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63  he record */.  c
1b951 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20  har *zAffinity; 
1b952 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66        /* The aff
1b953 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72  inity string for
1b954 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1b955 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74   int file_format
1b956 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20  ;       /* File 
1b957 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f  format to use fo
1b958 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  r encoding */.  
1b959 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1b95a 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
1b95b 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f  used in zNewReco
1b95c 72 64 5b 5d 20 2a 2f 0a 0a 20 20 6e 46 69 65 6c  rd[] */..  nFiel
1b95d 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 7a  d = pOp->p1;.  z
1b95e 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e  Affinity = pOp->
1b95f 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20  p4.z;.  assert( 
1b960 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d  nField>0 && pOp-
1b961 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  >p2>0 && pOp->p2
1b962 2b 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d  +nField<=p->nMem
1b963 2b 31 20 29 3b 0a 20 20 70 44 61 74 61 30 20 3d  +1 );.  pData0 =
1b964 20 26 70 2d 3e 61 4d 65 6d 5b 6e 46 69 65 6c 64   &p->aMem[nField
1b965 5d 3b 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4f  ];.  nField = pO
1b966 70 2d 3e 70 32 3b 0a 20 20 70 4c 61 73 74 20 3d  p->p2;.  pLast =
1b967 20 26 70 44 61 74 61 30 5b 6e 46 69 65 6c 64 2d   &pData0[nField-
1b968 31 5d 3b 0a 20 20 66 69 6c 65 5f 66 6f 72 6d 61  1];.  file_forma
1b969 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46  t = p->minWriteF
1b96a 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a  ileFormat;..  /*
1b96b 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   Loop through th
1b96c 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20  e elements that 
1b96d 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68 65  will make up the
1b96e 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 75 72   record to figur
1b96f 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77 20 6d  e.  ** out how m
1b970 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 71  uch space is req
1b971 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 6e 65  uired for the ne
1b972 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20  w record..  */. 
1b973 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30   for(pRec=pData0
1b974 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70  ; pRec<=pLast; p
1b975 52 65 63 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  Rec++){.    int 
1b976 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20 7a 41 66  len;.    if( zAf
1b977 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20  finity ){.      
1b978 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52  applyAffinity(pR
1b979 65 63 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 52  ec, zAffinity[pR
1b97a 65 63 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f  ec-pData0], enco
1b97b 64 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ding);.    }.   
1b97c 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73   if( pRec->flags
1b97d 26 4d 45 4d 5f 5a 65 72 6f 20 26 26 20 70 52 65  &MEM_Zero && pRe
1b97e 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20  c->n>0 ){.      
1b97f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
1b980 70 61 6e 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a  pandBlob(pRec);.
1b981 20 20 20 20 7d 0a 20 20 20 20 73 65 72 69 61 6c      }.    serial
1b982 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56  _type = sqlite3V
1b983 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 52  dbeSerialType(pR
1b984 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29  ec, file_format)
1b985 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69  ;.    len = sqli
1b986 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1b987 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65  eLen(serial_type
1b988 29 3b 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d 20  );.    nData += 
1b989 6c 65 6e 3b 0a 20 20 20 20 6e 48 64 72 20 2b 3d  len;.    nHdr +=
1b98a 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65   sqlite3VarintLe
1b98b 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a  n(serial_type);.
1b98c 20 20 20 20 69 66 28 20 70 52 65 63 2d 3e 66 6c      if( pRec->fl
1b98d 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
1b98e 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20  {.      /* Only 
1b98f 70 75 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  pure zero-filled
1b990 20 42 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 6e   BLOBs can be in
1b991 70 75 74 20 74 6f 20 74 68 69 73 20 4f 70 63 6f  put to this Opco
1b992 64 65 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 20  de..      ** We 
1b993 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f  do not allow blo
1b994 62 73 20 77 69 74 68 20 61 20 70 72 65 66 69 78  bs with a prefix
1b995 20 61 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c   and a zero-fill
1b996 65 64 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20  ed tail. */.    
1b997 20 20 6e 5a 65 72 6f 20 2b 3d 20 70 52 65 63 2d    nZero += pRec-
1b998 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65  >u.nZero;.    }e
1b999 6c 73 65 20 69 66 28 20 6c 65 6e 20 29 7b 0a 20  lse if( len ){. 
1b99a 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a       nZero = 0;.
1b99b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1b99c 41 64 64 20 74 68 65 20 69 6e 69 74 69 61 6c 20  Add the initial 
1b99d 68 65 61 64 65 72 20 76 61 72 69 6e 74 20 61 6e  header varint an
1b99e 64 20 74 6f 74 61 6c 20 74 68 65 20 73 69 7a 65  d total the size
1b99f 20 2a 2f 0a 20 20 6e 48 64 72 20 2b 3d 20 6e 56   */.  nHdr += nV
1b9a0 61 72 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  arint = sqlite3V
1b9a1 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29 3b 0a  arintLen(nHdr);.
1b9a2 20 20 69 66 28 20 6e 56 61 72 69 6e 74 3c 73 71    if( nVarint<sq
1b9a3 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 6e  lite3VarintLen(n
1b9a4 48 64 72 29 20 29 7b 0a 20 20 20 20 6e 48 64 72  Hdr) ){.    nHdr
1b9a5 2b 2b 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20  ++;.  }.  nByte 
1b9a6 3d 20 6e 48 64 72 2b 6e 44 61 74 61 2d 6e 5a 65  = nHdr+nData-nZe
1b9a7 72 6f 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e  ro;.  if( nByte>
1b9a8 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1b9a9 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
1b9aa 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  ){.    goto too_
1b9ab 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  big;.  }..  /* M
1b9ac 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 75 74  ake sure the out
1b9ad 70 75 74 20 72 65 67 69 73 74 65 72 20 68 61 73  put register has
1b9ae 20 61 20 62 75 66 66 65 72 20 6c 61 72 67 65 20   a buffer large 
1b9af 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72 65 20  enough to store 
1b9b0 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 72 65  .  ** the new re
1b9b1 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70 75 74  cord. The output
1b9b2 20 72 65 67 69 73 74 65 72 20 28 70 4f 70 2d 3e   register (pOp->
1b9b3 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77  p3) is not allow
1b9b4 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20 6f 6e  ed to.  ** be on
1b9b5 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 72  e of the input r
1b9b6 65 67 69 73 74 65 72 73 20 28 62 65 63 61 75 73  egisters (becaus
1b9b7 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
1b9b8 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73 71 6c  call to.  ** sql
1b9b9 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1b9ba 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65 72 20  ) could clobber 
1b9bb 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f 72 65  the value before
1b9bc 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a 20 20   it is used)..  
1b9bd 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1b9be 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c 7c 20  ->p3<pOp->p1 || 
1b9bf 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 31  pOp->p3>=pOp->p1
1b9c0 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 70 4f  +pOp->p2 );.  pO
1b9c1 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1b9c2 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 73 71  p->p3];.  if( sq
1b9c3 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1b9c4 28 70 4f 75 74 2c 20 28 69 6e 74 29 6e 42 79 74  (pOut, (int)nByt
1b9c5 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74  e, 0) ){.    got
1b9c6 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20  o no_mem;.  }.  
1b9c7 7a 4e 65 77 52 65 63 6f 72 64 20 3d 20 28 75 38  zNewRecord = (u8
1b9c8 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 2f   *)pOut->z;..  /
1b9c9 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 63 6f  * Write the reco
1b9ca 72 64 20 2a 2f 0a 20 20 69 20 3d 20 70 75 74 56  rd */.  i = putV
1b9cb 61 72 69 6e 74 33 32 28 7a 4e 65 77 52 65 63 6f  arint32(zNewReco
1b9cc 72 64 2c 20 6e 48 64 72 29 3b 0a 20 20 66 6f 72  rd, nHdr);.  for
1b9cd 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52  (pRec=pData0; pR
1b9ce 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b  ec<=pLast; pRec+
1b9cf 2b 29 7b 0a 20 20 20 20 73 65 72 69 61 6c 5f 74  +){.    serial_t
1b9d0 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ype = sqlite3Vdb
1b9d1 65 53 65 72 69 61 6c 54 79 70 65 28 70 52 65 63  eSerialType(pRec
1b9d2 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a  , file_format);.
1b9d3 20 20 20 20 69 20 2b 3d 20 70 75 74 56 61 72 69      i += putVari
1b9d4 6e 74 33 32 28 26 7a 4e 65 77 52 65 63 6f 72 64  nt32(&zNewRecord
1b9d5 5b 69 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65  [i], serial_type
1b9d6 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69 61  );      /* seria
1b9d7 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20 20  l type */.  }.  
1b9d8 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b  for(pRec=pData0;
1b9d9 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52   pRec<=pLast; pR
1b9da 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61  ec++){  /* seria
1b9db 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69 20  l data */.    i 
1b9dc 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  += sqlite3VdbeSe
1b9dd 72 69 61 6c 50 75 74 28 26 7a 4e 65 77 52 65 63  rialPut(&zNewRec
1b9de 6f 72 64 5b 69 5d 2c 20 28 69 6e 74 29 28 6e 42  ord[i], (int)(nB
1b9df 79 74 65 2d 69 29 2c 20 70 52 65 63 2c 66 69 6c  yte-i), pRec,fil
1b9e0 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20  e_format);.  }. 
1b9e1 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 42 79 74   assert( i==nByt
1b9e2 65 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  e );..  assert( 
1b9e3 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70  pOp->p3>0 && pOp
1b9e4 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1b9e5 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e  .  pOut->n = (in
1b9e6 74 29 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d  t)nByte;.  pOut-
1b9e7 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f  >flags = MEM_Blo
1b9e8 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70  b | MEM_Dyn;.  p
1b9e9 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20  Out->xDel = 0;. 
1b9ea 20 69 66 28 20 6e 5a 65 72 6f 20 29 7b 0a 20 20   if( nZero ){.  
1b9eb 20 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 6f 20    pOut->u.nZero 
1b9ec 3d 20 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4f 75  = nZero;.    pOu
1b9ed 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  t->flags |= MEM_
1b9ee 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 70 4f 75 74  Zero;.  }.  pOut
1b9ef 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
1b9f0 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65  TF8;  /* In case
1b9f1 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 65   the blob is eve
1b9f2 72 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 74  r converted to t
1b9f3 65 78 74 20 2a 2f 0a 20 20 52 45 47 49 53 54 45  ext */.  REGISTE
1b9f4 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
1b9f5 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 41 54 45   pOut);.  UPDATE
1b9f6 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f  _MAX_BLOBSIZE(pO
1b9f7 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
1b9f8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 75 6e  ./* Opcode: Coun
1b9f9 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1b9fa 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 6e 75  .** Store the nu
1b9fb 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
1b9fc 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75  (an integer valu
1b9fd 65 29 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  e) in the table 
1b9fe 6f 72 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 70 65  or index .** ope
1b9ff 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 31  ned by cursor P1
1ba00 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a   in register P2.
1ba01 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1ba02 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e  E_OMIT_BTREECOUN
1ba03 54 0a 63 61 73 65 20 4f 50 5f 43 6f 75 6e 74 3a  T.case OP_Count:
1ba04 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75   {         /* ou
1ba05 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
1ba06 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a 20  .  i64 nEntry;. 
1ba07 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1ba08 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1ba09 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  >p1]->pCursor;. 
1ba0a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1ba0b 65 65 43 6f 75 6e 74 28 70 43 72 73 72 2c 20 26  eeCount(pCrsr, &
1ba0c 6e 45 6e 74 72 79 29 3b 0a 20 20 70 4f 75 74 2d  nEntry);.  pOut-
1ba0d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1ba0e 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
1ba0f 6e 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b 3b  nEntry;.  break;
1ba10 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70  .}.#endif../* Op
1ba11 63 6f 64 65 3a 20 53 74 61 74 65 6d 65 6e 74 20  code: Statement 
1ba12 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1ba13 20 42 65 67 69 6e 20 61 6e 20 69 6e 64 69 76 69   Begin an indivi
1ba14 64 75 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74  dual statement t
1ba15 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 63 68  ransaction which
1ba16 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c 61   is part of a la
1ba17 72 67 65 72 0a 2a 2a 20 74 72 61 6e 73 61 63 74  rger.** transact
1ba18 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65  ion.  This is ne
1ba19 65 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  eded so that the
1ba1a 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61   statement.** ca
1ba1b 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n be rolled back
1ba1c 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20   after an error 
1ba1d 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74  without having t
1ba1e 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a  o roll back the.
1ba1f 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61  ** entire transa
1ba20 63 74 69 6f 6e 2e 20 20 54 68 65 20 73 74 61 74  ction.  The stat
1ba21 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1ba22 6e 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63  n will automatic
1ba23 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 77  ally.** commit w
1ba24 68 65 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c  hen the VDBE hal
1ba25 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ts..**.** If the
1ba26 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1ba27 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c  tion is currentl
1ba28 79 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20  y in autocommit 
1ba29 6d 6f 64 65 20 28 74 68 61 74 20 0a 2a 2a 20 69  mode (that .** i
1ba2a 73 20 74 6f 20 73 61 79 2c 20 69 66 20 69 74 20  s to say, if it 
1ba2b 69 73 20 69 6e 20 62 65 74 77 65 65 6e 20 42 45  is in between BE
1ba2c 47 49 4e 20 61 6e 64 20 43 4f 4d 4d 49 54 29 0a  GIN and COMMIT).
1ba2d 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 72 65 20  ** and if there 
1ba2e 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 61 63 74  are no other act
1ba2f 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  ive statements o
1ba30 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  n the same datab
1ba31 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
1ba32 6e 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 65  n, then this ope
1ba33 72 61 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  ration is a no-o
1ba34 70 2e 20 20 4e 6f 20 73 74 61 74 65 6d 65 6e 74  p.  No statement
1ba35 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
1ba36 69 73 20 6e 65 65 64 65 64 20 73 69 6e 63 65 20  is needed since 
1ba37 61 6e 79 20 65 72 72 6f 72 20 63 61 6e 20 75 73  any error can us
1ba38 65 20 74 68 65 20 6e 6f 72 6d 61 6c 20 52 4f 4c  e the normal ROL
1ba39 4c 42 41 43 4b 20 70 72 6f 63 65 73 73 20 74 6f  LBACK process to
1ba3a 0a 2a 2a 20 75 6e 64 6f 20 63 68 61 6e 67 65 73  .** undo changes
1ba3b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 74 61  ..**.** If a sta
1ba3c 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1ba3d 6f 6e 20 69 73 20 73 74 61 72 74 65 64 2c 20 74  on is started, t
1ba3e 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20  hen a statement 
1ba3f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20  journal file.** 
1ba40 77 69 6c 6c 20 62 65 20 61 6c 6c 6f 63 61 74 65  will be allocate
1ba41 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  d and initialize
1ba42 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61  d..**.** The sta
1ba43 74 65 6d 65 6e 74 20 69 73 20 62 65 67 75 6e 20  tement is begun 
1ba44 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
1ba45 66 69 6c 65 20 77 69 74 68 20 69 6e 64 65 78 20  file with index 
1ba46 50 31 2e 20 20 54 68 65 20 6d 61 69 6e 0a 2a 2a  P1.  The main.**
1ba47 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
1ba48 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30  as an index of 0
1ba49 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 75 73   and the file us
1ba4a 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  ed for temporary
1ba4b 20 74 61 62 6c 65 73 0a 2a 2a 20 68 61 73 20 61   tables.** has a
1ba4c 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2f  n index of 1..*/
1ba4d 0a 63 61 73 65 20 4f 50 5f 53 74 61 74 65 6d 65  .case OP_Stateme
1ba4e 6e 74 3a 20 7b 0a 20 20 69 66 28 20 64 62 2d 3e  nt: {.  if( db->
1ba4f 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c  autoCommit==0 ||
1ba50 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
1ba51 6e 74 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20  nt>1 ){.    int 
1ba52 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20  i = pOp->p1;.   
1ba53 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20   Btree *pBt;.   
1ba54 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
1ba55 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20   i<db->nDb );.  
1ba56 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
1ba57 62 5b 69 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20  b[i].pBt!=0 );. 
1ba58 20 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62     pBt = db->aDb
1ba59 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 73  [i].pBt;.    ass
1ba5a 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1ba5b 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20  eIsInTrans(pBt) 
1ba5c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
1ba5d 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1ba5e 31 3c 3c 69 29 29 21 3d 30 20 29 3b 0a 20 20 20  1<<i))!=0 );.   
1ba5f 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65   if( p->iStateme
1ba60 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  nt==0 ){.      a
1ba61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74  ssert( db->nStat
1ba62 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e  ement>=0 && db->
1ba63 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b  nSavepoint>=0 );
1ba64 0a 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74  .      db->nStat
1ba65 65 6d 65 6e 74 2b 2b 3b 20 0a 20 20 20 20 20 20  ement++; .      
1ba66 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20  p->iStatement = 
1ba67 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b  db->nSavepoint +
1ba68 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3b   db->nStatement;
1ba69 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1ba6a 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
1ba6b 6e 53 74 6d 74 28 70 42 74 2c 20 70 2d 3e 69 53  nStmt(pBt, p->iS
1ba6c 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20  tatement);.  }. 
1ba6d 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1ba6e 63 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 20  code: Savepoint 
1ba6f 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  P1 * * P4 *.**.*
1ba70 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20  * Open, release 
1ba71 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  or rollback the 
1ba72 73 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 20  savepoint named 
1ba73 62 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 2c  by parameter P4,
1ba74 20 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e   depending.** on
1ba75 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 31   the value of P1
1ba76 2e 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20  . To open a new 
1ba77 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 30  savepoint, P1==0
1ba78 2e 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 6f  . To release (co
1ba79 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 73  mmit) an.** exis
1ba7a 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c 20  ting savepoint, 
1ba7b 50 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f 6c  P1==1, or to rol
1ba7c 6c 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 6e  lback an existin
1ba7d 67 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d 3d  g savepoint P1==
1ba7e 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 61  2..*/.case OP_Sa
1ba7f 76 65 70 6f 69 6e 74 3a 20 7b 0a 20 20 69 6e 74  vepoint: {.  int
1ba80 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20   p1 = pOp->p1;. 
1ba81 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70   char *zName = p
1ba82 4f 70 2d 3e 70 34 2e 7a 3b 20 20 20 20 20 20 20  Op->p4.z;       
1ba83 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76    /* Name of sav
1ba84 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20  epoint */..  /* 
1ba85 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20  Assert that the 
1ba86 70 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  p1 parameter is 
1ba87 76 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 74  valid. Also that
1ba88 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
1ba89 6f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61  open.  ** transa
1ba8a 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72  ction, then ther
1ba8b 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20  e cannot be any 
1ba8c 73 61 76 65 70 6f 69 6e 74 73 2e 20 0a 20 20 2a  savepoints. .  *
1ba8d 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  /.  assert( db->
1ba8e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c  pSavepoint==0 ||
1ba8f 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d   db->autoCommit=
1ba90 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1ba91 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45  p1==SAVEPOINT_BE
1ba92 47 49 4e 7c 7c 70 31 3d 3d 53 41 56 45 50 4f 49  GIN||p1==SAVEPOI
1ba93 4e 54 5f 52 45 4c 45 41 53 45 7c 7c 70 31 3d 3d  NT_RELEASE||p1==
1ba94 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
1ba95 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
1ba96 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c  db->pSavepoint |
1ba97 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74  | db->isTransact
1ba98 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20  ionSavepoint==0 
1ba99 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 65  );.  assert( che
1ba9a 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74  ckSavepointCount
1ba9b 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 70  (db) );..  if( p
1ba9c 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  1==SAVEPOINT_BEG
1ba9d 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  IN ){.    if( db
1ba9e 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30  ->writeVdbeCnt>0
1ba9f 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e   ){.      /* A n
1baa0 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e  ew savepoint can
1baa1 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 69  not be created i
1baa2 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69  f there are acti
1baa3 76 65 20 77 72 69 74 65 20 0a 20 20 20 20 20 20  ve write .      
1baa4 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 69  ** statements (i
1baa5 2e 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 72  .e. open read/wr
1baa6 69 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20  ite incremental 
1baa7 62 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a 20  blob handles).. 
1baa8 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
1baa9 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1baaa 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1baab 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 76  "cannot open sav
1baac 65 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 20  epoint - ".     
1baad 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e     "SQL statemen
1baae 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29  ts in progress")
1baaf 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
1bab0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65  ITE_BUSY;.    }e
1bab1 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  lse{.      int n
1bab2 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
1bab3 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
1bab4 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a       Savepoint *
1bab5 70 4e 65 77 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  pNew;..      /* 
1bab6 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 76  Create a new sav
1bab7 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65  epoint structure
1bab8 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 20  . */.      pNew 
1bab9 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1baba 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
1babb 53 61 76 65 70 6f 69 6e 74 29 2b 6e 4e 61 6d 65  Savepoint)+nName
1babc 2b 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  +1);.      if( p
1babd 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 70  New ){.        p
1babe 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68  New->zName = (ch
1babf 61 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20  ar *)&pNew[1];. 
1bac0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e         memcpy(pN
1bac1 65 77 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  ew->zName, zName
1bac2 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20  , nName+1);.    
1bac3 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74  .        /* If t
1bac4 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20  here is no open 
1bac5 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
1bac6 6e 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61  n mark this as a
1bac7 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20   special.       
1bac8 20 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e   ** "transaction
1bac9 20 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0a   savepoint". */.
1baca 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e          if( db->
1bacb 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20  autoCommit ){.  
1bacc 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f          db->auto
1bacd 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  Commit = 0;.    
1bace 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 6e        db->isTran
1bacf 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74  sactionSavepoint
1bad0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65   = 1;.        }e
1bad1 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64  lse{.          d
1bad2 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b 3b  b->nSavepoint++;
1bad3 0a 09 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 20  ..}.    .       
1bad4 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77   /* Link the new
1bad5 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74 6f 20   savepoint into 
1bad6 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
1bad7 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f 0a 20  dle's list. */. 
1bad8 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65         pNew->pNe
1bad9 78 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f  xt = db->pSavepo
1bada 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d  int;.        db-
1badb 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 4e  >pSavepoint = pN
1badc 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ew;.      }.    
1badd 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 53  }.  }else{.    S
1bade 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70  avepoint *pSavep
1badf 6f 69 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 53  oint;.    int iS
1bae0 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20  avepoint = 0;.. 
1bae1 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e     /* Find the n
1bae2 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20  amed savepoint. 
1bae3 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73  If there is no s
1bae4 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c 20 74  uch savepoint, t
1bae5 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e  hen an.    ** an
1bae6 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
1bae7 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ed to the user. 
1bae8 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20 20   */.    for(.   
1bae9 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 64 62     pSavepoint=db
1baea 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 0a 20  ->pSavepoint; . 
1baeb 20 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 20       pSavepoint 
1baec 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
1baed 70 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e  p(pSavepoint->zN
1baee 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  ame, zName);.   
1baef 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 70 53     pSavepoint=pS
1baf0 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a  avepoint->pNext.
1baf1 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 53 61      ){.      iSa
1baf2 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d  vepoint++;.    }
1baf3 0a 20 20 20 20 69 66 28 20 21 70 53 61 76 65 70  .    if( !pSavep
1baf4 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  oint ){.      sq
1baf5 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1baf6 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1baf7 22 6e 6f 20 73 75 63 68 20 73 61 76 65 70 6f 69  "no such savepoi
1baf8 6e 74 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  nt: %s", zName);
1baf9 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1bafa 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65  TE_ERROR;.    }e
1bafb 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20 20 20  lse if( .       
1bafc 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e   db->writeVdbeCn
1bafd 74 3e 30 20 7c 7c 20 28 70 31 3d 3d 53 41 56 45  t>0 || (p1==SAVE
1bafe 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26  POINT_ROLLBACK &
1baff 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  & db->activeVdbe
1bb00 43 6e 74 3e 31 29 20 0a 20 20 20 20 29 7b 0a 20  Cnt>1) .    ){. 
1bb01 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f       /* It is no
1bb02 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65  t possible to re
1bb03 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61  lease (commit) a
1bb04 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68   savepoint if th
1bb05 65 72 65 20 61 72 65 20 0a 20 20 20 20 20 20 2a  ere are .      *
1bb06 2a 20 61 63 74 69 76 65 20 77 72 69 74 65 20 73  * active write s
1bb07 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73  tatements. It is
1bb08 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
1bb09 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 65   rollback a save
1bb0a 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69  point.      ** i
1bb0b 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
1bb0c 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74  active statement
1bb0d 73 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20  s at all..      
1bb0e 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1bb0f 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1bb10 72 72 4d 73 67 2c 20 64 62 2c 20 0a 20 20 20 20  rrMsg, db, .    
1bb11 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73      "cannot %s s
1bb12 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73  avepoint - SQL s
1bb13 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f  tatements in pro
1bb14 67 72 65 73 73 22 2c 0a 20 20 20 20 20 20 20 20  gress",.        
1bb15 28 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  (p1==SAVEPOINT_R
1bb16 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62  OLLBACK ? "rollb
1bb17 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29  ack": "release")
1bb18 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
1bb19 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
1bb1a 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20  ;.    }else{..  
1bb1b 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
1bb1c 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1bb1d 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61  this is a transa
1bb1e 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e  ction savepoint.
1bb1f 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a   If so,.      **
1bb20 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 52   and this is a R
1bb21 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20  ELEASE command, 
1bb22 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74  then the current
1bb23 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20   transaction .  
1bb24 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74      ** is commit
1bb25 74 65 64 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20  ted. .      */. 
1bb26 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73       int isTrans
1bb27 61 63 74 69 6f 6e 20 3d 20 70 53 61 76 65 70 6f  action = pSavepo
1bb28 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26  int->pNext==0 &&
1bb29 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69   db->isTransacti
1bb2a 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20  onSavepoint;.   
1bb2b 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61 63     if( isTransac
1bb2c 74 69 6f 6e 20 26 26 20 70 31 3d 3d 53 41 56 45  tion && p1==SAVE
1bb2d 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b  POINT_RELEASE ){
1bb2e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74  .        db->aut
1bb2f 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  oCommit = 1;.   
1bb30 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1bb31 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c  VdbeHalt(p)==SQL
1bb32 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
1bb33 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
1bb34 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e  ;.          db->
1bb35 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a  autoCommit = 0;.
1bb36 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20            p->rc 
1bb37 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  = rc = SQLITE_BU
1bb38 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  SY;.          go
1bb39 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1bb3a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1bb3b 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74    db->isTransact
1bb3c 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30  ionSavepoint = 0
1bb3d 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  ;.        rc = p
1bb3e 2d 3e 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ->rc;.      }els
1bb3f 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  e{.        int i
1bb40 69 3b 0a 20 20 20 20 20 20 20 20 69 53 61 76 65  i;.        iSave
1bb41 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76  point = db->nSav
1bb42 65 70 6f 69 6e 74 20 2d 20 69 53 61 76 65 70 6f  epoint - iSavepo
1bb43 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  int - 1;.       
1bb44 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 64 62   for(ii=0; ii<db
1bb45 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20  ->nDb; ii++){.  
1bb46 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1bb47 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
1bb48 6e 74 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70  nt(db->aDb[ii].p
1bb49 42 74 2c 20 70 31 2c 20 69 53 61 76 65 70 6f 69  Bt, p1, iSavepoi
1bb4a 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  nt);.          i
1bb4b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1bb4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1bb4d 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1bb4e 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1bb4f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1bb50 20 20 20 20 20 20 20 69 66 28 20 70 31 3d 3d 53         if( p1==S
1bb51 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
1bb52 4b 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 26  K && (db->flags&
1bb53 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
1bb54 6e 67 65 73 29 21 3d 30 20 29 7b 0a 20 20 20 20  nges)!=0 ){.    
1bb55 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1bb56 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
1bb57 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 20  ments(db);.     
1bb58 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65       sqlite3Rese
1bb59 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
1bb5a 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  db, 0);.        
1bb5b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20  }.      }.  .   
1bb5c 20 20 20 2f 2a 20 52 65 67 61 72 64 6c 65 73 73     /* Regardless
1bb5d 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 69 73   of whether this
1bb5e 20 69 73 20 61 20 52 45 4c 45 41 53 45 20 6f 72   is a RELEASE or
1bb5f 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65 73 74 72   ROLLBACK, destr
1bb60 6f 79 20 61 6c 6c 20 0a 20 20 20 20 20 20 2a 2a  oy all .      **
1bb61 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65 73 74   savepoints nest
1bb62 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65  ed inside of the
1bb63 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67   savepoint being
1bb64 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20 2a 2f   operated on. */
1bb65 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 64 62  .      while( db
1bb66 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d 70 53  ->pSavepoint!=pS
1bb67 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20  avepoint ){.    
1bb68 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70      Savepoint *p
1bb69 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 70  Tmp = db->pSavep
1bb6a 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62  oint;.        db
1bb6b 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70  ->pSavepoint = p
1bb6c 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Tmp->pNext;.    
1bb6d 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1bb6e 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20 20  e(db, pTmp);.   
1bb6f 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f       db->nSavepo
1bb70 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a  int--;.      }..
1bb71 20 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69        /* If it i
1bb72 73 20 61 20 52 45 4c 45 41 53 45 2c 20 74 68 65  s a RELEASE, the
1bb73 6e 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 61  n destroy the sa
1bb74 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70  vepoint being op
1bb75 65 72 61 74 65 64 20 6f 6e 20 74 6f 6f 20 2a 2f  erated on too */
1bb76 0a 20 20 20 20 20 20 69 66 28 20 70 31 3d 3d 53  .      if( p1==S
1bb77 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
1bb78 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1bb79 72 74 28 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d  rt( pSavepoint==
1bb7a 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29  db->pSavepoint )
1bb7b 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53  ;.        db->pS
1bb7c 61 76 65 70 6f 69 6e 74 20 3d 20 70 53 61 76 65  avepoint = pSave
1bb7d 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  point->pNext;.  
1bb7e 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1bb7f 72 65 65 28 64 62 2c 20 70 53 61 76 65 70 6f 69  ree(db, pSavepoi
1bb80 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  nt);.        if(
1bb81 20 21 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20   !isTransaction 
1bb82 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  ){.          db-
1bb83 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20  >nSavepoint--;. 
1bb84 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1bb85 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72  .    }.  }..  br
1bb86 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1bb87 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31  e: AutoCommit P1
1bb88 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1bb89 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  Set the database
1bb8a 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61   auto-commit fla
1bb8b 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29  g to P1 (1 or 0)
1bb8c 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c  . If P2 is true,
1bb8d 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e   roll.** back an
1bb8e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69  y currently acti
1bb8f 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63  ve btree transac
1bb90 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20  tions. If there 
1bb91 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a  are any active.*
1bb92 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f  * VMs (apart fro
1bb93 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65  m this one), the
1bb94 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69  n a ROLLBACK fai
1bb95 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61  ls.  A COMMIT fa
1bb96 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20  ils if.** there 
1bb97 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69  are active writi
1bb98 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65  ng VMs or active
1bb99 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68   VMs that use sh
1bb9a 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a  ared cache..**.*
1bb9b 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
1bb9c 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d  on causes the VM
1bb9d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73   to halt..*/.cas
1bb9e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a  e OP_AutoCommit:
1bb9f 20 7b 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64   {.  int desired
1bba0 41 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70  AutoCommit = pOp
1bba1 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 72 6f 6c 6c  ->p1;.  int roll
1bba2 62 61 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  back = pOp->p2;.
1bba3 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 20 3d    int turnOnAC =
1bba4 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d   desiredAutoComm
1bba5 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43  it && !db->autoC
1bba6 6f 6d 6d 69 74 3b 0a 0a 20 20 61 73 73 65 72 74  ommit;..  assert
1bba7 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  ( desiredAutoCom
1bba8 6d 69 74 3d 3d 31 20 7c 7c 20 64 65 73 69 72 65  mit==1 || desire
1bba9 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29  dAutoCommit==0 )
1bbaa 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69  ;.  assert( desi
1bbab 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31  redAutoCommit==1
1bbac 20 7c 7c 20 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20   || rollback==0 
1bbad 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  );..  assert( db
1bbae 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
1bbaf 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73  0 );  /* At leas
1bbb0 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73  t this one VM is
1bbb1 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66   active */..  if
1bbb2 28 20 74 75 72 6e 4f 6e 41 43 20 26 26 20 72 6f  ( turnOnAC && ro
1bbb3 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63  llback && db->ac
1bbb4 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b  tiveVdbeCnt>1 ){
1bbb5 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
1bbb6 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c  instruction impl
1bbb7 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43  ements a ROLLBAC
1bbb8 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20  K and other VMs 
1bbb9 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c  are.    ** still
1bbba 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20   running, and a 
1bbbb 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
1bbbc 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e  ctive, return an
1bbbd 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e   error indicatin
1bbbe 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68  g.    ** that th
1bbbf 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74  e other VMs must
1bbc0 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e   complete first.
1bbc1 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c   .    */.    sql
1bbc2 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1bbc3 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1bbc4 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  cannot rollback 
1bbc5 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a  transaction - ".
1bbc6 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61          "SQL sta
1bbc7 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72  tements in progr
1bbc8 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20  ess");.    rc = 
1bbc9 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
1bbca 65 6c 73 65 20 69 66 28 20 74 75 72 6e 4f 6e 41  else if( turnOnA
1bbcb 43 20 26 26 20 21 72 6f 6c 6c 62 61 63 6b 20 26  C && !rollback &
1bbcc 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  & db->writeVdbeC
1bbcd 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49  nt>1 ){.    /* I
1bbce 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  f this instructi
1bbcf 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  on implements a 
1bbd0 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 74 68 65 72  COMMIT and other
1bbd1 20 56 4d 73 20 61 72 65 20 77 72 69 74 69 6e 67   VMs are writing
1bbd2 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61  .    ** return a
1bbd3 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69  n error indicati
1bbd4 6e 67 20 74 68 61 74 20 74 68 65 20 6f 74 68 65  ng that the othe
1bbd5 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c  r VMs must compl
1bbd6 65 74 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20  ete first. .    
1bbd7 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  */.    sqlite3Se
1bbd8 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1bbd9 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74  Msg, db, "cannot
1bbda 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74   commit transact
1bbdb 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20  ion - ".        
1bbdc 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20  "SQL statements 
1bbdd 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20  in progress");. 
1bbde 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
1bbdf 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  USY;.  }else if(
1bbe0 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d   desiredAutoComm
1bbe1 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  it!=db->autoComm
1bbe2 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 72 6f  it ){.    if( ro
1bbe3 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20  llback ){.      
1bbe4 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 41  assert( desiredA
1bbe5 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a  utoCommit==1 );.
1bbe6 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c        sqlite3Rol
1bbe7 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20  lbackAll(db);.  
1bbe8 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1bbe9 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  it = 1;.    }els
1bbea 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74  e{.      db->aut
1bbeb 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 64 65  oCommit = (u8)de
1bbec 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b  siredAutoCommit;
1bbed 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1bbee 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53  e3VdbeHalt(p)==S
1bbef 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
1bbf0 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
1bbf1 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75  ;.        db->au
1bbf2 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28  toCommit = (u8)(
1bbf3 31 2d 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  1-desiredAutoCom
1bbf4 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  mit);.        p-
1bbf5 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54  >rc = rc = SQLIT
1bbf6 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20  E_BUSY;.        
1bbf7 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1bbf8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1bbf9 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1bbfa 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 3b  nStatement==0 );
1bbfb 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73  .    sqlite3Clos
1bbfc 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b  eSavepoints(db);
1bbfd 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d  .    if( p->rc==
1bbfe 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1bbff 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
1bc00 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ONE;.    }else{.
1bc01 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1bc02 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
1bc03 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
1bc04 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  urn;.  }else{.  
1bc05 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1bc06 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1bc07 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21 64 65  db,.        (!de
1bc08 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 29  siredAutoCommit)
1bc09 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 74 20 61  ?"cannot start a
1bc0a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 74   transaction wit
1bc0b 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  hin a transactio
1bc0c 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 28 72 6f  n":(.        (ro
1bc0d 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20  llback)?"cannot 
1bc0e 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72  rollback - no tr
1bc0f 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
1bc10 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20  ive":.          
1bc11 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74           "cannot
1bc12 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61   commit - no tra
1bc13 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
1bc14 76 65 22 29 29 3b 0a 20 20 20 20 20 20 20 20 20  ve"));.         
1bc15 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1bc16 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 62 72  _ERROR;.  }.  br
1bc17 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1bc18 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 50  e: Transaction P
1bc19 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1bc1a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63   Begin a transac
1bc1b 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 6e 73  tion.  The trans
1bc1c 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 65 6e  action ends when
1bc1d 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 6f 6c   a Commit or Rol
1bc1e 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64 65 20  lback.** opcode 
1bc1f 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20  is encountered. 
1bc20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   Depending on th
1bc21 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 73 65  e ON CONFLICT se
1bc22 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20 74 72  tting, the.** tr
1bc23 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20  ansaction might 
1bc24 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  also be rolled b
1bc25 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ack if an error 
1bc26 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  is encountered..
1bc27 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
1bc28 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
1bc29 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 77 68  abase file on wh
1bc2a 69 63 68 20 74 68 65 20 74 72 61 6e 73 61 63 74  ich the transact
1bc2b 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72 74 65  ion is.** starte
1bc2c 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73 20 74  d.  Index 0 is t
1bc2d 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1bc2e 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65 78 20   file and index 
1bc2f 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65  1 is the.** file
1bc30 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72   used for tempor
1bc31 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49 6e 64  ary tables.  Ind
1bc32 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d 6f 72  ices of 2 or mor
1bc33 65 20 61 72 65 20 75 73 65 64 20 66 6f 72 0a 2a  e are used for.*
1bc34 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  * attached datab
1bc35 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  ases..**.** If P
1bc36 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  2 is non-zero, t
1bc37 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e  hen a write-tran
1bc38 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
1bc39 65 64 2e 20 20 41 20 52 45 53 45 52 56 45 44 20  ed.  A RESERVED 
1bc3a 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69  lock is.** obtai
1bc3b 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ned on the datab
1bc3c 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 61 20  ase file when a 
1bc3d 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1bc3e 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 4e  n is started.  N
1bc3f 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65  o.** other proce
1bc40 73 73 20 63 61 6e 20 73 74 61 72 74 20 61 6e 6f  ss can start ano
1bc41 74 68 65 72 20 77 72 69 74 65 20 74 72 61 6e 73  ther write trans
1bc42 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69  action while thi
1bc43 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  s transaction is
1bc44 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20 20 53  .** underway.  S
1bc45 74 61 72 74 69 6e 67 20 61 20 77 72 69 74 65 20  tarting a write 
1bc46 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 73 6f  transaction also
1bc47 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c 6c 62   creates a rollb
1bc48 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 0a 2a  ack journal. A.*
1bc49 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  * write transact
1bc4a 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72  ion must be star
1bc4b 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63  ted before any c
1bc4c 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20 6d 61  hanges can be ma
1bc4d 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74  de to the.** dat
1bc4e 61 62 61 73 65 2e 20 20 49 66 20 50 32 20 69 73  abase.  If P2 is
1bc4f 20 32 20 6f 72 20 67 72 65 61 74 65 72 20 74 68   2 or greater th
1bc50 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  en an EXCLUSIVE 
1bc51 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f 62 74  lock is also obt
1bc52 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  ained.** on the 
1bc53 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  file..**.** If P
1bc54 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  2 is zero, then 
1bc55 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f  a read-lock is o
1bc56 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64  btained on the d
1bc57 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f  atabase file..*/
1bc58 0a 63 61 73 65 20 4f 50 5f 54 72 61 6e 73 61 63  .case OP_Transac
1bc59 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74 20 69 20  tion: {.  int i 
1bc5a 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 72  = pOp->p1;.  Btr
1bc5b 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73 73 65  ee *pBt;..  asse
1bc5c 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62  rt( i>=0 && i<db
1bc5d 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1bc5e 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1bc5f 20 26 20 28 31 3c 3c 69 29 29 21 3d 30 20 29 3b   & (1<<i))!=0 );
1bc60 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  .  pBt = db->aDb
1bc61 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20  [i].pBt;..  if( 
1bc62 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  pBt ){.    rc = 
1bc63 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
1bc64 6e 54 72 61 6e 73 28 70 42 74 2c 20 70 4f 70 2d  nTrans(pBt, pOp-
1bc65 3e 70 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63  >p2);.    if( rc
1bc66 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b  ==SQLITE_BUSY ){
1bc67 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70  .      p->pc = p
1bc68 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d  c;.      p->rc =
1bc69 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1bc6a 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64  Y;.      goto vd
1bc6b 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d  be_return;.    }
1bc6c 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1bc6d 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
1bc6e 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 2f 2a  LITE_READONLY /*
1bc6f 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42   && rc!=SQLITE_B
1bc70 55 53 59 20 2a 2f 20 29 7b 0a 20 20 20 20 20 20  USY */ ){.      
1bc71 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1bc72 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  o_error;.    }. 
1bc73 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1bc74 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 6f  * Opcode: ReadCo
1bc75 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20  okie P1 P2 P3 * 
1bc76 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f  *.**.** Read coo
1bc77 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72  kie number P3 fr
1bc78 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61  om database P1 a
1bc79 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f  nd write it into
1bc7a 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a   register P2..**
1bc7b 20 50 33 3d 3d 30 20 69 73 20 74 68 65 20 73 63   P3==0 is the sc
1bc7c 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50  hema version.  P
1bc7d 33 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61  3==1 is the data
1bc7e 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20  base format..** 
1bc7f 50 33 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63  P3==2 is the rec
1bc80 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63  ommended pager c
1bc81 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73  ache size, and s
1bc82 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20  o forth.  P1==0 
1bc83 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  is.** the main d
1bc84 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1bc85 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P1==1 is the da
1bc86 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64  tabase file used
1bc87 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d   to store.** tem
1bc88 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
1bc89 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 65  *.** If P1 is ne
1bc8a 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 69  gative, then thi
1bc8b 73 20 69 73 20 61 20 72 65 71 75 65 73 74 20 74  s is a request t
1bc8c 6f 20 72 65 61 64 20 74 68 65 20 73 69 7a 65 20  o read the size 
1bc8d 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65  of a.** database
1bc8e 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 50 33 20  s free-list. P3 
1bc8f 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 31  must be set to 1
1bc90 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 54   in this case. T
1bc91 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 64 61 74  he actual.** dat
1bc92 61 62 61 73 65 20 61 63 63 65 73 73 65 64 20 69  abase accessed i
1bc93 73 20 28 28 50 31 2b 31 29 2a 2d 31 29 2e 20 46  s ((P1+1)*-1). F
1bc94 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 20 50 31  or example, a P1
1bc95 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 2d 31   parameter of -1
1bc96 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  .** corresponds 
1bc97 74 6f 20 64 61 74 61 62 61 73 65 20 30 20 28 22  to database 0 ("
1bc98 6d 61 69 6e 22 29 2c 20 61 20 50 31 20 6f 66 20  main"), a P1 of 
1bc99 2d 32 20 69 73 20 64 61 74 61 62 61 73 65 20 31  -2 is database 1
1bc9a 20 28 22 74 65 6d 70 22 29 2e 0a 2a 2a 0a 2a 2a   ("temp")..**.**
1bc9b 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
1bc9c 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
1bc9d 65 20 64 61 74 61 62 61 73 65 20 28 65 69 74 68  e database (eith
1bc9e 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  er a transaction
1bc9f 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 61 72  .** must be star
1bca0 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d 75 73  ted or there mus
1bca1 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72  t be an open cur
1bca2 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a 20 65  sor) before.** e
1bca3 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e  xecuting this in
1bca4 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
1bca5 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  se OP_ReadCookie
1bca6 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1bca7 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1bca8 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d  ease */.  int iM
1bca9 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d  eta;.  int iDb =
1bcaa 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20   pOp->p1;.  int 
1bcab 69 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70  iCookie = pOp->p
1bcac 33 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  3;..  assert( pO
1bcad 70 2d 3e 70 33 3c 53 51 4c 49 54 45 5f 4e 5f 42  p->p3<SQLITE_N_B
1bcae 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 69  TREE_META );.  i
1bcaf 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20  f( iDb<0 ){.    
1bcb0 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62 2b 31  iDb = (-1*(iDb+1
1bcb1 29 29 3b 0a 20 20 20 20 69 43 6f 6f 6b 69 65 20  ));.    iCookie 
1bcb2 2a 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 61 73 73  *= -1;.  }.  ass
1bcb3 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
1bcb4 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
1bcb5 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
1bcb6 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20  iDb].pBt!=0 );. 
1bcb7 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1bcb8 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62  eeMask & (1<<iDb
1bcb9 29 29 21 3d 30 20 29 3b 0a 20 20 2f 2a 20 54 68  ))!=0 );.  /* Th
1bcba 65 20 69 6e 64 65 78 69 6e 67 20 6f 66 20 6d 65  e indexing of me
1bcbb 74 61 20 76 61 6c 75 65 73 20 61 74 20 74 68 65  ta values at the
1bcbc 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 69 73   schema layer is
1bcbd 20 6f 66 66 20 62 79 20 6f 6e 65 20 66 72 6f 6d   off by one from
1bcbe 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 69  .  ** the indexi
1bcbf 6e 67 20 69 6e 20 74 68 65 20 62 74 72 65 65 20  ng in the btree 
1bcc0 6c 61 79 65 72 2e 20 20 54 68 65 20 62 74 72 65  layer.  The btre
1bcc1 65 20 63 6f 6e 73 69 64 65 72 73 20 6d 65 74 61  e considers meta
1bcc2 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20 62 65 20 74  [0] to.  ** be t
1bcc3 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65  he number of fre
1bcc4 65 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  e pages in the d
1bcc5 61 74 61 62 61 73 65 20 28 61 20 72 65 61 64 2d  atabase (a read-
1bcc6 6f 6e 6c 79 20 76 61 6c 75 65 29 0a 20 20 2a 2a  only value).  **
1bcc7 20 61 6e 64 20 6d 65 74 61 5b 31 5d 20 74 6f 20   and meta[1] to 
1bcc8 62 65 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  be the schema co
1bcc9 6f 6b 69 65 2e 20 20 54 68 65 20 73 63 68 65 6d  okie.  The schem
1bcca 61 20 6c 61 79 65 72 20 63 6f 6e 73 69 64 65 72  a layer consider
1bccb 73 0a 20 20 2a 2a 20 6d 65 74 61 5b 31 5d 20 74  s.  ** meta[1] t
1bccc 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d 61 20  o be the schema 
1bccd 63 6f 6f 6b 69 65 2e 20 20 53 6f 20 77 65 20 68  cookie.  So we h
1bcce 61 76 65 20 74 6f 20 73 68 69 66 74 20 74 68 65  ave to shift the
1bccf 20 69 6e 64 65 78 0a 20 20 2a 2a 20 62 79 20 6f   index.  ** by o
1bcd0 6e 65 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  ne in the follow
1bcd1 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20  ing statement.. 
1bcd2 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
1bcd3 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 64  e3BtreeGetMeta(d
1bcd4 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c  b->aDb[iDb].pBt,
1bcd5 20 31 20 2b 20 69 43 6f 6f 6b 69 65 2c 20 28 75   1 + iCookie, (u
1bcd6 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20 20  32 *)&iMeta);.  
1bcd7 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 69 4d 65 74  pOut->u.i = iMet
1bcd8 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  a;.  MemSetTypeF
1bcd9 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1bcda 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1bcdb 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f  /* Opcode: SetCo
1bcdc 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20  okie P1 P2 P3 * 
1bcdd 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  *.**.** Write th
1bcde 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67  e content of reg
1bcdf 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 72 70  ister P3 (interp
1bce0 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65  reted as an inte
1bce1 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f  ger).** into coo
1bce2 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66  kie number P2 of
1bce3 20 64 61 74 61 62 61 73 65 20 50 31 2e 0a 2a 2a   database P1..**
1bce4 20 50 32 3d 3d 30 20 69 73 20 74 68 65 20 73 63   P2==0 is the sc
1bce5 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50  hema version.  P
1bce6 32 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61  2==1 is the data
1bce7 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20  base format..** 
1bce8 50 32 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63  P2==2 is the rec
1bce9 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63  ommended pager c
1bcea 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73  ache size, and s
1bceb 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20  o forth.  P1==0 
1bcec 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  is.** the main d
1bced 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1bcee 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P1==1 is the da
1bcef 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64  tabase file used
1bcf0 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d   to store.** tem
1bcf1 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
1bcf2 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69  *.** A transacti
1bcf3 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74  on must be start
1bcf4 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74  ed before execut
1bcf5 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
1bcf6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43  .*/.case OP_SetC
1bcf7 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f  ookie: {       /
1bcf8 2a 20 69 6e 33 20 2a 2f 0a 20 20 44 62 20 2a 70  * in3 */.  Db *p
1bcf9 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  Db;.  assert( pO
1bcfa 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42  p->p2<SQLITE_N_B
1bcfb 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61  TREE_META );.  a
1bcfc 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1bcfd 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d  0 && pOp->p1<db-
1bcfe 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1bcff 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1bd00 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21  & (1<<pOp->p1))!
1bd01 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  =0 );.  pDb = &d
1bd02 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b  b->aDb[pOp->p1];
1bd03 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 2d 3e  .  assert( pDb->
1bd04 70 42 74 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69  pBt!=0 );.  sqli
1bd05 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
1bd06 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a  rify(pIn3);.  /*
1bd07 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20   See note about 
1bd08 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f  index shifting o
1bd09 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20  n OP_ReadCookie 
1bd0a 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
1bd0b 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
1bd0c 28 70 44 62 2d 3e 70 42 74 2c 20 31 2b 70 4f 70  (pDb->pBt, 1+pOp
1bd0d 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d  ->p2, (int)pIn3-
1bd0e 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70  >u.i);.  if( pOp
1bd0f 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  ->p2==0 ){.    /
1bd10 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68 65 6d  * When the schem
1bd11 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73  a cookie changes
1bd12 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77  , record the new
1bd13 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e 61 6c   cookie internal
1bd14 6c 79 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70  ly */.    pDb->p
1bd15 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
1bd16 6f 6f 6b 69 65 20 3d 20 28 69 6e 74 29 70 49 6e  ookie = (int)pIn
1bd17 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e  3->u.i;.    db->
1bd18 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
1bd19 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
1bd1a 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
1bd1b 70 32 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  p2==1 ){.    /* 
1bd1c 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69  Record changes i
1bd1d 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61  n the file forma
1bd1e 74 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53  t */.    pDb->pS
1bd1f 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
1bd20 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75  at = (u8)pIn3->u
1bd21 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f  .i;.  }.  if( pO
1bd22 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20  p->p1==1 ){.    
1bd23 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c  /* Invalidate al
1bd24 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  l prepared state
1bd25 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74  ments whenever t
1bd26 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
1bd27 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69  .    ** schema i
1bd28 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b  s changed.  Tick
1bd29 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20  et #1644 */.    
1bd2a 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
1bd2b 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
1bd2c 64 62 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  db);.  }.  break
1bd2d 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1bd2e 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20  VerifyCookie P1 
1bd2f 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b  P2 *.**.** Check
1bd30 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c   the value of gl
1bd31 6f 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 61  obal database pa
1bd32 72 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30  rameter number 0
1bd33 20 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20   (the.** schema 
1bd34 76 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b  version) and mak
1bd35 65 20 73 75 72 65 20 69 74 20 69 73 20 65 71 75  e sure it is equ
1bd36 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50  al to P2.  .** P
1bd37 31 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  1 is the databas
1bd38 65 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69  e number which i
1bd39 73 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  s 0 for the main
1bd3a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
1bd3b 2a 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20  * and 1 for the 
1bd3c 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d  file holding tem
1bd3d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e  porary tables an
1bd3e 64 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75  d some higher nu
1bd3f 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69  mber.** for auxi
1bd40 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e  liary databases.
1bd41 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69  .**.** The cooki
1bd42 65 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 61  e changes its va
1bd43 6c 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 65  lue whenever the
1bd44 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1bd45 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69   changes..** Thi
1bd46 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75  s operation is u
1bd47 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 68  sed to detect wh
1bd48 65 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b  en that the cook
1bd49 69 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a  ie has changed.*
1bd4a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63  * and that the c
1bd4b 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e  urrent process n
1bd4c 65 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 74  eeds to reread t
1bd4d 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a  he schema..**.**
1bd4e 20 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 61   Either a transa
1bd4f 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68  ction needs to h
1bd50 61 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 64  ave been started
1bd51 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e   or an OP_Open n
1bd52 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78  eeds.** to be ex
1bd53 65 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 62  ecuted (to estab
1bd54 6c 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b  lish a read lock
1bd55 29 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70  ) before this op
1bd56 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b  code is.** invok
1bd57 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  ed..*/.case OP_V
1bd58 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 20  erifyCookie: {. 
1bd59 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 74   int iMeta;.  Bt
1bd5a 72 65 65 20 2a 70 42 74 3b 0a 20 20 61 73 73 65  ree *pBt;.  asse
1bd5b 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1bd5c 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
1bd5d 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1bd5e 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1bd5f 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20  1<<pOp->p1))!=0 
1bd60 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61  );.  pBt = db->a
1bd61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b  Db[pOp->p1].pBt;
1bd62 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20  .  if( pBt ){.  
1bd63 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1bd64 72 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c 20  reeGetMeta(pBt, 
1bd65 31 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 74 61  1, (u32 *)&iMeta
1bd66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1bd67 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1bd68 20 20 20 20 69 4d 65 74 61 20 3d 20 30 3b 0a 20      iMeta = 0;. 
1bd69 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
1bd6a 49 54 45 5f 4f 4b 20 26 26 20 69 4d 65 74 61 21  ITE_OK && iMeta!
1bd6b 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20  =pOp->p2 ){.    
1bd6c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1bd6d 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1bd6e 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
1bd6f 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1bd70 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 73 63  db, "database sc
1bd71 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64  hema has changed
1bd72 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  ");.    /* If th
1bd73 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20  e schema-cookie 
1bd74 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1bd75 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74  e file matches t
1bd76 68 65 20 63 6f 6f 6b 69 65 20 0a 20 20 20 20 2a  he cookie .    *
1bd77 2a 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68  * stored with th
1bd78 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72  e in-memory repr
1bd79 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
1bd7a 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20  e schema, do.   
1bd7b 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74   ** not reload t
1bd7c 68 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74  he schema from t
1bd7d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1bd7e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1bd7f 49 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  If virtual-table
1bd80 73 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68  s are in use, th
1bd81 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61  is is not just a
1bd82 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a  n optimization..
1bd83 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d      ** Often, v-
1bd84 74 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65  tables store the
1bd85 69 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72  ir data in other
1bd86 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20   SQLite tables, 
1bd87 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65  which.    ** are
1bd88 20 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69   queried from wi
1bd89 74 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64  thin xNext() and
1bd8a 20 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d   other v-table m
1bd8b 65 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20  ethods using.   
1bd8c 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65   ** prepared que
1bd8d 72 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20  ries. If such a 
1bd8e 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d  query is out-of-
1bd8f 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20  date, we do not 
1bd90 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64  want to.    ** d
1bd91 69 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62  iscard the datab
1bd92 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74  ase schema, as t
1bd93 68 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70  he user code imp
1bd94 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20  lementing the.  
1bd95 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75    ** v-table wou
1bd96 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65  ld have to be re
1bd97 61 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ady for the sqli
1bd98 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
1bd99 72 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a  re itself.    **
1bd9a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74   to be invalidat
1bd9b 65 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69  ed whenever sqli
1bd9c 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61  te3_step() is ca
1bd9d 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
1bd9e 20 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62   .    ** a v-tab
1bd9f 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a  le method..    *
1bda0 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44  /.    if( db->aD
1bda1 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65  b[pOp->p1].pSche
1bda2 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
1bda3 65 21 3d 69 4d 65 74 61 20 29 7b 0a 20 20 20 20  e!=iMeta ){.    
1bda4 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
1bda5 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
1bda6 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d   pOp->p1);.    }
1bda7 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ..    sqlite3Exp
1bda8 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
1bda9 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 72  ments(db);.    r
1bdaa 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  c = SQLITE_SCHEM
1bdab 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  A;.  }.  break;.
1bdac 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  }../* Opcode: Op
1bdad 65 6e 52 65 61 64 20 50 31 20 50 32 20 50 33 20  enRead P1 P2 P3 
1bdae 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e  P4 P5.**.** Open
1bdaf 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72   a read-only cur
1bdb0 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61  sor for the data
1bdb1 62 61 73 65 20 74 61 62 6c 65 20 77 68 6f 73 65  base table whose
1bdb2 20 72 6f 6f 74 20 70 61 67 65 20 69 73 0a 2a 2a   root page is.**
1bdb3 20 50 32 20 69 6e 20 61 20 64 61 74 61 62 61 73   P2 in a databas
1bdb4 65 20 66 69 6c 65 2e 20 20 54 68 65 20 64 61 74  e file.  The dat
1bdb5 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 64 65  abase file is de
1bdb6 74 65 72 6d 69 6e 65 64 20 62 79 20 50 33 2e 20  termined by P3. 
1bdb7 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61 6e 73 20  .** P3==0 means 
1bdb8 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1bdb9 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e 73 20 74  e, P3==1 means t
1bdba 68 65 20 64 61 74 61 62 61 73 65 20 75 73 65 64  he database used
1bdbb 20 66 6f 72 20 0a 2a 2a 20 74 65 6d 70 6f 72 61   for .** tempora
1bdbc 72 79 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 50  ry tables, and P
1bdbd 33 3e 31 20 6d 65 61 6e 73 20 75 73 65 64 20 74  3>1 means used t
1bdbe 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
1bdbf 20 61 74 74 61 63 68 65 64 0a 2a 2a 20 64 61 74   attached.** dat
1bdc0 61 62 61 73 65 2e 20 20 47 69 76 65 20 74 68 65  abase.  Give the
1bdc1 20 6e 65 77 20 63 75 72 73 6f 72 20 61 6e 20 69   new cursor an i
1bdc2 64 65 6e 74 69 66 69 65 72 20 6f 66 20 50 31 2e  dentifier of P1.
1bdc3 20 20 54 68 65 20 50 31 0a 2a 2a 20 76 61 6c 75    The P1.** valu
1bdc4 65 73 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 63  es need not be c
1bdc5 6f 6e 74 69 67 75 6f 75 73 20 62 75 74 20 61 6c  ontiguous but al
1bdc6 6c 20 50 31 20 76 61 6c 75 65 73 20 73 68 6f 75  l P1 values shou
1bdc7 6c 64 20 62 65 20 73 6d 61 6c 6c 20 69 6e 74 65  ld be small inte
1bdc8 67 65 72 73 2e 0a 2a 2a 20 49 74 20 69 73 20 61  gers..** It is a
1bdc9 6e 20 65 72 72 6f 72 20 66 6f 72 20 50 31 20 74  n error for P1 t
1bdca 6f 20 62 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a  o be negative..*
1bdcb 2a 0a 2a 2a 20 49 66 20 50 35 21 3d 30 20 74 68  *.** If P5!=0 th
1bdcc 65 6e 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65  en use the conte
1bdcd 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  nt of register P
1bdce 32 20 61 73 20 74 68 65 20 72 6f 6f 74 20 70 61  2 as the root pa
1bdcf 67 65 2c 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 76  ge, not.** the v
1bdd0 61 6c 75 65 20 6f 66 20 50 32 20 69 74 73 65 6c  alue of P2 itsel
1bdd1 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 77  f..**.** There w
1bdd2 69 6c 6c 20 62 65 20 61 20 72 65 61 64 20 6c 6f  ill be a read lo
1bdd3 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
1bdd4 73 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 72  se whenever ther
1bdd5 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20  e is an.** open 
1bdd6 63 75 72 73 6f 72 2e 20 20 49 66 20 74 68 65 20  cursor.  If the 
1bdd7 64 61 74 61 62 61 73 65 20 77 61 73 20 75 6e 6c  database was unl
1bdd8 6f 63 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 74  ocked prior to t
1bdd9 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a  his instruction.
1bdda 2a 2a 20 74 68 65 6e 20 61 20 72 65 61 64 20 6c  ** then a read l
1bddb 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65 64 20  ock is acquired 
1bddc 61 73 20 70 61 72 74 20 6f 66 20 74 68 69 73 20  as part of this 
1bddd 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 41 20  instruction.  A 
1bdde 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c  read.** lock all
1bddf 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ows other proces
1bde0 73 65 73 20 74 6f 20 72 65 61 64 20 74 68 65 20  ses to read the 
1bde1 64 61 74 61 62 61 73 65 20 62 75 74 20 70 72 6f  database but pro
1bde2 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79 20 6f 74  hibits.** any ot
1bde3 68 65 72 20 70 72 6f 63 65 73 73 20 66 72 6f 6d  her process from
1bde4 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 64   modifying the d
1bde5 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 72 65  atabase.  The re
1bde6 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 72 65  ad lock is.** re
1bde7 6c 65 61 73 65 64 20 77 68 65 6e 20 61 6c 6c 20  leased when all 
1bde8 63 75 72 73 6f 72 73 20 61 72 65 20 63 6c 6f 73  cursors are clos
1bde9 65 64 2e 20 20 49 66 20 74 68 69 73 20 69 6e 73  ed.  If this ins
1bdea 74 72 75 63 74 69 6f 6e 20 61 74 74 65 6d 70 74  truction attempt
1bdeb 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61 20 72 65  s.** to get a re
1bdec 61 64 20 6c 6f 63 6b 20 62 75 74 20 66 61 69 6c  ad lock but fail
1bded 73 2c 20 74 68 65 20 73 63 72 69 70 74 20 74 65  s, the script te
1bdee 72 6d 69 6e 61 74 65 73 20 77 69 74 68 20 61 6e  rminates with an
1bdef 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20  .** SQLITE_BUSY 
1bdf0 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  error code..**.*
1bdf1 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d  * The P4 value m
1bdf2 61 79 20 62 65 20 65 69 74 68 65 72 20 61 6e 20  ay be either an 
1bdf3 69 6e 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33  integer (P4_INT3
1bdf4 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20  2) or a pointer 
1bdf5 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20  to.** a KeyInfo 
1bdf6 73 74 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45  structure (P4_KE
1bdf7 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73  YINFO). If it is
1bdf8 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1bdf9 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75  KeyInfo .** stru
1bdfa 63 74 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64  cture, then said
1bdfb 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69 6e   structure defin
1bdfc 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61  es the content a
1bdfd 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a  nd collating .**
1bdfe 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65   sequence of the
1bdff 20 69 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65   index being ope
1be00 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
1be01 69 66 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65  if P4 is an inte
1be02 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69  ger .** value, i
1be03 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  t is set to the 
1be04 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
1be05 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  s in the table..
1be06 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f  **.** See also O
1be07 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a 2f 2a 20  penWrite..*/./* 
1be08 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57 72 69 74  Opcode: OpenWrit
1be09 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  e P1 P2 P3 P4 P5
1be0a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65  .**.** Open a re
1be0b 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20  ad/write cursor 
1be0c 6e 61 6d 65 64 20 50 31 20 6f 6e 20 74 68 65 20  named P1 on the 
1be0d 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77  table or index w
1be0e 68 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67  hose root.** pag
1be0f 65 20 69 73 20 50 32 2e 20 20 4f 72 20 69 66 20  e is P2.  Or if 
1be10 50 35 21 3d 30 20 75 73 65 20 74 68 65 20 63 6f  P5!=0 use the co
1be11 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
1be12 72 20 50 32 20 74 6f 20 66 69 6e 64 20 74 68 65  r P2 to find the
1be13 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 0a 2a  .** root page..*
1be14 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75  *.** The P4 valu
1be15 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20  e may be either 
1be16 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49  an integer (P4_I
1be17 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74  NT32) or a point
1be18 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e  er to.** a KeyIn
1be19 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 50 34  fo structure (P4
1be1a 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74  _KEYINFO). If it
1be1b 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1be1c 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73   a KeyInfo .** s
1be1d 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73  tructure, then s
1be1e 61 69 64 20 73 74 72 75 63 74 75 72 65 20 64 65  aid structure de
1be1f 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e  fines the conten
1be20 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20  t and collating 
1be21 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20  .** sequence of 
1be22 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20  the index being 
1be23 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  opened. Otherwis
1be24 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e 20 69  e, if P4 is an i
1be25 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65  nteger .** value
1be26 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74  , it is set to t
1be27 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1be28 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
1be29 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  e..**.** This in
1be2a 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20  struction works 
1be2b 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65  just like OpenRe
1be2c 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ad except that i
1be2d 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73  t opens the curs
1be2e 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72  or.** in read/wr
1be2f 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61  ite mode.  For a
1be30 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68   given table, th
1be31 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f  ere can be one o
1be32 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79  r more read-only
1be33 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61  .** cursors or a
1be34 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69   single read/wri
1be35 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f  te cursor but no
1be36 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65  t both..**.** Se
1be37 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e  e also OpenRead.
1be38 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e  .*/.case OP_Open
1be39 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f 4f 70  Read:.case OP_Op
1be3a 65 6e 57 72 69 74 65 3a 20 7b 0a 20 20 69 6e 74  enWrite: {.  int
1be3b 20 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 4b   nField = 0;.  K
1be3c 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
1be3d 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 20 3d 20   = 0;.  int i = 
1be3e 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70  pOp->p1;.  int p
1be3f 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69  2 = pOp->p2;.  i
1be40 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33  nt iDb = pOp->p3
1be41 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a  ;.  int wrFlag;.
1be42 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56    Btree *pX;.  V
1be43 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  dbeCursor *pCur;
1be44 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 0a 20  .  Db *pDb;.  . 
1be45 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
1be46 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
1be47 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
1be48 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c  btreeMask & (1<<
1be49 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 70 44  iDb))!=0 );.  pD
1be4a 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
1be4b 5d 3b 0a 20 20 70 58 20 3d 20 70 44 62 2d 3e 70  ];.  pX = pDb->p
1be4c 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 58  Bt;.  assert( pX
1be4d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70  !=0 );.  if( pOp
1be4e 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65  ->opcode==OP_Ope
1be4f 6e 57 72 69 74 65 20 29 7b 0a 20 20 20 20 77 72  nWrite ){.    wr
1be50 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 66  Flag = 1;.    if
1be51 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
1be52 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d  file_format < p-
1be53 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72  >minWriteFileFor
1be54 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  mat ){.      p->
1be55 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d  minWriteFileForm
1be56 61 74 20 3d 20 70 44 62 2d 3e 70 53 63 68 65 6d  at = pDb->pSchem
1be57 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a  a->file_format;.
1be58 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1be59 20 20 20 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20     wrFlag = 0;. 
1be5a 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35   }.  if( pOp->p5
1be5b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1be5c 70 32 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  p2>0 );.    asse
1be5d 72 74 28 20 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  rt( p2<=p->nMem 
1be5e 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 70  );.    pIn2 = &p
1be5f 2d 3e 61 4d 65 6d 5b 70 32 5d 3b 0a 20 20 20 20  ->aMem[p2];.    
1be60 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
1be61 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a  tegerify(pIn2);.
1be62 20 20 20 20 70 32 20 3d 20 28 69 6e 74 29 70 49      p2 = (int)pI
1be63 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 69 66 28  n2->u.i;.    if(
1be64 20 70 32 3c 32 20 29 20 7b 0a 20 20 20 20 20 20   p2<2 ) {.      
1be65 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
1be66 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
1be67 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1be68 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  o_error;.    }. 
1be69 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d   }.  assert( i>=
1be6a 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  0 );.  if( pOp->
1be6b 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e  p4type==P4_KEYIN
1be6c 46 4f 20 29 7b 0a 20 20 20 20 70 4b 65 79 49 6e  FO ){.    pKeyIn
1be6d 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65  fo = pOp->p4.pKe
1be6e 79 49 6e 66 6f 3b 0a 20 20 20 20 70 4b 65 79 49  yInfo;.    pKeyI
1be6f 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70  nfo->enc = ENC(p
1be70 2d 3e 64 62 29 3b 0a 20 20 20 20 6e 46 69 65 6c  ->db);.    nFiel
1be71 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  d = pKeyInfo->nF
1be72 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73 65 20  ield+1;.  }else 
1be73 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  if( pOp->p4type=
1be74 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20 20  =P4_INT32 ){.   
1be75 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70   nField = pOp->p
1be76 34 2e 69 3b 0a 20 20 7d 0a 20 20 70 43 75 72 20  4.i;.  }.  pCur 
1be77 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72  = allocateCursor
1be78 28 70 2c 20 69 2c 20 6e 46 69 65 6c 64 2c 20 69  (p, i, nField, i
1be79 44 62 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 43  Db, 1);.  if( pC
1be7a 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  ur==0 ) goto no_
1be7b 6d 65 6d 3b 0a 20 20 70 43 75 72 2d 3e 6e 75 6c  mem;.  pCur->nul
1be7c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d  lRow = 1;.  rc =
1be7d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
1be7e 73 6f 72 28 70 58 2c 20 70 32 2c 20 77 72 46 6c  sor(pX, p2, wrFl
1be7f 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43  ag, pKeyInfo, pC
1be80 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  ur->pCursor);.  
1be81 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d  pCur->pKeyInfo =
1be82 20 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 20 73 77   pKeyInfo;..  sw
1be83 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20  itch( rc ){.    
1be84 63 61 73 65 20 53 51 4c 49 54 45 5f 42 55 53 59  case SQLITE_BUSY
1be85 3a 20 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20  : {.      p->pc 
1be86 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72  = pc;.      p->r
1be87 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  c = rc = SQLITE_
1be88 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BUSY;.      goto
1be89 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20   vdbe_return;.  
1be8a 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
1be8b 49 54 45 5f 4f 4b 3a 20 7b 0a 20 20 20 20 20 20  ITE_OK: {.      
1be8c 69 6e 74 20 66 6c 61 67 73 20 3d 20 73 71 6c 69  int flags = sqli
1be8d 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43  te3BtreeFlags(pC
1be8e 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  ur->pCursor);.  
1be8f 20 20 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68      /* Sanity ch
1be90 65 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 74 68  ecking.  Only th
1be91 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74  e lower four bit
1be92 73 20 6f 66 20 74 68 65 20 66 6c 61 67 73 20 62  s of the flags b
1be93 79 74 65 20 73 68 6f 75 6c 64 0a 20 20 20 20 20  yte should.     
1be94 20 2a 2a 20 62 65 20 75 73 65 64 2e 20 20 42 69   ** be used.  Bi
1be95 74 20 33 20 28 6d 61 73 6b 20 30 78 30 38 29 20  t 3 (mask 0x08) 
1be96 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65  is unpredictable
1be97 2e 20 20 54 68 65 20 6c 6f 77 65 72 20 33 20 62  .  The lower 3 b
1be98 69 74 73 0a 20 20 20 20 20 20 2a 2a 20 28 6d 61  its.      ** (ma
1be99 73 6b 20 30 78 30 37 29 20 73 68 6f 75 6c 64 20  sk 0x07) should 
1be9a 62 65 20 65 69 74 68 65 72 20 35 20 28 69 6e 74  be either 5 (int
1be9b 6b 65 79 2b 6c 65 61 66 64 61 74 61 20 66 6f 72  key+leafdata for
1be9c 20 74 61 62 6c 65 73 29 20 6f 72 0a 20 20 20 20   tables) or.    
1be9d 20 20 2a 2a 20 32 20 28 7a 65 72 6f 64 61 74 61    ** 2 (zerodata
1be9e 20 66 6f 72 20 69 6e 64 69 63 65 73 29 2e 20 20   for indices).  
1be9f 49 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69  If these conditi
1bea0 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6d 65 74 20  ons are not met 
1bea1 69 74 20 63 61 6e 0a 20 20 20 20 20 20 2a 2a 20  it can.      ** 
1bea2 6f 6e 6c 79 20 6d 65 61 6e 20 74 68 61 74 20 77  only mean that w
1bea3 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
1bea4 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  th a corrupt dat
1bea5 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20  abase file.     
1bea6 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 66   */.      if( (f
1bea7 6c 61 67 73 20 26 20 30 78 66 30 29 21 3d 30 20  lags & 0xf0)!=0 
1bea8 7c 7c 20 28 28 66 6c 61 67 73 20 26 20 30 78 30  || ((flags & 0x0
1bea9 37 29 21 3d 35 20 26 26 20 28 66 6c 61 67 73 20  7)!=5 && (flags 
1beaa 26 20 30 78 30 37 29 21 3d 32 29 20 29 7b 0a 20  & 0x07)!=2) ){. 
1beab 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1beac 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1bead 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
1beae 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1beaf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1beb0 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20  pCur->isTable = 
1beb1 28 66 6c 61 67 73 20 26 20 42 54 52 45 45 5f 49  (flags & BTREE_I
1beb2 4e 54 4b 45 59 29 21 3d 30 20 3f 31 3a 30 3b 0a  NTKEY)!=0 ?1:0;.
1beb3 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e        pCur->isIn
1beb4 64 65 78 20 3d 20 28 66 6c 61 67 73 20 26 20 42  dex = (flags & B
1beb5 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 21 3d  TREE_ZERODATA)!=
1beb6 30 20 3f 31 3a 30 3b 0a 20 20 20 20 20 20 2f 2a  0 ?1:0;.      /*
1beb7 20 49 66 20 50 34 3d 3d 30 20 69 74 20 6d 65 61   If P4==0 it mea
1beb8 6e 73 20 77 65 20 61 72 65 20 65 78 70 65 63 74  ns we are expect
1beb9 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74 61 62  ed to open a tab
1beba 6c 65 2e 20 20 49 66 20 50 34 21 3d 30 20 74 68  le.  If P4!=0 th
1bebb 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 65  en.      ** we e
1bebc 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70 65 6e  xpect to be open
1bebd 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20 20 49  ing an index.  I
1bebe 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 77 68  f this is not wh
1bebf 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20 20 20  at happened,.   
1bec0 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 64     ** then the d
1bec1 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75  atabase is corru
1bec2 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  pt.      */.    
1bec3 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69 73 54    if( (pCur->isT
1bec4 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70 34 74  able && pOp->p4t
1bec5 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 29  ype==P4_KEYINFO)
1bec6 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43 75 72  .       || (pCur
1bec7 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70 4f 70  ->isIndex && pOp
1bec8 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 59  ->p4type!=P4_KEY
1bec9 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20 20 20  INFO) ){.       
1beca 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
1becb 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
1becc 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1becd 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1bece 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1becf 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1bed0 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20 7b 0a  SQLITE_EMPTY: {.
1bed1 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 54 61        pCur->isTa
1bed2 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70  ble = pOp->p4typ
1bed3 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20  e!=P4_KEYINFO;. 
1bed4 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64       pCur->isInd
1bed5 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73 54 61  ex = !pCur->isTa
1bed6 62 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d  ble;.      pCur-
1bed7 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  >pCursor = 0;.  
1bed8 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1bed9 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OK;.      break;
1beda 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
1bedb 6c 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f  lt: {.      goto
1bedc 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1bedd 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ror;.    }.  }. 
1bede 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1bedf 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65  code: OpenEpheme
1bee0 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a  ral P1 P2 * P4 *
1bee1 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65  .**.** Open a ne
1bee2 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61  w cursor P1 to a
1bee3 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65   transient table
1bee4 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20  ..** The cursor 
1bee5 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64  is always opened
1bee6 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e   read/write even
1bee7 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e   if .** the main
1bee8 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 61   database is rea
1bee9 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 61  d-only.  The tra
1beea 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 75 61  nsient or virtua
1beeb 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65  l.** table is de
1beec 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61  leted automatica
1beed 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75 72  lly when the cur
1beee 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a  sor is closed..*
1beef 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e  *.** P2 is the n
1bef0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1bef1 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20   in the virtual 
1bef2 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75  table..** The cu
1bef3 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61  rsor points to a
1bef4 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 66 20   BTree table if 
1bef5 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 42  P4==0 and to a B
1bef6 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66  Tree index.** if
1bef7 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 49   P4 is not 0.  I
1bef8 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f P4 is not NULL
1bef9 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  , it points to a
1befa 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
1befb 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 6e  re.** that defin
1befc 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66  es the format of
1befd 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 6e 64   keys in the ind
1befe 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  ex..**.** This o
1beff 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 63  pcode was once c
1bf00 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20  alled OpenTemp. 
1bf01 20 42 75 74 20 74 68 61 74 20 63 72 65 61 74 65   But that create
1bf02 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62  d.** confusion b
1bf03 65 63 61 75 73 65 20 74 68 65 20 74 65 72 6d 20  ecause the term 
1bf04 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69  "temp table", mi
1bf05 67 68 74 20 72 65 66 65 72 20 65 69 74 68 65 72  ght refer either
1bf06 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 61  .** to a TEMP ta
1bf07 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c 20 6c  ble at the SQL l
1bf08 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61  evel, or to a ta
1bf09 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a  ble opened by.**
1bf0a 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 54   this opcode.  T
1bf0b 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20  hen this opcode 
1bf0c 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72  was call OpenVir
1bf0d 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68  tual.  But.** th
1bf0e 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e 66 75  at created confu
1bf0f 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 77 68  sion with the wh
1bf10 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  ole virtual-tabl
1bf11 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20  e idea..*/.case 
1bf12 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
1bf13 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f  : {.  int i = pO
1bf14 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72  p->p1;.  VdbeCur
1bf15 73 6f 72 20 2a 70 43 78 3b 0a 20 20 73 74 61 74  sor *pCx;.  stat
1bf16 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65  ic const int ope
1bf17 6e 46 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20  nFlags = .      
1bf18 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
1bf19 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51  WRITE |.      SQ
1bf1a 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
1bf1b 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f   |.      SQLITE_
1bf1c 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c  OPEN_EXCLUSIVE |
1bf1d 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
1bf1e 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
1bf1f 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f   |.      SQLITE_
1bf20 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44  OPEN_TRANSIENT_D
1bf21 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  B;..  assert( i>
1bf22 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 6c  =0 );.  pCx = al
1bf23 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20  locateCursor(p, 
1bf24 69 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20  i, pOp->p2, -1, 
1bf25 31 29 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30  1);.  if( pCx==0
1bf26 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1bf27 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d    pCx->nullRow =
1bf28 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   1;.  rc = sqlit
1bf29 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64  e3BtreeFactory(d
1bf2a 62 2c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f  b, 0, 1, SQLITE_
1bf2b 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43  DEFAULT_TEMP_CAC
1bf2c 48 45 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61  HE_SIZE, openFla
1bf2d 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  gs,.            
1bf2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
1bf2f 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28  pCx->pBt);.  if(
1bf30 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1bf31 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1bf32 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
1bf33 73 28 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a  s(pCx->pBt, 1);.
1bf34 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
1bf35 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
1bf36 2a 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74  * If a transient
1bf37 20 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72   index is requir
1bf38 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79  ed, create it by
1bf39 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20   calling.    ** 
1bf3a 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
1bf3b 74 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74  teTable() with t
1bf3c 68 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54  he BTREE_ZERODAT
1bf3d 41 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20  A flag before.  
1bf3e 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e    ** opening it.
1bf3f 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20   If a transient 
1bf40 74 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65  table is require
1bf41 64 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a  d, just use the.
1bf42 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63      ** automatic
1bf43 61 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62  ally created tab
1bf44 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  le with root-pag
1bf45 65 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74  e 1 (an INTKEY t
1bf46 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  able)..    */.  
1bf47 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b    if( pOp->p4.pK
1bf48 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20  eyInfo ){.      
1bf49 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20  int pgno;.      
1bf4a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1bf4b 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20  ype==P4_KEYINFO 
1bf4c 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
1bf4d 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
1bf4e 54 61 62 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20  Table(pCx->pBt, 
1bf4f 26 70 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52  &pgno, BTREE_ZER
1bf50 4f 44 41 54 41 29 3b 20 0a 20 20 20 20 20 20 69  ODATA); .      i
1bf51 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1bf52 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1bf53 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52  rt( pgno==MASTER
1bf54 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20  _ROOT+1 );.     
1bf55 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1bf56 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e  treeCursor(pCx->
1bf57 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20  pBt, pgno, 1, . 
1bf58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bf59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1bf5a 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34  KeyInfo*)pOp->p4
1bf5b 2e 7a 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72  .z, pCx->pCursor
1bf5c 29 3b 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e  );.        pCx->
1bf5d 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e  pKeyInfo = pOp->
1bf5e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  p4.pKeyInfo;.   
1bf5f 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e       pCx->pKeyIn
1bf60 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d  fo->enc = ENC(p-
1bf61 3e 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  >db);.      }.  
1bf62 20 20 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65      pCx->isTable
1bf63 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
1bf64 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1bf65 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 70  te3BtreeCursor(p
1bf66 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f  Cx->pBt, MASTER_
1bf67 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 70 43 78 2d  ROOT, 1, 0, pCx-
1bf68 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >pCursor);.     
1bf69 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20   pCx->isTable = 
1bf6a 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
1bf6b 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 70  Cx->isIndex = !p
1bf6c 43 78 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62  Cx->isTable;.  b
1bf6d 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1bf6e 64 65 3a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50  de: OpenPseudo P
1bf6f 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1bf70 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72  * Open a new cur
1bf71 73 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20  sor that points 
1bf72 74 6f 20 61 20 66 61 6b 65 20 74 61 62 6c 65 20  to a fake table 
1bf73 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20  that contains a 
1bf74 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66  single.** row of
1bf75 20 64 61 74 61 2e 20 20 41 6e 79 20 61 74 74 65   data.  Any atte
1bf76 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 73  mpt to write a s
1bf77 65 63 6f 6e 64 20 72 6f 77 20 6f 66 20 64 61 74  econd row of dat
1bf78 61 20 63 61 75 73 65 73 20 74 68 65 0a 2a 2a 20  a causes the.** 
1bf79 66 69 72 73 74 20 72 6f 77 20 74 6f 20 62 65 20  first row to be 
1bf7a 64 65 6c 65 74 65 64 2e 20 20 41 6c 6c 20 64 61  deleted.  All da
1bf7b 74 61 20 69 73 20 64 65 6c 65 74 65 64 20 77 68  ta is deleted wh
1bf7c 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  en the cursor is
1bf7d 0a 2a 2a 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a  .** closed..**.*
1bf7e 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  * A pseudo-table
1bf7f 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73   created by this
1bf80 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 66 75   opcode is usefu
1bf81 6c 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68  l for holding th
1bf82 65 0a 2a 2a 20 4e 45 57 20 6f 72 20 4f 4c 44 20  e.** NEW or OLD 
1bf83 74 61 62 6c 65 73 20 69 6e 20 61 20 74 72 69 67  tables in a trig
1bf84 67 65 72 2e 20 20 41 6c 73 6f 20 75 73 65 64 20  ger.  Also used 
1bf85 74 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73 69  to hold the a si
1bf86 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70  ngle.** row outp
1bf87 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74  ut from the sort
1bf88 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 72  er so that the r
1bf89 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70  ow can be decomp
1bf8a 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64  osed into.** ind
1bf8b 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20  ividual columns 
1bf8c 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c  using the OP_Col
1bf8d 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a  umn opcode..**.*
1bf8e 2a 20 57 68 65 6e 20 4f 50 5f 49 6e 73 65 72 74  * When OP_Insert
1bf8f 20 69 73 20 65 78 65 63 75 74 65 64 20 74 6f 20   is executed to 
1bf90 69 6e 73 65 72 74 20 61 20 72 6f 77 20 69 6e 20  insert a row in 
1bf91 74 6f 20 74 68 65 20 70 73 65 75 64 6f 20 74 61  to the pseudo ta
1bf92 62 6c 65 2c 0a 2a 2a 20 74 68 65 20 70 73 65 75  ble,.** the pseu
1bf93 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f 72 20  do-table cursor 
1bf94 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 6d  may or may not m
1bf95 61 6b 65 20 69 74 27 73 20 6f 77 6e 20 63 6f 70  ake it's own cop
1bf96 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67  y of the.** orig
1bf97 69 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e 20 49  inal row data. I
1bf98 66 20 50 32 20 69 73 20 30 2c 20 74 68 65 6e 20  f P2 is 0, then 
1bf99 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  the pseudo-table
1bf9a 20 77 69 6c 6c 20 63 6f 70 79 20 74 68 65 0a 2a   will copy the.*
1bf9b 2a 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 20 64  * original row d
1bf9c 61 74 61 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ata. Otherwise, 
1bf9d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1bf9e 20 6f 72 69 67 69 6e 61 6c 20 6d 65 6d 6f 72 79   original memory
1bf9f 20 63 65 6c 6c 0a 2a 2a 20 69 73 20 73 74 6f 72   cell.** is stor
1bfa0 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
1bfa1 2c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  , the vdbe progr
1bfa2 61 6d 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  am must ensure t
1bfa3 68 61 74 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f  hat the .** memo
1bfa4 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69  ry cell containi
1bfa5 6e 67 20 74 68 65 20 72 6f 77 20 64 61 74 61 20  ng the row data 
1bfa6 69 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 74  is not overwritt
1bfa7 65 6e 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20  en until the.** 
1bfa8 70 73 65 75 64 6f 20 74 61 62 6c 65 20 69 73 20  pseudo table is 
1bfa9 63 6c 6f 73 65 64 20 28 6f 72 20 61 20 6e 65 77  closed (or a new
1bfaa 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64   row is inserted
1bfab 20 69 6e 74 6f 20 69 74 29 2e 0a 2a 2a 0a 2a 2a   into it)..**.**
1bfac 20 50 33 20 69 73 20 74 68 65 20 6e 75 6d 62 65   P3 is the numbe
1bfad 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
1bfae 68 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20  he records that 
1bfaf 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 62  will be stored b
1bfb0 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d  y.** the pseudo-
1bfb1 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
1bfb2 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a  P_OpenPseudo: {.
1bfb3 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1bfb4 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  1;.  VdbeCursor 
1bfb5 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28 20  *pCx;.  assert( 
1bfb6 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20  i>=0 );.  pCx = 
1bfb7 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70  allocateCursor(p
1bfb8 2c 20 69 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31  , i, pOp->p3, -1
1bfb9 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 78 3d  , 0);.  if( pCx=
1bfba 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
1bfbb 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77  ;.  pCx->nullRow
1bfbc 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 70 73 65   = 1;.  pCx->pse
1bfbd 75 64 6f 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20  udoTable = 1;.  
1bfbe 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f  pCx->ephemPseudo
1bfbf 54 61 62 6c 65 20 3d 20 28 75 38 29 70 4f 70 2d  Table = (u8)pOp-
1bfc0 3e 70 32 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61  >p2;.  pCx->isTa
1bfc1 62 6c 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e  ble = 1;.  pCx->
1bfc2 69 73 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62  isIndex = 0;.  b
1bfc3 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1bfc4 64 65 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a  de: Close P1 * *
1bfc5 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65   * *.**.** Close
1bfc6 20 61 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f   a cursor previo
1bfc7 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50  usly opened as P
1bfc8 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74  1.  If P1 is not
1bfc9 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  .** currently op
1bfca 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63  en, this instruc
1bfcb 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
1bfcc 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73  .*/.case OP_Clos
1bfcd 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70  e: {.  int i = p
1bfce 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74  Op->p1;.  assert
1bfcf 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
1bfd0 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69  Cursor );.  sqli
1bfd1 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f  te3VdbeFreeCurso
1bfd2 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d  r(p, p->apCsr[i]
1bfd3 29 3b 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d  );.  p->apCsr[i]
1bfd4 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1bfd5 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65  ../* Opcode: See
1bfd6 6b 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20  kGe P1 P2 P3 P4 
1bfd7 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f  *.**.** If curso
1bfd8 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61  r P1 refers to a
1bfd9 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54  n SQL table (B-T
1bfda 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e  ree that uses in
1bfdb 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a  teger keys), .**
1bfdc 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69   use the value i
1bfdd 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73  n register P3 as
1bfde 20 74 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75   the key.  If cu
1bfdf 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a  rsor P1 refers .
1bfe0 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64  ** to an SQL ind
1bfe1 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74  ex, then P3 is t
1bfe2 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61  he first in an a
1bfe3 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73  rray of P4 regis
1bfe4 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72  ters .** that ar
1bfe5 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70  e used as an unp
1bfe6 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e  acked index key.
1bfe7 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69   .**.** Repositi
1bfe8 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20  on cursor P1 so 
1bfe9 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20  that  it points 
1bfea 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20  to the smallest 
1bfeb 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69  entry that .** i
1bfec 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
1bfed 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b  r equal to the k
1bfee 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  ey value. If the
1bfef 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
1bff0 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68  s .** greater th
1bff1 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
1bff2 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73  he key and P2 is
1bff3 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20   not zero, then 
1bff4 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
1bff5 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
1bff6 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73  d, NotFound, Dis
1bff7 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53  tinct, SeekLt, S
1bff8 65 65 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f  eekGt, SeekLe.*/
1bff9 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b  ./* Opcode: Seek
1bffa 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  Gt P1 P2 P3 P4 *
1bffb 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  .**.** If cursor
1bffc 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e   P1 refers to an
1bffd 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72   SQL table (B-Tr
1bffe 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74  ee that uses int
1bfff 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20  eger keys), .** 
1c000 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  use the value in
1c001 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
1c002 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
1c003 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
1c004 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
1c005 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
1c006 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
1c007 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1c008 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
1c009 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
1c00a 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
1c00b 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
1c00c 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1c00d 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74    it points to t
1c00e 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72  he smallest entr
1c00f 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72  y that .** is gr
1c010 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b  eater than the k
1c011 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  ey value. If the
1c012 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
1c013 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a  s greater than .
1c014 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50  ** the key and P
1c015 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74  2 is not zero, t
1c016 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a  hen jump to P2..
1c017 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1c018 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
1c019 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c   Distinct, SeekL
1c01a 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c  t, SeekGe, SeekL
1c01b 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  e.*/./* Opcode: 
1c01c 53 65 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20  SeekLt P1 P2 P3 
1c01d 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63  P4 * .**.** If c
1c01e 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
1c01f 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20  to an SQL table 
1c020 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 65  (B-Tree that use
1c021 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c  s integer keys),
1c022 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c   .** use the val
1c023 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1c024 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63  3 as a key. If c
1c025 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
1c026 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e  .** to an SQL in
1c027 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20  dex, then P3 is 
1c028 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
1c029 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69  array of P4 regi
1c02a 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61  sters .** that a
1c02b 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e  re used as an un
1c02c 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
1c02d 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74  . .**.** Reposit
1c02e 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f  ion cursor P1 so
1c02f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73   that  it points
1c030 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20   to the largest 
1c031 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69  entry that .** i
1c032 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
1c033 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68  key value. If th
1c034 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72  ere are no recor
1c035 64 73 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a  ds less than .**
1c036 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
1c037 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
1c038 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1c039 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1c03a 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44  und, NotFound, D
1c03b 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c  istinct, SeekGt,
1c03c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a   SeekGe, SeekLe.
1c03d 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  */./* Opcode: Se
1c03e 65 6b 4c 65 20 50 31 20 50 32 20 50 33 20 50 34  ekLe P1 P2 P3 P4
1c03f 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73   *.**.** If curs
1c040 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20  or P1 refers to 
1c041 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d  an SQL table (B-
1c042 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69  Tree that uses i
1c043 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a  nteger keys), .*
1c044 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20  * use the value 
1c045 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61  in register P3 a
1c046 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73  s a key. If curs
1c047 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a  or P1 refers .**
1c048 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78   to an SQL index
1c049 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65  , then P3 is the
1c04a 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72   first in an arr
1c04b 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65  ay of P4 registe
1c04c 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  rs .** that are 
1c04d 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63  used as an unpac
1c04e 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a  ked index key. .
1c04f 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e  **.** Reposition
1c050 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
1c051 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
1c052 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72  the largest entr
1c053 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65  y that .** is le
1c054 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
1c055 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75   to the key valu
1c056 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20  e. If there are 
1c057 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c  no records .** l
1c058 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
1c059 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64  l to the key and
1c05a 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c   P2 is not zero,
1c05b 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
1c05c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1c05d 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e  : Found, NotFoun
1c05e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65  d, Distinct, See
1c05f 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65  kGt, SeekGe, See
1c060 6b 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  kLt.*/.case OP_S
1c061 65 65 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f  eekLt:         /
1c062 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63  * jump, in3 */.c
1c063 61 73 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20  ase OP_SeekLe:  
1c064 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1c065 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  in3 */.case OP_S
1c066 65 65 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f  eekGe:         /
1c067 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63  * jump, in3 */.c
1c068 61 73 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b  ase OP_SeekGt: {
1c069 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1c06a 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  in3 */.  int i =
1c06b 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65   pOp->p1;.  Vdbe
1c06c 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61  Cursor *pC;..  a
1c06d 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
1c06e 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1c06f 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1c070 21 3d 30 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d  !=0 );.  pC = p-
1c071 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73  >apCsr[i];.  ass
1c072 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20  ert( pC!=0 );.  
1c073 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21  if( pC->pCursor!
1c074 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  =0 ){.    int re
1c075 73 2c 20 6f 63 3b 0a 20 20 20 20 6f 63 20 3d 20  s, oc;.    oc = 
1c076 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20  pOp->opcode;.   
1c077 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30   pC->nullRow = 0
1c078 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73  ;.    if( pC->is
1c079 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69  Table ){.      i
1c07a 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a  64 iKey;      /*
1c07b 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61 72   The rowid we ar
1c07c 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a  e to seek to */.
1c07d 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e  .      /* The in
1c07e 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20  put value in P3 
1c07f 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20  might be of any 
1c080 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72  type: integer, r
1c081 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20  eal, string,.   
1c082 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e     ** blob, or N
1c083 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65  ULL.  But it nee
1c084 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65  ds to be an inte
1c085 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 61  ger before we ca
1c086 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  n do.      ** th
1c087 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72  e seek, so cover
1c088 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 61  t it. */.      a
1c089 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
1c08a 69 74 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 20  ity(pIn3);.     
1c08b 20 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56   iKey = sqlite3V
1c08c 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33  dbeIntValue(pIn3
1c08d 29 3b 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77  );.      pC->row
1c08e 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 0a  idIsValid = 0;..
1c08f 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
1c090 50 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 6e  P3 value could n
1c091 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20  ot be converted 
1c092 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  into an integer 
1c093 77 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a 2a  without.      **
1c094 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61   loss of informa
1c095 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 69  tion, then speci
1c096 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73  al processing is
1c097 20 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f 0a   required... */.
1c098 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d        if( (pIn3-
1c099 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1c09a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1c09b 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73  if( (pIn3->flags
1c09c 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20   & MEM_Real)==0 
1c09d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
1c09e 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20  If the P3 value 
1c09f 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72  cannot be conver
1c0a0 74 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 6e  ted into any kin
1c0a1 64 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a 20  d of a number,. 
1c0a2 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e           ** then
1c0a3 20 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f 74   the seek is not
1c0a4 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a 75   possible, so ju
1c0a5 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20  mp to P2 */.    
1c0a6 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1c0a7 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20  p2 - 1;.        
1c0a8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1c0a9 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66   }.        /* If
1c0aa 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70   we reach this p
1c0ab 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 50  oint, then the P
1c0ac 33 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20  3 value must be 
1c0ad 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 20  a floating.     
1c0ae 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62     ** point numb
1c0af 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  er. */.        a
1c0b0 73 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c  ssert( (pIn3->fl
1c0b1 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21  ags & MEM_Real)!
1c0b2 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  =0 );..        i
1c0b3 66 28 20 69 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53  f( iKey==SMALLES
1c0b4 54 5f 49 4e 54 36 34 20 26 26 20 28 70 49 6e 33  T_INT64 && (pIn3
1c0b5 2d 3e 72 3c 28 64 6f 75 62 6c 65 29 69 4b 65 79  ->r<(double)iKey
1c0b6 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29   || pIn3->r>0) )
1c0b7 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
1c0b8 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 74  he P3 value is t
1c0b9 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69  o large in magni
1c0ba 74 75 64 65 20 74 6f 20 62 65 20 65 78 70 72 65  tude to be expre
1c0bb 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20  ssed as an.     
1c0bc 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e       ** integer.
1c0bd 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65   */.          re
1c0be 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  s = 1;.         
1c0bf 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 29   if( pIn3->r<0 )
1c0c0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
1c0c1 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20  ( oc==OP_SeekGt 
1c0c2 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65  || oc==OP_SeekGe
1c0c3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1c0c4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1c0c5 72 65 65 46 69 72 73 74 28 70 43 2d 3e 70 43 75  reeFirst(pC->pCu
1c0c6 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20  rsor, &res);.   
1c0c7 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72             if( r
1c0c8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
1c0c9 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1c0ca 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
1c0cb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1c0cc 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1c0cd 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65     if( oc==OP_Se
1c0ce 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53  ekLt || oc==OP_S
1c0cf 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20  eekLe ){.       
1c0d0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1c0d1 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d  te3BtreeLast(pC-
1c0d2 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b  >pCursor, &res);
1c0d3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
1c0d4 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1c0d5 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1c0d6 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1c0d7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1c0d8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1c0d9 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20  if( res ){.     
1c0da 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d         pc = pOp-
1c0db 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  >p2 - 1;.       
1c0dc 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
1c0dd 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65  reak;.        }e
1c0de 6c 73 65 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53  lse if( oc==OP_S
1c0df 65 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f  eekLt || oc==OP_
1c0e0 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20  SeekGe ){.      
1c0e1 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 63      /* Use the c
1c0e2 65 69 6c 69 6e 67 28 29 20 66 75 6e 63 74 69 6f  eiling() functio
1c0e3 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61  n to convert rea
1c0e4 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20  l->int */.      
1c0e5 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20      if( pIn3->r 
1c0e6 3e 20 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29  > (double)iKey )
1c0e7 20 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20   iKey++;.       
1c0e8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1c0e9 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f    /* Use the flo
1c0ea 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  or() function to
1c0eb 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69   convert real->i
1c0ec 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  nt */.          
1c0ed 61 73 73 65 72 74 28 20 6f 63 3d 3d 4f 50 5f 53  assert( oc==OP_S
1c0ee 65 65 6b 4c 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f  eekLe || oc==OP_
1c0ef 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20  SeekGt );.      
1c0f0 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20      if( pIn3->r 
1c0f1 3c 20 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29  < (double)iKey )
1c0f2 20 69 4b 65 79 2d 2d 3b 0a 20 20 20 20 20 20 20   iKey--;.       
1c0f3 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20   }.      } .    
1c0f4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1c0f5 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1c0f6 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30  d(pC->pCursor, 0
1c0f7 2c 20 28 75 36 34 29 69 4b 65 79 2c 20 30 2c 20  , (u64)iKey, 0, 
1c0f8 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28  &res);.      if(
1c0f9 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1c0fa 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61  {.        goto a
1c0fb 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1c0fc 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  r;.      }.     
1c0fd 20 69 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20   if( res==0 ){. 
1c0fe 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64         pC->rowid
1c0ff 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20  IsValid = 1;.   
1c100 20 20 20 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77       pC->lastRow
1c101 69 64 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 20  id = iKey;.     
1c102 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
1c103 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f      UnpackedReco
1c104 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e 74 20  rd r;.      int 
1c105 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34  nField = pOp->p4
1c106 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  .i;.      assert
1c107 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1c108 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20  4_INT32 );.     
1c109 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c 64 3e   assert( nField>
1c10a 30 20 29 3b 0a 20 20 20 20 20 20 72 2e 70 4b 65  0 );.      r.pKe
1c10b 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79  yInfo = pC->pKey
1c10c 49 6e 66 6f 3b 0a 20 20 20 20 20 20 72 2e 6e 46  Info;.      r.nF
1c10d 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 46 69 65  ield = (u16)nFie
1c10e 6c 64 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 63  ld;.      if( oc
1c10f 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 6f  ==OP_SeekGt || o
1c110 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a  c==OP_SeekLe ){.
1c111 20 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20          r.flags 
1c112 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b  = UNPACKED_INCRK
1c113 45 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EY;.      }else{
1c114 0a 20 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73  .        r.flags
1c115 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
1c116 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d      r.aMem = &p-
1c117 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1c118 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1c119 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1c11a 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f  acked(pC->pCurso
1c11b 72 2c 20 26 72 2c 20 30 2c 20 30 2c 20 26 72 65  r, &r, 0, 0, &re
1c11c 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
1c11d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1c11e 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
1c11f 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1c120 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
1c121 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1c122 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43   0;.    }.    pC
1c123 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1c124 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61   = 0;.    pC->ca
1c125 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1c126 45 5f 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 20  E_STALE;.#ifdef 
1c127 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20  SQLITE_TEST.    
1c128 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63  sqlite3_search_c
1c129 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
1c12a 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65     if( oc==OP_Se
1c12b 65 6b 47 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53  ekGe || oc==OP_S
1c12c 65 65 6b 47 74 20 29 7b 0a 20 20 20 20 20 20 69  eekGt ){.      i
1c12d 66 28 20 72 65 73 3c 30 20 7c 7c 20 28 72 65 73  f( res<0 || (res
1c12e 3d 3d 30 20 26 26 20 6f 63 3d 3d 4f 50 5f 53 65  ==0 && oc==OP_Se
1c12f 65 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20  ekGt) ){.       
1c130 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1c131 65 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73  eeNext(pC->pCurs
1c132 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  or, &res);.     
1c133 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1c134 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72  E_OK ) goto abor
1c135 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1c136 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69          pC->rowi
1c137 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1c138 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1c139 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20     res = 0;.    
1c13a 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
1c13b 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d       assert( oc=
1c13c 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f 63  =OP_SeekLt || oc
1c13d 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20  ==OP_SeekLe );. 
1c13e 20 20 20 20 20 69 66 28 20 72 65 73 3e 30 20 7c       if( res>0 |
1c13f 7c 20 28 72 65 73 3d 3d 30 20 26 26 20 6f 63 3d  | (res==0 && oc=
1c140 3d 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20  =OP_SeekLt) ){. 
1c141 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1c142 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
1c143 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72  (pC->pCursor, &r
1c144 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  es);.        if(
1c145 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1c146 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1c147 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1c148 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c    pC->rowidIsVal
1c149 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  id = 0;.      }e
1c14a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
1c14b 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67  res might be neg
1c14c 61 74 69 76 65 20 62 65 63 61 75 73 65 20 74 68  ative because th
1c14d 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79  e table is empty
1c14e 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20  .  Check to.    
1c14f 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68      ** see if th
1c150 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a  is is the case..
1c151 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1c152 20 20 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33     res = sqlite3
1c153 42 74 72 65 65 45 6f 66 28 70 43 2d 3e 70 43 75  BtreeEof(pC->pCu
1c154 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rsor);.      }. 
1c155 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1c156 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20   pOp->p2>0 );.  
1c157 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
1c158 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1c159 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  - 1;.    }.  }el
1c15a 73 65 20 69 66 28 20 21 70 43 2d 3e 70 73 65 75  se if( !pC->pseu
1c15b 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f  doTable ){.    /
1c15c 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77  * This happens w
1c15d 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74  hen attempting t
1c15e 6f 20 6f 70 65 6e 20 74 68 65 20 73 71 6c 69 74  o open the sqlit
1c15f 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a  e3_master table.
1c160 20 20 20 20 2a 2a 20 66 6f 72 20 72 65 61 64 20      ** for read 
1c161 61 63 63 65 73 73 20 72 65 74 75 72 6e 73 20 53  access returns S
1c162 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20  QLITE_EMPTY. In 
1c163 74 68 69 73 20 63 61 73 65 20 61 6c 77 61 79 73  this case always
1c164 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20 74 68 65  .    ** take the
1c165 20 6a 75 6d 70 20 28 73 69 6e 63 65 20 74 68 65   jump (since the
1c166 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
1c167 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 29 2e  s in the table).
1c168 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d  .    */.    pc =
1c169 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1c16a 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1c16b 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 20 50 31   Opcode: Seek P1
1c16c 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1c16d 50 31 20 69 73 20 61 6e 20 6f 70 65 6e 20 74 61  P1 is an open ta
1c16e 62 6c 65 20 63 75 72 73 6f 72 20 61 6e 64 20 50  ble cursor and P
1c16f 32 20 69 73 20 61 20 72 6f 77 69 64 20 69 6e 74  2 is a rowid int
1c170 65 67 65 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a  eger.  Arrange.*
1c171 2a 20 66 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65  * for P1 to move
1c172 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e   so that it poin
1c173 74 73 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20  ts to the rowid 
1c174 67 69 76 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a  given by P2..**.
1c175 2a 2a 20 54 68 69 73 20 69 73 20 61 63 74 75 61  ** This is actua
1c176 6c 6c 79 20 61 20 64 65 66 65 72 72 65 64 20 73  lly a deferred s
1c177 65 65 6b 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63  eek.  Nothing ac
1c178 74 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 75  tually happens u
1c179 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73  ntil.** the curs
1c17a 6f 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  or is used to re
1c17b 61 64 20 61 20 72 65 63 6f 72 64 2e 20 20 54 68  ad a record.  Th
1c17c 61 74 20 77 61 79 2c 20 69 66 20 6e 6f 20 72 65  at way, if no re
1c17d 61 64 73 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f  ads.** occur, no
1c17e 20 75 6e 6e 65 63 65 73 73 61 72 79 20 49 2f 4f   unnecessary I/O
1c17f 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73   happens..*/.cas
1c180 65 20 4f 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20  e OP_Seek: {    
1c181 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20  /* in2 */.  int 
1c182 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56  i = pOp->p1;.  V
1c183 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a  dbeCursor *pC;..
1c184 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1c185 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1c186 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73  ;.  pC = p->apCs
1c187 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  r[i];.  assert( 
1c188 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  pC!=0 );.  if( p
1c189 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b  C->pCursor!=0 ){
1c18a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1c18b 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
1c18c 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b  pC->nullRow = 0;
1c18d 0a 20 20 20 20 70 43 2d 3e 6d 6f 76 65 74 6f 54  .    pC->movetoT
1c18e 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56  arget = sqlite3V
1c18f 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32  dbeIntValue(pIn2
1c190 29 3b 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64  );.    pC->rowid
1c191 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1c192 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
1c193 65 74 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62  eto = 1;.  }.  b
1c194 72 65 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f  reak;.}.  ../* O
1c195 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20  pcode: Found P1 
1c196 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1c197 52 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64  Register P3 hold
1c198 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75  s a blob constru
1c199 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f  cted by MakeReco
1c19a 72 64 2e 20 20 50 31 20 69 73 20 61 6e 20 69 6e  rd.  P1 is an in
1c19b 64 65 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 6e  dex..** If an en
1c19c 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 73  try that matches
1c19d 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1c19e 67 69 73 74 65 72 20 70 33 20 65 78 69 73 74 73  gister p3 exists
1c19f 20 69 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a   in P1 then.** j
1c1a0 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74  ump to P2.  If t
1c1a1 68 65 20 50 33 20 76 61 6c 75 65 20 64 6f 65 73  he P3 value does
1c1a2 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 65   not match any e
1c1a3 6e 74 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 68  ntry in P1.** th
1c1a4 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54  en fall thru.  T
1c1a5 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 20  he P1 cursor is 
1c1a6 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
1c1a7 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e   the matching en
1c1a8 74 72 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 69  try.** if it exi
1c1a9 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  sts..**.** This 
1c1aa 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75  instruction is u
1c1ab 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
1c1ac 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
1c1ad 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c 65   where the.** le
1c1ae 66 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20  ft-hand side is 
1c1af 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
1c1b0 6e 74 2e 20 20 50 31 20 6d 61 79 20 62 65 20 61  nt.  P1 may be a
1c1b1 20 74 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 20   true index, or 
1c1b2 69 74 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 74  it.** may be a t
1c1b3 65 6d 70 6f 72 61 72 79 20 69 6e 64 65 78 20 74  emporary index t
1c1b4 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 72 65  hat holds the re
1c1b5 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c  sults of the SEL
1c1b6 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ECT.** statement
1c1b7 2e 20 20 20 54 68 69 73 20 69 6e 73 74 72 75 63  .   This instruc
1c1b8 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65  tion is also use
1c1b9 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
1c1ba 68 65 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 6b  he.** DISTINCT k
1c1bb 65 79 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 54  eyword in SELECT
1c1bc 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
1c1bd 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
1c1be 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 69 6e  ion checks if in
1c1bf 64 65 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 20  dex P1 contains 
1c1c0 61 20 72 65 63 6f 72 64 20 66 6f 72 20 77 68 69  a record for whi
1c1c1 63 68 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  ch .** the first
1c1c2 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61   N serialized va
1c1c3 6c 75 65 73 20 65 78 61 63 74 6c 79 20 6d 61 74  lues exactly mat
1c1c4 63 68 20 74 68 65 20 4e 20 73 65 72 69 61 6c 69  ch the N seriali
1c1c5 7a 65 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e  zed values.** in
1c1c6 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72   the record in r
1c1c7 65 67 69 73 74 65 72 20 50 33 2c 20 77 68 65 72  egister P3, wher
1c1c8 65 20 4e 20 69 73 20 74 68 65 20 74 6f 74 61 6c  e N is the total
1c1c9 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65   number of value
1c1ca 73 20 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 72  s in.** the P3 r
1c1cb 65 63 6f 72 64 20 28 74 68 65 20 50 33 20 72 65  ecord (the P3 re
1c1cc 63 6f 72 64 20 69 73 20 61 20 70 72 65 66 69 78  cord is a prefix
1c1cd 20 6f 66 20 74 68 65 20 50 31 20 72 65 63 6f 72   of the P1 recor
1c1ce 64 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  d). .**.** See a
1c1cf 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 49  lso: NotFound, I
1c1d0 73 55 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 73  sUnique, NotExis
1c1d1 74 73 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ts.*/./* Opcode:
1c1d2 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20   NotFound P1 P2 
1c1d3 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67  P3 * *.**.** Reg
1c1d4 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61  ister P3 holds a
1c1d5 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65   blob constructe
1c1d6 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e  d by MakeRecord.
1c1d7 20 20 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e    P1 is.** an in
1c1d8 64 65 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72  dex.  If no entr
1c1d9 79 20 65 78 69 73 74 73 20 69 6e 20 50 31 20 74  y exists in P1 t
1c1da 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20  hat matches the 
1c1db 62 6c 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a  blob then jump.*
1c1dc 2a 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20  * to P2.  If an 
1c1dd 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69 73 74  entry does exist
1c1de 69 6e 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  ing, fall throug
1c1df 68 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 69  h.  The cursor i
1c1e0 73 20 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69  s left.** pointi
1c1e1 6e 67 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20  ng to the entry 
1c1e2 74 68 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a 2a  that matches..**
1c1e3 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1c1e4 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20  und, NotExists, 
1c1e5 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65  IsUnique.*/.case
1c1e6 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20   OP_NotFound:   
1c1e7 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
1c1e8 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e   */.case OP_Foun
1c1e9 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  d: {        /* j
1c1ea 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e  ump, in3 */.  in
1c1eb 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
1c1ec 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73   int alreadyExis
1c1ed 74 73 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75  ts = 0;.  VdbeCu
1c1ee 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
1c1ef 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1c1f0 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
1c1f1 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
1c1f2 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  ]!=0 );.  if( (p
1c1f3 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
1c1f4 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a  ->pCursor!=0 ){.
1c1f5 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1c1f6 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1c1f7 2a 70 49 64 78 4b 65 79 3b 0a 0a 20 20 20 20 61  *pIdxKey;..    a
1c1f8 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62  ssert( pC->isTab
1c1f9 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  le==0 );.    ass
1c1fa 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73  ert( pIn3->flags
1c1fb 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20   & MEM_Blob );. 
1c1fc 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c     pIdxKey = sql
1c1fd 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e  ite3VdbeRecordUn
1c1fe 70 61 63 6b 28 70 43 2d 3e 70 4b 65 79 49 6e 66  pack(pC->pKeyInf
1c1ff 6f 2c 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33  o, pIn3->n, pIn3
1c200 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->z,.           
1c201 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c202 20 20 20 20 20 20 20 20 20 20 20 61 54 65 6d 70             aTemp
1c203 52 65 63 2c 20 73 69 7a 65 6f 66 28 61 54 65 6d  Rec, sizeof(aTem
1c204 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20  pRec));.    if( 
1c205 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20  pIdxKey==0 ){.  
1c206 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1c207 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1c208 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46  Op->opcode==OP_F
1c209 6f 75 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 49  ound ){.      pI
1c20a 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20  dxKey->flags |= 
1c20b 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f  UNPACKED_PREFIX_
1c20c 4d 41 54 43 48 3b 0a 20 20 20 20 7d 0a 20 20 20  MATCH;.    }.   
1c20d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1c20e 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
1c20f 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 70 49  (pC->pCursor, pI
1c210 64 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 72 65  dxKey, 0, 0, &re
1c211 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  s);.    sqlite3V
1c212 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65  dbeDeleteUnpacke
1c213 64 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29  dRecord(pIdxKey)
1c214 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1c215 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1c216 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1c217 20 20 61 6c 72 65 61 64 79 45 78 69 73 74 73 20    alreadyExists 
1c218 3d 20 28 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20  = (res==0);.    
1c219 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1c21a 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e  to = 0;.    pC->
1c21b 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1c21c 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20  CHE_STALE;.  }. 
1c21d 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1c21e 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20  ==OP_Found ){.  
1c21f 20 20 69 66 28 20 61 6c 72 65 61 64 79 45 78 69    if( alreadyExi
1c220 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e  sts ) pc = pOp->
1c221 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  p2 - 1;.  }else{
1c222 0a 20 20 20 20 69 66 28 20 21 61 6c 72 65 61 64  .    if( !alread
1c223 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20 70  yExists ) pc = p
1c224 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
1c225 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c226 70 63 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20  pcode: IsUnique 
1c227 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
1c228 0a 2a 2a 20 54 68 65 20 50 33 20 72 65 67 69 73  .** The P3 regis
1c229 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  ter contains an 
1c22a 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e  integer record n
1c22b 75 6d 62 65 72 2e 20 20 43 61 6c 6c 20 74 68 69  umber.  Call thi
1c22c 73 0a 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62  s.** record numb
1c22d 65 72 20 52 2e 20 20 54 68 65 20 50 34 20 72 65  er R.  The P4 re
1c22e 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20  gister contains 
1c22f 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 63 72 65  an index key cre
1c230 61 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4d 61  ated.** using Ma
1c231 6b 65 52 65 63 6f 72 64 2e 20 20 43 61 6c 6c 20  keRecord.  Call 
1c232 69 74 20 4b 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69  it K..**.** P1 i
1c233 73 20 61 6e 20 69 6e 64 65 78 2e 20 20 53 6f 20  s an index.  So 
1c234 69 74 20 68 61 73 20 6e 6f 20 64 61 74 61 20 61  it has no data a
1c235 6e 64 20 69 74 73 20 6b 65 79 20 63 6f 6e 73 69  nd its key consi
1c236 73 74 73 20 6f 66 20 61 0a 2a 2a 20 72 65 63 6f  sts of a.** reco
1c237 72 64 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  rd generated by 
1c238 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 77 68  OP_MakeRecord wh
1c239 65 72 65 20 74 68 65 20 6c 61 73 74 20 66 69 65  ere the last fie
1c23a 6c 64 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 6f  ld is the .** ro
1c23b 77 69 64 20 6f 66 20 74 68 65 20 65 6e 74 72 79  wid of the entry
1c23c 20 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 20   that the index 
1c23d 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 20 0a 2a  refers to..** .*
1c23e 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
1c23f 6f 6e 20 61 73 6b 73 20 69 66 20 74 68 65 72 65  on asks if there
1c240 20 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20   is an entry in 
1c241 50 31 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20  P1 where the.** 
1c242 66 69 65 6c 64 73 20 6d 61 74 63 68 65 73 20 4b  fields matches K
1c243 20 62 75 74 20 74 68 65 20 72 6f 77 69 64 20 69   but the rowid i
1c244 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
1c245 20 52 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20   R..** If there 
1c246 69 73 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79  is no such entry
1c247 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  , then there is 
1c248 61 6e 20 69 6d 6d 65 64 69 61 74 65 0a 2a 2a 20  an immediate.** 
1c249 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20  jump to P2.  If 
1c24a 61 6e 79 20 65 6e 74 72 79 20 64 6f 65 73 20 65  any entry does e
1c24b 78 69 73 74 20 77 68 65 72 65 20 74 68 65 20 69  xist where the i
1c24c 6e 64 65 78 20 73 74 72 69 6e 67 0a 2a 2a 20 6d  ndex string.** m
1c24d 61 74 63 68 65 73 20 4b 20 62 75 74 20 74 68 65  atches K but the
1c24e 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69   record number i
1c24f 73 20 6e 6f 74 20 52 2c 20 74 68 65 6e 20 74 68  s not R, then th
1c250 65 20 72 65 63 6f 72 64 0a 2a 2a 20 6e 75 6d 62  e record.** numb
1c251 65 72 20 66 6f 72 20 74 68 61 74 20 65 6e 74 72  er for that entr
1c252 79 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  y is written int
1c253 6f 20 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c  o P3 and control
1c254 0a 2a 2a 20 66 61 6c 6c 73 20 74 68 72 6f 75 67  .** falls throug
1c255 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e  h to the next in
1c256 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  struction..**.**
1c257 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f   See also: NotFo
1c258 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20  und, NotExists, 
1c259 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50  Found.*/.case OP
1c25a 5f 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20 20  _IsUnique: {    
1c25b 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
1c25c 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f   */.  int i = pO
1c25d 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72  p->p1;.  VdbeCur
1c25e 73 6f 72 20 2a 70 43 78 3b 0a 20 20 42 74 43 75  sor *pCx;.  BtCu
1c25f 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 4d  rsor *pCrsr;.  M
1c260 65 6d 20 2a 70 4b 3b 0a 20 20 69 36 34 20 52 3b  em *pK;.  i64 R;
1c261 0a 0a 20 20 2f 2a 20 50 6f 70 20 74 68 65 20 76  ..  /* Pop the v
1c262 61 6c 75 65 20 52 20 6f 66 66 20 74 68 65 20 74  alue R off the t
1c263 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b 0a  op of the stack.
1c264 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
1c265 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49  Op->p4type==P4_I
1c266 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74  NT32 );.  assert
1c267 28 20 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26  ( pOp->p4.i>0 &&
1c268 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e   pOp->p4.i<=p->n
1c269 4d 65 6d 20 29 3b 0a 20 20 70 4b 20 3d 20 26 70  Mem );.  pK = &p
1c26a 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69  ->aMem[pOp->p4.i
1c26b 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ];.  sqlite3Vdbe
1c26c 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49  MemIntegerify(pI
1c26d 6e 33 29 3b 0a 20 20 52 20 3d 20 70 49 6e 33 2d  n3);.  R = pIn3-
1c26e 3e 75 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 20  >u.i;.  assert( 
1c26f 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1c270 72 73 6f 72 20 29 3b 0a 20 20 70 43 78 20 3d 20  rsor );.  pCx = 
1c271 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61  p->apCsr[i];.  a
1c272 73 73 65 72 74 28 20 70 43 78 21 3d 30 20 29 3b  ssert( pCx!=0 );
1c273 0a 20 20 70 43 72 73 72 20 3d 20 70 43 78 2d 3e  .  pCrsr = pCx->
1c274 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70  pCursor;.  if( p
1c275 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 69  Crsr!=0 ){.    i
1c276 6e 74 20 72 65 73 3b 0a 20 20 20 20 69 36 34 20  nt res;.    i64 
1c277 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
1c278 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65         /* The re
1c279 63 6f 72 64 20 6e 75 6d 62 65 72 20 74 68 61 74  cord number that
1c27a 20 6d 61 74 63 68 65 73 20 4b 20 2a 2f 0a 20 20   matches K */.  
1c27b 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1c27c 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a 20   *pIdxKey;   /* 
1c27d 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f 6e  Unpacked version
1c27e 20 6f 66 20 50 34 20 2a 2f 0a 0a 20 20 20 20 2f   of P4 */..    /
1c27f 2a 20 4d 61 6b 65 20 73 75 72 65 20 4b 20 69 73  * Make sure K is
1c280 20 61 20 73 74 72 69 6e 67 20 61 6e 64 20 6d 61   a string and ma
1c281 6b 65 20 7a 4b 65 79 20 70 6f 69 6e 74 20 74 6f  ke zKey point to
1c282 20 4b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73   K.    */.    as
1c283 73 65 72 74 28 20 70 4b 2d 3e 66 6c 61 67 73 20  sert( pK->flags 
1c284 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20  & MEM_Blob );.  
1c285 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69    pIdxKey = sqli
1c286 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
1c287 61 63 6b 28 70 43 78 2d 3e 70 4b 65 79 49 6e 66  ack(pCx->pKeyInf
1c288 6f 2c 20 70 4b 2d 3e 6e 2c 20 70 4b 2d 3e 7a 2c  o, pK->n, pK->z,
1c289 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1c28a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c28b 20 20 20 20 20 20 20 61 54 65 6d 70 52 65 63 2c         aTempRec,
1c28c 20 73 69 7a 65 6f 66 28 61 54 65 6d 70 52 65 63   sizeof(aTempRec
1c28d 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78  ));.    if( pIdx
1c28e 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Key==0 ){.      
1c28f 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
1c290 20 7d 0a 20 20 20 20 70 49 64 78 4b 65 79 2d 3e   }.    pIdxKey->
1c291 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45  flags |= UNPACKE
1c292 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a  D_IGNORE_ROWID;.
1c293 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66  .    /* Search f
1c294 6f 72 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 50  or an entry in P
1c295 31 20 77 68 65 72 65 20 61 6c 6c 20 62 75 74 20  1 where all but 
1c296 74 68 65 20 6c 61 73 74 20 72 6f 77 69 64 20 6d  the last rowid m
1c297 61 74 63 68 20 4b 0a 20 20 20 20 2a 2a 20 49 66  atch K.    ** If
1c298 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63   there is no suc
1c299 68 20 65 6e 74 72 79 2c 20 6a 75 6d 70 20 69 6d  h entry, jump im
1c29a 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e  mediately to P2.
1c29b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
1c29c 72 74 28 20 70 43 78 2d 3e 64 65 66 65 72 72 65  rt( pCx->deferre
1c29d 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
1c29e 20 20 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74    pCx->cacheStat
1c29f 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1c2a0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1c2a1 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1c2a2 61 63 6b 65 64 28 70 43 72 73 72 2c 20 70 49 64  acked(pCrsr, pId
1c2a3 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 72 65 73  xKey, 0, 0, &res
1c2a4 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
1c2a5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1c2a6 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
1c2a7 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
1c2a8 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20  d(pIdxKey);.    
1c2a9 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1c2aa 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  _to_error;.    }
1c2ab 0a 20 20 20 20 69 66 28 20 72 65 73 3c 30 20 29  .    if( res<0 )
1c2ac 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1c2ad 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 70 43  ite3BtreeNext(pC
1c2ae 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  rsr, &res);.    
1c2af 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
1c2b0 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1c2b1 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 73  2 - 1;.        s
1c2b2 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
1c2b3 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70  UnpackedRecord(p
1c2b4 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  IdxKey);.       
1c2b5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1c2b6 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
1c2b7 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79  qlite3VdbeIdxKey
1c2b8 43 6f 6d 70 61 72 65 28 70 43 78 2c 20 70 49 64  Compare(pCx, pId
1c2b9 78 4b 65 79 2c 20 26 72 65 73 29 3b 20 0a 20 20  xKey, &res); .  
1c2ba 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
1c2bb 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
1c2bc 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20  d(pIdxKey);.    
1c2bd 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1c2be 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  K ) goto abort_d
1c2bf 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1c2c0 20 69 66 28 20 72 65 73 3e 30 20 29 7b 0a 20 20   if( res>0 ){.  
1c2c1 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1c2c2 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61   - 1;.      brea
1c2c3 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
1c2c4 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
1c2c5 70 43 72 73 72 20 69 73 20 70 6f 69 6e 74 69 6e  pCrsr is pointin
1c2c6 67 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 69 6e  g to an entry in
1c2c7 20 50 31 20 77 68 65 72 65 20 61 6c 6c 20 62 75   P1 where all bu
1c2c8 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6e  t.    ** the fin
1c2c9 61 6c 20 65 6e 74 72 79 20 28 74 68 65 20 72 6f  al entry (the ro
1c2ca 77 69 64 29 20 6d 61 74 63 68 65 73 20 4b 2e 20  wid) matches K. 
1c2cb 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
1c2cc 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 6e 61   the.    ** fina
1c2cd 6c 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69  l rowid column i
1c2ce 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
1c2cf 20 52 2e 20 20 49 66 20 69 74 20 65 71 75 61 6c   R.  If it equal
1c2d0 73 20 52 20 74 68 65 6e 20 6a 75 6d 70 0a 20 20  s R then jump.  
1c2d1 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79    ** immediately
1c2d2 20 74 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20   to P2..    */. 
1c2d3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1c2d4 64 62 65 49 64 78 52 6f 77 69 64 28 70 43 72 73  dbeIdxRowid(pCrs
1c2d5 72 2c 20 26 76 29 3b 0a 20 20 20 20 69 66 28 20  r, &v);.    if( 
1c2d6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1c2d7 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
1c2d8 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1c2d9 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 76 3d      }.    if( v=
1c2da 3d 52 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  =R ){.      pc =
1c2db 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1c2dc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1c2dd 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6e  ..    /* The fin
1c2de 61 6c 20 76 61 72 69 6e 74 20 6f 66 20 74 68 65  al varint of the
1c2df 20 6b 65 79 20 69 73 20 64 69 66 66 65 72 65 6e   key is differen
1c2e0 74 20 66 72 6f 6d 20 52 2e 20 20 53 74 6f 72 65  t from R.  Store
1c2e1 20 69 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20   it back.    ** 
1c2e2 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52 33  into register R3
1c2e3 2e 20 20 28 54 68 65 20 72 65 63 6f 72 64 20 6e  .  (The record n
1c2e4 75 6d 62 65 72 20 6f 66 20 61 6e 20 65 6e 74 72  umber of an entr
1c2e5 79 20 74 68 61 74 20 76 69 6f 6c 61 74 65 73 0a  y that violates.
1c2e6 20 20 20 20 2a 2a 20 61 20 55 4e 49 51 55 45 20      ** a UNIQUE 
1c2e7 63 6f 6e 73 74 72 61 69 6e 74 2e 29 0a 20 20 20  constraint.).   
1c2e8 20 2a 2f 0a 20 20 20 20 70 49 6e 33 2d 3e 75 2e   */.    pIn3->u.
1c2e9 69 20 3d 20 76 3b 0a 20 20 20 20 61 73 73 65 72  i = v;.    asser
1c2ea 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d  t( pIn3->flags&M
1c2eb 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 7d 0a 20 20  EM_Int );.  }.  
1c2ec 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c2ed 6f 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50  ode: NotExists P
1c2ee 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1c2ef 2a 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e  * Use the conten
1c2f0 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33  t of register P3
1c2f1 20 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65   as a integer ke
1c2f2 79 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64 20  y.  If a record 
1c2f3 0a 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65  .** with that ke
1c2f4 79 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  y does not exist
1c2f5 20 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c   in table of P1,
1c2f6 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
1c2f7 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63  . .** If the rec
1c2f8 6f 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20  ord does exist, 
1c2f9 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20  then fall thru. 
1c2fa 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c   The cursor is l
1c2fb 65 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67  eft .** pointing
1c2fc 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69   to the record i
1c2fd 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a  f it exists..**.
1c2fe 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  ** The differenc
1c2ff 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f  e between this o
1c300 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74  peration and Not
1c301 46 6f 75 6e 64 20 69 73 20 74 68 61 74 20 74 68  Found is that th
1c302 69 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20  is.** operation 
1c303 61 73 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20  assumes the key 
1c304 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e  is an integer an
1c305 64 20 74 68 61 74 20 50 31 20 69 73 20 61 20 74  d that P1 is a t
1c306 61 62 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20  able whereas.** 
1c307 4e 6f 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73  NotFound assumes
1c308 20 6b 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63   key is a blob c
1c309 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20  onstructed from 
1c30a 4d 61 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a  MakeRecord and.*
1c30b 2a 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78  * P1 is an index
1c30c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1c30d 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e  : Found, NotFoun
1c30e 64 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63  d, IsUnique.*/.c
1c30f 61 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  ase OP_NotExists
1c310 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1c311 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74  mp, in3 */.  int
1c312 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
1c313 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c314 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1c315 72 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  r;.  assert( i>=
1c316 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
1c317 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1c318 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b  ->apCsr[i]!=0 );
1c319 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20  .  if( (pCrsr = 
1c31a 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  (pC = p->apCsr[i
1c31b 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20  ])->pCursor)!=0 
1c31c 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d  ){.    int res =
1c31d 20 30 3b 0a 20 20 20 20 75 36 34 20 69 4b 65 79   0;.    u64 iKey
1c31e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49  ;.    assert( pI
1c31f 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n3->flags & MEM_
1c320 49 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  Int );.    asser
1c321 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d 3e  t( p->apCsr[i]->
1c322 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 69  isTable );.    i
1c323 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70  Key = intToKey(p
1c324 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 72  In3->u.i);.    r
1c325 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1c326 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70  MovetoUnpacked(p
1c327 43 72 73 72 2c 20 30 2c 20 69 4b 65 79 2c 20 30  Crsr, 0, iKey, 0
1c328 2c 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 3e  ,&res);.    pC->
1c329 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33  lastRowid = pIn3
1c32a 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 43 2d 3e 72  ->u.i;.    pC->r
1c32b 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65  owidIsValid = re
1c32c 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70  s==0 ?1:0;.    p
1c32d 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a  C->nullRow = 0;.
1c32e 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61      pC->cacheSta
1c32f 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1c330 45 3b 0a 20 20 20 20 69 66 28 20 72 65 73 21 3d  E;.    if( res!=
1c331 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  0 ){.      pc = 
1c332 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
1c333 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72     assert( pC->r
1c334 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29  owidIsValid==0 )
1c335 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
1c336 69 66 28 20 21 70 43 2d 3e 70 73 65 75 64 6f 54  if( !pC->pseudoT
1c337 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54  able ){.    /* T
1c338 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e  his happens when
1c339 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f   an attempt to o
1c33a 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f  pen a read curso
1c33b 72 20 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a  r on the .    **
1c33c 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
1c33d 61 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c  able returns SQL
1c33e 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a  ITE_EMPTY..    *
1c33f 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  /.    assert( pC
1c340 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20  ->isTable );.   
1c341 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1c342 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  1;.    assert( p
1c343 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d  C->rowidIsValid=
1c344 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  =0 );.  }.  brea
1c345 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1c346 20 53 65 71 75 65 6e 63 65 20 50 31 20 50 32 20   Sequence P1 P2 
1c347 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64  * * *.**.** Find
1c348 20 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61   the next availa
1c349 62 6c 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d  ble sequence num
1c34a 62 65 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50  ber for cursor P
1c34b 31 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  1..** Write the 
1c34c 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20  sequence number 
1c34d 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
1c34e 2e 0a 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63  ..** The sequenc
1c34f 65 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20  e number on the 
1c350 63 75 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d  cursor is increm
1c351 65 6e 74 65 64 20 61 66 74 65 72 20 74 68 69 73  ented after this
1c352 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  .** instruction.
1c353 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65    .*/.case OP_Se
1c354 71 75 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20  quence: {       
1c355 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1c356 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  elease */.  int 
1c357 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61  i = pOp->p1;.  a
1c358 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
1c359 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1c35a 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73   assert( p->apCs
1c35b 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75  r[i]!=0 );.  pOu
1c35c 74 2d 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73  t->u.i = p->apCs
1c35d 72 5b 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b  r[i]->seqCount++
1c35e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  ;.  MemSetTypeFl
1c35f 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74  ag(pOut, MEM_Int
1c360 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a  );.  break;.}...
1c361 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f  /* Opcode: NewRo
1c362 77 69 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a  wid P1 P2 P3 * *
1c363 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77  .**.** Get a new
1c364 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20   integer record 
1c365 6e 75 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72  number (a.k.a "r
1c366 6f 77 69 64 22 29 20 75 73 65 64 20 61 73 20 74  owid") used as t
1c367 68 65 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c  he key to a tabl
1c368 65 2e 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64  e..** The record
1c369 20 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70   number is not p
1c36a 72 65 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61  reviously used a
1c36b 73 20 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64  s a key in the d
1c36c 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65  atabase.** table
1c36d 20 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20   that cursor P1 
1c36e 70 6f 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20  points to.  The 
1c36f 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  new record numbe
1c370 72 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  r is written.** 
1c371 77 72 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73  written to regis
1c372 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66  ter P2..**.** If
1c373 20 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73   P3>0 then P3 is
1c374 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74   a register that
1c375 20 68 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65   holds the large
1c376 73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a  st previously.**
1c377 20 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72   generated recor
1c378 64 20 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65  d number.  No ne
1c379 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73  w record numbers
1c37a 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20   are allowed to 
1c37b 62 65 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  be less.** than 
1c37c 74 68 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65  this value.  Whe
1c37d 6e 20 74 68 69 73 20 76 61 6c 75 65 20 72 65 61  n this value rea
1c37e 63 68 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d  ches its maximum
1c37f 2c 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a  , a SQLITE_FULL.
1c380 2a 2a 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65  ** error is gene
1c381 72 61 74 65 64 2e 20 20 54 68 65 20 50 33 20 72  rated.  The P3 r
1c382 65 67 69 73 74 65 72 20 69 73 20 75 70 64 61 74  egister is updat
1c383 65 64 20 77 69 74 68 20 74 68 65 20 67 65 6e 65  ed with the gene
1c384 72 61 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20  rated.** record 
1c385 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33  number.  This P3
1c386 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73   mechanism is us
1c387 65 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65  ed to help imple
1c388 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f  ment the.** AUTO
1c389 49 4e 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72  INCREMENT featur
1c38a 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65  e..*/.case OP_Ne
1c38b 77 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20  wRowid: {       
1c38c 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1c38d 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  elease */.  int 
1c38e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69  i = pOp->p1;.  i
1c38f 36 34 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65  64 v = 0;.  Vdbe
1c390 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73  Cursor *pC;.  as
1c391 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1c392 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1c393 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
1c394 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  [i]!=0 );.  if( 
1c395 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  (pC = p->apCsr[i
1c396 5d 29 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29  ])->pCursor==0 )
1c397 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72  {.    /* The zer
1c398 6f 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  o initialization
1c399 20 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68   above is all th
1c39a 61 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a  at is needed */.
1c39b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1c39c 54 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f  The next rowid o
1c39d 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  r record number 
1c39e 28 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73  (different terms
1c39f 20 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20   for the same.  
1c3a0 20 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f    ** thing) is o
1c3a1 62 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f  btained in a two
1c3a2 2d 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e  -step algorithm.
1c3a3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
1c3a4 69 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20  irst we attempt 
1c3a5 74 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67  to find the larg
1c3a6 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77  est existing row
1c3a7 69 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20  id and add one. 
1c3a8 20 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20     ** to that.  
1c3a9 42 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65  But if the large
1c3aa 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69  st existing rowi
1c3ab 64 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65  d is already the
1c3ac 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20   maximum.    ** 
1c3ad 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72  positive integer
1c3ae 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c  , we have to fal
1c3af 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
1c3b0 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70   second.    ** p
1c3b1 72 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67  robabilistic alg
1c3b2 6f 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20  orithm.    **.  
1c3b3 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20    ** The second 
1c3b4 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20  algorithm is to 
1c3b5 73 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61  select a rowid a
1c3b6 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65  t random and see
1c3b7 20 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c   if.    ** it al
1c3b8 72 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20  ready exists in 
1c3b9 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69  the table.  If i
1c3ba 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  t does not exist
1c3bb 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a  , we have.    **
1c3bc 20 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20   succeeded.  If 
1c3bd 74 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64  the random rowid
1c3be 20 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20   does exist, we 
1c3bf 73 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65  select a new one
1c3c0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20  .    ** and try 
1c3c1 61 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30  again, up to 100
1c3c2 30 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a  0 times..    **.
1c3c3 20 20 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62      ** For a tab
1c3c4 6c 65 20 77 69 74 68 20 6c 65 73 73 20 74 68 61  le with less tha
1c3c5 6e 20 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72  n 2 billion entr
1c3c6 69 65 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69  ies, the probabi
1c3c7 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e  lity.    ** of n
1c3c8 6f 74 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75  ot finding a unu
1c3c9 73 65 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f  sed rowid is abo
1c3ca 75 74 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68  ut 1.0e-300.  Th
1c3cb 69 73 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20  is is a .    ** 
1c3cc 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69  non-zero probabi
1c3cd 6c 69 74 79 2c 20 62 75 74 20 69 74 20 69 73 20  lity, but it is 
1c3ce 73 74 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c  still vanishingl
1c3cf 79 20 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75  y small and shou
1c3d0 6c 64 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20  ld.    ** never 
1c3d1 63 61 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e  cause a problem.
1c3d2 20 20 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20    You are much, 
1c3d3 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79  much more likely
1c3d4 20 74 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a   to have a.    *
1c3d5 2a 20 68 61 72 64 77 61 72 65 20 66 61 69 6c 75  * hardware failu
1c3d6 72 65 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73  re than for this
1c3d7 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61   algorithm to fa
1c3d8 69 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  il..    **.    *
1c3d9 2a 20 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69  * The analysis i
1c3da 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70  n the previous p
1c3db 61 72 61 67 72 61 70 68 20 61 73 73 75 6d 65 73  aragraph assumes
1c3dc 20 74 68 61 74 20 79 6f 75 20 68 61 76 65 20 61   that you have a
1c3dd 20 67 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75   good.    ** sou
1c3de 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75  rce of random nu
1c3df 6d 62 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62  mbers.  Is a lib
1c3e0 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69  rary function li
1c3e1 6b 65 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20  ke lrand48().   
1c3e2 20 2a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f   ** good enough?
1c3e3 20 20 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e    Maybe. Maybe n
1c3e4 6f 74 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f  ot. It's hard to
1c3e5 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68   know whether th
1c3e6 65 72 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74  ere.    ** might
1c3e7 20 62 65 20 73 75 62 74 6c 65 20 62 75 67 73 20   be subtle bugs 
1c3e8 69 73 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e  is some implemen
1c3e9 74 61 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64  tations of lrand
1c3ea 34 38 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a  48() that.    **
1c3eb 20 63 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f   could cause pro
1c3ec 62 6c 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20  blems. To avoid 
1c3ed 75 6e 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c  uncertainty, SQL
1c3ee 69 74 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e  ite uses its own
1c3ef 20 0a 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20   .    ** random 
1c3f0 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
1c3f1 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43   based on the RC
1c3f2 34 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20  4 algorithm..   
1c3f3 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72   **.    ** To pr
1c3f4 6f 6d 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f  omote locality o
1c3f5 66 20 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20  f reference for 
1c3f6 72 65 70 65 74 69 74 69 76 65 20 69 6e 73 65 72  repetitive inser
1c3f7 74 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66  ts, the.    ** f
1c3f8 69 72 73 74 20 66 65 77 20 61 74 74 65 6d 70 74  irst few attempt
1c3f9 73 20 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20  s at choosing a 
1c3fa 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63  random rowid pic
1c3fb 6b 20 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20  k values just a 
1c3fc 6c 69 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61  little.    ** la
1c3fd 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72  rger than the pr
1c3fe 65 76 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54  evious rowid.  T
1c3ff 68 69 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f  his has been sho
1c400 77 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c  wn experimentall
1c401 79 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62  y.    ** to doub
1c402 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20  le the speed of 
1c403 74 68 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69  the COPY operati
1c404 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  on..    */.    i
1c405 6e 74 20 72 65 73 3d 30 2c 20 72 78 3d 53 51 4c  nt res=0, rx=SQL
1c406 49 54 45 5f 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20  ITE_OK, cnt;.   
1c407 20 69 36 34 20 78 3b 0a 20 20 20 20 63 6e 74 20   i64 x;.    cnt 
1c408 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 73 71  = 0;.    if( (sq
1c409 6c 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 28  lite3BtreeFlags(
1c40a 70 43 2d 3e 70 43 75 72 73 6f 72 29 26 28 42 54  pC->pCursor)&(BT
1c40b 52 45 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45  REE_INTKEY|BTREE
1c40c 5f 5a 45 52 4f 44 41 54 41 29 29 20 21 3d 0a 20  _ZERODATA)) !=. 
1c40d 20 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 49           BTREE_I
1c40e 4e 54 4b 45 59 20 29 7b 0a 20 20 20 20 20 20 72  NTKEY ){.      r
1c40f 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
1c410 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67  PT_BKPT;.      g
1c411 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1c412 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  _error;.    }.  
1c413 20 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74    assert( (sqlit
1c414 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d  e3BtreeFlags(pC-
1c415 3e 70 43 75 72 73 6f 72 29 20 26 20 42 54 52 45  >pCursor) & BTRE
1c416 45 5f 49 4e 54 4b 45 59 29 21 3d 30 20 29 3b 0a  E_INTKEY)!=0 );.
1c417 20 20 20 20 61 73 73 65 72 74 28 20 28 73 71 6c      assert( (sql
1c418 69 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70  ite3BtreeFlags(p
1c419 43 2d 3e 70 43 75 72 73 6f 72 29 20 26 20 42 54  C->pCursor) & BT
1c41a 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 3d 3d 30  REE_ZERODATA)==0
1c41b 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   );..#ifdef SQLI
1c41c 54 45 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23  TE_32BIT_ROWID.#
1c41d 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f     define MAX_RO
1c41e 57 49 44 20 30 78 37 66 66 66 66 66 66 66 0a 23  WID 0x7fffffff.#
1c41f 65 6c 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65  else.    /* Some
1c420 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c   compilers compl
1c421 61 69 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61  ain about consta
1c422 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  nts of the form 
1c423 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66  0x7fffffffffffff
1c424 66 66 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72  ff..    ** Other
1c425 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74  s complain about
1c426 20 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66   0x7ffffffffffff
1c427 66 66 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c  ffffLL.  The fol
1c428 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65  lowing macro see
1c429 6d 73 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f  ms.    ** to pro
1c42a 76 69 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e  vide the constan
1c42b 74 20 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61  t while making a
1c42c 6c 6c 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70  ll compilers hap
1c42d 70 79 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64  py..    */.#   d
1c42e 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20  efine MAX_ROWID 
1c42f 20 28 69 36 34 29 28 20 28 28 28 75 36 34 29 30   (i64)( (((u64)0
1c430 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 20  x7fffffff)<<32) 
1c431 7c 20 28 75 36 34 29 30 78 66 66 66 66 66 66 66  | (u64)0xfffffff
1c432 66 20 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  f ).#endif..    
1c433 69 66 28 20 21 70 43 2d 3e 75 73 65 52 61 6e 64  if( !pC->useRand
1c434 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  omRowid ){.     
1c435 20 76 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65   v = sqlite3Btre
1c436 65 47 65 74 43 61 63 68 65 64 52 6f 77 69 64 28  eGetCachedRowid(
1c437 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  pC->pCursor);.  
1c438 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a      if( v==0 ){.
1c439 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1c43a 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43  ite3BtreeLast(pC
1c43b 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29  ->pCursor, &res)
1c43c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
1c43d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1c43e 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62           goto ab
1c43f 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1c440 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1c441 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20      if( res ){. 
1c442 20 20 20 20 20 20 20 20 20 76 20 3d 20 31 3b 0a           v = 1;.
1c443 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1c444 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1c445 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d  BtreeKeySize(pC-
1c446 3e 70 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20  >pCursor, &v);. 
1c447 20 20 20 20 20 20 20 20 20 76 20 3d 20 6b 65 79           v = key
1c448 54 6f 49 6e 74 28 76 29 3b 0a 20 20 20 20 20 20  ToInt(v);.      
1c449 20 20 20 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52      if( v==MAX_R
1c44a 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20  OWID ){.        
1c44b 20 20 20 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f      pC->useRando
1c44c 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20  mRowid = 1;.    
1c44d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1c44e 20 20 20 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20           v++;.  
1c44f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1c450 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66    }.      }..#if
1c451 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1c452 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20  _AUTOINCREMENT. 
1c453 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33       if( pOp->p3
1c454 20 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20   ){.        Mem 
1c455 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 61  *pMem;.        a
1c456 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1c457 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e   && pOp->p3<=p->
1c458 6e 4d 65 6d 20 29 3b 20 2f 2a 20 50 33 20 69 73  nMem ); /* P3 is
1c459 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20   a valid memory 
1c45a 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20  cell */.        
1c45b 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pMem = &p->aMem[
1c45c 70 4f 70 2d 3e 70 33 5d 3b 0a 09 52 45 47 49 53  pOp->p3];..REGIS
1c45d 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1c45e 33 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  3, pMem);.      
1c45f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1c460 49 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29  Integerify(pMem)
1c461 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1c462 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
1c463 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20   MEM_Int)!=0 ); 
1c464 20 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64   /* mem(P3) hold
1c465 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a  s an integer */.
1c466 20 20 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d          if( pMem
1c467 2d 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44  ->u.i==MAX_ROWID
1c468 20 7c 7c 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f   || pC->useRando
1c469 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  mRowid ){.      
1c46a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1c46b 46 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20  FULL;.          
1c46c 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1c46d 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1c46e 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 76   }.        if( v
1c46f 3c 70 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a  <pMem->u.i+1 ){.
1c470 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 70 4d            v = pM
1c471 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20  em->u.i + 1;.   
1c472 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
1c473 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20  Mem->u.i = v;.  
1c474 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
1c475 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1c476 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 70  SetCachedRowid(p
1c477 43 2d 3e 70 43 75 72 73 6f 72 2c 20 76 3c 4d 41  C->pCursor, v<MA
1c478 58 5f 52 4f 57 49 44 20 3f 20 76 2b 31 20 3a 20  X_ROWID ? v+1 : 
1c479 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  0);.    }.    if
1c47a 28 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52  ( pC->useRandomR
1c47b 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73  owid ){.      as
1c47c 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30  sert( pOp->p3==0
1c47d 20 29 3b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46   );  /* SQLITE_F
1c47e 55 4c 4c 20 6d 75 73 74 20 68 61 76 65 20 6f 63  ULL must have oc
1c47f 63 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20  curred prior to 
1c480 74 68 69 73 20 2a 2f 0a 20 20 20 20 20 20 76 20  this */.      v 
1c481 3d 20 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f  = db->priorNewRo
1c482 77 69 64 3b 0a 20 20 20 20 20 20 63 6e 74 20 3d  wid;.      cnt =
1c483 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20   0;.      do{.  
1c484 20 20 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30        if( cnt==0
1c485 20 26 26 20 28 76 26 30 78 66 66 66 66 66 66 29   && (v&0xffffff)
1c486 3d 3d 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==v ){.         
1c487 20 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65   v++;.        }e
1c488 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
1c489 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
1c48a 73 28 73 69 7a 65 6f 66 28 76 29 2c 20 26 76 29  s(sizeof(v), &v)
1c48b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1c48c 63 6e 74 3c 35 20 29 20 76 20 26 3d 20 30 78 66  cnt<5 ) v &= 0xf
1c48d 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 20 7d  fffff;.        }
1c48e 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d  .        if( v==
1c48f 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
1c490 20 20 20 20 20 20 78 20 3d 20 69 6e 74 54 6f 4b        x = intToK
1c491 65 79 28 76 29 3b 0a 20 20 20 20 20 20 20 20 72  ey(v);.        r
1c492 78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  x = sqlite3Btree
1c493 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70  MovetoUnpacked(p
1c494 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28  C->pCursor, 0, (
1c495 75 36 34 29 78 2c 20 30 2c 20 26 72 65 73 29 3b  u64)x, 0, &res);
1c496 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a  .        cnt++;.
1c497 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 63 6e        }while( cn
1c498 74 3c 31 30 30 20 26 26 20 72 78 3d 3d 53 51 4c  t<100 && rx==SQL
1c499 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30  ITE_OK && res==0
1c49a 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 72   );.      db->pr
1c49b 69 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 76 3b  iorNewRowid = v;
1c49c 0a 20 20 20 20 20 20 69 66 28 20 72 78 3d 3d 53  .      if( rx==S
1c49d 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d  QLITE_OK && res=
1c49e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
1c49f 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a   = SQLITE_FULL;.
1c4a0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
1c4a1 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1c4a2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1c4a3 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61     pC->rowidIsVa
1c4a4 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d  lid = 0;.    pC-
1c4a5 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1c4a6 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63  = 0;.    pC->cac
1c4a7 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1c4a8 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d 65  _STALE;.  }.  Me
1c4a9 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1c4aa 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70  t, MEM_Int);.  p
1c4ab 4f 75 74 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20  Out->u.i = v;.  
1c4ac 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c4ad 6f 64 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50  ode: Insert P1 P
1c4ae 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
1c4af 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20   Write an entry 
1c4b0 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f  into the table o
1c4b1 66 20 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20  f cursor P1.  A 
1c4b2 6e 65 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20  new entry is.** 
1c4b3 63 72 65 61 74 65 64 20 69 66 20 69 74 20 64 6f  created if it do
1c4b4 65 73 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78  esn't already ex
1c4b5 69 73 74 20 6f 72 20 74 68 65 20 64 61 74 61 20  ist or the data 
1c4b6 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  for an existing.
1c4b7 2a 2a 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72  ** entry is over
1c4b8 77 72 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61  written.  The da
1c4b9 74 61 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ta is the value 
1c4ba 73 74 6f 72 65 64 20 72 65 67 69 73 74 65 72 0a  stored register.
1c4bb 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20 54 68  ** number P2. Th
1c4bc 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65 64 20  e key is stored 
1c4bd 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 20  in register P3. 
1c4be 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a 20  The key must.** 
1c4bf 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  be an integer..*
1c4c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c  *.** If the OPFL
1c4c1 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20  AG_NCHANGE flag 
1c4c2 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68  of P5 is set, th
1c4c3 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  en the row chang
1c4c4 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e  e count is.** in
1c4c5 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72  cremented (other
1c4c6 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74  wise not).  If t
1c4c7 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  he OPFLAG_LASTRO
1c4c8 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69  WID flag of P5 i
1c4c9 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72  s set,.** then r
1c4ca 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 66  owid is stored f
1c4cb 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 65  or subsequent re
1c4cc 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73  turn by the.** s
1c4cd 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
1c4ce 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74  rt_rowid() funct
1c4cf 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69  ion (otherwise i
1c4d0 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29  t is unmodified)
1c4d1 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ..**.** Paramete
1c4d2 72 20 50 34 20 6d 61 79 20 70 6f 69 6e 74 20 74  r P4 may point t
1c4d3 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61  o a string conta
1c4d4 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2d  ining the table-
1c4d5 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20  name, or.** may 
1c4d6 62 65 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69  be NULL. If it i
1c4d7 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
1c4d8 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b   the update-hook
1c4d9 20 0a 2a 2a 20 28 73 71 6c 69 74 65 33 2e 78 55   .** (sqlite3.xU
1c4da 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 20 69  pdateCallback) i
1c4db 73 20 69 6e 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77  s invoked follow
1c4dc 69 6e 67 20 61 20 73 75 63 63 65 73 73 66 75 6c  ing a successful
1c4dd 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28   insert..**.** (
1c4de 57 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66  WARNING/TODO: If
1c4df 20 50 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d   P1 is a pseudo-
1c4e0 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73  cursor and P2 is
1c4e1 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20   dynamically.** 
1c4e2 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20  allocated, then 
1c4e3 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20  ownership of P2 
1c4e4 69 73 20 74 72 61 6e 73 66 65 72 72 65 64 20 74  is transferred t
1c4e5 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72  o the pseudo-cur
1c4e6 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73  sor.** and regis
1c4e7 74 65 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65  ter P2 becomes e
1c4e8 70 68 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68  phemeral.  If th
1c4e9 65 20 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e  e cursor is chan
1c4ea 67 65 64 2c 20 74 68 65 0a 2a 2a 20 76 61 6c 75  ged, the.** valu
1c4eb 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32  e of register P2
1c4ec 20 77 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67   will then chang
1c4ed 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  e.  Make sure th
1c4ee 69 73 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63  is does not.** c
1c4ef 61 75 73 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d  ause any problem
1c4f0 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  s.).**.** This i
1c4f1 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20  nstruction only 
1c4f2 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e  works on tables.
1c4f3 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74    The equivalent
1c4f4 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20   instruction.** 
1c4f5 66 6f 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f  for indices is O
1c4f6 50 5f 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a  P_IdxInsert..*/.
1c4f7 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20  case OP_Insert: 
1c4f8 7b 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 20 3d  {.  Mem *pData =
1c4f9 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1c4fa 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 20  2];.  Mem *pKey 
1c4fb 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1c4fc 70 33 5d 3b 0a 0a 20 20 69 36 34 20 69 4b 65 79  p3];..  i64 iKey
1c4fd 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67  ;   /* The integ
1c4fe 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20  er ROWID or key 
1c4ff 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74  for the record t
1c500 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f  o be inserted */
1c501 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
1c502 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p1;.  VdbeCursor
1c503 20 2a 70 43 3b 0a 20 20 61 73 73 65 72 74 28 20   *pC;.  assert( 
1c504 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1c505 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70  rsor );.  pC = p
1c506 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73  ->apCsr[i];.  as
1c507 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20  sert( pC!=0 );. 
1c508 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75   assert( pC->pCu
1c509 72 73 6f 72 21 3d 30 20 7c 7c 20 70 43 2d 3e 70  rsor!=0 || pC->p
1c50a 73 65 75 64 6f 54 61 62 6c 65 20 29 3b 0a 20 20  seudoTable );.  
1c50b 61 73 73 65 72 74 28 20 70 4b 65 79 2d 3e 66 6c  assert( pKey->fl
1c50c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
1c50d 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69  .  assert( pC->i
1c50e 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49  sTable );.  REGI
1c50f 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1c510 70 32 2c 20 70 44 61 74 61 29 3b 0a 20 20 52 45  p2, pData);.  RE
1c511 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1c512 2d 3e 70 33 2c 20 70 4b 65 79 29 3b 0a 0a 20 20  ->p3, pKey);..  
1c513 69 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28  iKey = intToKey(
1c514 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a 20 20 69 66  pKey->u.i);.  if
1c515 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c  ( pOp->p5 & OPFL
1c516 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e  AG_NCHANGE ) p->
1c517 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28  nChange++;.  if(
1c518 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41   pOp->p5 & OPFLA
1c519 47 5f 4c 41 53 54 52 4f 57 49 44 20 29 20 64 62  G_LASTROWID ) db
1c51a 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 4b  ->lastRowid = pK
1c51b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 70  ey->u.i;.  if( p
1c51c 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45  Data->flags & ME
1c51d 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 44  M_Null ){.    pD
1c51e 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20  ata->z = 0;.    
1c51f 70 44 61 74 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20  pData->n = 0;.  
1c520 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
1c521 74 28 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20  t( pData->flags 
1c522 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f  & (MEM_Blob|MEM_
1c523 53 74 72 29 20 29 3b 0a 20 20 7d 0a 20 20 69 66  Str) );.  }.  if
1c524 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c  ( pC->pseudoTabl
1c525 65 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 43  e ){.    if( !pC
1c526 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62  ->ephemPseudoTab
1c527 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  le ){.      sqli
1c528 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
1c529 2d 3e 70 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a  ->pData);.    }.
1c52a 20 20 20 20 70 43 2d 3e 69 4b 65 79 20 3d 20 69      pC->iKey = i
1c52b 4b 65 79 3b 0a 20 20 20 20 70 43 2d 3e 6e 44 61  Key;.    pC->nDa
1c52c 74 61 20 3d 20 70 44 61 74 61 2d 3e 6e 3b 0a 20  ta = pData->n;. 
1c52d 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e 7a 3d     if( pData->z=
1c52e 3d 70 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20  =pData->zMalloc 
1c52f 7c 7c 20 70 43 2d 3e 65 70 68 65 6d 50 73 65 75  || pC->ephemPseu
1c530 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  doTable ){.     
1c531 20 70 43 2d 3e 70 44 61 74 61 20 3d 20 70 44 61   pC->pData = pDa
1c532 74 61 2d 3e 7a 3b 0a 20 20 20 20 20 20 69 66 28  ta->z;.      if(
1c533 20 21 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 64   !pC->ephemPseud
1c534 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  oTable ){.      
1c535 20 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26    pData->flags &
1c536 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20  = ~MEM_Dyn;.    
1c537 20 20 20 20 70 44 61 74 61 2d 3e 66 6c 61 67 73      pData->flags
1c538 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20   |= MEM_Ephem;. 
1c539 20 20 20 20 20 20 20 70 44 61 74 61 2d 3e 7a 4d         pData->zM
1c53a 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20  alloc = 0;.     
1c53b 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
1c53c 20 20 20 20 70 43 2d 3e 70 44 61 74 61 20 3d 20      pC->pData = 
1c53d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 70  sqlite3Malloc( p
1c53e 43 2d 3e 6e 44 61 74 61 2b 32 20 29 3b 0a 20 20  C->nData+2 );.  
1c53f 20 20 20 20 69 66 28 20 21 70 43 2d 3e 70 44 61      if( !pC->pDa
1c540 74 61 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  ta ) goto no_mem
1c541 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  ;.      memcpy(p
1c542 43 2d 3e 70 44 61 74 61 2c 20 70 44 61 74 61 2d  C->pData, pData-
1c543 3e 7a 2c 20 70 43 2d 3e 6e 44 61 74 61 29 3b 0a  >z, pC->nData);.
1c544 20 20 20 20 20 20 70 43 2d 3e 70 44 61 74 61 5b        pC->pData[
1c545 70 43 2d 3e 6e 44 61 74 61 5d 20 3d 20 30 3b 0a  pC->nData] = 0;.
1c546 20 20 20 20 20 20 70 43 2d 3e 70 44 61 74 61 5b        pC->pData[
1c547 70 43 2d 3e 6e 44 61 74 61 2b 31 5d 20 3d 20 30  pC->nData+1] = 0
1c548 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e  ;.    }.    pC->
1c549 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 7d  nullRow = 0;.  }
1c54a 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 5a  else{.    int nZ
1c54b 65 72 6f 3b 0a 20 20 20 20 69 66 28 20 70 44 61  ero;.    if( pDa
1c54c 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ta->flags & MEM_
1c54d 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 6e 5a  Zero ){.      nZ
1c54e 65 72 6f 20 3d 20 70 44 61 74 61 2d 3e 75 2e 6e  ero = pData->u.n
1c54f 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Zero;.    }else{
1c550 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20 3d 20 30  .      nZero = 0
1c551 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1c552 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
1c553 64 52 6f 77 69 64 28 70 43 2d 3e 70 43 75 72 73  dRowid(pC->pCurs
1c554 6f 72 2c 20 30 29 3b 0a 20 20 20 20 72 63 20 3d  or, 0);.    rc =
1c555 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73   sqlite3BtreeIns
1c556 65 72 74 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c  ert(pC->pCursor,
1c557 20 30 2c 20 69 4b 65 79 2c 0a 20 20 20 20 20 20   0, iKey,.      
1c558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c559 20 20 20 20 20 20 70 44 61 74 61 2d 3e 7a 2c 20        pData->z, 
1c55a 70 44 61 74 61 2d 3e 6e 2c 20 6e 5a 65 72 6f 2c  pData->n, nZero,
1c55b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1c55c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 70               pOp
1c55d 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 41 50  ->p5 & OPFLAG_AP
1c55e 50 45 4e 44 29 3b 0a 20 20 7d 0a 20 20 0a 20 20  PEND);.  }.  .  
1c55f 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1c560 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 64 65 66 65   = 0;.  pC->defe
1c561 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a  rredMoveto = 0;.
1c562 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75    pC->cacheStatu
1c563 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1c564 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ..  /* Invoke th
1c565 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66  e update-hook if
1c566 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20   required. */.  
1c567 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1c568 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65  K && db->xUpdate
1c569 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d  Callback && pOp-
1c56a 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e  >p4.z ){.    con
1c56b 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64  st char *zDb = d
1c56c 62 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e  b->aDb[pC->iDb].
1c56d 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74  zName;.    const
1c56e 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f   char *zTbl = pO
1c56f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 69 6e 74  p->p4.z;.    int
1c570 20 6f 70 20 3d 20 28 28 70 4f 70 2d 3e 70 35 20   op = ((pOp->p5 
1c571 26 20 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54  & OPFLAG_ISUPDAT
1c572 45 29 20 3f 20 53 51 4c 49 54 45 5f 55 50 44 41  E) ? SQLITE_UPDA
1c573 54 45 20 3a 20 53 51 4c 49 54 45 5f 49 4e 53 45  TE : SQLITE_INSE
1c574 52 54 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  RT);.    assert(
1c575 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a   pC->isTable );.
1c576 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43      db->xUpdateC
1c577 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64  allback(db->pUpd
1c578 61 74 65 41 72 67 2c 20 6f 70 2c 20 7a 44 62 2c  ateArg, op, zDb,
1c579 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20   zTbl, iKey);.  
1c57a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 44    assert( pC->iD
1c57b 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72  b>=0 );.  }.  br
1c57c 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1c57d 65 3a 20 44 65 6c 65 74 65 20 50 31 20 50 32 20  e: Delete P1 P2 
1c57e 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c  * P4 *.**.** Del
1c57f 65 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 61  ete the record a
1c580 74 20 77 68 69 63 68 20 74 68 65 20 50 31 20 63  t which the P1 c
1c581 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74  ursor is current
1c582 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a  ly pointing..**.
1c583 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 77 69  ** The cursor wi
1c584 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f 69 6e 74  ll be left point
1c585 69 6e 67 20 61 74 20 65 69 74 68 65 72 20 74 68  ing at either th
1c586 65 20 6e 65 78 74 20 6f 72 20 74 68 65 20 70 72  e next or the pr
1c587 65 76 69 6f 75 73 0a 2a 2a 20 72 65 63 6f 72 64  evious.** record
1c588 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49   in the table. I
1c589 66 20 69 74 20 69 73 20 6c 65 66 74 20 70 6f 69  f it is left poi
1c58a 6e 74 69 6e 67 20 61 74 20 74 68 65 20 6e 65 78  nting at the nex
1c58b 74 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 0a 2a  t record, then.*
1c58c 2a 20 74 68 65 20 6e 65 78 74 20 4e 65 78 74 20  * the next Next 
1c58d 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c  instruction will
1c58e 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 65   be a no-op.  He
1c58f 6e 63 65 20 69 74 20 69 73 20 4f 4b 20 74 6f 20  nce it is OK to 
1c590 64 65 6c 65 74 65 0a 2a 2a 20 61 20 72 65 63 6f  delete.** a reco
1c591 72 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61  rd from within a
1c592 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a  n Next loop..**.
1c593 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47  ** If the OPFLAG
1c594 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 6f 66  _NCHANGE flag of
1c595 20 50 32 20 69 73 20 73 65 74 2c 20 74 68 65 6e   P2 is set, then
1c596 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20   the row change 
1c597 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72  count is.** incr
1c598 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 77 69  emented (otherwi
1c599 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50  se not)..**.** P
1c59a 31 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 70 73  1 must not be ps
1c59b 65 75 64 6f 2d 74 61 62 6c 65 2e 20 20 49 74 20  eudo-table.  It 
1c59c 68 61 73 20 74 6f 20 62 65 20 61 20 72 65 61 6c  has to be a real
1c59d 20 74 61 62 6c 65 20 77 69 74 68 0a 2a 2a 20 6d   table with.** m
1c59e 75 6c 74 69 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a  ultiple rows..**
1c59f 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74  .** If P4 is not
1c5a0 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69   NULL, then it i
1c5a1 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1c5a2 65 20 74 61 62 6c 65 20 74 68 61 74 20 50 31 20  e table that P1 
1c5a3 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74  is.** pointing t
1c5a4 6f 2e 20 20 54 68 65 20 75 70 64 61 74 65 20 68  o.  The update h
1c5a5 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f  ook will be invo
1c5a6 6b 65 64 2c 20 69 66 20 69 74 20 65 78 69 73 74  ked, if it exist
1c5a7 73 2e 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e  s..** If P4 is n
1c5a8 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65  ot NULL then the
1c5a9 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20   P1 cursor must 
1c5aa 68 61 76 65 20 62 65 65 6e 20 70 6f 73 69 74 69  have been positi
1c5ab 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4f 50  oned.** using OP
1c5ac 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 69 6f 72 20  _NotFound prior 
1c5ad 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73  to invoking this
1c5ae 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65   opcode..*/.case
1c5af 20 4f 50 5f 44 65 6c 65 74 65 3a 20 7b 0a 20 20   OP_Delete: {.  
1c5b0 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  int i = pOp->p1;
1c5b1 0a 20 20 69 36 34 20 69 4b 65 79 20 3d 20 30 3b  .  i64 iKey = 0;
1c5b2 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1c5b3 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  C;..  assert( i>
1c5b4 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
1c5b5 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e  or );.  pC = p->
1c5b6 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65  apCsr[i];.  asse
1c5b7 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61  rt( pC!=0 );.  a
1c5b8 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73  ssert( pC->pCurs
1c5b9 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c  or!=0 );  /* Onl
1c5ba 79 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c  y valid for real
1c5bb 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75   tables, no pseu
1c5bc 64 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f  dotables */..  /
1c5bd 2a 20 49 66 20 74 68 65 20 75 70 64 61 74 65 2d  * If the update-
1c5be 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76  hook will be inv
1c5bf 6f 6b 65 64 2c 20 73 65 74 20 69 4b 65 79 20 74  oked, set iKey t
1c5c0 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74  o the rowid of t
1c5c1 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e  he.  ** row bein
1c5c2 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a  g deleted..  */.
1c5c3 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74    if( db->xUpdat
1c5c4 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70  eCallback && pOp
1c5c5 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73  ->p4.z ){.    as
1c5c6 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c  sert( pC->isTabl
1c5c7 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  e );.    assert(
1c5c8 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69   pC->rowidIsVali
1c5c9 64 20 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77  d );  /* lastRow
1c5ca 69 64 20 73 65 74 20 62 79 20 70 72 65 76 69 6f  id set by previo
1c5cb 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a  us OP_NotFound *
1c5cc 2f 0a 20 20 20 20 69 4b 65 79 20 3d 20 70 43 2d  /.    iKey = pC-
1c5cd 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a  >lastRowid;.  }.
1c5ce 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
1c5cf 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
1c5d0 70 43 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  pC);.  if( rc ) 
1c5d1 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1c5d2 6f 5f 65 72 72 6f 72 3b 0a 20 20 73 71 6c 69 74  o_error;.  sqlit
1c5d3 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64  e3BtreeSetCached
1c5d4 52 6f 77 69 64 28 70 43 2d 3e 70 43 75 72 73 6f  Rowid(pC->pCurso
1c5d5 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71  r, 0);.  rc = sq
1c5d6 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65  lite3BtreeDelete
1c5d7 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20  (pC->pCursor);. 
1c5d8 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73   pC->cacheStatus
1c5d9 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1c5da 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
1c5db 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20   update-hook if 
1c5dc 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69  required. */.  i
1c5dd 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1c5de 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43   && db->xUpdateC
1c5df 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e  allback && pOp->
1c5e0 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73  p4.z ){.    cons
1c5e1 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62  t char *zDb = db
1c5e2 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a  ->aDb[pC->iDb].z
1c5e3 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
1c5e4 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70  char *zTbl = pOp
1c5e5 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e  ->p4.z;.    db->
1c5e6 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28  xUpdateCallback(
1c5e7 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20  db->pUpdateArg, 
1c5e8 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a  SQLITE_DELETE, z
1c5e9 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b  Db, zTbl, iKey);
1c5ea 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1c5eb 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20  >iDb>=0 );.  }. 
1c5ec 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f   if( pOp->p2 & O
1c5ed 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 20  PFLAG_NCHANGE ) 
1c5ee 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20  p->nChange++;.  
1c5ef 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1c5f0 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20  ode: ResetCount 
1c5f1 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  P1 * *.**.** Thi
1c5f2 73 20 6f 70 63 6f 64 65 20 72 65 73 65 74 73 20  s opcode resets 
1c5f3 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c  the VMs internal
1c5f4 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1c5f5 74 6f 20 30 2e 20 49 66 20 50 31 20 69 73 20 74  to 0. If P1 is t
1c5f6 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  rue,.** then the
1c5f7 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68   value of the ch
1c5f8 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20  ange counter is 
1c5f9 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64 61  copied to the da
1c5fa 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a  tabase handle.**
1c5fb 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1c5fc 28 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62  (returned by sub
1c5fd 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
1c5fe 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
1c5ff 28 29 29 0a 2a 2a 20 62 65 66 6f 72 65 20 69 74  ()).** before it
1c600 20 69 73 20 72 65 73 65 74 2e 20 54 68 69 73 20   is reset. This 
1c601 69 73 20 75 73 65 64 20 62 79 20 74 72 69 67 67  is used by trigg
1c602 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a  er programs..*/.
1c603 63 61 73 65 20 4f 50 5f 52 65 73 65 74 43 6f 75  case OP_ResetCou
1c604 6e 74 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d  nt: {.  if( pOp-
1c605 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >p1 ){.    sqlit
1c606 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73  e3VdbeSetChanges
1c607 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29  (db, p->nChange)
1c608 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e  ;.  }.  p->nChan
1c609 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b  ge = 0;.  break;
1c60a 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
1c60b 6f 77 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a  owData P1 P2 * *
1c60c 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69   *.**.** Write i
1c60d 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20  nto register P2 
1c60e 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77  the complete row
1c60f 20 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72   data for cursor
1c610 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73   P1..** There is
1c611 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69   no interpretati
1c612 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20  on of the data. 
1c613 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20   .** It is just 
1c614 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20  copied onto the 
1c615 50 32 20 72 65 67 69 73 74 65 72 20 65 78 61 63  P2 register exac
1c616 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73  tly as .** it is
1c617 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61   found in the da
1c618 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1c619 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72  ** If the P1 cur
1c61a 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e  sor must be poin
1c61b 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20  ting to a valid 
1c61c 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20  row (not a NULL 
1c61d 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61  row).** of a rea
1c61e 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70  l table, not a p
1c61f 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a  seudo-table..*/.
1c620 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65  /* Opcode: RowKe
1c621 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  y P1 P2 * * *.**
1c622 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72  .** Write into r
1c623 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63  egister P2 the c
1c624 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20  omplete row key 
1c625 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a  for cursor P1..*
1c626 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e  * There is no in
1c627 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20  terpretation of 
1c628 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54  the data.  .** T
1c629 68 65 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64  he key is copied
1c62a 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67   onto the P3 reg
1c62b 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73  ister exactly as
1c62c 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64   .** it is found
1c62d 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1c62e 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
1c62f 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75  the P1 cursor mu
1c630 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74  st be pointing t
1c631 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e  o a valid row (n
1c632 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a  ot a NULL row).*
1c633 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c  * of a real tabl
1c634 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d  e, not a pseudo-
1c635 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
1c636 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f  P_RowKey:.case O
1c637 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 20 20 69  P_RowData: {.  i
1c638 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
1c639 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c63a 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ;.  BtCursor *pC
1c63b 72 73 72 3b 0a 20 20 75 33 32 20 6e 3b 0a 0a 20  rsr;.  u32 n;.. 
1c63c 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
1c63d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a  [pOp->p2];..  /*
1c63e 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b 65   Note that RowKe
1c63f 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 72  y and RowData ar
1c640 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c 79  e really exactly
1c641 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 75   the same instru
1c642 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72  ction */.  asser
1c643 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
1c644 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20  nCursor );.  pC 
1c645 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20  = p->apCsr[i];. 
1c646 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54   assert( pC->isT
1c647 61 62 6c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63  able || pOp->opc
1c648 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29  ode==OP_RowKey )
1c649 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e  ;.  assert( pC->
1c64a 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e  isIndex || pOp->
1c64b 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61  opcode==OP_RowDa
1c64c 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ta );.  assert( 
1c64d 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pC!=0 );.  asser
1c64e 74 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d  t( pC->nullRow==
1c64f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1c650 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 3d 3d  C->pseudoTable==
1c651 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1c652 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b  C->pCursor!=0 );
1c653 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70  .  pCrsr = pC->p
1c654 43 75 72 73 6f 72 3b 0a 20 20 72 63 20 3d 20 73  Cursor;.  rc = s
1c655 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
1c656 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 69 66  Moveto(pC);.  if
1c657 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72  ( rc ) goto abor
1c658 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1c659 20 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65    if( pC->isInde
1c65a 78 20 29 7b 0a 20 20 20 20 69 36 34 20 6e 36 34  x ){.    i64 n64
1c65b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  ;.    assert( !p
1c65c 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1c65d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65    sqlite3BtreeKe
1c65e 79 53 69 7a 65 28 70 43 72 73 72 2c 20 26 6e 36  ySize(pCrsr, &n6
1c65f 34 29 3b 0a 20 20 20 20 69 66 28 20 6e 36 34 3e  4);.    if( n64>
1c660 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1c661 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
1c662 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f  ){.      goto to
1c663 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20  o_big;.    }.   
1c664 20 6e 20 3d 20 28 69 6e 74 29 6e 36 34 3b 0a 20   n = (int)n64;. 
1c665 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
1c666 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65  te3BtreeDataSize
1c667 28 70 43 72 73 72 2c 20 26 6e 29 3b 0a 20 20 20  (pCrsr, &n);.   
1c668 20 69 66 28 20 28 69 6e 74 29 6e 3e 64 62 2d 3e   if( (int)n>db->
1c669 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1c66a 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
1c66b 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69       goto too_bi
1c66c 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  g;.    }.  }.  i
1c66d 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
1c66e 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e 2c 20 30  mGrow(pOut, n, 0
1c66f 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f  ) ){.    goto no
1c670 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 4f 75 74  _mem;.  }.  pOut
1c671 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 4d 65 6d 53 65  ->n = n;.  MemSe
1c672 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1c673 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28  MEM_Blob);.  if(
1c674 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a   pC->isIndex ){.
1c675 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1c676 42 74 72 65 65 4b 65 79 28 70 43 72 73 72 2c 20  BtreeKey(pCrsr, 
1c677 30 2c 20 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a  0, n, pOut->z);.
1c678 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
1c679 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  = sqlite3BtreeDa
1c67a 74 61 28 70 43 72 73 72 2c 20 30 2c 20 6e 2c 20  ta(pCrsr, 0, n, 
1c67b 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20 20  pOut->z);.  }.  
1c67c 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pOut->enc = SQLI
1c67d 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20  TE_UTF8;  /* In 
1c67e 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73  case the blob is
1c67f 20 65 76 65 72 20 63 61 73 74 20 74 6f 20 74 65   ever cast to te
1c680 78 74 20 2a 2f 0a 20 20 55 50 44 41 54 45 5f 4d  xt */.  UPDATE_M
1c681 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74  AX_BLOBSIZE(pOut
1c682 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1c683 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 69 64 20  * Opcode: Rowid 
1c684 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1c685 2a 20 53 74 6f 72 65 20 69 6e 20 72 65 67 69 73  * Store in regis
1c686 74 65 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65  ter P2 an intege
1c687 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6b  r which is the k
1c688 65 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ey of the table 
1c689 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 50 31  entry that.** P1
1c68a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
1c68b 69 6e 74 20 74 6f 2e 0a 2a 2f 0a 63 61 73 65 20  int to..*/.case 
1c68c 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 20 20 20 20  OP_Rowid: {     
1c68d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f              /* o
1c68e 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1c68f 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
1c690 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  >p1;.  VdbeCurso
1c691 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a  r *pC;.  i64 v;.
1c692 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1c693 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
1c694 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43  );.  pC = p->apC
1c695 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28  sr[i];.  assert(
1c696 20 70 43 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d   pC!=0 );.  rc =
1c697 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73   sqlite3VdbeCurs
1c698 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20  orMoveto(pC);.  
1c699 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62  if( rc ) goto ab
1c69a 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1c69b 3b 0a 20 20 69 66 28 20 70 43 2d 3e 72 6f 77 69  ;.  if( pC->rowi
1c69c 64 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 20 20  dIsValid ){.    
1c69d 76 20 3d 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69  v = pC->lastRowi
1c69e 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  d;.  }else if( p
1c69f 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29  C->pseudoTable )
1c6a0 7b 0a 20 20 20 20 76 20 3d 20 6b 65 79 54 6f 49  {.    v = keyToI
1c6a1 6e 74 28 70 43 2d 3e 69 4b 65 79 29 3b 0a 20 20  nt(pC->iKey);.  
1c6a2 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 6e 75  }else if( pC->nu
1c6a3 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 2f 2a 20  llRow ){.    /* 
1c6a4 4c 65 61 76 65 20 74 68 65 20 72 6f 77 69 64 20  Leave the rowid 
1c6a5 73 65 74 20 74 6f 20 61 20 4e 55 4c 4c 20 2a 2f  set to a NULL */
1c6a6 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 65  .    break;.  }e
1c6a7 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
1c6a8 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20   pC->pCursor!=0 
1c6a9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
1c6aa 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e 70  reeKeySize(pC->p
1c6ab 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20  Cursor, &v);.   
1c6ac 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28 76 29   v = keyToInt(v)
1c6ad 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e  ;.  }.  pOut->u.
1c6ae 69 20 3d 20 76 3b 0a 20 20 4d 65 6d 53 65 74 54  i = v;.  MemSetT
1c6af 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1c6b0 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b  M_Int);.  break;
1c6b1 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .}../* Opcode: N
1c6b2 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a 20 2a 20  ullRow P1 * * * 
1c6b3 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  *.**.** Move the
1c6b4 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20   cursor P1 to a 
1c6b5 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e 79 20 4f  null row.  Any O
1c6b6 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 61 74 69  P_Column operati
1c6b7 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75  ons.** that occu
1c6b8 72 20 77 68 69 6c 65 20 74 68 65 20 63 75 72 73  r while the curs
1c6b9 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 6e 75 6c  or is on the nul
1c6ba 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c 77 61 79  l row will alway
1c6bb 73 0a 2a 2a 20 77 72 69 74 65 20 61 20 4e 55 4c  s.** write a NUL
1c6bc 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75  L..*/.case OP_Nu
1c6bd 6c 6c 52 6f 77 3a 20 7b 0a 20 20 69 6e 74 20 69  llRow: {.  int i
1c6be 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64   = pOp->p1;.  Vd
1c6bf 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20  beCursor *pC;.. 
1c6c0 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
1c6c1 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b   i<p->nCursor );
1c6c2 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  .  pC = p->apCsr
1c6c3 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  [i];.  assert( p
1c6c4 43 21 3d 30 20 29 3b 0a 20 20 70 43 2d 3e 6e 75  C!=0 );.  pC->nu
1c6c5 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 70 43 2d  llRow = 1;.  pC-
1c6c6 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1c6c7 30 3b 0a 20 20 69 66 28 20 70 43 2d 3e 70 43 75  0;.  if( pC->pCu
1c6c8 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  rsor ){.    sqli
1c6c9 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
1c6ca 73 6f 72 28 70 43 2d 3e 70 43 75 72 73 6f 72 29  sor(pC->pCursor)
1c6cb 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1c6cc 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 61 73  ../* Opcode: Las
1c6cd 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1c6ce 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65  .** The next use
1c6cf 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72   of the Rowid or
1c6d0 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20   Column or Next 
1c6d1 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20  instruction for 
1c6d2 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65  P1 .** will refe
1c6d3 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  r to the last en
1c6d4 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
1c6d5 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  ase table or ind
1c6d6 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61  ex..** If the ta
1c6d7 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20  ble or index is 
1c6d8 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20  empty and P2>0, 
1c6d9 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69  then jump immedi
1c6da 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20  ately to P2..** 
1c6db 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66  If P2 is 0 or if
1c6dc 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
1c6dd 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  dex is not empty
1c6de 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a  , fall through.*
1c6df 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * to the followi
1c6e0 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ng instruction..
1c6e1 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 73 74 3a  */.case OP_Last:
1c6e2 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1c6e3 70 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  p */.  int i = p
1c6e4 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75  Op->p1;.  VdbeCu
1c6e5 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75  rsor *pC;.  BtCu
1c6e6 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69  rsor *pCrsr;.  i
1c6e7 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72  nt res;..  asser
1c6e8 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
1c6e9 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20  nCursor );.  pC 
1c6ea 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20  = p->apCsr[i];. 
1c6eb 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29   assert( pC!=0 )
1c6ec 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e  ;.  pCrsr = pC->
1c6ed 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72  pCursor;.  asser
1c6ee 74 28 20 70 43 72 73 72 21 3d 30 20 29 3b 0a 20  t( pCrsr!=0 );. 
1c6ef 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1c6f0 65 65 4c 61 73 74 28 70 43 72 73 72 2c 20 26 72  eeLast(pCrsr, &r
1c6f1 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52  es);.  pC->nullR
1c6f2 6f 77 20 3d 20 28 75 38 29 72 65 73 3b 0a 20 20  ow = (u8)res;.  
1c6f3 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1c6f4 74 6f 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 72 6f  to = 0;.  pC->ro
1c6f5 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1c6f6 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75    pC->cacheStatu
1c6f7 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1c6f8 0a 20 20 69 66 28 20 72 65 73 20 26 26 20 70 4f  .  if( res && pO
1c6f9 70 2d 3e 70 32 3e 30 20 29 7b 0a 20 20 20 20 70  p->p2>0 ){.    p
1c6fa 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1c6fb 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1c6fc 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72  ../* Opcode: Sor
1c6fd 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1c6fe 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1c6ff 64 6f 65 73 20 65 78 61 63 74 6c 79 20 74 68 65  does exactly the
1c700 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 4f   same thing as O
1c701 50 5f 52 65 77 69 6e 64 20 65 78 63 65 70 74 20  P_Rewind except 
1c702 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65  that.** it incre
1c703 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d  ments an undocum
1c704 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 61 72  ented global var
1c705 69 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 74  iable used for t
1c706 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f  esting..**.** So
1c707 72 74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c  rting is accompl
1c708 69 73 68 65 64 20 62 79 20 77 72 69 74 69 6e 67  ished by writing
1c709 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20   records into a 
1c70a 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a  sorting index,.*
1c70b 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e 67  * then rewinding
1c70c 20 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64 20   that index and 
1c70d 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b 20  playing it back 
1c70e 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74  from beginning t
1c70f 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 73  o.** end.  We us
1c710 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70  e the OP_Sort op
1c711 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66 20  code instead of 
1c712 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f 20  OP_Rewind to do 
1c713 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67  the.** rewinding
1c714 20 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c 6f   so that the glo
1c715 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69 6c  bal variable wil
1c716 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 64  l be incremented
1c717 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73 69   and.** regressi
1c718 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64 65 74  on tests can det
1c719 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
1c71a 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d 69  r not the optimi
1c71b 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63  zer is.** correc
1c71c 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f  tly optimizing o
1c71d 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73  ut sorts..*/.cas
1c71e 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20  e OP_Sort: {    
1c71f 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23      /* jump */.#
1c720 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1c721 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 74  T.  sqlite3_sort
1c722 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69  _count++;.  sqli
1c723 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74  te3_search_count
1c724 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e  --;.#endif.  p->
1c725 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f  aCounter[SQLITE_
1c726 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 2d  STMTSTATUS_SORT-
1c727 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20  1]++;.  /* Fall 
1c728 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f  through into OP_
1c729 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f  Rewind */.}./* O
1c72a 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50 31  pcode: Rewind P1
1c72b 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1c72c 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20  The next use of 
1c72d 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c  the Rowid or Col
1c72e 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74  umn or Next inst
1c72f 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a  ruction for P1 .
1c730 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f  ** will refer to
1c731 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
1c732 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1c733 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e   table or index.
1c734 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65  .** If the table
1c735 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70   or index is emp
1c736 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65  ty and P2>0, the
1c737 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  n jump immediate
1c738 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20  ly to P2..** If 
1c739 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68  P2 is 0 or if th
1c73a 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
1c73b 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66   is not empty, f
1c73c 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  all through.** t
1c73d 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
1c73e 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
1c73f 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20  case OP_Rewind: 
1c740 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1c741 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f   */.  int i = pO
1c742 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72  p->p1;.  VdbeCur
1c743 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
1c744 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e  sor *pCrsr;.  in
1c745 74 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 74  t res;..  assert
1c746 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
1c747 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d  Cursor );.  pC =
1c748 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20   p->apCsr[i];.  
1c749 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b  assert( pC!=0 );
1c74a 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20  .  if( (pCrsr = 
1c74b 70 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20  pC->pCursor)!=0 
1c74c 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1c74d 74 65 33 42 74 72 65 65 46 69 72 73 74 28 70 43  te3BtreeFirst(pC
1c74e 72 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  rsr, &res);.    
1c74f 70 43 2d 3e 61 74 46 69 72 73 74 20 3d 20 72 65  pC->atFirst = re
1c750 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70  s==0 ?1:0;.    p
1c751 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1c752 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63  o = 0;.    pC->c
1c753 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
1c754 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 70 43  HE_STALE;.    pC
1c755 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1c756 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
1c757 20 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20   res = 1;.  }.  
1c758 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75  pC->nullRow = (u
1c759 38 29 72 65 73 3b 0a 20 20 61 73 73 65 72 74 28  8)res;.  assert(
1c75a 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f   pOp->p2>0 && pO
1c75b 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a  p->p2<p->nOp );.
1c75c 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
1c75d 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1c75e 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1c75f 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65  }../* Opcode: Ne
1c760 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  xt P1 P2 * * *.*
1c761 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 63 75 72  *.** Advance cur
1c762 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69  sor P1 so that i
1c763 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
1c764 6e 65 78 74 20 6b 65 79 2f 64 61 74 61 20 70 61  next key/data pa
1c765 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62  ir in its.** tab
1c766 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 66  le or index.  If
1c767 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f   there are no mo
1c768 72 65 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69  re key/value pai
1c769 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  rs then fall thr
1c76a 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66  ough.** to the f
1c76b 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63  ollowing instruc
1c76c 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68  tion.  But if th
1c76d 65 20 63 75 72 73 6f 72 20 61 64 76 61 6e 63 65  e cursor advance
1c76e 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c   was successful,
1c76f 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61  .** jump immedia
1c770 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  tely to P2..**.*
1c771 2a 20 54 68 65 20 50 31 20 63 75 72 73 6f 72 20  * The P1 cursor 
1c772 6d 75 73 74 20 62 65 20 66 6f 72 20 61 20 72 65  must be for a re
1c773 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20  al table, not a 
1c774 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a  pseudo-table..**
1c775 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 50 72  .** See also: Pr
1c776 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ev.*/./* Opcode:
1c777 20 50 72 65 76 20 50 31 20 50 32 20 2a 20 2a 20   Prev P1 P2 * * 
1c778 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20  *.**.** Back up 
1c779 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61  cursor P1 so tha
1c77a 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  t it points to t
1c77b 68 65 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f  he previous key/
1c77c 64 61 74 61 20 70 61 69 72 20 69 6e 20 69 74 73  data pair in its
1c77d 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  .** table or ind
1c77e 65 78 2e 20 20 49 66 20 74 68 65 72 65 20 69 73  ex.  If there is
1c77f 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6b 65 79   no previous key
1c780 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 68 65  /value pairs the
1c781 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a  n fall through.*
1c782 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * to the followi
1c783 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  ng instruction. 
1c784 20 42 75 74 20 69 66 20 74 68 65 20 63 75 72 73   But if the curs
1c785 6f 72 20 62 61 63 6b 75 70 20 77 61 73 20 73 75  or backup was su
1c786 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d  ccessful,.** jum
1c787 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  p immediately to
1c788 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50   P2..**.** The P
1c789 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65  1 cursor must be
1c78a 20 66 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c   for a real tabl
1c78b 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d  e, not a pseudo-
1c78c 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
1c78d 50 5f 50 72 65 76 3a 20 20 20 20 20 20 20 20 20  P_Prev:         
1c78e 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65   /* jump */.case
1c78f 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 20 20 20 20   OP_Next: {     
1c790 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20     /* jump */.  
1c791 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c792 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1c793 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20  r;.  int res;.. 
1c794 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52   CHECK_FOR_INTER
1c795 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28 20  RUPT;.  assert( 
1c796 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1c797 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  p->p1<p->nCursor
1c798 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70   );.  pC = p->ap
1c799 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1c79a 69 66 28 20 70 43 3d 3d 30 20 29 7b 0a 20 20 20  if( pC==0 ){.   
1c79b 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 65 65 20   break;  /* See 
1c79c 74 69 63 6b 65 74 20 23 32 32 37 33 20 2a 2f 0a  ticket #2273 */.
1c79d 20 20 7d 0a 20 20 70 43 72 73 72 20 3d 20 70 43    }.  pCrsr = pC
1c79e 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73  ->pCursor;.  ass
1c79f 65 72 74 28 20 70 43 72 73 72 20 29 3b 0a 20 20  ert( pCrsr );.  
1c7a0 72 65 73 20 3d 20 31 3b 0a 20 20 61 73 73 65 72  res = 1;.  asser
1c7a1 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  t( pC->deferredM
1c7a2 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63  oveto==0 );.  rc
1c7a3 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d   = pOp->opcode==
1c7a4 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c 69 74 65  OP_Next ? sqlite
1c7a5 33 42 74 72 65 65 4e 65 78 74 28 70 43 72 73 72  3BtreeNext(pCrsr
1c7a6 2c 20 26 72 65 73 29 20 3a 0a 20 20 20 20 20 20  , &res) :.      
1c7a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c7a8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1c7a9 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 72  treePrevious(pCr
1c7aa 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 70 43 2d  sr, &res);.  pC-
1c7ab 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72  >nullRow = (u8)r
1c7ac 65 73 3b 0a 20 20 70 43 2d 3e 63 61 63 68 65 53  es;.  pC->cacheS
1c7ad 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1c7ae 41 4c 45 3b 0a 20 20 69 66 28 20 72 65 73 3d 3d  ALE;.  if( res==
1c7af 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  0 ){.    pc = pO
1c7b0 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 69  p->p2 - 1;.    i
1c7b1 66 28 20 70 4f 70 2d 3e 70 35 20 29 20 70 2d 3e  f( pOp->p5 ) p->
1c7b2 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 35  aCounter[pOp->p5
1c7b3 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 20 53 51  -1]++;.#ifdef SQ
1c7b4 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71  LITE_TEST.    sq
1c7b5 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
1c7b6 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  nt++;.#endif.  }
1c7b7 0a 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  .  pC->rowidIsVa
1c7b8 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b  lid = 0;.  break
1c7b9 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1c7ba 49 64 78 49 6e 73 65 72 74 20 50 31 20 50 32 20  IdxInsert P1 P2 
1c7bb 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67  P3 * *.**.** Reg
1c7bc 69 73 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61  ister P2 holds a
1c7bd 20 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d   SQL index key m
1c7be 61 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  ade using the.**
1c7bf 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74   MakeRecord inst
1c7c0 72 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20  ructions.  This 
1c7c1 6f 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 68  opcode writes th
1c7c2 61 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74  at key.** into t
1c7c3 68 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61  he index P1.  Da
1c7c4 74 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79  ta for the entry
1c7c5 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50   is nil..**.** P
1c7c6 33 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74  3 is a flag that
1c7c7 20 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74   provides a hint
1c7c8 20 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c   to the b-tree l
1c7c9 61 79 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a  ayer that this.*
1c7ca 2a 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65  * insert is like
1c7cb 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
1c7cc 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  nd..**.** This i
1c7cd 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20  nstruction only 
1c7ce 77 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65  works for indice
1c7cf 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c 65  s.  The equivale
1c7d0 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a  nt instruction.*
1c7d1 2a 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20  * for tables is 
1c7d2 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61  OP_Insert..*/.ca
1c7d3 73 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a  se OP_IdxInsert:
1c7d4 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32   {        /* in2
1c7d5 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f   */.  int i = pO
1c7d6 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72  p->p1;.  VdbeCur
1c7d7 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
1c7d8 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73  sor *pCrsr;.  as
1c7d9 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1c7da 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1c7db 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
1c7dc 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  [i]!=0 );.  asse
1c7dd 72 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20  rt( pIn2->flags 
1c7de 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20  & MEM_Blob );.  
1c7df 69 66 28 20 28 70 43 72 73 72 20 3d 20 28 70 43  if( (pCrsr = (pC
1c7e0 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d   = p->apCsr[i])-
1c7e1 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a  >pCursor)!=0 ){.
1c7e2 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
1c7e3 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  isTable==0 );.  
1c7e4 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f    rc = ExpandBlo
1c7e5 62 28 70 49 6e 32 29 3b 0a 20 20 20 20 69 66 28  b(pIn2);.    if(
1c7e6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1c7e7 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79  {.      int nKey
1c7e8 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20   = pIn2->n;.    
1c7e9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b    const char *zK
1c7ea 65 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20  ey = pIn2->z;.  
1c7eb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1c7ec 42 74 72 65 65 49 6e 73 65 72 74 28 70 43 72 73  BtreeInsert(pCrs
1c7ed 72 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 22  r, zKey, nKey, "
1c7ee 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33  ", 0, 0, pOp->p3
1c7ef 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1c7f0 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
1c7f1 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  eto==0 );.      
1c7f2 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1c7f3 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1c7f4 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1c7f5 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1c7f6 49 64 78 44 65 6c 65 74 65 20 50 31 20 50 32 20  IdxDelete P1 P2 
1c7f7 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  P3 * *.**.** The
1c7f8 20 63 6f 6e 74 65 6e 74 20 6f 66 20 50 33 20 72   content of P3 r
1c7f9 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
1c7fa 67 20 61 74 20 72 65 67 69 73 74 65 72 20 50 32  g at register P2
1c7fb 20 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61   form.** an unpa
1c7fc 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20  cked index key. 
1c7fd 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f  This opcode remo
1c7fe 76 65 73 20 74 68 61 74 20 65 6e 74 72 79 20 66  ves that entry f
1c7ff 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65  rom the .** inde
1c800 78 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73  x opened by curs
1c801 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f  or P1..*/.case O
1c802 50 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b 0a 20  P_IdxDelete: {. 
1c803 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31   int i = pOp->p1
1c804 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
1c805 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  pC;.  BtCursor *
1c806 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74 28  pCrsr;.  assert(
1c807 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20   pOp->p3>0 );.  
1c808 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
1c809 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70  0 && pOp->p2+pOp
1c80a 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20  ->p3<=p->nMem+1 
1c80b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  );.  assert( i>=
1c80c 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  0 && i<p->nCurso
1c80d 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1c80e 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b  ->apCsr[i]!=0 );
1c80f 0a 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20  .  if( (pCrsr = 
1c810 28 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69  (pC = p->apCsr[i
1c811 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20  ])->pCursor)!=0 
1c812 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  ){.    int res;.
1c813 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f      UnpackedReco
1c814 72 64 20 72 3b 0a 20 20 20 20 72 2e 70 4b 65 79  rd r;.    r.pKey
1c815 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79 49  Info = pC->pKeyI
1c816 6e 66 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c  nfo;.    r.nFiel
1c817 64 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 33  d = (u16)pOp->p3
1c818 3b 0a 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20  ;.    r.flags = 
1c819 30 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20  0;.    r.aMem = 
1c81a 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1c81b 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ];.    rc = sqli
1c81c 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
1c81d 70 61 63 6b 65 64 28 70 43 72 73 72 2c 20 26 72  packed(pCrsr, &r
1c81e 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20  , 0, 0, &res);. 
1c81f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1c820 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20 29  E_OK && res==0 )
1c821 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1c822 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28  ite3BtreeDelete(
1c823 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20  pCrsr);.    }.  
1c824 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65    assert( pC->de
1c825 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20  ferredMoveto==0 
1c826 29 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65  );.    pC->cache
1c827 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1c828 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61  TALE;.  }.  brea
1c829 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1c82a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 20   IdxRowid P1 P2 
1c82b 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  * * *.**.** Writ
1c82c 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
1c82d 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68  P2 an integer wh
1c82e 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 20  ich is the last 
1c82f 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 63  entry in the rec
1c830 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e  ord at.** the en
1c831 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b  d of the index k
1c832 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ey pointed to by
1c833 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 69   cursor P1.  Thi
1c834 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c 64  s integer should
1c835 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64   be.** the rowid
1c836 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e   of the table en
1c837 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 69  try to which thi
1c838 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 6f  s index entry po
1c839 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ints..**.** See 
1c83a 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b  also: Rowid, Mak
1c83b 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65  eRecord..*/.case
1c83c 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20   OP_IdxRowid: { 
1c83d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c83e 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1c83f 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70  */.  int i = pOp
1c840 2d 3e 70 31 3b 0a 20 20 42 74 43 75 72 73 6f 72  ->p1;.  BtCursor
1c841 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 62 65 43   *pCrsr;.  VdbeC
1c842 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73  ursor *pC;..  as
1c843 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1c844 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1c845 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
1c846 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  [i]!=0 );.  if( 
1c847 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20 70  (pCrsr = (pC = p
1c848 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75  ->apCsr[i])->pCu
1c849 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20  rsor)!=0 ){.    
1c84a 69 36 34 20 72 6f 77 69 64 3b 0a 0a 20 20 20 20  i64 rowid;..    
1c84b 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66 65  assert( pC->defe
1c84c 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b  rredMoveto==0 );
1c84d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1c84e 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  >isTable==0 );. 
1c84f 20 20 20 69 66 28 20 21 70 43 2d 3e 6e 75 6c 6c     if( !pC->null
1c850 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Row ){.      rc 
1c851 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  = sqlite3VdbeIdx
1c852 52 6f 77 69 64 28 70 43 72 73 72 2c 20 26 72 6f  Rowid(pCrsr, &ro
1c853 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  wid);.      if( 
1c854 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1c855 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
1c856 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1c857 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1c858 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1c859 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Out, MEM_Int);. 
1c85a 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d       pOut->u.i =
1c85b 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20   rowid;.    }.  
1c85c 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1c85d 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 20 50   Opcode: IdxGE P
1c85e 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1c85f 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73  .** The P4 regis
1c860 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e  ter values begin
1c861 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72  ning with P3 for
1c862 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e  m an unpacked in
1c863 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74  dex .** key that
1c864 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44   omits the ROWID
1c865 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20  .  Compare this 
1c866 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73  key value agains
1c867 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20  t the index .** 
1c868 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65  that P1 is curre
1c869 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
1c86a 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52  , ignoring the R
1c86b 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69  OWID on the P1 i
1c86c 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ndex..**.** If t
1c86d 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72  he P1 index entr
1c86e 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  y is greater tha
1c86f 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68  n or equal to th
1c870 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20 74  e key value.** t
1c871 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20  hen jump to P2. 
1c872 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20   Otherwise fall 
1c873 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
1c874 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
1c875 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20  .**.** If P5 is 
1c876 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68  non-zero then th
1c877 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69  e key value is i
1c878 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65  ncreased by an e
1c879 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f 72  psilon .** prior
1c87a 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73   to the comparis
1c87b 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 20 74  on.  This make t
1c87c 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c  he opcode work l
1c87d 69 6b 65 20 49 64 78 47 54 20 65 78 63 65 70 74  ike IdxGT except
1c87e 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 68 65 20  .** that if the 
1c87f 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 73 74 65  key from registe
1c880 72 20 50 33 20 69 73 20 61 20 70 72 65 66 69 78  r P3 is a prefix
1c881 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 20 74   of the key in t
1c882 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68  he cursor,.** th
1c883 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73  e result is fals
1c884 65 20 77 68 65 72 65 61 73 20 69 74 20 77 6f 75  e whereas it wou
1c885 6c 64 20 62 65 20 74 72 75 65 20 77 69 74 68 20  ld be true with 
1c886 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  IdxGT..*/./* Opc
1c887 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 20 50 32  ode: IdxLT P1 P2
1c888 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 54   P3 * P5.**.** T
1c889 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20 76  he P4 register v
1c88a 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20  alues beginning 
1c88b 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20  with P3 form an 
1c88c 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a  unpacked index .
1c88d 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74  ** key that omit
1c88e 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f  s the ROWID.  Co
1c88f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76  mpare this key v
1c890 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 65  alue against the
1c891 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20   index .** that 
1c892 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  P1 is currently 
1c893 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e  pointing to, ign
1c894 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20  oring the ROWID 
1c895 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e  on the P1 index.
1c896 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31  .**.** If the P1
1c897 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20   index entry is 
1c898 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65  less than the ke
1c899 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75 6d  y value then jum
1c89a 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68 65  p to P2..** Othe
1c89b 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75  rwise fall throu
1c89c 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  gh to the next i
1c89d 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
1c89e 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a  * If P5 is non-z
1c89f 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79  ero then the key
1c8a0 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61   value is increa
1c8a1 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f  sed by an epsilo
1c8a2 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 74  n prior .** to t
1c8a3 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20  he comparison.  
1c8a4 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20 6f  This makes the o
1c8a5 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20  pcode work like 
1c8a6 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f  IdxLE..*/.case O
1c8a7 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20 20  P_IdxLT:        
1c8a8 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1c8a9 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45 3a  /.case OP_IdxGE:
1c8aa 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1c8ab 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20  p, in3 */.  int 
1c8ac 69 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64  i= pOp->p1;.  Vd
1c8ad 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20  beCursor *pC;.. 
1c8ae 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
1c8af 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b   i<p->nCursor );
1c8b0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70  .  assert( p->ap
1c8b1 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69  Csr[i]!=0 );.  i
1c8b2 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73  f( (pC = p->apCs
1c8b3 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 3d  r[i])->pCursor!=
1c8b4 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73  0 ){.    int res
1c8b5 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65  ;.    UnpackedRe
1c8b6 63 6f 72 64 20 72 3b 0a 20 20 20 20 61 73 73 65  cord r;.    asse
1c8b7 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64  rt( pC->deferred
1c8b8 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20  Moveto==0 );.   
1c8b9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 35   assert( pOp->p5
1c8ba 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d  ==0 || pOp->p5==
1c8bb 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1 );.    assert(
1c8bc 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
1c8bd 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 72 2e  _INT32 );.    r.
1c8be 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70  pKeyInfo = pC->p
1c8bf 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72 2e 6e  KeyInfo;.    r.n
1c8c0 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70  Field = (u16)pOp
1c8c1 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 20  ->p4.i;.    if( 
1c8c2 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 20  pOp->p5 ){.     
1c8c3 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43   r.flags = UNPAC
1c8c4 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c 20 55 4e  KED_INCRKEY | UN
1c8c5 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f  PACKED_IGNORE_RO
1c8c6 57 49 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  WID;.    }else{.
1c8c7 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20        r.flags = 
1c8c8 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f  UNPACKED_IGNORE_
1c8c9 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20  ROWID;.    }.   
1c8ca 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d   r.aMem = &p->aM
1c8cb 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20  em[pOp->p3];.   
1c8cc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1c8cd 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 70  eIdxKeyCompare(p
1c8ce 43 2c 20 26 72 2c 20 26 72 65 73 29 3b 0a 20 20  C, &r, &res);.  
1c8cf 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
1c8d0 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20  e==OP_IdxLT ){. 
1c8d1 20 20 20 20 20 72 65 73 20 3d 20 2d 72 65 73 3b       res = -res;
1c8d2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1c8d3 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f    assert( pOp->o
1c8d4 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47 45 20  pcode==OP_IdxGE 
1c8d5 29 3b 0a 20 20 20 20 20 20 72 65 73 2b 2b 3b 0a  );.      res++;.
1c8d6 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65      }.    if( re
1c8d7 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20  s>0 ){.      pc 
1c8d8 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 3b 0a  = pOp->p2 - 1 ;.
1c8d9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
1c8da 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1c8db 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 20 50   Destroy P1 P2 P
1c8dc 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65  3 * *.**.** Dele
1c8dd 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64 61 74  te an entire dat
1c8de 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69  abase table or i
1c8df 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20  ndex whose root 
1c8e0 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  page in the data
1c8e1 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20  base.** file is 
1c8e2 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a  given by P1..**.
1c8e3 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69  ** The table bei
1c8e4 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69 73 20  ng destroyed is 
1c8e5 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
1c8e6 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 33 3d  base file if P3=
1c8e7 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3d 31  =0.  If.** P3==1
1c8e8 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   then the table 
1c8e9 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69  to be clear is i
1c8ea 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  n the auxiliary 
1c8eb 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a  database file.**
1c8ec 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f   that is used to
1c8ed 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72   store tables cr
1c8ee 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54  eate using CREAT
1c8ef 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c  E TEMPORARY TABL
1c8f0 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f  E..**.** If AUTO
1c8f1 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65  VACUUM is enable
1c8f2 64 20 74 68 65 6e 20 69 74 20 69 73 20 70 6f 73  d then it is pos
1c8f3 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68  sible that anoth
1c8f4 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20  er root page.** 
1c8f5 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 20 69  might be moved i
1c8f6 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 64 65  nto the newly de
1c8f7 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67 65 20  leted root page 
1c8f8 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70  in order to keep
1c8f9 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67   all.** root pag
1c8fa 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 74  es contiguous at
1c8fb 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
1c8fc 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  f the database. 
1c8fd 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a 20 76   The former.** v
1c8fe 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 6f 74  alue of the root
1c8ff 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76 65 64   page that moved
1c900 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62 65 66   - its value bef
1c901 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f 63 63  ore the move occ
1c902 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 73 74  urred -.** is st
1c903 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1c904 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 67 65   P2.  If no page
1c905 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 61   .** movement wa
1c906 73 20 72 65 71 75 69 72 65 64 20 28 62 65 63 61  s required (beca
1c907 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 62 65  use the table be
1c908 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61 73 20  ing dropped was 
1c909 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 65 20  already .** the 
1c90a 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 65 20  last one in the 
1c90b 64 61 74 61 62 61 73 65 29 20 74 68 65 6e 20 61  database) then a
1c90c 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20   zero is stored 
1c90d 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  in register P2..
1c90e 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d  ** If AUTOVACUUM
1c90f 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 68 65   is disabled the
1c910 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72  n a zero is stor
1c911 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1c912 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  2..**.** See als
1c913 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 73 65  o: Clear.*/.case
1c914 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b 20 20   OP_Destroy: {  
1c915 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1c916 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lease */.  int i
1c917 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e  Moved;.  int iCn
1c918 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  t;.#ifndef SQLIT
1c919 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1c91a 42 4c 45 0a 20 20 56 64 62 65 20 2a 70 56 64 62  BLE.  Vdbe *pVdb
1c91b 65 3b 0a 20 20 69 43 6e 74 20 3d 20 30 3b 0a 20  e;.  iCnt = 0;. 
1c91c 20 66 6f 72 28 70 56 64 62 65 3d 64 62 2d 3e 70   for(pVdbe=db->p
1c91d 56 64 62 65 3b 20 70 56 64 62 65 3b 20 70 56 64  Vdbe; pVdbe; pVd
1c91e 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65 78 74 29  be=pVdbe->pNext)
1c91f 7b 0a 20 20 20 20 69 66 28 20 70 56 64 62 65 2d  {.    if( pVdbe-
1c920 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1c921 49 43 5f 52 55 4e 20 26 26 20 70 56 64 62 65 2d  IC_RUN && pVdbe-
1c922 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 3c 32 20  >inVtabMethod<2 
1c923 26 26 20 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20  && pVdbe->pc>=0 
1c924 29 7b 0a 20 20 20 20 20 20 69 43 6e 74 2b 2b 3b  ){.      iCnt++;
1c925 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65  .    }.  }.#else
1c926 0a 20 20 69 43 6e 74 20 3d 20 64 62 2d 3e 61 63  .  iCnt = db->ac
1c927 74 69 76 65 56 64 62 65 43 6e 74 3b 0a 23 65 6e  tiveVdbeCnt;.#en
1c928 64 69 66 0a 20 20 69 66 28 20 69 43 6e 74 3e 31  dif.  if( iCnt>1
1c929 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
1c92a 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20  ITE_LOCKED;.    
1c92b 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d  p->errorAction =
1c92c 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c   OE_Abort;.  }el
1c92d 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20  se{.    int iDb 
1c92e 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 61  = pOp->p3;.    a
1c92f 73 73 65 72 74 28 20 69 43 6e 74 3d 3d 31 20 29  ssert( iCnt==1 )
1c930 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1c931 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1c932 3c 3c 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20  <<iDb))!=0 );.  
1c933 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1c934 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64 62 2d  reeDropTable(db-
1c935 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c 20 70  >aDb[iDb].pBt, p
1c936 4f 70 2d 3e 70 31 2c 20 26 69 4d 6f 76 65 64 29  Op->p1, &iMoved)
1c937 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
1c938 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1c939 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  nt);.    pOut->u
1c93a 2e 69 20 3d 20 69 4d 6f 76 65 64 3b 0a 23 69 66  .i = iMoved;.#if
1c93b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1c93c 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
1c93d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1c93e 4b 20 26 26 20 69 4d 6f 76 65 64 21 3d 30 20 29  K && iMoved!=0 )
1c93f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
1c940 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62  ootPageMoved(&db
1c941 2d 3e 61 44 62 5b 69 44 62 5d 2c 20 69 4d 6f 76  ->aDb[iDb], iMov
1c942 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20  ed, pOp->p1);.  
1c943 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
1c944 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1c945 63 6f 64 65 3a 20 43 6c 65 61 72 20 50 31 20 50  code: Clear P1 P
1c946 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74  2 P3.**.** Delet
1c947 65 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 73 20 6f  e all contents o
1c948 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
1c949 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68  able or index wh
1c94a 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a  ose root page.**
1c94b 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1c94c 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62   file is given b
1c94d 79 20 50 31 2e 20 20 42 75 74 2c 20 75 6e 6c 69  y P1.  But, unli
1c94e 6b 65 20 44 65 73 74 72 6f 79 2c 20 64 6f 20 6e  ke Destroy, do n
1c94f 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65  ot.** remove the
1c950 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1c951 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1c952 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
1c953 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c  e table being cl
1c954 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 6d 61  ear is in the ma
1c955 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
1c956 20 69 66 20 50 32 3d 3d 30 2e 20 20 49 66 0a 2a   if P2==0.  If.*
1c957 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 74 68 65  * P2==1 then the
1c958 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65   table to be cle
1c959 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78  ar is in the aux
1c95a 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
1c95b 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20  file.** that is 
1c95c 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61  used to store ta
1c95d 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 69 6e  bles create usin
1c95e 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41  g CREATE TEMPORA
1c95f 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20  RY TABLE..**.** 
1c960 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20  If the P3 value 
1c961 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1c962 6e 20 74 68 65 20 74 61 62 6c 65 20 72 65 66 65  n the table refe
1c963 72 72 65 64 20 74 6f 20 6d 75 73 74 20 62 65 20  rred to must be 
1c964 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 74 61 62  an.** intkey tab
1c965 6c 65 20 28 61 6e 20 53 51 4c 20 74 61 62 6c 65  le (an SQL table
1c966 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 65 78 29 2e  , not an index).
1c967 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
1c968 65 20 72 6f 77 20 63 68 61 6e 67 65 20 0a 2a 2a  e row change .**
1c969 20 63 6f 75 6e 74 20 69 73 20 69 6e 63 72 65 6d   count is increm
1c96a 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  ented by the num
1c96b 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74  ber of rows in t
1c96c 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63  he table being c
1c96d 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 49 66 20 50  leared. .** If P
1c96e 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  3 is greater tha
1c96f 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n zero, then the
1c970 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
1c971 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 0a   register P3 is.
1c972 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 65 6d 65 6e  ** also incremen
1c973 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65  ted by the numbe
1c974 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  r of rows in the
1c975 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65   table being cle
1c976 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ared..**.** See 
1c977 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79 0a 2a 2f  also: Destroy.*/
1c978 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61 72 3a 20  .case OP_Clear: 
1c979 7b 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 20  {.  int nChange 
1c97a 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28  = 0;.  assert( (
1c97b 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1c97c 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30 20  1<<pOp->p2))!=0 
1c97d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1c97e 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
1c97f 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b  (.      db->aDb[
1c980 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70 4f  pOp->p2].pBt, pO
1c981 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33 20  p->p1, (pOp->p3 
1c982 3f 20 26 6e 43 68 61 6e 67 65 20 3a 20 30 29 0a  ? &nChange : 0).
1c983 20 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e    );.  if( pOp->
1c984 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 68  p3 ){.    p->nCh
1c985 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e 67 65 3b  ange += nChange;
1c986 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33  .    if( pOp->p3
1c987 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  >0 ){.      p->a
1c988 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69  Mem[pOp->p3].u.i
1c989 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20   += nChange;.   
1c98a 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1c98b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72  }../* Opcode: Cr
1c98c 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 32 20  eateTable P1 P2 
1c98d 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f  * * *.**.** Allo
1c98e 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 6c 65  cate a new table
1c98f 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
1c990 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31  abase file if P1
1c991 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a  ==0 or in the.**
1c992 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
1c993 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d  ase file if P1==
1c994 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 61 63  1 or in an attac
1c995 68 65 64 20 64 61 74 61 62 61 73 65 20 69 66 0a  hed database if.
1c996 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 65 20  ** P1>1.  Write 
1c997 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
1c998 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20  mber of the new 
1c999 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65  table into.** re
1c99a 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a 20  gister P2.**.** 
1c99b 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  The difference b
1c99c 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 20 61  etween a table a
1c99d 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74  nd an index is t
1c99e 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 6d 75  his:  A table mu
1c99f 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d 62  st.** have a 4-b
1c9a0 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20  yte integer key 
1c9a1 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 72 62  and can have arb
1c9a2 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 41 6e  itrary data.  An
1c9a3 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e   index.** has an
1c9a4 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 20 62   arbitrary key b
1c9a5 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a  ut no data..**.*
1c9a6 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 65 61  * See also: Crea
1c9a7 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70  teIndex.*/./* Op
1c9a8 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e 64 65  code: CreateInde
1c9a9 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  x P1 P2 * * *.**
1c9aa 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
1c9ab 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20  ew index in the 
1c9ac 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1c9ad 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 20 69  le if P1==0 or i
1c9ae 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61  n the.** auxilia
1c9af 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ry database file
1c9b0 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20   if P1==1 or in 
1c9b1 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61  an attached data
1c9b2 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e  base if.** P1>1.
1c9b3 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 6f 74    Write the root
1c9b4 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
1c9b5 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e  the new table in
1c9b6 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  to.** register P
1c9b7 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f 63  2..**.** See doc
1c9b8 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50  umentation on OP
1c9b9 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 6f 72  _CreateTable for
1c9ba 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
1c9bb 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  rmation..*/.case
1c9bc 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 3a   OP_CreateIndex:
1c9bd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f              /* o
1c9be 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1c9bf 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65  /.case OP_Create
1c9c0 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20 20  Table: {        
1c9c1 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1c9c2 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 70 67  ease */.  int pg
1c9c3 6e 6f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 66 6c  no = 0;.  int fl
1c9c4 61 67 73 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a  ags;.  Db *pDb;.
1c9c5 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c9c6 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1c9c7 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
1c9c8 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
1c9c9 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31  sk & (1<<pOp->p1
1c9ca 29 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d  ))!=0 );.  pDb =
1c9cb 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70   &db->aDb[pOp->p
1c9cc 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44  1];.  assert( pD
1c9cd 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69  b->pBt!=0 );.  i
1c9ce 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1c9cf 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29  OP_CreateTable )
1c9d0 7b 0a 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d  {.    /* flags =
1c9d1 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20 2a   BTREE_INTKEY; *
1c9d2 2f 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 42 54  /.    flags = BT
1c9d3 52 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52  REE_LEAFDATA|BTR
1c9d4 45 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c  EE_INTKEY;.  }el
1c9d5 73 65 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d 20  se{.    flags = 
1c9d6 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b 0a  BTREE_ZERODATA;.
1c9d7 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
1c9d8 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62  e3BtreeCreateTab
1c9d9 6c 65 28 70 44 62 2d 3e 70 42 74 2c 20 26 70 67  le(pDb->pBt, &pg
1c9da 6e 6f 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4f  no, flags);.  pO
1c9db 75 74 2d 3e 75 2e 69 20 3d 20 70 67 6e 6f 3b 0a  ut->u.i = pgno;.
1c9dc 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1c9dd 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1c9de 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1c9df 4f 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63 68  Opcode: ParseSch
1c9e0 65 6d 61 20 50 31 20 50 32 20 2a 20 50 34 20 2a  ema P1 P2 * P4 *
1c9e1 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64 20  .**.** Read and 
1c9e2 70 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69 65  parse all entrie
1c9e3 73 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54  s from the SQLIT
1c9e4 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 6f  E_MASTER table o
1c9e5 66 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a 2a  f database P1.**
1c9e6 20 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 20   that match the 
1c9e7 57 48 45 52 45 20 63 6c 61 75 73 65 20 50 34 2e  WHERE clause P4.
1c9e8 20 20 50 32 20 69 73 20 74 68 65 20 22 66 6f 72    P2 is the "for
1c9e9 63 65 22 20 66 6c 61 67 2e 20 20 20 41 6c 77 61  ce" flag.   Alwa
1c9ea 79 73 20 64 6f 0a 2a 2a 20 74 68 65 20 70 61 72  ys do.** the par
1c9eb 73 69 6e 67 20 69 66 20 50 32 20 69 73 20 74 72  sing if P2 is tr
1c9ec 75 65 2e 20 20 49 66 20 50 32 20 69 73 20 66 61  ue.  If P2 is fa
1c9ed 6c 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72  lse, then this r
1c9ee 6f 75 74 69 6e 65 20 69 73 20 61 0a 2a 2a 20 6e  outine is a.** n
1c9ef 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 63 68 65  o-op if the sche
1c9f0 6d 61 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  ma is not curren
1c9f1 74 6c 79 20 6c 6f 61 64 65 64 2e 20 20 49 6e 20  tly loaded.  In 
1c9f2 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
1c9f3 50 32 0a 2a 2a 20 69 73 20 66 61 6c 73 65 2c 20  P2.** is false, 
1c9f4 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45  the SQLITE_MASTE
1c9f5 52 20 74 61 62 6c 65 20 69 73 20 6f 6e 6c 79 20  R table is only 
1c9f6 70 61 72 73 65 64 20 69 66 20 74 68 65 20 72 65  parsed if the re
1c9f7 73 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68  st of the.** sch
1c9f8 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 6c  ema is already l
1c9f9 6f 61 64 65 64 20 69 6e 74 6f 20 74 68 65 20 73  oaded into the s
1c9fa 79 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  ymbol table..**.
1c9fb 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
1c9fc 6e 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73 65  nvokes the parse
1c9fd 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  r to create a ne
1c9fe 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  w virtual machin
1c9ff 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73 20  e,.** then runs 
1ca00 74 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20  the new virtual 
1ca01 6d 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73 20  machine.  It is 
1ca02 74 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61 6e  thus a re-entran
1ca03 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73  t opcode..*/.cas
1ca04 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  e OP_ParseSchema
1ca05 3a 20 7b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20  : {.  int iDb = 
1ca06 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72  pOp->p1;.  asser
1ca07 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
1ca08 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f  <db->nDb );..  /
1ca09 2a 20 49 66 20 70 4f 70 2d 3e 70 32 20 69 73 20  * If pOp->p2 is 
1ca0a 30 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63  0, then this opc
1ca0b 6f 64 65 20 69 73 20 62 65 69 6e 67 20 65 78 65  ode is being exe
1ca0c 63 75 74 65 64 20 74 6f 20 72 65 61 64 20 61 0a  cuted to read a.
1ca0d 20 20 2a 2a 20 73 69 6e 67 6c 65 20 72 6f 77 2c    ** single row,
1ca0e 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65   for example the
1ca0f 20 72 6f 77 20 63 6f 72 72 65 73 70 6f 6e 64 69   row correspondi
1ca10 6e 67 20 74 6f 20 61 20 6e 65 77 20 69 6e 64 65  ng to a new inde
1ca11 78 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62  x.  ** created b
1ca12 79 20 74 68 69 73 20 56 44 42 45 2c 20 66 72 6f  y this VDBE, fro
1ca13 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  m the sqlite_mas
1ca14 74 65 72 20 74 61 62 6c 65 2e 20 49 74 20 6f 6e  ter table. It on
1ca15 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 20 74 68 69  ly.  ** does thi
1ca16 73 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70  s if the corresp
1ca17 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79  onding in-memory
1ca18 20 73 63 68 65 6d 61 20 69 73 20 63 75 72 72 65   schema is curre
1ca19 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61 64 65 64  ntly.  ** loaded
1ca1a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  . Otherwise, the
1ca1b 20 6e 65 77 20 69 6e 64 65 78 20 64 65 66 69 6e   new index defin
1ca1c 69 74 69 6f 6e 20 63 61 6e 20 62 65 20 6c 6f 61  ition can be loa
1ca1d 64 65 64 20 61 6c 6f 6e 67 0a 20 20 2a 2a 20 77  ded along.  ** w
1ca1e 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20  ith the rest of 
1ca1f 74 68 65 20 73 63 68 65 6d 61 20 77 68 65 6e 20  the schema when 
1ca20 69 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  it is required..
1ca21 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74 68 6f 75    **.  ** Althou
1ca22 67 68 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20  gh the mutex on 
1ca23 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
1ca24 65 63 74 20 74 68 61 74 20 63 6f 72 72 65 73 70  ect that corresp
1ca25 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a 20 64 61 74  onds to.  ** dat
1ca26 61 62 61 73 65 20 69 44 62 20 28 74 68 65 20 64  abase iDb (the d
1ca27 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
1ca28 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  ng the sqlite_ma
1ca29 73 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20  ster table.  ** 
1ca2a 72 65 61 64 20 62 79 20 74 68 69 73 20 69 6e 73  read by this ins
1ca2b 74 72 75 63 74 69 6f 6e 29 20 69 73 20 63 75 72  truction) is cur
1ca2c 72 65 6e 74 6c 79 20 68 65 6c 64 2c 20 69 74 20  rently held, it 
1ca2d 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a  is necessary to.
1ca2e 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20    ** obtain the 
1ca2f 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20 61  mutexes on all a
1ca30 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1ca31 73 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69 6e  s before checkin
1ca32 67 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 73 63  g if.  ** the sc
1ca33 68 65 6d 61 20 6f 66 20 69 44 62 20 69 73 20 6c  hema of iDb is l
1ca34 6f 61 64 65 64 2e 20 54 68 69 73 20 69 73 20 62  oaded. This is b
1ca35 65 63 61 75 73 65 2c 20 61 74 20 74 68 65 20 73  ecause, at the s
1ca36 74 61 72 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65  tart of.  ** the
1ca37 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
1ca38 63 61 6c 6c 20 62 65 6c 6f 77 2c 20 53 51 4c 69  call below, SQLi
1ca39 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a  te will invoke .
1ca3a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65    ** sqlite3Btre
1ca3b 65 45 6e 74 65 72 41 6c 6c 28 29 2e 20 49 66 20  eEnterAll(). If 
1ca3c 61 6c 6c 20 6d 75 74 65 78 65 73 20 61 72 65 20  all mutexes are 
1ca3d 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64  not already held
1ca3e 2c 20 74 68 65 0a 20 20 2a 2a 20 69 44 62 20 6d  , the.  ** iDb m
1ca3f 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 6d 70  utex may be temp
1ca40 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 65 64  orarily released
1ca41 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f   to avoid deadlo
1ca42 63 6b 2e 20 49 66 20 0a 20 20 2a 2a 20 74 68 69  ck. If .  ** thi
1ca43 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20  s happens, then 
1ca44 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61  some other threa
1ca45 64 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 65  d may delete the
1ca46 20 69 6e 2d 6d 65 6d 6f 72 79 20 0a 20 20 2a 2a   in-memory .  **
1ca47 20 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62   schema of datab
1ca48 61 73 65 20 69 44 62 20 62 65 66 6f 72 65 20 74  ase iDb before t
1ca49 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1ca4a 20 72 75 6e 73 2e 20 54 68 65 20 73 63 68 65 6d   runs. The schem
1ca4b 61 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  a.  ** will not 
1ca4c 62 65 20 72 65 6c 6f 61 64 65 64 20 62 65 63 75  be reloaded becu
1ca4d 61 73 65 20 74 68 65 20 64 62 2d 3e 69 6e 69 74  ase the db->init
1ca4e 2e 62 75 73 79 20 66 6c 61 67 20 69 73 20 73 65  .busy flag is se
1ca4f 74 2e 20 54 68 69 73 0a 20 20 2a 2a 20 63 61 6e  t. This.  ** can
1ca50 20 72 65 73 75 6c 74 20 69 6e 20 61 20 22 6e 6f   result in a "no
1ca51 20 73 75 63 68 20 74 61 62 6c 65 3a 20 73 71 6c   such table: sql
1ca52 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22  ite_master" or "
1ca53 6d 61 6c 66 6f 72 6d 65 64 0a 20 20 2a 2a 20 64  malformed.  ** d
1ca54 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 22 20  atabase schema" 
1ca55 65 72 72 6f 72 20 62 65 69 6e 67 20 72 65 74 75  error being retu
1ca56 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72  rned to the user
1ca57 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
1ca58 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
1ca59 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b  dsMutex(db->aDb[
1ca5a 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 20 20 73  iDb].pBt) );.  s
1ca5b 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1ca5c 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20 70  All(db);.  if( p
1ca5d 4f 70 2d 3e 70 32 20 7c 7c 20 44 62 48 61 73 50  Op->p2 || DbHasP
1ca5e 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c  roperty(db, iDb,
1ca5f 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
1ca60 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ) ){.    const c
1ca61 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 53  har *zMaster = S
1ca62 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
1ca63 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c  ;.    char *zSql
1ca64 3b 0a 20 20 20 20 49 6e 69 74 44 61 74 61 20 69  ;.    InitData i
1ca65 6e 69 74 44 61 74 61 3b 0a 20 20 20 20 69 6e 69  nitData;.    ini
1ca66 74 44 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a 20  tData.db = db;. 
1ca67 20 20 20 69 6e 69 74 44 61 74 61 2e 69 44 62 20     initData.iDb 
1ca68 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 69  = pOp->p1;.    i
1ca69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67  nitData.pzErrMsg
1ca6a 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0a   = &p->zErrMsg;.
1ca6b 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74      zSql = sqlit
1ca6c 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20  e3MPrintf(db,.  
1ca6d 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d       "SELECT nam
1ca6e 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
1ca6f 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20 57 48   FROM '%q'.%s WH
1ca70 45 52 45 20 25 73 22 2c 0a 20 20 20 20 20 20 20  ERE %s",.       
1ca71 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
1ca72 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4f 70  me, zMaster, pOp
1ca73 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 69 66 28  ->p4.z);.    if(
1ca74 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20   zSql==0 ){.    
1ca75 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
1ca76 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  MEM;.    }else{.
1ca77 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69        (void)sqli
1ca78 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1ca79 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ca7a 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30  db->init.busy==0
1ca7b 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e   );.      db->in
1ca7c 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20 20  it.busy = 1;.   
1ca7d 20 20 20 69 6e 69 74 44 61 74 61 2e 72 63 20 3d     initData.rc =
1ca7e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
1ca7f 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
1ca80 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
1ca81 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1ca82 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c  3_exec(db, zSql,
1ca83 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c   sqlite3InitCall
1ca84 62 61 63 6b 2c 20 26 69 6e 69 74 44 61 74 61 2c  back, &initData,
1ca85 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
1ca86 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c==SQLITE_OK ) r
1ca87 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b  c = initData.rc;
1ca88 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1ca89 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a  Free(db, zSql);.
1ca8a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62        db->init.b
1ca8b 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 28  usy = 0;.      (
1ca8c 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
1ca8d 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d 0a  tyOn(db);.    }.
1ca8e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
1ca8f 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
1ca90 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1ca91 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f  _NOMEM ){.    go
1ca92 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
1ca93 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 0a 23 69 66   break;  .}..#if
1ca94 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1ca95 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 26  _OMIT_ANALYZE) &
1ca96 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
1ca97 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 29 0a 2f  E_OMIT_PARSER)./
1ca98 2a 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e  * Opcode: LoadAn
1ca99 61 6c 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20  alysis P1 * * * 
1ca9a 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65  *.**.** Read the
1ca9b 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
1ca9c 62 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65  ble for database
1ca9d 20 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65   P1 and load the
1ca9e 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74   content.** of t
1ca9f 68 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74  hat table into t
1caa0 68 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65  he internal inde
1caa1 78 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  x hash table.  T
1caa2 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a  his will cause.*
1caa3 2a 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74  * the analysis t
1caa4 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70  o be used when p
1caa5 72 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62  reparing all sub
1caa6 73 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e  sequent queries.
1caa7 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64  .*/.case OP_Load
1caa8 41 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 20 69 6e  Analysis: {.  in
1caa9 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b  t iDb = pOp->p1;
1caaa 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
1caab 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
1caac 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
1caad 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64  e3AnalysisLoad(d
1caae 62 2c 20 69 44 62 29 3b 0a 20 20 62 72 65 61 6b  b, iDb);.  break
1caaf 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;  .}.#endif /* 
1cab0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1cab1 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 26 26  OMIT_ANALYZE) &&
1cab2 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1cab3 5f 4f 4d 49 54 5f 50 41 52 53 45 52 29 20 20 2a  _OMIT_PARSER)  *
1cab4 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72  /../* Opcode: Dr
1cab5 6f 70 54 61 62 6c 65 20 50 31 20 2a 20 2a 20 50  opTable P1 * * P
1cab6 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65  4 *.**.** Remove
1cab7 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69   the internal (i
1cab8 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73  n-memory) data s
1cab9 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64  tructures that d
1caba 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 74  escribe.** the t
1cabb 61 62 6c 65 20 6e 61 6d 65 64 20 50 34 20 69 6e  able named P4 in
1cabc 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 54   database P1.  T
1cabd 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66  his is called af
1cabe 74 65 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20 69  ter a table.** i
1cabf 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64  s dropped in ord
1cac0 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69  er to keep the i
1cac1 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e  nternal represen
1cac2 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  tation of the.**
1cac3 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65   schema consiste
1cac4 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20  nt with what is 
1cac5 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65  on disk..*/.case
1cac6 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20 7b   OP_DropTable: {
1cac7 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  .  sqlite3Unlink
1cac8 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 64  AndDeleteTable(d
1cac9 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d  b, pOp->p1, pOp-
1caca 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b  >p4.z);.  break;
1cacb 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44  .}../* Opcode: D
1cacc 72 6f 70 49 6e 64 65 78 20 50 31 20 2a 20 2a 20  ropIndex P1 * * 
1cacd 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76  P4 *.**.** Remov
1cace 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28  e the internal (
1cacf 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20  in-memory) data 
1cad0 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
1cad1 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20  describe.** the 
1cad2 69 6e 64 65 78 20 6e 61 6d 65 64 20 50 34 20 69  index named P4 i
1cad3 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20  n database P1.  
1cad4 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61  This is called a
1cad5 66 74 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a 2a  fter an index.**
1cad6 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f   is dropped in o
1cad7 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65  rder to keep the
1cad8 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73   internal repres
1cad9 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  entation of the.
1cada 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73  ** schema consis
1cadb 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 69  tent with what i
1cadc 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61  s on disk..*/.ca
1cadd 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 3a  se OP_DropIndex:
1cade 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69   {.  sqlite3Unli
1cadf 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78  nkAndDeleteIndex
1cae0 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  (db, pOp->p1, pO
1cae1 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61  p->p4.z);.  brea
1cae2 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1cae3 20 44 72 6f 70 54 72 69 67 67 65 72 20 50 31 20   DropTrigger P1 
1cae4 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52  * * P4 *.**.** R
1cae5 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e  emove the intern
1cae6 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64  al (in-memory) d
1cae7 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74  ata structures t
1cae8 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20  hat describe.** 
1cae9 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65  the trigger name
1caea 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65  d P4 in database
1caeb 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61   P1.  This is ca
1caec 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 72 69  lled after a tri
1caed 67 67 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70 70  gger.** is dropp
1caee 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  ed in order to k
1caef 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  eep the internal
1caf0 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1caf1 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61  of the.** schema
1caf2 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68   consistent with
1caf3 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b   what is on disk
1caf4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f  ..*/.case OP_Dro
1caf5 70 54 72 69 67 67 65 72 3a 20 7b 0a 20 20 73 71  pTrigger: {.  sq
1caf6 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
1caf7 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20  leteTrigger(db, 
1caf8 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34  pOp->p1, pOp->p4
1caf9 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .z);.  break;.}.
1cafa 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1cafb 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
1cafc 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a  CHECK./* Opcode:
1cafd 20 49 6e 74 65 67 72 69 74 79 43 6b 20 50 31 20   IntegrityCk P1 
1cafe 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a  P2 P3 * P5.**.**
1caff 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20   Do an analysis 
1cb00 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79  of the currently
1cb01 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20   open database. 
1cb02 20 53 74 6f 72 65 20 69 6e 0a 2a 2a 20 72 65 67   Store in.** reg
1cb03 69 73 74 65 72 20 50 31 20 74 68 65 20 74 65 78  ister P1 the tex
1cb04 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65  t of an error me
1cb05 73 73 61 67 65 20 64 65 73 63 72 69 62 69 6e 67  ssage describing
1cb06 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a   any problems..*
1cb07 2a 20 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73  * If no problems
1cb08 20 61 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f 72   are found, stor
1cb09 65 20 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67 69  e a NULL in regi
1cb0a 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54  ster P1..**.** T
1cb0b 68 65 20 72 65 67 69 73 74 65 72 20 50 33 20 63  he register P3 c
1cb0c 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 78 69  ontains the maxi
1cb0d 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c  mum number of al
1cb0e 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e 0a 2a 2a  lowed errors..**
1cb0f 20 41 74 20 6d 6f 73 74 20 72 65 67 28 50 33 29   At most reg(P3)
1cb10 20 65 72 72 6f 72 73 20 77 69 6c 6c 20 62 65 20   errors will be 
1cb11 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e 20  reported..** In 
1cb12 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
1cb13 20 61 6e 61 6c 79 73 69 73 20 73 74 6f 70 73 20   analysis stops 
1cb14 61 73 20 73 6f 6f 6e 20 61 73 20 72 65 67 28 50  as soon as reg(P
1cb15 31 29 20 65 72 72 6f 72 73 20 61 72 65 20 0a 2a  1) errors are .*
1cb16 2a 20 73 65 65 6e 2e 20 20 52 65 67 28 50 31 29  * seen.  Reg(P1)
1cb17 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68   is updated with
1cb18 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1cb19 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 2e  rrors remaining.
1cb1a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20  .**.** The root 
1cb1b 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6f 66 20  page numbers of 
1cb1c 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68  all tables in th
1cb1d 65 20 64 61 74 61 62 61 73 65 20 61 72 65 20 69  e database are i
1cb1e 6e 74 65 67 65 72 0a 2a 2a 20 73 74 6f 72 65 64  nteger.** stored
1cb1f 20 69 6e 20 72 65 67 28 50 31 29 2c 20 72 65 67   in reg(P1), reg
1cb20 28 50 31 2b 31 29 2c 20 72 65 67 28 50 31 2b 32  (P1+1), reg(P1+2
1cb21 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 72 65 20 61  ), ....  There a
1cb22 72 65 20 50 32 20 74 61 62 6c 65 73 0a 2a 2a 20  re P2 tables.** 
1cb23 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  total..**.** If 
1cb24 50 35 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20  P5 is not zero, 
1cb25 74 68 65 20 63 68 65 63 6b 20 69 73 20 64 6f 6e  the check is don
1cb26 65 20 6f 6e 20 74 68 65 20 61 75 78 69 6c 69 61  e on the auxilia
1cb27 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66  ry database.** f
1cb28 69 6c 65 2c 20 6e 6f 74 20 74 68 65 20 6d 61 69  ile, not the mai
1cb29 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
1cb2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1cb2b 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d  de is used to im
1cb2c 70 6c 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 65  plement the inte
1cb2d 67 72 69 74 79 5f 63 68 65 63 6b 20 70 72 61 67  grity_check prag
1cb2e 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  ma..*/.case OP_I
1cb2f 6e 74 65 67 72 69 74 79 43 6b 3a 20 7b 0a 20 20  ntegrityCk: {.  
1cb30 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20  int nRoot;      
1cb31 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62  /* Number of tab
1cb32 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 20 28  les to check.  (
1cb33 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20 70  Number of root p
1cb34 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 6e 74 20  ages.) */.  int 
1cb35 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41  *aRoot;     /* A
1cb36 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65  rray of rootpage
1cb37 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62   numbers for tab
1cb38 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65  les to be checke
1cb39 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20  d */.  int j;   
1cb3a 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
1cb3b 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
1cb3c 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e  nErr;       /* N
1cb3d 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
1cb3e 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 63 68  reported */.  ch
1cb3f 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a  ar *z;        /*
1cb40 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 72 72   Text of the err
1cb41 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20 4d  or report */.  M
1cb42 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f  em *pnErr;     /
1cb43 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70 69  * Register keepi
1cb44 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f  ng track of erro
1cb45 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a  rs remaining */.
1cb46 20 20 0a 20 20 6e 52 6f 6f 74 20 3d 20 70 4f 70    .  nRoot = pOp
1cb47 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p2;.  assert( 
1cb48 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20 61 52 6f  nRoot>0 );.  aRo
1cb49 6f 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ot = sqlite3DbMa
1cb4a 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
1cb4b 6f 66 28 69 6e 74 29 2a 28 6e 52 6f 6f 74 2b 31  of(int)*(nRoot+1
1cb4c 29 20 29 3b 0a 20 20 69 66 28 20 61 52 6f 6f 74  ) );.  if( aRoot
1cb4d 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
1cb4e 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  m;.  assert( pOp
1cb4f 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
1cb50 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1cb51 70 6e 45 72 72 20 3d 20 26 70 2d 3e 61 4d 65 6d  pnErr = &p->aMem
1cb52 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73  [pOp->p3];.  ass
1cb53 65 72 74 28 20 28 70 6e 45 72 72 2d 3e 66 6c 61  ert( (pnErr->fla
1cb54 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30  gs & MEM_Int)!=0
1cb55 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
1cb56 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 28 4d  nErr->flags & (M
1cb57 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
1cb58 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 20 3d  )==0 );.  pIn1 =
1cb59 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1cb5a 31 5d 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  1];.  for(j=0; j
1cb5b 3c 6e 52 6f 6f 74 3b 20 6a 2b 2b 29 7b 0a 20 20  <nRoot; j++){.  
1cb5c 20 20 61 52 6f 6f 74 5b 6a 5d 20 3d 20 28 69 6e    aRoot[j] = (in
1cb5d 74 29 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  t)sqlite3VdbeInt
1cb5e 56 61 6c 75 65 28 26 70 49 6e 31 5b 6a 5d 29 3b  Value(&pIn1[j]);
1cb5f 0a 20 20 7d 0a 20 20 61 52 6f 6f 74 5b 6a 5d 20  .  }.  aRoot[j] 
1cb60 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  = 0;.  assert( p
1cb61 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62 20 29  Op->p5<db->nDb )
1cb62 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
1cb63 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c  btreeMask & (1<<
1cb64 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20 29 3b 0a  pOp->p5))!=0 );.
1cb65 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72    z = sqlite3Btr
1cb66 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b  eeIntegrityCheck
1cb67 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35  (db->aDb[pOp->p5
1cb68 5d 2e 70 42 74 2c 20 61 52 6f 6f 74 2c 20 6e 52  ].pBt, aRoot, nR
1cb69 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  oot,.           
1cb6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cb6b 20 20 20 20 20 20 28 69 6e 74 29 70 6e 45 72 72        (int)pnErr
1cb6c 2d 3e 75 2e 69 2c 20 26 6e 45 72 72 29 3b 0a 20  ->u.i, &nErr);. 
1cb6d 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1cb6e 62 2c 20 61 52 6f 6f 74 29 3b 0a 20 20 70 6e 45  b, aRoot);.  pnE
1cb6f 72 72 2d 3e 75 2e 69 20 2d 3d 20 6e 45 72 72 3b  rr->u.i -= nErr;
1cb70 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1cb71 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a  mSetNull(pIn1);.
1cb72 20 20 69 66 28 20 6e 45 72 72 3d 3d 30 20 29 7b    if( nErr==0 ){
1cb73 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 3d 3d  .    assert( z==
1cb74 30 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  0 );.  }else if(
1cb75 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74   z==0 ){.    got
1cb76 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65 6c 73  o no_mem;.  }els
1cb77 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  e{.    sqlite3Vd
1cb78 62 65 4d 65 6d 53 65 74 53 74 72 28 70 49 6e 31  beMemSetStr(pIn1
1cb79 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  , z, -1, SQLITE_
1cb7a 55 54 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 72  UTF8, sqlite3_fr
1cb7b 65 65 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54  ee);.  }.  UPDAT
1cb7c 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1cb7d 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  In1);.  sqlite3V
1cb7e 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
1cb7f 67 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67  g(pIn1, encoding
1cb80 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  );.  break;.}.#e
1cb81 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1cb82 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
1cb83 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  ECK */../* Opcod
1cb84 65 3a 20 52 6f 77 53 65 74 41 64 64 20 50 31 20  e: RowSetAdd P1 
1cb85 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49  P2 * * *.**.** I
1cb86 6e 73 65 72 74 20 74 68 65 20 69 6e 74 65 67 65  nsert the intege
1cb87 72 20 76 61 6c 75 65 20 68 65 6c 64 20 62 79 20  r value held by 
1cb88 72 65 67 69 73 74 65 72 20 50 32 20 69 6e 74 6f  register P2 into
1cb89 20 61 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78   a boolean index
1cb8a 0a 2a 2a 20 68 65 6c 64 20 69 6e 20 72 65 67 69  .** held in regi
1cb8b 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 41  ster P1..**.** A
1cb8c 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c  n assertion fail
1cb8d 73 20 69 66 20 50 32 20 69 73 20 6e 6f 74 20 61  s if P2 is not a
1cb8e 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61  n integer..*/.ca
1cb8f 73 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a  se OP_RowSetAdd:
1cb90 20 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20   {       /* in2 
1cb91 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a  */.  Mem *pIdx;.
1cb92 20 20 4d 65 6d 20 2a 70 56 61 6c 3b 0a 20 20 61    Mem *pVal;.  a
1cb93 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30  ssert( pOp->p1>0
1cb94 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e   && pOp->p1<=p->
1cb95 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 64 78 20 3d  nMem );.  pIdx =
1cb96 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1cb97 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  1];.  assert( pO
1cb98 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p2>0 && pOp->
1cb99 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p2<=p->nMem );. 
1cb9a 20 70 56 61 6c 20 3d 20 26 70 2d 3e 61 4d 65 6d   pVal = &p->aMem
1cb9b 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73  [pOp->p2];.  ass
1cb9c 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67  ert( (pVal->flag
1cb9d 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20  s & MEM_Int)!=0 
1cb9e 29 3b 0a 20 20 69 66 28 20 28 70 49 64 78 2d 3e  );.  if( (pIdx->
1cb9f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
1cba0 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  et)==0 ){.    sq
1cba1 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52  lite3VdbeMemSetR
1cba2 6f 77 53 65 74 28 70 49 64 78 29 3b 0a 20 20 20  owSet(pIdx);.   
1cba3 20 69 66 28 20 28 70 49 64 78 2d 3e 66 6c 61 67   if( (pIdx->flag
1cba4 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
1cba5 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
1cba6 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  ;.  }.  sqlite3R
1cba7 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49 64 78  owSetInsert(pIdx
1cba8 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70 56 61  ->u.pRowSet, pVa
1cba9 6c 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 6b  l->u.i);.  break
1cbaa 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cbab 52 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 32  RowSetRead P1 P2
1cbac 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 78   P3 * *.**.** Ex
1cbad 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65  tract the smalle
1cbae 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 6f  st value from bo
1cbaf 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 61  olean index P1 a
1cbb0 6e 64 20 70 75 74 20 74 68 61 74 20 76 61 6c 75  nd put that valu
1cbb1 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74  e into.** regist
1cbb2 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 62  er P3.  Or, if b
1cbb3 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20  oolean index P1 
1cbb4 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70  is initially emp
1cbb5 74 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a 20  ty, leave P3.** 
1cbb6 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75  unchanged and ju
1cbb7 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  mp to instructio
1cbb8 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  n P2..*/.case OP
1cbb9 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20  _RowSetRead: {  
1cbba 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 6f 75       /* jump, ou
1cbbb 74 33 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64  t3 */.  Mem *pId
1cbbc 78 3b 0a 20 20 69 36 34 20 76 61 6c 3b 0a 20 20  x;.  i64 val;.  
1cbbd 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1cbbe 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  0 && pOp->p1<=p-
1cbbf 3e 6e 4d 65 6d 20 29 3b 0a 20 20 43 48 45 43 4b  >nMem );.  CHECK
1cbc0 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
1cbc1 20 20 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65    pIdx = &p->aMe
1cbc2 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f  m[pOp->p1];.  pO
1cbc3 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1cbc4 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 70  p->p3];.  if( (p
1cbc5 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Idx->flags & MEM
1cbc6 5f 52 6f 77 53 65 74 29 3d 3d 30 20 0a 20 20 20  _RowSet)==0 .   
1cbc7 7c 7c 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  || sqlite3RowSet
1cbc8 4e 65 78 74 28 70 49 64 78 2d 3e 75 2e 70 52 6f  Next(pIdx->u.pRo
1cbc9 77 53 65 74 2c 20 26 76 61 6c 29 3d 3d 30 0a 20  wSet, &val)==0. 
1cbca 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62   ){.    /* The b
1cbcb 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 69 73 20  oolean index is 
1cbcc 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 73 71 6c  empty */.    sql
1cbcd 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1cbce 6c 6c 28 70 49 64 78 29 3b 0a 20 20 20 20 70 63  ll(pIdx);.    pc
1cbcf 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1cbd0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1cbd1 41 20 76 61 6c 75 65 20 77 61 73 20 70 75 6c 6c  A value was pull
1cbd2 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65  ed from the inde
1cbd3 78 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  x */.    assert(
1cbd4 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f   pOp->p3>0 && pO
1cbd5 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1cbd6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1cbd7 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f 75  eMemSetInt64(pOu
1cbd8 74 2c 20 76 61 6c 29 3b 0a 20 20 7d 0a 20 20 62  t, val);.  }.  b
1cbd9 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65  reak;.}...#ifnde
1cbda 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1cbdb 49 47 47 45 52 0a 2f 2a 20 4f 70 63 6f 64 65 3a  IGGER./* Opcode:
1cbdc 20 43 6f 6e 74 65 78 74 50 75 73 68 20 2a 20 2a   ContextPush * *
1cbdd 20 2a 20 0a 2a 2a 0a 2a 2a 20 53 61 76 65 20 74   * .**.** Save t
1cbde 68 65 20 63 75 72 72 65 6e 74 20 56 64 62 65 20  he current Vdbe 
1cbdf 63 6f 6e 74 65 78 74 20 73 75 63 68 20 74 68 61  context such tha
1cbe0 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 74  t it can be rest
1cbe1 6f 72 65 64 20 62 79 20 61 20 43 6f 6e 74 65 78  ored by a Contex
1cbe2 74 50 6f 70 0a 2a 2a 20 6f 70 63 6f 64 65 2e 20  tPop.** opcode. 
1cbe3 54 68 65 20 63 6f 6e 74 65 78 74 20 73 74 6f 72  The context stor
1cbe4 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65  es the last inse
1cbe5 72 74 20 72 6f 77 20 69 64 2c 20 74 68 65 20 6c  rt row id, the l
1cbe6 61 73 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68  ast statement ch
1cbe7 61 6e 67 65 0a 2a 2a 20 63 6f 75 6e 74 2c 20 61  ange.** count, a
1cbe8 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  nd the current s
1cbe9 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 20  tatement change 
1cbea 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f  count..*/.case O
1cbeb 50 5f 43 6f 6e 74 65 78 74 50 75 73 68 3a 20 7b  P_ContextPush: {
1cbec 0a 20 20 69 6e 74 20 69 20 3d 20 70 2d 3e 63 6f  .  int i = p->co
1cbed 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 2b 2b 3b  ntextStackTop++;
1cbee 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  .  Context *pCon
1cbef 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  text;..  assert(
1cbf0 20 69 3e 3d 30 20 29 3b 0a 20 20 2f 2a 20 46 49   i>=0 );.  /* FI
1cbf1 58 20 4d 45 3a 20 54 68 69 73 20 73 68 6f 75 6c  X ME: This shoul
1cbf2 64 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 61  d be allocated a
1cbf3 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 76 64  s part of the vd
1cbf4 62 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  be at compile-ti
1cbf5 6d 65 20 2a 2f 0a 20 20 69 66 28 20 69 3e 3d 70  me */.  if( i>=p
1cbf6 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65  ->contextStackDe
1cbf7 70 74 68 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6f  pth ){.    p->co
1cbf8 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74 68 20  ntextStackDepth 
1cbf9 3d 20 69 2b 31 3b 0a 20 20 20 20 70 2d 3e 63 6f  = i+1;.    p->co
1cbfa 6e 74 65 78 74 53 74 61 63 6b 20 3d 20 73 71 6c  ntextStack = sql
1cbfb 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
1cbfc 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f 6e 74 65  ree(db, p->conte
1cbfd 78 74 53 74 61 63 6b 2c 0a 20 20 20 20 20 20 20  xtStack,.       
1cbfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cbff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cc00 20 20 20 73 69 7a 65 6f 66 28 43 6f 6e 74 65 78     sizeof(Contex
1cc01 74 29 2a 28 69 2b 31 29 29 3b 0a 20 20 20 20 69  t)*(i+1));.    i
1cc02 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61  f( p->contextSta
1cc03 63 6b 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  ck==0 ) goto no_
1cc04 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 43 6f 6e 74  mem;.  }.  pCont
1cc05 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e 74 65 78  ext = &p->contex
1cc06 74 53 74 61 63 6b 5b 69 5d 3b 0a 20 20 70 43 6f  tStack[i];.  pCo
1cc07 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f 77 69 64  ntext->lastRowid
1cc08 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64   = db->lastRowid
1cc09 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 6e 43  ;.  pContext->nC
1cc0a 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 61 6e  hange = p->nChan
1cc0b 67 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  ge;.  break;.}..
1cc0c 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65  /* Opcode: Conte
1cc0d 78 74 50 6f 70 20 2a 20 2a 20 2a 20 0a 2a 2a 0a  xtPop * * * .**.
1cc0e 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 56  ** Restore the V
1cc0f 64 62 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 74  dbe context to t
1cc10 68 65 20 73 74 61 74 65 20 69 74 20 77 61 73 20  he state it was 
1cc11 69 6e 20 77 68 65 6e 20 63 6f 6e 74 65 78 74 50  in when contextP
1cc12 75 73 68 20 77 61 73 20 6c 61 73 74 0a 2a 2a 20  ush was last.** 
1cc13 65 78 65 63 75 74 65 64 2e 20 54 68 65 20 63 6f  executed. The co
1cc14 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65  ntext stores the
1cc15 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77   last insert row
1cc16 20 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74   id, the last st
1cc17 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 68 61 6e 67  atement.** chang
1cc18 65 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65  e count, and the
1cc19 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65   current stateme
1cc1a 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e  nt change count.
1cc1b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74  .*/.case OP_Cont
1cc1c 65 78 74 50 6f 70 3a 20 7b 0a 20 20 43 6f 6e 74  extPop: {.  Cont
1cc1d 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 20 3d 20  ext *pContext = 
1cc1e 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b  &p->contextStack
1cc1f 5b 2d 2d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61  [--p->contextSta
1cc20 63 6b 54 6f 70 5d 3b 0a 20 20 61 73 73 65 72 74  ckTop];.  assert
1cc21 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  ( p->contextStac
1cc22 6b 54 6f 70 3e 3d 30 20 29 3b 0a 20 20 64 62 2d  kTop>=0 );.  db-
1cc23 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 43 6f  >lastRowid = pCo
1cc24 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f 77 69 64  ntext->lastRowid
1cc25 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d  ;.  p->nChange =
1cc26 20 70 43 6f 6e 74 65 78 74 2d 3e 6e 43 68 61 6e   pContext->nChan
1cc27 67 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  ge;.  break;.}.#
1cc28 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66  endif /* #ifndef
1cc29 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
1cc2a 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GGER */..#ifndef
1cc2b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1cc2c 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70  OINCREMENT./* Op
1cc2d 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50 31 20  code: MemMax P1 
1cc2e 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  P2 * * *.**.** S
1cc2f 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
1cc30 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74  register P1 to t
1cc31 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 69 74  he maximum of it
1cc32 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 0a  s current value.
1cc33 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  ** and the value
1cc34 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
1cc35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
1cc36 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 20 61  ruction throws a
1cc37 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 6d  n error if the m
1cc38 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 6e 6f  emory cell is no
1cc39 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 61  t initially.** a
1cc3a 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61  n integer..*/.ca
1cc3b 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b 20  se OP_MemMax: { 
1cc3c 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20 69         /* in1, i
1cc3d 6e 32 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  n2 */.  sqlite3V
1cc3e 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
1cc3f 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65  (pIn1);.  sqlite
1cc40 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
1cc41 66 79 28 70 49 6e 32 29 3b 0a 20 20 69 66 28 20  fy(pIn2);.  if( 
1cc42 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32 2d 3e  pIn1->u.i<pIn2->
1cc43 75 2e 69 29 7b 0a 20 20 20 20 70 49 6e 31 2d 3e  u.i){.    pIn1->
1cc44 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b  u.i = pIn2->u.i;
1cc45 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1cc46 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1cc47 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
1cc48 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  ENT */../* Opcod
1cc49 65 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a  e: IfPos P1 P2 *
1cc4a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   * *.**.** If th
1cc4b 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73  e value of regis
1cc4c 74 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67  ter P1 is 1 or g
1cc4d 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20  reater, jump to 
1cc4e 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  P2..**.** It is 
1cc4f 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74  illegal to use t
1cc50 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1cc51 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68  on a register th
1cc52 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  at does.** not c
1cc53 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
1cc54 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  r.  An assertion
1cc55 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75   fault will resu
1cc56 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a  lt if you try..*
1cc57 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a  /.case OP_IfPos:
1cc58 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1cc59 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65  p, in1 */.  asse
1cc5a 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26  rt( pIn1->flags&
1cc5b 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28  MEM_Int );.  if(
1cc5c 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a   pIn1->u.i>0 ){.
1cc5d 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1cc5e 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1cc5f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1cc60 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20  : IfNeg P1 P2 * 
1cc61 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  * *.**.** If the
1cc62 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
1cc63 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68  er P1 is less th
1cc64 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f  an zero, jump to
1cc65 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69   P2. .**.** It i
1cc66 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65  s illegal to use
1cc67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1cc68 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20  n on a register 
1cc69 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  that does.** not
1cc6a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65   contain an inte
1cc6b 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69  ger.  An asserti
1cc6c 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65  on fault will re
1cc6d 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e  sult if you try.
1cc6e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65  .*/.case OP_IfNe
1cc6f 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  g: {        /* j
1cc70 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73  ump, in1 */.  as
1cc71 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1cc72 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69  s&MEM_Int );.  i
1cc73 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29  f( pIn1->u.i<0 )
1cc74 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1cc75 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1cc76 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cc77 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32  de: IfZero P1 P2
1cc78 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20   * * *.**.** If 
1cc79 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67  the value of reg
1cc7a 69 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63  ister P1 is exac
1cc7b 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50  tly 0, jump to P
1cc7c 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  2. .**.** It is 
1cc7d 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74  illegal to use t
1cc7e 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1cc7f 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68  on a register th
1cc80 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  at does.** not c
1cc81 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
1cc82 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  r.  An assertion
1cc83 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75   fault will resu
1cc84 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a  lt if you try..*
1cc85 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f  /.case OP_IfZero
1cc86 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1cc87 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73  mp, in1 */.  ass
1cc88 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
1cc89 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66  &MEM_Int );.  if
1cc8a 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29  ( pIn1->u.i==0 )
1cc8b 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1cc8c 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1cc8d 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cc8e 64 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32  de: AggStep * P2
1cc8f 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1cc90 45 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70  Execute the step
1cc91 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e   function for an
1cc92 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65   aggregate.  The
1cc93 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  .** function has
1cc94 20 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   P5 arguments.  
1cc95 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
1cc96 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a   to the FuncDef.
1cc97 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ** structure tha
1cc98 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
1cc99 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72  function.  Use r
1cc9a 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73  egister.** P3 as
1cc9b 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1cc9c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61  ..**.** The P5 a
1cc9d 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b  rguments are tak
1cc9e 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  en from register
1cc9f 20 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73   P2 and its.** s
1cca0 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61  uccessors..*/.ca
1cca1 73 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b  se OP_AggStep: {
1cca2 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e  .  int n = pOp->
1cca3 70 35 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d  p5;.  int i;.  M
1cca4 65 6d 20 2a 70 4d 65 6d 2c 20 2a 70 52 65 63 3b  em *pMem, *pRec;
1cca5 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1cca6 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65  xt ctx;.  sqlite
1cca7 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b  3_value **apVal;
1cca8 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30  ..  assert( n>=0
1cca9 20 29 3b 0a 20 20 70 52 65 63 20 3d 20 26 70 2d   );.  pRec = &p-
1ccaa 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
1ccab 20 20 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41    apVal = p->apA
1ccac 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 61 70  rg;.  assert( ap
1ccad 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 20  Val || n==0 );. 
1ccae 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
1ccaf 2b 2b 2c 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20  ++, pRec++){.   
1ccb0 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 52 65 63   apVal[i] = pRec
1ccb1 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 49  ;.    storeTypeI
1ccb2 6e 66 6f 28 70 52 65 63 2c 20 65 6e 63 6f 64 69  nfo(pRec, encodi
1ccb3 6e 67 29 3b 0a 20 20 7d 0a 20 20 63 74 78 2e 70  ng);.  }.  ctx.p
1ccb4 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Func = pOp->p4.p
1ccb5 46 75 6e 63 3b 0a 20 20 61 73 73 65 72 74 28 20  Func;.  assert( 
1ccb6 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70  pOp->p3>0 && pOp
1ccb7 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1ccb8 0a 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d  .  ctx.pMem = pM
1ccb9 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  em = &p->aMem[pO
1ccba 70 2d 3e 70 33 5d 3b 0a 20 20 70 4d 65 6d 2d 3e  p->p3];.  pMem->
1ccbb 6e 2b 2b 3b 0a 20 20 63 74 78 2e 73 2e 66 6c 61  n++;.  ctx.s.fla
1ccbc 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1ccbd 20 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20   ctx.s.z = 0;.  
1ccbe 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20  ctx.s.zMalloc = 
1ccbf 30 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c 20  0;.  ctx.s.xDel 
1ccc0 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20  = 0;.  ctx.s.db 
1ccc1 3d 20 64 62 3b 0a 20 20 63 74 78 2e 69 73 45 72  = db;.  ctx.isEr
1ccc2 72 6f 72 20 3d 20 30 3b 0a 20 20 63 74 78 2e 70  ror = 0;.  ctx.p
1ccc3 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20  Coll = 0;.  if( 
1ccc4 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73  ctx.pFunc->flags
1ccc5 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
1ccc6 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61  EEDCOLL ){.    a
1ccc7 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f  ssert( pOp>p->aO
1ccc8 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  p );.    assert(
1ccc9 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d   pOp[-1].p4type=
1ccca 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20  =P4_COLLSEQ );. 
1cccb 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d     assert( pOp[-
1cccc 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f  1].opcode==OP_Co
1cccd 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78  llSeq );.    ctx
1ccce 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d  .pColl = pOp[-1]
1cccf 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20  .p4.pColl;.  }. 
1ccd0 20 28 63 74 78 2e 70 46 75 6e 63 2d 3e 78 53 74   (ctx.pFunc->xSt
1ccd1 65 70 29 28 26 63 74 78 2c 20 6e 2c 20 61 70 56  ep)(&ctx, n, apV
1ccd2 61 6c 29 3b 0a 20 20 69 66 28 20 63 74 78 2e 69  al);.  if( ctx.i
1ccd3 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 71  sError ){.    sq
1ccd4 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1ccd5 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1ccd6 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61  "%s", sqlite3_va
1ccd7 6c 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73 29  lue_text(&ctx.s)
1ccd8 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 78 2e  );.    rc = ctx.
1ccd9 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 73  isError;.  }.  s
1ccda 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1ccdb 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20  ease(&ctx.s);.  
1ccdc 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1ccdd 6f 64 65 3a 20 41 67 67 46 69 6e 61 6c 20 50 31  ode: AggFinal P1
1ccde 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
1ccdf 20 45 78 65 63 75 74 65 20 74 68 65 20 66 69 6e   Execute the fin
1cce0 61 6c 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20  alizer function 
1cce1 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65  for an aggregate
1cce2 2e 20 20 50 31 20 69 73 0a 2a 2a 20 74 68 65 20  .  P1 is.** the 
1cce3 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  memory location 
1cce4 74 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75  that is the accu
1cce5 6d 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20  mulator for the 
1cce6 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a  aggregate..**.**
1cce7 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65   P2 is the numbe
1cce8 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
1cce9 68 61 74 20 74 68 65 20 73 74 65 70 20 66 75 6e  hat the step fun
1ccea 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64 0a  ction takes and.
1cceb 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1ccec 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65  er to the FuncDe
1cced 66 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 74  f for this funct
1ccee 69 6f 6e 2e 20 20 54 68 65 20 50 32 0a 2a 2a 20  ion.  The P2.** 
1ccef 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20  argument is not 
1ccf0 75 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 63  used by this opc
1ccf1 6f 64 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ode.  It is only
1ccf2 20 74 68 65 72 65 20 74 6f 20 64 69 73 61 6d 62   there to disamb
1ccf3 69 67 75 61 74 65 0a 2a 2a 20 66 75 6e 63 74 69  iguate.** functi
1ccf4 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 74 61 6b  ons that can tak
1ccf5 65 20 76 61 72 79 69 6e 67 20 6e 75 6d 62 65 72  e varying number
1ccf6 73 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20  s of arguments. 
1ccf7 20 54 68 65 0a 2a 2a 20 50 34 20 61 72 67 75 6d   The.** P4 argum
1ccf8 65 6e 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64  ent is only need
1ccf9 65 64 20 66 6f 72 20 74 68 65 20 64 65 67 65 6e  ed for the degen
1ccfa 65 72 61 74 65 20 63 61 73 65 20 77 68 65 72 65  erate case where
1ccfb 0a 2a 2a 20 74 68 65 20 73 74 65 70 20 66 75 6e  .** the step fun
1ccfc 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 70 72  ction was not pr
1ccfd 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2e  eviously called.
1ccfe 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 46  .*/.case OP_AggF
1ccff 69 6e 61 6c 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70  inal: {.  Mem *p
1cd00 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Mem;.  assert( p
1cd01 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d  Op->p1>0 && pOp-
1cd02 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
1cd03 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65    pMem = &p->aMe
1cd04 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  m[pOp->p1];.  as
1cd05 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
1cd06 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c  gs & ~(MEM_Null|
1cd07 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a  MEM_Agg))==0 );.
1cd08 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1cd09 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 4d  beMemFinalize(pM
1cd0a 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e  em, pOp->p4.pFun
1cd0b 63 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  c);.  if( rc==SQ
1cd0c 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20 20  LITE_ERROR ){.  
1cd0d 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1cd0e 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1cd0f 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
1cd10 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 4d 65  3_value_text(pMe
1cd11 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  m));.  }.  sqlit
1cd12 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
1cd13 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 6f 64  ding(pMem, encod
1cd14 69 6e 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d  ing);.  UPDATE_M
1cd15 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4d 65 6d  AX_BLOBSIZE(pMem
1cd16 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
1cd17 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4d  VdbeMemTooBig(pM
1cd18 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  em) ){.    goto 
1cd19 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62  too_big;.  }.  b
1cd1a 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64  reak;.}...#if !d
1cd1b 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1cd1c 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64  IT_VACUUM) && !d
1cd1d 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1cd1e 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a 20 4f 70  IT_ATTACH)./* Op
1cd1f 63 6f 64 65 3a 20 56 61 63 75 75 6d 20 2a 20 2a  code: Vacuum * *
1cd20 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 56 61 63   * * *.**.** Vac
1cd21 75 75 6d 20 74 68 65 20 65 6e 74 69 72 65 20 64  uum the entire d
1cd22 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 6f  atabase.  This o
1cd23 70 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65  pcode will cause
1cd24 20 6f 74 68 65 72 20 76 69 72 74 75 61 6c 0a 2a   other virtual.*
1cd25 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f 20 62 65  * machines to be
1cd26 20 63 72 65 61 74 65 64 20 61 6e 64 20 72 75 6e   created and run
1cd27 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74 20 62 65  .  It may not be
1cd28 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
1cd29 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63  hin.** a transac
1cd2a 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
1cd2b 5f 56 61 63 75 75 6d 3a 20 7b 0a 20 20 69 66 28  _Vacuum: {.  if(
1cd2c 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1cd2d 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1cd2e 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1cd2f 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ; .  rc = sqlite
1cd30 33 52 75 6e 56 61 63 75 75 6d 28 26 70 2d 3e 7a  3RunVacuum(&p->z
1cd31 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a 20 20 69  ErrMsg, db);.  i
1cd32 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1cd33 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
1cd34 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1cd35 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  e;.  break;.}.#e
1cd36 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e  ndif..#if !defin
1cd37 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ed(SQLITE_OMIT_A
1cd38 55 54 4f 56 41 43 55 55 4d 29 0a 2f 2a 20 4f 70  UTOVACUUM)./* Op
1cd39 63 6f 64 65 3a 20 49 6e 63 72 56 61 63 75 75 6d  code: IncrVacuum
1cd3a 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1cd3b 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73 69 6e  ** Perform a sin
1cd3c 67 6c 65 20 73 74 65 70 20 6f 66 20 74 68 65 20  gle step of the 
1cd3d 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  incremental vacu
1cd3e 75 6d 20 70 72 6f 63 65 64 75 72 65 20 6f 6e 0a  um procedure on.
1cd3f 2a 2a 20 74 68 65 20 50 31 20 64 61 74 61 62 61  ** the P1 databa
1cd40 73 65 2e 20 49 66 20 74 68 65 20 76 61 63 75 75  se. If the vacuu
1cd41 6d 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20  m has finished, 
1cd42 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74  jump to instruct
1cd43 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f 74 68 65 72  ion.** P2. Other
1cd44 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75  wise, fall throu
1cd45 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  gh to the next i
1cd46 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63  nstruction..*/.c
1cd47 61 73 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75  ase OP_IncrVacuu
1cd48 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  m: {        /* j
1cd49 75 6d 70 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  ump */.  Btree *
1cd4a 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pBt;..  assert( 
1cd4b 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1cd4c 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
1cd4d 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
1cd4e 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70  treeMask & (1<<p
1cd4f 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20  Op->p1))!=0 );. 
1cd50 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70   pBt = db->aDb[p
1cd51 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 72  Op->p1].pBt;.  r
1cd52 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1cd53 49 6e 63 72 56 61 63 75 75 6d 28 70 42 74 29 3b  IncrVacuum(pBt);
1cd54 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1cd55 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 70 63  E_DONE ){.    pc
1cd56 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1cd57 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1cd58 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  OK;.  }.  break;
1cd59 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70  .}.#endif../* Op
1cd5a 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50 31 20  code: Expire P1 
1cd5b 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61  * * * *.**.** Ca
1cd5c 75 73 65 20 70 72 65 63 6f 6d 70 69 6c 65 64 20  use precompiled 
1cd5d 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 62 65  statements to be
1cd5e 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 41 6e  come expired. An
1cd5f 20 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65   expired stateme
1cd60 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68  nt.** fails with
1cd61 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f   an error code o
1cd62 66 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  f SQLITE_SCHEMA 
1cd63 69 66 20 69 74 20 69 73 20 65 76 65 72 20 65 78  if it is ever ex
1cd64 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 69 61 20  ecuted .** (via 
1cd65 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 29 2e  sqlite3_step()).
1cd66 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 20 69 73  .** .** If P1 is
1cd67 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 53 51 4c   0, then all SQL
1cd68 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65 63 6f   statements beco
1cd69 6d 65 20 65 78 70 69 72 65 64 2e 20 49 66 20 50  me expired. If P
1cd6a 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a  1 is non-zero,.*
1cd6b 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20  * then only the 
1cd6c 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74  currently execut
1cd6d 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ing statement is
1cd6e 20 61 66 66 65 63 74 65 64 2e 20 0a 2a 2f 0a 63   affected. .*/.c
1cd6f 61 73 65 20 4f 50 5f 45 78 70 69 72 65 3a 20 7b  ase OP_Expire: {
1cd70 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e 70 31 20  .  if( !pOp->p1 
1cd71 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
1cd72 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
1cd73 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 65  ements(db);.  }e
1cd74 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69  lse{.    p->expi
1cd75 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62  red = 1;.  }.  b
1cd76 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  reak;.}..#ifndef
1cd77 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1cd78 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 4f 70 63  RED_CACHE./* Opc
1cd79 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 6b 20 50  ode: TableLock P
1cd7a 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1cd7b 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b  ** Obtain a lock
1cd7c 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   on a particular
1cd7d 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69 6e 73   table. This ins
1cd7e 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  truction is only
1cd7f 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68   used when.** th
1cd80 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66  e shared-cache f
1cd81 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65  eature is enable
1cd82 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20  d. .**.** If P1 
1cd83 69 73 20 20 74 68 65 20 69 6e 64 65 78 20 6f 66  is  the index of
1cd84 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   the database in
1cd85 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20 6f   sqlite3.aDb[] o
1cd86 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  f the database.*
1cd87 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 6c  * on which the l
1cd88 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65 64 2e  ock is acquired.
1cd89 20 20 41 20 72 65 61 64 6c 6f 63 6b 20 69 73 20    A readlock is 
1cd8a 6f 62 74 61 69 6e 65 64 20 69 66 20 50 33 3d 3d  obtained if P3==
1cd8b 30 20 6f 72 0a 2a 2a 20 61 20 77 72 69 74 65 20  0 or.** a write 
1cd8c 6c 6f 63 6b 20 69 66 20 50 33 3d 3d 31 2e 0a 2a  lock if P3==1..*
1cd8d 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74 61 69 6e 73  *.** P2 contains
1cd8e 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f   the root-page o
1cd8f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c  f the table to l
1cd90 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 63 6f  ock..**.** P4 co
1cd91 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
1cd92 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   to the name of 
1cd93 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
1cd94 6c 6f 63 6b 65 64 2e 20 54 68 69 73 20 69 73 20  locked. This is 
1cd95 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64 20 74 6f 20  only.** used to 
1cd96 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  generate an erro
1cd97 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68 65  r message if the
1cd98 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20   lock cannot be 
1cd99 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 63 61 73  obtained..*/.cas
1cd9a 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20  e OP_TableLock: 
1cd9b 7b 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70  {.  int p1 = pOp
1cd9c 2d 3e 70 31 3b 20 0a 20 20 75 38 20 69 73 57 72  ->p1; .  u8 isWr
1cd9d 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 29 70 4f  iteLock = (u8)pO
1cd9e 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28  p->p3;.  assert(
1cd9f 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 64 62 2d   p1>=0 && p1<db-
1cda0 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1cda1 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1cda2 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 20 29 3b  & (1<<p1))!=0 );
1cda3 0a 20 20 61 73 73 65 72 74 28 20 69 73 57 72 69  .  assert( isWri
1cda4 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57  teLock==0 || isW
1cda5 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20  riteLock==1 );. 
1cda6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1cda7 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e  eeLockTable(db->
1cda8 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70  aDb[p1].pBt, pOp
1cda9 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63  ->p2, isWriteLoc
1cdaa 6b 29 3b 0a 20 20 69 66 28 20 28 72 63 26 30 78  k);.  if( (rc&0x
1cdab 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b  FF)==SQLITE_LOCK
1cdac 45 44 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  ED ){.    const 
1cdad 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70  char *z = pOp->p
1cdae 34 2e 7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  4.z;.    sqlite3
1cdaf 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1cdb0 72 72 4d 73 67 2c 20 64 62 2c 20 22 64 61 74 61  rrMsg, db, "data
1cdb1 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c 6f  base table is lo
1cdb2 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29 3b 0a 20  cked: %s", z);. 
1cdb3 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1cdb4 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1cdb5 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1cdb6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1cdb7 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1cdb8 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
1cdb9 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 50 34   VBegin * * * P4
1cdba 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20   *.**.** P4 may 
1cdbb 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
1cdbc 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  an sqlite3_vtab 
1cdbd 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 73 6f  structure. If so
1cdbe 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a 2a 20 78  , call the .** x
1cdbf 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66 6f 72  Begin method for
1cdc0 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a   that table..**.
1cdc1 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 68 65 72  ** Also, whether
1cdc2 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 20 73 65   or not P4 is se
1cdc3 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68  t, check that th
1cdc4 69 73 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  is is not being 
1cdc5 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a 2a 20 77  called from.** w
1cdc6 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 61 63 6b  ithin a callback
1cdc7 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61   to a virtual ta
1cdc8 62 6c 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68  ble xSync() meth
1cdc9 6f 64 2e 20 49 66 20 69 74 20 69 73 2c 20 74 68  od. If it is, th
1cdca 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20  e error.** code 
1cdcb 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 53  will be set to S
1cdcc 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f  QLITE_LOCKED..*/
1cdcd 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 69 6e 3a  .case OP_VBegin:
1cdce 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61   {.  sqlite3_vta
1cdcf 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e  b *pVtab = pOp->
1cdd0 70 34 2e 70 56 74 61 62 3b 0a 20 20 72 63 20 3d  p4.pVtab;.  rc =
1cdd1 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
1cdd2 6e 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20  n(db, pVtab);.  
1cdd3 69 66 28 20 70 56 74 61 62 20 29 7b 0a 20 20 20  if( pVtab ){.   
1cdd4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1cdd5 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, p->zErrMsg);.
1cdd6 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d      p->zErrMsg =
1cdd7 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b   pVtab->zErrMsg;
1cdd8 0a 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72  .    pVtab->zErr
1cdd9 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62  Msg = 0;.  }.  b
1cdda 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1cddb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1cddc 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1cddd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1cdde 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1cddf 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61  /* Opcode: VCrea
1cde0 74 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a  te P1 * * P4 *.*
1cde1 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e  *.** P4 is the n
1cde2 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c  ame of a virtual
1cde3 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61   table in databa
1cde4 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20  se P1. Call the 
1cde5 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a  xCreate method.*
1cde6 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65  * for that table
1cde7 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72  ..*/.case OP_VCr
1cde8 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73  eate: {.  rc = s
1cde9 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72  qlite3VtabCallCr
1cdea 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31  eate(db, pOp->p1
1cdeb 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d  , pOp->p4.z, &p-
1cdec 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65  >zErrMsg);.  bre
1cded 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1cdee 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1cdef 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1cdf0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1cdf1 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1cdf2 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f   Opcode: VDestro
1cdf3 79 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  y P1 * * P4 *.**
1cdf4 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61  .** P4 is the na
1cdf5 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20  me of a virtual 
1cdf6 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73  table in databas
1cdf7 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20  e P1.  Call the 
1cdf8 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a  xDestroy method.
1cdf9 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65  ** of that table
1cdfa 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65  ..*/.case OP_VDe
1cdfb 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e  stroy: {.  p->in
1cdfc 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a  VtabMethod = 2;.
1cdfd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74    rc = sqlite3Vt
1cdfe 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62  abCallDestroy(db
1cdff 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
1ce00 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74  p4.z);.  p->inVt
1ce01 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1ce02 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1ce03 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1ce04 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1ce05 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ce06 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1ce07 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65  ./* Opcode: VOpe
1ce08 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  n P1 * * P4 *.**
1ce09 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
1ce0a 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c  ter to a virtual
1ce0b 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61   table object, a
1ce0c 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  n sqlite3_vtab s
1ce0d 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20  tructure..** P1 
1ce0e 69 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62  is a cursor numb
1ce0f 65 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65  er.  This opcode
1ce10 20 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20   opens a cursor 
1ce11 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  to the virtual.*
1ce12 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  * table and stor
1ce13 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69  es that cursor i
1ce14 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  n P1..*/.case OP
1ce15 5f 56 4f 70 65 6e 3a 20 7b 0a 20 20 56 64 62 65  _VOpen: {.  Vdbe
1ce16 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 30  Cursor *pCur = 0
1ce17 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ;.  sqlite3_vtab
1ce18 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75  _cursor *pVtabCu
1ce19 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20 73 71 6c  rsor = 0;..  sql
1ce1a 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1ce1b 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62   = pOp->p4.pVtab
1ce1c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  ;.  sqlite3_modu
1ce1d 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 28 73  le *pModule = (s
1ce1e 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29  qlite3_module *)
1ce1f 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  pVtab->pModule;.
1ce20 0a 20 20 61 73 73 65 72 74 28 70 56 74 61 62 20  .  assert(pVtab 
1ce21 26 26 20 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69  && pModule);.  i
1ce22 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1ce23 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61  Off(db) ) goto a
1ce24 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1ce25 73 65 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75  se;.  rc = pModu
1ce26 6c 65 2d 3e 78 4f 70 65 6e 28 70 56 74 61 62 2c  le->xOpen(pVtab,
1ce27 20 26 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a   &pVtabCursor);.
1ce28 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ce29 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
1ce2a 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  .  p->zErrMsg = 
1ce2b 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a  pVtab->zErrMsg;.
1ce2c 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67    pVtab->zErrMsg
1ce2d 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69   = 0;.  if( sqli
1ce2e 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1ce2f 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1ce30 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 69 66  _to_misuse;.  if
1ce31 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
1ce32 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  ){.    /* Initia
1ce33 6c 69 7a 65 20 73 71 6c 69 74 65 33 5f 76 74 61  lize sqlite3_vta
1ce34 62 5f 63 75 72 73 6f 72 20 62 61 73 65 20 63 6c  b_cursor base cl
1ce35 61 73 73 20 2a 2f 0a 20 20 20 20 70 56 74 61 62  ass */.    pVtab
1ce36 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 20 3d 20  Cursor->pVtab = 
1ce37 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49  pVtab;..    /* I
1ce38 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20 63  nitialise vdbe c
1ce39 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  ursor object */.
1ce3a 20 20 20 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63      pCur = alloc
1ce3b 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70  ateCursor(p, pOp
1ce3c 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20 30 29 3b  ->p1, 0, -1, 0);
1ce3d 0a 20 20 20 20 69 66 28 20 70 43 75 72 20 29 7b  .    if( pCur ){
1ce3e 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 56 74  .      pCur->pVt
1ce3f 61 62 43 75 72 73 6f 72 20 3d 20 70 56 74 61 62  abCursor = pVtab
1ce40 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 43  Cursor;.      pC
1ce41 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 56  ur->pModule = pV
1ce42 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1ce43 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d  ->pModule;.    }
1ce44 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e  else{.      db->
1ce45 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1ce46 3b 0a 20 20 20 20 20 20 70 4d 6f 64 75 6c 65 2d  ;.      pModule-
1ce47 3e 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72  >xClose(pVtabCur
1ce48 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  sor);.    }.  }.
1ce49 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1ce4a 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1ce4b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1ce4c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1ce4d 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1ce4e 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46  LE./* Opcode: VF
1ce4f 69 6c 74 65 72 20 50 31 20 50 32 20 50 33 20 50  ilter P1 P2 P3 P
1ce50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  4 *.**.** P1 is 
1ce51 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20  a cursor opened 
1ce52 75 73 69 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32  using VOpen.  P2
1ce53 20 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 74   is an address t
1ce54 6f 20 6a 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20  o jump to if.** 
1ce55 74 68 65 20 66 69 6c 74 65 72 65 64 20 72 65 73  the filtered res
1ce56 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79  ult set is empty
1ce57 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69  ..**.** P4 is ei
1ce58 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73  ther NULL or a s
1ce59 74 72 69 6e 67 20 74 68 61 74 20 77 61 73 20 67  tring that was g
1ce5a 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20  enerated by the 
1ce5b 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65  xBestIndex.** me
1ce5c 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75  thod of the modu
1ce5d 6c 65 2e 20 20 54 68 65 20 69 6e 74 65 72 70 72  le.  The interpr
1ce5e 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 50  etation of the P
1ce5f 34 20 73 74 72 69 6e 67 20 69 73 20 6c 65 66 74  4 string is left
1ce60 0a 2a 2a 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c  .** to the modul
1ce61 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1ce62 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
1ce63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ode invokes the 
1ce64 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f  xFilter method o
1ce65 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  n the virtual ta
1ce66 62 6c 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  ble specified.**
1ce67 20 62 79 20 50 31 2e 20 20 54 68 65 20 69 6e 74   by P1.  The int
1ce68 65 67 65 72 20 71 75 65 72 79 20 70 6c 61 6e 20  eger query plan 
1ce69 70 61 72 61 6d 65 74 65 72 20 74 6f 20 78 46 69  parameter to xFi
1ce6a 6c 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 69  lter is stored i
1ce6b 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33  n register.** P3
1ce6c 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 31 20  . Register P3+1 
1ce6d 73 74 6f 72 65 73 20 74 68 65 20 61 72 67 63 20  stores the argc 
1ce6e 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20  parameter to be 
1ce6f 70 61 73 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a  passed to the.**
1ce70 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e   xFilter method.
1ce71 20 52 65 67 69 73 74 65 72 73 20 50 33 2b 32 2e   Registers P3+2.
1ce72 2e 50 33 2b 31 2b 61 72 67 63 20 61 72 65 20 74  .P3+1+argc are t
1ce73 68 65 20 61 72 67 63 0a 2a 2a 20 61 64 64 69 74  he argc.** addit
1ce74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73  ional parameters
1ce75 20 77 68 69 63 68 20 61 72 65 20 70 61 73 73 65   which are passe
1ce76 64 20 74 6f 0a 2a 2a 20 78 46 69 6c 74 65 72 20  d to.** xFilter 
1ce77 61 73 20 61 72 67 76 2e 20 52 65 67 69 73 74 65  as argv. Registe
1ce78 72 20 50 33 2b 32 20 62 65 63 6f 6d 65 73 20 61  r P3+2 becomes a
1ce79 72 67 76 5b 30 5d 20 77 68 65 6e 20 70 61 73 73  rgv[0] when pass
1ce7a 65 64 20 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a  ed to xFilter..*
1ce7b 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 69 73 20 6d  *.** A jump is m
1ce7c 61 64 65 20 74 6f 20 50 32 20 69 66 20 74 68 65  ade to P2 if the
1ce7d 20 72 65 73 75 6c 74 20 73 65 74 20 61 66 74 65   result set afte
1ce7e 72 20 66 69 6c 74 65 72 69 6e 67 20 77 6f 75 6c  r filtering woul
1ce7f 64 20 62 65 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63  d be empty..*/.c
1ce80 61 73 65 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20  ase OP_VFilter: 
1ce81 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20  {   /* jump */. 
1ce82 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e 74   int nArg;.  int
1ce83 20 69 51 75 65 72 79 3b 0a 20 20 63 6f 6e 73 74   iQuery;.  const
1ce84 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1ce85 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20  *pModule;.  Mem 
1ce86 2a 70 51 75 65 72 79 20 3d 20 26 70 2d 3e 61 4d  *pQuery = &p->aM
1ce87 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d  em[pOp->p3];.  M
1ce88 65 6d 20 2a 70 41 72 67 63 20 3d 20 26 70 51 75  em *pArgc = &pQu
1ce89 65 72 79 5b 31 5d 3b 0a 20 20 73 71 6c 69 74 65  ery[1];.  sqlite
1ce8a 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
1ce8b 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71  VtabCursor;.  sq
1ce8c 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1ce8d 62 3b 0a 0a 20 20 56 64 62 65 43 75 72 73 6f 72  b;..  VdbeCursor
1ce8e 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73   *pCur = p->apCs
1ce8f 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20 20 52  r[pOp->p1];..  R
1ce90 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1ce91 70 2d 3e 70 33 2c 20 70 51 75 65 72 79 29 3b 0a  p->p3, pQuery);.
1ce92 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1ce93 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20  pVtabCursor );. 
1ce94 20 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70   pVtabCursor = p
1ce95 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
1ce96 3b 0a 20 20 70 56 74 61 62 20 3d 20 70 56 74 61  ;.  pVtab = pVta
1ce97 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a  bCursor->pVtab;.
1ce98 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61    pModule = pVta
1ce99 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f  b->pModule;..  /
1ce9a 2a 20 47 72 61 62 20 74 68 65 20 69 6e 64 65 78  * Grab the index
1ce9b 20 6e 75 6d 62 65 72 20 61 6e 64 20 61 72 67 63   number and argc
1ce9c 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20   parameters */. 
1ce9d 20 61 73 73 65 72 74 28 20 28 70 51 75 65 72 79   assert( (pQuery
1ce9e 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29  ->flags&MEM_Int)
1ce9f 21 3d 30 20 26 26 20 70 41 72 67 63 2d 3e 66 6c  !=0 && pArgc->fl
1cea0 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a  ags==MEM_Int );.
1cea1 20 20 6e 41 72 67 20 3d 20 28 69 6e 74 29 70 41    nArg = (int)pA
1cea2 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 69 51 75 65  rgc->u.i;.  iQue
1cea3 72 79 20 3d 20 28 69 6e 74 29 70 51 75 65 72 79  ry = (int)pQuery
1cea4 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49 6e 76  ->u.i;..  /* Inv
1cea5 6f 6b 65 20 74 68 65 20 78 46 69 6c 74 65 72 20  oke the xFilter 
1cea6 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a 20 20  method */.  {.  
1cea7 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20    int res = 0;. 
1cea8 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4d 65     int i;.    Me
1cea9 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61  m **apArg = p->a
1ceaa 70 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 69 20  pArg;.    for(i 
1ceab 3d 20 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b 2b  = 0; i<nArg; i++
1ceac 29 7b 0a 20 20 20 20 20 20 61 70 41 72 67 5b 69  ){.      apArg[i
1cead 5d 20 3d 20 26 70 41 72 67 63 5b 69 2b 31 5d 3b  ] = &pArgc[i+1];
1ceae 0a 20 20 20 20 20 20 73 74 6f 72 65 54 79 70 65  .      storeType
1ceaf 49 6e 66 6f 28 61 70 41 72 67 5b 69 5d 2c 20 30  Info(apArg[i], 0
1ceb0 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
1ceb1 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1ceb2 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1ceb3 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1ceb4 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74  e;.    sqlite3Vt
1ceb5 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20  abLock(pVtab);. 
1ceb6 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68     p->inVtabMeth
1ceb7 6f 64 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d  od = 1;.    rc =
1ceb8 20 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65   pModule->xFilte
1ceb9 72 28 70 56 74 61 62 43 75 72 73 6f 72 2c 20 69  r(pVtabCursor, i
1ceba 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70 34 2e 7a  Query, pOp->p4.z
1cebb 2c 20 6e 41 72 67 2c 20 61 70 41 72 67 29 3b 0a  , nArg, apArg);.
1cebc 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74      p->inVtabMet
1cebd 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  hod = 0;.    sql
1cebe 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1cebf 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
1cec0 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74  p->zErrMsg = pVt
1cec1 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20  ab->zErrMsg;.   
1cec2 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20   pVtab->zErrMsg 
1cec3 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
1cec4 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70  VtabUnlock(db, p
1cec5 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20 72  Vtab);.    if( r
1cec6 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1cec7 20 20 20 20 20 20 72 65 73 20 3d 20 70 4d 6f 64        res = pMod
1cec8 75 6c 65 2d 3e 78 45 6f 66 28 70 56 74 61 62 43  ule->xEof(pVtabC
1cec9 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20  ursor);.    }.  
1ceca 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1cecb 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
1cecc 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1cecd 73 75 73 65 3b 0a 0a 20 20 20 20 69 66 28 20 72  suse;..    if( r
1cece 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  es ){.      pc =
1cecf 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1ced0 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e    }.  }.  pCur->
1ced1 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20  nullRow = 0;..  
1ced2 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1ced3 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1ced4 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1ced5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ced6 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1ced7 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 6f 77  ./* Opcode: VRow
1ced8 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  id P1 P2 * * *.*
1ced9 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 74 6f 20  *.** Store into 
1ceda 72 65 67 69 73 74 65 72 20 50 32 20 20 74 68 65  register P2  the
1cedb 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 74 68 65   rowid of.** the
1cedc 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74   virtual-table t
1cedd 68 61 74 20 74 68 65 20 50 31 20 63 75 72 73 6f  hat the P1 curso
1cede 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
1cedf 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 52 6f  ..*/.case OP_VRo
1cee0 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20  wid: {          
1cee1 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1cee2 6c 65 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74  lease */.  sqlit
1cee3 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1cee4 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
1cee5 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1cee6 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
1cee7 69 52 6f 77 3b 0a 20 20 56 64 62 65 43 75 72 73  iRow;.  VdbeCurs
1cee8 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70  or *pCur = p->ap
1cee9 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a 20  Csr[pOp->p1];.. 
1ceea 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70   assert( pCur->p
1ceeb 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20  VtabCursor );.  
1ceec 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f  if( pCur->nullRo
1ceed 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  w ){.    break;.
1ceee 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43    }.  pVtab = pC
1ceef 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d  ur->pVtabCursor-
1cef0 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c  >pVtab;.  pModul
1cef1 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75  e = pVtab->pModu
1cef2 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  le;.  assert( pM
1cef3 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b  odule->xRowid );
1cef4 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1cef5 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1cef6 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1cef7 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70  misuse;.  rc = p
1cef8 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 70  Module->xRowid(p
1cef9 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
1cefa 2c 20 26 69 52 6f 77 29 3b 0a 20 20 73 71 6c 69  , &iRow);.  sqli
1cefb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1cefc 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e  >zErrMsg);.  p->
1cefd 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d  zErrMsg = pVtab-
1cefe 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61  >zErrMsg;.  pVta
1ceff 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  b->zErrMsg = 0;.
1cf00 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1cf01 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
1cf02 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1cf03 73 75 73 65 3b 0a 20 20 4d 65 6d 53 65 74 54 79  suse;.  MemSetTy
1cf04 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1cf05 5f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e 75  _Int);.  pOut->u
1cf06 2e 69 20 3d 20 69 52 6f 77 3b 0a 20 20 62 72 65  .i = iRow;.  bre
1cf07 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1cf08 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1cf09 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1cf0a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1cf0b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1cf0c 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d 6e   Opcode: VColumn
1cf0d 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1cf0e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 76 61  .** Store the va
1cf0f 6c 75 65 20 6f 66 20 74 68 65 20 50 32 2d 74 68  lue of the P2-th
1cf10 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68   column of.** th
1cf11 65 20 72 6f 77 20 6f 66 20 74 68 65 20 76 69 72  e row of the vir
1cf12 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 74 20  tual-table that 
1cf13 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 73 6f  the .** P1 curso
1cf14 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
1cf15 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1cf16 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43  3..*/.case OP_VC
1cf17 6f 6c 75 6d 6e 3a 20 7b 0a 20 20 73 71 6c 69 74  olumn: {.  sqlit
1cf18 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1cf19 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
1cf1a 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1cf1b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20  .  Mem *pDest;. 
1cf1c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1cf1d 20 73 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 56 64   sContext;..  Vd
1cf1e 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d  beCursor *pCur =
1cf1f 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1cf20 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  1];.  assert( pC
1cf21 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  ur->pVtabCursor 
1cf22 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1cf23 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
1cf24 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1cf25 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d  pDest = &p->aMem
1cf26 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28  [pOp->p3];.  if(
1cf27 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29   pCur->nullRow )
1cf28 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1cf29 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 44 65 73  eMemSetNull(pDes
1cf2a 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20  t);.    break;. 
1cf2b 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70 43 75   }.  pVtab = pCu
1cf2c 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e  r->pVtabCursor->
1cf2d 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65  pVtab;.  pModule
1cf2e 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   = pVtab->pModul
1cf2f 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 6f  e;.  assert( pMo
1cf30 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b  dule->xColumn );
1cf31 0a 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74  .  memset(&sCont
1cf32 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ext, 0, sizeof(s
1cf33 43 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 20 2f 2a  Context));..  /*
1cf34 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c   The output cell
1cf35 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 76   may already hav
1cf36 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63  e a buffer alloc
1cf37 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20  ated. Move.  ** 
1cf38 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74  the current cont
1cf39 65 6e 74 73 20 74 6f 20 73 43 6f 6e 74 65 78 74  ents to sContext
1cf3a 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68  .s so in case th
1cf3b 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20  e user-function 
1cf3c 0a 20 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 68  .  ** can use th
1cf3d 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61  e already alloca
1cf3e 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 65  ted buffer inste
1cf3f 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67  ad of allocating
1cf40 20 61 20 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e 65   a .  ** new one
1cf41 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
1cf42 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 43 6f  VdbeMemMove(&sCo
1cf43 6e 74 65 78 74 2e 73 2c 20 70 44 65 73 74 29 3b  ntext.s, pDest);
1cf44 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1cf45 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 4d  g(&sContext.s, M
1cf46 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 69 66 28  EM_Null);..  if(
1cf47 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1cf48 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1cf49 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1cf4a 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65  ;.  rc = pModule
1cf4b 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e  ->xColumn(pCur->
1cf4c 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 73 43  pVtabCursor, &sC
1cf4d 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29  ontext, pOp->p2)
1cf4e 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1cf4f 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1cf50 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  );.  p->zErrMsg 
1cf51 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  = pVtab->zErrMsg
1cf52 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  ;.  pVtab->zErrM
1cf53 73 67 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f  sg = 0;..  /* Co
1cf54 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  py the result of
1cf55 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f   the function to
1cf56 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72   the P3 register
1cf57 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69  . We.  ** do thi
1cf58 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  s regardless of 
1cf59 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  whether or not a
1cf5a 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  n error occurred
1cf5b 20 74 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20   to ensure any. 
1cf5c 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f   ** dynamic allo
1cf5d 63 61 74 69 6f 6e 20 69 6e 20 73 43 6f 6e 74 65  cation in sConte
1cf5e 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75  xt.s (a Mem stru
1cf5f 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65 64  ct) is  released
1cf60 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
1cf61 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
1cf62 6e 67 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20  ng(&sContext.s, 
1cf63 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47  encoding);.  REG
1cf64 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1cf65 3e 70 33 2c 20 70 44 65 73 74 29 3b 0a 20 20 73  >p3, pDest);.  s
1cf66 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1cf67 65 28 70 44 65 73 74 2c 20 26 73 43 6f 6e 74 65  e(pDest, &sConte
1cf68 78 74 2e 73 29 3b 0a 20 20 55 50 44 41 54 45 5f  xt.s);.  UPDATE_
1cf69 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65  MAX_BLOBSIZE(pDe
1cf6a 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69  st);..  if( sqli
1cf6b 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1cf6c 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  ){.    goto abor
1cf6d 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1cf6e 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
1cf6f 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
1cf70 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f  pDest) ){.    go
1cf71 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1cf72 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1cf73 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1cf74 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1cf75 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1cf76 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1cf77 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e  LE./* Opcode: VN
1cf78 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ext P1 P2 * * *.
1cf79 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69  **.** Advance vi
1cf7a 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74  rtual table P1 t
1cf7b 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69  o the next row i
1cf7c 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74  n its result set
1cf7d 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20   and.** jump to 
1cf7e 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20  instruction P2. 
1cf7f 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74   Or, if the virt
1cf80 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65  ual table has re
1cf81 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64  ached.** the end
1cf82 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 73   of its result s
1cf83 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68  et, then fall th
1cf84 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1cf85 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
1cf86 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a  /.case OP_VNext:
1cf87 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a   {   /* jump */.
1cf88 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1cf89 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73  pVtab;.  const s
1cf8a 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
1cf8b 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65  Module;.  int re
1cf8c 73 20 3d 20 30 3b 0a 0a 20 20 56 64 62 65 43 75  s = 0;..  VdbeCu
1cf8d 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e  rsor *pCur = p->
1cf8e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1cf8f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1cf90 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20  pVtabCursor );. 
1cf91 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52   if( pCur->nullR
1cf92 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b  ow ){.    break;
1cf93 0a 20 20 7d 0a 20 20 70 56 74 61 62 20 3d 20 70  .  }.  pVtab = p
1cf94 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
1cf95 2d 3e 70 56 74 61 62 3b 0a 20 20 70 4d 6f 64 75  ->pVtab;.  pModu
1cf96 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64  le = pVtab->pMod
1cf97 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ule;.  assert( p
1cf98 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 20 29 3b  Module->xNext );
1cf99 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ..  /* Invoke th
1cf9a 65 20 78 4e 65 78 74 28 29 20 6d 65 74 68 6f 64  e xNext() method
1cf9b 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20   of the module. 
1cf9c 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  There is no way 
1cf9d 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64  for the.  ** und
1cf9e 65 72 6c 79 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  erlying implemen
1cf9f 74 61 74 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e  tation to return
1cfa0 20 61 6e 20 65 72 72 6f 72 20 69 66 20 6f 6e 65   an error if one
1cfa1 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 20   occurs during. 
1cfa2 20 2a 2a 20 78 4e 65 78 74 28 29 2e 20 49 6e 73   ** xNext(). Ins
1cfa3 74 65 61 64 2c 20 69 66 20 61 6e 20 65 72 72 6f  tead, if an erro
1cfa4 72 20 6f 63 63 75 72 73 2c 20 74 72 75 65 20 69  r occurs, true i
1cfa5 73 20 72 65 74 75 72 6e 65 64 20 28 69 6e 64 69  s returned (indi
1cfa6 63 61 74 69 6e 67 20 74 68 61 74 20 0a 20 20 2a  cating that .  *
1cfa7 2a 20 64 61 74 61 20 69 73 20 61 76 61 69 6c 61  * data is availa
1cfa8 62 6c 65 29 20 61 6e 64 20 74 68 65 20 65 72 72  ble) and the err
1cfa9 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64  or code returned
1cfaa 20 77 68 65 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72   when xColumn or
1cfab 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72  .  ** some other
1cfac 20 6d 65 74 68 6f 64 20 69 73 20 6e 65 78 74 20   method is next 
1cfad 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 73  invoked on the s
1cfae 61 76 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ave virtual tabl
1cfaf 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20  e cursor..  */. 
1cfb0 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1cfb1 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1cfb2 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1cfb3 73 75 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 56  suse;.  sqlite3V
1cfb4 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a  tabLock(pVtab);.
1cfb5 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
1cfb6 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 70 4d  d = 1;.  rc = pM
1cfb7 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75  odule->xNext(pCu
1cfb8 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b  r->pVtabCursor);
1cfb9 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68  .  p->inVtabMeth
1cfba 6f 64 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  od = 0;.  sqlite
1cfbb 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1cfbc 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
1cfbd 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a  rrMsg = pVtab->z
1cfbe 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d  ErrMsg;.  pVtab-
1cfbf 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1cfc0 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
1cfc1 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20  k(db, pVtab);.  
1cfc2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1cfc3 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 70  K ){.    res = p
1cfc4 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 70 43 75  Module->xEof(pCu
1cfc5 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b  r->pVtabCursor);
1cfc6 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
1cfc7 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1cfc8 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1cfc9 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66  to_misuse;..  if
1cfca 28 20 21 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a  ( !res ){.    /*
1cfcb 20 49 66 20 74 68 65 72 65 20 69 73 20 64 61 74   If there is dat
1cfcc 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f  a, jump to P2 */
1cfcd 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1cfce 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1cfcf 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1cfd0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1cfd1 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1cfd2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1cfd3 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1cfd4 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d 65   Opcode: VRename
1cfd5 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a   P1 * * P4 *.**.
1cfd6 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1cfd7 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20  er to a virtual 
1cfd8 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e  table object, an
1cfd9 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74   sqlite3_vtab st
1cfda 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73  ructure..** This
1cfdb 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
1cfdc 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1cfdd 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64  g xRename method
1cfde 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69  . The value.** i
1cfdf 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  n register P1 is
1cfe0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 7a   passed as the z
1cfe1 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 6f  Name argument to
1cfe2 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74   the xRename met
1cfe3 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  hod..*/.case OP_
1cfe4 56 52 65 6e 61 6d 65 3a 20 7b 0a 20 20 73 71 6c  VRename: {.  sql
1cfe5 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1cfe6 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62   = pOp->p4.pVtab
1cfe7 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 20 3d  ;.  Mem *pName =
1cfe8 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1cfe9 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56  1];.  assert( pV
1cfea 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52  tab->pModule->xR
1cfeb 65 6e 61 6d 65 20 29 3b 0a 20 20 52 45 47 49 53  ename );.  REGIS
1cfec 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1cfed 31 2c 20 70 4e 61 6d 65 29 3b 0a 0a 20 20 53 74  1, pName);..  St
1cfee 72 69 6e 67 69 66 79 28 70 4e 61 6d 65 2c 20 65  ringify(pName, e
1cfef 6e 63 6f 64 69 6e 67 29 3b 0a 0a 20 20 69 66 28  ncoding);..  if(
1cff0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1cff1 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1cff2 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1cff3 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c  ;.  sqlite3VtabL
1cff4 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20 72 63  ock(pVtab);.  rc
1cff5 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   = pVtab->pModul
1cff6 65 2d 3e 78 52 65 6e 61 6d 65 28 70 56 74 61 62  e->xRename(pVtab
1cff7 2c 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73  , pName->z);.  s
1cff8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1cff9 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1cffa 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74  p->zErrMsg = pVt
1cffb 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70  ab->zErrMsg;.  p
1cffc 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
1cffd 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62  0;.  sqlite3Vtab
1cffe 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62  Unlock(db, pVtab
1cfff 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
1d000 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1d001 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1d002 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61  _misuse;..  brea
1d003 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  k;.}.#endif..#if
1d004 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d005 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1d006 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65   Opcode: VUpdate
1d007 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1d008 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69  *.** P4 is a poi
1d009 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61  nter to a virtua
1d00a 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20  l table object, 
1d00b 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  an sqlite3_vtab 
1d00c 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
1d00d 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65  is opcode invoke
1d00e 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  s the correspond
1d00f 69 6e 67 20 78 55 70 64 61 74 65 20 6d 65 74 68  ing xUpdate meth
1d010 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a  od. P2 values.**
1d011 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20   are contiguous 
1d012 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61  memory cells sta
1d013 72 74 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70  rting at P3 to p
1d014 61 73 73 20 74 6f 20 74 68 65 20 78 55 70 64 61  ass to the xUpda
1d015 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f  te .** invocatio
1d016 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20  n. The value in 
1d017 72 65 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d  register (P3+P2-
1d018 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  1) corresponds t
1d019 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65  o the .** p2th e
1d01a 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72  lement of the ar
1d01b 67 76 20 61 72 72 61 79 20 70 61 73 73 65 64 20  gv array passed 
1d01c 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a  to xUpdate..**.*
1d01d 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65  * The xUpdate me
1d01e 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44  thod will do a D
1d01f 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45  ELETE or an INSE
1d020 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54  RT or both..** T
1d021 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65  he argv[0] eleme
1d022 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72 65 73  nt (which corres
1d023 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20  ponds to memory 
1d024 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74  cell P3).** is t
1d025 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f  he rowid of a ro
1d026 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66  w to delete.  If
1d027 20 61 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c   argv[0] is NULL
1d028 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c   then no .** del
1d029 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54  etion occurs.  T
1d02a 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65  he argv[1] eleme
1d02b 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20  nt is the rowid 
1d02c 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72  of the new .** r
1d02d 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ow.  This can be
1d02e 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68   NULL to have th
1d02f 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
1d030 73 65 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a  select the new .
1d031 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73  ** rowid for its
1d032 65 6c 66 2e 20 20 54 68 65 20 73 75 62 73 65 71  elf.  The subseq
1d033 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e  uent elements in
1d034 20 74 68 65 20 61 72 72 61 79 20 61 72 65 20 0a   the array are .
1d035 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  ** the values of
1d036 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
1d037 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49  new row..**.** I
1d038 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20  f P2==1 then no 
1d039 69 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f 72  insert is perfor
1d03a 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73  med.  argv[0] is
1d03b 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a   the rowid of.**
1d03c 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65   a row to delete
1d03d 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20  ..**.** P1 is a 
1d03e 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66  boolean flag. If
1d03f 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 72   it is set to tr
1d040 75 65 20 61 6e 64 20 74 68 65 20 78 55 70 64 61  ue and the xUpda
1d041 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75  te call.** is su
1d042 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74  ccessful, then t
1d043 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
1d044 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73  d by sqlite3_las
1d045 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
1d046 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74   .** is set to t
1d047 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1d048 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f  rowid for the ro
1d049 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e  w just inserted.
1d04a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64  .*/.case OP_VUpd
1d04b 61 74 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33  ate: {.  sqlite3
1d04c 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70  _vtab *pVtab = p
1d04d 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20  Op->p4.pVtab;.  
1d04e 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
1d04f 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74  pModule = (sqlit
1d050 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61  e3_module *)pVta
1d051 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e  b->pModule;.  in
1d052 74 20 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32  t nArg = pOp->p2
1d053 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1d054 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42  >p4type==P4_VTAB
1d055 20 29 3b 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c   );.  if( pModul
1d056 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 20 29 7b  e->xUpdate==0 ){
1d057 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1d058 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1d059 67 2c 20 64 62 2c 20 22 72 65 61 64 2d 6f 6e 6c  g, db, "read-onl
1d05a 79 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 72  y table");.    r
1d05b 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1d05c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
1d05d 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65  nt i;.    sqlite
1d05e 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20  _int64 rowid;.  
1d05f 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20    Mem **apArg = 
1d060 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 4d 65  p->apArg;.    Me
1d061 6d 20 2a 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d  m *pX = &p->aMem
1d062 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66  [pOp->p3];.    f
1d063 6f 72 28 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20  or(i=0; i<nArg; 
1d064 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 6f 72  i++){.      stor
1d065 65 54 79 70 65 49 6e 66 6f 28 70 58 2c 20 30 29  eTypeInfo(pX, 0)
1d066 3b 0a 20 20 20 20 20 20 61 70 41 72 67 5b 69 5d  ;.      apArg[i]
1d067 20 3d 20 70 58 3b 0a 20 20 20 20 20 20 70 58 2b   = pX;.      pX+
1d068 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
1d069 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1d06a 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1d06b 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1d06c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61  ;.    sqlite3Vta
1d06d 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b 0a 20 20  bLock(pVtab);.  
1d06e 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
1d06f 78 55 70 64 61 74 65 28 70 56 74 61 62 2c 20 6e  xUpdate(pVtab, n
1d070 41 72 67 2c 20 61 70 41 72 67 2c 20 26 72 6f 77  Arg, apArg, &row
1d071 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
1d072 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1d073 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
1d074 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e  ErrMsg = pVtab->
1d075 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74  zErrMsg;.    pVt
1d076 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1d077 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62  .    sqlite3Vtab
1d078 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62  Unlock(db, pVtab
1d079 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
1d07a 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1d07b 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1d07c 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 69  to_misuse;.    i
1d07d 66 28 20 70 4f 70 2d 3e 70 31 20 26 26 20 72 63  f( pOp->p1 && rc
1d07e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1d07f 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72       assert( nAr
1d080 67 3e 31 20 26 26 20 61 70 41 72 67 5b 30 5d 20  g>1 && apArg[0] 
1d081 26 26 20 28 61 70 41 72 67 5b 30 5d 2d 3e 66 6c  && (apArg[0]->fl
1d082 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b  ags&MEM_Null) );
1d083 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74 52  .      db->lastR
1d084 6f 77 69 64 20 3d 20 72 6f 77 69 64 3b 0a 20 20  owid = rowid;.  
1d085 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e    }.    p->nChan
1d086 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61  ge++;.  }.  brea
1d087 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1d088 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1d089 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e  ALTABLE */..#ifn
1d08a 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54  def  SQLITE_OMIT
1d08b 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f  _PAGER_PRAGMAS./
1d08c 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f  * Opcode: Pageco
1d08d 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  unt P1 P2 * * *.
1d08e 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
1d08f 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f  current number o
1d090 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62  f pages in datab
1d091 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79  ase P1 to memory
1d092 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73   cell P2..*/.cas
1d093 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20  e OP_Pagecount: 
1d094 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1d095 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1d096 2a 2f 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f  */.  int p1 = pO
1d097 70 2d 3e 70 31 3b 20 0a 20 20 69 6e 74 20 6e 50  p->p1; .  int nP
1d098 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  age;.  Pager *pP
1d099 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
1d09a 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62  reePager(db->aDb
1d09b 5b 70 31 5d 2e 70 42 74 29 3b 0a 0a 20 20 72 63  [p1].pBt);..  rc
1d09c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50   = sqlite3PagerP
1d09d 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
1d09e 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20   &nPage);.  if( 
1d09f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1d0a0 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
1d0a1 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1d0a2 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 6e 50 61 67  pOut->u.i = nPag
1d0a3 65 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  e;.  }.  break;.
1d0a4 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
1d0a5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1d0a6 41 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  ACE./* Opcode: T
1d0a7 72 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a  race * * * P4 *.
1d0a8 2a 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67  **.** If tracing
1d0a9 20 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20   is enabled (by 
1d0aa 74 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  the sqlite3_trac
1d0ab 65 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20  e()) interface, 
1d0ac 74 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d  then.** the UTF-
1d0ad 38 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  8 string contain
1d0ae 65 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74  ed in P4 is emit
1d0af 74 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65  ted on the trace
1d0b0 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61   callback..*/.ca
1d0b1 73 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 20  se OP_Trace: {. 
1d0b2 20 63 68 61 72 20 2a 7a 54 72 61 63 65 20 3d 20   char *zTrace = 
1d0b3 28 70 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70  (pOp->p4.z ? pOp
1d0b4 2d 3e 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c  ->p4.z : p->zSql
1d0b5 29 3b 0a 20 20 69 66 28 20 7a 54 72 61 63 65 20  );.  if( zTrace 
1d0b6 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78  ){.    if( db->x
1d0b7 54 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 64  Trace ){.      d
1d0b8 62 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54  b->xTrace(db->pT
1d0b9 72 61 63 65 41 72 67 2c 20 7a 54 72 61 63 65 29  raceArg, zTrace)
1d0ba 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  ;.    }.#ifdef S
1d0bb 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
1d0bc 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  if( (db->flags &
1d0bd 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65   SQLITE_SqlTrace
1d0be 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
1d0bf 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
1d0c0 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 25 73 5c  ("SQL-trace: %s\
1d0c1 6e 22 2c 20 7a 54 72 61 63 65 29 3b 0a 20 20 20  n", zTrace);.   
1d0c2 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
1d0c3 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d  ITE_DEBUG */.  }
1d0c4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1d0c5 69 66 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  if.../* Opcode: 
1d0c6 4e 6f 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a  Noop * * * * *.*
1d0c7 2a 0a 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e  *.** Do nothing.
1d0c8 20 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69    This instructi
1d0c9 6f 6e 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66  on is often usef
1d0ca 75 6c 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20  ul as a jump.** 
1d0cb 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a  destination..*/.
1d0cc 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20  /*.** The magic 
1d0cd 45 78 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61  Explain opcode a
1d0ce 72 65 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64  re only inserted
1d0cf 20 77 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32   when explain==2
1d0d0 20 28 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f   (which.** is to
1d0d1 20 73 61 79 20 77 68 65 6e 20 74 68 65 20 45 58   say when the EX
1d0d2 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e  PLAIN QUERY PLAN
1d0d3 20 73 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e   syntax is used.
1d0d4 29 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  ).** This opcode
1d0d5 20 72 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61   records informa
1d0d6 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70  tion from the op
1d0d7 74 69 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20  timizer.  It is 
1d0d8 74 68 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  the.** the same 
1d0d9 61 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69  as a no-op.  Thi
1d0da 73 20 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61  s opcodesnever a
1d0db 70 70 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c  ppears in a real
1d0dc 20 56 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a   VM program..*/.
1d0dd 64 65 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20  default: {      
1d0de 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72      /* This is r
1d0df 65 61 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e  eally OP_Noop an
1d0e0 64 20 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a  d OP_Explain */.
1d0e1 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a    break;.}../***
1d0e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1d0e7 65 20 63 61 73 65 73 20 6f 66 20 74 68 65 20 73  e cases of the s
1d0e8 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20  witch statement 
1d0e9 61 62 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20  above this line 
1d0ea 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e  should all be in
1d0eb 64 65 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73  dented.** by 6 s
1d0ec 70 61 63 65 73 2e 20 20 42 75 74 20 74 68 65 20  paces.  But the 
1d0ed 6c 65 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63  left-most 6 spac
1d0ee 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d  es have been rem
1d0ef 6f 76 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20  oved to improve 
1d0f0 74 68 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69  the.** readabili
1d0f1 74 79 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70  ty.  From this p
1d0f2 6f 69 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68  oint on down, th
1d0f3 65 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61  e normal indenta
1d0f4 74 69 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a  tion rules are.*
1d0f5 2a 20 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a  * restored..****
1d0f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d0fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d  *********/.    }
1d0fb 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ..#ifdef VDBE_PR
1d0fc 4f 46 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20  OFILE.    {.    
1d0fd 20 20 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20    u64 elapsed = 
1d0fe 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
1d0ff 2d 20 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70  - start;.      p
1d100 4f 70 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c  Op->cycles += el
1d101 61 70 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70  apsed;.      pOp
1d102 2d 3e 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20  ->cnt++;.#if 0. 
1d103 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73         fprintf(s
1d104 74 64 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22  tdout, "%10llu "
1d105 2c 20 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20  , elapsed);.    
1d106 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
1d107 72 69 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f  rintOp(stdout, o
1d108 72 69 67 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f  rigPc, &p->aOp[o
1d109 72 69 67 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a  rigPc]);.#endif.
1d10a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
1d10b 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
1d10c 6e 67 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74  ng code adds not
1d10d 68 69 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75  hing to the actu
1d10e 61 6c 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  al functionality
1d10f 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70  .    ** of the p
1d110 72 6f 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f  rogram.  It is o
1d111 6e 6c 79 20 68 65 72 65 20 66 6f 72 20 74 65 73  nly here for tes
1d112 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69  ting and debuggi
1d113 6e 67 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68  ng..    ** On th
1d114 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74  e other hand, it
1d115 20 64 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63   does burn CPU c
1d116 79 63 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65  ycles every time
1d117 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20   through.    ** 
1d118 74 68 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f  the evaluator lo
1d119 6f 70 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c  op.  So we can l
1d11a 65 61 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e  eave it out when
1d11b 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e   NDEBUG is defin
1d11c 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64  ed..    */.#ifnd
1d11d 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 61 73  ef NDEBUG.    as
1d11e 73 65 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20  sert( pc>=-1 && 
1d11f 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69  pc<p->nOp );..#i
1d120 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1d121 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61  G.    if( p->tra
1d122 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
1d123 72 63 21 3d 30 20 29 20 66 70 72 69 6e 74 66 28  rc!=0 ) fprintf(
1d124 70 2d 3e 74 72 61 63 65 2c 22 72 63 3d 25 64 5c  p->trace,"rc=%d\
1d125 6e 22 2c 72 63 29 3b 0a 20 20 20 20 20 20 69 66  n",rc);.      if
1d126 28 20 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f  ( opProperty & O
1d127 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c  PFLG_OUT2_PREREL
1d128 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20  EASE ){.        
1d129 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d  registerTrace(p-
1d12a 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c  >trace, pOp->p2,
1d12b 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a   pOut);.      }.
1d12c 20 20 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70        if( opProp
1d12d 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54  erty & OPFLG_OUT
1d12e 33 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67  3 ){.        reg
1d12f 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72  isterTrace(p->tr
1d130 61 63 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f  ace, pOp->p3, pO
1d131 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ut);.      }.   
1d132 20 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51   }.#endif  /* SQ
1d133 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65  LITE_DEBUG */.#e
1d134 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20  ndif  /* NDEBUG 
1d135 2a 2f 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65  */.  }  /* The e
1d136 6e 64 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b  nd of the for(;;
1d137 29 20 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73  ) loop the loops
1d138 20 74 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73   through opcodes
1d139 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20   */..  /* If we 
1d13a 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74  reach this point
1d13b 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
1d13c 65 78 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e  execution is fin
1d13d 69 73 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20  ished with.  ** 
1d13e 61 6e 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65  an error of some
1d13f 20 6b 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65   kind..  */.vdbe
1d140 5f 65 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61  _error_halt:.  a
1d141 73 73 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70  ssert( rc );.  p
1d142 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c  ->rc = rc;.  sql
1d143 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b  ite3VdbeHalt(p);
1d144 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1d145 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20  E_IOERR_NOMEM ) 
1d146 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1d147 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c   = 1;.  rc = SQL
1d148 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a  ITE_ERROR;..  /*
1d149 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c   This is the onl
1d14a 79 20 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69  y way out of thi
1d14b 73 20 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65  s procedure.  We
1d14c 20 68 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65   have to.  ** re
1d14d 6c 65 61 73 65 20 74 68 65 20 6d 75 74 65 78 65  lease the mutexe
1d14e 73 20 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74  s on btrees that
1d14f 20 77 65 72 65 20 61 63 71 75 69 72 65 64 20 61   were acquired a
1d150 74 20 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20  t the.  ** top. 
1d151 2a 2f 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a  */.vdbe_return:.
1d152 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75    sqlite3BtreeMu
1d153 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70  texArrayLeave(&p
1d154 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74  ->aMutex);.  ret
1d155 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75  urn rc;..  /* Ju
1d156 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20  mp to here if a 
1d157 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c  string or blob l
1d158 61 72 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54  arger than SQLIT
1d159 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a  E_MAX_LENGTH.  *
1d15a 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  * is encountered
1d15b 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a  ..  */.too_big:.
1d15c 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d15d 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1d15e 64 62 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62  db, "string or b
1d15f 6c 6f 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20  lob too big");. 
1d160 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f   rc = SQLITE_TOO
1d161 42 49 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65  BIG;.  goto vdbe
1d162 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20  _error_halt;..  
1d163 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
1d164 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
1d165 69 6c 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d  ils..  */.no_mem
1d166 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  :.  db->mallocFa
1d167 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69  iled = 1;.  sqli
1d168 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1d169 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f  >zErrMsg, db, "o
1d16a 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a  ut of memory");.
1d16b 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
1d16c 4d 45 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65  MEM;.  goto vdbe
1d16d 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20  _error_halt;..  
1d16e 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
1d16f 66 6f 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49  for an SQLITE_MI
1d170 53 55 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f  SUSE error..  */
1d171 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69  .abort_due_to_mi
1d172 73 75 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c  suse:.  rc = SQL
1d173 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a  ITE_MISUSE;.  /*
1d174 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20   Fall thru into 
1d175 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1d176 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70  or */..  /* Jump
1d177 20 74 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79   to here for any
1d178 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66   other kind of f
1d179 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65  atal error.  The
1d17a 20 22 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20   "rc" variable. 
1d17b 20 2a 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20   ** should hold 
1d17c 74 68 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72  the error number
1d17d 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65  ..  */.abort_due
1d17e 5f 74 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73  _to_error:.  ass
1d17f 65 72 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d  ert( p->zErrMsg=
1d180 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  =0 );.  if( db->
1d181 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
1d182 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1d183 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1d184 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29  TE_IOERR_NOMEM )
1d185 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
1d186 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1d187 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
1d188 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29  lite3ErrStr(rc))
1d189 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62  ;.  }.  goto vdb
1d18a 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20  e_error_halt;.. 
1d18b 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65   /* Jump to here
1d18c 20 69 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   if the sqlite3_
1d18d 69 6e 74 65 72 72 75 70 74 28 29 20 41 50 49 20  interrupt() API 
1d18e 73 65 74 73 20 74 68 65 20 69 6e 74 65 72 72 75  sets the interru
1d18f 70 74 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20  pt.  ** flag..  
1d190 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  */.abort_due_to_
1d191 69 6e 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73  interrupt:.  ass
1d192 65 72 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e  ert( db->u1.isIn
1d193 74 65 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72  terrupted );.  r
1d194 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52  c = SQLITE_INTER
1d195 52 55 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20  RUPT;.  p->rc = 
1d196 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74  rc;.  sqlite3Set
1d197 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1d198 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
1d199 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29  lite3ErrStr(rc))
1d19a 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72  ;.  goto vdbe_er
1d19b 72 6f 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a  ror_halt;.}../**
1d19c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
1d19d 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a   of vdbe.c *****
1d19e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d19f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1d1a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1d1a2 69 6e 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62  in file vdbeblob
1d1a3 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1d1a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1d1a6 2a 2a 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a  ** 2007 May 1.**
1d1a7 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1d1a8 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1d1a9 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1d1aa 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1d1ab 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1d1ac 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1d1ad 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1d1ae 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1d1af 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1d1b0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1d1b1 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1d1b2 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1d1b3 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1d1b4 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1d1b5 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1d1b6 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1d1b7 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1d1b8 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1d1b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d1bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
1d1bd 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1d1be 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
1d1bf 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63  to implement inc
1d1c0 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f  remental BLOB I/
1d1c1 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64  O..**.** $Id: vd
1d1c2 62 65 62 6c 6f 62 2e 63 2c 76 20 31 2e 33 31 20  beblob.c,v 1.31 
1d1c3 32 30 30 39 2f 30 33 2f 32 34 20 31 35 3a 30 38  2009/03/24 15:08
1d1c4 3a 31 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :10 drh Exp $.*/
1d1c5 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1d1c6 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
1d1c7 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c  ./*.** Valid sql
1d1c8 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c  ite3_blob* handl
1d1c9 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72  es point to Incr
1d1ca 62 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e  blob structures.
1d1cb 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
1d1cc 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72  ct Incrblob Incr
1d1cd 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63  blob;.struct Inc
1d1ce 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c  rblob {.  int fl
1d1cf 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
1d1d0 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c    /* Copy of "fl
1d1d1 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20 73  ags" passed to s
1d1d2 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
1d1d3 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  () */.  int nByt
1d1d4 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1d1d5 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20  /* Size of open 
1d1d6 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a  blob, in bytes *
1d1d7 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b  /.  int iOffset;
1d1d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1d1d9 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c  yte offset of bl
1d1da 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74  ob in cursor dat
1d1db 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  a */.  BtCursor 
1d1dc 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f  *pCsr;         /
1d1dd 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  * Cursor pointin
1d1de 67 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f  g at blob row */
1d1df 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
1d1e0 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74  *pStmt;    /* St
1d1e1 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20  atement holding 
1d1e2 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20  cursor open */. 
1d1e3 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
1d1e4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d1e5 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62  associated datab
1d1e6 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ase */.};../*.**
1d1e7 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e   Open a blob han
1d1e8 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
1d1e9 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1d1ea 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69  lob_open(.  sqli
1d1eb 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20  te3* db,        
1d1ec 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1d1ed 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
1d1ee 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1d1ef 7a 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  zDb,        /* T
1d1f0 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61  he attached data
1d1f1 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
1d1f2 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f  the blob */.  co
1d1f3 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
1d1f4 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  ,     /* The tab
1d1f5 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
1d1f6 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73  e blob */.  cons
1d1f7 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c  t char *zColumn,
1d1f8 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d      /* The colum
1d1f9 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  n containing the
1d1fa 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74   blob */.  sqlit
1d1fb 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20  e_int64 iRow,   
1d1fc 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f     /* The row co
1d1fd 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f  ntaining the glo
1d1fe 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  b */.  int flags
1d1ff 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1d200 2a 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77  * True -> read/w
1d201 72 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c  rite access, fal
1d202 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20  se -> read-only 
1d203 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  */.  sqlite3_blo
1d204 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20  b **ppBlob   /* 
1d205 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73  Handle for acces
1d206 73 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65  sing the blob re
1d207 74 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29  turned here */.)
1d208 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74  {.  int nAttempt
1d209 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c   = 0;.  int iCol
1d20a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1d20b 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c  /* Index of zCol
1d20c 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72  umn in row-recor
1d20d 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20  d */..  /* This 
1d20e 56 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65  VDBE program see
1d20f 6b 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f  ks a btree curso
1d210 72 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66  r to the identif
1d211 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62  ied .  ** db/tab
1d212 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68  le/row entry. Th
1d213 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69  e reason for usi
1d214 6e 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61  ng a vdbe progra
1d215 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f  m instead.  ** o
1d216 66 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74  f writing code t
1d217 6f 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65  o use the b-tree
1d218 20 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20   layer directly 
1d219 69 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a  is that the.  **
1d21a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69   vdbe program wi
1d21b 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  ll take advantag
1d21c 65 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73  e of the various
1d21d 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20   transaction,.  
1d21e 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65  ** locking and e
1d21f 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e  rror handling in
1d220 66 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69  frastructure bui
1d221 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65  lt into the vdbe
1d222 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65  ..  **.  ** Afte
1d223 72 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75  r seeking the cu
1d224 72 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65  rsor, the vdbe e
1d225 78 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65  xecutes an OP_Re
1d226 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f  sultRow..  ** Co
1d227 64 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74  de external to t
1d228 68 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f  he Vdbe then "bo
1d229 72 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65  rrows" the b-tre
1d22a 65 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a  e cursor and.  *
1d22b 2a 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70  * uses it to imp
1d22c 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f  lement the blob_
1d22d 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69  read(), blob_wri
1d22e 74 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62  te() and .  ** b
1d22f 6c 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63  lob_bytes() func
1d230 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
1d231 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f   The sqlite3_blo
1d232 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69  b_close() functi
1d233 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65  on finalizes the
1d234 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20   vdbe program,. 
1d235 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73   ** which closes
1d236 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73   the b-tree curs
1d237 6f 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79  or and (possibly
1d238 29 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20  ) commits the . 
1d239 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
1d23a 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
1d23b 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
1d23c 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20  openBlob[] = {. 
1d23d 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69     {OP_Transacti
1d23e 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  on, 0, 0, 0},   
1d23f 20 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20    /* 0: Start a 
1d240 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
1d241 20 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f     {OP_VerifyCoo
1d242 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  kie, 0, 0, 0},  
1d243 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68    /* 1: Check th
1d244 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
1d245 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f  */..    /* One o
1d246 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
1d247 74 77 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  two instructions
1d248 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20   is replaced by 
1d249 61 6e 0a 20 20 20 20 2a 2a 20 4f 50 5f 4e 6f 6f  an.    ** OP_Noo
1d24a 70 20 62 65 66 6f 72 65 20 65 78 65 63 74 69 6f  p before exectio
1d24b 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7b 4f  n..    */.    {O
1d24c 50 5f 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 30  P_OpenRead, 0, 0
1d24d 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20  , 0},        /* 
1d24e 32 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30  2: Open cursor 0
1d24f 20 66 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a   for reading */.
1d250 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74      {OP_OpenWrit
1d251 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20  e, 0, 0, 0},    
1d252 20 20 20 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75     /* 3: Open cu
1d253 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 2f  rsor 0 for read/
1d254 77 72 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f  write */..    {O
1d255 50 5f 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 31  P_Variable, 1, 1
1d256 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20  , 1},        /* 
1d257 34 3a 20 50 75 73 68 20 74 68 65 20 72 6f 77 69  4: Push the rowi
1d258 64 20 74 6f 20 74 68 65 20 73 74 61 63 6b 20 2a  d to the stack *
1d259 2f 0a 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69  /.    {OP_NotExi
1d25a 73 74 73 2c 20 30 2c 20 38 2c 20 31 7d 2c 20 20  sts, 0, 8, 1},  
1d25b 20 20 20 20 20 2f 2a 20 35 3a 20 53 65 65 6b 20       /* 5: Seek 
1d25c 74 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  the cursor */.  
1d25d 20 20 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c    {OP_Column, 0,
1d25e 20 30 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20   0, 1},         
1d25f 20 2f 2a 20 36 20 20 2a 2f 0a 20 20 20 20 7b 4f   /* 6  */.    {O
1d260 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
1d261 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20  0, 0},       /* 
1d262 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c  7  */.    {OP_Cl
1d263 6f 73 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  ose, 0, 0, 0},  
1d264 20 20 20 20 20 20 20 20 20 2f 2a 20 38 20 20 2a           /* 8  *
1d265 2f 0a 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20  /.    {OP_Halt, 
1d266 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20  0, 0, 0},       
1d267 20 20 20 20 20 2f 2a 20 39 20 2a 2f 0a 20 20 7d       /* 9 */.  }
1d268 3b 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30  ;..  Vdbe *v = 0
1d269 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
1d26a 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 7a  ITE_OK;.  char z
1d26b 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 7a 45 72  Err[128];..  zEr
1d26c 72 5b 30 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69  r[0] = 0;.  sqli
1d26d 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1d26e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 6f  db->mutex);.  do
1d26f 20 7b 0a 20 20 20 20 50 61 72 73 65 20 73 50 61   {.    Parse sPa
1d270 72 73 65 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  rse;.    Table *
1d271 70 54 61 62 3b 0a 0a 20 20 20 20 6d 65 6d 73 65  pTab;..    memse
1d272 74 28 26 73 50 61 72 73 65 2c 20 30 2c 20 73 69  t(&sParse, 0, si
1d273 7a 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20  zeof(Parse));.  
1d274 20 20 73 50 61 72 73 65 2e 64 62 20 3d 20 64 62    sParse.db = db
1d275 3b 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ;..    if( sqlit
1d276 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1d277 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1d278 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
1d279 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65  mutex);.      re
1d27a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
1d27b 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73  SE;.    }..    s
1d27c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1d27d 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 61  All(db);.    pTa
1d27e 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  b = sqlite3Locat
1d27f 65 54 61 62 6c 65 28 26 73 50 61 72 73 65 2c 20  eTable(&sParse, 
1d280 30 2c 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b  0, zTable, zDb);
1d281 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26  .    if( pTab &&
1d282 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1d283 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   ){.      pTab =
1d284 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
1d285 33 45 72 72 6f 72 4d 73 67 28 26 73 50 61 72 73  3ErrorMsg(&sPars
1d286 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20  e, "cannot open 
1d287 76 69 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25  virtual table: %
1d288 73 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20  s", zTable);.   
1d289 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1d28a 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20  E_OMIT_VIEW.    
1d28b 69 66 28 20 70 54 61 62 20 26 26 20 70 54 61 62  if( pTab && pTab
1d28c 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
1d28d 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20     pTab = 0;.   
1d28e 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1d28f 73 67 28 26 73 50 61 72 73 65 2c 20 22 63 61 6e  sg(&sParse, "can
1d290 6e 6f 74 20 6f 70 65 6e 20 76 69 65 77 3a 20 25  not open view: %
1d291 73 22 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20  s", zTable);.   
1d292 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66   }.#endif.    if
1d293 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20  ( !pTab ){.     
1d294 20 69 66 28 20 73 50 61 72 73 65 2e 7a 45 72 72   if( sParse.zErr
1d295 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Msg ){.        s
1d296 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1d297 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45  sizeof(zErr), zE
1d298 72 72 2c 20 22 25 73 22 2c 20 73 50 61 72 73 65  rr, "%s", sParse
1d299 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  .zErrMsg);.     
1d29a 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1d29b 44 62 46 72 65 65 28 64 62 2c 20 73 50 61 72 73  DbFree(db, sPars
1d29c 65 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  e.zErrMsg);.    
1d29d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1d29e 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64  ROR;.      (void
1d29f 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
1d2a0 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c  f(db);.      sql
1d2a1 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
1d2a2 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74  l(db);.      got
1d2a3 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b  o blob_open_out;
1d2a4 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e  .    }..    /* N
1d2a5 6f 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66  ow search pTab f
1d2a6 6f 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c  or the exact col
1d2a7 75 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  umn. */.    for(
1d2a8 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70  iCol=0; iCol < p
1d2a9 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b  Tab->nCol; iCol+
1d2aa 2b 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73  +) {.      if( s
1d2ab 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54  qlite3StrICmp(pT
1d2ac 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
1d2ad 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d  Name, zColumn)==
1d2ae 30 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  0 ){.        bre
1d2af 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1d2b0 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d  }.    if( iCol==
1d2b1 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
1d2b2 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1d2b3 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
1d2b4 29 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63  ), zErr, "no suc
1d2b5 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22  h column: \"%s\"
1d2b6 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  ", zColumn);.   
1d2b7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1d2b8 52 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69  RROR;.      (voi
1d2b9 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
1d2ba 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71  ff(db);.      sq
1d2bb 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41  lite3BtreeLeaveA
1d2bc 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f  ll(db);.      go
1d2bd 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
1d2be 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1d2bf 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
1d2c0 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72  being opened for
1d2c1 20 77 72 69 74 69 6e 67 2c 20 63 68 65 63 6b 20   writing, check 
1d2c2 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  that the.    ** 
1d2c3 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e  column is not in
1d2c4 64 65 78 65 64 2e 20 49 74 20 69 73 20 61 67 61  dexed. It is aga
1d2c5 69 6e 73 74 20 74 68 65 20 72 75 6c 65 73 20 74  inst the rules t
1d2c6 6f 20 6f 70 65 6e 20 61 6e 0a 20 20 20 20 2a 2a  o open an.    **
1d2c7 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20   indexed column 
1d2c8 66 6f 72 20 77 72 69 74 69 6e 67 2e 0a 20 20 20  for writing..   
1d2c9 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 67   */.    if( flag
1d2ca 73 20 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78  s ){.      Index
1d2cb 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20 66 6f   *pIdx;.      fo
1d2cc 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
1d2cd 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
1d2ce 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
1d2cf 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
1d2d0 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
1d2d1 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a  pIdx->nColumn; j
1d2d2 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
1d2d3 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  f( pIdx->aiColum
1d2d4 6e 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20  n[j]==iCol ){.  
1d2d5 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1d2d6 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1d2d7 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20  f(zErr), zErr,. 
1d2d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d2d9 20 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e              "can
1d2da 6e 6f 74 20 6f 70 65 6e 20 69 6e 64 65 78 65 64  not open indexed
1d2db 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74   column for writ
1d2dc 69 6e 67 22 29 3b 0a 20 20 20 20 20 20 20 20 20  ing");.         
1d2dd 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1d2de 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20  RROR;.          
1d2df 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1d2e0 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
1d2e1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1d2e2 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
1d2e3 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  b);.            
1d2e4 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
1d2e5 75 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ut;.          }.
1d2e6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1d2e7 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d  }.    }..    v =
1d2e8 20 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61   sqlite3VdbeCrea
1d2e9 74 65 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20  te(db);.    if( 
1d2ea 76 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  v ){.      int i
1d2eb 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
1d2ec 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
1d2ed 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
1d2ee 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1d2ef 64 64 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65  ddOpList(v, size
1d2f0 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a  of(openBlob)/siz
1d2f1 65 6f 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c  eof(VdbeOpList),
1d2f2 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a 0a 20 20 20   openBlob);..   
1d2f3 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20     /* Configure 
1d2f4 74 68 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69  the OP_Transacti
1d2f5 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  on */.      sqli
1d2f6 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
1d2f7 76 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20  v, 0, iDb);.    
1d2f8 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1d2f9 6e 67 65 50 32 28 76 2c 20 30 2c 20 28 66 6c 61  ngeP2(v, 0, (fla
1d2fa 67 73 20 3f 20 31 20 3a 20 30 29 29 3b 0a 0a 20  gs ? 1 : 0));.. 
1d2fb 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
1d2fc 65 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43  e the OP_VerifyC
1d2fd 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73  ookie */.      s
1d2fe 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1d2ff 50 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20  P1(v, 1, iDb);. 
1d300 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1d301 43 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70  ChangeP2(v, 1, p
1d302 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63  Tab->pSchema->sc
1d303 68 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20  hema_cookie);.. 
1d304 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72       /* Make sur
1d305 65 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c  e a mutex is hel
1d306 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74  d on the table t
1d307 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f  o be accessed */
1d308 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d309 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
1d30a 44 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20  Db); ..      /* 
1d30b 52 65 6d 6f 76 65 20 65 69 74 68 65 72 20 74 68  Remove either th
1d30c 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 6f  e OP_OpenWrite o
1d30d 72 20 4f 70 65 6e 52 65 61 64 2e 20 53 65 74 20  r OpenRead. Set 
1d30e 74 68 65 20 50 32 20 0a 20 20 20 20 20 20 2a 2a  the P2 .      **
1d30f 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68   parameter of th
1d310 65 20 6f 74 68 65 72 20 74 6f 20 70 54 61 62 2d  e other to pTab-
1d311 3e 74 6e 75 6d 2e 20 0a 20 20 20 20 20 20 2a 2f  >tnum. .      */
1d312 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d313 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76  beChangeToNoop(v
1d314 2c 20 28 66 6c 61 67 73 20 3f 20 32 20 3a 20 33  , (flags ? 2 : 3
1d315 29 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ), 1);.      sql
1d316 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
1d317 28 76 2c 20 28 66 6c 61 67 73 20 3f 20 33 20 3a  (v, (flags ? 3 :
1d318 20 32 29 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 29   2), pTab->tnum)
1d319 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d31a 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 28  dbeChangeP3(v, (
1d31b 66 6c 61 67 73 20 3f 20 33 20 3a 20 32 29 2c 20  flags ? 3 : 2), 
1d31c 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  iDb);..      /* 
1d31d 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 6e 75  Configure the nu
1d31e 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e  mber of columns.
1d31f 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63   Configure the c
1d320 75 72 73 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a  ursor to.      *
1d321 2a 20 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65  * think that the
1d322 20 74 61 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d   table has one m
1d323 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20  ore column than 
1d324 69 74 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20  it really.      
1d325 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43  ** does. An OP_C
1d326 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76  olumn to retriev
1d327 65 20 74 68 69 73 20 69 6d 61 67 69 6e 61 72 79  e this imaginary
1d328 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20   column will.   
1d329 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74     ** always ret
1d32a 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e  urn an SQL NULL.
1d32b 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 20   This is useful 
1d32c 62 65 63 61 75 73 65 20 69 74 20 6d 65 61 6e 73  because it means
1d32d 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 63 61 6e  .      ** we can
1d32e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d   invoke OP_Colum
1d32f 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65  n to fill in the
1d330 20 76 64 62 65 20 63 75 72 73 6f 72 73 20 74 79   vdbe cursors ty
1d331 70 65 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64  pe .      ** and
1d332 20 6f 66 66 73 65 74 20 63 61 63 68 65 20 77 69   offset cache wi
1d333 74 68 6f 75 74 20 63 61 75 73 69 6e 67 20 61 6e  thout causing an
1d334 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  y IO..      */. 
1d335 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1d336 43 68 61 6e 67 65 50 34 28 76 2c 20 66 6c 61 67  ChangeP4(v, flag
1d337 73 20 3f 20 33 20 3a 20 32 2c 20 53 51 4c 49 54  s ? 3 : 2, SQLIT
1d338 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61  E_INT_TO_PTR(pTa
1d339 62 2d 3e 6e 43 6f 6c 2b 31 29 2c 20 50 34 5f 49  b->nCol+1), P4_I
1d33a 4e 54 33 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  NT32);.      sql
1d33b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
1d33c 28 76 2c 20 36 2c 20 70 54 61 62 2d 3e 6e 43 6f  (v, 6, pTab->nCo
1d33d 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64  l);.      if( !d
1d33e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1d33f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1d340 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28  e3VdbeMakeReady(
1d341 76 2c 20 31 2c 20 31 2c 20 31 2c 20 30 29 3b 0a  v, 1, 1, 1, 0);.
1d342 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1d343 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72   .    sqlite3Btr
1d344 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
1d345 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d346 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
1d347 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1d348 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  E_OK || db->mall
1d349 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
1d34a 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e    goto blob_open
1d34b 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _out;.    }..   
1d34c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
1d34d 74 36 34 28 28 73 71 6c 69 74 65 33 5f 73 74 6d  t64((sqlite3_stm
1d34e 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f 77 29 3b  t *)v, 1, iRow);
1d34f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1d350 33 5f 73 74 65 70 28 28 73 71 6c 69 74 65 33 5f  3_step((sqlite3_
1d351 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 69  stmt *)v);.    i
1d352 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
1d353 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 74 74 65  W ){.      nAtte
1d354 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20  mpt++;.      rc 
1d355 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  = sqlite3_finali
1d356 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ze((sqlite3_stmt
1d357 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 73 71 6c   *)v);.      sql
1d358 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1d359 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72  zeof(zErr), zErr
1d35a 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
1d35b 28 64 62 29 29 3b 0a 20 20 20 20 20 20 76 20 3d  (db));.      v =
1d35c 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68   0;.    }.  } wh
1d35d 69 6c 65 28 20 6e 41 74 74 65 6d 70 74 3c 35 20  ile( nAttempt<5 
1d35e 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43  && rc==SQLITE_SC
1d35f 48 45 4d 41 20 29 3b 0a 0a 20 20 69 66 28 20 72  HEMA );..  if( r
1d360 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
1d361 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d  .    /* The row-
1d362 72 65 63 6f 72 64 20 68 61 73 20 62 65 65 6e 20  record has been 
1d363 6f 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75  opened successfu
1d364 6c 6c 79 2e 20 43 68 65 63 6b 20 74 68 61 74 20  lly. Check that 
1d365 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  the.    ** colum
1d366 6e 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f  n in question co
1d367 6e 74 61 69 6e 73 20 74 65 78 74 20 6f 72 20 61  ntains text or a
1d368 20 62 6c 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e   blob. If it con
1d369 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78  tains.    ** tex
1d36a 74 2c 20 69 74 20 69 73 20 75 70 20 74 6f 20 74  t, it is up to t
1d36b 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 67 65 74  he caller to get
1d36c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 72 69   the encoding ri
1d36d 67 68 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ght..    */.    
1d36e 49 6e 63 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b  Incrblob *pBlob;
1d36f 0a 20 20 20 20 75 33 32 20 74 79 70 65 20 3d 20  .    u32 type = 
1d370 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79  v->apCsr[0]->aTy
1d371 70 65 5b 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69  pe[iCol];..    i
1d372 66 28 20 74 79 70 65 3c 31 32 20 29 7b 0a 20 20  f( type<12 ){.  
1d373 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1d374 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
1d375 29 2c 20 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74  ), zErr, "cannot
1d376 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74   open value of t
1d377 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20  ype %s",.       
1d378 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c     type==0?"null
1d379 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c  ": type==7?"real
1d37a 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20  ": "integer".   
1d37b 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d     );.      rc =
1d37c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1d37d 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1d37e 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
1d37f 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72     pBlob = (Incr
1d380 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62  blob *)sqlite3Db
1d381 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
1d382 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29  izeof(Incrblob))
1d383 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
1d384 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1d385 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1d386 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20  e(db, pBlob);.  
1d387 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1d388 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  en_out;.    }.  
1d389 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d    pBlob->flags =
1d38a 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f   flags;.    pBlo
1d38b 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70  b->pCsr =  v->ap
1d38c 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b  Csr[0]->pCursor;
1d38d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1d38e 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 6c  eEnterCursor(pBl
1d38f 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73  ob->pCsr);.    s
1d390 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65  qlite3BtreeCache
1d391 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e  Overflow(pBlob->
1d392 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  pCsr);.    sqlit
1d393 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
1d394 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b  or(pBlob->pCsr);
1d395 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d  .    pBlob->pStm
1d396 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d  t = (sqlite3_stm
1d397 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f 62  t *)v;.    pBlob
1d398 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e 61  ->iOffset = v->a
1d399 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 74  pCsr[0]->aOffset
1d39a 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f  [iCol];.    pBlo
1d39b 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 74  b->nByte = sqlit
1d39c 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1d39d 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 70  Len(type);.    p
1d39e 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20  Blob->db = db;. 
1d39f 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 71     *ppBlob = (sq
1d3a0 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 6c  lite3_blob *)pBl
1d3a1 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ob;.    rc = SQL
1d3a2 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20  ITE_OK;.  }else 
1d3a3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1d3a4 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
1d3a5 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
1d3a6 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 6e  (zErr), zErr, "n
1d3a7 6f 20 73 75 63 68 20 72 6f 77 69 64 3a 20 25 6c  o such rowid: %l
1d3a8 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20  ld", iRow);.    
1d3a9 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1d3aa 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65  R;.  }..blob_ope
1d3ab 6e 5f 6f 75 74 3a 0a 20 20 7a 45 72 72 5b 73 69  n_out:.  zErr[si
1d3ac 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d 20  zeof(zErr)-1] = 
1d3ad 27 5c 30 27 3b 0a 20 20 69 66 28 20 76 20 26 26  '\0';.  if( v &&
1d3ae 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20   (rc!=SQLITE_OK 
1d3af 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
1d3b0 6c 65 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  led) ){.    sqli
1d3b1 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
1d3b2 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  v);.  }.  sqlite
1d3b3 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28  3Error(db, rc, (
1d3b4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 30 3a  rc==SQLITE_OK?0:
1d3b5 7a 45 72 72 29 29 3b 0a 20 20 72 63 20 3d 20 73  zErr));.  rc = s
1d3b6 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
1d3b7 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  , rc);.  sqlite3
1d3b8 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
1d3b9 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
1d3ba 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
1d3bb 6c 6f 73 65 20 61 20 62 6c 6f 62 20 68 61 6e 64  lose a blob hand
1d3bc 6c 65 20 74 68 61 74 20 77 61 73 20 70 72 65 76  le that was prev
1d3bd 69 6f 75 73 6c 79 20 63 72 65 61 74 65 64 20 75  iously created u
1d3be 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  sing.** sqlite3_
1d3bf 62 6c 6f 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a  blob_open()..*/.
1d3c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1d3c1 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
1d3c2 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  e(sqlite3_blob *
1d3c3 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c  pBlob){.  Incrbl
1d3c4 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f  ob *p = (Incrblo
1d3c5 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74  b *)pBlob;.  int
1d3c6 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a   rc;.  sqlite3 *
1d3c7 64 62 3b 0a 0a 20 20 64 62 20 3d 20 70 2d 3e 64  db;..  db = p->d
1d3c8 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  b;.  sqlite3_mut
1d3c9 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
1d3ca 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ex);.  rc = sqli
1d3cb 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e  te3_finalize(p->
1d3cc 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65  pStmt);.  sqlite
1d3cd 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
1d3ce 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1d3cf 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
1d3d0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1d3d1 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20  ../*.** Perform 
1d3d2 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  a read or write 
1d3d3 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62  operation on a b
1d3d4 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  lob.*/.static in
1d3d5 74 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28  t blobReadWrite(
1d3d6 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  .  sqlite3_blob 
1d3d7 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20  *pBlob, .  void 
1d3d8 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
1d3d9 20 69 6e 74 20 69 4f 66 66 73 65 74 2c 20 0a 20   int iOffset, . 
1d3da 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 28 42 74   int (*xCall)(Bt
1d3db 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33  Cursor*, u32, u3
1d3dc 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69  2, void*).){.  i
1d3dd 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f  nt rc;.  Incrblo
1d3de 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62  b *p = (Incrblob
1d3df 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65   *)pBlob;.  Vdbe
1d3e0 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a   *v;.  sqlite3 *
1d3e1 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 0a 0a 20  db = p->db;  .. 
1d3e2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1d3e3 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
1d3e4 0a 20 20 76 20 3d 20 28 56 64 62 65 2a 29 70 2d  .  v = (Vdbe*)p-
1d3e5 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 66 28 20 6e  >pStmt;..  if( n
1d3e6 3c 30 20 7c 7c 20 69 4f 66 66 73 65 74 3c 30 20  <0 || iOffset<0 
1d3e7 7c 7c 20 28 69 4f 66 66 73 65 74 2b 6e 29 3e 70  || (iOffset+n)>p
1d3e8 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 2f  ->nByte ){.    /
1d3e9 2a 20 52 65 71 75 65 73 74 20 69 73 20 6f 75 74  * Request is out
1d3ea 20 6f 66 20 72 61 6e 67 65 2e 20 52 65 74 75 72   of range. Retur
1d3eb 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20 65 72  n a transient er
1d3ec 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  ror. */.    rc =
1d3ed 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1d3ee 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
1d3ef 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  db, SQLITE_ERROR
1d3f0 2c 20 30 29 3b 0a 20 20 7d 20 65 6c 73 65 20 69  , 0);.  } else i
1d3f1 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  f( v==0 ){.    /
1d3f2 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
1d3f3 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   statement handl
1d3f4 65 2c 20 74 68 65 6e 20 74 68 65 20 62 6c 6f 62  e, then the blob
1d3f5 2d 68 61 6e 64 6c 65 20 68 61 73 0a 20 20 20 20  -handle has.    
1d3f6 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
1d3f7 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 52 65 74  invalidated. Ret
1d3f8 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  urn SQLITE_ABORT
1d3f9 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20   in this case.. 
1d3fa 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53     */.    rc = S
1d3fb 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d  QLITE_ABORT;.  }
1d3fc 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 61 6c  else{.    /* Cal
1d3fd 6c 20 65 69 74 68 65 72 20 42 74 72 65 65 44 61  l either BtreeDa
1d3fe 74 61 28 29 20 6f 72 20 42 74 72 65 65 50 75 74  ta() or BtreePut
1d3ff 44 61 74 61 28 29 2e 20 49 66 20 53 51 4c 49 54  Data(). If SQLIT
1d400 45 5f 41 42 4f 52 54 20 69 73 0a 20 20 20 20 2a  E_ABORT is.    *
1d401 2a 20 72 65 74 75 72 6e 65 64 2c 20 63 6c 65 61  * returned, clea
1d402 6e 2d 75 70 20 74 68 65 20 73 74 61 74 65 6d 65  n-up the stateme
1d403 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 2a  nt handle..    *
1d404 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  /.    assert( db
1d405 20 3d 3d 20 76 2d 3e 64 62 20 29 3b 0a 20 20 20   == v->db );.   
1d406 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1d407 65 72 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72  erCursor(p->pCsr
1d408 29 3b 0a 20 20 20 20 72 63 20 3d 20 78 43 61 6c  );.    rc = xCal
1d409 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f 66 66 73  l(p->pCsr, iOffs
1d40a 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74 2c 20 6e  et+p->iOffset, n
1d40b 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , z);.    sqlite
1d40c 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f  3BtreeLeaveCurso
1d40d 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  r(p->pCsr);.    
1d40e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 41  if( rc==SQLITE_A
1d40f 42 4f 52 54 20 29 7b 0a 20 20 20 20 20 20 73 71  BORT ){.      sq
1d410 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
1d411 65 28 76 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  e(v);.      p->p
1d412 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65  Stmt = 0;.    }e
1d413 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 65  lse{.      db->e
1d414 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20  rrCode = rc;.   
1d415 20 20 20 76 2d 3e 72 63 20 3d 20 72 63 3b 0a 20     v->rc = rc;. 
1d416 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20     }.  }.  rc = 
1d417 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
1d418 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
1d419 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
1d41a 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
1d41b 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1d41c 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  Read data from a
1d41d 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f   blob handle..*/
1d41e 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1d41f 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61  sqlite3_blob_rea
1d420 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  d(sqlite3_blob *
1d421 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a 7a 2c 20  pBlob, void *z, 
1d422 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73  int n, int iOffs
1d423 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c  et){.  return bl
1d424 6f 62 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f  obReadWrite(pBlo
1d425 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74  b, z, n, iOffset
1d426 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  , sqlite3BtreeDa
1d427 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  ta);.}../*.** Wr
1d428 69 74 65 20 64 61 74 61 20 74 6f 20 61 20 62 6c  ite data to a bl
1d429 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  ob handle..*/.SQ
1d42a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1d42b 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
1d42c 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42  sqlite3_blob *pB
1d42d 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  lob, const void 
1d42e 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69  *z, int n, int i
1d42f 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72  Offset){.  retur
1d430 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28  n blobReadWrite(
1d431 70 42 6c 6f 62 2c 20 28 76 6f 69 64 20 2a 29 7a  pBlob, (void *)z
1d432 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71  , n, iOffset, sq
1d433 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74  lite3BtreePutDat
1d434 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65  a);.}../*.** Que
1d435 72 79 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  ry a blob handle
1d436 20 66 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66   for the size of
1d437 20 74 68 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a   the data..**.**
1d438 20 54 68 65 20 49 6e 63 72 62 6c 6f 62 2e 6e 42   The Incrblob.nB
1d439 79 74 65 20 66 69 65 6c 64 20 69 73 20 66 69 78  yte field is fix
1d43a 65 64 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74  ed for the lifet
1d43b 69 6d 65 20 6f 66 20 74 68 65 20 49 6e 63 72 62  ime of the Incrb
1d43c 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f 20 6d 75 74  lob.** so no mut
1d43d 65 78 20 69 73 20 72 65 71 75 69 72 65 64 20 66  ex is required f
1d43e 6f 72 20 61 63 63 65 73 73 2e 0a 2a 2f 0a 53 51  or access..*/.SQ
1d43f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1d440 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28  ite3_blob_bytes(
1d441 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42  sqlite3_blob *pB
1d442 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62  lob){.  Incrblob
1d443 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20   *p = (Incrblob 
1d444 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 65 74 75 72  *)pBlob;.  retur
1d445 6e 20 70 2d 3e 6e 42 79 74 65 3b 0a 7d 0a 0a 23  n p->nByte;.}..#
1d446 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66  endif /* #ifndef
1d447 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
1d448 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  RBLOB */../*****
1d449 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1d44a 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a   vdbeblob.c ****
1d44b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d44c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d44d 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1d44e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1d44f 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a  file journal.c *
1d450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d451 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d452 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1d453 32 30 30 37 20 41 75 67 75 73 74 20 32 32 0a 2a  2007 August 22.*
1d454 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1d455 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1d456 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1d457 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1d458 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
1d459 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1d45a 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1d45b 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1d45c 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1d45d 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1d45e 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1d45f 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1d460 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1d461 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1d462 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1d463 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1d464 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1d465 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1d466 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d467 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d468 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d469 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1d46a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6a 6f  .** @(#) $Id: jo
1d46b 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 39 20 32 30  urnal.c,v 1.9 20
1d46c 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36 3a 32  09/01/20 17:06:2
1d46d 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  7 danielk1977 Ex
1d46e 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53  p $.*/..#ifdef S
1d46f 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
1d470 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a 2a 2a  MIC_WRITE../*.**
1d471 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
1d472 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c 20  ments a special 
1d473 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f  kind of sqlite3_
1d474 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64  file object used
1d475 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f  .** by SQLite to
1d476 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20   create journal 
1d477 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f  files if the ato
1d478 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69  mic-write optimi
1d479 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61  zation.** is ena
1d47a 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  bled..**.** The 
1d47b 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 61 72  distinctive char
1d47c 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 74 68  acteristic of th
1d47d 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  is sqlite3_file 
1d47e 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61  is that the.** a
1d47f 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 69  ctual on disk fi
1d480 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 6c 61  le is created la
1d481 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 66  zily. When the f
1d482 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 0a  ile is created,.
1d483 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73 70  ** the caller sp
1d484 65 63 69 66 69 65 73 20 61 20 62 75 66 66 65 72  ecifies a buffer
1d485 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e 2d   size for an in-
1d486 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f  memory buffer to
1d487 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 73  .** be used to s
1d488 65 72 76 69 63 65 20 72 65 61 64 28 29 20 61 6e  ervice read() an
1d489 64 20 77 72 69 74 65 28 29 20 72 65 71 75 65 73  d write() reques
1d48a 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20 66  ts. The actual f
1d48b 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69  ile.** on disk i
1d48c 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72  s not created or
1d48d 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 6c   populated until
1d48e 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20   either:.**.**  
1d48f 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72   1) The in-memor
1d490 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  y representation
1d491 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 65   grows too large
1d492 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 74   for the allocat
1d493 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 66  ed .**      buff
1d494 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 54  er, or.**   2) T
1d495 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  he sqlite3Journa
1d496 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69  lCreate() functi
1d497 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  on is called..*/
1d498 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72  ..../*.** A Jour
1d499 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69  nalFile object i
1d49a 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20  s a subclass of 
1d49b 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65  sqlite3_file use
1d49c 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70  d by.** as an op
1d49d 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66  en file handle f
1d49e 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  or journal files
1d49f 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72  ..*/.struct Jour
1d4a0 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69  nalFile {.  sqli
1d4a1 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
1d4a2 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49  pMethod;    /* I
1d4a3 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f  /O methods on jo
1d4a4 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20  urnal files */. 
1d4a5 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20   int nBuf;      
1d4a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4a7 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66   /* Size of zBuf
1d4a8 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  [] in bytes */. 
1d4a9 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20   char *zBuf;    
1d4aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4ab 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66   /* Space to buf
1d4ac 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74  fer journal writ
1d4ad 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a  es */.  int iSiz
1d4ae 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1d4af 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e          /* Amoun
1d4b0 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72  t of zBuf[] curr
1d4b1 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20  ently used */.  
1d4b2 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
1d4b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4b4 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a  /* xOpen flags *
1d4b5 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  /.  sqlite3_vfs 
1d4b6 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20  *pVfs;          
1d4b7 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c      /* The "real
1d4b8 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53  " underlying VFS
1d4b9 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
1d4ba 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20  le *pReal;      
1d4bb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65        /* The "re
1d4bc 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66  al" underlying f
1d4bd 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
1d4be 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1d4bf 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20  zJournal;       
1d4c0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1d4c1 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1d4c2 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  */.};.typedef st
1d4c3 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  ruct JournalFile
1d4c4 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f   JournalFile;../
1d4c5 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20  *.** If it does 
1d4c6 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
1d4c7 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70  ts, create and p
1d4c8 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64  opulate the on-d
1d4c9 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72  isk file .** for
1d4ca 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a   JournalFile p..
1d4cb 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72  */.static int cr
1d4cc 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c  eateFile(Journal
1d4cd 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  File *p){.  int 
1d4ce 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1d4cf 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20    if( !p->pReal 
1d4d0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
1d4d1 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71  ile *pReal = (sq
1d4d2 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b  lite3_file *)&p[
1d4d3 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  1];.    rc = sql
1d4d4 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56  ite3OsOpen(p->pV
1d4d5 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  fs, p->zJournal,
1d4d6 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73   pReal, p->flags
1d4d7 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
1d4d8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1d4d9 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20       p->pReal = 
1d4da 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28  pReal;.      if(
1d4db 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20   p->iSize>0 ){. 
1d4dc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 2d         assert(p-
1d4dd 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29  >iSize<=p->nBuf)
1d4de 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
1d4df 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d  qlite3OsWrite(p-
1d4e0 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c  >pReal, p->zBuf,
1d4e1 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20   p->iSize, 0);. 
1d4e2 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1d4e3 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1d4e4 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65  ./*.** Close the
1d4e5 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1d4e6 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73   int jrnlClose(s
1d4e7 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1d4e8 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c  d){.  JournalFil
1d4e9 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46  e *p = (JournalF
1d4ea 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66  ile *)pJfd;.  if
1d4eb 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20  ( p->pReal ){.  
1d4ec 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
1d4ed 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a  (p->pReal);.  }.
1d4ee 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1d4ef 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72  ->zBuf);.  retur
1d4f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1d4f1 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  /*.** Read data 
1d4f2 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a  from the file..*
1d4f3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  /.static int jrn
1d4f4 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33  lRead(.  sqlite3
1d4f5 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20  _file *pJfd,    
1d4f6 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
1d4f7 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ile from which t
1d4f8 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64  o read */.  void
1d4f9 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20   *zBuf,         
1d4fa 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65     /* Put the re
1d4fb 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
1d4fc 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1d4fd 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1d4fe 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
1d4ff 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  d */.  sqlite_in
1d500 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1d501 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
1d502 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
1d503 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
1d504 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72  QLITE_OK;.  Jour
1d505 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1d506 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1d507 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ;.  if( p->pReal
1d508 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1d509 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52  ite3OsRead(p->pR
1d50a 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c  eal, zBuf, iAmt,
1d50b 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65   iOfst);.  }else
1d50c 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 73 74   if( (iAmt+iOfst
1d50d 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20  )>p->iSize ){.  
1d50e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
1d50f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a  ERR_SHORT_READ;.
1d510 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d    }else{.    mem
1d511 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42  cpy(zBuf, &p->zB
1d512 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29  uf[iOfst], iAmt)
1d513 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1d514 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  c;.}../*.** Writ
1d515 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69  e data to the fi
1d516 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1d517 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 73  t jrnlWrite(.  s
1d518 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1d519 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75  d,    /* The jou
1d51a 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77  rnal file into w
1d51b 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a 2f  hich to write */
1d51c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1d51d 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b  Buf,      /* Tak
1d51e 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69  e data to be wri
1d51f 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a  tten from here *
1d520 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20  /.  int iAmt,   
1d521 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1d522 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f  mber of bytes to
1d523 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69   write */.  sqli
1d524 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20  te_int64 iOfst  
1d525 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 74     /* Begin writ
1d526 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73  ing at this offs
1d527 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65  et into the file
1d528 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
1d529 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a  = SQLITE_OK;.  J
1d52a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20  ournalFile *p = 
1d52b 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1d52c 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70  Jfd;.  if( !p->p
1d52d 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b 69  Real && (iOfst+i
1d52e 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a  Amt)>p->nBuf ){.
1d52f 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 46      rc = createF
1d530 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66  ile(p);.  }.  if
1d531 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1d532 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 52  ){.    if( p->pR
1d533 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  eal ){.      rc 
1d534 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
1d535 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c  (p->pReal, zBuf,
1d536 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20   iAmt, iOfst);. 
1d537 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d538 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b  memcpy(&p->zBuf[
1d539 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41  iOfst], zBuf, iA
1d53a 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  mt);.      if( p
1d53b 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69  ->iSize<(iOfst+i
1d53c 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Amt) ){.        
1d53d 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f 66 73  p->iSize = (iOfs
1d53e 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d  t+iAmt);.      }
1d53f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1d540 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1d541 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 69   Truncate the fi
1d542 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1d543 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73  t jrnlTruncate(s
1d544 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1d545 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  d, sqlite_int64 
1d546 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  size){.  int rc 
1d547 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a  = SQLITE_OK;.  J
1d548 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20  ournalFile *p = 
1d549 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1d54a 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52  Jfd;.  if( p->pR
1d54b 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  eal ){.    rc = 
1d54c 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
1d54d 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65  e(p->pReal, size
1d54e 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  );.  }else if( s
1d54f 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a  ize<p->iSize ){.
1d550 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73      p->iSize = s
1d551 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ize;.  }.  retur
1d552 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
1d553 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  ync the file..*/
1d554 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
1d555 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c  Sync(sqlite3_fil
1d556 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61  e *pJfd, int fla
1d557 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  gs){.  int rc;. 
1d558 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
1d559 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
1d55a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e  )pJfd;.  if( p->
1d55b 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20  pReal ){.    rc 
1d55c 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
1d55d 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73 29  p->pReal, flags)
1d55e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
1d55f 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1d560 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1d561 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74  }../*.** Query t
1d562 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  he size of the f
1d563 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f  ile in bytes..*/
1d564 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
1d565 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33  FileSize(sqlite3
1d566 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c  _file *pJfd, sql
1d567 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65  ite_int64 *pSize
1d568 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1d569 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1d56a 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1d56b 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1d56c 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20  .  if( p->pReal 
1d56d 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1d56e 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d  te3OsFileSize(p-
1d56f 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a  >pReal, pSize);.
1d570 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53    }else{.    *pS
1d571 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e  ize = (sqlite_in
1d572 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20  t64) p->iSize;. 
1d573 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1d574 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f  }../*.** Table o
1d575 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f  f methods for Jo
1d576 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 74 65  urnalFile sqlite
1d577 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a  3_file object..*
1d578 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
1d579 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
1d57a 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65  ds JournalFileMe
1d57b 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20  thods = {.  1,  
1d57c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56             /* iV
1d57d 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c  ersion */.  jrnl
1d57e 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43  Close,     /* xC
1d57f 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65  lose */.  jrnlRe
1d580 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61  ad,      /* xRea
1d581 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65  d */.  jrnlWrite
1d582 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20  ,     /* xWrite 
1d583 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74  */.  jrnlTruncat
1d584 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65  e,  /* xTruncate
1d585 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20   */.  jrnlSync, 
1d586 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f       /* xSync */
1d587 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c  .  jrnlFileSize,
1d588 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a    /* xFileSize *
1d589 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
1d58a 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20     /* xLock */. 
1d58b 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1d58c 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20  /* xUnlock */.  
1d58d 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1d58e 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
1d58f 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Lock */.  0,    
1d590 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c           /* xFil
1d591 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c  eControl */.  0,
1d592 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d593 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20  xSectorSize */. 
1d594 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
1d595 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63  /* xDeviceCharac
1d596 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a  teristics */.};.
1d597 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a  ./* .** Open a j
1d598 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a  ournal file..*/.
1d599 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1d59a 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  nt sqlite3Journa
1d59b 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  lOpen(.  sqlite3
1d59c 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
1d59d 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74      /* The VFS t
1d59e 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 61 6c  o use for actual
1d59f 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63   file I/O */.  c
1d5a0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1d5a1 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ,         /* Nam
1d5a2 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
1d5a3 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74   file */.  sqlit
1d5a4 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1d5a5 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f        /* Preallo
1d5a6 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c  cated, blank fil
1d5a7 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e  e handle */.  in
1d5a8 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
1d5a9 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
1d5aa 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69  ing flags */.  i
1d5ab 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 20 20  nt nBuf         
1d5ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
1d5ad 65 73 20 62 75 66 66 65 72 65 64 20 62 65 66 6f  es buffered befo
1d5ae 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66  re opening the f
1d5af 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72  ile */.){.  Jour
1d5b0 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1d5b1 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1d5b2 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ;.  memset(p, 0,
1d5b3 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
1d5b4 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69 66  ize(pVfs));.  if
1d5b5 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20 20  ( nBuf>0 ){.    
1d5b6 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 65  p->zBuf = sqlite
1d5b7 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66  3MallocZero(nBuf
1d5b8 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 7a  );.    if( !p->z
1d5b9 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65 74  Buf ){.      ret
1d5ba 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1d5bb 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1d5bc 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
1d5bd 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
1d5be 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61  zName, pJfd, fla
1d5bf 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d  gs, 0);.  }.  p-
1d5c0 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72  >pMethod = &Jour
1d5c1 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a  nalFileMethods;.
1d5c2 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66    p->nBuf = nBuf
1d5c3 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66  ;.  p->flags = f
1d5c4 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72  lags;.  p->zJour
1d5c5 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70  nal = zName;.  p
1d5c6 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20  ->pVfs = pVfs;. 
1d5c7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1d5c8 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  K;.}../*.** If t
1d5c9 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 6f  he argument p po
1d5ca 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61  ints to a Journa
1d5cb 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c  lFile structure,
1d5cc 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79   and the underly
1d5cd 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20  ing.** file has 
1d5ce 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65  not yet been cre
1d5cf 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 74 20  ated, create it 
1d5d0 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  now..*/.SQLITE_P
1d5d1 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1d5d2 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28  e3JournalCreate(
1d5d3 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29  sqlite3_file *p)
1d5d4 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74 68  {.  if( p->pMeth
1d5d5 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c  ods!=&JournalFil
1d5d6 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  eMethods ){.    
1d5d7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1d5d8 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
1d5d9 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e  reateFile((Journ
1d5da 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a  alFile *)p);.}..
1d5db 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /* .** Return th
1d5dc 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1d5dd 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74  s required to st
1d5de 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c  ore a JournalFil
1d5df 65 20 74 68 61 74 20 75 73 65 73 20 76 66 73 0a  e that uses vfs.
1d5e0 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 74  ** pVfs to creat
1d5e1 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  e the underlying
1d5e2 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a   on-disk files..
1d5e3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d5e4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75  E int sqlite3Jou
1d5e5 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33  rnalSize(sqlite3
1d5e6 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 72  _vfs *pVfs){.  r
1d5e7 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f  eturn (pVfs->szO
1d5e8 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75  sFile+sizeof(Jou
1d5e9 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 65  rnalFile));.}.#e
1d5ea 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
1d5eb 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f 75  ***** End of jou
1d5ec 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  rnal.c *********
1d5ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d5ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d5ef 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1d5f0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1d5f1 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a   memjournal.c **
1d5f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d5f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d5f4 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
1d5f5 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a   October 7.**.**
1d5f6 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1d5f7 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1d5f8 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1d5f9 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1d5fa 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1d5fb 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1d5fc 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1d5fd 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1d5fe 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1d5ff 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1d600 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1d601 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1d602 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1d603 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1d604 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1d605 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1d606 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1d607 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d60a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d60b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1d60c 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1d60d 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69  ns code use to i
1d60e 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d  mplement an in-m
1d60f 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a  emory rollback j
1d610 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 69  ournal..** The i
1d611 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63  n-memory rollbac
1d612 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65  k journal is use
1d613 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 61  d to journal tra
1d614 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a  nsactions for.**
1d615 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 74 61   ":memory:" data
1d616 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e 20 74  bases and when t
1d617 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d  he journal_mode=
1d618 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 69 73  MEMORY pragma is
1d619 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23   used..**.** @(#
1d61a 29 20 24 49 64 3a 20 6d 65 6d 6a 6f 75 72 6e 61  ) $Id: memjourna
1d61b 6c 2e 63 2c 76 20 31 2e 38 20 32 30 30 38 2f 31  l.c,v 1.8 2008/1
1d61c 32 2f 32 30 20 30 32 3a 31 34 3a 34 30 20 64 72  2/20 02:14:40 dr
1d61d 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 46  h Exp $.*/../* F
1d61e 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65  orward reference
1d61f 73 20 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74  s to internal st
1d620 72 75 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65  ructures */.type
1d621 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f  def struct MemJo
1d622 75 72 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c  urnal MemJournal
1d623 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
1d624 20 46 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50   FilePoint FileP
1d625 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74  oint;.typedef st
1d626 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46  ruct FileChunk F
1d627 69 6c 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70  ileChunk;../* Sp
1d628 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
1d629 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
1d62a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e   is allocated in
1d62b 20 69 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a   increments of.*
1d62c 2a 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65  * this many byte
1d62d 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f  s..*/.#define JO
1d62e 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20  URNAL_CHUNKSIZE 
1d62f 31 30 32 34 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74  1024../* Macro t
1d630 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d  o find the minim
1d631 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69  um of two numeri
1d632 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66  c values..*/.#if
1d633 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e  ndef MIN.# defin
1d634 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c  e MIN(x,y) ((x)<
1d635 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e  (y)?(x):(y)).#en
1d636 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72  dif../*.** The r
1d637 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
1d638 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61  is composed of a
1d639 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
1d63a 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73  these structures
1d63b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65  ..*/.struct File
1d63c 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 43 68  Chunk {.  FileCh
1d63d 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  unk *pNext;     
1d63e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78            /* Nex
1d63f 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65 20 6a  t chunk in the j
1d640 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 7a  ournal */.  u8 z
1d641 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48  Chunk[JOURNAL_CH
1d642 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 43  UNKSIZE];   /* C
1d643 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 63  ontent of this c
1d644 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  hunk */.};../*.*
1d645 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
1d646 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 65 72   this object ser
1d647 76 65 73 20 61 73 20 61 20 63 75 72 73 6f 72 20  ves as a cursor 
1d648 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63  into the rollbac
1d649 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68  k journal..** Th
1d64a 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20  e cursor can be 
1d64b 65 69 74 68 65 72 20 66 6f 72 20 72 65 61 64 69  either for readi
1d64c 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a  ng or writing..*
1d64d 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50 6f 69  /.struct FilePoi
1d64e 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  nt {.  sqlite3_i
1d64f 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20  nt64 iOffset;   
1d650 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1d651 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e   from the beginn
1d652 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ing of the file 
1d653 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a  */.  FileChunk *
1d654 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 20 20  pChunk;         
1d655 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 69 63       /* Specific
1d656 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68 69 63   chunk into whic
1d657 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20  h cursor points 
1d658 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  */.};../*.** Thi
1d659 73 20 73 75 62 63 6c 61 73 73 20 69 73 20 61 20  s subclass is a 
1d65a 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
1d65b 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63 68 20  te3_file.  Each 
1d65c 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72  open memory-jour
1d65d 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 73  nal.** is an ins
1d65e 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 63 6c  tance of this cl
1d65f 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d  ass..*/.struct M
1d660 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 73 71  emJournal {.  sq
1d661 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
1d662 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a   *pMethod;    /*
1d663 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e 20 4d   Parent class. M
1d664 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a  UST BE FIRST */.
1d665 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 46 69    FileChunk *pFi
1d666 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  rst;            
1d667 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69 6e 2d    /* Head of in-
1d668 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c 69 73  memory chunk-lis
1d669 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74  t */.  FilePoint
1d66a 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20 20 20   endpoint;      
1d66b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1d66c 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  r to the end of 
1d66d 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69  the file */.  Fi
1d66e 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f 69 6e  lePoint readpoin
1d66f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
1d670 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
1d671 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20  end of the last 
1d672 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f  xRead() */.};../
1d673 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66  *.** Read data f
1d674 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  rom the file..*/
1d675 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a  .static int memj
1d676 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74  rnlRead(.  sqlit
1d677 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1d678 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1d679 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68   file from which
1d67a 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f   to read */.  vo
1d67b 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20  id *zBuf,       
1d67c 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20       /* Put the 
1d67d 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
1d67e 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20    int iAmt,     
1d67f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1d680 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72  er of bytes to r
1d681 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  ead */.  sqlite_
1d682 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20  int64 iOfst     
1d683 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67  /* Begin reading
1d684 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
1d685 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e  */.){.  MemJourn
1d686 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72  al *p = (MemJour
1d687 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38  nal *)pJfd;.  u8
1d688 20 2a 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20   *zOut = zBuf;. 
1d689 20 69 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d   int nRead = iAm
1d68a 74 3b 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f  t;.  int iChunkO
1d68b 66 66 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75  ffset;.  FileChu
1d68c 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 61  nk *pChunk;..  a
1d68d 73 73 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d  ssert( iOfst+iAm
1d68e 74 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69  t<=p->endpoint.i
1d68f 4f 66 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28  Offset );..  if(
1d690 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f   p->readpoint.iO
1d691 66 66 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20  ffset!=iOfst || 
1d692 69 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  iOfst==0 ){.    
1d693 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f  sqlite3_int64 iO
1d694 66 66 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  ff = 0;.    for(
1d695 70 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74  pChunk=p->pFirst
1d696 3b 20 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e  ; .        pChun
1d697 6b 20 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 4e  k && (iOff+JOURN
1d698 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d 69  AL_CHUNKSIZE)<=i
1d699 4f 66 73 74 3b 0a 20 20 20 20 20 20 20 20 70 43  Ofst;.        pC
1d69a 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65  hunk=pChunk->pNe
1d69b 78 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  xt.    ){.      
1d69c 69 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f  iOff += JOURNAL_
1d69d 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 7d  CHUNKSIZE;.    }
1d69e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43  .  }else{.    pC
1d69f 68 75 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 6f  hunk = p->readpo
1d6a0 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d 0a  int.pChunk;.  }.
1d6a1 0a 20 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20  .  iChunkOffset 
1d6a2 3d 20 28 69 6e 74 29 28 69 4f 66 73 74 25 4a 4f  = (int)(iOfst%JO
1d6a3 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29  URNAL_CHUNKSIZE)
1d6a4 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74  ;.  do {.    int
1d6a5 20 69 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e 41   iSpace = JOURNA
1d6a6 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43  L_CHUNKSIZE - iC
1d6a7 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20 20  hunkOffset;.    
1d6a8 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28  int nCopy = MIN(
1d6a9 6e 52 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c 5f  nRead, (JOURNAL_
1d6aa 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75  CHUNKSIZE - iChu
1d6ab 6e 6b 4f 66 66 73 65 74 29 29 3b 0a 20 20 20 20  nkOffset));.    
1d6ac 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70 43  memcpy(zOut, &pC
1d6ad 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68  hunk->zChunk[iCh
1d6ae 75 6e 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f 70  unkOffset], nCop
1d6af 79 29 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d 20  y);.    zOut += 
1d6b0 6e 43 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61 64  nCopy;.    nRead
1d6b1 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20   -= iSpace;.    
1d6b2 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 30  iChunkOffset = 0
1d6b3 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 52 65  ;.  } while( nRe
1d6b4 61 64 3e 3d 30 20 26 26 20 28 70 43 68 75 6e 6b  ad>=0 && (pChunk
1d6b5 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29 21  =pChunk->pNext)!
1d6b6 3d 30 20 26 26 20 6e 52 65 61 64 3e 30 20 29 3b  =0 && nRead>0 );
1d6b7 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e  .  p->readpoint.
1d6b8 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 73 74 2b  iOffset = iOfst+
1d6b9 69 41 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64 70  iAmt;.  p->readp
1d6ba 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 43  oint.pChunk = pC
1d6bb 68 75 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e 20  hunk;..  return 
1d6bc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
1d6bd 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 74  .** Write data t
1d6be 6f 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  o the file..*/.s
1d6bf 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e  tatic int memjrn
1d6c0 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65  lWrite(.  sqlite
1d6c1 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20  3_file *pJfd,   
1d6c2 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20   /* The journal 
1d6c3 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  file into which 
1d6c4 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 6f  to write */.  co
1d6c5 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20  nst void *zBuf, 
1d6c6 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 74       /* Take dat
1d6c7 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  a to be written 
1d6c8 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 69  from here */.  i
1d6c9 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20  nt iAmt,        
1d6ca 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d6cb 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74  of bytes to writ
1d6cc 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  e */.  sqlite_in
1d6cd 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1d6ce 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61   Begin writing a
1d6cf 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 69 6e  t this offset in
1d6d0 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29  to the file */.)
1d6d1 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a  {.  MemJournal *
1d6d2 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20  p = (MemJournal 
1d6d3 2a 29 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e 57  *)pJfd;.  int nW
1d6d4 72 69 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20 75  rite = iAmt;.  u
1d6d5 38 20 2a 7a 57 72 69 74 65 20 3d 20 28 75 38 20  8 *zWrite = (u8 
1d6d6 2a 29 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41 6e  *)zBuf;..  /* An
1d6d7 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
1d6d8 61 6c 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 6f  al file should o
1d6d9 6e 6c 79 20 65 76 65 72 20 62 65 20 61 70 70 65  nly ever be appe
1d6da 6e 64 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d 0a  nded to. Random.
1d6db 20 20 2a 2a 20 61 63 63 65 73 73 20 77 72 69 74    ** access writ
1d6dc 65 73 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69  es are not requi
1d6dd 72 65 64 20 62 79 20 73 71 6c 69 74 65 2e 0a 20  red by sqlite.. 
1d6de 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 69 4f 66   */.  assert(iOf
1d6df 73 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e  st==p->endpoint.
1d6e0 69 4f 66 66 73 65 74 29 3b 0a 20 20 55 4e 55 53  iOffset);.  UNUS
1d6e1 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 4f 66  ED_PARAMETER(iOf
1d6e2 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e  st);..  while( n
1d6e3 57 72 69 74 65 3e 30 20 29 7b 0a 20 20 20 20 46  Write>0 ){.    F
1d6e4 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b  ileChunk *pChunk
1d6e5 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70   = p->endpoint.p
1d6e6 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e 74 20 69  Chunk;.    int i
1d6e7 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69  ChunkOffset = (i
1d6e8 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e  nt)(p->endpoint.
1d6e9 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f  iOffset%JOURNAL_
1d6ea 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 20 20  CHUNKSIZE);.    
1d6eb 69 6e 74 20 69 53 70 61 63 65 20 3d 20 4d 49 4e  int iSpace = MIN
1d6ec 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c  (nWrite, JOURNAL
1d6ed 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68  _CHUNKSIZE - iCh
1d6ee 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20  unkOffset);..   
1d6ef 20 69 66 28 20 69 43 68 75 6e 6b 4f 66 66 73 65   if( iChunkOffse
1d6f0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  t==0 ){.      /*
1d6f1 20 4e 65 77 20 63 68 75 6e 6b 20 69 73 20 72 65   New chunk is re
1d6f2 71 75 69 72 65 64 20 74 6f 20 65 78 74 65 6e 64  quired to extend
1d6f3 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20   the file. */.  
1d6f4 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70      FileChunk *p
1d6f5 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  New = sqlite3_ma
1d6f6 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 69 6c 65  lloc(sizeof(File
1d6f7 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69  Chunk));.      i
1d6f8 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20  f( !pNew ){.    
1d6f9 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1d6fa 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20  E_IOERR_NOMEM;. 
1d6fb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65       }.      pNe
1d6fc 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  w->pNext = 0;.  
1d6fd 20 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20 29      if( pChunk )
1d6fe 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1d6ff 28 20 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20  ( p->pFirst );. 
1d700 20 20 20 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70         pChunk->p
1d701 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20  Next = pNew;.   
1d702 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d703 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 46    assert( !p->pF
1d704 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20  irst );.        
1d705 70 2d 3e 70 46 69 72 73 74 20 3d 20 70 4e 65 77  p->pFirst = pNew
1d706 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1d707 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75  p->endpoint.pChu
1d708 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d  nk = pNew;.    }
1d709 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d  ..    memcpy(&p-
1d70a 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b  >endpoint.pChunk
1d70b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f  ->zChunk[iChunkO
1d70c 66 66 73 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20  ffset], zWrite, 
1d70d 69 53 70 61 63 65 29 3b 0a 20 20 20 20 7a 57 72  iSpace);.    zWr
1d70e 69 74 65 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20  ite += iSpace;. 
1d70f 20 20 20 6e 57 72 69 74 65 20 2d 3d 20 69 53 70     nWrite -= iSp
1d710 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70  ace;.    p->endp
1d711 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20  oint.iOffset += 
1d712 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72  iSpace;.  }..  r
1d713 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1d714 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61  .}../*.** Trunca
1d715 74 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  te the file..*/.
1d716 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72  static int memjr
1d717 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  nlTruncate(sqlit
1d718 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73  e3_file *pJfd, s
1d719 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65  qlite_int64 size
1d71a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ){.  MemJournal 
1d71b 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c  *p = (MemJournal
1d71c 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43   *)pJfd;.  FileC
1d71d 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20  hunk *pChunk;.  
1d71e 61 73 73 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b  assert(size==0);
1d71f 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1d720 54 45 52 28 73 69 7a 65 29 3b 0a 20 20 70 43 68  TER(size);.  pCh
1d721 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b  unk = p->pFirst;
1d722 0a 20 20 77 68 69 6c 65 28 20 70 43 68 75 6e 6b  .  while( pChunk
1d723 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e   ){.    FileChun
1d724 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b  k *pTmp = pChunk
1d725 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70  ;.    pChunk = p
1d726 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20  Chunk->pNext;.  
1d727 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1d728 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Tmp);.  }.  sqli
1d729 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65  te3MemJournalOpe
1d72a 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65 74 75 72  n(pJfd);.  retur
1d72b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1d72c 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20  /*.** Close the 
1d72d 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1d72e 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65  int memjrnlClose
1d72f 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1d730 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54  Jfd){.  memjrnlT
1d731 72 75 6e 63 61 74 65 28 70 4a 66 64 2c 20 30 29  runcate(pJfd, 0)
1d732 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1d733 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
1d734 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a  Sync the file..*
1d735 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  /.static int mem
1d736 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33  jrnlSync(sqlite3
1d737 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20  _file *NotUsed, 
1d738 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20  int NotUsed2){. 
1d739 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1d73a 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
1d73b 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  sed2);.  return 
1d73c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
1d73d 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69  .** Query the si
1d73e 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
1d73f 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  n bytes..*/.stat
1d740 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69  ic int memjrnlFi
1d741 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  leSize(sqlite3_f
1d742 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74  ile *pJfd, sqlit
1d743 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b  e_int64 *pSize){
1d744 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70  .  MemJournal *p
1d745 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a   = (MemJournal *
1d746 29 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20  )pJfd;.  *pSize 
1d747 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  = (sqlite_int64)
1d748 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66   p->endpoint.iOf
1d749 66 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53  fset;.  return S
1d74a 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1d74b 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68  ** Table of meth
1d74c 6f 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e  ods for MemJourn
1d74d 61 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  al sqlite3_file 
1d74e 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69  object..*/.stati
1d74f 63 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  c struct sqlite3
1d750 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a  _io_methods MemJ
1d751 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20  ournalMethods = 
1d752 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  {.  1,          
1d753 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
1d754 6e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c  n */.  memjrnlCl
1d755 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f  ose,     /* xClo
1d756 73 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52  se */.  memjrnlR
1d757 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65  ead,      /* xRe
1d758 61 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57  ad */.  memjrnlW
1d759 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72  rite,     /* xWr
1d75a 69 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c  ite */.  memjrnl
1d75b 54 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54  Truncate,  /* xT
1d75c 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d  runcate */.  mem
1d75d 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f  jrnlSync,      /
1d75e 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d  * xSync */.  mem
1d75f 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f  jrnlFileSize,  /
1d760 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20  * xFileSize */. 
1d761 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1d762 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20     /* xLock */. 
1d763 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1d764 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f     /* xUnlock */
1d765 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
1d766 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
1d767 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20  servedLock */.  
1d768 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1d769 20 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f    /* xFileContro
1d76a 6c 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  l */.  0,       
1d76b 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63           /* xSec
1d76c 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20  torSize */.  0  
1d76d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d76e 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74  * xDeviceCharact
1d76f 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a  eristics */.};..
1d770 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f  /* .** Open a jo
1d771 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53  urnal file..*/.S
1d772 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1d773 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  id sqlite3MemJou
1d774 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33  rnalOpen(sqlite3
1d775 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20  _file *pJfd){.  
1d776 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20  MemJournal *p = 
1d777 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a  (MemJournal *)pJ
1d778 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20  fd;.  memset(p, 
1d779 30 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  0, sqlite3MemJou
1d77a 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70  rnalSize());.  p
1d77b 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d  ->pMethod = &Mem
1d77c 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a  JournalMethods;.
1d77d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1d77e 74 72 75 65 20 69 66 20 74 68 65 20 66 69 6c 65  true if the file
1d77f 2d 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61  -handle passed a
1d780 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73  s an argument is
1d781 20 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   .** an in-memor
1d782 79 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51  y journal .*/.SQ
1d783 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1d784 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75   sqlite3IsMemJou
1d785 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c  rnal(sqlite3_fil
1d786 65 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75  e *pJfd){.  retu
1d787 72 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64  rn pJfd->pMethod
1d788 73 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65  s==&MemJournalMe
1d789 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  thods;.}../* .**
1d78a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1d78b 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75  er of bytes requ
1d78c 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20  ired to store a 
1d78d 4d 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20  MemJournal that 
1d78e 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73  uses vfs.** pVfs
1d78f 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75   to create the u
1d790 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73  nderlying on-dis
1d791 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49  k files..*/.SQLI
1d792 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1d793 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
1d794 53 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65  Size(void){.  re
1d795 74 75 72 6e 20 73 69 7a 65 6f 66 28 4d 65 6d 4a  turn sizeof(MemJ
1d796 6f 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  ournal);.}../***
1d797 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1d798 6f 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20  of memjournal.c 
1d799 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d79a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d79b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1d79c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1d79d 6e 20 66 69 6c 65 20 77 61 6c 6b 65 72 2e 63 20  n file walker.c 
1d79e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d79f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1d7a1 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 31 36  * 2008 August 16
1d7a2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1d7a3 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1d7a4 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1d7a5 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1d7a6 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1d7a7 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1d7a8 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1d7a9 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1d7aa 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1d7ab 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1d7ac 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1d7ad 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1d7ae 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1d7af 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1d7b0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1d7b1 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1d7b2 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1d7b3 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1d7b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d7b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d7b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d7b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1d7b8 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1d7b9 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75  tains routines u
1d7ba 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20  sed for walking 
1d7bb 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20  the parser tree 
1d7bc 66 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 73 74  for.** an SQL st
1d7bd 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24  atement..**.** $
1d7be 49 64 3a 20 77 61 6c 6b 65 72 2e 63 2c 76 20 31  Id: walker.c,v 1
1d7bf 2e 32 20 32 30 30 39 2f 30 32 2f 31 39 20 31 34  .2 2009/02/19 14
1d7c0 3a 33 39 3a 32 35 20 64 61 6e 69 65 6c 6b 31 39  :39:25 danielk19
1d7c1 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a  77 Exp $.*/.../*
1d7c2 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72  .** Walk an expr
1d7c3 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e  ession tree.  In
1d7c4 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63  voke the callbac
1d7c5 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  k once for each 
1d7c6 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65  node.** of the e
1d7c7 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65  xpression, while
1d7c8 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e   decending.  (In
1d7c9 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68   other words, th
1d7ca 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73  e callback.** is
1d7cb 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72 65 20   invoked before 
1d7cc 76 69 73 69 74 69 6e 67 20 63 68 69 6c 64 72 65  visiting childre
1d7cd 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  n.).**.** The re
1d7ce 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20  turn value from 
1d7cf 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f  the callback sho
1d7d0 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68  uld be one of th
1d7d1 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74  e WRC_*.** const
1d7d2 61 6e 74 73 20 74 6f 20 73 70 65 63 69 66 79 20  ants to specify 
1d7d3 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 64 20 77  how to proceed w
1d7d4 69 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a  ith the walk..**
1d7d5 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e 74 69  .**    WRC_Conti
1d7d6 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 69 6e 75  nue      Continu
1d7d7 65 20 64 65 73 63 65 6e 64 69 6e 67 20 64 6f 77  e descending dow
1d7d8 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a  n the tree..**.*
1d7d9 2a 20 20 20 20 57 52 43 5f 50 72 75 6e 65 20 20  *    WRC_Prune  
1d7da 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65         Do not de
1d7db 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64  scend into child
1d7dc 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 61 6c 6c   nodes.  But all
1d7dd 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ow.**           
1d7de 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 77             the w
1d7df 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  alk to continue 
1d7e0 77 69 74 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64  with sibling nod
1d7e1 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43  es..**.**    WRC
1d7e2 5f 41 62 6f 72 74 20 20 20 20 20 20 20 20 20 44  _Abort         D
1d7e3 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61  o no more callba
1d7e4 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74 68 65  cks.  Unwind the
1d7e5 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20   stack and.**   
1d7e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d7e7 20 20 20 72 65 74 75 72 6e 20 74 68 65 20 74 6f     return the to
1d7e8 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c  p-level walk cal
1d7e9 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  l..**.** The ret
1d7ea 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  urn value from t
1d7eb 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 57  his routine is W
1d7ec 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 62 61 6e  RC_Abort to aban
1d7ed 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c  don the tree wal
1d7ee 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e  k.** and WRC_Con
1d7ef 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75  tinue to continu
1d7f0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1d7f1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1d7f2 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 20  WalkExpr(Walker 
1d7f3 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a  *pWalker, Expr *
1d7f4 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 72 63  pExpr){.  int rc
1d7f5 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  ;.  if( pExpr==0
1d7f6 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f   ) return WRC_Co
1d7f7 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20 70  ntinue;.  rc = p
1d7f8 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72 43 61 6c  Walker->xExprCal
1d7f9 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70  lback(pWalker, p
1d7fa 45 78 70 72 29 3b 0a 20 20 69 66 28 20 72 63 3d  Expr);.  if( rc=
1d7fb 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 29 7b  =WRC_Continue ){
1d7fc 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1d7fd 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72  WalkExpr(pWalker
1d7fe 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 20  , pExpr->pLeft) 
1d7ff 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1d800 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  rt;.    if( sqli
1d801 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1d802 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  ker, pExpr->pRig
1d803 68 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  ht) ) return WRC
1d804 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20  _Abort;.    if( 
1d805 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1d806 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
1d807 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66  ect) ){.      if
1d808 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  ( sqlite3WalkSel
1d809 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78  ect(pWalker, pEx
1d80a 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 29  pr->x.pSelect) )
1d80b 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1d80c 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  t;.    }else{.  
1d80d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
1d80e 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1d80f 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ker, pExpr->x.pL
1d810 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  ist) ) return WR
1d811 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  C_Abort;.    }. 
1d812 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26   }.  return rc &
1d813 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f   WRC_Abort;.}../
1d814 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
1d815 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20  3WalkExpr() for 
1d816 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  every expression
1d817 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 6e   in list p or un
1d818 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20  til.** an abort 
1d819 72 65 71 75 65 73 74 20 69 73 20 73 65 65 6e 2e  request is seen.
1d81a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1d81b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
1d81c 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65  lkExprList(Walke
1d81d 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72  r *pWalker, Expr
1d81e 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20  List *p){.  int 
1d81f 69 2c 20 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74  i, rc = WRC_Cont
1d820 69 6e 75 65 3b 0a 20 20 73 74 72 75 63 74 20 45  inue;.  struct E
1d821 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
1d822 74 65 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  tem;.  if( p ){.
1d823 20 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78      for(i=p->nEx
1d824 70 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20  pr, pItem=p->a; 
1d825 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b  i>0; i--, pItem+
1d826 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  +){.      if( sq
1d827 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57  lite3WalkExpr(pW
1d828 61 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45  alker, pItem->pE
1d829 78 70 72 29 20 29 20 72 65 74 75 72 6e 20 57 52  xpr) ) return WR
1d82a 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  C_Abort;.    }. 
1d82b 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26   }.  return rc &
1d82c 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
1d82d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6c 6c  ../*.** Walk all
1d82e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 73 73   expressions ass
1d82f 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 45 4c  ociated with SEL
1d830 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e  ECT statement p.
1d831 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 6f    Do.** not invo
1d832 6b 65 20 74 68 65 20 53 45 4c 45 43 54 20 63 61  ke the SELECT ca
1d833 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74  llback on p, but
1d834 20 64 6f 20 28 6f 66 20 63 6f 75 72 73 65 29 20   do (of course) 
1d835 69 6e 76 6f 6b 65 0a 2a 2a 20 61 6e 79 20 65 78  invoke.** any ex
1d836 70 72 20 63 61 6c 6c 62 61 63 6b 73 20 61 6e 64  pr callbacks and
1d837 20 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b   SELECT callback
1d838 73 20 74 68 61 74 20 63 6f 6d 65 20 66 72 6f 6d  s that come from
1d839 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20   subqueries..** 
1d83a 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  Return WRC_Abort
1d83b 20 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65   or WRC_Continue
1d83c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1d83d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
1d83e 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 57 61  alkSelectExpr(Wa
1d83f 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
1d840 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 66 28  elect *p){.  if(
1d841 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
1d842 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d  List(pWalker, p-
1d843 3e 70 45 4c 69 73 74 29 20 29 20 72 65 74 75 72  >pEList) ) retur
1d844 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69  n WRC_Abort;.  i
1d845 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  f( sqlite3WalkEx
1d846 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70  pr(pWalker, p->p
1d847 57 68 65 72 65 29 20 29 20 72 65 74 75 72 6e 20  Where) ) return 
1d848 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28  WRC_Abort;.  if(
1d849 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
1d84a 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d  List(pWalker, p-
1d84b 3e 70 47 72 6f 75 70 42 79 29 20 29 20 72 65 74  >pGroupBy) ) ret
1d84c 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1d84d 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1d84e 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d  Expr(pWalker, p-
1d84f 3e 70 48 61 76 69 6e 67 29 20 29 20 72 65 74 75  >pHaving) ) retu
1d850 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1d851 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  if( sqlite3WalkE
1d852 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c  xprList(pWalker,
1d853 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 20 29 20   p->pOrderBy) ) 
1d854 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1d855 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ;.  if( sqlite3W
1d856 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  alkExpr(pWalker,
1d857 20 70 2d 3e 70 4c 69 6d 69 74 29 20 29 20 72 65   p->pLimit) ) re
1d858 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1d859 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1d85a 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70  kExpr(pWalker, p
1d85b 2d 3e 70 4f 66 66 73 65 74 29 20 29 20 72 65 74  ->pOffset) ) ret
1d85c 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1d85d 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
1d85e 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  inue;.}../*.** W
1d85f 61 6c 6b 20 74 68 65 20 70 61 72 73 65 20 74 72  alk the parse tr
1d860 65 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ees associated w
1d861 69 74 68 20 61 6c 6c 20 73 75 62 71 75 65 72 69  ith all subqueri
1d862 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 46 52 4f  es in the.** FRO
1d863 4d 20 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45  M clause of SELE
1d864 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 20  CT statement p. 
1d865 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74   Do not invoke t
1d866 68 65 20 73 65 6c 65 63 74 0a 2a 2a 20 63 61 6c  he select.** cal
1d867 6c 62 61 63 6b 20 6f 6e 20 70 2c 20 62 75 74 20  lback on p, but 
1d868 64 6f 20 69 6e 76 6f 6b 65 20 69 74 20 6f 6e 20  do invoke it on 
1d869 65 61 63 68 20 46 52 4f 4d 20 63 6c 61 75 73 65  each FROM clause
1d86a 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 61 6e 64   subquery.** and
1d86b 20 6f 6e 20 61 6e 79 20 73 75 62 71 75 65 72 69   on any subqueri
1d86c 65 73 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20  es further down 
1d86d 69 6e 20 74 68 65 20 74 72 65 65 2e 20 20 52 65  in the tree.  Re
1d86e 74 75 72 6e 20 0a 2a 2a 20 57 52 43 5f 41 62 6f  turn .** WRC_Abo
1d86f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e 74 69 6e  rt or WRC_Contin
1d870 75 65 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ue;.*/.SQLITE_PR
1d871 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1d872 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28  3WalkSelectFrom(
1d873 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
1d874 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53   Select *p){.  S
1d875 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20  rcList *pSrc;.  
1d876 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
1d877 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
1d878 74 65 6d 3b 0a 0a 20 20 70 53 72 63 20 3d 20 70  tem;..  pSrc = p
1d879 2d 3e 70 53 72 63 3b 0a 20 20 69 66 28 20 70 53  ->pSrc;.  if( pS
1d87a 72 63 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  rc ){.    for(i=
1d87b 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65  pSrc->nSrc, pIte
1d87c 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20  m=pSrc->a; i>0; 
1d87d 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i--, pItem++){. 
1d87e 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1d87f 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b  WalkSelect(pWalk
1d880 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  er, pItem->pSele
1d881 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ct) ){.        r
1d882 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1d883 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1d884 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f   }.  return WRC_
1d885 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a  Continue;.} ../*
1d886 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  .** Call sqlite3
1d887 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65  WalkExpr() for e
1d888 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  very expression 
1d889 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d  in Select statem
1d88a 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65  ent p..** Invoke
1d88b 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
1d88c 63 74 28 29 20 66 6f 72 20 73 75 62 71 75 65 72  ct() for subquer
1d88d 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ies in the FROM 
1d88e 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e  clause and.** on
1d88f 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65   the compound se
1d890 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70  lect chain, p->p
1d891 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  Prior..**.** Ret
1d892 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  urn WRC_Continue
1d893 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f   under normal co
1d894 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 72  nditions.  Retur
1d895 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a  n WRC_Abort if.*
1d896 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 62  * there is an ab
1d897 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a  ort request..**.
1d898 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65 72  ** If the Walker
1d899 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61   does not have a
1d89a 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  n xSelectCallbac
1d89b 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f  k() then this ro
1d89c 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f  utine.** is a no
1d89d 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57 52  -op returning WR
1d89e 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53  C_Continue..*/.S
1d89f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1d8a0 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  t sqlite3WalkSel
1d8a1 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  ect(Walker *pWal
1d8a2 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  ker, Select *p){
1d8a3 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
1d8a4 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72   p==0 || pWalker
1d8a5 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  ->xSelectCallbac
1d8a6 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52  k==0 ) return WR
1d8a7 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63  C_Continue;.  rc
1d8a8 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b   = WRC_Continue;
1d8a9 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29 7b 0a  .  while( p  ){.
1d8aa 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72      rc = pWalker
1d8ab 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  ->xSelectCallbac
1d8ac 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20  k(pWalker, p);. 
1d8ad 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65 61     if( rc ) brea
1d8ae 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  k;.    if( sqlit
1d8af 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72  e3WalkSelectExpr
1d8b0 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72  (pWalker, p) ) r
1d8b1 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1d8b2 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1d8b3 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70  WalkSelectFrom(p
1d8b4 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74  Walker, p) ) ret
1d8b5 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1d8b6 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72     p = p->pPrior
1d8b7 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1d8b8 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d  c & WRC_Abort;.}
1d8b9 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1d8ba 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e  * End of walker.
1d8bb 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1d8bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8be 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1d8bf 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 65 73  * Begin file res
1d8c0 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  olve.c *********
1d8c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8c3 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67  /./*.** 2008 Aug
1d8c4 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 18.**.** The
1d8c5 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1d8c6 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1d8c7 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1d8c8 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1d8c9 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1d8ca 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1d8cb 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1d8cc 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1d8cd 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1d8ce 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1d8cf 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1d8d0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1d8d1 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1d8d2 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1d8d3 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1d8d4 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1d8d5 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1d8d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d8da 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1d8db 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72   file contains r
1d8dc 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72  outines used for
1d8dd 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 72   walking the par
1d8de 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20  ser tree and.** 
1d8df 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e  resolve all iden
1d8e0 74 69 66 69 65 72 73 20 62 79 20 61 73 73 6f 63  tifiers by assoc
1d8e1 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69 74 68  iating them with
1d8e2 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
1d8e3 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
1d8e4 6e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 65  n..**.** $Id: re
1d8e5 73 6f 6c 76 65 2e 63 2c 76 20 31 2e 32 30 20 32  solve.c,v 1.20 2
1d8e6 30 30 39 2f 30 33 2f 30 35 20 30 34 3a 32 33 3a  009/03/05 04:23:
1d8e7 34 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  47 shane Exp $.*
1d8e8 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68  /../*.** Turn th
1d8e9 65 20 70 45 78 70 72 20 65 78 70 72 65 73 73 69  e pExpr expressi
1d8ea 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73  on into an alias
1d8eb 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68   for the iCol-th
1d8ec 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a   column of the.*
1d8ed 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e 20  * result set in 
1d8ee 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  pEList..**.** If
1d8ef 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1d8f0 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d 70  column is a simp
1d8f1 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65  le column refere
1d8f2 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72  nce, then this r
1d8f3 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20  outine.** makes 
1d8f4 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 20  an exact copy.  
1d8f5 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 65  But for any othe
1d8f6 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 73  r kind of expres
1d8f7 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f  sion, this.** ro
1d8f8 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70  utine make a cop
1d8f9 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  y of the result 
1d8fa 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68  set column as th
1d8fb 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  e argument to th
1d8fc 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61  e.** TK_AS opera
1d8fd 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 20  tor.  The TK_AS 
1d8fe 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 20  operator causes 
1d8ff 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
1d900 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65  o be.** evaluate
1d901 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 20  d just once and 
1d902 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 20  then reused for 
1d903 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a  each alias..**.*
1d904 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72  * The reason for
1d905 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65   suppressing the
1d906 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 6e   TK_AS term when
1d907 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1d908 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63  is a simple.** c
1d909 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
1d90a 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  is so that the c
1d90b 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
1d90c 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a  will be recogniz
1d90d 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20  ed as.** usable 
1d90e 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69  by indices withi
1d90f 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
1d910 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f  se processing lo
1d911 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b  gic. .**.** Hack
1d912 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65  :  The TK_AS ope
1d913 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 74  rator is inhibit
1d914 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d  ed if zType[0]==
1d915 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e 73  'G'.  This means
1d916 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 52  .** that in a GR
1d917 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 74  OUP BY clause, t
1d918 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1d919 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 65   evaluated twice
1d91a 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20  .  Hence:.**.** 
1d91b 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f      SELECT rando
1d91c 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e  m()%5 AS x, coun
1d91d 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52  t(*) FROM tab GR
1d91e 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49  OUP BY x.**.** I
1d91f 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a  s equivalent to:
1d920 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
1d921 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20  T random()%5 AS 
1d922 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  x, count(*) FROM
1d923 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 61   tab GROUP BY ra
1d924 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54  ndom()%5.**.** T
1d925 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 6e  he result of ran
1d926 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 47  dom()%5 in the G
1d927 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69  ROUP BY clause i
1d928 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 65  s probably diffe
1d929 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  rent.** from the
1d92a 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 72   result in the r
1d92b 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d  esult-set.  We m
1d92c 69 67 68 74 20 66 69 78 20 74 68 69 73 20 73 6f  ight fix this so
1d92d 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68  meday.  Or.** th
1d92e 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 67  en again, we mig
1d92f 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61  ht not....*/.sta
1d930 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65  tic void resolve
1d931 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 2a  Alias(.  Parse *
1d932 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
1d933 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1d934 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
1d935 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f   *pEList,      /
1d936 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a  * A result set *
1d937 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20  /.  int iCol,   
1d938 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
1d939 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65  column in the re
1d93a 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45  sult set.  0..pE
1d93b 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f  List->nExpr-1 */
1d93c 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
1d93d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61            /* Tra
1d93e 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f  nsform this into
1d93f 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 65   an alias to the
1d940 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
1d941 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
1d942 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f 55  pe      /* "GROU
1d943 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f 72  P" or "ORDER" or
1d944 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72   "" */.){.  Expr
1d945 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20   *pOrig;        
1d946 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74     /* The iCol-t
1d947 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
1d948 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
1d949 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20  Expr *pDup;     
1d94a 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
1d94b 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c  f pOrig */.  sql
1d94c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
1d94d 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1d94e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
1d94f 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f  /..  assert( iCo
1d950 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c  l>=0 && iCol<pEL
1d951 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20  ist->nExpr );.  
1d952 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e  pOrig = pEList->
1d953 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20  a[iCol].pExpr;. 
1d954 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d   assert( pOrig!=
1d955 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1d956 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50  Orig->flags & EP
1d957 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64  _Resolved );.  d
1d958 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1d959 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
1d95a 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69  ExprDup(db, pOri
1d95b 67 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 44 75  g, 0);.  if( pDu
1d95c 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
1d95d 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
1d95e 79 28 64 62 2c 20 26 70 44 75 70 2d 3e 74 6f 6b  y(db, &pDup->tok
1d95f 65 6e 2c 20 26 70 4f 72 69 67 2d 3e 74 6f 6b 65  en, &pOrig->toke
1d960 6e 29 3b 0a 20 20 69 66 28 20 70 44 75 70 2d 3e  n);.  if( pDup->
1d961 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26  op!=TK_COLUMN &&
1d962 20 7a 54 79 70 65 5b 30 5d 21 3d 27 47 27 20 29   zType[0]!='G' )
1d963 7b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c  {.    pDup = sql
1d964 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
1d965 2c 20 54 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30  , TK_AS, pDup, 0
1d966 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44  , 0);.    if( pD
1d967 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  up==0 ) return;.
1d968 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e      if( pEList->
1d969 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d  a[iCol].iAlias==
1d96a 30 20 29 7b 0a 20 20 20 20 20 20 70 45 4c 69 73  0 ){.      pELis
1d96b 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61  t->a[iCol].iAlia
1d96c 73 20 3d 20 28 75 31 36 29 28 2b 2b 70 50 61 72  s = (u16)(++pPar
1d96d 73 65 2d 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20  se->nAlias);.   
1d96e 20 7d 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61   }.    pDup->iTa
1d96f 62 6c 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  ble = pEList->a[
1d970 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20  iCol].iAlias;.  
1d971 7d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66  }.  if( pExpr->f
1d972 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
1d973 6c 61 74 65 20 29 7b 0a 20 20 20 20 70 44 75 70  late ){.    pDup
1d974 2d 3e 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d  ->pColl = pExpr-
1d975 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 70  >pColl;.    pDup
1d976 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78  ->flags |= EP_Ex
1d977 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 20  pCollate;.  }.  
1d978 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72  sqlite3ExprClear
1d979 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d  (db, pExpr);.  m
1d97a 65 6d 63 70 79 28 70 45 78 70 72 2c 20 70 44 75  emcpy(pExpr, pDu
1d97b 70 2c 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 72  p, sizeof(*pExpr
1d97c 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  ));.  sqlite3DbF
1d97d 72 65 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d  ree(db, pDup);.}
1d97e 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68  ../*.** Given th
1d97f 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75  e name of a colu
1d980 6d 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58  mn of the form X
1d981 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a  .Y.Z or Y.Z or j
1d982 75 73 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a  ust Z, look up.*
1d983 2a 20 74 68 61 74 20 6e 61 6d 65 20 69 6e 20 74  * that name in t
1d984 68 65 20 73 65 74 20 6f 66 20 73 6f 75 72 63 65  he set of source
1d985 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63 4c   tables in pSrcL
1d986 69 73 74 20 61 6e 64 20 6d 61 6b 65 20 74 68 65  ist and make the
1d987 20 70 45 78 70 72 20 0a 2a 2a 20 65 78 70 72 65   pExpr .** expre
1d988 73 73 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72  ssion node refer
1d989 20 62 61 63 6b 20 74 6f 20 74 68 61 74 20 73 6f   back to that so
1d98a 75 72 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68  urce column.  Th
1d98b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e  e following chan
1d98c 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20  ges.** are made 
1d98d 74 6f 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20  to pExpr:.**.** 
1d98e 20 20 20 70 45 78 70 72 2d 3e 69 44 62 20 20 20     pExpr->iDb   
1d98f 20 20 20 20 20 20 20 20 53 65 74 20 74 68 65 20          Set the 
1d990 69 6e 64 65 78 20 69 6e 20 64 62 2d 3e 61 44 62  index in db->aDb
1d991 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  [] of the databa
1d992 73 65 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20  se X.**         
1d993 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d994 28 65 76 65 6e 20 69 66 20 58 20 69 73 20 69 6d  (even if X is im
1d995 70 6c 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45  plied)..**    pE
1d996 78 70 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 20  xpr->iTable     
1d997 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 75     Set to the cu
1d998 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
1d999 74 68 65 20 74 61 62 6c 65 20 6f 62 74 61 69 6e  the table obtain
1d99a 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ed.**           
1d99b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 72                fr
1d99c 6f 6d 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20  om pSrcList..** 
1d99d 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 20     pExpr->pTab  
1d99e 20 20 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74          Points t
1d99f 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  o the Table stru
1d9a0 63 74 75 72 65 20 6f 66 20 58 2e 59 20 28 65 76  cture of X.Y (ev
1d9a1 65 6e 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20  en if.**        
1d9a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d9a3 20 58 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 20   X and/or Y are 
1d9a4 69 6d 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20  implied.).**    
1d9a5 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20  pExpr->iColumn  
1d9a6 20 20 20 20 20 53 65 74 20 74 6f 20 74 68 65 20       Set to the 
1d9a7 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69  column number wi
1d9a8 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  thin the table..
1d9a9 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20  **    pExpr->op 
1d9aa 20 20 20 20 20 20 20 20 20 20 20 53 65 74 20 74             Set t
1d9ab 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20  o TK_COLUMN..** 
1d9ac 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20     pExpr->pLeft 
1d9ad 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 72          Any expr
1d9ae 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e  ession this poin
1d9af 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64  ts to is deleted
1d9b0 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 52  .**    pExpr->pR
1d9b1 69 67 68 74 20 20 20 20 20 20 20 20 41 6e 79 20  ight        Any 
1d9b2 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73 20  expression this 
1d9b3 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65 6c  points to is del
1d9b4 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eted..**.** The 
1d9b5 70 44 62 54 6f 6b 65 6e 20 69 73 20 74 68 65 20  pDbToken is the 
1d9b6 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
1d9b7 62 61 73 65 20 28 74 68 65 20 22 58 22 29 2e 20  base (the "X"). 
1d9b8 20 54 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20   This value may 
1d9b9 62 65 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69  be.** NULL meani
1d9ba 6e 67 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20  ng that name is 
1d9bb 6f 66 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20  of the form Y.Z 
1d9bc 6f 72 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c  or Z.  Any avail
1d9bd 61 62 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  able database.**
1d9be 20 63 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54   can be used.  T
1d9bf 68 65 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69  he pTableToken i
1d9c0 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1d9c1 65 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22  e table (the "Y"
1d9c2 29 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75  ).  This.** valu
1d9c3 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66  e can be NULL if
1d9c4 20 70 44 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73   pDbToken is als
1d9c5 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 70 54 61 62  o NULL.  If pTab
1d9c6 6c 65 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20  leToken is NULL 
1d9c7 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74  it.** means that
1d9c8 20 74 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65   the form of the
1d9c9 20 6e 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74   name is Z and t
1d9ca 68 61 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d  hat columns from
1d9cb 20 61 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61   any table.** ca
1d9cc 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  n be used..**.**
1d9cd 20 49 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e   If the name can
1d9ce 6e 6f 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20  not be resolved 
1d9cf 75 6e 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c  unambiguously, l
1d9d0 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  eave an error me
1d9d1 73 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72  ssage.** in pPar
1d9d2 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f  se and return no
1d9d3 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20  n-zero.  Return 
1d9d4 7a 65 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e  zero on success.
1d9d5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
1d9d6 6f 6f 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72  ookupName(.  Par
1d9d7 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
1d9d8 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
1d9d9 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f   context */.  To
1d9da 6b 65 6e 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20  ken *pDbToken,  
1d9db 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
1d9dc 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
1d9dd 69 6e 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20  ining table, or 
1d9de 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  NULL */.  Token 
1d9df 2a 70 54 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f  *pTableToken,  /
1d9e0 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20  * Name of table 
1d9e1 63 6f 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d  containing colum
1d9e2 6e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  n, or NULL */.  
1d9e3 54 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f  Token *pColumnTo
1d9e4 6b 65 6e 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ken, /* Name of 
1d9e5 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20  the column. */. 
1d9e6 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
1d9e7 43 2c 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d  C,    /* The nam
1d9e8 65 20 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74  e context used t
1d9e9 6f 20 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61  o resolve the na
1d9ea 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  me */.  Expr *pE
1d9eb 78 70 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20  xpr          /* 
1d9ec 4d 61 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e  Make this EXPR n
1d9ed 6f 64 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  ode point to the
1d9ee 20 73 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e   selected column
1d9ef 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a   */.){.  char *z
1d9f0 44 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  Db = 0;       /*
1d9f1 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
1d9f2 61 62 61 73 65 2e 20 20 54 68 65 20 22 58 22 20  abase.  The "X" 
1d9f3 69 6e 20 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68  in X.Y.Z */.  ch
1d9f4 61 72 20 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20  ar *zTab = 0;   
1d9f5 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
1d9f6 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 22 59  e table.  The "Y
1d9f7 22 20 69 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e  " in X.Y.Z or Y.
1d9f8 5a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  Z */.  char *zCo
1d9f9 6c 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e  l = 0;      /* N
1d9fa 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ame of the colum
1d9fb 6e 2e 20 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20  n.  The "Z" */. 
1d9fc 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
1d9fd 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1d9fe 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  unters */.  int 
1d9ff 63 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  cnt = 0;        
1da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1da01 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68   Number of match
1da02 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ing column names
1da03 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 54 61 62   */.  int cntTab
1da04 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1da05 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1da06 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 74 61  r of matching ta
1da07 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73  ble names */.  s
1da08 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1da09 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20  rse->db;        
1da0a 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1da0b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
1da0c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
1da0d 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20  item *pItem;    
1da0e 20 20 20 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f     /* Use for lo
1da0f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53 72 63 4c  oping over pSrcL
1da10 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73  ist items */.  s
1da11 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1da12 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20  em *pMatch = 0; 
1da13 20 2f 2a 20 54 68 65 20 6d 61 74 63 68 69 6e 67   /* The matching
1da14 20 70 53 72 63 4c 69 73 74 20 69 74 65 6d 20 2a   pSrcList item *
1da15 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
1da16 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20  *pTopNC = pNC;  
1da17 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6e        /* First n
1da18 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e 20 74 68  amecontext in th
1da19 65 20 6c 69 73 74 20 2a 2f 0a 20 20 53 63 68 65  e list */.  Sche
1da1a 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b  ma *pSchema = 0;
1da1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1da1c 20 53 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65   Schema of the e
1da1d 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 0a 20 20  xpression */..  
1da1e 61 73 73 65 72 74 28 20 70 4e 43 20 29 3b 20 2f  assert( pNC ); /
1da1f 2a 20 74 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65  * the name conte
1da20 78 74 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c  xt cannot be NUL
1da21 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  L. */.  assert( 
1da22 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 20 26 26 20  pColumnToken && 
1da23 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 2d 3e 7a 20  pColumnToken->z 
1da24 29 3b 20 2f 2a 20 54 68 65 20 5a 20 69 6e 20 58  ); /* The Z in X
1da25 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20 62 65 20 4e  .Y.Z cannot be N
1da26 55 4c 4c 20 2a 2f 0a 0a 20 20 2f 2a 20 44 65 71  ULL */..  /* Deq
1da27 75 6f 74 65 20 61 6e 64 20 7a 65 72 6f 2d 74 65  uote and zero-te
1da28 72 6d 69 6e 61 74 65 20 74 68 65 20 6e 61 6d 65  rminate the name
1da29 73 20 2a 2f 0a 20 20 7a 44 62 20 3d 20 73 71 6c  s */.  zDb = sql
1da2a 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
1da2b 6e 28 64 62 2c 20 70 44 62 54 6f 6b 65 6e 29 3b  n(db, pDbToken);
1da2c 0a 20 20 7a 54 61 62 20 3d 20 73 71 6c 69 74 65  .  zTab = sqlite
1da2d 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
1da2e 62 2c 20 70 54 61 62 6c 65 54 6f 6b 65 6e 29 3b  b, pTableToken);
1da2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65  .  zCol = sqlite
1da30 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
1da31 62 2c 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e 29  b, pColumnToken)
1da32 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
1da33 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
1da34 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f  goto lookupname_
1da35 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  end;.  }..  /* I
1da36 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e 6f  nitialize the no
1da37 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63 68 20 2a  de to no-match *
1da38 2f 0a 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  /.  pExpr->iTabl
1da39 65 20 3d 20 2d 31 3b 0a 20 20 70 45 78 70 72 2d  e = -1;.  pExpr-
1da3a 3e 70 54 61 62 20 3d 20 30 3b 0a 0a 20 20 2f 2a  >pTab = 0;..  /*
1da3b 20 53 74 61 72 74 20 61 74 20 74 68 65 20 69 6e   Start at the in
1da3c 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 74  ner-most context
1da3d 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 72   and move outwar
1da3e 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 20  d until a match 
1da3f 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 68  is found */.  wh
1da40 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d  ile( pNC && cnt=
1da41 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69  =0 ){.    ExprLi
1da42 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20  st *pEList;.    
1da43 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
1da44 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
1da45 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63  t;..    if( pSrc
1da46 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f  List ){.      fo
1da47 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72  r(i=0, pItem=pSr
1da48 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63  cList->a; i<pSrc
1da49 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
1da4a 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1da4b 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
1da4c 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b          int iDb;
1da4d 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  .        Column 
1da4e 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20  *pCol;.  .      
1da4f 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e    pTab = pItem->
1da50 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73  pTab;.        as
1da51 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26  sert( pTab!=0 &&
1da52 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20   pTab->zName!=0 
1da53 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d  );.        iDb =
1da54 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1da55 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
1da56 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
1da57 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
1da58 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20  nCol>0 );.      
1da59 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20    if( zTab ){.  
1da5a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65          if( pIte
1da5b 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20  m->zAlias ){.   
1da5c 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
1da5d 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d  TabName = pItem-
1da5e 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20  >zAlias;.       
1da5f 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1da60 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65  StrICmp(zTabName
1da61 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e  , zTab)!=0 ) con
1da62 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20  tinue;.         
1da63 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1da64 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61      char *zTabNa
1da65 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  me = pTab->zName
1da66 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
1da67 28 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c  ( zTabName==0 ||
1da68 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1da69 7a 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21  zTabName, zTab)!
1da6a 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
1da6b 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a             if( z
1da6c 44 62 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33  Db!=0 && sqlite3
1da6d 53 74 72 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b  StrICmp(db->aDb[
1da6e 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29  iDb].zName, zDb)
1da6f 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
1da70 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1da71 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1da72 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1da73 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 30   }.        if( 0
1da74 3d 3d 28 63 6e 74 54 61 62 2b 2b 29 20 29 7b 0a  ==(cntTab++) ){.
1da75 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1da76 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d  >iTable = pItem-
1da77 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  >iCursor;.      
1da78 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
1da79 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20  = pTab;.        
1da7a 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62    pSchema = pTab
1da7b 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20  ->pSchema;.     
1da7c 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49       pMatch = pI
1da7d 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  tem;.        }. 
1da7e 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20         for(j=0, 
1da7f 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b  pCol=pTab->aCol;
1da80 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
1da81 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ++, pCol++){.   
1da82 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
1da83 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e  e3StrICmp(pCol->
1da84 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20  zName, zCol)==0 
1da85 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 49  ){.            I
1da86 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b 0a 20  dList *pUsing;. 
1da87 20 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b             cnt++
1da88 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  ;.            pE
1da89 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49  xpr->iTable = pI
1da8a 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
1da8b 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1da8c 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20  >pTab = pTab;.  
1da8d 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
1da8e 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20   = pItem;.      
1da8f 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20        pSchema = 
1da90 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20  pTab->pSchema;. 
1da91 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75             /* Su
1da92 62 73 74 69 74 75 74 65 20 74 68 65 20 72 6f 77  bstitute the row
1da93 69 64 20 28 63 6f 6c 75 6d 6e 20 2d 31 29 20 66  id (column -1) f
1da94 6f 72 20 74 68 65 20 49 4e 54 45 47 45 52 20 50  or the INTEGER P
1da95 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20  RIMARY KEY */.  
1da96 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1da97 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a 3d 3d 70 54  >iColumn = j==pT
1da98 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a  ab->iPKey ? -1 :
1da99 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   j;.            
1da9a 69 66 28 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e  if( i<pSrcList->
1da9b 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20 20 20 20  nSrc-1 ){.      
1da9c 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65          if( pIte
1da9d 6d 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20  m[1].jointype & 
1da9e 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20  JT_NATURAL ){.  
1da9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1daa0 20 49 66 20 74 68 69 73 20 6d 61 74 63 68 20 6f   If this match o
1daa1 63 63 75 72 72 65 64 20 69 6e 20 74 68 65 20 6c  ccurred in the l
1daa2 65 66 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6e  eft table of a n
1daa3 61 74 75 72 61 6c 20 6a 6f 69 6e 2c 0a 20 20 20  atural join,.   
1daa4 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
1daa5 74 68 65 6e 20 73 6b 69 70 20 74 68 65 20 72 69  then skip the ri
1daa6 67 68 74 20 74 61 62 6c 65 20 74 6f 20 61 76 6f  ght table to avo
1daa7 69 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d  id a duplicate m
1daa8 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20  atch */.        
1daa9 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b          pItem++;
1daaa 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1daab 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20   i++;.          
1daac 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
1daad 55 73 69 6e 67 20 3d 20 70 49 74 65 6d 5b 31 5d  Using = pItem[1]
1daae 2e 70 55 73 69 6e 67 29 21 3d 30 20 29 7b 0a 20  .pUsing)!=0 ){. 
1daaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1dab0 2a 20 49 66 20 74 68 69 73 20 6d 61 74 63 68 20  * If this match 
1dab1 6f 63 63 75 72 73 20 6f 6e 20 61 20 63 6f 6c 75  occurs on a colu
1dab2 6d 6e 20 74 68 61 74 20 69 73 20 69 6e 20 74 68  mn that is in th
1dab3 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 0a 20  e USING clause. 
1dab4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
1dab5 2a 20 6f 66 20 61 20 6a 6f 69 6e 2c 20 73 6b 69  * of a join, ski
1dab6 70 20 74 68 65 20 73 65 61 72 63 68 20 6f 66 20  p the search of 
1dab7 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
1dab8 6f 66 20 74 68 65 20 6a 6f 69 6e 0a 20 20 20 20  of the join.    
1dab9 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
1daba 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63  o avoid a duplic
1dabb 61 74 65 20 6d 61 74 63 68 20 74 68 65 72 65 2e  ate match there.
1dabc 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
1dabd 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20      int k;.     
1dabe 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b             for(k
1dabf 3d 30 3b 20 6b 3c 70 55 73 69 6e 67 2d 3e 6e 49  =0; k<pUsing->nI
1dac0 64 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20  d; k++){.       
1dac1 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73             if( s
1dac2 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 55  qlite3StrICmp(pU
1dac3 73 69 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65  sing->a[k].zName
1dac4 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20  , zCol)==0 ){.  
1dac5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dac6 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20    pItem++;.     
1dac7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
1dac8 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
1dac9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1daca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dacb 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
1dacc 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
1dacd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
1dace 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 62   }.            b
1dacf 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
1dad0 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
1dad1 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64    }.    }..#ifnd
1dad2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1dad3 52 49 47 47 45 52 0a 20 20 20 20 2f 2a 20 49 66  RIGGER.    /* If
1dad4 20 77 65 20 68 61 76 65 20 6e 6f 74 20 61 6c 72   we have not alr
1dad5 65 61 64 79 20 72 65 73 6f 6c 76 65 64 20 74 68  eady resolved th
1dad6 65 20 6e 61 6d 65 2c 20 74 68 65 6e 20 6d 61 79  e name, then may
1dad7 62 65 20 0a 20 20 20 20 2a 2a 20 69 74 20 69 73  be .    ** it is
1dad8 20 61 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e   a new.* or old.
1dad9 2a 20 74 72 69 67 67 65 72 20 61 72 67 75 6d 65  * trigger argume
1dada 6e 74 20 72 65 66 65 72 65 6e 63 65 0a 20 20 20  nt reference.   
1dadb 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62 3d   */.    if( zDb=
1dadc 3d 30 20 26 26 20 7a 54 61 62 21 3d 30 20 26 26  =0 && zTab!=0 &&
1dadd 20 63 6e 74 3d 3d 30 20 26 26 20 70 50 61 72 73   cnt==0 && pPars
1dade 65 2d 3e 74 72 69 67 53 74 61 63 6b 21 3d 30 20  e->trigStack!=0 
1dadf 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72  ){.      Trigger
1dae0 53 74 61 63 6b 20 2a 70 54 72 69 67 67 65 72 53  Stack *pTriggerS
1dae1 74 61 63 6b 20 3d 20 70 50 61 72 73 65 2d 3e 74  tack = pParse->t
1dae2 72 69 67 53 74 61 63 6b 3b 0a 20 20 20 20 20 20  rigStack;.      
1dae3 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b  Table *pTab = 0;
1dae4 0a 20 20 20 20 20 20 75 33 32 20 2a 70 69 43 6f  .      u32 *piCo
1dae5 6c 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 20  lMask = 0;.     
1dae6 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74 61   if( pTriggerSta
1dae7 63 6b 2d 3e 6e 65 77 49 64 78 20 21 3d 20 2d 31  ck->newIdx != -1
1dae8 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1dae9 6d 70 28 22 6e 65 77 22 2c 20 7a 54 61 62 29 20  mp("new", zTab) 
1daea 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20  == 0 ){.        
1daeb 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
1daec 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e  pTriggerStack->n
1daed 65 77 49 64 78 3b 0a 20 20 20 20 20 20 20 20 61  ewIdx;.        a
1daee 73 73 65 72 74 28 20 70 54 72 69 67 67 65 72 53  ssert( pTriggerS
1daef 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20  tack->pTab );.  
1daf0 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 72        pTab = pTr
1daf1 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62  iggerStack->pTab
1daf2 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f 6c 4d  ;.        piColM
1daf3 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67 65 72  ask = &(pTrigger
1daf4 53 74 61 63 6b 2d 3e 6e 65 77 43 6f 6c 4d 61 73  Stack->newColMas
1daf5 6b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  k);.      }else 
1daf6 69 66 28 20 70 54 72 69 67 67 65 72 53 74 61 63  if( pTriggerStac
1daf7 6b 2d 3e 6f 6c 64 49 64 78 20 21 3d 20 2d 31 20  k->oldIdx != -1 
1daf8 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
1daf9 70 28 22 6f 6c 64 22 2c 20 7a 54 61 62 29 3d 3d  p("old", zTab)==
1dafa 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78  0 ){.        pEx
1dafb 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 54 72  pr->iTable = pTr
1dafc 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 49  iggerStack->oldI
1dafd 64 78 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  dx;.        asse
1dafe 72 74 28 20 70 54 72 69 67 67 65 72 53 74 61 63  rt( pTriggerStac
1daff 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20 20 20 20  k->pTab );.     
1db00 20 20 20 70 54 61 62 20 3d 20 70 54 72 69 67 67     pTab = pTrigg
1db01 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20  erStack->pTab;. 
1db02 20 20 20 20 20 20 20 70 69 43 6f 6c 4d 61 73 6b         piColMask
1db03 20 3d 20 26 28 70 54 72 69 67 67 65 72 53 74 61   = &(pTriggerSta
1db04 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d 61 73 6b 29 3b  ck->oldColMask);
1db05 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1db06 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20 20 20  if( pTab ){ .   
1db07 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20       int iCol;. 
1db08 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70         Column *p
1db09 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  Col = pTab->aCol
1db0a 3b 0a 0a 20 20 20 20 20 20 20 20 70 53 63 68 65  ;..        pSche
1db0b 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
1db0c 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 54  ma;.        cntT
1db0d 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 66 6f  ab++;.        fo
1db0e 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c  r(iCol=0; iCol <
1db0f 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f   pTab->nCol; iCo
1db10 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 20 7b 0a 20  l++, pCol++) {. 
1db11 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
1db12 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c  ite3StrICmp(pCol
1db13 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d  ->zName, zCol)==
1db14 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1db15 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20   cnt++;.        
1db16 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
1db17 6d 6e 20 3d 20 69 43 6f 6c 3d 3d 70 54 61 62 2d  mn = iCol==pTab-
1db18 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20 69 43  >iPKey ? -1 : iC
1db19 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ol;.            
1db1a 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54  pExpr->pTab = pT
1db1b 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ab;.            
1db1c 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20  if( iCol>=0 ){. 
1db1d 20 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73               tes
1db1e 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 31 20  tcase( iCol==31 
1db1f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
1db20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d   testcase( iCol=
1db21 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 20  =32 );.         
1db22 20 20 20 20 20 2a 70 69 43 6f 6c 4d 61 73 6b 20       *piColMask 
1db23 7c 3d 20 28 28 75 33 32 29 31 3c 3c 69 43 6f 6c  |= ((u32)1<<iCol
1db24 29 20 7c 20 28 69 43 6f 6c 3e 3d 33 32 3f 30 78  ) | (iCol>=32?0x
1db25 66 66 66 66 66 66 66 66 3a 30 29 3b 0a 20 20 20  ffffffff:0);.   
1db26 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1db27 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1db28 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1db29 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1db2a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
1db2b 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1db2c 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20 20  _TRIGGER) */..  
1db2d 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72 68    /*.    ** Perh
1db2e 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73 20  aps the name is 
1db2f 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
1db30 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f 0a  he ROWID.    */.
1db31 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26      if( cnt==0 &
1db32 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20 73  & cntTab==1 && s
1db33 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43  qlite3IsRowid(zC
1db34 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e 74  ol) ){.      cnt
1db35 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78 70   = 1;.      pExp
1db36 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b  r->iColumn = -1;
1db37 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66  .      pExpr->af
1db38 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
1db39 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
1db3a 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   }..    /*.    *
1db3b 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 69  * If the input i
1db3c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a 20  s of the form Z 
1db3d 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59 2e  (not Y.Z or X.Y.
1db3e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65  Z) then the name
1db3f 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20   Z.    ** might 
1db40 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73 75  refer to an resu
1db41 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 54  lt-set alias.  T
1db42 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72  his happens, for
1db43 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 20   example, when. 
1db44 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 73     ** we are res
1db45 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20  olving names in 
1db46 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1db47 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
1db48 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20 2a  g command:.    *
1db49 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45 4c  *.    **     SEL
1db4a 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52 4f  ECT a+b AS x FRO
1db4b 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78 3c  M table WHERE x<
1db4c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  10;.    **.    *
1db4d 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65 20  * In cases like 
1db4e 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70 45  this, replace pE
1db4f 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 79 20  xpr with a copy 
1db50 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
1db51 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66 6f  n that.    ** fo
1db52 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20 73  rms the result s
1db53 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22 20  et entry ("a+b" 
1db54 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29 20  in the example) 
1db55 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 64  and return immed
1db56 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 4e  iately..    ** N
1db57 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 78 70  ote that the exp
1db58 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72  ression in the r
1db59 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c 64  esult set should
1db5a 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
1db5b 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76  en.    ** resolv
1db5c 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 20 74  ed by the time t
1db5d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1db5e 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20  is resolved..   
1db5f 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d   */.    if( cnt=
1db60 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d 20  =0 && (pEList = 
1db61 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30 20  pNC->pEList)!=0 
1db62 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20 20  && zTab==0 ){.  
1db63 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
1db64 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a 2b  EList->nExpr; j+
1db65 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72  +){.        char
1db66 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e   *zAs = pEList->
1db67 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  a[j].zName;.    
1db68 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26      if( zAs!=0 &
1db69 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
1db6a 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29  (zAs, zCol)==0 )
1db6b 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72  {.          Expr
1db6c 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 20   *pOrig;.       
1db6d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1db6e 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45  ->pLeft==0 && pE
1db6f 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29  xpr->pRight==0 )
1db70 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
1db71 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  rt( pExpr->x.pLi
1db72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  st==0 );.       
1db73 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1db74 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 29  ->x.pSelect==0 )
1db75 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 69  ;.          pOri
1db76 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d  g = pEList->a[j]
1db77 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
1db78 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f    if( !pNC->allo
1db79 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73 50  wAgg && ExprHasP
1db7a 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45  roperty(pOrig, E
1db7b 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20  P_Agg) ){.      
1db7c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1db7d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d  orMsg(pParse, "m
1db7e 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64  isuse of aliased
1db7f 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c 20   aggregate %s", 
1db80 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  zAs);.          
1db81 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1db82 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20  db, zCol);.     
1db83 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
1db84 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1db85 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c         resolveAl
1db86 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69  ias(pParse, pELi
1db87 73 74 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22  st, j, pExpr, ""
1db88 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74  );.          cnt
1db89 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
1db8a 70 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20  pMatch = 0;.    
1db8b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 54        assert( zT
1db8c 61 62 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20  ab==0 && zDb==0 
1db8d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
1db8e 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64  o lookupname_end
1db8f 5f 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  _2;.        }.  
1db90 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20      } .    }..  
1db91 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20    /* Advance to 
1db92 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f  the next name co
1db93 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70  ntext.  The loop
1db94 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20   will exit when 
1db95 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65  either.    ** we
1db96 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 28 63   have a match (c
1db97 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65  nt>0) or when we
1db98 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65   run out of name
1db99 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a   contexts..    *
1db9a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30  /.    if( cnt==0
1db9b 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20   ){.      pNC = 
1db9c 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pNC->pNext;.    
1db9d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }.  }..  /*.  **
1db9e 20 49 66 20 58 20 61 6e 64 20 59 20 61 72 65 20   If X and Y are 
1db9f 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77  NULL (in other w
1dba0 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65  ords if only the
1dba1 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69   column name Z i
1dba2 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29  s.  ** supplied)
1dba3 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f   and the value o
1dba4 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20  f Z is enclosed 
1dba5 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73  in double-quotes
1dba6 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73  , then.  ** Z is
1dba7 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61   a string litera
1dba8 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20  l if it doesn't 
1dba9 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e  match any column
1dbaa 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74   names.  In that
1dbab 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e  .  ** case, we n
1dbac 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69  eed to return ri
1dbad 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74  ght away and not
1dbae 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65   make any change
1dbaf 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e  s to.  ** pExpr.
1dbb0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75  .  **.  ** Becau
1dbb1 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20  se no reference 
1dbb2 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65  was made to oute
1dbb3 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20  r contexts, the 
1dbb4 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66  pNC->nRef.  ** f
1dbb5 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68  ields are not ch
1dbb6 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e  anged in any con
1dbb7 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  text..  */.  if(
1dbb8 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d   cnt==0 && zTab=
1dbb9 3d 30 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b  =0 && pColumnTok
1dbba 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 7b  en->z[0]=='"' ){
1dbbb 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1dbbc 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20  ee(db, zCol);.  
1dbbd 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
1dbbe 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 78  _STRING;.    pEx
1dbbf 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20  pr->pTab = 0;.  
1dbc0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1dbc1 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d  .  /*.  ** cnt==
1dbc2 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61  0 means there wa
1dbc3 73 20 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e  s not match.  cn
1dbc4 74 3e 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20  t>1 means there 
1dbc5 77 65 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a 2a  were two or.  **
1dbc6 20 6d 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20   more matches.  
1dbc7 45 69 74 68 65 72 20 77 61 79 2c 20 77 65 20 68  Either way, we h
1dbc8 61 76 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  ave an error..  
1dbc9 2a 2f 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 20  */.  if( cnt!=1 
1dbca 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
1dbcb 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 72  r *zErr;.    zEr
1dbcc 72 20 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f  r = cnt==0 ? "no
1dbcd 20 73 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a 20   such column" : 
1dbce 22 61 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 6d  "ambiguous colum
1dbcf 6e 20 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 28  n name";.    if(
1dbd0 20 7a 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71   zDb ){.      sq
1dbd1 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1dbd2 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73  arse, "%s: %s.%s
1dbd3 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 2c  .%s", zErr, zDb,
1dbd4 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20   zTab, zCol);.  
1dbd5 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 62    }else if( zTab
1dbd6 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1dbd7 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1dbd8 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a  , "%s: %s.%s", z
1dbd9 45 72 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29  Err, zTab, zCol)
1dbda 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1dbdb 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1dbdc 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20  sg(pParse, "%s: 
1dbdd 25 73 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c 29  %s", zErr, zCol)
1dbde 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 70  ;.    }.    pTop
1dbdf 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a  NC->nErr++;.  }.
1dbe0 0a 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d  .  /* If a colum
1dbe1 6e 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 69  n from a table i
1dbe2 6e 20 70 53 72 63 4c 69 73 74 20 69 73 20 72 65  n pSrcList is re
1dbe3 66 65 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 72  ferenced, then r
1dbe4 65 63 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 20  ecord.  ** this 
1dbe5 66 61 63 74 20 69 6e 20 74 68 65 20 70 53 72 63  fact in the pSrc
1dbe6 4c 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64  List.a[].colUsed
1dbe7 20 62 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d   bitmask.  Colum
1dbe8 6e 20 30 20 63 61 75 73 65 73 0a 20 20 2a 2a 20  n 0 causes.  ** 
1dbe9 62 69 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e  bit 0 to be set.
1dbea 20 20 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20    Column 1 sets 
1dbeb 62 69 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66  bit 1.  And so f
1dbec 6f 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20  orth.  If the.  
1dbed 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  ** column number
1dbee 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
1dbef 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
1dbf0 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61  its in the bitma
1dbf1 73 6b 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74  sk.  ** then set
1dbf2 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20   the high-order 
1dbf3 62 69 74 20 6f 66 20 74 68 65 20 62 69 74 6d 61  bit of the bitma
1dbf4 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  sk..  */.  if( p
1dbf5 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30  Expr->iColumn>=0
1dbf6 20 26 26 20 70 4d 61 74 63 68 21 3d 30 20 29 7b   && pMatch!=0 ){
1dbf7 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78  .    int n = pEx
1dbf8 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
1dbf9 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 42 4d   testcase( n==BM
1dbfa 53 2d 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e  S-1 );.    if( n
1dbfb 3e 3d 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 6e  >=BMS ){.      n
1dbfc 20 3d 20 42 4d 53 2d 31 3b 0a 20 20 20 20 7d 0a   = BMS-1;.    }.
1dbfd 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 61 74      assert( pMat
1dbfe 63 68 2d 3e 69 43 75 72 73 6f 72 3d 3d 70 45 78  ch->iCursor==pEx
1dbff 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20  pr->iTable );.  
1dc00 20 20 70 4d 61 74 63 68 2d 3e 63 6f 6c 55 73 65    pMatch->colUse
1dc01 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29 31  d |= ((Bitmask)1
1dc02 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a 6c 6f 6f 6b 75  )<<n;.  }..looku
1dc03 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 2f 2a 20  pname_end:.  /* 
1dc04 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74  Clean up and ret
1dc05 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  urn.  */.  sqlit
1dc06 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 44 62  e3DbFree(db, zDb
1dc07 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1dc08 65 65 28 64 62 2c 20 7a 54 61 62 29 3b 0a 20 20  ee(db, zTab);.  
1dc09 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1dc0a 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65  e(db, pExpr->pLe
1dc0b 66 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c  ft);.  pExpr->pL
1dc0c 65 66 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  eft = 0;.  sqlit
1dc0d 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
1dc0e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b   pExpr->pRight);
1dc0f 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  .  pExpr->pRight
1dc10 20 3d 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e 6f   = 0;.  pExpr->o
1dc11 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 6c  p = TK_COLUMN;.l
1dc12 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32 3a  ookupname_end_2:
1dc13 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1dc14 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69 66  (db, zCol);.  if
1dc15 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 20  ( cnt==1 ){.    
1dc16 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29  assert( pNC!=0 )
1dc17 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74  ;.    sqlite3Aut
1dc18 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 70 45  hRead(pParse, pE
1dc19 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 70 4e  xpr, pSchema, pN
1dc1a 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20 20  C->pSrcList);.  
1dc1b 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74    /* Increment t
1dc1c 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f 6e  he nRef value on
1dc1d 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65 78   all name contex
1dc1e 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75 70  ts from TopNC up
1dc1f 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 70   to.    ** the p
1dc20 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 20 6e  oint where the n
1dc21 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f 0a  ame matched. */.
1dc22 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20      for(;;){.   
1dc23 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 70 4e     assert( pTopN
1dc24 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 54  C!=0 );.      pT
1dc25 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  opNC->nRef++;.  
1dc26 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 3d 3d      if( pTopNC==
1dc27 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  pNC ) break;.   
1dc28 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f 70     pTopNC = pTop
1dc29 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  NC->pNext;.    }
1dc2a 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
1dc2b 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65   } else {.    re
1dc2c 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 1;.  }.}../
1dc2d 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1dc2e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f  e is callback fo
1dc2f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70  r sqlite3WalkExp
1dc30 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f 6c  r()..**.** Resol
1dc31 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65  ve symbolic name
1dc32 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e  s into TK_COLUMN
1dc33 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 20 74   operators for t
1dc34 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e 6f  he current.** no
1dc35 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  de in the expres
1dc36 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75  sion tree.  Retu
1dc37 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75 65  rn 0 to continue
1dc38 20 74 68 65 20 73 65 61 72 63 68 20 64 6f 77 6e   the search down
1dc39 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f 72 20  .** the tree or 
1dc3a 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20 74  2 to abort the t
1dc3b 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20  ree walk..**.** 
1dc3c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73  This routine als
1dc3d 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 68 65  o does error che
1dc3e 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20 72  cking and name r
1dc3f 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a 2a  esolution for.**
1dc40 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e   function names.
1dc41 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 66    The operator f
1dc42 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  or aggregate fun
1dc43 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e 67 65  ctions is change
1dc44 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f 46  d.** to TK_AGG_F
1dc45 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74  UNCTION..*/.stat
1dc46 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 45 78  ic int resolveEx
1dc47 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70  prStep(Walker *p
1dc48 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45  Walker, Expr *pE
1dc49 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74  xpr){.  NameCont
1dc4a 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72 73  ext *pNC;.  Pars
1dc4b 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70 4e  e *pParse;..  pN
1dc4c 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70  C = pWalker->u.p
1dc4d 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e  NC;.  assert( pN
1dc4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65  C!=0 );.  pParse
1dc4f 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a   = pNC->pParse;.
1dc50 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
1dc51 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73  ==pWalker->pPars
1dc52 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 70 72  e );..  if( Expr
1dc53 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
1dc54 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65  Expr, EP_Resolve
1dc55 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  d) ) return WRC_
1dc56 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 65 74  Prune;.  ExprSet
1dc57 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
1dc58 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23 69  EP_Resolved);.#i
1dc59 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
1dc5a 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74  f( pNC->pSrcList
1dc5b 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73   && pNC->pSrcLis
1dc5c 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a 20  t->nAlloc>0 ){. 
1dc5d 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
1dc5e 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63  List = pNC->pSrc
1dc5f 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b  List;.    int i;
1dc60 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1dc61 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e  pNC->pSrcList->n
1dc62 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Src; i++){.     
1dc63 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 73   assert( pSrcLis
1dc64 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3e  t->a[i].iCursor>
1dc65 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e  =0 && pSrcList->
1dc66 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 61  a[i].iCursor<pPa
1dc67 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20 20  rse->nTab);.    
1dc68 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73  }.  }.#endif.  s
1dc69 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
1dc6a 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64   ){..#if defined
1dc6b 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55  (SQLITE_ENABLE_U
1dc6c 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d  PDATE_DELETE_LIM
1dc6d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  IT) && !defined(
1dc6e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1dc6f 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 68 65  UERY).    /* The
1dc70 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f   special operato
1dc71 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20 75  r TK_ROW means u
1dc72 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72  se the rowid for
1dc73 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 2a   the first.    *
1dc74 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  * column in the 
1dc75 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68  FROM clause.  Th
1dc76 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68  is is used by th
1dc77 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44 45  e LIMIT and ORDE
1dc78 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75  R BY.    ** clau
1dc79 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 6e  se processing on
1dc7a 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 4c 45   UPDATE and DELE
1dc7b 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20  TE statements.. 
1dc7c 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54     */.    case T
1dc7d 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20 53  K_ROW: {.      S
1dc7e 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74  rcList *pSrcList
1dc7f 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
1dc80 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53  ;.      struct S
1dc81 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
1dc82 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  em;.      assert
1dc83 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 70 53  ( pSrcList && pS
1dc84 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20  rcList->nSrc==1 
1dc85 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 20 3d  );.      pItem =
1dc86 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a 20   pSrcList->a; . 
1dc87 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
1dc88 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20   TK_COLUMN;.    
1dc89 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
1dc8a 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  pItem->pTab;.   
1dc8b 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1dc8c 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f   = pItem->iCurso
1dc8d 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  r;.      pExpr->
1dc8e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20  iColumn = -1;.  
1dc8f 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e      pExpr->affin
1dc90 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  ity = SQLITE_AFF
1dc91 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20  _INTEGER;.      
1dc92 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
1dc93 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
1dc94 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
1dc95 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
1dc96 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
1dc97 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1dc98 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41  RY) */..    /* A
1dc99 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 65 72   lone identifier
1dc9a 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
1dc9b 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f  a column..    */
1dc9c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a  .    case TK_ID:
1dc9d 20 7b 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 4e   {.      lookupN
1dc9e 61 6d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30  ame(pParse, 0, 0
1dc9f 2c 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2c  , &pExpr->token,
1dca0 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20   pNC, pExpr);.  
1dca1 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
1dca2 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  rune;.    }.  . 
1dca3 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e 61     /* A table na
1dca4 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61  me and column na
1dca5 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0a 20 20  me:     ID.ID.  
1dca6 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61 62 61    ** Or a databa
1dca7 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  se, table and co
1dca8 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44 0a  lumn:  ID.ID.ID.
1dca9 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
1dcaa 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 20 20  TK_DOT: {.      
1dcab 54 6f 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 3b 0a  Token *pColumn;.
1dcac 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 61        Token *pTa
1dcad 62 6c 65 3b 0a 20 20 20 20 20 20 54 6f 6b 65 6e  ble;.      Token
1dcae 20 2a 70 44 62 3b 0a 20 20 20 20 20 20 45 78 70   *pDb;.      Exp
1dcaf 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20 20  r *pRight;..    
1dcb0 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c 69 73    /* if( pSrcLis
1dcb1 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a 2f  t==0 ) break; */
1dcb2 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20  .      pRight = 
1dcb3 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
1dcb4 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d       if( pRight-
1dcb5 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20  >op==TK_ID ){.  
1dcb6 20 20 20 20 20 20 70 44 62 20 3d 20 30 3b 0a 20        pDb = 0;. 
1dcb7 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20         pTable = 
1dcb8 26 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 74  &pExpr->pLeft->t
1dcb9 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 43  oken;.        pC
1dcba 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 2d  olumn = &pRight-
1dcbb 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65  >token;.      }e
1dcbc 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
1dcbd 65 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d  ert( pRight->op=
1dcbe 3d 54 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20  =TK_DOT );.     
1dcbf 20 20 20 70 44 62 20 3d 20 26 70 45 78 70 72 2d     pDb = &pExpr-
1dcc0 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20  >pLeft->token;. 
1dcc1 20 20 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20         pTable = 
1dcc2 26 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e  &pRight->pLeft->
1dcc3 74 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70  token;.        p
1dcc4 43 6f 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74  Column = &pRight
1dcc5 2d 3e 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b  ->pRight->token;
1dcc6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c  .      }.      l
1dcc7 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73 65  ookupName(pParse
1dcc8 2c 20 70 44 62 2c 20 70 54 61 62 6c 65 2c 20 70  , pDb, pTable, p
1dcc9 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 45 78  Column, pNC, pEx
1dcca 70 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  pr);.      retur
1dccb 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20  n WRC_Prune;.   
1dccc 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c   }..    /* Resol
1dccd 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  ve function name
1dcce 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  s.    */.    cas
1dccf 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a  e TK_CONST_FUNC:
1dcd0 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e  .    case TK_FUN
1dcd1 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45  CTION: {.      E
1dcd2 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
1dcd3 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b   pExpr->x.pList;
1dcd4 20 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d      /* The argum
1dcd5 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20 20  ent list */.    
1dcd6 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 20    int n = pList 
1dcd7 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a  ? pList->nExpr :
1dcd8 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72   0;    /* Number
1dcd9 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f   of arguments */
1dcda 0a 20 20 20 20 20 20 69 6e 74 20 6e 6f 5f 73 75  .      int no_su
1dcdb 63 68 5f 66 75 6e 63 20 3d 20 30 3b 20 20 20 20  ch_func = 0;    
1dcdc 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f     /* True if no
1dcdd 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65   such function e
1dcde 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 20 20 69  xists */.      i
1dcdf 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67  nt wrong_num_arg
1dce0 73 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72  s = 0;     /* Tr
1dce1 75 65 20 69 66 20 77 72 6f 6e 67 20 6e 75 6d 62  ue if wrong numb
1dce2 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
1dce3 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 5f  */.      int is_
1dce4 61 67 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  agg = 0;        
1dce5 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1dce6 69 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  is an aggregate 
1dce7 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  function */.    
1dce8 20 20 69 6e 74 20 61 75 74 68 3b 20 20 20 20 20    int auth;     
1dce9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1dcea 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74   Authorization t
1dceb 6f 20 75 73 65 20 74 68 65 20 66 75 6e 63 74 69  o use the functi
1dcec 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  on */.      int 
1dced 6e 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  nId;            
1dcee 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1dcef 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
1dcf0 69 6e 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  in function name
1dcf1 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   */.      const 
1dcf2 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20 20  char *zId;      
1dcf3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e        /* The fun
1dcf4 63 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20  ction name. */. 
1dcf5 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44       FuncDef *pD
1dcf6 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
1dcf7 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
1dcf8 61 62 6f 75 74 20 74 68 65 20 66 75 6e 63 74 69  about the functi
1dcf9 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 65  on */.      u8 e
1dcfa 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d  nc = ENC(pParse-
1dcfb 3e 64 62 29 3b 20 20 20 2f 2a 20 54 68 65 20 64  >db);   /* The d
1dcfc 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67  atabase encoding
1dcfd 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72   */..      asser
1dcfe 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
1dcff 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
1dd00 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20  sSelect) );.    
1dd01 20 20 7a 49 64 20 3d 20 28 63 68 61 72 2a 29 70    zId = (char*)p
1dd02 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20  Expr->token.z;. 
1dd03 20 20 20 20 20 6e 49 64 20 3d 20 70 45 78 70 72       nId = pExpr
1dd04 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20  ->token.n;.     
1dd05 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46   pDef = sqlite3F
1dd06 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  indFunction(pPar
1dd07 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64  se->db, zId, nId
1dd08 2c 20 6e 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20  , n, enc, 0);.  
1dd09 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20      if( pDef==0 
1dd0a 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20  ){.        pDef 
1dd0b 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e  = sqlite3FindFun
1dd0c 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62  ction(pParse->db
1dd0d 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20  , zId, nId, -1, 
1dd0e 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  enc, 0);.       
1dd0f 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a   if( pDef==0 ){.
1dd10 20 20 20 20 20 20 20 20 20 20 6e 6f 5f 73 75 63            no_suc
1dd11 68 5f 66 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20  h_func = 1;.    
1dd12 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1dd13 20 20 20 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61       wrong_num_a
1dd14 72 67 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  rgs = 1;.       
1dd15 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
1dd16 20 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d          is_agg =
1dd17 20 70 44 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b   pDef->xFunc==0;
1dd18 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  .      }.#ifndef
1dd19 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1dd1a 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20  HORIZATION.     
1dd1b 20 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20   if( pDef ){.   
1dd1c 20 20 20 20 20 61 75 74 68 20 3d 20 73 71 6c 69       auth = sqli
1dd1d 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
1dd1e 72 73 65 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43  rse, SQLITE_FUNC
1dd1f 54 49 4f 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a  TION, 0, pDef->z
1dd20 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
1dd21 20 20 69 66 28 20 61 75 74 68 21 3d 53 51 4c 49    if( auth!=SQLI
1dd22 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1dd23 20 20 20 69 66 28 20 61 75 74 68 3d 3d 53 51 4c     if( auth==SQL
1dd24 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20  ITE_DENY ){.    
1dd25 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1dd26 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1dd27 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 20  "not authorized 
1dd28 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a  to use function:
1dd29 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %s",.          
1dd2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd2b 20 20 20 20 20 20 20 20 20 20 70 44 65 66 2d 3e            pDef->
1dd2c 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
1dd2d 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b      pNC->nErr++;
1dd2e 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1dd2f 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
1dd30 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20   = TK_NULL;.    
1dd31 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1dd32 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 20 20  _Prune;.        
1dd33 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
1dd34 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67  .      if( is_ag
1dd35 67 20 26 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77  g && !pNC->allow
1dd36 41 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Agg ){.        s
1dd37 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1dd38 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f  Parse, "misuse o
1dd39 66 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  f aggregate func
1dd3a 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49  tion %.*s()", nI
1dd3b 64 2c 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20  d,zId);.        
1dd3c 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  pNC->nErr++;.   
1dd3d 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 30 3b       is_agg = 0;
1dd3e 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1dd3f 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b   no_such_func ){
1dd40 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1dd41 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1dd42 20 22 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69   "no such functi
1dd43 6f 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20  on: %.*s", nId, 
1dd44 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e  zId);.        pN
1dd45 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20  C->nErr++;.     
1dd46 20 7d 65 6c 73 65 20 69 66 28 20 77 72 6f 6e 67   }else if( wrong
1dd47 5f 6e 75 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20  _num_args ){.   
1dd48 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1dd49 72 4d 73 67 28 70 50 61 72 73 65 2c 22 77 72 6f  rMsg(pParse,"wro
1dd4a 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ng number of arg
1dd4b 75 6d 65 6e 74 73 20 74 6f 20 66 75 6e 63 74 69  uments to functi
1dd4c 6f 6e 20 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20  on %.*s()",.    
1dd4d 20 20 20 20 20 20 20 20 20 6e 49 64 2c 20 7a 49           nId, zI
1dd4e 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d  d);.        pNC-
1dd4f 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d  >nErr++;.      }
1dd50 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67  .      if( is_ag
1dd51 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78  g ){.        pEx
1dd52 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f  pr->op = TK_AGG_
1dd53 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20  FUNCTION;.      
1dd54 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20    pNC->hasAgg = 
1dd55 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
1dd56 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e   if( is_agg ) pN
1dd57 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b  C->allowAgg = 0;
1dd58 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61  .      sqlite3Wa
1dd59 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b  lkExprList(pWalk
1dd5a 65 72 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20  er, pList);.    
1dd5b 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 20 70    if( is_agg ) p
1dd5c 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31  NC->allowAgg = 1
1dd5d 3b 0a 20 20 20 20 20 20 2f 2a 20 46 49 58 20 4d  ;.      /* FIX M
1dd5e 45 3a 20 20 43 6f 6d 70 75 74 65 20 70 45 78 70  E:  Compute pExp
1dd5f 72 2d 3e 61 66 66 69 6e 69 74 79 20 62 61 73 65  r->affinity base
1dd60 64 20 6f 6e 20 74 68 65 20 65 78 70 65 63 74 65  d on the expecte
1dd61 64 20 72 65 74 75 72 6e 0a 20 20 20 20 20 20 2a  d return.      *
1dd62 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 75  * type of the fu
1dd63 6e 63 74 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f  nction .      */
1dd64 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52  .      return WR
1dd65 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23  C_Prune;.    }.#
1dd66 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1dd67 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
1dd68 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a  case TK_SELECT:.
1dd69 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53      case TK_EXIS
1dd6a 54 53 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63  TS:.#endif.    c
1dd6b 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20  ase TK_IN: {.   
1dd6c 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
1dd6d 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1dd6e 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
1dd6f 20 20 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20         int nRef 
1dd70 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66  = pNC->nRef;.#if
1dd71 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1dd72 5f 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69  _CHECK.        i
1dd73 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20  f( pNC->isCheck 
1dd74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1dd75 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1dd76 72 73 65 2c 22 73 75 62 71 75 65 72 69 65 73 20  rse,"subqueries 
1dd77 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48  prohibited in CH
1dd78 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22  ECK constraints"
1dd79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  );.        }.#en
1dd7a 64 69 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69  dif.        sqli
1dd7b 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57  te3WalkSelect(pW
1dd7c 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e  alker, pExpr->x.
1dd7d 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  pSelect);.      
1dd7e 20 20 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e    assert( pNC->n
1dd7f 52 65 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20  Ref>=nRef );.   
1dd80 20 20 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70       if( nRef!=p
1dd81 4e 43 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20  NC->nRef ){.    
1dd82 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f        ExprSetPro
1dd83 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1dd84 56 61 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  VarSelect);.    
1dd85 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1dd86 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1dd87 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1dd88 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63  OMIT_CHECK.    c
1dd89 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a  ase TK_VARIABLE:
1dd8a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43   {.      if( pNC
1dd8b 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20  ->isCheck ){.   
1dd8c 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1dd8d 72 4d 73 67 28 70 50 61 72 73 65 2c 22 70 61 72  rMsg(pParse,"par
1dd8e 61 6d 65 74 65 72 73 20 70 72 6f 68 69 62 69 74  ameters prohibit
1dd8f 65 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73  ed in CHECK cons
1dd90 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20  traints");.     
1dd91 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1dd92 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
1dd93 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 72 73  .  return (pPars
1dd94 65 2d 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73  e->nErr || pPars
1dd95 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
1dd96 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74  led) ? WRC_Abort
1dd97 20 3a 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b   : WRC_Continue;
1dd98 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74  .}../*.** pEList
1dd99 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78   is a list of ex
1dd9a 70 72 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20  pressions which 
1dd9b 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72  are really the r
1dd9c 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
1dd9d 0a 2a 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61  .** a SELECT sta
1dd9e 74 65 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61  tement.  pE is a
1dd9f 20 74 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45   term in an ORDE
1dda0 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59  R BY or GROUP BY
1dda1 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73   clause..** This
1dda2 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
1dda3 74 6f 20 73 65 65 20 69 66 20 70 45 20 69 73 20  to see if pE is 
1dda4 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66  a simple identif
1dda5 69 65 72 20 77 68 69 63 68 20 63 6f 72 72 65 73  ier which corres
1dda6 70 6f 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20  ponds.** to the 
1dda7 41 53 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f  AS-name of one o
1dda8 66 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74  f the terms of t
1dda9 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
1ddaa 73 74 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a  st.  If it is,.*
1ddab 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * this routine r
1ddac 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
1ddad 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e   between 1 and N
1ddae 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
1ddaf 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65  number of.** ele
1ddb0 6d 65 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c  ments in pEList,
1ddb1 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
1ddb2 6f 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65  o the matching e
1ddb3 6e 74 72 79 2e 20 20 49 66 20 74 68 65 72 65 20  ntry.  If there 
1ddb4 69 73 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20  is.** no match, 
1ddb5 6f 72 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20  or if pE is not 
1ddb6 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66  a simple identif
1ddb7 69 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ier, then this r
1ddb8 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e  outine.** return
1ddb9 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74   0..**.** pEList
1ddba 20 68 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76   has been resolv
1ddbb 65 64 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e  ed.  pE has not.
1ddbc 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
1ddbd 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20  esolveAsName(.  
1ddbe 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1ddbf 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
1ddc0 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20  ntext for error 
1ddc1 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78  messages */.  Ex
1ddc2 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20  prList *pEList, 
1ddc3 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72   /* List of expr
1ddc4 65 73 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20  essions to scan 
1ddc5 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20  */.  Expr *pE   
1ddc6 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
1ddc7 73 73 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79  ssion we are try
1ddc8 69 6e 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a  ing to match */.
1ddc9 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
1ddca 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1ddcb 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 69 66  counter */..  if
1ddcc 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20  ( pE->op==TK_ID 
1ddcd 7c 7c 20 28 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 53  || (pE->op==TK_S
1ddce 54 52 49 4e 47 20 26 26 20 70 45 2d 3e 74 6f 6b  TRING && pE->tok
1ddcf 65 6e 2e 7a 5b 30 5d 21 3d 27 5c 27 27 29 20 29  en.z[0]!='\'') )
1ddd0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  {.    sqlite3 *d
1ddd1 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1ddd2 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d      char *zCol =
1ddd3 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
1ddd4 54 6f 6b 65 6e 28 64 62 2c 20 26 70 45 2d 3e 74  Token(db, &pE->t
1ddd5 6f 6b 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a  oken);.    if( z
1ddd6 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Col==0 ){.      
1ddd7 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d  return -1;.    }
1ddd8 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1ddd9 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
1ddda 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  ++){.      char 
1dddb 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61  *zAs = pEList->a
1dddc 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [i].zName;.     
1dddd 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73   if( zAs!=0 && s
1ddde 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41  qlite3StrICmp(zA
1dddf 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20  s, zCol)==0 ){. 
1dde0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
1dde1 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a  Free(db, zCol);.
1dde2 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69          return i
1dde3 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  +1;.      }.    
1dde4 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
1dde5 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20  ree(db, zCol);. 
1dde6 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
1dde7 0a 0a 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20  ../*.** pE is a 
1dde8 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78  pointer to an ex
1dde9 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69  pression which i
1ddea 73 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  s a single term 
1ddeb 69 6e 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20  in the.** ORDER 
1ddec 42 59 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64  BY of a compound
1dded 20 53 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78   SELECT.  The ex
1ddee 70 72 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74  pression has not
1ddef 20 62 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65   been.** name re
1ddf0 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74  solved..**.** At
1ddf1 20 74 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20   the point this 
1ddf2 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
1ddf3 64 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e  d, we already kn
1ddf4 6f 77 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f  ow that the.** O
1ddf5 52 44 45 52 20 42 59 20 74 65 72 6d 20 69 73 20  RDER BY term is 
1ddf6 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69  not an integer i
1ddf7 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65  ndex into the re
1ddf8 73 75 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a  sult set.  That.
1ddf9 2a 2a 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c  ** case is handl
1ddfa 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ed by the callin
1ddfb 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a  g routine..**.**
1ddfc 20 41 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63   Attempt to matc
1ddfd 68 20 70 45 20 61 67 61 69 6e 73 74 20 72 65 73  h pE against res
1ddfe 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20  ult set columns 
1ddff 69 6e 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  in the left-most
1de00 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65  .** SELECT state
1de01 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68  ment.  Return th
1de02 65 20 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65  e index i of the
1de03 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e   matching column
1de04 2c 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63  ,.** as an indic
1de05 61 74 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c  ation to the cal
1de06 6c 65 72 20 74 68 61 74 20 69 74 20 73 68 6f 75  ler that it shou
1de07 6c 64 20 73 6f 72 74 20 62 79 20 74 68 65 20 69  ld sort by the i
1de08 2d 74 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54  -th column..** T
1de09 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c  he left-most col
1de0a 75 6d 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74  umn is 1.  In ot
1de0b 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76  her words, the v
1de0c 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
1de0d 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74   the.** same int
1de0e 65 67 65 72 20 76 61 6c 75 65 20 74 68 61 74 20  eger value that 
1de0f 77 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e  would be used in
1de10 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
1de11 6e 74 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a  nt to indicate.*
1de12 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a  * the column..**
1de13 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
1de14 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e  no match, return
1de15 20 30 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69   0.  Return -1 i
1de16 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1de17 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
1de18 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54   resolveOrderByT
1de19 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20  ermToExprList(. 
1de1a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
1de1b 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1de1c 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72  ontext for error
1de1d 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53   messages */.  S
1de1e 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
1de1f 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
1de20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74  statement with t
1de21 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
1de22 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  se */.  Expr *pE
1de23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1de24 65 20 73 70 65 63 69 66 69 63 20 4f 52 44 45 52  e specific ORDER
1de25 20 42 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20   BY term */.){. 
1de26 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1de27 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1de28 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ter */.  ExprLis
1de29 74 20 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54  t *pEList;  /* T
1de2a 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  he columns of th
1de2b 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  e result set */.
1de2c 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63    NameContext nc
1de2d 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e  ;    /* Name con
1de2e 74 65 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69  text for resolvi
1de2f 6e 67 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65  ng pE */..  asse
1de30 72 74 28 20 73 71 6c 69 74 65 33 45 78 70 72 49  rt( sqlite3ExprI
1de31 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29  sInteger(pE, &i)
1de32 3d 3d 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20  ==0 );.  pEList 
1de33 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
1de34 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65  t;..  /* Resolve
1de35 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68   all names in th
1de36 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20  e ORDER BY term 
1de37 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a  expression.  */.
1de38 20 20 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c    memset(&nc, 0,
1de39 20 73 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20   sizeof(nc));.  
1de3a 6e 63 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  nc.pParse = pPar
1de3b 73 65 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73  se;.  nc.pSrcLis
1de3c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  t = pSelect->pSr
1de3d 63 3b 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d  c;.  nc.pEList =
1de3e 20 70 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c   pEList;.  nc.al
1de3f 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63  lowAgg = 1;.  nc
1de40 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28  .nErr = 0;.  if(
1de41 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
1de42 78 70 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45  xprNames(&nc, pE
1de43 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
1de44 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73  ErrorClear(pPars
1de45 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  e);.    return 0
1de46 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20  ;.  }..  /* Try 
1de47 74 6f 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44  to match the ORD
1de48 45 52 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e  ER BY expression
1de49 20 61 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72   against an expr
1de4a 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74  ession.  ** in t
1de4b 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20  he result set.  
1de4c 52 65 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65  Return an 1-base
1de4d 64 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d  d index of the m
1de4e 61 74 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73  atching.  ** res
1de4f 75 6c 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20  ult-set entry.. 
1de50 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
1de51 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
1de52 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  i++){.    if( sq
1de53 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
1de54 28 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  (pEList->a[i].pE
1de55 78 70 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20  xpr, pE) ){.    
1de56 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20    return i+1;.  
1de57 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
1de58 20 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72   no match, retur
1de59 6e 20 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  n 0. */.  return
1de5a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   0;.}../*.** Gen
1de5b 65 72 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42  erate an ORDER B
1de5c 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65  Y or GROUP BY te
1de5d 72 6d 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20  rm out-of-range 
1de5e 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  error..*/.static
1de5f 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74   void resolveOut
1de60 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20  OfRangeError(.  
1de61 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1de62 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72         /* The er
1de63 72 6f 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f  ror context into
1de64 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20   which to write 
1de65 74 68 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63  the error */.  c
1de66 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
1de67 2c 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22  ,     /* "ORDER"
1de68 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20   or "GROUP" */. 
1de69 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20   int i,         
1de6a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
1de6b 6e 64 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f  ndex (1-based) o
1de6c 66 20 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f  f the term out o
1de6d 66 20 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74  f range */.  int
1de6e 20 6d 78 20 20 20 20 20 20 20 20 20 20 20 20 20   mx             
1de6f 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70      /* Largest p
1de70 65 72 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65  ermissible value
1de71 20 6f 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71   of i */.){.  sq
1de72 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1de73 61 72 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25  arse, .    "%r %
1de74 73 20 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66  s BY term out of
1de75 20 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20   range - should 
1de76 62 65 20 22 0a 20 20 20 20 22 62 65 74 77 65 65  be ".    "betwee
1de77 6e 20 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20  n 1 and %d", i, 
1de78 7a 54 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f  zType, mx);.}../
1de79 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65  *.** Analyze the
1de7a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
1de7b 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53   in a compound S
1de7c 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
1de7d 20 20 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63     Modify.** eac
1de7e 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52  h term of the OR
1de7f 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73  DER BY clause is
1de80 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65   a constant inte
1de81 67 65 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a  ger between 1.**
1de82 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69   and N where N i
1de83 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
1de84 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63  columns in the c
1de85 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a  ompound SELECT..
1de86 2a 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74  **.** ORDER BY t
1de87 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c  erms that are al
1de88 72 65 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72  ready an integer
1de89 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e   between 1 and N
1de8a 20 61 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69   are.** unmodifi
1de8b 65 64 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65  ed.  ORDER BY te
1de8c 72 6d 73 20 74 68 61 74 20 61 72 65 20 69 6e 74  rms that are int
1de8d 65 67 65 72 73 20 6f 75 74 73 69 64 65 20 74 68  egers outside th
1de8e 65 20 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20  e range of.** 1 
1de8f 74 68 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61  through N genera
1de90 74 65 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52  te an error.  OR
1de91 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 68 61  DER BY terms tha
1de92 74 20 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e  t are expression
1de93 73 0a 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64  s.** are matched
1de94 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20   against result 
1de95 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20  set expressions 
1de96 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45  of compound SELE
1de97 43 54 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20  CT.** beginning 
1de98 77 69 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f  with the left-mo
1de99 73 74 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f  st SELECT and wo
1de9a 72 6b 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65  rking toward the
1de9b 20 72 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68   right..** At th
1de9c 65 20 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74  e first match, t
1de9d 68 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72  he ORDER BY expr
1de9e 65 73 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66  ession is transf
1de9f 6f 72 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68  ormed into.** th
1dea0 65 20 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e  e integer column
1dea1 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52   number..**.** R
1dea2 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1dea3 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e   of errors seen.
1dea4 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
1dea5 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72  esolveCompoundOr
1dea6 64 65 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a  derBy(.  Parse *
1dea7 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
1dea8 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1dea9 74 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20  t.  Leave error 
1deaa 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f  messages here */
1deab 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
1deac 63 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ct       /* The 
1dead 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1deae 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1deaf 4f 52 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20  ORDER BY */.){. 
1deb0 20 69 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69   int i;.  ExprLi
1deb1 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20  st *pOrderBy;.  
1deb2 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1deb3 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
1deb4 0a 20 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20  .  int moreToDo 
1deb5 3d 20 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79  = 1;..  pOrderBy
1deb6 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64   = pSelect->pOrd
1deb7 65 72 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64  erBy;.  if( pOrd
1deb8 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e  erBy==0 ) return
1deb9 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73   0;.  db = pPars
1deba 65 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54  e->db;.#if SQLIT
1debb 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69  E_MAX_COLUMN.  i
1debc 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78  f( pOrderBy->nEx
1debd 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  pr>db->aLimit[SQ
1debe 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d  LITE_LIMIT_COLUM
1debf 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  N] ){.    sqlite
1dec0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1dec1 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d  , "too many term
1dec2 73 20 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c  s in ORDER BY cl
1dec3 61 75 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75  ause");.    retu
1dec4 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  rn 1;.  }.#endif
1dec5 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f  .  for(i=0; i<pO
1dec6 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
1dec7 2b 2b 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42  ++){.    pOrderB
1dec8 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30  y->a[i].done = 0
1dec9 3b 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d  ;.  }.  pSelect-
1deca 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68  >pNext = 0;.  wh
1decb 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50  ile( pSelect->pP
1decc 72 69 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c  rior ){.    pSel
1decd 65 63 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65  ect->pPrior->pNe
1dece 78 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20  xt = pSelect;.  
1decf 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c    pSelect = pSel
1ded0 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d  ect->pPrior;.  }
1ded1 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63  .  while( pSelec
1ded2 74 20 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b  t && moreToDo ){
1ded3 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72  .    struct Expr
1ded4 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1ded5 3b 0a 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d  ;.    moreToDo =
1ded6 20 30 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d   0;.    pEList =
1ded7 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
1ded8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ;.    assert( pE
1ded9 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66  List!=0 );.    f
1deda 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f  or(i=0, pItem=pO
1dedb 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72  rderBy->a; i<pOr
1dedc 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b  derBy->nExpr; i+
1dedd 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
1dede 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31     int iCol = -1
1dedf 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45  ;.      Expr *pE
1dee0 2c 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69  , *pDup;.      i
1dee1 66 28 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29  f( pItem->done )
1dee2 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
1dee3 20 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78   pE = pItem->pEx
1dee4 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71  pr;.      if( sq
1dee5 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
1dee6 65 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b  er(pE, &iCol) ){
1dee7 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
1dee8 6c 3c 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69  l<0 || iCol>pELi
1dee9 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
1deea 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75         resolveOu
1deeb 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50  tOfRangeError(pP
1deec 61 72 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69  arse, "ORDER", i
1deed 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  +1, pEList->nExp
1deee 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  r);.          re
1deef 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20  turn 1;.        
1def0 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
1def1 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65         iCol = re
1def2 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72  solveAsName(pPar
1def3 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b  se, pEList, pE);
1def4 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
1def5 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  l==0 ){.        
1def6 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
1def7 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20  ExprDup(db, pE, 
1def8 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0);.          if
1def9 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
1defa 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  led ){.         
1defb 20 20 20 61 73 73 65 72 74 28 70 44 75 70 29 3b     assert(pDup);
1defc 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f  .            iCo
1defd 6c 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72  l = resolveOrder
1defe 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74  ByTermToExprList
1deff 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
1df00 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20  , pDup);.       
1df01 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73     }.          s
1df02 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1df03 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20  (db, pDup);.    
1df04 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1df05 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20  ( iCol<0 ){.    
1df06 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1df07 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1df08 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  }.      if( iCol
1df09 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 43 6f  >0 ){.        Co
1df0a 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70  llSeq *pColl = p
1df0b 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  E->pColl;.      
1df0c 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 45    int flags = pE
1df0d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
1df0e 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20  Collate;.       
1df0f 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
1df10 74 65 28 64 62 2c 20 70 45 29 3b 0a 20 20 20 20  te(db, pE);.    
1df11 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72      pItem->pExpr
1df12 20 3d 20 70 45 20 3d 20 73 71 6c 69 74 65 33 45   = pE = sqlite3E
1df13 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45 47  xpr(db, TK_INTEG
1df14 45 52 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  ER, 0, 0, 0);.  
1df15 20 20 20 20 20 20 69 66 28 20 70 45 3d 3d 30 20        if( pE==0 
1df16 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
1df17 20 20 20 20 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20      pE->pColl = 
1df18 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70  pColl;.        p
1df19 45 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49  E->flags |= EP_I
1df1a 6e 74 56 61 6c 75 65 20 7c 20 66 6c 61 67 73 3b  ntValue | flags;
1df1b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 69 54 61  .        pE->iTa
1df1c 62 6c 65 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20  ble = iCol;.    
1df1d 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20      pItem->iCol 
1df1e 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20  = (u16)iCol;.   
1df1f 20 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65       pItem->done
1df20 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 1;.      }els
1df21 65 7b 0a 20 20 20 20 20 20 20 20 6d 6f 72 65 54  e{.        moreT
1df22 6f 44 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  oDo = 1;.      }
1df23 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c 65  .    }.    pSele
1df24 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e  ct = pSelect->pN
1df25 65 78 74 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  ext;.  }.  for(i
1df26 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e  =0; i<pOrderBy->
1df27 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
1df28 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 61   if( pOrderBy->a
1df29 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a 20  [i].done==0 ){. 
1df2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1df2b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 72  rMsg(pParse, "%r
1df2c 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 64   ORDER BY term d
1df2d 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e  oes not match an
1df2e 79 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  y ".            
1df2f 22 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72  "column in the r
1df30 65 73 75 6c 74 20 73 65 74 22 2c 20 69 2b 31 29  esult set", i+1)
1df31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
1df32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1df33 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
1df34 20 43 68 65 63 6b 20 65 76 65 72 79 20 74 65 72   Check every ter
1df35 6d 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42  m in the ORDER B
1df36 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c  Y or GROUP BY cl
1df37 61 75 73 65 20 70 4f 72 64 65 72 42 79 20 6f 66  ause pOrderBy of
1df38 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73  .** the SELECT s
1df39 74 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74  tatement pSelect
1df3a 2e 20 20 49 66 20 61 6e 79 20 74 65 72 6d 20 69  .  If any term i
1df3b 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61  s reference to a
1df3c 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 65  .** result set e
1df3d 78 70 72 65 73 73 69 6f 6e 20 28 61 73 20 64 65  xpression (as de
1df3e 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
1df3f 45 78 70 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20  ExprList.a.iCol 
1df40 66 69 65 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 63  field).** then c
1df41 6f 6e 76 65 72 74 20 74 68 61 74 20 74 65 72 6d  onvert that term
1df42 20 69 6e 74 6f 20 61 20 63 6f 70 79 20 6f 66 20   into a copy of 
1df43 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1df44 67 20 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20  g result set.** 
1df45 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  column..**.** If
1df46 20 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 20   any errors are 
1df47 64 65 74 65 63 74 65 64 2c 20 61 64 64 20 61 6e  detected, add an
1df48 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
1df49 6f 20 70 50 61 72 73 65 20 61 6e 64 0a 2a 2a 20  o pParse and.** 
1df4a 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e  return non-zero.
1df4b 20 20 52 65 74 75 72 6e 20 7a 65 72 6f 20 69 66    Return zero if
1df4c 20 6e 6f 20 65 72 72 6f 72 73 20 61 72 65 20 73   no errors are s
1df4d 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  een..*/.SQLITE_P
1df4e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1df4f 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  e3ResolveOrderGr
1df50 6f 75 70 42 79 28 0a 20 20 50 61 72 73 65 20 2a  oupBy(.  Parse *
1df51 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
1df52 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1df53 74 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20  t.  Leave error 
1df54 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f  messages here */
1df55 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
1df56 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ct,      /* The 
1df57 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1df58 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1df59 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
1df5a 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
1df5b 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
1df5c 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c  Y or GROUP BY cl
1df5d 61 75 73 65 20 74 6f 20 62 65 20 70 72 6f 63 65  ause to be proce
1df5e 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ssed */.  const 
1df5f 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 20  char *zType     
1df60 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47  /* "ORDER" or "G
1df61 52 4f 55 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ROUP" */.){.  in
1df62 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  t i;.  sqlite3 *
1df63 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1df64 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
1df65 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 45 78  ist;.  struct Ex
1df66 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
1df67 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4f 72 64 65  em;..  if( pOrde
1df68 72 42 79 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65  rBy==0 || pParse
1df69 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1df6a 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  ed ) return 0;.#
1df6b 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  if SQLITE_MAX_CO
1df6c 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65  LUMN.  if( pOrde
1df6d 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61  rBy->nExpr>db->a
1df6e 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1df6f 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20  IT_COLUMN] ){.  
1df70 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1df71 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d  g(pParse, "too m
1df72 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 25 73 20  any terms in %s 
1df73 42 59 20 63 6c 61 75 73 65 22 2c 20 7a 54 79 70  BY clause", zTyp
1df74 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
1df75 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70  ;.  }.#endif.  p
1df76 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d  EList = pSelect-
1df77 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65 72  >pEList;.  asser
1df78 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 20  t( pEList!=0 ); 
1df79 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63   /* sqlite3Selec
1df7a 74 4e 65 77 28 29 20 67 75 61 72 61 6e 74 65 65  tNew() guarantee
1df7b 73 20 74 68 69 73 20 2a 2f 0a 20 20 66 6f 72 28  s this */.  for(
1df7c 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65  i=0, pItem=pOrde
1df7d 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72  rBy->a; i<pOrder
1df7e 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  By->nExpr; i++, 
1df7f 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66  pItem++){.    if
1df80 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 7b  ( pItem->iCol ){
1df81 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d  .      if( pItem
1df82 2d 3e 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e  ->iCol>pEList->n
1df83 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20  Expr ){.        
1df84 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67  resolveOutOfRang
1df85 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 7a  eError(pParse, z
1df86 54 79 70 65 2c 20 69 2b 31 2c 20 70 45 4c 69 73  Type, i+1, pELis
1df87 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20  t->nExpr);.     
1df88 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1df89 20 20 20 7d 0a 20 20 20 20 20 20 72 65 73 6f 6c     }.      resol
1df8a 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20  veAlias(pParse, 
1df8b 70 45 4c 69 73 74 2c 20 70 49 74 65 6d 2d 3e 69  pEList, pItem->i
1df8c 43 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 45  Col-1, pItem->pE
1df8d 78 70 72 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20  xpr, zType);.   
1df8e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1df8f 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64  0;.}../*.** pOrd
1df90 65 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45 52  erBy is an ORDER
1df91 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
1df92 63 6c 61 75 73 65 20 69 6e 20 53 45 4c 45 43 54  clause in SELECT
1df93 20 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c 65   statement pSele
1df94 63 74 2e 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20  ct..** The Name 
1df95 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53  context of the S
1df96 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1df97 69 73 20 70 4e 43 2e 20 20 7a 54 79 70 65 20 69  is pNC.  zType i
1df98 73 20 65 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44  s either.** "ORD
1df99 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 64  ER" or "GROUP" d
1df9a 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63  epending on whic
1df9b 68 20 74 79 70 65 20 6f 66 20 63 6c 61 75 73 65  h type of clause
1df9c 20 70 4f 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a   pOrderBy is..**
1df9d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1df9e 20 72 65 73 6f 6c 76 65 73 20 65 61 63 68 20 74   resolves each t
1df9f 65 72 6d 20 6f 66 20 74 68 65 20 63 6c 61 75 73  erm of the claus
1dfa0 65 20 69 6e 74 6f 20 61 6e 20 65 78 70 72 65 73  e into an expres
1dfa1 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20  sion..** If the 
1dfa2 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73  order-by term is
1dfa3 20 61 6e 20 69 6e 74 65 67 65 72 20 49 20 62 65   an integer I be
1dfa4 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 28 77  tween 1 and N (w
1dfa5 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a  here N is the.**
1dfa6 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1dfa7 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
1dfa8 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45   set of the SELE
1dfa9 43 54 29 20 74 68 65 6e 20 74 68 65 20 65 78 70  CT) then the exp
1dfaa 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68  ression.** in th
1dfab 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20  e resolution is 
1dfac 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 49 2d  a copy of the I-
1dfad 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78  th result-set ex
1dfae 70 72 65 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a  pression.  If.**
1dfaf 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74 65   the order-by te
1dfb0 72 6d 20 69 73 20 61 6e 20 69 64 65 6e 74 69 66  rm is an identif
1dfb1 79 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e  y that correspon
1dfb2 64 73 20 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d  ds to the AS-nam
1dfb3 65 20 6f 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74  e of.** a result
1dfb4 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2c  -set expression,
1dfb5 20 74 68 65 6e 20 74 68 65 20 74 65 72 6d 20 72   then the term r
1dfb6 65 73 6f 6c 76 65 73 20 74 6f 20 61 20 63 6f 70  esolves to a cop
1dfb7 79 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75  y of the.** resu
1dfb8 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt-set expressio
1dfb9 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74  n.  Otherwise, t
1dfba 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1dfbb 20 72 65 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20   resolved in.** 
1dfbc 74 68 65 20 75 73 75 61 6c 20 77 61 79 20 2d 20  the usual way - 
1dfbd 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52 65 73  using sqlite3Res
1dfbe 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 2e  olveExprNames().
1dfbf 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1dfc0 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
1dfc1 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
1dfc2 2e 20 20 49 66 20 65 72 72 6f 72 73 20 6f 63 63  .  If errors occ
1dfc3 75 72 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61  ur, then.** an a
1dfc4 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
1dfc5 20 6d 65 73 73 61 67 65 20 6d 69 67 68 74 20 62   message might b
1dfc6 65 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65  e left in pParse
1dfc7 2e 20 20 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a  .  (OOM errors.*
1dfc8 2a 20 65 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a  * excepted.).*/.
1dfc9 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c  static int resol
1dfca 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a  veOrderGroupBy(.
1dfcb 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1dfcc 4e 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e  NC,     /* The n
1dfcd 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74  ame context of t
1dfce 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
1dfcf 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ent */.  Select 
1dfd0 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f  *pSelect,      /
1dfd1 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
1dfd2 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70  tement holding p
1dfd3 4f 72 64 65 72 42 79 20 2a 2f 0a 20 20 45 78 70  OrderBy */.  Exp
1dfd4 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c  rList *pOrderBy,
1dfd5 20 20 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42     /* An ORDER B
1dfd6 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c  Y or GROUP BY cl
1dfd7 61 75 73 65 20 74 6f 20 72 65 73 6f 6c 76 65 20  ause to resolve 
1dfd8 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1dfd9 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 45 69  *zType     /* Ei
1dfda 74 68 65 72 20 22 4f 52 44 45 52 22 20 6f 72 20  ther "ORDER" or 
1dfdb 22 47 52 4f 55 50 22 2c 20 61 73 20 61 70 70 72  "GROUP", as appr
1dfdc 6f 70 72 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20  opriate */.){.  
1dfdd 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1dfde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1dfdf 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1dfe0 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20  /.  int iCol;   
1dfe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dfe2 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
1dfe3 62 65 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ber */.  struct 
1dfe4 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
1dfe5 49 74 65 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72  Item;   /* A ter
1dfe6 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  m of the ORDER B
1dfe7 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61  Y clause */.  Pa
1dfe8 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20  rse *pParse;    
1dfe9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1dfea 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
1dfeb 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c 74  */.  int nResult
1dfec 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1dfed 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1dfee 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65   terms in the re
1dfef 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69  sult set */..  i
1dff0 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  f( pOrderBy==0 )
1dff1 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65   return 0;.  nRe
1dff2 73 75 6c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  sult = pSelect->
1dff3 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
1dff4 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70   pParse = pNC->p
1dff5 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30  Parse;.  for(i=0
1dff6 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79  , pItem=pOrderBy
1dff7 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d  ->a; i<pOrderBy-
1dff8 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
1dff9 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20  em++){.    Expr 
1dffa 2a 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78  *pE = pItem->pEx
1dffb 70 72 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72  pr;.    iCol = r
1dffc 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61  esolveAsName(pPa
1dffd 72 73 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45  rse, pSelect->pE
1dffe 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 69  List, pE);.    i
1dfff 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20  f( iCol<0 ){.   
1e000 20 20 20 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a     return 1;  /*
1e001 20 4f 4f 4d 20 65 72 72 6f 72 20 2a 2f 0a 20 20   OOM error */.  
1e002 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c    }.    if( iCol
1e003 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  >0 ){.      /* I
1e004 66 20 61 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74  f an AS-name mat
1e005 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72  ch is found, mar
1e006 6b 20 74 68 69 73 20 4f 52 44 45 52 20 42 59 20  k this ORDER BY 
1e007 63 6f 6c 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a  column as being.
1e008 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 70 79 20        ** a copy 
1e009 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72  of the iCol-th r
1e00a 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e  esult-set column
1e00b 2e 20 20 54 68 65 20 73 75 62 73 65 71 75 65 6e  .  The subsequen
1e00c 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20  t call to.      
1e00d 2a 2a 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  ** sqlite3Resolv
1e00e 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20  eOrderGroupBy() 
1e00f 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65  will convert the
1e010 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 61   expression to a
1e011 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f  .      ** copy o
1e012 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65  f the iCol-th re
1e013 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
1e014 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49  ion. */.      pI
1e015 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36  tem->iCol = (u16
1e016 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e  )iCol;.      con
1e017 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20  tinue;.    }.   
1e018 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72   if( sqlite3Expr
1e019 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69  IsInteger(pE, &i
1e01a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Col) ){.      /*
1e01b 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 74 65   The ORDER BY te
1e01c 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  rm is an integer
1e01d 20 63 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69   constant.  Agai
1e01e 6e 2c 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d  n, set the colum
1e01f 6e 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65  n.      ** numbe
1e020 72 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65  r so that sqlite
1e021 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f  3ResolveOrderGro
1e022 75 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76  upBy() will conv
1e023 65 72 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  ert the.      **
1e024 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74   order-by term t
1e025 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  o a copy of the 
1e026 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65  result-set expre
1e027 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69  ssion */.      i
1e028 66 28 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20  f( iCol<1 ){.   
1e029 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f       resolveOutO
1e02a 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72  fRangeError(pPar
1e02b 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20  se, zType, i+1, 
1e02c 6e 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20  nResult);.      
1e02d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
1e02e 20 20 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d    }.      pItem-
1e02f 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f  >iCol = (u16)iCo
1e030 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  l;.      continu
1e031 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  e;.    }..    /*
1e032 20 4f 74 68 65 72 77 69 73 65 2c 20 74 72 65 61   Otherwise, trea
1e033 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74  t the ORDER BY t
1e034 65 72 6d 20 61 73 20 61 6e 20 6f 72 64 69 6e 61  erm as an ordina
1e035 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  ry expression */
1e036 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c  .    pItem->iCol
1e037 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71   = 0;.    if( sq
1e038 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
1e039 4e 61 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29  Names(pNC, pE) )
1e03a 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1e03b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1e03c 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 73 6f  turn sqlite3Reso
1e03d 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
1e03e 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c  pParse, pSelect,
1e03f 20 70 4f 72 64 65 72 42 79 2c 20 7a 54 79 70 65   pOrderBy, zType
1e040 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f  );.}../*.** Reso
1e041 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65  lve names in the
1e042 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1e043 74 20 70 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  t p and all of i
1e044 74 73 20 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a  ts descendents..
1e045 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
1e046 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 28  solveSelectStep(
1e047 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
1e048 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e   Select *p){.  N
1e049 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74  ameContext *pOut
1e04a 65 72 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78  erNC;  /* Contex
1e04b 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  t that contains 
1e04c 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20  this SELECT */. 
1e04d 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
1e04e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ;        /* Name
1e04f 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73   context of this
1e050 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74   SELECT */.  int
1e051 20 69 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20   isCompound;    
1e052 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1e053 70 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20  p is a compound 
1e054 73 65 6c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20  select */.  int 
1e055 6e 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20  nCompound;      
1e056 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1e057 20 63 6f 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20   compound terms 
1e058 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72  processed so far
1e059 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
1e05a 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  rse;          /*
1e05b 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
1e05c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1e05d 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a  pEList;       /*
1e05e 20 52 65 73 75 6c 74 20 73 65 74 20 65 78 70 72   Result set expr
1e05f 65 73 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20  ession list */. 
1e060 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1e061 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1e062 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78   counter */.  Ex
1e063 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
1e064 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f  ;     /* The GRO
1e065 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  UP BY clause */.
1e066 20 20 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d    Select *pLeftm
1e067 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66  ost;      /* Lef
1e068 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54  t-most of SELECT
1e069 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a   of a compound *
1e06a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
1e06b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
1e06c 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1e06d 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65  on */.  ..  asse
1e06e 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66  rt( p!=0 );.  if
1e06f 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  ( p->selFlags & 
1e070 53 46 5f 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20  SF_Resolved ){. 
1e071 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
1e072 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65  une;.  }.  pOute
1e073 72 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75  rNC = pWalker->u
1e074 2e 70 4e 43 3b 0a 20 20 70 50 61 72 73 65 20 3d  .pNC;.  pParse =
1e075 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65   pWalker->pParse
1e076 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
1e077 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61  >db;..  /* Norma
1e078 6c 6c 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63  lly sqlite3Selec
1e079 74 45 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62  tExpand() will b
1e07a 65 20 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61  e called first a
1e07b 6e 64 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a  nd will have.  *
1e07c 2a 20 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64  * already expand
1e07d 65 64 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20  ed this SELECT. 
1e07e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69   However, if thi
1e07f 73 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20  s is a subquery 
1e080 77 69 74 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65  within.  ** an e
1e081 78 70 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74  xpression, sqlit
1e082 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
1e083 65 73 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c  es() will be cal
1e084 6c 65 64 20 77 69 74 68 6f 75 74 20 61 0a 20 20  led without a.  
1e085 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f  ** prior call to
1e086 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
1e087 70 61 6e 64 28 29 2e 20 20 57 68 65 6e 20 74 68  pand().  When th
1e088 61 74 20 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a  at happens, let.
1e089 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65    ** sqlite3Sele
1e08a 63 74 50 72 65 70 28 29 20 64 6f 20 61 6c 6c 20  ctPrep() do all 
1e08b 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e  of the processin
1e08c 67 20 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43  g for this SELEC
1e08d 54 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53  T..  ** sqlite3S
1e08e 65 6c 65 63 74 50 72 65 70 28 29 20 77 69 6c 6c  electPrep() will
1e08f 20 69 6e 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c   invoke both sql
1e090 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64  ite3SelectExpand
1e091 28 29 20 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73  () and.  ** this
1e092 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 65 20   routine in the 
1e093 63 6f 72 72 65 63 74 20 6f 72 64 65 72 2e 0a 20  correct order.. 
1e094 20 2a 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65   */.  if( (p->se
1e095 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61  lFlags & SF_Expa
1e096 6e 64 65 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20  nded)==0 ){.    
1e097 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65  sqlite3SelectPre
1e098 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75  p(pParse, p, pOu
1e099 74 65 72 4e 43 29 3b 0a 20 20 20 20 72 65 74 75  terNC);.    retu
1e09a 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72  rn (pParse->nErr
1e09b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1e09c 69 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72  iled) ? WRC_Abor
1e09d 74 20 3a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  t : WRC_Prune;. 
1e09e 20 7d 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64   }..  isCompound
1e09f 20 3d 20 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b   = p->pPrior!=0;
1e0a0 0a 20 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30  .  nCompound = 0
1e0a1 3b 0a 20 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20  ;.  pLeftmost = 
1e0a2 70 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  p;.  while( p ){
1e0a3 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d  .    assert( (p-
1e0a4 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45  >selFlags & SF_E
1e0a5 78 70 61 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20  xpanded)!=0 );. 
1e0a6 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73     assert( (p->s
1e0a7 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73  elFlags & SF_Res
1e0a8 6f 6c 76 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20  olved)==0 );.   
1e0a9 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20   p->selFlags |= 
1e0aa 53 46 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20  SF_Resolved;..  
1e0ab 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65    /* Resolve the
1e0ac 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
1e0ad 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46  the LIMIT and OF
1e0ae 46 53 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68  FSET clauses. Th
1e0af 65 73 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e  ese.    ** are n
1e0b0 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65  ot allowed to re
1e0b1 66 65 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73  fer to any names
1e0b2 2c 20 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70  , so pass an emp
1e0b3 74 79 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a  ty NameContext..
1e0b4 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65      */.    memse
1e0b5 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
1e0b6 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43  f(sNC));.    sNC
1e0b7 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
1e0b8 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1e0b9 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
1e0ba 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69  s(&sNC, p->pLimi
1e0bb 74 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71  t) ||.        sq
1e0bc 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
1e0bd 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70  Names(&sNC, p->p
1e0be 4f 66 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20  Offset) ){.     
1e0bf 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1e0c0 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  t;.    }.  .    
1e0c1 2f 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f  /* Set up the lo
1e0c2 63 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74  cal name-context
1e0c3 20 74 6f 20 70 61 73 73 20 74 6f 20 73 71 6c 69   to pass to sqli
1e0c4 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
1e0c5 6d 65 73 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20  mes() to.    ** 
1e0c6 72 65 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75  resolve the resu
1e0c7 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt-set expressio
1e0c8 6e 20 6c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20  n list..    */. 
1e0c9 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20     sNC.allowAgg 
1e0ca 3d 20 31 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72  = 1;.    sNC.pSr
1e0cb 63 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b  cList = p->pSrc;
1e0cc 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d  .    sNC.pNext =
1e0cd 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20   pOuterNC;.  .  
1e0ce 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d    /* Resolve nam
1e0cf 65 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  es in the result
1e0d0 20 73 65 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c   set. */.    pEL
1e0d1 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b  ist = p->pEList;
1e0d2 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c  .    assert( pEL
1e0d3 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f  ist!=0 );.    fo
1e0d4 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d  r(i=0; i<pEList-
1e0d5 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
1e0d6 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70      Expr *pX = p
1e0d7 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  EList->a[i].pExp
1e0d8 72 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  r;.      if( sql
1e0d9 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
1e0da 61 6d 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29  ames(&sNC, pX) )
1e0db 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
1e0dc 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
1e0dd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
1e0de 20 2f 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20   /* Recursively 
1e0df 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e  resolve names in
1e0e0 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0a   all subqueries.
1e0e1 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
1e0e2 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e  =0; i<p->pSrc->n
1e0e3 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Src; i++){.     
1e0e4 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
1e0e5 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
1e0e6 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20  ->pSrc->a[i];.  
1e0e7 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70      if( pItem->p
1e0e8 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
1e0e9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
1e0ea 61 76 65 64 43 6f 6e 74 65 78 74 20 3d 20 70 50  avedContext = pP
1e0eb 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
1e0ec 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  xt;.        if( 
1e0ed 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70  pItem->zName ) p
1e0ee 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
1e0ef 65 78 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61  ext = pItem->zNa
1e0f0 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  me;.        sqli
1e0f1 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74  te3ResolveSelect
1e0f2 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49  Names(pParse, pI
1e0f3 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f  tem->pSelect, pO
1e0f4 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20  uterNC);.       
1e0f5 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
1e0f6 6e 74 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f  ntext = zSavedCo
1e0f7 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69  ntext;.        i
1e0f8 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
1e0f9 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
1e0fa 6c 65 64 20 29 20 72 65 74 75 72 6e 20 57 52 43  led ) return WRC
1e0fb 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a  _Abort;.      }.
1e0fc 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
1e0fd 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
1e0fe 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
1e0ff 6f 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  ons in the resul
1e100 74 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52  t-set, and no GR
1e101 4f 55 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65  OUP BY .    ** e
1e102 78 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f  xpression, do no
1e103 74 20 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74  t allow aggregat
1e104 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65  es in any of the
1e105 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f   other expressio
1e106 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  ns..    */.    a
1e107 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c  ssert( (p->selFl
1e108 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61  ags & SF_Aggrega
1e109 74 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47  te)==0 );.    pG
1e10a 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f  roupBy = p->pGro
1e10b 75 70 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47  upBy;.    if( pG
1e10c 72 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61  roupBy || sNC.ha
1e10d 73 41 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d  sAgg ){.      p-
1e10e 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
1e10f 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d  Aggregate;.    }
1e110 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e  else{.      sNC.
1e111 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20  allowAgg = 0;.  
1e112 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66    }.  .    /* If
1e113 20 61 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65   a HAVING clause
1e114 20 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65   is present, the
1e115 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20  n there must be 
1e116 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  a GROUP BY claus
1e117 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
1e118 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20  ( p->pHaving && 
1e119 21 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20  !pGroupBy ){.   
1e11a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1e11b 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 47 52  sg(pParse, "a GR
1e11c 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73  OUP BY clause is
1e11d 20 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65   required before
1e11e 20 48 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20   HAVING");.     
1e11f 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1e120 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  t;.    }.  .    
1e121 2f 2a 20 41 64 64 20 74 68 65 20 65 78 70 72 65  /* Add the expre
1e122 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68  ssion list to th
1e123 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62  e name-context b
1e124 65 66 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68  efore parsing th
1e125 65 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65  e.    ** other e
1e126 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
1e127 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
1e128 6e 74 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74  nt. This is so t
1e129 68 61 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65  hat.    ** expre
1e12a 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48  ssions in the WH
1e12b 45 52 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e  ERE clause (etc.
1e12c 29 20 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65  ) can refer to e
1e12d 78 70 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20  xpressions by.  
1e12e 20 20 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20    ** aliases in 
1e12f 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a  the result set..
1e130 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69      **.    ** Mi
1e131 6e 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68  nor point: If th
1e132 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
1e133 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73  then the express
1e134 69 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  ion will be.    
1e135 2a 2a 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20  ** re-evaluated 
1e136 66 6f 72 20 65 61 63 68 20 72 65 66 65 72 65 6e  for each referen
1e137 63 65 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f  ce to it..    */
1e138 0a 20 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20  .    sNC.pEList 
1e139 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20  = p->pEList;.   
1e13a 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
1e13b 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
1e13c 43 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c  C, p->pWhere) ||
1e13d 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  .       sqlite3R
1e13e 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
1e13f 26 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67  &sNC, p->pHaving
1e140 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
1e141 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1e142 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
1e143 68 65 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20  he ORDER BY and 
1e144 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73  GROUP BY clauses
1e145 20 6d 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74   may not refer t
1e146 6f 20 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a  o terms in.    *
1e147 2a 20 6f 75 74 65 72 20 71 75 65 72 69 65 73 20  * outer queries 
1e148 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e  .    */.    sNC.
1e149 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73  pNext = 0;.    s
1e14a 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b  NC.allowAgg = 1;
1e14b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73  ..    /* Process
1e14c 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
1e14d 61 75 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74  ause for singlet
1e14e 6f 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  on SELECT statem
1e14f 65 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65  ents..    ** The
1e150 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
1e151 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53   for compounds S
1e152 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
1e153 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20   is handled.    
1e154 2a 2a 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20  ** below, after 
1e155 61 6c 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c  all of the resul
1e156 74 2d 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f  t-sets for all o
1e157 66 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f  f the elements o
1e158 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d  f.    ** the com
1e159 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20  pound have been 
1e15a 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f  resolved..    */
1e15b 0a 20 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70  .    if( !isComp
1e15c 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f  ound && resolveO
1e15d 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43  rderGroupBy(&sNC
1e15e 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  , p, p->pOrderBy
1e15f 2c 20 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20  , "ORDER") ){.  
1e160 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
1e161 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  bort;.    }.    
1e162 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1e163 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65  iled ){.      re
1e164 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1e165 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
1e166 52 65 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55  Resolve the GROU
1e167 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74  P BY clause.  At
1e168 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20   the same time, 
1e169 6d 61 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a  make sure .    *
1e16a 2a 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63  * the GROUP BY c
1e16b 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63  lause does not c
1e16c 6f 6e 74 61 69 6e 20 61 67 67 72 65 67 61 74 65  ontain aggregate
1e16d 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20   functions..    
1e16e 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75  */.    if( pGrou
1e16f 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72  pBy ){.      str
1e170 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
1e171 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20  m *pItem;.    . 
1e172 20 20 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65       if( resolve
1e173 4f 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e  OrderGroupBy(&sN
1e174 43 2c 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20  C, p, pGroupBy, 
1e175 22 47 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e  "GROUP") || db->
1e176 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1e177 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57          return W
1e178 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
1e179 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c  }.      for(i=0,
1e17a 20 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d   pItem=pGroupBy-
1e17b 3e 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e  >a; i<pGroupBy->
1e17c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
1e17d 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  m++){.        if
1e17e 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
1e17f 79 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20  y(pItem->pExpr, 
1e180 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20  EP_Agg) ){.     
1e181 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1e182 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 67  rMsg(pParse, "ag
1e183 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
1e184 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65  s are not allowe
1e185 64 20 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20  d in ".         
1e186 20 20 20 20 20 22 74 68 65 20 47 52 4f 55 50 20       "the GROUP 
1e187 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20  BY clause");.   
1e188 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52         return WR
1e189 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20  C_Abort;.       
1e18a 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1e18b 0a 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65  ..    /* Advance
1e18c 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72   to the next ter
1e18d 6d 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e  m of the compoun
1e18e 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d  d.    */.    p =
1e18f 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20   p->pPrior;.    
1e190 6e 43 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d  nCompound++;.  }
1e191 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
1e192 68 65 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61  he ORDER BY on a
1e193 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
1e194 20 61 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73   after all terms
1e195 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d   of.  ** the com
1e196 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20  pound have been 
1e197 72 65 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20  resolved..  */. 
1e198 20 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20   if( isCompound 
1e199 26 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75  && resolveCompou
1e19a 6e 64 4f 72 64 65 72 42 79 28 70 50 61 72 73 65  ndOrderBy(pParse
1e19b 2c 20 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a  , pLeftmost) ){.
1e19c 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
1e19d 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  bort;.  }..  ret
1e19e 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d  urn WRC_Prune;.}
1e19f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
1e1a0 74 69 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78  tine walks an ex
1e1a1 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e  pression tree an
1e1a2 64 20 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72  d resolves refer
1e1a3 65 6e 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c  ences to.** tabl
1e1a4 65 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65  e columns and re
1e1a5 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73  sult-set columns
1e1a6 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74  .  At the same t
1e1a7 69 6d 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a  ime, do error.**
1e1a8 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e   checking on fun
1e1a9 63 74 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20  ction usage and 
1e1aa 73 65 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e  set a flag if an
1e1ab 79 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  y aggregate func
1e1ac 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65  tions.** are see
1e1ad 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f  n..**.** To reso
1e1ae 6c 76 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  lve table column
1e1af 73 20 72 65 66 65 72 65 6e 63 65 73 20 77 65 20  s references we 
1e1b0 6c 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28  look for nodes (
1e1b1 6f 72 20 73 75 62 74 72 65 65 73 29 20 6f 66 20  or subtrees) of 
1e1b2 74 68 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59  the .** form X.Y
1e1b3 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73  .Z or Y.Z or jus
1e1b4 74 20 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20  t Z where.**.** 
1e1b5 20 20 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61       X:   The na
1e1b6 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65  me of a database
1e1b7 2e 20 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f  .  Ex:  "main" o
1e1b8 72 20 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20  r "temp" or.**  
1e1b9 20 20 20 20 20 20 20 20 20 74 68 65 20 73 79 6d           the sym
1e1ba 62 6f 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67  bolic name assig
1e1bb 6e 65 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48  ned to an ATTACH
1e1bc 2d 65 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  -ed database..**
1e1bd 0a 2a 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68  .**      Y:   Th
1e1be 65 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c  e name of a tabl
1e1bf 65 20 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75  e in a FROM clau
1e1c0 73 65 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69  se.  Or in a tri
1e1c1 67 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  gger.**         
1e1c2 20 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65    one of the spe
1e1c3 63 69 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22  cial names "old"
1e1c4 20 6f 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a   or "new"..**.**
1e1c5 20 20 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e        Z:   The n
1e1c6 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20  ame of a column 
1e1c7 69 6e 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a  in table Y..**.*
1e1c8 2a 20 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68  * The node at th
1e1c9 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75  e root of the su
1e1ca 62 74 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65  btree is modifie
1e1cb 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  d as follows:.**
1e1cc 0a 2a 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20  .**    Expr.op  
1e1cd 20 20 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f        Changed to
1e1ce 20 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20   TK_COLUMN.**   
1e1cf 20 45 78 70 72 2e 70 54 61 62 20 20 20 20 20 20   Expr.pTab      
1e1d0 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61  Points to the Ta
1e1d1 62 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58  ble object for X
1e1d2 2e 59 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43  .Y.**    Expr.iC
1e1d3 6f 6c 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75  olumn   The colu
1e1d4 6d 6e 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e  mn index in X.Y.
1e1d5 20 20 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77    -1 for the row
1e1d6 69 64 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69  id..**    Expr.i
1e1d7 54 61 62 6c 65 20 20 20 20 54 68 65 20 56 44 42  Table    The VDB
1e1d8 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
1e1d9 66 6f 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a  for X.Y.**.**.**
1e1da 20 54 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75   To resolve resu
1e1db 6c 74 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65  lt-set reference
1e1dc 73 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72  s, look for expr
1e1dd 65 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20  ession nodes of 
1e1de 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77  the.** form Z (w
1e1df 69 74 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70  ith no X and Y p
1e1e0 72 65 66 69 78 29 20 77 68 65 72 65 20 74 68 65  refix) where the
1e1e1 20 5a 20 6d 61 74 63 68 65 73 20 74 68 65 20 72   Z matches the r
1e1e2 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a  ight-hand.** siz
1e1e3 65 20 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73  e of an AS claus
1e1e4 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d  e in the result-
1e1e5 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e  set of a SELECT.
1e1e6 20 20 54 68 65 20 5a 20 65 78 70 72 65 73 73 69    The Z expressi
1e1e7 6f 6e 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65  on.** is replace
1e1e8 64 20 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74  d by a copy of t
1e1e9 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64  he left-hand sid
1e1ea 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d  e of the result-
1e1eb 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  set expression..
1e1ec 2a 2a 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e  ** Table-name an
1e1ed 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c  d function resol
1e1ee 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20  ution occurs on 
1e1ef 74 68 65 20 73 75 62 73 74 69 74 75 74 65 64 20  the substituted 
1e1f0 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72  expression.** tr
1e1f1 65 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  ee.  For example
1e1f2 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  , in:.**.**     
1e1f3 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78   SELECT a+b AS x
1e1f4 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20  , c+d AS y FROM 
1e1f5 74 31 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a  t1 ORDER BY x;.*
1e1f6 2a 0a 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72  *.** The "x" ter
1e1f7 6d 20 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62  m of the order b
1e1f8 79 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79  y is replaced by
1e1f9 20 22 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72   "a+b" to render
1e1fa 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c  :.**.**      SEL
1e1fb 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b  ECT a+b AS x, c+
1e1fc 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f  d AS y FROM t1 O
1e1fd 52 44 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a  RDER BY a+b;.**.
1e1fe 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c  ** Function call
1e1ff 73 20 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f  s are checked to
1e200 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
1e201 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
1e202 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20  .** defined and 
1e203 74 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74  that the correct
1e204 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
1e205 65 6e 74 73 20 61 72 65 20 73 70 65 63 69 66 69  ents are specifi
1e206 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75  ed..** If the fu
1e207 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67  nction is an agg
1e208 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c  regate function,
1e209 20 74 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68   then the pNC->h
1e20a 61 73 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20  asAgg is.** set 
1e20b 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69  and the opcode i
1e20c 73 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54  s changed from T
1e20d 4b 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b  K_FUNCTION to TK
1e20e 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a  _AGG_FUNCTION..*
1e20f 2a 20 49 66 20 61 6e 20 65 78 70 72 65 73 73 69  * If an expressi
1e210 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72  on contains aggr
1e211 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
1e212 74 68 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a  then the EP_Agg.
1e213 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74  ** property on t
1e214 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1e215 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65   set..**.** An e
1e216 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20  rror message is 
1e217 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 20 69  left in pParse i
1e218 66 20 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d  f anything is am
1e219 69 73 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  iss.  The number
1e21a 0a 2a 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73  .** if errors is
1e21b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
1e21c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1e21d 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
1e21e 78 70 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d  xprNames( .  Nam
1e21f 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20  eContext *pNC,  
1e220 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63       /* Namespac
1e221 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70  e to resolve exp
1e222 72 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a  ressions in. */.
1e223 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20    Expr *pExpr   
1e224 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1e225 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62   expression to b
1e226 65 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29  e analyzed. */.)
1e227 7b 0a 20 20 69 6e 74 20 73 61 76 65 64 48 61 73  {.  int savedHas
1e228 41 67 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b  Agg;.  Walker w;
1e229 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  ..  if( pExpr==0
1e22a 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66   ) return 0;.#if
1e22b 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52   SQLITE_MAX_EXPR
1e22c 5f 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20  _DEPTH>0.  {.   
1e22d 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d   Parse *pParse =
1e22e 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pNC->pParse;.  
1e22f 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
1e230 72 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61  rCheckHeight(pPa
1e231 72 73 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69  rse, pExpr->nHei
1e232 67 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d  ght+pNC->pParse-
1e233 3e 6e 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20  >nHeight) ){.   
1e234 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1e235 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e   }.    pParse->n
1e236 48 65 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d  Height += pExpr-
1e237 3e 6e 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65  >nHeight;.  }.#e
1e238 6e 64 69 66 0a 20 20 73 61 76 65 64 48 61 73 41  ndif.  savedHasA
1e239 67 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67  gg = pNC->hasAgg
1e23a 3b 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20  ;.  pNC->hasAgg 
1e23b 3d 20 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  = 0;.  w.xExprCa
1e23c 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65  llback = resolve
1e23d 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53  ExprStep;.  w.xS
1e23e 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20  electCallback = 
1e23f 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65  resolveSelectSte
1e240 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20  p;.  w.pParse = 
1e241 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77  pNC->pParse;.  w
1e242 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20  .u.pNC = pNC;.  
1e243 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1e244 26 77 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20  &w, pExpr);.#if 
1e245 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
1e246 44 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70  DEPTH>0.  pNC->p
1e247 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d  Parse->nHeight -
1e248 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74  = pExpr->nHeight
1e249 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  ;.#endif.  if( p
1e24a 4e 43 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 20  NC->nErr>0 ){.  
1e24b 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
1e24c 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f  y(pExpr, EP_Erro
1e24d 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e  r);.  }.  if( pN
1e24e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20 20 20  C->hasAgg ){.   
1e24f 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
1e250 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29 3b  (pExpr, EP_Agg);
1e251 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 61 76  .  }else if( sav
1e252 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20 20 20  edHasAgg ){.    
1e253 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b  pNC->hasAgg = 1;
1e254 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 45 78  .  }.  return Ex
1e255 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
1e256 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b 0a  xpr, EP_Error);.
1e257 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76  }.../*.** Resolv
1e258 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 61  e all names in a
1e259 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f  ll expressions o
1e25a 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64 20 69  f a SELECT and i
1e25b 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e 64 65  n all.** decende
1e25c 6e 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43  nts of the SELEC
1e25d 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 6f 6d  T, including com
1e25e 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20 70 2d  pounds off of p-
1e25f 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75 62 71  >pPrior,.** subq
1e260 75 65 72 69 65 73 20 69 6e 20 65 78 70 72 65 73  ueries in expres
1e261 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62 71 75  sions, and subqu
1e262 65 72 69 65 73 20 75 73 65 64 20 61 73 20 46 52  eries used as FR
1e263 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74 65 72  OM clause.** ter
1e264 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 73 71  ms..**.** See sq
1e265 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
1e266 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 20 64 65  Names() for a de
1e267 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65  scription of the
1e268 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74 72 61   kinds of.** tra
1e269 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74 68 61  nsformations tha
1e26a 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 41  t occur..**.** A
1e26b 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ll SELECT statem
1e26c 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 61 76 65  ents should have
1e26d 20 62 65 65 6e 20 65 78 70 61 6e 64 65 64 20 75   been expanded u
1e26e 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 53  sing.** sqlite3S
1e26f 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 70 72  electExpand() pr
1e270 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20  ior to invoking 
1e271 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f  this routine..*/
1e272 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1e273 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 6f  void sqlite3Reso
1e274 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 0a  lveSelectNames(.
1e275 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1e276 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1e277 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
1e278 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
1e279 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1e27a 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
1e27b 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20  nt being coded. 
1e27c 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  */.  NameContext
1e27d 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e   *pOuterNC  /* N
1e27e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ame context for 
1e27f 70 61 72 65 6e 74 20 53 45 4c 45 43 54 20 73 74  parent SELECT st
1e280 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  atement */.){.  
1e281 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61 73 73  Walker w;..  ass
1e282 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 77  ert( p!=0 );.  w
1e283 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
1e284 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70   resolveExprStep
1e285 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c  ;.  w.xSelectCal
1e286 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53  lback = resolveS
1e287 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 2e 70  electStep;.  w.p
1e288 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
1e289 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f 75 74    w.u.pNC = pOut
1e28a 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57  erNC;.  sqlite3W
1e28b 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 29  alkSelect(&w, p)
1e28c 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
1e28d 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 73 6f  **** End of reso
1e28e 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  lve.c **********
1e28f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e291 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1e292 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1e293 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  expr.c *********
1e294 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e295 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e296 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
1e297 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
1e298 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1e299 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1e29a 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1e29b 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1e29c 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1e29d 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1e29e 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1e29f 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1e2a0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1e2a1 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1e2a2 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1e2a3 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1e2a4 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1e2a5 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1e2a6 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1e2a7 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1e2a8 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1e2a9 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1e2aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e2ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e2ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e2ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1e2ae 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
1e2af 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20  s routines used 
1e2b0 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 65 78  for analyzing ex
1e2b1 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a 2a 2a  pressions and.**
1e2b2 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e 67 20   for generating 
1e2b3 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 65  VDBE code that e
1e2b4 76 61 6c 75 61 74 65 73 20 65 78 70 72 65 73 73  valuates express
1e2b5 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a  ions in SQLite..
1e2b6 2a 2a 0a 2a 2a 20 24 49 64 3a 20 65 78 70 72 2e  **.** $Id: expr.
1e2b7 63 2c 76 20 31 2e 34 32 34 20 32 30 30 39 2f 30  c,v 1.424 2009/0
1e2b8 33 2f 32 35 20 31 36 3a 35 31 3a 34 33 20 64 72  3/25 16:51:43 dr
1e2b9 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
1e2ba 2a 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66  * Return the 'af
1e2bb 66 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65  finity' of the e
1e2bc 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
1e2bd 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66  if any..**.** If
1e2be 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75   pExpr is a colu
1e2bf 6d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20  mn, a reference 
1e2c0 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20  to a column via 
1e2c1 61 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a  an 'AS' alias,.*
1e2c2 2a 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63  * or a sub-selec
1e2c3 74 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20  t with a column 
1e2c4 61 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  as the return va
1e2c5 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a  lue, then the .*
1e2c6 2a 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68  * affinity of th
1e2c7 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74  at column is ret
1e2c8 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
1e2c9 2c 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e  , 0x00 is return
1e2ca 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  ed,.** indicatin
1e2cb 67 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f  g no affinity fo
1e2cc 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  r the expression
1e2cd 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65  ..**.** i.e. the
1e2ce 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78   WHERE clause ex
1e2cf 70 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68  presssions in th
1e2d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74  e following stat
1e2d1 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61  ements all.** ha
1e2d2 76 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a  ve an affinity:.
1e2d3 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42  **.** CREATE TAB
1e2d4 4c 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c  LE t1(a);.** SEL
1e2d5 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48  ECT * FROM t1 WH
1e2d6 45 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54  ERE a;.** SELECT
1e2d7 20 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20   a AS b FROM t1 
1e2d8 57 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45  WHERE b;.** SELE
1e2d9 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
1e2da 52 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f  RE (select a fro
1e2db 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45  m t1);.*/.SQLITE
1e2dc 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
1e2dd 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
1e2de 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  y(Expr *pExpr){.
1e2df 20 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72    int op = pExpr
1e2e0 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  ->op;.  if( op==
1e2e1 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20  TK_SELECT ){.   
1e2e2 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1e2e3 66 6c 61 67 73 26 45 50 5f 78 49 73 53 65 6c 65  flags&EP_xIsSele
1e2e4 63 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ct );.    return
1e2e5 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
1e2e6 6e 69 74 79 28 70 45 78 70 72 2d 3e 78 2e 70 53  nity(pExpr->x.pS
1e2e7 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61  elect->pEList->a
1e2e8 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a  [0].pExpr);.  }.
1e2e9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1e2ea 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f  MIT_CAST.  if( o
1e2eb 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20  p==TK_CAST ){.  
1e2ec 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1e2ed 41 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45  AffinityType(&pE
1e2ee 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d  xpr->token);.  }
1e2ef 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f  .#endif.  if( (o
1e2f0 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
1e2f1 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d   || op==TK_COLUM
1e2f2 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49  N || op==TK_REGI
1e2f3 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78  STER) .   && pEx
1e2f4 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b  pr->pTab!=0.  ){
1e2f5 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52  .    /* op==TK_R
1e2f6 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72  EGISTER && pExpr
1e2f7 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e  ->pTab!=0 happen
1e2f8 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73  s when pExpr was
1e2f9 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20   originally.    
1e2fa 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62  ** a TK_COLUMN b
1e2fb 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  ut was previousl
1e2fc 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  y evaluated and 
1e2fd 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69  cached in a regi
1e2fe 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ster */.    int 
1e2ff 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  j = pExpr->iColu
1e300 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20  mn;.    if( j<0 
1e301 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1e302 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
1e303 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1e304 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d  pTab && j<pExpr-
1e305 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  >pTab->nCol );. 
1e306 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d     return pExpr-
1e307 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61  >pTab->aCol[j].a
1e308 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72  ffinity;.  }.  r
1e309 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66  eturn pExpr->aff
1e30a 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  inity;.}../*.** 
1e30b 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  Set the collatin
1e30c 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65  g sequence for e
1e30d 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
1e30e 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74  to be the collat
1e30f 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ing.** sequence 
1e310 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e  named by pToken.
1e311 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e     Return a poin
1e312 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73  ter to the revis
1e313 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  ed expression..*
1e314 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  * The collating 
1e315 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b  sequence is mark
1e316 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22  ed as "explicit"
1e317 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78   using the EP_Ex
1e318 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67  pCollate.** flag
1e319 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63  .  An explicit c
1e31a 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1e31b 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20  e will override 
1e31c 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c  implicit.** coll
1e31d 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e  ating sequences.
1e31e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1e31f 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
1e320 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73  ExprSetColl(Pars
1e321 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1e322 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70  *pExpr, Token *p
1e323 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61  CollName){.  cha
1e324 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20  r *zColl = 0;   
1e325 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75           /* Dequ
1e326 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c  oted name of col
1e327 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
1e328 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  */.  CollSeq *pC
1e329 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  oll;.  sqlite3 *
1e32a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1e32b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  zColl = sqlit
1e32c 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1e32d 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a  db, pCollName);.
1e32e 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a    if( pExpr && z
1e32f 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Coll ){.    pCol
1e330 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  l = sqlite3Locat
1e331 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
1e332 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20   zColl, -1);.   
1e333 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
1e334 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c      pExpr->pColl
1e335 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20   = pColl;.      
1e336 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20  pExpr->flags |= 
1e337 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20  EP_ExpCollate;. 
1e338 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
1e339 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
1e33a 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 45  ll);.  return pE
1e33b 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  xpr;.}../*.** Re
1e33c 74 75 72 6e 20 74 68 65 20 64 65 66 61 75 6c 74  turn the default
1e33d 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
1e33e 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78 70 72  nce for the expr
1e33f 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66  ession pExpr. If
1e340 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20  .** there is no 
1e341 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f  default collatio
1e342 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e 20 30  n type, return 0
1e343 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e344 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  ATE CollSeq *sql
1e345 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
1e346 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
1e347 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 43  xpr *pExpr){.  C
1e348 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
1e349 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 70  0;.  Expr *p = p
1e34a 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 70  Expr;.  while( p
1e34b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 3b 0a   ){.    int op;.
1e34c 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 2d 3e 70      pColl = p->p
1e34d 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20 70 43  Coll;.    if( pC
1e34e 6f 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  oll ) break;.   
1e34f 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 20   op = p->op;.   
1e350 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47   if( (op==TK_AGG
1e351 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54  _COLUMN || op==T
1e352 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d  K_COLUMN || op==
1e353 54 4b 5f 52 45 47 49 53 54 45 52 29 20 26 26 20  TK_REGISTER) && 
1e354 70 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20  p->pTab!=0 ){.  
1e355 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45      /* op==TK_RE
1e356 47 49 53 54 45 52 20 26 26 20 70 2d 3e 70 54 61  GISTER && p->pTa
1e357 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65  b!=0 happens whe
1e358 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72 69 67  n pExpr was orig
1e359 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20  inally.      ** 
1e35a 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 74 20  a TK_COLUMN but 
1e35b 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 65  was previously e
1e35c 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 61 63  valuated and cac
1e35d 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 74 65  hed in a registe
1e35e 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74  r */.      const
1e35f 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20   char *zColl;.  
1e360 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d 3e 69      int j = p->i
1e361 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69 66  Column;.      if
1e362 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( j>=0 ){.      
1e363 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1e364 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20  pParse->db;.    
1e365 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d 3e 70      zColl = p->p
1e366 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f  Tab->aCol[j].zCo
1e367 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c  ll;.        pCol
1e368 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
1e369 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
1e36a 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c 20 30  b), zColl, -1, 0
1e36b 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  );.        pExpr
1e36c 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b  ->pColl = pColl;
1e36d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1e36e 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1e36f 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20  if( op!=TK_CAST 
1e370 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20  && op!=TK_UPLUS 
1e371 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ){.      break;.
1e372 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d      }.    p = p-
1e373 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66  >pLeft;.  }.  if
1e374 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f  ( sqlite3CheckCo
1e375 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43  llSeq(pParse, pC
1e376 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f  oll) ){ .    pCo
1e377 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ll = 0;.  }.  re
1e378 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  turn pColl;.}../
1e379 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e  *.** pExpr is an
1e37a 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f   operand of a co
1e37b 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f  mparison operato
1e37c 72 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a  r.  aff2 is the.
1e37d 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79  ** type affinity
1e37e 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70   of the other op
1e37f 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75  erand.  This rou
1e380 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
1e381 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74  .** type affinit
1e382 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  y that should be
1e383 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f   used for the co
1e384 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f  mparison operato
1e385 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
1e386 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65  VATE char sqlite
1e387 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
1e388 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
1e389 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72  ar aff2){.  char
1e38a 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45   aff1 = sqlite3E
1e38b 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70  xprAffinity(pExp
1e38c 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26  r);.  if( aff1 &
1e38d 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a  & aff2 ){.    /*
1e38e 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74   Both sides of t
1e38f 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72  he comparison ar
1e390 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e  e columns. If on
1e391 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20  e has numeric.  
1e392 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75    ** affinity, u
1e393 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69  se that. Otherwi
1e394 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69  se use no affini
1e395 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ty..    */.    i
1e396 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65  f( sqlite3IsNume
1e397 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31  ricAffinity(aff1
1e398 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75  ) || sqlite3IsNu
1e399 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66  mericAffinity(af
1e39a 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  f2) ){.      ret
1e39b 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  urn SQLITE_AFF_N
1e39c 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73  UMERIC;.    }els
1e39d 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
1e39e 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
1e39f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
1e3a0 66 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66  f( !aff1 && !aff
1e3a1 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74  2 ){.    /* Neit
1e3a2 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20  her side of the 
1e3a3 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20  comparison is a 
1e3a4 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65  column.  Compare
1e3a5 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75   the.    ** resu
1e3a6 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20  lts directly..  
1e3a7 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20    */.    return 
1e3a8 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
1e3a9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1e3aa 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63   One side is a c
1e3ab 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72  olumn, the other
1e3ac 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65   is not. Use the
1e3ad 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74   columns affinit
1e3ae 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  y. */.    assert
1e3af 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66  ( aff1==0 || aff
1e3b0 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75  2==0 );.    retu
1e3b1 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29  rn (aff1 + aff2)
1e3b2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  ;.  }.}../*.** p
1e3b3 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72  Expr is a compar
1e3b4 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20  ison operator.  
1e3b5 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20  Return the type 
1e3b6 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
1e3b7 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69  ould.** be appli
1e3b8 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61  ed to both opera
1e3b9 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69  nds prior to doi
1e3ba 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ng the compariso
1e3bb 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  n..*/.static cha
1e3bc 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69  r comparisonAffi
1e3bd 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
1e3be 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20  ){.  char aff;. 
1e3bf 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1e3c0 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78  op==TK_EQ || pEx
1e3c1 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c  pr->op==TK_IN ||
1e3c2 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c   pExpr->op==TK_L
1e3c3 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70  T ||.          p
1e3c4 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20  Expr->op==TK_GT 
1e3c5 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  || pExpr->op==TK
1e3c6 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  _GE || pExpr->op
1e3c7 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20  ==TK_LE ||.     
1e3c8 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d       pExpr->op==
1e3c9 54 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72  TK_NE );.  asser
1e3ca 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20  t( pExpr->pLeft 
1e3cb 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74  );.  aff = sqlit
1e3cc 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70  e3ExprAffinity(p
1e3cd 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
1e3ce 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68  if( pExpr->pRigh
1e3cf 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73  t ){.    aff = s
1e3d0 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
1e3d1 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69  inity(pExpr->pRi
1e3d2 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c  ght, aff);.  }el
1e3d3 73 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72  se if( ExprHasPr
1e3d4 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1e3d5 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
1e3d6 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33     aff = sqlite3
1e3d7 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
1e3d8 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
1e3d9 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  ->pEList->a[0].p
1e3da 45 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65  Expr, aff);.  }e
1e3db 6c 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a  lse if( !aff ){.
1e3dc 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
1e3dd 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20  _AFF_NONE;.  }. 
1e3de 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a   return aff;.}..
1e3df 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61  /*.** pExpr is a
1e3e0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72   comparison expr
1e3e1 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c  ession, eg. '=',
1e3e2 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74   '<', IN(...) et
1e3e3 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69  c..** idx_affini
1e3e4 74 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69  ty is the affini
1e3e5 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64  ty of an indexed
1e3e6 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20   column. Return 
1e3e7 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69  true.** if the i
1e3e8 6e 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69  ndex with affini
1e3e9 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20  ty idx_affinity 
1e3ea 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69  may be used to i
1e3eb 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20  mplement.** the 
1e3ec 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45  comparison in pE
1e3ed 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  xpr..*/.SQLITE_P
1e3ee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1e3ef 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f  e3IndexAffinityO
1e3f0 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63  k(Expr *pExpr, c
1e3f1 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79  har idx_affinity
1e3f2 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20  ){.  char aff = 
1e3f3 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69  comparisonAffini
1e3f4 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69  ty(pExpr);.  swi
1e3f5 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20  tch( aff ){.    
1e3f6 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  case SQLITE_AFF_
1e3f7 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75  NONE:.      retu
1e3f8 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53  rn 1;.    case S
1e3f9 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a  QLITE_AFF_TEXT:.
1e3fa 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78        return idx
1e3fb 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54  _affinity==SQLIT
1e3fc 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20  E_AFF_TEXT;.    
1e3fd 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72  default:.      r
1e3fe 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e  eturn sqlite3IsN
1e3ff 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69  umericAffinity(i
1e400 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20  dx_affinity);.  
1e401 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
1e402 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74  n the P5 value t
1e403 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73  hat should be us
1e404 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20  ed for a binary 
1e405 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70  comparison.** op
1e406 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f  code (OP_Eq, OP_
1e407 47 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f  Ge etc.) used to
1e408 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20   compare pExpr1 
1e409 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73  and pExpr2..*/.s
1e40a 74 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43  tatic u8 binaryC
1e40b 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70  ompareP5(Expr *p
1e40c 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78  Expr1, Expr *pEx
1e40d 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e  pr2, int jumpIfN
1e40e 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d  ull){.  u8 aff =
1e40f 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78   (char)sqlite3Ex
1e410 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  prAffinity(pExpr
1e411 32 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29  2);.  aff = (u8)
1e412 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
1e413 66 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61  finity(pExpr1, a
1e414 66 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66  ff) | (u8)jumpIf
1e415 4e 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61  Null;.  return a
1e416 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ff;.}../*.** Ret
1e417 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
1e418 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
1e419 65 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f  equence that sho
1e41a 75 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a  uld be used by.*
1e41b 2a 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61  * a binary compa
1e41c 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63  rison operator c
1e41d 6f 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61  omparing pLeft a
1e41e 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a  nd pRight..**.**
1e41f 20 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e   If the left han
1e420 64 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73  d expression has
1e421 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71   a collating seq
1e422 75 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e  uence type, then
1e423 20 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20   it is.** used. 
1e424 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f  Otherwise the co
1e425 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
1e426 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68   for the right h
1e427 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  and expression.*
1e428 2a 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68  * is used, or th
1e429 65 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52  e default (BINAR
1e42a 59 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78  Y) if neither ex
1e42b 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63  pression has a c
1e42c 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65  ollating.** type
1e42d 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ..**.** Argument
1e42e 20 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74   pRight (but not
1e42f 20 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61   pLeft) may be a
1e430 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49   null pointer. I
1e431 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20  n this case,.** 
1e432 69 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64  it is not consid
1e433 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ered..*/.SQLITE_
1e434 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
1e435 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f  *sqlite3BinaryCo
1e436 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20  mpareCollSeq(.  
1e437 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a  Parse *pParse, .
1e438 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a    Expr *pLeft, .
1e439 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29    Expr *pRight.)
1e43a 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
1e43b 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  ll;.  assert( pL
1e43c 65 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65  eft );.  if( pLe
1e43d 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  ft->flags & EP_E
1e43e 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20  xpCollate ){.   
1e43f 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e   assert( pLeft->
1e440 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f  pColl );.    pCo
1e441 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c  ll = pLeft->pCol
1e442 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  l;.  }else if( p
1e443 52 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d  Right && pRight-
1e444 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43  >flags & EP_ExpC
1e445 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73  ollate ){.    as
1e446 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43  sert( pRight->pC
1e447 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c  oll );.    pColl
1e448 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c   = pRight->pColl
1e449 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1e44a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
1e44b 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
1e44c 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66  , pLeft);.    if
1e44d 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
1e44e 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
1e44f 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
1e450 72 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20  rse, pRight);.  
1e451 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1e452 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pColl;.}../*.**
1e453 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70   Generate the op
1e454 65 72 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d  erands for a com
1e455 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f  parison operatio
1e456 6e 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65  n.  Before.** ge
1e457 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64  nerating the cod
1e458 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61  e for each opera
1e459 6e 64 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41  nd, set the EP_A
1e45a 6e 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e  nyAff.** flag on
1e45b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1e45c 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20  so that it will 
1e45d 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20  be able to used 
1e45e 61 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75  a.** cached colu
1e45f 6d 6e 20 76 61 6c 75 65 20 74 68 61 74 20 68 61  mn value that ha
1e460 73 20 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64  s previously und
1e461 65 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66  ergone an.** aff
1e462 69 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f  inity change..*/
1e463 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
1e464 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73  eCompareOperands
1e465 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1e466 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  e,    /* Parsing
1e467 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61   and code genera
1e468 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ting context */.
1e469 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20    Expr *pLeft,  
1e46a 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20      /* The left 
1e46b 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74  operand */.  int
1e46c 20 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f   *pRegLeft,    /
1e46d 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 72 65  * Register where
1e46e 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73   left operand is
1e46f 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74   stored */.  int
1e470 20 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f   *pFreeLeft,   /
1e471 2a 20 46 72 65 65 20 74 68 69 73 20 72 65 67 69  * Free this regi
1e472 73 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a  ster when done *
1e473 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  /.  Expr *pRight
1e474 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67  ,     /* The rig
1e475 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
1e476 69 6e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20  int *pRegRight, 
1e477 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68    /* Register wh
1e478 65 72 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  ere right operan
1e479 64 20 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20  d is stored */. 
1e47a 20 69 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74   int *pFreeRight
1e47b 20 20 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70     /* Write temp
1e47c 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 69   register for ri
1e47d 67 68 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72  ght operand ther
1e47e 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28  e */.){.  while(
1e47f 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55   pLeft->op==TK_U
1e480 50 4c 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70  PLUS ) pLeft = p
1e481 4c 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70  Left->pLeft;.  p
1e482 4c 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Left->flags |= E
1e483 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65  P_AnyAff;.  *pRe
1e484 67 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  gLeft = sqlite3E
1e485 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
1e486 73 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65  se, pLeft, pFree
1e487 4c 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20  Left);.  while( 
1e488 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55  pRight->op==TK_U
1e489 50 4c 55 53 20 29 20 70 52 69 67 68 74 20 3d 20  PLUS ) pRight = 
1e48a 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20  pRight->pLeft;. 
1e48b 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c   pRight->flags |
1e48c 3d 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a  = EP_AnyAff;.  *
1e48d 70 52 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69  pRegRight = sqli
1e48e 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
1e48f 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20  pParse, pRight, 
1e490 70 46 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a  pFreeRight);.}..
1e491 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
1e492 6f 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72  ode for a compar
1e493 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  ison operator..*
1e494 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64  /.static int cod
1e495 65 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73  eCompare(.  Pars
1e496 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a  e *pParse,    /*
1e497 20 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e   The parsing (an
1e498 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
1e499 67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  g) context */.  
1e49a 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20  Expr *pLeft,    
1e49b 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70    /* The left op
1e49c 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20  erand */.  Expr 
1e49d 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20  *pRight,     /* 
1e49e 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  The right operan
1e49f 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64  d */.  int opcod
1e4a0 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e,       /* The 
1e4a1 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64  comparison opcod
1e4a2 65 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20  e */.  int in1, 
1e4a3 69 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69  int in2, /* Regi
1e4a4 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65  ster holding ope
1e4a5 72 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64  rands */.  int d
1e4a6 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  est,         /* 
1e4a7 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75  Jump here if tru
1e4a8 65 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d  e.  */.  int jum
1e4a9 70 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66  pIfNull    /* If
1e4aa 20 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65   true, jump if e
1e4ab 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1e4ac 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e   NULL */.){.  in
1e4ad 74 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72  t p5;.  int addr
1e4ae 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b  ;.  CollSeq *p4;
1e4af 0a 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33  ..  p4 = sqlite3
1e4b0 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
1e4b1 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65  lSeq(pParse, pLe
1e4b2 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70  ft, pRight);.  p
1e4b3 35 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72  5 = binaryCompar
1e4b4 65 50 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68  eP5(pLeft, pRigh
1e4b5 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
1e4b6 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
1e4b7 56 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73  VdbeAddOp4(pPars
1e4b8 65 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65  e->pVdbe, opcode
1e4b9 2c 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31  , in2, dest, in1
1e4ba 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1e4bb 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f               (vo
1e4bc 69 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53  id*)p4, P4_COLLS
1e4bd 45 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  EQ);.  sqlite3Vd
1e4be 62 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73  beChangeP5(pPars
1e4bf 65 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35  e->pVdbe, (u8)p5
1e4c0 29 3b 0a 20 20 69 66 28 20 28 70 35 20 26 20 53  );.  if( (p5 & S
1e4c1 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21  QLITE_AFF_MASK)!
1e4c2 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
1e4c3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1e4c4 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
1e4c5 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69  Change(pParse, i
1e4c6 6e 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  n1, 1);.    sqli
1e4c7 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
1e4c8 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
1e4c9 65 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a  e, in2, 1);.  }.
1e4ca 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d    return addr;.}
1e4cb 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ..#if SQLITE_MAX
1e4cc 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a  _EXPR_DEPTH>0./*
1e4cd 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61  .** Check that a
1e4ce 72 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20  rgument nHeight 
1e4cf 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
1e4d0 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78  equal to the max
1e4d1 69 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69  imum.** expressi
1e4d2 6f 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64  on depth allowed
1e4d3 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20  . If it is not, 
1e4d4 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
1e4d5 65 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61  essage in.** pPa
1e4d6 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rse..*/.SQLITE_P
1e4d7 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1e4d8 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68  e3ExprCheckHeigh
1e4d9 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
1e4da 20 69 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20   int nHeight){. 
1e4db 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1e4dc 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69  _OK;.  int mxHei
1e4dd 67 68 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  ght = pParse->db
1e4de 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1e4df 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48  LIMIT_EXPR_DEPTH
1e4e0 5d 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74  ];.  if( nHeight
1e4e1 3e 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20  >mxHeight ){.   
1e4e2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1e4e3 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
1e4e4 20 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65   "Expression tre
1e4e5 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28  e is too large (
1e4e6 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64  maximum depth %d
1e4e7 29 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20  )", mxHeight.   
1e4e8 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   );.    rc = SQL
1e4e9 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
1e4ea 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1e4eb 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1e4ec 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c  three functions,
1e4ed 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c   heightOfExpr(),
1e4ee 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
1e4ef 74 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68  t().** and heigh
1e4f0 74 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65  tOfSelect(), are
1e4f1 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
1e4f2 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68  ne the maximum h
1e4f3 65 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20  eight.** of any 
1e4f4 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
1e4f5 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68  referenced by th
1e4f6 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  e structure pass
1e4f7 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72  ed as the.** fir
1e4f8 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  st argument..**.
1e4f9 2a 2a 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d  ** If this maxim
1e4fa 75 6d 20 68 65 69 67 68 74 20 69 73 20 67 72 65  um height is gre
1e4fb 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75  ater than the cu
1e4fc 72 72 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e  rrent value poin
1e4fd 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48  ted.** to by pnH
1e4fe 65 69 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e  eight, the secon
1e4ff 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65  d parameter, the
1e500 6e 20 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20  n set *pnHeight 
1e501 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65  to that.** value
1e502 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1e503 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 45 78   heightOfExpr(Ex
1e504 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65  pr *p, int *pnHe
1e505 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29  ight){.  if( p )
1e506 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65  {.    if( p->nHe
1e507 69 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29  ight>*pnHeight )
1e508 7b 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68  {.      *pnHeigh
1e509 74 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a  t = p->nHeight;.
1e50a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74      }.  }.}.stat
1e50b 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66  ic void heightOf
1e50c 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73  ExprList(ExprLis
1e50d 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69  t *p, int *pnHei
1e50e 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b  ght){.  if( p ){
1e50f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1e510 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45  for(i=0; i<p->nE
1e511 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
1e512 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d   heightOfExpr(p-
1e513 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48  >a[i].pExpr, pnH
1e514 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20  eight);.    }.  
1e515 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
1e516 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53  heightOfSelect(S
1e517 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70  elect *p, int *p
1e518 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20  nHeight){.  if( 
1e519 70 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f  p ){.    heightO
1e51a 66 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c  fExpr(p->pWhere,
1e51b 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20   pnHeight);.    
1e51c 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e  heightOfExpr(p->
1e51d 70 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68  pHaving, pnHeigh
1e51e 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66  t);.    heightOf
1e51f 45 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20  Expr(p->pLimit, 
1e520 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
1e521 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
1e522 4f 66 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74  Offset, pnHeight
1e523 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45  );.    heightOfE
1e524 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73  xprList(p->pELis
1e525 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  t, pnHeight);.  
1e526 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69    heightOfExprLi
1e527 73 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20  st(p->pGroupBy, 
1e528 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
1e529 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28  eightOfExprList(
1e52a 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48  p->pOrderBy, pnH
1e52b 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
1e52c 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50  htOfSelect(p->pP
1e52d 72 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b  rior, pnHeight);
1e52e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
1e52f 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67  t the Expr.nHeig
1e530 68 74 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74  ht variable in t
1e531 68 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73  he structure pas
1e532 73 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72  sed as an .** ar
1e533 67 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65  gument. An expre
1e534 73 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68  ssion with no ch
1e535 69 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69  ildren, Expr.pLi
1e536 73 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70  st or .** Expr.p
1e537 53 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61  Select member ha
1e538 73 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e  s a height of 1.
1e539 20 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65   Any other expre
1e53a 73 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68  ssion.** has a h
1e53b 65 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74  eight equal to t
1e53c 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68  he maximum heigh
1e53d 74 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a  t of any other .
1e53e 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 45 78  ** referenced Ex
1e53f 70 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a  pr plus one..*/.
1e540 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72  static void expr
1e541 53 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a  SetHeight(Expr *
1e542 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68  p){.  int nHeigh
1e543 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f  t = 0;.  heightO
1e544 66 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20  fExpr(p->pLeft, 
1e545 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69  &nHeight);.  hei
1e546 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69  ghtOfExpr(p->pRi
1e547 67 68 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a  ght, &nHeight);.
1e548 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
1e549 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53  perty(p, EP_xIsS
1e54a 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65  elect) ){.    he
1e54b 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e  ightOfSelect(p->
1e54c 78 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69  x.pSelect, &nHei
1e54d 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ght);.  }else{. 
1e54e 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c     heightOfExprL
1e54f 69 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20  ist(p->x.pList, 
1e550 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20  &nHeight);.  }. 
1e551 20 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48   p->nHeight = nH
1e552 65 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a  eight + 1;.}../*
1e553 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72  .** Set the Expr
1e554 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c  .nHeight variabl
1e555 65 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72  e using the expr
1e556 53 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63  SetHeight() func
1e557 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20  tion. If.** the 
1e558 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65  height is greate
1e559 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d  r than the maxim
1e55a 75 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65  um allowed expre
1e55b 73 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20  ssion depth,.** 
1e55c 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69  leave an error i
1e55d 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c  n pParse..*/.SQL
1e55e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1e55f 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
1e560 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61  eight(Parse *pPa
1e561 72 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20  rse, Expr *p){. 
1e562 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70   exprSetHeight(p
1e563 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
1e564 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72  CheckHeight(pPar
1e565 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b  se, p->nHeight);
1e566 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1e567 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69   the maximum hei
1e568 67 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65  ght of any expre
1e569 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72  ssion tree refer
1e56a 65 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  enced.** by the 
1e56b 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
1e56c 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72   passed as an ar
1e56d 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
1e56e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1e56f 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48  lite3SelectExprH
1e570 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29  eight(Select *p)
1e571 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20  {.  int nHeight 
1e572 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53  = 0;.  heightOfS
1e573 65 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68  elect(p, &nHeigh
1e574 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65  t);.  return nHe
1e575 69 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20  ight;.}.#else.  
1e576 23 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48  #define exprSetH
1e577 65 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20  eight(y).#endif 
1e578 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  /* SQLITE_MAX_EX
1e579 50 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f  PR_DEPTH>0 */../
1e57a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61  *.** Construct a
1e57b 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20   new expression 
1e57c 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 72 6e 20  node and return 
1e57d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
1e57e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72 20    Memory.** for 
1e57f 74 68 69 73 20 6e 6f 64 65 20 69 73 20 6f 62 74  this node is obt
1e580 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
1e581 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 54 68  e3_malloc().  Th
1e582 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
1e583 6f 6e 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73  on.** is respons
1e584 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20  ible for making 
1e585 73 75 72 65 20 74 68 65 20 6e 6f 64 65 20 65 76  sure the node ev
1e586 65 6e 74 75 61 6c 6c 79 20 67 65 74 73 20 66 72  entually gets fr
1e587 65 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  eed..*/.SQLITE_P
1e588 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
1e589 69 74 65 33 45 78 70 72 28 0a 20 20 73 71 6c 69  ite3Expr(.  sqli
1e58a 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
1e58b 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f      /* Handle fo
1e58c 72 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  r sqlite3DbMallo
1e58d 63 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20  cZero() (may be 
1e58e 6e 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f  null) */.  int o
1e58f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
1e590 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
1e591 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70   opcode */.  Exp
1e592 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20  r *pLeft,       
1e593 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65       /* Left ope
1e594 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a  rand */.  Expr *
1e595 70 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20  pRight,         
1e596 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61    /* Right opera
1e597 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f  nd */.  const To
1e598 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20  ken *pToken     
1e599 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65  /* Argument toke
1e59a 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a  n */.){.  Expr *
1e59b 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73  pNew;.  pNew = s
1e59c 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1e59d 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78  ro(db, sizeof(Ex
1e59e 70 72 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  pr));.  if( pNew
1e59f 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68  ==0 ){.    /* Wh
1e5a0 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c  en malloc fails,
1e5a1 20 64 65 6c 65 74 65 20 70 4c 65 66 74 20 61 6e   delete pLeft an
1e5a2 64 20 70 52 69 67 68 74 2e 20 45 78 70 72 65 73  d pRight. Expres
1e5a3 73 69 6f 6e 73 20 70 61 73 73 65 64 20 74 6f 20  sions passed to 
1e5a4 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 66 75 6e  .    ** this fun
1e5a5 63 74 69 6f 6e 20 6d 75 73 74 20 61 6c 77 61 79  ction must alway
1e5a6 73 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 77  s be allocated w
1e5a7 69 74 68 20 73 71 6c 69 74 65 33 45 78 70 72 28  ith sqlite3Expr(
1e5a8 29 20 66 6f 72 20 74 68 69 73 20 0a 20 20 20 20  ) for this .    
1e5a9 2a 2a 20 72 65 61 73 6f 6e 2e 20 0a 20 20 20 20  ** reason. .    
1e5aa 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  */.    sqlite3Ex
1e5ab 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 65  prDelete(db, pLe
1e5ac 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ft);.    sqlite3
1e5ad 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1e5ae 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75  Right);.    retu
1e5af 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77  rn 0;.  }.  pNew
1e5b0 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20  ->op = (u8)op;. 
1e5b1 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70   pNew->pLeft = p
1e5b2 4c 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 52  Left;.  pNew->pR
1e5b3 69 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20  ight = pRight;. 
1e5b4 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31   pNew->iAgg = -1
1e5b5 3b 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a  ;.  pNew->span.z
1e5b6 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 69 66   = (u8*)"";.  if
1e5b7 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20  ( pToken ){.    
1e5b8 69 6e 74 20 63 3b 0a 20 20 20 20 61 73 73 65 72  int c;.    asser
1e5b9 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d  t( pToken->dyn==
1e5ba 30 20 29 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 73  0 );.    pNew->s
1e5bb 70 61 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20  pan = *pToken;. 
1e5bc 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 54 6f 6b  .    /* The pTok
1e5bd 65 6e 2d 3e 7a 20 76 61 6c 75 65 20 69 73 20 72  en->z value is r
1e5be 65 61 64 2d 6f 6e 6c 79 2e 20 20 42 75 74 20 74  ead-only.  But t
1e5bf 68 65 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f  he new expressio
1e5c0 6e 0a 20 20 20 20 2a 2a 20 6e 6f 64 65 20 63 72  n.    ** node cr
1e5c1 65 61 74 65 64 20 68 65 72 65 20 6d 69 67 68 74  eated here might
1e5c2 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71   be passed to sq
1e5c3 6c 69 74 65 33 44 65 71 75 6f 74 65 45 78 70 72  lite3DequoteExpr
1e5c4 28 29 20 77 68 69 63 68 0a 20 20 20 20 2a 2a 20  () which.    ** 
1e5c5 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20  will attempt to 
1e5c6 6d 6f 64 69 66 79 20 70 4e 65 77 2d 3e 74 6f 6b  modify pNew->tok
1e5c7 65 6e 2e 7a 2e 20 20 48 65 6e 63 65 2c 20 69 66  en.z.  Hence, if
1e5c8 20 74 68 65 20 74 6f 6b 65 6e 0a 20 20 20 20 2a   the token.    *
1e5c9 2a 20 69 73 20 71 75 6f 74 65 64 2c 20 6d 61 6b  * is quoted, mak
1e5ca 65 20 61 20 63 6f 70 79 20 6e 6f 77 20 73 6f 20  e a copy now so 
1e5cb 74 68 61 74 20 44 65 71 75 6f 74 65 45 78 70 72  that DequoteExpr
1e5cc 28 29 20 77 69 6c 6c 20 63 68 61 6e 67 65 0a 20  () will change. 
1e5cd 20 20 20 2a 2a 20 74 68 65 20 63 6f 70 79 20 72     ** the copy r
1e5ce 61 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f  ather than the o
1e5cf 72 69 67 69 6e 61 6c 20 74 65 78 74 2e 0a 20 20  riginal text..  
1e5d0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 6f    */.    if( pTo
1e5d1 6b 65 6e 2d 3e 6e 3e 3d 32 20 0a 20 20 20 20 20  ken->n>=2 .     
1e5d2 20 20 20 20 26 26 20 28 28 63 20 3d 20 70 54 6f      && ((c = pTo
1e5d3 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 27  ken->z[0])=='\''
1e5d4 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63 3d   || c=='"' || c=
1e5d5 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29 20  ='[' || c=='`') 
1e5d6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e5d7 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70  TokenCopy(db, &p
1e5d8 4e 65 77 2d 3e 74 6f 6b 65 6e 2c 20 70 54 6f 6b  New->token, pTok
1e5d9 65 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  en);.    }else{.
1e5da 20 20 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65        pNew->toke
1e5db 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 20  n = *pToken;.   
1e5dc 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c     pNew->flags |
1e5dd 3d 20 45 50 5f 44 65 71 75 6f 74 65 64 3b 0a 20  = EP_Dequoted;. 
1e5de 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70       VVA_ONLY( p
1e5df 4e 65 77 2d 3e 76 76 61 46 6c 61 67 73 20 7c 3d  New->vvaFlags |=
1e5e0 20 45 56 56 41 5f 52 65 61 64 4f 6e 6c 79 54 6f   EVVA_ReadOnlyTo
1e5e1 6b 65 6e 3b 20 29 0a 20 20 20 20 7d 0a 20 20 7d  ken; ).    }.  }
1e5e2 65 6c 73 65 20 69 66 28 20 70 4c 65 66 74 20 29  else if( pLeft )
1e5e3 7b 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74  {.    if( pRight
1e5e4 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 52   ){.      if( pR
1e5e5 69 67 68 74 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d  ight->span.dyn==
1e5e6 30 20 26 26 20 70 4c 65 66 74 2d 3e 73 70 61 6e  0 && pLeft->span
1e5e7 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  .dyn==0 ){.     
1e5e8 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70     sqlite3ExprSp
1e5e9 61 6e 28 70 4e 65 77 2c 20 26 70 4c 65 66 74 2d  an(pNew, &pLeft-
1e5ea 3e 73 70 61 6e 2c 20 26 70 52 69 67 68 74 2d 3e  >span, &pRight->
1e5eb 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  span);.      }. 
1e5ec 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d       if( pRight-
1e5ed 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43  >flags & EP_ExpC
1e5ee 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20  ollate ){.      
1e5ef 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d    pNew->flags |=
1e5f0 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a   EP_ExpCollate;.
1e5f1 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 43          pNew->pC
1e5f2 6f 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43  oll = pRight->pC
1e5f3 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
1e5f4 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74   }.    if( pLeft
1e5f5 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
1e5f6 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20  Collate ){.     
1e5f7 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20   pNew->flags |= 
1e5f8 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20  EP_ExpCollate;. 
1e5f9 20 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c       pNew->pColl
1e5fa 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b   = pLeft->pColl;
1e5fb 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 65 78  .    }.  }..  ex
1e5fc 70 72 53 65 74 48 65 69 67 68 74 28 70 4e 65 77  prSetHeight(pNew
1e5fd 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  );.  return pNew
1e5fe 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 6f 72 6b 73  ;.}../*.** Works
1e5ff 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 45 78 70   like sqlite3Exp
1e600 72 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20  r() except that 
1e601 69 74 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72  it takes an extr
1e602 61 20 50 61 72 73 65 2a 0a 2a 2a 20 61 72 67 75  a Parse*.** argu
1e603 6d 65 6e 74 20 61 6e 64 20 6e 6f 74 69 66 69 65  ment and notifie
1e604 73 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  s the associated
1e605 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65   connection obje
1e606 63 74 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69  ct if malloc fai
1e607 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ls..*/.SQLITE_PR
1e608 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
1e609 74 65 33 50 45 78 70 72 28 0a 20 20 50 61 72 73  te3PExpr(.  Pars
1e60a 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1e60b 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1e60c 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
1e60d 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
1e60e 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
1e60f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78  n opcode */.  Ex
1e610 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20  pr *pLeft,      
1e611 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70        /* Left op
1e612 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20  erand */.  Expr 
1e613 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20 20 20  *pRight,        
1e614 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72     /* Right oper
1e615 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54  and */.  const T
1e616 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20  oken *pToken    
1e617 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b   /* Argument tok
1e618 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20  en */.){.  Expr 
1e619 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  *p = sqlite3Expr
1e61a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c  (pParse->db, op,
1e61b 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20   pLeft, pRight, 
1e61c 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70  pToken);.  if( p
1e61d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1e61e 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 70  xprCheckHeight(p
1e61f 50 61 72 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68  Parse, p->nHeigh
1e620 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
1e621 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65   p;.}../*.** Whe
1e622 6e 20 64 6f 69 6e 67 20 61 20 6e 65 73 74 65 64  n doing a nested
1e623 20 70 61 72 73 65 2c 20 79 6f 75 20 63 61 6e 20   parse, you can 
1e624 69 6e 63 6c 75 64 65 20 74 65 72 6d 73 20 69 6e  include terms in
1e625 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a   an expression.*
1e626 2a 20 74 68 61 74 20 6c 6f 6f 6b 20 6c 69 6b 65  * that look like
1e627 20 74 68 69 73 3a 20 20 20 23 31 20 23 32 20 2e   this:   #1 #2 .
1e628 2e 2e 20 20 54 68 65 73 65 20 74 65 72 6d 73 20  ..  These terms 
1e629 72 65 66 65 72 20 74 6f 20 72 65 67 69 73 74 65  refer to registe
1e62a 72 73 0a 2a 2a 20 69 6e 20 74 68 65 20 76 69 72  rs.** in the vir
1e62b 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 23  tual machine.  #
1e62c 4e 20 69 73 20 74 68 65 20 4e 2d 74 68 20 72 65  N is the N-th re
1e62d 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  gister..**.** Th
1e62e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1e62f 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
1e630 65 72 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20  er to deal with 
1e631 6f 6e 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d  on of those term
1e632 73 2e 0a 2a 2a 20 49 74 20 69 6d 6d 65 64 69 61  s..** It immedia
1e633 74 65 6c 79 20 67 65 6e 65 72 61 74 65 73 20 63  tely generates c
1e634 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ode to store the
1e635 20 76 61 6c 75 65 20 69 6e 20 61 20 6d 65 6d 6f   value in a memo
1e636 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20  ry location..** 
1e637 54 68 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65  The returns an e
1e638 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77  xpression that w
1e639 69 6c 6c 20 63 6f 64 65 20 74 6f 20 65 78 74 72  ill code to extr
1e63a 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 66 72  act the value fr
1e63b 6f 6d 0a 2a 2a 20 74 68 61 74 20 6d 65 6d 6f 72  om.** that memor
1e63c 79 20 6c 6f 63 61 74 69 6f 6e 20 61 73 20 6e 65  y location as ne
1e63d 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eded..*/.SQLITE_
1e63e 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
1e63f 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78 70  lite3RegisterExp
1e640 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  r(Parse *pParse,
1e641 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b   Token *pToken){
1e642 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
1e643 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 45 78  rse->pVdbe;.  Ex
1e644 70 72 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 61  pr *p;.  if( pPa
1e645 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
1e646 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1e647 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
1e648 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74  ear \"%T\": synt
1e649 61 78 20 65 72 72 6f 72 22 2c 20 70 54 6f 6b 65  ax error", pToke
1e64a 6e 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  n);.    return s
1e64b 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
1e64c 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20  se, TK_NULL, 0, 
1e64d 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  0, 0);.  }.  if(
1e64e 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   v==0 ) return 0
1e64f 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50  ;.  p = sqlite3P
1e650 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
1e651 52 45 47 49 53 54 45 52 2c 20 30 2c 20 30 2c 20  REGISTER, 0, 0, 
1e652 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70  pToken);.  if( p
1e653 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1e654 6e 20 30 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20  n 0;  /* Malloc 
1e655 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20  failed */.  }.  
1e656 70 2d 3e 69 54 61 62 6c 65 20 3d 20 61 74 6f 69  p->iTable = atoi
1e657 28 28 63 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d  ((char*)&pToken-
1e658 3e 7a 5b 31 5d 29 3b 0a 20 20 72 65 74 75 72 6e  >z[1]);.  return
1e659 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69   p;.}../*.** Joi
1e65a 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e  n two expression
1e65b 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f  s using an AND o
1e65c 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74  perator.  If eit
1e65d 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69  her expression i
1e65e 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  s.** NULL, then 
1e65f 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  just return the 
1e660 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  other expression
1e661 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e662 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
1e663 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65 33  3ExprAnd(sqlite3
1e664 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 66   *db, Expr *pLef
1e665 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 74 29  t, Expr *pRight)
1e666 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d 30  {.  if( pLeft==0
1e667 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
1e668 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69  Right;.  }else i
1e669 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a  f( pRight==0 ){.
1e66a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66 74      return pLeft
1e66b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
1e66c 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70  eturn sqlite3Exp
1e66d 72 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 70 4c  r(db, TK_AND, pL
1e66e 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b  eft, pRight, 0);
1e66f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
1e670 74 20 74 68 65 20 45 78 70 72 2e 73 70 61 6e 20  t the Expr.span 
1e671 66 69 65 6c 64 20 6f 66 20 74 68 65 20 67 69 76  field of the giv
1e672 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  en expression to
1e673 20 73 70 61 6e 20 61 6c 6c 0a 2a 2a 20 74 65 78   span all.** tex
1e674 74 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77  t between the tw
1e675 6f 20 67 69 76 65 6e 20 74 6f 6b 65 6e 73 2e 20  o given tokens. 
1e676 20 42 6f 74 68 20 74 6f 6b 65 6e 73 20 6d 75 73   Both tokens mus
1e677 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a  t be pointing.**
1e678 20 61 74 20 74 68 65 20 73 61 6d 65 20 73 74 72   at the same str
1e679 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
1e67a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1e67b 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 70 72  te3ExprSpan(Expr
1e67c 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a   *pExpr, Token *
1e67d 70 4c 65 66 74 2c 20 54 6f 6b 65 6e 20 2a 70 52  pLeft, Token *pR
1e67e 69 67 68 74 29 7b 0a 20 20 61 73 73 65 72 74 28  ight){.  assert(
1e67f 20 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20   pRight!=0 );.  
1e680 61 73 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30  assert( pLeft!=0
1e681 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20   );.  if( pExpr 
1e682 29 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e 73 70  ){.    pExpr->sp
1e683 61 6e 2e 7a 20 3d 20 70 4c 65 66 74 2d 3e 7a 3b  an.z = pLeft->z;
1e684 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
1e685 6f 77 69 6e 67 20 61 73 73 65 72 74 28 29 20 6d  owing assert() m
1e686 61 79 20 66 61 69 6c 20 77 68 65 6e 20 74 68 69  ay fail when thi
1e687 73 20 69 73 20 63 61 6c 6c 65 64 20 0a 20 20 20  s is called .   
1e688 20 2a 2a 20 76 69 61 20 73 71 6c 69 74 65 33 50   ** via sqlite3P
1e689 45 78 70 72 28 29 2f 73 71 6c 69 74 65 33 45 78  Expr()/sqlite3Ex
1e68a 70 72 28 29 20 66 72 6f 6d 20 61 64 64 57 68 65  pr() from addWhe
1e68b 72 65 54 65 72 6d 28 29 2e 20 2a 2f 0a 20 20 20  reTerm(). */.   
1e68c 20 2f 2a 20 61 73 73 65 72 74 28 70 52 69 67 68   /* assert(pRigh
1e68d 74 2d 3e 7a 20 3e 3d 20 70 4c 65 66 74 2d 3e 7a  t->z >= pLeft->z
1e68e 29 3b 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d  ); */.    pExpr-
1e68f 3e 73 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68 74  >span.n = pRight
1e690 2d 3e 6e 20 2b 20 28 75 6e 73 69 67 6e 65 64 29  ->n + (unsigned)
1e691 28 70 52 69 67 68 74 2d 3e 7a 20 2d 20 70 4c 65  (pRight->z - pLe
1e692 66 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ft->z);.  }.}../
1e693 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61  *.** Construct a
1e694 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20   new expression 
1e695 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e 63 74  node for a funct
1e696 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69 70 6c  ion with multipl
1e697 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a  e.** arguments..
1e698 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e699 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
1e69a 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 73  xprFunction(Pars
1e69b 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c  e *pParse, ExprL
1e69c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65  ist *pList, Toke
1e69d 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45 78  n *pToken){.  Ex
1e69e 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 71 6c 69  pr *pNew;.  sqli
1e69f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1e6a0 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20  ->db;.  assert( 
1e6a1 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e 65 77  pToken );.  pNew
1e6a2 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1e6a3 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
1e6a4 66 28 45 78 70 72 29 20 29 3b 0a 20 20 69 66 28  f(Expr) );.  if(
1e6a5 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
1e6a6 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
1e6a7 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
1e6a8 3b 20 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72  ; /* Avoid memor
1e6a9 79 20 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c  y leak when mall
1e6aa 6f 63 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20  oc fails */.    
1e6ab 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1e6ac 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 46 55  pNew->op = TK_FU
1e6ad 4e 43 54 49 4f 4e 3b 0a 20 20 70 4e 65 77 2d 3e  NCTION;.  pNew->
1e6ae 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b  x.pList = pList;
1e6af 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72  .  assert( !Expr
1e6b0 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77  HasProperty(pNew
1e6b1 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
1e6b2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  );.  assert( pTo
1e6b3 6b 65 6e 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20  ken->dyn==0 );. 
1e6b4 20 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 2a 70   pNew->span = *p
1e6b5 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65 33  Token;.  sqlite3
1e6b6 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70  TokenCopy(db, &p
1e6b7 4e 65 77 2d 3e 74 6f 6b 65 6e 2c 20 70 54 6f 6b  New->token, pTok
1e6b8 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  en);.  sqlite3Ex
1e6b9 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
1e6ba 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72 65 74  se, pNew);.  ret
1e6bb 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
1e6bc 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 72 69  ** Assign a vari
1e6bd 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20 61  able number to a
1e6be 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  n expression tha
1e6bf 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 6c 64  t encodes a wild
1e6c0 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f  card.** in the o
1e6c1 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74  riginal SQL stat
1e6c2 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57  ement.  .**.** W
1e6c3 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69 73 74  ildcards consist
1e6c4 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  ing of a single 
1e6c5 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e 65 64  "?" are assigned
1e6c6 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e   the next sequen
1e6c7 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65  tial.** variable
1e6c8 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57   number..**.** W
1e6c9 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20  ildcards of the 
1e6ca 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20  form "?nnn" are 
1e6cb 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 75 6d  assigned the num
1e6cc 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d  ber "nnn".  We m
1e6cd 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e  ake.** sure "nnn
1e6ce 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20  " is not too be 
1e6cf 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e 69 61  to avoid a denia
1e6d0 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61 74 74  l of service att
1e6d1 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  ack when.** the 
1e6d2 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  SQL statement co
1e6d3 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78 74 65  mes from an exte
1e6d4 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a  rnal source..**.
1e6d5 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20  ** Wildcards of 
1e6d6 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20  the form ":aaa" 
1e6d7 6f 72 20 22 24 61 61 61 22 20 61 72 65 20 61 73  or "$aaa" are as
1e6d8 73 69 67 6e 65 64 20 74 68 65 20 73 61 6d 65 20  signed the same 
1e6d9 6e 75 6d 62 65 72 0a 2a 2a 20 61 73 20 74 68 65  number.** as the
1e6da 20 70 72 65 76 69 6f 75 73 20 69 6e 73 74 61 6e   previous instan
1e6db 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 77  ce of the same w
1e6dc 69 6c 64 63 61 72 64 2e 20 20 4f 72 20 69 66 20  ildcard.  Or if 
1e6dd 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
1e6de 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  t.** instance of
1e6df 20 74 68 65 20 77 69 6c 64 63 61 72 64 2c 20 74   the wildcard, t
1e6e0 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e 69 61  he next sequenia
1e6e1 6c 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65  l variable numbe
1e6e2 72 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64  r is.** assigned
1e6e3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e6e4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1e6e5 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75 6d  ExprAssignVarNum
1e6e6 62 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  ber(Parse *pPars
1e6e7 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  e, Expr *pExpr){
1e6e8 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  .  Token *pToken
1e6e9 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1e6ea 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
1e6eb 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
1e6ec 72 65 74 75 72 6e 3b 0a 20 20 70 54 6f 6b 65 6e  return;.  pToken
1e6ed 20 3d 20 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e   = &pExpr->token
1e6ee 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b  ;.  assert( pTok
1e6ef 65 6e 2d 3e 6e 3e 3d 31 20 29 3b 0a 20 20 61 73  en->n>=1 );.  as
1e6f0 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 21  sert( pToken->z!
1e6f1 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e6f2 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d 21 3d 30 20  pToken->z[0]!=0 
1e6f3 29 3b 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d  );.  if( pToken-
1e6f4 3e 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  >n==1 ){.    /* 
1e6f5 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20  Wildcard of the 
1e6f6 66 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67  form "?".  Assig
1e6f7 6e 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61  n the next varia
1e6f8 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ble number */.  
1e6f9 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1e6fa 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72  = ++pParse->nVar
1e6fb 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54  ;.  }else if( pT
1e6fc 6f 6b 65 6e 2d 3e 7a 5b 30 5d 3d 3d 27 3f 27 20  oken->z[0]=='?' 
1e6fd 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61  ){.    /* Wildca
1e6fe 72 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  rd of the form "
1e6ff 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20  ?nnn".  Convert 
1e700 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65  "nnn" to an inte
1e701 67 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75  ger and.    ** u
1e702 73 65 20 69 74 20 61 73 20 74 68 65 20 76 61 72  se it as the var
1e703 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  iable number */.
1e704 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70      int i;.    p
1e705 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69  Expr->iTable = i
1e706 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26   = atoi((char*)&
1e707 70 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20  pToken->z[1]);. 
1e708 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
1e709 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  0 );.    testcas
1e70a 65 28 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 74  e( i==1 );.    t
1e70b 65 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e  estcase( i==db->
1e70c 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1e70d 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
1e70e 42 45 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 65  BER]-1 );.    te
1e70f 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61  stcase( i==db->a
1e710 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1e711 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  IT_VARIABLE_NUMB
1e712 45 52 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 69  ER] );.    if( i
1e713 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d  <1 || i>db->aLim
1e714 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1e715 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d  VARIABLE_NUMBER]
1e716 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1e717 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1e718 2c 20 22 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  , "variable numb
1e719 65 72 20 6d 75 73 74 20 62 65 20 62 65 74 77 65  er must be betwe
1e71a 65 6e 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c 0a  en ?1 and ?%d",.
1e71b 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c            db->aL
1e71c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1e71d 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
1e71e 52 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  R]);.    }.    i
1e71f 66 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 61  f( i>pParse->nVa
1e720 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  r ){.      pPars
1e721 65 2d 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 20  e->nVar = i;.   
1e722 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
1e723 2f 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20  /* Wildcards of 
1e724 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 20  the form ":aaa" 
1e725 6f 72 20 22 24 61 61 61 22 2e 20 20 52 65 75 73  or "$aaa".  Reus
1e726 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61  e the same varia
1e727 62 6c 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65  ble.    ** numbe
1e728 72 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 61  r as the prior a
1e729 70 70 65 61 72 61 6e 63 65 20 6f 66 20 74 68 65  ppearance of the
1e72a 20 73 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69   same name, or i
1e72b 66 20 74 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a  f the name.    *
1e72c 2a 20 68 61 73 20 6e 65 76 65 72 20 61 70 70 65  * has never appe
1e72d 61 72 65 64 20 62 65 66 6f 72 65 2c 20 72 65 75  ared before, reu
1e72e 73 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69  se the same vari
1e72f 61 62 6c 65 20 6e 75 6d 62 65 72 0a 20 20 20 20  able number.    
1e730 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  */.    int i;.  
1e731 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d    u32 n;.    n =
1e732 20 70 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20   pToken->n;.    
1e733 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73  for(i=0; i<pPars
1e734 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b  e->nVarExpr; i++
1e735 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
1e736 45 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 45  E;.      if( (pE
1e737 20 3d 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72   = pParse->apVar
1e738 45 78 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20  Expr[i])!=0.    
1e739 20 20 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b        && pE->tok
1e73a 65 6e 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20  en.n==n.        
1e73b 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e    && memcmp(pE->
1e73c 74 6f 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d  token.z, pToken-
1e73d 3e 7a 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  >z, n)==0 ){.   
1e73e 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1e73f 6c 65 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b  le = pE->iTable;
1e740 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1e741 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1e742 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d    if( i>=pParse-
1e743 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20  >nVarExpr ){.   
1e744 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1e745 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61   = ++pParse->nVa
1e746 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  r;.      if( pPa
1e747 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70  rse->nVarExpr>=p
1e748 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
1e749 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20  lloc-1 ){.      
1e74a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78    pParse->nVarEx
1e74b 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73  prAlloc += pPars
1e74c 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63  e->nVarExprAlloc
1e74d 20 2b 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70   + 10;.        p
1e74e 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
1e74f 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73   =.            s
1e750 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
1e751 72 46 72 65 65 28 0a 20 20 20 20 20 20 20 20 20  rFree(.         
1e752 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20       db,.       
1e753 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61         pParse->a
1e754 70 56 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20  pVarExpr,.      
1e755 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
1e756 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69  nVarExprAlloc*si
1e757 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56  zeof(pParse->apV
1e758 61 72 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20  arExpr[0]).     
1e759 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
1e75a 7d 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d  }.      if( !db-
1e75b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
1e75c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1e75d 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
1e75e 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  pr!=0 );.       
1e75f 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
1e760 70 72 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  pr[pParse->nVarE
1e761 78 70 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a  xpr++] = pExpr;.
1e762 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1e763 7d 20 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  } .  if( !pParse
1e764 2d 3e 6e 45 72 72 20 26 26 20 70 50 61 72 73 65  ->nErr && pParse
1e765 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69  ->nVar>db->aLimi
1e766 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t[SQLITE_LIMIT_V
1e767 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20  ARIABLE_NUMBER] 
1e768 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1e769 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1e76a 74 6f 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72  too many SQL var
1e76b 69 61 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a  iables");.  }.}.
1e76c 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20  ./*.** Clear an 
1e76d 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63  expression struc
1e76e 74 75 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c  ture without del
1e76f 65 74 69 6e 67 20 74 68 65 20 73 74 72 75 63 74  eting the struct
1e770 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53  ure itself..** S
1e771 75 62 73 74 72 75 63 74 75 72 65 20 69 73 20 64  ubstructure is d
1e772 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  eleted..*/.SQLIT
1e773 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1e774 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28  qlite3ExprClear(
1e775 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
1e776 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  r *p){.  if( p->
1e777 74 6f 6b 65 6e 2e 64 79 6e 20 29 20 73 71 6c 69  token.dyn ) sqli
1e778 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63  te3DbFree(db, (c
1e779 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29  har*)p->token.z)
1e77a 3b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73  ;.  if( !ExprHas
1e77b 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45  AnyProperty(p, E
1e77c 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53  P_TokenOnly|EP_S
1e77d 70 61 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20  panOnly) ){.    
1e77e 69 66 28 20 70 2d 3e 73 70 61 6e 2e 64 79 6e 20  if( p->span.dyn 
1e77f 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  ) sqlite3DbFree(
1e780 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73 70  db, (char*)p->sp
1e781 61 6e 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 45  an.z);.    if( E
1e782 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
1e783 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 7b  , EP_Reduced) ){
1e784 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c  .      if( p->pL
1e785 65 66 74 20 29 20 73 71 6c 69 74 65 33 45 78 70  eft ) sqlite3Exp
1e786 72 43 6c 65 61 72 28 64 62 2c 20 70 2d 3e 70 4c  rClear(db, p->pL
1e787 65 66 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  eft);.      if( 
1e788 70 2d 3e 70 52 69 67 68 74 20 29 20 73 71 6c 69  p->pRight ) sqli
1e789 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 62 2c  te3ExprClear(db,
1e78a 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20   p->pRight);.   
1e78b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
1e78c 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1e78d 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20  db, p->pLeft);. 
1e78e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1e78f 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52  Delete(db, p->pR
1e790 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ight);.    }.   
1e791 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
1e792 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65  erty(p, EP_xIsSe
1e793 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73  lect) ){.      s
1e794 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
1e795 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c  te(db, p->x.pSel
1e796 65 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ect);.    }else{
1e797 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1e798 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
1e799 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20   p->x.pList);.  
1e79a 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1e79b 20 52 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c   Recursively del
1e79c 65 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ete an expressio
1e79d 6e 20 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54  n tree..*/.SQLIT
1e79e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1e79f 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1e7a0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78  (sqlite3 *db, Ex
1e7a1 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  pr *p){.  if( p=
1e7a2 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  =0 ) return;.  s
1e7a3 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28  qlite3ExprClear(
1e7a4 64 62 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65  db, p);.  sqlite
1e7a5 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
1e7a6 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 45 78 70  }../*.** The Exp
1e7a7 72 2e 74 6f 6b 65 6e 20 66 69 65 6c 64 20 6d 69  r.token field mi
1e7a8 67 68 74 20 62 65 20 61 20 73 74 72 69 6e 67 20  ght be a string 
1e7a9 6c 69 74 65 72 61 6c 20 74 68 61 74 20 69 73 20  literal that is 
1e7aa 71 75 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 73 6f  quoted..** If so
1e7ab 2c 20 72 65 6d 6f 76 65 20 74 68 65 20 71 75 6f  , remove the quo
1e7ac 74 61 74 69 6f 6e 20 6d 61 72 6b 73 2e 0a 2a 2f  tation marks..*/
1e7ad 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1e7ae 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 71 75  void sqlite3Dequ
1e7af 6f 74 65 45 78 70 72 28 45 78 70 72 20 2a 70 29  oteExpr(Expr *p)
1e7b0 7b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73  {.  if( !ExprHas
1e7b1 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45  AnyProperty(p, E
1e7b2 50 5f 44 65 71 75 6f 74 65 64 29 20 29 7b 0a 20  P_Dequoted) ){. 
1e7b3 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
1e7b4 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f 74 65  ty(p, EP_Dequote
1e7b5 64 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  d);.    assert( 
1e7b6 28 70 2d 3e 76 76 61 46 6c 61 67 73 20 26 20 45  (p->vvaFlags & E
1e7b7 56 56 41 5f 52 65 61 64 4f 6e 6c 79 54 6f 6b 65  VVA_ReadOnlyToke
1e7b8 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  n)==0 );.    sql
1e7b9 69 74 65 33 44 65 71 75 6f 74 65 28 28 63 68 61  ite3Dequote((cha
1e7ba 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a  r*)p->token.z);.
1e7bb 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
1e7bc 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1e7bd 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65  f bytes allocate
1e7be 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73  d for the expres
1e7bf 73 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 0a  sion structure .
1e7c0 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ** passed as the
1e7c1 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
1e7c2 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20   This is always 
1e7c3 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c  one of EXPR_FULL
1e7c4 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 45  SIZE,.** EXPR_RE
1e7c5 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50  DUCEDSIZE or EXP
1e7c6 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e  R_TOKENONLYSIZE.
1e7c7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65  .*/.static int e
1e7c8 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 45 78  xprStructSize(Ex
1e7c9 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78  pr *p){.  if( Ex
1e7ca 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
1e7cb 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29   EP_TokenOnly) )
1e7cc 20 72 65 74 75 72 6e 20 45 58 50 52 5f 54 4f 4b   return EXPR_TOK
1e7cd 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 66  ENONLYSIZE;.  if
1e7ce 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
1e7cf 79 28 70 2c 20 45 50 5f 53 70 61 6e 4f 6e 6c 79  y(p, EP_SpanOnly
1e7d0 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f  ) ) return EXPR_
1e7d1 53 50 41 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20  SPANONLYSIZE;.  
1e7d2 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
1e7d3 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65  rty(p, EP_Reduce
1e7d4 64 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52  d) ) return EXPR
1e7d5 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20 20  _REDUCEDSIZE;.  
1e7d6 72 65 74 75 72 6e 20 45 58 50 52 5f 46 55 4c 4c  return EXPR_FULL
1e7d7 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73  SIZE;.}../*.** s
1e7d8 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 20  qlite3ExprDup() 
1e7d9 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  has been called 
1e7da 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79  to create a copy
1e7db 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70   of expression p
1e7dc 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 45 58 50   with.** the EXP
1e7dd 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 20 70  RDUP_XXX flags p
1e7de 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
1e7df 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  ond argument. Th
1e7e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20  is function .** 
1e7e1 72 65 74 75 72 6e 73 20 74 68 65 20 73 70 61 63  returns the spac
1e7e2 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74  e required for t
1e7e3 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45  he copy of the E
1e7e4 78 70 72 20 73 74 72 75 63 74 75 72 65 20 6f 6e  xpr structure on
1e7e5 6c 79 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ly..** This is a
1e7e6 6c 77 61 79 73 20 6f 6e 65 20 6f 66 20 45 58 50  lways one of EXP
1e7e7 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 45 58 50 52  R_FULLSIZE, EXPR
1e7e8 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20  _REDUCEDSIZE or 
1e7e9 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49  EXPR_TOKENONLYSI
1e7ea 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ZE..*/.static in
1e7eb 74 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63  t dupedExprStruc
1e7ec 74 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69  tSize(Expr *p, i
1e7ed 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74  nt flags){.  int
1e7ee 20 6e 53 69 7a 65 3b 0a 20 20 69 66 28 20 30 3d   nSize;.  if( 0=
1e7ef 3d 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f  =(flags&EXPRDUP_
1e7f0 52 45 44 55 43 45 29 20 29 7b 0a 20 20 20 20 6e  REDUCE) ){.    n
1e7f1 53 69 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c 4c  Size = EXPR_FULL
1e7f2 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66  SIZE;.  }else if
1e7f3 28 20 70 2d 3e 70 4c 65 66 74 20 7c 7c 20 70 2d  ( p->pLeft || p-
1e7f4 3e 70 52 69 67 68 74 20 7c 7c 20 70 2d 3e 70 43  >pRight || p->pC
1e7f5 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 2e 70 4c 69 73  oll || p->x.pLis
1e7f6 74 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d  t ){.    nSize =
1e7f7 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a   EXPR_REDUCEDSIZ
1e7f8 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  E;.  }else if( f
1e7f9 6c 61 67 73 26 28 45 58 50 52 44 55 50 5f 53 50  lags&(EXPRDUP_SP
1e7fa 41 4e 7c 45 58 50 52 44 55 50 5f 44 49 53 54 49  AN|EXPRDUP_DISTI
1e7fb 4e 43 54 53 50 41 4e 29 20 29 7b 0a 20 20 20 20  NCTSPAN) ){.    
1e7fc 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 53 50 41  nSize = EXPR_SPA
1e7fd 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 7d 65 6c  NONLYSIZE;.  }el
1e7fe 73 65 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20  se{.    nSize = 
1e7ff 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49  EXPR_TOKENONLYSI
1e800 5a 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ZE;.  }.  return
1e801 20 6e 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   nSize;.}../*.**
1e802 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
1e803 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65  ) has been calle
1e804 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f  d to create a co
1e805 70 79 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  py of expression
1e806 20 70 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 45   p with.** the E
1e807 58 50 52 44 55 50 5f 58 58 58 20 70 61 73 73 65  XPRDUP_XXX passe
1e808 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
1e809 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 66  argument. This f
1e80a 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a  unction returns.
1e80b 2a 2a 20 74 68 65 20 73 70 61 63 65 20 69 6e 20  ** the space in 
1e80c 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74  bytes required t
1e80d 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79  o store the copy
1e80e 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72   of the Expr str
1e80f 75 63 74 75 72 65 0a 2a 2a 20 61 6e 64 20 74 68  ucture.** and th
1e810 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20  e copies of the 
1e811 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 61 6e 64  Expr.token.z and
1e812 20 45 78 70 72 2e 73 70 61 6e 2e 7a 20 28 69 66   Expr.span.z (if
1e813 20 61 70 70 6c 69 63 61 62 6c 65 29 0a 2a 2a 20   applicable).** 
1e814 73 74 72 69 6e 67 20 62 75 66 66 65 72 73 2e 0a  string buffers..
1e815 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75  */.static int du
1e816 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28  pedExprNodeSize(
1e817 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61  Expr *p, int fla
1e818 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65  gs){.  int nByte
1e819 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 72 75   = dupedExprStru
1e81a 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29  ctSize(p, flags)
1e81b 20 2b 20 28 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 3f   + (p->token.z ?
1e81c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 20 2b 20 31 20   p->token.n + 1 
1e81d 3a 20 30 29 3b 0a 20 20 69 66 28 20 28 66 6c 61  : 0);.  if( (fla
1e81e 67 73 26 45 58 50 52 44 55 50 5f 44 49 53 54 49  gs&EXPRDUP_DISTI
1e81f 4e 43 54 53 50 41 4e 29 0a 20 20 20 7c 7c 20 28  NCTSPAN).   || (
1e820 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 53 50  flags&EXPRDUP_SP
1e821 41 4e 20 26 26 20 28 70 2d 3e 74 6f 6b 65 6e 2e  AN && (p->token.
1e822 7a 21 3d 70 2d 3e 73 70 61 6e 2e 7a 20 7c 7c 20  z!=p->span.z || 
1e823 70 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 2d 3e 73  p->token.n!=p->s
1e824 70 61 6e 2e 6e 29 29 20 0a 20 20 29 7b 0a 20 20  pan.n)) .  ){.  
1e825 20 20 6e 42 79 74 65 20 2b 3d 20 70 2d 3e 73 70    nByte += p->sp
1e826 61 6e 2e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75  an.n;.  }.  retu
1e827 72 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29  rn ROUND8(nByte)
1e828 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1e829 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1e82a 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74  bytes required t
1e82b 6f 20 63 72 65 61 74 65 20 61 20 64 75 70 6c 69  o create a dupli
1e82c 63 61 74 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20  cate of the .** 
1e82d 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73 73 65  expression passe
1e82e 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
1e82f 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 73 65 63  rgument. The sec
1e830 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
1e831 61 0a 2a 2a 20 6d 61 73 6b 20 63 6f 6e 74 61 69  a.** mask contai
1e832 6e 69 6e 67 20 45 58 50 52 44 55 50 5f 58 58 58  ning EXPRDUP_XXX
1e833 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   flags..**.** Th
1e834 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
1e835 20 69 6e 63 6c 75 64 65 73 20 73 70 61 63 65 20   includes space 
1e836 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79  to create a copy
1e837 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72   of the Expr str
1e838 75 63 74 0a 2a 2a 20 69 74 73 65 6c 66 20 61 6e  uct.** itself an
1e839 64 20 74 68 65 20 62 75 66 66 65 72 20 72 65 66  d the buffer ref
1e83a 65 72 72 65 64 20 74 6f 20 62 79 20 45 78 70 72  erred to by Expr
1e83b 2e 74 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 20  .token, if any. 
1e83c 49 66 20 74 68 65 20 0a 2a 2a 20 45 58 50 52 44  If the .** EXPRD
1e83d 55 50 5f 53 50 41 4e 20 66 6c 61 67 20 69 73 20  UP_SPAN flag is 
1e83e 73 65 74 2c 20 74 68 65 6e 20 73 70 61 63 65 20  set, then space 
1e83f 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 79  to create a copy
1e840 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a   of the buffer.*
1e841 2a 20 72 65 66 65 72 65 64 20 74 6f 20 62 79 20  * refered to by 
1e842 45 78 70 72 2e 73 70 61 6e 20 69 73 20 61 6c 73  Expr.span is als
1e843 6f 20 69 6e 63 6c 75 64 65 64 2e 0a 2a 2a 0a 2a  o included..**.*
1e844 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50  * If the EXPRDUP
1e845 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20  _REDUCE flag is 
1e846 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65  set, then the re
1e847 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75  turn value inclu
1e848 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f  des .** space to
1e849 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45   duplicate all E
1e84a 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65  xpr nodes in the
1e84b 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20   tree formed by 
1e84c 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61  Expr.pLeft .** a
1e84d 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 76  nd Expr.pRight v
1e84e 61 72 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f  ariables (but no
1e84f 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74  t for any struct
1e850 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20  ures pointed to 
1e851 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64  or .** descended
1e852 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78   from the Expr.x
1e853 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78  .pList or Expr.x
1e854 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c  .pSelect variabl
1e855 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  es)..*/.static i
1e856 6e 74 20 64 75 70 65 64 45 78 70 72 53 69 7a 65  nt dupedExprSize
1e857 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c  (Expr *p, int fl
1e858 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ags){.  int nByt
1e859 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29  e = 0;.  if( p )
1e85a 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75  {.    nByte = du
1e85b 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28  pedExprNodeSize(
1e85c 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69  p, flags);.    i
1e85d 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50  f( flags&EXPRDUP
1e85e 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20  _REDUCE ){.     
1e85f 20 69 6e 74 20 66 20 3d 20 66 6c 61 67 73 26 28   int f = flags&(
1e860 7e 28 45 58 50 52 44 55 50 5f 53 50 41 4e 7c 45  ~(EXPRDUP_SPAN|E
1e861 58 50 52 44 55 50 5f 44 49 53 54 49 4e 43 54 53  XPRDUP_DISTINCTS
1e862 50 41 4e 29 29 3b 0a 20 20 20 20 20 20 6e 42 79  PAN));.      nBy
1e863 74 65 20 2b 3d 20 64 75 70 65 64 45 78 70 72 53  te += dupedExprS
1e864 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20 66 29  ize(p->pLeft, f)
1e865 20 2b 20 64 75 70 65 64 45 78 70 72 53 69 7a 65   + dupedExprSize
1e866 28 70 2d 3e 70 52 69 67 68 74 2c 20 66 29 3b 0a  (p->pRight, f);.
1e867 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1e868 72 6e 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a  rn nByte;.}../*.
1e869 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1e86a 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73   is similar to s
1e86b 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 2c  qlite3ExprDup(),
1e86c 20 65 78 63 65 70 74 20 74 68 61 74 20 69 66 20   except that if 
1e86d 70 7a 42 75 66 66 65 72 20 0a 2a 2a 20 69 73 20  pzBuffer .** is 
1e86e 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70  not NULL then *p
1e86f 7a 42 75 66 66 65 72 20 69 73 20 61 73 73 75 6d  zBuffer is assum
1e870 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61  ed to point to a
1e871 20 62 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e   buffer large en
1e872 6f 75 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f 72  ough .** to stor
1e873 65 20 74 68 65 20 63 6f 70 79 20 6f 66 20 65 78  e the copy of ex
1e874 70 72 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 20  pression p, the 
1e875 63 6f 70 69 65 73 20 6f 66 20 70 2d 3e 74 6f 6b  copies of p->tok
1e876 65 6e 20 61 6e 64 20 70 2d 3e 73 70 61 6e 20 0a  en and p->span .
1e877 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c  ** (if applicabl
1e878 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f 70 69  e), and the copi
1e879 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c 65  es of the p->pLe
1e87a 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 68 74  ft and p->pRight
1e87b 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a   expressions,.**
1e87c 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 65 20   if any. Before 
1e87d 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42 75  returning, *pzBu
1e87e 66 66 65 72 20 69 73 20 73 65 74 20 74 6f 20 74  ffer is set to t
1e87f 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61  he first byte pa
1e880 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72 74  ssed the.** port
1e881 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 66 65  ion of the buffe
1e882 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 79  r copied into by
1e883 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
1e884 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20 2a  */.static Expr *
1e885 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 20  exprDup(sqlite3 
1e886 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e  *db, Expr *p, in
1e887 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70 7a  t flags, u8 **pz
1e888 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 72 20  Buffer){.  Expr 
1e889 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20  *pNew = 0;      
1e88a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e88b 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
1e88c 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b  rn */.  if( p ){
1e88d 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69  .    const int i
1e88e 73 52 65 71 75 69 72 65 44 69 73 74 69 6e 63 74  sRequireDistinct
1e88f 53 70 61 6e 20 3d 20 28 66 6c 61 67 73 26 45 58  Span = (flags&EX
1e890 50 52 44 55 50 5f 44 49 53 54 49 4e 43 54 53 50  PRDUP_DISTINCTSP
1e891 41 4e 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69  AN);.    const i
1e892 6e 74 20 69 73 52 65 71 75 69 72 65 53 70 61 6e  nt isRequireSpan
1e893 20 3d 20 28 66 6c 61 67 73 26 28 45 58 50 52 44   = (flags&(EXPRD
1e894 55 50 5f 53 50 41 4e 7c 45 58 50 52 44 55 50 5f  UP_SPAN|EXPRDUP_
1e895 44 49 53 54 49 4e 43 54 53 50 41 4e 29 29 3b 0a  DISTINCTSPAN));.
1e896 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73      const int is
1e897 52 65 64 75 63 65 64 20 3d 20 28 66 6c 61 67 73  Reduced = (flags
1e898 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29  &EXPRDUP_REDUCE)
1e899 3b 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f 63  ;.    u8 *zAlloc
1e89a 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ;..    assert( p
1e89b 7a 42 75 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73  zBuffer==0 || is
1e89c 52 65 64 75 63 65 64 20 29 3b 0a 0a 20 20 20 20  Reduced );..    
1e89d 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68  /* Figure out wh
1e89e 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 65  ere to write the
1e89f 20 6e 65 77 20 45 78 70 72 20 73 74 72 75 63 74   new Expr struct
1e8a0 75 72 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ure. */.    if( 
1e8a1 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20  pzBuffer ){.    
1e8a2 20 20 7a 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75    zAlloc = *pzBu
1e8a3 66 66 65 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ffer;.    }else{
1e8a4 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20  .      zAlloc = 
1e8a5 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
1e8a6 61 77 28 64 62 2c 20 64 75 70 65 64 45 78 70 72  aw(db, dupedExpr
1e8a7 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 29 3b  Size(p, flags));
1e8a8 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20  .    }.    pNew 
1e8a9 3d 20 28 45 78 70 72 20 2a 29 7a 41 6c 6c 6f 63  = (Expr *)zAlloc
1e8aa 3b 0a 0a 20 20 20 20 69 66 28 20 70 4e 65 77 20  ;..    if( pNew 
1e8ab 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20  ){.      /* Set 
1e8ac 6e 4e 65 77 53 69 7a 65 20 74 6f 20 74 68 65 20  nNewSize to the 
1e8ad 73 69 7a 65 20 61 6c 6c 6f 63 61 74 65 64 20 66  size allocated f
1e8ae 6f 72 20 74 68 65 20 73 74 72 75 63 74 75 72 65  or the structure
1e8af 20 70 6f 69 6e 74 65 64 20 74 6f 0a 20 20 20 20   pointed to.    
1e8b0 20 20 2a 2a 20 62 79 20 70 4e 65 77 2e 20 54 68    ** by pNew. Th
1e8b1 69 73 20 69 73 20 65 69 74 68 65 72 20 45 58 50  is is either EXP
1e8b2 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 45 58 50 52  R_FULLSIZE, EXPR
1e8b3 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 0a  _REDUCEDSIZE or.
1e8b4 20 20 20 20 20 20 2a 2a 20 45 58 50 52 5f 54 4f        ** EXPR_TO
1e8b5 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 20 6e 54 6f  KENONLYSIZE. nTo
1e8b6 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 74 68  ken is set to th
1e8b7 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1e8b8 73 20 63 6f 6e 73 75 6d 65 64 0a 20 20 20 20 20  s consumed.     
1e8b9 20 2a 2a 20 62 79 20 74 68 65 20 63 6f 70 79 20   ** by the copy 
1e8ba 6f 66 20 74 68 65 20 70 2d 3e 74 6f 6b 65 6e 2e  of the p->token.
1e8bb 7a 20 73 74 72 69 6e 67 20 28 69 66 20 61 6e 79  z string (if any
1e8bc 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
1e8bd 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77    const int nNew
1e8be 53 69 7a 65 20 3d 20 64 75 70 65 64 45 78 70 72  Size = dupedExpr
1e8bf 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c  StructSize(p, fl
1e8c0 61 67 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73  ags);.      cons
1e8c1 74 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 28  t int nToken = (
1e8c2 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 3f 20 70 2d 3e  p->token.z ? p->
1e8c3 74 6f 6b 65 6e 2e 6e 20 2b 20 31 20 3a 20 30 29  token.n + 1 : 0)
1e8c4 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 52 65  ;.      if( isRe
1e8c5 64 75 63 65 64 20 29 7b 0a 20 20 20 20 20 20 20  duced ){.       
1e8c6 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73   assert( ExprHas
1e8c7 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52  Property(p, EP_R
1e8c8 65 64 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20  educed)==0 );.  
1e8c9 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c        memcpy(zAl
1e8ca 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65  loc, p, nNewSize
1e8cb 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1e8cc 20 20 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a          int nSiz
1e8cd 65 20 3d 20 65 78 70 72 53 74 72 75 63 74 53 69  e = exprStructSi
1e8ce 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 6d  ze(p);.        m
1e8cf 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c  emcpy(zAlloc, p,
1e8d0 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20   nSize);.       
1e8d1 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b   memset(&zAlloc[
1e8d2 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f  nSize], 0, EXPR_
1e8d3 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b  FULLSIZE-nSize);
1e8d4 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1e8d5 2f 2a 20 53 65 74 20 74 68 65 20 45 50 5f 52 65  /* Set the EP_Re
1e8d6 64 75 63 65 64 20 61 6e 64 20 45 50 5f 54 6f 6b  duced and EP_Tok
1e8d7 65 6e 4f 6e 6c 79 20 66 6c 61 67 73 20 61 70 70  enOnly flags app
1e8d8 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20  ropriately. */. 
1e8d9 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73       pNew->flags
1e8da 20 26 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64   &= ~(EP_Reduced
1e8db 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50  |EP_TokenOnly|EP
1e8dc 5f 53 70 61 6e 4f 6e 6c 79 29 3b 0a 20 20 20 20  _SpanOnly);.    
1e8dd 20 20 73 77 69 74 63 68 28 20 6e 4e 65 77 53 69    switch( nNewSi
1e8de 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61  ze ){.        ca
1e8df 73 65 20 45 58 50 52 5f 52 45 44 55 43 45 44 53  se EXPR_REDUCEDS
1e8e0 49 5a 45 3a 20 20 20 70 4e 65 77 2d 3e 66 6c 61  IZE:   pNew->fla
1e8e1 67 73 20 7c 3d 20 45 50 5f 52 65 64 75 63 65 64  gs |= EP_Reduced
1e8e2 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
1e8e3 20 63 61 73 65 20 45 58 50 52 5f 54 4f 4b 45 4e   case EXPR_TOKEN
1e8e4 4f 4e 4c 59 53 49 5a 45 3a 20 70 4e 65 77 2d 3e  ONLYSIZE: pNew->
1e8e5 66 6c 61 67 73 20 7c 3d 20 45 50 5f 54 6f 6b 65  flags |= EP_Toke
1e8e6 6e 4f 6e 6c 79 3b 20 62 72 65 61 6b 3b 0a 20 20  nOnly; break;.  
1e8e7 20 20 20 20 20 20 63 61 73 65 20 45 58 50 52 5f        case EXPR_
1e8e8 53 50 41 4e 4f 4e 4c 59 53 49 5a 45 3a 20 20 70  SPANONLYSIZE:  p
1e8e9 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  New->flags |= EP
1e8ea 5f 53 70 61 6e 4f 6e 6c 79 3b 20 62 72 65 61 6b  _SpanOnly; break
1e8eb 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
1e8ec 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e   /* Copy the p->
1e8ed 74 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 66  token string, if
1e8ee 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69   any. */.      i
1e8ef 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20  f( nToken ){.   
1e8f0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68       unsigned ch
1e8f1 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 26 7a 41  ar *zToken = &zA
1e8f2 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a  lloc[nNewSize];.
1e8f3 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a          memcpy(z
1e8f4 54 6f 6b 65 6e 2c 20 70 2d 3e 74 6f 6b 65 6e 2e  Token, p->token.
1e8f5 7a 2c 20 6e 54 6f 6b 65 6e 2d 31 29 3b 0a 20 20  z, nToken-1);.  
1e8f6 20 20 20 20 20 20 7a 54 6f 6b 65 6e 5b 6e 54 6f        zToken[nTo
1e8f7 6b 65 6e 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20  ken-1] = '\0';. 
1e8f8 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b         pNew->tok
1e8f9 65 6e 2e 64 79 6e 20 3d 20 30 3b 0a 20 20 20 20  en.dyn = 0;.    
1e8fa 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e      pNew->token.
1e8fb 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  z = zToken;.    
1e8fc 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30    }..      if( 0
1e8fd 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65  ==((p->flags|pNe
1e8fe 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f 54  w->flags) & EP_T
1e8ff 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20  okenOnly) ){.   
1e900 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20       /* Fill in 
1e901 74 68 65 20 70 4e 65 77 2d 3e 73 70 61 6e 20 74  the pNew->span t
1e902 6f 6b 65 6e 2c 20 69 66 20 72 65 71 75 69 72 65  oken, if require
1e903 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  d. */.        if
1e904 28 20 69 73 52 65 71 75 69 72 65 53 70 61 6e 20  ( isRequireSpan 
1e905 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
1e906 20 69 73 52 65 71 75 69 72 65 44 69 73 74 69 6e   isRequireDistin
1e907 63 74 53 70 61 6e 20 0a 20 20 20 20 20 20 20 20  ctSpan .        
1e908 20 20 20 7c 7c 20 70 2d 3e 74 6f 6b 65 6e 2e 7a     || p->token.z
1e909 21 3d 70 2d 3e 73 70 61 6e 2e 7a 20 7c 7c 20 70  !=p->span.z || p
1e90a 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 2d 3e 73 70  ->token.n!=p->sp
1e90b 61 6e 2e 6e 0a 20 20 20 20 20 20 20 20 20 20 29  an.n.          )
1e90c 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4e  {.            pN
1e90d 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 26 7a 41  ew->span.z = &zA
1e90e 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 2b 6e 54  lloc[nNewSize+nT
1e90f 6f 6b 65 6e 5d 3b 0a 20 20 20 20 20 20 20 20 20  oken];.         
1e910 20 20 20 6d 65 6d 63 70 79 28 28 63 68 61 72 20     memcpy((char 
1e911 2a 29 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 2c 20  *)pNew->span.z, 
1e912 70 2d 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70  p->span.z, p->sp
1e913 61 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  an.n);.         
1e914 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 64 79     pNew->span.dy
1e915 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  n = 0;.         
1e916 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1e917 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a      pNew->span.z
1e918 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a   = pNew->token.z
1e919 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4e  ;.            pN
1e91a 65 77 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 4e 65  ew->span.n = pNe
1e91b 77 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20  w->token.n;.    
1e91c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e91d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1e91e 20 70 4e 65 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20   pNew->span.z = 
1e91f 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65  0;.          pNe
1e920 77 2d 3e 73 70 61 6e 2e 6e 20 3d 20 30 3b 0a 20  w->span.n = 0;. 
1e921 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1e922 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28  ..      if( 0==(
1e923 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d 3e  (p->flags|pNew->
1e924 66 6c 61 67 73 29 20 26 20 28 45 50 5f 54 6f 6b  flags) & (EP_Tok
1e925 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 4f 6e  enOnly|EP_SpanOn
1e926 6c 79 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ly)) ){.        
1e927 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70  /* Fill in the p
1e928 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 20 6f  New->x.pSelect o
1e929 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20  r pNew->x.pList 
1e92a 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20  member. */.     
1e92b 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
1e92c 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73  operty(p, EP_xIs
1e92d 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20  Select) ){.     
1e92e 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 53 65       pNew->x.pSe
1e92f 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
1e930 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 78  lectDup(db, p->x
1e931 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 65 64 75  .pSelect, isRedu
1e932 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  ced);.        }e
1e933 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70  lse{.          p
1e934 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 73  New->x.pList = s
1e935 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
1e936 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74  p(db, p->x.pList
1e937 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20  , isReduced);.  
1e938 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1e939 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69  .      /* Fill i
1e93a 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 61 6e  n pNew->pLeft an
1e93b 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 2e 20  d pNew->pRight. 
1e93c 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 45 78 70  */.      if( Exp
1e93d 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
1e93e 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 64  pNew, EP_Reduced
1e93f 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50  |EP_TokenOnly|EP
1e940 5f 53 70 61 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  _SpanOnly) ){.  
1e941 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 2b 3d 20        zAlloc += 
1e942 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a  dupedExprNodeSiz
1e943 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20  e(p, flags);.   
1e944 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
1e945 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
1e946 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0a 20 20  P_Reduced) ){.  
1e947 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c          pNew->pL
1e948 65 66 74 20 3d 20 65 78 70 72 44 75 70 28 64 62  eft = exprDup(db
1e949 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45 58 50 52  , p->pLeft, EXPR
1e94a 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c  DUP_REDUCE, &zAl
1e94b 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20  loc);.          
1e94c 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 65  pNew->pRight = e
1e94d 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52  xprDup(db, p->pR
1e94e 69 67 68 74 2c 20 45 58 50 52 44 55 50 5f 52 45  ight, EXPRDUP_RE
1e94f 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a  DUCE, &zAlloc);.
1e950 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e951 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20 29    if( pzBuffer )
1e952 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a 42  {.          *pzB
1e953 75 66 66 65 72 20 3d 20 7a 41 6c 6c 6f 63 3b 0a  uffer = zAlloc;.
1e954 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e955 7d 65 6c 73 65 20 69 66 28 20 21 45 78 70 72 48  }else if( !ExprH
1e956 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c  asAnyProperty(p,
1e957 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50   EP_TokenOnly|EP
1e958 5f 53 70 61 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  _SpanOnly) ){.  
1e959 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66        pNew->pLef
1e95a 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  t = sqlite3ExprD
1e95b 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c  up(db, p->pLeft,
1e95c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 65   0);.        pNe
1e95d 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c 69  w->pRight = sqli
1e95e 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
1e95f 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20  ->pRight, 0);.  
1e960 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1e961 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
1e962 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
1e963 6f 77 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 72  owing group of r
1e964 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 65  outines make dee
1e965 70 20 63 6f 70 69 65 73 20 6f 66 20 65 78 70 72  p copies of expr
1e966 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72  essions,.** expr
1e967 65 73 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44  ession lists, ID
1e968 20 6c 69 73 74 73 2c 20 61 6e 64 20 73 65 6c 65   lists, and sele
1e969 63 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  ct statements.  
1e96a 54 68 65 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a  The copies can.*
1e96b 2a 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 79  * be deleted (by
1e96c 20 62 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f   being passed to
1e96d 20 74 68 65 69 72 20 72 65 73 70 65 63 74 69 76   their respectiv
1e96e 65 20 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f  e ...Delete() ro
1e96f 75 74 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f  utines).** witho
1e970 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 65  ut effecting the
1e971 20 6f 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a   originals..**.*
1e972 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
1e973 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 73   list, ID, and s
1e974 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74 75  ource lists retu
1e975 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78 70  rn by sqlite3Exp
1e976 72 4c 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73  rListDup(),.** s
1e977 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28  qlite3IdListDup(
1e978 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 53 72  ), and sqlite3Sr
1e979 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e 20 6e  cListDup() can n
1e97a 6f 74 20 62 65 20 66 75 72 74 68 65 72 20 65 78  ot be further ex
1e97b 70 61 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 75  panded .** by su
1e97c 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
1e97d 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 70  o sqlite*ListApp
1e97e 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a  end() routines..
1e97f 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73  **.** Any tables
1e980 20 74 68 61 74 20 74 68 65 20 53 72 63 4c 69 73   that the SrcLis
1e981 74 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f  t might point to
1e982 20 61 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 61   are not duplica
1e983 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ted..**.** The f
1e984 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63  lags parameter c
1e985 6f 6e 74 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e  ontains a combin
1e986 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 45 58 50  ation of the EXP
1e987 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 20  RDUP_XXX flags. 
1e988 49 66 0a 2a 2a 20 74 68 65 20 45 58 50 52 44 55  If.** the EXPRDU
1e989 50 5f 53 50 41 4e 20 66 6c 61 67 20 69 73 20 73  P_SPAN flag is s
1e98a 65 74 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65  et in the argume
1e98b 6e 74 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68  nt parameter, th
1e98c 65 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e  en the .** Expr.
1e98d 73 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74 68  span field of th
1e98e 65 20 69 6e 70 75 74 20 65 78 70 72 65 73 73 69  e input expressi
1e98f 6f 6e 20 69 73 20 63 6f 70 69 65 64 2e 20 49 66  on is copied. If
1e990 20 45 58 50 52 44 55 50 5f 53 50 41 4e 20 69 73   EXPRDUP_SPAN is
1e991 0a 2a 2a 20 63 6c 65 61 72 2c 20 74 68 65 6e 20  .** clear, then 
1e992 74 68 65 20 45 78 70 72 2e 73 70 61 6e 20 66 69  the Expr.span fi
1e993 65 6c 64 20 6f 66 20 74 68 65 20 72 65 74 75 72  eld of the retur
1e994 6e 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 73  ned expression s
1e995 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 7a  tructure.** is z
1e996 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  eroed..**.** If 
1e997 74 68 65 20 45 58 50 52 44 55 50 5f 52 45 44 55  the EXPRDUP_REDU
1e998 43 45 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  CE flag is set, 
1e999 74 68 65 6e 20 74 68 65 20 73 74 72 75 63 74 75  then the structu
1e99a 72 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61  re returned is a
1e99b 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 76 65  .** truncated ve
1e99c 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 75 73 75  rsion of the usu
1e99d 61 6c 20 45 78 70 72 20 73 74 72 75 63 74 75 72  al Expr structur
1e99e 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73  e that will be s
1e99f 74 6f 72 65 64 20 61 73 0a 2a 2a 20 70 61 72 74  tored as.** part
1e9a0 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   of the in-memor
1e9a1 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  y representation
1e9a2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1e9a3 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49   schema..*/.SQLI
1e9a4 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
1e9a5 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28  *sqlite3ExprDup(
1e9a6 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
1e9a7 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29  r *p, int flags)
1e9a8 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 44  {.  return exprD
1e9a9 75 70 28 64 62 2c 20 70 2c 20 66 6c 61 67 73 2c  up(db, p, flags,
1e9aa 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   0);.}.SQLITE_PR
1e9ab 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1e9ac 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c 69  e3TokenCopy(sqli
1e9ad 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a  te3 *db, Token *
1e9ae 70 54 6f 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  pTo, const Token
1e9af 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66 28 20   *pFrom){.  if( 
1e9b0 70 54 6f 2d 3e 64 79 6e 20 29 20 73 71 6c 69 74  pTo->dyn ) sqlit
1e9b1 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68  e3DbFree(db, (ch
1e9b2 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b 0a 20 20 69  ar*)pTo->z);.  i
1e9b3 66 28 20 70 46 72 6f 6d 2d 3e 7a 20 29 7b 0a 20  f( pFrom->z ){. 
1e9b4 20 20 20 70 54 6f 2d 3e 6e 20 3d 20 70 46 72 6f     pTo->n = pFro
1e9b5 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54 6f 2d 3e 7a  m->n;.    pTo->z
1e9b6 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 44   = (u8*)sqlite3D
1e9b7 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68  bStrNDup(db, (ch
1e9b8 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a 2c 20 70 46  ar*)pFrom->z, pF
1e9b9 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 6f  rom->n);.    pTo
1e9ba 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c  ->dyn = 1;.  }el
1e9bb 73 65 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d  se{.    pTo->z =
1e9bc 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45   0;.  }.}.SQLITE
1e9bd 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
1e9be 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
1e9bf 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  stDup(sqlite3 *d
1e9c0 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 2c 20  b, ExprList *p, 
1e9c1 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 45 78  int flags){.  Ex
1e9c2 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20  prList *pNew;.  
1e9c3 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1e9c4 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 4f  item *pItem, *pO
1e9c5 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b  ldItem;.  int i;
1e9c6 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
1e9c7 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d  turn 0;.  pNew =
1e9c8 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1e9c9 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  Raw(db, sizeof(*
1e9ca 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70  pNew) );.  if( p
1e9cb 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
1e9cc 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 43 75 72  0;.  pNew->iECur
1e9cd 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  sor = 0;.  pNew-
1e9ce 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77 2d 3e 6e  >nExpr = pNew->n
1e9cf 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 72  Alloc = p->nExpr
1e9d0 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 70 49  ;.  pNew->a = pI
1e9d1 74 65 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  tem = sqlite3DbM
1e9d2 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 20 70 2d  allocRaw(db,  p-
1e9d3 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d  >nExpr*sizeof(p-
1e9d4 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  >a[0]) );.  if( 
1e9d5 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20  pItem==0 ){.    
1e9d6 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1e9d7 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74  , pNew);.    ret
1e9d8 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f  urn 0;.  } .  pO
1e9d9 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20  ldItem = p->a;. 
1e9da 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
1e9db 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
1e9dc 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b  ++, pOldItem++){
1e9dd 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45  .    Expr *pNewE
1e9de 78 70 72 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  xpr;.    Expr *p
1e9df 4f 6c 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74  OldExpr = pOldIt
1e9e0 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 70  em->pExpr;.    p
1e9e1 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 4e  Item->pExpr = pN
1e9e2 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  ewExpr = sqlite3
1e9e3 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  ExprDup(db, pOld
1e9e4 45 78 70 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20  Expr, flags);.  
1e9e5 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d    pItem->zName =
1e9e6 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
1e9e7 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a  (db, pOldItem->z
1e9e8 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 6d  Name);.    pItem
1e9e9 2d 3e 73 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f  ->sortOrder = pO
1e9ea 6c 64 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65  ldItem->sortOrde
1e9eb 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f  r;.    pItem->do
1e9ec 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 49 74 65  ne = 0;.    pIte
1e9ed 6d 2d 3e 69 43 6f 6c 20 3d 20 70 4f 6c 64 49 74  m->iCol = pOldIt
1e9ee 65 6d 2d 3e 69 43 6f 6c 3b 0a 20 20 20 20 70 49  em->iCol;.    pI
1e9ef 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 70 4f  tem->iAlias = pO
1e9f0 6c 64 49 74 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a  ldItem->iAlias;.
1e9f1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
1e9f2 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63  w;.}../*.** If c
1e9f3 75 72 73 6f 72 73 2c 20 74 72 69 67 67 65 72 73  ursors, triggers
1e9f4 2c 20 76 69 65 77 73 20 61 6e 64 20 73 75 62 71  , views and subq
1e9f5 75 65 72 69 65 73 20 61 72 65 20 61 6c 6c 20 6f  ueries are all o
1e9f6 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74  mitted from.** t
1e9f7 68 65 20 62 75 69 6c 64 2c 20 74 68 65 6e 20 6e  he build, then n
1e9f8 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
1e9f9 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 2c 20 65  wing routines, e
1e9fa 78 63 65 70 74 20 66 6f 72 20 0a 2a 2a 20 73 71  xcept for .** sq
1e9fb 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29  lite3SelectDup()
1e9fc 2c 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e  , can be called.
1e9fd 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
1e9fe 70 28 29 20 69 73 20 73 6f 6d 65 74 69 6d 65 73  p() is sometimes
1e9ff 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 20  .** called with 
1ea00 61 20 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e  a NULL argument.
1ea01 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
1ea02 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
1ea03 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  W) || !defined(S
1ea04 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
1ea05 45 52 29 20 5c 0a 20 7c 7c 20 21 64 65 66 69 6e  ER) \. || !defin
1ea06 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
1ea07 55 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f  UBQUERY).SQLITE_
1ea08 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20  PRIVATE SrcList 
1ea09 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44  *sqlite3SrcListD
1ea0a 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
1ea0b 53 72 63 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20  SrcList *p, int 
1ea0c 66 6c 61 67 73 29 7b 0a 20 20 53 72 63 4c 69 73  flags){.  SrcLis
1ea0d 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69  t *pNew;.  int i
1ea0e 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  ;.  int nByte;. 
1ea0f 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
1ea10 72 6e 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20  rn 0;.  nByte = 
1ea11 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 28 70 2d  sizeof(*p) + (p-
1ea12 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a 65 6f 66  >nSrc>0 ? sizeof
1ea13 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28 70 2d 3e  (p->a[0]) * (p->
1ea14 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b 0a 20 20  nSrc-1) : 0);.  
1ea15 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
1ea16 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42  MallocRaw(db, nB
1ea17 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70 4e 65  yte );.  if( pNe
1ea18 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  w==0 ) return 0;
1ea19 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 63 20 3d 20  .  pNew->nSrc = 
1ea1a 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70  pNew->nAlloc = p
1ea1b 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d  ->nSrc;.  for(i=
1ea1c 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b  0; i<p->nSrc; i+
1ea1d 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53  +){.    struct S
1ea1e 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65  rcList_item *pNe
1ea1f 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61  wItem = &pNew->a
1ea20 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20  [i];.    struct 
1ea21 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f  SrcList_item *pO
1ea22 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69  ldItem = &p->a[i
1ea23 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ];.    Table *pT
1ea24 61 62 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  ab;.    pNewItem
1ea25 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71  ->zDatabase = sq
1ea26 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
1ea27 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61 74  , pOldItem->zDat
1ea28 61 62 61 73 65 29 3b 0a 20 20 20 20 70 4e 65 77  abase);.    pNew
1ea29 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71  Item->zName = sq
1ea2a 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
1ea2b 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d  , pOldItem->zNam
1ea2c 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  e);.    pNewItem
1ea2d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74  ->zAlias = sqlit
1ea2e 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
1ea2f 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29  OldItem->zAlias)
1ea30 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
1ea31 6a 6f 69 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49  jointype = pOldI
1ea32 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20  tem->jointype;. 
1ea33 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75     pNewItem->iCu
1ea34 72 73 6f 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  rsor = pOldItem-
1ea35 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 4e  >iCursor;.    pN
1ea36 65 77 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61  ewItem->isPopula
1ea37 74 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  ted = pOldItem->
1ea38 69 73 50 6f 70 75 6c 61 74 65 64 3b 0a 20 20 20  isPopulated;.   
1ea39 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65   pNewItem->zInde
1ea3a 78 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  x = sqlite3DbStr
1ea3b 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
1ea3c 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 70  ->zIndex);.    p
1ea3d 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65  NewItem->notInde
1ea3e 78 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  xed = pOldItem->
1ea3f 6e 6f 74 49 6e 64 65 78 65 64 3b 0a 20 20 20 20  notIndexed;.    
1ea40 70 4e 65 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78  pNewItem->pIndex
1ea41 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e   = pOldItem->pIn
1ea42 64 65 78 3b 0a 20 20 20 20 70 54 61 62 20 3d 20  dex;.    pTab = 
1ea43 70 4e 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d  pNewItem->pTab =
1ea44 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b   pOldItem->pTab;
1ea45 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b  .    if( pTab ){
1ea46 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65  .      pTab->nRe
1ea47 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  f++;.    }.    p
1ea48 4e 65 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  NewItem->pSelect
1ea49 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
1ea4a 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
1ea4b 2d 3e 70 53 65 6c 65 63 74 2c 20 66 6c 61 67 73  ->pSelect, flags
1ea4c 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  );.    pNewItem-
1ea4d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74 65 33 45 78  >pOn = sqlite3Ex
1ea4e 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74  prDup(db, pOldIt
1ea4f 65 6d 2d 3e 70 4f 6e 2c 20 66 6c 61 67 73 29 3b  em->pOn, flags);
1ea50 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70  .    pNewItem->p
1ea51 55 73 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49  Using = sqlite3I
1ea52 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c  dListDup(db, pOl
1ea53 64 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a  dItem->pUsing);.
1ea54 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f      pNewItem->co
1ea55 6c 55 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d  lUsed = pOldItem
1ea56 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20  ->colUsed;.  }. 
1ea57 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
1ea58 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
1ea59 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64  dList *sqlite3Id
1ea5a 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20  ListDup(sqlite3 
1ea5b 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b  *db, IdList *p){
1ea5c 0a 20 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b  .  IdList *pNew;
1ea5d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1ea5e 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
1ea5f 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
1ea60 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
1ea61 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29   sizeof(*pNew) )
1ea62 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
1ea63 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
1ea64 65 77 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e  ew->nId = pNew->
1ea65 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b  nAlloc = p->nId;
1ea66 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c  .  pNew->a = sql
1ea67 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
1ea68 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f  db, p->nId*sizeo
1ea69 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20  f(p->a[0]) );.  
1ea6a 69 66 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29  if( pNew->a==0 )
1ea6b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
1ea6c 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20  ree(db, pNew);. 
1ea6d 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1ea6e 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
1ea6f 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nId; i++){.    
1ea70 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74  struct IdList_it
1ea71 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26  em *pNewItem = &
1ea72 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  pNew->a[i];.    
1ea73 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74  struct IdList_it
1ea74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26  em *pOldItem = &
1ea75 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65  p->a[i];.    pNe
1ea76 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73  wItem->zName = s
1ea77 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
1ea78 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61  b, pOldItem->zNa
1ea79 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  me);.    pNewIte
1ea7a 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65  m->idx = pOldIte
1ea7b 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65  m->idx;.  }.  re
1ea7c 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c  turn pNew;.}.SQL
1ea7d 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65  ITE_PRIVATE Sele
1ea7e 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63  ct *sqlite3Selec
1ea7f 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  tDup(sqlite3 *db
1ea80 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74  , Select *p, int
1ea81 20 66 6c 61 67 73 29 7b 0a 20 20 53 65 6c 65 63   flags){.  Selec
1ea82 74 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70  t *pNew;.  if( p
1ea83 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1ea84 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
1ea85 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
1ea86 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20  sizeof(*p) );.  
1ea87 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65  if( pNew==0 ) re
1ea88 74 75 72 6e 20 30 3b 0a 20 20 2f 2a 20 41 6c 77  turn 0;.  /* Alw
1ea89 61 79 73 20 6d 61 6b 65 20 61 20 63 6f 70 79 20  ays make a copy 
1ea8a 6f 66 20 74 68 65 20 73 70 61 6e 20 66 6f 72 20  of the span for 
1ea8b 74 6f 70 2d 6c 65 76 65 6c 20 65 78 70 72 65 73  top-level expres
1ea8c 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20 2a  sions in the.  *
1ea8d 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  * expression lis
1ea8e 74 2e 20 20 54 68 65 20 6c 6f 67 69 63 20 69 6e  t.  The logic in
1ea8f 20 53 45 4c 45 43 54 20 70 72 6f 63 65 73 73 69   SELECT processi
1ea90 6e 67 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  ng that determin
1ea91 65 73 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65  es.  ** the name
1ea92 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  s of columns in 
1ea93 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6e  the result set n
1ea94 65 65 64 73 20 74 68 69 73 20 69 6e 66 6f 72 6d  eeds this inform
1ea95 61 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e 65 77 2d  ation */.  pNew-
1ea96 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65  >pEList = sqlite
1ea97 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
1ea98 20 70 2d 3e 70 45 4c 69 73 74 2c 20 66 6c 61 67   p->pEList, flag
1ea99 73 7c 45 58 50 52 44 55 50 5f 53 50 41 4e 29 3b  s|EXPRDUP_SPAN);
1ea9a 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20  .  pNew->pSrc = 
1ea9b 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
1ea9c 70 28 64 62 2c 20 70 2d 3e 70 53 72 63 2c 20 66  p(db, p->pSrc, f
1ea9d 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  lags);.  pNew->p
1ea9e 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
1ea9f 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 57  xprDup(db, p->pW
1eaa0 68 65 72 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20  here, flags);.  
1eaa1 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d  pNew->pGroupBy =
1eaa2 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
1eaa3 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75  Dup(db, p->pGrou
1eaa4 70 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  pBy, flags);.  p
1eaa5 4e 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73  New->pHaving = s
1eaa6 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
1eaa7 2c 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c  , p->pHaving, fl
1eaa8 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f  ags);.  pNew->pO
1eaa9 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33  rderBy = sqlite3
1eaaa 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
1eaab 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 66 6c 61  p->pOrderBy, fla
1eaac 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20  gs);.  pNew->op 
1eaad 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d  = p->op;.  pNew-
1eaae 3e 70 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 65  >pPrior = sqlite
1eaaf 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
1eab0 2d 3e 70 50 72 69 6f 72 2c 20 66 6c 61 67 73 29  ->pPrior, flags)
1eab1 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74  ;.  pNew->pLimit
1eab2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
1eab3 70 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c  p(db, p->pLimit,
1eab4 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d   flags);.  pNew-
1eab5 3e 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74  >pOffset = sqlit
1eab6 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d  e3ExprDup(db, p-
1eab7 3e 70 4f 66 66 73 65 74 2c 20 66 6c 61 67 73 29  >pOffset, flags)
1eab8 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74  ;.  pNew->iLimit
1eab9 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f   = 0;.  pNew->iO
1eaba 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 4e 65  ffset = 0;.  pNe
1eabb 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 70 2d  w->selFlags = p-
1eabc 3e 73 65 6c 46 6c 61 67 73 20 26 20 7e 53 46 5f  >selFlags & ~SF_
1eabd 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20  UsesEphemeral;. 
1eabe 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73   pNew->pRightmos
1eabf 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 61  t = 0;.  pNew->a
1eac0 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d  ddrOpenEphm[0] =
1eac1 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64   -1;.  pNew->add
1eac2 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d  rOpenEphm[1] = -
1eac3 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  1;.  pNew->addrO
1eac4 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b  penEphm[2] = -1;
1eac5 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
1eac6 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50  }.#else.SQLITE_P
1eac7 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73  RIVATE Select *s
1eac8 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
1eac9 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c  sqlite3 *db, Sel
1eaca 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  ect *p, int flag
1eacb 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d  s){.  assert( p=
1eacc 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  =0 );.  return 0
1eacd 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  ;.}.#endif.../*.
1eace 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65  ** Add a new ele
1eacf 6d 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64 20  ment to the end 
1ead0 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
1ead1 20 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73 74   list.  If pList
1ead2 20 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79   is.** initially
1ead3 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61   NULL, then crea
1ead4 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 73  te a new express
1ead5 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c  ion list..*/.SQL
1ead6 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
1ead7 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70  List *sqlite3Exp
1ead8 72 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20 50  rListAppend(.  P
1ead9 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1eada 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
1eadb 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
1eadc 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
1eadd 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74         /* List t
1eade 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 65 6e  o which to appen
1eadf 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c  d. Might be NULL
1eae0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
1eae1 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r,            /*
1eae2 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62   Expression to b
1eae3 65 20 61 70 70 65 6e 64 65 64 20 2a 2f 0a 20 20  e appended */.  
1eae4 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20  Token *pName    
1eae5 20 20 20 20 20 20 20 20 2f 2a 20 41 53 20 6b 65          /* AS ke
1eae6 79 77 6f 72 64 20 66 6f 72 20 74 68 65 20 65 78  yword for the ex
1eae7 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  pression */.){. 
1eae8 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1eae9 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
1eaea 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20   pList==0 ){.   
1eaeb 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
1eaec 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
1eaed 20 73 69 7a 65 6f 66 28 45 78 70 72 4c 69 73 74   sizeof(ExprList
1eaee 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69  ) );.    if( pLi
1eaef 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67  st==0 ){.      g
1eaf0 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20  oto no_mem;.    
1eaf1 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  }.    assert( pL
1eaf2 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29  ist->nAlloc==0 )
1eaf3 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73  ;.  }.  if( pLis
1eaf4 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74  t->nAlloc<=pList
1eaf5 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73  ->nExpr ){.    s
1eaf6 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
1eaf7 74 65 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20  tem *a;.    int 
1eaf8 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f  n = pList->nAllo
1eaf9 63 2a 32 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d  c*2 + 4;.    a =
1eafa 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
1eafb 63 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20  c(db, pList->a, 
1eafc 6e 2a 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e  n*sizeof(pList->
1eafd 61 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20  a[0]));.    if( 
1eafe 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  a==0 ){.      go
1eaff 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d  to no_mem;.    }
1eb00 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20  .    pList->a = 
1eb01 61 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41  a;.    pList->nA
1eb02 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
1eb03 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61  MallocSize(db, a
1eb04 29 2f 73 69 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a  )/sizeof(a[0]);.
1eb05 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4c    }.  assert( pL
1eb06 69 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 69  ist->a!=0 );.  i
1eb07 66 28 20 70 45 78 70 72 20 7c 7c 20 70 4e 61 6d  f( pExpr || pNam
1eb08 65 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  e ){.    struct 
1eb09 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
1eb0a 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61  Item = &pList->a
1eb0b 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d  [pList->nExpr++]
1eb0c 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 74  ;.    memset(pIt
1eb0d 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70  em, 0, sizeof(*p
1eb0e 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 65  Item));.    pIte
1eb0f 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  m->zName = sqlit
1eb10 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1eb11 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20  db, pName);.    
1eb12 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70  pItem->pExpr = p
1eb13 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d  Expr;.    pItem-
1eb14 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 7d  >iAlias = 0;.  }
1eb15 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b  .  return pList;
1eb16 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a 20  ..no_mem:     . 
1eb17 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b 69 6e   /* Avoid leakin
1eb18 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61 6c 6c  g memory if mall
1eb19 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2e 20 2a  oc has failed. *
1eb1a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  /.  sqlite3ExprD
1eb1b 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 29  elete(db, pExpr)
1eb1c 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
1eb1d 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  istDelete(db, pL
1eb1e 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  ist);.  return 0
1eb1f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
1eb20 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
1eb21 74 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69 6e  t pEList contain
1eb22 73 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69 6d  s more than iLim
1eb23 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20  it elements,.** 
1eb24 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
1eb25 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
1eb26 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1eb27 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1eb28 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e  ExprListCheckLen
1eb29 67 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50  gth(.  Parse *pP
1eb2a 61 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73 74  arse,.  ExprList
1eb2b 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e 73   *pEList,.  cons
1eb2c 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74 0a  t char *zObject.
1eb2d 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70 50  ){.  int mx = pP
1eb2e 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74  arse->db->aLimit
1eb2f 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
1eb30 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61 73  LUMN];.  testcas
1eb31 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c  e( pEList && pEL
1eb32 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20 29  ist->nExpr==mx )
1eb33 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45  ;.  testcase( pE
1eb34 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e  List && pEList->
1eb35 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20  nExpr==mx+1 );. 
1eb36 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70   if( pEList && p
1eb37 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78 20  EList->nExpr>mx 
1eb38 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1eb39 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1eb3a 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73  too many columns
1eb3b 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63 74   in %s", zObject
1eb3c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1eb3d 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65  Delete an entire
1eb3e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1eb3f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1eb40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1eb41 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73  ExprListDelete(s
1eb42 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
1eb43 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
1eb44 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
1eb45 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
1eb46 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73  Item;.  if( pLis
1eb47 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
1eb48 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e   assert( pList->
1eb49 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d 3e  a!=0 || (pList->
1eb4a 6e 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 73  nExpr==0 && pLis
1eb4b 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b  t->nAlloc==0) );
1eb4c 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
1eb4d 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d 3e  ->nExpr<=pList->
1eb4e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 28  nAlloc );.  for(
1eb4f 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20  pItem=pList->a, 
1eb50 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
1eb51 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
1eb52 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  +){.    sqlite3E
1eb53 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49  xprDelete(db, pI
1eb54 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  tem->pExpr);.   
1eb55 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1eb56 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  b, pItem->zName)
1eb57 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
1eb58 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d  bFree(db, pList-
1eb59 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  >a);.  sqlite3Db
1eb5a 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b  Free(db, pList);
1eb5b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20  .}../*.** These 
1eb5c 72 6f 75 74 69 6e 65 73 20 61 72 65 20 57 61 6c  routines are Wal
1eb5d 6b 65 72 20 63 61 6c 6c 62 61 63 6b 73 2e 20 20  ker callbacks.  
1eb5e 57 61 6c 6b 65 72 2e 75 2e 70 69 20 69 73 20 61  Walker.u.pi is a
1eb5f 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61   pointer.** to a
1eb60 6e 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65 73  n integer.  Thes
1eb61 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63  e routines are c
1eb62 68 65 63 6b 69 6e 67 20 61 6e 20 65 78 70 72 65  hecking an expre
1eb63 73 73 69 6f 6e 20 74 6f 20 73 65 65 0a 2a 2a 20  ssion to see.** 
1eb64 69 66 20 69 74 20 69 73 20 61 20 63 6f 6e 73 74  if it is a const
1eb65 61 6e 74 2e 20 20 53 65 74 20 2a 57 61 6c 6b 65  ant.  Set *Walke
1eb66 72 2e 75 2e 70 69 20 74 6f 20 30 20 69 66 20 74  r.u.pi to 0 if t
1eb67 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1eb68 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74  .** not constant
1eb69 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61  ..**.** These ca
1eb6a 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 73 20  llback routines 
1eb6b 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  are used to impl
1eb6c 65 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77  ement the follow
1eb6d 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73  ing:.**.**     s
1eb6e 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
1eb6f 74 61 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71  tant().**     sq
1eb70 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
1eb71 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20  antNotJoin().** 
1eb72 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
1eb73 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74  sConstantOrFunct
1eb74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74  ion().**.*/.stat
1eb75 69 63 20 69 6e 74 20 65 78 70 72 4e 6f 64 65 49  ic int exprNodeI
1eb76 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72  sConstant(Walker
1eb77 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20   *pWalker, Expr 
1eb78 2a 70 45 78 70 72 29 7b 0a 0a 20 20 2f 2a 20 49  *pExpr){..  /* I
1eb79 66 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 69  f pWalker->u.i i
1eb7a 73 20 33 20 74 68 65 6e 20 61 6e 79 20 74 65 72  s 3 then any ter
1eb7b 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  m of the express
1eb7c 69 6f 6e 20 74 68 61 74 20 63 6f 6d 65 73 20 66  ion that comes f
1eb7d 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e 20  rom.  ** the ON 
1eb7e 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73  or USING clauses
1eb7f 20 6f 66 20 61 20 6a 6f 69 6e 20 64 69 73 71 75   of a join disqu
1eb80 61 6c 69 66 69 65 73 20 74 68 65 20 65 78 70 72  alifies the expr
1eb81 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d  ession.  ** from
1eb82 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65   being considere
1eb83 64 20 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20  d constant. */. 
1eb84 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e   if( pWalker->u.
1eb85 69 3d 3d 33 20 26 26 20 45 78 70 72 48 61 73 41  i==3 && ExprHasA
1eb86 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  nyProperty(pExpr
1eb87 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29  , EP_FromJoin) )
1eb88 7b 0a 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75  {.    pWalker->u
1eb89 2e 69 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  .i = 0;.    retu
1eb8a 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1eb8b 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  }..  switch( pEx
1eb8c 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f 2a  pr->op ){.    /*
1eb8d 20 43 6f 6e 73 69 64 65 72 20 66 75 6e 63 74 69   Consider functi
1eb8e 6f 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73 74 61  ons to be consta
1eb8f 6e 74 20 69 66 20 61 6c 6c 20 74 68 65 69 72 20  nt if all their 
1eb90 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 63 6f  arguments are co
1eb91 6e 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61 6e  nstant.    ** an
1eb92 64 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d  d pWalker->u.i==
1eb93 32 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b  2 */.    case TK
1eb94 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 20  _FUNCTION:.     
1eb95 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e   if( pWalker->u.
1eb96 69 3d 3d 32 20 29 20 72 65 74 75 72 6e 20 30 3b  i==2 ) return 0;
1eb97 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74  .      /* Fall t
1eb98 68 72 6f 75 67 68 20 2a 2f 0a 20 20 20 20 63 61  hrough */.    ca
1eb99 73 65 20 54 4b 5f 49 44 3a 0a 20 20 20 20 63 61  se TK_ID:.    ca
1eb9a 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20  se TK_COLUMN:.  
1eb9b 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55    case TK_AGG_FU
1eb9c 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65  NCTION:.    case
1eb9d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a   TK_AGG_COLUMN:.
1eb9e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1eb9f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20  MIT_SUBQUERY.   
1eba0 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a   case TK_SELECT:
1eba1 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49  .    case TK_EXI
1eba2 53 54 53 3a 0a 20 20 20 20 20 20 74 65 73 74 63  STS:.      testc
1eba3 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
1eba4 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20  TK_SELECT );.   
1eba5 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
1eba6 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54  pr->op==TK_EXIST
1eba7 53 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  S );.#endif.    
1eba8 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
1eba9 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a  r->op==TK_ID );.
1ebaa 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ebab 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pExpr->op==TK_CO
1ebac 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65  LUMN );.      te
1ebad 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
1ebae 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  p==TK_AGG_FUNCTI
1ebaf 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ON );.      test
1ebb0 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
1ebb1 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29  =TK_AGG_COLUMN )
1ebb2 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 72 2d  ;.      pWalker-
1ebb3 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20  >u.i = 0;.      
1ebb4 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1ebb5 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20  ;.    default:. 
1ebb6 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
1ebb7 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a  Continue;.  }.}.
1ebb8 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63  static int selec
1ebb9 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28  tNodeIsConstant(
1ebba 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
1ebbb 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65 64   Select *NotUsed
1ebbc 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
1ebbd 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
1ebbe 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d    pWalker->u.i =
1ebbf 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43   0;.  return WRC
1ebc0 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 63  _Abort;.}.static
1ebc1 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 74   int exprIsConst
1ebc2 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 6e  (Expr *p, int in
1ebc3 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b 65  itFlag){.  Walke
1ebc4 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 69  r w;.  w.u.i = i
1ebc5 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 78  nitFlag;.  w.xEx
1ebc6 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70  prCallback = exp
1ebc7 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b  rNodeIsConstant;
1ebc8 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
1ebc9 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f 64  back = selectNod
1ebca 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 73  eIsConstant;.  s
1ebcb 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26  qlite3WalkExpr(&
1ebcc 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  w, p);.  return 
1ebcd 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  w.u.i;.}../*.** 
1ebce 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69  Walk an expressi
1ebcf 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e  on tree.  Return
1ebd0 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73   1 if the expres
1ebd1 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74  sion is constant
1ebd2 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74 20  .** and 0 if it 
1ebd3 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c  involves variabl
1ebd4 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63  es or function c
1ebd5 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  alls..**.** For 
1ebd6 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
1ebd7 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61  this function, a
1ebd8 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73   double-quoted s
1ebd9 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22  tring (ex: "abc"
1ebda 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72  ).** is consider
1ebdb 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75  ed a variable bu
1ebdc 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65  t a single-quote
1ebdd 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61  d string (ex: 'a
1ebde 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e  bc') is.** a con
1ebdf 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stant..*/.SQLITE
1ebe0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1ebe1 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
1ebe2 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72  nt(Expr *p){.  r
1ebe3 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73  eturn exprIsCons
1ebe4 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  t(p, 1);.}../*.*
1ebe5 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73  * Walk an expres
1ebe6 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75  sion tree.  Retu
1ebe7 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72  rn 1 if the expr
1ebe8 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61  ession is consta
1ebe9 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20  nt.** that does 
1ebea 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72 6f  no originate fro
1ebeb 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e  m the ON or USIN
1ebec 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a  G clauses of a j
1ebed 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30  oin..** Return 0
1ebee 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20   if it involves 
1ebef 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e  variables or fun
1ebf0 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 74  ction calls or t
1ebf1 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20  erms from.** an 
1ebf2 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
1ebf3 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
1ebf4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1ebf5 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
1ebf6 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 7b  otJoin(Expr *p){
1ebf7 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73  .  return exprIs
1ebf8 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a  Const(p, 3);.}..
1ebf9 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  /*.** Walk an ex
1ebfa 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
1ebfb 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
1ebfc 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f  expression is co
1ebfd 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66  nstant.** or a f
1ebfe 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74  unction call wit
1ebff 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d  h constant argum
1ec00 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e  ents.  Return an
1ec01 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20  d 0 if there.** 
1ec02 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65  are any variable
1ec03 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  s..**.** For the
1ec04 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69   purposes of thi
1ec05 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f  s function, a do
1ec06 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69  uble-quoted stri
1ec07 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a  ng (ex: "abc").*
1ec08 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  * is considered 
1ec09 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61  a variable but a
1ec0a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73   single-quoted s
1ec0b 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27  tring (ex: 'abc'
1ec0c 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61  ) is.** a consta
1ec0d 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
1ec0e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1ec0f 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f  3ExprIsConstantO
1ec10 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a  rFunction(Expr *
1ec11 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70  p){.  return exp
1ec12 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b 0a  rIsConst(p, 2);.
1ec13 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
1ec14 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64  expression p cod
1ec15 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e  es a constant in
1ec16 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73 6d  teger that is sm
1ec17 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f  all enough.** to
1ec18 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74   fit in a 32-bit
1ec19 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
1ec1a 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 76   1 and put the v
1ec1b 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65  alue of the inte
1ec1c 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75  ger.** in *pValu
1ec1d 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 65  e.  If the expre
1ec1e 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20  ssion is not an 
1ec1f 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 74  integer or if it
1ec20 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74   is too big.** t
1ec21 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65  o fit in a signe
1ec22 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  d 32-bit integer
1ec23 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c  , return 0 and l
1ec24 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63  eave *pValue unc
1ec25 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  hanged..*/.SQLIT
1ec26 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1ec27 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
1ec28 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  er(Expr *p, int 
1ec29 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20  *pValue){.  int 
1ec2a 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d  rc = 0;.  if( p-
1ec2b 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56  >flags & EP_IntV
1ec2c 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 61  alue ){.    *pVa
1ec2d 6c 75 65 20 3d 20 70 2d 3e 69 54 61 62 6c 65 3b  lue = p->iTable;
1ec2e 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
1ec2f 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e   }.  switch( p->
1ec30 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
1ec31 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20  K_INTEGER: {.   
1ec32 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47     rc = sqlite3G
1ec33 65 74 49 6e 74 33 32 28 28 63 68 61 72 2a 29 70  etInt32((char*)p
1ec34 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 56 61 6c 75  ->token.z, pValu
1ec35 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  e);.      break;
1ec36 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1ec37 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20  TK_UPLUS: {.    
1ec38 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 45 78    rc = sqlite3Ex
1ec39 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70  prIsInteger(p->p
1ec3a 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20  Left, pValue);. 
1ec3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1ec3c 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d  }.    case TK_UM
1ec3d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e  INUS: {.      in
1ec3e 74 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 73  t v;.      if( s
1ec3f 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65  qlite3ExprIsInte
1ec40 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 76  ger(p->pLeft, &v
1ec41 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 56  ) ){.        *pV
1ec42 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20  alue = -v;.     
1ec43 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20     rc = 1;.     
1ec44 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1ec45 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
1ec46 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20  t: break;.  }.  
1ec47 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 2d  if( rc ){.    p-
1ec48 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52  >op = TK_INTEGER
1ec49 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c  ;.    p->flags |
1ec4a 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20  = EP_IntValue;. 
1ec4b 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 2a     p->iTable = *
1ec4c 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 20 72 65  pValue;.  }.  re
1ec4d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1ec4e 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
1ec4f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e   the given strin
1ec50 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63 6f  g is a row-id co
1ec51 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51  lumn name..*/.SQ
1ec52 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1ec53 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
1ec54 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a  const char *z){.
1ec55 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
1ec56 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 44 5f  ICmp(z, "_ROWID_
1ec57 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  ")==0 ) return 1
1ec58 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
1ec59 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 49 44  trICmp(z, "ROWID
1ec5a 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  ")==0 ) return 1
1ec5b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
1ec5c 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 22 29  trICmp(z, "OID")
1ec5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==0 ) return 1;.
1ec5e 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
1ec5f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
1ec60 20 69 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61   if the IN opera
1ec61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  tor optimization
1ec62 20 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 0a   is enabled and.
1ec63 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  ** the SELECT st
1ec64 61 74 65 6d 65 6e 74 20 70 20 65 78 69 73 74 73  atement p exists
1ec65 20 61 6e 64 20 69 73 20 6f 66 20 74 68 65 0a 2a   and is of the.*
1ec66 2a 20 73 69 6d 70 6c 65 20 66 6f 72 6d 3a 0a 2a  * simple form:.*
1ec67 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
1ec68 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74  <column> FROM <t
1ec69 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  able>.**.** If t
1ec6a 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c  his is the case,
1ec6b 20 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69   it may be possi
1ec6c 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78  ble to use an ex
1ec6d 69 73 74 69 6e 67 20 74 61 62 6c 65 0a 2a 2a 20  isting table.** 
1ec6e 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61 64  or index instead
1ec6f 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20 61   of generating a
1ec70 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c  n epheremal tabl
1ec71 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
1ec72 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1ec73 52 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  RY.static int is
1ec74 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70  CandidateForInOp
1ec75 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  t(Select *p){.  
1ec76 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20  SrcList *pSrc;. 
1ec77 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
1ec78 74 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  t;.  Table *pTab
1ec79 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
1ec7a 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
1ec7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 69             /* ri
1ec7c 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  ght-hand side of
1ec7d 20 49 4e 20 69 73 20 53 45 4c 45 43 54 20 2a 2f   IN is SELECT */
1ec7e 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72  .  if( p->pPrior
1ec7f 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
1ec80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74            /* Not
1ec81 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
1ec82 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73  CT */.  if( p->s
1ec83 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69  elFlags & (SF_Di
1ec84 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67  stinct|SF_Aggreg
1ec85 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ate) ){.      re
1ec86 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 49  turn 0; /* No DI
1ec87 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 61  STINCT keyword a
1ec88 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20  nd no aggregate 
1ec89 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d  functions */.  }
1ec8a 0a 20 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 70  .  if( p->pGroup
1ec8b 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  By ) return 0;  
1ec8c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
1ec8d 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61   no GROUP BY cla
1ec8e 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  use */.  if( p->
1ec8f 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20  pLimit ) return 
1ec90 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
1ec91 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54 20  /* Has no LIMIT 
1ec92 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20  clause */.  if( 
1ec93 70 2d 3e 70 4f 66 66 73 65 74 20 29 20 72 65 74  p->pOffset ) ret
1ec94 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e  urn 0;.  if( p->
1ec95 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 20  pWhere ) return 
1ec96 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
1ec97 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 52 45 20  /* Has no WHERE 
1ec98 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 53 72 63  clause */.  pSrc
1ec99 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73   = p->pSrc;.  as
1ec9a 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29 3b  sert( pSrc!=0 );
1ec9b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53 72  .  if( pSrc->nSr
1ec9c 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b  c!=1 ) return 0;
1ec9d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 6e            /* Sin
1ec9e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f 4d  gle term in FROM
1ec9f 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28   clause */.  if(
1eca0 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c   pSrc->a[0].pSel
1eca1 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ect ) return 0; 
1eca2 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75      /* FROM clau
1eca3 73 65 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71  se is not a subq
1eca4 75 65 72 79 20 2a 2f 0a 20 20 70 54 61 62 20 3d  uery */.  pTab =
1eca5 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62   pSrc->a[0].pTab
1eca6 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
1eca7 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
1eca8 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
1eca9 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
1ecaa 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61       /* FROM cla
1ecab 75 73 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65  use is not a vie
1ecac 77 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72  w */.  if( IsVir
1ecad 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74  tual(pTab) ) ret
1ecae 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  urn 0;        /*
1ecaf 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74   FROM clause not
1ecb0 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1ecb1 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 70   */.  pEList = p
1ecb2 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20  ->pEList;.  if( 
1ecb3 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
1ecb4 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
1ecb5 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e     /* One column
1ecb6 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1ecb7 65 74 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69  et */.  if( pELi
1ecb8 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e  st->a[0].pExpr->
1ecb9 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
1ecba 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73  return 0; /* Res
1ecbb 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20  ult is a column 
1ecbc 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  */.  return 1;.}
1ecbd 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1ecbe 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20  E_OMIT_SUBQUERY 
1ecbf 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  */../*.** This f
1ecc0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
1ecc1 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  by the implement
1ecc2 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20  ation of the IN 
1ecc3 28 2e 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a  (...) operator..
1ecc4 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69 73 20 74  ** It's job is t
1ecc5 6f 20 66 69 6e 64 20 6f 72 20 63 72 65 61 74 65  o find or create
1ecc6 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 74   a b-tree struct
1ecc7 75 72 65 20 74 68 61 74 20 6d 61 79 20 62 65 20  ure that may be 
1ecc8 75 73 65 64 0a 2a 2a 20 65 69 74 68 65 72 20 74  used.** either t
1ecc9 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 6d 62 65  o test for membe
1ecca 72 73 68 69 70 20 6f 66 20 74 68 65 20 28 2e 2e  rship of the (..
1eccb 2e 29 20 73 65 74 20 6f 72 20 74 6f 20 69 74 65  .) set or to ite
1eccc 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20  rate through.** 
1eccd 69 74 73 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69  its members, ski
1ecce 70 70 69 6e 67 20 64 75 70 6c 69 63 61 74 65 73  pping duplicates
1eccf 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73  ..**.** The curs
1ecd0 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65  or opened on the
1ecd1 20 73 74 72 75 63 74 75 72 65 20 28 64 61 74 61   structure (data
1ecd2 62 61 73 65 20 74 61 62 6c 65 2c 20 64 61 74 61  base table, data
1ecd3 62 61 73 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6f  base index .** o
1ecd4 72 20 65 70 68 65 72 6d 61 6c 20 74 61 62 6c 65  r ephermal table
1ecd5 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70  ) is stored in p
1ecd6 58 2d 3e 69 54 61 62 6c 65 20 62 65 66 6f 72 65  X->iTable before
1ecd7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
1ecd8 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 72  eturns..** The r
1ecd9 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e  eturned value in
1ecda 64 69 63 61 74 65 73 20 74 68 65 20 73 74 72 75  dicates the stru
1ecdb 63 74 75 72 65 20 74 79 70 65 2c 20 61 73 20 66  cture type, as f
1ecdc 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
1ecdd 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 2d  IN_INDEX_ROWID -
1ecde 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 20   The cursor was 
1ecdf 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 61  opened on a data
1ece0 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20  base table..**  
1ece1 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20   IN_INDEX_INDEX 
1ece2 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73  - The cursor was
1ece3 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74   opened on a dat
1ece4 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a 20  abase index..** 
1ece5 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 2d    IN_INDEX_EPH -
1ece6 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77 61     The cursor wa
1ece7 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73 70  s opened on a sp
1ece8 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64 20  ecially created 
1ece9 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
1ecea 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c 61            popula
1eceb 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74 61  ted epheremal ta
1ecec 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78  ble..**.** An ex
1eced 69 73 74 69 6e 67 20 73 74 72 75 63 74 75 72 65  isting structure
1ecee 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65   may only be use
1ecef 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20  d if the SELECT 
1ecf0 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65  is of the simple
1ecf1 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  .** form:.**.** 
1ecf2 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75      SELECT <colu
1ecf3 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e  mn> FROM <table>
1ecf4 0a 2a 2a 0a 2a 2a 20 49 66 20 70 72 4e 6f 74 46  .**.** If prNotF
1ecf5 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69  ound parameter i
1ecf6 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 73 74  s 0, then the st
1ecf7 72 75 63 74 75 72 65 20 77 69 6c 6c 20 62 65 20  ructure will be 
1ecf8 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 0a  used to iterate.
1ecf9 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 73  ** through the s
1ecfa 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70  et members, skip
1ecfb 70 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 61  ping any duplica
1ecfc 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73  tes. In this cas
1ecfd 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d 61  e an.** epherema
1ecfe 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20  l table must be 
1ecff 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20  used unless the 
1ed00 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d 6e  selected <column
1ed01 3e 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a  > is guaranteed.
1ed02 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 20  ** to be unique 
1ed03 2d 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65  - either because
1ed04 20 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 45   it is an INTEGE
1ed05 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72  R PRIMARY KEY or
1ed06 20 69 74 0a 2a 2a 20 69 73 20 75 6e 69 71 75 65   it.** is unique
1ed07 20 62 79 20 76 69 72 74 75 65 20 6f 66 20 61 20   by virtue of a 
1ed08 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 20 69 6d  constraint or im
1ed09 70 6c 69 63 69 74 20 69 6e 64 65 78 2e 0a 2a 2a  plicit index..**
1ed0a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f 74  .** If the prNot
1ed0b 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20  Found parameter 
1ed0c 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74  is not 0, then t
1ed0d 68 65 20 73 74 72 75 63 74 75 72 65 20 77 69 6c  he structure wil
1ed0e 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f  l be used .** fo
1ed0f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65  r fast set membe
1ed10 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20  rship tests. In 
1ed11 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 70 68  this case an eph
1ed12 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73  eremal table mus
1ed13 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e  t .** be used un
1ed14 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73  less <column> is
1ed15 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
1ed16 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e  ARY KEY or an in
1ed17 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 66  dex can .** be f
1ed18 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d  ound with <colum
1ed19 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d  n> as its left-m
1ed1a 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  ost column..**.*
1ed1b 2a 20 57 68 65 6e 20 74 68 65 20 73 74 72 75 63  * When the struc
1ed1c 74 75 72 65 20 69 73 20 62 65 69 6e 67 20 75 73  ture is being us
1ed1d 65 64 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65  ed for set membe
1ed1e 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 68 65  rship tests, the
1ed1f 20 75 73 65 72 0a 2a 2a 20 6e 65 65 64 73 20 74   user.** needs t
1ed20 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f  o know whether o
1ed21 72 20 6e 6f 74 20 74 68 65 20 73 74 72 75 63 74  r not the struct
1ed22 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  ure contains an 
1ed23 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c  SQL NULL .** val
1ed24 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63  ue in order to c
1ed25 6f 72 72 65 63 74 6c 79 20 65 76 61 6c 75 61 74  orrectly evaluat
1ed26 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 69  e expressions li
1ed27 6b 65 20 22 58 20 49 4e 20 28 59 2c 20 5a 29 22  ke "X IN (Y, Z)"
1ed28 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  ..** If there is
1ed29 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 74   a chance that t
1ed2a 68 65 20 73 74 72 75 63 74 75 72 65 20 6d 61 79  he structure may
1ed2b 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20   contain a NULL 
1ed2c 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 75 6e 74  value at.** runt
1ed2d 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 65 67 69  ime, then a regi
1ed2e 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65  ster is allocate
1ed2f 64 20 61 6e 64 20 74 68 65 20 72 65 67 69 73 74  d and the regist
1ed30 65 72 20 6e 75 6d 62 65 72 20 77 72 69 74 74 65  er number writte
1ed31 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f 74 46 6f  n.** to *prNotFo
1ed32 75 6e 64 2e 20 49 66 20 74 68 65 72 65 20 69 73  und. If there is
1ed33 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20   no chance that 
1ed34 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  the structure co
1ed35 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c  ntains a.** NULL
1ed36 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 72   value, then *pr
1ed37 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 74  NotFound is left
1ed38 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a   unchanged..**.*
1ed39 2a 20 49 66 20 61 20 72 65 67 69 73 74 65 72 20  * If a register 
1ed3a 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
1ed3b 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 74   its location st
1ed3c 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 6f  ored in *prNotFo
1ed3d 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 73  und, then.** its
1ed3e 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 69   initial value i
1ed3f 73 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73  s NULL. If the s
1ed40 74 72 75 63 74 75 72 65 20 64 6f 65 73 20 6e 6f  tructure does no
1ed41 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e  t remain constan
1ed42 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72  t.** for the dur
1ed43 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65  ation of the que
1ed44 72 79 20 28 69 2e 65 2e 20 74 68 65 20 73 65 74  ry (i.e. the set
1ed45 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64   is a correlated
1ed46 20 73 75 62 2d 73 65 6c 65 63 74 29 2c 20 0a 2a   sub-select), .*
1ed47 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  * the value of t
1ed48 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67  he allocated reg
1ed49 69 73 74 65 72 20 69 73 20 72 65 73 65 74 20 74  ister is reset t
1ed4a 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 6d 65  o NULL each time
1ed4b 20 74 68 65 20 0a 2a 2a 20 73 74 72 75 63 74 75   the .** structu
1ed4c 72 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74 65  re is repopulate
1ed4d 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 74  d. This allows t
1ed4e 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65  he caller to use
1ed4f 20 76 64 62 65 20 63 6f 64 65 20 0a 2a 2a 20 65   vdbe code .** e
1ed50 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65  quivalent to the
1ed51 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
1ed52 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72  *   if( register
1ed53 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20  ==NULL ){.**    
1ed54 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73   has_null = <tes
1ed55 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74  t if data struct
1ed56 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c  ure contains nul
1ed57 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74  l>.**     regist
1ed58 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a  er = 1.**   }.**
1ed59 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  .** in order to 
1ed5a 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68  avoid running th
1ed5b 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20  e <test if data 
1ed5c 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
1ed5d 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74  ns null>.** test
1ed5e 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e   more often than
1ed5f 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   is necessary..*
1ed60 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1ed61 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53  _OMIT_SUBQUERY.S
1ed62 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1ed63 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49  t sqlite3FindInI
1ed64 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72  ndex(Parse *pPar
1ed65 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e  se, Expr *pX, in
1ed66 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a  t *prNotFound){.
1ed67 20 20 53 65 6c 65 63 74 20 2a 70 3b 0a 20 20 69    Select *p;.  i
1ed68 6e 74 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 20  nt eType = 0;.  
1ed69 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73  int iTab = pPars
1ed6a 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 6e 74  e->nTab++;.  int
1ed6b 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d 20   mustBeUnique = 
1ed6c 21 70 72 4e 6f 74 46 6f 75 6e 64 3b 0a 0a 20 20  !prNotFound;..  
1ed6d 2f 2a 20 54 68 65 20 66 6f 6c 6c 77 69 6e 67 20  /* The follwing 
1ed6e 69 66 28 2e 2e 2e 29 20 65 78 70 72 65 73 73 69  if(...) expressi
1ed6f 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74 68  on is true if th
1ed70 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74  e SELECT is of t
1ed71 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20  he .  ** simple 
1ed72 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  form:.  **.  ** 
1ed73 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75      SELECT <colu
1ed74 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e  mn> FROM <table>
1ed75 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
1ed76 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
1ed77 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 62  it may be possib
1ed78 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78 69  le to use an exi
1ed79 73 74 69 6e 67 20 74 61 62 6c 65 0a 20 20 2a 2a  sting table.  **
1ed7a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61   or index instea
1ed7b 64 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20  d of generating 
1ed7c 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62  an epheremal tab
1ed7d 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 28  le..  */.  p = (
1ed7e 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1ed7f 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pX, EP_xIsSelect
1ed80 29 20 3f 20 70 58 2d 3e 78 2e 70 53 65 6c 65 63  ) ? pX->x.pSelec
1ed81 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 69 73  t : 0);.  if( is
1ed82 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70  CandidateForInOp
1ed83 74 28 70 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  t(p) ){.    sqli
1ed84 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1ed85 2d 3e 64 62 3b 0a 20 20 20 20 49 6e 64 65 78 20  ->db;.    Index 
1ed86 2a 70 49 64 78 3b 0a 20 20 20 20 45 78 70 72 20  *pIdx;.    Expr 
1ed87 2a 70 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69  *pExpr = p->pELi
1ed88 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
1ed89 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70      int iCol = p
1ed8a 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  Expr->iColumn;. 
1ed8b 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c     Vdbe *v = sql
1ed8c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
1ed8d 73 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69  se);..    /* Thi
1ed8e 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e  s function is on
1ed8f 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74  ly called from t
1ed90 77 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f  wo places. In bo
1ed91 74 68 20 63 61 73 65 73 20 74 68 65 20 76 64 62  th cases the vdb
1ed92 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72  e.    ** has alr
1ed93 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
1ed94 74 65 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 73  ted. So assume s
1ed95 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 29 20  qlite3GetVdbe() 
1ed96 69 73 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a  is always.    **
1ed97 20 73 75 63 63 65 73 73 66 75 6c 20 68 65 72 65   successful here
1ed98 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
1ed99 65 72 74 28 76 29 3b 0a 20 20 20 20 69 66 28 20  ert(v);.    if( 
1ed9a 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20  iCol<0 ){.      
1ed9b 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61  int iMem = ++pPa
1ed9c 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
1ed9d 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20   int iAddr;.    
1ed9e 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
1ed9f 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54  p->pSrc->a[0].pT
1eda0 61 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44  ab;.      int iD
1eda1 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1eda2 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
1eda3 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
1eda4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
1eda5 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
1eda6 0a 0a 20 20 20 20 20 20 69 41 64 64 72 20 3d 20  ..      iAddr = 
1eda7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1eda8 31 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d  1(v, OP_If, iMem
1eda9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1edaa 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1edab 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65  _Integer, 1, iMe
1edac 6d 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  m);..      sqlit
1edad 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
1edae 73 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70  se, iTab, iDb, p
1edaf 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  Tab, OP_OpenRead
1edb0 29 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d  );.      eType =
1edb1 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b   IN_INDEX_ROWID;
1edb2 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
1edb3 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69  dbeJumpHere(v, i
1edb4 41 64 64 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Addr);.    }else
1edb5 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  {.      /* The c
1edb6 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1edb7 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 6f  e used by the co
1edb8 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20  mparison. If an 
1edb9 69 6e 64 65 78 20 69 73 20 74 6f 20 0a 20 20 20  index is to .   
1edba 20 20 20 2a 2a 20 62 65 20 75 73 65 64 20 69 6e     ** be used in
1edbb 20 70 6c 61 63 65 20 6f 66 20 61 20 74 65 6d 70   place of a temp
1edbc 2d 74 61 62 6c 65 2c 20 69 74 20 6d 75 73 74 20  -table, it must 
1edbd 62 65 20 6f 72 64 65 72 65 64 20 61 63 63 6f 72  be ordered accor
1edbe 64 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 6f  ding.      ** to
1edbf 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20   this collation 
1edc0 73 65 71 75 65 6e 63 65 2e 0a 20 20 20 20 20 20  sequence..      
1edc1 2a 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  */.      CollSeq
1edc2 20 2a 70 52 65 71 20 3d 20 73 71 6c 69 74 65 33   *pReq = sqlite3
1edc3 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
1edc4 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58 2d  lSeq(pParse, pX-
1edc5 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 29 3b 0a  >pLeft, pExpr);.
1edc6 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  .      /* Check 
1edc7 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 74  that the affinit
1edc8 79 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75  y that will be u
1edc9 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 74  sed to perform t
1edca 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d  he .      ** com
1edcb 70 61 72 69 73 6f 6e 20 69 73 20 74 68 65 20 73  parison is the s
1edcc 61 6d 65 20 61 73 20 74 68 65 20 61 66 66 69 6e  ame as the affin
1edcd 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ity of the colum
1edce 6e 2e 20 49 66 0a 20 20 20 20 20 20 2a 2a 20 69  n. If.      ** i
1edcf 74 20 69 73 20 6e 6f 74 2c 20 69 74 20 69 73 20  t is not, it is 
1edd0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
1edd1 75 73 65 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20  use any index.. 
1edd2 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54 61       */.      Ta
1edd3 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70  ble *pTab = p->p
1edd4 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a  Src->a[0].pTab;.
1edd5 20 20 20 20 20 20 63 68 61 72 20 61 66 66 20 3d        char aff =
1edd6 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e   comparisonAffin
1edd7 69 74 79 28 70 58 29 3b 0a 20 20 20 20 20 20 69  ity(pX);.      i
1edd8 6e 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d  nt affinity_ok =
1edd9 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   (pTab->aCol[iCo
1edda 6c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61 66 66  l].affinity==aff
1eddb 7c 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46  ||aff==SQLITE_AF
1eddc 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20  F_NONE);..      
1eddd 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
1edde 49 6e 64 65 78 3b 20 70 49 64 78 20 26 26 20 65  Index; pIdx && e
1eddf 54 79 70 65 3d 3d 30 20 26 26 20 61 66 66 69 6e  Type==0 && affin
1ede0 69 74 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64  ity_ok; pIdx=pId
1ede1 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
1ede2 20 20 20 69 66 28 20 28 70 49 64 78 2d 3e 61 69     if( (pIdx->ai
1ede3 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29  Column[0]==iCol)
1ede4 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70 52  .         && (pR
1ede5 65 71 3d 3d 73 71 6c 69 74 65 33 46 69 6e 64 43  eq==sqlite3FindC
1ede6 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
1ede7 62 29 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  b), pIdx->azColl
1ede8 5b 30 5d 2c 20 2d 31 2c 20 30 29 29 0a 20 20 20  [0], -1, 0)).   
1ede9 20 20 20 20 20 20 26 26 20 28 21 6d 75 73 74 42        && (!mustB
1edea 65 55 6e 69 71 75 65 20 7c 7c 20 28 70 49 64 78  eUnique || (pIdx
1edeb 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20  ->nColumn==1 && 
1edec 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  pIdx->onError!=O
1eded 45 5f 4e 6f 6e 65 29 29 0a 20 20 20 20 20 20 20  E_None)).       
1edee 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
1edef 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 20  t iDb;.         
1edf0 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50   int iMem = ++pP
1edf1 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
1edf2 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b        int iAddr;
1edf3 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
1edf4 2a 70 4b 65 79 3b 0a 20 20 0a 20 20 20 20 20 20  *pKey;.  .      
1edf5 20 20 20 20 70 4b 65 79 20 3d 20 28 63 68 61 72      pKey = (char
1edf6 20 2a 29 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   *)sqlite3IndexK
1edf7 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
1edf8 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Idx);.          
1edf9 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1edfa 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
1edfb 49 64 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Idx->pSchema);. 
1edfc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1edfd 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
1edfe 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 20 20   iDb);..        
1edff 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65    iAddr = sqlite
1ee00 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
1ee01 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20  P_If, iMem);.   
1ee02 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ee03 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
1ee04 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29  nteger, 1, iMem)
1ee05 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 73  ;.  .          s
1ee06 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1ee07 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c  (v, OP_OpenRead,
1ee08 20 69 54 61 62 2c 20 70 49 64 78 2d 3e 74 6e 75   iTab, pIdx->tnu
1ee09 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20  m, iDb,.        
1ee0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ee0b 20 20 20 20 20 20 20 70 4b 65 79 2c 50 34 5f 4b         pKey,P4_K
1ee0c 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
1ee0d 0a 20 20 20 20 20 20 20 20 20 20 56 64 62 65 43  .          VdbeC
1ee0e 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
1ee0f 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pIdx->zName));.
1ee10 20 20 20 20 20 20 20 20 20 20 65 54 79 70 65 20            eType 
1ee11 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58  = IN_INDEX_INDEX
1ee12 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ;..          sql
1ee13 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1ee14 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20  (v, iAddr);.    
1ee15 20 20 20 20 20 20 69 66 28 20 70 72 4e 6f 74 46        if( prNotF
1ee16 6f 75 6e 64 20 26 26 20 21 70 54 61 62 2d 3e 61  ound && !pTab->a
1ee17 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e 75 6c  Col[iCol].notNul
1ee18 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  l ){.           
1ee19 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b   *prNotFound = +
1ee1a 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
1ee1b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1ee1c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1ee1d 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 54   }.  }..  if( eT
1ee1e 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e  ype==0 ){.    in
1ee1f 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d  t rMayHaveNull =
1ee20 20 30 3b 0a 20 20 20 20 65 54 79 70 65 20 3d 20   0;.    eType = 
1ee21 49 4e 5f 49 4e 44 45 58 5f 45 50 48 3b 0a 20 20  IN_INDEX_EPH;.  
1ee22 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64    if( prNotFound
1ee23 20 29 7b 0a 20 20 20 20 20 20 2a 70 72 4e 6f 74   ){.      *prNot
1ee24 46 6f 75 6e 64 20 3d 20 72 4d 61 79 48 61 76 65  Found = rMayHave
1ee25 4e 75 6c 6c 20 3d 20 2b 2b 70 50 61 72 73 65 2d  Null = ++pParse-
1ee26 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65  >nMem;.    }else
1ee27 20 69 66 28 20 70 58 2d 3e 70 4c 65 66 74 2d 3e   if( pX->pLeft->
1ee28 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 45 78  iColumn<0 && !Ex
1ee29 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
1ee2a 28 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  (pX, EP_xIsSelec
1ee2b 74 29 20 29 7b 0a 20 20 20 20 20 20 65 54 79 70  t) ){.      eTyp
1ee2c 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57  e = IN_INDEX_ROW
1ee2d 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ID;.    }.    sq
1ee2e 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65  lite3CodeSubsele
1ee2f 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c 20 72  ct(pParse, pX, r
1ee30 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65 54 79  MayHaveNull, eTy
1ee31 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57  pe==IN_INDEX_ROW
1ee32 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ID);.  }else{.  
1ee33 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20 69    pX->iTable = i
1ee34 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Tab;.  }.  retur
1ee35 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64 69  n eType;.}.#endi
1ee36 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  f../*.** Generat
1ee37 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 6c 61  e code for scala
1ee38 72 20 73 75 62 71 75 65 72 69 65 73 20 75 73 65  r subqueries use
1ee39 64 20 61 73 20 61 6e 20 65 78 70 72 65 73 73 69  d as an expressi
1ee3a 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f 70 65  on.** and IN ope
1ee3b 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c 65  rators.  Example
1ee3c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53 45  s:.**.**     (SE
1ee3d 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20  LECT a FROM b)  
1ee3e 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62 71 75          -- subqu
1ee3f 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 53 54  ery.**     EXIST
1ee40 53 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d  S (SELECT a FROM
1ee41 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54 53 20   b)   -- EXISTS 
1ee42 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 20  subquery.**     
1ee43 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 20 20  x IN (4,5,11)   
1ee44 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 49 4e             -- IN
1ee45 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 6c   operator with l
1ee46 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 61 6e  ist on right-han
1ee47 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 78 20  d side.**     x 
1ee48 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f  IN (SELECT a FRO
1ee49 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e 20 6f  M b)     -- IN o
1ee4a 70 65 72 61 74 6f 72 20 77 69 74 68 20 73 75 62  perator with sub
1ee4b 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72 69 67  query on the rig
1ee4c 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45 78  ht.**.** The pEx
1ee4d 70 72 20 70 61 72 61 6d 65 74 65 72 20 64 65 73  pr parameter des
1ee4e 63 72 69 62 65 73 20 74 68 65 20 65 78 70 72 65  cribes the expre
1ee4f 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 61  ssion that conta
1ee50 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f 70  ins the IN.** op
1ee51 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71 75 65  erator or subque
1ee52 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72  ry..**.** If par
1ee53 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64 20 69  ameter isRowid i
1ee54 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1ee55 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70   expression pExp
1ee56 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a  r is guaranteed.
1ee57 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 65 20  ** to be of the 
1ee58 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49 4e  form "<rowid> IN
1ee59 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68 65   (?, ?, ?)", whe
1ee5a 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 61 20  re <rowid> is a 
1ee5b 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f 20  reference.** to 
1ee5c 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b 65 79  some integer key
1ee5d 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 62   column of a tab
1ee5e 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68  le B-Tree. In th
1ee5f 69 73 20 63 61 73 65 2c 20 75 73 65 20 61 6e 0a  is case, use an.
1ee60 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65  ** intkey B-Tree
1ee61 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65   to store the se
1ee62 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61 6c  t of IN(...) val
1ee63 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 74  ues instead of t
1ee64 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c 6f  he usual.** (slo
1ee65 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 6c 65  wer) variable le
1ee66 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 65 65  ngth keys B-Tree
1ee67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1ee68 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
1ee69 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  Y.SQLITE_PRIVATE
1ee6a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
1ee6b 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20 50 61  eSubselect(.  Pa
1ee6c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20  rse *pParse, .  
1ee6d 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a 20 20  Expr *pExpr, .  
1ee6e 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c  int rMayHaveNull
1ee6f 2c 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 64 0a  ,.  int isRowid.
1ee70 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64 64  ){.  int testAdd
1ee71 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
1ee72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ee73 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61 64  One-time test ad
1ee74 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 62 65 20  dress */.  Vdbe 
1ee75 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
1ee76 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
1ee77 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
1ee78 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f  ;...  /* This co
1ee79 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20 69  de must be run i
1ee7a 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 20 65  n its entirety e
1ee7b 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 73 20  very time it is 
1ee7c 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a 2a  encountered.  **
1ee7d 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66   if any of the f
1ee7e 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65  ollowing is true
1ee7f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a  :.  **.  **    *
1ee80 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64    The right-hand
1ee81 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 72 65   side is a corre
1ee82 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 0a 20  lated subquery. 
1ee83 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69   **    *  The ri
1ee84 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73  ght-hand side is
1ee85 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c   an expression l
1ee86 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76  ist containing v
1ee87 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20 20  ariables.  **   
1ee88 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69 64   *  We are insid
1ee89 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a 2a  e a trigger.  **
1ee8a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 20  .  ** If all of 
1ee8b 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66 61  the above are fa
1ee8c 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61 6e  lse, then we can
1ee8d 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 6a   run this code j
1ee8e 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 61  ust once.  ** sa
1ee8f 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c 20  ve the results, 
1ee90 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 73 61  and reuse the sa
1ee91 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75 62  me result on sub
1ee92 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69  sequent invocati
1ee93 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ons..  */.  if( 
1ee94 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
1ee95 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61  rty(pExpr, EP_Va
1ee96 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 50 61  rSelect) && !pPa
1ee97 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29  rse->trigStack )
1ee98 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d 20  {.    int mem = 
1ee99 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1ee9a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ee9b 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp1(v, OP_If, 
1ee9c 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 41 64  mem);.    testAd
1ee9d 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
1ee9e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
1ee9f 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a 20  eger, 1, mem);. 
1eea0 20 20 20 61 73 73 65 72 74 28 20 74 65 73 74 41     assert( testA
1eea1 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 65 2d  ddr>0 || pParse-
1eea2 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1eea3 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74  d );.  }..  swit
1eea4 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b  ch( pExpr->op ){
1eea5 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a  .    case TK_IN:
1eea6 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66   {.      char af
1eea7 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b 65  finity;.      Ke
1eea8 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a 20  yInfo keyInfo;. 
1eea9 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20 20       int addr;  
1eeaa 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
1eeab 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d   of OP_OpenEphem
1eeac 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  eral instruction
1eead 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
1eeae 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70  pLeft = pExpr->p
1eeaf 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 66 28  Left;..      if(
1eeb0 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 7b   rMayHaveNull ){
1eeb1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1eeb2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1eeb3 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 48 61  _Null, 0, rMayHa
1eeb4 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 7d  veNull);.      }
1eeb5 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 79  ..      affinity
1eeb6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66   = sqlite3ExprAf
1eeb7 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b 0a 0a  finity(pLeft);..
1eeb8 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68 65 72        /* Whether
1eeb9 20 74 68 69 73 20 69 73 20 61 6e 20 27 78 20 49   this is an 'x I
1eeba 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 6f 72  N(SELECT...)' or
1eebb 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 72 6c   an 'x IN(<exprl
1eebc 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a 2a 20  ist>)'.      ** 
1eebd 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20 69 73  expression it is
1eebe 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73 61 6d   handled the sam
1eebf 65 20 77 61 79 2e 20 41 20 76 69 72 74 75 61 6c  e way. A virtual
1eec0 20 74 61 62 6c 65 20 69 73 20 0a 20 20 20 20 20   table is .     
1eec1 20 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68 20   ** filled with 
1eec2 73 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 6e 64  single-field ind
1eec3 65 78 20 6b 65 79 73 20 72 65 70 72 65 73 65 6e  ex keys represen
1eec4 74 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 73  ting the results
1eec5 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74  .      ** from t
1eec6 68 65 20 53 45 4c 45 43 54 20 6f 72 20 74 68 65  he SELECT or the
1eec7 20 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 20 20   <exprlist>..   
1eec8 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49     **.      ** I
1eec9 66 20 74 68 65 20 27 78 27 20 65 78 70 72 65 73  f the 'x' expres
1eeca 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e  sion is a column
1eecb 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20 53   value, or the S
1eecc 45 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 20 2a  ELECT....      *
1eecd 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75  * statement retu
1eece 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c  rns a column val
1eecf 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66 66  ue, then the aff
1eed0 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0a 20 20  inity of that.  
1eed1 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73      ** column is
1eed2 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 74   used to build t
1eed3 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49  he index keys. I
1eed4 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64 20 74  f both 'x' and t
1eed5 68 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c 45  he.      ** SELE
1eed6 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20  CT... statement 
1eed7 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65  are columns, the
1eed8 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69  n numeric affini
1eed9 74 79 20 69 73 20 75 73 65 64 0a 20 20 20 20 20  ty is used.     
1eeda 20 2a 2a 20 69 66 20 65 69 74 68 65 72 20 63 6f   ** if either co
1eedb 6c 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 49 43  lumn has NUMERIC
1eedc 20 6f 72 20 49 4e 54 45 47 45 52 20 61 66 66 69   or INTEGER affi
1eedd 6e 69 74 79 2e 20 49 66 20 6e 65 69 74 68 65 72  nity. If neither
1eede 0a 20 20 20 20 20 20 2a 2a 20 27 78 27 20 6e 6f  .      ** 'x' no
1eedf 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20  r the SELECT... 
1eee0 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f  statement are co
1eee1 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65  lumns, then nume
1eee2 72 69 63 20 61 66 66 69 6e 69 74 79 0a 20 20 20  ric affinity.   
1eee3 20 20 20 2a 2a 20 69 73 20 75 73 65 64 2e 0a 20     ** is used.. 
1eee4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 45       */.      pE
1eee5 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 50  xpr->iTable = pP
1eee6 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
1eee7 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
1eee8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1eee9 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
1eeea 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
1eeeb 20 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 20 20   !isRowid);.    
1eeec 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e 66    memset(&keyInf
1eeed 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79  o, 0, sizeof(key
1eeee 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b 65  Info));.      ke
1eeef 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20 31  yInfo.nField = 1
1eef0 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 45 78 70  ;..      if( Exp
1eef1 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
1eef2 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
1eef3 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
1eef4 43 61 73 65 20 31 3a 20 20 20 20 20 65 78 70 72  Case 1:     expr
1eef5 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
1eef6 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
1eef7 20 20 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 20      ** Generate 
1eef8 63 6f 64 65 20 74 6f 20 77 72 69 74 65 20 74 68  code to write th
1eef9 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
1eefa 20 73 65 6c 65 63 74 20 69 6e 74 6f 20 74 68 65   select into the
1eefb 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 20   temporary.     
1eefc 20 20 20 2a 2a 20 74 61 62 6c 65 20 61 6c 6c 6f     ** table allo
1eefd 63 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64  cated and opened
1eefe 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20   above..        
1eeff 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63  */.        Selec
1ef00 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20  tDest dest;.    
1ef01 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45      ExprList *pE
1ef02 4c 69 73 74 3b 0a 0a 20 20 20 20 20 20 20 20 61  List;..        a
1ef03 73 73 65 72 74 28 20 21 69 73 52 6f 77 69 64 20  ssert( !isRowid 
1ef04 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1ef05 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
1ef06 28 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74 2c  (&dest, SRT_Set,
1ef07 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b   pExpr->iTable);
1ef08 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 61 66  .        dest.af
1ef09 66 69 6e 69 74 79 20 3d 20 28 75 38 29 61 66 66  finity = (u8)aff
1ef0a 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 61  inity;.        a
1ef0b 73 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e 69  ssert( (pExpr->i
1ef0c 54 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 46  Table&0x0000FFFF
1ef0d 29 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  )==pExpr->iTable
1ef0e 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
1ef0f 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
1ef10 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 78 2e 70  arse, pExpr->x.p
1ef11 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29 20 29  Select, &dest) )
1ef12 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
1ef13 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rn;.        }.  
1ef14 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70        pEList = p
1ef15 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d  Expr->x.pSelect-
1ef16 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 20  >pEList;.       
1ef17 20 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70   if( pEList && p
1ef18 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29  EList->nExpr>0 )
1ef19 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79  { .          key
1ef1a 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20  Info.aColl[0] = 
1ef1b 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
1ef1c 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  pareCollSeq(pPar
1ef1d 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
1ef1e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1ef1f 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
1ef20 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pr);.        }. 
1ef21 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
1ef22 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 29 7b  Expr->x.pList ){
1ef23 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65  .        /* Case
1ef24 20 32 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20   2:     expr IN 
1ef25 28 65 78 70 72 6c 69 73 74 29 0a 20 20 20 20 20  (exprlist).     
1ef26 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
1ef27 20 46 6f 72 20 65 61 63 68 20 65 78 70 72 65 73   For each expres
1ef28 73 69 6f 6e 2c 20 62 75 69 6c 64 20 61 6e 20 69  sion, build an i
1ef29 6e 64 65 78 20 6b 65 79 20 66 72 6f 6d 20 74 68  ndex key from th
1ef2a 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 61 6e 64  e evaluation and
1ef2b 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72  .        ** stor
1ef2c 65 20 69 74 20 69 6e 20 74 68 65 20 74 65 6d 70  e it in the temp
1ef2d 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 49 66 20  orary table. If 
1ef2e 3c 65 78 70 72 3e 20 69 73 20 61 20 63 6f 6c 75  <expr> is a colu
1ef2f 6d 6e 2c 20 74 68 65 6e 20 75 73 65 0a 20 20 20  mn, then use.   
1ef30 20 20 20 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c       ** that col
1ef31 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 20 77 68  umns affinity wh
1ef32 65 6e 20 62 75 69 6c 64 69 6e 67 20 69 6e 64 65  en building inde
1ef33 78 20 6b 65 79 73 2e 20 49 66 20 3c 65 78 70 72  x keys. If <expr
1ef34 3e 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 20  > is not.       
1ef35 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e 2c 20 75 73   ** a column, us
1ef36 65 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69  e numeric affini
1ef37 74 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ty..        */. 
1ef38 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20         int i;.  
1ef39 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
1ef3a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78  pList = pExpr->x
1ef3b 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20  .pList;.        
1ef3c 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1ef3d 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
1ef3e 20 20 20 20 20 69 6e 74 20 72 31 2c 20 72 32 2c       int r1, r2,
1ef3f 20 72 33 3b 0a 0a 20 20 20 20 20 20 20 20 69 66   r3;..        if
1ef40 28 20 21 61 66 66 69 6e 69 74 79 20 29 7b 0a 20  ( !affinity ){. 
1ef41 20 20 20 20 20 20 20 20 20 61 66 66 69 6e 69 74           affinit
1ef42 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  y = SQLITE_AFF_N
1ef43 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ONE;.        }. 
1ef44 20 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61         keyInfo.a
1ef45 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65  Coll[0] = sqlite
1ef46 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
1ef47 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1ef48 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  t);..        /* 
1ef49 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61 63  Loop through eac
1ef4a 68 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  h expression in 
1ef4b 3c 65 78 70 72 6c 69 73 74 3e 2e 20 2a 2f 0a 20  <exprlist>. */. 
1ef4c 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69         r1 = sqli
1ef4d 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
1ef4e 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72  arse);.        r
1ef4f 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  2 = sqlite3GetTe
1ef50 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
1ef51 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ef52 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
1ef53 75 6c 6c 2c 20 30 2c 20 72 32 29 3b 0a 20 20 20  ull, 0, r2);.   
1ef54 20 20 20 20 20 66 6f 72 28 69 3d 70 4c 69 73 74       for(i=pList
1ef55 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70  ->nExpr, pItem=p
1ef56 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d  List->a; i>0; i-
1ef57 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  -, pItem++){.   
1ef58 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 32         Expr *pE2
1ef59 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b   = pItem->pExpr;
1ef5a 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ..          /* I
1ef5b 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1ef5c 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74   is not constant
1ef5d 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e 65   then we will ne
1ef5e 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20  ed to.          
1ef5f 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 65 20 74  ** disable the t
1ef60 65 73 74 20 74 68 61 74 20 77 61 73 20 67 65 6e  est that was gen
1ef61 65 72 61 74 65 64 20 61 62 6f 76 65 20 74 68 61  erated above tha
1ef62 74 20 6d 61 6b 65 73 20 73 75 72 65 0a 20 20 20  t makes sure.   
1ef63 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63         ** this c
1ef64 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 75 74 65  ode only execute
1ef65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 75 73 65  s once.  Because
1ef66 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f 6e 73 74   for a non-const
1ef67 61 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ant.          **
1ef68 20 65 78 70 72 65 73 73 69 6f 6e 20 77 65 20 6e   expression we n
1ef69 65 65 64 20 74 6f 20 72 65 72 75 6e 20 74 68 69  eed to rerun thi
1ef6a 73 20 63 6f 64 65 20 65 61 63 68 20 74 69 6d 65  s code each time
1ef6b 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
1ef6c 20 20 20 20 20 20 20 20 20 69 66 28 20 74 65 73           if( tes
1ef6d 74 41 64 64 72 20 26 26 20 21 73 71 6c 69 74 65  tAddr && !sqlite
1ef6e 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28  3ExprIsConstant(
1ef6f 70 45 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pE2) ){.        
1ef70 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1ef71 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 74  hangeToNoop(v, t
1ef72 65 73 74 41 64 64 72 2d 31 2c 20 32 29 3b 0a 20  estAddr-1, 2);. 
1ef73 20 20 20 20 20 20 20 20 20 20 20 74 65 73 74 41             testA
1ef74 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ddr = 0;.       
1ef75 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20     }..          
1ef76 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20  /* Evaluate the 
1ef77 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 69  expression and i
1ef78 6e 73 65 72 74 20 69 74 20 69 6e 74 6f 20 74 68  nsert it into th
1ef79 65 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a  e temp table */.
1ef7a 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
1ef7b 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
1ef7c 65 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 72  e++;.          r
1ef7d 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  3 = sqlite3ExprC
1ef7e 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65  odeTarget(pParse
1ef7f 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 20  , pE2, r1);.    
1ef80 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1ef81 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1ef82 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20  Cache>0 );.     
1ef83 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73       pParse->dis
1ef84 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a  ableColCache--;.
1ef85 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
1ef86 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  sRowid ){.      
1ef87 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ef88 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75  eAddOp2(v, OP_Mu
1ef89 73 74 42 65 49 6e 74 2c 20 72 33 2c 20 73 71 6c  stBeInt, r3, sql
1ef8a 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
1ef8b 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20  ddr(v)+2);.     
1ef8c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1ef8d 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
1ef8e 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 54  nsert, pExpr->iT
1ef8f 61 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a 20  able, r2, r3);. 
1ef90 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
1ef91 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1ef92 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1ef93 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
1ef94 72 33 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69  r3, 1, r2, &affi
1ef95 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
1ef96 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1ef97 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
1ef98 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 33 2c  ange(pParse, r3,
1ef99 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   1);.           
1ef9a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ef9b 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p2(v, OP_IdxInse
1ef9c 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  rt, pExpr->iTabl
1ef9d 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20  e, r2);.        
1ef9e 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1ef9f 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
1efa0 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1efa1 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20  se, r1);.       
1efa2 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1efa3 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
1efa4 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  2);.      }.    
1efa5 20 20 69 66 28 20 21 69 73 52 6f 77 69 64 20 29    if( !isRowid )
1efa6 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1efa7 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
1efa8 20 61 64 64 72 2c 20 28 76 6f 69 64 20 2a 29 26   addr, (void *)&
1efa9 6b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  keyInfo, P4_KEYI
1efaa 4e 46 4f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  NFO);.      }.  
1efab 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1efac 0a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58  ..    case TK_EX
1efad 49 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54  ISTS:.    case T
1efae 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20  K_SELECT: {.    
1efaf 20 20 2f 2a 20 54 68 69 73 20 68 61 73 20 74 6f    /* This has to
1efb0 20 62 65 20 61 20 73 63 61 6c 61 72 20 53 45 4c   be a scalar SEL
1efb1 45 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63  ECT.  Generate c
1efb2 6f 64 65 20 74 6f 20 70 75 74 20 74 68 65 0a 20  ode to put the. 
1efb3 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66       ** value of
1efb4 20 74 68 69 73 20 73 65 6c 65 63 74 20 69 6e 20   this select in 
1efb5 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e  a memory cell an
1efb6 64 20 72 65 63 6f 72 64 20 74 68 65 20 6e 75 6d  d record the num
1efb7 62 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  ber.      ** of 
1efb8 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
1efb9 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20  in iColumn..    
1efba 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69    */.      stati
1efbb 63 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e  c const Token on
1efbc 65 20 3d 20 7b 20 28 75 38 2a 29 22 31 22 2c 20  e = { (u8*)"1", 
1efbd 30 2c 20 31 20 7d 3b 0a 20 20 20 20 20 20 53 65  0, 1 };.      Se
1efbe 6c 65 63 74 20 2a 70 53 65 6c 3b 0a 20 20 20 20  lect *pSel;.    
1efbf 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73    SelectDest des
1efc0 74 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  t;..      assert
1efc1 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
1efc2 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
1efc3 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20  elect) );.      
1efc4 70 53 65 6c 20 3d 20 70 45 78 70 72 2d 3e 78 2e  pSel = pExpr->x.
1efc5 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 73  pSelect;.      s
1efc6 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
1efc7 49 6e 69 74 28 26 64 65 73 74 2c 20 30 2c 20 2b  Init(&dest, 0, +
1efc8 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a  +pParse->nMem);.
1efc9 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1efca 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29  >op==TK_SELECT )
1efcb 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65  {.        dest.e
1efcc 44 65 73 74 20 3d 20 53 52 54 5f 4d 65 6d 3b 0a  Dest = SRT_Mem;.
1efcd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1efce 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1efcf 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 74 2e 69 50  Null, 0, dest.iP
1efd0 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64  arm);.        Vd
1efd1 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49  beComment((v, "I
1efd2 6e 69 74 20 73 75 62 71 75 65 72 79 20 72 65 73  nit subquery res
1efd3 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d 65  ult"));.      }e
1efd4 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 65 73  lse{.        des
1efd5 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 45 78  t.eDest = SRT_Ex
1efd6 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20 73 71  ists;.        sq
1efd7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1efd8 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
1efd9 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20  , dest.iParm);. 
1efda 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65         VdbeComme
1efdb 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 45 58 49  nt((v, "Init EXI
1efdc 53 54 53 20 72 65 73 75 6c 74 22 29 29 3b 0a 20  STS result"));. 
1efdd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
1efde 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
1efdf 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c 2d  Parse->db, pSel-
1efe0 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20  >pLimit);.      
1efe1 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20 73  pSel->pLimit = s
1efe2 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
1efe3 73 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20  se, TK_INTEGER, 
1efe4 30 2c 20 30 2c 20 26 6f 6e 65 29 3b 0a 20 20 20  0, 0, &one);.   
1efe5 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65     if( sqlite3Se
1efe6 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65  lect(pParse, pSe
1efe7 6c 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20  l, &dest) ){.   
1efe8 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
1efe9 20 20 20 7d 0a 20 20 20 20 20 20 70 45 78 70 72     }.      pExpr
1efea 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 64 65 73 74  ->iColumn = dest
1efeb 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 62 72  .iParm;.      br
1efec 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  eak;.    }.  }..
1efed 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 29    if( testAddr )
1efee 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1efef 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 65 73  eJumpHere(v, tes
1eff0 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d 0a 0a 20  tAddr-1);.  }.. 
1eff1 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69   return;.}.#endi
1eff2 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1eff3 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a  _SUBQUERY */../*
1eff4 0a 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 61 6e  .** Duplicate an
1eff5 20 38 2d 62 79 74 65 20 76 61 6c 75 65 0a 2a 2f   8-byte value.*/
1eff6 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 75  .static char *du
1eff7 70 38 62 79 74 65 73 28 56 64 62 65 20 2a 76 2c  p8bytes(Vdbe *v,
1eff8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 29   const char *in)
1eff9 7b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20  {.  char *out = 
1effa 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
1effb 61 77 28 73 71 6c 69 74 65 33 56 64 62 65 44 62  aw(sqlite3VdbeDb
1effc 28 76 29 2c 20 38 29 3b 0a 20 20 69 66 28 20 6f  (v), 8);.  if( o
1effd 75 74 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  ut ){.    memcpy
1effe 28 6f 75 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 20  (out, in, 8);.  
1efff 7d 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a  }.  return out;.
1f000 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1f001 65 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e  e an instruction
1f002 20 74 68 61 74 20 77 69 6c 6c 20 70 75 74 20 74   that will put t
1f003 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  he floating poin
1f004 74 0a 2a 2a 20 76 61 6c 75 65 20 64 65 73 63 72  t.** value descr
1f005 69 62 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31  ibed by z[0..n-1
1f006 5d 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  ] into register 
1f007 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  iMem..**.** The 
1f008 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20  z[] string will 
1f009 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20  probably not be 
1f00a 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e  zero-terminated.
1f00b 20 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b    But the .** z[
1f00c 6e 5d 20 63 68 61 72 61 63 74 65 72 20 69 73 20  n] character is 
1f00d 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65  guaranteed to be
1f00e 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20   something that 
1f00f 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a  does not look.**
1f010 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e   like the contin
1f011 75 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75  uation of the nu
1f012 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mber..*/.static 
1f013 76 6f 69 64 20 63 6f 64 65 52 65 61 6c 28 56 64  void codeReal(Vd
1f014 62 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61  be *v, const cha
1f015 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  r *z, int n, int
1f016 20 6e 65 67 61 74 65 46 6c 61 67 2c 20 69 6e 74   negateFlag, int
1f017 20 69 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74   iMem){.  assert
1f018 28 20 7a 20 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73  ( z || v==0 || s
1f019 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2d  qlite3VdbeDb(v)-
1f01a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
1f01b 0a 20 20 61 73 73 65 72 74 28 20 21 7a 20 7c 7c  .  assert( !z ||
1f01c 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74   !sqlite3Isdigit
1f01d 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20 55 4e 55 53  (z[n]) );.  UNUS
1f01e 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b  ED_PARAMETER(n);
1f01f 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20  .  if( z ){.    
1f020 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20 20  double value;.  
1f021 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20    char *zV;.    
1f022 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20 26  sqlite3AtoF(z, &
1f023 76 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20  value);.    if( 
1f024 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c  sqlite3IsNaN(val
1f025 75 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ue) ){.      sql
1f026 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1f027 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d  , OP_Null, 0, iM
1f028 65 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  em);.    }else{.
1f029 20 20 20 20 20 20 69 66 28 20 6e 65 67 61 74 65        if( negate
1f02a 46 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d  Flag ) value = -
1f02b 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20  value;.      zV 
1f02c 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28  = dup8bytes(v, (
1f02d 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20  char*)&value);. 
1f02e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f02f 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61  AddOp4(v, OP_Rea
1f030 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a  l, 0, iMem, 0, z
1f031 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 20  V, P4_REAL);.   
1f032 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a   }.  }.}.../*.**
1f033 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73   Generate an ins
1f034 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69  truction that wi
1f035 6c 6c 20 70 75 74 20 74 68 65 20 69 6e 74 65 67  ll put the integ
1f036 65 72 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a  er describe by.*
1f037 2a 20 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d  * text z[0..n-1]
1f038 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69   into register i
1f039 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a  Mem..**.** The z
1f03a 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70  [] string will p
1f03b 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a  robably not be z
1f03c 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20  ero-terminated. 
1f03d 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e   But the .** z[n
1f03e 5d 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67  ] character is g
1f03f 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20  uaranteed to be 
1f040 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64  something that d
1f041 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20  oes not look.** 
1f042 6c 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75  like the continu
1f043 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d  ation of the num
1f044 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ber..*/.static v
1f045 6f 69 64 20 63 6f 64 65 49 6e 74 65 67 65 72 28  oid codeInteger(
1f046 56 64 62 65 20 2a 76 2c 20 45 78 70 72 20 2a 70  Vdbe *v, Expr *p
1f047 45 78 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61  Expr, int negFla
1f048 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20  g, int iMem){.  
1f049 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20  const char *z;. 
1f04a 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67   if( pExpr->flag
1f04b 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20  s & EP_IntValue 
1f04c 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70  ){.    int i = p
1f04d 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20  Expr->iTable;.  
1f04e 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20    if( negFlag ) 
1f04f 69 20 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69  i = -i;.    sqli
1f050 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1f051 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
1f052 69 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 20 69  iMem);.  }else i
1f053 66 28 20 28 7a 20 3d 20 28 63 68 61 72 2a 29 70  f( (z = (char*)p
1f054 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 29 21 3d  Expr->token.z)!=
1f055 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
1f056 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70      int n = pExp
1f057 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20  r->token.n;.    
1f058 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33  assert( !sqlite3
1f059 49 73 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b  Isdigit(z[n]) );
1f05a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1f05b 47 65 74 49 6e 74 33 32 28 7a 2c 20 26 69 29 20  GetInt32(z, &i) 
1f05c 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67  ){.      if( neg
1f05d 46 6c 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20  Flag ) i = -i;. 
1f05e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f05f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
1f060 65 67 65 72 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a  eger, i, iMem);.
1f061 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71      }else if( sq
1f062 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74  lite3FitsIn64Bit
1f063 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b  s(z, negFlag) ){
1f064 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c 75 65  .      i64 value
1f065 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56  ;.      char *zV
1f066 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41  ;.      sqlite3A
1f067 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29  toi64(z, &value)
1f068 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 46  ;.      if( negF
1f069 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76  lag ) value = -v
1f06a 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d  alue;.      zV =
1f06b 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28 63   dup8bytes(v, (c
1f06c 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20  har*)&value);.  
1f06d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f06e 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36  ddOp4(v, OP_Int6
1f06f 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a  4, 0, iMem, 0, z
1f070 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20  V, P4_INT64);.  
1f071 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
1f072 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 2c  odeReal(v, z, n,
1f073 20 6e 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b   negFlag, iMem);
1f074 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f  .    }.  }.}.../
1f075 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1f076 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74  de that will ext
1f077 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75 6d 6e  ract the iColumn
1f078 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a  -th column from.
1f079 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e  ** table pTab an
1f07a 64 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75  d store the colu
1f07b 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61 20 72 65  mn value in a re
1f07c 67 69 73 74 65 72 2e 20 20 41 6e 20 65 66 66 6f  gister.  An effo
1f07d 72 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f  rt.** is made to
1f07e 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d   store the colum
1f07f 6e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  n value in regis
1f080 74 65 72 20 69 52 65 67 2c 20 62 75 74 20 74 68  ter iReg, but th
1f081 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 61  is is.** not gua
1f082 72 61 6e 74 65 65 64 2e 20 20 54 68 65 20 6c 6f  ranteed.  The lo
1f083 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f  cation of the co
1f084 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 65  lumn value is re
1f085 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
1f086 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f  ere must be an o
1f087 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20 70 54  pen cursor to pT
1f088 61 62 20 69 6e 20 69 54 61 62 6c 65 20 77 68 65  ab in iTable whe
1f089 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
1f08a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 49 66  * is called.  If
1f08b 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e 20   iColumn<0 then 
1f08c 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74 65  code is generate
1f08d 64 20 74 68 61 74 20 65 78 74 72 61 63 74 73 20  d that extracts 
1f08e 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a  the rowid..**.**
1f08f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69   This routine mi
1f090 67 68 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72  ght attempt to r
1f091 65 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f  euse the value o
1f092 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61  f the column tha
1f093 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79  t.** has already
1f094 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e 74   been loaded int
1f095 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 54  o a register.  T
1f096 68 65 20 76 61 6c 75 65 20 77 69 6c 6c 20 61 6c  he value will al
1f097 77 61 79 73 0a 2a 2a 20 62 65 20 75 73 65 64 20  ways.** be used 
1f098 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 75 6e  if it has not un
1f099 64 65 72 67 6f 6e 65 20 61 6e 79 20 61 66 66 69  dergone any affi
1f09a 6e 69 74 79 20 63 68 61 6e 67 65 73 2e 20 20 42  nity changes.  B
1f09b 75 74 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66 69  ut if.** an affi
1f09c 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 20  nity change has 
1f09d 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74  occurred, then t
1f09e 68 65 20 63 61 63 68 65 64 20 76 61 6c 75 65 20  he cached value 
1f09f 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20  will only be.** 
1f0a0 75 73 65 64 20 69 66 20 61 6c 6c 6f 77 41 66 66  used if allowAff
1f0a1 43 68 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a 2f  Chng is true..*/
1f0a2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f0a3 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
1f0a4 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 20  odeGetColumn(.  
1f0a5 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1f0a6 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20   /* Parsing and 
1f0a7 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20  code generating 
1f0a8 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
1f0a9 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a  le *pTab,     /*
1f0aa 20 44 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20   Description of 
1f0ab 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  the table we are
1f0ac 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f   reading from */
1f0ad 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20  .  int iColumn, 
1f0ae 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1f0af 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  the table column
1f0b0 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
1f0b1 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75  ,      /* The cu
1f0b2 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  rsor pointing to
1f0b3 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
1f0b4 69 6e 74 20 69 52 65 67 2c 20 20 20 20 20 20 20  int iReg,       
1f0b5 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
1f0b6 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  s here */.  int 
1f0b7 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a 20  allowAffChng /* 
1f0b8 54 72 75 65 20 69 66 20 70 72 69 6f 72 20 61 66  True if prior af
1f0b9 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 20 61  finity changes a
1f0ba 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64  re OK */.){.  Vd
1f0bb 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
1f0bc 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  pVdbe;.  int i;.
1f0bd 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63    struct yColCac
1f0be 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d  he *p;..  for(i=
1f0bf 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f  0, p=pParse->aCo
1f0c0 6c 43 61 63 68 65 3b 20 69 3c 70 50 61 72 73 65  lCache; i<pParse
1f0c1 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b  ->nColCache; i++
1f0c2 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , p++){.    if( 
1f0c3 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c  p->iTable==iTabl
1f0c4 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d  e && p->iColumn=
1f0c5 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20  =iColumn.       
1f0c6 20 20 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43      && (!p->affC
1f0c7 68 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66  hange || allowAf
1f0c8 66 43 68 6e 67 29 20 29 7b 0a 23 69 66 20 30 0a  fChng) ){.#if 0.
1f0c9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f0ca 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f  eAddOp0(v, OP_No
1f0cb 6f 70 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  op);.      VdbeC
1f0cc 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 50 54 3a  omment((v, "OPT:
1f0cd 20 74 61 62 25 64 2e 63 6f 6c 25 64 20 2d 3e 20   tab%d.col%d -> 
1f0ce 72 25 64 22 2c 20 69 54 61 62 6c 65 2c 20 69 43  r%d", iTable, iC
1f0cf 6f 6c 75 6d 6e 2c 20 70 2d 3e 69 52 65 67 29 29  olumn, p->iReg))
1f0d0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72  ;.#endif.      r
1f0d1 65 74 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20  eturn p->iReg;. 
1f0d2 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73     }.  }  .  ass
1f0d3 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 69  ert( v!=0 );.  i
1f0d4 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a  f( iColumn<0 ){.
1f0d5 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 28 70 54      int op = (pT
1f0d6 61 62 20 26 26 20 49 73 56 69 72 74 75 61 6c 28  ab && IsVirtual(
1f0d7 70 54 61 62 29 29 20 3f 20 4f 50 5f 56 52 6f 77  pTab)) ? OP_VRow
1f0d8 69 64 20 3a 20 4f 50 5f 52 6f 77 69 64 3b 0a 20  id : OP_Rowid;. 
1f0d9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1f0da 64 4f 70 32 28 76 2c 20 6f 70 2c 20 69 54 61 62  dOp2(v, op, iTab
1f0db 6c 65 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c  le, iReg);.  }el
1f0dc 73 65 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  se if( pTab==0 )
1f0dd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1f0de 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
1f0df 6c 75 6d 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43  lumn, iTable, iC
1f0e0 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20  olumn, iReg);.  
1f0e1 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6f  }else{.    int o
1f0e2 70 20 3d 20 49 73 56 69 72 74 75 61 6c 28 70 54  p = IsVirtual(pT
1f0e3 61 62 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e  ab) ? OP_VColumn
1f0e4 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20   : OP_Column;.  
1f0e5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f0e6 4f 70 33 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c  Op3(v, op, iTabl
1f0e7 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67  e, iColumn, iReg
1f0e8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  );.    sqlite3Co
1f0e9 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70  lumnDefault(v, p
1f0ea 54 61 62 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 23  Tab, iColumn);.#
1f0eb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f0ec 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
1f0ed 54 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e  T.    if( pTab->
1f0ee 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66  aCol[iColumn].af
1f0ef 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
1f0f0 46 46 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20 20  FF_REAL ){.     
1f0f1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f0f2 70 31 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66  p1(v, OP_RealAff
1f0f3 69 6e 69 74 79 2c 20 69 52 65 67 29 3b 0a 20 20  inity, iReg);.  
1f0f4 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
1f0f5 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73   if( pParse->dis
1f0f6 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20  ableColCache==0 
1f0f7 29 7b 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73  ){.    i = pPars
1f0f8 65 2d 3e 69 43 6f 6c 43 61 63 68 65 3b 0a 20 20  e->iColCache;.  
1f0f9 20 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61    p = &pParse->a
1f0fa 43 6f 6c 43 61 63 68 65 5b 69 5d 3b 0a 20 20 20  ColCache[i];.   
1f0fb 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61   p->iTable = iTa
1f0fc 62 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c  ble;.    p->iCol
1f0fd 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20  umn = iColumn;. 
1f0fe 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65     p->iReg = iRe
1f0ff 67 3b 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61  g;.    p->affCha
1f100 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b  nge = 0;.    i++
1f101 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 41 72 72  ;.    if( i>=Arr
1f102 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e 61  aySize(pParse->a
1f103 43 6f 6c 43 61 63 68 65 29 20 29 20 69 20 3d 20  ColCache) ) i = 
1f104 30 3b 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61  0;.    if( i>pPa
1f105 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 20 29  rse->nColCache )
1f106 20 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63   pParse->nColCac
1f107 68 65 20 3d 20 69 3b 0a 20 20 20 20 70 50 61 72  he = i;.    pPar
1f108 73 65 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20  se->iColCache = 
1f109 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  i;.  }.  return 
1f10a 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  iReg;.}../*.** C
1f10b 6c 65 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20  lear all column 
1f10c 63 61 63 68 65 20 65 6e 74 72 69 65 73 20 61 73  cache entries as
1f10d 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
1f10e 65 20 76 64 62 65 0a 2a 2a 20 63 75 72 73 6f 72  e vdbe.** cursor
1f10f 20 77 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d   with cursor num
1f110 62 65 72 20 69 54 61 62 6c 65 2e 0a 2a 2f 0a 53  ber iTable..*/.S
1f111 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1f112 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c  id sqlite3ExprCl
1f113 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50  earColumnCache(P
1f114 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1f115 74 20 69 54 61 62 6c 65 29 7b 0a 20 20 69 66 28  t iTable){.  if(
1f116 20 69 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20   iTable<0 ){.   
1f117 20 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63   pParse->nColCac
1f118 68 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72  he = 0;.    pPar
1f119 73 65 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20  se->iColCache = 
1f11a 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
1f11b 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
1f11c 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43  =0; i<pParse->nC
1f11d 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20  olCache; i++){. 
1f11e 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d       if( pParse-
1f11f 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 54  >aColCache[i].iT
1f120 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a  able==iTable ){.
1f121 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
1f122 28 20 69 3d 3d 70 50 61 72 73 65 2d 3e 6e 43 6f  ( i==pParse->nCo
1f123 6c 43 61 63 68 65 2d 31 20 29 3b 0a 20 20 20 20  lCache-1 );.    
1f124 20 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c      pParse->aCol
1f125 43 61 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73  Cache[i] = pPars
1f126 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70  e->aColCache[--p
1f127 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65  Parse->nColCache
1f128 5d 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  ];.        pPars
1f129 65 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70  e->iColCache = p
1f12a 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65  Parse->nColCache
1f12b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1f12c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63    }.}../*.** Rec
1f12d 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61  ord the fact tha
1f12e 74 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68  t an affinity ch
1f12f 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65  ange has occurre
1f130 64 20 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a 20 72  d on iCount.** r
1f131 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
1f132 67 20 77 69 74 68 20 69 53 74 61 72 74 2e 0a 2a  g with iStart..*
1f133 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f134 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
1f135 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
1f136 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 61 72  ange(Parse *pPar
1f137 73 65 2c 20 69 6e 74 20 69 53 74 61 72 74 2c 20  se, int iStart, 
1f138 69 6e 74 20 69 43 6f 75 6e 74 29 7b 0a 20 20 69  int iCount){.  i
1f139 6e 74 20 69 45 6e 64 20 3d 20 69 53 74 61 72 74  nt iEnd = iStart
1f13a 20 2b 20 69 43 6f 75 6e 74 20 2d 20 31 3b 0a 20   + iCount - 1;. 
1f13b 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
1f13c 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f  0; i<pParse->nCo
1f13d 6c 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20  lCache; i++){.  
1f13e 20 20 69 6e 74 20 72 20 3d 20 70 50 61 72 73 65    int r = pParse
1f13f 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69  ->aColCache[i].i
1f140 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d  Reg;.    if( r>=
1f141 69 53 74 61 72 74 20 26 26 20 72 3c 3d 69 45 6e  iStart && r<=iEn
1f142 64 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  d ){.      pPars
1f143 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e  e->aColCache[i].
1f144 61 66 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20  affChange = 1;. 
1f145 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
1f146 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1f147 74 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20  to move content 
1f148 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69  from registers i
1f149 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65  From...iFrom+nRe
1f14a 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69  g-1.** over to i
1f14b 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20  To..iTo+nReg-1. 
1f14c 4b 65 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20  Keep the column 
1f14d 63 61 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65  cache up-to-date
1f14e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f14f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f150 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72  ExprCodeMove(Par
1f151 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1f152 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20  iFrom, int iTo, 
1f153 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74  int nReg){.  int
1f154 20 69 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d 3d   i;.  if( iFrom=
1f155 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20  =iTo ) return;. 
1f156 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f157 70 33 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  p3(pParse->pVdbe
1f158 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d  , OP_Move, iFrom
1f159 2c 20 69 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 20  , iTo, nReg);.  
1f15a 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73  for(i=0; i<pPars
1f15b 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b  e->nColCache; i+
1f15c 2b 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20  +){.    int x = 
1f15d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
1f15e 65 5b 69 5d 2e 69 52 65 67 3b 0a 20 20 20 20 69  e[i].iReg;.    i
1f15f 66 28 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78  f( x>=iFrom && x
1f160 3c 69 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20  <iFrom+nReg ){. 
1f161 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f       pParse->aCo
1f162 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 20 2b  lCache[i].iReg +
1f163 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20  = iTo-iFrom;.   
1f164 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
1f165 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1f166 20 63 6f 70 79 20 63 6f 6e 74 65 6e 74 20 66 72   copy content fr
1f167 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 72  om registers iFr
1f168 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d  om...iFrom+nReg-
1f169 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f  1.** over to iTo
1f16a 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f  ..iTo+nReg-1..*/
1f16b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f16c 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1f16d 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65 20 2a  CodeCopy(Parse *
1f16e 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f  pParse, int iFro
1f16f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20  m, int iTo, int 
1f170 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  nReg){.  int i;.
1f171 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f    if( iFrom==iTo
1f172 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
1f173 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b  (i=0; i<nReg; i+
1f174 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  +){.    sqlite3V
1f175 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65  dbeAddOp2(pParse
1f176 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79  ->pVdbe, OP_Copy
1f177 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69  , iFrom+i, iTo+i
1f178 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1f179 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61  Return true if a
1f17a 6e 79 20 72 65 67 69 73 74 65 72 20 69 6e 20 74  ny register in t
1f17b 68 65 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e  he range iFrom..
1f17c 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a  iTo (inclusive).
1f17d 2a 2a 20 69 73 20 75 73 65 64 20 61 73 20 70 61  ** is used as pa
1f17e 72 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  rt of the column
1f17f 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69   cache..*/.stati
1f180 63 20 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75  c int usedAsColu
1f181 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a 70  mnCache(Parse *p
1f182 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d  Parse, int iFrom
1f183 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e  , int iTo){.  in
1f184 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
1f185 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61  i<pParse->nColCa
1f186 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  che; i++){.    i
1f187 6e 74 20 72 20 3d 20 70 50 61 72 73 65 2d 3e 61  nt r = pParse->a
1f188 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67  ColCache[i].iReg
1f189 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72  ;.    if( r>=iFr
1f18a 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72  om && r<=iTo ) r
1f18b 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
1f18c 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1f18d 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 61 6c  * There is a val
1f18e 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 69  ue in register i
1f18f 52 65 67 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 72  Reg..**.** We ar
1f190 65 20 67 6f 69 6e 67 20 74 6f 20 6d 6f 64 69 66  e going to modif
1f191 79 20 74 68 65 20 76 61 6c 75 65 2c 20 73 6f 20  y the value, so 
1f192 77 65 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20  we need to make 
1f193 73 75 72 65 20 69 74 0a 2a 2a 20 69 73 20 6e 6f  sure it.** is no
1f194 74 20 61 20 63 61 63 68 65 64 20 72 65 67 69 73  t a cached regis
1f195 74 65 72 2e 20 20 49 66 20 69 52 65 67 20 69 73  ter.  If iReg is
1f196 20 61 20 63 61 63 68 65 64 20 72 65 67 69 73 74   a cached regist
1f197 65 72 2c 0a 2a 2a 20 74 68 65 6e 20 63 6c 65 61  er,.** then clea
1f198 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  r the correspond
1f199 69 6e 67 20 63 61 63 68 65 20 6c 69 6e 65 2e 0a  ing cache line..
1f19a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f19b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
1f19c 70 72 57 72 69 74 61 62 6c 65 52 65 67 69 73 74  prWritableRegist
1f19d 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  er(Parse *pParse
1f19e 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69  , int iReg){.  i
1f19f 6e 74 20 69 3b 0a 20 20 69 66 28 20 75 73 65 64  nt i;.  if( used
1f1a0 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50  AsColumnCache(pP
1f1a1 61 72 73 65 2c 20 69 52 65 67 2c 20 69 52 65 67  arse, iReg, iReg
1f1a2 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  ) ){.    for(i=0
1f1a3 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c  ; i<pParse->nCol
1f1a4 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  Cache; i++){.   
1f1a5 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61     if( pParse->a
1f1a6 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67  ColCache[i].iReg
1f1a7 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20  ==iReg ){.      
1f1a8 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61    pParse->aColCa
1f1a9 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d  che[i] = pParse-
1f1aa 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61  >aColCache[--pPa
1f1ab 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b  rse->nColCache];
1f1ac 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
1f1ad 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61  >iColCache = pPa
1f1ae 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a  rse->nColCache;.
1f1af 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1f1b0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  }.}../*.** If th
1f1b1 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69  e last instructi
1f1b2 6f 6e 20 63 6f 64 65 64 20 69 73 20 61 6e 20 65  on coded is an e
1f1b3 70 68 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66  phemeral copy of
1f1b4 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 72   any of.** the r
1f1b5 65 67 69 73 74 65 72 73 20 69 6e 20 74 68 65 20  egisters in the 
1f1b6 6e 52 65 67 20 72 65 67 69 73 74 65 72 73 20 62  nReg registers b
1f1b7 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 52  eginning with iR
1f1b8 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76  eg, then.** conv
1f1b9 65 72 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73  ert the last ins
1f1ba 74 72 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f 50  truction from OP
1f1bb 5f 53 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f 70  _SCopy to OP_Cop
1f1bc 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
1f1bd 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1f1be 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61  3ExprHardCopy(Pa
1f1bf 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
1f1c0 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29   iReg, int nReg)
1f1c1 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20  {.  int addr;.  
1f1c2 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 56  VdbeOp *pOp;.  V
1f1c3 64 62 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 70  dbe *v;..  v = p
1f1c4 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
1f1c5 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1f1c6 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
1f1c7 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65  ;.  pOp = sqlite
1f1c8 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 61 64  3VdbeGetOp(v, ad
1f1c9 64 72 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28  dr-1);.  assert(
1f1ca 20 70 4f 70 20 7c 7c 20 70 50 61 72 73 65 2d 3e   pOp || pParse->
1f1cb 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1f1cc 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 20 26 26   );.  if( pOp &&
1f1cd 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1f1ce 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70  _SCopy && pOp->p
1f1cf 31 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e  1>=iReg && pOp->
1f1d0 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a  p1<iReg+nReg ){.
1f1d1 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20      pOp->opcode 
1f1d2 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d  = OP_Copy;.  }.}
1f1d3 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1f1d4 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74   code to store t
1f1d5 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1f1d6 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20  iAlias-th alias 
1f1d7 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74  in register.** t
1f1d8 61 72 67 65 74 2e 20 20 54 68 65 20 66 69 72 73  arget.  The firs
1f1d9 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63  t time this is c
1f1da 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20  alled, pExpr is 
1f1db 65 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d  evaluated to com
1f1dc 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75  pute.** the valu
1f1dd 65 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20  e of the alias. 
1f1de 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74   The value is st
1f1df 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c  ored in an auxil
1f1e0 69 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a  iary register.**
1f1e1 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20   and the number 
1f1e2 6f 66 20 74 68 61 74 20 72 65 67 69 73 74 65 72  of that register
1f1e3 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f   is returned.  O
1f1e4 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  n subsequent cal
1f1e5 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73  ls,.** the regis
1f1e6 74 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65  ter number is re
1f1e7 74 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67  turned without g
1f1e8 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f  enerating any co
1f1e9 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  de..**.** Note t
1f1ea 68 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72  hat in order for
1f1eb 20 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63   this to work, c
1f1ec 6f 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65  ode must be gene
1f1ed 72 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  rated in the.** 
1f1ee 73 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20  same order that 
1f1ef 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a  it is executed..
1f1f0 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72  **.** Aliases ar
1f1f1 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74  e numbered start
1f1f2 69 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20  ing with 1.  So 
1f1f3 69 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65  iAlias is in the
1f1f4 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74   range.** of 1 t
1f1f5 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  o pParse->nAlias
1f1f6 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a   inclusive.  .**
1f1f7 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69  .** pParse->aAli
1f1f8 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63  as[iAlias-1] rec
1f1f9 6f 72 64 73 20 74 68 65 20 72 65 67 69 73 74 65  ords the registe
1f1fa 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74  r number where t
1f1fb 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74  he value.** of t
1f1fc 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69  he iAlias-th ali
1f1fd 61 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49  as is stored.  I
1f1fe 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61  f zero, that mea
1f1ff 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61  ns that the.** a
1f200 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74  lias has not yet
1f201 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a   been computed..
1f202 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
1f203 64 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70  deAlias(Parse *p
1f204 50 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61  Parse, int iAlia
1f205 73 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  s, Expr *pExpr, 
1f206 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 73  int target){.  s
1f207 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1f208 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
1f209 52 65 67 3b 0a 20 20 69 66 28 20 70 50 61 72 73  Reg;.  if( pPars
1f20a 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c 70  e->nAliasAlloc<p
1f20b 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 7b  Parse->nAlias ){
1f20c 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c  .    pParse->aAl
1f20d 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 52  ias = sqlite3DbR
1f20e 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62 2c  eallocOrFree(db,
1f20f 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 2c   pParse->aAlias,
1f210 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1f211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f212 20 20 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d    sizeof(pParse-
1f213 3e 61 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72  >aAlias[0])*pPar
1f214 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20  se->nAlias );.  
1f215 20 20 74 65 73 74 63 61 73 65 28 20 64 62 2d 3e    testcase( db->
1f216 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20  mallocFailed && 
1f217 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c  pParse->nAliasAl
1f218 6c 6f 63 3e 30 20 29 3b 0a 20 20 20 20 69 66 28  loc>0 );.    if(
1f219 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1f21a 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  d ) return 0;.  
1f21b 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65    memset(&pParse
1f21c 2d 3e 61 41 6c 69 61 73 5b 70 50 61 72 73 65 2d  ->aAlias[pParse-
1f21d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20 30  >nAliasAlloc], 0
1f21e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 70 50  ,.           (pP
1f21f 61 72 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 50 61  arse->nAlias-pPa
1f220 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63  rse->nAliasAlloc
1f221 29 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d  )*sizeof(pParse-
1f222 3e 61 41 6c 69 61 73 5b 30 5d 29 29 3b 0a 20 20  >aAlias[0]));.  
1f223 20 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73    pParse->nAlias
1f224 41 6c 6c 6f 63 20 3d 20 70 50 61 72 73 65 2d 3e  Alloc = pParse->
1f225 6e 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 61 73  nAlias;.  }.  as
1f226 73 65 72 74 28 20 69 41 6c 69 61 73 3e 30 20 26  sert( iAlias>0 &
1f227 26 20 69 41 6c 69 61 73 3c 3d 70 50 61 72 73 65  & iAlias<=pParse
1f228 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 69 52  ->nAlias );.  iR
1f229 65 67 20 3d 20 70 50 61 72 73 65 2d 3e 61 41 6c  eg = pParse->aAl
1f22a 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a 20  ias[iAlias-1];. 
1f22b 20 69 66 28 20 69 52 65 67 3d 3d 30 20 29 7b 0a   if( iReg==0 ){.
1f22c 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
1f22d 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 20  disableColCache 
1f22e 29 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20  ){.      iReg = 
1f22f 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1f230 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45  arget(pParse, pE
1f231 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  xpr, target);.  
1f232 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1f233 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Reg = ++pParse->
1f234 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69  nMem;.      sqli
1f235 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
1f236 73 65 2c 20 70 45 78 70 72 2c 20 69 52 65 67 29  se, pExpr, iReg)
1f237 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
1f238 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d  aAlias[iAlias-1]
1f239 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20   = iReg;.    }. 
1f23a 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67   }.  return iReg
1f23b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
1f23c 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68  ate code into th
1f23d 65 20 63 75 72 72 65 6e 74 20 56 64 62 65 20 74  e current Vdbe t
1f23e 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 67  o evaluate the g
1f23f 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  iven.** expressi
1f240 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20  on.  Attempt to 
1f241 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1f242 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 22 74  s in register "t
1f243 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65 74 75 72  arget"..** Retur
1f244 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 77  n the register w
1f245 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65  here results are
1f246 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57   stored..**.** W
1f247 69 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ith this routine
1f248 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 67 75  , there is no gu
1f249 61 72 61 6e 74 65 65 20 74 68 61 74 20 72 65 73  arantee that res
1f24a 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20  ults will.** be 
1f24b 73 74 6f 72 65 64 20 69 6e 20 74 61 72 67 65 74  stored in target
1f24c 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 69  .  The result mi
1f24d 67 68 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e  ght be stored in
1f24e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 72   some other.** r
1f24f 65 67 69 73 74 65 72 20 69 66 20 69 74 20 69 73  egister if it is
1f250 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64   convenient to d
1f251 6f 20 73 6f 2e 20 20 54 68 65 20 63 61 6c 6c 69  o so.  The calli
1f252 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d  ng function.** m
1f253 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65  ust check the re
1f254 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 6d 6f  turn code and mo
1f255 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 74  ve the results t
1f256 6f 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a  o the desired.**
1f257 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51   register..*/.SQ
1f258 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1f259 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1f25a 54 61 72 67 65 74 28 50 61 72 73 65 20 2a 70 50  Target(Parse *pP
1f25b 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
1f25c 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a  r, int target){.
1f25d 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
1f25e 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 54  se->pVdbe;  /* T
1f25f 68 65 20 56 4d 20 75 6e 64 65 72 20 63 6f 6e 73  he VM under cons
1f260 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  truction */.  in
1f261 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
1f262 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
1f263 70 63 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65  pcode being code
1f264 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 65 67  d */.  int inReg
1f265 20 3d 20 74 61 72 67 65 74 3b 20 20 20 20 20 20   = target;      
1f266 20 2f 2a 20 52 65 73 75 6c 74 73 20 73 74 6f 72   /* Results stor
1f267 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 69  ed in register i
1f268 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65  nReg */.  int re
1f269 67 46 72 65 65 31 20 3d 20 30 3b 20 20 20 20 20  gFree1 = 0;     
1f26a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65      /* If non-ze
1f26b 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d  ro free this tem
1f26c 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20  porary register 
1f26d 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65  */.  int regFree
1f26e 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  2 = 0;         /
1f26f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72  * If non-zero fr
1f270 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72  ee this temporar
1f271 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  y register */.  
1f272 69 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20  int r1, r2, r3, 
1f273 72 34 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72  r4;       /* Var
1f274 69 6f 75 73 20 72 65 67 69 73 74 65 72 20 6e 75  ious register nu
1f275 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69 74  mbers */.  sqlit
1f276 65 33 20 2a 64 62 3b 0a 0a 20 20 64 62 20 3d 20  e3 *db;..  db = 
1f277 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
1f278 73 65 72 74 28 20 76 21 3d 30 20 7c 7c 20 64 62  sert( v!=0 || db
1f279 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1f27a 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67  ;.  assert( targ
1f27b 65 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d  et>0 && target<=
1f27c 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a  pParse->nMem );.
1f27d 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
1f27e 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 45  urn 0;..  if( pE
1f27f 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70  xpr==0 ){.    op
1f280 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65   = TK_NULL;.  }e
1f281 6c 73 65 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45  lse{.    op = pE
1f282 78 70 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73  xpr->op;.  }.  s
1f283 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
1f284 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c   case TK_AGG_COL
1f285 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67  UMN: {.      Agg
1f286 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d  Info *pAggInfo =
1f287 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f   pExpr->pAggInfo
1f288 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 41  ;.      struct A
1f289 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c  ggInfo_col *pCol
1f28a 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43   = &pAggInfo->aC
1f28b 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 3b  ol[pExpr->iAgg];
1f28c 0a 20 20 20 20 20 20 69 66 28 20 21 70 41 67 67  .      if( !pAgg
1f28d 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65  Info->directMode
1f28e 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1f28f 72 74 28 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30  rt( pCol->iMem>0
1f290 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65   );.        inRe
1f291 67 20 3d 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a  g = pCol->iMem;.
1f292 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1f293 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
1f294 41 67 67 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74  AggInfo->useSort
1f295 69 6e 67 49 64 78 20 29 7b 0a 20 20 20 20 20 20  ingIdx ){.      
1f296 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f297 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
1f298 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73 6f 72 74  , pAggInfo->sort
1f299 69 6e 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20  ingIdx,.        
1f29a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f29b 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72        pCol->iSor
1f29c 74 65 72 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65  terColumn, targe
1f29d 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  t);.        brea
1f29e 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
1f29f 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66   /* Otherwise, f
1f2a0 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68  all thru into th
1f2a1 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73 65  e TK_COLUMN case
1f2a2 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   */.    }.    ca
1f2a3 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a  se TK_COLUMN: {.
1f2a4 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1f2a5 3e 69 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20  >iTable<0 ){.   
1f2a6 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c       /* This onl
1f2a7 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63  y happens when c
1f2a8 6f 64 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73  oding check cons
1f2a9 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20  traints */.     
1f2aa 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
1f2ab 65 2d 3e 63 6b 42 61 73 65 3e 30 20 29 3b 0a 20  e->ckBase>0 );. 
1f2ac 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70         inReg = p
1f2ad 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20  Expr->iColumn + 
1f2ae 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 3b 0a  pParse->ckBase;.
1f2af 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1f2b0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28       testcase( (
1f2b1 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45  pExpr->flags & E
1f2b2 50 5f 41 6e 79 41 66 66 29 21 3d 30 20 29 3b 0a  P_AnyAff)!=0 );.
1f2b3 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20          inReg = 
1f2b4 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47  sqlite3ExprCodeG
1f2b5 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  etColumn(pParse,
1f2b6 20 70 45 78 70 72 2d 3e 70 54 61 62 2c 0a 20 20   pExpr->pTab,.  
1f2b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f2b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1f2b9 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70  Expr->iColumn, p
1f2ba 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61  Expr->iTable, ta
1f2bb 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20  rget,.          
1f2bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f2bd 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c         pExpr->fl
1f2be 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29  ags & EP_AnyAff)
1f2bf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1f2c0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1f2c1 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52   case TK_INTEGER
1f2c2 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 65 49 6e  : {.      codeIn
1f2c3 74 65 67 65 72 28 76 2c 20 70 45 78 70 72 2c 20  teger(v, pExpr, 
1f2c4 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
1f2c5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1f2c6 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54     case TK_FLOAT
1f2c7 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 65 52 65  : {.      codeRe
1f2c8 61 6c 28 76 2c 20 28 63 68 61 72 2a 29 70 45 78  al(v, (char*)pEx
1f2c9 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78  pr->token.z, pEx
1f2ca 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 30 2c 20  pr->token.n, 0, 
1f2cb 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62  target);.      b
1f2cc 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1f2cd 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20  case TK_STRING: 
1f2ce 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
1f2cf 65 71 75 6f 74 65 45 78 70 72 28 70 45 78 70 72  equoteExpr(pExpr
1f2d0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f2d1 56 64 62 65 41 64 64 4f 70 34 28 76 2c 4f 50 5f  VdbeAddOp4(v,OP_
1f2d2 53 74 72 69 6e 67 38 2c 20 30 2c 20 74 61 72 67  String8, 0, targ
1f2d3 65 74 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  et, 0,.         
1f2d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1f2d5 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
1f2d6 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b  en.z, pExpr->tok
1f2d7 65 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 62 72 65  en.n);.      bre
1f2d8 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1f2d9 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20  se TK_NULL: {.  
1f2da 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f2db 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
1f2dc 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 0, target);.  
1f2dd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1f2de 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f2df 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
1f2e0 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c  L.    case TK_BL
1f2e1 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  OB: {.      int 
1f2e2 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  n;.      const c
1f2e3 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68  har *z;.      ch
1f2e4 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20  ar *zBlob;.     
1f2e5 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1f2e6 74 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20  token.n>=3 );.  
1f2e7 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
1f2e8 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27  r->token.z[0]=='
1f2e9 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 6b  x' || pExpr->tok
1f2ea 65 6e 2e 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a  en.z[0]=='X' );.
1f2eb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
1f2ec 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d  xpr->token.z[1]=
1f2ed 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 61  ='\'' );.      a
1f2ee 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f  ssert( pExpr->to
1f2ef 6b 65 6e 2e 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b  ken.z[pExpr->tok
1f2f0 65 6e 2e 6e 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b  en.n-1]=='\'' );
1f2f1 0a 20 20 20 20 20 20 6e 20 3d 20 70 45 78 70 72  .      n = pExpr
1f2f2 2d 3e 74 6f 6b 65 6e 2e 6e 20 2d 20 33 3b 0a 20  ->token.n - 3;. 
1f2f3 20 20 20 20 20 7a 20 3d 20 28 63 68 61 72 2a 29       z = (char*)
1f2f4 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b  pExpr->token.z +
1f2f5 20 32 3b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 20   2;.      zBlob 
1f2f6 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c  = sqlite3HexToBl
1f2f7 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65 44 62  ob(sqlite3VdbeDb
1f2f8 28 76 29 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20  (v), z, n);.    
1f2f9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f2fa 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20  Op4(v, OP_Blob, 
1f2fb 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30 2c 20  n/2, target, 0, 
1f2fc 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41 4d 49  zBlob, P4_DYNAMI
1f2fd 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  C);.      break;
1f2fe 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1f2ff 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42    case TK_VARIAB
1f300 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  LE: {.      int 
1f301 69 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 56 64  iPrior;.      Vd
1f302 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20  beOp *pOp;.     
1f303 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65   if( pExpr->toke
1f304 6e 2e 6e 3c 3d 31 0a 20 20 20 20 20 20 20 20 20  n.n<=1.         
1f305 26 26 20 28 69 50 72 69 6f 72 20 3d 20 73 71 6c  && (iPrior = sql
1f306 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
1f307 64 64 72 28 76 29 2d 31 29 3e 3d 30 0a 20 20 20  ddr(v)-1)>=0.   
1f308 20 20 20 20 20 20 26 26 20 28 70 4f 70 20 3d 20        && (pOp = 
1f309 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70  sqlite3VdbeGetOp
1f30a 28 76 2c 20 69 50 72 69 6f 72 29 29 2d 3e 6f 70  (v, iPrior))->op
1f30b 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c  code==OP_Variabl
1f30c 65 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f  e.         && pO
1f30d 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d 3d 70  p->p1+pOp->p3==p
1f30e 45 78 70 72 2d 3e 69 54 61 62 6c 65 0a 20 20 20  Expr->iTable.   
1f30f 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 32        && pOp->p2
1f310 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 67 65 74  +pOp->p3==target
1f311 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70  .         && pOp
1f312 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 20 20 20  ->p4.z==0.      
1f313 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
1f314 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e   the previous in
1f315 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 20  struction was a 
1f316 63 6f 70 79 20 6f 66 20 74 68 65 20 70 72 65 76  copy of the prev
1f317 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a 20 20 20  ious unnamed.   
1f318 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65       ** paramete
1f319 72 20 69 6e 74 6f 20 74 68 65 20 70 72 65 76 69  r into the previ
1f31a 6f 75 73 20 72 65 67 69 73 74 65 72 2c 20 74 68  ous register, th
1f31b 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63 72 65 6d  en simply increm
1f31c 65 6e 74 20 74 68 65 0a 20 20 20 20 20 20 20 20  ent the.        
1f31d 2a 2a 20 72 65 70 65 61 74 20 63 6f 75 6e 74 20  ** repeat count 
1f31e 6f 6e 20 74 68 65 20 70 72 69 6f 72 20 69 6e 73  on the prior ins
1f31f 74 72 75 63 74 69 6f 6e 20 72 61 74 68 65 72 20  truction rather 
1f320 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61 20 6e 65  than making a ne
1f321 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73  w.        ** ins
1f322 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20  truction..      
1f323 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 70    */.        pOp
1f324 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  ->p3++;.      }e
1f325 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
1f326 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1f327 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 70  , OP_Variable, p
1f328 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61  Expr->iTable, ta
1f329 72 67 65 74 2c 20 31 29 3b 0a 20 20 20 20 20 20  rget, 1);.      
1f32a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b    if( pExpr->tok
1f32b 65 6e 2e 6e 3e 31 20 29 7b 0a 20 20 20 20 20 20  en.n>1 ){.      
1f32c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1f32d 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28  hangeP4(v, -1, (
1f32e 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
1f32f 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b  en.z, pExpr->tok
1f330 65 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d  en.n);.        }
1f331 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1f332 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1f333 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52  case TK_REGISTER
1f334 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20  : {.      inReg 
1f335 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b  = pExpr->iTable;
1f336 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1f337 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1f338 41 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65  AS: {.      inRe
1f339 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 50  g = codeAlias(pP
1f33a 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 69 54 61  arse, pExpr->iTa
1f33b 62 6c 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  ble, pExpr->pLef
1f33c 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
1f33d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1f33e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f33f 49 54 5f 43 41 53 54 0a 20 20 20 20 63 61 73 65  IT_CAST.    case
1f340 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20 20 20 20   TK_CAST: {.    
1f341 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73    /* Expressions
1f342 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 20   of the form:   
1f343 43 41 53 54 28 70 4c 65 66 74 20 41 53 20 74 6f  CAST(pLeft AS to
1f344 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ken) */.      in
1f345 74 20 61 66 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20  t aff, to_op;.  
1f346 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69      inReg = sqli
1f347 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65  te3ExprCodeTarge
1f348 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  t(pParse, pExpr-
1f349 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b  >pLeft, target);
1f34a 0a 20 20 20 20 20 20 61 66 66 20 3d 20 73 71 6c  .      aff = sql
1f34b 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
1f34c 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b  (&pExpr->token);
1f34d 0a 20 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61  .      to_op = a
1f34e 66 66 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff - SQLITE_AFF_
1f34f 54 45 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74  TEXT + OP_ToText
1f350 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f351 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74  to_op==OP_ToText
1f352 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
1f353 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29  TE_AFF_TEXT    )
1f354 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f355 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62  to_op==OP_ToBlob
1f356 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
1f357 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29  TE_AFF_NONE    )
1f358 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f359 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65  to_op==OP_ToNume
1f35a 72 69 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49  ric || aff!=SQLI
1f35b 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29  TE_AFF_NUMERIC )
1f35c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f35d 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20  to_op==OP_ToInt 
1f35e 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
1f35f 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29  TE_AFF_INTEGER )
1f360 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f361 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c  to_op==OP_ToReal
1f362 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49      || aff!=SQLI
1f363 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29  TE_AFF_REAL    )
1f364 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f365 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65  ( to_op==OP_ToTe
1f366 78 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  xt );.      test
1f367 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  case( to_op==OP_
1f368 54 6f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20  ToBlob );.      
1f369 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d  testcase( to_op=
1f36a 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b  =OP_ToNumeric );
1f36b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1f36c 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74   to_op==OP_ToInt
1f36d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f36e 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  se( to_op==OP_To
1f36f 52 65 61 6c 20 29 3b 0a 20 20 20 20 20 20 69 66  Real );.      if
1f370 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20  ( inReg!=target 
1f371 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1f372 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1f373 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c  OP_SCopy, inReg,
1f374 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1f375 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74    inReg = target
1f376 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1f377 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f378 31 28 76 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65  1(v, to_op, inRe
1f379 67 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  g);.      testca
1f37a 73 65 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e  se( usedAsColumn
1f37b 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e  Cache(pParse, in
1f37c 52 65 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20  Reg, inReg) );. 
1f37d 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1f37e 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
1f37f 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65  nge(pParse, inRe
1f380 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65  g, 1);.      bre
1f381 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
1f382 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1f383 43 41 53 54 20 2a 2f 0a 20 20 20 20 63 61 73 65  CAST */.    case
1f384 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65   TK_LT:.    case
1f385 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65   TK_LE:.    case
1f386 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65   TK_GT:.    case
1f387 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65   TK_GE:.    case
1f388 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65   TK_NE:.    case
1f389 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20   TK_EQ: {.      
1f38a 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f  assert( TK_LT==O
1f38b 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Lt );.      as
1f38c 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f  sert( TK_LE==OP_
1f38d 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Le );.      asse
1f38e 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74  rt( TK_GT==OP_Gt
1f38f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1f390 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29  ( TK_GE==OP_Ge )
1f391 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f392 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a  TK_EQ==OP_Eq );.
1f393 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1f394 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20  _NE==OP_Ne );.  
1f395 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1f396 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20  ==TK_LT );.     
1f397 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1f398 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_LE );.      te
1f399 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
1f39a 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
1f39b 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29  ase( op==TK_GE )
1f39c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f39d 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20  ( op==TK_EQ );. 
1f39e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1f39f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20  p==TK_NE );.    
1f3a0 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
1f3a1 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
1f3a2 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
1f3a3 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
1f3a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f3a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1f3a6 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
1f3a7 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
1f3a8 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
1f3a9 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1f3aa 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
1f3ab 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
1f3ac 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
1f3ad 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45  2, inReg, SQLITE
1f3ae 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20  _STOREP2);.     
1f3af 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
1f3b0 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
1f3b1 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1f3b2 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e2==0 );.      b
1f3b3 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1f3b4 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20  case TK_AND:.   
1f3b5 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20   case TK_OR:.   
1f3b6 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20   case TK_PLUS:. 
1f3b7 20 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a     case TK_STAR:
1f3b8 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e  .    case TK_MIN
1f3b9 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  US:.    case TK_
1f3ba 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  REM:.    case TK
1f3bb 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 63 61 73  _BITAND:.    cas
1f3bc 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20  e TK_BITOR:.    
1f3bd 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20  case TK_SLASH:. 
1f3be 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 48 49 46     case TK_LSHIF
1f3bf 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52  T:.    case TK_R
1f3c0 53 48 49 46 54 3a 20 0a 20 20 20 20 63 61 73 65  SHIFT: .    case
1f3c1 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20   TK_CONCAT: {.  
1f3c2 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 41      assert( TK_A
1f3c3 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20  ND==OP_And );.  
1f3c4 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4f      assert( TK_O
1f3c5 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20  R==OP_Or );.    
1f3c6 20 20 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55    assert( TK_PLU
1f3c7 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20  S==OP_Add );.   
1f3c8 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49     assert( TK_MI
1f3c9 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74  NUS==OP_Subtract
1f3ca 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1f3cb 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d  ( TK_REM==OP_Rem
1f3cc 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 20 20 20  ainder );.      
1f3cd 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41 4e  assert( TK_BITAN
1f3ce 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a  D==OP_BitAnd );.
1f3cf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1f3d0 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72  _BITOR==OP_BitOr
1f3d1 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1f3d2 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44  ( TK_SLASH==OP_D
1f3d3 69 76 69 64 65 20 29 3b 0a 20 20 20 20 20 20 61  ivide );.      a
1f3d4 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 49 46 54  ssert( TK_LSHIFT
1f3d5 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29  ==OP_ShiftLeft )
1f3d6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f3d7 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68  TK_RSHIFT==OP_Sh
1f3d8 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20  iftRight );.    
1f3d9 20 20 61 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e    assert( TK_CON
1f3da 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29  CAT==OP_Concat )
1f3db 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f3dc 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a  ( op==TK_AND );.
1f3dd 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1f3de 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20  op==TK_OR );.   
1f3df 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1f3e0 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20  =TK_PLUS );.    
1f3e1 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1f3e2 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20  TK_MINUS );.    
1f3e3 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1f3e4 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20 20 20 20  TK_REM );.      
1f3e5 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1f3e6 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20 20 20 20  _BITAND );.     
1f3e7 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1f3e8 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20 20 20 20  K_BITOR );.     
1f3e9 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1f3ea 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20 20 20 20  K_SLASH );.     
1f3eb 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1f3ec 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20 20 20 20  K_LSHIFT );.    
1f3ed 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1f3ee 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a 20 20 20  TK_RSHIFT );.   
1f3ef 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1f3f0 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20  =TK_CONCAT );.  
1f3f1 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
1f3f2 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
1f3f3 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1f3f4 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
1f3f5 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
1f3f6 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1f3f7 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69  arse, pExpr->pRi
1f3f8 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b  ght, &regFree2);
1f3f9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1f3fa 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20  beAddOp3(v, op, 
1f3fb 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b  r2, r1, target);
1f3fc 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1f3fd 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
1f3fe 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1f3ff 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20  regFree2==0 );. 
1f400 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1f401 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d  }.    case TK_UM
1f402 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 45 78  INUS: {.      Ex
1f403 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70  pr *pLeft = pExp
1f404 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
1f405 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b  assert( pLeft );
1f406 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74  .      if( pLeft
1f407 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29  ->op==TK_FLOAT )
1f408 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 52 65  {.        codeRe
1f409 61 6c 28 76 2c 20 28 63 68 61 72 2a 29 70 4c 65  al(v, (char*)pLe
1f40a 66 74 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 4c 65  ft->token.z, pLe
1f40b 66 74 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 31 2c 20  ft->token.n, 1, 
1f40c 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d  target);.      }
1f40d 65 6c 73 65 20 69 66 28 20 70 4c 65 66 74 2d 3e  else if( pLeft->
1f40e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29  op==TK_INTEGER )
1f40f 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 49 6e  {.        codeIn
1f410 74 65 67 65 72 28 76 2c 20 70 4c 65 66 74 2c 20  teger(v, pLeft, 
1f411 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  1, target);.    
1f412 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1f413 20 72 65 67 46 72 65 65 31 20 3d 20 72 31 20 3d   regFree1 = r1 =
1f414 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
1f415 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
1f416 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f417 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
1f418 67 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20  ger, 0, r1);.   
1f419 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
1f41a 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1f41b 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
1f41c 66 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a  ft, &regFree2);.
1f41d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1f41e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1f41f 53 75 62 74 72 61 63 74 2c 20 72 32 2c 20 72 31  Subtract, r2, r1
1f420 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1f421 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1f422 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20  Free2==0 );.    
1f423 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67 20    }.      inReg 
1f424 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20  = target;.      
1f425 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1f426 20 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54 3a   case TK_BITNOT:
1f427 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
1f428 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
1f429 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50 5f  ( TK_BITNOT==OP_
1f42a 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20  BitNot );.      
1f42b 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d 3d  assert( TK_NOT==
1f42c 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20  OP_Not );.      
1f42d 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1f42e 5f 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20 20 20  _BITNOT );.     
1f42f 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1f430 4b 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 72  K_NOT );.      r
1f431 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
1f432 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
1f433 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
1f434 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
1f435 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1f436 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  e1==0 );.      i
1f437 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20  nReg = target;. 
1f438 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f439 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31  AddOp2(v, op, r1
1f43a 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20  , inReg);.      
1f43b 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1f43c 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a   case TK_ISNULL:
1f43d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
1f43e 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e  NULL: {.      in
1f43f 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61 73  t addr;.      as
1f440 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d  sert( TK_ISNULL=
1f441 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20  =OP_IsNull );.  
1f442 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e      assert( TK_N
1f443 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75  OTNULL==OP_NotNu
1f444 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ll );.      test
1f445 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e  case( op==TK_ISN
1f446 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ULL );.      tes
1f447 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f  tcase( op==TK_NO
1f448 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73  TNULL );.      s
1f449 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1f44a 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1f44b 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  1, target);.    
1f44c 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
1f44d 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
1f44e 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
1f44f 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20   &regFree1);.   
1f450 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1f451 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
1f452 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
1f453 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 6f 70  VdbeAddOp1(v, op
1f454 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
1f455 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1f456 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 61 72  , OP_AddImm, tar
1f457 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  get, -1);.      
1f458 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1f459 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20  ere(v, addr);.  
1f45a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1f45b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47  .    case TK_AGG
1f45c 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20  _FUNCTION: {.   
1f45d 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66     AggInfo *pInf
1f45e 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49  o = pExpr->pAggI
1f45f 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70  nfo;.      if( p
1f460 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Info==0 ){.     
1f461 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f462 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75  sg(pParse, "misu
1f463 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a  se of aggregate:
1f464 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %T",.          
1f465 20 20 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b    &pExpr->span);
1f466 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1f467 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 49        inReg = pI
1f468 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78 70 72  nfo->aFunc[pExpr
1f469 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a 20 20  ->iAgg].iMem;.  
1f46a 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
1f46b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1f46c 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a  e TK_CONST_FUNC:
1f46d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e  .    case TK_FUN
1f46e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45  CTION: {.      E
1f46f 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
1f470 20 28 0a 20 20 20 20 20 20 20 20 45 78 70 72 48   (.        ExprH
1f471 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
1f472 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  xpr, EP_TokenOnl
1f473 79 7c 45 50 5f 53 70 61 6e 4f 6e 6c 79 29 20 3f  y|EP_SpanOnly) ?
1f474 20 30 20 3a 20 70 45 78 70 72 2d 3e 78 2e 70 4c   0 : pExpr->x.pL
1f475 69 73 74 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ist.      );.   
1f476 20 20 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70     int nExpr = p
1f477 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45  List ? pList->nE
1f478 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 46  xpr : 0;.      F
1f479 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20  uncDef *pDef;.  
1f47a 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20      int nId;.   
1f47b 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1f47c 49 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f  Id;.      int co
1f47d 6e 73 74 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20  nstMask = 0;.   
1f47e 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
1f47f 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  u8 enc = ENC(db)
1f480 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  ;.      CollSeq 
1f481 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 0a 20 20 20  *pColl = 0;..   
1f482 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
1f483 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
1f484 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
1f485 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f486 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54  se( op==TK_CONST
1f487 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 20 20 74  _FUNC );.      t
1f488 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1f489 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20  FUNCTION );.    
1f48a 20 20 7a 49 64 20 3d 20 28 63 68 61 72 2a 29 70    zId = (char*)p
1f48b 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20  Expr->token.z;. 
1f48c 20 20 20 20 20 6e 49 64 20 3d 20 70 45 78 70 72       nId = pExpr
1f48d 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20  ->token.n;.     
1f48e 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46   pDef = sqlite3F
1f48f 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  indFunction(db, 
1f490 7a 49 64 2c 20 6e 49 64 2c 20 6e 45 78 70 72 2c  zId, nId, nExpr,
1f491 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20   enc, 0);.      
1f492 61 73 73 65 72 74 28 20 70 44 65 66 21 3d 30 20  assert( pDef!=0 
1f493 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69  );.      if( pLi
1f494 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 45  st ){.        nE
1f495 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78  xpr = pList->nEx
1f496 70 72 3b 0a 20 20 20 20 20 20 20 20 72 31 20 3d  pr;.        r1 =
1f497 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
1f498 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 45 78  ange(pParse, nEx
1f499 70 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  pr);.        sql
1f49a 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
1f49b 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4c 69  List(pParse, pLi
1f49c 73 74 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20  st, r1, 1);.    
1f49d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1f49e 20 6e 45 78 70 72 20 3d 20 72 31 20 3d 20 30 3b   nExpr = r1 = 0;
1f49f 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  .      }.#ifndef
1f4a0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1f4a1 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20  TUALTABLE.      
1f4a2 2f 2a 20 50 6f 73 73 69 62 6c 79 20 6f 76 65 72  /* Possibly over
1f4a3 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f  load the functio
1f4a4 6e 20 69 66 20 74 68 65 20 66 69 72 73 74 20 61  n if the first a
1f4a5 72 67 75 6d 65 6e 74 20 69 73 0a 20 20 20 20 20  rgument is.     
1f4a6 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 74 61   ** a virtual ta
1f4a7 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20  ble column..    
1f4a8 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f    **.      ** Fo
1f4a9 72 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e  r infix function
1f4aa 73 20 28 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52  s (LIKE, GLOB, R
1f4ab 45 47 45 58 50 2c 20 61 6e 64 20 4d 41 54 43 48  EGEXP, and MATCH
1f4ac 29 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20  ) use the.      
1f4ad 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  ** second argume
1f4ae 6e 74 2c 20 6e 6f 74 20 74 68 65 20 66 69 72 73  nt, not the firs
1f4af 74 2c 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  t, as the argume
1f4b0 6e 74 20 74 6f 20 74 65 73 74 20 74 6f 0a 20 20  nt to test to.  
1f4b1 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74      ** see if it
1f4b2 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   is a column in 
1f4b3 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
1f4b4 20 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62    This is done b
1f4b5 65 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20  ecause.      ** 
1f4b6 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  the left operand
1f4b7 20 6f 66 20 69 6e 66 69 78 20 66 75 6e 63 74 69   of infix functi
1f4b8 6f 6e 73 20 28 74 68 65 20 6f 70 65 72 61 6e 64  ons (the operand
1f4b9 20 77 65 20 77 61 6e 74 20 74 6f 0a 20 20 20 20   we want to.    
1f4ba 20 20 2a 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65    ** control ove
1f4bb 72 6c 6f 61 64 69 6e 67 29 20 65 6e 64 73 20 75  rloading) ends u
1f4bc 70 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  p as the second 
1f4bd 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a  argument to the.
1f4be 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f        ** functio
1f4bf 6e 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 69  n.  The expressi
1f4c0 6f 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 69 73  on "A glob B" is
1f4c1 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a   equivalent to .
1f4c2 20 20 20 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42        ** "glob(B
1f4c3 2c 41 29 2e 20 20 57 65 20 77 61 6e 74 20 74 6f  ,A).  We want to
1f4c4 20 75 73 65 20 74 68 65 20 41 20 69 6e 20 22 41   use the A in "A
1f4c5 20 67 6c 6f 62 20 42 22 20 74 6f 20 74 65 73 74   glob B" to test
1f4c6 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 66 75  .      ** for fu
1f4c7 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69  nction overloadi
1f4c8 6e 67 2e 20 20 42 75 74 20 77 65 20 75 73 65 20  ng.  But we use 
1f4c9 74 68 65 20 42 20 74 65 72 6d 20 69 6e 20 22 67  the B term in "g
1f4ca 6c 6f 62 28 42 2c 41 29 22 2e 0a 20 20 20 20 20  lob(B,A)"..     
1f4cb 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 45   */.      if( nE
1f4cc 78 70 72 3e 3d 32 20 26 26 20 28 70 45 78 70 72  xpr>=2 && (pExpr
1f4cd 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 66  ->flags & EP_Inf
1f4ce 69 78 46 75 6e 63 29 20 29 7b 0a 20 20 20 20 20  ixFunc) ){.     
1f4cf 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65     pDef = sqlite
1f4d0 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e  3VtabOverloadFun
1f4d1 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20  ction(db, pDef, 
1f4d2 6e 45 78 70 72 2c 20 70 4c 69 73 74 2d 3e 61 5b  nExpr, pList->a[
1f4d3 31 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  1].pExpr);.     
1f4d4 20 7d 65 6c 73 65 20 69 66 28 20 6e 45 78 70 72   }else if( nExpr
1f4d5 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  >0 ){.        pD
1f4d6 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62  ef = sqlite3Vtab
1f4d7 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e  OverloadFunction
1f4d8 28 64 62 2c 20 70 44 65 66 2c 20 6e 45 78 70 72  (db, pDef, nExpr
1f4d9 2c 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  , pList->a[0].pE
1f4da 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  xpr);.      }.#e
1f4db 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72 28 69  ndif.      for(i
1f4dc 3d 30 3b 20 69 3c 6e 45 78 70 72 20 26 26 20 69  =0; i<nExpr && i
1f4dd 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  <32; i++){.     
1f4de 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
1f4df 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 4c 69  prIsConstant(pLi
1f4e0 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20  st->a[i].pExpr) 
1f4e1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  ){.          con
1f4e2 73 74 4d 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29  stMask |= (1<<i)
1f4e3 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1f4e4 20 20 20 20 69 66 28 20 28 70 44 65 66 2d 3e 66      if( (pDef->f
1f4e5 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
1f4e6 4e 43 5f 4e 45 45 44 43 4f 4c 4c 29 21 3d 30 20  NC_NEEDCOLL)!=0 
1f4e7 26 26 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  && !pColl ){.   
1f4e8 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73         pColl = s
1f4e9 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
1f4ea 71 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d  q(pParse, pList-
1f4eb 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20  >a[i].pExpr);.  
1f4ec 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1f4ed 20 20 20 20 20 20 69 66 28 20 70 44 65 66 2d 3e        if( pDef->
1f4ee 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
1f4ef 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a  UNC_NEEDCOLL ){.
1f4f0 20 20 20 20 20 20 20 20 69 66 28 20 21 70 43 6f          if( !pCo
1f4f1 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d  ll ) pColl = db-
1f4f2 3e 70 44 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20  >pDfltColl; .   
1f4f3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f4f4 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp4(v, OP_Col
1f4f5 6c 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28  lSeq, 0, 0, 0, (
1f4f6 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34  char *)pColl, P4
1f4f7 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20  _COLLSEQ);.     
1f4f8 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1f4f9 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1f4fa 5f 46 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74  _Function, const
1f4fb 4d 61 73 6b 2c 20 72 31 2c 20 74 61 72 67 65 74  Mask, r1, target
1f4fc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1f4fd 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
1f4fe 29 70 44 65 66 2c 20 50 34 5f 46 55 4e 43 44 45  )pDef, P4_FUNCDE
1f4ff 46 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  F);.      sqlite
1f500 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
1f501 20 28 75 38 29 6e 45 78 70 72 29 3b 0a 20 20 20   (u8)nExpr);.   
1f502 20 20 20 69 66 28 20 6e 45 78 70 72 20 29 7b 0a     if( nExpr ){.
1f503 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1f504 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
1f505 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 45 78 70  pParse, r1, nExp
1f506 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
1f507 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
1f508 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
1f509 28 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 45 78  (pParse, r1, nEx
1f50a 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  pr);.      break
1f50b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
1f50c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1f50d 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b  UERY.    case TK
1f50e 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 73  _EXISTS:.    cas
1f50f 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20  e TK_SELECT: {. 
1f510 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1f511 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a  p==TK_EXISTS );.
1f512 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1f513 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b  op==TK_SELECT );
1f514 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72  .      if( pExpr
1f515 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a  ->iColumn==0 ){.
1f516 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
1f517 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 61  odeSubselect(pPa
1f518 72 73 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30  rse, pExpr, 0, 0
1f519 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1f51a 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e   inReg = pExpr->
1f51b 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 62  iColumn;.      b
1f51c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1f51d 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20  case TK_IN: {.  
1f51e 20 20 20 20 69 6e 74 20 72 4e 6f 74 46 6f 75 6e      int rNotFoun
1f51f 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74  d = 0;.      int
1f520 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20   rMayHaveNull = 
1f521 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 32 2c  0;.      int j2,
1f522 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20 20   j3, j4, j5;.   
1f523 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79     char affinity
1f524 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 54 79 70  ;.      int eTyp
1f525 65 3b 0a 0a 20 20 20 20 20 20 56 64 62 65 4e 6f  e;..      VdbeNo
1f526 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62  opComment((v, "b
1f527 65 67 69 6e 20 49 4e 20 65 78 70 72 20 72 25 64  egin IN expr r%d
1f528 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20 20  ", target));.   
1f529 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74     eType = sqlit
1f52a 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70 50  e3FindInIndex(pP
1f52b 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 4d  arse, pExpr, &rM
1f52c 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20  ayHaveNull);.   
1f52d 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e     if( rMayHaveN
1f52e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ull ){.        r
1f52f 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61  NotFound = ++pPa
1f530 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
1f531 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 67   }..      /* Fig
1f532 75 72 65 20 6f 75 74 20 74 68 65 20 61 66 66 69  ure out the affi
1f533 6e 69 74 79 20 74 6f 20 75 73 65 20 74 6f 20 63  nity to use to c
1f534 72 65 61 74 65 20 61 20 6b 65 79 20 66 72 6f 6d  reate a key from
1f535 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20   the results.   
1f536 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 78 70     ** of the exp
1f537 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 6e 69 74  ression. affinit
1f538 79 53 74 72 20 73 74 6f 72 65 73 20 61 20 73 74  yStr stores a st
1f539 61 74 69 63 20 73 74 72 69 6e 67 20 73 75 69 74  atic string suit
1f53a 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 20 20 2a  able for.      *
1f53b 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52  * P4 of OP_MakeR
1f53c 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  ecord..      */.
1f53d 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 3d        affinity =
1f53e 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e   comparisonAffin
1f53f 69 74 79 28 70 45 78 70 72 29 3b 0a 0a 0a 20 20  ity(pExpr);...  
1f540 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20      /* Code the 
1f541 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22 3c 65 78  <expr> from "<ex
1f542 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22 2e 20 54  pr> IN (...)". T
1f543 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
1f544 6c 65 0a 20 20 20 20 20 20 2a 2a 20 70 45 78 70  le.      ** pExp
1f545 72 2d 3e 69 54 61 62 6c 65 20 63 6f 6e 74 61 69  r->iTable contai
1f546 6e 73 20 74 68 65 20 76 61 6c 75 65 73 20 74 68  ns the values th
1f547 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 28  at make up the (
1f548 2e 2e 2e 29 20 73 65 74 2e 0a 20 20 20 20 20 20  ...) set..      
1f549 2a 2f 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  */.      pParse-
1f54a 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65  >disableColCache
1f54b 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ++;.      sqlite
1f54c 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
1f54d 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
1f54e 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 70  target);.      p
1f54f 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f  Parse->disableCo
1f550 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20 20  lCache--;.      
1f551 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
1f552 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e  AddOp1(v, OP_IsN
1f553 75 6c 6c 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  ull, target);.  
1f554 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49      if( eType==I
1f555 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b  N_INDEX_ROWID ){
1f556 0a 20 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71  .        j3 = sq
1f557 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
1f558 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
1f559 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1f55a 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64    j4 = sqlite3Vd
1f55b 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
1f55c 6f 74 45 78 69 73 74 73 2c 20 70 45 78 70 72 2d  otExists, pExpr-
1f55d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 74 61 72 67  >iTable, 0, targ
1f55e 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  et);.        sql
1f55f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1f560 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
1f561 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1f562 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64    j5 = sqlite3Vd
1f563 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
1f564 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 71  oto);.        sq
1f565 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1f566 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20  e(v, j3);.      
1f567 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1f568 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20  pHere(v, j4);.  
1f569 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f56a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
1f56b 74 65 67 65 72 2c 20 30 2c 20 74 61 72 67 65 74  teger, 0, target
1f56c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1f56d 20 20 20 20 20 20 20 20 72 32 20 3d 20 72 65 67          r2 = reg
1f56e 46 72 65 65 32 20 3d 20 73 71 6c 69 74 65 33 47  Free2 = sqlite3G
1f56f 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
1f570 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 43  );..        /* C
1f571 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 61  reate a record a
1f572 6e 64 20 74 65 73 74 20 66 6f 72 20 73 65 74 20  nd test for set 
1f573 6d 65 6d 62 65 72 73 68 69 70 2e 20 49 66 20 74  membership. If t
1f574 68 65 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 0a  he set contains.
1f575 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76          ** the v
1f576 61 6c 75 65 2c 20 74 68 65 6e 20 6a 75 6d 70 20  alue, then jump 
1f577 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
1f578 65 20 74 65 73 74 20 63 6f 64 65 2e 20 54 68 65  e test code. The
1f579 20 74 61 72 67 65 74 0a 20 20 20 20 20 20 20 20   target.        
1f57a 2a 2a 20 72 65 67 69 73 74 65 72 20 73 74 69 6c  ** register stil
1f57b 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  l contains the t
1f57c 72 75 65 20 28 31 29 20 76 61 6c 75 65 20 77 72  rue (1) value wr
1f57d 69 74 74 65 6e 20 74 6f 20 69 74 20 65 61 72 6c  itten to it earl
1f57e 69 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ier..        */.
1f57f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1f580 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1f581 4d 61 6b 65 52 65 63 6f 72 64 2c 20 74 61 72 67  MakeRecord, targ
1f582 65 74 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69  et, 1, r2, &affi
1f583 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
1f584 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f585 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
1f586 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20  r, 1, target);. 
1f587 20 20 20 20 20 20 20 6a 35 20 3d 20 73 71 6c 69         j5 = sqli
1f588 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1f589 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72   OP_Found, pExpr
1f58a 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 32 29  ->iTable, 0, r2)
1f58b 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ;..        /* If
1f58c 20 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73   the set members
1f58d 68 69 70 20 74 65 73 74 20 66 61 69 6c 73 2c 20  hip test fails, 
1f58e 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  then the result 
1f58f 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  of the .        
1f590 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e 29 22 20  ** "x IN (...)" 
1f591 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20  expression must 
1f592 62 65 20 65 69 74 68 65 72 20 30 20 6f 72 20 4e  be either 0 or N
1f593 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 65 74 0a  ULL. If the set.
1f594 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61          ** conta
1f595 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75  ins no NULL valu
1f596 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  es, then the res
1f597 75 6c 74 20 69 73 20 30 2e 20 49 66 20 74 68 65  ult is 0. If the
1f598 20 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a   set .        **
1f599 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72   contains one or
1f59a 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c 75 65   more NULL value
1f59b 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  s, then the resu
1f59c 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20  lt of the.      
1f59d 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
1f59e 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20  is also NULL..  
1f59f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1f5a0 20 69 66 28 20 72 4e 6f 74 46 6f 75 6e 64 3d 3d   if( rNotFound==
1f5a1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  0 ){.          /
1f5a2 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75  * This branch ru
1f5a3 6e 73 20 69 66 20 69 74 20 69 73 20 6b 6e 6f 77  ns if it is know
1f5a4 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d  n at compile tim
1f5a5 65 20 28 6e 6f 77 29 20 74 68 61 74 20 0a 20 20  e (now) that .  
1f5a6 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73          ** the s
1f5a7 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e  et contains no N
1f5a8 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54 68 69 73  ULL values. This
1f5a9 20 68 61 70 70 65 6e 73 20 61 73 20 74 68 65 20   happens as the 
1f5aa 72 65 73 75 6c 74 0a 20 20 20 20 20 20 20 20 20  result.         
1f5ab 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20 4e 55   ** of a "NOT NU
1f5ac 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  LL" constraint i
1f5ad 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
1f5ae 63 68 65 6d 61 2e 20 4e 6f 20 6e 65 65 64 0a 20  chema. No need. 
1f5af 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74           ** to t
1f5b0 65 73 74 20 74 68 65 20 64 61 74 61 20 73 74 72  est the data str
1f5b1 75 63 74 75 72 65 20 61 74 20 72 75 6e 74 69 6d  ucture at runtim
1f5b2 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  e in this case..
1f5b3 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
1f5b4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1f5b5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
1f5b6 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72 67 65  nteger, 0, targe
1f5b7 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  t);.        }els
1f5b8 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e{.          /* 
1f5b9 54 68 69 73 20 62 6c 6f 63 6b 20 70 6f 70 75 6c  This block popul
1f5ba 61 74 65 73 20 74 68 65 20 72 4e 6f 74 46 6f 75  ates the rNotFou
1f5bb 6e 64 20 72 65 67 69 73 74 65 72 20 77 69 74 68  nd register with
1f5bc 20 65 69 74 68 65 72 20 4e 55 4c 4c 0a 20 20 20   either NULL.   
1f5bd 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 30 20 28         ** or 0 (
1f5be 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  an integer value
1f5bf 29 2e 20 49 66 20 74 68 65 20 64 61 74 61 20 73  ). If the data s
1f5c0 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
1f5c1 73 20 6f 6e 65 0a 20 20 20 20 20 20 20 20 20 20  s one.          
1f5c2 2a 2a 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 73  ** or more NULLs
1f5c3 2c 20 74 68 65 6e 20 73 65 74 20 72 4e 6f 74 46  , then set rNotF
1f5c4 6f 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e 20 4f 74  ound to NULL. Ot
1f5c5 68 65 72 77 69 73 65 2c 20 73 65 74 20 69 74 0a  herwise, set it.
1f5c6 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20            ** to 
1f5c7 30 2e 20 49 66 20 72 65 67 69 73 74 65 72 20 72  0. If register r
1f5c8 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 61  MayHaveNull is a
1f5c9 6c 72 65 61 64 79 20 73 65 74 20 74 6f 20 73 6f  lready set to so
1f5ca 6d 65 20 76 61 6c 75 65 0a 20 20 20 20 20 20 20  me value.       
1f5cb 20 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e     ** other than
1f5cc 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20   NULL, then the 
1f5cd 74 65 73 74 20 68 61 73 20 61 6c 72 65 61 64 79  test has already
1f5ce 20 62 65 65 6e 20 72 75 6e 20 61 6e 64 20 0a 20   been run and . 
1f5cf 20 20 20 20 20 20 20 20 20 2a 2a 20 72 4e 6f 74           ** rNot
1f5d0 46 6f 75 6e 64 20 69 73 20 61 6c 72 65 61 64 79  Found is already
1f5d1 20 70 6f 70 75 6c 61 74 65 64 2e 0a 20 20 20 20   populated..    
1f5d2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1f5d3 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
1f5d4 63 68 61 72 20 6e 75 6c 6c 52 65 63 6f 72 64 5b  char nullRecord[
1f5d5 5d 20 3d 20 7b 20 30 78 30 32 2c 20 30 78 30 30  ] = { 0x02, 0x00
1f5d6 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 33   };.          j3
1f5d7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1f5d8 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75  dOp1(v, OP_NotNu
1f5d9 6c 6c 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c  ll, rMayHaveNull
1f5da 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1f5db 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1f5dc 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4e  , OP_Null, 0, rN
1f5dd 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20  otFound);.      
1f5de 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f5df 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62  ddOp4(v, OP_Blob
1f5e0 2c 20 32 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c  , 2, rMayHaveNul
1f5e1 6c 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20  l, 0, .         
1f5e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5e3 20 20 20 20 6e 75 6c 6c 52 65 63 6f 72 64 2c 20      nullRecord, 
1f5e4 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
1f5e5 20 20 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74        j4 = sqlit
1f5e6 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
1f5e7 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d  OP_Found, pExpr-
1f5e8 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 4d 61 79  >iTable, 0, rMay
1f5e9 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  HaveNull);.     
1f5ea 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5eb 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
1f5ec 65 67 65 72 2c 20 30 2c 20 72 4e 6f 74 46 6f 75  eger, 0, rNotFou
1f5ed 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  nd);.          s
1f5ee 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
1f5ef 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20  re(v, j4);.     
1f5f0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5f1 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b  JumpHere(v, j3);
1f5f2 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  ..          /* C
1f5f3 6f 70 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66  opy the value of
1f5f4 20 72 65 67 69 73 74 65 72 20 72 4e 6f 74 46 6f   register rNotFo
1f5f5 75 6e 64 20 28 77 68 69 63 68 20 69 73 20 65 69  und (which is ei
1f5f6 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 30 29 0a  ther NULL or 0).
1f5f7 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74            ** int
1f5f8 6f 20 74 68 65 20 74 61 72 67 65 74 20 72 65 67  o the target reg
1f5f9 69 73 74 65 72 2e 20 54 68 69 73 20 77 69 6c 6c  ister. This will
1f5fa 20 62 65 20 74 68 65 20 72 65 73 75 6c 74 20 6f   be the result o
1f5fb 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  f the.          
1f5fc 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20  ** expression.. 
1f5fd 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
1f5fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f5ff 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
1f600 70 79 2c 20 72 4e 6f 74 46 6f 75 6e 64 2c 20 74  py, rNotFound, t
1f601 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20  arget);.        
1f602 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
1f603 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1f604 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20  ere(v, j2);.    
1f605 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1f606 70 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20  pHere(v, j5);.  
1f607 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
1f608 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72  (v, "end IN expr
1f609 20 72 25 64 22 2c 20 74 61 72 67 65 74 29 29 3b   r%d", target));
1f60a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1f60b 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f    }.#endif.    /
1f60c 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 20 42 45  *.    **    x BE
1f60d 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20  TWEEN y AND z.  
1f60e 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
1f60f 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   is equivalent t
1f610 6f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  o.    **.    ** 
1f611 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a     x>=y AND x<=z
1f612 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58  .    **.    ** X
1f613 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45   is stored in pE
1f614 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20  xpr->pLeft..    
1f615 2a 2a 20 59 20 69 73 20 73 74 6f 72 65 64 20 69  ** Y is stored i
1f616 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  n pExpr->pList->
1f617 61 5b 30 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20  a[0].pExpr..    
1f618 2a 2a 20 5a 20 69 73 20 73 74 6f 72 65 64 20 69  ** Z is stored i
1f619 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  n pExpr->pList->
1f61a 61 5b 31 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20  a[1].pExpr..    
1f61b 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42  */.    case TK_B
1f61c 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20  ETWEEN: {.      
1f61d 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45  Expr *pLeft = pE
1f61e 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  xpr->pLeft;.    
1f61f 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1f620 74 5f 69 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d  t_item *pLItem =
1f621 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
1f622 3e 61 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  >a;.      Expr *
1f623 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d  pRight = pLItem-
1f624 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 20 63  >pExpr;..      c
1f625 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
1f626 64 73 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  ds(pParse, pLeft
1f627 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31  , &r1, &regFree1
1f628 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1f629 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f62a 20 20 20 20 70 52 69 67 68 74 2c 20 26 72 32 2c      pRight, &r2,
1f62b 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20   &regFree2);.   
1f62c 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
1f62d 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
1f62e 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
1f62f 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree2==0 );.     
1f630 20 72 33 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r3 = sqlite3Get
1f631 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
1f632 0a 20 20 20 20 20 20 72 34 20 3d 20 73 71 6c 69  .      r4 = sqli
1f633 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
1f634 61 72 73 65 29 3b 0a 20 20 20 20 20 20 63 6f 64  arse);.      cod
1f635 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c  eCompare(pParse,
1f636 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20   pLeft, pRight, 
1f637 4f 50 5f 47 65 2c 0a 20 20 20 20 20 20 20 20 20  OP_Ge,.         
1f638 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c           r1, r2,
1f639 20 72 33 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52   r3, SQLITE_STOR
1f63a 45 50 32 29 3b 0a 20 20 20 20 20 20 70 4c 49 74  EP2);.      pLIt
1f63b 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 70 52 69 67  em++;.      pRig
1f63c 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78  ht = pLItem->pEx
1f63d 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  pr;.      sqlite
1f63e 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1f63f 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32  pParse, regFree2
1f640 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71  );.      r2 = sq
1f641 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
1f642 70 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  p(pParse, pRight
1f643 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
1f644 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
1f645 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20  gFree2==0 );.   
1f646 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70     codeCompare(p
1f647 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52  Parse, pLeft, pR
1f648 69 67 68 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c  ight, OP_Le, r1,
1f649 20 72 32 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f   r2, r4, SQLITE_
1f64a 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20  STOREP2);.      
1f64b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f64c 33 28 76 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c  3(v, OP_And, r3,
1f64d 20 72 34 2c 20 74 61 72 67 65 74 29 3b 0a 20 20   r4, target);.  
1f64e 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
1f64f 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
1f650 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r3);.      sql
1f651 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
1f652 65 67 28 70 50 61 72 73 65 2c 20 72 34 29 3b 0a  eg(pParse, r4);.
1f653 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1f654 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55   }.    case TK_U
1f655 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e  PLUS: {.      in
1f656 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
1f657 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72  rCodeTarget(pPar
1f658 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
1f659 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1f65a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
1f65b 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f 72     /*.    ** For
1f65c 6d 20 41 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41  m A:.    **   CA
1f65d 53 45 20 78 20 57 48 45 4e 20 65 31 20 54 48 45  SE x WHEN e1 THE
1f65e 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45  N r1 WHEN e2 THE
1f65f 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e  N r2 ... WHEN eN
1f660 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20   THEN rN ELSE y 
1f661 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  END.    **.    *
1f662 2a 20 46 6f 72 6d 20 42 3a 0a 20 20 20 20 2a 2a  * Form B:.    **
1f663 20 20 20 43 41 53 45 20 57 48 45 4e 20 65 31 20     CASE WHEN e1 
1f664 54 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20  THEN r1 WHEN e2 
1f665 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e  THEN r2 ... WHEN
1f666 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45   eN THEN rN ELSE
1f667 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20   y END.    **.  
1f668 20 20 2a 2a 20 46 6f 72 6d 20 41 20 69 73 20 63    ** Form A is c
1f669 61 6e 20 62 65 20 74 72 61 6e 73 66 6f 72 6d 65  an be transforme
1f66a 64 20 69 6e 74 6f 20 74 68 65 20 65 71 75 69 76  d into the equiv
1f66b 61 6c 65 6e 74 20 66 6f 72 6d 20 42 20 61 73 20  alent form B as 
1f66c 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 20  follows:.    ** 
1f66d 20 20 43 41 53 45 20 57 48 45 4e 20 78 3d 65 31    CASE WHEN x=e1
1f66e 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 78 3d   THEN r1 WHEN x=
1f66f 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 0a 20  e2 THEN r2 .... 
1f670 20 20 20 2a 2a 20 20 20 20 20 20 20 20 57 48 45     **        WHE
1f671 4e 20 78 3d 65 4e 20 54 48 45 4e 20 72 4e 20 45  N x=eN THEN rN E
1f672 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a  LSE y END.    **
1f673 0a 20 20 20 20 2a 2a 20 58 20 28 69 66 20 69 74  .    ** X (if it
1f674 20 65 78 69 73 74 73 29 20 69 73 20 69 6e 20 70   exists) is in p
1f675 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20  Expr->pLeft..   
1f676 20 2a 2a 20 59 20 69 73 20 69 6e 20 70 45 78 70   ** Y is in pExp
1f677 72 2d 3e 70 52 69 67 68 74 2e 20 20 54 68 65 20  r->pRight.  The 
1f678 59 20 69 73 20 61 6c 73 6f 20 6f 70 74 69 6f 6e  Y is also option
1f679 61 6c 2e 20 20 49 66 20 74 68 65 72 65 20 69 73  al.  If there is
1f67a 20 6e 6f 0a 20 20 20 20 2a 2a 20 45 4c 53 45 20   no.    ** ELSE 
1f67b 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 20 6f 74  clause and no ot
1f67c 68 65 72 20 74 65 72 6d 20 6d 61 74 63 68 65 73  her term matches
1f67d 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  , then the resul
1f67e 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  t of the.    ** 
1f67f 65 78 70 72 73 73 69 6f 6e 20 69 73 20 4e 55 4c  exprssion is NUL
1f680 4c 2e 0a 20 20 20 20 2a 2a 20 45 69 20 69 73 20  L..    ** Ei is 
1f681 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d  in pExpr->pList-
1f682 3e 61 5b 69 2a 32 5d 20 61 6e 64 20 52 69 20 69  >a[i*2] and Ri i
1f683 73 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  s pExpr->pList->
1f684 61 5b 69 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a  a[i*2+1]..    **
1f685 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 75  .    ** The resu
1f686 6c 74 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  lt of the expres
1f687 73 69 6f 6e 20 69 73 20 74 68 65 20 52 69 20 66  sion is the Ri f
1f688 6f 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  or the first mat
1f689 63 68 69 6e 67 20 45 69 2c 0a 20 20 20 20 2a 2a  ching Ei,.    **
1f68a 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73 20   or if there is 
1f68b 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45 69 2c 20  no matching Ei, 
1f68c 74 68 65 20 45 4c 53 45 20 74 65 72 6d 20 59 2c  the ELSE term Y,
1f68d 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73 0a   or if there is.
1f68e 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53 45 20 74      ** no ELSE t
1f68f 65 72 6d 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a  erm, NULL..    *
1f690 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 41  /.    case TK_CA
1f691 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  SE: {.      int 
1f692 65 6e 64 4c 61 62 65 6c 3b 20 20 20 20 20 20 20  endLabel;       
1f693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f694 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20   GOTO label for 
1f695 65 6e 64 20 6f 66 20 43 41 53 45 20 73 74 6d 74  end of CASE stmt
1f696 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 65   */.      int ne
1f697 78 74 43 61 73 65 3b 20 20 20 20 20 20 20 20 20  xtCase;         
1f698 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
1f699 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65  OTO label for ne
1f69a 78 74 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a  xt WHEN clause *
1f69b 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70  /.      int nExp
1f69c 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1f69d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 78 20            /* 2x 
1f69e 6e 75 6d 62 65 72 20 6f 66 20 57 48 45 4e 20 74  number of WHEN t
1f69f 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e  erms */.      in
1f6a0 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
1f6a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6a2 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1f6a3 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73  */.      ExprLis
1f6a4 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20  t *pEList;      
1f6a5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69             /* Li
1f6a6 73 74 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73  st of WHEN terms
1f6a7 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74   */.      struct
1f6a8 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1f6a9 61 4c 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41  aListelem;  /* A
1f6aa 72 72 61 79 20 6f 66 20 57 48 45 4e 20 74 65 72  rray of WHEN ter
1f6ab 6d 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  ms */.      Expr
1f6ac 20 6f 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 20   opCompare;     
1f6ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f6ae 20 54 68 65 20 58 3d 3d 45 69 20 65 78 70 72 65   The X==Ei expre
1f6af 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45  ssion */.      E
1f6b0 78 70 72 20 63 61 63 68 65 58 3b 20 20 20 20 20  xpr cacheX;     
1f6b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6b2 20 2f 2a 20 43 61 63 68 65 64 20 65 78 70 72 65   /* Cached expre
1f6b3 73 73 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20  ssion X */.     
1f6b4 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20   Expr *pX;      
1f6b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6b6 20 20 20 2f 2a 20 54 68 65 20 58 20 65 78 70 72     /* The X expr
1f6b7 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  ession */.      
1f6b8 45 78 70 72 20 2a 70 54 65 73 74 20 3d 20 30 3b  Expr *pTest = 0;
1f6b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6ba 20 20 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d    /* X==Ei (form
1f6bb 20 41 29 20 6f 72 20 6a 75 73 74 20 45 69 20 28   A) or just Ei (
1f6bc 66 6f 72 6d 20 42 29 20 2a 2f 0a 0a 20 20 20 20  form B) */..    
1f6bd 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
1f6be 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
1f6bf 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
1f6c0 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  && pExpr->x.pLis
1f6c1 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
1f6c2 74 28 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  t((pExpr->x.pLis
1f6c3 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20 3d 3d  t->nExpr % 2) ==
1f6c4 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72   0);.      asser
1f6c5 74 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  t(pExpr->x.pList
1f6c6 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20 20  ->nExpr > 0);.  
1f6c7 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78      pEList = pEx
1f6c8 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20  pr->x.pList;.   
1f6c9 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20 70     aListelem = p
1f6ca 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20  EList->a;.      
1f6cb 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e  nExpr = pEList->
1f6cc 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 65 6e 64  nExpr;.      end
1f6cd 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 56  Label = sqlite3V
1f6ce 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1f6cf 0a 20 20 20 20 20 20 69 66 28 20 28 70 58 20 3d  .      if( (pX =
1f6d0 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 21 3d   pExpr->pLeft)!=
1f6d1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 63  0 ){.        cac
1f6d2 68 65 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20 20  heX = *pX;.     
1f6d3 20 20 20 74 65 73 74 63 61 73 65 28 20 70 58 2d     testcase( pX-
1f6d4 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c  >op==TK_COLUMN |
1f6d5 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47  | pX->op==TK_REG
1f6d6 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20  ISTER );.       
1f6d7 20 63 61 63 68 65 58 2e 69 54 61 62 6c 65 20 3d   cacheX.iTable =
1f6d8 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1f6d9 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2c  Temp(pParse, pX,
1f6da 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20   &regFree1);.   
1f6db 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1f6dc 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
1f6dd 20 20 20 20 20 20 63 61 63 68 65 58 2e 6f 70 20        cacheX.op 
1f6de 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20  = TK_REGISTER;. 
1f6df 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65         opCompare
1f6e0 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20  .op = TK_EQ;.   
1f6e1 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70       opCompare.p
1f6e2 4c 65 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0a  Left = &cacheX;.
1f6e3 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d 20          pTest = 
1f6e4 26 6f 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20  &opCompare;.    
1f6e5 20 20 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65    }.      pParse
1f6e6 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
1f6e7 65 2b 2b 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  e++;.      for(i
1f6e8 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69  =0; i<nExpr; i=i
1f6e9 2b 32 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  +2){.        if(
1f6ea 20 70 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20   pX ){.         
1f6eb 20 61 73 73 65 72 74 28 20 70 54 65 73 74 21 3d   assert( pTest!=
1f6ec 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f  0 );.          o
1f6ed 70 43 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20  pCompare.pRight 
1f6ee 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70  = aListelem[i].p
1f6ef 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65  Expr;.        }e
1f6f0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70  lse{.          p
1f6f1 54 65 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d  Test = aListelem
1f6f2 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
1f6f3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78     }.        nex
1f6f4 74 43 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56  tCase = sqlite3V
1f6f5 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
1f6f6 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
1f6f7 65 28 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b  e( pTest->op==TK
1f6f8 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 54 65 73 74  _COLUMN || pTest
1f6f9 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45  ->op==TK_REGISTE
1f6fa 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  R );.        sql
1f6fb 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
1f6fc 70 50 61 72 73 65 2c 20 70 54 65 73 74 2c 20 6e  pParse, pTest, n
1f6fd 65 78 74 43 61 73 65 2c 20 53 51 4c 49 54 45 5f  extCase, SQLITE_
1f6fe 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
1f6ff 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61       testcase( a
1f700 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45  Listelem[i+1].pE
1f701 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
1f702 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65  MN );.        te
1f703 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65  stcase( aListele
1f704 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70  m[i+1].pExpr->op
1f705 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b  ==TK_REGISTER );
1f706 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1f707 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
1f708 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e   aListelem[i+1].
1f709 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a  pExpr, target);.
1f70a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1f70b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1f70c 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62 65  Goto, 0, endLabe
1f70d 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l);.        sqli
1f70e 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
1f70f 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 73 65 29  bel(v, nextCase)
1f710 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1f711 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68  if( pExpr->pRigh
1f712 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
1f713 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1f714 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
1f715 68 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  ht, target);.   
1f716 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1f717 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f718 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
1f719 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
1f71a 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
1f71b 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1f71c 6c 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a  l(v, endLabel);.
1f71d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1f71e 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1f71f 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20  Cache>0 );.     
1f720 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
1f721 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20  ColCache--;.    
1f722 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1f723 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f724 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 63  IT_TRIGGER.    c
1f725 61 73 65 20 54 4b 5f 52 41 49 53 45 3a 20 7b 0a  ase TK_RAISE: {.
1f726 20 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73        if( !pPars
1f727 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a  e->trigStack ){.
1f728 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1f729 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a  rrorMsg(pParse,.
1f72a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f72b 20 20 20 20 20 20 20 22 52 41 49 53 45 28 29 20         "RAISE() 
1f72c 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  may only be used
1f72d 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65   within a trigge
1f72e 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20  r-program");.   
1f72f 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
1f730 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1f731 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
1f732 21 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20  !=OE_Ignore ){. 
1f733 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1f734 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d  pExpr->affinity=
1f735 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 0a  =OE_Rollback ||.
1f736 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f737 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
1f738 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 20 7c 7c 0a   == OE_Abort ||.
1f739 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f73a 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
1f73b 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 29 3b 0a 20   == OE_Fail );. 
1f73c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1f73d 65 71 75 6f 74 65 45 78 70 72 28 70 45 78 70 72  equoteExpr(pExpr
1f73e 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  );.         sqli
1f73f 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1f740 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
1f741 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 70 45 78  _CONSTRAINT, pEx
1f742 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c 20 30 2c  pr->affinity, 0,
1f743 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1f744 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
1f745 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20  pExpr->token.z, 
1f746 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b  pExpr->token.n);
1f747 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  .      } else {.
1f748 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
1f749 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
1f74a 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72 65 20 29 3b   == OE_Ignore );
1f74b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
1f74c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1f74d 50 5f 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30 2c  P_ContextPop, 0,
1f74e 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71   0);.         sq
1f74f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1f750 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70  v, OP_Goto, 0, p
1f751 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
1f752 2d 3e 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20  ->ignoreJump);. 
1f753 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
1f754 65 6e 74 28 28 76 2c 20 22 72 61 69 73 65 28 49  ent((v, "raise(I
1f755 47 4e 4f 52 45 29 22 29 29 3b 0a 20 20 20 20 20  GNORE)"));.     
1f756 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1f757 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
1f758 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
1f759 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
1f75a 20 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71   regFree1);.  sq
1f75b 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
1f75c 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46  Reg(pParse, regF
1f75d 72 65 65 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  ree2);.  return 
1f75e 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  inReg;.}../*.** 
1f75f 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1f760 20 65 76 61 6c 75 61 74 65 20 61 6e 20 65 78 70   evaluate an exp
1f761 72 65 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f 72  ression and stor
1f762 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a  e the results.**
1f763 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72   into a register
1f764 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65  .  Return the re
1f765 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68  gister number wh
1f766 65 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a  ere the results.
1f767 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a  ** are stored..*
1f768 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69  *.** If the regi
1f769 73 74 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72  ster is a tempor
1f76a 61 72 79 20 72 65 67 69 73 74 65 72 20 74 68 61  ary register tha
1f76b 74 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63  t can be dealloc
1f76c 61 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 72  ated,.** then wr
1f76d 69 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69  ite its number i
1f76e 6e 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74  nto *pReg.  If t
1f76f 68 65 20 72 65 73 75 6c 74 20 72 65 67 69 73 74  he result regist
1f770 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 74  er is not.** a t
1f771 65 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73  emporary, then s
1f772 65 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f  et *pReg to zero
1f773 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f774 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
1f775 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73  xprCodeTemp(Pars
1f776 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1f777 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 65  *pExpr, int *pRe
1f778 67 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73  g){.  int r1 = s
1f779 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
1f77a 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20  (pParse);.  int 
1f77b 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r2 = sqlite3Expr
1f77c 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
1f77d 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20  e, pExpr, r1);. 
1f77e 20 69 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a 20   if( r2==r1 ){. 
1f77f 20 20 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a 20     *pReg = r1;. 
1f780 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
1f781 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
1f782 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
1f783 20 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 20     *pReg = 0;.  
1f784 7d 0a 20 20 72 65 74 75 72 6e 20 72 32 3b 0a 7d  }.  return r2;.}
1f785 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1f786 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
1f787 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73  evaluate express
1f788 69 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74  ion pExpr and st
1f789 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c  ore the.** resul
1f78a 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 74  ts in register t
1f78b 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75  arget.  The resu
1f78c 6c 74 73 20 61 72 65 20 67 75 61 72 61 6e 74 65  lts are guarante
1f78d 65 64 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a 20  ed to appear.** 
1f78e 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67  in register targ
1f78f 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  et..*/.SQLITE_PR
1f790 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1f791 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 20  3ExprCode(Parse 
1f792 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1f793 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74  Expr, int target
1f794 29 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a  ){.  int inReg;.
1f795 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65  .  assert( targe
1f796 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70  t>0 && target<=p
1f797 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  Parse->nMem );. 
1f798 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
1f799 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
1f79a 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61  Parse, pExpr, ta
1f79b 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28  rget);.  assert(
1f79c 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 7c   pParse->pVdbe |
1f79d 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
1f79e 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
1f79f 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65  if( inReg!=targe
1f7a0 74 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64  t && pParse->pVd
1f7a1 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  be ){.    sqlite
1f7a2 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72  3VdbeAddOp2(pPar
1f7a3 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43  se->pVdbe, OP_SC
1f7a4 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67  opy, inReg, targ
1f7a5 65 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  et);.  }.  retur
1f7a6 6e 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a  n target;.}../*.
1f7a7 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1f7a8 20 74 68 61 74 20 65 76 61 6c 75 74 65 73 20 74   that evalutes t
1f7a9 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
1f7aa 69 6f 6e 20 61 6e 64 20 70 75 74 73 20 74 68 65  ion and puts the
1f7ab 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 65   result.** in re
1f7ac 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a  gister target..*
1f7ad 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61  *.** Also make a
1f7ae 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70   copy of the exp
1f7af 72 65 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 20  ression results 
1f7b0 69 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 61  into another "ca
1f7b1 63 68 65 22 20 72 65 67 69 73 74 65 72 0a 2a 2a  che" register.**
1f7b2 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20   and modify the 
1f7b3 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68  expression so th
1f7b4 61 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65  at the next time
1f7b5 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64   it is evaluated
1f7b6 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20  ,.** the result 
1f7b7 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  is a copy of the
1f7b8 20 63 61 63 68 65 20 72 65 67 69 73 74 65 72 2e   cache register.
1f7b9 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1f7ba 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ine is used for 
1f7bb 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74  expressions that
1f7bc 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69 70   are used multip
1f7bd 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 54  le .** times.  T
1f7be 68 65 79 20 61 72 65 20 65 76 61 6c 75 61 74 65  hey are evaluate
1f7bf 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 72  d once and the r
1f7c0 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78  esults of the ex
1f7c1 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 20  pression.** are 
1f7c2 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  reused..*/.SQLIT
1f7c3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1f7c4 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64  lite3ExprCodeAnd
1f7c5 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61  Cache(Parse *pPa
1f7c6 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
1f7c7 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20  , int target){. 
1f7c8 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
1f7c9 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
1f7ca 69 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 3d  inReg;.  inReg =
1f7cb 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1f7cc 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
1f7cd 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72  target);.  asser
1f7ce 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20  t( target>0 );. 
1f7cf 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
1f7d0 54 4b 5f 52 45 47 49 53 54 45 52 20 29 7b 20 20  TK_REGISTER ){  
1f7d1 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 20  .    int iMem;. 
1f7d2 20 20 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72     iMem = ++pPar
1f7d3 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
1f7d4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1f7d5 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 65  v, OP_Copy, inRe
1f7d6 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 45  g, iMem);.    pE
1f7d7 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 4d  xpr->iTable = iM
1f7d8 65 6d 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f  em;.    pExpr->o
1f7d9 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  p = TK_REGISTER;
1f7da 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e  .  }.  return in
1f7db 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  Reg;.}../*.** Re
1f7dc 74 75 72 6e 20 54 52 55 45 20 69 66 20 70 45 78  turn TRUE if pEx
1f7dd 70 72 20 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e  pr is an constan
1f7de 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  t expression tha
1f7df 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  t is appropriate
1f7e0 0a 2a 2a 20 66 6f 72 20 66 61 63 74 6f 72 69 6e  .** for factorin
1f7e1 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e  g out of a loop.
1f7e2 20 20 41 70 70 72 6f 70 72 69 61 74 65 20 65 78    Appropriate ex
1f7e3 70 72 65 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a  pressions are:.*
1f7e4 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 65  *.**    *  Any e
1f7e5 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65  xpression that e
1f7e6 76 61 6c 75 61 74 65 73 20 74 6f 20 74 77 6f 20  valuates to two 
1f7e7 6f 72 20 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e  or more opcodes.
1f7e8 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79  .**.**    *  Any
1f7e9 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f   OP_Integer, OP_
1f7ea 52 65 61 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c  Real, OP_String,
1f7eb 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c   OP_Blob, OP_Nul
1f7ec 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20  l, .**       or 
1f7ed 4f 50 5f 56 61 72 69 61 62 6c 65 20 74 68 61 74  OP_Variable that
1f7ee 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
1f7ef 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61  o be placed in a
1f7f0 20 0a 2a 2a 20 20 20 20 20 20 20 73 70 65 63 69   .**       speci
1f7f1 66 69 63 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a  fic register..**
1f7f2 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20  .** There is no 
1f7f3 70 6f 69 6e 74 20 69 6e 20 66 61 63 74 6f 72 69  point in factori
1f7f4 6e 67 20 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e  ng out single-in
1f7f5 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61  struction consta
1f7f6 6e 74 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  nt.** expression
1f7f7 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62  s that need to b
1f7f8 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 70 61  e placed in a pa
1f7f9 72 74 69 63 75 6c 61 72 20 72 65 67 69 73 74 65  rticular registe
1f7fa 72 2e 20 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64  r.  .** We could
1f7fb 20 66 61 63 74 6f 72 20 74 68 65 6d 20 6f 75 74   factor them out
1f7fc 2c 20 62 75 74 20 74 68 65 6e 20 77 65 20 77 6f  , but then we wo
1f7fd 75 6c 64 20 65 6e 64 20 75 70 20 61 64 64 69 6e  uld end up addin
1f7fe 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79  g an.** OP_SCopy
1f7ff 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20   instruction to 
1f800 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 69  move the value i
1f801 6e 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20  nto the correct 
1f802 72 65 67 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65  register.** late
1f803 72 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 20  r.  We might as 
1f804 77 65 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68  well just use th
1f805 65 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72  e original instr
1f806 75 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76  uction and.** av
1f807 6f 69 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79  oid the OP_SCopy
1f808 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1f809 69 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72  isAppropriateFor
1f80a 46 61 63 74 6f 72 69 6e 67 28 45 78 70 72 20 2a  Factoring(Expr *
1f80b 70 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74  p){.  if( !sqlit
1f80c 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
1f80d 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20  NotJoin(p) ){.  
1f80e 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20    return 0;  /* 
1f80f 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e 74 20 65 78  Only constant ex
1f810 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 61 70  pressions are ap
1f811 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 66 61  propriate for fa
1f812 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20  ctoring */.  }. 
1f813 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26   if( (p->flags &
1f814 20 45 50 5f 46 69 78 65 64 44 65 73 74 29 3d 3d   EP_FixedDest)==
1f815 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1f816 31 3b 20 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74  1;  /* Any const
1f817 61 6e 74 20 77 69 74 68 6f 75 74 20 61 20 66 69  ant without a fi
1f818 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  xed destination 
1f819 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a  is appropriate *
1f81a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70  /.  }.  while( p
1f81b 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29  ->op==TK_UPLUS )
1f81c 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20   p = p->pLeft;. 
1f81d 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
1f81e 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
1f81f 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52  _OMIT_BLOB_LITER
1f820 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42  AL.    case TK_B
1f821 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20  LOB:.#endif.    
1f822 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45  case TK_VARIABLE
1f823 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  :.    case TK_IN
1f824 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20  TEGER:.    case 
1f825 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61  TK_FLOAT:.    ca
1f826 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20  se TK_NULL:.    
1f827 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20  case TK_STRING: 
1f828 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
1f829 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42  ( p->op==TK_BLOB
1f82a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f82b 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41  se( p->op==TK_VA
1f82c 52 49 41 42 4c 45 20 29 3b 0a 20 20 20 20 20 20  RIABLE );.      
1f82d 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d  testcase( p->op=
1f82e 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20  =TK_INTEGER );. 
1f82f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
1f830 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29  ->op==TK_FLOAT )
1f831 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f832 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c  ( p->op==TK_NULL
1f833 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f834 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54  se( p->op==TK_ST
1f835 52 49 4e 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a  RING );.      /*
1f836 20 53 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74   Single-instruct
1f837 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 73 20 77 69  ion constants wi
1f838 74 68 20 61 20 66 69 78 65 64 20 64 65 73 74 69  th a fixed desti
1f839 6e 61 74 69 6f 6e 20 61 72 65 0a 20 20 20 20 20  nation are.     
1f83a 20 2a 2a 20 62 65 74 74 65 72 20 64 6f 6e 65 20   ** better done 
1f83b 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 65 20  in-line.  If we 
1f83c 66 61 63 74 6f 72 20 74 68 65 6d 2c 20 74 68 65  factor them, the
1f83d 79 20 77 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a  y will just end.
1f83e 20 20 20 20 20 20 2a 2a 20 75 70 20 67 65 6e 65        ** up gene
1f83f 72 61 74 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f  rating an OP_SCo
1f840 70 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76  py to move the v
1f841 61 6c 75 65 20 74 6f 20 74 68 65 20 64 65 73 74  alue to the dest
1f842 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  ination.      **
1f843 20 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20   register. */.  
1f844 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1f845 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1f846 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20  UMINUS: {.      
1f847 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f   if( p->pLeft->o
1f848 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70  p==TK_FLOAT || p
1f849 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ->pLeft->op==TK_
1f84a 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20  INTEGER ){.     
1f84b 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1f84c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 62 72       }.       br
1f84d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
1f84e 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
1f84f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
1f850 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
1f851 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69  /*.** If pExpr i
1f852 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70  s a constant exp
1f853 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20  ression that is 
1f854 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a  appropriate for.
1f855 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74  ** factoring out
1f856 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e   of a loop, then
1f857 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 65 78   evaluate the ex
1f858 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f  pression.** into
1f859 20 61 20 72 65 67 69 73 74 65 72 20 61 6e 64 20   a register and 
1f85a 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72  convert the expr
1f85b 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b  ession into a TK
1f85c 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70  _REGISTER.** exp
1f85d 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  ression..*/.stat
1f85e 69 63 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74  ic int evalConst
1f85f 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  Expr(Walker *pWa
1f860 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70  lker, Expr *pExp
1f861 72 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  r){.  Parse *pPa
1f862 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70  rse = pWalker->p
1f863 50 61 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28  Parse;.  switch(
1f864 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
1f865 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54    case TK_REGIST
1f866 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75  ER: {.      retu
1f867 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
1f868 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  case TK_FUNCTION
1f869 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  :.    case TK_AG
1f86a 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20  G_FUNCTION:.    
1f86b 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55  case TK_CONST_FU
1f86c 4e 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54  NC: {.      /* T
1f86d 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  he arguments to 
1f86e 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20  a function have 
1f86f 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74  a fixed destinat
1f870 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 61  ion..      ** Ma
1f871 72 6b 20 74 68 65 6d 20 74 68 69 73 20 77 61 79  rk them this way
1f872 20 74 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 61   to avoid genera
1f873 74 65 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 5f  ted unneeded OP_
1f874 53 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 69  SCopy.      ** i
1f875 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 20  nstructions. .  
1f876 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70      */.      Exp
1f877 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70  rList *pList = p
1f878 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20  Expr->x.pList;. 
1f879 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
1f87a 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
1f87b 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
1f87c 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  t) );.      if( 
1f87d 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20  pList ){.       
1f87e 20 69 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d 3e   int i = pList->
1f87f 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 73  nExpr;.        s
1f880 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
1f881 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 4c 69  tem *pItem = pLi
1f882 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66  st->a;.        f
1f883 6f 72 28 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70  or(; i>0; i--, p
1f884 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
1f885 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 45     if( pItem->pE
1f886 78 70 72 20 29 20 70 49 74 65 6d 2d 3e 70 45 78  xpr ) pItem->pEx
1f887 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  pr->flags |= EP_
1f888 46 69 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20  FixedDest;.     
1f889 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1f88a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1f88b 20 20 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72    }.  if( isAppr
1f88c 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72  opriateForFactor
1f88d 69 6e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20  ing(pExpr) ){.  
1f88e 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61    int r1 = ++pPa
1f88f 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69  rse->nMem;.    i
1f890 6e 74 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20  nt r2;.    r2 = 
1f891 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1f892 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45  arget(pParse, pE
1f893 78 70 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66  xpr, r1);.    if
1f894 28 20 72 31 21 3d 72 32 20 29 20 73 71 6c 69 74  ( r1!=r2 ) sqlit
1f895 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
1f896 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
1f897 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
1f898 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 70  _REGISTER;.    p
1f899 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 72  Expr->iTable = r
1f89a 32 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  2;.    return WR
1f89b 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 72  C_Prune;.  }.  r
1f89c 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
1f89d 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65  ue;.}../*.** Pre
1f89e 65 76 61 6c 75 61 74 65 20 63 6f 6e 73 74 61 6e  evaluate constan
1f89f 74 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  t subexpressions
1f8a0 20 77 69 74 68 69 6e 20 70 45 78 70 72 20 61 6e   within pExpr an
1f8a1 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72  d store the.** r
1f8a2 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74  esults in regist
1f8a3 65 72 73 2e 20 20 4d 6f 64 69 66 79 20 70 45 78  ers.  Modify pEx
1f8a4 70 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  pr so that the c
1f8a5 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65  onstant subexpre
1f8a6 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 54 4b 5f  sions.** are TK_
1f8a7 52 45 47 49 53 54 45 52 20 6f 70 63 6f 64 65 73  REGISTER opcodes
1f8a8 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
1f8a9 68 65 20 70 72 65 63 6f 6d 70 75 74 65 64 20 76  he precomputed v
1f8aa 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  alues..*/.SQLITE
1f8ab 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1f8ac 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e  lite3ExprCodeCon
1f8ad 73 74 61 6e 74 73 28 50 61 72 73 65 20 2a 70 50  stants(Parse *pP
1f8ae 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
1f8af 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a  r){.  Walker w;.
1f8b0 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
1f8b1 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78 70  k = evalConstExp
1f8b2 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  r;.  w.xSelectCa
1f8b3 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77 2e  llback = 0;.  w.
1f8b4 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
1f8b5 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  .  sqlite3WalkEx
1f8b6 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d  pr(&w, pExpr);.}
1f8b7 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  .../*.** Generat
1f8b8 65 20 63 6f 64 65 20 74 68 61 74 20 70 75 73 68  e code that push
1f8b9 65 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  es the value of 
1f8ba 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f 66  every element of
1f8bb 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78   the given.** ex
1f8bc 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 69 6e  pression list in
1f8bd 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  to a sequence of
1f8be 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e   registers begin
1f8bf 6e 69 6e 67 20 61 74 20 74 61 72 67 65 74 2e 0a  ning at target..
1f8c0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  **.** Return the
1f8c1 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   number of eleme
1f8c2 6e 74 73 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a  nts evaluated..*
1f8c3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f8c4 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
1f8c5 43 6f 64 65 45 78 70 72 4c 69 73 74 28 0a 20 20  CodeExprList(.  
1f8c6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1f8c7 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
1f8c8 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
1f8c9 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a  ist *pList,   /*
1f8ca 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
1f8cb 6c 69 73 74 20 74 6f 20 62 65 20 63 6f 64 65 64  list to be coded
1f8cc 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72 67 65 74   */.  int target
1f8cd 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72  ,        /* Wher
1f8ce 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c  e to write resul
1f8cf 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48 61  ts */.  int doHa
1f8d0 72 64 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d 61  rdCopy     /* Ma
1f8d1 6b 65 20 61 20 68 61 72 64 20 63 6f 70 79 20 6f  ke a hard copy o
1f8d2 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20  f every element 
1f8d3 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 45  */.){.  struct E
1f8d4 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
1f8d5 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  tem;.  int i, n;
1f8d6 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
1f8d7 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1f8d8 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 6e   target>0 );.  n
1f8d9 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
1f8da 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69  .  for(pItem=pLi
1f8db 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e 3b  st->a, i=0; i<n;
1f8dc 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
1f8dd 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
1f8de 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 69  Alias ){.      i
1f8df 6e 74 20 69 52 65 67 20 3d 20 63 6f 64 65 41 6c  nt iReg = codeAl
1f8e0 69 61 73 28 70 50 61 72 73 65 2c 20 70 49 74 65  ias(pParse, pIte
1f8e1 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49 74 65 6d  m->iAlias, pItem
1f8e2 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 2b  ->pExpr, target+
1f8e3 69 29 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a  i);.      Vdbe *
1f8e4 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1f8e5 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
1f8e6 20 20 69 66 28 20 69 52 65 67 21 3d 74 61 72 67    if( iReg!=targ
1f8e7 65 74 2b 69 20 29 7b 0a 20 20 20 20 20 20 20 20  et+i ){.        
1f8e8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f8e9 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69  2(v, OP_SCopy, i
1f8ea 52 65 67 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a  Reg, target+i);.
1f8eb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
1f8ec 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
1f8ed 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
1f8ee 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74   pItem->pExpr, t
1f8ef 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 7d 0a  arget+i);.    }.
1f8f0 20 20 20 20 69 66 28 20 64 6f 48 61 72 64 43 6f      if( doHardCo
1f8f1 70 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  py ){.      sqli
1f8f2 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28  te3ExprHardCopy(
1f8f3 70 50 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20  pParse, target, 
1f8f4 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  n);.    }.  }.  
1f8f5 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a  return n;.}../*.
1f8f6 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1f8f7 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65   for a boolean e
1f8f8 78 70 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74  xpression such t
1f8f9 68 61 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61  hat a jump is ma
1f8fa 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62  de.** to the lab
1f8fb 65 6c 20 22 64 65 73 74 22 20 69 66 20 74 68 65  el "dest" if the
1f8fc 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
1f8fd 72 75 65 20 62 75 74 20 65 78 65 63 75 74 69 6f  rue but executio
1f8fe 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73  n.** continues s
1f8ff 74 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20  traight thru if 
1f900 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
1f901 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  s false..**.** I
1f902 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1f903 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55   evaluates to NU
1f904 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72 75 65  LL (neither true
1f905 20 6e 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65   nor false), the
1f906 6e 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75  n.** take the ju
1f907 6d 70 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66  mp if the jumpIf
1f908 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 53 51 4c  Null flag is SQL
1f909 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a  ITE_JUMPIFNULL..
1f90a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20  **.** This code 
1f90b 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 66  depends on the f
1f90c 61 63 74 20 74 68 61 74 20 63 65 72 74 61 69 6e  act that certain
1f90d 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28 65   token values (e
1f90e 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65  x: TK_EQ).** are
1f90f 20 74 68 65 20 73 61 6d 65 20 61 73 20 6f 70 63   the same as opc
1f910 6f 64 65 20 76 61 6c 75 65 73 20 28 65 78 3a 20  ode values (ex: 
1f911 4f 50 5f 45 71 29 20 74 68 61 74 20 69 6d 70 6c  OP_Eq) that impl
1f912 65 6d 65 6e 74 20 74 68 65 20 63 6f 72 72 65 73  ement the corres
1f913 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61  ponding.** opera
1f914 74 69 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20 63  tion.  Special c
1f915 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64 62 65 2e  omments in vdbe.
1f916 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f  c and the mkopco
1f917 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 69  deh.awk script i
1f918 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70 72  n.** the make pr
1f919 6f 63 65 73 73 20 63 61 75 73 65 20 74 68 65 73  ocess cause thes
1f91a 65 20 76 61 6c 75 65 73 20 74 6f 20 61 6c 69 67  e values to alig
1f91b 6e 2e 20 20 41 73 73 65 72 74 28 29 73 20 69 6e  n.  Assert()s in
1f91c 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c   the code.** bel
1f91d 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 74  ow verify that t
1f91e 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61  he numbers are a
1f91f 6c 69 67 6e 65 64 20 63 6f 72 72 65 63 74 6c 79  ligned correctly
1f920 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f921 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f922 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73 65  ExprIfTrue(Parse
1f923 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
1f924 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c  pExpr, int dest,
1f925 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29   int jumpIfNull)
1f926 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
1f927 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
1f928 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74  nt op = 0;.  int
1f929 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20   regFree1 = 0;. 
1f92a 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20   int regFree2 = 
1f92b 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b  0;.  int r1, r2;
1f92c 0a 0a 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70  ..  assert( jump
1f92d 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a  IfNull==SQLITE_J
1f92e 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d  UMPIFNULL || jum
1f92f 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
1f930 69 66 28 20 76 3d 3d 30 20 7c 7c 20 70 45 78 70  if( v==0 || pExp
1f931 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
1f932 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b   op = pExpr->op;
1f933 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
1f934 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44  .    case TK_AND
1f935 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32  : {.      int d2
1f936 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
1f937 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
1f938 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
1f939 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
1f93a 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61     testcase( pPa
1f93b 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1f93c 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ache==0 );.     
1f93d 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
1f93e 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  lse(pParse, pExp
1f93f 72 2d 3e 70 4c 65 66 74 2c 20 64 32 2c 6a 75 6d  r->pLeft, d2,jum
1f940 70 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a  pIfNull^SQLITE_J
1f941 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
1f942 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c    pParse->disabl
1f943 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20  eColCache++;.   
1f944 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
1f945 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78  True(pParse, pEx
1f946 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74  pr->pRight, dest
1f947 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
1f948 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
1f949 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1f94a 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20  ache>0 );.      
1f94b 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1f94c 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20  olCache--;.     
1f94d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
1f94e 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b  lveLabel(v, d2);
1f94f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1f950 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1f951 4f 52 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  OR: {.      test
1f952 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
1f953 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
1f954 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e 64  tcase( pParse->d
1f955 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d  isableColCache==
1f956 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
1f957 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61  e3ExprIfTrue(pPa
1f958 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1f959 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
1f95a 75 6c 6c 29 3b 0a 20 20 20 20 20 20 70 50 61 72  ull);.      pPar
1f95b 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
1f95c 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c  che++;.      sql
1f95d 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
1f95e 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
1f95f 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  ight, dest, jump
1f960 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 61  IfNull);.      a
1f961 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64  ssert( pParse->d
1f962 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30  isableColCache>0
1f963 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65   );.      pParse
1f964 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
1f965 65 2d 2d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  e--;.      break
1f966 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1f967 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20   TK_NOT: {.     
1f968 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
1f969 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
1f96a 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
1f96b 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
1f96c 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c  pr->pLeft, dest,
1f96d 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
1f96e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1f96f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a  .    case TK_LT:
1f970 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a  .    case TK_LE:
1f971 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54 3a  .    case TK_GT:
1f972 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45 3a  .    case TK_GE:
1f973 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a  .    case TK_NE:
1f974 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a  .    case TK_EQ:
1f975 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
1f976 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b   TK_LT==OP_Lt );
1f977 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1f978 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20  K_LE==OP_Le );. 
1f979 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1f97a 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 20  GT==OP_Gt );.   
1f97b 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45     assert( TK_GE
1f97c 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20 20  ==OP_Ge );.     
1f97d 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d   assert( TK_EQ==
1f97e 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 20 61  OP_Eq );.      a
1f97f 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50  ssert( TK_NE==OP
1f980 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _Ne );.      tes
1f981 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54  tcase( op==TK_LT
1f982 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f983 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b  se( op==TK_LE );
1f984 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1f985 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20   op==TK_GT );.  
1f986 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1f987 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20  ==TK_GE );.     
1f988 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1f989 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65  K_EQ );.      te
1f98a 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e  stcase( op==TK_N
1f98b 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
1f98c 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
1f98d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  =0 );.      code
1f98e 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28  CompareOperands(
1f98f 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1f990 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46  Left, &r1, &regF
1f991 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20  ree1,.          
1f992 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f993 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1f994 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67  Right, &r2, &reg
1f995 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f  Free2);.      co
1f996 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65  deCompare(pParse
1f997 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
1f998 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f  pExpr->pRight, o
1f999 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  p,.             
1f99a 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73       r1, r2, des
1f99b 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
1f99c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1f99d 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
1f99e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1f99f 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20  egFree2==0 );.  
1f9a0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1f9a1 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e  .    case TK_ISN
1f9a2 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  ULL:.    case TK
1f9a3 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20  _NOTNULL: {.    
1f9a4 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e    assert( TK_ISN
1f9a5 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29  ULL==OP_IsNull )
1f9a6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1f9a7 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e  TK_NOTNULL==OP_N
1f9a8 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20  otNull );.      
1f9a9 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1f9aa 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  _ISNULL );.     
1f9ab 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1f9ac 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20  K_NOTNULL );.   
1f9ad 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
1f9ae 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
1f9af 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
1f9b0 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20  , &regFree1);.  
1f9b1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f9b2 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c  ddOp2(v, op, r1,
1f9b3 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65   dest);.      te
1f9b4 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
1f9b5 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
1f9b6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1f9b7 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b  se TK_BETWEEN: {
1f9b8 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42  .      /*    x B
1f9b9 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20  ETWEEN y AND z. 
1f9ba 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
1f9bb 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   Is equivalent t
1f9bc 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  o .      **.    
1f9bd 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44    **    x>=y AND
1f9be 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20   x<=z.      **. 
1f9bf 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20       ** Code it 
1f9c0 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20  as such, taking 
1f9c1 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63  care to do the c
1f9c2 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73  ommon subexpress
1f9c3 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65  ion.      ** ele
1f9c4 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a  mentation of x..
1f9c5 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45        */.      E
1f9c6 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20  xpr exprAnd;.   
1f9c7 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74     Expr compLeft
1f9c8 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d  ;.      Expr com
1f9c9 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78  pRight;.      Ex
1f9ca 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20  pr exprX;..     
1f9cb 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
1f9cc 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
1f9cd 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
1f9ce 3b 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20  ;.      exprX = 
1f9cf 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20  *pExpr->pLeft;. 
1f9d0 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20       exprAnd.op 
1f9d1 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20  = TK_AND;.      
1f9d2 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20  exprAnd.pLeft = 
1f9d3 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20  &compLeft;.     
1f9d4 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20   exprAnd.pRight 
1f9d5 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20  = &compRight;.  
1f9d6 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20      compLeft.op 
1f9d7 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63  = TK_GE;.      c
1f9d8 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20  ompLeft.pLeft = 
1f9d9 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f  &exprX;.      co
1f9da 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20  mpLeft.pRight = 
1f9db 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
1f9dc 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[0].pExpr;.    
1f9dd 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d    compRight.op =
1f9de 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f   TK_LE;.      co
1f9df 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20  mpRight.pLeft = 
1f9e0 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f  &exprX;.      co
1f9e1 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d  mpRight.pRight =
1f9e2 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
1f9e3 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[1].pExpr;.   
1f9e4 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20     exprX.iTable 
1f9e5 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
1f9e6 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65  eTemp(pParse, &e
1f9e7 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29  xprX, &regFree1)
1f9e8 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f9e9 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
1f9ea 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20  .      exprX.op 
1f9eb 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20  = TK_REGISTER;. 
1f9ec 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
1f9ed 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
1f9ee 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1f9ef 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
1f9f0 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20  &exprAnd, dest, 
1f9f1 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
1f9f2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1f9f3 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
1f9f4 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
1f9f5 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1f9f6 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65  arse, pExpr, &re
1f9f7 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73  gFree1);.      s
1f9f8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1f9f9 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64  (v, OP_If, r1, d
1f9fa 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21  est, jumpIfNull!
1f9fb 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  =0);.      testc
1f9fc 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
1f9fd 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f9fe 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
1f9ff 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
1fa00 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
1fa01 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
1fa02 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46  Reg(pParse, regF
1fa03 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  ree1);.  sqlite3
1fa04 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
1fa05 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29  Parse, regFree2)
1fa06 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  ;  .}../*.** Gen
1fa07 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
1fa08 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73   boolean express
1fa09 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20  ion such that a 
1fa0a 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20  jump is made.** 
1fa0b 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65  to the label "de
1fa0c 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65  st" if the expre
1fa0d 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62  ssion is false b
1fa0e 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20  ut execution.** 
1fa0f 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67  continues straig
1fa10 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65  ht thru if the e
1fa11 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75  xpression is tru
1fa12 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
1fa13 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75  expression evalu
1fa14 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65  ates to NULL (ne
1fa15 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66  ither true nor f
1fa16 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75  alse) then.** ju
1fa17 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c  mp if jumpIfNull
1fa18 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49   is SQLITE_JUMPI
1fa19 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68  FNULL or fall th
1fa1a 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49 66 4e  rough if jumpIfN
1fa1b 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a  ull.** is 0..*/.
1fa1c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1fa1d 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
1fa1e 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a 70 50  fFalse(Parse *pP
1fa1f 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
1fa20 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74  r, int dest, int
1fa21 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20   jumpIfNull){.  
1fa22 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
1fa23 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f  ->pVdbe;.  int o
1fa24 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67  p = 0;.  int reg
1fa25 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74  Free1 = 0;.  int
1fa26 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20   regFree2 = 0;. 
1fa27 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20   int r1, r2;..  
1fa28 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75  assert( jumpIfNu
1fa29 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ll==SQLITE_JUMPI
1fa2a 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e  FNULL || jumpIfN
1fa2b 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  ull==0 );.  if( 
1fa2c 76 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30  v==0 || pExpr==0
1fa2d 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a   ) return;..  /*
1fa2e 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45   The value of pE
1fa2f 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61  xpr->op and op a
1fa30 72 65 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f  re related as fo
1fa31 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  llows:.  **.  **
1fa32 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
1fa33 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20              op. 
1fa34 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d   **       ------
1fa35 2d 2d 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d  ---          ---
1fa36 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20  -------.  **    
1fa37 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20     TK_ISNULL    
1fa38 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c        OP_NotNull
1fa39 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e  .  **       TK_N
1fa3a 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f  OTNULL         O
1fa3b 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20  P_IsNull.  **   
1fa3c 20 20 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20      TK_NE       
1fa3d 20 20 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a         OP_Eq.  *
1fa3e 2a 20 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20  *       TK_EQ   
1fa3f 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65             OP_Ne
1fa40 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47  .  **       TK_G
1fa41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  T              O
1fa42 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Le.  **       
1fa43 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20  TK_LE           
1fa44 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20     OP_Gt.  **   
1fa45 20 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20      TK_GE       
1fa46 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a         OP_Lt.  *
1fa47 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20  *       TK_LT   
1fa48 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65             OP_Ge
1fa49 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f  .  **.  ** For o
1fa4a 74 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70  ther values of p
1fa4b 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20  Expr->op, op is 
1fa4c 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e  undefined and un
1fa4d 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76  used..  ** The v
1fa4e 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20  alue of TK_ and 
1fa4f 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  OP_ constants ar
1fa50 65 20 61 72 72 61 6e 67 65 64 20 73 75 63 68 20  e arranged such 
1fa51 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e  that we.  ** can
1fa52 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70   compute the map
1fa53 70 69 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67  ping above using
1fa54 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
1fa55 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20  xpression..  ** 
1fa56 41 73 73 65 72 74 28 29 73 20 76 65 72 69 66 79  Assert()s verify
1fa57 20 74 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74   that the comput
1fa58 61 74 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74  ation is correct
1fa59 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28  ..  */.  op = ((
1fa5a 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53  pExpr->op+(TK_IS
1fa5b 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f  NULL&1))^1)-(TK_
1fa5c 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a  ISNULL&1);..  /*
1fa5d 20 56 65 72 69 66 79 20 63 6f 72 72 65 63 74 20   Verify correct 
1fa5e 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f  alignment of TK_
1fa5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e   and OP_ constan
1fa60 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  ts.  */.  assert
1fa61 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
1fa62 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50  ISNULL || op==OP
1fa63 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73  _NotNull );.  as
1fa64 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
1fa65 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f  =TK_NOTNULL || o
1fa66 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a  p==OP_IsNull );.
1fa67 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1fa68 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70  >op!=TK_NE || op
1fa69 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73  ==OP_Eq );.  ass
1fa6a 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
1fa6b 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TK_EQ || op==OP_
1fa6c 4e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Ne );.  assert( 
1fa6d 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54  pExpr->op!=TK_LT
1fa6e 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b   || op==OP_Ge );
1fa6f 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
1fa70 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f  ->op!=TK_LE || o
1fa71 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73  p==OP_Gt );.  as
1fa72 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
1fa73 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50  =TK_GT || op==OP
1fa74 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _Le );.  assert(
1fa75 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47   pExpr->op!=TK_G
1fa76 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29  E || op==OP_Lt )
1fa77 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  ;..  switch( pEx
1fa78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61  pr->op ){.    ca
1fa79 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20  se TK_AND: {.   
1fa7a 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
1fa7b 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
1fa7c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50      testcase( pP
1fa7d 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1fa7e 43 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20  Cache==0 );.    
1fa7f 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
1fa80 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
1fa81 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c  pr->pLeft, dest,
1fa82 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
1fa83 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61      pParse->disa
1fa84 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20  bleColCache++;. 
1fa85 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1fa86 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
1fa87 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64  pExpr->pRight, d
1fa88 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
1fa89 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1fa8a 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1fa8b 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20  olCache>0 );.   
1fa8c 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62     pParse->disab
1fa8d 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20  leColCache--;.  
1fa8e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1fa8f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a  .    case TK_OR:
1fa90 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20   {.      int d2 
1fa91 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
1fa92 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
1fa93 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
1fa94 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
1fa95 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72    testcase( pPar
1fa96 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
1fa97 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  che==0 );.      
1fa98 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
1fa99 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
1fa9a 3e 70 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70  >pLeft, d2, jump
1fa9b 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55  IfNull^SQLITE_JU
1fa9c 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  MPIFNULL);.     
1fa9d 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
1fa9e 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20  ColCache++;.    
1fa9f 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
1faa0 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
1faa1 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74  pr->pRight, dest
1faa2 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
1faa3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
1faa4 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1faa5 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20  ache>0 );.      
1faa6 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1faa7 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20  olCache--;.     
1faa8 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
1faa9 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b  lveLabel(v, d2);
1faaa 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1faab 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1faac 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  NOT: {.      sql
1faad 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
1faae 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1faaf 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
1fab0 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72  fNull);.      br
1fab1 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1fab2 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63  ase TK_LT:.    c
1fab3 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63  ase TK_LE:.    c
1fab4 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63  ase TK_GT:.    c
1fab5 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63  ase TK_GE:.    c
1fab6 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63  ase TK_NE:.    c
1fab7 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20  ase TK_EQ: {.   
1fab8 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1fab9 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20  =TK_LT );.      
1faba 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1fabb 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _LE );.      tes
1fabc 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54  tcase( op==TK_GT
1fabd 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1fabe 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b  se( op==TK_GE );
1fabf 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fac0 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20   op==TK_EQ );.  
1fac1 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1fac2 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20  ==TK_NE );.     
1fac3 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
1fac4 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
1fac5 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
1fac6 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
1fac7 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
1fac8 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
1fac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1faca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1facb 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
1facc 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
1facd 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
1face 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1facf 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
1fad0 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
1fad1 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
1fad2 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  2, dest, jumpIfN
1fad3 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ull);.      test
1fad4 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1fad5 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
1fad6 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
1fad7 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
1fad8 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1fad9 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63  TK_ISNULL:.    c
1fada 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
1fadb 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
1fadc 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
1fadd 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1fade 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c  e( op==TK_NOTNUL
1fadf 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  L );.      r1 = 
1fae0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1fae1 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
1fae2 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
1fae3 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee1);.      sqli
1fae4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1fae5 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a   op, r1, dest);.
1fae6 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1fae7 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
1fae8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1fae9 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45  }.    case TK_BE
1faea 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f  TWEEN: {.      /
1faeb 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79  *    x BETWEEN y
1faec 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a   AND z.      **.
1faed 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 69        ** Is equi
1faee 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20  valent to .     
1faef 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   **.      **    
1faf0 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20  x>=y AND x<=z.  
1faf1 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
1faf2 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 2c  Code it as such,
1faf3 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f 20   taking care to 
1faf4 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75  do the common su
1faf5 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20  bexpression.    
1faf6 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f    ** elementatio
1faf7 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f  n of x..      */
1faf8 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72  .      Expr expr
1faf9 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 20  And;.      Expr 
1fafa 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  compLeft;.      
1fafb 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a  Expr compRight;.
1fafc 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 58        Expr exprX
1fafd 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
1fafe 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
1faff 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
1fb00 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20  elect) );.      
1fb01 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d 3e  exprX = *pExpr->
1fb02 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70  pLeft;.      exp
1fb03 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44  rAnd.op = TK_AND
1fb04 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e  ;.      exprAnd.
1fb05 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65 66  pLeft = &compLef
1fb06 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64  t;.      exprAnd
1fb07 2e 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70 52  .pRight = &compR
1fb08 69 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d 70  ight;.      comp
1fb09 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b  Left.op = TK_GE;
1fb0a 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e  .      compLeft.
1fb0b 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b 0a  pLeft = &exprX;.
1fb0c 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70        compLeft.p
1fb0d 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78  Right = pExpr->x
1fb0e 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  .pList->a[0].pEx
1fb0f 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69  pr;.      compRi
1fb10 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a  ght.op = TK_LE;.
1fb11 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e        compRight.
1fb12 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b 0a  pLeft = &exprX;.
1fb13 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e        compRight.
1fb14 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
1fb15 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  x.pList->a[1].pE
1fb16 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72 58  xpr;.      exprX
1fb17 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65  .iTable = sqlite
1fb18 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1fb19 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26 72  arse, &exprX, &r
1fb1a 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
1fb1b 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1fb1c 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65  e1==0 );.      e
1fb1d 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47  xprX.op = TK_REG
1fb1e 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65 73  ISTER;.      tes
1fb1f 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
1fb20 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  l==0 );.      sq
1fb21 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
1fb22 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e  (pParse, &exprAn
1fb23 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  d, dest, jumpIfN
1fb24 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ull);.      brea
1fb25 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
1fb26 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 31  ault: {.      r1
1fb27 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1fb28 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
1fb29 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31 29  Expr, &regFree1)
1fb2a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1fb2b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1fb2c 49 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73 74 2c  IfNot, r1, dest,
1fb2d 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b   jumpIfNull!=0);
1fb2e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fb2f 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
1fb30 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1fb31 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
1fb32 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1fb33 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
1fb34 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1fb35 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31  pParse, regFree1
1fb36 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
1fb37 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
1fb38 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 7d 0a  e, regFree2);.}.
1fb39 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65 65 70  ./*.** Do a deep
1fb3a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74   comparison of t
1fb3b 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  wo expression tr
1fb3c 65 65 73 2e 20 20 52 65 74 75 72 6e 20 54 52 55  ees.  Return TRU
1fb3d 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a 20  E (non-zero).** 
1fb3e 69 66 20 74 68 65 79 20 61 72 65 20 69 64 65 6e  if they are iden
1fb3f 74 69 63 61 6c 20 61 6e 64 20 72 65 74 75 72 6e  tical and return
1fb40 20 46 41 4c 53 45 20 69 66 20 74 68 65 79 20 64   FALSE if they d
1fb41 69 66 66 65 72 20 69 6e 20 61 6e 79 20 77 61 79  iffer in any way
1fb42 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  ..**.** Sometime
1fb43 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  s this routine w
1fb44 69 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c 53 45  ill return FALSE
1fb45 20 65 76 65 6e 20 69 66 20 74 68 65 20 74 77 6f   even if the two
1fb46 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20   expressions.** 
1fb47 72 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69 76  really are equiv
1fb48 61 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63 61  alent.  If we ca
1fb49 6e 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74 20  nnot prove that 
1fb4a 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
1fb4b 61 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c  are.** identical
1fb4c 2c 20 77 65 20 72 65 74 75 72 6e 20 46 41 4c 53  , we return FALS
1fb4d 45 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61 66  E just to be saf
1fb4e 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20 72  e.  So if this r
1fb4f 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e  outine.** return
1fb50 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 79 6f  s false, then yo
1fb51 75 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20  u do not really 
1fb52 6b 6e 6f 77 20 66 6f 72 20 63 65 72 74 61 69 6e  know for certain
1fb53 20 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a 20 65   if the two.** e
1fb54 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 74  xpressions are t
1fb55 68 65 20 73 61 6d 65 2e 20 20 42 75 74 20 69 66  he same.  But if
1fb56 20 79 6f 75 20 67 65 74 20 61 20 54 52 55 45 20   you get a TRUE 
1fb57 72 65 74 75 72 6e 2c 20 74 68 65 6e 20 79 6f 75  return, then you
1fb58 0a 2a 2a 20 63 61 6e 20 62 65 20 73 75 72 65 20  .** can be sure 
1fb59 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
1fb5a 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 49  are the same.  I
1fb5b 6e 20 74 68 65 20 70 6c 61 63 65 73 20 77 68 65  n the places whe
1fb5c 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  re.** this routi
1fb5d 6e 65 20 69 73 20 75 73 65 64 2c 20 69 74 20 64  ne is used, it d
1fb5e 6f 65 73 20 6e 6f 74 20 68 75 72 74 20 74 6f 20  oes not hurt to 
1fb5f 67 65 74 20 61 6e 20 65 78 74 72 61 20 46 41 4c  get an extra FAL
1fb60 53 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a 75 73  SE - that.** jus
1fb61 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69  t might result i
1fb62 6e 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c 79 20  n some slightly 
1fb63 73 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20 42 75  slower code.  Bu
1fb64 74 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 61  t returning.** a
1fb65 6e 20 69 6e 63 6f 72 72 65 63 74 20 54 52 55 45  n incorrect TRUE
1fb66 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 61   could lead to a
1fb67 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f   malfunction..*/
1fb68 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1fb69 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
1fb6a 6f 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41 2c  ompare(Expr *pA,
1fb6b 20 45 78 70 72 20 2a 70 42 29 7b 0a 20 20 69 6e   Expr *pB){.  in
1fb6c 74 20 69 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30  t i;.  if( pA==0
1fb6d 7c 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ||pB==0 ){.    r
1fb6e 65 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a 20 20  eturn pB==pA;.  
1fb6f 7d 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  }.  if( ExprHasP
1fb70 72 6f 70 65 72 74 79 28 70 41 2c 20 45 50 5f 78  roperty(pA, EP_x
1fb71 49 73 53 65 6c 65 63 74 29 20 7c 7c 20 45 78 70  IsSelect) || Exp
1fb72 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c  rHasProperty(pB,
1fb73 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
1fb74 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
1fb75 20 20 7d 0a 20 20 69 66 28 20 28 70 41 2d 3e 66    }.  if( (pA->f
1fb76 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e  lags & EP_Distin
1fb77 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20  ct)!=(pB->flags 
1fb78 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 20 29  & EP_Distinct) )
1fb79 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
1fb7a 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20   pA->op!=pB->op 
1fb7b 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
1fb7c 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f  ( !sqlite3ExprCo
1fb7d 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c  mpare(pA->pLeft,
1fb7e 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 20 72 65   pB->pLeft) ) re
1fb7f 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 73  turn 0;.  if( !s
1fb80 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
1fb81 65 28 70 41 2d 3e 70 52 69 67 68 74 2c 20 70 42  e(pA->pRight, pB
1fb82 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65 74 75  ->pRight) ) retu
1fb83 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d  rn 0;..  if( pA-
1fb84 3e 78 2e 70 4c 69 73 74 20 26 26 20 70 42 2d 3e  >x.pList && pB->
1fb85 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 69  x.pList ){.    i
1fb86 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e  f( pA->x.pList->
1fb87 6e 45 78 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69  nExpr!=pB->x.pLi
1fb88 73 74 2d 3e 6e 45 78 70 72 20 29 20 72 65 74 75  st->nExpr ) retu
1fb89 72 6e 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d  rn 0;.    for(i=
1fb8a 30 3b 20 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74  0; i<pA->x.pList
1fb8b 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
1fb8c 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
1fb8d 41 20 3d 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d  A = pA->x.pList-
1fb8e 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
1fb8f 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 42 20     Expr *pExprB 
1fb90 3d 20 70 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  = pB->x.pList->a
1fb91 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
1fb92 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70   if( !sqlite3Exp
1fb93 72 43 6f 6d 70 61 72 65 28 70 45 78 70 72 41 2c  rCompare(pExprA,
1fb94 20 70 45 78 70 72 42 29 20 29 20 72 65 74 75 72   pExprB) ) retur
1fb95 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  n 0;.    }.  }el
1fb96 73 65 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69  se if( pA->x.pLi
1fb97 73 74 20 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73  st || pB->x.pLis
1fb98 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
1fb99 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 41  0;.  }..  if( pA
1fb9a 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54  ->iTable!=pB->iT
1fb9b 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c  able || pA->iCol
1fb9c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e  umn!=pB->iColumn
1fb9d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
1fb9e 66 28 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f  f( pA->op!=TK_CO
1fb9f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e 74 6f 6b 65  LUMN && pA->toke
1fba0 6e 2e 7a 20 29 7b 0a 20 20 20 20 69 66 28 20 70  n.z ){.    if( p
1fba1 42 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29 20  B->token.z==0 ) 
1fba2 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66  return 0;.    if
1fba3 28 20 70 42 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70  ( pB->token.n!=p
1fba4 41 2d 3e 74 6f 6b 65 6e 2e 6e 20 29 20 72 65 74  A->token.n ) ret
1fba5 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 73  urn 0;.    if( s
1fba6 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28  qlite3StrNICmp((
1fba7 63 68 61 72 2a 29 70 41 2d 3e 74 6f 6b 65 6e 2e  char*)pA->token.
1fba8 7a 2c 28 63 68 61 72 2a 29 70 42 2d 3e 74 6f 6b  z,(char*)pB->tok
1fba9 65 6e 2e 7a 2c 70 42 2d 3e 74 6f 6b 65 6e 2e 6e  en.z,pB->token.n
1fbaa 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )!=0 ){.      re
1fbab 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
1fbac 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
1fbad 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
1fbae 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
1fbaf 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b   pAggInfo->aCol[
1fbb0 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e  ] array.  Return
1fbb1 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a   the index of.**
1fbb2 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74   the new element
1fbb3 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61  .  Return a nega
1fbb4 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d  tive number if m
1fbb5 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a  alloc fails..*/.
1fbb6 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 67  static int addAg
1fbb7 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 73 71 6c 69  gInfoColumn(sqli
1fbb8 74 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f  te3 *db, AggInfo
1fbb9 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20   *pInfo){.  int 
1fbba 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c  i;.  pInfo->aCol
1fbbb 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41   = sqlite3ArrayA
1fbbc 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20  llocate(.       
1fbbd 64 62 2c 0a 20 20 20 20 20 20 20 70 49 6e 66 6f  db,.       pInfo
1fbbe 2d 3e 61 43 6f 6c 2c 0a 20 20 20 20 20 20 20 73  ->aCol,.       s
1fbbf 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 43 6f  izeof(pInfo->aCo
1fbc0 6c 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c  l[0]),.       3,
1fbc1 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e  .       &pInfo->
1fbc2 6e 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20  nColumn,.       
1fbc3 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 41  &pInfo->nColumnA
1fbc4 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a  lloc,.       &i.
1fbc5 20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b    );.  return i;
1fbc6 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 41 64  .}    ../*.** Ad
1fbc7 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  d a new element 
1fbc8 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d  to the pAggInfo-
1fbc9 3e 61 46 75 6e 63 5b 5d 20 61 72 72 61 79 2e 20  >aFunc[] array. 
1fbca 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65   Return the inde
1fbcb 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20  x of.** the new 
1fbcc 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e  element.  Return
1fbcd 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
1fbce 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69  er if malloc fai
1fbcf 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ls..*/.static in
1fbd0 74 20 61 64 64 41 67 67 49 6e 66 6f 46 75 6e 63  t addAggInfoFunc
1fbd1 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67  (sqlite3 *db, Ag
1fbd2 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20  gInfo *pInfo){. 
1fbd3 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d   int i;.  pInfo-
1fbd4 3e 61 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33  >aFunc = sqlite3
1fbd5 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20  ArrayAllocate(. 
1fbd6 20 20 20 20 20 20 64 62 2c 20 0a 20 20 20 20 20        db, .     
1fbd7 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 0a    pInfo->aFunc,.
1fbd8 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49         sizeof(pI
1fbd9 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 29 2c 0a  nfo->aFunc[0]),.
1fbda 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20         3,.      
1fbdb 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2c 0a   &pInfo->nFunc,.
1fbdc 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e         &pInfo->n
1fbdd 46 75 6e 63 41 6c 6c 6f 63 2c 0a 20 20 20 20 20  FuncAlloc,.     
1fbde 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75    &i.  );.  retu
1fbdf 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a  rn i;.}    ../*.
1fbe0 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 78  ** This is the x
1fbe1 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 66 6f 72  ExprCallback for
1fbe2 20 61 20 74 72 65 65 20 77 61 6c 6b 65 72 2e 20   a tree walker. 
1fbe3 20 49 74 20 69 73 20 75 73 65 64 20 74 6f 0a 2a   It is used to.*
1fbe4 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 73 71 6c 69  * implement sqli
1fbe5 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
1fbe6 67 72 65 67 61 74 65 73 28 29 2e 20 20 53 65 65  gregates().  See
1fbe7 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
1fbe8 79 7a 65 41 67 67 72 65 67 61 74 65 73 0a 2a 2a  yzeAggregates.**
1fbe9 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
1fbea 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1fbeb 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61 6c 79  static int analy
1fbec 7a 65 41 67 67 72 65 67 61 74 65 28 57 61 6c 6b  zeAggregate(Walk
1fbed 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
1fbee 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
1fbef 20 69 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   i;.  NameContex
1fbf0 74 20 2a 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72  t *pNC = pWalker
1fbf1 2d 3e 75 2e 70 4e 43 3b 0a 20 20 50 61 72 73 65  ->u.pNC;.  Parse
1fbf2 20 2a 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e   *pParse = pNC->
1fbf3 70 50 61 72 73 65 3b 0a 20 20 53 72 63 4c 69 73  pParse;.  SrcLis
1fbf4 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e  t *pSrcList = pN
1fbf5 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 41  C->pSrcList;.  A
1fbf6 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
1fbf7 20 3d 20 70 4e 43 2d 3e 70 41 67 67 49 6e 66 6f   = pNC->pAggInfo
1fbf8 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  ;..  switch( pEx
1fbf9 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61  pr->op ){.    ca
1fbfa 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  se TK_AGG_COLUMN
1fbfb 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f  :.    case TK_CO
1fbfc 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65  LUMN: {.      te
1fbfd 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
1fbfe 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
1fbff 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1fc00 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
1fc01 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  K_COLUMN );.    
1fc02 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
1fc03 65 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e if the column 
1fc04 69 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65  is in one of the
1fc05 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
1fc06 52 4f 4d 0a 20 20 20 20 20 20 2a 2a 20 63 6c 61  ROM.      ** cla
1fc07 75 73 65 20 6f 66 20 74 68 65 20 61 67 67 72 65  use of the aggre
1fc08 67 61 74 65 20 71 75 65 72 79 20 2a 2f 0a 20 20  gate query */.  
1fc09 20 20 20 20 69 66 28 20 70 53 72 63 4c 69 73 74      if( pSrcList
1fc0a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75   ){.        stru
1fc0b 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
1fc0c 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73  *pItem = pSrcLis
1fc0d 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f  t->a;.        fo
1fc0e 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 4c 69 73  r(i=0; i<pSrcLis
1fc0f 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49  t->nSrc; i++, pI
1fc10 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
1fc11 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f    struct AggInfo
1fc12 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20 20  _col *pCol;.    
1fc13 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1fc14 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e  >iTable==pItem->
1fc15 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20  iCursor ){.     
1fc16 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20         /* If we 
1fc17 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74  reach this point
1fc18 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
1fc19 70 45 78 70 72 20 72 65 66 65 72 73 20 74 6f 20  pExpr refers to 
1fc1a 61 20 74 61 62 6c 65 0a 20 20 20 20 20 20 20 20  a table.        
1fc1b 20 20 20 20 2a 2a 20 74 68 61 74 20 69 73 20 69      ** that is i
1fc1c 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
1fc1d 65 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61  e of the aggrega
1fc1e 74 65 20 71 75 65 72 79 2e 20 20 0a 20 20 20 20  te query.  .    
1fc1f 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
1fc20 20 20 20 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61         ** Make a
1fc21 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  n entry for the 
1fc22 63 6f 6c 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e  column in pAggIn
1fc23 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68  fo->aCol[] if th
1fc24 65 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  ere.            
1fc25 2a 2a 20 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74  ** is not an ent
1fc26 72 79 20 74 68 65 72 65 20 61 6c 72 65 61 64 79  ry there already
1fc27 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  ..            */
1fc28 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
1fc29 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   k;.            
1fc2a 70 43 6f 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d  pCol = pAggInfo-
1fc2b 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20  >aCol;.         
1fc2c 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41     for(k=0; k<pA
1fc2d 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b  ggInfo->nColumn;
1fc2e 20 6b 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20   k++, pCol++){. 
1fc2f 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
1fc30 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70   pCol->iTable==p
1fc31 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a  Expr->iTable &&.
1fc32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc33 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d    pCol->iColumn=
1fc34 3d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20  =pExpr->iColumn 
1fc35 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1fc36 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1fc37 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1fc38 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fc39 20 20 20 20 69 66 28 20 28 6b 3e 3d 70 41 67 67      if( (k>=pAgg
1fc3a 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20  Info->nColumn). 
1fc3b 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
1fc3c 6b 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f  k = addAggInfoCo
1fc3d 6c 75 6d 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c  lumn(pParse->db,
1fc3e 20 70 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a   pAggInfo))>=0 .
1fc3f 20 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20              ){. 
1fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
1fc41 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61  l = &pAggInfo->a
1fc42 43 6f 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20  Col[k];.        
1fc43 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62        pCol->pTab
1fc44 20 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a   = pExpr->pTab;.
1fc45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
1fc46 6f 6c 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 78  ol->iTable = pEx
1fc47 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20  pr->iTable;.    
1fc48 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
1fc49 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d  iColumn = pExpr-
1fc4a 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
1fc4b 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d          pCol->iM
1fc4c 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
1fc4d 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Mem;.           
1fc4e 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72     pCol->iSorter
1fc4f 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20  Column = -1;.   
1fc50 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
1fc51 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a  >pExpr = pExpr;.
1fc52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
1fc53 28 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f  ( pAggInfo->pGro
1fc54 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20  upBy ){.        
1fc55 20 20 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e          int j, n
1fc56 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1fc57 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 42 20    ExprList *pGB 
1fc58 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f  = pAggInfo->pGro
1fc59 75 70 42 79 3b 0a 20 20 20 20 20 20 20 20 20 20  upBy;.          
1fc5a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70        struct Exp
1fc5b 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 65 72  rList_item *pTer
1fc5c 6d 20 3d 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20  m = pGB->a;.    
1fc5d 20 20 20 20 20 20 20 20 20 20 20 20 6e 20 3d 20              n = 
1fc5e 70 47 42 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  pGB->nExpr;.    
1fc5f 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28              for(
1fc60 6a 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70  j=0; j<n; j++, p
1fc61 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Term++){.       
1fc62 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20             Expr 
1fc63 2a 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  *pE = pTerm->pEx
1fc64 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr;.            
1fc65 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70        if( pE->op
1fc66 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70  ==TK_COLUMN && p
1fc67 45 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72  E->iTable==pExpr
1fc68 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20  ->iTable &&.    
1fc69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc6a 20 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70    pE->iColumn==p
1fc6b 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b  Expr->iColumn ){
1fc6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1fc6d 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74       pCol->iSort
1fc6e 65 72 43 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20  erColumn = j;.  
1fc6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc70 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1fc71 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1fc72 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
1fc73 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
1fc74 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
1fc75 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
1fc76 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20  lumn<0 ){.      
1fc77 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
1fc78 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20  iSorterColumn = 
1fc79 70 41 67 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69  pAggInfo->nSorti
1fc7a 6e 67 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20  ngColumn++;.    
1fc7b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1fc7c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1fc7d 20 20 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69        /* There i
1fc7e 73 20 6e 6f 77 20 61 6e 20 65 6e 74 72 79 20 66  s now an entry f
1fc7f 6f 72 20 70 45 78 70 72 20 69 6e 20 70 41 67 67  or pExpr in pAgg
1fc80 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69  Info->aCol[] (ei
1fc81 74 68 65 72 0a 20 20 20 20 20 20 20 20 20 20 20  ther.           
1fc82 20 2a 2a 20 62 65 63 61 75 73 65 20 69 74 20 77   ** because it w
1fc83 61 73 20 74 68 65 72 65 20 62 65 66 6f 72 65 20  as there before 
1fc84 6f 72 20 62 65 63 61 75 73 65 20 77 65 20 6a 75  or because we ju
1fc85 73 74 20 63 72 65 61 74 65 64 20 69 74 29 2e 0a  st created it)..
1fc86 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 43              ** C
1fc87 6f 6e 76 65 72 74 20 74 68 65 20 70 45 78 70 72  onvert the pExpr
1fc88 20 74 6f 20 62 65 20 61 20 54 4b 5f 41 47 47 5f   to be a TK_AGG_
1fc89 43 4f 4c 55 4d 4e 20 72 65 66 65 72 72 69 6e 67  COLUMN referring
1fc8a 20 74 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20   to that.       
1fc8b 20 20 20 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f       ** pAggInfo
1fc8c 2d 3e 61 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a  ->aCol[] entry..
1fc8d 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
1fc8e 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
1fc8f 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67  ->pAggInfo = pAg
1fc90 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 20  gInfo;.         
1fc91 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
1fc92 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20  K_AGG_COLUMN;.  
1fc93 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1fc94 3e 69 41 67 67 20 3d 20 6b 3b 0a 20 20 20 20 20  >iAgg = k;.     
1fc95 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1fc96 20 20 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64          } /* end
1fc97 69 66 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  if pExpr->iTable
1fc98 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  ==pItem->iCursor
1fc99 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 20 2f 2a   */.        } /*
1fc9a 20 65 6e 64 20 6c 6f 6f 70 20 6f 76 65 72 20 70   end loop over p
1fc9b 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 20 20 20  SrcList */.     
1fc9c 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
1fc9d 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d  WRC_Prune;.    }
1fc9e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47  .    case TK_AGG
1fc9f 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20  _FUNCTION: {.   
1fca0 20 20 20 2f 2a 20 54 68 65 20 70 4e 43 2d 3e 6e     /* The pNC->n
1fca1 44 65 70 74 68 3d 3d 30 20 74 65 73 74 20 63 61  Depth==0 test ca
1fca2 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66  uses aggregate f
1fca3 75 6e 63 74 69 6f 6e 73 20 69 6e 20 73 75 62 71  unctions in subq
1fca4 75 65 72 69 65 73 0a 20 20 20 20 20 20 2a 2a 20  ueries.      ** 
1fca5 74 6f 20 62 65 20 69 67 6e 6f 72 65 64 20 2a 2f  to be ignored */
1fca6 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e  .      if( pNC->
1fca7 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20  nDepth==0 ){.   
1fca8 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f       /* Check to
1fca9 20 73 65 65 20 69 66 20 70 45 78 70 72 20 69 73   see if pExpr is
1fcaa 20 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 20   a duplicate of 
1fcab 61 6e 6f 74 68 65 72 20 61 67 67 72 65 67 61 74  another aggregat
1fcac 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75  e .        ** fu
1fcad 6e 63 74 69 6f 6e 20 74 68 61 74 20 69 73 20 61  nction that is a
1fcae 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 70 41  lready in the pA
1fcaf 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ggInfo structure
1fcb0 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1fcb1 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e      struct AggIn
1fcb2 66 6f 5f 66 75 6e 63 20 2a 70 49 74 65 6d 20 3d  fo_func *pItem =
1fcb3 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63   pAggInfo->aFunc
1fcb4 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
1fcb5 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  0; i<pAggInfo->n
1fcb6 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Func; i++, pItem
1fcb7 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
1fcb8 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  f( sqlite3ExprCo
1fcb9 6d 70 61 72 65 28 70 49 74 65 6d 2d 3e 70 45 78  mpare(pItem->pEx
1fcba 70 72 2c 20 70 45 78 70 72 29 20 29 7b 0a 20 20  pr, pExpr) ){.  
1fcbb 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
1fcbc 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1fcbd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1fcbe 66 28 20 69 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e  f( i>=pAggInfo->
1fcbf 6e 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  nFunc ){.       
1fcc0 20 20 20 2f 2a 20 70 45 78 70 72 20 69 73 20 6f     /* pExpr is o
1fcc1 72 69 67 69 6e 61 6c 2e 20 20 4d 61 6b 65 20 61  riginal.  Make a
1fcc2 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 70 41   new entry in pA
1fcc3 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 0a  ggInfo->aFunc[].
1fcc4 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
1fcc5 20 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20         u8 enc = 
1fcc6 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b  ENC(pParse->db);
1fcc7 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20 61  .          i = a
1fcc8 64 64 41 67 67 49 6e 66 6f 46 75 6e 63 28 70 50  ddAggInfoFunc(pP
1fcc9 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e  arse->db, pAggIn
1fcca 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  fo);.          i
1fccb 66 28 20 69 3e 3d 30 20 29 7b 0a 20 20 20 20 20  f( i>=0 ){.     
1fccc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
1fccd 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1fcce 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
1fccf 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 20 20  ect) );.        
1fcd0 20 20 20 20 70 49 74 65 6d 20 3d 20 26 70 41 67      pItem = &pAg
1fcd1 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b  gInfo->aFunc[i];
1fcd2 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74  .            pIt
1fcd3 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70  em->pExpr = pExp
1fcd4 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  r;.            p
1fcd5 49 74 65 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70  Item->iMem = ++p
1fcd6 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
1fcd7 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
1fcd8 70 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 46  pFunc = sqlite3F
1fcd9 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  indFunction(pPar
1fcda 73 65 2d 3e 64 62 2c 0a 20 20 20 20 20 20 20 20  se->db,.        
1fcdb 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
1fcdc 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
1fcdd 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  , pExpr->token.n
1fcde 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1fcdf 20 20 20 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c       pExpr->x.pL
1fce0 69 73 74 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70  ist ? pExpr->x.p
1fce1 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c  List->nExpr : 0,
1fce2 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20   enc, 0);.      
1fce3 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1fce4 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74  >flags & EP_Dist
1fce5 69 6e 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  inct ){.        
1fce6 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44 69        pItem->iDi
1fce7 73 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d  stinct = pParse-
1fce8 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20  >nTab++;.       
1fce9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1fcea 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
1fceb 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b  >iDistinct = -1;
1fcec 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
1fced 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fcee 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
1fcef 4d 61 6b 65 20 70 45 78 70 72 20 70 6f 69 6e 74  Make pExpr point
1fcf0 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69   to the appropri
1fcf1 61 74 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46  ate pAggInfo->aF
1fcf2 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20  unc[] entry.    
1fcf3 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70      */.        p
1fcf4 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 69 3b 0a  Expr->iAgg = i;.
1fcf5 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1fcf6 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e  AggInfo = pAggIn
1fcf7 66 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  fo;.        retu
1fcf8 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
1fcf9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1fcfa 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e    return WRC_Con
1fcfb 74 69 6e 75 65 3b 0a 7d 0a 73 74 61 74 69 63 20  tinue;.}.static 
1fcfc 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65  int analyzeAggre
1fcfd 67 61 74 65 73 49 6e 53 65 6c 65 63 74 28 57 61  gatesInSelect(Wa
1fcfe 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
1fcff 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b  elect *pSelect){
1fd00 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  .  NameContext *
1fd01 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75  pNC = pWalker->u
1fd02 2e 70 4e 43 3b 0a 20 20 69 66 28 20 70 4e 43 2d  .pNC;.  if( pNC-
1fd03 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20  >nDepth==0 ){.  
1fd04 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b    pNC->nDepth++;
1fd05 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  .    sqlite3Walk
1fd06 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20  Select(pWalker, 
1fd07 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e  pSelect);.    pN
1fd08 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 20  C->nDepth--;.   
1fd09 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
1fd0a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
1fd0b 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
1fd0c 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  nue;.  }.}../*.*
1fd0d 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 67 69  * Analyze the gi
1fd0e 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  ven expression l
1fd0f 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 67 67 72 65  ooking for aggre
1fd10 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  gate functions a
1fd11 6e 64 0a 2a 2a 20 66 6f 72 20 76 61 72 69 61 62  nd.** for variab
1fd12 6c 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  les that need to
1fd13 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65   be added to the
1fd14 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20   pParse->aAgg[] 
1fd15 61 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61  array..** Make a
1fd16 64 64 69 74 69 6f 6e 61 6c 20 65 6e 74 72 69 65  dditional entrie
1fd17 73 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 2d  s to the pParse-
1fd18 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 20 61 73  >aAgg[] array as
1fd19 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a   necessary..**.*
1fd1a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
1fd1b 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61  hould only be ca
1fd1c 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 65  lled after the e
1fd1d 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 62 65  xpression has be
1fd1e 65 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 62  en.** analyzed b
1fd1f 79 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  y sqlite3Resolve
1fd20 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a  ExprNames()..*/.
1fd21 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1fd22 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
1fd23 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
1fd24 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e  (NameContext *pN
1fd25 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  C, Expr *pExpr){
1fd26 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77  .  Walker w;.  w
1fd27 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
1fd28 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74   analyzeAggregat
1fd29 65 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  e;.  w.xSelectCa
1fd2a 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65  llback = analyze
1fd2b 41 67 67 72 65 67 61 74 65 73 49 6e 53 65 6c 65  AggregatesInSele
1fd2c 63 74 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20  ct;.  w.u.pNC = 
1fd2d 70 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61  pNC;.  sqlite3Wa
1fd2e 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 72  lkExpr(&w, pExpr
1fd2f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  );.}../*.** Call
1fd30 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
1fd31 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 20  yzeAggregates() 
1fd32 66 6f 72 20 65 76 65 72 79 20 65 78 70 72 65 73  for every expres
1fd33 73 69 6f 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65 78  sion in an.** ex
1fd34 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20  pression list.  
1fd35 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1fd36 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a  r of errors..**.
1fd37 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69  ** If an error i
1fd38 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61  s found, the ana
1fd39 6c 79 73 69 73 20 69 73 20 63 75 74 20 73 68 6f  lysis is cut sho
1fd3a 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rt..*/.SQLITE_PR
1fd3b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1fd3c 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
1fd3d 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74  List(NameContext
1fd3e 20 2a 70 4e 43 2c 20 45 78 70 72 4c 69 73 74 20   *pNC, ExprList 
1fd3f 2a 70 4c 69 73 74 29 7b 0a 20 20 73 74 72 75 63  *pList){.  struc
1fd40 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
1fd41 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b  *pItem;.  int i;
1fd42 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a  .  if( pList ){.
1fd43 20 20 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c      for(pItem=pL
1fd44 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70  ist->a, i=0; i<p
1fd45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
1fd46 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
1fd47 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61    sqlite3ExprAna
1fd48 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 70  lyzeAggregates(p
1fd49 4e 43 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  NC, pItem->pExpr
1fd4a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
1fd4b 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f  /*.** Allocate o
1fd4c 72 20 64 65 61 6c 6c 6f 63 61 74 65 20 74 65 6d  r deallocate tem
1fd4d 70 6f 72 61 72 79 20 75 73 65 20 72 65 67 69 73  porary use regis
1fd4e 74 65 72 73 20 64 75 72 69 6e 67 20 63 6f 64 65  ters during code
1fd4f 20 67 65 6e 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a   generation..*/.
1fd50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1fd51 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d  nt sqlite3GetTem
1fd52 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72  pReg(Parse *pPar
1fd53 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73  se){.  if( pPars
1fd54 65 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29  e->nTempReg==0 )
1fd55 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b 70  {.    return ++p
1fd56 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d  Parse->nMem;.  }
1fd57 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65  .  return pParse
1fd58 2d 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70 50 61  ->aTempReg[--pPa
1fd59 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a  rse->nTempReg];.
1fd5a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
1fd5b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c   void sqlite3Rel
1fd5c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72 73  easeTempReg(Pars
1fd5d 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
1fd5e 52 65 67 29 7b 0a 20 20 69 66 28 20 69 52 65 67  Reg){.  if( iReg
1fd5f 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d   && pParse->nTem
1fd60 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70  pReg<ArraySize(p
1fd61 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29  Parse->aTempReg)
1fd62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1fd63 78 70 72 57 72 69 74 61 62 6c 65 52 65 67 69 73  xprWritableRegis
1fd64 74 65 72 28 70 50 61 72 73 65 2c 20 69 52 65 67  ter(pParse, iReg
1fd65 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61  );.    pParse->a
1fd66 54 65 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e  TempReg[pParse->
1fd67 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 69 52  nTempReg++] = iR
1fd68 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  eg;.  }.}../*.**
1fd69 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61   Allocate or dea
1fd6a 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20  llocate a block 
1fd6b 6f 66 20 6e 52 65 67 20 63 6f 6e 73 65 63 75 74  of nReg consecut
1fd6c 69 76 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2f  ive registers.*/
1fd6d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1fd6e 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65  int sqlite3GetTe
1fd6f 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70  mpRange(Parse *p
1fd70 50 61 72 73 65 2c 20 69 6e 74 20 6e 52 65 67 29  Parse, int nReg)
1fd71 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  {.  int i, n;.  
1fd72 69 20 3d 20 70 50 61 72 73 65 2d 3e 69 52 61 6e  i = pParse->iRan
1fd73 67 65 52 65 67 3b 0a 20 20 6e 20 3d 20 70 50 61  geReg;.  n = pPa
1fd74 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 3b 0a  rse->nRangeReg;.
1fd75 20 20 69 66 28 20 6e 52 65 67 3c 3d 6e 20 26 26    if( nReg<=n &&
1fd76 20 21 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61   !usedAsColumnCa
1fd77 63 68 65 28 70 50 61 72 73 65 2c 20 69 2c 20 69  che(pParse, i, i
1fd78 2b 6e 2d 31 29 20 29 7b 0a 20 20 20 20 70 50 61  +n-1) ){.    pPa
1fd79 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 2b  rse->iRangeReg +
1fd7a 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 61 72  = nReg;.    pPar
1fd7b 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 2d 3d  se->nRangeReg -=
1fd7c 20 6e 52 65 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a   nReg;.  }else{.
1fd7d 20 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d 3e      i = pParse->
1fd7e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72  nMem+1;.    pPar
1fd7f 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67  se->nMem += nReg
1fd80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  ;.  }.  return i
1fd81 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
1fd82 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
1fd83 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
1fd84 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
1fd85 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65  nt iReg, int nRe
1fd86 67 29 7b 0a 20 20 69 66 28 20 6e 52 65 67 3e 70  g){.  if( nReg>p
1fd87 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67  Parse->nRangeReg
1fd88 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
1fd89 6e 52 61 6e 67 65 52 65 67 20 3d 20 6e 52 65 67  nRangeReg = nReg
1fd8a 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52  ;.    pParse->iR
1fd8b 61 6e 67 65 52 65 67 20 3d 20 69 52 65 67 3b 0a  angeReg = iReg;.
1fd8c 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
1fd8d 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 65 78  ****** End of ex
1fd8e 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pr.c ***********
1fd8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fd91 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1fd92 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1fd93 65 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a  e alter.c ******
1fd94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fd95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fd96 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1fd97 35 20 46 65 62 72 75 61 72 79 20 31 35 0a 2a 2a  5 February 15.**
1fd98 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1fd99 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1fd9a 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1fd9b 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1fd9c 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1fd9d 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1fd9e 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1fd9f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1fda0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1fda1 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1fda2 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1fda3 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1fda4 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1fda5 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1fda6 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1fda7 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1fda8 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1fda9 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1fdaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1fdae 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1fdaf 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e  ns C code routin
1fdb0 65 73 20 74 68 61 74 20 75 73 65 64 20 74 6f 20  es that used to 
1fdb1 67 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f  generate VDBE co
1fdb2 64 65 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65  de.** that imple
1fdb3 6d 65 6e 74 73 20 74 68 65 20 41 4c 54 45 52 20  ments the ALTER 
1fdb4 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  TABLE command..*
1fdb5 2a 0a 2a 2a 20 24 49 64 3a 20 61 6c 74 65 72 2e  *.** $Id: alter.
1fdb6 63 2c 76 20 31 2e 35 35 20 32 30 30 39 2f 30 33  c,v 1.55 2009/03
1fdb7 2f 32 34 20 31 35 3a 30 38 3a 31 30 20 64 72 68  /24 15:08:10 drh
1fdb8 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
1fdb9 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
1fdba 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69 73  s file only exis
1fdbb 74 73 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74  ts if we are not
1fdbc 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a   omitting the.**
1fdbd 20 41 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f 67   ALTER TABLE log
1fdbe 69 63 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c  ic from the buil
1fdbf 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
1fdc0 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54  LITE_OMIT_ALTERT
1fdc1 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ABLE.../*.** Thi
1fdc2 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
1fdc3 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61  ed by SQL genera
1fdc4 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ted to implement
1fdc5 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45 52 20 54   the .** ALTER T
1fdc6 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68  ABLE command. Th
1fdc7 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
1fdc8 20 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20   is the text of 
1fdc9 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f  a CREATE TABLE o
1fdca 72 0a 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45  r.** CREATE INDE
1fdcb 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 73  X command. The s
1fdcc 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c 65  econd is a table
1fdcd 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c 65   name. The table
1fdce 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 74 68 65   name in .** the
1fdcf 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72   CREATE TABLE or
1fdd0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74   CREATE INDEX st
1fdd1 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c 61  atement is repla
1fdd2 63 65 64 20 77 69 74 68 20 74 68 65 20 74 68 69  ced with the thi
1fdd3 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61  rd.** argument a
1fdd4 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 72 65  nd the result re
1fdd5 74 75 72 6e 65 64 2e 20 45 78 61 6d 70 6c 65 73  turned. Examples
1fdd6 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72  :.**.** sqlite_r
1fdd7 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52 45  ename_table('CRE
1fdd8 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 2c  ATE TABLE abc(a,
1fdd9 20 62 2c 20 63 29 27 2c 20 27 64 65 66 27 29 0a   b, c)', 'def').
1fdda 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54  **     -> 'CREAT
1fddb 45 20 54 41 42 4c 45 20 64 65 66 28 61 2c 20 62  E TABLE def(a, b
1fddc 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  , c)'.**.** sqli
1fddd 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28  te_rename_table(
1fdde 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20  'CREATE INDEX i 
1fddf 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 64 65 66  ON abc(a)', 'def
1fde0 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52  ').**     -> 'CR
1fde1 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20  EATE INDEX i ON 
1fde2 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2f  def(a, b, c)'.*/
1fde3 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e  .static void ren
1fde4 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 0a 20 20  ameTableFunc(.  
1fde5 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1fde6 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
1fde7 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
1fde8 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
1fde9 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
1fdea 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d  ar const *zSql =
1fdeb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1fdec 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
1fded 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
1fdee 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20  nst *zTableName 
1fdef 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1fdf0 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a  text(argv[1]);..
1fdf1 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54    int token;.  T
1fdf2 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e  oken tname;.  un
1fdf3 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
1fdf4 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a  t *zCsr = zSql;.
1fdf5 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20    int len = 0;. 
1fdf6 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20   char *zRet;..  
1fdf7 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
1fdf8 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
1fdf9 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
1fdfa 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
1fdfb 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
1fdfc 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69  .  /* The princi
1fdfd 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61  ple used to loca
1fdfe 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  te the table nam
1fdff 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20  e in the CREATE 
1fe00 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74  TABLE .  ** stat
1fe01 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68  ement is that th
1fe02 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
1fe03 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70  the first non-sp
1fe04 61 63 65 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20  ace token that. 
1fe05 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69 61 74 65   ** is immediate
1fe06 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  ly followed by a
1fe07 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f 55 53 49   TK_LP or TK_USI
1fe08 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a 20  NG token..  */. 
1fe09 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20   if( zSql ){.   
1fe0a 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 66 28 20   do {.      if( 
1fe0b 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20  !*zCsr ){.      
1fe0c 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20    /* Ran out of 
1fe0d 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e  input before fin
1fe0e 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 6e 67 20  ding an opening 
1fe0f 62 72 61 63 6b 65 74 2e 20 52 65 74 75 72 6e 20  bracket. Return 
1fe10 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20  NULL. */.       
1fe11 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
1fe12 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65  ..      /* Store
1fe13 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   the token that 
1fe14 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69  zCsr points to i
1fe15 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20  n tname. */.    
1fe16 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72    tname.z = zCsr
1fe17 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20  ;.      tname.n 
1fe18 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a  = len;..      /*
1fe19 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f   Advance zCsr to
1fe1a 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e   the next token.
1fe1b 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65   Store that toke
1fe1c 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e  n type in 'token
1fe1d 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  ',.      ** and 
1fe1e 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c  its length in 'l
1fe1f 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20  en' (to be used 
1fe20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
1fe21 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20  f this loop)..  
1fe22 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20      */.      do 
1fe23 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b  {.        zCsr +
1fe24 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c  = len;.        l
1fe25 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  en = sqlite3GetT
1fe26 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65  oken(zCsr, &toke
1fe27 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77 68 69 6c  n);.      } whil
1fe28 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41  e( token==TK_SPA
1fe29 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  CE );.      asse
1fe2a 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 20 20 20  rt( len>0 );.   
1fe2b 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 21   } while( token!
1fe2c 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b 65 6e 21  =TK_LP && token!
1fe2d 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a 0a 20 20  =TK_USING );..  
1fe2e 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65 33    zRet = sqlite3
1fe2f 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 2e 2a  MPrintf(db, "%.*
1fe30 73 5c 22 25 77 5c 22 25 73 22 2c 20 74 6e 61 6d  s\"%w\"%s", tnam
1fe31 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c  e.z - zSql, zSql
1fe32 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65  , .       zTable
1fe33 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e  Name, tname.z+tn
1fe34 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69  ame.n);.    sqli
1fe35 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
1fe36 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d  context, zRet, -
1fe37 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  1, SQLITE_DYNAMI
1fe38 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64  C);.  }.}..#ifnd
1fe39 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1fe3a 52 49 47 47 45 52 0a 2f 2a 20 54 68 69 73 20 66  RIGGER./* This f
1fe3b 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
1fe3c 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64  by SQL generated
1fe3d 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
1fe3e 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45  e.** ALTER TABLE
1fe3f 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69   command. The fi
1fe40 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
1fe41 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43 52  the text of a CR
1fe42 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 2a 2a  EATE TRIGGER .**
1fe43 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20   statement. The 
1fe44 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c  second is a tabl
1fe45 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c  e name. The tabl
1fe46 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52  e name in the CR
1fe47 45 41 54 45 20 0a 2a 2a 20 54 52 49 47 47 45 52  EATE .** TRIGGER
1fe48 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65   statement is re
1fe49 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20  placed with the 
1fe4a 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 61  third argument a
1fe4b 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 0a 2a  nd the result .*
1fe4c 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73  * returned. This
1fe4d 20 69 73 20 61 6e 61 6c 61 67 6f 75 73 20 74 6f   is analagous to
1fe4e 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63   renameTableFunc
1fe4f 28 29 20 61 62 6f 76 65 2c 20 65 78 63 65 70 74  () above, except
1fe50 20 66 6f 72 20 43 52 45 41 54 45 0a 2a 2a 20 54   for CREATE.** T
1fe51 52 49 47 47 45 52 2c 20 6e 6f 74 20 43 52 45 41  RIGGER, not CREA
1fe52 54 45 20 49 4e 44 45 58 20 61 6e 64 20 43 52 45  TE INDEX and CRE
1fe53 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f 0a 73 74  ATE TABLE..*/.st
1fe54 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65  atic void rename
1fe55 54 72 69 67 67 65 72 46 75 6e 63 28 0a 20 20 73  TriggerFunc(.  s
1fe56 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1fe57 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
1fe58 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
1fe59 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
1fe5a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
1fe5b 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20  r const *zSql = 
1fe5c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1fe5d 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75  xt(argv[0]);.  u
1fe5e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e  nsigned char con
1fe5f 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d  st *zTableName =
1fe60 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1fe61 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20  ext(argv[1]);.. 
1fe62 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f   int token;.  To
1fe63 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 69 6e 74  ken tname;.  int
1fe64 20 64 69 73 74 20 3d 20 33 3b 0a 20 20 75 6e 73   dist = 3;.  uns
1fe65 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
1fe66 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20   *zCsr = zSql;. 
1fe67 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20   int len = 0;.  
1fe68 63 68 61 72 20 2a 7a 52 65 74 3b 0a 20 20 73 71  char *zRet;.  sq
1fe69 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
1fe6a 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
1fe6b 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
1fe6c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1fe6d 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20  TER(NotUsed);.. 
1fe6e 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c   /* The principl
1fe6f 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  e used to locate
1fe70 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
1fe71 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 52  in the CREATE TR
1fe72 49 47 47 45 52 20 0a 20 20 2a 2a 20 73 74 61 74  IGGER .  ** stat
1fe73 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68  ement is that th
1fe74 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
1fe75 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20  the first token 
1fe76 74 68 61 74 20 69 73 20 69 6d 6d 65 64 69 61 74  that is immediat
1fe77 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65 63 65 64  edly.  ** preced
1fe78 65 64 20 62 79 20 65 69 74 68 65 72 20 54 4b 5f  ed by either TK_
1fe79 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20 61 6e 64  ON or TK_DOT and
1fe7a 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 20 66 6f   immediatedly fo
1fe7b 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65 0a 20 20  llowed by one.  
1fe7c 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e 2c 20 54  ** of TK_WHEN, T
1fe7d 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b 5f 46 4f  K_BEGIN or TK_FO
1fe7e 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53  R..  */.  if( zS
1fe7f 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 0a  ql ){.    do {..
1fe80 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72        if( !*zCsr
1fe81 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52   ){.        /* R
1fe82 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20  an out of input 
1fe83 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 74  before finding t
1fe84 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 52  he table name. R
1fe85 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20  eturn NULL. */. 
1fe86 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
1fe87 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
1fe88 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e   Store the token
1fe89 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e 74   that zCsr point
1fe8a 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a  s to in tname. *
1fe8b 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20  /.      tname.z 
1fe8c 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e  = zCsr;.      tn
1fe8d 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20  ame.n = len;..  
1fe8e 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a      /* Advance z
1fe8f 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20  Csr to the next 
1fe90 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61  token. Store tha
1fe91 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20  t token type in 
1fe92 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a  'token',.      *
1fe93 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68  * and its length
1fe94 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65   in 'len' (to be
1fe95 20 75 73 65 64 20 6e 65 78 74 20 69 74 65 72 61   used next itera
1fe96 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f  tion of this loo
1fe97 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  p)..      */.   
1fe98 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20     do {.        
1fe99 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20  zCsr += len;.   
1fe9a 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74       len = sqlit
1fe9b 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c  e3GetToken(zCsr,
1fe9c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20   &token);.      
1fe9d 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54  }while( token==T
1fe9e 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20  K_SPACE );.     
1fe9f 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29   assert( len>0 )
1fea0 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 56 61 72 69  ;..      /* Vari
1fea1 61 62 6c 65 20 27 64 69 73 74 27 20 73 74 6f 72  able 'dist' stor
1fea2 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  es the number of
1fea3 20 74 6f 6b 65 6e 73 20 72 65 61 64 20 73 69 6e   tokens read sin
1fea4 63 65 20 74 68 65 20 6d 6f 73 74 0a 20 20 20 20  ce the most.    
1fea5 20 20 2a 2a 20 72 65 63 65 6e 74 20 54 4b 5f 44    ** recent TK_D
1fea6 4f 54 20 6f 72 20 54 4b 5f 4f 4e 2e 20 54 68 69  OT or TK_ON. Thi
1fea7 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65  s means that whe
1fea8 6e 20 61 20 57 48 45 4e 2c 20 46 4f 52 20 6f 72  n a WHEN, FOR or
1fea9 20 42 45 47 49 4e 20 0a 20 20 20 20 20 20 2a 2a   BEGIN .      **
1feaa 20 74 6f 6b 65 6e 20 69 73 20 72 65 61 64 20 61   token is read a
1feab 6e 64 20 27 64 69 73 74 27 20 65 71 75 61 6c 73  nd 'dist' equals
1feac 20 32 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f   2, the conditio
1fead 6e 20 73 74 61 74 65 64 20 61 62 6f 76 65 0a 20  n stated above. 
1feae 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6d 65       ** to be me
1feaf 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  t..      **.    
1feb0 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 4f    ** Note that O
1feb1 4e 20 63 61 6e 6e 6f 74 20 62 65 20 61 20 64 61  N cannot be a da
1feb2 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 6f 72  tabase, table or
1feb3 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20 73 6f   column name, so
1feb4 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20  .      ** there 
1feb5 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 6f  is no need to wo
1feb6 72 72 79 20 61 62 6f 75 74 20 73 79 6e 74 61 78  rry about syntax
1feb7 20 6c 69 6b 65 20 0a 20 20 20 20 20 20 2a 2a 20   like .      ** 
1feb8 22 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20  "CREATE TRIGGER 
1feb9 2e 2e 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 42 45 47  ... ON ON.ON BEG
1feba 49 4e 20 2e 2e 2e 22 20 65 74 63 2e 0a 20 20 20  IN ..." etc..   
1febb 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 69 73 74     */.      dist
1febc 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 74 6f  ++;.      if( to
1febd 6b 65 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74  ken==TK_DOT || t
1febe 6f 6b 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0a 20  oken==TK_ON ){. 
1febf 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 30 3b         dist = 0;
1fec0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 77  .      }.    } w
1fec1 68 69 6c 65 28 20 64 69 73 74 21 3d 32 20 7c 7c  hile( dist!=2 ||
1fec2 20 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45 4e   (token!=TK_WHEN
1fec3 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46 4f   && token!=TK_FO
1fec4 52 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 42  R && token!=TK_B
1fec5 45 47 49 4e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a  EGIN) );..    /*
1fec6 20 56 61 72 69 61 62 6c 65 20 74 6e 61 6d 65 20   Variable tname 
1fec7 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  now contains the
1fec8 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 74   token that is t
1fec9 68 65 20 6f 6c 64 20 74 61 62 6c 65 2d 6e 61 6d  he old table-nam
1feca 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20  e.    ** in the 
1fecb 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73  CREATE TRIGGER s
1fecc 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f  tatement..    */
1fecd 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69  .    zRet = sqli
1fece 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
1fecf 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 74  %.*s\"%w\"%s", t
1fed0 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a  name.z - zSql, z
1fed1 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61  Sql, .       zTa
1fed2 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a  bleName, tname.z
1fed3 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73  +tname.n);.    s
1fed4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
1fed5 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74  xt(context, zRet
1fed6 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e  , -1, SQLITE_DYN
1fed7 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  AMIC);.  }.}.#en
1fed8 64 69 66 20 20 20 2f 2a 20 21 53 51 4c 49 54 45  dif   /* !SQLITE
1fed9 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f  _OMIT_TRIGGER */
1feda 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
1fedb 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
1fedc 6f 6e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70  ons used to help
1fedd 20 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52   implement ALTER
1fede 20 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54 45   TABLE.*/.SQLITE
1fedf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1fee0 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69  lite3AlterFuncti
1fee1 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ons(sqlite3 *db)
1fee2 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74  {.  sqlite3Creat
1fee3 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74  eFunc(db, "sqlit
1fee4 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22 2c  e_rename_table",
1fee5 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
1fee6 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
1fee7 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e               ren
1fee8 61 6d 65 54 61 62 6c 65 46 75 6e 63 2c 20 30 2c  ameTableFunc, 0,
1fee9 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   0);.#ifndef SQL
1feea 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1feeb 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  .  sqlite3Create
1feec 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65  Func(db, "sqlite
1feed 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 22  _rename_trigger"
1feee 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 2, SQLITE_UTF8
1feef 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
1fef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
1fef1 6e 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 2c  nameTriggerFunc,
1fef2 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d   0, 0);.#endif.}
1fef3 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1fef4 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 57   the text of a W
1fef5 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20  HERE expression 
1fef6 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65  which can be use
1fef7 64 20 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a  d to select all.
1fef8 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69  ** temporary tri
1fef9 67 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70  ggers on table p
1fefa 54 61 62 20 66 72 6f 6d 20 74 68 65 20 73 71 6c  Tab from the sql
1fefb 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20  ite_temp_master 
1fefc 74 61 62 6c 65 2e 20 49 66 0a 2a 2a 20 74 61 62  table. If.** tab
1fefd 6c 65 20 70 54 61 62 20 68 61 73 20 6e 6f 20 74  le pTab has no t
1fefe 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72  emporary trigger
1feff 73 2c 20 6f 72 20 69 73 20 69 74 73 65 6c 66 20  s, or is itself 
1ff00 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 0a 2a  stored in the .*
1ff01 2a 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  * temporary data
1ff02 62 61 73 65 2c 20 4e 55 4c 4c 20 69 73 20 72 65  base, NULL is re
1ff03 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
1ff04 63 20 63 68 61 72 20 2a 77 68 65 72 65 54 65 6d  c char *whereTem
1ff05 70 54 72 69 67 67 65 72 73 28 50 61 72 73 65 20  pTriggers(Parse 
1ff06 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
1ff07 70 54 61 62 29 7b 0a 20 20 54 72 69 67 67 65 72  pTab){.  Trigger
1ff08 20 2a 70 54 72 69 67 3b 0a 20 20 63 68 61 72 20   *pTrig;.  char 
1ff09 2a 7a 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 63  *zWhere = 0;.  c
1ff0a 68 61 72 20 2a 74 6d 70 20 3d 20 30 3b 0a 20 20  har *tmp = 0;.  
1ff0b 63 6f 6e 73 74 20 53 63 68 65 6d 61 20 2a 70 54  const Schema *pT
1ff0c 65 6d 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72  empSchema = pPar
1ff0d 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  se->db->aDb[1].p
1ff0e 53 63 68 65 6d 61 3b 20 2f 2a 20 54 65 6d 70 20  Schema; /* Temp 
1ff0f 64 62 20 73 63 68 65 6d 61 20 2a 2f 0a 0a 20 20  db schema */..  
1ff10 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  /* If the table 
1ff11 69 73 20 6e 6f 74 20 6c 6f 63 61 74 65 64 20 69  is not located i
1ff12 6e 20 74 68 65 20 74 65 6d 70 2d 64 62 20 28 69  n the temp-db (i
1ff13 6e 20 77 68 69 63 68 20 63 61 73 65 20 4e 55 4c  n which case NUL
1ff14 4c 20 69 73 20 0a 20 20 2a 2a 20 72 65 74 75 72  L is .  ** retur
1ff15 6e 65 64 2c 20 6c 6f 6f 70 20 74 68 72 6f 75 67  ned, loop throug
1ff16 68 20 74 68 65 20 74 61 62 6c 65 73 20 6c 69 73  h the tables lis
1ff17 74 20 6f 66 20 74 72 69 67 67 65 72 73 2e 20 46  t of triggers. F
1ff18 6f 72 20 65 61 63 68 20 74 72 69 67 67 65 72 0a  or each trigger.
1ff19 20 20 2a 2a 20 74 68 61 74 20 69 73 20 6e 6f 74    ** that is not
1ff1a 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 65 6d   part of the tem
1ff1b 70 2d 64 62 20 73 63 68 65 6d 61 2c 20 61 64 64  p-db schema, add
1ff1c 20 61 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65   a clause to the
1ff1d 20 57 48 45 52 45 20 0a 20 20 2a 2a 20 65 78 70   WHERE .  ** exp
1ff1e 72 65 73 73 69 6f 6e 20 62 65 69 6e 67 20 62 75  ression being bu
1ff1f 69 6c 74 20 75 70 20 69 6e 20 7a 57 68 65 72 65  ilt up in zWhere
1ff20 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61  ..  */.  if( pTa
1ff21 62 2d 3e 70 53 63 68 65 6d 61 21 3d 70 54 65 6d  b->pSchema!=pTem
1ff22 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 73  pSchema ){.    s
1ff23 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1ff24 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 66 6f 72  rse->db;.    for
1ff25 28 70 54 72 69 67 3d 73 71 6c 69 74 65 33 54 72  (pTrig=sqlite3Tr
1ff26 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65  iggerList(pParse
1ff27 2c 20 70 54 61 62 29 3b 20 70 54 72 69 67 3b 20  , pTab); pTrig; 
1ff28 70 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65  pTrig=pTrig->pNe
1ff29 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  xt){.      if( p
1ff2a 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  Trig->pSchema==p
1ff2b 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  TempSchema ){.  
1ff2c 20 20 20 20 20 20 69 66 28 20 21 7a 57 68 65 72        if( !zWher
1ff2d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  e ){.          z
1ff2e 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d  Where = sqlite3M
1ff2f 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65  Printf(db, "name
1ff30 3d 25 51 22 2c 20 70 54 72 69 67 2d 3e 6e 61 6d  =%Q", pTrig->nam
1ff31 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  e);.        }els
1ff32 65 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6d 70  e{.          tmp
1ff33 20 3d 20 7a 57 68 65 72 65 3b 0a 20 20 20 20 20   = zWhere;.     
1ff34 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73 71       zWhere = sq
1ff35 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
1ff36 20 22 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22   "%s OR name=%Q"
1ff37 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 69 67 2d  , zWhere, pTrig-
1ff38 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  >name);.        
1ff39 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ff3a 64 62 2c 20 74 6d 70 29 3b 0a 20 20 20 20 20 20  db, tmp);.      
1ff3b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1ff3c 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  }.  }.  return z
1ff3d 57 68 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Where;.}../*.** 
1ff3e 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1ff3f 20 64 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64   drop and reload
1ff40 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65   the internal re
1ff41 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1ff42 74 61 62 6c 65 0a 2a 2a 20 70 54 61 62 20 66 72  table.** pTab fr
1ff43 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  om the database,
1ff44 20 69 6e 63 6c 75 64 69 6e 67 20 74 72 69 67 67   including trigg
1ff45 65 72 73 20 61 6e 64 20 74 65 6d 70 6f 72 61 72  ers and temporar
1ff46 79 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 20 41  y triggers..** A
1ff47 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65 20 69 73  rgument zName is
1ff48 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1ff49 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61   table in the da
1ff4a 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 74  tabase schema at
1ff4b 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 74 68 65  .** the time the
1ff4c 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20   generated code 
1ff4d 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68 69  is executed. Thi
1ff4e 73 20 63 61 6e 20 62 65 20 64 69 66 66 65 72 65  s can be differe
1ff4f 6e 74 20 66 72 6f 6d 0a 2a 2a 20 70 54 61 62 2d  nt from.** pTab-
1ff50 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69 73 20 66  >zName if this f
1ff51 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67  unction is being
1ff52 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64 65 20   called to code 
1ff53 70 61 72 74 20 6f 66 20 61 6e 20 0a 2a 2a 20 22  part of an .** "
1ff54 41 4c 54 45 52 20 54 41 42 4c 45 20 52 45 4e 41  ALTER TABLE RENA
1ff55 4d 45 20 54 4f 22 20 73 74 61 74 65 6d 65 6e 74  ME TO" statement
1ff56 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1ff57 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65   reloadTableSche
1ff58 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ma(Parse *pParse
1ff59 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63  , Table *pTab, c
1ff5a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1ff5b 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  ){.  Vdbe *v;.  
1ff5c 63 68 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20  char *zWhere;.  
1ff5d 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
1ff5e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1ff5f 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20  dex of database 
1ff60 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20  containing pTab 
1ff61 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1ff62 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
1ff63 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b   Trigger *pTrig;
1ff64 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73  .#endif..  v = s
1ff65 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1ff66 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20  arse);.  if( !v 
1ff67 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
1ff68 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1ff69 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
1ff6a 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20  pParse->db) );. 
1ff6b 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1ff6c 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
1ff6d 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
1ff6e 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74  chema);.  assert
1ff6f 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 23 69 66  ( iDb>=0 );..#if
1ff70 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1ff71 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 44 72  _TRIGGER.  /* Dr
1ff72 6f 70 20 61 6e 79 20 74 61 62 6c 65 20 74 72 69  op any table tri
1ff73 67 67 65 72 73 20 66 72 6f 6d 20 74 68 65 20 69  ggers from the i
1ff74 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20  nternal schema. 
1ff75 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69 67 3d 73  */.  for(pTrig=s
1ff76 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73  qlite3TriggerLis
1ff77 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  t(pParse, pTab);
1ff78 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54   pTrig; pTrig=pT
1ff79 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  rig->pNext){.   
1ff7a 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d 20 73   int iTrigDb = s
1ff7b 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
1ff7c 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
1ff7d 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b  pTrig->pSchema);
1ff7e 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 54 72  .    assert( iTr
1ff7f 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54 72  igDb==iDb || iTr
1ff80 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 73  igDb==1 );.    s
1ff81 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1ff82 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  (v, OP_DropTrigg
1ff83 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30 2c 20  er, iTrigDb, 0, 
1ff84 30 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c 20  0, pTrig->name, 
1ff85 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  0);.  }.#endif..
1ff86 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 61    /* Drop the ta
1ff87 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 66 72  ble and index fr
1ff88 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  om the internal 
1ff89 73 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69  schema */.  sqli
1ff8a 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1ff8b 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 69   OP_DropTable, i
1ff8c 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e  Db, 0, 0, pTab->
1ff8d 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 20 20 2f 2a  zName, 0);..  /*
1ff8e 20 52 65 6c 6f 61 64 20 74 68 65 20 74 61 62 6c   Reload the tabl
1ff8f 65 2c 20 69 6e 64 65 78 20 61 6e 64 20 70 65 72  e, index and per
1ff90 6d 61 6e 65 6e 74 20 74 72 69 67 67 65 72 20 73  manent trigger s
1ff91 63 68 65 6d 61 73 2e 20 2a 2f 0a 20 20 7a 57 68  chemas. */.  zWh
1ff92 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ere = sqlite3MPr
1ff93 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62 2c  intf(pParse->db,
1ff94 20 22 74 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c 20   "tbl_name=%Q", 
1ff95 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a  zName);.  if( !z
1ff96 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 3b 0a  Where ) return;.
1ff97 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ff98 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53  Op4(v, OP_ParseS
1ff99 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30  chema, iDb, 0, 0
1ff9a 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e  , zWhere, P4_DYN
1ff9b 41 4d 49 43 29 3b 0a 0a 23 69 66 6e 64 65 66 20  AMIC);..#ifndef 
1ff9c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1ff9d 47 45 52 0a 20 20 2f 2a 20 4e 6f 77 2c 20 69 66  GER.  /* Now, if
1ff9e 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f   the table is no
1ff9f 74 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  t stored in the 
1ffa0 74 65 6d 70 20 64 61 74 61 62 61 73 65 2c 20 72  temp database, r
1ffa1 65 6c 6f 61 64 20 61 6e 79 20 74 65 6d 70 20 0a  eload any temp .
1ffa2 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 44    ** triggers. D
1ffa3 6f 6e 27 74 20 75 73 65 20 49 4e 28 2e 2e 2e 29  on't use IN(...)
1ffa4 20 69 6e 20 63 61 73 65 20 53 51 4c 49 54 45 5f   in case SQLITE_
1ffa5 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 69 73  OMIT_SUBQUERY is
1ffa6 20 64 65 66 69 6e 65 64 2e 20 0a 20 20 2a 2f 0a   defined. .  */.
1ffa7 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68    if( (zWhere=wh
1ffa8 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 28  ereTempTriggers(
1ffa9 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d  pParse, pTab))!=
1ffaa 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1ffab 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1ffac 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 31 2c  _ParseSchema, 1,
1ffad 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50   0, 0, zWhere, P
1ffae 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a  4_DYNAMIC);.  }.
1ffaf 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
1ffb0 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1ffb1 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 22   implement the "
1ffb2 41 4c 54 45 52 20 54 41 42 4c 45 20 78 78 78 20  ALTER TABLE xxx 
1ffb3 52 45 4e 41 4d 45 20 54 4f 20 79 79 79 22 20 0a  RENAME TO yyy" .
1ffb4 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a  ** command. .*/.
1ffb5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1ffb6 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
1ffb7 52 65 6e 61 6d 65 54 61 62 6c 65 28 0a 20 20 50  RenameTable(.  P
1ffb8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1ffb9 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
1ffba 65 72 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20  er context. */. 
1ffbb 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
1ffbc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1ffbd 65 20 74 61 62 6c 65 20 74 6f 20 72 65 6e 61 6d  e table to renam
1ffbe 65 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  e. */.  Token *p
1ffbf 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  Name            
1ffc0 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 61 62    /* The new tab
1ffc1 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 29 7b 0a 20  le name. */.){. 
1ffc2 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
1ffc3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
1ffc4 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74  tabase that cont
1ffc5 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65 20 2a  ains the table *
1ffc6 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  /.  char *zDb;  
1ffc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ffc8 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
1ffc9 65 20 69 44 62 20 2a 2f 0a 20 20 54 61 62 6c 65  e iDb */.  Table
1ffca 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
1ffcb 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65       /* Table be
1ffcc 69 6e 67 20 72 65 6e 61 6d 65 64 20 2a 2f 0a 20  ing renamed */. 
1ffcd 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30   char *zName = 0
1ffce 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 55  ;          /* NU
1ffcf 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20 76 65  LL-terminated ve
1ffd0 72 73 69 6f 6e 20 6f 66 20 70 4e 61 6d 65 20 2a  rsion of pName *
1ffd1 2f 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  / .  sqlite3 *db
1ffd2 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f   = pParse->db; /
1ffd3 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
1ffd4 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ction */.  int n
1ffd5 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  TabName;        
1ffd6 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1ffd7 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  f UTF-8 characte
1ffd8 72 73 20 69 6e 20 7a 54 61 62 4e 61 6d 65 20 2a  rs in zTabName *
1ffd9 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1ffda 7a 54 61 62 4e 61 6d 65 3b 20 20 20 20 20 2f 2a  zTabName;     /*
1ffdb 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 20 6f   Original name o
1ffdc 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  f the table */. 
1ffdd 20 56 64 62 65 20 2a 76 3b 0a 23 69 66 6e 64 65   Vdbe *v;.#ifnde
1ffde 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1ffdf 49 47 47 45 52 0a 20 20 63 68 61 72 20 2a 7a 57  IGGER.  char *zW
1ffe0 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20  here = 0;       
1ffe1 20 20 2f 2a 20 57 68 65 72 65 20 63 6c 61 75 73    /* Where claus
1ffe2 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 65 6d 70  e to locate temp
1ffe3 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e   triggers */.#en
1ffe4 64 69 66 0a 20 20 69 6e 74 20 69 73 56 69 72 74  dif.  int isVirt
1ffe5 75 61 6c 52 65 6e 61 6d 65 20 3d 20 30 3b 20 20  ualRename = 0;  
1ffe6 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
1ffe7 69 73 20 61 20 76 2d 74 61 62 6c 65 20 77 69 74  is a v-table wit
1ffe8 68 20 61 6e 20 78 52 65 6e 61 6d 65 28 29 20 2a  h an xRename() *
1ffe9 2f 0a 20 20 0a 20 20 69 66 28 20 64 62 2d 3e 6d  /.  .  if( db->m
1ffea 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
1ffeb 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
1ffec 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20  able;.  assert( 
1ffed 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  pSrc->nSrc==1 );
1ffee 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1ffef 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
1fff0 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
1fff1 62 29 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20  b) );..  pTab = 
1fff2 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
1fff3 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53  le(pParse, 0, pS
1fff4 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20  rc->a[0].zName, 
1fff5 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61  pSrc->a[0].zData
1fff6 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54  base);.  if( !pT
1fff7 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72  ab ) goto exit_r
1fff8 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 69  ename_table;.  i
1fff9 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
1fffa 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
1fffb 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
1fffc 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62  ema);.  zDb = db
1fffd 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
1fffe 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e 55  ;..  /* Get a NU
1ffff 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64 20 76 65  LL terminated ve
20000 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77  rsion of the new
20001 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a   table name. */.
20002 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
20003 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
20004 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b, pName);.  if(
20005 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 65   !zName ) goto e
20006 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
20007 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  ;..  /* Check th
20008 61 74 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e  at a table or in
20009 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d 65  dex named 'zName
2000a 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61  ' does not alrea
2000b 64 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 6e  dy exist.  ** in
2000c 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 49   database iDb. I
2000d 66 20 73 6f 2c 20 74 68 69 73 20 69 73 20 61 6e  f so, this is an
2000e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69   error..  */.  i
2000f 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61  f( sqlite3FindTa
20010 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a  ble(db, zName, z
20011 44 62 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 69  Db) || sqlite3Fi
20012 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d  ndIndex(db, zNam
20013 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73  e, zDb) ){.    s
20014 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
20015 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
20016 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64  "there is alread
20017 79 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20  y another table 
20018 6f 72 20 69 6e 64 65 78 20 77 69 74 68 20 74 68  or index with th
20019 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e  is name: %s", zN
2001a 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
2001b 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
2001c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
2001d 20 73 75 72 65 20 69 74 20 69 73 20 6e 6f 74 20   sure it is not 
2001e 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 62  a system table b
2001f 65 69 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f 72  eing altered, or
20020 20 61 20 72 65 73 65 72 76 65 64 20 6e 61 6d 65   a reserved name
20021 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 74  .  ** that the t
20022 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 72 65  able is being re
20023 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20  named to..  */. 
20024 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 6c   if( sqlite3Strl
20025 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d 65  en30(pTab->zName
20026 29 3e 36 20 0a 20 20 20 26 26 20 30 3d 3d 73 71  )>6 .   && 0==sq
20027 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54  lite3StrNICmp(pT
20028 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69  ab->zName, "sqli
20029 74 65 5f 22 2c 20 37 29 0a 20 20 29 7b 0a 20 20  te_", 7).  ){.  
2002a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2002b 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65  g(pParse, "table
2002c 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61   %s may not be a
2002d 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a  ltered", pTab->z
2002e 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
2002f 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
20030 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c  e;.  }.  if( SQL
20031 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43  ITE_OK!=sqlite3C
20032 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70  heckObjectName(p
20033 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b  Parse, zName) ){
20034 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72  .    goto exit_r
20035 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d  ename_table;.  }
20036 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
20037 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28  _OMIT_VIEW.  if(
20038 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
20039 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2003a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76  orMsg(pParse, "v
2003b 69 65 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62  iew %s may not b
2003c 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62  e altered", pTab
2003d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
2003e 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
2003f 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  able;.  }.#endif
20040 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
20041 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
20042 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ION.  /* Invoke 
20043 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  the authorizatio
20044 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20  n callback. */. 
20045 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
20046 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
20047 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45  LITE_ALTER_TABLE
20048 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61  , zDb, pTab->zNa
20049 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f  me, 0) ){.    go
2004a 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
2004b 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  able;.  }.#endif
2004c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2004d 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2004e 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  LE.  if( sqlite3
2004f 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
20050 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  es(pParse, pTab)
20051 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
20052 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
20053 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74    }.  if( IsVirt
20054 75 61 6c 28 70 54 61 62 29 20 26 26 20 70 54 61  ual(pTab) && pTa
20055 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65  b->pMod->pModule
20056 2d 3e 78 52 65 6e 61 6d 65 20 29 7b 0a 20 20 20  ->xRename ){.   
20057 20 69 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65   isVirtualRename
20058 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66   = 1;.  }.#endif
20059 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74  ..  /* Begin a t
2005a 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 63  ransaction and c
2005b 6f 64 65 20 74 68 65 20 56 65 72 69 66 79 43 6f  ode the VerifyCo
2005c 6f 6b 69 65 20 66 6f 72 20 64 61 74 61 62 61 73  okie for databas
2005d 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54 68 65  e iDb. .  ** The
2005e 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63 68  n modify the sch
2005f 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69 6e 63  ema cookie (sinc
20060 65 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c  e the ALTER TABL
20061 45 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0a 20  E modifies the. 
20062 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f 70 65   ** schema). Ope
20063 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  n a statement tr
20064 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65  ansaction if the
20065 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74   table is a virt
20066 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 0a  ual.  ** table..
20067 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
20068 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
20069 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b  );.  if( v==0 ){
2006a 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72  .    goto exit_r
2006b 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d  ename_table;.  }
2006c 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57  .  sqlite3BeginW
2006d 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
2006e 61 72 73 65 2c 20 69 73 56 69 72 74 75 61 6c 52  arse, isVirtualR
2006f 65 6e 61 6d 65 2c 20 69 44 62 29 3b 0a 20 20 73  ename, iDb);.  s
20070 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
20071 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
20072 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
20073 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  s a virtual tabl
20074 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52  e, invoke the xR
20075 65 6e 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e  ename() function
20076 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20   if.  ** one is 
20077 64 65 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65  defined. The xRe
20078 6e 61 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20  name() callback 
20079 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20  will modify the 
2007a 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e  names.  ** of an
2007b 79 20 72 65 73 6f 75 72 63 65 73 20 75 73 65 64  y resources used
2007c 20 62 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20   by the v-table 
2007d 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28  implementation (
2007e 69 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a  including other.
2007f 20 20 2a 2a 20 53 51 4c 69 74 65 20 74 61 62 6c    ** SQLite tabl
20080 65 73 29 20 74 68 61 74 20 61 72 65 20 69 64 65  es) that are ide
20081 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 6e  ntified by the n
20082 61 6d 65 20 6f 66 20 74 68 65 20 76 69 72 74 75  ame of the virtu
20083 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23  al table..  */.#
20084 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20085 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
20086 20 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 52    if( isVirtualR
20087 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74  ename ){.    int
20088 20 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e   i = ++pParse->n
20089 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
2008a 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2008b 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 69 2c 20  _String8, 0, i, 
2008c 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  0, zName, 0);.  
2008d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2008e 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d  Op4(v, OP_VRenam
2008f 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73  e, i, 0, 0,(cons
20090 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56  t char*)pTab->pV
20091 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  tab, P4_VTAB);. 
20092 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
20093 66 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  figure out how m
20094 61 6e 79 20 55 54 46 2d 38 20 63 68 61 72 61 63  any UTF-8 charac
20095 74 65 72 73 20 61 72 65 20 69 6e 20 7a 4e 61 6d  ters are in zNam
20096 65 20 2a 2f 0a 20 20 7a 54 61 62 4e 61 6d 65 20  e */.  zTabName 
20097 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20  = pTab->zName;. 
20098 20 6e 54 61 62 4e 61 6d 65 20 3d 20 73 71 6c 69   nTabName = sqli
20099 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a  te3Utf8CharLen(z
2009a 54 61 62 4e 61 6d 65 2c 20 2d 31 29 3b 0a 0a 20  TabName, -1);.. 
2009b 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 73   /* Modify the s
2009c 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
2009d 6c 65 20 74 6f 20 75 73 65 20 74 68 65 20 6e 65  le to use the ne
2009e 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
2009f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64  .  sqlite3Nested
200a0 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
200a1 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25      "UPDATE %Q.%
200a2 73 20 53 45 54 20 22 0a 23 69 66 64 65 66 20 53  s SET ".#ifdef S
200a3 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
200a4 45 52 0a 20 20 20 20 20 20 20 20 20 20 22 73 71  ER.          "sq
200a5 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d  l = sqlite_renam
200a6 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29  e_table(sql, %Q)
200a7 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  , ".#else.      
200a8 20 20 20 20 22 73 71 6c 20 3d 20 43 41 53 45 20      "sql = CASE 
200a9 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57  ".            "W
200aa 48 45 4e 20 74 79 70 65 20 3d 20 27 74 72 69 67  HEN type = 'trig
200ab 67 65 72 27 20 54 48 45 4e 20 73 71 6c 69 74 65  ger' THEN sqlite
200ac 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 28  _rename_trigger(
200ad 73 71 6c 2c 20 25 51 29 22 0a 20 20 20 20 20 20  sql, %Q)".      
200ae 20 20 20 20 20 20 22 45 4c 53 45 20 73 71 6c 69        "ELSE sqli
200af 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28  te_rename_table(
200b0 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20 22 0a  sql, %Q) END, ".
200b1 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20  #endif.         
200b2 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 2c   "tbl_name = %Q,
200b3 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 6e 61   ".          "na
200b4 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20  me = CASE ".    
200b5 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 74 79          "WHEN ty
200b6 70 65 3d 27 74 61 62 6c 65 27 20 54 48 45 4e 20  pe='table' THEN 
200b7 25 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  %Q ".           
200b8 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49 4b 45   "WHEN name LIKE
200b9 20 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64   'sqlite_autoind
200ba 65 78 25 25 27 20 41 4e 44 20 74 79 70 65 3d 27  ex%%' AND type='
200bb 69 6e 64 65 78 27 20 54 48 45 4e 20 22 0a 20 20  index' THEN ".  
200bc 20 20 20 20 20 20 20 20 20 20 20 22 27 73 71 6c             "'sql
200bd 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 27 20  ite_autoindex_' 
200be 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72 28  || %Q || substr(
200bf 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22 0a 20 20  name,%d+18) ".  
200c0 20 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20            "ELSE 
200c1 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20 20 20 20  name END ".     
200c2 20 22 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65   "WHERE tbl_name
200c3 3d 25 51 20 41 4e 44 20 22 0a 20 20 20 20 20 20  =%Q AND ".      
200c4 20 20 20 20 22 28 74 79 70 65 3d 27 74 61 62 6c      "(type='tabl
200c5 65 27 20 4f 52 20 74 79 70 65 3d 27 69 6e 64 65  e' OR type='inde
200c6 78 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69 67  x' OR type='trig
200c7 67 65 72 27 29 3b 22 2c 20 0a 20 20 20 20 20 20  ger');", .      
200c8 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  zDb, SCHEMA_TABL
200c9 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c 20 7a  E(iDb), zName, z
200ca 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 0a 23 69  Name, zName, .#i
200cb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
200cc 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20  T_TRIGGER.      
200cd 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66 0a 20 20  zName,.#endif.  
200ce 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 61 62 4e      zName, nTabN
200cf 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 0a 20 20  ame, zTabName.  
200d0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
200d1 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
200d2 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66 20 74 68  EMENT.  /* If th
200d3 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
200d4 65 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 69  e table exists i
200d5 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2c  n this database,
200d6 20 74 68 65 6e 20 75 70 64 61 74 65 20 0a 20 20   then update .  
200d7 2a 2a 20 69 74 20 77 69 74 68 20 74 68 65 20 6e  ** it with the n
200d8 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 0a 20  ew table name.. 
200d9 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
200da 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22  3FindTable(db, "
200db 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22  sqlite_sequence"
200dc 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71  , zDb) ){.    sq
200dd 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
200de 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
200df 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e   "UPDATE \"%w\".
200e0 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
200e1 73 65 74 20 6e 61 6d 65 20 3d 20 25 51 20 57 48  set name = %Q WH
200e2 45 52 45 20 6e 61 6d 65 20 3d 20 25 51 22 2c 0a  ERE name = %Q",.
200e3 20 20 20 20 20 20 20 20 7a 44 62 2c 20 7a 4e 61          zDb, zNa
200e4 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  me, pTab->zName)
200e5 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
200e6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
200e7 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 49  T_TRIGGER.  /* I
200e8 66 20 74 68 65 72 65 20 61 72 65 20 54 45 4d 50  f there are TEMP
200e9 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69   triggers on thi
200ea 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20  s table, modify 
200eb 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f  the sqlite_temp_
200ec 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61 62 6c  master.  ** tabl
200ed 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68 69 73  e. Don't do this
200ee 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
200ef 69 6e 67 20 41 4c 54 45 52 65 64 20 69 73 20 69  ing ALTERed is i
200f0 74 73 65 6c 66 20 6c 6f 63 61 74 65 64 20 69 6e  tself located in
200f1 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d 70 20 64  .  ** the temp d
200f2 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  atabase..  */.  
200f3 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72  if( (zWhere=wher
200f4 65 54 65 6d 70 54 72 69 67 67 65 72 73 28 70 50  eTempTriggers(pP
200f5 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20  arse, pTab))!=0 
200f6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  ){.    sqlite3Ne
200f7 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
200f8 2c 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 41  , .        "UPDA
200f9 54 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  TE sqlite_temp_m
200fa 61 73 74 65 72 20 53 45 54 20 22 0a 20 20 20 20  aster SET ".    
200fb 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73          "sql = s
200fc 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69  qlite_rename_tri
200fd 67 67 65 72 28 73 71 6c 2c 20 25 51 29 2c 20 22  gger(sql, %Q), "
200fe 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 74 62  .            "tb
200ff 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a 20 20  l_name = %Q ".  
20100 20 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45            "WHERE
20101 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20 7a 4e   %s;", zName, zN
20102 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20  ame, zWhere);.  
20103 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
20104 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 7d  db, zWhere);.  }
20105 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72  .#endif..  /* Dr
20106 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68  op and reload th
20107 65 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65  e internal table
20108 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65   schema. */.  re
20109 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28  loadTableSchema(
2010a 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 4e  pParse, pTab, zN
2010b 61 6d 65 29 3b 0a 0a 65 78 69 74 5f 72 65 6e 61  ame);..exit_rena
2010c 6d 65 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69  me_table:.  sqli
2010d 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
2010e 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 73 71  (db, pSrc);.  sq
2010f 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
20110 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  zName);.}.../*.*
20111 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
20112 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
20113 61 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20  an "ALTER TABLE 
20114 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65  ... ADD" stateme
20115 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 70  nt.** has been p
20116 61 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20  arsed. Argument 
20117 70 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73  pColDef contains
20118 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65   the text of the
20119 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64   new.** column d
2011a 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  efinition..**.**
2011b 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   The Table struc
2011c 74 75 72 65 20 70 50 61 72 73 65 2d 3e 70 4e 65  ture pParse->pNe
2011d 77 54 61 62 6c 65 20 77 61 73 20 65 78 74 65 6e  wTable was exten
2011e 64 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a  ded to include.*
2011f 2a 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  * the new column
20120 20 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 2e   during parsing.
20121 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20122 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
20123 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c  lterFinishAddCol
20124 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  umn(Parse *pPars
20125 65 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65  e, Token *pColDe
20126 66 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65  f){.  Table *pNe
20127 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
20128 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50 61 72 73  /* Copy of pPars
20129 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a  e->pNewTable */.
2012a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
2012b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2012c 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72  able being alter
2012d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  ed */.  int iDb;
2012e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2012f 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75    /* Database nu
20130 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  mber */.  const 
20131 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
20132 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
20133 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
20134 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20  char *zTab;     
20135 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d      /* Table nam
20136 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  e */.  char *zCo
20137 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
20138 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61   /* Null-termina
20139 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e  ted column defin
2013a 69 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d  ition */.  Colum
2013b 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20  n *pCol;        
2013c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
2013d 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72  column */.  Expr
2013e 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 20 20 20   *pDflt;        
2013f 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
20140 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e   value for the n
20141 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73  ew column */.  s
20142 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
20143 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
20144 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
20145 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d 20  ion; */..  db = 
20146 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
20147 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
20148 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
20149 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  ed ) return;.  p
2014a 4e 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  New = pParse->pN
2014b 65 77 54 61 62 6c 65 3b 0a 20 20 61 73 73 65 72  ewTable;.  asser
2014c 74 28 20 70 4e 65 77 20 29 3b 0a 0a 20 20 61 73  t( pNew );..  as
2014d 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
2014e 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
2014f 73 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d  s(db) );.  iDb =
20150 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
20151 49 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e  Index(db, pNew->
20152 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20  pSchema);.  zDb 
20153 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
20154 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 26  Name;.  zTab = &
20155 70 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b  pNew->zName[16];
20156 20 20 2f 2a 20 53 6b 69 70 20 74 68 65 20 22 73    /* Skip the "s
20157 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22  qlite_altertab_"
20158 20 70 72 65 66 69 78 20 6f 6e 20 74 68 65 20 6e   prefix on the n
20159 61 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c 20 3d 20  ame */.  pCol = 
2015a 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77  &pNew->aCol[pNew
2015b 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66  ->nCol-1];.  pDf
2015c 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  lt = pCol->pDflt
2015d 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ;.  pTab = sqlit
2015e 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
2015f 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20 20 61 73  zTab, zDb);.  as
20160 73 65 72 74 28 20 70 54 61 62 20 29 3b 0a 0a 23  sert( pTab );..#
20161 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20162 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
20163 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
20164 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
20165 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66  allback. */.  if
20166 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
20167 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
20168 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a  E_ALTER_TABLE, z
20169 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  Db, pTab->zName,
2016a 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
2016b 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  n;.  }.#endif.. 
2016c 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75   /* If the defau
2016d 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
2016e 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77 61 73 20   new column was 
2016f 73 70 65 63 69 66 69 65 64 20 77 69 74 68 20 61  specified with a
20170 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61 6c 20 4e   .  ** literal N
20171 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 70 44  ULL, then set pD
20172 66 6c 74 20 74 6f 20 30 2e 20 54 68 69 73 20 73  flt to 0. This s
20173 69 6d 70 6c 69 66 69 65 73 20 63 68 65 63 6b 69  implifies checki
20174 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61 6e 20 53  ng.  ** for an S
20175 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20  QL NULL default 
20176 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  below..  */.  if
20177 28 20 70 44 66 6c 74 20 26 26 20 70 44 66 6c 74  ( pDflt && pDflt
20178 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b  ->op==TK_NULL ){
20179 0a 20 20 20 20 70 44 66 6c 74 20 3d 20 30 3b 0a  .    pDflt = 0;.
2017a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
2017b 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 6f 6c  that the new col
2017c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70 65 63 69  umn is not speci
2017d 66 69 65 64 20 61 73 20 50 52 49 4d 41 52 59 20  fied as PRIMARY 
2017e 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 2e 0a 20  KEY or UNIQUE.. 
2017f 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   ** If there is 
20180 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74  a NOT NULL const
20181 72 61 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20  raint, then the 
20182 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  default value fo
20183 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d  r the.  ** colum
20184 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 4e 55  n must not be NU
20185 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  LL..  */.  if( p
20186 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 20 29  Col->isPrimKey )
20187 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
20188 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43  orMsg(pParse, "C
20189 61 6e 6e 6f 74 20 61 64 64 20 61 20 50 52 49 4d  annot add a PRIM
2018a 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 22 29  ARY KEY column")
2018b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
2018c 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49  }.  if( pNew->pI
2018d 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69  ndex ){.    sqli
2018e 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2018f 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20  se, "Cannot add 
20190 61 20 55 4e 49 51 55 45 20 63 6f 6c 75 6d 6e 22  a UNIQUE column"
20191 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
20192 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e 6e   }.  if( pCol->n
20193 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44 66 6c 74  otNull && !pDflt
20194 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
20195 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
20196 0a 20 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74  .        "Cannot
20197 20 61 64 64 20 61 20 4e 4f 54 20 4e 55 4c 4c 20   add a NOT NULL 
20198 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64 65 66 61  column with defa
20199 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c 4c 22 29  ult value NULL")
2019a 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
2019b 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  }..  /* Ensure t
2019c 68 65 20 64 65 66 61 75 6c 74 20 65 78 70 72 65  he default expre
2019d 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65 74 68 69  ssion is somethi
2019e 6e 67 20 74 68 61 74 20 73 71 6c 69 74 65 33 56  ng that sqlite3V
2019f 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29 0a 20  alueFromExpr(). 
201a0 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c 65 20 28   ** can handle (
201a1 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52 45 4e 54  i.e. not CURRENT
201a2 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20 20 2a 2f  _TIME etc.).  */
201a3 0a 20 20 69 66 28 20 70 44 66 6c 74 20 29 7b 0a  .  if( pDflt ){.
201a4 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
201a5 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20 69 66 28  e *pVal;.    if(
201a6 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
201a7 6d 45 78 70 72 28 64 62 2c 20 70 44 66 6c 74 2c  mExpr(db, pDflt,
201a8 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
201a9 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2c 20 26  LITE_AFF_NONE, &
201aa 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 64  pVal) ){.      d
201ab 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
201ac 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
201ad 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  n;.    }.    if(
201ae 20 21 70 56 61 6c 20 29 7b 0a 20 20 20 20 20 20   !pVal ){.      
201af 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
201b0 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20  pParse, "Cannot 
201b1 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 77 69 74  add a column wit
201b2 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 20 64  h non-constant d
201b3 65 66 61 75 6c 74 22 29 3b 0a 20 20 20 20 20 20  efault");.      
201b4 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
201b5 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
201b6 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 0a 20  ee(pVal);.  }.. 
201b7 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 43   /* Modify the C
201b8 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
201b9 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a 43 6f 6c  ement. */.  zCol
201ba 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
201bb 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70  Dup(db, (char*)p
201bc 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43 6f 6c 44  ColDef->z, pColD
201bd 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 7a 43  ef->n);.  if( zC
201be 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  ol ){.    char *
201bf 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b 70 43 6f  zEnd = &zCol[pCo
201c0 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20 20 20 20  lDef->n-1];.    
201c1 77 68 69 6c 65 28 20 28 7a 45 6e 64 3e 7a 43 6f  while( (zEnd>zCo
201c2 6c 20 26 26 20 2a 7a 45 6e 64 3d 3d 27 3b 27 29  l && *zEnd==';')
201c3 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 73 70 61   || sqlite3Isspa
201c4 63 65 28 2a 7a 45 6e 64 29 20 29 7b 0a 20 20 20  ce(*zEnd) ){.   
201c5 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30     *zEnd-- = '\0
201c6 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  ';.    }.    sql
201c7 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
201c8 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
201c9 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e   "UPDATE \"%w\".
201ca 25 73 20 53 45 54 20 22 0a 20 20 20 20 20 20 20  %s SET ".       
201cb 20 20 20 22 73 71 6c 20 3d 20 73 75 62 73 74 72     "sql = substr
201cc 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c 20 27 2c  (sql,1,%d) || ',
201cd 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73   ' || %Q || subs
201ce 74 72 28 73 71 6c 2c 25 64 29 20 22 0a 20 20 20  tr(sql,%d) ".   
201cf 20 20 20 20 20 22 57 48 45 52 45 20 74 79 70 65       "WHERE type
201d0 20 3d 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e   = 'table' AND n
201d1 61 6d 65 20 3d 20 25 51 22 2c 20 0a 20 20 20 20  ame = %Q", .    
201d2 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41    zDb, SCHEMA_TA
201d3 42 4c 45 28 69 44 62 29 2c 20 70 4e 65 77 2d 3e  BLE(iDb), pNew->
201d4 61 64 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43  addColOffset, zC
201d5 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c  ol, pNew->addCol
201d6 4f 66 66 73 65 74 2b 31 2c 0a 20 20 20 20 20 20  Offset+1,.      
201d7 7a 54 61 62 0a 20 20 20 20 29 3b 0a 20 20 20 20  zTab.    );.    
201d8 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
201d9 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20  , zCol);.  }..  
201da 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c  /* If the defaul
201db 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e  t value of the n
201dc 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c  ew column is NUL
201dd 4c 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20  L, then set the 
201de 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61 74  file.  ** format
201df 20 74 6f 20 32 2e 20 49 66 20 74 68 65 20 64 65   to 2. If the de
201e0 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74  fault value of t
201e1 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73  he new column is
201e2 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a 20   not NULL,.  ** 
201e3 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  the file format 
201e4 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f 0a  becomes 3..  */.
201e5 20 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d    sqlite3Minimum
201e6 46 69 6c 65 46 6f 72 6d 61 74 28 70 50 61 72 73  FileFormat(pPars
201e7 65 2c 20 69 44 62 2c 20 70 44 66 6c 74 20 3f 20  e, iDb, pDflt ? 
201e8 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52 65  3 : 2);..  /* Re
201e9 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  load the schema 
201ea 6f 66 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20  of the modified 
201eb 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c 6f  table. */.  relo
201ec 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50  adTableSchema(pP
201ed 61 72 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62  arse, pTab, pTab
201ee 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a  ->zName);.}../*.
201ef 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
201f0 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
201f1 65 20 70 61 72 73 65 72 20 61 66 74 65 72 20 74  e parser after t
201f2 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 6e  he table-name in
201f3 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 41  .** an "ALTER TA
201f4 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 3e  BLE <table-name>
201f5 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 20   ADD" statement 
201f6 69 73 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d  is parsed. Argum
201f7 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 20  ent .** pSrc is 
201f8 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f 66  the full-name of
201f9 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
201fa 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   altered..**.** 
201fb 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b  This routine mak
201fc 65 73 20 61 20 28 70 61 72 74 69 61 6c 29 20 63  es a (partial) c
201fd 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65  opy of the Table
201fe 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 6f   structure.** fo
201ff 72 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  r the table bein
20200 67 20 61 6c 74 65 72 65 64 20 61 6e 64 20 73 65  g altered and se
20201 74 73 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62  ts Parse.pNewTab
20202 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74  le to point.** t
20203 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 63  o it. Routines c
20204 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
20205 73 65 72 20 61 73 20 74 68 65 20 63 6f 6c 75 6d  ser as the colum
20206 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20  n definition.** 
20207 69 73 20 70 61 72 73 65 64 20 28 69 2e 65 2e 20  is parsed (i.e. 
20208 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
20209 28 29 29 20 61 64 64 20 74 68 65 20 6e 65 77 20  ()) add the new 
2020a 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 0a  Column data to .
2020b 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20 54 68 65  ** the copy. The
2020c 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62   copy of the Tab
2020d 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  le structure is 
2020e 64 65 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 6e  deleted by token
2020f 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 20  ize.c .** after 
20210 70 61 72 73 69 6e 67 20 69 73 20 66 69 6e 69 73  parsing is finis
20211 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 69  hed..**.** Routi
20212 6e 65 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  ne sqlite3AlterF
20213 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29  inishAddColumn()
20214 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
20215 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63  to complete.** c
20216 6f 64 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52  oding the "ALTER
20217 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20   TABLE ... ADD" 
20218 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51  statement..*/.SQ
20219 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2021a 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65  d sqlite3AlterBe
2021b 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72  ginAddColumn(Par
2021c 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
2021d 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61  ist *pSrc){.  Ta
2021e 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61 62  ble *pNew;.  Tab
2021f 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65  le *pTab;.  Vdbe
20220 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a   *v;.  int iDb;.
20221 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
20222 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33  Alloc;.  sqlite3
20223 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
20224 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70  b;..  /* Look up
20225 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
20226 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a 20 20 61   altered. */.  a
20227 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
20228 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  NewTable==0 );. 
20229 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2022a 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
2022b 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69 66  exes(db) );.  if
2022c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2022d 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62  ed ) goto exit_b
2022e 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
2022f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
20230 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
20231 72 73 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b  rse, 0, pSrc->a[
20232 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e  0].zName, pSrc->
20233 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b  a[0].zDatabase);
20234 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 20 67  .  if( !pTab ) g
20235 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
20236 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66 6e  dd_column;..#ifn
20237 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
20238 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69  VIRTUALTABLE.  i
20239 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2023a 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  b) ){.    sqlite
2023b 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2023c 2c 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65  , "virtual table
2023d 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74  s may not be alt
2023e 65 72 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f  ered");.    goto
2023f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f   exit_begin_add_
20240 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e 64  column;.  }.#end
20241 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  if..  /* Make su
20242 72 65 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61  re this is not a
20243 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 41 4c 54  n attempt to ALT
20244 45 52 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20  ER a view. */.  
20245 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
20246 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
20247 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
20248 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63   "Cannot add a c
20249 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69 65 77 22  olumn to a view"
2024a 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
2024b 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d  _begin_add_colum
2024c 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  n;.  }..  assert
2024d 28 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66  ( pTab->addColOf
2024e 66 73 65 74 3e 30 20 29 3b 0a 20 20 69 44 62 20  fset>0 );.  iDb 
2024f 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
20250 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
20251 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a  >pSchema);..  /*
20252 20 50 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74   Put a copy of t
20253 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 20  he Table struct 
20254 69 6e 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62  in Parse.pNewTab
20255 6c 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20  le for the.  ** 
20256 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
20257 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20  () function and 
20258 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66  friends to modif
20259 79 2e 20 20 42 75 74 20 6d 6f 64 69 66 79 0a 20  y.  But modify. 
2025a 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 62 79 20   ** the name by 
2025b 61 64 64 69 6e 67 20 61 6e 20 22 73 71 6c 69 74  adding an "sqlit
2025c 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 70 72 65  e_altertab_" pre
2025d 66 69 78 2e 20 20 42 79 20 61 64 64 69 6e 67 20  fix.  By adding 
2025e 74 68 69 73 0a 20 20 2a 2a 20 70 72 65 66 69 78  this.  ** prefix
2025f 2c 20 77 65 20 69 6e 73 75 72 65 20 74 68 61 74  , we insure that
20260 20 74 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 6e   the name will n
20261 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20  ot collide with 
20262 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a  an existing.  **
20263 20 74 61 62 6c 65 20 62 65 63 61 75 73 65 20 75   table because u
20264 73 65 72 20 74 61 62 6c 65 20 61 72 65 20 6e 6f  ser table are no
20265 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 68 61 76  t allowed to hav
20266 65 20 74 68 65 20 22 73 71 6c 69 74 65 5f 22 0a  e the "sqlite_".
20267 20 20 2a 2a 20 70 72 65 66 69 78 20 6f 6e 20 74    ** prefix on t
20268 68 65 69 72 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a  heir name..  */.
20269 20 20 70 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a    pNew = (Table*
2026a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  )sqlite3DbMalloc
2026b 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
2026c 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21  Table));.  if( !
2026d 70 4e 65 77 20 29 20 67 6f 74 6f 20 65 78 69 74  pNew ) goto exit
2026e 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d  _begin_add_colum
2026f 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  n;.  pParse->pNe
20270 77 54 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20  wTable = pNew;. 
20271 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b   pNew->nRef = 1;
20272 0a 20 20 70 4e 65 77 2d 3e 64 62 4d 65 6d 20 3d  .  pNew->dbMem =
20273 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3b 0a 20 20   pTab->dbMem;.  
20274 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54 61  pNew->nCol = pTa
20275 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 65 72  b->nCol;.  asser
20276 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30 20  t( pNew->nCol>0 
20277 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 28  );.  nAlloc = ((
20278 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f 38  (pNew->nCol-1)/8
20279 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65 72 74  )*8)+8;.  assert
2027a 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d 3e  ( nAlloc>=pNew->
2027b 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25 38  nCol && nAlloc%8
2027c 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70 4e  ==0 && nAlloc-pN
2027d 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20 20  ew->nCol<8 );.  
2027e 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 43 6f  pNew->aCol = (Co
2027f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44 62 4d  lumn*)sqlite3DbM
20280 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
20281 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e 41 6c  zeof(Column)*nAl
20282 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e  loc);.  pNew->zN
20283 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
20284 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65  intf(db, "sqlite
20285 5f 61 6c 74 65 72 74 61 62 5f 25 73 22 2c 20 70  _altertab_%s", p
20286 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69  Tab->zName);.  i
20287 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c  f( !pNew->aCol |
20288 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29  | !pNew->zName )
20289 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
2028a 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
2028b 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
2028c 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a  add_column;.  }.
2028d 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 61    memcpy(pNew->a
2028e 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c  Col, pTab->aCol,
2028f 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a   sizeof(Column)*
20290 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66  pNew->nCol);.  f
20291 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e  or(i=0; i<pNew->
20292 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
20293 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26  Column *pCol = &
20294 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20  pNew->aCol[i];. 
20295 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d     pCol->zName =
20296 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
20297 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  (db, pCol->zName
20298 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f  );.    pCol->zCo
20299 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c  ll = 0;.    pCol
2029a 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 20  ->zType = 0;.   
2029b 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30   pCol->pDflt = 0
2029c 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 53  ;.  }.  pNew->pS
2029d 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
2029e 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20  iDb].pSchema;.  
2029f 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73  pNew->addColOffs
202a0 65 74 20 3d 20 70 54 61 62 2d 3e 61 64 64 43 6f  et = pTab->addCo
202a1 6c 4f 66 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d  lOffset;.  pNew-
202a2 3e 6e 52 65 66 20 3d 20 31 3b 0a 0a 20 20 2f 2a  >nRef = 1;..  /*
202a3 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63   Begin a transac
202a4 74 69 6f 6e 20 61 6e 64 20 69 6e 63 72 65 6d 65  tion and increme
202a5 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  nt the schema co
202a6 6f 6b 69 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69  okie.  */.  sqli
202a7 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
202a8 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
202a9 2c 20 69 44 62 29 3b 0a 20 20 76 20 3d 20 73 71  , iDb);.  v = sq
202aa 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
202ab 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 29  rse);.  if( !v )
202ac 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
202ad 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 73  _add_column;.  s
202ae 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
202af 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
202b0 0a 0a 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  ..exit_begin_add
202b1 5f 63 6f 6c 75 6d 6e 3a 0a 20 20 73 71 6c 69 74  _column:.  sqlit
202b2 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
202b3 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 72 65 74  db, pSrc);.  ret
202b4 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f  urn;.}.#endif  /
202b5 2a 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54  * SQLITE_ALTER_T
202b6 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ABLE */../******
202b7 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
202b8 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  alter.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 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
202bc 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
202bd 69 6c 65 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a  ile analyze.c **
202be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
202c1 30 30 35 20 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a  005 July 8.**.**
202c2 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
202c3 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
202c4 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
202c5 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
202c6 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
202c7 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
202c8 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
202c9 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
202ca 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
202cb 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
202cc 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
202cd 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
202ce 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
202cf 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
202d0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
202d1 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
202d2 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
202d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
202d8 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
202d9 63 6f 64 65 20 61 73 73 6f 63 69 61 74 65 64 20  code associated 
202da 77 69 74 68 20 74 68 65 20 41 4e 41 4c 59 5a 45  with the ANALYZE
202db 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
202dc 40 28 23 29 20 24 49 64 3a 20 61 6e 61 6c 79 7a  @(#) $Id: analyz
202dd 65 2e 63 2c 76 20 31 2e 35 31 20 32 30 30 39 2f  e.c,v 1.51 2009/
202de 30 32 2f 32 38 20 31 30 3a 34 37 3a 34 32 20 64  02/28 10:47:42 d
202df 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
202e0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
202e1 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a  TE_OMIT_ANALYZE.
202e2 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
202e3 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
202e4 64 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 68  de that opens th
202e5 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
202e6 61 62 6c 65 20 6f 6e 20 63 75 72 73 6f 72 0a 2a  able on cursor.*
202e7 2a 20 69 53 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a  * iStatCur..**.*
202e8 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f  * If the sqlite_
202e9 73 74 61 74 31 20 74 61 62 6c 65 73 20 64 6f 65  stat1 tables doe
202ea 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  s not previously
202eb 20 65 78 69 73 74 2c 20 69 74 20 69 73 20 63 72   exist, it is cr
202ec 65 61 74 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20  eated..** If it 
202ed 64 6f 65 73 20 70 72 65 76 69 6f 75 73 6c 79 20  does previously 
202ee 65 78 69 73 74 2c 20 61 6c 6c 20 65 6e 74 69 72  exist, all entir
202ef 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
202f0 74 68 20 74 61 62 6c 65 20 7a 57 68 65 72 65 0a  th table zWhere.
202f1 2a 2a 20 61 72 65 20 72 65 6d 6f 76 65 64 2e 20  ** are removed. 
202f2 20 49 66 20 7a 57 68 65 72 65 3d 3d 30 20 74 68   If zWhere==0 th
202f3 65 6e 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 61  en all entries a
202f4 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73  re removed..*/.s
202f5 74 61 74 69 63 20 76 6f 69 64 20 6f 70 65 6e 53  tatic void openS
202f6 74 61 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73  tatTable(.  Pars
202f7 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
202f8 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
202f9 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
202fa 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  iDb,            
202fb 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
202fc 61 73 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69  ase we are looki
202fd 6e 67 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ng in */.  int i
202fe 53 74 61 74 43 75 72 2c 20 20 20 20 20 20 20 20  StatCur,        
202ff 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73     /* Open the s
20300 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
20301 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72  e on this cursor
20302 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
20303 20 2a 7a 57 68 65 72 65 20 20 20 20 20 20 2f 2a   *zWhere      /*
20304 20 44 65 6c 65 74 65 20 65 6e 74 72 69 65 73 20   Delete entries 
20305 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
20306 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b  this table */.){
20307 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
20308 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
20309 62 20 2a 70 44 62 3b 0a 20 20 69 6e 74 20 69 52  b *pDb;.  int iR
2030a 6f 6f 74 50 61 67 65 3b 0a 20 20 75 38 20 63 72  ootPage;.  u8 cr
2030b 65 61 74 65 53 74 61 74 31 20 3d 20 30 3b 0a 20  eateStat1 = 0;. 
2030c 20 54 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20   Table *pStat;. 
2030d 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
2030e 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2030f 29 3b 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  );..  if( v==0 )
20310 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
20311 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
20312 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64  oldsAllMutexes(d
20313 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  b) );.  assert( 
20314 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
20315 3d 3d 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20  ==db );.  pDb = 
20316 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
20317 20 69 66 28 20 28 70 53 74 61 74 20 3d 20 73 71   if( (pStat = sq
20318 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
20319 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31  b, "sqlite_stat1
2031a 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d  ", pDb->zName))=
2031b 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  =0 ){.    /* The
2031c 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
2031d 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74 20 65 78  bles does not ex
2031e 69 73 74 2e 20 20 43 72 65 61 74 65 20 69 74 2e  ist.  Create it.
2031f 20 20 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74    .    ** Note t
20320 68 61 74 20 61 20 73 69 64 65 2d 65 66 66 65 63  hat a side-effec
20321 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  t of the CREATE 
20322 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
20323 69 73 20 74 6f 20 6c 65 61 76 65 0a 20 20 20 20  is to leave.    
20324 2a 2a 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20  ** the rootpage 
20325 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
20326 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61   in register pPa
20327 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54  rse->regRoot.  T
20328 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d  his is.    ** im
20329 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20  portant because 
2032a 74 68 65 20 4f 70 65 6e 57 72 69 74 65 20 6f 70  the OpenWrite op
2032b 63 6f 64 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20  code below will 
2032c 62 65 20 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a  be needing it. *
2032d 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  /.    sqlite3Nes
2032e 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
2032f 0a 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54  .      "CREATE T
20330 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73  ABLE %Q.sqlite_s
20331 74 61 74 31 28 74 62 6c 2c 69 64 78 2c 73 74 61  tat1(tbl,idx,sta
20332 74 29 22 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e  t)",.      pDb->
20333 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20  zName.    );.   
20334 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 50 61   iRootPage = pPa
20335 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20  rse->regRoot;.  
20336 20 20 63 72 65 61 74 65 53 74 61 74 31 20 3d 20    createStat1 = 
20337 31 3b 20 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f  1;  /* Cause roo
20338 74 70 61 67 65 20 74 6f 20 62 65 20 74 61 6b 65  tpage to be take
20339 6e 20 66 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74  n from top of st
2033a 61 63 6b 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69  ack */.  }else i
2033b 66 28 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20  f( zWhere ){.   
2033c 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73   /* The sqlite_s
2033d 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74  tat1 table exist
2033e 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 65  s.  Delete all e
2033f 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65  ntries associate
20340 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68  d with.    ** th
20341 65 20 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20  e table zWhere. 
20342 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  */.    sqlite3Ne
20343 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
20344 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54 45  ,.       "DELETE
20345 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f   FROM %Q.sqlite_
20346 73 74 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d  stat1 WHERE tbl=
20347 25 51 22 2c 0a 20 20 20 20 20 20 20 70 44 62 2d  %Q",.       pDb-
20348 3e 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20  >zName, zWhere. 
20349 20 20 20 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50     );.    iRootP
2034a 61 67 65 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75  age = pStat->tnu
2034b 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  m;.  }else{.    
2034c 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74  /* The sqlite_st
2034d 61 74 31 20 74 61 62 6c 65 20 61 6c 72 65 61 64  at1 table alread
2034e 79 20 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74  y exists.  Delet
2034f 65 20 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20  e all rows. */. 
20350 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d 20 70     iRootPage = p
20351 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20  Stat->tnum;.    
20352 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20353 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70  2(v, OP_Clear, p
20354 53 74 61 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  Stat->tnum, iDb)
20355 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e  ;.  }..  /* Open
20356 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
20357 31 20 74 61 62 6c 65 20 66 6f 72 20 77 72 69 74  1 table for writ
20358 69 6e 67 2e 20 55 6e 6c 65 73 73 20 69 74 20 77  ing. Unless it w
20359 61 73 20 63 72 65 61 74 65 64 0a 20 20 2a 2a 20  as created.  ** 
2035a 62 79 20 74 68 69 73 20 76 64 62 65 20 70 72 6f  by this vdbe pro
2035b 67 72 61 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f  gram, lock it fo
2035c 72 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 65  r writing at the
2035d 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65   shared-cache le
2035e 76 65 6c 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68  vel. .  ** If th
2035f 69 73 20 76 64 62 65 20 64 69 64 20 63 72 65 61  is vdbe did crea
20360 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  te the sqlite_st
20361 61 74 31 20 74 61 62 6c 65 2c 20 74 68 65 6e 20  at1 table, then 
20362 69 74 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20  it must have .  
20363 2a 2a 20 61 6c 72 65 61 64 79 20 6f 62 74 61 69  ** already obtai
20364 6e 65 64 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63  ned a schema-loc
20365 6b 2c 20 6d 61 6b 69 6e 67 20 74 68 65 20 77 72  k, making the wr
20366 69 74 65 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61  ite-lock redunda
20367 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  nt..  */.  if( !
20368 63 72 65 61 74 65 53 74 61 74 31 20 29 7b 0a 20  createStat1 ){. 
20369 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c     sqlite3TableL
2036a 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ock(pParse, iDb,
2036b 20 69 52 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22   iRootPage, 1, "
2036c 73 71 6c 69 74 65 5f 73 74 61 74 31 22 29 3b 0a  sqlite_stat1");.
2036d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
2036e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  eAddOp3(v, OP_Op
2036f 65 6e 57 72 69 74 65 2c 20 69 53 74 61 74 43 75  enWrite, iStatCu
20370 72 2c 20 69 52 6f 6f 74 50 61 67 65 2c 20 69 44  r, iRootPage, iD
20371 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
20372 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
20373 20 28 63 68 61 72 20 2a 29 33 2c 20 50 34 5f 49   (char *)3, P4_I
20374 4e 54 33 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  NT32);.  sqlite3
20375 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
20376 63 72 65 61 74 65 53 74 61 74 31 29 3b 0a 7d 0a  createStat1);.}.
20377 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
20378 63 6f 64 65 20 74 6f 20 64 6f 20 61 6e 20 61 6e  code to do an an
20379 61 6c 79 73 69 73 20 6f 66 20 61 6c 6c 20 69 6e  alysis of all in
2037a 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64  dices associated
2037b 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c   with.** a singl
2037c 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  e table..*/.stat
2037d 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 4f  ic void analyzeO
2037e 6e 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  neTable(.  Parse
2037f 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50   *pParse,   /* P
20380 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
20381 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
20382 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 77 68 6f      /* Table who
20383 73 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 74  se indices are t
20384 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  o be analyzed */
20385 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 2c  .  int iStatCur,
20386 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
20387 56 64 62 65 43 75 72 73 6f 72 20 74 68 61 74 20  VdbeCursor that 
20388 77 72 69 74 65 73 20 74 68 65 20 73 71 6c 69 74  writes the sqlit
20389 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f  e_stat1 table */
2038a 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20  .  int iMem     
2038b 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65      /* Available
2038c 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
2038d 73 20 62 65 67 69 6e 20 68 65 72 65 20 2a 2f 0a  s begin here */.
2038e 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  ){.  Index *pIdx
2038f 3b 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65  ;     /* An inde
20390 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61 6c 79  x to being analy
20391 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64  zed */.  int iId
20392 78 43 75 72 3b 20 20 20 20 20 2f 2a 20 49 6e 64  xCur;     /* Ind
20393 65 78 20 6f 66 20 56 64 62 65 43 75 72 73 6f 72  ex of VdbeCursor
20394 20 66 6f 72 20 69 6e 64 65 78 20 62 65 69 6e 67   for index being
20395 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69   analyzed */.  i
20396 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt nCol;        
20397 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
20398 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65  umns in the inde
20399 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  x */.  Vdbe *v; 
2039a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
2039b 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62  irtual machine b
2039c 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 2a 2f  eing built up */
2039d 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2039e 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2039f 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70  ter */.  int top
203a0 4f 66 4c 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65  OfLoop;   /* The
203a1 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
203a2 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66 4c   */.  int endOfL
203a3 6f 6f 70 3b 20 20 20 2f 2a 20 54 68 65 20 65 6e  oop;   /* The en
203a4 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  d of the loop */
203a5 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20  .  int addr;    
203a6 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 72 65      /* The addre
203a7 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 75 63  ss of an instruc
203a8 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44  tion */.  int iD
203a9 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  b;         /* In
203aa 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20  dex of database 
203ab 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20  containing pTab 
203ac 2a 2f 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  */..  v = sqlite
203ad 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
203ae 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c 7c 20  ;.  if( v==0 || 
203af 70 54 61 62 3d 3d 30 20 7c 7c 20 70 54 61 62 2d  pTab==0 || pTab-
203b0 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20  >pIndex==0 ){.  
203b1 20 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61 6c 79    /* Do no analy
203b2 73 69 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74  sis for tables t
203b3 68 61 74 20 68 61 76 65 20 6e 6f 20 69 6e 64 69  hat have no indi
203b4 63 65 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ces */.    retur
203b5 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
203b6 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
203b7 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61  dsAllMutexes(pPa
203b8 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44  rse->db) );.  iD
203b9 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
203ba 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
203bb 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
203bc 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ma);.  assert( i
203bd 44 62 3e 3d 30 20 29 3b 0a 23 69 66 6e 64 65 66  Db>=0 );.#ifndef
203be 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
203bf 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28  HORIZATION.  if(
203c0 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
203c1 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
203c2 5f 41 4e 41 4c 59 5a 45 2c 20 70 54 61 62 2d 3e  _ANALYZE, pTab->
203c3 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20  zName, 0,.      
203c4 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b  pParse->db->aDb[
203c5 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a  iDb].zName ) ){.
203c6 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
203c7 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 73 74  #endif..  /* Est
203c8 61 62 6c 69 73 68 20 61 20 72 65 61 64 2d 6c 6f  ablish a read-lo
203c9 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20  ck on the table 
203ca 61 74 20 74 68 65 20 73 68 61 72 65 64 2d 63 61  at the shared-ca
203cb 63 68 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a 20 20  che level. */.  
203cc 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
203cd 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
203ce 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61  ab->tnum, 0, pTa
203cf 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 49  b->zName);..  iI
203d0 64 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e  dxCur = pParse->
203d1 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49  nTab++;.  for(pI
203d2 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
203d3 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
203d4 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 4b 65  ->pNext){.    Ke
203d5 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71  yInfo *pKey = sq
203d6 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
203d7 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b  o(pParse, pIdx);
203d8 0a 20 20 20 20 69 6e 74 20 72 65 67 46 69 65 6c  .    int regFiel
203d9 64 73 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74  ds;    /* Regist
203da 65 72 20 62 6c 6f 63 6b 20 66 6f 72 20 62 75 69  er block for bui
203db 6c 64 69 6e 67 20 72 65 63 6f 72 64 73 20 2a 2f  lding records */
203dc 0a 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b  .    int regRec;
203dd 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
203de 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6d 70 6c  er holding compl
203df 65 74 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20  eted record */. 
203e0 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 3b 20     int regTemp; 
203e1 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
203e2 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20 2a  y use register *
203e3 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 43 6f 6c  /.    int regCol
203e4 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ;       /* Conte
203e5 6e 74 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 66  nt of a column f
203e6 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 62 65  rom the table be
203e7 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a  ing analyzed */.
203e8 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64      int regRowid
203e9 3b 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 66  ;     /* Rowid f
203ea 6f 72 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  or the inserted 
203eb 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e  record */.    in
203ec 74 20 72 65 67 46 32 3b 0a 0a 20 20 20 20 2f 2a  t regF2;..    /*
203ed 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72 20 74   Open a cursor t
203ee 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 62  o the index to b
203ef 65 20 61 6e 61 6c 79 7a 65 64 0a 20 20 20 20 2a  e analyzed.    *
203f0 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  /.    assert( iD
203f1 62 3d 3d 73 71 6c 69 74 65 33 53 63 68 65 6d 61  b==sqlite3Schema
203f2 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
203f3 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d  db, pIdx->pSchem
203f4 61 29 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d  a) );.    nCol =
203f5 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a   pIdx->nColumn;.
203f6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
203f7 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
203f8 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70  Read, iIdxCur, p
203f9 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  Idx->tnum, iDb,.
203fa 20 20 20 20 20 20 20 20 28 63 68 61 72 20 2a 29          (char *)
203fb 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  pKey, P4_KEYINFO
203fc 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56  _HANDOFF);.    V
203fd 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
203fe 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  %s", pIdx->zName
203ff 29 29 3b 0a 20 20 20 20 72 65 67 46 69 65 6c 64  ));.    regField
20400 73 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 3b  s = iMem+nCol*2;
20401 0a 20 20 20 20 72 65 67 54 65 6d 70 20 3d 20 72  .    regTemp = r
20402 65 67 52 6f 77 69 64 20 3d 20 72 65 67 43 6f 6c  egRowid = regCol
20403 20 3d 20 72 65 67 46 69 65 6c 64 73 2b 33 3b 0a   = regFields+3;.
20404 20 20 20 20 72 65 67 52 65 63 20 3d 20 72 65 67      regRec = reg
20405 43 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28 20 72  Col+1;.    if( r
20406 65 67 52 65 63 3e 70 50 61 72 73 65 2d 3e 6e 4d  egRec>pParse->nM
20407 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  em ){.      pPar
20408 73 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65  se->nMem = regRe
20409 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  c;.    }..    /*
2040a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 61 72   Memory cells ar
2040b 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77  e used as follow
2040c 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s:.    **.    **
2040d 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a 20 20      mem[iMem]:  
2040e 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 74             The t
2040f 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72  otal number of r
20410 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ows in the table
20411 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b  ..    **    mem[
20412 69 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 20 20  iMem+1]:        
20413 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73     Number of dis
20414 74 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20  tinct values in 
20415 63 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20  column 1.    ** 
20416 20 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20     ....    **   
20417 20 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a   mem[iMem+nCol]:
20418 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f          Number o
20419 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65  f distinct value
2041a 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20  s in column N.  
2041b 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d    **    mem[iMem
2041c 2b 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 20 4c  +nCol+1]       L
2041d 61 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c  ast observed val
2041e 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 0a 20  ue of column 1. 
2041f 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 20     **    ....   
20420 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b   **    mem[iMem+
20421 6e 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 4c 61  nCol+nCol]:   La
20422 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75  st observed valu
20423 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20  e of column N.  
20424 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c    **.    ** Cell
20425 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69  s iMem through i
20426 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69  Mem+nCol are ini
20427 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 20  tialized to 0.  
20428 54 68 65 20 6f 74 68 65 72 73 0a 20 20 20 20 2a  The others.    *
20429 2a 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65  * are initialize
2042a 64 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a  d to NULL..    *
2042b 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
2042c 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <=nCol; i++){.  
2042d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2042e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2042f 67 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b  ger, 0, iMem+i);
20430 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
20431 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
20432 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
20433 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
20434 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43  Null, 0, iMem+nC
20435 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a  ol+i+1);.    }..
20436 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 61 6e      /* Do the an
20437 61 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f 0a 20  alysis..    */. 
20438 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73     endOfLoop = s
20439 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
2043a 62 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  bel(v);.    sqli
2043b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2043c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78   OP_Rewind, iIdx
2043d 43 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  Cur, endOfLoop);
2043e 0a 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d  .    topOfLoop =
2043f 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
20440 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
20441 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20442 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
20443 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 66 6f  iMem, 1);.    fo
20444 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
20445 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
20446 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
20447 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43  OP_Column, iIdxC
20448 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b 0a  ur, i, regCol);.
20449 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2044a 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65  eAddOp3(v, OP_Ne
2044b 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65  , regCol, 0, iMe
2044c 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20  m+nCol+i+1);.   
2044d 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20     /**** TODO:  
2044e 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  add collating se
2044f 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20  quence *****/.  
20450 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
20451 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
20452 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
20453 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
20454 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
20455 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c  _Goto, 0, endOfL
20456 6f 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  oop);.    for(i=
20457 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
20458 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
20459 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 6f  beJumpHere(v, to
2045a 70 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69 20 2b  pOfLoop + 2*(i +
2045b 20 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   1));.      sqli
2045c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2045d 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d   OP_AddImm, iMem
2045e 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20  +i+1, 1);.      
2045f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20460 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
20461 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d  iIdxCur, i, iMem
20462 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20  +nCol+i+1);.    
20463 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
20464 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
20465 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20   endOfLoop);.   
20466 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20467 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  p2(v, OP_Next, i
20468 49 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f  IdxCur, topOfLoo
20469 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  p);.    sqlite3V
2046a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2046b 43 6c 6f 73 65 2c 20 69 49 64 78 43 75 72 29 3b  Close, iIdxCur);
2046c 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74  ..    /* Store t
2046d 68 65 20 72 65 73 75 6c 74 73 2e 20 20 0a 20 20  he results.  .  
2046e 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
2046f 72 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67  result is a sing
20470 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71  le row of the sq
20471 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
20472 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20  .  The first.   
20473 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20   ** two columns 
20474 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  are the names of
20475 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69   the table and i
20476 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64  ndex.  The third
20477 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69   column.    ** i
20478 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f  s a string compo
20479 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66  sed of a list of
2047a 20 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74   integer statist
2047b 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20  ics about the.  
2047c 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65    ** index.  The
2047d 20 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69   first integer i
2047e 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68  n the list is th
2047f 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
20480 66 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a  f entries.    **
20481 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20   in the index.  
20482 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64  There is one add
20483 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20  itional integer 
20484 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20  in the list for 
20485 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75  each.    ** colu
20486 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  mn of the table.
20487 20 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61    This additiona
20488 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67  l integer is a g
20489 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79  uess of how many
2048a 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20  .    ** rows of 
2048b 74 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e  the table the in
2048c 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e  dex will select.
2048d 20 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f    If D is the co
2048e 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a  unt of distinct.
2048f 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e      ** values an
20490 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c  d K is the total
20491 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c   number of rows,
20492 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65   then the intege
20493 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20  r is computed.  
20494 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a    ** as:.    **.
20495 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20      **        I 
20496 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20  = (K+D-1)/D.    
20497 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d  **.    ** If K==
20498 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20  0 then no entry 
20499 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65  is made into the
2049a 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
2049b 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66  ble.  .    ** If
2049c 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20   K>0 then it is 
2049d 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20  always the case 
2049e 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73  the D>0 so divis
2049f 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20  ion by zero.    
204a0 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73  ** is never poss
204a1 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ible..    */.   
204a2 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
204a3 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
204a4 49 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20  IfNot, iMem);.  
204a5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
204a6 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
204a7 38 2c 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2c  8, 0, regFields,
204a8 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   0, pTab->zName,
204a9 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
204aa 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
204ab 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67  _String8, 0, reg
204ac 46 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70 49 64  Fields+1, 0, pId
204ad 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  x->zName, 0);.  
204ae 20 20 72 65 67 46 32 20 3d 20 72 65 67 46 69 65    regF2 = regFie
204af 6c 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74  lds+2;.    sqlit
204b0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
204b1 4f 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20  OP_SCopy, iMem, 
204b2 72 65 67 46 32 29 3b 0a 20 20 20 20 66 6f 72 28  regF2);.    for(
204b3 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
204b4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
204b5 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
204b6 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67  _String8, 0, reg
204b7 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30 29  Temp, 0, " ", 0)
204b8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
204b9 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
204ba 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c  Concat, regTemp,
204bb 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a   regF2, regF2);.
204bc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
204bd 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64  eAddOp3(v, OP_Ad
204be 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b  d, iMem, iMem+i+
204bf 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20  1, regTemp);.   
204c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
204c1 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
204c2 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b  m, regTemp, -1);
204c3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
204c4 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44  beAddOp3(v, OP_D
204c5 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c  ivide, iMem+i+1,
204c6 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d   regTemp, regTem
204c7 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
204c8 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
204c9 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70  P_ToInt, regTemp
204ca 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
204cb 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
204cc 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70  _Concat, regTemp
204cd 2c 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b  , regF2, regF2);
204ce 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
204cf 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
204d0 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
204d1 65 67 46 69 65 6c 64 73 2c 20 33 2c 20 72 65 67  egFields, 3, reg
204d2 52 65 63 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a  Rec, "aaa", 0);.
204d3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
204d4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
204d5 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20  owid, iStatCur, 
204d6 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  regRowid);.    s
204d7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
204d8 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69  (v, OP_Insert, i
204d9 53 74 61 74 43 75 72 2c 20 72 65 67 52 65 63 2c  StatCur, regRec,
204da 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
204db 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
204dc 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50  eP5(v, OPFLAG_AP
204dd 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74  PEND);.    sqlit
204de 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
204df 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a  , addr);.  }.}..
204e0 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
204e1 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61  ode that will ca
204e2 75 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63  use the most rec
204e3 65 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73  ent index analys
204e4 69 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64  is to.** be laod
204e5 65 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c  ed into internal
204e6 20 68 61 73 68 20 74 61 62 6c 65 73 20 77 68 65   hash tables whe
204e7 72 65 20 69 73 20 63 61 6e 20 62 65 20 75 73 65  re is can be use
204e8 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
204e9 64 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 50  d loadAnalysis(P
204ea 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
204eb 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a  t iDb){.  Vdbe *
204ec 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
204ed 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
204ee 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( v ){.    sqlit
204ef 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
204f0 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c  OP_LoadAnalysis,
204f1 20 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a   iDb);.  }.}../*
204f2 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
204f3 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61  e that will do a
204f4 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e  n analysis of an
204f5 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65   entire database
204f6 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
204f7 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28  analyzeDatabase(
204f8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
204f9 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74  nt iDb){.  sqlit
204fa 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
204fb 3e 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70  >db;.  Schema *p
204fc 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62  Schema = db->aDb
204fd 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20  [iDb].pSchema;  
204fe 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64    /* Schema of d
204ff 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20  atabase iDb */. 
20500 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20   HashElem *k;.  
20501 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20  int iStatCur;.  
20502 69 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c  int iMem;..  sql
20503 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
20504 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
20505 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74  0, iDb);.  iStat
20506 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Cur = pParse->nT
20507 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74  ab++;.  openStat
20508 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
20509 62 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b  b, iStatCur, 0);
2050a 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65  .  iMem = pParse
2050b 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28  ->nMem+1;.  for(
2050c 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  k=sqliteHashFirs
2050d 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  t(&pSchema->tblH
2050e 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74  ash); k; k=sqlit
2050f 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20  eHashNext(k)){. 
20510 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
20511 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48   (Table*)sqliteH
20512 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20  ashData(k);.    
20513 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28  analyzeOneTable(
20514 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53  pParse, pTab, iS
20515 74 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20  tatCur, iMem);. 
20516 20 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69   }.  loadAnalysi
20517 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  s(pParse, iDb);.
20518 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
20519 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
2051a 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20   do an analysis 
2051b 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
2051c 65 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61  e in.** a databa
2051d 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  se..*/.static vo
2051e 69 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28  id analyzeTable(
2051f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
20520 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69  able *pTab){.  i
20521 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53  nt iDb;.  int iS
20522 74 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72  tatCur;..  asser
20523 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20  t( pTab!=0 );.  
20524 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
20525 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
20526 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20  xes(pParse->db) 
20527 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
20528 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
20529 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
2052a 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71  ->pSchema);.  sq
2052b 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
2052c 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
2052d 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61   0, iDb);.  iSta
2052e 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  tCur = pParse->n
2052f 54 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61  Tab++;.  openSta
20530 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  tTable(pParse, i
20531 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54  Db, iStatCur, pT
20532 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e  ab->zName);.  an
20533 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50  alyzeOneTable(pP
20534 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61  arse, pTab, iSta
20535 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d  tCur, pParse->nM
20536 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61  em+1);.  loadAna
20537 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44  lysis(pParse, iD
20538 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  b);.}../*.** Gen
20539 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
2053a 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  he ANALYZE comma
2053b 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  nd.  The parser 
2053c 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
2053d 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65  ne.** when it re
2053e 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c  cognizes an ANAL
2053f 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  YZE command..**.
20540 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a  **        ANALYZ
20541 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
20542 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
20543 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c  1.**        ANAL
20544 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20  YZE  <database> 
20545 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
20546 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e  - 2.**        AN
20547 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73  ALYZE  ?<databas
20548 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20  e>.?<tablename> 
20549 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d   -- 3.**.** Form
2054a 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e   1 causes all in
2054b 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74  dices in all att
2054c 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20  ached databases 
2054d 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a  to be analyzed..
2054e 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a  ** Form 2 analyz
2054f 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74  es all indices t
20550 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61  he single databa
20551 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72  se named..** For
20552 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c  m 3 analyzes all
20553 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61   indices associa
20554 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d  ted with the nam
20555 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  ed table..*/.SQL
20556 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
20557 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28   sqlite3Analyze(
20558 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
20559 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f  oken *pName1, To
2055a 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20  ken *pName2){.  
2055b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2055c 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
2055d 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  iDb;.  int i;.  
2055e 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20  char *z, *zDb;. 
2055f 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
20560 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d  Token *pTableNam
20561 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68  e;..  /* Read th
20562 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
20563 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  a. If an error o
20564 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20  ccurs, leave an 
20565 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20  error message.  
20566 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70  ** and code in p
20567 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e  Parse and return
20568 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65   NULL. */.  asse
20569 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
2056a 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
2056b 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20  pParse->db) );. 
2056c 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
2056d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
2056e 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  a(pParse) ){.   
2056f 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
20570 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b  if( pName1==0 ){
20571 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a 20  .    /* Form 1: 
20572 20 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74 68   Analyze everyth
20573 69 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  ing */.    for(i
20574 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
20575 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  ++){.      if( i
20576 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20  ==1 ) continue; 
20577 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c 79   /* Do not analy
20578 7a 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61  ze the TEMP data
20579 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 6e  base */.      an
2057a 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50  alyzeDatabase(pP
2057b 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a  arse, i);.    }.
2057c 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d    }else if( pNam
2057d 65 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d  e2==0 || pName2-
2057e 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  >n==0 ){.    /* 
2057f 46 6f 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65  Form 2:  Analyze
20580 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72   the database or
20581 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a   table named */.
20582 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
20583 33 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d  3FindDb(db, pNam
20584 65 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62  e1);.    if( iDb
20585 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61  >=0 ){.      ana
20586 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61  lyzeDatabase(pPa
20587 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d  rse, iDb);.    }
20588 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20  else{.      z = 
20589 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
2058a 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29  oken(db, pName1)
2058b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b  ;.      if( z ){
2058c 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20  .        pTab = 
2058d 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
2058e 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c  le(pParse, 0, z,
2058f 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
20590 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
20591 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
20592 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Tab ){.         
20593 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50   analyzeTable(pP
20594 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
20595 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
20596 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
20597 20 20 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61    /* Form 3: Ana
20598 6c 79 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71  lyze the fully q
20599 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e  ualified table n
2059a 61 6d 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d  ame */.    iDb =
2059b 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
2059c 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ame(pParse, pNam
2059d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61  e1, pName2, &pTa
2059e 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  bleName);.    if
2059f 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20  ( iDb>=0 ){.    
205a0 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b    zDb = db->aDb[
205a1 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
205a2 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d    z = sqlite3Nam
205a3 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
205a4 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20  TableName);.    
205a5 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20    if( z ){.     
205a6 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
205a7 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
205a8 72 73 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b  rse, 0, z, zDb);
205a9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
205aa 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
205ab 20 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20         if( pTab 
205ac 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61  ){.          ana
205ad 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65  lyzeTable(pParse
205ae 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20  , pTab);.       
205af 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
205b0 20 20 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a     .  }.}../*.**
205b1 20 55 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e   Used to pass in
205b2 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
205b3 68 65 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64  he analyzer read
205b4 65 72 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  er through to th
205b5 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f  e.** callback ro
205b6 75 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65  utine..*/.typede
205b7 66 20 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69  f struct analysi
205b8 73 49 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e  sInfo analysisIn
205b9 66 6f 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79  fo;.struct analy
205ba 73 69 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69  sisInfo {.  sqli
205bb 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74  te3 *db;.  const
205bc 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65   char *zDatabase
205bd 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ;.};../*.** This
205be 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76   callback is inv
205bf 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  oked once for ea
205c0 63 68 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65  ch index when re
205c1 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c  ading the.** sql
205c2 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e  ite_stat1 table.
205c3 20 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67    .**.**     arg
205c4 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74  v[0] = name of t
205c5 68 65 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20  he index.**     
205c6 61 72 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74  argv[1] = result
205c7 73 20 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20  s of analysis - 
205c8 6f 6e 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65  on integer for e
205c9 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74  ach column.*/.st
205ca 61 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69  atic int analysi
205cb 73 4c 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44  sLoader(void *pD
205cc 61 74 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63  ata, int argc, c
205cd 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72  har **argv, char
205ce 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 61   **NotUsed){.  a
205cf 6e 61 6c 79 73 69 73 49 6e 66 6f 20 2a 70 49 6e  nalysisInfo *pIn
205d0 66 6f 20 3d 20 28 61 6e 61 6c 79 73 69 73 49 6e  fo = (analysisIn
205d1 66 6f 2a 29 70 44 61 74 61 3b 0a 20 20 49 6e 64  fo*)pData;.  Ind
205d2 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e  ex *pIndex;.  in
205d3 74 20 69 2c 20 63 3b 0a 20 20 75 6e 73 69 67 6e  t i, c;.  unsign
205d4 65 64 20 69 6e 74 20 76 3b 0a 20 20 63 6f 6e 73  ed int v;.  cons
205d5 74 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73  t char *z;..  as
205d6 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20 29 3b  sert( argc==2 );
205d7 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
205d8 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72  TER2(NotUsed, ar
205d9 67 63 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 76  gc);..  if( argv
205da 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d  ==0 || argv[0]==
205db 30 20 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20  0 || argv[1]==0 
205dc 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
205dd 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20  .  }.  pIndex = 
205de 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
205df 28 70 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76  (pInfo->db, argv
205e0 5b 30 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74  [0], pInfo->zDat
205e1 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49  abase);.  if( pI
205e2 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ndex==0 ){.    r
205e3 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a  eturn 0;.  }.  z
205e4 20 3d 20 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f   = argv[1];.  fo
205e5 72 28 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d  r(i=0; *z && i<=
205e6 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  pIndex->nColumn;
205e7 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30   i++){.    v = 0
205e8 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d  ;.    while( (c=
205e9 7a 5b 30 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c  z[0])>='0' && c<
205ea 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 76 20  ='9' ){.      v 
205eb 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27  = v*10 + c - '0'
205ec 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  ;.      z++;.   
205ed 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61   }.    pIndex->a
205ee 69 52 6f 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a  iRowEst[i] = v;.
205ef 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20      if( *z==' ' 
205f0 29 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74  ) z++;.  }.  ret
205f1 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
205f2 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  Load the content
205f3 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   of the sqlite_s
205f4 74 61 74 31 20 74 61 62 6c 65 20 69 6e 74 6f 20  tat1 table into 
205f5 74 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74  the index hash t
205f6 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ables..*/.SQLITE
205f7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
205f8 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64  ite3AnalysisLoad
205f9 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
205fa 74 20 69 44 62 29 7b 0a 20 20 61 6e 61 6c 79 73  t iDb){.  analys
205fb 69 73 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20 20  isInfo sInfo;.  
205fc 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 63  HashElem *i;.  c
205fd 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74  har *zSql;.  int
205fe 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
205ff 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
20600 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
20601 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
20602 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  pBt!=0 );.  asse
20603 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
20604 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61  HoldsMutex(db->a
20605 44 62 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a  Db[iDb].pBt) );.
20606 0a 20 20 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20  .  /* Clear any 
20607 70 72 69 6f 72 20 73 74 61 74 69 73 74 69 63 73  prior statistics
20608 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69   */.  for(i=sqli
20609 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d  teHashFirst(&db-
2060a 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
2060b 61 2d 3e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d  a->idxHash);i;i=
2060c 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69  sqliteHashNext(i
2060d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  )){.    Index *p
2060e 49 64 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Idx = sqliteHash
2060f 44 61 74 61 28 69 29 3b 0a 20 20 20 20 73 71 6c  Data(i);.    sql
20610 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73  ite3DefaultRowEs
20611 74 28 70 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20  t(pIdx);.  }..  
20612 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65  /* Check to make
20613 20 73 75 72 65 20 74 68 65 20 73 71 6c 69 74 65   sure the sqlite
20614 5f 73 74 61 74 31 20 74 61 62 6c 65 20 65 78 69  _stat1 table exi
20615 73 74 73 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e  stss */.  sInfo.
20616 64 62 20 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f  db = db;.  sInfo
20617 2e 7a 44 61 74 61 62 61 73 65 20 3d 20 64 62 2d  .zDatabase = db-
20618 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
20619 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
2061a 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c  ndTable(db, "sql
2061b 69 74 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66  ite_stat1", sInf
2061c 6f 2e 7a 44 61 74 61 62 61 73 65 29 3d 3d 30 20  o.zDatabase)==0 
2061d 29 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 53  ){.     return S
2061e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2061f 0a 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77  ...  /* Load new
20620 20 73 74 61 74 69 73 74 69 63 73 20 6f 75 74 20   statistics out 
20621 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  of the sqlite_st
20622 61 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a  at1 table */.  z
20623 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Sql = sqlite3MPr
20624 69 6e 74 66 28 64 62 2c 20 22 53 45 4c 45 43 54  intf(db, "SELECT
20625 20 69 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20   idx, stat FROM 
20626 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22  %Q.sqlite_stat1"
20627 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
20628 20 20 20 20 20 20 20 20 20 20 73 49 6e 66 6f 2e            sInfo.
20629 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66  zDatabase);.  if
2062a 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20  ( zSql==0 ){.   
2062b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
2062c 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  EM;.  }else{.   
2062d 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2062e 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
2062f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78   rc = sqlite3_ex
20630 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 61 6e 61  ec(db, zSql, ana
20631 6c 79 73 69 73 4c 6f 61 64 65 72 2c 20 26 73 49  lysisLoader, &sI
20632 6e 66 6f 2c 20 30 29 3b 0a 20 20 20 20 28 76 6f  nfo, 0);.    (vo
20633 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
20634 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69  On(db);.    sqli
20635 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
20636 71 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ql);.    if( rc=
20637 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 20  =SQLITE_NOMEM ) 
20638 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
20639 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
2063a 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69  rn rc;.}...#endi
2063b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2063c 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a  _ANALYZE */../**
2063d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2063e 20 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a   of analyze.c **
2063f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20641 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
20642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
20643 69 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e 63  in file attach.c
20644 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
20645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20646 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
20647 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a  ** 2003 April 6.
20648 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
20649 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2064a 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2064b 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2064c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2064d 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2064e 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2064f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
20650 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
20651 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
20652 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
20653 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
20654 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
20655 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
20656 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
20657 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
20658 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
20659 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2065a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2065b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2065c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2065d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2065e 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
2065f 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74  ains code used t
20660 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
20661 41 54 54 41 43 48 20 61 6e 64 20 44 45 54 41 43  ATTACH and DETAC
20662 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a 2a  H commands..**.*
20663 2a 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63 2c  * $Id: attach.c,
20664 76 20 31 2e 38 33 20 32 30 30 39 2f 30 32 2f 31  v 1.83 2009/02/1
20665 39 20 31 34 3a 33 39 3a 32 35 20 64 61 6e 69 65  9 14:39:25 danie
20666 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
20667 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20668 4f 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a  OMIT_ATTACH./*.*
20669 2a 20 52 65 73 6f 6c 76 65 20 61 6e 20 65 78 70  * Resolve an exp
2066a 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 61 73  ression that was
2066b 20 70 61 72 74 20 6f 66 20 61 6e 20 41 54 54 41   part of an ATTA
2066c 43 48 20 6f 72 20 44 45 54 41 43 48 20 73 74 61  CH or DETACH sta
2066d 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a 20  tement. This.** 
2066e 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66  is slightly diff
2066f 65 72 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f 6c  erent from resol
20670 76 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53 51  ving a normal SQ
20671 4c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 65  L expression, be
20672 63 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a 20  cause simple.** 
20673 69 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 20  identifiers are 
20674 74 72 65 61 74 65 64 20 61 73 20 73 74 72 69 6e  treated as strin
20675 67 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65  gs, not possible
20676 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 72   column names or
20677 20 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20   aliases..**.** 
20678 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72 73  i.e. if the pars
20679 65 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  er sees:.**.**  
2067a 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41     ATTACH DATABA
2067b 53 45 20 61 62 63 20 41 53 20 64 65 66 0a 2a 2a  SE abc AS def.**
2067c 0a 2a 2a 20 69 74 20 74 72 65 61 74 73 20 74 68  .** it treats th
2067d 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e  e two expression
2067e 73 20 61 73 20 6c 69 74 65 72 61 6c 20 73 74 72  s as literal str
2067f 69 6e 67 73 20 27 61 62 63 27 20 61 6e 64 20 27  ings 'abc' and '
20680 64 65 66 27 20 69 6e 73 74 65 61 64 20 6f 66 0a  def' instead of.
20681 2a 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 63  ** looking for c
20682 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 61  olumns of the sa
20683 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54  me name..**.** T
20684 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73  his only applies
20685 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64   to the root nod
20686 65 20 6f 66 20 70 45 78 70 72 2c 20 73 6f 20 74  e of pExpr, so t
20687 68 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a  he statement:.**
20688 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 44  .**     ATTACH D
20689 41 54 41 42 41 53 45 20 61 62 63 7c 7c 64 65 66  ATABASE abc||def
2068a 20 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20   AS 'db2'.**.** 
2068b 77 69 6c 6c 20 66 61 69 6c 20 62 65 63 61 75 73  will fail becaus
2068c 65 20 6e 65 69 74 68 65 72 20 61 62 63 20 6f 72  e neither abc or
2068d 20 64 65 66 20 63 61 6e 20 62 65 20 72 65 73 6f   def can be reso
2068e 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lved..*/.static 
2068f 69 6e 74 20 72 65 73 6f 6c 76 65 41 74 74 61 63  int resolveAttac
20690 68 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65 78  hExpr(NameContex
20691 74 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20 2a  t *pName, Expr *
20692 70 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20 72  pExpr).{.  int r
20693 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
20694 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20   if( pExpr ){.  
20695 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21    if( pExpr->op!
20696 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20 20  =TK_ID ){.      
20697 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73 6f  rc = sqlite3Reso
20698 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 61  lveExprNames(pNa
20699 6d 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  me, pExpr);.    
2069a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2069b 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33 45  _OK && !sqlite3E
2069c 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 45  xprIsConstant(pE
2069d 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  xpr) ){.        
2069e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2069f 70 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20 22  pName->pParse, "
206a0 69 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c 22  invalid name: \"
206a1 25 54 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e 73  %T\"", &pExpr->s
206a2 70 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65  pan);.        re
206a3 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
206a4 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  R;.      }.    }
206a5 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70  else{.      pExp
206a6 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e  r->op = TK_STRIN
206a7 47 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  G;.    }.  }.  r
206a8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
206a9 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66  ** An SQL user-f
206aa 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72  unction register
206ab 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72  ed to do the wor
206ac 6b 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73  k of an ATTACH s
206ad 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a  tatement. The.**
206ae 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73   three arguments
206af 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
206b0 20 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66   come directly f
206b1 72 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74  rom an attach st
206b2 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20  atement:.**.**  
206b3 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41     ATTACH DATABA
206b4 53 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a  SE x AS y KEY z.
206b5 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
206b6 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78   sqlite_attach(x
206b7 2c 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66  , y, z).**.** If
206b8 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b   the optional "K
206b9 45 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20  EY z" syntax is 
206ba 6f 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20  omitted, an SQL 
206bb 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61  NULL is passed a
206bc 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61  s the.** third a
206bd 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  rgument..*/.stat
206be 69 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 75  ic void attachFu
206bf 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
206c0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
206c1 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
206c2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
206c3 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69  *argv.){.  int i
206c4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a  ;.  int rc = 0;.
206c5 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
206c6 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
206c7 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
206c8 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
206c9 20 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74   *zName;.  const
206ca 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20   char *zFile;.  
206cb 44 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 72  Db *aNew;.  char
206cc 20 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a 20   *zErrDyn = 0;. 
206cd 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b   char zErr[128];
206ce 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
206cf 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a  ETER(NotUsed);..
206d0 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74    zFile = (const
206d1 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
206d2 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
206d3 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28  0]);.  zName = (
206d4 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
206d5 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
206d6 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20  argv[1]);.  if( 
206d7 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65  zFile==0 ) zFile
206d8 20 3d 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e 61   = "";.  if( zNa
206d9 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20  me==0 ) zName = 
206da 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  "";..  /* Check 
206db 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  for the followin
206dc 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20  g errors:.  **. 
206dd 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d 61   **     * Too ma
206de 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ny attached data
206df 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20 20  bases,.  **     
206e0 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 63 75  * Transaction cu
206e1 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 2a  rrently open.  *
206e2 2a 20 20 20 20 20 2a 20 53 70 65 63 69 66 69 65  *     * Specifie
206e3 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  d database name 
206e4 61 6c 72 65 61 64 79 20 62 65 69 6e 67 20 75 73  already being us
206e5 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ed..  */.  if( d
206e6 62 2d 3e 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d  b->nDb>=db->aLim
206e7 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
206e8 41 54 54 41 43 48 45 44 5d 2b 32 20 29 7b 0a 20  ATTACHED]+2 ){. 
206e9 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
206ea 6e 74 66 28 0a 20 20 20 20 20 20 73 69 7a 65 6f  ntf(.      sizeo
206eb 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22  f(zErr), zErr, "
206ec 74 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65  too many attache
206ed 64 20 64 61 74 61 62 61 73 65 73 20 2d 20 6d 61  d databases - ma
206ee 78 20 25 64 22 2c 20 0a 20 20 20 20 20 20 64 62  x %d", .      db
206ef 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
206f0 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 0a  LIMIT_ATTACHED].
206f1 20 20 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20      );.    goto 
206f2 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  attach_error;.  
206f3 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74  }.  if( !db->aut
206f4 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73  oCommit ){.    s
206f5 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
206f6 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45  sizeof(zErr), zE
206f7 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rr,.            
206f8 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74           "cannot
206f9 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65   ATTACH database
206fa 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74   within transact
206fb 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ion");.    goto 
206fc 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  attach_error;.  
206fd 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64  }.  for(i=0; i<d
206fe 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
206ff 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d 3e    char *z = db->
20700 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  aDb[i].zName;.  
20701 20 20 69 66 28 20 7a 20 26 26 20 7a 4e 61 6d 65    if( z && zName
20702 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
20703 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20  mp(z, zName)==0 
20704 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
20705 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
20706 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a 20  (zErr), zErr, . 
20707 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20708 20 20 20 20 20 20 22 64 61 74 61 62 61 73 65 20        "database 
20709 25 73 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  %s is already in
2070a 20 75 73 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20   use", zName);. 
2070b 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68       goto attach
2070c 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  _error;.    }.  
2070d 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
2070e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69   the new entry i
2070f 6e 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20  n the db->aDb[] 
20710 61 72 72 61 79 20 61 6e 64 20 69 6e 69 74 69 61  array and initia
20711 6c 69 73 65 20 74 68 65 20 73 63 68 65 6d 61 0a  lise the schema.
20712 20 20 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 73    ** hash tables
20713 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
20714 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61  >aDb==db->aDbSta
20715 74 69 63 20 29 7b 0a 20 20 20 20 61 4e 65 77 20  tic ){.    aNew 
20716 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
20717 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
20718 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b  db->aDb[0])*3 );
20719 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30  .    if( aNew==0
2071a 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6d   ) return;.    m
2071b 65 6d 63 70 79 28 61 4e 65 77 2c 20 64 62 2d 3e  emcpy(aNew, db->
2071c 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e  aDb, sizeof(db->
2071d 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d 65  aDb[0])*2);.  }e
2071e 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20  lse{.    aNew = 
2071f 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
20720 28 64 62 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69  (db, db->aDb, si
20721 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29  zeof(db->aDb[0])
20722 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a  *(db->nDb+1) );.
20723 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20      if( aNew==0 
20724 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20  ) return;.  }.  
20725 64 62 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a  db->aDb = aNew;.
20726 20 20 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44    aNew = &db->aD
20727 62 5b 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20 20  b[db->nDb++];.  
20728 6d 65 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c 20  memset(aNew, 0, 
20729 73 69 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b 0a  sizeof(*aNew));.
2072a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64  .  /* Open the d
2072b 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66  atabase file. If
2072c 20 74 68 65 20 62 74 72 65 65 20 69 73 20 73 75   the btree is su
2072d 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65  ccessfully opene
2072e 64 2c 20 75 73 65 0a 20 20 2a 2a 20 69 74 20 74  d, use.  ** it t
2072f 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 64 61 74  o obtain the dat
20730 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 41 74  abase schema. At
20731 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20   this point the 
20732 73 63 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a 20  schema may.  ** 
20733 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e  or may not be in
20734 69 74 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f 0a  itialised..  */.
20735 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
20736 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 7a  reeFactory(db, z
20737 46 69 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f  File, 0, SQLITE_
20738 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
20739 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ZE,.            
2073a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
2073b 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20 53  b->openFlags | S
2073c 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
2073d 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  DB,.            
2073e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
2073f 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69 66  aNew->pBt);.  if
20740 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
20741 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50  ){.    Pager *pP
20742 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e  ager;.    aNew->
20743 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65  pSchema = sqlite
20744 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 61  3SchemaGet(db, a
20745 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69  New->pBt);.    i
20746 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d  f( !aNew->pSchem
20747 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  a ){.      rc = 
20748 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
20749 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77    }else if( aNew
2074a 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
2074b 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e  format && aNew->
2074c 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e  pSchema->enc!=EN
2074d 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73  C(db) ){.      s
2074e 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
2074f 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45  sizeof(zErr), zE
20750 72 72 2c 20 0a 20 20 20 20 20 20 20 20 22 61 74  rr, .        "at
20751 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
20752 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73 61   must use the sa
20753 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  me text encoding
20754 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73   as main databas
20755 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  e");.      goto 
20756 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  attach_error;.  
20757 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 20 3d    }.    pPager =
20758 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
20759 65 72 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20  er(aNew->pBt);. 
2075a 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c     sqlite3PagerL
2075b 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65  ockingMode(pPage
2075c 72 2c 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d  r, db->dfltLockM
2075d 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ode);.    sqlite
2075e 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64  3PagerJournalMod
2075f 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 66  e(pPager, db->df
20760 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a  ltJournalMode);.
20761 20 20 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d    }.  aNew->zNam
20762 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
20763 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  Dup(db, zName);.
20764 20 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c    aNew->safety_l
20765 65 76 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53  evel = 3;..#if S
20766 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a  QLITE_HAS_CODEC.
20767 20 20 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69    {.    extern i
20768 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41  nt sqlite3CodecA
20769 74 74 61 63 68 28 73 71 6c 69 74 65 33 2a 2c 20  ttach(sqlite3*, 
2076a 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  int, const void*
2076b 2c 20 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 65  , int);.    exte
2076c 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  rn void sqlite3C
2076d 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74  odecGetKey(sqlit
2076e 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a  e3*, int, void**
2076f 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74  , int*);.    int
20770 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20   nKey;.    char 
20771 2a 7a 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 74  *zKey;.    int t
20772 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
20773 5f 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a  _type(argv[2]);.
20774 20 20 20 20 73 77 69 74 63 68 28 20 74 20 29 7b      switch( t ){
20775 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49  .      case SQLI
20776 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20  TE_INTEGER:.    
20777 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c    case SQLITE_FL
20778 4f 41 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 72  OAT:.        zEr
20779 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 44 62  rDyn = sqlite3Db
2077a 53 74 72 44 75 70 28 64 62 2c 20 22 49 6e 76 61  StrDup(db, "Inva
2077b 6c 69 64 20 6b 65 79 20 76 61 6c 75 65 22 29 3b  lid key value");
2077c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
2077d 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
2077e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2077f 20 20 20 0a 20 20 20 20 20 20 63 61 73 65 20 53     .      case S
20780 51 4c 49 54 45 5f 54 45 58 54 3a 0a 20 20 20 20  QLITE_TEXT:.    
20781 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c    case SQLITE_BL
20782 4f 42 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65 79  OB:.        nKey
20783 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
20784 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b  _bytes(argv[2]);
20785 0a 20 20 20 20 20 20 20 20 7a 4b 65 79 20 3d 20  .        zKey = 
20786 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
20787 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
20788 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  2]);.        sql
20789 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 28  ite3CodecAttach(
2078a 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a  db, db->nDb-1, z
2078b 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20  Key, nKey);.    
2078c 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20      break;..    
2078d 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55    case SQLITE_NU
2078e 4c 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e  LL:.        /* N
2078f 6f 20 6b 65 79 20 73 70 65 63 69 66 69 65 64 2e  o key specified.
20790 20 20 55 73 65 20 74 68 65 20 6b 65 79 20 66 72    Use the key fr
20791 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  om the main data
20792 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  base */.        
20793 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b  sqlite3CodecGetK
20794 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a  ey(db, 0, (void*
20795 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b  *)&zKey, &nKey);
20796 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
20797 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 20  CodecAttach(db, 
20798 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c  db->nDb-1, zKey,
20799 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20   nKey);.        
2079a 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
2079b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
2079c 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70   the file was op
2079d 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c  ened successfull
2079e 79 2c 20 72 65 61 64 20 74 68 65 20 73 63 68 65  y, read the sche
2079f 6d 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64  ma for the new d
207a0 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66  atabase..  ** If
207a1 20 74 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 20   this fails, or 
207a2 69 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66  if opening the f
207a3 69 6c 65 20 66 61 69 6c 65 64 2c 20 74 68 65 6e  ile failed, then
207a4 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20   close the file 
207a5 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65  and .  ** remove
207a6 20 74 68 65 20 65 6e 74 72 79 20 66 72 6f 6d 20   the entry from 
207a7 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72  the db->aDb[] ar
207a8 72 61 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 76  ray. i.e. put ev
207a9 65 72 79 74 68 69 6e 67 20 62 61 63 6b 20 74 68  erything back th
207aa 65 20 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f  e way.  ** we fo
207ab 75 6e 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69  und it..  */.  i
207ac 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
207ad 20 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71   ){.    (void)sq
207ae 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
207af 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
207b0 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
207b1 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
207b2 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 44  3Init(db, &zErrD
207b3 79 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  yn);.    sqlite3
207b4 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
207b5 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  );.    (void)sql
207b6 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
207b7 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20  );.  }.  if( rc 
207b8 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20 3d  ){.    int iDb =
207b9 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20   db->nDb - 1;.  
207ba 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 32    assert( iDb>=2
207bb 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   );.    if( db->
207bc 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a  aDb[iDb].pBt ){.
207bd 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
207be 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b  eeClose(db->aDb[
207bf 69 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20  iDb].pBt);.     
207c0 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42   db->aDb[iDb].pB
207c1 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 2d  t = 0;.      db-
207c2 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
207c3 61 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  a = 0;.    }.   
207c4 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
207c5 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
207c6 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20  0);.    db->nDb 
207c7 3d 20 69 44 62 3b 0a 20 20 20 20 69 66 28 20 72  = iDb;.    if( r
207c8 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
207c9 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  || rc==SQLITE_IO
207ca 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ERR_NOMEM ){.   
207cb 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
207cc 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73  led = 1;.      s
207cd 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
207ce 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72  sizeof(zErr),zEr
207cf 72 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  r, "out of memor
207d0 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  y");.    }else{.
207d1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
207d2 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
207d3 72 72 29 2c 7a 45 72 72 2c 20 22 75 6e 61 62 6c  rr),zErr, "unabl
207d4 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 61  e to open databa
207d5 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29 3b  se: %s", zFile);
207d6 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20  .    }.    goto 
207d7 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  attach_error;.  
207d8 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a 0a  }.  .  return;..
207d9 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20  attach_error:.  
207da 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  /* Return an err
207db 6f 72 20 69 66 20 77 65 20 67 65 74 20 68 65 72  or if we get her
207dc 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72 44  e */.  if( zErrD
207dd 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  yn ){.    sqlite
207de 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
207df 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c  ontext, zErrDyn,
207e0 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   -1);.    sqlite
207e1 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
207e2 44 79 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Dyn);.  }else{. 
207e3 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a     zErr[sizeof(z
207e4 45 72 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20 20  Err)-1] = 0;.   
207e5 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
207e6 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
207e7 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20  Err, -1);.  }.  
207e8 69 66 28 20 72 63 20 29 20 73 71 6c 69 74 65 33  if( rc ) sqlite3
207e9 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  _result_error_co
207ea 64 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b  de(context, rc);
207eb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c  .}../*.** An SQL
207ec 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72   user-function r
207ed 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20  egistered to do 
207ee 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44  the work of an D
207ef 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e  ETACH statement.
207f0 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72   The.** three ar
207f1 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
207f2 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72  unction come dir
207f3 65 63 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 74  ectly from a det
207f4 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a  ach statement:.*
207f5 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20  *.**     DETACH 
207f6 44 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a  DATABASE x.**.**
207f7 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69       SELECT sqli
207f8 74 65 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a  te_detach(x).*/.
207f9 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 74 61  static void deta
207fa 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  chFunc(.  sqlite
207fb 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
207fc 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
207fd 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
207fe 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
207ff 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
20800 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
20801 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
20802 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
20803 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
20804 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
20805 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
20806 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44 62 20  ;.  int i;.  Db 
20807 2a 70 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 72  *pDb = 0;.  char
20808 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 55   zErr[128];..  U
20809 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
2080a 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28  NotUsed);..  if(
2080b 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d   zName==0 ) zNam
2080c 65 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 3d  e = "";.  for(i=
2080d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
2080e 2b 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64  +){.    pDb = &d
2080f 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69  b->aDb[i];.    i
20810 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29  f( pDb->pBt==0 )
20811 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
20812 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
20813 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e  p(pDb->zName, zN
20814 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b  ame)==0 ) break;
20815 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d 64  .  }..  if( i>=d
20816 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73 71  b->nDb ){.    sq
20817 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
20818 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72  izeof(zErr),zErr
20819 2c 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61 62  , "no such datab
2081a 61 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29  ase: %s", zName)
2081b 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63  ;.    goto detac
2081c 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69  h_error;.  }.  i
2081d 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 73 71  f( i<2 ){.    sq
2081e 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
2081f 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72  izeof(zErr),zErr
20820 2c 20 22 63 61 6e 6e 6f 74 20 64 65 74 61 63 68  , "cannot detach
20821 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 7a   database %s", z
20822 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
20823 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  detach_error;.  
20824 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74  }.  if( !db->aut
20825 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73  oCommit ){.    s
20826 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
20827 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45  sizeof(zErr), zE
20828 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rr,.            
20829 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74           "cannot
2082a 20 44 45 54 41 43 48 20 64 61 74 61 62 61 73 65   DETACH database
2082b 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74   within transact
2082c 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ion");.    goto 
2082d 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  detach_error;.  
2082e 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 42  }.  if( sqlite3B
2082f 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e  treeIsInReadTran
20830 73 28 70 44 62 2d 3e 70 42 74 29 20 7c 7c 20 73  s(pDb->pBt) || s
20831 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 42  qlite3BtreeIsInB
20832 61 63 6b 75 70 28 70 44 62 2d 3e 70 42 74 29 20  ackup(pDb->pBt) 
20833 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
20834 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
20835 45 72 72 29 2c 7a 45 72 72 2c 20 22 64 61 74 61  Err),zErr, "data
20836 62 61 73 65 20 25 73 20 69 73 20 6c 6f 63 6b 65  base %s is locke
20837 64 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  d", zName);.    
20838 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f  goto detach_erro
20839 72 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  r;.  }..  sqlite
2083a 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d  3BtreeClose(pDb-
2083b 3e 70 42 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42  >pBt);.  pDb->pB
2083c 74 20 3d 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53  t = 0;.  pDb->pS
2083d 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c  chema = 0;.  sql
2083e 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
2083f 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
20840 20 20 72 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63    return;..detac
20841 68 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74  h_error:.  sqlit
20842 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
20843 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
20844 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  1);.}../*.** Thi
20845 73 20 70 72 6f 63 65 64 75 72 65 20 67 65 6e 65  s procedure gene
20846 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20  rates VDBE code 
20847 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 6e 76  for a single inv
20848 6f 63 61 74 69 6f 6e 20 6f 66 20 65 69 74 68 65  ocation of eithe
20849 72 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f  r the.** sqlite_
2084a 64 65 74 61 63 68 28 29 20 6f 72 20 73 71 6c 69  detach() or sqli
2084b 74 65 5f 61 74 74 61 63 68 28 29 20 53 51 4c 20  te_attach() SQL 
2084c 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  user functions..
2084d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
2084e 6f 64 65 41 74 74 61 63 68 28 0a 20 20 50 61 72  odeAttach(.  Par
2084f 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
20850 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
20851 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
20852 20 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20   type,          
20853 20 20 2f 2a 20 45 69 74 68 65 72 20 53 51 4c 49    /* Either SQLI
20854 54 45 5f 41 54 54 41 43 48 20 6f 72 20 53 51 4c  TE_ATTACH or SQL
20855 49 54 45 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20  ITE_DETACH */.  
20856 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 2c 20  FuncDef *pFunc, 
20857 20 20 20 20 20 2f 2a 20 46 75 6e 63 44 65 66 20       /* FuncDef 
20858 77 72 61 70 70 65 72 20 66 6f 72 20 64 65 74 61  wrapper for deta
20859 63 68 46 75 6e 63 28 29 20 6f 72 20 61 74 74 61  chFunc() or atta
2085a 63 68 46 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78  chFunc() */.  Ex
2085b 70 72 20 2a 70 41 75 74 68 41 72 67 2c 20 20 20  pr *pAuthArg,   
2085c 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
2085d 20 74 6f 20 70 61 73 73 20 74 6f 20 61 75 74 68   to pass to auth
2085e 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
2085f 63 6b 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46  ck */.  Expr *pF
20860 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20  ilename,     /* 
20861 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65  Name of database
20862 20 66 69 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20   file */.  Expr 
20863 2a 70 44 62 6e 61 6d 65 2c 20 20 20 20 20 20 20  *pDbname,       
20864 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
20865 61 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 69  atabase to use i
20866 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45  nternally */.  E
20867 78 70 72 20 2a 70 4b 65 79 20 20 20 20 20 20 20  xpr *pKey       
20868 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
20869 6b 65 79 20 66 6f 72 20 65 6e 63 72 79 70 74 69  key for encrypti
2086a 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a  on extension */.
2086b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e  ){.  int rc;.  N
2086c 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65  ameContext sName
2086d 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73  ;.  Vdbe *v;.  s
2086e 71 6c 69 74 65 33 2a 20 64 62 20 3d 20 70 50 61  qlite3* db = pPa
2086f 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72  rse->db;.  int r
20870 65 67 41 72 67 73 3b 0a 0a 23 69 66 6e 64 65 66  egArgs;..#ifndef
20871 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
20872 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73  HORIZATION.  ass
20873 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
20874 61 69 6c 65 64 20 7c 7c 20 70 41 75 74 68 41 72  ailed || pAuthAr
20875 67 20 29 3b 0a 20 20 69 66 28 20 70 41 75 74 68  g );.  if( pAuth
20876 41 72 67 20 29 7b 0a 20 20 20 20 63 68 61 72 20  Arg ){.    char 
20877 2a 7a 41 75 74 68 41 72 67 20 3d 20 73 71 6c 69  *zAuthArg = sqli
20878 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
20879 28 64 62 2c 20 26 70 41 75 74 68 41 72 67 2d 3e  (db, &pAuthArg->
2087a 73 70 61 6e 29 3b 0a 20 20 20 20 69 66 28 20 21  span);.    if( !
2087b 7a 41 75 74 68 41 72 67 20 29 7b 0a 20 20 20 20  zAuthArg ){.    
2087c 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e    goto attach_en
2087d 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  d;.    }.    rc 
2087e 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  = sqlite3AuthChe
2087f 63 6b 28 70 50 61 72 73 65 2c 20 74 79 70 65 2c  ck(pParse, type,
20880 20 7a 41 75 74 68 41 72 67 2c 20 30 2c 20 30 29   zAuthArg, 0, 0)
20881 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
20882 72 65 65 28 64 62 2c 20 7a 41 75 74 68 41 72 67  ree(db, zAuthArg
20883 29 3b 0a 20 20 20 20 69 66 28 72 63 21 3d 53 51  );.    if(rc!=SQ
20884 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
20885 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64   goto attach_end
20886 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
20887 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
20888 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20  T_AUTHORIZATION 
20889 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e  */..  memset(&sN
2088a 61 6d 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e  ame, 0, sizeof(N
2088b 61 6d 65 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20  ameContext));.  
2088c 73 4e 61 6d 65 2e 70 50 61 72 73 65 20 3d 20 70  sName.pParse = p
2088d 50 61 72 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20  Parse;..  if( . 
2088e 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d       SQLITE_OK!=
2088f 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74  (rc = resolveAtt
20890 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20  achExpr(&sName, 
20891 70 46 69 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20  pFilename)) ||. 
20892 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d       SQLITE_OK!=
20893 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74  (rc = resolveAtt
20894 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20  achExpr(&sName, 
20895 70 44 62 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20  pDbname)) ||.   
20896 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72     SQLITE_OK!=(r
20897 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63  c = resolveAttac
20898 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b  hExpr(&sName, pK
20899 65 79 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 50  ey)).  ){.    pP
2089a 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
2089b 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e    goto attach_en
2089c 64 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71  d;.  }..  v = sq
2089d 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2089e 72 73 65 29 3b 0a 20 20 72 65 67 41 72 67 73 20  rse);.  regArgs 
2089f 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
208a0 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 34 29  Range(pParse, 4)
208a1 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
208a2 6f 64 65 28 70 50 61 72 73 65 2c 20 70 46 69 6c  ode(pParse, pFil
208a3 65 6e 61 6d 65 2c 20 72 65 67 41 72 67 73 29 3b  ename, regArgs);
208a4 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  .  sqlite3ExprCo
208a5 64 65 28 70 50 61 72 73 65 2c 20 70 44 62 6e 61  de(pParse, pDbna
208a6 6d 65 2c 20 72 65 67 41 72 67 73 2b 31 29 3b 0a  me, regArgs+1);.
208a7 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
208a8 65 28 70 50 61 72 73 65 2c 20 70 4b 65 79 2c 20  e(pParse, pKey, 
208a9 72 65 67 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61  regArgs+2);..  a
208aa 73 73 65 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e  ssert( v || db->
208ab 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
208ac 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
208ad 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
208ae 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c  (v, OP_Function,
208af 20 30 2c 20 72 65 67 41 72 67 73 2b 33 2d 70 46   0, regArgs+3-pF
208b0 75 6e 63 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72  unc->nArg, regAr
208b1 67 73 2b 33 29 3b 0a 20 20 20 20 61 73 73 65 72  gs+3);.    asser
208b2 74 28 20 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d  t( pFunc->nArg==
208b3 2d 31 20 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41  -1 || (pFunc->nA
208b4 72 67 26 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d  rg&0xff)==pFunc-
208b5 3e 6e 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c  >nArg );.    sql
208b6 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
208b7 28 76 2c 20 28 75 38 29 28 70 46 75 6e 63 2d 3e  (v, (u8)(pFunc->
208b8 6e 41 72 67 29 29 3b 0a 20 20 20 20 73 71 6c 69  nArg));.    sqli
208b9 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
208ba 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70  v, -1, (char *)p
208bb 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46  Func, P4_FUNCDEF
208bc 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20  );..    /* Code 
208bd 61 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f  an OP_Expire. Fo
208be 72 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74  r an ATTACH stat
208bf 65 6d 65 6e 74 2c 20 73 65 74 20 50 31 20 74 6f  ement, set P1 to
208c0 20 74 72 75 65 20 28 65 78 70 69 72 65 20 74 68   true (expire th
208c1 69 73 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d  is.    ** statem
208c2 65 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44  ent only). For D
208c3 45 54 41 43 48 2c 20 73 65 74 20 69 74 20 74 6f  ETACH, set it to
208c4 20 66 61 6c 73 65 20 28 65 78 70 69 72 65 20 61   false (expire a
208c5 6c 6c 20 65 78 69 73 74 69 6e 67 0a 20 20 20 20  ll existing.    
208c6 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a  ** statements)..
208c7 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
208c8 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
208c9 4f 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65  OP_Expire, (type
208ca 3d 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29  ==SQLITE_ATTACH)
208cb 29 3b 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63 68  );.  }.  .attach
208cc 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45  _end:.  sqlite3E
208cd 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 46  xprDelete(db, pF
208ce 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  ilename);.  sqli
208cf 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
208d0 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71  , pDbname);.  sq
208d1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
208d2 64 62 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a  db, pKey);.}../*
208d3 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68  .** Called by th
208d4 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70  e parser to comp
208d5 69 6c 65 20 61 20 44 45 54 41 43 48 20 73 74 61  ile a DETACH sta
208d6 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20  tement..**.**   
208d7 20 20 44 45 54 41 43 48 20 70 44 62 6e 61 6d 65    DETACH pDbname
208d8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
208d9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
208da 65 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 61  etach(Parse *pPa
208db 72 73 65 2c 20 45 78 70 72 20 2a 70 44 62 6e 61  rse, Expr *pDbna
208dc 6d 65 29 7b 0a 20 20 73 74 61 74 69 63 20 46 75  me){.  static Fu
208dd 6e 63 44 65 66 20 64 65 74 61 63 68 5f 66 75 6e  ncDef detach_fun
208de 63 20 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20  c = {.    1,    
208df 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
208e0 41 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54  Arg */.    SQLIT
208e1 45 5f 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20  E_UTF8,      /* 
208e2 69 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20  iPrefEnc */.    
208e3 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
208e4 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20    /* flags */.  
208e5 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
208e6 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61      /* pUserData
208e7 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
208e8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
208e9 78 74 20 2a 2f 0a 20 20 20 20 64 65 74 61 63 68  xt */.    detach
208ea 46 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78  Func,       /* x
208eb 46 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Func */.    0,  
208ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
208ed 20 78 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c   xStep */.    0,
208ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
208ef 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a  /* xFinalize */.
208f0 20 20 20 20 22 73 71 6c 69 74 65 5f 64 65 74 61      "sqlite_deta
208f1 63 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a  ch",  /* zName *
208f2 2f 0a 20 20 20 20 30 20 20 20 20 20 20 20 20 20  /.    0         
208f3 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68          /* pHash
208f4 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41   */.  };.  codeA
208f5 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 53 51  ttach(pParse, SQ
208f6 4c 49 54 45 5f 44 45 54 41 43 48 2c 20 26 64 65  LITE_DETACH, &de
208f7 74 61 63 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61  tach_func, pDbna
208f8 6d 65 2c 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d  me, 0, 0, pDbnam
208f9 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c  e);.}../*.** Cal
208fa 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
208fb 72 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20  r to compile an 
208fc 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74  ATTACH statement
208fd 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41  ..**.**     ATTA
208fe 43 48 20 70 20 41 53 20 70 44 62 6e 61 6d 65 20  CH p AS pDbname 
208ff 4b 45 59 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49  KEY pKey.*/.SQLI
20900 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
20901 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61  sqlite3Attach(Pa
20902 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
20903 72 20 2a 70 2c 20 45 78 70 72 20 2a 70 44 62 6e  r *p, Expr *pDbn
20904 61 6d 65 2c 20 45 78 70 72 20 2a 70 4b 65 79 29  ame, Expr *pKey)
20905 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 44  {.  static FuncD
20906 65 66 20 61 74 74 61 63 68 5f 66 75 6e 63 20 3d  ef attach_func =
20907 20 7b 0a 20 20 20 20 33 2c 20 20 20 20 20 20 20   {.    3,       
20908 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67           /* nArg
20909 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55   */.    SQLITE_U
2090a 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72  TF8,      /* iPr
2090b 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20  efEnc */.    0, 
2090c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2090d 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30  * flags */.    0
2090e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2090f 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f   /* pUserData */
20910 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
20911 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20         /* pNext 
20912 2a 2f 0a 20 20 20 20 61 74 74 61 63 68 46 75 6e  */.    attachFun
20913 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e  c,       /* xFun
20914 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  c */.    0,     
20915 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
20916 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  tep */.    0,   
20917 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20918 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20  xFinalize */.   
20919 20 22 73 71 6c 69 74 65 5f 61 74 74 61 63 68 22   "sqlite_attach"
2091a 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20  ,  /* zName */. 
2091b 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
2091c 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f       /* pHash */
2091d 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61  .  };.  codeAtta
2091e 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ch(pParse, SQLIT
2091f 45 5f 41 54 54 41 43 48 2c 20 26 61 74 74 61 63  E_ATTACH, &attac
20920 68 5f 66 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44  h_func, p, p, pD
20921 62 6e 61 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a  bname, pKey);.}.
20922 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
20923 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a  _OMIT_ATTACH */.
20924 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
20925 65 20 61 20 44 62 46 69 78 65 72 20 73 74 72 75  e a DbFixer stru
20926 63 74 75 72 65 2e 20 20 54 68 69 73 20 72 6f 75  cture.  This rou
20927 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c  tine must be cal
20928 6c 65 64 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20  led prior.** to 
20929 70 61 73 73 69 6e 67 20 74 68 65 20 73 74 72 75  passing the stru
2092a 63 74 75 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20  cture to one of 
2092b 74 68 65 20 73 71 6c 69 74 65 46 69 78 41 41 41  the sqliteFixAAA
2092c 41 28 29 20 72 6f 75 74 69 6e 65 73 20 62 65 6c  A() routines bel
2092d 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ow..**.** The re
2092e 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63  turn value indic
2092f 61 74 65 73 20 77 68 65 74 68 65 72 20 6f 72 20  ates whether or 
20930 6e 6f 74 20 66 69 78 61 74 69 6f 6e 20 69 73 20  not fixation is 
20931 72 65 71 75 69 72 65 64 2e 20 20 54 52 55 45 0a  required.  TRUE.
20932 2a 2a 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e  ** means we do n
20933 65 65 64 20 74 6f 20 66 69 78 20 74 68 65 20 64  eed to fix the d
20934 61 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63  atabase referenc
20935 65 73 2c 20 46 41 4c 53 45 20 6d 65 61 6e 73 20  es, FALSE means 
20936 77 65 20 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51  we do not..*/.SQ
20937 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
20938 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
20939 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78  .  DbFixer *pFix
2093a 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69  ,      /* The fi
2093b 78 65 72 20 74 6f 20 62 65 20 69 6e 69 74 69 61  xer to be initia
2093c 6c 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65  lized */.  Parse
2093d 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f   *pParse,      /
2093e 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 73  * Error messages
2093f 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e   will be written
20940 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69   here */.  int i
20941 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  Db,            /
20942 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64 61  * This is the da
20943 74 61 62 61 73 65 20 74 68 61 74 20 6d 75 73 74  tabase that must
20944 20 62 65 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f   be used */.  co
20945 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c  nst char *zType,
20946 20 20 2f 2a 20 22 76 69 65 77 22 2c 20 22 74 72    /* "view", "tr
20947 69 67 67 65 72 22 2c 20 6f 72 20 22 69 6e 64 65  igger", or "inde
20948 78 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f  x" */.  const To
20949 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e  ken *pName  /* N
2094a 61 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77 2c  ame of the view,
2094b 20 74 72 69 67 67 65 72 2c 20 6f 72 20 69 6e 64   trigger, or ind
2094c 65 78 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  ex */.){.  sqlit
2094d 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 69  e3 *db;..  if( i
2094e 44 62 3c 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29  Db<0 || iDb==1 )
2094f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20   return 0;.  db 
20950 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
20951 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e  assert( db->nDb>
20952 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70  iDb );.  pFix->p
20953 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
20954 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62    pFix->zDb = db
20955 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
20956 3b 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20  ;.  pFix->zType 
20957 3d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 2d  = zType;.  pFix-
20958 3e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a  >pName = pName;.
20959 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
2095a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
2095b 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e  ng set of routin
2095c 65 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 20  es walk through 
2095d 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 61  the parse tree a
2095e 6e 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73  nd assign.** a s
2095f 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73 65  pecific database
20960 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 65   to all table re
20961 66 65 72 65 6e 63 65 73 20 77 68 65 72 65 20 74  ferences where t
20962 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
20963 0a 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e 73  .** was left uns
20964 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
20965 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61  original SQL sta
20966 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69  tement.  The pFi
20967 78 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d  x structure.** m
20968 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ust have been in
20969 69 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20 70  itialized by a p
2096a 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c  rior call to sql
2096b 69 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a  ite3FixInit()..*
2096c 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
2096d 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  nes are used to 
2096e 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61  make sure that a
2096f 6e 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65 72  n index, trigger
20970 2c 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20  , or.** view in 
20971 6f 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f 65  one database doe
20972 73 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 6f  s not refer to o
20973 62 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66 66  bjects in a diff
20974 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e 0a  erent database..
20975 2a 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 69  ** (Exception: i
20976 6e 64 69 63 65 73 2c 20 74 72 69 67 67 65 72 73  ndices, triggers
20977 2c 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20 74  , and views in t
20978 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
20979 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20   are.** allowed 
2097a 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 74  to refer to anyt
2097b 68 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65 66  hing.)  If a ref
2097c 65 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69 63  erence is explic
2097d 69 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20  itly made.** to 
2097e 61 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 64  an object in a d
2097f 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73  ifferent databas
20980 65 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  e, an error mess
20981 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a  age is added to.
20982 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  ** pParse->zErrM
20983 73 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75  sg and these rou
20984 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e  tines return non
20985 2d 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79  -zero.  If every
20986 74 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20  thing.** checks 
20987 6f 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69  out, these routi
20988 6e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f  nes return 0..*/
20989 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2098a 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72  int sqlite3FixSr
2098b 63 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72  cList(.  DbFixer
2098c 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a   *pFix,       /*
2098d 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   Context of the 
2098e 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72  fixation */.  Sr
2098f 63 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20  cList *pList    
20990 20 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 65     /* The Source
20991 20 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20 61   list to check a
20992 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a  nd modify */.){.
20993 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74    int i;.  const
20994 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74   char *zDb;.  st
20995 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
20996 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28  m *pItem;..  if(
20997 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
20998 72 6e 20 30 3b 0a 20 20 7a 44 62 20 3d 20 70 46  rn 0;.  zDb = pF
20999 69 78 2d 3e 7a 44 62 3b 0a 20 20 66 6f 72 28 69  ix->zDb;.  for(i
2099a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d  =0, pItem=pList-
2099b 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72  >a; i<pList->nSr
2099c 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  c; i++, pItem++)
2099d 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d  {.    if( pItem-
2099e 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30 20 29 7b  >zDatabase==0 ){
2099f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 44  .      pItem->zD
209a0 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65  atabase = sqlite
209a1 33 44 62 53 74 72 44 75 70 28 70 46 69 78 2d 3e  3DbStrDup(pFix->
209a2 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 44 62 29  pParse->db, zDb)
209a3 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
209a4 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
209a5 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c  Item->zDatabase,
209a6 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  zDb)!=0 ){.     
209a7 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
209a8 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2c 0a 20  (pFix->pParse,. 
209a9 20 20 20 20 20 20 20 20 22 25 73 20 25 54 20 63          "%s %T c
209aa 61 6e 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20  annot reference 
209ab 6f 62 6a 65 63 74 73 20 69 6e 20 64 61 74 61 62  objects in datab
209ac 61 73 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20  ase %s",.       
209ad 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 2c 20 70    pFix->zType, p
209ae 46 69 78 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 65  Fix->pName, pIte
209af 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  m->zDatabase);. 
209b0 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
209b1 20 20 20 7d 0a 23 69 66 20 21 64 65 66 69 6e 65     }.#if !define
209b2 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
209b3 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  EW) || !defined(
209b4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
209b5 47 45 52 29 0a 20 20 20 20 69 66 28 20 73 71 6c  GER).    if( sql
209b6 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46  ite3FixSelect(pF
209b7 69 78 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  ix, pItem->pSele
209b8 63 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ct) ) return 1;.
209b9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
209ba 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 74  ixExpr(pFix, pIt
209bb 65 6d 2d 3e 70 4f 6e 29 20 29 20 72 65 74 75 72  em->pOn) ) retur
209bc 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  n 1;.#endif.  }.
209bd 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 69    return 0;.}.#i
209be 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
209bf 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
209c0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
209c1 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51  OMIT_TRIGGER).SQ
209c2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
209c3 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63   sqlite3FixSelec
209c4 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  t(.  DbFixer *pF
209c5 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ix,       /* Con
209c6 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61  text of the fixa
209c7 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74  tion */.  Select
209c8 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 2f   *pSelect      /
209c9 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
209ca 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 66 69 78  tement to be fix
209cb 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61  ed to one databa
209cc 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65  se */.){.  while
209cd 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
209ce 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
209cf 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53  xprList(pFix, pS
209d0 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 29 20 29  elect->pEList) )
209d1 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
209d2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
209d3 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73  sqlite3FixSrcLis
209d4 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d  t(pFix, pSelect-
209d5 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20 20 20  >pSrc) ){.      
209d6 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
209d7 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
209d8 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 65  ixExpr(pFix, pSe
209d9 6c 65 63 74 2d 3e 70 57 68 65 72 65 29 20 29 7b  lect->pWhere) ){
209da 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
209db 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
209dc 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46  qlite3FixExpr(pF
209dd 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 48 61  ix, pSelect->pHa
209de 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 20 20 72  ving) ){.      r
209df 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
209e0 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65     pSelect = pSe
209e1 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20  lect->pPrior;.  
209e2 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
209e3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
209e4 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  nt sqlite3FixExp
209e5 72 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  r(.  DbFixer *pF
209e6 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
209e7 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
209e8 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  on */.  Expr *pE
209e9 78 70 72 20 20 20 20 20 20 20 20 2f 2a 20 54 68  xpr        /* Th
209ea 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
209eb 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20  be fixed to one 
209ec 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20  database */.){. 
209ed 20 77 68 69 6c 65 28 20 70 45 78 70 72 20 29 7b   while( pExpr ){
209ee 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73  .    if( ExprHas
209ef 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
209f0 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  r, EP_TokenOnly|
209f1 45 50 5f 53 70 61 6e 4f 6e 6c 79 29 20 29 20 62  EP_SpanOnly) ) b
209f2 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 45 78  reak;.    if( Ex
209f3 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
209f4 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
209f5 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  t) ){.      if( 
209f6 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74  sqlite3FixSelect
209f7 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 78 2e  (pFix, pExpr->x.
209f8 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72  pSelect) ) retur
209f9 6e 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  n 1;.    }else{.
209fa 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
209fb 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69  3FixExprList(pFi
209fc 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  x, pExpr->x.pLis
209fd 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  t) ) return 1;. 
209fe 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
209ff 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
20a00 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
20a01 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
20a02 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45   1;.    }.    pE
20a03 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  xpr = pExpr->pLe
20a04 66 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ft;.  }.  return
20a05 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49   0;.}.SQLITE_PRI
20a06 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
20a07 46 69 78 45 78 70 72 4c 69 73 74 28 0a 20 20 44  FixExprList(.  D
20a08 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20  bFixer *pFix,   
20a09 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20    /* Context of 
20a0a 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a  the fixation */.
20a0b 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
20a0c 74 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72  t    /* The expr
20a0d 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78  ession to be fix
20a0e 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61  ed to one databa
20a0f 73 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  se */.){.  int i
20a10 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ;.  struct ExprL
20a11 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
20a12 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
20a13 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f  ) return 0;.  fo
20a14 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69  r(i=0, pItem=pLi
20a15 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e  st->a; i<pList->
20a16 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
20a17 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  m++){.    if( sq
20a18 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69  lite3FixExpr(pFi
20a19 78 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  x, pItem->pExpr)
20a1a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
20a1b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
20a1c 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
20a1d 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
20a1e 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
20a1f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20a20 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69  nt sqlite3FixTri
20a21 67 67 65 72 53 74 65 70 28 0a 20 20 44 62 46 69  ggerStep(.  DbFi
20a22 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f  xer *pFix,     /
20a23 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
20a24 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54   fixation */.  T
20a25 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65  riggerStep *pSte
20a26 70 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72  p /* The trigger
20a27 20 73 74 65 70 20 62 65 20 66 69 78 65 64 20 74   step be fixed t
20a28 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a  o one database *
20a29 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53  /.){.  while( pS
20a2a 74 65 70 20 29 7b 0a 20 20 20 20 69 66 28 20 73  tep ){.    if( s
20a2b 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
20a2c 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 53 65  pFix, pStep->pSe
20a2d 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72  lect) ){.      r
20a2e 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
20a2f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
20a30 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 74 65  xExpr(pFix, pSte
20a31 70 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20  p->pWhere) ){.  
20a32 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
20a33 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
20a34 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70  te3FixExprList(p
20a35 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 45 78 70  Fix, pStep->pExp
20a36 72 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20  rList) ){.      
20a37 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
20a38 20 20 20 20 70 53 74 65 70 20 3d 20 70 53 74 65      pStep = pSte
20a39 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  p->pNext;.  }.  
20a3a 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
20a3b 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
20a3c 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 74 74 61 63  *** End of attac
20a3d 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
20a3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a40 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
20a41 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61  *** Begin file a
20a42 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  uth.c **********
20a43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a45 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a  **/./*.** 2003 J
20a46 61 6e 75 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20  anuary 11.**.** 
20a47 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
20a48 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
20a49 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
20a4a 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
20a4b 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
20a4c 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
20a4d 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
20a4e 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
20a4f 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
20a50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
20a51 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
20a52 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
20a53 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
20a54 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
20a55 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
20a56 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
20a57 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
20a58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a5c 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
20a5d 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
20a5e 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
20a5f 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65  ement the sqlite
20a60 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
20a61 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69 73  ().** API.  This
20a62 20 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e 20   facility is an 
20a63 6f 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72 65  optional feature
20a64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e   of the library.
20a65 20 20 45 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79    Embedded.** sy
20a66 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f  stems that do no
20a67 74 20 6e 65 65 64 20 74 68 69 73 20 66 61 63 69  t need this faci
20a68 6c 69 74 79 20 6d 61 79 20 6f 6d 69 74 20 69 74  lity may omit it
20a69 20 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a   by recompiling.
20a6a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 77  ** the library w
20a6b 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49  ith -DSQLITE_OMI
20a6c 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d  T_AUTHORIZATION=
20a6d 31 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 75 74  1.**.** $Id: aut
20a6e 68 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 37 2f  h.c,v 1.29 2007/
20a6f 30 39 2f 31 38 20 31 35 3a 35 35 3a 30 37 20 64  09/18 15:55:07 d
20a70 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  rh Exp $.*/../*.
20a71 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f  ** All of the co
20a72 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
20a73 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62  may be omitted b
20a74 79 20 64 65 66 69 6e 69 6e 67 20 61 20 73 69 6e  y defining a sin
20a75 67 6c 65 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f  gle.** macro..*/
20a76 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20a77 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
20a78 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72  ON../*.** Set or
20a79 20 63 6c 65 61 72 20 74 68 65 20 61 63 63 65 73   clear the acces
20a7a 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  s authorization 
20a7b 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
20a7c 54 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f  The access autho
20a7d 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
20a7e 6e 20 69 73 20 62 65 20 63 61 6c 6c 65 64 20 64  n is be called d
20a7f 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c  uring the compil
20a80 61 74 69 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 74  ation.** phase t
20a81 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68  o verify that th
20a82 65 20 75 73 65 72 20 68 61 73 20 72 65 61 64 20  e user has read 
20a83 61 6e 64 2f 6f 72 20 77 72 69 74 65 20 61 63 63  and/or write acc
20a84 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 6f  ess permission o
20a85 6e 0a 2a 2a 20 76 61 72 69 6f 75 73 20 66 69 65  n.** various fie
20a86 6c 64 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  lds of the datab
20a87 61 73 65 2e 20 20 54 68 65 20 66 69 72 73 74 20  ase.  The first 
20a88 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
20a89 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  auth function.**
20a8a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68   is a copy of th
20a8b 65 20 33 72 64 20 61 72 67 75 6d 65 6e 74 20 74  e 3rd argument t
20a8c 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  o this routine. 
20a8d 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
20a8e 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 61  ment.** to the a
20a8f 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  uth function is 
20a90 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f 6e  one of these con
20a91 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  stants:.**.**   
20a92 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54      SQLITE_CREAT
20a93 45 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20  E_INDEX.**      
20a94 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
20a95 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51  ABLE.**       SQ
20a96 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
20a97 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20  _INDEX.**       
20a98 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
20a99 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20  MP_TABLE.**     
20a9a 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
20a9b 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20  TEMP_TRIGGER.** 
20a9c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45        SQLITE_CRE
20a9d 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a  ATE_TEMP_VIEW.**
20a9e 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
20a9f 45 41 54 45 5f 54 52 49 47 47 45 52 0a 2a 2a 20  EATE_TRIGGER.** 
20aa0 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45        SQLITE_CRE
20aa1 41 54 45 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20  ATE_VIEW.**     
20aa2 20 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 0a    SQLITE_DELETE.
20aa3 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
20aa4 44 52 4f 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20  DROP_INDEX.**   
20aa5 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f      SQLITE_DROP_
20aa6 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53  TABLE.**       S
20aa7 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
20aa8 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53  INDEX.**       S
20aa9 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
20aaa 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53  TABLE.**       S
20aab 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
20aac 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20  TRIGGER.**      
20aad 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
20aae 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  P_VIEW.**       
20aaf 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47  SQLITE_DROP_TRIG
20ab0 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  GER.**       SQL
20ab1 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 0a 2a 2a  ITE_DROP_VIEW.**
20ab2 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e         SQLITE_IN
20ab3 53 45 52 54 0a 2a 2a 20 20 20 20 20 20 20 53 51  SERT.**       SQ
20ab4 4c 49 54 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 20  LITE_PRAGMA.**  
20ab5 20 20 20 20 20 53 51 4c 49 54 45 5f 52 45 41 44       SQLITE_READ
20ab6 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
20ab7 5f 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20  _SELECT.**      
20ab8 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54   SQLITE_TRANSACT
20ab9 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  ION.**       SQL
20aba 49 54 45 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a  ITE_UPDATE.**.**
20abb 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 66   The third and f
20abc 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20  ourth arguments 
20abd 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  to the auth func
20abe 74 69 6f 6e 20 61 72 65 20 74 68 65 20 6e 61 6d  tion are the nam
20abf 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  e of.** the tabl
20ac0 65 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e  e and the column
20ac1 20 74 68 61 74 20 61 72 65 20 62 65 69 6e 67 20   that are being 
20ac2 61 63 63 65 73 73 65 64 2e 20 20 54 68 65 20 61  accessed.  The a
20ac3 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  uth function.** 
20ac4 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69  should return ei
20ac5 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  ther SQLITE_OK, 
20ac6 53 51 4c 49 54 45 5f 44 45 4e 59 2c 20 6f 72 20  SQLITE_DENY, or 
20ac7 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2e 20 20  SQLITE_IGNORE.  
20ac8 49 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  If.** SQLITE_OK 
20ac9 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 20  is returned, it 
20aca 6d 65 61 6e 73 20 74 68 61 74 20 61 63 63 65 73  means that acces
20acb 73 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20 53  s is allowed.  S
20acc 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65  QLITE_DENY.** me
20acd 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 4c  ans that the SQL
20ace 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20   statement will 
20acf 6e 65 76 65 72 2d 72 75 6e 20 2d 20 74 68 65 20  never-run - the 
20ad0 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63  sqlite3_exec() c
20ad1 61 6c 6c 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75  all.** will retu
20ad2 72 6e 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  rn with an error
20ad3 2e 20 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  .  SQLITE_IGNORE
20ad4 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
20ad5 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  SQL statement.**
20ad6 20 73 68 6f 75 6c 64 20 72 75 6e 20 62 75 74 20   should run but 
20ad7 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61 64  attempts to read
20ad8 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63   the specified c
20ad9 6f 6c 75 6d 6e 20 77 69 6c 6c 20 72 65 74 75 72  olumn will retur
20ada 6e 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74  n NULL.** and at
20adb 74 65 6d 70 74 73 20 74 6f 20 77 72 69 74 65 20  tempts to write 
20adc 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20  the column will 
20add 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a  be ignored..**.*
20ade 2a 20 53 65 74 74 69 6e 67 20 74 68 65 20 61 75  * Setting the au
20adf 74 68 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e  th function to N
20ae0 55 4c 4c 20 64 69 73 61 62 6c 65 73 20 74 68 69  ULL disables thi
20ae1 73 20 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66  s hook.  The def
20ae2 61 75 6c 74 0a 2a 2a 20 73 65 74 74 69 6e 67 20  ault.** setting 
20ae3 6f 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  of the auth func
20ae4 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  tion is NULL..*/
20ae5 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
20ae6 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
20ae7 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65  orizer(.  sqlite
20ae8 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78  3 *db,.  int (*x
20ae9 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  Auth)(void*,int,
20aea 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
20aeb 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
20aec 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  ar*,const char*)
20aed 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29  ,.  void *pArg.)
20aee 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
20aef 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
20af0 78 29 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68 20  x);.  db->xAuth 
20af1 3d 20 78 41 75 74 68 3b 0a 20 20 64 62 2d 3e 70  = xAuth;.  db->p
20af2 41 75 74 68 41 72 67 20 3d 20 70 41 72 67 3b 0a  AuthArg = pArg;.
20af3 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50    sqlite3ExpireP
20af4 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
20af5 73 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  s(db);.  sqlite3
20af6 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
20af7 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
20af8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
20af9 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65  /*.** Write an e
20afa 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74  rror message int
20afb 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  o pParse->zErrMs
20afc 67 20 74 68 61 74 20 65 78 70 6c 61 69 6e 73 20  g that explains 
20afd 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 73 65 72  that the.** user
20afe 2d 73 75 70 70 6c 69 65 64 20 61 75 74 68 6f 72  -supplied author
20aff 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ization function
20b00 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6c 6c   returned an ill
20b01 65 67 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73  egal value..*/.s
20b02 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
20b03 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f  eAuthBadReturnCo
20b04 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  de(Parse *pParse
20b05 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 73 71 6c  , int rc){.  sql
20b06 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
20b07 72 73 65 2c 20 22 69 6c 6c 65 67 61 6c 20 72 65  rse, "illegal re
20b08 74 75 72 6e 20 76 61 6c 75 65 20 28 25 64 29 20  turn value (%d) 
20b09 66 72 6f 6d 20 74 68 65 20 22 0a 20 20 20 20 22  from the ".    "
20b0a 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75  authorization fu
20b0b 6e 63 74 69 6f 6e 20 2d 20 73 68 6f 75 6c 64 20  nction - should 
20b0c 62 65 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51  be SQLITE_OK, SQ
20b0d 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 22 0a 20  LITE_IGNORE, ". 
20b0e 20 20 20 22 6f 72 20 53 51 4c 49 54 45 5f 44 45     "or SQLITE_DE
20b0f 4e 59 22 2c 20 72 63 29 3b 0a 20 20 70 50 61 72  NY", rc);.  pPar
20b10 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
20b11 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ERROR;.}../*.** 
20b12 54 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c 64  The pExpr should
20b13 20 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20   be a TK_COLUMN 
20b14 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65  expression.  The
20b15 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20   table referred 
20b16 74 6f 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61 62  to.** is in pTab
20b17 4c 69 73 74 20 6f 72 20 65 6c 73 65 20 69 74 20  List or else it 
20b18 69 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c  is the NEW or OL
20b19 44 20 74 61 62 6c 65 20 6f 66 20 61 20 74 72 69  D table of a tri
20b1a 67 67 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b  gger.  .** Check
20b1b 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 69 73   to see if it is
20b1c 20 4f 4b 20 74 6f 20 72 65 61 64 20 74 68 69 73   OK to read this
20b1d 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75   particular colu
20b1e 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  mn..**.** If the
20b1f 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72   auth function r
20b20 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47  eturns SQLITE_IG
20b21 4e 4f 52 45 2c 20 63 68 61 6e 67 65 20 74 68 65  NORE, change the
20b22 20 54 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69   TK_COLUMN .** i
20b23 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20  nstruction into 
20b24 61 20 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74  a TK_NULL.  If t
20b25 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  he auth function
20b26 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
20b27 44 45 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65  DENY,.** then ge
20b28 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e  nerate an error.
20b29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20b2a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
20b2b 75 74 68 52 65 61 64 28 0a 20 20 50 61 72 73 65  uthRead(.  Parse
20b2c 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
20b2d 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
20b2e 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
20b2f 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
20b30 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
20b31 69 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75 74  ion to check aut
20b32 68 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f  horization on */
20b33 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  .  Schema *pSche
20b34 6d 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ma,      /* The 
20b35 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78  schema of the ex
20b36 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72  pression */.  Sr
20b37 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20  cList *pTabList 
20b38 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65      /* All table
20b39 20 74 68 61 74 20 70 45 78 70 72 20 6d 69 67 68   that pExpr migh
20b3a 74 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b  t refer to */.){
20b3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
20b3c 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
20b3d 6e 74 20 72 63 3b 0a 20 20 54 61 62 6c 65 20 2a  nt rc;.  Table *
20b3e 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 2f  pTab = 0;      /
20b3f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e  * The table bein
20b40 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73  g read */.  cons
20b41 74 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20  t char *zCol;   
20b42 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
20b43 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74   column of the t
20b44 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53  able */.  int iS
20b45 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
20b46 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 54 61 62  /* Index in pTab
20b47 4c 69 73 74 2d 3e 61 5b 5d 20 6f 66 20 74 61 62  List->a[] of tab
20b48 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f  le being read */
20b49 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
20b4a 44 42 61 73 65 3b 20 20 20 2f 2a 20 4e 61 6d 65  DBase;   /* Name
20b4b 20 6f 66 20 64 61 74 61 62 61 73 65 20 62 65 69   of database bei
20b4c 6e 67 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20  ng accessed */. 
20b4d 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70   TriggerStack *p
20b4e 53 74 61 63 6b 3b 20 2f 2a 20 54 68 65 20 73 74  Stack; /* The st
20b4f 61 63 6b 20 6f 66 20 63 75 72 72 65 6e 74 20 74  ack of current t
20b50 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74  riggers */.  int
20b51 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
20b52 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20     /* The index 
20b53 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
20b54 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72  the expression r
20b55 65 66 65 72 73 20 74 6f 20 2a 2f 0a 0a 20 20 69  efers to */..  i
20b56 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20  f( db->xAuth==0 
20b57 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
20b58 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f  pExpr->op!=TK_CO
20b59 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 3b 0a 20  LUMN ) return;. 
20b5a 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
20b5b 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
20b5c 73 65 2d 3e 64 62 2c 20 70 53 63 68 65 6d 61 29  se->db, pSchema)
20b5d 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b  ;.  if( iDb<0 ){
20b5e 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74 74 65 6d  .    /* An attem
20b5f 70 74 20 74 6f 20 72 65 61 64 20 61 20 63 6f 6c  pt to read a col
20b60 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20 73 75 62  umn out of a sub
20b61 71 75 65 72 79 20 6f 72 20 6f 74 68 65 72 0a 20  query or other. 
20b62 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20     ** temporary 
20b63 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 65  table. */.    re
20b64 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
20b65 69 53 72 63 3d 30 3b 20 70 54 61 62 4c 69 73 74  iSrc=0; pTabList
20b66 20 26 26 20 69 53 72 63 3c 70 54 61 62 4c 69 73   && iSrc<pTabLis
20b67 74 2d 3e 6e 53 72 63 3b 20 69 53 72 63 2b 2b 29  t->nSrc; iSrc++)
20b68 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d  {.    if( pExpr-
20b69 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69 73  >iTable==pTabLis
20b6a 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72 73  t->a[iSrc].iCurs
20b6b 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  or ) break;.  }.
20b6c 20 20 69 66 28 20 69 53 72 63 3e 3d 30 20 26 26    if( iSrc>=0 &&
20b6d 20 70 54 61 62 4c 69 73 74 20 26 26 20 69 53 72   pTabList && iSr
20b6e 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  c<pTabList->nSrc
20b6f 20 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 70   ){.    pTab = p
20b70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d  TabList->a[iSrc]
20b71 2e 70 54 61 62 3b 0a 20 20 7d 65 6c 73 65 20 69  .pTab;.  }else i
20b72 66 28 20 28 70 53 74 61 63 6b 20 3d 20 70 50 61  f( (pStack = pPa
20b73 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 29 21  rse->trigStack)!
20b74 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  =0 ){.    /* Thi
20b75 73 20 6d 75 73 74 20 62 65 20 61 6e 20 61 74 74  s must be an att
20b76 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 65  empt to read the
20b77 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75   NEW or OLD pseu
20b78 64 6f 2d 74 61 62 6c 65 73 0a 20 20 20 20 2a 2a  do-tables.    **
20b79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a 20   of a trigger.. 
20b7a 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
20b7b 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d  ( pExpr->iTable=
20b7c 3d 70 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20  =pStack->newIdx 
20b7d 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  || pExpr->iTable
20b7e 3d 3d 70 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78  ==pStack->oldIdx
20b7f 20 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70   );.    pTab = p
20b80 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 20 7d  Stack->pTab;.  }
20b81 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
20b82 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
20b83 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30  Expr->iColumn>=0
20b84 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
20b85 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70  pExpr->iColumn<p
20b86 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20  Tab->nCol );.   
20b87 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43   zCol = pTab->aC
20b88 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  ol[pExpr->iColum
20b89 6e 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73  n].zName;.  }els
20b8a 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b 65  e if( pTab->iPKe
20b8b 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  y>=0 ){.    asse
20b8c 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c  rt( pTab->iPKey<
20b8d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
20b8e 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
20b8f 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d  Col[pTab->iPKey]
20b90 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b  .zName;.  }else{
20b91 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f 57  .    zCol = "ROW
20b92 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ID";.  }.  asser
20b93 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
20b94 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44  <db->nDb );.  zD
20b95 42 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69  Base = db->aDb[i
20b96 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 20  Db].zName;.  rc 
20b97 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e  = db->xAuth(db->
20b98 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54 45  pAuthArg, SQLITE
20b99 5f 52 45 41 44 2c 20 70 54 61 62 2d 3e 7a 4e 61  _READ, pTab->zNa
20b9a 6d 65 2c 20 7a 43 6f 6c 2c 20 7a 44 42 61 73 65  me, zCol, zDBase
20b9b 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
20b9c 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74      pParse->zAut
20b9d 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28  hContext);.  if(
20b9e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f   rc==SQLITE_IGNO
20b9f 52 45 20 29 7b 0a 20 20 20 20 70 45 78 70 72 2d  RE ){.    pExpr-
20ba0 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20  >op = TK_NULL;. 
20ba1 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
20ba2 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20  QLITE_DENY ){.  
20ba3 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32 20    if( db->nDb>2 
20ba4 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20 20 20  || iDb!=0 ){.   
20ba5 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
20ba6 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63 65  sg(pParse, "acce
20ba7 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73 20 69  ss to %s.%s.%s i
20ba8 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20 0a  s prohibited", .
20ba9 20 20 20 20 20 20 20 20 20 7a 44 42 61 73 65 2c           zDBase,
20baa 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
20bab 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ol);.    }else{.
20bac 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
20bad 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61  orMsg(pParse, "a
20bae 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20 69  ccess to %s.%s i
20baf 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 70 54  s prohibited",pT
20bb0 61 62 2d 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29 3b  ab->zName,zCol);
20bb1 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
20bb2 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41  e->rc = SQLITE_A
20bb3 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  UTH;.  }else if(
20bb4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
20bb5 7b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68  {.    sqliteAuth
20bb6 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50  BadReturnCode(pP
20bb7 61 72 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 7d  arse, rc);.  }.}
20bb8 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75  ../*.** Do an au
20bb9 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63  thorization chec
20bba 6b 20 75 73 69 6e 67 20 74 68 65 20 63 6f 64 65  k using the code
20bbb 20 61 6e 64 20 61 72 67 75 6d 65 6e 74 73 20 67   and arguments g
20bbc 69 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  iven.  Return.**
20bbd 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f   either SQLITE_O
20bbe 4b 20 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49  K (zero) or SQLI
20bbf 54 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c  TE_IGNORE or SQL
20bc0 49 54 45 5f 44 45 4e 59 2e 20 20 49 66 20 53 51  ITE_DENY.  If SQ
20bc1 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20  LITE_DENY.** is 
20bc2 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 74  returned, then t
20bc3 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 61  he error count a
20bc4 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  nd error message
20bc5 20 69 6e 20 70 50 61 72 73 65 20 61 72 65 0a 2a   in pParse are.*
20bc6 2a 20 6d 6f 64 69 66 69 65 64 20 61 70 70 72 6f  * modified appro
20bc7 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c  priately..*/.SQL
20bc8 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
20bc9 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
20bca 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
20bcb 65 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20  e,.  int code,. 
20bcc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
20bcd 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  g1,.  const char
20bce 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74   *zArg2,.  const
20bcf 20 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a   char *zArg3.){.
20bd0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
20bd1 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
20bd2 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27  t rc;..  /* Don'
20bd3 74 20 64 6f 20 61 6e 79 20 61 75 74 68 6f 72 69  t do any authori
20bd4 7a 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66  zation checks if
20bd5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
20bd6 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20 20   initialising.  
20bd7 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 72  ** or if the par
20bd8 73 65 72 20 69 73 20 62 65 69 6e 67 20 69 6e 76  ser is being inv
20bd9 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  oked from within
20bda 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
20bdb 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66  _vtab..  */.  if
20bdc 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ( db->init.busy 
20bdd 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  || IN_DECLARE_VT
20bde 41 42 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  AB ){.    return
20bdf 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
20be0 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68  .  if( db->xAuth
20be1 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
20be2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
20be3 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74  .  rc = db->xAut
20be4 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20  h(db->pAuthArg, 
20be5 63 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72  code, zArg1, zAr
20be6 67 32 2c 20 7a 41 72 67 33 2c 20 70 50 61 72 73  g2, zArg3, pPars
20be7 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29  e->zAuthContext)
20be8 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
20be9 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 73  TE_DENY ){.    s
20bea 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
20beb 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68  Parse, "not auth
20bec 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 70 50  orized");.    pP
20bed 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
20bee 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20  E_AUTH;.  }else 
20bef 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
20bf0 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  K && rc!=SQLITE_
20bf1 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 72 63  IGNORE ){.    rc
20bf2 20 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a   = SQLITE_DENY;.
20bf3 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61      sqliteAuthBa
20bf4 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72  dReturnCode(pPar
20bf5 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72  se, rc);.  }.  r
20bf6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
20bf7 2a 2a 20 50 75 73 68 20 61 6e 20 61 75 74 68 6f  ** Push an autho
20bf8 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  rization context
20bf9 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  .  After this ro
20bfa 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
20bfb 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72   the.** zArg3 ar
20bfc 67 75 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f 72  gument to author
20bfd 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
20bfe 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65  s will be zConte
20bff 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70  xt until.** popp
20c00 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73  ed.  Or if pPars
20c01 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69  e==0, this routi
20c02 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
20c03 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20c04 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
20c05 68 43 6f 6e 74 65 78 74 50 75 73 68 28 0a 20 20  hContextPush(.  
20c06 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20  Parse *pParse,. 
20c07 20 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43   AuthContext *pC
20c08 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74  ontext, .  const
20c09 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a   char *zContext.
20c0a 29 7b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70  ){.  pContext->p
20c0b 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
20c0c 20 20 69 66 28 20 70 50 61 72 73 65 20 29 7b 0a    if( pParse ){.
20c0d 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41      pContext->zA
20c0e 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61  uthContext = pPa
20c0f 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
20c10 74 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a  t;.    pParse->z
20c11 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43  AuthContext = zC
20c12 6f 6e 74 65 78 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ontext;.  }.}../
20c13 2a 0a 2a 2a 20 50 6f 70 20 61 6e 20 61 75 74 68  *.** Pop an auth
20c14 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78  orization contex
20c15 74 20 74 68 61 74 20 77 61 73 20 70 72 65 76 69  t that was previ
20c16 6f 75 73 6c 79 20 70 75 73 68 65 64 0a 2a 2a 20  ously pushed.** 
20c17 62 79 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  by sqlite3AuthCo
20c18 6e 74 65 78 74 50 75 73 68 0a 2a 2f 0a 53 51 4c  ntextPush.*/.SQL
20c19 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
20c1a 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
20c1b 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65  extPop(AuthConte
20c1c 78 74 20 2a 70 43 6f 6e 74 65 78 74 29 7b 0a 20  xt *pContext){. 
20c1d 20 69 66 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70   if( pContext->p
20c1e 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f  Parse ){.    pCo
20c1f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a  ntext->pParse->z
20c20 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 43  AuthContext = pC
20c21 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e  ontext->zAuthCon
20c22 74 65 78 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65  text;.    pConte
20c23 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 30 3b 0a  xt->pParse = 0;.
20c24 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
20c25 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
20c26 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 2f  HORIZATION */../
20c27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
20c28 6e 64 20 6f 66 20 61 75 74 68 2e 63 20 2a 2a 2a  nd of auth.c ***
20c29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
20c2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
20c2d 65 67 69 6e 20 66 69 6c 65 20 62 75 69 6c 64 2e  egin file build.
20c2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
20c2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
20c31 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
20c32 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
20c33 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
20c34 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
20c35 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
20c36 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
20c37 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
20c38 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
20c39 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
20c3a 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
20c3b 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
20c3c 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
20c3d 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
20c3e 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
20c3f 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
20c40 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
20c41 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
20c42 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
20c43 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
20c44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20c48 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
20c49 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
20c4a 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
20c4b 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74   are called by t
20c4c 68 65 20 53 51 4c 69 74 65 20 70 61 72 73 65 72  he SQLite parser
20c4d 0a 2a 2a 20 77 68 65 6e 20 73 79 6e 74 61 78 20  .** when syntax 
20c4e 72 75 6c 65 73 20 61 72 65 20 72 65 64 75 63 65  rules are reduce
20c4f 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73  d.  The routines
20c50 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 68 61   in this file ha
20c51 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c  ndle the.** foll
20c52 6f 77 69 6e 67 20 6b 69 6e 64 73 20 6f 66 20 53  owing kinds of S
20c53 51 4c 20 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a  QL syntax:.**.**
20c54 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
20c55 45 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 54 41  E.**     DROP TA
20c56 42 4c 45 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  BLE.**     CREAT
20c57 45 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 44  E INDEX.**     D
20c58 52 4f 50 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20  ROP INDEX.**    
20c59 20 63 72 65 61 74 69 6e 67 20 49 44 20 6c 69 73   creating ID lis
20c5a 74 73 0a 2a 2a 20 20 20 20 20 42 45 47 49 4e 20  ts.**     BEGIN 
20c5b 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20  TRANSACTION.**  
20c5c 20 20 20 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 20     COMMIT.**    
20c5d 20 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a 20   ROLLBACK.**.** 
20c5e 24 49 64 3a 20 62 75 69 6c 64 2e 63 2c 76 20 31  $Id: build.c,v 1
20c5f 2e 35 32 37 20 32 30 30 39 2f 30 33 2f 33 31 20  .527 2009/03/31 
20c60 30 33 3a 34 31 3a 35 37 20 73 68 61 6e 65 20 45  03:41:57 shane E
20c61 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  xp $.*/../*.** T
20c62 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
20c63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 6e 65 77  alled when a new
20c64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69   SQL statement i
20c65 73 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a  s beginning to.*
20c66 2a 20 62 65 20 70 61 72 73 65 64 2e 20 20 49 6e  * be parsed.  In
20c67 69 74 69 61 6c 69 7a 65 20 74 68 65 20 70 50 61  itialize the pPa
20c68 72 73 65 20 73 74 72 75 63 74 75 72 65 20 61 73  rse structure as
20c69 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49   needed..*/.SQLI
20c6a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
20c6b 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73  sqlite3BeginPars
20c6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
20c6d 20 69 6e 74 20 65 78 70 6c 61 69 6e 46 6c 61 67   int explainFlag
20c6e 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 65 78 70  ){.  pParse->exp
20c6f 6c 61 69 6e 20 3d 20 28 75 38 29 65 78 70 6c 61  lain = (u8)expla
20c70 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61 72 73 65  inFlag;.  pParse
20c71 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a 0a 23  ->nVar = 0;.}..#
20c72 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20c73 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
20c74 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 4c  /*.** The TableL
20c75 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20 69 73  ock structure is
20c76 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 74 68   only used by th
20c77 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f  e sqlite3TableLo
20c78 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 6f 64 65  ck() and.** code
20c79 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66 75 6e  TableLocks() fun
20c7a 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63  ctions..*/.struc
20c7b 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a 20 20  t TableLock {.  
20c7c 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
20c7d 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
20c7e 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
20c7f 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
20c80 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20  locked */.  int 
20c81 69 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  iTab;           
20c82 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67   /* The root pag
20c83 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
20c84 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20  o be locked */. 
20c85 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b   u8 isWriteLock;
20c86 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
20c87 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e 20 20 46  r write lock.  F
20c88 61 6c 73 65 20 66 6f 72 20 61 20 72 65 61 64 20  alse for a read 
20c89 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  lock */.  const 
20c8a 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f  char *zName;   /
20c8b 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
20c8c 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ble */.};../*.**
20c8d 20 52 65 63 6f 72 64 20 74 68 65 20 66 61 63 74   Record the fact
20c8e 20 74 68 61 74 20 77 65 20 77 61 6e 74 20 74 6f   that we want to
20c8f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20 61 74   lock a table at
20c90 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a 2a 0a   run-time.  .**.
20c91 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  ** The table to 
20c92 62 65 20 6c 6f 63 6b 65 64 20 68 61 73 20 72 6f  be locked has ro
20c93 6f 74 20 70 61 67 65 20 69 54 61 62 20 61 6e 64  ot page iTab and
20c94 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 64 61 74   is found in dat
20c95 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41 20  abase iDb..** A 
20c96 72 65 61 64 20 6f 72 20 61 20 77 72 69 74 65 20  read or a write 
20c97 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74 61 6b 65  lock can be take
20c98 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 69  n depending on i
20c99 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  sWritelock..**.*
20c9a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a  * This routine j
20c9b 75 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20  ust records the 
20c9c 66 61 63 74 20 74 68 61 74 20 74 68 65 20 6c 6f  fact that the lo
20c9d 63 6b 20 69 73 20 64 65 73 69 72 65 64 2e 20 20  ck is desired.  
20c9e 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 6d  The.** code to m
20c9f 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f 63 63  ake the lock occ
20ca0 75 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  ur is generated 
20ca1 62 79 20 61 20 6c 61 74 65 72 20 63 61 6c 6c 20  by a later call 
20ca2 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c  to.** codeTableL
20ca3 6f 63 6b 73 28 29 20 77 68 69 63 68 20 6f 63 63  ocks() which occ
20ca4 75 72 73 20 64 75 72 69 6e 67 20 73 71 6c 69 74  urs during sqlit
20ca5 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 29  e3FinishCoding()
20ca6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20ca7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20ca8 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50 61 72  TableLock(.  Par
20ca9 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
20caa 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
20cab 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
20cac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
20cad 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
20cae 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ase containing t
20caf 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b  he table to lock
20cb0 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20   */.  int iTab, 
20cb1 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
20cb2 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
20cb3 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
20cb4 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69  locked */.  u8 i
20cb5 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20 20 2f  sWriteLock,    /
20cb6 2a 20 54 72 75 65 20 66 6f 72 20 61 20 77 72 69  * True for a wri
20cb7 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e  te lock */.  con
20cb8 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20  st char *zName  
20cb9 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
20cba 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  able to be locke
20cbb 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  d */.){.  int i;
20cbc 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 0a 20  .  int nBytes;. 
20cbd 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0a 0a   TableLock *p;..
20cbe 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20    if( iDb<0 ){. 
20cbf 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
20cc0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
20cc1 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b  rse->nTableLock;
20cc2 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 26   i++){.    p = &
20cc3 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f  pParse->aTableLo
20cc4 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  ck[i];.    if( p
20cc5 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26 20 70 2d  ->iDb==iDb && p-
20cc6 3e 69 54 61 62 3d 3d 69 54 61 62 20 29 7b 0a 20  >iTab==iTab ){. 
20cc7 20 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c       p->isWriteL
20cc8 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57 72 69 74  ock = (p->isWrit
20cc9 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 69 74 65  eLock || isWrite
20cca 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 72 65 74  Lock);.      ret
20ccb 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  urn;.    }.  }..
20ccc 20 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f    nBytes = sizeo
20ccd 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20 2a 20 28  f(TableLock) * (
20cce 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
20ccf 63 6b 2b 31 29 3b 0a 20 20 70 50 61 72 73 65 2d  ck+1);.  pParse-
20cd0 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 0a 20  >aTableLock = . 
20cd1 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65       sqlite3DbRe
20cd2 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 50 61 72  allocOrFree(pPar
20cd3 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e  se->db, pParse->
20cd4 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e 42 79 74  aTableLock, nByt
20cd5 65 73 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  es);.  if( pPars
20cd6 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 29 7b  e->aTableLock ){
20cd7 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 73 65  .    p = &pParse
20cd8 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 70 50 61  ->aTableLock[pPa
20cd9 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b  rse->nTableLock+
20cda 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44 62 20 3d  +];.    p->iDb =
20cdb 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61   iDb;.    p->iTa
20cdc 62 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 70 2d  b = iTab;.    p-
20cdd 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 69  >isWriteLock = i
20cde 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20  sWriteLock;.    
20cdf 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65  p->zName = zName
20ce0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
20ce1 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63  Parse->nTableLoc
20ce2 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73  k = 0;.    pPars
20ce3 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
20ce4 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a  led = 1;.  }.}..
20ce5 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50  /*.** Code an OP
20ce6 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72  _TableLock instr
20ce7 75 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20  uction for each 
20ce8 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62 79 20  table locked by 
20ce9 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  the.** statement
20cea 20 28 63 6f 6e 66 69 67 75 72 65 64 20 62 79 20   (configured by 
20ceb 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
20cec 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a 2a 2f  TableLock())..*/
20ced 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
20cee 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50 61 72 73  eTableLocks(Pars
20cef 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e  e *pParse){.  in
20cf0 74 20 69 3b 0a 20 20 56 64 62 65 20 2a 70 56 64  t i;.  Vdbe *pVd
20cf1 62 65 3b 20 0a 0a 20 20 69 66 28 20 30 3d 3d 28  be; ..  if( 0==(
20cf2 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 47  pVdbe = sqlite3G
20cf3 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29 20  etVdbe(pParse)) 
20cf4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
20cf5 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   }..  for(i=0; i
20cf6 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c  <pParse->nTableL
20cf7 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 54  ock; i++){.    T
20cf8 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d 20 26 70  ableLock *p = &p
20cf9 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63  Parse->aTableLoc
20cfa 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 70 31  k[i];.    int p1
20cfb 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20 20 20 73   = p->iDb;.    s
20cfc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
20cfd 28 70 56 64 62 65 2c 20 4f 50 5f 54 61 62 6c 65  (pVdbe, OP_Table
20cfe 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e 69 54 61  Lock, p1, p->iTa
20cff 62 2c 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63  b, p->isWriteLoc
20d00 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k,.             
20d01 20 20 20 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d           p->zNam
20d02 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
20d03 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65   }.}.#else.  #de
20d04 66 69 6e 65 20 63 6f 64 65 54 61 62 6c 65 4c 6f  fine codeTableLo
20d05 63 6b 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f  cks(x).#endif../
20d06 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
20d07 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  e is called afte
20d08 72 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73  r a single SQL s
20d09 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65  tatement has bee
20d0a 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61 6e 64 20  n.** parsed and 
20d0b 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 74  a VDBE program t
20d0c 6f 20 65 78 65 63 75 74 65 20 74 68 61 74 20 73  o execute that s
20d0d 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65  tatement has bee
20d0e 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64 2e 20 20  n.** prepared.  
20d0f 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 75 74  This routine put
20d10 73 20 74 68 65 20 66 69 6e 69 73 68 69 6e 67 20  s the finishing 
20d11 74 6f 75 63 68 65 73 20 6f 6e 20 74 68 65 0a 2a  touches on the.*
20d12 2a 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61  * VDBE program a
20d13 6e 64 20 72 65 73 65 74 73 20 74 68 65 20 70 50  nd resets the pP
20d14 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66  arse structure f
20d15 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 70  or the next.** p
20d16 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  arse..**.** Note
20d17 20 74 68 61 74 20 69 66 20 61 6e 20 65 72 72 6f   that if an erro
20d18 72 20 6f 63 63 75 72 72 65 64 2c 20 69 74 20 6d  r occurred, it m
20d19 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65  ight be the case
20d1a 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 45   that.** no VDBE
20d1b 20 63 6f 64 65 20 77 61 73 20 67 65 6e 65 72 61   code was genera
20d1c 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
20d1d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20d1e 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28  te3FinishCoding(
20d1f 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
20d20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
20d21 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 64 62 20   Vdbe *v;..  db 
20d22 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
20d23 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
20d24 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
20d25 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
20d26 74 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ted ) return;.  
20d27 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
20d28 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a   ) return;..  /*
20d29 20 42 65 67 69 6e 20 62 79 20 67 65 6e 65 72 61   Begin by genera
20d2a 74 69 6e 67 20 73 6f 6d 65 20 74 65 72 6d 69 6e  ting some termin
20d2b 61 74 69 6f 6e 20 63 6f 64 65 20 61 74 20 74 68  ation code at th
20d2c 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a  e end of the.  *
20d2d 2a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 0a 20  * vdbe program. 
20d2e 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
20d2f 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
20d30 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
20d31 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20d32 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a  p0(v, OP_Halt);.
20d33 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6f 6b  .    /* The cook
20d34 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 73  ie mask contains
20d35 20 6f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63   one bit for eac
20d36 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  h database file 
20d37 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 28 42 69  open..    ** (Bi
20d38 74 20 30 20 69 73 20 66 6f 72 20 6d 61 69 6e 2c  t 0 is for main,
20d39 20 62 69 74 20 31 20 69 73 20 66 6f 72 20 74 65   bit 1 is for te
20d3a 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  mp, and so forth
20d3b 2e 29 20 20 42 69 74 73 20 61 72 65 0a 20 20 20  .)  Bits are.   
20d3c 20 2a 2a 20 73 65 74 20 66 6f 72 20 65 61 63 68   ** set for each
20d3d 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 69   database that i
20d3e 73 20 75 73 65 64 2e 20 20 47 65 6e 65 72 61 74  s used.  Generat
20d3f 65 20 63 6f 64 65 20 74 6f 20 73 74 61 72 74 20  e code to start 
20d40 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63  a.    ** transac
20d41 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20 75 73 65  tion on each use
20d42 64 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74  d database and t
20d43 6f 20 76 65 72 69 66 79 20 74 68 65 20 73 63 68  o verify the sch
20d44 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a  ema cookie.    *
20d45 2a 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20 64  * on each used d
20d46 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a  atabase..    */.
20d47 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
20d48 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 29 7b 0a  cookieGoto>0 ){.
20d49 20 20 20 20 20 20 75 33 32 20 6d 61 73 6b 3b 0a        u32 mask;.
20d4a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20        int iDb;. 
20d4b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20d4c 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 50 61 72  JumpHere(v, pPar
20d4d 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 2d 31  se->cookieGoto-1
20d4e 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 44 62  );.      for(iDb
20d4f 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 44 62 3c  =0, mask=1; iDb<
20d50 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b 3c 3c 3d  db->nDb; mask<<=
20d51 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20  1, iDb++){.     
20d52 20 20 20 69 66 28 20 28 6d 61 73 6b 20 26 20 70     if( (mask & p
20d53 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73  Parse->cookieMas
20d54 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  k)==0 ) continue
20d55 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
20d56 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
20d57 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20  , iDb);.        
20d58 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20d59 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61 63 74 69  2(v,OP_Transacti
20d5a 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73 6b 20 26  on, iDb, (mask &
20d5b 20 70 50 61 72 73 65 2d 3e 77 72 69 74 65 4d 61   pParse->writeMa
20d5c 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20 20 20 20  sk)!=0);.       
20d5d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20d5e 70 32 28 76 2c 4f 50 5f 56 65 72 69 66 79 43 6f  p2(v,OP_VerifyCo
20d5f 6f 6b 69 65 2c 20 69 44 62 2c 20 70 50 61 72 73  okie, iDb, pPars
20d60 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69  e->cookieValue[i
20d61 44 62 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 69  Db]);.      }.#i
20d62 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
20d63 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
20d64 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69       {.        i
20d65 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f  nt i;.        fo
20d66 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d  r(i=0; i<pParse-
20d67 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29  >nVtabLock; i++)
20d68 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72  {.          char
20d69 20 2a 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a   *vtab = (char *
20d6a 29 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c  )pParse->apVtabL
20d6b 6f 63 6b 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20  ock[i]->pVtab;. 
20d6c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
20d6d 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
20d6e 5f 56 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30  _VBegin, 0, 0, 0
20d6f 2c 20 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29  , vtab, P4_VTAB)
20d70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
20d71 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61      pParse->nVta
20d72 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20  bLock = 0;.     
20d73 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20   }.#endif..     
20d74 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65   /* Once all the
20d75 20 63 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65   cookies have be
20d76 65 6e 20 76 65 72 69 66 69 65 64 20 61 6e 64 20  en verified and 
20d77 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65  transactions ope
20d78 6e 65 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f  ned, .      ** o
20d79 62 74 61 69 6e 20 74 68 65 20 72 65 71 75 69 72  btain the requir
20d7a 65 64 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20  ed table-locks. 
20d7b 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20  This is a no-op 
20d7c 75 6e 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20  unless the .    
20d7d 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68    ** shared-cach
20d7e 65 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61  e feature is ena
20d7f 62 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  bled..      */. 
20d80 20 20 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f       codeTableLo
20d81 63 6b 73 28 70 50 61 72 73 65 29 3b 0a 20 20 20  cks(pParse);.   
20d82 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20d83 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
20d84 20 30 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b   0, pParse->cook
20d85 69 65 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 20  ieGoto);.    }. 
20d86 20 7d 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68   }...  /* Get th
20d87 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72  e VDBE program r
20d88 65 61 64 79 20 66 6f 72 20 65 78 65 63 75 74 69  eady for executi
20d89 6f 6e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76 20  on.  */.  if( v 
20d8a 26 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  && pParse->nErr=
20d8b 3d 30 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f  =0 && !db->mallo
20d8c 63 46 61 69 6c 65 64 20 29 7b 0a 23 69 66 64 65  cFailed ){.#ifde
20d8d 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
20d8e 20 20 20 46 49 4c 45 20 2a 74 72 61 63 65 20 3d     FILE *trace =
20d8f 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
20d90 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 29 21  LITE_VdbeTrace)!
20d91 3d 30 20 3f 20 73 74 64 6f 75 74 20 3a 20 30 3b  =0 ? stdout : 0;
20d92 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
20d93 54 72 61 63 65 28 76 2c 20 74 72 61 63 65 29 3b  Trace(v, trace);
20d94 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65  .#endif.    asse
20d95 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  rt( pParse->disa
20d96 62 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29  bleColCache==0 )
20d97 3b 20 20 2f 2a 20 44 69 73 61 62 6c 65 73 20 61  ;  /* Disables a
20d98 6e 64 20 72 65 2d 65 6e 61 62 6c 65 73 20 6d 61  nd re-enables ma
20d99 74 63 68 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  tch */.    sqlit
20d9a 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28  e3VdbeMakeReady(
20d9b 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c  v, pParse->nVar,
20d9c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2c 0a 20   pParse->nMem,. 
20d9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d9e 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
20d9f 6e 54 61 62 2c 20 70 50 61 72 73 65 2d 3e 65 78  nTab, pParse->ex
20da0 70 6c 61 69 6e 29 3b 0a 20 20 20 20 70 50 61 72  plain);.    pPar
20da1 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
20da2 44 4f 4e 45 3b 0a 20 20 20 20 70 50 61 72 73 65  DONE;.    pParse
20da3 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20  ->colNamesSet = 
20da4 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  0;.  }else if( p
20da5 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
20da6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72  E_OK ){.    pPar
20da7 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
20da8 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61  ERROR;.  }.  pPa
20da9 72 73 65 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20  rse->nTab = 0;. 
20daa 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
20dab 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 65  0;.  pParse->nSe
20dac 74 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d  t = 0;.  pParse-
20dad 3e 6e 56 61 72 20 3d 20 30 3b 0a 20 20 70 50 61  >nVar = 0;.  pPa
20dae 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20  rse->cookieMask 
20daf 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63  = 0;.  pParse->c
20db0 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d  ookieGoto = 0;.}
20db1 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20  ../*.** Run the 
20db2 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65 20  parser and code 
20db3 67 65 6e 65 72 61 74 6f 72 20 72 65 63 75 72 73  generator recurs
20db4 69 76 65 6c 79 20 69 6e 20 6f 72 64 65 72 20 74  ively in order t
20db5 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f  o generate.** co
20db6 64 65 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73  de for the SQL s
20db7 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f  tatement given o
20db8 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74  nto the end of t
20db9 68 65 20 70 50 61 72 73 65 20 63 6f 6e 74 65 78  he pParse contex
20dba 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75  t.** currently u
20dbb 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
20dbc 6e 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 72  n.  When the par
20dbd 73 65 72 20 69 73 20 72 75 6e 20 72 65 63 75 72  ser is run recur
20dbe 73 69 76 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77  sively.** this w
20dbf 61 79 2c 20 74 68 65 20 66 69 6e 61 6c 20 4f 50  ay, the final OP
20dc0 5f 48 61 6c 74 20 69 73 20 6e 6f 74 20 61 70 70  _Halt is not app
20dc1 65 6e 64 65 64 20 61 6e 64 20 6f 74 68 65 72 20  ended and other 
20dc2 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a  initialization.*
20dc3 2a 20 61 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69  * and finalizati
20dc4 6f 6e 20 73 74 65 70 73 20 61 72 65 20 6f 6d 69  on steps are omi
20dc5 74 74 65 64 20 62 65 63 61 75 73 65 20 74 68 6f  tted because tho
20dc6 73 65 20 61 72 65 20 68 61 6e 64 6c 69 6e 67 20  se are handling 
20dc7 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d  by the.** outerm
20dc8 6f 73 74 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a  ost parser..**.*
20dc9 2a 20 4e 6f 74 20 65 76 65 72 79 74 68 69 6e 67  * Not everything
20dca 20 69 73 20 6e 65 73 74 61 62 6c 65 2e 20 20 54   is nestable.  T
20dcb 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20  his facility is 
20dcc 64 65 73 69 67 6e 65 64 20 74 6f 20 70 65 72 6d  designed to perm
20dcd 69 74 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50  it.** INSERT, UP
20dce 44 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45  DATE, and DELETE
20dcf 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 67 61 69   operations agai
20dd0 6e 73 74 20 53 51 4c 49 54 45 5f 4d 41 53 54 45  nst SQLITE_MASTE
20dd1 52 2e 20 20 55 73 65 0a 2a 2a 20 63 61 72 65 20  R.  Use.** care 
20dd2 69 66 20 79 6f 75 20 64 65 63 69 64 65 20 74 6f  if you decide to
20dd3 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 69 73   try to use this
20dd4 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d   routine for som
20dd5 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73  e other purposes
20dd6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20dd7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20dd8 4e 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73  NestedParse(Pars
20dd9 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
20dda 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
20ddb 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
20ddc 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  ap;.  char *zSql
20ddd 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  ;.  char *zErrMs
20dde 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  g = 0;.  sqlite3
20ddf 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
20de0 62 3b 0a 23 20 64 65 66 69 6e 65 20 53 41 56 45  b;.# define SAVE
20de1 5f 53 5a 20 20 28 73 69 7a 65 6f 66 28 50 61 72  _SZ  (sizeof(Par
20de2 73 65 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 50  se) - offsetof(P
20de3 61 72 73 65 2c 6e 56 61 72 29 29 0a 20 20 63 68  arse,nVar)).  ch
20de4 61 72 20 73 61 76 65 42 75 66 5b 53 41 56 45 5f  ar saveBuf[SAVE_
20de5 53 5a 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72  SZ];..  if( pPar
20de6 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72  se->nErr ) retur
20de7 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  n;.  assert( pPa
20de8 72 73 65 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29  rse->nested<10 )
20de9 3b 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68  ;  /* Nesting sh
20dea 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20  ould only be of 
20deb 6c 69 6d 69 74 65 64 20 64 65 70 74 68 20 2a 2f  limited depth */
20dec 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
20ded 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c  zFormat);.  zSql
20dee 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
20def 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  tf(db, zFormat, 
20df0 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
20df1 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30  );.  if( zSql==0
20df2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20   ){.    return; 
20df3 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75    /* A malloc mu
20df4 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a  st have failed *
20df5 2f 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e  /.  }.  pParse->
20df6 6e 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63  nested++;.  memc
20df7 70 79 28 73 61 76 65 42 75 66 2c 20 26 70 50 61  py(saveBuf, &pPa
20df8 72 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f  rse->nVar, SAVE_
20df9 53 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70  SZ);.  memset(&p
20dfa 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20  Parse->nVar, 0, 
20dfb 53 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69  SAVE_SZ);.  sqli
20dfc 74 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61  te3RunParser(pPa
20dfd 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72  rse, zSql, &zErr
20dfe 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Msg);.  sqlite3D
20dff 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73  bFree(db, zErrMs
20e00 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  g);.  sqlite3DbF
20e01 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20  ree(db, zSql);. 
20e02 20 6d 65 6d 63 70 79 28 26 70 50 61 72 73 65 2d   memcpy(&pParse-
20e03 3e 6e 56 61 72 2c 20 73 61 76 65 42 75 66 2c 20  >nVar, saveBuf, 
20e04 53 41 56 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72  SAVE_SZ);.  pPar
20e05 73 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a  se->nested--;.}.
20e06 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68  ./*.** Locate th
20e07 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75  e in-memory stru
20e08 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72  cture that descr
20e09 69 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61  ibes a particula
20e0a 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61  r database.** ta
20e0b 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61  ble given the na
20e0c 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65  me of that table
20e0d 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79   and (optionally
20e0e 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  ) the name of th
20e0f 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  e.** database co
20e10 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62  ntaining the tab
20e11 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c  le.  Return NULL
20e12 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a   if not found..*
20e13 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73  *.** If zDatabas
20e14 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61  e is 0, all data
20e15 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68  bases are search
20e16 65 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ed for the table
20e17 20 61 6e 64 20 74 68 65 0a 2a 2a 20 66 69 72 73   and the.** firs
20e18 74 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65  t matching table
20e19 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28   is returned.  (
20e1a 4e 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20  No checking for 
20e1b 64 75 70 6c 69 63 61 74 65 20 74 61 62 6c 65 0a  duplicate table.
20e1c 2a 2a 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65  ** names is done
20e1d 2e 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f  .)  The search o
20e1e 72 64 65 72 20 69 73 20 54 45 4d 50 20 66 69 72  rder is TEMP fir
20e1f 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74  st, then MAIN, t
20e20 68 65 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c  hen any.** auxil
20e21 69 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61  iary databases a
20e22 64 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 41  dded using the A
20e23 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  TTACH command..*
20e24 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71  *.** See also sq
20e25 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
20e26 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
20e27 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
20e28 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71  ite3FindTable(sq
20e29 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
20e2a 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f   char *zName, co
20e2b 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62  nst char *zDatab
20e2c 61 73 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ase){.  Table *p
20e2d 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   = 0;.  int i;. 
20e2e 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73   int nName;.  as
20e2f 73 65 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29  sert( zName!=0 )
20e30 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  ;.  nName = sqli
20e31 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e  te3Strlen(db, zN
20e32 61 6d 65 29 20 2b 20 31 3b 0a 20 20 66 6f 72 28  ame) + 1;.  for(
20e33 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69  i=OMIT_TEMPDB; i
20e34 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
20e35 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32      int j = (i<2
20e36 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f  ) ? i^1 : i;   /
20e37 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65  * Search TEMP be
20e38 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20  fore MAIN */.   
20e39 20 69 66 28 20 7a 44 61 74 61 62 61 73 65 21 3d   if( zDatabase!=
20e3a 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  0 && sqlite3StrI
20e3b 43 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64  Cmp(zDatabase, d
20e3c 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29  b->aDb[j].zName)
20e3d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
20e3e 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
20e3f 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d  Find(&db->aDb[j]
20e40 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  .pSchema->tblHas
20e41 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
20e42 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72  ;.    if( p ) br
20e43 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  eak;.  }.  retur
20e44 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  n p;.}../*.** Lo
20e45 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  cate the in-memo
20e46 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ry structure tha
20e47 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61  t describes a pa
20e48 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
20e49 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e  e.** table given
20e4a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61   the name of tha
20e4b 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74  t table and (opt
20e4c 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d  ionally) the nam
20e4d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  e of the.** data
20e4e 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
20e4f 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75  the table.  Retu
20e50 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66  rn NULL if not f
20e51 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61 76  ound.  Also leav
20e52 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d 65  e an.** error me
20e53 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d  ssage in pParse-
20e54 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20  >zErrMsg..**.** 
20e55 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  The difference b
20e56 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74  etween this rout
20e57 69 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33 46  ine and sqlite3F
20e58 69 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74 68  indTable() is th
20e59 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  at this.** routi
20e5a 6e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72 72  ne leaves an err
20e5b 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
20e5c 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77 68  arse->zErrMsg wh
20e5d 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69  ere.** sqlite3Fi
20e5e 6e 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20 6e  ndTable() does n
20e5f 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ot..*/.SQLITE_PR
20e60 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
20e61 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
20e62 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20e63 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6e  ,         /* con
20e64 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 74 6f  text in which to
20e65 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 20 2a   report errors *
20e66 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20  /.  int isView, 
20e67 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
20e68 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f  ue if looking fo
20e69 72 20 61 20 56 49 45 57 20 72 61 74 68 65 72 20  r a VIEW rather 
20e6a 74 68 61 6e 20 61 20 54 41 42 4c 45 20 2a 2f 0a  than a TABLE */.
20e6b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
20e6c 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame,     /* Name
20e6d 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65   of the table we
20e6e 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72   are looking for
20e6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
20e70 20 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a 20   *zDbase     /* 
20e71 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
20e72 62 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65 20  base.  Might be 
20e73 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61 62  NULL */.){.  Tab
20e74 6c 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65 61  le *p;..  /* Rea
20e75 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
20e76 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72  chema. If an err
20e77 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65  or occurs, leave
20e78 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
20e79 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20  e.  ** and code 
20e7a 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65  in pParse and re
20e7b 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  turn NULL. */.  
20e7c 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
20e7d 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
20e7e 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
20e7f 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
20e80 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   p = sqlite3Find
20e81 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62  Table(pParse->db
20e82 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65 29  , zName, zDbase)
20e83 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a  ;.  if( p==0 ){.
20e84 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
20e85 7a 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f 20  zMsg = isView ? 
20e86 22 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20 3a  "no such view" :
20e87 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 22   "no such table"
20e88 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73 65  ;.    if( zDbase
20e89 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
20e8a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
20e8b 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a  , "%s: %s.%s", z
20e8c 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e 61  Msg, zDbase, zNa
20e8d 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  me);.    }else{.
20e8e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
20e8f 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
20e90 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e  s: %s", zMsg, zN
20e91 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ame);.    }.    
20e92 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68  pParse->checkSch
20e93 65 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  ema = 1;.  }.  r
20e94 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
20e95 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d  * Locate the in-
20e96 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
20e97 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
20e98 0a 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72  .** a particular
20e99 20 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65   index given the
20e9a 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e   name of that in
20e9b 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  dex.** and the n
20e9c 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
20e9d 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ase that contain
20e9e 73 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20  s the index..** 
20e9f 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e  Return NULL if n
20ea0 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20  ot found..**.** 
20ea1 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20  If zDatabase is 
20ea2 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  0, all databases
20ea3 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f   are searched fo
20ea4 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61  r the.** table a
20ea5 6e 64 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  nd the first mat
20ea6 63 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72  ching index is r
20ea7 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68  eturned.  (No ch
20ea8 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75  ecking.** for du
20ea9 70 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61  plicate index na
20eaa 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54  mes is done.)  T
20eab 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20  he search order 
20eac 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74  is.** TEMP first
20ead 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65  , then MAIN, the
20eae 6e 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20  n any auxiliary 
20eaf 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 0a  databases added.
20eb0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54 54  ** using the ATT
20eb1 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a  ACH command..*/.
20eb2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
20eb3 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e  ndex *sqlite3Fin
20eb4 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a  dIndex(sqlite3 *
20eb5 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
20eb6 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  zName, const cha
20eb7 72 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78  r *zDb){.  Index
20eb8 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69   *p = 0;.  int i
20eb9 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  ;.  int nName = 
20eba 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62  sqlite3Strlen(db
20ebb 2c 20 7a 4e 61 6d 65 29 2b 31 3b 0a 20 20 66 6f  , zName)+1;.  fo
20ebc 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b  r(i=OMIT_TEMPDB;
20ebd 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
20ebe 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69  {.    int j = (i
20ebf 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20  <2) ? i^1 : i;  
20ec0 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62  /* Search TEMP b
20ec1 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20  efore MAIN */.  
20ec2 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
20ec3 61 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70  a = db->aDb[j].p
20ec4 53 63 68 65 6d 61 3b 0a 20 20 20 20 69 66 28 20  Schema;.    if( 
20ec5 7a 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74  zDb && sqlite3St
20ec6 72 49 43 6d 70 28 7a 44 62 2c 20 64 62 2d 3e 61  rICmp(zDb, db->a
20ec7 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63  Db[j].zName) ) c
20ec8 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 73  ontinue;.    ass
20ec9 65 72 74 28 20 70 53 63 68 65 6d 61 20 7c 7c 20  ert( pSchema || 
20eca 28 6a 3d 3d 31 20 26 26 20 21 64 62 2d 3e 61 44  (j==1 && !db->aD
20ecb 62 5b 31 5d 2e 70 42 74 29 20 29 3b 0a 20 20 20  b[1].pBt) );.   
20ecc 20 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a   if( pSchema ){.
20ecd 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65        p = sqlite
20ece 33 48 61 73 68 46 69 6e 64 28 26 70 53 63 68 65  3HashFind(&pSche
20ecf 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61  ma->idxHash, zNa
20ed0 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
20ed1 7d 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72  }.    if( p ) br
20ed2 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  eak;.  }.  retur
20ed3 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n p;.}../*.** Re
20ed4 63 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72 79  claim the memory
20ed5 20 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64 65   used by an inde
20ed6 78 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  x.*/.static void
20ed7 20 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65 78   freeIndex(Index
20ed8 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20   *p){.  sqlite3 
20ed9 2a 64 62 20 3d 20 70 2d 3e 70 54 61 62 6c 65 2d  *db = p->pTable-
20eda 3e 64 62 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65  >dbMem;.  sqlite
20edb 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
20edc 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69 74  ColAff);.  sqlit
20edd 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
20ede 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
20edf 20 74 68 65 20 67 69 76 65 6e 20 69 6e 64 65 78   the given index
20ee0 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20   from the index 
20ee1 68 61 73 68 20 74 61 62 6c 65 2c 20 61 6e 64 20  hash table, and 
20ee2 66 72 65 65 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f  free.** its memo
20ee3 72 79 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a  ry structures..*
20ee4 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 69  *.** The index i
20ee5 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  s removed from t
20ee6 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 68  he database hash
20ee7 20 74 61 62 6c 65 73 20 62 75 74 0a 2a 2a 20 69   tables but.** i
20ee8 74 20 69 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65  t is not unlinke
20ee9 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65  d from the Table
20eea 20 74 68 61 74 20 69 74 20 69 6e 64 65 78 65 73   that it indexes
20eeb 2e 0a 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20 66  ..** Unlinking f
20eec 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 6d 75  rom the Table mu
20eed 73 74 20 62 65 20 64 6f 6e 65 20 62 79 20 74 68  st be done by th
20eee 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
20eef 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
20ef0 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
20ef1 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b  Index(Index *p){
20ef2 0a 20 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b 0a  .  Index *pOld;.
20ef3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
20ef4 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a  ame = p->zName;.
20ef5 0a 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65  .  pOld = sqlite
20ef6 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e  3HashInsert(&p->
20ef7 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
20ef8 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  , zName,.       
20ef9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20efa 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65      sqlite3Strle
20efb 6e 33 30 28 7a 4e 61 6d 65 29 2b 31 2c 20 30 29  n30(zName)+1, 0)
20efc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 6c 64  ;.  assert( pOld
20efd 3d 3d 30 20 7c 7c 20 70 4f 6c 64 3d 3d 70 20 29  ==0 || pOld==p )
20efe 3b 0a 20 20 66 72 65 65 49 6e 64 65 78 28 70 29  ;.  freeIndex(p)
20eff 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74  ;.}../*.** For t
20f00 68 65 20 69 6e 64 65 78 20 63 61 6c 6c 65 64 20  he index called 
20f01 7a 49 64 78 4e 61 6d 65 20 77 68 69 63 68 20 69  zIdxName which i
20f02 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64  s found in the d
20f03 61 74 61 62 61 73 65 20 69 44 62 2c 0a 2a 2a 20  atabase iDb,.** 
20f04 75 6e 6c 69 6b 65 20 74 68 61 74 20 69 6e 64 65  unlike that inde
20f05 78 20 66 72 6f 6d 20 69 74 73 20 54 61 62 6c 65  x from its Table
20f06 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65   then remove the
20f07 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74   index from.** t
20f08 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61  he index hash ta
20f09 62 6c 65 20 61 6e 64 20 66 72 65 65 20 61 6c 6c  ble and free all
20f0a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72   memory structur
20f0b 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  es associated.**
20f0c 20 77 69 74 68 20 74 68 65 20 69 6e 64 65 78 2e   with the index.
20f0d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20f0e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
20f0f 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e  nlinkAndDeleteIn
20f10 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  dex(sqlite3 *db,
20f11 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20   int iDb, const 
20f12 63 68 61 72 20 2a 7a 49 64 78 4e 61 6d 65 29 7b  char *zIdxName){
20f13 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
20f14 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 48  ;.  int len;.  H
20f15 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64 62  ash *pHash = &db
20f16 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
20f17 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 0a 20 20  ma->idxHash;..  
20f18 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  len = sqlite3Str
20f19 6c 65 6e 28 64 62 2c 20 7a 49 64 78 4e 61 6d 65  len(db, zIdxName
20f1a 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71  );.  pIndex = sq
20f1b 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
20f1c 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 6d 65 2c  pHash, zIdxName,
20f1d 20 6c 65 6e 2b 31 2c 20 30 29 3b 0a 20 20 69 66   len+1, 0);.  if
20f1e 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ( pIndex ){.    
20f1f 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 54 61 62  if( pIndex->pTab
20f20 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e 64  le->pIndex==pInd
20f21 65 78 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64  ex ){.      pInd
20f22 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64  ex->pTable->pInd
20f23 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65  ex = pIndex->pNe
20f24 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  xt;.    }else{. 
20f25 20 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20       Index *p;. 
20f26 20 20 20 20 20 66 6f 72 28 70 3d 70 49 6e 64 65       for(p=pInde
20f27 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65  x->pTable->pInde
20f28 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74  x; p && p->pNext
20f29 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70 2d 3e 70  !=pIndex; p=p->p
20f2a 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20 20 69 66  Next){}.      if
20f2b 28 20 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d  ( p && p->pNext=
20f2c 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20  =pIndex ){.     
20f2d 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49     p->pNext = pI
20f2e 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ndex->pNext;.   
20f2f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66     }.    }.    f
20f30 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29  reeIndex(pIndex)
20f31 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67  ;.  }.  db->flag
20f32 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
20f33 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a  rnChanges;.}../*
20f34 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 63  .** Erase all sc
20f35 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hema information
20f36 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d   from the in-mem
20f37 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65 73 20  ory hash tables 
20f38 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64  of.** a single d
20f39 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72  atabase.  This r
20f3a 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
20f3b 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f   to reclaim memo
20f3c 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65  ry.** before the
20f3d 20 64 61 74 61 62 61 73 65 20 63 6c 6f 73 65 73   database closes
20f3e 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 20 63 61  .  It is also ca
20f3f 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 72 6f  lled during a ro
20f40 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 74 68 65  llback.** if the
20f41 72 65 20 77 65 72 65 20 73 63 68 65 6d 61 20 63  re were schema c
20f42 68 61 6e 67 65 73 20 64 75 72 69 6e 67 20 74 68  hanges during th
20f43 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72  e transaction or
20f44 20 69 66 20 61 0a 2a 2a 20 73 63 68 65 6d 61 2d   if a.** schema-
20f45 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74 63 68 20  cookie mismatch 
20f46 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  occurs..**.** If
20f47 20 69 44 62 3d 3d 30 20 74 68 65 6e 20 72 65 73   iDb==0 then res
20f48 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  et the internal 
20f49 73 63 68 65 6d 61 20 74 61 62 6c 65 73 20 66 6f  schema tables fo
20f4a 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a  r all database.*
20f4b 2a 20 66 69 6c 65 73 2e 20 20 49 66 20 69 44 62  * files.  If iDb
20f4c 3e 3d 31 20 74 68 65 6e 20 72 65 73 65 74 20 74  >=1 then reset t
20f4d 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
20f4e 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74 68 65 0a  ma for only the.
20f4f 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 69  ** single file i
20f50 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  ndicated..*/.SQL
20f51 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
20f52 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
20f53 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69  ernalSchema(sqli
20f54 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
20f55 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ){.  int i, j;. 
20f56 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
20f57 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
20f58 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20  ;..  if( iDb==0 
20f59 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
20f5a 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
20f5b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 44 62  .  }.  for(i=iDb
20f5c 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
20f5d 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d  ){.    Db *pDb =
20f5e 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20   &db->aDb[i];.  
20f5f 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65    if( pDb->pSche
20f60 6d 61 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ma ){.      asse
20f61 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70 44 62 2d  rt(i==1 || (pDb-
20f62 3e 70 42 74 20 26 26 20 73 71 6c 69 74 65 33 42  >pBt && sqlite3B
20f63 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
20f64 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20 20 20 20  Db->pBt)));.    
20f65 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46    sqlite3SchemaF
20f66 72 65 65 28 70 44 62 2d 3e 70 53 63 68 65 6d 61  ree(pDb->pSchema
20f67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
20f68 20 69 44 62 3e 30 20 29 20 72 65 74 75 72 6e 3b   iDb>0 ) return;
20f69 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
20f6a 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 66  Db==0 );.  db->f
20f6b 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f  lags &= ~SQLITE_
20f6c 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
20f6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
20f6e 76 65 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a  veAll(db);..  /*
20f6f 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   If one or more 
20f70 6f 66 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  of the auxiliary
20f71 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
20f72 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c  has been closed,
20f73 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76  .  ** then remov
20f74 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20  e them from the 
20f75 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
20f76 73 65 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b  se list.  We tak
20f77 65 20 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72  e the.  ** oppor
20f78 74 75 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69  tunity to do thi
20f79 73 20 68 65 72 65 20 73 69 6e 63 65 20 77 65 20  s here since we 
20f7a 68 61 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65  have just delete
20f7b 64 20 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a  d all of the.  *
20f7c 2a 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61  * schema hash ta
20f7d 62 6c 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f  bles and therefo
20f7e 72 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74  re do not have t
20f7f 6f 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67  o make any chang
20f80 65 73 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f  es.  ** to any o
20f81 66 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a  f those tables..
20f82 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
20f83 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
20f84 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a  .    struct Db *
20f85 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
20f86 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e  ];.    if( pDb->
20f87 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pBt==0 ){.      
20f88 69 66 28 20 70 44 62 2d 3e 70 41 75 78 20 26 26  if( pDb->pAux &&
20f89 20 70 44 62 2d 3e 78 46 72 65 65 41 75 78 20 29   pDb->xFreeAux )
20f8a 20 70 44 62 2d 3e 78 46 72 65 65 41 75 78 28 70   pDb->xFreeAux(p
20f8b 44 62 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 20  Db->pAux);.     
20f8c 20 70 44 62 2d 3e 70 41 75 78 20 3d 20 30 3b 0a   pDb->pAux = 0;.
20f8d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
20f8e 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e 6e 44 62  i=j=2; i<db->nDb
20f8f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
20f90 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62  ct Db *pDb = &db
20f91 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66  ->aDb[i];.    if
20f92 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b  ( pDb->pBt==0 ){
20f93 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
20f94 46 72 65 65 28 64 62 2c 20 70 44 62 2d 3e 7a 4e  Free(db, pDb->zN
20f95 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 44 62 2d  ame);.      pDb-
20f96 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20  >zName = 0;.    
20f97 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
20f98 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69 20 29 7b  }.    if( j<i ){
20f99 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 6a  .      db->aDb[j
20f9a 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a  ] = db->aDb[i];.
20f9b 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20      }.    j++;. 
20f9c 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 64 62 2d   }.  memset(&db-
20f9d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28 64 62 2d  >aDb[j], 0, (db-
20f9e 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f 66 28 64  >nDb-j)*sizeof(d
20f9f 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a 20 20 64  b->aDb[j]));.  d
20fa0 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 69 66  b->nDb = j;.  if
20fa1 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 26 20  ( db->nDb<=2 && 
20fa2 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 44 62  db->aDb!=db->aDb
20fa3 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 6d 65  Static ){.    me
20fa4 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 61 74  mcpy(db->aDbStat
20fa5 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 2a 73  ic, db->aDb, 2*s
20fa6 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d  izeof(db->aDb[0]
20fa7 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  ));.    sqlite3D
20fa8 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 44  bFree(db, db->aD
20fa9 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 20  b);.    db->aDb 
20faa 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b  = db->aDbStatic;
20fab 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
20fac 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
20fad 6c 6c 65 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d  lled when a comm
20fae 69 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51  it occurs..*/.SQ
20faf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20fb0 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49  d sqlite3CommitI
20fb1 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73  nternalChanges(s
20fb2 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64  qlite3 *db){.  d
20fb3 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
20fb4 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
20fb5 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  s;.}../*.** Clea
20fb6 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  r the column nam
20fb7 65 73 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  es from a table 
20fb8 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a 73 74 61 74  or view..*/.stat
20fb9 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 52 65  ic void sqliteRe
20fba 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 54  setColumnNames(T
20fbb 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20  able *pTable){. 
20fbc 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e   int i;.  Column
20fbd 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65   *pCol;.  sqlite
20fbe 33 20 2a 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e  3 *db = pTable->
20fbf 64 62 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  dbMem;.  assert(
20fc0 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20   pTable!=0 );.  
20fc1 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62  if( (pCol = pTab
20fc2 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a  le->aCol)!=0 ){.
20fc3 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
20fc4 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  Table->nCol; i++
20fc5 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
20fc6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
20fc7 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b  b, pCol->zName);
20fc8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
20fc9 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  prDelete(db, pCo
20fca 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20  l->pDflt);.     
20fcb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
20fcc 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b  b, pCol->zType);
20fcd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
20fce 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a  Free(db, pCol->z
20fcf 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Coll);.    }.   
20fd0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
20fd1 62 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29  b, pTable->aCol)
20fd2 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e  ;.  }.  pTable->
20fd3 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62  aCol = 0;.  pTab
20fd4 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a  le->nCol = 0;.}.
20fd5 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  ./*.** Remove th
20fd6 65 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74  e memory data st
20fd7 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61  ructures associa
20fd8 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76  ted with the giv
20fd9 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f  en.** Table.  No
20fda 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64   changes are mad
20fdb 65 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69  e to disk by thi
20fdc 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a  s routine..**.**
20fdd 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75   This routine ju
20fde 73 74 20 64 65 6c 65 74 65 73 20 74 68 65 20 64  st deletes the d
20fdf 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 20 20  ata structure.  
20fe0 49 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69  It does not unli
20fe1 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  nk.** the table 
20fe2 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66  data structure f
20fe3 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
20fe4 6c 65 2e 20 20 4e 6f 72 20 64 6f 65 73 20 69 74  le.  Nor does it
20fe5 20 72 65 6d 6f 76 65 0a 2a 2a 20 66 6f 72 65 69   remove.** forei
20fe6 67 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74 68 65  gn keys from the
20fe7 20 73 71 6c 69 74 65 2e 61 46 4b 65 79 20 68 61   sqlite.aFKey ha
20fe8 73 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69  sh table.  But i
20fe9 74 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a  t does destroy.*
20fea 2a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75  * memory structu
20feb 72 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63  res of the indic
20fec 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b  es and foreign k
20fed 65 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  eys associated w
20fee 69 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c  ith .** the tabl
20fef 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
20ff0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
20ff1 33 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62  3DeleteTable(Tab
20ff2 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49  le *pTable){.  I
20ff3 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70  ndex *pIndex, *p
20ff4 4e 65 78 74 3b 0a 20 20 46 4b 65 79 20 2a 70 46  Next;.  FKey *pF
20ff5 4b 65 79 2c 20 2a 70 4e 65 78 74 46 4b 65 79 3b  Key, *pNextFKey;
20ff6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
20ff7 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
20ff8 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20   ) return;.  db 
20ff9 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b  = pTable->dbMem;
20ffa 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 65  ..  /* Do not de
20ffb 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65 20 75  lete the table u
20ffc 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72 65 6e  ntil the referen
20ffd 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73  ce count reaches
20ffe 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54 61 62   zero. */.  pTab
20fff 6c 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66  le->nRef--;.  if
21000 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e 30  ( pTable->nRef>0
21001 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
21002 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54    }.  assert( pT
21003 61 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b  able->nRef==0 );
21004 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c  ..  /* Delete al
21005 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69  l indices associ
21006 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74  ated with this t
21007 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  able.  */.  for(
21008 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c 65 2d  pIndex = pTable-
21009 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b  >pIndex; pIndex;
2100a 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0a   pIndex=pNext){.
2100b 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49 6e 64      pNext = pInd
2100c 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61  ex->pNext;.    a
2100d 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 70  ssert( pIndex->p
2100e 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65 2d 3e  Schema==pTable->
2100f 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73  pSchema );.    s
21010 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65  qlite3DeleteInde
21011 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a  x(pIndex);.  }..
21012 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
21013 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a  MIT_FOREIGN_KEY.
21014 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20    /* Delete all 
21015 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 73  foreign keys ass
21016 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69  ociated with thi
21017 73 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6b 65  s table.  The ke
21018 79 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68  ys.  ** should h
21019 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e  ave already been
2101a 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74   unlinked from t
2101b 68 65 20 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65  he pSchema->aFKe
2101c 79 20 68 61 73 68 20 74 61 62 6c 65 20 0a 20 20  y hash table .  
2101d 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70  */.  for(pFKey=p
2101e 54 61 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 46  Table->pFKey; pF
2101f 4b 65 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74  Key; pFKey=pNext
21020 46 4b 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 74  FKey){.    pNext
21021 46 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 4e  FKey = pFKey->pN
21022 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 61 73 73  extFrom;.    ass
21023 65 72 74 28 20 73 71 6c 69 74 65 33 48 61 73 68  ert( sqlite3Hash
21024 46 69 6e 64 28 26 70 54 61 62 6c 65 2d 3e 70 53  Find(&pTable->pS
21025 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 0a 20 20  chema->aFKey,.  
21026 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21027 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e           pFKey->
21028 7a 54 6f 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  zTo, sqlite3Strl
21029 65 6e 33 30 28 70 46 4b 65 79 2d 3e 7a 54 6f 29  en30(pFKey->zTo)
2102a 2b 31 29 21 3d 70 46 4b 65 79 20 29 3b 0a 20 20  +1)!=pFKey );.  
2102b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2102c 64 62 2c 20 70 46 4b 65 79 29 3b 0a 20 20 7d 0a  db, pFKey);.  }.
2102d 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 65 6c  #endif..  /* Del
2102e 65 74 65 20 74 68 65 20 54 61 62 6c 65 20 73 74  ete the Table st
2102f 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a  ructure itself..
21030 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65 73    */.  sqliteRes
21031 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 54  etColumnNames(pT
21032 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  able);.  sqlite3
21033 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c  DbFree(db, pTabl
21034 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  e->zName);.  sql
21035 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
21036 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 3b  Table->zColAff);
21037 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
21038 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c  Delete(db, pTabl
21039 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 23 69 66  e->pSelect);.#if
2103a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2103b 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69 74 65 33  _CHECK.  sqlite3
2103c 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2103d 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b 0a  Table->pCheck);.
2103e 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
2103f 56 74 61 62 43 6c 65 61 72 28 70 54 61 62 6c 65  VtabClear(pTable
21040 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
21041 65 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a  ee(db, pTable);.
21042 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  }../*.** Unlink 
21043 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20  the given table 
21044 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
21045 62 6c 65 73 20 61 6e 64 20 74 68 65 20 64 65 6c  bles and the del
21046 65 74 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65  ete the.** table
21047 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 20   structure with 
21048 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 20  all its indices 
21049 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73  and foreign keys
2104a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2104b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2104c 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
2104d 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62  able(sqlite3 *db
2104e 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74  , int iDb, const
2104f 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 29   char *zTabName)
21050 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20  {.  Table *p;.  
21051 46 4b 65 79 20 2a 70 46 31 2c 20 2a 70 46 32 3b  FKey *pF1, *pF2;
21052 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a 20 20 61  .  Db *pDb;..  a
21053 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a  ssert( db!=0 );.
21054 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
21055 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
21056 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 54 61  );.  assert( zTa
21057 62 4e 61 6d 65 20 26 26 20 7a 54 61 62 4e 61 6d  bName && zTabNam
21058 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62 20 3d 20  e[0] );.  pDb = 
21059 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
2105a 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
2105b 49 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 63  Insert(&pDb->pSc
2105c 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a  hema->tblHash, z
2105d 54 61 62 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  TabName,.       
2105e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2105f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
21060 28 7a 54 61 62 4e 61 6d 65 29 2b 31 2c 30 29 3b  (zTabName)+1,0);
21061 0a 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 6e  .  if( p ){.#ifn
21062 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
21063 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20  FOREIGN_KEY.    
21064 66 6f 72 28 70 46 31 3d 70 2d 3e 70 46 4b 65 79  for(pF1=p->pFKey
21065 3b 20 70 46 31 3b 20 70 46 31 3d 70 46 31 2d 3e  ; pF1; pF1=pF1->
21066 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20  pNextFrom){.    
21067 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 71 6c 69    int nTo = sqli
21068 74 65 33 53 74 72 6c 65 6e 33 30 28 70 46 31 2d  te3Strlen30(pF1-
21069 3e 7a 54 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20  >zTo) + 1;.     
2106a 20 70 46 32 20 3d 20 73 71 6c 69 74 65 33 48 61   pF2 = sqlite3Ha
2106b 73 68 46 69 6e 64 28 26 70 44 62 2d 3e 70 53 63  shFind(&pDb->pSc
2106c 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 31  hema->aFKey, pF1
2106d 2d 3e 7a 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20  ->zTo, nTo);.   
2106e 20 20 20 69 66 28 20 70 46 32 3d 3d 70 46 31 20     if( pF2==pF1 
2106f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
21070 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 44  e3HashInsert(&pD
21071 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65  b->pSchema->aFKe
21072 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20 6e 54 6f  y, pF1->zTo, nTo
21073 2c 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f 29 3b  , pF1->pNextTo);
21074 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
21075 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46 32        while( pF2
21076 20 26 26 20 70 46 32 2d 3e 70 4e 65 78 74 54 6f   && pF2->pNextTo
21077 21 3d 70 46 31 20 29 7b 20 70 46 32 3d 70 46 32  !=pF1 ){ pF2=pF2
21078 2d 3e 70 4e 65 78 74 54 6f 3b 20 7d 0a 20 20 20  ->pNextTo; }.   
21079 20 20 20 20 20 69 66 28 20 70 46 32 20 29 7b 0a       if( pF2 ){.
2107a 20 20 20 20 20 20 20 20 20 20 70 46 32 2d 3e 70            pF2->p
2107b 4e 65 78 74 54 6f 20 3d 20 70 46 31 2d 3e 70 4e  NextTo = pF1->pN
2107c 65 78 74 54 6f 3b 0a 20 20 20 20 20 20 20 20 7d  extTo;.        }
2107d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
2107e 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65  endif.    sqlite
2107f 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 29 3b  3DeleteTable(p);
21080 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 73  .  }.  db->flags
21081 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
21082 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a  nChanges;.}../*.
21083 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e  ** Given a token
21084 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72 69 6e  , return a strin
21085 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20  g that consists 
21086 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66 20 74  of the text of t
21087 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 20 77 69 74  hat.** token wit
21088 68 20 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e 73  h any quotations
21089 20 72 65 6d 6f 76 65 64 2e 20 20 53 70 61 63 65   removed.  Space
2108a 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
2108b 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20  urned string.** 
2108c 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
2108d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
2108e 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65  and must be free
2108f 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  d by the calling
21090 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  .** function..**
21091 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f  .** Tokens are o
21092 66 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65  ften just pointe
21093 72 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67  rs into the orig
21094 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61 6e  inal SQL text an
21095 64 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 20  d so.** are not 
21096 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20  \000 terminated 
21097 61 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72 73  and are not pers
21098 69 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65 74  istent.  The ret
21099 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20  urned string.** 
2109a 69 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74  is \000 terminat
2109b 65 64 20 61 6e 64 20 69 73 20 70 65 72 73 69 73  ed and is persis
2109c 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tent..*/.SQLITE_
2109d 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
2109e 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
2109f 65 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  en(sqlite3 *db, 
210a0 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20  Token *pName){. 
210a1 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
210a2 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20  if( pName ){.   
210a3 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
210a4 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
210a5 68 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70  har*)pName->z, p
210a6 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71  Name->n);.    sq
210a7 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61  lite3Dequote(zNa
210a8 6d 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  me);.  }else{.  
210a9 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d    zName = 0;.  }
210aa 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b  .  return zName;
210ab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74  .}../*.** Open t
210ac 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
210ad 20 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69 6e   table stored in
210ae 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72   database number
210af 20 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74   iDb for.** writ
210b0 69 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20 69  ing. The table i
210b1 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 63  s opened using c
210b2 75 72 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49  ursor 0..*/.SQLI
210b3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
210b4 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65  sqlite3OpenMaste
210b5 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 2c  rTable(Parse *p,
210b6 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62   int iDb){.  Vdb
210b7 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
210b8 74 56 64 62 65 28 70 29 3b 0a 20 20 73 71 6c 69  tVdbe(p);.  sqli
210b9 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  te3TableLock(p, 
210ba 69 44 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  iDb, MASTER_ROOT
210bb 2c 20 31 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  , 1, SCHEMA_TABL
210bc 45 28 69 44 62 29 29 3b 0a 20 20 73 71 6c 69 74  E(iDb));.  sqlit
210bd 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
210be 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c  OP_OpenWrite, 0,
210bf 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44   MASTER_ROOT, iD
210c0 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
210c1 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
210c2 20 28 63 68 61 72 20 2a 29 35 2c 20 50 34 5f 49   (char *)5, P4_I
210c3 4e 54 33 32 29 3b 20 20 2f 2a 20 35 20 63 6f 6c  NT32);  /* 5 col
210c4 75 6d 6e 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  umn table */.  i
210c5 66 28 20 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b  f( p->nTab==0 ){
210c6 0a 20 20 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31  .    p->nTab = 1
210c7 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ;.  }.}../*.** P
210c8 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70  arameter zName p
210c9 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74  oints to a nul-t
210ca 65 72 6d 69 6e 61 74 65 64 20 62 75 66 66 65 72  erminated buffer
210cb 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
210cc 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74  name.** of a dat
210cd 61 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22  abase ("main", "
210ce 74 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d  temp" or the nam
210cf 65 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64  e of an attached
210d0 20 64 62 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75   db). This.** fu
210d1 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
210d2 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
210d3 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 69  named database i
210d4 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a  n db->aDb[], or.
210d5 2a 2a 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d  ** -1 if the nam
210d6 65 64 20 64 62 20 63 61 6e 6e 6f 74 20 62 65 20  ed db cannot be 
210d7 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  found..*/.SQLITE
210d8 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
210d9 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73  ite3FindDbName(s
210da 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
210db 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a  t char *zName){.
210dc 20 20 69 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20    int i = -1;   
210dd 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
210de 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 66  e number */.  if
210df 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 44  ( zName ){.    D
210e0 62 20 2a 70 44 62 3b 0a 20 20 20 20 69 6e 74 20  b *pDb;.    int 
210e1 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
210e2 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  n30(zName);.    
210e3 66 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31  for(i=(db->nDb-1
210e4 29 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b  ), pDb=&db->aDb[
210e5 69 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70  i]; i>=0; i--, p
210e6 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 28  Db--){.      if(
210e7 20 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c   (!OMIT_TEMPDB |
210e8 7c 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73  | i!=1 ) && n==s
210e9 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
210ea 44 62 2d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20  Db->zName) && . 
210eb 20 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c 69           0==sqli
210ec 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e  te3StrICmp(pDb->
210ed 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b  zName, zName) ){
210ee 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
210ef 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
210f0 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a  }.  return i;.}.
210f1 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e  ./*.** The token
210f2 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73   *pName contains
210f3 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64   the name of a d
210f4 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20  atabase (either 
210f5 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65  "main" or.** "te
210f6 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20  mp" or the name 
210f7 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  of an attached d
210f8 62 29 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65  b). This routine
210f9 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
210fa 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d  index of the nam
210fb 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64  ed database in d
210fc 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20  b->aDb[], or -1 
210fd 69 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20  if the named db 
210fe 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  .** does not exi
210ff 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  st..*/.SQLITE_PR
21100 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21101 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 20  3FindDb(sqlite3 
21102 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  *db, Token *pNam
21103 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  e){.  int i;    
21104 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21105 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
21106 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f  tabase number */
21107 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
21108 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21109 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2110a 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67  we are searching
2110b 20 66 6f 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20   for */.  zName 
2110c 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
2110d 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
2110e 29 3b 0a 20 20 69 20 3d 20 73 71 6c 69 74 65 33  );.  i = sqlite3
2110f 46 69 6e 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a  FindDbName(db, z
21110 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
21111 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65  DbFree(db, zName
21112 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d  );.  return i;.}
21113 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f  ../* The table o
21114 72 20 76 69 65 77 20 6f 72 20 74 72 69 67 67 65  r view or trigge
21115 72 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65 64  r name is passed
21116 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
21117 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70   via tokens.** p
21118 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32  Name1 and pName2
21119 2e 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e  . If the table n
2111a 61 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75  ame was fully qu
2111b 61 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61  alified, for exa
2111c 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41  mple:.**.** CREA
2111d 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79 79  TE TABLE xxx.yyy
2111e 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54   (...);.** .** T
2111f 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65  hen pName1 is se
21120 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20 70  t to "xxx" and p
21121 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e 20  Name2 "yyy". On 
21122 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20 69  the other hand i
21123 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e  f.** the table n
21124 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79  ame is not fully
21125 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e   qualified, i.e.
21126 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54  :.**.** CREATE T
21127 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a  ABLE yyy(...);.*
21128 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31  *.** Then pName1
21129 20 69 73 20 73 65 74 20 74 6f 20 22 79 79 79 22   is set to "yyy"
2112a 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22   and pName2 is "
2112b 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  "..**.** This ro
2112c 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 2a  utine sets the *
2112d 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72  ppUnqual pointer
2112e 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
2112f 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f   token (pName1 o
21130 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61  r.** pName2) tha
21131 74 20 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71  t stores the unq
21132 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e  ualified table n
21133 61 6d 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20  ame.  The index 
21134 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  of the.** databa
21135 73 65 20 22 78 78 78 22 20 69 73 20 72 65 74 75  se "xxx" is retu
21136 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
21137 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21138 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a  te3TwoPartName(.
21139 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2113a 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2113b 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61   and code genera
2113c 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ting context */.
2113d 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c    Token *pName1,
2113e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 78 78        /* The "xx
2113f 78 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 22  x" in the name "
21140 78 78 78 2e 79 79 79 22 20 6f 72 20 22 78 78 78  xxx.yyy" or "xxx
21141 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  " */.  Token *pN
21142 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20 54 68  ame2,      /* Th
21143 65 20 22 79 79 79 22 20 69 6e 20 74 68 65 20 6e  e "yyy" in the n
21144 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 2a 2f  ame "xxx.yyy" */
21145 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75  .  Token **pUnqu
21146 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  al     /* Write 
21147 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  the unqualified 
21148 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68 65 72 65  object name here
21149 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62   */.){.  int iDb
2114a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2114b 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2114c 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a   holding the obj
2114d 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ect */.  sqlite3
2114e 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2114f 62 3b 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 32  b;..  if( pName2
21150 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20   && pName2->n>0 
21151 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 69  ){.    if( db->i
21152 6e 69 74 2e 62 75 73 79 20 29 20 7b 0a 20 20 20  nit.busy ) {.   
21153 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
21154 73 67 28 70 50 61 72 73 65 2c 20 22 63 6f 72 72  sg(pParse, "corr
21155 75 70 74 20 64 61 74 61 62 61 73 65 22 29 3b 0a  upt database");.
21156 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45        pParse->nE
21157 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75  rr++;.      retu
21158 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20  rn -1;.    }.   
21159 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d   *pUnqual = pNam
2115a 65 32 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71  e2;.    iDb = sq
2115b 6c 69 74 65 33 46 69 6e 64 44 62 28 64 62 2c 20  lite3FindDb(db, 
2115c 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28  pName1);.    if(
2115d 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 20 20   iDb<0 ){.      
2115e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2115f 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e  pParse, "unknown
21160 20 64 61 74 61 62 61 73 65 20 25 54 22 2c 20 70   database %T", p
21161 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 70 50  Name1);.      pP
21162 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
21163 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20      return -1;. 
21164 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
21165 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e    assert( db->in
21166 69 74 2e 69 44 62 3d 3d 30 20 7c 7c 20 64 62 2d  it.iDb==0 || db-
21167 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a 20 20  >init.busy );.  
21168 20 20 69 44 62 20 3d 20 64 62 2d 3e 69 6e 69 74    iDb = db->init
21169 2e 69 44 62 3b 0a 20 20 20 20 2a 70 55 6e 71 75  .iDb;.    *pUnqu
2116a 61 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d  al = pName1;.  }
2116b 0a 20 20 72 65 74 75 72 6e 20 69 44 62 3b 0a 7d  .  return iDb;.}
2116c 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2116d 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20  tine is used to 
2116e 63 68 65 63 6b 20 69 66 20 74 68 65 20 55 54 46  check if the UTF
2116f 2d 38 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 20  -8 string zName 
21170 69 73 20 61 20 6c 65 67 61 6c 0a 2a 2a 20 75 6e  is a legal.** un
21171 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 66  qualified name f
21172 6f 72 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20  or a new schema 
21173 6f 62 6a 65 63 74 20 28 74 61 62 6c 65 2c 20 69  object (table, i
21174 6e 64 65 78 2c 20 76 69 65 77 20 6f 72 0a 2a 2a  ndex, view or.**
21175 20 74 72 69 67 67 65 72 29 2e 20 41 6c 6c 20 6e   trigger). All n
21176 61 6d 65 73 20 61 72 65 20 6c 65 67 61 6c 20 65  ames are legal e
21177 78 63 65 70 74 20 74 68 6f 73 65 20 74 68 61 74  xcept those that
21178 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20   begin with the 
21179 73 74 72 69 6e 67 0a 2a 2a 20 22 73 71 6c 69 74  string.** "sqlit
2117a 65 5f 22 20 28 69 6e 20 75 70 70 65 72 2c 20 6c  e_" (in upper, l
2117b 6f 77 65 72 20 6f 72 20 6d 69 78 65 64 20 63 61  ower or mixed ca
2117c 73 65 29 2e 20 54 68 69 73 20 70 6f 72 74 69 6f  se). This portio
2117d 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65 73 70 61  n of the namespa
2117e 63 65 0a 2a 2a 20 69 73 20 72 65 73 65 72 76 65  ce.** is reserve
2117f 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75  d for internal u
21180 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
21181 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21182 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
21183 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21184 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
21185 65 29 7b 0a 20 20 69 66 28 20 21 70 50 61 72 73  e){.  if( !pPars
21186 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  e->db->init.busy
21187 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74   && pParse->nest
21188 65 64 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20  ed==0 .         
21189 20 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d   && (pParse->db-
2118a 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2118b 57 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a  WriteSchema)==0.
2118c 20 20 20 20 20 20 20 20 20 20 26 26 20 30 3d 3d            && 0==
2118d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2118e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22  zName, "sqlite_"
2118f 2c 20 37 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  , 7) ){.    sqli
21190 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
21191 73 65 2c 20 22 6f 62 6a 65 63 74 20 6e 61 6d 65  se, "object name
21192 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e   reserved for in
21193 74 65 72 6e 61 6c 20 75 73 65 3a 20 25 73 22 2c  ternal use: %s",
21194 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74   zName);.    ret
21195 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
21196 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
21197 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
21198 2a 2a 20 42 65 67 69 6e 20 63 6f 6e 73 74 72 75  ** Begin constru
21199 63 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c  cting a new tabl
2119a 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  e representation
2119b 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69   in memory.  Thi
2119c 73 20 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73  s is.** the firs
2119d 74 20 6f 66 20 73 65 76 65 72 61 6c 20 61 63 74  t of several act
2119e 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ion routines tha
2119f 74 20 67 65 74 20 63 61 6c 6c 65 64 20 69 6e 20  t get called in 
211a0 72 65 73 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20 61  response.** to a
211a1 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
211a2 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 70 61 72  atement.  In par
211a3 74 69 63 75 6c 61 72 2c 20 74 68 69 73 20 72 6f  ticular, this ro
211a4 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 0a  utine is called.
211a5 2a 2a 20 61 66 74 65 72 20 73 65 65 69 6e 67 20  ** after seeing 
211a6 74 6f 6b 65 6e 73 20 22 43 52 45 41 54 45 22 20  tokens "CREATE" 
211a7 61 6e 64 20 22 54 41 42 4c 45 22 20 61 6e 64 20  and "TABLE" and 
211a8 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  the table name. 
211a9 54 68 65 20 69 73 54 65 6d 70 0a 2a 2a 20 66 6c  The isTemp.** fl
211aa 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74 68  ag is true if th
211ab 65 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 62  e table should b
211ac 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
211ad 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
211ae 73 65 0a 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65  se.** file inste
211af 61 64 20 6f 66 20 69 6e 20 74 68 65 20 6d 61 69  ad of in the mai
211b0 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
211b1 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c    This is normal
211b2 6c 79 20 74 68 65 20 63 61 73 65 0a 2a 2a 20 77  ly the case.** w
211b3 68 65 6e 20 74 68 65 20 22 54 45 4d 50 22 20 6f  hen the "TEMP" o
211b4 72 20 22 54 45 4d 50 4f 52 41 52 59 22 20 6b 65  r "TEMPORARY" ke
211b5 79 77 6f 72 64 20 6f 63 63 75 72 73 20 69 6e 20  yword occurs in 
211b6 62 65 74 77 65 65 6e 0a 2a 2a 20 43 52 45 41 54  between.** CREAT
211b7 45 20 61 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a 0a  E and TABLE..**.
211b8 2a 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65  ** The new table
211b9 20 72 65 63 6f 72 64 20 69 73 20 69 6e 69 74 69   record is initi
211ba 61 6c 69 7a 65 64 20 61 6e 64 20 70 75 74 20 69  alized and put i
211bb 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  n pParse->pNewTa
211bc 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65 20  ble..** As more 
211bd 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41  of the CREATE TA
211be 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73  BLE statement is
211bf 20 70 61 72 73 65 64 2c 20 61 64 64 69 74 69 6f   parsed, additio
211c0 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a 20 72 6f  nal action.** ro
211c1 75 74 69 6e 65 73 20 77 69 6c 6c 20 62 65 20 63  utines will be c
211c2 61 6c 6c 65 64 20 74 6f 20 61 64 64 20 6d 6f 72  alled to add mor
211c3 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  e information to
211c4 20 74 68 69 73 20 72 65 63 6f 72 64 2e 0a 2a 2a   this record..**
211c5 20 41 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   At the end of t
211c6 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
211c7 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 73  statement, the s
211c8 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 29  qlite3EndTable()
211c9 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63   routine.** is c
211ca 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74  alled to complet
211cb 65 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74 69  e the constructi
211cc 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61  on of the new ta
211cd 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53  ble record..*/.S
211ce 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
211cf 69 64 20 73 71 6c 69 74 65 33 53 74 61 72 74 54  id sqlite3StartT
211d0 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
211d1 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73  Parse,   /* Pars
211d2 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
211d3 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20  Token *pName1,  
211d4 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f   /* First part o
211d5 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  f the name of th
211d6 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20  e table or view 
211d7 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
211d8 65 32 2c 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20  e2,   /* Second 
211d9 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65  part of the name
211da 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   of the table or
211db 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69   view */.  int i
211dc 73 54 65 6d 70 2c 20 20 20 20 20 20 2f 2a 20 54  sTemp,      /* T
211dd 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61  rue if this is a
211de 20 54 45 4d 50 20 74 61 62 6c 65 20 2a 2f 0a 20   TEMP table */. 
211df 20 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20   int isView,    
211e0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
211e1 73 20 69 73 20 61 20 56 49 45 57 20 2a 2f 0a 20  s is a VIEW */. 
211e2 20 69 6e 74 20 69 73 56 69 72 74 75 61 6c 2c 20   int isVirtual, 
211e3 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
211e4 73 20 69 73 20 61 20 56 49 52 54 55 41 4c 20 74  s is a VIRTUAL t
211e5 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  able */.  int no
211e6 45 72 72 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  Err        /* Do
211e7 20 6e 6f 74 68 69 6e 67 20 69 66 20 74 61 62 6c   nothing if tabl
211e8 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  e already exists
211e9 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
211ea 70 54 61 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a  pTable;.  char *
211eb 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f 2a 20 54 68  zName = 0; /* Th
211ec 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65  e name of the ne
211ed 77 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c  w table */.  sql
211ee 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
211ef 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76  e->db;.  Vdbe *v
211f0 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  ;.  int iDb;    
211f1 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
211f2 20 6e 75 6d 62 65 72 20 74 6f 20 63 72 65 61 74   number to creat
211f3 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 2a  e the table in *
211f4 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
211f5 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66  ;    /* Unqualif
211f6 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  ied name of the 
211f7 74 61 62 6c 65 20 74 6f 20 63 72 65 61 74 65 20  table to create 
211f8 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 74 61 62  */..  /* The tab
211f9 6c 65 20 6f 72 20 76 69 65 77 20 6e 61 6d 65 20  le or view name 
211fa 74 6f 20 63 72 65 61 74 65 20 69 73 20 70 61 73  to create is pas
211fb 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74  sed to this rout
211fc 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 20  ine via tokens. 
211fd 20 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 70   ** pName1 and p
211fe 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74 61  Name2. If the ta
211ff 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75 6c  ble name was ful
21200 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66 6f  ly qualified, fo
21201 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a  r example:.  **.
21202 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c    ** CREATE TABL
21203 45 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b  E xxx.yyy (...);
21204 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 54 68 65 6e  .  ** .  ** Then
21205 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74   pName1 is set t
21206 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d  o "xxx" and pNam
21207 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65  e2 "yyy". On the
21208 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 20   other hand if. 
21209 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61   ** the table na
2120a 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20  me is not fully 
2120b 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a  qualified, i.e.:
2120c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54  .  **.  ** CREAT
2120d 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29  E TABLE yyy(...)
2120e 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e  ;.  **.  ** Then
2120f 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74   pName1 is set t
21210 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d  o "yyy" and pNam
21211 65 32 20 69 73 20 22 22 2e 0a 20 20 2a 2a 0a 20  e2 is ""..  **. 
21212 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 62 65 6c   ** The call bel
21213 6f 77 20 73 65 74 73 20 74 68 65 20 70 4e 61 6d  ow sets the pNam
21214 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69  e pointer to poi
21215 6e 74 20 61 74 20 74 68 65 20 74 6f 6b 65 6e 20  nt at the token 
21216 28 70 4e 61 6d 65 31 20 6f 72 0a 20 20 2a 2a 20  (pName1 or.  ** 
21217 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73 74 6f  pName2) that sto
21218 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c 69 66  res the unqualif
21219 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  ied table name. 
2121a 54 68 65 20 76 61 72 69 61 62 6c 65 20 69 44 62  The variable iDb
2121b 20 69 73 0a 20 20 2a 2a 20 73 65 74 20 74 6f 20   is.  ** set to 
2121c 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
2121d 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 74   database that t
2121e 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  he table or view
2121f 20 69 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63   is to be.  ** c
21220 72 65 61 74 65 64 20 69 6e 2e 0a 20 20 2a 2f 0a  reated in..  */.
21221 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54    iDb = sqlite3T
21222 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73  woPartName(pPars
21223 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65  e, pName1, pName
21224 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 66  2, &pName);.  if
21225 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e  ( iDb<0 ) return
21226 3b 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45  ;.  if( !OMIT_TE
21227 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20 26  MPDB && isTemp &
21228 26 20 69 44 62 3e 31 20 29 7b 0a 20 20 20 20 2f  & iDb>1 ){.    /
21229 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20  * If creating a 
2122a 74 65 6d 70 20 74 61 62 6c 65 2c 20 74 68 65 20  temp table, the 
2122b 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65 20  name may not be 
2122c 71 75 61 6c 69 66 69 65 64 20 2a 2f 0a 20 20 20  qualified */.   
2122d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2122e 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f 72  (pParse, "tempor
2122f 61 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d  ary table name m
21230 75 73 74 20 62 65 20 75 6e 71 75 61 6c 69 66 69  ust be unqualifi
21231 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ed");.    return
21232 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 4f 4d 49  ;.  }.  if( !OMI
21233 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65  T_TEMPDB && isTe
21234 6d 70 20 29 20 69 44 62 20 3d 20 31 3b 0a 0a 20  mp ) iDb = 1;.. 
21235 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f   pParse->sNameTo
21236 6b 65 6e 20 3d 20 2a 70 4e 61 6d 65 3b 0a 20 20  ken = *pName;.  
21237 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
21238 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
21239 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a   pName);.  if( z
2123a 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e  Name==0 ) return
2123b 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
2123c 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f  K!=sqlite3CheckO
2123d 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65  bjectName(pParse
2123e 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  , zName) ){.    
2123f 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
21240 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66  _error;.  }.  if
21241 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d  ( db->init.iDb==
21242 31 20 29 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a  1 ) isTemp = 1;.
21243 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
21244 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
21245 4e 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 54  N.  assert( (isT
21246 65 6d 70 20 26 20 31 29 3d 3d 69 73 54 65 6d 70  emp & 1)==isTemp
21247 20 29 3b 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20   );.  {.    int 
21248 63 6f 64 65 3b 0a 20 20 20 20 63 68 61 72 20 2a  code;.    char *
21249 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
2124a 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66  b].zName;.    if
2124b 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
2124c 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
2124d 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41  E_INSERT, SCHEMA
2124e 5f 54 41 42 4c 45 28 69 73 54 65 6d 70 29 2c 20  _TABLE(isTemp), 
2124f 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  0, zDb) ){.     
21250 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
21251 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  e_error;.    }. 
21252 20 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b     if( isView ){
21253 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54  .      if( !OMIT
21254 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d  _TEMPDB && isTem
21255 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  p ){.        cod
21256 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  e = SQLITE_CREAT
21257 45 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20  E_TEMP_VIEW;.   
21258 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21259 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
2125a 43 52 45 41 54 45 5f 56 49 45 57 3b 0a 20 20 20  CREATE_VIEW;.   
2125b 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
2125c 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f        if( !OMIT_
2125d 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70  TEMPDB && isTemp
2125e 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65   ){.        code
2125f 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
21260 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20  _TEMP_TABLE;.   
21261 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21262 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
21263 43 52 45 41 54 45 5f 54 41 42 4c 45 3b 0a 20 20  CREATE_TABLE;.  
21264 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
21265 69 66 28 20 21 69 73 56 69 72 74 75 61 6c 20 26  if( !isVirtual &
21266 26 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  & sqlite3AuthChe
21267 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c  ck(pParse, code,
21268 20 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20   zName, 0, zDb) 
21269 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65  ){.      goto be
2126a 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
2126b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
2126c 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  f..  /* Make sur
2126d 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  e the new table 
2126e 6e 61 6d 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f  name does not co
2126f 6c 6c 69 64 65 20 77 69 74 68 20 61 6e 20 65 78  llide with an ex
21270 69 73 74 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65  isting.  ** inde
21271 78 20 6f 72 20 74 61 62 6c 65 20 6e 61 6d 65 20  x or table name 
21272 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  in the same data
21273 62 61 73 65 2e 20 20 49 73 73 75 65 20 61 6e 20  base.  Issue an 
21274 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66  error message if
21275 0a 20 20 2a 2a 20 69 74 20 64 6f 65 73 2e 20 54  .  ** it does. T
21276 68 65 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20  he exception is 
21277 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  if the statement
21278 20 62 65 69 6e 67 20 70 61 72 73 65 64 20 77 61   being parsed wa
21279 73 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 74 6f  s passed.  ** to
2127a 20 61 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c   an sqlite3_decl
2127b 61 72 65 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e  are_vtab() call.
2127c 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 6f 6e   In that case on
2127d 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  ly the column na
2127e 6d 65 73 0a 20 20 2a 2a 20 61 6e 64 20 74 79 70  mes.  ** and typ
2127f 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2c  es will be used,
21280 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20   so there is no 
21281 6e 65 65 64 20 74 6f 20 74 65 73 74 20 66 6f 72  need to test for
21282 20 6e 61 6d 65 73 70 61 63 65 0a 20 20 2a 2a 20   namespace.  ** 
21283 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 20 20 2a 2f  collisions..  */
21284 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c 41  .  if( !IN_DECLA
21285 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 69  RE_VTAB ){.    i
21286 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
21287 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
21288 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20  pParse) ){.     
21289 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
2128a 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  e_error;.    }. 
2128b 20 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69     pTable = sqli
2128c 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
2128d 20 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b   zName, db->aDb[
2128e 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  iDb].zName);.   
2128f 20 69 66 28 20 70 54 61 62 6c 65 20 29 7b 0a 20   if( pTable ){. 
21290 20 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20       if( !noErr 
21291 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
21292 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
21293 65 2c 20 22 74 61 62 6c 65 20 25 54 20 61 6c 72  e, "table %T alr
21294 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 70 4e  eady exists", pN
21295 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ame);.      }.  
21296 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74      goto begin_t
21297 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20  able_error;.    
21298 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
21299 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a  3FindIndex(db, z
2129a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 26 26 20 28  Name, 0)!=0 && (
2129b 69 44 62 3d 3d 30 20 7c 7c 20 21 64 62 2d 3e 69  iDb==0 || !db->i
2129c 6e 69 74 2e 62 75 73 79 29 20 29 7b 0a 20 20 20  nit.busy) ){.   
2129d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2129e 73 67 28 70 50 61 72 73 65 2c 20 22 74 68 65 72  sg(pParse, "ther
2129f 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e 20  e is already an 
212a0 69 6e 64 65 78 20 6e 61 6d 65 64 20 25 73 22 2c  index named %s",
212a1 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67   zName);.      g
212a2 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f  oto begin_table_
212a3 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  error;.    }.  }
212a4 0a 0a 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c  ..  pTable = sql
212a5 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
212a6 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c  (db, sizeof(Tabl
212a7 65 29 29 3b 0a 20 20 69 66 28 20 70 54 61 62 6c  e));.  if( pTabl
212a8 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  e==0 ){.    db->
212a9 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
212aa 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63  ;.    pParse->rc
212ab 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
212ac 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  .    pParse->nEr
212ad 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 62 65  r++;.    goto be
212ae 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
212af 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 7a  .  }.  pTable->z
212b0 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20  Name = zName;.  
212b1 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20 3d 20  pTable->iPKey = 
212b2 2d 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 70 53  -1;.  pTable->pS
212b3 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
212b4 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20  iDb].pSchema;.  
212b5 70 54 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31  pTable->nRef = 1
212b6 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65  ;.  pTable->dbMe
212b7 6d 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  m = db->lookasid
212b8 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 64 62 20  e.bEnabled ? db 
212b9 3a 20 30 3b 0a 20 20 69 66 28 20 70 50 61 72 73  : 0;.  if( pPars
212ba 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 29 20 73  e->pNewTable ) s
212bb 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
212bc 65 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  e(pParse->pNewTa
212bd 62 6c 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  ble);.  pParse->
212be 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 54 61 62  pNewTable = pTab
212bf 6c 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69  le;..  /* If thi
212c0 73 20 69 73 20 74 68 65 20 6d 61 67 69 63 20 73  s is the magic s
212c1 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
212c2 61 62 6c 65 20 75 73 65 64 20 62 79 20 61 75 74  able used by aut
212c3 6f 69 6e 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a  oincrement,.  **
212c4 20 74 68 65 6e 20 72 65 63 6f 72 64 20 61 20 70   then record a p
212c5 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 74  ointer to this t
212c6 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e  able in the main
212c7 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
212c8 75 72 65 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74  ure.  ** so that
212c9 20 49 4e 53 45 52 54 20 63 61 6e 20 66 69 6e 64   INSERT can find
212ca 20 74 68 65 20 74 61 62 6c 65 20 65 61 73 69 6c   the table easil
212cb 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  y..  */.#ifndef 
212cc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
212cd 49 4e 43 52 45 4d 45 4e 54 0a 20 20 69 66 28 20  INCREMENT.  if( 
212ce 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20  !pParse->nested 
212cf 26 26 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c  && strcmp(zName,
212d0 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63   "sqlite_sequenc
212d1 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54  e")==0 ){.    pT
212d2 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70  able->pSchema->p
212d3 53 65 71 54 61 62 20 3d 20 70 54 61 62 6c 65 3b  SeqTab = pTable;
212d4 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
212d5 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69  * Begin generati
212d6 6e 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 74  ng the code that
212d7 20 77 69 6c 6c 20 69 6e 73 65 72 74 20 74 68 65   will insert the
212d8 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 69 6e   table record in
212d9 74 6f 0a 20 20 2a 2a 20 74 68 65 20 53 51 4c 49  to.  ** the SQLI
212da 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e  TE_MASTER table.
212db 20 20 4e 6f 74 65 20 69 6e 20 70 61 72 74 69 63    Note in partic
212dc 75 6c 61 72 20 74 68 61 74 20 77 65 20 6d 75 73  ular that we mus
212dd 74 20 67 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20  t go ahead.  ** 
212de 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 74 68 65  and allocate the
212df 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66   record number f
212e0 6f 72 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74  or the table ent
212e1 72 79 20 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20  ry now.  Before 
212e2 61 6e 79 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59  any.  ** PRIMARY
212e3 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20 6b   KEY or UNIQUE k
212e4 65 79 77 6f 72 64 73 20 61 72 65 20 70 61 72 73  eywords are pars
212e5 65 64 2e 20 20 54 68 6f 73 65 20 6b 65 79 77 6f  ed.  Those keywo
212e6 72 64 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 20  rds will cause. 
212e7 20 2a 2a 20 69 6e 64 69 63 65 73 20 74 6f 20 62   ** indices to b
212e8 65 20 63 72 65 61 74 65 64 20 61 6e 64 20 74 68  e created and th
212e9 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 6d  e table record m
212ea 75 73 74 20 63 6f 6d 65 20 62 65 66 6f 72 65 20  ust come before 
212eb 74 68 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65  the .  ** indice
212ec 73 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 72  s.  Hence, the r
212ed 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72  ecord number for
212ee 20 74 68 65 20 74 61 62 6c 65 20 6d 75 73 74 20   the table must 
212ef 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a  be allocated.  *
212f0 2a 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  * now..  */.  if
212f1 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
212f2 20 26 26 20 28 76 20 3d 20 73 71 6c 69 74 65 33   && (v = sqlite3
212f3 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29  GetVdbe(pParse))
212f4 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a  !=0 ){.    int j
212f5 31 3b 0a 20 20 20 20 69 6e 74 20 66 69 6c 65 46  1;.    int fileF
212f6 6f 72 6d 61 74 3b 0a 20 20 20 20 69 6e 74 20 72  ormat;.    int r
212f7 65 67 31 2c 20 72 65 67 32 2c 20 72 65 67 33 3b  eg1, reg2, reg3;
212f8 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
212f9 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
212fa 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
212fb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
212fc 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
212fd 4c 45 0a 20 20 20 20 69 66 28 20 69 73 56 69 72  LE.    if( isVir
212fe 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71  tual ){.      sq
212ff 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
21300 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20  v, OP_VBegin);. 
21301 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
21302 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20   /* If the file 
21303 66 6f 72 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64  format and encod
21304 69 6e 67 20 69 6e 20 74 68 65 20 64 61 74 61 62  ing in the datab
21305 61 73 65 20 68 61 76 65 20 6e 6f 74 20 62 65 65  ase have not bee
21306 6e 20 73 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73  n set, .    ** s
21307 65 74 20 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20  et them now..   
21308 20 2a 2f 0a 20 20 20 20 72 65 67 31 20 3d 20 70   */.    reg1 = p
21309 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 20  Parse->regRowid 
2130a 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2130b 3b 0a 20 20 20 20 72 65 67 32 20 3d 20 70 50 61  ;.    reg2 = pPa
2130c 72 73 65 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b  rse->regRoot = +
2130d 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
2130e 20 20 20 72 65 67 33 20 3d 20 2b 2b 70 50 61 72     reg3 = ++pPar
2130f 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
21310 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21311 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  v, OP_ReadCookie
21312 2c 20 69 44 62 2c 20 72 65 67 33 2c 20 31 29 3b  , iDb, reg3, 1);
21313 20 20 20 2f 2a 20 66 69 6c 65 5f 66 6f 72 6d 61     /* file_forma
21314 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  t */.    sqlite3
21315 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
21316 20 69 44 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20   iDb);.    j1 = 
21317 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21318 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33  1(v, OP_If, reg3
21319 29 3b 0a 20 20 20 20 66 69 6c 65 46 6f 72 6d 61  );.    fileForma
2131a 74 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  t = (db->flags &
2131b 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69   SQLITE_LegacyFi
2131c 6c 65 46 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20  leFmt)!=0 ?.    
2131d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
2131e 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c  : SQLITE_MAX_FIL
2131f 45 5f 46 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71  E_FORMAT;.    sq
21320 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21321 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66  v, OP_Integer, f
21322 69 6c 65 46 6f 72 6d 61 74 2c 20 72 65 67 33 29  ileFormat, reg3)
21323 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21324 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65  eAddOp3(v, OP_Se
21325 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 31 2c  tCookie, iDb, 1,
21326 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69   reg3);.    sqli
21327 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21328 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43   OP_Integer, ENC
21329 28 64 62 29 2c 20 72 65 67 33 29 3b 0a 20 20 20  (db), reg3);.   
2132a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2132b 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b  p3(v, OP_SetCook
2132c 69 65 2c 20 69 44 62 2c 20 34 2c 20 72 65 67 33  ie, iDb, 4, reg3
2132d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2132e 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
2132f 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  );..    /* This 
21330 6a 75 73 74 20 63 72 65 61 74 65 73 20 61 20 70  just creates a p
21331 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 65 63 6f  lace-holder reco
21332 72 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  rd in the sqlite
21333 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20  _master table.. 
21334 20 20 20 2a 2a 20 54 68 65 20 72 65 63 6f 72 64     ** The record
21335 20 63 72 65 61 74 65 64 20 64 6f 65 73 20 6e 6f   created does no
21336 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 74 68 69  t contain anythi
21337 6e 67 20 79 65 74 2e 20 20 49 74 20 77 69 6c 6c  ng yet.  It will
21338 20 62 65 20 72 65 70 6c 61 63 65 64 0a 20 20 20   be replaced.   
21339 20 2a 2a 20 62 79 20 74 68 65 20 72 65 61 6c 20   ** by the real 
2133a 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 20 67 65  entry in code ge
2133b 6e 65 72 61 74 65 64 20 61 74 20 73 71 6c 69 74  nerated at sqlit
2133c 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 20 20  e3EndTable()..  
2133d 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
2133e 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 6e 65  rowid for the ne
2133f 77 20 65 6e 74 72 79 20 69 73 20 6c 65 66 74 20  w entry is left 
21340 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61 72  in register pPar
21341 73 65 2d 3e 72 65 67 52 6f 77 69 64 2e 0a 20 20  se->regRowid..  
21342 20 20 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61    ** The root pa
21343 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
21344 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20 6c 65   new table is le
21345 66 74 20 69 6e 20 72 65 67 20 70 50 61 72 73 65  ft in reg pParse
21346 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a  ->regRoot..    *
21347 2a 20 54 68 65 20 72 6f 77 69 64 20 61 6e 64 20  * The rowid and 
21348 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
21349 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 65 64   values are need
2134a 65 64 20 62 79 20 74 68 65 20 63 6f 64 65 20 74  ed by the code t
2134b 68 61 74 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  hat.    ** sqlit
2134c 65 33 45 6e 64 54 61 62 6c 65 20 77 69 6c 6c 20  e3EndTable will 
2134d 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 20 2a 2f  generate..    */
2134e 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2134f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
21350 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
21351 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
21352 41 42 4c 45 29 0a 20 20 20 20 69 66 28 20 69 73  ABLE).    if( is
21353 56 69 65 77 20 7c 7c 20 69 73 56 69 72 74 75 61  View || isVirtua
21354 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
21355 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21356 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72  OP_Integer, 0, r
21357 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a  eg2);.    }else.
21358 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20  #endif.    {.   
21359 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2135a 64 4f 70 32 28 76 2c 20 4f 50 5f 43 72 65 61 74  dOp2(v, OP_Creat
2135b 65 54 61 62 6c 65 2c 20 69 44 62 2c 20 72 65 67  eTable, iDb, reg
2135c 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  2);.    }.    sq
2135d 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54  lite3OpenMasterT
2135e 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
2135f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
21360 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
21361 65 77 52 6f 77 69 64 2c 20 30 2c 20 72 65 67 31  ewRowid, 0, reg1
21362 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
21363 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
21364 75 6c 6c 2c 20 30 2c 20 72 65 67 33 29 3b 0a 20  ull, 0, reg3);. 
21365 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21366 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
21367 74 2c 20 30 2c 20 72 65 67 33 2c 20 72 65 67 31  t, 0, reg3, reg1
21368 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
21369 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
2136a 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
2136b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2136c 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29  Op0(v, OP_Close)
2136d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d  ;.  }..  /* Norm
2136e 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72 29 20 72  al (non-error) r
2136f 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 72 65 74 75  eturn. */.  retu
21370 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20  rn;..  /* If an 
21371 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 65  error occurs, we
21372 20 6a 75 6d 70 20 68 65 72 65 20 2a 2f 0a 62 65   jump here */.be
21373 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3a  gin_table_error:
21374 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
21375 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72  (db, zName);.  r
21376 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
21377 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73  This macro is us
21378 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 77  ed to compare tw
21379 6f 20 73 74 72 69 6e 67 73 20 69 6e 20 61 20 63  o strings in a c
2137a 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 65 20  ase-insensitive 
2137b 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49 74 20 69 73  manner..** It is
2137c 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72   slightly faster
2137d 20 74 68 61 6e 20 63 61 6c 6c 69 6e 67 20 73 71   than calling sq
2137e 6c 69 74 65 33 53 74 72 49 43 6d 70 28 29 20 64  lite3StrICmp() d
2137f 69 72 65 63 74 6c 79 2c 20 62 75 74 0a 2a 2a 20  irectly, but.** 
21380 70 72 6f 64 75 63 65 73 20 6c 61 72 67 65 72 20  produces larger 
21381 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e  code..**.** WARN
21382 49 4e 47 3a 20 54 68 69 73 20 6d 61 63 72 6f 20  ING: This macro 
21383 69 73 20 6e 6f 74 20 63 6f 6d 70 61 74 69 62 6c  is not compatibl
21384 65 20 77 69 74 68 20 74 68 65 20 73 74 72 63 6d  e with the strcm
21385 70 28 29 20 66 61 6d 69 6c 79 2e 20 49 74 0a 2a  p() family. It.*
21386 2a 20 72 65 74 75 72 6e 73 20 74 72 75 65 20 69  * returns true i
21387 66 20 74 68 65 20 74 77 6f 20 73 74 72 69 6e 67  f the two string
21388 73 20 61 72 65 20 65 71 75 61 6c 2c 20 6f 74 68  s are equal, oth
21389 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f  erwise false..*/
2138a 0a 23 64 65 66 69 6e 65 20 53 54 52 49 43 4d 50  .#define STRICMP
2138b 28 78 2c 20 79 29 20 28 5c 0a 73 71 6c 69 74 65  (x, y) (\.sqlite
2138c 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 28  3UpperToLower[*(
2138d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
2138e 28 78 29 5d 3d 3d 20 20 20 5c 0a 73 71 6c 69 74  (x)]==   \.sqlit
2138f 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  e3UpperToLower[*
21390 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
21391 29 28 79 29 5d 20 20 20 20 20 5c 0a 26 26 20 73  )(y)]     \.&& s
21392 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 28 78  qlite3StrICmp((x
21393 29 2b 31 2c 28 79 29 2b 31 29 3d 3d 30 20 29 0a  )+1,(y)+1)==0 ).
21394 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
21395 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20 74   column to the t
21396 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 62  able currently b
21397 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
21398 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ..**.** The pars
21399 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
2139a 75 74 69 6e 65 20 6f 6e 63 65 20 66 6f 72 20 65  utine once for e
2139b 61 63 68 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61  ach column decla
2139c 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 20 43  ration.** in a C
2139d 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
2139e 65 6d 65 6e 74 2e 20 20 73 71 6c 69 74 65 33 53  ement.  sqlite3S
2139f 74 61 72 74 54 61 62 6c 65 28 29 20 67 65 74 73  tartTable() gets
213a0 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 69 72 73 74   called.** first
213a1 20 74 6f 20 67 65 74 20 74 68 69 6e 67 73 20 67   to get things g
213a2 6f 69 6e 67 2e 20 20 54 68 65 6e 20 74 68 69 73  oing.  Then this
213a3 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
213a4 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 63  ed for each.** c
213a5 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  olumn..*/.SQLITE
213a6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
213a7 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50  lite3AddColumn(P
213a8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f  arse *pParse, To
213a9 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 54  ken *pName){.  T
213aa 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69  able *p;.  int i
213ab 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 43  ;.  char *z;.  C
213ac 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73  olumn *pCol;.  s
213ad 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
213ae 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28  rse->db;.  if( (
213af 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
213b0 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75  Table)==0 ) retu
213b1 72 6e 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  rn;.#if SQLITE_M
213b2 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20  AX_COLUMN.  if( 
213b3 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e 61 4c  p->nCol+1>db->aL
213b4 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
213b5 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20  T_COLUMN] ){.   
213b6 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
213b7 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
213b8 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20 25 73  ny columns on %s
213b9 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  ", p->zName);.  
213ba 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65    return;.  }.#e
213bb 6e 64 69 66 0a 20 20 7a 20 3d 20 73 71 6c 69 74  ndif.  z = sqlit
213bc 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
213bd 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66  db, pName);.  if
213be 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( z==0 ) return;
213bf 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
213c0 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
213c1 20 69 66 28 20 53 54 52 49 43 4d 50 28 7a 2c 20   if( STRICMP(z, 
213c2 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65  p->aCol[i].zName
213c3 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
213c4 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
213c5 65 2c 20 22 64 75 70 6c 69 63 61 74 65 20 63 6f  e, "duplicate co
213c6 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25 73 22 2c 20  lumn name: %s", 
213c7 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  z);.      sqlite
213c8 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a  3DbFree(db, z);.
213c9 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
213ca 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 28 70    }.  }.  if( (p
213cb 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d 3d 30  ->nCol & 0x7)==0
213cc 20 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a   ){.    Column *
213cd 61 4e 65 77 3b 0a 20 20 20 20 61 4e 65 77 20 3d  aNew;.    aNew =
213ce 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
213cf 63 28 64 62 2c 70 2d 3e 61 43 6f 6c 2c 28 70 2d  c(db,p->aCol,(p-
213d0 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65 6f 66 28  >nCol+8)*sizeof(
213d1 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20  p->aCol[0]));.  
213d2 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b    if( aNew==0 ){
213d3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
213d4 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20  Free(db, z);.   
213d5 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
213d6 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 61  .    p->aCol = a
213d7 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 20  New;.  }.  pCol 
213d8 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43  = &p->aCol[p->nC
213d9 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 70 43  ol];.  memset(pC
213da 6f 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d  ol, 0, sizeof(p-
213db 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20 70 43  >aCol[0]));.  pC
213dc 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 3b 0a 20  ol->zName = z;. 
213dd 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
213de 73 20 6e 6f 20 74 79 70 65 20 73 70 65 63 69 66  s no type specif
213df 69 65 64 2c 20 63 6f 6c 75 6d 6e 73 20 68 61 76  ied, columns hav
213e0 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 61 66  e the default af
213e1 66 69 6e 69 74 79 0a 20 20 2a 2a 20 27 4e 4f 4e  finity.  ** 'NON
213e2 45 27 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  E'. If there is 
213e3 61 20 74 79 70 65 20 73 70 65 63 69 66 69 65 64  a type specified
213e4 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 41 64  , then sqlite3Ad
213e5 64 43 6f 6c 75 6d 6e 54 79 70 65 28 29 20 77 69  dColumnType() wi
213e6 6c 6c 0a 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65  ll.  ** be calle
213e7 64 20 6e 65 78 74 20 74 6f 20 73 65 74 20 70 43  d next to set pC
213e8 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 63 6f 72  ol->affinity cor
213e9 72 65 63 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 70  rectly..  */.  p
213ea 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20  Col->affinity = 
213eb 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
213ec 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a  .  p->nCol++;.}.
213ed 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
213ee 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ine is called by
213ef 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c   the parser whil
213f0 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  e in the middle 
213f1 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20  of.** parsing a 
213f2 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
213f3 74 65 6d 65 6e 74 2e 20 20 41 20 22 4e 4f 54 20  tement.  A "NOT 
213f4 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74  NULL" constraint
213f5 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 73 65 65   has.** been see
213f6 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e 20 20  n on a column.  
213f7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
213f8 73 20 74 68 65 20 6e 6f 74 4e 75 6c 6c 20 66 6c  s the notNull fl
213f9 61 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 6f 6c  ag on.** the col
213fa 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  umn currently un
213fb 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
213fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
213fd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
213fe 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65  AddNotNull(Parse
213ff 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e   *pParse, int on
21400 45 72 72 6f 72 29 7b 0a 20 20 54 61 62 6c 65 20  Error){.  Table 
21401 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  *p;.  int i;.  i
21402 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e  f( (p = pParse->
21403 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20  pNewTable)==0 ) 
21404 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d  return;.  i = p-
21405 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69  >nCol-1;.  if( i
21406 3e 3d 30 20 29 20 70 2d 3e 61 43 6f 6c 5b 69 5d  >=0 ) p->aCol[i]
21407 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 28 75 38 29 6f  .notNull = (u8)o
21408 6e 45 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  nError;.}../*.**
21409 20 53 63 61 6e 20 74 68 65 20 63 6f 6c 75 6d 6e   Scan the column
2140a 20 74 79 70 65 20 6e 61 6d 65 20 7a 54 79 70 65   type name zType
2140b 20 28 6c 65 6e 67 74 68 20 6e 54 79 70 65 29 20   (length nType) 
2140c 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 0a 2a  and return the.*
2140d 2a 20 61 73 73 6f 63 69 61 74 65 64 20 61 66 66  * associated aff
2140e 69 6e 69 74 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a  inity type..**.*
2140f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
21410 6f 65 73 20 61 20 63 61 73 65 2d 69 6e 64 65 70  oes a case-indep
21411 65 6e 64 65 6e 74 20 73 65 61 72 63 68 20 6f 66  endent search of
21412 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65 20 0a   zType for the .
21413 2a 2a 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e  ** substrings in
21414 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
21415 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20 6f 66 20  able. If one of 
21416 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 20 69  the substrings i
21417 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65 20  s.** found, the 
21418 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 66  corresponding af
21419 66 69 6e 69 74 79 20 69 73 20 72 65 74 75 72 6e  finity is return
2141a 65 64 2e 20 49 66 20 7a 54 79 70 65 20 63 6f 6e  ed. If zType con
2141b 74 61 69 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74 68  tains.** more th
2141c 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75  an one of the su
2141d 62 73 74 72 69 6e 67 73 2c 20 65 6e 74 72 69 65  bstrings, entrie
2141e 73 20 74 6f 77 61 72 64 20 74 68 65 20 74 6f 70  s toward the top
2141f 20 6f 66 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c   of .** the tabl
21420 65 20 74 61 6b 65 20 70 72 69 6f 72 69 74 79 2e  e take priority.
21421 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
21422 20 7a 54 79 70 65 20 69 73 20 27 42 4c 4f 42 49   zType is 'BLOBI
21423 4e 54 27 2c 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  NT', .** SQLITE_
21424 41 46 46 5f 49 4e 54 45 47 45 52 20 69 73 20 72  AFF_INTEGER is r
21425 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  eturned..**.** S
21426 75 62 73 74 72 69 6e 67 20 20 20 20 20 7c 20 41  ubstring     | A
21427 66 66 69 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d  ffinity.** -----
21428 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
21429 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27  -----------.** '
2142a 49 4e 54 27 20 20 20 20 20 20 20 20 20 7c 20 53  INT'         | S
2142b 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
2142c 52 0a 2a 2a 20 27 43 48 41 52 27 20 20 20 20 20  R.** 'CHAR'     
2142d 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
2142e 54 45 58 54 0a 2a 2a 20 27 43 4c 4f 42 27 20 20  TEXT.** 'CLOB'  
2142f 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
21430 46 46 5f 54 45 58 54 0a 2a 2a 20 27 54 45 58 54  FF_TEXT.** 'TEXT
21431 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
21432 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 42  E_AFF_TEXT.** 'B
21433 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53 51  LOB'        | SQ
21434 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a 2a  LITE_AFF_NONE.**
21435 20 27 52 45 41 4c 27 20 20 20 20 20 20 20 20 7c   'REAL'        |
21436 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
21437 0a 2a 2a 20 27 46 4c 4f 41 27 20 20 20 20 20 20  .** 'FLOA'      
21438 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52    | SQLITE_AFF_R
21439 45 41 4c 0a 2a 2a 20 27 44 4f 55 42 27 20 20 20  EAL.** 'DOUB'   
2143a 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
2143b 46 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20  F_REAL.**.** If 
2143c 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73  none of the subs
2143d 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 61 62  trings in the ab
2143e 6f 76 65 20 74 61 62 6c 65 20 61 72 65 20 66 6f  ove table are fo
2143f 75 6e 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 41  und,.** SQLITE_A
21440 46 46 5f 4e 55 4d 45 52 49 43 20 69 73 20 72 65  FF_NUMERIC is re
21441 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
21442 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73  E_PRIVATE char s
21443 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79  qlite3AffinityTy
21444 70 65 28 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a  pe(const Token *
21445 70 54 79 70 65 29 7b 0a 20 20 75 33 32 20 68 20  pType){.  u32 h 
21446 3d 20 30 3b 0a 20 20 63 68 61 72 20 61 66 66 20  = 0;.  char aff 
21447 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  = SQLITE_AFF_NUM
21448 45 52 49 43 3b 0a 20 20 63 6f 6e 73 74 20 75 6e  ERIC;.  const un
21449 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e  signed char *zIn
2144a 20 3d 20 70 54 79 70 65 2d 3e 7a 3b 0a 20 20 63   = pType->z;.  c
2144b 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2144c 61 72 20 2a 7a 45 6e 64 20 3d 20 26 70 54 79 70  ar *zEnd = &pTyp
2144d 65 2d 3e 7a 5b 70 54 79 70 65 2d 3e 6e 5d 3b 0a  e->z[pType->n];.
2144e 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a  .  while( zIn!=z
2144f 45 6e 64 20 29 7b 0a 20 20 20 20 68 20 3d 20 28  End ){.    h = (
21450 68 3c 3c 38 29 20 2b 20 73 71 6c 69 74 65 33 55  h<<8) + sqlite3U
21451 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 49 6e  pperToLower[*zIn
21452 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20  ];.    zIn++;.  
21453 20 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c    if( h==(('c'<<
21454 32 34 29 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27  24)+('h'<<16)+('
21455 61 27 3c 3c 38 29 2b 27 72 27 29 20 29 7b 20 20  a'<<8)+'r') ){  
21456 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 48             /* CH
21457 41 52 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20  AR */.      aff 
21458 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  = SQLITE_AFF_TEX
21459 54 3b 20 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  T; .    }else if
2145a 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b  ( h==(('c'<<24)+
2145b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c  ('l'<<16)+('o'<<
2145c 38 29 2b 27 62 27 29 20 29 7b 20 20 20 20 20 20  8)+'b') ){      
2145d 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20   /* CLOB */.    
2145e 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
2145f 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c  FF_TEXT;.    }el
21460 73 65 20 69 66 28 20 68 3d 3d 28 28 27 74 27 3c  se if( h==(('t'<
21461 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28  <24)+('e'<<16)+(
21462 27 78 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20  'x'<<8)+'t') ){ 
21463 20 20 20 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f        /* TEXT */
21464 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c  .      aff = SQL
21465 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20  ITE_AFF_TEXT;.  
21466 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28    }else if( h==(
21467 28 27 62 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c  ('b'<<24)+('l'<<
21468 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27  16)+('o'<<8)+'b'
21469 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 4c  )          /* BL
2146a 4f 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26  OB */.        &&
2146b 20 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46   (aff==SQLITE_AF
2146c 46 5f 4e 55 4d 45 52 49 43 20 7c 7c 20 61 66 66  F_NUMERIC || aff
2146d 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  ==SQLITE_AFF_REA
2146e 4c 29 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20  L) ){.      aff 
2146f 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
21470 45 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  E;.#ifndef SQLIT
21471 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
21472 50 4f 49 4e 54 0a 20 20 20 20 7d 65 6c 73 65 20  POINT.    }else 
21473 69 66 28 20 68 3d 3d 28 28 27 72 27 3c 3c 32 34  if( h==(('r'<<24
21474 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 61 27  )+('e'<<16)+('a'
21475 3c 3c 38 29 2b 27 6c 27 29 20 20 20 20 20 20 20  <<8)+'l')       
21476 20 20 20 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 20     /* REAL */.  
21477 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51        && aff==SQ
21478 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
21479 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20   ){.      aff = 
2147a 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b  SQLITE_AFF_REAL;
2147b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68  .    }else if( h
2147c 3d 3d 28 28 27 66 27 3c 3c 32 34 29 2b 28 27 6c  ==(('f'<<24)+('l
2147d 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b  '<<16)+('o'<<8)+
2147e 27 61 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a  'a')          /*
2147f 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 20 20 20 20   FLOA */.       
21480 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f   && aff==SQLITE_
21481 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20  AFF_NUMERIC ){. 
21482 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54       aff = SQLIT
21483 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20  E_AFF_REAL;.    
21484 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
21485 64 27 3c 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 36  d'<<24)+('o'<<16
21486 29 2b 28 27 75 27 3c 3c 38 29 2b 27 62 27 29 20  )+('u'<<8)+'b') 
21487 20 20 20 20 20 20 20 20 20 2f 2a 20 44 4f 55 42           /* DOUB
21488 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61   */.        && a
21489 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
2148a 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20  UMERIC ){.      
2148b 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
2148c 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 20  _REAL;.#endif.  
2148d 20 20 7d 65 6c 73 65 20 69 66 28 20 28 68 26 30    }else if( (h&0
2148e 78 30 30 46 46 46 46 46 46 29 3d 3d 28 28 27 69  x00FFFFFF)==(('i
2148f 27 3c 3c 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b  '<<16)+('n'<<8)+
21490 27 74 27 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e  't') ){    /* IN
21491 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d  T */.      aff =
21492 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
21493 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  GER;.      break
21494 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
21495 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a  eturn aff;.}../*
21496 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
21497 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
21498 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69  e parser while i
21499 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a  n the middle of.
2149a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45  ** parsing a CRE
2149b 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
2149c 65 6e 74 2e 20 20 54 68 65 20 70 46 69 72 73 74  ent.  The pFirst
2149d 20 74 6f 6b 65 6e 20 69 73 20 74 68 65 20 66 69   token is the fi
2149e 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20  rst.** token in 
2149f 74 68 65 20 73 65 71 75 65 6e 63 65 20 6f 66 20  the sequence of 
214a0 74 6f 6b 65 6e 73 20 74 68 61 74 20 64 65 73 63  tokens that desc
214a1 72 69 62 65 20 74 68 65 20 74 79 70 65 20 6f 66  ribe the type of
214a2 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63   the.** column c
214a3 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63  urrently under c
214a4 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 20 70  onstruction.   p
214a5 4c 61 73 74 20 69 73 20 74 68 65 20 6c 61 73 74  Last is the last
214a6 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68 65   token.** in the
214a7 20 73 65 71 75 65 6e 63 65 2e 20 20 55 73 65 20   sequence.  Use 
214a8 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  this information
214a9 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 61 20   to construct a 
214aa 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 63  string.** that c
214ab 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 79 70 65  ontains the type
214ac 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  name of the colu
214ad 6d 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68 61  mn and store tha
214ae 74 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 7a  t string.** in z
214af 54 79 70 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45  Type..*/ .SQLITE
214b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
214b1 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79  lite3AddColumnTy
214b2 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  pe(Parse *pParse
214b3 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b  , Token *pType){
214b4 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69  .  Table *p;.  i
214b5 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a  nt i;.  Column *
214b6 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20  pCol;.  sqlite3 
214b7 2a 64 62 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d  *db;..  if( (p =
214b8 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
214b9 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  le)==0 ) return;
214ba 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31  .  i = p->nCol-1
214bb 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 20 72 65  ;.  if( i<0 ) re
214bc 74 75 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26  turn;.  pCol = &
214bd 70 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 64 62  p->aCol[i];.  db
214be 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
214bf 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
214c0 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b  b, pCol->zType);
214c1 0a 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d  .  pCol->zType =
214c2 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
214c3 54 6f 6b 65 6e 28 64 62 2c 20 70 54 79 70 65 29  Token(db, pType)
214c4 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  ;.  pCol->affini
214c5 74 79 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69  ty = sqlite3Affi
214c6 6e 69 74 79 54 79 70 65 28 70 54 79 70 65 29 3b  nityType(pType);
214c7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78  .}../*.** The ex
214c8 70 72 65 73 73 69 6f 6e 20 69 73 20 74 68 65 20  pression is the 
214c9 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  default value fo
214ca 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  r the most recen
214cb 74 6c 79 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e  tly added column
214cc 0a 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65  .** of the table
214cd 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
214ce 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a   construction..*
214cf 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 76 61 6c  *.** Default val
214d0 75 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6d  ue expressions m
214d1 75 73 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e  ust be constant.
214d2 20 20 52 61 69 73 65 20 61 6e 20 65 78 63 65 70    Raise an excep
214d3 74 69 6f 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20  tion if this.** 
214d4 69 73 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2e  is not the case.
214d5 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
214d6 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ine is called by
214d7 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c   the parser whil
214d8 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  e in the middle 
214d9 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20  of.** parsing a 
214da 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
214db 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  tement..*/.SQLIT
214dc 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
214dd 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
214de 56 61 6c 75 65 28 50 61 72 73 65 20 2a 70 50 61  Value(Parse *pPa
214df 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
214e0 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
214e1 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
214e2 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
214e3 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
214e4 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e   (p = pParse->pN
214e5 65 77 54 61 62 6c 65 29 21 3d 30 20 29 7b 0a 20  ewTable)!=0 ){. 
214e6 20 20 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61     pCol = &(p->a
214e7 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b  Col[p->nCol-1]);
214e8 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
214e9 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f  3ExprIsConstantO
214ea 72 46 75 6e 63 74 69 6f 6e 28 70 45 78 70 72 29  rFunction(pExpr)
214eb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
214ec 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
214ed 2c 20 22 64 65 66 61 75 6c 74 20 76 61 6c 75 65  , "default value
214ee 20 6f 66 20 63 6f 6c 75 6d 6e 20 5b 25 73 5d 20   of column [%s] 
214ef 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 22  is not constant"
214f0 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c  ,.          pCol
214f1 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65  ->zName);.    }e
214f2 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  lse{.      /* A 
214f3 63 6f 70 79 20 6f 66 20 70 45 78 70 72 20 69 73  copy of pExpr is
214f4 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66   used instead of
214f5 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 2c 20 61   the original, a
214f6 73 20 70 45 78 70 72 20 63 6f 6e 74 61 69 6e 73  s pExpr contains
214f7 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 73  .      ** tokens
214f8 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 76   that point to v
214f9 6f 6c 61 74 69 6c 65 20 6d 65 6d 6f 72 79 2e 20  olatile memory. 
214fa 54 68 65 20 27 73 70 61 6e 27 20 6f 66 20 74 68  The 'span' of th
214fb 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20  e expression.   
214fc 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65     ** is require
214fd 64 20 62 79 20 70 72 61 67 6d 61 20 74 61 62 6c  d by pragma tabl
214fe 65 5f 69 6e 66 6f 2e 0a 20 20 20 20 20 20 2a 2f  e_info..      */
214ff 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21500 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  prDelete(db, pCo
21501 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20  l->pDflt);.     
21502 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 73   pCol->pDflt = s
21503 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 0a 20  qlite3ExprDup(. 
21504 20 20 20 20 20 20 20 20 20 64 62 2c 20 70 45 78           db, pEx
21505 70 72 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  pr, EXPRDUP_REDU
21506 43 45 7c 45 58 50 52 44 55 50 5f 44 49 53 54 49  CE|EXPRDUP_DISTI
21507 4e 43 54 53 50 41 4e 0a 20 20 20 20 20 20 29 3b  NCTSPAN.      );
21508 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
21509 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2150a 62 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a  b, pExpr);.}../*
2150b 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74 68  .** Designate th
2150c 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 66 6f  e PRIMARY KEY fo
2150d 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20 70 4c  r the table.  pL
2150e 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66  ist is a list of
2150f 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 6f   names .** of co
21510 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d 20  lumns that form 
21511 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e  the primary key.
21512 20 20 49 66 20 70 4c 69 73 74 20 69 73 20 4e 55    If pList is NU
21513 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  LL, then the.** 
21514 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64  most recently ad
21515 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ded column of th
21516 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 70  e table is the p
21517 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a  rimary key..**.*
21518 2a 20 41 20 74 61 62 6c 65 20 63 61 6e 20 68 61  * A table can ha
21519 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 70  ve at most one p
2151a 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20  rimary key.  If 
2151b 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64  the table alread
2151c 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d 61  y has.** a prima
2151d 72 79 20 6b 65 79 20 28 61 6e 64 20 74 68 69 73  ry key (and this
2151e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   is the second p
2151f 72 69 6d 61 72 79 20 6b 65 79 29 20 74 68 65 6e  rimary key) then
21520 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65 72   create an.** er
21521 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ror..**.** If th
21522 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73  e PRIMARY KEY is
21523 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c   on a single col
21524 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 61 74 79  umn whose dataty
21525 70 65 20 69 73 20 49 4e 54 45 47 45 52 2c 0a 2a  pe is INTEGER,.*
21526 2a 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 74  * then we will t
21527 72 79 20 74 6f 20 75 73 65 20 74 68 61 74 20 63  ry to use that c
21528 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 6f 77  olumn as the row
21529 69 64 2e 20 20 53 65 74 20 74 68 65 20 54 61 62  id.  Set the Tab
2152a 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65 6c  le.iPKey.** fiel
2152b 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 75  d of the table u
2152c 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
2152d 6e 20 74 6f 20 62 65 20 74 68 65 20 69 6e 64 65  n to be the inde
2152e 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54 45  x of the.** INTE
2152f 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
21530 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e 69  column.  Table.i
21531 50 4b 65 79 20 69 73 20 73 65 74 20 74 6f 20 2d  PKey is set to -
21532 31 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a  1 if there is.**
21533 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d   no INTEGER PRIM
21534 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49  ARY KEY..**.** I
21535 66 20 74 68 65 20 6b 65 79 20 69 73 20 6e 6f 74  f the key is not
21536 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
21537 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 63 72  ARY KEY, then cr
21538 65 61 74 65 20 61 20 75 6e 69 71 75 65 0a 2a 2a  eate a unique.**
21539 20 69 6e 64 65 78 20 66 6f 72 20 74 68 65 20 6b   index for the k
2153a 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69 73  ey.  No index is
2153b 20 63 72 65 61 74 65 64 20 66 6f 72 20 49 4e 54   created for INT
2153c 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
2153d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
2153e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2153f 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 0a  3AddPrimaryKey(.
21540 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
21541 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
21542 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
21543 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a  List *pList,  /*
21544 20 4c 69 73 74 20 6f 66 20 66 69 65 6c 64 20 6e   List of field n
21545 61 6d 65 73 20 74 6f 20 62 65 20 69 6e 64 65 78  ames to be index
21546 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72  ed */.  int onEr
21547 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57 68 61  ror,      /* Wha
21548 74 20 74 6f 20 64 6f 20 77 69 74 68 20 61 20 75  t to do with a u
21549 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69  niqueness confli
2154a 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74 6f  ct */.  int auto
2154b 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54 72 75  Inc,      /* Tru
2154c 65 20 69 66 20 74 68 65 20 41 55 54 4f 49 4e 43  e if the AUTOINC
2154d 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20 69  REMENT keyword i
2154e 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69  s present */.  i
2154f 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 20 20 20  nt sortOrder    
21550 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53   /* SQLITE_SO_AS
21551 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f 44  C or SQLITE_SO_D
21552 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  ESC */.){.  Tabl
21553 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65  e *pTab = pParse
21554 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 63  ->pNewTable;.  c
21555 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a  har *zType = 0;.
21556 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 2c    int iCol = -1,
21557 20 69 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d   i;.  if( pTab==
21558 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f  0 || IN_DECLARE_
21559 56 54 41 42 20 29 20 67 6f 74 6f 20 70 72 69 6d  VTAB ) goto prim
2155a 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20  ary_key_exit;.  
2155b 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61  if( pTab->tabFla
2155c 67 73 20 26 20 54 46 5f 48 61 73 50 72 69 6d 61  gs & TF_HasPrima
2155d 72 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c  ryKey ){.    sql
2155e 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2155f 72 73 65 2c 20 0a 20 20 20 20 20 20 22 74 61 62  rse, .      "tab
21560 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 20 6d 6f  le \"%s\" has mo
21561 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 72 69 6d  re than one prim
21562 61 72 79 20 6b 65 79 22 2c 20 70 54 61 62 2d 3e  ary key", pTab->
21563 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
21564 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69   primary_key_exi
21565 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e 74  t;.  }.  pTab->t
21566 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61  abFlags |= TF_Ha
21567 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20 69  sPrimaryKey;.  i
21568 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20  f( pList==0 ){. 
21569 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     iCol = pTab->
2156a 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70 54  nCol - 1;.    pT
2156b 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69  ab->aCol[iCol].i
2156c 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20  sPrimKey = 1;.  
2156d 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69  }else{.    for(i
2156e 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
2156f 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
21570 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c  for(iCol=0; iCol
21571 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f  <pTab->nCol; iCo
21572 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  l++){.        if
21573 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
21574 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61  (pList->a[i].zNa
21575 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  me, pTab->aCol[i
21576 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29  Col].zName)==0 )
21577 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  {.          brea
21578 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
21579 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
2157a 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  Col<pTab->nCol )
2157b 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e  {.        pTab->
2157c 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69  aCol[iCol].isPri
2157d 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 20  mKey = 1;.      
2157e 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
2157f 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20 29  pList->nExpr>1 )
21580 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d 0a   iCol = -1;.  }.
21581 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26    if( iCol>=0 &&
21582 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c   iCol<pTab->nCol
21583 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20   ){.    zType = 
21584 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
21585 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69 66  .zType;.  }.  if
21586 28 20 7a 54 79 70 65 20 26 26 20 73 71 6c 69 74  ( zType && sqlit
21587 65 33 53 74 72 49 43 6d 70 28 7a 54 79 70 65 2c  e3StrICmp(zType,
21588 20 22 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a 20   "INTEGER")==0. 
21589 20 20 20 20 20 20 20 26 26 20 73 6f 72 74 4f 72         && sortOr
2158a 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41  der==SQLITE_SO_A
2158b 53 43 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e  SC ){.    pTab->
2158c 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20 20  iPKey = iCol;.  
2158d 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 20    pTab->keyConf 
2158e 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20  = (u8)onError;. 
2158f 20 20 20 61 73 73 65 72 74 28 20 61 75 74 6f 49     assert( autoI
21590 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63  nc==0 || autoInc
21591 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d  ==1 );.    pTab-
21592 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74  >tabFlags |= aut
21593 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72  oInc*TF_Autoincr
21594 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69  ement;.  }else i
21595 66 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69  f( autoInc ){.#i
21596 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
21597 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a  T_AUTOINCREMENT.
21598 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
21599 4d 73 67 28 70 50 61 72 73 65 2c 20 22 41 55 54  Msg(pParse, "AUT
2159a 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e  OINCREMENT is on
2159b 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e  ly allowed on an
2159c 20 22 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47   ".       "INTEG
2159d 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29  ER PRIMARY KEY")
2159e 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65  ;.#endif.  }else
2159f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65  {.    sqlite3Cre
215a0 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ateIndex(pParse,
215a1 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c   0, 0, 0, pList,
215a2 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20   onError, 0, 0, 
215a3 73 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20  sortOrder, 0);. 
215a4 20 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20     pList = 0;.  
215a5 7d 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65  }..primary_key_e
215a6 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78  xit:.  sqlite3Ex
215a7 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  prListDelete(pPa
215a8 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b  rse->db, pList);
215a9 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
215aa 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48  .** Add a new CH
215ab 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74  ECK constraint t
215ac 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72  o the table curr
215ad 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
215ae 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  truction..*/.SQL
215af 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
215b0 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b   sqlite3AddCheck
215b1 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61  Constraint(.  Pa
215b2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
215b3 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
215b4 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43  xt */.  Expr *pC
215b5 68 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65  heckExpr  /* The
215b6 20 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f   check expressio
215b7 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  n */.){.  sqlite
215b8 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
215b9 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  db;.#ifndef SQLI
215ba 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
215bb 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50  Table *pTab = pP
215bc 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
215bd 0a 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21  .  if( pTab && !
215be 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
215bf 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 43 48  ){.    /* The CH
215c0 45 43 4b 20 65 78 70 72 65 73 73 69 6f 6e 20 6d  ECK expression m
215c1 75 73 74 20 62 65 20 64 75 70 6c 69 63 61 74 65  ust be duplicate
215c2 64 20 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e 73  d so that tokens
215c3 20 72 65 66 65 72 0a 20 20 20 20 2a 2a 20 74 6f   refer.    ** to
215c4 20 6d 61 6c 6c 6f 63 65 64 20 73 70 61 63 65 20   malloced space 
215c5 61 6e 64 20 6e 6f 74 20 74 68 65 20 28 65 70 68  and not the (eph
215c6 65 6d 65 72 61 6c 29 20 74 65 78 74 20 6f 66 20  emeral) text of 
215c7 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
215c8 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  .    ** statemen
215c9 74 20 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e 70  t */.    pTab->p
215ca 43 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45  Check = sqlite3E
215cb 78 70 72 41 6e 64 28 64 62 2c 20 70 54 61 62 2d  xprAnd(db, pTab-
215cc 3e 70 43 68 65 63 6b 2c 20 0a 20 20 20 20 20 20  >pCheck, .      
215cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
215ce 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
215cf 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
215d0 43 68 65 63 6b 45 78 70 72 2c 20 30 29 29 3b 0a  CheckExpr, 0));.
215d1 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c    }.#endif.  sql
215d2 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
215d3 62 2c 20 70 43 68 65 63 6b 45 78 70 72 29 3b 0a  b, pCheckExpr);.
215d4 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
215d5 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74   collation funct
215d6 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20  ion of the most 
215d7 72 65 63 65 6e 74 6c 79 20 70 61 72 73 65 64 20  recently parsed 
215d8 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  table column.** 
215d9 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65 71 20 67  to the CollSeq g
215da 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  iven..*/.SQLITE_
215db 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
215dc 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79  ite3AddCollateTy
215dd 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  pe(Parse *pParse
215de 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29  , Token *pToken)
215df 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20  {.  Table *p;.  
215e0 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a  int i;.  char *z
215e1 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Coll;           
215e2 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e     /* Dequoted n
215e3 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e  ame of collation
215e4 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 73   sequence */.  s
215e5 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69  qlite3 *db;..  i
215e6 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e  f( (p = pParse->
215e7 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20  pNewTable)==0 ) 
215e8 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d  return;.  i = p-
215e9 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d 20  >nCol-1;.  db = 
215ea 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43  pParse->db;.  zC
215eb 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  oll = sqlite3Nam
215ec 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
215ed 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a  Token);.  if( !z
215ee 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 0a  Coll ) return;..
215ef 20 20 69 66 28 20 73 71 6c 69 74 65 33 4c 6f 63    if( sqlite3Loc
215f0 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
215f1 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b  e, zColl, -1) ){
215f2 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
215f3 3b 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d  ;.    p->aCol[i]
215f4 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a  .zColl = zColl;.
215f5 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65    .    /* If the
215f6 20 63 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61   column is decla
215f7 72 65 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50  red as "<name> P
215f8 52 49 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41  RIMARY KEY COLLA
215f9 54 45 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20  TE <type>",.    
215fa 2a 2a 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78  ** then an index
215fb 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 63   may have been c
215fc 72 65 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63  reated on this c
215fd 6f 6c 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65  olumn before the
215fe 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f  .    ** collatio
215ff 6e 20 74 79 70 65 20 77 61 73 20 61 64 64 65 64  n type was added
21600 2e 20 43 6f 72 72 65 63 74 20 74 68 69 73 20 69  . Correct this i
21601 66 20 69 74 20 69 73 20 74 68 65 20 63 61 73 65  f it is the case
21602 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
21603 28 70 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b  (pIdx=p->pIndex;
21604 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
21605 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
21606 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43  assert( pIdx->nC
21607 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20  olumn==1 );.    
21608 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f    if( pIdx->aiCo
21609 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20  lumn[0]==i ){.  
2160a 20 20 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f        pIdx->azCo
2160b 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b  ll[0] = p->aCol[
2160c 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  i].zColl;.      
2160d 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  }.    }.  }else{
2160e 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2160f 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20  ee(db, zColl);. 
21610 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
21611 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
21612 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  s the collation 
21613 73 65 71 75 65 6e 63 65 20 66 6f 72 20 64 61 74  sequence for dat
21614 61 62 61 73 65 20 6e 61 74 69 76 65 20 74 65 78  abase native tex
21615 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64  t.** encoding id
21616 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
21617 73 74 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65  string zName, le
21618 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a  ngth nName..**.*
21619 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74  * If the request
2161a 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
2161b 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61  uence is not ava
2161c 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61  ilable, or not a
2161d 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74  vailable.** in t
2161e 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69  he database nati
2161f 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65  ve encoding, the
21620 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f   collation facto
21621 72 79 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f  ry is invoked to
21622 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 74 2e 20  .** request it. 
21623 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  If the collation
21624 20 66 61 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f   factory does no
21625 74 20 73 75 70 70 6c 79 20 73 75 63 68 20 61 20  t supply such a 
21626 73 65 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64  sequence,.** and
21627 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 69 73   the sequence is
21628 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e   available in an
21629 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64  other text encod
2162a 69 6e 67 2c 20 74 68 65 6e 20 74 68 61 74 20 69  ing, then that i
2162b 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e  s.** returned in
2162c 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  stead..**.** If 
2162d 6e 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  no versions of t
2162e 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c  he requested col
2162f 6c 61 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 65  lations sequence
21630 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20   are available, 
21631 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72  or.** another er
21632 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c  ror occurs, NULL
21633 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
21634 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
21635 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a  e written into.*
21636 2a 20 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20  * pParse..**.** 
21637 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
21638 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64  a wrapper around
21639 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
2163a 53 65 71 28 29 2e 20 20 54 68 69 73 20 72 6f 75  Seq().  This rou
2163b 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20  tine.** invokes 
2163c 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
2163d 63 74 6f 72 79 20 69 66 20 74 68 65 20 6e 61 6d  ctory if the nam
2163e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e  ed collation can
2163f 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20  not be found.** 
21640 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e  and generates an
21641 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
21642 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21643 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
21644 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
21645 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
21646 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
21647 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 20  , int nName){.  
21648 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
21649 61 72 73 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65  arse->db;.  u8 e
2164a 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20  nc = ENC(db);.  
2164b 75 38 20 69 6e 69 74 62 75 73 79 20 3d 20 64 62  u8 initbusy = db
2164c 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43  ->init.busy;.  C
2164d 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a  ollSeq *pColl;..
2164e 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2164f 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
21650 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61   enc, zName, nNa
21651 6d 65 2c 20 69 6e 69 74 62 75 73 79 29 3b 0a 20  me, initbusy);. 
21652 20 69 66 28 20 21 69 6e 69 74 62 75 73 79 20 26   if( !initbusy &
21653 26 20 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43  & (!pColl || !pC
21654 6f 6c 6c 2d 3e 78 43 6d 70 29 20 29 7b 0a 20 20  oll->xCmp) ){.  
21655 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
21656 33 47 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20  3GetCollSeq(db, 
21657 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  pColl, zName, nN
21658 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70  ame);.    if( !p
21659 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  Coll ){.      if
2165a 28 20 6e 4e 61 6d 65 3c 30 20 29 7b 0a 20 20 20  ( nName<0 ){.   
2165b 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c       nName = sql
2165c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a  ite3Strlen(db, z
2165d 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Name);.      }. 
2165e 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2165f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
21660 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20   such collation 
21661 73 65 71 75 65 6e 63 65 3a 20 25 2e 2a 73 22 2c  sequence: %.*s",
21662 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a   nName, zName);.
21663 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b        pColl = 0;
21664 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
21665 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a  turn pColl;.}...
21666 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
21667 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e  ode that will in
21668 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65  crement the sche
21669 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a  ma cookie..**.**
2166a 20 54 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   The schema cook
2166b 69 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  ie is used to de
2166c 74 65 72 6d 69 6e 65 20 77 68 65 6e 20 74 68 65  termine when the
2166d 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65 0a   schema for the.
2166e 2a 2a 20 64 61 74 61 62 61 73 65 20 63 68 61 6e  ** database chan
2166f 67 65 73 2e 20 20 41 66 74 65 72 20 65 61 63 68  ges.  After each
21670 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2c 20   schema change, 
21671 74 68 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65  the cookie value
21672 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 57 68  .** changes.  Wh
21673 65 6e 20 61 20 70 72 6f 63 65 73 73 20 66 69 72  en a process fir
21674 73 74 20 72 65 61 64 73 20 74 68 65 20 73 63 68  st reads the sch
21675 65 6d 61 20 69 74 20 72 65 63 6f 72 64 73 20 74  ema it records t
21676 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20 54  he.** cookie.  T
21677 68 65 72 65 61 66 74 65 72 2c 20 77 68 65 6e 65  hereafter, whene
21678 76 65 72 20 69 74 20 67 6f 65 73 20 74 6f 20 61  ver it goes to a
21679 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62 61  ccess the databa
2167a 73 65 2c 0a 2a 2a 20 69 74 20 63 68 65 63 6b 73  se,.** it checks
2167b 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 6d   the cookie to m
2167c 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 63 68  ake sure the sch
2167d 65 6d 61 20 68 61 73 20 6e 6f 74 20 63 68 61 6e  ema has not chan
2167e 67 65 64 0a 2a 2a 20 73 69 6e 63 65 20 69 74 20  ged.** since it 
2167f 77 61 73 20 6c 61 73 74 20 72 65 61 64 2e 0a 2a  was last read..*
21680 2a 0a 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20 69  *.** This plan i
21681 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79  s not completely
21682 20 62 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20 20   bullet-proof.  
21683 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66  It is possible f
21684 6f 72 0a 2a 2a 20 74 68 65 20 73 63 68 65 6d 61  or.** the schema
21685 20 74 6f 20 63 68 61 6e 67 65 20 6d 75 6c 74 69   to change multi
21686 70 6c 65 20 74 69 6d 65 73 20 61 6e 64 20 66 6f  ple times and fo
21687 72 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20  r the cookie to 
21688 62 65 0a 2a 2a 20 73 65 74 20 62 61 63 6b 20 74  be.** set back t
21689 6f 20 70 72 69 6f 72 20 76 61 6c 75 65 2e 20 20  o prior value.  
2168a 42 75 74 20 73 63 68 65 6d 61 20 63 68 61 6e 67  But schema chang
2168b 65 73 20 61 72 65 20 69 6e 66 72 65 71 75 65 6e  es are infrequen
2168c 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 6f  t.** and the pro
2168d 62 61 62 69 6c 69 74 79 20 6f 66 20 68 69 74 74  bability of hitt
2168e 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 6f 6f  ing the same coo
2168f 6b 69 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 6c  kie value is onl
21690 79 0a 2a 2a 20 31 20 63 68 61 6e 63 65 20 69 6e  y.** 1 chance in
21691 20 32 5e 33 32 2e 20 20 53 6f 20 77 65 27 72 65   2^32.  So we're
21692 20 73 61 66 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f   safe enough..*/
21693 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21694 76 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e  void sqlite3Chan
21695 67 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 20 2a  geCookie(Parse *
21696 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29  pParse, int iDb)
21697 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c  {.  int r1 = sql
21698 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
21699 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
2169a 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2169b 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  db;.  Vdbe *v = 
2169c 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2169d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2169e 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2169f 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  , db->aDb[iDb].p
216a0 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
216a1 6f 6f 6b 69 65 2b 31 2c 20 72 31 29 3b 0a 20 20  ookie+1, r1);.  
216a2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
216a3 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69  3(v, OP_SetCooki
216a4 65 2c 20 69 44 62 2c 20 30 2c 20 72 31 29 3b 0a  e, iDb, 0, r1);.
216a5 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
216a6 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
216a7 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65  r1);.}../*.** Me
216a8 61 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72  asure the number
216a9 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 6e   of characters n
216aa 65 65 64 65 64 20 74 6f 20 6f 75 74 70 75 74 20  eeded to output 
216ab 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 69 64 65  the given.** ide
216ac 6e 74 69 66 69 65 72 2e 20 20 54 68 65 20 6e 75  ntifier.  The nu
216ad 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 69 6e  mber returned in
216ae 63 6c 75 64 65 73 20 61 6e 79 20 71 75 6f 74 65  cludes any quote
216af 73 20 75 73 65 64 0a 2a 2a 20 62 75 74 20 64 6f  s used.** but do
216b0 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74  es not include t
216b1 68 65 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74  he null terminat
216b2 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73  or..**.** The es
216b3 74 69 6d 61 74 65 20 69 73 20 63 6f 6e 73 65 72  timate is conser
216b4 76 61 74 69 76 65 2e 20 20 49 74 20 6d 69 67 68  vative.  It migh
216b5 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 74  t be larger that
216b6 20 77 68 61 74 20 69 73 0a 2a 2a 20 72 65 61 6c   what is.** real
216b7 6c 79 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74  ly needed..*/.st
216b8 61 74 69 63 20 69 6e 74 20 69 64 65 6e 74 4c 65  atic int identLe
216b9 6e 67 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20  ngth(const char 
216ba 2a 7a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  *z){.  int n;.  
216bb 66 6f 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b  for(n=0; *z; n++
216bc 2c 20 7a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , z++){.    if( 
216bd 2a 7a 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b 20  *z=='"' ){ n++; 
216be 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  }.  }.  return n
216bf 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   + 2;.}../*.** T
216c0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
216c1 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64  a wrapper around
216c2 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
216c3 28 29 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 69  () used by .** i
216c4 73 56 61 6c 69 64 44 69 6d 65 6e 73 69 6f 6e 28  sValidDimension(
216c5 29 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ). This function
216c6 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 73 71   differs from sq
216c7 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 29 20  lite3GetToken() 
216c8 69 6e 0a 2a 2a 20 74 68 61 74 3a 0a 2a 2a 0a 2a  in.** that:.**.*
216c9 2a 20 20 20 2a 20 57 68 69 74 65 73 70 61 63 65  *   * Whitespace
216ca 20 69 73 20 69 67 6e 6f 72 65 64 2c 20 61 6e 64   is ignored, and
216cb 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6f 75 74 70  .**   * The outp
216cc 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70 65 54  ut variable *peT
216cd 6f 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 30  oken is set to 0
216ce 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   if the end of t
216cf 68 65 0a 2a 2a 20 20 20 20 20 6e 75 6c 2d 74 65  he.**     nul-te
216d0 72 6d 69 6e 61 74 65 64 20 69 6e 70 75 74 20 73  rminated input s
216d1 74 72 69 6e 67 20 69 73 20 72 65 61 63 68 65 64  tring is reached
216d2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
216d3 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65 28  getTokenNoSpace(
216d4 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
216d5 2c 20 69 6e 74 20 2a 70 65 54 6f 6b 65 6e 29 7b  , int *peToken){
216d6 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20  .  int n = 0;.  
216d7 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
216d8 73 70 61 63 65 28 7a 5b 6e 5d 29 20 29 20 6e 2b  space(z[n]) ) n+
216d9 2b 3b 0a 20 20 69 66 28 20 21 7a 5b 6e 5d 20 29  +;.  if( !z[n] )
216da 7b 0a 20 20 20 20 2a 70 65 54 6f 6b 65 6e 20 3d  {.    *peToken =
216db 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   0;.    return 0
216dc 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
216dd 20 2b 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b   + sqlite3GetTok
216de 65 6e 28 26 7a 5b 6e 5d 2c 20 70 65 54 6f 6b 65  en(&z[n], peToke
216df 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  n);.}../*.** Par
216e0 61 6d 65 74 65 72 20 7a 20 70 6f 69 6e 74 73 20  ameter z points 
216e1 74 6f 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  to a nul-termina
216e2 74 65 64 20 73 74 72 69 6e 67 2e 20 52 65 74 75  ted string. Retu
216e3 72 6e 20 74 72 75 65 20 69 66 2c 20 77 68 65 6e  rn true if, when
216e4 0a 2a 2a 20 77 68 69 74 65 73 70 61 63 65 20 69  .** whitespace i
216e5 73 20 69 67 6e 6f 72 65 64 2c 20 74 68 65 20 63  s ignored, the c
216e6 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73 20  ontents of this 
216e7 73 74 72 69 6e 67 20 6d 61 74 63 68 65 73 20 6f  string matches o
216e8 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 66 6f  ne of .** the fo
216e9 6c 6c 6f 77 69 6e 67 20 70 61 74 74 65 72 6e 73  llowing patterns
216ea 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 22 0a 2a  :.**.**     "".*
216eb 2a 20 20 20 20 20 22 28 6e 75 6d 62 65 72 29 22  *     "(number)"
216ec 0a 2a 2a 20 20 20 20 20 22 28 6e 75 6d 62 65 72  .**     "(number
216ed 2c 6e 75 6d 62 65 72 29 22 0a 2a 2f 0a 73 74 61  ,number)".*/.sta
216ee 74 69 63 20 69 6e 74 20 69 73 56 61 6c 69 64 44  tic int isValidD
216ef 69 6d 65 6e 73 69 6f 6e 28 75 6e 73 69 67 6e 65  imension(unsigne
216f0 64 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e  d char *z){.  in
216f1 74 20 65 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20  t eToken;.  int 
216f2 6e 20 3d 20 30 3b 0a 20 20 6e 20 2b 3d 20 67 65  n = 0;.  n += ge
216f3 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65 28 26 7a  tTokenNoSpace(&z
216f4 5b 6e 5d 2c 20 26 65 54 6f 6b 65 6e 29 3b 0a 20  [n], &eToken);. 
216f5 20 69 66 28 20 65 54 6f 6b 65 6e 20 29 7b 0a 20   if( eToken ){. 
216f6 20 20 20 69 66 28 20 65 54 6f 6b 65 6e 21 3d 54     if( eToken!=T
216f7 4b 5f 4c 50 20 29 20 72 65 74 75 72 6e 20 30 3b  K_LP ) return 0;
216f8 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b  .    n += getTok
216f9 65 6e 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c  enNoSpace(&z[n],
216fa 20 26 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 69   &eToken);.    i
216fb 66 28 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f 50 4c  f( eToken==TK_PL
216fc 55 53 20 7c 7c 20 65 54 6f 6b 65 6e 3d 3d 54 4b  US || eToken==TK
216fd 5f 4d 49 4e 55 53 20 29 7b 0a 20 20 20 20 20 20  _MINUS ){.      
216fe 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e 6f 53  n += getTokenNoS
216ff 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f  pace(&z[n], &eTo
21700 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ken);.    }.    
21701 69 66 28 20 65 54 6f 6b 65 6e 21 3d 54 4b 5f 49  if( eToken!=TK_I
21702 4e 54 45 47 45 52 20 26 26 20 65 54 6f 6b 65 6e  NTEGER && eToken
21703 21 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 72 65 74  !=TK_FLOAT ) ret
21704 75 72 6e 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20  urn 0;.    n += 
21705 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65 28  getTokenNoSpace(
21706 26 7a 5b 6e 5d 2c 20 26 65 54 6f 6b 65 6e 29 3b  &z[n], &eToken);
21707 0a 20 20 20 20 69 66 28 20 65 54 6f 6b 65 6e 3d  .    if( eToken=
21708 3d 54 4b 5f 43 4f 4d 4d 41 20 29 7b 0a 20 20 20  =TK_COMMA ){.   
21709 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e     n += getToken
2170a 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26  NoSpace(&z[n], &
2170b 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 69  eToken);.      i
2170c 66 28 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f 50 4c  f( eToken==TK_PL
2170d 55 53 20 7c 7c 20 65 54 6f 6b 65 6e 3d 3d 54 4b  US || eToken==TK
2170e 5f 4d 49 4e 55 53 20 29 7b 0a 20 20 20 20 20 20  _MINUS ){.      
2170f 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e    n += getTokenN
21710 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65  oSpace(&z[n], &e
21711 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  Token);.      }.
21712 20 20 20 20 20 20 69 66 28 20 65 54 6f 6b 65 6e        if( eToken
21713 21 3d 54 4b 5f 49 4e 54 45 47 45 52 20 26 26 20  !=TK_INTEGER && 
21714 65 54 6f 6b 65 6e 21 3d 54 4b 5f 46 4c 4f 41 54  eToken!=TK_FLOAT
21715 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
21716 20 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e     n += getToken
21717 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26  NoSpace(&z[n], &
21718 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20  eToken);.    }. 
21719 20 20 20 69 66 28 20 65 54 6f 6b 65 6e 21 3d 54     if( eToken!=T
2171a 4b 5f 52 50 20 29 20 72 65 74 75 72 6e 20 30 3b  K_RP ) return 0;
2171b 0a 20 20 20 20 67 65 74 54 6f 6b 65 6e 4e 6f 53  .    getTokenNoS
2171c 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f  pace(&z[n], &eTo
2171d 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ken);.  }.  if( 
2171e 65 54 6f 6b 65 6e 20 29 20 72 65 74 75 72 6e 20  eToken ) return 
2171f 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  0;.  return 1;.}
21720 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ../*.** The firs
21721 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  t parameter is a
21722 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f   pointer to an o
21723 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 54 68  utput buffer. Th
21724 65 20 73 65 63 6f 6e 64 20 0a 2a 2a 20 70 61 72  e second .** par
21725 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e  ameter is a poin
21726 74 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65  ter to an intege
21727 72 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  r that contains 
21728 74 68 65 20 6f 66 66 73 65 74 20 61 74 0a 2a 2a  the offset at.**
21729 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20   which to write 
2172a 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20  into the output 
2172b 62 75 66 66 65 72 2e 20 54 68 69 73 20 66 75 6e  buffer. This fun
2172c 63 74 69 6f 6e 20 63 6f 70 69 65 73 20 74 68 65  ction copies the
2172d 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  .** nul-terminat
2172e 65 64 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65  ed string pointe
2172f 64 20 74 6f 20 62 79 20 74 68 65 20 74 68 69 72  d to by the thir
21730 64 20 70 61 72 61 6d 65 74 65 72 2c 20 7a 53 69  d parameter, zSi
21731 67 6e 65 64 49 64 65 6e 74 2c 0a 2a 2a 20 74 6f  gnedIdent,.** to
21732 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6f   the specified o
21733 66 66 73 65 74 20 69 6e 20 74 68 65 20 62 75 66  ffset in the buf
21734 66 65 72 20 61 6e 64 20 75 70 64 61 74 65 73 20  fer and updates 
21735 2a 70 49 64 78 20 74 6f 20 72 65 66 65 72 0a 2a  *pIdx to refer.*
21736 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62  * to the first b
21737 79 74 65 20 61 66 74 65 72 20 74 68 65 20 6c 61  yte after the la
21738 73 74 20 62 79 74 65 20 77 72 69 74 74 65 6e 20  st byte written 
21739 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
2173a 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20  ..** .** If the 
2173b 73 74 72 69 6e 67 20 7a 53 69 67 6e 65 64 49 64  string zSignedId
2173c 65 6e 74 20 63 6f 6e 73 69 73 74 73 20 65 6e 74  ent consists ent
2173d 69 72 65 6c 79 20 6f 66 20 61 6c 70 68 61 2d 6e  irely of alpha-n
2173e 75 6d 65 72 69 63 0a 2a 2a 20 63 68 61 72 61 63  umeric.** charac
2173f 74 65 72 73 2c 20 64 6f 65 73 20 6e 6f 74 20 62  ters, does not b
21740 65 67 69 6e 20 77 69 74 68 20 61 20 64 69 67 69  egin with a digi
21741 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 6e 20  t and is not an 
21742 53 51 4c 20 6b 65 79 77 6f 72 64 2c 0a 2a 2a 20  SQL keyword,.** 
21743 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65  then it is copie
21744 64 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20  d to the output 
21745 62 75 66 66 65 72 20 65 78 61 63 74 6c 79 20 61  buffer exactly a
21746 73 20 69 74 20 69 73 2e 20 4f 74 68 65 72 77 69  s it is. Otherwi
21747 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 71 75 6f  se,.** it is quo
21748 74 65 64 20 75 73 69 6e 67 20 64 6f 75 62 6c 65  ted using double
21749 2d 71 75 6f 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  -quotes..*/.stat
2174a 69 63 20 76 6f 69 64 20 69 64 65 6e 74 50 75 74  ic void identPut
2174b 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70  (char *z, int *p
2174c 49 64 78 2c 20 63 68 61 72 20 2a 7a 53 69 67 6e  Idx, char *zSign
2174d 65 64 49 64 65 6e 74 2c 20 69 6e 74 20 69 73 54  edIdent, int isT
2174e 79 70 65 6e 61 6d 65 29 7b 0a 20 20 75 6e 73 69  ypename){.  unsi
2174f 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 64 65 6e  gned char *zIden
21750 74 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  t = (unsigned ch
21751 61 72 2a 29 7a 53 69 67 6e 65 64 49 64 65 6e 74  ar*)zSignedIdent
21752 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 65  ;.  int i, j, ne
21753 65 64 51 75 6f 74 65 3b 0a 20 20 69 20 3d 20 2a  edQuote;.  i = *
21754 70 49 64 78 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30  pIdx;..  for(j=0
21755 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b  ; zIdent[j]; j++
21756 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69  ){.    if( !sqli
21757 74 65 33 49 73 61 6c 6e 75 6d 28 7a 49 64 65 6e  te3Isalnum(zIden
21758 74 5b 6a 5d 29 20 26 26 20 7a 49 64 65 6e 74 5b  t[j]) && zIdent[
21759 6a 5d 21 3d 27 5f 27 20 29 20 62 72 65 61 6b 3b  j]!='_' ) break;
2175a 0a 20 20 7d 0a 20 20 6e 65 65 64 51 75 6f 74 65  .  }.  needQuote
2175b 20 3d 20 73 71 6c 69 74 65 33 49 73 64 69 67 69   = sqlite3Isdigi
2175c 74 28 7a 49 64 65 6e 74 5b 30 5d 29 20 7c 7c 20  t(zIdent[0]) || 
2175d 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f  sqlite3KeywordCo
2175e 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54  de(zIdent, j)!=T
2175f 4b 5f 49 44 3b 0a 20 20 69 66 28 20 21 6e 65 65  K_ID;.  if( !nee
21760 64 51 75 6f 74 65 20 29 7b 0a 20 20 20 20 69 66  dQuote ){.    if
21761 28 20 69 73 54 79 70 65 6e 61 6d 65 20 29 7b 0a  ( isTypename ){.
21762 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
21763 20 69 73 20 61 20 74 79 70 65 2d 6e 61 6d 65 2c   is a type-name,
21764 20 61 6c 6c 6f 77 20 61 20 6c 69 74 74 6c 65 20   allow a little 
21765 6d 6f 72 65 20 66 6c 65 78 69 62 69 6c 69 74 79  more flexibility
21766 2e 20 49 6e 20 53 51 4c 69 74 65 2c 0a 20 20 20  . In SQLite,.   
21767 20 20 20 2a 2a 20 61 20 74 79 70 65 2d 6e 61 6d     ** a type-nam
21768 65 20 69 73 20 73 70 65 63 69 66 69 65 64 20 61  e is specified a
21769 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  s:.      **.    
2176a 20 20 2a 2a 20 20 20 69 64 73 20 5b 69 64 73 5d    **   ids [ids]
2176b 20 5b 28 6e 75 6d 62 65 72 20 5b 2c 20 6e 75 6d   [(number [, num
2176c 62 65 72 5d 29 5d 0a 20 20 20 20 20 20 2a 2a 0a  ber])].      **.
2176d 20 20 20 20 20 20 2a 2a 20 77 68 65 72 65 20 22        ** where "
2176e 69 64 73 22 20 69 73 20 65 69 74 68 65 72 20 61  ids" is either a
2176f 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 6f   quoted string o
21770 72 20 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74  r a simple ident
21771 69 66 69 65 72 20 28 69 6e 20 74 68 65 0a 20 20  ifier (in the.  
21772 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 6e 6f 74      ** above not
21773 61 74 69 6f 6e 2c 20 5b 5d 20 6d 65 61 6e 73 20  ation, [] means 
21774 6f 70 74 69 6f 6e 61 6c 29 2e 20 49 74 20 69 73  optional). It is
21775 20 61 20 62 69 74 20 74 72 69 63 6b 79 20 74 6f   a bit tricky to
21776 20 63 68 65 63 6b 0a 20 20 20 20 20 20 2a 2a 20   check.      ** 
21777 66 6f 72 20 61 6c 6c 20 63 61 73 65 73 2c 20 62  for all cases, b
21778 75 74 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f  ut it is good to
21779 20 61 76 6f 69 64 20 75 6e 6e 65 63 65 73 73 61   avoid unnecessa
2177a 72 69 6c 79 20 71 75 6f 74 69 6e 67 20 63 6f 6d  rily quoting com
2177b 6d 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79 70  mon.      ** typ
2177c 65 6e 61 6d 65 73 20 6c 69 6b 65 20 56 41 52 43  enames like VARC
2177d 48 41 52 28 31 30 29 2e 0a 20 20 20 20 20 20 2a  HAR(10)..      *
2177e 2f 0a 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74  /.      needQuot
2177f 65 20 3d 20 21 69 73 56 61 6c 69 64 44 69 6d 65  e = !isValidDime
21780 6e 73 69 6f 6e 28 26 7a 49 64 65 6e 74 5b 6a 5d  nsion(&zIdent[j]
21781 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
21782 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20      needQuote = 
21783 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 7d  zIdent[j];.    }
21784 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 65 65 64  .  }..  if( need
21785 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d  Quote ) z[i++] =
21786 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b   '"';.  for(j=0;
21787 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29   zIdent[j]; j++)
21788 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a  {.    z[i++] = z
21789 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66  Ident[j];.    if
2178a 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27  ( zIdent[j]=='"'
2178b 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b   ) z[i++] = '"';
2178c 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51  .  }.  if( needQ
2178d 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20  uote ) z[i++] = 
2178e 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b  '"';.  z[i] = 0;
2178f 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a  .  *pIdx = i;.}.
21790 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
21791 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
21792 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72  tatement appropr
21793 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 69 76  iate for the giv
21794 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65  en.** table.  Me
21795 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
21796 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 74 61   text of the sta
21797 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e  tement is obtain
21798 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  ed.** from sqlit
21799 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75  eMalloc() and mu
2179a 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  st be freed by t
2179b 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
2179c 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ion..*/.static c
2179d 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65  har *createTable
2179e 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Stmt(sqlite3 *db
2179f 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69  , Table *p){.  i
217a0 6e 74 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68  nt i, k, n;.  ch
217a1 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61  ar *zStmt;.  cha
217a2 72 20 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c  r *zSep, *zSep2,
217a3 20 2a 7a 45 6e 64 2c 20 2a 7a 3b 0a 20 20 43 6f   *zEnd, *z;.  Co
217a4 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20  lumn *pCol;.  n 
217a5 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 20  = 0;.  for(pCol 
217a6 3d 20 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20  = p->aCol, i=0; 
217a7 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20  i<p->nCol; i++, 
217a8 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b  pCol++){.    n +
217a9 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 43  = identLength(pC
217aa 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ol->zName);.    
217ab 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b  z = pCol->zType;
217ac 0a 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20  .    if( z ){.  
217ad 20 20 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65      n += identLe
217ae 6e 67 74 68 28 7a 29 3b 0a 20 20 20 20 7d 0a 20  ngth(z);.    }. 
217af 20 7d 0a 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c   }.  n += identL
217b0 65 6e 67 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b  ength(p->zName);
217b1 0a 20 20 69 66 28 20 6e 3c 35 30 20 29 7b 0a 20  .  if( n<50 ){. 
217b2 20 20 20 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20     zSep = "";.  
217b3 20 20 7a 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20    zSep2 = ",";. 
217b4 20 20 20 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20     zEnd = ")";. 
217b5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70   }else{.    zSep
217b6 20 3d 20 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a   = "\n  ";.    z
217b7 53 65 70 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a  Sep2 = ",\n  ";.
217b8 20 20 20 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22      zEnd = "\n)"
217b9 3b 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20  ;.  }.  n += 35 
217ba 2b 20 36 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a  + 6*p->nCol;.  z
217bb 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 61  Stmt = sqlite3Ma
217bc 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 69 66 28  lloc( n );.  if(
217bd 20 7a 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   zStmt==0 ){.   
217be 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
217bf 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  d = 1;.    retur
217c0 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  n 0;.  }.  sqlit
217c1 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a  e3_snprintf(n, z
217c2 53 74 6d 74 2c 20 22 43 52 45 41 54 45 20 54 41  Stmt, "CREATE TA
217c3 42 4c 45 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71  BLE ");.  k = sq
217c4 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 53  lite3Strlen30(zS
217c5 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50 75 74  tmt);.  identPut
217c6 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a  (zStmt, &k, p->z
217c7 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7a 53 74 6d  Name, 0);.  zStm
217c8 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20 20  t[k++] = '(';.  
217c9 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c  for(pCol=p->aCol
217ca 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c  , i=0; i<p->nCol
217cb 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
217cc 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
217cd 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74  intf(n-k, &zStmt
217ce 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20 20  [k], zSep);.    
217cf 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  k += sqlite3Strl
217d0 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b  en30(&zStmt[k]);
217d1 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a 53 65 70  .    zSep = zSep
217d2 32 3b 0a 20 20 20 20 69 64 65 6e 74 50 75 74 28  2;.    identPut(
217d3 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d  zStmt, &k, pCol-
217d4 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
217d5 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c 2d 3e 7a  if( (z = pCol->z
217d6 54 79 70 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Type)!=0 ){.    
217d7 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27    zStmt[k++] = '
217d8 20 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ';.      assert
217d9 28 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33 53  ( (int)(sqlite3S
217da 74 72 6c 65 6e 33 30 28 7a 29 2b 6b 2b 31 29 3c  trlen30(z)+k+1)<
217db 3d 6e 20 29 3b 0a 20 20 20 20 20 20 69 64 65 6e  =n );.      iden
217dc 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20  tPut(zStmt, &k, 
217dd 7a 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  z, 1);.    }.  }
217de 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
217df 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b  ntf(n-k, &zStmt[
217e0 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 3b  k], "%s", zEnd);
217e1 0a 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 3b  .  return zStmt;
217e2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
217e3 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
217e4 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 66   to report the f
217e5 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 65  inal ")" that te
217e6 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 52  rminates.** a CR
217e7 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
217e8 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
217e9 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  table structure 
217ea 74 68 61 74 20 6f 74 68 65 72 20 61 63 74 69 6f  that other actio
217eb 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20  n routines have 
217ec 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a 2a  been building.**
217ed 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65   is added to the
217ee 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74   internal hash t
217ef 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67 20  ables, assuming 
217f0 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a 2a  no errors have.*
217f1 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a  * occurred..**.*
217f2 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20 74  * An entry for t
217f3 68 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64 65  he table is made
217f4 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 74   in the master t
217f5 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75 6e  able on disk, un
217f6 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73 20  less.** this is 
217f7 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
217f8 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62 75  e or db->init.bu
217f9 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62 2d  sy==1.  When db-
217fa 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a  >init.busy==1.**
217fb 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65   it means we are
217fc 20 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c   reading the sql
217fd 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
217fe 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74   because we just
217ff 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f  .** connected to
21800 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72   the database or
21801 20 62 65 63 61 75 73 65 20 74 68 65 20 73 71 6c   because the sql
21802 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
21803 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c 79   has.** recently
21804 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 65   changed, so the
21805 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20   entry for this 
21806 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78  table already ex
21807 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73  ists in.** the s
21808 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
21809 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77  le.  We do not w
2180a 61 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 74  ant to create it
2180b 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66   again..**.** If
2180c 20 74 68 65 20 70 53 65 6c 65 63 74 20 61 72 67   the pSelect arg
2180d 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c  ument is not NUL
2180e 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  L, it means that
2180f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
21810 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 63   was called to c
21811 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 67 65  reate a table ge
21812 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20 0a  nerated from a .
21813 2a 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45  ** "CREATE TABLE
21814 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20 2e   ... AS SELECT .
21815 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  .." statement.  
21816 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  The column names
21817 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 74   of.** the new t
21818 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68 20  able will match 
21819 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
2181a 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f  f the SELECT..*/
2181b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2181c 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54  void sqlite3EndT
2181d 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
2181e 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2181f 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  /* Parse context
21820 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f   */.  Token *pCo
21821 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ns,           /*
21822 20 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20 61   The ',' token a
21823 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 6f  fter the last co
21824 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20 20  lumn defn. */.  
21825 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20  Token *pEnd,    
21826 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
21827 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20 69  inal ')' token i
21828 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  n the CREATE TAB
21829 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  LE */.  Select *
2182a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 20 20  pSelect         
2182b 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 61  /* Select from a
2182c 20 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 20   "CREATE ... AS 
2182d 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 20  SELECT" */.){.  
2182e 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69  Table *p;.  sqli
2182f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
21830 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b  ->db;.  int iDb;
21831 0a 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d 30  ..  if( (pEnd==0
21832 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 20   && pSelect==0) 
21833 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  || pParse->nErr 
21834 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
21835 6c 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74 75  led ) {.    retu
21836 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 50  rn;.  }.  p = pP
21837 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
21838 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
21839 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28  turn;..  assert(
2183a 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
2183b 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a 0a  || !pSelect );..
2183c 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2183d 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
2183e 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23   p->pSchema);..#
2183f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21840 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 65  IT_CHECK.  /* Re
21841 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61  solve names in a
21842 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  ll CHECK constra
21843 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e  int expressions.
21844 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  .  */.  if( p->p
21845 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72 63  Check ){.    Src
21846 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20 20 20  List sSrc;      
21847 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21848 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f 72  Fake SrcList for
21849 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2184a 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f  le */.    NameCo
2184b 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20  ntext sNC;      
2184c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
2184d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 50  e context for pP
2184e 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
2184f 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  */..    memset(&
21850 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
21851 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  NC));.    memset
21852 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 6f  (&sSrc, 0, sizeo
21853 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73 53  f(sSrc));.    sS
21854 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20  rc.nSrc = 1;.   
21855 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d 65   sSrc.a[0].zName
21856 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20   = p->zName;.   
21857 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62 20   sSrc.a[0].pTab 
21858 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b  = p;.    sSrc.a[
21859 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b  0].iCursor = -1;
2185a 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20  .    sNC.pParse 
2185b 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e  = pParse;.    sN
2185c 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73 53  C.pSrcList = &sS
2185d 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43 68  rc;.    sNC.isCh
2185e 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66 28  eck = 1;.    if(
2185f 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
21860 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
21861 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 20  ->pCheck) ){.   
21862 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
21863 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  .  }.#endif /* !
21864 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
21865 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20  MIT_CHECK) */.. 
21866 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69   /* If the db->i
21867 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 69 74  nit.busy is 1 it
21868 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65   means we are re
21869 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f 66  ading the SQL of
2186a 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c 69  f the.  ** "sqli
2186b 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 73  te_master" or "s
2186c 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65  qlite_temp_maste
2186d 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20  r" table on the 
2186e 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64 6f  disk..  ** So do
2186f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68   not write to th
21870 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20 45  e disk again.  E
21871 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74 20  xtract the root 
21872 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a  page number.  **
21873 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 66   for the table f
21874 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69 74  rom the db->init
21875 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e 20  .newTnum field. 
21876 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65   (The page numbe
21877 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61  r.  ** should ha
21878 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 65 72  ve been put ther
21879 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 4f  e by the sqliteO
2187a 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29 0a  penCb routine.).
2187b 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69    */.  if( db->i
2187c 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
2187d 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e  p->tnum = db->in
2187e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d 0a  it.newTnum;.  }.
2187f 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e 69  .  /* If not ini
21880 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e 20  tializing, then 
21881 63 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 20  create a record 
21882 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c  for the new tabl
21883 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53 51  e.  ** in the SQ
21884 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
21885 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
21886 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  e..  **.  ** If 
21887 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f 52  this is a TEMPOR
21888 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74 65  ARY table, write
21889 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20   the entry into 
2188a 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20  the auxiliary.  
2188b 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20  ** file instead 
2188c 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e  of into the main
2188d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
2188e 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e    */.  if( !db->
2188f 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
21890 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62 65   int n;.    Vdbe
21891 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a   *v;.    char *z
21892 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69 65  Type;    /* "vie
21893 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a 2f  w" or "table" */
21894 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65  .    char *zType
21895 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20 6f  2;   /* "VIEW" o
21896 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20 20  r "TABLE" */.   
21897 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20 20   char *zStmt;   
21898 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
21899 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20  CREATE TABLE or 
2189a 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74  CREATE VIEW stat
2189b 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76 20  ement */..    v 
2189c 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2189d 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
2189e 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
2189f 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
218a0 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
218a1 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a  ose, 0);..    /*
218a2 20 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69 61 6c   .    ** Initial
218a3 69 7a 65 20 7a 54 79 70 65 20 66 6f 72 20 74 68  ize zType for th
218a4 65 20 6e 65 77 20 76 69 65 77 20 6f 72 20 74 61  e new view or ta
218a5 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ble..    */.    
218a6 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d 3d  if( p->pSelect==
218a7 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  0 ){.      /* A 
218a8 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20 2a 2f  regular table */
218a9 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22  .      zType = "
218aa 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a 54  table";.      zT
218ab 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22 3b 0a  ype2 = "TABLE";.
218ac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
218ad 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 6c  MIT_VIEW.    }el
218ae 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 76  se{.      /* A v
218af 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79  iew */.      zTy
218b0 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 20  pe = "view";.   
218b1 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56 49 45     zType2 = "VIE
218b2 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  W";.#endif.    }
218b3 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
218b4 20 69 73 20 61 20 43 52 45 41 54 45 20 54 41 42   is a CREATE TAB
218b5 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43 54 20  LE xx AS SELECT 
218b6 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68 65  ..., execute the
218b7 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 73   SELECT.    ** s
218b8 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 75  tatement to popu
218b9 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74 61 62  late the new tab
218ba 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 67  le. The root-pag
218bb 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  e number for the
218bc 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 6c  .    ** new tabl
218bd 65 20 69 73 20 69 6e 20 72 65 67 69 73 74 65 72  e is in register
218be 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74   pParse->regRoot
218bf 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
218c0 4f 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54 20  Once the SELECT 
218c1 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20 62  has been coded b
218c2 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  y sqlite3Select(
218c3 29 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20 20  ), it is in a.  
218c4 20 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73 74    ** suitable st
218c5 61 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f 72  ate to query for
218c6 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
218c7 73 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20 62  s and types to b
218c8 65 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62 79  e used.    ** by
218c9 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a   the new table..
218ca 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20      **.    ** A 
218cb 73 68 61 72 65 64 2d 63 61 63 68 65 20 77 72 69  shared-cache wri
218cc 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72  te-lock is not r
218cd 65 71 75 69 72 65 64 20 74 6f 20 77 72 69 74 65  equired to write
218ce 20 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62 6c   to the new tabl
218cf 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 73  e,.    ** as a s
218d0 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74 20  chema-lock must 
218d1 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
218d2 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63 72  n obtained to cr
218d3 65 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a 20  eate it. Since. 
218d4 20 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d 6c     ** a schema-l
218d5 6f 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c 6c  ock excludes all
218d6 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20   other database 
218d7 75 73 65 72 73 2c 20 74 68 65 20 77 72 69 74 65  users, the write
218d8 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20 20  -lock would.    
218d9 2a 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74 2e  ** be redundant.
218da 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
218db 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
218dc 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
218dd 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  ;.      Table *p
218de 53 65 6c 54 61 62 3b 0a 0a 20 20 20 20 20 20 61  SelTab;..      a
218df 73 73 65 72 74 28 70 50 61 72 73 65 2d 3e 6e 54  ssert(pParse->nT
218e0 61 62 3d 3d 31 29 3b 0a 20 20 20 20 20 20 73 71  ab==1);.      sq
218e1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
218e2 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c  v, OP_OpenWrite,
218e3 20 31 2c 20 70 50 61 72 73 65 2d 3e 72 65 67 52   1, pParse->regR
218e4 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  oot, iDb);.     
218e5 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
218e6 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 20 20  geP5(v, 1);.    
218e7 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d    pParse->nTab =
218e8 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   2;.      sqlite
218e9 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
218ea 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65  &dest, SRT_Table
218eb 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
218ec 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
218ed 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74  , pSelect, &dest
218ee 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
218ef 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
218f0 5f 43 6c 6f 73 65 2c 20 31 29 3b 0a 20 20 20 20  _Close, 1);.    
218f1 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
218f2 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rr==0 ){.       
218f3 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74   pSelTab = sqlit
218f4 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
218f5 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ect(pParse, pSel
218f6 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ect);.        if
218f7 28 20 70 53 65 6c 54 61 62 3d 3d 30 20 29 20 72  ( pSelTab==0 ) r
218f8 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 61  eturn;.        a
218f9 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d  ssert( p->aCol==
218fa 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  0 );.        p->
218fb 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  nCol = pSelTab->
218fc 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d  nCol;.        p-
218fd 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d  >aCol = pSelTab-
218fe 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70  >aCol;.        p
218ff 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30  SelTab->nCol = 0
21900 3b 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61  ;.        pSelTa
21901 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20  b->aCol = 0;.   
21902 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65       sqlite3Dele
21903 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29  teTable(pSelTab)
21904 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
21905 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  .    /* Compute 
21906 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78  the complete tex
21907 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  t of the CREATE 
21908 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20  statement */.   
21909 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
2190a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 72        zStmt = cr
2190b 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28 64 62  eateTableStmt(db
2190c 2c 20 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , p);.    }else{
2190d 0a 20 20 20 20 20 20 6e 20 3d 20 28 69 6e 74 29  .      n = (int)
2190e 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 73  (pEnd->z - pPars
2190f 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29  e->sNameToken.z)
21910 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74 6d   + 1;.      zStm
21911 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
21912 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20  tf(db, .        
21913 20 20 22 43 52 45 41 54 45 20 25 73 20 25 2e 2a    "CREATE %s %.*
21914 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 70  s", zType2, n, p
21915 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
21916 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  n.z.      );.   
21917 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f   }..    /* A slo
21918 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  t for the record
21919 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
2191a 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74  n allocated in t
2191b 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54  he .    ** SQLIT
2191c 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20  E_MASTER table. 
2191d 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f   We just need to
2191e 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f   update that slo
2191f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a  t with all.    *
21920 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f  * the informatio
21921 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65  n we've collecte
21922 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
21923 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
21924 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22  (pParse,.      "
21925 55 50 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20  UPDATE %Q.%s ". 
21926 20 20 20 20 20 20 20 20 22 53 45 54 20 74 79 70          "SET typ
21927 65 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25 51 2c  e='%s', name=%Q,
21928 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f   tbl_name=%Q, ro
21929 6f 74 70 61 67 65 3d 23 25 64 2c 20 73 71 6c 3d  otpage=#%d, sql=
2192a 25 51 20 22 0a 20 20 20 20 20 20 20 22 57 48 45  %Q ".       "WHE
2192b 52 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a 20  RE rowid=#%d",. 
2192c 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
2192d 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ].zName, SCHEMA_
2192e 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20  TABLE(iDb),.    
2192f 20 20 7a 54 79 70 65 2c 0a 20 20 20 20 20 20 70    zType,.      p
21930 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70  ->zName,.      p
21931 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70  ->zName,.      p
21932 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a  Parse->regRoot,.
21933 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20 20        zStmt,.   
21934 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f     pParse->regRo
21935 77 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 73  wid.    );.    s
21936 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
21937 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c   zStmt);.    sql
21938 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
21939 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a  (pParse, iDb);..
2193a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2193b 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
2193c 54 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74  T.    /* Check t
2193d 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 65 64  o see if we need
2193e 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 73 71   to create an sq
2193f 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
21940 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a 20 6b  ble for.    ** k
21941 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20  eeping track of 
21942 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6b 65  autoincrement ke
21943 79 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ys..    */.    i
21944 66 28 20 70 2d 3e 74 61 62 46 6c 61 67 73 20 26  f( p->tabFlags &
21945 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e   TF_Autoincremen
21946 74 20 29 7b 0a 20 20 20 20 20 20 44 62 20 2a 70  t ){.      Db *p
21947 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  Db = &db->aDb[iD
21948 62 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44  b];.      if( pD
21949 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71  b->pSchema->pSeq
2194a 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Tab==0 ){.      
2194b 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
2194c 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
2194d 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54         "CREATE T
2194e 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73  ABLE %Q.sqlite_s
2194f 65 71 75 65 6e 63 65 28 6e 61 6d 65 2c 73 65 71  equence(name,seq
21950 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 44  )",.          pD
21951 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 20  b->zName.       
21952 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
21953 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
21954 20 52 65 70 61 72 73 65 20 65 76 65 72 79 74 68   Reparse everyth
21955 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20 6f 75  ing to update ou
21956 72 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20  r internal data 
21957 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 20 20  structures */.  
21958 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21959 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53  Op4(v, OP_ParseS
2195a 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30  chema, iDb, 0, 0
2195b 2c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ,.        sqlite
2195c 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 62  3MPrintf(db, "tb
2195d 6c 5f 6e 61 6d 65 3d 27 25 71 27 22 2c 70 2d 3e  l_name='%q'",p->
2195e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d  zName), P4_DYNAM
2195f 49 43 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20  IC);.  }...  /* 
21960 41 64 64 20 74 68 65 20 74 61 62 6c 65 20 74 6f  Add the table to
21961 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72   the in-memory r
21962 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
21963 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20   the database.. 
21964 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   */.  if( db->in
21965 69 74 2e 62 75 73 79 20 26 26 20 70 50 61 72 73  it.busy && pPars
21966 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20  e->nErr==0 ){.  
21967 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20    Table *pOld;. 
21968 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20     FKey *pFKey; 
21969 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63  .    Schema *pSc
2196a 68 65 6d 61 20 3d 20 70 2d 3e 70 53 63 68 65 6d  hema = p->pSchem
2196b 61 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71  a;.    pOld = sq
2196c 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
2196d 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  &pSchema->tblHas
2196e 68 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  h, p->zName,.   
2196f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21970 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
21971 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 7a 4e 61  3Strlen30(p->zNa
21972 6d 65 29 2b 31 2c 70 29 3b 0a 20 20 20 20 69 66  me)+1,p);.    if
21973 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20  ( pOld ){.      
21974 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 20  assert( p==pOld 
21975 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75  );  /* Malloc mu
21976 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 69  st have failed i
21977 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 74  nside HashInsert
21978 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e  () */.      db->
21979 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2197a 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
2197b 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
2197c 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
2197d 4e 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70 46  N_KEY.    for(pF
2197e 4b 65 79 3d 70 2d 3e 70 46 4b 65 79 3b 20 70 46  Key=p->pFKey; pF
2197f 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79  Key; pFKey=pFKey
21980 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20  ->pNextFrom){.  
21981 20 20 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 0a      void *data;.
21982 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20        int nTo = 
21983 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
21984 70 46 4b 65 79 2d 3e 7a 54 6f 29 20 2b 20 31 3b  pFKey->zTo) + 1;
21985 0a 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 70 4e  .      pFKey->pN
21986 65 78 74 54 6f 20 3d 20 73 71 6c 69 74 65 33 48  extTo = sqlite3H
21987 61 73 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61  ashFind(&pSchema
21988 2d 3e 61 46 4b 65 79 2c 20 70 46 4b 65 79 2d 3e  ->aFKey, pFKey->
21989 7a 54 6f 2c 20 6e 54 6f 29 3b 0a 20 20 20 20 20  zTo, nTo);.     
2198a 20 64 61 74 61 20 3d 20 73 71 6c 69 74 65 33 48   data = sqlite3H
2198b 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68 65  ashInsert(&pSche
2198c 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 4b 65 79  ma->aFKey, pFKey
2198d 2d 3e 7a 54 6f 2c 20 6e 54 6f 2c 20 70 46 4b 65  ->zTo, nTo, pFKe
2198e 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 61  y);.      if( da
2198f 74 61 3d 3d 28 76 6f 69 64 20 2a 29 70 46 4b 65  ta==(void *)pFKe
21990 79 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d  y ){.        db-
21991 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
21992 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
21993 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61 72  .#endif.    pPar
21994 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20  se->pNewTable = 
21995 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 54 61 62 6c  0;.    db->nTabl
21996 65 2b 2b 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61  e++;.    db->fla
21997 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
21998 65 72 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66  ernChanges;..#if
21999 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2199a 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 20  _ALTERTABLE.    
2199b 69 66 28 20 21 70 2d 3e 70 53 65 6c 65 63 74 20  if( !p->pSelect 
2199c 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
2199d 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f  har *zName = (co
2199e 6e 73 74 20 63 68 61 72 20 2a 29 70 50 61 72 73  nst char *)pPars
2199f 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b  e->sNameToken.z;
219a0 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65  .      int nName
219a1 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
219a2 21 70 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e  !pSelect && pCon
219a3 73 20 26 26 20 70 45 6e 64 20 29 3b 0a 20 20 20  s && pEnd );.   
219a4 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d     if( pCons->z=
219a5 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  =0 ){.        pC
219a6 6f 6e 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20  ons = pEnd;.    
219a7 20 20 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20    }.      nName 
219a8 3d 20 28 69 6e 74 29 28 28 63 6f 6e 73 74 20 63  = (int)((const c
219a9 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d  har *)pCons->z -
219aa 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70   zName);.      p
219ab 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d  ->addColOffset =
219ac 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66   13 + sqlite3Utf
219ad 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20  8CharLen(zName, 
219ae 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65  nName);.    }.#e
219af 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  ndif.  }.}..#ifn
219b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
219b1 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  VIEW./*.** The p
219b2 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
219b3 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65   routine in orde
219b4 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  r to create a ne
219b5 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45  w VIEW.*/.SQLITE
219b6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
219b7 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28  lite3CreateView(
219b8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
219b9 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72  ,     /* The par
219ba 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
219bb 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c    Token *pBegin,
219bc 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41       /* The CREA
219bd 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65  TE token that be
219be 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65  gins the stateme
219bf 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
219c0 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68  Name1,     /* Th
219c1 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c  e token that hol
219c2 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ds the name of t
219c3 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b  he view */.  Tok
219c4 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20  en *pName2,     
219c5 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61  /* The token tha
219c6 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  t holds the name
219c7 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a   of the view */.
219c8 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
219c9 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54  t,   /* A SELECT
219ca 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
219cb 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20  will become the 
219cc 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e  new view */.  in
219cd 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20  t isTemp,       
219ce 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54   /* TRUE for a T
219cf 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f  EMPORARY view */
219d0 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20  .  int noErr    
219d1 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73        /* Suppres
219d2 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  s error messages
219d3 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 79   if VIEW already
219d4 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20   exists */.){.  
219d5 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20  Table *p;.  int 
219d6 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  n;.  const unsig
219d7 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54  ned char *z;.  T
219d8 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46  oken sEnd;.  DbF
219d9 69 78 65 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b  ixer sFix;.  Tok
219da 65 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e 74  en *pName;.  int
219db 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 20   iDb;.  sqlite3 
219dc 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
219dd 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ;..  if( pParse-
219de 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 73  >nVar>0 ){.    s
219df 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
219e0 50 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74 65  Parse, "paramete
219e1 72 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77  rs are not allow
219e2 65 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a 20  ed in views");. 
219e3 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
219e4 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
219e5 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ct);.    return;
219e6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74  .  }.  sqlite3St
219e7 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c  artTable(pParse,
219e8 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
219e9 20 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e   isTemp, 1, 0, n
219ea 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 61  oErr);.  p = pPa
219eb 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
219ec 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50    if( p==0 || pP
219ed 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20  arse->nErr ){.  
219ee 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
219ef 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63  elete(db, pSelec
219f0 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
219f1 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 54 77 6f    }.  sqlite3Two
219f2 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
219f3 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
219f4 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20   &pName);.  iDb 
219f5 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
219f6 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53  oIndex(db, p->pS
219f7 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 73 71  chema);.  if( sq
219f8 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46  lite3FixInit(&sF
219f9 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c  ix, pParse, iDb,
219fa 20 22 76 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a   "view", pName).
219fb 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69      && sqlite3Fi
219fc 78 53 65 6c 65 63 74 28 26 73 46 69 78 2c 20 70  xSelect(&sFix, p
219fd 53 65 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20 20  Select).  ){.   
219fe 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
219ff 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
21a00 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
21a01 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20   }..  /* Make a 
21a02 63 6f 70 79 20 6f 66 20 74 68 65 20 65 6e 74 69  copy of the enti
21a03 72 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  re SELECT statem
21a04 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73  ent that defines
21a05 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2a 20   the view..  ** 
21a06 54 68 69 73 20 77 69 6c 6c 20 66 6f 72 63 65 20  This will force 
21a07 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b  all the Expr.tok
21a08 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20 62  en.z values to b
21a09 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20  e dynamically.  
21a0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 72 61 74  ** allocated rat
21a0b 68 65 72 20 74 68 61 6e 20 70 6f 69 6e 74 20 74  her than point t
21a0c 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69  o the input stri
21a0d 6e 67 20 2d 20 77 68 69 63 68 20 6d 65 61 6e 73  ng - which means
21a0e 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 20   that.  ** they 
21a0f 77 69 6c 6c 20 70 65 72 73 69 73 74 20 61 66 74  will persist aft
21a10 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  er the current s
21a11 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61  qlite3_exec() ca
21a12 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f  ll returns..  */
21a13 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20  .  p->pSelect = 
21a14 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
21a15 28 64 62 2c 20 70 53 65 6c 65 63 74 2c 20 45 58  (db, pSelect, EX
21a16 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20  PRDUP_REDUCE);. 
21a17 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
21a18 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
21a19 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
21a1a 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
21a1b 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
21a1c 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
21a1d 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  y ){.    sqlite3
21a1e 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
21a1f 65 73 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  es(pParse, p);. 
21a20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20   }..  /* Locate 
21a21 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43  the end of the C
21a22 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65  REATE VIEW state
21a23 6d 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64  ment.  Make sEnd
21a24 20 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74   point to.  ** t
21a25 68 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73  he end..  */.  s
21a26 45 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c  End = pParse->sL
21a27 61 73 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20  astToken;.  if( 
21a28 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30 20 26 26 20  sEnd.z[0]!=0 && 
21a29 73 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29  sEnd.z[0]!=';' )
21a2a 7b 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20  {.    sEnd.z += 
21a2b 73 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45  sEnd.n;.  }.  sE
21a2c 6e 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20  nd.n = 0;.  n = 
21a2d 28 69 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20 70  (int)(sEnd.z - p
21a2e 42 65 67 69 6e 2d 3e 7a 29 3b 0a 20 20 7a 20 3d  Begin->z);.  z =
21a2f 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
21a30 20 63 68 61 72 2a 29 70 42 65 67 69 6e 2d 3e 7a   char*)pBegin->z
21a31 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 26  ;.  while( n>0 &
21a32 26 20 28 7a 5b 6e 2d 31 5d 3d 3d 27 3b 27 20 7c  & (z[n-1]==';' |
21a33 7c 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  | sqlite3Isspace
21a34 28 7a 5b 6e 2d 31 5d 29 29 20 29 7b 20 6e 2d 2d  (z[n-1])) ){ n--
21a35 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20 26  ; }.  sEnd.z = &
21a36 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 2e 6e  z[n-1];.  sEnd.n
21a37 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 65 20   = 1;..  /* Use 
21a38 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28  sqlite3EndTable(
21a39 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 69 65  ) to add the vie
21a3a 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 45 5f  w to the SQLITE_
21a3b 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a 2f 0a  MASTER table */.
21a3c 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c    sqlite3EndTabl
21a3d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 73 45  e(pParse, 0, &sE
21a3e 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  nd, 0);.  return
21a3f 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
21a40 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a  LITE_OMIT_VIEW *
21a41 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
21a42 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
21a43 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
21a44 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
21a45 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 68  LTABLE)./*.** Th
21a46 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
21a47 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 61 6c  e pTable is real
21a48 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c 6c  ly a VIEW.  Fill
21a49 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66   in the names of
21a4a 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20  .** the columns 
21a4b 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e 20 74  of the view in t
21a4c 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 63 74  he pTable struct
21a4d 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ure.  Return the
21a4e 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65 72   number.** of er
21a4f 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72 72  rors.  If an err
21a50 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76 65  or is seen leave
21a51 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
21a52 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72  e in pParse->zEr
21a53 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rMsg..*/.SQLITE_
21a54 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21a55 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
21a56 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50 61  Names(Parse *pPa
21a57 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
21a58 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 53  le){.  Table *pS
21a59 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66 61  elTab;   /* A fa
21a5a 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68  ke table from wh
21a5b 69 63 68 20 77 65 20 67 65 74 20 74 68 65 20 72  ich we get the r
21a5c 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 53  esult set */.  S
21a5d 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 20  elect *pSel;    
21a5e 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65 20   /* Copy of the 
21a5f 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70 6c  SELECT that impl
21a60 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 20  ements the view 
21a61 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20  */.  int nErr = 
21a62 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  0;     /* Number
21a63 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75   of errors encou
21a64 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ntered */.  int 
21a65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
21a66 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 6f 6c   Temporarily hol
21a67 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ds the number of
21a68 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 6e 65   cursors assigne
21a69 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  d */.  sqlite3 *
21a6a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
21a6b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
21a6c 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d 61 6c  nnection for mal
21a6d 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  loc errors */.  
21a6e 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69  int (*xAuth)(voi
21a6f 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
21a70 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
21a71 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
21a72 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73 73 65   char*);..  asse
21a73 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 0a 23  rt( pTable );..#
21a74 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21a75 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
21a76 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 74 61    if( sqlite3Vta
21a77 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50 61  bCallConnect(pPa
21a78 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29 7b 0a  rse, pTable) ){.
21a79 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
21a7a 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69  E_ERROR;.  }.  i
21a7b 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
21a7c 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ble) ) return 0;
21a7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
21a7e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
21a7f 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 69 76  W.  /* A positiv
21a80 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68 65  e nCol means the
21a81 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 66   columns names f
21a82 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 72 65  or this view are
21a83 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6b 6e  .  ** already kn
21a84 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  own..  */.  if( 
21a85 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 20 29  pTable->nCol>0 )
21a86 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a   return 0;..  /*
21a87 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c   A negative nCol
21a88 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 6d 61   is a special ma
21a89 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 68 61  rker meaning tha
21a8a 74 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74  t we are current
21a8b 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74  ly.  ** trying t
21a8c 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 63 6f  o compute the co
21a8d 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 66 20  lumn names.  If 
21a8e 77 65 20 65 6e 74 65 72 20 74 68 69 73 20 72 6f  we enter this ro
21a8f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a 2a 20  utine with.  ** 
21a90 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c 2c  a negative nCol,
21a91 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 6f 72   it means two or
21a92 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f 72 6d   more views form
21a93 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 74 68   a loop, like th
21a94 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  is:.  **.  **   
21a95 20 20 43 52 45 41 54 45 20 56 49 45 57 20 6f 6e    CREATE VIEW on
21a96 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52  e AS SELECT * FR
21a97 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 20 20  OM two;.  **    
21a98 20 43 52 45 41 54 45 20 56 49 45 57 20 74 77 6f   CREATE VIEW two
21a99 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f   AS SELECT * FRO
21a9a 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a  M one;.  **.  **
21a9b 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 69 73 20   Actually, this 
21a9c 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74 20  error is caught 
21a9d 70 72 65 76 69 6f 75 73 6c 79 20 61 6e 64 20 73  previously and s
21a9e 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
21a9f 74 65 73 74 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  test.  ** should
21aa0 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 42   always fail.  B
21aa1 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65  ut we will leave
21aa2 20 69 74 20 69 6e 20 70 6c 61 63 65 20 6a 75 73   it in place jus
21aa3 74 20 74 6f 20 62 65 20 73 61 66 65 2e 0a 20 20  t to be safe..  
21aa4 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d  */.  if( pTable-
21aa5 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 73  >nCol<0 ){.    s
21aa6 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
21aa7 50 61 72 73 65 2c 20 22 76 69 65 77 20 25 73 20  Parse, "view %s 
21aa8 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20 64 65  is circularly de
21aa9 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 2d 3e  fined", pTable->
21aaa 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  zName);.    retu
21aab 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65  rn 1;.  }.  asse
21aac 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  rt( pTable->nCol
21aad 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  >=0 );..  /* If 
21aae 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c  we get this far,
21aaf 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e 65 65   it means we nee
21ab0 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65  d to compute the
21ab1 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a 20 20   table names..  
21ab2 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
21ab3 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
21ab4 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63  ResultSetOfSelec
21ab5 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e 64 20  t() will expand 
21ab6 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 6c 65  any.  ** "*" ele
21ab7 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72 65 73  ments in the res
21ab8 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68 65 20  ults set of the 
21ab9 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 61 73  view and will as
21aba 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 20 2a  sign cursors.  *
21abb 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e 74  * to the element
21abc 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  s of the FROM cl
21abd 61 75 73 65 2e 20 20 42 75 74 20 77 65 20 64 6f  ause.  But we do
21abe 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 65 20   not want these 
21abf 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20  changes.  ** to 
21ac0 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20 53  be permanent.  S
21ac1 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f  o the computatio
21ac2 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20 63  n is done on a c
21ac3 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43  opy of the SELEC
21ac4 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74  T.  ** statement
21ac5 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
21ac6 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 20 61  e view..  */.  a
21ac7 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70  ssert( pTable->p
21ac8 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 65 6c  Select );.  pSel
21ac9 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
21aca 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  Dup(db, pTable->
21acb 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69  pSelect, 0);.  i
21acc 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 75  f( pSel ){.    u
21acd 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  8 enableLookasid
21ace 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  e = db->lookasid
21acf 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20 20  e.bEnabled;.    
21ad0 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  n = pParse->nTab
21ad1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63  ;.    sqlite3Src
21ad2 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72  ListAssignCursor
21ad3 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e  s(pParse, pSel->
21ad4 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c  pSrc);.    pTabl
21ad5 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20  e->nCol = -1;.  
21ad6 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
21ad7 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23 69  bEnabled = 0;.#i
21ad8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
21ad9 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
21ada 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e      xAuth = db->
21adb 78 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78  xAuth;.    db->x
21adc 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53  Auth = 0;.    pS
21add 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52  elTab = sqlite3R
21ade 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74  esultSetOfSelect
21adf 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a  (pParse, pSel);.
21ae0 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
21ae1 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20  xAuth;.#else.   
21ae2 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74   pSelTab = sqlit
21ae3 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
21ae4 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ect(pParse, pSel
21ae5 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 62  );.#endif.    db
21ae6 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
21ae7 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f  bled = enableLoo
21ae8 6b 61 73 69 64 65 3b 0a 20 20 20 20 70 50 61 72  kaside;.    pPar
21ae9 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 20  se->nTab = n;.  
21aea 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29 7b    if( pSelTab ){
21aeb 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
21aec 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29  Table->aCol==0 )
21aed 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e  ;.      pTable->
21aee 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  nCol = pSelTab->
21aef 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 62  nCol;.      pTab
21af0 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54  le->aCol = pSelT
21af1 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20  ab->aCol;.      
21af2 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20  pSelTab->nCol = 
21af3 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62  0;.      pSelTab
21af4 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->aCol = 0;.    
21af5 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
21af6 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a 20  able(pSelTab);. 
21af7 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63       pTable->pSc
21af8 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 44  hema->flags |= D
21af9 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0a  B_UnresetViews;.
21afa 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21afb 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
21afc 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b  0;.      nErr++;
21afd 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
21afe 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
21aff 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65 6c  b, pSel);.  } el
21b00 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b  se {.    nErr++;
21b01 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
21b02 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20  QLITE_OMIT_VIEW 
21b03 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72  */.  return nErr
21b04 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;  .}.#endif /* 
21b05 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
21b06 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
21b07 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
21b08 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
21b09 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
21b0a 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a  ITE_OMIT_VIEW./*
21b0b 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f  .** Clear the co
21b0c 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20  lumn names from 
21b0d 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61  every VIEW in da
21b0e 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73  tabase idx..*/.s
21b0f 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
21b10 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71  eViewResetAll(sq
21b11 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
21b12 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  dx){.  HashElem 
21b13 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73  *i;.  if( !DbHas
21b14 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78  Property(db, idx
21b15 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  , DB_UnresetView
21b16 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  s) ) return;.  f
21b17 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46  or(i=sqliteHashF
21b18 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64  irst(&db->aDb[id
21b19 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  x].pSchema->tblH
21b1a 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65  ash); i;i=sqlite
21b1b 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
21b1c 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
21b1d 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69  sqliteHashData(i
21b1e 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  );.    if( pTab-
21b1f 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
21b20 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c    sqliteResetCol
21b21 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b 0a  umnNames(pTab);.
21b22 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c      }.  }.  DbCl
21b23 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20  earProperty(db, 
21b24 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56  idx, DB_UnresetV
21b25 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  iews);.}.#else.#
21b26 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69   define sqliteVi
21b27 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a  ewResetAll(A,B).
21b28 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
21b29 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f  _OMIT_VIEW */../
21b2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
21b2b 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  on is called by 
21b2c 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75  the VDBE to adju
21b2d 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  st the internal 
21b2e 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62  schema.** used b
21b2f 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68  y SQLite when th
21b30 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f  e btree layer mo
21b31 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74  ves a table root
21b32 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f   page. The.** ro
21b33 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62  ot-page of a tab
21b34 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64  le or index in d
21b35 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20  atabase iDb has 
21b36 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72  changed from iFr
21b37 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a  om.** to iTo..**
21b38 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38  .** Ticket #1728
21b39 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61  :  The symbol ta
21b3a 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20  ble might still 
21b3b 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74  contain informat
21b3c 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73  ion.** on tables
21b3d 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20   and/or indices 
21b3e 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f  that are the pro
21b3f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65  cess of being de
21b40 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75  leted..** If you
21b41 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e   are unlucky, on
21b42 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74  e of those delet
21b43 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61  ed indices or ta
21b44 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61  bles might.** ha
21b45 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74  ve the same root
21b46 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74  page number as t
21b47 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72  he real table or
21b48 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a   index that is.*
21b49 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20  * being moved.  
21b4a 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f  So we cannot sto
21b4b 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65  p searching afte
21b4c 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63  r the first matc
21b4d 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68  h .** because th
21b4e 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69  e first match mi
21b4f 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f  ght be for one o
21b50 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e  f the deleted in
21b51 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c  dices.** or tabl
21b52 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74  es and not the t
21b53 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20  able/index that 
21b54 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e  is actually bein
21b55 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d  g moved..** We m
21b56 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f  ust continue loo
21b57 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74  ping until all t
21b58 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65  ables and indice
21b59 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61  s with.** rootpa
21b5a 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62  ge==iFrom have b
21b5b 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f  een converted to
21b5c 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65   have a rootpage
21b5d 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72   of iTo.** in or
21b5e 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69  der to be certai
21b5f 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68  n that we got th
21b60 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a  e right one..*/.
21b61 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
21b62 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 53  MIT_AUTOVACUUM.S
21b63 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
21b64 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61  id sqlite3RootPa
21b65 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 2c  geMoved(Db *pDb,
21b66 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20   int iFrom, int 
21b67 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  iTo){.  HashElem
21b68 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20   *pElem;.  Hash 
21b69 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 68  *pHash;..  pHash
21b6a 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61   = &pDb->pSchema
21b6b 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72  ->tblHash;.  for
21b6c 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73  (pElem=sqliteHas
21b6d 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70  hFirst(pHash); p
21b6e 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69  Elem; pElem=sqli
21b6f 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d  teHashNext(pElem
21b70 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  )){.    Table *p
21b71 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Tab = sqliteHash
21b72 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20  Data(pElem);.   
21b73 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d   if( pTab->tnum=
21b74 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20  =iFrom ){.      
21b75 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f  pTab->tnum = iTo
21b76 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48  ;.    }.  }.  pH
21b77 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68  ash = &pDb->pSch
21b78 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20  ema->idxHash;.  
21b79 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65  for(pElem=sqlite
21b7a 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29  HashFirst(pHash)
21b7b 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73  ; pElem; pElem=s
21b7c 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45  qliteHashNext(pE
21b7d 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78  lem)){.    Index
21b7e 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48   *pIdx = sqliteH
21b7f 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a  ashData(pElem);.
21b80 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e      if( pIdx->tn
21b81 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  um==iFrom ){.   
21b82 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20     pIdx->tnum = 
21b83 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  iTo;.    }.  }.}
21b84 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
21b85 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61  rite code to era
21b86 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  se the table wit
21b87 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62  h root-page iTab
21b88 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65  le from database
21b89 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72   iDb..** Also wr
21b8a 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69  ite code to modi
21b8b 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  fy the sqlite_ma
21b8c 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69  ster table and i
21b8d 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a  nternal schema.*
21b8e 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65  * if a root-page
21b8f 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c   of another tabl
21b90 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68  e is moved by th
21b91 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68  e btree-layer wh
21b92 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20  ilst.** erasing 
21b93 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e  iTable (this can
21b94 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20   happen with an 
21b95 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
21b96 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69  base)..*/ .stati
21b97 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f  c void destroyRo
21b98 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50  otPage(Parse *pP
21b99 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65  arse, int iTable
21b9a 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64  , int iDb){.  Vd
21b9b 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
21b9c 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
21b9d 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
21b9e 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
21b9f 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  rse);.  sqlite3V
21ba0 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
21ba1 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c  Destroy, iTable,
21ba2 20 72 31 2c 20 69 44 62 29 3b 0a 23 69 66 6e 64   r1, iDb);.#ifnd
21ba3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
21ba4 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 4f  UTOVACUUM.  /* O
21ba5 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65 73  P_Destroy stores
21ba6 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20 72   an in integer r
21ba7 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65 67  1. If this integ
21ba8 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d 7a  er.  ** is non-z
21ba9 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ero, then it is 
21baa 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
21bab 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65 20  mber of a table 
21bac 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c 6f  moved to.  ** lo
21bad 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20 54  cation iTable. T
21bae 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  he following cod
21baf 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20 73  e modifies the s
21bb0 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
21bb1 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c 65  le to.  ** refle
21bb2 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20 20  ct this..  **.  
21bb3 2a 2a 20 54 68 65 20 22 23 4e 4e 4e 22 20 69 6e  ** The "#NNN" in
21bb4 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 73 70   the SQL is a sp
21bb5 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 74  ecial constant t
21bb6 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65 76  hat means whatev
21bb7 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69 73  er value.  ** is
21bb8 20 69 6e 20 72 65 67 69 73 74 65 72 20 4e 4e 4e   in register NNN
21bb9 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 52 65  .  See sqlite3Re
21bba 67 69 73 74 65 72 45 78 70 72 28 29 2e 0a 20 20  gisterExpr()..  
21bbb 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74  */.  sqlite3Nest
21bbc 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20  edParse(pParse, 
21bbd 0a 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51  .     "UPDATE %Q
21bbe 2e 25 73 20 53 45 54 20 72 6f 6f 74 70 61 67 65  .%s SET rootpage
21bbf 3d 25 64 20 57 48 45 52 45 20 23 25 64 20 41 4e  =%d WHERE #%d AN
21bc0 44 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 22 2c  D rootpage=#%d",
21bc1 0a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62  .     pParse->db
21bc2 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
21bc3 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
21bc4 44 62 29 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c  Db), iTable, r1,
21bc5 20 72 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73   r1);.#endif.  s
21bc6 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
21bc7 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
21bc8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
21bc9 20 56 44 42 45 20 63 6f 64 65 20 74 6f 20 65 72   VDBE code to er
21bca 61 73 65 20 74 61 62 6c 65 20 70 54 61 62 20 61  ase table pTab a
21bcb 6e 64 20 61 6c 6c 20 61 73 73 6f 63 69 61 74 65  nd all associate
21bcc 64 20 69 6e 64 69 63 65 73 20 6f 6e 20 64 69 73  d indices on dis
21bcd 6b 2e 0a 2a 2a 20 43 6f 64 65 20 74 6f 20 75 70  k..** Code to up
21bce 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f  date the sqlite_
21bcf 6d 61 73 74 65 72 20 74 61 62 6c 65 73 20 61 6e  master tables an
21bd0 64 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  d internal schem
21bd1 61 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a  a definitions.**
21bd2 20 69 6e 20 63 61 73 65 20 61 20 72 6f 6f 74 2d   in case a root-
21bd3 70 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74  page belonging t
21bd4 6f 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20  o another table 
21bd5 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68 65 20  is moved by the 
21bd6 62 74 72 65 65 20 6c 61 79 65 72 0a 2a 2a 20 69  btree layer.** i
21bd7 73 20 61 6c 73 6f 20 61 64 64 65 64 20 28 74 68  s also added (th
21bd8 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69  is can happen wi
21bd9 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  th an auto-vacuu
21bda 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a  m database)..*/.
21bdb 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 73 74  static void dest
21bdc 72 6f 79 54 61 62 6c 65 28 50 61 72 73 65 20 2a  royTable(Parse *
21bdd 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
21bde 54 61 62 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  Tab){.#ifdef SQL
21bdf 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
21be0 55 55 4d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64  UUM.  Index *pId
21be1 78 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 73  x;.  int iDb = s
21be2 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
21be3 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
21be4 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
21be5 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67    destroyRootPag
21be6 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e  e(pParse, pTab->
21be7 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 66 6f  tnum, iDb);.  fo
21be8 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
21be9 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
21bea 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
21beb 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67    destroyRootPag
21bec 65 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e  e(pParse, pIdx->
21bed 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d 0a  tnum, iDb);.  }.
21bee 23 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20 74 68  #else.  /* If th
21bef 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62  e database may b
21bf0 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61  e auto-vacuum ca
21bf1 70 61 62 6c 65 20 28 69 66 20 53 51 4c 49 54 45  pable (if SQLITE
21bf2 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
21bf3 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66  .  ** is not def
21bf4 69 6e 65 64 29 2c 20 74 68 65 6e 20 69 74 20 69  ined), then it i
21bf5 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63  s important to c
21bf6 61 6c 6c 20 4f 50 5f 44 65 73 74 72 6f 79 20 6f  all OP_Destroy o
21bf7 6e 20 74 68 65 0a 20 20 2a 2a 20 74 61 62 6c 65  n the.  ** table
21bf8 20 61 6e 64 20 69 6e 64 65 78 20 72 6f 6f 74 2d   and index root-
21bf9 70 61 67 65 73 20 69 6e 20 6f 72 64 65 72 2c 20  pages in order, 
21bfa 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 68  starting with th
21bfb 65 20 6e 75 6d 65 72 69 63 61 6c 6c 79 20 0a 20  e numerically . 
21bfc 20 2a 2a 20 6c 61 72 67 65 73 74 20 72 6f 6f 74   ** largest root
21bfd 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 54 68  -page number. Th
21bfe 69 73 20 67 75 61 72 61 6e 74 65 65 73 20 74 68  is guarantees th
21bff 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 72  at none of the r
21c00 6f 6f 74 2d 70 61 67 65 73 0a 20 20 2a 2a 20 74  oot-pages.  ** t
21c01 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 69  o be destroyed i
21c02 73 20 72 65 6c 6f 63 61 74 65 64 20 62 79 20 61  s relocated by a
21c03 6e 20 65 61 72 6c 69 65 72 20 4f 50 5f 44 65 73  n earlier OP_Des
21c04 74 72 6f 79 2e 20 69 2e 65 2e 20 69 66 20 74 68  troy. i.e. if th
21c05 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  e.  ** following
21c06 20 77 65 72 65 20 63 6f 64 65 64 3a 0a 20 20 2a   were coded:.  *
21c07 2a 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f  *.  ** OP_Destro
21c08 79 20 34 20 30 0a 20 20 2a 2a 20 2e 2e 2e 0a 20  y 4 0.  ** .... 
21c09 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 35   ** OP_Destroy 5
21c0a 20 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64   0.  **.  ** and
21c0b 20 72 6f 6f 74 20 70 61 67 65 20 35 20 68 61 70   root page 5 hap
21c0c 70 65 6e 65 64 20 74 6f 20 62 65 20 74 68 65 20  pened to be the 
21c0d 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67  largest root-pag
21c0e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a  e number in the.
21c0f 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74    ** database, t
21c10 68 65 6e 20 72 6f 6f 74 20 70 61 67 65 20 35 20  hen root page 5 
21c11 77 6f 75 6c 64 20 62 65 20 6d 6f 76 65 64 20 74  would be moved t
21c12 6f 20 70 61 67 65 20 34 20 62 79 20 74 68 65 20  o page 4 by the 
21c13 0a 20 20 2a 2a 20 22 4f 50 5f 44 65 73 74 72 6f  .  ** "OP_Destro
21c14 79 20 34 20 30 22 20 6f 70 63 6f 64 65 2e 20 54  y 4 0" opcode. T
21c15 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 22 4f  he subsequent "O
21c16 50 5f 44 65 73 74 72 6f 79 20 35 20 30 22 20 77  P_Destroy 5 0" w
21c17 6f 75 6c 64 20 68 69 74 0a 20 20 2a 2a 20 61 20  ould hit.  ** a 
21c18 66 72 65 65 2d 6c 69 73 74 20 70 61 67 65 2e 0a  free-list page..
21c19 20 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20    */.  int iTab 
21c1a 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 0a 20 20  = pTab->tnum;.  
21c1b 69 6e 74 20 69 44 65 73 74 72 6f 79 65 64 20 3d  int iDestroyed =
21c1c 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20 31 20   0;..  while( 1 
21c1d 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
21c1e 64 78 3b 0a 20 20 20 20 69 6e 74 20 69 4c 61 72  dx;.    int iLar
21c1f 67 65 73 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69  gest = 0;..    i
21c20 66 28 20 69 44 65 73 74 72 6f 79 65 64 3d 3d 30  f( iDestroyed==0
21c21 20 7c 7c 20 69 54 61 62 3c 69 44 65 73 74 72 6f   || iTab<iDestro
21c22 79 65 64 20 29 7b 0a 20 20 20 20 20 20 69 4c 61  yed ){.      iLa
21c23 72 67 65 73 74 20 3d 20 69 54 61 62 3b 0a 20 20  rgest = iTab;.  
21c24 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 49 64 78    }.    for(pIdx
21c25 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
21c26 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
21c27 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e  pNext){.      in
21c28 74 20 69 49 64 78 20 3d 20 70 49 64 78 2d 3e 74  t iIdx = pIdx->t
21c29 6e 75 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  num;.      asser
21c2a 74 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61  t( pIdx->pSchema
21c2b 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20  ==pTab->pSchema 
21c2c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 44  );.      if( (iD
21c2d 65 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 28  estroyed==0 || (
21c2e 69 49 64 78 3c 69 44 65 73 74 72 6f 79 65 64 29  iIdx<iDestroyed)
21c2f 29 20 26 26 20 69 49 64 78 3e 69 4c 61 72 67 65  ) && iIdx>iLarge
21c30 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c  st ){.        iL
21c31 61 72 67 65 73 74 20 3d 20 69 49 64 78 3b 0a 20  argest = iIdx;. 
21c32 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
21c33 20 69 66 28 20 69 4c 61 72 67 65 73 74 3d 3d 30   if( iLargest==0
21c34 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
21c35 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
21c36 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c     int iDb = sql
21c37 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
21c38 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
21c39 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
21c3a 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50      destroyRootP
21c3b 61 67 65 28 70 50 61 72 73 65 2c 20 69 4c 61 72  age(pParse, iLar
21c3c 67 65 73 74 2c 20 69 44 62 29 3b 0a 20 20 20 20  gest, iDb);.    
21c3d 20 20 69 44 65 73 74 72 6f 79 65 64 20 3d 20 69    iDestroyed = i
21c3e 4c 61 72 67 65 73 74 3b 0a 20 20 20 20 7d 0a 20  Largest;.    }. 
21c3f 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
21c40 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
21c41 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 6f 20  is called to do 
21c42 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 20 44 52  the work of a DR
21c43 4f 50 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  OP TABLE stateme
21c44 6e 74 2e 0a 2a 2a 20 70 4e 61 6d 65 20 69 73 20  nt..** pName is 
21c45 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
21c46 74 61 62 6c 65 20 74 6f 20 62 65 20 64 72 6f 70  table to be drop
21c47 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ped..*/.SQLITE_P
21c48 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21c49 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72  te3DropTable(Par
21c4a 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
21c4b 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20  ist *pName, int 
21c4c 69 73 56 69 65 77 2c 20 69 6e 74 20 6e 6f 45 72  isView, int noEr
21c4d 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  r){.  Table *pTa
21c4e 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  b;.  Vdbe *v;.  
21c4f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
21c50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
21c51 69 44 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72  iDb;..  if( pPar
21c52 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
21c53 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
21c54 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
21c55 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20  op_table;.  }.  
21c56 61 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e  assert( pName->n
21c57 53 72 63 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62  Src==1 );.  pTab
21c58 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
21c59 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 73  Table(pParse, is
21c5a 56 69 65 77 2c 20 0a 20 20 20 20 20 20 20 20 20  View, .         
21c5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c5c 20 20 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a     pName->a[0].z
21c5d 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30  Name, pName->a[0
21c5e 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 0a 20  ].zDatabase);.. 
21c5f 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a   if( pTab==0 ){.
21c60 20 20 20 20 69 66 28 20 6e 6f 45 72 72 20 29 7b      if( noErr ){
21c61 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
21c62 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29  rorClear(pParse)
21c63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f  ;.    }.    goto
21c64 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
21c65 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71  ;.  }.  iDb = sq
21c66 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
21c67 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
21c68 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28  hema);.  assert(
21c69 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
21c6a 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20  b->nDb );..  /* 
21c6b 49 66 20 70 54 61 62 20 69 73 20 61 20 76 69 72  If pTab is a vir
21c6c 74 75 61 6c 20 74 61 62 6c 65 2c 20 63 61 6c 6c  tual table, call
21c6d 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61   ViewGetColumnNa
21c6e 6d 65 73 28 29 20 74 6f 20 65 6e 73 75 72 65 0a  mes() to ensure.
21c6f 20 20 2a 2a 20 69 74 20 69 73 20 69 6e 69 74 69    ** it is initi
21c70 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  alized..  */.  i
21c71 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
21c72 62 29 20 26 26 20 73 71 6c 69 74 65 33 56 69 65  b) && sqlite3Vie
21c73 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
21c74 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
21c75 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
21c76 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23  rop_table;.  }.#
21c77 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21c78 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
21c79 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64  .  {.    int cod
21c7a 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
21c7b 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41  r *zTab = SCHEMA
21c7c 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20  _TABLE(iDb);.   
21c7d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
21c7e 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
21c7f 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74  zName;.    const
21c80 20 63 68 61 72 20 2a 7a 41 72 67 32 20 3d 20 30   char *zArg2 = 0
21c81 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
21c82 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
21c83 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  e, SQLITE_DELETE
21c84 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 29  , zTab, 0, zDb))
21c85 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
21c86 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
21c87 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69    }.    if( isVi
21c88 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ew ){.      if( 
21c89 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
21c8a 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  iDb==1 ){.      
21c8b 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
21c8c 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 3b 0a  DROP_TEMP_VIEW;.
21c8d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
21c8e 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
21c8f 54 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a 20 20  TE_DROP_VIEW;.  
21c90 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
21c91 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
21c92 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65  LTABLE.    }else
21c93 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
21c94 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 63 6f  Tab) ){.      co
21c95 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50  de = SQLITE_DROP
21c96 5f 56 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7a  _VTABLE;.      z
21c97 41 72 67 32 20 3d 20 70 54 61 62 2d 3e 70 4d 6f  Arg2 = pTab->pMo
21c98 64 2d 3e 7a 4e 61 6d 65 3b 0a 23 65 6e 64 69 66  d->zName;.#endif
21c99 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
21c9a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
21c9b 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a  DB && iDb==1 ){.
21c9c 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
21c9d 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
21c9e 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c  TABLE;.      }el
21c9f 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65  se{.        code
21ca0 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54   = SQLITE_DROP_T
21ca1 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ABLE;.      }.  
21ca2 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
21ca3 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
21ca4 72 73 65 2c 20 63 6f 64 65 2c 20 70 54 61 62 2d  rse, code, pTab-
21ca5 3e 7a 4e 61 6d 65 2c 20 7a 41 72 67 32 2c 20 7a  >zName, zArg2, z
21ca6 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
21ca7 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
21ca8 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
21ca9 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
21caa 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
21cab 5f 44 45 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a  _DELETE, pTab->z
21cac 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b  Name, 0, zDb) ){
21cad 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
21cae 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20  _drop_table;.   
21caf 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
21cb0 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
21cb1 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  Cmp(pTab->zName,
21cb2 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d   "sqlite_", 7)==
21cb3 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
21cb4 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
21cb5 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e   "table %s may n
21cb6 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 2c 20  ot be dropped", 
21cb7 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
21cb8 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
21cb9 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66  _table;.  }..#if
21cba 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
21cbb 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e 73 75 72  _VIEW.  /* Ensur
21cbc 65 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20  e DROP TABLE is 
21cbd 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61 20 76 69  not used on a vi
21cbe 65 77 2c 20 61 6e 64 20 44 52 4f 50 20 56 49 45  ew, and DROP VIE
21cbf 57 20 69 73 20 6e 6f 74 20 75 73 65 64 0a 20 20  W is not used.  
21cc0 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65 2e 0a 20  ** on a table.. 
21cc1 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77   */.  if( isView
21cc2 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   && pTab->pSelec
21cc3 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  t==0 ){.    sqli
21cc4 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
21cc5 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 54 41  se, "use DROP TA
21cc6 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 20 74 61  BLE to delete ta
21cc7 62 6c 65 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a  ble %s", pTab->z
21cc8 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
21cc9 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
21cca 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73 56 69  .  }.  if( !isVi
21ccb 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c  ew && pTab->pSel
21ccc 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ect ){.    sqlit
21ccd 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
21cce 65 2c 20 22 75 73 65 20 44 52 4f 50 20 56 49 45  e, "use DROP VIE
21ccf 57 20 74 6f 20 64 65 6c 65 74 65 20 76 69 65 77  W to delete view
21cd0 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d   %s", pTab->zNam
21cd1 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  e);.    goto exi
21cd2 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
21cd3 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47  }.#endif..  /* G
21cd4 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
21cd5 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65  remove the table
21cd6 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 65 72   from the master
21cd7 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 6e 20 64   table.  ** on d
21cd8 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  isk..  */.  v = 
21cd9 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
21cda 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
21cdb 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72 20 2a  ){.    Trigger *
21cdc 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20 44 62  pTrigger;.    Db
21cdd 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
21cde 5b 69 44 62 5d 3b 0a 20 20 20 20 73 71 6c 69 74  [iDb];.    sqlit
21cdf 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
21ce0 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c  ation(pParse, 1,
21ce1 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20   iDb);..#ifndef 
21ce2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
21ce3 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28  UALTABLE.    if(
21ce4 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
21ce5 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 76 20   ){.      if( v 
21ce6 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
21ce7 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
21ce8 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20  OP_VBegin);.    
21ce9 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
21cea 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c  ..    /* Drop al
21ceb 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63  l triggers assoc
21cec 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74  iated with the t
21ced 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70  able being dropp
21cee 65 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20  ed. Code.    ** 
21cef 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  is generated to 
21cf0 72 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66  remove entries f
21cf1 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  rom sqlite_maste
21cf2 72 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20  r and/or.    ** 
21cf3 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
21cf4 65 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  er if required..
21cf5 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67      */.    pTrig
21cf6 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ger = sqlite3Tri
21cf7 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ggerList(pParse,
21cf8 20 70 54 61 62 29 3b 0a 20 20 20 20 77 68 69 6c   pTab);.    whil
21cf9 65 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  e( pTrigger ){. 
21cfa 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72       assert( pTr
21cfb 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d  igger->pSchema==
21cfc 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c  pTab->pSchema ||
21cfd 20 0a 20 20 20 20 20 20 20 20 20 20 70 54 72 69   .          pTri
21cfe 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64  gger->pSchema==d
21cff 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
21d00 61 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  a );.      sqlit
21d01 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72  e3DropTriggerPtr
21d02 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
21d03 72 29 3b 0a 20 20 20 20 20 20 70 54 72 69 67 67  r);.      pTrigg
21d04 65 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 70  er = pTrigger->p
21d05 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  Next;.    }..#if
21d06 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
21d07 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20  _AUTOINCREMENT. 
21d08 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79     /* Remove any
21d09 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20   entries of the 
21d0a 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
21d0b 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64  table associated
21d0c 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65   with.    ** the
21d0d 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f   table being dro
21d0e 70 70 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f  pped. This is do
21d0f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 61  ne before the ta
21d10 62 6c 65 20 69 73 20 64 72 6f 70 70 65 64 0a 20  ble is dropped. 
21d11 20 20 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72     ** at the btr
21d12 65 65 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73  ee level, in cas
21d13 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  e the sqlite_seq
21d14 75 65 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64  uence table need
21d15 73 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65  s to.    ** move
21d16 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
21d17 74 68 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61  the drop (can ha
21d18 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63  ppen in auto-vac
21d19 75 75 6d 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a  uum mode)..    *
21d1a 2f 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e  /.    if( pTab->
21d1b 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75  tabFlags & TF_Au
21d1c 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20  toincrement ){. 
21d1d 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74       sqlite3Nest
21d1e 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
21d1f 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20          "DELETE 
21d20 46 52 4f 4d 20 25 73 2e 73 71 6c 69 74 65 5f 73  FROM %s.sqlite_s
21d21 65 71 75 65 6e 63 65 20 57 48 45 52 45 20 6e 61  equence WHERE na
21d22 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20  me=%Q",.        
21d23 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62  pDb->zName, pTab
21d24 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b  ->zName.      );
21d25 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
21d26 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53     /* Drop all S
21d27 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
21d28 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74  le and index ent
21d29 72 69 65 73 20 74 68 61 74 20 72 65 66 65 72 20  ries that refer 
21d2a 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61  to the.    ** ta
21d2b 62 6c 65 2e 20 54 68 65 20 70 72 6f 67 72 61 6d  ble. The program
21d2c 20 6e 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f   name loops thro
21d2d 75 67 68 20 74 68 65 20 6d 61 73 74 65 72 20 74  ugh the master t
21d2e 61 62 6c 65 20 61 6e 64 20 64 65 6c 65 74 65 73  able and deletes
21d2f 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20 72 6f  .    ** every ro
21d30 77 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f  w that refers to
21d31 20 61 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20   a table of the 
21d32 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 74 68 65  same name as the
21d33 20 6f 6e 65 20 62 65 69 6e 67 0a 20 20 20 20 2a   one being.    *
21d34 2a 20 64 72 6f 70 70 65 64 2e 20 54 72 69 67 67  * dropped. Trigg
21d35 65 72 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20  ers are handled 
21d36 73 65 70 65 72 61 74 65 6c 79 20 62 65 63 61 75  seperately becau
21d37 73 65 20 61 20 74 72 69 67 67 65 72 20 63 61 6e  se a trigger can
21d38 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74   be.    ** creat
21d39 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64  ed in the temp d
21d3a 61 74 61 62 61 73 65 20 74 68 61 74 20 72 65 66  atabase that ref
21d3b 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 20 69  ers to a table i
21d3c 6e 20 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a  n another.    **
21d3d 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a   database..    *
21d3e 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  /.    sqlite3Nes
21d3f 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
21d40 20 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54   .        "DELET
21d41 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45  E FROM %Q.%s WHE
21d42 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61  RE tbl_name=%Q a
21d43 6e 64 20 74 79 70 65 21 3d 27 74 72 69 67 67 65  nd type!='trigge
21d44 72 27 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62  r'",.        pDb
21d45 2d 3e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ->zName, SCHEMA_
21d46 54 41 42 4c 45 28 69 44 62 29 2c 20 70 54 61 62  TABLE(iDb), pTab
21d47 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f  ->zName);..    /
21d48 2a 20 44 72 6f 70 20 61 6e 79 20 73 74 61 74 69  * Drop any stati
21d49 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65 20 73  stics from the s
21d4a 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
21d4b 65 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 20  e, if it exists 
21d4c 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
21d4d 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
21d4e 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20  "sqlite_stat1", 
21d4f 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
21d50 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  me) ){.      sql
21d51 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
21d52 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
21d53 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
21d54 73 71 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45  sqlite_stat1 WHE
21d55 52 45 20 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d  RE tbl=%Q", pDb-
21d56 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e  >zName, pTab->zN
21d57 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ame.      );.   
21d58 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56   }..    if( !isV
21d59 69 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61  iew && !IsVirtua
21d5a 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
21d5b 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50   destroyTable(pP
21d5c 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
21d5d 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76   }..    /* Remov
21d5e 65 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72  e the table entr
21d5f 79 20 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20  y from SQLite's 
21d60 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20  internal schema 
21d61 61 6e 64 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a  and modify.    *
21d62 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  * the schema coo
21d63 6b 69 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  kie..    */.    
21d64 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
21d65 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ab) ){.      sql
21d66 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
21d67 2c 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 69  , OP_VDestroy, i
21d68 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e  Db, 0, 0, pTab->
21d69 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d  zName, 0);.    }
21d6a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21d6b 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f  AddOp4(v, OP_Dro
21d6c 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20  pTable, iDb, 0, 
21d6d 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  0, pTab->zName, 
21d6e 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  0);.    sqlite3C
21d6f 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
21d70 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20  se, iDb);.  }.  
21d71 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41  sqliteViewResetA
21d72 6c 6c 28 64 62 2c 20 69 44 62 29 3b 0a 0a 65 78  ll(db, iDb);..ex
21d73 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3a 0a 20  it_drop_table:. 
21d74 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
21d75 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29  elete(db, pName)
21d76 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
21d77 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
21d78 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  d to create a ne
21d79 77 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e  w foreign key on
21d7a 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75   the table.** cu
21d7b 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
21d7c 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72  nstruction.  pFr
21d7d 6f 6d 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73  omCol determines
21d7e 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a   which columns.*
21d7f 2a 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74  * in the current
21d80 20 74 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20   table point to 
21d81 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e  the foreign key.
21d82 20 20 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30    If pFromCol==0
21d83 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74   then.** connect
21d84 20 74 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20   the key to the 
21d85 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65  last column inse
21d86 72 74 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68  rted.  pTo is th
21d87 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65  e name of.** the
21d88 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20   table referred 
21d89 74 6f 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61  to.  pToCol is a
21d8a 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20   list of tables 
21d8b 69 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20  in the other.** 
21d8c 70 54 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74  pTo table that t
21d8d 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70  he foreign key p
21d8e 6f 69 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73  oints to.  flags
21d8f 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a   contains all.**
21d90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
21d91 75 74 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20  ut the conflict 
21d92 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72  resolution algor
21d93 69 74 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a  ithms specified.
21d94 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c  ** in the ON DEL
21d95 45 54 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61  ETE, ON UPDATE a
21d96 6e 64 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61  nd ON INSERT cla
21d97 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46  uses..**.** An F
21d98 4b 65 79 20 73 74 72 75 63 74 75 72 65 20 69 73  Key structure is
21d99 20 63 72 65 61 74 65 64 20 61 6e 64 20 61 64 64   created and add
21d9a 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ed to the table 
21d9b 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64  currently.** und
21d9c 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
21d9d 69 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70  in the pParse->p
21d9e 4e 65 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 20  NewTable field. 
21d9f 20 54 68 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a   The new FKey.**
21da0 20 69 73 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69   is not linked i
21da1 6e 74 6f 20 64 62 2d 3e 61 46 4b 65 79 20 61 74  nto db->aFKey at
21da2 20 74 68 69 73 20 70 6f 69 6e 74 20 2d 20 74 68   this point - th
21da3 61 74 20 64 6f 65 73 20 6e 6f 74 20 68 61 70 70  at does not happ
21da4 65 6e 0a 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69  en.** until sqli
21da5 74 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a  te3EndTable()..*
21da6 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 65 69 67 6e  *.** The foreign
21da7 20 6b 65 79 20 69 73 20 73 65 74 20 66 6f 72 20   key is set for 
21da8 49 4d 4d 45 44 49 41 54 45 20 70 72 6f 63 65 73  IMMEDIATE proces
21da9 73 69 6e 67 2e 20 20 41 20 73 75 62 73 65 71 75  sing.  A subsequ
21daa 65 6e 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73  ent call.** to s
21dab 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69  qlite3DeferForei
21dac 67 6e 4b 65 79 28 29 20 6d 69 67 68 74 20 63 68  gnKey() might ch
21dad 61 6e 67 65 20 74 68 69 73 20 74 6f 20 44 45 46  ange this to DEF
21dae 45 52 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ERRED..*/.SQLITE
21daf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
21db0 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69  lite3CreateForei
21db1 67 6e 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a  gnKey(.  Parse *
21db2 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
21db3 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
21db4 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
21db5 70 46 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f  pFromCol,  /* Co
21db6 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61  lumns in this ta
21db7 62 6c 65 20 74 68 61 74 20 70 6f 69 6e 74 20 74  ble that point t
21db8 6f 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f  o other table */
21db9 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20  .  Token *pTo,  
21dba 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
21dbb 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 61 62  of the other tab
21dbc 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  le */.  ExprList
21dbd 20 2a 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20   *pToCol,    /* 
21dbe 43 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f  Columns in the o
21dbf 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ther table */.  
21dc0 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20 20  int flags       
21dc1 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74       /* Conflict
21dc2 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f   resolution algo
21dc3 72 69 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20  rithms. */.){.  
21dc4 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
21dc5 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65  arse->db;.#ifnde
21dc6 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
21dc7 52 45 49 47 4e 5f 4b 45 59 0a 20 20 46 4b 65 79  REIGN_KEY.  FKey
21dc8 20 2a 70 46 4b 65 79 20 3d 20 30 3b 0a 20 20 54   *pFKey = 0;.  T
21dc9 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72 73 65  able *p = pParse
21dca 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69  ->pNewTable;.  i
21dcb 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 6e 74 20  nt nByte;.  int 
21dcc 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20  i;.  int nCol;. 
21dcd 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73   char *z;..  ass
21dce 65 72 74 28 20 70 54 6f 21 3d 30 20 29 3b 0a 20  ert( pTo!=0 );. 
21dcf 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61   if( p==0 || pPa
21dd0 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 49 4e 5f  rse->nErr || IN_
21dd1 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67  DECLARE_VTAB ) g
21dd2 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 69 66  oto fk_end;.  if
21dd3 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b  ( pFromCol==0 ){
21dd4 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20  .    int iCol = 
21dd5 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20 20 69  p->nCol-1;.    i
21dd6 66 28 20 69 43 6f 6c 3c 30 20 29 20 67 6f 74 6f  f( iCol<0 ) goto
21dd7 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69 66 28   fk_end;.    if(
21dd8 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f   pToCol && pToCo
21dd9 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20  l->nExpr!=1 ){. 
21dda 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
21ddb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 66 6f  rMsg(pParse, "fo
21ddc 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25 73 22  reign key on %s"
21ddd 0a 20 20 20 20 20 20 20 20 20 22 20 73 68 6f 75  .         " shou
21dde 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c  ld reference onl
21ddf 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  y one column of 
21de0 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20 20 20  table %T",.     
21de1 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43 6f 6c      p->aCol[iCol
21de2 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b 0a 20  ].zName, pTo);. 
21de3 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64       goto fk_end
21de4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c  ;.    }.    nCol
21de5 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 1;.  }else if
21de6 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43  ( pToCol && pToC
21de7 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72 6f 6d  ol->nExpr!=pFrom
21de8 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20  Col->nExpr ){.  
21de9 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
21dea 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  g(pParse,.      
21deb 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c    "number of col
21dec 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67 6e 20  umns in foreign 
21ded 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  key does not mat
21dee 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ch the number of
21def 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f 6c 75   ".        "colu
21df0 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65 72  mns in the refer
21df1 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b 0a 20  enced table");. 
21df2 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a     goto fk_end;.
21df3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 43 6f    }else{.    nCo
21df4 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45  l = pFromCol->nE
21df5 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65  xpr;.  }.  nByte
21df6 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b 65 79   = sizeof(*pFKey
21df7 29 20 2b 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28  ) + nCol*sizeof(
21df8 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20  pFKey->aCol[0]) 
21df9 2b 20 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20  + pTo->n + 1;.  
21dfa 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20  if( pToCol ){.  
21dfb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f    for(i=0; i<pTo
21dfc 43 6f 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  Col->nExpr; i++)
21dfd 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d  {.      nByte +=
21dfe 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
21dff 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e  (pToCol->a[i].zN
21e00 61 6d 65 29 20 2b 20 31 3b 0a 20 20 20 20 7d 0a  ame) + 1;.    }.
21e01 20 20 7d 0a 20 20 70 46 4b 65 79 20 3d 20 73 71    }.  pFKey = sq
21e02 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
21e03 6f 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20  o(db, nByte );. 
21e04 20 69 66 28 20 70 46 4b 65 79 3d 3d 30 20 29 7b   if( pFKey==0 ){
21e05 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
21e06 3b 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 70  ;.  }.  pFKey->p
21e07 46 72 6f 6d 20 3d 20 70 3b 0a 20 20 70 46 4b 65  From = p;.  pFKe
21e08 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 3d 20 70  y->pNextFrom = p
21e09 2d 3e 70 46 4b 65 79 3b 0a 20 20 7a 20 3d 20 28  ->pFKey;.  z = (
21e0a 63 68 61 72 2a 29 26 70 46 4b 65 79 5b 31 5d 3b  char*)&pFKey[1];
21e0b 0a 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 20 3d  .  pFKey->aCol =
21e0c 20 28 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 70   (struct sColMap
21e0d 2a 29 7a 3b 0a 20 20 7a 20 2b 3d 20 73 69 7a 65  *)z;.  z += size
21e0e 6f 66 28 73 74 72 75 63 74 20 73 43 6f 6c 4d 61  of(struct sColMa
21e0f 70 29 2a 6e 43 6f 6c 3b 0a 20 20 70 46 4b 65 79  p)*nCol;.  pFKey
21e10 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20 20 6d 65 6d  ->zTo = z;.  mem
21e11 63 70 79 28 7a 2c 20 70 54 6f 2d 3e 7a 2c 20 70  cpy(z, pTo->z, p
21e12 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b 70 54 6f 2d  To->n);.  z[pTo-
21e13 3e 6e 5d 20 3d 20 30 3b 0a 20 20 7a 20 2b 3d 20  >n] = 0;.  z += 
21e14 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65  pTo->n+1;.  pFKe
21e15 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 30 3b 0a  y->pNextTo = 0;.
21e16 20 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 20 3d 20    pFKey->nCol = 
21e17 6e 43 6f 6c 3b 0a 20 20 69 66 28 20 70 46 72 6f  nCol;.  if( pFro
21e18 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 70  mCol==0 ){.    p
21e19 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46  FKey->aCol[0].iF
21e1a 72 6f 6d 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b  rom = p->nCol-1;
21e1b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f  .  }else{.    fo
21e1c 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
21e1d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a  ++){.      int j
21e1e 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
21e1f 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29   j<p->nCol; j++)
21e20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
21e21 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 2d 3e  lite3StrICmp(p->
21e22 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70  aCol[j].zName, p
21e23 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e  FromCol->a[i].zN
21e24 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ame)==0 ){.     
21e25 20 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c       pFKey->aCol
21e26 5b 69 5d 2e 69 46 72 6f 6d 20 3d 20 6a 3b 0a 20  [i].iFrom = j;. 
21e27 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
21e28 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21e29 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70  }.      if( j>=p
21e2a 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
21e2b 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
21e2c 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  g(pParse, .     
21e2d 20 20 20 20 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f       "unknown co
21e2e 6c 75 6d 6e 20 5c 22 25 73 5c 22 20 69 6e 20 66  lumn \"%s\" in f
21e2f 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e  oreign key defin
21e30 69 74 69 6f 6e 22 2c 20 0a 20 20 20 20 20 20 20  ition", .       
21e31 20 20 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69     pFromCol->a[i
21e32 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ].zName);.      
21e33 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20    goto fk_end;. 
21e34 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
21e35 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b  .  if( pToCol ){
21e36 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
21e37 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
21e38 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
21e39 33 53 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c  3Strlen30(pToCol
21e3a 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  ->a[i].zName);. 
21e3b 20 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c       pFKey->aCol
21e3c 5b 69 5d 2e 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20  [i].zCol = z;.  
21e3d 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54      memcpy(z, pT
21e3e 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  oCol->a[i].zName
21e3f 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d  , n);.      z[n]
21e40 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 20 2b 3d   = 0;.      z +=
21e41 20 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   n+1;.    }.  }.
21e42 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72    pFKey->isDefer
21e43 72 65 64 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79  red = 0;.  pFKey
21e44 2d 3e 64 65 6c 65 74 65 43 6f 6e 66 20 3d 20 28  ->deleteConf = (
21e45 75 38 29 28 66 6c 61 67 73 20 26 20 30 78 66 66  u8)(flags & 0xff
21e46 29 3b 0a 20 20 70 46 4b 65 79 2d 3e 75 70 64 61  );.  pFKey->upda
21e47 74 65 43 6f 6e 66 20 3d 20 28 75 38 29 28 28 66  teConf = (u8)((f
21e48 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78  lags >> 8 ) & 0x
21e49 66 66 29 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e  ff);.  pFKey->in
21e4a 73 65 72 74 43 6f 6e 66 20 3d 20 28 75 38 29 28  sertConf = (u8)(
21e4b 28 66 6c 61 67 73 20 3e 3e 20 31 36 20 29 20 26  (flags >> 16 ) &
21e4c 20 30 78 66 66 29 3b 0a 0a 20 20 2f 2a 20 4c 69   0xff);..  /* Li
21e4d 6e 6b 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  nk the foreign k
21e4e 65 79 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ey to the table 
21e4f 61 73 20 74 68 65 20 6c 61 73 74 20 73 74 65 70  as the last step
21e50 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 46 4b 65  ..  */.  p->pFKe
21e51 79 20 3d 20 70 46 4b 65 79 3b 0a 20 20 70 46 4b  y = pFKey;.  pFK
21e52 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e 64 3a  ey = 0;..fk_end:
21e53 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
21e54 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 23 65 6e  (db, pFKey);.#en
21e55 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
21e56 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
21e57 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20 73 71  IGN_KEY) */.  sq
21e58 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
21e59 65 74 65 28 64 62 2c 20 70 46 72 6f 6d 43 6f 6c  ete(db, pFromCol
21e5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
21e5b 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
21e5c 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ToCol);.}../*.**
21e5d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
21e5e 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20   called when an 
21e5f 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49  INITIALLY IMMEDI
21e60 41 54 45 20 6f 72 20 49 4e 49 54 49 41 4c 4c 59  ATE or INITIALLY
21e61 20 44 45 46 45 52 52 45 44 0a 2a 2a 20 63 6c 61   DEFERRED.** cla
21e62 75 73 65 20 69 73 20 73 65 65 6e 20 61 73 20 70  use is seen as p
21e63 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e  art of a foreign
21e64 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e   key definition.
21e65 20 20 54 68 65 20 69 73 44 65 66 65 72 72 65 64    The isDeferred
21e66 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73  .** parameter is
21e67 20 31 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59   1 for INITIALLY
21e68 20 44 45 46 45 52 52 45 44 20 61 6e 64 20 30 20   DEFERRED and 0 
21e69 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d  for INITIALLY IM
21e6a 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 54 68 65 20  MEDIATE..** The 
21e6b 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20  behavior of the 
21e6c 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72  most recently cr
21e6d 65 61 74 65 64 20 66 6f 72 65 69 67 6e 20 6b 65  eated foreign ke
21e6e 79 20 69 73 20 61 64 6a 75 73 74 65 64 0a 2a 2a  y is adjusted.**
21e6f 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2f   accordingly..*/
21e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21e71 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65  void sqlite3Defe
21e72 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73  rForeignKey(Pars
21e73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
21e74 73 44 65 66 65 72 72 65 64 29 7b 0a 23 69 66 6e  sDeferred){.#ifn
21e75 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
21e76 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 54 61  FOREIGN_KEY.  Ta
21e77 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 46 4b 65  ble *pTab;.  FKe
21e78 79 20 2a 70 46 4b 65 79 3b 0a 20 20 69 66 28 20  y *pFKey;.  if( 
21e79 28 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  (pTab = pParse->
21e7a 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 7c 7c  pNewTable)==0 ||
21e7b 20 28 70 46 4b 65 79 20 3d 20 70 54 61 62 2d 3e   (pFKey = pTab->
21e7c 70 46 4b 65 79 29 3d 3d 30 20 29 20 72 65 74 75  pFKey)==0 ) retu
21e7d 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73  rn;.  assert( is
21e7e 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c 20 69  Deferred==0 || i
21e7f 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29 3b 0a  sDeferred==1 );.
21e80 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72    pFKey->isDefer
21e81 72 65 64 20 3d 20 28 75 38 29 69 73 44 65 66 65  red = (u8)isDefe
21e82 72 72 65 64 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  rred;.#endif.}..
21e83 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
21e84 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 72  ode that will er
21e85 61 73 65 20 61 6e 64 20 72 65 66 69 6c 6c 20 69  ase and refill i
21e86 6e 64 65 78 20 2a 70 49 64 78 2e 20 20 54 68 69  ndex *pIdx.  Thi
21e87 73 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  s is.** used to 
21e88 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77  initialize a new
21e89 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64 65 78  ly created index
21e8a 20 6f 72 20 74 6f 20 72 65 63 6f 6d 70 75 74 65   or to recompute
21e8b 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20   the.** content 
21e8c 6f 66 20 61 6e 20 69 6e 64 65 78 20 69 6e 20 72  of an index in r
21e8d 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 52 45 49  esponse to a REI
21e8e 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  NDEX command..**
21e8f 0a 2a 2a 20 69 66 20 6d 65 6d 52 6f 6f 74 50 61  .** if memRootPa
21e90 67 65 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69  ge is not negati
21e91 76 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ve, it means tha
21e92 74 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 6e  t the index is n
21e93 65 77 6c 79 0a 2a 2a 20 63 72 65 61 74 65 64 2e  ewly.** created.
21e94 20 20 54 68 65 20 72 65 67 69 73 74 65 72 20 73    The register s
21e95 70 65 63 69 66 69 65 64 20 62 79 20 6d 65 6d 52  pecified by memR
21e96 6f 6f 74 50 61 67 65 20 63 6f 6e 74 61 69 6e 73  ootPage contains
21e97 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67   the.** root pag
21e98 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
21e99 69 6e 64 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f  index.  If memRo
21e9a 6f 74 50 61 67 65 20 69 73 20 6e 65 67 61 74 69  otPage is negati
21e9b 76 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ve, then.** the 
21e9c 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78  index already ex
21e9d 69 73 74 73 20 61 6e 64 20 6d 75 73 74 20 62 65  ists and must be
21e9e 20 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20   cleared before 
21e9f 62 65 69 6e 67 20 72 65 66 69 6c 6c 65 64 20 61  being refilled a
21ea0 6e 64 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70  nd.** the root p
21ea1 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
21ea2 65 20 69 6e 64 65 78 20 69 73 20 74 61 6b 65 6e  e index is taken
21ea3 20 66 72 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e   from pIndex->tn
21ea4 75 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  um..*/.static vo
21ea5 69 64 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c  id sqlite3Refill
21ea6 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61  Index(Parse *pPa
21ea7 72 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64  rse, Index *pInd
21ea8 65 78 2c 20 69 6e 74 20 6d 65 6d 52 6f 6f 74 50  ex, int memRootP
21ea9 61 67 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  age){.  Table *p
21eaa 54 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54  Tab = pIndex->pT
21eab 61 62 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 61  able;  /* The ta
21eac 62 6c 65 20 74 68 61 74 20 69 73 20 69 6e 64 65  ble that is inde
21ead 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  xed */.  int iTa
21eae 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  b = pParse->nTab
21eaf 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65  ++;     /* Btree
21eb0 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72   cursor used for
21eb1 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69   pTab */.  int i
21eb2 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Idx = pParse->nT
21eb3 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72  ab++;     /* Btr
21eb4 65 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 66  ee cursor used f
21eb5 6f 72 20 70 49 6e 64 65 78 20 2a 2f 0a 20 20 69  or pIndex */.  i
21eb6 6e 74 20 61 64 64 72 31 3b 20 20 20 20 20 20 20  nt addr1;       
21eb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21eb8 20 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20   Address of top 
21eb9 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  of loop */.  int
21eba 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20   tnum;          
21ebb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
21ebc 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 6e 64 65  oot page of inde
21ebd 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  x */.  Vdbe *v; 
21ebe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ebf 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
21ec0 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73  e code into this
21ec1 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
21ec2 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
21ec3 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
21ec4 20 20 20 20 20 2f 2a 20 4b 65 79 49 6e 66 6f 20       /* KeyInfo 
21ec5 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  for index */.  i
21ec6 6e 74 20 72 65 67 49 64 78 4b 65 79 3b 20 20 20  nt regIdxKey;   
21ec7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21ec8 20 52 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61   Registers conta
21ec9 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20  ining the index 
21eca 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  key */.  int reg
21ecb 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 20  Record;         
21ecc 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
21ecd 74 65 72 20 68 6f 6c 64 69 6e 67 20 61 73 73 65  ter holding asse
21ece 6d 62 6c 69 65 64 20 69 6e 64 65 78 20 72 65 63  mblied index rec
21ecf 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ord */.  sqlite3
21ed0 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
21ed1 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64  b;      /* The d
21ed2 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
21ed3 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 20  on */.  int iDb 
21ed4 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
21ed5 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65  oIndex(db, pInde
21ed6 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69  x->pSchema);..#i
21ed7 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
21ed8 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
21ed9 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
21eda 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
21edb 51 4c 49 54 45 5f 52 45 49 4e 44 45 58 2c 20 70  QLITE_REINDEX, p
21edc 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 2c  Index->zName, 0,
21edd 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
21ede 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a 20  Db].zName ) ){. 
21edf 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
21ee0 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 71 75  endif..  /* Requ
21ee1 69 72 65 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ire a write-lock
21ee2 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f   on the table to
21ee3 20 70 65 72 66 6f 72 6d 20 74 68 69 73 20 6f 70   perform this op
21ee4 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  eration */.  sql
21ee5 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50  ite3TableLock(pP
21ee6 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d  arse, iDb, pTab-
21ee7 3e 74 6e 75 6d 2c 20 31 2c 20 70 54 61 62 2d 3e  >tnum, 1, pTab->
21ee8 7a 4e 61 6d 65 29 3b 0a 0a 20 20 76 20 3d 20 73  zName);..  v = s
21ee9 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
21eea 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
21eeb 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
21eec 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d 30  ( memRootPage>=0
21eed 20 29 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20 6d   ){.    tnum = m
21eee 65 6d 52 6f 6f 74 50 61 67 65 3b 0a 20 20 7d 65  emRootPage;.  }e
21eef 6c 73 65 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20  lse{.    tnum = 
21ef0 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 3b 0a 20 20  pIndex->tnum;.  
21ef1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21ef2 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c  Op2(v, OP_Clear,
21ef3 20 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d   tnum, iDb);.  }
21ef4 0a 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  .  pKey = sqlite
21ef5 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
21ef6 61 72 73 65 2c 20 70 49 6e 64 65 78 29 3b 0a 20  arse, pIndex);. 
21ef7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21ef8 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p4(v, OP_OpenWri
21ef9 74 65 2c 20 69 49 64 78 2c 20 74 6e 75 6d 2c 20  te, iIdx, tnum, 
21efa 69 44 62 2c 20 0a 20 20 20 20 20 20 20 20 20 20  iDb, .          
21efb 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 20            (char 
21efc 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
21efd 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 69  FO_HANDOFF);.  i
21efe 66 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d  f( memRootPage>=
21eff 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
21f00 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
21f01 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  1);.  }.  sqlite
21f02 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
21f03 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54  e, iTab, iDb, pT
21f04 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
21f05 3b 0a 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69  ;.  addr1 = sqli
21f06 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21f07 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62   OP_Rewind, iTab
21f08 2c 20 30 29 3b 0a 20 20 72 65 67 52 65 63 6f 72  , 0);.  regRecor
21f09 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
21f0a 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
21f0b 20 72 65 67 49 64 78 4b 65 79 20 3d 20 73 71 6c   regIdxKey = sql
21f0c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
21f0d 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49 6e  xKey(pParse, pIn
21f0e 64 65 78 2c 20 69 54 61 62 2c 20 72 65 67 52 65  dex, iTab, regRe
21f0f 63 6f 72 64 2c 20 31 29 3b 0a 20 20 69 66 28 20  cord, 1);.  if( 
21f10 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21  pIndex->onError!
21f11 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20  =OE_None ){.    
21f12 69 6e 74 20 6a 31 2c 20 6a 32 3b 0a 20 20 20 20  int j1, j2;.    
21f13 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 0a 0a 20  int regRowid;.. 
21f14 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65     regRowid = re
21f15 67 49 64 78 4b 65 79 20 2b 20 70 49 6e 64 65 78  gIdxKey + pIndex
21f16 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 6a  ->nColumn;.    j
21f17 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
21f18 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp3(v, OP_IsNu
21f19 6c 6c 2c 20 72 65 67 49 64 78 4b 65 79 2c 20 30  ll, regIdxKey, 0
21f1a 2c 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d  , pIndex->nColum
21f1b 6e 29 3b 0a 20 20 20 20 6a 32 20 3d 20 73 71 6c  n);.    j2 = sql
21f1c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
21f1d 2c 20 4f 50 5f 49 73 55 6e 69 71 75 65 2c 20 69  , OP_IsUnique, i
21f1e 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Idx,.           
21f1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f20 30 2c 20 72 65 67 52 6f 77 69 64 2c 20 53 51 4c  0, regRowid, SQL
21f21 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72  ITE_INT_TO_PTR(r
21f22 65 67 52 65 63 6f 72 64 29 2c 20 50 34 5f 49 4e  egRecord), P4_IN
21f23 54 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  T32);.    sqlite
21f24 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
21f25 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43  P_Halt, SQLITE_C
21f26 4f 4e 53 54 52 41 49 4e 54 2c 20 4f 45 5f 41 62  ONSTRAINT, OE_Ab
21f27 6f 72 74 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  ort, 0,.        
21f28 20 20 20 20 20 20 20 20 20 20 20 20 22 69 6e 64              "ind
21f29 65 78 65 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65  exed columns are
21f2a 20 6e 6f 74 20 75 6e 69 71 75 65 22 2c 20 50 34   not unique", P4
21f2b 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
21f2c 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
21f2d 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71  e(v, j1);.    sq
21f2e 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
21f2f 65 28 76 2c 20 6a 32 29 3b 0a 20 20 7d 0a 20 20  e(v, j2);.  }.  
21f30 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21f31 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
21f32 74 2c 20 69 49 64 78 2c 20 72 65 67 52 65 63 6f  t, iIdx, regReco
21f33 72 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  rd);.  sqlite3Re
21f34 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
21f35 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b  rse, regRecord);
21f36 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
21f37 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
21f38 20 69 54 61 62 2c 20 61 64 64 72 31 2b 31 29 3b   iTab, addr1+1);
21f39 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
21f3a 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
21f3b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
21f3c 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
21f3d 65 2c 20 69 54 61 62 29 3b 0a 20 20 73 71 6c 69  e, iTab);.  sqli
21f3e 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
21f3f 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 29   OP_Close, iIdx)
21f40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
21f41 65 20 61 20 6e 65 77 20 69 6e 64 65 78 20 66 6f  e a new index fo
21f42 72 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20  r an SQL table. 
21f43 20 70 4e 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69   pName1.pName2 i
21f44 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
21f45 65 20 69 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20  e index .** and 
21f46 70 54 62 6c 4c 69 73 74 20 69 73 20 74 68 65 20  pTblList is the 
21f47 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
21f48 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  e that is to be 
21f49 69 6e 64 65 78 65 64 2e 20 20 42 6f 74 68 20 77  indexed.  Both w
21f4a 69 6c 6c 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20  ill .** be NULL 
21f4b 66 6f 72 20 61 20 70 72 69 6d 61 72 79 20 6b 65  for a primary ke
21f4c 79 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 74 68  y or an index th
21f4d 61 74 20 69 73 20 63 72 65 61 74 65 64 20 74 6f  at is created to
21f4e 20 73 61 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e   satisfy a.** UN
21f4f 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e  IQUE constraint.
21f50 20 20 49 66 20 70 54 61 62 6c 65 20 61 6e 64 20    If pTable and 
21f51 70 49 6e 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c  pIndex are NULL,
21f52 20 75 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65   use pParse->pNe
21f53 77 54 61 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65  wTable.** as the
21f54 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   table to be ind
21f55 65 78 65 64 2e 20 20 70 50 61 72 73 65 2d 3e 70  exed.  pParse->p
21f56 4e 65 77 54 61 62 6c 65 20 69 73 20 61 20 74 61  NewTable is a ta
21f57 62 6c 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 63  ble that is.** c
21f58 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63  urrently being c
21f59 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 61 20  onstructed by a 
21f5a 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
21f5b 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c  tement..**.** pL
21f5c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66  ist is a list of
21f5d 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69   columns to be i
21f5e 6e 64 65 78 65 64 2e 20 20 70 4c 69 73 74 20 77  ndexed.  pList w
21f5f 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 66 20 74  ill be NULL if t
21f60 68 69 73 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d  his.** is a prim
21f61 61 72 79 20 6b 65 79 20 6f 72 20 75 6e 69 71 75  ary key or uniqu
21f62 65 2d 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20  e-constraint on 
21f63 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
21f64 63 6f 6c 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20  column added.** 
21f65 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
21f66 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
21f67 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a  struction.  .*/.
21f68 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21f69 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
21f6a 65 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  eIndex(.  Parse 
21f6b 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
21f6c 41 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  All information 
21f6d 61 62 6f 75 74 20 74 68 69 73 20 70 61 72 73 65  about this parse
21f6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
21f6f 6d 65 31 2c 20 20 20 20 20 2f 2a 20 46 69 72 73  me1,     /* Firs
21f70 74 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20  t part of index 
21f71 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c  name. May be NUL
21f72 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  L */.  Token *pN
21f73 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 53 65 63  ame2,     /* Sec
21f74 6f 6e 64 20 70 61 72 74 20 6f 66 20 69 6e 64 65  ond part of inde
21f75 78 20 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e  x name. May be N
21f76 55 4c 4c 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ULL */.  SrcList
21f77 20 2a 70 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54   *pTblName, /* T
21f78 61 62 6c 65 20 74 6f 20 69 6e 64 65 78 2e 20 55  able to index. U
21f79 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  se pParse->pNewT
21f7a 61 62 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 45  able if 0 */.  E
21f7b 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
21f7c 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 63    /* A list of c
21f7d 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64  olumns to be ind
21f7e 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  exed */.  int on
21f7f 45 72 72 6f 72 2c 20 20 20 20 20 20 20 2f 2a 20  Error,       /* 
21f80 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e  OE_Abort, OE_Ign
21f81 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c  ore, OE_Replace,
21f82 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20   or OE_None */. 
21f83 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20   Token *pStart, 
21f84 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54      /* The CREAT
21f85 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67  E token that beg
21f86 69 6e 73 20 74 68 69 73 20 73 74 61 74 65 6d 65  ins this stateme
21f87 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
21f88 45 6e 64 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  End,       /* Th
21f89 65 20 22 29 22 20 74 68 61 74 20 63 6c 6f 73 65  e ")" that close
21f8a 73 20 74 68 65 20 43 52 45 41 54 45 20 49 4e 44  s the CREATE IND
21f8b 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  EX statement */.
21f8c 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 2c    int sortOrder,
21f8d 20 20 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72 64       /* Sort ord
21f8e 65 72 20 6f 66 20 70 72 69 6d 61 72 79 20 6b 65  er of primary ke
21f8f 79 20 77 68 65 6e 20 70 4c 69 73 74 3d 3d 4e 55  y when pList==NU
21f90 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 66 4e 6f  LL */.  int ifNo
21f91 74 45 78 69 73 74 20 20 20 20 20 2f 2a 20 4f 6d  tExist     /* Om
21f92 69 74 20 65 72 72 6f 72 20 69 66 20 69 6e 64 65  it error if inde
21f93 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  x already exists
21f94 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
21f95 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 2f 2a  pTab = 0;     /*
21f96 20 54 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   Table to be ind
21f97 65 78 65 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20  exed */.  Index 
21f98 2a 70 49 6e 64 65 78 20 3d 20 30 3b 20 20 20 2f  *pIndex = 0;   /
21f99 2a 20 54 68 65 20 69 6e 64 65 78 20 74 6f 20 62  * The index to b
21f9a 65 20 63 72 65 61 74 65 64 20 2a 2f 0a 20 20 63  e created */.  c
21f9b 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20  har *zName = 0; 
21f9c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
21f9d 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  he index */.  in
21f9e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
21f9f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
21fa0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e  characters in zN
21fa1 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  ame */.  int i, 
21fa2 6a 3b 0a 20 20 54 6f 6b 65 6e 20 6e 75 6c 6c 49  j;.  Token nullI
21fa3 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6b  d;        /* Fak
21fa4 65 20 74 6f 6b 65 6e 20 66 6f 72 20 61 6e 20 65  e token for an e
21fa5 6d 70 74 79 20 49 44 20 6c 69 73 74 20 2a 2f 0a  mpty ID list */.
21fa6 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 20    DbFixer sFix; 
21fa7 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 73         /* For as
21fa8 73 69 67 6e 69 6e 67 20 64 61 74 61 62 61 73 65  signing database
21fa9 20 6e 61 6d 65 73 20 74 6f 20 70 54 61 62 6c 65   names to pTable
21faa 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72   */.  int sortOr
21fab 64 65 72 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 20  derMask;   /* 1 
21fac 74 6f 20 68 6f 6e 6f 72 20 44 45 53 43 20 69 6e  to honor DESC in
21fad 20 69 6e 64 65 78 2e 20 20 30 20 74 6f 20 69 67   index.  0 to ig
21fae 6e 6f 72 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  nore. */.  sqlit
21faf 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
21fb0 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 20  >db;.  Db *pDb; 
21fb1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
21fb2 68 65 20 73 70 65 63 69 66 69 63 20 74 61 62 6c  he specific tabl
21fb3 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
21fb4 20 69 6e 64 65 78 65 64 20 64 61 74 61 62 61 73   indexed databas
21fb5 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  e */.  int iDb; 
21fb6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
21fb7 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
21fb8 62 61 73 65 20 74 68 61 74 20 69 73 20 62 65 69  base that is bei
21fb9 6e 67 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  ng written */.  
21fba 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30  Token *pName = 0
21fbb 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66  ;    /* Unqualif
21fbc 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  ied name of the 
21fbd 69 6e 64 65 78 20 74 6f 20 63 72 65 61 74 65 20  index to create 
21fbe 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  */.  struct Expr
21fbf 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 69 73 74  List_item *pList
21fc0 49 74 65 6d 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f  Item; /* For loo
21fc1 70 69 6e 67 20 6f 76 65 72 20 70 4c 69 73 74 20  ping over pList 
21fc2 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20  */.  int nCol;. 
21fc3 20 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b   int nExtra = 0;
21fc4 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b  .  char *zExtra;
21fc5 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
21fc6 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
21fc7 6f 63 46 61 69 6c 65 64 20 7c 7c 20 49 4e 5f 44  ocFailed || IN_D
21fc8 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20  ECLARE_VTAB ){. 
21fc9 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
21fca 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 0a  ate_index;.  }..
21fcb 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74    /*.  ** Find t
21fcc 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73  he table that is
21fcd 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
21fce 20 52 65 74 75 72 6e 20 65 61 72 6c 79 20 69 66   Return early if
21fcf 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f   not found..  */
21fd0 0a 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21  .  if( pTblName!
21fd1 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 55 73  =0 ){..    /* Us
21fd2 65 20 74 68 65 20 74 77 6f 2d 70 61 72 74 20 69  e the two-part i
21fd3 6e 64 65 78 20 6e 61 6d 65 20 74 6f 20 64 65 74  ndex name to det
21fd4 65 72 6d 69 6e 65 20 74 68 65 20 64 61 74 61 62  ermine the datab
21fd5 61 73 65 20 0a 20 20 20 20 2a 2a 20 74 6f 20 73  ase .    ** to s
21fd6 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 74 61  earch for the ta
21fd7 62 6c 65 2e 20 27 46 69 78 27 20 74 68 65 20 74  ble. 'Fix' the t
21fd8 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68 69  able name to thi
21fd9 73 20 64 62 0a 20 20 20 20 2a 2a 20 62 65 66 6f  s db.    ** befo
21fda 72 65 20 6c 6f 6f 6b 69 6e 67 20 75 70 20 74 68  re looking up th
21fdb 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a  e table..    */.
21fdc 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d      assert( pNam
21fdd 65 31 20 26 26 20 70 4e 61 6d 65 32 20 29 3b 0a  e1 && pName2 );.
21fde 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
21fdf 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61  3TwoPartName(pPa
21fe0 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61  rse, pName1, pNa
21fe1 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20  me2, &pName);.  
21fe2 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 67 6f    if( iDb<0 ) go
21fe3 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
21fe4 6e 64 65 78 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ndex;..#ifndef S
21fe5 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44  QLITE_OMIT_TEMPD
21fe6 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  B.    /* If the 
21fe7 69 6e 64 65 78 20 6e 61 6d 65 20 77 61 73 20 75  index name was u
21fe8 6e 71 75 61 6c 69 66 69 65 64 2c 20 63 68 65 63  nqualified, chec
21fe9 6b 20 69 66 20 74 68 65 20 74 68 65 20 74 61 62  k if the the tab
21fea 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 61 20 74  le.    ** is a t
21feb 65 6d 70 20 74 61 62 6c 65 2e 20 49 66 20 73 6f  emp table. If so
21fec 2c 20 73 65 74 20 74 68 65 20 64 61 74 61 62 61  , set the databa
21fed 73 65 20 74 6f 20 31 2e 20 44 6f 20 6e 6f 74 20  se to 1. Do not 
21fee 64 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69  do this.    ** i
21fef 66 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 20 61  f initialising a
21ff0 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
21ff1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
21ff2 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
21ff3 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  ){.      pTab = 
21ff4 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
21ff5 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 62  okup(pParse, pTb
21ff6 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66  lName);.      if
21ff7 28 20 70 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d  ( pName2 && pNam
21ff8 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62  e2->n==0 && pTab
21ff9 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d   && pTab->pSchem
21ffa 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  a==db->aDb[1].pS
21ffb 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20  chema ){.       
21ffc 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20   iDb = 1;.      
21ffd 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
21ffe 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
21fff 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50  ixInit(&sFix, pP
22000 61 72 73 65 2c 20 69 44 62 2c 20 22 69 6e 64 65  arse, iDb, "inde
22001 78 22 2c 20 70 4e 61 6d 65 29 20 26 26 0a 20 20  x", pName) &&.  
22002 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 69 78        sqlite3Fix
22003 53 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70  SrcList(&sFix, p
22004 54 62 6c 4e 61 6d 65 29 0a 20 20 20 20 29 7b 0a  TblName).    ){.
22005 20 20 20 20 20 20 2f 2a 20 42 65 63 61 75 73 65        /* Because
22006 20 74 68 65 20 70 61 72 73 65 72 20 63 6f 6e 73   the parser cons
22007 74 72 75 63 74 73 20 70 54 62 6c 4e 61 6d 65 20  tructs pTblName 
22008 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 69 64  from a single id
22009 65 6e 74 69 66 69 65 72 2c 0a 20 20 20 20 20 20  entifier,.      
2200a 2a 2a 20 73 71 6c 69 74 65 33 46 69 78 53 72 63  ** sqlite3FixSrc
2200b 4c 69 73 74 20 63 61 6e 20 6e 65 76 65 72 20 66  List can never f
2200c 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73  ail. */.      as
2200d 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20  sert(0);.    }. 
2200e 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
2200f 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
22010 72 73 65 2c 20 30 2c 20 70 54 62 6c 4e 61 6d 65  rse, 0, pTblName
22011 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 0a 20  ->a[0].zName, . 
22012 20 20 20 20 20 20 20 70 54 62 6c 4e 61 6d 65 2d         pTblName-
22013 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29  >a[0].zDatabase)
22014 3b 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20  ;.    if( !pTab 
22015 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
22016 6c 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  led ) goto exit_
22017 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
22018 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
22019 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3d 3d  b[iDb].pSchema==
2201a 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
2201b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
2201c 73 65 72 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29  sert( pName==0 )
2201d 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50 61  ;.    pTab = pPa
2201e 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
2201f 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 20      if( !pTab ) 
22020 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
22021 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 44 62 20  _index;.    iDb 
22022 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
22023 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
22024 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7d 0a 20  >pSchema);.  }. 
22025 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
22026 69 44 62 5d 3b 0a 0a 20 20 69 66 28 20 70 54 61  iDb];..  if( pTa
22027 62 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e  b==0 || pParse->
22028 6e 45 72 72 20 29 20 67 6f 74 6f 20 65 78 69 74  nErr ) goto exit
22029 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
2202a 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
2202b 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65  ICmp(pTab->zName
2202c 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d  , "sqlite_", 7)=
2202d 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20 6d 65  =0 .       && me
2202e 6d 63 6d 70 28 26 70 54 61 62 2d 3e 7a 4e 61 6d  mcmp(&pTab->zNam
2202f 65 5b 37 5d 2c 22 61 6c 74 65 72 74 61 62 5f 22  e[7],"altertab_"
22030 2c 39 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ,9)!=0 ){.    sq
22031 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
22032 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
22033 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78  may not be index
22034 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ed", pTab->zName
22035 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
22036 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
22037 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
22038 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66  E_OMIT_VIEW.  if
22039 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
2203a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2203b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2203c 76 69 65 77 73 20 6d 61 79 20 6e 6f 74 20 62 65  views may not be
2203d 20 69 6e 64 65 78 65 64 22 29 3b 0a 20 20 20 20   indexed");.    
2203e 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
2203f 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64  _index;.  }.#end
22040 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
22041 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
22042 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74  BLE.  if( IsVirt
22043 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
22044 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22045 28 70 50 61 72 73 65 2c 20 22 76 69 72 74 75 61  (pParse, "virtua
22046 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74  l tables may not
22047 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a 20   be indexed");. 
22048 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
22049 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23  ate_index;.  }.#
2204a 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  endif..  /*.  **
2204b 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f   Find the name o
2204c 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 4d 61  f the index.  Ma
2204d 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73  ke sure there is
2204e 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 6e 6f   not already ano
2204f 74 68 65 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20  ther.  ** index 
22050 6f 72 20 74 61 62 6c 65 20 77 69 74 68 20 74 68  or table with th
22051 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 20 0a 20  e same name.  . 
22052 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 69   **.  ** Excepti
22053 6f 6e 3a 20 20 49 66 20 77 65 20 61 72 65 20 72  on:  If we are r
22054 65 61 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 73  eading the names
22055 20 6f 66 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e   of permanent in
22056 64 69 63 65 73 20 66 72 6f 6d 20 74 68 65 0a 20  dices from the. 
22057 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65   ** sqlite_maste
22058 72 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 65  r table (because
22059 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
2205a 65 73 73 20 63 68 61 6e 67 65 64 20 74 68 65 20  ess changed the 
2205b 73 63 68 65 6d 61 29 20 61 6e 64 0a 20 20 2a 2a  schema) and.  **
2205c 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 64 65   one of the inde
2205d 78 20 6e 61 6d 65 73 20 63 6f 6c 6c 69 64 65 73  x names collides
2205e 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 20 6f   with the name o
2205f 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  f a temporary ta
22060 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65  ble or.  ** inde
22061 78 2c 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20  x, then we will 
22062 63 6f 6e 74 69 6e 75 65 20 74 6f 20 70 72 6f 63  continue to proc
22063 65 73 73 20 74 68 69 73 20 69 6e 64 65 78 2e 0a  ess this index..
22064 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 4e 61    **.  ** If pNa
22065 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74  me==0 it means t
22066 68 61 74 20 77 65 20 61 72 65 0a 20 20 2a 2a 20  hat we are.  ** 
22067 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 70  dealing with a p
22068 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 55 4e  rimary key or UN
22069 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e  IQUE constraint.
2206a 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76    We have to inv
2206b 65 6e 74 20 6f 75 72 0a 20 20 2a 2a 20 6f 77 6e  ent our.  ** own
2206c 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   name..  */.  if
2206d 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a  ( pName ){.    z
2206e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
2206f 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
22070 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  pName);.    if( 
22071 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
22072 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
22073 72 73 65 29 20 29 20 67 6f 74 6f 20 65 78 69 74  rse) ) goto exit
22074 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
22075 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20     if( zName==0 
22076 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61  ) goto exit_crea
22077 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 66  te_index;.    if
22078 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
22079 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e  ite3CheckObjectN
2207a 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d  ame(pParse, zNam
2207b 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  e) ){.      goto
2207c 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
2207d 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ex;.    }.    if
2207e 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
2207f 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 53 51   ){.      if( SQ
22080 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
22081 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
22082 65 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63  e) ) goto exit_c
22083 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
22084 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
22085 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d  ndTable(db, zNam
22086 65 2c 20 30 29 21 3d 30 20 29 7b 0a 20 20 20 20  e, 0)!=0 ){.    
22087 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
22088 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 68 65  Msg(pParse, "the
22089 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
2208a 74 61 62 6c 65 20 6e 61 6d 65 64 20 25 73 22 2c  table named %s",
2208b 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
2208c 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2208d 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d  e_index;.      }
2208e 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
2208f 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
22090 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 44 62 2d 3e  db, zName, pDb->
22091 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20  zName)!=0 ){.   
22092 20 20 20 69 66 28 20 21 69 66 4e 6f 74 45 78 69     if( !ifNotExi
22093 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  st ){.        sq
22094 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
22095 61 72 73 65 2c 20 22 69 6e 64 65 78 20 25 73 20  arse, "index %s 
22096 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c  already exists",
22097 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d   zName);.      }
22098 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
22099 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
2209a 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
2209b 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 49 6e 64    int n;.    Ind
2209c 65 78 20 2a 70 4c 6f 6f 70 3b 0a 20 20 20 20 66  ex *pLoop;.    f
2209d 6f 72 28 70 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70  or(pLoop=pTab->p
2209e 49 6e 64 65 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f  Index, n=1; pLoo
2209f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e  p; pLoop=pLoop->
220a0 70 4e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20  pNext, n++){}.  
220a1 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
220a2 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71  3MPrintf(db, "sq
220a3 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 25  lite_autoindex_%
220a4 73 5f 25 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  s_%d", pTab->zNa
220a5 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 69 66 28 20  me, n);.    if( 
220a6 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  zName==0 ){.    
220a7 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
220a8 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
220a9 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
220aa 66 6f 72 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  for authorizatio
220ab 6e 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 69  n to create an i
220ac 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  ndex..  */.#ifnd
220ad 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
220ae 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b  UTHORIZATION.  {
220af 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
220b0 2a 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d  *zDb = pDb->zNam
220b1 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  e;.    if( sqlit
220b2 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
220b3 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52  se, SQLITE_INSER
220b4 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  T, SCHEMA_TABLE(
220b5 69 44 62 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b  iDb), 0, zDb) ){
220b6 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
220b7 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
220b8 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 53 51 4c     }.    i = SQL
220b9 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58  ITE_CREATE_INDEX
220ba 3b 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f  ;.    if( !OMIT_
220bb 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31  TEMPDB && iDb==1
220bc 20 29 20 69 20 3d 20 53 51 4c 49 54 45 5f 43 52   ) i = SQLITE_CR
220bd 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 3b  EATE_TEMP_INDEX;
220be 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
220bf 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
220c0 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62  , i, zName, pTab
220c1 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b  ->zName, zDb) ){
220c2 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
220c3 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
220c4 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
220c5 0a 20 20 2f 2a 20 49 66 20 70 4c 69 73 74 3d 3d  .  /* If pList==
220c6 30 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73  0, it means this
220c7 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
220c8 6c 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 70 72  led to make a pr
220c9 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20 6f  imary.  ** key o
220ca 75 74 20 6f 66 20 74 68 65 20 6c 61 73 74 20 63  ut of the last c
220cb 6f 6c 75 6d 6e 20 61 64 64 65 64 20 74 6f 20 74  olumn added to t
220cc 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63  he table under c
220cd 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 2a  onstruction..  *
220ce 2a 20 53 6f 20 63 72 65 61 74 65 20 61 20 66 61  * So create a fa
220cf 6b 65 20 6c 69 73 74 20 74 6f 20 73 69 6d 75 6c  ke list to simul
220d0 61 74 65 20 74 68 69 73 2e 0a 20 20 2a 2f 0a 20  ate this..  */. 
220d1 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b   if( pList==0 ){
220d2 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 7a 20 3d 20  .    nullId.z = 
220d3 28 75 38 2a 29 70 54 61 62 2d 3e 61 43 6f 6c 5b  (u8*)pTab->aCol[
220d4 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 5d 2e 7a 4e  pTab->nCol-1].zN
220d5 61 6d 65 3b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e  ame;.    nullId.
220d6 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
220d7 6e 33 30 28 28 63 68 61 72 2a 29 6e 75 6c 6c 49  n30((char*)nullI
220d8 64 2e 7a 29 3b 0a 20 20 20 20 70 4c 69 73 74 20  d.z);.    pList 
220d9 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
220da 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20  tAppend(pParse, 
220db 30 2c 20 30 2c 20 26 6e 75 6c 6c 49 64 29 3b 0a  0, 0, &nullId);.
220dc 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30      if( pList==0
220dd 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
220de 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 70  ate_index;.    p
220df 4c 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f  List->a[0].sortO
220e0 72 64 65 72 20 3d 20 28 75 38 29 73 6f 72 74 4f  rder = (u8)sortO
220e1 72 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rder;.  }..  /* 
220e2 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  Figure out how m
220e3 61 6e 79 20 62 79 74 65 73 20 6f 66 20 73 70 61  any bytes of spa
220e4 63 65 20 61 72 65 20 72 65 71 75 69 72 65 64 20  ce are required 
220e5 74 6f 20 73 74 6f 72 65 20 65 78 70 6c 69 63 69  to store explici
220e6 74 6c 79 0a 20 20 2a 2a 20 73 70 65 63 69 66 69  tly.  ** specifi
220e7 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
220e8 75 65 6e 63 65 20 6e 61 6d 65 73 2e 0a 20 20 2a  uence names..  *
220e9 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
220ea 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
220eb 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78  ){.    Expr *pEx
220ec 70 72 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20  pr;.    CollSeq 
220ed 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20  *pColl;.    if( 
220ee 28 70 45 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e  (pExpr = pList->
220ef 61 5b 69 5d 2e 70 45 78 70 72 29 21 3d 30 20 26  a[i].pExpr)!=0 &
220f0 26 20 28 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72  & (pColl = pExpr
220f1 2d 3e 70 43 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20  ->pColl)!=0 ){. 
220f2 20 20 20 20 20 6e 45 78 74 72 61 20 2b 3d 20 28       nExtra += (
220f3 31 20 2b 20 73 71 6c 69 74 65 33 53 74 72 6c 65  1 + sqlite3Strle
220f4 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  n30(pColl->zName
220f5 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ));.    }.  }.. 
220f6 20 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61   /* .  ** Alloca
220f7 74 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72  te the index str
220f8 75 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20  ucture. .  */.  
220f9 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
220fa 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
220fb 20 20 6e 43 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e    nCol = pList->
220fc 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64 65 78 20  nExpr;.  pIndex 
220fd 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
220fe 63 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20  cZero(db, .     
220ff 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 20 2b   sizeof(Index) +
22100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22101 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65   Index structure
22102 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f    */.      sizeo
22103 66 28 69 6e 74 29 2a 6e 43 6f 6c 20 2b 20 20 20  f(int)*nCol +   
22104 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
22105 2e 61 69 43 6f 6c 75 6d 6e 20 20 20 2a 2f 0a 20  .aiColumn   */. 
22106 20 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29       sizeof(int)
22107 2a 28 6e 43 6f 6c 2b 31 29 20 2b 20 20 20 20 20  *(nCol+1) +     
22108 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 52 6f 77    /* Index.aiRow
22109 45 73 74 20 20 20 2a 2f 0a 20 20 20 20 20 20 73  Est   */.      s
2210a 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 6e 43  izeof(char *)*nC
2210b 6f 6c 20 2b 20 20 20 20 20 20 20 20 2f 2a 20 49  ol +        /* I
2210c 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20 20 20 20 20  ndex.azColl     
2210d 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  */.      sizeof(
2210e 75 38 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20  u8)*nCol +      
2210f 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61        /* Index.a
22110 53 6f 72 74 4f 72 64 65 72 20 2a 2f 0a 20 20 20  SortOrder */.   
22111 20 20 20 6e 4e 61 6d 65 20 2b 20 31 20 2b 20 20     nName + 1 +  
22112 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22113 2f 2a 20 49 6e 64 65 78 2e 7a 4e 61 6d 65 20 20  /* Index.zName  
22114 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6e 45 78      */.      nEx
22115 74 72 61 20 20 20 20 20 20 20 20 20 20 20 20 20  tra             
22116 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
22117 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
22118 6e 61 6d 65 73 20 2a 2f 0a 20 20 29 3b 0a 20 20  names */.  );.  
22119 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2211a 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
2211b 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
2211c 65 78 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78  ex;.  }.  pIndex
2211d 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 28 63 68 61 72  ->azColl = (char
2211e 2a 2a 29 28 26 70 49 6e 64 65 78 5b 31 5d 29 3b  **)(&pIndex[1]);
2211f 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c  .  pIndex->aiCol
22120 75 6d 6e 20 3d 20 28 69 6e 74 20 2a 29 28 26 70  umn = (int *)(&p
22121 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 43  Index->azColl[nC
22122 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  ol]);.  pIndex->
22123 61 69 52 6f 77 45 73 74 20 3d 20 28 75 6e 73 69  aiRowEst = (unsi
22124 67 6e 65 64 20 2a 29 28 26 70 49 6e 64 65 78 2d  gned *)(&pIndex-
22125 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 29  >aiColumn[nCol])
22126 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72  ;.  pIndex->aSor
22127 74 4f 72 64 65 72 20 3d 20 28 75 38 20 2a 29 28  tOrder = (u8 *)(
22128 26 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73  &pIndex->aiRowEs
22129 74 5b 6e 43 6f 6c 2b 31 5d 29 3b 0a 20 20 70 49  t[nCol+1]);.  pI
2212a 6e 64 65 78 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63  ndex->zName = (c
2212b 68 61 72 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e  har *)(&pIndex->
2212c 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d  aSortOrder[nCol]
2212d 29 3b 0a 20 20 7a 45 78 74 72 61 20 3d 20 28 63  );.  zExtra = (c
2212e 68 61 72 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e  har *)(&pIndex->
2212f 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 2b 31 5d 29 3b  zName[nName+1]);
22130 0a 20 20 6d 65 6d 63 70 79 28 70 49 6e 64 65 78  .  memcpy(pIndex
22131 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20  ->zName, zName, 
22132 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 70 49 6e 64  nName+1);.  pInd
22133 65 78 2d 3e 70 54 61 62 6c 65 20 3d 20 70 54 61  ex->pTable = pTa
22134 62 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6e 43 6f  b;.  pIndex->nCo
22135 6c 75 6d 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45  lumn = pList->nE
22136 78 70 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6f  xpr;.  pIndex->o
22137 6e 45 72 72 6f 72 20 3d 20 28 75 38 29 6f 6e 45  nError = (u8)onE
22138 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  rror;.  pIndex->
22139 61 75 74 6f 49 6e 64 65 78 20 3d 20 28 75 38 29  autoIndex = (u8)
2213a 28 70 4e 61 6d 65 3d 3d 30 29 3b 0a 20 20 70 49  (pName==0);.  pI
2213b 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 20 3d 20  ndex->pSchema = 
2213c 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
2213d 68 65 6d 61 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  hema;..  /* Chec
2213e 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 73  k to see if we s
2213f 68 6f 75 6c 64 20 68 6f 6e 6f 72 20 44 45 53 43  hould honor DESC
22140 20 72 65 71 75 65 73 74 73 20 6f 6e 20 69 6e 64   requests on ind
22141 65 78 20 63 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a  ex columns.  */.
22142 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65    if( pDb->pSche
22143 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e  ma->file_format>
22144 3d 34 20 29 7b 0a 20 20 20 20 73 6f 72 74 4f 72  =4 ){.    sortOr
22145 64 65 72 4d 61 73 6b 20 3d 20 2d 31 3b 20 20 20  derMask = -1;   
22146 2f 2a 20 48 6f 6e 6f 72 20 44 45 53 43 20 2a 2f  /* Honor DESC */
22147 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 6f  .  }else{.    so
22148 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20 30 3b  rtOrderMask = 0;
22149 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 44 45      /* Ignore DE
2214a 53 43 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20  SC */.  }..  /* 
2214b 53 63 61 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f  Scan the names o
2214c 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66  f the columns of
2214d 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
2214e 20 69 6e 64 65 78 65 64 20 61 6e 64 0a 20 20 2a   indexed and.  *
2214f 2a 20 6c 6f 61 64 20 74 68 65 20 63 6f 6c 75 6d  * load the colum
22150 6e 20 69 6e 64 69 63 65 73 20 69 6e 74 6f 20 74  n indices into t
22151 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  he Index structu
22152 72 65 2e 20 20 52 65 70 6f 72 74 20 61 6e 20 65  re.  Report an e
22153 72 72 6f 72 0a 20 20 2a 2a 20 69 66 20 61 6e 79  rror.  ** if any
22154 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 66   column is not f
22155 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ound..  */.  for
22156 28 69 3d 30 2c 20 70 4c 69 73 74 49 74 65 6d 3d  (i=0, pListItem=
22157 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73  pList->a; i<pLis
22158 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  t->nExpr; i++, p
22159 4c 69 73 74 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  ListItem++){.   
2215a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
2215b 6c 4e 61 6d 65 20 3d 20 70 4c 69 73 74 49 74 65  lName = pListIte
2215c 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f  m->zName;.    Co
2215d 6c 75 6d 6e 20 2a 70 54 61 62 43 6f 6c 3b 0a 20  lumn *pTabCol;. 
2215e 20 20 20 69 6e 74 20 72 65 71 75 65 73 74 65 64     int requested
2215f 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 63  SortOrder;.    c
22160 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20  har *zColl;     
22161 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22162 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   Collation seque
22163 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20  nce name */..   
22164 20 66 6f 72 28 6a 3d 30 2c 20 70 54 61 62 43 6f   for(j=0, pTabCo
22165 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c  l=pTab->aCol; j<
22166 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c  pTab->nCol; j++,
22167 20 70 54 61 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20   pTabCol++){.   
22168 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
22169 72 49 43 6d 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20  rICmp(zColName, 
2216a 70 54 61 62 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d  pTabCol->zName)=
2216b 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
2216c 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61  }.    if( j>=pTa
2216d 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
2216e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2216f 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
22170 25 73 20 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e  %s has no column
22171 20 6e 61 6d 65 64 20 25 73 22 2c 0a 20 20 20 20   named %s",.    
22172 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
22173 20 7a 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 20 20   zColName);.    
22174 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
22175 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
22176 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 20 41 64      /* TODO:  Ad
22177 64 20 61 20 74 65 73 74 20 74 6f 20 6d 61 6b 65  d a test to make
22178 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73   sure that the s
22179 61 6d 65 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f  ame column is no
2217a 74 20 6e 61 6d 65 64 0a 20 20 20 20 2a 2a 20 6d  t named.    ** m
2217b 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69  ore than once wi
2217c 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 69 6e  thin the same in
2217d 64 65 78 2e 20 20 4f 6e 6c 79 20 74 68 65 20 66  dex.  Only the f
2217e 69 72 73 74 20 69 6e 73 74 61 6e 63 65 20 6f 66  irst instance of
2217f 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6c 75  .    ** the colu
22180 6d 6e 20 77 69 6c 6c 20 65 76 65 72 20 62 65 20  mn will ever be 
22181 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 74 69  used by the opti
22182 6d 69 7a 65 72 2e 20 20 4e 6f 74 65 20 74 68 61  mizer.  Note tha
22183 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 20 20  t using the.    
22184 2a 2a 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d  ** same column m
22185 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 63 61  ore than once ca
22186 6e 6e 6f 74 20 62 65 20 61 6e 20 65 72 72 6f 72  nnot be an error
22187 20 62 65 63 61 75 73 65 20 74 68 61 74 20 77 6f   because that wo
22188 75 6c 64 20 0a 20 20 20 20 2a 2a 20 62 72 65 61  uld .    ** brea
22189 6b 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  k backwards comp
2218a 61 74 69 62 69 6c 69 74 79 20 2d 20 69 74 20 6e  atibility - it n
2218b 65 65 64 73 20 74 6f 20 62 65 20 61 20 77 61 72  eeds to be a war
2218c 6e 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ning..    */.   
2218d 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d   pIndex->aiColum
2218e 6e 5b 69 5d 20 3d 20 6a 3b 0a 20 20 20 20 69 66  n[i] = j;.    if
2218f 28 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78  ( pListItem->pEx
22190 70 72 20 26 26 20 70 4c 69 73 74 49 74 65 6d 2d  pr && pListItem-
22191 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b  >pExpr->pColl ){
22192 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
22193 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d  ListItem->pExpr-
22194 3e 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20  >pColl );.      
22195 7a 43 6f 6c 6c 20 3d 20 7a 45 78 74 72 61 3b 0a  zColl = zExtra;.
22196 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
22197 70 72 69 6e 74 66 28 6e 45 78 74 72 61 2c 20 7a  printf(nExtra, z
22198 45 78 74 72 61 2c 20 22 25 73 22 2c 20 70 4c 69  Extra, "%s", pLi
22199 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70  stItem->pExpr->p
2219a 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Coll->zName);.  
2219b 20 20 20 20 7a 45 78 74 72 61 20 2b 3d 20 28 73      zExtra += (s
2219c 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2219d 43 6f 6c 6c 29 20 2b 20 31 29 3b 0a 20 20 20 20  Coll) + 1);.    
2219e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 43 6f  }else{.      zCo
2219f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ll = pTab->aCol[
221a0 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  j].zColl;.      
221a1 69 66 28 20 21 7a 43 6f 6c 6c 20 29 7b 0a 20 20  if( !zColl ){.  
221a2 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 64 62        zColl = db
221a3 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e 7a 4e 61  ->pDfltColl->zNa
221a4 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  me;.      }.    
221a5 7d 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69  }.    if( !db->i
221a6 6e 69 74 2e 62 75 73 79 20 26 26 20 21 73 71 6c  nit.busy && !sql
221a7 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
221a8 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c  q(pParse, zColl,
221a9 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 67 6f   -1) ){.      go
221aa 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
221ab 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
221ac 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  pIndex->azColl[i
221ad 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 20 20 72  ] = zColl;.    r
221ae 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65  equestedSortOrde
221af 72 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 73  r = pListItem->s
221b0 6f 72 74 4f 72 64 65 72 20 26 20 73 6f 72 74 4f  ortOrder & sortO
221b1 72 64 65 72 4d 61 73 6b 3b 0a 20 20 20 20 70 49  rderMask;.    pI
221b2 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  ndex->aSortOrder
221b3 5b 69 5d 20 3d 20 28 75 38 29 72 65 71 75 65 73  [i] = (u8)reques
221b4 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20  tedSortOrder;.  
221b5 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 66 61 75  }.  sqlite3Defau
221b6 6c 74 52 6f 77 45 73 74 28 70 49 6e 64 65 78 29  ltRowEst(pIndex)
221b7 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70  ;..  if( pTab==p
221b8 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
221b9 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
221ba 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e  routine has been
221bb 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
221bc 65 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 69  e an automatic i
221bd 6e 64 65 78 20 61 73 20 61 0a 20 20 20 20 2a 2a  ndex as a.    **
221be 20 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 49   result of a PRI
221bf 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
221c0 55 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 63  UE clause on a c
221c1 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
221c2 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 50 52  , or.    ** a PR
221c3 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49  IMARY KEY or UNI
221c4 51 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c 6f  QUE clause follo
221c5 77 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20  wing the column 
221c6 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 20  definitions..   
221c7 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a   ** i.e. one of:
221c8 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
221c9 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 20  REATE TABLE t(x 
221ca 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b  PRIMARY KEY, y);
221cb 0a 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54  .    ** CREATE T
221cc 41 42 4c 45 20 74 28 78 2c 20 79 2c 20 55 4e 49  ABLE t(x, y, UNI
221cd 51 55 45 28 78 2c 20 79 29 29 3b 0a 20 20 20 20  QUE(x, y));.    
221ce 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68 65 72  **.    ** Either
221cf 20 77 61 79 2c 20 63 68 65 63 6b 20 74 6f 20 73   way, check to s
221d0 65 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ee if the table 
221d1 61 6c 72 65 61 64 79 20 68 61 73 20 73 75 63 68  already has such
221d2 20 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a 20 20   an index. If.  
221d3 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 62    ** so, don't b
221d4 6f 74 68 65 72 20 63 72 65 61 74 69 6e 67 20 74  other creating t
221d5 68 69 73 20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e  his one. This on
221d6 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 0a 20 20  ly applies to.  
221d7 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c    ** automatical
221d8 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64 69 63  ly created indic
221d9 65 73 2e 20 55 73 65 72 73 20 63 61 6e 20 64 6f  es. Users can do
221da 20 61 73 20 74 68 65 79 20 77 69 73 68 20 77 69   as they wish wi
221db 74 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c 69 63  th.    ** explic
221dc 69 74 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20  it indices..    
221dd 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  */.    Index *pI
221de 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78  dx;.    for(pIdx
221df 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
221e0 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
221e1 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e  pNext){.      in
221e2 74 20 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t k;.      asser
221e3 74 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  t( pIdx->onError
221e4 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20  !=OE_None );.   
221e5 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
221e6 3e 61 75 74 6f 49 6e 64 65 78 20 29 3b 0a 20 20  >autoIndex );.  
221e7 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64      assert( pInd
221e8 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  ex->onError!=OE_
221e9 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20 20 20 20 69  None );..      i
221ea 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  f( pIdx->nColumn
221eb 21 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d  !=pIndex->nColum
221ec 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  n ) continue;.  
221ed 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70      for(k=0; k<p
221ee 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b  Idx->nColumn; k+
221ef 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  +){.        cons
221f0 74 20 63 68 61 72 20 2a 7a 31 20 3d 20 70 49 64  t char *z1 = pId
221f1 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20  x->azColl[k];.  
221f2 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
221f3 20 2a 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61   *z2 = pIndex->a
221f4 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20  zColl[k];.      
221f5 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f    if( pIdx->aiCo
221f6 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d  lumn[k]!=pIndex-
221f7 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62  >aiColumn[k] ) b
221f8 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
221f9 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64  ( pIdx->aSortOrd
221fa 65 72 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61  er[k]!=pIndex->a
221fb 53 6f 72 74 4f 72 64 65 72 5b 6b 5d 20 29 20 62  SortOrder[k] ) b
221fc 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
221fd 28 20 7a 31 21 3d 7a 32 20 26 26 20 73 71 6c 69  ( z1!=z2 && sqli
221fe 74 65 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a  te3StrICmp(z1, z
221ff 32 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  2) ) break;.    
22200 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d    }.      if( k=
22201 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  =pIdx->nColumn )
22202 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  {.        if( pI
22203 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e  dx->onError!=pIn
22204 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a  dex->onError ){.
22205 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
22206 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65  s constraint cre
22207 61 74 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e  ates the same in
22208 64 65 78 20 61 73 20 61 20 70 72 65 76 69 6f 75  dex as a previou
22209 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  s.          ** c
2220a 6f 6e 73 74 72 61 69 6e 74 20 73 70 65 63 69 66  onstraint specif
2220b 69 65 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e  ied somewhere in
2220c 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c   the CREATE TABL
2220d 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  E statement..   
2220e 20 20 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65         ** Howeve
2220f 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43  r the ON CONFLIC
22210 54 20 63 6c 61 75 73 65 73 20 61 72 65 20 64 69  T clauses are di
22211 66 66 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68  fferent. If both
22212 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 20   this .         
22213 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61   ** constraint a
22214 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  nd the previous 
22215 65 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74  equivalent const
22216 72 61 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69  raint have expli
22217 63 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  cit.          **
22218 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61   ON CONFLICT cla
22219 75 73 65 73 20 74 68 69 73 20 69 73 20 61 6e 20  uses this is an 
2221a 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65  error. Otherwise
2221b 2c 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20  , use the.      
2221c 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c      ** explicitl
2221d 79 20 73 70 65 63 69 66 69 65 64 20 62 65 68 61  y specified beha
2221e 76 69 6f 75 72 20 66 6f 72 20 74 68 65 20 69 6e  viour for the in
2221f 64 65 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  dex..          *
22220 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
22221 21 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d  !(pIdx->onError=
22222 3d 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70  =OE_Default || p
22223 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d  Index->onError==
22224 4f 45 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20  OE_Default) ){. 
22225 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22226 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
22227 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
22228 20 20 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67      "conflicting
22229 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61   ON CONFLICT cla
2222a 75 73 65 73 20 73 70 65 63 69 66 69 65 64 22 2c  uses specified",
2222b 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   0);.          }
2222c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
2222d 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  Idx->onError==OE
2222e 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20  _Default ){.    
2222f 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e          pIdx->on
22230 45 72 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e  Error = pIndex->
22231 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  onError;.       
22232 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
22233 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
22234 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
22235 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
22236 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20  ..  /* Link the 
22237 6e 65 77 20 49 6e 64 65 78 20 73 74 72 75 63 74  new Index struct
22238 75 72 65 20 74 6f 20 69 74 73 20 74 61 62 6c 65  ure to its table
22239 20 61 6e 64 20 74 6f 20 74 68 65 20 6f 74 68 65   and to the othe
2223a 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79  r.  ** in-memory
2223b 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
2223c 75 72 65 73 2e 20 0a 20 20 2a 2f 0a 20 20 69 66  ures. .  */.  if
2223d 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ( db->init.busy 
2223e 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 3b  ){.    Index *p;
2223f 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
22240 48 61 73 68 49 6e 73 65 72 74 28 26 70 49 6e 64  HashInsert(&pInd
22241 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78  ex->pSchema->idx
22242 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20 20 20  Hash, .         
22243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22244 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20   pIndex->zName, 
22245 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
22246 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2b 31  pIndex->zName)+1
22247 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
22248 20 20 20 20 20 20 20 20 20 20 20 20 70 49 6e 64              pInd
22249 65 78 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29  ex);.    if( p )
2224a 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2224b 70 3d 3d 70 49 6e 64 65 78 20 29 3b 20 20 2f 2a  p==pIndex );  /*
2224c 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76   Malloc must hav
2224d 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20 20  e failed */.    
2224e 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2224f 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 67 6f  ed = 1;.      go
22250 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
22251 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
22252 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
22253 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
22254 73 3b 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e  s;.    if( pTblN
22255 61 6d 65 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ame!=0 ){.      
22256 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20 64  pIndex->tnum = d
22257 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b  b->init.newTnum;
22258 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
22259 20 49 66 20 74 68 65 20 64 62 2d 3e 69 6e 69 74   If the db->init
2225a 2e 62 75 73 79 20 69 73 20 30 20 74 68 65 6e 20  .busy is 0 then 
2225b 63 72 65 61 74 65 20 74 68 65 20 69 6e 64 65 78  create the index
2225c 20 6f 6e 20 64 69 73 6b 2e 20 20 54 68 69 73 0a   on disk.  This.
2225d 20 20 2a 2a 20 69 6e 76 6f 6c 76 65 73 20 77 72    ** involves wr
2225e 69 74 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20  iting the index 
2225f 69 6e 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20  into the master 
22260 74 61 62 6c 65 20 61 6e 64 20 66 69 6c 6c 69 6e  table and fillin
22261 67 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 69 6e  g in the.  ** in
22262 64 65 78 20 77 69 74 68 20 74 68 65 20 63 75 72  dex with the cur
22263 72 65 6e 74 20 74 61 62 6c 65 20 63 6f 6e 74 65  rent table conte
22264 6e 74 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  nts..  **.  ** T
22265 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  he db->init.busy
22266 20 69 73 20 30 20 77 68 65 6e 20 74 68 65 20 75   is 0 when the u
22267 73 65 72 20 66 69 72 73 74 20 65 6e 74 65 72 73  ser first enters
22268 20 61 20 43 52 45 41 54 45 20 49 4e 44 45 58 20   a CREATE INDEX 
22269 0a 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20  .  ** command.  
2226a 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73  db->init.busy is
2226b 20 31 20 77 68 65 6e 20 61 20 64 61 74 61 62 61   1 when a databa
2226c 73 65 20 69 73 20 6f 70 65 6e 65 64 20 61 6e 64  se is opened and
2226d 20 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 49 4e   .  ** CREATE IN
2226e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 73 20 61  DEX statements a
2226f 72 65 20 72 65 61 64 20 6f 75 74 20 6f 66 20 74  re read out of t
22270 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  he master table.
22271 20 20 49 6e 0a 20 20 2a 2a 20 74 68 65 20 6c 61    In.  ** the la
22272 74 74 65 72 20 63 61 73 65 20 74 68 65 20 69 6e  tter case the in
22273 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69 73  dex already exis
22274 74 73 20 6f 6e 20 64 69 73 6b 2c 20 77 68 69 63  ts on disk, whic
22275 68 20 69 73 20 77 68 79 0a 20 20 2a 2a 20 77 65  h is why.  ** we
22276 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 72   don't want to r
22277 65 63 72 65 61 74 65 20 69 74 2e 0a 20 20 2a 2a  ecreate it..  **
22278 0a 20 20 2a 2a 20 49 66 20 70 54 62 6c 4e 61 6d  .  ** If pTblNam
22279 65 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 68  e==0 it means th
2227a 69 73 20 69 6e 64 65 78 20 69 73 20 67 65 6e 65  is index is gene
2227b 72 61 74 65 64 20 61 73 20 61 20 70 72 69 6d 61  rated as a prima
2227c 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 72 20 55  ry key.  ** or U
2227d 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
2227e 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 41 42   of a CREATE TAB
2227f 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53  LE statement.  S
22280 69 6e 63 65 20 74 68 65 20 74 61 62 6c 65 0a 20  ince the table. 
22281 20 2a 2a 20 68 61 73 20 6a 75 73 74 20 62 65 65   ** has just bee
22282 6e 20 63 72 65 61 74 65 64 2c 20 69 74 20 63 6f  n created, it co
22283 6e 74 61 69 6e 73 20 6e 6f 20 64 61 74 61 20 61  ntains no data a
22284 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e 69  nd the index ini
22285 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a  tialization.  **
22286 20 73 74 65 70 20 63 61 6e 20 62 65 20 73 6b 69   step can be ski
22287 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73  pped..  */.  els
22288 65 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62  e if( db->init.b
22289 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20 56 64  usy==0 ){.    Vd
2228a 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20  be *v;.    char 
2228b 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 69 6e 74 20  *zStmt;.    int 
2228c 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iMem = ++pParse-
2228d 3e 6e 4d 65 6d 3b 0a 0a 20 20 20 20 76 20 3d 20  >nMem;..    v = 
2228e 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2228f 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20  Parse);.    if( 
22290 76 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74  v==0 ) goto exit
22291 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a  _create_index;..
22292 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74  .    /* Create t
22293 68 65 20 72 6f 6f 74 70 61 67 65 20 66 6f 72 20  he rootpage for 
22294 74 68 65 20 69 6e 64 65 78 0a 20 20 20 20 2a 2f  the index.    */
22295 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
22296 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
22297 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b  pParse, 1, iDb);
22298 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22299 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 72 65  AddOp2(v, OP_Cre
2229a 61 74 65 49 6e 64 65 78 2c 20 69 44 62 2c 20 69  ateIndex, iDb, i
2229b 4d 65 6d 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 61  Mem);..    /* Ga
2229c 74 68 65 72 20 74 68 65 20 63 6f 6d 70 6c 65 74  ther the complet
2229d 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52  e text of the CR
2229e 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65  EATE INDEX state
2229f 6d 65 6e 74 20 69 6e 74 6f 0a 20 20 20 20 2a 2a  ment into.    **
222a0 20 74 68 65 20 7a 53 74 6d 74 20 76 61 72 69 61   the zStmt varia
222a1 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ble.    */.    i
222a2 66 28 20 70 53 74 61 72 74 20 26 26 20 70 45 6e  f( pStart && pEn
222a3 64 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  d ){.      /* A 
222a4 6e 61 6d 65 64 20 69 6e 64 65 78 20 77 69 74 68  named index with
222a5 20 61 6e 20 65 78 70 6c 69 63 69 74 20 43 52 45   an explicit CRE
222a6 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ATE INDEX statem
222a7 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74  ent */.      zSt
222a8 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  mt = sqlite3MPri
222a9 6e 74 66 28 64 62 2c 20 22 43 52 45 41 54 45 25  ntf(db, "CREATE%
222aa 73 20 49 4e 44 45 58 20 25 2e 2a 73 22 2c 0a 20  s INDEX %.*s",. 
222ab 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 3d 3d         onError==
222ac 4f 45 5f 4e 6f 6e 65 20 3f 20 22 22 20 3a 20 22  OE_None ? "" : "
222ad 20 55 4e 49 51 55 45 22 2c 0a 20 20 20 20 20 20   UNIQUE",.      
222ae 20 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 4e 61 6d    pEnd->z - pNam
222af 65 2d 3e 7a 20 2b 20 31 2c 0a 20 20 20 20 20 20  e->z + 1,.      
222b0 20 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 20    pName->z);.   
222b1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
222b2 20 41 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 6e   An automatic in
222b3 64 65 78 20 63 72 65 61 74 65 64 20 62 79 20 61  dex created by a
222b4 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
222b5 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e  UNIQUE constrain
222b6 74 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 7a 53  t */.      /* zS
222b7 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  tmt = sqlite3MPr
222b8 69 6e 74 66 28 22 22 29 3b 20 2a 2f 0a 20 20 20  intf(""); */.   
222b9 20 20 20 7a 53 74 6d 74 20 3d 20 30 3b 0a 20 20     zStmt = 0;.  
222ba 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20    }..    /* Add 
222bb 61 6e 20 65 6e 74 72 79 20 69 6e 20 73 71 6c 69  an entry in sqli
222bc 74 65 5f 6d 61 73 74 65 72 20 66 6f 72 20 74 68  te_master for th
222bd 69 73 20 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a  is index.    */.
222be 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
222bf 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a  dParse(pParse, .
222c0 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
222c1 49 4e 54 4f 20 25 51 2e 25 73 20 56 41 4c 55 45  INTO %Q.%s VALUE
222c2 53 28 27 69 6e 64 65 78 27 2c 25 51 2c 25 51 2c  S('index',%Q,%Q,
222c3 23 25 64 2c 25 51 29 3b 22 2c 0a 20 20 20 20 20  #%d,%Q);",.     
222c4 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e     db->aDb[iDb].
222c5 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41  zName, SCHEMA_TA
222c6 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20  BLE(iDb),.      
222c7 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c    pIndex->zName,
222c8 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  .        pTab->z
222c9 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 69 4d  Name,.        iM
222ca 65 6d 2c 0a 20 20 20 20 20 20 20 20 7a 53 74 6d  em,.        zStm
222cb 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c  t.    );.    sql
222cc 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
222cd 53 74 6d 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 46  Stmt);..    /* F
222ce 69 6c 6c 20 74 68 65 20 69 6e 64 65 78 20 77 69  ill the index wi
222cf 74 68 20 64 61 74 61 20 61 6e 64 20 72 65 70 61  th data and repa
222d0 72 73 65 20 74 68 65 20 73 63 68 65 6d 61 2e 20  rse the schema. 
222d1 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70 69 72  Code an OP_Expir
222d2 65 0a 20 20 20 20 2a 2a 20 74 6f 20 69 6e 76 61  e.    ** to inva
222d3 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 2d 63  lidate all pre-c
222d4 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
222d5 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ts..    */.    i
222d6 66 28 20 70 54 62 6c 4e 61 6d 65 20 29 7b 0a 20  f( pTblName ){. 
222d7 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69       sqlite3Refi
222d8 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  llIndex(pParse, 
222d9 70 49 6e 64 65 78 2c 20 69 4d 65 6d 29 3b 0a 20  pIndex, iMem);. 
222da 20 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e       sqlite3Chan
222db 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
222dc 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
222dd 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
222de 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  , OP_ParseSchema
222df 2c 20 69 44 62 2c 20 30 2c 20 30 2c 0a 20 20 20  , iDb, 0, 0,.   
222e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72        sqlite3MPr
222e1 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65 3d 27  intf(db, "name='
222e2 25 71 27 22 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e  %q'", pIndex->zN
222e3 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43  ame), P4_DYNAMIC
222e4 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
222e5 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
222e6 5f 45 78 70 69 72 65 2c 20 30 29 3b 0a 20 20 20  _Expire, 0);.   
222e7 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 68 65   }.  }..  /* Whe
222e8 6e 20 61 64 64 69 6e 67 20 61 6e 20 69 6e 64 65  n adding an inde
222e9 78 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66  x to the list of
222ea 20 69 6e 64 69 63 65 73 20 66 6f 72 20 61 20 74   indices for a t
222eb 61 62 6c 65 2c 20 6d 61 6b 65 0a 20 20 2a 2a 20  able, make.  ** 
222ec 73 75 72 65 20 61 6c 6c 20 69 6e 64 69 63 65 73  sure all indices
222ed 20 6c 61 62 65 6c 65 64 20 4f 45 5f 52 65 70 6c   labeled OE_Repl
222ee 61 63 65 20 63 6f 6d 65 20 61 66 74 65 72 20 61  ace come after a
222ef 6c 6c 20 74 68 6f 73 65 20 6c 61 62 65 6c 65 64  ll those labeled
222f0 0a 20 20 2a 2a 20 4f 45 5f 49 67 6e 6f 72 65 2e  .  ** OE_Ignore.
222f1 20 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73    This is necess
222f2 61 72 79 20 66 6f 72 20 74 68 65 20 63 6f 72 72  ary for the corr
222f3 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
222f4 20 55 50 44 41 54 45 0a 20 20 2a 2a 20 61 6e 64   UPDATE.  ** and
222f5 20 49 4e 53 45 52 54 2e 0a 20 20 2a 2f 0a 20 20   INSERT..  */.  
222f6 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
222f7 79 20 7c 7c 20 70 54 62 6c 4e 61 6d 65 3d 3d 30  y || pTblName==0
222f8 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72   ){.    if( onEr
222f9 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror!=OE_Replace 
222fa 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d  || pTab->pIndex=
222fb 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70  =0.         || p
222fc 54 61 62 2d 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45  Tab->pIndex->onE
222fd 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
222fe 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d  ){.      pIndex-
222ff 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70  >pNext = pTab->p
22300 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 70 54 61  Index;.      pTa
22301 62 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64  b->pIndex = pInd
22302 65 78 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ex;.    }else{. 
22303 20 20 20 20 20 49 6e 64 65 78 20 2a 70 4f 74 68       Index *pOth
22304 65 72 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65  er = pTab->pInde
22305 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  x;.      while( 
22306 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 26 26  pOther->pNext &&
22307 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 2d 3e   pOther->pNext->
22308 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c  onError!=OE_Repl
22309 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ace ){.        p
2230a 4f 74 68 65 72 20 3d 20 70 4f 74 68 65 72 2d 3e  Other = pOther->
2230b 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
2230c 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65       pIndex->pNe
2230d 78 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65  xt = pOther->pNe
2230e 78 74 3b 0a 20 20 20 20 20 20 70 4f 74 68 65 72  xt;.      pOther
2230f 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78  ->pNext = pIndex
22310 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64  ;.    }.    pInd
22311 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ex = 0;.  }..  /
22312 2a 20 43 6c 65 61 6e 20 75 70 20 62 65 66 6f 72  * Clean up befor
22313 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 65 78 69  e exiting */.exi
22314 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3a 0a  t_create_index:.
22315 20 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a    if( pIndex ){.
22316 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
22317 28 70 49 6e 64 65 78 2d 3e 7a 43 6f 6c 41 66 66  (pIndex->zColAff
22318 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
22319 46 72 65 65 28 64 62 2c 20 70 49 6e 64 65 78 29  Free(db, pIndex)
2231a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
2231b 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2231c 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  , pList);.  sqli
2231d 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
2231e 28 64 62 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a  (db, pTblName);.
2231f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
22320 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65  db, zName);.  re
22321 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  turn;.}../*.** G
22322 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
22323 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 66 69  make sure the fi
22324 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65 72  le format number
22325 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69 6e   is at least min
22326 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20 67  Format..** The g
22327 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77 69  enerated code wi
22328 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  ll increase the 
22329 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62  file format numb
2232a 65 72 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  er if necessary.
2232b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2232c 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
2232d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74  inimumFileFormat
2232e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2232f 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69 6e  int iDb, int min
22330 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65 20  Format){.  Vdbe 
22331 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  *v;.  v = sqlite
22332 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22333 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
22334 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65   int r1 = sqlite
22335 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
22336 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 72 32 20  se);.    int r2 
22337 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
22338 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
22339 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 73 71 6c   int j1;.    sql
2233a 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2233b 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  , OP_ReadCookie,
2233c 20 69 44 62 2c 20 72 31 2c 20 31 29 3b 0a 20 20   iDb, r1, 1);.  
2233d 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
2233e 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
2233f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22340 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
22341 67 65 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20  ger, minFormat, 
22342 72 32 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71  r2);.    j1 = sq
22343 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
22344 76 2c 20 4f 50 5f 47 65 2c 20 72 32 2c 20 30 2c  v, OP_Ge, r2, 0,
22345 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   r1);.    sqlite
22346 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22347 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62  P_SetCookie, iDb
22348 2c 20 31 2c 20 72 32 29 3b 0a 20 20 20 20 73 71  , 1, r2);.    sq
22349 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2234a 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71  e(v, j1);.    sq
2234b 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2234c 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2234d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
2234e 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2234f 65 2c 20 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  e, r2);.  }.}../
22350 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e  *.** Fill the In
22351 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 20 61  dex.aiRowEst[] a
22352 72 72 61 79 20 77 69 74 68 20 64 65 66 61 75 6c  rray with defaul
22353 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20  t information - 
22354 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 74  information.** t
22355 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 77  o be used when w
22356 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e 20 74  e have not run t
22357 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  he ANALYZE comma
22358 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 6f 77 45  nd..**.** aiRowE
22359 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f 73 65  st[0] is suppose
2235a 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   to contain the 
2235b 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
2235c 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  ts in the index.
2235d 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20 64 6f 20  .** Since we do 
2235e 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65 73 73 20  not know, guess 
2235f 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61 69 52 6f  1 million.  aiRo
22360 77 45 73 74 5b 31 5d 20 69 73 20 61 6e 20 65 73  wEst[1] is an es
22361 74 69 6d 61 74 65 20 6f 66 20 74 68 65 0a 2a 2a  timate of the.**
22362 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
22363 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  in the table tha
22364 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 74  t match any part
22365 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20  icular value of 
22366 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c  the.** first col
22367 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
22368 2e 20 20 61 69 52 6f 77 45 73 74 5b 32 5d 20 69  .  aiRowEst[2] i
22369 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66  s an estimate of
2236a 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   the number.** o
2236b 66 20 72 6f 77 73 20 74 68 61 74 20 6d 61 74 63  f rows that matc
2236c 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72  h any particular
2236d 20 63 6f 6d 62 69 6e 69 61 74 69 6f 6e 20 6f 66   combiniation of
2236e 20 74 68 65 20 66 69 72 73 74 20 32 20 63 6f 6c   the first 2 col
2236f 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 20 69  umns.** of the i
22370 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f 20 66 6f  ndex.  And so fo
22371 72 74 68 2e 20 20 49 74 20 6d 75 73 74 20 61 6c  rth.  It must al
22372 77 61 79 73 20 62 65 20 74 68 65 20 63 61 73 65  ways be the case
22373 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 20 20 20   that.*.**      
22374 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d       aiRowEst[N]
22375 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d 31 5d 0a  <=aiRowEst[N-1].
22376 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 52  **           aiR
22377 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a  owEst[N]>=1.**.*
22378 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 74 68 61  * Apart from tha
22379 74 2c 20 77 65 20 68 61 76 65 20 6c 69 74 74 6c  t, we have littl
2237a 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 73 69 64  e to go on besid
2237b 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 61 73 20  es intuition as 
2237c 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 6f 77 45  to.** how aiRowE
2237d 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 69  st[] should be i
2237e 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68 65  nitialized.  The
2237f 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 72 61 74   numbers generat
22380 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 65 20 62  ed here.** are b
22381 61 73 65 64 20 6f 6e 20 74 79 70 69 63 61 6c 20  ased on typical 
22382 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 69 6e 20  values found in 
22383 61 63 74 75 61 6c 20 69 6e 64 69 63 65 73 2e 0a  actual indices..
22384 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22385 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
22386 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65  faultRowEst(Inde
22387 78 20 2a 70 49 64 78 29 7b 0a 20 20 75 6e 73 69  x *pIdx){.  unsi
22388 67 6e 65 64 20 2a 61 20 3d 20 70 49 64 78 2d 3e  gned *a = pIdx->
22389 61 69 52 6f 77 45 73 74 3b 0a 20 20 69 6e 74 20  aiRowEst;.  int 
2238a 69 3b 0a 20 20 61 73 73 65 72 74 28 20 61 21 3d  i;.  assert( a!=
2238b 30 20 29 3b 0a 20 20 61 5b 30 5d 20 3d 20 31 30  0 );.  a[0] = 10
2238c 30 30 30 30 30 3b 0a 20 20 66 6f 72 28 69 3d 70  00000;.  for(i=p
2238d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 3e  Idx->nColumn; i>
2238e 3d 35 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 61 5b  =5; i--){.    a[
2238f 69 5d 20 3d 20 35 3b 0a 20 20 7d 0a 20 20 77 68  i] = 5;.  }.  wh
22390 69 6c 65 28 20 69 3e 3d 31 20 29 7b 0a 20 20 20  ile( i>=1 ){.   
22391 20 61 5b 69 5d 20 3d 20 31 31 20 2d 20 69 3b 0a   a[i] = 11 - i;.
22392 20 20 20 20 69 2d 2d 3b 0a 20 20 7d 0a 20 20 69      i--;.  }.  i
22393 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  f( pIdx->onError
22394 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20  !=OE_None ){.   
22395 20 61 5b 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   a[pIdx->nColumn
22396 5d 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ] = 1;.  }.}../*
22397 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
22398 20 77 69 6c 6c 20 64 72 6f 70 20 61 6e 20 65 78   will drop an ex
22399 69 73 74 69 6e 67 20 6e 61 6d 65 64 20 69 6e 64  isting named ind
2239a 65 78 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ex.  This routin
2239b 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 73 20  e.** implements 
2239c 74 68 65 20 44 52 4f 50 20 49 4e 44 45 58 20 73  the DROP INDEX s
2239d 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  tatement..*/.SQL
2239e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2239f 20 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65   sqlite3DropInde
223a0 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  x(Parse *pParse,
223a1 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c   SrcList *pName,
223a2 20 69 6e 74 20 69 66 45 78 69 73 74 73 29 7b 0a   int ifExists){.
223a3 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
223a4 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
223a5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
223a6 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  se->db;.  int iD
223a7 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  b;..  if( pParse
223a8 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
223a9 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
223aa 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
223ab 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61 73  _index;.  }.  as
223ac 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72  sert( pName->nSr
223ad 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53 51  c==1 );.  if( SQ
223ae 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
223af 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
223b0 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  e) ){.    goto e
223b1 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
223b2 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73    }.  pIndex = s
223b3 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
223b4 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e  db, pName->a[0].
223b5 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b  zName, pName->a[
223b6 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
223b7 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29   if( pIndex==0 )
223b8 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78 69  {.    if( !ifExi
223b9 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  sts ){.      sql
223ba 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
223bb 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e  rse, "no such in
223bc 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65 2c  dex: %S", pName,
223bd 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70   0);.    }.    p
223be 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65  Parse->checkSche
223bf 6d 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f  ma = 1;.    goto
223c0 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
223c1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 64  ;.  }.  if( pInd
223c2 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 7b  ex->autoIndex ){
223c3 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
223c4 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e  rMsg(pParse, "in
223c5 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
223c6 69 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20 20  ith UNIQUE ".   
223c7 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20 4b     "or PRIMARY K
223c8 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61  EY constraint ca
223c9 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22  nnot be dropped"
223ca 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  , 0);.    goto e
223cb 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
223cc 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
223cd 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
223ce 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  (db, pIndex->pSc
223cf 68 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20 53  hema);.#ifndef S
223d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
223d1 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
223d2 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49   int code = SQLI
223d3 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a 20  TE_DROP_INDEX;. 
223d4 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
223d5 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b   pIndex->pTable;
223d6 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
223d7 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *zDb = db->aDb[i
223d8 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
223d9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20  onst char *zTab 
223da 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
223db 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Db);.    if( sql
223dc 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
223dd 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c  arse, SQLITE_DEL
223de 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44  ETE, zTab, 0, zD
223df 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
223e0 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
223e1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
223e2 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
223e3 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  iDb ) code = SQL
223e4 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e  ITE_DROP_TEMP_IN
223e5 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  DEX;.    if( sql
223e6 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
223e7 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e 64  arse, code, pInd
223e8 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  ex->zName, pTab-
223e9 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a  >zName, zDb) ){.
223ea 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
223eb 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20  drop_index;.    
223ec 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
223ed 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
223ee 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 69   to remove the i
223ef 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74 68  ndex and from th
223f0 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a  e master table *
223f1 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
223f2 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
223f3 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
223f4 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
223f5 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
223f6 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 1, iDb);.    s
223f7 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
223f8 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
223f9 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51   "DELETE FROM %Q
223fa 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  .%s WHERE name=%
223fb 51 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61  Q",.       db->a
223fc 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
223fd 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
223fe 2c 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d  ,.       pIndex-
223ff 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20  >zName.    );.  
22400 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
22401 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69  dTable(db, "sqli
22402 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e 61  te_stat1", db->a
22403 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29  Db[iDb].zName) )
22404 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e  {.      sqlite3N
22405 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
22406 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45  e,.        "DELE
22407 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74  TE FROM %Q.sqlit
22408 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 69 64  e_stat1 WHERE id
22409 78 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 64  x=%Q",.        d
2240a 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2240b 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  e, pIndex->zName
2240c 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a  .      );.    }.
2240d 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
2240e 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
2240f 69 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72 6f  iDb);.    destro
22410 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
22411 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c 20  , pIndex->tnum, 
22412 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
22413 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
22414 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44 62  P_DropIndex, iDb
22415 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e  , 0, 0, pIndex->
22416 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 0a  zName, 0);.  }..
22417 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3a  exit_drop_index:
22418 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
22419 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d  tDelete(db, pNam
2241a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41 72  e);.}../*.** pAr
2241b 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ray is a pointer
2241c 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20   to an array of 
2241d 6f 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20 6f  objects.  Each o
2241e 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20  bject in the.** 
2241f 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72 79  array is szEntry
22420 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
22421 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   This routine al
22422 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
22423 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65 20 65   object on the e
22424 6e 64 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e  nd of the array.
22425 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79 20  .**.** *pnEntry 
22426 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
22427 20 65 6e 74 72 69 65 73 20 61 6c 72 65 61 64 79   entries already
22428 20 69 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c 6c   in use.  *pnAll
22429 6f 63 20 69 73 0a 2a 2a 20 74 68 65 20 70 72 65  oc is.** the pre
2242a 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
2242b 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72  d size of the ar
2242c 72 61 79 2e 20 20 69 6e 69 74 53 69 7a 65 20 69  ray.  initSize i
2242d 73 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74  s the.** suggest
2242e 65 64 20 69 6e 69 74 69 61 6c 20 61 72 72 61 79  ed initial array
2242f 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   size allocation
22430 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65  ..**.** The inde
22431 78 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74  x of the new ent
22432 72 79 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ry is returned i
22433 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 54  n *pIdx..**.** T
22434 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
22435 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
22436 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 62   the array of ob
22437 6a 65 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a 20  jects.  This.** 
22438 6d 69 67 68 74 20 62 65 20 74 68 65 20 73 61 6d  might be the sam
22439 65 20 61 73 20 74 68 65 20 70 41 72 72 61 79 20  e as the pArray 
2243a 70 61 72 61 6d 65 74 65 72 20 6f 72 20 69 74 20  parameter or it 
2243b 6d 69 67 68 74 20 62 65 20 61 20 64 69 66 66 65  might be a diffe
2243c 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  rent.** pointer 
2243d 69 66 20 74 68 65 20 61 72 72 61 79 20 77 61 73  if the array was
2243e 20 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c   resized..*/.SQL
2243f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
22440 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c   *sqlite3ArrayAl
22441 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74 65  locate(.  sqlite
22442 33 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 43  3 *db,      /* C
22443 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74  onnection to not
22444 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61  ify of malloc fa
22445 69 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 64  ilures */.  void
22446 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f 2a   *pArray,     /*
22447 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74   Array of object
22448 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65 61  s.  Might be rea
22449 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 6e  llocated */.  in
2244a 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 20  t szEntry,      
2244b 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20  /* Size of each 
2244c 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61 72  object in the ar
2244d 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 69  ray */.  int ini
2244e 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53 75  tSize,     /* Su
2244f 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20  ggested initial 
22450 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65  allocation, in e
22451 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  lements */.  int
22452 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20 2f   *pnEntry,     /
22453 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65  * Number of obje
22454 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  cts currently in
22455 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70   use */.  int *p
22456 6e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20 43  nAlloc,     /* C
22457 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74  urrent size of t
22458 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  he allocation, i
22459 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  n elements */.  
2245a 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20 20  int *pIdx       
2245b 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 69    /* Write the i
2245c 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73 6c  ndex of a new sl
2245d 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ot here */.){.  
2245e 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 2a  char *z;.  if( *
2245f 70 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41 6c  pnEntry >= *pnAl
22460 6c 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  loc ){.    void 
22461 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e  *pNew;.    int n
22462 65 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77 53  ewSize;.    newS
22463 69 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63 29  ize = (*pnAlloc)
22464 2a 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a 20  *2 + initSize;. 
22465 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
22466 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70  3DbRealloc(db, p
22467 41 72 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a 73  Array, newSize*s
22468 7a 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28  zEntry);.    if(
22469 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
2246a 20 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20 20    *pIdx = -1;.  
2246b 20 20 20 20 72 65 74 75 72 6e 20 70 41 72 72 61      return pArra
2246c 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 6e  y;.    }.    *pn
2246d 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  Alloc = sqlite3D
2246e 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
2246f 70 4e 65 77 29 2f 73 7a 45 6e 74 72 79 3b 0a 20  pNew)/szEntry;. 
22470 20 20 20 70 41 72 72 61 79 20 3d 20 70 4e 65 77     pArray = pNew
22471 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61  ;.  }.  z = (cha
22472 72 2a 29 70 41 72 72 61 79 3b 0a 20 20 6d 65 6d  r*)pArray;.  mem
22473 73 65 74 28 26 7a 5b 2a 70 6e 45 6e 74 72 79 20  set(&z[*pnEntry 
22474 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30 2c 20 73  * szEntry], 0, s
22475 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70 49 64 78  zEntry);.  *pIdx
22476 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 2b   = *pnEntry;.  +
22477 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 72 65 74  +*pnEntry;.  ret
22478 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d 0a 0a 2f  urn pArray;.}../
22479 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65  *.** Append a ne
2247a 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
2247b 20 67 69 76 65 6e 20 49 64 4c 69 73 74 2e 20 20   given IdList.  
2247c 43 72 65 61 74 65 20 61 20 6e 65 77 20 49 64 4c  Create a new IdL
2247d 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62  ist if.** need b
2247e 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 49  e..**.** A new I
2247f 64 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e 65  dList is returne
22480 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6d 61  d, or NULL if ma
22481 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f  lloc() fails..*/
22482 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22483 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49  IdList *sqlite3I
22484 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69  dListAppend(sqli
22485 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20  te3 *db, IdList 
22486 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70  *pList, Token *p
22487 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 69 3b  Token){.  int i;
22488 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
22489 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73  ){.    pList = s
2248a 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2248b 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 64  ro(db, sizeof(Id
2248c 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28  List) );.    if(
2248d 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
2248e 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d  rn 0;.    pList-
2248f 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 7d  >nAlloc = 0;.  }
22490 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 73 71  .  pList->a = sq
22491 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61  lite3ArrayAlloca
22492 74 65 28 0a 20 20 20 20 20 20 64 62 2c 0a 20 20  te(.      db,.  
22493 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c 0a 20 20      pList->a,.  
22494 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 69 73 74      sizeof(pList
22495 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 20 20 35  ->a[0]),.      5
22496 2c 0a 20 20 20 20 20 20 26 70 4c 69 73 74 2d 3e  ,.      &pList->
22497 6e 49 64 2c 0a 20 20 20 20 20 20 26 70 4c 69 73  nId,.      &pLis
22498 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20  t->nAlloc,.     
22499 20 26 69 0a 20 20 29 3b 0a 20 20 69 66 28 20 69   &i.  );.  if( i
2249a 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <0 ){.    sqlite
2249b 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
2249c 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65  , pList);.    re
2249d 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c  turn 0;.  }.  pL
2249e 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20  ist->a[i].zName 
2249f 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
224a0 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65  mToken(db, pToke
224a1 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69  n);.  return pLi
224a2 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  st;.}../*.** Del
224a3 65 74 65 20 61 6e 20 49 64 4c 69 73 74 2e 0a 2a  ete an IdList..*
224a4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
224a5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64 4c   void sqlite3IdL
224a6 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  istDelete(sqlite
224a7 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70  3 *db, IdList *p
224a8 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  List){.  int i;.
224a9 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
224aa 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
224ab 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64  =0; i<pList->nId
224ac 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
224ad 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
224ae 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  ist->a[i].zName)
224af 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
224b0 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d  bFree(db, pList-
224b1 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  >a);.  sqlite3Db
224b2 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b  Free(db, pList);
224b3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
224b4 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70 4c   the index in pL
224b5 69 73 74 20 6f 66 20 74 68 65 20 69 64 65 6e 74  ist of the ident
224b6 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 64 2e  ifier named zId.
224b7 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a 20 69    Return -1.** i
224b8 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  f not found..*/.
224b9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
224ba 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  nt sqlite3IdList
224bb 49 6e 64 65 78 28 49 64 4c 69 73 74 20 2a 70 4c  Index(IdList *pL
224bc 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ist, const char 
224bd 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69  *zName){.  int i
224be 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
224bf 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
224c0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
224c1 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nId; i++){.   
224c2 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
224c3 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  Cmp(pList->a[i].
224c4 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30  zName, zName)==0
224c5 20 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 7d   ) return i;.  }
224c6 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a  .  return -1;.}.
224c7 0a 2f 2a 0a 2a 2a 20 45 78 70 61 6e 64 20 74 68  ./*.** Expand th
224c8 65 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  e space allocate
224c9 64 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  d for the given 
224ca 53 72 63 4c 69 73 74 20 6f 62 6a 65 63 74 20 62  SrcList object b
224cb 79 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 6e 45  y.** creating nE
224cc 78 74 72 61 20 6e 65 77 20 73 6c 6f 74 73 20 62  xtra new slots b
224cd 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 53 74 61  eginning at iSta
224ce 72 74 2e 20 20 69 53 74 61 72 74 20 69 73 20 7a  rt.  iStart is z
224cf 65 72 6f 20 62 61 73 65 64 2e 0a 2a 2a 20 4e 65  ero based..** Ne
224d0 77 20 73 6c 6f 74 73 20 61 72 65 20 7a 65 72 6f  w slots are zero
224d1 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  ed..**.** For ex
224d2 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 20 61  ample, suppose a
224d3 20 53 72 63 4c 69 73 74 20 69 6e 69 74 69 61 6c   SrcList initial
224d4 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20  ly contains two 
224d5 65 6e 74 72 69 65 73 3a 20 41 2c 42 2e 0a 2a 2a  entries: A,B..**
224d6 20 54 6f 20 61 70 70 65 6e 64 20 33 20 6e 65 77   To append 3 new
224d7 20 65 6e 74 72 69 65 73 20 6f 6e 74 6f 20 74 68   entries onto th
224d8 65 20 65 6e 64 2c 20 64 6f 20 74 68 69 73 3a 0a  e end, do this:.
224d9 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  **.**    sqlite3
224da 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64  SrcListEnlarge(d
224db 62 2c 20 70 53 72 63 6c 69 73 74 2c 20 33 2c 20  b, pSrclist, 3, 
224dc 32 29 3b 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  2);.**.** After 
224dd 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 20 69  the call above i
224de 74 20 77 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 3a  t would contain:
224df 20 20 41 2c 20 42 2c 20 6e 69 6c 2c 20 6e 69 6c    A, B, nil, nil
224e0 2c 20 6e 69 6c 2e 0a 2a 2a 20 49 66 20 74 68 65  , nil..** If the
224e1 20 69 53 74 61 72 74 20 61 72 67 75 6d 65 6e 74   iStart argument
224e2 20 68 61 64 20 62 65 65 6e 20 31 20 69 6e 73 74   had been 1 inst
224e3 65 61 64 20 6f 66 20 32 2c 20 74 68 65 6e 20 74  ead of 2, then t
224e4 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 77 6f 75  he result.** wou
224e5 6c 64 20 68 61 76 65 20 62 65 65 6e 3a 20 20 41  ld have been:  A
224e6 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c  , nil, nil, nil,
224e7 20 42 2e 20 20 54 6f 20 70 72 65 70 65 6e 64 20   B.  To prepend 
224e8 74 68 65 20 6e 65 77 20 73 6c 6f 74 73 2c 0a 2a  the new slots,.*
224e9 2a 20 74 68 65 20 69 53 74 61 72 74 20 76 61 6c  * the iStart val
224ea 75 65 20 77 6f 75 6c 64 20 62 65 20 30 2e 20 20  ue would be 0.  
224eb 54 68 65 20 72 65 73 75 6c 74 20 74 68 65 6e 20  The result then 
224ec 77 6f 75 6c 64 0a 2a 2a 20 62 65 3a 20 6e 69 6c  would.** be: nil
224ed 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 2c 20 42  , nil, nil, A, B
224ee 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d  ..**.** If a mem
224ef 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
224f0 61 69 6c 73 20 74 68 65 20 53 72 63 4c 69 73 74  ails the SrcList
224f1 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 20 20   is unchanged.  
224f2 54 68 65 0a 2a 2a 20 64 62 2d 3e 6d 61 6c 6c 6f  The.** db->mallo
224f3 63 46 61 69 6c 65 64 20 66 6c 61 67 20 77 69 6c  cFailed flag wil
224f4 6c 20 62 65 20 73 65 74 20 74 6f 20 74 72 75 65  l be set to true
224f5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
224f6 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
224f7 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
224f8 67 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ge(.  sqlite3 *d
224f9 62 2c 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  b,       /* Data
224fa 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
224fb 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 4f 4f 4d  to notify of OOM
224fc 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 53 72 63   errors */.  Src
224fd 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
224fe 2f 2a 20 54 68 65 20 53 72 63 4c 69 73 74 20 74  /* The SrcList t
224ff 6f 20 62 65 20 65 6e 6c 61 72 67 65 64 20 2a 2f  o be enlarged */
22500 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20  .  int nExtra,  
22501 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
22502 6f 66 20 6e 65 77 20 73 6c 6f 74 73 20 74 6f 20  of new slots to 
22503 61 64 64 20 74 6f 20 70 53 72 63 2d 3e 61 5b 5d  add to pSrc->a[]
22504 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72 74   */.  int iStart
22505 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
22506 78 20 69 6e 20 70 53 72 63 2d 3e 61 5b 5d 20 6f  x in pSrc->a[] o
22507 66 20 66 69 72 73 74 20 6e 65 77 20 73 6c 6f 74  f first new slot
22508 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
22509 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65  .  /* Sanity che
2250a 63 6b 69 6e 67 20 6f 6e 20 63 61 6c 6c 69 6e 67  cking on calling
2250b 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20   parameters */. 
2250c 20 61 73 73 65 72 74 28 20 69 53 74 61 72 74 3e   assert( iStart>
2250d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2250e 6e 45 78 74 72 61 3e 3d 31 20 29 3b 0a 20 20 69  nExtra>=1 );.  i
2250f 66 28 20 70 53 72 63 3d 3d 30 20 7c 7c 20 69 53  f( pSrc==0 || iS
22510 74 61 72 74 3e 70 53 72 63 2d 3e 6e 53 72 63 20  tart>pSrc->nSrc 
22511 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ){.    assert( d
22512 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22513 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 53  );.    return pS
22514 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c  rc;.  }..  /* Al
22515 6c 6f 63 61 74 65 20 61 64 64 69 74 69 6f 6e 61  locate additiona
22516 6c 20 73 70 61 63 65 20 69 66 20 6e 65 65 64 65  l space if neede
22517 64 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 2d  d */.  if( pSrc-
22518 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3e 70 53 72  >nSrc+nExtra>pSr
22519 63 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  c->nAlloc ){.   
2251a 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a   SrcList *pNew;.
2251b 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d      int nAlloc =
2251c 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74   pSrc->nSrc+nExt
2251d 72 61 3b 0a 20 20 20 20 69 6e 74 20 6e 47 6f 74  ra;.    int nGot
2251e 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c  ;.    pNew = sql
2251f 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
22520 2c 20 70 53 72 63 2c 0a 20 20 20 20 20 20 20 20  , pSrc,.        
22521 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70         sizeof(*p
22522 53 72 63 29 20 2b 20 28 6e 41 6c 6c 6f 63 2d 31  Src) + (nAlloc-1
22523 29 2a 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61  )*sizeof(pSrc->a
22524 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20  [0]) );.    if( 
22525 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pNew==0 ){.     
22526 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
22527 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
22528 20 20 20 72 65 74 75 72 6e 20 70 53 72 63 3b 0a     return pSrc;.
22529 20 20 20 20 7d 0a 20 20 20 20 70 53 72 63 20 3d      }.    pSrc =
2252a 20 70 4e 65 77 3b 0a 20 20 20 20 6e 47 6f 74 20   pNew;.    nGot 
2252b 3d 20 28 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  = (sqlite3DbMall
2252c 6f 63 53 69 7a 65 28 64 62 2c 20 70 4e 65 77 29  ocSize(db, pNew)
2252d 20 2d 20 73 69 7a 65 6f 66 28 2a 70 53 72 63 29   - sizeof(*pSrc)
2252e 29 2f 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61  )/sizeof(pSrc->a
2252f 5b 30 5d 29 2b 31 3b 0a 20 20 20 20 70 53 72 63  [0])+1;.    pSrc
22530 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 75 31 36 29  ->nAlloc = (u16)
22531 6e 47 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  nGot;.  }..  /* 
22532 4d 6f 76 65 20 65 78 69 73 74 69 6e 67 20 73 6c  Move existing sl
22533 6f 74 73 20 74 68 61 74 20 63 6f 6d 65 20 61 66  ots that come af
22534 74 65 72 20 74 68 65 20 6e 65 77 6c 79 20 69 6e  ter the newly in
22535 73 65 72 74 65 64 20 73 6c 6f 74 73 0a 20 20 2a  serted slots.  *
22536 2a 20 6f 75 74 20 6f 66 20 74 68 65 20 77 61 79  * out of the way
22537 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 70 53 72 63   */.  for(i=pSrc
22538 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 69 53 74  ->nSrc-1; i>=iSt
22539 61 72 74 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70  art; i--){.    p
2253a 53 72 63 2d 3e 61 5b 69 2b 6e 45 78 74 72 61 5d  Src->a[i+nExtra]
2253b 20 3d 20 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20   = pSrc->a[i];. 
2253c 20 7d 0a 20 20 70 53 72 63 2d 3e 6e 53 72 63 20   }.  pSrc->nSrc 
2253d 2b 3d 20 28 69 31 36 29 6e 45 78 74 72 61 3b 0a  += (i16)nExtra;.
2253e 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 6e  .  /* Zero the n
2253f 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ewly allocated s
22540 6c 6f 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  lots */.  memset
22541 28 26 70 53 72 63 2d 3e 61 5b 69 53 74 61 72 74  (&pSrc->a[iStart
22542 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 53 72  ], 0, sizeof(pSr
22543 63 2d 3e 61 5b 30 5d 29 2a 6e 45 78 74 72 61 29  c->a[0])*nExtra)
22544 3b 0a 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74  ;.  for(i=iStart
22545 3b 20 69 3c 69 53 74 61 72 74 2b 6e 45 78 74 72  ; i<iStart+nExtr
22546 61 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 53 72  a; i++){.    pSr
22547 63 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 20  c->a[i].iCursor 
22548 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  = -1;.  }..  /* 
22549 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
2254a 20 74 6f 20 74 68 65 20 65 6e 6c 61 72 67 65 64   to the enlarged
2254b 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 72 65   SrcList */.  re
2254c 74 75 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a 0a 2f  turn pSrc;.}.../
2254d 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65  *.** Append a ne
2254e 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20  w table name to 
2254f 74 68 65 20 67 69 76 65 6e 20 53 72 63 4c 69 73  the given SrcLis
22550 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65 77  t.  Create a new
22551 20 53 72 63 4c 69 73 74 20 69 66 0a 2a 2a 20 6e   SrcList if.** n
22552 65 65 64 20 62 65 2e 20 20 41 20 6e 65 77 20 65  eed be.  A new e
22553 6e 74 72 79 20 69 73 20 63 72 65 61 74 65 64 20  ntry is created 
22554 69 6e 20 74 68 65 20 53 72 63 4c 69 73 74 20 65  in the SrcList e
22555 76 65 6e 20 69 66 20 70 54 6f 6b 65 6e 20 69 73  ven if pToken is
22556 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 53   NULL..**.** A S
22557 72 63 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e  rcList is return
22558 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  ed, or NULL if t
22559 68 65 72 65 20 69 73 20 61 6e 20 4f 4f 4d 20 65  here is an OOM e
2255a 72 72 6f 72 2e 20 20 54 68 65 20 72 65 74 75 72  rror.  The retur
2255b 6e 65 64 0a 2a 2a 20 53 72 63 4c 69 73 74 20 6d  ned.** SrcList m
2255c 69 67 68 74 20 62 65 20 74 68 65 20 73 61 6d 65  ight be the same
2255d 20 61 73 20 74 68 65 20 53 72 63 4c 69 73 74 20   as the SrcList 
2255e 74 68 61 74 20 77 61 73 20 69 6e 70 75 74 20 6f  that was input o
2255f 72 20 69 74 20 6d 69 67 68 74 20 62 65 0a 2a 2a  r it might be.**
22560 20 61 20 6e 65 77 20 6f 6e 65 2e 20 20 49 66 20   a new one.  If 
22561 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 64 6f 65  an OOM error doe
22562 73 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74  s occurs, then t
22563 68 65 20 70 72 69 6f 72 20 76 61 6c 75 65 20 6f  he prior value o
22564 66 20 70 4c 69 73 74 0a 2a 2a 20 74 68 61 74 20  f pList.** that 
22565 69 73 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73  is input to this
22566 20 72 6f 75 74 69 6e 65 20 69 73 20 61 75 74 6f   routine is auto
22567 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 64 2e  matically freed.
22568 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 61 74 61 62  .**.** If pDatab
22569 61 73 65 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c  ase is not null,
2256a 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
2256b 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20  he table has an 
2256c 6f 70 74 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61  optional.** data
2256d 62 61 73 65 20 6e 61 6d 65 20 70 72 65 66 69 78  base name prefix
2256e 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 20 20 22  .  Like this:  "
2256f 64 61 74 61 62 61 73 65 2e 74 61 62 6c 65 22 2e  database.table".
22570 20 20 54 68 65 20 70 44 61 74 61 62 61 73 65 0a    The pDatabase.
22571 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  ** points to the
22572 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20   table name and 
22573 74 68 65 20 70 54 61 62 6c 65 20 70 6f 69 6e 74  the pTable point
22574 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  s to the databas
22575 65 20 6e 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 53  e name..** The S
22576 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65  rcList.a[].zName
22577 20 66 69 65 6c 64 20 69 73 20 66 69 6c 6c 65 64   field is filled
22578 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20   with the table 
22579 6e 61 6d 65 20 77 68 69 63 68 20 6d 69 67 68 74  name which might
2257a 0a 2a 2a 20 63 6f 6d 65 20 66 72 6f 6d 20 70 54  .** come from pT
2257b 61 62 6c 65 20 28 69 66 20 70 44 61 74 61 62 61  able (if pDataba
2257c 73 65 20 69 73 20 4e 55 4c 4c 29 20 6f 72 20 66  se is NULL) or f
2257d 72 6f 6d 20 70 44 61 74 61 62 61 73 65 2e 20 20  rom pDatabase.  
2257e 0a 2a 2a 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e  .** SrcList.a[].
2257f 7a 44 61 74 61 62 61 73 65 20 69 73 20 66 69 6c  zDatabase is fil
22580 6c 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74  led with the dat
22581 61 62 61 73 65 20 6e 61 6d 65 20 66 72 6f 6d 20  abase name from 
22582 70 54 61 62 6c 65 2c 0a 2a 2a 20 6f 72 20 77 69  pTable,.** or wi
22583 74 68 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 64 61  th NULL if no da
22584 74 61 62 61 73 65 20 69 73 20 73 70 65 63 69 66  tabase is specif
22585 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74  ied..**.** In ot
22586 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 63 61  her words, if ca
22587 6c 6c 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ll like this:.**
22588 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69  .**         sqli
22589 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
2258a 28 44 2c 41 2c 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a  (D,A,B,0);.**.**
2258b 20 54 68 65 6e 20 42 20 69 73 20 61 20 74 61 62   Then B is a tab
2258c 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20  le name and the 
2258d 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73  database name is
2258e 20 75 6e 73 70 65 63 69 66 69 65 64 2e 20 20 49   unspecified.  I
2258f 66 20 63 61 6c 6c 65 64 0a 2a 2a 20 6c 69 6b 65  f called.** like
22590 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
22591 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
22592 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c  istAppend(D,A,B,
22593 43 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 43  C);.**.** Then C
22594 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 6e 61   is the table na
22595 6d 65 20 61 6e 64 20 42 20 69 73 20 74 68 65 20  me and B is the 
22596 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a  database name..*
22597 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22598 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
22599 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 0a  3SrcListAppend(.
2259a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2259b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74        /* Connect
2259c 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66  ion to notify of
2259d 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
2259e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2259f 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 41 70 70  List,     /* App
225a0 65 6e 64 20 74 6f 20 74 68 69 73 20 53 72 63 4c  end to this SrcL
225a1 69 73 74 2e 20 4e 55 4c 4c 20 63 72 65 61 74 65  ist. NULL create
225a2 73 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20  s a new SrcList 
225a3 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62  */.  Token *pTab
225a4 6c 65 2c 20 20 20 20 20 20 2f 2a 20 54 61 62 6c  le,      /* Tabl
225a5 65 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20  e to append */. 
225a6 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73   Token *pDatabas
225a7 65 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  e    /* Database
225a8 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
225a9 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63  .){.  struct Src
225aa 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
225ab 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
225ac 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20   ){.    pList = 
225ad 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
225ae 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 53  ero(db, sizeof(S
225af 72 63 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69  rcList) );.    i
225b0 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
225b1 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73  turn 0;.    pLis
225b2 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20  t->nAlloc = 1;. 
225b3 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c   }.  pList = sql
225b4 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
225b5 67 65 28 64 62 2c 20 70 4c 69 73 74 2c 20 31 2c  ge(db, pList, 1,
225b6 20 70 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 0a 20   pList->nSrc);. 
225b7 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
225b8 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  ailed ){.    sql
225b9 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
225ba 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
225bb 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
225bc 20 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74    pItem = &pList
225bd 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63 2d  ->a[pList->nSrc-
225be 31 5d 3b 0a 20 20 69 66 28 20 70 44 61 74 61 62  1];.  if( pDatab
225bf 61 73 65 20 26 26 20 70 44 61 74 61 62 61 73 65  ase && pDatabase
225c0 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44  ->z==0 ){.    pD
225c1 61 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 7d  atabase = 0;.  }
225c2 0a 20 20 69 66 28 20 70 44 61 74 61 62 61 73 65  .  if( pDatabase
225c3 20 26 26 20 70 54 61 62 6c 65 20 29 7b 0a 20 20   && pTable ){.  
225c4 20 20 54 6f 6b 65 6e 20 2a 70 54 65 6d 70 20 3d    Token *pTemp =
225c5 20 70 44 61 74 61 62 61 73 65 3b 0a 20 20 20 20   pDatabase;.    
225c6 70 44 61 74 61 62 61 73 65 20 3d 20 70 54 61 62  pDatabase = pTab
225c7 6c 65 3b 0a 20 20 20 20 70 54 61 62 6c 65 20 3d  le;.    pTable =
225c8 20 70 54 65 6d 70 3b 0a 20 20 7d 0a 20 20 70 49   pTemp;.  }.  pI
225c9 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c  tem->zName = sql
225ca 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
225cb 6e 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 20  n(db, pTable);. 
225cc 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
225cd 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
225ce 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 61  romToken(db, pDa
225cf 74 61 62 61 73 65 29 3b 0a 20 20 72 65 74 75 72  tabase);.  retur
225d0 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  n pList;.}../*.*
225d1 2a 20 41 73 73 69 67 6e 20 56 64 62 65 43 75 72  * Assign VdbeCur
225d2 73 6f 72 20 69 6e 64 65 78 20 6e 75 6d 62 65 72  sor index number
225d3 73 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20  s to all tables 
225d4 69 6e 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f 0a  in a SrcList.*/.
225d5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
225d6 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
225d7 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
225d8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
225d9 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a  rcList *pList){.
225da 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
225db 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
225dc 70 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  pItem;.  assert(
225dd 70 4c 69 73 74 20 7c 7c 20 70 50 61 72 73 65 2d  pList || pParse-
225de 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
225df 64 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  d );.  if( pList
225e0 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c   ){.    for(i=0,
225e1 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b   pItem=pList->a;
225e2 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20   i<pList->nSrc; 
225e3 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
225e4 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
225e5 69 43 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65  iCursor>=0 ) bre
225e6 61 6b 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d  ak;.      pItem-
225e7 3e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73  >iCursor = pPars
225e8 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20  e->nTab++;.     
225e9 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c   if( pItem->pSel
225ea 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ect ){.        s
225eb 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73  qlite3SrcListAss
225ec 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73  ignCursors(pPars
225ed 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  e, pItem->pSelec
225ee 74 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20  t->pSrc);.      
225ef 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  }.    }.  }.}../
225f0 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  *.** Delete an e
225f1 6e 74 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e  ntire SrcList in
225f2 63 6c 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20  cluding all its 
225f3 73 75 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  substructure..*/
225f4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
225f5 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
225f6 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  istDelete(sqlite
225f7 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a  3 *db, SrcList *
225f8 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
225f9 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
225fa 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
225fb 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
225fc 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70 49  return;.  for(pI
225fd 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d  tem=pList->a, i=
225fe 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63  0; i<pList->nSrc
225ff 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
22600 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
22601 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 44  ee(db, pItem->zD
22602 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71  atabase);.    sq
22603 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
22604 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pItem->zName);. 
22605 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22606 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  (db, pItem->zAli
22607 61 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  as);.    sqlite3
22608 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d  DbFree(db, pItem
22609 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 73  ->zIndex);.    s
2260a 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
2260b 65 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a  e(pItem->pTab);.
2260c 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2260d 74 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65  tDelete(db, pIte
2260e 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  m->pSelect);.   
2260f 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
22610 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 4f  te(db, pItem->pO
22611 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  n);.    sqlite3I
22612 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
22613 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a  pItem->pUsing);.
22614 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
22615 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a  ree(db, pList);.
22616 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
22617 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
22618 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f  by the parser to
22619 20 61 64 64 20 61 20 6e 65 77 20 74 65 72 6d 20   add a new term 
2261a 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66  to the.** end of
2261b 20 61 20 67 72 6f 77 69 6e 67 20 46 52 4f 4d 20   a growing FROM 
2261c 63 6c 61 75 73 65 2e 20 20 54 68 65 20 22 70 22  clause.  The "p"
2261d 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
2261e 65 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65  e part of.** the
2261f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 68 61   FROM clause tha
22620 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  t has already be
22621 65 6e 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20  en constructed. 
22622 20 22 70 22 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20   "p" is NULL.** 
22623 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66  if this is the f
22624 69 72 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65  irst term of the
22625 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 70   FROM clause.  p
22626 54 61 62 6c 65 20 61 6e 64 20 70 44 61 74 61 62  Table and pDatab
22627 61 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 6e  ase.** are the n
22628 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
22629 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61   and database na
2262a 6d 65 64 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  med in the FROM 
2262b 63 6c 61 75 73 65 20 74 65 72 6d 2e 0a 2a 2a 20  clause term..** 
2262c 70 44 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c  pDatabase is NUL
2262d 4c 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  L if the databas
2262e 65 20 6e 61 6d 65 20 71 75 61 6c 69 66 69 65 72  e name qualifier
2262f 20 69 73 20 6d 69 73 73 69 6e 67 20 2d 20 74 68   is missing - th
22630 65 0a 2a 2a 20 75 73 75 61 6c 20 63 61 73 65 2e  e.** usual case.
22631 20 20 49 66 20 74 68 65 20 74 65 72 6d 20 68 61    If the term ha
22632 73 20 61 20 61 6c 69 61 73 2c 20 74 68 65 6e 20  s a alias, then 
22633 70 41 6c 69 61 73 20 70 6f 69 6e 74 73 20 74 6f  pAlias points to
22634 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 74 6f   the.** alias to
22635 6b 65 6e 2e 20 20 49 66 20 74 68 65 20 74 65 72  ken.  If the ter
22636 6d 20 69 73 20 61 20 73 75 62 71 75 65 72 79 2c  m is a subquery,
22637 20 74 68 65 6e 20 70 53 75 62 71 75 65 72 79 20   then pSubquery 
22638 69 73 20 74 68 65 0a 2a 2a 20 53 45 4c 45 43 54  is the.** SELECT
22639 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
2263a 74 68 65 20 73 75 62 71 75 65 72 79 20 65 6e 63  the subquery enc
2263b 6f 64 65 73 2e 20 20 54 68 65 20 70 54 61 62 6c  odes.  The pTabl
2263c 65 20 61 6e 64 0a 2a 2a 20 70 44 61 74 61 62 61  e and.** pDataba
2263d 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  se parameters ar
2263e 65 20 4e 55 4c 4c 20 66 6f 72 20 73 75 62 71 75  e NULL for subqu
2263f 65 72 69 65 73 2e 20 20 54 68 65 20 70 4f 6e 20  eries.  The pOn 
22640 61 6e 64 20 70 55 73 69 6e 67 0a 2a 2a 20 70 61  and pUsing.** pa
22641 72 61 6d 65 74 65 72 73 20 61 72 65 20 74 68 65  rameters are the
22642 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
22643 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61  ON and USING cla
22644 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  uses..**.** Retu
22645 72 6e 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74  rn a new SrcList
22646 20 77 68 69 63 68 20 65 6e 63 6f 64 65 73 20 69   which encodes i
22647 73 20 74 68 65 20 46 52 4f 4d 20 77 69 74 68 20  s the FROM with 
22648 74 68 65 20 6e 65 77 0a 2a 2a 20 74 65 72 6d 20  the new.** term 
22649 61 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  added..*/.SQLITE
2264a 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
2264b 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
2264c 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 0a  AppendFromTerm(.
2264d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2264e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2264f 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
22650 20 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 20 20    SrcList *p,   
22651 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22652 20 6c 65 66 74 20 70 61 72 74 20 6f 66 20 74 68   left part of th
22653 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6c  e FROM clause al
22654 72 65 61 64 79 20 73 65 65 6e 20 2a 2f 0a 20 20  ready seen */.  
22655 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20  Token *pTable,  
22656 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
22657 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
22658 61 64 64 20 74 6f 20 74 68 65 20 46 52 4f 4d 20  add to the FROM 
22659 63 6c 61 75 73 65 20 2a 2f 0a 20 20 54 6f 6b 65  clause */.  Toke
2265a 6e 20 2a 70 44 61 74 61 62 61 73 65 2c 20 20 20  n *pDatabase,   
2265b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2265c 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
2265d 61 69 6e 69 6e 67 20 70 54 61 62 6c 65 20 2a 2f  aining pTable */
2265e 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 6c 69 61 73  .  Token *pAlias
2265f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
22660 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
22661 65 20 6f 66 20 74 68 65 20 41 53 20 73 75 62 65  e of the AS sube
22662 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53  xpression */.  S
22663 65 6c 65 63 74 20 2a 70 53 75 62 71 75 65 72 79  elect *pSubquery
22664 2c 20 20 20 20 20 20 2f 2a 20 41 20 73 75 62 71  ,      /* A subq
22665 75 65 72 79 20 75 73 65 64 20 69 6e 20 70 6c 61  uery used in pla
22666 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6e 61  ce of a table na
22667 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f  me */.  Expr *pO
22668 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
22669 2f 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65  /* The ON clause
2266a 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20   of a join */.  
2266b 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 20 20  IdList *pUsing  
2266c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55          /* The U
2266d 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61  SING clause of a
2266e 20 6a 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 73 74   join */.){.  st
2266f 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
22670 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 73 71 6c 69  m *pItem;.  sqli
22671 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
22672 2d 3e 64 62 3b 0a 20 20 70 20 3d 20 73 71 6c 69  ->db;.  p = sqli
22673 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
22674 28 64 62 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20  (db, p, pTable, 
22675 70 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66  pDatabase);.  if
22676 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 53 72  ( p==0 || p->nSr
22677 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  c==0 ){.    sqli
22678 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
22679 2c 20 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c 69  , pOn);.    sqli
2267a 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
2267b 64 62 2c 20 70 55 73 69 6e 67 29 3b 0a 20 20 20  db, pUsing);.   
2267c 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2267d 6c 65 74 65 28 64 62 2c 20 70 53 75 62 71 75 65  lete(db, pSubque
2267e 72 79 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ry);.    return 
2267f 70 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d  p;.  }.  pItem =
22680 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31   &p->a[p->nSrc-1
22681 5d 3b 0a 20 20 69 66 28 20 70 41 6c 69 61 73 20  ];.  if( pAlias 
22682 26 26 20 70 41 6c 69 61 73 2d 3e 6e 20 29 7b 0a  && pAlias->n ){.
22683 20 20 20 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61      pItem->zAlia
22684 73 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  s = sqlite3NameF
22685 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 41 6c  romToken(db, pAl
22686 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65  ias);.  }.  pIte
22687 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 75  m->pSelect = pSu
22688 62 71 75 65 72 79 3b 0a 20 20 70 49 74 65 6d 2d  bquery;.  pItem-
22689 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20 70 49  >pOn = pOn;.  pI
2268a 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 70 55  tem->pUsing = pU
2268b 73 69 6e 67 3b 0a 20 20 72 65 74 75 72 6e 20 70  sing;.  return p
2268c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  ;.}../*.** Add a
2268d 6e 20 49 4e 44 45 58 45 44 20 42 59 20 6f 72 20  n INDEXED BY or 
2268e 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75  NOT INDEXED clau
2268f 73 65 20 74 6f 20 74 68 65 20 6d 6f 73 74 20 72  se to the most r
22690 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 0a 2a  ecently added .*
22691 2a 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  * element of the
22692 20 73 6f 75 72 63 65 2d 6c 69 73 74 20 70 61 73   source-list pas
22693 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
22694 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53  d argument..*/.S
22695 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
22696 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
22697 74 49 6e 64 65 78 65 64 42 79 28 50 61 72 73 65  tIndexedBy(Parse
22698 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73   *pParse, SrcLis
22699 74 20 2a 70 2c 20 54 6f 6b 65 6e 20 2a 70 49 6e  t *p, Token *pIn
2269a 64 65 78 65 64 42 79 29 7b 0a 20 20 69 66 28 20  dexedBy){.  if( 
2269b 70 49 6e 64 65 78 65 64 42 79 20 26 26 20 70 20  pIndexedBy && p 
2269c 26 26 20 70 2d 3e 6e 53 72 63 3e 30 20 29 7b 0a  && p->nSrc>0 ){.
2269d 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
2269e 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2269f 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31   &p->a[p->nSrc-1
226a0 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ];.    assert( p
226a1 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64  Item->notIndexed
226a2 3d 3d 30 20 26 26 20 70 49 74 65 6d 2d 3e 7a 49  ==0 && pItem->zI
226a3 6e 64 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 69  ndex==0 );.    i
226a4 66 28 20 70 49 6e 64 65 78 65 64 42 79 2d 3e 6e  f( pIndexedBy->n
226a5 3d 3d 31 20 26 26 20 21 70 49 6e 64 65 78 65 64  ==1 && !pIndexed
226a6 42 79 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 2f  By->z ){.      /
226a7 2a 20 41 20 22 4e 4f 54 20 49 4e 44 45 58 45 44  * A "NOT INDEXED
226a8 22 20 63 6c 61 75 73 65 20 77 61 73 20 73 75 70  " clause was sup
226a9 70 6c 69 65 64 2e 20 53 65 65 20 70 61 72 73 65  plied. See parse
226aa 2e 79 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e  .y .      ** con
226ab 73 74 72 75 63 74 20 22 69 6e 64 65 78 65 64 5f  struct "indexed_
226ac 6f 70 74 22 20 66 6f 72 20 64 65 74 61 69 6c 73  opt" for details
226ad 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d  . */.      pItem
226ae 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 31  ->notIndexed = 1
226af 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
226b0 20 20 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78     pItem->zIndex
226b1 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
226b2 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e  omToken(pParse->
226b3 64 62 2c 20 70 49 6e 64 65 78 65 64 42 79 29 3b  db, pIndexedBy);
226b4 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
226b5 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 64 69 6e  .** When buildin
226b6 67 20 75 70 20 61 20 46 52 4f 4d 20 63 6c 61 75  g up a FROM clau
226b7 73 65 20 69 6e 20 74 68 65 20 70 61 72 73 65 72  se in the parser
226b8 2c 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61  , the join opera
226b9 74 6f 72 0a 2a 2a 20 69 73 20 69 6e 69 74 69 61  tor.** is initia
226ba 6c 6c 79 20 61 74 74 61 63 68 65 64 20 74 6f 20  lly attached to 
226bb 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  the left operand
226bc 2e 20 20 42 75 74 20 74 68 65 20 63 6f 64 65 20  .  But the code 
226bd 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 78 70  generator.** exp
226be 65 63 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70  ects the join op
226bf 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e 20  erator to be on 
226c0 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  the right operan
226c1 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  d.  This routine
226c2 0a 2a 2a 20 53 68 69 66 74 73 20 61 6c 6c 20 6a  .** Shifts all j
226c3 6f 69 6e 20 6f 70 65 72 61 74 6f 72 73 20 66 72  oin operators fr
226c4 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74  om left to right
226c5 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 46   for an entire F
226c6 52 4f 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e 0a 2a  ROM.** clause..*
226c7 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 53 75  *.** Example: Su
226c8 70 70 6f 73 65 20 74 68 65 20 6a 6f 69 6e 20 69  ppose the join i
226c9 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
226ca 2a 2a 20 20 20 20 20 20 20 20 20 20 20 41 20 6e  **           A n
226cb 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69  atural cross joi
226cc 6e 20 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70  n B.**.** The op
226cd 65 72 61 74 6f 72 20 69 73 20 22 6e 61 74 75 72  erator is "natur
226ce 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 22 2e 20  al cross join". 
226cf 20 54 68 65 20 41 20 61 6e 64 20 42 20 6f 70 65   The A and B ope
226d0 72 61 6e 64 73 20 61 72 65 20 73 74 6f 72 65 64  rands are stored
226d1 0a 2a 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d 20 61  .** in p->a[0] a
226d2 6e 64 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 73 70  nd p->a[1], resp
226d3 65 63 74 69 76 65 6c 79 2e 20 20 54 68 65 20 70  ectively.  The p
226d4 61 72 73 65 72 20 69 6e 69 74 69 61 6c 6c 79 20  arser initially 
226d5 73 74 6f 72 65 73 20 74 68 65 0a 2a 2a 20 6f 70  stores the.** op
226d6 65 72 61 74 6f 72 20 77 69 74 68 20 41 2e 20 20  erator with A.  
226d7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 69  This routine shi
226d8 66 74 73 20 74 68 61 74 20 6f 70 65 72 61 74 6f  fts that operato
226d9 72 20 6f 76 65 72 20 74 6f 20 42 2e 0a 2a 2f 0a  r over to B..*/.
226da 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
226db 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
226dc 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
226dd 53 72 63 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69  SrcList *p){.  i
226de 66 28 20 70 20 26 26 20 70 2d 3e 61 20 29 7b 0a  f( p && p->a ){.
226df 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
226e0 6f 72 28 69 3d 70 2d 3e 6e 53 72 63 2d 31 3b 20  or(i=p->nSrc-1; 
226e1 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  i>0; i--){.     
226e2 20 70 2d 3e 61 5b 69 5d 2e 6a 6f 69 6e 74 79 70   p->a[i].jointyp
226e3 65 20 3d 20 70 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f  e = p->a[i-1].jo
226e4 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 0a 20 20  intype;.    }.  
226e5 20 20 70 2d 3e 61 5b 30 5d 2e 6a 6f 69 6e 74 79    p->a[0].jointy
226e6 70 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  pe = 0;.  }.}../
226e7 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61  *.** Begin a tra
226e8 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49  nsaction.*/.SQLI
226e9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
226ea 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e  sqlite3BeginTran
226eb 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70  saction(Parse *p
226ec 50 61 72 73 65 2c 20 69 6e 74 20 74 79 70 65 29  Parse, int type)
226ed 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
226ee 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e  .  Vdbe *v;.  in
226ef 74 20 69 3b 0a 0a 20 20 69 66 28 20 70 50 61 72  t i;..  if( pPar
226f0 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61  se==0 || (db=pPa
226f1 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64  rse->db)==0 || d
226f2 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30  b->aDb[0].pBt==0
226f3 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
226f4 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
226f5 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
226f6 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  d ) return;.  if
226f7 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
226f8 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
226f9 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22  E_TRANSACTION, "
226fa 42 45 47 49 4e 22 2c 20 30 2c 20 30 29 20 29 20  BEGIN", 0, 0) ) 
226fb 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73  return;..  v = s
226fc 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
226fd 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20  arse);.  if( !v 
226fe 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
226ff 74 79 70 65 21 3d 54 4b 5f 44 45 46 45 52 52 45  type!=TK_DEFERRE
22700 44 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  D ){.    for(i=0
22701 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
22702 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
22703 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22704 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 2c  _Transaction, i,
22705 20 28 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c 55   (type==TK_EXCLU
22706 53 49 56 45 29 2b 31 29 3b 0a 20 20 20 20 20 20  SIVE)+1);.      
22707 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
22708 74 72 65 65 28 76 2c 20 69 29 3b 0a 20 20 20 20  tree(v, i);.    
22709 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  }.  }.  sqlite3V
2270a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2270b 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20 30  AutoCommit, 0, 0
2270c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d  );.}../*.** Comm
2270d 69 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  it a transaction
2270e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2270f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
22710 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e  ommitTransaction
22711 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
22712 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
22713 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66    Vdbe *v;..  if
22714 28 20 70 50 61 72 73 65 3d 3d 30 20 7c 7c 20 28  ( pParse==0 || (
22715 64 62 3d 70 50 61 72 73 65 2d 3e 64 62 29 3d 3d  db=pParse->db)==
22716 30 20 7c 7c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e  0 || db->aDb[0].
22717 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  pBt==0 ) return;
22718 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
22719 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
2271a 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
2271b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  ;.  if( sqlite3A
2271c 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2271d 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54   SQLITE_TRANSACT
2271e 49 4f 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30  ION, "COMMIT", 0
2271f 2c 20 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a  , 0) ) return;..
22720 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
22721 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
22722 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
22723 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22724 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c  , OP_AutoCommit,
22725 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   1, 0);.  }.}../
22726 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20  *.** Rollback a 
22727 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53  transaction.*/.S
22728 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
22729 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61  id sqlite3Rollba
2272a 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61  ckTransaction(Pa
2272b 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
2272c 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56  sqlite3 *db;.  V
2272d 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70  dbe *v;..  if( p
2272e 50 61 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d  Parse==0 || (db=
2272f 70 50 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c  pParse->db)==0 |
22730 7c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  | db->aDb[0].pBt
22731 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
22732 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
22733 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
22734 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
22735 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
22736 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
22737 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e  LITE_TRANSACTION
22738 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c  , "ROLLBACK", 0,
22739 20 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20   0) ) return;.. 
2273a 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2273b 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
2273c 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
2273d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2273e 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20   OP_AutoCommit, 
2273f 31 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  1, 1);.  }.}../*
22740 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
22741 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  n is called by t
22742 68 65 20 70 61 72 73 65 72 20 77 68 65 6e 20 69  he parser when i
22743 74 20 70 61 72 73 65 73 20 61 20 63 6f 6d 6d 61  t parses a comma
22744 6e 64 20 74 6f 20 63 72 65 61 74 65 2c 0a 2a 2a  nd to create,.**
22745 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c   release or roll
22746 62 61 63 6b 20 61 6e 20 53 51 4c 20 73 61 76 65  back an SQL save
22747 70 6f 69 6e 74 2e 20 0a 2a 2f 0a 53 51 4c 49 54  point. .*/.SQLIT
22748 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
22749 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28  qlite3Savepoint(
2274a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
2274b 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70 4e  nt op, Token *pN
2274c 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  ame){.  char *zN
2274d 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
2274e 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73  eFromToken(pPars
2274f 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20  e->db, pName);. 
22750 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20   if( zName ){.  
22751 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
22752 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
22753 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  e);.#ifndef SQLI
22754 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
22755 41 54 49 4f 4e 0a 20 20 20 20 73 74 61 74 69 63  ATION.    static
22756 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 5b   const char *az[
22757 5d 20 3d 20 7b 20 22 42 45 47 49 4e 22 2c 20 22  ] = { "BEGIN", "
22758 52 45 4c 45 41 53 45 22 2c 20 22 52 4f 4c 4c 42  RELEASE", "ROLLB
22759 41 43 4b 22 20 7d 3b 0a 20 20 20 20 61 73 73 65  ACK" };.    asse
2275a 72 74 28 20 21 53 41 56 45 50 4f 49 4e 54 5f 42  rt( !SAVEPOINT_B
2275b 45 47 49 4e 20 26 26 20 53 41 56 45 50 4f 49 4e  EGIN && SAVEPOIN
2275c 54 5f 52 45 4c 45 41 53 45 3d 3d 31 20 26 26 20  T_RELEASE==1 && 
2275d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
2275e 43 4b 3d 3d 32 20 29 3b 0a 23 65 6e 64 69 66 0a  CK==2 );.#endif.
2275f 20 20 20 20 69 66 28 20 21 76 20 7c 7c 20 73 71      if( !v || sq
22760 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
22761 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 53 41  Parse, SQLITE_SA
22762 56 45 50 4f 49 4e 54 2c 20 61 7a 5b 6f 70 5d 2c  VEPOINT, az[op],
22763 20 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20   zName, 0) ){.  
22764 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
22765 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e  e(pParse->db, zN
22766 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ame);.      retu
22767 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  rn;.    }.    sq
22768 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
22769 76 2c 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 2c  v, OP_Savepoint,
2276a 20 6f 70 2c 20 30 2c 20 30 2c 20 7a 4e 61 6d 65   op, 0, 0, zName
2276b 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P4_DYNAMIC);. 
2276c 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
2276d 20 73 75 72 65 20 74 68 65 20 54 45 4d 50 20 64   sure the TEMP d
2276e 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20  atabase is open 
2276f 61 6e 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  and available fo
22770 72 20 75 73 65 2e 20 20 52 65 74 75 72 6e 0a 2a  r use.  Return.*
22771 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
22772 65 72 72 6f 72 73 2e 20 20 4c 65 61 76 65 20 61  errors.  Leave a
22773 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ny error message
22774 73 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 20  s in the pParse 
22775 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
22776 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
22777 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70   sqlite3OpenTemp
22778 44 61 74 61 62 61 73 65 28 50 61 72 73 65 20 2a  Database(Parse *
22779 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74  pParse){.  sqlit
2277a 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2277b 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61  >db;.  if( db->a
2277c 44 62 5b 31 5d 2e 70 42 74 3d 3d 30 20 26 26 20  Db[1].pBt==0 && 
2277d 21 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e  !pParse->explain
2277e 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a   ){.    int rc;.
2277f 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
22780 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 0a 20 20   int flags = .  
22781 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
22782 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 0a  PEN_READWRITE |.
22783 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
22784 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20  _OPEN_CREATE |. 
22785 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
22786 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c  OPEN_EXCLUSIVE |
22787 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
22788 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
22789 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 20 20 20  LOSE |.         
2278a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d   SQLITE_OPEN_TEM
2278b 50 5f 44 42 3b 0a 0a 20 20 20 20 72 63 20 3d 20  P_DB;..    rc = 
2278c 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74  sqlite3BtreeFact
2278d 6f 72 79 28 64 62 2c 20 30 2c 20 30 2c 20 53 51  ory(db, 0, 0, SQ
2278e 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
2278f 48 45 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c 0a  HE_SIZE, flags,.
22790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22791 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22792 20 26 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74   &db->aDb[1].pBt
22793 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
22794 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
22795 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
22796 67 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c  g(pParse, "unabl
22797 65 20 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70  e to open a temp
22798 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20 22  orary database "
22799 0a 20 20 20 20 20 20 20 20 22 66 69 6c 65 20 66  .        "file f
2279a 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 6d 70 6f  or storing tempo
2279b 72 61 72 79 20 74 61 62 6c 65 73 22 29 3b 0a 20  rary tables");. 
2279c 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20       pParse->rc 
2279d 3d 20 72 63 3b 0a 20 20 20 20 20 20 72 65 74 75  = rc;.      retu
2279e 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
2279f 61 73 73 65 72 74 28 20 28 64 62 2d 3e 66 6c 61  assert( (db->fla
227a0 67 73 20 26 20 53 51 4c 49 54 45 5f 49 6e 54 72  gs & SQLITE_InTr
227a1 61 6e 73 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61  ans)==0 || db->a
227a2 75 74 6f 43 6f 6d 6d 69 74 20 29 3b 0a 20 20 20  utoCommit );.   
227a3 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
227a4 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20  [1].pSchema );. 
227a5 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a     sqlite3PagerJ
227a6 6f 75 72 6e 61 6c 4d 6f 64 65 28 73 71 6c 69 74  ournalMode(sqlit
227a7 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d  e3BtreePager(db-
227a8 3e 61 44 62 5b 31 5d 2e 70 42 74 29 2c 0a 20 20  >aDb[1].pBt),.  
227a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
227aa 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66            db->df
227ab 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a  ltJournalMode);.
227ac 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
227ad 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
227ae 65 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74  e VDBE code that
227af 20 77 69 6c 6c 20 76 65 72 69 66 79 20 74 68 65   will verify the
227b0 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 61   schema cookie a
227b1 6e 64 20 73 74 61 72 74 0a 2a 2a 20 61 20 72 65  nd start.** a re
227b2 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66  ad-transaction f
227b3 6f 72 20 61 6c 6c 20 6e 61 6d 65 64 20 64 61 74  or all named dat
227b4 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 2a 2a 0a  abase files..**.
227b5 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61  ** It is importa
227b6 6e 74 20 74 68 61 74 20 61 6c 6c 20 73 63 68 65  nt that all sche
227b7 6d 61 20 63 6f 6f 6b 69 65 73 20 62 65 20 76 65  ma cookies be ve
227b8 72 69 66 69 65 64 20 61 6e 64 20 61 6c 6c 0a 2a  rified and all.*
227b9 2a 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  * read transacti
227ba 6f 6e 73 20 62 65 20 73 74 61 72 74 65 64 20 62  ons be started b
227bb 65 66 6f 72 65 20 61 6e 79 74 68 69 6e 67 20 65  efore anything e
227bc 6c 73 65 20 68 61 70 70 65 6e 73 20 69 6e 0a 2a  lse happens in.*
227bd 2a 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  * the VDBE progr
227be 61 6d 2e 20 20 42 75 74 20 74 68 69 73 20 72 6f  am.  But this ro
227bf 75 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c  utine can be cal
227c0 6c 65 64 20 61 66 74 65 72 20 6d 75 63 68 20 6f  led after much o
227c1 74 68 65 72 0a 2a 2a 20 63 6f 64 65 20 68 61 73  ther.** code has
227c2 20 62 65 65 6e 20 67 65 6e 65 72 61 74 65 64 2e   been generated.
227c3 20 20 53 6f 20 68 65 72 65 20 69 73 20 77 68 61    So here is wha
227c4 74 20 77 65 20 64 6f 3a 0a 2a 2a 0a 2a 2a 20 54  t we do:.**.** T
227c5 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
227c6 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
227c7 6c 6c 65 64 2c 20 77 65 20 63 6f 64 65 20 61 6e  lled, we code an
227c8 20 4f 50 5f 47 6f 74 6f 20 74 68 61 74 0a 2a 2a   OP_Goto that.**
227c9 20 77 69 6c 6c 20 6a 75 6d 70 20 74 6f 20 61 20   will jump to a 
227ca 73 75 62 72 6f 75 74 69 6e 65 20 61 74 20 74 68  subroutine at th
227cb 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f  e end of the pro
227cc 67 72 61 6d 2e 20 20 54 68 65 6e 20 77 65 0a 2a  gram.  Then we.*
227cd 2a 20 72 65 63 6f 72 64 20 65 76 65 72 79 20 64  * record every d
227ce 61 74 61 62 61 73 65 20 74 68 61 74 20 6e 65 65  atabase that nee
227cf 64 73 20 69 74 73 20 73 63 68 65 6d 61 20 76 65  ds its schema ve
227d0 72 69 66 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a  rified in the.**
227d1 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d   pParse->cookieM
227d2 61 73 6b 20 66 69 65 6c 64 2e 20 20 4c 61 74 65  ask field.  Late
227d3 72 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f 74 68  r, after all oth
227d4 65 72 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e  er code has been
227d5 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 2c 20 74  .** generated, t
227d6 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68  he subroutine th
227d7 61 74 20 64 6f 65 73 20 74 68 65 20 63 6f 6f 6b  at does the cook
227d8 69 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e 73  ie verifications
227d9 20 61 6e 64 0a 2a 2a 20 73 74 61 72 74 73 20 74   and.** starts t
227da 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  he transactions 
227db 77 69 6c 6c 20 62 65 20 63 6f 64 65 64 20 61 6e  will be coded an
227dc 64 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 50 32  d the OP_Goto P2
227dd 20 76 61 6c 75 65 0a 2a 2a 20 77 69 6c 6c 20 62   value.** will b
227de 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20  e made to point 
227df 74 6f 20 74 68 61 74 20 73 75 62 72 6f 75 74 69  to that subrouti
227e0 6e 65 2e 20 20 54 68 65 20 67 65 6e 65 72 61 74  ne.  The generat
227e1 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ion of the.** co
227e2 6f 6b 69 65 20 76 65 72 69 66 69 63 61 74 69 6f  okie verificatio
227e3 6e 20 73 75 62 72 6f 75 74 69 6e 65 20 63 6f 64  n subroutine cod
227e4 65 20 68 61 70 70 65 6e 73 20 69 6e 20 73 71 6c  e happens in sql
227e5 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67  ite3FinishCoding
227e6 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62  ()..**.** If iDb
227e7 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 74 68 65  <0 then code the
227e8 20 4f 50 5f 47 6f 74 6f 20 6f 6e 6c 79 20 2d 20   OP_Goto only - 
227e9 64 6f 6e 27 74 20 73 65 74 20 66 6c 61 67 20 74  don't set flag t
227ea 6f 20 76 65 72 69 66 79 20 74 68 65 0a 2a 2a 20  o verify the.** 
227eb 73 63 68 65 6d 61 20 6f 6e 20 61 6e 79 20 64 61  schema on any da
227ec 74 61 62 61 73 65 73 2e 20 20 54 68 69 73 20 63  tabases.  This c
227ed 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 70 6f  an be used to po
227ee 73 69 74 69 6f 6e 20 74 68 65 20 4f 50 5f 47 6f  sition the OP_Go
227ef 74 6f 0a 2a 2a 20 65 61 72 6c 79 20 69 6e 20 74  to.** early in t
227f0 68 65 20 63 6f 64 65 2c 20 62 65 66 6f 72 65 20  he code, before 
227f1 77 65 20 6b 6e 6f 77 20 69 66 20 61 6e 79 20 64  we know if any d
227f2 61 74 61 62 61 73 65 20 74 61 62 6c 65 73 20 77  atabase tables w
227f3 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a  ill be used..*/.
227f4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
227f5 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56  oid sqlite3CodeV
227f6 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73  erifySchema(Pars
227f7 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
227f8 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Db){.  sqlite3 *
227f9 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  db;.  Vdbe *v;. 
227fa 20 69 6e 74 20 6d 61 73 6b 3b 0a 0a 20 20 76 20   int mask;..  v 
227fb 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
227fc 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
227fd 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 20  v==0 ) return;  
227fe 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70  /* This only hap
227ff 70 65 6e 73 20 69 66 20 74 68 65 72 65 20 77 61  pens if there wa
22800 73 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20  s a prior error 
22801 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  */.  db = pParse
22802 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72  ->db;.  if( pPar
22803 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3d 3d  se->cookieGoto==
22804 30 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  0 ){.    pParse-
22805 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 73 71  >cookieGoto = sq
22806 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22807 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30  v, OP_Goto, 0, 0
22808 29 2b 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  )+1;.  }.  if( i
22809 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  Db>=0 ){.    ass
2280a 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62  ert( iDb<db->nDb
2280b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2280c 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
2280d 21 3d 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b  !=0 || iDb==1 );
2280e 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62  .    assert( iDb
2280f 3c 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41  <SQLITE_MAX_ATTA
22810 43 48 45 44 2b 32 20 29 3b 0a 20 20 20 20 6d 61  CHED+2 );.    ma
22811 73 6b 20 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 20  sk = 1<<iDb;.   
22812 20 69 66 28 20 28 70 50 61 72 73 65 2d 3e 63 6f   if( (pParse->co
22813 6f 6b 69 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29  okieMask & mask)
22814 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 50 61  ==0 ){.      pPa
22815 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20  rse->cookieMask 
22816 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70  |= mask;.      p
22817 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c  Parse->cookieVal
22818 75 65 5b 69 44 62 5d 20 3d 20 64 62 2d 3e 61 44  ue[iDb] = db->aD
22819 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
2281a 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 0a 20  schema_cookie;. 
2281b 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54       if( !OMIT_T
2281c 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20  EMPDB && iDb==1 
2281d 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2281e 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61  e3OpenTempDataba
2281f 73 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  se(pParse);.    
22820 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
22821 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
22822 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 70  VDBE code that p
22823 72 65 70 61 72 65 73 20 66 6f 72 20 64 6f 69 6e  repares for doin
22824 67 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 74  g an operation t
22825 68 61 74 0a 2a 2a 20 6d 69 67 68 74 20 63 68 61  hat.** might cha
22826 6e 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65  nge the database
22827 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
22828 74 69 6e 65 20 73 74 61 72 74 73 20 61 20 6e 65  tine starts a ne
22829 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66  w transaction if
2282a 20 77 65 20 61 72 65 20 6e 6f 74 20 61 6c 72 65   we are not alre
2282b 61 64 79 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20  ady within.** a 
2282c 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66  transaction.  If
2282d 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 79 20   we are already 
2282e 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63  within a transac
2282f 74 69 6f 6e 2c 20 74 68 65 6e 20 61 20 63 68 65  tion, then a che
22830 63 6b 70 6f 69 6e 74 0a 2a 2a 20 69 73 20 73 65  ckpoint.** is se
22831 74 20 69 66 20 74 68 65 20 73 65 74 53 74 61 74  t if the setStat
22832 65 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20  ement parameter 
22833 69 73 20 74 72 75 65 2e 20 20 41 20 63 68 65 63  is true.  A chec
22834 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a  kpoint should.**
22835 20 62 65 20 73 65 74 20 66 6f 72 20 6f 70 65 72   be set for oper
22836 61 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 68  ations that migh
22837 74 20 66 61 69 6c 20 28 64 75 65 20 74 6f 20 61  t fail (due to a
22838 20 63 6f 6e 73 74 72 61 69 6e 74 29 20 70 61 72   constraint) par
22839 74 20 6f 66 0a 2a 2a 20 74 68 65 20 77 61 79 20  t of.** the way 
2283a 74 68 72 6f 75 67 68 20 61 6e 64 20 77 68 69 63  through and whic
2283b 68 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75  h will need to u
2283c 6e 64 6f 20 73 6f 6d 65 20 77 72 69 74 65 73 20  ndo some writes 
2283d 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74  without having t
2283e 6f 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 74 68  o.** rollback th
2283f 65 20 77 68 6f 6c 65 20 74 72 61 6e 73 61 63 74  e whole transact
22840 69 6f 6e 2e 20 20 46 6f 72 20 6f 70 65 72 61 74  ion.  For operat
22841 69 6f 6e 73 20 77 68 65 72 65 20 61 6c 6c 20 63  ions where all c
22842 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 20 63 61  onstraints.** ca
22843 6e 20 62 65 20 63 68 65 63 6b 65 64 20 62 65 66  n be checked bef
22844 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20  ore any changes 
22845 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20  are made to the 
22846 64 61 74 61 62 61 73 65 2c 20 69 74 20 69 73 20  database, it is 
22847 6e 65 76 65 72 0a 2a 2a 20 6e 65 63 65 73 73 61  never.** necessa
22848 72 79 20 74 6f 20 75 6e 64 6f 20 61 20 77 72 69  ry to undo a wri
22849 74 65 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b  te and the check
2284a 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 6e 6f 74  point should not
2284b 20 62 65 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49   be set..*/.SQLI
2284c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2284d 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
2284e 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65  eOperation(Parse
2284f 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 73 65   *pParse, int se
22850 74 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e 74 20  tStatement, int 
22851 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  iDb){.  Vdbe *v 
22852 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
22853 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
22854 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  v==0 ) return;. 
22855 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
22856 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  fySchema(pParse,
22857 20 69 44 62 29 3b 0a 20 20 70 50 61 72 73 65 2d   iDb);.  pParse-
22858 3e 77 72 69 74 65 4d 61 73 6b 20 7c 3d 20 31 3c  >writeMask |= 1<
22859 3c 69 44 62 3b 0a 20 20 69 66 28 20 73 65 74 53  <iDb;.  if( setS
2285a 74 61 74 65 6d 65 6e 74 20 26 26 20 70 50 61 72  tatement && pPar
2285b 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b  se->nested==0 ){
2285c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2285d 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 53 74 61  AddOp1(v, OP_Sta
2285e 74 65 6d 65 6e 74 2c 20 69 44 62 29 3b 0a 20 20  tement, iDb);.  
2285f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  }.}../*.** Check
22860 20 74 6f 20 73 65 65 20 69 66 20 70 49 6e 64 65   to see if pInde
22861 78 20 75 73 65 73 20 74 68 65 20 63 6f 6c 6c 61  x uses the colla
22862 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43  ting sequence pC
22863 6f 6c 6c 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  oll.  Return.** 
22864 74 72 75 65 20 69 66 20 69 74 20 64 6f 65 73 20  true if it does 
22865 61 6e 64 20 66 61 6c 73 65 20 69 66 20 69 74 20  and false if it 
22866 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 23 69 66  does not..*/.#if
22867 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22868 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20  _REINDEX.static 
22869 69 6e 74 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74  int collationMat
2286a 63 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ch(const char *z
2286b 43 6f 6c 6c 2c 20 49 6e 64 65 78 20 2a 70 49 6e  Coll, Index *pIn
2286c 64 65 78 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  dex){.  int i;. 
2286d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64   for(i=0; i<pInd
2286e 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  ex->nColumn; i++
2286f 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
22870 72 20 2a 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61  r *z = pIndex->a
22871 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 69 66  zColl[i];.    if
22872 28 20 7a 3d 3d 7a 43 6f 6c 6c 20 7c 7c 20 28 7a  ( z==zColl || (z
22873 20 26 26 20 7a 43 6f 6c 6c 20 26 26 20 30 3d 3d   && zColl && 0==
22874 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
22875 2c 20 7a 43 6f 6c 6c 29 29 20 29 7b 0a 20 20 20  , zColl)) ){.   
22876 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
22877 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
22878 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0;.}.#endif../*.
22879 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61 6c 6c  ** Recompute all
2287a 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62   indices of pTab
2287b 20 74 68 61 74 20 75 73 65 20 74 68 65 20 63 6f   that use the co
2287c 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2287d 20 70 43 6f 6c 6c 2e 0a 2a 2a 20 49 66 20 70 43   pColl..** If pC
2287e 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f  oll==0 then reco
2287f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65  mpute all indice
22880 73 20 6f 66 20 70 54 61 62 2e 0a 2a 2f 0a 23 69  s of pTab..*/.#i
22881 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22882 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 63  T_REINDEX.static
22883 20 76 6f 69 64 20 72 65 69 6e 64 65 78 54 61 62   void reindexTab
22884 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  le(Parse *pParse
22885 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63  , Table *pTab, c
22886 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c  har const *zColl
22887 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  ){.  Index *pInd
22888 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ex;             
22889 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73   /* An index ass
2288a 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54 61  ociated with pTa
2288b 62 20 2a 2f 0a 0a 20 20 66 6f 72 28 70 49 6e 64  b */..  for(pInd
2288c 65 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  ex=pTab->pIndex;
2288d 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d   pIndex; pIndex=
2288e 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a  pIndex->pNext){.
2288f 20 20 20 20 69 66 28 20 7a 43 6f 6c 6c 3d 3d 30      if( zColl==0
22890 20 7c 7c 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74   || collationMat
22891 63 68 28 7a 43 6f 6c 6c 2c 20 70 49 6e 64 65 78  ch(zColl, pIndex
22892 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ) ){.      int i
22893 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
22894 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
22895 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
22896 65 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ema);.      sqli
22897 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
22898 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
22899 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
2289a 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78  lite3RefillIndex
2289b 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c  (pParse, pIndex,
2289c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   -1);.    }.  }.
2289d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
2289e 52 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e  Recompute all in
2289f 64 69 63 65 73 20 6f 66 20 61 6c 6c 20 74 61 62  dices of all tab
228a0 6c 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61 62  les in all datab
228a1 61 73 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a  ases where the.*
228a2 2a 20 69 6e 64 69 63 65 73 20 75 73 65 20 74 68  * indices use th
228a3 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
228a4 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 49 66 20  ence pColl.  If 
228a5 70 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20 72 65  pColl==0 then re
228a6 63 6f 6d 70 75 74 65 0a 2a 2a 20 61 6c 6c 20 69  compute.** all i
228a7 6e 64 69 63 65 73 20 65 76 65 72 79 77 68 65 72  ndices everywher
228a8 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
228a9 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45  LITE_OMIT_REINDE
228aa 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  X.static void re
228ab 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28 50  indexDatabases(P
228ac 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 68  arse *pParse, ch
228ad 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29  ar const *zColl)
228ae 7b 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20  {.  Db *pDb;    
228af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228b0 2f 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61  /* A single data
228b1 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44  base */.  int iD
228b2 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
228b3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
228b4 61 62 61 73 65 20 69 6e 64 65 78 20 6e 75 6d 62  abase index numb
228b5 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  er */.  sqlite3 
228b6 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
228b7 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  ;   /* The datab
228b8 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
228b9 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b  /.  HashElem *k;
228ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228bb 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
228bc 76 65 72 20 74 61 62 6c 65 73 20 69 6e 20 70 44  ver tables in pD
228bd 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
228be 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
228bf 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e     /* A table in
228c0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
228c1 0a 0a 20 20 66 6f 72 28 69 44 62 3d 30 2c 20 70  ..  for(iDb=0, p
228c2 44 62 3d 64 62 2d 3e 61 44 62 3b 20 69 44 62 3c  Db=db->aDb; iDb<
228c3 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 2c 20  db->nDb; iDb++, 
228c4 70 44 62 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65  pDb++){.    asse
228c5 72 74 28 20 70 44 62 21 3d 30 20 29 3b 0a 20 20  rt( pDb!=0 );.  
228c6 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48 61    for(k=sqliteHa
228c7 73 68 46 69 72 73 74 28 26 70 44 62 2d 3e 70 53  shFirst(&pDb->pS
228c8 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b  chema->tblHash);
228c9 20 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48 61 73    k; k=sqliteHas
228ca 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20 20 20  hNext(k)){.     
228cb 20 70 54 61 62 20 3d 20 28 54 61 62 6c 65 2a 29   pTab = (Table*)
228cc 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 6b  sqliteHashData(k
228cd 29 3b 0a 20 20 20 20 20 20 72 65 69 6e 64 65 78  );.      reindex
228ce 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
228cf 61 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  ab, zColl);.    
228d0 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  }.  }.}.#endif..
228d1 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
228d2 6f 64 65 20 66 6f 72 20 74 68 65 20 52 45 49 4e  ode for the REIN
228d3 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  DEX command..**.
228d4 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45  **        REINDE
228d5 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  X               
228d6 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
228d7 31 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e  1.**        REIN
228d8 44 45 58 20 20 3c 63 6f 6c 6c 61 74 69 6f 6e 3e  DEX  <collation>
228d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
228da 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 52 45  - 2.**        RE
228db 49 4e 44 45 58 20 20 3f 3c 64 61 74 61 62 61 73  INDEX  ?<databas
228dc 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20  e>.?<tablename> 
228dd 20 2d 2d 20 33 0a 2a 2a 20 20 20 20 20 20 20 20   -- 3.**        
228de 52 45 49 4e 44 45 58 20 20 3f 3c 64 61 74 61 62  REINDEX  ?<datab
228df 61 73 65 3e 2e 3f 3c 69 6e 64 65 78 6e 61 6d 65  ase>.?<indexname
228e0 3e 20 20 2d 2d 20 34 0a 2a 2a 0a 2a 2a 20 46 6f  >  -- 4.**.** Fo
228e1 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20  rm 1 causes all 
228e2 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61  indices in all a
228e3 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
228e4 73 20 74 6f 20 62 65 20 72 65 62 75 69 6c 74 2e  s to be rebuilt.
228e5 0a 2a 2a 20 46 6f 72 6d 20 32 20 72 65 62 75 69  .** Form 2 rebui
228e6 6c 64 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  lds all indices 
228e7 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  in all databases
228e8 20 74 68 61 74 20 75 73 65 20 74 68 65 20 6e 61   that use the na
228e9 6d 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67  med.** collating
228ea 20 66 75 6e 63 74 69 6f 6e 2e 20 20 46 6f 72 6d   function.  Form
228eb 73 20 33 20 61 6e 64 20 34 20 72 65 62 75 69 6c  s 3 and 4 rebuil
228ec 64 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65  d the named inde
228ed 78 20 6f 72 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69  x or all.** indi
228ee 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
228ef 69 74 68 20 74 68 65 20 6e 61 6d 65 64 20 74 61  ith the named ta
228f0 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ble..*/.#ifndef 
228f1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e  SQLITE_OMIT_REIN
228f2 44 45 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  DEX.SQLITE_PRIVA
228f3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
228f4 65 69 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50  eindex(Parse *pP
228f5 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
228f6 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  me1, Token *pNam
228f7 65 32 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  e2){.  CollSeq *
228f8 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  pColl;          
228f9 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20     /* Collating 
228fa 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65 20 72  sequence to be r
228fb 65 69 6e 64 65 78 65 64 2c 20 6f 72 20 4e 55 4c  eindexed, or NUL
228fc 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20  L */.  char *z; 
228fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228fe 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 61 20     /* Name of a 
228ff 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a  table or index *
22900 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
22901 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  zDb;            
22902 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
22903 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 54 61 62  atabase */.  Tab
22904 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
22905 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74 61           /* A ta
22906 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  ble in the datab
22907 61 73 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  ase */.  Index *
22908 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20  pIndex;         
22909 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78       /* An index
2290a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2290b 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69   pTab */.  int i
2290c 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
2290d 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2290e 74 61 62 61 73 65 20 69 6e 64 65 78 20 6e 75 6d  tabase index num
2290f 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ber */.  sqlite3
22910 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
22911 62 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  b;   /* The data
22912 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
22913 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4f 62 6a  */.  Token *pObj
22914 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
22915 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
22916 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74  table or index t
22917 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 20 2a  o be reindexed *
22918 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  /..  /* Read the
22919 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
2291a 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
2291b 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65  curs, leave an e
2291c 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a  rror message.  *
2291d 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50  * and code in pP
2291e 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20  arse and return 
2291f 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53  NULL. */.  if( S
22920 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
22921 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
22922 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  se) ){.    retur
22923 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4e  n;.  }..  if( pN
22924 61 6d 65 31 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65  ame1==0 || pName
22925 31 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72  1->z==0 ){.    r
22926 65 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28  eindexDatabases(
22927 70 50 61 72 73 65 2c 20 30 29 3b 0a 20 20 20 20  pParse, 0);.    
22928 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 20  return;.  }else 
22929 69 66 28 20 70 4e 61 6d 65 32 3d 3d 30 20 7c 7c  if( pName2==0 ||
2292a 20 70 4e 61 6d 65 32 2d 3e 7a 3d 3d 30 20 29 7b   pName2->z==0 ){
2292b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  .    char *zColl
2292c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  ;.    assert( pN
2292d 61 6d 65 31 2d 3e 7a 20 29 3b 0a 20 20 20 20 7a  ame1->z );.    z
2292e 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61  Coll = sqlite3Na
2292f 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72  meFromToken(pPar
22930 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 31 29 3b  se->db, pName1);
22931 0a 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20  .    if( !zColl 
22932 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 70 43  ) return;.    pC
22933 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
22934 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43  dCollSeq(db, ENC
22935 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c  (db), zColl, -1,
22936 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f   0);.    if( pCo
22937 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
22938 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  zColl ){.       
22939 20 72 65 69 6e 64 65 78 44 61 74 61 62 61 73 65   reindexDatabase
2293a 73 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29  s(pParse, zColl)
2293b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2293c 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c  3DbFree(db, zCol
2293d 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  l);.      }.    
2293e 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
2293f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
22940 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  e(db, zColl);.  
22941 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  }.  iDb = sqlite
22942 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61  3TwoPartName(pPa
22943 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61  rse, pName1, pNa
22944 6d 65 32 2c 20 26 70 4f 62 6a 4e 61 6d 65 29 3b  me2, &pObjName);
22945 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72  .  if( iDb<0 ) r
22946 65 74 75 72 6e 3b 0a 20 20 7a 20 3d 20 73 71 6c  eturn;.  z = sql
22947 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
22948 6e 28 64 62 2c 20 70 4f 62 6a 4e 61 6d 65 29 3b  n(db, pObjName);
22949 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65  .  if( z==0 ) re
2294a 74 75 72 6e 3b 0a 20 20 7a 44 62 20 3d 20 64 62  turn;.  zDb = db
2294b 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2294c 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ;.  pTab = sqlit
2294d 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
2294e 7a 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 70  z, zDb);.  if( p
2294f 54 61 62 20 29 7b 0a 20 20 20 20 72 65 69 6e 64  Tab ){.    reind
22950 65 78 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  exTable(pParse, 
22951 70 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 73 71  pTab, 0);.    sq
22952 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
22953 7a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  z);.    return;.
22954 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73    }.  pIndex = s
22955 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
22956 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 73  db, z, zDb);.  s
22957 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
22958 20 7a 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65   z);.  if( pInde
22959 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  x ){.    sqlite3
2295a 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
2295b 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
2295c 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
2295d 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72  RefillIndex(pPar
2295e 73 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b  se, pIndex, -1);
2295f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
22960 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
22961 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62  sg(pParse, "unab
22962 6c 65 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74  le to identify t
22963 68 65 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  he object to be 
22964 72 65 69 6e 64 65 78 65 64 22 29 3b 0a 7d 0a 23  reindexed");.}.#
22965 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
22966 75 72 6e 20 61 20 64 79 6e 61 6d 69 63 6c 79 20  urn a dynamicly 
22967 61 6c 6c 6f 63 61 74 65 64 20 4b 65 79 49 6e 66  allocated KeyInf
22968 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
22969 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20   can be used.** 
2296a 77 69 74 68 20 4f 50 5f 4f 70 65 6e 52 65 61 64  with OP_OpenRead
2296b 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65   or OP_OpenWrite
2296c 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 62   to access datab
2296d 61 73 65 20 69 6e 64 65 78 20 70 49 64 78 2e 0a  ase index pIdx..
2296e 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  **.** If success
2296f 66 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74  ful, a pointer t
22970 6f 20 74 68 65 20 6e 65 77 20 73 74 72 75 63 74  o the new struct
22971 75 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ure is returned.
22972 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 2a 2a   In this case.**
22973 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   the caller is r
22974 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63  esponsible for c
22975 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 44 62  alling sqlite3Db
22976 46 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 74 68  Free(db, ) on th
22977 65 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20 70  e returned .** p
22978 6f 69 6e 74 65 72 2e 20 49 66 20 61 6e 20 65 72  ointer. If an er
22979 72 6f 72 20 6f 63 63 75 72 73 20 28 6f 75 74 20  ror occurs (out 
2297a 6f 66 20 6d 65 6d 6f 72 79 20 6f 72 20 6d 69 73  of memory or mis
2297b 73 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a  sing collation .
2297c 2a 2a 20 73 65 71 75 65 6e 63 65 29 2c 20 4e 55  ** sequence), NU
2297d 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  LL is returned a
2297e 6e 64 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  nd the state of 
2297f 70 50 61 72 73 65 20 75 70 64 61 74 65 64 20 74  pParse updated t
22980 6f 20 72 65 66 6c 65 63 74 0a 2a 2a 20 74 68 65  o reflect.** the
22981 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54   error..*/.SQLIT
22982 45 5f 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66  E_PRIVATE KeyInf
22983 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b  o *sqlite3IndexK
22984 65 79 69 6e 66 6f 28 50 61 72 73 65 20 2a 70 50  eyinfo(Parse *pP
22985 61 72 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 64  arse, Index *pId
22986 78 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  x){.  int i;.  i
22987 6e 74 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e  nt nCol = pIdx->
22988 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e  nColumn;.  int n
22989 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 4b  Bytes = sizeof(K
2298a 65 79 49 6e 66 6f 29 20 2b 20 28 6e 43 6f 6c 2d  eyInfo) + (nCol-
2298b 31 29 2a 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65  1)*sizeof(CollSe
2298c 71 2a 29 20 2b 20 6e 43 6f 6c 3b 0a 20 20 73 71  q*) + nCol;.  sq
2298d 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2298e 73 65 2d 3e 64 62 3b 0a 20 20 4b 65 79 49 6e 66  se->db;.  KeyInf
2298f 6f 20 2a 70 4b 65 79 20 3d 20 28 4b 65 79 49 6e  o *pKey = (KeyIn
22990 66 6f 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  fo *)sqlite3DbMa
22991 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79  llocZero(db, nBy
22992 74 65 73 29 3b 0a 0a 20 20 69 66 28 20 70 4b 65  tes);..  if( pKe
22993 79 20 29 7b 0a 20 20 20 20 70 4b 65 79 2d 3e 64  y ){.    pKey->d
22994 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
22995 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f      pKey->aSortO
22996 72 64 65 72 20 3d 20 28 75 38 20 2a 29 26 28 70  rder = (u8 *)&(p
22997 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d  Key->aColl[nCol]
22998 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 26  );.    assert( &
22999 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pKey->aSortOrder
2299a 5b 6e 43 6f 6c 5d 3d 3d 26 28 28 28 75 38 20 2a  [nCol]==&(((u8 *
2299b 29 70 4b 65 79 29 5b 6e 42 79 74 65 73 5d 29 20  )pKey)[nBytes]) 
2299c 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
2299d 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
2299e 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20      char *zColl 
2299f 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  = pIdx->azColl[i
229a0 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ];.      assert(
229a1 20 7a 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20   zColl );.      
229a2 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d  pKey->aColl[i] =
229a3 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f   sqlite3LocateCo
229a4 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43  llSeq(pParse, zC
229a5 6f 6c 6c 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  oll, -1);.      
229a6 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pKey->aSortOrder
229a7 5b 69 5d 20 3d 20 70 49 64 78 2d 3e 61 53 6f 72  [i] = pIdx->aSor
229a8 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20 20 7d  tOrder[i];.    }
229a9 0a 20 20 20 20 70 4b 65 79 2d 3e 6e 46 69 65 6c  .    pKey->nFiel
229aa 64 20 3d 20 28 75 31 36 29 6e 43 6f 6c 3b 0a 20  d = (u16)nCol;. 
229ab 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65   }..  if( pParse
229ac 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 73 71  ->nErr ){.    sq
229ad 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
229ae 70 4b 65 79 29 3b 0a 20 20 20 20 70 4b 65 79 20  pKey);.    pKey 
229af 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
229b0 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  n pKey;.}../****
229b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
229b2 66 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a  f build.c ******
229b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
229b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
229b7 20 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 2e 63   file callback.c
229b8 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
229b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
229bb 20 32 30 30 35 20 4d 61 79 20 32 33 20 0a 2a 2a   2005 May 23 .**
229bc 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
229bd 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
229be 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
229bf 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
229c0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
229c1 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
229c2 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
229c3 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
229c4 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
229c5 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
229c6 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
229c7 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
229c8 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
229c9 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
229ca 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
229cb 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
229cc 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
229cd 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
229ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
229d2 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
229d3 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20  tains functions 
229d4 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74  used to access t
229d5 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68  he internal hash
229d6 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 75 73   tables.** of us
229d7 65 72 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er defined funct
229d8 69 6f 6e 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69  ions and collati
229d9 6f 6e 20 73 65 71 75 65 6e 63 65 73 2e 0a 2a 2a  on sequences..**
229da 0a 2a 2a 20 24 49 64 3a 20 63 61 6c 6c 62 61 63  .** $Id: callbac
229db 6b 2e 63 2c 76 20 31 2e 33 37 20 32 30 30 39 2f  k.c,v 1.37 2009/
229dc 30 33 2f 32 34 20 31 35 3a 30 38 3a 31 30 20 64  03/24 15:08:10 d
229dd 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a  rh Exp $.*/.../*
229de 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 27  .** Invoke the '
229df 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64  collation needed
229e0 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 72 65  ' callback to re
229e1 71 75 65 73 74 20 61 20 63 6f 6c 6c 61 74 69 6f  quest a collatio
229e2 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 69 6e  n sequence.** in
229e3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 65   the database te
229e4 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 6e  xt encoding of n
229e5 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74  ame zName, lengt
229e6 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 20 49 66 20 74  h nName..** If t
229e7 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
229e8 75 65 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20  uence.*/.static 
229e9 76 6f 69 64 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65  void callCollNee
229ea 64 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ded(sqlite3 *db,
229eb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
229ec 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a  me, int nName){.
229ed 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 78    assert( !db->x
229ee 43 6f 6c 6c 4e 65 65 64 65 64 20 7c 7c 20 21 64  CollNeeded || !d
229ef 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36  b->xCollNeeded16
229f0 20 29 3b 0a 20 20 69 66 28 20 6e 4e 61 6d 65 3c   );.  if( nName<
229f1 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  0 ) nName = sqli
229f2 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e  te3Strlen(db, zN
229f3 61 6d 65 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ame);.  if( db->
229f4 78 43 6f 6c 6c 4e 65 65 64 65 64 20 29 7b 0a 20  xCollNeeded ){. 
229f5 20 20 20 63 68 61 72 20 2a 7a 45 78 74 65 72 6e     char *zExtern
229f6 61 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  al = sqlite3DbSt
229f7 72 4e 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 2c  rNDup(db, zName,
229f8 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   nName);.    if(
229f9 20 21 7a 45 78 74 65 72 6e 61 6c 20 29 20 72 65   !zExternal ) re
229fa 74 75 72 6e 3b 0a 20 20 20 20 64 62 2d 3e 78 43  turn;.    db->xC
229fb 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2d 3e 70 43  ollNeeded(db->pC
229fc 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 64 62  ollNeededArg, db
229fd 2c 20 28 69 6e 74 29 45 4e 43 28 64 62 29 2c 20  , (int)ENC(db), 
229fe 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20  zExternal);.    
229ff 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22a00 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20  , zExternal);.  
22a01 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
22a02 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66  _OMIT_UTF16.  if
22a03 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65  ( db->xCollNeede
22a04 64 31 36 20 29 7b 0a 20 20 20 20 63 68 61 72 20  d16 ){.    char 
22a05 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72 6e 61 6c  const *zExternal
22a06 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  ;.    sqlite3_va
22a07 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73 71 6c 69  lue *pTmp = sqli
22a08 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b  te3ValueNew(db);
22a09 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
22a0a 65 53 65 74 53 74 72 28 70 54 6d 70 2c 20 6e 4e  eSetStr(pTmp, nN
22a0b 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49  ame, zName, SQLI
22a0c 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
22a0d 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7a 45 78  STATIC);.    zEx
22a0e 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33  ternal = sqlite3
22a0f 56 61 6c 75 65 54 65 78 74 28 70 54 6d 70 2c 20  ValueText(pTmp, 
22a10 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
22a11 56 45 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 78  VE);.    if( zEx
22a12 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20  ternal ){.      
22a13 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31  db->xCollNeeded1
22a14 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65  6(db->pCollNeede
22a15 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74 29 45  dArg, db, (int)E
22a16 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72 6e 61  NC(db), zExterna
22a17 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  l);.    }.    sq
22a18 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
22a19 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Tmp);.  }.#endif
22a1a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
22a1b 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
22a1c 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   if the collatio
22a1d 6e 20 66 61 63 74 6f 72 79 20 66 61 69 6c 73 20  n factory fails 
22a1e 74 6f 20 64 65 6c 69 76 65 72 20 61 0a 2a 2a 20  to deliver a.** 
22a1f 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69  collation functi
22a20 6f 6e 20 69 6e 20 74 68 65 20 62 65 73 74 20 65  on in the best e
22a21 6e 63 6f 64 69 6e 67 20 62 75 74 20 74 68 65 72  ncoding but ther
22a22 65 20 6d 61 79 20 62 65 20 6f 74 68 65 72 20 76  e may be other v
22a23 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68  ersions.** of th
22a24 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e  is collation fun
22a25 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74 68 65 72  ction (for other
22a26 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 29   text encodings)
22a27 20 61 76 61 69 6c 61 62 6c 65 2e 20 55 73 65 20   available. Use 
22a28 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20  one.** of these 
22a29 69 6e 73 74 65 61 64 20 69 66 20 74 68 65 79 20  instead if they 
22a2a 65 78 69 73 74 2e 20 41 76 6f 69 64 20 61 20 55  exist. Avoid a U
22a2b 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d 31 36 20  TF-8 <-> UTF-16 
22a2c 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 0a 2a 2a  conversion if.**
22a2d 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73 74   possible..*/.st
22a2e 61 74 69 63 20 69 6e 74 20 73 79 6e 74 68 43 6f  atic int synthCo
22a2f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 20 2a 64  llSeq(sqlite3 *d
22a30 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  b, CollSeq *pCol
22a31 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  l){.  CollSeq *p
22a32 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72 20 2a 7a  Coll2;.  char *z
22a33 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b   = pColl->zName;
22a34 0a 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74  .  int n = sqlit
22a35 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20  e3Strlen30(z);. 
22a36 20 69 6e 74 20 69 3b 0a 20 20 73 74 61 74 69 63   int i;.  static
22a37 20 63 6f 6e 73 74 20 75 38 20 61 45 6e 63 5b 5d   const u8 aEnc[]
22a38 20 3d 20 7b 20 53 51 4c 49 54 45 5f 55 54 46 31   = { SQLITE_UTF1
22a39 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  6BE, SQLITE_UTF1
22a3a 36 4c 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  6LE, SQLITE_UTF8
22a3b 20 7d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   };.  for(i=0; i
22a3c 3c 33 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43  <3; i++){.    pC
22a3d 6f 6c 6c 32 20 3d 20 73 71 6c 69 74 65 33 46 69  oll2 = sqlite3Fi
22a3e 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 61 45  ndCollSeq(db, aE
22a3f 6e 63 5b 69 5d 2c 20 7a 2c 20 6e 2c 20 30 29 3b  nc[i], z, n, 0);
22a40 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 32 2d  .    if( pColl2-
22a41 3e 78 43 6d 70 21 3d 30 20 29 7b 0a 20 20 20 20  >xCmp!=0 ){.    
22a42 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c 6c 2c 20    memcpy(pColl, 
22a43 70 43 6f 6c 6c 32 2c 20 73 69 7a 65 6f 66 28 43  pColl2, sizeof(C
22a44 6f 6c 6c 53 65 71 29 29 3b 0a 20 20 20 20 20 20  ollSeq));.      
22a45 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b  pColl->xDel = 0;
22a46 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e           /* Do n
22a47 6f 74 20 63 6f 70 79 20 74 68 65 20 64 65 73 74  ot copy the dest
22a48 72 75 63 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20  ructor */.      
22a49 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
22a4a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
22a4b 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
22a4c 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  R;.}../*.** This
22a4d 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73   function is res
22a4e 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76  ponsible for inv
22a4f 6f 6b 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74  oking the collat
22a50 69 6f 6e 20 66 61 63 74 6f 72 79 20 63 61 6c 6c  ion factory call
22a51 62 61 63 6b 0a 2a 2a 20 6f 72 20 73 75 62 73 74  back.** or subst
22a52 69 74 75 74 69 6e 67 20 61 20 63 6f 6c 6c 61 74  ituting a collat
22a53 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ion sequence of 
22a54 61 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f  a different enco
22a55 64 69 6e 67 20 77 68 65 6e 20 74 68 65 0a 2a 2a  ding when the.**
22a56 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61   requested colla
22a57 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73  tion sequence is
22a58 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69   not available i
22a59 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  n the database n
22a5a 61 74 69 76 65 0a 2a 2a 20 65 6e 63 6f 64 69 6e  ative.** encodin
22a5b 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 69 74 20  g..** .** If it 
22a5c 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
22a5d 6e 20 70 43 6f 6c 6c 20 6d 75 73 74 20 70 6f 69  n pColl must poi
22a5e 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  nt to the databa
22a5f 73 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69  se native encodi
22a60 6e 67 20 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e  ng .** collation
22a61 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20 6e   sequence with n
22a62 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74  ame zName, lengt
22a63 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54  h nName..**.** T
22a64 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
22a65 69 73 20 65 69 74 68 65 72 20 74 68 65 20 63 6f  is either the co
22a66 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
22a67 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 64   to be used in d
22a68 61 74 61 62 61 73 65 0a 2a 2a 20 64 62 20 66 6f  atabase.** db fo
22a69 72 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65  r collation type
22a6a 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e   name zName, len
22a6b 67 74 68 20 6e 4e 61 6d 65 2c 20 6f 72 20 4e 55  gth nName, or NU
22a6c 4c 4c 2c 20 69 66 20 6e 6f 20 63 6f 6c 6c 61 74  LL, if no collat
22a6d 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ion.** sequence 
22a6e 63 61 6e 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f  can be found..*/
22a6f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22a70 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
22a71 47 65 74 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71  GetCollSeq(.  sq
22a72 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 43 6f  lite3* db, .  Co
22a73 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 2c 20 0a 20  llSeq *pColl, . 
22a74 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
22a75 6d 65 2c 20 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  me, .  int nName
22a76 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  .){.  CollSeq *p
22a77 3b 0a 0a 20 20 70 20 3d 20 70 43 6f 6c 6c 3b 0a  ;..  p = pColl;.
22a78 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20    if( !p ){.    
22a79 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  p = sqlite3FindC
22a7a 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
22a7b 62 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  b), zName, nName
22a7c 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 0);.  }.  if( 
22a7d 21 70 20 7c 7c 20 21 70 2d 3e 78 43 6d 70 20 29  !p || !p->xCmp )
22a7e 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c  {.    /* No coll
22a7f 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f  ation sequence o
22a80 66 20 74 68 69 73 20 74 79 70 65 20 66 6f 72 20  f this type for 
22a81 74 68 69 73 20 65 6e 63 6f 64 69 6e 67 20 69 73  this encoding is
22a82 20 72 65 67 69 73 74 65 72 65 64 2e 0a 20 20 20   registered..   
22a83 20 2a 2a 20 43 61 6c 6c 20 74 68 65 20 63 6f 6c   ** Call the col
22a84 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 74  lation factory t
22a85 6f 20 73 65 65 20 69 66 20 69 74 20 63 61 6e 20  o see if it can 
22a86 73 75 70 70 6c 79 20 75 73 20 77 69 74 68 20 6f  supply us with o
22a87 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ne..    */.    c
22a88 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62  allCollNeeded(db
22a89 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
22a8a 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
22a8b 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
22a8c 45 4e 43 28 64 62 29 2c 20 7a 4e 61 6d 65 2c 20  ENC(db), zName, 
22a8d 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20  nName, 0);.  }. 
22a8e 20 69 66 28 20 70 20 26 26 20 21 70 2d 3e 78 43   if( p && !p->xC
22a8f 6d 70 20 26 26 20 73 79 6e 74 68 43 6f 6c 6c 53  mp && synthCollS
22a90 65 71 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20  eq(db, p) ){.   
22a91 20 70 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73   p = 0;.  }.  as
22a92 73 65 72 74 28 20 21 70 20 7c 7c 20 70 2d 3e 78  sert( !p || p->x
22a93 43 6d 70 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  Cmp );.  return 
22a94 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  p;.}../*.** This
22a95 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
22a96 65 64 20 6f 6e 20 61 20 63 6f 6c 6c 61 74 69 6f  ed on a collatio
22a97 6e 20 73 65 71 75 65 6e 63 65 20 62 65 66 6f 72  n sequence befor
22a98 65 20 69 74 20 69 73 20 75 73 65 64 20 74 6f 0a  e it is used to.
22a99 2a 2a 20 63 68 65 63 6b 20 74 68 61 74 20 69 74  ** check that it
22a9a 20 69 73 20 64 65 66 69 6e 65 64 2e 20 41 6e 20   is defined. An 
22a9b 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74  undefined collat
22a9c 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 65 78 69  ion sequence exi
22a9d 73 74 73 20 77 68 65 6e 0a 2a 2a 20 61 20 64 61  sts when.** a da
22a9e 74 61 62 61 73 65 20 69 73 20 6c 6f 61 64 65 64  tabase is loaded
22a9f 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 72   that contains r
22aa0 65 66 65 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c  eferences to col
22aa1 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
22aa2 0a 2a 2a 20 74 68 61 74 20 68 61 76 65 20 6e 6f  .** that have no
22aa3 74 20 62 65 65 6e 20 64 65 66 69 6e 65 64 20 62  t been defined b
22aa4 79 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  y sqlite3_create
22aa5 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 65 74 63  _collation() etc
22aa6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 71 75 69  ..**.** If requi
22aa7 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  red, this routin
22aa8 65 20 63 61 6c 6c 73 20 74 68 65 20 27 63 6f 6c  e calls the 'col
22aa9 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63  lation needed' c
22aaa 61 6c 6c 62 61 63 6b 20 74 6f 0a 2a 2a 20 72 65  allback to.** re
22aab 71 75 65 73 74 20 61 20 64 65 66 69 6e 69 74 69  quest a definiti
22aac 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74  on of the collat
22aad 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 49 66  ing sequence. If
22aae 20 74 68 69 73 20 64 6f 65 73 6e 27 74 20 77 6f   this doesn't wo
22aaf 72 6b 2c 20 0a 2a 2a 20 61 6e 20 65 71 75 69 76  rk, .** an equiv
22ab0 61 6c 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20  alent collating 
22ab1 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 75 73  sequence that us
22ab2 65 73 20 61 20 74 65 78 74 20 65 6e 63 6f 64 69  es a text encodi
22ab3 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20  ng different.** 
22ab4 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61  from the main da
22ab5 74 61 62 61 73 65 20 69 73 20 73 75 62 73 74 69  tabase is substi
22ab6 74 75 74 65 64 2c 20 69 66 20 6f 6e 65 20 69 73  tuted, if one is
22ab7 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53   available..*/.S
22ab8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
22ab9 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f  t sqlite3CheckCo
22aba 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
22abb 72 73 65 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43  rse, CollSeq *pC
22abc 6f 6c 6c 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c  oll){.  if( pCol
22abd 6c 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  l ){.    const c
22abe 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f  har *zName = pCo
22abf 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 43  ll->zName;.    C
22ac0 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 73 71 6c 69  ollSeq *p = sqli
22ac1 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 70 50  te3GetCollSeq(pP
22ac2 61 72 73 65 2d 3e 64 62 2c 20 70 43 6f 6c 6c 2c  arse->db, pColl,
22ac3 20 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20   zName, -1);.   
22ac4 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20   if( !p ){.     
22ac5 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
22ac6 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r==0 ){.        
22ac7 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
22ac8 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
22ac9 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
22aca 6e 63 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29  nce: %s", zName)
22acb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
22acc 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a  pParse->nErr++;.
22acd 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
22ace 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d  ITE_ERROR;.    }
22acf 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d  .    assert( p==
22ad0 70 43 6f 6c 6c 20 29 3b 0a 20 20 7d 0a 20 20 72  pColl );.  }.  r
22ad1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
22ad2 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61  .}..../*.** Loca
22ad3 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e  te and return an
22ad4 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20   entry from the 
22ad5 64 62 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68  db.aCollSeq hash
22ad6 20 74 61 62 6c 65 2e 20 49 66 20 74 68 65 20 65   table. If the e
22ad7 6e 74 72 79 0a 2a 2a 20 73 70 65 63 69 66 69 65  ntry.** specifie
22ad8 64 20 62 79 20 7a 4e 61 6d 65 20 61 6e 64 20 6e  d by zName and n
22ad9 4e 61 6d 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e  Name is not foun
22ada 64 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20  d and parameter 
22adb 27 63 72 65 61 74 65 27 20 69 73 0a 2a 2a 20 74  'create' is.** t
22adc 72 75 65 2c 20 74 68 65 6e 20 63 72 65 61 74 65  rue, then create
22add 20 61 20 6e 65 77 20 65 6e 74 72 79 2e 20 4f 74   a new entry. Ot
22ade 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e  herwise return N
22adf 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  ULL..**.** Each 
22ae0 70 6f 69 6e 74 65 72 20 73 74 6f 72 65 64 20 69  pointer stored i
22ae1 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 43  n the sqlite3.aC
22ae2 6f 6c 6c 53 65 71 20 68 61 73 68 20 74 61 62 6c  ollSeq hash tabl
22ae3 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a  e contains an.**
22ae4 20 61 72 72 61 79 20 6f 66 20 74 68 72 65 65 20   array of three 
22ae5 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72  CollSeq structur
22ae6 65 73 2e 20 54 68 65 20 66 69 72 73 74 20 69 73  es. The first is
22ae7 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
22ae8 65 71 75 65 6e 63 65 0a 2a 2a 20 70 72 65 66 66  equence.** preff
22ae9 65 72 72 65 64 20 66 6f 72 20 55 54 46 2d 38 2c  erred for UTF-8,
22aea 20 74 68 65 20 73 65 63 6f 6e 64 20 55 54 46 2d   the second UTF-
22aeb 31 36 6c 65 2c 20 61 6e 64 20 74 68 65 20 74 68  16le, and the th
22aec 69 72 64 20 55 54 46 2d 31 36 62 65 2e 0a 2a 2a  ird UTF-16be..**
22aed 0a 2a 2a 20 53 74 6f 72 65 64 20 69 6d 6d 65 64  .** Stored immed
22aee 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 68 65  iately after the
22aef 20 74 68 72 65 65 20 63 6f 6c 6c 61 74 69 6f 6e   three collation
22af0 20 73 65 71 75 65 6e 63 65 73 20 69 73 20 61 20   sequences is a 
22af1 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63  copy of.** the c
22af2 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
22af3 65 20 6e 61 6d 65 2e 20 41 20 70 6f 69 6e 74 65  e name. A pointe
22af4 72 20 74 6f 20 74 68 69 73 20 73 74 72 69 6e 67  r to this string
22af5 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a   is stored in.**
22af6 20 65 61 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20   each collation 
22af7 73 65 71 75 65 6e 63 65 20 73 74 72 75 63 74 75  sequence structu
22af8 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f  re..*/.static Co
22af9 6c 6c 53 65 71 20 2a 66 69 6e 64 43 6f 6c 6c 53  llSeq *findCollS
22afa 65 71 45 6e 74 72 79 28 0a 20 20 73 71 6c 69 74  eqEntry(.  sqlit
22afb 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
22afc 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69  char *zName,.  i
22afd 6e 74 20 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  nt nName,.  int 
22afe 63 72 65 61 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c  create.){.  Coll
22aff 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66  Seq *pColl;.  if
22b00 28 20 6e 4e 61 6d 65 3c 30 20 29 20 6e 4e 61 6d  ( nName<0 ) nNam
22b01 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  e = sqlite3Strle
22b02 6e 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  n(db, zName);.  
22b03 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48  pColl = sqlite3H
22b04 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f  ashFind(&db->aCo
22b05 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  llSeq, zName, nN
22b06 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 30 3d 3d  ame);..  if( 0==
22b07 70 43 6f 6c 6c 20 26 26 20 63 72 65 61 74 65 20  pColl && create 
22b08 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  ){.    pColl = s
22b09 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
22b0a 72 6f 28 64 62 2c 20 33 2a 73 69 7a 65 6f 66 28  ro(db, 3*sizeof(
22b0b 2a 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20  *pColl) + nName 
22b0c 2b 20 31 20 29 3b 0a 20 20 20 20 69 66 28 20 70  + 1 );.    if( p
22b0d 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 43 6f  Coll ){.      Co
22b0e 6c 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 30 3b  llSeq *pDel = 0;
22b0f 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e  .      pColl[0].
22b10 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26  zName = (char*)&
22b11 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20  pColl[3];.      
22b12 70 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53  pColl[0].enc = S
22b13 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
22b14 20 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65    pColl[1].zName
22b15 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c   = (char*)&pColl
22b16 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  [3];.      pColl
22b17 5b 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45  [1].enc = SQLITE
22b18 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 20 20  _UTF16LE;.      
22b19 70 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d  pColl[2].zName =
22b1a 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33   (char*)&pColl[3
22b1b 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32  ];.      pColl[2
22b1c 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ].enc = SQLITE_U
22b1d 54 46 31 36 42 45 3b 0a 20 20 20 20 20 20 6d 65  TF16BE;.      me
22b1e 6d 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e  mcpy(pColl[0].zN
22b1f 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
22b20 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b  e);.      pColl[
22b21 30 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20  0].zName[nName] 
22b22 3d 20 30 3b 0a 20 20 20 20 20 20 70 44 65 6c 20  = 0;.      pDel 
22b23 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
22b24 65 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ert(&db->aCollSe
22b25 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d  q, pColl[0].zNam
22b26 65 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29  e, nName, pColl)
22b27 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61  ;..      /* If a
22b28 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
22b29 65 20 6f 63 63 75 72 72 65 64 20 69 6e 20 73 71  e occurred in sq
22b2a 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
22b2b 29 2c 20 69 74 20 77 69 6c 6c 20 0a 20 20 20 20  ), it will .    
22b2c 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20    ** return the 
22b2d 70 43 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74 6f  pColl pointer to
22b2e 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 65 63   be deleted (bec
22b2f 61 75 73 65 20 69 74 20 77 61 73 6e 27 74 20 61  ause it wasn't a
22b30 64 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f  dded.      ** to
22b31 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 29   the hash table)
22b32 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
22b33 20 61 73 73 65 72 74 28 20 70 44 65 6c 3d 3d 30   assert( pDel==0
22b34 20 7c 7c 20 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20   || pDel==pColl 
22b35 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65  );.      if( pDe
22b36 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  l!=0 ){.        
22b37 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
22b38 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71   = 1;.        sq
22b39 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
22b3a 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 70  pDel);.        p
22b3b 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  Coll = 0;.      
22b3c 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
22b3d 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  turn pColl;.}../
22b3e 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
22b3f 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61  Name points to a
22b40 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 73   UTF-8 encoded s
22b41 74 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79 74 65  tring nName byte
22b42 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75 72  s long..** Retur
22b43 6e 20 74 68 65 20 43 6f 6c 6c 53 65 71 2a 20 70  n the CollSeq* p
22b44 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20 63  ointer for the c
22b45 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
22b46 65 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a  e named zName.**
22b47 20 66 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e   for the encodin
22b48 67 20 27 65 6e 63 27 20 66 72 6f 6d 20 74 68 65  g 'enc' from the
22b49 20 64 61 74 61 62 61 73 65 20 27 64 62 27 2e 0a   database 'db'..
22b4a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 6e 74  **.** If the ent
22b4b 72 79 20 73 70 65 63 69 66 69 65 64 20 69 73 20  ry specified is 
22b4c 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 27 63  not found and 'c
22b4d 72 65 61 74 65 27 20 69 73 20 74 72 75 65 2c 20  reate' is true, 
22b4e 74 68 65 6e 20 63 72 65 61 74 65 20 61 0a 2a 2a  then create a.**
22b4f 20 6e 65 77 20 65 6e 74 72 79 2e 20 20 4f 74 68   new entry.  Oth
22b50 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55  erwise return NU
22b51 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 70 61  LL..**.** A sepa
22b52 72 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 71  rate function sq
22b53 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
22b54 65 71 28 29 20 69 73 20 61 20 77 72 61 70 70 65  eq() is a wrappe
22b55 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 69 73  r around.** this
22b56 20 72 6f 75 74 69 6e 65 2e 20 20 73 71 6c 69 74   routine.  sqlit
22b57 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
22b58 29 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f  ) invokes the co
22b59 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 0a  llation factory.
22b5a 2a 2a 20 69 66 20 6e 65 63 65 73 73 61 72 79 20  ** if necessary 
22b5b 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e  and generates an
22b5c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
22b5d 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  f the collating 
22b5e 73 65 71 75 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e  sequence.** cann
22b5f 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  ot be found..*/.
22b60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
22b61 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 46  ollSeq *sqlite3F
22b62 69 6e 64 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71  indCollSeq(.  sq
22b63 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 75 38 20  lite3 *db,.  u8 
22b64 65 6e 63 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  enc,.  const cha
22b65 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  r *zName,.  int 
22b66 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65  nName,.  int cre
22b67 61 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  ate.){.  CollSeq
22b68 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 7a   *pColl;.  if( z
22b69 4e 61 6d 65 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Name ){.    pCol
22b6a 6c 20 3d 20 66 69 6e 64 43 6f 6c 6c 53 65 71 45  l = findCollSeqE
22b6b 6e 74 72 79 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  ntry(db, zName, 
22b6c 6e 4e 61 6d 65 2c 20 63 72 65 61 74 65 29 3b 0a  nName, create);.
22b6d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f    }else{.    pCo
22b6e 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
22b6f 6c 6c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ll;.  }.  assert
22b70 28 20 53 51 4c 49 54 45 5f 55 54 46 38 3d 3d 31  ( SQLITE_UTF8==1
22b71 20 26 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36   && SQLITE_UTF16
22b72 4c 45 3d 3d 32 20 26 26 20 53 51 4c 49 54 45 5f  LE==2 && SQLITE_
22b73 55 54 46 31 36 42 45 3d 3d 33 20 29 3b 0a 20 20  UTF16BE==3 );.  
22b74 61 73 73 65 72 74 28 20 65 6e 63 3e 3d 53 51 4c  assert( enc>=SQL
22b75 49 54 45 5f 55 54 46 38 20 26 26 20 65 6e 63 3c  ITE_UTF8 && enc<
22b76 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
22b77 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  );.  if( pColl )
22b78 20 70 43 6f 6c 6c 20 2b 3d 20 65 6e 63 2d 31 3b   pColl += enc-1;
22b79 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b  .  return pColl;
22b7a 0a 7d 0a 0a 2f 2a 20 44 75 72 69 6e 67 20 74 68  .}../* During th
22b7b 65 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65  e search for the
22b7c 20 62 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 64   best function d
22b7d 65 66 69 6e 69 74 69 6f 6e 2c 20 74 68 69 73 20  efinition, this 
22b7e 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20  procedure.** is 
22b7f 63 61 6c 6c 65 64 20 74 6f 20 74 65 73 74 20 68  called to test h
22b80 6f 77 20 77 65 6c 6c 20 74 68 65 20 66 75 6e 63  ow well the func
22b81 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74  tion passed as t
22b82 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
22b83 74 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65  t.** matches the
22b84 20 72 65 71 75 65 73 74 20 66 6f 72 20 61 20 66   request for a f
22b85 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e 41 72  unction with nAr
22b86 67 20 61 72 67 75 6d 65 6e 74 73 20 69 6e 20 61  g arguments in a
22b87 20 73 79 73 74 65 6d 0a 2a 2a 20 74 68 61 74 20   system.** that 
22b88 75 73 65 73 20 65 6e 63 6f 64 69 6e 67 20 65 6e  uses encoding en
22b89 63 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  c. The value ret
22b8a 75 72 6e 65 64 20 69 6e 64 69 63 61 74 65 73 20  urned indicates 
22b8b 68 6f 77 20 77 65 6c 6c 20 74 68 65 0a 2a 2a 20  how well the.** 
22b8c 72 65 71 75 65 73 74 20 69 73 20 6d 61 74 63 68  request is match
22b8d 65 64 2e 20 41 20 68 69 67 68 65 72 20 76 61 6c  ed. A higher val
22b8e 75 65 20 69 6e 64 69 63 61 74 65 73 20 61 20 62  ue indicates a b
22b8f 65 74 74 65 72 20 6d 61 74 63 68 2e 0a 2a 2a 0a  etter match..**.
22b90 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
22b91 76 61 6c 75 65 20 69 73 20 61 6c 77 61 79 73 20  value is always 
22b92 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 36 2c  between 0 and 6,
22b93 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
22b94 2a 2a 20 30 3a 20 4e 6f 74 20 61 20 6d 61 74 63  ** 0: Not a matc
22b95 68 2c 20 6f 72 20 69 66 20 6e 41 72 67 3c 30 20  h, or if nArg<0 
22b96 61 6e 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  and the function
22b97 20 69 73 20 68 61 73 20 6e 6f 20 69 6d 70 6c 65   is has no imple
22b98 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 31 3a  mentation..** 1:
22b99 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75   A variable argu
22b9a 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 74  ments function t
22b9b 68 61 74 20 70 72 65 66 65 72 73 20 55 54 46 2d  hat prefers UTF-
22b9c 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36 0a  8 when a UTF-16.
22b9d 2a 2a 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 69  **    encoding i
22b9e 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20  s requested, or 
22b9f 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 32  vice versa..** 2
22ba0 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67  : A variable arg
22ba1 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20  uments function 
22ba2 74 68 61 74 20 75 73 65 73 20 55 54 46 2d 31 36  that uses UTF-16
22ba3 42 45 20 77 68 65 6e 20 55 54 46 2d 31 36 4c 45  BE when UTF-16LE
22ba4 20 69 73 0a 2a 2a 20 20 20 20 72 65 71 75 65 73   is.**    reques
22ba5 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72  ted, or vice ver
22ba6 73 61 2e 0a 2a 2a 20 33 3a 20 41 20 76 61 72 69  sa..** 3: A vari
22ba7 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66  able arguments f
22ba8 75 6e 63 74 69 6f 6e 20 75 73 69 6e 67 20 74 68  unction using th
22ba9 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63 6f  e same text enco
22baa 64 69 6e 67 2e 0a 2a 2a 20 34 3a 20 41 20 66 75  ding..** 4: A fu
22bab 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
22bac 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66 20  exact number of 
22bad 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65 73  arguments reques
22bae 74 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20 70  ted that.**    p
22baf 72 65 66 65 72 73 20 55 54 46 2d 38 20 77 68 65  refers UTF-8 whe
22bb0 6e 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  n a UTF-16 encod
22bb1 69 6e 67 20 69 73 20 72 65 71 75 65 73 74 65 64  ing is requested
22bb2 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e  , or vice versa.
22bb3 0a 2a 2a 20 35 3a 20 41 20 66 75 6e 63 74 69 6f  .** 5: A functio
22bb4 6e 20 77 69 74 68 20 74 68 65 20 65 78 61 63 74  n with the exact
22bb5 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
22bb6 65 6e 74 73 20 72 65 71 75 65 73 74 65 64 20 74  ents requested t
22bb7 68 61 74 0a 2a 2a 20 20 20 20 70 72 65 66 65 72  hat.**    prefer
22bb8 73 20 55 54 46 2d 31 36 4c 45 20 77 68 65 6e 20  s UTF-16LE when 
22bb9 55 54 46 2d 31 36 42 45 20 69 73 20 72 65 71 75  UTF-16BE is requ
22bba 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76  ested, or vice v
22bbb 65 72 73 61 2e 0a 2a 2a 20 36 3a 20 41 6e 20 65  ersa..** 6: An e
22bbc 78 61 63 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a  xact match..**.*
22bbd 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61 74  /.static int mat
22bbe 63 68 51 75 61 6c 69 74 79 28 46 75 6e 63 44 65  chQuality(FuncDe
22bbf 66 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20  f *p, int nArg, 
22bc0 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 6d  u8 enc){.  int m
22bc1 61 74 63 68 20 3d 20 30 3b 0a 20 20 69 66 28 20  atch = 0;.  if( 
22bc2 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 70  p->nArg==-1 || p
22bc3 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 0a 20 20  ->nArg==nArg .  
22bc4 20 7c 7c 20 28 6e 41 72 67 3d 3d 2d 31 20 26 26   || (nArg==-1 &&
22bc5 20 28 70 2d 3e 78 46 75 6e 63 21 3d 30 20 7c 7c   (p->xFunc!=0 ||
22bc6 20 70 2d 3e 78 53 74 65 70 21 3d 30 29 29 0a 20   p->xStep!=0)). 
22bc7 20 29 7b 0a 20 20 20 20 6d 61 74 63 68 20 3d 20   ){.    match = 
22bc8 31 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 41  1;.    if( p->nA
22bc9 72 67 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 72 67  rg==nArg || nArg
22bca 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 6d 61  ==-1 ){.      ma
22bcb 74 63 68 20 3d 20 34 3b 0a 20 20 20 20 7d 0a 20  tch = 4;.    }. 
22bcc 20 20 20 69 66 28 20 65 6e 63 3d 3d 70 2d 3e 69     if( enc==p->i
22bcd 50 72 65 66 45 6e 63 20 29 7b 0a 20 20 20 20 20  PrefEnc ){.     
22bce 20 6d 61 74 63 68 20 2b 3d 20 32 3b 0a 20 20 20   match += 2;.   
22bcf 20 7d 0a 20 20 20 20 65 6c 73 65 20 69 66 28 20   }.    else if( 
22bd0 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  (enc==SQLITE_UTF
22bd1 31 36 4c 45 20 26 26 20 70 2d 3e 69 50 72 65 66  16LE && p->iPref
22bd2 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  Enc==SQLITE_UTF1
22bd3 36 42 45 29 20 7c 7c 0a 20 20 20 20 20 20 20 20  6BE) ||.        
22bd4 20 20 20 20 20 28 65 6e 63 3d 3d 53 51 4c 49 54       (enc==SQLIT
22bd5 45 5f 55 54 46 31 36 42 45 20 26 26 20 70 2d 3e  E_UTF16BE && p->
22bd6 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54 45  iPrefEnc==SQLITE
22bd7 5f 55 54 46 31 36 4c 45 29 20 29 7b 0a 20 20 20  _UTF16LE) ){.   
22bd8 20 20 20 6d 61 74 63 68 20 2b 3d 20 31 3b 0a 20     match += 1;. 
22bd9 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
22bda 6e 20 6d 61 74 63 68 3b 0a 7d 0a 0a 2f 2a 0a 2a  n match;.}../*.*
22bdb 2a 20 53 65 61 72 63 68 20 61 20 46 75 6e 63 44  * Search a FuncD
22bdc 65 66 48 61 73 68 20 66 6f 72 20 61 20 66 75 6e  efHash for a fun
22bdd 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67  ction with the g
22bde 69 76 65 6e 20 6e 61 6d 65 2e 20 20 52 65 74 75  iven name.  Retu
22bdf 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
22be0 74 6f 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20  to the matching 
22be1 46 75 6e 63 44 65 66 20 69 66 20 66 6f 75 6e 64  FuncDef if found
22be2 2c 20 6f 72 20 30 20 69 66 20 74 68 65 72 65 20  , or 0 if there 
22be3 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a  is no match..*/.
22be4 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20 2a  static FuncDef *
22be5 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 0a  functionSearch(.
22be6 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70    FuncDefHash *p
22be7 48 61 73 68 2c 20 20 2f 2a 20 48 61 73 68 20 74  Hash,  /* Hash t
22be8 61 62 6c 65 20 74 6f 20 73 65 61 72 63 68 20 2a  able to search *
22be9 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20 20  /.  int h,      
22bea 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68           /* Hash
22beb 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a   of the name */.
22bec 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
22bed 75 6e 63 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  unc,   /* Name o
22bee 66 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  f function */.  
22bef 69 6e 74 20 6e 46 75 6e 63 20 20 20 20 20 20 20  int nFunc       
22bf0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
22bf1 66 20 62 79 74 65 73 20 69 6e 20 7a 46 75 6e 63  f bytes in zFunc
22bf2 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66   */.){.  FuncDef
22bf3 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 48 61   *p;.  for(p=pHa
22bf4 73 68 2d 3e 61 5b 68 5d 3b 20 70 3b 20 70 3d 70  sh->a[h]; p; p=p
22bf5 2d 3e 70 48 61 73 68 29 7b 0a 20 20 20 20 69 66  ->pHash){.    if
22bf6 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ( sqlite3StrNICm
22bf7 70 28 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 46 75 6e  p(p->zName, zFun
22bf8 63 2c 20 6e 46 75 6e 63 29 3d 3d 30 20 26 26 20  c, nFunc)==0 && 
22bf9 70 2d 3e 7a 4e 61 6d 65 5b 6e 46 75 6e 63 5d 3d  p->zName[nFunc]=
22bfa 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
22bfb 72 6e 20 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn p;.    }.  }.
22bfc 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
22bfd 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65  *.** Insert a ne
22bfe 77 20 46 75 6e 63 44 65 66 20 69 6e 74 6f 20 61  w FuncDef into a
22bff 20 46 75 6e 63 44 65 66 48 61 73 68 20 68 61 73   FuncDefHash has
22c00 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  h table..*/.SQLI
22c01 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
22c02 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e  sqlite3FuncDefIn
22c03 73 65 72 74 28 0a 20 20 46 75 6e 63 44 65 66 48  sert(.  FuncDefH
22c04 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a 20  ash *pHash,  /* 
22c05 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69  The hash table i
22c06 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 69 6e 73  nto which to ins
22c07 65 72 74 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66  ert */.  FuncDef
22c08 20 2a 70 44 65 66 20 20 20 20 20 20 20 20 2f 2a   *pDef        /*
22c09 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65   The function de
22c0a 66 69 6e 69 74 69 6f 6e 20 74 6f 20 69 6e 73 65  finition to inse
22c0b 72 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44  rt */.){.  FuncD
22c0c 65 66 20 2a 70 4f 74 68 65 72 3b 0a 20 20 69 6e  ef *pOther;.  in
22c0d 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  t nName = sqlite
22c0e 33 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e  3Strlen30(pDef->
22c0f 7a 4e 61 6d 65 29 3b 0a 20 20 75 38 20 63 31 20  zName);.  u8 c1 
22c10 3d 20 28 75 38 29 70 44 65 66 2d 3e 7a 4e 61 6d  = (u8)pDef->zNam
22c11 65 5b 30 5d 3b 0a 20 20 69 6e 74 20 68 20 3d 20  e[0];.  int h = 
22c12 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c  (sqlite3UpperToL
22c13 6f 77 65 72 5b 63 31 5d 20 2b 20 6e 4e 61 6d 65  ower[c1] + nName
22c14 29 20 25 20 41 72 72 61 79 53 69 7a 65 28 70 48  ) % ArraySize(pH
22c15 61 73 68 2d 3e 61 29 3b 0a 20 20 70 4f 74 68 65  ash->a);.  pOthe
22c16 72 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72  r = functionSear
22c17 63 68 28 70 48 61 73 68 2c 20 68 2c 20 70 44 65  ch(pHash, h, pDe
22c18 66 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  f->zName, nName)
22c19 3b 0a 20 20 69 66 28 20 70 4f 74 68 65 72 20 29  ;.  if( pOther )
22c1a 7b 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78  {.    pDef->pNex
22c1b 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78  t = pOther->pNex
22c1c 74 3b 0a 20 20 20 20 70 4f 74 68 65 72 2d 3e 70  t;.    pOther->p
22c1d 4e 65 78 74 20 3d 20 70 44 65 66 3b 0a 20 20 7d  Next = pDef;.  }
22c1e 65 6c 73 65 7b 0a 20 20 20 20 70 44 65 66 2d 3e  else{.    pDef->
22c1f 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70  pNext = 0;.    p
22c20 44 65 66 2d 3e 70 48 61 73 68 20 3d 20 70 48 61  Def->pHash = pHa
22c21 73 68 2d 3e 61 5b 68 5d 3b 0a 20 20 20 20 70 48  sh->a[h];.    pH
22c22 61 73 68 2d 3e 61 5b 68 5d 20 3d 20 70 44 65 66  ash->a[h] = pDef
22c23 3b 0a 20 20 7d 0a 7d 0a 20 20 0a 20 20 0a 0a 2f  ;.  }.}.  .  ../
22c24 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20 75 73  *.** Locate a us
22c25 65 72 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65  er function give
22c26 6e 20 61 20 6e 61 6d 65 2c 20 61 20 6e 75 6d 62  n a name, a numb
22c27 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
22c28 61 6e 64 20 61 20 66 6c 61 67 0a 2a 2a 20 69 6e  and a flag.** in
22c29 64 69 63 61 74 69 6e 67 20 77 68 65 74 68 65 72  dicating whether
22c2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 72   the function pr
22c2b 65 66 65 72 73 20 55 54 46 2d 31 36 20 6f 76 65  efers UTF-16 ove
22c2c 72 20 55 54 46 2d 38 2e 20 20 52 65 74 75 72 6e  r UTF-8.  Return
22c2d 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f   a.** pointer to
22c2e 20 74 68 65 20 46 75 6e 63 44 65 66 20 73 74 72   the FuncDef str
22c2f 75 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69  ucture that defi
22c30 6e 65 73 20 74 68 61 74 20 66 75 6e 63 74 69 6f  nes that functio
22c31 6e 2c 20 6f 72 20 72 65 74 75 72 6e 0a 2a 2a 20  n, or return.** 
22c32 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 75 6e 63  NULL if the func
22c33 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78  tion does not ex
22c34 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ist..**.** If th
22c35 65 20 63 72 65 61 74 65 46 6c 61 67 20 61 72 67  e createFlag arg
22c36 75 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74  ument is true, t
22c37 68 65 6e 20 61 20 6e 65 77 20 28 62 6c 61 6e 6b  hen a new (blank
22c38 29 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72  ) FuncDef.** str
22c39 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65  ucture is create
22c3a 64 20 61 6e 64 20 6c 69 6b 65 64 20 69 6e 74 6f  d and liked into
22c3b 20 74 68 65 20 22 64 62 22 20 73 74 72 75 63 74   the "db" struct
22c3c 75 72 65 20 69 66 20 61 0a 2a 2a 20 6e 6f 20 6d  ure if a.** no m
22c3d 61 74 63 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e  atching function
22c3e 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73   previously exis
22c3f 74 65 64 2e 20 20 57 68 65 6e 20 63 72 65 61 74  ted.  When creat
22c40 65 46 6c 61 67 20 69 73 20 74 72 75 65 0a 2a 2a  eFlag is true.**
22c41 20 61 6e 64 20 74 68 65 20 6e 41 72 67 20 70 61   and the nArg pa
22c42 72 61 6d 65 74 65 72 20 69 73 20 2d 31 2c 20 74  rameter is -1, t
22c43 68 65 6e 20 6f 6e 6c 79 20 61 20 66 75 6e 63 74  hen only a funct
22c44 69 6f 6e 20 74 68 61 74 20 61 63 63 65 70 74 73  ion that accepts
22c45 0a 2a 2a 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f  .** any number o
22c46 66 20 61 72 67 75 6d 65 6e 74 73 20 77 69 6c 6c  f arguments will
22c47 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   be returned..**
22c48 0a 2a 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61  .** If createFla
22c49 67 20 69 73 20 66 61 6c 73 65 20 61 6e 64 20 6e  g is false and n
22c4a 41 72 67 20 69 73 20 2d 31 2c 20 74 68 65 6e 20  Arg is -1, then 
22c4b 74 68 65 20 66 69 72 73 74 20 76 61 6c 69 64 0a  the first valid.
22c4c 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 6f 75 6e  ** function foun
22c4d 64 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  d is returned.  
22c4e 41 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 76 61  A function is va
22c4f 6c 69 64 20 69 66 20 65 69 74 68 65 72 20 78 46  lid if either xF
22c50 75 6e 63 0a 2a 2a 20 6f 72 20 78 53 74 65 70 20  unc.** or xStep 
22c51 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a  is non-zero..**.
22c52 2a 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61 67  ** If createFlag
22c53 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20   is false, then 
22c54 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  a function with 
22c55 74 68 65 20 72 65 71 75 69 72 65 64 20 6e 61 6d  the required nam
22c56 65 20 61 6e 64 0a 2a 2a 20 6e 75 6d 62 65 72 20  e and.** number 
22c57 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6d 61 79  of arguments may
22c58 20 62 65 20 72 65 74 75 72 6e 65 64 20 65 76 65   be returned eve
22c59 6e 20 69 66 20 74 68 65 20 65 54 65 78 74 52 65  n if the eTextRe
22c5a 70 20 66 6c 61 67 20 64 6f 65 73 20 6e 6f 74 0a  p flag does not.
22c5b 2a 2a 20 6d 61 74 63 68 20 74 68 61 74 20 72 65  ** match that re
22c5c 71 75 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  quested..*/.SQLI
22c5d 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44  TE_PRIVATE FuncD
22c5e 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46  ef *sqlite3FindF
22c5f 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74  unction(.  sqlit
22c60 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a  e3 *db,       /*
22c61 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73   An open databas
22c62 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
22c63 72 20 2a 7a 4e 61 6d 65 2c 20 2f 2a 20 4e 61 6d  r *zName, /* Nam
22c64 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  e of the functio
22c65 6e 2e 20 20 4e 6f 74 20 6e 75 6c 6c 2d 74 65 72  n.  Not null-ter
22c66 6d 69 6e 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74  minated */.  int
22c67 20 6e 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20   nName,         
22c68 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
22c69 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 6e  racters in the n
22c6a 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72  ame */.  int nAr
22c6b 67 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  g,          /* N
22c6c 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
22c6d 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 61 6e  ts.  -1 means an
22c6e 79 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38  y number */.  u8
22c6f 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20   enc,           
22c70 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65   /* Preferred te
22c71 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  xt encoding */. 
22c72 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 20   int createFlag 
22c73 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65      /* Create ne
22c74 77 20 65 6e 74 72 79 20 69 66 20 74 72 75 65 20  w entry if true 
22c75 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6f 74 68  and does not oth
22c76 65 72 77 69 73 65 20 65 78 69 73 74 20 2a 2f 0a  erwise exist */.
22c77 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b  ){.  FuncDef *p;
22c78 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
22c79 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
22c7a 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 42 65 73  .  FuncDef *pBes
22c7b 74 20 3d 20 30 3b 20 2f 2a 20 42 65 73 74 20 6d  t = 0; /* Best m
22c7c 61 74 63 68 20 66 6f 75 6e 64 20 73 6f 20 66 61  atch found so fa
22c7d 72 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73 74 53  r */.  int bestS
22c7e 63 6f 72 65 20 3d 20 30 3b 20 20 2f 2a 20 53 63  core = 0;  /* Sc
22c7f 6f 72 65 20 6f 66 20 62 65 73 74 20 6d 61 74 63  ore of best matc
22c80 68 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20  h */.  int h;   
22c81 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
22c82 73 68 20 76 61 6c 75 65 20 2a 2f 0a 0a 0a 20 20  sh value */...  
22c83 61 73 73 65 72 74 28 20 65 6e 63 3d 3d 53 51 4c  assert( enc==SQL
22c84 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63 3d  ITE_UTF8 || enc=
22c85 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
22c86 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  || enc==SQLITE_U
22c87 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20  TF16BE );.  if( 
22c88 6e 41 72 67 3c 2d 31 20 29 20 6e 41 72 67 20 3d  nArg<-1 ) nArg =
22c89 20 2d 31 3b 0a 20 20 68 20 3d 20 28 73 71 6c 69   -1;.  h = (sqli
22c8a 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
22c8b 28 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20  (u8)zName[0]] + 
22c8c 6e 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69  nName) % ArraySi
22c8d 7a 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b  ze(db->aFunc.a);
22c8e 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61  ..  /* First sea
22c8f 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68 20  rch for a match 
22c90 61 6d 6f 6e 67 73 74 20 74 68 65 20 61 70 70 6c  amongst the appl
22c91 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
22c92 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  functions..  */.
22c93 20 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65    p = functionSe
22c94 61 72 63 68 28 26 64 62 2d 3e 61 46 75 6e 63 2c  arch(&db->aFunc,
22c95 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65   h, zName, nName
22c96 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  );.  while( p ){
22c97 0a 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d  .    int score =
22c98 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c   matchQuality(p,
22c99 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20   nArg, enc);.   
22c9a 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53   if( score>bestS
22c9b 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70 42  core ){.      pB
22c9c 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 62  est = p;.      b
22c9d 65 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65  estScore = score
22c9e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20  ;.    }.    p = 
22c9f 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20  p->pNext;.  }.. 
22ca0 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 20   /* If no match 
22ca1 69 73 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68  is found, search
22ca2 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 66 75   the built-in fu
22ca3 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20  nctions..  **.  
22ca4 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 63 72  ** Except, if cr
22ca5 65 61 74 65 46 6c 61 67 20 69 73 20 74 72 75 65  eateFlag is true
22ca6 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
22ca7 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20  t we are trying 
22ca8 74 6f 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20  to.  ** install 
22ca9 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20  a new function. 
22caa 20 57 68 61 74 65 76 65 72 20 46 75 6e 63 44 65   Whatever FuncDe
22cab 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72  f structure is r
22cac 65 74 75 72 6e 65 64 20 77 69 6c 6c 0a 20 20 2a  eturned will.  *
22cad 2a 20 68 61 76 65 20 66 69 65 6c 64 73 20 6f 76  * have fields ov
22cae 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 6e  erwritten with n
22caf 65 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ew information a
22cb0 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
22cb1 68 65 0a 20 20 2a 2a 20 6e 65 77 20 66 75 6e 63  he.  ** new func
22cb2 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20 46  tion.  But the F
22cb3 75 6e 63 44 65 66 73 20 66 6f 72 20 62 75 69 6c  uncDefs for buil
22cb4 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 61  t-in functions a
22cb5 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20  re read-only..  
22cb6 2a 2a 20 53 6f 20 77 65 20 6d 75 73 74 20 6e 6f  ** So we must no
22cb7 74 20 73 65 61 72 63 68 20 66 6f 72 20 62 75 69  t search for bui
22cb8 6c 74 2d 69 6e 73 20 77 68 65 6e 20 63 72 65 61  lt-ins when crea
22cb9 74 69 6e 67 20 61 20 6e 65 77 20 66 75 6e 63 74  ting a new funct
22cba 69 6f 6e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28  ion..  */ .  if(
22cbb 20 21 63 72 65 61 74 65 46 6c 61 67 20 26 26 20   !createFlag && 
22cbc 21 70 42 65 73 74 20 29 7b 0a 20 20 20 20 46 75  !pBest ){.    Fu
22cbd 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
22cbe 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44   = &GLOBAL(FuncD
22cbf 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47  efHash, sqlite3G
22cc0 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b  lobalFunctions);
22cc1 0a 20 20 20 20 70 20 3d 20 66 75 6e 63 74 69 6f  .    p = functio
22cc2 6e 53 65 61 72 63 68 28 70 48 61 73 68 2c 20 68  nSearch(pHash, h
22cc3 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
22cc4 0a 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b  .    while( p ){
22cc5 0a 20 20 20 20 20 20 69 6e 74 20 73 63 6f 72 65  .      int score
22cc6 20 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28   = matchQuality(
22cc7 70 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20  p, nArg, enc);. 
22cc8 20 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e 62       if( score>b
22cc9 65 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20 20  estScore ){.    
22cca 20 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20      pBest = p;. 
22ccb 20 20 20 20 20 20 20 62 65 73 74 53 63 6f 72 65         bestScore
22ccc 20 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 20 20   = score;.      
22ccd 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70  }.      p = p->p
22cce 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Next;.    }.  }.
22ccf 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72 65  .  /* If the cre
22cd0 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65  ateFlag paramete
22cd1 72 20 69 73 20 74 72 75 65 20 61 6e 64 20 74 68  r is true and th
22cd2 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74  e search did not
22cd3 20 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a 20   reveal an.  ** 
22cd4 65 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20  exact match for 
22cd5 74 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65 72  the name, number
22cd6 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e   of arguments an
22cd7 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e  d encoding, then
22cd8 20 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77 20   add a.  ** new 
22cd9 65 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61 73  entry to the has
22cda 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74 75  h table and retu
22cdb 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  rn it..  */.  if
22cdc 28 20 63 72 65 61 74 65 46 6c 61 67 20 26 26 20  ( createFlag && 
22cdd 28 62 65 73 74 53 63 6f 72 65 3c 36 20 7c 7c 20  (bestScore<6 || 
22cde 70 42 65 73 74 2d 3e 6e 41 72 67 21 3d 6e 41 72  pBest->nArg!=nAr
22cdf 67 29 20 26 26 20 0a 20 20 20 20 20 20 28 70 42  g) && .      (pB
22ce0 65 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  est = sqlite3DbM
22ce1 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
22ce2 7a 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e 61  zeof(*pBest)+nNa
22ce3 6d 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20 20 20  me+1))!=0 ){.   
22ce4 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20   pBest->zName = 
22ce5 28 63 68 61 72 20 2a 29 26 70 42 65 73 74 5b 31  (char *)&pBest[1
22ce6 5d 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 6e 41  ];.    pBest->nA
22ce7 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b 0a  rg = (u16)nArg;.
22ce8 20 20 20 20 70 42 65 73 74 2d 3e 69 50 72 65 66      pBest->iPref
22ce9 45 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 20 20 6d  Enc = enc;.    m
22cea 65 6d 63 70 79 28 70 42 65 73 74 2d 3e 7a 4e 61  emcpy(pBest->zNa
22ceb 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  me, zName, nName
22cec 29 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a 4e  );.    pBest->zN
22ced 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a  ame[nName] = 0;.
22cee 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44      sqlite3FuncD
22cef 65 66 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 46  efInsert(&db->aF
22cf0 75 6e 63 2c 20 70 42 65 73 74 29 3b 0a 20 20 7d  unc, pBest);.  }
22cf1 0a 0a 20 20 69 66 28 20 70 42 65 73 74 20 26 26  ..  if( pBest &&
22cf2 20 28 70 42 65 73 74 2d 3e 78 53 74 65 70 20 7c   (pBest->xStep |
22cf3 7c 20 70 42 65 73 74 2d 3e 78 46 75 6e 63 20 7c  | pBest->xFunc |
22cf4 7c 20 63 72 65 61 74 65 46 6c 61 67 29 20 29 7b  | createFlag) ){
22cf5 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 65 73  .    return pBes
22cf6 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
22cf7 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  0;.}../*.** Free
22cf8 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 68   all resources h
22cf9 65 6c 64 20 62 79 20 74 68 65 20 73 63 68 65 6d  eld by the schem
22cfa 61 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65  a structure. The
22cfb 20 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e 74 20   void* argument 
22cfc 70 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61 20 53  points.** at a S
22cfd 63 68 65 6d 61 20 73 74 72 75 63 74 2e 20 54 68  chema struct. Th
22cfe 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
22cff 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65   not call sqlite
22d00 33 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f 6e  3DbFree(db, ) on
22d01 20 74 68 65 20 0a 2a 2a 20 70 6f 69 6e 74 65 72   the .** pointer
22d02 20 69 74 73 65 6c 66 2c 20 69 74 20 6a 75 73 74   itself, it just
22d03 20 63 6c 65 61 6e 73 20 75 70 20 73 75 62 73 69   cleans up subsi
22d04 64 75 61 72 79 20 72 65 73 6f 75 72 63 65 73 20  duary resources 
22d05 28 69 2e 65 2e 20 74 68 65 20 63 6f 6e 74 65 6e  (i.e. the conten
22d06 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 63 68  ts.** of the sch
22d07 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73 29  ema hash tables)
22d08 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 63 68 65  ..**.** The Sche
22d09 6d 61 2e 63 61 63 68 65 5f 73 69 7a 65 20 76 61  ma.cache_size va
22d0a 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 6c  riable is not cl
22d0b 65 61 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eared..*/.SQLITE
22d0c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22d0d 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28  lite3SchemaFree(
22d0e 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48 61 73 68  void *p){.  Hash
22d0f 20 74 65 6d 70 31 3b 0a 20 20 48 61 73 68 20 74   temp1;.  Hash t
22d10 65 6d 70 32 3b 0a 20 20 48 61 73 68 45 6c 65 6d  emp2;.  HashElem
22d11 20 2a 70 45 6c 65 6d 3b 0a 20 20 53 63 68 65 6d   *pElem;.  Schem
22d12 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 63  a *pSchema = (Sc
22d13 68 65 6d 61 20 2a 29 70 3b 0a 0a 20 20 74 65 6d  hema *)p;..  tem
22d14 70 31 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74 62  p1 = pSchema->tb
22d15 6c 48 61 73 68 3b 0a 20 20 74 65 6d 70 32 20 3d  lHash;.  temp2 =
22d16 20 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61   pSchema->trigHa
22d17 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73  sh;.  sqlite3Has
22d18 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61 2d 3e  hInit(&pSchema->
22d19 74 72 69 67 48 61 73 68 2c 20 30 29 3b 0a 20 20  trigHash, 0);.  
22d1a 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
22d1b 28 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79  (&pSchema->aFKey
22d1c 29 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68  );.  sqlite3Hash
22d1d 43 6c 65 61 72 28 26 70 53 63 68 65 6d 61 2d 3e  Clear(&pSchema->
22d1e 69 64 78 48 61 73 68 29 3b 0a 20 20 66 6f 72 28  idxHash);.  for(
22d1f 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68  pElem=sqliteHash
22d20 46 69 72 73 74 28 26 74 65 6d 70 32 29 3b 20 70  First(&temp2); p
22d21 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69  Elem; pElem=sqli
22d22 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d  teHashNext(pElem
22d23 29 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  )){.    sqlite3D
22d24 65 6c 65 74 65 54 72 69 67 67 65 72 28 30 2c 20  eleteTrigger(0, 
22d25 28 54 72 69 67 67 65 72 2a 29 73 71 6c 69 74 65  (Trigger*)sqlite
22d26 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 29  HashData(pElem))
22d27 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48  ;.  }.  sqlite3H
22d28 61 73 68 43 6c 65 61 72 28 26 74 65 6d 70 32 29  ashClear(&temp2)
22d29 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 49  ;.  sqlite3HashI
22d2a 6e 69 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62  nit(&pSchema->tb
22d2b 6c 48 61 73 68 2c 20 30 29 3b 0a 20 20 66 6f 72  lHash, 0);.  for
22d2c 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73  (pElem=sqliteHas
22d2d 68 46 69 72 73 74 28 26 74 65 6d 70 31 29 3b 20  hFirst(&temp1); 
22d2e 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c  pElem; pElem=sql
22d2f 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65  iteHashNext(pEle
22d30 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  m)){.    Table *
22d31 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73  pTab = sqliteHas
22d32 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20  hData(pElem);.  
22d33 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
22d34 64 62 4d 65 6d 3d 3d 30 20 29 3b 0a 20 20 20 20  dbMem==0 );.    
22d35 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
22d36 6c 65 28 70 54 61 62 29 3b 0a 20 20 7d 0a 20 20  le(pTab);.  }.  
22d37 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
22d38 28 26 74 65 6d 70 31 29 3b 0a 20 20 70 53 63 68  (&temp1);.  pSch
22d39 65 6d 61 2d 3e 70 53 65 71 54 61 62 20 3d 20 30  ema->pSeqTab = 0
22d3a 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e 66 6c 61  ;.  pSchema->fla
22d3b 67 73 20 26 3d 20 7e 44 42 5f 53 63 68 65 6d 61  gs &= ~DB_Schema
22d3c 4c 6f 61 64 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Loaded;.}../*.**
22d3d 20 46 69 6e 64 20 61 6e 64 20 72 65 74 75 72 6e   Find and return
22d3e 20 74 68 65 20 73 63 68 65 6d 61 20 61 73 73 6f   the schema asso
22d3f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 42 54  ciated with a BT
22d40 72 65 65 2e 20 20 43 72 65 61 74 65 0a 2a 2a 20  ree.  Create.** 
22d41 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63  a new one if nec
22d42 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  essary..*/.SQLIT
22d43 45 5f 50 52 49 56 41 54 45 20 53 63 68 65 6d 61  E_PRIVATE Schema
22d44 20 2a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47   *sqlite3SchemaG
22d45 65 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  et(sqlite3 *db, 
22d46 42 74 72 65 65 20 2a 70 42 74 29 7b 0a 20 20 53  Btree *pBt){.  S
22d47 63 68 65 6d 61 20 2a 20 70 3b 0a 20 20 69 66 28  chema * p;.  if(
22d48 20 70 42 74 20 29 7b 0a 20 20 20 20 70 20 3d 20   pBt ){.    p = 
22d49 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69 74 65  (Schema *)sqlite
22d4a 33 42 74 72 65 65 53 63 68 65 6d 61 28 70 42 74  3BtreeSchema(pBt
22d4b 2c 20 73 69 7a 65 6f 66 28 53 63 68 65 6d 61 29  , sizeof(Schema)
22d4c 2c 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46  , sqlite3SchemaF
22d4d 72 65 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ree);.  }else{. 
22d4e 20 20 20 70 20 3d 20 28 53 63 68 65 6d 61 20 2a     p = (Schema *
22d4f 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  )sqlite3MallocZe
22d50 72 6f 28 73 69 7a 65 6f 66 28 53 63 68 65 6d 61  ro(sizeof(Schema
22d51 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70  ));.  }.  if( !p
22d52 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
22d53 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
22d54 7d 65 6c 73 65 20 69 66 20 28 20 30 3d 3d 70 2d  }else if ( 0==p-
22d55 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 29 7b 0a  >file_format ){.
22d56 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
22d57 6e 69 74 28 26 70 2d 3e 74 62 6c 48 61 73 68 2c  nit(&p->tblHash,
22d58 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
22d59 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 69 64 78  HashInit(&p->idx
22d5a 48 61 73 68 2c 20 30 29 3b 0a 20 20 20 20 73 71  Hash, 0);.    sq
22d5b 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70  lite3HashInit(&p
22d5c 2d 3e 74 72 69 67 48 61 73 68 2c 20 30 29 3b 0a  ->trigHash, 0);.
22d5d 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
22d5e 6e 69 74 28 26 70 2d 3e 61 46 4b 65 79 2c 20 31  nit(&p->aFKey, 1
22d5f 29 3b 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d 20  );.    p->enc = 
22d60 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 7d  SQLITE_UTF8;.  }
22d61 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
22d62 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
22d63 45 6e 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e  End of callback.
22d64 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
22d65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
22d67 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
22d68 42 65 67 69 6e 20 66 69 6c 65 20 64 65 6c 65 74  Begin file delet
22d69 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
22d6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
22d6c 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
22d6d 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
22d6e 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
22d6f 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
22d70 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
22d71 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
22d72 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
22d73 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
22d74 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
22d75 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
22d76 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
22d77 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
22d78 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
22d79 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
22d7a 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
22d7b 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
22d7c 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
22d7d 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
22d7e 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
22d7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d83 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
22d84 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
22d85 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ode routines tha
22d86 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20  t are called by 
22d87 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 69 6e  the parser.** in
22d88 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61   order to genera
22d89 74 65 20 63 6f 64 65 20 66 6f 72 20 44 45 4c 45  te code for DELE
22d8a 54 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e  TE FROM statemen
22d8b 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64  ts..**.** $Id: d
22d8c 65 6c 65 74 65 2e 63 2c 76 20 31 2e 31 39 38 20  elete.c,v 1.198 
22d8d 32 30 30 39 2f 30 33 2f 30 35 20 30 33 3a 34 38  2009/03/05 03:48
22d8e 3a 30 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a  :07 shane Exp $.
22d8f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 75  */../*.** Look u
22d90 70 20 65 76 65 72 79 20 74 61 62 6c 65 20 74 68  p every table th
22d91 61 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 70  at is named in p
22d92 53 72 63 2e 20 20 49 66 20 61 6e 79 20 74 61 62  Src.  If any tab
22d93 6c 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c  le is not found,
22d94 0a 2a 2a 20 61 64 64 20 61 6e 20 65 72 72 6f 72  .** add an error
22d95 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 72   message to pPar
22d96 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20  se->zErrMsg and 
22d97 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 20 49 66  return NULL.  If
22d98 20 61 6c 6c 20 74 61 62 6c 65 73 0a 2a 2a 20 61   all tables.** a
22d99 72 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e  re found, return
22d9a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
22d9b 65 20 6c 61 73 74 20 74 61 62 6c 65 2e 0a 2a 2f  e last table..*/
22d9c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22d9d 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72  Table *sqlite3Sr
22d9e 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73  cListLookup(Pars
22d9f 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69  e *pParse, SrcLi
22da0 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 73 74 72  st *pSrc){.  str
22da1 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
22da2 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e   *pItem = pSrc->
22da3 61 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  a;.  Table *pTab
22da4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 74 65  ;.  assert( pIte
22da5 6d 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3d  m && pSrc->nSrc=
22da6 3d 31 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73  =1 );.  pTab = s
22da7 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
22da8 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74  e(pParse, 0, pIt
22da9 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d  em->zName, pItem
22daa 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
22dab 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
22dac 6c 65 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b  le(pItem->pTab);
22dad 0a 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 3d  .  pItem->pTab =
22dae 20 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61   pTab;.  if( pTa
22daf 62 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 6e  b ){.    pTab->n
22db0 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28  Ref++;.  }.  if(
22db1 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42   sqlite3IndexedB
22db2 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  yLookup(pParse, 
22db3 70 49 74 65 6d 29 20 29 7b 0a 20 20 20 20 70 54  pItem) ){.    pT
22db4 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ab = 0;.  }.  re
22db5 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a  turn pTab;.}../*
22db6 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b  .** Check to mak
22db7 65 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e  e sure the given
22db8 20 74 61 62 6c 65 20 69 73 20 77 72 69 74 61 62   table is writab
22db9 6c 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f  le.  If it is no
22dba 74 0a 2a 2a 20 77 72 69 74 61 62 6c 65 2c 20 67  t.** writable, g
22dbb 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72  enerate an error
22dbc 20 6d 65 73 73 61 67 65 20 61 6e 64 20 72 65 74   message and ret
22dbd 75 72 6e 20 31 2e 20 20 49 66 20 69 74 20 69 73  urn 1.  If it is
22dbe 0a 2a 2a 20 77 72 69 74 61 62 6c 65 20 72 65 74  .** writable ret
22dbf 75 72 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49 54 45  urn 0;.*/.SQLITE
22dc0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
22dc1 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50  ite3IsReadOnly(P
22dc2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
22dc3 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 76  ble *pTab, int v
22dc4 69 65 77 4f 6b 29 7b 0a 20 20 69 66 28 20 28 28  iewOk){.  if( ((
22dc5 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
22dc6 20 54 46 5f 52 65 61 64 6f 6e 6c 79 29 21 3d 30   TF_Readonly)!=0
22dc7 0a 20 20 20 20 20 20 20 20 26 26 20 28 70 50 61  .        && (pPa
22dc8 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26  rse->db->flags &
22dc9 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
22dca 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  ema)==0.        
22dcb 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  && pParse->neste
22dcc 64 3d 3d 30 29 20 0a 23 69 66 6e 64 65 66 20 53  d==0) .#ifndef S
22dcd 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
22dce 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 7c 7c  ALTABLE.      ||
22dcf 20 28 70 54 61 62 2d 3e 70 4d 6f 64 20 26 26 20   (pTab->pMod && 
22dd0 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64  pTab->pMod->pMod
22dd1 75 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 29  ule->xUpdate==0)
22dd2 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20  .#endif.  ){.   
22dd3 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22dd4 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
22dd5 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f  %s may not be mo
22dd6 64 69 66 69 65 64 22 2c 20 70 54 61 62 2d 3e 7a  dified", pTab->z
22dd7 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Name);.    retur
22dd8 6e 20 31 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  n 1;.  }.#ifndef
22dd9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
22dda 57 0a 20 20 69 66 28 20 21 76 69 65 77 4f 6b 20  W.  if( !viewOk 
22ddb 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  && pTab->pSelect
22ddc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
22ddd 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
22dde 63 61 6e 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73  cannot modify %s
22ddf 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 61   because it is a
22de0 20 76 69 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61   view",pTab->zNa
22de1 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
22de2 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  1;.  }.#endif.  
22de3 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
22de4 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
22de5 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20   that will open 
22de6 61 20 74 61 62 6c 65 20 66 6f 72 20 72 65 61 64  a table for read
22de7 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
22de8 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
22de9 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a 20 20  te3OpenTable(.  
22dea 50 61 72 73 65 20 2a 70 2c 20 20 20 20 20 20 20  Parse *p,       
22deb 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
22dec 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20   into this VDBE 
22ded 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20  */.  int iCur,  
22dee 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
22def 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  or number of the
22df0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
22df1 69 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  iDb,        /* T
22df2 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65  he database inde
22df3 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62  x in sqlite3.aDb
22df4 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  [] */.  Table *p
22df5 54 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 20 74  Tab,    /* The t
22df6 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65  able to be opene
22df7 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64  d */.  int opcod
22df8 65 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65  e      /* OP_Ope
22df9 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e  nRead or OP_Open
22dfa 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 56 64  Write */.){.  Vd
22dfb 62 65 20 2a 76 3b 0a 20 20 69 66 28 20 49 73 56  be *v;.  if( IsV
22dfc 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72  irtual(pTab) ) r
22dfd 65 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73 71 6c  eturn;.  v = sql
22dfe 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b 0a  ite3GetVdbe(p);.
22dff 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65    assert( opcode
22e00 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 7c  ==OP_OpenWrite |
22e01 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65  | opcode==OP_Ope
22e02 6e 52 65 61 64 20 29 3b 0a 20 20 73 71 6c 69 74  nRead );.  sqlit
22e03 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69  e3TableLock(p, i
22e04 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
22e05 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e  (opcode==OP_Open
22e06 57 72 69 74 65 29 3f 31 3a 30 2c 20 70 54 61 62  Write)?1:0, pTab
22e07 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  ->zName);.  sqli
22e08 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
22e09 20 6f 70 63 6f 64 65 2c 20 69 43 75 72 2c 20 70   opcode, iCur, p
22e0a 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b  Tab->tnum, iDb);
22e0b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
22e0c 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 53 51  angeP4(v, -1, SQ
22e0d 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
22e0e 70 54 61 62 2d 3e 6e 43 6f 6c 29 2c 20 50 34 5f  pTab->nCol), P4_
22e0f 49 4e 54 33 32 29 3b 0a 20 20 56 64 62 65 43 6f  INT32);.  VdbeCo
22e10 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20  mment((v, "%s", 
22e11 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 7d  pTab->zName));.}
22e12 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  ...#if !defined(
22e13 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
22e14 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
22e15 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
22e16 52 29 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74  R)./*.** Evaluat
22e17 65 20 61 20 76 69 65 77 20 61 6e 64 20 73 74 6f  e a view and sto
22e18 72 65 20 69 74 73 20 72 65 73 75 6c 74 20 69 6e  re its result in
22e19 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61   an ephemeral ta
22e1a 62 6c 65 2e 20 20 54 68 65 0a 2a 2a 20 70 57 68  ble.  The.** pWh
22e1b 65 72 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ere argument is 
22e1c 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 57 48 45 52  an optional WHER
22e1d 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65  E clause that re
22e1e 73 74 72 69 63 74 73 20 74 68 65 0a 2a 2a 20 73  stricts the.** s
22e1f 65 74 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  et of rows in th
22e20 65 20 76 69 65 77 20 74 68 61 74 20 61 72 65 20  e view that are 
22e21 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74  to be added to t
22e22 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
22e23 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
22e24 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
22e25 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65  e3MaterializeVie
22e26 77 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  w(.  Parse *pPar
22e27 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
22e28 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
22e29 20 20 54 61 62 6c 65 20 2a 70 56 69 65 77 2c 20    Table *pView, 
22e2a 20 20 20 20 20 20 20 2f 2a 20 56 69 65 77 20 64         /* View d
22e2b 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 45  efinition */.  E
22e2c 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
22e2d 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20      /* Optional 
22e2e 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20  WHERE clause to 
22e2f 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e  be added */.  in
22e30 74 20 69 43 75 72 20 20 20 20 20 20 20 20 20 20  t iCur          
22e31 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
22e32 62 65 72 20 66 6f 72 20 65 70 68 65 6d 65 72 69  ber for ephemeri
22e33 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  al table */.){. 
22e34 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
22e35 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 75 70  ;.  Select *pDup
22e36 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
22e37 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
22e38 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53   pDup = sqlite3S
22e39 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 56 69  electDup(db, pVi
22e3a 65 77 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b  ew->pSelect, 0);
22e3b 0a 20 20 69 66 28 20 70 57 68 65 72 65 20 29 7b  .  if( pWhere ){
22e3c 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 46  .    SrcList *pF
22e3d 72 6f 6d 3b 0a 20 20 20 20 54 6f 6b 65 6e 20 76  rom;.    Token v
22e3e 69 65 77 4e 61 6d 65 3b 0a 20 20 20 20 0a 20 20  iewName;.    .  
22e3f 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74    pWhere = sqlit
22e40 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 57  e3ExprDup(db, pW
22e41 68 65 72 65 2c 20 30 29 3b 0a 20 20 20 20 76 69  here, 0);.    vi
22e42 65 77 4e 61 6d 65 2e 7a 20 3d 20 28 75 38 2a 29  ewName.z = (u8*)
22e43 70 56 69 65 77 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pView->zName;.  
22e44 20 20 76 69 65 77 4e 61 6d 65 2e 6e 20 3d 20 28    viewName.n = (
22e45 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 73 71 6c  unsigned int)sql
22e46 69 74 65 33 53 74 72 6c 65 6e 33 30 28 28 63 6f  ite3Strlen30((co
22e47 6e 73 74 20 63 68 61 72 2a 29 76 69 65 77 4e 61  nst char*)viewNa
22e48 6d 65 2e 7a 29 3b 0a 20 20 20 20 70 46 72 6f 6d  me.z);.    pFrom
22e49 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
22e4a 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28  tAppendFromTerm(
22e4b 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c  pParse, 0, 0, 0,
22e4c 20 26 76 69 65 77 4e 61 6d 65 2c 20 70 44 75 70   &viewName, pDup
22e4d 2c 20 30 2c 30 29 3b 0a 20 20 20 20 70 44 75 70  , 0,0);.    pDup
22e4e 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
22e4f 4e 65 77 28 70 50 61 72 73 65 2c 20 30 2c 20 70  New(pParse, 0, p
22e50 46 72 6f 6d 2c 20 70 57 68 65 72 65 2c 20 30 2c  From, pWhere, 0,
22e51 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b   0, 0, 0, 0, 0);
22e52 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 65  .  }.  sqlite3Se
22e53 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
22e54 73 74 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62  st, SRT_EphemTab
22e55 2c 20 69 43 75 72 29 3b 0a 20 20 73 71 6c 69 74  , iCur);.  sqlit
22e56 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
22e57 20 70 44 75 70 2c 20 26 64 65 73 74 29 3b 0a 20   pDup, &dest);. 
22e58 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
22e59 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a  lete(db, pDup);.
22e5a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
22e5b 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
22e5c 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e  _VIEW) && !defin
22e5d 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
22e5e 52 49 47 47 45 52 29 20 2a 2f 0a 0a 23 69 66 20  RIGGER) */..#if 
22e5f 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
22e60 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c  NABLE_UPDATE_DEL
22e61 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64  ETE_LIMIT) && !d
22e62 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
22e63 49 54 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a  IT_SUBQUERY)./*.
22e64 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65  ** Generate an e
22e65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74  xpression tree t
22e66 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
22e67 57 48 45 52 45 2c 20 4f 52 44 45 52 20 42 59 2c  WHERE, ORDER BY,
22e68 0a 2a 2a 20 61 6e 64 20 4c 49 4d 49 54 2f 4f 46  .** and LIMIT/OF
22e69 46 53 45 54 20 70 6f 72 74 69 6f 6e 20 6f 66 20  FSET portion of 
22e6a 44 45 4c 45 54 45 20 61 6e 64 20 55 50 44 41 54  DELETE and UPDAT
22e6b 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a  E statements..**
22e6c 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46  .**     DELETE F
22e6d 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57  ROM table_wxyz W
22e6e 48 45 52 45 20 61 3c 35 20 4f 52 44 45 52 20 42  HERE a<5 ORDER B
22e6f 59 20 61 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a 20  Y a LIMIT 1;.** 
22e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22e71 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f             \____
22e72 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
22e73 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20  ______/.**      
22e74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22e75 20 20 20 20 20 20 20 20 20 70 4c 69 6d 69 74 57           pLimitW
22e76 68 65 72 65 20 28 70 49 6e 43 6c 61 75 73 65 29  here (pInClause)
22e77 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22e78 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
22e79 4c 69 6d 69 74 57 68 65 72 65 28 0a 20 20 50 61  LimitWhere(.  Pa
22e7a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
22e7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
22e7c 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
22e7d 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
22e7e 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
22e7f 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63     /* the FROM c
22e80 6c 61 75 73 65 20 2d 2d 20 77 68 69 63 68 20 74  lause -- which t
22e81 61 62 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f  ables to scan */
22e82 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c  .  Expr *pWhere,
22e83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22e84 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
22e85 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c  use.  May be nul
22e86 6c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  l */.  ExprList 
22e87 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20  *pOrderBy,      
22e88 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
22e89 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79   BY clause.  May
22e8a 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78   be null */.  Ex
22e8b 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20  pr *pLimit,     
22e8c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
22e8d 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20  e LIMIT clause. 
22e8e 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a   May be null */.
22e8f 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 2c    Expr *pOffset,
22e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22e91 2a 20 54 68 65 20 4f 46 46 53 45 54 20 63 6c 61  * The OFFSET cla
22e92 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c  use.  May be nul
22e93 6c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 74  l */.  char *zSt
22e94 6d 74 54 79 70 65 20 20 20 20 20 20 20 20 20 20  mtType          
22e95 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 44 45      /* Either DE
22e96 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 2e 20  LETE or UPDATE. 
22e97 20 46 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   For error messa
22e98 67 65 73 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  ges. */.){.  Exp
22e99 72 20 2a 70 57 68 65 72 65 52 6f 77 69 64 20 3d  r *pWhereRowid =
22e9a 20 4e 55 4c 4c 3b 20 20 20 20 2f 2a 20 57 48 45   NULL;    /* WHE
22e9b 52 45 20 72 6f 77 69 64 20 2e 2e 20 2a 2f 0a 20  RE rowid .. */. 
22e9c 20 45 78 70 72 20 2a 70 49 6e 43 6c 61 75 73 65   Expr *pInClause
22e9d 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a   = NULL;      /*
22e9e 20 57 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20   WHERE rowid IN 
22e9f 28 20 73 65 6c 65 63 74 20 29 20 2a 2f 0a 20 20  ( select ) */.  
22ea0 45 78 70 72 20 2a 70 53 65 6c 65 63 74 52 6f 77  Expr *pSelectRow
22ea1 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a 20  id = NULL;   /* 
22ea2 53 45 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e 2e  SELECT rowid ...
22ea3 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
22ea4 70 45 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20 20  pEList = NULL;  
22ea5 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
22ea6 20 6c 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67 20   list contaning 
22ea7 6f 6e 6c 79 20 70 53 65 6c 65 63 74 52 6f 77 69  only pSelectRowi
22ea8 64 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  d */.  SrcList *
22ea9 70 53 65 6c 65 63 74 53 72 63 20 3d 20 4e 55 4c  pSelectSrc = NUL
22eaa 4c 3b 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f  L;  /* SELECT ro
22eab 77 69 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20 28  wid FROM x ... (
22eac 64 75 70 20 6f 66 20 70 53 72 63 29 20 2a 2f 0a  dup of pSrc) */.
22ead 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
22eae 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f  t = NULL;      /
22eaf 2a 20 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45 43  * Complete SELEC
22eb0 54 20 74 72 65 65 20 2a 2f 0a 0a 20 20 2f 2a 20  T tree */..  /* 
22eb1 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 65  Check that there
22eb2 20 69 73 6e 27 74 20 61 6e 20 4f 52 44 45 52 20   isn't an ORDER 
22eb3 42 59 20 77 69 74 68 6f 75 74 20 61 20 4c 49 4d  BY without a LIM
22eb4 49 54 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a  IT clause..  */.
22eb5 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 26    if( pOrderBy &
22eb6 26 20 28 70 4c 69 6d 69 74 20 3d 3d 20 30 29 20  & (pLimit == 0) 
22eb7 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ) {.    sqlite3E
22eb8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
22eb9 22 4f 52 44 45 52 20 42 59 20 77 69 74 68 6f 75  "ORDER BY withou
22eba 74 20 4c 49 4d 49 54 20 6f 6e 20 25 73 22 2c 20  t LIMIT on %s", 
22ebb 7a 53 74 6d 74 54 79 70 65 29 3b 0a 20 20 20 20  zStmtType);.    
22ebc 70 50 61 72 73 65 2d 3e 70 61 72 73 65 45 72 72  pParse->parseErr
22ebd 6f 72 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f  or = 1;.    goto
22ebe 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65   limit_where_cle
22ebf 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f  anup_2;.  }..  /
22ec0 2a 20 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74  * We only need t
22ec1 6f 20 67 65 6e 65 72 61 74 65 20 61 20 73 65 6c  o generate a sel
22ec2 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ect expression i
22ec3 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20  f there.  ** is 
22ec4 61 20 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20 74  a limit/offset t
22ec5 65 72 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e 0a  erm to enforce..
22ec6 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d 69    */.  if( pLimi
22ec7 74 20 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 2f  t == 0 ) {.    /
22ec8 2a 20 69 66 20 70 4c 69 6d 69 74 20 69 73 20 6e  * if pLimit is n
22ec9 75 6c 6c 2c 20 70 4f 66 66 73 65 74 20 77 69 6c  ull, pOffset wil
22eca 6c 20 61 6c 77 61 79 73 20 62 65 20 6e 75 6c 6c  l always be null
22ecb 20 61 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20 20   as well. */.   
22ecc 20 61 73 73 65 72 74 28 20 70 4f 66 66 73 65 74   assert( pOffset
22ecd 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 72 65 74   == 0 );.    ret
22ece 75 72 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d 0a  urn pWhere;.  }.
22ecf 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
22ed0 20 73 65 6c 65 63 74 20 65 78 70 72 65 73 73 69   select expressi
22ed1 6f 6e 20 74 72 65 65 20 74 6f 20 65 6e 66 6f 72  on tree to enfor
22ed2 63 65 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66 66  ce the limit/off
22ed3 73 65 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20 66  set .  ** term f
22ed4 6f 72 20 74 68 65 20 44 45 4c 45 54 45 20 6f 72  or the DELETE or
22ed5 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
22ed6 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a  t.  For example:
22ed7 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54 45 20 46  .  **   DELETE F
22ed8 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52  ROM table_a WHER
22ed9 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42  E col1=1 ORDER B
22eda 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f  Y col2 LIMIT 1 O
22edb 46 46 53 45 54 20 31 0a 20 20 2a 2a 20 62 65 63  FFSET 1.  ** bec
22edc 6f 6d 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45 4c  omes:.  **   DEL
22edd 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61  ETE FROM table_a
22ede 20 57 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20   WHERE rowid IN 
22edf 28 20 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  ( .  **     SELE
22ee0 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 61  CT rowid FROM ta
22ee1 62 6c 65 5f 61 20 57 48 45 52 45 20 63 6f 6c 31  ble_a WHERE col1
22ee2 3d 31 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 32  =1 ORDER BY col2
22ee3 20 4c 49 4d 49 54 20 31 20 4f 46 46 53 45 54 20   LIMIT 1 OFFSET 
22ee4 31 0a 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a 2f  1.  **   );.  */
22ee5 0a 0a 20 20 70 53 65 6c 65 63 74 52 6f 77 69 64  ..  pSelectRowid
22ee6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70   = sqlite3Expr(p
22ee7 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52 4f  Parse->db, TK_RO
22ee8 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69  W, 0, 0, 0);.  i
22ee9 66 28 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20  f( pSelectRowid 
22eea 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69  == 0 ) goto limi
22eeb 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
22eec 32 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71  2;.  pEList = sq
22eed 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
22eee 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 70  end(pParse, 0, p
22eef 53 65 6c 65 63 74 52 6f 77 69 64 2c 20 30 29 3b  SelectRowid, 0);
22ef0 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 3d 3d  .  if( pEList ==
22ef1 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f   0 ) goto limit_
22ef2 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b  where_cleanup_2;
22ef3 0a 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61 74 65  ..  /* duplicate
22ef4 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
22ef5 20 61 73 20 69 74 20 69 73 20 6e 65 65 64 65 64   as it is needed
22ef6 20 62 79 20 62 6f 74 68 20 74 68 65 20 44 45 4c   by both the DEL
22ef7 45 54 45 2f 55 50 44 41 54 45 20 74 72 65 65 0a  ETE/UPDATE tree.
22ef8 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c    ** and the SEL
22ef9 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a 2f 0a  ECT subtree. */.
22efa 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d 20 73    pSelectSrc = s
22efb 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
22efc 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72  (pParse->db, pSr
22efd 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53 65  c, 0);.  if( pSe
22efe 6c 65 63 74 53 72 63 20 3d 3d 20 30 20 29 20 7b  lectSrc == 0 ) {
22eff 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
22f00 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
22f01 65 2d 3e 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a  e->db, pEList);.
22f02 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77      goto limit_w
22f03 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a  here_cleanup_2;.
22f04 20 20 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65 72 61    }..  /* genera
22f05 74 65 20 74 68 65 20 53 45 4c 45 43 54 20 65 78  te the SELECT ex
22f06 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 2a  pression tree. *
22f07 2f 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71  /.  pSelect = sq
22f08 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70  lite3SelectNew(p
22f09 50 61 72 73 65 2c 70 45 4c 69 73 74 2c 70 53 65  Parse,pEList,pSe
22f0a 6c 65 63 74 53 72 63 2c 70 57 68 65 72 65 2c 30  lectSrc,pWhere,0
22f0b 2c 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ,0,.            
22f0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f0d 20 70 4f 72 64 65 72 42 79 2c 30 2c 70 4c 69 6d   pOrderBy,0,pLim
22f0e 69 74 2c 70 4f 66 66 73 65 74 29 3b 0a 20 20 69  it,pOffset);.  i
22f0f 66 28 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 20  f( pSelect == 0 
22f10 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f  ) return 0;..  /
22f11 2a 20 6e 6f 77 20 67 65 6e 65 72 61 74 65 20 74  * now generate t
22f12 68 65 20 6e 65 77 20 57 48 45 52 45 20 72 6f 77  he new WHERE row
22f13 69 64 20 49 4e 20 63 6c 61 75 73 65 20 66 6f 72  id IN clause for
22f14 20 74 68 65 20 44 45 4c 45 54 45 2f 55 44 50 41   the DELETE/UDPA
22f15 54 45 20 2a 2f 0a 20 20 70 57 68 65 72 65 52 6f  TE */.  pWhereRo
22f16 77 69 64 20 3d 20 73 71 6c 69 74 65 33 45 78 70  wid = sqlite3Exp
22f17 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b  r(pParse->db, TK
22f18 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  _ROW, 0, 0, 0);.
22f19 20 20 69 66 28 20 70 57 68 65 72 65 52 6f 77 69    if( pWhereRowi
22f1a 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69  d == 0 ) goto li
22f1b 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75  mit_where_cleanu
22f1c 70 5f 31 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65  p_1;.  pInClause
22f1d 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
22f1e 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70  pParse, TK_IN, p
22f1f 57 68 65 72 65 52 6f 77 69 64 2c 20 30 2c 20 30  WhereRowid, 0, 0
22f20 29 3b 0a 20 20 69 66 28 20 70 49 6e 43 6c 61 75  );.  if( pInClau
22f21 73 65 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c  se == 0 ) goto l
22f22 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
22f23 75 70 5f 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75  up_1;..  pInClau
22f24 73 65 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20  se->x.pSelect = 
22f25 70 53 65 6c 65 63 74 3b 0a 20 20 70 49 6e 43 6c  pSelect;.  pInCl
22f26 61 75 73 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  ause->flags |= E
22f27 50 5f 78 49 73 53 65 6c 65 63 74 3b 0a 20 20 73  P_xIsSelect;.  s
22f28 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
22f29 67 68 74 28 70 50 61 72 73 65 2c 20 70 49 6e 43  ght(pParse, pInC
22f2a 6c 61 75 73 65 29 3b 0a 20 20 72 65 74 75 72 6e  lause);.  return
22f2b 20 70 49 6e 43 6c 61 75 73 65 3b 0a 0a 20 20 2f   pInClause;..  /
22f2c 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74  * something went
22f2d 20 77 72 6f 6e 67 2e 20 63 6c 65 61 6e 20 75 70   wrong. clean up
22f2e 20 61 6e 79 74 68 69 6e 67 20 61 6c 6c 6f 63 61   anything alloca
22f2f 74 65 64 2e 20 2a 2f 0a 6c 69 6d 69 74 5f 77 68  ted. */.limit_wh
22f30 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3a 0a 20  ere_cleanup_1:. 
22f31 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
22f32 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
22f33 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 72 65 74   pSelect);.  ret
22f34 75 72 6e 20 30 3b 0a 0a 6c 69 6d 69 74 5f 77 68  urn 0;..limit_wh
22f35 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3a 0a 20  ere_cleanup_2:. 
22f36 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
22f37 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
22f38 57 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65  Where);.  sqlite
22f39 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
22f3a 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64  pParse->db, pOrd
22f3b 65 72 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33  erBy);.  sqlite3
22f3c 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73  ExprDelete(pPars
22f3d 65 2d 3e 64 62 2c 20 70 4c 69 6d 69 74 29 3b 0a  e->db, pLimit);.
22f3e 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
22f3f 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
22f40 70 4f 66 66 73 65 74 29 3b 0a 20 20 72 65 74 75  pOffset);.  retu
22f41 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 0;.}.#endif /
22f42 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  * defined(SQLITE
22f43 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44  _ENABLE_UPDATE_D
22f44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20  ELETE_LIMIT) && 
22f45 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
22f46 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a  OMIT_SUBQUERY) *
22f47 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  /../*.** Generat
22f48 65 20 63 6f 64 65 20 66 6f 72 20 61 20 44 45 4c  e code for a DEL
22f49 45 54 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65  ETE FROM stateme
22f4a 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45  nt..**.**     DE
22f4b 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f  LETE FROM table_
22f4c 77 78 79 7a 20 57 48 45 52 45 20 61 3c 35 20 41  wxyz WHERE a<5 A
22f4d 4e 44 20 62 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a  ND b NOT NULL;.*
22f4e 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
22f4f 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20    \________/    
22f50 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f     \____________
22f51 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20  ____/.**        
22f52 20 20 20 20 20 20 20 20 20 20 70 54 61 62 4c 69            pTabLi
22f53 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  st              
22f54 70 57 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45  pWhere.*/.SQLITE
22f55 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22f56 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28  lite3DeleteFrom(
22f57 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
22f58 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
22f59 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
22f5a 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
22f5b 61 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54  abList,     /* T
22f5c 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68  he table from wh
22f5d 69 63 68 20 77 65 20 73 68 6f 75 6c 64 20 64 65  ich we should de
22f5e 6c 65 74 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20  lete things */. 
22f5f 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20   Expr *pWhere   
22f60 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
22f61 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61  HERE clause.  Ma
22f62 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a  y be null */.){.
22f63 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
22f64 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
22f65 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65  virtual database
22f66 20 65 6e 67 69 6e 65 20 2a 2f 0a 20 20 54 61 62   engine */.  Tab
22f67 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
22f68 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
22f69 20 66 72 6f 6d 20 77 68 69 63 68 20 72 65 63 6f   from which reco
22f6a 72 64 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65  rds will be dele
22f6b 74 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ted */.  const c
22f6c 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
22f6d 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
22f6e 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62  ase holding pTab
22f6f 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 2c 20 61   */.  int end, a
22f70 64 64 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ddr = 0;     /* 
22f71 41 20 63 6f 75 70 6c 65 20 61 64 64 72 65 73 73  A couple address
22f72 65 73 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20  es of generated 
22f73 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  code */.  int i;
22f74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f75 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
22f76 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20   */.  WhereInfo 
22f77 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20  *pWInfo;     /* 
22f78 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
22f79 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  t the WHERE clau
22f7a 73 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70  se */.  Index *p
22f7b 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Idx;           /
22f7c 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
22f7d 65 72 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68  er indices of th
22f7e 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
22f7f 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 20 20   iCur;          
22f80 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73      /* VDBE Curs
22f81 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54  or number for pT
22f82 61 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ab */.  sqlite3 
22f83 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  *db;           /
22f84 2a 20 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20  * Main database 
22f85 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 41  structure */.  A
22f86 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74  uthContext sCont
22f87 65 78 74 3b 20 20 2f 2a 20 41 75 74 68 6f 72 69  ext;  /* Authori
22f88 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a  zation context *
22f89 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78 20 3d  /.  int oldIdx =
22f8a 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 43 75   -1;       /* Cu
22f8b 72 73 6f 72 20 66 6f 72 20 74 68 65 20 4f 4c 44  rsor for the OLD
22f8c 20 74 61 62 6c 65 20 6f 66 20 41 46 54 45 52 20   table of AFTER 
22f8d 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 4e 61  triggers */.  Na
22f8e 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
22f8f 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e       /* Name con
22f90 74 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20  text to resolve 
22f91 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a  expressions in *
22f92 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
22f93 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
22f94 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f  tabase number */
22f95 0a 20 20 69 6e 74 20 6d 65 6d 43 6e 74 20 3d 20  .  int memCnt = 
22f96 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d  -1;       /* Mem
22f97 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f  ory cell used fo
22f98 72 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 69 6e  r change countin
22f99 67 20 2a 2f 0a 20 20 69 6e 74 20 72 63 61 75 74  g */.  int rcaut
22f9a 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  h;            /*
22f9b 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   Value returned 
22f9c 62 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  by authorization
22f9d 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 23 69   callback */..#i
22f9e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
22f9f 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20  T_TRIGGER.  int 
22fa0 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20  isView;         
22fa1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
22fa2 20 69 66 20 61 74 74 65 6d 70 74 69 6e 67 20 74   if attempting t
22fa3 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 61 20  o delete from a 
22fa4 76 69 65 77 20 2a 2f 0a 20 20 54 72 69 67 67 65  view */.  Trigge
22fa5 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20  r *pTrigger;    
22fa6 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
22fa7 66 20 74 61 62 6c 65 20 74 72 69 67 67 65 72 73  f table triggers
22fa8 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f  , if required */
22fa9 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 42  .#endif.  int iB
22faa 65 67 69 6e 41 66 74 65 72 54 72 69 67 67 65 72  eginAfterTrigger
22fab 20 3d 20 30 3b 20 20 2f 2a 20 41 64 64 72 65 73   = 0;  /* Addres
22fac 73 20 6f 66 20 61 66 74 65 72 20 74 72 69 67 67  s of after trigg
22fad 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  er program */.  
22fae 69 6e 74 20 69 45 6e 64 41 66 74 65 72 54 72 69  int iEndAfterTri
22faf 67 67 65 72 20 3d 20 30 3b 20 20 20 20 2f 2a 20  gger = 0;    /* 
22fb0 45 78 69 74 20 6f 66 20 61 66 74 65 72 20 74 72  Exit of after tr
22fb1 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f  igger program */
22fb2 0a 20 20 69 6e 74 20 69 42 65 67 69 6e 42 65 66  .  int iBeginBef
22fb3 6f 72 65 54 72 69 67 67 65 72 20 3d 20 30 3b 20  oreTrigger = 0; 
22fb4 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 62 65  /* Address of be
22fb5 66 6f 72 65 20 74 72 69 67 67 65 72 20 70 72 6f  fore trigger pro
22fb6 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45  gram */.  int iE
22fb7 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 20  ndBeforeTrigger 
22fb8 3d 20 30 3b 20 20 20 2f 2a 20 45 78 69 74 20 6f  = 0;   /* Exit o
22fb9 66 20 62 65 66 6f 72 65 20 74 72 69 67 67 65 72  f before trigger
22fba 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75 33   program */.  u3
22fbb 32 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 3d  2 old_col_mask =
22fbc 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61   0;        /* Ma
22fbd 73 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75  sk of OLD.* colu
22fbe 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a 20  mns in use */.. 
22fbf 20 73 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 65   sContext.pParse
22fc0 20 3d 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61   = 0;.  db = pPa
22fc1 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
22fc2 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
22fc3 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22fc4 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65  ){.    goto dele
22fc5 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b  te_from_cleanup;
22fc6 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
22fc7 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
22fc8 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65   );..  /* Locate
22fc9 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63 68   the table which
22fca 20 77 65 20 77 61 6e 74 20 74 6f 20 64 65 6c 65   we want to dele
22fcb 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c 65 20  te.  This table 
22fcc 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70  has to be.  ** p
22fcd 75 74 20 69 6e 20 61 6e 20 53 72 63 4c 69 73 74  ut in an SrcList
22fce 20 73 74 72 75 63 74 75 72 65 20 62 65 63 61 75   structure becau
22fcf 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 73  se some of the s
22fd0 75 62 72 6f 75 74 69 6e 65 73 20 77 65 0a 20 20  ubroutines we.  
22fd1 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 69  ** will be calli
22fd2 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65 64 20  ng are designed 
22fd3 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d 75 6c  to work with mul
22fd4 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 6e 64  tiple tables and
22fd5 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e 20   expect.  ** an 
22fd6 53 72 63 4c 69 73 74 2a 20 70 61 72 61 6d 65 74  SrcList* paramet
22fd7 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75  er instead of ju
22fd8 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72 61  st a Table* para
22fd9 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 54  meter..  */.  pT
22fda 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  ab = sqlite3SrcL
22fdb 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  istLookup(pParse
22fdc 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69  , pTabList);.  i
22fdd 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20 67 6f  f( pTab==0 )  go
22fde 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  to delete_from_c
22fdf 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46 69  leanup;..  /* Fi
22fe0 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 68  gure out if we h
22fe1 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 73  ave any triggers
22fe2 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c   and if the tabl
22fe3 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65 6c  e being.  ** del
22fe4 65 74 65 64 20 66 72 6f 6d 20 69 73 20 61 20 76  eted from is a v
22fe5 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  iew.  */.#ifndef
22fe6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
22fe7 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72 20  GGER.  pTrigger 
22fe8 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
22fe9 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70  sExist(pParse, p
22fea 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20  Tab, TK_DELETE, 
22feb 30 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77 20  0, 0);.  isView 
22fec 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21  = pTab->pSelect!
22fed 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  =0;.#else.# defi
22fee 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23 20  ne pTrigger 0.# 
22fef 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a  define isView 0.
22ff0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
22ff1 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23  LITE_OMIT_VIEW.#
22ff2 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20   undef isView.# 
22ff3 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a  define isView 0.
22ff4 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71  #endif..  if( sq
22ff5 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28  lite3IsReadOnly(
22ff6 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 28 70  pParse, pTab, (p
22ff7 54 72 69 67 67 65 72 3f 31 3a 30 29 29 20 29 7b  Trigger?1:0)) ){
22ff8 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
22ff9 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
22ffa 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74   }.  iDb = sqlit
22ffb 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
22ffc 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
22ffd 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  a);.  assert( iD
22ffe 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a  b<db->nDb );.  z
22fff 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
23000 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 61 75 74  ].zName;.  rcaut
23001 68 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43  h = sqlite3AuthC
23002 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
23003 49 54 45 5f 44 45 4c 45 54 45 2c 20 70 54 61 62  ITE_DELETE, pTab
23004 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29  ->zName, 0, zDb)
23005 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 61 75  ;.  assert( rcau
23006 74 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  th==SQLITE_OK ||
23007 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f   rcauth==SQLITE_
23008 44 45 4e 59 20 7c 7c 20 72 63 61 75 74 68 3d 3d  DENY || rcauth==
23009 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 3b  SQLITE_IGNORE );
2300a 0a 20 20 69 66 28 20 72 63 61 75 74 68 3d 3d 53  .  if( rcauth==S
2300b 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20  QLITE_DENY ){.  
2300c 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72    goto delete_fr
2300d 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  om_cleanup;.  }.
2300e 20 20 61 73 73 65 72 74 28 21 69 73 56 69 65 77    assert(!isView
2300f 20 7c 7c 20 70 54 72 69 67 67 65 72 29 3b 0a 0a   || pTrigger);..
23010 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20    /* If pTab is 
23011 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d  really a view, m
23012 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20  ake sure it has 
23013 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
23014 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
23015 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
23016 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
23017 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
23018 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
23019 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
2301a 41 6c 6c 6f 63 61 74 65 20 61 20 63 75 72 73 6f  Allocate a curso
2301b 72 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  r used to store 
2301c 74 68 65 20 6f 6c 64 2e 2a 20 64 61 74 61 20 66  the old.* data f
2301d 6f 72 20 61 20 74 72 69 67 67 65 72 2e 0a 20 20  or a trigger..  
2301e 2a 2f 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  */.  if( pTrigge
2301f 72 20 29 7b 20 0a 20 20 20 20 6f 6c 64 49 64 78  r ){ .    oldIdx
23020 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
23021 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73  +;.  }..  /* Ass
23022 69 67 6e 20 20 63 75 72 73 6f 72 20 6e 75 6d 62  ign  cursor numb
23023 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  er to the table 
23024 61 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e 64 69  and all its indi
23025 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ces..  */.  asse
23026 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  rt( pTabList->nS
23027 72 63 3d 3d 31 20 29 3b 0a 20 20 69 43 75 72 20  rc==1 );.  iCur 
23028 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d  = pTabList->a[0]
23029 2e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73  .iCursor = pPars
2302a 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72  e->nTab++;.  for
2302b 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
2302c 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
2302d 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
2302e 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
2302f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 61 72 74  .  }..  /* Start
23030 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74 65 78   the view contex
23031 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56  t.  */.  if( isV
23032 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  iew ){.    sqlit
23033 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73  e3AuthContextPus
23034 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f 6e 74  h(pParse, &sCont
23035 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ext, pTab->zName
23036 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67  );.  }..  /* Beg
23037 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  in generating co
23038 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73  de..  */.  v = s
23039 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2303a 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
2303b 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65  0 ){.    goto de
2303c 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75  lete_from_cleanu
2303d 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  p;.  }.  if( pPa
2303e 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
2303f 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e   sqlite3VdbeCoun
23040 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73  tChanges(v);.  s
23041 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
23042 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
23043 2c 20 28 70 54 72 69 67 67 65 72 3f 31 3a 30 29  , (pTrigger?1:0)
23044 2c 20 69 44 62 29 3b 0a 0a 20 20 69 66 28 20 70  , iDb);..  if( p
23045 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 69  Trigger ){.    i
23046 6e 74 20 6f 72 63 6f 6e 66 20 3d 20 28 28 70 50  nt orconf = ((pP
23047 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 29  arse->trigStack)
23048 3f 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  ?pParse->trigSta
23049 63 6b 2d 3e 6f 72 63 6f 6e 66 3a 4f 45 5f 44 65  ck->orconf:OE_De
2304a 66 61 75 6c 74 29 3b 0a 20 20 20 20 69 6e 74 20  fault);.    int 
2304b 69 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56  iGoto = sqlite3V
2304c 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
2304d 47 6f 74 6f 29 3b 0a 20 20 20 20 61 64 64 72 20  Goto);.    addr 
2304e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2304f 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20  eLabel(v);..    
23050 69 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69 67  iBeginBeforeTrig
23051 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ger = sqlite3Vdb
23052 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
23053 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
23054 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
23055 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
23056 72 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c  r, TK_DELETE, 0,
23057 20 0a 20 20 20 20 20 20 20 20 54 52 49 47 47 45   .        TRIGGE
23058 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 20  R_BEFORE, pTab, 
23059 2d 31 2c 20 6f 6c 64 49 64 78 2c 20 6f 72 63 6f  -1, oldIdx, orco
2305a 6e 66 2c 20 61 64 64 72 2c 20 26 6f 6c 64 5f 63  nf, addr, &old_c
2305b 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b 0a 20 20 20  ol_mask, 0);.   
2305c 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69 67 67   iEndBeforeTrigg
2305d 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  er = sqlite3Vdbe
2305e 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74  AddOp0(v, OP_Got
2305f 6f 29 3b 0a 0a 20 20 20 20 69 42 65 67 69 6e 41  o);..    iBeginA
23060 66 74 65 72 54 72 69 67 67 65 72 20 3d 20 73 71  fterTrigger = sq
23061 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
23062 41 64 64 72 28 76 29 3b 0a 20 20 20 20 28 76 6f  Addr(v);.    (vo
23063 69 64 29 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  id)sqlite3CodeRo
23064 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  wTrigger(pParse,
23065 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 44 45   pTrigger, TK_DE
23066 4c 45 54 45 2c 20 30 2c 20 0a 20 20 20 20 20 20  LETE, 0, .      
23067 20 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c    TRIGGER_AFTER,
23068 20 70 54 61 62 2c 20 2d 31 2c 20 6f 6c 64 49 64   pTab, -1, oldId
23069 78 2c 20 6f 72 63 6f 6e 66 2c 20 61 64 64 72 2c  x, orconf, addr,
2306a 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c 20   &old_col_mask, 
2306b 30 29 3b 0a 20 20 20 20 69 45 6e 64 41 66 74 65  0);.    iEndAfte
2306c 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  rTrigger = sqlit
2306d 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
2306e 4f 50 5f 47 6f 74 6f 29 3b 0a 0a 20 20 20 20 73  OP_Goto);..    s
2306f 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
23070 72 65 28 76 2c 20 69 47 6f 74 6f 29 3b 0a 20 20  re(v, iGoto);.  
23071 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72  }..  /* If we ar
23072 65 20 74 72 79 69 6e 67 20 74 6f 20 64 65 6c 65  e trying to dele
23073 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77 2c 20  te from a view, 
23074 72 65 61 6c 69 7a 65 20 74 68 61 74 20 76 69 65  realize that vie
23075 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70  w into.  ** a ep
23076 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20  hemeral table.. 
23077 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64   */.#if !defined
23078 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
23079 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  W) && !defined(S
2307a 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2307b 45 52 29 0a 20 20 69 66 28 20 69 73 56 69 65 77  ER).  if( isView
2307c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d   ){.    sqlite3M
2307d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 70  aterializeView(p
2307e 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 57 68  Parse, pTab, pWh
2307f 65 72 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a  ere, iCur);.  }.
23080 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 73  #endif..  /* Res
23081 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  olve the column 
23082 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57 48 45  names in the WHE
23083 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a  RE clause..  */.
23084 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
23085 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
23086 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
23087 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72  Parse;.  sNC.pSr
23088 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74  cList = pTabList
23089 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52  ;.  if( sqlite3R
2308a 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
2308b 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20 29 7b  &sNC, pWhere) ){
2308c 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
2308d 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
2308e 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c   }..  /* Initial
2308f 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 65 72 20  ize the counter 
23090 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
23091 20 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 69   rows deleted, i
23092 66 0a 20 20 2a 2a 20 77 65 20 61 72 65 20 63 6f  f.  ** we are co
23093 75 6e 74 69 6e 67 20 72 6f 77 73 2e 0a 20 20 2a  unting rows..  *
23094 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67  /.  if( db->flag
23095 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74  s & SQLITE_Count
23096 52 6f 77 73 20 29 7b 0a 20 20 20 20 6d 65 6d 43  Rows ){.    memC
23097 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  nt = ++pParse->n
23098 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
23099 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2309a 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65 6d  _Integer, 0, mem
2309b 43 6e 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  Cnt);.  }..#ifnd
2309c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2309d 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41  RUNCATE_OPTIMIZA
2309e 54 49 4f 4e 0a 20 20 2f 2a 20 53 70 65 63 69 61  TION.  /* Specia
2309f 6c 20 63 61 73 65 3a 20 41 20 44 45 4c 45 54 45  l case: A DELETE
230a0 20 77 69 74 68 6f 75 74 20 61 20 57 48 45 52 45   without a WHERE
230a1 20 63 6c 61 75 73 65 20 64 65 6c 65 74 65 73 20   clause deletes 
230a2 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a 2a  everything..  **
230a3 20 49 74 20 69 73 20 65 61 73 69 65 72 20 6a 75   It is easier ju
230a4 73 74 20 74 6f 20 65 72 61 73 65 20 74 68 65 20  st to erase the 
230a5 77 68 6f 6c 65 20 74 61 62 6c 65 2e 20 20 4e 6f  whole table.  No
230a6 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61  te, however, tha
230a7 74 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65 61 6e  t.  ** this mean
230a8 73 20 74 68 61 74 20 74 68 65 20 72 6f 77 20 63  s that the row c
230a9 68 61 6e 67 65 20 63 6f 75 6e 74 20 77 69 6c 6c  hange count will
230aa 20 62 65 20 69 6e 63 6f 72 72 65 63 74 2e 0a 20   be incorrect.. 
230ab 20 2a 2f 0a 20 20 69 66 28 20 72 63 61 75 74 68   */.  if( rcauth
230ac 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
230ad 57 68 65 72 65 3d 3d 30 20 26 26 20 21 70 54 72  Where==0 && !pTr
230ae 69 67 67 65 72 20 26 26 20 21 49 73 56 69 72 74  igger && !IsVirt
230af 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
230b0 20 61 73 73 65 72 74 28 20 21 69 73 56 69 65 77   assert( !isView
230b1 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
230b2 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
230b3 43 6c 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e 75  Clear, pTab->tnu
230b4 6d 2c 20 69 44 62 2c 20 6d 65 6d 43 6e 74 29 3b  m, iDb, memCnt);
230b5 0a 20 20 20 20 69 66 28 20 21 70 50 61 72 73 65  .    if( !pParse
230b6 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20  ->nested ){.    
230b7 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
230b8 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61  ngeP4(v, -1, pTa
230b9 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41  b->zName, P4_STA
230ba 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  TIC);.    }.    
230bb 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
230bc 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
230bd 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
230be 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
230bf 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  dx->pSchema==pTa
230c0 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  b->pSchema );.  
230c1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
230c2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61  ddOp2(v, OP_Clea
230c3 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69  r, pIdx->tnum, i
230c4 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  Db);.    }.  }el
230c5 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
230c6 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54  ITE_OMIT_TRUNCAT
230c7 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a  E_OPTIMIZATION *
230c8 2f 0a 20 20 2f 2a 20 54 68 65 20 75 73 75 61 6c  /.  /* The usual
230c9 20 63 61 73 65 3a 20 54 68 65 72 65 20 69 73 20   case: There is 
230ca 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 73  a WHERE clause s
230cb 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73 63 61  o we have to sca
230cc 6e 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 74  n through.  ** t
230cd 68 65 20 74 61 62 6c 65 20 61 6e 64 20 70 69 63  he table and pic
230ce 6b 20 77 68 69 63 68 20 72 65 63 6f 72 64 73 20  k which records 
230cf 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 2a 2f 0a  to delete..  */.
230d0 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 52 6f 77    {.    int iRow
230d1 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  id = ++pParse->n
230d2 4d 65 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64 20  Mem;    /* Used 
230d3 66 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77 69  for storing rowi
230d4 64 20 76 61 6c 75 65 73 2e 20 2a 2f 0a 20 20 20  d values. */.   
230d5 20 69 6e 74 20 69 52 6f 77 53 65 74 20 3d 20 2b   int iRowSet = +
230d6 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20  +pParse->nMem;  
230d7 20 2f 2a 20 52 65 67 69 73 74 65 72 20 66 6f 72   /* Register for
230d8 20 72 6f 77 73 65 74 20 6f 66 20 72 6f 77 73 20   rowset of rows 
230d9 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 0a 20 20  to delete */..  
230da 20 20 2f 2a 20 43 6f 6c 6c 65 63 74 20 72 6f 77    /* Collect row
230db 69 64 73 20 6f 66 20 65 76 65 72 79 20 72 6f 77  ids of every row
230dc 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a   to be deleted..
230dd 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
230de 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
230df 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 6f 77  OP_Null, 0, iRow
230e0 53 65 74 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f  Set);.    pWInfo
230e1 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42   = sqlite3WhereB
230e2 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61  egin(pParse, pTa
230e3 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30  bList, pWhere, 0
230e4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
230e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
230e6 20 57 48 45 52 45 5f 46 49 4c 4c 5f 52 4f 57 53   WHERE_FILL_ROWS
230e7 45 54 2c 20 69 52 6f 77 53 65 74 29 3b 0a 20 20  ET, iRowSet);.  
230e8 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20    if( pWInfo==0 
230e9 29 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72  ) goto delete_fr
230ea 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  om_cleanup;.    
230eb 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
230ec 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
230ed 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
230ee 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
230ef 50 5f 41 64 64 49 6d 6d 2c 20 6d 65 6d 43 6e 74  P_AddImm, memCnt
230f0 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 1);.    }.    
230f1 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
230f2 70 57 49 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a  pWInfo);..    /*
230f3 20 4f 70 65 6e 20 74 68 65 20 70 73 65 75 64 6f   Open the pseudo
230f4 2d 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 73  -table used to s
230f5 74 6f 72 65 20 4f 4c 44 20 69 66 20 74 68 65 72  tore OLD if ther
230f6 65 20 61 72 65 20 74 72 69 67 67 65 72 73 2e 0a  e are triggers..
230f7 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
230f8 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20  Trigger ){.     
230f9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
230fa 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65  p3(v, OP_OpenPse
230fb 75 64 6f 2c 20 6f 6c 64 49 64 78 2c 20 30 2c 20  udo, oldIdx, 0, 
230fc 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20  pTab->nCol);.   
230fd 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74   }..    /* Delet
230fe 65 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f  e every item who
230ff 73 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74  se key was writt
23100 65 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64  en to the list d
23101 75 72 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a  uring the.    **
23102 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20   database scan. 
23103 20 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65   We have to dele
23104 74 65 20 69 74 65 6d 73 20 61 66 74 65 72 20 74  te items after t
23105 68 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c  he scan is compl
23106 65 74 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75  ete.    ** becau
23107 73 65 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69  se deleting an i
23108 74 65 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74  tem can change t
23109 68 65 20 73 63 61 6e 20 6f 72 64 65 72 2e 0a 20  he scan order.. 
2310a 20 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20     */.    end = 
2310b 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2310c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 69 66  abel(v);..    if
2310d 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20  ( !isView ){.   
2310e 20 20 20 2f 2a 20 4f 70 65 6e 20 63 75 72 73 6f     /* Open curso
2310f 72 73 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  rs for the table
23110 20 77 65 20 61 72 65 20 64 65 6c 65 74 69 6e 67   we are deleting
23111 20 66 72 6f 6d 20 61 6e 64 20 0a 20 20 20 20 20   from and .     
23112 20 2a 2a 20 61 6c 6c 20 69 74 73 20 69 6e 64 69   ** all its indi
23113 63 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ces..      */.  
23114 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
23115 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70  ableAndIndices(p
23116 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
23117 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29  r, OP_OpenWrite)
23118 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
23119 54 68 69 73 20 69 73 20 74 68 65 20 62 65 67 69  This is the begi
2311a 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 65 6c  nning of the del
2311b 65 74 65 20 6c 6f 6f 70 2e 20 49 66 20 61 20 74  ete loop. If a t
2311c 72 69 67 67 65 72 20 65 6e 63 6f 75 6e 74 65 72  rigger encounter
2311d 73 0a 20 20 20 20 2a 2a 20 61 6e 20 49 47 4e 4f  s.    ** an IGNO
2311e 52 45 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 69  RE constraint, i
2311f 74 20 6a 75 6d 70 73 20 62 61 63 6b 20 74 6f 20  t jumps back to 
23120 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
23121 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b   if( pTrigger ){
23122 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
23123 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
23124 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 7d 0a 20  , addr);.    }. 
23125 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
23126 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
23127 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 69 52  P_RowSetRead, iR
23128 6f 77 53 65 74 2c 20 65 6e 64 2c 20 69 52 6f 77  owSet, end, iRow
23129 69 64 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 54  id);..    if( pT
2312a 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20  rigger ){.      
2312b 69 6e 74 20 69 44 61 74 61 20 3d 20 2b 2b 70 50  int iData = ++pP
2312c 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 2f 2a  arse->nMem;   /*
2312d 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77   For storing row
2312e 20 64 61 74 61 20 6f 66 20 4f 4c 44 20 74 61 62   data of OLD tab
2312f 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20  le */..      /* 
23130 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73  If the record is
23131 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 65 73 65   no longer prese
23132 6e 74 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  nt in the table,
23133 20 6a 75 6d 70 20 74 6f 20 74 68 65 0a 20 20 20   jump to the.   
23134 20 20 20 2a 2a 20 6e 65 78 74 20 69 74 65 72 61     ** next itera
23135 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70  tion of the loop
23136 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6e   through the con
23137 74 65 6e 74 73 20 6f 66 20 74 68 65 20 66 69 66  tents of the fif
23138 6f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  o..      */.    
23139 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2313a 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
2313b 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c  sts, iCur, addr,
2313c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 20   iRowid);..     
2313d 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65   /* Populate the
2313e 20 4f 4c 44 2e 2a 20 70 73 65 75 64 6f 2d 74 61   OLD.* pseudo-ta
2313f 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ble */.      if(
23140 20 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 29 7b   old_col_mask ){
23141 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
23142 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23143 5f 52 6f 77 44 61 74 61 2c 20 69 43 75 72 2c 20  _RowData, iCur, 
23144 69 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 65  iData);.      }e
23145 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
23146 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
23147 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 44  , OP_Null, 0, iD
23148 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ata);.      }.  
23149 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2314a 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
2314b 72 74 2c 20 6f 6c 64 49 64 78 2c 20 69 44 61 74  rt, oldIdx, iDat
2314c 61 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20  a, iRowid);..   
2314d 20 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b 20     /* Jump back 
2314e 61 6e 64 20 72 75 6e 20 74 68 65 20 42 45 46 4f  and run the BEFO
2314f 52 45 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  RE triggers */. 
23150 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23151 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
23152 6f 2c 20 30 2c 20 69 42 65 67 69 6e 42 65 66 6f  o, 0, iBeginBefo
23153 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  reTrigger);.    
23154 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
23155 70 48 65 72 65 28 76 2c 20 69 45 6e 64 42 65 66  pHere(v, iEndBef
23156 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20 20  oreTrigger);.   
23157 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56   }..    if( !isV
23158 69 65 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  iew ){.      /* 
23159 44 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20 2a  Delete the row *
2315a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2315b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2315c 4c 45 0a 20 20 20 20 20 20 69 66 28 20 49 73 56  LE.      if( IsV
2315d 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2315e 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
2315f 61 72 20 2a 70 56 74 61 62 20 3d 20 28 63 6f 6e  ar *pVtab = (con
23160 73 74 20 63 68 61 72 20 2a 29 70 54 61 62 2d 3e  st char *)pTab->
23161 70 56 74 61 62 3b 0a 20 20 20 20 20 20 20 20 73  pVtab;.        s
23162 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72  qlite3VtabMakeWr
23163 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  itable(pParse, p
23164 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71  Tab);.        sq
23165 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
23166 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20 30  v, OP_VUpdate, 0
23167 2c 20 31 2c 20 69 52 6f 77 69 64 2c 20 70 56 74  , 1, iRowid, pVt
23168 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20  ab, P4_VTAB);.  
23169 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
2316a 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
2316b 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
2316c 52 6f 77 44 65 6c 65 74 65 28 70 50 61 72 73 65  RowDelete(pParse
2316d 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 69 52  , pTab, iCur, iR
2316e 6f 77 69 64 2c 20 70 50 61 72 73 65 2d 3e 6e 65  owid, pParse->ne
2316f 73 74 65 64 3d 3d 30 29 3b 0a 20 20 20 20 20 20  sted==0);.      
23170 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
23171 49 66 20 74 68 65 72 65 20 61 72 65 20 72 6f 77  If there are row
23172 20 74 72 69 67 67 65 72 73 2c 20 63 6c 6f 73 65   triggers, close
23173 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 74 68 65   all cursors the
23174 6e 20 69 6e 76 6f 6b 65 0a 20 20 20 20 2a 2a 20  n invoke.    ** 
23175 74 68 65 20 41 46 54 45 52 20 74 72 69 67 67 65  the AFTER trigge
23176 72 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  rs.    */.    if
23177 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  ( pTrigger ){.  
23178 20 20 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b      /* Jump back
23179 20 61 6e 64 20 72 75 6e 20 74 68 65 20 41 46 54   and run the AFT
2317a 45 52 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  ER triggers */. 
2317b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2317c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2317d 6f 2c 20 30 2c 20 69 42 65 67 69 6e 41 66 74 65  o, 0, iBeginAfte
2317e 72 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20  rTrigger);.     
2317f 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
23180 48 65 72 65 28 76 2c 20 69 45 6e 64 41 66 74 65  Here(v, iEndAfte
23181 72 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 7d  rTrigger);.    }
23182 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20  ..    /* End of 
23183 74 68 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70 20  the delete loop 
23184 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
23185 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
23186 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20  oto, 0, addr);. 
23187 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
23188 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e  solveLabel(v, en
23189 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73  d);..    /* Clos
2318a 65 20 74 68 65 20 63 75 72 73 6f 72 73 20 61 66  e the cursors af
2318b 74 65 72 20 74 68 65 20 6c 6f 6f 70 20 69 66 20  ter the loop if 
2318c 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 6f 77  there are no row
2318d 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20   triggers */.   
2318e 20 69 66 28 20 21 69 73 56 69 65 77 20 20 26 26   if( !isView  &&
2318f 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62   !IsVirtual(pTab
23190 29 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ) ){.      for(i
23191 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =1, pIdx=pTab->p
23192 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b  Index; pIdx; i++
23193 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  , pIdx=pIdx->pNe
23194 78 74 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  xt){.        sql
23195 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
23196 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72  , OP_Close, iCur
23197 20 2b 20 69 2c 20 70 49 64 78 2d 3e 74 6e 75 6d   + i, pIdx->tnum
23198 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
23199 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2319a 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
2319b 69 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  iCur);.    }.  }
2319c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75  ..  /*.  ** Retu
2319d 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
2319e 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 20   rows that were 
2319f 64 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73  deleted. If this
231a0 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a   routine is .  *
231a1 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64  * generating cod
231a2 65 20 62 65 63 61 75 73 65 20 6f 66 20 61 20 63  e because of a c
231a3 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65  all to sqlite3Ne
231a4 73 74 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20  stedParse(), do 
231a5 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20  not.  ** invoke 
231a6 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
231a7 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  ction..  */.  if
231a8 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
231a9 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26  LITE_CountRows &
231aa 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  & pParse->nested
231ab 3d 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ==0 && !pParse->
231ac 74 72 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20  trigStack ){.   
231ad 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
231ae 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
231af 6f 77 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a  ow, memCnt, 1);.
231b0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
231b1 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
231b2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
231b3 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
231b4 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
231b5 72 6f 77 73 20 64 65 6c 65 74 65 64 22 2c 20 53  rows deleted", S
231b6 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
231b7 20 7d 0a 0a 64 65 6c 65 74 65 5f 66 72 6f 6d 5f   }..delete_from_
231b8 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74  cleanup:.  sqlit
231b9 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70  e3AuthContextPop
231ba 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73  (&sContext);.  s
231bb 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
231bc 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73 74  ete(db, pTabList
231bd 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
231be 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72  Delete(db, pWher
231bf 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  e);.  return;.}.
231c0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
231c1 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44  ine generates VD
231c2 42 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75  BE code that cau
231c3 73 65 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  ses a single row
231c4 20 6f 66 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20   of a.** single 
231c5 74 61 62 6c 65 20 74 6f 20 62 65 20 64 65 6c 65  table to be dele
231c6 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  ted..**.** The V
231c7 44 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61  DBE must be in a
231c8 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 61 74   particular stat
231c9 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  e when this rout
231ca 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ine is called..*
231cb 2a 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20  * These are the 
231cc 72 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  requirements:.**
231cd 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64  .**   1.  A read
231ce 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f  /write cursor po
231cf 69 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20  inting to pTab, 
231d0 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  the table contai
231d1 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20  ning the row.** 
231d2 20 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65        to be dele
231d3 74 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65  ted, must be ope
231d4 6e 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75  ned as cursor nu
231d5 6d 62 65 72 20 22 62 61 73 65 22 2e 0a 2a 2a 0a  mber "base"..**.
231d6 2a 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72  **   2.  Read/wr
231d7 69 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20  ite cursors for 
231d8 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70  all indices of p
231d9 54 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  Tab must be open
231da 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72   as.**       cur
231db 73 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65 2b  sor number base+
231dc 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69  i for the i-th i
231dd 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e  ndex..**.**   3.
231de 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d    The record num
231df 62 65 72 20 6f 66 20 74 68 65 20 72 6f 77 20 74  ber of the row t
231e0 6f 20 62 65 20 64 65 6c 65 74 65 64 20 6d 75 73  o be deleted mus
231e1 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a  t be stored in.*
231e2 2a 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 63  *       memory c
231e3 65 6c 6c 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a  ell iRowid..**.*
231e4 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70  * This routine p
231e5 6f 70 73 20 74 68 65 20 74 6f 70 20 6f 66 20 74  ops the top of t
231e6 68 65 20 73 74 61 63 6b 20 74 6f 20 72 65 6d 6f  he stack to remo
231e7 76 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  ve the record nu
231e8 6d 62 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 6e  mber.** and then
231e9 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
231ea 74 6f 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74  to remove both t
231eb 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20  he table record 
231ec 61 6e 64 20 61 6c 6c 20 69 6e 64 65 78 0a 2a 2a  and all index.**
231ed 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 70 6f   entries that po
231ee 69 6e 74 20 74 6f 20 74 68 61 74 20 72 65 63 6f  int to that reco
231ef 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rd..*/.SQLITE_PR
231f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
231f1 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
231f2 65 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ete(.  Parse *pP
231f3 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
231f4 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
231f5 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
231f6 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f       /* Table co
231f7 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77  ntaining the row
231f8 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a   to be deleted *
231f9 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  /.  int iCur,   
231fa 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
231fb 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
231fc 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
231fd 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a  Rowid,        /*
231fe 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61   Memory cell tha
231ff 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72  t contains the r
23200 6f 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a  owid to delete *
23201 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 20 20 20  /.  int count   
23202 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d         /* Increm
23203 65 6e 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e  ent the row chan
23204 67 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 29 7b  ge counter */.){
23205 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 56  .  int addr;.  V
23206 64 62 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20 70  dbe *v;..  v = p
23207 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
23208 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
23209 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
2320a 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20  otExists, iCur, 
2320b 30 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 73 71  0, iRowid);.  sq
2320c 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
2320d 49 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72  IndexDelete(pPar
2320e 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20  se, pTab, iCur, 
2320f 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  0);.  sqlite3Vdb
23210 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65  eAddOp2(v, OP_De
23211 6c 65 74 65 2c 20 69 43 75 72 2c 20 28 63 6f 75  lete, iCur, (cou
23212 6e 74 3f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  nt?OPFLAG_NCHANG
23213 45 3a 30 29 29 3b 0a 20 20 69 66 28 20 63 6f 75  E:0));.  if( cou
23214 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  nt ){.    sqlite
23215 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
23216 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65   -1, pTab->zName
23217 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
23218 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  }.  sqlite3VdbeJ
23219 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
2321a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2321b 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65  routine generate
2321c 73 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74  s VDBE code that
2321d 20 63 61 75 73 65 73 20 74 68 65 20 64 65 6c 65   causes the dele
2321e 74 69 6f 6e 20 6f 66 20 61 6c 6c 0a 2a 2a 20 69  tion of all.** i
2321f 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61 73 73  ndex entries ass
23220 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73  ociated with a s
23221 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73  ingle row of a s
23222 69 6e 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  ingle table..**.
23223 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73 74  ** The VDBE must
23224 20 62 65 20 69 6e 20 61 20 70 61 72 74 69 63 75   be in a particu
23225 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e 20 74  lar state when t
23226 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
23227 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20  alled..** These 
23228 61 72 65 20 74 68 65 20 72 65 71 75 69 72 65 6d  are the requirem
23229 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  ents:.**.**   1.
2322a 20 20 41 20 72 65 61 64 2f 77 72 69 74 65 20 63    A read/write c
2322b 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
2322c 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61 62 6c  o pTab, the tabl
2322d 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
2322e 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74 6f   row.**       to
2322f 20 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75 73   be deleted, mus
23230 74 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 63  t be opened as c
23231 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 69 43  ursor number "iC
23232 75 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20  ur"..**.**   2. 
23233 20 52 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   Read/write curs
23234 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69  ors for all indi
23235 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74  ces of pTab must
23236 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20   be open as.**  
23237 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d 62       cursor numb
23238 65 72 20 69 43 75 72 2b 69 20 66 6f 72 20 74 68  er iCur+i for th
23239 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a  e i-th index..**
2323a 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 22 69  .**   3.  The "i
2323b 43 75 72 22 20 63 75 72 73 6f 72 20 6d 75 73 74  Cur" cursor must
2323c 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   be pointing to 
2323d 74 68 65 20 72 6f 77 20 74 68 61 74 20 69 73 20  the row that is 
2323e 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 64  to be.**       d
2323f 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  eleted..*/.SQLIT
23240 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23241 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f  qlite3GenerateRo
23242 77 49 6e 64 65 78 44 65 6c 65 74 65 28 0a 20 20  wIndexDelete(.  
23243 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
23244 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
23245 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
23246 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
23247 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
23248 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61    /* Table conta
23249 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74 6f  ining the row to
2324a 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20   be deleted */. 
2324b 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20   int iCur,      
2324c 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
2324d 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62  mber for the tab
2324e 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65  le */.  int *aRe
2324f 67 49 64 78 20 20 20 20 20 20 20 2f 2a 20 4f 6e  gIdx       /* On
23250 6c 79 20 64 65 6c 65 74 65 20 69 66 20 61 52 65  ly delete if aRe
23251 67 49 64 78 21 3d 30 20 26 26 20 61 52 65 67 49  gIdx!=0 && aRegI
23252 64 78 5b 69 5d 3e 30 20 2a 2f 0a 29 7b 0a 20 20  dx[i]>0 */.){.  
23253 69 6e 74 20 69 3b 0a 20 20 49 6e 64 65 78 20 2a  int i;.  Index *
23254 70 49 64 78 3b 0a 20 20 69 6e 74 20 72 31 3b 0a  pIdx;.  int r1;.
23255 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78  .  for(i=1, pIdx
23256 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
23257 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70  Idx; i++, pIdx=p
23258 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
23259 20 69 66 28 20 61 52 65 67 49 64 78 21 3d 30 20   if( aRegIdx!=0 
2325a 26 26 20 61 52 65 67 49 64 78 5b 69 2d 31 5d 3d  && aRegIdx[i-1]=
2325b 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
2325c 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47     r1 = sqlite3G
2325d 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28  enerateIndexKey(
2325e 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20 69 43  pParse, pIdx, iC
2325f 75 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  ur, 0, 0);.    s
23260 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
23261 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
23262 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c 20 69 43  OP_IdxDelete, iC
23263 75 72 2b 69 2c 20 72 31 2c 70 49 64 78 2d 3e 6e  ur+i, r1,pIdx->n
23264 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 7d 0a 7d  Column+1);.  }.}
23265 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
23266 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
23267 61 73 73 65 6d 62 6c 65 20 61 6e 20 69 6e 64 65  assemble an inde
23268 78 20 6b 65 79 20 61 6e 64 20 70 75 74 20 69 74  x key and put it
23269 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20   in register.** 
2326a 72 65 67 4f 75 74 2e 20 20 54 68 65 20 6b 65 79  regOut.  The key
2326b 20 77 69 74 68 20 62 65 20 66 6f 72 20 69 6e 64   with be for ind
2326c 65 78 20 70 49 64 78 20 77 68 69 63 68 20 69 73  ex pIdx which is
2326d 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61   an index on pTa
2326e 62 2e 0a 2a 2a 20 69 43 75 72 20 69 73 20 74 68  b..** iCur is th
2326f 65 20 69 6e 64 65 78 20 6f 66 20 61 20 63 75 72  e index of a cur
23270 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  sor open on the 
23271 70 54 61 62 20 74 61 62 6c 65 20 61 6e 64 20 70  pTab table and p
23272 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 74 68  ointing to.** th
23273 65 20 65 6e 74 72 79 20 74 68 61 74 20 6e 65 65  e entry that nee
23274 64 73 20 69 6e 64 65 78 69 6e 67 2e 0a 2a 2a 0a  ds indexing..**.
23275 2a 2a 20 52 65 74 75 72 6e 20 61 20 72 65 67 69  ** Return a regi
23276 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 69 63  ster number whic
23277 68 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69  h is the first i
23278 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20  n a block of.** 
23279 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20 68  registers that h
2327a 6f 6c 64 73 20 74 68 65 20 65 6c 65 6d 65 6e 74  olds the element
2327b 73 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b  s of the index k
2327c 65 79 2e 20 20 54 68 65 0a 2a 2a 20 62 6c 6f 63  ey.  The.** bloc
2327d 6b 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 68  k of registers h
2327e 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
2327f 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74  deallocated by t
23280 68 65 20 74 69 6d 65 0a 2a 2a 20 74 68 69 73 20  he time.** this 
23281 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e  routine returns.
23282 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23283 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
23284 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 0a  nerateIndexKey(.
23285 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
23286 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
23287 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 49 6e 64  context */.  Ind
23288 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20  ex *pIdx,       
23289 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72  /* The index for
2328a 20 77 68 69 63 68 20 74 6f 20 67 65 6e 65 72 61   which to genera
2328b 74 65 20 61 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  te a key */.  in
2328c 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20  t iCur,         
2328d 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
2328e 72 20 66 6f 72 20 74 68 65 20 70 49 64 78 2d 3e  r for the pIdx->
2328f 70 54 61 62 6c 65 20 74 61 62 6c 65 20 2a 2f 0a  pTable table */.
23290 20 20 69 6e 74 20 72 65 67 4f 75 74 2c 20 20 20    int regOut,   
23291 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
23292 65 20 6e 65 77 20 69 6e 64 65 78 20 6b 65 79 20  e new index key 
23293 74 6f 20 74 68 69 73 20 72 65 67 69 73 74 65 72  to this register
23294 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 4d 61 6b 65   */.  int doMake
23295 52 65 63 20 20 20 20 20 20 2f 2a 20 52 75 6e 20  Rec      /* Run 
23296 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  the OP_MakeRecor
23297 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 66  d instruction if
23298 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 56 64   true */.){.  Vd
23299 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2329a 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6a 3b 0a  pVdbe;.  int j;.
2329b 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2329c 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20  pIdx->pTable;.  
2329d 69 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20 69  int regBase;.  i
2329e 6e 74 20 6e 43 6f 6c 3b 0a 0a 20 20 6e 43 6f 6c  nt nCol;..  nCol
2329f 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   = pIdx->nColumn
232a0 3b 0a 20 20 72 65 67 42 61 73 65 20 3d 20 73 71  ;.  regBase = sq
232a1 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
232a2 65 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 2b 31  e(pParse, nCol+1
232a3 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
232a4 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
232a5 69 64 2c 20 69 43 75 72 2c 20 72 65 67 42 61 73  id, iCur, regBas
232a6 65 2b 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 6a  e+nCol);.  for(j
232a7 3d 30 3b 20 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29  =0; j<nCol; j++)
232a8 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20  {.    int idx = 
232a9 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a  pIdx->aiColumn[j
232aa 5d 3b 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d  ];.    if( idx==
232ab 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
232ac 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
232ad 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
232ae 70 79 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c  py, regBase+nCol
232af 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20  , regBase+j);.  
232b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
232b1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
232b2 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69  (v, OP_Column, i
232b3 43 75 72 2c 20 69 64 78 2c 20 72 65 67 42 61 73  Cur, idx, regBas
232b4 65 2b 6a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  e+j);.      sqli
232b5 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74  te3ColumnDefault
232b6 28 76 2c 20 70 54 61 62 2c 20 69 64 78 29 3b 0a  (v, pTab, idx);.
232b7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
232b8 64 6f 4d 61 6b 65 52 65 63 20 29 7b 0a 20 20 20  doMakeRec ){.   
232b9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
232ba 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
232bb 6f 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43  ord, regBase, nC
232bc 6f 6c 2b 31 2c 20 72 65 67 4f 75 74 29 3b 0a 20  ol+1, regOut);. 
232bd 20 20 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41     sqlite3IndexA
232be 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49  ffinityStr(v, pI
232bf 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dx);.    sqlite3
232c0 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
232c1 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
232c2 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29  regBase, nCol+1)
232c3 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  ;.  }.  sqlite3R
232c4 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
232c5 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c  pParse, regBase,
232c6 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 72 65 74 75   nCol+1);.  retu
232c7 72 6e 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f  rn regBase;.}../
232c8 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56  * Make sure "isV
232c9 69 65 77 22 20 67 65 74 73 20 75 6e 64 65 66 69  iew" gets undefi
232ca 6e 65 64 20 69 6e 20 63 61 73 65 20 74 68 69 73  ned in case this
232cb 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70 61   file becomes pa
232cc 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d 61  rt of.** the ama
232cd 6c 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20 74  lgamation - so t
232ce 68 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 66  hat subsequent f
232cf 69 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65 65 20  iles do not see 
232d0 69 73 56 69 65 77 20 61 73 20 61 0a 2a 2a 20 6d  isView as a.** m
232d1 61 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66 20  acro. */.#undef 
232d2 69 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  isView../*******
232d3 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64  ******* End of d
232d4 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  elete.c ********
232d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232d7 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
232d8 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
232d9 6c 65 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a  le func.c ******
232da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232dc 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
232dd 30 32 20 46 65 62 72 75 61 72 79 20 32 33 0a 2a  02 February 23.*
232de 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
232df 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
232e0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
232e1 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
232e2 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
232e3 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
232e4 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
232e5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
232e6 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
232e7 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
232e8 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
232e9 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
232ea 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
232eb 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
232ec 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
232ed 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
232ee 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
232ef 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
232f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
232f4 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
232f5 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
232f6 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
232f7 6e 74 20 76 61 72 69 6f 75 73 20 53 51 4c 0a 2a  nt various SQL.*
232f8 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53  * functions of S
232f9 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  QLite.  .**.** T
232fa 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65  here is only one
232fb 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c   exported symbol
232fc 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20   in this file - 
232fd 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  the function.** 
232fe 73 71 6c 69 74 65 52 65 67 69 73 74 65 72 42 75  sqliteRegisterBu
232ff 69 6c 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28 29  ildinFunctions()
23300 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f   found at the bo
23301 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65  ttom of the file
23302 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63  ..** All other c
23303 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f  ode has file sco
23304 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66  pe..**.** $Id: f
23305 75 6e 63 2e 63 2c 76 20 31 2e 32 32 35 20 32 30  unc.c,v 1.225 20
23306 30 39 2f 30 33 2f 32 37 20 31 35 3a 32 36 3a 30  09/03/27 15:26:0
23307 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  3 danielk1977 Ex
23308 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  p $.*/../*.** Re
23309 74 75 72 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69  turn the collati
2330a 6e 67 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 6f  ng function asso
2330b 63 69 61 74 65 64 20 77 69 74 68 20 61 20 66 75  ciated with a fu
2330c 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
2330d 63 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  c CollSeq *sqlit
2330e 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71  e3GetFuncCollSeq
2330f 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
23310 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 72 65   *context){.  re
23311 74 75 72 6e 20 63 6f 6e 74 65 78 74 2d 3e 70 43  turn context->pC
23312 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  oll;.}../*.** Im
23313 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
23314 74 68 65 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74  the non-aggregat
23315 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28  e min() and max(
23316 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73  ) functions.*/.s
23317 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61  tatic void minma
23318 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  xFunc(.  sqlite3
23319 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
2331a 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
2331b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2331c 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69  *argv.){.  int i
2331d 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20  ;.  int mask;   
2331e 20 2f 2a 20 30 20 66 6f 72 20 6d 69 6e 28 29 20   /* 0 for min() 
2331f 6f 72 20 30 78 66 66 66 66 66 66 66 66 20 66 6f  or 0xffffffff fo
23320 72 20 6d 61 78 28 29 20 2a 2f 0a 20 20 69 6e 74  r max() */.  int
23321 20 69 42 65 73 74 3b 0a 20 20 43 6f 6c 6c 53 65   iBest;.  CollSe
23322 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 61 73 73  q *pColl;..  ass
23323 65 72 74 28 20 61 72 67 63 3e 31 20 29 3b 0a 20  ert( argc>1 );. 
23324 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 5f   mask = sqlite3_
23325 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
23326 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b 0a  t)==0 ? 0 : -1;.
23327 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
23328 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28  3GetFuncCollSeq(
23329 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65  context);.  asse
2332a 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20 61  rt( pColl );.  a
2332b 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31 20  ssert( mask==-1 
2332c 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20  || mask==0 );.  
2332d 69 42 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28  iBest = 0;.  if(
2332e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2332f 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
23330 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
23331 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69  rn;.  for(i=1; i
23332 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  <argc; i++){.   
23333 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
23334 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29  ue_type(argv[i])
23335 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20  ==SQLITE_NULL ) 
23336 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20  return;.    if( 
23337 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61  (sqlite3MemCompa
23338 72 65 28 61 72 67 76 5b 69 42 65 73 74 5d 2c 20  re(argv[iBest], 
23339 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e  argv[i], pColl)^
2333a 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20 20  mask)>=0 ){.    
2333b 20 20 74 65 73 74 63 61 73 65 28 20 6d 61 73 6b    testcase( mask
2333c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 42 65  ==0 );.      iBe
2333d 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20  st = i;.    }.  
2333e 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  }.  sqlite3_resu
2333f 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74  lt_value(context
23340 2c 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b 0a  , argv[iBest]);.
23341 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
23342 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20  the type of the 
23343 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  argument..*/.sta
23344 74 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66 46  tic void typeofF
23345 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
23346 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
23347 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
23348 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
23349 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73  **argv.){.  cons
2334a 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20  t char *z = 0;. 
2334b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2334c 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 73 77  R(NotUsed);.  sw
2334d 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61  itch( sqlite3_va
2334e 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
2334f 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  ) ){.    case SQ
23350 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a 20  LITE_INTEGER: z 
23351 3d 20 22 69 6e 74 65 67 65 72 22 3b 20 62 72 65  = "integer"; bre
23352 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
23353 49 54 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 3d  ITE_TEXT:    z =
23354 20 22 74 65 78 74 22 3b 20 20 20 20 62 72 65 61   "text";    brea
23355 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
23356 54 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d 20  TE_FLOAT:   z = 
23357 22 72 65 61 6c 22 3b 20 20 20 20 62 72 65 61 6b  "real";    break
23358 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
23359 45 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 22  E_BLOB:    z = "
2335a 62 6c 6f 62 22 3b 20 20 20 20 62 72 65 61 6b 3b  blob";    break;
2335b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20  .    default:   
2335c 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 6e            z = "n
2335d 75 6c 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  ull";    break;.
2335e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65    }.  sqlite3_re
2335f 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
23360 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45  t, z, -1, SQLITE
23361 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f 2a  _STATIC);.}.../*
23362 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
23363 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68  on of the length
23364 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73  () function.*/.s
23365 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67 74  tatic void lengt
23366 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  hFunc(.  sqlite3
23367 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23368 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
23369 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2336a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6c  *argv.){.  int l
2336b 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61  en;..  assert( a
2336c 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53  rgc==1 );.  UNUS
2336d 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
2336e 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71  c);.  switch( sq
2336f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
23370 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20  (argv[0]) ){.   
23371 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f   case SQLITE_BLO
23372 42 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  B:.    case SQLI
23373 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20  TE_INTEGER:.    
23374 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41  case SQLITE_FLOA
23375 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  T: {.      sqlit
23376 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f  e3_result_int(co
23377 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76  ntext, sqlite3_v
23378 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
23379 30 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61  0]));.      brea
2337a 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2337b 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b  e SQLITE_TEXT: {
2337c 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  .      const uns
2337d 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d 20  igned char *z = 
2337e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2337f 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
23380 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65     if( z==0 ) re
23381 74 75 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e 20  turn;.      len 
23382 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  = 0;.      while
23383 28 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  ( *z ){.        
23384 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 53  len++;.        S
23385 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
23386 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  z);.      }.    
23387 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23388 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c 65  _int(context, le
23389 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  n);.      break;
2338a 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
2338b 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  lt: {.      sqli
2338c 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28  te3_result_null(
2338d 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
2338e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
2338f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
23390 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
23391 61 62 73 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a  abs() function.*
23392 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 62  /.static void ab
23393 73 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f  sFunc(sqlite3_co
23394 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
23395 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
23396 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
23397 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
23398 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =1 );.  UNUSED_P
23399 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a  ARAMETER(argc);.
2339a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65    switch( sqlite
2339b 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
2339c 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73  v[0]) ){.    cas
2339d 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
2339e 3a 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 56  : {.      i64 iV
2339f 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
233a0 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d  ue_int64(argv[0]
233a1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 56 61  );.      if( iVa
233a2 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  l<0 ){.        i
233a3 66 28 20 28 69 56 61 6c 3c 3c 31 29 3d 3d 30 20  f( (iVal<<1)==0 
233a4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
233a5 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
233a6 72 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e 74 65  r(context, "inte
233a7 67 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 20 2d  ger overflow", -
233a8 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  1);.          re
233a9 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a  turn;.        }.
233aa 20 20 20 20 20 20 20 20 69 56 61 6c 20 3d 20 2d          iVal = -
233ab 69 56 61 6c 3b 0a 20 20 20 20 20 20 7d 20 0a 20  iVal;.      } . 
233ac 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
233ad 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78  ult_int64(contex
233ae 74 2c 20 69 56 61 6c 29 3b 0a 20 20 20 20 20 20  t, iVal);.      
233af 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
233b0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c   case SQLITE_NUL
233b1 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L: {.      sqlit
233b2 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63  e3_result_null(c
233b3 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62  ontext);.      b
233b4 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
233b5 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
233b6 20 64 6f 75 62 6c 65 20 72 56 61 6c 20 3d 20 73   double rVal = s
233b7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
233b8 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ble(argv[0]);.  
233b9 20 20 20 20 69 66 28 20 72 56 61 6c 3c 30 20 29      if( rVal<0 )
233ba 20 72 56 61 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20   rVal = -rVal;. 
233bb 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
233bc 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65  ult_double(conte
233bd 78 74 2c 20 72 56 61 6c 29 3b 0a 20 20 20 20 20  xt, rVal);.     
233be 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
233bf 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
233c0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
233c1 20 73 75 62 73 74 72 28 29 20 66 75 6e 63 74 69   substr() functi
233c2 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74 72  on..**.** substr
233c3 28 78 2c 70 31 2c 70 32 29 20 20 72 65 74 75 72  (x,p1,p2)  retur
233c4 6e 73 20 70 32 20 63 68 61 72 61 63 74 65 72 73  ns p2 characters
233c5 20 6f 66 20 78 5b 5d 20 62 65 67 69 6e 6e 69 6e   of x[] beginnin
233c6 67 20 77 69 74 68 20 70 31 2e 0a 2a 2a 20 70 31  g with p1..** p1
233c7 20 69 73 20 31 2d 69 6e 64 65 78 65 64 2e 20 20   is 1-indexed.  
233c8 53 6f 20 73 75 62 73 74 72 28 78 2c 31 2c 31 29  So substr(x,1,1)
233c9 20 72 65 74 75 72 6e 73 20 74 68 65 20 66 69 72   returns the fir
233ca 73 74 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20  st character.** 
233cb 6f 66 20 78 2e 20 20 49 66 20 78 20 69 73 20 74  of x.  If x is t
233cc 65 78 74 2c 20 74 68 65 6e 20 77 65 20 61 63 74  ext, then we act
233cd 75 61 6c 6c 79 20 63 6f 75 6e 74 20 55 54 46 2d  ually count UTF-
233ce 38 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a  8 characters..**
233cf 20 49 66 20 78 20 69 73 20 61 20 62 6c 6f 62 2c   If x is a blob,
233d0 20 74 68 65 6e 20 77 65 20 63 6f 75 6e 74 20 62   then we count b
233d1 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  ytes..**.** If p
233d2 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74  1 is negative, t
233d3 68 65 6e 20 77 65 20 62 65 67 69 6e 20 61 62 73  hen we begin abs
233d4 28 70 31 29 20 66 72 6f 6d 20 74 68 65 20 65 6e  (p1) from the en
233d5 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2f 0a 73 74 61  d of x[]..*/.sta
233d6 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 72 46  tic void substrF
233d7 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
233d8 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
233d9 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
233da 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
233db 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75  rgv.){.  const u
233dc 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b  nsigned char *z;
233dd 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
233de 64 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e  d char *z2;.  in
233df 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 70 30 74  t len;.  int p0t
233e0 79 70 65 3b 0a 20 20 69 36 34 20 70 31 2c 20 70  ype;.  i64 p1, p
233e1 32 3b 0a 20 20 69 6e 74 20 6e 65 67 50 32 20 3d  2;.  int negP2 =
233e2 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61   0;..  assert( a
233e3 72 67 63 3d 3d 33 20 7c 7c 20 61 72 67 63 3d 3d  rgc==3 || argc==
233e4 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  2 );.  if( sqlit
233e5 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
233e6 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  gv[1])==SQLITE_N
233e7 55 4c 4c 0a 20 20 20 7c 7c 20 28 61 72 67 63 3d  ULL.   || (argc=
233e8 3d 33 20 26 26 20 73 71 6c 69 74 65 33 5f 76 61  =3 && sqlite3_va
233e9 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 32 5d  lue_type(argv[2]
233ea 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a  )==SQLITE_NULL).
233eb 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b    ){.    return;
233ec 0a 20 20 7d 0a 20 20 70 30 74 79 70 65 20 3d 20  .  }.  p0type = 
233ed 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
233ee 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  pe(argv[0]);.  i
233ef 66 28 20 70 30 74 79 70 65 3d 3d 53 51 4c 49 54  f( p0type==SQLIT
233f0 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 6c 65  E_BLOB ){.    le
233f1 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
233f2 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
233f3 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65  ;.    z = sqlite
233f4 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
233f5 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a  v[0]);.    if( z
233f6 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
233f7 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3d 3d 73    assert( len==s
233f8 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
233f9 65 73 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20  es(argv[0]) );. 
233fa 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
233fb 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
233fc 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
233fd 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75   if( z==0 ) retu
233fe 72 6e 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b  rn;.    len = 0;
233ff 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a  .    for(z2=z; *
23400 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a 20 20 20 20  z2; len++){.    
23401 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
23402 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20  F8(z2);.    }.  
23403 7d 0a 20 20 70 31 20 3d 20 73 71 6c 69 74 65 33  }.  p1 = sqlite3
23404 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
23405 31 5d 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d  1]);.  if( argc=
23406 3d 33 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 73  =3 ){.    p2 = s
23407 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
23408 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69  (argv[2]);.    i
23409 66 28 20 70 32 3c 30 20 29 7b 0a 20 20 20 20 20  f( p2<0 ){.     
2340a 20 70 32 20 3d 20 2d 70 32 3b 0a 20 20 20 20 20   p2 = -p2;.     
2340b 20 6e 65 67 50 32 20 3d 20 31 3b 0a 20 20 20 20   negP2 = 1;.    
2340c 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  }.  }else{.    p
2340d 32 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  2 = sqlite3_cont
2340e 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
2340f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53  ntext)->aLimit[S
23410 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
23411 54 48 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  TH];.  }.  if( p
23412 31 3c 30 20 29 7b 0a 20 20 20 20 70 31 20 2b 3d  1<0 ){.    p1 +=
23413 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 31   len;.    if( p1
23414 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 20 2b  <0 ){.      p2 +
23415 3d 20 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20  = p1;.      if( 
23416 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20  p2<0 ) p2 = 0;. 
23417 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20       p1 = 0;.   
23418 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70   }.  }else if( p
23419 31 3e 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b  1>0 ){.    p1--;
2341a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 32 3e  .  }else if( p2>
2341b 30 20 29 7b 0a 20 20 20 20 70 32 2d 2d 3b 0a 20  0 ){.    p2--;. 
2341c 20 7d 0a 20 20 69 66 28 20 6e 65 67 50 32 20 29   }.  if( negP2 )
2341d 7b 0a 20 20 20 20 70 31 20 2d 3d 20 70 32 3b 0a  {.    p1 -= p2;.
2341e 20 20 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a      if( p1<0 ){.
2341f 20 20 20 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a        p2 += p1;.
23420 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20        p1 = 0;.  
23421 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
23422 28 20 70 31 3e 3d 30 20 26 26 20 70 32 3e 3d 30  ( p1>=0 && p2>=0
23423 20 29 3b 0a 20 20 69 66 28 20 70 31 2b 70 32 3e   );.  if( p1+p2>
23424 6c 65 6e 20 29 7b 0a 20 20 20 20 70 32 20 3d 20  len ){.    p2 = 
23425 6c 65 6e 2d 70 31 3b 0a 20 20 20 20 69 66 28 20  len-p1;.    if( 
23426 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20  p2<0 ) p2 = 0;. 
23427 20 7d 0a 20 20 69 66 28 20 70 30 74 79 70 65 21   }.  if( p0type!
23428 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a  =SQLITE_BLOB ){.
23429 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 26 26      while( *z &&
2342a 20 70 31 20 29 7b 0a 20 20 20 20 20 20 53 51 4c   p1 ){.      SQL
2342b 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29  ITE_SKIP_UTF8(z)
2342c 3b 0a 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20 20  ;.      p1--;.  
2342d 20 20 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a    }.    for(z2=z
2342e 3b 20 2a 7a 32 20 26 26 20 70 32 3b 20 70 32 2d  ; *z2 && p2; p2-
2342f 2d 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45  -){.      SQLITE
23430 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a  _SKIP_UTF8(z2);.
23431 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
23432 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
23433 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c  ntext, (char*)z,
23434 20 28 69 6e 74 29 28 7a 32 2d 7a 29 2c 20 53 51   (int)(z2-z), SQ
23435 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
23436 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
23437 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
23438 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72  b(context, (char
23439 2a 29 26 7a 5b 70 31 5d 2c 20 28 69 6e 74 29 70  *)&z[p1], (int)p
2343a 32 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  2, SQLITE_TRANSI
2343b 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ENT);.  }.}../*.
2343c 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
2343d 6e 20 6f 66 20 74 68 65 20 72 6f 75 6e 64 28 29  n of the round()
2343e 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 23 69 66   function.*/.#if
2343f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23440 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
23441 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 75 6e  static void roun
23442 64 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f  dFunc(sqlite3_co
23443 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
23444 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
23445 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
23446 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20  .  int n = 0;.  
23447 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72  double r;.  char
23448 20 7a 42 75 66 5b 35 30 30 5d 3b 20 20 2f 2a 20   zBuf[500];  /* 
23449 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
2344a 25 66 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  %f representatio
2344b 6e 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  n of the largest
2344c 20 64 6f 75 62 6c 65 20 2a 2f 0a 20 20 61 73 73   double */.  ass
2344d 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20  ert( argc==1 || 
2344e 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28  argc==2 );.  if(
2344f 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20   argc==2 ){.    
23450 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d  if( SQLITE_NULL=
23451 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
23452 79 70 65 28 61 72 67 76 5b 31 5d 29 20 29 20 72  ype(argv[1]) ) r
23453 65 74 75 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 73  eturn;.    n = s
23454 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
23455 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69  (argv[1]);.    i
23456 66 28 20 6e 3e 33 30 20 29 20 6e 20 3d 20 33 30  f( n>30 ) n = 30
23457 3b 0a 20 20 20 20 69 66 28 20 6e 3c 30 20 29 20  ;.    if( n<0 ) 
23458 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  n = 0;.  }.  if(
23459 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2345a 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
2345b 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
2345c 72 6e 3b 0a 20 20 72 20 3d 20 73 71 6c 69 74 65  rn;.  r = sqlite
2345d 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61  3_value_double(a
2345e 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74  rgv[0]);.  sqlit
2345f 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
23460 6f 66 28 7a 42 75 66 29 2c 7a 42 75 66 2c 22 25  of(zBuf),zBuf,"%
23461 2e 2a 66 22 2c 6e 2c 72 29 3b 0a 20 20 73 71 6c  .*f",n,r);.  sql
23462 69 74 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 26  ite3AtoF(zBuf, &
23463 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  r);.  sqlite3_re
23464 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
23465 65 78 74 2c 20 72 29 3b 0a 7d 0a 23 65 6e 64 69  ext, r);.}.#endi
23466 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  f../*.** Allocat
23467 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66  e nByte bytes of
23468 20 73 70 61 63 65 20 75 73 69 6e 67 20 73 71 6c   space using sql
23469 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49  ite3_malloc(). I
2346a 66 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74  f the.** allocat
2346b 69 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c 6c 20  ion fails, call 
2346c 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2346d 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20  rror_nomem() to 
2346e 6e 6f 74 69 66 79 0a 2a 2a 20 74 68 65 20 64 61  notify.** the da
2346f 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68  tabase handle th
23470 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  at malloc() has 
23471 66 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  failed..*/.stati
23472 63 20 76 6f 69 64 20 2a 63 6f 6e 74 65 78 74 4d  c void *contextM
23473 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 63 6f  alloc(sqlite3_co
23474 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
23475 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 63 68  i64 nByte){.  ch
23476 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 6e 42 79  ar *z;.  if( nBy
23477 74 65 3e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  te>sqlite3_conte
23478 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
23479 74 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51  text)->aLimit[SQ
2347a 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
2347b 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  H] ){.    sqlite
2347c 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
2347d 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a  oobig(context);.
2347e 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 7d 65 6c      z = 0;.  }el
2347f 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  se{.    z = sqli
23480 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 6e  te3Malloc((int)n
23481 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Byte);.    if( !
23482 7a 20 26 26 20 6e 42 79 74 65 3e 30 20 29 7b 0a  z && nByte>0 ){.
23483 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
23484 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
23485 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d  (context);.    }
23486 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b  .  }.  return z;
23487 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
23488 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
23489 75 70 70 65 72 28 29 20 61 6e 64 20 6c 6f 77 65  upper() and lowe
2348a 72 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  r() SQL function
2348b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
2348c 64 20 75 70 70 65 72 46 75 6e 63 28 73 71 6c 69  d upperFunc(sqli
2348d 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2348e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
2348f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
23490 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a  argv){.  char *z
23491 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  1;.  const char 
23492 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  *z2;.  int i, n;
23493 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
23494 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a 32 20  TER(argc);.  z2 
23495 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
23496 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
23497 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  [0]);.  n = sqli
23498 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
23499 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56  argv[0]);.  /* V
2349a 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63  erify that the c
2349b 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20  all to _bytes() 
2349c 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64  does not invalid
2349d 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 20  ate the _text() 
2349e 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73  pointer */.  ass
2349f 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29  ert( z2==(char*)
234a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
234a1 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20  xt(argv[0]) );. 
234a2 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a   if( z2 ){.    z
234a3 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  1 = contextMallo
234a4 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
234a5 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20  )n)+1);.    if( 
234a6 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  z1 ){.      memc
234a7 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b  py(z1, z2, n+1);
234a8 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
234a9 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  z1[i]; i++){.   
234aa 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 28 63 68       z1[i] = (ch
234ab 61 72 29 73 71 6c 69 74 65 33 54 6f 75 70 70 65  ar)sqlite3Touppe
234ac 72 28 7a 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20  r(z1[i]);.      
234ad 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
234ae 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
234af 65 78 74 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c  ext, z1, -1, sql
234b0 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20  ite3_free);.    
234b1 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  }.  }.}.static v
234b2 6f 69 64 20 6c 6f 77 65 72 46 75 6e 63 28 73 71  oid lowerFunc(sq
234b3 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
234b4 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
234b5 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
234b6 2a 2a 61 72 67 76 29 7b 0a 20 20 75 38 20 2a 7a  **argv){.  u8 *z
234b7 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  1;.  const char 
234b8 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  *z2;.  int i, n;
234b9 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
234ba 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a 32 20  TER(argc);.  z2 
234bb 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
234bc 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
234bd 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  [0]);.  n = sqli
234be 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
234bf 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56  argv[0]);.  /* V
234c0 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63  erify that the c
234c1 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20  all to _bytes() 
234c2 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64  does not invalid
234c3 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 20  ate the _text() 
234c4 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73  pointer */.  ass
234c5 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29  ert( z2==(char*)
234c6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
234c7 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20  xt(argv[0]) );. 
234c8 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a   if( z2 ){.    z
234c9 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  1 = contextMallo
234ca 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
234cb 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20  )n)+1);.    if( 
234cc 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  z1 ){.      memc
234cd 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b  py(z1, z2, n+1);
234ce 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
234cf 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  z1[i]; i++){.   
234d0 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 73 71 6c       z1[i] = sql
234d1 69 74 65 33 54 6f 6c 6f 77 65 72 28 7a 31 5b 69  ite3Tolower(z1[i
234d2 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
234d3 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
234d4 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28  _text(context, (
234d5 63 68 61 72 20 2a 29 7a 31 2c 20 2d 31 2c 20 73  char *)z1, -1, s
234d6 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
234d7 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
234d8 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
234d9 6f 66 20 74 68 65 20 49 46 4e 55 4c 4c 28 29 2c  of the IFNULL(),
234da 20 4e 56 4c 28 29 2c 20 61 6e 64 20 43 4f 41 4c   NVL(), and COAL
234db 45 53 43 45 28 29 20 66 75 6e 63 74 69 6f 6e 73  ESCE() functions
234dc 2e 20 20 0a 2a 2a 20 41 6c 6c 20 74 68 72 65 65  .  .** All three
234dd 20 64 6f 20 74 68 65 20 73 61 6d 65 20 74 68 69   do the same thi
234de 6e 67 2e 20 20 54 68 65 79 20 72 65 74 75 72 6e  ng.  They return
234df 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 4e   the first non-N
234e0 55 4c 4c 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e  ULL.** argument.
234e1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
234e2 69 66 6e 75 6c 6c 46 75 6e 63 28 0a 20 20 73 71  ifnullFunc(.  sq
234e3 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
234e4 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
234e5 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
234e6 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
234e7 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
234e8 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a  ; i<argc; i++){.
234e9 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e      if( SQLITE_N
234ea 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c  ULL!=sqlite3_val
234eb 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29  ue_type(argv[i])
234ec 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
234ed 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63  3_result_value(c
234ee 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 5d 29  ontext, argv[i])
234ef 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
234f0 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
234f1 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
234f2 20 6f 66 20 72 61 6e 64 6f 6d 28 29 2e 20 20 52   of random().  R
234f3 65 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 69  eturn a random i
234f4 6e 74 65 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61  nteger.  .*/.sta
234f5 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 46  tic void randomF
234f6 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
234f7 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
234f8 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
234f9 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
234fa 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20  **NotUsed2.){.  
234fb 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 3b 0a  sqlite_int64 r;.
234fc 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
234fd 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
234fe 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65  Used2);.  sqlite
234ff 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
23500 65 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20 20 69  eof(r), &r);.  i
23501 66 28 20 28 72 3c 3c 31 29 3d 3d 30 20 29 20 72  f( (r<<1)==0 ) r
23502 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e   = 0;  /* Preven
23503 74 20 30 78 38 30 30 30 2e 2e 2e 2e 20 61 73 20  t 0x8000.... as 
23504 74 68 65 20 72 65 73 75 6c 74 20 73 6f 20 74 68  the result so th
23505 61 74 20 77 65 20 2a 2f 0a 20 20 20 20 20 20 20  at we */.       
23506 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23507 20 20 20 2f 2a 20 63 61 6e 20 61 6c 77 61 79 73     /* can always
23508 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74 68 65   do abs() of the
23509 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 73 71 6c   result */.  sql
2350a 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
2350b 34 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 7d  4(context, r);.}
2350c 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
2350d 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d  tation of random
2350e 62 6c 6f 62 28 4e 29 2e 20 20 52 65 74 75 72 6e  blob(N).  Return
2350f 20 61 20 72 61 6e 64 6f 6d 20 62 6c 6f 62 0a 2a   a random blob.*
23510 2a 20 74 68 61 74 20 69 73 20 4e 20 62 79 74 65  * that is N byte
23511 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  s long..*/.stati
23512 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 42 6c 6f  c void randomBlo
23513 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  b(.  sqlite3_con
23514 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
23515 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
23516 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
23517 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  v.){.  int n;.  
23518 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
23519 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
2351a 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  ==1 );.  UNUSED_
2351b 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
2351c 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
2351d 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d  alue_int(argv[0]
2351e 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29 7b 0a  );.  if( n<1 ){.
2351f 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20      n = 1;.  }. 
23520 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c   p = contextMall
23521 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b 0a  oc(context, n);.
23522 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73    if( p ){.    s
23523 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
23524 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c  s(n, p);.    sql
23525 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
23526 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
23527 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66  )p, n, sqlite3_f
23528 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ree);.  }.}../*.
23529 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
2352a 6e 20 6f 66 20 74 68 65 20 6c 61 73 74 5f 69 6e  n of the last_in
2352b 73 65 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c  sert_rowid() SQL
2352c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
2352d 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20  return.** value 
2352e 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
2352f 68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  he sqlite3_last_
23530 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 41  insert_rowid() A
23531 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  PI function..*/.
23532 73 74 61 74 69 63 20 76 6f 69 64 20 6c 61 73 74  static void last
23533 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 0a 20  _insert_rowid(. 
23534 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
23535 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e   *context, .  in
23536 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71  t NotUsed, .  sq
23537 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
23538 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69  tUsed2.){.  sqli
23539 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
2353a 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
2353b 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
2353c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
2353d 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
2353e 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ed2);.  sqlite3_
2353f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e  result_int64(con
23540 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 6c 61  text, sqlite3_la
23541 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
23542 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  db));.}../*.** I
23543 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
23544 20 74 68 65 20 63 68 61 6e 67 65 73 28 29 20 53   the changes() S
23545 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
23546 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
23547 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73  s the.** same as
23548 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61   the sqlite3_cha
23549 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74  nges() API funct
2354a 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
2354b 6f 69 64 20 63 68 61 6e 67 65 73 28 0a 20 20 73  oid changes(.  s
2354c 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
2354d 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
2354e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
2354f 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
23550 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  d2.){.  sqlite3 
23551 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
23552 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
23553 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53  context);.  UNUS
23554 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
23555 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
23556 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
23557 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20  lt_int(context, 
23558 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
23559 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  db));.}../*.** I
2355a 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2355b 20 74 68 65 20 74 6f 74 61 6c 5f 63 68 61 6e 67   the total_chang
2355c 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  es() SQL functio
2355d 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  n.  The return v
2355e 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 73  alue is.** the s
2355f 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74  ame as the sqlit
23560 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
23561 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  () API function.
23562 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23563 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 0a 20  total_changes(. 
23564 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
23565 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
23566 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
23567 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
23568 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65  sed2.){.  sqlite
23569 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
2356a 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2356b 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e  e(context);.  UN
2356c 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
2356d 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
2356e 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  2);.  sqlite3_re
2356f 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
23570 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  , sqlite3_total_
23571 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a  changes(db));.}.
23572 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75  ./*.** A structu
23573 72 65 20 64 65 66 69 6e 69 6e 67 20 68 6f 77 20  re defining how 
23574 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 79 6c 65  to do GLOB-style
23575 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f   comparisons..*/
23576 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49  .struct compareI
23577 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 74 63 68  nfo {.  u8 match
23578 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f  All;.  u8 matchO
23579 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65  ne;.  u8 matchSe
2357a 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 3b 0a  t;.  u8 noCase;.
2357b 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 4c 49  };../*.** For LI
2357c 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d 61 74 63  KE and GLOB matc
2357d 68 69 6e 67 20 6f 6e 20 45 42 43 44 49 43 20 6d  hing on EBCDIC m
2357e 61 63 68 69 6e 65 73 2c 20 61 73 73 75 6d 65 20  achines, assume 
2357f 74 68 61 74 20 65 76 65 72 79 0a 2a 2a 20 63 68  that every.** ch
23580 61 72 61 63 74 65 72 20 69 73 20 65 78 61 63 74  aracter is exact
23581 6c 79 20 6f 6e 65 20 62 79 74 65 20 69 6e 20 73  ly one byte in s
23582 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 61 6c 6c 20  ize.  Also, all 
23583 63 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 2a  characters are.*
23584 2a 20 61 62 6c 65 20 74 6f 20 70 61 72 74 69 63  * able to partic
23585 69 70 61 74 65 20 69 6e 20 75 70 70 65 72 2d 63  ipate in upper-c
23586 61 73 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63 61 73  ase-to-lower-cas
23587 65 20 6d 61 70 70 69 6e 67 73 20 69 6e 20 45 42  e mappings in EB
23588 43 44 49 43 0a 2a 2a 20 77 68 65 72 65 61 73 20  CDIC.** whereas 
23589 6f 6e 6c 79 20 63 68 61 72 61 63 74 65 72 73 20  only characters 
2358a 6c 65 73 73 20 74 68 61 6e 20 30 78 38 30 20 64  less than 0x80 d
2358b 6f 20 69 6e 20 41 53 43 49 49 2e 0a 2a 2f 0a 23  o in ASCII..*/.#
2358c 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
2358d 45 5f 45 42 43 44 49 43 29 0a 23 20 64 65 66 69  E_EBCDIC).# defi
2358e 6e 65 20 73 71 6c 69 74 65 33 55 74 66 38 52 65  ne sqlite3Utf8Re
2358f 61 64 28 41 2c 42 2c 43 29 20 20 28 2a 28 41 2b  ad(A,B,C)  (*(A+
23590 2b 29 29 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f  +)).# define Glo
23591 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 41 29  gUpperToLower(A)
23592 20 20 20 20 20 41 20 3d 20 73 71 6c 69 74 65 33       A = sqlite3
23593 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 0a  UpperToLower[A].
23594 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 47  #else.# define G
23595 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
23596 41 29 20 20 20 20 20 69 66 28 20 41 3c 30 78 38  A)     if( A<0x8
23597 30 20 29 7b 20 41 20 3d 20 73 71 6c 69 74 65 33  0 ){ A = sqlite3
23598 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 3b  UpperToLower[A];
23599 20 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69   }.#endif..stati
2359a 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63  c const struct c
2359b 6f 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f 62 49  ompareInfo globI
2359c 6e 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 3f 27  nfo = { '*', '?'
2359d 2c 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a 20 54  , '[', 0 };./* T
2359e 68 65 20 63 6f 72 72 65 63 74 20 53 51 4c 2d 39  he correct SQL-9
2359f 32 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f  2 behavior is fo
235a0 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61  r the LIKE opera
235a1 74 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a 2a 2a  tor to ignore.**
235a2 20 63 61 73 65 2e 20 20 54 68 75 73 20 20 27 61   case.  Thus  'a
235a3 27 20 4c 49 4b 45 20 27 41 27 20 77 6f 75 6c 64  ' LIKE 'A' would
235a4 20 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73 74 61   be true. */.sta
235a5 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
235a6 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b   compareInfo lik
235a7 65 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 27 25  eInfoNorm = { '%
235a8 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31 20 7d  ', '_',   0, 1 }
235a9 3b 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 43  ;./* If SQLITE_C
235aa 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49  ASE_SENSITIVE_LI
235ab 4b 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  KE is defined, t
235ac 68 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f 70 65  hen the LIKE ope
235ad 72 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61 73 65  rator.** is case
235ae 20 73 65 6e 73 69 74 69 76 65 20 63 61 75 73 69   sensitive causi
235af 6e 67 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20  ng 'a' LIKE 'A' 
235b0 74 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f 0a 73  to be false */.s
235b1 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
235b2 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c  ct compareInfo l
235b3 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 27  ikeInfoAlt = { '
235b4 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 20  %', '_',   0, 0 
235b5 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72  };../*.** Compar
235b6 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69  e two UTF-8 stri
235b7 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79  ngs for equality
235b8 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74   where the first
235b9 20 73 74 72 69 6e 67 20 63 61 6e 0a 2a 2a 20 70   string can.** p
235ba 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 61 20  otentially be a 
235bb 22 67 6c 6f 62 22 20 65 78 70 72 65 73 73 69 6f  "glob" expressio
235bc 6e 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20  n.  Return true 
235bd 28 31 29 20 69 66 20 74 68 65 79 0a 2a 2a 20 61  (1) if they.** a
235be 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20  re the same and 
235bf 66 61 6c 73 65 20 28 30 29 20 69 66 20 74 68 65  false (0) if the
235c0 79 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e  y are different.
235c1 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e 67 20  .**.** Globbing 
235c2 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  rules:.**.**    
235c3 20 20 27 2a 27 20 20 20 20 20 20 20 4d 61 74 63    '*'       Matc
235c4 68 65 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65  hes any sequence
235c5 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65   of zero or more
235c6 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
235c7 2a 2a 20 20 20 20 20 20 27 3f 27 20 20 20 20 20  **      '?'     
235c8 20 20 4d 61 74 63 68 65 73 20 65 78 61 63 74 6c    Matches exactl
235c9 79 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 2e  y one character.
235ca 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e 2e 5d  .**.**     [...]
235cb 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e        Matches on
235cc 65 20 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d  e character from
235cd 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69   the enclosed li
235ce 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  st of.**        
235cf 20 20 20 20 20 20 20 20 63 68 61 72 61 63 74 65          characte
235d0 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 5e  rs..**.**     [^
235d1 2e 2e 2e 5d 20 20 20 20 20 4d 61 74 63 68 65 73  ...]     Matches
235d2 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 6e   one character n
235d3 6f 74 20 69 6e 20 74 68 65 20 65 6e 63 6c 6f 73  ot in the enclos
235d4 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 57  ed list..**.** W
235d5 69 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e  ith the [...] an
235d6 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e  d [^...] matchin
235d7 67 2c 20 61 20 27 5d 27 20 63 68 61 72 61 63 74  g, a ']' charact
235d8 65 72 20 63 61 6e 20 62 65 20 69 6e 63 6c 75 64  er can be includ
235d9 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73  ed.** in the lis
235da 74 20 62 79 20 6d 61 6b 69 6e 67 20 69 74 20 74  t by making it t
235db 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74  he first charact
235dc 65 72 20 61 66 74 65 72 20 27 5b 27 20 6f 72 20  er after '[' or 
235dd 27 5e 27 2e 20 20 41 0a 2a 2a 20 72 61 6e 67 65  '^'.  A.** range
235de 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 63   of characters c
235df 61 6e 20 62 65 20 73 70 65 63 69 66 69 65 64 20  an be specified 
235e0 75 73 69 6e 67 20 27 2d 27 2e 20 20 45 78 61 6d  using '-'.  Exam
235e1 70 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d 22 20  ple:.** "[a-z]" 
235e2 6d 61 74 63 68 65 73 20 61 6e 79 20 73 69 6e 67  matches any sing
235e3 6c 65 20 6c 6f 77 65 72 2d 63 61 73 65 20 6c 65  le lower-case le
235e4 74 74 65 72 2e 20 20 54 6f 20 6d 61 74 63 68 20  tter.  To match 
235e5 61 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a 20 69  a '-', make.** i
235e6 74 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61  t the last chara
235e7 63 74 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74  cter in the list
235e8 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
235e9 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20  tine is usually 
235ea 71 75 69 63 6b 2c 20 62 75 74 20 63 61 6e 20 62  quick, but can b
235eb 65 20 4e 2a 2a 32 20 69 6e 20 74 68 65 20 77 6f  e N**2 in the wo
235ec 72 73 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  rst case..**.** 
235ed 48 69 6e 74 73 3a 20 74 6f 20 6d 61 74 63 68 20  Hints: to match 
235ee 27 2a 27 20 6f 72 20 27 3f 27 2c 20 70 75 74 20  '*' or '?', put 
235ef 74 68 65 6d 20 69 6e 20 22 5b 5d 22 2e 20 20 4c  them in "[]".  L
235f0 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
235f1 20 20 20 20 20 20 20 20 61 62 63 5b 2a 5d 78 79          abc[*]xy
235f2 7a 20 20 20 20 20 20 20 20 4d 61 74 63 68 65 73  z        Matches
235f3 20 22 61 62 63 2a 78 79 7a 22 20 6f 6e 6c 79 0a   "abc*xyz" only.
235f4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
235f5 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 0a 20 20  tternCompare(.  
235f6 63 6f 6e 73 74 20 75 38 20 2a 7a 50 61 74 74 65  const u8 *zPatte
235f7 72 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rn,             
235f8 20 2f 2a 20 54 68 65 20 67 6c 6f 62 20 70 61 74   /* The glob pat
235f9 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tern */.  const 
235fa 75 38 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20  u8 *zString,    
235fb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
235fc 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70  e string to comp
235fd 61 72 65 20 61 67 61 69 6e 73 74 20 74 68 65 20  are against the 
235fe 67 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  glob */.  const 
235ff 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
23600 66 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e  fo *pInfo, /* In
23601 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
23602 68 6f 77 20 74 6f 20 64 6f 20 74 68 65 20 63 6f  how to do the co
23603 6d 70 61 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  mpare */.  const
23604 20 69 6e 74 20 65 73 63 20 20 20 20 20 20 20 20   int esc        
23605 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
23606 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63  he escape charac
23607 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ter */.){.  int 
23608 63 2c 20 63 32 3b 0a 20 20 69 6e 74 20 69 6e 76  c, c2;.  int inv
23609 65 72 74 3b 0a 20 20 69 6e 74 20 73 65 65 6e 3b  ert;.  int seen;
2360a 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 20 3d  .  u8 matchOne =
2360b 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65   pInfo->matchOne
2360c 3b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 20  ;.  u8 matchAll 
2360d 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c  = pInfo->matchAl
2360e 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74  l;.  u8 matchSet
2360f 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 53   = pInfo->matchS
23610 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 20  et;.  u8 noCase 
23611 3d 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b  = pInfo->noCase;
23612 20 0a 20 20 69 6e 74 20 70 72 65 76 45 73 63 61   .  int prevEsca
23613 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54  pe = 0;     /* T
23614 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76 69  rue if the previ
23615 6f 75 73 20 63 68 61 72 61 63 74 65 72 20 77 61  ous character wa
23616 73 20 27 65 73 63 61 70 65 27 20 2a 2f 0a 0a 20  s 'escape' */.. 
23617 20 77 68 69 6c 65 28 20 28 63 20 3d 20 73 71 6c   while( (c = sql
23618 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
23619 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74 74 65 72  ttern,0,&zPatter
2361a 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66  n))!=0 ){.    if
2361b 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
2361c 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 29 7b 0a   c==matchAll ){.
2361d 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d        while( (c=
2361e 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
2361f 7a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74  zPattern,0,&zPat
23620 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 68 41  tern)) == matchA
23621 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ll.             
23622 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 68 4f    || c == matchO
23623 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ne ){.        if
23624 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26 26  ( c==matchOne &&
23625 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
23626 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53  (zString, 0, &zS
23627 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 20  tring)==0 ){.   
23628 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
23629 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2362a 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d   }.      if( c==
2362b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  0 ){.        ret
2362c 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 1;.      }el
2362d 73 65 20 69 66 28 20 63 3d 3d 65 73 63 20 29 7b  se if( c==esc ){
2362e 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c  .        c = sql
2362f 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
23630 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74  ttern, 0, &zPatt
23631 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ern);.        if
23632 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
23633 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
23634 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
23635 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
23636 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Set ){.        a
23637 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b  ssert( esc==0 );
23638 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
23639 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49   is GLOB, not LI
2363a 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  KE */.        as
2363b 73 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c 30  sert( matchSet<0
2363c 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 69  x80 );  /* '[' i
2363d 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  s a single-byte 
2363e 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20  character */.   
2363f 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 74       while( *zSt
23640 72 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e 43  ring && patternC
23641 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e  ompare(&zPattern
23642 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e  [-1],zString,pIn
23643 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 20  fo,esc)==0 ){.  
23644 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53          SQLITE_S
23645 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e 67  KIP_UTF8(zString
23646 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
23647 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 74       return *zSt
23648 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 7d  ring!=0;.      }
23649 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63  .      while( (c
2364a 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
2364b 65 61 64 28 7a 53 74 72 69 6e 67 2c 30 2c 26 7a  ead(zString,0,&z
2364c 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20  String))!=0 ){. 
2364d 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73         if( noCas
2364e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 47  e ){.          G
2364f 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
23650 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 47  c2);.          G
23651 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
23652 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68  c);.          wh
23653 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20  ile( c2 != 0 && 
23654 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20  c2 != c ){.     
23655 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
23656 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72  te3Utf8Read(zStr
23657 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67  ing, 0, &zString
23658 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 47  );.            G
23659 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
2365a 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  c2);.          }
2365b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
2365c 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
2365d 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 21   c2 != 0 && c2 !
2365e 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  = c ){.         
2365f 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55     c2 = sqlite3U
23660 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
23661 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20   0, &zString);. 
23662 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
23663 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
23664 20 63 32 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   c2==0 ) return 
23665 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  0;.        if( p
23666 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 50  atternCompare(zP
23667 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67 2c 70  attern,zString,p
23668 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65 74 75  Info,esc) ) retu
23669 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
2366a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2366b 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65    }else if( !pre
2366c 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61  vEscape && c==ma
2366d 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20  tchOne ){.      
2366e 69 66 28 20 73 71 6c 69 74 65 33 55 74 66 38 52  if( sqlite3Utf8R
2366f 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20  ead(zString, 0, 
23670 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a  &zString)==0 ){.
23671 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
23672 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
23673 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
23674 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Set ){.      int
23675 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20   prior_c = 0;.  
23676 20 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d      assert( esc=
23677 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68 69 73  =0 );    /* This
23678 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66 6f 72   only occurs for
23679 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20   GLOB, not LIKE 
2367a 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20 3d 20  */.      seen = 
2367b 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72 74 20  0;.      invert 
2367c 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d 20 73  = 0;.      c = s
2367d 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
2367e 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72  String, 0, &zStr
2367f 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ing);.      if( 
23680 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  c==0 ) return 0;
23681 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69  .      c2 = sqli
23682 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
23683 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65  tern, 0, &zPatte
23684 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63  rn);.      if( c
23685 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20  2=='^' ){.      
23686 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20    invert = 1;.  
23687 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
23688 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
23689 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72  ern, 0, &zPatter
2368a 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
2368b 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b    if( c2==']' ){
2368c 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
2368d 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a  ']' ) seen = 1;.
2368e 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c          c2 = sql
2368f 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
23690 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74  ttern, 0, &zPatt
23691 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ern);.      }.  
23692 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 26 26      while( c2 &&
23693 20 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20 20 20   c2!=']' ){.    
23694 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d 27 20      if( c2=='-' 
23695 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d  && zPattern[0]!=
23696 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b  ']' && zPattern[
23697 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72 5f 63  0]!=0 && prior_c
23698 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >0 ){.          
23699 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
2369a 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30  Read(zPattern, 0
2369b 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20  , &zPattern);.  
2369c 20 20 20 20 20 20 20 20 69 66 28 20 63 3e 3d 70          if( c>=p
2369d 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63 32 20  rior_c && c<=c2 
2369e 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20  ) seen = 1;.    
2369f 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20        prior_c = 
236a0 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
236a1 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
236a2 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20  c==c2 ){.       
236a3 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a 20       seen = 1;. 
236a4 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
236a5 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 63       prior_c = c
236a6 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  2;.        }.   
236a7 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
236a8 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65  3Utf8Read(zPatte
236a9 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72 6e  rn, 0, &zPattern
236aa 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
236ab 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 73   if( c2==0 || (s
236ac 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d 30  een ^ invert)==0
236ad 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
236ae 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
236af 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73 63 3d    }else if( esc=
236b0 3d 63 20 26 26 20 21 70 72 65 76 45 73 63 61 70  =c && !prevEscap
236b1 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65 76 45  e ){.      prevE
236b2 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20 20 7d  scape = 1;.    }
236b3 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32 20 3d  else{.      c2 =
236b4 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
236b5 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53  (zString, 0, &zS
236b6 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66  tring);.      if
236b7 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20  ( noCase ){.    
236b8 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c      GlogUpperToL
236b9 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20 20  ower(c);.       
236ba 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
236bb 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r(c2);.      }. 
236bc 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 20 29       if( c!=c2 )
236bd 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
236be 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
236bf 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30    prevEscape = 0
236c0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
236c1 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d 30  turn *zString==0
236c2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74  ;.}../*.** Count
236c3 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
236c4 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c 49  imes that the LI
236c5 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72 20  KE operator (or 
236c6 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a 2a  GLOB which is.**
236c7 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 69 6f   just a variatio
236c8 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73 20  n of LIKE) gets 
236c9 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73  called.  This is
236ca 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
236cb 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69  g.** only..*/.#i
236cc 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
236cd 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
236ce 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f 75  sqlite3_like_cou
236cf 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
236d0 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
236d1 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b  ation of the lik
236d2 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  e() SQL function
236d3 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  .  This function
236d4 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74   implements.** t
236d5 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45  he build-in LIKE
236d6 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20   operator.  The 
236d7 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
236d8 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  o the function i
236d9 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e  s the.** pattern
236da 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
236db 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
236dc 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65  string.  So, the
236dd 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a   SQL statements:
236de 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c  .**.**       A L
236df 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69  IKE B.**.** is i
236e0 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69  mplemented as li
236e1 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 54  ke(B,A)..**.** T
236e2 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74 69 6f  his same functio
236e3 6e 20 28 77 69 74 68 20 61 20 64 69 66 66 65 72  n (with a differ
236e4 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  ent compareInfo 
236e5 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d 70 75  structure) compu
236e6 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42 20  tes.** the GLOB 
236e7 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61  operator..*/.sta
236e8 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 75 6e  tic void likeFun
236e9 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
236ea 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
236eb 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73    int argc, .  s
236ec 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
236ed 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75  rgv.){.  const u
236ee 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41  nsigned char *zA
236ef 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 73 63  , *zB;.  int esc
236f0 61 70 65 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  ape = 0;.  sqlit
236f1 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
236f2 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
236f3 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20  le(context);..  
236f4 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  zB = sqlite3_val
236f5 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
236f6 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 65 33  ;.  zA = sqlite3
236f7 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
236f8 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69  [1]);..  /* Limi
236f9 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  t the length of 
236fa 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  the LIKE or GLOB
236fb 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 6f 69   pattern to avoi
236fc 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20  d problems.  ** 
236fd 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 69 6f  of deep recursio
236fe 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69  n and N*N behavi
236ff 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d  or in patternCom
23700 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 69  pare()..  */.  i
23701 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
23702 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20  _bytes(argv[0]) 
23703 3e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c  >.        db->aL
23704 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
23705 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  T_LIKE_PATTERN_L
23706 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71  ENGTH] ){.    sq
23707 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
23708 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b  or(context, "LIK
23709 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72  E or GLOB patter
2370a 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20  n too complex", 
2370b 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  -1);.    return;
2370c 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a  .  }.  assert( z
2370d 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  B==sqlite3_value
2370e 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29  _text(argv[0]) )
2370f 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 64  ;  /* Encoding d
23710 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 20 2a 2f  id not change */
23711 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20  ..  if( argc==3 
23712 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73  ){.    /* The es
23713 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20 73  cape character s
23714 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69  tring must consi
23715 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55  st of a single U
23716 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a  TF-8 character..
23717 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65      ** Otherwise
23718 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
23719 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f  r..    */.    co
2371a 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2371b 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69 74 65  r *zEsc = sqlite
2371c 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2371d 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a  v[2]);.    if( z
2371e 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Esc==0 ) return;
2371f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
23720 55 74 66 38 43 68 61 72 4c 65 6e 28 28 63 68 61  Utf8CharLen((cha
23721 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d 31 20  r*)zEsc, -1)!=1 
23722 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
23723 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
23724 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20  ntext, .        
23725 20 20 22 45 53 43 41 50 45 20 65 78 70 72 65 73    "ESCAPE expres
23726 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 20 73  sion must be a s
23727 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 22  ingle character"
23728 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74  , -1);.      ret
23729 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65  urn;.    }.    e
2372a 73 63 61 70 65 20 3d 20 73 71 6c 69 74 65 33 55  scape = sqlite3U
2372b 74 66 38 52 65 61 64 28 7a 45 73 63 2c 20 30 2c  tf8Read(zEsc, 0,
2372c 20 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20 20 69   &zEsc);.  }.  i
2372d 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20  f( zA && zB ){. 
2372e 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72     struct compar
2372f 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 73  eInfo *pInfo = s
23730 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
23731 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66 64 65  (context);.#ifde
23732 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
23733 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63    sqlite3_like_c
23734 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
23735 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f     .    sqlite3_
23736 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65  result_int(conte
23737 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d 70 61  xt, patternCompa
23738 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e 66 6f  re(zB, zA, pInfo
23739 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20 7d 0a  , escape));.  }.
2373a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
2373b 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4e  ntation of the N
2373c 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e 63 74  ULLIF(x,y) funct
2373d 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74  ion.  The result
2373e 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   is the first.**
2373f 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 68 65   argument if the
23740 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 64   arguments are d
23741 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65 20 72  ifferent.  The r
23742 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 69 66  esult is NULL if
23743 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74   the.** argument
23744 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f 20 65  s are equal to e
23745 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 73 74  ach other..*/.st
23746 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c 69 66  atic void nullif
23747 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
23748 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
23749 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
2374a 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2374b 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43 6f 6c   **argv.){.  Col
2374c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71  lSeq *pColl = sq
2374d 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c  lite3GetFuncColl
2374e 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  Seq(context);.  
2374f 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
23750 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28  (NotUsed);.  if(
23751 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61   sqlite3MemCompa
23752 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72 67 76  re(argv[0], argv
23753 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 20 29  [1], pColl)!=0 )
23754 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
23755 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
23756 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20  xt, argv[0]);.  
23757 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
23758 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
23759 20 56 45 52 53 49 4f 4e 28 2a 29 20 66 75 6e 63   VERSION(*) func
2375a 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c  tion.  The resul
2375b 74 20 69 73 20 74 68 65 20 76 65 72 73 69 6f 6e  t is the version
2375c 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 69 74  .** of the SQLit
2375d 65 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69  e library that i
2375e 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  s running..*/.st
2375f 61 74 69 63 20 76 6f 69 64 20 76 65 72 73 69 6f  atic void versio
23760 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  nFunc(.  sqlite3
23761 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23762 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
23763 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
23764 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a  e **NotUsed2.){.
23765 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
23766 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
23767 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65  Used2);.  sqlite
23768 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
23769 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76  ntext, sqlite3_v
2376a 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53 51 4c 49  ersion, -1, SQLI
2376b 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f  TE_STATIC);.}../
2376c 2a 20 41 72 72 61 79 20 66 6f 72 20 63 6f 6e 76  * Array for conv
2376d 65 72 74 69 6e 67 20 66 72 6f 6d 20 68 61 6c 66  erting from half
2376e 2d 62 79 74 65 73 20 28 6e 79 62 62 6c 65 73 29  -bytes (nybbles)
2376f 20 69 6e 74 6f 20 41 53 43 49 49 20 68 65 78 0a   into ASCII hex.
23770 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f 0a 73 74  ** digits. */.st
23771 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
23772 68 65 78 64 69 67 69 74 73 5b 5d 20 3d 20 7b 0a  hexdigits[] = {.
23773 20 20 27 30 27 2c 20 27 31 27 2c 20 27 32 27 2c    '0', '1', '2',
23774 20 27 33 27 2c 20 27 34 27 2c 20 27 35 27 2c 20   '3', '4', '5', 
23775 27 36 27 2c 20 27 37 27 2c 0a 20 20 27 38 27 2c  '6', '7',.  '8',
23776 20 27 39 27 2c 20 27 41 27 2c 20 27 42 27 2c 20   '9', 'A', 'B', 
23777 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c 20 27  'C', 'D', 'E', '
23778 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 58  F' .};../*.** EX
23779 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 54 68 69  PERIMENTAL - Thi
2377a 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f 66 66 69  s is not an offi
2377b 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 2e 20 20  cial function.  
2377c 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d 61  The interface ma
2377d 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20 20 54 68  y.** change.  Th
2377e 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20  is function may 
2377f 64 69 73 61 70 70 65 61 72 2e 20 20 44 6f 20 6e  disappear.  Do n
23780 6f 74 20 77 72 69 74 65 20 63 6f 64 65 20 74 68  ot write code th
23781 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e  at depends.** on
23782 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
23783 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  **.** Implementa
23784 74 69 6f 6e 20 6f 66 20 74 68 65 20 51 55 4f 54  tion of the QUOT
23785 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  E() function.  T
23786 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  his function tak
23787 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 61  es a single.** a
23788 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65  rgument.  If the
23789 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 75 6d   argument is num
2378a 65 72 69 63 2c 20 74 68 65 20 72 65 74 75 72 6e  eric, the return
2378b 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73 61   value is the sa
2378c 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20 61 72 67  me as.** the arg
2378d 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 61  ument.  If the a
2378e 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c 2c  rgument is NULL,
2378f 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
23790 65 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 0a  e is the string.
23791 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f 74 68 65  ** "NULL".  Othe
23792 72 77 69 73 65 2c 20 74 68 65 20 61 72 67 75 6d  rwise, the argum
23793 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73 65 64 20  ent is enclosed 
23794 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73  in single quotes
23795 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67 6c 65 2d   with.** single-
23796 71 75 6f 74 65 20 65 73 63 61 70 65 73 2e 0a 2a  quote escapes..*
23797 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71 75  /.static void qu
23798 6f 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33 5f  oteFunc(sqlite3_
23799 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2379a 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
2379b 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2379c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ){.  assert( arg
2379d 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==1 );.  UNUSED
2379e 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
2379f 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69  ;.  switch( sqli
237a0 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
237a1 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63  rgv[0]) ){.    c
237a2 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  ase SQLITE_INTEG
237a3 45 52 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ER:.    case SQL
237a4 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20  ITE_FLOAT: {.   
237a5 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
237a6 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
237a7 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20   argv[0]);.     
237a8 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
237a9 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c    case SQLITE_BL
237aa 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72  OB: {.      char
237ab 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20 20   *zText = 0;.   
237ac 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a     char const *z
237ad 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76  Blob = sqlite3_v
237ae 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
237af 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42  ]);.      int nB
237b0 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  lob = sqlite3_va
237b1 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
237b2 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ]);.      assert
237b3 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 33  ( zBlob==sqlite3
237b4 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76  _value_blob(argv
237b5 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f 20 65 6e  [0]) ); /* No en
237b6 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f  coding change */
237b7 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20 28  .      zText = (
237b8 63 68 61 72 20 2a 29 63 6f 6e 74 65 78 74 4d 61  char *)contextMa
237b9 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 32  lloc(context, (2
237ba 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b 34 29 3b  *(i64)nBlob)+4);
237bb 20 0a 20 20 20 20 20 20 69 66 28 20 7a 54 65 78   .      if( zTex
237bc 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  t ){.        int
237bd 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   i;.        for(
237be 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b 20 69 2b  i=0; i<nBlob; i+
237bf 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54  +){.          zT
237c0 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20 3d 20 68  ext[(i*2)+2] = h
237c1 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b  exdigits[(zBlob[
237c2 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b 0a 20 20  i]>>4)&0x0F];.  
237c3 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 69          zText[(i
237c4 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64 69 67 69  *2)+3] = hexdigi
237c5 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29 26 30 78  ts[(zBlob[i])&0x
237c6 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  0F];.        }. 
237c7 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 6e 42         zText[(nB
237c8 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 5c 27 27  lob*2)+2] = '\''
237c9 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b  ;.        zText[
237ca 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 3d 20 27  (nBlob*2)+3] = '
237cb 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65  \0';.        zTe
237cc 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a 20 20 20  xt[0] = 'X';.   
237cd 20 20 20 20 20 7a 54 65 78 74 5b 31 5d 20 3d 20       zText[1] = 
237ce 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 73 71  '\'';.        sq
237cf 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
237d0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54 65 78 74  t(context, zText
237d1 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41  , -1, SQLITE_TRA
237d2 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 20  NSIENT);.       
237d3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
237d4 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ext);.      }.  
237d5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
237d6 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
237d7 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 69  _TEXT: {.      i
237d8 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20 20 75 36  nt i,j;.      u6
237d9 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  4 n;.      const
237da 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
237db 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 33 5f 76  zArg = sqlite3_v
237dc 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
237dd 5d 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  ]);.      char *
237de 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 7a 41  z;..      if( zA
237df 72 67 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  rg==0 ) return;.
237e0 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6e        for(i=0, n
237e1 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b  =0; zArg[i]; i++
237e2 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d 3d  ){ if( zArg[i]==
237e3 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d 0a 20 20  '\'' ) n++; }.  
237e4 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 78 74 4d      z = contextM
237e5 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28  alloc(context, (
237e6 28 69 36 34 29 69 29 2b 28 28 69 36 34 29 6e 29  (i64)i)+((i64)n)
237e7 2b 33 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  +3);.      if( z
237e8 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 30 5d   ){.        z[0]
237e9 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20   = '\'';.       
237ea 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31 3b 20 7a   for(i=0, j=1; z
237eb 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  Arg[i]; i++){.  
237ec 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
237ed 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20 20 20   zArg[i];.      
237ee 20 20 20 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d      if( zArg[i]=
237ef 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20  ='\'' ){.       
237f0 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c       z[j++] = '\
237f1 27 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  '';.          }.
237f2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
237f3 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b    z[j++] = '\'';
237f4 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d 20 3d 20  .        z[j] = 
237f5 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  0;.        sqlit
237f6 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
237f7 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c 20 73 71  ontext, z, j, sq
237f8 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20  lite3_free);.   
237f9 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
237fa 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
237fb 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
237fc 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 61 6c  ert( sqlite3_val
237fd 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
237fe 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 3b  ==SQLITE_NULL );
237ff 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
23800 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
23801 78 74 2c 20 22 4e 55 4c 4c 22 2c 20 34 2c 20 53  xt, "NULL", 4, S
23802 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
23803 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23804 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  }.  }.}../*.** T
23805 68 65 20 68 65 78 28 29 20 66 75 6e 63 74 69 6f  he hex() functio
23806 6e 2e 20 20 49 6e 74 65 72 70 72 65 74 20 74 68  n.  Interpret th
23807 65 20 61 72 67 75 6d 65 6e 74 20 61 73 20 61 20  e argument as a 
23808 62 6c 6f 62 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  blob.  Return.**
23809 20 61 20 68 65 78 61 64 65 63 69 6d 61 6c 20 72   a hexadecimal r
2380a 65 6e 64 65 72 69 6e 67 20 61 73 20 74 65 78 74  endering as text
2380b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2380c 20 68 65 78 46 75 6e 63 28 0a 20 20 73 71 6c 69   hexFunc(.  sqli
2380d 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2380e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
2380f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
23810 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e  e **argv.){.  in
23811 74 20 69 2c 20 6e 3b 0a 20 20 63 6f 6e 73 74 20  t i, n;.  const 
23812 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
23813 42 6c 6f 62 3b 0a 20 20 63 68 61 72 20 2a 7a 48  Blob;.  char *zH
23814 65 78 2c 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74  ex, *z;.  assert
23815 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55  ( argc==1 );.  U
23816 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
23817 61 72 67 63 29 3b 0a 20 20 70 42 6c 6f 62 20 3d  argc);.  pBlob =
23818 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
23819 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  lob(argv[0]);.  
2381a 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
2381b 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
2381c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 6c 6f  ;.  assert( pBlo
2381d 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  b==sqlite3_value
2381e 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20 29  _blob(argv[0]) )
2381f 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e  ;  /* No encodin
23820 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 20  g change */.  z 
23821 3d 20 7a 48 65 78 20 3d 20 63 6f 6e 74 65 78 74  = zHex = context
23822 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
23823 28 28 69 36 34 29 6e 29 2a 32 20 2b 20 31 29 3b  ((i64)n)*2 + 1);
23824 0a 20 20 69 66 28 20 7a 48 65 78 20 29 7b 0a 20  .  if( zHex ){. 
23825 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b     for(i=0; i<n;
23826 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b 29 7b 0a   i++, pBlob++){.
23827 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63        unsigned c
23828 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f 62 3b 0a  har c = *pBlob;.
23829 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68        *(z++) = h
2382a 65 78 64 69 67 69 74 73 5b 28 63 3e 3e 34 29 26  exdigits[(c>>4)&
2382b 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a 28 7a 2b  0xf];.      *(z+
2382c 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b 63  +) = hexdigits[c
2382d 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  &0xf];.    }.   
2382e 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c   *z = 0;.    sql
2382f 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
23830 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65 78 2c 20  (context, zHex, 
23831 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  n*2, sqlite3_fre
23832 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
23833 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62 28 4e 29   The zeroblob(N)
23834 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
23835 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  s a zero-filled 
23836 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20 4e 20 62  blob of size N b
23837 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ytes..*/.static 
23838 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62 46 75 6e  void zeroblobFun
23839 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
2383a 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
2383b 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
2383c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
2383d 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b 0a 20 20  v.){.  i64 n;.  
2383e 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
2383f 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
23840 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e  METER(argc);.  n
23841 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
23842 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b  _int64(argv[0]);
23843 0a 20 20 69 66 28 20 6e 3e 53 51 4c 49 54 45 5f  .  if( n>SQLITE_
23844 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20  MAX_LENGTH ){.  
23845 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23846 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f  _error_toobig(co
23847 6e 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  ntext);.  }else{
23848 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
23849 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e  ult_zeroblob(con
2384a 74 65 78 74 2c 20 28 69 6e 74 29 6e 29 3b 0a 20  text, (int)n);. 
2384b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
2384c 72 65 70 6c 61 63 65 28 29 20 66 75 6e 63 74 69  replace() functi
2384d 6f 6e 2e 20 20 54 68 72 65 65 20 61 72 67 75 6d  on.  Three argum
2384e 65 6e 74 73 20 61 72 65 20 61 6c 6c 20 73 74 72  ents are all str
2384f 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20 74 68  ings: call.** th
23850 65 6d 20 41 2c 20 42 2c 20 61 6e 64 20 43 2e 20  em A, B, and C. 
23851 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c  The result is al
23852 73 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69 63  so a string whic
23853 68 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20  h is derived.** 
23854 66 72 6f 6d 20 41 20 62 79 20 72 65 70 6c 61 63  from A by replac
23855 69 6e 67 20 65 76 65 72 79 20 6f 63 63 75 72 61  ing every occura
23856 6e 63 65 20 6f 66 20 42 20 77 69 74 68 20 43 2e  nce of B with C.
23857 20 20 54 68 65 20 6d 61 74 63 68 0a 2a 2a 20 6d    The match.** m
23858 75 73 74 20 62 65 20 65 78 61 63 74 2e 20 20 43  ust be exact.  C
23859 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2385a 65 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e  es are not used.
2385b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2385c 72 65 70 6c 61 63 65 46 75 6e 63 28 0a 20 20 73  replaceFunc(.  s
2385d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
2385e 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
2385f 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
23860 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
23861 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
23862 63 68 61 72 20 2a 7a 53 74 72 3b 20 20 20 20 20  char *zStr;     
23863 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 20     /* The input 
23864 73 74 72 69 6e 67 20 41 20 2a 2f 0a 20 20 63 6f  string A */.  co
23865 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
23866 72 20 2a 7a 50 61 74 74 65 72 6e 3b 20 20 20 20  r *zPattern;    
23867 2f 2a 20 54 68 65 20 70 61 74 74 65 72 6e 20 73  /* The pattern s
23868 74 72 69 6e 67 20 42 20 2a 2f 0a 20 20 63 6f 6e  tring B */.  con
23869 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2386a 20 2a 7a 52 65 70 3b 20 20 20 20 20 20 20 20 2f   *zRep;        /
2386b 2a 20 54 68 65 20 72 65 70 6c 61 63 65 6d 65 6e  * The replacemen
2386c 74 20 73 74 72 69 6e 67 20 43 20 2a 2f 0a 20 20  t string C */.  
2386d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2386e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Out;            
2386f 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20    /* The output 
23870 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 72 3b 20 20  */.  int nStr;  
23871 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23872 20 53 69 7a 65 20 6f 66 20 7a 53 74 72 20 2a 2f   Size of zStr */
23873 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72 6e 3b  .  int nPattern;
23874 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
23875 69 7a 65 20 6f 66 20 7a 50 61 74 74 65 72 6e 20  ize of zPattern 
23876 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 70 3b 20 20  */.  int nRep;  
23877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23878 20 53 69 7a 65 20 6f 66 20 7a 52 65 70 20 2a 2f   Size of zRep */
23879 0a 20 20 69 36 34 20 6e 4f 75 74 3b 20 20 20 20  .  i64 nOut;    
2387a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
2387b 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 7a  aximum size of z
2387c 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 6f  Out */.  int loo
2387d 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20  pLimit;         
2387e 20 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72 5b 5d    /* Last zStr[]
2387f 20 74 68 61 74 20 6d 69 67 68 74 20 6d 61 74 63   that might matc
23880 68 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a 2f 0a  h zPattern[] */.
23881 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
23882 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
23883 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 0a  op counters */..
23884 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
23885 33 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  3 );.  UNUSED_PA
23886 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
23887 20 7a 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f   zStr = sqlite3_
23888 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
23889 30 5d 29 3b 0a 20 20 69 66 28 20 7a 53 74 72 3d  0]);.  if( zStr=
2388a 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e  =0 ) return;.  n
2388b 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Str = sqlite3_va
2388c 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
2388d 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53  ]);.  assert( zS
2388e 74 72 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  tr==sqlite3_valu
2388f 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20  e_text(argv[0]) 
23890 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69  );  /* No encodi
23891 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a  ng change */.  z
23892 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65  Pattern = sqlite
23893 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
23894 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 50 61  v[1]);.  if( zPa
23895 74 74 65 72 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  ttern==0 ){.    
23896 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
23897 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
23898 31 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  1])==SQLITE_NULL
23899 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
2389a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
2389b 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
2389c 74 29 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  t)->mallocFailed
2389d 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   );.    return;.
2389e 20 20 7d 0a 20 20 69 66 28 20 7a 50 61 74 74 65    }.  if( zPatte
2389f 72 6e 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  rn[0]==0 ){.    
238a0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
238a1 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
238a2 31 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  1])!=SQLITE_NULL
238a3 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   );.    sqlite3_
238a4 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e  result_value(con
238a5 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a  text, argv[0]);.
238a6 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
238a7 20 20 6e 50 61 74 74 65 72 6e 20 3d 20 73 71 6c    nPattern = sql
238a8 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
238a9 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73  (argv[1]);.  ass
238aa 65 72 74 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73  ert( zPattern==s
238ab 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
238ac 74 28 61 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f  t(argv[1]) );  /
238ad 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68  * No encoding ch
238ae 61 6e 67 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d  ange */.  zRep =
238af 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
238b0 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  ext(argv[2]);.  
238b1 69 66 28 20 7a 52 65 70 3d 3d 30 20 29 20 72 65  if( zRep==0 ) re
238b2 74 75 72 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73  turn;.  nRep = s
238b3 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
238b4 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61  es(argv[2]);.  a
238b5 73 73 65 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c  ssert( zRep==sql
238b6 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
238b7 61 72 67 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f  argv[2]) );.  nO
238b8 75 74 20 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20  ut = nStr + 1;. 
238b9 20 61 73 73 65 72 74 28 20 6e 4f 75 74 3c 53 51   assert( nOut<SQ
238ba 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20  LITE_MAX_LENGTH 
238bb 29 3b 0a 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74  );.  zOut = cont
238bc 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
238bd 74 2c 20 28 69 36 34 29 6e 4f 75 74 29 3b 0a 20  t, (i64)nOut);. 
238be 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a   if( zOut==0 ){.
238bf 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
238c0 20 20 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53    loopLimit = nS
238c1 74 72 20 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20  tr - nPattern;  
238c2 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c  .  for(i=j=0; i<
238c3 3d 6c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29  =loopLimit; i++)
238c4 7b 0a 20 20 20 20 69 66 28 20 7a 53 74 72 5b 69  {.    if( zStr[i
238c5 5d 21 3d 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c  ]!=zPattern[0] |
238c6 7c 20 6d 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69  | memcmp(&zStr[i
238c7 5d 2c 20 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61  ], zPattern, nPa
238c8 74 74 65 72 6e 29 20 29 7b 0a 20 20 20 20 20 20  ttern) ){.      
238c9 7a 4f 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72  zOut[j++] = zStr
238ca 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  [i];.    }else{.
238cb 20 20 20 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a        u8 *zOld;.
238cc 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64        sqlite3 *d
238cd 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
238ce 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
238cf 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 6e 4f  ntext);.      nO
238d0 75 74 20 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61  ut += nRep - nPa
238d1 74 74 65 72 6e 3b 0a 20 20 20 20 20 20 69 66 28  ttern;.      if(
238d2 20 6e 4f 75 74 3e 3d 64 62 2d 3e 61 4c 69 6d 69   nOut>=db->aLimi
238d3 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
238d4 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20  ENGTH] ){.      
238d5 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
238d6 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f  _error_toobig(co
238d7 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20  ntext);.        
238d8 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
238d9 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 20 20 20  , zOut);.       
238da 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
238db 0a 20 20 20 20 20 20 7a 4f 6c 64 20 3d 20 7a 4f  .      zOld = zO
238dc 75 74 3b 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d  ut;.      zOut =
238dd 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
238de 28 7a 4f 75 74 2c 20 28 69 6e 74 29 6e 4f 75 74  (zOut, (int)nOut
238df 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4f 75  );.      if( zOu
238e0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
238e1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
238e2 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
238e3 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  xt);.        sql
238e4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
238e5 4f 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 72 65  Old);.        re
238e6 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
238e7 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74      memcpy(&zOut
238e8 5b 6a 5d 2c 20 7a 52 65 70 2c 20 6e 52 65 70 29  [j], zRep, nRep)
238e9 3b 0a 20 20 20 20 20 20 6a 20 2b 3d 20 6e 52 65  ;.      j += nRe
238ea 70 3b 0a 20 20 20 20 20 20 69 20 2b 3d 20 6e 50  p;.      i += nP
238eb 61 74 74 65 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a  attern-1;.    }.
238ec 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 2b    }.  assert( j+
238ed 6e 53 74 72 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29  nStr-i+1==nOut )
238ee 3b 0a 20 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74  ;.  memcpy(&zOut
238ef 5b 6a 5d 2c 20 26 7a 53 74 72 5b 69 5d 2c 20 6e  [j], &zStr[i], n
238f0 53 74 72 2d 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e  Str-i);.  j += n
238f1 53 74 72 20 2d 20 69 3b 0a 20 20 61 73 73 65 72  Str - i;.  asser
238f2 74 28 20 6a 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20  t( j<=nOut );.  
238f3 7a 4f 75 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73  zOut[j] = 0;.  s
238f4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
238f5 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  xt(context, (cha
238f6 72 2a 29 7a 4f 75 74 2c 20 6a 2c 20 73 71 6c 69  r*)zOut, j, sqli
238f7 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a  te3_free);.}../*
238f8 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
238f9 6f 6e 20 6f 66 20 74 68 65 20 54 52 49 4d 28 29  on of the TRIM()
238fa 2c 20 4c 54 52 49 4d 28 29 2c 20 61 6e 64 20 52  , LTRIM(), and R
238fb 54 52 49 4d 28 29 20 66 75 6e 63 74 69 6f 6e 73  TRIM() functions
238fc 2e 0a 2a 2a 20 54 68 65 20 75 73 65 72 64 61 74  ..** The userdat
238fd 61 20 69 73 20 30 78 31 20 66 6f 72 20 6c 65 66  a is 0x1 for lef
238fe 74 20 74 72 69 6d 2c 20 30 78 32 20 66 6f 72 20  t trim, 0x2 for 
238ff 72 69 67 68 74 20 74 72 69 6d 2c 20 30 78 33 20  right trim, 0x3 
23900 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61  for both..*/.sta
23901 74 69 63 20 76 6f 69 64 20 74 72 69 6d 46 75 6e  tic void trimFun
23902 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
23903 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
23904 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
23905 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
23906 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  v.){.  const uns
23907 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b  igned char *zIn;
23908 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
23909 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 6f  t string */.  co
2390a 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2390b 72 20 2a 7a 43 68 61 72 53 65 74 3b 20 20 20 20  r *zCharSet;    
2390c 2f 2a 20 53 65 74 20 6f 66 20 63 68 61 72 61 63  /* Set of charac
2390d 74 65 72 73 20 74 6f 20 74 72 69 6d 20 2a 2f 0a  ters to trim */.
2390e 20 20 69 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20    int nIn;      
2390f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23910 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
23911 20 62 79 74 65 73 20 69 6e 20 69 6e 70 75 74 20   bytes in input 
23912 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20  */.  int flags; 
23913 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23914 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 74 72 69         /* 1: tri
23915 6d 6c 65 66 74 20 20 32 3a 20 74 72 69 6d 72 69  mleft  2: trimri
23916 67 68 74 20 20 33 3a 20 74 72 69 6d 20 2a 2f 0a  ght  3: trim */.
23917 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
23918 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23919 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2391a 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ter */.  unsigne
2391b 64 20 63 68 61 72 20 2a 61 4c 65 6e 20 3d 20 30  d char *aLen = 0
2391c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  ;          /* Le
2391d 6e 67 74 68 20 6f 66 20 65 61 63 68 20 63 68 61  ngth of each cha
2391e 72 61 63 74 65 72 20 69 6e 20 7a 43 68 61 72 53  racter in zCharS
2391f 65 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  et */.  unsigned
23920 20 63 68 61 72 20 2a 2a 61 7a 43 68 61 72 20 3d   char **azChar =
23921 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64   0;       /* Ind
23922 69 76 69 64 75 61 6c 20 63 68 61 72 61 63 74 65  ividual characte
23923 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  rs in zCharSet *
23924 2f 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 20 20  /.  int nChar;  
23925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23926 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
23927 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
23928 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 0a 20 20   zCharSet */..  
23929 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
2392a 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d  e_type(argv[0])=
2392b 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a  =SQLITE_NULL ){.
2392c 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2392d 20 20 7a 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f    zIn = sqlite3_
2392e 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
2392f 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d  0]);.  if( zIn==
23930 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 49  0 ) return;.  nI
23931 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
23932 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
23933 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 49 6e 3d  ;.  assert( zIn=
23934 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
23935 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a  ext(argv[0]) );.
23936 20 20 69 66 28 20 61 72 67 63 3d 3d 31 20 29 7b    if( argc==1 ){
23937 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
23938 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
23939 6c 65 6e 4f 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d  lenOne[] = { 1 }
2393a 3b 0a 20 20 20 20 73 74 61 74 69 63 20 75 6e 73  ;.    static uns
2393b 69 67 6e 65 64 20 63 68 61 72 20 2a 20 63 6f 6e  igned char * con
2393c 73 74 20 61 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28  st azOne[] = { (
2393d 75 38 2a 29 22 20 22 20 7d 3b 0a 20 20 20 20 6e  u8*)" " };.    n
2393e 43 68 61 72 20 3d 20 31 3b 0a 20 20 20 20 61 4c  Char = 1;.    aL
2393f 65 6e 20 3d 20 28 75 38 2a 29 6c 65 6e 4f 6e 65  en = (u8*)lenOne
23940 3b 0a 20 20 20 20 61 7a 43 68 61 72 20 3d 20 28  ;.    azChar = (
23941 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a  unsigned char **
23942 29 61 7a 4f 6e 65 3b 0a 20 20 20 20 7a 43 68 61  )azOne;.    zCha
23943 72 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  rSet = 0;.  }els
23944 65 20 69 66 28 20 28 7a 43 68 61 72 53 65 74 20  e if( (zCharSet 
23945 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
23946 74 65 78 74 28 61 72 67 76 5b 31 5d 29 29 3d 3d  text(argv[1]))==
23947 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  0 ){.    return;
23948 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f  .  }else{.    co
23949 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2394a 72 20 2a 7a 3b 0a 20 20 20 20 66 6f 72 28 7a 3d  r *z;.    for(z=
2394b 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d  zCharSet, nChar=
2394c 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b  0; *z; nChar++){
2394d 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b  .      SQLITE_SK
2394e 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20  IP_UTF8(z);.    
2394f 7d 0a 20 20 20 20 69 66 28 20 6e 43 68 61 72 3e  }.    if( nChar>
23950 30 20 29 7b 0a 20 20 20 20 20 20 61 7a 43 68 61  0 ){.      azCha
23951 72 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  r = contextMallo
23952 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
23953 29 6e 43 68 61 72 29 2a 28 73 69 7a 65 6f 66 28  )nChar)*(sizeof(
23954 63 68 61 72 2a 29 2b 31 29 29 3b 0a 20 20 20 20  char*)+1));.    
23955 20 20 69 66 28 20 61 7a 43 68 61 72 3d 3d 30 20    if( azChar==0 
23956 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
23957 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
23958 20 61 4c 65 6e 20 3d 20 28 75 6e 73 69 67 6e 65   aLen = (unsigne
23959 64 20 63 68 61 72 2a 29 26 61 7a 43 68 61 72 5b  d char*)&azChar[
2395a 6e 43 68 61 72 5d 3b 0a 20 20 20 20 20 20 66 6f  nChar];.      fo
2395b 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43  r(z=zCharSet, nC
2395c 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72  har=0; *z; nChar
2395d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 7a 43  ++){.        azC
2395e 68 61 72 5b 6e 43 68 61 72 5d 20 3d 20 28 75 6e  har[nChar] = (un
2395f 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 3b  signed char *)z;
23960 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  .        SQLITE_
23961 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20  SKIP_UTF8(z);.  
23962 20 20 20 20 20 20 61 4c 65 6e 5b 6e 43 68 61 72        aLen[nChar
23963 5d 20 3d 20 28 75 38 29 28 7a 20 2d 20 61 7a 43  ] = (u8)(z - azC
23964 68 61 72 5b 6e 43 68 61 72 5d 29 3b 0a 20 20 20  har[nChar]);.   
23965 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
23966 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a   if( nChar>0 ){.
23967 20 20 20 20 66 6c 61 67 73 20 3d 20 53 51 4c 49      flags = SQLI
23968 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 73 71  TE_PTR_TO_INT(sq
23969 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
2396a 63 6f 6e 74 65 78 74 29 29 3b 0a 20 20 20 20 69  context));.    i
2396b 66 28 20 66 6c 61 67 73 20 26 20 31 20 29 7b 0a  f( flags & 1 ){.
2396c 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e        while( nIn
2396d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  >0 ){.        in
2396e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  t len = 0;.     
2396f 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
23970 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  har; i++){.     
23971 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b       len = aLen[
23972 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  i];.          if
23973 28 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 61 7a  ( memcmp(zIn, az
23974 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d 3d 30  Char[i], len)==0
23975 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
23976 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
23977 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b  i>=nChar ) break
23978 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20 2b 3d  ;.        zIn +=
23979 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6e 49   len;.        nI
2397a 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20  n -= len;.      
2397b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
2397c 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20 20 20  flags & 2 ){.   
2397d 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 20     while( nIn>0 
2397e 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c  ){.        int l
2397f 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  en = 0;.        
23980 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72  for(i=0; i<nChar
23981 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
23982 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b    len = aLen[i];
23983 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c  .          if( l
23984 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d  en<=nIn && memcm
23985 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c  p(&zIn[nIn-len],
23986 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d  azChar[i],len)==
23987 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
23988 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
23989 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61   i>=nChar ) brea
2398a 6b 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d  k;.        nIn -
2398b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  = len;.      }. 
2398c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 68     }.    if( zCh
2398d 61 72 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73  arSet ){.      s
2398e 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 68  qlite3_free(azCh
2398f 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ar);.    }.  }. 
23990 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
23991 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63  text(context, (c
23992 68 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53  har*)zIn, nIn, S
23993 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
23994 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  ;.}...#ifdef SQL
23995 49 54 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a  ITE_SOUNDEX./*.*
23996 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f  * Compute the so
23997 75 6e 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f  undex encoding o
23998 66 20 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61  f a word..*/.sta
23999 74 69 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 78  tic void soundex
2399a 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
2399b 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2399c 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
2399d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2399e 61 72 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 7a  argv.){.  char z
2399f 52 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e  Result[8];.  con
239a0 73 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e  st u8 *zIn;.  in
239a1 74 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 63  t i, j;.  static
239a2 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
239a3 63 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 7b  char iCode[] = {
239a4 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c  .    0, 0, 0, 0,
239a5 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
239a6 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
239a7 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30  , 0,.    0, 0, 0
239a8 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
239a9 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
239aa 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20  0, 0, 0,.    0, 
239ab 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
239ac 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
239ad 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20   0, 0, 0, 0,.   
239ae 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
239af 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
239b0 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
239b1 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c  .    0, 0, 1, 2,
239b2 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20   3, 0, 1, 2, 0, 
239b3 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35  0, 2, 2, 4, 5, 5
239b4 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36  , 0,.    1, 2, 6
239b5 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c  , 2, 3, 0, 1, 0,
239b6 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20   2, 0, 2, 0, 0, 
239b7 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20  0, 0, 0,.    0, 
239b8 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31  0, 1, 2, 3, 0, 1
239b9 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c  , 2, 0, 0, 2, 2,
239ba 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20   4, 5, 5, 0,.   
239bb 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20   1, 2, 6, 2, 3, 
239bc 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32  0, 1, 0, 2, 0, 2
239bd 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
239be 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20  .  };.  assert( 
239bf 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e  argc==1 );.  zIn
239c0 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 5f   = (u8*)sqlite3_
239c1 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
239c2 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d  0]);.  if( zIn==
239c3 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 22  0 ) zIn = (u8*)"
239c4 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 49  ";.  for(i=0; zI
239c5 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 74 65 33  n[i] && !sqlite3
239c6 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29 3b  Isalpha(zIn[i]);
239c7 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a 49   i++){}.  if( zI
239c8 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38 20 70  n[i] ){.    u8 p
239c9 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b  revcode = iCode[
239ca 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20  zIn[i]&0x7f];.  
239cb 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 73    zResult[0] = s
239cc 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 7a 49  qlite3Toupper(zI
239cd 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28 6a  n[i]);.    for(j
239ce 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b 69  =1; j<4 && zIn[i
239cf 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ]; i++){.      i
239d0 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b  nt code = iCode[
239d1 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20  zIn[i]&0x7f];.  
239d2 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20 29      if( code>0 )
239d3 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f  {.        if( co
239d4 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b 0a  de!=prevcode ){.
239d5 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63 6f            prevco
239d6 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20 20  de = code;.     
239d7 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b       zResult[j++
239d8 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b 0a  ] = code + '0';.
239d9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
239da 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
239db 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20 20  revcode = 0;.   
239dc 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77     }.    }.    w
239dd 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20 20  hile( j<4 ){.   
239de 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20     zResult[j++] 
239df 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20  = '0';.    }.   
239e0 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30 3b   zResult[j] = 0;
239e1 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
239e2 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
239e3 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53 51  , zResult, 4, SQ
239e4 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
239e5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
239e6 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
239e7 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30 30  t(context, "?000
239e8 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41  ", 4, SQLITE_STA
239e9 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  TIC);.  }.}.#end
239ea 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
239eb 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
239ec 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66  ENSION./*.** A f
239ed 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61  unction that loa
239ee 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69 62 72  ds a shared-libr
239ef 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20 74 68  ary extension th
239f0 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  en returns NULL.
239f1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
239f2 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65 33 5f  loadExt(sqlite3_
239f3 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
239f4 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
239f5 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
239f6 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
239f7 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20  *zFile = (const 
239f8 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
239f9 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
239fa 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ]);.  const char
239fb 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69 74   *zProc;.  sqlit
239fc 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
239fd 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
239fe 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63  le(context);.  c
239ff 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30  har *zErrMsg = 0
23a00 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32  ;..  if( argc==2
23a01 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20   ){.    zProc = 
23a02 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
23a03 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
23a04 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c  (argv[1]);.  }el
23a05 73 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20  se{.    zProc = 
23a06 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46 69  0;.  }.  if( zFi
23a07 6c 65 20 26 26 20 73 71 6c 69 74 65 33 5f 6c 6f  le && sqlite3_lo
23a08 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62 2c  ad_extension(db,
23a09 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 26   zFile, zProc, &
23a0a 7a 45 72 72 4d 73 67 29 20 29 7b 0a 20 20 20 20  zErrMsg) ){.    
23a0b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
23a0c 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
23a0d 72 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20 20  rrMsg, -1);.    
23a0e 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
23a0f 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  rMsg);.  }.}.#en
23a10 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  dif.../*.** An i
23a11 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
23a12 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
23a13 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63 6f 6e  re holds the con
23a14 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75 6d  text of a.** sum
23a15 28 29 20 6f 72 20 61 76 67 28 29 20 61 67 67 72  () or avg() aggr
23a16 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74 69 6f  egate computatio
23a17 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
23a18 72 75 63 74 20 53 75 6d 43 74 78 20 53 75 6d 43  ruct SumCtx SumC
23a19 74 78 3b 0a 73 74 72 75 63 74 20 53 75 6d 43 74  tx;.struct SumCt
23a1a 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53 75  x {.  double rSu
23a1b 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61 74  m;      /* Float
23a1c 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a 2f  ing point sum */
23a1d 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 20 20 20  .  i64 iSum;    
23a1e 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20       /* Integer 
23a1f 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34 20  sum */   .  i64 
23a20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  cnt;          /*
23a21 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
23a22 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20 20  nts summed */.  
23a23 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20 20  u8 overflow;    
23a24 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74    /* True if int
23a25 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 73 65  eger overflow se
23a26 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 70 72 6f  en */.  u8 appro
23a27 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  x;        /* Tru
23a28 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65 72  e if non-integer
23a29 20 76 61 6c 75 65 20 77 61 73 20 69 6e 70 75 74   value was input
23a2a 20 74 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a 7d   to the sum */.}
23a2b 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  ;../*.** Routine
23a2c 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74  s used to comput
23a2d 65 20 74 68 65 20 73 75 6d 2c 20 61 76 65 72 61  e the sum, avera
23a2e 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a  ge, and total..*
23a2f 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29 20 66  *.** The SUM() f
23a30 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20  unction follows 
23a31 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 53 51 4c  the (broken) SQL
23a32 20 73 74 61 6e 64 61 72 64 20 77 68 69 63 68 20   standard which 
23a33 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 74  means.** that it
23a34 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66   returns NULL if
23a35 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20 6e 6f   it sums over no
23a36 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c 20   inputs.  TOTAL 
23a37 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20 69  returns.** 0.0 i
23a38 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20 49 6e  n that case.  In
23a39 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41 4c   addition, TOTAL
23a3a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
23a3b 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a 2a  a float where.**
23a3c 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74 75 72   SUM might retur
23a3d 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66 20  n an integer if 
23a3e 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e 74  it never encount
23a3f 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70  ers a floating p
23a40 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20  oint.** value.  
23a41 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 61 69 6c  TOTAL never fail
23a42 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 67 68 74  s, but SUM might
23a43 20 74 68 72 6f 75 67 68 20 61 6e 20 65 78 63 65   through an exce
23a44 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20 6f  ption if.** it o
23a45 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74 65  verflows an inte
23a46 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ger..*/.static v
23a47 6f 69 64 20 73 75 6d 53 74 65 70 28 73 71 6c 69  oid sumStep(sqli
23a48 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
23a49 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
23a4a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
23a4b 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 74 78 20  argv){.  SumCtx 
23a4c 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a  *p;.  int type;.
23a4d 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
23a4e 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  1 );.  UNUSED_PA
23a4f 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
23a50 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
23a51 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
23a52 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a  ontext, sizeof(*
23a53 70 29 29 3b 0a 20 20 74 79 70 65 20 3d 20 73 71  p));.  type = sq
23a54 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
23a55 72 69 63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  ric_type(argv[0]
23a56 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 74 79  );.  if( p && ty
23a57 70 65 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  pe!=SQLITE_NULL 
23a58 29 7b 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b  ){.    p->cnt++;
23a59 0a 20 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53  .    if( type==S
23a5a 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b  QLITE_INTEGER ){
23a5b 0a 20 20 20 20 20 20 69 36 34 20 76 20 3d 20 73  .      i64 v = s
23a5c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
23a5d 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  64(argv[0]);.   
23a5e 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b     p->rSum += v;
23a5f 0a 20 20 20 20 20 20 69 66 28 20 28 70 2d 3e 61  .      if( (p->a
23a60 70 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f  pprox|p->overflo
23a61 77 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  w)==0 ){.       
23a62 20 69 36 34 20 69 4e 65 77 53 75 6d 20 3d 20 70   i64 iNewSum = p
23a63 2d 3e 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20 20  ->iSum + v;.    
23a64 20 20 20 20 69 6e 74 20 73 31 20 3d 20 28 69 6e      int s1 = (in
23a65 74 29 28 70 2d 3e 69 53 75 6d 20 3e 3e 20 28 73  t)(p->iSum >> (s
23a66 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 29  izeof(i64)*8-1))
23a67 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 32  ;.        int s2
23a68 20 3d 20 28 69 6e 74 29 28 76 20 20 20 20 20 20   = (int)(v      
23a69 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34 29   >> (sizeof(i64)
23a6a 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20  *8-1));.        
23a6b 69 6e 74 20 73 33 20 3d 20 28 69 6e 74 29 28 69  int s3 = (int)(i
23a6c 4e 65 77 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f  NewSum >> (sizeo
23a6d 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20 20  f(i64)*8-1));.  
23a6e 20 20 20 20 20 20 70 2d 3e 6f 76 65 72 66 6c 6f        p->overflo
23a6f 77 20 3d 20 28 28 73 31 26 73 32 26 7e 73 33 29  w = ((s1&s2&~s3)
23a70 20 7c 20 28 7e 73 31 26 7e 73 32 26 73 33 29 29   | (~s1&~s2&s3))
23a71 3f 31 3a 30 3b 0a 20 20 20 20 20 20 20 20 70 2d  ?1:0;.        p-
23a72 3e 69 53 75 6d 20 3d 20 69 4e 65 77 53 75 6d 3b  >iSum = iNewSum;
23a73 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
23a74 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75  se{.      p->rSu
23a75 6d 20 2b 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  m += sqlite3_val
23a76 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30  ue_double(argv[0
23a77 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 70  ]);.      p->app
23a78 72 6f 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  rox = 1;.    }. 
23a79 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
23a7a 20 73 75 6d 46 69 6e 61 6c 69 7a 65 28 73 71 6c   sumFinalize(sql
23a7b 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
23a7c 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78  ntext){.  SumCtx
23a7d 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74   *p;.  p = sqlit
23a7e 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
23a7f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29  text(context, 0)
23a80 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e  ;.  if( p && p->
23a81 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28  cnt>0 ){.    if(
23a82 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 29 7b 0a   p->overflow ){.
23a83 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
23a84 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
23a85 78 74 2c 22 69 6e 74 65 67 65 72 20 6f 76 65 72  xt,"integer over
23a86 66 6c 6f 77 22 2c 2d 31 29 3b 0a 20 20 20 20 7d  flow",-1);.    }
23a87 65 6c 73 65 20 69 66 28 20 70 2d 3e 61 70 70 72  else if( p->appr
23a88 6f 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ox ){.      sqli
23a89 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
23a8a 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53  e(context, p->rS
23a8b 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  um);.    }else{.
23a8c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
23a8d 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
23a8e 78 74 2c 20 70 2d 3e 69 53 75 6d 29 3b 0a 20 20  xt, p->iSum);.  
23a8f 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63    }.  }.}.static
23a90 20 76 6f 69 64 20 61 76 67 46 69 6e 61 6c 69 7a   void avgFinaliz
23a91 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
23a92 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53  t *context){.  S
23a93 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20  umCtx *p;.  p = 
23a94 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
23a95 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
23a96 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26  t, 0);.  if( p &
23a97 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20  & p->cnt>0 ){.  
23a98 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23a99 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c  _double(context,
23a9a 20 70 2d 3e 72 53 75 6d 2f 28 64 6f 75 62 6c 65   p->rSum/(double
23a9b 29 70 2d 3e 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a  )p->cnt);.  }.}.
23a9c 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61  static void tota
23a9d 6c 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65  lFinalize(sqlite
23a9e 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
23a9f 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70  xt){.  SumCtx *p
23aa0 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  p = sqlite3_
23aa1 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
23aa2 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20  t(context, 0);. 
23aa3 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e   /* (double)0 In
23aa4 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f   case of SQLITE_
23aa5 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
23aa6 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 73 71 6c 69  INT... */.  sqli
23aa7 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
23aa8 65 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70  e(context, p ? p
23aa9 2d 3e 72 53 75 6d 20 3a 20 28 64 6f 75 62 6c 65  ->rSum : (double
23aaa 29 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  )0);.}../*.** Th
23aab 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
23aac 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63  cture keeps trac
23aad 6b 20 6f 66 20 73 74 61 74 65 20 69 6e 66 6f 72  k of state infor
23aae 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 0a 2a  mation for the.*
23aaf 2a 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65 67  * count() aggreg
23ab0 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  ate function..*/
23ab1 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
23ab2 43 6f 75 6e 74 43 74 78 20 43 6f 75 6e 74 43 74  CountCtx CountCt
23ab3 78 3b 0a 73 74 72 75 63 74 20 43 6f 75 6e 74 43  x;.struct CountC
23ab4 74 78 20 7b 0a 20 20 69 36 34 20 6e 3b 0a 7d 3b  tx {.  i64 n;.};
23ab5 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73  ../*.** Routines
23ab6 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
23ab7 65 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65 67  e count() aggreg
23ab8 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  ate function..*/
23ab9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 75  .static void cou
23aba 6e 74 53 74 65 70 28 73 71 6c 69 74 65 33 5f 63  ntStep(sqlite3_c
23abb 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
23abc 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
23abd 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
23abe 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b  {.  CountCtx *p;
23abf 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61  .  p = sqlite3_a
23ac0 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
23ac1 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66  (context, sizeof
23ac2 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 28 61 72  (*p));.  if( (ar
23ac3 67 63 3d 3d 30 20 7c 7c 20 53 51 4c 49 54 45 5f  gc==0 || SQLITE_
23ac4 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61  NULL!=sqlite3_va
23ac5 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
23ac6 29 29 20 26 26 20 70 20 29 7b 0a 20 20 20 20 70  )) && p ){.    p
23ac7 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 7d 20 20 20 0a  ->n++;.  }.}   .
23ac8 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 75 6e  static void coun
23ac9 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65  tFinalize(sqlite
23aca 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
23acb 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20  xt){.  CountCtx 
23acc 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  *p;.  p = sqlite
23acd 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
23ace 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  ext(context, 0);
23acf 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
23ad0 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c  t_int64(context,
23ad1 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30 29 3b 0a   p ? p->n : 0);.
23ad2 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  }../*.** Routine
23ad3 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 6d  s to implement m
23ad4 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 61  in() and max() a
23ad5 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
23ad6 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ns..*/.static vo
23ad7 69 64 20 6d 69 6e 6d 61 78 53 74 65 70 28 0a 20  id minmaxStep(. 
23ad8 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
23ad9 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e   *context, .  in
23ada 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71  t NotUsed, .  sq
23adb 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
23adc 67 76 0a 29 7b 0a 20 20 4d 65 6d 20 2a 70 41 72  gv.){.  Mem *pAr
23add 67 20 20 3d 20 28 4d 65 6d 20 2a 29 61 72 67 76  g  = (Mem *)argv
23ade 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a 70 42 65 73  [0];.  Mem *pBes
23adf 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  t;.  UNUSED_PARA
23ae0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
23ae1 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  .  if( sqlite3_v
23ae2 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
23ae3 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
23ae4 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 42 65 73  ) return;.  pBes
23ae5 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71 6c 69 74  t = (Mem *)sqlit
23ae6 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
23ae7 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69  text(context, si
23ae8 7a 65 6f 66 28 2a 70 42 65 73 74 29 29 3b 0a 20  zeof(*pBest));. 
23ae9 20 69 66 28 20 21 70 42 65 73 74 20 29 20 72 65   if( !pBest ) re
23aea 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 70 42 65  turn;..  if( pBe
23aeb 73 74 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20  st->flags ){.   
23aec 20 69 6e 74 20 6d 61 78 3b 0a 20 20 20 20 69 6e   int max;.    in
23aed 74 20 63 6d 70 3b 0a 20 20 20 20 43 6f 6c 6c 53  t cmp;.    CollS
23aee 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  eq *pColl = sqli
23aef 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65  te3GetFuncCollSe
23af0 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  q(context);.    
23af1 2f 2a 20 54 68 69 73 20 73 74 65 70 20 66 75 6e  /* This step fun
23af2 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f  ction is used fo
23af3 72 20 62 6f 74 68 20 74 68 65 20 6d 69 6e 28 29  r both the min()
23af4 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65   and max() aggre
23af5 67 61 74 65 73 2c 0a 20 20 20 20 2a 2a 20 74 68  gates,.    ** th
23af6 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63  e only differenc
23af7 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77  e between the tw
23af8 6f 20 62 65 69 6e 67 20 74 68 61 74 20 74 68 65  o being that the
23af9 20 73 65 6e 73 65 20 6f 66 20 74 68 65 0a 20 20   sense of the.  
23afa 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20    ** comparison 
23afb 69 73 20 69 6e 76 65 72 74 65 64 2e 20 46 6f 72  is inverted. For
23afc 20 74 68 65 20 6d 61 78 28 29 20 61 67 67 72 65   the max() aggre
23afd 67 61 74 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a  gate, the.    **
23afe 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
23aff 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65  ta() function re
23b00 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 2d 31  turns (void *)-1
23b01 2e 20 46 6f 72 20 6d 69 6e 28 29 20 69 74 0a 20  . For min() it. 
23b02 20 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 28 76     ** returns (v
23b03 6f 69 64 20 2a 29 64 62 2c 20 77 68 65 72 65 20  oid *)db, where 
23b04 64 62 20 69 73 20 74 68 65 20 73 71 6c 69 74 65  db is the sqlite
23b05 33 2a 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e  3* database poin
23b06 74 65 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65 72  ter..    ** Ther
23b07 65 66 6f 72 65 20 74 68 65 20 6e 65 78 74 20 73  efore the next s
23b08 74 61 74 65 6d 65 6e 74 20 73 65 74 73 20 76 61  tatement sets va
23b09 72 69 61 62 6c 65 20 27 6d 61 78 27 20 74 6f 20  riable 'max' to 
23b0a 31 20 66 6f 72 20 74 68 65 20 6d 61 78 28 29 0a  1 for the max().
23b0b 20 20 20 20 2a 2a 20 61 67 67 72 65 67 61 74 65      ** aggregate
23b0c 2c 20 6f 72 20 30 20 66 6f 72 20 6d 69 6e 28 29  , or 0 for min()
23b0d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 61 78  ..    */.    max
23b0e 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f   = sqlite3_user_
23b0f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 21 3d 30  data(context)!=0
23b10 3b 0a 20 20 20 20 63 6d 70 20 3d 20 73 71 6c 69  ;.    cmp = sqli
23b11 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 42  te3MemCompare(pB
23b12 65 73 74 2c 20 70 41 72 67 2c 20 70 43 6f 6c 6c  est, pArg, pColl
23b13 29 3b 0a 20 20 20 20 69 66 28 20 28 6d 61 78 20  );.    if( (max 
23b14 26 26 20 63 6d 70 3c 30 29 20 7c 7c 20 28 21 6d  && cmp<0) || (!m
23b15 61 78 20 26 26 20 63 6d 70 3e 30 29 20 29 7b 0a  ax && cmp>0) ){.
23b16 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23b17 65 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c 20  eMemCopy(pBest, 
23b18 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pArg);.    }.  }
23b19 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
23b1a 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42 65  3VdbeMemCopy(pBe
23b1b 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a 7d  st, pArg);.  }.}
23b1c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e  .static void min
23b1d 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69  MaxFinalize(sqli
23b1e 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
23b1f 74 65 78 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  text){.  sqlite3
23b20 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b 0a 20 20  _value *pRes;.  
23b21 70 52 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f  pRes = (sqlite3_
23b22 76 61 6c 75 65 20 2a 29 73 71 6c 69 74 65 33 5f  value *)sqlite3_
23b23 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
23b24 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20  t(context, 0);. 
23b25 20 69 66 28 20 70 52 65 73 20 29 7b 0a 20 20 20   if( pRes ){.   
23b26 20 69 66 28 20 70 52 65 73 2d 3e 66 6c 61 67 73   if( pRes->flags
23b27 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23b28 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63  3_result_value(c
23b29 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 3b 0a 20  ontext, pRes);. 
23b2a 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
23b2b 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
23b2c 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  Res);.  }.}../*.
23b2d 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28  ** group_concat(
23b2e 45 58 50 52 2c 20 3f 53 45 50 41 52 41 54 4f 52  EXPR, ?SEPARATOR
23b2f 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ?).*/.static voi
23b30 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65  d groupConcatSte
23b31 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  p(.  sqlite3_con
23b32 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
23b33 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
23b34 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
23b35 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  v.){.  const cha
23b36 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 74 72 41 63  r *zVal;.  StrAc
23b37 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 63  cum *pAccum;.  c
23b38 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 3b  onst char *zSep;
23b39 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53 65  .  int nVal, nSe
23b3a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  p;.  assert( arg
23b3b 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20  c==1 || argc==2 
23b3c 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
23b3d 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
23b3e 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  [0])==SQLITE_NUL
23b3f 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 41  L ) return;.  pA
23b40 63 63 75 6d 20 3d 20 28 53 74 72 41 63 63 75 6d  ccum = (StrAccum
23b41 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  *)sqlite3_aggreg
23b42 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
23b43 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 41 63  ext, sizeof(*pAc
23b44 63 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20 70 41  cum));..  if( pA
23b45 63 63 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69  ccum ){.    sqli
23b46 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
23b47 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
23b48 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
23b49 20 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c    pAccum->useMal
23b4a 6c 6f 63 20 3d 20 31 3b 0a 20 20 20 20 70 41 63  loc = 1;.    pAc
23b4b 63 75 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64  cum->mxAlloc = d
23b4c 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
23b4d 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a  _LIMIT_LENGTH];.
23b4e 20 20 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e      if( pAccum->
23b4f 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 69  nChar ){.      i
23b50 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20  f( argc==2 ){.  
23b51 20 20 20 20 20 20 7a 53 65 70 20 3d 20 28 63 68        zSep = (ch
23b52 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
23b53 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
23b54 0a 20 20 20 20 20 20 20 20 6e 53 65 70 20 3d 20  .        nSep = 
23b55 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
23b56 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  tes(argv[1]);.  
23b57 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23b58 20 20 20 7a 53 65 70 20 3d 20 22 2c 22 3b 0a 20     zSep = ",";. 
23b59 20 20 20 20 20 20 20 6e 53 65 70 20 3d 20 31 3b         nSep = 1;
23b5a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
23b5b 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
23b5c 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 65  pend(pAccum, zSe
23b5d 70 2c 20 6e 53 65 70 29 3b 0a 20 20 20 20 7d 0a  p, nSep);.    }.
23b5e 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 68 61 72      zVal = (char
23b5f 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
23b60 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
23b61 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65     nVal = sqlite
23b62 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
23b63 67 76 5b 30 5d 29 3b 0a 20 20 20 20 73 71 6c 69  gv[0]);.    sqli
23b64 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
23b65 64 28 70 41 63 63 75 6d 2c 20 7a 56 61 6c 2c 20  d(pAccum, zVal, 
23b66 6e 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  nVal);.  }.}.sta
23b67 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70 43 6f  tic void groupCo
23b68 6e 63 61 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c  ncatFinalize(sql
23b69 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
23b6a 6e 74 65 78 74 29 7b 0a 20 20 53 74 72 41 63 63  ntext){.  StrAcc
23b6b 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 70 41  um *pAccum;.  pA
23b6c 63 63 75 6d 20 3d 20 73 71 6c 69 74 65 33 5f 61  ccum = sqlite3_a
23b6d 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
23b6e 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
23b6f 69 66 28 20 70 41 63 63 75 6d 20 29 7b 0a 20 20  if( pAccum ){.  
23b70 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 74 6f    if( pAccum->to
23b71 6f 42 69 67 20 29 7b 0a 20 20 20 20 20 20 73 71  oBig ){.      sq
23b72 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
23b73 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
23b74 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  t);.    }else if
23b75 28 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63  ( pAccum->malloc
23b76 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
23b77 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
23b78 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
23b79 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 20  xt);.    }else{ 
23b7a 20 20 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65     .      sqlite
23b7b 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
23b7c 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 53 74  ntext, sqlite3St
23b7d 72 41 63 63 75 6d 46 69 6e 69 73 68 28 70 41 63  rAccumFinish(pAc
23b7e 63 75 6d 29 2c 20 2d 31 2c 20 0a 20 20 20 20 20  cum), -1, .     
23b7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23b80 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
23b81 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  e);.    }.  }.}.
23b82 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
23b83 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
23b84 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65  all of the above
23b85 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20   C functions as 
23b86 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  SQL.** functions
23b87 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  .  This should b
23b88 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69  e the only routi
23b89 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ne in this file 
23b8a 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c  with.** external
23b8b 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c   linkage..*/.SQL
23b8c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
23b8d 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
23b8e 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73  BuiltinFunctions
23b8f 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 23  (sqlite3 *db){.#
23b90 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23b91 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20  IT_ALTERTABLE.  
23b92 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63  sqlite3AlterFunc
23b93 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65 6e 64 69  tions(db);.#endi
23b94 66 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c  f.  if( !db->mal
23b95 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
23b96 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
23b97 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
23b98 69 6f 6e 28 64 62 2c 20 22 4d 41 54 43 48 22 2c  ion(db, "MATCH",
23b99 20 32 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   2);.    assert(
23b9a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
23b9b 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  M || rc==SQLITE_
23b9c 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  OK );.    if( rc
23b9d 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
23b9e 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
23b9f 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
23ba0 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53    }.  }.#ifdef S
23ba1 51 4c 49 54 45 5f 53 53 45 0a 20 20 28 76 6f 69  QLITE_SSE.  (voi
23ba2 64 29 73 71 6c 69 74 65 33 53 73 65 46 75 6e 63  d)sqlite3SseFunc
23ba3 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65 6e 64 69  tions(db);.#endi
23ba4 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  f.}../*.** Set t
23ba5 68 65 20 4c 49 4b 45 4f 50 54 20 66 6c 61 67 20  he LIKEOPT flag 
23ba6 6f 6e 20 74 68 65 20 32 2d 61 72 67 75 6d 65 6e  on the 2-argumen
23ba7 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  t function with 
23ba8 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 0a  the given name..
23ba9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
23baa 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 73 71  etLikeOptFlag(sq
23bab 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
23bac 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 75 38   char *zName, u8
23bad 20 66 6c 61 67 56 61 6c 29 7b 0a 20 20 46 75 6e   flagVal){.  Fun
23bae 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 70 44  cDef *pDef;.  pD
23baf 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ef = sqlite3Find
23bb0 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61  Function(db, zNa
23bb1 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  me, sqlite3Strle
23bb2 6e 33 30 28 7a 4e 61 6d 65 29 2c 0a 20 20 20 20  n30(zName),.    
23bb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23bb4 20 20 20 20 20 20 20 20 20 32 2c 20 53 51 4c 49           2, SQLI
23bb5 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69  TE_UTF8, 0);.  i
23bb6 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 70  f( pDef ){.    p
23bb7 44 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61  Def->flags = fla
23bb8 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  gVal;.  }.}../*.
23bb9 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20  ** Register the 
23bba 62 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 61 6e  built-in LIKE an
23bbb 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73  d GLOB functions
23bbc 2e 20 20 54 68 65 20 63 61 73 65 53 65 6e 73 69  .  The caseSensi
23bbd 74 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65  tive.** paramete
23bbe 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  r determines whe
23bbf 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
23bc0 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 69 73  LIKE operator is
23bc1 20 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69 74 69   case.** sensiti
23bc2 76 65 2e 20 20 47 4c 4f 42 20 69 73 20 61 6c 77  ve.  GLOB is alw
23bc3 61 79 73 20 63 61 73 65 20 73 65 6e 73 69 74 69  ays case sensiti
23bc4 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
23bc5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23bc6 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75  e3RegisterLikeFu
23bc7 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  nctions(sqlite3 
23bc8 2a 64 62 2c 20 69 6e 74 20 63 61 73 65 53 65 6e  *db, int caseSen
23bc9 73 69 74 69 76 65 29 7b 0a 20 20 73 74 72 75 63  sitive){.  struc
23bca 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70  t compareInfo *p
23bcb 49 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61 73 65  Info;.  if( case
23bcc 53 65 6e 73 69 74 69 76 65 20 29 7b 0a 20 20 20  Sensitive ){.   
23bcd 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74   pInfo = (struct
23bce 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c   compareInfo*)&l
23bcf 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65  ikeInfoAlt;.  }e
23bd0 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d  lse{.    pInfo =
23bd1 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65   (struct compare
23bd2 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e  Info*)&likeInfoN
23bd3 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  orm;.  }.  sqlit
23bd4 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
23bd5 20 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c 49   "like", 2, SQLI
23bd6 54 45 5f 41 4e 59 2c 20 70 49 6e 66 6f 2c 20 6c  TE_ANY, pInfo, l
23bd7 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  ikeFunc, 0, 0);.
23bd8 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
23bd9 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20  unc(db, "like", 
23bda 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 70  3, SQLITE_ANY, p
23bdb 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20  Info, likeFunc, 
23bdc 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0);.  sqlite3
23bdd 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
23bde 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54 45  glob", 2, SQLITE
23bdf 5f 41 4e 59 2c 20 0a 20 20 20 20 20 20 28 73 74  _ANY, .      (st
23be0 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
23be1 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69 6b  *)&globInfo, lik
23be2 65 46 75 6e 63 2c 20 30 2c 30 29 3b 0a 20 20 73  eFunc, 0,0);.  s
23be3 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64 62  etLikeOptFlag(db
23be4 2c 20 22 67 6c 6f 62 22 2c 20 53 51 4c 49 54 45  , "glob", SQLITE
23be5 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c  _FUNC_LIKE | SQL
23be6 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3b 0a  ITE_FUNC_CASE);.
23be7 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67    setLikeOptFlag
23be8 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 0a 20 20  (db, "like", .  
23be9 20 20 20 20 63 61 73 65 53 65 6e 73 69 74 69 76      caseSensitiv
23bea 65 20 3f 20 28 53 51 4c 49 54 45 5f 46 55 4e 43  e ? (SQLITE_FUNC
23beb 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46  _LIKE | SQLITE_F
23bec 55 4e 43 5f 43 41 53 45 29 20 3a 20 53 51 4c 49  UNC_CASE) : SQLI
23bed 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b 0a 7d  TE_FUNC_LIKE);.}
23bee 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 70 6f  ../*.** pExpr po
23bef 69 6e 74 73 20 74 6f 20 61 6e 20 65 78 70 72 65  ints to an expre
23bf0 73 73 69 6f 6e 20 77 68 69 63 68 20 69 6d 70 6c  ssion which impl
23bf1 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f  ements a functio
23bf2 6e 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 20  n.  If.** it is 
23bf3 61 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 61  appropriate to a
23bf4 70 70 6c 79 20 74 68 65 20 4c 49 4b 45 20 6f 70  pply the LIKE op
23bf5 74 69 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 74 68  timization to th
23bf6 61 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74  at function.** t
23bf7 68 65 6e 20 73 65 74 20 61 57 63 5b 30 5d 20 74  hen set aWc[0] t
23bf8 68 72 6f 75 67 68 20 61 57 63 5b 32 5d 20 74 6f  hrough aWc[2] to
23bf9 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 63 68   the wildcard ch
23bfa 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a 20  aracters and.** 
23bfb 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66  return TRUE.  If
23bfc 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
23bfd 20 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79 6c   not a LIKE-styl
23bfe 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e 0a  e function then.
23bff 2a 2a 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e  ** return FALSE.
23c00 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23c01 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
23c02 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c  LikeFunction(sql
23c03 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a  ite3 *db, Expr *
23c04 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 49 73 4e  pExpr, int *pIsN
23c05 6f 63 61 73 65 2c 20 63 68 61 72 20 2a 61 57 63  ocase, char *aWc
23c06 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44  ){.  FuncDef *pD
23c07 65 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  ef;.  if( pExpr-
23c08 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e  >op!=TK_FUNCTION
23c09 20 0a 20 20 20 7c 7c 20 21 70 45 78 70 72 2d 3e   .   || !pExpr->
23c0a 78 2e 70 4c 69 73 74 20 0a 20 20 20 7c 7c 20 70  x.pList .   || p
23c0b 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e  Expr->x.pList->n
23c0c 45 78 70 72 21 3d 32 0a 20 20 29 7b 0a 20 20 20  Expr!=2.  ){.   
23c0d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
23c0e 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
23c0f 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
23c10 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
23c11 3b 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74  ;.  pDef = sqlit
23c12 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64  e3FindFunction(d
23c13 62 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d  b, (char*)pExpr-
23c14 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d  >token.z, pExpr-
23c15 3e 74 6f 6b 65 6e 2e 6e 2c 20 32 2c 0a 20 20 20  >token.n, 2,.   
23c16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23c17 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
23c18 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28  _UTF8, 0);.  if(
23c19 20 70 44 65 66 3d 3d 30 20 7c 7c 20 28 70 44 65   pDef==0 || (pDe
23c1a 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f->flags & SQLIT
23c1b 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20  E_FUNC_LIKE)==0 
23c1c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
23c1d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6d  .  }..  /* The m
23c1e 65 6d 63 70 79 28 29 20 73 74 61 74 65 6d 65 6e  emcpy() statemen
23c1f 74 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74  t assumes that t
23c20 68 65 20 77 69 6c 64 63 61 72 64 20 63 68 61 72  he wildcard char
23c21 61 63 74 65 72 73 20 61 72 65 0a 20 20 2a 2a 20  acters are.  ** 
23c22 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20  the first three 
23c23 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68  statements in th
23c24 65 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74  e compareInfo st
23c25 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 20 20  ructure.  The.  
23c26 2a 2a 20 61 73 73 65 72 74 73 28 29 20 74 68 61  ** asserts() tha
23c27 74 20 66 6f 6c 6c 6f 77 20 76 65 72 69 66 79 20  t follow verify 
23c28 74 68 61 74 20 61 73 73 75 6d 70 74 69 6f 6e 0a  that assumption.
23c29 20 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 61 57    */.  memcpy(aW
23c2a 63 2c 20 70 44 65 66 2d 3e 70 55 73 65 72 44 61  c, pDef->pUserDa
23c2b 74 61 2c 20 33 29 3b 0a 20 20 61 73 73 65 72 74  ta, 3);.  assert
23c2c 28 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e  ( (char*)&likeIn
23c2d 66 6f 41 6c 74 20 3d 3d 20 28 63 68 61 72 2a 29  foAlt == (char*)
23c2e 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74  &likeInfoAlt.mat
23c2f 63 68 41 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72  chAll );.  asser
23c30 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b  t( &((char*)&lik
23c31 65 49 6e 66 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20  eInfoAlt)[1] == 
23c32 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f  (char*)&likeInfo
23c33 41 6c 74 2e 6d 61 74 63 68 4f 6e 65 20 29 3b 0a  Alt.matchOne );.
23c34 20 20 61 73 73 65 72 74 28 20 26 28 28 63 68 61    assert( &((cha
23c35 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29  r*)&likeInfoAlt)
23c36 5b 32 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c  [2] == (char*)&l
23c37 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68  ikeInfoAlt.match
23c38 53 65 74 20 29 3b 0a 20 20 2a 70 49 73 4e 6f 63  Set );.  *pIsNoc
23c39 61 73 65 20 3d 20 28 70 44 65 66 2d 3e 66 6c 61  ase = (pDef->fla
23c3a 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43  gs & SQLITE_FUNC
23c3b 5f 43 41 53 45 29 3d 3d 30 3b 0a 20 20 72 65 74  _CASE)==0;.  ret
23c3c 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
23c3d 41 6c 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 46  All all of the F
23c3e 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65  uncDef structure
23c3f 73 20 69 6e 20 74 68 65 20 61 42 75 69 6c 74 69  s in the aBuilti
23c40 6e 46 75 6e 63 5b 5d 20 61 72 72 61 79 20 61 62  nFunc[] array ab
23c41 6f 76 65 0a 2a 2a 20 74 6f 20 74 68 65 20 67 6c  ove.** to the gl
23c42 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20 68 61  obal function ha
23c43 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  sh table.  This 
23c44 6f 63 63 75 72 73 20 61 74 20 73 74 61 72 74 2d  occurs at start-
23c45 74 69 6d 65 20 28 61 73 0a 2a 2a 20 61 20 63 6f  time (as.** a co
23c46 6e 73 65 71 75 65 6e 63 65 20 6f 66 20 63 61 6c  nsequence of cal
23c47 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69  ling sqlite3_ini
23c48 74 69 61 6c 69 7a 65 28 29 29 2e 0a 2a 2a 0a 2a  tialize())..**.*
23c49 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75  * After this rou
23c4a 74 69 6e 65 20 72 75 6e 73 0a 2a 2f 0a 53 51 4c  tine runs.*/.SQL
23c4b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
23c4c 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
23c4d 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28  GlobalFunctions(
23c4e 76 6f 69 64 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a  void){.  /*.  **
23c4f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
23c50 72 72 61 79 20 68 6f 6c 64 73 20 46 75 6e 63 44  rray holds FuncD
23c51 65 66 20 73 74 72 75 63 74 75 72 65 73 20 66 6f  ef structures fo
23c52 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 75 6e  r all of the fun
23c53 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 64 65 66 69  ctions.  ** defi
23c54 6e 65 64 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ned in this file
23c55 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
23c56 61 72 72 61 79 20 63 61 6e 6e 6f 74 20 62 65 20  array cannot be 
23c57 63 6f 6e 73 74 61 6e 74 20 73 69 6e 63 65 20 63  constant since c
23c58 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
23c59 74 6f 20 74 68 65 0a 20 20 2a 2a 20 46 75 6e 63  to the.  ** Func
23c5a 44 65 66 2e 70 48 61 73 68 20 65 6c 65 6d 65 6e  Def.pHash elemen
23c5b 74 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65  ts at start-time
23c5c 2e 20 20 54 68 65 20 65 6c 65 6d 65 6e 74 73 20  .  The elements 
23c5d 6f 66 20 74 68 69 73 20 61 72 72 61 79 0a 20 20  of this array.  
23c5e 2a 2a 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79  ** are read-only
23c5f 20 61 66 74 65 72 20 69 6e 69 74 69 61 6c 69 7a   after initializ
23c60 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74  ation is complet
23c61 65 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63  e..  */.  static
23c62 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63   SQLITE_WSD Func
23c63 44 65 66 20 61 42 75 69 6c 74 69 6e 46 75 6e 63  Def aBuiltinFunc
23c64 5b 5d 20 3d 20 7b 0a 20 20 20 20 46 55 4e 43 54  [] = {.    FUNCT
23c65 49 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20  ION(ltrim,      
23c66 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20 30 2c          1, 1, 0,
23c67 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
23c68 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
23c69 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20  N(ltrim,        
23c6a 20 20 20 20 20 20 32 2c 20 31 2c 20 30 2c 20 74        2, 1, 0, t
23c6b 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20  rimFunc         
23c6c 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
23c6d 72 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20  rtrim,          
23c6e 20 20 20 20 31 2c 20 32 2c 20 30 2c 20 74 72 69      1, 2, 0, tri
23c6f 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c  mFunc         ),
23c70 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74  .    FUNCTION(rt
23c71 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  rim,            
23c72 20 20 32 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46    2, 2, 0, trimF
23c73 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20  unc         ),. 
23c74 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d     FUNCTION(trim
23c75 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
23c76 31 2c 20 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e  1, 3, 0, trimFun
23c77 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  c         ),.   
23c78 20 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20   FUNCTION(trim, 
23c79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
23c7a 20 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20   3, 0, trimFunc 
23c7b 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
23c7c 55 4e 43 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20  UNCTION(min,    
23c7d 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30             -1, 0
23c7e 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20  , 1, minmaxFunc 
23c7f 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
23c80 43 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20  CTION(min,      
23c81 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20            0, 0, 
23c82 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20  1, 0            
23c83 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45      ),.    AGGRE
23c84 47 41 54 45 28 6d 69 6e 2c 20 20 20 20 20 20 20  GATE(min,       
23c85 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 31 2c          1, 0, 1,
23c86 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 20   minmaxStep,    
23c87 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65    minMaxFinalize
23c88 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
23c89 28 6d 61 78 2c 20 20 20 20 20 20 20 20 20 20 20  (max,           
23c8a 20 20 20 20 2d 31 2c 20 31 2c 20 31 2c 20 6d 69      -1, 1, 1, mi
23c8b 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20 29  nmaxFunc       )
23c8c 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d  ,.    FUNCTION(m
23c8d 61 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ax,             
23c8e 20 20 20 30 2c 20 31 2c 20 31 2c 20 30 20 20 20     0, 1, 1, 0   
23c8f 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a               ),.
23c90 20 20 20 20 41 47 47 52 45 47 41 54 45 28 6d 61      AGGREGATE(ma
23c91 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
23c92 20 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78   1, 1, 1, minmax
23c93 53 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61  Step,      minMa
23c94 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20  xFinalize ),.   
23c95 20 46 55 4e 43 54 49 4f 4e 28 74 79 70 65 6f 66   FUNCTION(typeof
23c96 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c  ,             1,
23c97 20 30 2c 20 30 2c 20 74 79 70 65 6f 66 46 75 6e   0, 0, typeofFun
23c98 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  c       ),.    F
23c99 55 4e 43 54 49 4f 4e 28 6c 65 6e 67 74 68 2c 20  UNCTION(length, 
23c9a 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
23c9b 2c 20 30 2c 20 6c 65 6e 67 74 68 46 75 6e 63 20  , 0, lengthFunc 
23c9c 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
23c9d 43 54 49 4f 4e 28 73 75 62 73 74 72 2c 20 20 20  CTION(substr,   
23c9e 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20            2, 0, 
23c9f 30 2c 20 73 75 62 73 74 72 46 75 6e 63 20 20 20  0, substrFunc   
23ca0 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
23ca1 49 4f 4e 28 73 75 62 73 74 72 2c 20 20 20 20 20  ION(substr,     
23ca2 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30 2c          3, 0, 0,
23ca3 20 73 75 62 73 74 72 46 75 6e 63 20 20 20 20 20   substrFunc     
23ca4 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
23ca5 4e 28 61 62 73 2c 20 20 20 20 20 20 20 20 20 20  N(abs,          
23ca6 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 61        1, 0, 0, a
23ca7 62 73 46 75 6e 63 20 20 20 20 20 20 20 20 20 20  bsFunc          
23ca8 29 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ),.#ifndef SQLIT
23ca9 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
23caa 50 4f 49 4e 54 0a 20 20 20 20 46 55 4e 43 54 49  POINT.    FUNCTI
23cab 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20 20  ON(round,       
23cac 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
23cad 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20 20  roundFunc       
23cae 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
23caf 28 72 6f 75 6e 64 2c 20 20 20 20 20 20 20 20 20  (round,         
23cb0 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 72 6f       2, 0, 0, ro
23cb1 75 6e 64 46 75 6e 63 20 20 20 20 20 20 20 20 29  undFunc        )
23cb2 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 46 55 4e  ,.#endif.    FUN
23cb3 43 54 49 4f 4e 28 75 70 70 65 72 2c 20 20 20 20  CTION(upper,    
23cb4 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
23cb5 30 2c 20 75 70 70 65 72 46 75 6e 63 20 20 20 20  0, upperFunc    
23cb6 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
23cb7 49 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20 20 20  ION(lower,      
23cb8 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
23cb9 20 6c 6f 77 65 72 46 75 6e 63 20 20 20 20 20 20   lowerFunc      
23cba 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
23cbb 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20  N(coalesce,     
23cbc 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 30        1, 0, 0, 0
23cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23cbe 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
23cbf 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20  coalesce,       
23cc0 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 69 66 6e     -1, 0, 0, ifn
23cc1 75 6c 6c 46 75 6e 63 20 20 20 20 20 20 20 29 2c  ullFunc       ),
23cc2 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f  .    FUNCTION(co
23cc3 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20  alesce,         
23cc4 20 20 30 2c 20 30 2c 20 30 2c 20 30 20 20 20 20    0, 0, 0, 0    
23cc5 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20              ),. 
23cc6 20 20 20 46 55 4e 43 54 49 4f 4e 28 68 65 78 2c     FUNCTION(hex,
23cc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23cc8 31 2c 20 30 2c 20 30 2c 20 68 65 78 46 75 6e 63  1, 0, 0, hexFunc
23cc9 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20            ),.   
23cca 20 46 55 4e 43 54 49 4f 4e 28 69 66 6e 75 6c 6c   FUNCTION(ifnull
23ccb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c  ,             2,
23ccc 20 30 2c 20 31 2c 20 69 66 6e 75 6c 6c 46 75 6e   0, 1, ifnullFun
23ccd 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  c       ),.    F
23cce 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c 20  UNCTION(random, 
23ccf 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30              0, 0
23cd0 2c 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 63 20  , 0, randomFunc 
23cd1 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
23cd2 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f 62  CTION(randomblob
23cd3 2c 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20  ,         1, 0, 
23cd4 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 20 20  0, randomBlob   
23cd5 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
23cd6 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 20 20  ION(nullif,     
23cd7 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 31 2c          2, 0, 1,
23cd8 20 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 20 20   nullifFunc     
23cd9 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
23cda 4e 28 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e  N(sqlite_version
23cdb 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 76  ,     0, 0, 0, v
23cdc 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 20 20  ersionFunc      
23cdd 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
23cde 71 75 6f 74 65 2c 20 20 20 20 20 20 20 20 20 20  quote,          
23cdf 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f      1, 0, 0, quo
23ce0 74 65 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c  teFunc        ),
23ce1 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61  .    FUNCTION(la
23ce2 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c  st_insert_rowid,
23ce3 20 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f    0, 0, 0, last_
23ce4 69 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20  insert_rowid),. 
23ce5 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e     FUNCTION(chan
23ce6 67 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ges,            
23ce7 30 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67 65 73  0, 0, 0, changes
23ce8 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20            ),.   
23ce9 20 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f   FUNCTION(total_
23cea 63 68 61 6e 67 65 73 2c 20 20 20 20 20 20 30 2c  changes,      0,
23ceb 20 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61   0, 0, total_cha
23cec 6e 67 65 73 20 20 20 20 29 2c 0a 20 20 20 20 46  nges    ),.    F
23ced 55 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c  UNCTION(replace,
23cee 20 20 20 20 20 20 20 20 20 20 20 20 33 2c 20 30              3, 0
23cef 2c 20 30 2c 20 72 65 70 6c 61 63 65 46 75 6e 63  , 0, replaceFunc
23cf0 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
23cf1 43 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20  CTION(zeroblob, 
23cf2 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
23cf3 30 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20  0, zeroblobFunc 
23cf4 20 20 20 20 29 2c 0a 20 20 23 69 66 64 65 66 20      ),.  #ifdef 
23cf5 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20  SQLITE_SOUNDEX. 
23cf6 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e     FUNCTION(soun
23cf7 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  dex,            
23cf8 31 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78  1, 0, 0, soundex
23cf9 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 23  Func      ),.  #
23cfa 65 6e 64 69 66 0a 20 20 23 69 66 6e 64 65 66 20  endif.  #ifndef 
23cfb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
23cfc 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46  _EXTENSION.    F
23cfd 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74  UNCTION(load_ext
23cfe 65 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30  ension,     1, 0
23cff 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20  , 0, loadExt    
23d00 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
23d01 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e  CTION(load_exten
23d02 73 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20  sion,     2, 0, 
23d03 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20  0, loadExt      
23d04 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a      ),.  #endif.
23d05 20 20 20 20 41 47 47 52 45 47 41 54 45 28 73 75      AGGREGATE(su
23d06 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
23d07 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65   1, 0, 0, sumSte
23d08 70 2c 20 20 20 20 20 20 20 20 20 73 75 6d 46 69  p,         sumFi
23d09 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20  nalize    ),.   
23d0a 20 41 47 47 52 45 47 41 54 45 28 74 6f 74 61 6c   AGGREGATE(total
23d0b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c  ,             1,
23d0c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20   0, 0, sumStep, 
23d0d 20 20 20 20 20 20 20 20 74 6f 74 61 6c 46 69 6e          totalFin
23d0e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20  alize    ),.    
23d0f 41 47 47 52 45 47 41 54 45 28 61 76 67 2c 20 20  AGGREGATE(avg,  
23d10 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
23d11 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20  0, 0, sumStep,  
23d12 20 20 20 20 20 20 20 61 76 67 46 69 6e 61 6c 69         avgFinali
23d13 7a 65 20 20 20 20 29 2c 0a 20 2f 2a 20 41 47 47  ze    ),. /* AGG
23d14 52 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20  REGATE(count,   
23d15 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20            0, 0, 
23d16 30 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20  0, countStep,   
23d17 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a      countFinaliz
23d18 65 20 20 29 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c  e  ), */.    {0,
23d19 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49  SQLITE_UTF8,SQLI
23d1a 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c  TE_FUNC_COUNT,0,
23d1b 30 2c 30 2c 63 6f 75 6e 74 53 74 65 70 2c 63 6f  0,0,countStep,co
23d1c 75 6e 74 46 69 6e 61 6c 69 7a 65 2c 22 63 6f 75  untFinalize,"cou
23d1d 6e 74 22 2c 30 7d 2c 0a 20 20 20 20 41 47 47 52  nt",0},.    AGGR
23d1e 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20  EGATE(count,    
23d1f 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
23d20 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20  , countStep,    
23d21 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65     countFinalize
23d22 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41    ),.    AGGREGA
23d23 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c  TE(group_concat,
23d24 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 67        1, 0, 0, g
23d25 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20  roupConcatStep, 
23d26 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c  groupConcatFinal
23d27 69 7a 65 29 2c 0a 20 20 20 20 41 47 47 52 45 47  ize),.    AGGREG
23d28 41 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74  ATE(group_concat
23d29 2c 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20  ,      2, 0, 0, 
23d2a 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c  groupConcatStep,
23d2b 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61   groupConcatFina
23d2c 6c 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49  lize),.  .    LI
23d2d 4b 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20  KEFUNC(glob, 2, 
23d2e 26 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54  &globInfo, SQLIT
23d2f 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49  E_FUNC_LIKE|SQLI
23d30 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20  TE_FUNC_CASE),. 
23d31 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43   #ifdef SQLITE_C
23d32 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49  ASE_SENSITIVE_LI
23d33 4b 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28  KE.    LIKEFUNC(
23d34 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e  like, 2, &likeIn
23d35 66 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55  foAlt, SQLITE_FU
23d36 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46  NC_LIKE|SQLITE_F
23d37 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c  UNC_CASE),.    L
23d38 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c  IKEFUNC(like, 3,
23d39 20 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53   &likeInfoAlt, S
23d3a 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c  QLITE_FUNC_LIKE|
23d3b 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45  SQLITE_FUNC_CASE
23d3c 29 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 4c  ),.  #else.    L
23d3d 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c  IKEFUNC(like, 2,
23d3e 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20   &likeInfoNorm, 
23d3f 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
23d40 29 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28  ),.    LIKEFUNC(
23d41 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e  like, 3, &likeIn
23d42 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46  foNorm, SQLITE_F
23d43 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e  UNC_LIKE),.  #en
23d44 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20  dif.  };..  int 
23d45 69 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68  i;.  FuncDefHash
23d46 20 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41   *pHash = &GLOBA
23d47 4c 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73  L(FuncDefHash, s
23d48 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
23d49 74 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65  tions);.  FuncDe
23d4a 66 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63  f *aFunc = (Func
23d4b 44 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e  Def*)&GLOBAL(Fun
23d4c 63 44 65 66 2c 20 61 42 75 69 6c 74 69 6e 46 75  cDef, aBuiltinFu
23d4d 6e 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  nc);..  for(i=0;
23d4e 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75   i<ArraySize(aBu
23d4f 69 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29  iltinFunc); i++)
23d50 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e  {.    sqlite3Fun
23d51 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68  cDefInsert(pHash
23d52 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20  , &aFunc[i]);.  
23d53 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73  }.  sqlite3Regis
23d54 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74  terDateTimeFunct
23d55 69 6f 6e 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  ions();.}../****
23d56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
23d57 66 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f func.c *******
23d58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
23d5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
23d5c 20 66 69 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a   file insert.c *
23d5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
23d60 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
23d61 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
23d62 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
23d63 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
23d64 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
23d65 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
23d66 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
23d67 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
23d68 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
23d69 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
23d6a 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
23d6b 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
23d6c 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
23d6d 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
23d6e 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
23d6f 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
23d70 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
23d71 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
23d72 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
23d73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23d77 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
23d78 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
23d79 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65  outines that are
23d7a 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
23d7b 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64  arser.** to hand
23d7c 6c 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  le INSERT statem
23d7d 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a  ents in SQLite..
23d7e 2a 2a 0a 2a 2a 20 24 49 64 3a 20 69 6e 73 65 72  **.** $Id: inser
23d7f 74 2e 63 2c 76 20 31 2e 32 36 30 20 32 30 30 39  t.c,v 1.260 2009
23d80 2f 30 32 2f 32 38 20 31 30 3a 34 37 3a 34 32 20  /02/28 10:47:42 
23d81 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
23d82 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  $.*/../*.** Set 
23d83 50 34 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  P4 of the most r
23d84 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64  ecently inserted
23d85 20 6f 70 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c   opcode to a col
23d86 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20  umn affinity.** 
23d87 73 74 72 69 6e 67 20 66 6f 72 20 69 6e 64 65 78  string for index
23d88 20 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d 6e 20   pIdx. A column 
23d89 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
23d8a 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65  has one characte
23d8b 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f  r.** for each co
23d8c 6c 75 6d 6e 20 69 6e 20 74 68 65 20 74 61 62 6c  lumn in the tabl
23d8d 65 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  e, according to 
23d8e 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
23d8f 74 68 65 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a  the column:.**.*
23d90 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 20 20  *  Character    
23d91 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74    Column affinit
23d92 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  y.**  ----------
23d93 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23d94 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20  ----.**  'a'    
23d95 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20          TEXT.** 
23d96 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20 20   'b'            
23d97 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20  NONE.**  'c'    
23d98 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a          NUMERIC.
23d99 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 20 20  **  'd'         
23d9a 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27     INTEGER.**  '
23d9b 65 27 20 20 20 20 20 20 20 20 20 20 20 20 52 45  e'            RE
23d9c 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 74 72  AL.**.** An extr
23d9d 61 20 27 62 27 20 69 73 20 61 70 70 65 6e 64 65  a 'b' is appende
23d9e 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  d to the end of 
23d9f 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f  the string to co
23da0 76 65 72 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64  ver the.** rowid
23da1 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61 73   that appears as
23da2 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e   the last column
23da3 20 69 6e 20 65 76 65 72 79 20 69 6e 64 65 78 2e   in every index.
23da4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23da5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49  TE void sqlite3I
23da6 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28  ndexAffinityStr(
23da7 56 64 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a  Vdbe *v, Index *
23da8 70 49 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49  pIdx){.  if( !pI
23da9 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20  dx->zColAff ){. 
23daa 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20     /* The first 
23dab 74 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66  time a column af
23dac 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f  finity string fo
23dad 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69  r a particular i
23dae 6e 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72  ndex is.    ** r
23daf 65 71 75 69 72 65 64 2c 20 69 74 20 69 73 20 61  equired, it is a
23db0 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70  llocated and pop
23db1 75 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74 20  ulated here. It 
23db2 69 73 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61  is then stored a
23db3 73 0a 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65  s.    ** a membe
23db4 72 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73  r of the Index s
23db5 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 75 62  tructure for sub
23db6 73 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20  sequent use..   
23db7 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63   **.    ** The c
23db8 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
23db9 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74  tring will event
23dba 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64  ually be deleted
23dbb 20 62 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   by.    ** sqlit
23dbc 65 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77  eDeleteIndex() w
23dbd 68 65 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74  hen the Index st
23dbe 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69  ructure itself i
23dbf 73 20 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a  s cleaned.    **
23dc0 20 75 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   up..    */.    
23dc1 69 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65  int n;.    Table
23dc2 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70   *pTab = pIdx->p
23dc3 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74  Table;.    sqlit
23dc4 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
23dc5 56 64 62 65 44 62 28 76 29 3b 0a 20 20 20 20 70  VdbeDb(v);.    p
23dc6 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28  Idx->zColAff = (
23dc7 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61  char *)sqlite3Ma
23dc8 6c 6c 6f 63 28 70 49 64 78 2d 3e 6e 43 6f 6c 75  lloc(pIdx->nColu
23dc9 6d 6e 2b 32 29 3b 0a 20 20 20 20 69 66 28 20 21  mn+2);.    if( !
23dca 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b  pIdx->zColAff ){
23dcb 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
23dcc 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
23dcd 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
23dce 0a 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c  .    for(n=0; n<
23dcf 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e  pIdx->nColumn; n
23dd0 2b 2b 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d  ++){.      pIdx-
23dd1 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54  >zColAff[n] = pT
23dd2 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61  ab->aCol[pIdx->a
23dd3 69 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69  iColumn[n]].affi
23dd4 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nity;.    }.    
23dd5 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b  pIdx->zColAff[n+
23dd6 2b 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  +] = SQLITE_AFF_
23dd7 4e 4f 4e 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e  NONE;.    pIdx->
23dd8 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a  zColAff[n] = 0;.
23dd9 20 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65 33 56    }. .  sqlite3V
23dda 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
23ddb 31 2c 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66  1, pIdx->zColAff
23ddc 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  , 0);.}../*.** S
23ddd 65 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f 73  et P4 of the mos
23dde 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72  t recently inser
23ddf 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61 20  ted opcode to a 
23de0 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a  column affinity.
23de1 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 74 61  ** string for ta
23de2 62 6c 65 20 70 54 61 62 2e 20 41 20 63 6f 6c 75  ble pTab. A colu
23de3 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
23de4 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61  ng has one chara
23de5 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68  cter.** for each
23de6 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65 64 20   column indexed 
23de7 62 79 20 74 68 65 20 69 6e 64 65 78 2c 20 61 63  by the index, ac
23de8 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61  cording to the a
23de9 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 0a 2a  ffinity of the.*
23dea 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20  * column:.**.** 
23deb 20 43 68 61 72 61 63 74 65 72 20 20 20 20 20 20   Character      
23dec 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a  Column affinity.
23ded 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **  ------------
23dee 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23def 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 20  --.**  'a'      
23df0 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 27        TEXT.**  '
23df1 62 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 4f  b'            NO
23df2 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 20  NE.**  'c'      
23df3 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a        NUMERIC.**
23df4 20 20 27 64 27 20 20 20 20 20 20 20 20 20 20 20    'd'           
23df5 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 27   INTEGER.**  'e'
23df6 20 20 20 20 20 20 20 20 20 20 20 20 52 45 41 4c              REAL
23df7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23df8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54  TE void sqlite3T
23df9 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28  ableAffinityStr(
23dfa 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a  Vdbe *v, Table *
23dfb 70 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68 65 20  pTab){.  /* The 
23dfc 66 69 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c  first time a col
23dfd 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
23dfe 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74 69 63  ing for a partic
23dff 75 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20  ular table.  ** 
23e00 69 73 20 72 65 71 75 69 72 65 64 2c 20 69 74 20  is required, it 
23e01 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
23e02 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72 65 2e   populated here.
23e03 20 49 74 20 69 73 20 74 68 65 6e 20 0a 20 20 2a   It is then .  *
23e04 2a 20 73 74 6f 72 65 64 20 61 73 20 61 20 6d 65  * stored as a me
23e05 6d 62 65 72 20 6f 66 20 74 68 65 20 54 61 62 6c  mber of the Tabl
23e06 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  e structure for 
23e07 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e 0a  subsequent use..
23e08 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f    **.  ** The co
23e09 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
23e0a 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75  ring will eventu
23e0b 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  ally be deleted 
23e0c 62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 44  by.  ** sqlite3D
23e0d 65 6c 65 74 65 54 61 62 6c 65 28 29 20 77 68 65  eleteTable() whe
23e0e 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  n the Table stru
23e0f 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20  cture itself is 
23e10 63 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20 2a 2f  cleaned up..  */
23e11 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 7a 43  .  if( !pTab->zC
23e12 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 63 68 61  olAff ){.    cha
23e13 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20 20 20  r *zColAff;.    
23e14 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74  int i;.    sqlit
23e15 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
23e16 56 64 62 65 44 62 28 76 29 3b 0a 0a 20 20 20 20  VdbeDb(v);..    
23e17 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20  zColAff = (char 
23e18 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  *)sqlite3Malloc(
23e19 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20  pTab->nCol+1);. 
23e1a 20 20 20 69 66 28 20 21 7a 43 6f 6c 41 66 66 20     if( !zColAff 
23e1b 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
23e1c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
23e1d 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
23e1e 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   }..    for(i=0;
23e1f 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
23e20 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 41  ++){.      zColA
23e21 66 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e 61 43  ff[i] = pTab->aC
23e22 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3b 0a  ol[i].affinity;.
23e23 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 41 66      }.    zColAf
23e24 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d 20  f[pTab->nCol] = 
23e25 27 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61 62 2d  '\0';..    pTab-
23e26 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c 41  >zColAff = zColA
23e27 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  ff;.  }..  sqlit
23e28 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
23e29 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43 6f 6c  , -1, pTab->zCol
23e2a 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Aff, 0);.}../*.*
23e2b 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * Return non-zer
23e2c 6f 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 70  o if the table p
23e2d 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20  Tab in database 
23e2e 69 44 62 20 6f 72 20 61 6e 79 20 6f 66 20 69 74  iDb or any of it
23e2f 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 68 61 76  s indices.** hav
23e30 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 61 74  e been opened at
23e31 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68   any point in th
23e32 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 62  e VDBE program b
23e33 65 67 69 6e 6e 69 6e 67 20 61 74 20 6c 6f 63 61  eginning at loca
23e34 74 69 6f 6e 0a 2a 2a 20 69 53 74 61 72 74 41 64  tion.** iStartAd
23e35 64 72 20 74 68 72 6f 75 67 68 74 20 74 68 65 20  dr throught the 
23e36 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72  end of the progr
23e37 61 6d 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  am.  This is use
23e38 64 20 74 6f 20 73 65 65 20 69 66 20 0a 2a 2a 20  d to see if .** 
23e39 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74  a statement of t
23e3a 68 65 20 66 6f 72 6d 20 20 22 49 4e 53 45 52 54  he form  "INSERT
23e3b 20 49 4e 54 4f 20 3c 69 44 62 2c 20 70 54 61 62   INTO <iDb, pTab
23e3c 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 63 61  > SELECT ..." ca
23e3d 6e 20 0a 2a 2a 20 72 75 6e 20 77 69 74 68 6f 75  n .** run withou
23e3e 74 20 75 73 69 6e 67 20 74 65 6d 70 6f 72 61 72  t using temporar
23e3f 79 20 74 61 62 6c 65 20 66 6f 72 20 74 68 65 20  y table for the 
23e40 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53  results of the S
23e41 45 4c 45 43 54 2e 20 0a 2a 2f 0a 73 74 61 74 69  ELECT. .*/.stati
23e42 63 20 69 6e 74 20 72 65 61 64 73 54 61 62 6c 65  c int readsTable
23e43 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 53  (Vdbe *v, int iS
23e44 74 61 72 74 41 64 64 72 2c 20 69 6e 74 20 69 44  tartAddr, int iD
23e45 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  b, Table *pTab){
23e46 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
23e47 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64  iEnd = sqlite3Vd
23e48 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
23e49 3b 0a 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74  ;.  for(i=iStart
23e4a 41 64 64 72 3b 20 69 3c 69 45 6e 64 3b 20 69 2b  Addr; i<iEnd; i+
23e4b 2b 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a  +){.    VdbeOp *
23e4c 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  pOp = sqlite3Vdb
23e4d 65 47 65 74 4f 70 28 76 2c 20 69 29 3b 0a 20 20  eGetOp(v, i);.  
23e4e 20 20 61 73 73 65 72 74 28 20 70 4f 70 21 3d 30    assert( pOp!=0
23e4f 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   );.    if( pOp-
23e50 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e  >opcode==OP_Open
23e51 52 65 61 64 20 26 26 20 70 4f 70 2d 3e 70 33 3d  Read && pOp->p3=
23e52 3d 69 44 62 20 29 7b 0a 20 20 20 20 20 20 49 6e  =iDb ){.      In
23e53 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20  dex *pIndex;.   
23e54 20 20 20 69 6e 74 20 74 6e 75 6d 20 3d 20 70 4f     int tnum = pO
23e55 70 2d 3e 70 32 3b 0a 20 20 20 20 20 20 69 66 28  p->p2;.      if(
23e56 20 74 6e 75 6d 3d 3d 70 54 61 62 2d 3e 74 6e 75   tnum==pTab->tnu
23e57 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  m ){.        ret
23e58 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
23e59 20 20 20 20 20 66 6f 72 28 70 49 6e 64 65 78 3d       for(pIndex=
23e5a 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
23e5b 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e  ndex; pIndex=pIn
23e5c 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  dex->pNext){.   
23e5d 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70       if( tnum==p
23e5e 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 29 7b 0a 20  Index->tnum ){. 
23e5f 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
23e60 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
23e61 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
23e62 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
23e63 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
23e64 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
23e65 3d 4f 50 5f 56 4f 70 65 6e 20 26 26 20 70 4f 70  =OP_VOpen && pOp
23e66 2d 3e 70 34 2e 70 56 74 61 62 3d 3d 70 54 61 62  ->p4.pVtab==pTab
23e67 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20 20  ->pVtab ){.     
23e68 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
23e69 2e 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20 20  .pVtab!=0 );.   
23e6a 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
23e6b 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20  p4type==P4_VTAB 
23e6c 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
23e6d 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  1;.    }.#endif.
23e6e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
23e6f 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
23e70 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
23e71 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  MENT./*.** Write
23e72 20 6f 75 74 20 63 6f 64 65 20 74 6f 20 69 6e 69   out code to ini
23e73 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 74 6f  tialize the auto
23e74 69 6e 63 72 65 6d 65 6e 74 20 6c 6f 67 69 63 2e  increment logic.
23e75 20 20 54 68 69 73 20 63 6f 64 65 0a 2a 2a 20 6c    This code.** l
23e76 6f 6f 6b 73 20 75 70 20 74 68 65 20 63 75 72 72  ooks up the curr
23e77 65 6e 74 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e  ent autoincremen
23e78 74 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 73  t value in the s
23e79 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a 2a  qlite_sequence.*
23e7a 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  * table and stor
23e7b 65 73 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e  es that value in
23e7c 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 43 6f   a register.  Co
23e7d 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 0a  de generated by.
23e7e 2a 2a 20 61 75 74 6f 49 6e 63 53 74 65 70 28 29  ** autoIncStep()
23e7f 20 77 69 6c 6c 20 6b 65 65 70 20 74 68 61 74 20   will keep that 
23e80 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  register holding
23e81 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20   the largest.** 
23e82 72 6f 77 69 64 20 76 61 6c 75 65 2e 20 20 43 6f  rowid value.  Co
23e83 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  de generated by 
23e84 61 75 74 6f 49 6e 63 45 6e 64 28 29 20 77 69 6c  autoIncEnd() wil
23e85 6c 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 0a  l write the new.
23e86 2a 2a 20 6c 61 72 67 65 73 74 20 76 61 6c 75 65  ** largest value
23e87 20 6f 66 20 74 68 65 20 63 6f 75 6e 74 65 72 20   of the counter 
23e88 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 73 71  back into the sq
23e89 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
23e8a 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ble..**.** This 
23e8b 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
23e8c 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
23e8d 20 6d 65 6d 5b 5d 20 63 65 6c 6c 20 74 68 61 74   mem[] cell that
23e8e 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65   contains.** the
23e8f 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63   maximum rowid c
23e90 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ounter..**.** Th
23e91 72 65 65 20 63 6f 6e 73 65 63 75 74 69 76 65 20  ree consecutive 
23e92 72 65 67 69 73 74 65 72 73 20 61 72 65 20 61 6c  registers are al
23e93 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20  located by this 
23e94 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 0a 2a 2a  routine.  The.**
23e95 20 66 69 72 73 74 20 74 77 6f 20 68 6f 6c 64 20   first two hold 
23e96 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
23e97 74 61 72 67 65 74 20 74 61 62 6c 65 20 61 6e 64  target table and
23e98 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77   the maximum row
23e99 69 64 20 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20  id .** inserted 
23e9a 69 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20  into the target 
23e9b 74 61 62 6c 65 2c 20 72 65 73 70 65 63 74 69 76  table, respectiv
23e9c 65 6c 79 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72  ely..** The thir
23e9d 64 20 68 6f 6c 64 73 20 74 68 65 20 72 6f 77 69  d holds the rowi
23e9e 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75  d in sqlite_sequ
23e9f 65 6e 63 65 20 77 68 65 72 65 20 77 65 20 77 69  ence where we wi
23ea0 6c 6c 0a 2a 2a 20 77 72 69 74 65 20 62 61 63 6b  ll.** write back
23ea1 20 74 68 65 20 72 65 76 69 73 65 64 20 6d 61 78   the revised max
23ea2 69 6d 75 6d 20 72 6f 77 69 64 2e 20 20 54 68 69  imum rowid.  Thi
23ea3 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
23ea4 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f  s the.** index o
23ea5 66 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 66 20  f the second of 
23ea6 74 68 65 73 65 20 74 68 72 65 65 20 72 65 67 69  these three regi
23ea7 73 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  sters..*/.static
23ea8 20 69 6e 74 20 61 75 74 6f 49 6e 63 42 65 67 69   int autoIncBegi
23ea9 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
23eaa 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
23eab 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
23eac 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20   int iDb,       
23ead 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
23eae 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 6f   the database ho
23eaf 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20  lding pTab */.  
23eb0 54 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20 20  Table *pTab     
23eb1 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
23eb2 20 77 65 20 61 72 65 20 77 72 69 74 69 6e 67 20   we are writing 
23eb3 74 6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d  to */.){.  int m
23eb4 65 6d 49 64 20 3d 20 30 3b 20 20 20 20 20 20 2f  emId = 0;      /
23eb5 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
23eb6 6e 67 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  ng maximum rowid
23eb7 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e   */.  if( pTab->
23eb8 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75  tabFlags & TF_Au
23eb9 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20  toincrement ){. 
23eba 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61     Vdbe *v = pPa
23ebb 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20  rse->pVdbe;.    
23ebc 44 62 20 2a 70 44 62 20 3d 20 26 70 50 61 72 73  Db *pDb = &pPars
23ebd 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  e->db->aDb[iDb];
23ebe 0a 20 20 20 20 69 6e 74 20 69 43 75 72 20 3d 20  .    int iCur = 
23ebf 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
23ec0 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20      int addr;   
23ec1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
23ec2 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 74 6f  ddress of the to
23ec3 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  p of the loop */
23ec4 0a 20 20 20 20 61 73 73 65 72 74 28 20 76 20 29  .    assert( v )
23ec5 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
23ec6 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a  em++;         /*
23ec7 20 48 6f 6c 64 73 20 6e 61 6d 65 20 6f 66 20 74   Holds name of t
23ec8 61 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 49  able */.    memI
23ec9 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  d = ++pParse->nM
23eca 65 6d 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  em;.    pParse->
23ecb 6e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 73 71 6c 69  nMem++;.    sqli
23ecc 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
23ecd 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c 20  rse, iCur, iDb, 
23ece 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53  pDb->pSchema->pS
23ecf 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65  eqTab, OP_OpenRe
23ed0 61 64 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20  ad);.    addr = 
23ed1 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
23ed2 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73  ntAddr(v);.    s
23ed3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
23ed4 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
23ed5 30 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c 20 70  0, memId-1, 0, p
23ed6 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Tab->zName, 0);.
23ed7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23ed8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
23ed9 6e 64 2c 20 69 43 75 72 2c 20 61 64 64 72 2b 39  nd, iCur, addr+9
23eda 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23edb 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
23edc 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 30 2c 20  olumn, iCur, 0, 
23edd 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69  memId);.    sqli
23ede 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
23edf 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c   OP_Ne, memId-1,
23ee0 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64 29 3b   addr+7, memId);
23ee1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23ee2 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49  ChangeP5(v, SQLI
23ee3 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
23ee4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23ee5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
23ee6 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31  d, iCur, memId+1
23ee7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23ee8 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
23ee9 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 31 2c 20  olumn, iCur, 1, 
23eea 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69  memId);.    sqli
23eeb 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23eec 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64   OP_Goto, 0, add
23eed 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r+9);.    sqlite
23eee 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
23eef 50 5f 4e 65 78 74 2c 20 69 43 75 72 2c 20 61 64  P_Next, iCur, ad
23ef0 64 72 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr+2);.    sqlit
23ef1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23ef2 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d  OP_Integer, 0, m
23ef3 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  emId);.    sqlit
23ef4 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23ef5 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20  OP_Close, iCur, 
23ef6 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
23ef7 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   memId;.}../*.**
23ef8 20 55 70 64 61 74 65 20 74 68 65 20 6d 61 78 69   Update the maxi
23ef9 6d 75 6d 20 72 6f 77 69 64 20 66 6f 72 20 61 6e  mum rowid for an
23efa 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63   autoincrement c
23efb 61 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  alculation..**.*
23efc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
23efd 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20  hould be called 
23efe 77 68 65 6e 20 74 68 65 20 74 6f 70 20 6f 66 20  when the top of 
23eff 74 68 65 20 73 74 61 63 6b 20 68 6f 6c 64 73 20  the stack holds 
23f00 61 0a 2a 2a 20 6e 65 77 20 72 6f 77 69 64 20 74  a.** new rowid t
23f01 68 61 74 20 69 73 20 61 62 6f 75 74 20 74 6f 20  hat is about to 
23f02 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 49 66  be inserted.  If
23f03 20 74 68 61 74 20 6e 65 77 20 72 6f 77 69 64 20   that new rowid 
23f04 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  is.** larger tha
23f05 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f  n the maximum ro
23f06 77 69 64 20 69 6e 20 74 68 65 20 6d 65 6d 49 64  wid in the memId
23f07 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68   memory cell, th
23f08 65 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79  en the.** memory
23f09 20 63 65 6c 6c 20 69 73 20 75 70 64 61 74 65 64   cell is updated
23f0a 2e 20 20 54 68 65 20 73 74 61 63 6b 20 69 73 20  .  The stack is 
23f0b 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74  unchanged..*/.st
23f0c 61 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e  atic void autoIn
23f0d 63 53 74 65 70 28 50 61 72 73 65 20 2a 70 50 61  cStep(Parse *pPa
23f0e 72 73 65 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20  rse, int memId, 
23f0f 69 6e 74 20 72 65 67 52 6f 77 69 64 29 7b 0a 20  int regRowid){. 
23f10 20 69 66 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a   if( memId>0 ){.
23f11 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23f12 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56  ddOp2(pParse->pV
23f13 64 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20  dbe, OP_MemMax, 
23f14 6d 65 6d 49 64 2c 20 72 65 67 52 6f 77 69 64 29  memId, regRowid)
23f15 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
23f16 66 74 65 72 20 64 6f 69 6e 67 20 6f 6e 65 20 6f  fter doing one o
23f17 72 20 6d 6f 72 65 20 69 6e 73 65 72 74 73 2c 20  r more inserts, 
23f18 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69  the maximum rowi
23f19 64 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69  d is stored.** i
23f1a 6e 20 72 65 67 5b 6d 65 6d 49 64 5d 2e 20 20 47  n reg[memId].  G
23f1b 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
23f1c 77 72 69 74 65 20 74 68 69 73 20 76 61 6c 75 65  write this value
23f1d 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 0a 2a   back into the.*
23f1e 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  * the sqlite_seq
23f1f 75 65 6e 63 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  uence table..*/.
23f20 73 74 61 74 69 63 20 76 6f 69 64 20 61 75 74 6f  static void auto
23f21 49 6e 63 45 6e 64 28 0a 20 20 50 61 72 73 65 20  IncEnd(.  Parse 
23f22 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
23f23 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
23f24 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ext */.  int iDb
23f25 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ,           /* I
23f26 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
23f27 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
23f28 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
23f29 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 61 62  ab,       /* Tab
23f2a 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72 74  le we are insert
23f2b 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e  ing into */.  in
23f2c 74 20 6d 65 6d 49 64 20 20 20 20 20 20 20 20 20  t memId         
23f2d 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
23f2e 68 6f 6c 64 69 6e 67 20 74 68 65 20 6d 61 78 69  holding the maxi
23f2f 6d 75 6d 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a  mum rowid */.){.
23f30 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46    if( pTab->tabF
23f31 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e  lags & TF_Autoin
23f32 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69  crement ){.    i
23f33 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72 73 65  nt iCur = pParse
23f34 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 56 64  ->nTab++;.    Vd
23f35 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
23f36 70 56 64 62 65 3b 0a 20 20 20 20 44 62 20 2a 70  pVdbe;.    Db *p
23f37 44 62 20 3d 20 26 70 50 61 72 73 65 2d 3e 64 62  Db = &pParse->db
23f38 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20  ->aDb[iDb];.    
23f39 69 6e 74 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20  int j1;.    int 
23f3a 69 52 65 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iRec = ++pParse-
23f3b 3e 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 4d 65 6d  >nMem;    /* Mem
23f3c 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f  ory cell used fo
23f3d 72 20 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 20  r record */..   
23f3e 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20   assert( v );.  
23f3f 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
23f40 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c  le(pParse, iCur,
23f41 20 69 44 62 2c 20 70 44 62 2d 3e 70 53 63 68 65   iDb, pDb->pSche
23f42 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f  ma->pSeqTab, OP_
23f43 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20  OpenWrite);.    
23f44 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
23f45 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
23f46 4e 75 6c 6c 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a  Null, memId+1);.
23f47 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23f48 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
23f49 6f 77 69 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49  owid, iCur, memI
23f4a 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  d+1);.    sqlite
23f4b 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
23f4c 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j1);.    sqlite
23f4d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
23f4e 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65  P_MakeRecord, me
23f4f 6d 49 64 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b  mId-1, 2, iRec);
23f50 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23f51 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
23f52 65 72 74 2c 20 69 43 75 72 2c 20 69 52 65 63 2c  ert, iCur, iRec,
23f53 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73   memId+1);.    s
23f54 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
23f55 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
23f56 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  END);.    sqlite
23f57 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
23f58 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a  P_Close, iCur);.
23f59 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a    }.}.#else./*.*
23f5a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  * If SQLITE_OMIT
23f5b 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69  _AUTOINCREMENT i
23f5c 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  s defined, then 
23f5d 74 68 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e  the three routin
23f5e 65 73 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20  es.** above are 
23f5f 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20  all no-ops.*/.# 
23f60 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 42 65  define autoIncBe
23f61 67 69 6e 28 41 2c 42 2c 43 29 20 28 30 29 0a 23  gin(A,B,C) (0).#
23f62 20 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 53   define autoIncS
23f63 74 65 70 28 41 2c 42 2c 43 29 0a 23 20 64 65 66  tep(A,B,C).# def
23f64 69 6e 65 20 61 75 74 6f 49 6e 63 45 6e 64 28 41  ine autoIncEnd(A
23f65 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 20 2f  ,B,C,D).#endif /
23f66 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  * SQLITE_OMIT_AU
23f67 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a  TOINCREMENT */..
23f68 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
23f69 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69  aration */.stati
23f6a 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69 6d 69  c int xferOptimi
23f6b 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20  zation(.  Parse 
23f6c 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
23f6d 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
23f6e 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44  t */.  Table *pD
23f6f 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  est,         /* 
23f70 54 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  The table we are
23f71 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 20   inserting into 
23f72 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
23f73 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20  lect,      /* A 
23f74 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
23f75 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64   to use as the d
23f76 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20  ata source */.  
23f77 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20  int onError,    
23f78 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
23f79 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e  handle constrain
23f7a 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e  t errors */.  in
23f7b 74 20 69 44 62 44 65 73 74 20 20 20 20 20 20 20  t iDbDest       
23f7c 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
23f7d 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a  ase of pDest */.
23f7e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  );../*.** This r
23f7f 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 20 74  outine is call t
23f80 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20  o handle SQL of 
23f81 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
23f82 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e  rms:.**.**    in
23f83 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20  sert into TABLE 
23f84 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65 73 28  (IDLIST) values(
23f85 45 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20  EXPRLIST).**    
23f86 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c  insert into TABL
23f87 45 20 28 49 44 4c 49 53 54 29 20 73 65 6c 65 63  E (IDLIST) selec
23f88 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49  t.**.** The IDLI
23f89 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ST following the
23f8a 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 61   table name is a
23f8b 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20  lways optional. 
23f8c 20 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20   If omitted,.** 
23f8d 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66 20 61  then a list of a
23f8e 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74  ll columns for t
23f8f 68 65 20 74 61 62 6c 65 20 69 73 20 73 75 62 73  he table is subs
23f90 74 69 74 75 74 65 64 2e 20 20 54 68 65 20 49 44  tituted.  The ID
23f91 4c 49 53 54 0a 2a 2a 20 61 70 70 65 61 72 73 20  LIST.** appears 
23f92 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70  in the pColumn p
23f93 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75  arameter.  pColu
23f94 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20 49 44  mn is NULL if ID
23f95 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65 64 2e  LIST is omitted.
23f96 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74  .**.** The pList
23f97 20 70 61 72 61 6d 65 74 65 72 20 68 6f 6c 64 73   parameter holds
23f98 20 45 58 50 52 4c 49 53 54 20 69 6e 20 74 68 65   EXPRLIST in the
23f99 20 66 69 72 73 74 20 66 6f 72 6d 20 6f 66 20 74   first form of t
23f9a 68 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61  he INSERT.** sta
23f9b 74 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e  tement above, an
23f9c 64 20 70 53 65 6c 65 63 74 20 69 73 20 4e 55 4c  d pSelect is NUL
23f9d 4c 2e 20 20 46 6f 72 20 74 68 65 20 73 65 63 6f  L.  For the seco
23f9e 6e 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69  nd form, pList i
23f9f 73 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53  s.** NULL and pS
23fa0 65 6c 65 63 74 20 69 73 20 61 20 70 6f 69 6e 74  elect is a point
23fa1 65 72 20 74 6f 20 74 68 65 20 73 65 6c 65 63 74  er to the select
23fa2 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20   statement used 
23fa3 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64  to generate.** d
23fa4 61 74 61 20 66 6f 72 20 74 68 65 20 69 6e 73 65  ata for the inse
23fa5 72 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  rt..**.** The co
23fa6 64 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f 6c  de generated fol
23fa7 6c 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72  lows one of four
23fa8 20 74 65 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72   templates.  For
23fa9 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c   a simple.** sel
23faa 65 63 74 20 77 69 74 68 20 64 61 74 61 20 63 6f  ect with data co
23fab 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55  ming from a VALU
23fac 45 53 20 63 6c 61 75 73 65 2c 20 74 68 65 20 63  ES clause, the c
23fad 6f 64 65 20 65 78 65 63 75 74 65 73 0a 2a 2a 20  ode executes.** 
23fae 6f 6e 63 65 20 73 74 72 61 69 67 68 74 20 64 6f  once straight do
23faf 77 6e 20 74 68 72 6f 75 67 68 2e 20 20 50 73 65  wn through.  Pse
23fb0 75 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77 73  udo-code follows
23fb1 20 28 77 65 20 63 61 6c 6c 20 74 68 69 73 0a 2a   (we call this.*
23fb2 2a 20 74 68 65 20 22 31 73 74 20 74 65 6d 70 6c  * the "1st templ
23fb3 61 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ate"):.**.**    
23fb4 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74 65 20       open write 
23fb5 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65  cursor to <table
23fb6 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65  > and its indice
23fb7 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75 74  s.**         put
23fb8 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20  s VALUES clause 
23fb9 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f  expressions onto
23fba 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20   the stack.**   
23fbb 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65 20        write the 
23fbc 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72 64  resulting record
23fbd 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a   into <table>.**
23fbe 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70           cleanup
23fbf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 65  .**.** The three
23fc0 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c   remaining templ
23fc1 61 74 65 73 20 61 73 73 75 6d 65 20 74 68 65 20  ates assume the 
23fc2 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20  statement is of 
23fc3 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
23fc4 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61   INSERT INTO <ta
23fc5 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a  ble> SELECT ....
23fc6 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45 4c  **.** If the SEL
23fc7 45 43 54 20 63 6c 61 75 73 65 20 69 73 20 6f 66  ECT clause is of
23fc8 20 74 68 65 20 72 65 73 74 72 69 63 74 65 64 20   the restricted 
23fc9 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a 20 46  form "SELECT * F
23fca 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a  ROM <table2>" -.
23fcb 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** in other word
23fcc 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20  s if the SELECT 
23fcd 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e  pulls all column
23fce 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20  s from a single 
23fcf 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65  table.** and the
23fd0 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45 20 6f  re is no WHERE o
23fd1 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55 50  r LIMIT or GROUP
23fd2 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20   BY or ORDER BY 
23fd3 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20  clauses, and.** 
23fd4 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20  if <table2> and 
23fd5 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64 69 73  <table1> are dis
23fd6 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62 75 74  tinct tables but
23fd7 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c 0a   have identical.
23fd8 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c  ** schemas, incl
23fd9 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20 73 61  uding all the sa
23fda 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68 65 6e  me indices, then
23fdb 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d   a special optim
23fdc 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e  ization.** is in
23fdd 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70 69 65  voked that copie
23fde 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72  s raw records fr
23fdf 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72  om <table2> over
23fe0 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a   to <table1>..**
23fe1 20 53 65 65 20 74 68 65 20 78 66 65 72 4f 70 74   See the xferOpt
23fe2 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63  imization() func
23fe3 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d 70  tion for the imp
23fe4 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
23fe5 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e  his.** template.
23fe6 20 20 54 68 69 73 20 69 73 20 74 68 65 20 32 6e    This is the 2n
23fe7 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a  d template..**.*
23fe8 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61  *         open a
23fe9 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f   write cursor to
23fea 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20   <table>.**     
23feb 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20 63 75      open read cu
23fec 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65 32 3e  rsor on <table2>
23fed 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72 61 6e  .**         tran
23fee 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73  sfer all records
23fef 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65   in <table2> ove
23ff0 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20  r to <table>.** 
23ff1 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75          close cu
23ff2 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
23ff3 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78 20 6f   foreach index o
23ff4 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20  n <table>.**    
23ff5 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72         open a wr
23ff6 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20 74 68  ite cursor on th
23ff7 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65 78 0a  e <table> index.
23ff8 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70 65  **           ope
23ff9 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20  n a read cursor 
23ffa 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  on the correspon
23ffb 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20 69 6e  ding <table2> in
23ffc 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  dex.**          
23ffd 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72 65   transfer all re
23ffe 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 72  cords from the r
23fff 65 61 64 20 74 6f 20 74 68 65 20 77 72 69 74 65  ead to the write
24000 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20   cursors.**     
24001 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72 73        close curs
24002 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  ors.**         e
24003 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a 2a  nd foreach.**.**
24004 20 54 68 65 20 33 72 64 20 74 65 6d 70 6c 61 74   The 3rd templat
24005 65 20 69 73 20 66 6f 72 20 77 68 65 6e 20 74 68  e is for when th
24006 65 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74  e second templat
24007 65 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 6c 79  e does not apply
24008 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45  .** and the SELE
24009 43 54 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e  CT clause does n
2400a 6f 74 20 72 65 61 64 20 66 72 6f 6d 20 3c 74 61  ot read from <ta
2400b 62 6c 65 3e 20 61 74 20 61 6e 79 20 74 69 6d 65  ble> at any time
2400c 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74  ..** The generat
2400d 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20  ed code follows 
2400e 74 68 69 73 20 74 65 6d 70 6c 61 74 65 3a 0a 2a  this template:.*
2400f 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46  *.**         EOF
24010 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20   <- 0.**        
24011 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20   X <- A.**      
24012 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20     goto B.**    
24013 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74    A: setup for t
24014 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  he SELECT.**    
24015 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74       loop over t
24016 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20 53  he rows in the S
24017 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
24018 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 73 20 69     load values i
24019 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20 52 2e  nto registers R.
2401a 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  .R+n.**         
2401b 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20    yield X.**    
2401c 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a       end loop.**
2401d 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70           cleanup
2401e 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43   after the SELEC
2401f 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46  T.**         EOF
24020 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20   <- 1.**        
24021 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20   yield X.**     
24022 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20 20 20      goto A.**   
24023 20 20 20 42 3a 20 6f 70 65 6e 20 77 72 69 74 65     B: open write
24024 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
24025 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63  e> and its indic
24026 65 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20 79 69  es.**      C: yi
24027 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  eld X.**        
24028 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a 2a   if EOF goto D.*
24029 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74  *         insert
2402a 20 74 68 65 20 73 65 6c 65 63 74 20 72 65 73 75   the select resu
2402b 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20  lt into <table> 
2402c 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20  from R..R+n.**  
2402d 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 2a 2a         goto C.**
2402e 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e 75 70        D: cleanup
2402f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68 20 74  .**.** The 4th t
24030 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65 64 20  emplate is used 
24031 69 66 20 74 68 65 20 69 6e 73 65 72 74 20 73 74  if the insert st
24032 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20 69 74  atement takes it
24033 73 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72 6f 6d  s.** values from
24034 20 61 20 53 45 4c 45 43 54 20 62 75 74 20 74 68   a SELECT but th
24035 65 20 64 61 74 61 20 69 73 20 62 65 69 6e 67 20  e data is being 
24036 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 20  inserted into a 
24037 74 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73  table.** that is
24038 20 61 6c 73 6f 20 72 65 61 64 20 61 73 20 70 61   also read as pa
24039 72 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  rt of the SELECT
2403a 2e 20 20 49 6e 20 74 68 65 20 74 68 69 72 64 20  .  In the third 
2403b 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61 76 65  form,.** we have
2403c 20 74 6f 20 75 73 65 20 61 20 69 6e 74 65 72 6d   to use a interm
2403d 65 64 69 61 74 65 20 74 61 62 6c 65 20 74 6f 20  ediate table to 
2403e 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
2403f 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65 6c 65  s of.** the sele
24040 63 74 2e 20 20 54 68 65 20 74 65 6d 70 6c 61 74  ct.  The templat
24041 65 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  e is like this:.
24042 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f  **.**         EO
24043 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20  F <- 0.**       
24044 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20    X <- A.**     
24045 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20      goto B.**   
24046 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20     A: setup for 
24047 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20  the SELECT.**   
24048 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20        loop over 
24049 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68  the tables in th
2404a 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  e SELECT.**     
2404b 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65        load value
2404c 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52   into register R
2404d 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ..R+n.**        
2404e 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20     yield X.**   
2404f 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a        end loop.*
24050 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
24051 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45  p after the SELE
24052 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f  CT.**         EO
24053 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20  F <- 1.**       
24054 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20    yield X.**    
24055 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a       halt-error.
24056 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65 6e 20  **      B: open 
24057 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20  temp table.**   
24058 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 2a 2a     L: yield X.**
24059 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20           if EOF 
2405a 67 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20 20 20  goto M.**       
2405b 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f    insert row fro
2405c 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65  m R..R+n into te
2405d 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20  mp table.**     
2405e 20 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20 20      goto L.**   
2405f 20 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69 74 65     M: open write
24060 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
24061 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63  e> and its indic
24062 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 72 65  es.**         re
24063 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c 65 0a  wind temp table.
24064 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20  **      C: loop 
24065 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e 74  over rows of int
24066 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 0a  ermediate table.
24067 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 61  **           tra
24068 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f 72  nsfer values for
24069 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74  m intermediate t
2406a 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65  able into <table
2406b 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64  >.**         end
2406c 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20 44 3a   loop.**      D:
2406d 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c 49   cleanup.*/.SQLI
2406e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2406f 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 0a 20  sqlite3Insert(. 
24070 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
24071 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
24072 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
24073 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
24074 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
24075 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
24076 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67  we are inserting
24077 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
24078 70 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 4c  pList,      /* L
24079 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ist of values to
2407a 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
2407b 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
2407c 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c  t,      /* A SEL
2407d 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
2407e 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61   use as the data
2407f 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49 64 4c   source */.  IdL
24080 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20  ist *pColumn,   
24081 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
24082 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  es corresponding
24083 20 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f 0a 20   to IDLIST. */. 
24084 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20   int onError    
24085 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f         /* How to
24086 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69   handle constrai
24087 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a  nt errors */.){.
24088 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
24089 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
2408a 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72  ain database str
2408b 75 63 74 75 72 65 20 2a 2f 0a 20 20 54 61 62 6c  ucture */.  Tabl
2408c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
2408d 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
2408e 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 20 20  o insert into.  
2408f 61 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63  aka TABLE */.  c
24090 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20  har *zTab;      
24091 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
24092 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  the table into w
24093 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65  hich we are inse
24094 72 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74  rting */.  const
24095 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20   char *zDb;     
24096 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
24097 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
24098 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
24099 20 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20   int i, j, idx; 
2409a 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2409b 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56 64 62  ounters */.  Vdb
2409c 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
2409d 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
2409e 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69  ode into this vi
2409f 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f  rtual machine */
240a0 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
240a1 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
240a2 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64  looping over ind
240a3 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ices of the tabl
240a4 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  e */.  int nColu
240a5 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  mn;          /* 
240a6 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
240a7 73 20 69 6e 20 74 68 65 20 64 61 74 61 20 2a 2f  s in the data */
240a8 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d  .  int nHidden =
240a9 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62   0;      /* Numb
240aa 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c  er of hidden col
240ab 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20 69 73  umns if TABLE is
240ac 20 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20 69 6e   virtual */.  in
240ad 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b 20 20  t baseCur = 0;  
240ae 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73      /* VDBE Curs
240af 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54  or number for pT
240b0 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 43  ab */.  int keyC
240b1 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f 2a  olumn = -1;   /*
240b2 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   Column that is 
240b3 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d  the INTEGER PRIM
240b4 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69 6e 74  ARY KEY */.  int
240b5 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20   endOfLoop;     
240b6 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20     /* Label for 
240b7 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
240b8 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f  nsertion loop */
240b9 0a 20 20 69 6e 74 20 75 73 65 54 65 6d 70 54 61  .  int useTempTa
240ba 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74 6f 72  ble = 0; /* Stor
240bb 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73  e SELECT results
240bc 20 69 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65   in intermediate
240bd 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
240be 73 72 63 54 61 62 20 3d 20 30 3b 20 20 20 20 20  srcTab = 0;     
240bf 20 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65 73 20    /* Data comes 
240c0 66 72 6f 6d 20 74 68 69 73 20 74 65 6d 70 6f 72  from this tempor
240c1 61 72 79 20 63 75 72 73 6f 72 20 69 66 20 3e 3d  ary cursor if >=
240c2 30 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 49  0 */.  int addrI
240c3 6e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f 2a 20  nsTop = 0;   /* 
240c4 4a 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20 22 44  Jump to label "D
240c5 22 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43  " */.  int addrC
240c6 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ont = 0;     /* 
240c7 54 6f 70 20 6f 66 20 69 6e 73 65 72 74 20 6c 6f  Top of insert lo
240c8 6f 70 2e 20 4c 61 62 65 6c 20 22 43 22 20 69 6e  op. Label "C" in
240c9 20 74 65 6d 70 6c 61 74 65 73 20 33 20 61 6e 64   templates 3 and
240ca 20 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72   4 */.  int addr
240cb 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 2f 2a  Select = 0;   /*
240cc 20 41 64 64 72 65 73 73 20 6f 66 20 63 6f 72 6f   Address of coro
240cd 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65  utine that imple
240ce 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43 54  ments the SELECT
240cf 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
240d0 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a 20 44   dest;      /* D
240d1 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20 53  estination for S
240d2 45 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f 66 20  ELECT on rhs of 
240d3 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20  INSERT */.  int 
240d4 6e 65 77 49 64 78 20 3d 20 2d 31 3b 20 20 20 20  newIdx = -1;    
240d5 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 6f 72 20    /* Cursor for 
240d6 74 68 65 20 4e 45 57 20 70 73 65 75 64 6f 2d 74  the NEW pseudo-t
240d7 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44  able */.  int iD
240d8 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
240d9 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61  /* Index of data
240da 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 54 41 42  base holding TAB
240db 4c 45 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b  LE */.  Db *pDb;
240dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
240dd 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
240de 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 62  ntaining table b
240df 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e  eing inserted in
240e0 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65  to */.  int appe
240e1 6e 64 46 6c 61 67 20 3d 20 30 3b 20 20 20 2f 2a  ndFlag = 0;   /*
240e2 20 54 72 75 65 20 69 66 20 74 68 65 20 69 6e 73   True if the ins
240e3 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f  ert is likely to
240e4 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f   be an append */
240e5 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
240e6 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  allocations */. 
240e7 20 69 6e 74 20 72 65 67 46 72 6f 6d 53 65 6c 65   int regFromSele
240e8 63 74 20 3d 20 30 3b 2f 2a 20 42 61 73 65 20 72  ct = 0;/* Base r
240e9 65 67 69 73 74 65 72 20 66 6f 72 20 64 61 74 61  egister for data
240ea 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 53 45 4c   coming from SEL
240eb 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ECT */.  int reg
240ec 41 75 74 6f 69 6e 63 20 3d 20 30 3b 20 20 20 2f  Autoinc = 0;   /
240ed 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
240ee 6e 67 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45  ng the AUTOINCRE
240ef 4d 45 4e 54 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  MENT counter */.
240f0 20 20 69 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e    int regRowCoun
240f1 74 20 3d 20 30 3b 20 20 2f 2a 20 4d 65 6d 6f 72  t = 0;  /* Memor
240f2 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20  y cell used for 
240f3 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65 72 20  the row counter 
240f4 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 6e 73 3b  */.  int regIns;
240f5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c             /* Bl
240f6 6f 63 6b 20 6f 66 20 72 65 67 73 20 68 6f 6c 64  ock of regs hold
240f7 69 6e 67 20 72 6f 77 69 64 2b 64 61 74 61 20 62  ing rowid+data b
240f8 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 2a 2f  eing inserted */
240f9 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b  .  int regRowid;
240fa 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67 69           /* regi
240fb 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 69 6e  sters holding in
240fc 73 65 72 74 20 72 6f 77 69 64 20 2a 2f 0a 20 20  sert rowid */.  
240fd 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20 20  int regData;    
240fe 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65        /* registe
240ff 72 20 68 6f 6c 64 69 6e 67 20 66 69 72 73 74 20  r holding first 
24100 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e 73 65 72 74  column to insert
24101 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63   */.  int regRec
24102 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 48  ord;        /* H
24103 6f 6c 64 73 20 74 68 65 20 61 73 73 65 6d 62 6c  olds the assembl
24104 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64 20 2a  ied row record *
24105 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 20 3d  /.  int regEof =
24106 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 67   0;       /* Reg
24107 69 73 74 65 72 20 72 65 63 6f 72 64 69 6e 67 20  ister recording 
24108 65 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 64 61  end of SELECT da
24109 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65  ta */.  int *aRe
2410a 67 49 64 78 20 3d 20 30 3b 20 20 20 20 20 2f 2a  gIdx = 0;     /*
2410b 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61 6c   One register al
2410c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 20  located to each 
2410d 69 6e 64 65 78 20 2a 2f 0a 0a 0a 23 69 66 6e 64  index */...#ifnd
2410e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2410f 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56  RIGGER.  int isV
24110 69 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  iew;            
24111 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
24112 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 69 6e  attempting to in
24113 73 65 72 74 20 69 6e 74 6f 20 61 20 76 69 65 77  sert into a view
24114 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
24115 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20  Trigger;        
24116 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69    /* List of tri
24117 67 67 65 72 73 20 6f 6e 20 70 54 61 62 2c 20 69  ggers on pTab, i
24118 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  f required */.  
24119 69 6e 74 20 74 6d 61 73 6b 3b 20 20 20 20 20 20  int tmask;      
2411a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
2411b 61 73 6b 20 6f 66 20 74 72 69 67 67 65 72 20 74  ask of trigger t
2411c 69 6d 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  imes */.#endif..
2411d 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2411e 62 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 65 73  b;.  memset(&des
2411f 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 65 73  t, 0, sizeof(des
24120 74 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  t));.  if( pPars
24121 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
24122 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
24123 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
24124 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
24125 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  * Locate the tab
24126 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65  le into which we
24127 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 69   will be inserti
24128 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74 69  ng new informati
24129 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  on..  */.  asser
2412a 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  t( pTabList->nSr
2412b 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61 62 20 3d  c==1 );.  zTab =
2412c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e   pTabList->a[0].
2412d 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 7a 54 61  zName;.  if( zTa
2412e 62 3d 3d 30 20 29 20 67 6f 74 6f 20 69 6e 73 65  b==0 ) goto inse
2412f 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70 54  rt_cleanup;.  pT
24130 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  ab = sqlite3SrcL
24131 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  istLookup(pParse
24132 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69  , pTabList);.  i
24133 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20  f( pTab==0 ){.  
24134 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
24135 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 44 62  eanup;.  }.  iDb
24136 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
24137 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
24138 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
24139 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44  sert( iDb<db->nD
2413a 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62  b );.  pDb = &db
2413b 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 7a 44  ->aDb[iDb];.  zD
2413c 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b 0a  b = pDb->zName;.
2413d 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
2413e 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
2413f 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 70 54  QLITE_INSERT, pT
24140 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44  ab->zName, 0, zD
24141 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69  b) ){.    goto i
24142 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
24143 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20   }..  /* Figure 
24144 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20 61  out if we have a
24145 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20  ny triggers and 
24146 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  if the table bei
24147 6e 67 0a 20 20 2a 2a 20 69 6e 73 65 72 74 65 64  ng.  ** inserted
24148 20 69 6e 74 6f 20 69 73 20 61 20 76 69 65 77 0a   into is a view.
24149 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
2414a 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2414b 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71  .  pTrigger = sq
2414c 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69  lite3TriggersExi
2414d 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  st(pParse, pTab,
2414e 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 26   TK_INSERT, 0, &
2414f 74 6d 61 73 6b 29 3b 0a 20 20 69 73 56 69 65 77  tmask);.  isView
24150 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74   = pTab->pSelect
24151 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  !=0;.#else.# def
24152 69 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23  ine pTrigger 0.#
24153 20 64 65 66 69 6e 65 20 74 6d 61 73 6b 20 30 0a   define tmask 0.
24154 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
24155 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  0.#endif.#ifdef 
24156 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
24157 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a  .# undef isView.
24158 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
24159 30 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72  0.#endif.  asser
2415a 74 28 20 28 70 54 72 69 67 67 65 72 20 26 26 20  t( (pTrigger && 
2415b 74 6d 61 73 6b 29 20 7c 7c 20 28 70 54 72 69 67  tmask) || (pTrig
2415c 67 65 72 3d 3d 30 20 26 26 20 74 6d 61 73 6b 3d  ger==0 && tmask=
2415d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 45 6e 73  =0) );..  /* Ens
2415e 75 72 65 20 74 68 61 74 3a 0a 20 20 2a 20 20 28  ure that:.  *  (
2415f 61 29 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  a) the table is 
24160 6e 6f 74 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 0a  not read-only, .
24161 20 20 2a 20 20 28 62 29 20 74 68 61 74 20 69 66    *  (b) that if
24162 20 69 74 20 69 73 20 61 20 76 69 65 77 20 74 68   it is a view th
24163 65 6e 20 4f 4e 20 49 4e 53 45 52 54 20 74 72 69  en ON INSERT tri
24164 67 67 65 72 73 20 65 78 69 73 74 0a 20 20 2a 2f  ggers exist.  */
24165 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73  .  if( sqlite3Is
24166 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c  ReadOnly(pParse,
24167 20 70 54 61 62 2c 20 74 6d 61 73 6b 29 20 29 7b   pTab, tmask) ){
24168 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
24169 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
2416a 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20  assert( pTab!=0 
2416b 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62  );..  /* If pTab
2416c 20 69 73 20 72 65 61 6c 6c 79 20 61 20 76 69 65   is really a vie
2416d 77 2c 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  w, make sure it 
2416e 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  has been initial
2416f 69 7a 65 64 2e 0a 20 20 2a 2a 20 56 69 65 77 47  ized..  ** ViewG
24170 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20  etColumnNames() 
24171 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 70 54  is a no-op if pT
24172 61 62 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77  ab is not a view
24173 20 28 6f 72 20 76 69 72 74 75 61 6c 20 0a 20 20   (or virtual .  
24174 2a 2a 20 6d 6f 64 75 6c 65 20 74 61 62 6c 65 29  ** module table)
24175 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
24176 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
24177 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
24178 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
24179 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
2417a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
2417b 61 74 65 20 61 20 56 44 42 45 0a 20 20 2a 2f 0a  ate a VDBE.  */.
2417c 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
2417d 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
2417e 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
2417f 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
24180 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
24181 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65  sted==0 ) sqlite
24182 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
24183 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  s(v);.  sqlite3B
24184 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
24185 6f 6e 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  on(pParse, pSele
24186 63 74 20 7c 7c 20 70 54 72 69 67 67 65 72 2c 20  ct || pTrigger, 
24187 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 74  iDb);..  /* if t
24188 68 65 72 65 20 61 72 65 20 72 6f 77 20 74 72 69  here are row tri
24189 67 67 65 72 73 2c 20 61 6c 6c 6f 63 61 74 65 20  ggers, allocate 
2418a 61 20 74 65 6d 70 20 74 61 62 6c 65 20 66 6f 72  a temp table for
2418b 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65   new.* reference
2418c 73 2e 20 2a 2f 0a 20 20 69 66 28 20 70 54 72 69  s. */.  if( pTri
2418d 67 67 65 72 20 29 7b 0a 20 20 20 20 6e 65 77 49  gger ){.    newI
2418e 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  dx = pParse->nTa
2418f 62 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  b++;.  }..#ifnde
24190 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46  f SQLITE_OMIT_XF
24191 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66 20 74  ER_OPT.  /* If t
24192 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
24193 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20 2a 2a  of the form.  **
24194 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 53 45  .  **       INSE
24195 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 31 3e  RT INTO <table1>
24196 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c   SELECT * FROM <
24197 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a 20 20  table2>;.  **.  
24198 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 61 6c 20  ** Then special 
24199 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 63 61  optimizations ca
2419a 6e 20 62 65 20 61 70 70 6c 69 65 64 20 74 68 61  n be applied tha
2419b 74 20 6d 61 6b 65 20 74 68 65 20 74 72 61 6e 73  t make the trans
2419c 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 20 66 61  fer.  ** very fa
2419d 73 74 20 61 6e 64 20 77 68 69 63 68 20 72 65 64  st and which red
2419e 75 63 65 20 66 72 61 67 6d 65 6e 74 61 74 69 6f  uce fragmentatio
2419f 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a 20 20  n of indices..  
241a0 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20  **.  ** This is 
241a1 74 68 65 20 32 6e 64 20 74 65 6d 70 6c 61 74 65  the 2nd template
241a2 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f  ..  */.  if( pCo
241a3 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65 72 4f  lumn==0 && xferO
241a4 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50 61 72  ptimization(pPar
241a5 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c 65 63  se, pTab, pSelec
241a6 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44 62 29  t, onError, iDb)
241a7 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
241a8 21 70 54 72 69 67 67 65 72 20 29 3b 0a 20 20 20  !pTrigger );.   
241a9 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 3d 3d   assert( pList==
241aa 30 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  0 );.    goto in
241ab 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
241ac 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
241ad 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54  TE_OMIT_XFER_OPT
241ae 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69   */..  /* If thi
241af 73 20 69 73 20 61 6e 20 41 55 54 4f 49 4e 43 52  s is an AUTOINCR
241b0 45 4d 45 4e 54 20 74 61 62 6c 65 2c 20 6c 6f 6f  EMENT table, loo
241b1 6b 20 75 70 20 74 68 65 20 73 65 71 75 65 6e 63  k up the sequenc
241b2 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a  e number in the.
241b3 20 20 2a 2a 20 73 71 6c 69 74 65 5f 73 65 71 75    ** sqlite_sequ
241b4 65 6e 63 65 20 74 61 62 6c 65 20 61 6e 64 20 73  ence table and s
241b5 74 6f 72 65 20 69 74 20 69 6e 20 6d 65 6d 6f 72  tore it in memor
241b6 79 20 63 65 6c 6c 20 72 65 67 41 75 74 6f 69 6e  y cell regAutoin
241b7 63 2e 0a 20 20 2a 2f 0a 20 20 72 65 67 41 75 74  c..  */.  regAut
241b8 6f 69 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65  oinc = autoIncBe
241b9 67 69 6e 28 70 50 61 72 73 65 2c 20 69 44 62 2c  gin(pParse, iDb,
241ba 20 70 54 61 62 29 3b 0a 0a 20 20 2f 2a 20 46 69   pTab);..  /* Fi
241bb 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
241bc 79 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74  y columns of dat
241bd 61 20 61 72 65 20 73 75 70 70 6c 69 65 64 2e 20  a are supplied. 
241be 20 49 66 20 74 68 65 20 64 61 74 61 0a 20 20 2a   If the data.  *
241bf 2a 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d  * is coming from
241c0 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
241c1 65 6e 74 2c 20 74 68 65 6e 20 67 65 6e 65 72 61  ent, then genera
241c2 74 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65 20  te a co-routine 
241c3 74 68 61 74 0a 20 20 2a 2a 20 70 72 6f 64 75 63  that.  ** produc
241c4 65 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  es a single row 
241c5 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 6f 6e  of the SELECT on
241c6 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e   each invocation
241c7 2e 20 20 54 68 65 0a 20 20 2a 2a 20 63 6f 2d 72  .  The.  ** co-r
241c8 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 63 6f  outine is the co
241c9 6d 6d 6f 6e 20 68 65 61 64 65 72 20 74 6f 20 74  mmon header to t
241ca 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20 74  he 3rd and 4th t
241cb 65 6d 70 6c 61 74 65 73 2e 0a 20 20 2a 2f 0a 20  emplates..  */. 
241cc 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
241cd 20 20 20 20 2f 2a 20 44 61 74 61 20 69 73 20 63      /* Data is c
241ce 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c  oming from a SEL
241cf 45 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63  ECT.  Generate c
241d0 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
241d1 20 74 68 61 74 20 53 45 4c 45 43 54 0a 20 20 20   that SELECT.   
241d2 20 2a 2a 20 61 73 20 61 20 63 6f 2d 72 6f 75 74   ** as a co-rout
241d3 69 6e 65 2e 20 20 54 68 65 20 63 6f 64 65 20 69  ine.  The code i
241d4 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 62 6f 74 68  s common to both
241d5 20 74 68 65 20 33 72 64 20 61 6e 64 20 34 74 68   the 3rd and 4th
241d6 0a 20 20 20 20 2a 2a 20 74 65 6d 70 6c 61 74 65  .    ** template
241d7 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s:.    **.    **
241d8 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20           EOF <- 
241d9 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  0.    **        
241da 20 58 20 3c 2d 20 41 0a 20 20 20 20 2a 2a 20 20   X <- A.    **  
241db 20 20 20 20 20 20 20 67 6f 74 6f 20 42 0a 20 20         goto B.  
241dc 20 20 2a 2a 20 20 20 20 20 20 41 3a 20 73 65 74    **      A: set
241dd 75 70 20 66 6f 72 20 74 68 65 20 53 45 4c 45 43  up for the SELEC
241de 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  T.    **        
241df 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 74   loop over the t
241e0 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53 45 4c  ables in the SEL
241e1 45 43 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ECT.    **      
241e2 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20       load value 
241e3 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e  into register R.
241e4 2e 52 2b 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20  .R+n.    **     
241e5 20 20 20 20 20 20 79 69 65 6c 64 20 58 0a 20 20        yield X.  
241e6 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64    **         end
241e7 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20 20   loop.    **    
241e8 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74       cleanup aft
241e9 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20  er the SELECT.  
241ea 20 20 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46    **         EOF
241eb 20 3c 2d 20 31 0a 20 20 20 20 2a 2a 20 20 20 20   <- 1.    **    
241ec 20 20 20 20 20 79 69 65 6c 64 20 58 0a 20 20 20       yield X.   
241ed 20 2a 2a 20 20 20 20 20 20 20 20 20 68 61 6c 74   **         halt
241ee 2d 65 72 72 6f 72 0a 20 20 20 20 2a 2a 0a 20 20  -error.    **.  
241ef 20 20 2a 2a 20 4f 6e 20 65 61 63 68 20 69 6e 76    ** On each inv
241f0 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  ocation of the c
241f1 6f 2d 72 6f 75 74 69 6e 65 2c 20 69 74 20 70 75  o-routine, it pu
241f2 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  ts a single row 
241f3 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 53 45  of the.    ** SE
241f4 4c 45 43 54 20 72 65 73 75 6c 74 20 69 6e 74 6f  LECT result into
241f5 20 72 65 67 69 73 74 65 72 73 20 64 65 73 74 2e   registers dest.
241f6 69 4d 65 6d 2e 2e 2e 64 65 73 74 2e 69 4d 65 6d  iMem...dest.iMem
241f7 2b 64 65 73 74 2e 6e 4d 65 6d 2d 31 2e 0a 20 20  +dest.nMem-1..  
241f8 20 20 2a 2a 20 28 54 68 65 73 65 20 6f 75 74 70    ** (These outp
241f9 75 74 20 72 65 67 69 73 74 65 72 73 20 61 72 65  ut registers are
241fa 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71   allocated by sq
241fb 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 29 20  lite3Select().) 
241fc 20 57 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65   When.    ** the
241fd 20 53 45 4c 45 43 54 20 63 6f 6d 70 6c 65 74 65   SELECT complete
241fe 73 2c 20 69 74 20 73 65 74 73 20 74 68 65 20 45  s, it sets the E
241ff 4f 46 20 66 6c 61 67 20 73 74 6f 72 65 64 20 69  OF flag stored i
24200 6e 20 72 65 67 45 6f 66 2e 0a 20 20 20 20 2a 2f  n regEof..    */
24201 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 6a 31 3b  .    int rc, j1;
24202 0a 0a 20 20 20 20 72 65 67 45 6f 66 20 3d 20 2b  ..    regEof = +
24203 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
24204 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24205 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
24206 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 29 3b 20  er, 0, regEof); 
24207 20 20 20 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 30       /* EOF <- 0
24208 20 2a 2f 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d   */.    VdbeComm
24209 65 6e 74 28 28 76 2c 20 22 53 45 4c 45 43 54 20  ent((v, "SELECT 
2420a 65 6f 66 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  eof flag"));.   
2420b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2420c 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52  stInit(&dest, SR
2420d 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 2b 2b 70  T_Coroutine, ++p
2420e 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  Parse->nMem);.  
2420f 20 20 61 64 64 72 53 65 6c 65 63 74 20 3d 20 73    addrSelect = s
24210 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
24211 74 41 64 64 72 28 76 29 2b 32 3b 0a 20 20 20 20  tAddr(v)+2;.    
24212 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24213 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
24214 20 61 64 64 72 53 65 6c 65 63 74 2d 31 2c 20 64   addrSelect-1, d
24215 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20  est.iParm);.    
24216 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
24217 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
24218 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 56 64  o, 0, 0);.    Vd
24219 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a  beComment((v, "J
2421a 75 6d 70 20 6f 76 65 72 20 53 45 4c 45 43 54 20  ump over SELECT 
2421b 63 6f 72 6f 75 74 69 6e 65 22 29 29 3b 0a 0a 20  coroutine"));.. 
2421c 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68     /* Resolve th
2421d 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  e expressions in
2421e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2421f 65 6d 65 6e 74 20 61 6e 64 20 65 78 65 63 75 74  ement and execut
24220 65 20 69 74 2e 20 2a 2f 0a 20 20 20 20 72 63 20  e it. */.    rc 
24221 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
24222 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c  pParse, pSelect,
24223 20 26 64 65 73 74 29 3b 0a 20 20 20 20 69 66 28   &dest);.    if(
24224 20 72 63 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e   rc || pParse->n
24225 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
24226 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
24227 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
24228 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20  anup;.    }.    
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 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2422b 20 31 2c 20 72 65 67 45 6f 66 29 3b 20 20 20 20   1, regEof);    
2422c 20 20 20 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 31       /* EOF <- 1
2422d 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
2422e 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2422f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72  Yield, dest.iPar
24230 6d 29 3b 20 20 20 2f 2a 20 79 69 65 6c 64 20 58  m);   /* yield X
24231 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
24232 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24233 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 49 4e 54  Halt, SQLITE_INT
24234 45 52 4e 41 4c 2c 20 4f 45 5f 41 62 6f 72 74 29  ERNAL, OE_Abort)
24235 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
24236 74 28 28 76 2c 20 22 45 6e 64 20 6f 66 20 53 45  t((v, "End of SE
24237 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65 22 29  LECT coroutine")
24238 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
24239 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
2423a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
2423b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c              /* l
2423c 61 62 65 6c 20 42 3a 20 2a 2f 0a 0a 20 20 20 20  abel B: */..    
2423d 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20 3d 20  regFromSelect = 
2423e 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 20 20 61  dest.iMem;.    a
2423f 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d 3e  ssert( pSelect->
24240 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 6e 43  pEList );.    nC
24241 6f 6c 75 6d 6e 20 3d 20 70 53 65 6c 65 63 74 2d  olumn = pSelect-
24242 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a  >pEList->nExpr;.
24243 20 20 20 20 61 73 73 65 72 74 28 20 64 65 73 74      assert( dest
24244 2e 6e 4d 65 6d 3d 3d 6e 43 6f 6c 75 6d 6e 20 29  .nMem==nColumn )
24245 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 73  ;..    /* Set us
24246 65 54 65 6d 70 54 61 62 6c 65 20 74 6f 20 54 52  eTempTable to TR
24247 55 45 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  UE if the result
24248 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
24249 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20  tatement.    ** 
2424a 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65  should be writte
2424b 6e 20 69 6e 74 6f 20 61 20 74 65 6d 70 6f 72 61  n into a tempora
2424c 72 79 20 74 61 62 6c 65 20 28 74 65 6d 70 6c 61  ry table (templa
2424d 74 65 20 34 29 2e 20 20 53 65 74 20 74 6f 0a 20  te 4).  Set to. 
2424e 20 20 20 2a 2a 20 46 41 4c 53 45 20 69 66 20 65     ** FALSE if e
2424f 61 63 68 2a 20 72 6f 77 20 6f 66 20 74 68 65 20  ach* row of the 
24250 53 45 4c 45 43 54 20 63 61 6e 20 62 65 20 77 72  SELECT can be wr
24251 69 74 74 65 6e 20 64 69 72 65 63 74 6c 79 20 69  itten directly i
24252 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64  nto.    ** the d
24253 65 73 74 69 6e 61 74 69 6f 6e 20 74 61 62 6c 65  estination table
24254 20 28 74 65 6d 70 6c 61 74 65 20 33 29 2e 0a 20   (template 3).. 
24255 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 74     **.    ** A t
24256 65 6d 70 20 74 61 62 6c 65 20 6d 75 73 74 20 62  emp table must b
24257 65 20 75 73 65 64 20 69 66 20 74 68 65 20 74 61  e used if the ta
24258 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74 65  ble being update
24259 64 20 69 73 20 61 6c 73 6f 20 6f 6e 65 0a 20 20  d is also one.  
2425a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c    ** of the tabl
2425b 65 73 20 62 65 69 6e 67 20 72 65 61 64 20 62 79  es being read by
2425c 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2425d 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 75 73 65  ement.  Also use
2425e 20 61 20 0a 20 20 20 20 2a 2a 20 74 65 6d 70 20   a .    ** temp 
2425f 74 61 62 6c 65 20 69 6e 20 74 68 65 20 63 61 73  table in the cas
24260 65 20 6f 66 20 72 6f 77 20 74 72 69 67 67 65 72  e of row trigger
24261 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
24262 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20 72 65  ( pTrigger || re
24263 61 64 73 54 61 62 6c 65 28 76 2c 20 61 64 64 72  adsTable(v, addr
24264 53 65 6c 65 63 74 2c 20 69 44 62 2c 20 70 54 61  Select, iDb, pTa
24265 62 29 20 29 7b 0a 20 20 20 20 20 20 75 73 65 54  b) ){.      useT
24266 65 6d 70 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20  empTable = 1;.  
24267 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 75 73 65    }..    if( use
24268 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
24269 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65     /* Invoke the
2426a 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 78   coroutine to ex
2426b 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f  tract informatio
2426c 6e 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43  n from the SELEC
2426d 54 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61  T.      ** and a
2426e 64 64 20 69 74 20 74 6f 20 61 20 74 72 61 6e 73  dd it to a trans
2426f 69 65 6e 74 20 74 61 62 6c 65 20 73 72 63 54 61  ient table srcTa
24270 62 2e 20 20 54 68 65 20 63 6f 64 65 20 67 65 6e  b.  The code gen
24271 65 72 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20  erated.      ** 
24272 68 65 72 65 20 69 73 20 66 72 6f 6d 20 74 68 65  here is from the
24273 20 34 74 68 20 74 65 6d 70 6c 61 74 65 3a 0a 20   4th template:. 
24274 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
24275 20 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65        B: open te
24276 6d 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a  mp table.      *
24277 2a 20 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20  *      L: yield 
24278 58 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  X.      **      
24279 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d     if EOF goto M
2427a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
2427b 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f    insert row fro
2427c 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65  m R..R+n into te
2427d 6d 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a  mp table.      *
2427e 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 4c  *         goto L
2427f 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 4d  .      **      M
24280 3a 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  : ....      */. 
24281 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b       int regRec;
24282 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
24283 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 70 61  ister to hold pa
24284 63 6b 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20  cked record */. 
24285 20 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70       int regTemp
24286 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 52 65 67  Rowid;    /* Reg
24287 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 65  ister to hold te
24288 6d 70 20 74 61 62 6c 65 20 52 4f 57 49 44 20 2a  mp table ROWID *
24289 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  /.      int addr
2428a 54 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Top;         /* 
2428b 4c 61 62 65 6c 20 22 4c 22 20 2a 2f 0a 20 20 20  Label "L" */.   
2428c 20 20 20 69 6e 74 20 61 64 64 72 49 66 3b 20 20     int addrIf;  
2428d 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2428e 73 73 20 6f 66 20 6a 75 6d 70 20 74 6f 20 4d 20  ss of jump to M 
2428f 2a 2f 0a 0a 20 20 20 20 20 20 73 72 63 54 61 62  */..      srcTab
24290 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
24291 2b 3b 0a 20 20 20 20 20 20 72 65 67 52 65 63 20  +;.      regRec 
24292 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
24293 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
24294 20 20 20 72 65 67 54 65 6d 70 52 6f 77 69 64 20     regTempRowid 
24295 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
24296 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
24297 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24298 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
24299 70 68 65 6d 65 72 61 6c 2c 20 73 72 63 54 61 62  phemeral, srcTab
2429a 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
2429b 20 20 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69    addrTop = sqli
2429c 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2429d 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e   OP_Yield, dest.
2429e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 61 64  iParm);.      ad
2429f 64 72 49 66 20 3d 20 73 71 6c 69 74 65 33 56 64  drIf = sqlite3Vd
242a0 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
242a1 66 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20 20 20  f, regEof);.    
242a2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
242a3 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
242a4 63 6f 72 64 2c 20 72 65 67 46 72 6f 6d 53 65 6c  cord, regFromSel
242a5 65 63 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65  ect, nColumn, re
242a6 67 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c  gRec);.      sql
242a7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
242a8 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 73  , OP_NewRowid, s
242a9 72 63 54 61 62 2c 20 72 65 67 54 65 6d 70 52 6f  rcTab, regTempRo
242aa 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  wid);.      sqli
242ab 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
242ac 20 4f 50 5f 49 6e 73 65 72 74 2c 20 73 72 63 54   OP_Insert, srcT
242ad 61 62 2c 20 72 65 67 52 65 63 2c 20 72 65 67 54  ab, regRec, regT
242ae 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  empRowid);.     
242af 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
242b0 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
242b1 2c 20 61 64 64 72 54 6f 70 29 3b 0a 20 20 20 20  , addrTop);.    
242b2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
242b3 70 48 65 72 65 28 76 2c 20 61 64 64 72 49 66 29  pHere(v, addrIf)
242b4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
242b5 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
242b6 61 72 73 65 2c 20 72 65 67 52 65 63 29 3b 0a 20  arse, regRec);. 
242b7 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
242b8 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
242b9 65 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29  e, regTempRowid)
242ba 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
242bb 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20  .    /* This is 
242bc 74 68 65 20 63 61 73 65 20 69 66 20 74 68 65 20  the case if the 
242bd 64 61 74 61 20 66 6f 72 20 74 68 65 20 49 4e 53  data for the INS
242be 45 52 54 20 69 73 20 63 6f 6d 69 6e 67 20 66 72  ERT is coming fr
242bf 6f 6d 20 61 20 56 41 4c 55 45 53 0a 20 20 20 20  om a VALUES.    
242c0 2a 2a 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f  ** clause.    */
242c1 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  .    NameContext
242c2 20 73 4e 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74   sNC;.    memset
242c3 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
242c4 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e  (sNC));.    sNC.
242c5 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
242c6 0a 20 20 20 20 73 72 63 54 61 62 20 3d 20 2d 31  .    srcTab = -1
242c7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 73  ;.    assert( us
242c8 65 54 65 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b  eTempTable==0 );
242c9 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70  .    nColumn = p
242ca 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45  List ? pList->nE
242cb 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 66 6f 72  xpr : 0;.    for
242cc 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b  (i=0; i<nColumn;
242cd 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
242ce 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
242cf 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
242d0 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
242d1 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  ) ){.        got
242d2 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
242d3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
242d4 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
242d5 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ure the number o
242d6 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
242d7 20 73 6f 75 72 63 65 20 64 61 74 61 20 6d 61 74   source data mat
242d8 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 0a  ches the number.
242d9 20 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20    ** of columns 
242da 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69  to be inserted i
242db 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 20  nto the table.. 
242dc 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74   */.  if( IsVirt
242dd 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
242de 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
242df 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
242e0 20 20 20 20 6e 48 69 64 64 65 6e 20 2b 3d 20 28      nHidden += (
242e1 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26  IsHiddenColumn(&
242e2 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f  pTab->aCol[i]) ?
242e3 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20   1 : 0);.    }. 
242e4 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e   }.  if( pColumn
242e5 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26  ==0 && nColumn &
242e6 26 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62  & nColumn!=(pTab
242e7 2d 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20  ->nCol-nHidden) 
242e8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
242e9 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
242ea 20 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 53         "table %S
242eb 20 68 61 73 20 25 64 20 63 6f 6c 75 6d 6e 73 20   has %d columns 
242ec 62 75 74 20 25 64 20 76 61 6c 75 65 73 20 77 65  but %d values we
242ed 72 65 20 73 75 70 70 6c 69 65 64 22 2c 0a 20 20  re supplied",.  
242ee 20 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30       pTabList, 0
242ef 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 6e 43  , pTab->nCol, nC
242f0 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 67 6f 74 6f  olumn);.    goto
242f1 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
242f2 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75  .  }.  if( pColu
242f3 6d 6e 21 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e  mn!=0 && nColumn
242f4 21 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29  !=pColumn->nId )
242f5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
242f6 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
242f7 64 20 76 61 6c 75 65 73 20 66 6f 72 20 25 64 20  d values for %d 
242f8 63 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d  columns", nColum
242f9 6e 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29  n, pColumn->nId)
242fa 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  ;.    goto inser
242fb 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  t_cleanup;.  }..
242fc 20 20 2f 2a 20 49 66 20 74 68 65 20 49 4e 53 45    /* If the INSE
242fd 52 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 63  RT statement inc
242fe 6c 75 64 65 64 20 61 6e 20 49 44 4c 49 53 54 20  luded an IDLIST 
242ff 74 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b 65 20  term, then make 
24300 73 75 72 65 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c  sure.  ** all el
24301 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 49 44  ements of the ID
24302 4c 49 53 54 20 72 65 61 6c 6c 79 20 61 72 65 20  LIST really are 
24303 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74  columns of the t
24304 61 62 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72  able and .  ** r
24305 65 6d 65 6d 62 65 72 20 74 68 65 20 63 6f 6c 75  emember the colu
24306 6d 6e 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a  mn indices..  **
24307 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 74 61 62  .  ** If the tab
24308 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45  le has an INTEGE
24309 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  R PRIMARY KEY co
2430a 6c 75 6d 6e 20 61 6e 64 20 74 68 61 74 20 63 6f  lumn and that co
2430b 6c 75 6d 6e 0a 20 20 2a 2a 20 69 73 20 6e 61 6d  lumn.  ** is nam
2430c 65 64 20 69 6e 20 74 68 65 20 49 44 4c 49 53 54  ed in the IDLIST
2430d 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 20 69 6e  , then record in
2430e 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76   the keyColumn v
2430f 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20 74 68 65  ariable.  ** the
24310 20 69 6e 64 65 78 20 69 6e 74 6f 20 49 44 4c 49   index into IDLI
24311 53 54 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72  ST of the primar
24312 79 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b  y key column.  k
24313 65 79 43 6f 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a  eyColumn is.  **
24314 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
24315 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 73  e primary key as
24316 20 69 74 20 61 70 70 65 61 72 73 20 69 6e 20 49   it appears in I
24317 44 4c 49 53 54 2c 20 6e 6f 74 20 61 73 0a 20 20  DLIST, not as.  
24318 2a 2a 20 69 73 20 61 70 70 65 61 72 73 20 69 6e  ** is appears in
24319 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61   the original ta
2431a 62 6c 65 2e 20 20 28 54 68 65 20 69 6e 64 65 78  ble.  (The index
2431b 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 0a   of the primary.
2431c 20 20 2a 2a 20 6b 65 79 20 69 6e 20 74 68 65 20    ** key in the 
2431d 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20 69  original table i
2431e 73 20 70 54 61 62 2d 3e 69 50 4b 65 79 2e 29 0a  s pTab->iPKey.).
2431f 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75    */.  if( pColu
24320 6d 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  mn ){.    for(i=
24321 30 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49  0; i<pColumn->nI
24322 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70  d; i++){.      p
24323 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78  Column->a[i].idx
24324 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20   = -1;.    }.   
24325 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c   for(i=0; i<pCol
24326 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a  umn->nId; i++){.
24327 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
24328 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  <pTab->nCol; j++
24329 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
2432a 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
2432b 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
2432c 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  e, pTab->aCol[j]
2432d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20  .zName)==0 ){.  
2432e 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d          pColumn-
2432f 3e 61 5b 69 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20  >a[i].idx = j;. 
24330 20 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d           if( j==
24331 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
24332 20 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f             keyCo
24333 6c 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20  lumn = i;.      
24334 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
24335 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
24336 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
24337 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c  f( j>=pTab->nCol
24338 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
24339 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70  sqlite3IsRowid(p
2433a 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61  Column->a[i].zNa
2433b 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  me) ){.         
2433c 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a   keyColumn = i;.
2433d 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2433e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2433f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
24340 20 22 74 61 62 6c 65 20 25 53 20 68 61 73 20 6e   "table %S has n
24341 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25  o column named %
24342 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  s",.            
24343 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70    pTabList, 0, p
24344 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61  Column->a[i].zNa
24345 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  me);.          p
24346 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
24347 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e           goto in
24348 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
24349 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2434a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2434b 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 49  If there is no I
2434c 44 4c 49 53 54 20 74 65 72 6d 20 62 75 74 20 74  DLIST term but t
2434d 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20  he table has an 
2434e 69 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 0a  integer primary.
2434f 20 20 2a 2a 20 6b 65 79 2c 20 74 68 65 20 73 65    ** key, the se
24350 74 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20  t the keyColumn 
24351 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20  variable to the 
24352 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75  primary key colu
24353 6d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69 6e  mn index.  ** in
24354 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61   the original ta
24355 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a  ble definition..
24356 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75    */.  if( pColu
24357 6d 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e  mn==0 && nColumn
24358 3e 30 20 29 7b 0a 20 20 20 20 6b 65 79 43 6f 6c  >0 ){.    keyCol
24359 75 6d 6e 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65  umn = pTab->iPKe
2435a 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65  y;.  }..  /* Ope
2435b 6e 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65  n the temp table
2435c 20 66 6f 72 20 46 4f 52 20 45 41 43 48 20 52 4f   for FOR EACH RO
2435d 57 20 74 72 69 67 67 65 72 73 0a 20 20 2a 2f 0a  W triggers.  */.
2435e 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29    if( pTrigger )
2435f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
24360 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  eAddOp3(v, OP_Op
24361 65 6e 50 73 65 75 64 6f 2c 20 6e 65 77 49 64 78  enPseudo, newIdx
24362 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  , 0, pTab->nCol)
24363 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20  ;.  }.    .  /* 
24364 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63  Initialize the c
24365 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 6f 20  ount of rows to 
24366 62 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f  be inserted.  */
24367 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
24368 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52   & SQLITE_CountR
24369 6f 77 73 20 29 7b 0a 20 20 20 20 72 65 67 52 6f  ows ){.    regRo
2436a 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73  wCount = ++pPars
2436b 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
2436c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2436d 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2436e 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20   regRowCount);. 
2436f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73   }..  /* If this
24370 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 2c 20   is not a view, 
24371 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61  open the table a
24372 6e 64 20 61 6e 64 20 61 6c 6c 20 69 6e 64 69 63  nd and all indic
24373 65 73 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 56  es */.  if( !isV
24374 69 65 77 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  iew ){.    int n
24375 49 64 78 3b 0a 0a 20 20 20 20 62 61 73 65 43 75  Idx;..    baseCu
24376 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
24377 3b 0a 20 20 20 20 6e 49 64 78 20 3d 20 73 71 6c  ;.    nIdx = sql
24378 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
24379 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20  Indices(pParse, 
2437a 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 4f  pTab, baseCur, O
2437b 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20  P_OpenWrite);.  
2437c 20 20 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69    aRegIdx = sqli
2437d 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
2437e 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28  b, sizeof(int)*(
2437f 6e 49 64 78 2b 31 29 29 3b 0a 20 20 20 20 69 66  nIdx+1));.    if
24380 28 20 61 52 65 67 49 64 78 3d 3d 30 20 29 7b 0a  ( aRegIdx==0 ){.
24381 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72        goto inser
24382 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  t_cleanup;.    }
24383 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
24384 6e 49 64 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nIdx; i++){.    
24385 20 20 61 52 65 67 49 64 78 5b 69 5d 20 3d 20 2b    aRegIdx[i] = +
24386 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
24387 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
24388 68 69 73 20 69 73 20 74 68 65 20 74 6f 70 20 6f  his is the top o
24389 66 20 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72  f the main inser
2438a 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69  tion loop */.  i
2438b 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
2438c 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62  ){.    /* This b
2438d 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20 74  lock codes the t
2438e 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e  op of loop only.
2438f 20 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c    The complete l
24390 6f 6f 70 20 69 73 20 74 68 65 0a 20 20 20 20 2a  oop is the.    *
24391 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75  * following pseu
24392 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74 65  docode (template
24393 20 34 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20   4):.    **.    
24394 2a 2a 20 20 20 20 20 20 20 20 20 72 65 77 69 6e  **         rewin
24395 64 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20 20  d temp table.   
24396 20 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70   **      C: loop
24397 20 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e   over rows of in
24398 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
24399 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2439a 20 20 74 72 61 6e 73 66 65 72 20 76 61 6c 75 65    transfer value
2439b 73 20 66 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69  s form intermedi
2439c 61 74 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c  ate table into <
2439d 74 61 62 6c 65 3e 0a 20 20 20 20 2a 2a 20 20 20  table>.    **   
2439e 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20        end loop. 
2439f 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e     **      D: ..
243a0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64  ..    */.    add
243a1 72 49 6e 73 54 6f 70 20 3d 20 73 71 6c 69 74 65  rInsTop = sqlite
243a2 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
243a3 50 5f 52 65 77 69 6e 64 2c 20 73 72 63 54 61 62  P_Rewind, srcTab
243a4 29 3b 0a 20 20 20 20 61 64 64 72 43 6f 6e 74 20  );.    addrCont 
243a5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
243a6 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 7d  rentAddr(v);.  }
243a7 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74  else if( pSelect
243a8 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
243a9 62 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20  block codes the 
243aa 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79  top of loop only
243ab 2e 20 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  .  The complete 
243ac 6c 6f 6f 70 20 69 73 20 74 68 65 0a 20 20 20 20  loop is the.    
243ad 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65  ** following pse
243ae 75 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74  udocode (templat
243af 65 20 33 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  e 3):.    **.   
243b0 20 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65 6c   **      C: yiel
243b1 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  d X.    **      
243b2 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44     if EOF goto D
243b3 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
243b4 69 6e 73 65 72 74 20 74 68 65 20 73 65 6c 65 63  insert the selec
243b5 74 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 3c 74  t result into <t
243b6 61 62 6c 65 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b  able> from R..R+
243b7 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  n.    **        
243b8 20 67 6f 74 6f 20 43 0a 20 20 20 20 2a 2a 20 20   goto C.    **  
243b9 20 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a      D: ....    *
243ba 2f 0a 20 20 20 20 61 64 64 72 43 6f 6e 74 20 3d  /.    addrCont =
243bb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
243bc 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
243bd 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20  dest.iParm);.   
243be 20 61 64 64 72 49 6e 73 54 6f 70 20 3d 20 73 71   addrInsTop = sq
243bf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
243c0 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
243c1 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c  );.  }..  /* All
243c2 6f 63 61 74 65 20 72 65 67 69 73 74 65 72 73 20  ocate registers 
243c3 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  for holding the 
243c4 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77  rowid of the new
243c5 20 72 6f 77 2c 0a 20 20 2a 2a 20 74 68 65 20 63   row,.  ** the c
243c6 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65  ontent of the ne
243c7 77 20 72 6f 77 2c 20 61 6e 64 20 74 68 65 20 61  w row, and the a
243c8 73 73 65 6d 62 6c 69 65 64 20 72 6f 77 20 72 65  ssemblied row re
243c9 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 72 65 67  cord..  */.  reg
243ca 52 65 63 6f 72 64 20 3d 20 2b 2b 70 50 61 72 73  Record = ++pPars
243cb 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 52 6f  e->nMem;.  regRo
243cc 77 69 64 20 3d 20 72 65 67 49 6e 73 20 3d 20 70  wid = regIns = p
243cd 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20  Parse->nMem+1;. 
243ce 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
243cf 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b   pTab->nCol + 1;
243d0 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
243d1 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 72 65  (pTab) ){.    re
243d2 67 52 6f 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50  gRowid++;.    pP
243d3 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20  arse->nMem++;.  
243d4 7d 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65  }.  regData = re
243d5 67 52 6f 77 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20  gRowid+1;..  /* 
243d6 52 75 6e 20 74 68 65 20 42 45 46 4f 52 45 20 61  Run the BEFORE a
243d7 6e 64 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  nd INSTEAD OF tr
243d8 69 67 67 65 72 73 2c 20 69 66 20 74 68 65 72 65  iggers, if there
243d9 20 61 72 65 20 61 6e 79 0a 20 20 2a 2f 0a 20 20   are any.  */.  
243da 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69  endOfLoop = sqli
243db 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
243dc 28 76 29 3b 0a 20 20 69 66 28 20 74 6d 61 73 6b  (v);.  if( tmask
243dd 20 26 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52   & TRIGGER_BEFOR
243de 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67  E ){.    int reg
243df 54 72 69 67 52 6f 77 69 64 3b 0a 20 20 20 20 69  TrigRowid;.    i
243e0 6e 74 20 72 65 67 43 6f 6c 73 3b 0a 20 20 20 20  nt regCols;.    
243e1 69 6e 74 20 72 65 67 52 65 63 3b 0a 0a 20 20 20  int regRec;..   
243e2 20 2f 2a 20 62 75 69 6c 64 20 74 68 65 20 4e 45   /* build the NE
243e3 57 2e 2a 20 72 65 66 65 72 65 6e 63 65 20 72 6f  W.* reference ro
243e4 77 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66  w.  Note that if
243e5 20 74 68 65 72 65 20 69 73 20 61 6e 20 49 4e 54   there is an INT
243e6 45 47 45 52 0a 20 20 20 20 2a 2a 20 50 52 49 4d  EGER.    ** PRIM
243e7 41 52 59 20 4b 45 59 20 69 6e 74 6f 20 77 68 69  ARY KEY into whi
243e8 63 68 20 61 20 4e 55 4c 4c 20 69 73 20 62 65 69  ch a NULL is bei
243e9 6e 67 20 69 6e 73 65 72 74 65 64 2c 20 74 68 61  ng inserted, tha
243ea 74 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 0a 20  t NULL will be. 
243eb 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 65 64     ** translated
243ec 20 69 6e 74 6f 20 61 20 75 6e 69 71 75 65 20 49   into a unique I
243ed 44 20 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 20  D for the row.  
243ee 42 75 74 20 6f 6e 20 61 20 42 45 46 4f 52 45 20  But on a BEFORE 
243ef 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20  trigger,.    ** 
243f0 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 77  we do not know w
243f1 68 61 74 20 74 68 65 20 75 6e 69 71 75 65 20 49  hat the unique I
243f2 44 20 77 69 6c 6c 20 62 65 20 28 62 65 63 61 75  D will be (becau
243f3 73 65 20 74 68 65 20 69 6e 73 65 72 74 20 68 61  se the insert ha
243f4 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 70  s.    ** not hap
243f5 70 65 6e 65 64 20 79 65 74 29 20 73 6f 20 77 65  pened yet) so we
243f6 20 73 75 62 73 74 69 74 75 74 65 20 61 20 72 6f   substitute a ro
243f7 77 69 64 20 6f 66 20 2d 31 0a 20 20 20 20 2a 2f  wid of -1.    */
243f8 0a 20 20 20 20 72 65 67 54 72 69 67 52 6f 77 69  .    regTrigRowi
243f9 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
243fa 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
243fb 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e     if( keyColumn
243fc 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  <0 ){.      sqli
243fd 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
243fe 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c   OP_Integer, -1,
243ff 20 72 65 67 54 72 69 67 52 6f 77 69 64 29 3b 0a   regTrigRowid);.
24400 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73      }else if( us
24401 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
24402 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24403 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
24404 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43  mn, srcTab, keyC
24405 6f 6c 75 6d 6e 2c 20 72 65 67 54 72 69 67 52 6f  olumn, regTrigRo
24406 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  wid);.    }else{
24407 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20  .      int j1;. 
24408 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65       assert( pSe
24409 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 4f  lect==0 );  /* O
2440a 74 68 65 72 77 69 73 65 20 75 73 65 54 65 6d 70  therwise useTemp
2440b 54 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f  Table is true */
2440c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2440d 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
2440e 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d  List->a[keyColum
2440f 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67 54 72 69  n].pExpr, regTri
24410 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 6a  gRowid);.      j
24411 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
24412 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e  ddOp1(v, OP_NotN
24413 75 6c 6c 2c 20 72 65 67 54 72 69 67 52 6f 77 69  ull, regTrigRowi
24414 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
24415 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24416 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72  P_Integer, -1, r
24417 65 67 54 72 69 67 52 6f 77 69 64 29 3b 0a 20 20  egTrigRowid);.  
24418 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
24419 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
2441a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2441b 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75  eAddOp1(v, OP_Mu
2441c 73 74 42 65 49 6e 74 2c 20 72 65 67 54 72 69 67  stBeInt, regTrig
2441d 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20  Rowid);.    }.. 
2441e 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76     /* Cannot hav
2441f 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20 61 20  e triggers on a 
24420 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 49  virtual table. I
24421 66 20 69 74 20 77 65 72 65 20 70 6f 73 73 69 62  f it were possib
24422 6c 65 2c 0a 20 20 20 20 2a 2a 20 74 68 69 73 20  le,.    ** this 
24423 62 6c 6f 63 6b 20 77 6f 75 6c 64 20 68 61 76 65  block would have
24424 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20   to account for 
24425 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 2e 0a 20  hidden column.. 
24426 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
24427 28 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  (!IsVirtual(pTab
24428 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61  ));..    /* Crea
24429 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d  te the new colum
2442a 6e 20 64 61 74 61 0a 20 20 20 20 2a 2f 0a 20 20  n data.    */.  
2442b 20 20 72 65 67 43 6f 6c 73 20 3d 20 73 71 6c 69    regCols = sqli
2442c 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
2442d 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43  pParse, pTab->nC
2442e 6f 6c 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  ol);.    for(i=0
2442f 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
24430 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
24431 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20  pColumn==0 ){.  
24432 20 20 20 20 20 20 6a 20 3d 20 69 3b 0a 20 20 20        j = i;.   
24433 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24434 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f    for(j=0; j<pCo
24435 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b  lumn->nId; j++){
24436 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
24437 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78  Column->a[j].idx
24438 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==i ) break;.   
24439 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2443a 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e       if( pColumn
2443b 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e   && j>=pColumn->
2443c 6e 49 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73  nId ){.        s
2443d 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2443e 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f  Parse, pTab->aCo
2443f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 72 65 67 43  l[i].pDflt, regC
24440 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 7d 65  ols+i);.      }e
24441 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d 70 54  lse if( useTempT
24442 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
24443 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24444 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
24445 73 72 63 54 61 62 2c 20 6a 2c 20 72 65 67 43 6f  srcTab, j, regCo
24446 6c 73 2b 69 29 3b 20 0a 20 20 20 20 20 20 7d 65  ls+i); .      }e
24447 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
24448 65 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20  ert( pSelect==0 
24449 29 3b 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  ); /* Otherwise 
2444a 75 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20  useTempTable is 
2444b 74 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  true */.        
2444c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41  sqlite3ExprCodeA
2444d 6e 64 43 61 63 68 65 28 70 50 61 72 73 65 2c 20  ndCache(pParse, 
2444e 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70  pList->a[j].pExp
2444f 72 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20  r, regCols+i);. 
24450 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
24451 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74 65   regRec = sqlite
24452 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
24453 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  se);.    sqlite3
24454 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
24455 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
24456 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  Cols, pTab->nCol
24457 2c 20 72 65 67 52 65 63 29 3b 0a 0a 20 20 20 20  , regRec);..    
24458 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
24459 20 49 4e 53 45 52 54 20 6f 6e 20 61 20 76 69 65   INSERT on a vie
2445a 77 20 77 69 74 68 20 61 6e 20 49 4e 53 54 45 41  w with an INSTEA
2445b 44 20 4f 46 20 49 4e 53 45 52 54 20 74 72 69 67  D OF INSERT trig
2445c 67 65 72 2c 0a 20 20 20 20 2a 2a 20 64 6f 20 6e  ger,.    ** do n
2445d 6f 74 20 61 74 74 65 6d 70 74 20 61 6e 79 20 63  ot attempt any c
2445e 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 66 6f 72  onversions befor
2445f 65 20 61 73 73 65 6d 62 6c 69 6e 67 20 74 68 65  e assembling the
24460 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2a 20   record..    ** 
24461 49 66 20 74 68 69 73 20 69 73 20 61 20 72 65 61  If this is a rea
24462 6c 20 74 61 62 6c 65 2c 20 61 74 74 65 6d 70 74  l table, attempt
24463 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20   conversions as 
24464 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 0a  required by the.
24465 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 63 6f 6c      ** table col
24466 75 6d 6e 20 61 66 66 69 6e 69 74 69 65 73 2e 0a  umn affinities..
24467 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
24468 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20  isView ){.      
24469 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69  sqlite3TableAffi
2446a 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61 62 29  nityStr(v, pTab)
2446b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2446c 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2446d 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77 49   OP_Insert, newI
2446e 64 78 2c 20 72 65 67 52 65 63 2c 20 72 65 67 54  dx, regRec, regT
2446f 72 69 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  rigRowid);.    s
24470 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
24471 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
24472 52 65 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Rec);.    sqlite
24473 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
24474 70 50 61 72 73 65 2c 20 72 65 67 54 72 69 67 52  pParse, regTrigR
24475 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
24476 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
24477 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 43 6f  ge(pParse, regCo
24478 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b  ls, pTab->nCol);
24479 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 42 45  ..    /* Fire BE
2447a 46 4f 52 45 20 6f 72 20 49 4e 53 54 45 41 44 20  FORE or INSTEAD 
2447b 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  OF triggers */. 
2447c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f     if( sqlite3Co
2447d 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  deRowTrigger(pPa
2447e 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54  rse, pTrigger, T
2447f 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52 49  K_INSERT, 0, TRI
24480 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 0a 20 20  GGER_BEFORE, .  
24481 20 20 20 20 20 20 70 54 61 62 2c 20 6e 65 77 49        pTab, newI
24482 64 78 2c 20 2d 31 2c 20 6f 6e 45 72 72 6f 72 2c  dx, -1, onError,
24483 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 30 2c 20 30   endOfLoop, 0, 0
24484 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
24485 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
24486 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
24487 50 75 73 68 20 74 68 65 20 72 65 63 6f 72 64 20  Push the record 
24488 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e  number for the n
24489 65 77 20 65 6e 74 72 79 20 6f 6e 74 6f 20 74 68  ew entry onto th
2448a 65 20 73 74 61 63 6b 2e 20 20 54 68 65 0a 20 20  e stack.  The.  
2448b 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ** record number
2448c 20 69 73 20 61 20 72 61 6e 64 6f 6d 6c 79 20 67   is a randomly g
2448d 65 6e 65 72 61 74 65 20 69 6e 74 65 67 65 72 20  enerate integer 
2448e 63 72 65 61 74 65 64 20 62 79 20 4e 65 77 52 6f  created by NewRo
2448f 77 69 64 0a 20 20 2a 2a 20 65 78 63 65 70 74 20  wid.  ** except 
24490 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 68  when the table h
24491 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  as an INTEGER PR
24492 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e  IMARY KEY column
24493 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 2a 2a 20  , in which.  ** 
24494 63 61 73 65 20 74 68 65 20 72 65 63 6f 72 64 20  case the record 
24495 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 73 61  number is the sa
24496 6d 65 20 61 73 20 74 68 61 74 20 63 6f 6c 75 6d  me as that colum
24497 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  n. .  */.  if( !
24498 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69 66  isView ){.    if
24499 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
2449a 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  ) ){.      /* Th
2449b 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20 56  e row that the V
2449c 55 70 64 61 74 65 20 6f 70 63 6f 64 65 20 77 69  Update opcode wi
2449d 6c 6c 20 64 65 6c 65 74 65 3a 20 6e 6f 6e 65 20  ll delete: none 
2449e 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2449f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
244a0 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 49 6e 73  _Null, 0, regIns
244a1 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
244a2 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b   keyColumn>=0 ){
244a3 0a 20 20 20 20 20 20 69 66 28 20 75 73 65 54 65  .      if( useTe
244a4 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  mpTable ){.     
244a5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
244a6 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
244a7 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f  n, srcTab, keyCo
244a8 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b  lumn, regRowid);
244a9 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
244aa 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
244ab 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
244ac 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
244ad 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74  y, regFromSelect
244ae 2b 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52  +keyColumn, regR
244af 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c  owid);.      }el
244b0 73 65 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65  se{.        Vdbe
244b1 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 20  Op *pOp;.       
244b2 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
244b3 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e  (pParse, pList->
244b4 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78  a[keyColumn].pEx
244b5 70 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  pr, regRowid);. 
244b6 20 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c         pOp = sql
244b7 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
244b8 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
244b9 65 6e 74 41 64 64 72 28 76 29 20 2d 20 31 29 3b  entAddr(v) - 1);
244ba 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 70  .        if( pOp
244bb 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d   && pOp->opcode=
244bc 3d 4f 50 5f 4e 75 6c 6c 20 26 26 20 21 49 73 56  =OP_Null && !IsV
244bd 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
244be 20 20 20 20 20 20 20 20 20 20 61 70 70 65 6e 64            append
244bf 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  Flag = 1;.      
244c0 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20      pOp->opcode 
244c1 3d 20 4f 50 5f 4e 65 77 52 6f 77 69 64 3b 0a 20  = OP_NewRowid;. 
244c2 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31           pOp->p1
244c3 20 3d 20 62 61 73 65 43 75 72 3b 0a 20 20 20 20   = baseCur;.    
244c4 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20        pOp->p2 = 
244c5 72 65 67 52 6f 77 69 64 3b 0a 20 20 20 20 20 20  regRowid;.      
244c6 20 20 20 20 70 4f 70 2d 3e 70 33 20 3d 20 72 65      pOp->p3 = re
244c7 67 41 75 74 6f 69 6e 63 3b 0a 20 20 20 20 20 20  gAutoinc;.      
244c8 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
244c9 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 49 4d    /* If the PRIM
244ca 41 52 59 20 4b 45 59 20 65 78 70 72 65 73 73 69  ARY KEY expressi
244cb 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  on is NULL, then
244cc 20 75 73 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64   use OP_NewRowid
244cd 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 67 65 6e  .      ** to gen
244ce 65 72 61 74 65 20 61 20 75 6e 69 71 75 65 20 70  erate a unique p
244cf 72 69 6d 61 72 79 20 6b 65 79 20 76 61 6c 75 65  rimary key value
244d0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
244d1 20 69 66 28 20 21 61 70 70 65 6e 64 46 6c 61 67   if( !appendFlag
244d2 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
244d3 6a 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  j1;.        if( 
244d4 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
244d5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6a 31   ){.          j1
244d6 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
244d7 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75  dOp1(v, OP_NotNu
244d8 6c 6c 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  ll, regRowid);. 
244d9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
244da 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
244db 5f 4e 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43  _NewRowid, baseC
244dc 75 72 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65  ur, regRowid, re
244dd 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20  gAutoinc);.     
244de 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
244df 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
244e0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
244e1 20 20 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73            j1 = s
244e2 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
244e3 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
244e4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
244e5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp2(v, OP_IsNu
244e6 6c 6c 2c 20 72 65 67 52 6f 77 69 64 2c 20 6a 31  ll, regRowid, j1
244e7 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  +2);.        }. 
244e8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
244e9 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d  beAddOp1(v, OP_M
244ea 75 73 74 42 65 49 6e 74 2c 20 72 65 67 52 6f 77  ustBeInt, regRow
244eb 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  id);.      }.   
244ec 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69 72   }else if( IsVir
244ed 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
244ee 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
244ef 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
244f0 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  , 0, regRowid);.
244f1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
244f2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
244f3 70 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p3(v, OP_NewRowi
244f4 64 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52  d, baseCur, regR
244f5 6f 77 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63  owid, regAutoinc
244f6 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64 46  );.      appendF
244f7 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  lag = 1;.    }. 
244f8 20 20 20 61 75 74 6f 49 6e 63 53 74 65 70 28 70     autoIncStep(p
244f9 50 61 72 73 65 2c 20 72 65 67 41 75 74 6f 69 6e  Parse, regAutoin
244fa 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20  c, regRowid);.. 
244fb 20 20 20 2f 2a 20 50 75 73 68 20 6f 6e 74 6f 20     /* Push onto 
244fc 74 68 65 20 73 74 61 63 6b 2c 20 64 61 74 61 20  the stack, data 
244fd 66 6f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20  for all columns 
244fe 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79  of the new entry
244ff 2c 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20  , beginning.    
24500 2a 2a 20 77 69 74 68 20 74 68 65 20 66 69 72 73  ** with the firs
24501 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f  t column..    */
24502 0a 20 20 20 20 6e 48 69 64 64 65 6e 20 3d 20 30  .    nHidden = 0
24503 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
24504 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
24505 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65  ){.      int iRe
24506 67 53 74 6f 72 65 20 3d 20 72 65 67 52 6f 77 69  gStore = regRowi
24507 64 2b 31 2b 69 3b 0a 20 20 20 20 20 20 69 66 28  d+1+i;.      if(
24508 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20   i==pTab->iPKey 
24509 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
2450a 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 49  e value of the I
2450b 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
2450c 45 59 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6c 77  EY column is alw
2450d 61 79 73 20 61 20 4e 55 4c 4c 2e 0a 20 20 20 20  ays a NULL..    
2450e 20 20 20 20 2a 2a 20 57 68 65 6e 65 76 65 72 20      ** Whenever 
2450f 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 72  this column is r
24510 65 61 64 2c 20 74 68 65 20 72 65 63 6f 72 64 20  ead, the record 
24511 6e 75 6d 62 65 72 20 77 69 6c 6c 20 62 65 20 73  number will be s
24512 75 62 73 74 69 74 75 74 65 64 0a 20 20 20 20 20  ubstituted.     
24513 20 20 20 2a 2a 20 69 6e 20 69 74 73 20 70 6c 61     ** in its pla
24514 63 65 2e 20 20 53 6f 20 77 69 6c 6c 20 66 69 6c  ce.  So will fil
24515 6c 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 77 69  l this column wi
24516 74 68 20 61 20 4e 55 4c 4c 20 74 6f 20 61 76 6f  th a NULL to avo
24517 69 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61  id.        ** ta
24518 6b 69 6e 67 20 75 70 20 64 61 74 61 20 73 70 61  king up data spa
24519 63 65 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74  ce with informat
2451a 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 6e 65  ion that will ne
2451b 76 65 72 20 62 65 20 75 73 65 64 2e 20 2a 2f 0a  ver be used. */.
2451c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2451d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2451e 4e 75 6c 6c 2c 20 30 2c 20 69 52 65 67 53 74 6f  Null, 0, iRegSto
2451f 72 65 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e  re);.        con
24520 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  tinue;.      }. 
24521 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e       if( pColumn
24522 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
24523 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  f( IsHiddenColum
24524 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  n(&pTab->aCol[i]
24525 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  ) ){.          a
24526 73 73 65 72 74 28 20 49 73 56 69 72 74 75 61 6c  ssert( IsVirtual
24527 28 70 54 61 62 29 20 29 3b 0a 20 20 20 20 20 20  (pTab) );.      
24528 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 20 20      j = -1;.    
24529 20 20 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b 3b        nHidden++;
2452a 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
2452b 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20 69 20            j = i 
2452c 2d 20 6e 48 69 64 64 65 6e 3b 0a 20 20 20 20 20  - nHidden;.     
2452d 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
2452e 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  {.        for(j=
2452f 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49  0; j<pColumn->nI
24530 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  d; j++){.       
24531 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e     if( pColumn->
24532 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29 20 62 72  a[j].idx==i ) br
24533 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
24534 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
24535 20 6a 3c 30 20 7c 7c 20 6e 43 6f 6c 75 6d 6e 3d   j<0 || nColumn=
24536 3d 30 20 7c 7c 20 28 70 43 6f 6c 75 6d 6e 20 26  =0 || (pColumn &
24537 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49  & j>=pColumn->nI
24538 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  d) ){.        sq
24539 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2453a 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  arse, pTab->aCol
2453b 5b 69 5d 2e 70 44 66 6c 74 2c 20 69 52 65 67 53  [i].pDflt, iRegS
2453c 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c  tore);.      }el
2453d 73 65 20 69 66 28 20 75 73 65 54 65 6d 70 54 61  se if( useTempTa
2453e 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ble ){.        s
2453f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
24540 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73  (v, OP_Column, s
24541 72 63 54 61 62 2c 20 6a 2c 20 69 52 65 67 53 74  rcTab, j, iRegSt
24542 6f 72 65 29 3b 20 0a 20 20 20 20 20 20 7d 65 6c  ore); .      }el
24543 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29  se if( pSelect )
24544 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
24545 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24546 50 5f 53 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d  P_SCopy, regFrom
24547 53 65 6c 65 63 74 2b 6a 2c 20 69 52 65 67 53 74  Select+j, iRegSt
24548 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ore);.      }els
24549 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
2454a 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2454b 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70  e, pList->a[j].p
2454c 45 78 70 72 2c 20 69 52 65 67 53 74 6f 72 65 29  Expr, iRegStore)
2454d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2454e 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  .    /* Generate
2454f 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63   code to check c
24550 6f 6e 73 74 72 61 69 6e 74 73 20 61 6e 64 20 67  onstraints and g
24551 65 6e 65 72 61 74 65 20 69 6e 64 65 78 20 6b 65  enerate index ke
24552 79 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 6f  ys and.    ** do
24553 20 74 68 65 20 69 6e 73 65 72 74 69 6f 6e 2e 0a   the insertion..
24554 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
24555 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
24556 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20  ALTABLE.    if( 
24557 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
24558 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
24559 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65  VtabMakeWritable
2455a 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
2455b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2455c 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55  eAddOp4(v, OP_VU
2455d 70 64 61 74 65 2c 20 31 2c 20 70 54 61 62 2d 3e  pdate, 1, pTab->
2455e 6e 43 6f 6c 2b 32 2c 20 72 65 67 49 6e 73 2c 0a  nCol+2, regIns,.
2455f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24560 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72       (const char
24561 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50  *)pTab->pVtab, P
24562 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c  4_VTAB);.    }el
24563 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a  se.#endif.    {.
24564 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e        sqlite3Gen
24565 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
24566 68 65 63 6b 73 28 0a 20 20 20 20 20 20 20 20 20  hecks(.         
24567 20 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20   pParse,.       
24568 20 20 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20     pTab,.       
24569 20 20 20 62 61 73 65 43 75 72 2c 0a 20 20 20 20     baseCur,.    
2456a 20 20 20 20 20 20 72 65 67 49 6e 73 2c 0a 20 20        regIns,.  
2456b 20 20 20 20 20 20 20 20 61 52 65 67 49 64 78 2c          aRegIdx,
2456c 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f  .          keyCo
2456d 6c 75 6d 6e 3e 3d 30 2c 0a 20 20 20 20 20 20 20  lumn>=0,.       
2456e 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20     0,.          
2456f 6f 6e 45 72 72 6f 72 2c 0a 20 20 20 20 20 20 20  onError,.       
24570 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 0a 20 20 20     endOfLoop.   
24571 20 20 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69     );.      sqli
24572 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72  te3CompleteInser
24573 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 20 20  tion(.          
24574 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
24575 20 20 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20    pTab,.        
24576 20 20 62 61 73 65 43 75 72 2c 0a 20 20 20 20 20    baseCur,.     
24577 20 20 20 20 20 72 65 67 49 6e 73 2c 0a 20 20 20       regIns,.   
24578 20 20 20 20 20 20 20 61 52 65 67 49 64 78 2c 0a         aRegIdx,.
24579 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
2457a 20 20 20 20 20 20 20 28 74 6d 61 73 6b 26 54 52         (tmask&TR
2457b 49 47 47 45 52 5f 41 46 54 45 52 29 20 3f 20 6e  IGGER_AFTER) ? n
2457c 65 77 49 64 78 20 3a 20 2d 31 2c 0a 20 20 20 20  ewIdx : -1,.    
2457d 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67        appendFlag
2457e 0a 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  .       );.    }
2457f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
24580 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72  e the count of r
24581 6f 77 73 20 74 68 61 74 20 61 72 65 20 69 6e 73  ows that are ins
24582 65 72 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28  erted.  */.  if(
24583 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
24584 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21  LITE_CountRows)!
24585 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
24586 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24587 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77  P_AddImm, regRow
24588 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a  Count, 1);.  }..
24589 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29    if( pTrigger )
2458a 7b 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 41 46  {.    /* Code AF
2458b 54 45 52 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  TER triggers */.
2458c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43      if( sqlite3C
2458d 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
2458e 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
2458f 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52  TK_INSERT, 0, TR
24590 49 47 47 45 52 5f 41 46 54 45 52 2c 20 0a 20 20  IGGER_AFTER, .  
24591 20 20 20 20 20 20 20 20 70 54 61 62 2c 20 6e 65          pTab, ne
24592 77 49 64 78 2c 20 2d 31 2c 20 6f 6e 45 72 72 6f  wIdx, -1, onErro
24593 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 30 2c  r, endOfLoop, 0,
24594 20 30 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74   0) ){.      got
24595 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
24596 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
24597 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  * The bottom of 
24598 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72 74 69  the main inserti
24599 6f 6e 20 6c 6f 6f 70 2c 20 69 66 20 74 68 65 20  on loop, if the 
2459a 64 61 74 61 20 73 6f 75 72 63 65 0a 20 20 2a 2a  data source.  **
2459b 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74 61   is a SELECT sta
2459c 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73  tement..  */.  s
2459d 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2459e 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c  eLabel(v, endOfL
2459f 6f 6f 70 29 3b 0a 20 20 69 66 28 20 75 73 65 54  oop);.  if( useT
245a0 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
245a1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
245a2 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 72  2(v, OP_Next, sr
245a3 63 54 61 62 2c 20 61 64 64 72 43 6f 6e 74 29 3b  cTab, addrCont);
245a4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
245a5 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
245a6 49 6e 73 54 6f 70 29 3b 0a 20 20 20 20 73 71 6c  InsTop);.    sql
245a7 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
245a8 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 73 72 63 54  , OP_Close, srcT
245a9 61 62 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ab);.  }else if(
245aa 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
245ab 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
245ac 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
245ad 20 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20   addrCont);.    
245ae 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
245af 65 72 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f  ere(v, addrInsTo
245b0 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  p);.  }..  if( !
245b1 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
245b2 26 26 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  && !isView ){.  
245b3 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74    /* Close all t
245b4 61 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f 0a  ables opened */.
245b5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
245b6 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
245b7 65 2c 20 62 61 73 65 43 75 72 29 3b 0a 20 20 20  e, baseCur);.   
245b8 20 66 6f 72 28 69 64 78 3d 31 2c 20 70 49 64 78   for(idx=1, pIdx
245b9 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
245ba 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
245bb 70 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a 20  pNext, idx++){. 
245bc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
245bd 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
245be 73 65 2c 20 69 64 78 2b 62 61 73 65 43 75 72 29  se, idx+baseCur)
245bf 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
245c0 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 71 6c  * Update the sql
245c1 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
245c2 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68  le by storing th
245c3 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
245c4 0a 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 76 61  .  ** counter va
245c5 6c 75 65 20 69 6e 20 6d 65 6d 6f 72 79 20 72 65  lue in memory re
245c6 67 41 75 74 6f 69 6e 63 20 62 61 63 6b 20 69 6e  gAutoinc back in
245c7 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  to the sqlite_se
245c8 71 75 65 6e 63 65 0a 20 20 2a 2a 20 74 61 62 6c  quence.  ** tabl
245c9 65 2e 0a 20 20 2a 2f 0a 20 20 61 75 74 6f 49 6e  e..  */.  autoIn
245ca 63 45 6e 64 28 70 50 61 72 73 65 2c 20 69 44 62  cEnd(pParse, iDb
245cb 2c 20 70 54 61 62 2c 20 72 65 67 41 75 74 6f 69  , pTab, regAutoi
245cc 6e 63 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  nc);..  /*.  ** 
245cd 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
245ce 72 20 6f 66 20 72 6f 77 73 20 69 6e 73 65 72 74  r of rows insert
245cf 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74  ed. If this rout
245d0 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e  ine is .  ** gen
245d1 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63  erating code bec
245d2 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74  ause of a call t
245d3 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  o sqlite3NestedP
245d4 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20  arse(), do not. 
245d5 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63   ** invoke the c
245d6 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
245d7 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
245d8 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
245d9 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 70 50 61  CountRows && pPa
245da 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26  rse->nested==0 &
245db 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53  & !pParse->trigS
245dc 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69  tack ){.    sqli
245dd 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
245de 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72   OP_ResultRow, r
245df 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a  egRowCount, 1);.
245e0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
245e1 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
245e2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
245e3 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
245e4 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
245e5 72 6f 77 73 20 69 6e 73 65 72 74 65 64 22 2c 20  rows inserted", 
245e6 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
245e7 20 20 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61    }..insert_clea
245e8 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  nup:.  sqlite3Sr
245e9 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
245ea 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c  pTabList);.  sql
245eb 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
245ec 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
245ed 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
245ee 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
245ef 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
245f0 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  stDelete(db, pCo
245f1 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  lumn);.  sqlite3
245f2 44 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49  DbFree(db, aRegI
245f3 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  dx);.}../*.** Ge
245f4 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64  nerate code to d
245f5 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65  o constraint che
245f6 63 6b 73 20 70 72 69 6f 72 20 74 6f 20 61 6e 20  cks prior to an 
245f7 49 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50 44  INSERT or an UPD
245f8 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ATE..**.** The i
245f9 6e 70 75 74 20 69 73 20 61 20 72 61 6e 67 65 20  nput is a range 
245fa 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72  of consecutive r
245fb 65 67 69 73 74 65 72 73 20 61 73 20 66 6f 6c 6c  egisters as foll
245fc 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e  ows:.**.**    1.
245fd 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74    The rowid of t
245fe 68 65 20 72 6f 77 20 74 6f 20 62 65 20 75 70 64  he row to be upd
245ff 61 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  ated before the 
24600 75 70 64 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a  update.  This.**
24601 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 69 73          value is
24602 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65 73 73 20   omitted unless 
24603 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 6e 20  we are doing an 
24604 55 50 44 41 54 45 20 74 68 61 74 20 69 6e 76 6f  UPDATE that invo
24605 6c 76 65 73 20 61 0a 2a 2a 20 20 20 20 20 20 20  lves a.**       
24606 20 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20 72   change to the r
24607 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 72 20  ecord number or 
24608 77 72 69 74 69 6e 67 20 74 6f 20 61 20 76 69 72  writing to a vir
24609 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  tual table..**.*
2460a 2a 20 20 20 20 32 2e 20 20 54 68 65 20 72 6f 77  *    2.  The row
2460b 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20 61 66  id of the row af
2460c 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a  ter the update..
2460d 2a 2a 0a 2a 2a 20 20 20 20 33 2e 20 20 54 68 65  **.**    3.  The
2460e 20 64 61 74 61 20 69 6e 20 74 68 65 20 66 69 72   data in the fir
2460f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
24610 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68 65   entry after the
24611 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20   update..**.**  
24612 20 20 69 2e 20 20 44 61 74 61 20 66 72 6f 6d 20    i.  Data from 
24613 6d 69 64 64 6c 65 20 63 6f 6c 75 6d 6e 73 2e 2e  middle columns..
24614 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 2e 20 20 54  ..**.**    N.  T
24615 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 6c  he data in the l
24616 61 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ast column of th
24617 65 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68  e entry after th
24618 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  e update..**.** 
24619 54 68 65 20 72 65 67 52 6f 77 69 64 20 70 61 72  The regRowid par
2461a 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 69 6e  ameter is the in
2461b 64 65 78 20 6f 66 20 74 68 65 20 72 65 67 69 73  dex of the regis
2461c 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 28  ter containing (
2461d 32 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c  2)..**.** The ol
2461e 64 20 72 6f 77 69 64 20 73 68 6f 77 6e 20 61 73  d rowid shown as
2461f 20 65 6e 74 72 79 20 28 31 29 20 61 62 6f 76 65   entry (1) above
24620 20 69 73 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65   is omitted unle
24621 73 73 20 62 6f 74 68 20 69 73 55 70 64 61 74 65  ss both isUpdate
24622 0a 2a 2a 20 61 6e 64 20 72 6f 77 69 64 43 68 6e  .** and rowidChn
24623 67 20 61 72 65 20 31 2e 20 20 69 73 55 70 64 61  g are 1.  isUpda
24624 74 65 20 69 73 20 74 72 75 65 20 66 6f 72 20 55  te is true for U
24625 50 44 41 54 45 73 20 61 6e 64 20 66 61 6c 73 65  PDATEs and false
24626 20 66 6f 72 0a 2a 2a 20 49 4e 53 45 52 54 73 2e   for.** INSERTs.
24627 20 20 52 6f 77 69 64 43 68 6e 67 20 6d 65 61 6e    RowidChng mean
24628 73 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 72  s that the new r
24629 6f 77 69 64 20 69 73 20 65 78 70 6c 69 63 69 74  owid is explicit
2462a 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 79 0a  ly specified by.
2462b 2a 2a 20 74 68 65 20 75 70 64 61 74 65 20 6f 72  ** the update or
2462c 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 6e   insert statemen
2462d 74 2e 20 20 49 66 20 72 6f 77 69 64 43 68 6e 67  t.  If rowidChng
2462e 20 69 73 20 66 61 6c 73 65 2c 20 69 74 20 6d 65   is false, it me
2462f 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ans that.** the 
24630 72 6f 77 69 64 20 69 73 20 63 6f 6d 70 75 74 65  rowid is compute
24631 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
24632 69 6e 20 61 6e 20 69 6e 73 65 72 74 20 6f 72 20  in an insert or 
24633 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 76  that the rowid v
24634 61 6c 75 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 6d  alue.** is not m
24635 6f 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75  odified by the u
24636 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  pdate..**.** The
24637 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
24638 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
24639 73 74 6f 72 65 20 6e 65 77 20 69 6e 64 65 78 20  store new index 
2463a 65 6e 74 72 69 65 73 20 69 6e 74 6f 0a 2a 2a 20  entries into.** 
2463b 72 65 67 69 73 74 65 72 73 20 69 64 65 6e 74 69  registers identi
2463c 66 69 65 64 20 62 79 20 61 52 65 67 49 64 78 5b  fied by aRegIdx[
2463d 5d 2e 20 20 4e 6f 20 69 6e 64 65 78 20 65 6e 74  ].  No index ent
2463e 72 79 20 69 73 20 63 72 65 61 74 65 64 20 66 6f  ry is created fo
2463f 72 0a 2a 2a 20 69 6e 64 69 63 65 73 20 77 68 65  r.** indices whe
24640 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30  re aRegIdx[i]==0
24641 2e 20 20 54 68 65 20 6f 72 64 65 72 20 6f 66 20  .  The order of 
24642 69 6e 64 69 63 65 73 20 69 6e 20 61 52 65 67 49  indices in aRegI
24643 64 78 5b 5d 20 69 73 0a 2a 2a 20 74 68 65 20 73  dx[] is.** the s
24644 61 6d 65 20 61 73 20 74 68 65 20 6f 72 64 65 72  ame as the order
24645 20 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e 20 74   of indices on t
24646 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f  he linked list o
24647 66 20 69 6e 64 69 63 65 73 0a 2a 2a 20 61 74 74  f indices.** att
24648 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 61 62  ached to the tab
24649 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  le..**.** This r
2464a 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65  outine also gene
2464b 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 63 68  rates code to ch
2464c 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  eck constraints.
2464d 20 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 2a 2a 20 43    NOT NULL,.** C
2464e 48 45 43 4b 2c 20 61 6e 64 20 55 4e 49 51 55 45  HECK, and UNIQUE
2464f 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65   constraints are
24650 20 61 6c 6c 20 63 68 65 63 6b 65 64 2e 20 20 49   all checked.  I
24651 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  f a constraint f
24652 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ails,.** then th
24653 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63  e appropriate ac
24654 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65  tion is performe
24655 64 2e 20 20 54 68 65 72 65 20 61 72 65 20 66 69  d.  There are fi
24656 76 65 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 61  ve possible.** a
24657 63 74 69 6f 6e 73 3a 20 52 4f 4c 4c 42 41 43 4b  ctions: ROLLBACK
24658 2c 20 41 42 4f 52 54 2c 20 46 41 49 4c 2c 20 52  , ABORT, FAIL, R
24659 45 50 4c 41 43 45 2c 20 61 6e 64 20 49 47 4e 4f  EPLACE, and IGNO
2465a 52 45 2e 0a 2a 2a 0a 2a 2a 20 20 43 6f 6e 73 74  RE..**.**  Const
2465b 72 61 69 6e 74 20 74 79 70 65 20 20 41 63 74 69  raint type  Acti
2465c 6f 6e 20 20 20 20 20 20 20 57 68 61 74 20 48 61  on       What Ha
2465d 70 70 65 6e 73 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d  ppens.**  ------
2465e 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d  ---------  -----
2465f 2d 2d 2d 2d 2d 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  -----   --------
24660 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24661 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24662 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
24663 20 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 20        ROLLBACK  
24664 20 20 20 54 68 65 20 63 75 72 72 65 6e 74 20 74     The current t
24665 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
24666 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 0a 2a 2a  lled back and.**
24667 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24668 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24669 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72  sqlite3_exec() r
2466a 65 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65  eturns immediate
2466b 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20  ly with a.**    
2466c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2466d 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
2466e 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54  rn code of SQLIT
2466f 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a  E_CONSTRAINT..**
24670 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
24671 20 20 20 20 20 20 41 42 4f 52 54 20 20 20 20 20        ABORT     
24672 20 20 20 42 61 63 6b 20 6f 75 74 20 63 68 61 6e     Back out chan
24673 67 65 73 20 66 72 6f 6d 20 74 68 65 20 63 75 72  ges from the cur
24674 72 65 6e 74 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20  rent command.** 
24675 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24676 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
24677 6e 6c 79 20 28 64 6f 20 6e 6f 74 20 64 6f 20 61  nly (do not do a
24678 20 63 6f 6d 70 6c 65 74 65 20 72 6f 6c 6c 62 61   complete rollba
24679 63 6b 29 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20  ck) then.**     
2467a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2467b 20 20 20 20 20 20 20 20 20 20 20 63 61 75 73 65             cause
2467c 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
2467d 74 6f 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69  to return immedi
2467e 61 74 65 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20  ately.**        
2467f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24680 20 20 20 20 20 20 20 20 77 69 74 68 20 53 51 4c          with SQL
24681 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a  ITE_CONSTRAINT..
24682 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20  **.**  any      
24683 20 20 20 20 20 20 20 20 46 41 49 4c 20 20 20 20          FAIL    
24684 20 20 20 20 20 53 71 6c 69 74 65 5f 65 78 65 63       Sqlite_exec
24685 28 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65 64  () returns immed
24686 69 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a  iately with a.**
24687 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24688 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24689 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53  return code of S
2468a 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
2468b 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20  .  The.**       
2468c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2468d 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 61 63           transac
2468e 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c  tion is not roll
2468f 65 64 20 62 61 63 6b 20 61 6e 64 20 61 6e 79 0a  ed back and any.
24690 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
24691 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24692 20 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20    prior changes 
24693 61 72 65 20 72 65 74 61 69 6e 65 64 2e 0a 2a 2a  are retained..**
24694 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
24695 20 20 20 20 20 20 49 47 4e 4f 52 45 20 20 20 20        IGNORE    
24696 20 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75     The record nu
24697 6d 62 65 72 20 61 6e 64 20 64 61 74 61 20 69 73  mber and data is
24698 20 70 6f 70 70 65 64 20 66 72 6f 6d 0a 2a 2a 20   popped from.** 
24699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2469a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
2469b 68 65 20 73 74 61 63 6b 20 61 6e 64 20 74 68 65  he stack and the
2469c 72 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61  re is an immedia
2469d 74 65 20 6a 75 6d 70 0a 2a 2a 20 20 20 20 20 20  te jump.**      
2469e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2469f 20 20 20 20 20 20 20 20 20 20 74 6f 20 6c 61 62            to lab
246a0 65 6c 20 69 67 6e 6f 72 65 44 65 73 74 2e 0a 2a  el ignoreDest..*
246a1 2a 0a 2a 2a 20 20 4e 4f 54 20 4e 55 4c 4c 20 20  *.**  NOT NULL  
246a2 20 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20         REPLACE  
246a3 20 20 20 20 54 68 65 20 4e 55 4c 4c 20 76 61 6c      The NULL val
246a4 75 65 20 69 73 20 72 65 70 6c 61 63 65 20 62 79  ue is replace by
246a5 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20   the default.** 
246a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
246a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
246a8 61 6c 75 65 20 66 6f 72 20 74 68 61 74 20 63 6f  alue for that co
246a9 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65 20 64 65  lumn.  If the de
246aa 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 20  fault value.**  
246ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
246ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
246ad 20 4e 55 4c 4c 2c 20 74 68 65 20 61 63 74 69 6f   NULL, the actio
246ae 6e 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  n is the same as
246af 20 41 42 4f 52 54 2e 0a 2a 2a 0a 2a 2a 20 20 55   ABORT..**.**  U
246b0 4e 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20  NIQUE           
246b1 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68 65  REPLACE      The
246b2 20 6f 74 68 65 72 20 72 6f 77 20 74 68 61 74 20   other row that 
246b3 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 74  conflicts with t
246b4 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20  he row.**       
246b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
246b6 20 20 20 20 20 20 20 20 20 62 65 69 6e 67 20 69           being i
246b7 6e 73 65 72 74 65 64 20 69 73 20 72 65 6d 6f 76  nserted is remov
246b8 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 43 48 45 43 4b  ed..**.**  CHECK
246b9 20 20 20 20 20 20 20 20 20 20 20 20 52 45 50 4c              REPL
246ba 41 43 45 20 20 20 20 20 20 49 6c 6c 65 67 61 6c  ACE      Illegal
246bb 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 69  .  The results i
246bc 6e 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a  n an exception..
246bd 2a 2a 0a 2a 2a 20 57 68 69 63 68 20 61 63 74 69  **.** Which acti
246be 6f 6e 20 74 6f 20 74 61 6b 65 20 69 73 20 64 65  on to take is de
246bf 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
246c0 6f 76 65 72 72 69 64 65 45 72 72 6f 72 20 70 61  overrideError pa
246c1 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 72 20 69  rameter..** Or i
246c2 66 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3d  f overrideError=
246c3 3d 4f 45 5f 44 65 66 61 75 6c 74 2c 20 74 68 65  =OE_Default, the
246c4 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 6f 6e  n the pParse->on
246c5 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 0a  Error parameter.
246c6 2a 2a 20 69 73 20 75 73 65 64 2e 20 20 4f 72 20  ** is used.  Or 
246c7 69 66 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72  if pParse->onErr
246c8 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 74  or==OE_Default t
246c9 68 65 6e 20 74 68 65 20 6f 6e 45 72 72 6f 72 20  hen the onError 
246ca 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65  value.** for the
246cb 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75   constraint is u
246cc 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  sed..**.** The c
246cd 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d  alling routine m
246ce 75 73 74 20 6f 70 65 6e 20 61 20 72 65 61 64 2f  ust open a read/
246cf 77 72 69 74 65 20 63 75 72 73 6f 72 20 66 6f 72  write cursor for
246d0 20 70 54 61 62 20 77 69 74 68 0a 2a 2a 20 63 75   pTab with.** cu
246d1 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 62 61 73  rsor number "bas
246d2 65 43 75 72 22 2e 20 20 41 6c 6c 20 69 6e 64 69  eCur".  All indi
246d3 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74  ces of pTab must
246d4 20 61 6c 73 6f 20 68 61 76 65 20 6f 70 65 6e 0a   also have open.
246d5 2a 2a 20 72 65 61 64 2f 77 72 69 74 65 20 63 75  ** read/write cu
246d6 72 73 6f 72 73 20 77 69 74 68 20 63 75 72 73 6f  rsors with curso
246d7 72 20 6e 75 6d 62 65 72 20 62 61 73 65 43 75 72  r number baseCur
246d8 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20  +i for the i-th 
246d9 63 75 72 73 6f 72 2e 0a 2a 2a 20 45 78 63 65 70  cursor..** Excep
246da 74 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  t, if there is n
246db 6f 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66  o possibility of
246dc 20 61 20 52 45 50 4c 41 43 45 20 61 63 74 69 6f   a REPLACE actio
246dd 6e 20 74 68 65 6e 0a 2a 2a 20 63 75 72 73 6f 72  n then.** cursor
246de 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  s do not need to
246df 20 62 65 20 6f 70 65 6e 20 66 6f 72 20 69 6e 64   be open for ind
246e0 69 63 65 73 20 77 68 65 72 65 20 61 52 65 67 49  ices where aRegI
246e1 64 78 5b 69 5d 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c  dx[i]==0..*/.SQL
246e2 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
246e3 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
246e4 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
246e5 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
246e6 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  e,      /* The p
246e7 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
246e8 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
246e9 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61         /* the ta
246ea 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
246eb 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
246ec 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72  */.  int baseCur
246ed 2c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  ,        /* Inde
246ee 78 20 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74  x of a read/writ
246ef 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  e cursor pointin
246f0 67 20 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 69  g at pTab */.  i
246f1 6e 74 20 72 65 67 52 6f 77 69 64 2c 20 20 20 20  nt regRowid,    
246f2 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
246f3 68 65 20 72 61 6e 67 65 20 6f 66 20 69 6e 70 75  he range of inpu
246f4 74 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20  t registers */. 
246f5 20 69 6e 74 20 2a 61 52 65 67 49 64 78 2c 20 20   int *aRegIdx,  
246f6 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
246f7 20 75 73 65 64 20 62 79 20 65 61 63 68 20 69 6e   used by each in
246f8 64 65 78 2e 20 20 30 20 66 6f 72 20 75 6e 75 73  dex.  0 for unus
246f9 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ed indices */.  
246fa 69 6e 74 20 72 6f 77 69 64 43 68 6e 67 2c 20 20  int rowidChng,  
246fb 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
246fc 68 65 20 72 6f 77 69 64 20 6d 69 67 68 74 20 63  he rowid might c
246fd 6f 6c 6c 69 64 65 20 77 69 74 68 20 65 78 69 73  ollide with exis
246fe 74 69 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20  ting entry */.  
246ff 69 6e 74 20 69 73 55 70 64 61 74 65 2c 20 20 20  int isUpdate,   
24700 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
24701 55 50 44 41 54 45 2c 20 46 61 6c 73 65 20 66 6f  UPDATE, False fo
24702 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e  r INSERT */.  in
24703 74 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 2c  t overrideError,
24704 20 20 2f 2a 20 4f 76 65 72 72 69 64 65 20 6f 6e    /* Override on
24705 45 72 72 6f 72 20 74 6f 20 74 68 69 73 20 69 66  Error to this if
24706 20 6e 6f 74 20 4f 45 5f 44 65 66 61 75 6c 74 20   not OE_Default 
24707 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 44  */.  int ignoreD
24708 65 73 74 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70  est      /* Jump
24709 20 74 6f 20 74 68 69 73 20 6c 61 62 65 6c 20 6f   to this label o
2470a 6e 20 61 6e 20 4f 45 5f 49 67 6e 6f 72 65 20 72  n an OE_Ignore r
2470b 65 73 6f 6c 75 74 69 6f 6e 20 2a 2f 0a 29 7b 0a  esolution */.){.
2470c 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20    int i;.  Vdbe 
2470d 2a 76 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a  *v;.  int nCol;.
2470e 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 3b 0a 20    int onError;. 
2470f 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20   int j1;        
24710 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 73       /* Addresss
24711 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63   of jump instruc
24712 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 32  tion */.  int j2
24713 20 3d 20 30 2c 20 6a 33 3b 20 20 20 20 20 2f 2a   = 0, j3;     /*
24714 20 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a 75   Addresses of ju
24715 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  mp instructions 
24716 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
24717 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
24718 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
24719 66 69 72 73 74 20 64 61 74 61 20 63 6f 6c 75 6d  first data colum
2471a 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  n */.  int iCur;
2471b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a  .  Index *pIdx;.
2471c 20 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63    int seenReplac
2471d 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73  e = 0;.  int has
2471e 54 77 6f 52 6f 77 69 64 73 20 3d 20 28 69 73 55  TwoRowids = (isU
2471f 70 64 61 74 65 20 26 26 20 72 6f 77 69 64 43 68  pdate && rowidCh
24720 6e 67 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69  ng);..  v = sqli
24721 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
24722 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  e);.  assert( v!
24723 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
24724 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  pTab->pSelect==0
24725 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62   );  /* This tab
24726 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57  le is not a VIEW
24727 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61   */.  nCol = pTa
24728 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61  b->nCol;.  regDa
24729 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20  ta = regRowid + 
2472a 31 3b 0a 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61  1;...  /* Test a
2472b 6c 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73  ll NOT NULL cons
2472c 74 72 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20  traints..  */.  
2472d 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
2472e 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69   i++){.    if( i
2472f 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
24730 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
24731 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72 72  .    }.    onErr
24732 6f 72 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  or = pTab->aCol[
24733 69 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20  i].notNull;.    
24734 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
24735 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  None ) continue;
24736 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64  .    if( overrid
24737 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
24738 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
24739 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72  ror = overrideEr
2473a 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ror;.    }else i
2473b 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  f( onError==OE_D
2473c 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
2473d 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
2473e 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
2473f 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ( onError==OE_Re
24740 70 6c 61 63 65 20 26 26 20 70 54 61 62 2d 3e 61  place && pTab->a
24741 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20  Col[i].pDflt==0 
24742 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
24743 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
24744 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f   }.    assert( o
24745 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62  nError==OE_Rollb
24746 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d  ack || onError==
24747 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72  OE_Abort || onEr
24748 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20  ror==OE_Fail.   
24749 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d       || onError=
2474a 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e  =OE_Ignore || on
2474b 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
2474c 65 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28  e );.    switch(
2474d 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20   onError ){.    
2474e 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61    case OE_Rollba
2474f 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ck:.      case O
24750 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20 63  E_Abort:.      c
24751 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20  ase OE_Fail: {. 
24752 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73         char *zMs
24753 67 3b 0a 20 20 20 20 20 20 20 20 6a 31 20 3d 20  g;.        j1 = 
24754 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24755 33 28 76 2c 20 4f 50 5f 48 61 6c 74 49 66 4e 75  3(v, OP_HaltIfNu
24756 6c 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ll,.            
24757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24758 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 4f 4e        SQLITE_CON
24759 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72  STRAINT, onError
2475a 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20  , regData+i);.  
2475b 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c        zMsg = sql
2475c 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72  ite3MPrintf(pPar
2475d 73 65 2d 3e 64 62 2c 20 22 25 73 2e 25 73 20 6d  se->db, "%s.%s m
2475e 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c  ay not be NULL",
2475f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
24760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
24761 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62  Tab->zName, pTab
24762 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29  ->aCol[i].zName)
24763 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
24764 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
24765 20 2d 31 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59   -1, zMsg, P4_DY
24766 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20  NAMIC);.        
24767 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
24768 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e       case OE_Ign
24769 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  ore: {.        s
2476a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2476b 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72  (v, OP_IsNull, r
2476c 65 67 44 61 74 61 2b 69 2c 20 69 67 6e 6f 72 65  egData+i, ignore
2476d 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62  Dest);.        b
2476e 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2476f 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c      case OE_Repl
24770 61 63 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 6a  ace: {.        j
24771 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
24772 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e  ddOp1(v, OP_NotN
24773 75 6c 6c 2c 20 72 65 67 44 61 74 61 2b 69 29 3b  ull, regData+i);
24774 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
24775 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
24776 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70   pTab->aCol[i].p
24777 44 66 6c 74 2c 20 72 65 67 44 61 74 61 2b 69 29  Dflt, regData+i)
24778 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
24779 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2477a 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20 62 72   j1);.        br
2477b 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2477c 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73   }.  }..  /* Tes
2477d 74 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73  t all CHECK cons
2477e 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a 23 69 66  traints.  */.#if
2477f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24780 5f 43 48 45 43 4b 0a 20 20 69 66 28 20 70 54 61  _CHECK.  if( pTa
24781 62 2d 3e 70 43 68 65 63 6b 20 26 26 20 28 70 50  b->pCheck && (pP
24782 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  arse->db->flags 
24783 26 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43  & SQLITE_IgnoreC
24784 68 65 63 6b 73 29 3d 3d 30 20 29 7b 0a 20 20 20  hecks)==0 ){.   
24785 20 69 6e 74 20 61 6c 6c 4f 6b 20 3d 20 73 71 6c   int allOk = sql
24786 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
24787 6c 28 76 29 3b 0a 20 20 20 20 70 50 61 72 73 65  l(v);.    pParse
24788 2d 3e 63 6b 42 61 73 65 20 3d 20 72 65 67 44 61  ->ckBase = regDa
24789 74 61 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ta;.    sqlite3E
2478a 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
2478b 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20  , pTab->pCheck, 
2478c 61 6c 6c 4f 6b 2c 20 53 51 4c 49 54 45 5f 4a 55  allOk, SQLITE_JU
2478d 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 6f  MPIFNULL);.    o
2478e 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64  nError = overrid
2478f 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
24790 6c 74 20 3f 20 6f 76 65 72 72 69 64 65 45 72 72  lt ? overrideErr
24791 6f 72 20 3a 20 4f 45 5f 41 62 6f 72 74 3b 0a 20  or : OE_Abort;. 
24792 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d     if( onError==
24793 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20  OE_Ignore ){.   
24794 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24795 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
24796 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b   0, ignoreDest);
24797 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
24798 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24799 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op2(v, OP_Halt, 
2479a 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
2479b 54 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20  T, onError);.   
2479c 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2479d 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2479e 2c 20 61 6c 6c 4f 6b 29 3b 0a 20 20 7d 0a 23 65  , allOk);.  }.#e
2479f 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
247a0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45  (SQLITE_OMIT_CHE
247a1 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  CK) */..  /* If 
247a2 77 65 20 68 61 76 65 20 61 6e 20 49 4e 54 45 47  we have an INTEG
247a3 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
247a4 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 72  make sure the pr
247a5 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f  imary key.  ** o
247a6 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64  f the new record
247a7 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 6f   does not previo
247a8 75 73 6c 79 20 65 78 69 73 74 2e 20 20 45 78 63  usly exist.  Exc
247a9 65 70 74 2c 20 69 66 20 74 68 69 73 0a 20 20 2a  ept, if this.  *
247aa 2a 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 61  * is an UPDATE a
247ab 6e 64 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  nd the primary k
247ac 65 79 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 69  ey is not changi
247ad 6e 67 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 0a  ng, that is OK..
247ae 20 20 2a 2f 0a 20 20 69 66 28 20 72 6f 77 69 64    */.  if( rowid
247af 43 68 6e 67 20 29 7b 0a 20 20 20 20 6f 6e 45 72  Chng ){.    onEr
247b0 72 6f 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79 43  ror = pTab->keyC
247b1 6f 6e 66 3b 0a 20 20 20 20 69 66 28 20 6f 76 65  onf;.    if( ove
247b2 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44  rrideError!=OE_D
247b3 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
247b4 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69  onError = overri
247b5 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c  deError;.    }el
247b6 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d  se if( onError==
247b7 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
247b8 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
247b9 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
247ba 20 20 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72    .    if( onErr
247bb 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 7c  or!=OE_Replace |
247bc 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 29  | pTab->pIndex )
247bd 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 55 70  {.      if( isUp
247be 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  date ){.        
247bf 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
247c0 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 45 71 2c  AddOp3(v, OP_Eq,
247c1 20 72 65 67 52 6f 77 69 64 2c 20 30 2c 20 72 65   regRowid, 0, re
247c2 67 52 6f 77 69 64 2d 31 29 3b 0a 20 20 20 20 20  gRowid-1);.     
247c3 20 7d 0a 20 20 20 20 20 20 6a 33 20 3d 20 73 71   }.      j3 = sq
247c4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
247c5 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
247c6 20 62 61 73 65 43 75 72 2c 20 30 2c 20 72 65 67   baseCur, 0, reg
247c7 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 77  Rowid);.      sw
247c8 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b  itch( onError ){
247c9 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74  .        default
247ca 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 6e  : {.          on
247cb 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74  Error = OE_Abort
247cc 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;.          /* F
247cd 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68  all thru into th
247ce 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20  e next case */. 
247cf 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
247d0 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
247d1 6b 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k:.        case 
247d2 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20  OE_Abort:.      
247d3 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20    case OE_Fail: 
247d4 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
247d5 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
247d6 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
247d7 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e 45  _CONSTRAINT, onE
247d8 72 72 6f 72 2c 20 30 2c 0a 20 20 20 20 20 20 20  rror, 0,.       
247d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
247da 20 20 20 20 22 50 52 49 4d 41 52 59 20 4b 45 59      "PRIMARY KEY
247db 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 22   must be unique"
247dc 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
247dd 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
247de 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
247df 20 63 61 73 65 20 4f 45 5f 52 65 70 6c 61 63 65   case OE_Replace
247e0 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  : {.          sq
247e1 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
247e2 49 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72  IndexDelete(pPar
247e3 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75  se, pTab, baseCu
247e4 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  r, 0);.         
247e5 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31   seenReplace = 1
247e6 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
247e7 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
247e8 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e       case OE_Ign
247e9 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  ore: {.         
247ea 20 61 73 73 65 72 74 28 20 73 65 65 6e 52 65 70   assert( seenRep
247eb 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lace==0 );.     
247ec 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
247ed 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
247ee 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74  o, 0, ignoreDest
247ef 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
247f0 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
247f1 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
247f2 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
247f3 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 69 66  v, j3);.      if
247f4 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20  ( isUpdate ){.  
247f5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
247f6 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29  eJumpHere(v, j2)
247f7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
247f8 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61    }..  /* Test a
247f9 6c 6c 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  ll UNIQUE constr
247fa 61 69 6e 74 73 20 62 79 20 63 72 65 61 74 69 6e  aints by creatin
247fb 67 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61  g entries for ea
247fc 63 68 20 55 4e 49 51 55 45 0a 20 20 2a 2a 20 69  ch UNIQUE.  ** i
247fd 6e 64 65 78 20 61 6e 64 20 6d 61 6b 69 6e 67 20  ndex and making 
247fe 73 75 72 65 20 74 68 61 74 20 64 75 70 6c 69 63  sure that duplic
247ff 61 74 65 20 65 6e 74 72 69 65 73 20 64 6f 20 6e  ate entries do n
24800 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
24801 2e 0a 20 20 2a 2a 20 41 64 64 20 74 68 65 20 6e  ..  ** Add the n
24802 65 77 20 72 65 63 6f 72 64 73 20 74 6f 20 74 68  ew records to th
24803 65 20 69 6e 64 69 63 65 73 20 61 73 20 77 65 20  e indices as we 
24804 67 6f 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  go..  */.  for(i
24805 43 75 72 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  Cur=0, pIdx=pTab
24806 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
24807 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
24808 2c 20 69 43 75 72 2b 2b 29 7b 0a 20 20 20 20 69  , iCur++){.    i
24809 6e 74 20 72 65 67 49 64 78 3b 0a 20 20 20 20 69  nt regIdx;.    i
2480a 6e 74 20 72 65 67 52 3b 0a 0a 20 20 20 20 69 66  nt regR;..    if
2480b 28 20 61 52 65 67 49 64 78 5b 69 43 75 72 5d 3d  ( aRegIdx[iCur]=
2480c 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20  =0 ) continue;  
2480d 2f 2a 20 53 6b 69 70 20 75 6e 75 73 65 64 20 69  /* Skip unused i
2480e 6e 64 69 63 65 73 20 2a 2f 0a 0a 20 20 20 20 2f  ndices */..    /
2480f 2a 20 43 72 65 61 74 65 20 61 20 6b 65 79 20 66  * Create a key f
24810 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  or accessing the
24811 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f 0a   index entry */.
24812 20 20 20 20 72 65 67 49 64 78 20 3d 20 73 71 6c      regIdx = sql
24813 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
24814 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e 6e  (pParse, pIdx->n
24815 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 66  Column+1);.    f
24816 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e  or(i=0; i<pIdx->
24817 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
24818 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70       int idx = p
24819 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
2481a 3b 0a 20 20 20 20 20 20 69 66 28 20 69 64 78 3d  ;.      if( idx=
2481b 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
2481c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2481d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2481e 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69 64 2c  SCopy, regRowid,
2481f 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20 20   regIdx+i);.    
24820 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
24821 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24822 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
24823 72 65 67 44 61 74 61 2b 69 64 78 2c 20 72 65 67  regData+idx, reg
24824 49 64 78 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a  Idx+i);.      }.
24825 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
24826 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24827 50 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69  P_SCopy, regRowi
24828 64 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20  d, regIdx+i);.  
24829 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2482a 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
2482b 63 6f 72 64 2c 20 72 65 67 49 64 78 2c 20 70 49  cord, regIdx, pI
2482c 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 2c 20 61  dx->nColumn+1, a
2482d 52 65 67 49 64 78 5b 69 43 75 72 5d 29 3b 0a 20  RegIdx[iCur]);. 
2482e 20 20 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41     sqlite3IndexA
2482f 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49  ffinityStr(v, pI
24830 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dx);.    sqlite3
24831 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
24832 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
24833 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43  regIdx, pIdx->nC
24834 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 73 71  olumn+1);.    sq
24835 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
24836 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
24837 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c  gIdx, pIdx->nCol
24838 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20  umn+1);..    /* 
24839 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20 61 63  Find out what ac
2483a 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 69 6e 20  tion to take in 
2483b 63 61 73 65 20 74 68 65 72 65 20 69 73 20 61 6e  case there is an
2483c 20 69 6e 64 65 78 69 6e 67 20 63 6f 6e 66 6c 69   indexing confli
2483d 63 74 20 2a 2f 0a 20 20 20 20 6f 6e 45 72 72 6f  ct */.    onErro
2483e 72 20 3d 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  r = pIdx->onErro
2483f 72 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72  r;.    if( onErr
24840 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 20 63 6f  or==OE_None ) co
24841 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 70 49 64 78  ntinue;  /* pIdx
24842 20 69 73 20 6e 6f 74 20 61 20 55 4e 49 51 55 45   is not a UNIQUE
24843 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 66   index */.    if
24844 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21  ( overrideError!
24845 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
24846 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f       onError = o
24847 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20  verrideError;.  
24848 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72    }else if( onEr
24849 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
2484a 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
2484b 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
2484c 20 7d 0a 20 20 20 20 69 66 28 20 73 65 65 6e 52   }.    if( seenR
2484d 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eplace ){.      
2484e 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
2484f 49 67 6e 6f 72 65 20 29 20 6f 6e 45 72 72 6f 72  Ignore ) onError
24850 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a 20   = OE_Replace;. 
24851 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 6f 6e       else if( on
24852 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 20 29  Error==OE_Fail )
24853 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
24854 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a  ort;.    }.    .
24855 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  .    /* Check to
24856 20 73 65 65 20 69 66 20 74 68 65 20 6e 65 77 20   see if the new 
24857 69 6e 64 65 78 20 65 6e 74 72 79 20 77 69 6c 6c  index entry will
24858 20 62 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20   be unique */.  
24859 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64    j2 = sqlite3Vd
2485a 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
2485b 73 4e 75 6c 6c 2c 20 72 65 67 49 64 78 2c 20 30  sNull, regIdx, 0
2485c 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29  , pIdx->nColumn)
2485d 3b 0a 20 20 20 20 72 65 67 52 20 3d 20 73 71 6c  ;.    regR = sql
2485e 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
2485f 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69  Parse);.    sqli
24860 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24861 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f   OP_SCopy, regRo
24862 77 69 64 2d 68 61 73 54 77 6f 52 6f 77 69 64 73  wid-hasTwoRowids
24863 2c 20 72 65 67 52 29 3b 0a 20 20 20 20 6a 33 20  , regR);.    j3 
24864 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
24865 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71  Op4(v, OP_IsUniq
24866 75 65 2c 20 62 61 73 65 43 75 72 2b 69 43 75 72  ue, baseCur+iCur
24867 2b 31 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  +1, 0,.         
24868 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24869 20 20 72 65 67 52 2c 20 53 51 4c 49 54 45 5f 49    regR, SQLITE_I
2486a 4e 54 5f 54 4f 5f 50 54 52 28 61 52 65 67 49 64  NT_TO_PTR(aRegId
2486b 78 5b 69 43 75 72 5d 29 2c 0a 20 20 20 20 20 20  x[iCur]),.      
2486c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2486d 20 20 20 20 20 50 34 5f 49 4e 54 33 32 29 3b 0a       P4_INT32);.
2486e 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  .    /* Generate
2486f 20 63 6f 64 65 20 74 68 61 74 20 65 78 65 63 75   code that execu
24870 74 65 73 20 69 66 20 74 68 65 20 6e 65 77 20 69  tes if the new i
24871 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f  ndex entry is no
24872 74 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20 20 20  t unique */.    
24873 61 73 73 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d  assert( onError=
24874 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20  =OE_Rollback || 
24875 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72  onError==OE_Abor
24876 74 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  t || onError==OE
24877 5f 46 61 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c  _Fail.        ||
24878 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e   onError==OE_Ign
24879 6f 72 65 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d  ore || onError==
2487a 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b 0a 20 20  OE_Replace );.  
2487b 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f    switch( onErro
2487c 72 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  r ){.      case 
2487d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20  OE_Rollback:.   
2487e 20 20 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74     case OE_Abort
2487f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  :.      case OE_
24880 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20  Fail: {.        
24881 69 6e 74 20 6a 2c 20 6e 31 2c 20 6e 32 3b 0a 20  int j, n1, n2;. 
24882 20 20 20 20 20 20 20 63 68 61 72 20 7a 45 72 72         char zErr
24883 4d 73 67 5b 32 30 30 5d 3b 0a 20 20 20 20 20 20  Msg[200];.      
24884 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
24885 74 66 28 41 72 72 61 79 53 69 7a 65 28 7a 45 72  tf(ArraySize(zEr
24886 72 4d 73 67 29 2c 20 7a 45 72 72 4d 73 67 2c 0a  rMsg), zErrMsg,.
24887 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24888 20 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6e           pIdx->n
24889 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 63 6f 6c 75  Column>1 ? "colu
2488a 6d 6e 73 20 22 20 3a 20 22 63 6f 6c 75 6d 6e 20  mns " : "column 
2488b 22 29 3b 0a 20 20 20 20 20 20 20 20 6e 31 20 3d  ");.        n1 =
2488c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2488d 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  (zErrMsg);.     
2488e 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49     for(j=0; j<pI
2488f 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 20 6e  dx->nColumn && n
24890 31 3c 41 72 72 61 79 53 69 7a 65 28 7a 45 72 72  1<ArraySize(zErr
24891 4d 73 67 29 2d 33 30 3b 20 6a 2b 2b 29 7b 0a 20  Msg)-30; j++){. 
24892 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
24893 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  Col = pTab->aCol
24894 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  [pIdx->aiColumn[
24895 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  j]].zName;.     
24896 20 20 20 20 20 6e 32 20 3d 20 73 71 6c 69 74 65       n2 = sqlite
24897 33 53 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 29 3b  3Strlen30(zCol);
24898 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6a  .          if( j
24899 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >0 ){.          
2489a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
2489b 74 66 28 41 72 72 61 79 53 69 7a 65 28 7a 45 72  tf(ArraySize(zEr
2489c 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72 4d  rMsg)-n1, &zErrM
2489d 73 67 5b 6e 31 5d 2c 20 22 2c 20 22 29 3b 0a 20  sg[n1], ", ");. 
2489e 20 20 20 20 20 20 20 20 20 20 20 6e 31 20 2b 3d             n1 +=
2489f 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   2;.          }.
248a0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 31            if( n1
248a1 2b 6e 32 3e 41 72 72 61 79 53 69 7a 65 28 7a 45  +n2>ArraySize(zE
248a2 72 72 4d 73 67 29 2d 33 30 20 29 7b 0a 20 20 20  rrMsg)-30 ){.   
248a3 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
248a4 5f 73 6e 70 72 69 6e 74 66 28 41 72 72 61 79 53  _snprintf(ArrayS
248a5 69 7a 65 28 7a 45 72 72 4d 73 67 29 2d 6e 31 2c  ize(zErrMsg)-n1,
248a6 20 26 7a 45 72 72 4d 73 67 5b 6e 31 5d 2c 20 22   &zErrMsg[n1], "
248a7 2e 2e 2e 22 29 3b 0a 20 20 20 20 20 20 20 20 20  ...");.         
248a8 20 20 20 6e 31 20 2b 3d 20 33 3b 0a 20 20 20 20     n1 += 3;.    
248a9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
248aa 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
248ab 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
248ac 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 41 72 72  te3_snprintf(Arr
248ad 61 79 53 69 7a 65 28 7a 45 72 72 4d 73 67 29 2d  aySize(zErrMsg)-
248ae 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e 31 5d  n1, &zErrMsg[n1]
248af 2c 20 22 25 73 22 2c 20 7a 43 6f 6c 29 3b 0a 20  , "%s", zCol);. 
248b0 20 20 20 20 20 20 20 20 20 20 20 6e 31 20 2b 3d             n1 +=
248b1 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   n2;.          }
248b2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
248b3 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
248b4 6e 74 66 28 41 72 72 61 79 53 69 7a 65 28 7a 45  ntf(ArraySize(zE
248b5 72 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72  rrMsg)-n1, &zErr
248b6 4d 73 67 5b 6e 31 5d 2c 20 0a 20 20 20 20 20 20  Msg[n1], .      
248b7 20 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c        pIdx->nCol
248b8 75 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20 6e 6f  umn>1 ? " are no
248b9 74 20 75 6e 69 71 75 65 22 20 3a 20 22 20 69 73  t unique" : " is
248ba 20 6e 6f 74 20 75 6e 69 71 75 65 22 29 3b 0a 20   not unique");. 
248bb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
248bc 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48  beAddOp4(v, OP_H
248bd 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53  alt, SQLITE_CONS
248be 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c  TRAINT, onError,
248bf 20 30 2c 20 7a 45 72 72 4d 73 67 2c 30 29 3b 0a   0, zErrMsg,0);.
248c0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
248c1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
248c2 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20  e OE_Ignore: {. 
248c3 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
248c4 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b  eenReplace==0 );
248c5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
248c6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
248c7 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65  _Goto, 0, ignore
248c8 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62  Dest);.        b
248c9 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
248ca 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c      case OE_Repl
248cb 61 63 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  ace: {.        s
248cc 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f  qlite3GenerateRo
248cd 77 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20  wDelete(pParse, 
248ce 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72  pTab, baseCur, r
248cf 65 67 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  egR, 0);.       
248d0 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31   seenReplace = 1
248d1 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
248d2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
248d3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
248d4 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20  mpHere(v, j2);. 
248d5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
248d6 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20  mpHere(v, j3);. 
248d7 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
248d8 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
248d9 20 72 65 67 52 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   regR);.  }.}../
248da 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
248db 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65  e generates code
248dc 20 74 6f 20 66 69 6e 69 73 68 20 74 68 65 20 49   to finish the I
248dd 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20  NSERT or UPDATE 
248de 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 74 68 61  operation.** tha
248df 74 20 77 61 73 20 73 74 61 72 74 65 64 20 62 79  t was started by
248e0 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
248e1 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
248e2 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
248e3 2e 0a 2a 2a 20 41 20 63 6f 6e 73 65 63 75 74 69  ..** A consecuti
248e4 76 65 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69  ve range of regi
248e5 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61  sters starting a
248e6 74 20 72 65 67 52 6f 77 69 64 20 63 6f 6e 74 61  t regRowid conta
248e7 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64  ins the.** rowid
248e8 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74   and the content
248e9 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e   to be inserted.
248ea 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  .**.** The argum
248eb 65 6e 74 73 20 74 6f 20 74 68 69 73 20 72 6f 75  ents to this rou
248ec 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20 74  tine should be t
248ed 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 66  he same as the f
248ee 69 72 73 74 20 73 69 78 0a 2a 2a 20 61 72 67 75  irst six.** argu
248ef 6d 65 6e 74 73 20 74 6f 20 73 71 6c 69 74 65 33  ments to sqlite3
248f0 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
248f1 6e 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a 53 51 4c  ntChecks..*/.SQL
248f2 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
248f3 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65   sqlite3Complete
248f4 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20 50 61 72  Insertion(.  Par
248f5 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
248f6 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
248f7 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
248f8 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
248f9 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74  /* the table int
248fa 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69  o which we are i
248fb 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e  nserting */.  in
248fc 74 20 62 61 73 65 43 75 72 2c 20 20 20 20 20 20  t baseCur,      
248fd 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20    /* Index of a 
248fe 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
248ff 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70 54  r pointing at pT
24900 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52  ab */.  int regR
24901 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20 52  owid,       /* R
24902 61 6e 67 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20  ange of content 
24903 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64  */.  int *aRegId
24904 78 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  x,       /* Regi
24905 73 74 65 72 20 75 73 65 64 20 62 79 20 65 61 63  ster used by eac
24906 68 20 69 6e 64 65 78 2e 20 20 30 20 66 6f 72 20  h index.  0 for 
24907 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a  unused indices *
24908 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74 65  /.  int isUpdate
24909 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ,       /* True 
2490a 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c 73  for UPDATE, Fals
2490b 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a  e for INSERT */.
2490c 20 20 69 6e 74 20 6e 65 77 49 64 78 2c 20 20 20    int newIdx,   
2490d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2490e 66 20 4e 45 57 20 74 61 62 6c 65 20 66 6f 72 20  f NEW table for 
2490f 74 72 69 67 67 65 72 73 2e 20 20 2d 31 20 69 66  triggers.  -1 if
24910 20 6e 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61   none */.  int a
24911 70 70 65 6e 64 42 69 61 73 20 20 20 20 20 20 2f  ppendBias      /
24912 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
24913 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61  s likely to be a
24914 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 29 7b 0a 20  n append */.){. 
24915 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a   int i;.  Vdbe *
24916 76 3b 0a 20 20 69 6e 74 20 6e 49 64 78 3b 0a 20  v;.  int nIdx;. 
24917 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
24918 75 38 20 70 69 6b 5f 66 6c 61 67 73 3b 0a 20 20  u8 pik_flags;.  
24919 69 6e 74 20 72 65 67 44 61 74 61 3b 0a 20 20 69  int regData;.  i
2491a 6e 74 20 72 65 67 52 65 63 3b 0a 0a 20 20 76 20  nt regRec;..  v 
2491b 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2491c 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65  (pParse);.  asse
2491d 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 61 73  rt( v!=0 );.  as
2491e 73 65 72 74 28 20 70 54 61 62 2d 3e 70 53 65 6c  sert( pTab->pSel
2491f 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 54 68  ect==0 );  /* Th
24920 69 73 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  is table is not 
24921 61 20 56 49 45 57 20 2a 2f 0a 20 20 66 6f 72 28  a VIEW */.  for(
24922 6e 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 54 61  nIdx=0, pIdx=pTa
24923 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
24924 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
24925 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20 66  t, nIdx++){}.  f
24926 6f 72 28 69 3d 6e 49 64 78 2d 31 3b 20 69 3e 3d  or(i=nIdx-1; i>=
24927 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 66 28  0; i--){.    if(
24928 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30 20 29   aRegIdx[i]==0 )
24929 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 73   continue;.    s
2492a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2492b 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
2492c 2c 20 62 61 73 65 43 75 72 2b 69 2b 31 2c 20 61  , baseCur+i+1, a
2492d 52 65 67 49 64 78 5b 69 5d 29 3b 0a 20 20 7d 0a  RegIdx[i]);.  }.
2492e 20 20 72 65 67 44 61 74 61 20 3d 20 72 65 67 52    regData = regR
2492f 6f 77 69 64 20 2b 20 31 3b 0a 20 20 72 65 67 52  owid + 1;.  regR
24930 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ec = sqlite3GetT
24931 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
24932 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24933 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
24934 63 6f 72 64 2c 20 72 65 67 44 61 74 61 2c 20 70  cord, regData, p
24935 54 61 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65  Tab->nCol, regRe
24936 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62  c);.  sqlite3Tab
24937 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  leAffinityStr(v,
24938 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65   pTab);.  sqlite
24939 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
2493a 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
2493b 20 72 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e   regData, pTab->
2493c 6e 43 6f 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53  nCol);.#ifndef S
2493d 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2493e 45 52 0a 20 20 69 66 28 20 6e 65 77 49 64 78 3e  ER.  if( newIdx>
2493f 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
24940 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
24941 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77 49 64 78  P_Insert, newIdx
24942 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77  , regRec, regRow
24943 69 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  id);.  }.#endif.
24944 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
24945 73 74 65 64 20 29 7b 0a 20 20 20 20 70 69 6b 5f  sted ){.    pik_
24946 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 7d 65 6c  flags = 0;.  }el
24947 73 65 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67  se{.    pik_flag
24948 73 20 3d 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e  s = OPFLAG_NCHAN
24949 47 45 3b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67  GE;.    pik_flag
2494a 73 20 7c 3d 20 28 69 73 55 70 64 61 74 65 3f 4f  s |= (isUpdate?O
2494b 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 3a 4f  PFLAG_ISUPDATE:O
2494c 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 29  PFLAG_LASTROWID)
2494d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 70 70 65  ;.  }.  if( appe
2494e 6e 64 42 69 61 73 20 29 7b 0a 20 20 20 20 70 69  ndBias ){.    pi
2494f 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41  k_flags |= OPFLA
24950 47 5f 41 50 50 45 4e 44 3b 0a 20 20 7d 0a 20 20  G_APPEND;.  }.  
24951 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24952 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
24953 62 61 73 65 43 75 72 2c 20 72 65 67 52 65 63 2c  baseCur, regRec,
24954 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 69 66   regRowid);.  if
24955 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  ( !pParse->neste
24956 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
24957 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
24958 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  -1, pTab->zName,
24959 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d   P4_STATIC);.  }
2495a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
2495b 61 6e 67 65 50 35 28 76 2c 20 70 69 6b 5f 66 6c  angeP5(v, pik_fl
2495c 61 67 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ags);.}../*.** G
2495d 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
2495e 74 20 77 69 6c 6c 20 6f 70 65 6e 20 63 75 72 73  t will open curs
2495f 6f 72 73 20 66 6f 72 20 61 20 74 61 62 6c 65 20  ors for a table 
24960 61 6e 64 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 69  and for all.** i
24961 6e 64 69 63 65 73 20 6f 66 20 74 68 61 74 20 74  ndices of that t
24962 61 62 6c 65 2e 20 20 54 68 65 20 22 62 61 73 65  able.  The "base
24963 43 75 72 22 20 70 61 72 61 6d 65 74 65 72 20 69  Cur" parameter i
24964 73 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  s the cursor num
24965 62 65 72 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20  ber used.** for 
24966 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 64 69  the table.  Indi
24967 63 65 73 20 61 72 65 20 6f 70 65 6e 65 64 20 6f  ces are opened o
24968 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 75 72  n subsequent cur
24969 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  sors..**.** Retu
2496a 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
2496b 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20   indices on the 
2496c 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
2496d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2496e 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
2496f 49 6e 64 69 63 65 73 28 0a 20 20 50 61 72 73 65  Indices(.  Parse
24970 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50   *pParse,   /* P
24971 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
24972 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
24973 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
24974 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
24975 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20  int baseCur,    
24976 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
24977 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  r assigned to th
24978 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
24979 20 6f 70 20 20 20 20 20 20 20 20 20 20 20 2f 2a   op           /*
2497a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20   OP_OpenRead or 
2497b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a  OP_OpenWrite */.
2497c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
2497d 74 20 69 44 62 3b 0a 20 20 49 6e 64 65 78 20 2a  t iDb;.  Index *
2497e 70 49 64 78 3b 0a 20 20 56 64 62 65 20 2a 76 3b  pIdx;.  Vdbe *v;
2497f 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  ..  if( IsVirtua
24980 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  l(pTab) ) return
24981 20 30 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   0;.  iDb = sqli
24982 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
24983 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
24984 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 76  b->pSchema);.  v
24985 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
24986 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73  e(pParse);.  ass
24987 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 73  ert( v!=0 );.  s
24988 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
24989 70 50 61 72 73 65 2c 20 62 61 73 65 43 75 72 2c  pParse, baseCur,
2498a 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29 3b   iDb, pTab, op);
2498b 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78  .  for(i=1, pIdx
2498c 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2498d 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2498e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20  pNext, i++){.   
2498f 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
24990 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
24991 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64  info(pParse, pId
24992 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  x);.    assert( 
24993 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  pIdx->pSchema==p
24994 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  Tab->pSchema );.
24995 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24996 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 2b 62  ddOp4(v, op, i+b
24997 61 73 65 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e  aseCur, pIdx->tn
24998 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20  um, iDb,.       
24999 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2499a 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
2499b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
2499c 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
2499d 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d  ((v, "%s", pIdx-
2499e 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a 20 20  >zName));.  }.  
2499f 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  if( pParse->nTab
249a0 3c 3d 62 61 73 65 43 75 72 2b 69 20 29 7b 0a 20  <=baseCur+i ){. 
249a1 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20     pParse->nTab 
249a2 3d 20 62 61 73 65 43 75 72 2b 69 3b 0a 20 20 7d  = baseCur+i;.  }
249a3 0a 20 20 72 65 74 75 72 6e 20 69 2d 31 3b 0a 7d  .  return i-1;.}
249a4 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ...#ifdef SQLITE
249a5 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20  _TEST./*.** The 
249a6 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c  following global
249a7 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63   variable is inc
249a8 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65  remented wheneve
249a9 72 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 66 65  r the.** transfe
249aa 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  r optimization i
249ab 73 20 75 73 65 64 2e 20 20 54 68 69 73 20 69 73  s used.  This is
249ac 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
249ad 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 6f 6e  g.** purposes on
249ae 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20 73 75 72  ly - to make sur
249af 65 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f  e the transfer o
249b0 70 74 69 6d 69 7a 61 74 69 6f 6e 20 72 65 61 6c  ptimization real
249b1 6c 79 0a 2a 2a 20 69 73 20 68 61 70 70 65 6e 69  ly.** is happeni
249b2 6e 67 20 77 68 65 6e 20 69 74 20 69 73 20 73 75  ng when it is su
249b3 70 70 6f 73 65 20 74 6f 2e 0a 2a 2f 0a 53 51 4c  ppose to..*/.SQL
249b4 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
249b5 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f 75 6e  te3_xferopt_coun
249b6 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  t;.#endif /* SQL
249b7 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 23 69  ITE_TEST */...#i
249b8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
249b9 54 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a 0a 2a 2a  T_XFER_OPT./*.**
249ba 20 43 68 65 63 6b 20 74 6f 20 63 6f 6c 6c 61 74   Check to collat
249bb 69 6f 6e 20 6e 61 6d 65 73 20 74 6f 20 73 65 65  ion names to see
249bc 20 69 66 20 74 68 65 79 20 61 72 65 20 63 6f 6d   if they are com
249bd 70 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  patible..*/.stat
249be 69 63 20 69 6e 74 20 78 66 65 72 43 6f 6d 70 61  ic int xferCompa
249bf 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 63  tibleCollation(c
249c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31 2c 20 63  onst char *z1, c
249c1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 29 7b 0a  onst char *z2){.
249c2 20 20 69 66 28 20 7a 31 3d 3d 30 20 29 7b 0a 20    if( z1==0 ){. 
249c3 20 20 20 72 65 74 75 72 6e 20 7a 32 3d 3d 30 3b     return z2==0;
249c4 0a 20 20 7d 0a 20 20 69 66 28 20 7a 32 3d 3d 30  .  }.  if( z2==0
249c5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
249c6 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
249c7 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31  qlite3StrICmp(z1
249c8 2c 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a 0a 2f 2a  , z2)==0;.}.../*
249c9 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
249ca 20 69 66 20 69 6e 64 65 78 20 70 53 72 63 20 69   if index pSrc i
249cb 73 20 63 6f 6d 70 61 74 69 62 6c 65 20 61 73 20  s compatible as 
249cc 61 20 73 6f 75 72 63 65 20 6f 66 20 64 61 74 61  a source of data
249cd 0a 2a 2a 20 66 6f 72 20 69 6e 64 65 78 20 70 44  .** for index pD
249ce 65 73 74 20 69 6e 20 61 6e 20 69 6e 73 65 72 74  est in an insert
249cf 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
249d0 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 72 75 6c  zation.  The rul
249d1 65 73 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70  es.** for a comp
249d2 61 74 69 62 6c 65 20 69 6e 64 65 78 3a 0a 2a 2a  atible index:.**
249d3 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68 65 20 69  .**    *   The i
249d4 6e 64 65 78 20 69 73 20 6f 76 65 72 20 74 68 65  ndex is over the
249d5 20 73 61 6d 65 20 73 65 74 20 6f 66 20 63 6f 6c   same set of col
249d6 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 54  umns.**    *   T
249d7 68 65 20 73 61 6d 65 20 44 45 53 43 20 61 6e 64  he same DESC and
249d8 20 41 53 43 20 6d 61 72 6b 69 6e 67 73 20 6f 63   ASC markings oc
249d9 63 75 72 73 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75  curs on all colu
249da 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  mns.**    *   Th
249db 65 20 73 61 6d 65 20 6f 6e 45 72 72 6f 72 20 70  e same onError p
249dc 72 6f 63 65 73 73 69 6e 67 20 28 4f 45 5f 41 62  rocessing (OE_Ab
249dd 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20  ort, OE_Ignore, 
249de 65 74 63 29 0a 2a 2a 20 20 20 20 2a 20 20 20 54  etc).**    *   T
249df 68 65 20 73 61 6d 65 20 63 6f 6c 6c 61 74 69 6e  he same collatin
249e0 67 20 73 65 71 75 65 6e 63 65 20 6f 6e 20 65 61  g sequence on ea
249e1 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61  ch column.*/.sta
249e2 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f 6d 70  tic int xferComp
249e3 61 74 69 62 6c 65 49 6e 64 65 78 28 49 6e 64 65  atibleIndex(Inde
249e4 78 20 2a 70 44 65 73 74 2c 20 49 6e 64 65 78 20  x *pDest, Index 
249e5 2a 70 53 72 63 29 7b 0a 20 20 69 6e 74 20 69 3b  *pSrc){.  int i;
249e6 0a 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74  .  assert( pDest
249e7 20 26 26 20 70 53 72 63 20 29 3b 0a 20 20 61 73   && pSrc );.  as
249e8 73 65 72 74 28 20 70 44 65 73 74 2d 3e 70 54 61  sert( pDest->pTa
249e9 62 6c 65 21 3d 70 53 72 63 2d 3e 70 54 61 62 6c  ble!=pSrc->pTabl
249ea 65 20 29 3b 0a 20 20 69 66 28 20 70 44 65 73 74  e );.  if( pDest
249eb 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53 72 63 2d  ->nColumn!=pSrc-
249ec 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  >nColumn ){.    
249ed 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44  return 0;   /* D
249ee 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 72 20  ifferent number 
249ef 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
249f0 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6f  }.  if( pDest->o
249f1 6e 45 72 72 6f 72 21 3d 70 53 72 63 2d 3e 6f 6e  nError!=pSrc->on
249f2 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 72 65 74  Error ){.    ret
249f3 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66  urn 0;   /* Diff
249f4 65 72 65 6e 74 20 63 6f 6e 66 6c 69 63 74 20 72  erent conflict r
249f5 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61 74 65  esolution strate
249f6 67 69 65 73 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f  gies */.  }.  fo
249f7 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e  r(i=0; i<pSrc->n
249f8 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
249f9 20 20 69 66 28 20 70 53 72 63 2d 3e 61 69 43 6f    if( pSrc->aiCo
249fa 6c 75 6d 6e 5b 69 5d 21 3d 70 44 65 73 74 2d 3e  lumn[i]!=pDest->
249fb 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29 7b 0a 20  aiColumn[i] ){. 
249fc 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
249fd 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f   /* Different co
249fe 6c 75 6d 6e 73 20 69 6e 64 65 78 65 64 20 2a 2f  lumns indexed */
249ff 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
24a00 53 72 63 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Src->aSortOrder[
24a01 69 5d 21 3d 70 44 65 73 74 2d 3e 61 53 6f 72 74  i]!=pDest->aSort
24a02 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20  Order[i] ){.    
24a03 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
24a04 20 44 69 66 66 65 72 65 6e 74 20 73 6f 72 74 20   Different sort 
24a05 6f 72 64 65 72 73 20 2a 2f 0a 20 20 20 20 7d 0a  orders */.    }.
24a06 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61 7a      if( pSrc->az
24a07 43 6f 6c 6c 5b 69 5d 21 3d 70 44 65 73 74 2d 3e  Coll[i]!=pDest->
24a08 61 7a 43 6f 6c 6c 5b 69 5d 20 29 7b 0a 20 20 20  azColl[i] ){.   
24a09 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
24a0a 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c  * Different coll
24a0b 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
24a0c 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  */.    }.  }..  
24a0d 2f 2a 20 49 66 20 6e 6f 20 74 65 73 74 20 61 62  /* If no test ab
24a0e 6f 76 65 20 66 61 69 6c 73 20 74 68 65 6e 20 74  ove fails then t
24a0f 68 65 20 69 6e 64 69 63 65 73 20 6d 75 73 74 20  he indices must 
24a10 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 2a 2f  be compatible */
24a11 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
24a12 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 68  /*.** Attempt th
24a13 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d  e transfer optim
24a14 69 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e 53 45 52  ization on INSER
24a15 54 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a  Ts of the form.*
24a16 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20  *.**     INSERT 
24a17 49 4e 54 4f 20 74 61 62 31 20 53 45 4c 45 43 54  INTO tab1 SELECT
24a18 20 2a 20 46 52 4f 4d 20 74 61 62 32 3b 0a 2a 2a   * FROM tab2;.**
24a19 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6d 69 7a  .** This optimiz
24a1a 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 74  ation is only at
24a1b 74 65 6d 70 74 65 64 20 69 66 0a 2a 2a 0a 2a 2a  tempted if.**.**
24a1c 20 20 20 20 28 31 29 20 20 74 61 62 31 20 61 6e      (1)  tab1 an
24a1d 64 20 74 61 62 32 20 68 61 76 65 20 69 64 65 6e  d tab2 have iden
24a1e 74 69 63 61 6c 20 73 63 68 65 6d 61 73 20 69 6e  tical schemas in
24a1f 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 0a  cluding all the.
24a20 2a 2a 20 20 20 20 20 20 20 20 20 73 61 6d 65 20  **         same 
24a21 69 6e 64 69 63 65 73 20 61 6e 64 20 63 6f 6e 73  indices and cons
24a22 74 72 61 69 6e 74 73 0a 2a 2a 0a 2a 2a 20 20 20  traints.**.**   
24a23 20 28 32 29 20 20 74 61 62 31 20 61 6e 64 20 74   (2)  tab1 and t
24a24 61 62 32 20 61 72 65 20 64 69 66 66 65 72 65 6e  ab2 are differen
24a25 74 20 74 61 62 6c 65 73 0a 2a 2a 0a 2a 2a 20 20  t tables.**.**  
24a26 20 20 28 33 29 20 20 54 68 65 72 65 20 6d 75 73    (3)  There mus
24a27 74 20 62 65 20 6e 6f 20 74 72 69 67 67 65 72 73  t be no triggers
24a28 20 6f 6e 20 74 61 62 31 0a 2a 2a 0a 2a 2a 20 20   on tab1.**.**  
24a29 20 20 28 34 29 20 20 54 68 65 20 72 65 73 75 6c    (4)  The resul
24a2a 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c  t set of the SEL
24a2b 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ECT statement is
24a2c 20 22 2a 22 0a 2a 2a 0a 2a 2a 20 20 20 20 28 35   "*".**.**    (5
24a2d 29 20 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  )  The SELECT st
24a2e 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 6f 20 57  atement has no W
24a2f 48 45 52 45 2c 20 48 41 56 49 4e 47 2c 20 4f 52  HERE, HAVING, OR
24a30 44 45 52 20 42 59 2c 20 47 52 4f 55 50 20 42 59  DER BY, GROUP BY
24a31 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 72 20  ,.**         or 
24a32 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a  LIMIT clause..**
24a33 0a 2a 2a 20 20 20 20 28 36 29 20 20 54 68 65 20  .**    (6)  The 
24a34 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
24a35 20 69 73 20 61 20 73 69 6d 70 6c 65 20 28 6e 6f   is a simple (no
24a36 74 20 61 20 63 6f 6d 70 6f 75 6e 64 29 20 73 65  t a compound) se
24a37 6c 65 63 74 20 74 68 61 74 0a 2a 2a 20 20 20 20  lect that.**    
24a38 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 6f 6e       contains on
24a39 6c 79 20 74 61 62 32 20 69 6e 20 69 74 73 20 46  ly tab2 in its F
24a3a 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 0a 2a 2a  ROM clause.**.**
24a3b 20 54 68 69 73 20 6d 65 74 68 6f 64 20 66 6f 72   This method for
24a3c 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68   implementing th
24a3d 65 20 49 4e 53 45 52 54 20 74 72 61 6e 73 66 65  e INSERT transfe
24a3e 72 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66  rs raw records f
24a3f 72 6f 6d 0a 2a 2a 20 74 61 62 32 20 6f 76 65 72  rom.** tab2 over
24a40 20 74 6f 20 74 61 62 31 2e 20 20 54 68 65 20 63   to tab1.  The c
24a41 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 64  olumns are not d
24a42 65 63 6f 64 65 64 2e 20 20 52 61 77 20 72 65 63  ecoded.  Raw rec
24a43 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  ords from.** the
24a44 20 69 6e 64 69 63 65 73 20 6f 66 20 74 61 62 32   indices of tab2
24a45 20 61 72 65 20 74 72 61 6e 73 66 65 72 65 64 20   are transfered 
24a46 74 6f 20 74 61 62 31 20 61 73 20 77 65 6c 6c 2e  to tab1 as well.
24a47 20 20 49 6e 20 73 6f 20 64 6f 69 6e 67 2c 0a 2a    In so doing,.*
24a48 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20  * the resulting 
24a49 74 61 62 31 20 68 61 73 20 6d 75 63 68 20 6c 65  tab1 has much le
24a4a 73 73 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e  ss fragmentation
24a4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
24a4c 74 69 6e 65 20 72 65 74 75 72 6e 73 20 54 52 55  tine returns TRU
24a4d 45 20 69 66 20 74 68 65 20 6f 70 74 69 6d 69 7a  E if the optimiz
24a4e 61 74 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74  ation is attempt
24a4f 65 64 2e 20 20 49 66 20 61 6e 79 0a 2a 2a 20 6f  ed.  If any.** o
24a50 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73  f the conditions
24a51 20 61 62 6f 76 65 20 66 61 69 6c 20 73 6f 20 74   above fail so t
24a52 68 61 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 61  hat the optimiza
24a53 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 0a  tion should not.
24a54 2a 2a 20 62 65 20 61 74 74 65 6d 70 74 65 64 2c  ** be attempted,
24a55 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
24a56 6e 65 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45  ne returns FALSE
24a57 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
24a58 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e  xferOptimization
24a59 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
24a5a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
24a5b 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
24a5c 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20   Table *pDest,  
24a5d 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
24a5e 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72  ble we are inser
24a5f 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 53  ting into */.  S
24a60 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
24a61 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54       /* A SELECT
24a62 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73   statement to us
24a63 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f  e as the data so
24a64 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  urce */.  int on
24a65 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20  Error,          
24a66 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65  /* How to handle
24a67 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f   constraint erro
24a68 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 44  rs */.  int iDbD
24a69 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a  est           /*
24a6a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 66   The database of
24a6b 20 70 44 65 73 74 20 2a 2f 0a 29 7b 0a 20 20 45   pDest */.){.  E
24a6c 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
24a6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a6e 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65  /* The result se
24a6f 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  t of the SELECT 
24a70 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 72 63  */.  Table *pSrc
24a71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
24a72 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
24a73 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  le in the FROM c
24a74 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 43 54 20  lause of SELECT 
24a75 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 53 72 63  */.  Index *pSrc
24a76 49 64 78 2c 20 2a 70 44 65 73 74 49 64 78 3b 20  Idx, *pDestIdx; 
24a77 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
24a78 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  and destination 
24a79 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 73 74 72  indices */.  str
24a7a 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
24a7b 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 2f 2a   *pItem;      /*
24a7c 20 41 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70   An element of p
24a7d 53 65 6c 65 63 74 2d 3e 70 53 72 63 20 2a 2f 0a  Select->pSrc */.
24a7e 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
24a7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a80 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
24a81 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 53  er */.  int iDbS
24a82 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
24a83 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
24a84 64 61 74 61 62 61 73 65 20 6f 66 20 70 53 72 63  database of pSrc
24a85 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c 20   */.  int iSrc, 
24a86 69 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 20  iDest;          
24a87 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
24a88 73 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 61 6e  s from source an
24a89 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f  d destination */
24a8a 0a 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64  .  int addr1, ad
24a8b 64 72 32 3b 20 20 20 20 20 20 20 20 20 20 20 20  dr2;            
24a8c 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 61 64 64 72      /* Loop addr
24a8d 65 73 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 65  esses */.  int e
24a8e 6d 70 74 79 44 65 73 74 54 65 73 74 3b 20 20 20  mptyDestTest;   
24a8f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
24a90 64 64 72 65 73 73 20 6f 66 20 74 65 73 74 20 66  ddress of test f
24a91 6f 72 20 65 6d 70 74 79 20 70 44 65 73 74 20 2a  or empty pDest *
24a92 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79 53 72 63  /.  int emptySrc
24a93 54 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Test;           
24a94 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
24a95 6f 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74  of test for empt
24a96 79 20 70 53 72 63 20 2a 2f 0a 20 20 56 64 62 65  y pSrc */.  Vdbe
24a97 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
24a98 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24a99 54 68 65 20 56 44 42 45 20 77 65 20 61 72 65 20  The VDBE we are 
24a9a 62 75 69 6c 64 69 6e 67 20 2a 2f 0a 20 20 4b 65  building */.  Ke
24a9b 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20  yInfo *pKey;    
24a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24a9d 2a 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69 6f  * Key informatio
24a9e 6e 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 20 2a  n for an index *
24a9f 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69  /.  int regAutoi
24aa0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
24aa1 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72       /* Memory r
24aa2 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20  egister used by 
24aa3 41 55 54 4f 49 4e 43 20 2a 2f 0a 20 20 69 6e 74  AUTOINC */.  int
24aa4 20 64 65 73 74 48 61 73 55 6e 69 71 75 65 49 64   destHasUniqueId
24aa5 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  x = 0;        /*
24aa6 20 54 72 75 65 20 69 66 20 70 44 65 73 74 20 68   True if pDest h
24aa7 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65  as a UNIQUE inde
24aa8 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61  x */.  int regDa
24aa9 74 61 2c 20 72 65 67 52 6f 77 69 64 3b 20 20 20  ta, regRowid;   
24aaa 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
24aab 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 64 61 74  ters holding dat
24aac 61 20 61 6e 64 20 72 6f 77 69 64 20 2a 2f 0a 0a  a and rowid */..
24aad 20 20 69 66 28 20 70 53 65 6c 65 63 74 3d 3d 30    if( pSelect==0
24aae 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
24aaf 3b 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 6f  ;   /* Must be o
24ab0 66 20 74 68 65 20 66 6f 72 6d 20 20 49 4e 53 45  f the form  INSE
24ab1 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45  RT INTO ... SELE
24ab2 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20 20  CT ... */.  }.  
24ab3 69 66 28 20 73 71 6c 69 74 65 33 54 72 69 67 67  if( sqlite3Trigg
24ab4 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  erList(pParse, p
24ab5 44 65 73 74 29 20 29 7b 0a 20 20 20 20 72 65 74  Dest) ){.    ret
24ab6 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31  urn 0;   /* tab1
24ab7 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 74   must not have t
24ab8 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 23  riggers */.  }.#
24ab9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24aba 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
24abb 20 20 69 66 28 20 70 44 65 73 74 2d 3e 74 61 62    if( pDest->tab
24abc 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75  Flags & TF_Virtu
24abd 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  al ){.    return
24abe 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d 75   0;   /* tab1 mu
24abf 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72 74  st not be a virt
24ac0 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d  ual table */.  }
24ac1 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 6f 6e  .#endif.  if( on
24ac2 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c  Error==OE_Defaul
24ac3 74 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f 72  t ){.    onError
24ac4 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d   = OE_Abort;.  }
24ac5 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d  .  if( onError!=
24ac6 4f 45 5f 41 62 6f 72 74 20 26 26 20 6f 6e 45 72  OE_Abort && onEr
24ac7 72 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b  ror!=OE_Rollback
24ac8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
24ac9 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 6f  ;   /* Cannot do
24aca 20 4f 52 20 52 45 50 4c 41 43 45 20 6f 72 20 4f   OR REPLACE or O
24acb 52 20 49 47 4e 4f 52 45 20 6f 72 20 4f 52 20 46  R IGNORE or OR F
24acc 41 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  AIL */.  }.  ass
24acd 65 72 74 28 70 53 65 6c 65 63 74 2d 3e 70 53 72  ert(pSelect->pSr
24ace 63 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74  c);   /* allocat
24acf 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 72 65  ed even if there
24ad0 20 69 73 20 6e 6f 20 46 52 4f 4d 20 63 6c 61 75   is no FROM clau
24ad1 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c  se */.  if( pSel
24ad2 65 63 74 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21  ect->pSrc->nSrc!
24ad3 3d 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =1 ){.    return
24ad4 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c   0;   /* FROM cl
24ad5 61 75 73 65 20 6d 75 73 74 20 68 61 76 65 20 65  ause must have e
24ad6 78 61 63 74 6c 79 20 6f 6e 65 20 74 65 72 6d 20  xactly one term 
24ad7 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65  */.  }.  if( pSe
24ad8 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d  lect->pSrc->a[0]
24ad9 2e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  .pSelect ){.    
24ada 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46  return 0;   /* F
24adb 52 4f 4d 20 63 6c 61 75 73 65 20 63 61 6e 6e 6f  ROM clause canno
24adc 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 75 62 71  t contain a subq
24add 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  uery */.  }.  if
24ade 28 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72  ( pSelect->pWher
24adf 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
24ae0 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d  0;   /* SELECT m
24ae1 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 57 48  ay not have a WH
24ae2 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
24ae3 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  }.  if( pSelect-
24ae4 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
24ae5 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
24ae6 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68  SELECT may not h
24ae7 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ave an ORDER BY 
24ae8 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20  clause */.  }.  
24ae9 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65 65 64 20 74  /* Do not need t
24aea 6f 20 74 65 73 74 20 66 6f 72 20 61 20 48 41 56  o test for a HAV
24aeb 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 49 66 20  ING clause.  If 
24aec 48 41 56 49 4e 47 20 69 73 20 70 72 65 73 65 6e  HAVING is presen
24aed 74 20 62 75 74 0a 20 20 2a 2a 20 74 68 65 72 65  t but.  ** there
24aee 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 2c   is no ORDER BY,
24aef 20 77 65 20 77 69 6c 6c 20 67 65 74 20 61 6e 20   we will get an 
24af0 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20  error. */.  if( 
24af1 70 53 65 6c 65 63 74 2d 3e 70 47 72 6f 75 70 42  pSelect->pGroupB
24af2 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  y ){.    return 
24af3 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d  0;   /* SELECT m
24af4 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 47 52  ay not have a GR
24af5 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  OUP BY clause */
24af6 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65  .  }.  if( pSele
24af7 63 74 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20  ct->pLimit ){.  
24af8 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
24af9 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
24afa 68 61 76 65 20 61 20 4c 49 4d 49 54 20 63 6c 61  have a LIMIT cla
24afb 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  use */.  }.  ass
24afc 65 72 74 28 20 70 53 65 6c 65 63 74 2d 3e 70 4f  ert( pSelect->pO
24afd 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  ffset==0 );  /* 
24afe 4d 75 73 74 20 62 65 20 73 6f 20 69 66 20 70 4c  Must be so if pL
24aff 69 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20 69 66 28  imit==0 */.  if(
24b00 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72   pSelect->pPrior
24b01 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
24b02 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61  ;   /* SELECT ma
24b03 79 20 6e 6f 74 20 62 65 20 61 20 63 6f 6d 70 6f  y not be a compo
24b04 75 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20 20 7d  und query */.  }
24b05 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
24b06 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
24b07 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 72 65  stinct ){.    re
24b08 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c  turn 0;   /* SEL
24b09 45 43 54 20 6d 61 79 20 6e 6f 74 20 62 65 20 44  ECT may not be D
24b0a 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a 20  ISTINCT */.  }. 
24b0b 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63   pEList = pSelec
24b0c 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73  t->pEList;.  ass
24b0d 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
24b0e 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e  ;.  if( pEList->
24b0f 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20  nExpr!=1 ){.    
24b10 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54  return 0;   /* T
24b11 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75  he result set mu
24b12 73 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20  st have exactly 
24b13 6f 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  one column */.  
24b14 7d 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69  }.  assert( pELi
24b15 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20 29  st->a[0].pExpr )
24b16 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e  ;.  if( pEList->
24b17 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d  a[0].pExpr->op!=
24b18 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65  TK_ALL ){.    re
24b19 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65  turn 0;   /* The
24b1a 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74   result set must
24b1b 20 62 65 20 74 68 65 20 73 70 65 63 69 61 6c 20   be the special 
24b1c 6f 70 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f 0a  operator "*" */.
24b1d 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69    }..  /* At thi
24b1e 73 20 70 6f 69 6e 74 20 77 65 20 68 61 76 65 20  s point we have 
24b1f 65 73 74 61 62 6c 69 73 68 65 64 20 74 68 61 74  established that
24b20 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
24b21 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f  s of the.  ** co
24b22 72 72 65 63 74 20 73 79 6e 74 61 63 74 69 63 20  rrect syntactic 
24b23 66 6f 72 6d 20 74 6f 20 70 61 72 74 69 63 69 70  form to particip
24b24 61 74 65 20 69 6e 20 74 68 69 73 20 6f 70 74 69  ate in this opti
24b25 6d 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a 20  mization.  Now. 
24b26 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 63   ** we have to c
24b27 68 65 63 6b 20 74 68 65 20 73 65 6d 61 6e 74 69  heck the semanti
24b28 63 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65 6d  cs..  */.  pItem
24b29 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63   = pSelect->pSrc
24b2a 2d 3e 61 3b 0a 20 20 70 53 72 63 20 3d 20 73 71  ->a;.  pSrc = sq
24b2b 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
24b2c 28 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65  (pParse, 0, pIte
24b2d 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d  m->zName, pItem-
24b2e 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69  >zDatabase);.  i
24b2f 66 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20  f( pSrc==0 ){.  
24b30 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
24b31 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 64 6f 65   FROM clause doe
24b32 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  s not contain a 
24b33 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  real table */.  
24b34 7d 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 70 44  }.  if( pSrc==pD
24b35 65 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  est ){.    retur
24b36 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 61  n 0;   /* tab1 a
24b37 6e 64 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20  nd tab2 may not 
24b38 62 65 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c  be the same tabl
24b39 65 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66  e */.  }.#ifndef
24b3a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
24b3b 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
24b3c 70 53 72 63 2d 3e 74 61 62 46 6c 61 67 73 20 26  pSrc->tabFlags &
24b3d 20 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20   TF_Virtual ){. 
24b3e 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
24b3f 2a 20 74 61 62 32 20 6d 75 73 74 20 6e 6f 74 20  * tab2 must not 
24b40 62 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  be a virtual tab
24b41 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66  le */.  }.#endif
24b42 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70 53 65  .  if( pSrc->pSe
24b43 6c 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75  lect ){.    retu
24b44 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20  rn 0;   /* tab2 
24b45 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 76 69 65  may not be a vie
24b46 77 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  w */.  }.  if( p
24b47 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72 63  Dest->nCol!=pSrc
24b48 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 72 65  ->nCol ){.    re
24b49 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e 75 6d  turn 0;   /* Num
24b4a 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6d  ber of columns m
24b4b 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ust be the same 
24b4c 69 6e 20 74 61 62 31 20 61 6e 64 20 74 61 62 32  in tab1 and tab2
24b4d 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   */.  }.  if( pD
24b4e 65 73 74 2d 3e 69 50 4b 65 79 21 3d 70 53 72 63  est->iPKey!=pSrc
24b4f 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 72  ->iPKey ){.    r
24b50 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 42 6f  eturn 0;   /* Bo
24b51 74 68 20 74 61 62 6c 65 73 20 6d 75 73 74 20 68  th tables must h
24b52 61 76 65 20 74 68 65 20 73 61 6d 65 20 49 4e 54  ave the same INT
24b53 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
24b54 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d   */.  }.  for(i=
24b55 30 3b 20 69 3c 70 44 65 73 74 2d 3e 6e 43 6f 6c  0; i<pDest->nCol
24b56 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
24b57 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 61  pDest->aCol[i].a
24b58 66 66 69 6e 69 74 79 21 3d 70 53 72 63 2d 3e 61  ffinity!=pSrc->a
24b59 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 20  Col[i].affinity 
24b5a 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
24b5b 30 3b 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74  0;    /* Affinit
24b5c 79 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  y must be the sa
24b5d 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e  me on all column
24b5e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69  s */.    }.    i
24b5f 66 28 20 21 78 66 65 72 43 6f 6d 70 61 74 69 62  f( !xferCompatib
24b60 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 44 65 73  leCollation(pDes
24b61 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c  t->aCol[i].zColl
24b62 2c 20 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e  , pSrc->aCol[i].
24b63 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20  zColl) ){.      
24b64 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20  return 0;    /* 
24b65 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
24b66 63 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  ce must be the s
24b67 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d  ame on all colum
24b68 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ns */.    }.    
24b69 69 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b  if( pDest->aCol[
24b6a 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70  i].notNull && !p
24b6b 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74  Src->aCol[i].not
24b6c 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 65  Null ){.      re
24b6d 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74 61  turn 0;    /* ta
24b6e 62 32 20 6d 75 73 74 20 62 65 20 4e 4f 54 20 4e  b2 must be NOT N
24b6f 55 4c 4c 20 69 66 20 74 61 62 31 20 69 73 20 2a  ULL if tab1 is *
24b70 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f  /.    }.  }.  fo
24b71 72 28 70 44 65 73 74 49 64 78 3d 70 44 65 73 74  r(pDestIdx=pDest
24b72 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49  ->pIndex; pDestI
24b73 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65  dx; pDestIdx=pDe
24b74 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  stIdx->pNext){. 
24b75 20 20 20 69 66 28 20 70 44 65 73 74 49 64 78 2d     if( pDestIdx-
24b76 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
24b77 65 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74 48  e ){.      destH
24b78 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20 31 3b  asUniqueIdx = 1;
24b79 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70  .    }.    for(p
24b7a 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e  SrcIdx=pSrc->pIn
24b7b 64 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70 53  dex; pSrcIdx; pS
24b7c 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70  rcIdx=pSrcIdx->p
24b7d 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
24b7e 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49   xferCompatibleI
24b7f 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70  ndex(pDestIdx, p
24b80 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b  SrcIdx) ) break;
24b81 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
24b82 53 72 63 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20  SrcIdx==0 ){.   
24b83 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
24b84 2f 2a 20 70 44 65 73 74 49 64 78 20 68 61 73 20  /* pDestIdx has 
24b85 6e 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  no corresponding
24b86 20 69 6e 64 65 78 20 69 6e 20 70 53 72 63 20 2a   index in pSrc *
24b87 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e  /.    }.  }.#ifn
24b88 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
24b89 43 48 45 43 4b 0a 20 20 69 66 28 20 70 44 65 73  CHECK.  if( pDes
24b8a 74 2d 3e 70 43 68 65 63 6b 20 26 26 20 21 73 71  t->pCheck && !sq
24b8b 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
24b8c 28 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20 70  (pSrc->pCheck, p
24b8d 44 65 73 74 2d 3e 70 43 68 65 63 6b 29 20 29 7b  Dest->pCheck) ){
24b8e 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
24b8f 20 2f 2a 20 54 61 62 6c 65 73 20 68 61 76 65 20   /* Tables have 
24b90 64 69 66 66 65 72 65 6e 74 20 43 48 45 43 4b 20  different CHECK 
24b91 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 54 69  constraints.  Ti
24b92 63 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a 20 20  cket #2252 */.  
24b93 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
24b94 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61  f we get this fa
24b95 72 2c 20 69 74 20 6d 65 61 6e 73 20 65 69 74 68  r, it means eith
24b96 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  er:.  **.  **   
24b97 20 2a 20 20 20 57 65 20 63 61 6e 20 61 6c 77 61   *   We can alwa
24b98 79 73 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66  ys do the transf
24b99 65 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  er if the table 
24b9a 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 20 20 2a 2a  contains an.  **
24b9b 20 20 20 20 20 20 20 20 61 6e 20 69 6e 74 65 67          an integ
24b9c 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20  er primary key. 
24b9d 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20   **.  **    *   
24b9e 57 65 20 63 61 6e 20 63 6f 6e 64 69 74 69 6f 6e  We can condition
24b9f 61 6c 6c 79 20 64 6f 20 74 68 65 20 74 72 61 6e  ally do the tran
24ba0 73 66 65 72 20 69 66 20 74 68 65 20 64 65 73 74  sfer if the dest
24ba1 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  ination.  **    
24ba2 20 20 20 20 74 61 62 6c 65 20 69 73 20 65 6d 70      table is emp
24ba3 74 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  ty..  */.#ifdef 
24ba4 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71  SQLITE_TEST.  sq
24ba5 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f  lite3_xferopt_co
24ba6 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20  unt++;.#endif.  
24ba7 69 44 62 53 72 63 20 3d 20 73 71 6c 69 74 65 33  iDbSrc = sqlite3
24ba8 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
24ba9 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2d 3e  arse->db, pSrc->
24baa 70 53 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20  pSchema);.  v = 
24bab 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
24bac 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
24bad 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
24bae 61 28 70 50 61 72 73 65 2c 20 69 44 62 53 72 63  a(pParse, iDbSrc
24baf 29 3b 0a 20 20 69 53 72 63 20 3d 20 70 50 61 72  );.  iSrc = pPar
24bb0 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 44  se->nTab++;.  iD
24bb1 65 73 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  est = pParse->nT
24bb2 61 62 2b 2b 3b 0a 20 20 72 65 67 41 75 74 6f 69  ab++;.  regAutoi
24bb3 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69  nc = autoIncBegi
24bb4 6e 28 70 50 61 72 73 65 2c 20 69 44 62 44 65 73  n(pParse, iDbDes
24bb5 74 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c  t, pDest);.  sql
24bb6 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
24bb7 61 72 73 65 2c 20 69 44 65 73 74 2c 20 69 44 62  arse, iDest, iDb
24bb8 44 65 73 74 2c 20 70 44 65 73 74 2c 20 4f 50 5f  Dest, pDest, OP_
24bb9 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 69 66  OpenWrite);.  if
24bba 28 20 28 70 44 65 73 74 2d 3e 69 50 4b 65 79 3c  ( (pDest->iPKey<
24bbb 30 20 26 26 20 70 44 65 73 74 2d 3e 70 49 6e 64  0 && pDest->pInd
24bbc 65 78 21 3d 30 29 20 7c 7c 20 64 65 73 74 48 61  ex!=0) || destHa
24bbd 73 55 6e 69 71 75 65 49 64 78 20 29 7b 0a 20 20  sUniqueIdx ){.  
24bbe 20 20 2f 2a 20 49 66 20 74 61 62 6c 65 73 20 64    /* If tables d
24bbf 6f 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 49 4e  o not have an IN
24bc0 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
24bc1 59 20 61 6e 64 20 74 68 65 72 65 0a 20 20 20 20  Y and there.    
24bc2 2a 2a 20 61 72 65 20 69 6e 64 69 63 65 73 20 74  ** are indices t
24bc3 6f 20 62 65 20 63 6f 70 69 65 64 20 61 6e 64 20  o be copied and 
24bc4 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
24bc5 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20  is not empty,.  
24bc6 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20    ** we have to 
24bc7 64 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72 61  disallow the tra
24bc8 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  nsfer optimizati
24bc9 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 0a 20  on because the. 
24bca 20 20 20 2a 2a 20 74 68 65 20 72 6f 77 69 64 73     ** the rowids
24bcb 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 77 68   might change wh
24bcc 69 63 68 20 77 69 6c 6c 20 6d 65 73 73 20 75 70  ich will mess up
24bcd 20 69 6e 64 65 78 69 6e 67 2e 0a 20 20 20 20 2a   indexing..    *
24bce 2a 0a 20 20 20 20 2a 2a 20 4f 72 20 69 66 20 74  *.    ** Or if t
24bcf 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 68  he destination h
24bd0 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65  as a UNIQUE inde
24bd1 78 20 61 6e 64 20 69 73 20 6e 6f 74 20 65 6d 70  x and is not emp
24bd2 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20 61 6c  ty,.    ** we al
24bd3 73 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20  so disallow the 
24bd4 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a  transfer optimiz
24bd5 61 74 69 6f 6e 20 62 65 63 61 75 73 65 20 77 65  ation because we
24bd6 20 63 61 6e 6e 6f 74 0a 20 20 20 20 2a 2a 20 69   cannot.    ** i
24bd7 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 65  nsure that all e
24bd8 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 75 6e  ntries in the un
24bd9 69 6f 6e 20 6f 66 20 44 45 53 54 20 61 6e 64 20  ion of DEST and 
24bda 53 52 43 20 77 69 6c 6c 20 62 65 0a 20 20 20 20  SRC will be.    
24bdb 2a 2a 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a  ** unique..    *
24bdc 2f 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71  /.    addr1 = sq
24bdd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24bde 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 44  v, OP_Rewind, iD
24bdf 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 65 6d 70  est, 0);.    emp
24be0 74 79 44 65 73 74 54 65 73 74 20 3d 20 73 71 6c  tyDestTest = sql
24be1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24be2 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29  , OP_Goto, 0, 0)
24be3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
24be4 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
24be5 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  r1);.  }else{.  
24be6 20 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 20    emptyDestTest 
24be7 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
24be8 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
24be9 73 65 2c 20 69 53 72 63 2c 20 69 44 62 53 72 63  se, iSrc, iDbSrc
24bea 2c 20 70 53 72 63 2c 20 4f 50 5f 4f 70 65 6e 52  , pSrc, OP_OpenR
24beb 65 61 64 29 3b 0a 20 20 65 6d 70 74 79 53 72 63  ead);.  emptySrc
24bec 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Test = sqlite3Vd
24bed 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
24bee 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b  ewind, iSrc, 0);
24bef 0a 20 20 72 65 67 44 61 74 61 20 3d 20 73 71 6c  .  regData = sql
24bf0 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
24bf1 50 61 72 73 65 29 3b 0a 20 20 72 65 67 52 6f 77  Parse);.  regRow
24bf2 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  id = sqlite3GetT
24bf3 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
24bf4 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50 4b    if( pDest->iPK
24bf5 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 64 64  ey>=0 ){.    add
24bf6 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
24bf7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
24bf8 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77  id, iSrc, regRow
24bf9 69 64 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d  id);.    addr2 =
24bfa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24bfb 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
24bfc 74 73 2c 20 69 44 65 73 74 2c 20 30 2c 20 72 65  ts, iDest, 0, re
24bfd 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  gRowid);.    sql
24bfe 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
24bff 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
24c00 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e  E_CONSTRAINT, on
24c01 45 72 72 6f 72 2c 20 30 2c 0a 20 20 20 20 20 20  Error, 0,.      
24c02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24c03 22 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73  "PRIMARY KEY mus
24c04 74 20 62 65 20 75 6e 69 71 75 65 22 2c 20 50 34  t be unique", P4
24c05 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
24c06 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
24c07 65 28 76 2c 20 61 64 64 72 32 29 3b 0a 20 20 20  e(v, addr2);.   
24c08 20 61 75 74 6f 49 6e 63 53 74 65 70 28 70 50 61   autoIncStep(pPa
24c09 72 73 65 2c 20 72 65 67 41 75 74 6f 69 6e 63 2c  rse, regAutoinc,
24c0a 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 65   regRowid);.  }e
24c0b 6c 73 65 20 69 66 28 20 70 44 65 73 74 2d 3e 70  lse if( pDest->p
24c0c 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  Index==0 ){.    
24c0d 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56  addr1 = sqlite3V
24c0e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24c0f 4e 65 77 52 6f 77 69 64 2c 20 69 44 65 73 74 2c  NewRowid, iDest,
24c10 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 65   regRowid);.  }e
24c11 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 31 20 3d  lse{.    addr1 =
24c12 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24c13 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
24c14 69 53 72 63 2c 20 72 65 67 52 6f 77 69 64 29 3b  iSrc, regRowid);
24c15 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44  .    assert( (pD
24c16 65 73 74 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  est->tabFlags & 
24c17 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  TF_Autoincrement
24c18 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71  )==0 );.  }.  sq
24c19 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24c1a 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20 69  v, OP_RowData, i
24c1b 53 72 63 2c 20 72 65 67 44 61 74 61 29 3b 0a 20  Src, regData);. 
24c1c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24c1d 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
24c1e 20 69 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c   iDest, regData,
24c1f 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 73 71   regRowid);.  sq
24c20 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
24c21 35 28 76 2c 20 4f 50 46 4c 41 47 5f 4e 43 48 41  5(v, OPFLAG_NCHA
24c22 4e 47 45 7c 4f 50 46 4c 41 47 5f 4c 41 53 54 52  NGE|OPFLAG_LASTR
24c23 4f 57 49 44 7c 4f 50 46 4c 41 47 5f 41 50 50 45  OWID|OPFLAG_APPE
24c24 4e 44 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ND);.  sqlite3Vd
24c25 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
24c26 2c 20 70 44 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20  , pDest->zName, 
24c27 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  0);.  sqlite3Vdb
24c28 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
24c29 78 74 2c 20 69 53 72 63 2c 20 61 64 64 72 31 29  xt, iSrc, addr1)
24c2a 3b 0a 20 20 61 75 74 6f 49 6e 63 45 6e 64 28 70  ;.  autoIncEnd(p
24c2b 50 61 72 73 65 2c 20 69 44 62 44 65 73 74 2c 20  Parse, iDbDest, 
24c2c 70 44 65 73 74 2c 20 72 65 67 41 75 74 6f 69 6e  pDest, regAutoin
24c2d 63 29 3b 0a 20 20 66 6f 72 28 70 44 65 73 74 49  c);.  for(pDestI
24c2e 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65 78  dx=pDest->pIndex
24c2f 3b 20 70 44 65 73 74 49 64 78 3b 20 70 44 65 73  ; pDestIdx; pDes
24c30 74 49 64 78 3d 70 44 65 73 74 49 64 78 2d 3e 70  tIdx=pDestIdx->p
24c31 4e 65 78 74 29 7b 0a 20 20 20 20 66 6f 72 28 70  Next){.    for(p
24c32 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e  SrcIdx=pSrc->pIn
24c33 64 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70 53  dex; pSrcIdx; pS
24c34 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70  rcIdx=pSrcIdx->p
24c35 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
24c36 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49   xferCompatibleI
24c37 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70  ndex(pDestIdx, p
24c38 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b  SrcIdx) ) break;
24c39 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
24c3a 74 28 20 70 53 72 63 49 64 78 20 29 3b 0a 20 20  t( pSrcIdx );.  
24c3b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24c3c 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
24c3d 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 73   iSrc, 0);.    s
24c3e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24c3f 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 44  (v, OP_Close, iD
24c40 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 4b 65  est, 0);.    pKe
24c41 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
24c42 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
24c43 70 53 72 63 49 64 78 29 3b 0a 20 20 20 20 73 71  pSrcIdx);.    sq
24c44 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
24c45 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20  v, OP_OpenRead, 
24c46 69 53 72 63 2c 20 70 53 72 63 49 64 78 2d 3e 74  iSrc, pSrcIdx->t
24c47 6e 75 6d 2c 20 69 44 62 53 72 63 2c 0a 20 20 20  num, iDbSrc,.   
24c48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24c49 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20     (char*)pKey, 
24c4a 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
24c4b 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  FF);.    VdbeCom
24c4c 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70  ment((v, "%s", p
24c4d 53 72 63 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  SrcIdx->zName));
24c4e 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c 69  .    pKey = sqli
24c4f 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
24c50 70 50 61 72 73 65 2c 20 70 44 65 73 74 49 64 78  pParse, pDestIdx
24c51 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
24c52 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
24c53 70 65 6e 57 72 69 74 65 2c 20 69 44 65 73 74 2c  penWrite, iDest,
24c54 20 70 44 65 73 74 49 64 78 2d 3e 74 6e 75 6d 2c   pDestIdx->tnum,
24c55 20 69 44 62 44 65 73 74 2c 0a 20 20 20 20 20 20   iDbDest,.      
24c56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24c57 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
24c58 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
24c59 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
24c5a 74 28 28 76 2c 20 22 25 73 22 2c 20 70 44 65 73  t((v, "%s", pDes
24c5b 74 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20  tIdx->zName));. 
24c5c 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
24c5d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
24c5e 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c  OP_Rewind, iSrc,
24c5f 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
24c60 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24c61 5f 52 6f 77 4b 65 79 2c 20 69 53 72 63 2c 20 72  _RowKey, iSrc, r
24c62 65 67 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c  egData);.    sql
24c63 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
24c64 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
24c65 69 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c 20  iDest, regData, 
24c66 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
24c67 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24c68 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64 64 72  Next, iSrc, addr
24c69 31 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  1+1);.    sqlite
24c6a 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
24c6b 20 61 64 64 72 31 29 3b 0a 20 20 7d 0a 20 20 73   addr1);.  }.  s
24c6c 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
24c6d 72 65 28 76 2c 20 65 6d 70 74 79 53 72 63 54 65  re(v, emptySrcTe
24c6e 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  st);.  sqlite3Re
24c6f 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
24c70 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  rse, regRowid);.
24c71 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
24c72 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
24c73 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c 69  regData);.  sqli
24c74 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24c75 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63 2c   OP_Close, iSrc,
24c76 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   0);.  sqlite3Vd
24c77 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
24c78 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29 3b  lose, iDest, 0);
24c79 0a 20 20 69 66 28 20 65 6d 70 74 79 44 65 73 74  .  if( emptyDest
24c7a 54 65 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Test ){.    sqli
24c7b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24c7c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
24c7d 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  _OK, 0);.    sql
24c7e 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
24c7f 28 76 2c 20 65 6d 70 74 79 44 65 73 74 54 65 73  (v, emptyDestTes
24c80 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
24c81 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24c82 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29  Close, iDest, 0)
24c83 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
24c84 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
24c85 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  urn 1;.  }.}.#en
24c86 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
24c87 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a  IT_XFER_OPT */..
24c88 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73  /* Make sure "is
24c89 56 69 65 77 22 20 67 65 74 73 20 75 6e 64 65 66  View" gets undef
24c8a 69 6e 65 64 20 69 6e 20 63 61 73 65 20 74 68 69  ined in case thi
24c8b 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70  s file becomes p
24c8c 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 6d  art of.** the am
24c8d 61 6c 67 61 6d 61 74 69 6f 6e 20 2d 20 73 6f 20  algamation - so 
24c8e 74 68 61 74 20 73 75 62 73 65 71 75 65 6e 74 20  that subsequent 
24c8f 66 69 6c 65 73 20 64 6f 20 6e 6f 74 20 73 65 65  files do not see
24c90 20 69 73 56 69 65 77 20 61 73 20 61 0a 2a 2a 20   isView as a.** 
24c91 6d 61 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64 65 66  macro. */.#undef
24c92 20 69 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a   isView../******
24c93 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
24c94 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  insert.c *******
24c95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24c96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24c97 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
24c98 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
24c99 69 6c 65 20 6c 65 67 61 63 79 2e 63 20 2a 2a 2a  ile legacy.c ***
24c9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24c9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24c9c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
24c9d 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
24c9e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
24c9f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
24ca0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
24ca1 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
24ca2 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
24ca3 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
24ca4 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
24ca5 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
24ca6 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
24ca7 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
24ca8 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
24ca9 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
24caa 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
24cab 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
24cac 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
24cad 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
24cae 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
24caf 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
24cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24cb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24cb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24cb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
24cb4 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72  ** Main file for
24cb5 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
24cb6 61 72 79 2e 20 20 54 68 65 20 72 6f 75 74 69 6e  ary.  The routin
24cb7 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a  es in this file.
24cb8 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  ** implement the
24cb9 20 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65   programmer inte
24cba 72 66 61 63 65 20 74 6f 20 74 68 65 20 6c 69 62  rface to the lib
24cbb 72 61 72 79 2e 20 20 52 6f 75 74 69 6e 65 73 20  rary.  Routines 
24cbc 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65  in.** other file
24cbd 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e  s are for intern
24cbe 61 6c 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  al use by SQLite
24cbf 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20   and should not 
24cc0 62 65 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 62  be.** accessed b
24cc1 79 20 75 73 65 72 73 20 6f 66 20 74 68 65 20 6c  y users of the l
24cc2 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49  ibrary..**.** $I
24cc3 64 3a 20 6c 65 67 61 63 79 2e 63 2c 76 20 31 2e  d: legacy.c,v 1.
24cc4 33 32 20 32 30 30 39 2f 30 33 2f 31 39 20 31 38  32 2009/03/19 18
24cc5 3a 35 31 3a 30 37 20 64 61 6e 69 65 6c 6b 31 39  :51:07 danielk19
24cc6 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a  77 Exp $.*/.../*
24cc7 0a 2a 2a 20 45 78 65 63 75 74 65 20 53 51 4c 20  .** Execute SQL 
24cc8 63 6f 64 65 2e 20 20 52 65 74 75 72 6e 20 6f 6e  code.  Return on
24cc9 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f  e of the SQLITE_
24cca 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65   success/failure
24ccb 0a 2a 2a 20 63 6f 64 65 73 2e 20 20 41 6c 73 6f  .** codes.  Also
24ccc 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20   write an error 
24ccd 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 6d 65 6d  message into mem
24cce 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
24ccf 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e  m.** malloc() an
24cd0 64 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67  d make *pzErrMsg
24cd1 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 6d   point to that m
24cd2 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  essage..**.** If
24cd3 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 71 75   the SQL is a qu
24cd4 65 72 79 2c 20 74 68 65 6e 20 66 6f 72 20 65 61  ery, then for ea
24cd5 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20 71 75  ch row in the qu
24cd6 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 74 68  ery result.** th
24cd7 65 20 78 43 61 6c 6c 62 61 63 6b 28 29 20 66 75  e xCallback() fu
24cd8 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
24cd9 2e 20 20 70 41 72 67 20 62 65 63 6f 6d 65 73 20  .  pArg becomes 
24cda 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67  the first.** arg
24cdb 75 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61  ument to xCallba
24cdc 63 6b 28 29 2e 20 20 49 66 20 78 43 61 6c 6c 62  ck().  If xCallb
24cdd 61 63 6b 3d 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f  ack=NULL then no
24cde 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20   callback.** is 
24cdf 69 6e 76 6f 6b 65 64 2c 20 65 76 65 6e 20 66 6f  invoked, even fo
24ce0 72 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 53 51  r queries..*/.SQ
24ce1 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
24ce2 69 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71 6c  ite3_exec(.  sql
24ce3 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
24ce4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
24ce5 64 61 74 61 62 61 73 65 20 6f 6e 20 77 68 69 63  database on whic
24ce6 68 20 74 68 65 20 53 51 4c 20 65 78 65 63 75 74  h the SQL execut
24ce7 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  es */.  const ch
24ce8 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
24ce9 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74      /* The SQL t
24cea 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 2a 2f  o be executed */
24ceb 0a 20 20 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62  .  sqlite3_callb
24cec 61 63 6b 20 78 43 61 6c 6c 62 61 63 6b 2c 20 2f  ack xCallback, /
24ced 2a 20 49 6e 76 6f 6b 65 20 74 68 69 73 20 63 61  * Invoke this ca
24cee 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 2a  llback routine *
24cef 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 20  /.  void *pArg, 
24cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24cf1 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
24cf2 74 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29  t to xCallback()
24cf3 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45   */.  char **pzE
24cf4 72 72 4d 73 67 20 20 20 20 20 20 20 20 20 20 20  rrMsg           
24cf5 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72    /* Write error
24cf6 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a   messages here *
24cf7 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  /.){.  int rc = 
24cf8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e  SQLITE_OK;.  con
24cf9 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 6f 76  st char *zLeftov
24cfa 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  er;.  sqlite3_st
24cfb 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a 20  mt *pStmt = 0;. 
24cfc 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 73 20 3d   char **azCols =
24cfd 20 30 3b 0a 0a 20 20 69 6e 74 20 6e 52 65 74 72   0;..  int nRetr
24cfe 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 43 61  y = 0;.  int nCa
24cff 6c 6c 62 61 63 6b 3b 0a 0a 20 20 69 66 28 20 7a  llback;..  if( z
24d00 53 71 6c 3d 3d 30 20 29 20 7a 53 71 6c 20 3d 20  Sql==0 ) zSql = 
24d01 22 22 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d  "";..  sqlite3_m
24d02 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
24d03 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  utex);.  sqlite3
24d04 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
24d05 5f 4f 4b 2c 20 30 29 3b 0a 20 20 77 68 69 6c 65  _OK, 0);.  while
24d06 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  ( (rc==SQLITE_OK
24d07 20 7c 7c 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f   || (rc==SQLITE_
24d08 53 43 48 45 4d 41 20 26 26 20 28 2b 2b 6e 52 65  SCHEMA && (++nRe
24d09 74 72 79 29 3c 32 29 29 20 26 26 20 7a 53 71 6c  try)<2)) && zSql
24d0a 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  [0] ){.    int n
24d0b 43 6f 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 2a  Col;.    char **
24d0c 61 7a 56 61 6c 73 20 3d 20 30 3b 0a 0a 20 20 20  azVals = 0;..   
24d0d 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20   pStmt = 0;.    
24d0e 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
24d0f 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  pare(db, zSql, -
24d10 31 2c 20 26 70 53 74 6d 74 2c 20 26 7a 4c 65 66  1, &pStmt, &zLef
24d11 74 6f 76 65 72 29 3b 0a 20 20 20 20 61 73 73 65  tover);.    asse
24d12 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
24d13 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29 3b  K || pStmt==0 );
24d14 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
24d15 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
24d16 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a  continue;.    }.
24d17 20 20 20 20 69 66 28 20 21 70 53 74 6d 74 20 29      if( !pStmt )
24d18 7b 0a 20 20 20 20 20 20 2f 2a 20 74 68 69 73 20  {.      /* this 
24d19 68 61 70 70 65 6e 73 20 66 6f 72 20 61 20 63 6f  happens for a co
24d1a 6d 6d 65 6e 74 20 6f 72 20 77 68 69 74 65 2d 73  mment or white-s
24d1b 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 7a 53  pace */.      zS
24d1c 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b 0a  ql = zLeftover;.
24d1d 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
24d1e 20 20 20 20 7d 0a 0a 20 20 20 20 6e 43 61 6c 6c      }..    nCall
24d1f 62 61 63 6b 20 3d 20 30 3b 0a 20 20 20 20 6e 43  back = 0;.    nC
24d20 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ol = sqlite3_col
24d21 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29  umn_count(pStmt)
24d22 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 31 20  ;..    while( 1 
24d23 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
24d24 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
24d25 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a  e3_step(pStmt);.
24d26 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  .      /* Invoke
24d27 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
24d28 6e 63 74 69 6f 6e 20 69 66 20 72 65 71 75 69 72  nction if requir
24d29 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ed */.      if( 
24d2a 78 43 61 6c 6c 62 61 63 6b 20 26 26 20 28 53 51  xCallback && (SQ
24d2b 4c 49 54 45 5f 52 4f 57 3d 3d 72 63 20 7c 7c 20  LITE_ROW==rc || 
24d2c 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49  .          (SQLI
24d2d 54 45 5f 44 4f 4e 45 3d 3d 72 63 20 26 26 20 21  TE_DONE==rc && !
24d2e 6e 43 61 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d  nCallback && db-
24d2f 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 4e 75  >flags&SQLITE_Nu
24d30 6c 6c 43 61 6c 6c 62 61 63 6b 29 29 20 29 7b 0a  llCallback)) ){.
24d31 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 6e          if( 0==n
24d32 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Callback ){.    
24d33 20 20 20 20 20 20 69 66 28 20 61 7a 43 6f 6c 73        if( azCols
24d34 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
24d35 20 20 20 61 7a 43 6f 6c 73 20 3d 20 73 71 6c 69     azCols = sqli
24d36 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
24d37 64 62 2c 20 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f  db, 2*nCol*sizeo
24d38 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 2b  f(const char*) +
24d39 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   1);.           
24d3a 20 69 66 28 20 61 7a 43 6f 6c 73 3d 3d 30 20 29   if( azCols==0 )
24d3b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
24d3c 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20  goto exec_out;. 
24d3d 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
24d3e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
24d3f 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
24d40 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
24d41 20 20 20 20 20 20 61 7a 43 6f 6c 73 5b 69 5d 20        azCols[i] 
24d42 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
24d43 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53  3_column_name(pS
24d44 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 20  tmt, i);.       
24d45 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 56       /* sqlite3V
24d46 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 29 20  dbeSetColName() 
24d47 69 6e 73 74 61 6c 6c 73 20 63 6f 6c 75 6d 6e 20  installs column 
24d48 6e 61 6d 65 73 20 61 73 20 55 54 46 38 0a 20 20  names as UTF8.  
24d49 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 74 72            ** str
24d4a 69 6e 67 73 20 73 6f 20 74 68 65 72 65 20 69 73  ings so there is
24d4b 20 6e 6f 20 77 61 79 20 66 6f 72 20 73 71 6c 69   no way for sqli
24d4c 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28  te3_column_name(
24d4d 29 20 74 6f 20 66 61 69 6c 2e 20 2a 2f 0a 20 20  ) to fail. */.  
24d4e 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
24d4f 28 20 61 7a 43 6f 6c 73 5b 69 5d 21 3d 30 20 29  ( azCols[i]!=0 )
24d50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
24d51 20 20 20 20 20 20 20 20 6e 43 61 6c 6c 62 61 63          nCallbac
24d52 6b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  k++;.        }. 
24d53 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
24d54 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
24d55 20 20 20 20 20 20 20 61 7a 56 61 6c 73 20 3d 20         azVals = 
24d56 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a 20  &azCols[nCol];. 
24d57 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30           for(i=0
24d58 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
24d59 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 56 61              azVa
24d5a 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a 29  ls[i] = (char *)
24d5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
24d5c 65 78 74 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20  ext(pStmt, i);. 
24d5d 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21             if( !
24d5e 61 7a 56 61 6c 73 5b 69 5d 20 26 26 20 73 71 6c  azVals[i] && sql
24d5f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
24d60 28 70 53 74 6d 74 2c 20 69 29 21 3d 53 51 4c 49  (pStmt, i)!=SQLI
24d61 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20  TE_NULL ){.     
24d62 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c           db->mal
24d63 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
24d64 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74               got
24d65 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20  o exec_out;.    
24d66 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
24d67 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
24d68 20 20 20 20 20 20 20 20 69 66 28 20 78 43 61 6c          if( xCal
24d69 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 43 6f 6c  lback(pArg, nCol
24d6a 2c 20 61 7a 56 61 6c 73 2c 20 61 7a 43 6f 6c 73  , azVals, azCols
24d6b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  ) ){.          r
24d6c 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  c = SQLITE_ABORT
24d6d 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
24d6e 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
24d6f 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a  (Vdbe *)pStmt);.
24d70 20 20 20 20 20 20 20 20 20 20 70 53 74 6d 74 20            pStmt 
24d71 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 73  = 0;.          s
24d72 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
24d73 53 51 4c 49 54 45 5f 41 42 4f 52 54 2c 20 30 29  SQLITE_ABORT, 0)
24d74 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
24d75 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20   exec_out;.     
24d76 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
24d77 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
24d78 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20  TE_ROW ){.      
24d79 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
24d7a 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65  beFinalize((Vdbe
24d7b 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 20 20 20   *)pStmt);.     
24d7c 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20     pStmt = 0;.  
24d7d 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
24d7e 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20  LITE_SCHEMA ){. 
24d7f 20 20 20 20 20 20 20 20 20 6e 52 65 74 72 79 20           nRetry 
24d80 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  = 0;.          z
24d81 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b  Sql = zLeftover;
24d82 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
24d83 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ( sqlite3Isspace
24d84 28 7a 53 71 6c 5b 30 5d 29 20 29 20 7a 53 71 6c  (zSql[0]) ) zSql
24d85 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ++;.        }.  
24d86 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
24d87 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
24d88 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
24d89 2c 20 61 7a 43 6f 6c 73 29 3b 0a 20 20 20 20 61  , azCols);.    a
24d8a 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20 20 7d 0a 0a  zCols = 0;.  }..
24d8b 65 78 65 63 5f 6f 75 74 3a 0a 20 20 69 66 28 20  exec_out:.  if( 
24d8c 70 53 74 6d 74 20 29 20 73 71 6c 69 74 65 33 56  pStmt ) sqlite3V
24d8d 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
24d8e 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 73 71  e *)pStmt);.  sq
24d8f 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24d90 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20 72 63 20 3d  azCols);..  rc =
24d91 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
24d92 64 62 2c 20 72 63 29 3b 0a 20 20 69 66 28 20 72  db, rc);.  if( r
24d93 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
24d94 72 63 3d 3d 73 71 6c 69 74 65 33 5f 65 72 72 63  rc==sqlite3_errc
24d95 6f 64 65 28 64 62 29 20 26 26 20 70 7a 45 72 72  ode(db) && pzErr
24d96 4d 73 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  Msg ){.    int n
24d97 45 72 72 4d 73 67 20 3d 20 31 20 2b 20 73 71 6c  ErrMsg = 1 + sql
24d98 69 74 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c  ite3Strlen30(sql
24d99 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29  ite3_errmsg(db))
24d9a 3b 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20  ;.    *pzErrMsg 
24d9b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
24d9c 6e 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66  nErrMsg);.    if
24d9d 28 20 2a 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  ( *pzErrMsg ){. 
24d9e 20 20 20 20 20 6d 65 6d 63 70 79 28 2a 70 7a 45       memcpy(*pzE
24d9f 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 5f 65  rrMsg, sqlite3_e
24da0 72 72 6d 73 67 28 64 62 29 2c 20 6e 45 72 72 4d  rrmsg(db), nErrM
24da1 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  sg);.    }.  }el
24da2 73 65 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20  se if( pzErrMsg 
24da3 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67  ){.    *pzErrMsg
24da4 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 61 73 73   = 0;.  }..  ass
24da5 65 72 74 28 20 28 72 63 26 64 62 2d 3e 65 72 72  ert( (rc&db->err
24da6 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 73  Mask)==rc );.  s
24da7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
24da8 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
24da9 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
24daa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
24dab 6e 64 20 6f 66 20 6c 65 67 61 63 79 2e 63 20 2a  nd of legacy.c *
24dac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24dad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24dae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
24daf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
24db0 65 67 69 6e 20 66 69 6c 65 20 6c 6f 61 64 65 78  egin file loadex
24db1 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
24db2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24db3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
24db4 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20 37  *.** 2006 June 7
24db5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
24db6 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
24db7 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
24db8 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
24db9 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
24dba 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
24dbb 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
24dbc 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
24dbd 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
24dbe 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
24dbf 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
24dc0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
24dc1 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
24dc2 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
24dc3 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
24dc4 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
24dc5 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
24dc6 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
24dc7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24dc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24dc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24dca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
24dcb 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
24dcc 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
24dcd 74 6f 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 6c  to dynamically l
24dce 6f 61 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 69  oad extensions i
24dcf 6e 74 6f 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  nto.** the SQLit
24dd0 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  e library..**.**
24dd1 20 24 49 64 3a 20 6c 6f 61 64 65 78 74 2e 63 2c   $Id: loadext.c,
24dd2 76 20 31 2e 35 38 20 32 30 30 39 2f 30 31 2f 32  v 1.58 2009/01/2
24dd3 30 20 31 36 3a 35 33 3a 34 30 20 64 61 6e 69 65  0 16:53:40 danie
24dd4 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
24dd5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24dd6 43 4f 52 45 0a 20 20 23 64 65 66 69 6e 65 20 53  CORE.  #define S
24dd7 51 4c 49 54 45 5f 43 4f 52 45 20 31 20 20 2f 2a  QLITE_CORE 1  /*
24dd8 20 44 69 73 61 62 6c 65 20 74 68 65 20 41 50 49   Disable the API
24dd9 20 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 69 6e   redefinition in
24dda 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2f   sqlite3ext.h */
24ddb 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a 2a  .#endif./*******
24ddc 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
24ddd 73 71 6c 69 74 65 33 65 78 74 2e 68 20 69 6e 20  sqlite3ext.h in 
24dde 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6c 6f  the middle of lo
24ddf 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  adext.c ********
24de0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
24de1 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
24de2 6c 65 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20  le sqlite3ext.h 
24de3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24de4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24de5 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
24de6 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20  06 June 7.**.** 
24de7 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
24de8 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
24de9 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
24dea 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
24deb 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
24dec 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
24ded 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
24dee 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
24def 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
24df0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
24df1 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
24df2 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
24df3 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
24df4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
24df5 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
24df6 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
24df7 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
24df8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24df9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24dfa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24dfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24dfc 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
24dfd 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66   header file def
24dfe 69 6e 65 73 20 74 68 65 20 53 51 4c 69 74 65 20  ines the SQLite 
24dff 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 75 73  interface for us
24e00 65 20 62 79 0a 2a 2a 20 73 68 61 72 65 64 20 6c  e by.** shared l
24e01 69 62 72 61 72 69 65 73 20 74 68 61 74 20 77 61  ibraries that wa
24e02 6e 74 20 74 6f 20 62 65 20 69 6d 70 6f 72 74 65  nt to be importe
24e03 64 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20  d as extensions 
24e04 69 6e 74 6f 0a 2a 2a 20 61 6e 20 53 51 4c 69 74  into.** an SQLit
24e05 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 68 61  e instance.  Sha
24e06 72 65 64 20 6c 69 62 72 61 72 69 65 73 20 74 68  red libraries th
24e07 61 74 20 69 6e 74 65 6e 64 20 74 6f 20 62 65 20  at intend to be 
24e08 6c 6f 61 64 65 64 0a 2a 2a 20 61 73 20 65 78 74  loaded.** as ext
24e09 65 6e 73 69 6f 6e 73 20 62 79 20 53 51 4c 69 74  ensions by SQLit
24e0a 65 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75 64  e should #includ
24e0b 65 20 74 68 69 73 20 66 69 6c 65 20 69 6e 73 74  e this file inst
24e0c 65 61 64 20 6f 66 20 0a 2a 2a 20 73 71 6c 69 74  ead of .** sqlit
24e0d 65 33 2e 68 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  e3.h..**.** @(#)
24e0e 20 24 49 64 3a 20 73 71 6c 69 74 65 33 65 78 74   $Id: sqlite3ext
24e0f 2e 68 2c 76 20 31 2e 32 35 20 32 30 30 38 2f 31  .h,v 1.25 2008/1
24e10 30 2f 31 32 20 30 30 3a 32 37 3a 35 34 20 73 68  0/12 00:27:54 sh
24e11 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  ane Exp $.*/.#if
24e12 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 45 58 54  ndef _SQLITE3EXT
24e13 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
24e14 49 54 45 33 45 58 54 5f 48 5f 0a 0a 74 79 70 65  ITE3EXT_H_..type
24e15 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
24e16 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
24e17 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
24e18 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ines;../*.** The
24e19 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
24e1a 74 75 72 65 20 68 6f 6c 64 73 20 70 6f 69 6e 74  ture holds point
24e1b 65 72 73 20 74 6f 20 61 6c 6c 20 6f 66 20 74 68  ers to all of th
24e1c 65 20 53 51 4c 69 74 65 20 41 50 49 0a 2a 2a 20  e SQLite API.** 
24e1d 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
24e1e 57 41 52 4e 49 4e 47 3a 20 20 49 6e 20 6f 72 64  WARNING:  In ord
24e1f 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 62  er to maintain b
24e20 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
24e21 62 69 6c 69 74 79 2c 20 61 64 64 20 6e 65 77 0a  bility, add new.
24e22 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f  ** interfaces to
24e23 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73   the end of this
24e24 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 2e   structure only.
24e25 20 20 49 66 20 79 6f 75 20 69 6e 73 65 72 74 20    If you insert 
24e26 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  new.** interface
24e27 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  s in the middle 
24e28 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
24e29 65 2c 20 74 68 65 6e 20 6f 6c 64 65 72 20 64 69  e, then older di
24e2a 66 66 65 72 65 6e 74 0a 2a 2a 20 76 65 72 73 69  fferent.** versi
24e2b 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 77 69  ons of SQLite wi
24e2c 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
24e2d 6f 20 6c 6f 61 64 20 65 61 63 68 20 6f 74 68 65  o load each othe
24e2e 72 73 27 20 73 68 61 72 65 64 0a 2a 2a 20 6c 69  rs' shared.** li
24e2f 62 72 61 72 69 65 73 21 0a 2a 2f 0a 73 74 72 75  braries!.*/.stru
24e30 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  ct sqlite3_api_r
24e31 6f 75 74 69 6e 65 73 20 7b 0a 20 20 76 6f 69 64  outines {.  void
24e32 20 2a 20 28 2a 61 67 67 72 65 67 61 74 65 5f 63   * (*aggregate_c
24e33 6f 6e 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f  ontext)(sqlite3_
24e34 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20 6e 42 79  context*,int nBy
24e35 74 65 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 61  tes);.  int  (*a
24e36 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 29 28  ggregate_count)(
24e37 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
24e38 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
24e39 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73  _blob)(sqlite3_s
24e3a 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  tmt*,int,const v
24e3b 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28  oid*,int n,void(
24e3c 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  *)(void*));.  in
24e3d 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75 62 6c 65  t  (*bind_double
24e3e 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
24e3f 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0a 20 20 69  int,double);.  i
24e40 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 29 28  nt  (*bind_int)(
24e41 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
24e42 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  t,int);.  int  (
24e43 2a 62 69 6e 64 5f 69 6e 74 36 34 29 28 73 71 6c  *bind_int64)(sql
24e44 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 73  ite3_stmt*,int,s
24e45 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20  qlite_int64);.  
24e46 69 6e 74 20 20 28 2a 62 69 6e 64 5f 6e 75 6c 6c  int  (*bind_null
24e47 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
24e48 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  int);.  int  (*b
24e49 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
24e4a 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  unt)(sqlite3_stm
24e4b 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  t*);.  int  (*bi
24e4c 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
24e4d 65 78 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ex)(sqlite3_stmt
24e4e 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61  *,const char*zNa
24e4f 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  me);.  const cha
24e50 72 20 2a 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d  r * (*bind_param
24e51 65 74 65 72 5f 6e 61 6d 65 29 28 73 71 6c 69 74  eter_name)(sqlit
24e52 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
24e53 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74 65 78   int  (*bind_tex
24e54 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t)(sqlite3_stmt*
24e55 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
24e56 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76  ,int n,void(*)(v
24e57 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28  oid*));.  int  (
24e58 2a 62 69 6e 64 5f 74 65 78 74 31 36 29 28 73 71  *bind_text16)(sq
24e59 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
24e5a 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
24e5b 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
24e5c 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 76  .  int  (*bind_v
24e5d 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74  alue)(sqlite3_st
24e5e 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 73 71  mt*,int,const sq
24e5f 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
24e60 20 69 6e 74 20 20 28 2a 62 75 73 79 5f 68 61 6e   int  (*busy_han
24e61 64 6c 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69  dler)(sqlite3*,i
24e62 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29  nt(*)(void*,int)
24e63 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20  ,void*);.  int  
24e64 28 2a 62 75 73 79 5f 74 69 6d 65 6f 75 74 29 28  (*busy_timeout)(
24e65 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 6d 73 29  sqlite3*,int ms)
24e66 3b 0a 20 20 69 6e 74 20 20 28 2a 63 68 61 6e 67  ;.  int  (*chang
24e67 65 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  es)(sqlite3*);. 
24e68 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65 29 28 73   int  (*close)(s
24e69 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20  qlite3*);.  int 
24e6a 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65   (*collation_nee
24e6b 64 65 64 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ded)(sqlite3*,vo
24e6c 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  id*,void(*)(void
24e6d 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
24e6e 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
24e6f 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ar*));.  int  (*
24e70 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
24e71 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69  16)(sqlite3*,voi
24e72 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  d*,void(*)(void*
24e73 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
24e74 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
24e75 64 2a 29 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  d*));.  const vo
24e76 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62 6c  id * (*column_bl
24e77 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ob)(sqlite3_stmt
24e78 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
24e79 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74  nt  (*column_byt
24e7a 65 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  es)(sqlite3_stmt
24e7b 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
24e7c 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74  nt  (*column_byt
24e7d 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  es16)(sqlite3_st
24e7e 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
24e7f 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 63   int  (*column_c
24e80 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74  ount)(sqlite3_st
24e81 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 63 6f 6e  mt*pStmt);.  con
24e82 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75  st char * (*colu
24e83 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
24e84 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
24e85 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  int);.  const vo
24e86 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61  id * (*column_da
24e87 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 29 28 73  tabase_name16)(s
24e88 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24e89 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
24e8a 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  * (*column_declt
24e8b 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ype)(sqlite3_stm
24e8c 74 2a 2c 69 6e 74 20 69 29 3b 0a 20 20 63 6f 6e  t*,int i);.  con
24e8d 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
24e8e 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 29 28 73  mn_decltype16)(s
24e8f 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24e90 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 63  );.  double  (*c
24e91 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 29 28 73 71  olumn_double)(sq
24e92 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
24e93 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a  iCol);.  int  (*
24e94 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28 73 71 6c 69  column_int)(sqli
24e95 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
24e96 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  ol);.  sqlite_in
24e97 74 36 34 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e  t64  (*column_in
24e98 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  t64)(sqlite3_stm
24e99 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
24e9a 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63  const char * (*c
24e9b 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28 73 71 6c 69  olumn_name)(sqli
24e9c 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
24e9d 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
24e9e 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 29 28  *column_name16)(
24e9f 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
24ea0 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
24ea1 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67   * (*column_orig
24ea2 69 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  in_name)(sqlite3
24ea3 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
24ea4 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f  onst void * (*co
24ea5 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
24ea6 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
24ea7 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
24ea8 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  char * (*column_
24ea9 74 61 62 6c 65 5f 6e 61 6d 65 29 28 73 71 6c 69  table_name)(sqli
24eaa 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
24eab 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
24eac 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  *column_table_na
24ead 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  me16)(sqlite3_st
24eae 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73  mt*,int);.  cons
24eaf 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
24eb0 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 29  * (*column_text)
24eb1 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
24eb2 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73  nt iCol);.  cons
24eb3 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d  t void * (*colum
24eb4 6e 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65  n_text16)(sqlite
24eb5 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
24eb6 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75  );.  int  (*colu
24eb7 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33  mn_type)(sqlite3
24eb8 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
24eb9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
24eba 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76 61 6c 75  e* (*column_valu
24ebb 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
24ebc 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 76 6f  ,int iCol);.  vo
24ebd 69 64 20 2a 20 28 2a 63 6f 6d 6d 69 74 5f 68 6f  id * (*commit_ho
24ebe 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ok)(sqlite3*,int
24ebf 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a  (*)(void*),void*
24ec0 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70  );.  int  (*comp
24ec1 6c 65 74 65 29 28 63 6f 6e 73 74 20 63 68 61 72  lete)(const char
24ec2 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a  *sql);.  int  (*
24ec3 63 6f 6d 70 6c 65 74 65 31 36 29 28 63 6f 6e 73  complete16)(cons
24ec4 74 20 76 6f 69 64 2a 73 71 6c 29 3b 0a 20 20 69  t void*sql);.  i
24ec5 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c  nt  (*create_col
24ec6 6c 61 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a  lation)(sqlite3*
24ec7 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
24ec8 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f  ,void*,int(*)(vo
24ec9 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
24eca 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
24ecb 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  id*));.  int  (*
24ecc 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
24ecd 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  16)(sqlite3*,con
24ece 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69  st void*,int,voi
24ecf 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  d*,int(*)(void*,
24ed0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
24ed1 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
24ed2 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61  );.  int  (*crea
24ed3 74 65 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c  te_function)(sql
24ed4 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
24ed5 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c  *,int,int,void*,
24ed6 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
24ed7 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
24ed8 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
24ed9 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53 74 65 70  **),void (*xStep
24eda 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
24edb 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
24edc 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78  alue**),void (*x
24edd 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
24ede 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74  ontext*));.  int
24edf 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63 74    (*create_funct
24ee0 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c  ion16)(sqlite3*,
24ee1 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
24ee2 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28  int,void*,void (
24ee3 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
24ee4 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
24ee5 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f  ite3_value**),vo
24ee6 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69  id (*xStep)(sqli
24ee7 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
24ee8 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
24ee9 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29  ),void (*xFinal)
24eea 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
24eeb 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65  *));.  int (*cre
24eec 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73 71 6c 69  ate_module)(sqli
24eed 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
24eee 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d  ,const sqlite3_m
24eef 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29 3b 0a 20  odule*,void*);. 
24ef0 20 69 6e 74 20 20 28 2a 64 61 74 61 5f 63 6f 75   int  (*data_cou
24ef1 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
24ef2 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74  *pStmt);.  sqlit
24ef3 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e 64 6c 65  e3 * (*db_handle
24ef4 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
24ef5 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63 6c 61 72  ;.  int (*declar
24ef6 65 5f 76 74 61 62 29 28 73 71 6c 69 74 65 33 2a  e_vtab)(sqlite3*
24ef7 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
24ef8 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c 65 5f 73   int  (*enable_s
24ef9 68 61 72 65 64 5f 63 61 63 68 65 29 28 69 6e 74  hared_cache)(int
24efa 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 72 72 63  );.  int  (*errc
24efb 6f 64 65 29 28 73 71 6c 69 74 65 33 2a 64 62 29  ode)(sqlite3*db)
24efc 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
24efd 20 28 2a 65 72 72 6d 73 67 29 28 73 71 6c 69 74   (*errmsg)(sqlit
24efe 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  e3*);.  const vo
24eff 69 64 20 2a 20 28 2a 65 72 72 6d 73 67 31 36 29  id * (*errmsg16)
24f00 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e  (sqlite3*);.  in
24f01 74 20 20 28 2a 65 78 65 63 29 28 73 71 6c 69 74  t  (*exec)(sqlit
24f02 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
24f03 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b  sqlite3_callback
24f04 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a 29 3b 0a  ,void*,char**);.
24f05 20 20 69 6e 74 20 20 28 2a 65 78 70 69 72 65 64    int  (*expired
24f06 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
24f07 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69 6e 61 6c  ;.  int  (*final
24f08 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ize)(sqlite3_stm
24f09 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64  t*pStmt);.  void
24f0a 20 20 28 2a 66 72 65 65 29 28 76 6f 69 64 2a 29    (*free)(void*)
24f0b 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65 65  ;.  void  (*free
24f0c 5f 74 61 62 6c 65 29 28 63 68 61 72 2a 2a 72 65  _table)(char**re
24f0d 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  sult);.  int  (*
24f0e 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 29 28  get_autocommit)(
24f0f 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69  sqlite3*);.  voi
24f10 64 20 2a 20 28 2a 67 65 74 5f 61 75 78 64 61 74  d * (*get_auxdat
24f11 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  a)(sqlite3_conte
24f12 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  xt*,int);.  int 
24f13 20 28 2a 67 65 74 5f 74 61 62 6c 65 29 28 73 71   (*get_table)(sq
24f14 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
24f15 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e 74 2a 2c  r*,char***,int*,
24f16 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20  int*,char**);.  
24f17 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c 5f 72 65  int  (*global_re
24f18 63 6f 76 65 72 29 28 76 6f 69 64 29 3b 0a 20 20  cover)(void);.  
24f19 76 6f 69 64 20 20 28 2a 69 6e 74 65 72 72 75 70  void  (*interrup
24f1a 74 78 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  tx)(sqlite3*);. 
24f1b 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28   sqlite_int64  (
24f1c 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  *last_insert_row
24f1d 69 64 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  id)(sqlite3*);. 
24f1e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
24f1f 6c 69 62 76 65 72 73 69 6f 6e 29 28 76 6f 69 64  libversion)(void
24f20 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c 69 62 76  );.  int  (*libv
24f21 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 29 28 76  ersion_number)(v
24f22 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a  oid);.  void *(*
24f23 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20  malloc)(int);.  
24f24 63 68 61 72 20 2a 20 28 2a 6d 70 72 69 6e 74 66  char * (*mprintf
24f25 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  )(const char*,..
24f26 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65  .);.  int  (*ope
24f27 6e 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  n)(const char*,s
24f28 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e 74  qlite3**);.  int
24f29 20 20 28 2a 6f 70 65 6e 31 36 29 28 63 6f 6e 73    (*open16)(cons
24f2a 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  t void*,sqlite3*
24f2b 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72 65  *);.  int  (*pre
24f2c 70 61 72 65 29 28 73 71 6c 69 74 65 33 2a 2c 63  pare)(sqlite3*,c
24f2d 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73  onst char*,int,s
24f2e 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f  qlite3_stmt**,co
24f2f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69  nst char**);.  i
24f30 6e 74 20 20 28 2a 70 72 65 70 61 72 65 31 36 29  nt  (*prepare16)
24f31 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
24f32 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  void*,int,sqlite
24f33 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76  3_stmt**,const v
24f34 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  oid**);.  void *
24f35 20 28 2a 70 72 6f 66 69 6c 65 29 28 73 71 6c 69   (*profile)(sqli
24f36 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  te3*,void(*)(voi
24f37 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  d*,const char*,s
24f38 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c 76 6f  qlite_uint64),vo
24f39 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  id*);.  void  (*
24f3a 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
24f3b 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 69  )(sqlite3*,int,i
24f3c 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69  nt(*)(void*),voi
24f3d 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 72  d*);.  void *(*r
24f3e 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e  ealloc)(void*,in
24f3f 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 72 65 73  t);.  int  (*res
24f40 65 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  et)(sqlite3_stmt
24f41 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64 20  *pStmt);.  void 
24f42 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f 62 29 28   (*result_blob)(
24f43 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
24f44 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
24f45 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
24f46 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
24f47 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  lt_double)(sqlit
24f48 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64 6f 75 62  e3_context*,doub
24f49 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  le);.  void  (*r
24f4a 65 73 75 6c 74 5f 65 72 72 6f 72 29 28 73 71 6c  esult_error)(sql
24f4b 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f  ite3_context*,co
24f4c 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a  nst char*,int);.
24f4d 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
24f4e 5f 65 72 72 6f 72 31 36 29 28 73 71 6c 69 74 65  _error16)(sqlite
24f4f 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74  3_context*,const
24f50 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 76   void*,int);.  v
24f51 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e  oid  (*result_in
24f52 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
24f53 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64  xt*,int);.  void
24f54 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74 36 34    (*result_int64
24f55 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
24f56 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  t*,sqlite_int64)
24f57 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
24f58 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33  lt_null)(sqlite3
24f59 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f  _context*);.  vo
24f5a 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78  id  (*result_tex
24f5b 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
24f5c 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  xt*,const char*,
24f5d 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
24f5e 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  *));.  void  (*r
24f5f 65 73 75 6c 74 5f 74 65 78 74 31 36 29 28 73 71  esult_text16)(sq
24f60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
24f61 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
24f62 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
24f63 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
24f64 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c 69 74  _text16be)(sqlit
24f65 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
24f66 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64  t void*,int,void
24f67 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
24f68 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
24f69 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f  xt16le)(sqlite3_
24f6a 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76  context*,const v
24f6b 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
24f6c 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64  (void*));.  void
24f6d 20 20 28 2a 72 65 73 75 6c 74 5f 76 61 6c 75 65    (*result_value
24f6e 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
24f6f 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t*,sqlite3_value
24f70 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 72  *);.  void * (*r
24f71 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28 73 71  ollback_hook)(sq
24f72 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76  lite3*,void(*)(v
24f73 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  oid*),void*);.  
24f74 69 6e 74 20 20 28 2a 73 65 74 5f 61 75 74 68 6f  int  (*set_autho
24f75 72 69 7a 65 72 29 28 73 71 6c 69 74 65 33 2a 2c  rizer)(sqlite3*,
24f76 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
24f77 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
24f78 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
24f79 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
24f7a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  ),void*);.  void
24f7b 20 20 28 2a 73 65 74 5f 61 75 78 64 61 74 61 29    (*set_auxdata)
24f7c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
24f7d 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64  *,int,void*,void
24f7e 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20   (*)(void*));.  
24f7f 63 68 61 72 20 2a 20 28 2a 73 6e 70 72 69 6e 74  char * (*snprint
24f80 66 29 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e  f)(int,char*,con
24f81 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20  st char*,...);. 
24f82 20 69 6e 74 20 20 28 2a 73 74 65 70 29 28 73 71   int  (*step)(sq
24f83 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
24f84 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f 63 6f 6c  int  (*table_col
24f85 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29 28 73 71  umn_metadata)(sq
24f86 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
24f87 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
24f88 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72 20  onst char*,char 
24f89 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20 63 6f 6e  const**,char con
24f8a 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  st**,int*,int*,i
24f8b 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  nt*);.  void  (*
24f8c 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 29 28  thread_cleanup)(
24f8d 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a  void);.  int  (*
24f8e 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 29 28 73  total_changes)(s
24f8f 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69 64  qlite3*);.  void
24f90 20 2a 20 28 2a 74 72 61 63 65 29 28 73 71 6c 69   * (*trace)(sqli
24f91 74 65 33 2a 2c 76 6f 69 64 28 2a 78 54 72 61 63  te3*,void(*xTrac
24f92 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
24f93 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  har*),void*);.  
24f94 69 6e 74 20 20 28 2a 74 72 61 6e 73 66 65 72 5f  int  (*transfer_
24f95 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74 65  bindings)(sqlite
24f96 33 5f 73 74 6d 74 2a 2c 73 71 6c 69 74 65 33 5f  3_stmt*,sqlite3_
24f97 73 74 6d 74 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  stmt*);.  void *
24f98 20 28 2a 75 70 64 61 74 65 5f 68 6f 6f 6b 29 28   (*update_hook)(
24f99 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29  sqlite3*,void(*)
24f9a 28 76 6f 69 64 2a 2c 69 6e 74 20 2c 63 68 61 72  (void*,int ,char
24f9b 20 63 6f 6e 73 74 2a 2c 63 68 61 72 20 63 6f 6e   const*,char con
24f9c 73 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34  st*,sqlite_int64
24f9d 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  ),void*);.  void
24f9e 20 2a 20 28 2a 75 73 65 72 5f 64 61 74 61 29 28   * (*user_data)(
24f9f 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
24fa0 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
24fa1 2a 20 28 2a 76 61 6c 75 65 5f 62 6c 6f 62 29 28  * (*value_blob)(
24fa2 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
24fa3 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
24fa4 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f 76  bytes)(sqlite3_v
24fa5 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28  alue*);.  int  (
24fa6 2a 76 61 6c 75 65 5f 62 79 74 65 73 31 36 29 28  *value_bytes16)(
24fa7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
24fa8 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 76 61 6c  .  double  (*val
24fa9 75 65 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  ue_double)(sqlit
24faa 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e  e3_value*);.  in
24fab 74 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 29 28  t  (*value_int)(
24fac 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
24fad 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
24fae 20 28 2a 76 61 6c 75 65 5f 69 6e 74 36 34 29 28   (*value_int64)(
24faf 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
24fb0 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
24fb1 6e 75 6d 65 72 69 63 5f 74 79 70 65 29 28 73 71  numeric_type)(sq
24fb2 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
24fb3 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
24fb4 63 68 61 72 20 2a 20 28 2a 76 61 6c 75 65 5f 74  char * (*value_t
24fb5 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  ext)(sqlite3_val
24fb6 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  ue*);.  const vo
24fb7 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  id * (*value_tex
24fb8 74 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  t16)(sqlite3_val
24fb9 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  ue*);.  const vo
24fba 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  id * (*value_tex
24fbb 74 31 36 62 65 29 28 73 71 6c 69 74 65 33 5f 76  t16be)(sqlite3_v
24fbc 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  alue*);.  const 
24fbd 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74  void * (*value_t
24fbe 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33  ext16le)(sqlite3
24fbf 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
24fc0 20 28 2a 76 61 6c 75 65 5f 74 79 70 65 29 28 73   (*value_type)(s
24fc1 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
24fc2 20 20 63 68 61 72 20 2a 28 2a 76 6d 70 72 69 6e    char *(*vmprin
24fc3 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  tf)(const char*,
24fc4 76 61 5f 6c 69 73 74 29 3b 0a 20 20 2f 2a 20 41  va_list);.  /* A
24fc5 64 64 65 64 20 3f 3f 3f 20 2a 2f 0a 20 20 69 6e  dded ??? */.  in
24fc6 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  t (*overload_fun
24fc7 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c  ction)(sqlite3*,
24fc8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
24fc9 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72 67  ncName, int nArg
24fca 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79  );.  /* Added by
24fcb 20 33 2e 33 2e 31 33 20 2a 2f 0a 20 20 69 6e 74   3.3.13 */.  int
24fcc 20 28 2a 70 72 65 70 61 72 65 5f 76 32 29 28 73   (*prepare_v2)(s
24fcd 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
24fce 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  ar*,int,sqlite3_
24fcf 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61  stmt**,const cha
24fd0 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 70 72  r**);.  int (*pr
24fd1 65 70 61 72 65 31 36 5f 76 32 29 28 73 71 6c 69  epare16_v2)(sqli
24fd2 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  te3*,const void*
24fd3 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d  ,int,sqlite3_stm
24fd4 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a  t**,const void**
24fd5 29 3b 0a 20 20 69 6e 74 20 28 2a 63 6c 65 61 72  );.  int (*clear
24fd6 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74  _bindings)(sqlit
24fd7 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 2f 2a 20  e3_stmt*);.  /* 
24fd8 41 64 64 65 64 20 62 79 20 33 2e 34 2e 31 20 2a  Added by 3.4.1 *
24fd9 2f 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65  /.  int (*create
24fda 5f 6d 6f 64 75 6c 65 5f 76 32 29 28 73 71 6c 69  _module_v2)(sqli
24fdb 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
24fdc 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d  ,const sqlite3_m
24fdd 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 2c 76 6f 69  odule*,void*,voi
24fde 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  d (*xDestroy)(vo
24fdf 69 64 20 2a 29 29 3b 0a 20 20 2f 2a 20 41 64 64  id *));.  /* Add
24fe0 65 64 20 62 79 20 33 2e 35 2e 30 20 2a 2f 0a 20  ed by 3.5.0 */. 
24fe1 20 69 6e 74 20 28 2a 62 69 6e 64 5f 7a 65 72 6f   int (*bind_zero
24fe2 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74  blob)(sqlite3_st
24fe3 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  mt*,int,int);.  
24fe4 69 6e 74 20 28 2a 62 6c 6f 62 5f 62 79 74 65 73  int (*blob_bytes
24fe5 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29  )(sqlite3_blob*)
24fe6 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 63  ;.  int (*blob_c
24fe7 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 62 6c  lose)(sqlite3_bl
24fe8 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c  ob*);.  int (*bl
24fe9 6f 62 5f 6f 70 65 6e 29 28 73 71 6c 69 74 65 33  ob_open)(sqlite3
24fea 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
24feb 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
24fec 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  char*,sqlite3_in
24fed 74 36 34 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  t64,int,sqlite3_
24fee 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69 6e 74 20 28  blob**);.  int (
24fef 2a 62 6c 6f 62 5f 72 65 61 64 29 28 73 71 6c 69  *blob_read)(sqli
24ff0 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f 69 64 2a 2c  te3_blob*,void*,
24ff1 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  int,int);.  int 
24ff2 28 2a 62 6c 6f 62 5f 77 72 69 74 65 29 28 73 71  (*blob_write)(sq
24ff3 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 63 6f 6e 73  lite3_blob*,cons
24ff4 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29  t void*,int,int)
24ff5 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65  ;.  int (*create
24ff6 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 29 28 73  _collation_v2)(s
24ff7 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
24ff8 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e  ar*,int,void*,in
24ff9 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  t(*)(void*,int,c
24ffa 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
24ffb 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 76 6f 69 64  onst void*),void
24ffc 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69  (*)(void*));.  i
24ffd 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e 74 72 6f  nt (*file_contro
24ffe 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  l)(sqlite3*,cons
24fff 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
25000 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  *);.  sqlite3_in
25001 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 68 69 67  t64 (*memory_hig
25002 68 77 61 74 65 72 29 28 69 6e 74 29 3b 0a 20 20  hwater)(int);.  
25003 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a  sqlite3_int64 (*
25004 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28 76 6f 69  memory_used)(voi
25005 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d);.  sqlite3_mu
25006 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f 61 6c 6c  tex *(*mutex_all
25007 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64  oc)(int);.  void
25008 20 28 2a 6d 75 74 65 78 5f 65 6e 74 65 72 29 28   (*mutex_enter)(
25009 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
2500a 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f  .  void (*mutex_
2500b 66 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  free)(sqlite3_mu
2500c 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  tex*);.  void (*
2500d 6d 75 74 65 78 5f 6c 65 61 76 65 29 28 73 71 6c  mutex_leave)(sql
2500e 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20  ite3_mutex*);.  
2500f 69 6e 74 20 28 2a 6d 75 74 65 78 5f 74 72 79 29  int (*mutex_try)
25010 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
25011 3b 0a 20 20 69 6e 74 20 28 2a 6f 70 65 6e 5f 76  ;.  int (*open_v
25012 32 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  2)(const char*,s
25013 71 6c 69 74 65 33 2a 2a 2c 69 6e 74 2c 63 6f 6e  qlite3**,int,con
25014 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74  st char*);.  int
25015 20 28 2a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72   (*release_memor
25016 79 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  y)(int);.  void 
25017 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  (*result_error_n
25018 6f 6d 65 6d 29 28 73 71 6c 69 74 65 33 5f 63 6f  omem)(sqlite3_co
25019 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64 20  ntext*);.  void 
2501a 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  (*result_error_t
2501b 6f 6f 62 69 67 29 28 73 71 6c 69 74 65 33 5f 63  oobig)(sqlite3_c
2501c 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20  ontext*);.  int 
2501d 28 2a 73 6c 65 65 70 29 28 69 6e 74 29 3b 0a 20  (*sleep)(int);. 
2501e 20 76 6f 69 64 20 28 2a 73 6f 66 74 5f 68 65 61   void (*soft_hea
2501f 70 5f 6c 69 6d 69 74 29 28 69 6e 74 29 3b 0a 20  p_limit)(int);. 
25020 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 28 2a   sqlite3_vfs *(*
25021 76 66 73 5f 66 69 6e 64 29 28 63 6f 6e 73 74 20  vfs_find)(const 
25022 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  char*);.  int (*
25023 76 66 73 5f 72 65 67 69 73 74 65 72 29 28 73 71  vfs_register)(sq
25024 6c 69 74 65 33 5f 76 66 73 2a 2c 69 6e 74 29 3b  lite3_vfs*,int);
25025 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 75 6e 72  .  int (*vfs_unr
25026 65 67 69 73 74 65 72 29 28 73 71 6c 69 74 65 33  egister)(sqlite3
25027 5f 76 66 73 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  _vfs*);.  int (*
25028 78 74 68 72 65 61 64 73 61 66 65 29 28 76 6f 69  xthreadsafe)(voi
25029 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73  d);.  void (*res
2502a 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71  ult_zeroblob)(sq
2502b 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
2502c 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65  nt);.  void (*re
2502d 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 29  sult_error_code)
2502e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2502f 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  *,int);.  int (*
25030 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 29 28 69 6e  test_control)(in
25031 74 2c 20 2e 2e 2e 29 3b 0a 20 20 76 6f 69 64 20  t, ...);.  void 
25032 28 2a 72 61 6e 64 6f 6d 6e 65 73 73 29 28 69 6e  (*randomness)(in
25033 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69  t,void*);.  sqli
25034 74 65 33 20 2a 28 2a 63 6f 6e 74 65 78 74 5f 64  te3 *(*context_d
25035 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74 65  b_handle)(sqlite
25036 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69  3_context*);.  i
25037 6e 74 20 28 2a 65 78 74 65 6e 64 65 64 5f 72 65  nt (*extended_re
25038 73 75 6c 74 5f 63 6f 64 65 73 29 28 73 71 6c 69  sult_codes)(sqli
25039 74 65 33 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  te3*,int);.  int
2503a 20 28 2a 6c 69 6d 69 74 29 28 73 71 6c 69 74 65   (*limit)(sqlite
2503b 33 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 73  3*,int,int);.  s
2503c 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 28 2a 6e  qlite3_stmt *(*n
2503d 65 78 74 5f 73 74 6d 74 29 28 73 71 6c 69 74 65  ext_stmt)(sqlite
2503e 33 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  3*,sqlite3_stmt*
2503f 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
25040 2a 28 2a 73 71 6c 29 28 73 71 6c 69 74 65 33 5f  *(*sql)(sqlite3_
25041 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  stmt*);.  int (*
25042 73 74 61 74 75 73 29 28 69 6e 74 2c 69 6e 74 2a  status)(int,int*
25043 2c 69 6e 74 2a 2c 69 6e 74 29 3b 0a 7d 3b 0a 0a  ,int*,int);.};..
25044 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
25045 69 6e 67 20 6d 61 63 72 6f 73 20 72 65 64 65 66  ing macros redef
25046 69 6e 65 20 74 68 65 20 41 50 49 20 72 6f 75 74  ine the API rout
25047 69 6e 65 73 20 73 6f 20 74 68 61 74 20 74 68 65  ines so that the
25048 79 20 61 72 65 0a 2a 2a 20 72 65 64 69 72 65 63  y are.** redirec
25049 74 65 64 20 74 68 72 6f 75 67 68 74 20 74 68 65  ted throught the
2504a 20 67 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33 5f   global sqlite3_
2504b 61 70 69 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  api structure..*
2504c 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
2504d 20 66 69 6c 65 20 69 73 20 61 6c 73 6f 20 75 73   file is also us
2504e 65 64 20 62 79 20 74 68 65 20 6c 6f 61 64 65 78  ed by the loadex
2504f 74 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 0a  t.c source file.
25050 2a 2a 20 28 70 61 72 74 20 6f 66 20 74 68 65 20  ** (part of the 
25051 6d 61 69 6e 20 53 51 4c 69 74 65 20 6c 69 62 72  main SQLite libr
25052 61 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74  ary - not an ext
25053 65 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61 74 0a  ension) so that.
25054 2a 2a 20 69 74 20 63 61 6e 20 67 65 74 20 61 63  ** it can get ac
25055 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69  cess to the sqli
25056 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
25057 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 65   structure.** de
25058 66 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20 74  finition.  But t
25059 68 65 20 6d 61 69 6e 20 6c 69 62 72 61 72 79 20  he main library 
2505a 64 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20 74 6f  does not want to
2505b 20 72 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65   redefine.** the
2505c 20 41 50 49 2e 20 20 53 6f 20 74 68 65 20 72 65   API.  So the re
2505d 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72 6f  definition macro
2505e 73 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64  s are only valid
2505f 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54   if the.** SQLIT
25060 45 5f 43 4f 52 45 20 6d 61 63 72 6f 73 20 69 73  E_CORE macros is
25061 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23   undefined..*/.#
25062 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
25063 52 45 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  RE.#define sqlit
25064 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
25065 74 65 78 74 20 20 20 20 20 20 73 71 6c 69 74 65  text      sqlite
25066 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65  3_api->aggregate
25067 5f 63 6f 6e 74 65 78 74 0a 23 69 66 6e 64 65 66  _context.#ifndef
25068 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
25069 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20  RECATED.#define 
2506a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
2506b 65 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 73  e_count        s
2506c 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67 72  qlite3_api->aggr
2506d 65 67 61 74 65 5f 63 6f 75 6e 74 0a 23 65 6e 64  egate_count.#end
2506e 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  if.#define sqlit
2506f 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 20 20 20  e3_bind_blob    
25070 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25071 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f 62  3_api->bind_blob
25072 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25073 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 20 20 20 20  _bind_double    
25074 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25075 61 70 69 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c 65  api->bind_double
25076 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25077 5f 62 69 6e 64 5f 69 6e 74 20 20 20 20 20 20 20  _bind_int       
25078 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25079 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23 64  api->bind_int.#d
2507a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
2507b 6e 64 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20  nd_int64        
2507c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2507d 2d 3e 62 69 6e 64 5f 69 6e 74 36 34 0a 23 64 65  ->bind_int64.#de
2507e 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
2507f 64 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  d_null          
25080 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25081 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66 69  >bind_null.#defi
25082 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
25083 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 20  parameter_count 
25084 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
25085 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
25086 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
25087 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
25088 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c 69 74  er_index   sqlit
25089 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
2508a 61 6d 65 74 65 72 5f 69 6e 64 65 78 0a 23 64 65  ameter_index.#de
2508b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
2508c 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
2508d 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2508e 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
2508f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
25090 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 20  ite3_bind_text  
25091 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25092 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65  te3_api->bind_te
25093 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  xt.#define sqlit
25094 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20 20  e3_bind_text16  
25095 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25096 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74  3_api->bind_text
25097 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
25098 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 20 20 20  e3_bind_value   
25099 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2509a 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 76 61 6c 75  3_api->bind_valu
2509b 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2509c 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 20  3_busy_handler  
2509d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2509e 5f 61 70 69 2d 3e 62 75 73 79 5f 68 61 6e 64 6c  _api->busy_handl
2509f 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
250a0 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 20  e3_busy_timeout 
250a1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
250a2 33 5f 61 70 69 2d 3e 62 75 73 79 5f 74 69 6d 65  3_api->busy_time
250a3 6f 75 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  out.#define sqli
250a4 74 65 33 5f 63 68 61 6e 67 65 73 20 20 20 20 20  te3_changes     
250a5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
250a6 65 33 5f 61 70 69 2d 3e 63 68 61 6e 67 65 73 0a  e3_api->changes.
250a7 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
250a8 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20  close           
250a9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
250aa 70 69 2d 3e 63 6c 6f 73 65 0a 23 64 65 66 69 6e  pi->close.#defin
250ab 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  e sqlite3_collat
250ac 69 6f 6e 5f 6e 65 65 64 65 64 20 20 20 20 20 20  ion_needed      
250ad 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
250ae 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 0a 23  llation_needed.#
250af 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
250b0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
250b1 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  6     sqlite3_ap
250b2 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  i->collation_nee
250b3 64 65 64 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ded16.#define sq
250b4 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
250b5 62 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  b            sql
250b6 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
250b7 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71  _blob.#define sq
250b8 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
250b9 65 73 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  es           sql
250ba 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
250bb 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73  _bytes.#define s
250bc 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
250bd 74 65 73 31 36 20 20 20 20 20 20 20 20 20 73 71  tes16         sq
250be 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
250bf 6e 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e  n_bytes16.#defin
250c0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
250c1 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20  _count          
250c2 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
250c3 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65 66 69  lumn_count.#defi
250c4 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
250c5 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20  n_database_name 
250c6 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
250c7 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
250c8 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
250c9 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
250ca 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69 74  ase_name16 sqlit
250cb 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
250cc 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 0a 23  atabase_name16.#
250cd 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
250ce 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20 20  olumn_decltype  
250cf 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
250d0 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  i->column_declty
250d1 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  pe.#define sqlit
250d2 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
250d3 70 65 31 36 20 20 20 20 20 20 73 71 6c 69 74 65  pe16      sqlite
250d4 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65  3_api->column_de
250d5 63 6c 74 79 70 65 31 36 0a 23 64 65 66 69 6e 65  cltype16.#define
250d6 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
250d7 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20  double          
250d8 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
250d9 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69  umn_double.#defi
250da 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
250db 6e 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20  n_int           
250dc 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
250dd 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66 69 6e  olumn_int.#defin
250de 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
250df 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _int64          
250e0 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
250e1 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23 64 65 66 69  lumn_int64.#defi
250e2 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
250e3 6e 5f 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20  n_name          
250e4 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
250e5 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66 69  olumn_name.#defi
250e6 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
250e7 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20 20  n_name16        
250e8 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
250e9 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64 65  olumn_name16.#de
250ea 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
250eb 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20  umn_origin_name 
250ec 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
250ed 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
250ee 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
250ef 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
250f0 6e 5f 6e 61 6d 65 31 36 20 20 20 73 71 6c 69 74  n_name16   sqlit
250f1 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f  e3_api->column_o
250f2 72 69 67 69 6e 5f 6e 61 6d 65 31 36 0a 23 64 65  rigin_name16.#de
250f3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
250f4 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20  umn_table_name  
250f5 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
250f6 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  >column_table_na
250f7 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  me.#define sqlit
250f8 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
250f9 6e 61 6d 65 31 36 20 20 20 20 73 71 6c 69 74 65  name16    sqlite
250fa 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 61  3_api->column_ta
250fb 62 6c 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69  ble_name16.#defi
250fc 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
250fd 6e 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20  n_text          
250fe 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
250ff 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23 64 65 66 69  olumn_text.#defi
25100 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
25101 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  n_text16        
25102 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
25103 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 0a 23 64 65  olumn_text16.#de
25104 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
25105 75 6d 6e 5f 74 79 70 65 20 20 20 20 20 20 20 20  umn_type        
25106 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25107 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23 64 65  >column_type.#de
25108 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
25109 75 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20 20 20  umn_value       
2510a 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2510b 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23 64  >column_value.#d
2510c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
2510d 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20 20  mmit_hook       
2510e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2510f 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23 64  ->commit_hook.#d
25110 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
25111 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20 20 20  mplete          
25112 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25113 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23 64 65 66 69  ->complete.#defi
25114 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
25115 65 74 65 31 36 20 20 20 20 20 20 20 20 20 20 20  ete16           
25116 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
25117 6f 6d 70 6c 65 74 65 31 36 0a 23 64 65 66 69 6e  omplete16.#defin
25118 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
25119 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20 20 20 20  _collation      
2511a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
2511b 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a 23  eate_collation.#
2511c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
2511d 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
2511e 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  6     sqlite3_ap
2511f 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  i->create_collat
25120 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ion16.#define sq
25121 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
25122 63 74 69 6f 6e 20 20 20 20 20 20 20 20 73 71 6c  ction        sql
25123 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65  ite3_api->create
25124 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e  _function.#defin
25125 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
25126 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20 20  _function16     
25127 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
25128 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 0a  eate_function16.
25129 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2512a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 20 20  create_module   
2512b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2512c 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c  pi->create_modul
2512d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2512e 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
2512f 76 32 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  v2       sqlite3
25130 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64  _api->create_mod
25131 75 6c 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  ule_v2.#define s
25132 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
25133 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
25134 6c 69 74 65 33 5f 61 70 69 2d 3e 64 61 74 61 5f  lite3_api->data_
25135 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  count.#define sq
25136 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20  lite3_db_handle 
25137 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
25138 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 68 61 6e  ite3_api->db_han
25139 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dle.#define sqli
2513a 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
2513b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2513c 65 33 5f 61 70 69 2d 3e 64 65 63 6c 61 72 65 5f  e3_api->declare_
2513d 76 74 61 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  vtab.#define sql
2513e 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
2513f 65 64 5f 63 61 63 68 65 20 20 20 20 73 71 6c 69  ed_cache    sqli
25140 74 65 33 5f 61 70 69 2d 3e 65 6e 61 62 6c 65 5f  te3_api->enable_
25141 73 68 61 72 65 64 5f 63 61 63 68 65 0a 23 64 65  shared_cache.#de
25142 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
25143 63 6f 64 65 20 20 20 20 20 20 20 20 20 20 20 20  code            
25144 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25145 3e 65 72 72 63 6f 64 65 0a 23 64 65 66 69 6e 65  >errcode.#define
25146 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20   sqlite3_errmsg 
25147 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25148 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72 72  sqlite3_api->err
25149 6d 73 67 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  msg.#define sqli
2514a 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20  te3_errmsg16    
2514b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2514c 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31 36  e3_api->errmsg16
2514d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2514e 5f 65 78 65 63 20 20 20 20 20 20 20 20 20 20 20  _exec           
2514f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25150 61 70 69 2d 3e 65 78 65 63 0a 23 69 66 6e 64 65  api->exec.#ifnde
25151 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
25152 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
25153 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64   sqlite3_expired
25154 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25155 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 70  sqlite3_api->exp
25156 69 72 65 64 0a 23 65 6e 64 69 66 0a 23 64 65 66  ired.#endif.#def
25157 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  ine sqlite3_fina
25158 6c 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20  lize            
25159 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2515a 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66 69 6e 65  finalize.#define
2515b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 20 20   sqlite3_free   
2515c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2515d 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72 65  sqlite3_api->fre
2515e 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2515f 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20 20 20  3_free_table    
25160 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25161 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61 62 6c 65  _api->free_table
25162 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25163 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 20  _get_autocommit 
25164 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25165 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f 63 6f 6d  api->get_autocom
25166 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
25167 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 20  te3_get_auxdata 
25168 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25169 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 78 64  e3_api->get_auxd
2516a 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
2516b 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20 20  te3_get_table   
2516c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2516d 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74 61 62 6c  e3_api->get_tabl
2516e 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  e.#ifndef SQLITE
2516f 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
25170 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25171 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 20  _global_recover 
25172 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25173 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f 72 65 63 6f  api->global_reco
25174 76 65 72 0a 23 65 6e 64 69 66 0a 23 64 65 66 69  ver.#endif.#defi
25175 6e 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  ne sqlite3_inter
25176 72 75 70 74 20 20 20 20 20 20 20 20 20 20 20 20  rupt            
25177 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 69    sqlite3_api->i
25178 6e 74 65 72 72 75 70 74 78 0a 23 64 65 66 69 6e  nterruptx.#defin
25179 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  e sqlite3_last_i
2517a 6e 73 65 72 74 5f 72 6f 77 69 64 20 20 20 20 20  nsert_rowid     
2517b 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 61   sqlite3_api->la
2517c 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 0a  st_insert_rowid.
2517d 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2517e 6c 69 62 76 65 72 73 69 6f 6e 20 20 20 20 20 20  libversion      
2517f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25180 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 0a 23  pi->libversion.#
25181 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
25182 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
25183 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25184 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  i->libversion_nu
25185 6d 62 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  mber.#define sql
25186 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 20 20 20 20  ite3_malloc     
25187 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25188 74 65 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63 0a  te3_api->malloc.
25189 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2518a 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20 20 20  mprintf         
2518b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2518c 70 69 2d 3e 6d 70 72 69 6e 74 66 0a 23 64 65 66  pi->mprintf.#def
2518d 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ine sqlite3_open
2518e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2518f 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25190 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c  open.#define sql
25191 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20 20 20  ite3_open16     
25192 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25193 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 31 36 0a  te3_api->open16.
25194 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25195 70 72 65 70 61 72 65 20 20 20 20 20 20 20 20 20  prepare         
25196 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25197 70 69 2d 3e 70 72 65 70 61 72 65 0a 23 64 65 66  pi->prepare.#def
25198 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
25199 61 72 65 31 36 20 20 20 20 20 20 20 20 20 20 20  are16           
2519a 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2519b 70 72 65 70 61 72 65 31 36 0a 23 64 65 66 69 6e  prepare16.#defin
2519c 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
2519d 65 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 20  e_v2            
2519e 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
2519f 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65  epare_v2.#define
251a0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
251a1 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  16_v2           
251a2 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
251a3 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e  pare16_v2.#defin
251a4 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c  e sqlite3_profil
251a5 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
251a6 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
251a7 6f 66 69 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  ofile.#define sq
251a8 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
251a9 61 6e 64 6c 65 72 20 20 20 20 20 20 20 73 71 6c  andler       sql
251aa 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 67 72 65  ite3_api->progre
251ab 73 73 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66 69  ss_handler.#defi
251ac 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  ne sqlite3_reall
251ad 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  oc              
251ae 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
251af 65 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73  ealloc.#define s
251b0 71 6c 69 74 65 33 5f 72 65 73 65 74 20 20 20 20  qlite3_reset    
251b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
251b2 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 65 74  lite3_api->reset
251b3 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
251b4 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 20 20 20  _result_blob    
251b5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
251b6 61 70 69 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f 62  api->result_blob
251b7 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
251b8 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 20  _result_double  
251b9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
251ba 61 70 69 2d 3e 72 65 73 75 6c 74 5f 64 6f 75 62  api->result_doub
251bb 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
251bc 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 20  e3_result_error 
251bd 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
251be 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72  3_api->result_er
251bf 72 6f 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ror.#define sqli
251c0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
251c1 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  16         sqlit
251c2 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
251c3 72 72 6f 72 31 36 0a 23 64 65 66 69 6e 65 20 73  rror16.#define s
251c4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
251c5 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
251c6 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
251c7 74 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  t_int.#define sq
251c8 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
251c9 36 34 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  64           sql
251ca 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
251cb 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73  _int64.#define s
251cc 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
251cd 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ll            sq
251ce 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
251cf 74 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73  t_null.#define s
251d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
251d1 78 74 20 20 20 20 20 20 20 20 20 20 20 20 73 71  xt            sq
251d2 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
251d3 74 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73  t_text.#define s
251d4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
251d5 78 74 31 36 20 20 20 20 20 20 20 20 20 20 73 71  xt16          sq
251d6 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
251d7 74 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65  t_text16.#define
251d8 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
251d9 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20 20  text16be        
251da 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
251db 75 6c 74 5f 74 65 78 74 31 36 62 65 0a 23 64 65  ult_text16be.#de
251dc 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
251dd 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20  ult_text16le    
251de 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
251df 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  >result_text16le
251e0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
251e1 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 20 20 20  _result_value   
251e2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
251e3 61 70 69 2d 3e 72 65 73 75 6c 74 5f 76 61 6c 75  api->result_valu
251e4 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
251e5 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 20  3_rollback_hook 
251e6 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
251e7 5f 61 70 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f 68  _api->rollback_h
251e8 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ook.#define sqli
251e9 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
251ea 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  er         sqlit
251eb 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 74 68  e3_api->set_auth
251ec 6f 72 69 7a 65 72 0a 23 64 65 66 69 6e 65 20 73  orizer.#define s
251ed 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
251ee 74 61 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ta            sq
251ef 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61  lite3_api->set_a
251f0 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  uxdata.#define s
251f1 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 20  qlite3_snprintf 
251f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
251f3 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6e 70 72 69  lite3_api->snpri
251f4 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ntf.#define sqli
251f5 74 65 33 5f 73 74 65 70 20 20 20 20 20 20 20 20  te3_step        
251f6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
251f7 65 33 5f 61 70 69 2d 3e 73 74 65 70 0a 23 64 65  e3_api->step.#de
251f8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62  fine sqlite3_tab
251f9 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
251fa 74 61 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  ta  sqlite3_api-
251fb 3e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  >table_column_me
251fc 74 61 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  tadata.#define s
251fd 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
251fe 65 61 6e 75 70 20 20 20 20 20 20 20 20 20 73 71  eanup         sq
251ff 6c 69 74 65 33 5f 61 70 69 2d 3e 74 68 72 65 61  lite3_api->threa
25200 64 5f 63 6c 65 61 6e 75 70 0a 23 64 65 66 69 6e  d_cleanup.#defin
25201 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  e sqlite3_total_
25202 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20 20  changes         
25203 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 6f   sqlite3_api->to
25204 74 61 6c 5f 63 68 61 6e 67 65 73 0a 23 64 65 66  tal_changes.#def
25205 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  ine sqlite3_trac
25206 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
25207 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25208 74 72 61 63 65 0a 23 69 66 6e 64 65 66 20 53 51  trace.#ifndef SQ
25209 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
2520a 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c  ATED.#define sql
2520b 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
2520c 6e 64 69 6e 67 73 20 20 20 20 20 20 73 71 6c 69  ndings      sqli
2520d 74 65 33 5f 61 70 69 2d 3e 74 72 61 6e 73 66 65  te3_api->transfe
2520e 72 5f 62 69 6e 64 69 6e 67 73 0a 23 65 6e 64 69  r_bindings.#endi
2520f 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
25210 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 20 20 20  3_update_hook   
25211 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25212 5f 61 70 69 2d 3e 75 70 64 61 74 65 5f 68 6f 6f  _api->update_hoo
25213 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  k.#define sqlite
25214 33 5f 75 73 65 72 5f 64 61 74 61 20 20 20 20 20  3_user_data     
25215 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25216 5f 61 70 69 2d 3e 75 73 65 72 5f 64 61 74 61 0a  _api->user_data.
25217 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25218 76 61 6c 75 65 5f 62 6c 6f 62 20 20 20 20 20 20  value_blob      
25219 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2521a 70 69 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a 23  pi->value_blob.#
2521b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
2521c 61 6c 75 65 5f 62 79 74 65 73 20 20 20 20 20 20  alue_bytes      
2521d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2521e 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 0a 23  i->value_bytes.#
2521f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
25220 61 6c 75 65 5f 62 79 74 65 73 31 36 20 20 20 20  alue_bytes16    
25221 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25222 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 31 36  i->value_bytes16
25223 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25224 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 20 20  _value_double   
25225 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25226 61 70 69 2d 3e 76 61 6c 75 65 5f 64 6f 75 62 6c  api->value_doubl
25227 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
25228 33 5f 76 61 6c 75 65 5f 69 6e 74 20 20 20 20 20  3_value_int     
25229 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2522a 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 0a  _api->value_int.
2522b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2522c 76 61 6c 75 65 5f 69 6e 74 36 34 20 20 20 20 20  value_int64     
2522d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2522e 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 36 34 0a  pi->value_int64.
2522f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25230 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
25231 70 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  pe     sqlite3_a
25232 70 69 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65 72 69  pi->value_numeri
25233 63 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73  c_type.#define s
25234 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
25235 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
25236 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
25237 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71  _text.#define sq
25238 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
25239 31 36 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  16           sql
2523a 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
2523b 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73  text16.#define s
2523c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2523d 74 31 36 62 65 20 20 20 20 20 20 20 20 20 73 71  t16be         sq
2523e 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
2523f 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e  _text16be.#defin
25240 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
25241 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
25242 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
25243 6c 75 65 5f 74 65 78 74 31 36 6c 65 0a 23 64 65  lue_text16le.#de
25244 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
25245 75 65 5f 74 79 70 65 20 20 20 20 20 20 20 20 20  ue_type         
25246 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25247 3e 76 61 6c 75 65 5f 74 79 70 65 0a 23 64 65 66  >value_type.#def
25248 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 6d 70 72  ine sqlite3_vmpr
25249 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20  intf            
2524a 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2524b 76 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65  vmprintf.#define
2524c 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
2524d 64 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20  d_function      
2524e 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 76 65  sqlite3_api->ove
2524f 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 0a 23  rload_function.#
25250 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
25251 72 65 70 61 72 65 5f 76 32 20 20 20 20 20 20 20  repare_v2       
25252 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25253 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23 64  i->prepare_v2.#d
25254 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
25255 65 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20  epare16_v2      
25256 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25257 2d 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a 23  ->prepare16_v2.#
25258 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25259 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 20 20 20  lear_bindings   
2525a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2525b 69 2d 3e 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67  i->clear_binding
2525c 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  s.#define sqlite
2525d 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20  3_bind_zeroblob 
2525e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2525f 5f 61 70 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f 62  _api->bind_zerob
25260 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  lob.#define sqli
25261 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20  te3_blob_bytes  
25262 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25263 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62 79 74  e3_api->blob_byt
25264 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  es.#define sqlit
25265 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20  e3_blob_close   
25266 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25267 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f 73  3_api->blob_clos
25268 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
25269 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20  3_blob_open     
2526a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2526b 5f 61 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e 0a  _api->blob_open.
2526c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2526d 62 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20 20  blob_read       
2526e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2526f 70 69 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a 23 64  pi->blob_read.#d
25270 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
25271 6f 62 5f 77 72 69 74 65 20 20 20 20 20 20 20 20  ob_write        
25272 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25273 2d 3e 62 6c 6f 62 5f 77 72 69 74 65 0a 23 64 65  ->blob_write.#de
25274 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
25275 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
25276 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25277 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  >create_collatio
25278 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  n_v2.#define sql
25279 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
2527a 6c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  l           sqli
2527b 74 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63 6f  te3_api->file_co
2527c 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73 71  ntrol.#define sq
2527d 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
2527e 68 77 61 74 65 72 20 20 20 20 20 20 20 73 71 6c  hwater       sql
2527f 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79  ite3_api->memory
25280 5f 68 69 67 68 77 61 74 65 72 0a 23 64 65 66 69  _highwater.#defi
25281 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  ne sqlite3_memor
25282 79 5f 75 73 65 64 20 20 20 20 20 20 20 20 20 20  y_used          
25283 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
25284 65 6d 6f 72 79 5f 75 73 65 64 0a 23 64 65 66 69  emory_used.#defi
25285 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
25286 5f 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20  _alloc          
25287 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
25288 75 74 65 78 5f 61 6c 6c 6f 63 0a 23 64 65 66 69  utex_alloc.#defi
25289 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
2528a 5f 65 6e 74 65 72 20 20 20 20 20 20 20 20 20 20  _enter          
2528b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
2528c 75 74 65 78 5f 65 6e 74 65 72 0a 23 64 65 66 69  utex_enter.#defi
2528d 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
2528e 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20 20  _free           
2528f 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
25290 75 74 65 78 5f 66 72 65 65 0a 23 64 65 66 69 6e  utex_free.#defin
25291 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
25292 6c 65 61 76 65 20 20 20 20 20 20 20 20 20 20 20  leave           
25293 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
25294 74 65 78 5f 6c 65 61 76 65 0a 23 64 65 66 69 6e  tex_leave.#defin
25295 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
25296 74 72 79 20 20 20 20 20 20 20 20 20 20 20 20 20  try             
25297 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
25298 74 65 78 5f 74 72 79 0a 23 64 65 66 69 6e 65 20  tex_try.#define 
25299 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 20  sqlite3_open_v2 
2529a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2529b 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e  qlite3_api->open
2529c 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _v2.#define sqli
2529d 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
2529e 72 79 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  ry         sqlit
2529f 65 33 5f 61 70 69 2d 3e 72 65 6c 65 61 73 65 5f  e3_api->release_
252a0 6d 65 6d 6f 72 79 0a 23 64 65 66 69 6e 65 20 73  memory.#define s
252a1 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
252a2 72 6f 72 5f 6e 6f 6d 65 6d 20 20 20 20 20 73 71  ror_nomem     sq
252a3 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
252a4 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23 64  t_error_nomem.#d
252a5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
252a6 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
252a7 67 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  g    sqlite3_api
252a8 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  ->result_error_t
252a9 6f 6f 62 69 67 0a 23 64 65 66 69 6e 65 20 73 71  oobig.#define sq
252aa 6c 69 74 65 33 5f 73 6c 65 65 70 20 20 20 20 20  lite3_sleep     
252ab 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
252ac 69 74 65 33 5f 61 70 69 2d 3e 73 6c 65 65 70 0a  ite3_api->sleep.
252ad 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
252ae 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20  soft_heap_limit 
252af 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
252b0 70 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c 69  pi->soft_heap_li
252b1 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
252b2 74 65 33 5f 76 66 73 5f 66 69 6e 64 20 20 20 20  te3_vfs_find    
252b3 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
252b4 65 33 5f 61 70 69 2d 3e 76 66 73 5f 66 69 6e 64  e3_api->vfs_find
252b5 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
252b6 5f 76 66 73 5f 72 65 67 69 73 74 65 72 20 20 20  _vfs_register   
252b7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
252b8 61 70 69 2d 3e 76 66 73 5f 72 65 67 69 73 74 65  api->vfs_registe
252b9 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
252ba 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
252bb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
252bc 5f 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67 69  _api->vfs_unregi
252bd 73 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  ster.#define sql
252be 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 20  ite3_threadsafe 
252bf 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
252c0 74 65 33 5f 61 70 69 2d 3e 78 74 68 72 65 61 64  te3_api->xthread
252c1 73 61 66 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  safe.#define sql
252c2 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f  ite3_result_zero
252c3 62 6c 6f 62 20 20 20 20 20 20 20 20 73 71 6c 69  blob        sqli
252c4 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
252c5 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65  zeroblob.#define
252c6 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
252c7 65 72 72 6f 72 5f 63 6f 64 65 20 20 20 20 20 20  error_code      
252c8 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
252c9 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 0a 23  ult_error_code.#
252ca 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
252cb 65 73 74 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20  est_control     
252cc 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
252cd 69 2d 3e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 0a  i->test_control.
252ce 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
252cf 72 61 6e 64 6f 6d 6e 65 73 73 20 20 20 20 20 20  randomness      
252d0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
252d1 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a 23  pi->randomness.#
252d2 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
252d3 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
252d4 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
252d5 69 2d 3e 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  i->context_db_ha
252d6 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndle.#define sql
252d7 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
252d8 73 75 6c 74 5f 63 6f 64 65 73 20 20 73 71 6c 69  sult_codes  sqli
252d9 74 65 33 5f 61 70 69 2d 3e 65 78 74 65 6e 64 65  te3_api->extende
252da 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 0a 23  d_result_codes.#
252db 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
252dc 69 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20  imit            
252dd 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
252de 69 2d 3e 6c 69 6d 69 74 0a 23 64 65 66 69 6e 65  i->limit.#define
252df 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74   sqlite3_next_st
252e0 6d 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mt              
252e1 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6e 65 78  sqlite3_api->nex
252e2 74 5f 73 74 6d 74 0a 23 64 65 66 69 6e 65 20 73  t_stmt.#define s
252e3 71 6c 69 74 65 33 5f 73 71 6c 20 20 20 20 20 20  qlite3_sql      
252e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
252e5 6c 69 74 65 33 5f 61 70 69 2d 3e 73 71 6c 0a 23  lite3_api->sql.#
252e6 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
252e7 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20 20  tatus           
252e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
252e9 69 2d 3e 73 74 61 74 75 73 0a 23 65 6e 64 69 66  i->status.#endif
252ea 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20   /* SQLITE_CORE 
252eb 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
252ec 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
252ed 54 31 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  T1     const sql
252ee 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
252ef 73 20 2a 73 71 6c 69 74 65 33 5f 61 70 69 20 3d  s *sqlite3_api =
252f0 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   0;.#define SQLI
252f1 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
252f2 54 32 28 76 29 20 20 73 71 6c 69 74 65 33 5f 61  T2(v)  sqlite3_a
252f3 70 69 20 3d 20 76 3b 0a 0a 23 65 6e 64 69 66 20  pi = v;..#endif 
252f4 2f 2a 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48  /* _SQLITE3EXT_H
252f5 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
252f6 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
252f7 69 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a  ite3ext.h ******
252f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252fa 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
252fb 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
252fc 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
252fd 66 66 20 69 6e 20 6c 6f 61 64 65 78 74 2e 63 20  ff in loadext.c 
252fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252ff 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  ****/..#ifndef S
25300 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
25301 45 58 54 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a 2a 2a  EXTENSION../*.**
25302 20 53 6f 6d 65 20 41 50 49 20 72 6f 75 74 69 6e   Some API routin
25303 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77  es are omitted w
25304 68 65 6e 20 76 61 72 69 6f 75 73 20 66 65 61 74  hen various feat
25305 75 72 65 73 20 61 72 65 0a 2a 2a 20 65 78 63 6c  ures are.** excl
25306 75 64 65 64 20 66 72 6f 6d 20 61 20 62 75 69 6c  uded from a buil
25307 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 53 75  d of SQLite.  Su
25308 62 73 74 69 74 75 74 65 20 61 20 4e 55 4c 4c 20  bstitute a NULL 
25309 70 6f 69 6e 74 65 72 0a 2a 2a 20 66 6f 72 20 61  pointer.** for a
2530a 6e 79 20 6d 69 73 73 69 6e 67 20 41 50 49 73 2e  ny missing APIs.
2530b 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
2530c 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
2530d 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66 69  _METADATA.# defi
2530e 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
2530f 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20  n_database_name 
25310 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
25311 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
25312 62 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20  base_name16 0.# 
25313 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25314 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
25315 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
25316 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25317 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20  table_name16    
25318 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
25319 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
2531a 5f 6e 61 6d 65 20 20 20 20 20 30 0a 23 20 64 65  _name     0.# de
2531b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
2531c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
2531d 36 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73  6   0.# define s
2531e 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c  qlite3_table_col
2531f 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 30 0a  umn_metadata  0.
25320 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
25321 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
25322 52 49 5a 41 54 49 4f 4e 0a 23 20 64 65 66 69 6e  RIZATION.# defin
25323 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  e sqlite3_set_au
25324 74 68 6f 72 69 7a 65 72 20 20 20 20 20 20 20 20  thorizer        
25325 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65   0.#endif..#ifde
25326 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
25327 46 31 36 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  F16.# define sql
25328 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
25329 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
2532a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
2532b 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
2532c 36 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  6     0.# define
2532d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2532e 64 65 63 6c 74 79 70 65 31 36 20 20 20 20 20 20  decltype16      
2532f 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
25330 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
25331 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
25332 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
25333 75 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20  umn_text16      
25334 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
25335 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31  qlite3_complete1
25336 36 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a  6             0.
25337 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25338 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
25339 6e 31 36 20 20 20 20 20 30 0a 23 20 64 65 66 69  n16     0.# defi
2533a 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
2533b 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20  e_function16    
2533c 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
2533d 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20  ite3_errmsg16   
2533e 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
2533f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f  define sqlite3_o
25340 70 65 6e 31 36 20 20 20 20 20 20 20 20 20 20 20  pen16           
25341 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
25342 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
25343 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20  16              
25344 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
25345 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20  e3_prepare16_v2 
25346 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
25347 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
25348 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20  ult_error16     
25349 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
2534a 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2534b 78 74 31 36 20 20 20 20 20 20 20 20 20 20 30 0a  xt16          0.
2534c 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
2534d 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
2534e 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
2534f 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
25350 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20  t_text16le      
25351 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
25352 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
25353 36 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20  6           0.# 
25354 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
25355 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20 20  alue_text16be   
25356 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
25357 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25358 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20 20  ext16le         
25359 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
2535a 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
2535b 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64 65  se_name16 0.# de
2535c 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
2535d 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36  umn_table_name16
2535e 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
2535f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
25360 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a  igin_name16   0.
25361 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
25362 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c  QLITE_OMIT_COMPL
25363 45 54 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ETE.# define sql
25364 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20 30 0a  ite3_complete 0.
25365 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25366 5f 63 6f 6d 70 6c 65 74 65 31 36 20 30 0a 23 65  _complete16 0.#e
25367 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
25368 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53  ITE_OMIT_PROGRES
25369 53 5f 43 41 4c 4c 42 41 43 4b 0a 23 20 64 65 66  S_CALLBACK.# def
2536a 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 67  ine sqlite3_prog
2536b 72 65 73 73 5f 68 61 6e 64 6c 65 72 20 30 0a 23  ress_handler 0.#
2536c 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
2536d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2536e 4c 54 41 42 4c 45 0a 23 20 64 65 66 69 6e 65 20  LTABLE.# define 
2536f 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
25370 6f 64 75 6c 65 20 30 0a 23 20 64 65 66 69 6e 65  odule 0.# define
25371 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
25372 6d 6f 64 75 6c 65 5f 76 32 20 30 0a 23 20 64 65  module_v2 0.# de
25373 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63  fine sqlite3_dec
25374 6c 61 72 65 5f 76 74 61 62 20 30 0a 23 65 6e 64  lare_vtab 0.#end
25375 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
25376 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
25377 43 48 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  CHE.# define sql
25378 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
25379 65 64 5f 63 61 63 68 65 20 30 0a 23 65 6e 64 69  ed_cache 0.#endi
2537a 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
2537b 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 23 20 64 65  _OMIT_TRACE.# de
2537c 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f  fine sqlite3_pro
2537d 66 69 6c 65 20 20 20 20 20 20 20 30 0a 23 20 64  file       0.# d
2537e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72  efine sqlite3_tr
2537f 61 63 65 20 20 20 20 20 20 20 20 20 30 0a 23 65  ace         0.#e
25380 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
25381 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42  ITE_OMIT_GET_TAB
25382 4c 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  LE.# define sqli
25383 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20  te3_free_table  
25384 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
25385 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20  ite3_get_table  
25386 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66     0.#endif..#if
25387 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
25388 49 4e 43 52 42 4c 4f 42 0a 23 64 65 66 69 6e 65  INCRBLOB.#define
25389 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65   sqlite3_bind_ze
2538a 72 6f 62 6c 6f 62 20 20 30 0a 23 64 65 66 69 6e  roblob  0.#defin
2538b 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62  e sqlite3_blob_b
2538c 79 74 65 73 20 20 20 20 20 30 0a 23 64 65 66 69  ytes     0.#defi
2538d 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
2538e 63 6c 6f 73 65 20 20 20 20 20 30 0a 23 64 65 66  close     0.#def
2538f 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
25390 5f 6f 70 65 6e 20 20 20 20 20 20 30 0a 23 64 65  _open      0.#de
25391 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
25392 62 5f 72 65 61 64 20 20 20 20 20 20 30 0a 23 64  b_read      0.#d
25393 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
25394 6f 62 5f 77 72 69 74 65 20 20 20 20 20 30 0a 23  ob_write     0.#
25395 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
25396 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
25397 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 70 6f  ture contains po
25398 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 53 51  inters to all SQ
25399 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e 65  Lite API routine
2539a 73 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  s..** A pointer 
2539b 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
2539c 65 20 69 73 20 70 61 73 73 65 64 20 69 6e 74 6f  e is passed into
2539d 20 65 78 74 65 6e 73 69 6f 6e 73 20 77 68 65 6e   extensions when
2539e 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 6c 6f 61   they are.** loa
2539f 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ded so that the 
253a0 65 78 74 65 6e 73 69 6f 6e 20 63 61 6e 20 6d 61  extension can ma
253a1 6b 65 20 63 61 6c 6c 73 20 62 61 63 6b 20 69 6e  ke calls back in
253a2 74 6f 20 74 68 65 20 53 51 4c 69 74 65 0a 2a 2a  to the SQLite.**
253a3 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20   library..**.** 
253a4 57 68 65 6e 20 61 64 64 69 6e 67 20 6e 65 77 20  When adding new 
253a5 41 50 49 73 2c 20 61 64 64 20 74 68 65 6d 20 74  APIs, add them t
253a6 6f 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  o the bottom of 
253a7 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  this structure.*
253a8 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72  * in order to pr
253a9 65 73 65 72 76 65 20 62 61 63 6b 77 61 72 64 73  eserve backwards
253aa 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
253ab 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 73  **.** Extensions
253ac 20 74 68 61 74 20 75 73 65 20 6e 65 77 65 72 20   that use newer 
253ad 41 50 49 73 20 73 68 6f 75 6c 64 20 66 69 72 73  APIs should firs
253ae 74 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71  t call the.** sq
253af 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
253b0 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b  _number() to mak
253b1 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
253b2 41 50 49 20 74 68 65 79 0a 2a 2a 20 69 6e 74 65  API they.** inte
253b3 6e 64 20 74 6f 20 75 73 65 20 69 73 20 73 75 70  nd to use is sup
253b4 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 6c 69  ported by the li
253b5 62 72 61 72 79 2e 20 20 45 78 74 65 6e 73 69 6f  brary.  Extensio
253b6 6e 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 73  ns should.** als
253b7 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  o check to make 
253b8 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70 6f  sure that the po
253b9 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e  inter to the fun
253ba 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20  ction is.** not 
253bb 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61 6c 6c  NULL before call
253bc 69 6e 67 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69  ing it..*/.stati
253bd 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
253be 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c  api_routines sql
253bf 69 74 65 33 41 70 69 73 20 3d 20 7b 0a 20 20 73  ite3Apis = {.  s
253c0 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
253c1 5f 63 6f 6e 74 65 78 74 2c 0a 23 69 66 6e 64 65  _context,.#ifnde
253c2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
253c3 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69 74  PRECATED.  sqlit
253c4 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
253c5 6e 74 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23  nt,.#else.  0,.#
253c6 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
253c7 62 69 6e 64 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c  bind_blob,.  sql
253c8 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65  ite3_bind_double
253c9 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
253ca 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  _int,.  sqlite3_
253cb 62 69 6e 64 5f 69 6e 74 36 34 2c 0a 20 20 73 71  bind_int64,.  sq
253cc 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 2c  lite3_bind_null,
253cd 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
253ce 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 2c  parameter_count,
253cf 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
253d0 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 2c  parameter_index,
253d1 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
253d2 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 2c 0a  parameter_name,.
253d3 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74    sqlite3_bind_t
253d4 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ext,.  sqlite3_b
253d5 69 6e 64 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  ind_text16,.  sq
253d6 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65  lite3_bind_value
253d7 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79  ,.  sqlite3_busy
253d8 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69  _handler,.  sqli
253d9 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
253da 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  ,.  sqlite3_chan
253db 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ges,.  sqlite3_c
253dc 6c 6f 73 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lose,.  sqlite3_
253dd 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
253de 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  ,.  sqlite3_coll
253df 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 2c 0a  ation_needed16,.
253e0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
253e1 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33  _blob,.  sqlite3
253e2 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 2c 0a 20  _column_bytes,. 
253e3 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
253e4 62 79 74 65 73 31 36 2c 0a 20 20 73 71 6c 69 74  bytes16,.  sqlit
253e5 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 2c  e3_column_count,
253e6 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
253e7 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 2c  n_database_name,
253e8 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
253e9 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
253ea 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  6,.  sqlite3_col
253eb 75 6d 6e 5f 64 65 63 6c 74 79 70 65 2c 0a 20 20  umn_decltype,.  
253ec 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
253ed 65 63 6c 74 79 70 65 31 36 2c 0a 20 20 73 71 6c  ecltype16,.  sql
253ee 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62  ite3_column_doub
253ef 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  le,.  sqlite3_co
253f0 6c 75 6d 6e 5f 69 6e 74 2c 0a 20 20 73 71 6c 69  lumn_int,.  sqli
253f1 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
253f2 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
253f3 6d 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  mn_name,.  sqlit
253f4 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
253f5 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
253f6 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 2c 0a  mn_origin_name,.
253f7 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
253f8 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 2c 0a  _origin_name16,.
253f9 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
253fa 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 0a 20 20 73  _table_name,.  s
253fb 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
253fc 62 6c 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71  ble_name16,.  sq
253fd 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
253fe 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t,.  sqlite3_col
253ff 75 6d 6e 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  umn_text16,.  sq
25400 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
25401 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  e,.  sqlite3_col
25402 75 6d 6e 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c  umn_value,.  sql
25403 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
25404 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ,.  sqlite3_comp
25405 6c 65 74 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lete,.  sqlite3_
25406 63 6f 6d 70 6c 65 74 65 31 36 2c 0a 20 20 73 71  complete16,.  sq
25407 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
25408 6c 61 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65  lation,.  sqlite
25409 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
2540a 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  on16,.  sqlite3_
2540b 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 2c  create_function,
2540c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
2540d 65 5f 66 75 6e 63 74 69 6f 6e 31 36 2c 0a 20 20  e_function16,.  
2540e 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
2540f 6f 64 75 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  odule,.  sqlite3
25410 5f 64 61 74 61 5f 63 6f 75 6e 74 2c 0a 20 20 73  _data_count,.  s
25411 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
25412 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  ,.  sqlite3_decl
25413 61 72 65 5f 76 74 61 62 2c 0a 20 20 73 71 6c 69  are_vtab,.  sqli
25414 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65  te3_enable_share
25415 64 5f 63 61 63 68 65 2c 0a 20 20 73 71 6c 69 74  d_cache,.  sqlit
25416 65 33 5f 65 72 72 63 6f 64 65 2c 0a 20 20 73 71  e3_errcode,.  sq
25417 6c 69 74 65 33 5f 65 72 72 6d 73 67 2c 0a 20 20  lite3_errmsg,.  
25418 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
25419 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63  ,.  sqlite3_exec
2541a 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ,.#ifndef SQLITE
2541b 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
2541c 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  .  sqlite3_expir
2541d 65 64 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23  ed,.#else.  0,.#
2541e 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
2541f 66 69 6e 61 6c 69 7a 65 2c 0a 20 20 73 71 6c 69  finalize,.  sqli
25420 74 65 33 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69  te3_free,.  sqli
25421 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 2c 0a  te3_free_table,.
25422 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75    sqlite3_get_au
25423 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20 73 71 6c 69  tocommit,.  sqli
25424 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 2c  te3_get_auxdata,
25425 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  .  sqlite3_get_t
25426 61 62 6c 65 2c 0a 20 20 30 2c 20 20 20 20 20 2f  able,.  0,     /
25427 2a 20 57 61 73 20 73 71 6c 69 74 65 33 5f 67 6c  * Was sqlite3_gl
25428 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 29 2c 20  obal_recover(), 
25429 62 75 74 20 74 68 61 74 20 66 75 6e 63 74 69 6f  but that functio
2542a 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64 20  n is deprecated 
2542b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
2542c 65 72 72 75 70 74 2c 0a 20 20 73 71 6c 69 74 65  errupt,.  sqlite
2542d 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
2542e 77 69 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c  wid,.  sqlite3_l
2542f 69 62 76 65 72 73 69 6f 6e 2c 0a 20 20 73 71 6c  ibversion,.  sql
25430 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
25431 6e 75 6d 62 65 72 2c 0a 20 20 73 71 6c 69 74 65  number,.  sqlite
25432 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69  3_malloc,.  sqli
25433 74 65 33 5f 6d 70 72 69 6e 74 66 2c 0a 20 20 73  te3_mprintf,.  s
25434 71 6c 69 74 65 33 5f 6f 70 65 6e 2c 0a 20 20 73  qlite3_open,.  s
25435 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 2c 0a 20  qlite3_open16,. 
25436 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
25437 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ,.  sqlite3_prep
25438 61 72 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  are16,.  sqlite3
25439 5f 70 72 6f 66 69 6c 65 2c 0a 20 20 73 71 6c 69  _profile,.  sqli
2543a 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
2543b 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  dler,.  sqlite3_
2543c 72 65 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74  realloc,.  sqlit
2543d 65 33 5f 72 65 73 65 74 2c 0a 20 20 73 71 6c 69  e3_reset,.  sqli
2543e 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 2c  te3_result_blob,
2543f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
25440 74 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69  t_double,.  sqli
25441 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
25442 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
25443 6c 74 5f 65 72 72 6f 72 31 36 2c 0a 20 20 73 71  lt_error16,.  sq
25444 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
25445 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
25446 6c 74 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69  lt_int64,.  sqli
25447 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 2c  te3_result_null,
25448 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
25449 74 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65  t_text,.  sqlite
2544a 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 2c  3_result_text16,
2544b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
2544c 74 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73 71  t_text16be,.  sq
2544d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2544e 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  t16le,.  sqlite3
2544f 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 2c 0a 20  _result_value,. 
25450 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63   sqlite3_rollbac
25451 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65  k_hook,.  sqlite
25452 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
25453 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  ,.  sqlite3_set_
25454 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69 74  auxdata,.  sqlit
25455 65 33 5f 73 6e 70 72 69 6e 74 66 2c 0a 20 20 73  e3_snprintf,.  s
25456 71 6c 69 74 65 33 5f 73 74 65 70 2c 0a 20 20 73  qlite3_step,.  s
25457 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c  qlite3_table_col
25458 75 6d 6e 5f 6d 65 74 61 64 61 74 61 2c 0a 23 69  umn_metadata,.#i
25459 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2545a 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 73  T_DEPRECATED.  s
2545b 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
2545c 65 61 6e 75 70 2c 0a 23 65 6c 73 65 0a 20 20 30  eanup,.#else.  0
2545d 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ,.#endif.  sqlit
2545e 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
2545f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61 63  ,.  sqlite3_trac
25460 65 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e,.#ifndef SQLIT
25461 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
25462 44 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61 6e  D.  sqlite3_tran
25463 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 2c 0a 23  sfer_bindings,.#
25464 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66  else.  0,.#endif
25465 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  .  sqlite3_updat
25466 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65  e_hook,.  sqlite
25467 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20 20 73  3_user_data,.  s
25468 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
25469 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  b,.  sqlite3_val
2546a 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69  ue_bytes,.  sqli
2546b 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31  te3_value_bytes1
2546c 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  6,.  sqlite3_val
2546d 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c  ue_double,.  sql
2546e 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 2c 0a  ite3_value_int,.
2546f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
25470 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33  int64,.  sqlite3
25471 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
25472 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  ype,.  sqlite3_v
25473 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73 71 6c  alue_text,.  sql
25474 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
25475 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  6,.  sqlite3_val
25476 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73  ue_text16be,.  s
25477 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
25478 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  t16le,.  sqlite3
25479 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20 20 73  _value_type,.  s
2547a 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 2c  qlite3_vmprintf,
2547b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6f  .  /*.  ** The o
2547c 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65 74 20  riginal API set 
2547d 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c 6c 20  ends here.  All 
2547e 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e 20 63  extensions can c
2547f 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f 66 20  all any.  ** of 
25480 74 68 65 20 41 50 49 73 20 61 62 6f 76 65 20 70  the APIs above p
25481 72 6f 76 69 64 65 64 20 74 68 61 74 20 74 68 65  rovided that the
25482 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20   pointer is not 
25483 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a 2a 20  NULL.  But.  ** 
25484 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 41  before calling A
25485 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 2c  PIs that follow,
25486 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f 75 6c   extension shoul
25487 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20 2a 2a  d check the.  **
25488 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
25489 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20  ion_number() to 
2548a 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61  make sure they a
2548b 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 0a  re dealing with.
2548c 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79 20 74    ** a library t
2548d 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f 75 67  hat is new enoug
2548e 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 61  h to support tha
2548f 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a  t API..  *******
25490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25491 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25492 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25493 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25494 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  **.  */.  sqlite
25495 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
25496 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ion,..  /*.  ** 
25497 41 64 64 65 64 20 61 66 74 65 72 20 33 2e 33 2e  Added after 3.3.
25498 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  13.  */.  sqlite
25499 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a 20 20  3_prepare_v2,.  
2549a 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
2549b 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33 5f  6_v2,.  sqlite3_
2549c 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 2c 0a  clear_bindings,.
2549d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64  .  /*.  ** Added
2549e 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a 2f 0a   for 3.4.1.  */.
2549f 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
254a0 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20 20 2f  _module_v2,..  /
254a1 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72  *.  ** Added for
254a2 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20 73 71   3.5.0.  */.  sq
254a3 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
254a4 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  lob,.  sqlite3_b
254a5 6c 6f 62 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c  lob_bytes,.  sql
254a6 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 2c  ite3_blob_close,
254a7 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  .  sqlite3_blob_
254a8 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  open,.  sqlite3_
254a9 62 6c 6f 62 5f 72 65 61 64 2c 0a 20 20 73 71 6c  blob_read,.  sql
254aa 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 2c  ite3_blob_write,
254ab 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
254ac 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 2c 0a  e_collation_v2,.
254ad 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63    sqlite3_file_c
254ae 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65  ontrol,.  sqlite
254af 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
254b0 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65  er,.  sqlite3_me
254b1 6d 6f 72 79 5f 75 73 65 64 2c 0a 23 69 66 64 65  mory_used,.#ifde
254b2 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  f SQLITE_MUTEX_O
254b3 4d 49 54 0a 20 20 30 2c 20 0a 20 20 30 2c 20 0a  MIT.  0, .  0, .
254b4 20 20 30 2c 0a 20 20 30 2c 0a 20 20 30 2c 0a 23    0,.  0,.  0,.#
254b5 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 5f 6d  else.  sqlite3_m
254b6 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20 73 71  utex_alloc,.  sq
254b7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
254b8 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  r,.  sqlite3_mut
254b9 65 78 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69 74  ex_free,.  sqlit
254ba 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 2c 0a  e3_mutex_leave,.
254bb 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
254bc 74 72 79 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71  try,.#endif.  sq
254bd 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 2c 0a 20  lite3_open_v2,. 
254be 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
254bf 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73 71 6c 69 74  _memory,.  sqlit
254c0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
254c1 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c 69 74 65 33  nomem,.  sqlite3
254c2 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
254c3 6f 62 69 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f  obig,.  sqlite3_
254c4 73 6c 65 65 70 2c 0a 20 20 73 71 6c 69 74 65 33  sleep,.  sqlite3
254c5 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
254c6 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ,.  sqlite3_vfs_
254c7 66 69 6e 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  find,.  sqlite3_
254c8 76 66 73 5f 72 65 67 69 73 74 65 72 2c 0a 20 20  vfs_register,.  
254c9 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65  sqlite3_vfs_unre
254ca 67 69 73 74 65 72 2c 0a 0a 20 20 2f 2a 0a 20 20  gister,..  /*.  
254cb 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e 35  ** Added for 3.5
254cc 2e 38 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  .8.  */.  sqlite
254cd 33 5f 74 68 72 65 61 64 73 61 66 65 2c 0a 20 20  3_threadsafe,.  
254ce 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a  sqlite3_result_z
254cf 65 72 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74  eroblob,.  sqlit
254d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
254d1 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  code,.  sqlite3_
254d2 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 2c 0a 20 20  test_control,.  
254d3 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
254d4 73 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ss,.  sqlite3_co
254d5 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 2c  ntext_db_handle,
254d6 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
254d7 64 20 66 6f 72 20 33 2e 36 2e 30 0a 20 20 2a 2f  d for 3.6.0.  */
254d8 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  .  sqlite3_exten
254d9 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
254da 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  ,.  sqlite3_limi
254db 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6e 65 78  t,.  sqlite3_nex
254dc 74 5f 73 74 6d 74 2c 0a 20 20 73 71 6c 69 74 65  t_stmt,.  sqlite
254dd 33 5f 73 71 6c 2c 0a 20 20 73 71 6c 69 74 65 33  3_sql,.  sqlite3
254de 5f 73 74 61 74 75 73 2c 0a 7d 3b 0a 0a 2f 2a 0a  _status,.};../*.
254df 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f  ** Attempt to lo
254e0 61 64 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74  ad an SQLite ext
254e1 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63  ension library c
254e2 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
254e3 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20 20  file.** zFile.  
254e4 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20  The entry point 
254e5 69 73 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f 63  is zProc.  zProc
254e6 20 6d 61 79 20 62 65 20 30 20 69 6e 20 77 68 69   may be 0 in whi
254e7 63 68 20 63 61 73 65 20 61 0a 2a 2a 20 64 65 66  ch case a.** def
254e8 61 75 6c 74 20 65 6e 74 72 79 20 70 6f 69 6e 74  ault entry point
254e9 20 6e 61 6d 65 20 28 73 71 6c 69 74 65 33 5f 65   name (sqlite3_e
254ea 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 29 20 69  xtension_init) i
254eb 73 20 75 73 65 64 2e 20 20 55 73 65 0a 2a 2a 20  s used.  Use.** 
254ec 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e  of the default n
254ed 61 6d 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ame is recommend
254ee 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ed..**.** Return
254ef 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
254f0 63 63 65 73 73 20 61 6e 64 20 53 51 4c 49 54 45  ccess and SQLITE
254f1 5f 45 52 52 4f 52 20 69 66 20 73 6f 6d 65 74 68  _ERROR if someth
254f2 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a  ing goes wrong..
254f3 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
254f4 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45  r occurs and pzE
254f5 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20  rrMsg is not 0, 
254f6 74 68 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72 72  then fill *pzErr
254f7 4d 73 67 20 77 69 74 68 20 0a 2a 2a 20 65 72 72  Msg with .** err
254f8 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 2e  or message text.
254f9 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
254fa 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72  nction should fr
254fb 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a 2a  ee this memory.*
254fc 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c  * by calling sql
254fd 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29  ite3DbFree(db, )
254fe 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
254ff 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e  sqlite3LoadExten
25500 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  sion(.  sqlite3 
25501 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  *db,          /*
25502 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73   Load the extens
25503 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61  ion into this da
25504 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
25505 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
25506 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20  r *zFile,    /* 
25507 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72  Name of the shar
25508 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61  ed library conta
25509 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20  ining extension 
2550a 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2550b 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e  *zProc,    /* En
2550c 74 72 79 20 70 6f 69 6e 74 2e 20 20 55 73 65 20  try point.  Use 
2550d 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69  "sqlite3_extensi
2550e 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30 20 2a 2f  on_init" if 0 */
2550f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  .  char **pzErrM
25510 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20  sg       /* Put 
25511 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65  error message he
25512 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29  re if not 0 */.)
25513 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
25514 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73  *pVfs = db->pVfs
25515 3b 0a 20 20 76 6f 69 64 20 2a 68 61 6e 64 6c 65  ;.  void *handle
25516 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29  ;.  int (*xInit)
25517 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a  (sqlite3*,char**
25518 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61  ,const sqlite3_a
25519 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 20  pi_routines*);. 
2551a 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67 20 3d   char *zErrmsg =
2551b 20 30 3b 0a 20 20 76 6f 69 64 20 2a 2a 61 48 61   0;.  void **aHa
2551c 6e 64 6c 65 3b 0a 0a 20 20 2f 2a 20 54 69 63 6b  ndle;..  /* Tick
2551d 65 74 20 23 31 38 36 33 2e 20 20 54 6f 20 61 76  et #1863.  To av
2551e 6f 69 64 20 61 20 63 72 65 61 74 69 6e 67 20 73  oid a creating s
2551f 65 63 75 72 69 74 79 20 70 72 6f 62 6c 65 6d 73  ecurity problems
25520 20 66 6f 72 20 6f 6c 64 65 72 0a 20 20 2a 2a 20   for older.  ** 
25521 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  applications tha
25522 74 20 72 65 6c 69 6e 6b 20 61 67 61 69 6e 73 74  t relink against
25523 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20   newer versions 
25524 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65 0a 20  of SQLite, the. 
25525 20 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20 72   ** ability to r
25526 75 6e 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  un load_extensio
25527 6e 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 20  n is turned off 
25528 62 79 20 64 65 66 61 75 6c 74 2e 20 20 4f 6e 65  by default.  One
25529 0a 20 20 2a 2a 20 6d 75 73 74 20 63 61 6c 6c 20  .  ** must call 
2552a 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c  sqlite3_enable_l
2552b 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20  oad_extension() 
2552c 74 6f 20 74 75 72 6e 20 6f 6e 20 65 78 74 65 6e  to turn on exten
2552d 73 69 6f 6e 0a 20 20 2a 2a 20 6c 6f 61 64 69 6e  sion.  ** loadin
2552e 67 2e 20 20 4f 74 68 65 72 77 69 73 65 20 79 6f  g.  Otherwise yo
2552f 75 20 67 65 74 20 74 68 65 20 66 6f 6c 6c 6f 77  u get the follow
25530 69 6e 67 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a  ing error..  */.
25531 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
25532 20 26 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78   & SQLITE_LoadEx
25533 74 65 6e 73 69 6f 6e 29 3d 3d 30 20 29 7b 0a 20  tension)==0 ){. 
25534 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20     if( pzErrMsg 
25535 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  ){.      *pzErrM
25536 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  sg = sqlite3_mpr
25537 69 6e 74 66 28 22 6e 6f 74 20 61 75 74 68 6f 72  intf("not author
25538 69 7a 65 64 22 29 3b 0a 20 20 20 20 7d 0a 20 20  ized");.    }.  
25539 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2553a 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69 66  ERROR;.  }..  if
2553b 28 20 7a 50 72 6f 63 3d 3d 30 20 29 7b 0a 20 20  ( zProc==0 ){.  
2553c 20 20 7a 50 72 6f 63 20 3d 20 22 73 71 6c 69 74    zProc = "sqlit
2553d 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
2553e 74 22 3b 0a 20 20 7d 0a 0a 20 20 68 61 6e 64 6c  t";.  }..  handl
2553f 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 6c 4f  e = sqlite3OsDlO
25540 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 29  pen(pVfs, zFile)
25541 3b 0a 20 20 69 66 28 20 68 61 6e 64 6c 65 3d 3d  ;.  if( handle==
25542 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45  0 ){.    if( pzE
25543 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 63  rrMsg ){.      c
25544 68 61 72 20 7a 45 72 72 5b 32 35 36 5d 3b 0a 20  har zErr[256];. 
25545 20 20 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66       zErr[sizeof
25546 28 7a 45 72 72 29 2d 31 5d 20 3d 20 27 5c 30 27  (zErr)-1] = '\0'
25547 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
25548 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
25549 7a 45 72 72 29 2d 31 2c 20 7a 45 72 72 2c 20 0a  zErr)-1, zErr, .
2554a 20 20 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c            "unabl
2554b 65 20 74 6f 20 6f 70 65 6e 20 73 68 61 72 65 64  e to open shared
2554c 20 6c 69 62 72 61 72 79 20 5b 25 73 5d 22 2c 20   library [%s]", 
2554d 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 73 71  zFile);.      sq
2554e 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 70  lite3OsDlError(p
2554f 56 66 73 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72  Vfs, sizeof(zErr
25550 29 2d 31 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  )-1, zErr);.    
25551 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
25552 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
25553 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20   zErr);.    }.  
25554 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
25555 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 78 49 6e  ERROR;.  }.  xIn
25556 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71 6c  it = (int(*)(sql
25557 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e  ite3*,char**,con
25558 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
25559 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20 20 20 20  outines*)).     
2555a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2555b 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 70 56 66  lite3OsDlSym(pVf
2555c 73 2c 20 68 61 6e 64 6c 65 2c 20 7a 50 72 6f 63  s, handle, zProc
2555d 29 3b 0a 20 20 69 66 28 20 78 49 6e 69 74 3d 3d  );.  if( xInit==
2555e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45  0 ){.    if( pzE
2555f 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 63  rrMsg ){.      c
25560 68 61 72 20 7a 45 72 72 5b 32 35 36 5d 3b 0a 20  har zErr[256];. 
25561 20 20 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66       zErr[sizeof
25562 28 7a 45 72 72 29 2d 31 5d 20 3d 20 27 5c 30 27  (zErr)-1] = '\0'
25563 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
25564 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
25565 7a 45 72 72 29 2d 31 2c 20 7a 45 72 72 2c 0a 20  zErr)-1, zErr,. 
25566 20 20 20 20 20 20 20 20 20 22 6e 6f 20 65 6e 74           "no ent
25567 72 79 20 70 6f 69 6e 74 20 5b 25 73 5d 20 69 6e  ry point [%s] in
25568 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
25569 5b 25 73 5d 22 2c 20 7a 50 72 6f 63 2c 7a 46 69  [%s]", zProc,zFi
2556a 6c 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  le);.      sqlit
2556b 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56 66 73  e3OsDlError(pVfs
2556c 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31  , sizeof(zErr)-1
2556d 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 2a  , zErr);.      *
2556e 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
2556f 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 7a 45  e3DbStrDup(0, zE
25570 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  rr);.      sqlit
25571 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73  e3OsDlClose(pVfs
25572 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 7d  , handle);.    }
25573 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
25574 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73  TE_ERROR;.  }els
25575 65 20 69 66 28 20 78 49 6e 69 74 28 64 62 2c 20  e if( xInit(db, 
25576 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74  &zErrmsg, &sqlit
25577 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20 69  e3Apis) ){.    i
25578 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
25579 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
2557a 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
2557b 28 22 65 72 72 6f 72 20 64 75 72 69 6e 67 20 69  ("error during i
2557c 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 3a 20 25  nitialization: %
2557d 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  s", zErrmsg);.  
2557e 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
2557f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20  free(zErrmsg);. 
25580 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c     sqlite3OsDlCl
25581 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65  ose(pVfs, handle
25582 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
25583 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
25584 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65  .  /* Append the
25585 20 6e 65 77 20 73 68 61 72 65 64 20 6c 69 62 72   new shared libr
25586 61 72 79 20 68 61 6e 64 6c 65 20 74 6f 20 74 68  ary handle to th
25587 65 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  e db->aExtension
25588 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20 61 48 61   array. */.  aHa
25589 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62  ndle = sqlite3Db
2558a 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2558b 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 28 64  izeof(handle)*(d
2558c 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 31 29  b->nExtension+1)
2558d 29 3b 0a 20 20 69 66 28 20 61 48 61 6e 64 6c 65  );.  if( aHandle
2558e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
2558f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
25590 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 45    }.  if( db->nE
25591 78 74 65 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20 20  xtension>0 ){.  
25592 20 20 6d 65 6d 63 70 79 28 61 48 61 6e 64 6c 65    memcpy(aHandle
25593 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  , db->aExtension
25594 2c 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29  , sizeof(handle)
25595 2a 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 29  *db->nExtension)
25596 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
25597 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 45  bFree(db, db->aE
25598 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 64 62 2d  xtension);.  db-
25599 3e 61 45 78 74 65 6e 73 69 6f 6e 20 3d 20 61 48  >aExtension = aH
2559a 61 6e 64 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61 45  andle;..  db->aE
2559b 78 74 65 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78  xtension[db->nEx
2559c 74 65 6e 73 69 6f 6e 2b 2b 5d 20 3d 20 68 61 6e  tension++] = han
2559d 64 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  dle;.  return SQ
2559e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54  LITE_OK;.}.SQLIT
2559f 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
255a0 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
255a1 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
255a2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61            /* Loa
255a3 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  d the extension 
255a4 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  into this databa
255a5 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
255a6 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
255a7 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  File,    /* Name
255a8 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c   of the shared l
255a9 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e  ibrary containin
255aa 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20  g extension */. 
255ab 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72   const char *zPr
255ac 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20  oc,    /* Entry 
255ad 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c  point.  Use "sql
255ae 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
255af 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20 63  nit" if 0 */.  c
255b0 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20  har **pzErrMsg  
255b1 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f       /* Put erro
255b2 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69  r message here i
255b3 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20  f not 0 */.){.  
255b4 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
255b5 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
255b6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d  ->mutex);.  rc =
255b7 20 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65   sqlite3LoadExte
255b8 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c  nsion(db, zFile,
255b9 20 7a 50 72 6f 63 2c 20 70 7a 45 72 72 4d 73 67   zProc, pzErrMsg
255ba 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
255bb 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
255bc 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
255bd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
255be 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
255bf 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  n the database c
255c0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
255c1 73 69 6e 67 20 69 6e 20 6f 72 64 65 72 0a 2a 2a  sing in order.**
255c2 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 6c 6f 61   to clean up loa
255c3 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 0a 2a  ded extensions.*
255c4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
255c5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f   void sqlite3Clo
255c6 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c  seExtensions(sql
255c7 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
255c8 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   i;.  assert( sq
255c9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
255ca 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
255cb 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
255cc 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29  nExtension; i++)
255cd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44  {.    sqlite3OsD
255ce 6c 43 6c 6f 73 65 28 64 62 2d 3e 70 56 66 73 2c  lClose(db->pVfs,
255cf 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b   db->aExtension[
255d0 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  i]);.  }.  sqlit
255d1 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d  e3DbFree(db, db-
255d2 3e 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 7d 0a  >aExtension);.}.
255d3 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72  ./*.** Enable or
255d4 20 64 69 73 61 62 6c 65 20 65 78 74 65 6e 73 69   disable extensi
255d5 6f 6e 20 6c 6f 61 64 69 6e 67 2e 20 20 45 78 74  on loading.  Ext
255d6 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69  ension loading i
255d7 73 20 64 69 73 61 62 6c 65 64 20 62 79 0a 2a 2a  s disabled by.**
255d8 20 64 65 66 61 75 6c 74 20 73 6f 20 61 73 20 6e   default so as n
255d9 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72  ot to open secur
255da 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64  ity holes in old
255db 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e  er applications.
255dc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
255dd 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  nt sqlite3_enabl
255de 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  e_load_extension
255df 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
255e0 74 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69  t onoff){.  sqli
255e1 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
255e2 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66  db->mutex);.  if
255e3 28 20 6f 6e 6f 66 66 20 29 7b 0a 20 20 20 20 64  ( onoff ){.    d
255e4 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
255e5 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
255e6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
255e7 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
255e8 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
255e9 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  n;.  }.  sqlite3
255ea 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
255eb 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
255ec 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
255ed 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
255ee 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
255ef 53 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  SION */../*.** T
255f0 68 65 20 61 75 74 6f 2d 65 78 74 65 6e 73 69 6f  he auto-extensio
255f1 6e 20 63 6f 64 65 20 61 64 64 65 64 20 72 65 67  n code added reg
255f2 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
255f3 65 72 20 6f 72 20 6e 6f 74 20 65 78 74 65 6e 73  er or not extens
255f4 69 6f 6e 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 69  ion.** loading i
255f5 73 20 73 75 70 70 6f 72 74 65 64 2e 20 20 57 65  s supported.  We
255f6 20 6e 65 65 64 20 61 20 64 75 6d 6d 79 20 73 71   need a dummy sq
255f7 6c 69 74 65 33 41 70 69 73 20 70 6f 69 6e 74 65  lite3Apis pointe
255f8 72 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f  r for that.** co
255f9 64 65 20 69 66 20 72 65 67 75 6c 61 72 20 65 78  de if regular ex
255fa 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
255fb 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
255fc 2e 20 20 54 68 69 73 20 69 73 20 74 68 61 74 0a  .  This is that.
255fd 2a 2a 20 64 75 6d 6d 79 20 70 6f 69 6e 74 65 72  ** dummy pointer
255fe 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
255ff 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
25600 45 4e 53 49 4f 4e 0a 73 74 61 74 69 63 20 63 6f  ENSION.static co
25601 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
25602 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33  routines sqlite3
25603 41 70 69 73 20 3d 20 7b 20 30 20 7d 3b 0a 23 65  Apis = { 0 };.#e
25604 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ndif.../*.** The
25605 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63   following objec
25606 74 20 68 6f 6c 64 73 20 74 68 65 20 6c 69 73 74  t holds the list
25607 20 6f 66 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   of automaticall
25608 79 20 6c 6f 61 64 65 64 0a 2a 2a 20 65 78 74 65  y loaded.** exte
25609 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  nsions..**.** Th
2560a 69 73 20 6c 69 73 74 20 69 73 20 73 68 61 72 65  is list is share
2560b 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73  d across threads
2560c 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55  .  The SQLITE_MU
2560d 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
2560e 52 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20  R.** mutex must 
2560f 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 61 63  be held while ac
25610 63 65 73 73 69 6e 67 20 74 68 69 73 20 6c 69 73  cessing this lis
25611 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
25612 72 75 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f  ruct sqlite3Auto
25613 45 78 74 4c 69 73 74 20 73 71 6c 69 74 65 33 41  ExtList sqlite3A
25614 75 74 6f 45 78 74 4c 69 73 74 3b 0a 73 74 61 74  utoExtList;.stat
25615 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  ic SQLITE_WSD st
25616 72 75 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f  ruct sqlite3Auto
25617 45 78 74 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20  ExtList {.  int 
25618 6e 45 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  nExt;           
25619 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2561a 65 6e 74 72 69 65 73 20 69 6e 20 61 45 78 74 5b  entries in aExt[
2561b 5d 20 2a 2f 20 20 20 20 20 20 20 20 20 20 0a 20  ] */          . 
2561c 20 76 6f 69 64 20 28 2a 2a 61 45 78 74 29 28 76   void (**aExt)(v
2561d 6f 69 64 29 3b 20 20 20 2f 2a 20 50 6f 69 6e 74  oid);   /* Point
2561e 65 72 73 20 74 6f 20 74 68 65 20 65 78 74 65 6e  ers to the exten
2561f 73 69 6f 6e 20 69 6e 69 74 20 66 75 6e 63 74 69  sion init functi
25620 6f 6e 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33  ons */.} sqlite3
25621 41 75 74 6f 65 78 74 20 3d 20 7b 20 30 2c 20 30  Autoext = { 0, 0
25622 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64   };../* The "wsd
25623 41 75 74 6f 65 78 74 22 20 6d 61 63 72 6f 20 77  Autoext" macro w
25624 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74  ill resolve to t
25625 68 65 20 61 75 74 6f 65 78 74 65 6e 73 69 6f 6e  he autoextension
25626 0a 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72  .** state vector
25627 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73  .  If writable s
25628 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e  tatic data is un
25629 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65  supported on the
2562a 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68   target,.** we h
2562b 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  ave to locate th
2562c 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61  e state vector a
2562d 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20  t run-time.  In 
2562e 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a  the more common.
2562f 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72  ** case where wr
25630 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
25631 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c  ta is supported,
25632 20 77 73 64 53 74 61 74 20 63 61 6e 20 72 65 66   wsdStat can ref
25633 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74  er directly.** t
25634 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 41 75  o the "sqlite3Au
25635 74 6f 65 78 74 22 20 73 74 61 74 65 20 76 65 63  toext" state vec
25636 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f  tor declared abo
25637 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ve..*/.#ifdef SQ
25638 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20  LITE_OMIT_WSD.# 
25639 64 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78  define wsdAutoex
2563a 74 49 6e 69 74 20 5c 0a 20 20 73 71 6c 69 74 65  tInit \.  sqlite
2563b 33 41 75 74 6f 45 78 74 4c 69 73 74 20 2a 78 20  3AutoExtList *x 
2563c 3d 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65  = &GLOBAL(sqlite
2563d 33 41 75 74 6f 45 78 74 4c 69 73 74 2c 73 71 6c  3AutoExtList,sql
2563e 69 74 65 33 41 75 74 6f 65 78 74 29 0a 23 20 64  ite3Autoext).# d
2563f 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74  efine wsdAutoext
25640 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65   x[0].#else.# de
25641 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 49  fine wsdAutoextI
25642 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64  nit.# define wsd
25643 41 75 74 6f 65 78 74 20 73 71 6c 69 74 65 33 41  Autoext sqlite3A
25644 75 74 6f 65 78 74 0a 23 65 6e 64 69 66 0a 0a 0a  utoext.#endif...
25645 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
25646 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b   statically link
25647 65 64 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61  ed extension tha
25648 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
25649 6c 79 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20  ly.** loaded by 
2564a 65 76 65 72 79 20 6e 65 77 20 64 61 74 61 62 61  every new databa
2564b 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
2564c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2564d 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78   sqlite3_auto_ex
2564e 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78  tension(void (*x
2564f 49 6e 69 74 29 28 76 6f 69 64 29 29 7b 0a 20 20  Init)(void)){.  
25650 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
25651 4f 4b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  OK;.#ifndef SQLI
25652 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
25653 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
25654 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20  initialize();.  
25655 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
25656 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65  turn rc;.  }else
25657 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20  .#endif.  {.    
25658 69 6e 74 20 69 3b 0a 23 69 66 20 53 51 4c 49 54  int i;.#if SQLIT
25659 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20  E_THREADSAFE.   
2565a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
2565b 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
2565c 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
2565d 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
2565e 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20  STER);.#endif.  
2565f 20 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74    wsdAutoextInit
25660 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
25661 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
25662 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
25663 3c 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74  <wsdAutoext.nExt
25664 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
25665 28 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78  ( wsdAutoext.aEx
25666 74 5b 69 5d 3d 3d 78 49 6e 69 74 20 29 20 62 72  t[i]==xInit ) br
25667 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
25668 66 28 20 69 3d 3d 77 73 64 41 75 74 6f 65 78 74  f( i==wsdAutoext
25669 2e 6e 45 78 74 20 29 7b 0a 20 20 20 20 20 20 69  .nExt ){.      i
2566a 6e 74 20 6e 42 79 74 65 20 3d 20 28 77 73 64 41  nt nByte = (wsdA
2566b 75 74 6f 65 78 74 2e 6e 45 78 74 2b 31 29 2a 73  utoext.nExt+1)*s
2566c 69 7a 65 6f 66 28 77 73 64 41 75 74 6f 65 78 74  izeof(wsdAutoext
2566d 2e 61 45 78 74 5b 30 5d 29 3b 0a 20 20 20 20 20  .aExt[0]);.     
2566e 20 76 6f 69 64 20 28 2a 2a 61 4e 65 77 29 28 76   void (**aNew)(v
2566f 6f 69 64 29 3b 0a 20 20 20 20 20 20 61 4e 65 77  oid);.      aNew
25670 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
25671 6f 63 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45  oc(wsdAutoext.aE
25672 78 74 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20  xt, nByte);.    
25673 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b    if( aNew==0 ){
25674 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
25675 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
25676 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
25677 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
25678 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20   = aNew;.       
25679 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
2567a 5b 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74  [wsdAutoext.nExt
2567b 5d 20 3d 20 78 49 6e 69 74 3b 0a 20 20 20 20 20  ] = xInit;.     
2567c 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45     wsdAutoext.nE
2567d 78 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  xt++;.      }.  
2567e 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
2567f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
25680 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  x);.    assert( 
25681 28 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b  (rc&0xff)==rc );
25682 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
25683 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73    }.}../*.** Res
25684 65 74 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  et the automatic
25685 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
25686 6e 67 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f  ng mechanism..*/
25687 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
25688 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61   sqlite3_reset_a
25689 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f  uto_extension(vo
2568a 69 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  id){.#ifndef SQL
2568b 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
2568c 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
2568d 69 6e 69 74 69 61 6c 69 7a 65 28 29 3d 3d 53 51  initialize()==SQ
2568e 4c 49 54 45 5f 4f 4b 20 29 0a 23 65 6e 64 69 66  LITE_OK ).#endif
2568f 0a 20 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  .  {.#if SQLITE_
25690 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73  THREADSAFE.    s
25691 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
25692 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
25693 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
25694 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
25695 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ER);.#endif.    
25696 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a  wsdAutoextInit;.
25697 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
25698 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a  x_enter(mutex);.
25699 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2569a 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74  (wsdAutoext.aExt
2569b 29 3b 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78  );.    wsdAutoex
2569c 74 2e 61 45 78 74 20 3d 20 30 3b 0a 20 20 20 20  t.aExt = 0;.    
2569d 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20  wsdAutoext.nExt 
2569e 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
2569f 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
256a0 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ex);.  }.}../*.*
256a1 2a 20 4c 6f 61 64 20 61 6c 6c 20 61 75 74 6f 6d  * Load all autom
256a2 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e  atic extensions.
256a3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
256a4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75  TE int sqlite3Au
256a5 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73  toLoadExtensions
256a6 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
256a7 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 67 6f   int i;.  int go
256a8 20 3d 20 31 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 1;.  int rc =
256a9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
256aa 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74  t (*xInit)(sqlit
256ab 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74  e3*,char**,const
256ac 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
256ad 74 69 6e 65 73 2a 29 3b 0a 0a 20 20 77 73 64 41  tines*);..  wsdA
256ae 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 69 66  utoextInit;.  if
256af 28 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78  ( wsdAutoext.nEx
256b0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43  t==0 ){.    /* C
256b1 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61 72 6c  ommon case: earl
256b2 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20 65 76  y out without ev
256b3 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 61 63  ery having to ac
256b4 71 75 69 72 65 20 61 20 6d 75 74 65 78 20 2a 2f  quire a mutex */
256b5 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
256b6 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 66 6f 72  TE_OK;.  }.  for
256b7 28 69 3d 30 3b 20 67 6f 3b 20 69 2b 2b 29 7b 0a  (i=0; go; i++){.
256b8 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 6d 73      char *zErrms
256b9 67 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  g = 0;.#if SQLIT
256ba 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20  E_THREADSAFE.   
256bb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
256bc 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
256bd 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
256be 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
256bf 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20  STER);.#endif.  
256c0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
256c1 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
256c2 20 20 69 66 28 20 69 3e 3d 77 73 64 41 75 74 6f    if( i>=wsdAuto
256c3 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20 20 20 20  ext.nExt ){.    
256c4 20 20 78 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20    xInit = 0;.   
256c5 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 7d     go = 0;.    }
256c6 65 6c 73 65 7b 0a 20 20 20 20 20 20 78 49 6e 69  else{.      xIni
256c7 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71 6c 69  t = (int(*)(sqli
256c8 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73  te3*,char**,cons
256c9 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
256ca 75 74 69 6e 65 73 2a 29 29 0a 20 20 20 20 20 20  utines*)).      
256cb 20 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65          wsdAutoe
256cc 78 74 2e 61 45 78 74 5b 69 5d 3b 0a 20 20 20 20  xt.aExt[i];.    
256cd 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  }.    sqlite3_mu
256ce 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
256cf 3b 0a 20 20 20 20 69 66 28 20 78 49 6e 69 74 20  ;.    if( xInit 
256d0 26 26 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45  && xInit(db, &zE
256d1 72 72 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 41  rrmsg, &sqlite3A
256d2 70 69 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71  pis) ){.      sq
256d3 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
256d4 51 4c 49 54 45 5f 45 52 52 4f 52 2c 0a 20 20 20  QLITE_ERROR,.   
256d5 20 20 20 20 20 20 20 20 20 22 61 75 74 6f 6d 61           "automa
256d6 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  tic extension lo
256d7 61 64 69 6e 67 20 66 61 69 6c 65 64 3a 20 25 73  ading failed: %s
256d8 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  ", zErrmsg);.   
256d9 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 20     go = 0;.     
256da 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
256db 4f 52 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  OR;.      sqlite
256dc 33 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b  3_free(zErrmsg);
256dd 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
256de 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  urn rc;.}../****
256df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
256e0 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a  f loadext.c ****
256e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
256e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
256e5 20 66 69 6c 65 20 70 72 61 67 6d 61 2e 63 20 2a   file pragma.c *
256e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
256e9 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a   2003 April 6.**
256ea 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
256eb 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
256ec 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
256ed 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
256ee 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
256ef 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
256f0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
256f1 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
256f2 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
256f3 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
256f4 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
256f5 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
256f6 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
256f7 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
256f8 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
256f9 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
256fa 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
256fb 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
256fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
25700 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
25701 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
25702 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 50 52  implement the PR
25703 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  AGMA command..**
25704 0a 2a 2a 20 24 49 64 3a 20 70 72 61 67 6d 61 2e  .** $Id: pragma.
25705 63 2c 76 20 31 2e 32 30 34 20 32 30 30 39 2f 30  c,v 1.204 2009/0
25706 32 2f 32 33 20 31 36 3a 35 32 3a 30 38 20 64 72  2/23 16:52:08 dr
25707 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 49  h Exp $.*/../* I
25708 67 6e 6f 72 65 20 74 68 69 73 20 77 68 6f 6c 65  gnore this whole
25709 20 66 69 6c 65 20 69 66 20 70 72 61 67 6d 61 73   file if pragmas
2570a 20 61 72 65 20 64 69 73 61 62 6c 65 64 0a 2a 2f   are disabled.*/
2570b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2570c 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47 4d 41  LITE_OMIT_PRAGMA
2570d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
2570e 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52  LITE_OMIT_PARSER
2570f 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72  )../*.** Interpr
25710 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  et the given str
25711 69 6e 67 20 61 73 20 61 20 73 61 66 65 74 79 20  ing as a safety 
25712 6c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 20 30  level.  Return 0
25713 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a 20 31 20 66   for OFF,.** 1 f
25714 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52 4d 41 4c 20  or ON or NORMAL 
25715 61 6e 64 20 32 20 66 6f 72 20 46 55 4c 4c 2e 20  and 2 for FULL. 
25716 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20 61 6e   Return 1 for an
25717 20 65 6d 70 74 79 20 6f 72 20 0a 2a 2a 20 75 6e   empty or .** un
25718 72 65 63 6f 67 6e 69 7a 65 64 20 73 74 72 69 6e  recognized strin
25719 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  g argument..**.*
2571a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
2571b 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20  values returned 
2571c 61 72 65 20 6f 6e 65 20 6c 65 73 73 20 74 68 61  are one less tha
2571d 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 68 61  t the values tha
2571e 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 70  t.** should be p
2571f 61 73 73 65 64 20 69 6e 74 6f 20 73 71 6c 69 74  assed into sqlit
25720 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79  e3BtreeSetSafety
25721 4c 65 76 65 6c 28 29 2e 20 20 54 68 65 20 69 73  Level().  The is
25722 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 73 75 70 70   done.** to supp
25723 6f 72 74 20 6c 65 67 61 63 79 20 53 51 4c 20 63  ort legacy SQL c
25724 6f 64 65 2e 20 20 54 68 65 20 73 61 66 65 74 79  ode.  The safety
25725 20 6c 65 76 65 6c 20 75 73 65 64 20 74 6f 20 62   level used to b
25726 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 61 6e 64  e boolean.** and
25727 20 6f 6c 64 65 72 20 73 63 72 69 70 74 73 20 6d   older scripts m
25728 61 79 20 68 61 76 65 20 75 73 65 64 20 6e 75 6d  ay have used num
25729 62 65 72 73 20 30 20 66 6f 72 20 4f 46 46 20 61  bers 0 for OFF a
2572a 6e 64 20 31 20 66 6f 72 20 4f 4e 2e 0a 2a 2f 0a  nd 1 for ON..*/.
2572b 73 74 61 74 69 63 20 75 38 20 67 65 74 53 61 66  static u8 getSaf
2572c 65 74 79 4c 65 76 65 6c 28 63 6f 6e 73 74 20 63  etyLevel(const c
2572d 68 61 72 20 2a 7a 29 7b 0a 20 20 20 20 20 20 20  har *z){.       
2572e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2572f 20 20 20 20 20 20 2f 2a 20 31 32 33 34 35 36 37        /* 1234567
25730 38 39 20 31 32 33 34 35 36 37 38 39 20 2a 2f 0a  89 123456789 */.
25731 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
25732 68 61 72 20 7a 54 65 78 74 5b 5d 20 3d 20 22 6f  har zText[] = "o
25733 6e 6f 66 66 61 6c 73 65 79 65 73 74 72 75 65 66  noffalseyestruef
25734 75 6c 6c 22 3b 0a 20 20 73 74 61 74 69 63 20 63  ull";.  static c
25735 6f 6e 73 74 20 75 38 20 69 4f 66 66 73 65 74 5b  onst u8 iOffset[
25736 5d 20 3d 20 7b 30 2c 20 31 2c 20 32 2c 20 34 2c  ] = {0, 1, 2, 4,
25737 20 39 2c 20 31 32 2c 20 31 36 7d 3b 0a 20 20 73   9, 12, 16};.  s
25738 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69  tatic const u8 i
25739 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b 32 2c 20 32  Length[] = {2, 2
2573a 2c 20 33 2c 20 35 2c 20 33 2c 20 34 2c 20 34 7d  , 3, 5, 3, 4, 4}
2573b 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
2573c 20 75 38 20 69 56 61 6c 75 65 5b 5d 20 3d 20 20   u8 iValue[] =  
2573d 7b 31 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20  {1, 0, 0, 0, 1, 
2573e 31 2c 20 32 7d 3b 0a 20 20 69 6e 74 20 69 2c 20  1, 2};.  int i, 
2573f 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  n;.  if( sqlite3
25740 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20  Isdigit(*z) ){. 
25741 20 20 20 72 65 74 75 72 6e 20 28 75 38 29 61 74     return (u8)at
25742 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d  oi(z);.  }.  n =
25743 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
25744 28 7a 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  (z);.  for(i=0; 
25745 69 3c 41 72 72 61 79 53 69 7a 65 28 69 4c 65 6e  i<ArraySize(iLen
25746 67 74 68 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  gth); i++){.    
25747 69 66 28 20 69 4c 65 6e 67 74 68 5b 69 5d 3d 3d  if( iLength[i]==
25748 6e 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  n && sqlite3StrN
25749 49 43 6d 70 28 26 7a 54 65 78 74 5b 69 4f 66 66  ICmp(&zText[iOff
2574a 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20  set[i]],z,n)==0 
2574b 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
2574c 69 56 61 6c 75 65 5b 69 5d 3b 0a 20 20 20 20 7d  iValue[i];.    }
2574d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
2574e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70  .}../*.** Interp
2574f 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74  ret the given st
25750 72 69 6e 67 20 61 73 20 61 20 62 6f 6f 6c 65 61  ring as a boolea
25751 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  n value..*/.stat
25752 69 63 20 75 38 20 67 65 74 42 6f 6f 6c 65 61 6e  ic u8 getBoolean
25753 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
25754 0a 20 20 72 65 74 75 72 6e 20 67 65 74 53 61 66  .  return getSaf
25755 65 74 79 4c 65 76 65 6c 28 7a 29 26 31 3b 0a 7d  etyLevel(z)&1;.}
25756 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  ../*.** Interpre
25757 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  t the given stri
25758 6e 67 20 61 73 20 61 20 6c 6f 63 6b 69 6e 67 20  ng as a locking 
25759 6d 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73  mode value..*/.s
2575a 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c 6f 63  tatic int getLoc
2575b 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e 73 74 20 63  kingMode(const c
2575c 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20 7a  har *z){.  if( z
2575d 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 73   ){.    if( 0==s
2575e 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
2575f 20 22 65 78 63 6c 75 73 69 76 65 22 29 20 29 20   "exclusive") ) 
25760 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43  return PAGER_LOC
25761 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
25762 56 45 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 73  VE;.    if( 0==s
25763 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
25764 20 22 6e 6f 72 6d 61 6c 22 29 20 29 20 72 65 74   "normal") ) ret
25765 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  urn PAGER_LOCKIN
25766 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20  GMODE_NORMAL;.  
25767 7d 0a 20 20 72 65 74 75 72 6e 20 50 41 47 45 52  }.  return PAGER
25768 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45  _LOCKINGMODE_QUE
25769 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  RY;.}..#ifndef S
2576a 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
2576b 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  ACUUM./*.** Inte
2576c 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
2576d 73 74 72 69 6e 67 20 61 73 20 61 6e 20 61 75 74  string as an aut
2576e 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 76 61  o-vacuum mode va
2576f 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  lue..**.** The f
25770 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 69 6e 67 73  ollowing strings
25771 2c 20 22 6e 6f 6e 65 22 2c 20 22 66 75 6c 6c 22  , "none", "full"
25772 20 61 6e 64 20 22 69 6e 63 72 65 6d 65 6e 74 61   and "incrementa
25773 6c 22 20 61 72 65 20 0a 2a 2a 20 61 63 63 65 70  l" are .** accep
25774 74 61 62 6c 65 2c 20 61 73 20 61 72 65 20 74 68  table, as are th
25775 65 69 72 20 6e 75 6d 65 72 69 63 20 65 71 75 69  eir numeric equi
25776 76 61 6c 65 6e 74 73 3a 20 30 2c 20 31 20 61 6e  valents: 0, 1 an
25777 64 20 32 20 72 65 73 70 65 63 74 69 76 65 6c 79  d 2 respectively
25778 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
25779 67 65 74 41 75 74 6f 56 61 63 75 75 6d 28 63 6f  getAutoVacuum(co
2577a 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
2577b 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 30 3d 3d  int i;.  if( 0==
2577c 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2577d 2c 20 22 6e 6f 6e 65 22 29 20 29 20 72 65 74 75  , "none") ) retu
2577e 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  rn BTREE_AUTOVAC
2577f 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20 69 66 28 20  UUM_NONE;.  if( 
25780 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
25781 70 28 7a 2c 20 22 66 75 6c 6c 22 29 20 29 20 72  p(z, "full") ) r
25782 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f  eturn BTREE_AUTO
25783 56 41 43 55 55 4d 5f 46 55 4c 4c 3b 0a 20 20 69  VACUUM_FULL;.  i
25784 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  f( 0==sqlite3Str
25785 49 43 6d 70 28 7a 2c 20 22 69 6e 63 72 65 6d 65  ICmp(z, "increme
25786 6e 74 61 6c 22 29 20 29 20 72 65 74 75 72 6e 20  ntal") ) return 
25787 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
25788 5f 49 4e 43 52 3b 0a 20 20 69 20 3d 20 61 74 6f  _INCR;.  i = ato
25789 69 28 7a 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  i(z);.  return (
2578a 75 38 29 28 28 69 3e 3d 30 26 26 69 3c 3d 32 29  u8)((i>=0&&i<=2)
2578b 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  ?i:0);.}.#endif 
2578c 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
2578d 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
2578e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
2578f 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
25790 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 74  RAGMAS./*.** Int
25791 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
25792 20 73 74 72 69 6e 67 20 61 73 20 61 20 74 65 6d   string as a tem
25793 70 20 64 62 20 6c 6f 63 61 74 69 6f 6e 2e 20 52  p db location. R
25794 65 74 75 72 6e 20 31 20 66 6f 72 20 66 69 6c 65  eturn 1 for file
25795 0a 2a 2a 20 62 61 63 6b 65 64 20 74 65 6d 70 6f  .** backed tempo
25796 72 61 72 79 20 64 61 74 61 62 61 73 65 73 2c 20  rary databases, 
25797 32 20 66 6f 72 20 74 68 65 20 52 65 64 2d 42 6c  2 for the Red-Bl
25798 61 63 6b 20 74 72 65 65 20 69 6e 20 6d 65 6d 6f  ack tree in memo
25799 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 61  ry database.** a
2579a 6e 64 20 30 20 74 6f 20 75 73 65 20 74 68 65 20  nd 0 to use the 
2579b 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 64 65 66  compile-time def
2579c 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ault..*/.static 
2579d 69 6e 74 20 67 65 74 54 65 6d 70 53 74 6f 72 65  int getTempStore
2579e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
2579f 0a 20 20 69 66 28 20 7a 5b 30 5d 3e 3d 27 30 27  .  if( z[0]>='0'
257a0 20 26 26 20 7a 5b 30 5d 3c 3d 27 32 27 20 29 7b   && z[0]<='2' ){
257a1 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 5b 30 5d  .    return z[0]
257a2 20 2d 20 27 30 27 3b 0a 20 20 7d 65 6c 73 65 20   - '0';.  }else 
257a3 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
257a4 6d 70 28 7a 2c 20 22 66 69 6c 65 22 29 3d 3d 30  mp(z, "file")==0
257a5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
257a6 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
257a7 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
257a8 22 6d 65 6d 6f 72 79 22 29 3d 3d 30 20 29 7b 0a  "memory")==0 ){.
257a9 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
257aa 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
257ab 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  n 0;.  }.}.#endi
257ac 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45  f /* SQLITE_PAGE
257ad 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  R_PRAGMAS */..#i
257ae 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
257af 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
257b0 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65  /*.** Invalidate
257b1 20 74 65 6d 70 20 73 74 6f 72 61 67 65 2c 20 65   temp storage, e
257b2 69 74 68 65 72 20 77 68 65 6e 20 74 68 65 20 74  ither when the t
257b3 65 6d 70 20 73 74 6f 72 61 67 65 20 69 73 20 63  emp storage is c
257b4 68 61 6e 67 65 64 0a 2a 2a 20 66 72 6f 6d 20 64  hanged.** from d
257b5 65 66 61 75 6c 74 2c 20 6f 72 20 77 68 65 6e 20  efault, or when 
257b6 27 66 69 6c 65 27 20 61 6e 64 20 74 68 65 20 74  'file' and the t
257b7 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
257b8 6f 72 79 20 68 61 73 20 63 68 61 6e 67 65 64 0a  ory has changed.
257b9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e  */.static int in
257ba 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72  validateTempStor
257bb 61 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  age(Parse *pPars
257bc 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e){.  sqlite3 *d
257bd 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
257be 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d    if( db->aDb[1]
257bf 2e 70 42 74 21 3d 30 20 29 7b 0a 20 20 20 20 69  .pBt!=0 ){.    i
257c0 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
257c1 69 74 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72  it || sqlite3Btr
257c2 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28  eeIsInReadTrans(
257c3 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 20  db->aDb[1].pBt) 
257c4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
257c5 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
257c6 20 22 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72   "temporary stor
257c7 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 68  age cannot be ch
257c8 61 6e 67 65 64 20 22 0a 20 20 20 20 20 20 20 20  anged ".        
257c9 22 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74  "from within a t
257ca 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
257cb 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
257cc 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
257cd 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
257ce 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 31 5d 2e  lose(db->aDb[1].
257cf 70 42 74 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44  pBt);.    db->aD
257d0 62 5b 31 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20  b[1].pBt = 0;.  
257d1 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
257d2 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
257d3 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
257d4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
257d5 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
257d6 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
257d7 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
257d8 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
257d9 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  MAS./*.** If the
257da 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 69   TEMP database i
257db 73 20 6f 70 65 6e 2c 20 63 6c 6f 73 65 20 69 74  s open, close it
257dc 20 61 6e 64 20 6d 61 72 6b 20 74 68 65 20 64 61   and mark the da
257dd 74 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a 2a  tabase schema.**
257de 20 61 73 20 6e 65 65 64 69 6e 67 20 72 65 6c 6f   as needing relo
257df 61 64 69 6e 67 2e 20 20 54 68 69 73 20 6d 75 73  ading.  This mus
257e0 74 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 75  t be done when u
257e1 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f  sing the SQLITE_
257e2 54 45 4d 50 5f 53 54 4f 52 45 0a 2a 2a 20 6f 72  TEMP_STORE.** or
257e3 20 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 53 54   DEFAULT_TEMP_ST
257e4 4f 52 45 20 70 72 61 67 6d 61 73 2e 0a 2a 2f 0a  ORE pragmas..*/.
257e5 73 74 61 74 69 63 20 69 6e 74 20 63 68 61 6e 67  static int chang
257e6 65 54 65 6d 70 53 74 6f 72 61 67 65 28 50 61 72  eTempStorage(Par
257e7 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
257e8 74 20 63 68 61 72 20 2a 7a 53 74 6f 72 61 67 65  t char *zStorage
257e9 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 73 20  Type){.  int ts 
257ea 3d 20 67 65 74 54 65 6d 70 53 74 6f 72 65 28 7a  = getTempStore(z
257eb 53 74 6f 72 61 67 65 54 79 70 65 29 3b 0a 20 20  StorageType);.  
257ec 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
257ed 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
257ee 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d  db->temp_store==
257ef 74 73 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  ts ) return SQLI
257f0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 6e 76  TE_OK;.  if( inv
257f1 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61  alidateTempStora
257f2 67 65 28 20 70 50 61 72 73 65 20 29 20 21 3d 20  ge( pParse ) != 
257f3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
257f4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
257f5 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  RROR;.  }.  db->
257f6 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 28 75 38  temp_store = (u8
257f7 29 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  )ts;.  return SQ
257f8 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
257f9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45  f /* SQLITE_PAGE
257fa 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a  R_PRAGMAS */../*
257fb 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
257fc 65 20 74 6f 20 72 65 74 75 72 6e 20 61 20 73 69  e to return a si
257fd 6e 67 6c 65 20 69 6e 74 65 67 65 72 20 76 61 6c  ngle integer val
257fe 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ue..*/.static vo
257ff 69 64 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49  id returnSingleI
25800 6e 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  nt(Parse *pParse
25801 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c  , const char *zL
25802 61 62 65 6c 2c 20 69 6e 74 20 76 61 6c 75 65 29  abel, int value)
25803 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
25804 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
25805 72 73 65 29 3b 0a 20 20 69 6e 74 20 6d 65 6d 20  rse);.  int mem 
25806 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
25807 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
25808 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
25809 67 65 72 2c 20 76 61 6c 75 65 2c 20 6d 65 6d 29  ger, value, mem)
2580a 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2580b 65 78 70 6c 61 69 6e 3d 3d 30 20 29 7b 0a 20 20  explain==0 ){.  
2580c 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2580d 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
2580e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2580f 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
25810 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61  OLNAME_NAME, zLa
25811 62 65 6c 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  bel, SQLITE_STAT
25812 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  IC);.  }.  sqlit
25813 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25814 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 6d 65  OP_ResultRow, me
25815 6d 2c 20 31 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  m, 1);.}..#ifnde
25816 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
25817 41 47 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  AG_PRAGMAS./*.**
25818 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
25819 20 7a 52 69 67 68 74 20 61 6e 64 20 7a 4c 65 66   zRight and zLef
2581a 74 20 72 65 66 65 72 20 74 6f 20 61 20 70 72 61  t refer to a pra
2581b 67 6d 61 20 74 68 61 74 20 71 75 65 72 69 65 73  gma that queries
2581c 0a 2a 2a 20 6f 72 20 63 68 61 6e 67 65 73 20 6f  .** or changes o
2581d 6e 65 20 6f 66 20 74 68 65 20 66 6c 61 67 73 20  ne of the flags 
2581e 69 6e 20 64 62 2d 3e 66 6c 61 67 73 2e 20 20 52  in db->flags.  R
2581f 65 74 75 72 6e 20 31 20 69 66 20 73 6f 20 61 6e  eturn 1 if so an
25820 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2a 20 41  d 0 if not..** A
25821 6c 73 6f 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 74  lso, implement t
25822 68 65 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 73 74  he pragma..*/.st
25823 61 74 69 63 20 69 6e 74 20 66 6c 61 67 50 72 61  atic int flagPra
25824 67 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73  gma(Parse *pPars
25825 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
25826 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  Left, const char
25827 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 73 74 61   *zRight){.  sta
25828 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
25829 20 73 50 72 61 67 6d 61 54 79 70 65 20 7b 0a 20   sPragmaType {. 
2582a 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2582b 4e 61 6d 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f  Name;  /* Name o
2582c 66 20 74 68 65 20 70 72 61 67 6d 61 20 2a 2f 0a  f the pragma */.
2582d 20 20 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20      int mask;   
2582e 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
2582f 66 6f 72 20 74 68 65 20 64 62 2d 3e 66 6c 61 67  for the db->flag
25830 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 61  s value */.  } a
25831 50 72 61 67 6d 61 5b 5d 20 3d 20 7b 0a 20 20 20  Pragma[] = {.   
25832 20 7b 20 22 66 75 6c 6c 5f 63 6f 6c 75 6d 6e 5f   { "full_column_
25833 6e 61 6d 65 73 22 2c 20 20 20 20 20 20 20 20 53  names",        S
25834 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d  QLITE_FullColNam
25835 65 73 20 20 7d 2c 0a 20 20 20 20 7b 20 22 73 68  es  },.    { "sh
25836 6f 72 74 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73  ort_column_names
25837 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ",       SQLITE_
25838 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 20 7d 2c  ShortColNames },
25839 0a 20 20 20 20 7b 20 22 63 6f 75 6e 74 5f 63 68  .    { "count_ch
2583a 61 6e 67 65 73 22 2c 20 20 20 20 20 20 20 20 20  anges",         
2583b 20 20 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52     SQLITE_CountR
2583c 6f 77 73 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b  ows     },.    {
2583d 20 22 65 6d 70 74 79 5f 72 65 73 75 6c 74 5f 63   "empty_result_c
2583e 61 6c 6c 62 61 63 6b 73 22 2c 20 20 20 53 51 4c  allbacks",   SQL
2583f 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b  ITE_NullCallback
25840 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 65 67 61    },.    { "lega
25841 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 22 2c  cy_file_format",
25842 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4c 65         SQLITE_Le
25843 67 61 63 79 46 69 6c 65 46 6d 74 20 7d 2c 0a 20  gacyFileFmt },. 
25844 20 20 20 7b 20 22 66 75 6c 6c 66 73 79 6e 63 22     { "fullfsync"
25845 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
25846 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e   SQLITE_FullFSyn
25847 63 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22  c     },.    { "
25848 72 65 76 65 72 73 65 5f 75 6e 6f 72 64 65 72 65  reverse_unordere
25849 64 5f 73 65 6c 65 63 74 73 22 2c 20 53 51 4c 49  d_selects", SQLI
2584a 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20  TE_ReverseOrder 
2584b 20 7d 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54   },.#ifdef SQLIT
2584c 45 5f 44 45 42 55 47 0a 20 20 20 20 7b 20 22 73  E_DEBUG.    { "s
2584d 71 6c 5f 74 72 61 63 65 22 2c 20 20 20 20 20 20  ql_trace",      
2584e 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2584f 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20 7d  _SqlTrace      }
25850 2c 0a 20 20 20 20 7b 20 22 76 64 62 65 5f 6c 69  ,.    { "vdbe_li
25851 73 74 69 6e 67 22 2c 20 20 20 20 20 20 20 20 20  sting",         
25852 20 20 20 20 53 51 4c 49 54 45 5f 56 64 62 65 4c      SQLITE_VdbeL
25853 69 73 74 69 6e 67 20 20 20 7d 2c 0a 20 20 20 20  isting   },.    
25854 7b 20 22 76 64 62 65 5f 74 72 61 63 65 22 2c 20  { "vdbe_trace", 
25855 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
25856 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 20  LITE_VdbeTrace  
25857 20 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 69 66     },.#endif.#if
25858 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
25859 5f 43 48 45 43 4b 0a 20 20 20 20 7b 20 22 69 67  _CHECK.    { "ig
2585a 6e 6f 72 65 5f 63 68 65 63 6b 5f 63 6f 6e 73 74  nore_check_const
2585b 72 61 69 6e 74 73 22 2c 20 53 51 4c 49 54 45 5f  raints", SQLITE_
2585c 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 7d 2c  IgnoreChecks  },
2585d 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 20 54  .#endif.    /* T
2585e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
2585f 56 45 52 59 20 65 78 70 65 72 69 6d 65 6e 74 61  VERY experimenta
25860 6c 20 2a 2f 0a 20 20 20 20 7b 20 22 77 72 69 74  l */.    { "writ
25861 61 62 6c 65 5f 73 63 68 65 6d 61 22 2c 20 20 20  able_schema",   
25862 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 57 72         SQLITE_Wr
25863 69 74 65 53 63 68 65 6d 61 7c 53 51 4c 49 54 45  iteSchema|SQLITE
25864 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 7d 2c  _RecoveryMode },
25865 0a 20 20 20 20 7b 20 22 6f 6d 69 74 5f 72 65 61  .    { "omit_rea
25866 64 6c 6f 63 6b 22 2c 20 20 20 20 20 20 20 20 20  dlock",         
25867 20 20 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64     SQLITE_NoRead
25868 6c 6f 63 6b 20 20 20 20 7d 2c 0a 0a 20 20 20 20  lock    },..    
25869 2f 2a 20 54 4f 44 4f 3a 20 4d 61 79 62 65 20 69  /* TODO: Maybe i
2586a 74 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70  t shouldn't be p
2586b 6f 73 73 69 62 6c 65 20 74 6f 20 63 68 61 6e 67  ossible to chang
2586c 65 20 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d  e the ReadUncomm
2586d 69 74 74 65 64 0a 20 20 20 20 2a 2a 20 66 6c 61  itted.    ** fla
2586e 67 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  g if there are a
2586f 6e 79 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  ny active statem
25870 65 6e 74 73 2e 20 2a 2f 0a 20 20 20 20 7b 20 22  ents. */.    { "
25871 72 65 61 64 5f 75 6e 63 6f 6d 6d 69 74 74 65 64  read_uncommitted
25872 22 2c 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  ",         SQLIT
25873 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  E_ReadUncommitte
25874 64 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20  d },.  };.  int 
25875 69 3b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63  i;.  const struc
25876 74 20 73 50 72 61 67 6d 61 54 79 70 65 20 2a 70  t sPragmaType *p
25877 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 61  ;.  for(i=0, p=a
25878 50 72 61 67 6d 61 3b 20 69 3c 41 72 72 61 79 53  Pragma; i<ArrayS
25879 69 7a 65 28 61 50 72 61 67 6d 61 29 3b 20 69 2b  ize(aPragma); i+
2587a 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, p++){.    if(
2587b 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2587c 7a 4c 65 66 74 2c 20 70 2d 3e 7a 4e 61 6d 65 29  zLeft, p->zName)
2587d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
2587e 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2587f 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 56 64 62  e->db;.      Vdb
25880 65 20 2a 76 3b 0a 20 20 20 20 20 20 76 20 3d 20  e *v;.      v = 
25881 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
25882 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 61 73  Parse);.      as
25883 73 65 72 74 28 20 76 21 3d 30 20 29 3b 20 20 2f  sert( v!=0 );  /
25884 2a 20 41 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61  * Already alloca
25885 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 72  ted by sqlite3Pr
25886 61 67 6d 61 28 29 20 2a 2f 0a 20 20 20 20 20 20  agma() */.      
25887 69 66 28 20 41 4c 57 41 59 53 28 76 29 20 29 7b  if( ALWAYS(v) ){
25888 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 52 69  .        if( zRi
25889 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ght==0 ){.      
2588a 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
2588b 49 6e 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 7a  Int(pParse, p->z
2588c 4e 61 6d 65 2c 20 28 64 62 2d 3e 66 6c 61 67 73  Name, (db->flags
2588d 20 26 20 70 2d 3e 6d 61 73 6b 29 21 3d 30 20 29   & p->mask)!=0 )
2588e 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
2588f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 67  .          if( g
25890 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74  etBoolean(zRight
25891 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
25892 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 2d   db->flags |= p-
25893 3e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20  >mask;.         
25894 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
25895 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d      db->flags &=
25896 20 7e 70 2d 3e 6d 61 73 6b 3b 0a 20 20 20 20 20   ~p->mask;.     
25897 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
25898 20 20 2f 2a 20 4d 61 6e 79 20 6f 66 20 74 68 65    /* Many of the
25899 20 66 6c 61 67 2d 70 72 61 67 6d 61 73 20 6d 6f   flag-pragmas mo
2589a 64 69 66 79 20 74 68 65 20 63 6f 64 65 20 67 65  dify the code ge
2589b 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 53  nerated by the S
2589c 51 4c 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  QL .          **
2589d 20 63 6f 6d 70 69 6c 65 72 20 28 65 67 2e 20 63   compiler (eg. c
2589e 6f 75 6e 74 5f 63 68 61 6e 67 65 73 29 2e 20 53  ount_changes). S
2589f 6f 20 61 64 64 20 61 6e 20 6f 70 63 6f 64 65 20  o add an opcode 
258a0 74 6f 20 65 78 70 69 72 65 20 61 6c 6c 0a 20 20  to expire all.  
258a1 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69          ** compi
258a2 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  led SQL statemen
258a3 74 73 20 61 66 74 65 72 20 6d 6f 64 69 66 79 69  ts after modifyi
258a4 6e 67 20 61 20 70 72 61 67 6d 61 20 76 61 6c 75  ng a pragma valu
258a5 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
258a6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
258a7 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
258a8 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30 29 3b  P_Expire, 0, 0);
258a9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
258aa 20 7d 0a 0a 20 20 20 20 20 20 72 65 74 75 72 6e   }..      return
258ab 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
258ac 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
258ad 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
258ae 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 20 2a  T_FLAG_PRAGMAS *
258af 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
258b0 61 20 68 75 6d 61 6e 2d 72 65 61 64 61 62 6c 65  a human-readable
258b1 20 6e 61 6d 65 20 66 6f 72 20 61 20 63 6f 6e 73   name for a cons
258b2 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f  traint resolutio
258b3 6e 20 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  n action..*/.sta
258b4 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
258b5 61 63 74 69 6f 6e 4e 61 6d 65 28 75 38 20 61 63  actionName(u8 ac
258b6 74 69 6f 6e 29 7b 0a 20 20 63 6f 6e 73 74 20 63  tion){.  const c
258b7 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 73 77  har *zName;.  sw
258b8 69 74 63 68 28 20 61 63 74 69 6f 6e 20 29 7b 0a  itch( action ){.
258b9 20 20 20 20 63 61 73 65 20 4f 45 5f 53 65 74 4e      case OE_SetN
258ba 75 6c 6c 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 53  ull:  zName = "S
258bb 45 54 20 4e 55 4c 4c 22 3b 20 20 20 20 20 20 20  ET NULL";       
258bc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
258bd 63 61 73 65 20 4f 45 5f 53 65 74 44 66 6c 74 3a  case OE_SetDflt:
258be 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20 44    zName = "SET D
258bf 45 46 41 55 4c 54 22 3b 20 20 20 20 20 20 20 20  EFAULT";        
258c0 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
258c1 20 4f 45 5f 43 61 73 63 61 64 65 3a 20 20 7a 4e   OE_Cascade:  zN
258c2 61 6d 65 20 3d 20 22 43 41 53 43 41 44 45 22 3b  ame = "CASCADE";
258c3 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
258c4 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a  ak;.    default:
258c5 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20            zName 
258c6 3d 20 22 52 45 53 54 52 49 43 54 22 3b 20 20 0a  = "RESTRICT";  .
258c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
258c8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 63        assert( ac
258c9 74 69 6f 6e 3d 3d 4f 45 5f 52 65 73 74 72 69 63  tion==OE_Restric
258ca 74 20 29 3b 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  t ); break;.  }.
258cb 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a    return zName;.
258cc 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73  }../*.** Process
258cd 20 61 20 70 72 61 67 6d 61 20 73 74 61 74 65 6d   a pragma statem
258ce 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 72 61  ent.  .**.** Pra
258cf 67 6d 61 73 20 61 72 65 20 6f 66 20 74 68 69 73  gmas are of this
258d0 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   form:.**.**    
258d1 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
258d2 73 65 2e 5d 69 64 20 5b 3d 20 76 61 6c 75 65 5d  se.]id [= value]
258d3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 65 6e 74  .**.** The ident
258d4 69 66 69 65 72 20 6d 69 67 68 74 20 61 6c 73 6f  ifier might also
258d5 20 62 65 20 61 20 73 74 72 69 6e 67 2e 20 20 54   be a string.  T
258d6 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74  he value is a st
258d7 72 69 6e 67 2c 20 61 6e 64 0a 2a 2a 20 69 64 65  ring, and.** ide
258d8 6e 74 69 66 69 65 72 2c 20 6f 72 20 61 20 6e 75  ntifier, or a nu
258d9 6d 62 65 72 2e 20 20 49 66 20 6d 69 6e 75 73 46  mber.  If minusF
258da 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 65  lag is true, the
258db 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 0a 2a  n the value is.*
258dc 2a 20 61 20 6e 75 6d 62 65 72 20 74 68 61 74 20  * a number that 
258dd 77 61 73 20 70 72 65 63 65 64 65 64 20 62 79 20  was preceded by 
258de 61 20 6d 69 6e 75 73 20 73 69 67 6e 2e 0a 2a 2a  a minus sign..**
258df 0a 2a 2a 20 49 66 20 74 68 65 20 6c 65 66 74 20  .** If the left 
258e0 73 69 64 65 20 69 73 20 22 64 61 74 61 62 61 73  side is "databas
258e1 65 2e 69 64 22 20 74 68 65 6e 20 70 49 64 31 20  e.id" then pId1 
258e2 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
258e3 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 70 49 64 32  name.** and pId2
258e4 20 69 73 20 74 68 65 20 69 64 2e 20 20 49 66 20   is the id.  If 
258e5 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69 73  the left side is
258e6 20 6a 75 73 74 20 22 69 64 22 20 74 68 65 6e 20   just "id" then 
258e7 70 49 64 31 20 69 73 20 74 68 65 0a 2a 2a 20 69  pId1 is the.** i
258e8 64 20 61 6e 64 20 70 49 64 32 20 69 73 20 61 6e  d and pId2 is an
258e9 79 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2e 0a  y empty string..
258ea 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
258eb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
258ec 61 67 6d 61 28 0a 20 20 50 61 72 73 65 20 2a 70  agma(.  Parse *p
258ed 50 61 72 73 65 2c 20 0a 20 20 54 6f 6b 65 6e 20  Parse, .  Token 
258ee 2a 70 49 64 31 2c 20 20 20 20 20 20 20 20 2f 2a  *pId1,        /*
258ef 20 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 5b   First part of [
258f0 64 61 74 61 62 61 73 65 2e 5d 69 64 20 66 69 65  database.]id fie
258f1 6c 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  ld */.  Token *p
258f2 49 64 32 2c 20 20 20 20 20 20 20 20 2f 2a 20 53  Id2,        /* S
258f3 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 5b 64  econd part of [d
258f4 61 74 61 62 61 73 65 2e 5d 69 64 20 66 69 65 6c  atabase.]id fiel
258f5 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  d, or NULL */.  
258f6 54 6f 6b 65 6e 20 2a 70 56 61 6c 75 65 2c 20 20  Token *pValue,  
258f7 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72      /* Token for
258f8 20 3c 76 61 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c   <value>, or NUL
258f9 4c 20 2a 2f 0a 20 20 69 6e 74 20 6d 69 6e 75 73  L */.  int minus
258fa 46 6c 61 67 20 20 20 20 20 20 20 2f 2a 20 54 72  Flag       /* Tr
258fb 75 65 20 69 66 20 61 20 27 2d 27 20 73 69 67 6e  ue if a '-' sign
258fc 20 70 72 65 63 65 64 65 64 20 3c 76 61 6c 75 65   preceded <value
258fd 3e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a  > */.){.  char *
258fe 7a 4c 65 66 74 20 3d 20 30 3b 20 20 20 20 20 20  zLeft = 0;      
258ff 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61 74   /* Nul-terminat
25900 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20  ed UTF-8 string 
25901 3c 69 64 3e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  <id> */.  char *
25902 7a 52 69 67 68 74 20 3d 20 30 3b 20 20 20 20 20  zRight = 0;     
25903 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61 74   /* Nul-terminat
25904 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20  ed UTF-8 string 
25905 3c 76 61 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c  <value>, or NULL
25906 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
25907 20 2a 7a 44 62 20 3d 20 30 3b 20 20 20 2f 2a 20   *zDb = 0;   /* 
25908 54 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  The database nam
25909 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 49  e */.  Token *pI
2590a 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  d;            /*
2590b 20 50 6f 69 6e 74 65 72 20 74 6f 20 3c 69 64 3e   Pointer to <id>
2590c 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
2590d 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
2590e 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69     /* Database i
2590f 6e 64 65 78 20 66 6f 72 20 3c 64 61 74 61 62 61  ndex for <databa
25910 73 65 3e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  se> */.  sqlite3
25911 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
25912 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20  b;.  Db *pDb;.  
25913 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
25914 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65  ->pVdbe = sqlite
25915 33 56 64 62 65 43 72 65 61 74 65 28 64 62 29 3b  3VdbeCreate(db);
25916 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
25917 74 75 72 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e  turn;.  pParse->
25918 6e 4d 65 6d 20 3d 20 32 3b 0a 0a 20 20 2f 2a 20  nMem = 2;..  /* 
25919 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 5b 64  Interpret the [d
2591a 61 74 61 62 61 73 65 2e 5d 20 70 61 72 74 20 6f  atabase.] part o
2591b 66 20 74 68 65 20 70 72 61 67 6d 61 20 73 74 61  f the pragma sta
2591c 74 65 6d 65 6e 74 2e 20 69 44 62 20 69 73 20 74  tement. iDb is t
2591d 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 66  he.  ** index of
2591e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
2591f 69 73 20 70 72 61 67 6d 61 20 69 73 20 62 65 69  is pragma is bei
25920 6e 67 20 61 70 70 6c 69 65 64 20 74 6f 20 69 6e  ng applied to in
25921 20 64 62 2e 61 44 62 5b 5d 2e 20 2a 2f 0a 20 20   db.aDb[]. */.  
25922 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f  iDb = sqlite3Two
25923 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
25924 20 70 49 64 31 2c 20 70 49 64 32 2c 20 26 70 49   pId1, pId2, &pI
25925 64 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20  d);.  if( iDb<0 
25926 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 44 62 20  ) return;.  pDb 
25927 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
25928 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 65  ..  /* If the te
25929 6d 70 20 64 61 74 61 62 61 73 65 20 68 61 73 20  mp database has 
2592a 62 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20  been explicitly 
2592b 6e 61 6d 65 64 20 61 73 20 70 61 72 74 20 6f 66  named as part of
2592c 20 74 68 65 20 0a 20 20 2a 2a 20 70 72 61 67 6d   the .  ** pragm
2592d 61 2c 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  a, make sure it 
2592e 69 73 20 6f 70 65 6e 2e 20 0a 20 20 2a 2f 0a 20  is open. .  */. 
2592f 20 69 66 28 20 69 44 62 3d 3d 31 20 26 26 20 73   if( iDb==1 && s
25930 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61  qlite3OpenTempDa
25931 74 61 62 61 73 65 28 70 50 61 72 73 65 29 20 29  tabase(pParse) )
25932 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
25933 7d 0a 0a 20 20 7a 4c 65 66 74 20 3d 20 73 71 6c  }..  zLeft = sql
25934 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
25935 6e 28 64 62 2c 20 70 49 64 29 3b 0a 20 20 69 66  n(db, pId);.  if
25936 28 20 21 7a 4c 65 66 74 20 29 20 72 65 74 75 72  ( !zLeft ) retur
25937 6e 3b 0a 20 20 69 66 28 20 6d 69 6e 75 73 46 6c  n;.  if( minusFl
25938 61 67 20 29 7b 0a 20 20 20 20 7a 52 69 67 68 74  ag ){.    zRight
25939 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2593a 66 28 64 62 2c 20 22 2d 25 54 22 2c 20 70 56 61  f(db, "-%T", pVa
2593b 6c 75 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  lue);.  }else{. 
2593c 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71 6c 69     zRight = sqli
2593d 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
2593e 28 64 62 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20  (db, pValue);.  
2593f 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49 64  }..  assert( pId
25940 32 20 29 3b 0a 20 20 7a 44 62 20 3d 20 70 49 64  2 );.  zDb = pId
25941 32 2d 3e 6e 3e 30 20 3f 20 70 44 62 2d 3e 7a 4e  2->n>0 ? pDb->zN
25942 61 6d 65 20 3a 20 30 3b 0a 20 20 69 66 28 20 73  ame : 0;.  if( s
25943 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
25944 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 50  pParse, SQLITE_P
25945 52 41 47 4d 41 2c 20 7a 4c 65 66 74 2c 20 7a 52  RAGMA, zLeft, zR
25946 69 67 68 74 2c 20 7a 44 62 29 20 29 7b 0a 20 20  ight, zDb) ){.  
25947 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75    goto pragma_ou
25948 74 3b 0a 20 20 7d 0a 20 0a 23 69 66 6e 64 65 66  t;.  }. .#ifndef
25949 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
2594a 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a  ER_PRAGMAS.  /*.
2594b 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
2594c 74 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f  tabase.]default_
2594d 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a 2a 20  cache_size.  ** 
2594e 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
2594f 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68 65  e.]default_cache
25950 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  _size=N.  **.  *
25951 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d  * The first form
25952 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75 72   reports the cur
25953 72 65 6e 74 20 70 65 72 73 69 73 74 65 6e 74 20  rent persistent 
25954 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a  setting for the.
25955 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20    ** page cache 
25956 73 69 7a 65 2e 20 20 54 68 65 20 76 61 6c 75 65  size.  The value
25957 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
25958 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
25959 6f 66 0a 20 20 2a 2a 20 70 61 67 65 73 20 69 6e  of.  ** pages in
2595a 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e   the page cache.
2595b 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72    The second for
2595c 6d 20 73 65 74 73 20 62 6f 74 68 20 74 68 65 20  m sets both the 
2595d 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 70 61 67  current.  ** pag
2595e 65 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c  e cache size val
2595f 75 65 20 61 6e 64 20 74 68 65 20 70 65 72 73 69  ue and the persi
25960 73 74 65 6e 74 20 70 61 67 65 20 63 61 63 68 65  stent page cache
25961 20 73 69 7a 65 20 76 61 6c 75 65 0a 20 20 2a 2a   size value.  **
25962 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
25963 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20  atabase file..  
25964 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 65 66 61  **.  ** The defa
25965 75 6c 74 20 63 61 63 68 65 20 73 69 7a 65 20 69  ult cache size i
25966 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 74 61  s stored in meta
25967 2d 76 61 6c 75 65 20 32 20 6f 66 20 70 61 67 65  -value 2 of page
25968 20 31 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64   1 of the.  ** d
25969 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
2596a 68 65 20 63 61 63 68 65 20 73 69 7a 65 20 69 73  he cache size is
2596b 20 61 63 74 75 61 6c 6c 79 20 74 68 65 20 61 62   actually the ab
2596c 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f 66 0a  solute value of.
2596d 20 20 2a 2a 20 74 68 69 73 20 6d 65 6d 6f 72 79    ** this memory
2596e 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20   location.  The 
2596f 73 69 67 6e 20 6f 66 20 6d 65 74 61 2d 76 61 6c  sign of meta-val
25970 75 65 20 32 20 64 65 74 65 72 6d 69 6e 65 73 20  ue 2 determines 
25971 74 68 65 0a 20 20 2a 2a 20 73 79 6e 63 68 72 6f  the.  ** synchro
25972 6e 6f 75 73 20 73 65 74 74 69 6e 67 2e 20 20 41  nous setting.  A
25973 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20   negative value 
25974 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75  means synchronou
25975 73 20 69 73 20 6f 66 66 0a 20 20 2a 2a 20 61 6e  s is off.  ** an
25976 64 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c  d a positive val
25977 75 65 20 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f  ue means synchro
25978 6e 6f 75 73 20 69 73 20 6f 6e 2e 0a 20 20 2a 2f  nous is on..  */
25979 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2597a 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 64 65 66  rICmp(zLeft,"def
2597b 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 22  ault_cache_size"
2597c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74  )==0 ){.    stat
2597d 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
2597e 69 73 74 20 67 65 74 43 61 63 68 65 53 69 7a 65  ist getCacheSize
2597f 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f  [] = {.      { O
25980 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 30  P_ReadCookie,  0
25981 2c 20 31 2c 20 20 20 20 20 20 20 20 32 7d 2c 20  , 1,        2}, 
25982 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 0 */.      {
25983 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20 20 20   OP_IfPos,      
25984 20 31 2c 20 36 2c 20 20 20 20 20 20 20 20 30 7d   1, 6,        0}
25985 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74  ,.      { OP_Int
25986 65 67 65 72 2c 20 20 20 20 20 30 2c 20 32 2c 20  eger,     0, 2, 
25987 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20         0},.     
25988 20 7b 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20   { OP_Subtract, 
25989 20 20 20 31 2c 20 32 2c 20 20 20 20 20 20 20 20     1, 2,        
2598a 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49  1},.      { OP_I
2598b 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 36  fPos,       1, 6
2598c 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20  ,        0},.   
2598d 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
2598e 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20       0, 1,      
2598f 20 20 30 7d 2c 20 20 2f 2a 20 35 20 2a 2f 0a 20    0},  /* 5 */. 
25990 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74       { OP_Result
25991 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20 20 20 20  Row,   1, 1,    
25992 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 20      0},.    };. 
25993 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
25994 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
25995 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
25996 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
25997 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25998 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
25999 62 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69  b);.    if( !zRi
2599a 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ght ){.      sql
2599b 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
2599c 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20  ls(v, 1);.      
2599d 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2599e 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
2599f 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 61 63 68 65  AME_NAME, "cache
259a0 5f 73 69 7a 65 22 2c 20 53 51 4c 49 54 45 5f 53  _size", SQLITE_S
259a1 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 70 50  TATIC);.      pP
259a2 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32 3b  arse->nMem += 2;
259a3 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
259a4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
259a5 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
259a6 67 65 74 43 61 63 68 65 53 69 7a 65 29 2c 20 67  getCacheSize), g
259a7 65 74 43 61 63 68 65 53 69 7a 65 29 3b 0a 20 20  etCacheSize);.  
259a8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
259a9 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c  hangeP1(v, addr,
259aa 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
259ab 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
259ac 28 76 2c 20 61 64 64 72 2b 35 2c 20 53 51 4c 49  (v, addr+5, SQLI
259ad 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  TE_DEFAULT_CACHE
259ae 5f 53 49 5a 45 29 3b 0a 20 20 20 20 7d 65 6c 73  _SIZE);.    }els
259af 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a  e{.      int siz
259b0 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29  e = atoi(zRight)
259b1 3b 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a 65  ;.      if( size
259b2 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a  <0 ) size = -siz
259b3 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e;.      sqlite3
259b4 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
259b5 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
259b6 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
259b7 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
259b8 4f 50 5f 49 6e 74 65 67 65 72 2c 20 73 69 7a 65  OP_Integer, size
259b9 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
259ba 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
259bb 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20   OP_ReadCookie, 
259bc 69 44 62 2c 20 32 2c 20 32 29 3b 0a 20 20 20 20  iDb, 2, 2);.    
259bd 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
259be 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
259bf 5f 49 66 50 6f 73 2c 20 32 2c 20 30 29 3b 0a 20  _IfPos, 2, 0);. 
259c0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
259c1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
259c2 65 67 65 72 2c 20 2d 73 69 7a 65 2c 20 31 29 3b  eger, -size, 1);
259c3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
259c4 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
259c5 64 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  dr);.      sqlit
259c6 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
259c7 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44  OP_SetCookie, iD
259c8 62 2c 20 32 2c 20 31 29 3b 0a 20 20 20 20 20 20  b, 2, 1);.      
259c9 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
259ca 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b  che_size = size;
259cb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
259cc 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28  reeSetCacheSize(
259cd 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70  pDb->pBt, pDb->p
259ce 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
259cf 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ze);.    }.  }el
259d0 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
259d1 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
259d2 5d 70 61 67 65 5f 73 69 7a 65 0a 20 20 2a 2a 20  ]page_size.  ** 
259d3 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
259d4 65 2e 5d 70 61 67 65 5f 73 69 7a 65 3d 4e 0a 20  e.]page_size=N. 
259d5 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72   **.  ** The fir
259d6 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20  st form reports 
259d7 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 74  the current sett
259d8 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ing for the.  **
259d9 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73   database page s
259da 69 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20 54  ize in bytes.  T
259db 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73  he second form s
259dc 65 74 73 20 74 68 65 0a 20 20 2a 2a 20 64 61 74  ets the.  ** dat
259dd 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20  abase page size 
259de 76 61 6c 75 65 2e 20 20 54 68 65 20 76 61 6c 75  value.  The valu
259df 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 73 65  e can only be se
259e0 74 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 64 61  t if.  ** the da
259e1 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74 20 79  tabase has not y
259e2 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e  et been created.
259e3 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
259e4 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
259e5 2c 22 70 61 67 65 5f 73 69 7a 65 22 29 3d 3d 30  ,"page_size")==0
259e6 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   ){.    Btree *p
259e7 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20  Bt = pDb->pBt;. 
259e8 20 20 20 61 73 73 65 72 74 28 20 70 42 74 21 3d     assert( pBt!=
259e9 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 52  0 );.    if( !zR
259ea 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  ight ){.      in
259eb 74 20 73 69 7a 65 20 3d 20 41 4c 57 41 59 53 28  t size = ALWAYS(
259ec 70 42 74 29 20 3f 20 73 71 6c 69 74 65 33 42 74  pBt) ? sqlite3Bt
259ed 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
259ee 42 74 29 20 3a 20 30 3b 0a 20 20 20 20 20 20 72  Bt) : 0;.      r
259ef 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
259f0 50 61 72 73 65 2c 20 22 70 61 67 65 5f 73 69 7a  Parse, "page_siz
259f1 65 22 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 7d  e", size);.    }
259f2 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4d  else{.      /* M
259f3 61 6c 6c 6f 63 20 6d 61 79 20 66 61 69 6c 20 77  alloc may fail w
259f4 68 65 6e 20 73 65 74 74 69 6e 67 20 74 68 65 20  hen setting the 
259f5 70 61 67 65 2d 73 69 7a 65 2c 20 61 73 20 74 68  page-size, as th
259f6 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  ere is an intern
259f7 61 6c 0a 20 20 20 20 20 20 2a 2a 20 62 75 66 66  al.      ** buff
259f8 65 72 20 74 68 61 74 20 74 68 65 20 70 61 67 65  er that the page
259f9 72 20 6d 6f 64 75 6c 65 20 72 65 73 69 7a 65 73  r module resizes
259fa 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 72   using sqlite3_r
259fb 65 61 6c 6c 6f 63 28 29 2e 0a 20 20 20 20 20 20  ealloc()..      
259fc 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65 78  */.      db->nex
259fd 74 50 61 67 65 73 69 7a 65 20 3d 20 61 74 6f 69  tPagesize = atoi
259fe 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  (zRight);.      
259ff 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  if( SQLITE_NOMEM
25a00 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  ==sqlite3BtreeSe
25a01 74 50 61 67 65 53 69 7a 65 28 70 42 74 2c 20 64  tPageSize(pBt, d
25a02 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 2c  b->nextPagesize,
25a03 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20   -1) ){.        
25a04 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
25a05 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
25a06 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f    }.  }else..  /
25a07 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
25a08 64 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61  database.]max_pa
25a09 67 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50  ge_count.  **  P
25a0a 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
25a0b 5d 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d  ]max_page_count=
25a0c 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
25a0d 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
25a0e 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ts the current s
25a0f 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20  etting for the. 
25a10 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62   ** maximum numb
25a11 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
25a12 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
25a13 2e 20 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63  .  The .  ** sec
25a14 6f 6e 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74  ond form attempt
25a15 73 20 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73  s to change this
25a16 20 73 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a   setting.  Both.
25a17 20 20 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72    ** forms retur
25a18 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  n the current se
25a19 74 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66  tting..  */.  if
25a1a 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
25a1b 28 7a 4c 65 66 74 2c 22 6d 61 78 5f 70 61 67 65  (zLeft,"max_page
25a1c 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20  _count")==0 ){. 
25a1d 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
25a1e 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e  pDb->pBt;.    in
25a1f 74 20 6e 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20  t newMax = 0;.  
25a20 20 20 61 73 73 65 72 74 28 20 70 42 74 21 3d 30    assert( pBt!=0
25a21 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67   );.    if( zRig
25a22 68 74 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d  ht ){.      newM
25a23 61 78 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74  ax = atoi(zRight
25a24 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
25a25 20 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a   ALWAYS(pBt) ){.
25a26 20 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 73        newMax = s
25a27 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61  qlite3BtreeMaxPa
25a28 67 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77  geCount(pBt, new
25a29 4d 61 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Max);.    }.    
25a2a 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
25a2b 70 50 61 72 73 65 2c 20 22 6d 61 78 5f 70 61 67  pParse, "max_pag
25a2c 65 5f 63 6f 75 6e 74 22 2c 20 6e 65 77 4d 61 78  e_count", newMax
25a2d 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a  );.  }else..  /*
25a2e 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
25a2f 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 63 6f  atabase.]page_co
25a30 75 6e 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65  unt.  **.  ** Re
25a31 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
25a32 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
25a33 73 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61  specified databa
25a34 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  se..  */.  if( s
25a35 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
25a36 65 66 74 2c 22 70 61 67 65 5f 63 6f 75 6e 74 22  eft,"page_count"
25a37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
25a38 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 73 71  iReg;.    if( sq
25a39 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
25a3a 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
25a3b 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73  ragma_out;.    s
25a3c 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
25a3d 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
25a3e 44 62 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20  Db);.    iReg = 
25a3f 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
25a40 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25a41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 50 61 67 65  ddOp2(v, OP_Page
25a42 63 6f 75 6e 74 2c 20 69 44 62 2c 20 69 52 65 67  count, iDb, iReg
25a43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
25a44 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
25a45 65 73 75 6c 74 52 6f 77 2c 20 69 52 65 67 2c 20  esultRow, iReg, 
25a46 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
25a47 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
25a48 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
25a49 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
25a4a 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
25a4b 45 2c 20 22 70 61 67 65 5f 63 6f 75 6e 74 22 2c  E, "page_count",
25a4c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
25a4d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
25a4e 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
25a4f 61 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d  abase.]locking_m
25a50 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  ode.  **  PRAGMA
25a51 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b   [database.]lock
25a52 69 6e 67 5f 6d 6f 64 65 20 3d 20 28 6e 6f 72 6d  ing_mode = (norm
25a53 61 6c 7c 65 78 63 6c 75 73 69 76 65 29 0a 20 20  al|exclusive).  
25a54 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
25a55 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6c  StrICmp(zLeft,"l
25a56 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 29 3d 3d 30  ocking_mode")==0
25a57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   ){.    const ch
25a58 61 72 20 2a 7a 52 65 74 20 3d 20 22 6e 6f 72 6d  ar *zRet = "norm
25a59 61 6c 22 3b 0a 20 20 20 20 69 6e 74 20 65 4d 6f  al";.    int eMo
25a5a 64 65 20 3d 20 67 65 74 4c 6f 63 6b 69 6e 67 4d  de = getLockingM
25a5b 6f 64 65 28 7a 52 69 67 68 74 29 3b 0a 0a 20 20  ode(zRight);..  
25a5c 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30    if( pId2->n==0
25a5d 20 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   && eMode==PAGER
25a5e 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45  _LOCKINGMODE_QUE
25a5f 52 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53  RY ){.      /* S
25a60 69 6d 70 6c 65 20 22 50 52 41 47 4d 41 20 6c 6f  imple "PRAGMA lo
25a61 63 6b 69 6e 67 5f 6d 6f 64 65 3b 22 20 73 74 61  cking_mode;" sta
25a62 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20  tement. This is 
25a63 61 20 71 75 65 72 79 20 66 6f 72 0a 20 20 20 20  a query for.    
25a64 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74    ** the current
25a65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67   default locking
25a66 20 6d 6f 64 65 20 28 77 68 69 63 68 20 6d 61 79   mode (which may
25a67 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f   be different to
25a68 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f  .      ** the lo
25a69 63 6b 69 6e 67 2d 6d 6f 64 65 20 6f 66 20 74 68  cking-mode of th
25a6a 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 29  e main database)
25a6b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
25a6c 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c   eMode = db->dfl
25a6d 74 4c 6f 63 6b 4d 6f 64 65 3b 0a 20 20 20 20 7d  tLockMode;.    }
25a6e 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65  else{.      Page
25a6f 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20  r *pPager;.     
25a70 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20   if( pId2->n==0 
25a71 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
25a72 69 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  is indicates tha
25a73 74 20 6e 6f 20 64 61 74 61 62 61 73 65 20 6e 61  t no database na
25a74 6d 65 20 77 61 73 20 73 70 65 63 69 66 69 65 64  me was specified
25a75 20 61 73 20 70 61 72 74 0a 20 20 20 20 20 20 20   as part.       
25a76 20 2a 2a 20 6f 66 20 74 68 65 20 50 52 41 47 4d   ** of the PRAGM
25a77 41 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68  A command. In th
25a78 69 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63 6b  is case the lock
25a79 69 6e 67 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65  ing-mode must be
25a7a 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20  .        ** set 
25a7b 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20  on all attached 
25a7c 64 61 74 61 62 61 73 65 73 2c 20 61 73 20 77 65  databases, as we
25a7d 6c 6c 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64  ll as the main d
25a7e 62 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20  b file..        
25a7f 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 6c  **.        ** Al
25a80 73 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 33 2e  so, the sqlite3.
25a81 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 76 61 72  dfltLockMode var
25a82 69 61 62 6c 65 20 69 73 20 73 65 74 20 73 6f 20  iable is set so 
25a83 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  that.        ** 
25a84 61 6e 79 20 73 75 62 73 65 71 75 65 6e 74 6c 79  any subsequently
25a85 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
25a86 73 65 73 20 61 6c 73 6f 20 75 73 65 20 74 68 65  ses also use the
25a87 20 73 70 65 63 69 66 69 65 64 0a 20 20 20 20 20   specified.     
25a88 20 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 6d 6f     ** locking mo
25a89 64 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  de..        */. 
25a8a 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20         int ii;. 
25a8b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 44         assert(pD
25a8c 62 3d 3d 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b  b==&db->aDb[0]);
25a8d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d  .        for(ii=
25a8e 32 3b 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  2; ii<db->nDb; i
25a8f 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
25a90 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
25a91 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61  BtreePager(db->a
25a92 44 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20  Db[ii].pBt);.   
25a93 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
25a94 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70  gerLockingMode(p
25a95 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20  Pager, eMode);. 
25a96 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
25a97 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
25a98 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20  e = (u8)eMode;. 
25a99 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61       }.      pPa
25a9a 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
25a9b 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74  eePager(pDb->pBt
25a9c 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  );.      eMode =
25a9d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
25a9e 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
25a9f 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a   eMode);.    }..
25aa0 20 20 20 20 61 73 73 65 72 74 28 65 4d 6f 64 65      assert(eMode
25aa1 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
25aa2 4f 44 45 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64  ODE_NORMAL||eMod
25aa3 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
25aa4 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 29 3b  MODE_EXCLUSIVE);
25aa5 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d  .    if( eMode==
25aa6 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
25aa7 45 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20  E_EXCLUSIVE ){. 
25aa8 20 20 20 20 20 7a 52 65 74 20 3d 20 22 65 78 63       zRet = "exc
25aa9 6c 75 73 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20  lusive";.    }. 
25aaa 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25aab 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
25aac 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
25aad 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
25aae 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c  COLNAME_NAME, "l
25aaf 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 2c 20 53 51  ocking_mode", SQ
25ab0 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
25ab1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25ab2 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
25ab3 38 2c 20 30 2c 20 31 2c 20 30 2c 20 7a 52 65 74  8, 0, 1, 0, zRet
25ab4 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
25ab5 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25ab6 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
25ab7 31 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f  1);.  }else..  /
25ab8 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
25ab9 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61  database.]journa
25aba 6c 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41  l_mode.  **  PRA
25abb 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a  GMA [database.]j
25abc 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 28 64  ournal_mode = (d
25abd 65 6c 65 74 65 7c 70 65 72 73 69 73 74 7c 6f 66  elete|persist|of
25abe 66 7c 74 72 75 6e 63 61 74 65 7c 6d 65 6d 6f 72  f|truncate|memor
25abf 79 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  y).  */.  if( sq
25ac0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
25ac1 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  ft,"journal_mode
25ac2 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ")==0 ){.    int
25ac3 20 65 4d 6f 64 65 3b 0a 20 20 20 20 73 74 61 74   eMode;.    stat
25ac4 69 63 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20  ic char * const 
25ac5 61 7a 4d 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b  azModeName[] = {
25ac6 0a 20 20 20 20 20 20 22 64 65 6c 65 74 65 22 2c  .      "delete",
25ac7 20 22 70 65 72 73 69 73 74 22 2c 20 22 6f 66 66   "persist", "off
25ac8 22 2c 20 22 74 72 75 6e 63 61 74 65 22 2c 20 22  ", "truncate", "
25ac9 6d 65 6d 6f 72 79 22 0a 20 20 20 20 7d 3b 0a 0a  memory".    };..
25aca 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d      if( zRight==
25acb 30 20 29 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65  0 ){.      eMode
25acc 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c   = PAGER_JOURNAL
25acd 4d 4f 44 45 5f 51 55 45 52 59 3b 0a 20 20 20 20  MODE_QUERY;.    
25ace 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
25acf 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
25ad0 65 6e 33 30 28 7a 52 69 67 68 74 29 3b 0a 20 20  en30(zRight);.  
25ad1 20 20 20 20 65 4d 6f 64 65 20 3d 20 73 69 7a 65      eMode = size
25ad2 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 29 2f 73  of(azModeName)/s
25ad3 69 7a 65 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65  izeof(azModeName
25ad4 5b 30 5d 29 20 2d 20 31 3b 0a 20 20 20 20 20 20  [0]) - 1;.      
25ad5 77 68 69 6c 65 28 20 65 4d 6f 64 65 3e 3d 30 20  while( eMode>=0 
25ad6 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
25ad7 6d 70 28 7a 52 69 67 68 74 2c 20 61 7a 4d 6f 64  mp(zRight, azMod
25ad8 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 6e 29  eName[eMode], n)
25ad9 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 65  !=0 ){.        e
25ada 4d 6f 64 65 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a  Mode--;.      }.
25adb 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49      }.    if( pI
25adc 64 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64  d2->n==0 && eMod
25add 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
25ade 4d 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20  MODE_QUERY ){.  
25adf 20 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50      /* Simple "P
25ae0 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
25ae1 64 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20  de;" statement. 
25ae2 54 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20  This is a query 
25ae3 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  for.      ** the
25ae4 20 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74   current default
25ae5 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 28 77   journal mode (w
25ae6 68 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66  hich may be diff
25ae7 65 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a  erent to.      *
25ae8 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  * the journal-mo
25ae9 64 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64  de of the main d
25aea 61 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20  atabase)..      
25aeb 2a 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  */.      eMode =
25aec 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c   db->dfltJournal
25aed 4d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Mode;.    }else{
25aee 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50  .      Pager *pP
25aef 61 67 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20  ager;.      if( 
25af0 70 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20  pId2->n==0 ){.  
25af1 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 6e        /* This in
25af2 64 69 63 61 74 65 73 20 74 68 61 74 20 6e 6f 20  dicates that no 
25af3 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 77 61  database name wa
25af4 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70  s specified as p
25af5 61 72 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  art.        ** o
25af6 66 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d  f the PRAGMA com
25af7 6d 61 6e 64 2e 20 49 6e 20 74 68 69 73 20 63 61  mand. In this ca
25af8 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d  se the journal-m
25af9 6f 64 65 20 6d 75 73 74 20 62 65 0a 20 20 20 20  ode must be.    
25afa 20 20 20 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c      ** set on al
25afb 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  l attached datab
25afc 61 73 65 73 2c 20 61 73 20 77 65 6c 6c 20 61 73  ases, as well as
25afd 20 74 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c   the main db fil
25afe 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  e..        **.  
25aff 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74        ** Also, t
25b00 68 65 20 73 71 6c 69 74 65 33 2e 64 66 6c 74 4a  he sqlite3.dfltJ
25b01 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 72 69 61  ournalMode varia
25b02 62 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74 68  ble is set so th
25b03 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  at.        ** an
25b04 79 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 61  y subsequently a
25b05 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
25b06 73 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20 73  s also use the s
25b07 70 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 20  pecified.       
25b08 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65   ** journal mode
25b09 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
25b0a 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20       int ii;.   
25b0b 20 20 20 20 20 61 73 73 65 72 74 28 70 44 62 3d       assert(pDb=
25b0c 3d 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20  =&db->aDb[0]);. 
25b0d 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 31 3b         for(ii=1;
25b0e 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b   ii<db->nDb; ii+
25b0f 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
25b10 28 20 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42  ( db->aDb[ii].pB
25b11 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
25b12 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65   pPager = sqlite
25b13 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e  3BtreePager(db->
25b14 61 44 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20  aDb[ii].pBt);.  
25b15 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25b16 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64  3PagerJournalMod
25b17 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29  e(pPager, eMode)
25b18 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
25b19 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
25b1a 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d  db->dfltJournalM
25b1b 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b  ode = (u8)eMode;
25b1c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
25b1d 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
25b1e 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70  treePager(pDb->p
25b1f 42 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65  Bt);.      eMode
25b20 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a   = sqlite3PagerJ
25b21 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65  ournalMode(pPage
25b22 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d  r, eMode);.    }
25b23 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 4d 6f  .    assert( eMo
25b24 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
25b25 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 20 20  LMODE_DELETE.   
25b26 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
25b27 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
25b28 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a  ALMODE_TRUNCATE.
25b29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
25b2a 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
25b2b 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
25b2c 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T.              
25b2d 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
25b2e 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a  JOURNALMODE_OFF.
25b2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
25b30 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
25b31 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
25b32 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
25b33 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
25b34 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
25b35 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
25b36 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
25b37 45 2c 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  E, "journal_mode
25b38 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
25b39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
25b3a 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
25b3b 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c  tring8, 0, 1, 0,
25b3c 20 0a 20 20 20 20 20 20 20 20 20 20 20 61 7a 4d   .           azM
25b3d 6f 64 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20  odeName[eMode], 
25b3e 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
25b3f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25b40 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
25b41 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73  w, 1, 1);.  }els
25b42 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  e..  /*.  **  PR
25b43 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
25b44 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d  journal_size_lim
25b45 69 74 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  it.  **  PRAGMA 
25b46 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e  [database.]journ
25b47 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 3d 4e 0a  al_size_limit=N.
25b48 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72    **.  ** Get or
25b49 20 73 65 74 20 74 68 65 20 73 69 7a 65 20 6c 69   set the size li
25b4a 6d 69 74 20 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20  mit on rollback 
25b4b 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 20  journal files.. 
25b4c 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
25b4d 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
25b4e 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d  journal_size_lim
25b4f 69 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 50  it")==0 ){.    P
25b50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73  ager *pPager = s
25b51 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
25b52 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20  (pDb->pBt);.    
25b53 69 36 34 20 69 4c 69 6d 69 74 20 3d 20 2d 32 3b  i64 iLimit = -2;
25b54 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20  .    if( zRight 
25b55 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4c 69  ){.      int iLi
25b56 6d 69 74 33 32 20 3d 20 61 74 6f 69 28 7a 52 69  mit32 = atoi(zRi
25b57 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ght);.      if( 
25b58 69 4c 69 6d 69 74 33 32 3c 2d 31 20 29 7b 0a 20  iLimit32<-1 ){. 
25b59 20 20 20 20 20 20 20 69 4c 69 6d 69 74 33 32 20         iLimit32 
25b5a 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = -1;.      }.  
25b5b 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 69 4c 69      iLimit = iLi
25b5c 6d 69 74 33 32 3b 0a 20 20 20 20 7d 0a 20 20 20  mit32;.    }.   
25b5d 20 69 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65   iLimit = sqlite
25b5e 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a  3PagerJournalSiz
25b5f 65 4c 69 6d 69 74 28 70 50 61 67 65 72 2c 20 69  eLimit(pPager, i
25b60 4c 69 6d 69 74 29 3b 0a 20 20 20 20 72 65 74 75  Limit);.    retu
25b61 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
25b62 73 65 2c 20 22 6a 6f 75 72 6e 61 6c 5f 73 69 7a  se, "journal_siz
25b63 65 5f 6c 69 6d 69 74 22 2c 20 28 69 6e 74 29 69  e_limit", (int)i
25b64 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 0a  Limit);.  }else.
25b65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
25b66 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
25b67 47 4d 41 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20  GMAS */..  /*.  
25b68 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
25b69 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75  base.]auto_vacuu
25b6a 6d 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  m.  **  PRAGMA [
25b6b 64 61 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76  database.]auto_v
25b6c 61 63 75 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  acuum=N.  **.  *
25b6d 2a 20 47 65 74 20 6f 72 20 73 65 74 20 74 68 65  * Get or set the
25b6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61   value of the da
25b6f 74 61 62 61 73 65 20 27 61 75 74 6f 2d 76 61 63  tabase 'auto-vac
25b70 75 75 6d 27 20 70 61 72 61 6d 65 74 65 72 2e 0a  uum' parameter..
25b71 20 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69    ** The value i
25b72 73 20 6f 6e 65 20 6f 66 3a 20 20 30 20 4e 4f 4e  s one of:  0 NON
25b73 45 20 31 20 46 55 4c 4c 20 32 20 49 4e 43 52 45  E 1 FULL 2 INCRE
25b74 4d 45 4e 54 41 4c 0a 20 20 2a 2f 0a 23 69 66 6e  MENTAL.  */.#ifn
25b75 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
25b76 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28  AUTOVACUUM.  if(
25b77 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
25b78 7a 4c 65 66 74 2c 22 61 75 74 6f 5f 76 61 63 75  zLeft,"auto_vacu
25b79 75 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42  um")==0 ){.    B
25b7a 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d  tree *pBt = pDb-
25b7b 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74  >pBt;.    assert
25b7c 28 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20  ( pBt!=0 );.    
25b7d 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
25b7e 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
25b7f 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67  .      goto prag
25b80 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  ma_out;.    }.  
25b81 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
25b82 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74 6f 5f  .      int auto_
25b83 76 61 63 75 75 6d 3b 0a 20 20 20 20 20 20 69 66  vacuum;.      if
25b84 28 20 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b  ( ALWAYS(pBt) ){
25b85 0a 20 20 20 20 20 20 20 20 20 61 75 74 6f 5f 76  .         auto_v
25b86 61 63 75 75 6d 20 3d 20 73 71 6c 69 74 65 33 42  acuum = sqlite3B
25b87 74 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75  treeGetAutoVacuu
25b88 6d 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 65  m(pBt);.      }e
25b89 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 61 75  lse{.         au
25b8a 74 6f 5f 76 61 63 75 75 6d 20 3d 20 53 51 4c 49  to_vacuum = SQLI
25b8b 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56  TE_DEFAULT_AUTOV
25b8c 41 43 55 55 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  ACUUM;.      }. 
25b8d 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c       returnSingl
25b8e 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 61 75  eInt(pParse, "au
25b8f 74 6f 5f 76 61 63 75 75 6d 22 2c 20 61 75 74 6f  to_vacuum", auto
25b90 5f 76 61 63 75 75 6d 29 3b 0a 20 20 20 20 7d 65  _vacuum);.    }e
25b91 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 65  lse{.      int e
25b92 41 75 74 6f 20 3d 20 67 65 74 41 75 74 6f 56 61  Auto = getAutoVa
25b93 63 75 75 6d 28 7a 52 69 67 68 74 29 3b 0a 20 20  cuum(zRight);.  
25b94 20 20 20 20 61 73 73 65 72 74 28 20 65 41 75 74      assert( eAut
25b95 6f 3e 3d 30 20 26 26 20 65 41 75 74 6f 3c 3d 32  o>=0 && eAuto<=2
25b96 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65   );.      db->ne
25b97 78 74 41 75 74 6f 76 61 63 20 3d 20 28 75 38 29  xtAutovac = (u8)
25b98 65 41 75 74 6f 3b 0a 20 20 20 20 20 20 69 66 28  eAuto;.      if(
25b99 20 41 4c 57 41 59 53 28 65 41 75 74 6f 3e 3d 30   ALWAYS(eAuto>=0
25b9a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
25b9b 43 61 6c 6c 20 53 65 74 41 75 74 6f 56 61 63 75  Call SetAutoVacu
25b9c 75 6d 28 29 20 74 6f 20 73 65 74 20 69 6e 69 74  um() to set init
25b9d 69 61 6c 69 7a 65 20 74 68 65 20 69 6e 74 65 72  ialize the inter
25b9e 6e 61 6c 20 61 75 74 6f 20 61 6e 64 0a 20 20 20  nal auto and.   
25b9f 20 20 20 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63       ** incr-vac
25ba0 75 75 6d 20 66 6c 61 67 73 2e 20 54 68 69 73 20  uum flags. This 
25ba1 69 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 63  is required in c
25ba2 61 73 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74  ase this connect
25ba3 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  ion.        ** c
25ba4 72 65 61 74 65 73 20 74 68 65 20 64 61 74 61 62  reates the datab
25ba5 61 73 65 20 66 69 6c 65 2e 20 49 74 20 69 73 20  ase file. It is 
25ba6 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 69  important that i
25ba7 74 20 69 73 20 63 72 65 61 74 65 64 0a 20 20 20  t is created.   
25ba8 20 20 20 20 20 2a 2a 20 61 73 20 61 6e 20 61 75       ** as an au
25ba9 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c  to-vacuum capabl
25baa 65 20 64 62 2e 0a 20 20 20 20 20 20 20 20 2a 2f  e db..        */
25bab 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20  .        int rc 
25bac 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  = sqlite3BtreeSe
25bad 74 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74 2c  tAutoVacuum(pBt,
25bae 20 65 41 75 74 6f 29 3b 0a 20 20 20 20 20 20 20   eAuto);.       
25baf 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
25bb0 4f 4b 20 26 26 20 28 65 41 75 74 6f 3d 3d 31 20  OK && (eAuto==1 
25bb1 7c 7c 20 65 41 75 74 6f 3d 3d 32 29 20 29 7b 0a  || eAuto==2) ){.
25bb2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65            /* Whe
25bb3 6e 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75  n setting the au
25bb4 74 6f 5f 76 61 63 75 75 6d 20 6d 6f 64 65 20 74  to_vacuum mode t
25bb5 6f 20 65 69 74 68 65 72 20 22 66 75 6c 6c 22 20  o either "full" 
25bb6 6f 72 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  or .          **
25bb7 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 2c 20   "incremental", 
25bb8 77 72 69 74 65 20 74 68 65 20 76 61 6c 75 65 20  write the value 
25bb9 6f 66 20 6d 65 74 61 5b 36 5d 20 69 6e 20 74 68  of meta[6] in th
25bba 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20  e database.     
25bbb 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65       ** file. Be
25bbc 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20  fore writing to 
25bbd 6d 65 74 61 5b 36 5d 2c 20 63 68 65 63 6b 20 74  meta[6], check t
25bbe 68 61 74 20 6d 65 74 61 5b 33 5d 20 69 6e 64 69  hat meta[3] indi
25bbf 63 61 74 65 73 0a 20 20 20 20 20 20 20 20 20 20  cates.          
25bc0 2a 2a 20 74 68 61 74 20 74 68 69 73 20 72 65 61  ** that this rea
25bc1 6c 6c 79 20 69 73 20 61 6e 20 61 75 74 6f 2d 76  lly is an auto-v
25bc2 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64 61  acuum capable da
25bc3 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20  tabase..        
25bc4 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73    */.          s
25bc5 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
25bc6 4f 70 4c 69 73 74 20 73 65 74 4d 65 74 61 36 5b  OpList setMeta6[
25bc7 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
25bc8 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69    { OP_Transacti
25bc9 6f 6e 2c 20 20 20 20 30 2c 20 20 20 20 20 20 20  on,    0,       
25bca 20 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20          1,      
25bcb 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f    0},    /* 0 */
25bcc 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
25bcd 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20  P_ReadCookie,   
25bce 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
25bcf 20 20 20 31 2c 20 20 20 20 20 20 20 20 33 7d 2c     1,        3},
25bd0 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
25bd1 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 2c          { OP_If,
25bd2 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
25bd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
25bd4 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
25bd5 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 2 */.         
25bd6 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20 20 20     { OP_Halt,   
25bd7 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
25bd8 4b 2c 20 20 20 20 20 20 20 4f 45 5f 41 62 6f 72  K,       OE_Abor
25bd9 74 2c 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a  t, 0},    /* 3 *
25bda 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
25bdb 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20  OP_Integer,     
25bdc 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
25bdd 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 30 7d      1,        0}
25bde 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20  ,    /* 4 */.   
25bdf 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65           { OP_Se
25be0 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c  tCookie,      0,
25be1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
25be2 2c 20 20 20 20 20 20 20 20 31 7d 2c 20 20 20 20  ,        1},    
25be3 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 5 */.        
25be4 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 69    };.          i
25be5 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20  nt iAddr;.      
25be6 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69      iAddr = sqli
25be7 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
25be8 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73 65  (v, ArraySize(se
25be9 74 4d 65 74 61 36 29 2c 20 73 65 74 4d 65 74 61  tMeta6), setMeta
25bea 36 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  6);.          sq
25beb 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
25bec 31 28 76 2c 20 69 41 64 64 72 2c 20 69 44 62 29  1(v, iAddr, iDb)
25bed 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
25bee 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
25bef 76 2c 20 69 41 64 64 72 2b 31 2c 20 69 44 62 29  v, iAddr+1, iDb)
25bf0 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
25bf1 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
25bf2 76 2c 20 69 41 64 64 72 2b 32 2c 20 69 41 64 64  v, iAddr+2, iAdd
25bf3 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+4);.          
25bf4 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
25bf5 65 50 31 28 76 2c 20 69 41 64 64 72 2b 34 2c 20  eP1(v, iAddr+4, 
25bf6 65 41 75 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20  eAuto-1);.      
25bf7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
25bf8 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72  hangeP1(v, iAddr
25bf9 2b 35 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  +5, iDb);.      
25bfa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55      sqlite3VdbeU
25bfb 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29  sesBtree(v, iDb)
25bfc 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
25bfd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
25bfe 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20  e.#endif..  /*. 
25bff 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
25c00 61 62 61 73 65 2e 5d 69 6e 63 72 65 6d 65 6e 74  abase.]increment
25c01 61 6c 5f 76 61 63 75 75 6d 28 4e 29 0a 20 20 2a  al_vacuum(N).  *
25c02 2a 0a 20 20 2a 2a 20 44 6f 20 4e 20 73 74 65 70  *.  ** Do N step
25c03 73 20 6f 66 20 69 6e 63 72 65 6d 65 6e 74 61 6c  s of incremental
25c04 20 76 61 63 75 75 6d 69 6e 67 20 6f 6e 20 61 20   vacuuming on a 
25c05 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 23  database..  */.#
25c06 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25c07 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
25c08 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
25c09 6d 70 28 7a 4c 65 66 74 2c 22 69 6e 63 72 65 6d  mp(zLeft,"increm
25c0a 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 29 3d 3d  ental_vacuum")==
25c0b 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4c 69  0 ){.    int iLi
25c0c 6d 69 74 2c 20 61 64 64 72 3b 0a 20 20 20 20 69  mit, addr;.    i
25c0d 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
25c0e 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a  hema(pParse) ){.
25c0f 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d        goto pragm
25c10 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  a_out;.    }.   
25c11 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 7c   if( zRight==0 |
25c12 7c 20 21 73 71 6c 69 74 65 33 47 65 74 49 6e 74  | !sqlite3GetInt
25c13 33 32 28 7a 52 69 67 68 74 2c 20 26 69 4c 69 6d  32(zRight, &iLim
25c14 69 74 29 20 7c 7c 20 69 4c 69 6d 69 74 3c 3d 30  it) || iLimit<=0
25c15 20 29 7b 0a 20 20 20 20 20 20 69 4c 69 6d 69 74   ){.      iLimit
25c16 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20   = 0x7fffffff;. 
25c17 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
25c18 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
25c19 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
25c1a 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
25c1b 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25c1c 5f 49 6e 74 65 67 65 72 2c 20 69 4c 69 6d 69 74  _Integer, iLimit
25c1d 2c 20 31 29 3b 0a 20 20 20 20 61 64 64 72 20 3d  , 1);.    addr =
25c1e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25c1f 70 31 28 76 2c 20 4f 50 5f 49 6e 63 72 56 61 63  p1(v, OP_IncrVac
25c20 75 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  uum, iDb);.    s
25c21 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
25c22 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
25c23 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
25c24 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25c25 50 5f 41 64 64 49 6d 6d 2c 20 31 2c 20 2d 31 29  P_AddImm, 1, -1)
25c26 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25c27 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
25c28 50 6f 73 2c 20 31 2c 20 61 64 64 72 29 3b 0a 20  Pos, 1, addr);. 
25c29 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
25c2a 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
25c2b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a  .  }else.#endif.
25c2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
25c2d 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
25c2e 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  AS.  /*.  **  PR
25c2f 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
25c30 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a 2a 20  cache_size.  ** 
25c31 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
25c32 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0a  e.]cache_size=N.
25c33 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69    **.  ** The fi
25c34 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73  rst form reports
25c35 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63   the current loc
25c36 61 6c 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74  al setting for t
25c37 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63  he.  ** page cac
25c38 68 65 20 73 69 7a 65 2e 20 20 54 68 65 20 6c 6f  he size.  The lo
25c39 63 61 6c 20 73 65 74 74 69 6e 67 20 63 61 6e 20  cal setting can 
25c3a 62 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  be different fro
25c3b 6d 0a 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69  m.  ** the persi
25c3c 73 74 65 6e 74 20 63 61 63 68 65 20 73 69 7a 65  stent cache size
25c3d 20 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 73   value that is s
25c3e 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74  tored in the dat
25c3f 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 20  abase.  ** file 
25c40 69 74 73 65 6c 66 2e 20 20 54 68 65 20 76 61 6c  itself.  The val
25c41 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
25c42 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
25c43 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65 73 20  r of.  ** pages 
25c44 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  in the page cach
25c45 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66  e.  The second f
25c46 6f 72 6d 20 73 65 74 73 20 74 68 65 20 6c 6f 63  orm sets the loc
25c47 61 6c 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63  al.  ** page cac
25c48 68 65 20 73 69 7a 65 20 76 61 6c 75 65 2e 20 20  he size value.  
25c49 49 74 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e  It does not chan
25c4a 67 65 20 74 68 65 20 70 65 72 73 69 73 74 65 6e  ge the persisten
25c4b 74 0a 20 20 2a 2a 20 63 61 63 68 65 20 73 69 7a  t.  ** cache siz
25c4c 65 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20  e stored on the 
25c4d 64 69 73 6b 20 73 6f 20 74 68 65 20 63 61 63 68  disk so the cach
25c4e 65 20 73 69 7a 65 20 77 69 6c 6c 20 72 65 76 65  e size will reve
25c4f 72 74 0a 20 20 2a 2a 20 74 6f 20 69 74 73 20 64  rt.  ** to its d
25c50 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77 68 65  efault value whe
25c51 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
25c52 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 72 65 6f  s closed and reo
25c53 70 65 6e 65 64 2e 0a 20 20 2a 2a 20 4e 20 73 68  pened..  ** N sh
25c54 6f 75 6c 64 20 62 65 20 61 20 70 6f 73 69 74 69  ould be a positi
25c55 76 65 20 69 6e 74 65 67 65 72 2e 0a 20 20 2a 2f  ve integer..  */
25c56 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
25c57 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 63 61 63  rICmp(zLeft,"cac
25c58 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a  he_size")==0 ){.
25c59 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
25c5a 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
25c5b 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
25c5c 6f 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52  out;.    if( !zR
25c5d 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65  ight ){.      re
25c5e 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
25c5f 61 72 73 65 2c 20 22 63 61 63 68 65 5f 73 69 7a  arse, "cache_siz
25c60 65 22 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  e", pDb->pSchema
25c61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20  ->cache_size);. 
25c62 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
25c63 69 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f 69 28  int size = atoi(
25c64 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69  zRight);.      i
25c65 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69 7a 65  f( size<0 ) size
25c66 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20 20 20   = -size;.      
25c67 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
25c68 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b  che_size = size;
25c69 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
25c6a 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28  reeSetCacheSize(
25c6b 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70  pDb->pBt, pDb->p
25c6c 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
25c6d 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ze);.    }.  }el
25c6e 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20  se..  /*.  **   
25c6f 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72  PRAGMA temp_stor
25c70 65 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  e.  **   PRAGMA 
25c71 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 22 64 65  temp_store = "de
25c72 66 61 75 6c 74 22 7c 22 6d 65 6d 6f 72 79 22 7c  fault"|"memory"|
25c73 22 66 69 6c 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a  "file".  **.  **
25c74 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74   Return or set t
25c75 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f  he local value o
25c76 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f 72 65  f the temp_store
25c77 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67   flag.  Changing
25c78 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20  .  ** the local 
25c79 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20 6d  value does not m
25c7a 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74  ake changes to t
25c7b 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e 64  he disk file and
25c7c 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20 2a   the default.  *
25c7d 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20  * value will be 
25c7e 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e 65 78  restored the nex
25c7f 74 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62  t time the datab
25c80 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2e 0a 20  ase is opened.. 
25c81 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68   **.  ** Note th
25c82 61 74 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  at it is possibl
25c83 65 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72  e for the librar
25c84 79 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f  y compile-time o
25c85 70 74 69 6f 6e 73 20 74 6f 0a 20 20 2a 2a 20 6f  ptions to.  ** o
25c86 76 65 72 72 69 64 65 20 74 68 69 73 20 73 65 74  verride this set
25c87 74 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ting.  */.  if( 
25c88 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
25c89 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74 6f 72  Left, "temp_stor
25c8a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  e")==0 ){.    if
25c8b 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  ( !zRight ){.   
25c8c 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49     returnSingleI
25c8d 6e 74 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70  nt(pParse, "temp
25c8e 5f 73 74 6f 72 65 22 2c 20 64 62 2d 3e 74 65 6d  _store", db->tem
25c8f 70 5f 73 74 6f 72 65 29 3b 0a 20 20 20 20 7d 65  p_store);.    }e
25c90 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 61 6e 67  lse{.      chang
25c91 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70 50 61  eTempStorage(pPa
25c92 72 73 65 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20  rse, zRight);.  
25c93 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f    }.  }else..  /
25c94 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  *.  **   PRAGMA 
25c95 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
25c96 74 6f 72 79 0a 20 20 2a 2a 20 20 20 50 52 41 47  tory.  **   PRAG
25c97 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  MA temp_store_di
25c98 72 65 63 74 6f 72 79 20 3d 20 22 22 7c 22 64 69  rectory = ""|"di
25c99 72 65 63 74 6f 72 79 5f 6e 61 6d 65 22 0a 20 20  rectory_name".  
25c9a 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f  **.  ** Return o
25c9b 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20  r set the local 
25c9c 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 65 6d  value of the tem
25c9d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72  p_store_director
25c9e 79 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e  y flag.  Changin
25c9f 67 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65  g.  ** the value
25ca0 20 73 65 74 73 20 61 20 73 70 65 63 69 66 69 63   sets a specific
25ca1 20 64 69 72 65 63 74 6f 72 79 20 74 6f 20 62 65   directory to be
25ca2 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72   used for tempor
25ca3 61 72 79 20 66 69 6c 65 73 2e 0a 20 20 2a 2a 20  ary files..  ** 
25ca4 53 65 74 74 69 6e 67 20 74 6f 20 61 20 6e 75 6c  Setting to a nul
25ca5 6c 20 73 74 72 69 6e 67 20 72 65 76 65 72 74 73  l string reverts
25ca6 20 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20   to the default 
25ca7 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74  temporary direct
25ca8 6f 72 79 20 73 65 61 72 63 68 2e 0a 20 20 2a 2a  ory search..  **
25ca9 20 49 66 20 74 65 6d 70 6f 72 61 72 79 20 64 69   If temporary di
25caa 72 65 63 74 6f 72 79 20 69 73 20 63 68 61 6e 67  rectory is chang
25cab 65 64 2c 20 74 68 65 6e 20 69 6e 76 61 6c 69 64  ed, then invalid
25cac 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 2e 0a  ateTempStorage..
25cad 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20 69 66 28 20    **.  */.  if( 
25cae 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
25caf 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74 6f 72  Left, "temp_stor
25cb0 65 5f 64 69 72 65 63 74 6f 72 79 22 29 3d 3d 30  e_directory")==0
25cb1 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52 69   ){.    if( !zRi
25cb2 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ght ){.      if(
25cb3 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
25cb4 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 20  rectory ){.     
25cb5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25cb6 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
25cb7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
25cb8 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
25cb9 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
25cba 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  , .            "
25cbb 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
25cbc 74 6f 72 79 22 2c 20 53 51 4c 49 54 45 5f 53 54  tory", SQLITE_ST
25cbd 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
25cbe 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
25cbf 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
25cc0 30 2c 20 31 2c 20 30 2c 20 73 71 6c 69 74 65 33  0, 1, 0, sqlite3
25cc1 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 2c  _temp_directory,
25cc2 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
25cc3 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25cc4 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
25cc5 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1, 1);.      }. 
25cc6 20 20 20 7d 65 6c 73 65 7b 0a 23 69 66 6e 64 65     }else{.#ifnde
25cc7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53  f SQLITE_OMIT_WS
25cc8 44 0a 20 20 20 20 20 20 69 66 28 20 7a 52 69 67  D.      if( zRig
25cc9 68 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 20  ht[0] ){.       
25cca 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 20   int rc;.       
25ccb 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20   int res;.      
25ccc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
25ccd 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73 2c  Access(db->pVfs,
25cce 20 7a 52 69 67 68 74 2c 20 53 51 4c 49 54 45 5f   zRight, SQLITE_
25ccf 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45  ACCESS_READWRITE
25cd0 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20  , &res);.       
25cd1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
25cd2 4f 4b 20 7c 7c 20 72 65 73 3d 3d 30 20 29 7b 0a  OK || res==0 ){.
25cd3 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25cd4 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
25cd5 2c 20 22 6e 6f 74 20 61 20 77 72 69 74 61 62 6c  , "not a writabl
25cd6 65 20 64 69 72 65 63 74 6f 72 79 22 29 3b 0a 20  e directory");. 
25cd7 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 72           goto pr
25cd8 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20  agma_out;.      
25cd9 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
25cda 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 45 4d    if( SQLITE_TEM
25cdb 50 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20 20 20  P_STORE==0.     
25cdc 20 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54 45 4d    || (SQLITE_TEM
25cdd 50 5f 53 54 4f 52 45 3d 3d 31 20 26 26 20 64 62  P_STORE==1 && db
25cde 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3c 3d 31 29  ->temp_store<=1)
25cdf 0a 20 20 20 20 20 20 20 7c 7c 20 28 53 51 4c 49  .       || (SQLI
25ce0 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32  TE_TEMP_STORE==2
25ce1 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f   && db->temp_sto
25ce2 72 65 3d 3d 31 29 0a 20 20 20 20 20 20 29 7b 0a  re==1).      ){.
25ce3 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61          invalida
25ce4 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70 50  teTempStorage(pP
25ce5 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  arse);.      }. 
25ce6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
25ce7 65 28 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64  e(sqlite3_temp_d
25ce8 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 20  irectory);.     
25ce9 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29   if( zRight[0] )
25cea 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
25ceb 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
25cec 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
25ced 75 70 28 30 2c 20 7a 52 69 67 68 74 29 3b 0a 20  up(0, zRight);. 
25cee 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
25cef 20 20 20 20 73 71 6c 69 74 65 33 5f 74 65 6d 70      sqlite3_temp
25cf0 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a  _directory = 0;.
25cf1 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f        }.#endif /
25cf2 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53  * SQLITE_OMIT_WS
25cf3 44 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  D */.    }.  }el
25cf4 73 65 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  se..#if !defined
25cf5 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c  (SQLITE_ENABLE_L
25cf6 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20  OCKING_STYLE).# 
25cf7 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   if defined(__AP
25cf8 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69  PLE__).#    defi
25cf9 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ne SQLITE_ENABLE
25cfa 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31  _LOCKING_STYLE 1
25cfb 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65  .#  else.#    de
25cfc 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  fine SQLITE_ENAB
25cfd 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
25cfe 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64   0.#  endif.#end
25cff 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  if.#if SQLITE_EN
25d00 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
25d01 4c 45 0a 20 20 2f 2a 0a 20 20 20 2a 2a 20 20 20  LE.  /*.   **   
25d02 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
25d03 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c  .]lock_proxy_fil
25d04 65 0a 20 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  e.   **   PRAGMA
25d05 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b   [database.]lock
25d06 5f 70 72 6f 78 79 5f 66 69 6c 65 20 3d 20 22 3a  _proxy_file = ":
25d07 61 75 74 6f 3a 22 7c 22 6c 6f 63 6b 5f 66 69 6c  auto:"|"lock_fil
25d08 65 5f 70 61 74 68 22 0a 20 20 20 2a 2a 0a 20 20  e_path".   **.  
25d09 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65   ** Return or se
25d0a 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  t the value of t
25d0b 68 65 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69  he lock_proxy_fi
25d0c 6c 65 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69  le flag.  Changi
25d0d 6e 67 0a 20 20 20 2a 2a 20 74 68 65 20 76 61 6c  ng.   ** the val
25d0e 75 65 20 73 65 74 73 20 61 20 73 70 65 63 69 66  ue sets a specif
25d0f 69 63 20 66 69 6c 65 20 74 6f 20 62 65 20 75 73  ic file to be us
25d10 65 64 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  ed for database 
25d11 61 63 63 65 73 73 20 6c 6f 63 6b 73 2e 0a 20 20  access locks..  
25d12 20 2a 2a 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20   **.   */.  if( 
25d13 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
25d14 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 70 72 6f 78  Left, "lock_prox
25d15 79 5f 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0a 20  y_file")==0 ){. 
25d16 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
25d17 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70  {.      Pager *p
25d18 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
25d19 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70  treePager(pDb->p
25d1a 42 74 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20  Bt);.      char 
25d1b 2a 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68  *proxy_file_path
25d1c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 73   = NULL;.      s
25d1d 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
25d1e 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  le = sqlite3Page
25d1f 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a 20  rFile(pPager);. 
25d20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69       sqlite3OsFi
25d21 6c 65 43 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c  leControl(pFile,
25d22 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b   SQLITE_GET_LOCK
25d23 50 52 4f 58 59 46 49 4c 45 2c 20 0a 20 20 20 20  PROXYFILE, .    
25d24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d25 20 20 20 20 20 20 20 26 70 72 6f 78 79 5f 66 69         &proxy_fi
25d26 6c 65 5f 70 61 74 68 29 3b 0a 20 20 20 20 20 20  le_path);.      
25d27 0a 20 20 20 20 20 20 69 66 28 20 70 72 6f 78 79  .      if( proxy
25d28 5f 66 69 6c 65 5f 70 61 74 68 20 29 7b 0a 20 20  _file_path ){.  
25d29 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25d2a 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
25d2b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
25d2c 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
25d2d 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
25d2e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20  AME, .          
25d2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d30 20 20 20 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f      "lock_proxy_
25d31 66 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  file", SQLITE_ST
25d32 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
25d33 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
25d34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
25d35 30 2c 20 31 2c 20 30 2c 20 70 72 6f 78 79 5f 66  0, 1, 0, proxy_f
25d36 69 6c 65 5f 70 61 74 68 2c 20 30 29 3b 0a 20 20  ile_path, 0);.  
25d37 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25d38 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
25d39 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a  sultRow, 1, 1);.
25d3a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
25d3b 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a  e{.      Pager *
25d3c 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
25d3d 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e  BtreePager(pDb->
25d3e 70 42 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  pBt);.      sqli
25d3f 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20  te3_file *pFile 
25d40 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69  = sqlite3PagerFi
25d41 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  le(pPager);.    
25d42 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
25d43 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29   if( zRight[0] )
25d44 7b 0a 20 20 20 20 20 20 20 20 72 65 73 3d 73 71  {.        res=sq
25d45 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
25d46 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45  ol(pFile, SQLITE
25d47 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
25d48 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  LE, .           
25d49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d4a 20 20 20 20 20 20 20 20 20 20 7a 52 69 67 68 74            zRight
25d4b 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20  );.      } else 
25d4c 7b 0a 20 20 20 20 20 20 20 20 72 65 73 3d 73 71  {.        res=sq
25d4d 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
25d4e 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45  ol(pFile, SQLITE
25d4f 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
25d50 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  LE, .           
25d51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d52 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 29 3b            NULL);
25d53 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
25d54 66 28 20 72 65 73 21 3d 53 51 4c 49 54 45 5f 4f  f( res!=SQLITE_O
25d55 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  K ){.        sql
25d56 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
25d57 72 73 65 2c 20 22 66 61 69 6c 65 64 20 74 6f 20  rse, "failed to 
25d58 73 65 74 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66  set lock proxy f
25d59 69 6c 65 22 29 3b 0a 20 20 20 20 20 20 20 20 67  ile");.        g
25d5a 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
25d5b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
25d5c 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
25d5d 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
25d5e 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 20 20  CKING_STYLE */  
25d5f 20 20 20 20 0a 20 20 20 20 0a 20 20 2f 2a 0a 20      .    .  /*. 
25d60 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
25d61 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e  tabase.]synchron
25d62 6f 75 73 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ous.  **   PRAGM
25d63 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e  A [database.]syn
25d64 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c 4f 4e 7c  chronous=OFF|ON|
25d65 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20 20 2a 2a  NORMAL|FULL.  **
25d66 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20  .  ** Return or 
25d67 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61  set the local va
25d68 6c 75 65 20 6f 66 20 74 68 65 20 73 79 6e 63 68  lue of the synch
25d69 72 6f 6e 6f 75 73 20 66 6c 61 67 2e 20 20 43 68  ronous flag.  Ch
25d6a 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  anging.  ** the 
25d6b 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73  local value does
25d6c 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65   not make change
25d6d 73 20 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69  s to the disk fi
25d6e 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20  le and the.  ** 
25d6f 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77 69  default value wi
25d70 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 74  ll be restored t
25d71 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65  he next time the
25d72 20 64 61 74 61 62 61 73 65 20 69 73 0a 20 20 2a   database is.  *
25d73 2a 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20  * opened..  */. 
25d74 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
25d75 43 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e 63 68  Cmp(zLeft,"synch
25d76 72 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b 0a 20  ronous")==0 ){. 
25d77 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
25d78 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
25d79 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
25d7a 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69  ut;.    if( !zRi
25d7b 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ght ){.      ret
25d7c 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
25d7d 72 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e 6f 75  rse, "synchronou
25d7e 73 22 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f  s", pDb->safety_
25d7f 6c 65 76 65 6c 2d 31 29 3b 0a 20 20 20 20 7d 65  level-1);.    }e
25d80 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21  lse{.      if( !
25d81 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
25d82 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
25d83 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
25d84 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  , .            "
25d85 53 61 66 65 74 79 20 6c 65 76 65 6c 20 6d 61 79  Safety level may
25d86 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20   not be changed 
25d87 69 6e 73 69 64 65 20 61 20 74 72 61 6e 73 61 63  inside a transac
25d88 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 7d 65  tion");.      }e
25d89 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 62  lse{.        pDb
25d8a 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d  ->safety_level =
25d8b 20 67 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28   getSafetyLevel(
25d8c 7a 52 69 67 68 74 29 2b 31 3b 0a 20 20 20 20 20  zRight)+1;.     
25d8d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
25d8e 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
25d8f 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
25d90 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
25d91 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41   SQLITE_OMIT_FLA
25d92 47 5f 50 52 41 47 4d 41 53 0a 20 20 69 66 28 20  G_PRAGMAS.  if( 
25d93 66 6c 61 67 50 72 61 67 6d 61 28 70 50 61 72 73  flagPragma(pPars
25d94 65 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74  e, zLeft, zRight
25d95 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  ) ){.    /* The 
25d96 66 6c 61 67 50 72 61 67 6d 61 28 29 20 73 75 62  flagPragma() sub
25d97 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e  routine also gen
25d98 65 72 61 74 65 73 20 61 6e 79 20 6e 65 63 65 73  erates any neces
25d99 73 61 72 79 20 63 6f 64 65 0a 20 20 20 20 2a 2a  sary code.    **
25d9a 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e   there is nothin
25d9b 67 20 6d 6f 72 65 20 74 6f 20 64 6f 20 68 65 72  g more to do her
25d9c 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  e */.  }else.#en
25d9d 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
25d9e 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 20  IT_FLAG_PRAGMAS 
25d9f 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
25da0 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50  TE_OMIT_SCHEMA_P
25da1 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a  RAGMAS.  /*.  **
25da2 20 20 20 50 52 41 47 4d 41 20 74 61 62 6c 65 5f     PRAGMA table_
25da3 69 6e 66 6f 28 3c 74 61 62 6c 65 3e 29 0a 20 20  info(<table>).  
25da4 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 61  **.  ** Return a
25da5 20 73 69 6e 67 6c 65 20 72 6f 77 20 66 6f 72 20   single row for 
25da6 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  each column of t
25da7 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 20  he named table. 
25da8 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20  The columns of. 
25da9 20 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64   ** the returned
25daa 20 64 61 74 61 20 73 65 74 20 61 72 65 3a 0a 20   data set are:. 
25dab 20 2a 2a 0a 20 20 2a 2a 20 63 69 64 3a 20 20 20   **.  ** cid:   
25dac 20 20 20 20 20 43 6f 6c 75 6d 6e 20 69 64 20 28       Column id (
25dad 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 6c 65  numbered from le
25dae 66 74 20 74 6f 20 72 69 67 68 74 2c 20 73 74 61  ft to right, sta
25daf 72 74 69 6e 67 20 61 74 20 30 29 0a 20 20 2a 2a  rting at 0).  **
25db0 20 6e 61 6d 65 3a 20 20 20 20 20 20 20 43 6f 6c   name:       Col
25db1 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 79  umn name.  ** ty
25db2 70 65 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e  pe:       Column
25db3 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
25db4 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e 75 6c 6c 3a  e..  ** notnull:
25db5 20 20 20 20 54 72 75 65 20 69 66 20 27 4e 4f 54      True if 'NOT
25db6 20 4e 55 4c 4c 27 20 69 73 20 70 61 72 74 20 6f   NULL' is part o
25db7 66 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61  f column declara
25db8 74 69 6f 6e 0a 20 20 2a 2a 20 64 66 6c 74 5f 76  tion.  ** dflt_v
25db9 61 6c 75 65 3a 20 54 68 65 20 64 65 66 61 75 6c  alue: The defaul
25dba 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
25dbb 63 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e 79 2e 0a  column, if any..
25dbc 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
25dbd 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
25dbe 20 22 74 61 62 6c 65 5f 69 6e 66 6f 22 29 3d 3d   "table_info")==
25dbf 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20  0 && zRight ){. 
25dc0 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
25dc1 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
25dc2 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
25dc3 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
25dc4 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20 3d 20  out;.    pTab = 
25dc5 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
25dc6 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62  (db, zRight, zDb
25dc7 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 20  );.    if( pTab 
25dc8 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
25dc9 20 20 20 20 20 20 69 6e 74 20 6e 48 69 64 64 65        int nHidde
25dca 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 43 6f 6c  n = 0;.      Col
25dcb 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20  umn *pCol;.     
25dcc 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
25dcd 75 6d 43 6f 6c 73 28 76 2c 20 36 29 3b 0a 20 20  umCols(v, 6);.  
25dce 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
25dcf 20 3d 20 36 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 6;.      sqli
25dd0 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
25dd1 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
25dd2 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c  NAME, "cid", SQL
25dd3 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
25dd4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25dd5 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43  tColName(v, 1, C
25dd6 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61  OLNAME_NAME, "na
25dd7 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  me", SQLITE_STAT
25dd8 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
25dd9 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
25dda 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
25ddb 41 4d 45 2c 20 22 74 79 70 65 22 2c 20 53 51 4c  AME, "type", SQL
25ddc 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
25ddd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25dde 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43  tColName(v, 3, C
25ddf 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f  OLNAME_NAME, "no
25de0 74 6e 75 6c 6c 22 2c 20 53 51 4c 49 54 45 5f 53  tnull", SQLITE_S
25de1 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
25de2 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
25de3 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d  ame(v, 4, COLNAM
25de4 45 5f 4e 41 4d 45 2c 20 22 64 66 6c 74 5f 76 61  E_NAME, "dflt_va
25de5 6c 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  lue", SQLITE_STA
25de6 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
25de7 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
25de8 65 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 5, COLNAME_
25de9 4e 41 4d 45 2c 20 22 70 6b 22 2c 20 53 51 4c 49  NAME, "pk", SQLI
25dea 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
25deb 20 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74    sqlite3ViewGet
25dec 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
25ded 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  se, pTab);.     
25dee 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 70   for(i=0, pCol=p
25def 54 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c 70 54 61  Tab->aCol; i<pTa
25df0 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43  b->nCol; i++, pC
25df1 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  ol++){.        i
25df2 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  f( IsHiddenColum
25df3 6e 28 70 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20  n(pCol) ){.     
25df4 20 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a       nHidden++;.
25df5 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
25df6 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ue;.        }.  
25df7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25df8 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
25df9 74 65 67 65 72 2c 20 69 2d 6e 48 69 64 64 65 6e  teger, i-nHidden
25dfa 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
25dfb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
25dfc 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
25dfd 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 2d 3e 7a 4e  , 2, 0, pCol->zN
25dfe 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
25dff 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25e00 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
25e01 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20  , 0, 3, 0,.     
25e02 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70        pCol->zTyp
25e03 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20  e ? pCol->zType 
25e04 3a 20 22 22 2c 20 30 29 3b 0a 20 20 20 20 20 20  : "", 0);.      
25e05 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25e06 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
25e07 72 2c 20 28 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c  r, (pCol->notNul
25e08 6c 20 3f 20 31 20 3a 20 30 29 2c 20 34 29 3b 0a  l ? 1 : 0), 4);.
25e09 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c          if( pCol
25e0a 2d 3e 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 20  ->pDflt ){.     
25e0b 20 20 20 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e       const Token
25e0c 20 2a 70 20 3d 20 26 70 43 6f 6c 2d 3e 70 44 66   *p = &pCol->pDf
25e0d 6c 74 2d 3e 73 70 61 6e 3b 0a 20 20 20 20 20 20  lt->span;.      
25e0e 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a      assert( p->z
25e0f 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71   );.          sq
25e10 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
25e11 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
25e12 2c 20 35 2c 20 30 2c 20 28 63 68 61 72 2a 29 70  , 5, 0, (char*)p
25e13 2d 3e 7a 2c 20 70 2d 3e 6e 29 3b 0a 20 20 20 20  ->z, p->n);.    
25e14 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
25e15 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25e16 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
25e17 6c 2c 20 30 2c 20 35 29 3b 0a 20 20 20 20 20 20  l, 0, 5);.      
25e18 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
25e19 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
25e1a 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f   OP_Integer, pCo
25e1b 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 2c 20 36 29  l->isPrimKey, 6)
25e1c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
25e1d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25e1e 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
25e1f 36 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  6);.      }.    
25e20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28  }.  }else..  if(
25e21 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
25e22 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e  zLeft, "index_in
25e23 66 6f 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  fo")==0 && zRigh
25e24 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  t ){.    Index *
25e25 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20  pIdx;.    Table 
25e26 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73  *pTab;.    if( s
25e27 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
25e28 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
25e29 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
25e2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69  pIdx = sqlite3Fi
25e2b 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 52 69 67  ndIndex(db, zRig
25e2c 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66  ht, zDb);.    if
25e2d 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20  ( pIdx ){.      
25e2e 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 70 54 61  int i;.      pTa
25e2f 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  b = pIdx->pTable
25e30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25e31 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
25e32 20 33 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73   3);.      pPars
25e33 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20  e->nMem = 3;.   
25e34 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25e35 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
25e36 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65  OLNAME_NAME, "se
25e37 71 6e 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  qno", SQLITE_STA
25e38 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
25e39 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
25e3a 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
25e3b 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c  NAME, "cid", SQL
25e3c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
25e3d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25e3e 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43  tColName(v, 2, C
25e3f 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61  OLNAME_NAME, "na
25e40 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  me", SQLITE_STAT
25e41 49 43 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  IC);.      for(i
25e42 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; i<pIdx->nCol
25e43 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
25e44 20 20 20 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49     int cnum = pI
25e45 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b  dx->aiColumn[i];
25e46 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
25e47 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25e48 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b  _Integer, i, 1);
25e49 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
25e4a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25e4b 5f 49 6e 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20  _Integer, cnum, 
25e4c 32 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  2);.        asse
25e4d 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63  rt( pTab->nCol>c
25e4e 6e 75 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 73  num );.        s
25e4f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
25e50 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
25e51 30 2c 20 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61  0, 3, 0, pTab->a
25e52 43 6f 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c  Col[cnum].zName,
25e53 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
25e54 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25e55 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
25e56 31 2c 20 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1, 3);.      }. 
25e57 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20     }.  }else..  
25e58 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
25e59 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78  mp(zLeft, "index
25e5a 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52  _list")==0 && zR
25e5b 69 67 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65  ight ){.    Inde
25e5c 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62  x *pIdx;.    Tab
25e5d 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66  le *pTab;.    if
25e5e 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
25e5f 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
25e60 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
25e61 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
25e62 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
25e63 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20  Right, zDb);.   
25e64 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
25e65 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
25e66 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
25e67 20 20 20 20 20 70 49 64 78 20 3d 20 70 54 61 62       pIdx = pTab
25e68 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20  ->pIndex;.      
25e69 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20  if( pIdx ){.    
25e6a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a      int i = 0; .
25e6b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
25e6c 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
25e6d 20 33 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61   3);.        pPa
25e6e 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20  rse->nMem = 3;. 
25e6f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
25e70 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
25e71 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
25e72 20 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53   "seq", SQLITE_S
25e73 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
25e74 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
25e75 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
25e76 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
25e77 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
25e78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
25e79 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
25e7a 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
25e7b 4d 45 2c 20 22 75 6e 69 71 75 65 22 2c 20 53 51  ME, "unique", SQ
25e7c 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
25e7d 20 20 20 20 20 20 77 68 69 6c 65 28 70 49 64 78        while(pIdx
25e7e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
25e7f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25e80 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c  , OP_Integer, i,
25e81 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   1);.          s
25e82 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
25e83 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
25e84 30 2c 20 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a  0, 2, 0, pIdx->z
25e85 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
25e86 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25e87 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
25e88 67 65 72 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72  ger, pIdx->onErr
25e89 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b  or!=OE_None, 3);
25e8a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
25e8b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25e8c 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
25e8d 20 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2b   3);.          +
25e8e 2b 69 3b 0a 20 20 20 20 20 20 20 20 20 20 70 49  +i;.          pI
25e8f 64 78 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74  dx = pIdx->pNext
25e90 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
25e91 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
25e92 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  e..  if( sqlite3
25e93 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
25e94 64 61 74 61 62 61 73 65 5f 6c 69 73 74 22 29 3d  database_list")=
25e95 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  =0 ){.    int i;
25e96 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
25e97 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
25e98 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
25e99 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  _out;.    sqlite
25e9a 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
25e9b 76 2c 20 33 29 3b 0a 20 20 20 20 70 50 61 72 73  v, 3);.    pPars
25e9c 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20  e->nMem = 3;.   
25e9d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
25e9e 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
25e9f 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22  NAME_NAME, "seq"
25ea0 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
25ea1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25ea2 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
25ea3 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
25ea4 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53  "name", SQLITE_S
25ea5 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
25ea6 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
25ea7 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 2, COLNAME_
25ea8 4e 41 4d 45 2c 20 22 66 69 6c 65 22 2c 20 53 51  NAME, "file", SQ
25ea9 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
25eaa 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
25eab 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
25eac 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d    if( db->aDb[i]
25ead 2e 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  .pBt==0 ) contin
25eae 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ue;.      assert
25eaf 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61  ( db->aDb[i].zNa
25eb0 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  me!=0 );.      s
25eb1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25eb2 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
25eb3 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  i, 1);.      sql
25eb4 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
25eb5 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
25eb6 20 32 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69   2, 0, db->aDb[i
25eb7 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ].zName, 0);.   
25eb8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25eb9 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
25eba 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20  g8, 0, 3, 0,.   
25ebb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
25ebc 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
25ebd 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c  db->aDb[i].pBt),
25ebe 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
25ebf 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25ec0 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
25ec1 20 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   3);.    }.  }el
25ec2 73 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  se..  if( sqlite
25ec3 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
25ec4 22 63 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22  "collation_list"
25ec5 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
25ec6 69 20 3d 20 30 3b 0a 20 20 20 20 48 61 73 68 45  i = 0;.    HashE
25ec7 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69  lem *p;.    sqli
25ec8 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
25ec9 73 28 76 2c 20 32 29 3b 0a 20 20 20 20 70 50 61  s(v, 2);.    pPa
25eca 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20  rse->nMem = 2;. 
25ecb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25ecc 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
25ecd 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65  OLNAME_NAME, "se
25ece 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  q", SQLITE_STATI
25ecf 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  C);.    sqlite3V
25ed0 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
25ed1 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
25ed2 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45  , "name", SQLITE
25ed3 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f  _STATIC);.    fo
25ed4 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(p=sqliteHashFi
25ed5 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  rst(&db->aCollSe
25ed6 71 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48  q); p; p=sqliteH
25ed7 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20  ashNext(p)){.   
25ed8 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
25ed9 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73  l = (CollSeq *)s
25eda 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29  qliteHashData(p)
25edb 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25edc 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25edd 49 6e 74 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29  Integer, i++, 1)
25ede 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25edf 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
25ee0 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30  String8, 0, 2, 0
25ee1 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20  , pColl->zName, 
25ee2 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
25ee3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25ee4 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
25ee5 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  2);.    }.  }els
25ee6 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
25ee7 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50  TE_OMIT_SCHEMA_P
25ee8 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64  RAGMAS */..#ifnd
25ee9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
25eea 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28  OREIGN_KEY.  if(
25eeb 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
25eec 7a 4c 65 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f  zLeft, "foreign_
25eed 6b 65 79 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26  key_list")==0 &&
25eee 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 46   zRight ){.    F
25eef 4b 65 79 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61  Key *pFK;.    Ta
25ef0 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69  ble *pTab;.    i
25ef1 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
25ef2 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
25ef3 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
25ef4 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74      pTab = sqlit
25ef5 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
25ef6 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20  zRight, zDb);.  
25ef7 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
25ef8 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
25ef9 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
25efa 20 20 20 20 20 20 70 46 4b 20 3d 20 70 54 61 62        pFK = pTab
25efb 2d 3e 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 69  ->pFKey;.      i
25efc 66 28 20 70 46 4b 20 29 7b 0a 20 20 20 20 20 20  f( pFK ){.      
25efd 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20    int i = 0; .  
25efe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25eff 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38  eSetNumCols(v, 8
25f00 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  );.        pPars
25f01 65 2d 3e 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20  e->nMem = 8;.   
25f02 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25f03 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
25f04 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
25f05 69 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  id", SQLITE_STAT
25f06 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  IC);.        sql
25f07 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
25f08 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  me(v, 1, COLNAME
25f09 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51  _NAME, "seq", SQ
25f0a 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
25f0b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25f0c 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32  eSetColName(v, 2
25f0d 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
25f0e 22 74 61 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f  "table", SQLITE_
25f0f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
25f10 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
25f11 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c  olName(v, 3, COL
25f12 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d  NAME_NAME, "from
25f13 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
25f14 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
25f15 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
25f16 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 4, COLNAME_N
25f17 41 4d 45 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54  AME, "to", SQLIT
25f18 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
25f19 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
25f1a 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43  tColName(v, 5, C
25f1b 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e  OLNAME_NAME, "on
25f1c 5f 75 70 64 61 74 65 22 2c 20 53 51 4c 49 54 45  _update", SQLITE
25f1d 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
25f1e 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
25f1f 43 6f 6c 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f  ColName(v, 6, CO
25f20 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f  LNAME_NAME, "on_
25f21 64 65 6c 65 74 65 22 2c 20 53 51 4c 49 54 45 5f  delete", SQLITE_
25f22 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
25f23 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
25f24 6f 6c 4e 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c  olName(v, 7, COL
25f25 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63  NAME_NAME, "matc
25f26 68 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  h", SQLITE_STATI
25f27 43 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  C);.        whil
25f28 65 28 70 46 4b 29 7b 0a 20 20 20 20 20 20 20 20  e(pFK){.        
25f29 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
25f2a 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46     for(j=0; j<pF
25f2b 4b 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20  K->nCol; j++){. 
25f2c 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
25f2d 2a 7a 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f  *zCol = pFK->aCo
25f2e 6c 5b 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20  l[j].zCol;.     
25f2f 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e         char *zOn
25f30 55 70 64 61 74 65 20 3d 20 28 63 68 61 72 20 2a  Update = (char *
25f31 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d  )actionName(pFK-
25f32 3e 75 70 64 61 74 65 43 6f 6e 66 29 3b 0a 20 20  >updateConf);.  
25f33 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
25f34 7a 4f 6e 44 65 6c 65 74 65 20 3d 20 28 63 68 61  zOnDelete = (cha
25f35 72 20 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70  r *)actionName(p
25f36 46 4b 2d 3e 64 65 6c 65 74 65 43 6f 6e 66 29 3b  FK->deleteConf);
25f37 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
25f38 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25f39 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c  , OP_Integer, i,
25f3a 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   1);.           
25f3b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25f3c 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
25f3d 2c 20 6a 2c 20 32 29 3b 0a 20 20 20 20 20 20 20  , j, 2);.       
25f3e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25f3f 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
25f40 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70  ing8, 0, 3, 0, p
25f41 46 4b 2d 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20  FK->zTo, 0);.   
25f42 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25f43 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
25f44 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20  _String8, 0, 4, 
25f45 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
25f46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25f47 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d   pTab->aCol[pFK-
25f48 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e  >aCol[j].iFrom].
25f49 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20  zName, 0);.     
25f4a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
25f4b 62 65 41 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c  beAddOp4(v, zCol
25f4c 20 3f 20 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20   ? OP_String8 : 
25f4d 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30  OP_Null, 0, 5, 0
25f4e 2c 20 7a 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20  , zCol, 0);.    
25f4f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
25f50 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
25f51 53 74 72 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30  String8, 0, 6, 0
25f52 2c 20 7a 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b  , zOnUpdate, 0);
25f53 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
25f54 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
25f55 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
25f56 20 37 2c 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65   7, 0, zOnDelete
25f57 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
25f58 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25f59 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
25f5a 38 2c 20 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e  8, 0, 8, 0, "NON
25f5b 45 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  E", 0);.        
25f5c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25f5d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
25f5e 6c 74 52 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20  ltRow, 1, 8);.  
25f5f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25f60 20 20 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20      ++i;.       
25f61 20 20 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e     pFK = pFK->pN
25f62 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20  extFrom;.       
25f63 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
25f64 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
25f65 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
25f66 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
25f67 4b 45 59 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  KEY) */..#ifndef
25f68 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 73 71   NDEBUG.  if( sq
25f69 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
25f6a 66 74 2c 20 22 70 61 72 73 65 72 5f 74 72 61 63  ft, "parser_trac
25f6b 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  e")==0 ){.    if
25f6c 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ( zRight ){.    
25f6d 20 20 69 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e    if( getBoolean
25f6e 28 7a 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20  (zRight) ){.    
25f6f 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
25f70 72 54 72 61 63 65 28 73 74 64 65 72 72 2c 20 22  rTrace(stderr, "
25f71 70 61 72 73 65 72 3a 20 22 29 3b 0a 20 20 20 20  parser: ");.    
25f72 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
25f73 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
25f74 61 63 65 28 30 2c 20 30 29 3b 0a 20 20 20 20 20  ace(0, 0);.     
25f75 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
25f76 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65  .#endif..  /* Re
25f77 69 6e 73 74 61 6c 6c 20 74 68 65 20 4c 49 4b 45  install the LIKE
25f78 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69   and GLOB functi
25f79 6f 6e 73 2e 20 20 54 68 65 20 76 61 72 69 61 6e  ons.  The varian
25f7a 74 20 6f 66 20 4c 49 4b 45 0a 20 20 2a 2a 20 75  t of LIKE.  ** u
25f7b 73 65 64 20 77 69 6c 6c 20 62 65 20 63 61 73 65  sed will be case
25f7c 20 73 65 6e 73 69 74 69 76 65 20 6f 72 20 6e 6f   sensitive or no
25f7d 74 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  t depending on t
25f7e 68 65 20 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 69  he RHS..  */.  i
25f7f 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
25f80 70 28 7a 4c 65 66 74 2c 20 22 63 61 73 65 5f 73  p(zLeft, "case_s
25f81 65 6e 73 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d  ensitive_like")=
25f82 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 52  =0 ){.    if( zR
25f83 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71  ight ){.      sq
25f84 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b  lite3RegisterLik
25f85 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 67  eFunctions(db, g
25f86 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74  etBoolean(zRight
25f87 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ));.    }.  }els
25f88 65 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e..#ifndef SQLIT
25f89 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  E_INTEGRITY_CHEC
25f8a 4b 5f 45 52 52 4f 52 5f 4d 41 58 0a 23 20 64 65  K_ERROR_MAX.# de
25f8b 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  fine SQLITE_INTE
25f8c 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
25f8d 52 5f 4d 41 58 20 31 30 30 0a 23 65 6e 64 69 66  R_MAX 100.#endif
25f8e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
25f8f 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
25f90 43 48 45 43 4b 0a 20 20 2f 2a 20 50 72 61 67 6d  CHECK.  /* Pragm
25f91 61 20 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 20  a "quick_check" 
25f92 69 73 20 61 6e 20 65 78 70 65 72 69 6d 65 6e 74  is an experiment
25f93 61 6c 20 72 65 64 75 63 65 64 20 76 65 72 73 69  al reduced versi
25f94 6f 6e 20 6f 66 20 0a 20 20 2a 2a 20 69 6e 74 65  on of .  ** inte
25f95 67 72 69 74 79 5f 63 68 65 63 6b 20 64 65 73 69  grity_check desi
25f96 67 6e 65 64 20 74 6f 20 64 65 74 65 63 74 20 6d  gned to detect m
25f97 6f 73 74 20 64 61 74 61 62 61 73 65 20 63 6f 72  ost database cor
25f98 72 75 70 74 69 6f 6e 0a 20 20 2a 2a 20 77 69 74  ruption.  ** wit
25f99 68 6f 75 74 20 6d 6f 73 74 20 6f 66 20 74 68 65  hout most of the
25f9a 20 6f 76 65 72 68 65 61 64 20 6f 66 20 61 20 66   overhead of a f
25f9b 75 6c 6c 20 69 6e 74 65 67 72 69 74 79 2d 63 68  ull integrity-ch
25f9c 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  eck..  */.  if( 
25f9d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
25f9e 4c 65 66 74 2c 20 22 69 6e 74 65 67 72 69 74 79  Left, "integrity
25f9f 5f 63 68 65 63 6b 22 29 3d 3d 30 0a 20 20 20 7c  _check")==0.   |
25fa0 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
25fa1 28 7a 4c 65 66 74 2c 20 22 71 75 69 63 6b 5f 63  (zLeft, "quick_c
25fa2 68 65 63 6b 22 29 3d 3d 30 20 0a 20 20 29 7b 0a  heck")==0 .  ){.
25fa3 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 61 64      int i, j, ad
25fa4 64 72 2c 20 6d 78 45 72 72 3b 0a 0a 20 20 20 20  dr, mxErr;..    
25fa5 2f 2a 20 43 6f 64 65 20 74 68 61 74 20 61 70 70  /* Code that app
25fa6 65 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 20  ears at the end 
25fa7 6f 66 20 74 68 65 20 69 6e 74 65 67 72 69 74 79  of the integrity
25fa8 20 63 68 65 63 6b 2e 20 20 49 66 20 6e 6f 20 65   check.  If no e
25fa9 72 72 6f 72 0a 20 20 20 20 2a 2a 20 6d 65 73 73  rror.    ** mess
25faa 61 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 67  ages have been g
25fab 65 6e 65 72 61 74 65 64 2c 20 6f 75 74 70 75 74  enerated, output
25fac 20 4f 4b 2e 20 20 4f 74 68 65 72 77 69 73 65 20   OK.  Otherwise 
25fad 6f 75 74 70 75 74 20 74 68 65 0a 20 20 20 20 2a  output the.    *
25fae 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a  * error message.
25faf 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69      */.    stati
25fb0 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
25fb1 73 74 20 65 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b  st endCode[] = {
25fb2 0a 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49  .      { OP_AddI
25fb3 6d 6d 2c 20 20 20 20 20 20 31 2c 20 30 2c 20 20  mm,      1, 0,  
25fb4 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20        0},    /* 
25fb5 30 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  0 */.      { OP_
25fb6 49 66 4e 65 67 2c 20 20 20 20 20 20 20 31 2c 20  IfNeg,       1, 
25fb7 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20  0,        0},   
25fb8 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 1 */.      {
25fb9 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
25fba 20 30 2c 20 33 2c 20 20 20 20 20 20 20 20 30 7d   0, 3,        0}
25fbb 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20  ,    /* 2 */.   
25fbc 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f     { OP_ResultRo
25fbd 77 2c 20 20 20 33 2c 20 31 2c 20 20 20 20 20 20  w,   3, 1,      
25fbe 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 0a 20 20    0},.    };..  
25fbf 20 20 69 6e 74 20 69 73 51 75 69 63 6b 20 3d 20    int isQuick = 
25fc0 28 7a 4c 65 66 74 5b 30 5d 3d 3d 27 71 27 29 3b  (zLeft[0]=='q');
25fc1 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c  ..    /* Initial
25fc2 69 7a 65 20 74 68 65 20 56 44 42 45 20 70 72 6f  ize the VDBE pro
25fc3 67 72 61 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20  gram */.    if( 
25fc4 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
25fc5 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
25fc6 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
25fc7 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
25fc8 36 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  6;.    sqlite3Vd
25fc9 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
25fca 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
25fcb 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
25fcc 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
25fcd 2c 20 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65  , "integrity_che
25fce 63 6b 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ck", SQLITE_STAT
25fcf 49 43 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74  IC);..    /* Set
25fd0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 65 72 72   the maximum err
25fd1 6f 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20  or count */.    
25fd2 6d 78 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49  mxErr = SQLITE_I
25fd3 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45  NTEGRITY_CHECK_E
25fd4 52 52 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66  RROR_MAX;.    if
25fd5 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ( zRight ){.    
25fd6 20 20 6d 78 45 72 72 20 3d 20 61 74 6f 69 28 7a    mxErr = atoi(z
25fd7 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66  Right);.      if
25fd8 28 20 6d 78 45 72 72 3c 3d 30 20 29 7b 0a 20 20  ( mxErr<=0 ){.  
25fd9 20 20 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51        mxErr = SQ
25fda 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43  LITE_INTEGRITY_C
25fdb 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a  HECK_ERROR_MAX;.
25fdc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
25fdd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25fde 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
25fdf 72 2c 20 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f  r, mxErr, 1);  /
25fe0 2a 20 72 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65  * reg[1] holds e
25fe1 72 72 6f 72 73 20 6c 65 66 74 20 2a 2f 0a 0a 20  rrors left */.. 
25fe2 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65     /* Do an inte
25fe3 67 72 69 74 79 20 63 68 65 63 6b 20 6f 6e 20 65  grity check on e
25fe4 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c  ach database fil
25fe5 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30  e */.    for(i=0
25fe6 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
25fe7 29 7b 0a 20 20 20 20 20 20 48 61 73 68 45 6c 65  ){.      HashEle
25fe8 6d 20 2a 78 3b 0a 20 20 20 20 20 20 48 61 73 68  m *x;.      Hash
25fe9 20 2a 70 54 62 6c 73 3b 0a 20 20 20 20 20 20 69   *pTbls;.      i
25fea 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 20  nt cnt = 0;..   
25feb 20 20 20 69 66 28 20 4f 4d 49 54 5f 54 45 4d 50     if( OMIT_TEMP
25fec 44 42 20 26 26 20 69 3d 3d 31 20 29 20 63 6f 6e  DB && i==1 ) con
25fed 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 73 71  tinue;..      sq
25fee 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
25fef 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 29  chema(pParse, i)
25ff0 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
25ff1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
25ff2 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29  (v, OP_IfPos, 1)
25ff3 3b 20 2f 2a 20 48 61 6c 74 20 69 66 20 6f 75 74  ; /* Halt if out
25ff4 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20   of errors */.  
25ff5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25ff6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp2(v, OP_Halt
25ff7 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  , 0, 0);.      s
25ff8 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
25ff9 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20  re(v, addr);..  
25ffa 20 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74      /* Do an int
25ffb 65 67 72 69 74 79 20 63 68 65 63 6b 20 6f 66 20  egrity check of 
25ffc 74 68 65 20 42 2d 54 72 65 65 0a 20 20 20 20 20  the B-Tree.     
25ffd 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 42 65 67   **.      ** Beg
25ffe 69 6e 20 62 79 20 66 69 6c 6c 69 6e 67 20 72 65  in by filling re
25fff 67 69 73 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e  gisters 2, 3, ..
26000 2e 20 77 69 74 68 20 74 68 65 20 72 6f 6f 74 20  . with the root 
26001 70 61 67 65 73 20 6e 75 6d 62 65 72 73 0a 20 20  pages numbers.  
26002 20 20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74      ** for all t
26003 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65  ables and indice
26004 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
26005 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
26006 20 20 70 54 62 6c 73 20 3d 20 26 64 62 2d 3e 61    pTbls = &db->a
26007 44 62 5b 69 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  Db[i].pSchema->t
26008 62 6c 48 61 73 68 3b 0a 20 20 20 20 20 20 66 6f  blHash;.      fo
26009 72 28 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(x=sqliteHashFi
2600a 72 73 74 28 70 54 62 6c 73 29 3b 20 78 3b 20 78  rst(pTbls); x; x
2600b 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
2600c 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62  x)){.        Tab
2600d 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74  le *pTab = sqlit
2600e 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20  eHashData(x);.  
2600f 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
26010 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  x;.        sqlit
26011 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26012 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 54 61 62  OP_Integer, pTab
26013 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a  ->tnum, 2+cnt);.
26014 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20          cnt++;. 
26015 20 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d         for(pIdx=
26016 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
26017 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
26018 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20  Next){.         
26019 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2601a 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2601b 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b  , pIdx->tnum, 2+
2601c 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  cnt);.          
2601d 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  cnt++;.        }
2601e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2601f 66 28 20 63 6e 74 3d 3d 30 20 29 20 63 6f 6e 74  f( cnt==0 ) cont
26020 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  inue;..      /* 
26021 4d 61 6b 65 20 73 75 72 65 20 73 75 66 66 69 63  Make sure suffic
26022 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 72  ient number of r
26023 65 67 69 73 74 65 72 73 20 68 61 76 65 20 62 65  egisters have be
26024 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  en allocated */.
26025 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65        if( pParse
26026 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74 2b 34 20 29  ->nMem < cnt+4 )
26027 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  {.        pParse
26028 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74 2b 34 3b 0a  ->nMem = cnt+4;.
26029 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
2602a 2a 20 44 6f 20 74 68 65 20 62 2d 74 72 65 65 20  * Do the b-tree 
2602b 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 73  integrity checks
2602c 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2602d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2602e 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 2c 20 32  P_IntegrityCk, 2
2602f 2c 20 63 6e 74 2c 20 31 29 3b 0a 20 20 20 20 20  , cnt, 1);.     
26030 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
26031 67 65 50 35 28 76 2c 20 28 75 38 29 69 29 3b 0a  geP5(v, (u8)i);.
26032 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
26033 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
26034 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32 29 3b  , OP_IsNull, 2);
26035 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26036 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
26037 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c  tring8, 0, 3, 0,
26038 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
26039 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2a 2a  3MPrintf(db, "**
2603a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 25 73  * in database %s
2603b 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61 44 62   ***\n", db->aDb
2603c 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20  [i].zName),.    
2603d 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49 43 29       P4_DYNAMIC)
2603e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2603f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26040 4d 6f 76 65 2c 20 32 2c 20 34 2c 20 31 29 3b 0a  Move, 2, 4, 1);.
26041 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26042 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
26043 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32 29 3b 0a  ncat, 4, 3, 2);.
26044 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26045 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
26046 73 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29 3b 0a  sultRow, 2, 1);.
26047 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26048 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
26049 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61  r);..      /* Ma
2604a 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20  ke sure all the 
2604b 69 6e 64 69 63 65 73 20 61 72 65 20 63 6f 6e 73  indices are cons
2604c 74 72 75 63 74 65 64 20 63 6f 72 72 65 63 74 6c  tructed correctl
2604d 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  y..      */.    
2604e 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61    for(x=sqliteHa
2604f 73 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20  shFirst(pTbls); 
26050 78 20 26 26 20 21 69 73 51 75 69 63 6b 3b 20 78  x && !isQuick; x
26051 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
26052 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62  x)){.        Tab
26053 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74  le *pTab = sqlit
26054 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20  eHashData(x);.  
26055 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
26056 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c  x;.        int l
26057 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20 20 20  oopTop;..       
26058 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65   if( pTab->pInde
26059 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  x==0 ) continue;
2605a 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20  .        addr = 
2605b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2605c 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31  1(v, OP_IfPos, 1
2605d 29 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66 20 6f  );  /* Stop if o
2605e 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a  ut of errors */.
2605f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
26060 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26061 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Halt, 0, 0);.   
26062 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26063 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
26064 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
26065 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e  e3OpenTableAndIn
26066 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70 54  dices(pParse, pT
26067 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e 52 65  ab, 1, OP_OpenRe
26068 61 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ad);.        sql
26069 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2606a 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2606b 20 32 29 3b 20 20 2f 2a 20 72 65 67 28 32 29 20   2);  /* reg(2) 
2606c 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74 72 69  will count entri
2606d 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 6f  es */.        lo
2606e 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  opTop = sqlite3V
2606f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26070 52 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b 0a 20  Rewind, 1, 0);. 
26071 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26072 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
26073 64 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20 20 20  ddImm, 2, 1);   
26074 2f 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65 6e 74  /* increment ent
26075 72 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20  ry count */.    
26076 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64      for(j=0, pId
26077 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
26078 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
26079 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20  >pNext, j++){.  
2607a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6d 70 32          int jmp2
2607b 3b 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74  ;.          stat
2607c 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
2607d 69 73 74 20 69 64 78 45 72 72 5b 5d 20 3d 20 7b  ist idxErr[] = {
2607e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
2607f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 31  P_AddImm,      1
26080 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  , -1,  0},.     
26081 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69         { OP_Stri
26082 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 33 2c 20  ng8,     0,  3, 
26083 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a   0},    /* 1 */.
26084 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
26085 5f 52 6f 77 69 64 2c 20 20 20 20 20 20 20 31 2c  _Rowid,       1,
26086 20 20 34 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    4,  0},.      
26087 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
26088 67 38 2c 20 20 20 20 20 30 2c 20 20 35 2c 20 20  g8,     0,  5,  
26089 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20  0},    /* 3 */. 
2608a 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2608b 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20  String8,     0, 
2608c 20 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34   6,  0},    /* 4
2608d 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
2608e 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20  { OP_Concat,    
2608f 20 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20    4,  3,  3},.  
26090 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43            { OP_C
26091 6f 6e 63 61 74 2c 20 20 20 20 20 20 35 2c 20 20  oncat,      5,  
26092 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20  3,  3},.        
26093 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c      { OP_Concat,
26094 20 20 20 20 20 20 36 2c 20 20 33 2c 20 20 33 7d        6,  3,  3}
26095 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
26096 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20  OP_ResultRow,   
26097 33 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20  3,  1,  0},.    
26098 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50          { OP_IfP
26099 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 20 30 2c  os,       1,  0,
2609a 20 20 30 7d 2c 20 20 20 20 2f 2a 20 39 20 2a 2f    0},    /* 9 */
2609b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
2609c 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 30  P_Halt,        0
2609d 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  0,  0},.     
2609e 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20       };.        
2609f 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74    sqlite3Generat
260a0 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65  eIndexKey(pParse
260a1 2c 20 70 49 64 78 2c 20 31 2c 20 33 2c 20 31 29  , pIdx, 1, 3, 1)
260a2 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 6d 70 32  ;.          jmp2
260a3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
260a4 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64  dOp3(v, OP_Found
260a5 2c 20 6a 2b 32 2c 20 30 2c 20 33 29 3b 0a 20 20  , j+2, 0, 3);.  
260a6 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73          addr = s
260a7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
260a8 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
260a9 28 69 64 78 45 72 72 29 2c 20 69 64 78 45 72 72  (idxErr), idxErr
260aa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
260ab 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
260ac 28 76 2c 20 61 64 64 72 2b 31 2c 20 22 72 6f 77  (v, addr+1, "row
260ad 69 64 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29  id ", P4_STATIC)
260ae 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
260af 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
260b0 76 2c 20 61 64 64 72 2b 33 2c 20 22 20 6d 69 73  v, addr+3, " mis
260b1 73 69 6e 67 20 66 72 6f 6d 20 69 6e 64 65 78 20  sing from index 
260b2 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
260b3 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
260b4 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
260b5 61 64 64 72 2b 34 2c 20 70 49 64 78 2d 3e 7a 4e  addr+4, pIdx->zN
260b6 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ame, P4_STATIC);
260b7 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
260b8 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
260b9 2c 20 61 64 64 72 2b 39 29 3b 0a 20 20 20 20 20  , addr+9);.     
260ba 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
260bb 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 6d 70 32  JumpHere(v, jmp2
260bc 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
260bd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
260be 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
260bf 74 2c 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29  t, 1, loopTop+1)
260c0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
260c1 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
260c2 20 6c 6f 6f 70 54 6f 70 29 3b 0a 20 20 20 20 20   loopTop);.     
260c3 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78     for(j=0, pIdx
260c4 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
260c5 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
260c6 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20  pNext, j++){.   
260c7 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f         static co
260c8 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 63  nst VdbeOpList c
260c9 6e 74 49 64 78 5b 5d 20 3d 20 7b 0a 20 20 20 20  ntIdx[] = {.    
260ca 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e           { OP_In
260cb 74 65 67 65 72 2c 20 20 20 20 20 20 30 2c 20 20  teger,      0,  
260cc 33 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20  3,  0},.        
260cd 20 20 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64       { OP_Rewind
260ce 2c 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20  ,       0,  0,  
260cf 30 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20  0},  /* 1 */.   
260d0 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41            { OP_A
260d1 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 33 2c 20  ddImm,       3, 
260d2 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20   1,  0},.       
260d3 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c        { OP_Next,
260d4 20 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20           0,  0, 
260d5 20 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a 20 20   0},  /* 3 */.  
260d6 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
260d7 45 71 2c 20 20 20 20 20 20 20 20 20 20 20 32 2c  Eq,           2,
260d8 20 20 30 2c 20 20 33 7d 2c 20 20 2f 2a 20 34 20    0,  3},  /* 4 
260d9 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
260da 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20  { OP_AddImm,    
260db 20 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20     1, -1,  0},. 
260dc 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
260dd 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30  _String8,      0
260de 2c 20 20 32 2c 20 20 30 7d 2c 20 20 2f 2a 20 36  ,  2,  0},  /* 6
260df 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
260e0 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
260e1 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20      0,  3,  0}, 
260e2 20 2f 2a 20 37 20 2a 2f 0a 20 20 20 20 20 20 20   /* 7 */.       
260e3 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61        { OP_Conca
260e4 74 2c 20 20 20 20 20 20 20 33 2c 20 20 32 2c 20  t,       3,  2, 
260e5 20 32 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   2},.           
260e6 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77    { OP_ResultRow
260e7 2c 20 20 20 20 32 2c 20 20 31 2c 20 20 30 7d 2c  ,    2,  1,  0},
260e8 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20  .          };.  
260e9 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
260ea 2d 3e 74 6e 75 6d 3d 3d 30 20 29 20 63 6f 6e 74  ->tnum==0 ) cont
260eb 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  inue;.          
260ec 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
260ed 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
260ee 66 50 6f 73 2c 20 31 29 3b 0a 20 20 20 20 20 20  fPos, 1);.      
260ef 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
260f0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp2(v, OP_Halt
260f1 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
260f2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
260f3 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
260f4 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20  .          addr 
260f5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
260f6 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
260f7 69 7a 65 28 63 6e 74 49 64 78 29 2c 20 63 6e 74  ize(cntIdx), cnt
260f8 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Idx);.          
260f9 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
260fa 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 6a  eP1(v, addr+1, j
260fb 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +2);.          s
260fc 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
260fd 50 32 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 64  P2(v, addr+1, ad
260fe 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20  dr+4);.         
260ff 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
26100 67 65 50 31 28 76 2c 20 61 64 64 72 2b 33 2c 20  geP1(v, addr+3, 
26101 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  j+2);.          
26102 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
26103 65 50 32 28 76 2c 20 61 64 64 72 2b 33 2c 20 61  eP2(v, addr+3, a
26104 64 64 72 2b 32 29 3b 0a 20 20 20 20 20 20 20 20  ddr+2);.        
26105 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
26106 70 48 65 72 65 28 76 2c 20 61 64 64 72 2b 34 29  pHere(v, addr+4)
26107 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
26108 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
26109 76 2c 20 61 64 64 72 2b 36 2c 20 0a 20 20 20 20  v, addr+6, .    
2610a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2610b 20 22 77 72 6f 6e 67 20 23 20 6f 66 20 65 6e 74   "wrong # of ent
2610c 72 69 65 73 20 69 6e 20 69 6e 64 65 78 20 22 2c  ries in index ",
2610d 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
2610e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2610f 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64  beChangeP4(v, ad
26110 64 72 2b 37 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  dr+7, pIdx->zNam
26111 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
26112 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
26113 20 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72   .    }.    addr
26114 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
26115 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79  dOpList(v, Array
26116 53 69 7a 65 28 65 6e 64 43 6f 64 65 29 2c 20 65  Size(endCode), e
26117 6e 64 43 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c  ndCode);.    sql
26118 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
26119 28 76 2c 20 61 64 64 72 2c 20 2d 6d 78 45 72 72  (v, addr, -mxErr
2611a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2611b 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2611c 64 72 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr+1);.    sqlit
2611d 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2611e 2c 20 61 64 64 72 2b 32 2c 20 22 6f 6b 22 2c 20  , addr+2, "ok", 
2611f 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 65  P4_STATIC);.  }e
26120 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
26121 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
26122 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69  ITY_CHECK */..#i
26123 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
26124 54 5f 55 54 46 31 36 0a 20 20 2f 2a 0a 20 20 2a  T_UTF16.  /*.  *
26125 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64  *   PRAGMA encod
26126 69 6e 67 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ing.  **   PRAGM
26127 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20 22 75 74  A encoding = "ut
26128 66 2d 38 22 7c 22 75 74 66 2d 31 36 22 7c 22 75  f-8"|"utf-16"|"u
26129 74 66 2d 31 36 6c 65 22 7c 22 75 74 66 2d 31 36  tf-16le"|"utf-16
2612a 62 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e  be".  **.  ** In
2612b 20 69 74 73 20 66 69 72 73 74 20 66 6f 72 6d 2c   its first form,
2612c 20 74 68 69 73 20 70 72 61 67 6d 61 20 72 65 74   this pragma ret
2612d 75 72 6e 73 20 74 68 65 20 65 6e 63 6f 64 69 6e  urns the encodin
2612e 67 20 6f 66 20 74 68 65 20 6d 61 69 6e 0a 20 20  g of the main.  
2612f 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 49 66 20  ** database. If 
26130 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
26131 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c  not initialized,
26132 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a   it is initializ
26133 65 64 20 6e 6f 77 2e 0a 20 20 2a 2a 0a 20 20 2a  ed now..  **.  *
26134 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72  * The second for
26135 6d 20 6f 66 20 74 68 69 73 20 70 72 61 67 6d 61  m of this pragma
26136 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74   is a no-op if t
26137 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
26138 20 66 69 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6e   file.  ** has n
26139 6f 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ot already been 
2613a 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 6e 20  initialized. In 
2613b 74 68 69 73 20 63 61 73 65 20 69 74 20 73 65 74  this case it set
2613c 73 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20  s the default.  
2613d 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 74  ** encoding that
2613e 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f   will be used fo
2613f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
26140 61 73 65 20 66 69 6c 65 20 69 66 20 61 20 6e 65  ase file if a ne
26141 77 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20 63  w file.  ** is c
26142 72 65 61 74 65 64 2e 20 49 66 20 61 6e 20 65 78  reated. If an ex
26143 69 73 74 69 6e 67 20 6d 61 69 6e 20 64 61 74 61  isting main data
26144 62 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65  base file is ope
26145 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 20 20  ned, then the.  
26146 2a 2a 20 64 65 66 61 75 6c 74 20 74 65 78 74 20  ** default text 
26147 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65  encoding for the
26148 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61   existing databa
26149 73 65 20 69 73 20 75 73 65 64 2e 0a 20 20 2a 2a  se is used..  **
2614a 20 0a 20 20 2a 2a 20 49 6e 20 61 6c 6c 20 63 61   .  ** In all ca
2614b 73 65 73 20 6e 65 77 20 64 61 74 61 62 61 73 65  ses new database
2614c 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
2614d 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61  the ATTACH comma
2614e 6e 64 20 61 72 65 0a 20 20 2a 2a 20 63 72 65 61  nd are.  ** crea
2614f 74 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 73  ted to use the s
26150 61 6d 65 20 64 65 66 61 75 6c 74 20 74 65 78 74  ame default text
26151 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 65   encoding as the
26152 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20   main database. 
26153 49 66 0a 20 20 2a 2a 20 74 68 65 20 6d 61 69 6e  If.  ** the main
26154 20 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f   database has no
26155 74 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  t been initializ
26156 65 64 20 61 6e 64 2f 6f 72 20 63 72 65 61 74 65  ed and/or create
26157 64 20 77 68 65 6e 20 41 54 54 41 43 48 0a 20 20  d when ATTACH.  
26158 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2c 20  ** is executed, 
26159 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66  this is done bef
2615a 6f 72 65 20 74 68 65 20 41 54 54 41 43 48 20 6f  ore the ATTACH o
2615b 70 65 72 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20  peration..  **. 
2615c 20 2a 2a 20 49 6e 20 74 68 65 20 73 65 63 6f 6e   ** In the secon
2615d 64 20 66 6f 72 6d 20 74 68 69 73 20 70 72 61 67  d form this prag
2615e 6d 61 20 73 65 74 73 20 74 68 65 20 74 65 78 74  ma sets the text
2615f 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20   encoding to be 
26160 75 73 65 64 20 69 6e 0a 20 20 2a 2a 20 6e 65 77  used in.  ** new
26161 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
26162 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68  created using th
26163 69 73 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  is database hand
26164 6c 65 2e 20 49 74 20 69 73 20 6f 6e 6c 79 0a 20  le. It is only. 
26165 20 2a 2a 20 75 73 65 66 75 6c 20 69 66 20 69 6e   ** useful if in
26166 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  voked immediatel
26167 79 20 61 66 74 65 72 20 74 68 65 20 6d 61 69 6e  y after the main
26168 20 64 61 74 61 62 61 73 65 20 69 0a 20 20 2a 2f   database i.  */
26169 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2616a 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 65 6e  rICmp(zLeft, "en
2616b 63 6f 64 69 6e 67 22 29 3d 3d 30 20 29 7b 0a 20  coding")==0 ){. 
2616c 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
2616d 73 74 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 7b  struct EncName {
2616e 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61  .      char *zNa
2616f 6d 65 3b 0a 20 20 20 20 20 20 75 38 20 65 6e 63  me;.      u8 enc
26170 3b 0a 20 20 20 20 7d 20 65 6e 63 6e 61 6d 65 73  ;.    } encnames
26171 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 22  [] = {.      { "
26172 55 54 46 38 22 2c 20 20 20 20 20 53 51 4c 49 54  UTF8",     SQLIT
26173 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 7d 2c  E_UTF8        },
26174 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 38 22  .      { "UTF-8"
26175 2c 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38  ,    SQLITE_UTF8
26176 20 20 20 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d          },  /* M
26177 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b  ust be element [
26178 31 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55  1] */.      { "U
26179 54 46 2d 31 36 6c 65 22 2c 20 53 51 4c 49 54 45  TF-16le", SQLITE
2617a 5f 55 54 46 31 36 4c 45 20 20 20 20 20 7d 2c 20  _UTF16LE     }, 
2617b 20 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d   /* Must be elem
2617c 65 6e 74 20 5b 32 5d 20 2a 2f 0a 20 20 20 20 20  ent [2] */.     
2617d 20 7b 20 22 55 54 46 2d 31 36 62 65 22 2c 20 53   { "UTF-16be", S
2617e 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20  QLITE_UTF16BE   
2617f 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65    },  /* Must be
26180 20 65 6c 65 6d 65 6e 74 20 5b 33 5d 20 2a 2f 0a   element [3] */.
26181 20 20 20 20 20 20 7b 20 22 55 54 46 31 36 6c 65        { "UTF16le
26182 22 2c 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ",  SQLITE_UTF16
26183 4c 45 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20  LE     },.      
26184 7b 20 22 55 54 46 31 36 62 65 22 2c 20 20 53 51  { "UTF16be",  SQ
26185 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20  LITE_UTF16BE    
26186 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46   },.      { "UTF
26187 2d 31 36 22 2c 20 20 20 30 20 20 20 20 20 20 20  -16",   0       
26188 20 20 20 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a             }, /*
26189 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
2618a 49 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22  IVE */.      { "
2618b 55 54 46 31 36 22 2c 20 20 20 20 30 20 20 20 20  UTF16",    0    
2618c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c                },
2618d 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36   /* SQLITE_UTF16
2618e 4e 41 54 49 56 45 20 2a 2f 0a 20 20 20 20 20 20  NATIVE */.      
2618f 7b 20 30 2c 20 30 20 7d 0a 20 20 20 20 7d 3b 0a  { 0, 0 }.    };.
26190 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74      const struct
26191 20 45 6e 63 4e 61 6d 65 20 2a 70 45 6e 63 3b 0a   EncName *pEnc;.
26192 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
26193 29 7b 20 20 20 20 2f 2a 20 22 50 52 41 47 4d 41  ){    /* "PRAGMA
26194 20 65 6e 63 6f 64 69 6e 67 22 20 2a 2f 0a 20 20   encoding" */.  
26195 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
26196 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
26197 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
26198 6f 75 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  out;.      sqlit
26199 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
2619a 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  (v, 1);.      sq
2619b 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2619c 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
2619d 45 5f 4e 41 4d 45 2c 20 22 65 6e 63 6f 64 69 6e  E_NAME, "encodin
2619e 67 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  g", SQLITE_STATI
2619f 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
261a0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
261a1 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 29  P_String8, 0, 1)
261a2 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
261a3 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f  encnames[SQLITE_
261a4 55 54 46 38 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54  UTF8].enc==SQLIT
261a5 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 20 20  E_UTF8 );.      
261a6 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65 73  assert( encnames
261a7 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d  [SQLITE_UTF16LE]
261a8 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  .enc==SQLITE_UTF
261a9 31 36 4c 45 20 29 3b 0a 20 20 20 20 20 20 61 73  16LE );.      as
261aa 73 65 72 74 28 20 65 6e 63 6e 61 6d 65 73 5b 53  sert( encnames[S
261ab 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2e 65  QLITE_UTF16BE].e
261ac 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
261ad 42 45 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  BE );.      sqli
261ae 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
261af 76 2c 20 2d 31 2c 20 65 6e 63 6e 61 6d 65 73 5b  v, -1, encnames[
261b0 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 5d  ENC(pParse->db)]
261b1 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49  .zName, P4_STATI
261b2 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
261b3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
261b4 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
261b5 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 20 20  1);.    }else{  
261b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
261b7 20 20 20 20 20 20 2f 2a 20 22 50 52 41 47 4d 41        /* "PRAGMA
261b8 20 65 6e 63 6f 64 69 6e 67 20 3d 20 58 58 58 22   encoding = XXX"
261b9 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c   */.      /* Onl
261ba 79 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  y change the val
261bb 75 65 20 6f 66 20 73 71 6c 69 74 65 2e 65 6e 63  ue of sqlite.enc
261bc 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
261bd 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 0a 20   handle is not. 
261be 20 20 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69       ** initiali
261bf 7a 65 64 2e 20 49 66 20 74 68 65 20 6d 61 69 6e  zed. If the main
261c0 20 64 61 74 61 62 61 73 65 20 65 78 69 73 74 73   database exists
261c1 2c 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65  , the new sqlite
261c2 2e 65 6e 63 20 76 61 6c 75 65 0a 20 20 20 20 20  .enc value.     
261c3 20 2a 2a 20 77 69 6c 6c 20 62 65 20 6f 76 65 72   ** will be over
261c4 77 72 69 74 74 65 6e 20 77 68 65 6e 20 74 68 65  written when the
261c5 20 73 63 68 65 6d 61 20 69 73 20 6e 65 78 74 20   schema is next 
261c6 6c 6f 61 64 65 64 2e 20 49 66 20 69 74 20 64 6f  loaded. If it do
261c7 65 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20  es not.      ** 
261c8 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c 20  already exists, 
261c9 69 74 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74  it will be creat
261ca 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 6e 65  ed to use the ne
261cb 77 20 65 6e 63 6f 64 69 6e 67 20 76 61 6c 75 65  w encoding value
261cc 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
261cd 20 69 66 28 20 0a 20 20 20 20 20 20 20 20 21 28   if( .        !(
261ce 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62  DbHasProperty(db
261cf 2c 20 30 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f  , 0, DB_SchemaLo
261d0 61 64 65 64 29 29 20 7c 7c 20 0a 20 20 20 20 20  aded)) || .     
261d1 20 20 20 44 62 48 61 73 50 72 6f 70 65 72 74 79     DbHasProperty
261d2 28 64 62 2c 20 30 2c 20 44 42 5f 45 6d 70 74 79  (db, 0, DB_Empty
261d3 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  ) .      ){.    
261d4 20 20 20 20 66 6f 72 28 70 45 6e 63 3d 26 65 6e      for(pEnc=&en
261d5 63 6e 61 6d 65 73 5b 30 5d 3b 20 70 45 6e 63 2d  cnames[0]; pEnc-
261d6 3e 7a 4e 61 6d 65 3b 20 70 45 6e 63 2b 2b 29 7b  >zName; pEnc++){
261d7 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 30  .          if( 0
261d8 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
261d9 28 7a 52 69 67 68 74 2c 20 70 45 6e 63 2d 3e 7a  (zRight, pEnc->z
261da 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Name) ){.       
261db 20 20 20 20 20 45 4e 43 28 70 50 61 72 73 65 2d       ENC(pParse-
261dc 3e 64 62 29 20 3d 20 70 45 6e 63 2d 3e 65 6e 63  >db) = pEnc->enc
261dd 20 3f 20 70 45 6e 63 2d 3e 65 6e 63 20 3a 20 53   ? pEnc->enc : S
261de 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
261df 45 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  E;.            b
261e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
261e1 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
261e2 20 20 20 20 69 66 28 20 21 70 45 6e 63 2d 3e 7a      if( !pEnc->z
261e3 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Name ){.        
261e4 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
261e5 67 28 70 50 61 72 73 65 2c 20 22 75 6e 73 75 70  g(pParse, "unsup
261e6 70 6f 72 74 65 64 20 65 6e 63 6f 64 69 6e 67 3a  ported encoding:
261e7 20 25 73 22 2c 20 7a 52 69 67 68 74 29 3b 0a 20   %s", zRight);. 
261e8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
261e9 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
261ea 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
261eb 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 23  OMIT_UTF16 */..#
261ec 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
261ed 49 54 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f  IT_SCHEMA_VERSIO
261ee 4e 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20  N_PRAGMAS.  /*. 
261ef 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
261f0 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61 5f 76  tabase.]schema_v
261f1 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 50 52  ersion.  **   PR
261f2 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
261f3 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 20 3d  schema_version =
261f4 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a 2a 0a   <integer>.  **.
261f5 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
261f6 61 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76 65  atabase.]user_ve
261f7 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 50 52 41  rsion.  **   PRA
261f8 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 75  GMA [database.]u
261f9 73 65 72 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69  ser_version = <i
261fa 6e 74 65 67 65 72 3e 0a 20 20 2a 2a 0a 20 20 2a  nteger>.  **.  *
261fb 2a 20 54 68 65 20 70 72 61 67 6d 61 27 73 20 73  * The pragma's s
261fc 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 20 61 6e  chema_version an
261fd 64 20 75 73 65 72 5f 76 65 72 73 69 6f 6e 20 61  d user_version a
261fe 72 65 20 75 73 65 64 20 74 6f 20 73 65 74 20 6f  re used to set o
261ff 72 20 67 65 74 0a 20 20 2a 2a 20 74 68 65 20 76  r get.  ** the v
26200 61 6c 75 65 20 6f 66 20 74 68 65 20 73 63 68 65  alue of the sche
26201 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20 75  ma-version and u
26202 73 65 72 2d 76 65 72 73 69 6f 6e 2c 20 72 65 73  ser-version, res
26203 70 65 63 74 69 76 65 6c 79 2e 20 42 6f 74 68 0a  pectively. Both.
26204 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 2d    ** the schema-
26205 76 65 72 73 69 6f 6e 20 61 6e 64 20 74 68 65 20  version and the 
26206 75 73 65 72 2d 76 65 72 73 69 6f 6e 20 61 72 65  user-version are
26207 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69   32-bit signed i
26208 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 73 74 6f  ntegers.  ** sto
26209 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  red in the datab
2620a 61 73 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a  ase header..  **
2620b 0a 20 20 2a 2a 20 54 68 65 20 73 63 68 65 6d 61  .  ** The schema
2620c 2d 63 6f 6f 6b 69 65 20 69 73 20 75 73 75 61 6c  -cookie is usual
2620d 6c 79 20 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c 61  ly only manipula
2620e 74 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62  ted internally b
2620f 79 20 53 51 4c 69 74 65 2e 20 49 74 0a 20 20 2a  y SQLite. It.  *
26210 2a 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  * is incremented
26211 20 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e 65   by SQLite whene
26212 76 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65  ver the database
26213 20 73 63 68 65 6d 61 20 69 73 20 6d 6f 64 69 66   schema is modif
26214 69 65 64 20 28 62 79 0a 20 20 2a 2a 20 63 72 65  ied (by.  ** cre
26215 61 74 69 6e 67 20 6f 72 20 64 72 6f 70 70 69 6e  ating or droppin
26216 67 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  g a table or ind
26217 65 78 29 2e 20 54 68 65 20 73 63 68 65 6d 61 20  ex). The schema 
26218 76 65 72 73 69 6f 6e 20 69 73 20 75 73 65 64 20  version is used 
26219 62 79 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 65  by.  ** SQLite e
2621a 61 63 68 20 74 69 6d 65 20 61 20 71 75 65 72 79  ach time a query
2621b 20 69 73 20 65 78 65 63 75 74 65 64 20 74 6f 20   is executed to 
2621c 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20  ensure that the 
2621d 69 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 0a 20  internal cache. 
2621e 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 68 65 6d   ** of the schem
2621f 61 20 75 73 65 64 20 77 68 65 6e 20 63 6f 6d 70  a used when comp
26220 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20 71 75  iling the SQL qu
26221 65 72 79 20 6d 61 74 63 68 65 73 20 74 68 65 20  ery matches the 
26222 73 63 68 65 6d 61 20 6f 66 0a 20 20 2a 2a 20 74  schema of.  ** t
26223 68 65 20 64 61 74 61 62 61 73 65 20 61 67 61 69  he database agai
26224 6e 73 74 20 77 68 69 63 68 20 74 68 65 20 63 6f  nst which the co
26225 6d 70 69 6c 65 64 20 71 75 65 72 79 20 69 73 20  mpiled query is 
26226 61 63 74 75 61 6c 6c 79 20 65 78 65 63 75 74 65  actually execute
26227 64 2e 0a 20 20 2a 2a 20 53 75 62 76 65 72 74 69  d..  ** Subverti
26228 6e 67 20 74 68 69 73 20 6d 65 63 68 61 6e 69 73  ng this mechanis
26229 6d 20 62 79 20 75 73 69 6e 67 20 22 50 52 41 47  m by using "PRAG
2622a 4d 41 20 73 63 68 65 6d 61 5f 76 65 72 73 69 6f  MA schema_versio
2622b 6e 22 20 74 6f 20 6d 6f 64 69 66 79 0a 20 20 2a  n" to modify.  *
2622c 2a 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72  * the schema-ver
2622d 73 69 6f 6e 20 69 73 20 70 6f 74 65 6e 74 69 61  sion is potentia
2622e 6c 6c 79 20 64 61 6e 67 65 72 6f 75 73 20 61 6e  lly dangerous an
2622f 64 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 70 72  d may lead to pr
26230 6f 67 72 61 6d 0a 20 20 2a 2a 20 63 72 61 73 68  ogram.  ** crash
26231 65 73 20 6f 72 20 64 61 74 61 62 61 73 65 20 63  es or database c
26232 6f 72 72 75 70 74 69 6f 6e 2e 20 55 73 65 20 77  orruption. Use w
26233 69 74 68 20 63 61 75 74 69 6f 6e 21 0a 20 20 2a  ith caution!.  *
26234 2a 0a 20 20 2a 2a 20 54 68 65 20 75 73 65 72 2d  *.  ** The user-
26235 76 65 72 73 69 6f 6e 20 69 73 20 6e 6f 74 20 75  version is not u
26236 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62  sed internally b
26237 79 20 53 51 4c 69 74 65 2e 20 49 74 20 6d 61 79  y SQLite. It may
26238 20 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a   be used by.  **
26239 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 66 6f   applications fo
2623a 72 20 61 6e 79 20 70 75 72 70 6f 73 65 2e 0a 20  r any purpose.. 
2623b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2623c 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
2623d 22 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 22  "schema_version"
2623e 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71 6c 69  )==0 .   || sqli
2623f 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
26240 2c 20 22 75 73 65 72 5f 76 65 72 73 69 6f 6e 22  , "user_version"
26241 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71 6c 69  )==0 .   || sqli
26242 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
26243 2c 20 22 66 72 65 65 6c 69 73 74 5f 63 6f 75 6e  , "freelist_coun
26244 74 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20 20 20  t")==0 .  ){.   
26245 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 20 20 20   int iCookie;   
26246 2f 2a 20 43 6f 6f 6b 69 65 20 69 6e 64 65 78 2e  /* Cookie index.
26247 20 30 20 66 6f 72 20 73 63 68 65 6d 61 2d 63 6f   0 for schema-co
26248 6f 6b 69 65 2c 20 36 20 66 6f 72 20 75 73 65 72  okie, 6 for user
26249 2d 63 6f 6f 6b 69 65 2e 20 2a 2f 0a 20 20 20 20  -cookie. */.    
2624a 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
2624b 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20  tree(v, iDb);.  
2624c 20 20 73 77 69 74 63 68 28 20 7a 4c 65 66 74 5b    switch( zLeft[
2624d 30 5d 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  0] ){.      case
2624e 20 27 73 27 3a 20 63 61 73 65 20 27 53 27 3a 0a   's': case 'S':.
2624f 20 20 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20          iCookie 
26250 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = 0;.        bre
26251 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 27  ak;.      case '
26252 66 27 3a 20 63 61 73 65 20 27 46 27 3a 0a 20 20  f': case 'F':.  
26253 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20        iCookie = 
26254 31 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d  1;.        iDb =
26255 20 28 2d 31 2a 28 69 44 62 2b 31 29 29 3b 0a 20   (-1*(iDb+1));. 
26256 20 20 20 20 20 20 20 61 73 73 65 72 74 28 69 44         assert(iD
26257 62 3c 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 62  b<=0);.        b
26258 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61  reak;.      defa
26259 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 69 43 6f  ult:.        iCo
2625a 6f 6b 69 65 20 3d 20 35 3b 0a 20 20 20 20 20 20  okie = 5;.      
2625b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
2625c 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 26      if( zRight &
2625d 26 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20  & iDb>=0 ){.    
2625e 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73    /* Write the s
2625f 70 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20  pecified cookie 
26260 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 73  value */.      s
26261 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
26262 4f 70 4c 69 73 74 20 73 65 74 43 6f 6f 6b 69 65  OpList setCookie
26263 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b  [] = {.        {
26264 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c   OP_Transaction,
26265 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20      0,  1,  0}, 
26266 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20     /* 0 */.     
26267 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
26268 20 20 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20          0,  1,  
26269 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20  0},    /* 1 */. 
2626a 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65 74 43         { OP_SetC
2626b 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20  ookie,      0,  
2626c 30 2c 20 20 31 7d 2c 20 20 20 20 2f 2a 20 32 20  0,  1},    /* 2 
2626d 2a 2f 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20  */.      };.    
2626e 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c    int addr = sql
2626f 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
26270 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73  t(v, ArraySize(s
26271 65 74 43 6f 6f 6b 69 65 29 2c 20 73 65 74 43 6f  etCookie), setCo
26272 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  okie);.      sql
26273 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
26274 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b 0a  (v, addr, iDb);.
26275 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26276 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
26277 72 2b 31 2c 20 61 74 6f 69 28 7a 52 69 67 68 74  r+1, atoi(zRight
26278 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
26279 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
2627a 20 61 64 64 72 2b 32 2c 20 69 44 62 29 3b 0a 20   addr+2, iDb);. 
2627b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2627c 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72  ChangeP2(v, addr
2627d 2b 32 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20  +2, iCookie);.  
2627e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
2627f 2a 20 52 65 61 64 20 74 68 65 20 73 70 65 63 69  * Read the speci
26280 66 69 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75  fied cookie valu
26281 65 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69  e */.      stati
26282 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
26283 73 74 20 72 65 61 64 43 6f 6f 6b 69 65 5b 5d 20  st readCookie[] 
26284 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 50  = {.        { OP
26285 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20 20  _ReadCookie,    
26286 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20    0,  1,  0},   
26287 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20   /* 0 */.       
26288 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
26289 20 20 20 20 20 20 20 31 2c 20 20 31 2c 20 20 30         1,  1,  0
2628a 7d 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20  }.      };.     
2628b 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69   int addr = sqli
2628c 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
2628d 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 72 65  (v, ArraySize(re
2628e 61 64 43 6f 6f 6b 69 65 29 2c 20 72 65 61 64 43  adCookie), readC
2628f 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71  ookie);.      sq
26290 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
26291 31 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b  1(v, addr, iDb);
26292 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26293 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 61 64  beChangeP3(v, ad
26294 64 72 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20  dr, iCookie);.  
26295 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
26296 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
26297 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26298 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
26299 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
2629a 20 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f 54   zLeft, SQLITE_T
2629b 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d  RANSIENT);.    }
2629c 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
2629d 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
2629e 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52  CHEMA_VERSION_PR
2629f 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 20 64 65  AGMAS */..#if de
262a0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
262a1 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  UG) || defined(S
262a2 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 2f 2a  QLITE_TEST).  /*
262a3 0a 20 20 2a 2a 20 52 65 70 6f 72 74 20 74 68 65  .  ** Report the
262a4 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f   current state o
262a5 66 20 66 69 6c 65 20 6c 6f 67 73 20 66 6f 72 20  f file logs for 
262a6 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0a 20 20  all databases.  
262a7 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
262a8 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
262a9 6c 6f 63 6b 5f 73 74 61 74 75 73 22 29 3d 3d 30  lock_status")==0
262aa 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
262ab 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
262ac 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d 20 3d 20   azLockName[] = 
262ad 7b 0a 20 20 20 20 20 20 22 75 6e 6c 6f 63 6b 65  {.      "unlocke
262ae 64 22 2c 20 22 73 68 61 72 65 64 22 2c 20 22 72  d", "shared", "r
262af 65 73 65 72 76 65 64 22 2c 20 22 70 65 6e 64 69  eserved", "pendi
262b0 6e 67 22 2c 20 22 65 78 63 6c 75 73 69 76 65 22  ng", "exclusive"
262b1 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20  .    };.    int 
262b2 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  i;.    sqlite3Vd
262b3 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
262b4 32 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  2);.    pParse->
262b5 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71  nMem = 2;.    sq
262b6 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
262b7 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
262b8 45 5f 4e 41 4d 45 2c 20 22 64 61 74 61 62 61 73  E_NAME, "databas
262b9 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
262ba 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  C);.    sqlite3V
262bb 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
262bc 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
262bd 2c 20 22 73 74 61 74 75 73 22 2c 20 53 51 4c 49  , "status", SQLI
262be 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
262bf 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
262c0 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
262c1 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20  Btree *pBt;.    
262c2 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
262c3 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
262c4 72 20 2a 7a 53 74 61 74 65 20 3d 20 22 75 6e 6b  r *zState = "unk
262c5 6e 6f 77 6e 22 3b 0a 20 20 20 20 20 20 69 6e 74  nown";.      int
262c6 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62   j;.      if( db
262c7 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d  ->aDb[i].zName==
262c8 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
262c9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
262ca 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
262cb 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 64 62  ng8, 0, 1, 0, db
262cc 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  ->aDb[i].zName, 
262cd 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
262ce 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b    pBt = db->aDb[
262cf 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
262d0 28 20 70 42 74 3d 3d 30 20 7c 7c 20 28 70 50 61  ( pBt==0 || (pPa
262d1 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
262d2 65 65 50 61 67 65 72 28 70 42 74 29 29 3d 3d 30  eePager(pBt))==0
262d3 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 74 61   ){.        zSta
262d4 74 65 20 3d 20 22 63 6c 6f 73 65 64 22 3b 0a 20  te = "closed";. 
262d5 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73       }else if( s
262d6 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
262d7 72 6f 6c 28 64 62 2c 20 69 20 3f 20 64 62 2d 3e  rol(db, i ? db->
262d8 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20 30  aDb[i].zName : 0
262d9 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
262da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262db 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46          SQLITE_F
262dc 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 2c 20  CNTL_LOCKSTATE, 
262dd 26 6a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  &j)==SQLITE_OK )
262de 7b 0a 20 20 20 20 20 20 20 20 20 7a 53 74 61 74  {.         zStat
262df 65 20 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a  e = azLockName[j
262e0 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ];.      }.     
262e1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
262e2 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
262e3 2c 20 30 2c 20 32 2c 20 30 2c 20 7a 53 74 61 74  , 0, 2, 0, zStat
262e4 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
262e5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
262e6 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
262e7 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20  ultRow, 1, 2);. 
262e8 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 0a 23 65     }..  }else.#e
262e9 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
262ea 49 54 45 5f 53 53 45 0a 20 20 2f 2a 0a 20 20 2a  ITE_SSE.  /*.  *
262eb 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
262ec 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
262ed 74 65 6d 65 6e 74 73 20 74 61 62 6c 65 20 65 78  tements table ex
262ee 69 73 74 73 2e 20 20 43 72 65 61 74 65 20 69 74  ists.  Create it
262ef 0a 20 20 2a 2a 20 69 66 20 69 74 20 64 6f 65 73  .  ** if it does
262f0 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   not..  */.  if(
262f1 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
262f2 7a 4c 65 66 74 2c 20 22 63 72 65 61 74 65 5f 73  zLeft, "create_s
262f3 71 6c 69 74 65 5f 73 74 61 74 65 6d 65 6e 74 5f  qlite_statement_
262f4 74 61 62 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20  table")==0 ){.  
262f5 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c    extern int sql
262f6 69 74 65 33 43 72 65 61 74 65 53 74 61 74 65 6d  ite3CreateStatem
262f7 65 6e 74 73 54 61 62 6c 65 28 50 61 72 73 65 2a  entsTable(Parse*
262f8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72  );.    sqlite3Cr
262f9 65 61 74 65 53 74 61 74 65 6d 65 6e 74 73 54 61  eateStatementsTa
262fa 62 6c 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d  ble(pParse);.  }
262fb 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66  else.#endif..#if
262fc 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
262fd 43 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  C.  if( sqlite3S
262fe 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b  trICmp(zLeft, "k
262ff 65 79 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  ey")==0 && zRigh
26300 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
26301 5f 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c  _key(db, zRight,
26302 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
26303 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c  (zRight));.  }el
26304 73 65 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  se.  if( sqlite3
26305 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
26306 72 65 6b 65 79 22 29 3d 3d 30 20 26 26 20 7a 52  rekey")==0 && zR
26307 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  ight ){.    sqli
26308 74 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 52  te3_rekey(db, zR
26309 69 67 68 74 2c 20 73 71 6c 69 74 65 33 53 74 72  ight, sqlite3Str
2630a 6c 65 6e 33 30 28 7a 52 69 67 68 74 29 29 3b 0a  len30(zRight));.
2630b 20 20 7d 65 6c 73 65 0a 20 20 69 66 28 20 7a 52    }else.  if( zR
2630c 69 67 68 74 20 26 26 20 28 73 71 6c 69 74 65 33  ight && (sqlite3
2630d 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
2630e 68 65 78 6b 65 79 22 29 3d 3d 30 20 7c 7c 0a 20  hexkey")==0 ||. 
2630f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26310 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
26311 4c 65 66 74 2c 20 22 68 65 78 72 65 6b 65 79 22  Left, "hexrekey"
26312 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74  )==0) ){.    int
26313 20 69 2c 20 68 31 2c 20 68 32 3b 0a 20 20 20 20   i, h1, h2;.    
26314 63 68 61 72 20 7a 4b 65 79 5b 34 30 5d 3b 0a 20  char zKey[40];. 
26315 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 68 31 20     for(i=0; (h1 
26316 3d 20 7a 52 69 67 68 74 5b 69 5d 29 21 3d 30 20  = zRight[i])!=0 
26317 26 26 20 28 68 32 20 3d 20 7a 52 69 67 68 74 5b  && (h2 = zRight[
26318 69 2b 31 5d 29 21 3d 30 3b 20 69 2b 3d 32 29 7b  i+1])!=0; i+=2){
26319 0a 20 20 20 20 20 20 68 31 20 2b 3d 20 39 2a 28  .      h1 += 9*(
2631a 31 26 28 68 31 3e 3e 36 29 29 3b 0a 20 20 20 20  1&(h1>>6));.    
2631b 20 20 68 32 20 2b 3d 20 39 2a 28 31 26 28 68 32    h2 += 9*(1&(h2
2631c 3e 3e 36 29 29 3b 0a 20 20 20 20 20 20 7a 4b 65  >>6));.      zKe
2631d 79 5b 69 2f 32 5d 20 3d 20 28 68 32 20 26 20 30  y[i/2] = (h2 & 0
2631e 78 30 66 29 20 7c 20 28 28 68 31 20 26 20 30 78  x0f) | ((h1 & 0x
2631f 66 29 3c 3c 34 29 3b 0a 20 20 20 20 7d 0a 20 20  f)<<4);.    }.  
26320 20 20 69 66 28 20 28 7a 4c 65 66 74 5b 33 5d 20    if( (zLeft[3] 
26321 26 20 30 78 66 29 3d 3d 30 78 62 20 29 7b 0a 20  & 0xf)==0xb ){. 
26322 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79       sqlite3_key
26323 28 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b  (db, zKey, i/2);
26324 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
26325 20 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28    sqlite3_rekey(
26326 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a  db, zKey, i/2);.
26327 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
26328 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
26329 48 41 53 5f 43 4f 44 45 43 20 7c 7c 20 64 65 66  HAS_CODEC || def
2632a 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
2632b 4c 45 5f 43 45 52 4f 44 29 0a 20 20 69 66 28 20  LE_CEROD).  if( 
2632c 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2632d 4c 65 66 74 2c 20 22 61 63 74 69 76 61 74 65 5f  Left, "activate_
2632e 65 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d 30 20  extensions")==0 
2632f 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41  ){.#if SQLITE_HA
26330 53 5f 43 4f 44 45 43 0a 20 20 20 20 69 66 28 20  S_CODEC.    if( 
26331 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
26332 7a 52 69 67 68 74 2c 20 22 73 65 65 2d 22 2c 20  zRight, "see-", 
26333 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65  4)==0 ){.      e
26334 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74  xtern void sqlit
26335 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65 65 28  e3_activate_see(
26336 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
26337 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63 74 69      sqlite3_acti
26338 76 61 74 65 5f 73 65 65 28 26 7a 52 69 67 68 74  vate_see(&zRight
26339 5b 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  [4]);.    }.#end
2633a 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
2633b 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a 20 20  _ENABLE_CEROD.  
2633c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2633d 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 22 63  NICmp(zRight, "c
2633e 65 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20 29 7b  erod-", 6)==0 ){
2633f 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20 76 6f  .      extern vo
26340 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76  id sqlite3_activ
26341 61 74 65 5f 63 65 72 6f 64 28 63 6f 6e 73 74 20  ate_cerod(const 
26342 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71  char*);.      sq
26343 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63  lite3_activate_c
26344 65 72 6f 64 28 26 7a 52 69 67 68 74 5b 36 5d 29  erod(&zRight[6])
26345 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
26346 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20   }else.#endif.. 
26347 0a 20 20 7b 2f 2a 20 45 6d 70 74 79 20 45 4c 53  .  {/* Empty ELS
26348 45 20 63 6c 61 75 73 65 20 2a 2f 7d 0a 0a 20 20  E clause */}..  
26349 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78  /* Code an OP_Ex
2634a 70 69 72 65 20 61 74 20 74 68 65 20 65 6e 64 20  pire at the end 
2634b 6f 66 20 65 61 63 68 20 50 52 41 47 4d 41 20 70  of each PRAGMA p
2634c 72 6f 67 72 61 6d 20 74 6f 20 63 61 75 73 65 0a  rogram to cause.
2634d 20 20 2a 2a 20 74 68 65 20 56 44 42 45 20 69 6d    ** the VDBE im
2634e 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 70  plementing the p
2634f 72 61 67 6d 61 20 74 6f 20 65 78 70 69 72 65 2e  ragma to expire.
26350 20 4d 6f 73 74 20 28 61 6c 6c 3f 29 20 70 72 61   Most (all?) pra
26351 67 6d 61 73 0a 20 20 2a 2a 20 61 72 65 20 6f 6e  gmas.  ** are on
26352 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 20 73  ly valid for a s
26353 69 6e 67 6c 65 20 65 78 65 63 75 74 69 6f 6e 2e  ingle execution.
26354 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
26355 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26356 45 78 70 69 72 65 2c 20 31 2c 20 30 29 3b 0a 0a  Expire, 1, 0);..
26357 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 73 65 74 20    /*.  ** Reset 
26358 74 68 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c  the safety level
26359 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 66 75  , in case the fu
2635a 6c 6c 66 73 79 6e 63 20 66 6c 61 67 20 6f 72 20  llfsync flag or 
2635b 73 79 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a  synchronous.  **
2635c 20 73 65 74 74 69 6e 67 20 63 68 61 6e 67 65 64   setting changed
2635d 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
2635e 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
2635f 5f 50 52 41 47 4d 41 53 0a 20 20 69 66 28 20 64  _PRAGMAS.  if( d
26360 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
26361 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
26362 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28  eSetSafetyLevel(
26363 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 73  pDb->pBt, pDb->s
26364 61 66 65 74 79 5f 6c 65 76 65 6c 2c 0a 20 20 20  afety_level,.   
26365 20 20 20 20 20 20 20 20 20 20 20 20 28 64 62 2d              (db-
26366 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 75  >flags&SQLITE_Fu
26367 6c 6c 46 53 79 6e 63 29 21 3d 30 29 3b 0a 20 20  llFSync)!=0);.  
26368 7d 0a 23 65 6e 64 69 66 0a 70 72 61 67 6d 61 5f  }.#endif.pragma_
26369 6f 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 44 62  out:.  sqlite3Db
2636a 46 72 65 65 28 64 62 2c 20 7a 4c 65 66 74 29 3b  Free(db, zLeft);
2636b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2636c 28 64 62 2c 20 7a 52 69 67 68 74 29 3b 0a 7d 0a  (db, zRight);.}.
2636d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2636e 45 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 20 7c 7c  E_OMIT_PRAGMA ||
2636f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52   SQLITE_OMIT_PAR
26370 53 45 52 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  SER */../*******
26371 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
26372 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ragma.c ********
26373 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26374 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26375 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
26376 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
26377 6c 65 20 70 72 65 70 61 72 65 2e 63 20 2a 2a 2a  le prepare.c ***
26378 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26379 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2637a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2637b 30 35 20 4d 61 79 20 32 35 0a 2a 2a 0a 2a 2a 20  05 May 25.**.** 
2637c 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
2637d 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
2637e 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
2637f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
26380 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
26381 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
26382 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
26383 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
26384 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
26385 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
26386 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
26387 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
26388 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
26389 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
2638a 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
2638b 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
2638c 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
2638d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2638e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2638f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26391 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
26392 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
26393 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
26394 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
26395 5f 70 72 65 70 61 72 65 28 29 0a 2a 2a 20 69 6e  _prepare().** in
26396 74 65 72 66 61 63 65 2c 20 61 6e 64 20 72 6f 75  terface, and rou
26397 74 69 6e 65 73 20 74 68 61 74 20 63 6f 6e 74 72  tines that contr
26398 69 62 75 74 65 20 74 6f 20 6c 6f 61 64 69 6e 67  ibute to loading
26399 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
2639a 68 65 6d 61 0a 2a 2a 20 66 72 6f 6d 20 64 69 73  hema.** from dis
2639b 6b 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72  k..**.** $Id: pr
2639c 65 70 61 72 65 2e 63 2c 76 20 31 2e 31 31 34 20  epare.c,v 1.114 
2639d 32 30 30 39 2f 30 33 2f 32 34 20 31 35 3a 30 38  2009/03/24 15:08
2639e 3a 31 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :10 drh Exp $.*/
2639f 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65  ../*.** Fill the
263a0 20 49 6e 69 74 44 61 74 61 20 73 74 72 75 63 74   InitData struct
263a1 75 72 65 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ure with an erro
263a2 72 20 6d 65 73 73 61 67 65 20 74 68 61 74 20 69  r message that i
263a3 6e 64 69 63 61 74 65 73 0a 2a 2a 20 74 68 61 74  ndicates.** that
263a4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
263a5 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 73 74 61   corrupt..*/.sta
263a6 74 69 63 20 76 6f 69 64 20 63 6f 72 72 75 70 74  tic void corrupt
263a7 53 63 68 65 6d 61 28 0a 20 20 49 6e 69 74 44 61  Schema(.  InitDa
263a8 74 61 20 2a 70 44 61 74 61 2c 20 20 20 20 20 2f  ta *pData,     /
263a9 2a 20 49 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  * Initialization
263aa 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
263ab 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a 2c 20  nst char *zObj, 
263ac 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 62 65 69     /* Object bei
263ad 6e 67 20 70 61 72 73 65 64 20 61 74 20 74 68 65  ng parsed at the
263ae 20 70 6f 69 6e 74 20 6f 66 20 65 72 72 6f 72 20   point of error 
263af 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
263b0 2a 7a 45 78 74 72 61 20 20 20 2f 2a 20 45 72 72  *zExtra   /* Err
263b1 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a  or information *
263b2 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
263b3 64 62 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b 0a  db = pData->db;.
263b4 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
263b5 63 46 61 69 6c 65 64 20 26 26 20 28 64 62 2d 3e  cFailed && (db->
263b6 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52  flags & SQLITE_R
263b7 65 63 6f 76 65 72 79 4d 6f 64 65 29 3d 3d 30 20  ecoveryMode)==0 
263b8 29 7b 0a 20 20 20 20 69 66 28 20 7a 4f 62 6a 3d  ){.    if( zObj=
263b9 3d 30 20 29 20 7a 4f 62 6a 20 3d 20 22 3f 22 3b  =0 ) zObj = "?";
263ba 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
263bb 74 72 69 6e 67 28 70 44 61 74 61 2d 3e 70 7a 45  tring(pData->pzE
263bc 72 72 4d 73 67 2c 20 70 44 61 74 61 2d 3e 64 62  rrMsg, pData->db
263bd 2c 0a 20 20 20 20 20 20 20 22 6d 61 6c 66 6f 72  ,.       "malfor
263be 6d 65 64 20 64 61 74 61 62 61 73 65 20 73 63 68  med database sch
263bf 65 6d 61 20 28 25 73 29 22 2c 20 7a 4f 62 6a 29  ema (%s)", zObj)
263c0 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 72 61  ;.    if( zExtra
263c1 20 26 26 20 7a 45 78 74 72 61 5b 30 5d 20 29 7b   && zExtra[0] ){
263c2 0a 20 20 20 20 20 20 2a 70 44 61 74 61 2d 3e 70  .      *pData->p
263c3 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
263c4 33 4d 41 70 70 65 6e 64 66 28 70 44 61 74 61 2d  3MAppendf(pData-
263c5 3e 64 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45  >db, *pData->pzE
263c6 72 72 4d 73 67 2c 20 22 25 73 20 2d 20 25 73 22  rrMsg, "%s - %s"
263c7 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
263c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263c9 20 20 20 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72      *pData->pzEr
263ca 72 4d 73 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20  rMsg, zExtra);. 
263cb 20 20 20 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61     }.  }.  pData
263cc 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f  ->rc = SQLITE_CO
263cd 52 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  RRUPT;.}../*.** 
263ce 54 68 69 73 20 69 73 20 74 68 65 20 63 61 6c 6c  This is the call
263cf 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 66 6f 72  back routine for
263d0 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 69   the code that i
263d1 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a  nitializes the.*
263d2 2a 20 64 61 74 61 62 61 73 65 2e 20 20 53 65 65  * database.  See
263d3 20 73 71 6c 69 74 65 33 49 6e 69 74 28 29 20 62   sqlite3Init() b
263d4 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f  elow for additio
263d5 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
263d6 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
263d7 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20   is also called 
263d8 66 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61 72 73  from the OP_Pars
263d9 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 20 6f  eSchema opcode o
263da 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a 0a 2a  f the VDBE..**.*
263db 2a 20 45 61 63 68 20 63 61 6c 6c 62 61 63 6b 20  * Each callback 
263dc 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f 6c  contains the fol
263dd 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  lowing informati
263de 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72  on:.**.**     ar
263df 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20  gv[0] = name of 
263e0 74 68 69 6e 67 20 62 65 69 6e 67 20 63 72 65 61  thing being crea
263e1 74 65 64 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b  ted.**     argv[
263e2 31 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65 20 6e  1] = root page n
263e3 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
263e4 6f 72 20 69 6e 64 65 78 2e 20 30 20 66 6f 72 20  or index. 0 for 
263e5 74 72 69 67 67 65 72 20 6f 72 20 76 69 65 77 2e  trigger or view.
263e6 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 32 5d 20  .**     argv[2] 
263e7 3d 20 53 51 4c 20 74 65 78 74 20 66 6f 72 20 74  = SQL text for t
263e8 68 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d  he CREATE statem
263e9 65 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  ent..**.*/.SQLIT
263ea 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
263eb 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
263ec 6b 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c 20 69  k(void *pInit, i
263ed 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a  nt argc, char **
263ee 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74  argv, char **Not
263ef 55 73 65 64 29 7b 0a 20 20 49 6e 69 74 44 61 74  Used){.  InitDat
263f0 61 20 2a 70 44 61 74 61 20 3d 20 28 49 6e 69 74  a *pData = (Init
263f1 44 61 74 61 2a 29 70 49 6e 69 74 3b 0a 20 20 73  Data*)pInit;.  s
263f2 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 44 61  qlite3 *db = pDa
263f3 74 61 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  ta->db;.  int iD
263f4 62 20 3d 20 70 44 61 74 61 2d 3e 69 44 62 3b 0a  b = pData->iDb;.
263f5 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
263f6 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =3 );.  UNUSED_P
263f7 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
263f8 64 2c 20 61 72 67 63 29 3b 0a 20 20 61 73 73 65  d, argc);.  asse
263f9 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
263fa 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
263fb 29 20 29 3b 0a 20 20 44 62 43 6c 65 61 72 50 72  ) );.  DbClearPr
263fc 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20  operty(db, iDb, 
263fd 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20 69 66 28  DB_Empty);.  if(
263fe 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
263ff 64 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74  d ){.    corrupt
26400 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72  Schema(pData, ar
26401 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 20 20 72  gv[0], 0);.    r
26402 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
26403 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  EM;.  }..  asser
26404 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
26405 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 66  <db->nDb );.  if
26406 28 20 61 72 67 76 3d 3d 30 20 29 20 72 65 74 75  ( argv==0 ) retu
26407 72 6e 20 30 3b 20 20 20 2f 2a 20 4d 69 67 68 74  rn 0;   /* Might
26408 20 68 61 70 70 65 6e 20 69 66 20 45 4d 50 54 59   happen if EMPTY
26409 5f 52 45 53 55 4c 54 5f 43 41 4c 4c 42 41 43 4b  _RESULT_CALLBACK
2640a 53 20 61 72 65 20 6f 6e 20 2a 2f 0a 20 20 69 66  S are on */.  if
2640b 28 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a  ( argv[1]==0 ){.
2640c 20 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d      corruptSchem
2640d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d  a(pData, argv[0]
2640e 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  , 0);.  }else if
2640f 28 20 61 72 67 76 5b 32 5d 20 26 26 20 61 72 67  ( argv[2] && arg
26410 76 5b 32 5d 5b 30 5d 20 29 7b 0a 20 20 20 20 2f  v[2][0] ){.    /
26411 2a 20 43 61 6c 6c 20 74 68 65 20 70 61 72 73 65  * Call the parse
26412 72 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 43  r to process a C
26413 52 45 41 54 45 20 54 41 42 4c 45 2c 20 49 4e 44  REATE TABLE, IND
26414 45 58 20 6f 72 20 56 49 45 57 2e 0a 20 20 20 20  EX or VIEW..    
26415 2a 2a 20 42 75 74 20 62 65 63 61 75 73 65 20 64  ** But because d
26416 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20  b->init.busy is 
26417 73 65 74 20 74 6f 20 31 2c 20 6e 6f 20 56 44 42  set to 1, no VDB
26418 45 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61  E code is genera
26419 74 65 64 0a 20 20 20 20 2a 2a 20 6f 72 20 65 78  ted.    ** or ex
2641a 65 63 75 74 65 64 2e 20 20 41 6c 6c 20 74 68 65  ecuted.  All the
2641b 20 70 61 72 73 65 72 20 64 6f 65 73 20 69 73 20   parser does is 
2641c 62 75 69 6c 64 20 74 68 65 20 69 6e 74 65 72 6e  build the intern
2641d 61 6c 20 64 61 74 61 0a 20 20 20 20 2a 2a 20 73  al data.    ** s
2641e 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64  tructures that d
2641f 65 73 63 72 69 62 65 20 74 68 65 20 74 61 62 6c  escribe the tabl
26420 65 2c 20 69 6e 64 65 78 2c 20 6f 72 20 76 69 65  e, index, or vie
26421 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68  w..    */.    ch
26422 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20 69 6e  ar *zErr;.    in
26423 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74  t rc;.    assert
26424 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ( db->init.busy 
26425 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e  );.    db->init.
26426 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 64  iDb = iDb;.    d
26427 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 20  b->init.newTnum 
26428 3d 20 61 74 6f 69 28 61 72 67 76 5b 31 5d 29 3b  = atoi(argv[1]);
26429 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2642a 33 5f 65 78 65 63 28 64 62 2c 20 61 72 67 76 5b  3_exec(db, argv[
2642b 32 5d 2c 20 30 2c 20 30 2c 20 26 7a 45 72 72 29  2], 0, 0, &zErr)
2642c 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69  ;.    db->init.i
2642d 44 62 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65  Db = 0;.    asse
2642e 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
2642f 4b 20 7c 7c 20 7a 45 72 72 3d 3d 30 20 29 3b 0a  K || zErr==0 );.
26430 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
26431 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 70  K!=rc ){.      p
26432 44 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b 0a 20  Data->rc = rc;. 
26433 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
26434 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ITE_NOMEM ){.   
26435 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
26436 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
26437 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53   }else if( rc!=S
26438 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20  QLITE_INTERRUPT 
26439 26 26 20 28 72 63 26 30 78 66 66 29 21 3d 53 51  && (rc&0xff)!=SQ
2643a 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20  LITE_LOCKED ){. 
2643b 20 20 20 20 20 20 20 63 6f 72 72 75 70 74 53 63         corruptSc
2643c 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76  hema(pData, argv
2643d 5b 30 5d 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  [0], zErr);.    
2643e 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2643f 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
26440 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
26441 20 69 66 28 20 61 72 67 76 5b 30 5d 3d 3d 30 20   if( argv[0]==0 
26442 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53 63  ){.    corruptSc
26443 68 65 6d 61 28 70 44 61 74 61 2c 20 30 2c 20 30  hema(pData, 0, 0
26444 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
26445 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 20 63 6f  /* If the SQL co
26446 6c 75 6d 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74  lumn is blank it
26447 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 61   means this is a
26448 6e 20 69 6e 64 65 78 20 74 68 61 74 0a 20 20 20  n index that.   
26449 20 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 20   ** was created 
2644a 74 6f 20 62 65 20 74 68 65 20 50 52 49 4d 41 52  to be the PRIMAR
2644b 59 20 4b 45 59 20 6f 72 20 74 6f 20 66 75 6c 66  Y KEY or to fulf
2644c 69 6c 6c 20 61 20 55 4e 49 51 55 45 0a 20 20 20  ill a UNIQUE.   
2644d 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 66   ** constraint f
2644e 6f 72 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  or a CREATE TABL
2644f 45 2e 20 20 54 68 65 20 69 6e 64 65 78 20 73 68  E.  The index sh
26450 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64  ould have alread
26451 79 0a 20 20 20 20 2a 2a 20 62 65 65 6e 20 63 72  y.    ** been cr
26452 65 61 74 65 64 20 77 68 65 6e 20 77 65 20 70 72  eated when we pr
26453 6f 63 65 73 73 65 64 20 74 68 65 20 43 52 45 41  ocessed the CREA
26454 54 45 20 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77  TE TABLE.  All w
26455 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f  e have.    ** to
26456 20 64 6f 20 68 65 72 65 20 69 73 20 72 65 63 6f   do here is reco
26457 72 64 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  rd the root page
26458 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74   number for that
26459 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20   index..    */. 
2645a 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78     Index *pIndex
2645b 3b 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 73  ;.    pIndex = s
2645c 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
2645d 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20 64 62 2d  db, argv[0], db-
2645e 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
2645f 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78  ;.    if( pIndex
26460 3d 3d 30 20 7c 7c 20 70 49 6e 64 65 78 2d 3e 74  ==0 || pIndex->t
26461 6e 75 6d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  num!=0 ){.      
26462 2f 2a 20 54 68 69 73 20 63 61 6e 20 6f 63 63 75  /* This can occu
26463 72 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74  r if there exist
26464 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 61 20  s an index on a 
26465 54 45 4d 50 20 74 61 62 6c 65 20 77 68 69 63 68  TEMP table which
26466 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 74 68  .      ** has th
26467 65 20 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 61  e same name as a
26468 6e 6f 74 68 65 72 20 69 6e 64 65 78 20 6f 6e 20  nother index on 
26469 61 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e 64 65  a permanent inde
2646a 78 2e 20 20 53 69 6e 63 65 0a 20 20 20 20 20 20  x.  Since.      
2646b 2a 2a 20 74 68 65 20 70 65 72 6d 61 6e 65 6e 74  ** the permanent
2646c 20 74 61 62 6c 65 20 69 73 20 68 69 64 64 65 6e   table is hidden
2646d 20 62 79 20 74 68 65 20 54 45 4d 50 20 74 61 62   by the TEMP tab
2646e 6c 65 2c 20 77 65 20 63 61 6e 20 61 6c 73 6f 0a  le, we can also.
2646f 20 20 20 20 20 20 2a 2a 20 73 61 66 65 6c 79 20        ** safely 
26470 69 67 6e 6f 72 65 20 74 68 65 20 69 6e 64 65 78  ignore the index
26471 20 6f 6e 20 74 68 65 20 70 65 72 6d 61 6e 65 6e   on the permanen
26472 74 20 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a  t table..      *
26473 2f 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20 4e 6f  /.      /* Do No
26474 74 68 69 6e 67 20 2a 2f 3b 0a 20 20 20 20 7d 65  thing */;.    }e
26475 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 64 65  lse{.      pInde
26476 78 2d 3e 74 6e 75 6d 20 3d 20 61 74 6f 69 28 61  x->tnum = atoi(a
26477 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  rgv[1]);.    }. 
26478 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
26479 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
2647a 74 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61  to read the data
2647b 62 61 73 65 20 73 63 68 65 6d 61 20 61 6e 64 20  base schema and 
2647c 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72  initialize inter
2647d 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 73 74 72 75  nal.** data stru
2647e 63 74 75 72 65 73 20 66 6f 72 20 61 20 73 69 6e  ctures for a sin
2647f 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  gle database fil
26480 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66  e.  The index of
26481 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
26482 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62   file is given b
26483 79 20 69 44 62 2e 20 20 69 44 62 3d 3d 30 20 69  y iDb.  iDb==0 i
26484 73 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d  s used for the m
26485 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e  ain.** database.
26486 20 20 69 44 62 3d 3d 31 20 73 68 6f 75 6c 64 20    iDb==1 should 
26487 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 20  never be used.  
26488 69 44 62 3e 3d 32 20 69 73 20 75 73 65 64 20 66  iDb>=2 is used f
26489 6f 72 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20  or.** auxiliary 
2648a 64 61 74 61 62 61 73 65 73 2e 20 20 52 65 74 75  databases.  Retu
2648b 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51  rn one of the SQ
2648c 4c 49 54 45 5f 20 65 72 72 6f 72 20 63 6f 64 65  LITE_ error code
2648d 73 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61 74 65  s to.** indicate
2648e 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c   success or fail
2648f 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
26490 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e  nt sqlite3InitOn
26491 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  e(sqlite3 *db, i
26492 6e 74 20 69 44 62 2c 20 63 68 61 72 20 2a 2a 70  nt iDb, char **p
26493 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrMsg){.  int 
26494 72 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  rc;.  BtCursor *
26495 63 75 72 4d 61 69 6e 3b 0a 20 20 69 6e 74 20 73  curMain;.  int s
26496 69 7a 65 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  ize;.  Table *pT
26497 61 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20  ab;.  Db *pDb;. 
26498 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 61 7a 41   char const *azA
26499 72 67 5b 34 5d 3b 0a 20 20 69 6e 74 20 6d 65 74  rg[4];.  int met
2649a 61 5b 31 30 5d 3b 0a 20 20 49 6e 69 74 44 61 74  a[10];.  InitDat
2649b 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20 63 68  a initData;.  ch
2649c 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65  ar const *zMaste
2649d 72 53 63 68 65 6d 61 3b 0a 20 20 63 68 61 72 20  rSchema;.  char 
2649e 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72 4e 61  const *zMasterNa
2649f 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c  me = SCHEMA_TABL
264a0 45 28 69 44 62 29 3b 0a 0a 20 20 2f 2a 0a 20 20  E(iDb);..  /*.  
264a1 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 64 61  ** The master da
264a2 74 61 62 61 73 65 20 74 61 62 6c 65 20 68 61 73  tabase table has
264a3 20 61 20 73 74 72 75 63 74 75 72 65 20 6c 69 6b   a structure lik
264a4 65 20 74 68 69 73 0a 20 20 2a 2f 0a 20 20 73 74  e this.  */.  st
264a5 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
264a6 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20  master_schema[] 
264a7 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54 45 20  = .     "CREATE 
264a8 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 6d 61 73  TABLE sqlite_mas
264a9 74 65 72 28 5c 6e 22 0a 20 20 20 20 20 22 20 20  ter(\n".     "  
264aa 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  type text,\n".  
264ab 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78 74 2c     "  name text,
264ac 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 62 6c 5f  \n".     "  tbl_
264ad 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  name text,\n".  
264ae 20 20 20 22 20 20 72 6f 6f 74 70 61 67 65 20 69     "  rootpage i
264af 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20  nteger,\n".     
264b0 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20  "  sql text\n". 
264b1 20 20 20 20 22 29 22 0a 20 20 3b 0a 23 69 66 6e      ")".  ;.#ifn
264b2 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
264b3 54 45 4d 50 44 42 0a 20 20 73 74 61 74 69 63 20  TEMPDB.  static 
264b4 63 6f 6e 73 74 20 63 68 61 72 20 74 65 6d 70 5f  const char temp_
264b5 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20  master_schema[] 
264b6 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54 45 20  = .     "CREATE 
264b7 54 45 4d 50 20 54 41 42 4c 45 20 73 71 6c 69 74  TEMP TABLE sqlit
264b8 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 28 5c 6e  e_temp_master(\n
264b9 22 0a 20 20 20 20 20 22 20 20 74 79 70 65 20 74  ".     "  type t
264ba 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
264bb 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  name text,\n".  
264bc 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74     "  tbl_name t
264bd 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
264be 72 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72  rootpage integer
264bf 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c  ,\n".     "  sql
264c0 20 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29   text\n".     ")
264c1 22 0a 20 20 3b 0a 23 65 6c 73 65 0a 20 20 23 64  ".  ;.#else.  #d
264c2 65 66 69 6e 65 20 74 65 6d 70 5f 6d 61 73 74 65  efine temp_maste
264c3 72 5f 73 63 68 65 6d 61 20 30 0a 23 65 6e 64 69  r_schema 0.#endi
264c4 66 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  f..  assert( iDb
264c5 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
264c6 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
264c7 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
264c8 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74  hema );.  assert
264c9 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
264ca 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
264cb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
264cc 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65 33 42 74  ==1 || sqlite3Bt
264cd 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62  reeHoldsMutex(db
264ce 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20  ->aDb[iDb].pBt) 
264cf 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73 74 65 72  );..  /* zMaster
264d0 53 63 68 65 6d 61 20 61 6e 64 20 7a 49 6e 69 74  Schema and zInit
264d1 53 63 72 69 70 74 20 61 72 65 20 73 65 74 20 74  Script are set t
264d2 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6d  o point at the m
264d3 61 73 74 65 72 20 73 63 68 65 6d 61 0a 20 20 2a  aster schema.  *
264d4 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 61  * and initialisa
264d5 74 69 6f 6e 20 73 63 72 69 70 74 20 61 70 70 72  tion script appr
264d6 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20  opriate for the 
264d7 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 0a 20  database being. 
264d8 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73 65 64 2e   ** initialised.
264d9 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20 69 73 20   zMasterName is 
264da 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
264db 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20  master table..  
264dc 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  */.  if( !OMIT_T
264dd 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20  EMPDB && iDb==1 
264de 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63  ){.    zMasterSc
264df 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d 61 73 74  hema = temp_mast
264e0 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 65 6c  er_schema;.  }el
264e1 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53  se{.    zMasterS
264e2 63 68 65 6d 61 20 3d 20 6d 61 73 74 65 72 5f 73  chema = master_s
264e3 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20 7a 4d 61  chema;.  }.  zMa
264e4 73 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d  sterName = SCHEM
264e5 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20  A_TABLE(iDb);.. 
264e6 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68   /* Construct th
264e7 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73 2e  e schema tables.
264e8 20 20 2a 2f 0a 20 20 61 7a 41 72 67 5b 30 5d 20    */.  azArg[0] 
264e9 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65 3b 0a 20  = zMasterName;. 
264ea 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22 31 22 3b   azArg[1] = "1";
264eb 0a 20 20 61 7a 41 72 67 5b 32 5d 20 3d 20 7a 4d  .  azArg[2] = zM
264ec 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 61  asterSchema;.  a
264ed 7a 41 72 67 5b 33 5d 20 3d 20 30 3b 0a 20 20 69  zArg[3] = 0;.  i
264ee 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62 3b  nitData.db = db;
264ef 0a 20 20 69 6e 69 74 44 61 74 61 2e 69 44 62 20  .  initData.iDb 
264f0 3d 20 69 44 62 3b 0a 20 20 69 6e 69 74 44 61 74  = iDb;.  initDat
264f1 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  a.rc = SQLITE_OK
264f2 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45  ;.  initData.pzE
264f3 72 72 4d 73 67 20 3d 20 70 7a 45 72 72 4d 73 67  rrMsg = pzErrMsg
264f4 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  ;.  (void)sqlite
264f5 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
264f6 20 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c    sqlite3InitCal
264f7 6c 62 61 63 6b 28 26 69 6e 69 74 44 61 74 61 2c  lback(&initData,
264f8 20 33 2c 20 28 63 68 61 72 20 2a 2a 29 61 7a 41   3, (char **)azA
264f9 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f 69 64 29  rg, 0);.  (void)
264fa 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
264fb 64 62 29 3b 0a 20 20 69 66 28 20 69 6e 69 74 44  db);.  if( initD
264fc 61 74 61 2e 72 63 20 29 7b 0a 20 20 20 20 72 63  ata.rc ){.    rc
264fd 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a   = initData.rc;.
264fe 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
264ff 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d  ut;.  }.  pTab =
26500 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
26501 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 4e 61 6d  e(db, zMasterNam
26502 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e, db->aDb[iDb].
26503 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54  zName);.  if( pT
26504 61 62 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e  ab ){.    pTab->
26505 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 52  tabFlags |= TF_R
26506 65 61 64 6f 6e 6c 79 3b 0a 20 20 7d 0a 0a 20 20  eadonly;.  }..  
26507 2f 2a 20 43 72 65 61 74 65 20 61 20 63 75 72 73  /* Create a curs
26508 6f 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64  or to hold the d
26509 61 74 61 62 61 73 65 20 6f 70 65 6e 0a 20 20 2a  atabase open.  *
2650a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  /.  pDb = &db->a
2650b 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66 28 20 70  Db[iDb];.  if( p
2650c 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20  Db->pBt==0 ){.  
2650d 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
2650e 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a  DB && iDb==1 ){.
2650f 20 20 20 20 20 20 44 62 53 65 74 50 72 6f 70 65        DbSetPrope
26510 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53 63  rty(db, 1, DB_Sc
26511 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20  hemaLoaded);.   
26512 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   }.    return SQ
26513 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 63  LITE_OK;.  }.  c
26514 75 72 4d 61 69 6e 20 3d 20 73 71 6c 69 74 65 33  urMain = sqlite3
26515 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74  MallocZero(sqlit
26516 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a  e3BtreeCursorSiz
26517 65 28 29 29 3b 0a 20 20 69 66 28 20 21 63 75 72  e());.  if( !cur
26518 4d 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d  Main ){.    rc =
26519 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2651a 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75     goto error_ou
2651b 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  t;.  }.  sqlite3
2651c 42 74 72 65 65 45 6e 74 65 72 28 70 44 62 2d 3e  BtreeEnter(pDb->
2651d 70 42 74 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  pBt);.  rc = sql
2651e 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28  ite3BtreeCursor(
2651f 70 44 62 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52  pDb->pBt, MASTER
26520 5f 52 4f 4f 54 2c 20 30 2c 20 30 2c 20 63 75 72  _ROOT, 0, 0, cur
26521 4d 61 69 6e 29 3b 0a 20 20 69 66 28 20 72 63 21  Main);.  if( rc!
26522 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63  =SQLITE_OK && rc
26523 21 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29  !=SQLITE_EMPTY )
26524 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
26525 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c  String(pzErrMsg,
26526 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
26527 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20  e3ErrStr(rc));. 
26528 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
26529 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
2652a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 64 61 74    /* Get the dat
2652b 61 62 61 73 65 20 6d 65 74 61 20 69 6e 66 6f 72  abase meta infor
2652c 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a  mation..  **.  *
2652d 2a 20 4d 65 74 61 20 76 61 6c 75 65 73 20 61 72  * Meta values ar
2652e 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20  e as follows:.  
2652f 2a 2a 20 20 20 20 6d 65 74 61 5b 30 5d 20 20 20  **    meta[0]   
26530 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20  Schema cookie.  
26531 43 68 61 6e 67 65 73 20 77 69 74 68 20 65 61 63  Changes with eac
26532 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e  h schema change.
26533 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 31 5d  .  **    meta[1]
26534 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 6f     File format o
26535 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72 2e 0a  f schema layer..
26536 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 32 5d 20    **    meta[2] 
26537 20 20 53 69 7a 65 20 6f 66 20 74 68 65 20 70 61    Size of the pa
26538 67 65 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20 20  ge cache..  **  
26539 20 20 6d 65 74 61 5b 33 5d 20 20 20 55 73 65 20    meta[3]   Use 
2653a 66 72 65 65 6c 69 73 74 20 69 66 20 30 2e 20 20  freelist if 0.  
2653b 41 75 74 6f 76 61 63 75 75 6d 20 69 66 20 67 72  Autovacuum if gr
2653c 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e  eater than zero.
2653d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 34 5d  .  **    meta[4]
2653e 20 20 20 44 62 20 74 65 78 74 20 65 6e 63 6f 64     Db text encod
2653f 69 6e 67 2e 20 31 3a 55 54 46 2d 38 20 32 3a 55  ing. 1:UTF-8 2:U
26540 54 46 2d 31 36 4c 45 20 33 3a 55 54 46 2d 31 36  TF-16LE 3:UTF-16
26541 42 45 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  BE.  **    meta[
26542 35 5d 20 20 20 54 68 65 20 75 73 65 72 20 63 6f  5]   The user co
26543 6f 6b 69 65 2e 20 55 73 65 64 20 62 79 20 74 68  okie. Used by th
26544 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 0a 20  e application.. 
26545 20 2a 2a 20 20 20 20 6d 65 74 61 5b 36 5d 20 20   **    meta[6]  
26546 20 49 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61 63   Incremental-vac
26547 75 75 6d 20 66 6c 61 67 2e 0a 20 20 2a 2a 20 20  uum flag..  **  
26548 20 20 6d 65 74 61 5b 37 5d 0a 20 20 2a 2a 20 20    meta[7].  **  
26549 20 20 6d 65 74 61 5b 38 5d 0a 20 20 2a 2a 20 20    meta[8].  **  
2654a 20 20 6d 65 74 61 5b 39 5d 0a 20 20 2a 2a 0a 20    meta[9].  **. 
2654b 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65 20 23 64   ** Note: The #d
2654c 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 55 54  efined SQLITE_UT
2654d 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e 20 73 71  F* symbols in sq
2654e 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72 72 65 73  liteInt.h corres
2654f 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20 74 68 65  pond to.  ** the
26550 20 70 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73   possible values
26551 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a 20 20 2a   of meta[4]..  *
26552 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
26553 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74  TE_OK ){.    int
26554 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   i;.    for(i=0;
26555 20 69 3c 41 72 72 61 79 53 69 7a 65 28 6d 65 74   i<ArraySize(met
26556 61 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  a); i++){.      
26557 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
26558 65 47 65 74 4d 65 74 61 28 70 44 62 2d 3e 70 42  eGetMeta(pDb->pB
26559 74 2c 20 69 2b 31 2c 20 28 75 33 32 20 2a 29 26  t, i+1, (u32 *)&
2655a 6d 65 74 61 5b 69 5d 29 3b 0a 20 20 20 20 20 20  meta[i]);.      
2655b 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
2655c 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
2655d 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c  ng(pzErrMsg, db,
2655e 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72   "%s", sqlite3Er
2655f 72 53 74 72 28 72 63 29 29 3b 0a 20 20 20 20 20  rStr(rc));.     
26560 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
26561 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 20  error_out;.     
26562 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
26563 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 6d 65 74  {.    memset(met
26564 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 74  a, 0, sizeof(met
26565 61 29 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e  a));.  }.  pDb->
26566 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
26567 63 6f 6f 6b 69 65 20 3d 20 6d 65 74 61 5b 30 5d  cookie = meta[0]
26568 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69  ;..  /* If openi
26569 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70 74 79 20 64  ng a non-empty d
2656a 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b 20 74  atabase, check t
2656b 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  he text encoding
2656c 2e 20 46 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d  . For the.  ** m
2656d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65  ain database, se
2656e 74 20 73 71 6c 69 74 65 33 2e 65 6e 63 20 74 6f  t sqlite3.enc to
2656f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66   the encoding of
26570 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
26571 73 65 2e 0a 20 20 2a 2a 20 46 6f 72 20 61 6e 20  se..  ** For an 
26572 61 74 74 61 63 68 65 64 20 64 62 2c 20 69 74 20  attached db, it 
26573 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74  is an error if t
26574 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 6e  he encoding is n
26575 6f 74 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a  ot the same.  **
26576 20 61 73 20 73 71 6c 69 74 65 33 2e 65 6e 63 2e   as sqlite3.enc.
26577 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 74 61  .  */.  if( meta
26578 5b 34 5d 20 29 7b 20 20 2f 2a 20 74 65 78 74 20  [4] ){  /* text 
26579 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20  encoding */.    
2657a 69 66 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20  if( iDb==0 ){.  
2657b 20 20 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e      /* If openin
2657c 67 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  g the main datab
2657d 61 73 65 2c 20 73 65 74 20 45 4e 43 28 64 62 29  ase, set ENC(db)
2657e 2e 20 2a 2f 0a 20 20 20 20 20 20 45 4e 43 28 64  . */.      ENC(d
2657f 62 29 20 3d 20 28 75 38 29 6d 65 74 61 5b 34 5d  b) = (u8)meta[4]
26580 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 44 66 6c  ;.      db->pDfl
26581 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  tColl = sqlite3F
26582 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53  indCollSeq(db, S
26583 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 42 49 4e  QLITE_UTF8, "BIN
26584 41 52 59 22 2c 20 36 2c 20 30 29 3b 0a 20 20 20  ARY", 6, 0);.   
26585 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
26586 20 49 66 20 6f 70 65 6e 69 6e 67 20 61 6e 20 61   If opening an a
26587 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
26588 2c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6d  , the encoding m
26589 75 63 68 20 6d 61 74 63 68 20 45 4e 43 28 64 62  uch match ENC(db
2658a 29 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6d  ) */.      if( m
2658b 65 74 61 5b 34 5d 21 3d 45 4e 43 28 64 62 29 20  eta[4]!=ENC(db) 
2658c 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2658d 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72  e3SetString(pzEr
2658e 72 4d 73 67 2c 20 64 62 2c 20 22 61 74 74 61 63  rMsg, db, "attac
2658f 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d 75  hed databases mu
26590 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65 22  st use the same"
26591 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 20 74  .            " t
26592 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20  ext encoding as 
26593 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22 29 3b  main database");
26594 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
26595 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
26596 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65      goto initone
26597 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20  _error_out;.    
26598 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
26599 65 7b 0a 20 20 20 20 44 62 53 65 74 50 72 6f 70  e{.    DbSetProp
2659a 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42  erty(db, iDb, DB
2659b 5f 45 6d 70 74 79 29 3b 0a 20 20 7d 0a 20 20 70  _Empty);.  }.  p
2659c 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63  Db->pSchema->enc
2659d 20 3d 20 45 4e 43 28 64 62 29 3b 0a 0a 20 20 69   = ENC(db);..  i
2659e 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  f( pDb->pSchema-
2659f 3e 63 61 63 68 65 5f 73 69 7a 65 3d 3d 30 20 29  >cache_size==0 )
265a0 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 74  {.    size = met
265a1 61 5b 32 5d 3b 0a 20 20 20 20 69 66 28 20 73 69  a[2];.    if( si
265a2 7a 65 3d 3d 30 20 29 7b 20 73 69 7a 65 20 3d 20  ze==0 ){ size = 
265a3 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43  SQLITE_DEFAULT_C
265a4 41 43 48 45 5f 53 49 5a 45 3b 20 7d 0a 20 20 20  ACHE_SIZE; }.   
265a5 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69   if( size<0 ) si
265a6 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20  ze = -size;.    
265a7 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
265a8 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b  che_size = size;
265a9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
265aa 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 70 44  eSetCacheSize(pD
265ab 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63  b->pBt, pDb->pSc
265ac 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
265ad 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  );.  }..  /*.  *
265ae 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 31  * file_format==1
265af 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 30 2e      Version 3.0.
265b0 30 2e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  0..  ** file_for
265b1 6d 61 74 3d 3d 32 20 20 20 20 56 65 72 73 69 6f  mat==2    Versio
265b2 6e 20 33 2e 31 2e 33 2e 20 20 2f 2f 20 41 4c 54  n 3.1.3.  // ALT
265b3 45 52 20 54 41 42 4c 45 20 41 44 44 20 43 4f 4c  ER TABLE ADD COL
265b4 55 4d 4e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f  UMN.  ** file_fo
265b5 72 6d 61 74 3d 3d 33 20 20 20 20 56 65 72 73 69  rmat==3    Versi
265b6 6f 6e 20 33 2e 31 2e 34 2e 20 20 2f 2f 20 64 69  on 3.1.4.  // di
265b7 74 74 6f 20 62 75 74 20 77 69 74 68 20 6e 6f 6e  tto but with non
265b8 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74 73 0a 20  -NULL defaults. 
265b9 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d   ** file_format=
265ba 3d 34 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e  =4    Version 3.
265bb 33 2e 30 2e 20 20 2f 2f 20 44 45 53 43 20 69 6e  3.0.  // DESC in
265bc 64 69 63 65 73 2e 20 20 42 6f 6f 6c 65 61 6e 20  dices.  Boolean 
265bd 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a 2f 0a 20  constants.  */. 
265be 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
265bf 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38  ile_format = (u8
265c0 29 6d 65 74 61 5b 31 5d 3b 0a 20 20 69 66 28 20  )meta[1];.  if( 
265c1 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
265c2 6c 65 5f 66 6f 72 6d 61 74 3d 3d 30 20 29 7b 0a  le_format==0 ){.
265c3 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
265c4 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20  ->file_format = 
265c5 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 62  1;.  }.  if( pDb
265c6 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
265c7 66 6f 72 6d 61 74 3e 53 51 4c 49 54 45 5f 4d 41  format>SQLITE_MA
265c8 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b  X_FILE_FORMAT ){
265c9 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
265ca 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
265cb 64 62 2c 20 22 75 6e 73 75 70 70 6f 72 74 65 64  db, "unsupported
265cc 20 66 69 6c 65 20 66 6f 72 6d 61 74 22 29 3b 0a   file format");.
265cd 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
265ce 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20  ERROR;.    goto 
265cf 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75  initone_error_ou
265d0 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 69 63  t;.  }..  /* Tic
265d1 6b 65 74 20 23 32 38 30 34 3a 20 20 57 68 65 6e  ket #2804:  When
265d2 20 77 65 20 6f 70 65 6e 20 61 20 64 61 74 61 62   we open a datab
265d3 61 73 65 20 69 6e 20 74 68 65 20 6e 65 77 65 72  ase in the newer
265d4 20 66 69 6c 65 20 66 6f 72 6d 61 74 2c 0a 20 20   file format,.  
265d5 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 6c 65 67  ** clear the leg
265d6 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 20  acy_file_format 
265d7 70 72 61 67 6d 61 20 66 6c 61 67 20 73 6f 20 74  pragma flag so t
265d8 68 61 74 20 61 20 56 41 43 55 55 4d 20 77 69 6c  hat a VACUUM wil
265d9 6c 0a 20 20 2a 2a 20 6e 6f 74 20 64 6f 77 6e 67  l.  ** not downg
265da 72 61 64 65 20 74 68 65 20 64 61 74 61 62 61 73  rade the databas
265db 65 20 61 6e 64 20 74 68 75 73 20 69 6e 76 61 6c  e and thus inval
265dc 69 64 61 74 65 20 61 6e 79 20 64 65 73 63 65 6e  idate any descen
265dd 64 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 69 63 65  ding.  ** indice
265de 73 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20  s that the user 
265df 6d 69 67 68 74 20 68 61 76 65 20 63 72 65 61 74  might have creat
265e0 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  ed..  */.  if( i
265e1 44 62 3d 3d 30 20 26 26 20 6d 65 74 61 5b 31 5d  Db==0 && meta[1]
265e2 3e 3d 34 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66  >=4 ){.    db->f
265e3 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f  lags &= ~SQLITE_
265e4 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 3b 0a 20  LegacyFileFmt;. 
265e5 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68   }..  /* Read th
265e6 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61  e schema informa
265e7 74 69 6f 6e 20 6f 75 74 20 6f 66 20 74 68 65 20  tion out of the 
265e8 73 63 68 65 6d 61 20 74 61 62 6c 65 73 0a 20 20  schema tables.  
265e9 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  */.  assert( db-
265ea 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a 20 20  >init.busy );.  
265eb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45  if( rc==SQLITE_E
265ec 4d 50 54 59 20 29 7b 0a 20 20 20 20 2f 2a 20 46  MPTY ){.    /* F
265ed 6f 72 20 61 6e 20 65 6d 70 74 79 20 64 61 74 61  or an empty data
265ee 62 61 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e  base, there is n
265ef 6f 74 68 69 6e 67 20 74 6f 20 72 65 61 64 20 2a  othing to read *
265f0 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  /.    rc = SQLIT
265f1 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
265f2 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20     char *zSql;. 
265f3 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65     zSql = sqlite
265f4 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20  3MPrintf(db, .  
265f5 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61        "SELECT na
265f6 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71  me, rootpage, sq
265f7 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 22 2c  l FROM '%q'.%s",
265f8 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62  .        db->aDb
265f9 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61  [iDb].zName, zMa
265fa 73 74 65 72 4e 61 6d 65 29 3b 0a 20 20 20 20 28  sterName);.    (
265fb 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
265fc 74 79 4f 66 66 28 64 62 29 3b 0a 23 69 66 6e 64  tyOff(db);.#ifnd
265fd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
265fe 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20  UTHORIZATION.   
265ff 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 28 2a 78   {.      int (*x
26600 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  Auth)(void*,int,
26601 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
26602 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
26603 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  ar*,const char*)
26604 3b 0a 20 20 20 20 20 20 78 41 75 74 68 20 3d 20  ;.      xAuth = 
26605 64 62 2d 3e 78 41 75 74 68 3b 0a 20 20 20 20 20  db->xAuth;.     
26606 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a   db->xAuth = 0;.
26607 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20  #endif.      rc 
26608 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
26609 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33  b, zSql, sqlite3
2660a 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69  InitCallback, &i
2660b 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 23 69 66  nitData, 0);.#if
2660c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2660d 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
2660e 20 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d       db->xAuth =
2660f 20 78 41 75 74 68 3b 0a 20 20 20 20 7d 0a 23 65   xAuth;.    }.#e
26610 6e 64 69 66 0a 20 20 20 20 69 66 28 20 72 63 3d  ndif.    if( rc=
26611 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20  =SQLITE_OK ) rc 
26612 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20  = initData.rc;. 
26613 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
26614 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
26615 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
26616 64 62 2c 20 7a 53 71 6c 29 3b 0a 23 69 66 6e 64  db, zSql);.#ifnd
26617 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
26618 4e 41 4c 59 5a 45 0a 20 20 20 20 69 66 28 20 72  NALYZE.    if( r
26619 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2661a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 6e 61        sqlite3Ana
2661b 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 69 44  lysisLoad(db, iD
2661c 62 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  b);.    }.#endif
2661d 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d  .  }.  if( db->m
2661e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2661f 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
26620 4f 4d 45 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65  OMEM;.    sqlite
26621 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
26622 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d  hema(db, 0);.  }
26623 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
26624 45 5f 4f 4b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61  E_OK || (db->fla
26625 67 73 26 53 51 4c 49 54 45 5f 52 65 63 6f 76 65  gs&SQLITE_Recove
26626 72 79 4d 6f 64 65 29 29 7b 0a 20 20 20 20 2f 2a  ryMode)){.    /*
26627 20 42 6c 61 63 6b 20 6d 61 67 69 63 3a 20 49 66   Black magic: If
26628 20 74 68 65 20 53 51 4c 49 54 45 5f 52 65 63 6f   the SQLITE_Reco
26629 76 65 72 79 4d 6f 64 65 20 66 6c 61 67 20 69 73  veryMode flag is
2662a 20 73 65 74 2c 20 74 68 65 6e 20 63 6f 6e 73 69   set, then consi
2662b 64 65 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  der.    ** the s
2662c 63 68 65 6d 61 20 6c 6f 61 64 65 64 2c 20 65 76  chema loaded, ev
2662d 65 6e 20 69 66 20 65 72 72 6f 72 73 20 6f 63 63  en if errors occ
2662e 75 72 72 65 64 2e 20 49 6e 20 74 68 69 73 20 73  urred. In this s
2662f 69 74 75 61 74 69 6f 6e 20 74 68 65 20 0a 20 20  ituation the .  
26630 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 73 71 6c    ** current sql
26631 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 6f  ite3_prepare() o
26632 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 66 61  peration will fa
26633 69 6c 2c 20 62 75 74 20 74 68 65 20 66 6f 6c 6c  il, but the foll
26634 6f 77 69 6e 67 20 6f 6e 65 0a 20 20 20 20 2a 2a  owing one.    **
26635 20 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f   will attempt to
26636 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 73 75 70   compile the sup
26637 70 6c 69 65 64 20 73 74 61 74 65 6d 65 6e 74 20  plied statement 
26638 61 67 61 69 6e 73 74 20 77 68 61 74 65 76 65 72  against whatever
26639 20 73 75 62 73 65 74 0a 20 20 20 20 2a 2a 20 6f   subset.    ** o
2663a 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 61 73  f the schema was
2663b 20 6c 6f 61 64 65 64 20 62 65 66 6f 72 65 20 74   loaded before t
2663c 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65  he error occurre
2663d 64 2e 20 54 68 65 20 70 72 69 6d 61 72 79 0a 20  d. The primary. 
2663e 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66     ** purpose of
2663f 20 74 68 69 73 20 69 73 20 74 6f 20 61 6c 6c 6f   this is to allo
26640 77 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  w access to the 
26641 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
26642 62 6c 65 0a 20 20 20 20 2a 2a 20 65 76 65 6e 20  ble.    ** even 
26643 77 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74  when its content
26644 73 20 68 61 76 65 20 62 65 65 6e 20 63 6f 72 72  s have been corr
26645 75 70 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  upted..    */.  
26646 20 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28    DbSetProperty(
26647 64 62 2c 20 69 44 62 2c 20 44 42 5f 53 63 68 65  db, iDb, DB_Sche
26648 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20 20 72  maLoaded);.    r
26649 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
2664a 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65   }..  /* Jump he
2664b 72 65 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 20  re for an error 
2664c 74 68 61 74 20 6f 63 63 75 72 73 20 61 66 74 65  that occurs afte
2664d 72 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61  r successfully a
2664e 6c 6c 6f 63 61 74 69 6e 67 0a 20 20 2a 2a 20 63  llocating.  ** c
2664f 75 72 4d 61 69 6e 20 61 6e 64 20 63 61 6c 6c 69  urMain and calli
26650 6e 67 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  ng sqlite3BtreeE
26651 6e 74 65 72 28 29 2e 20 46 6f 72 20 61 6e 20 65  nter(). For an e
26652 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73  rror that occurs
26653 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 61  .  ** before tha
26654 74 20 70 6f 69 6e 74 2c 20 6a 75 6d 70 20 74 6f  t point, jump to
26655 20 65 72 72 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f   error_out..  */
26656 0a 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f  .initone_error_o
26657 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 42 74 72  ut:.  sqlite3Btr
26658 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 63 75  eeCloseCursor(cu
26659 72 4d 61 69 6e 29 3b 0a 20 20 73 71 6c 69 74 65  rMain);.  sqlite
2665a 33 5f 66 72 65 65 28 63 75 72 4d 61 69 6e 29 3b  3_free(curMain);
2665b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
2665c 65 61 76 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a  eave(pDb->pBt);.
2665d 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69 66  .error_out:.  if
2665e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
2665f 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45  EM || rc==SQLITE
26660 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
26661 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
26662 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  iled = 1;.  }.  
26663 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
26664 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
26665 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ll database file
26666 73 20 2d 20 74 68 65 20 6d 61 69 6e 20 64 61 74  s - the main dat
26667 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20  abase file, the 
26668 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20  file.** used to 
26669 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72 79 20  store temporary 
2666a 74 61 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79 20  tables, and any 
2666b 61 64 64 69 74 69 6f 6e 61 6c 20 64 61 74 61 62  additional datab
2666c 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65  ase files.** cre
2666d 61 74 65 64 20 75 73 69 6e 67 20 41 54 54 41 43  ated using ATTAC
2666e 48 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 52  H statements.  R
2666f 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20  eturn a success 
26670 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a 2a 2a 20  code.  If an.** 
26671 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 72  error occurs, wr
26672 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
26673 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72 72  sage into *pzErr
26674 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72  Msg..**.** After
26675 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20 69   a database is i
26676 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 20  nitialized, the 
26677 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20  DB_SchemaLoaded 
26678 62 69 74 20 69 73 20 73 65 74 0a 2a 2a 20 62 69  bit is set.** bi
26679 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  t is set in the 
2667a 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74  flags field of t
2667b 68 65 20 44 62 20 73 74 72 75 63 74 75 72 65 2e  he Db structure.
2667c 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
2667d 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20 6f 66 20  .** file was of 
2667e 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20 74 68 65  zero-length, the
2667f 6e 20 74 68 65 20 44 42 5f 45 6d 70 74 79 20 66  n the DB_Empty f
26680 6c 61 67 20 69 73 20 61 6c 73 6f 20 73 65 74 2e  lag is also set.
26681 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
26682 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
26683 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  it(sqlite3 *db, 
26684 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29  char **pzErrMsg)
26685 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a 20  {.  int i, rc;. 
26686 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69 6e 74 65   int commit_inte
26687 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e 66 6c 61  rnal = !(db->fla
26688 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
26689 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a 20 20 61  Changes);.  .  a
2668a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
2668b 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
2668c 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62  tex) );.  if( db
2668d 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20 72 65  ->init.busy ) re
2668e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2668f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
26690 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  ;.  db->init.bus
26691 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69 3d 30  y = 1;.  for(i=0
26692 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
26693 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  && i<db->nDb; i+
26694 2b 29 7b 0a 20 20 20 20 69 66 28 20 44 62 48 61  +){.    if( DbHa
26695 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 2c  sProperty(db, i,
26696 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
26697 29 20 7c 7c 20 69 3d 3d 31 20 29 20 63 6f 6e 74  ) || i==1 ) cont
26698 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d 20 73  inue;.    rc = s
26699 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28 64 62  qlite3InitOne(db
2669a 2c 20 69 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  , i, pzErrMsg);.
2669b 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
2669c 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
2669d 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2669e 62 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  b, i);.    }.  }
2669f 0a 0a 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20  ..  /* Once all 
266a0 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62 61  the other databa
266a1 73 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ses have been in
266a2 69 74 69 61 6c 69 73 65 64 2c 20 6c 6f 61 64 20  itialised, load 
266a3 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20  the schema.  ** 
266a4 66 6f 72 20 74 68 65 20 54 45 4d 50 20 64 61 74  for the TEMP dat
266a5 61 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 6c  abase. This is l
266a6 6f 61 64 65 64 20 6c 61 73 74 2c 20 61 73 20 74  oaded last, as t
266a7 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
266a8 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6d 61 79  .  ** schema may
266a9 20 63 6f 6e 74 61 69 6e 20 72 65 66 65 72 65 6e   contain referen
266aa 63 65 73 20 74 6f 20 6f 62 6a 65 63 74 73 20 69  ces to objects i
266ab 6e 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  n other database
266ac 73 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  s..  */.#ifndef 
266ad 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50  SQLITE_OMIT_TEMP
266ae 44 42 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  DB.  if( rc==SQL
266af 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 6e 44  ITE_OK && db->nD
266b0 62 3e 31 20 26 26 20 21 44 62 48 61 73 50 72 6f  b>1 && !DbHasPro
266b1 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f  perty(db, 1, DB_
266b2 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b  SchemaLoaded) ){
266b3 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
266b4 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 31 2c 20  3InitOne(db, 1, 
266b5 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69  pzErrMsg);.    i
266b6 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73  f( rc ){.      s
266b7 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
266b8 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 31 29  nalSchema(db, 1)
266b9 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
266ba 69 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62  if..  db->init.b
266bb 75 73 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 72  usy = 0;.  if( r
266bc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
266bd 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20  commit_internal 
266be 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  ){.    sqlite3Co
266bf 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e  mmitInternalChan
266c0 67 65 73 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20  ges(db);.  }..  
266c1 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f  return rc; .}../
266c2 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
266c3 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  e is a no-op if 
266c4 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
266c5 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 69  ema is already i
266c6 6e 69 74 69 61 6c 69 73 65 64 2e 0a 2a 2a 20 4f  nitialised..** O
266c7 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73 63  therwise, the sc
266c8 68 65 6d 61 20 69 73 20 6c 6f 61 64 65 64 2e 20  hema is loaded. 
266c9 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  An error code is
266ca 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
266cb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
266cc 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
266cd 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ma(Parse *pParse
266ce 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
266cf 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74  LITE_OK;.  sqlit
266d0 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
266d1 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73  >db;.  assert( s
266d2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
266d3 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
266d4 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
266d5 62 75 73 79 20 29 7b 0a 20 20 20 20 72 63 20 3d  busy ){.    rc =
266d6 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c   sqlite3Init(db,
266d7 20 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73   &pParse->zErrMs
266d8 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  g);.  }.  if( rc
266d9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
266da 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
266db 72 63 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  rc;.    pParse->
266dc 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  nErr++;.  }.  re
266dd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
266de 2a 2a 20 43 68 65 63 6b 20 73 63 68 65 6d 61 20  ** Check schema 
266df 63 6f 6f 6b 69 65 73 20 69 6e 20 61 6c 6c 20 64  cookies in all d
266e0 61 74 61 62 61 73 65 73 2e 20 20 49 66 20 61 6e  atabases.  If an
266e1 79 20 63 6f 6f 6b 69 65 20 69 73 20 6f 75 74 0a  y cookie is out.
266e2 2a 2a 20 6f 66 20 64 61 74 65 2c 20 72 65 74 75  ** of date, retu
266e3 72 6e 20 30 2e 20 20 49 66 20 61 6c 6c 20 73 63  rn 0.  If all sc
266e4 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 61 72 65  hema cookies are
266e5 20 63 75 72 72 65 6e 74 2c 20 72 65 74 75 72 6e   current, return
266e6 20 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   1..*/.static in
266e7 74 20 73 63 68 65 6d 61 49 73 56 61 6c 69 64 28  t schemaIsValid(
266e8 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
266e9 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 72  int iDb;.  int r
266ea 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 63  c;.  BtCursor *c
266eb 75 72 54 65 6d 70 3b 0a 20 20 69 6e 74 20 63 6f  urTemp;.  int co
266ec 6f 6b 69 65 3b 0a 20 20 69 6e 74 20 61 6c 6c 4f  okie;.  int allO
266ed 6b 20 3d 20 31 3b 0a 0a 20 20 63 75 72 54 65 6d  k = 1;..  curTem
266ee 70 20 3d 20 28 42 74 43 75 72 73 6f 72 20 2a 29  p = (BtCursor *)
266ef 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 71  sqlite3Malloc(sq
266f0 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
266f1 53 69 7a 65 28 29 29 3b 0a 20 20 69 66 28 20 63  Size());.  if( c
266f2 75 72 54 65 6d 70 20 29 7b 0a 20 20 20 20 61 73  urTemp ){.    as
266f3 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
266f4 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
266f5 65 78 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 69  ex) );.    for(i
266f6 44 62 3d 30 3b 20 61 6c 6c 4f 6b 20 26 26 20 69  Db=0; allOk && i
266f7 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b  Db<db->nDb; iDb+
266f8 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20  +){.      Btree 
266f9 2a 70 42 74 3b 0a 20 20 20 20 20 20 70 42 74 20  *pBt;.      pBt 
266fa 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
266fb 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42  Bt;.      if( pB
266fc 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  t==0 ) continue;
266fd 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 63 75  .      memset(cu
266fe 72 54 65 6d 70 2c 20 30 2c 20 73 71 6c 69 74 65  rTemp, 0, sqlite
266ff 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
26700 28 29 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ());.      rc = 
26701 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
26702 6f 72 28 70 42 74 2c 20 4d 41 53 54 45 52 5f 52  or(pBt, MASTER_R
26703 4f 4f 54 2c 20 30 2c 20 30 2c 20 63 75 72 54 65  OOT, 0, 0, curTe
26704 6d 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  mp);.      if( r
26705 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
26706 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
26707 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
26708 28 70 42 74 2c 20 31 2c 20 28 75 33 32 20 2a 29  (pBt, 1, (u32 *)
26709 26 63 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20  &cookie);.      
2670a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2670b 5f 4f 4b 20 26 26 20 63 6f 6f 6b 69 65 21 3d 64  _OK && cookie!=d
2670c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
2670d 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b  ema->schema_cook
2670e 69 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ie ){.          
2670f 61 6c 6c 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 20  allOk = 0;.     
26710 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
26711 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75  ite3BtreeCloseCu
26712 72 73 6f 72 28 63 75 72 54 65 6d 70 29 3b 0a 20  rsor(curTemp);. 
26713 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
26714 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
26715 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  M || rc==SQLITE_
26716 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  IOERR_NOMEM ){. 
26717 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
26718 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
26719 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
2671a 71 6c 69 74 65 33 5f 66 72 65 65 28 63 75 72 54  qlite3_free(curT
2671b 65 6d 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  emp);.  }else{. 
2671c 20 20 20 61 6c 6c 4f 6b 20 3d 20 30 3b 0a 20 20     allOk = 0;.  
2671d 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2671e 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72  ed = 1;.  }..  r
2671f 65 74 75 72 6e 20 61 6c 6c 4f 6b 3b 0a 7d 0a 0a  eturn allOk;.}..
26720 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
26721 73 63 68 65 6d 61 20 70 6f 69 6e 74 65 72 20 69  schema pointer i
26722 6e 74 6f 20 74 68 65 20 69 44 62 20 69 6e 64 65  nto the iDb inde
26723 78 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73  x that indicates
26724 0a 2a 2a 20 77 68 69 63 68 20 64 61 74 61 62 61  .** which databa
26725 73 65 20 66 69 6c 65 20 69 6e 20 64 62 2d 3e 61  se file in db->a
26726 44 62 5b 5d 20 74 68 65 20 73 63 68 65 6d 61 20  Db[] the schema 
26727 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a  refers to..**.**
26728 20 49 66 20 74 68 65 20 73 61 6d 65 20 64 61 74   If the same dat
26729 61 62 61 73 65 20 69 73 20 61 74 74 61 63 68 65  abase is attache
2672a 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  d more than once
2672b 2c 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61  , the first.** a
2672c 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
2672d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
2672e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2672f 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d  int sqlite3Schem
26730 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33  aToIndex(sqlite3
26731 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 70 53   *db, Schema *pS
26732 63 68 65 6d 61 29 7b 0a 20 20 69 6e 74 20 69 20  chema){.  int i 
26733 3d 20 2d 31 30 30 30 30 30 30 3b 0a 0a 20 20 2f  = -1000000;..  /
26734 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69 73 20  * If pSchema is 
26735 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 74 75 72  NULL, then retur
26736 6e 20 2d 31 30 30 30 30 30 30 2e 20 54 68 69 73  n -1000000. This
26737 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f   happens when co
26738 64 65 20 69 6e 20 0a 20 20 2a 2a 20 65 78 70 72  de in .  ** expr
26739 2e 63 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20  .c is trying to 
2673a 72 65 73 6f 6c 76 65 20 61 20 72 65 66 65 72 65  resolve a refere
2673b 6e 63 65 20 74 6f 20 61 20 74 72 61 6e 73 69 65  nce to a transie
2673c 6e 74 20 74 61 62 6c 65 20 28 69 2e 65 2e 20 6f  nt table (i.e. o
2673d 6e 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20  ne.  ** created 
2673e 62 79 20 61 20 73 75 62 2d 73 65 6c 65 63 74 29  by a sub-select)
2673f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
26740 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
26741 6f 66 20 74 68 69 73 20 0a 20 20 2a 2a 20 66 75  of this .  ** fu
26742 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65  nction should ne
26743 76 65 72 20 62 65 20 75 73 65 64 2e 0a 20 20 2a  ver be used..  *
26744 2a 0a 20 20 2a 2a 20 57 65 20 72 65 74 75 72 6e  *.  ** We return
26745 20 2d 31 30 30 30 30 30 30 20 69 6e 73 74 65 61   -1000000 instea
26746 64 20 6f 66 20 74 68 65 20 6d 6f 72 65 20 75 73  d of the more us
26747 75 61 6c 20 2d 31 20 73 69 6d 70 6c 79 20 62 65  ual -1 simply be
26748 63 61 75 73 65 20 75 73 69 6e 67 0a 20 20 2a 2a  cause using.  **
26749 20 2d 31 30 30 30 30 30 30 20 61 73 20 74 68 65   -1000000 as the
2674a 20 69 6e 63 6f 72 72 65 63 74 20 69 6e 64 65 78   incorrect index
2674b 20 69 6e 74 6f 20 64 62 2d 3e 61 44 62 5b 5d 20   into db->aDb[] 
2674c 69 73 20 6d 75 63 68 20 0a 20 20 2a 2a 20 6d 6f  is much .  ** mo
2674d 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 63 61 75  re likely to cau
2674e 73 65 20 61 20 73 65 67 66 61 75 6c 74 20 74 68  se a segfault th
2674f 61 6e 20 2d 31 20 28 6f 66 20 63 6f 75 72 73 65  an -1 (of course
26750 20 74 68 65 72 65 20 61 72 65 20 61 73 73 65 72   there are asser
26751 74 28 29 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  t().  ** stateme
26752 6e 74 73 20 74 6f 6f 2c 20 62 75 74 20 69 74 20  nts too, but it 
26753 6e 65 76 65 72 20 68 75 72 74 73 20 74 6f 20 70  never hurts to p
26754 6c 61 79 20 74 68 65 20 6f 64 64 73 29 2e 0a 20  lay the odds).. 
26755 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
26756 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
26757 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
26758 20 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a   if( pSchema ){.
26759 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57      for(i=0; ALW
2675a 41 59 53 28 69 3c 64 62 2d 3e 6e 44 62 29 3b 20  AYS(i<db->nDb); 
2675b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
2675c 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65  db->aDb[i].pSche
2675d 6d 61 3d 3d 70 53 63 68 65 6d 61 20 29 7b 0a 20  ma==pSchema ){. 
2675e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2675f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
26760 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
26761 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7d  i<db->nDb );.  }
26762 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a  .  return i;.}..
26763 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68  /*.** Compile th
26764 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  e UTF-8 encoded 
26765 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53  SQL statement zS
26766 71 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d  ql into a statem
26767 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73  ent handle..*/.s
26768 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
26769 33 50 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69  3Prepare(.  sqli
2676a 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2676b 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2676c 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63  e handle. */.  c
2676d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
2676e 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d           /* UTF-
2676f 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74  8 encoded SQL st
26770 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e  atement. */.  in
26771 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20  t nBytes,       
26772 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
26773 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
26774 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  es. */.  int sav
26775 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20  eSqlFlag,       
26776 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f     /* True to co
26777 70 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f  py SQL text into
26778 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d   the sqlite3_stm
26779 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  t */.  sqlite3_s
2677a 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20  tmt **ppStmt,   
2677b 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74   /* OUT: A point
2677c 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72  er to the prepar
2677d 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ed statement */.
2677e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
2677f 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f  zTail       /* O
26780 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  UT: End of parse
26781 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20  d string */.){. 
26782 20 50 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20   Parse sParse;. 
26783 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d   char *zErrMsg =
26784 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   0;.  int rc = S
26785 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
26786 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 70  i;..  assert( pp
26787 53 74 6d 74 20 29 3b 0a 20 20 2a 70 70 53 74 6d  Stmt );.  *ppStm
26788 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c  t = 0;.  if( sql
26789 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
2678a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2678b 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
2678c 7d 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  }.  assert( !db-
2678d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
2678e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2678f 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
26790 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f  ->mutex) );..  /
26791 2a 20 43 68 65 63 6b 20 74 6f 20 76 65 72 69 66  * Check to verif
26792 79 20 74 68 61 74 20 69 74 20 69 73 20 70 6f 73  y that it is pos
26793 73 69 62 6c 65 20 74 6f 20 67 65 74 20 61 20 72  sible to get a r
26794 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 0a  ead lock on all.
26795 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63    ** database sc
26796 68 65 6d 61 73 2e 20 20 54 68 65 20 69 6e 61 62  hemas.  The inab
26797 69 6c 69 74 79 20 74 6f 20 67 65 74 20 61 20 72  ility to get a r
26798 65 61 64 20 6c 6f 63 6b 20 69 6e 64 69 63 61 74  ead lock indicat
26799 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 73 6f 6d  es that.  ** som
2679a 65 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  e other database
2679b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68   connection is h
2679c 6f 6c 64 69 6e 67 20 61 20 77 72 69 74 65 2d 6c  olding a write-l
2679d 6f 63 6b 2c 20 77 68 69 63 68 20 69 6e 0a 20 20  ock, which in.  
2679e 2a 2a 20 74 75 72 6e 20 6d 65 61 6e 73 20 74 68  ** turn means th
2679f 61 74 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e  at the other con
267a0 6e 65 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65  nection has made
267a1 20 75 6e 63 6f 6d 6d 69 74 74 65 64 20 63 68 61   uncommitted cha
267a2 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65  nges.  ** to the
267a3 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2a 0a 20 20   schema..  **.  
267a4 2a 2a 20 57 65 72 65 20 77 65 20 74 6f 20 70 72  ** Were we to pr
267a5 6f 63 65 65 64 20 61 6e 64 20 70 72 65 70 61 72  oceed and prepar
267a6 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  e the statement 
267a7 61 67 61 69 6e 73 74 20 74 68 65 20 75 6e 63 6f  against the unco
267a8 6d 6d 69 74 74 65 64 0a 20 20 2a 2a 20 73 63 68  mmitted.  ** sch
267a9 65 6d 61 20 63 68 61 6e 67 65 73 20 61 6e 64 20  ema changes and 
267aa 69 66 20 74 68 6f 73 65 20 73 63 68 65 6d 61 20  if those schema 
267ab 63 68 61 6e 67 65 73 20 61 72 65 20 73 75 62 73  changes are subs
267ac 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 0a  equently rolled.
267ad 20 20 2a 2a 20 62 61 63 6b 20 61 6e 64 20 64 69    ** back and di
267ae 66 66 65 72 65 6e 74 20 63 68 61 6e 67 65 73 20  fferent changes 
267af 61 72 65 20 6d 61 64 65 20 69 6e 20 74 68 65 69  are made in thei
267b0 72 20 70 6c 61 63 65 2c 20 74 68 65 6e 20 77 68  r place, then wh
267b1 65 6e 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65  en this.  ** pre
267b2 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
267b3 67 6f 65 73 20 74 6f 20 72 75 6e 20 74 68 65 20  goes to run the 
267b4 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 77 6f  schema cookie wo
267b5 75 6c 64 20 66 61 69 6c 20 74 6f 20 64 65 74 65  uld fail to dete
267b6 63 74 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65  ct.  ** the sche
267b7 6d 61 20 63 68 61 6e 67 65 2e 20 20 44 69 73 61  ma change.  Disa
267b8 73 74 65 72 20 77 6f 75 6c 64 20 66 6f 6c 6c 6f  ster would follo
267b9 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  w..  **.  ** Thi
267ba 73 20 74 68 72 65 61 64 20 69 73 20 63 75 72 72  s thread is curr
267bb 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 6d 75  ently holding mu
267bc 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20 42 74 72  texes on all Btr
267bd 65 65 73 20 28 62 65 63 61 75 73 65 0a 20 20 2a  ees (because.  *
267be 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
267bf 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20  BtreeEnterAll() 
267c0 69 6e 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e  in sqlite3LockAn
267c1 64 50 72 65 70 61 72 65 28 29 29 20 73 6f 20 69  dPrepare()) so i
267c2 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f  t.  ** is not po
267c3 73 73 69 62 6c 65 20 66 6f 72 20 61 6e 6f 74 68  ssible for anoth
267c4 65 72 20 74 68 72 65 61 64 20 74 6f 20 73 74 61  er thread to sta
267c5 72 74 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20  rt a new schema 
267c6 63 68 61 6e 67 65 0a 20 20 2a 2a 20 77 68 69 6c  change.  ** whil
267c7 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
267c8 73 20 72 75 6e 6e 69 6e 67 2e 20 20 48 65 6e 63  s running.  Henc
267c9 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65  e, we do not nee
267ca 64 20 74 6f 20 68 6f 6c 64 20 0a 20 20 2a 2a 20  d to hold .  ** 
267cb 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 63 68  locks on the sch
267cc 65 6d 61 2c 20 77 65 20 6a 75 73 74 20 6e 65 65  ema, we just nee
267cd 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 6e  d to make sure n
267ce 6f 62 6f 64 79 20 65 6c 73 65 20 69 73 20 0a 20  obody else is . 
267cf 20 2a 2a 20 68 6f 6c 64 69 6e 67 20 74 68 65 6d   ** holding them
267d0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ..  **.  ** Note
267d1 20 74 68 61 74 20 73 65 74 74 69 6e 67 20 52 45   that setting RE
267d2 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 6f  AD_UNCOMMITTED o
267d3 76 65 72 72 69 64 65 73 20 6d 6f 73 74 20 6c 6f  verrides most lo
267d4 63 6b 20 64 65 74 65 63 74 69 6f 6e 2c 0a 20 20  ck detection,.  
267d5 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 20 2a  ** but it does *
267d6 6e 6f 74 2a 20 6f 76 65 72 72 69 64 65 20 73 63  not* override sc
267d7 68 65 6d 61 20 6c 6f 63 6b 20 64 65 74 65 63 74  hema lock detect
267d8 69 6f 6e 2c 20 73 6f 20 74 68 69 73 20 61 6c 6c  ion, so this all
267d9 20 73 74 69 6c 6c 0a 20 20 2a 2a 20 77 6f 72 6b   still.  ** work
267da 73 20 65 76 65 6e 20 69 66 20 52 45 41 44 5f 55  s even if READ_U
267db 4e 43 4f 4d 4d 49 54 54 45 44 20 69 73 20 73 65  NCOMMITTED is se
267dc 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  t..  */.  for(i=
267dd 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
267de 2b 29 20 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  +) {.    Btree *
267df 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
267e0 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42  .pBt;.    if( pB
267e1 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t ){.      asser
267e2 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
267e3 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 29 20 29  oldsMutex(pBt) )
267e4 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
267e5 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c  ite3BtreeSchemaL
267e6 6f 63 6b 65 64 28 70 42 74 29 3b 0a 20 20 20 20  ocked(pBt);.    
267e7 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
267e8 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
267e9 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  zDb = db->aDb[i]
267ea 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
267eb 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
267ec 20 72 63 2c 20 22 64 61 74 61 62 61 73 65 20 73   rc, "database s
267ed 63 68 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a  chema is locked:
267ee 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20   %s", zDb);.    
267ef 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
267f0 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
267f1 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
267f2 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
267f3 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
267f4 74 74 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  tted );.        
267f5 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70  return sqlite3Ap
267f6 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
267f7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
267f8 0a 20 20 0a 20 20 6d 65 6d 73 65 74 28 26 73 50  .  .  memset(&sP
267f9 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  arse, 0, sizeof(
267fa 73 50 61 72 73 65 29 29 3b 0a 20 20 73 50 61 72  sParse));.  sPar
267fb 73 65 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66  se.db = db;.  if
267fc 28 20 6e 42 79 74 65 73 3e 3d 30 20 26 26 20 28  ( nBytes>=0 && (
267fd 6e 42 79 74 65 73 3d 3d 30 20 7c 7c 20 7a 53 71  nBytes==0 || zSq
267fe 6c 5b 6e 42 79 74 65 73 2d 31 5d 21 3d 30 29 20  l[nBytes-1]!=0) 
267ff 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71  ){.    char *zSq
26800 6c 43 6f 70 79 3b 0a 20 20 20 20 69 6e 74 20 6d  lCopy;.    int m
26801 78 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69  xLen = db->aLimi
26802 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53  t[SQLITE_LIMIT_S
26803 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20  QL_LENGTH];.    
26804 69 66 28 20 6e 42 79 74 65 73 3e 6d 78 4c 65 6e  if( nBytes>mxLen
26805 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
26806 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
26807 45 5f 54 4f 4f 42 49 47 2c 20 22 73 74 61 74 65  E_TOOBIG, "state
26808 6d 65 6e 74 20 74 6f 6f 20 6c 6f 6e 67 22 29 3b  ment too long");
26809 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c  .      (void)sql
2680a 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
2680b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
2680c 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2680d 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  b, SQLITE_TOOBIG
2680e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71  );.    }.    zSq
2680f 6c 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 44  lCopy = sqlite3D
26810 62 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 53 71  bStrNDup(db, zSq
26811 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  l, nBytes);.    
26812 69 66 28 20 7a 53 71 6c 43 6f 70 79 20 29 7b 0a  if( zSqlCopy ){.
26813 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e        sqlite3Run
26814 50 61 72 73 65 72 28 26 73 50 61 72 73 65 2c 20  Parser(&sParse, 
26815 7a 53 71 6c 43 6f 70 79 2c 20 26 7a 45 72 72 4d  zSqlCopy, &zErrM
26816 73 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  sg);.      sqlit
26817 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71  e3DbFree(db, zSq
26818 6c 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 73 50  lCopy);.      sP
26819 61 72 73 65 2e 7a 54 61 69 6c 20 3d 20 26 7a 53  arse.zTail = &zS
2681a 71 6c 5b 73 50 61 72 73 65 2e 7a 54 61 69 6c 2d  ql[sParse.zTail-
2681b 7a 53 71 6c 43 6f 70 79 5d 3b 0a 20 20 20 20 7d  zSqlCopy];.    }
2681c 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 50 61 72  else{.      sPar
2681d 73 65 2e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  se.zTail = &zSql
2681e 5b 6e 42 79 74 65 73 5d 3b 0a 20 20 20 20 7d 0a  [nBytes];.    }.
2681f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
26820 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 26 73  ite3RunParser(&s
26821 50 61 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45  Parse, zSql, &zE
26822 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 0a 20 20 69  rrMsg);.  }..  i
26823 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
26824 6c 65 64 20 29 7b 0a 20 20 20 20 73 50 61 72 73  led ){.    sPars
26825 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  e.rc = SQLITE_NO
26826 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  MEM;.  }.  if( s
26827 50 61 72 73 65 2e 72 63 3d 3d 53 51 4c 49 54 45  Parse.rc==SQLITE
26828 5f 44 4f 4e 45 20 29 20 73 50 61 72 73 65 2e 72  _DONE ) sParse.r
26829 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
2682a 20 69 66 28 20 73 50 61 72 73 65 2e 63 68 65 63   if( sParse.chec
2682b 6b 53 63 68 65 6d 61 20 26 26 20 21 73 63 68 65  kSchema && !sche
2682c 6d 61 49 73 56 61 6c 69 64 28 64 62 29 20 29 7b  maIsValid(db) ){
2682d 0a 20 20 20 20 73 50 61 72 73 65 2e 72 63 20 3d  .    sParse.rc =
2682e 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a   SQLITE_SCHEMA;.
2682f 20 20 7d 0a 20 20 69 66 28 20 73 50 61 72 73 65    }.  if( sParse
26830 2e 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45  .rc==SQLITE_SCHE
26831 4d 41 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  MA ){.    sqlite
26832 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
26833 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d  hema(db, 0);.  }
26834 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
26835 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73  cFailed ){.    s
26836 50 61 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54  Parse.rc = SQLIT
26837 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69  E_NOMEM;.  }.  i
26838 66 28 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20  f( pzTail ){.   
26839 20 2a 70 7a 54 61 69 6c 20 3d 20 73 50 61 72 73   *pzTail = sPars
2683a 65 2e 7a 54 61 69 6c 3b 0a 20 20 7d 0a 20 20 72  e.zTail;.  }.  r
2683b 63 20 3d 20 73 50 61 72 73 65 2e 72 63 3b 0a 0a  c = sParse.rc;..
2683c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2683d 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66  MIT_EXPLAIN.  if
2683e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2683f 26 26 20 73 50 61 72 73 65 2e 70 56 64 62 65 20  && sParse.pVdbe 
26840 26 26 20 73 50 61 72 73 65 2e 65 78 70 6c 61 69  && sParse.explai
26841 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 73 50 61  n ){.    if( sPa
26842 72 73 65 2e 65 78 70 6c 61 69 6e 3d 3d 32 20 29  rse.explain==2 )
26843 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
26844 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 73 50  dbeSetNumCols(sP
26845 61 72 73 65 2e 70 56 64 62 65 2c 20 33 29 3b 0a  arse.pVdbe, 3);.
26846 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26847 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
26848 73 65 2e 70 56 64 62 65 2c 20 30 2c 20 43 4f 4c  se.pVdbe, 0, COL
26849 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 72 64 65  NAME_NAME, "orde
2684a 72 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  r", SQLITE_STATI
2684b 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
2684c 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2684d 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 31 2c  sParse.pVdbe, 1,
2684e 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
2684f 66 72 6f 6d 22 2c 20 53 51 4c 49 54 45 5f 53 54  from", SQLITE_ST
26850 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
26851 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
26852 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  me(sParse.pVdbe,
26853 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   2, COLNAME_NAME
26854 2c 20 22 64 65 74 61 69 6c 22 2c 20 53 51 4c 49  , "detail", SQLI
26855 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
26856 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
26857 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
26858 6c 73 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  ls(sParse.pVdbe,
26859 20 38 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   8);.      sqlit
2685a 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2685b 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 30  (sParse.pVdbe, 0
2685c 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2685d 22 61 64 64 72 22 2c 20 53 51 4c 49 54 45 5f 53  "addr", SQLITE_S
2685e 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
2685f 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
26860 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65  ame(sParse.pVdbe
26861 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 1, COLNAME_NAM
26862 45 2c 20 22 6f 70 63 6f 64 65 22 2c 20 53 51 4c  E, "opcode", SQL
26863 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
26864 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
26865 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e  tColName(sParse.
26866 70 56 64 62 65 2c 20 32 2c 20 43 4f 4c 4e 41 4d  pVdbe, 2, COLNAM
26867 45 5f 4e 41 4d 45 2c 20 22 70 31 22 2c 20 53 51  E_NAME, "p1", SQ
26868 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
26869 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2686a 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65  etColName(sParse
2686b 2e 70 56 64 62 65 2c 20 33 2c 20 43 4f 4c 4e 41  .pVdbe, 3, COLNA
2686c 4d 45 5f 4e 41 4d 45 2c 20 22 70 32 22 2c 20 53  ME_NAME, "p2", S
2686d 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2686e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2686f 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73  SetColName(sPars
26870 65 2e 70 56 64 62 65 2c 20 34 2c 20 43 4f 4c 4e  e.pVdbe, 4, COLN
26871 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 33 22 2c 20  AME_NAME, "p3", 
26872 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
26873 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26874 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
26875 73 65 2e 70 56 64 62 65 2c 20 35 2c 20 43 4f 4c  se.pVdbe, 5, COL
26876 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 34 22 2c  NAME_NAME, "p4",
26877 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
26878 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26879 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61  beSetColName(sPa
2687a 72 73 65 2e 70 56 64 62 65 2c 20 36 2c 20 43 4f  rse.pVdbe, 6, CO
2687b 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 35 22  LNAME_NAME, "p5"
2687c 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2687d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2687e 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50  dbeSetColName(sP
2687f 61 72 73 65 2e 70 56 64 62 65 2c 20 37 2c 20 43  arse.pVdbe, 7, C
26880 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 6f  OLNAME_NAME, "co
26881 6d 6d 65 6e 74 22 2c 20 53 51 4c 49 54 45 5f 53  mment", SQLITE_S
26882 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20  TATIC);.    }.  
26883 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  }.#endif..  if( 
26884 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
26885 28 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  (db) ){.    rc =
26886 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
26887 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 64    }..  assert( d
26888 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20  b->init.busy==0 
26889 7c 7c 20 73 61 76 65 53 71 6c 46 6c 61 67 3d 3d  || saveSqlFlag==
2688a 30 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 69  0 );.  if( db->i
2688b 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20  nit.busy==0 ){. 
2688c 20 20 20 56 64 62 65 20 2a 70 56 64 62 65 20 3d     Vdbe *pVdbe =
2688d 20 73 50 61 72 73 65 2e 70 56 64 62 65 3b 0a 20   sParse.pVdbe;. 
2688e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2688f 74 53 71 6c 28 70 56 64 62 65 2c 20 7a 53 71 6c  tSql(pVdbe, zSql
26890 2c 20 28 69 6e 74 29 28 73 50 61 72 73 65 2e 7a  , (int)(sParse.z
26891 54 61 69 6c 2d 7a 53 71 6c 29 2c 20 73 61 76 65  Tail-zSql), save
26892 53 71 6c 46 6c 61 67 29 3b 0a 20 20 7d 0a 20 20  SqlFlag);.  }.  
26893 69 66 28 20 73 50 61 72 73 65 2e 70 56 64 62 65  if( sParse.pVdbe
26894 20 26 26 20 28 72 63 21 3d 53 51 4c 49 54 45 5f   && (rc!=SQLITE_
26895 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  OK || db->malloc
26896 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 73  Failed) ){.    s
26897 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
26898 7a 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 29  ze(sParse.pVdbe)
26899 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 28 2a  ;.    assert(!(*
2689a 70 70 53 74 6d 74 29 29 3b 0a 20 20 7d 65 6c 73  ppStmt));.  }els
2689b 65 7b 0a 20 20 20 20 2a 70 70 53 74 6d 74 20 3d  e{.    *ppStmt =
2689c 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29   (sqlite3_stmt*)
2689d 73 50 61 72 73 65 2e 70 56 64 62 65 3b 0a 20 20  sParse.pVdbe;.  
2689e 7d 0a 0a 20 20 69 66 28 20 7a 45 72 72 4d 73 67  }..  if( zErrMsg
2689f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
268a0 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 25 73  rror(db, rc, "%s
268a1 22 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  ", zErrMsg);.   
268a2 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
268a3 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d  b, zErrMsg);.  }
268a4 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
268a5 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30  3Error(db, rc, 0
268a6 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73  );.  }..  rc = s
268a7 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
268a8 2c 20 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28  , rc);.  assert(
268a9 20 28 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b   (rc&db->errMask
268aa 29 3d 3d 72 63 20 29 3b 0a 20 20 72 65 74 75 72  )==rc );.  retur
268ab 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69  n rc;.}.static i
268ac 6e 74 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e  nt sqlite3LockAn
268ad 64 50 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69  dPrepare(.  sqli
268ae 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
268af 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
268b0 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63  e handle. */.  c
268b1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
268b2 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d           /* UTF-
268b3 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74  8 encoded SQL st
268b4 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e  atement. */.  in
268b5 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20  t nBytes,       
268b6 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
268b7 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
268b8 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  es. */.  int sav
268b9 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20  eSqlFlag,       
268ba 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f     /* True to co
268bb 70 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f  py SQL text into
268bc 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d   the sqlite3_stm
268bd 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  t */.  sqlite3_s
268be 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20  tmt **ppStmt,   
268bf 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74   /* OUT: A point
268c0 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72  er to the prepar
268c1 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ed statement */.
268c2 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
268c3 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f  zTail       /* O
268c4 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  UT: End of parse
268c5 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20  d string */.){. 
268c6 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 21   int rc;.  if( !
268c7 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
268c8 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  ckOk(db) ){.    
268c9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
268ca 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  SUSE;.  }.  sqli
268cb 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
268cc 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71  db->mutex);.  sq
268cd 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
268ce 6c 6c 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73  ll(db);.  rc = s
268cf 71 6c 69 74 65 33 50 72 65 70 61 72 65 28 64 62  qlite3Prepare(db
268d0 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20  , zSql, nBytes, 
268d1 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 70 53  saveSqlFlag, ppS
268d2 74 6d 74 2c 20 70 7a 54 61 69 6c 29 3b 0a 20 20  tmt, pzTail);.  
268d3 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
268d4 65 41 6c 6c 28 64 62 29 3b 0a 20 20 73 71 6c 69  eAll(db);.  sqli
268d5 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
268d6 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
268d7 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
268d8 2a 20 52 65 72 75 6e 20 74 68 65 20 63 6f 6d 70  * Rerun the comp
268d9 69 6c 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61  ilation of a sta
268da 74 65 6d 65 6e 74 20 61 66 74 65 72 20 61 20 73  tement after a s
268db 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a  chema change..**
268dc 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 74 65  .** If the state
268dd 6d 65 6e 74 20 69 73 20 73 75 63 63 65 73 73 66  ment is successf
268de 75 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 2c  ully recompiled,
268df 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
268e0 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a  K. Otherwise,.**
268e1 20 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   if the statemen
268e2 74 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 63 6f  t cannot be reco
268e3 6d 70 69 6c 65 64 20 62 65 63 61 75 73 65 20 61  mpiled because a
268e4 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f  nother connectio
268e5 6e 20 68 61 73 0a 2a 2a 20 6c 6f 63 6b 65 64 20  n has.** locked 
268e6 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 73 74  the sqlite3_mast
268e7 65 72 20 74 61 62 6c 65 2c 20 72 65 74 75 72 6e  er table, return
268e8 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 20   SQLITE_LOCKED. 
268e9 49 66 20 61 6e 79 20 6f 74 68 65 72 20 65 72 72  If any other err
268ea 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 72 65  or.** occurs, re
268eb 74 75 72 6e 20 53 51 4c 49 54 45 5f 53 43 48 45  turn SQLITE_SCHE
268ec 4d 41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  MA..*/.SQLITE_PR
268ed 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
268ee 33 52 65 70 72 65 70 61 72 65 28 56 64 62 65 20  3Reprepare(Vdbe 
268ef 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *p){.  int rc;. 
268f0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
268f1 4e 65 77 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  New;.  const cha
268f2 72 20 2a 7a 53 71 6c 3b 0a 20 20 73 71 6c 69 74  r *zSql;.  sqlit
268f3 65 33 20 2a 64 62 3b 0a 0a 20 20 61 73 73 65 72  e3 *db;..  asser
268f4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
268f5 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 56 64 62  _held(sqlite3Vdb
268f6 65 44 62 28 70 29 2d 3e 6d 75 74 65 78 29 20 29  eDb(p)->mutex) )
268f7 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74  ;.  zSql = sqlit
268f8 65 33 5f 73 71 6c 28 28 73 71 6c 69 74 65 33 5f  e3_sql((sqlite3_
268f9 73 74 6d 74 20 2a 29 70 29 3b 0a 20 20 61 73 73  stmt *)p);.  ass
268fa 65 72 74 28 20 7a 53 71 6c 21 3d 30 20 29 3b 20  ert( zSql!=0 ); 
268fb 20 2f 2a 20 52 65 70 72 65 70 61 72 65 20 6f 6e   /* Reprepare on
268fc 6c 79 20 63 61 6c 6c 65 64 20 66 6f 72 20 70 72  ly called for pr
268fd 65 70 61 72 65 5f 76 32 28 29 20 73 74 61 74 65  epare_v2() state
268fe 6d 65 6e 74 73 20 2a 2f 0a 20 20 64 62 20 3d 20  ments */.  db = 
268ff 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 70 29  sqlite3VdbeDb(p)
26900 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
26901 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
26902 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72  b->mutex) );.  r
26903 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41  c = sqlite3LockA
26904 6e 64 50 72 65 70 61 72 65 28 64 62 2c 20 7a 53  ndPrepare(db, zS
26905 71 6c 2c 20 2d 31 2c 20 30 2c 20 26 70 4e 65 77  ql, -1, 0, &pNew
26906 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29  , 0);.  if( rc )
26907 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  {.    if( rc==SQ
26908 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
26909 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
2690a 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  iled = 1;.    }.
2690b 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77      assert( pNew
2690c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ==0 );.    retur
2690d 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f  n (rc==SQLITE_LO
2690e 43 4b 45 44 29 20 3f 20 53 51 4c 49 54 45 5f 4c  CKED) ? SQLITE_L
2690f 4f 43 4b 45 44 20 3a 20 53 51 4c 49 54 45 5f 53  OCKED : SQLITE_S
26910 43 48 45 4d 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a  CHEMA;.  }else{.
26911 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77      assert( pNew
26912 21 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  !=0 );.  }.  sql
26913 69 74 65 33 56 64 62 65 53 77 61 70 28 28 56 64  ite3VdbeSwap((Vd
26914 62 65 2a 29 70 4e 65 77 2c 20 70 29 3b 0a 20 20  be*)pNew, p);.  
26915 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42  sqlite3TransferB
26916 69 6e 64 69 6e 67 73 28 70 4e 65 77 2c 20 28 73  indings(pNew, (s
26917 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 29 3b  qlite3_stmt*)p);
26918 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
26919 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 28 56  setStepResult((V
2691a 64 62 65 2a 29 70 4e 65 77 29 3b 0a 20 20 73 71  dbe*)pNew);.  sq
2691b 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
2691c 65 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b 0a  e((Vdbe*)pNew);.
2691d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2691e 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 77  OK;.}.../*.** Tw
2691f 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  o versions of th
26920 65 20 6f 66 66 69 63 69 61 6c 20 41 50 49 2e 20  e official API. 
26921 20 4c 65 67 61 63 79 20 61 6e 64 20 6e 65 77 20   Legacy and new 
26922 75 73 65 2e 20 20 49 6e 20 74 68 65 20 6c 65 67  use.  In the leg
26923 61 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20  acy.** version, 
26924 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
26925 20 74 65 78 74 20 69 73 20 6e 6f 74 20 73 61 76   text is not sav
26926 65 64 20 69 6e 20 74 68 65 20 70 72 65 70 61 72  ed in the prepar
26927 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  ed statement.** 
26928 61 6e 64 20 73 6f 20 69 66 20 61 20 73 63 68 65  and so if a sche
26929 6d 61 20 63 68 61 6e 67 65 20 6f 63 63 75 72 73  ma change occurs
2692a 2c 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  , SQLITE_SCHEMA 
2692b 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  is returned by.*
2692c 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
2692d 2e 20 20 49 6e 20 74 68 65 20 6e 65 77 20 76 65  .  In the new ve
2692e 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69  rsion, the origi
2692f 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20  nal SQL text is 
26930 72 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20  retained.** and 
26931 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
26932 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72   automatically r
26933 65 63 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e 20  ecompiled if an 
26934 73 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a  schema change.**
26935 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49   occurs..*/.SQLI
26936 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
26937 65 33 5f 70 72 65 70 61 72 65 28 0a 20 20 73 71  e3_prepare(.  sq
26938 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
26939 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2693a 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20  ase handle. */. 
2693b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
2693c 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54  l,         /* UT
2693d 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  F-8 encoded SQL 
2693e 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
2693f 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
26940 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
26941 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
26942 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ytes. */.  sqlit
26943 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
26944 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70  ,    /* OUT: A p
26945 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72  ointer to the pr
26946 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
26947 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
26948 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20   **pzTail       
26949 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70  /* OUT: End of p
2694a 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a  arsed string */.
2694b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72  ){.  int rc;.  r
2694c 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41  c = sqlite3LockA
2694d 6e 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53 71  ndPrepare(db,zSq
2694e 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70 53 74 6d  l,nBytes,0,ppStm
2694f 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73  t,pzTail);.  ass
26950 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
26951 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20  OK || ppStmt==0 
26952 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b  || *ppStmt==0 );
26953 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33    /* VERIFY: F13
26954 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  021 */.  return 
26955 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  rc;.}.SQLITE_API
26956 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
26957 70 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74  pare_v2(.  sqlit
26958 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
26959 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2695a 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f   handle. */.  co
2695b 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
2695c 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
2695d 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
2695e 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
2695f 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
26960 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
26961 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
26962 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
26963 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
26964 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e    /* OUT: A poin
26965 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61  ter to the prepa
26966 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  red statement */
26967 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
26968 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
26969 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
2696a 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ed string */.){.
2696b 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d    int rc;.  rc =
2696c 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
2696d 72 65 70 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e  repare(db,zSql,n
2696e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c 70  Bytes,1,ppStmt,p
2696f 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74  zTail);.  assert
26970 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
26971 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20  || ppStmt==0 || 
26972 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f  *ppStmt==0 );  /
26973 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31  * VERIFY: F13021
26974 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
26975 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .}...#ifndef SQL
26976 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f  ITE_OMIT_UTF16./
26977 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65  *.** Compile the
26978 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20   UTF-16 encoded 
26979 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53  SQL statement zS
2697a 71 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d  ql into a statem
2697b 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73  ent handle..*/.s
2697c 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
2697d 33 50 72 65 70 61 72 65 31 36 28 0a 20 20 73 71  3Prepare16(.  sq
2697e 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2697f 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
26980 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a  ase handle. */ .
26981 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53    const void *zS
26982 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ql,         /* U
26983 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
26984 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
26985 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
26986 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
26987 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
26988 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20  bytes. */.  int 
26989 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20  saveSqlFlag,    
2698a 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
2698b 20 73 61 76 65 20 53 51 4c 20 74 65 78 74 20 69   save SQL text i
2698c 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f  nto the sqlite3_
2698d 73 74 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  stmt */.  sqlite
2698e 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c  3_stmt **ppStmt,
2698f 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f      /* OUT: A po
26990 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65  inter to the pre
26991 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
26992 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
26993 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f  **pzTail       /
26994 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61  * OUT: End of pa
26995 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29  rsed string */.)
26996 7b 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63  {.  /* This func
26997 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 77  tion currently w
26998 6f 72 6b 73 20 62 79 20 66 69 72 73 74 20 74 72  orks by first tr
26999 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 20 55  ansforming the U
2699a 54 46 2d 31 36 0a 20 20 2a 2a 20 65 6e 63 6f 64  TF-16.  ** encod
2699b 65 64 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46  ed string to UTF
2699c 2d 38 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 69 6e  -8, then invokin
2699d 67 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  g sqlite3_prepar
2699e 65 28 29 2e 20 54 68 65 0a 20 20 2a 2a 20 74 72  e(). The.  ** tr
2699f 69 63 6b 79 20 62 69 74 20 69 73 20 66 69 67 75  icky bit is figu
269a0 72 69 6e 67 20 6f 75 74 20 74 68 65 20 70 6f 69  ring out the poi
269a1 6e 74 65 72 20 74 6f 20 72 65 74 75 72 6e 20 69  nter to return i
269a2 6e 20 2a 70 7a 54 61 69 6c 2e 0a 20 20 2a 2f 0a  n *pzTail..  */.
269a3 20 20 63 68 61 72 20 2a 7a 53 71 6c 38 3b 0a 20    char *zSql8;. 
269a4 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
269a5 69 6c 38 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  il8 = 0;.  int r
269a6 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
269a7 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61    if( !sqlite3Sa
269a8 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20  fetyCheckOk(db) 
269a9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
269aa 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
269ab 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
269ac 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
269ad 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c  );.  zSql8 = sql
269ae 69 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c  ite3Utf16to8(db,
269af 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b 0a   zSql, nBytes);.
269b0 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20    if( zSql8 ){. 
269b1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c     rc = sqlite3L
269b2 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62  ockAndPrepare(db
269b3 2c 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73 61 76  , zSql8, -1, sav
269b4 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74 6d 74  eSqlFlag, ppStmt
269b5 2c 20 26 7a 54 61 69 6c 38 29 3b 0a 20 20 7d 0a  , &zTail8);.  }.
269b6 0a 20 20 69 66 28 20 7a 54 61 69 6c 38 20 26 26  .  if( zTail8 &&
269b7 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2f   pzTail ){.    /
269b8 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 70 72 65  * If sqlite3_pre
269b9 70 61 72 65 20 72 65 74 75 72 6e 73 20 61 20 74  pare returns a t
269ba 61 69 6c 20 70 6f 69 6e 74 65 72 2c 20 77 65 20  ail pointer, we 
269bb 63 61 6c 63 75 6c 61 74 65 20 74 68 65 0a 20 20  calculate the.  
269bc 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20    ** equivalent 
269bd 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74 68 65  pointer into the
269be 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 62   UTF-16 string b
269bf 79 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 20 75  y counting the u
269c0 6e 69 63 6f 64 65 0a 20 20 20 20 2a 2a 20 63 68  nicode.    ** ch
269c1 61 72 61 63 74 65 72 73 20 62 65 74 77 65 65 6e  aracters between
269c2 20 7a 53 71 6c 38 20 61 6e 64 20 7a 54 61 69 6c   zSql8 and zTail
269c3 38 2c 20 61 6e 64 20 74 68 65 6e 20 72 65 74 75  8, and then retu
269c4 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 0a  rning a pointer.
269c5 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 20      ** the same 
269c6 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
269c7 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20 55 54  ters into the UT
269c8 46 2d 31 36 20 73 74 72 69 6e 67 2e 0a 20 20 20  F-16 string..   
269c9 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68 61 72   */.    int char
269ca 73 5f 70 61 72 73 65 64 20 3d 20 73 71 6c 69 74  s_parsed = sqlit
269cb 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a 53  e3Utf8CharLen(zS
269cc 71 6c 38 2c 20 28 69 6e 74 29 28 7a 54 61 69 6c  ql8, (int)(zTail
269cd 38 2d 7a 53 71 6c 38 29 29 3b 0a 20 20 20 20 2a  8-zSql8));.    *
269ce 70 7a 54 61 69 6c 20 3d 20 28 75 38 20 2a 29 7a  pzTail = (u8 *)z
269cf 53 71 6c 20 2b 20 73 71 6c 69 74 65 33 55 74 66  Sql + sqlite3Utf
269d0 31 36 42 79 74 65 4c 65 6e 28 7a 53 71 6c 2c 20  16ByteLen(zSql, 
269d1 63 68 61 72 73 5f 70 61 72 73 65 64 29 3b 0a 20  chars_parsed);. 
269d2 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72   }.  sqlite3DbFr
269d3 65 65 28 64 62 2c 20 7a 53 71 6c 38 29 3b 20 0a  ee(db, zSql8); .
269d4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
269d5 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
269d6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
269d7 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
269d8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
269d9 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69  ./*.** Two versi
269da 6f 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69 63  ons of the offic
269db 69 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63 79  ial API.  Legacy
269dc 20 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20 49   and new use.  I
269dd 6e 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20  n the legacy.** 
269de 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69  version, the ori
269df 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69  ginal SQL text i
269e0 73 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20 74  s not saved in t
269e1 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
269e2 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f 20  ement.** and so 
269e3 69 66 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e  if a schema chan
269e4 67 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54  ge occurs, SQLIT
269e5 45 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74 75  E_SCHEMA is retu
269e6 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74  rned by.** sqlit
269e7 65 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20 74  e3_step().  In t
269e8 68 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20  he new version, 
269e9 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
269ea 20 74 65 78 74 20 69 73 20 72 65 74 61 69 6e 65   text is retaine
269eb 64 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61  d.** and the sta
269ec 74 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61  tement is automa
269ed 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c  tically recompil
269ee 65 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61 20  ed if an schema 
269ef 63 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72 73  change.** occurs
269f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
269f1 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  int sqlite3_prep
269f2 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33  are16(.  sqlite3
269f3 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
269f4 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
269f5 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e  andle. */ .  con
269f6 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20  st void *zSql,  
269f7 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20         /* UTF-8 
269f8 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
269f9 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20  ement. */.  int 
269fa 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
269fb 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
269fc 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
269fd 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
269fe 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20  tmt **ppStmt,   
269ff 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74   /* OUT: A point
26a00 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72  er to the prepar
26a01 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ed statement */.
26a02 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70    const void **p
26a03 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f  zTail       /* O
26a04 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  UT: End of parse
26a05 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20  d string */.){. 
26a06 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20   int rc;.  rc = 
26a07 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36  sqlite3Prepare16
26a08 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c  (db,zSql,nBytes,
26a09 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  0,ppStmt,pzTail)
26a0a 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
26a0b 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53  SQLITE_OK || ppS
26a0c 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d  tmt==0 || *ppStm
26a0d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49  t==0 );  /* VERI
26a0e 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20  FY: F13021 */.  
26a0f 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
26a10 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
26a11 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
26a12 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
26a13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26a14 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
26a15 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f  . */ .  const vo
26a16 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  id *zSql,       
26a17 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
26a18 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
26a19 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
26a1a 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
26a1b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
26a1c 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
26a1d 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
26a1e 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f  *ppStmt,    /* O
26a1f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  UT: A pointer to
26a20 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
26a21 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e  atement */.  con
26a22 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c  st void **pzTail
26a23 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
26a24 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72  nd of parsed str
26a25 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ing */.){.  int 
26a26 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  rc;.  rc = sqlit
26a27 65 33 50 72 65 70 61 72 65 31 36 28 64 62 2c 7a  e3Prepare16(db,z
26a28 53 71 6c 2c 6e 42 79 74 65 73 2c 31 2c 70 70 53  Sql,nBytes,1,ppS
26a29 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61  tmt,pzTail);.  a
26a2a 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
26a2b 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d  E_OK || ppStmt==
26a2c 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20  0 || *ppStmt==0 
26a2d 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46  );  /* VERIFY: F
26a2e 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72  13021 */.  retur
26a2f 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  n rc;.}..#endif 
26a30 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
26a31 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TF16 */../******
26a32 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
26a33 70 72 65 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a  prepare.c ******
26a34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a36 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
26a37 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
26a38 69 6c 65 20 73 65 6c 65 63 74 2e 63 20 2a 2a 2a  ile select.c ***
26a39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a3b 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
26a3c 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
26a3d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
26a3e 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
26a3f 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
26a40 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
26a41 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
26a42 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
26a43 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
26a44 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
26a45 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
26a46 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
26a47 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
26a48 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
26a49 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
26a4a 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
26a4b 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
26a4c 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
26a4d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
26a4e 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
26a4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
26a53 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
26a54 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75  tains C code rou
26a55 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63  tines that are c
26a56 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
26a57 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65  ser.** to handle
26a58 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
26a59 74 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a  ts in SQLite..**
26a5a 0a 2a 2a 20 24 49 64 3a 20 73 65 6c 65 63 74 2e  .** $Id: select.
26a5b 63 2c 76 20 31 2e 35 30 36 20 32 30 30 39 2f 30  c,v 1.506 2009/0
26a5c 33 2f 33 31 20 30 33 3a 34 31 3a 35 37 20 73 68  3/31 03:41:57 sh
26a5d 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f  ane Exp $.*/.../
26a5e 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20  *.** Delete all 
26a5f 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61  the content of a
26a60 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72   Select structur
26a61 65 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 61  e but do not dea
26a62 6c 6c 6f 63 61 74 65 0a 2a 2a 20 74 68 65 20 73  llocate.** the s
26a63 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20  elect structure 
26a64 69 74 73 65 6c 66 2e 0a 2a 2f 0a 73 74 61 74 69  itself..*/.stati
26a65 63 20 76 6f 69 64 20 63 6c 65 61 72 53 65 6c 65  c void clearSele
26a66 63 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ct(sqlite3 *db, 
26a67 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 73 71  Select *p){.  sq
26a68 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
26a69 65 74 65 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73  ete(db, p->pELis
26a6a 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  t);.  sqlite3Src
26a6b 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
26a6c 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74  ->pSrc);.  sqlit
26a6d 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
26a6e 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 73   p->pWhere);.  s
26a6f 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
26a70 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 47 72 6f  lete(db, p->pGro
26a71 75 70 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33  upBy);.  sqlite3
26a72 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
26a73 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 73 71  ->pHaving);.  sq
26a74 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
26a75 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65  ete(db, p->pOrde
26a76 72 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  rBy);.  sqlite3S
26a77 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
26a78 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 73 71  p->pPrior);.  sq
26a79 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
26a7a 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a  db, p->pLimit);.
26a7b 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
26a7c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73  ete(db, p->pOffs
26a7d 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  et);.}../*.** In
26a7e 69 74 69 61 6c 69 7a 65 20 61 20 53 65 6c 65 63  itialize a Selec
26a7f 74 44 65 73 74 20 73 74 72 75 63 74 75 72 65 2e  tDest structure.
26a80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
26a81 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
26a82 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53 65  electDestInit(Se
26a83 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c  lectDest *pDest,
26a84 20 69 6e 74 20 65 44 65 73 74 2c 20 69 6e 74 20   int eDest, int 
26a85 69 50 61 72 6d 29 7b 0a 20 20 70 44 65 73 74 2d  iParm){.  pDest-
26a86 3e 65 44 65 73 74 20 3d 20 28 75 38 29 65 44 65  >eDest = (u8)eDe
26a87 73 74 3b 0a 20 20 70 44 65 73 74 2d 3e 69 50 61  st;.  pDest->iPa
26a88 72 6d 20 3d 20 69 50 61 72 6d 3b 0a 20 20 70 44  rm = iParm;.  pD
26a89 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20  est->affinity = 
26a8a 30 3b 0a 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d  0;.  pDest->iMem
26a8b 20 3d 20 30 3b 0a 20 20 70 44 65 73 74 2d 3e 6e   = 0;.  pDest->n
26a8c 4d 65 6d 20 3d 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a  Mem = 0;.}.../*.
26a8d 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ** Allocate a ne
26a8e 77 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75  w Select structu
26a8f 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  re and return a 
26a90 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a  pointer to that.
26a91 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ** structure..*/
26a92 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
26a93 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53  Select *sqlite3S
26a94 65 6c 65 63 74 4e 65 77 28 0a 20 20 50 61 72 73  electNew(.  Pars
26a95 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
26a96 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
26a97 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
26a98 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20  st *pEList,     
26a99 2f 2a 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73  /* which columns
26a9a 20 74 6f 20 69 6e 63 6c 75 64 65 20 69 6e 20 74   to include in t
26a9b 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 53  he result */.  S
26a9c 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
26a9d 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d       /* the FROM
26a9e 20 63 6c 61 75 73 65 20 2d 2d 20 77 68 69 63 68   clause -- which
26a9f 20 74 61 62 6c 65 73 20 74 6f 20 73 63 61 6e 20   tables to scan 
26aa0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
26aa1 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68  e,         /* th
26aa2 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
26aa3 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47  /.  ExprList *pG
26aa4 72 6f 75 70 42 79 2c 20 20 20 2f 2a 20 74 68 65  roupBy,   /* the
26aa5 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
26aa6 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76   */.  Expr *pHav
26aa7 69 6e 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 74  ing,        /* t
26aa8 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65  he HAVING clause
26aa9 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
26aaa 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 74  pOrderBy,   /* t
26aab 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
26aac 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69  se */.  int isDi
26aad 73 74 69 6e 63 74 2c 20 20 20 20 20 20 20 2f 2a  stinct,       /*
26aae 20 74 72 75 65 20 69 66 20 74 68 65 20 44 49 53   true if the DIS
26aaf 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73  TINCT keyword is
26ab0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 45 78   present */.  Ex
26ab1 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20  pr *pLimit,     
26ab2 20 20 20 20 2f 2a 20 4c 49 4d 49 54 20 76 61 6c      /* LIMIT val
26ab3 75 65 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73 20  ue.  NULL means 
26ab4 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 45 78  not used */.  Ex
26ab5 70 72 20 2a 70 4f 66 66 73 65 74 20 20 20 20 20  pr *pOffset     
26ab6 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 76 61      /* OFFSET va
26ab7 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e 73  lue.  NULL means
26ab8 20 6e 6f 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b   no offset */.){
26ab9 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b  .  Select *pNew;
26aba 0a 20 20 53 65 6c 65 63 74 20 73 74 61 6e 64 69  .  Select standi
26abb 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n;.  sqlite3 *db
26abc 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
26abd 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
26abe 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
26abf 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b  sizeof(*pNew) );
26ac0 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d  .  assert( db->m
26ac1 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 21  allocFailed || !
26ac2 70 4f 66 66 73 65 74 20 7c 7c 20 70 4c 69 6d 69  pOffset || pLimi
26ac3 74 20 29 3b 20 2f 2a 20 4f 46 46 53 45 54 20 69  t ); /* OFFSET i
26ac4 6d 70 6c 69 65 73 20 4c 49 4d 49 54 20 2a 2f 0a  mplies LIMIT */.
26ac5 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
26ac6 0a 20 20 20 20 70 4e 65 77 20 3d 20 26 73 74 61  .    pNew = &sta
26ac7 6e 64 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 65 74  ndin;.    memset
26ac8 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66  (pNew, 0, sizeof
26ac9 28 2a 70 4e 65 77 29 29 3b 0a 20 20 7d 0a 20 20  (*pNew));.  }.  
26aca 69 66 28 20 70 45 4c 69 73 74 3d 3d 30 20 29 7b  if( pEList==0 ){
26acb 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71  .    pEList = sq
26acc 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
26acd 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 73  end(pParse, 0, s
26ace 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 54 4b  qlite3Expr(db,TK
26acf 5f 41 4c 4c 2c 30 2c 30 2c 30 29 2c 20 30 29 3b  _ALL,0,0,0), 0);
26ad0 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 45 4c  .  }.  pNew->pEL
26ad1 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20  ist = pEList;.  
26ad2 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 70 53 72  pNew->pSrc = pSr
26ad3 63 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72  c;.  pNew->pWher
26ad4 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 70 4e  e = pWhere;.  pN
26ad5 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 70  ew->pGroupBy = p
26ad6 47 72 6f 75 70 42 79 3b 0a 20 20 70 4e 65 77 2d  GroupBy;.  pNew-
26ad7 3e 70 48 61 76 69 6e 67 20 3d 20 70 48 61 76 69  >pHaving = pHavi
26ad8 6e 67 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64  ng;.  pNew->pOrd
26ad9 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b  erBy = pOrderBy;
26ada 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67  .  pNew->selFlag
26adb 73 20 3d 20 69 73 44 69 73 74 69 6e 63 74 20 3f  s = isDistinct ?
26adc 20 53 46 5f 44 69 73 74 69 6e 63 74 20 3a 20 30   SF_Distinct : 0
26add 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54  ;.  pNew->op = T
26ade 4b 5f 53 45 4c 45 43 54 3b 0a 20 20 70 4e 65 77  K_SELECT;.  pNew
26adf 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69  ->pLimit = pLimi
26ae0 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73  t;.  pNew->pOffs
26ae1 65 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20  et = pOffset;.  
26ae2 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70  pNew->addrOpenEp
26ae3 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e  hm[0] = -1;.  pN
26ae4 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d  ew->addrOpenEphm
26ae5 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77  [1] = -1;.  pNew
26ae6 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32  ->addrOpenEphm[2
26ae7 5d 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 64 62  ] = -1;.  if( db
26ae8 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
26ae9 20 7b 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65   {.    clearSele
26aea 63 74 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20  ct(db, pNew);.  
26aeb 20 20 69 66 28 20 70 4e 65 77 21 3d 26 73 74 61    if( pNew!=&sta
26aec 6e 64 69 6e 20 29 20 73 71 6c 69 74 65 33 44 62  ndin ) sqlite3Db
26aed 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a  Free(db, pNew);.
26aee 20 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20      pNew = 0;.  
26aef 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
26af0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
26af1 20 74 68 65 20 67 69 76 65 6e 20 53 65 6c 65 63   the given Selec
26af2 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  t structure and 
26af3 61 6c 6c 20 6f 66 20 69 74 73 20 73 75 62 73 74  all of its subst
26af4 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c  ructures..*/.SQL
26af5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
26af6 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
26af7 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
26af8 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
26af9 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63 6c 65  if( p ){.    cle
26afa 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70 29 3b  arSelect(db, p);
26afb 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
26afc 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d  ee(db, p);.  }.}
26afd 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 31 20  ../*.** Given 1 
26afe 74 6f 20 33 20 69 64 65 6e 74 69 66 69 65 72 73  to 3 identifiers
26aff 20 70 72 65 63 65 65 64 69 6e 67 20 74 68 65 20   preceeding the 
26b00 4a 4f 49 4e 20 6b 65 79 77 6f 72 64 2c 20 64 65  JOIN keyword, de
26b01 74 65 72 6d 69 6e 65 20 74 68 65 0a 2a 2a 20 74  termine the.** t
26b02 79 70 65 20 6f 66 20 6a 6f 69 6e 2e 20 20 52 65  ype of join.  Re
26b03 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20  turn an integer 
26b04 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 65 78  constant that ex
26b05 70 72 65 73 73 65 73 20 74 68 61 74 20 74 79 70  presses that typ
26b06 65 0a 2a 2a 20 69 6e 20 74 65 72 6d 73 20 6f 66  e.** in terms of
26b07 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   the following b
26b08 69 74 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a  it values:.**.**
26b09 20 20 20 20 20 4a 54 5f 49 4e 4e 45 52 0a 2a 2a       JT_INNER.**
26b0a 20 20 20 20 20 4a 54 5f 43 52 4f 53 53 0a 2a 2a       JT_CROSS.**
26b0b 20 20 20 20 20 4a 54 5f 4f 55 54 45 52 0a 2a 2a       JT_OUTER.**
26b0c 20 20 20 20 20 4a 54 5f 4e 41 54 55 52 41 4c 0a       JT_NATURAL.
26b0d 2a 2a 20 20 20 20 20 4a 54 5f 4c 45 46 54 0a 2a  **     JT_LEFT.*
26b0e 2a 20 20 20 20 20 4a 54 5f 52 49 47 48 54 0a 2a  *     JT_RIGHT.*
26b0f 2a 0a 2a 2a 20 41 20 66 75 6c 6c 20 6f 75 74 65  *.** A full oute
26b10 72 20 6a 6f 69 6e 20 69 73 20 74 68 65 20 63 6f  r join is the co
26b11 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4a 54 5f  mbination of JT_
26b12 4c 45 46 54 20 61 6e 64 20 4a 54 5f 52 49 47 48  LEFT and JT_RIGH
26b13 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69  T..**.** If an i
26b14 6c 6c 65 67 61 6c 20 6f 72 20 75 6e 73 75 70 70  llegal or unsupp
26b15 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20  orted join type 
26b16 69 73 20 73 65 65 6e 2c 20 74 68 65 6e 20 73 74  is seen, then st
26b17 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ill return.** a 
26b18 6a 6f 69 6e 20 74 79 70 65 2c 20 62 75 74 20 70  join type, but p
26b19 75 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 74  ut an error in t
26b1a 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74  he pParse struct
26b1b 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
26b1c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
26b1d 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65  e3JoinType(Parse
26b1e 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
26b1f 2a 70 41 2c 20 54 6f 6b 65 6e 20 2a 70 42 2c 20  *pA, Token *pB, 
26b20 54 6f 6b 65 6e 20 2a 70 43 29 7b 0a 20 20 69 6e  Token *pC){.  in
26b21 74 20 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a  t jointype = 0;.
26b22 20 20 54 6f 6b 65 6e 20 2a 61 70 41 6c 6c 5b 33    Token *apAll[3
26b23 5d 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 3b 0a 20  ];.  Token *p;. 
26b24 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74   static const st
26b25 72 75 63 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74  ruct {.    const
26b26 20 63 68 61 72 20 7a 4b 65 79 77 6f 72 64 5b 38   char zKeyword[8
26b27 5d 3b 0a 20 20 20 20 75 38 20 6e 43 68 61 72 3b  ];.    u8 nChar;
26b28 0a 20 20 20 20 75 38 20 63 6f 64 65 3b 0a 20 20  .    u8 code;.  
26b29 7d 20 6b 65 79 77 6f 72 64 73 5b 5d 20 3d 20 7b  } keywords[] = {
26b2a 0a 20 20 20 20 7b 20 22 6e 61 74 75 72 61 6c 22  .    { "natural"
26b2b 2c 20 37 2c 20 4a 54 5f 4e 41 54 55 52 41 4c 20  , 7, JT_NATURAL 
26b2c 7d 2c 0a 20 20 20 20 7b 20 22 6c 65 66 74 22 2c  },.    { "left",
26b2d 20 20 20 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a      4, JT_LEFT|J
26b2e 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20 7b  T_OUTER },.    {
26b2f 20 22 72 69 67 68 74 22 2c 20 20 20 35 2c 20 4a   "right",   5, J
26b30 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52  T_RIGHT|JT_OUTER
26b31 20 7d 2c 0a 20 20 20 20 7b 20 22 66 75 6c 6c 22   },.    { "full"
26b32 2c 20 20 20 20 34 2c 20 4a 54 5f 4c 45 46 54 7c  ,    4, JT_LEFT|
26b33 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45  JT_RIGHT|JT_OUTE
26b34 52 20 7d 2c 0a 20 20 20 20 7b 20 22 6f 75 74 65  R },.    { "oute
26b35 72 22 2c 20 20 20 35 2c 20 4a 54 5f 4f 55 54 45  r",   5, JT_OUTE
26b36 52 20 7d 2c 0a 20 20 20 20 7b 20 22 69 6e 6e 65  R },.    { "inne
26b37 72 22 2c 20 20 20 35 2c 20 4a 54 5f 49 4e 4e 45  r",   5, JT_INNE
26b38 52 20 7d 2c 0a 20 20 20 20 7b 20 22 63 72 6f 73  R },.    { "cros
26b39 73 22 2c 20 20 20 35 2c 20 4a 54 5f 49 4e 4e 45  s",   5, JT_INNE
26b3a 52 7c 4a 54 5f 43 52 4f 53 53 20 7d 2c 0a 20 20  R|JT_CROSS },.  
26b3b 7d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  };.  int i, j;. 
26b3c 20 61 70 41 6c 6c 5b 30 5d 20 3d 20 70 41 3b 0a   apAll[0] = pA;.
26b3d 20 20 61 70 41 6c 6c 5b 31 5d 20 3d 20 70 42 3b    apAll[1] = pB;
26b3e 0a 20 20 61 70 41 6c 6c 5b 32 5d 20 3d 20 70 43  .  apAll[2] = pC
26b3f 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 33  ;.  for(i=0; i<3
26b40 20 26 26 20 61 70 41 6c 6c 5b 69 5d 3b 20 69 2b   && apAll[i]; i+
26b41 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61 70 41 6c  +){.    p = apAl
26b42 6c 5b 69 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  l[i];.    for(j=
26b43 30 3b 20 6a 3c 41 72 72 61 79 53 69 7a 65 28 6b  0; j<ArraySize(k
26b44 65 79 77 6f 72 64 73 29 3b 20 6a 2b 2b 29 7b 0a  eywords); j++){.
26b45 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6e 3d 3d        if( p->n==
26b46 6b 65 79 77 6f 72 64 73 5b 6a 5d 2e 6e 43 68 61  keywords[j].nCha
26b47 72 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  r .          && 
26b48 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
26b49 28 63 68 61 72 2a 29 70 2d 3e 7a 2c 20 6b 65 79  (char*)p->z, key
26b4a 77 6f 72 64 73 5b 6a 5d 2e 7a 4b 65 79 77 6f 72  words[j].zKeywor
26b4b 64 2c 20 70 2d 3e 6e 29 3d 3d 30 20 29 7b 0a 20  d, p->n)==0 ){. 
26b4c 20 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20         jointype 
26b4d 7c 3d 20 6b 65 79 77 6f 72 64 73 5b 6a 5d 2e 63  |= keywords[j].c
26b4e 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ode;.        bre
26b4f 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
26b50 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d 41 72 72  }.    if( j>=Arr
26b51 61 79 53 69 7a 65 28 6b 65 79 77 6f 72 64 73 29  aySize(keywords)
26b52 20 29 7b 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79   ){.      jointy
26b53 70 65 20 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b 0a  pe |= JT_ERROR;.
26b54 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
26b55 20 7d 0a 20 20 7d 0a 20 20 69 66 28 0a 20 20 20   }.  }.  if(.   
26b56 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a    (jointype & (J
26b57 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52  T_INNER|JT_OUTER
26b58 29 29 3d 3d 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54  ))==(JT_INNER|JT
26b59 5f 4f 55 54 45 52 29 20 7c 7c 0a 20 20 20 20 20  _OUTER) ||.     
26b5a 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 45  (jointype & JT_E
26b5b 52 52 4f 52 29 21 3d 30 0a 20 20 29 7b 0a 20 20  RROR)!=0.  ){.  
26b5c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
26b5d 70 20 3d 20 22 20 22 3b 0a 20 20 20 20 61 73 73  p = " ";.    ass
26b5e 65 72 74 28 20 70 42 21 3d 30 20 29 3b 0a 20 20  ert( pB!=0 );.  
26b5f 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 20 7a    if( pC==0 ){ z
26b60 53 70 2b 2b 3b 20 7d 0a 20 20 20 20 73 71 6c 69  Sp++; }.    sqli
26b61 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
26b62 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20  se, "unknown or 
26b63 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e  unsupported join
26b64 20 74 79 70 65 3a 20 22 0a 20 20 20 20 20 20 20   type: ".       
26b65 22 25 54 20 25 54 25 73 25 54 22 2c 20 70 41 2c  "%T %T%s%T", pA,
26b66 20 70 42 2c 20 7a 53 70 2c 20 70 43 29 3b 0a 20   pB, zSp, pC);. 
26b67 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54     jointype = JT
26b68 5f 49 4e 4e 45 52 3b 0a 20 20 7d 65 6c 73 65 20  _INNER;.  }else 
26b69 69 66 28 20 6a 6f 69 6e 74 79 70 65 20 26 20 4a  if( jointype & J
26b6a 54 5f 52 49 47 48 54 20 29 7b 0a 20 20 20 20 73  T_RIGHT ){.    s
26b6b 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
26b6c 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 52  Parse, .      "R
26b6d 49 47 48 54 20 61 6e 64 20 46 55 4c 4c 20 4f 55  IGHT and FULL OU
26b6e 54 45 52 20 4a 4f 49 4e 73 20 61 72 65 20 6e 6f  TER JOINs are no
26b6f 74 20 63 75 72 72 65 6e 74 6c 79 20 73 75 70 70  t currently supp
26b70 6f 72 74 65 64 22 29 3b 0a 20 20 20 20 6a 6f 69  orted");.    joi
26b71 6e 74 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52  ntype = JT_INNER
26b72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6a  ;.  }.  return j
26b73 6f 69 6e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ointype;.}../*.*
26b74 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  * Return the ind
26b75 65 78 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69  ex of a column i
26b76 6e 20 61 20 74 61 62 6c 65 2e 20 20 52 65 74 75  n a table.  Retu
26b77 72 6e 20 2d 31 20 69 66 20 74 68 65 20 63 6f 6c  rn -1 if the col
26b78 75 6d 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f  umn.** is not co
26b79 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 74  ntained in the t
26b7a 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
26b7b 69 6e 74 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28  int columnIndex(
26b7c 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e  Table *pTab, con
26b7d 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 29 7b 0a  st char *zCol){.
26b7e 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
26b7f 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
26b80 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
26b81 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
26b82 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61  Tab->aCol[i].zNa
26b83 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 20 72  me, zCol)==0 ) r
26b84 65 74 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72  eturn i;.  }.  r
26b85 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a  eturn -1;.}../*.
26b86 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65  ** Set the value
26b87 20 6f 66 20 61 20 74 6f 6b 65 6e 20 74 6f 20 61   of a token to a
26b88 20 27 5c 30 30 30 27 2d 74 65 72 6d 69 6e 61 74   '\000'-terminat
26b89 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74  ed string..*/.st
26b8a 61 74 69 63 20 76 6f 69 64 20 73 65 74 54 6f 6b  atic void setTok
26b8b 65 6e 28 54 6f 6b 65 6e 20 2a 70 2c 20 63 6f 6e  en(Token *p, con
26b8c 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 70  st char *z){.  p
26b8d 2d 3e 7a 20 3d 20 28 75 38 2a 29 7a 3b 0a 20 20  ->z = (u8*)z;.  
26b8e 70 2d 3e 6e 20 3d 20 7a 20 3f 20 73 71 6c 69 74  p->n = z ? sqlit
26b8f 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 3a 20  e3Strlen30(z) : 
26b90 30 3b 0a 20 20 70 2d 3e 64 79 6e 20 3d 20 30 3b  0;.  p->dyn = 0;
26b91 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
26b92 65 20 74 6f 6b 65 6e 20 74 6f 20 74 68 65 20 64  e token to the d
26b93 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 61 6e 64  ouble-quoted and
26b94 20 65 73 63 61 70 65 64 20 76 65 72 73 69 6f 6e   escaped version
26b95 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 70   of the string p
26b96 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20  ointed.** to by 
26b97 7a 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 3b 0a  z. For example;.
26b98 2a 2a 0a 2a 2a 20 20 20 20 7b 61 22 62 63 7d 20  **.**    {a"bc} 
26b99 20 2d 3e 20 20 7b 22 61 22 22 62 63 22 7d 0a 2a   ->  {"a""bc"}.*
26b9a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
26b9b 74 51 75 6f 74 65 64 54 6f 6b 65 6e 28 50 61 72  tQuotedToken(Par
26b9c 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
26b9d 6e 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  n *p, const char
26b9e 20 2a 7a 29 7b 0a 0a 20 20 2f 2a 20 43 68 65 63   *z){..  /* Chec
26b9f 6b 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20  k if the string 
26ba0 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 71 75  appears to be qu
26ba1 6f 74 65 64 20 75 73 69 6e 67 20 22 2e 2e 2e 22  oted using "..."
26ba2 20 6f 72 20 60 2e 2e 2e 60 0a 20 20 2a 2a 20 6f   or `...`.  ** o
26ba3 72 20 5b 2e 2e 2e 5d 20 6f 72 20 27 2e 2e 2e 27  r [...] or '...'
26ba4 20 6f 72 20 69 66 20 74 68 65 20 73 74 72 69 6e   or if the strin
26ba5 67 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 22  g contains any "
26ba6 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 0a 20   characters.  . 
26ba7 20 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20   ** If it does, 
26ba8 74 68 65 6e 20 72 65 63 6f 72 64 20 61 20 76 65  then record a ve
26ba9 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 72  rsion of the str
26baa 69 6e 67 20 77 69 74 68 20 74 68 65 20 73 70 65  ing with the spe
26bab 63 69 61 6c 0a 20 20 2a 2a 20 63 68 61 72 61 63  cial.  ** charac
26bac 74 65 72 73 20 65 73 63 61 70 65 64 2e 0a 20 20  ters escaped..  
26bad 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
26bae 2a 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 2a  *z2 = z;.  if( *
26baf 7a 32 21 3d 27 5b 27 20 26 26 20 2a 7a 32 21 3d  z2!='[' && *z2!=
26bb0 27 60 27 20 26 26 20 2a 7a 32 21 3d 27 5c 27 27  '`' && *z2!='\''
26bb1 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a   ){.    while( *
26bb2 7a 32 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  z2 ){.      if( 
26bb3 2a 7a 32 3d 3d 27 22 27 20 29 20 62 72 65 61 6b  *z2=='"' ) break
26bb4 3b 0a 20 20 20 20 20 20 7a 32 2b 2b 3b 0a 20 20  ;.      z2++;.  
26bb5 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 2a    }.  }..  if( *
26bb6 7a 32 20 29 7b 0a 20 20 20 20 2f 2a 20 53 74 72  z2 ){.    /* Str
26bb7 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 22 20 63  ing contains " c
26bb8 68 61 72 61 63 74 65 72 73 20 2d 20 63 6f 70 79  haracters - copy
26bb9 20 61 6e 64 20 71 75 6f 74 65 20 74 68 65 20 73   and quote the s
26bba 74 72 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 70 2d  tring. */.    p-
26bbb 3e 7a 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  >z = (u8 *)sqlit
26bbc 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65  e3MPrintf(pParse
26bbd 2d 3e 64 62 2c 20 22 5c 22 25 77 5c 22 22 2c 20  ->db, "\"%w\"", 
26bbe 7a 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a  z);.    if( p->z
26bbf 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 20 3d   ){.      p->n =
26bc0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
26bc1 28 28 63 68 61 72 20 2a 29 70 2d 3e 7a 29 3b 0a  ((char *)p->z);.
26bc2 20 20 20 20 20 20 70 2d 3e 64 79 6e 20 3d 20 31        p->dyn = 1
26bc3 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
26bc4 0a 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 63  .    /* String c
26bc5 6f 6e 74 61 69 6e 73 20 6e 6f 20 22 20 63 68 61  ontains no " cha
26bc6 72 61 63 74 65 72 73 20 2d 20 63 6f 70 79 20 74  racters - copy t
26bc7 68 65 20 70 6f 69 6e 74 65 72 2e 20 2a 2f 0a 20  he pointer. */. 
26bc8 20 20 20 70 2d 3e 7a 20 3d 20 28 75 38 2a 29 7a     p->z = (u8*)z
26bc9 3b 0a 20 20 20 20 70 2d 3e 6e 20 3d 20 28 69 6e  ;.    p->n = (in
26bca 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 20 20 20 20  t)(z2 - z);.    
26bcb 70 2d 3e 64 79 6e 20 3d 20 30 3b 0a 20 20 7d 0a  p->dyn = 0;.  }.
26bcc 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
26bcd 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  an expression no
26bce 64 65 20 66 6f 72 20 61 6e 20 69 64 65 6e 74 69  de for an identi
26bcf 66 69 65 72 20 77 69 74 68 20 74 68 65 20 6e 61  fier with the na
26bd0 6d 65 20 6f 66 20 7a 4e 61 6d 65 0a 2a 2f 0a 53  me of zName.*/.S
26bd1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
26bd2 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74  pr *sqlite3Creat
26bd3 65 49 64 45 78 70 72 28 50 61 72 73 65 20 2a 70  eIdExpr(Parse *p
26bd4 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
26bd5 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 54 6f 6b  r *zName){.  Tok
26bd6 65 6e 20 64 75 6d 6d 79 3b 0a 20 20 73 65 74 54  en dummy;.  setT
26bd7 6f 6b 65 6e 28 26 64 75 6d 6d 79 2c 20 7a 4e 61  oken(&dummy, zNa
26bd8 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  me);.  return sq
26bd9 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
26bda 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
26bdb 26 64 75 6d 6d 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  &dummy);.}../*.*
26bdc 2a 20 41 64 64 20 61 20 74 65 72 6d 20 74 6f 20  * Add a term to 
26bdd 74 68 65 20 57 48 45 52 45 20 65 78 70 72 65 73  the WHERE expres
26bde 73 69 6f 6e 20 69 6e 20 2a 70 70 45 78 70 72 20  sion in *ppExpr 
26bdf 74 68 61 74 20 72 65 71 75 69 72 65 73 20 74 68  that requires th
26be0 65 0a 2a 2a 20 7a 43 6f 6c 20 63 6f 6c 75 6d 6e  e.** zCol column
26be1 20 74 6f 20 62 65 20 65 71 75 61 6c 20 69 6e 20   to be equal in 
26be2 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20 70  the two tables p
26be3 54 61 62 31 20 61 6e 64 20 70 54 61 62 32 2e 0a  Tab1 and pTab2..
26be4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
26be5 64 64 57 68 65 72 65 54 65 72 6d 28 0a 20 20 50  ddWhereTerm(.  P
26be6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
26be7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
26be8 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
26be9 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
26bea 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
26beb 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a   of the column *
26bec 2f 0a 20 20 63 6f 6e 73 74 20 54 61 62 6c 65 20  /.  const Table 
26bed 2a 70 54 61 62 31 2c 20 20 20 20 20 20 2f 2a 20  *pTab1,      /* 
26bee 46 69 72 73 74 20 74 61 62 6c 65 20 2a 2f 0a 20  First table */. 
26bef 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 6c   const char *zAl
26bf0 69 61 73 31 2c 20 20 20 20 20 2f 2a 20 41 6c 69  ias1,     /* Ali
26bf1 61 73 20 66 6f 72 20 66 69 72 73 74 20 74 61 62  as for first tab
26bf2 6c 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  le.  May be NULL
26bf3 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 61 62 6c   */.  const Tabl
26bf4 65 20 2a 70 54 61 62 32 2c 20 20 20 20 20 20 2f  e *pTab2,      /
26bf5 2a 20 53 65 63 6f 6e 64 20 74 61 62 6c 65 20 2a  * Second table *
26bf6 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
26bf7 7a 41 6c 69 61 73 32 2c 20 20 20 20 20 2f 2a 20  zAlias2,     /* 
26bf8 41 6c 69 61 73 20 66 6f 72 20 73 65 63 6f 6e 64  Alias for second
26bf9 20 74 61 62 6c 65 2e 20 20 4d 61 79 20 62 65 20   table.  May be 
26bfa 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 52  NULL */.  int iR
26bfb 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 2c 20 20  ightJoinTable,  
26bfc 20 20 20 2f 2a 20 56 44 42 45 20 63 75 72 73 6f     /* VDBE curso
26bfd 72 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 20  r for the right 
26bfe 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20  table */.  Expr 
26bff 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20 20  **ppExpr,       
26c00 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65      /* Add the e
26c01 71 75 61 6c 69 74 79 20 74 65 72 6d 20 74 6f 20  quality term to 
26c02 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20  this expression 
26c03 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 75 74 65 72  */.  int isOuter
26c04 4a 6f 69 6e 20 20 20 20 20 20 20 20 20 20 2f 2a  Join          /*
26c05 20 54 72 75 65 20 69 66 20 64 65 61 6c 69 6e 67   True if dealing
26c06 20 77 69 74 68 20 61 6e 20 4f 55 54 45 52 20 6a   with an OUTER j
26c07 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72  oin */.){.  Expr
26c08 20 2a 70 45 31 61 2c 20 2a 70 45 31 62 2c 20 2a   *pE1a, *pE1b, *
26c09 70 45 31 63 3b 0a 20 20 45 78 70 72 20 2a 70 45  pE1c;.  Expr *pE
26c0a 32 61 2c 20 2a 70 45 32 62 2c 20 2a 70 45 32 63  2a, *pE2b, *pE2c
26c0b 3b 0a 20 20 45 78 70 72 20 2a 70 45 3b 0a 0a 20  ;.  Expr *pE;.. 
26c0c 20 70 45 31 61 20 3d 20 73 71 6c 69 74 65 33 43   pE1a = sqlite3C
26c0d 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72  reateIdExpr(pPar
26c0e 73 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 70 45 32  se, zCol);.  pE2
26c0f 61 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  a = sqlite3Creat
26c10 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20  eIdExpr(pParse, 
26c11 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20 7a 41 6c  zCol);.  if( zAl
26c12 69 61 73 31 3d 3d 30 20 29 7b 0a 20 20 20 20 7a  ias1==0 ){.    z
26c13 41 6c 69 61 73 31 20 3d 20 70 54 61 62 31 2d 3e  Alias1 = pTab1->
26c14 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 70 45 31  zName;.  }.  pE1
26c15 62 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  b = sqlite3Creat
26c16 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20  eIdExpr(pParse, 
26c17 7a 41 6c 69 61 73 31 29 3b 0a 20 20 69 66 28 20  zAlias1);.  if( 
26c18 7a 41 6c 69 61 73 32 3d 3d 30 20 29 7b 0a 20 20  zAlias2==0 ){.  
26c19 20 20 7a 41 6c 69 61 73 32 20 3d 20 70 54 61 62    zAlias2 = pTab
26c1a 32 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20  2->zName;.  }.  
26c1b 70 45 32 62 20 3d 20 73 71 6c 69 74 65 33 43 72  pE2b = sqlite3Cr
26c1c 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73  eateIdExpr(pPars
26c1d 65 2c 20 7a 41 6c 69 61 73 32 29 3b 0a 20 20 70  e, zAlias2);.  p
26c1e 45 31 63 20 3d 20 73 71 6c 69 74 65 33 50 45 78  E1c = sqlite3PEx
26c1f 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
26c20 54 2c 20 70 45 31 62 2c 20 70 45 31 61 2c 20 30  T, pE1b, pE1a, 0
26c21 29 3b 0a 20 20 70 45 32 63 20 3d 20 73 71 6c 69  );.  pE2c = sqli
26c22 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
26c23 20 54 4b 5f 44 4f 54 2c 20 70 45 32 62 2c 20 70   TK_DOT, pE2b, p
26c24 45 32 61 2c 20 30 29 3b 0a 20 20 70 45 20 3d 20  E2a, 0);.  pE = 
26c25 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
26c26 72 73 65 2c 20 54 4b 5f 45 51 2c 20 70 45 31 63  rse, TK_EQ, pE1c
26c27 2c 20 70 45 32 63 2c 20 30 29 3b 0a 20 20 69 66  , pE2c, 0);.  if
26c28 28 20 70 45 20 26 26 20 69 73 4f 75 74 65 72 4a  ( pE && isOuterJ
26c29 6f 69 6e 20 29 7b 0a 20 20 20 20 45 78 70 72 53  oin ){.    ExprS
26c2a 65 74 50 72 6f 70 65 72 74 79 28 70 45 2c 20 45  etProperty(pE, E
26c2b 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20  P_FromJoin);.   
26c2c 20 70 45 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54   pE->iRightJoinT
26c2d 61 62 6c 65 20 3d 20 69 52 69 67 68 74 4a 6f 69  able = iRightJoi
26c2e 6e 54 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 2a 70  nTable;.  }.  *p
26c2f 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
26c30 78 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64  xprAnd(pParse->d
26c31 62 2c 2a 70 70 45 78 70 72 2c 20 70 45 29 3b 0a  b,*ppExpr, pE);.
26c32 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
26c33 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f   EP_FromJoin pro
26c34 70 65 72 74 79 20 6f 6e 20 61 6c 6c 20 74 65 72  perty on all ter
26c35 6d 73 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20  ms of the given 
26c36 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 41  expression..** A
26c37 6e 64 20 73 65 74 20 74 68 65 20 45 78 70 72 2e  nd set the Expr.
26c38 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
26c39 74 6f 20 69 54 61 62 6c 65 20 66 6f 72 20 65 76  to iTable for ev
26c3a 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 0a  ery term in the.
26c3b 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  ** expression..*
26c3c 2a 0a 2a 2a 20 54 68 65 20 45 50 5f 46 72 6f 6d  *.** The EP_From
26c3d 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 69 73  Join property is
26c3e 20 75 73 65 64 20 6f 6e 20 74 65 72 6d 73 20 6f   used on terms o
26c3f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  f an expression 
26c40 74 6f 20 74 65 6c 6c 0a 2a 2a 20 74 68 65 20 4c  to tell.** the L
26c41 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 70  EFT OUTER JOIN p
26c42 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 20  rocessing logic 
26c43 74 68 61 74 20 74 68 69 73 20 74 65 72 6d 20 69  that this term i
26c44 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a  s part of the.**
26c45 20 6a 6f 69 6e 20 72 65 73 74 72 69 63 74 69 6f   join restrictio
26c46 6e 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  n specified in t
26c47 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63  he ON or USING c
26c48 6c 61 75 73 65 20 61 6e 64 20 6e 6f 74 20 61 20  lause and not a 
26c49 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 6d  part.** of the m
26c4a 6f 72 65 20 67 65 6e 65 72 61 6c 20 57 48 45 52  ore general WHER
26c4b 45 20 63 6c 61 75 73 65 2e 20 20 54 68 65 73 65  E clause.  These
26c4c 20 74 65 72 6d 73 20 61 72 65 20 6d 6f 76 65 64   terms are moved
26c4d 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20   over to the.** 
26c4e 57 48 45 52 45 20 63 6c 61 75 73 65 20 64 75 72  WHERE clause dur
26c4f 69 6e 67 20 6a 6f 69 6e 20 70 72 6f 63 65 73 73  ing join process
26c50 69 6e 67 20 62 75 74 20 77 65 20 6e 65 65 64 20  ing but we need 
26c51 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74  to remember that
26c52 20 74 68 65 79 0a 2a 2a 20 6f 72 69 67 69 6e 61   they.** origina
26c53 74 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72  ted in the ON or
26c54 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a   USING clause..*
26c55 2a 0a 2a 2a 20 54 68 65 20 45 78 70 72 2e 69 52  *.** The Expr.iR
26c56 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74 65  ightJoinTable te
26c57 6c 6c 73 20 74 68 65 20 57 48 45 52 45 20 63 6c  lls the WHERE cl
26c58 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20  ause processing 
26c59 74 68 61 74 20 74 68 65 0a 2a 2a 20 65 78 70 72  that the.** expr
26c5a 65 73 73 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f  ession depends o
26c5b 6e 20 74 61 62 6c 65 20 69 52 69 67 68 74 4a 6f  n table iRightJo
26c5c 69 6e 54 61 62 6c 65 20 65 76 65 6e 20 69 66 20  inTable even if 
26c5d 74 68 61 74 20 74 61 62 6c 65 20 69 73 20 6e 6f  that table is no
26c5e 74 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20  t.** explicitly 
26c5f 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68 65  mentioned in the
26c60 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68   expression.  Th
26c61 61 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  at information i
26c62 73 20 6e 65 65 64 65 64 0a 2a 2a 20 66 6f 72 20  s needed.** for 
26c63 63 61 73 65 73 20 6c 69 6b 65 20 74 68 69 73 3a  cases like this:
26c64 0a 2a 2a 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54  .**.**    SELECT
26c65 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20   * FROM t1 LEFT 
26c66 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d  JOIN t2 ON t1.a=
26c67 74 32 2e 62 20 41 4e 44 20 74 31 2e 78 3d 35 0a  t2.b AND t1.x=5.
26c68 2a 2a 0a 2a 2a 20 54 68 65 20 77 68 65 72 65 20  **.** The where 
26c69 63 6c 61 75 73 65 20 6e 65 65 64 73 20 74 6f 20  clause needs to 
26c6a 64 65 66 65 72 20 74 68 65 20 68 61 6e 64 6c 69  defer the handli
26c6b 6e 67 20 6f 66 20 74 68 65 20 74 31 2e 78 3d 35  ng of the t1.x=5
26c6c 0a 2a 2a 20 74 65 72 6d 20 75 6e 74 69 6c 20 61  .** term until a
26c6d 66 74 65 72 20 74 68 65 20 74 32 20 6c 6f 6f 70  fter the t2 loop
26c6e 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 49   of the join.  I
26c6f 6e 20 74 68 61 74 20 77 61 79 2c 20 61 0a 2a 2a  n that way, a.**
26c70 20 4e 55 4c 4c 20 74 32 20 72 6f 77 20 77 69 6c   NULL t2 row wil
26c71 6c 20 62 65 20 69 6e 73 65 72 74 65 64 20 77 68  l be inserted wh
26c72 65 6e 65 76 65 72 20 74 31 2e 78 21 3d 35 2e 20  enever t1.x!=5. 
26c73 20 49 66 20 77 65 20 64 6f 20 6e 6f 74 0a 2a 2a   If we do not.**
26c74 20 64 65 66 65 72 20 74 68 65 20 68 61 6e 64 6c   defer the handl
26c75 69 6e 67 20 6f 66 20 74 31 2e 78 3d 35 2c 20 69  ing of t1.x=5, i
26c76 74 20 77 69 6c 6c 20 62 65 20 70 72 6f 63 65 73  t will be proces
26c77 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a  sed immediately.
26c78 2a 2a 20 61 66 74 65 72 20 74 68 65 20 74 31 20  ** after the t1 
26c79 6c 6f 6f 70 20 61 6e 64 20 72 6f 77 73 20 77 69  loop and rows wi
26c7a 74 68 20 74 31 2e 78 21 3d 35 20 77 69 6c 6c 20  th t1.x!=5 will 
26c7b 6e 65 76 65 72 20 61 70 70 65 61 72 20 69 6e 0a  never appear in.
26c7c 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2c 20 77  ** the output, w
26c7d 68 69 63 68 20 69 73 20 69 6e 63 6f 72 72 65 63  hich is incorrec
26c7e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
26c7f 64 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 45 78  d setJoinExpr(Ex
26c80 70 72 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c  pr *p, int iTabl
26c81 65 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 20 29  e){.  while( p )
26c82 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f  {.    ExprSetPro
26c83 70 65 72 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d  perty(p, EP_From
26c84 4a 6f 69 6e 29 3b 0a 20 20 20 20 70 2d 3e 69 52  Join);.    p->iR
26c85 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20  ightJoinTable = 
26c86 69 54 61 62 6c 65 3b 0a 20 20 20 20 73 65 74 4a  iTable;.    setJ
26c87 6f 69 6e 45 78 70 72 28 70 2d 3e 70 4c 65 66 74  oinExpr(p->pLeft
26c88 2c 20 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 70  , iTable);.    p
26c89 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20   = p->pRight;.  
26c8a 7d 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  } .}../*.** This
26c8b 20 72 6f 75 74 69 6e 65 20 70 72 6f 63 65 73 73   routine process
26c8c 65 73 20 74 68 65 20 6a 6f 69 6e 20 69 6e 66 6f  es the join info
26c8d 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 53 45  rmation for a SE
26c8e 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
26c8f 2a 2a 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20  ** ON and USING 
26c90 63 6c 61 75 73 65 73 20 61 72 65 20 63 6f 6e 76  clauses are conv
26c91 65 72 74 65 64 20 69 6e 74 6f 20 65 78 74 72 61  erted into extra
26c92 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48   terms of the WH
26c93 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 4e  ERE clause..** N
26c94 41 54 55 52 41 4c 20 6a 6f 69 6e 73 20 61 6c 73  ATURAL joins als
26c95 6f 20 63 72 65 61 74 65 20 65 78 74 72 61 20 57  o create extra W
26c96 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
26c97 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72  s..**.** The ter
26c98 6d 73 20 6f 66 20 61 20 46 52 4f 4d 20 63 6c 61  ms of a FROM cla
26c99 75 73 65 20 61 72 65 20 63 6f 6e 74 61 69 6e 65  use are containe
26c9a 64 20 69 6e 20 74 68 65 20 53 65 6c 65 63 74 2e  d in the Select.
26c9b 70 53 72 63 20 73 74 72 75 63 74 75 72 65 2e 0a  pSrc structure..
26c9c 2a 2a 20 54 68 65 20 6c 65 66 74 20 6d 6f 73 74  ** The left most
26c9d 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 66 69   table is the fi
26c9e 72 73 74 20 65 6e 74 72 79 20 69 6e 20 53 65 6c  rst entry in Sel
26c9f 65 63 74 2e 70 53 72 63 2e 20 20 54 68 65 20 72  ect.pSrc.  The r
26ca0 69 67 68 74 2d 6d 6f 73 74 0a 2a 2a 20 74 61 62  ight-most.** tab
26ca1 6c 65 20 69 73 20 74 68 65 20 6c 61 73 74 20 65  le is the last e
26ca2 6e 74 72 79 2e 20 20 54 68 65 20 6a 6f 69 6e 20  ntry.  The join 
26ca3 6f 70 65 72 61 74 6f 72 20 69 73 20 68 65 6c 64  operator is held
26ca4 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74 6f   in the entry to
26ca5 0a 2a 2a 20 74 68 65 20 6c 65 66 74 2e 20 20 54  .** the left.  T
26ca6 68 75 73 20 65 6e 74 72 79 20 30 20 63 6f 6e 74  hus entry 0 cont
26ca7 61 69 6e 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70  ains the join op
26ca8 65 72 61 74 6f 72 20 66 6f 72 20 74 68 65 20 6a  erator for the j
26ca9 6f 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 65  oin between.** e
26caa 6e 74 72 69 65 73 20 30 20 61 6e 64 20 31 2e 20  ntries 0 and 1. 
26cab 20 41 6e 79 20 4f 4e 20 6f 72 20 55 53 49 4e 47   Any ON or USING
26cac 20 63 6c 61 75 73 65 73 20 61 73 73 6f 63 69 61   clauses associa
26cad 74 65 64 20 77 69 74 68 20 74 68 65 20 6a 6f 69  ted with the joi
26cae 6e 20 61 72 65 0a 2a 2a 20 61 6c 73 6f 20 61 74  n are.** also at
26caf 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65  tached to the le
26cb0 66 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20  ft entry..**.** 
26cb1 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
26cb2 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
26cb3 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e  of errors encoun
26cb4 74 65 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  tered..*/.static
26cb5 20 69 6e 74 20 73 71 6c 69 74 65 50 72 6f 63 65   int sqliteProce
26cb6 73 73 4a 6f 69 6e 28 50 61 72 73 65 20 2a 70 50  ssJoin(Parse *pP
26cb7 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 29  arse, Select *p)
26cb8 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  {.  SrcList *pSr
26cb9 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
26cba 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65      /* All table
26cbb 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
26cbc 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c  ause */.  int i,
26cbd 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
26cbe 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
26cbf 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
26cc0 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
26cc1 74 65 6d 20 2a 70 4c 65 66 74 3b 20 20 20 20 20  tem *pLeft;     
26cc2 2f 2a 20 4c 65 66 74 20 74 61 62 6c 65 20 62 65  /* Left table be
26cc3 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 20 20  ing joined */.  
26cc4 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
26cc5 74 65 6d 20 2a 70 52 69 67 68 74 3b 20 20 20 20  tem *pRight;    
26cc6 2f 2a 20 52 69 67 68 74 20 74 61 62 6c 65 20 62  /* Right table b
26cc7 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 0a  eing joined */..
26cc8 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63    pSrc = p->pSrc
26cc9 3b 0a 20 20 70 4c 65 66 74 20 3d 20 26 70 53 72  ;.  pLeft = &pSr
26cca 63 2d 3e 61 5b 30 5d 3b 0a 20 20 70 52 69 67 68  c->a[0];.  pRigh
26ccb 74 20 3d 20 26 70 4c 65 66 74 5b 31 5d 3b 0a 20  t = &pLeft[1];. 
26ccc 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63   for(i=0; i<pSrc
26ccd 2d 3e 6e 53 72 63 2d 31 3b 20 69 2b 2b 2c 20 70  ->nSrc-1; i++, p
26cce 52 69 67 68 74 2b 2b 2c 20 70 4c 65 66 74 2b 2b  Right++, pLeft++
26ccf 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4c  ){.    Table *pL
26cd0 65 66 74 54 61 62 20 3d 20 70 4c 65 66 74 2d 3e  eftTab = pLeft->
26cd1 70 54 61 62 3b 0a 20 20 20 20 54 61 62 6c 65 20  pTab;.    Table 
26cd2 2a 70 52 69 67 68 74 54 61 62 20 3d 20 70 52 69  *pRightTab = pRi
26cd3 67 68 74 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69  ght->pTab;.    i
26cd4 6e 74 20 69 73 4f 75 74 65 72 3b 0a 0a 20 20 20  nt isOuter;..   
26cd5 20 69 66 28 20 4e 45 56 45 52 28 70 4c 65 66 74   if( NEVER(pLeft
26cd6 54 61 62 3d 3d 30 20 7c 7c 20 70 52 69 67 68 74  Tab==0 || pRight
26cd7 54 61 62 3d 3d 30 29 20 29 20 63 6f 6e 74 69 6e  Tab==0) ) contin
26cd8 75 65 3b 0a 20 20 20 20 69 73 4f 75 74 65 72 20  ue;.    isOuter 
26cd9 3d 20 28 70 52 69 67 68 74 2d 3e 6a 6f 69 6e 74  = (pRight->joint
26cda 79 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21  ype & JT_OUTER)!
26cdb 3d 30 3b 0a 0a 20 20 20 20 2f 2a 20 57 68 65 6e  =0;..    /* When
26cdc 20 74 68 65 20 4e 41 54 55 52 41 4c 20 6b 65 79   the NATURAL key
26cdd 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 2c  word is present,
26cde 20 61 64 64 20 57 48 45 52 45 20 63 6c 61 75 73   add WHERE claus
26cdf 65 20 74 65 72 6d 73 20 66 6f 72 0a 20 20 20 20  e terms for.    
26ce0 2a 2a 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20  ** every column 
26ce1 74 68 61 74 20 74 68 65 20 74 77 6f 20 74 61 62  that the two tab
26ce2 6c 65 73 20 68 61 76 65 20 69 6e 20 63 6f 6d 6d  les have in comm
26ce3 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  on..    */.    i
26ce4 66 28 20 70 52 69 67 68 74 2d 3e 6a 6f 69 6e 74  f( pRight->joint
26ce5 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c  ype & JT_NATURAL
26ce6 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 52   ){.      if( pR
26ce7 69 67 68 74 2d 3e 70 4f 6e 20 7c 7c 20 70 52 69  ight->pOn || pRi
26ce8 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20  ght->pUsing ){. 
26ce9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
26cea 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
26ceb 61 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 20 6d  a NATURAL join m
26cec 61 79 20 6e 6f 74 20 68 61 76 65 20 22 0a 20 20  ay not have ".  
26ced 20 20 20 20 20 20 20 20 20 22 61 6e 20 4f 4e 20           "an ON 
26cee 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 22  or USING clause"
26cef 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 72 65  , 0);.        re
26cf0 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
26cf1 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
26cf2 3c 70 4c 65 66 74 54 61 62 2d 3e 6e 43 6f 6c 3b  <pLeftTab->nCol;
26cf3 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63   j++){.        c
26cf4 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 4c 65  har *zName = pLe
26cf5 66 74 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a  ftTab->aCol[j].z
26cf6 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66  Name;.        if
26cf7 28 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 52  ( columnIndex(pR
26cf8 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3e  ightTab, zName)>
26cf9 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
26cfa 61 64 64 57 68 65 72 65 54 65 72 6d 28 70 50 61  addWhereTerm(pPa
26cfb 72 73 65 2c 20 7a 4e 61 6d 65 2c 20 70 4c 65 66  rse, zName, pLef
26cfc 74 54 61 62 2c 20 70 4c 65 66 74 2d 3e 7a 41 6c  tTab, pLeft->zAl
26cfd 69 61 73 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ias, .          
26cfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26cff 20 20 20 20 70 52 69 67 68 74 54 61 62 2c 20 70      pRightTab, p
26d00 52 69 67 68 74 2d 3e 7a 41 6c 69 61 73 2c 0a 20  Right->zAlias,. 
26d01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d02 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69               pRi
26d03 67 68 74 2d 3e 69 43 75 72 73 6f 72 2c 20 26 70  ght->iCursor, &p
26d04 2d 3e 70 57 68 65 72 65 2c 20 69 73 4f 75 74 65  ->pWhere, isOute
26d05 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 0a 20  r);.          . 
26d06 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
26d07 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
26d08 69 73 61 6c 6c 6f 77 20 62 6f 74 68 20 4f 4e 20  isallow both ON 
26d09 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
26d0a 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f  s in the same jo
26d0b 69 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  in.    */.    if
26d0c 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 26 26  ( pRight->pOn &&
26d0d 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20   pRight->pUsing 
26d0e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
26d0f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
26d10 20 22 63 61 6e 6e 6f 74 20 68 61 76 65 20 62 6f   "cannot have bo
26d11 74 68 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20  th ON and USING 
26d12 22 0a 20 20 20 20 20 20 20 20 22 63 6c 61 75 73  ".        "claus
26d13 65 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6a  es in the same j
26d14 6f 69 6e 22 29 3b 0a 20 20 20 20 20 20 72 65 74  oin");.      ret
26d15 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20  urn 1;.    }..  
26d16 20 20 2f 2a 20 41 64 64 20 74 68 65 20 4f 4e 20    /* Add the ON 
26d17 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20 65 6e  clause to the en
26d18 64 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  d of the WHERE c
26d19 6c 61 75 73 65 2c 20 63 6f 6e 6e 65 63 74 65 64  lause, connected
26d1a 20 62 79 0a 20 20 20 20 2a 2a 20 61 6e 20 41 4e   by.    ** an AN
26d1b 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20  D operator..    
26d1c 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68  */.    if( pRigh
26d1d 74 2d 3e 70 4f 6e 20 29 7b 0a 20 20 20 20 20 20  t->pOn ){.      
26d1e 69 66 28 20 69 73 4f 75 74 65 72 20 29 20 73 65  if( isOuter ) se
26d1f 74 4a 6f 69 6e 45 78 70 72 28 70 52 69 67 68 74  tJoinExpr(pRight
26d20 2d 3e 70 4f 6e 2c 20 70 52 69 67 68 74 2d 3e 69  ->pOn, pRight->i
26d21 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 70  Cursor);.      p
26d22 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74  ->pWhere = sqlit
26d23 65 33 45 78 70 72 41 6e 64 28 70 50 61 72 73 65  e3ExprAnd(pParse
26d24 2d 3e 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c  ->db, p->pWhere,
26d25 20 70 52 69 67 68 74 2d 3e 70 4f 6e 29 3b 0a 20   pRight->pOn);. 
26d26 20 20 20 20 20 70 52 69 67 68 74 2d 3e 70 4f 6e       pRight->pOn
26d27 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 0;.    }..   
26d28 20 2f 2a 20 43 72 65 61 74 65 20 65 78 74 72 61   /* Create extra
26d29 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20 57 48   terms on the WH
26d2a 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20 65  ERE clause for e
26d2b 61 63 68 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64  ach column named
26d2c 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 55  .    ** in the U
26d2d 53 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 45 78  SING clause.  Ex
26d2e 61 6d 70 6c 65 3a 20 49 66 20 74 68 65 20 74 77  ample: If the tw
26d2f 6f 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6a  o tables to be j
26d30 6f 69 6e 65 64 20 61 72 65 20 0a 20 20 20 20 2a  oined are .    *
26d31 2a 20 41 20 61 6e 64 20 42 20 61 6e 64 20 74 68  * A and B and th
26d32 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6e  e USING clause n
26d33 61 6d 65 73 20 58 2c 20 59 2c 20 61 6e 64 20 5a  ames X, Y, and Z
26d34 2c 20 74 68 65 6e 20 61 64 64 20 74 68 69 73 0a  , then add this.
26d35 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 57 48      ** to the WH
26d36 45 52 45 20 63 6c 61 75 73 65 3a 20 20 20 20 41  ERE clause:    A
26d37 2e 58 3d 42 2e 58 20 41 4e 44 20 41 2e 59 3d 42  .X=B.X AND A.Y=B
26d38 2e 59 20 41 4e 44 20 41 2e 5a 3d 42 2e 5a 0a 20  .Y AND A.Z=B.Z. 
26d39 20 20 20 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20     ** Report an 
26d3a 65 72 72 6f 72 20 69 66 20 61 6e 79 20 63 6f 6c  error if any col
26d3b 75 6d 6e 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e  umn mentioned in
26d3c 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73   the USING claus
26d3d 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20  e is.    ** not 
26d3e 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 62 6f 74  contained in bot
26d3f 68 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6a  h tables to be j
26d40 6f 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  oined..    */.  
26d41 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 55    if( pRight->pU
26d42 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 49 64  sing ){.      Id
26d43 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 52  List *pList = pR
26d44 69 67 68 74 2d 3e 70 55 73 69 6e 67 3b 0a 20 20  ight->pUsing;.  
26d45 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
26d46 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b  List->nId; j++){
26d47 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
26d48 4e 61 6d 65 20 3d 20 70 4c 69 73 74 2d 3e 61 5b  Name = pList->a[
26d49 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  j].zName;.      
26d4a 20 20 69 66 28 20 63 6f 6c 75 6d 6e 49 6e 64 65    if( columnInde
26d4b 78 28 70 4c 65 66 74 54 61 62 2c 20 7a 4e 61 6d  x(pLeftTab, zNam
26d4c 65 29 3c 30 20 7c 7c 20 63 6f 6c 75 6d 6e 49 6e  e)<0 || columnIn
26d4d 64 65 78 28 70 52 69 67 68 74 54 61 62 2c 20 7a  dex(pRightTab, z
26d4e 4e 61 6d 65 29 3c 30 20 29 7b 0a 20 20 20 20 20  Name)<0 ){.     
26d4f 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
26d50 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
26d51 6e 6e 6f 74 20 6a 6f 69 6e 20 75 73 69 6e 67 20  nnot join using 
26d52 63 6f 6c 75 6d 6e 20 25 73 20 2d 20 63 6f 6c 75  column %s - colu
26d53 6d 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  mn ".           
26d54 20 22 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e   "not present in
26d55 20 62 6f 74 68 20 74 61 62 6c 65 73 22 2c 20 7a   both tables", z
26d56 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Name);.         
26d57 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
26d58 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 64 64     }.        add
26d59 57 68 65 72 65 54 65 72 6d 28 70 50 61 72 73 65  WhereTerm(pParse
26d5a 2c 20 7a 4e 61 6d 65 2c 20 70 4c 65 66 74 54 61  , zName, pLeftTa
26d5b 62 2c 20 70 4c 65 66 74 2d 3e 7a 41 6c 69 61 73  b, pLeft->zAlias
26d5c 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
26d5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
26d5e 52 69 67 68 74 54 61 62 2c 20 70 52 69 67 68 74  RightTab, pRight
26d5f 2d 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20 20 20  ->zAlias,.      
26d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d61 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 69 43        pRight->iC
26d62 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57 68 65 72  ursor, &p->pWher
26d63 65 2c 20 69 73 4f 75 74 65 72 29 3b 0a 20 20 20  e, isOuter);.   
26d64 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
26d65 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
26d66 0a 2a 2a 20 49 6e 73 65 72 74 20 63 6f 64 65 20  .** Insert code 
26d67 69 6e 74 6f 20 22 76 22 20 74 68 61 74 20 77 69  into "v" that wi
26d68 6c 6c 20 70 75 73 68 20 74 68 65 20 72 65 63 6f  ll push the reco
26d69 72 64 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66  rd on the top of
26d6a 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69 6e   the.** stack in
26d6b 74 6f 20 74 68 65 20 73 6f 72 74 65 72 2e 0a 2a  to the sorter..*
26d6c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 75  /.static void pu
26d6d 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 0a 20 20  shOntoSorter(.  
26d6e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
26d6f 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
26d70 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
26d71 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
26d72 2c 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45  ,    /* The ORDE
26d73 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  R BY clause */. 
26d74 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
26d75 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77  ,       /* The w
26d76 68 6f 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74  hole SELECT stat
26d77 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72  ement */.  int r
26d78 65 67 44 61 74 61 20 20 20 20 20 20 20 20 20 20  egData          
26d79 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f    /* Register ho
26d7a 6c 64 69 6e 67 20 64 61 74 61 20 74 6f 20 62 65  lding data to be
26d7b 20 73 6f 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20   sorted */.){.  
26d7c 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
26d7d 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6e  ->pVdbe;.  int n
26d7e 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d  Expr = pOrderBy-
26d7f 3e 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20 72 65  >nExpr;.  int re
26d80 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33 47  gBase = sqlite3G
26d81 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
26d82 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a 20 20  se, nExpr+2);.  
26d83 69 6e 74 20 72 65 67 52 65 63 6f 72 64 20 3d 20  int regRecord = 
26d84 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
26d85 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  g(pParse);.  sql
26d86 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
26d87 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4f 72  List(pParse, pOr
26d88 64 65 72 42 79 2c 20 72 65 67 42 61 73 65 2c 20  derBy, regBase, 
26d89 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  0);.  sqlite3Vdb
26d8a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65  eAddOp2(v, OP_Se
26d8b 71 75 65 6e 63 65 2c 20 70 4f 72 64 65 72 42 79  quence, pOrderBy
26d8c 2d 3e 69 45 43 75 72 73 6f 72 2c 20 72 65 67 42  ->iECursor, regB
26d8d 61 73 65 2b 6e 45 78 70 72 29 3b 0a 20 20 73 71  ase+nExpr);.  sq
26d8e 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
26d8f 65 28 70 50 61 72 73 65 2c 20 72 65 67 44 61 74  e(pParse, regDat
26d90 61 2c 20 72 65 67 42 61 73 65 2b 6e 45 78 70 72  a, regBase+nExpr
26d91 2b 31 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65  +1, 1);.  sqlite
26d92 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
26d93 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
26d94 67 42 61 73 65 2c 20 6e 45 78 70 72 20 2b 20 32  gBase, nExpr + 2
26d95 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
26d96 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26d97 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
26d98 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43  t, pOrderBy->iEC
26d99 75 72 73 6f 72 2c 20 72 65 67 52 65 63 6f 72 64  ursor, regRecord
26d9a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
26d9b 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
26d9c 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  e, regRecord);. 
26d9d 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
26d9e 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
26d9f 20 72 65 67 42 61 73 65 2c 20 6e 45 78 70 72 2b   regBase, nExpr+
26da0 32 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63  2);.  if( pSelec
26da1 74 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  t->iLimit ){.   
26da2 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64 64 72   int addr1, addr
26da3 32 3b 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d 69  2;.    int iLimi
26da4 74 3b 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65  t;.    if( pSele
26da5 63 74 2d 3e 69 4f 66 66 73 65 74 20 29 7b 0a 20  ct->iOffset ){. 
26da6 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 53       iLimit = pS
26da7 65 6c 65 63 74 2d 3e 69 4f 66 66 73 65 74 2b 31  elect->iOffset+1
26da8 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
26da9 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c     iLimit = pSel
26daa 65 63 74 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20  ect->iLimit;.   
26dab 20 7d 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73   }.    addr1 = s
26dac 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
26dad 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69  (v, OP_IfZero, i
26dae 4c 69 6d 69 74 29 3b 0a 20 20 20 20 73 71 6c 69  Limit);.    sqli
26daf 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26db0 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4c 69 6d   OP_AddImm, iLim
26db1 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 61 64 64  it, -1);.    add
26db2 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r2 = sqlite3Vdbe
26db3 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74  AddOp0(v, OP_Got
26db4 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  o);.    sqlite3V
26db5 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
26db6 64 64 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  ddr1);.    sqlit
26db7 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
26db8 4f 50 5f 4c 61 73 74 2c 20 70 4f 72 64 65 72 42  OP_Last, pOrderB
26db9 79 2d 3e 69 45 43 75 72 73 6f 72 29 3b 0a 20 20  y->iECursor);.  
26dba 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26dbb 4f 70 31 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65  Op1(v, OP_Delete
26dbc 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  , pOrderBy->iECu
26dbd 72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  rsor);.    sqlit
26dbe 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
26dbf 2c 20 61 64 64 72 32 29 3b 0a 20 20 20 20 70 53  , addr2);.    pS
26dc0 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 20 3d 20  elect->iLimit = 
26dc1 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
26dc2 41 64 64 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c  Add code to impl
26dc3 65 6d 65 6e 74 20 74 68 65 20 4f 46 46 53 45 54  ement the OFFSET
26dc4 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
26dc5 63 6f 64 65 4f 66 66 73 65 74 28 0a 20 20 56 64  codeOffset(.  Vd
26dc6 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20 20  be *v,          
26dc7 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
26dc8 20 69 6e 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f   into this VM */
26dc9 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
26dca 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
26dcb 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  CT statement bei
26dcc 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e  ng coded */.  in
26dcd 74 20 69 43 6f 6e 74 69 6e 75 65 20 20 20 20 20  t iContinue     
26dce 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
26dcf 73 6b 69 70 20 74 68 65 20 63 75 72 72 65 6e 74  skip the current
26dd0 20 72 65 63 6f 72 64 20 2a 2f 0a 29 7b 0a 20 20   record */.){.  
26dd1 69 66 28 20 70 2d 3e 69 4f 66 66 73 65 74 20 26  if( p->iOffset &
26dd2 26 20 69 43 6f 6e 74 69 6e 75 65 21 3d 30 20 29  & iContinue!=0 )
26dd3 7b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a  {.    int addr;.
26dd4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26dd5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
26dd6 6d 6d 2c 20 70 2d 3e 69 4f 66 66 73 65 74 2c 20  mm, p->iOffset, 
26dd7 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20  -1);.    addr = 
26dd8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26dd9 31 28 76 2c 20 4f 50 5f 49 66 4e 65 67 2c 20 70  1(v, OP_IfNeg, p
26dda 2d 3e 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  ->iOffset);.    
26ddb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26ddc 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
26ddd 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20   iContinue);.   
26dde 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
26ddf 20 22 73 6b 69 70 20 4f 46 46 53 45 54 20 72 65   "skip OFFSET re
26de0 63 6f 72 64 73 22 29 29 3b 0a 20 20 20 20 73 71  cords"));.    sq
26de1 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
26de2 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a  e(v, addr);.  }.
26de3 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64  }../*.** Add cod
26de4 65 20 74 68 61 74 20 77 69 6c 6c 20 63 68 65 63  e that will chec
26de5 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  k to make sure t
26de6 68 65 20 4e 20 72 65 67 69 73 74 65 72 73 20 73  he N registers s
26de7 74 61 72 74 69 6e 67 20 61 74 20 69 4d 65 6d 0a  tarting at iMem.
26de8 2a 2a 20 66 6f 72 6d 20 61 20 64 69 73 74 69 6e  ** form a distin
26de9 63 74 20 65 6e 74 72 79 2e 20 20 69 54 61 62 20  ct entry.  iTab 
26dea 69 73 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64  is a sorting ind
26deb 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 70 72  ex that holds pr
26dec 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 73 65 65 6e  eviously.** seen
26ded 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66   combinations of
26dee 20 74 68 65 20 4e 20 76 61 6c 75 65 73 2e 20 20   the N values.  
26def 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 6d  A new entry is m
26df0 61 64 65 20 69 6e 20 69 54 61 62 0a 2a 2a 20 69  ade in iTab.** i
26df1 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 4e 20  f the current N 
26df2 76 61 6c 75 65 73 20 61 72 65 20 6e 65 77 2e 0a  values are new..
26df3 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 74 6f 20  **.** A jump to 
26df4 61 64 64 72 52 65 70 65 61 74 20 69 73 20 6d 61  addrRepeat is ma
26df5 64 65 20 61 6e 64 20 74 68 65 20 4e 2b 31 20 76  de and the N+1 v
26df6 61 6c 75 65 73 20 61 72 65 20 70 6f 70 70 65 64  alues are popped
26df7 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 74 61   from the.** sta
26df8 63 6b 20 69 66 20 74 68 65 20 74 6f 70 20 4e 20  ck if the top N 
26df9 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74  elements are not
26dfa 20 64 69 73 74 69 6e 63 74 2e 0a 2a 2f 0a 73 74   distinct..*/.st
26dfb 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 44 69  atic void codeDi
26dfc 73 74 69 6e 63 74 28 0a 20 20 50 61 72 73 65 20  stinct(.  Parse 
26dfd 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
26dfe 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65  Parsing and code
26dff 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74   generating cont
26e00 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ext */.  int iTa
26e01 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  b,          /* A
26e02 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 75   sorting index u
26e03 73 65 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20  sed to test for 
26e04 64 69 73 74 69 6e 63 74 6e 65 73 73 20 2a 2f 0a  distinctness */.
26e05 20 20 69 6e 74 20 61 64 64 72 52 65 70 65 61 74    int addrRepeat
26e06 2c 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ,    /* Jump to 
26e07 68 65 72 65 20 69 66 20 6e 6f 74 20 64 69 73 74  here if not dist
26e08 69 6e 63 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c  inct */.  int N,
26e09 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26e0a 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  Number of elemen
26e0b 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d  ts */.  int iMem
26e0c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
26e0d 72 73 74 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29  rst element */.)
26e0e 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  {.  Vdbe *v;.  i
26e0f 6e 74 20 72 31 3b 0a 0a 20 20 76 20 3d 20 70 50  nt r1;..  v = pP
26e10 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 72  arse->pVdbe;.  r
26e11 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
26e12 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
26e13 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26e14 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
26e15 6f 72 64 2c 20 69 4d 65 6d 2c 20 4e 2c 20 72 31  ord, iMem, N, r1
26e16 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
26e17 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75  AddOp3(v, OP_Fou
26e18 6e 64 2c 20 69 54 61 62 2c 20 61 64 64 72 52 65  nd, iTab, addrRe
26e19 70 65 61 74 2c 20 72 31 29 3b 0a 20 20 73 71 6c  peat, r1);.  sql
26e1a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26e1b 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
26e1c 69 54 61 62 2c 20 72 31 29 3b 0a 20 20 73 71 6c  iTab, r1);.  sql
26e1d 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
26e1e 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
26e1f 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
26e20 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
26e21 67 65 20 77 68 65 6e 20 61 20 53 45 4c 45 43 54  ge when a SELECT
26e22 20 69 73 20 75 73 65 64 20 77 69 74 68 69 6e 20   is used within 
26e23 61 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a  a subexpression.
26e24 2a 2a 20 28 65 78 61 6d 70 6c 65 3a 20 20 22 61  ** (example:  "a
26e25 20 49 4e 20 28 53 45 4c 45 43 54 20 2a 20 46 52   IN (SELECT * FR
26e26 4f 4d 20 74 61 62 6c 65 29 22 29 20 62 75 74 20  OM table)") but 
26e27 69 74 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e  it has more than
26e28 20 31 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c   1 result.** col
26e29 75 6d 6e 2e 20 20 57 65 20 64 6f 20 74 68 69 73  umn.  We do this
26e2a 20 69 6e 20 61 20 73 75 62 72 6f 75 74 69 6e 65   in a subroutine
26e2b 20 62 65 63 61 75 73 65 20 74 68 65 20 65 72 72   because the err
26e2c 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 6d 75 6c  or occurs in mul
26e2d 74 69 70 6c 65 0a 2a 2a 20 70 6c 61 63 65 73 2e  tiple.** places.
26e2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
26e2f 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75  heckForMultiColu
26e30 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 0a 20  mnSelectError(. 
26e31 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
26e32 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
26e33 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53 65 6c  ontext. */.  Sel
26e34 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20  ectDest *pDest, 
26e35 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e    /* Destination
26e36 20 6f 66 20 53 45 4c 45 43 54 20 72 65 73 75 6c   of SELECT resul
26e37 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 70  ts */.  int nExp
26e38 72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r            /* 
26e39 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74  Number of result
26e3a 20 63 6f 6c 75 6d 6e 73 20 72 65 74 75 72 6e 65   columns returne
26e3b 64 20 62 79 20 53 45 4c 45 43 54 20 2a 2f 0a 29  d by SELECT */.)
26e3c 7b 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20  {.  int eDest = 
26e3d 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20  pDest->eDest;.  
26e3e 69 66 28 20 6e 45 78 70 72 3e 31 20 26 26 20 28  if( nExpr>1 && (
26e3f 65 44 65 73 74 3d 3d 53 52 54 5f 4d 65 6d 20 7c  eDest==SRT_Mem |
26e40 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74  | eDest==SRT_Set
26e41 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
26e42 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
26e43 20 22 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20   "only a single 
26e44 72 65 73 75 6c 74 20 61 6c 6c 6f 77 65 64 20 66  result allowed f
26e45 6f 72 20 22 0a 20 20 20 20 20 20 20 22 61 20 53  or ".       "a S
26e46 45 4c 45 43 54 20 74 68 61 74 20 69 73 20 70 61  ELECT that is pa
26e47 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  rt of an express
26e48 69 6f 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72  ion");.    retur
26e49 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 1;.  }else{.  
26e4a 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
26e4b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
26e4c 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20  utine generates 
26e4d 74 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65  the code for the
26e4e 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 69   inside of the i
26e4f 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 20  nner loop.** of 
26e50 61 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20  a SELECT..**.** 
26e51 49 66 20 73 72 63 54 61 62 20 61 6e 64 20 6e 43  If srcTab and nC
26e52 6f 6c 75 6d 6e 20 61 72 65 20 62 6f 74 68 20 7a  olumn are both z
26e53 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 45  ero, then the pE
26e54 4c 69 73 74 20 65 78 70 72 65 73 73 69 6f 6e 73  List expressions
26e55 0a 2a 2a 20 61 72 65 20 65 76 61 6c 75 61 74 65  .** are evaluate
26e56 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65  d in order to ge
26e57 74 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  t the data for t
26e58 68 69 73 20 72 6f 77 2e 20 20 49 66 20 6e 43 6f  his row.  If nCo
26e59 6c 75 6d 6e 3e 30 0a 2a 2a 20 74 68 65 6e 20 64  lumn>0.** then d
26e5a 61 74 61 20 69 73 20 70 75 6c 6c 65 64 20 66 72  ata is pulled fr
26e5b 6f 6d 20 73 72 63 54 61 62 20 61 6e 64 20 70 45  om srcTab and pE
26e5c 4c 69 73 74 20 69 73 20 75 73 65 64 20 6f 6e 6c  List is used onl
26e5d 79 20 74 6f 20 67 65 74 20 74 68 65 0a 2a 2a 20  y to get the.** 
26e5e 64 61 74 61 74 79 70 65 73 20 66 6f 72 20 65 61  datatypes for ea
26e5f 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74  ch column..*/.st
26e60 61 74 69 63 20 76 6f 69 64 20 73 65 6c 65 63 74  atic void select
26e61 49 6e 6e 65 72 4c 6f 6f 70 28 0a 20 20 50 61 72  InnerLoop(.  Par
26e62 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
26e63 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
26e64 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
26e65 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
26e66 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
26e67 6f 6d 70 6c 65 74 65 20 73 65 6c 65 63 74 20 73  omplete select s
26e68 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63  tatement being c
26e69 6f 64 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69  oded */.  ExprLi
26e6a 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20  st *pEList,     
26e6b 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c    /* List of val
26e6c 75 65 73 20 62 65 69 6e 67 20 65 78 74 72 61 63  ues being extrac
26e6d 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63  ted */.  int src
26e6e 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  Tab,            
26e6f 20 2f 2a 20 50 75 6c 6c 20 64 61 74 61 20 66 72   /* Pull data fr
26e70 6f 6d 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  om this table */
26e71 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20  .  int nColumn, 
26e72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
26e73 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
26e74 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61  in the source ta
26e75 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ble */.  ExprLis
26e76 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20  t *pOrderBy,    
26e77 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c   /* If not NULL,
26e78 20 73 6f 72 74 20 72 65 73 75 6c 74 73 20 75 73   sort results us
26e79 69 6e 67 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a  ing this key */.
26e7a 20 20 69 6e 74 20 64 69 73 74 69 6e 63 74 2c 20    int distinct, 
26e7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
26e7c 3e 3d 30 2c 20 6d 61 6b 65 20 73 75 72 65 20 72  >=0, make sure r
26e7d 65 73 75 6c 74 73 20 61 72 65 20 64 69 73 74 69  esults are disti
26e7e 6e 63 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  nct */.  SelectD
26e7f 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 20 20  est *pDest,     
26e80 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f   /* How to dispo
26e81 73 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  se of the result
26e82 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74  s */.  int iCont
26e83 69 6e 75 65 2c 20 20 20 20 20 20 20 20 20 20 2f  inue,          /
26e84 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63  * Jump here to c
26e85 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78  ontinue with nex
26e86 74 20 72 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69  t row */.  int i
26e87 42 72 65 61 6b 20 20 20 20 20 20 20 20 20 20 20  Break           
26e88 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
26e89 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20  to break out of 
26e8a 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 20 2a  the inner loop *
26e8b 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
26e8c 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
26e8d 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 68    int i;.  int h
26e8e 61 73 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20  asDistinct;     
26e8f 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
26e90 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f  e DISTINCT keywo
26e91 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
26e92 0a 20 20 69 6e 74 20 72 65 67 52 65 73 75 6c 74  .  int regResult
26e93 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
26e94 2a 20 53 74 61 72 74 20 6f 66 20 6d 65 6d 6f 72  * Start of memor
26e95 79 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74  y holding result
26e96 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 65 44   set */.  int eD
26e97 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65 44 65  est = pDest->eDe
26e98 73 74 3b 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20  st;   /* How to 
26e99 64 69 73 70 6f 73 65 20 6f 66 20 72 65 73 75 6c  dispose of resul
26e9a 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72  ts */.  int iPar
26e9b 6d 20 3d 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  m = pDest->iParm
26e9c 3b 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67  ;   /* First arg
26e9d 75 6d 65 6e 74 20 74 6f 20 64 69 73 70 6f 73 61  ument to disposa
26e9e 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e  l method */.  in
26e9f 74 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 20 20 20  t nResultCol;   
26ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
26ea1 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f  ber of result co
26ea2 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65  lumns */..  asse
26ea3 72 74 28 20 76 20 29 3b 0a 20 20 69 66 28 20 4e  rt( v );.  if( N
26ea4 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74  EVER(v==0) ) ret
26ea5 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  urn;.  assert( p
26ea6 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 68 61  EList!=0 );.  ha
26ea7 73 44 69 73 74 69 6e 63 74 20 3d 20 64 69 73 74  sDistinct = dist
26ea8 69 6e 63 74 3e 3d 30 3b 0a 20 20 69 66 28 20 70  inct>=0;.  if( p
26ea9 4f 72 64 65 72 42 79 3d 3d 30 20 26 26 20 21 68  OrderBy==0 && !h
26eaa 61 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20  asDistinct ){.  
26eab 20 20 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20    codeOffset(v, 
26eac 70 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20  p, iContinue);. 
26ead 20 7d 0a 0a 20 20 2f 2a 20 50 75 6c 6c 20 74 68   }..  /* Pull th
26eae 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 75  e requested colu
26eaf 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  mns..  */.  if( 
26eb0 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20  nColumn>0 ){.   
26eb1 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20 6e 43   nResultCol = nC
26eb2 6f 6c 75 6d 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a  olumn;.  }else{.
26eb3 20 20 20 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d      nResultCol =
26eb4 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a   pEList->nExpr;.
26eb5 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d    }.  if( pDest-
26eb6 3e 69 4d 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20  >iMem==0 ){.    
26eb7 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 70 50  pDest->iMem = pP
26eb8 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20  arse->nMem+1;.  
26eb9 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20    pDest->nMem = 
26eba 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20 20 20 20  nResultCol;.    
26ebb 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20  pParse->nMem += 
26ebc 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20 20 7d 65  nResultCol;.  }e
26ebd 6c 73 65 7b 20 0a 20 20 20 20 61 73 73 65 72 74  lse{ .    assert
26ebe 28 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 3d 3d 6e  ( pDest->nMem==n
26ebf 52 65 73 75 6c 74 43 6f 6c 20 29 3b 0a 20 20 7d  ResultCol );.  }
26ec0 0a 20 20 72 65 67 52 65 73 75 6c 74 20 3d 20 70  .  regResult = p
26ec1 44 65 73 74 2d 3e 69 4d 65 6d 3b 0a 20 20 69 66  Dest->iMem;.  if
26ec2 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20  ( nColumn>0 ){. 
26ec3 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
26ec4 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
26ec5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26ec6 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
26ec7 6e 2c 20 73 72 63 54 61 62 2c 20 69 2c 20 72 65  n, srcTab, i, re
26ec8 67 52 65 73 75 6c 74 2b 69 29 3b 0a 20 20 20 20  gResult+i);.    
26ec9 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65 44  }.  }else if( eD
26eca 65 73 74 21 3d 53 52 54 5f 45 78 69 73 74 73 20  est!=SRT_Exists 
26ecb 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ){.    /* If the
26ecc 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20   destination is 
26ecd 61 6e 20 45 58 49 53 54 53 28 2e 2e 2e 29 20 65  an EXISTS(...) e
26ece 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 20 61  xpression, the a
26ecf 63 74 75 61 6c 0a 20 20 20 20 2a 2a 20 76 61 6c  ctual.    ** val
26ed0 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ues returned by 
26ed1 74 68 65 20 53 45 4c 45 43 54 20 61 72 65 20 6e  the SELECT are n
26ed2 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20  ot required..   
26ed3 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45   */.    sqlite3E
26ed4 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
26ed5 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20  pParse, pEList, 
26ed6 72 65 67 52 65 73 75 6c 74 2c 20 65 44 65 73 74  regResult, eDest
26ed7 3d 3d 53 52 54 5f 4f 75 74 70 75 74 29 3b 0a 20  ==SRT_Output);. 
26ed8 20 7d 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 6e   }.  nColumn = n
26ed9 52 65 73 75 6c 74 43 6f 6c 3b 0a 0a 20 20 2f 2a  ResultCol;..  /*
26eda 20 49 66 20 74 68 65 20 44 49 53 54 49 4e 43 54   If the DISTINCT
26edb 20 6b 65 79 77 6f 72 64 20 77 61 73 20 70 72 65   keyword was pre
26edc 73 65 6e 74 20 6f 6e 20 74 68 65 20 53 45 4c 45  sent on the SELE
26edd 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a  CT statement.  *
26ede 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 77 20 68  * and this row h
26edf 61 73 20 62 65 65 6e 20 73 65 65 6e 20 62 65 66  as been seen bef
26ee0 6f 72 65 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  ore, then do not
26ee1 20 6d 61 6b 65 20 74 68 69 73 20 72 6f 77 0a 20   make this row. 
26ee2 20 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20   ** part of the 
26ee3 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 69  result..  */.  i
26ee4 66 28 20 68 61 73 44 69 73 74 69 6e 63 74 20 29  f( hasDistinct )
26ee5 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  {.    assert( pE
26ee6 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 61  List!=0 );.    a
26ee7 73 73 65 72 74 28 20 70 45 4c 69 73 74 2d 3e 6e  ssert( pEList->n
26ee8 45 78 70 72 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b  Expr==nColumn );
26ee9 0a 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63  .    codeDistinc
26eea 74 28 70 50 61 72 73 65 2c 20 64 69 73 74 69 6e  t(pParse, distin
26eeb 63 74 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6e  ct, iContinue, n
26eec 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 65 73 75 6c  Column, regResul
26eed 74 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 72 64  t);.    if( pOrd
26eee 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  erBy==0 ){.     
26eef 20 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70   codeOffset(v, p
26ef0 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20  , iContinue);.  
26ef1 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 63    }.  }..  if( c
26ef2 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75  heckForMultiColu
26ef3 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50  mnSelectError(pP
26ef4 61 72 73 65 2c 20 70 44 65 73 74 2c 20 70 45 4c  arse, pDest, pEL
26ef5 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20  ist->nExpr) ){. 
26ef6 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
26ef7 20 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20    switch( eDest 
26ef8 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  ){.    /* In thi
26ef9 73 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 65 61  s mode, write ea
26efa 63 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 20  ch query result 
26efb 74 6f 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68  to the key of th
26efc 65 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20  e temporary.    
26efd 2a 2a 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a  ** table iParm..
26efe 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
26eff 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
26f00 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 20 20 63  UND_SELECT.    c
26f01 61 73 65 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b  ase SRT_Union: {
26f02 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 20  .      int r1;. 
26f03 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
26f04 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
26f05 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  se);.      sqlit
26f06 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
26f07 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
26f08 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d  egResult, nColum
26f09 6e 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  n, r1);.      sq
26f0a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26f0b 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
26f0c 20 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20   iParm, r1);.   
26f0d 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
26f0e 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
26f0f 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r1);.      brea
26f10 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
26f11 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 63   Construct a rec
26f12 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 71 75 65  ord from the que
26f13 72 79 20 72 65 73 75 6c 74 2c 20 62 75 74 20 69  ry result, but i
26f14 6e 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a  nstead of.    **
26f15 20 73 61 76 69 6e 67 20 74 68 61 74 20 72 65 63   saving that rec
26f16 6f 72 64 2c 20 75 73 65 20 69 74 20 61 73 20 61  ord, use it as a
26f17 20 6b 65 79 20 74 6f 20 64 65 6c 65 74 65 20 65   key to delete e
26f18 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 0a 20 20 20  lements from.   
26f19 20 2a 2a 20 74 68 65 20 74 65 6d 70 6f 72 61 72   ** the temporar
26f1a 79 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20  y table iParm.. 
26f1b 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
26f1c 52 54 5f 45 78 63 65 70 74 3a 20 7b 0a 20 20 20  RT_Except: {.   
26f1d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26f1e 64 4f 70 33 28 76 2c 20 4f 50 5f 49 64 78 44 65  dOp3(v, OP_IdxDe
26f1f 6c 65 74 65 2c 20 69 50 61 72 6d 2c 20 72 65 67  lete, iParm, reg
26f20 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29  Result, nColumn)
26f21 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
26f22 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
26f23 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65   /* Store the re
26f24 73 75 6c 74 20 61 73 20 64 61 74 61 20 75 73 69  sult as data usi
26f25 6e 67 20 61 20 75 6e 69 71 75 65 20 6b 65 79 2e  ng a unique key.
26f26 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
26f27 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20   SRT_Table:.    
26f28 63 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61  case SRT_EphemTa
26f29 62 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  b: {.      int r
26f2a 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
26f2b 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
26f2c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26f2d 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
26f2e 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75  eRecord, regResu
26f2f 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29  lt, nColumn, r1)
26f30 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64  ;.      if( pOrd
26f31 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20  erBy ){.        
26f32 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70  pushOntoSorter(p
26f33 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c  Parse, pOrderBy,
26f34 20 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 7d   p, r1);.      }
26f35 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
26f36 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65  t r2 = sqlite3Ge
26f37 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
26f38 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
26f39 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26f3a 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72  P_NewRowid, iPar
26f3b 6d 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20  m, r2);.        
26f3c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26f3d 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
26f3e 69 50 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a  iParm, r1, r2);.
26f3f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
26f40 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
26f41 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
26f42 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
26f43 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
26f44 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20  rse, r2);.      
26f45 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  }.      sqlite3R
26f46 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
26f47 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
26f48 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23   break;.    }..#
26f49 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
26f4a 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
26f4b 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 72 65  /* If we are cre
26f4c 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f 72 20  ating a set for 
26f4d 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53 45 4c  an "expr IN (SEL
26f4e 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72  ECT ...)" constr
26f4f 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  uct,.    ** then
26f50 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65   there should be
26f51 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f   a single item o
26f52 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72  n the stack.  Wr
26f53 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20  ite this.    ** 
26f54 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65  item into the se
26f55 74 20 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67  t table with bog
26f56 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a  us data..    */.
26f57 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 65 74      case SRT_Set
26f58 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
26f59 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
26f5a 20 20 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74        p->affinit
26f5b 79 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61  y = sqlite3Compa
26f5c 72 65 41 66 66 69 6e 69 74 79 28 70 45 4c 69 73  reAffinity(pELis
26f5d 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70  t->a[0].pExpr, p
26f5e 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 29 3b  Dest->affinity);
26f5f 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
26f60 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  rBy ){.        /
26f61 2a 20 41 74 20 66 69 72 73 74 20 67 6c 61 6e 63  * At first glanc
26f62 65 20 79 6f 75 20 77 6f 75 6c 64 20 74 68 69 6e  e you would thin
26f63 6b 20 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d  k we could optim
26f64 69 7a 65 20 6f 75 74 20 74 68 65 0a 20 20 20 20  ize out the.    
26f65 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20      ** ORDER BY 
26f66 69 6e 20 74 68 69 73 20 63 61 73 65 20 73 69 6e  in this case sin
26f67 63 65 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20  ce the order of 
26f68 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 73  entries in the s
26f69 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 6f  et.        ** do
26f6a 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 2e 20 20  es not matter.  
26f6b 42 75 74 20 74 68 65 72 65 20 6d 69 67 68 74 20  But there might 
26f6c 62 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  be a LIMIT claus
26f6d 65 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 20 20  e, in which.    
26f6e 20 20 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20      ** case the 
26f6f 6f 72 64 65 72 20 64 6f 65 73 20 6d 61 74 74 65  order does matte
26f70 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 73  r */.        pus
26f71 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72  hOntoSorter(pPar
26f72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c  se, pOrderBy, p,
26f73 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20   regResult);.   
26f74 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26f75 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
26f76 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
26f77 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
26f78 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
26f79 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
26f7a 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31 2c 20  , regResult, 1, 
26f7b 72 31 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79  r1, &p->affinity
26f7c 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
26f7d 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
26f7e 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
26f7f 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  rse, regResult, 
26f80 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
26f81 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26f82 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69   OP_IdxInsert, i
26f83 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20  Parm, r1);.     
26f84 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
26f85 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
26f86 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   r1);.      }.  
26f87 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
26f88 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79 20  ..    /* If any 
26f89 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68 65  row exist in the
26f8a 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65 63   result set, rec
26f8b 6f 72 64 20 74 68 61 74 20 66 61 63 74 20 61 6e  ord that fact an
26f8c 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a  d abort..    */.
26f8d 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 69      case SRT_Exi
26f8e 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  sts: {.      sql
26f8f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26f90 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
26f91 20 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 2f   iParm);.      /
26f92 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75  * The LIMIT clau
26f93 73 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74  se will terminat
26f94 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  e the loop for u
26f95 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b  s */.      break
26f96 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
26f97 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61  If this is a sca
26f98 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20  lar select that 
26f99 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78  is part of an ex
26f9a 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20  pression, then. 
26f9b 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20     ** store the 
26f9c 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61  results in the a
26f9d 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72  ppropriate memor
26f9e 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b  y cell and break
26f9f 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74   out.    ** of t
26fa0 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20  he scan loop..  
26fa1 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
26fa2 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61  T_Mem: {.      a
26fa3 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d  ssert( nColumn==
26fa4 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  1 );.      if( p
26fa5 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
26fa6 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65     pushOntoSorte
26fa7 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  r(pParse, pOrder
26fa8 42 79 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74  By, p, regResult
26fa9 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
26faa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
26fab 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72  xprCodeMove(pPar
26fac 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 69  se, regResult, i
26fad 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20  Parm, 1);.      
26fae 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
26faf 6c 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20  lause will jump 
26fb0 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  out of the loop 
26fb1 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20  for us */.      
26fb2 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
26fb3 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23     }.#endif /* #
26fb4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
26fb5 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a  IT_SUBQUERY */..
26fb6 20 20 20 20 2f 2a 20 53 65 6e 64 20 74 68 65 20      /* Send the 
26fb7 64 61 74 61 20 74 6f 20 74 68 65 20 63 61 6c 6c  data to the call
26fb8 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6f 72  back function or
26fb9 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65   to a subroutine
26fba 2e 20 20 49 6e 20 74 68 65 0a 20 20 20 20 2a 2a  .  In the.    **
26fbb 20 63 61 73 65 20 6f 66 20 61 20 73 75 62 72 6f   case of a subro
26fbc 75 74 69 6e 65 2c 20 74 68 65 20 73 75 62 72 6f  utine, the subro
26fbd 75 74 69 6e 65 20 69 74 73 65 6c 66 20 69 73 20  utine itself is 
26fbe 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a  responsible for.
26fbf 20 20 20 20 2a 2a 20 70 6f 70 70 69 6e 67 20 74      ** popping t
26fc0 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  he data from the
26fc1 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20   stack..    */. 
26fc2 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f     case SRT_Coro
26fc3 75 74 69 6e 65 3a 0a 20 20 20 20 63 61 73 65 20  utine:.    case 
26fc4 53 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20 20  SRT_Output: {.  
26fc5 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
26fc6 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
26fc7 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
26fc8 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
26fc9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
26fca 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26fcb 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52  MakeRecord, regR
26fcc 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20  esult, nColumn, 
26fcd 72 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 73  r1);.        pus
26fce 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72  hOntoSorter(pPar
26fcf 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c  se, pOrderBy, p,
26fd0 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r1);.        sq
26fd1 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
26fd2 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
26fd3 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
26fd4 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
26fd5 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 20 20 20  utine ){.       
26fd6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26fd7 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
26fd8 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20  pDest->iParm);. 
26fd9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
26fda 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26fdb 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
26fdc 6c 74 52 6f 77 2c 20 72 65 67 52 65 73 75 6c 74  ltRow, regResult
26fdd 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
26fde 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
26fdf 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
26fe0 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 52 65  ge(pParse, regRe
26fe1 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a  sult, nColumn);.
26fe2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
26fe3 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20  eak;.    }..#if 
26fe4 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
26fe5 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20  OMIT_TRIGGER).  
26fe6 20 20 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65    /* Discard the
26fe7 20 72 65 73 75 6c 74 73 2e 20 20 54 68 69 73 20   results.  This 
26fe8 69 73 20 75 73 65 64 20 66 6f 72 20 53 45 4c 45  is used for SELE
26fe9 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  CT statements in
26fea 73 69 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 20  side.    ** the 
26feb 62 6f 64 79 20 6f 66 20 61 20 54 52 49 47 47 45  body of a TRIGGE
26fec 52 2e 20 20 54 68 65 20 70 75 72 70 6f 73 65 20  R.  The purpose 
26fed 6f 66 20 73 75 63 68 20 73 65 6c 65 63 74 73 20  of such selects 
26fee 69 73 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 2a  is to call.    *
26fef 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66  * user-defined f
26ff0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61  unctions that ha
26ff1 76 65 20 73 69 64 65 20 65 66 66 65 63 74 73 2e  ve side effects.
26ff2 20 20 57 65 20 64 6f 20 6e 6f 74 20 63 61 72 65    We do not care
26ff3 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20 74 68  .    ** about th
26ff4 65 20 61 63 74 75 61 6c 20 72 65 73 75 6c 74 73  e actual results
26ff5 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e 0a   of the select..
26ff6 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 75      */.    defau
26ff7 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  lt: {.      asse
26ff8 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 44  rt( eDest==SRT_D
26ff9 69 73 63 61 72 64 20 29 3b 0a 20 20 20 20 20 20  iscard );.      
26ffa 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
26ffb 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75  dif.  }..  /* Ju
26ffc 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  mp to the end of
26ffd 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65   the loop if the
26ffe 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68 65   LIMIT is reache
26fff 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  d..  */.  if( p-
27000 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 61  >iLimit ){.    a
27001 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 3d  ssert( pOrderBy=
27002 3d 30 20 29 3b 20 20 2f 2a 20 49 66 20 74 68 65  =0 );  /* If the
27003 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
27004 59 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a 20  Y, the call to. 
27005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27006 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 75             ** pu
27007 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 29 20 77  shOntoSorter() w
27008 6f 75 6c 64 20 68 61 76 65 20 63 6c 65 61 72 65  ould have cleare
27009 64 20 70 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20  d p->iLimit */. 
2700a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2700b 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
2700c 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31  m, p->iLimit, -1
2700d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2700e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2700f 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74  fZero, p->iLimit
27010 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d 0a 7d  , iBreak);.  }.}
27011 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e  ../*.** Given an
27012 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
27013 2c 20 67 65 6e 65 72 61 74 65 20 61 20 4b 65 79  , generate a Key
27014 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  Info structure t
27015 68 61 74 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74  hat records.** t
27016 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
27017 75 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20 65  uence for each e
27018 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 61  xpression in tha
27019 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  t expression lis
2701a 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
2701b 45 78 70 72 4c 69 73 74 20 69 73 20 61 6e 20 4f  ExprList is an O
2701c 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
2701d 20 42 59 20 63 6c 61 75 73 65 20 74 68 65 6e 20   BY clause then 
2701e 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a  the resulting.**
2701f 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
27020 72 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  re is appropriat
27021 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69  e for initializi
27022 6e 67 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64  ng a virtual ind
27023 65 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65  ex to.** impleme
27024 6e 74 20 74 68 61 74 20 63 6c 61 75 73 65 2e 20  nt that clause. 
27025 20 49 66 20 74 68 65 20 45 78 70 72 4c 69 73 74   If the ExprList
27026 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 73   is the result s
27027 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 0a 2a  et of a SELECT.*
27028 2a 20 74 68 65 6e 20 74 68 65 20 4b 65 79 49 6e  * then the KeyIn
27029 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
2702a 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
2702b 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20 76  initializing a v
2702c 69 72 74 75 61 6c 0a 2a 2a 20 69 6e 64 65 78 20  irtual.** index 
2702d 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 44  to implement a D
2702e 49 53 54 49 4e 43 54 20 74 65 73 74 2e 0a 2a 2a  ISTINCT test..**
2702f 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
27030 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  d the KeyInfo st
27031 72 75 63 74 75 72 65 20 69 73 20 6f 62 74 61 69  ructure is obtai
27032 6e 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 20  n from malloc.  
27033 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66  The calling.** f
27034 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f  unction is respo
27035 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 65 69 6e  nsible for seein
27036 67 20 74 68 61 74 20 74 68 69 73 20 73 74 72 75  g that this stru
27037 63 74 75 72 65 20 69 73 20 65 76 65 6e 74 75 61  cture is eventua
27038 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 20 20 41  lly.** freed.  A
27039 64 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  dd the KeyInfo s
2703a 74 72 75 63 74 75 72 65 20 74 6f 20 74 68 65 20  tructure to the 
2703b 50 34 20 66 69 65 6c 64 20 6f 66 20 61 6e 20 6f  P4 field of an o
2703c 70 63 6f 64 65 20 75 73 69 6e 67 0a 2a 2a 20 50  pcode using.** P
2703d 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
2703e 46 20 69 73 20 74 68 65 20 75 73 75 61 6c 20 77  F is the usual w
2703f 61 79 20 6f 66 20 64 65 61 6c 69 6e 67 20 77 69  ay of dealing wi
27040 74 68 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74  th this..*/.stat
27041 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b 65 79 49  ic KeyInfo *keyI
27042 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  nfoFromExprList(
27043 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
27044 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  xprList *pList){
27045 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
27046 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
27047 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b 65 79 49  nt nExpr;.  KeyI
27048 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 73 74  nfo *pInfo;.  st
27049 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
2704a 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74  em *pItem;.  int
2704b 20 69 3b 0a 0a 20 20 6e 45 78 70 72 20 3d 20 70   i;..  nExpr = p
2704c 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70  List->nExpr;.  p
2704d 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62  Info = sqlite3Db
2704e 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2704f 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 20 2b 20  izeof(*pInfo) + 
27050 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f  nExpr*(sizeof(Co
27051 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20  llSeq*)+1) );.  
27052 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20  if( pInfo ){.   
27053 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64   pInfo->aSortOrd
27054 65 72 20 3d 20 28 75 38 2a 29 26 70 49 6e 66 6f  er = (u8*)&pInfo
27055 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a  ->aColl[nExpr];.
27056 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c      pInfo->nFiel
27057 64 20 3d 20 28 75 31 36 29 6e 45 78 70 72 3b 0a  d = (u16)nExpr;.
27058 20 20 20 20 70 49 6e 66 6f 2d 3e 65 6e 63 20 3d      pInfo->enc =
27059 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70 49   ENC(db);.    pI
2705a 6e 66 6f 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20  nfo->db = db;.  
2705b 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d    for(i=0, pItem
2705c 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 6e 45 78  =pList->a; i<nEx
2705d 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
2705e 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  ){.      CollSeq
2705f 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70   *pColl;.      p
27060 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
27061 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
27062 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b  , pItem->pExpr);
27063 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c  .      if( !pCol
27064 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f  l ){.        pCo
27065 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
27066 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
27067 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69    pInfo->aColl[i
27068 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  ] = pColl;.     
27069 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64   pInfo->aSortOrd
2706a 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 73  er[i] = pItem->s
2706b 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a  ortOrder;.    }.
2706c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 49 6e    }.  return pIn
2706d 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66  fo;.}.../*.** If
2706e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 20   the inner loop 
2706f 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 75 73  was generated us
27070 69 6e 67 20 61 20 6e 6f 6e 2d 6e 75 6c 6c 20 70  ing a non-null p
27071 4f 72 64 65 72 42 79 20 61 72 67 75 6d 65 6e 74  OrderBy argument
27072 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65  ,.** then the re
27073 73 75 6c 74 73 20 77 65 72 65 20 70 6c 61 63 65  sults were place
27074 64 20 69 6e 20 61 20 73 6f 72 74 65 72 2e 20 20  d in a sorter.  
27075 41 66 74 65 72 20 74 68 65 20 6c 6f 6f 70 20 69  After the loop i
27076 73 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20  s terminated.** 
27077 77 65 20 6e 65 65 64 20 74 6f 20 72 75 6e 20 74  we need to run t
27078 68 65 20 73 6f 72 74 65 72 20 61 6e 64 20 6f 75  he sorter and ou
27079 74 70 75 74 20 74 68 65 20 72 65 73 75 6c 74 73  tput the results
2707a 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
2707b 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  .** routine gene
2707c 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 6e  rates the code n
2707d 65 65 64 65 64 20 74 6f 20 64 6f 20 74 68 61 74  eeded to do that
2707e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2707f 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54 61 69   generateSortTai
27080 6c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  l(.  Parse *pPar
27081 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
27082 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
27083 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
27084 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
27085 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56 64  tatement */.  Vd
27086 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20 20  be *v,          
27087 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
27088 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20   into this VDBE 
27089 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
2708a 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ,      /* Number
2708b 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64   of columns of d
2708c 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  ata */.  SelectD
2708d 65 73 74 20 2a 70 44 65 73 74 20 2f 2a 20 57 72  est *pDest /* Wr
2708e 69 74 65 20 74 68 65 20 73 6f 72 74 65 64 20 72  ite the sorted r
2708f 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29  esults here */.)
27090 7b 0a 20 20 69 6e 74 20 61 64 64 72 42 72 65 61  {.  int addrBrea
27091 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  k = sqlite3VdbeM
27092 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 20 20  akeLabel(v);    
27093 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
27094 20 65 78 69 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20   exit loop */.  
27095 69 6e 74 20 61 64 64 72 43 6f 6e 74 69 6e 75 65  int addrContinue
27096 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
27097 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 2f 2a 20  keLabel(v);  /* 
27098 4a 75 6d 70 20 68 65 72 65 20 66 6f 72 20 6e 65  Jump here for ne
27099 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e  xt cycle */.  in
2709a 74 20 61 64 64 72 3b 0a 20 20 69 6e 74 20 69 54  t addr;.  int iT
2709b 61 62 3b 0a 20 20 69 6e 74 20 70 73 65 75 64 6f  ab;.  int pseudo
2709c 54 61 62 20 3d 20 30 3b 0a 20 20 45 78 70 72 4c  Tab = 0;.  ExprL
2709d 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20 3d 20  ist *pOrderBy = 
2709e 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 0a 20 20  p->pOrderBy;..  
2709f 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65 73  int eDest = pDes
270a0 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69 6e 74 20  t->eDest;.  int 
270a1 69 50 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69  iParm = pDest->i
270a2 50 61 72 6d 3b 0a 0a 20 20 69 6e 74 20 72 65 67  Parm;..  int reg
270a3 52 6f 77 3b 0a 20 20 69 6e 74 20 72 65 67 52 6f  Row;.  int regRo
270a4 77 69 64 3b 0a 0a 20 20 69 54 61 62 20 3d 20 70  wid;..  iTab = p
270a5 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
270a6 72 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d  r;.  if( eDest==
270a7 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44  SRT_Output || eD
270a8 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
270a9 6e 65 20 29 7b 0a 20 20 20 20 70 73 65 75 64 6f  ne ){.    pseudo
270aa 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
270ab 61 62 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65  ab++;.    sqlite
270ac 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
270ad 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 70 73  P_OpenPseudo, ps
270ae 65 75 64 6f 54 61 62 2c 20 65 44 65 73 74 3d 3d  eudoTab, eDest==
270af 53 52 54 5f 4f 75 74 70 75 74 2c 20 6e 43 6f 6c  SRT_Output, nCol
270b0 75 6d 6e 29 3b 0a 20 20 7d 0a 20 20 61 64 64 72  umn);.  }.  addr
270b1 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64   = 1 + sqlite3Vd
270b2 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
270b3 6f 72 74 2c 20 69 54 61 62 2c 20 61 64 64 72 42  ort, iTab, addrB
270b4 72 65 61 6b 29 3b 0a 20 20 63 6f 64 65 4f 66 66  reak);.  codeOff
270b5 73 65 74 28 76 2c 20 70 2c 20 61 64 64 72 43 6f  set(v, p, addrCo
270b6 6e 74 69 6e 75 65 29 3b 0a 20 20 72 65 67 52 6f  ntinue);.  regRo
270b7 77 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  w = sqlite3GetTe
270b8 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
270b9 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69   regRowid = sqli
270ba 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
270bb 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
270bc 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
270bd 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 70  _Column, iTab, p
270be 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 2b  OrderBy->nExpr +
270bf 20 31 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 73   1, regRow);.  s
270c0 77 69 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a  witch( eDest ){.
270c1 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61 62      case SRT_Tab
270c2 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54  le:.    case SRT
270c3 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20  _EphemTab: {.   
270c4 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
270c5 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b  st==SRT_Table );
270c6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
270c7 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65   eDest==SRT_Ephe
270c8 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73 71  mTab );.      sq
270c9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
270ca 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
270cb 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69 64 29  iParm, regRowid)
270cc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
270cd 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
270ce 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72  Insert, iParm, r
270cf 65 67 52 6f 77 2c 20 72 65 67 52 6f 77 69 64 29  egRow, regRowid)
270d0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
270d1 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
270d2 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
270d3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
270d4 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
270d5 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
270d6 20 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a     case SRT_Set:
270d7 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
270d8 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20   nColumn==1 );. 
270d9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
270da 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
270db 65 52 65 63 6f 72 64 2c 20 72 65 67 52 6f 77 2c  eRecord, regRow,
270dc 20 31 2c 20 72 65 67 52 6f 77 69 64 2c 20 26 70   1, regRowid, &p
270dd 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
270de 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
270df 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
270e0 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
270e1 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  Row, 1);.      s
270e2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
270e3 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
270e4 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69  , iParm, regRowi
270e5 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  d);.      break;
270e6 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
270e7 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20  SRT_Mem: {.     
270e8 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e   assert( nColumn
270e9 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
270ea 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
270eb 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c  (pParse, regRow,
270ec 20 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20   iParm, 1);.    
270ed 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
270ee 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69  lause will termi
270ef 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f  nate the loop fo
270f0 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72  r us */.      br
270f1 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
270f2 66 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4f  f.    case SRT_O
270f3 75 74 70 75 74 3a 0a 20 20 20 20 63 61 73 65 20  utput:.    case 
270f4 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a 20 7b  SRT_Coroutine: {
270f5 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
270f6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
270f7 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
270f8 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
270f9 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f  e( eDest==SRT_Co
270fa 72 6f 75 74 69 6e 65 20 29 3b 0a 20 20 20 20 20  routine );.     
270fb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
270fc 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
270fd 2c 20 31 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  , 1, regRowid);.
270fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
270ff 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
27100 73 65 72 74 2c 20 70 73 65 75 64 6f 54 61 62 2c  sert, pseudoTab,
27101 20 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77 69   regRow, regRowi
27102 64 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  d);.      for(i=
27103 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  0; i<nColumn; i+
27104 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  +){.        asse
27105 72 74 28 20 72 65 67 52 6f 77 21 3d 70 44 65 73  rt( regRow!=pDes
27106 74 2d 3e 69 4d 65 6d 2b 69 20 29 3b 0a 20 20 20  t->iMem+i );.   
27107 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27108 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
27109 75 6d 6e 2c 20 70 73 65 75 64 6f 54 61 62 2c 20  umn, pseudoTab, 
2710a 69 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2b 69  i, pDest->iMem+i
2710b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2710c 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f   if( eDest==SRT_
2710d 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20  Output ){.      
2710e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2710f 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
27110 52 6f 77 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d  Row, pDest->iMem
27111 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
27112 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
27113 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
27114 67 65 28 70 50 61 72 73 65 2c 20 70 44 65 73 74  ge(pParse, pDest
27115 2d 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29  ->iMem, nColumn)
27116 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
27117 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
27118 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
27119 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 50 61  ield, pDest->iPa
2711a 72 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rm);.      }.   
2711b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2711c 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
2711d 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69       /* Do nothi
2711e 6e 67 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61  ng */.      brea
2711f 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
27120 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
27121 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
27122 52 6f 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  Row);.  sqlite3R
27123 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
27124 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b  arse, regRowid);
27125 0a 0a 20 20 2f 2a 20 4c 49 4d 49 54 20 68 61 73  ..  /* LIMIT has
27126 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65   been implemente
27127 64 20 62 79 20 74 68 65 20 70 75 73 68 4f 6e 74  d by the pushOnt
27128 6f 53 6f 72 74 65 72 28 29 20 72 6f 75 74 69 6e  oSorter() routin
27129 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
2712a 28 20 70 2d 3e 69 4c 69 6d 69 74 3d 3d 30 20 29  ( p->iLimit==0 )
2712b 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74  ;..  /* The bott
2712c 6f 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20  om of the loop. 
2712d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2712e 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2712f 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a   addrContinue);.
27130 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27131 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
27132 69 54 61 62 2c 20 61 64 64 72 29 3b 0a 20 20 73  iTab, addr);.  s
27133 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
27134 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 42 72  eLabel(v, addrBr
27135 65 61 6b 29 3b 0a 20 20 69 66 28 20 65 44 65 73  eak);.  if( eDes
27136 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c  t==SRT_Output ||
27137 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
27138 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c  utine ){.    sql
27139 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2713a 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75  , OP_Close, pseu
2713b 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 7d  doTab, 0);.  }.}
2713c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
2713d 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
2713e 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
2713f 74 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f 6e  the 'declaration
27140 20 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a   type' of the.**
27141 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70   expression pExp
27142 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d 61  r. The string ma
27143 79 20 62 65 20 74 72 65 61 74 65 64 20 61 73 20  y be treated as 
27144 73 74 61 74 69 63 20 62 79 20 74 68 65 20 63 61  static by the ca
27145 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ller..**.** The 
27146 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
27147 20 69 73 20 74 68 65 20 65 78 61 63 74 20 64 61   is the exact da
27148 74 61 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f  tatype definitio
27149 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d  n extracted from
2714a 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
2714b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
2714c 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 65  atement if the e
2714d 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63  xpression is a c
2714e 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64 65  olumn. The.** de
2714f 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 66  claration type f
27150 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65 6c 64  or a ROWID field
27151 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45 78 61   is INTEGER. Exa
27152 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 78 70  ctly when an exp
27153 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f  ression.** is co
27154 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
27155 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 78  n can be complex
27156 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e 63 65   in the presence
27157 20 6f 66 20 73 75 62 71 75 65 72 69 65 73 2e 20   of subqueries. 
27158 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65  The.** result-se
27159 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  t expression in 
2715a 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  all of the follo
2715b 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74  wing SELECT stat
2715c 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63 6f  ements is .** co
2715d 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
2715e 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  n by this functi
2715f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  on..**.**   SELE
27160 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b  CT col FROM tbl;
27161 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45  .**   SELECT (SE
27162 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62  LECT col FROM tb
27163 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28  l;.**   SELECT (
27164 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20  SELECT col FROM 
27165 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43  tbl);.**   SELEC
27166 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45 4c 45  T abc FROM (SELE
27167 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20 46 52  CT col AS abc FR
27168 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20  OM tbl);.** .** 
27169 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  The declaration 
2716a 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78 70  type for any exp
2716b 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74 68  ression other th
2716c 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e  an a column is N
2716d 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ULL..*/.static c
2716e 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d  onst char *colum
2716f 6e 54 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f 6e  nType(.  NameCon
27170 74 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78  text *pNC, .  Ex
27171 70 72 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f 6e  pr *pExpr,.  con
27172 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67  st char **pzOrig
27173 69 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  inDb,.  const ch
27174 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61 62  ar **pzOriginTab
27175 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
27176 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a  *pzOriginCol.){.
27177 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
27178 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  ype = 0;.  char 
27179 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44 62  const *zOriginDb
2717a 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 0;.  char con
2717b 73 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20 3d  st *zOriginTab =
2717c 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   0;.  char const
2717d 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 30   *zOriginCol = 0
2717e 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66 28  ;.  int j;.  if(
2717f 20 70 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 43   pExpr==0 || pNC
27180 2d 3e 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20  ->pSrcList==0 ) 
27181 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77 69  return 0;..  swi
27182 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
27183 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  {.    case TK_AG
27184 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61  G_COLUMN:.    ca
27185 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a  se TK_COLUMN: {.
27186 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
27187 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c  ression is a col
27188 75 6d 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65 20  umn. Locate the 
27189 74 61 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d 6e  table the column
2718a 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20   is being.      
2718b 2a 2a 20 65 78 74 72 61 63 74 65 64 20 66 72 6f  ** extracted fro
2718c 6d 20 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78 74  m in NameContext
2718d 2e 70 53 72 63 4c 69 73 74 2e 20 54 68 69 73 20  .pSrcList. This 
2718e 74 61 62 6c 65 20 6d 61 79 20 62 65 20 72 65 61  table may be rea
2718f 6c 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62  l.      ** datab
27190 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 20 73  ase table or a s
27191 75 62 71 75 65 72 79 2e 0a 20 20 20 20 20 20 2a  ubquery..      *
27192 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  /.      Table *p
27193 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Tab = 0;        
27194 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 73 74 72      /* Table str
27195 75 63 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73  ucture column is
27196 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
27197 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20  */.      Select 
27198 2a 70 53 20 3d 20 30 3b 20 20 20 20 20 20 20 20  *pS = 0;        
27199 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74       /* Select t
2719a 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74  he column is ext
2719b 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20  racted from */. 
2719c 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
2719d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20  pExpr->iColumn; 
2719e 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
2719f 75 6d 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20  umn in pTab */. 
271a0 20 20 20 20 20 77 68 69 6c 65 28 20 70 4e 43 20       while( pNC 
271a1 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20  && !pTab ){.    
271a2 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61      SrcList *pTa
271a3 62 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  bList = pNC->pSr
271a4 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 66  cList;.        f
271a5 6f 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69 73  or(j=0;j<pTabLis
271a6 74 2d 3e 6e 53 72 63 20 26 26 20 70 54 61 62 4c  t->nSrc && pTabL
271a7 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f  ist->a[j].iCurso
271a8 72 21 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  r!=pExpr->iTable
271a9 3b 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20 69  ;j++);.        i
271aa 66 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  f( j<pTabList->n
271ab 53 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Src ){.         
271ac 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74   pTab = pTabList
271ad 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20  ->a[j].pTab;.   
271ae 20 20 20 20 20 20 20 70 53 20 3d 20 70 54 61 62         pS = pTab
271af 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65  List->a[j].pSele
271b0 63 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ct;.        }els
271b1 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 43  e{.          pNC
271b2 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20   = pNC->pNext;. 
271b3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
271b4 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ..      if( pTab
271b5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
271b6 2a 20 46 49 58 20 4d 45 3a 0a 20 20 20 20 20 20  * FIX ME:.      
271b7 20 20 2a 2a 20 54 68 69 73 20 63 61 6e 20 6f 63    ** This can oc
271b8 63 75 72 73 20 69 66 20 79 6f 75 20 68 61 76 65  curs if you have
271b9 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20   something like 
271ba 22 53 45 4c 45 43 54 20 6e 65 77 2e 78 3b 22 20  "SELECT new.x;" 
271bb 69 6e 73 69 64 65 0a 20 20 20 20 20 20 20 20 2a  inside.        *
271bc 2a 20 61 20 74 72 69 67 67 65 72 2e 20 20 49 6e  * a trigger.  In
271bd 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66   other words, if
271be 20 79 6f 75 20 72 65 66 65 72 65 6e 63 65 20 74   you reference t
271bf 68 65 20 73 70 65 63 69 61 6c 20 22 6e 65 77 22  he special "new"
271c0 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  .        ** tabl
271c1 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  e in the result 
271c2 73 65 74 20 6f 66 20 61 20 73 65 6c 65 63 74 2e  set of a select.
271c3 20 20 57 65 20 64 6f 20 6e 6f 74 20 68 61 76 65    We do not have
271c4 20 61 20 67 6f 6f 64 20 77 61 79 0a 20 20 20 20   a good way.    
271c5 20 20 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20 74      ** to find t
271c6 68 65 20 61 63 74 75 61 6c 20 74 61 62 6c 65 20  he actual table 
271c7 74 79 70 65 2c 20 73 6f 20 63 61 6c 6c 20 69 74  type, so call it
271c8 20 22 54 45 58 54 22 2e 20 20 54 68 69 73 20 69   "TEXT".  This i
271c9 73 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 20  s really.       
271ca 20 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 66   ** something of
271cb 20 61 20 62 75 67 2c 20 62 75 74 20 49 20 64 6f   a bug, but I do
271cc 20 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 74 6f   not know how to
271cd 20 66 69 78 20 69 74 2e 0a 20 20 20 20 20 20 20   fix it..       
271ce 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54   **.        ** T
271cf 68 69 73 20 63 6f 64 65 20 64 6f 65 73 20 6e 6f  his code does no
271d0 74 20 70 72 6f 64 75 63 65 20 74 68 65 20 63 6f  t produce the co
271d1 72 72 65 63 74 20 61 6e 73 77 65 72 20 2d 20 69  rrect answer - i
271d2 74 20 6a 75 73 74 20 70 72 65 76 65 6e 74 73 0a  t just prevents.
271d3 20 20 20 20 20 20 20 20 2a 2a 20 61 20 73 65 67          ** a seg
271d4 66 61 75 6c 74 2e 20 20 53 65 65 20 74 69 63 6b  fault.  See tick
271d5 65 74 20 23 31 32 32 39 2e 0a 20 20 20 20 20 20  et #1229..      
271d6 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 54 79    */.        zTy
271d7 70 65 20 3d 20 22 54 45 58 54 22 3b 0a 20 20 20  pe = "TEXT";.   
271d8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
271d9 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72    }..      asser
271da 74 28 20 70 54 61 62 20 29 3b 0a 20 20 20 20 20  t( pTab );.     
271db 20 69 66 28 20 70 53 20 29 7b 0a 20 20 20 20 20   if( pS ){.     
271dc 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65     /* The "table
271dd 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20  " is actually a 
271de 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20  sub-select or a 
271df 76 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d  view in the FROM
271e0 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20   clause.        
271e1 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ** of the SELECT
271e2 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74 75   statement. Retu
271e3 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74 69  rn the declarati
271e4 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69 67  on type and orig
271e5 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61  in.        ** da
271e6 74 61 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c  ta for the resul
271e7 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  t-set column of 
271e8 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a  the sub-select..
271e9 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
271ea 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 69 43     if( ALWAYS(iC
271eb 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 53  ol>=0 && iCol<pS
271ec 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  ->pEList->nExpr)
271ed 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
271ee 20 49 66 20 69 43 6f 6c 20 69 73 20 6c 65 73 73   If iCol is less
271ef 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e   than zero, then
271f0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
271f1 72 65 71 75 65 73 74 73 20 74 68 65 0a 20 20 20  requests the.   
271f2 20 20 20 20 20 20 20 2a 2a 20 72 6f 77 69 64 20         ** rowid 
271f3 6f 66 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63  of the sub-selec
271f4 74 20 6f 72 20 76 69 65 77 2e 20 54 68 69 73 20  t or view. This 
271f5 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6c 65  expression is le
271f6 67 61 6c 20 28 73 65 65 20 0a 20 20 20 20 20 20  gal (see .      
271f7 20 20 20 20 2a 2a 20 74 65 73 74 20 63 61 73 65      ** test case
271f8 20 6d 69 73 63 32 2e 32 2e 32 29 20 2d 20 69 74   misc2.2.2) - it
271f9 20 61 6c 77 61 79 73 20 65 76 61 6c 75 61 74 65   always evaluate
271fa 73 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20  s to NULL..     
271fb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
271fc 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
271fd 43 3b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  C;.          Exp
271fe 72 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73  r *p = pS->pELis
271ff 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72  t->a[iCol].pExpr
27200 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e  ;.          sNC.
27201 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70  pSrcList = pS->p
27202 53 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73  Src;.          s
27203 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  NC.pNext = 0;.  
27204 20 20 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72          sNC.pPar
27205 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
27206 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70  ;.          zTyp
27207 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
27208 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
27209 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
2720a 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0a   &zOriginCol); .
2720b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2720c 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53  }else if( ALWAYS
2720d 28 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 20  (pTab->pSchema) 
2720e 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20  ){.        /* A 
2720f 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  real table */.  
27210 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
27211 53 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  S );.        if(
27212 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d   iCol<0 ) iCol =
27213 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20   pTab->iPKey;.  
27214 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 43        assert( iC
27215 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e  ol==-1 || (iCol>
27216 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d  =0 && iCol<pTab-
27217 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20  >nCol) );.      
27218 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a    if( iCol<0 ){.
27219 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 20            zType 
2721a 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20  = "INTEGER";.   
2721b 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f         zOriginCo
2721c 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20  l = "rowid";.   
2721d 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2721e 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54        zType = pT
2721f 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
27220 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Type;.          
27221 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54 61  zOriginCol = pTa
27222 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
27223 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ame;.        }. 
27224 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54 61         zOriginTa
27225 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  b = pTab->zName;
27226 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 43  .        if( pNC
27227 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20  ->pParse ){.    
27228 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20        int iDb = 
27229 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2722a 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73 65  ndex(pNC->pParse
2722b 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
2722c 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ema);.          
2722d 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43 2d  zOriginDb = pNC-
2722e 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  >pParse->db->aDb
2722f 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
27230 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
27231 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27232 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
27233 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
27234 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
27235 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  T: {.      /* Th
27236 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
27237 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52 65  a sub-select. Re
27238 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61  turn the declara
27239 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20 20  tion type and.  
2723a 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69 6e      ** origin in
2723b 66 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67 6c  fo for the singl
2723c 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  e column in the 
2723d 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
2723e 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a  e SELECT.      *
2723f 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  * statement..   
27240 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d 65     */.      Name
27241 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
27242 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d 20     Select *pS = 
27243 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
27244 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 20  ;.      Expr *p 
27245 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  = pS->pEList->a[
27246 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  0].pExpr;.      
27247 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50  assert( ExprHasP
27248 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
27249 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2724a 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69        sNC.pSrcLi
2724b 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20  st = pS->pSrc;. 
2724c 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d       sNC.pNext =
2724d 20 70 4e 43 3b 0a 20 20 20 20 20 20 73 4e 43 2e   pNC;.      sNC.
2724e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
2724f 61 72 73 65 3b 0a 20 20 20 20 20 20 7a 54 79 70  arse;.      zTyp
27250 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
27251 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69 6e  sNC, p, &zOrigin
27252 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62 2c  Db, &zOriginTab,
27253 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20 0a   &zOriginCol); .
27254 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27255 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20   }.#endif.  }.  
27256 0a 20 20 69 66 28 20 70 7a 4f 72 69 67 69 6e 44  .  if( pzOriginD
27257 62 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  b ){.    assert(
27258 20 70 7a 4f 72 69 67 69 6e 54 61 62 20 26 26 20   pzOriginTab && 
27259 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 29 3b 0a 20  pzOriginCol );. 
2725a 20 20 20 2a 70 7a 4f 72 69 67 69 6e 44 62 20 3d     *pzOriginDb =
2725b 20 7a 4f 72 69 67 69 6e 44 62 3b 0a 20 20 20 20   zOriginDb;.    
2725c 2a 70 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 7a  *pzOriginTab = z
2725d 4f 72 69 67 69 6e 54 61 62 3b 0a 20 20 20 20 2a  OriginTab;.    *
2725e 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 7a 4f  pzOriginCol = zO
2725f 72 69 67 69 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20  riginCol;.  }.  
27260 72 65 74 75 72 6e 20 7a 54 79 70 65 3b 0a 7d 0a  return zType;.}.
27261 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
27262 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 74  code that will t
27263 65 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68 65  ell the VDBE the
27264 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
27265 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  es of columns.**
27266 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
27267 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  et..*/.static vo
27268 69 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d  id generateColum
27269 6e 54 79 70 65 73 28 0a 20 20 50 61 72 73 65 20  nTypes(.  Parse 
2726a 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
2726b 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
2726c 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
2726d 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74  abList,  /* List
2726e 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20   of tables */.  
2726f 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
27270 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
27271 6e 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20  ns defining the 
27272 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29 7b  result set */.){
27273 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
27274 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 20 20  OMIT_DECLTYPE.  
27275 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
27276 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
27277 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
27278 73 4e 43 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c  sNC;.  sNC.pSrcL
27279 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a  ist = pTabList;.
2727a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
2727b 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30  Parse;.  for(i=0
2727c 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; i<pEList->nExp
2727d 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  r; i++){.    Exp
2727e 72 20 2a 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61  r *p = pEList->a
2727f 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 63  [i].pExpr;.    c
27280 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
27281 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
27282 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
27283 54 41 44 41 54 41 0a 20 20 20 20 63 6f 6e 73 74  TADATA.    const
27284 20 63 68 61 72 20 2a 7a 4f 72 69 67 44 62 20 3d   char *zOrigDb =
27285 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   0;.    const ch
27286 61 72 20 2a 7a 4f 72 69 67 54 61 62 20 3d 20 30  ar *zOrigTab = 0
27287 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
27288 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d 20 30 3b 0a   *zOrigCol = 0;.
27289 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
2728a 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
2728b 26 7a 4f 72 69 67 44 62 2c 20 26 7a 4f 72 69 67  &zOrigDb, &zOrig
2728c 54 61 62 2c 20 26 7a 4f 72 69 67 43 6f 6c 29 3b  Tab, &zOrigCol);
2728d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 76 64 62  ..    /* The vdb
2728e 65 20 6d 75 73 74 20 6d 61 6b 65 20 69 74 73 20  e must make its 
2728f 6f 77 6e 20 63 6f 70 79 20 6f 66 20 74 68 65 20  own copy of the 
27290 63 6f 6c 75 6d 6e 2d 74 79 70 65 20 61 6e 64 20  column-type and 
27291 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 63 6f  other .    ** co
27292 6c 75 6d 6e 20 73 70 65 63 69 66 69 63 20 73 74  lumn specific st
27293 72 69 6e 67 73 2c 20 69 6e 20 63 61 73 65 20 74  rings, in case t
27294 68 65 20 73 63 68 65 6d 61 20 69 73 20 72 65 73  he schema is res
27295 65 74 20 62 65 66 6f 72 65 20 74 68 69 73 0a 20  et before this. 
27296 20 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61     ** virtual ma
27297 63 68 69 6e 65 20 69 73 20 64 65 6c 65 74 65 64  chine is deleted
27298 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
27299 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2729a 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
2729b 5f 44 41 54 41 42 41 53 45 2c 20 7a 4f 72 69 67  _DATABASE, zOrig
2729c 44 62 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  Db, SQLITE_TRANS
2729d 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  IENT);.    sqlit
2729e 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2729f 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 54  (v, i, COLNAME_T
272a0 41 42 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c 20  ABLE, zOrigTab, 
272a1 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
272a2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
272a3 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
272a4 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  i, COLNAME_COLUM
272a5 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c 20 53 51 4c  N, zOrigCol, SQL
272a6 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
272a7 23 65 6c 73 65 0a 20 20 20 20 7a 54 79 70 65 20  #else.    zType 
272a8 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e  = columnType(&sN
272a9 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  C, p, 0, 0, 0);.
272aa 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
272ab 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
272ac 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44  (v, i, COLNAME_D
272ad 45 43 4c 54 59 50 45 2c 20 7a 54 79 70 65 2c 20  ECLTYPE, zType, 
272ae 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
272af 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
272b0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
272b1 4c 54 59 50 45 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a  LTYPE */.}../*.*
272b2 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
272b3 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74  that will tell t
272b4 68 65 20 56 44 42 45 20 74 68 65 20 6e 61 6d 65  he VDBE the name
272b5 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  s of columns.** 
272b6 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
272b7 74 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  t.  This informa
272b8 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
272b9 70 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61  provide the.** a
272ba 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65 73 20 69 6e  zCol[] values in
272bb 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a   the callback..*
272bc 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
272bd 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65  nerateColumnName
272be 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  s(.  Parse *pPar
272bf 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
272c0 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
272c1 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
272c2 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  t,  /* List of t
272c3 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c  ables */.  ExprL
272c4 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f  ist *pEList    /
272c5 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65  * Expressions de
272c6 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c  fining the resul
272c7 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  t set */.){.  Vd
272c8 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
272c9 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 2c 20  pVdbe;.  int i, 
272ca 6a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  j;.  sqlite3 *db
272cb 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
272cc 20 69 6e 74 20 66 75 6c 6c 4e 61 6d 65 73 2c 20   int fullNames, 
272cd 73 68 6f 72 74 4e 61 6d 65 73 3b 0a 0a 23 69 66  shortNames;..#if
272ce 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
272cf 5f 45 58 50 4c 41 49 4e 0a 20 20 2f 2a 20 49 66  _EXPLAIN.  /* If
272d0 20 74 68 69 73 20 69 73 20 61 6e 20 45 58 50 4c   this is an EXPL
272d1 41 49 4e 2c 20 73 6b 69 70 20 74 68 69 73 20 73  AIN, skip this s
272d2 74 65 70 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  tep */.  if( pPa
272d3 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a  rse->explain ){.
272d4 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
272d5 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74  #endif..  assert
272d6 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ( v!=0 );.  if( 
272d7 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73  pParse->colNames
272d8 53 65 74 20 7c 7c 20 4e 45 56 45 52 28 76 3d 3d  Set || NEVER(v==
272d9 30 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  0) || db->malloc
272da 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b  Failed ) return;
272db 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61  .  pParse->colNa
272dc 6d 65 73 53 65 74 20 3d 20 31 3b 0a 20 20 66 75  mesSet = 1;.  fu
272dd 6c 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66  llNames = (db->f
272de 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75  lags & SQLITE_Fu
272df 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a  llColNames)!=0;.
272e0 20 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28    shortNames = (
272e1 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
272e2 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
272e3 29 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56  )!=0;.  sqlite3V
272e4 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
272e5 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b   pEList->nExpr);
272e6 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45  .  for(i=0; i<pE
272e7 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
272e8 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 3b 0a  ){.    Expr *p;.
272e9 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e      p = pEList->
272ea 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
272eb 69 66 28 20 70 3d 3d 30 20 29 20 63 6f 6e 74 69  if( p==0 ) conti
272ec 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 45 4c  nue;.    if( pEL
272ed 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20  ist->a[i].zName 
272ee 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
272ef 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61  Name = pEList->a
272f0 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [i].zName;.     
272f1 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
272f2 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
272f3 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65  NAME_NAME, zName
272f4 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
272f5 4e 54 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  NT);.    }else i
272f6 66 28 20 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  f( (p->op==TK_CO
272f7 4c 55 4d 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54  LUMN || p->op==T
272f8 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 29 20 26 26  K_AGG_COLUMN) &&
272f9 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 20 20   pTabList ){.   
272fa 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
272fb 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c        char *zCol
272fc 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  ;.      int iCol
272fd 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20   = p->iColumn;. 
272fe 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 41 4c       for(j=0; AL
272ff 57 41 59 53 28 6a 3c 70 54 61 62 4c 69 73 74 2d  WAYS(j<pTabList-
27300 3e 6e 53 72 63 29 3b 20 6a 2b 2b 29 7b 0a 20 20  >nSrc); j++){.  
27301 20 20 20 20 20 20 69 66 28 20 70 54 61 62 4c 69        if( pTabLi
27302 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72  st->a[j].iCursor
27303 3d 3d 70 2d 3e 69 54 61 62 6c 65 20 29 20 62 72  ==p->iTable ) br
27304 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
27305 20 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54 61     assert( j<pTa
27306 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20  bList->nSrc );. 
27307 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62       pTab = pTab
27308 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b  List->a[j].pTab;
27309 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
2730a 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d  0 ) iCol = pTab-
2730b 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61 73  >iPKey;.      as
2730c 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c  sert( iCol==-1 |
2730d 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  | (iCol>=0 && iC
2730e 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29  ol<pTab->nCol) )
2730f 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
27310 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43  <0 ){.        zC
27311 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20  ol = "rowid";.  
27312 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27313 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     zCol = pTab->
27314 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
27315 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
27316 69 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20  if( !shortNames 
27317 26 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b  && !fullNames ){
27318 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27319 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
2731a 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
2731b 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
2731c 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
2731d 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73  (db, (char*)p->s
2731e 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e  pan.z, p->span.n
2731f 29 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  ), SQLITE_DYNAMI
27320 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  C);.      }else 
27321 69 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20 29 7b  if( fullNames ){
27322 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
27323 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Name = 0;.      
27324 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
27325 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
27326 2e 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  .%s", pTab->zNam
27327 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20  e, zCol);.      
27328 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
27329 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
2732a 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d  LNAME_NAME, zNam
2732b 65 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  e, SQLITE_DYNAMI
2732c 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  C);.      }else{
2732d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2732e 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
2732f 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
27330 45 2c 20 7a 43 6f 6c 2c 20 53 51 4c 49 54 45 5f  E, zCol, SQLITE_
27331 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
27332 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
27333 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27334 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
27335 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a   COLNAME_NAME, .
27336 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27337 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
27338 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a 2c  char*)p->span.z,
27339 20 70 2d 3e 73 70 61 6e 2e 6e 29 2c 20 53 51 4c   p->span.n), SQL
2733a 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ITE_DYNAMIC);.  
2733b 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65 72 61    }.  }.  genera
2733c 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 70 50  teColumnTypes(pP
2733d 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
2733e 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69 66 6e  pEList);.}..#ifn
2733f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27340 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
27341 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68  /*.** Name of th
27342 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65  e connection ope
27343 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f 72 20  rator, used for 
27344 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 0a  error messages..
27345 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
27346 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70 4e 61  char *selectOpNa
27347 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20 63 68  me(int id){.  ch
27348 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63 68 28  ar *z;.  switch(
27349 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20   id ){.    case 
2734a 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20 7a 20  TK_ALL:       z 
2734b 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20 20  = "UNION ALL";  
2734c 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
2734d 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20 7a   TK_INTERSECT: z
2734e 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22 3b 20   = "INTERSECT"; 
2734f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
27350 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20 20 20  e TK_EXCEPT:    
27351 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20 20 20  z = "EXCEPT";   
27352 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
27353 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20  fault:          
27354 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20 20 20   z = "UNION";   
27355 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
27356 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e   return z;.}.#en
27357 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
27358 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
27359 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 69 76  CT */../*.** Giv
2735a 65 6e 20 61 20 61 6e 20 65 78 70 72 65 73 73 69  en a an expressi
2735b 6f 6e 20 6c 69 73 74 20 28 77 68 69 63 68 20 69  on list (which i
2735c 73 20 72 65 61 6c 6c 79 20 74 68 65 20 6c 69 73  s really the lis
2735d 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
2735e 0a 2a 2a 20 74 68 61 74 20 66 6f 72 6d 20 74 68  .** that form th
2735f 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
27360 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
27361 6e 74 29 20 63 6f 6d 70 75 74 65 20 61 70 70 72  nt) compute appr
27362 6f 70 72 69 61 74 65 0a 2a 2a 20 63 6f 6c 75 6d  opriate.** colum
27363 6e 20 6e 61 6d 65 73 20 66 6f 72 20 61 20 74 61  n names for a ta
27364 62 6c 65 20 74 68 61 74 20 77 6f 75 6c 64 20 68  ble that would h
27365 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73 69  old the expressi
27366 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41  on list..**.** A
27367 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ll column names 
27368 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 2e 0a  will be unique..
27369 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 63  **.** Only the c
2736a 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 72 65 20  olumn names are 
2736b 63 6f 6d 70 75 74 65 64 2e 20 20 43 6f 6c 75 6d  computed.  Colum
2736c 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c 75 6d 6e 2e  n.zType, Column.
2736d 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e 64 20 6f 74  zColl,.** and ot
2736e 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 43 6f  her fields of Co
2736f 6c 75 6d 6e 20 61 72 65 20 7a 65 72 6f 65 64 2e  lumn are zeroed.
27370 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
27371 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
27372 73 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79  ss.  If a memory
27373 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
27374 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73 74 6f  r occurs,.** sto
27375 72 65 20 4e 55 4c 4c 20 69 6e 20 2a 70 61 43 6f  re NULL in *paCo
27376 6c 20 61 6e 64 20 30 20 69 6e 20 2a 70 6e 43 6f  l and 0 in *pnCo
27377 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  l and return SQL
27378 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74  ITE_NOMEM..*/.st
27379 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 43  atic int selectC
2737a 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69  olumnsFromExprLi
2737b 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
2737c 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
2737d 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2737e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2737f 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  pEList,       /*
27380 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f 6d 20   Expr list from 
27381 77 68 69 63 68 20 74 6f 20 64 65 72 69 76 65 20  which to derive 
27382 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a  column names */.
27383 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 2c 20 20 20    int *pnCol,   
27384 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
27385 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  te the number of
27386 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65 20 2a 2f   columns here */
27387 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a 70 61 43 6f  .  Column **paCo
27388 6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  l          /* Wr
27389 69 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c 75  ite the new colu
2738a 6d 6e 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  mn list here */.
2738b 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2738c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
2738d 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2738e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  nection */.  int
2738f 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20   i, j;          
27390 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
27391 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
27392 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20  nt cnt;         
27393 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
27394 64 65 78 20 61 64 64 65 64 20 74 6f 20 6d 61 6b  dex added to mak
27395 65 20 74 68 65 20 6e 61 6d 65 20 75 6e 69 71 75  e the name uniqu
27396 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61  e */.  Column *a
27397 43 6f 6c 2c 20 2a 70 43 6f 6c 3b 20 20 20 20 20  Col, *pCol;     
27398 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
27399 67 20 6f 76 65 72 20 72 65 73 75 6c 74 20 63 6f  g over result co
2739a 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  lumns */.  int n
2739b 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
2739c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2739d 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
2739e 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
2739f 0a 20 20 45 78 70 72 20 2a 70 3b 20 20 20 20 20  .  Expr *p;     
273a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
273a1 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 66 6f 72  * Expression for
273a2 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c 74   a single result
273a3 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61   column */.  cha
273a4 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
273a5 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
273a6 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74  mn name */.  int
273a7 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20   nName;         
273a8 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
273a9 20 6f 66 20 6e 61 6d 65 20 69 6e 20 7a 4e 61 6d   of name in zNam
273aa 65 5b 5d 20 2a 2f 0a 0a 20 20 2a 70 6e 43 6f 6c  e[] */..  *pnCol
273ab 20 3d 20 6e 43 6f 6c 20 3d 20 70 45 4c 69 73 74   = nCol = pEList
273ac 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 43 6f 6c 20  ->nExpr;.  aCol 
273ad 3d 20 2a 70 61 43 6f 6c 20 3d 20 73 71 6c 69 74  = *paCol = sqlit
273ae 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
273af 62 2c 20 73 69 7a 65 6f 66 28 61 43 6f 6c 5b 30  b, sizeof(aCol[0
273b0 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20 69 66 28 20  ])*nCol);.  if( 
273b1 61 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  aCol==0 ) return
273b2 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
273b3 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 61   for(i=0, pCol=a
273b4 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  Col; i<nCol; i++
273b5 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 2f  , pCol++){.    /
273b6 2a 20 47 65 74 20 61 6e 20 61 70 70 72 6f 70 72  * Get an appropr
273b7 69 61 74 65 20 6e 61 6d 65 20 66 6f 72 20 74 68  iate name for th
273b8 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a  e column.    */.
273b9 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e      p = pEList->
273ba 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
273bb 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68  assert( p->pRigh
273bc 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 52 69 67 68  t==0 || p->pRigh
273bd 74 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 7c 7c  t->token.z==0 ||
273be 20 70 2d 3e 70 52 69 67 68 74 2d 3e 74 6f 6b 65   p->pRight->toke
273bf 6e 2e 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20  n.z[0]!=0 );.   
273c0 20 69 66 28 20 28 7a 4e 61 6d 65 20 3d 20 70 45   if( (zName = pE
273c1 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
273c2 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  )!=0 ){.      /*
273c3 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   If the column c
273c4 6f 6e 74 61 69 6e 73 20 61 6e 20 22 41 53 20 3c  ontains an "AS <
273c5 6e 61 6d 65 3e 22 20 70 68 72 61 73 65 2c 20 75  name>" phrase, u
273c6 73 65 20 3c 6e 61 6d 65 3e 20 61 73 20 74 68 65  se <name> as the
273c7 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a   name */.      z
273c8 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Name = sqlite3Db
273c9 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65  StrDup(db, zName
273ca 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
273cb 20 20 20 20 45 78 70 72 20 2a 70 43 6f 6c 45 78      Expr *pColEx
273cc 70 72 20 3d 20 70 3b 20 20 2f 2a 20 54 68 65 20  pr = p;  /* The 
273cd 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
273ce 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  is the result co
273cf 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  lumn name */.   
273d0 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20     Table *pTab; 
273d1 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
273d2 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
273d3 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   this expression
273d4 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
273d5 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54   pColExpr->op==T
273d6 4b 5f 44 4f 54 20 29 20 70 43 6f 6c 45 78 70 72  K_DOT ) pColExpr
273d7 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 70 52 69   = pColExpr->pRi
273d8 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ght;.      if( p
273d9 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ColExpr->op==TK_
273da 43 4f 4c 55 4d 4e 20 26 26 20 28 70 54 61 62 20  COLUMN && (pTab 
273db 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62  = pColExpr->pTab
273dc 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
273dd 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d 6e 73 20 75  /* For columns u
273de 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  se the column na
273df 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  me name */.     
273e0 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 43     int iCol = pC
273e1 6f 6c 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b  olExpr->iColumn;
273e2 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
273e3 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61  l<0 ) iCol = pTa
273e4 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20  b->iPKey;.      
273e5 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
273e6 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
273e7 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
273e8 20 20 20 20 69 43 6f 6c 3e 3d 30 20 3f 20 70 54      iCol>=0 ? pT
273e9 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
273ea 4e 61 6d 65 20 3a 20 22 72 6f 77 69 64 22 29 3b  Name : "rowid");
273eb 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
273ec 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65        /* Use the
273ed 20 6f 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f   original text o
273ee 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 65 78 70  f the column exp
273ef 72 65 73 73 69 6f 6e 20 61 73 20 69 74 73 20 6e  ression as its n
273f0 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 54  ame */.        T
273f1 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 28  oken *pToken = (
273f2 70 43 6f 6c 45 78 70 72 2d 3e 73 70 61 6e 2e 7a  pColExpr->span.z
273f3 3f 26 70 43 6f 6c 45 78 70 72 2d 3e 73 70 61 6e  ?&pColExpr->span
273f4 3a 26 70 43 6f 6c 45 78 70 72 2d 3e 74 6f 6b 65  :&pColExpr->toke
273f5 6e 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d  n);.        zNam
273f6 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
273f7 74 66 28 64 62 2c 20 22 25 54 22 2c 20 70 54 6f  tf(db, "%T", pTo
273f8 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ken);.      }.  
273f9 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
273fa 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
273fb 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
273fc 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  ree(db, zName);.
273fd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
273fe 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65   }.    sqlite3De
273ff 71 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 0a 20  quote(zName);.. 
27400 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
27401 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  the column name 
27402 69 73 20 75 6e 69 71 75 65 2e 20 20 49 66 20 74  is unique.  If t
27403 68 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 75  he name is not u
27404 6e 69 71 75 65 2c 0a 20 20 20 20 2a 2a 20 61 70  nique,.    ** ap
27405 70 65 6e 64 20 61 20 69 6e 74 65 67 65 72 20 74  pend a integer t
27406 6f 20 74 68 65 20 6e 61 6d 65 20 73 6f 20 74 68  o the name so th
27407 61 74 20 69 74 20 62 65 63 6f 6d 65 73 20 75 6e  at it becomes un
27408 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ique..    */.   
27409 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2740a 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
2740b 0a 20 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30  .    for(j=cnt=0
2740c 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ; j<i; j++){.   
2740d 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
2740e 72 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e  rICmp(aCol[j].zN
2740f 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29  ame, zName)==0 )
27410 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
27411 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 20 20 20  zNewName;.      
27412 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d    zName[nName] =
27413 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65 77   0;.        zNew
27414 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
27415 72 69 6e 74 66 28 64 62 2c 20 22 25 73 3a 25 64  rintf(db, "%s:%d
27416 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29  ", zName, ++cnt)
27417 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
27418 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
27419 65 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d  e);.        zNam
2741a 65 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20  e = zNewName;.  
2741b 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20        j = -1;.  
2741c 20 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d        if( zName=
2741d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
2741e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43    }.    }.    pC
2741f 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d  ol->zName = zNam
27420 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  e;.  }.  if( db-
27421 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
27422 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
27423 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73  i; j++){.      s
27424 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
27425 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3b   aCol[j].zName);
27426 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
27427 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 43 6f  e3DbFree(db, aCo
27428 6c 29 3b 0a 20 20 20 20 2a 70 61 43 6f 6c 20 3d  l);.    *paCol =
27429 20 30 3b 0a 20 20 20 20 2a 70 6e 43 6f 6c 20 3d   0;.    *pnCol =
2742a 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0;.    return S
2742b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
2742c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2742d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  _OK;.}../*.** Ad
2742e 64 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61  d type and colla
2742f 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tion information
27430 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 6c 69 73   to a column lis
27431 74 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 61 20  t based on.** a 
27432 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
27433 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 6c  ..** .** The col
27434 75 6d 6e 20 6c 69 73 74 20 70 72 65 73 75 6d 61  umn list presuma
27435 62 6c 79 20 63 61 6d 65 20 66 72 6f 6d 20 73 65  bly came from se
27436 6c 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 46  lectColumnNamesF
27437 72 6f 6d 45 78 70 72 4c 69 73 74 28 29 2e 0a 2a  romExprList()..*
27438 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73  * The column lis
27439 74 20 68 61 73 20 6f 6e 6c 79 20 6e 61 6d 65 73  t has only names
2743a 2c 20 6e 6f 74 20 74 79 70 65 73 20 6f 72 20 63  , not types or c
2743b 6f 6c 6c 61 74 69 6f 6e 73 2e 20 20 54 68 69 73  ollations.  This
2743c 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 6f 65 73  .** routine goes
2743d 20 74 68 72 6f 75 67 68 20 61 6e 64 20 61 64 64   through and add
2743e 73 20 74 68 65 20 74 79 70 65 73 20 61 6e 64 20  s the types and 
2743f 63 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  collations..**.*
27440 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
27441 65 71 75 69 72 65 73 20 74 68 61 74 20 61 6c 6c  equires that all
27442 20 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20   identifiers in 
27443 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74  the SELECT.** st
27444 61 74 65 6d 65 6e 74 20 62 65 20 72 65 73 6f 6c  atement be resol
27445 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ved..*/.static v
27446 6f 69 64 20 73 65 6c 65 63 74 41 64 64 43 6f 6c  oid selectAddCol
27447 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74  umnTypeAndCollat
27448 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
27449 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
2744a 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 73  Parsing contexts
2744b 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20   */.  int nCol, 
2744c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2744d 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
2744e 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43   */.  Column *aC
2744f 6f 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ol,         /* L
27450 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a  ist of columns *
27451 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
27452 65 63 74 20 20 20 20 20 20 20 2f 2a 20 53 45 4c  ect       /* SEL
27453 45 43 54 20 75 73 65 64 20 74 6f 20 64 65 74 65  ECT used to dete
27454 72 6d 69 6e 65 20 74 79 70 65 73 20 61 6e 64 20  rmine types and 
27455 63 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f 0a 29 7b  collations */.){
27456 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
27457 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 4e   pParse->db;.  N
27458 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a  ameContext sNC;.
27459 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
2745a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2745b 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78 70  ;.  int i;.  Exp
2745c 72 20 2a 70 3b 0a 20 20 73 74 72 75 63 74 20 45  r *p;.  struct E
2745d 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 3b  xprList_item *a;
2745e 0a 0a 20 20 61 73 73 65 72 74 28 20 70 53 65 6c  ..  assert( pSel
2745f 65 63 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ect!=0 );.  asse
27460 72 74 28 20 28 70 53 65 6c 65 63 74 2d 3e 73 65  rt( (pSelect->se
27461 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f  lFlags & SF_Reso
27462 6c 76 65 64 29 21 3d 30 20 29 3b 0a 20 20 61 73  lved)!=0 );.  as
27463 73 65 72 74 28 20 6e 43 6f 6c 3d 3d 70 53 65 6c  sert( nCol==pSel
27464 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  ect->pEList->nEx
27465 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  pr || db->malloc
27466 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
27467 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
27468 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d   ) return;.  mem
27469 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
2746a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
2746b 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c  .pSrcList = pSel
2746c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 61 20 3d  ect->pSrc;.  a =
2746d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
2746e 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  ->a;.  for(i=0, 
2746f 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f  pCol=aCol; i<nCo
27470 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; i++, pCol++){
27471 0a 20 20 20 20 70 20 3d 20 61 5b 69 5d 2e 70 45  .    p = a[i].pE
27472 78 70 72 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a  xpr;.    pCol->z
27473 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Type = sqlite3Db
27474 53 74 72 44 75 70 28 64 62 2c 20 63 6f 6c 75 6d  StrDup(db, colum
27475 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 30  nType(&sNC, p, 0
27476 2c 20 30 2c 20 30 29 29 3b 0a 20 20 20 20 70 43  , 0, 0));.    pC
27477 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73  ol->affinity = s
27478 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
27479 74 79 28 70 29 3b 0a 20 20 20 20 70 43 6f 6c 6c  ty(p);.    pColl
2747a 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2747b 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 29  llSeq(pParse, p)
2747c 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ;.    if( pColl 
2747d 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a  ){.      pCol->z
2747e 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  Coll = sqlite3Db
2747f 53 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 6c  StrDup(db, pColl
27480 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ->zName);.    }.
27481 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76    }.}../*.** Giv
27482 65 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  en a SELECT stat
27483 65 6d 65 6e 74 2c 20 67 65 6e 65 72 61 74 65 20  ement, generate 
27484 61 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  a Table structur
27485 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
27486 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73  .** the result s
27487 65 74 20 6f 66 20 74 68 61 74 20 53 45 4c 45 43  et of that SELEC
27488 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  T..*/.SQLITE_PRI
27489 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
2748a 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65  te3ResultSetOfSe
2748b 6c 65 63 74 28 50 61 72 73 65 20 2a 70 50 61 72  lect(Parse *pPar
2748c 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  se, Select *pSel
2748d 65 63 74 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ect){.  Table *p
2748e 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Tab;.  sqlite3 *
2748f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
27490 0a 20 20 69 6e 74 20 73 61 76 65 64 46 6c 61 67  .  int savedFlag
27491 73 3b 0a 0a 20 20 73 61 76 65 64 46 6c 61 67 73  s;..  savedFlags
27492 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20   = db->flags;.  
27493 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51  db->flags &= ~SQ
27494 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65  LITE_FullColName
27495 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  s;.  db->flags |
27496 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f  = SQLITE_ShortCo
27497 6c 4e 61 6d 65 73 3b 0a 20 20 73 71 6c 69 74 65  lNames;.  sqlite
27498 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 72  3SelectPrep(pPar
27499 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 30 29 3b  se, pSelect, 0);
2749a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
2749b 45 72 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  Err ) return 0;.
2749c 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74    while( pSelect
2749d 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c 65  ->pPrior ) pSele
2749e 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50  ct = pSelect->pP
2749f 72 69 6f 72 3b 0a 20 20 64 62 2d 3e 66 6c 61 67  rior;.  db->flag
274a0 73 20 3d 20 73 61 76 65 64 46 6c 61 67 73 3b 0a  s = savedFlags;.
274a1 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
274a2 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
274a3 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 20 29   sizeof(Table) )
274a4 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
274a5 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
274a6 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e 64 62 4d  .  }.  pTab->dbM
274a7 65 6d 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  em = db->lookasi
274a8 64 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 64 62  de.bEnabled ? db
274a9 20 3a 20 30 3b 0a 20 20 70 54 61 62 2d 3e 6e 52   : 0;.  pTab->nR
274aa 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62 2d 3e  ef = 1;.  pTab->
274ab 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 73 65 6c  zName = 0;.  sel
274ac 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78  ectColumnsFromEx
274ad 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
274ae 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20  Select->pEList, 
274af 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70 54  &pTab->nCol, &pT
274b0 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 73 65 6c  ab->aCol);.  sel
274b1 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  ectAddColumnType
274b2 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61  AndCollation(pPa
274b3 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  rse, pTab->nCol,
274b4 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65   pTab->aCol, pSe
274b5 6c 65 63 74 29 3b 0a 20 20 70 54 61 62 2d 3e 69  lect);.  pTab->i
274b6 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 69 66 28  PKey = -1;.  if(
274b7 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
274b8 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
274b9 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62  DeleteTable(pTab
274ba 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
274bb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54  .  }.  return pT
274bc 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  ab;.}../*.** Get
274bd 20 61 20 56 44 42 45 20 66 6f 72 20 74 68 65 20   a VDBE for the 
274be 67 69 76 65 6e 20 70 61 72 73 65 72 20 63 6f 6e  given parser con
274bf 74 65 78 74 2e 20 20 43 72 65 61 74 65 20 61 20  text.  Create a 
274c0 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73  new one if neces
274c1 73 61 72 79 2e 0a 2a 2a 20 49 66 20 61 6e 20 65  sary..** If an e
274c2 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74  rror occurs, ret
274c3 75 72 6e 20 4e 55 4c 4c 20 61 6e 64 20 6c 65 61  urn NULL and lea
274c4 76 65 20 61 20 6d 65 73 73 61 67 65 20 69 6e 20  ve a message in 
274c5 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  pParse..*/.SQLIT
274c6 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a  E_PRIVATE Vdbe *
274c7 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 50  sqlite3GetVdbe(P
274c8 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
274c9 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
274ca 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 66 28 20  e->pVdbe;.  if( 
274cb 76 3d 3d 30 20 29 7b 0a 20 20 20 20 76 20 3d 20  v==0 ){.    v = 
274cc 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20  pParse->pVdbe = 
274cd 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74  sqlite3VdbeCreat
274ce 65 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 23  e(pParse->db);.#
274cf 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
274d0 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28  IT_TRACE.    if(
274d1 20 76 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69   v ){.      sqli
274d2 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
274d3 20 4f 50 5f 54 72 61 63 65 29 3b 0a 20 20 20 20   OP_Trace);.    
274d4 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  }.#endif.  }.  r
274d5 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn v;.}.../*.
274d6 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 69  ** Compute the i
274d7 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
274d8 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  t fields of the 
274d9 53 45 4c 45 43 54 20 62 61 73 65 64 20 6f 6e 20  SELECT based on 
274da 74 68 65 0a 2a 2a 20 70 4c 69 6d 69 74 20 61 6e  the.** pLimit an
274db 64 20 70 4f 66 66 73 65 74 20 65 78 70 72 65 73  d pOffset expres
274dc 73 69 6f 6e 73 2e 20 20 70 4c 69 6d 69 74 20 61  sions.  pLimit a
274dd 6e 64 20 70 4f 66 66 73 65 74 20 68 6f 6c 64 20  nd pOffset hold 
274de 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a  the expressions.
274df 2a 2a 20 74 68 61 74 20 61 70 70 65 61 72 20 69  ** that appear i
274e0 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  n the original S
274e1 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74  QL statement aft
274e2 65 72 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64  er the LIMIT and
274e3 20 4f 46 46 53 45 54 0a 2a 2a 20 6b 65 79 77 6f   OFFSET.** keywo
274e4 72 64 73 2e 20 20 4f 72 20 4e 55 4c 4c 20 69 66  rds.  Or NULL if
274e5 20 74 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20   those keywords 
274e6 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 69 4c 69  are omitted. iLi
274e7 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20  mit and iOffset 
274e8 0a 2a 2a 20 61 72 65 20 74 68 65 20 69 6e 74 65  .** are the inte
274e9 67 65 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73  ger memory regis
274ea 74 65 72 20 6e 75 6d 62 65 72 73 20 66 6f 72 20  ter numbers for 
274eb 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20 74 6f  counters used to
274ec 20 63 6f 6d 70 75 74 65 20 0a 2a 2a 20 74 68 65   compute .** the
274ed 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65   limit and offse
274ee 74 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  t.  If there is 
274ef 6e 6f 20 6c 69 6d 69 74 20 61 6e 64 2f 6f 72 20  no limit and/or 
274f0 6f 66 66 73 65 74 2c 20 74 68 65 6e 20 0a 2a 2a  offset, then .**
274f1 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66   iLimit and iOff
274f2 73 65 74 20 61 72 65 20 6e 65 67 61 74 69 76 65  set are negative
274f3 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
274f4 74 69 6e 65 20 63 68 61 6e 67 65 73 20 74 68 65  tine changes the
274f5 20 76 61 6c 75 65 73 20 6f 66 20 69 4c 69 6d 69   values of iLimi
274f6 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 6f 6e  t and iOffset on
274f7 6c 79 20 69 66 0a 2a 2a 20 61 20 6c 69 6d 69 74  ly if.** a limit
274f8 20 6f 72 20 6f 66 66 73 65 74 20 69 73 20 64 65   or offset is de
274f9 66 69 6e 65 64 20 62 79 20 70 4c 69 6d 69 74 20  fined by pLimit 
274fa 61 6e 64 20 70 4f 66 66 73 65 74 2e 20 20 69 4c  and pOffset.  iL
274fb 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 69 4f 66 66  imit and.** iOff
274fc 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20  set should have 
274fd 62 65 65 6e 20 70 72 65 73 65 74 20 74 6f 20 61  been preset to a
274fe 70 70 72 6f 70 72 69 61 74 65 20 64 65 66 61 75  ppropriate defau
274ff 6c 74 20 76 61 6c 75 65 73 0a 2a 2a 20 28 75 73  lt values.** (us
27500 75 61 6c 6c 79 20 62 75 74 20 6e 6f 74 20 61 6c  ually but not al
27501 77 61 79 73 20 2d 31 29 20 70 72 69 6f 72 20 74  ways -1) prior t
27502 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  o calling this r
27503 6f 75 74 69 6e 65 2e 0a 2a 2a 20 4f 6e 6c 79 20  outine..** Only 
27504 69 66 20 70 4c 69 6d 69 74 21 3d 30 20 6f 72 20  if pLimit!=0 or 
27505 70 4f 66 66 73 65 74 21 3d 30 20 64 6f 20 74 68  pOffset!=0 do th
27506 65 20 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72  e limit register
27507 73 20 67 65 74 0a 2a 2a 20 72 65 64 65 66 69 6e  s get.** redefin
27508 65 64 2e 20 20 54 68 65 20 55 4e 49 4f 4e 20 41  ed.  The UNION A
27509 4c 4c 20 6f 70 65 72 61 74 6f 72 20 75 73 65 73  LL operator uses
2750a 20 74 68 69 73 20 70 72 6f 70 65 72 74 79 20 74   this property t
2750b 6f 20 66 6f 72 63 65 0a 2a 2a 20 74 68 65 20 72  o force.** the r
2750c 65 75 73 65 20 6f 66 20 74 68 65 20 73 61 6d 65  euse of the same
2750d 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65   limit and offse
2750e 74 20 72 65 67 69 73 74 65 72 73 20 61 63 72 6f  t registers acro
2750f 73 73 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 53  ss multiple.** S
27510 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
27511 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
27512 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
27513 69 73 74 65 72 73 28 50 61 72 73 65 20 2a 70 50  isters(Parse *pP
27514 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c  arse, Select *p,
27515 20 69 6e 74 20 69 42 72 65 61 6b 29 7b 0a 20 20   int iBreak){.  
27516 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69  Vdbe *v = 0;.  i
27517 6e 74 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  nt iLimit = 0;. 
27518 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 20 20   int iOffset;.  
27519 69 6e 74 20 61 64 64 72 31 3b 0a 20 20 69 66 28  int addr1;.  if(
2751a 20 70 2d 3e 69 4c 69 6d 69 74 20 29 20 72 65 74   p->iLimit ) ret
2751b 75 72 6e 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a  urn;..  /* .  **
2751c 20 22 4c 49 4d 49 54 20 2d 31 22 20 61 6c 77 61   "LIMIT -1" alwa
2751d 79 73 20 73 68 6f 77 73 20 61 6c 6c 20 72 6f 77  ys shows all row
2751e 73 2e 20 20 54 68 65 72 65 20 69 73 20 73 6f 6d  s.  There is som
2751f 65 0a 20 20 2a 2a 20 63 6f 6e 74 72 61 76 65 72  e.  ** contraver
27520 73 79 20 61 62 6f 75 74 20 77 68 61 74 20 74 68  sy about what th
27521 65 20 63 6f 72 72 65 63 74 20 62 65 68 61 76 69  e correct behavi
27522 6f 72 20 73 68 6f 75 6c 64 20 62 65 2e 0a 20 20  or should be..  
27523 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 69  ** The current i
27524 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e  mplementation in
27525 74 65 72 70 72 65 74 73 20 22 4c 49 4d 49 54 20  terprets "LIMIT 
27526 30 22 20 74 6f 20 6d 65 61 6e 0a 20 20 2a 2a 20  0" to mean.  ** 
27527 6e 6f 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20  no rows..  */.  
27528 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b  if( p->pLimit ){
27529 0a 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d  .    p->iLimit =
2752a 20 69 4c 69 6d 69 74 20 3d 20 2b 2b 70 50 61 72   iLimit = ++pPar
2752b 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 76 20  se->nMem;.    v 
2752c 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2752d 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
2752e 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
2752f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
27530 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d 3e  Code(pParse, p->
27531 70 4c 69 6d 69 74 2c 20 69 4c 69 6d 69 74 29 3b  pLimit, iLimit);
27532 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27533 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
27534 74 42 65 49 6e 74 2c 20 69 4c 69 6d 69 74 29 3b  tBeInt, iLimit);
27535 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
27536 28 28 76 2c 20 22 4c 49 4d 49 54 20 63 6f 75 6e  ((v, "LIMIT coun
27537 74 65 72 22 29 29 3b 0a 20 20 20 20 73 71 6c 69  ter"));.    sqli
27538 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27539 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d   OP_IfZero, iLim
2753a 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d  it, iBreak);.  }
2753b 0a 20 20 69 66 28 20 70 2d 3e 70 4f 66 66 73 65  .  if( p->pOffse
2753c 74 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4f 66 66  t ){.    p->iOff
2753d 73 65 74 20 3d 20 69 4f 66 66 73 65 74 20 3d 20  set = iOffset = 
2753e 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2753f 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69      if( p->pLimi
27540 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  t ){.      pPars
27541 65 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20 20 2f 2a 20  e->nMem++;   /* 
27542 41 6c 6c 6f 63 61 74 65 20 61 6e 20 65 78 74 72  Allocate an extr
27543 61 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 6c  a register for l
27544 69 6d 69 74 2b 6f 66 66 73 65 74 20 2a 2f 0a 20  imit+offset */. 
27545 20 20 20 7d 0a 20 20 20 20 76 20 3d 20 73 71 6c     }.    v = sql
27546 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
27547 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d  se);.    if( v==
27548 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
27549 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2754a 70 50 61 72 73 65 2c 20 70 2d 3e 70 4f 66 66 73  pParse, p->pOffs
2754b 65 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20  et, iOffset);.  
2754c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2754d 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65  Op1(v, OP_MustBe
2754e 49 6e 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20  Int, iOffset);. 
2754f 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
27550 76 2c 20 22 4f 46 46 53 45 54 20 63 6f 75 6e 74  v, "OFFSET count
27551 65 72 22 29 29 3b 0a 20 20 20 20 61 64 64 72 31  er"));.    addr1
27552 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
27553 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73  dOp1(v, OP_IfPos
27554 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  , iOffset);.    
27555 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27556 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
27557 20 30 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20   0, iOffset);.  
27558 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
27559 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
2755a 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d  .    if( p->pLim
2755b 69 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  it ){.      sqli
2755c 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2755d 20 4f 50 5f 41 64 64 2c 20 69 4c 69 6d 69 74 2c   OP_Add, iLimit,
2755e 20 69 4f 66 66 73 65 74 2c 20 69 4f 66 66 73 65   iOffset, iOffse
2755f 74 2b 31 29 3b 0a 20 20 20 20 20 20 56 64 62 65  t+1);.      Vdbe
27560 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d  Comment((v, "LIM
27561 49 54 2b 4f 46 46 53 45 54 22 29 29 3b 0a 20 20  IT+OFFSET"));.  
27562 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
27563 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
27564 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4c 69 6d 69   OP_IfPos, iLimi
27565 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
27566 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27567 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 69  P_Integer, -1, i
27568 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 20 20 20  Offset+1);.     
27569 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2756a 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a  Here(v, addr1);.
2756b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66      }.  }.}..#if
2756c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2756d 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2756e 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
2756f 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 6f  e appropriate co
27570 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
27571 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68   for the iCol-th
27572 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68   column of.** th
27573 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72  e result set for
27574 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 2d 73 65   the compound-se
27575 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 22  lect statement "
27576 70 22 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c  p".  Return NULL
27577 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d   if.** the colum
27578 6e 20 68 61 73 20 6e 6f 20 64 65 66 61 75 6c 74  n has no default
27579 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2757a 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  nce..**.** The c
2757b 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2757c 65 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f 75  e for the compou
2757d 6e 64 20 73 65 6c 65 63 74 20 69 73 20 74 61 6b  nd select is tak
2757e 65 6e 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c  en from the.** l
2757f 65 66 74 2d 6d 6f 73 74 20 74 65 72 6d 20 6f 66  eft-most term of
27580 20 74 68 65 20 73 65 6c 65 63 74 20 74 68 61 74   the select that
27581 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67   has a collating
27582 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2f 0a 73 74   sequence..*/.st
27583 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 6d 75  atic CollSeq *mu
27584 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71  ltiSelectCollSeq
27585 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
27586 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69  Select *p, int i
27587 43 6f 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  Col){.  CollSeq 
27588 2a 70 52 65 74 3b 0a 20 20 69 66 28 20 70 2d 3e  *pRet;.  if( p->
27589 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 70 52  pPrior ){.    pR
2758a 65 74 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74  et = multiSelect
2758b 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2758c 70 2d 3e 70 50 72 69 6f 72 2c 20 69 43 6f 6c 29  p->pPrior, iCol)
2758d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2758e 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  Ret = 0;.  }.  i
2758f 66 28 20 70 52 65 74 3d 3d 30 20 29 7b 0a 20 20  f( pRet==0 ){.  
27590 20 20 70 52 65 74 20 3d 20 73 71 6c 69 74 65 33    pRet = sqlite3
27591 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
27592 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61  se, p->pEList->a
27593 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 29 3b 0a 20  [iCol].pExpr);. 
27594 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74   }.  return pRet
27595 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
27596 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
27597 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a  ND_SELECT */../*
27598 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
27599 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ce */.static int
2759a 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65   multiSelectOrde
2759b 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50  rBy(.  Parse *pP
2759c 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
2759d 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2759e 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
2759f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
275a0 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20  e right-most of 
275a1 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f  SELECTs to be co
275a2 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  ded */.  SelectD
275a3 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f  est *pDest     /
275a4 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74  * What to do wit
275a5 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20  h query results 
275a6 2a 2f 0a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20  */.);...#ifndef 
275a7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
275a8 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a  OUND_SELECT./*.*
275a9 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
275aa 73 20 63 61 6c 6c 65 64 20 74 6f 20 70 72 6f 63  s called to proc
275ab 65 73 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71  ess a compound q
275ac 75 65 72 79 20 66 6f 72 6d 20 66 72 6f 6d 0a 2a  uery form from.*
275ad 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 65  * two or more se
275ae 70 61 72 61 74 65 20 71 75 65 72 69 65 73 20 75  parate queries u
275af 73 69 6e 67 20 55 4e 49 4f 4e 2c 20 55 4e 49 4f  sing UNION, UNIO
275b0 4e 20 41 4c 4c 2c 20 45 58 43 45 50 54 2c 20 6f  N ALL, EXCEPT, o
275b1 72 0a 2a 2a 20 49 4e 54 45 52 53 45 43 54 0a 2a  r.** INTERSECT.*
275b2 2a 0a 2a 2a 20 22 70 22 20 70 6f 69 6e 74 73 20  *.** "p" points 
275b3 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  to the right-mos
275b4 74 20 6f 66 20 74 68 65 20 74 77 6f 20 71 75 65  t of the two que
275b5 72 69 65 73 2e 20 20 74 68 65 20 71 75 65 72 79  ries.  the query
275b6 20 6f 6e 20 74 68 65 0a 2a 2a 20 6c 65 66 74 20   on the.** left 
275b7 69 73 20 70 2d 3e 70 50 72 69 6f 72 2e 20 20 54  is p->pPrior.  T
275b8 68 65 20 6c 65 66 74 20 71 75 65 72 79 20 63 6f  he left query co
275b9 75 6c 64 20 61 6c 73 6f 20 62 65 20 61 20 63 6f  uld also be a co
275ba 6d 70 6f 75 6e 64 20 71 75 65 72 79 0a 2a 2a 20  mpound query.** 
275bb 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
275bc 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
275bd 62 65 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73  be called recurs
275be 69 76 65 6c 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  ively. .**.** Th
275bf 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  e results of the
275c0 20 74 6f 74 61 6c 20 71 75 65 72 79 20 61 72 65   total query are
275c1 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69   to be written i
275c2 6e 74 6f 20 61 20 64 65 73 74 69 6e 61 74 69 6f  nto a destinatio
275c3 6e 0a 2a 2a 20 6f 66 20 74 79 70 65 20 65 44 65  n.** of type eDe
275c4 73 74 20 77 69 74 68 20 70 61 72 61 6d 65 74 65  st with paramete
275c5 72 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 45  r iParm..**.** E
275c6 78 61 6d 70 6c 65 20 31 3a 20 20 43 6f 6e 73 69  xample 1:  Consi
275c7 64 65 72 20 61 20 74 68 72 65 65 2d 77 61 79 20  der a three-way 
275c8 63 6f 6d 70 6f 75 6e 64 20 53 51 4c 20 73 74 61  compound SQL sta
275c9 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20  tement..**.**   
275ca 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20    SELECT a FROM 
275cb 74 31 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20  t1 UNION SELECT 
275cc 62 20 46 52 4f 4d 20 74 32 20 55 4e 49 4f 4e 20  b FROM t2 UNION 
275cd 53 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33  SELECT c FROM t3
275ce 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 61 74  .**.** This stat
275cf 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 20  ement is parsed 
275d0 75 70 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  up as follows:.*
275d1 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
275d2 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 20 20 20 20  c FROM t3.**    
275d3 20 20 7c 0a 2a 2a 20 20 20 20 20 20 60 2d 2d 2d    |.**      `---
275d4 2d 2d 3e 20 20 53 45 4c 45 43 54 20 62 20 46 52  -->  SELECT b FR
275d5 4f 4d 20 74 32 0a 2a 2a 20 20 20 20 20 20 20 20  OM t2.**        
275d6 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20          |.**    
275d7 20 20 20 20 20 20 20 20 20 20 20 20 60 2d 2d 2d              `---
275d8 2d 2d 2d 3e 20 20 53 45 4c 45 43 54 20 61 20 46  --->  SELECT a F
275d9 52 4f 4d 20 74 31 0a 2a 2a 0a 2a 2a 20 54 68 65  ROM t1.**.** The
275da 20 61 72 72 6f 77 73 20 69 6e 20 74 68 65 20 64   arrows in the d
275db 69 61 67 72 61 6d 20 61 62 6f 76 65 20 72 65 70  iagram above rep
275dc 72 65 73 65 6e 74 20 74 68 65 20 53 65 6c 65 63  resent the Selec
275dd 74 2e 70 50 72 69 6f 72 20 70 6f 69 6e 74 65 72  t.pPrior pointer
275de 2e 0a 2a 2a 20 53 6f 20 69 66 20 74 68 69 73 20  ..** So if this 
275df 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
275e0 64 20 77 69 74 68 20 70 20 65 71 75 61 6c 20 74  d with p equal t
275e1 6f 20 74 68 65 20 74 33 20 71 75 65 72 79 2c 20  o the t3 query, 
275e2 74 68 65 6e 0a 2a 2a 20 70 50 72 69 6f 72 20 77  then.** pPrior w
275e3 69 6c 6c 20 62 65 20 74 68 65 20 74 32 20 71 75  ill be the t2 qu
275e4 65 72 79 2e 20 20 70 2d 3e 6f 70 20 77 69 6c 6c  ery.  p->op will
275e5 20 62 65 20 54 4b 5f 55 4e 49 4f 4e 20 69 6e 20   be TK_UNION in 
275e6 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  this case..**.**
275e7 20 4e 6f 74 69 63 65 20 74 68 61 74 20 62 65 63   Notice that bec
275e8 61 75 73 65 20 6f 66 20 74 68 65 20 77 61 79 20  ause of the way 
275e9 53 51 4c 69 74 65 20 70 61 72 73 65 73 20 63 6f  SQLite parses co
275ea 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 2c 20  mpound SELECTs, 
275eb 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61  the.** individua
275ec 6c 20 73 65 6c 65 63 74 73 20 61 6c 77 61 79 73  l selects always
275ed 20 67 72 6f 75 70 20 66 72 6f 6d 20 6c 65 66 74   group from left
275ee 20 74 6f 20 72 69 67 68 74 2e 0a 2a 2f 0a 73 74   to right..*/.st
275ef 61 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65  atic int multiSe
275f0 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70  lect(.  Parse *p
275f1 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
275f2 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
275f3 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
275f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
275f5 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66  he right-most of
275f6 20 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63   SELECTs to be c
275f7 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74  oded */.  Select
275f8 44 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20  Dest *pDest     
275f9 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69  /* What to do wi
275fa 74 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  th query results
275fb 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
275fc 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f  = SQLITE_OK;   /
275fd 2a 20 53 75 63 63 65 73 73 20 63 6f 64 65 20 66  * Success code f
275fe 72 6f 6d 20 61 20 73 75 62 72 6f 75 74 69 6e 65  rom a subroutine
275ff 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50   */.  Select *pP
27600 72 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 41  rior;       /* A
27601 6e 6f 74 68 65 72 20 53 45 4c 45 43 54 20 69 6d  nother SELECT im
27602 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6f 75 72  mediately to our
27603 20 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62 65 20   left */.  Vdbe 
27604 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
27605 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
27606 65 20 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a  e to this VDBE *
27607 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64  /.  SelectDest d
27608 65 73 74 3b 20 20 20 20 20 20 2f 2a 20 41 6c 74  est;      /* Alt
27609 65 72 6e 61 74 69 76 65 20 64 61 74 61 20 64 65  ernative data de
2760a 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 53  stination */.  S
2760b 65 6c 65 63 74 20 2a 70 44 65 6c 65 74 65 20 3d  elect *pDelete =
2760c 20 30 3b 20 20 2f 2a 20 43 68 61 69 6e 20 6f 66   0;  /* Chain of
2760d 20 73 69 6d 70 6c 65 20 73 65 6c 65 63 74 73 20   simple selects 
2760e 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 73  to delete */.  s
2760f 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
27610 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
27611 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
27612 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
27613 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52  here is no ORDER
27614 20 42 59 20 6f 72 20 4c 49 4d 49 54 20 63 6c 61   BY or LIMIT cla
27615 75 73 65 20 6f 6e 20 70 72 69 6f 72 20 53 45 4c  use on prior SEL
27616 45 43 54 73 2e 20 20 4f 6e 6c 79 0a 20 20 2a 2a  ECTs.  Only.  **
27617 20 74 68 65 20 6c 61 73 74 20 28 72 69 67 68 74   the last (right
27618 2d 6d 6f 73 74 29 20 53 45 4c 45 43 54 20 69 6e  -most) SELECT in
27619 20 74 68 65 20 73 65 72 69 65 73 20 6d 61 79 20   the series may 
2761a 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59  have an ORDER BY
2761b 20 6f 72 20 4c 49 4d 49 54 2e 0a 20 20 2a 2f 0a   or LIMIT..  */.
2761c 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70    assert( p && p
2761d 2d 3e 70 50 72 69 6f 72 20 29 3b 20 20 2f 2a 20  ->pPrior );  /* 
2761e 43 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  Calling function
2761f 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 69 73   guarantees this
27620 20 6d 75 63 68 20 2a 2f 0a 20 20 64 62 20 3d 20   much */.  db = 
27621 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50  pParse->db;.  pP
27622 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72  rior = p->pPrior
27623 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69  ;.  assert( pPri
27624 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d  or->pRightmost!=
27625 70 50 72 69 6f 72 20 29 3b 0a 20 20 61 73 73 65  pPrior );.  asse
27626 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67  rt( pPrior->pRig
27627 68 74 6d 6f 73 74 3d 3d 70 2d 3e 70 52 69 67 68  htmost==p->pRigh
27628 74 6d 6f 73 74 20 29 3b 0a 20 20 64 65 73 74 20  tmost );.  dest 
27629 3d 20 2a 70 44 65 73 74 3b 0a 20 20 69 66 28 20  = *pDest;.  if( 
2762a 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79  pPrior->pOrderBy
2762b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2762c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
2762d 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2762e 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65  should come afte
2762f 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22  r %s not before"
27630 2c 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70  ,.      selectOp
27631 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20  Name(p->op));.  
27632 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f    rc = 1;.    go
27633 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
27634 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  end;.  }.  if( p
27635 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 7b  Prior->pLimit ){
27636 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
27637 72 4d 73 67 28 70 50 61 72 73 65 2c 22 4c 49 4d  rMsg(pParse,"LIM
27638 49 54 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64  IT clause should
27639 20 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e   come after %s n
2763a 6f 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20  ot before",.    
2763b 20 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70    selectOpName(p
2763c 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d  ->op));.    rc =
2763d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c   1;.    goto mul
2763e 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
2763f 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   }..  v = sqlite
27640 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
27641 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
27642 20 29 3b 20 20 2f 2a 20 54 68 65 20 56 44 42 45   );  /* The VDBE
27643 20 61 6c 72 65 61 64 79 20 63 72 65 61 74 65 64   already created
27644 20 62 79 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63   by calling func
27645 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 43 72  tion */..  /* Cr
27646 65 61 74 65 20 74 68 65 20 64 65 73 74 69 6e 61  eate the destina
27647 74 69 6f 6e 20 74 65 6d 70 6f 72 61 72 79 20 74  tion temporary t
27648 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72  able if necessar
27649 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 65 73  y.  */.  if( des
2764a 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68  t.eDest==SRT_Eph
2764b 65 6d 54 61 62 20 29 7b 0a 20 20 20 20 61 73 73  emTab ){.    ass
2764c 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29  ert( p->pEList )
2764d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2764e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
2764f 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64 65 73  enEphemeral, des
27650 74 2e 69 50 61 72 6d 2c 20 70 2d 3e 70 45 4c 69  t.iParm, p->pELi
27651 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20  st->nExpr);.    
27652 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54  dest.eDest = SRT
27653 5f 54 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f  _Table;.  }..  /
27654 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  * Make sure all 
27655 53 45 4c 45 43 54 73 20 69 6e 20 74 68 65 20 73  SELECTs in the s
27656 74 61 74 65 6d 65 6e 74 20 68 61 76 65 20 74 68  tatement have th
27657 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66  e same number of
27658 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 69   elements.  ** i
27659 6e 20 74 68 65 69 72 20 72 65 73 75 6c 74 20 73  n their result s
2765a 65 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ets..  */.  asse
2765b 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26  rt( p->pEList &&
2765c 20 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 20   pPrior->pEList 
2765d 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c 69  );.  if( p->pELi
2765e 73 74 2d 3e 6e 45 78 70 72 21 3d 70 50 72 69 6f  st->nExpr!=pPrio
2765f 72 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  r->pEList->nExpr
27660 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
27661 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
27662 22 53 45 4c 45 43 54 73 20 74 6f 20 74 68 65 20  "SELECTs to the 
27663 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 6f  left and right o
27664 66 20 25 73 22 0a 20 20 20 20 20 20 22 20 64 6f  f %s".      " do
27665 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 73 61   not have the sa
27666 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73  me number of res
27667 75 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c 20 73 65  ult columns", se
27668 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70  lectOpName(p->op
27669 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a  ));.    rc = 1;.
2766a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73      goto multi_s
2766b 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a  elect_end;.  }..
2766c 20 20 2f 2a 20 43 6f 6d 70 6f 75 6e 64 20 53 45    /* Compound SE
2766d 4c 45 43 54 73 20 74 68 61 74 20 68 61 76 65 20  LECTs that have 
2766e 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
2766f 73 65 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73  se are handled s
27670 65 70 61 72 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a  eparately..  */.
27671 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42    if( p->pOrderB
27672 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  y ){.    return 
27673 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72  multiSelectOrder
27674 42 79 28 70 50 61 72 73 65 2c 20 70 2c 20 70 44  By(pParse, p, pD
27675 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  est);.  }..  /* 
27676 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
27677 72 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72  r the left and r
27678 69 67 68 74 20 53 45 4c 45 43 54 20 73 74 61 74  ight SELECT stat
27679 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 73  ements..  */.  s
2767a 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a  witch( p->op ){.
2767b 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a      case TK_ALL:
2767c 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64   {.      int add
2767d 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73  r = 0;.      ass
2767e 65 72 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4c  ert( !pPrior->pL
2767f 69 6d 69 74 20 29 3b 0a 20 20 20 20 20 20 70 50  imit );.      pP
27680 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 3d 20 70  rior->pLimit = p
27681 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20  ->pLimit;.      
27682 70 50 72 69 6f 72 2d 3e 70 4f 66 66 73 65 74 20  pPrior->pOffset 
27683 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20  = p->pOffset;.  
27684 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
27685 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
27686 50 72 69 6f 72 2c 20 26 64 65 73 74 29 3b 0a 20  Prior, &dest);. 
27687 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d       p->pLimit =
27688 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66   0;.      p->pOf
27689 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  fset = 0;.      
2768a 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
2768b 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
2768c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
2768d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  .      p->pPrior
2768e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 69   = 0;.      p->i
2768f 4c 69 6d 69 74 20 3d 20 70 50 72 69 6f 72 2d 3e  Limit = pPrior->
27690 69 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d  iLimit;.      p-
27691 3e 69 4f 66 66 73 65 74 20 3d 20 70 50 72 69 6f  >iOffset = pPrio
27692 72 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20  r->iOffset;.    
27693 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20    if( p->iLimit 
27694 29 7b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20  ){.        addr 
27695 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
27696 4f 70 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f  Op1(v, OP_IfZero
27697 2c 20 70 2d 3e 69 4c 69 6d 69 74 29 3b 0a 20 20  , p->iLimit);.  
27698 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
27699 74 28 28 76 2c 20 22 4a 75 6d 70 20 61 68 65 61  t((v, "Jump ahea
2769a 64 20 69 66 20 4c 49 4d 49 54 20 72 65 61 63 68  d if LIMIT reach
2769b 65 64 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ed"));.      }. 
2769c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2769d 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2769e 70 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20  p, &dest);.     
2769f 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50   pDelete = p->pP
276a0 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70  rior;.      p->p
276a1 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a  Prior = pPrior;.
276a2 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
276a3 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
276a4 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
276a5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
276a6 20 61 64 64 72 20 29 7b 0a 20 20 20 20 20 20 20   addr ){.       
276a7 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
276a8 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20  Here(v, addr);. 
276a9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
276aa 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
276ab 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 0a 20 20  se TK_EXCEPT:.  
276ac 20 20 63 61 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a    case TK_UNION:
276ad 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 75 6e 69   {.      int uni
276ae 6f 6e 54 61 62 3b 20 20 20 20 2f 2a 20 43 75 72  onTab;    /* Cur
276af 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
276b0 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
276b1 65 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74  e holding result
276b2 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 6f 70 20   */.      u8 op 
276b3 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e  = 0;       /* On
276b4 65 20 6f 66 20 74 68 65 20 53 52 54 5f 20 6f 70  e of the SRT_ op
276b5 65 72 61 74 69 6f 6e 73 20 74 6f 20 61 70 70 6c  erations to appl
276b6 79 20 74 6f 20 73 65 6c 66 20 2a 2f 0a 20 20 20  y to self */.   
276b7 20 20 20 69 6e 74 20 70 72 69 6f 72 4f 70 3b 20     int priorOp; 
276b8 20 20 20 20 2f 2a 20 54 68 65 20 53 52 54 5f 20      /* The SRT_ 
276b9 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 70 70  operation to app
276ba 6c 79 20 74 6f 20 70 72 69 6f 72 20 73 65 6c 65  ly to prior sele
276bb 63 74 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70  cts */.      Exp
276bc 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66  r *pLimit, *pOff
276bd 73 65 74 3b 20 2f 2a 20 53 61 76 65 64 20 76 61  set; /* Saved va
276be 6c 75 65 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d 69  lues of p->nLimi
276bf 74 20 61 6e 64 20 70 2d 3e 6e 4f 66 66 73 65 74  t and p->nOffset
276c0 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
276c1 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 74  dr;.      Select
276c2 44 65 73 74 20 75 6e 69 6f 6e 64 65 73 74 3b 0a  Dest uniondest;.
276c3 0a 20 20 20 20 20 20 70 72 69 6f 72 4f 70 20 3d  .      priorOp =
276c4 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20   SRT_Union;.    
276c5 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
276c6 3d 3d 70 72 69 6f 72 4f 70 20 26 26 20 41 4c 57  ==priorOp && ALW
276c7 41 59 53 28 21 70 2d 3e 70 4c 69 6d 69 74 20 26  AYS(!p->pLimit &
276c8 26 21 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b  &!p->pOffset) ){
276c9 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 63  .        /* We c
276ca 61 6e 20 72 65 75 73 65 20 61 20 74 65 6d 70 6f  an reuse a tempo
276cb 72 61 72 79 20 74 61 62 6c 65 20 67 65 6e 65 72  rary table gener
276cc 61 74 65 64 20 62 79 20 61 20 53 45 4c 45 43 54  ated by a SELECT
276cd 20 74 6f 20 6f 75 72 0a 20 20 20 20 20 20 20 20   to our.        
276ce 2a 2a 20 72 69 67 68 74 2e 0a 20 20 20 20 20 20  ** right..      
276cf 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73    */.        ass
276d0 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f  ert( p->pRightmo
276d1 73 74 21 3d 70 20 29 3b 20 20 2f 2a 20 43 61 6e  st!=p );  /* Can
276d2 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 66 6f 72   only happen for
276d3 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
276d4 74 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ts.             
276d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
276d6 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20          ** of a 
276d7 33 2d 77 61 79 20 6f 72 20 6d 6f 72 65 20 63 6f  3-way or more co
276d8 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20  mpound */.      
276d9 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4c 69    assert( p->pLi
276da 6d 69 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f  mit==0 );      /
276db 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e  * Not allowed on
276dc 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
276dd 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  ts */.        as
276de 73 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74  sert( p->pOffset
276df 3d 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20 4e 6f  ==0 );     /* No
276e0 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 6c 65 66  t allowed on lef
276e1 74 77 61 72 64 20 65 6c 65 6d 65 6e 74 73 20 2a  tward elements *
276e2 2f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54  /.        unionT
276e3 61 62 20 3d 20 64 65 73 74 2e 69 50 61 72 6d 3b  ab = dest.iParm;
276e4 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
276e5 20 20 20 20 20 20 2f 2a 20 57 65 20 77 69 6c 6c        /* We will
276e6 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20   need to create 
276e7 6f 75 72 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72  our own temporar
276e8 79 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20  y table to hold 
276e9 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  the.        ** i
276ea 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65 73 75  ntermediate resu
276eb 6c 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  lts..        */.
276ec 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54 61 62          unionTab
276ed 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
276ee 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  +;.        asser
276ef 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d  t( p->pOrderBy==
276f0 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 64 64  0 );.        add
276f1 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
276f2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
276f3 45 70 68 65 6d 65 72 61 6c 2c 20 75 6e 69 6f 6e  Ephemeral, union
276f4 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Tab, 0);.       
276f5 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72   assert( p->addr
276f6 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d 20 2d  OpenEphm[0] == -
276f7 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  1 );.        p->
276f8 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
276f9 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 20 20  = addr;.        
276fa 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73  p->pRightmost->s
276fb 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73  elFlags |= SF_Us
276fc 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 20  esEphemeral;.   
276fd 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
276fe 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20  pEList );.      
276ff 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65  }..      /* Code
27700 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
27701 65 6d 65 6e 74 73 20 74 6f 20 6f 75 72 20 6c 65  ements to our le
27702 66 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ft.      */.    
27703 20 20 61 73 73 65 72 74 28 20 21 70 50 72 69 6f    assert( !pPrio
27704 72 2d 3e 70 4f 72 64 65 72 42 79 20 29 3b 0a 20  r->pOrderBy );. 
27705 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
27706 63 74 44 65 73 74 49 6e 69 74 28 26 75 6e 69 6f  ctDestInit(&unio
27707 6e 64 65 73 74 2c 20 70 72 69 6f 72 4f 70 2c 20  ndest, priorOp, 
27708 75 6e 69 6f 6e 54 61 62 29 3b 0a 20 20 20 20 20  unionTab);.     
27709 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
2770a 65 63 74 28 70 50 61 72 73 65 2c 20 70 50 72 69  ect(pParse, pPri
2770b 6f 72 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b  or, &uniondest);
2770c 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
2770d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
2770e 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
2770f 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
27710 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65  * Code the curre
27711 6e 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nt SELECT statem
27712 65 6e 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ent.      */.   
27713 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b     if( p->op==TK
27714 5f 45 58 43 45 50 54 20 29 7b 0a 20 20 20 20 20  _EXCEPT ){.     
27715 20 20 20 6f 70 20 3d 20 53 52 54 5f 45 78 63 65     op = SRT_Exce
27716 70 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  pt;.      }else{
27717 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
27718 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e   p->op==TK_UNION
27719 20 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 20 3d   );.        op =
2771a 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20   SRT_Union;.    
2771b 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72    }.      p->pPr
2771c 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ior = 0;.      p
2771d 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
2771e 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  t;.      p->pLim
2771f 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f  it = 0;.      pO
27720 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73  ffset = p->pOffs
27721 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66  et;.      p->pOf
27722 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  fset = 0;.      
27723 75 6e 69 6f 6e 64 65 73 74 2e 65 44 65 73 74 20  uniondest.eDest 
27724 3d 20 6f 70 3b 0a 20 20 20 20 20 20 72 63 20 3d  = op;.      rc =
27725 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
27726 50 61 72 73 65 2c 20 70 2c 20 26 75 6e 69 6f 6e  Parse, p, &union
27727 64 65 73 74 29 3b 0a 20 20 20 20 20 20 2f 2a 20  dest);.      /* 
27728 51 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e 67  Query flattening
27729 20 69 6e 20 73 71 6c 69 74 65 33 53 65 6c 65 63   in sqlite3Selec
2772a 74 28 29 20 6d 69 67 68 74 20 72 65 66 69 6c 6c  t() might refill
2772b 20 70 2d 3e 70 4f 72 64 65 72 42 79 2e 0a 20 20   p->pOrderBy..  
2772c 20 20 20 20 2a 2a 20 42 65 20 73 75 72 65 20 74      ** Be sure t
2772d 6f 20 64 65 6c 65 74 65 20 70 2d 3e 70 4f 72 64  o delete p->pOrd
2772e 65 72 42 79 2c 20 74 68 65 72 65 66 6f 72 65 2c  erBy, therefore,
2772f 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f   to avoid a memo
27730 72 79 20 6c 65 61 6b 2e 20 2a 2f 0a 20 20 20 20  ry leak. */.    
27731 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
27732 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
27733 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 20 20  OrderBy);.      
27734 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50 72  pDelete = p->pPr
27735 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50  ior;.      p->pP
27736 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20  rior = pPrior;. 
27737 20 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79       p->pOrderBy
27738 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 0;.      sqli
27739 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2773a 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20  , p->pLimit);.  
2773b 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
2773c 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d  pLimit;.      p-
2773d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73  >pOffset = pOffs
2773e 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 69  et;.      p->iLi
2773f 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  mit = 0;.      p
27740 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ->iOffset = 0;. 
27741 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
27742 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
27743 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
27744 20 20 20 20 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a      }...      /*
27745 20 43 6f 6e 76 65 72 74 20 74 68 65 20 64 61 74   Convert the dat
27746 61 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61  a in the tempora
27747 72 79 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  ry table into wh
27748 61 74 65 76 65 72 20 66 6f 72 6d 0a 20 20 20 20  atever form.    
27749 20 20 2a 2a 20 69 74 20 69 73 20 74 68 61 74 20    ** it is that 
2774a 77 65 20 63 75 72 72 65 6e 74 6c 79 20 6e 65 65  we currently nee
2774b 64 2e 0a 20 20 20 20 20 20 2a 2f 20 20 20 20 20  d..      */     
2774c 20 0a 20 20 20 20 20 20 69 66 28 20 64 65 73 74   .      if( dest
2774d 2e 65 44 65 73 74 21 3d 70 72 69 6f 72 4f 70 20  .eDest!=priorOp 
2774e 7c 7c 20 75 6e 69 6f 6e 54 61 62 21 3d 64 65 73  || unionTab!=des
2774f 74 2e 69 50 61 72 6d 20 29 7b 0a 20 20 20 20 20  t.iParm ){.     
27750 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42     int iCont, iB
27751 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20  reak, iStart;.  
27752 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
27753 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20  >pEList );.     
27754 20 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73     if( dest.eDes
27755 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b  t==SRT_Output ){
27756 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c 65 63  .          Selec
27757 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a 20  t *pFirst = p;. 
27758 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
27759 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29  pFirst->pPrior )
2775a 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 73 74   pFirst = pFirst
2775b 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
2775c 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75      generateColu
2775d 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2775e 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73  0, pFirst->pELis
2775f 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  t);.        }.  
27760 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d 20 73        iBreak = s
27761 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
27762 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20  bel(v);.        
27763 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  iCont = sqlite3V
27764 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
27765 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
27766 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
27767 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b  Parse, p, iBreak
27768 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
27769 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2776a 4f 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69 6f 6e  OP_Rewind, union
2776b 54 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20  Tab, iBreak);.  
2776c 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 73        iStart = s
2776d 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2776e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
2776f 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f    selectInnerLoo
27770 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e  p(pParse, p, p->
27771 70 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54 61 62  pEList, unionTab
27772 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  , p->pEList->nEx
27773 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr,.            
27774 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 2d              0, -
27775 31 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74 2c  1, &dest, iCont,
27776 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20   iBreak);.      
27777 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
27778 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f  olveLabel(v, iCo
27779 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nt);.        sql
2777a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2777b 2c 20 4f 50 5f 4e 65 78 74 2c 20 75 6e 69 6f 6e  , OP_Next, union
2777c 54 61 62 2c 20 69 53 74 61 72 74 29 3b 0a 20 20  Tab, iStart);.  
2777d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2777e 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2777f 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20   iBreak);.      
27780 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27781 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
27782 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20   unionTab, 0);. 
27783 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
27784 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
27785 73 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a  se TK_INTERSECT:
27786 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 61 62   {.      int tab
27787 31 2c 20 74 61 62 32 3b 0a 20 20 20 20 20 20 69  1, tab2;.      i
27788 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b  nt iCont, iBreak
27789 2c 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20  , iStart;.      
2778a 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70  Expr *pLimit, *p
2778b 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69 6e  Offset;.      in
2778c 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 53 65  t addr;.      Se
2778d 6c 65 63 74 44 65 73 74 20 69 6e 74 65 72 73 65  lectDest interse
2778e 63 74 64 65 73 74 3b 0a 20 20 20 20 20 20 69 6e  ctdest;.      in
2778f 74 20 72 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  t r1;..      /* 
27790 49 4e 54 45 52 53 45 43 54 20 69 73 20 64 69 66  INTERSECT is dif
27791 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  ferent from the 
27792 6f 74 68 65 72 73 20 73 69 6e 63 65 20 69 74 20  others since it 
27793 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a  requires.      *
27794 2a 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 20  * two temporary 
27795 74 61 62 6c 65 73 2e 20 20 48 65 6e 63 65 20 69  tables.  Hence i
27796 74 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63 61  t has its own ca
27797 73 65 2e 20 20 42 65 67 69 6e 0a 20 20 20 20 20  se.  Begin.     
27798 20 2a 2a 20 62 79 20 61 6c 6c 6f 63 61 74 69 6e   ** by allocatin
27799 67 20 74 68 65 20 74 61 62 6c 65 73 20 77 65 20  g the tables we 
2779a 77 69 6c 6c 20 6e 65 65 64 2e 0a 20 20 20 20 20  will need..     
2779b 20 2a 2f 0a 20 20 20 20 20 20 74 61 62 31 20 3d   */.      tab1 =
2779c 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
2779d 0a 20 20 20 20 20 20 74 61 62 32 20 3d 20 70 50  .      tab2 = pP
2779e 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
2779f 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
277a0 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 0a 20  OrderBy==0 );.. 
277a1 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
277a2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
277a3 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
277a4 6c 2c 20 74 61 62 31 2c 20 30 29 3b 0a 20 20 20  l, tab1, 0);.   
277a5 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64     assert( p->ad
277a6 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d  drOpenEphm[0] ==
277a7 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e   -1 );.      p->
277a8 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
277a9 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d  = addr;.      p-
277aa 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73 65 6c  >pRightmost->sel
277ab 46 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73 65 73  Flags |= SF_Uses
277ac 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 20 20 20  Ephemeral;.     
277ad 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69   assert( p->pELi
277ae 73 74 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  st );..      /* 
277af 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54 73  Code the SELECTs
277b0 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 69 6e 74   to our left int
277b1 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  o temporary tabl
277b2 65 20 22 74 61 62 31 22 2e 0a 20 20 20 20 20 20  e "tab1"..      
277b3 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
277b4 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
277b5 69 6e 74 65 72 73 65 63 74 64 65 73 74 2c 20 53  intersectdest, S
277b6 52 54 5f 55 6e 69 6f 6e 2c 20 74 61 62 31 29 3b  RT_Union, tab1);
277b7 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
277b8 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
277b9 2c 20 70 50 72 69 6f 72 2c 20 26 69 6e 74 65 72  , pPrior, &inter
277ba 73 65 63 74 64 65 73 74 29 3b 0a 20 20 20 20 20  sectdest);.     
277bb 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
277bc 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65     goto multi_se
277bd 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20  lect_end;.      
277be 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65  }..      /* Code
277bf 20 74 68 65 20 63 75 72 72 65 6e 74 20 53 45 4c   the current SEL
277c0 45 43 54 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61  ECT into tempora
277c1 72 79 20 74 61 62 6c 65 20 22 74 61 62 32 22 0a  ry table "tab2".
277c2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
277c3 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
277c4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
277c5 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 74 61 62  enEphemeral, tab
277c6 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73  2, 0);.      ass
277c7 65 72 74 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e  ert( p->addrOpen
277c8 45 70 68 6d 5b 31 5d 20 3d 3d 20 2d 31 20 29 3b  Ephm[1] == -1 );
277c9 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70  .      p->addrOp
277ca 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 61 64 64 72  enEphm[1] = addr
277cb 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f  ;.      p->pPrio
277cc 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 69  r = 0;.      pLi
277cd 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b  mit = p->pLimit;
277ce 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74  .      p->pLimit
277cf 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 66 66   = 0;.      pOff
277d0 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74  set = p->pOffset
277d1 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73  ;.      p->pOffs
277d2 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e  et = 0;.      in
277d3 74 65 72 73 65 63 74 64 65 73 74 2e 69 50 61 72  tersectdest.iPar
277d4 6d 20 3d 20 74 61 62 32 3b 0a 20 20 20 20 20 20  m = tab2;.      
277d5 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  rc = sqlite3Sele
277d6 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 69  ct(pParse, p, &i
277d7 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a 20  ntersectdest);. 
277d8 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70       pDelete = p
277d9 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
277da 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
277db 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  or;.      sqlite
277dc 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
277dd 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20  p->pLimit);.    
277de 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c    p->pLimit = pL
277df 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  imit;.      p->p
277e0 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74  Offset = pOffset
277e1 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
277e2 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d  {.        goto m
277e3 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b  ulti_select_end;
277e4 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
277e5 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
277e6 20 74 6f 20 74 61 6b 65 20 74 68 65 20 69 6e 74   to take the int
277e7 65 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65  ersection of the
277e8 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a 20   two temporary. 
277e9 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e 0a       ** tables..
277ea 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
277eb 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74  ssert( p->pEList
277ec 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65   );.      if( de
277ed 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  st.eDest==SRT_Ou
277ee 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20  tput ){.        
277ef 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d  Select *pFirst =
277f0 20 70 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c   p;.        whil
277f1 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f  e( pFirst->pPrio
277f2 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69  r ) pFirst = pFi
277f3 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  rst->pPrior;.   
277f4 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c       generateCol
277f5 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
277f6 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69   0, pFirst->pELi
277f7 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  st);.      }.   
277f8 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69     iBreak = sqli
277f9 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
277fa 28 76 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e 74  (v);.      iCont
277fb 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
277fc 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
277fd 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65    computeLimitRe
277fe 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20  gisters(pParse, 
277ff 70 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  p, iBreak);.    
27800 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27801 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
27802 2c 20 74 61 62 31 2c 20 69 42 72 65 61 6b 29 3b  , tab1, iBreak);
27803 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
27804 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
27805 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 53 74  arse);.      iSt
27806 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  art = sqlite3Vdb
27807 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
27808 77 4b 65 79 2c 20 74 61 62 31 2c 20 72 31 29 3b  wKey, tab1, r1);
27809 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2780a 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
2780b 6f 74 46 6f 75 6e 64 2c 20 74 61 62 32 2c 20 69  otFound, tab2, i
2780c 43 6f 6e 74 2c 20 72 31 29 3b 0a 20 20 20 20 20  Cont, r1);.     
2780d 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2780e 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2780f 31 29 3b 0a 20 20 20 20 20 20 73 65 6c 65 63 74  1);.      select
27810 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65  InnerLoop(pParse
27811 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20  , p, p->pEList, 
27812 74 61 62 31 2c 20 70 2d 3e 70 45 4c 69 73 74 2d  tab1, p->pEList-
27813 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20  >nExpr,.        
27814 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
27815 20 2d 31 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e   -1, &dest, iCon
27816 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  t, iBreak);.    
27817 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
27818 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f  olveLabel(v, iCo
27819 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  nt);.      sqlit
2781a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2781b 4f 50 5f 4e 65 78 74 2c 20 74 61 62 31 2c 20 69  OP_Next, tab1, i
2781c 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20 73 71  Start);.      sq
2781d 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2781e 4c 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29  Label(v, iBreak)
2781f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27820 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27821 43 6c 6f 73 65 2c 20 74 61 62 32 2c 20 30 29 3b  Close, tab2, 0);
27822 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
27823 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
27824 6c 6f 73 65 2c 20 74 61 62 31 2c 20 30 29 3b 0a  lose, tab1, 0);.
27825 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27826 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d   }.  }..  /* Com
27827 70 75 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73  pute collating s
27828 65 71 75 65 6e 63 65 73 20 75 73 65 64 20 62 79  equences used by
27829 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79   .  ** temporary
2782a 20 74 61 62 6c 65 73 20 6e 65 65 64 65 64 20 74   tables needed t
2782b 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
2782c 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e  compound select.
2782d 0a 20 20 2a 2a 20 41 74 74 61 63 68 20 74 68 65  .  ** Attach the
2782e 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
2782f 72 65 20 74 6f 20 61 6c 6c 20 74 65 6d 70 6f 72  re to all tempor
27830 61 72 79 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a  ary tables..  **
27831 0a 20 20 2a 2a 20 54 68 69 73 20 73 65 63 74 69  .  ** This secti
27832 6f 6e 20 69 73 20 72 75 6e 20 62 79 20 74 68 65  on is run by the
27833 20 72 69 67 68 74 2d 6d 6f 73 74 20 53 45 4c 45   right-most SELE
27834 43 54 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 6c  CT statement onl
27835 79 2e 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 73  y..  ** SELECT s
27836 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 74 68 65  tatements to the
27837 20 6c 65 66 74 20 61 6c 77 61 79 73 20 73 6b 69   left always ski
27838 70 20 74 68 69 73 20 70 61 72 74 2e 20 20 54 68  p this part.  Th
27839 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a  e right-most.  *
2783a 2a 20 53 45 4c 45 43 54 20 6d 69 67 68 74 20 61  * SELECT might a
2783b 6c 73 6f 20 73 6b 69 70 20 74 68 69 73 20 70 61  lso skip this pa
2783c 72 74 20 69 66 20 69 74 20 68 61 73 20 6e 6f 20  rt if it has no 
2783d 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2783e 61 6e 64 0a 20 20 2a 2a 20 6e 6f 20 74 65 6d 70  and.  ** no temp
2783f 20 74 61 62 6c 65 73 20 61 72 65 20 72 65 71 75   tables are requ
27840 69 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ired..  */.  if(
27841 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53   p->selFlags & S
27842 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 20  F_UsesEphemeral 
27843 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20  ){.    int i;   
27844 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27845 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
27846 6e 74 65 72 20 2a 2f 0a 20 20 20 20 4b 65 79 49  nter */.    KeyI
27847 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20  nfo *pKeyInfo;  
27848 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
27849 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
2784a 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73  for the result s
2784b 65 74 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74  et */.    Select
2784c 20 2a 70 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20   *pLoop;        
2784d 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
2784e 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 53  ooping through S
2784f 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
27850 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20   */.    CollSeq 
27851 2a 2a 61 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20  **apColl;       
27852 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
27853 70 69 6e 67 20 74 68 72 6f 75 67 68 20 70 4b 65  ping through pKe
27854 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d 20 2a  yInfo->aColl[] *
27855 2f 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 20  /.    int nCol; 
27856 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27857 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
27858 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73 75   columns in resu
27859 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 20 20 61  lt set */..    a
2785a 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74  ssert( p->pRight
2785b 6d 6f 73 74 3d 3d 70 20 29 3b 0a 20 20 20 20 6e  most==p );.    n
2785c 43 6f 6c 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d  Col = p->pEList-
2785d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 70 4b 65 79  >nExpr;.    pKey
2785e 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62  Info = sqlite3Db
2785f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 0a 20  MallocZero(db,. 
27860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27861 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4b        sizeof(*pK
27862 65 79 49 6e 66 6f 29 2b 6e 43 6f 6c 2a 28 73 69  eyInfo)+nCol*(si
27863 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b  zeof(CollSeq*) +
27864 20 31 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70   1));.    if( !p
27865 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20  KeyInfo ){.     
27866 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
27867 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d  EM;.      goto m
27868 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b  ulti_select_end;
27869 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4b 65 79  .    }..    pKey
2786a 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28  Info->enc = ENC(
2786b 64 62 29 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66  db);.    pKeyInf
2786c 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  o->nField = (u16
2786d 29 6e 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72 28  )nCol;..    for(
2786e 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65 79  i=0, apColl=pKey
2786f 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c 6e  Info->aColl; i<n
27870 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c 6c  Col; i++, apColl
27871 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 61 70 43 6f  ++){.      *apCo
27872 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74  ll = multiSelect
27873 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
27874 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28  p, i);.      if(
27875 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a 20   0==*apColl ){. 
27876 20 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d         *apColl =
27877 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a   db->pDfltColl;.
27878 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
27879 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20     for(pLoop=p; 
2787a 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f  pLoop; pLoop=pLo
2787b 6f 70 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20  op->pPrior){.   
2787c 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 3b     for(i=0; i<2;
2787d 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
2787e 6e 74 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70 2d  nt addr = pLoop-
2787f 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d  >addrOpenEphm[i]
27880 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 64  ;.        if( ad
27881 64 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  dr<0 ){.        
27882 20 20 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20 75    /* If [0] is u
27883 6e 75 73 65 64 20 74 68 65 6e 20 5b 31 5d 20 69  nused then [1] i
27884 73 20 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20 20  s also unused.  
27885 53 6f 20 77 65 20 63 61 6e 0a 20 20 20 20 20 20  So we can.      
27886 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 61      ** always sa
27887 66 65 6c 79 20 61 62 6f 72 74 20 61 73 20 73 6f  fely abort as so
27888 6f 6e 20 61 73 20 74 68 65 20 66 69 72 73 74 20  on as the first 
27889 75 6e 75 73 65 64 20 73 6c 6f 74 20 69 73 20 66  unused slot is f
2788a 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  ound */.        
2788b 20 20 61 73 73 65 72 74 28 20 70 4c 6f 6f 70 2d    assert( pLoop-
2788c 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d  >addrOpenEphm[1]
2788d 3c 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  <0 );.          
2788e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
2788f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27890 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
27891 61 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20  addr, nCol);.   
27892 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27893 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
27894 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66  , (char*)pKeyInf
27895 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a  o, P4_KEYINFO);.
27896 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 61          pLoop->a
27897 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20 3d  ddrOpenEphm[i] =
27898 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   -1;.      }.   
27899 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
2789a 46 72 65 65 28 64 62 2c 20 70 4b 65 79 49 6e 66  Free(db, pKeyInf
2789b 6f 29 3b 0a 20 20 7d 0a 0a 6d 75 6c 74 69 5f 73  o);.  }..multi_s
2789c 65 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 70 44 65  elect_end:.  pDe
2789d 73 74 2d 3e 69 4d 65 6d 20 3d 20 64 65 73 74 2e  st->iMem = dest.
2789e 69 4d 65 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 6e  iMem;.  pDest->n
2789f 4d 65 6d 20 3d 20 64 65 73 74 2e 6e 4d 65 6d 3b  Mem = dest.nMem;
278a0 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
278a1 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 65  Delete(db, pDele
278a2 74 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  te);.  return rc
278a3 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
278a4 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
278a5 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a  ND_SELECT */../*
278a6 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 6f 75 74 70  .** Code an outp
278a7 75 74 20 73 75 62 72 6f 75 74 69 6e 65 20 66 6f  ut subroutine fo
278a8 72 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 69 6d  r a coroutine im
278a9 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
278aa 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74  a.** SELECT stat
278ab 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
278ac 64 61 74 61 20 74 6f 20 62 65 20 6f 75 74 70 75  data to be outpu
278ad 74 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69  t is contained i
278ae 6e 20 70 49 6e 2d 3e 69 4d 65 6d 2e 20 20 54 68  n pIn->iMem.  Th
278af 65 72 65 20 61 72 65 0a 2a 2a 20 70 49 6e 2d 3e  ere are.** pIn->
278b0 6e 4d 65 6d 20 63 6f 6c 75 6d 6e 73 20 74 6f 20  nMem columns to 
278b1 62 65 20 6f 75 74 70 75 74 2e 20 20 70 44 65 73  be output.  pDes
278b2 74 20 69 73 20 77 68 65 72 65 20 74 68 65 20 6f  t is where the o
278b3 75 74 70 75 74 20 73 68 6f 75 6c 64 0a 2a 2a 20  utput should.** 
278b4 62 65 20 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 72  be sent..**.** r
278b5 65 67 52 65 74 75 72 6e 20 69 73 20 74 68 65 20  egReturn is the 
278b6 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 65  number of the re
278b7 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74  gister holding t
278b8 68 65 20 73 75 62 72 6f 75 74 69 6e 65 0a 2a 2a  he subroutine.**
278b9 20 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 2e   return address.
278ba 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 50 72 65  .**.** If regPre
278bb 76 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 61  v>0 then it is a
278bc 20 74 68 65 20 66 69 72 73 74 20 72 65 67 69 73   the first regis
278bd 74 65 72 20 69 6e 20 61 20 76 65 63 74 6f 72 20  ter in a vector 
278be 74 68 61 74 0a 2a 2a 20 72 65 63 6f 72 64 73 20  that.** records 
278bf 74 68 65 20 70 72 65 76 69 6f 75 73 20 6f 75 74  the previous out
278c0 70 75 74 2e 20 20 6d 65 6d 5b 72 65 67 50 72 65  put.  mem[regPre
278c1 76 5d 20 69 73 20 61 20 66 6c 61 67 20 74 68 61  v] is a flag tha
278c2 74 20 69 73 20 66 61 6c 73 65 0a 2a 2a 20 69 66  t is false.** if
278c3 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e 20   there has been 
278c4 6e 6f 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70  no previous outp
278c5 75 74 2e 20 20 49 66 20 72 65 67 50 72 65 76 3e  ut.  If regPrev>
278c6 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 0a 2a  0 then code is.*
278c7 2a 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 73  * generated to s
278c8 75 70 70 72 65 73 73 20 64 75 70 6c 69 63 61 74  uppress duplicat
278c9 65 73 2e 20 20 70 4b 65 79 49 6e 66 6f 20 69 73  es.  pKeyInfo is
278ca 20 75 73 65 64 20 66 6f 72 20 63 6f 6d 70 61 72   used for compar
278cb 69 6e 67 0a 2a 2a 20 6b 65 79 73 2e 0a 2a 2a 0a  ing.** keys..**.
278cc 2a 2a 20 49 66 20 74 68 65 20 4c 49 4d 49 54 20  ** If the LIMIT 
278cd 66 6f 75 6e 64 20 69 6e 20 70 2d 3e 69 4c 69 6d  found in p->iLim
278ce 69 74 20 69 73 20 72 65 61 63 68 65 64 2c 20 6a  it is reached, j
278cf 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
278d0 74 6f 0a 2a 2a 20 69 42 72 65 61 6b 2e 0a 2a 2f  to.** iBreak..*/
278d1 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e 65  .static int gene
278d2 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75  rateOutputSubrou
278d3 74 69 6e 65 28 0a 20 20 50 61 72 73 65 20 2a 70  tine(.  Parse *p
278d4 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
278d5 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
278d6 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
278d7 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
278d8 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
278d9 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c  atement */.  Sel
278da 65 63 74 44 65 73 74 20 2a 70 49 6e 2c 20 20 20  ectDest *pIn,   
278db 20 20 20 20 20 2f 2a 20 43 6f 72 6f 75 74 69 6e       /* Coroutin
278dc 65 20 73 75 70 70 6c 79 69 6e 67 20 64 61 74 61  e supplying data
278dd 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
278de 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a   *pDest,      /*
278df 20 57 68 65 72 65 20 74 6f 20 73 65 6e 64 20 74   Where to send t
278e0 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74  he data */.  int
278e1 20 72 65 67 52 65 74 75 72 6e 2c 20 20 20 20 20   regReturn,     
278e2 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75       /* The retu
278e3 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73  rn address regis
278e4 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ter */.  int reg
278e5 50 72 65 76 2c 20 20 20 20 20 20 20 20 20 20 20  Prev,           
278e6 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 72 65 73   /* Previous res
278e7 75 6c 74 20 72 65 67 69 73 74 65 72 2e 20 20 4e  ult register.  N
278e8 6f 20 75 6e 69 71 75 65 6e 65 73 73 20 69 66 20  o uniqueness if 
278e9 30 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  0 */.  KeyInfo *
278ea 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 2f  pKeyInfo,      /
278eb 2a 20 46 6f 72 20 63 6f 6d 70 61 72 69 6e 67 20  * For comparing 
278ec 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 65 6e  with previous en
278ed 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 70 34 74  try */.  int p4t
278ee 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ype,            
278ef 20 2f 2a 20 54 68 65 20 70 34 20 74 79 70 65 20   /* The p4 type 
278f0 66 6f 72 20 70 4b 65 79 49 6e 66 6f 20 2a 2f 0a  for pKeyInfo */.
278f1 20 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20 20    int iBreak    
278f2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
278f3 70 20 68 65 72 65 20 69 66 20 77 65 20 68 69 74  p here if we hit
278f4 20 74 68 65 20 4c 49 4d 49 54 20 2a 2f 0a 29 7b   the LIMIT */.){
278f5 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
278f6 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
278f7 74 20 69 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 69  t iContinue;.  i
278f8 6e 74 20 61 64 64 72 3b 0a 0a 20 20 61 64 64 72  nt addr;..  addr
278f9 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
278fa 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
278fb 69 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c 69  iContinue = sqli
278fc 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
278fd 28 76 29 3b 0a 0a 20 20 2f 2a 20 53 75 70 70 72  (v);..  /* Suppr
278fe 65 73 73 20 64 75 70 6c 69 63 61 74 65 73 20 66  ess duplicates f
278ff 6f 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54  or UNION, EXCEPT
27900 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54 20  , and INTERSECT 
27901 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67 50  .  */.  if( regP
27902 72 65 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a  rev ){.    int j
27903 31 2c 20 6a 32 3b 0a 20 20 20 20 6a 31 20 3d 20  1, j2;.    j1 = 
27904 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27905 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72  1(v, OP_IfNot, r
27906 65 67 50 72 65 76 29 3b 0a 20 20 20 20 6a 32 20  egPrev);.    j2 
27907 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
27908 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72  Op4(v, OP_Compar
27909 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72 65  e, pIn->iMem, re
2790a 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e 4d  gPrev+1, pIn->nM
2790b 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  em,.            
2790c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2790d 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66    (char*)pKeyInf
2790e 6f 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 20 20  o, p4type);.    
2790f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27910 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 32  3(v, OP_Jump, j2
27911 2b 32 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6a  +2, iContinue, j
27912 32 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  2+2);.    sqlite
27913 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
27914 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j1);.    sqlite
27915 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28 70 50  3ExprCodeCopy(pP
27916 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  arse, pIn->iMem,
27917 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d   regPrev+1, pIn-
27918 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69  >nMem);.    sqli
27919 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2791a 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
2791b 72 65 67 50 72 65 76 29 3b 0a 20 20 7d 0a 20 20  regPrev);.  }.  
2791c 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  if( pParse->db->
2791d 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
2791e 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 53  eturn 0;..  /* S
2791f 75 70 70 72 65 73 73 20 74 68 65 20 74 68 65 20  uppress the the 
27920 66 69 72 73 74 20 4f 46 46 53 45 54 20 65 6e 74  first OFFSET ent
27921 72 69 65 73 20 69 66 20 74 68 65 72 65 20 69 73  ries if there is
27922 20 61 6e 20 4f 46 46 53 45 54 20 63 6c 61 75 73   an OFFSET claus
27923 65 0a 20 20 2a 2f 0a 20 20 63 6f 64 65 4f 66 66  e.  */.  codeOff
27924 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69  set(v, p, iConti
27925 6e 75 65 29 3b 0a 0a 20 20 73 77 69 74 63 68 28  nue);..  switch(
27926 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 29 7b   pDest->eDest ){
27927 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68  .    /* Store th
27928 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 61  e result as data
27929 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65 20   using a unique 
2792a 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  key..    */.    
2792b 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a  case SRT_Table:.
2792c 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 70 68      case SRT_Eph
2792d 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 69  emTab: {.      i
2792e 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  nt r1 = sqlite3G
2792f 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
27930 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 32 20  );.      int r2 
27931 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
27932 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
27933 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27934 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
27935 65 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d  ecord, pIn->iMem
27936 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 2c 20 72 31 29  , pIn->nMem, r1)
27937 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27938 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27939 4e 65 77 52 6f 77 69 64 2c 20 70 44 65 73 74 2d  NewRowid, pDest-
2793a 3e 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20  >iParm, r2);.   
2793b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2793c 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
2793d 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c  t, pDest->iParm,
2793e 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20   r1, r2);.      
2793f 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
27940 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50  eP5(v, OPFLAG_AP
27941 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 73 71 6c  PEND);.      sql
27942 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
27943 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a  eg(pParse, r2);.
27944 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
27945 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
27946 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62  se, r1);.      b
27947 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
27948 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27949 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 2f 2a  _SUBQUERY.    /*
2794a 20 49 66 20 77 65 20 61 72 65 20 63 72 65 61 74   If we are creat
2794b 69 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e  ing a set for an
2794c 20 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43   "expr IN (SELEC
2794d 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63  T ...)" construc
2794e 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  t,.    ** then t
2794f 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61  here should be a
27950 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20   single item on 
27951 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74  the stack.  Writ
27952 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 74  e this.    ** it
27953 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74 20  em into the set 
27954 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75 73  table with bogus
27955 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20   data..    */.  
27956 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20    case SRT_Set: 
27957 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a  {.      int r1;.
27958 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
27959 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20  n->nMem==1 );.  
2795a 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79 20      p->affinity 
2795b 3d 20 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  = .         sqli
2795c 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
2795d 74 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  ty(p->pEList->a[
2795e 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d  0].pExpr, pDest-
2795f 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20  >affinity);.    
27960 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
27961 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
27962 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27963 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
27964 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d  MakeRecord, pIn-
27965 3e 69 4d 65 6d 2c 20 31 2c 20 72 31 2c 20 26 70  >iMem, 1, r1, &p
27966 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
27967 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
27968 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
27969 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e  ange(pParse, pIn
2796a 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20  ->iMem, 1);.    
2796b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2796c 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
2796d 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72  ert, pDest->iPar
2796e 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  m, r1);.      sq
2796f 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
27970 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
27971 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
27972 20 20 7d 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e    }..#if 0  /* N
27973 65 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20 61  ever occurs on a
27974 6e 20 4f 52 44 45 52 20 42 59 20 71 75 65 72 79  n ORDER BY query
27975 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e   */.    /* If an
27976 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74  y row exist in t
27977 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72  he result set, r
27978 65 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 20  ecord that fact 
27979 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a  and abort..    *
2797a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45  /.    case SRT_E
2797b 78 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73  xists: {.      s
2797c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2797d 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2797e 31 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  1, pDest->iParm)
2797f 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c  ;.      /* The L
27980 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c  IMIT clause will
27981 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c   terminate the l
27982 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
27983 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
27984 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
27985 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61  If this is a sca
27986 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20  lar select that 
27987 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78  is part of an ex
27988 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20  pression, then. 
27989 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20     ** store the 
2798a 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61  results in the a
2798b 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72  ppropriate memor
2798c 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b  y cell and break
2798d 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74   out.    ** of t
2798e 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20  he scan loop..  
2798f 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
27990 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61  T_Mem: {.      a
27991 73 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d  ssert( pIn->nMem
27992 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
27993 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
27994 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d  (pParse, pIn->iM
27995 65 6d 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  em, pDest->iParm
27996 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  , 1);.      /* T
27997 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
27998 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66  will jump out of
27999 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73   the loop for us
2799a 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   */.      break;
2799b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
2799c 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
2799d 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
2799e 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73  ..    /* The res
2799f 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ults are stored 
279a0 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  in a sequence of
279a1 20 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a   registers.    *
279a2 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 44  * starting at pD
279a3 65 73 74 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 6e  est->iMem.  Then
279a4 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20   the co-routine 
279a5 79 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  yields..    */. 
279a6 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f     case SRT_Coro
279a7 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20 69  utine: {.      i
279a8 66 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d  f( pDest->iMem==
279a9 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65  0 ){.        pDe
279aa 73 74 2d 3e 69 4d 65 6d 20 3d 20 73 71 6c 69 74  st->iMem = sqlit
279ab 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
279ac 50 61 72 73 65 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  Parse, pIn->nMem
279ad 29 3b 0a 20 20 20 20 20 20 20 20 70 44 65 73 74  );.        pDest
279ae 2d 3e 6e 4d 65 6d 20 3d 20 70 49 6e 2d 3e 6e 4d  ->nMem = pIn->nM
279af 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  em;.      }.    
279b0 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
279b1 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49  eMove(pParse, pI
279b2 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e  n->iMem, pDest->
279b3 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d 65  iMem, pDest->nMe
279b4 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  m);.      sqlite
279b5 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
279b6 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e  P_Yield, pDest->
279b7 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 62 72  iParm);.      br
279b8 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
279b9 2f 2a 20 52 65 73 75 6c 74 73 20 61 72 65 20 73  /* Results are s
279ba 74 6f 72 65 64 20 69 6e 20 61 20 73 65 71 75 65  tored in a seque
279bb 6e 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73  nce of registers
279bc 2e 20 20 54 68 65 6e 20 74 68 65 0a 20 20 20 20  .  Then the.    
279bd 2a 2a 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20  ** OP_ResultRow 
279be 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74  opcode is used t
279bf 6f 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f  o cause sqlite3_
279c0 73 74 65 70 28 29 20 74 6f 20 72 65 74 75 72 6e  step() to return
279c1 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78 74  .    ** the next
279c2 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a   row of result..
279c3 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
279c4 53 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20 20  SRT_Output: {.  
279c5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
279c6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
279c7 6c 74 52 6f 77 2c 20 70 49 6e 2d 3e 69 4d 65 6d  ltRow, pIn->iMem
279c8 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  , pIn->nMem);.  
279c9 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
279ca 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
279cb 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e  ge(pParse, pIn->
279cc 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29  iMem, pIn->nMem)
279cd 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
279ce 20 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e     }..#if !defin
279cf 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
279d0 52 49 47 47 45 52 29 0a 20 20 20 20 2f 2a 20 44  RIGGER).    /* D
279d1 69 73 63 61 72 64 20 74 68 65 20 72 65 73 75 6c  iscard the resul
279d2 74 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  ts.  This is use
279d3 64 20 66 6f 72 20 53 45 4c 45 43 54 20 73 74 61  d for SELECT sta
279d4 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 0a 20  tements inside. 
279d5 20 20 20 2a 2a 20 74 68 65 20 62 6f 64 79 20 6f     ** the body o
279d6 66 20 61 20 54 52 49 47 47 45 52 2e 20 20 54 68  f a TRIGGER.  Th
279d7 65 20 70 75 72 70 6f 73 65 20 6f 66 20 73 75 63  e purpose of suc
279d8 68 20 73 65 6c 65 63 74 73 20 69 73 20 74 6f 20  h selects is to 
279d9 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75 73 65 72  call.    ** user
279da 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
279db 6e 73 20 74 68 61 74 20 68 61 76 65 20 73 69 64  ns that have sid
279dc 65 20 65 66 66 65 63 74 73 2e 20 20 57 65 20 64  e effects.  We d
279dd 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20 20 20 2a  o not care.    *
279de 2a 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 75  * about the actu
279df 61 6c 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  al results of th
279e0 65 20 73 65 6c 65 63 74 2e 0a 20 20 20 20 2a 2f  e select..    */
279e1 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
279e2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
279e3 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20   }.#endif.  }.. 
279e4 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20   /* Jump to the 
279e5 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  end of the loop 
279e6 69 66 20 74 68 65 20 4c 49 4d 49 54 20 69 73 20  if the LIMIT is 
279e7 72 65 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 20  reached..  */.  
279e8 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b  if( p->iLimit ){
279e9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
279ea 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
279eb 49 6d 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20  Imm, p->iLimit, 
279ec 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  -1);.    sqlite3
279ed 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
279ee 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d  _IfZero, p->iLim
279ef 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d  it, iBreak);.  }
279f0 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
279f1 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 72  the subroutine r
279f2 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c  eturn.  */.  sql
279f3 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
279f4 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 69 6e 75  abel(v, iContinu
279f5 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  e);.  sqlite3Vdb
279f6 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
279f7 74 75 72 6e 2c 20 72 65 67 52 65 74 75 72 6e 29  turn, regReturn)
279f8 3b 0a 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72  ;..  return addr
279f9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 74 65 72  ;.}../*.** Alter
279fa 6e 61 74 69 76 65 20 63 6f 6d 70 6f 75 6e 64 20  native compound 
279fb 73 65 6c 65 63 74 20 63 6f 64 65 20 67 65 6e 65  select code gene
279fc 72 61 74 6f 72 20 66 6f 72 20 63 61 73 65 73 20  rator for cases 
279fd 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 69 73  when there.** is
279fe 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
279ff 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 73  use..**.** We as
27a00 73 75 6d 65 20 61 20 71 75 65 72 79 20 6f 66 20  sume a query of 
27a01 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
27a02 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 3c  rm:.**.**      <
27a03 73 65 6c 65 63 74 41 3e 20 20 3c 6f 70 65 72 61  selectA>  <opera
27a04 74 6f 72 3e 20 20 3c 73 65 6c 65 63 74 42 3e 20  tor>  <selectB> 
27a05 20 4f 52 44 45 52 20 42 59 20 3c 6f 72 64 65 72   ORDER BY <order
27a06 62 79 6c 69 73 74 3e 0a 2a 2a 0a 2a 2a 20 3c 6f  bylist>.**.** <o
27a07 70 65 72 61 74 6f 72 3e 20 69 73 20 6f 6e 65 20  perator> is one 
27a08 6f 66 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 55 4e  of UNION ALL, UN
27a09 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72 20  ION, EXCEPT, or 
27a0a 49 4e 54 45 52 53 45 43 54 2e 20 20 54 68 65 20  INTERSECT.  The 
27a0b 69 64 65 61 0a 2a 2a 20 69 73 20 74 6f 20 63 6f  idea.** is to co
27a0c 64 65 20 62 6f 74 68 20 3c 73 65 6c 65 63 74 41  de both <selectA
27a0d 3e 20 61 6e 64 20 3c 73 65 6c 65 63 74 42 3e 20  > and <selectB> 
27a0e 77 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42  with the ORDER B
27a0f 59 20 63 6c 61 75 73 65 20 61 73 0a 2a 2a 20 63  Y clause as.** c
27a10 6f 2d 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65  o-routines.  The
27a11 6e 20 72 75 6e 20 74 68 65 20 63 6f 2d 72 6f 75  n run the co-rou
27a12 74 69 6e 65 73 20 69 6e 20 70 61 72 61 6c 6c 65  tines in paralle
27a13 6c 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20  l and merge the 
27a14 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20  results.** into 
27a15 74 68 65 20 6f 75 74 70 75 74 2e 20 20 49 6e 20  the output.  In 
27a16 61 64 64 69 74 69 6f 6e 20 74 6f 20 74 68 65 20  addition to the 
27a17 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20 28  two coroutines (
27a18 63 61 6c 6c 65 64 20 73 65 6c 65 63 74 41 20 61  called selectA a
27a19 6e 64 0a 2a 2a 20 73 65 6c 65 63 74 42 29 20 74  nd.** selectB) t
27a1a 68 65 72 65 20 61 72 65 20 37 20 73 75 62 72 6f  here are 7 subro
27a1b 75 74 69 6e 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  utines:.**.**   
27a1c 20 6f 75 74 41 3a 20 20 20 20 4d 6f 76 65 20 74   outA:    Move t
27a1d 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65  he output of the
27a1e 20 73 65 6c 65 63 74 41 20 63 6f 72 6f 75 74 69   selectA corouti
27a1f 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70  ne into the outp
27a20 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
27a21 20 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e    of the compoun
27a22 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20  d query..**.**  
27a23 20 20 6f 75 74 42 3a 20 20 20 20 4d 6f 76 65 20    outB:    Move 
27a24 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68  the output of th
27a25 65 20 73 65 6c 65 63 74 42 20 63 6f 72 6f 75 74  e selectB corout
27a26 69 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74  ine into the out
27a27 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  put.**          
27a28 20 20 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75     of the compou
27a29 6e 64 20 71 75 65 72 79 2e 20 20 28 4f 6e 6c 79  nd query.  (Only
27a2a 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 55   generated for U
27a2b 4e 49 4f 4e 20 61 6e 64 0a 2a 2a 20 20 20 20 20  NION and.**     
27a2c 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c          UNION AL
27a2d 4c 2e 20 20 45 58 43 45 50 54 20 61 6e 64 20 49  L.  EXCEPT and I
27a2e 4e 53 45 52 54 53 45 43 54 20 6e 65 76 65 72 20  NSERTSECT never 
27a2f 6f 75 74 70 75 74 20 61 20 72 6f 77 20 74 68 61  output a row tha
27a30 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
27a31 20 61 70 70 65 61 72 73 20 6f 6e 6c 79 20 69 6e   appears only in
27a32 20 42 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 41 6c   B.).**.**    Al
27a33 74 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68  tB:    Called wh
27a34 65 6e 20 74 68 65 72 65 20 69 73 20 64 61 74 61  en there is data
27a35 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75   from both corou
27a36 74 69 6e 65 73 20 61 6e 64 20 41 3c 42 2e 0a 2a  tines and A<B..*
27a37 2a 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20 20 20  *.**    AeqB:   
27a38 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   Called when the
27a39 72 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20  re is data from 
27a3a 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20  both coroutines 
27a3b 61 6e 64 20 41 3d 3d 42 2e 0a 2a 2a 0a 2a 2a 20  and A==B..**.** 
27a3c 20 20 20 41 67 74 42 3a 20 20 20 20 43 61 6c 6c     AgtB:    Call
27a3d 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73  ed when there is
27a3e 20 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20   data from both 
27a3f 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41  coroutines and A
27a40 3e 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66  >B..**.**    Eof
27a41 41 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65  A:    Called whe
27a42 6e 20 64 61 74 61 20 69 73 20 65 78 68 61 75 73  n data is exhaus
27a43 74 65 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 41  ted from selectA
27a44 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 42 3a  ..**.**    EofB:
27a45 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20      Called when 
27a46 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74 65  data is exhauste
27a47 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 42 2e 0a  d from selectB..
27a48 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d  **.** The implem
27a49 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
27a4a 6c 61 74 74 65 72 20 66 69 76 65 20 73 75 62 72  latter five subr
27a4b 6f 75 74 69 6e 65 73 20 64 65 70 65 6e 64 20 6f  outines depend o
27a4c 6e 20 77 68 69 63 68 20 0a 2a 2a 20 3c 6f 70 65  n which .** <ope
27a4d 72 61 74 6f 72 3e 20 69 73 20 75 73 65 64 3a 0a  rator> is used:.
27a4e 2a 2a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  **.**.**        
27a4f 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 20 20       UNION ALL  
27a50 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 20 20 20         UNION    
27a51 20 20 20 20 20 20 20 20 45 58 43 45 50 54 20 20          EXCEPT  
27a52 20 20 20 20 20 20 20 20 49 4e 54 45 52 53 45 43          INTERSEC
27a53 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2d 2d  T.**          --
27a54 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d  -----------  ---
27a55 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
27a56 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
27a57 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27a58 2d 0a 2a 2a 20 20 20 41 6c 74 42 3a 20 20 20 6f  -.**   AltB:   o
27a59 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
27a5a 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
27a5b 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20    outA, nextA   
27a5c 20 20 20 20 20 20 6e 65 78 74 41 0a 2a 2a 0a 2a        nextA.**.*
27a5d 2a 20 20 20 41 65 71 42 3a 20 20 20 6f 75 74 41  *   AeqB:   outA
27a5e 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20  , nextA         
27a5f 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 20 20  nextA           
27a60 20 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20    nextA         
27a61 6f 75 74 41 2c 20 6e 65 78 74 41 0a 2a 2a 0a 2a  outA, nextA.**.*
27a62 2a 20 20 20 41 67 74 42 3a 20 20 20 6f 75 74 42  *   AgtB:   outB
27a63 2c 20 6e 65 78 74 42 20 20 20 20 20 20 6f 75 74  , nextB      out
27a64 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 20 20  B, nextB        
27a65 20 20 6e 65 78 74 42 20 20 20 20 20 20 20 20 20    nextB         
27a66 20 20 20 6e 65 78 74 42 0a 2a 2a 0a 2a 2a 20 20     nextB.**.**  
27a67 20 45 6f 66 41 3a 20 20 20 6f 75 74 42 2c 20 6e   EofA:   outB, n
27a68 65 78 74 42 20 20 20 20 20 20 6f 75 74 42 2c 20  extB      outB, 
27a69 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20 68  nextB          h
27a6a 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  alt             
27a6b 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66  halt.**.**   Eof
27a6c 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41  B:   outA, nextA
27a6d 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74        outA, next
27a6e 41 20 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65  A       outA, ne
27a6f 78 74 41 20 20 20 20 20 20 20 20 20 68 61 6c 74  xtA         halt
27a70 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 41 6c  .**.** In the Al
27a71 74 42 2c 20 41 65 71 42 2c 20 61 6e 64 20 41 67  tB, AeqB, and Ag
27a72 74 42 20 73 75 62 72 6f 75 74 69 6e 65 73 2c 20  tB subroutines, 
27a73 61 6e 20 45 4f 46 20 6f 6e 20 41 20 66 6f 6c 6c  an EOF on A foll
27a74 6f 77 69 6e 67 20 6e 65 78 74 41 0a 2a 2a 20 63  owing nextA.** c
27a75 61 75 73 65 73 20 61 6e 20 69 6d 6d 65 64 69 61  auses an immedia
27a76 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66 41 20  te jump to EofA 
27a77 61 6e 64 20 61 6e 20 45 4f 46 20 6f 6e 20 42 20  and an EOF on B 
27a78 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 42 20  following nextB 
27a79 63 61 75 73 65 73 0a 2a 2a 20 61 6e 20 69 6d 6d  causes.** an imm
27a7a 65 64 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45  ediate jump to E
27a7b 6f 66 42 2e 20 20 57 69 74 68 69 6e 20 45 6f 66  ofB.  Within Eof
27a7c 41 20 61 6e 64 20 45 6f 66 42 2c 20 61 6e 64 20  A and EofB, and 
27a7d 45 4f 46 20 6f 6e 20 65 6e 74 72 79 20 6f 72 0a  EOF on entry or.
27a7e 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78  ** following nex
27a7f 74 58 20 63 61 75 73 65 73 20 61 20 6a 75 6d 70  tX causes a jump
27a80 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
27a81 68 65 20 73 65 6c 65 63 74 20 70 72 6f 63 65 73  he select proces
27a82 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 44 75 70 6c  sing..**.** Dupl
27a83 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20 69 6e  icate removal in
27a84 20 74 68 65 20 55 4e 49 4f 4e 2c 20 45 58 43 45   the UNION, EXCE
27a85 50 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43  PT, and INTERSEC
27a86 54 20 63 61 73 65 73 20 69 73 20 68 61 6e 64 6c  T cases is handl
27a87 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  ed.** within the
27a88 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69   output subrouti
27a89 6e 65 2e 20 20 54 68 65 20 72 65 67 50 72 65 76  ne.  The regPrev
27a8a 20 72 65 67 69 73 74 65 72 20 73 65 74 20 68 6f   register set ho
27a8b 6c 64 73 20 74 68 65 20 70 72 65 76 69 6f 75 73  lds the previous
27a8c 6c 79 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 6c  ly.** output val
27a8d 75 65 2e 20 20 41 20 63 6f 6d 70 61 72 69 73 6f  ue.  A compariso
27a8e 6e 20 69 73 20 6d 61 64 65 20 61 67 61 69 6e 73  n is made agains
27a8f 74 20 74 68 69 73 20 76 61 6c 75 65 20 61 6e 64  t this value and
27a90 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 69   the output.** i
27a91 73 20 73 6b 69 70 70 65 64 20 69 66 20 74 68 65  s skipped if the
27a92 20 6e 65 78 74 20 72 65 73 75 6c 74 73 20 77 6f   next results wo
27a93 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d 65 20  uld be the same 
27a94 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e  as the previous.
27a95 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  .**.** The imple
27a96 6d 65 6e 74 61 74 69 6f 6e 20 70 6c 61 6e 20 69  mentation plan i
27a97 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  s to implement t
27a98 68 65 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65  he two coroutine
27a99 73 20 61 6e 64 20 73 65 76 65 6e 0a 2a 2a 20 73  s and seven.** s
27a9a 75 62 72 6f 75 74 69 6e 65 73 20 66 69 72 73 74  ubroutines first
27a9b 2c 20 74 68 65 6e 20 70 75 74 20 74 68 65 20 63  , then put the c
27a9c 6f 6e 74 72 6f 6c 20 6c 6f 67 69 63 20 61 74 20  ontrol logic at 
27a9d 74 68 65 20 62 6f 74 74 6f 6d 2e 20 20 4c 69 6b  the bottom.  Lik
27a9e 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
27a9f 20 20 20 20 20 20 20 67 6f 74 6f 20 49 6e 69 74         goto Init
27aa0 0a 2a 2a 20 20 20 20 20 63 6f 41 3a 20 63 6f 72  .**     coA: cor
27aa1 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20  outine for left 
27aa2 71 75 65 72 79 20 28 41 29 0a 2a 2a 20 20 20 20  query (A).**    
27aa3 20 63 6f 42 3a 20 63 6f 72 6f 75 74 69 6e 65 20   coB: coroutine 
27aa4 66 6f 72 20 72 69 67 68 74 20 71 75 65 72 79 20  for right query 
27aa5 28 42 29 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20  (B).**    outA: 
27aa6 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f  output one row o
27aa7 66 20 41 0a 2a 2a 20 20 20 20 6f 75 74 42 3a 20  f A.**    outB: 
27aa8 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f  output one row o
27aa9 66 20 42 20 28 55 4e 49 4f 4e 20 61 6e 64 20 55  f B (UNION and U
27aaa 4e 49 4f 4e 20 41 4c 4c 20 6f 6e 6c 79 29 0a 2a  NION ALL only).*
27aab 2a 20 20 20 20 45 6f 66 41 3a 20 2e 2e 2e 0a 2a  *    EofA: ....*
27aac 2a 20 20 20 20 45 6f 66 42 3a 20 2e 2e 2e 0a 2a  *    EofB: ....*
27aad 2a 20 20 20 20 41 6c 74 42 3a 20 2e 2e 2e 0a 2a  *    AltB: ....*
27aae 2a 20 20 20 20 41 65 71 42 3a 20 2e 2e 2e 0a 2a  *    AeqB: ....*
27aaf 2a 20 20 20 20 41 67 74 42 3a 20 2e 2e 2e 0a 2a  *    AgtB: ....*
27ab0 2a 20 20 20 20 49 6e 69 74 3a 20 69 6e 69 74 69  *    Init: initi
27ab1 61 6c 69 7a 65 20 63 6f 72 6f 75 74 69 6e 65 20  alize coroutine 
27ab2 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 20 20 20  registers.**    
27ab3 20 20 20 20 20 20 79 69 65 6c 64 20 63 6f 41 0a        yield coA.
27ab4 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 65  **          if e
27ab5 6f 66 28 41 29 20 67 6f 74 6f 20 45 6f 66 41 0a  of(A) goto EofA.
27ab6 2a 2a 20 20 20 20 20 20 20 20 20 20 79 69 65 6c  **          yiel
27ab7 64 20 63 6f 42 0a 2a 2a 20 20 20 20 20 20 20 20  d coB.**        
27ab8 20 20 69 66 20 65 6f 66 28 42 29 20 67 6f 74 6f    if eof(B) goto
27ab9 20 45 6f 66 42 0a 2a 2a 20 20 20 20 43 6d 70 72   EofB.**    Cmpr
27aba 3a 20 43 6f 6d 70 61 72 65 20 41 2c 20 42 0a 2a  : Compare A, B.*
27abb 2a 20 20 20 20 20 20 20 20 20 20 4a 75 6d 70 20  *          Jump 
27abc 41 6c 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42  AltB, AeqB, AgtB
27abd 0a 2a 2a 20 20 20 20 20 45 6e 64 3a 20 2e 2e 2e  .**     End: ...
27abe 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6c 6c 20 41  .**.** We call A
27abf 6c 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42 2c  ltB, AeqB, AgtB,
27ac0 20 45 6f 66 41 2c 20 61 6e 64 20 45 6f 66 42 20   EofA, and EofB 
27ac1 22 73 75 62 72 6f 75 74 69 6e 65 73 22 20 62 75  "subroutines" bu
27ac2 74 20 74 68 65 79 20 61 72 65 20 6e 6f 74 0a 2a  t they are not.*
27ac3 2a 20 61 63 74 75 61 6c 6c 79 20 63 61 6c 6c 65  * actually calle
27ac4 64 20 75 73 69 6e 67 20 47 6f 73 75 62 20 61 6e  d using Gosub an
27ac5 64 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 52 65  d they do not Re
27ac6 74 75 72 6e 2e 20 20 45 6f 66 41 20 61 6e 64 20  turn.  EofA and 
27ac7 45 6f 66 42 20 6c 6f 6f 70 0a 2a 2a 20 75 6e 74  EofB loop.** unt
27ac8 69 6c 20 61 6c 6c 20 64 61 74 61 20 69 73 20 65  il all data is e
27ac9 78 68 61 75 73 74 65 64 20 74 68 65 6e 20 6a 75  xhausted then ju
27aca 6d 70 20 74 6f 20 74 68 65 20 22 65 6e 64 22 20  mp to the "end" 
27acb 6c 61 62 65 2e 20 20 41 6c 74 42 2c 20 41 65 71  labe.  AltB, Aeq
27acc 42 2c 0a 2a 2a 20 61 6e 64 20 41 67 74 42 20 6a  B,.** and AgtB j
27acd 75 6d 70 20 74 6f 20 65 69 74 68 65 72 20 4c 32  ump to either L2
27ace 20 6f 72 20 74 6f 20 6f 6e 65 20 6f 66 20 45 6f   or to one of Eo
27acf 66 41 20 6f 72 20 45 6f 66 42 2e 0a 2a 2f 0a 23  fA or EofB..*/.#
27ad0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27ad1 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
27ad2 43 54 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75  CT.static int mu
27ad3 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79  ltiSelectOrderBy
27ad4 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
27ad5 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
27ad6 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
27ad7 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20    Select *p,    
27ad8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
27ad9 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c  ight-most of SEL
27ada 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64  ECTs to be coded
27adb 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
27adc 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57   *pDest     /* W
27add 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71  hat to do with q
27ade 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a  uery results */.
27adf 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  ){.  int i, j;  
27ae0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
27ae1 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
27ae2 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b   Select *pPrior;
27ae3 20 20 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65         /* Anothe
27ae4 72 20 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61  r SELECT immedia
27ae5 74 65 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74  tely to our left
27ae6 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
27ae7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
27ae8 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
27ae9 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53  this VDBE */.  S
27aea 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 41 3b  electDest destA;
27aeb 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74       /* Destinat
27aec 69 6f 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e  ion for coroutin
27aed 65 20 41 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  e A */.  SelectD
27aee 65 73 74 20 64 65 73 74 42 3b 20 20 20 20 20 2f  est destB;     /
27aef 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f  * Destination fo
27af0 72 20 63 6f 72 6f 75 74 69 6e 65 20 42 20 2a 2f  r coroutine B */
27af1 0a 20 20 69 6e 74 20 72 65 67 41 64 64 72 41 3b  .  int regAddrA;
27af2 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
27af3 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72  ess register for
27af4 20 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f 75 74   select-A corout
27af5 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ine */.  int reg
27af6 45 6f 66 41 3b 20 20 20 20 20 20 20 20 20 20 2f  EofA;          /
27af7 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61  * Flag to indica
27af8 74 65 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 41  te when select-A
27af9 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a   is complete */.
27afa 20 20 69 6e 74 20 72 65 67 41 64 64 72 42 3b 20    int regAddrB; 
27afb 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
27afc 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
27afd 73 65 6c 65 63 74 2d 42 20 63 6f 72 6f 75 74 69  select-B corouti
27afe 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45  ne */.  int regE
27aff 6f 66 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ofB;          /*
27b00 20 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74   Flag to indicat
27b01 65 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 42 20  e when select-B 
27b02 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a 20  is complete */. 
27b03 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 41   int addrSelectA
27b04 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  ;      /* Addres
27b05 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d  s of the select-
27b06 41 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20  A coroutine */. 
27b07 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 42   int addrSelectB
27b08 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  ;      /* Addres
27b09 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d  s of the select-
27b0a 42 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20  B coroutine */. 
27b0b 20 69 6e 74 20 72 65 67 4f 75 74 41 3b 20 20 20   int regOutA;   
27b0c 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
27b0d 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74  s register for t
27b0e 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72  he output-A subr
27b0f 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
27b10 72 65 67 4f 75 74 42 3b 20 20 20 20 20 20 20 20  regOutB;        
27b11 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67    /* Address reg
27b12 69 73 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75  ister for the ou
27b13 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74 69 6e  tput-B subroutin
27b14 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f  e */.  int addrO
27b15 75 74 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  utA;         /* 
27b16 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6f  Address of the o
27b17 75 74 70 75 74 2d 41 20 73 75 62 72 6f 75 74 69  utput-A subrouti
27b18 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
27b19 4f 75 74 42 20 3d 20 30 3b 20 20 20 20 20 2f 2a  OutB = 0;     /*
27b1a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
27b1b 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74  output-B subrout
27b1c 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
27b1d 72 45 6f 66 41 3b 20 20 20 20 20 20 20 20 20 2f  rEofA;         /
27b1e 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
27b1f 20 73 65 6c 65 63 74 2d 41 2d 65 78 68 61 75 73   select-A-exhaus
27b20 74 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  ted subroutine *
27b21 2f 0a 20 20 69 6e 74 20 61 64 64 72 45 6f 66 42  /.  int addrEofB
27b22 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64  ;         /* Add
27b23 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65  ress of the sele
27b24 63 74 2d 42 2d 65 78 68 61 75 73 74 65 64 20 73  ct-B-exhausted s
27b25 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
27b26 6e 74 20 61 64 64 72 41 6c 74 42 3b 20 20 20 20  nt addrAltB;    
27b27 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
27b28 6f 66 20 74 68 65 20 41 3c 42 20 73 75 62 72 6f  of the A<B subro
27b29 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
27b2a 64 64 72 41 65 71 42 3b 20 20 20 20 20 20 20 20  ddrAeqB;        
27b2b 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
27b2c 68 65 20 41 3d 3d 42 20 73 75 62 72 6f 75 74 69  he A==B subrouti
27b2d 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
27b2e 41 67 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  AgtB;         /*
27b2f 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
27b30 41 3e 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  A>B subroutine *
27b31 2f 0a 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74  /.  int regLimit
27b32 41 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d  A;        /* Lim
27b33 69 74 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  it register for 
27b34 73 65 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e  select-A */.  in
27b35 74 20 72 65 67 4c 69 6d 69 74 42 3b 20 20 20 20  t regLimitB;    
27b36 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67      /* Limit reg
27b37 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74  ister for select
27b38 2d 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 50  -A */.  int regP
27b39 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  rev;          /*
27b3a 20 41 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69   A range of regi
27b3b 73 74 65 72 73 20 74 6f 20 68 6f 6c 64 20 70 72  sters to hold pr
27b3c 65 76 69 6f 75 73 20 6f 75 74 70 75 74 20 2a 2f  evious output */
27b3d 0a 20 20 69 6e 74 20 73 61 76 65 64 4c 69 6d 69  .  int savedLimi
27b3e 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65  t;       /* Save
27b3f 64 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4c  d value of p->iL
27b40 69 6d 69 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61  imit */.  int sa
27b41 76 65 64 4f 66 66 73 65 74 3b 20 20 20 20 20 20  vedOffset;      
27b42 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f  /* Saved value o
27b43 66 20 70 2d 3e 69 4f 66 66 73 65 74 20 2a 2f 0a  f p->iOffset */.
27b44 20 20 69 6e 74 20 6c 61 62 65 6c 43 6d 70 72 3b    int labelCmpr;
27b45 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c          /* Label
27b46 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f   for the start o
27b47 66 20 74 68 65 20 6d 65 72 67 65 20 61 6c 67 6f  f the merge algo
27b48 72 69 74 68 6d 20 2a 2f 0a 20 20 69 6e 74 20 6c  rithm */.  int l
27b49 61 62 65 6c 45 6e 64 3b 20 20 20 20 20 20 20 20  abelEnd;        
27b4a 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68   /* Label for th
27b4b 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 76 65  e end of the ove
27b4c 72 61 6c 6c 20 53 45 4c 45 43 54 20 73 74 6d 74  rall SELECT stmt
27b4d 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20   */.  int j1;   
27b4e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a              /* J
27b4f 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ump instructions
27b50 20 74 68 61 74 20 67 65 74 20 72 65 74 61 72 67   that get retarg
27b51 65 74 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  etted */.  int o
27b52 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
27b53 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 41 4c   /* One of TK_AL
27b54 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54 4b 5f  L, TK_UNION, TK_
27b55 45 58 43 45 50 54 2c 20 54 4b 5f 49 4e 54 45 52  EXCEPT, TK_INTER
27b56 53 45 43 54 20 2a 2f 0a 20 20 4b 65 79 49 6e 66  SECT */.  KeyInf
27b57 6f 20 2a 70 4b 65 79 44 75 70 20 3d 20 30 3b 20  o *pKeyDup = 0; 
27b58 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e  /* Comparison in
27b59 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 64 75  formation for du
27b5a 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20  plicate removal 
27b5b 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  */.  KeyInfo *pK
27b5c 65 79 4d 65 72 67 65 3b 20 20 20 2f 2a 20 43 6f  eyMerge;   /* Co
27b5d 6d 70 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d 61  mparison informa
27b5e 74 69 6f 6e 20 66 6f 72 20 6d 65 72 67 69 6e 67  tion for merging
27b5f 20 72 6f 77 73 20 2a 2f 0a 20 20 73 71 6c 69 74   rows */.  sqlit
27b60 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
27b61 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
27b62 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  nection */.  Exp
27b63 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b  rList *pOrderBy;
27b64 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
27b65 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  BY clause */.  i
27b66 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20  nt nOrderBy;    
27b67 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
27b68 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f  f terms in the O
27b69 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
27b6a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74  /.  int *aPermut
27b6b 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70  e;        /* Map
27b6c 70 69 6e 67 20 66 72 6f 6d 20 4f 52 44 45 52 20  ping from ORDER 
27b6d 42 59 20 74 65 72 6d 73 20 74 6f 20 72 65 73 75  BY terms to resu
27b6e 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a  lt set columns *
27b6f 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  /..  assert( p->
27b70 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b 0a 20  pOrderBy!=0 );. 
27b71 20 61 73 73 65 72 74 28 20 70 4b 65 79 44 75 70   assert( pKeyDup
27b72 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d 61 6e 61 67  ==0 ); /* "Manag
27b73 65 64 22 20 63 6f 64 65 20 6e 65 65 64 73 20 74  ed" code needs t
27b74 68 69 73 2e 20 20 54 69 63 6b 65 74 20 23 33 33  his.  Ticket #33
27b75 38 32 2e 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50  82. */.  db = pP
27b76 61 72 73 65 2d 3e 64 62 3b 0a 20 20 76 20 3d 20  arse->db;.  v = 
27b77 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
27b78 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
27b79 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
27b7a 0a 20 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73 71  .  labelEnd = sq
27b7b 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
27b7c 65 6c 28 76 29 3b 0a 20 20 6c 61 62 65 6c 43 6d  el(v);.  labelCm
27b7d 70 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  pr = sqlite3Vdbe
27b7e 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 0a  MakeLabel(v);...
27b7f 20 20 2f 2a 20 50 61 74 63 68 20 75 70 20 74 68    /* Patch up th
27b80 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
27b81 65 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 2d  e.  */.  op = p-
27b82 3e 6f 70 3b 20 20 0a 20 20 70 50 72 69 6f 72 20  >op;  .  pPrior 
27b83 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61  = p->pPrior;.  a
27b84 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70  ssert( pPrior->p
27b85 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20  OrderBy==0 );.  
27b86 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f  pOrderBy = p->pO
27b87 72 64 65 72 42 79 3b 0a 20 20 61 73 73 65 72 74  rderBy;.  assert
27b88 28 20 70 4f 72 64 65 72 42 79 20 29 3b 0a 20 20  ( pOrderBy );.  
27b89 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65  nOrderBy = pOrde
27b8a 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20 2f  rBy->nExpr;..  /
27b8b 2a 20 46 6f 72 20 6f 70 65 72 61 74 6f 72 73 20  * For operators 
27b8c 6f 74 68 65 72 20 74 68 61 6e 20 55 4e 49 4f 4e  other than UNION
27b8d 20 41 4c 4c 20 77 65 20 68 61 76 65 20 74 6f 20   ALL we have to 
27b8e 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20  make sure that. 
27b8f 20 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59   ** the ORDER BY
27b90 20 63 6c 61 75 73 65 20 63 6f 76 65 72 73 20 65   clause covers e
27b91 76 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65  very term of the
27b92 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 41 64   result set.  Ad
27b93 64 0a 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f 20  d.  ** terms to 
27b94 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
27b95 75 73 65 20 61 73 20 6e 65 63 65 73 73 61 72 79  use as necessary
27b96 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 21  ..  */.  if( op!
27b97 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 66  =TK_ALL ){.    f
27b98 6f 72 28 69 3d 31 3b 20 64 62 2d 3e 6d 61 6c 6c  or(i=1; db->mall
27b99 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26 26 20 69  ocFailed==0 && i
27b9a 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  <=p->pEList->nEx
27b9b 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
27b9c 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
27b9d 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
27b9e 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74 65     for(j=0, pIte
27b9f 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a  m=pOrderBy->a; j
27ba0 3c 6e 4f 72 64 65 72 42 79 3b 20 6a 2b 2b 2c 20  <nOrderBy; j++, 
27ba1 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
27ba2 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d    assert( pItem-
27ba3 3e 69 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20  >iCol>0 );.     
27ba4 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43     if( pItem->iC
27ba5 6f 6c 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20  ol==i ) break;. 
27ba6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
27ba7 20 6a 3d 3d 6e 4f 72 64 65 72 42 79 20 29 7b 0a   j==nOrderBy ){.
27ba8 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e          Expr *pN
27ba9 65 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ew = sqlite3PExp
27baa 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54  r(pParse, TK_INT
27bab 45 47 45 52 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  EGER, 0, 0, 0);.
27bac 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77          if( pNew
27bad 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
27bae 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
27baf 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c     pNew->flags |
27bb0 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20  = EP_IntValue;. 
27bb1 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 54 61         pNew->iTa
27bb2 62 6c 65 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  ble = i;.       
27bb3 20 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69   pOrderBy = sqli
27bb4 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
27bb5 64 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  d(pParse, pOrder
27bb6 42 79 2c 20 70 4e 65 77 2c 20 30 29 3b 0a 20 20  By, pNew, 0);.  
27bb7 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e        pOrderBy->
27bb8 61 5b 6e 4f 72 64 65 72 42 79 2b 2b 5d 2e 69 43  a[nOrderBy++].iC
27bb9 6f 6c 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 20  ol = (u16)i;.   
27bba 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
27bbb 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65    /* Compute the
27bbc 20 63 6f 6d 70 61 72 69 73 6f 6e 20 70 65 72 6d   comparison perm
27bbd 75 74 61 74 69 6f 6e 20 61 6e 64 20 6b 65 79 69  utation and keyi
27bbe 6e 66 6f 20 74 68 61 74 20 69 73 20 75 73 65 64  nfo that is used
27bbf 20 77 69 74 68 0a 20 20 2a 2a 20 74 68 65 20 70   with.  ** the p
27bc0 65 72 6d 75 74 61 74 69 6f 6e 20 69 6e 20 6f 72  ermutation in or
27bc1 64 65 72 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f  der to compariso
27bc2 6e 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ns to determine 
27bc3 69 66 20 74 68 65 20 6e 65 78 74 0a 20 20 2a 2a  if the next.  **
27bc4 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 73 20   row of results 
27bc5 63 6f 6d 65 73 20 66 72 6f 6d 20 73 65 6c 65 63  comes from selec
27bc6 74 41 20 6f 72 20 73 65 6c 65 63 74 42 2e 20 20  tA or selectB.  
27bc7 41 6c 73 6f 20 61 64 64 20 65 78 70 6c 69 63 69  Also add explici
27bc8 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e  t.  ** collation
27bc9 73 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42  s to the ORDER B
27bca 59 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 73  Y clause terms s
27bcb 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20  o that when the 
27bcc 73 75 62 71 75 65 72 69 65 73 0a 20 20 2a 2a 20  subqueries.  ** 
27bcd 74 6f 20 74 68 65 20 72 69 67 68 74 20 61 6e 64  to the right and
27bce 20 74 68 65 20 6c 65 66 74 20 61 72 65 20 65 76   the left are ev
27bcf 61 6c 75 61 74 65 64 2c 20 74 68 65 79 20 75 73  aluated, they us
27bd0 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 20 20  e the correct.  
27bd1 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 20 20  ** collation..  
27bd2 2a 2f 0a 20 20 61 50 65 72 6d 75 74 65 20 3d 20  */.  aPermute = 
27bd3 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
27bd4 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e  aw(db, sizeof(in
27bd5 74 29 2a 6e 4f 72 64 65 72 42 79 29 3b 0a 20 20  t)*nOrderBy);.  
27bd6 69 66 28 20 61 50 65 72 6d 75 74 65 20 29 7b 0a  if( aPermute ){.
27bd7 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
27bd8 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
27bd9 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  .    for(i=0, pI
27bda 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b  tem=pOrderBy->a;
27bdb 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b   i<nOrderBy; i++
27bdc 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
27bdd 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d    assert( pItem-
27bde 3e 69 43 6f 6c 3e 30 20 20 26 26 20 70 49 74 65  >iCol>0  && pIte
27bdf 6d 2d 3e 69 43 6f 6c 3c 3d 70 2d 3e 70 45 4c 69  m->iCol<=p->pELi
27be0 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20  st->nExpr );.   
27be1 20 20 20 61 50 65 72 6d 75 74 65 5b 69 5d 20 3d     aPermute[i] =
27be2 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 2d 20 31   pItem->iCol - 1
27be3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79  ;.    }.    pKey
27be4 4d 65 72 67 65 20 3d 0a 20 20 20 20 20 20 73 71  Merge =.      sq
27be5 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
27be6 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4b 65  (db, sizeof(*pKe
27be7 79 4d 65 72 67 65 29 2b 6e 4f 72 64 65 72 42 79  yMerge)+nOrderBy
27be8 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  *(sizeof(CollSeq
27be9 2a 29 2b 31 29 29 3b 0a 20 20 20 20 69 66 28 20  *)+1));.    if( 
27bea 70 4b 65 79 4d 65 72 67 65 20 29 7b 0a 20 20 20  pKeyMerge ){.   
27beb 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53     pKeyMerge->aS
27bec 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29  ortOrder = (u8*)
27bed 26 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c  &pKeyMerge->aCol
27bee 6c 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20 20  l[nOrderBy];.   
27bef 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 6e 46     pKeyMerge->nF
27bf0 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 4f 72 64  ield = (u16)nOrd
27bf1 65 72 42 79 3b 0a 20 20 20 20 20 20 70 4b 65 79  erBy;.      pKey
27bf2 4d 65 72 67 65 2d 3e 65 6e 63 20 3d 20 45 4e 43  Merge->enc = ENC
27bf3 28 64 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  (db);.      for(
27bf4 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b  i=0; i<nOrderBy;
27bf5 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 43   i++){.        C
27bf6 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
27bf7 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 54 65         Expr *pTe
27bf8 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  rm = pOrderBy->a
27bf9 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
27bfa 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 66 6c     if( pTerm->fl
27bfb 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
27bfc 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ate ){.         
27bfd 20 70 43 6f 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e   pColl = pTerm->
27bfe 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d  pColl;.        }
27bff 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
27c00 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c  pColl = multiSel
27c01 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ectCollSeq(pPars
27c02 65 2c 20 70 2c 20 61 50 65 72 6d 75 74 65 5b 69  e, p, aPermute[i
27c03 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54  ]);.          pT
27c04 65 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  erm->flags |= EP
27c05 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20  _ExpCollate;.   
27c06 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 43         pTerm->pC
27c07 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20  oll = pColl;.   
27c08 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
27c09 4b 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b  KeyMerge->aColl[
27c0a 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  i] = pColl;.    
27c0b 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61      pKeyMerge->a
27c0c 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70  SortOrder[i] = p
27c0d 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f  OrderBy->a[i].so
27c0e 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 7d  rtOrder;.      }
27c0f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
27c10 20 20 20 20 70 4b 65 79 4d 65 72 67 65 20 3d 20      pKeyMerge = 
27c11 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61  0;.  }..  /* Rea
27c12 74 74 61 63 68 20 74 68 65 20 4f 52 44 45 52 20  ttach the ORDER 
27c13 42 59 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65  BY clause to the
27c14 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 70   query..  */.  p
27c15 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72  ->pOrderBy = pOr
27c16 64 65 72 42 79 3b 0a 20 20 70 50 72 69 6f 72 2d  derBy;.  pPrior-
27c17 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69  >pOrderBy = sqli
27c18 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 70  te3ExprListDup(p
27c19 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65  Parse->db, pOrde
27c1a 72 42 79 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 41  rBy, 0);..  /* A
27c1b 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e 67 65 20  llocate a range 
27c1c 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  of temporary reg
27c1d 69 73 74 65 72 73 20 61 6e 64 20 74 68 65 20 4b  isters and the K
27c1e 65 79 49 6e 66 6f 20 6e 65 65 64 65 64 0a 20 20  eyInfo needed.  
27c1f 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f 67 69 63  ** for the logic
27c20 20 74 68 61 74 20 72 65 6d 6f 76 65 73 20 64 75   that removes du
27c21 70 6c 69 63 61 74 65 20 72 65 73 75 6c 74 20 72  plicate result r
27c22 6f 77 73 20 77 68 65 6e 20 74 68 65 0a 20 20 2a  ows when the.  *
27c23 2a 20 6f 70 65 72 61 74 6f 72 20 69 73 20 55 4e  * operator is UN
27c24 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72 20  ION, EXCEPT, or 
27c25 49 4e 54 45 52 53 45 43 54 20 28 62 75 74 20 6e  INTERSECT (but n
27c26 6f 74 20 55 4e 49 4f 4e 20 41 4c 4c 29 2e 0a 20  ot UNION ALL).. 
27c27 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b   */.  if( op==TK
27c28 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 67 50  _ALL ){.    regP
27c29 72 65 76 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  rev = 0;.  }else
27c2a 7b 0a 20 20 20 20 69 6e 74 20 6e 45 78 70 72 20  {.    int nExpr 
27c2b 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  = p->pEList->nEx
27c2c 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  pr;.    assert( 
27c2d 6e 4f 72 64 65 72 42 79 3e 3d 6e 45 78 70 72 20  nOrderBy>=nExpr 
27c2e 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
27c2f 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 67 50 72  led );.    regPr
27c30 65 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ev = sqlite3GetT
27c31 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
27c32 20 6e 45 78 70 72 2b 31 29 3b 0a 20 20 20 20 73   nExpr+1);.    s
27c33 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27c34 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
27c35 30 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20 20  0, regPrev);.   
27c36 20 70 4b 65 79 44 75 70 20 3d 20 73 71 6c 69 74   pKeyDup = sqlit
27c37 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
27c38 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
27c39 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4b 65       sizeof(*pKe
27c3a 79 44 75 70 29 20 2b 20 6e 45 78 70 72 2a 28 73  yDup) + nExpr*(s
27c3b 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b  izeof(CollSeq*)+
27c3c 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4b  1) );.    if( pK
27c3d 65 79 44 75 70 20 29 7b 0a 20 20 20 20 20 20 70  eyDup ){.      p
27c3e 4b 65 79 44 75 70 2d 3e 61 53 6f 72 74 4f 72 64  KeyDup->aSortOrd
27c3f 65 72 20 3d 20 28 75 38 2a 29 26 70 4b 65 79 44  er = (u8*)&pKeyD
27c40 75 70 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d  up->aColl[nExpr]
27c41 3b 0a 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d  ;.      pKeyDup-
27c42 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e  >nField = (u16)n
27c43 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4b 65 79  Expr;.      pKey
27c44 44 75 70 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64  Dup->enc = ENC(d
27c45 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  b);.      for(i=
27c46 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 29  0; i<nExpr; i++)
27c47 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 44 75  {.        pKeyDu
27c48 70 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75  p->aColl[i] = mu
27c49 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71  ltiSelectCollSeq
27c4a 28 70 50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a  (pParse, p, i);.
27c4b 20 20 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d          pKeyDup-
27c4c 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d  >aSortOrder[i] =
27c4d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
27c4e 7d 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 53 65 70  }.  }. .  /* Sep
27c4f 61 72 61 74 65 20 74 68 65 20 6c 65 66 74 20 61  arate the left a
27c50 6e 64 20 74 68 65 20 72 69 67 68 74 20 71 75 65  nd the right que
27c51 72 79 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74  ry from one anot
27c52 68 65 72 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 50  her.  */.  p->pP
27c53 72 69 6f 72 20 3d 20 30 3b 0a 20 20 70 50 72 69  rior = 0;.  pPri
27c54 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d  or->pRightmost =
27c55 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73   0;.  sqlite3Res
27c56 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
27c57 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70  (pParse, p, p->p
27c58 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22  OrderBy, "ORDER"
27c59 29 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d  );.  if( pPrior-
27c5a 3e 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20  >pPrior==0 ){.  
27c5b 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65    sqlite3Resolve
27c5c 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61  OrderGroupBy(pPa
27c5d 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 70 50 72  rse, pPrior, pPr
27c5e 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22  ior->pOrderBy, "
27c5f 4f 52 44 45 52 22 29 3b 0a 20 20 7d 0a 0a 20 20  ORDER");.  }..  
27c60 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6c  /* Compute the l
27c61 69 6d 69 74 20 72 65 67 69 73 74 65 72 73 20 2a  imit registers *
27c62 2f 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74  /.  computeLimit
27c63 52 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65  Registers(pParse
27c64 2c 20 70 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a  , p, labelEnd);.
27c65 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20    if( p->iLimit 
27c66 26 26 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b  && op==TK_ALL ){
27c67 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 41 20 3d  .    regLimitA =
27c68 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
27c69 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 42 20 3d  .    regLimitB =
27c6a 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
27c6b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27c6c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70  AddOp2(v, OP_Cop
27c6d 79 2c 20 70 2d 3e 69 4f 66 66 73 65 74 20 3f 20  y, p->iOffset ? 
27c6e 70 2d 3e 69 4f 66 66 73 65 74 2b 31 20 3a 20 70  p->iOffset+1 : p
27c6f 2d 3e 69 4c 69 6d 69 74 2c 0a 20 20 20 20 20 20  ->iLimit,.      
27c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27c71 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67 4c              regL
27c72 69 6d 69 74 41 29 3b 0a 20 20 20 20 73 71 6c 69  imitA);.    sqli
27c73 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27c74 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4c 69 6d   OP_Copy, regLim
27c75 69 74 41 2c 20 72 65 67 4c 69 6d 69 74 42 29 3b  itA, regLimitB);
27c76 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
27c77 67 4c 69 6d 69 74 41 20 3d 20 72 65 67 4c 69 6d  gLimitA = regLim
27c78 69 74 42 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73  itB = 0;.  }.  s
27c79 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
27c7a 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b  (db, p->pLimit);
27c7b 0a 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30  .  p->pLimit = 0
27c7c 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
27c7d 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66  elete(db, p->pOf
27c7e 66 73 65 74 29 3b 0a 20 20 70 2d 3e 70 4f 66 66  fset);.  p->pOff
27c7f 73 65 74 20 3d 20 30 3b 0a 0a 20 20 72 65 67 41  set = 0;..  regA
27c80 64 64 72 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d  ddrA = ++pParse-
27c81 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 45 6f 66 41  >nMem;.  regEofA
27c82 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
27c83 6d 3b 0a 20 20 72 65 67 41 64 64 72 42 20 3d 20  m;.  regAddrB = 
27c84 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
27c85 20 20 72 65 67 45 6f 66 42 20 3d 20 2b 2b 70 50    regEofB = ++pP
27c86 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65  arse->nMem;.  re
27c87 67 4f 75 74 41 20 3d 20 2b 2b 70 50 61 72 73 65  gOutA = ++pParse
27c88 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74  ->nMem;.  regOut
27c89 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  B = ++pParse->nM
27c8a 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  em;.  sqlite3Sel
27c8b 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73  ectDestInit(&des
27c8c 74 41 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e  tA, SRT_Coroutin
27c8d 65 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20  e, regAddrA);.  
27c8e 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
27c8f 74 49 6e 69 74 28 26 64 65 73 74 42 2c 20 53 52  tInit(&destB, SR
27c90 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67  T_Coroutine, reg
27c91 41 64 64 72 42 29 3b 0a 0a 20 20 2f 2a 20 4a 75  AddrB);..  /* Ju
27c92 6d 70 20 70 61 73 74 20 74 68 65 20 76 61 72 69  mp past the vari
27c93 6f 75 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20  ous subroutines 
27c94 61 6e 64 20 63 6f 72 6f 75 74 69 6e 65 73 20 74  and coroutines t
27c95 6f 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20  o the main.  ** 
27c96 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a  merge loop.  */.
27c97 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
27c98 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
27c99 6f 74 6f 29 3b 0a 20 20 61 64 64 72 53 65 6c 65  oto);.  addrSele
27c9a 63 74 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ctA = sqlite3Vdb
27c9b 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
27c9c 0a 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ...  /* Generate
27c9d 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20   a coroutine to 
27c9e 65 76 61 6c 75 61 74 65 20 74 68 65 20 53 45 4c  evaluate the SEL
27c9f 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
27ca0 20 74 68 65 0a 20 20 2a 2a 20 6c 65 66 74 20 6f   the.  ** left o
27ca1 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 6f  f the compound o
27ca2 70 65 72 61 74 6f 72 20 2d 20 74 68 65 20 22 41  perator - the "A
27ca3 22 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20  " select..  */. 
27ca4 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
27ca5 28 28 76 2c 20 22 42 65 67 69 6e 20 63 6f 72 6f  ((v, "Begin coro
27ca6 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 53  utine for left S
27ca7 45 4c 45 43 54 22 29 29 3b 0a 20 20 70 50 72 69  ELECT"));.  pPri
27ca8 6f 72 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67  or->iLimit = reg
27ca9 4c 69 6d 69 74 41 3b 0a 20 20 73 71 6c 69 74 65  LimitA;.  sqlite
27caa 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
27cab 70 50 72 69 6f 72 2c 20 26 64 65 73 74 41 29 3b  pPrior, &destA);
27cac 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27cad 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
27cae 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 41 29 3b  er, 1, regEofA);
27caf 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27cb0 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
27cb1 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 56  , regAddrA);.  V
27cb2 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
27cb3 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e  v, "End coroutin
27cb4 65 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45 43  e for left SELEC
27cb5 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65  T"));..  /* Gene
27cb6 72 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65  rate a coroutine
27cb7 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65   to evaluate the
27cb8 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
27cb9 74 20 6f 6e 20 0a 20 20 2a 2a 20 74 68 65 20 72  t on .  ** the r
27cba 69 67 68 74 20 2d 20 74 68 65 20 22 42 22 20 73  ight - the "B" s
27cbb 65 6c 65 63 74 0a 20 20 2a 2f 0a 20 20 61 64 64  elect.  */.  add
27cbc 72 53 65 6c 65 63 74 42 20 3d 20 73 71 6c 69 74  rSelectB = sqlit
27cbd 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
27cbe 72 28 76 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70  r(v);.  VdbeNoop
27cbf 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65 67  Comment((v, "Beg
27cc0 69 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72  in coroutine for
27cc1 20 72 69 67 68 74 20 53 45 4c 45 43 54 22 29 29   right SELECT"))
27cc2 3b 0a 20 20 73 61 76 65 64 4c 69 6d 69 74 20 3d  ;.  savedLimit =
27cc3 20 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 73 61   p->iLimit;.  sa
27cc4 76 65 64 4f 66 66 73 65 74 20 3d 20 70 2d 3e 69  vedOffset = p->i
27cc5 4f 66 66 73 65 74 3b 0a 20 20 70 2d 3e 69 4c 69  Offset;.  p->iLi
27cc6 6d 69 74 20 3d 20 72 65 67 4c 69 6d 69 74 42 3b  mit = regLimitB;
27cc7 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20  .  p->iOffset = 
27cc8 30 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 53 65  0;  .  sqlite3Se
27cc9 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20  lect(pParse, p, 
27cca 26 64 65 73 74 42 29 3b 0a 20 20 70 2d 3e 69 4c  &destB);.  p->iL
27ccb 69 6d 69 74 20 3d 20 73 61 76 65 64 4c 69 6d 69  imit = savedLimi
27ccc 74 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20  t;.  p->iOffset 
27ccd 3d 20 73 61 76 65 64 4f 66 66 73 65 74 3b 0a 20  = savedOffset;. 
27cce 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27ccf 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
27cd0 2c 20 31 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20  , 1, regEofB);. 
27cd1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27cd2 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
27cd3 72 65 67 41 64 64 72 42 29 3b 0a 20 20 56 64 62  regAddrB);.  Vdb
27cd4 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
27cd5 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20   "End coroutine 
27cd6 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43 54  for right SELECT
27cd7 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  "));..  /* Gener
27cd8 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65  ate a subroutine
27cd9 20 74 68 61 74 20 6f 75 74 70 75 74 73 20 74 68   that outputs th
27cda 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
27cdb 20 74 68 65 20 41 0a 20 20 2a 2a 20 73 65 6c 65   the A.  ** sele
27cdc 63 74 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f  ct as the next o
27cdd 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65  utput row of the
27cde 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
27cdf 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f  ..  */.  VdbeNoo
27ce0 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 75  pComment((v, "Ou
27ce1 74 70 75 74 20 72 6f 75 74 69 6e 65 20 66 6f 72  tput routine for
27ce2 20 41 22 29 29 3b 0a 20 20 61 64 64 72 4f 75 74   A"));.  addrOut
27ce3 41 20 3d 20 67 65 6e 65 72 61 74 65 4f 75 74 70  A = generateOutp
27ce4 75 74 53 75 62 72 6f 75 74 69 6e 65 28 70 50 61  utSubroutine(pPa
27ce5 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rse,.           
27ce6 20 20 20 20 20 20 70 2c 20 26 64 65 73 74 41 2c        p, &destA,
27ce7 20 70 44 65 73 74 2c 20 72 65 67 4f 75 74 41 2c   pDest, regOutA,
27ce8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
27ce9 20 20 72 65 67 50 72 65 76 2c 20 70 4b 65 79 44    regPrev, pKeyD
27cea 75 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  up, P4_KEYINFO_H
27ceb 41 4e 44 4f 46 46 2c 20 6c 61 62 65 6c 45 6e 64  ANDOFF, labelEnd
27cec 29 3b 0a 20 20 0a 20 20 2f 2a 20 47 65 6e 65 72  );.  .  /* Gener
27ced 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65  ate a subroutine
27cee 20 74 68 61 74 20 6f 75 74 70 75 74 73 20 74 68   that outputs th
27cef 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
27cf0 20 74 68 65 20 42 0a 20 20 2a 2a 20 73 65 6c 65   the B.  ** sele
27cf1 63 74 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f  ct as the next o
27cf2 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65  utput row of the
27cf3 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
27cf4 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d  ..  */.  if( op=
27cf5 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54  =TK_ALL || op==T
27cf6 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20 20 56  K_UNION ){.    V
27cf7 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
27cf8 76 2c 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69  v, "Output routi
27cf9 6e 65 20 66 6f 72 20 42 22 29 29 3b 0a 20 20 20  ne for B"));.   
27cfa 20 61 64 64 72 4f 75 74 42 20 3d 20 67 65 6e 65   addrOutB = gene
27cfb 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75  rateOutputSubrou
27cfc 74 69 6e 65 28 70 50 61 72 73 65 2c 0a 20 20 20  tine(pParse,.   
27cfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2c                p,
27cfe 20 26 64 65 73 74 42 2c 20 70 44 65 73 74 2c 20   &destB, pDest, 
27cff 72 65 67 4f 75 74 42 2c 0a 20 20 20 20 20 20 20  regOutB,.       
27d00 20 20 20 20 20 20 20 20 20 20 72 65 67 50 72 65            regPre
27d01 76 2c 20 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b  v, pKeyDup, P4_K
27d02 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c 20 6c  EYINFO_STATIC, l
27d03 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20  abelEnd);.  }.. 
27d04 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
27d05 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e  ubroutine to run
27d06 20 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   when the result
27d07 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 41 0a  s from select A.
27d08 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74    ** are exhaust
27d09 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61  ed and only data
27d0a 20 69 6e 20 73 65 6c 65 63 74 20 42 20 72 65 6d   in select B rem
27d0b 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56 64 62  ains..  */.  Vdb
27d0c 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
27d0d 20 22 65 6f 66 2d 41 20 73 75 62 72 6f 75 74 69   "eof-A subrouti
27d0e 6e 65 22 29 29 3b 0a 20 20 69 66 28 20 6f 70 3d  ne"));.  if( op=
27d0f 3d 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20 6f 70  =TK_EXCEPT || op
27d10 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29  ==TK_INTERSECT )
27d11 7b 0a 20 20 20 20 61 64 64 72 45 6f 66 41 20 3d  {.    addrEofA =
27d12 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27d13 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
27d14 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d  , labelEnd);.  }
27d15 65 6c 73 65 7b 20 20 0a 20 20 20 20 61 64 64 72  else{  .    addr
27d16 45 6f 66 41 20 3d 20 73 71 6c 69 74 65 33 56 64  EofA = sqlite3Vd
27d17 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
27d18 66 2c 20 72 65 67 45 6f 66 42 2c 20 6c 61 62 65  f, regEofB, labe
27d19 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  lEnd);.    sqlit
27d1a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27d1b 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
27d1c 42 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20  B, addrOutB);.  
27d1d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27d1e 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
27d1f 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20 20 20   regAddrB);.    
27d20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27d21 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
27d22 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 7d 0a   addrEofA);.  }.
27d23 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
27d24 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72   subroutine to r
27d25 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 75  un when the resu
27d26 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20  lts from select 
27d27 42 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75  B.  ** are exhau
27d28 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61  sted and only da
27d29 74 61 20 69 6e 20 73 65 6c 65 63 74 20 41 20 72  ta in select A r
27d2a 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  emains..  */.  i
27d2b 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53  f( op==TK_INTERS
27d2c 45 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72 45  ECT ){.    addrE
27d2d 6f 66 42 20 3d 20 61 64 64 72 45 6f 66 41 3b 0a  ofB = addrEofA;.
27d2e 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 56    }else{  .    V
27d2f 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
27d30 76 2c 20 22 65 6f 66 2d 42 20 73 75 62 72 6f 75  v, "eof-B subrou
27d31 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 61 64 64  tine"));.    add
27d32 72 45 6f 66 42 20 3d 20 73 71 6c 69 74 65 33 56  rEofB = sqlite3V
27d33 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27d34 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 6c 61 62  If, regEofA, lab
27d35 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69  elEnd);.    sqli
27d36 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27d37 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75   OP_Gosub, regOu
27d38 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20  tA, addrOutA);. 
27d39 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27d3a 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
27d3b 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20  , regAddrA);.   
27d3c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27d3d 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
27d3e 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 7d  , addrEofB);.  }
27d3f 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
27d40 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74  code to handle t
27d41 68 65 20 63 61 73 65 20 6f 66 20 41 3c 42 0a 20  he case of A<B. 
27d42 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f   */.  VdbeNoopCo
27d43 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 6c 74 2d  mment((v, "A-lt-
27d44 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b  B subroutine"));
27d45 0a 20 20 61 64 64 72 41 6c 74 42 20 3d 20 73 71  .  addrAltB = sq
27d46 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27d47 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
27d48 4f 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b  OutA, addrOutA);
27d49 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27d4a 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
27d4b 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73  , regAddrA);.  s
27d4c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27d4d 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f  (v, OP_If, regEo
27d4e 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20  fA, addrEofA);. 
27d4f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27d50 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
27d51 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20  , labelCmpr);.. 
27d52 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
27d53 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20  e to handle the 
27d54 63 61 73 65 20 6f 66 20 41 3d 3d 42 0a 20 20 2a  case of A==B.  *
27d55 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41  /.  if( op==TK_A
27d56 4c 4c 20 29 7b 0a 20 20 20 20 61 64 64 72 41 65  LL ){.    addrAe
27d57 71 42 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20  qB = addrAltB;. 
27d58 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54   }else if( op==T
27d59 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a 20  K_INTERSECT ){. 
27d5a 20 20 20 61 64 64 72 41 65 71 42 20 3d 20 61 64     addrAeqB = ad
27d5b 64 72 41 6c 74 42 3b 0a 20 20 20 20 61 64 64 72  drAltB;.    addr
27d5c 41 6c 74 42 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b  AltB++;.  }else{
27d5d 0a 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  .    VdbeNoopCom
27d5e 6d 65 6e 74 28 28 76 2c 20 22 41 2d 65 71 2d 42  ment((v, "A-eq-B
27d5f 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a   subroutine"));.
27d60 20 20 20 20 61 64 64 72 41 65 71 42 20 3d 0a 20      addrAeqB =. 
27d61 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27d62 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
27d63 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20  , regAddrA);.   
27d64 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27d65 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67  p2(v, OP_If, reg
27d66 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b  EofA, addrEofA);
27d67 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27d68 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
27d69 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29  o, 0, labelCmpr)
27d6a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65  ;.  }..  /* Gene
27d6b 72 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e  rate code to han
27d6c 64 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20  dle the case of 
27d6d 41 3e 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e  A>B.  */.  VdbeN
27d6e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
27d6f 41 2d 67 74 2d 42 20 73 75 62 72 6f 75 74 69 6e  A-gt-B subroutin
27d70 65 22 29 29 3b 0a 20 20 61 64 64 72 41 67 74 42  e"));.  addrAgtB
27d71 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
27d72 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
27d73 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c  if( op==TK_ALL |
27d74 7c 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29  | op==TK_UNION )
27d75 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
27d76 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
27d77 73 75 62 2c 20 72 65 67 4f 75 74 42 2c 20 61 64  sub, regOutB, ad
27d78 64 72 4f 75 74 42 29 3b 0a 20 20 7d 0a 20 20 73  drOutB);.  }.  s
27d79 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
27d7a 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
27d7b 67 41 64 64 72 42 29 3b 0a 20 20 73 71 6c 69 74  gAddrB);.  sqlit
27d7c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27d7d 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20  OP_If, regEofB, 
27d7e 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 73 71 6c  addrEofB);.  sql
27d7f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27d80 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61  , OP_Goto, 0, la
27d81 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20  belCmpr);..  /* 
27d82 54 68 69 73 20 63 6f 64 65 20 72 75 6e 73 20 6f  This code runs o
27d83 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  nce to initializ
27d84 65 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20  e everything..  
27d85 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
27d86 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
27d87 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27d88 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
27d89 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 41 29 3b  er, 0, regEofA);
27d8a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27d8b 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
27d8c 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 42 29 3b  er, 0, regEofB);
27d8d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27d8e 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
27d8f 2c 20 72 65 67 41 64 64 72 41 2c 20 61 64 64 72  , regAddrA, addr
27d90 53 65 6c 65 63 74 41 29 3b 0a 20 20 73 71 6c 69  SelectA);.  sqli
27d91 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27d92 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 41 64   OP_Gosub, regAd
27d93 64 72 42 2c 20 61 64 64 72 53 65 6c 65 63 74 42  drB, addrSelectB
27d94 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
27d95 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c  AddOp2(v, OP_If,
27d96 20 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f   regEofA, addrEo
27d97 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  fA);.  sqlite3Vd
27d98 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
27d99 66 2c 20 72 65 67 45 6f 66 42 2c 20 61 64 64 72  f, regEofB, addr
27d9a 45 6f 66 42 29 3b 0a 0a 20 20 2f 2a 20 49 6d 70  EofB);..  /* Imp
27d9b 6c 65 6d 65 6e 74 20 74 68 65 20 6d 61 69 6e 20  lement the main 
27d9c 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a  merge loop.  */.
27d9d 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
27d9e 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c 61 62  olveLabel(v, lab
27d9f 65 6c 43 6d 70 72 29 3b 0a 20 20 73 71 6c 69 74  elCmpr);.  sqlit
27da0 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
27da1 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20  OP_Permutation, 
27da2 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 2a 29  0, 0, 0, (char*)
27da3 61 50 65 72 6d 75 74 65 2c 20 50 34 5f 49 4e 54  aPermute, P4_INT
27da4 41 52 52 41 59 29 3b 0a 20 20 73 71 6c 69 74 65  ARRAY);.  sqlite
27da5 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
27da6 50 5f 43 6f 6d 70 61 72 65 2c 20 64 65 73 74 41  P_Compare, destA
27da7 2e 69 4d 65 6d 2c 20 64 65 73 74 42 2e 69 4d 65  .iMem, destB.iMe
27da8 6d 2c 20 6e 4f 72 64 65 72 42 79 2c 0a 20 20 20  m, nOrderBy,.   
27da9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27daa 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
27dab 79 4d 65 72 67 65 2c 20 50 34 5f 4b 45 59 49 4e  yMerge, P4_KEYIN
27dac 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 73  FO_HANDOFF);.  s
27dad 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
27dae 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61 64 64  (v, OP_Jump, add
27daf 72 41 6c 74 42 2c 20 61 64 64 72 41 65 71 42 2c  rAltB, addrAeqB,
27db0 20 61 64 64 72 41 67 74 42 29 3b 0a 0a 20 20 2f   addrAgtB);..  /
27db1 2a 20 52 65 6c 65 61 73 65 20 74 65 6d 70 6f 72  * Release tempor
27db2 61 72 79 20 72 65 67 69 73 74 65 72 73 0a 20 20  ary registers.  
27db3 2a 2f 0a 20 20 69 66 28 20 72 65 67 50 72 65 76  */.  if( regPrev
27db4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52   ){.    sqlite3R
27db5 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
27db6 70 50 61 72 73 65 2c 20 72 65 67 50 72 65 76 2c  pParse, regPrev,
27db7 20 6e 4f 72 64 65 72 42 79 2b 31 29 3b 0a 20 20   nOrderBy+1);.  
27db8 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  }..  /* Jump to 
27db9 74 68 65 20 74 68 69 73 20 70 6f 69 6e 74 20 69  the this point i
27dba 6e 20 6f 72 64 65 72 20 74 6f 20 74 65 72 6d 69  n order to termi
27dbb 6e 61 74 65 20 74 68 65 20 71 75 65 72 79 2e 0a  nate the query..
27dbc 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
27dbd 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
27dbe 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a 20 20  , labelEnd);..  
27dbf 2f 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65  /* Set the numbe
27dc0 72 20 6f 66 20 6f 75 74 70 75 74 20 63 6f 6c 75  r of output colu
27dc1 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  mns.  */.  if( p
27dc2 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54  Dest->eDest==SRT
27dc3 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 53  _Output ){.    S
27dc4 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20  elect *pFirst = 
27dc5 70 50 72 69 6f 72 3b 0a 20 20 20 20 77 68 69 6c  pPrior;.    whil
27dc6 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f  e( pFirst->pPrio
27dc7 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69  r ) pFirst = pFi
27dc8 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  rst->pPrior;.   
27dc9 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e   generateColumnN
27dca 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20  ames(pParse, 0, 
27dcb 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b  pFirst->pEList);
27dcc 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 73 73  .  }..  /* Reass
27dcd 65 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70 6f 75  embly the compou
27dce 6e 64 20 71 75 65 72 79 20 73 6f 20 74 68 61 74  nd query so that
27dcf 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65   it will be free
27dd0 64 20 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a  d correctly.  **
27dd1 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   by the calling 
27dd2 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 66  function */.  if
27dd3 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20  ( p->pPrior ){. 
27dd4 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
27dd5 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50  Delete(db, p->pP
27dd6 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e  rior);.  }.  p->
27dd7 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b  pPrior = pPrior;
27dd8 0a 0a 20 20 2f 2a 2a 2a 20 54 42 44 3a 20 20 49  ..  /*** TBD:  I
27dd9 6e 73 65 72 74 20 73 75 62 72 6f 75 74 69 6e 65  nsert subroutine
27dda 20 63 61 6c 6c 73 20 74 6f 20 63 6c 6f 73 65 20   calls to close 
27ddb 63 75 72 73 6f 72 73 20 6f 6e 20 69 6e 63 6f 6d  cursors on incom
27ddc 70 6c 65 74 65 0a 20 20 2a 2a 2a 2a 20 73 75 62  plete.  **** sub
27ddd 71 75 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a 20 20  queries ****/.  
27dde 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
27ddf 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  ;.}.#endif..#if 
27de0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
27de1 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c  OMIT_SUBQUERY) |
27de2 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
27de3 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 20  E_OMIT_VIEW)./* 
27de4 46 6f 72 77 61 72 64 20 44 65 63 6c 61 72 61 74  Forward Declarat
27de5 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 76  ions */.static v
27de6 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c 69 73  oid substExprLis
27de7 74 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72  t(sqlite3*, Expr
27de8 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 45 78 70 72  List*, int, Expr
27de9 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 76  List*);.static v
27dea 6f 69 64 20 73 75 62 73 74 53 65 6c 65 63 74 28  oid substSelect(
27deb 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63 74  sqlite3*, Select
27dec 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73   *, int, ExprLis
27ded 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 61  t *);../*.** Sca
27dee 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 78  n through the ex
27def 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20  pression pExpr. 
27df0 20 52 65 70 6c 61 63 65 20 65 76 65 72 79 20 72   Replace every r
27df1 65 66 65 72 65 6e 63 65 20 74 6f 0a 2a 2a 20 61  eference to.** a
27df2 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65   column in table
27df3 20 6e 75 6d 62 65 72 20 69 54 61 62 6c 65 20 77   number iTable w
27df4 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ith a copy of th
27df5 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20  e iColumn-th.** 
27df6 65 6e 74 72 79 20 69 6e 20 70 45 4c 69 73 74 2e  entry in pEList.
27df7 20 20 28 42 75 74 20 6c 65 61 76 65 20 72 65 66    (But leave ref
27df8 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 52  erences to the R
27df9 4f 57 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20  OWID column .** 
27dfa 75 6e 63 68 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a  unchanged.).**.*
27dfb 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
27dfc 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 6c  s part of the fl
27dfd 61 74 74 65 6e 69 6e 67 20 70 72 6f 63 65 64 75  attening procedu
27dfe 72 65 2e 20 20 41 20 73 75 62 71 75 65 72 79 0a  re.  A subquery.
27dff 2a 2a 20 77 68 6f 73 65 20 72 65 73 75 6c 74 20  ** whose result 
27e00 73 65 74 20 69 73 20 64 65 66 69 6e 65 64 20 62  set is defined b
27e01 79 20 70 45 4c 69 73 74 20 61 70 70 65 61 72 73  y pEList appears
27e02 20 61 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65   as entry in the
27e03 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  .** FROM clause 
27e04 6f 66 20 61 20 53 45 4c 45 43 54 20 73 75 63 68  of a SELECT such
27e05 20 74 68 61 74 20 74 68 65 20 56 44 42 45 20 63   that the VDBE c
27e06 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74  ursor assigned t
27e07 6f 20 74 68 61 74 0a 2a 2a 20 46 4f 52 4d 20 63  o that.** FORM c
27e08 6c 61 75 73 65 20 65 6e 74 72 79 20 69 73 20 69  lause entry is i
27e09 54 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75  Table.  This rou
27e0a 74 69 6e 65 20 6d 61 6b 65 20 74 68 65 20 6e 65  tine make the ne
27e0b 63 65 73 73 61 72 79 20 0a 2a 2a 20 63 68 61 6e  cessary .** chan
27e0c 67 65 73 20 74 6f 20 70 45 78 70 72 20 73 6f 20  ges to pExpr so 
27e0d 74 68 61 74 20 69 74 20 72 65 66 65 72 73 20 64  that it refers d
27e0e 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 73  irectly to the s
27e0f 6f 75 72 63 65 20 74 61 62 6c 65 0a 2a 2a 20 6f  ource table.** o
27e10 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 72  f the subquery r
27e11 61 74 68 65 72 20 74 68 65 20 72 65 73 75 6c 74  ather the result
27e12 20 73 65 74 20 6f 66 20 74 68 65 20 73 75 62 71   set of the subq
27e13 75 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  uery..*/.static 
27e14 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 28 0a  void substExpr(.
27e15 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
27e16 20 20 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20        /* Report 
27e17 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 74 6f  malloc errors to
27e18 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
27e19 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
27e1a 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70  r,        /* Exp
27e1b 72 20 69 6e 20 77 68 69 63 68 20 73 75 62 73 74  r in which subst
27e1c 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 2a  itution occurs *
27e1d 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
27e1e 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
27e1f 20 74 6f 20 62 65 20 73 75 62 73 74 69 74 75 74   to be substitut
27e20 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ed */.  ExprList
27e21 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 53   *pEList    /* S
27e22 75 62 73 74 69 74 75 74 65 20 65 78 70 72 65 73  ubstitute expres
27e23 73 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69 66  sions */.){.  if
27e24 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74  ( pExpr==0 ) ret
27e25 75 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72  urn;.  if( pExpr
27e26 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
27e27 26 26 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  && pExpr->iTable
27e28 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  ==iTable ){.    
27e29 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  if( pExpr->iColu
27e2a 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 45  mn<0 ){.      pE
27e2b 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c  xpr->op = TK_NUL
27e2c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  L;.    }else{.  
27e2d 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a      Expr *pNew;.
27e2e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
27e2f 4c 69 73 74 21 3d 30 20 26 26 20 70 45 78 70 72  List!=0 && pExpr
27e30 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69 73 74  ->iColumn<pEList
27e31 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 20 20  ->nExpr );.     
27e32 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
27e33 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78 70  pLeft==0 && pExp
27e34 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a  r->pRight==0 );.
27e35 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 45 4c        pNew = pEL
27e36 69 73 74 2d 3e 61 5b 70 45 78 70 72 2d 3e 69 43  ist->a[pExpr->iC
27e37 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 3b 0a 20 20  olumn].pExpr;.  
27e38 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77      assert( pNew
27e39 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78  !=0 );.      pEx
27e3a 70 72 2d 3e 6f 70 20 3d 20 70 4e 65 77 2d 3e 6f  pr->op = pNew->o
27e3b 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  p;.      assert(
27e3c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30   pExpr->pLeft==0
27e3d 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d   );.      pExpr-
27e3e 3e 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33  >pLeft = sqlite3
27e3f 45 78 70 72 44 75 70 28 64 62 2c 20 70 4e 65 77  ExprDup(db, pNew
27e40 2d 3e 70 4c 65 66 74 2c 20 30 29 3b 0a 20 20 20  ->pLeft, 0);.   
27e41 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
27e42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20  ->pRight==0 );. 
27e43 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67       pExpr->pRig
27e44 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ht = sqlite3Expr
27e45 44 75 70 28 64 62 2c 20 70 4e 65 77 2d 3e 70 52  Dup(db, pNew->pR
27e46 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  ight, 0);.      
27e47 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
27e48 70 4e 65 77 2d 3e 69 54 61 62 6c 65 3b 0a 20 20  pNew->iTable;.  
27e49 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
27e4a 3d 20 70 4e 65 77 2d 3e 70 54 61 62 3b 0a 20 20  = pNew->pTab;.  
27e4b 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
27e4c 6d 6e 20 3d 20 70 4e 65 77 2d 3e 69 43 6f 6c 75  mn = pNew->iColu
27e4d 6d 6e 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  mn;.      pExpr-
27e4e 3e 69 41 67 67 20 3d 20 70 4e 65 77 2d 3e 69 41  >iAgg = pNew->iA
27e4f 67 67 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  gg;.      sqlite
27e50 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26  3TokenCopy(db, &
27e51 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2c 20 26 70  pExpr->token, &p
27e52 4e 65 77 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20  New->token);.   
27e53 20 20 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43     sqlite3TokenC
27e54 6f 70 79 28 64 62 2c 20 26 70 45 78 70 72 2d 3e  opy(db, &pExpr->
27e55 73 70 61 6e 2c 20 26 70 4e 65 77 2d 3e 73 70 61  span, &pNew->spa
27e56 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  n);.      assert
27e57 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  ( pExpr->x.pList
27e58 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 78 2e  ==0 && pExpr->x.
27e59 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20  pSelect==0 );.  
27e5a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50      if( ExprHasP
27e5b 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50  roperty(pNew, EP
27e5c 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
27e5d 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 78 2e         pExpr->x.
27e5e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
27e5f 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
27e60 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20  New->x.pSelect, 
27e61 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  0);.      }else{
27e62 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
27e63 78 2e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65  x.pList = sqlite
27e64 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
27e65 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 2c 20   pNew->x.pList, 
27e66 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
27e67 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 3d    pExpr->flags =
27e68 20 70 4e 65 77 2d 3e 66 6c 61 67 73 3b 0a 20 20   pNew->flags;.  
27e69 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49      pExpr->pAggI
27e6a 6e 66 6f 20 3d 20 70 4e 65 77 2d 3e 70 41 67 67  nfo = pNew->pAgg
27e6b 49 6e 66 6f 3b 0a 20 20 20 20 20 20 70 4e 65 77  Info;.      pNew
27e6c 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 30 3b 0a  ->pAggInfo = 0;.
27e6d 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
27e6e 20 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c     substExpr(db,
27e6f 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 69   pExpr->pLeft, i
27e70 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
27e71 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64 62      substExpr(db
27e72 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
27e73 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
27e74 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61  ;.    if( ExprHa
27e75 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
27e76 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
27e77 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53 65 6c  {.      substSel
27e78 65 63 74 28 64 62 2c 20 70 45 78 70 72 2d 3e 78  ect(db, pExpr->x
27e79 2e 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c 65  .pSelect, iTable
27e7a 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d  , pEList);.    }
27e7b 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 75 62 73  else{.      subs
27e7c 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 45  tExprList(db, pE
27e7d 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 20 69 54  xpr->x.pList, iT
27e7e 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
27e7f 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69     }.  }.}.stati
27e80 63 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72  c void substExpr
27e81 4c 69 73 74 28 0a 20 20 73 71 6c 69 74 65 33 20  List(.  sqlite3 
27e82 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  *db,         /* 
27e83 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72  Report malloc er
27e84 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 45  rors here */.  E
27e85 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
27e86 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 73      /* List to s
27e87 63 61 6e 20 61 6e 64 20 69 6e 20 77 68 69 63 68  can and in which
27e88 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74   to make substit
27e89 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54  utes */.  int iT
27e8a 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f  able,          /
27e8b 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75  * Table to be su
27e8c 62 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45  bstituted */.  E
27e8d 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
27e8e 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74      /* Substitut
27e8f 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20  e values */.){. 
27e90 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c   int i;.  if( pL
27e91 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ist==0 ) return;
27e92 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c  .  for(i=0; i<pL
27e93 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
27e94 7b 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 28  {.    substExpr(
27e95 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  db, pList->a[i].
27e96 70 45 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70  pExpr, iTable, p
27e97 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74  EList);.  }.}.st
27e98 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53  atic void substS
27e99 65 6c 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33  elect(.  sqlite3
27e9a 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a   *db,         /*
27e9b 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65   Report malloc e
27e9c 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20  rrors here */.  
27e9d 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
27e9e 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73       /* SELECT s
27e9f 74 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63  tatement in whic
27ea0 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69  h to make substi
27ea1 74 75 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  tutions */.  int
27ea2 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20   iTable,        
27ea3 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65    /* Table to be
27ea4 20 72 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20 45   replaced */.  E
27ea5 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
27ea6 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74      /* Substitut
27ea7 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20  e values */.){. 
27ea8 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a   SrcList *pSrc;.
27ea9 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
27eaa 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
27eab 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 70 20  int i;.  if( !p 
27eac 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73  ) return;.  subs
27ead 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
27eae 3e 70 45 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c  >pEList, iTable,
27eaf 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
27eb0 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
27eb1 3e 70 47 72 6f 75 70 42 79 2c 20 69 54 61 62 6c  >pGroupBy, iTabl
27eb2 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75  e, pEList);.  su
27eb3 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20  bstExprList(db, 
27eb4 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 54 61  p->pOrderBy, iTa
27eb5 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
27eb6 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d  substExpr(db, p-
27eb7 3e 70 48 61 76 69 6e 67 2c 20 69 54 61 62 6c 65  >pHaving, iTable
27eb8 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62  , pEList);.  sub
27eb9 73 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 57  stExpr(db, p->pW
27eba 68 65 72 65 2c 20 69 54 61 62 6c 65 2c 20 70 45  here, iTable, pE
27ebb 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74 53 65  List);.  substSe
27ebc 6c 65 63 74 28 64 62 2c 20 70 2d 3e 70 50 72 69  lect(db, p->pPri
27ebd 6f 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  or, iTable, pELi
27ebe 73 74 29 3b 0a 20 20 70 53 72 63 20 3d 20 70 2d  st);.  pSrc = p-
27ebf 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28  >pSrc;.  assert(
27ec0 20 70 53 72 63 20 29 3b 20 20 2f 2a 20 45 76 65   pSrc );  /* Eve
27ec1 6e 20 66 6f 72 20 28 53 45 4c 45 43 54 20 31 29  n for (SELECT 1)
27ec2 20 77 65 20 68 61 76 65 3a 20 70 53 72 63 21 3d   we have: pSrc!=
27ec3 30 20 62 75 74 20 70 53 72 63 2d 3e 6e 53 72 63  0 but pSrc->nSrc
27ec4 3d 3d 30 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57  ==0 */.  if( ALW
27ec5 41 59 53 28 70 53 72 63 29 20 29 7b 0a 20 20 20  AYS(pSrc) ){.   
27ec6 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72   for(i=pSrc->nSr
27ec7 63 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61  c, pItem=pSrc->a
27ec8 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
27ec9 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73 75 62 73  m++){.      subs
27eca 74 53 65 6c 65 63 74 28 64 62 2c 20 70 49 74 65  tSelect(db, pIte
27ecb 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 69 54 61 62  m->pSelect, iTab
27ecc 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20  le, pEList);.   
27ecd 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20   }.  }.}.#endif 
27ece 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
27ecf 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
27ed0 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
27ed1 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
27ed2 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  */..#if !defined
27ed3 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
27ed4 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e  QUERY) || !defin
27ed5 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
27ed6 49 45 57 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  IEW)./*.** This 
27ed7 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73  routine attempts
27ed8 20 74 6f 20 66 6c 61 74 74 65 6e 20 73 75 62 71   to flatten subq
27ed9 75 65 72 69 65 73 20 69 6e 20 6f 72 64 65 72 20  ueries in order 
27eda 74 6f 20 73 70 65 65 64 0a 2a 2a 20 65 78 65 63  to speed.** exec
27edb 75 74 69 6f 6e 2e 20 20 49 74 20 72 65 74 75 72  ution.  It retur
27edc 6e 73 20 31 20 69 66 20 69 74 20 6d 61 6b 65 73  ns 1 if it makes
27edd 20 63 68 61 6e 67 65 73 20 61 6e 64 20 30 20 69   changes and 0 i
27ede 66 20 6e 6f 20 66 6c 61 74 74 65 6e 69 6e 67 0a  f no flattening.
27edf 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  ** occurs..**.**
27ee0 20 54 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 74   To understand t
27ee1 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 66 6c  he concept of fl
27ee2 61 74 74 65 6e 69 6e 67 2c 20 63 6f 6e 73 69 64  attening, consid
27ee3 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  er the following
27ee4 0a 2a 2a 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a  .** query:.**.**
27ee5 20 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52       SELECT a FR
27ee6 4f 4d 20 28 53 45 4c 45 43 54 20 78 2b 79 20 41  OM (SELECT x+y A
27ee7 53 20 61 20 46 52 4f 4d 20 74 31 20 57 48 45 52  S a FROM t1 WHER
27ee8 45 20 7a 3c 31 30 30 29 20 57 48 45 52 45 20 61  E z<100) WHERE a
27ee9 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66  >5.**.** The def
27eea 61 75 6c 74 20 77 61 79 20 6f 66 20 69 6d 70 6c  ault way of impl
27eeb 65 6d 65 6e 74 69 6e 67 20 74 68 69 73 20 71 75  ementing this qu
27eec 65 72 79 20 69 73 20 74 6f 20 65 78 65 63 75 74  ery is to execut
27eed 65 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72  e the.** subquer
27eee 79 20 66 69 72 73 74 20 61 6e 64 20 73 74 6f 72  y first and stor
27eef 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e  e the results in
27ef0 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62   a temporary tab
27ef1 6c 65 2c 20 74 68 65 6e 0a 2a 2a 20 72 75 6e 20  le, then.** run 
27ef2 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
27ef3 6f 6e 20 74 68 61 74 20 74 65 6d 70 6f 72 61 72  on that temporar
27ef4 79 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72  y table.  This r
27ef5 65 71 75 69 72 65 73 20 74 77 6f 0a 2a 2a 20 70  equires two.** p
27ef6 61 73 73 65 73 20 6f 76 65 72 20 74 68 65 20 64  asses over the d
27ef7 61 74 61 2e 20 20 46 75 72 74 68 65 72 6d 6f 72  ata.  Furthermor
27ef8 65 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 74  e, because the t
27ef9 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 2a  emporary table.*
27efa 2a 20 68 61 73 20 6e 6f 20 69 6e 64 69 63 65 73  * has no indices
27efb 2c 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  , the WHERE clau
27efc 73 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 20  se on the outer 
27efd 71 75 65 72 79 20 63 61 6e 6e 6f 74 20 62 65 0a  query cannot be.
27efe 2a 2a 20 6f 70 74 69 6d 69 7a 65 64 2e 0a 2a 2a  ** optimized..**
27eff 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
27f00 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 77   attempts to rew
27f01 72 69 74 65 20 71 75 65 72 69 65 73 20 73 75 63  rite queries suc
27f02 68 20 61 73 20 74 68 65 20 61 62 6f 76 65 20 69  h as the above i
27f03 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20  nto.** a single 
27f04 66 6c 61 74 20 73 65 6c 65 63 74 2c 20 6c 69 6b  flat select, lik
27f05 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
27f06 20 20 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20    SELECT x+y AS 
27f07 61 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  a FROM t1 WHERE 
27f08 7a 3c 31 30 30 20 41 4e 44 20 61 3e 35 0a 2a 2a  z<100 AND a>5.**
27f09 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e  .** The code gen
27f0a 65 72 61 74 65 64 20 66 6f 72 20 74 68 69 73 20  erated for this 
27f0b 73 69 6d 70 69 66 69 63 61 74 69 6f 6e 20 67 69  simpification gi
27f0c 76 65 73 20 74 68 65 20 73 61 6d 65 20 72 65 73  ves the same res
27f0d 75 6c 74 0a 2a 2a 20 62 75 74 20 6f 6e 6c 79 20  ult.** but only 
27f0e 68 61 73 20 74 6f 20 73 63 61 6e 20 74 68 65 20  has to scan the 
27f0f 64 61 74 61 20 6f 6e 63 65 2e 20 20 41 6e 64 20  data once.  And 
27f10 62 65 63 61 75 73 65 20 69 6e 64 69 63 65 73 20  because indices 
27f11 6d 69 67 68 74 20 0a 2a 2a 20 65 78 69 73 74 20  might .** exist 
27f12 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 31 2c  on the table t1,
27f13 20 61 20 63 6f 6d 70 6c 65 74 65 20 73 63 61 6e   a complete scan
27f14 20 6f 66 20 74 68 65 20 64 61 74 61 20 6d 69 67   of the data mig
27f15 68 74 20 62 65 0a 2a 2a 20 61 76 6f 69 64 65 64  ht be.** avoided
27f16 2e 0a 2a 2a 0a 2a 2a 20 46 6c 61 74 74 65 6e 69  ..**.** Flatteni
27f17 6e 67 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d  ng is only attem
27f18 70 74 65 64 20 69 66 20 61 6c 6c 20 6f 66 20 74  pted if all of t
27f19 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
27f1a 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 28   true:.**.**   (
27f1b 31 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  1)  The subquery
27f1c 20 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71   and the outer q
27f1d 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68  uery do not both
27f1e 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73 2e   use aggregates.
27f1f 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 20 54 68  .**.**   (2)  Th
27f20 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f  e subquery is no
27f21 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f  t an aggregate o
27f22 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
27f23 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e  y is not a join.
27f24 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 20 54 68  .**.**   (3)  Th
27f25 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f  e subquery is no
27f26 74 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72  t the right oper
27f27 61 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75  and of a left ou
27f28 74 65 72 20 6a 6f 69 6e 0a 2a 2a 20 20 20 20 20  ter join.**     
27f29 20 20 20 28 4f 72 69 67 69 6e 61 6c 6c 79 20 74     (Originally t
27f2a 69 63 6b 65 74 20 23 33 30 36 2e 20 20 53 74 72  icket #306.  Str
27f2b 65 6e 67 68 74 65 6e 65 64 20 62 79 20 74 69 63  enghtened by tic
27f2c 6b 65 74 20 23 33 33 30 30 29 0a 2a 2a 0a 2a 2a  ket #3300).**.**
27f2d 20 20 20 28 34 29 20 20 54 68 65 20 73 75 62 71     (4)  The subq
27f2e 75 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53 54  uery is not DIST
27f2f 49 4e 43 54 20 6f 72 20 74 68 65 20 6f 75 74 65  INCT or the oute
27f30 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61  r query is not a
27f31 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28   join..**.**   (
27f32 35 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  5)  The subquery
27f33 20 69 73 20 6e 6f 74 20 44 49 53 54 49 4e 43 54   is not DISTINCT
27f34 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
27f35 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65  ery does not use
27f36 0a 2a 2a 20 20 20 20 20 20 20 20 61 67 67 72 65  .**        aggre
27f37 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28  gates..**.**   (
27f38 36 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  6)  The subquery
27f39 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67   does not use ag
27f3a 67 72 65 67 61 74 65 73 20 6f 72 20 74 68 65 20  gregates or the 
27f3b 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e  outer query is n
27f3c 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 44 49 53  ot.**        DIS
27f3d 54 49 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20 20 20 28  TINCT..**.**   (
27f3e 37 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  7)  The subquery
27f3f 20 68 61 73 20 61 20 46 52 4f 4d 20 63 6c 61 75   has a FROM clau
27f40 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 38 29 20  se..**.**   (8) 
27f41 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
27f42 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54  es not use LIMIT
27f43 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
27f44 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69  ery is not a joi
27f45 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 39 29 20 20  n..**.**   (9)  
27f46 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65  The subquery doe
27f47 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20  s not use LIMIT 
27f48 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
27f49 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a  ry does not use.
27f4a 2a 2a 20 20 20 20 20 20 20 20 61 67 67 72 65 67  **        aggreg
27f4b 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 30  ates..**.**  (10
27f4c 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
27f4d 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67  does not use agg
27f4e 72 65 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f  regates or the o
27f4f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20  uter query does 
27f50 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 75 73  not.**        us
27f51 65 20 4c 49 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20 20  e LIMIT..**.**  
27f52 28 31 31 29 20 20 54 68 65 20 73 75 62 71 75 65  (11)  The subque
27f53 72 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65 72  ry and the outer
27f54 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f   query do not bo
27f55 74 68 20 68 61 76 65 20 4f 52 44 45 52 20 42 59  th have ORDER BY
27f56 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20   clauses..**.** 
27f57 20 28 31 32 29 20 20 4e 6f 74 20 69 6d 70 6c 65   (12)  Not imple
27f58 6d 65 6e 74 65 64 2e 20 20 53 75 62 73 75 6d 65  mented.  Subsume
27f59 64 20 69 6e 74 6f 20 72 65 73 74 72 69 63 74 69  d into restricti
27f5a 6f 6e 20 28 33 29 2e 20 20 57 61 73 20 70 72 65  on (3).  Was pre
27f5b 76 69 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20  viously.**      
27f5c 20 20 61 20 73 65 70 61 72 61 74 65 20 72 65 73    a separate res
27f5d 74 72 69 63 74 69 6f 6e 20 64 65 72 69 76 69 6e  triction derivin
27f5e 67 20 66 72 6f 6d 20 74 69 63 6b 65 74 20 23 33  g from ticket #3
27f5f 35 30 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 33 29 20  50..**.**  (13) 
27f60 20 54 68 65 20 73 75 62 71 75 65 72 79 20 61 6e   The subquery an
27f61 64 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f  d outer query do
27f62 20 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 4c 49   not both use LI
27f63 4d 49 54 0a 2a 2a 0a 2a 2a 20 20 28 31 34 29 20  MIT.**.**  (14) 
27f64 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
27f65 65 73 20 6e 6f 74 20 75 73 65 20 4f 46 46 53 45  es not use OFFSE
27f66 54 0a 2a 2a 0a 2a 2a 20 20 28 31 35 29 20 20 54  T.**.**  (15)  T
27f67 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69  he outer query i
27f68 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20  s not part of a 
27f69 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20  compound select 
27f6a 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  or the.**       
27f6b 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
27f6c 6f 74 20 68 61 76 65 20 62 6f 74 68 20 61 6e 20  ot have both an 
27f6d 4f 52 44 45 52 20 42 59 20 61 6e 64 20 61 20 4c  ORDER BY and a L
27f6e 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a 20  IMIT clause..** 
27f6f 20 20 20 20 20 20 20 28 53 65 65 20 74 69 63 6b         (See tick
27f70 65 74 20 23 32 33 33 39 29 0a 2a 2a 0a 2a 2a 20  et #2339).**.** 
27f71 20 28 31 36 29 20 20 54 68 65 20 6f 75 74 65 72   (16)  The outer
27f72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e   query is not an
27f73 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 74 68   aggregate or th
27f74 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 0a  e subquery does.
27f75 2a 2a 20 20 20 20 20 20 20 20 6e 6f 74 20 63 6f  **        not co
27f76 6e 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2e 20  ntain ORDER BY. 
27f77 20 28 54 69 63 6b 65 74 20 23 32 39 34 32 29 20   (Ticket #2942) 
27f78 20 54 68 69 73 20 75 73 65 64 20 74 6f 20 6e 6f   This used to no
27f79 74 20 6d 61 74 74 65 72 0a 2a 2a 20 20 20 20 20  t matter.**     
27f7a 20 20 20 75 6e 74 69 6c 20 77 65 20 69 6e 74 72     until we intr
27f7b 6f 64 75 63 65 64 20 74 68 65 20 67 72 6f 75 70  oduced the group
27f7c 5f 63 6f 6e 63 61 74 28 29 20 66 75 6e 63 74 69  _concat() functi
27f7d 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 28 31 37  on.  .**.**  (17
27f7e 29 20 20 54 68 65 20 73 75 62 2d 71 75 65 72 79  )  The sub-query
27f7f 20 69 73 20 6e 6f 74 20 61 20 63 6f 6d 70 6f 75   is not a compou
27f80 6e 64 20 73 65 6c 65 63 74 2c 20 6f 72 20 69 74  nd select, or it
27f81 20 69 73 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20   is a UNION ALL 
27f82 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6d 70 6f  .**        compo
27f83 75 6e 64 20 63 6c 61 75 73 65 20 6d 61 64 65 20  und clause made 
27f84 75 70 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 6e  up entirely of n
27f85 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75 65  on-aggregate que
27f86 72 69 65 73 2c 20 61 6e 64 20 0a 2a 2a 20 20 20  ries, and .**   
27f87 20 20 20 20 20 74 68 65 20 70 61 72 65 6e 74 20       the parent 
27f88 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  query:.**.**    
27f89 20 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 69        * is not i
27f8a 74 73 65 6c 66 20 70 61 72 74 20 6f 66 20 61 20  tself part of a 
27f8b 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c  compound select,
27f8c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69  .**          * i
27f8d 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61  s not an aggrega
27f8e 74 65 20 6f 72 20 44 49 53 54 49 4e 43 54 20 71  te or DISTINCT q
27f8f 75 65 72 79 2c 20 61 6e 64 0a 2a 2a 20 20 20 20  uery, and.**    
27f90 20 20 20 20 20 20 2a 20 68 61 73 20 6e 6f 20 6f        * has no o
27f91 74 68 65 72 20 74 61 62 6c 65 73 20 6f 72 20 73  ther tables or s
27f92 75 62 2d 73 65 6c 65 63 74 73 20 69 6e 20 74 68  ub-selects in th
27f93 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a  e FROM clause..*
27f94 2a 0a 2a 2a 20 20 20 20 20 20 20 20 54 68 65 20  *.**        The 
27f95 70 61 72 65 6e 74 20 61 6e 64 20 73 75 62 2d 71  parent and sub-q
27f96 75 65 72 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e  uery may contain
27f97 20 57 48 45 52 45 20 63 6c 61 75 73 65 73 2e 20   WHERE clauses. 
27f98 53 75 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 20 20  Subject to.**   
27f99 20 20 20 20 20 72 75 6c 65 73 20 28 31 31 29 2c       rules (11),
27f9a 20 28 31 33 29 20 61 6e 64 20 28 31 34 29 2c 20   (13) and (14), 
27f9b 74 68 65 79 20 6d 61 79 20 61 6c 73 6f 20 63 6f  they may also co
27f9c 6e 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2c 0a  ntain ORDER BY,.
27f9d 2a 2a 20 20 20 20 20 20 20 20 4c 49 4d 49 54 20  **        LIMIT 
27f9e 61 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73  and OFFSET claus
27f9f 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 38 29 20  es..**.**  (18) 
27fa0 20 49 66 20 74 68 65 20 73 75 62 2d 71 75 65 72   If the sub-quer
27fa1 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20  y is a compound 
27fa2 73 65 6c 65 63 74 2c 20 74 68 65 6e 20 61 6c 6c  select, then all
27fa3 20 74 65 72 6d 73 20 6f 66 20 74 68 65 0a 2a 2a   terms of the.**
27fa4 20 20 20 20 20 20 20 20 4f 52 44 45 52 20 62 79          ORDER by
27fa5 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 70   clause of the p
27fa6 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20 73 69  arent must be si
27fa7 6d 70 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20  mple references 
27fa8 74 6f 20 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f  to .**        co
27fa9 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73 75 62  lumns of the sub
27faa 2d 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 28  -query..**.**  (
27fab 31 39 29 20 20 54 68 65 20 73 75 62 71 75 65 72  19)  The subquer
27fac 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c  y does not use L
27fad 49 4d 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65  IMIT or the oute
27fae 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  r query does not
27faf 0a 2a 2a 20 20 20 20 20 20 20 20 68 61 76 65 20  .**        have 
27fb0 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a  a WHERE clause..
27fb1 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f  **.** In this ro
27fb2 75 74 69 6e 65 2c 20 74 68 65 20 22 70 22 20 70  utine, the "p" p
27fb3 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f  arameter is a po
27fb4 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 75 74  inter to the out
27fb5 65 72 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65  er query..** The
27fb6 20 73 75 62 71 75 65 72 79 20 69 73 20 70 2d 3e   subquery is p->
27fb7 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 20  pSrc->a[iFrom]. 
27fb8 20 69 73 41 67 67 20 69 73 20 74 72 75 65 20 69   isAgg is true i
27fb9 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  f the outer quer
27fba 79 0a 2a 2a 20 75 73 65 73 20 61 67 67 72 65 67  y.** uses aggreg
27fbb 61 74 65 73 20 61 6e 64 20 73 75 62 71 75 65 72  ates and subquer
27fbc 79 49 73 41 67 67 20 69 73 20 74 72 75 65 20 69  yIsAgg is true i
27fbd 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75  f the subquery u
27fbe 73 65 73 20 61 67 67 72 65 67 61 74 65 73 2e 0a  ses aggregates..
27fbf 2a 2a 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e  **.** If flatten
27fc0 69 6e 67 20 69 73 20 6e 6f 74 20 61 74 74 65 6d  ing is not attem
27fc1 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  pted, this routi
27fc2 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e  ne is a no-op an
27fc3 64 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 20  d returns 0..** 
27fc4 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73  If flattening is
27fc5 20 61 74 74 65 6d 70 74 65 64 20 74 68 69 73 20   attempted this 
27fc6 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
27fc7 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  1..**.** All of 
27fc8 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61  the expression a
27fc9 6e 61 6c 79 73 69 73 20 6d 75 73 74 20 6f 63 63  nalysis must occ
27fca 75 72 20 6f 6e 20 62 6f 74 68 20 74 68 65 20 6f  ur on both the o
27fcb 75 74 65 72 20 71 75 65 72 79 20 61 6e 64 0a 2a  uter query and.*
27fcc 2a 20 74 68 65 20 73 75 62 71 75 65 72 79 20 62  * the subquery b
27fcd 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69  efore this routi
27fce 6e 65 20 72 75 6e 73 2e 0a 2a 2f 0a 73 74 61 74  ne runs..*/.stat
27fcf 69 63 20 69 6e 74 20 66 6c 61 74 74 65 6e 53 75  ic int flattenSu
27fd0 62 71 75 65 72 79 28 0a 20 20 50 61 72 73 65 20  bquery(.  Parse 
27fd1 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
27fd2 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
27fd3 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
27fd4 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
27fd5 68 65 20 70 61 72 65 6e 74 20 6f 72 20 6f 75 74  he parent or out
27fd6 65 72 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  er SELECT statem
27fd7 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 46 72  ent */.  int iFr
27fd8 6f 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  om,           /*
27fd9 20 49 6e 64 65 78 20 69 6e 20 70 2d 3e 70 53 72   Index in p->pSr
27fda 63 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 20 69 6e  c->a[] of the in
27fdb 6e 65 72 20 73 75 62 71 75 65 72 79 20 2a 2f 0a  ner subquery */.
27fdc 20 20 69 6e 74 20 69 73 41 67 67 2c 20 20 20 20    int isAgg,    
27fdd 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
27fde 66 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20 75  f outer SELECT u
27fdf 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66 75  ses aggregate fu
27fe0 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  nctions */.  int
27fe1 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20 20   subqueryIsAgg  
27fe2 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
27fe3 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20 61   subquery uses a
27fe4 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
27fe5 6e 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  ns */.){.  const
27fe6 20 63 68 61 72 20 2a 7a 53 61 76 65 64 41 75 74   char *zSavedAut
27fe7 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73  hContext = pPars
27fe8 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b  e->zAuthContext;
27fe9 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 61 72 65  .  Select *pPare
27fea 6e 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 53  nt;.  Select *pS
27feb 75 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ub;       /* The
27fec 20 69 6e 6e 65 72 20 71 75 65 72 79 20 6f 72 20   inner query or 
27fed 22 73 75 62 71 75 65 72 79 22 20 2a 2f 0a 20 20  "subquery" */.  
27fee 53 65 6c 65 63 74 20 2a 70 53 75 62 31 3b 20 20  Select *pSub1;  
27fef 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
27ff0 6f 20 74 68 65 20 72 69 67 68 74 6d 6f 73 74 20  o the rightmost 
27ff1 73 65 6c 65 63 74 20 69 6e 20 73 75 62 2d 71 75  select in sub-qu
27ff2 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ery */.  SrcList
27ff3 20 2a 70 53 72 63 3b 20 20 20 20 20 20 2f 2a 20   *pSrc;      /* 
27ff4 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
27ff5 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  of the outer que
27ff6 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  ry */.  SrcList 
27ff7 2a 70 53 75 62 53 72 63 3b 20 20 20 2f 2a 20 54  *pSubSrc;   /* T
27ff8 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
27ff9 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 2a  f the subquery *
27ffa 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  /.  ExprList *pL
27ffb 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 72  ist;    /* The r
27ffc 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
27ffd 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a   outer query */.
27ffe 20 20 69 6e 74 20 69 50 61 72 65 6e 74 3b 20 20    int iParent;  
27fff 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 63 75        /* VDBE cu
28000 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
28001 68 65 20 70 53 75 62 20 72 65 73 75 6c 74 20 73  he pSub result s
28002 65 74 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f  et temp table */
28003 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
28004 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
28005 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72  ounter */.  Expr
28006 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20   *pWhere;       
28007 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28008 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
28009 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2800a 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 75 62 69  List_item *pSubi
2800b 74 65 6d 3b 20 20 20 2f 2a 20 54 68 65 20 73 75  tem;   /* The su
2800c 62 71 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69  bquery */.  sqli
2800d 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2800e 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  ->db;..  /* Chec
2800f 6b 20 74 6f 20 73 65 65 20 69 66 20 66 6c 61 74  k to see if flat
28010 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69 74  tening is permit
28011 74 65 64 2e 20 20 52 65 74 75 72 6e 20 30 20 69  ted.  Return 0 i
28012 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 61 73  f not..  */.  as
28013 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20  sert( p!=0 );.  
28014 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 69 6f  assert( p->pPrio
28015 72 3d 3d 30 20 29 3b 20 20 2f 2a 20 55 6e 61 62  r==0 );  /* Unab
28016 6c 65 20 74 6f 20 66 6c 61 74 74 65 6e 20 63 6f  le to flatten co
28017 6d 70 6f 75 6e 64 20 71 75 65 72 69 65 73 20 2a  mpound queries *
28018 2f 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53  /.  pSrc = p->pS
28019 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53  rc;.  assert( pS
2801a 72 63 20 26 26 20 69 46 72 6f 6d 3e 3d 30 20 26  rc && iFrom>=0 &
2801b 26 20 69 46 72 6f 6d 3c 70 53 72 63 2d 3e 6e 53  & iFrom<pSrc->nS
2801c 72 63 20 29 3b 0a 20 20 70 53 75 62 69 74 65 6d  rc );.  pSubitem
2801d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 46 72 6f   = &pSrc->a[iFro
2801e 6d 5d 3b 0a 20 20 69 50 61 72 65 6e 74 20 3d 20  m];.  iParent = 
2801f 70 53 75 62 69 74 65 6d 2d 3e 69 43 75 72 73 6f  pSubitem->iCurso
28020 72 3b 0a 20 20 70 53 75 62 20 3d 20 70 53 75 62  r;.  pSub = pSub
28021 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20  item->pSelect;. 
28022 20 61 73 73 65 72 74 28 20 70 53 75 62 21 3d 30   assert( pSub!=0
28023 20 29 3b 0a 20 20 69 66 28 20 69 73 41 67 67 20   );.  if( isAgg 
28024 26 26 20 73 75 62 71 75 65 72 79 49 73 41 67 67  && subqueryIsAgg
28025 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
28026 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28027 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 29 20  Restriction (1) 
28028 20 2a 2f 0a 20 20 69 66 28 20 73 75 62 71 75 65   */.  if( subque
28029 72 79 49 73 41 67 67 20 26 26 20 70 53 72 63 2d  ryIsAgg && pSrc-
2802a 3e 6e 53 72 63 3e 31 20 29 20 72 65 74 75 72 6e  >nSrc>1 ) return
2802b 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
2802c 52 65 73 74 72 69 63 74 69 6f 6e 20 28 32 29 20  Restriction (2) 
2802d 20 2a 2f 0a 20 20 70 53 75 62 53 72 63 20 3d 20   */.  pSubSrc = 
2802e 70 53 75 62 2d 3e 70 53 72 63 3b 0a 20 20 61 73  pSub->pSrc;.  as
2802f 73 65 72 74 28 20 70 53 75 62 53 72 63 20 29 3b  sert( pSubSrc );
28030 0a 20 20 2f 2a 20 50 72 69 6f 72 20 74 6f 20 76  .  /* Prior to v
28031 65 72 73 69 6f 6e 20 33 2e 31 2e 32 2c 20 77 68  ersion 3.1.2, wh
28032 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46  en LIMIT and OFF
28033 53 45 54 20 68 61 64 20 74 6f 20 62 65 20 73 69  SET had to be si
28034 6d 70 6c 65 20 63 6f 6e 73 74 61 6e 74 73 2c 0a  mple constants,.
28035 20 20 2a 2a 20 6e 6f 74 20 61 72 62 69 74 72 61    ** not arbitra
28036 72 79 20 65 78 70 72 65 73 73 73 69 6f 6e 73 2c  ry expresssions,
28037 20 77 65 20 61 6c 6c 6f 77 65 64 20 73 6f 6d 65   we allowed some
28038 20 63 6f 6d 62 69 6e 69 6e 67 20 6f 66 20 4c 49   combining of LI
28039 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 20  MIT and OFFSET. 
2803a 20 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65 79   ** because they
2803b 20 63 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74   could be comput
2803c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ed at compile-ti
2803d 6d 65 2e 20 20 42 75 74 20 77 68 65 6e 20 4c 49  me.  But when LI
2803e 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 20  MIT and OFFSET. 
2803f 20 2a 2a 20 62 65 63 61 6d 65 20 61 72 62 69 74   ** became arbit
28040 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 73  rary expressions
28041 2c 20 77 65 20 77 65 72 65 20 66 6f 72 63 65 64  , we were forced
28042 20 74 6f 20 61 64 64 20 72 65 73 74 72 69 63 74   to add restrict
28043 69 6f 6e 73 20 28 31 33 29 0a 20 20 2a 2a 20 61  ions (13).  ** a
28044 6e 64 20 28 31 34 29 2e 20 2a 2f 0a 20 20 69 66  nd (14). */.  if
28045 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26  ( pSub->pLimit &
28046 26 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 65  & p->pLimit ) re
28047 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
28048 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
28049 69 6f 6e 20 28 31 33 29 20 2a 2f 0a 20 20 69 66  ion (13) */.  if
2804a 28 20 70 53 75 62 2d 3e 70 4f 66 66 73 65 74 20  ( pSub->pOffset 
2804b 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
2804c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2804d 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
2804e 69 6f 6e 20 28 31 34 29 20 2a 2f 0a 20 20 69 66  ion (14) */.  if
2804f 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20  ( p->pRightmost 
28050 26 26 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20  && pSub->pLimit 
28051 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42  && pSub->pOrderB
28052 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  y ){.    return 
28053 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
28054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28055 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28056 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 35   Restriction (15
28057 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  ) */.  }.  if( p
28058 53 75 62 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20  SubSrc->nSrc==0 
28059 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
2805a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2805b 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
2805c 20 28 37 29 20 20 2a 2f 0a 20 20 69 66 28 20 28   (7)  */.  if( (
2805d 28 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20  (pSub->selFlags 
2805e 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d  & SF_Distinct)!=
2805f 30 20 7c 7c 20 70 53 75 62 2d 3e 70 4c 69 6d 69  0 || pSub->pLimi
28060 74 29 20 0a 20 20 20 20 20 20 20 20 20 26 26 20  t) .         && 
28061 28 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 7c 7c  (pSrc->nSrc>1 ||
28062 20 69 73 41 67 67 29 20 29 7b 20 20 20 20 20 20   isAgg) ){      
28063 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
28064 6f 6e 73 20 28 34 29 28 35 29 28 38 29 28 39 29  ons (4)(5)(8)(9)
28065 20 2a 2f 0a 20 20 20 20 20 72 65 74 75 72 6e 20   */.     return 
28066 30 3b 20 20 20 20 20 20 20 0a 20 20 7d 0a 20 20  0;       .  }.  
28067 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  if( (p->selFlags
28068 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21   & SF_Distinct)!
28069 3d 30 20 26 26 20 73 75 62 71 75 65 72 79 49 73  =0 && subqueryIs
2806a 41 67 67 20 29 7b 0a 20 20 20 20 20 72 65 74 75  Agg ){.     retu
2806b 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a  rn 0;         /*
2806c 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 36 29   Restriction (6)
2806d 20 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70    */.  }.  if( p
2806e 2d 3e 70 4f 72 64 65 72 42 79 20 26 26 20 70 53  ->pOrderBy && pS
2806f 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  ub->pOrderBy ){.
28070 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
28071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28073 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
28074 72 69 63 74 69 6f 6e 20 28 31 31 29 20 2a 2f 0a  riction (11) */.
28075 20 20 7d 0a 20 20 69 66 28 20 69 73 41 67 67 20    }.  if( isAgg 
28076 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42  && pSub->pOrderB
28077 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  y ) return 0;   
28078 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28079 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 36 29  Restriction (16)
2807a 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e   */.  if( pSub->
2807b 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 57 68  pLimit && p->pWh
2807c 65 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ere ) return 0; 
2807d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2807e 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 39 29  Restriction (19)
2807f 20 2a 2f 0a 0a 20 20 2f 2a 20 4f 42 53 4f 4c 45   */..  /* OBSOLE
28080 54 45 20 43 4f 4d 4d 45 4e 54 20 31 3a 0a 20 20  TE COMMENT 1:.  
28081 2a 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 33  ** Restriction 3
28082 3a 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65  :  If the subque
28083 72 79 20 69 73 20 61 20 6a 6f 69 6e 2c 20 6d 61  ry is a join, ma
28084 6b 65 20 73 75 72 65 20 74 68 65 20 73 75 62 71  ke sure the subq
28085 75 65 72 79 20 69 73 20 0a 20 20 2a 2a 20 6e 6f  uery is .  ** no
28086 74 20 75 73 65 64 20 61 73 20 74 68 65 20 72 69  t used as the ri
28087 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61  ght operand of a
28088 6e 20 6f 75 74 65 72 20 6a 6f 69 6e 2e 20 20 45  n outer join.  E
28089 78 61 6d 70 6c 65 73 20 6f 66 20 77 68 79 20 74  xamples of why t
2808a 68 69 73 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20  his.  ** is not 
2808b 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20  allowed:.  **.  
2808c 2a 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c 45  **         t1 LE
2808d 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 74  FT OUTER JOIN (t
2808e 32 20 4a 4f 49 4e 20 74 33 29 0a 20 20 2a 2a 0a  2 JOIN t3).  **.
2808f 20 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74 74    ** If we flatt
28090 65 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77 65  en the above, we
28091 20 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a   would get.  **.
28092 20 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31    **         (t1
28093 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
28094 20 74 32 29 20 4a 4f 49 4e 20 74 33 0a 20 20 2a   t2) JOIN t3.  *
28095 2a 0a 20 20 2a 2a 20 77 68 69 63 68 20 69 73 20  *.  ** which is 
28096 6e 6f 74 20 61 74 20 61 6c 6c 20 74 68 65 20 73  not at all the s
28097 61 6d 65 20 74 68 69 6e 67 2e 0a 20 20 2a 2a 0a  ame thing..  **.
28098 20 20 2a 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f    ** OBSOLETE CO
28099 4d 4d 45 4e 54 20 32 3a 0a 20 20 2a 2a 20 52 65  MMENT 2:.  ** Re
2809a 73 74 72 69 63 74 69 6f 6e 20 31 32 3a 20 20 49  striction 12:  I
2809b 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69  f the subquery i
2809c 73 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72  s the right oper
2809d 61 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75  and of a left ou
2809e 74 65 72 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20 6d  ter.  ** join, m
2809f 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 75 62  ake sure the sub
280a0 71 75 65 72 79 20 68 61 73 20 6e 6f 20 57 48 45  query has no WHE
280a1 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 20  RE clause..  ** 
280a2 41 6e 20 65 78 61 6d 70 6c 65 73 20 6f 66 20 77  An examples of w
280a3 68 79 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61  hy this is not a
280a4 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a  llowed:.  **.  *
280a5 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c 45 46  *         t1 LEF
280a6 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 53 45  T OUTER JOIN (SE
280a7 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 32 20 57  LECT * FROM t2 W
280a8 48 45 52 45 20 74 32 2e 78 3e 30 29 0a 20 20 2a  HERE t2.x>0).  *
280a9 2a 0a 20 20 2a 2a 20 49 66 20 77 65 20 66 6c 61  *.  ** If we fla
280aa 74 74 65 6e 20 74 68 65 20 61 62 6f 76 65 2c 20  tten the above, 
280ab 77 65 20 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a  we would get.  *
280ac 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 28  *.  **         (
280ad 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f  t1 LEFT OUTER JO
280ae 49 4e 20 74 32 29 20 57 48 45 52 45 20 74 32 2e  IN t2) WHERE t2.
280af 78 3e 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 75  x>0.  **.  ** Bu
280b0 74 20 74 68 65 20 74 32 2e 78 3e 30 20 74 65 73  t the t2.x>0 tes
280b1 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 66 61  t will always fa
280b2 69 6c 20 6f 6e 20 61 20 4e 55 4c 4c 20 72 6f 77  il on a NULL row
280b3 20 6f 66 20 74 32 2c 20 77 68 69 63 68 0a 20 20   of t2, which.  
280b4 2a 2a 20 65 66 66 65 63 74 69 76 65 6c 79 20 63  ** effectively c
280b5 6f 6e 76 65 72 74 73 20 74 68 65 20 4f 55 54 45  onverts the OUTE
280b6 52 20 4a 4f 49 4e 20 69 6e 74 6f 20 61 6e 20 49  R JOIN into an I
280b7 4e 4e 45 52 20 4a 4f 49 4e 2e 0a 20 20 2a 2a 0a  NNER JOIN..  **.
280b8 20 20 2a 2a 20 54 48 49 53 20 4f 56 45 52 52 49    ** THIS OVERRI
280b9 44 45 53 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d  DES OBSOLETE COM
280ba 4d 45 4e 54 53 20 31 20 41 4e 44 20 32 20 41 42  MENTS 1 AND 2 AB
280bb 4f 56 45 3a 0a 20 20 2a 2a 20 54 69 63 6b 65 74  OVE:.  ** Ticket
280bc 20 23 33 33 30 30 20 73 68 6f 77 73 20 74 68 61   #3300 shows tha
280bd 74 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65  t flattening the
280be 20 72 69 67 68 74 20 74 65 72 6d 20 6f 66 20 61   right term of a
280bf 20 4c 45 46 54 20 4a 4f 49 4e 0a 20 20 2a 2a 20   LEFT JOIN.  ** 
280c0 69 73 20 66 72 61 75 67 68 74 20 77 69 74 68 20  is fraught with 
280c1 64 61 6e 67 65 72 2e 20 20 42 65 73 74 20 74 6f  danger.  Best to
280c2 20 61 76 6f 69 64 20 74 68 65 20 77 68 6f 6c 65   avoid the whole
280c3 20 74 68 69 6e 67 2e 20 20 49 66 20 74 68 65 0a   thing.  If the.
280c4 20 20 2a 2a 20 73 75 62 71 75 65 72 79 20 69 73    ** subquery is
280c5 20 74 68 65 20 72 69 67 68 74 20 74 65 72 6d 20   the right term 
280c6 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2c 20  of a LEFT JOIN, 
280c7 74 68 65 6e 20 64 6f 20 6e 6f 74 20 66 6c 61 74  then do not flat
280c8 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ten..  */.  if( 
280c9 28 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74  (pSubitem->joint
280ca 79 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21  ype & JT_OUTER)!
280cb 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
280cc 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65   0;.  }..  /* Re
280cd 73 74 72 69 63 74 69 6f 6e 20 31 37 3a 20 49 66  striction 17: If
280ce 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69   the sub-query i
280cf 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  s a compound SEL
280d0 45 43 54 2c 20 74 68 65 6e 20 69 74 20 6d 75 73  ECT, then it mus
280d1 74 0a 20 20 2a 2a 20 75 73 65 20 6f 6e 6c 79 20  t.  ** use only 
280d2 74 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70  the UNION ALL op
280d3 65 72 61 74 6f 72 2e 20 41 6e 64 20 6e 6f 6e 65  erator. And none
280d4 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 20 73   of the simple s
280d5 65 6c 65 63 74 20 71 75 65 72 69 65 73 0a 20 20  elect queries.  
280d6 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20  ** that make up 
280d7 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  the compound SEL
280d8 45 43 54 20 61 72 65 20 61 6c 6c 6f 77 65 64 20  ECT are allowed 
280d9 74 6f 20 62 65 20 61 67 67 72 65 67 61 74 65 20  to be aggregate 
280da 6f 72 20 64 69 73 74 69 6e 63 74 0a 20 20 2a 2a  or distinct.  **
280db 20 71 75 65 72 69 65 73 2e 0a 20 20 2a 2f 0a 20   queries..  */. 
280dc 20 69 66 28 20 70 53 75 62 2d 3e 70 50 72 69 6f   if( pSub->pPrio
280dd 72 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73 41  r ){.    if( isA
280de 67 67 20 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61  gg || (p->selFla
280df 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74  gs & SF_Distinct
280e0 29 21 3d 30 20 7c 7c 20 70 53 72 63 2d 3e 6e 53  )!=0 || pSrc->nS
280e1 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72  rc!=1 ){.      r
280e2 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
280e3 20 20 20 66 6f 72 28 70 53 75 62 31 3d 70 53 75     for(pSub1=pSu
280e4 62 3b 20 70 53 75 62 31 3b 20 70 53 75 62 31 3d  b; pSub1; pSub1=
280e5 70 53 75 62 31 2d 3e 70 50 72 69 6f 72 29 7b 0a  pSub1->pPrior){.
280e6 20 20 20 20 20 20 69 66 28 20 28 70 53 75 62 31        if( (pSub1
280e7 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46  ->selFlags & (SF
280e8 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67  _Distinct|SF_Agg
280e9 72 65 67 61 74 65 29 29 21 3d 30 0a 20 20 20 20  regate))!=0.    
280ea 20 20 20 7c 7c 20 28 70 53 75 62 31 2d 3e 70 50     || (pSub1->pP
280eb 72 69 6f 72 20 26 26 20 70 53 75 62 31 2d 3e 6f  rior && pSub1->o
280ec 70 21 3d 54 4b 5f 41 4c 4c 29 20 0a 20 20 20 20  p!=TK_ALL) .    
280ed 20 20 20 7c 7c 20 21 70 53 75 62 31 2d 3e 70 53     || !pSub1->pS
280ee 72 63 20 7c 7c 20 70 53 75 62 31 2d 3e 70 53 72  rc || pSub1->pSr
280ef 63 2d 3e 6e 53 72 63 21 3d 31 0a 20 20 20 20 20  c->nSrc!=1.     
280f0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
280f1 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
280f2 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 74    }..    /* Rest
280f3 72 69 63 74 69 6f 6e 20 31 38 2e 20 2a 2f 0a 20  riction 18. */. 
280f4 20 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72     if( p->pOrder
280f5 42 79 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  By ){.      int 
280f6 69 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69  ii;.      for(ii
280f7 3d 30 3b 20 69 69 3c 70 2d 3e 70 4f 72 64 65 72  =0; ii<p->pOrder
280f8 42 79 2d 3e 6e 45 78 70 72 3b 20 69 69 2b 2b 29  By->nExpr; ii++)
280f9 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d  {.        if( p-
280fa 3e 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 69 5d  >pOrderBy->a[ii]
280fb 2e 69 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72  .iCol==0 ) retur
280fc 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
280fd 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 2a 2a 2a 2a   }.  }..  /*****
280fe 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69   If we reach thi
280ff 73 20 70 6f 69 6e 74 2c 20 66 6c 61 74 74 65 6e  s point, flatten
28100 69 6e 67 20 69 73 20 70 65 72 6d 69 74 74 65 64  ing is permitted
28101 2e 20 2a 2a 2a 2a 2a 2f 0a 0a 20 20 2f 2a 20 41  . *****/..  /* A
28102 75 74 68 6f 72 69 7a 65 20 74 68 65 20 73 75 62  uthorize the sub
28103 71 75 65 72 79 20 2a 2f 0a 20 20 70 50 61 72 73  query */.  pPars
28104 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20  e->zAuthContext 
28105 3d 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d  = pSubitem->zNam
28106 65 3b 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68  e;.  sqlite3Auth
28107 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
28108 4c 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20  LITE_SELECT, 0, 
28109 30 2c 20 30 29 3b 0a 20 20 70 50 61 72 73 65 2d  0, 0);.  pParse-
2810a 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20  >zAuthContext = 
2810b 7a 53 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78  zSavedAuthContex
2810c 74 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  t;..  /* If the 
2810d 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63  sub-query is a c
2810e 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73  ompound SELECT s
2810f 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 28  tatement, then (
28110 62 79 20 72 65 73 74 72 69 63 74 69 6f 6e 73 0a  by restrictions.
28111 20 20 2a 2a 20 31 37 20 61 6e 64 20 31 38 20 61    ** 17 and 18 a
28112 62 6f 76 65 29 20 69 74 20 6d 75 73 74 20 62 65  bove) it must be
28113 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 61 6e 64   a UNION ALL and
28114 20 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72   the parent quer
28115 79 20 6d 75 73 74 20 0a 20 20 2a 2a 20 62 65 20  y must .  ** be 
28116 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a  of the form:.  *
28117 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43  *.  **     SELEC
28118 54 20 3c 65 78 70 72 2d 6c 69 73 74 3e 20 46 52  T <expr-list> FR
28119 4f 4d 20 28 3c 73 75 62 2d 71 75 65 72 79 3e 29  OM (<sub-query>)
2811a 20 3c 77 68 65 72 65 2d 63 6c 61 75 73 65 3e 20   <where-clause> 
2811b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 66 6f 6c 6c 6f  .  **.  ** follo
2811c 77 65 64 20 62 79 20 61 6e 79 20 4f 52 44 45 52  wed by any ORDER
2811d 20 42 59 2c 20 4c 49 4d 49 54 20 61 6e 64 2f 6f   BY, LIMIT and/o
2811e 72 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73  r OFFSET clauses
2811f 2e 20 54 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a  . This block.  *
28120 2a 20 63 72 65 61 74 65 73 20 4e 2d 31 20 63 6f  * creates N-1 co
28121 70 69 65 73 20 6f 66 20 74 68 65 20 70 61 72 65  pies of the pare
28122 6e 74 20 71 75 65 72 79 20 77 69 74 68 6f 75 74  nt query without
28123 20 61 6e 79 20 4f 52 44 45 52 20 42 59 2c 20 4c   any ORDER BY, L
28124 49 4d 49 54 20 6f 72 20 0a 20 20 2a 2a 20 4f 46  IMIT or .  ** OF
28125 46 53 45 54 20 63 6c 61 75 73 65 73 20 61 6e 64  FSET clauses and
28126 20 6a 6f 69 6e 73 20 74 68 65 6d 20 74 6f 20 74   joins them to t
28127 68 65 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64  he left-hand-sid
28128 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  e of the origina
28129 6c 0a 20 20 2a 2a 20 75 73 69 6e 67 20 55 4e 49  l.  ** using UNI
2812a 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 73  ON ALL operators
2812b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 4e  . In this case N
2812c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
2812d 66 20 73 69 6d 70 6c 65 0a 20 20 2a 2a 20 73 65  f simple.  ** se
2812e 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73 20  lect statements 
2812f 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  in the compound 
28130 73 75 62 2d 71 75 65 72 79 2e 0a 20 20 2a 2a 0a  sub-query..  **.
28131 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20    ** Example:.  
28132 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  **.  **     SELE
28133 43 54 20 61 2b 31 20 46 52 4f 4d 20 28 0a 20 20  CT a+1 FROM (.  
28134 2a 2a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54  **        SELECT
28135 20 78 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a   x FROM tab.  **
28136 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c          UNION AL
28137 4c 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45  L.  **        SE
28138 4c 45 43 54 20 79 20 46 52 4f 4d 20 74 61 62 0a  LECT y FROM tab.
28139 20 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49 4f    **        UNIO
2813a 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20  N ALL.  **      
2813b 20 20 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32    SELECT abs(z*2
2813c 29 20 46 52 4f 4d 20 74 61 62 32 0a 20 20 2a 2a  ) FROM tab2.  **
2813d 20 20 20 20 20 29 20 57 48 45 52 45 20 61 21 3d       ) WHERE a!=
2813e 35 20 4f 52 44 45 52 20 42 59 20 31 0a 20 20 2a  5 ORDER BY 1.  *
2813f 2a 0a 20 20 2a 2a 20 54 72 61 6e 73 66 6f 72 6d  *.  ** Transform
28140 65 64 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20  ed into:.  **.  
28141 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b  **     SELECT x+
28142 31 20 46 52 4f 4d 20 74 61 62 20 57 48 45 52 45  1 FROM tab WHERE
28143 20 78 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20   x+1!=5.  **    
28144 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20   UNION ALL.  ** 
28145 20 20 20 20 53 45 4c 45 43 54 20 79 2b 31 20 46      SELECT y+1 F
28146 52 4f 4d 20 74 61 62 20 57 48 45 52 45 20 79 2b  ROM tab WHERE y+
28147 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e  1!=5.  **     UN
28148 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20  ION ALL.  **    
28149 20 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29   SELECT abs(z*2)
2814a 2b 31 20 46 52 4f 4d 20 74 61 62 32 20 57 48 45  +1 FROM tab2 WHE
2814b 52 45 20 61 62 73 28 7a 2a 32 29 2b 31 21 3d 35  RE abs(z*2)+1!=5
2814c 0a 20 20 2a 2a 20 20 20 20 20 4f 52 44 45 52 20  .  **     ORDER 
2814d 42 59 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  BY 1.  **.  ** W
2814e 65 20 63 61 6c 6c 20 74 68 69 73 20 74 68 65 20  e call this the 
2814f 22 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65  "compound-subque
28150 72 79 20 66 6c 61 74 74 65 6e 69 6e 67 22 2e 0a  ry flattening"..
28151 20 20 2a 2f 0a 20 20 66 6f 72 28 70 53 75 62 3d    */.  for(pSub=
28152 70 53 75 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53  pSub->pPrior; pS
28153 75 62 3b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70  ub; pSub=pSub->p
28154 50 72 69 6f 72 29 7b 0a 20 20 20 20 53 65 6c 65  Prior){.    Sele
28155 63 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 45 78  ct *pNew;.    Ex
28156 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
28157 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a   = p->pOrderBy;.
28158 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74      Expr *pLimit
28159 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20   = p->pLimit;.  
2815a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72    Select *pPrior
2815b 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
2815c 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20    p->pOrderBy = 
2815d 30 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d  0;.    p->pSrc =
2815e 20 30 3b 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f   0;.    p->pPrio
2815f 72 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c  r = 0;.    p->pL
28160 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 70 4e  imit = 0;.    pN
28161 65 77 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ew = sqlite3Sele
28162 63 74 44 75 70 28 64 62 2c 20 70 2c 20 30 29 3b  ctDup(db, p, 0);
28163 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d  .    p->pLimit =
28164 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70 2d 3e   pLimit;.    p->
28165 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65  pOrderBy = pOrde
28166 72 42 79 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63  rBy;.    p->pSrc
28167 20 3d 20 70 53 72 63 3b 0a 20 20 20 20 70 2d 3e   = pSrc;.    p->
28168 6f 70 20 3d 20 54 4b 5f 41 4c 4c 3b 0a 20 20 20  op = TK_ALL;.   
28169 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d   p->pRightmost =
2816a 20 30 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77   0;.    if( pNew
2816b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 65  ==0 ){.      pNe
2816c 77 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20  w = pPrior;.    
2816d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e 65  }else{.      pNe
2816e 77 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  w->pPrior = pPri
2816f 6f 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  or;.      pNew->
28170 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a  pRightmost = 0;.
28171 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50 72      }.    p->pPr
28172 69 6f 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ior = pNew;.    
28173 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
28174 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 31 3b  iled ) return 1;
28175 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  .  }..  /* Begin
28176 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20   flattening the 
28177 69 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79 20 6f  iFrom-th entry o
28178 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
28179 65 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f  e .  ** in the o
2817a 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20 2a 2f  uter query..  */
2817b 0a 20 20 70 53 75 62 20 3d 20 70 53 75 62 31 20  .  pSub = pSub1 
2817c 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c  = pSubitem->pSel
2817d 65 63 74 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74  ect;..  /* Delet
2817e 65 20 74 68 65 20 74 72 61 6e 73 69 65 6e 74 20  e the transient 
2817f 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  table structure 
28180 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
28181 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72  the.  ** subquer
28182 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  y.  */.  sqlite3
28183 44 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69  DbFree(db, pSubi
28184 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  tem->zDatabase);
28185 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
28186 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a  (db, pSubitem->z
28187 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
28188 44 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69  DbFree(db, pSubi
28189 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20  tem->zAlias);.  
2818a 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62  pSubitem->zDatab
2818b 61 73 65 20 3d 20 30 3b 0a 20 20 70 53 75 62 69  ase = 0;.  pSubi
2818c 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a  tem->zName = 0;.
2818d 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69    pSubitem->zAli
2818e 61 73 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74  as = 0;.  pSubit
2818f 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 30 3b  em->pSelect = 0;
28190 0a 0a 20 20 2f 2a 20 44 65 66 65 72 20 64 65 6c  ..  /* Defer del
28191 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20  eting the Table 
28192 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65  object associate
28193 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20  d with the.  ** 
28194 73 75 62 71 75 65 72 79 20 75 6e 74 69 6c 20 63  subquery until c
28195 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 69  ode generation i
28196 73 0a 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 2c  s.  ** complete,
28197 20 73 69 6e 63 65 20 74 68 65 72 65 20 6d 61 79   since there may
28198 20 73 74 69 6c 6c 20 65 78 69 73 74 20 45 78 70   still exist Exp
28199 72 2e 70 54 61 62 20 65 6e 74 72 69 65 73 20 74  r.pTab entries t
2819a 68 61 74 0a 20 20 2a 2a 20 72 65 66 65 72 20 74  hat.  ** refer t
2819b 6f 20 74 68 65 20 73 75 62 71 75 65 72 79 20 65  o the subquery e
2819c 76 65 6e 20 61 66 74 65 72 20 66 6c 61 74 74 65  ven after flatte
2819d 6e 69 6e 67 2e 20 20 54 69 63 6b 65 74 20 23 33  ning.  Ticket #3
2819e 33 34 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  346..  */.  if( 
2819f 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 21 3d  pSubitem->pTab!=
281a0 30 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  0 ){.    Table *
281a1 70 54 61 62 54 6f 44 65 6c 20 3d 20 70 53 75 62  pTabToDel = pSub
281a2 69 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  item->pTab;.    
281a3 69 66 28 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e  if( pTabToDel->n
281a4 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  Ref==1 ){.      
281a5 70 54 61 62 54 6f 44 65 6c 2d 3e 70 4e 65 78 74  pTabToDel->pNext
281a6 5a 6f 6d 62 69 65 20 3d 20 70 50 61 72 73 65 2d  Zombie = pParse-
281a7 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a 20 20 20  >pZombieTab;.   
281a8 20 20 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62     pParse->pZomb
281a9 69 65 54 61 62 20 3d 20 70 54 61 62 54 6f 44 65  ieTab = pTabToDe
281aa 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  l;.    }else{.  
281ab 20 20 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e      pTabToDel->n
281ac 52 65 66 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20  Ref--;.    }.   
281ad 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 20   pSubitem->pTab 
281ae 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  = 0;.  }..  /* T
281af 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f  he following loo
281b0 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20  p runs once for 
281b1 65 61 63 68 20 74 65 72 6d 20 69 6e 20 61 20 63  each term in a c
281b2 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79  ompound-subquery
281b3 0a 20 20 2a 2a 20 66 6c 61 74 74 65 6e 69 6e 67  .  ** flattening
281b4 20 28 61 73 20 64 65 73 63 72 69 62 65 64 20 61   (as described a
281b5 62 6f 76 65 29 2e 20 20 49 66 20 77 65 20 61 72  bove).  If we ar
281b6 65 20 64 6f 69 6e 67 20 61 20 64 69 66 66 65 72  e doing a differ
281b7 65 6e 74 20 6b 69 6e 64 0a 20 20 2a 2a 20 6f 66  ent kind.  ** of
281b8 20 66 6c 61 74 74 65 6e 69 6e 67 20 2d 20 61 20   flattening - a 
281b9 66 6c 61 74 74 65 6e 69 6e 67 20 6f 74 68 65 72  flattening other
281ba 20 74 68 61 6e 20 61 20 63 6f 6d 70 6f 75 6e 64   than a compound
281bb 2d 73 75 62 71 75 65 72 79 20 66 6c 61 74 74 65  -subquery flatte
281bc 6e 69 6e 67 20 2d 0a 20 20 2a 2a 20 74 68 65 6e  ning -.  ** then
281bd 20 74 68 69 73 20 6c 6f 6f 70 20 6f 6e 6c 79 20   this loop only 
281be 72 75 6e 73 20 6f 6e 63 65 2e 0a 20 20 2a 2a 0a  runs once..  **.
281bf 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 6d    ** This loop m
281c0 6f 76 65 73 20 61 6c 6c 20 6f 66 20 74 68 65 20  oves all of the 
281c1 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 73 20 6f 66  FROM elements of
281c2 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 6e   the subquery in
281c3 74 6f 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 20  to the.  ** the 
281c4 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
281c5 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20  he outer query. 
281c6 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 74 68   Before doing th
281c7 69 73 2c 20 72 65 6d 65 6d 62 65 72 0a 20 20 2a  is, remember.  *
281c8 2a 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  * the cursor num
281c9 62 65 72 20 66 6f 72 20 74 68 65 20 6f 72 69 67  ber for the orig
281ca 69 6e 61 6c 20 6f 75 74 65 72 20 71 75 65 72 79  inal outer query
281cb 20 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 20 69 6e   FROM element in
281cc 0a 20 20 2a 2a 20 69 50 61 72 65 6e 74 2e 20 20  .  ** iParent.  
281cd 54 68 65 20 69 50 61 72 65 6e 74 20 63 75 72 73  The iParent curs
281ce 6f 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  or will never be
281cf 20 75 73 65 64 2e 20 20 53 75 62 73 65 71 75 65   used.  Subseque
281d0 6e 74 20 63 6f 64 65 0a 20 20 2a 2a 20 77 69 6c  nt code.  ** wil
281d1 6c 20 73 63 61 6e 20 65 78 70 72 65 73 73 69 6f  l scan expressio
281d2 6e 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 69  ns looking for i
281d3 50 61 72 65 6e 74 20 72 65 66 65 72 65 6e 63 65  Parent reference
281d4 73 20 61 6e 64 20 72 65 70 6c 61 63 65 0a 20 20  s and replace.  
281d5 2a 2a 20 74 68 6f 73 65 20 72 65 66 65 72 65 6e  ** those referen
281d6 63 65 73 20 77 69 74 68 20 65 78 70 72 65 73 73  ces with express
281d7 69 6f 6e 73 20 74 68 61 74 20 72 65 73 6f 6c 76  ions that resolv
281d8 65 20 74 6f 20 74 68 65 20 73 75 62 71 75 65 72  e to the subquer
281d9 79 20 46 52 4f 4d 0a 20 20 2a 2a 20 65 6c 65 6d  y FROM.  ** elem
281da 65 6e 74 73 20 77 65 20 61 72 65 20 6e 6f 77 20  ents we are now 
281db 63 6f 70 79 69 6e 67 20 69 6e 2e 0a 20 20 2a 2f  copying in..  */
281dc 0a 20 20 66 6f 72 28 70 50 61 72 65 6e 74 3d 70  .  for(pParent=p
281dd 3b 20 70 50 61 72 65 6e 74 3b 20 70 50 61 72 65  ; pParent; pPare
281de 6e 74 3d 70 50 61 72 65 6e 74 2d 3e 70 50 72 69  nt=pParent->pPri
281df 6f 72 2c 20 70 53 75 62 3d 70 53 75 62 2d 3e 70  or, pSub=pSub->p
281e0 50 72 69 6f 72 29 7b 0a 20 20 20 20 69 6e 74 20  Prior){.    int 
281e1 6e 53 75 62 53 72 63 3b 0a 20 20 20 20 75 38 20  nSubSrc;.    u8 
281e2 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20  jointype = 0;.  
281e3 20 20 70 53 75 62 53 72 63 20 3d 20 70 53 75 62    pSubSrc = pSub
281e4 2d 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a 20 46  ->pSrc;     /* F
281e5 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 73 75  ROM clause of su
281e6 62 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 6e 53  bquery */.    nS
281e7 75 62 53 72 63 20 3d 20 70 53 75 62 53 72 63 2d  ubSrc = pSubSrc-
281e8 3e 6e 53 72 63 3b 20 20 2f 2a 20 4e 75 6d 62 65  >nSrc;  /* Numbe
281e9 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 73 75  r of terms in su
281ea 62 71 75 65 72 79 20 46 52 4f 4d 20 63 6c 61 75  bquery FROM clau
281eb 73 65 20 2a 2f 0a 20 20 20 20 70 53 72 63 20 3d  se */.    pSrc =
281ec 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 3b 20   pParent->pSrc; 
281ed 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75      /* FROM clau
281ee 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20  se of the outer 
281ef 71 75 65 72 79 20 2a 2f 0a 0a 20 20 20 20 69 66  query */..    if
281f0 28 20 70 53 72 63 20 29 7b 0a 20 20 20 20 20 20  ( pSrc ){.      
281f1 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 3d  assert( pParent=
281f2 3d 70 20 29 3b 20 20 2f 2a 20 46 69 72 73 74 20  =p );  /* First 
281f3 74 69 6d 65 20 74 68 72 6f 75 67 68 20 74 68 65  time through the
281f4 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 6a   loop */.      j
281f5 6f 69 6e 74 79 70 65 20 3d 20 70 53 75 62 69 74  ointype = pSubit
281f6 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20  em->jointype;.  
281f7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
281f8 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 21 3d  ssert( pParent!=
281f9 70 20 29 3b 20 20 2f 2a 20 32 6e 64 20 61 6e 64  p );  /* 2nd and
281fa 20 73 75 62 73 65 71 75 65 6e 74 20 74 69 6d 65   subsequent time
281fb 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 6f  s through the lo
281fc 6f 70 20 2a 2f 0a 20 20 20 20 20 20 70 53 72 63  op */.      pSrc
281fd 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63   = pParent->pSrc
281fe 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
281ff 74 41 70 70 65 6e 64 28 64 62 2c 20 30 2c 20 30  tAppend(db, 0, 0
28200 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
28201 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pSrc==0 ){.     
28202 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d     assert( db->m
28203 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
28204 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
28205 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
28206 20 2f 2a 20 54 68 65 20 73 75 62 71 75 65 72 79   /* The subquery
28207 20 75 73 65 73 20 61 20 73 69 6e 67 6c 65 20 73   uses a single s
28208 6c 6f 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  lot of the FROM 
28209 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75  clause of the ou
2820a 74 65 72 0a 20 20 20 20 2a 2a 20 71 75 65 72 79  ter.    ** query
2820b 2e 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65  .  If the subque
2820c 72 79 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e  ry has more than
2820d 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 20 69 6e 20   one element in 
2820e 69 74 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c  its FROM clause,
2820f 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 65 78 70  .    ** then exp
28210 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 75  and the outer qu
28211 65 72 79 20 74 6f 20 6d 61 6b 65 20 73 70 61 63  ery to make spac
28212 65 20 66 6f 72 20 69 74 20 74 6f 20 68 6f 6c 64  e for it to hold
28213 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 0a 20 20   all elements.  
28214 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 71    ** of the subq
28215 75 65 72 79 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  uery..    **.   
28216 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 20   ** Example:.   
28217 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 53 45   **.    **    SE
28218 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 41  LECT * FROM tabA
28219 2c 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  , (SELECT * FROM
2821a 20 73 75 62 31 2c 20 73 75 62 32 29 2c 20 74 61   sub1, sub2), ta
2821b 62 42 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  bB;.    **.    *
2821c 2a 20 54 68 65 20 6f 75 74 65 72 20 71 75 65 72  * The outer quer
2821d 79 20 68 61 73 20 33 20 73 6c 6f 74 73 20 69 6e  y has 3 slots in
2821e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61 75 73 65   its FROM clause
2821f 2e 20 20 4f 6e 65 20 73 6c 6f 74 20 6f 66 20 74  .  One slot of t
28220 68 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20  he.    ** outer 
28221 71 75 65 72 79 20 28 74 68 65 20 6d 69 64 64 6c  query (the middl
28222 65 20 73 6c 6f 74 29 20 69 73 20 75 73 65 64 20  e slot) is used 
28223 62 79 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  by the subquery.
28224 20 20 54 68 65 20 6e 65 78 74 0a 20 20 20 20 2a    The next.    *
28225 2a 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20  * block of code 
28226 77 69 6c 6c 20 65 78 70 61 6e 64 20 74 68 65 20  will expand the 
28227 6f 75 74 20 71 75 65 72 79 20 74 6f 20 34 20 73  out query to 4 s
28228 6c 6f 74 73 2e 20 20 54 68 65 20 6d 69 64 64 6c  lots.  The middl
28229 65 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20 69 73  e.    ** slot is
2822a 20 65 78 70 61 6e 64 65 64 20 74 6f 20 74 77 6f   expanded to two
2822b 20 73 6c 6f 74 73 20 69 6e 20 6f 72 64 65 72 20   slots in order 
2822c 74 6f 20 6d 61 6b 65 20 73 70 61 63 65 20 66 6f  to make space fo
2822d 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 77 6f  r the.    ** two
2822e 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65   elements in the
2822f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
28230 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20  the subquery..  
28231 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 53 75    */.    if( nSu
28232 62 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20  bSrc>1 ){.      
28233 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 20 3d 20  pParent->pSrc = 
28234 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72  pSrc = sqlite3Sr
28235 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c  cListEnlarge(db,
28236 20 70 53 72 63 2c 20 6e 53 75 62 53 72 63 2d 31   pSrc, nSubSrc-1
28237 2c 69 46 72 6f 6d 2b 31 29 3b 0a 20 20 20 20 20  ,iFrom+1);.     
28238 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
28239 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
2823a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2823b 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 72      }..    /* Tr
2823c 61 6e 73 66 65 72 20 74 68 65 20 46 52 4f 4d 20  ansfer the FROM 
2823d 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66 72 6f  clause terms fro
2823e 6d 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69  m the subquery i
2823f 6e 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f  nto the.    ** o
28240 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20 20 20  uter query..    
28241 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
28242 69 3c 6e 53 75 62 53 72 63 3b 20 69 2b 2b 29 7b  i<nSubSrc; i++){
28243 0a 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 69  .      pSrc->a[i
28244 2b 69 46 72 6f 6d 5d 20 3d 20 70 53 75 62 53 72  +iFrom] = pSubSr
28245 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 6d  c->a[i];.      m
28246 65 6d 73 65 74 28 26 70 53 75 62 53 72 63 2d 3e  emset(&pSubSrc->
28247 61 5b 69 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  a[i], 0, sizeof(
28248 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 29 29 3b  pSubSrc->a[i]));
28249 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 72 63 2d  .    }.    pSrc-
2824a 3e 61 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79  >a[iFrom].jointy
2824b 70 65 20 3d 20 6a 6f 69 6e 74 79 70 65 3b 0a 20  pe = jointype;. 
2824c 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 62 65 67   .    /* Now beg
2824d 69 6e 20 73 75 62 73 74 69 74 75 74 69 6e 67 20  in substituting 
2824e 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 20  subquery result 
2824f 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20  set expressions 
28250 66 6f 72 20 0a 20 20 20 20 2a 2a 20 72 65 66 65  for .    ** refe
28251 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 69 50  rences to the iP
28252 61 72 65 6e 74 20 69 6e 20 74 68 65 20 6f 75 74  arent in the out
28253 65 72 20 71 75 65 72 79 2e 0a 20 20 20 20 2a 2a  er query..    **
28254 20 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65   .    ** Example
28255 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
28256 20 20 53 45 4c 45 43 54 20 61 2b 35 2c 20 62 2a    SELECT a+5, b*
28257 31 30 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20  10 FROM (SELECT 
28258 78 2a 33 20 41 53 20 61 2c 20 79 2b 31 30 20 41  x*3 AS a, y+10 A
28259 53 20 62 20 46 52 4f 4d 20 74 31 29 20 57 48 45  S b FROM t1) WHE
2825a 52 45 20 61 3e 62 3b 0a 20 20 20 20 2a 2a 20 20  RE a>b;.    **  
2825b 20 5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20   \              
2825c 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f         \________
2825d 5f 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79 20 5f  _____ subquery _
2825e 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20  _________/      
2825f 20 20 20 20 2f 0a 20 20 20 20 2a 2a 20 20 20 20      /.    **    
28260 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  \_______________
28261 5f 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20 71 75 65  ______ outer que
28262 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ry _____________
28263 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
28264 5f 2f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  _/.    **.    **
28265 20 57 65 20 6c 6f 6f 6b 20 61 74 20 65 76 65 72   We look at ever
28266 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  y expression in 
28267 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
28268 61 6e 64 20 65 76 65 72 79 20 70 6c 61 63 65 20  and every place 
28269 77 65 20 73 65 65 0a 20 20 20 20 2a 2a 20 22 61  we see.    ** "a
2826a 22 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20  " we substitute 
2826b 22 78 2a 33 22 20 61 6e 64 20 65 76 65 72 79 20  "x*3" and every 
2826c 70 6c 61 63 65 20 77 65 20 73 65 65 20 22 62 22  place we see "b"
2826d 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22   we substitute "
2826e 79 2b 31 30 22 2e 0a 20 20 20 20 2a 2f 0a 20 20  y+10"..    */.  
2826f 20 20 70 4c 69 73 74 20 3d 20 70 50 61 72 65 6e    pList = pParen
28270 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 66  t->pEList;.    f
28271 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
28272 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
28273 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b      Expr *pExpr;
28274 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74  .      if( pList
28275 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20  ->a[i].zName==0 
28276 26 26 20 28 70 45 78 70 72 20 3d 20 70 4c 69 73  && (pExpr = pLis
28277 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 2d 3e  t->a[i].pExpr)->
28278 73 70 61 6e 2e 7a 21 3d 30 20 29 7b 0a 20 20 20  span.z!=0 ){.   
28279 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d       pList->a[i]
2827a 2e 7a 4e 61 6d 65 20 3d 20 0a 20 20 20 20 20 20  .zName = .      
2827b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2827c 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
2827d 68 61 72 2a 29 70 45 78 70 72 2d 3e 73 70 61 6e  har*)pExpr->span
2827e 2e 7a 2c 20 70 45 78 70 72 2d 3e 73 70 61 6e 2e  .z, pExpr->span.
2827f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
28280 7d 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 4c  }.    substExprL
28281 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  ist(db, pParent-
28282 3e 70 45 4c 69 73 74 2c 20 69 50 61 72 65 6e 74  >pEList, iParent
28283 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
28284 0a 20 20 20 20 69 66 28 20 69 73 41 67 67 20 29  .    if( isAgg )
28285 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  {.      substExp
28286 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e  rList(db, pParen
28287 74 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69 50 61  t->pGroupBy, iPa
28288 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
28289 73 74 29 3b 0a 20 20 20 20 20 20 73 75 62 73 74  st);.      subst
2828a 45 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74  Expr(db, pParent
2828b 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72 65  ->pHaving, iPare
2828c 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74  nt, pSub->pEList
2828d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2828e 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20   pSub->pOrderBy 
2828f 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
28290 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72   pParent->pOrder
28291 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  By==0 );.      p
28292 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79  Parent->pOrderBy
28293 20 3d 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42   = pSub->pOrderB
28294 79 3b 0a 20 20 20 20 20 20 70 53 75 62 2d 3e 70  y;.      pSub->p
28295 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20  OrderBy = 0;.   
28296 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 65   }else if( pPare
28297 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  nt->pOrderBy ){.
28298 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72 4c        substExprL
28299 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  ist(db, pParent-
2829a 3e 70 4f 72 64 65 72 42 79 2c 20 69 50 61 72 65  >pOrderBy, iPare
2829b 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74  nt, pSub->pEList
2829c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2829d 20 70 53 75 62 2d 3e 70 57 68 65 72 65 20 29 7b   pSub->pWhere ){
2829e 0a 20 20 20 20 20 20 70 57 68 65 72 65 20 3d 20  .      pWhere = 
2829f 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
282a0 62 2c 20 70 53 75 62 2d 3e 70 57 68 65 72 65 2c  b, pSub->pWhere,
282a1 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
282a2 20 20 20 20 20 20 70 57 68 65 72 65 20 3d 20 30        pWhere = 0
282a3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
282a4 73 75 62 71 75 65 72 79 49 73 41 67 67 20 29 7b  subqueryIsAgg ){
282a5 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
282a6 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 3d  Parent->pHaving=
282a7 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72  =0 );.      pPar
282a8 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70  ent->pHaving = p
282a9 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 3b 0a  Parent->pWhere;.
282aa 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
282ab 57 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a  Where = pWhere;.
282ac 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72 28        substExpr(
282ad 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61  db, pParent->pHa
282ae 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70  ving, iParent, p
282af 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
282b0 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61      pParent->pHa
282b1 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78  ving = sqlite3Ex
282b2 70 72 41 6e 64 28 64 62 2c 20 70 50 61 72 65 6e  prAnd(db, pParen
282b3 74 2d 3e 70 48 61 76 69 6e 67 2c 20 0a 20 20 20  t->pHaving, .   
282b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
282b6 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
282b7 2c 20 70 53 75 62 2d 3e 70 48 61 76 69 6e 67 2c  , pSub->pHaving,
282b8 20 30 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65   0));.      asse
282b9 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 47 72  rt( pParent->pGr
282ba 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20  oupBy==0 );.    
282bb 20 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75    pParent->pGrou
282bc 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  pBy = sqlite3Exp
282bd 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 75  rListDup(db, pSu
282be 62 2d 3e 70 47 72 6f 75 70 42 79 2c 20 30 29 3b  b->pGroupBy, 0);
282bf 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
282c0 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20    substExpr(db, 
282c1 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c  pParent->pWhere,
282c2 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e   iParent, pSub->
282c3 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70  pEList);.      p
282c4 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d  Parent->pWhere =
282c5 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
282c6 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68  db, pParent->pWh
282c7 65 72 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  ere, pWhere);.  
282c8 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68    }.  .    /* Th
282c9 65 20 66 6c 61 74 74 65 6e 65 64 20 71 75 65 72  e flattened quer
282ca 79 20 69 73 20 64 69 73 74 69 6e 63 74 20 69 66  y is distinct if
282cb 20 65 69 74 68 65 72 20 74 68 65 20 69 6e 6e 65   either the inne
282cc 72 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  r or the.    ** 
282cd 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 64  outer query is d
282ce 69 73 74 69 6e 63 74 2e 20 0a 20 20 20 20 2a 2f  istinct. .    */
282cf 0a 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 73 65  .    pParent->se
282d0 6c 46 6c 61 67 73 20 7c 3d 20 70 53 75 62 2d 3e  lFlags |= pSub->
282d1 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
282d2 73 74 69 6e 63 74 3b 0a 20 20 0a 20 20 20 20 2f  stinct;.  .    /
282d3 2a 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20  *.    ** SELECT 
282d4 2e 2e 2e 20 46 52 4f 4d 20 28 53 45 4c 45 43 54  ... FROM (SELECT
282d5 20 2e 2e 2e 20 4c 49 4d 49 54 20 61 20 4f 46 46   ... LIMIT a OFF
282d6 53 45 54 20 62 29 20 4c 49 4d 49 54 20 78 20 4f  SET b) LIMIT x O
282d7 46 46 53 45 54 20 79 3b 0a 20 20 20 20 2a 2a 0a  FFSET y;.    **.
282d8 20 20 20 20 2a 2a 20 4f 6e 65 20 69 73 20 74 65      ** One is te
282d9 6d 70 74 65 64 20 74 6f 20 74 72 79 20 74 6f 20  mpted to try to 
282da 61 64 64 20 61 20 61 6e 64 20 62 20 74 6f 20 63  add a and b to c
282db 6f 6d 62 69 6e 65 20 74 68 65 20 6c 69 6d 69 74  ombine the limit
282dc 73 2e 20 20 42 75 74 20 74 68 69 73 0a 20 20 20  s.  But this.   
282dd 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72   ** does not wor
282de 6b 20 69 66 20 65 69 74 68 65 72 20 6c 69 6d 69  k if either limi
282df 74 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 20  t is negative.. 
282e0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53     */.    if( pS
282e1 75 62 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20  ub->pLimit ){.  
282e2 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 4c 69      pParent->pLi
282e3 6d 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c 69 6d  mit = pSub->pLim
282e4 69 74 3b 0a 20 20 20 20 20 20 70 53 75 62 2d 3e  it;.      pSub->
282e5 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
282e6 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69  }.  }..  /* Fini
282e7 61 6c 6c 79 2c 20 64 65 6c 65 74 65 20 77 68 61  ally, delete wha
282e8 74 20 69 73 20 6c 65 66 74 20 6f 66 20 74 68 65  t is left of the
282e9 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 72 65   subquery and re
282ea 74 75 72 6e 0a 20 20 2a 2a 20 73 75 63 63 65 73  turn.  ** succes
282eb 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  s..  */.  sqlite
282ec 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
282ed 2c 20 70 53 75 62 31 29 3b 0a 0a 20 20 72 65 74  , pSub1);..  ret
282ee 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 1;.}.#endif 
282ef 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
282f0 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
282f1 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
282f2 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
282f3 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a  */../*.** Analyz
282f4 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  e the SELECT sta
282f5 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73  tement passed as
282f6 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
282f7 73 65 65 20 69 66 20 69 74 0a 2a 2a 20 69 73 20  see if it.** is 
282f8 61 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29  a min() or max()
282f9 20 71 75 65 72 79 2e 20 52 65 74 75 72 6e 20 57   query. Return W
282fa 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
282fb 20 6f 72 20 57 48 45 52 45 5f 4f 52 44 45 52 42   or WHERE_ORDERB
282fc 59 5f 4d 41 58 20 69 66 20 0a 2a 2a 20 69 74 20  Y_MAX if .** it 
282fd 69 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69  is, or 0 otherwi
282fe 73 65 2e 20 41 74 20 70 72 65 73 65 6e 74 2c 20  se. At present, 
282ff 61 20 71 75 65 72 79 20 69 73 20 63 6f 6e 73 69  a query is consi
28300 64 65 72 65 64 20 74 6f 20 62 65 0a 2a 2a 20 61  dered to be.** a
28301 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20 71 75 65   min()/max() que
28302 72 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ry if:.**.**   1
28303 2e 20 54 68 65 72 65 20 69 73 20 61 20 73 69 6e  . There is a sin
28304 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e 20 74 68  gle object in th
28305 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a  e FROM clause..*
28306 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65 72 65 20  *.**   2. There 
28307 69 73 20 61 20 73 69 6e 67 6c 65 20 65 78 70 72  is a single expr
28308 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 65  ession in the re
28309 73 75 6c 74 20 73 65 74 2c 20 61 6e 64 20 69 74  sult set, and it
2830a 20 69 73 0a 2a 2a 20 20 20 20 20 20 65 69 74 68   is.**      eith
2830b 65 72 20 6d 69 6e 28 78 29 20 6f 72 20 6d 61 78  er min(x) or max
2830c 28 78 29 2c 20 77 68 65 72 65 20 78 20 69 73 20  (x), where x is 
2830d 61 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e  a column referen
2830e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38  ce..*/.static u8
2830f 20 6d 69 6e 4d 61 78 51 75 65 72 79 28 53 65 6c   minMaxQuery(Sel
28310 65 63 74 20 2a 70 29 7b 0a 20 20 45 78 70 72 20  ect *p){.  Expr 
28311 2a 70 45 78 70 72 3b 0a 20 20 45 78 70 72 4c 69  *pExpr;.  ExprLi
28312 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 70 2d 3e  st *pEList = p->
28313 70 45 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70  pEList;..  if( p
28314 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20  EList->nExpr!=1 
28315 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f  ) return WHERE_O
28316 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 20  RDERBY_NORMAL;. 
28317 20 70 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d   pExpr = pEList-
28318 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69  >a[0].pExpr;.  i
28319 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
2831a 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
2831b 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e  Select) ) return
2831c 20 30 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70   0;.  pEList = p
2831d 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20  Expr->x.pList;. 
2831e 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2831f 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
28320 7c 7c 20 70 45 4c 69 73 74 3d 3d 30 20 7c 7c 20  || pEList==0 || 
28321 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
28322 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
28323 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  f( pEList->a[0].
28324 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47  pExpr->op!=TK_AG
28325 47 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72  G_COLUMN ) retur
28326 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  n WHERE_ORDERBY_
28327 4e 4f 52 4d 41 4c 3b 0a 20 20 69 66 28 20 70 45  NORMAL;.  if( pE
28328 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 33 20  xpr->token.n!=3 
28329 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f  ) return WHERE_O
2832a 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 20  RDERBY_NORMAL;. 
2832b 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
2832c 49 43 6d 70 28 28 63 68 61 72 2a 29 70 45 78 70  ICmp((char*)pExp
2832d 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 69 6e 22  r->token.z,"min"
2832e 2c 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ,3)==0 ){.    re
2832f 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
28330 42 59 5f 4d 49 4e 3b 0a 20 20 7d 65 6c 73 65 20  BY_MIN;.  }else 
28331 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
28332 43 6d 70 28 28 63 68 61 72 2a 29 70 45 78 70 72  Cmp((char*)pExpr
28333 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 61 78 22 2c  ->token.z,"max",
28334 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  3)==0 ){.    ret
28335 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42  urn WHERE_ORDERB
28336 59 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72 65 74  Y_MAX;.  }.  ret
28337 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42  urn WHERE_ORDERB
28338 59 5f 4e 4f 52 4d 41 4c 3b 0a 7d 0a 0a 2f 2a 0a  Y_NORMAL;.}../*.
28339 2a 2a 20 54 68 65 20 73 65 6c 65 63 74 20 73 74  ** The select st
2833a 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61  atement passed a
2833b 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
2833c 6d 65 6e 74 20 69 73 20 61 6e 20 61 67 67 72 65  ment is an aggre
2833d 67 61 74 65 20 71 75 65 72 79 2e 0a 2a 2a 20 54  gate query..** T
2833e 68 65 20 73 65 63 6f 6e 64 20 61 72 67 6d 65 6e  he second argmen
2833f 74 20 69 73 20 74 68 65 20 61 73 73 6f 63 69 61  t is the associa
28340 74 65 64 20 61 67 67 72 65 67 61 74 65 2d 69 6e  ted aggregate-in
28341 66 6f 20 6f 62 6a 65 63 74 2e 20 54 68 69 73 20  fo object. This 
28342 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 65 73  .** function tes
28343 74 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54  ts if the SELECT
28344 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a   is of the form:
28345 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
28346 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74  count(*) FROM <t
28347 62 6c 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20  bl>.**.** where 
28348 74 61 62 6c 65 20 69 73 20 61 20 64 61 74 61 62  table is a datab
28349 61 73 65 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61  ase table, not a
2834a 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 76   sub-select or v
2834b 69 65 77 2e 20 49 66 20 74 68 65 20 71 75 65 72  iew. If the quer
2834c 79 0a 2a 2a 20 64 6f 65 73 20 6d 61 74 63 68 20  y.** does match 
2834d 74 68 69 73 20 70 61 74 74 65 72 6e 2c 20 74 68  this pattern, th
2834e 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  en a pointer to 
2834f 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74  the Table object
28350 20 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a 2a   representing.**
28351 20 3c 74 62 6c 3e 20 69 73 20 72 65 74 75 72 6e   <tbl> is return
28352 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30  ed. Otherwise, 0
28353 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
28354 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20 2a 69  .static Table *i
28355 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 53 65 6c  sSimpleCount(Sel
28356 65 63 74 20 2a 70 2c 20 41 67 67 49 6e 66 6f 20  ect *p, AggInfo 
28357 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 54 61  *pAggInfo){.  Ta
28358 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 45 78 70  ble *pTab;.  Exp
28359 72 20 2a 70 45 78 70 72 3b 0a 0a 20 20 61 73 73  r *pExpr;..  ass
2835a 65 72 74 28 20 21 70 2d 3e 70 47 72 6f 75 70 42  ert( !p->pGroupB
2835b 79 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 70  y );..  if( p->p
2835c 57 68 65 72 65 20 7c 7c 20 70 2d 3e 70 45 4c 69  Where || p->pELi
2835d 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 0a 20 20  st->nExpr!=1 .  
2835e 20 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e 6e 53 72   || p->pSrc->nSr
2835f 63 21 3d 31 20 7c 7c 20 70 2d 3e 70 53 72 63 2d  c!=1 || p->pSrc-
28360 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 0a 20 20  >a[0].pSelect.  
28361 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
28362 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 70 2d  .  }.  pTab = p-
28363 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62  >pSrc->a[0].pTab
28364 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 2d 3e 70  ;.  pExpr = p->p
28365 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
28366 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  r;.  assert( pTa
28367 62 20 26 26 20 21 70 54 61 62 2d 3e 70 53 65 6c  b && !pTab->pSel
28368 65 63 74 20 26 26 20 70 45 78 70 72 20 29 3b 0a  ect && pExpr );.
28369 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
2836a 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20  (pTab) ) return 
2836b 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  0;.  if( pExpr->
2836c 6f 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54  op!=TK_AGG_FUNCT
2836d 49 4f 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ION ) return 0;.
2836e 20 20 69 66 28 20 28 70 41 67 67 49 6e 66 6f 2d    if( (pAggInfo-
2836f 3e 61 46 75 6e 63 5b 30 5d 2e 70 46 75 6e 63 2d  >aFunc[0].pFunc-
28370 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 55  >flags&SQLITE_FU
28371 4e 43 5f 43 4f 55 4e 54 29 3d 3d 30 20 29 20 72  NC_COUNT)==0 ) r
28372 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70  eturn 0;.  if( p
28373 45 78 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f 44  Expr->flags&EP_D
28374 69 73 74 69 6e 63 74 20 29 20 72 65 74 75 72 6e  istinct ) return
28375 20 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54   0;..  return pT
28376 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  ab;.}../*.** If 
28377 74 68 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20  the source-list 
28378 69 74 65 6d 20 70 61 73 73 65 64 20 61 73 20 61  item passed as a
28379 6e 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 61  n argument was a
2837a 75 67 6d 65 6e 74 65 64 20 77 69 74 68 20 61 6e  ugmented with an
2837b 0a 2a 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63  .** INDEXED BY c
2837c 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 72 79 20  lause, then try 
2837d 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 70  to locate the sp
2837e 65 63 69 66 69 65 64 20 69 6e 64 65 78 2e 20 49  ecified index. I
2837f 66 20 74 68 65 72 65 0a 2a 2a 20 77 61 73 20 73  f there.** was s
28380 75 63 68 20 61 20 63 6c 61 75 73 65 20 61 6e 64  uch a clause and
28381 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78   the named index
28382 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
28383 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c  , return .** SQL
28384 49 54 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c 65  ITE_ERROR and le
28385 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20  ave an error in 
28386 70 50 61 72 73 65 2e 20 4f 74 68 65 72 77 69 73  pParse. Otherwis
28387 65 2c 20 70 6f 70 75 6c 61 74 65 20 0a 2a 2a 20  e, populate .** 
28388 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 61 6e  pFrom->pIndex an
28389 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  d return SQLITE_
2838a 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  OK..*/.SQLITE_PR
2838b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2838c 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70  3IndexedByLookup
2838d 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2838e 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2838f 74 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69  tem *pFrom){.  i
28390 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 26  f( pFrom->pTab &
28391 26 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78 20  & pFrom->zIndex 
28392 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
28393 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62  ab = pFrom->pTab
28394 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64  ;.    char *zInd
28395 65 78 20 3d 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64  ex = pFrom->zInd
28396 65 78 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  ex;.    Index *p
28397 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64  Idx;.    for(pId
28398 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
28399 0a 20 20 20 20 20 20 20 20 70 49 64 78 20 26 26  .        pIdx &&
2839a 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2839b 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 49 6e  pIdx->zName, zIn
2839c 64 65 78 29 3b 20 0a 20 20 20 20 20 20 20 20 70  dex); .        p
2839d 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 0a  Idx=pIdx->pNext.
2839e 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21      );.    if( !
2839f 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71  pIdx ){.      sq
283a0 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
283a1 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69  arse, "no such i
283a2 6e 64 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64 65  ndex: %s", zInde
283a3 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 74  x, 0);.      ret
283a4 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
283a5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 46 72 6f  ;.    }.    pFro
283a6 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 64 78  m->pIndex = pIdx
283a7 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
283a8 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
283a9 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
283aa 69 73 20 61 20 57 61 6c 6b 65 72 20 63 61 6c 6c  is a Walker call
283ab 62 61 63 6b 20 66 6f 72 20 22 65 78 70 61 6e 64  back for "expand
283ac 69 6e 67 22 20 61 20 53 45 4c 45 43 54 20 73 74  ing" a SELECT st
283ad 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78 70  atement..** "Exp
283ae 61 6e 64 69 6e 67 22 20 6d 65 61 6e 73 20 74 6f  anding" means to
283af 20 64 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   do the followin
283b0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20  g:.**.**    (1) 
283b1 20 4d 61 6b 65 20 73 75 72 65 20 56 44 42 45 20   Make sure VDBE 
283b2 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68  cursor numbers h
283b3 61 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65  ave been assigne
283b4 64 20 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20 20  d to every.**   
283b5 20 20 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f 66        element of
283b6 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
283b7 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20  ..**.**    (2)  
283b8 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 54 61 62  Fill in the pTab
283b9 4c 69 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66  List->a[].pTab f
283ba 69 65 6c 64 73 20 69 6e 20 74 68 65 20 53 72 63  ields in the Src
283bb 4c 69 73 74 20 74 68 61 74 20 0a 2a 2a 20 20 20  List that .**   
283bc 20 20 20 20 20 20 64 65 66 69 6e 65 73 20 46 52        defines FR
283bd 4f 4d 20 63 6c 61 75 73 65 2e 20 20 57 68 65 6e  OM clause.  When
283be 20 76 69 65 77 73 20 61 70 70 65 61 72 20 69 6e   views appear in
283bf 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
283c0 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 69 6c  ,.**         fil
283c1 6c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e  l pTabList->a[].
283c2 70 53 65 6c 65 63 74 20 77 69 74 68 20 61 20 63  pSelect with a c
283c3 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43  opy of the SELEC
283c4 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20  T statement.**  
283c5 20 20 20 20 20 20 20 74 68 61 74 20 69 6d 70 6c         that impl
283c6 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 2e  ements the view.
283c7 20 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65    A copy is made
283c8 20 6f 66 20 74 68 65 20 76 69 65 77 27 73 20 53   of the view's S
283c9 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
283ca 20 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68   statement so th
283cb 61 74 20 77 65 20 63 61 6e 20 66 72 65 65 6c 79  at we can freely
283cc 20 6d 6f 64 69 66 79 20 6f 72 20 64 65 6c 65 74   modify or delet
283cd 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  e that statement
283ce 0a 2a 2a 20 20 20 20 20 20 20 20 20 77 69 74 68  .**         with
283cf 6f 75 74 20 77 6f 72 72 79 69 6e 67 20 61 62 6f  out worrying abo
283d0 75 74 20 6d 65 73 73 69 6e 67 20 75 70 20 74 68  ut messing up th
283d1 65 20 70 72 65 73 69 73 74 65 6e 74 20 72 65 70  e presistent rep
283d2 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20  resentation.**  
283d3 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 76 69         of the vi
283d4 65 77 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29  ew..**.**    (3)
283d5 20 20 41 64 64 20 74 65 72 6d 73 20 74 6f 20 74    Add terms to t
283d6 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
283d7 74 6f 20 61 63 63 6f 6d 6f 64 61 74 65 20 74 68  to accomodate th
283d8 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72  e NATURAL keywor
283d9 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e 20  d.**         on 
283da 6a 6f 69 6e 73 20 61 6e 64 20 74 68 65 20 4f 4e  joins and the ON
283db 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73   and USING claus
283dc 65 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a  e of joins..**.*
283dd 2a 20 20 20 20 28 34 29 20 20 53 63 61 6e 20 74  *    (4)  Scan t
283de 68 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d  he list of colum
283df 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
283e0 20 73 65 74 20 28 70 45 4c 69 73 74 29 20 6c 6f   set (pEList) lo
283e1 6f 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20  oking.**        
283e2 20 66 6f 72 20 69 6e 73 74 61 6e 63 65 73 20 6f   for instances o
283e3 66 20 74 68 65 20 22 2a 22 20 6f 70 65 72 61 74  f the "*" operat
283e4 6f 72 20 6f 72 20 74 68 65 20 54 41 42 4c 45 2e  or or the TABLE.
283e5 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 20  * operator..**  
283e6 20 20 20 20 20 20 20 49 66 20 66 6f 75 6e 64 2c         If found,
283e7 20 65 78 70 61 6e 64 20 65 61 63 68 20 22 2a 22   expand each "*"
283e8 20 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c   to be every col
283e9 75 6d 6e 20 69 6e 20 65 76 65 72 79 20 74 61 62  umn in every tab
283ea 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 6e  le.**         an
283eb 64 20 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65 20  d TABLE.* to be 
283ec 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20  every column in 
283ed 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61  TABLE..**.*/.sta
283ee 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 45 78  tic int selectEx
283ef 70 61 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a 70  pander(Walker *p
283f0 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
283f1 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  p){.  Parse *pPa
283f2 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70  rse = pWalker->p
283f3 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 2c 20  Parse;.  int i, 
283f4 6a 2c 20 6b 3b 0a 20 20 53 72 63 4c 69 73 74 20  j, k;.  SrcList 
283f5 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 45 78 70  *pTabList;.  Exp
283f6 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20  rList *pEList;. 
283f7 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
283f8 69 74 65 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20 73  item *pFrom;.  s
283f9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
283fa 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20  rse->db;..  if( 
283fb 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
283fc 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
283fd 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20  WRC_Abort;.  }. 
283fe 20 69 66 28 20 70 2d 3e 70 53 72 63 3d 3d 30 20   if( p->pSrc==0 
283ff 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  || (p->selFlags 
28400 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d  & SF_Expanded)!=
28401 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
28402 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20  WRC_Prune;.  }. 
28403 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20   p->selFlags |= 
28404 53 46 5f 45 78 70 61 6e 64 65 64 3b 0a 20 20 70  SF_Expanded;.  p
28405 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72  TabList = p->pSr
28406 63 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d  c;.  pEList = p-
28407 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 4d  >pEList;..  /* M
28408 61 6b 65 20 73 75 72 65 20 63 75 72 73 6f 72 20  ake sure cursor 
28409 6e 75 6d 62 65 72 73 20 68 61 76 65 20 62 65 65  numbers have bee
2840a 6e 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c  n assigned to al
2840b 6c 20 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 2a  l entries in.  *
2840c 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
2840d 65 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  e of the SELECT 
2840e 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a  statement..  */.
2840f 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
28410 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50  AssignCursors(pP
28411 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b  arse, pTabList);
28412 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65  ..  /* Look up e
28413 76 65 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 64  very table named
28414 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
28415 75 73 65 20 6f 66 20 74 68 65 20 73 65 6c 65 63  use of the selec
28416 74 2e 20 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65  t.  If.  ** an e
28417 6e 74 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d  ntry of the FROM
28418 20 63 6c 61 75 73 65 20 69 73 20 61 20 73 75 62   clause is a sub
28419 71 75 65 72 79 20 69 6e 73 74 65 61 64 20 6f 66  query instead of
2841a 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77   a table or view
2841b 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 63 72 65 61  ,.  ** then crea
2841c 74 65 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74  te a transient t
2841d 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74  able structure t
2841e 6f 20 64 65 73 63 72 69 62 65 20 74 68 65 20 73  o describe the s
2841f 75 62 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20  ubquery..  */.  
28420 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70  for(i=0, pFrom=p
28421 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54  TabList->a; i<pT
28422 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
28423 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20  +, pFrom++){.   
28424 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
28425 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61    if( pFrom->pTa
28426 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  b!=0 ){.      /*
28427 20 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   This statement 
28428 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
28429 20 70 72 65 70 61 72 65 64 2e 20 20 54 68 65 72   prepared.  Ther
2842a 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20  e is no need.   
2842b 20 20 20 2a 2a 20 74 6f 20 67 6f 20 66 75 72 74     ** to go furt
2842c 68 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73  her. */.      as
2842d 73 65 72 74 28 20 69 3d 3d 30 20 29 3b 0a 20 20  sert( i==0 );.  
2842e 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
2842f 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rune;.    }.    
28430 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65  if( pFrom->zName
28431 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  ==0 ){.#ifndef S
28432 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
28433 45 52 59 0a 20 20 20 20 20 20 53 65 6c 65 63 74  ERY.      Select
28434 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e   *pSel = pFrom->
28435 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 2f  pSelect;.      /
28436 2a 20 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e  * A sub-query in
28437 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
28438 20 6f 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a   of a SELECT */.
28439 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
2843a 65 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  el!=0 );.      a
2843b 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54  ssert( pFrom->pT
2843c 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ab==0 );.      s
2843d 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
2843e 28 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c 29 3b  (pWalker, pSel);
2843f 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54  .      pFrom->pT
28440 61 62 20 3d 20 70 54 61 62 20 3d 20 73 71 6c 69  ab = pTab = sqli
28441 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
28442 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65  db, sizeof(Table
28443 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  ));.      if( pT
28444 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57  ab==0 ) return W
28445 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
28446 70 54 61 62 2d 3e 64 62 4d 65 6d 20 3d 20 64 62  pTab->dbMem = db
28447 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
28448 62 6c 65 64 20 3f 20 64 62 20 3a 20 30 3b 0a 20  bled ? db : 0;. 
28449 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 20       pTab->nRef 
2844a 3d 20 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  = 1;.      pTab-
2844b 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
2844c 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c  MPrintf(db, "sql
2844d 69 74 65 5f 73 75 62 71 75 65 72 79 5f 25 70 5f  ite_subquery_%p_
2844e 22 2c 20 28 76 6f 69 64 2a 29 70 54 61 62 29 3b  ", (void*)pTab);
2844f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53  .      while( pS
28450 65 6c 2d 3e 70 50 72 69 6f 72 20 29 7b 20 70 53  el->pPrior ){ pS
28451 65 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f  el = pSel->pPrio
28452 72 3b 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63  r; }.      selec
28453 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72  tColumnsFromExpr
28454 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65  List(pParse, pSe
28455 6c 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62  l->pEList, &pTab
28456 2d 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61  ->nCol, &pTab->a
28457 43 6f 6c 29 3b 0a 20 20 20 20 20 20 70 54 61 62  Col);.      pTab
28458 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20  ->iPKey = -1;.  
28459 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61      pTab->tabFla
2845a 67 73 20 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72  gs |= TF_Ephemer
2845b 61 6c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  al;.#endif.    }
2845c 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41  else{.      /* A
2845d 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65  n ordinary table
2845e 20 6f 72 20 76 69 65 77 20 6e 61 6d 65 20 69 6e   or view name in
2845f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
28460 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
28461 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30  ( pFrom->pTab==0
28462 20 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d   );.      pFrom-
28463 3e 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20 0a  >pTab = pTab = .
28464 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c          sqlite3L
28465 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
28466 65 2c 30 2c 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65  e,0,pFrom->zName
28467 2c 70 46 72 6f 6d 2d 3e 7a 44 61 74 61 62 61 73  ,pFrom->zDatabas
28468 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  e);.      if( pT
28469 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57  ab==0 ) return W
2846a 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
2846b 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69  pTab->nRef++;.#i
2846c 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2846d 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
2846e 21 64 65 66 69 6e 65 64 20 28 53 51 4c 49 54 45  !defined (SQLITE
2846f 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
28470 4c 45 29 0a 20 20 20 20 20 20 69 66 28 20 70 54  LE).      if( pT
28471 61 62 2d 3e 70 53 65 6c 65 63 74 20 7c 7c 20 49  ab->pSelect || I
28472 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
28473 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20  {.        /* We 
28474 72 65 61 63 68 20 68 65 72 65 20 69 66 20 74 68  reach here if th
28475 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73  e named table is
28476 20 61 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77   a really a view
28477 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
28478 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
28479 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
2847a 2c 20 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  , pTab) ) return
2847b 20 57 52 43 5f 41 62 6f 72 74 3b 0a 0a 20 20 20   WRC_Abort;..   
2847c 20 20 20 20 20 2f 2a 20 49 66 20 70 46 72 6f 6d       /* If pFrom
2847d 2d 3e 70 53 65 6c 65 63 74 21 3d 30 20 69 74 20  ->pSelect!=0 it 
2847e 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61  means we are dea
2847f 6c 69 6e 67 20 77 69 74 68 20 61 0a 20 20 20 20  ling with a.    
28480 20 20 20 20 2a 2a 20 76 69 65 77 20 77 69 74 68      ** view with
28481 69 6e 20 61 20 76 69 65 77 2e 20 20 54 68 65 20  in a view.  The 
28482 53 45 4c 45 43 54 20 73 74 72 75 63 74 75 72 65  SELECT structure
28483 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
28484 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70  n.        ** cop
28485 69 65 64 20 62 79 20 74 68 65 20 6f 75 74 65 72  ied by the outer
28486 20 76 69 65 77 20 73 6f 20 77 65 20 63 61 6e 20   view so we can 
28487 73 6b 69 70 20 74 68 65 20 63 6f 70 79 20 73 74  skip the copy st
28488 65 70 20 68 65 72 65 0a 20 20 20 20 20 20 20 20  ep here.        
28489 2a 2a 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20  ** in the inner 
2848a 76 69 65 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f  view..        */
2848b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 46 72  .        if( pFr
2848c 6f 6d 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29  om->pSelect==0 )
2848d 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 6f  {.          pFro
2848e 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  m->pSelect = sql
2848f 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
28490 2c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c  , pTab->pSelect,
28491 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   0);.          s
28492 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
28493 28 70 57 61 6c 6b 65 72 2c 20 70 46 72 6f 6d 2d  (pWalker, pFrom-
28494 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  >pSelect);.     
28495 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e     }.      }.#en
28496 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  dif.    }..    /
28497 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 64  * Locate the ind
28498 65 78 20 6e 61 6d 65 64 20 62 79 20 74 68 65 20  ex named by the 
28499 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
2849a 65 2c 20 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20  e, if any. */.  
2849b 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 6e 64    if( sqlite3Ind
2849c 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61  exedByLookup(pPa
2849d 72 73 65 2c 20 70 46 72 6f 6d 29 20 29 7b 0a 20  rse, pFrom) ){. 
2849e 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
2849f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  Abort;.    }.  }
284a0 0a 0a 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 4e  ..  /* Process N
284a1 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 73 2c  ATURAL keywords,
284a2 20 61 6e 64 20 4f 4e 20 61 6e 64 20 55 53 49 4e   and ON and USIN
284a3 47 20 63 6c 61 75 73 65 73 20 6f 66 20 6a 6f 69  G clauses of joi
284a4 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ns..  */.  if( d
284a5 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
284a6 7c 7c 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73  || sqliteProcess
284a7 4a 6f 69 6e 28 70 50 61 72 73 65 2c 20 70 29 20  Join(pParse, p) 
284a8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  ){.    return WR
284a9 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20  C_Abort;.  }..  
284aa 2f 2a 20 46 6f 72 20 65 76 65 72 79 20 22 2a 22  /* For every "*"
284ab 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20   that occurs in 
284ac 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c  the column list,
284ad 20 69 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65   insert the name
284ae 73 20 6f 66 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f  s of.  ** all co
284af 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62  lumns in all tab
284b0 6c 65 73 2e 20 20 41 6e 64 20 66 6f 72 20 65 76  les.  And for ev
284b1 65 72 79 20 54 41 42 4c 45 2e 2a 20 69 6e 73 65  ery TABLE.* inse
284b2 72 74 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a  rt the names.  *
284b3 2a 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  * of all columns
284b4 20 69 6e 20 54 41 42 4c 45 2e 20 20 54 68 65 20   in TABLE.  The 
284b5 70 61 72 73 65 72 20 69 6e 73 65 72 74 65 64 20  parser inserted 
284b6 61 20 73 70 65 63 69 61 6c 20 65 78 70 72 65 73  a special expres
284b7 73 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 20 74  sion.  ** with t
284b8 68 65 20 54 4b 5f 41 4c 4c 20 6f 70 65 72 61 74  he TK_ALL operat
284b9 6f 72 20 66 6f 72 20 65 61 63 68 20 22 2a 22 20  or for each "*" 
284ba 74 68 61 74 20 69 74 20 66 6f 75 6e 64 20 69 6e  that it found in
284bb 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74   the column list
284bc 2e 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ..  ** The follo
284bd 77 69 6e 67 20 63 6f 64 65 20 6a 75 73 74 20 68  wing code just h
284be 61 73 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65  as to locate the
284bf 20 54 4b 5f 41 4c 4c 20 65 78 70 72 65 73 73 69   TK_ALL expressi
284c0 6f 6e 73 20 61 6e 64 20 65 78 70 61 6e 64 0a 20  ons and expand. 
284c1 20 2a 2a 20 65 61 63 68 20 6f 6e 65 20 74 6f 20   ** each one to 
284c2 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  the list of all 
284c3 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74  columns in all t
284c4 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ables..  **.  **
284c5 20 54 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 20   The first loop 
284c6 6a 75 73 74 20 63 68 65 63 6b 73 20 74 6f 20 73  just checks to s
284c7 65 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ee if there are 
284c8 61 6e 79 20 22 2a 22 20 6f 70 65 72 61 74 6f 72  any "*" operator
284c9 73 0a 20 20 2a 2a 20 74 68 61 74 20 6e 65 65 64  s.  ** that need
284ca 20 65 78 70 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f   expanding..  */
284cb 0a 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45  .  for(k=0; k<pE
284cc 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b  List->nExpr; k++
284cd 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20  ){.    Expr *pE 
284ce 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70  = pEList->a[k].p
284cf 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45  Expr;.    if( pE
284d0 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62  ->op==TK_ALL ) b
284d1 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 70 45  reak;.    if( pE
284d2 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20  ->op==TK_DOT && 
284d3 70 45 2d 3e 70 52 69 67 68 74 20 26 26 20 70 45  pE->pRight && pE
284d4 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b  ->pRight->op==TK
284d5 5f 41 4c 4c 0a 20 20 20 20 20 20 20 20 20 26 26  _ALL.         &&
284d6 20 70 45 2d 3e 70 4c 65 66 74 20 26 26 20 70 45   pE->pLeft && pE
284d7 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ->pLeft->op==TK_
284d8 49 44 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  ID ) break;.  }.
284d9 20 20 69 66 28 20 6b 3c 70 45 4c 69 73 74 2d 3e    if( k<pEList->
284da 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 2f 2a 0a  nExpr ){.    /*.
284db 20 20 20 20 2a 2a 20 49 66 20 77 65 20 67 65 74      ** If we get
284dc 20 68 65 72 65 20 69 74 20 6d 65 61 6e 73 20 74   here it means t
284dd 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f  he result set co
284de 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f  ntains one or mo
284df 72 65 20 22 2a 22 0a 20 20 20 20 2a 2a 20 6f 70  re "*".    ** op
284e0 65 72 61 74 6f 72 73 20 74 68 61 74 20 6e 65 65  erators that nee
284e1 64 20 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64  d to be expanded
284e2 2e 20 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  .  Loop through 
284e3 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 0a  each expression.
284e4 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65      ** in the re
284e5 73 75 6c 74 20 73 65 74 20 61 6e 64 20 65 78 70  sult set and exp
284e6 61 6e 64 20 74 68 65 6d 20 6f 6e 65 20 62 79 20  and them one by 
284e7 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  one..    */.    
284e8 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
284e9 69 74 65 6d 20 2a 61 20 3d 20 70 45 4c 69 73 74  item *a = pEList
284ea 2d 3e 61 3b 0a 20 20 20 20 45 78 70 72 4c 69 73  ->a;.    ExprLis
284eb 74 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20  t *pNew = 0;.   
284ec 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 50 61   int flags = pPa
284ed 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 3b 0a  rse->db->flags;.
284ee 20 20 20 20 69 6e 74 20 6c 6f 6e 67 4e 61 6d 65      int longName
284ef 73 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c  s = (flags & SQL
284f0 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73  ITE_FullColNames
284f1 29 21 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20  )!=0.           
284f2 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 66             && (f
284f3 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68  lags & SQLITE_Sh
284f4 6f 72 74 43 6f 6c 4e 61 6d 65 73 29 3d 3d 30 3b  ortColNames)==0;
284f5 0a 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b  ..    for(k=0; k
284f6 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
284f7 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72  k++){.      Expr
284f8 20 2a 70 45 20 3d 20 61 5b 6b 5d 2e 70 45 78 70   *pE = a[k].pExp
284f9 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d  r;.      if( pE-
284fa 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20 26 26 0a 20  >op!=TK_ALL &&. 
284fb 20 20 20 20 20 20 20 20 20 20 28 70 45 2d 3e 6f            (pE->o
284fc 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d  p!=TK_DOT || pE-
284fd 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 45  >pRight==0 || pE
284fe 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b  ->pRight->op!=TK
284ff 5f 41 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20  _ALL) ){.       
28500 20 2f 2a 20 54 68 69 73 20 70 61 72 74 69 63 75   /* This particu
28501 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 64  lar expression d
28502 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
28503 62 65 20 65 78 70 61 6e 64 65 64 2e 0a 20 20 20  be expanded..   
28504 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
28505 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78  pNew = sqlite3Ex
28506 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
28507 72 73 65 2c 20 70 4e 65 77 2c 20 61 5b 6b 5d 2e  rse, pNew, a[k].
28508 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20  pExpr, 0);.     
28509 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20     if( pNew ){. 
2850a 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 61           pNew->a
2850b 5b 70 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e  [pNew->nExpr-1].
2850c 7a 4e 61 6d 65 20 3d 20 61 5b 6b 5d 2e 7a 4e 61  zName = a[k].zNa
2850d 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  me;.        }.  
2850e 20 20 20 20 20 20 61 5b 6b 5d 2e 70 45 78 70 72        a[k].pExpr
2850f 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 61 5b   = 0;.        a[
28510 6b 5d 2e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20  k].zName = 0;.  
28511 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
28512 20 20 20 2f 2a 20 54 68 69 73 20 65 78 70 72 65     /* This expre
28513 73 73 69 6f 6e 20 69 73 20 61 20 22 2a 22 20 6f  ssion is a "*" o
28514 72 20 61 20 22 54 41 42 4c 45 2e 2a 22 20 61 6e  r a "TABLE.*" an
28515 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20  d needs to be.  
28516 20 20 20 20 20 20 2a 2a 20 65 78 70 61 6e 64 65        ** expande
28517 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  d. */.        in
28518 74 20 74 61 62 6c 65 53 65 65 6e 20 3d 20 30 3b  t tableSeen = 0;
28519 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20        /* Set to 
2851a 31 20 77 68 65 6e 20 54 41 42 4c 45 20 6d 61 74  1 when TABLE mat
2851b 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  ches */.        
2851c 63 68 61 72 20 2a 7a 54 4e 61 6d 65 3b 20 20 20  char *zTName;   
2851d 20 20 20 20 20 20 20 20 20 2f 2a 20 74 65 78 74           /* text
2851e 20 6f 66 20 6e 61 6d 65 20 6f 66 20 54 41 42 4c   of name of TABL
2851f 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  E */.        if(
28520 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20   pE->op==TK_DOT 
28521 26 26 20 70 45 2d 3e 70 4c 65 66 74 20 29 7b 0a  && pE->pLeft ){.
28522 20 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65            zTName
28523 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
28524 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70 45 2d  omToken(db, &pE-
28525 3e 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e 29 3b 0a  >pLeft->token);.
28526 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
28527 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20           zTName 
28528 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
28529 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20         for(i=0, 
2852a 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e  pFrom=pTabList->
2852b 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  a; i<pTabList->n
2852c 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b  Src; i++, pFrom+
2852d 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 54 61  +){.          Ta
2852e 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f  ble *pTab = pFro
2852f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20  m->pTab;.       
28530 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d     char *zTabNam
28531 65 20 3d 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61  e = pFrom->zAlia
28532 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  s;.          if(
28533 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 7c 7c 20   zTabName==0 || 
28534 7a 54 61 62 4e 61 6d 65 5b 30 5d 3d 3d 30 20 29  zTabName[0]==0 )
28535 7b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a  { .            z
28536 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e  TabName = pTab->
28537 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
28538 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
28539 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2853a 64 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  d ) break;.     
2853b 20 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65 20       if( zTName 
2853c 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
2853d 70 28 7a 54 4e 61 6d 65 2c 20 7a 54 61 62 4e 61  p(zTName, zTabNa
2853e 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)!=0 ){.      
2853f 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
28540 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
28541 20 20 20 20 20 20 74 61 62 6c 65 53 65 65 6e 20        tableSeen 
28542 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 66  = 1;.          f
28543 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e  or(j=0; j<pTab->
28544 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  nCol; j++){.    
28545 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
28546 78 70 72 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20  xpr, *pRight;.  
28547 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
28548 7a 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 61 43  zName = pTab->aC
28549 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20  ol[j].zName;..  
2854a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
2854b 61 20 63 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b  a column is mark
2854c 65 64 20 61 73 20 27 68 69 64 64 65 6e 27 20 28  ed as 'hidden' (
2854d 63 75 72 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70  currently only p
2854e 6f 73 73 69 62 6c 65 0a 20 20 20 20 20 20 20 20  ossible.        
2854f 20 20 20 20 2a 2a 20 66 6f 72 20 76 69 72 74 75      ** for virtu
28550 61 6c 20 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e  al tables), do n
28551 6f 74 20 69 6e 63 6c 75 64 65 20 69 74 20 69 6e  ot include it in
28552 20 74 68 65 20 65 78 70 61 6e 64 65 64 0a 20 20   the expanded.  
28553 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 73            ** res
28554 75 6c 74 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20  ult-set list..  
28555 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
28556 20 20 20 20 20 20 20 20 20 69 66 28 20 49 73 48           if( IsH
28557 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61  iddenColumn(&pTa
28558 62 2d 3e 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20  b->aCol[j]) ){. 
28559 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73               ass
2855a 65 72 74 28 49 73 56 69 72 74 75 61 6c 28 70 54  ert(IsVirtual(pT
2855b 61 62 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ab));.          
2855c 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
2855d 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20            }..   
2855e 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 30           if( i>0
2855f 20 26 26 20 7a 54 4e 61 6d 65 3d 3d 30 20 29 7b   && zTName==0 ){
28560 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
28561 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
28562 65 6d 20 2a 70 4c 65 66 74 20 3d 20 26 70 54 61  em *pLeft = &pTa
28563 62 4c 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b 0a 20  bList->a[i-1];. 
28564 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
28565 20 28 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69 6e 74   (pLeft[1].joint
28566 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c  ype & JT_NATURAL
28567 29 21 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20  )!=0 &&.        
28568 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28569 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66  columnIndex(pLef
2856a 74 2d 3e 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3e  t->pTab, zName)>
2856b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2856c 20 20 20 20 20 20 2f 2a 20 49 6e 20 61 20 4e 41        /* In a NA
2856d 54 55 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d 69 74  TURAL join, omit
2856e 20 74 68 65 20 6a 6f 69 6e 20 63 6f 6c 75 6d 6e   the join column
2856f 73 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20  s from the .    
28570 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
28571 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68  able on the righ
28572 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  t */.           
28573 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
28574 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
28575 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
28576 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e   sqlite3IdListIn
28577 64 65 78 28 70 4c 65 66 74 5b 31 5d 2e 70 55 73  dex(pLeft[1].pUs
28578 69 6e 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29  ing, zName)>=0 )
28579 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2857a 20 20 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77    /* In a join w
2857b 69 74 68 20 61 20 55 53 49 4e 47 20 63 6c 61 75  ith a USING clau
2857c 73 65 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73  se, omit columns
2857d 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20   in the.        
2857e 20 20 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67          ** using
2857f 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65   clause from the
28580 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69   table on the ri
28581 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ght. */.        
28582 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
28583 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
28584 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  }.            }.
28585 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67              pRig
28586 68 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ht = sqlite3PExp
28587 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
28588 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20   0, 0, 0);.     
28589 20 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68         if( pRigh
2858a 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  t==0 ) break;.  
2858b 20 20 20 20 20 20 20 20 20 20 73 65 74 51 75 6f            setQuo
2858c 74 65 64 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c  tedToken(pParse,
2858d 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2c   &pRight->token,
2858e 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
2858f 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d       if( longNam
28590 65 73 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e  es || pTabList->
28591 6e 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20  nSrc>1 ){.      
28592 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c          Expr *pL
28593 65 66 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78  eft = sqlite3PEx
28594 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44  pr(pParse, TK_ID
28595 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
28596 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 20            pExpr 
28597 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
28598 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70  Parse, TK_DOT, p
28599 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29  Left, pRight, 0)
2859a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2859b 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 62  if( pExpr==0 ) b
2859c 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
2859d 20 20 20 20 73 65 74 51 75 6f 74 65 64 54 6f 6b      setQuotedTok
2859e 65 6e 28 70 50 61 72 73 65 2c 20 26 70 4c 65 66  en(pParse, &pLef
2859f 74 2d 3e 74 6f 6b 65 6e 2c 20 7a 54 61 62 4e 61  t->token, zTabNa
285a0 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  me);.           
285a1 20 20 20 73 65 74 54 6f 6b 65 6e 28 26 70 45 78     setToken(&pEx
285a2 70 72 2d 3e 73 70 61 6e 2c 20 0a 20 20 20 20 20  pr->span, .     
285a3 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
285a4 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
285a5 22 25 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d  "%s.%s", zTabNam
285a6 65 2c 20 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20  e, zName));.    
285a7 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
285a8 3e 73 70 61 6e 2e 64 79 6e 20 3d 20 31 3b 0a 20  >span.dyn = 1;. 
285a9 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
285aa 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 30 3b  pr->token.z = 0;
285ab 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
285ac 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20  Expr->token.n = 
285ad 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0;.             
285ae 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 64 79   pExpr->token.dy
285af 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  n = 0;.         
285b0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
285b1 20 20 20 20 20 20 20 20 70 45 78 70 72 20 3d 20          pExpr = 
285b2 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
285b3 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73 70 61        pExpr->spa
285b4 6e 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  n = pExpr->token
285b5 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
285b6 70 45 78 70 72 2d 3e 73 70 61 6e 2e 64 79 6e 20  pExpr->span.dyn 
285b7 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
285b8 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69   }.            i
285b9 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a  f( longNames ){.
285ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
285bb 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
285bc 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
285bd 65 2c 20 70 4e 65 77 2c 20 70 45 78 70 72 2c 20  e, pNew, pExpr, 
285be 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20  &pExpr->span);. 
285bf 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
285c0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
285c1 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78  pNew = sqlite3Ex
285c2 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
285c3 72 73 65 2c 20 70 4e 65 77 2c 20 70 45 78 70 72  rse, pNew, pExpr
285c4 2c 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e  , &pRight->token
285c5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
285c6 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
285c7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
285c8 66 28 20 21 74 61 62 6c 65 53 65 65 6e 20 29 7b  f( !tableSeen ){
285c9 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
285ca 54 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20  TName ){.       
285cb 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
285cc 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
285cd 20 73 75 63 68 20 74 61 62 6c 65 3a 20 25 73 22   such table: %s"
285ce 2c 20 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20 20  , zTName);.     
285cf 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
285d0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
285d1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
285d2 22 6e 6f 20 74 61 62 6c 65 73 20 73 70 65 63 69  "no tables speci
285d3 66 69 65 64 22 29 3b 0a 20 20 20 20 20 20 20 20  fied");.        
285d4 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
285d5 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
285d6 72 65 65 28 64 62 2c 20 7a 54 4e 61 6d 65 29 3b  ree(db, zTName);
285d7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
285d8 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
285d9 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c  stDelete(db, pEL
285da 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70 45 4c  ist);.    p->pEL
285db 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ist = pNew;.  }.
285dc 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  #if SQLITE_MAX_C
285dd 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 70  OLUMN.  if( p->p
285de 45 4c 69 73 74 20 26 26 20 70 2d 3e 70 45 4c 69  EList && p->pELi
285df 73 74 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c  st->nExpr>db->aL
285e0 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
285e1 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20  T_COLUMN] ){.   
285e2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
285e3 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
285e4 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65  ny columns in re
285e5 73 75 6c 74 20 73 65 74 22 29 3b 0a 20 20 7d 0a  sult set");.  }.
285e6 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
285e7 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a  WRC_Continue;.}.
285e8 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 72 6f 75  ./*.** No-op rou
285e9 74 69 6e 65 20 66 6f 72 20 74 68 65 20 70 61 72  tine for the par
285ea 73 65 2d 74 72 65 65 20 77 61 6c 6b 65 72 2e 0a  se-tree walker..
285eb 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
285ec 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 57  routine is the W
285ed 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c 6c 62  alker.xExprCallb
285ee 61 63 6b 20 74 68 65 6e 20 65 78 70 72 65 73 73  ack then express
285ef 69 6f 6e 20 74 72 65 65 73 0a 2a 2a 20 61 72 65  ion trees.** are
285f0 20 77 61 6c 6b 65 64 20 77 69 74 68 6f 75 74 20   walked without 
285f1 61 6e 79 20 61 63 74 69 6f 6e 73 20 62 65 69 6e  any actions bein
285f2 67 20 74 61 6b 65 6e 20 61 74 20 65 61 63 68 20  g taken at each 
285f3 6e 6f 64 65 2e 20 20 50 72 65 73 75 6d 61 62 6c  node.  Presumabl
285f4 79 2c 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  y,.** when this 
285f5 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
285f6 66 6f 72 20 57 61 6c 6b 65 72 2e 78 45 78 70 72  for Walker.xExpr
285f7 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e 20 0a 2a  Callback then .*
285f8 2a 20 57 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74  * Walker.xSelect
285f9 43 61 6c 6c 62 61 63 6b 20 69 73 20 73 65 74 20  Callback is set 
285fa 74 6f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  to do something 
285fb 75 73 65 66 75 6c 20 66 6f 72 20 65 76 65 72 79  useful for every
285fc 20 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 69 6e   .** subquery in
285fd 20 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65   the parser tree
285fe 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
285ff 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 28 57 61 6c  exprWalkNoop(Wal
28600 6b 65 72 20 2a 4e 6f 74 55 73 65 64 2c 20 45 78  ker *NotUsed, Ex
28601 70 72 20 2a 4e 6f 74 55 73 65 64 32 29 7b 0a 20  pr *NotUsed2){. 
28602 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
28603 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
28604 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  sed2);.  return 
28605 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a  WRC_Continue;.}.
28606 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
28607 69 6e 65 20 22 65 78 70 61 6e 64 73 22 20 61 20  ine "expands" a 
28608 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
28609 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20   and all of its 
2860a 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 46  subqueries..** F
2860b 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
2860c 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 77 68 61  formation on wha
2860d 74 20 69 74 20 6d 65 61 6e 73 20 74 6f 20 22 65  t it means to "e
2860e 78 70 61 6e 64 22 20 61 20 53 45 4c 45 43 54 0a  xpand" a SELECT.
2860f 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65  ** statement, se
28610 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e  e the comment on
28611 20 74 68 65 20 73 65 6c 65 63 74 45 78 70 61 6e   the selectExpan
28612 64 20 77 6f 72 6b 65 72 20 63 61 6c 6c 62 61 63  d worker callbac
28613 6b 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 45  k above..**.** E
28614 78 70 61 6e 64 69 6e 67 20 61 20 53 45 4c 45 43  xpanding a SELEC
28615 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74  T statement is t
28616 68 65 20 66 69 72 73 74 20 73 74 65 70 20 69 6e  he first step in
28617 20 70 72 6f 63 65 73 73 69 6e 67 20 61 0a 2a 2a   processing a.**
28618 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
28619 74 2e 20 20 54 68 65 20 53 45 4c 45 43 54 20 73  t.  The SELECT s
2861a 74 61 74 65 6d 65 6e 74 20 6d 75 73 74 20 62 65  tatement must be
2861b 20 65 78 70 61 6e 64 65 64 20 62 65 66 6f 72 65   expanded before
2861c 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74  .** name resolut
2861d 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64  ion is performed
2861e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68  ..**.** If anyth
2861f 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20  ing goes wrong, 
28620 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
28621 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
28622 20 70 50 61 72 73 65 2e 0a 2a 2a 20 54 68 65 20   pParse..** The 
28623 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
28624 20 63 61 6e 20 64 65 74 65 63 74 20 74 68 65 20   can detect the 
28625 70 72 6f 62 6c 65 6d 20 62 79 20 6c 6f 6f 6b 69  problem by looki
28626 6e 67 20 61 74 20 70 50 61 72 73 65 2d 3e 6e 45  ng at pParse->nE
28627 72 72 0a 2a 2a 20 61 6e 64 2f 6f 72 20 70 50 61  rr.** and/or pPa
28628 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
28629 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ailed..*/.static
2862a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
2862b 65 63 74 45 78 70 61 6e 64 28 50 61 72 73 65 20  ectExpand(Parse 
2862c 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
2862d 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 57 61 6c  *pSelect){.  Wal
2862e 6b 65 72 20 77 3b 0a 20 20 77 2e 78 53 65 6c 65  ker w;.  w.xSele
2862f 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c  ctCallback = sel
28630 65 63 74 45 78 70 61 6e 64 65 72 3b 0a 20 20 77  ectExpander;.  w
28631 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
28632 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20   exprWalkNoop;. 
28633 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72   w.pParse = pPar
28634 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c  se;.  sqlite3Wal
28635 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 53 65 6c  kSelect(&w, pSel
28636 65 63 74 29 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65  ect);.}...#ifnde
28637 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
28638 42 51 55 45 52 59 0a 2f 2a 0a 2a 2a 20 54 68 69  BQUERY./*.** Thi
28639 73 20 69 73 20 61 20 57 61 6c 6b 65 72 2e 78 53  s is a Walker.xS
2863a 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 63 61  electCallback ca
2863b 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65 20 73  llback for the s
2863c 71 6c 69 74 65 33 53 65 6c 65 63 74 54 79 70 65  qlite3SelectType
2863d 49 6e 66 6f 28 29 0a 2a 2a 20 69 6e 74 65 72 66  Info().** interf
2863e 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65  ace..**.** For e
2863f 61 63 68 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20  ach FROM-clause 
28640 73 75 62 71 75 65 72 79 2c 20 61 64 64 20 43 6f  subquery, add Co
28641 6c 75 6d 6e 2e 7a 54 79 70 65 20 61 6e 64 20 43  lumn.zType and C
28642 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 0a 2a 2a 20 69  olumn.zColl.** i
28643 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68  nformation to th
28644 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
28645 65 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74  e that represent
28646 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  s the result set
28647 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73 75 62 71  .** of that subq
28648 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  uery..**.** The 
28649 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
2864a 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20  that represents 
2864b 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 77  the result set w
2864c 61 73 20 63 6f 6e 73 74 72 75 63 74 65 64 0a 2a  as constructed.*
2864d 2a 20 62 79 20 73 65 6c 65 63 74 45 78 70 61 6e  * by selectExpan
2864e 64 65 72 28 29 20 62 75 74 20 74 68 65 20 74 79  der() but the ty
2864f 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e  pe and collation
28650 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73   information was
28651 20 6f 6d 69 74 74 65 64 0a 2a 2a 20 61 74 20 74   omitted.** at t
28652 68 61 74 20 70 6f 69 6e 74 20 62 65 63 61 75 73  hat point becaus
28653 65 20 69 64 65 6e 74 69 66 69 65 72 73 20 68 61  e identifiers ha
28654 64 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72  d not yet been r
28655 65 73 6f 6c 76 65 64 2e 20 20 54 68 69 73 0a 2a  esolved.  This.*
28656 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  * routine is cal
28657 6c 65 64 20 61 66 74 65 72 20 69 64 65 6e 74 69  led after identi
28658 66 69 65 72 20 72 65 73 6f 6c 75 74 69 6f 6e 2e  fier resolution.
28659 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2865a 65 6c 65 63 74 41 64 64 53 75 62 71 75 65 72 79  electAddSubquery
2865b 54 79 70 65 49 6e 66 6f 28 57 61 6c 6b 65 72 20  TypeInfo(Walker 
2865c 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74  *pWalker, Select
2865d 20 2a 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70   *p){.  Parse *p
2865e 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  Parse;.  int i;.
2865f 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
28660 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 53 72  ist;.  struct Sr
28661 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f  cList_item *pFro
28662 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d  m;..  assert( p-
28663 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52  >selFlags & SF_R
28664 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 69 66 28  esolved );.  if(
28665 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
28666 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 29 3d  SF_HasTypeInfo)=
28667 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 73 65 6c  =0 ){.    p->sel
28668 46 6c 61 67 73 20 7c 3d 20 53 46 5f 48 61 73 54  Flags |= SF_HasT
28669 79 70 65 49 6e 66 6f 3b 0a 20 20 20 20 70 50 61  ypeInfo;.    pPa
2866a 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70  rse = pWalker->p
2866b 50 61 72 73 65 3b 0a 20 20 20 20 70 54 61 62 4c  Parse;.    pTabL
2866c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  ist = p->pSrc;. 
2866d 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f     for(i=0, pFro
2866e 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69  m=pTabList->a; i
2866f 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pTabList->nSrc;
28670 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a   i++, pFrom++){.
28671 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
28672 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b  b = pFrom->pTab;
28673 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20  .      if( pTab 
28674 26 26 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61  && (pTab->tabFla
28675 67 73 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61  gs & TF_Ephemera
28676 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)!=0 ){.       
28677 20 2f 2a 20 41 20 73 75 62 2d 71 75 65 72 79 20   /* A sub-query 
28678 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
28679 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 2a  se of a SELECT *
2867a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63 74  /.        Select
2867b 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e   *pSel = pFrom->
2867c 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 20  pSelect;.       
2867d 20 61 73 73 65 72 74 28 20 70 53 65 6c 20 29 3b   assert( pSel );
2867e 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
2867f 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20 29 20 70  pSel->pPrior ) p
28680 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69  Sel = pSel->pPri
28681 6f 72 3b 0a 20 20 20 20 20 20 20 20 73 65 6c 65  or;.        sele
28682 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41  ctAddColumnTypeA
28683 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 72  ndCollation(pPar
28684 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20  se, pTab->nCol, 
28685 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c  pTab->aCol, pSel
28686 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
28687 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52  .  }.  return WR
28688 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 23 65  C_Continue;.}.#e
28689 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ndif.../*.** Thi
2868a 73 20 72 6f 75 74 69 6e 65 20 61 64 64 73 20 64  s routine adds d
2868b 61 74 61 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c  atatype and coll
2868c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  ating sequence i
2868d 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 0a 2a 2a  nformation to.**
2868e 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
2868f 74 75 72 65 73 20 6f 66 20 61 6c 6c 20 46 52 4f  tures of all FRO
28690 4d 2d 63 6c 61 75 73 65 20 73 75 62 71 75 65 72  M-clause subquer
28691 69 65 73 20 69 6e 20 61 0a 2a 2a 20 53 45 4c 45  ies in a.** SELE
28692 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
28693 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 72 6f 75  .** Use this rou
28694 74 69 6e 65 20 61 66 74 65 72 20 6e 61 6d 65 20  tine after name 
28695 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a 73  resolution..*/.s
28696 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
28697 65 33 53 65 6c 65 63 74 41 64 64 54 79 70 65 49  e3SelectAddTypeI
28698 6e 66 6f 28 50 61 72 73 65 20 2a 70 50 61 72 73  nfo(Parse *pPars
28699 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  e, Select *pSele
2869a 63 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ct){.#ifndef SQL
2869b 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2869c 59 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20  Y.  Walker w;.  
2869d 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  w.xSelectCallbac
2869e 6b 20 3d 20 73 65 6c 65 63 74 41 64 64 53 75 62  k = selectAddSub
2869f 71 75 65 72 79 54 79 70 65 49 6e 66 6f 3b 0a 20  queryTypeInfo;. 
286a0 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   w.xExprCallback
286a1 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b   = exprWalkNoop;
286a2 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50  .  w.pParse = pP
286a3 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57  arse;.  sqlite3W
286a4 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 53  alkSelect(&w, pS
286a5 65 6c 65 63 74 29 3b 0a 23 65 6e 64 69 66 0a 7d  elect);.#endif.}
286a6 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
286a7 75 74 69 6e 65 20 73 65 74 73 20 6f 66 20 61 20  utine sets of a 
286a8 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
286a9 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e   for processing.
286aa 20 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69    The.** followi
286ab 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68  ng is accomplish
286ac 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20  ed:.**.**     * 
286ad 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d   VDBE Cursor num
286ae 62 65 72 73 20 61 72 65 20 61 73 73 69 67 6e 65  bers are assigne
286af 64 20 74 6f 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c  d to all FROM-cl
286b0 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 20  ause terms..**  
286b1 20 20 20 2a 20 20 45 70 68 65 6d 65 72 61 6c 20     *  Ephemeral 
286b2 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72  Table objects ar
286b3 65 20 63 72 65 61 74 65 64 20 66 6f 72 20 61 6c  e created for al
286b4 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75  l FROM-clause su
286b5 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 20 20 20  bqueries..**    
286b6 20 2a 20 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47   *  ON and USING
286b7 20 63 6c 61 75 73 65 73 20 61 72 65 20 73 68 69   clauses are shi
286b8 66 74 65 64 20 69 6e 74 6f 20 57 48 45 52 45 20  fted into WHERE 
286b9 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 20 20  statements.**   
286ba 20 20 2a 20 20 57 69 6c 64 63 61 72 64 73 20 22    *  Wildcards "
286bb 2a 22 20 61 6e 64 20 22 54 41 42 4c 45 2e 2a 22  *" and "TABLE.*"
286bc 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 73 20   in result sets 
286bd 61 72 65 20 65 78 70 61 6e 64 65 64 2e 0a 2a 2a  are expanded..**
286be 20 20 20 20 20 2a 20 20 49 64 65 6e 74 69 66 69       *  Identifi
286bf 65 72 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f  ers in expressio
286c0 6e 20 61 72 65 20 6d 61 74 63 68 65 64 20 74 6f  n are matched to
286c1 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54   tables..**.** T
286c2 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 74 73  his routine acts
286c3 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20   recursively on 
286c4 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 20 77  all subqueries w
286c5 69 74 68 69 6e 20 74 68 65 20 53 45 4c 45 43 54  ithin the SELECT
286c6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
286c7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
286c8 53 65 6c 65 63 74 50 72 65 70 28 0a 20 20 50 61  SelectPrep(.  Pa
286c9 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
286ca 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
286cb 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
286cc 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
286cd 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45         /* The SE
286ce 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62  LECT statement b
286cf 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20  eing coded. */. 
286d0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f   NameContext *pO
286d1 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20  uterNC  /* Name 
286d2 63 6f 6e 74 65 78 74 20 66 6f 72 20 63 6f 6e 74  context for cont
286d3 61 69 6e 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71  ainer */.){.  sq
286d4 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 66 28  lite3 *db;.  if(
286d5 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   p==0 ) return;.
286d6 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
286d7 62 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46  b;.  if( p->selF
286d8 6c 61 67 73 20 26 20 53 46 5f 48 61 73 54 79 70  lags & SF_HasTyp
286d9 65 49 6e 66 6f 20 29 20 72 65 74 75 72 6e 3b 0a  eInfo ) return;.
286da 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
286db 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
286dc 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b  Failed ) return;
286dd 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
286de 45 78 70 61 6e 64 28 70 50 61 72 73 65 2c 20 70  Expand(pParse, p
286df 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  );.  if( pParse-
286e0 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
286e1 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
286e2 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73  rn;.  sqlite3Res
286e3 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28  olveSelectNames(
286e4 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65  pParse, p, pOute
286e5 72 4e 43 29 3b 0a 20 20 69 66 28 20 70 50 61 72  rNC);.  if( pPar
286e6 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
286e7 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
286e8 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
286e9 53 65 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66  SelectAddTypeInf
286ea 6f 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 7d 0a  o(pParse, p);.}.
286eb 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65  ./*.** Reset the
286ec 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d   aggregate accum
286ed 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ulator..**.** Th
286ee 65 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75  e aggregate accu
286ef 6d 75 6c 61 74 6f 72 20 69 73 20 61 20 73 65 74  mulator is a set
286f0 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73   of memory cells
286f1 20 74 68 61 74 20 68 6f 6c 64 0a 2a 2a 20 69 6e   that hold.** in
286f2 74 65 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c  termediate resul
286f3 74 73 20 77 68 69 6c 65 20 63 61 6c 63 75 6c 61  ts while calcula
286f4 74 69 6e 67 20 61 6e 20 61 67 67 72 65 67 61 74  ting an aggregat
286f5 65 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74  e.  This.** rout
286f6 69 6e 65 20 73 69 6d 70 6c 79 20 73 74 6f 72 65  ine simply store
286f7 73 20 4e 55 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f  s NULLs in all o
286f8 66 20 74 68 6f 73 65 20 6d 65 6d 6f 72 79 20 63  f those memory c
286f9 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ells..*/.static 
286fa 76 6f 69 64 20 72 65 73 65 74 41 63 63 75 6d 75  void resetAccumu
286fb 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61  lator(Parse *pPa
286fc 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41  rse, AggInfo *pA
286fd 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20  ggInfo){.  Vdbe 
286fe 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
286ff 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  be;.  int i;.  s
28700 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
28701 6e 63 20 2a 70 46 75 6e 63 3b 0a 20 20 69 66 28  nc *pFunc;.  if(
28702 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63   pAggInfo->nFunc
28703 2b 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75  +pAggInfo->nColu
28704 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  mn==0 ){.    ret
28705 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  urn;.  }.  for(i
28706 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e  =0; i<pAggInfo->
28707 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
28708 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28709 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2870a 20 30 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43   0, pAggInfo->aC
2870b 6f 6c 5b 69 5d 2e 69 4d 65 6d 29 3b 0a 20 20 7d  ol[i].iMem);.  }
2870c 0a 20 20 66 6f 72 28 70 46 75 6e 63 3d 70 41 67  .  for(pFunc=pAg
2870d 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d  gInfo->aFunc, i=
2870e 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  0; i<pAggInfo->n
2870f 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 75 6e 63  Func; i++, pFunc
28710 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
28711 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28712 5f 4e 75 6c 6c 2c 20 30 2c 20 70 46 75 6e 63 2d  _Null, 0, pFunc-
28713 3e 69 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20  >iMem);.    if( 
28714 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74  pFunc->iDistinct
28715 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70  >=0 ){.      Exp
28716 72 20 2a 70 45 20 3d 20 70 46 75 6e 63 2d 3e 70  r *pE = pFunc->p
28717 45 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65  Expr;.      asse
28718 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
28719 65 72 74 79 28 70 45 2c 20 45 50 5f 78 49 73 53  erty(pE, EP_xIsS
2871a 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20  elect) );.      
2871b 69 66 28 20 70 45 2d 3e 78 2e 70 4c 69 73 74 3d  if( pE->x.pList=
2871c 3d 30 20 7c 7c 20 70 45 2d 3e 78 2e 70 4c 69 73  =0 || pE->x.pLis
2871d 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20  t->nExpr!=1 ){. 
2871e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
2871f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
28720 44 49 53 54 49 4e 43 54 20 61 67 67 72 65 67 61  DISTINCT aggrega
28721 74 65 73 20 6d 75 73 74 20 68 61 76 65 20 65 78  tes must have ex
28722 61 63 74 6c 79 20 6f 6e 65 20 22 0a 20 20 20 20  actly one ".    
28723 20 20 20 20 20 20 20 22 61 72 67 75 6d 65 6e 74         "argument
28724 22 29 3b 0a 20 20 20 20 20 20 20 20 70 46 75 6e  ");.        pFun
28725 63 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d  c->iDistinct = -
28726 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
28727 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20          KeyInfo 
28728 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49  *pKeyInfo = keyI
28729 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  nfoFromExprList(
2872a 70 50 61 72 73 65 2c 20 70 45 2d 3e 78 2e 70 4c  pParse, pE->x.pL
2872b 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ist);.        sq
2872c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2872d 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
2872e 72 61 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73  ral, pFunc->iDis
2872f 74 69 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20  tinct, 0, 0,.   
28730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28731 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
28732 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e  eyInfo, P4_KEYIN
28733 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
28734 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d     }.    }.  }.}
28735 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74  ../*.** Invoke t
28736 68 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a  he OP_AggFinaliz
28737 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65  e opcode for eve
28738 72 79 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  ry aggregate fun
28739 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20  ction.** in the 
2873a 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72  AggInfo structur
2873b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
2873c 64 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e  d finalizeAggFun
2873d 63 74 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50  ctions(Parse *pP
2873e 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70  arse, AggInfo *p
2873f 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65  AggInfo){.  Vdbe
28740 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
28741 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  dbe;.  int i;.  
28742 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66  struct AggInfo_f
28743 75 6e 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69  unc *pF;.  for(i
28744 3d 30 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d  =0, pF=pAggInfo-
28745 3e 61 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e  >aFunc; i<pAggIn
28746 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20  fo->nFunc; i++, 
28747 70 46 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c  pF++){.    ExprL
28748 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d  ist *pList = pF-
28749 3e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b  >pExpr->x.pList;
2874a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78  .    assert( !Ex
2874b 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 46  prHasProperty(pF
2874c 2d 3e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  ->pExpr, EP_xIsS
2874d 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 73 71  elect) );.    sq
2874e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2874f 76 2c 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20  v, OP_AggFinal, 
28750 70 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20  pF->iMem, pList 
28751 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a  ? pList->nExpr :
28752 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   0, 0,.         
28753 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f               (vo
28754 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50  id*)pF->pFunc, P
28755 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a  4_FUNCDEF);.  }.
28756 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20  }../*.** Update 
28757 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  the accumulator 
28758 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72  memory cells for
28759 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 62 61   an aggregate ba
2875a 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75  sed on.** the cu
2875b 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73  rrent cursor pos
2875c 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ition..*/.static
2875d 20 76 6f 69 64 20 75 70 64 61 74 65 41 63 63 75   void updateAccu
2875e 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70  mulator(Parse *p
2875f 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a  Parse, AggInfo *
28760 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62  pAggInfo){.  Vdb
28761 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
28762 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Vdbe;.  int i;. 
28763 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
28764 66 75 6e 63 20 2a 70 46 3b 0a 20 20 73 74 72 75  func *pF;.  stru
28765 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a  ct AggInfo_col *
28766 70 43 3b 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d  pC;..  pAggInfo-
28767 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b  >directMode = 1;
28768 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70  .  for(i=0, pF=p
28769 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20  AggInfo->aFunc; 
2876a 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e  i<pAggInfo->nFun
2876b 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20  c; i++, pF++){. 
2876c 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20     int nArg;.   
2876d 20 69 6e 74 20 61 64 64 72 4e 65 78 74 20 3d 20   int addrNext = 
2876e 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67  0;.    int regAg
2876f 67 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  g;.    ExprList 
28770 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78  *pList = pF->pEx
28771 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20  pr->x.pList;.   
28772 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
28773 73 50 72 6f 70 65 72 74 79 28 70 46 2d 3e 70 45  sProperty(pF->pE
28774 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
28775 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  t) );.    if( pL
28776 69 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 41 72  ist ){.      nAr
28777 67 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  g = pList->nExpr
28778 3b 0a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d  ;.      regAgg =
28779 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2877a 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 41 72  ange(pParse, nAr
2877b 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
2877c 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
2877d 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  t(pParse, pList,
2877e 20 72 65 67 41 67 67 2c 20 30 29 3b 0a 20 20 20   regAgg, 0);.   
2877f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 41   }else{.      nA
28780 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  rg = 0;.      re
28781 67 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  gAgg = 0;.    }.
28782 20 20 20 20 69 66 28 20 70 46 2d 3e 69 44 69 73      if( pF->iDis
28783 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20  tinct>=0 ){.    
28784 20 20 61 64 64 72 4e 65 78 74 20 3d 20 73 71 6c    addrNext = sql
28785 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
28786 6c 28 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65  l(v);.      asse
28787 72 74 28 20 6e 41 72 67 3d 3d 31 20 29 3b 0a 20  rt( nArg==1 );. 
28788 20 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63       codeDistinc
28789 74 28 70 50 61 72 73 65 2c 20 70 46 2d 3e 69 44  t(pParse, pF->iD
2878a 69 73 74 69 6e 63 74 2c 20 61 64 64 72 4e 65 78  istinct, addrNex
2878b 74 2c 20 31 2c 20 72 65 67 41 67 67 29 3b 0a 20  t, 1, regAgg);. 
2878c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d     }.    if( pF-
2878d 3e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20  >pFunc->flags & 
2878e 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
2878f 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 43 6f  COLL ){.      Co
28790 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30  llSeq *pColl = 0
28791 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  ;.      struct E
28792 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
28793 74 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  tem;.      int j
28794 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
28795 70 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20  pList!=0 );  /* 
28796 70 4c 69 73 74 21 3d 30 20 69 66 20 70 46 2d 3e  pList!=0 if pF->
28797 70 46 75 6e 63 20 68 61 73 20 4e 45 45 44 43 4f  pFunc has NEEDCO
28798 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28  LL */.      for(
28799 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74  j=0, pItem=pList
2879a 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a  ->a; !pColl && j
2879b 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65  <nArg; j++, pIte
2879c 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 43  m++){.        pC
2879d 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
2879e 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
2879f 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a   pItem->pExpr);.
287a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
287a1 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
287a2 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72      pColl = pPar
287a3 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  se->db->pDfltCol
287a4 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  l;.      }.     
287a5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
287a6 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71  p4(v, OP_CollSeq
287a7 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72  , 0, 0, 0, (char
287a8 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c   *)pColl, P4_COL
287a9 4c 53 45 51 29 3b 0a 20 20 20 20 7d 0a 20 20 20  LSEQ);.    }.   
287aa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
287ab 70 34 28 76 2c 20 4f 50 5f 41 67 67 53 74 65 70  p4(v, OP_AggStep
287ac 2c 20 30 2c 20 72 65 67 41 67 67 2c 20 70 46 2d  , 0, regAgg, pF-
287ad 3e 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20  >iMem,.         
287ae 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f               (vo
287af 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50  id*)pF->pFunc, P
287b0 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20  4_FUNCDEF);.    
287b1 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
287b2 65 50 35 28 76 2c 20 28 75 38 29 6e 41 72 67 29  eP5(v, (u8)nArg)
287b3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  ;.    sqlite3Rel
287b4 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
287b5 61 72 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41  arse, regAgg, nA
287b6 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rg);.    sqlite3
287b7 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
287b8 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
287b9 72 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20  regAgg, nArg);. 
287ba 20 20 20 69 66 28 20 61 64 64 72 4e 65 78 74 20     if( addrNext 
287bb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
287bc 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
287bd 28 76 2c 20 61 64 64 72 4e 65 78 74 29 3b 0a 20  (v, addrNext);. 
287be 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
287bf 3d 30 2c 20 70 43 3d 70 41 67 67 49 6e 66 6f 2d  =0, pC=pAggInfo-
287c0 3e 61 43 6f 6c 3b 20 69 3c 70 41 67 67 49 6e 66  >aCol; i<pAggInf
287c1 6f 2d 3e 6e 41 63 63 75 6d 75 6c 61 74 6f 72 3b  o->nAccumulator;
287c2 20 69 2b 2b 2c 20 70 43 2b 2b 29 7b 0a 20 20 20   i++, pC++){.   
287c3 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
287c4 28 70 50 61 72 73 65 2c 20 70 43 2d 3e 70 45 78  (pParse, pC->pEx
287c5 70 72 2c 20 70 43 2d 3e 69 4d 65 6d 29 3b 0a 20  pr, pC->iMem);. 
287c6 20 7d 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64   }.  pAggInfo->d
287c7 69 72 65 63 74 4d 6f 64 65 20 3d 20 30 3b 0a 7d  irectMode = 0;.}
287c8 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
287c9 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 53 45   code for the SE
287ca 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 67  LECT statement g
287cb 69 76 65 6e 20 69 6e 20 74 68 65 20 70 20 61 72  iven in the p ar
287cc 67 75 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20  gument.  .**.** 
287cd 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  The results are 
287ce 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 76  distributed in v
287cf 61 72 69 6f 75 73 20 77 61 79 73 20 64 65 70 65  arious ways depe
287d0 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a 2a 2a 20  nding on the.** 
287d1 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
287d2 53 65 6c 65 63 74 44 65 73 74 20 73 74 72 75 63  SelectDest struc
287d3 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  ture pointed to 
287d4 62 79 20 61 72 67 75 6d 65 6e 74 20 70 44 65 73  by argument pDes
287d5 74 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  t.** as follows:
287d6 0a 2a 2a 0a 2a 2a 20 20 20 20 20 70 44 65 73 74  .**.**     pDest
287d7 2d 3e 65 44 65 73 74 20 20 20 20 52 65 73 75 6c  ->eDest    Resul
287d8 74 0a 2a 2a 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  t.**     -------
287d9 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  -----    -------
287da 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
287db 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
287dc 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ----.**     SRT_
287dd 4f 75 74 70 75 74 20 20 20 20 20 20 47 65 6e 65  Output      Gene
287de 72 61 74 65 20 61 20 72 6f 77 20 6f 66 20 6f 75  rate a row of ou
287df 74 70 75 74 20 28 75 73 69 6e 67 20 74 68 65 20  tput (using the 
287e0 4f 50 5f 52 65 73 75 6c 74 52 6f 77 0a 2a 2a 20  OP_ResultRow.** 
287e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
287e2 20 20 20 20 6f 70 63 6f 64 65 29 20 66 6f 72 20      opcode) for 
287e3 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20  each row in the 
287e4 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a  result set..**.*
287e5 2a 20 20 20 20 20 53 52 54 5f 4d 65 6d 20 20 20  *     SRT_Mem   
287e6 20 20 20 20 20 20 4f 6e 6c 79 20 76 61 6c 69 64        Only valid
287e7 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   if the result i
287e8 73 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  s a single colum
287e9 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n..**           
287ea 20 20 20 20 20 20 20 20 20 20 53 74 6f 72 65 20            Store 
287eb 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  the first column
287ec 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 72 65   of the first re
287ed 73 75 6c 74 20 72 6f 77 0a 2a 2a 20 20 20 20 20  sult row.**     
287ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
287ef 69 6e 20 72 65 67 69 73 74 65 72 20 70 44 65 73  in register pDes
287f0 74 2d 3e 69 50 61 72 6d 20 74 68 65 6e 20 61 62  t->iParm then ab
287f1 61 6e 64 6f 6e 20 74 68 65 20 72 65 73 74 0a 2a  andon the rest.*
287f2 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
287f3 20 20 20 20 20 20 6f 66 20 74 68 65 20 71 75 65        of the que
287f4 72 79 2e 20 20 54 68 69 73 20 64 65 73 74 69 6e  ry.  This destin
287f5 61 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 22 4c  ation implies "L
287f6 49 4d 49 54 20 31 22 2e 0a 2a 2a 0a 2a 2a 20 20  IMIT 1"..**.**  
287f7 20 20 20 53 52 54 5f 53 65 74 20 20 20 20 20 20     SRT_Set      
287f8 20 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 75     The result mu
287f9 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63  st be a single c
287fa 6f 6c 75 6d 6e 2e 20 20 53 74 6f 72 65 20 65 61  olumn.  Store ea
287fb 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ch.**           
287fc 20 20 20 20 20 20 20 20 20 20 72 6f 77 20 6f 66            row of
287fd 20 72 65 73 75 6c 74 20 61 73 20 74 68 65 20 6b   result as the k
287fe 65 79 20 69 6e 20 74 61 62 6c 65 20 70 44 65 73  ey in table pDes
287ff 74 2d 3e 69 50 61 72 6d 2e 20 0a 2a 2a 20 20 20  t->iParm. .**   
28800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28801 20 20 41 70 70 6c 79 20 74 68 65 20 61 66 66 69    Apply the affi
28802 6e 69 74 79 20 70 44 65 73 74 2d 3e 61 66 66 69  nity pDest->affi
28803 6e 69 74 79 20 62 65 66 6f 72 65 20 73 74 6f 72  nity before stor
28804 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
28805 20 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c             resul
28806 74 73 2e 20 20 55 73 65 64 20 74 6f 20 69 6d 70  ts.  Used to imp
28807 6c 65 6d 65 6e 74 20 22 49 4e 20 28 53 45 4c 45  lement "IN (SELE
28808 43 54 20 2e 2e 2e 29 22 2e 0a 2a 2a 0a 2a 2a 20  CT ...)"..**.** 
28809 20 20 20 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20      SRT_Union   
2880a 20 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74      Store result
2880b 73 20 61 73 20 61 20 6b 65 79 20 69 6e 20 61 20  s as a key in a 
2880c 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2880d 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a  pDest->iParm..**
2880e 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45 78 63 65  .**     SRT_Exce
2880f 70 74 20 20 20 20 20 20 52 65 6d 6f 76 65 20 72  pt      Remove r
28810 65 73 75 6c 74 73 20 66 72 6f 6d 20 74 68 65 20  esults from the 
28811 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
28812 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a  pDest->iParm..**
28813 0a 2a 2a 20 20 20 20 20 53 52 54 5f 54 61 62 6c  .**     SRT_Tabl
28814 65 20 20 20 20 20 20 20 53 74 6f 72 65 20 72 65  e       Store re
28815 73 75 6c 74 73 20 69 6e 20 74 65 6d 70 6f 72 61  sults in tempora
28816 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e  ry table pDest->
28817 69 50 61 72 6d 2e 0a 2a 2a 20 20 20 20 20 20 20  iParm..**       
28818 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
28819 69 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f 45  is is like SRT_E
2881a 70 68 65 6d 54 61 62 20 65 78 63 65 70 74 20 74  phemTab except t
2881b 68 61 74 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a  hat the table.**
2881c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2881d 20 20 20 20 20 69 73 20 61 73 73 75 6d 65 64 20       is assumed 
2881e 74 6f 20 61 6c 72 65 61 64 79 20 62 65 20 6f 70  to already be op
2881f 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  en..**.**     SR
28820 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 43 72  T_EphemTab    Cr
28821 65 61 74 65 20 61 6e 20 74 65 6d 70 6f 72 61 72  eate an temporar
28822 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69  y table pDest->i
28823 50 61 72 6d 20 61 6e 64 20 73 74 6f 72 65 0a 2a  Parm and store.*
28824 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
28825 20 20 20 20 20 20 74 68 65 20 72 65 73 75 6c 74        the result
28826 20 74 68 65 72 65 2e 20 54 68 65 20 63 75 72 73   there. The curs
28827 6f 72 20 69 73 20 6c 65 66 74 20 6f 70 65 6e 20  or is left open 
28828 61 66 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20  after.**        
28829 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
2882a 75 72 6e 69 6e 67 2e 20 20 54 68 69 73 20 69 73  urning.  This is
2882b 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20   like SRT_Table 
2882c 65 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 20  except that.**  
2882d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2882e 20 20 20 74 68 69 73 20 64 65 73 74 69 6e 61 74     this destinat
2882f 69 6f 6e 20 75 73 65 73 20 4f 50 5f 4f 70 65 6e  ion uses OP_Open
28830 45 70 68 65 6d 65 72 61 6c 20 74 6f 20 63 72 65  Ephemeral to cre
28831 61 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ate.**          
28832 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74             the t
28833 61 62 6c 65 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a  able first..**.*
28834 2a 20 20 20 20 20 53 52 54 5f 43 6f 72 6f 75 74  *     SRT_Corout
28835 69 6e 65 20 20 20 47 65 6e 65 72 61 74 65 20 61  ine   Generate a
28836 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74   co-routine that
28837 20 72 65 74 75 72 6e 73 20 61 20 6e 65 77 20 72   returns a new r
28838 6f 77 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ow of.**        
28839 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 73               res
2883a 75 6c 74 73 20 65 61 63 68 20 74 69 6d 65 20 69  ults each time i
2883b 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 54  t is invoked.  T
2883c 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a  he entry point.*
2883d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2883e 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f 2d        of the co-
2883f 72 6f 75 74 69 6e 65 20 69 73 20 73 74 6f 72 65  routine is store
28840 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 44  d in register pD
28841 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a  est->iParm..**.*
28842 2a 20 20 20 20 20 53 52 54 5f 45 78 69 73 74 73  *     SRT_Exists
28843 20 20 20 20 20 20 53 74 6f 72 65 20 61 20 31 20        Store a 1 
28844 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70  in memory cell p
28845 44 65 73 74 2d 3e 69 50 61 72 6d 20 69 66 20 74  Dest->iParm if t
28846 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 20 20 20  he result.**    
28847 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28848 20 73 65 74 20 69 73 20 6e 6f 74 20 65 6d 70 74   set is not empt
28849 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  y..**.**     SRT
2884a 5f 44 69 73 63 61 72 64 20 20 20 20 20 54 68 72  _Discard     Thr
2884b 6f 77 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  ow the results a
2884c 77 61 79 2e 20 20 54 68 69 73 20 69 73 20 75 73  way.  This is us
2884d 65 64 20 62 79 20 53 45 4c 45 43 54 0a 2a 2a 20  ed by SELECT.** 
2884e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2884f 20 20 20 20 73 74 61 74 65 6d 65 6e 74 73 20 77      statements w
28850 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 77  ithin triggers w
28851 68 6f 73 65 20 6f 6e 6c 79 20 70 75 72 70 6f 73  hose only purpos
28852 65 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  e is.**         
28853 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
28854 73 69 64 65 2d 65 66 66 65 63 74 73 20 6f 66 20  side-effects of 
28855 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  functions..**.**
28856 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
28857 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
28858 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20   of errors.  If 
28859 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 0a 2a  any errors are.*
2885a 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74  * encountered, t
2885b 68 65 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61  hen an appropria
2885c 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
2885d 20 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a 20 70   is left in.** p
2885e 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a  Parse->zErrMsg..
2885f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
28860 6e 65 20 64 6f 65 73 20 4e 4f 54 20 66 72 65 65  ne does NOT free
28861 20 74 68 65 20 53 65 6c 65 63 74 20 73 74 72 75   the Select stru
28862 63 74 75 72 65 20 70 61 73 73 65 64 20 69 6e 2e  cture passed in.
28863 20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67    The.** calling
28864 20 66 75 6e 63 74 69 6f 6e 20 6e 65 65 64 73 20   function needs 
28865 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 53  to do that..*/.S
28866 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
28867 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  t sqlite3Select(
28868 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
28869 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2886a 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
2886b 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
2886c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2886d 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2886e 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e  ent being coded.
2886f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
28870 20 2a 70 44 65 73 74 20 20 20 20 20 20 2f 2a 20   *pDest      /* 
28871 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
28872 74 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74  the query result
28873 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  s */.){.  int i,
28874 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
28875 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
28876 73 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f  s */.  WhereInfo
28877 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a   *pWInfo;     /*
28878 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c   Return from sql
28879 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2887a 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
2887b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2887c 54 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  The virtual mach
2887d 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  ine under constr
2887e 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  uction */.  int 
2887f 69 73 41 67 67 3b 20 20 20 20 20 20 20 20 20 20  isAgg;          
28880 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 73     /* True for s
28881 65 6c 65 63 74 20 6c 69 73 74 73 20 6c 69 6b 65  elect lists like
28882 20 22 63 6f 75 6e 74 28 2a 29 22 20 2a 2f 0a 20   "count(*)" */. 
28883 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
28884 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  t;      /* List 
28885 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 65 78  of columns to ex
28886 74 72 61 63 74 2e 20 2a 2f 0a 20 20 53 72 63 4c  tract. */.  SrcL
28887 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20  ist *pTabList;  
28888 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61     /* List of ta
28889 62 6c 65 73 20 74 6f 20 73 65 6c 65 63 74 20 66  bles to select f
2888a 72 6f 6d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  rom */.  Expr *p
2888b 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 20 20  Where;          
2888c 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2888d 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  use.  May be NUL
2888e 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  L */.  ExprList 
2888f 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a  *pOrderBy;    /*
28890 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
28891 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  ause.  May be NU
28892 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  LL */.  ExprList
28893 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 2f   *pGroupBy;    /
28894 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 63  * The GROUP BY c
28895 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e  lause.  May be N
28896 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ULL */.  Expr *p
28897 48 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20  Having;         
28898 2f 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c  /* The HAVING cl
28899 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  ause.  May be NU
2889a 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69  LL */.  int isDi
2889b 73 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f  stinct;        /
2889c 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44 49  * True if the DI
2889d 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69  STINCT keyword i
2889e 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69  s present */.  i
2889f 6e 74 20 64 69 73 74 69 6e 63 74 3b 20 20 20 20  nt distinct;    
288a0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74        /* Table t
288a1 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 64 69  o use for the di
288a2 73 74 69 6e 63 74 20 73 65 74 20 2a 2f 0a 20 20  stinct set */.  
288a3 69 6e 74 20 72 63 20 3d 20 31 3b 20 20 20 20 20  int rc = 1;     
288a4 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
288a5 74 6f 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74  to return from t
288a6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  his function */.
288a7 20 20 69 6e 74 20 61 64 64 72 53 6f 72 74 49 6e    int addrSortIn
288a8 64 65 78 3b 20 20 20 20 20 2f 2a 20 41 64 64 72  dex;     /* Addr
288a9 65 73 73 20 6f 66 20 61 6e 20 4f 50 5f 4f 70 65  ess of an OP_Ope
288aa 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
288ab 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49  uction */.  AggI
288ac 6e 66 6f 20 73 41 67 67 49 6e 66 6f 3b 20 20 20  nfo sAggInfo;   
288ad 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
288ae 6e 20 75 73 65 64 20 62 79 20 61 67 67 72 65 67  n used by aggreg
288af 61 74 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20  ate queries */. 
288b0 20 69 6e 74 20 69 45 6e 64 3b 20 20 20 20 20 20   int iEnd;      
288b1 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
288b2 73 73 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66  ss of the end of
288b3 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20   the query */.  
288b4 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
288b5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
288b6 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
288b7 6e 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61  n */..  db = pPa
288b8 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
288b9 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
288ba 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73  cFailed || pPars
288bb 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 72  e->nErr ){.    r
288bc 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69  eturn 1;.  }.  i
288bd 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
288be 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
288bf 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c  TE_SELECT, 0, 0,
288c0 20 30 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a   0) ) return 1;.
288c1 20 20 6d 65 6d 73 65 74 28 26 73 41 67 67 49 6e    memset(&sAggIn
288c2 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41  fo, 0, sizeof(sA
288c3 67 67 49 6e 66 6f 29 29 3b 0a 0a 20 20 70 4f 72  ggInfo));..  pOr
288c4 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65  derBy = p->pOrde
288c5 72 42 79 3b 0a 20 20 69 66 28 20 49 67 6e 6f 72  rBy;.  if( Ignor
288c6 61 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73  ableOrderby(pDes
288c7 74 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4f 72  t) ){.    p->pOr
288c8 64 65 72 42 79 20 3d 20 30 3b 0a 0a 20 20 20 20  derBy = 0;..    
288c9 2f 2a 20 49 6e 20 74 68 65 73 65 20 63 61 73 65  /* In these case
288ca 73 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6f  s the DISTINCT o
288cb 70 65 72 61 74 6f 72 20 6d 61 6b 65 73 20 6e 6f  perator makes no
288cc 20 64 69 66 66 65 72 65 6e 63 65 20 74 6f 20 74   difference to t
288cd 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74  he.    ** result
288ce 73 2c 20 73 6f 20 72 65 6d 6f 76 65 20 69 74 20  s, so remove it 
288cf 69 66 20 69 74 20 77 65 72 65 20 73 70 65 63 69  if it were speci
288d0 66 69 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  fied..    */.   
288d1 20 61 73 73 65 72 74 28 70 44 65 73 74 2d 3e 65   assert(pDest->e
288d2 44 65 73 74 3d 3d 53 52 54 5f 45 78 69 73 74 73  Dest==SRT_Exists
288d3 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74   || pDest->eDest
288d4 3d 3d 53 52 54 5f 55 6e 69 6f 6e 20 7c 7c 20 0a  ==SRT_Union || .
288d5 20 20 20 20 20 20 20 20 20 20 20 70 44 65 73 74             pDest
288d6 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 78 63  ->eDest==SRT_Exc
288d7 65 70 74 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44  ept || pDest->eD
288d8 65 73 74 3d 3d 53 52 54 5f 44 69 73 63 61 72 64  est==SRT_Discard
288d9 29 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61  );.    p->selFla
288da 67 73 20 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e  gs &= ~SF_Distin
288db 63 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ct;.  }.  sqlite
288dc 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 72  3SelectPrep(pPar
288dd 73 65 2c 20 70 2c 20 30 29 3b 0a 20 20 70 54 61  se, p, 0);.  pTa
288de 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b  bList = p->pSrc;
288df 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70  .  pEList = p->p
288e0 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 50 61  EList;.  if( pPa
288e1 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
288e2 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
288e3 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74  .    goto select
288e4 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  _end;.  }.  p->p
288e5 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
288e6 42 79 3b 0a 20 20 69 73 41 67 67 20 3d 20 28 70  By;.  isAgg = (p
288e7 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
288e8 41 67 67 72 65 67 61 74 65 29 21 3d 30 3b 0a 20  Aggregate)!=0;. 
288e9 20 69 66 28 20 70 45 4c 69 73 74 3d 3d 30 20 29   if( pEList==0 )
288ea 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
288eb 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 44 6f  ;..  /* .  ** Do
288ec 20 6e 6f 74 20 65 76 65 6e 20 61 74 74 65 6d 70   not even attemp
288ed 74 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e  t to generate an
288ee 79 20 63 6f 64 65 20 69 66 20 77 65 20 68 61 76  y code if we hav
288ef 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e 0a 20  e already seen. 
288f0 20 2a 2a 20 65 72 72 6f 72 73 20 62 65 66 6f 72   ** errors befor
288f1 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  e this routine s
288f2 74 61 72 74 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  tarts..  */.  if
288f3 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30  ( pParse->nErr>0
288f4 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65   ) goto select_e
288f5 6e 64 3b 0a 0a 20 20 2f 2a 20 4f 52 44 45 52 20  nd;..  /* ORDER 
288f6 42 59 20 69 73 20 69 67 6e 6f 72 65 64 20 66 6f  BY is ignored fo
288f7 72 20 73 6f 6d 65 20 64 65 73 74 69 6e 61 74 69  r some destinati
288f8 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ons..  */.  if( 
288f9 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79  IgnorableOrderby
288fa 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 70  (pDest) ){.    p
288fb 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d  OrderBy = 0;.  }
288fc 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e  ..  /* Begin gen
288fd 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20  erating code..  
288fe 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  */.  v = sqlite3
288ff 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
28900 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f  .  if( v==0 ) go
28901 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a  to select_end;..
28902 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
28903 64 65 20 66 6f 72 20 61 6c 6c 20 73 75 62 2d 71  de for all sub-q
28904 75 65 72 69 65 73 20 69 6e 20 74 68 65 20 46 52  ueries in the FR
28905 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 23  OM clause.  */.#
28906 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
28907 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
28908 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
28909 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a  LITE_OMIT_VIEW).
2890a 20 20 66 6f 72 28 69 3d 30 3b 20 21 70 2d 3e 70    for(i=0; !p->p
2890b 50 72 69 6f 72 20 26 26 20 69 3c 70 54 61 62 4c  Prior && i<pTabL
2890c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  ist->nSrc; i++){
2890d 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
2890e 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
2890f 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  = &pTabList->a[i
28910 5d 3b 0a 20 20 20 20 53 65 6c 65 63 74 44 65 73  ];.    SelectDes
28911 74 20 64 65 73 74 3b 0a 20 20 20 20 53 65 6c 65  t dest;.    Sele
28912 63 74 20 2a 70 53 75 62 20 3d 20 70 49 74 65 6d  ct *pSub = pItem
28913 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 69  ->pSelect;.    i
28914 6e 74 20 69 73 41 67 67 53 75 62 3b 0a 0a 20 20  nt isAggSub;..  
28915 20 20 69 66 28 20 70 53 75 62 3d 3d 30 20 7c 7c    if( pSub==0 ||
28916 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61   pItem->isPopula
28917 74 65 64 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ted ) continue;.
28918 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e  .    /* Incremen
28919 74 20 50 61 72 73 65 2e 6e 48 65 69 67 68 74 20  t Parse.nHeight 
2891a 62 79 20 74 68 65 20 68 65 69 67 68 74 20 6f 66  by the height of
2891b 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 78 70   the largest exp
2891c 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20 74  ression.    ** t
2891d 72 65 65 20 72 65 66 65 72 65 64 20 74 6f 20 62  ree refered to b
2891e 79 20 74 68 69 73 2c 20 74 68 65 20 70 61 72 65  y this, the pare
2891f 6e 74 20 73 65 6c 65 63 74 2e 20 54 68 65 20 63  nt select. The c
28920 68 69 6c 64 20 73 65 6c 65 63 74 0a 20 20 20 20  hild select.    
28921 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 65  ** may contain e
28922 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73 20  xpression trees 
28923 6f 66 20 61 74 20 6d 6f 73 74 0a 20 20 20 20 2a  of at most.    *
28924 2a 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  * (SQLITE_MAX_EX
28925 50 52 5f 44 45 50 54 48 2d 50 61 72 73 65 2e 6e  PR_DEPTH-Parse.n
28926 48 65 69 67 68 74 29 20 68 65 69 67 68 74 2e 20  Height) height. 
28927 54 68 69 73 20 69 73 20 61 20 62 69 74 0a 20 20  This is a bit.  
28928 20 20 2a 2a 20 6d 6f 72 65 20 63 6f 6e 73 65 72    ** more conser
28929 76 61 74 69 76 65 20 74 68 61 6e 20 6e 65 63 65  vative than nece
2892a 73 73 61 72 79 2c 20 62 75 74 20 6d 75 63 68 20  ssary, but much 
2892b 65 61 73 69 65 72 20 74 68 61 6e 20 65 6e 66 6f  easier than enfo
2892c 72 63 69 6e 67 0a 20 20 20 20 2a 2a 20 61 6e 20  rcing.    ** an 
2892d 65 78 61 63 74 20 6c 69 6d 69 74 2e 0a 20 20 20  exact limit..   
2892e 20 2a 2f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   */.    pParse->
2892f 6e 48 65 69 67 68 74 20 2b 3d 20 73 71 6c 69 74  nHeight += sqlit
28930 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
28931 68 74 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  ht(p);..    /* C
28932 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
28933 68 65 20 73 75 62 71 75 65 72 79 20 63 61 6e 20  he subquery can 
28934 62 65 20 61 62 73 6f 72 62 65 64 20 69 6e 74 6f  be absorbed into
28935 20 74 68 65 20 70 61 72 65 6e 74 2e 20 2a 2f 0a   the parent. */.
28936 20 20 20 20 69 73 41 67 67 53 75 62 20 3d 20 28      isAggSub = (
28937 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26  pSub->selFlags &
28938 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d   SF_Aggregate)!=
28939 30 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 74 74  0;.    if( flatt
2893a 65 6e 53 75 62 71 75 65 72 79 28 70 50 61 72 73  enSubquery(pPars
2893b 65 2c 20 70 2c 20 69 2c 20 69 73 41 67 67 2c 20  e, p, i, isAgg, 
2893c 69 73 41 67 67 53 75 62 29 20 29 7b 0a 20 20 20  isAggSub) ){.   
2893d 20 20 20 69 66 28 20 69 73 41 67 67 53 75 62 20     if( isAggSub 
2893e 29 7b 0a 20 20 20 20 20 20 20 20 69 73 41 67 67  ){.        isAgg
2893f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 2d   = 1;.        p-
28940 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
28941 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 20  Aggregate;.     
28942 20 7d 0a 20 20 20 20 20 20 69 20 3d 20 2d 31 3b   }.      i = -1;
28943 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
28944 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
28945 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53  estInit(&dest, S
28946 52 54 5f 45 70 68 65 6d 54 61 62 2c 20 70 49 74  RT_EphemTab, pIt
28947 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  em->iCursor);.  
28948 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65      assert( pIte
28949 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3d 3d  m->isPopulated==
2894a 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
2894b 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
2894c 20 70 53 75 62 2c 20 26 64 65 73 74 29 3b 0a 20   pSub, &dest);. 
2894d 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 73 50 6f       pItem->isPo
2894e 70 75 6c 61 74 65 64 20 3d 20 31 3b 0a 20 20 20  pulated = 1;.   
2894f 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 72 73   }.    if( pPars
28950 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
28951 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
28952 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74       goto select
28953 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _end;.    }.    
28954 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20  pParse->nHeight 
28955 2d 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  -= sqlite3Select
28956 45 78 70 72 48 65 69 67 68 74 28 70 29 3b 0a 20  ExprHeight(p);. 
28957 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d     pTabList = p-
28958 3e 70 53 72 63 3b 0a 20 20 20 20 69 66 28 20 21  >pSrc;.    if( !
28959 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79  IgnorableOrderby
2895a 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 20  (pDest) ){.     
2895b 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70   pOrderBy = p->p
2895c 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 7d 0a 20  OrderBy;.    }. 
2895d 20 7d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d   }.  pEList = p-
2895e 3e 70 45 4c 69 73 74 3b 0a 23 65 6e 64 69 66 0a  >pEList;.#endif.
2895f 20 20 70 57 68 65 72 65 20 3d 20 70 2d 3e 70 57    pWhere = p->pW
28960 68 65 72 65 3b 0a 20 20 70 47 72 6f 75 70 42 79  here;.  pGroupBy
28961 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a   = p->pGroupBy;.
28962 20 20 70 48 61 76 69 6e 67 20 3d 20 70 2d 3e 70    pHaving = p->p
28963 48 61 76 69 6e 67 3b 0a 20 20 69 73 44 69 73 74  Having;.  isDist
28964 69 6e 63 74 20 3d 20 28 70 2d 3e 73 65 6c 46 6c  inct = (p->selFl
28965 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63  ags & SF_Distinc
28966 74 29 21 3d 30 3b 0a 0a 23 69 66 6e 64 65 66 20  t)!=0;..#ifndef 
28967 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
28968 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 2f 2a  OUND_SELECT.  /*
28969 20 49 66 20 74 68 65 72 65 20 69 73 20 61 72 65   If there is are
2896a 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 71   a sequence of q
2896b 75 65 72 69 65 73 2c 20 64 6f 20 74 68 65 20 65  ueries, do the e
2896c 61 72 6c 69 65 72 20 6f 6e 65 73 20 66 69 72 73  arlier ones firs
2896d 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  t..  */.  if( p-
2896e 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 69  >pPrior ){.    i
2896f 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  f( p->pRightmost
28970 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 65 6c  ==0 ){.      Sel
28971 65 63 74 20 2a 70 4c 6f 6f 70 2c 20 2a 70 52 69  ect *pLoop, *pRi
28972 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ght = 0;.      i
28973 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20  nt cnt = 0;.    
28974 20 20 69 6e 74 20 6d 78 53 65 6c 65 63 74 3b 0a    int mxSelect;.
28975 20 20 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d        for(pLoop=
28976 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d  p; pLoop; pLoop=
28977 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 2c 20 63  pLoop->pPrior, c
28978 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70  nt++){.        p
28979 4c 6f 6f 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  Loop->pRightmost
2897a 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 70 4c   = p;.        pL
2897b 6f 6f 70 2d 3e 70 4e 65 78 74 20 3d 20 70 52 69  oop->pNext = pRi
2897c 67 68 74 3b 0a 20 20 20 20 20 20 20 20 70 52 69  ght;.        pRi
2897d 67 68 74 20 3d 20 70 4c 6f 6f 70 3b 0a 20 20 20  ght = pLoop;.   
2897e 20 20 20 7d 0a 20 20 20 20 20 20 6d 78 53 65 6c     }.      mxSel
2897f 65 63 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  ect = db->aLimit
28980 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
28981 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 5d 3b 0a  MPOUND_SELECT];.
28982 20 20 20 20 20 20 69 66 28 20 6d 78 53 65 6c 65        if( mxSele
28983 63 74 20 26 26 20 63 6e 74 3e 6d 78 53 65 6c 65  ct && cnt>mxSele
28984 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ct ){.        sq
28985 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
28986 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
28987 74 65 72 6d 73 20 69 6e 20 63 6f 6d 70 6f 75 6e  terms in compoun
28988 64 20 53 45 4c 45 43 54 22 29 3b 0a 20 20 20 20  d SELECT");.    
28989 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2898a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2898b 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65  return multiSele
2898c 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 70 44  ct(pParse, p, pD
2898d 65 73 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  est);.  }.#endif
2898e 0a 0a 20 20 2f 2a 20 49 66 20 77 72 69 74 69 6e  ..  /* If writin
2898f 67 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 72 20 67  g to memory or g
28990 65 6e 65 72 61 74 69 6e 67 20 61 20 73 65 74 0a  enerating a set.
28991 20 20 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67    ** only a sing
28992 6c 65 20 63 6f 6c 75 6d 6e 20 6d 61 79 20 62 65  le column may be
28993 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 23 69   output..  */.#i
28994 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28995 54 5f 53 55 42 51 55 45 52 59 0a 20 20 69 66 28  T_SUBQUERY.  if(
28996 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f   checkForMultiCo
28997 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28  lumnSelectError(
28998 70 50 61 72 73 65 2c 20 70 44 65 73 74 2c 20 70  pParse, pDest, p
28999 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b  EList->nExpr) ){
2899a 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74  .    goto select
2899b 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  _end;.  }.#endif
2899c 0a 0a 20 20 2f 2a 20 49 66 20 70 6f 73 73 69 62  ..  /* If possib
2899d 6c 65 2c 20 72 65 77 72 69 74 65 20 74 68 65 20  le, rewrite the 
2899e 71 75 65 72 79 20 74 6f 20 75 73 65 20 47 52 4f  query to use GRO
2899f 55 50 20 42 59 20 69 6e 73 74 65 61 64 20 6f 66  UP BY instead of
289a0 20 44 49 53 54 49 4e 43 54 2e 0a 20 20 2a 2a 20   DISTINCT..  ** 
289a1 47 52 4f 55 50 20 42 59 20 6d 69 67 68 74 20 75  GROUP BY might u
289a2 73 65 20 61 6e 20 69 6e 64 65 78 2c 20 44 49 53  se an index, DIS
289a3 54 49 4e 43 54 20 6e 65 76 65 72 20 64 6f 65 73  TINCT never does
289a4 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 2d  ..  */.  if( (p-
289a5 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f  >selFlags & (SF_
289a6 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72  Distinct|SF_Aggr
289a7 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74  egate))==SF_Dist
289a8 69 6e 63 74 20 26 26 20 21 70 2d 3e 70 47 72 6f  inct && !p->pGro
289a9 75 70 42 79 20 29 7b 0a 20 20 20 20 70 2d 3e 70  upBy ){.    p->p
289aa 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74 65  GroupBy = sqlite
289ab 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
289ac 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30 29 3b 0a   p->pEList, 0);.
289ad 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70      pGroupBy = p
289ae 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20  ->pGroupBy;.    
289af 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20 7e  p->selFlags &= ~
289b0 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 20  SF_Distinct;.   
289b1 20 69 73 44 69 73 74 69 6e 63 74 20 3d 20 30 3b   isDistinct = 0;
289b2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
289b3 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20  ere is an ORDER 
289b4 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20  BY clause, then 
289b5 74 68 69 73 20 73 6f 72 74 69 6e 67 0a 20 20 2a  this sorting.  *
289b6 2a 20 69 6e 64 65 78 20 6d 69 67 68 74 20 65 6e  * index might en
289b7 64 20 75 70 20 62 65 69 6e 67 20 75 6e 75 73 65  d up being unuse
289b8 64 20 69 66 20 74 68 65 20 64 61 74 61 20 63 61  d if the data ca
289b9 6e 20 62 65 20 0a 20 20 2a 2a 20 65 78 74 72 61  n be .  ** extra
289ba 63 74 65 64 20 69 6e 20 70 72 65 2d 73 6f 72 74  cted in pre-sort
289bb 65 64 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68  ed order.  If th
289bc 61 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  at is the case, 
289bd 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 50  then the.  ** OP
289be 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69  _OpenEphemeral i
289bf 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20  nstruction will 
289c0 62 65 20 63 68 61 6e 67 65 64 20 74 6f 20 61 6e  be changed to an
289c1 20 4f 50 5f 4e 6f 6f 70 20 6f 6e 63 65 0a 20 20   OP_Noop once.  
289c2 2a 2a 20 77 65 20 66 69 67 75 72 65 20 6f 75 74  ** we figure out
289c3 20 74 68 61 74 20 74 68 65 20 73 6f 72 74 69 6e   that the sortin
289c4 67 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 6e  g index is not n
289c5 65 65 64 65 64 2e 20 20 54 68 65 20 61 64 64 72  eeded.  The addr
289c6 53 6f 72 74 49 6e 64 65 78 0a 20 20 2a 2a 20 76  SortIndex.  ** v
289c7 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20  ariable is used 
289c8 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 68  to facilitate th
289c9 61 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a  at change..  */.
289ca 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
289cb 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70  {.    KeyInfo *p
289cc 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 70 4b 65  KeyInfo;.    pKe
289cd 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46  yInfo = keyInfoF
289ce 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72  romExprList(pPar
289cf 73 65 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20  se, pOrderBy);. 
289d0 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43     pOrderBy->iEC
289d1 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e  ursor = pParse->
289d2 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 70 2d 3e 61  nTab++;.    p->a
289d3 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d  ddrOpenEphm[2] =
289d4 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20 3d   addrSortIndex =
289d5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
289d6 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
289d7 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 0a 20 20  penEphemeral,.  
289d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
289d9 20 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42           pOrderB
289da 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 70 4f 72  y->iECursor, pOr
289db 64 65 72 42 79 2d 3e 6e 45 78 70 72 2b 32 2c 20  derBy->nExpr+2, 
289dc 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
289dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
289de 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50  har*)pKeyInfo, P
289df 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
289e0 46 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  F);.  }else{.   
289e1 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20 3d   addrSortIndex =
289e2 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49   -1;.  }..  /* I
289e3 66 20 74 68 65 20 6f 75 74 70 75 74 20 69 73 20  f the output is 
289e4 64 65 73 74 69 6e 65 64 20 66 6f 72 20 61 20 74  destined for a t
289e5 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20  emporary table, 
289e6 6f 70 65 6e 20 74 68 61 74 20 74 61 62 6c 65 2e  open that table.
289e7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65 73  .  */.  if( pDes
289e8 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70  t->eDest==SRT_Ep
289e9 68 65 6d 54 61 62 20 29 7b 0a 20 20 20 20 73 71  hemTab ){.    sq
289ea 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
289eb 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
289ec 72 61 6c 2c 20 70 44 65 73 74 2d 3e 69 50 61 72  ral, pDest->iPar
289ed 6d 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  m, pEList->nExpr
289ee 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74  );.  }..  /* Set
289ef 20 74 68 65 20 6c 69 6d 69 74 65 72 2e 0a 20 20   the limiter..  
289f0 2a 2f 0a 20 20 69 45 6e 64 20 3d 20 73 71 6c 69  */.  iEnd = sqli
289f1 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
289f2 28 76 29 3b 0a 20 20 63 6f 6d 70 75 74 65 4c 69  (v);.  computeLi
289f3 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61  mitRegisters(pPa
289f4 72 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b 0a 0a  rse, p, iEnd);..
289f5 20 20 2f 2a 20 4f 70 65 6e 20 61 20 76 69 72 74    /* Open a virt
289f6 75 61 6c 20 69 6e 64 65 78 20 74 6f 20 75 73 65  ual index to use
289f7 20 66 6f 72 20 74 68 65 20 64 69 73 74 69 6e 63   for the distinc
289f8 74 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  t set..  */.  if
289f9 28 20 69 73 44 69 73 74 69 6e 63 74 20 29 7b 0a  ( isDistinct ){.
289fa 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
289fb 79 49 6e 66 6f 3b 0a 20 20 20 20 61 73 73 65 72  yInfo;.    asser
289fc 74 28 20 69 73 41 67 67 20 7c 7c 20 70 47 72 6f  t( isAgg || pGro
289fd 75 70 42 79 20 29 3b 0a 20 20 20 20 64 69 73 74  upBy );.    dist
289fe 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e  inct = pParse->n
289ff 54 61 62 2b 2b 3b 0a 20 20 20 20 70 4b 65 79 49  Tab++;.    pKeyI
28a00 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f  nfo = keyInfoFro
28a01 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65  mExprList(pParse
28a02 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  , p->pEList);.  
28a03 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28a04 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70  Op4(v, OP_OpenEp
28a05 68 65 6d 65 72 61 6c 2c 20 64 69 73 74 69 6e 63  hemeral, distinc
28a06 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20  t, 0, 0,.       
28a07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28a08 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
28a09 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
28a0a 44 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  DOFF);.  }else{.
28a0b 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d 20 2d      distinct = -
28a0c 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 67 67  1;.  }..  /* Agg
28a0d 72 65 67 61 74 65 20 61 6e 64 20 6e 6f 6e 2d 61  regate and non-a
28a0e 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
28a0f 20 61 72 65 20 68 61 6e 64 6c 65 64 20 64 69 66   are handled dif
28a10 66 65 72 65 6e 74 6c 79 20 2a 2f 0a 20 20 69 66  ferently */.  if
28a11 28 20 21 69 73 41 67 67 20 26 26 20 70 47 72 6f  ( !isAgg && pGro
28a12 75 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  upBy==0 ){.    /
28a13 2a 20 54 68 69 73 20 63 61 73 65 20 69 73 20 66  * This case is f
28a14 6f 72 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65  or non-aggregate
28a15 20 71 75 65 72 69 65 73 0a 20 20 20 20 2a 2a 20   queries.    ** 
28a16 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62 61  Begin the databa
28a17 73 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f 0a 20  se scan.    */. 
28a18 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69     pWInfo = sqli
28a19 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
28a1a 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
28a1b 70 57 68 65 72 65 2c 20 26 70 4f 72 64 65 72 42  pWhere, &pOrderB
28a1c 79 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  y, 0, 0);.    if
28a1d 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f  ( pWInfo==0 ) go
28a1e 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a  to select_end;..
28a1f 20 20 20 20 2f 2a 20 49 66 20 73 6f 72 74 69 6e      /* If sortin
28a20 67 20 69 6e 64 65 78 20 74 68 61 74 20 77 61 73  g index that was
28a21 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72   created by a pr
28a22 69 6f 72 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  ior OP_OpenEphem
28a23 65 72 61 6c 20 0a 20 20 20 20 2a 2a 20 69 6e 73  eral .    ** ins
28a24 74 72 75 63 74 69 6f 6e 20 65 6e 64 65 64 20 75  truction ended u
28a25 70 20 6e 6f 74 20 62 65 69 6e 67 20 6e 65 65 64  p not being need
28a26 65 64 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20  ed, then change 
28a27 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  the OP_OpenEphem
28a28 65 72 61 6c 0a 20 20 20 20 2a 2a 20 69 6e 74 6f  eral.    ** into
28a29 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a 20 20 20   an OP_Noop..   
28a2a 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 64 64 72   */.    if( addr
28a2b 53 6f 72 74 49 6e 64 65 78 3e 3d 30 20 26 26 20  SortIndex>=0 && 
28a2c 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20  pOrderBy==0 ){. 
28a2d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28a2e 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20  ChangeToNoop(v, 
28a2f 61 64 64 72 53 6f 72 74 49 6e 64 65 78 2c 20 31  addrSortIndex, 1
28a30 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72  );.      p->addr
28a31 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31  OpenEphm[2] = -1
28a32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
28a33 55 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64  Use the standard
28a34 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 20 20 20 20   inner loop.    
28a35 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 21 69  */.    assert(!i
28a36 73 44 69 73 74 69 6e 63 74 29 3b 0a 20 20 20 20  sDistinct);.    
28a37 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28  selectInnerLoop(
28a38 70 50 61 72 73 65 2c 20 70 2c 20 70 45 4c 69 73  pParse, p, pELis
28a39 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65 72 42  t, 0, 0, pOrderB
28a3a 79 2c 20 2d 31 2c 20 70 44 65 73 74 2c 0a 20 20  y, -1, pDest,.  
28a3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28a3c 20 20 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69    pWInfo->iConti
28a3d 6e 75 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72  nue, pWInfo->iBr
28a3e 65 61 6b 29 3b 0a 0a 20 20 20 20 2f 2a 20 45 6e  eak);..    /* En
28a3f 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
28a40 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f  can loop..    */
28a41 0a 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72  .    sqlite3Wher
28a42 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20  eEnd(pWInfo);.  
28a43 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
28a44 69 73 20 69 73 20 74 68 65 20 70 72 6f 63 65 73  is is the proces
28a45 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61  sing for aggrega
28a46 74 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20  te queries */.  
28a47 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
28a48 43 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f  C;    /* Name co
28a49 6e 74 65 78 74 20 66 6f 72 20 70 72 6f 63 65 73  ntext for proces
28a4a 73 69 6e 67 20 61 67 67 72 65 67 61 74 65 20 69  sing aggregate i
28a4b 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20  nformation */.  
28a4c 20 20 69 6e 74 20 69 41 4d 65 6d 3b 20 20 20 20    int iAMem;    
28a4d 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d        /* First M
28a4e 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72 20 73  em address for s
28a4f 74 6f 72 69 6e 67 20 63 75 72 72 65 6e 74 20 47  toring current G
28a50 52 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20 20 69  ROUP BY */.    i
28a51 6e 74 20 69 42 4d 65 6d 3b 20 20 20 20 20 20 20  nt iBMem;       
28a52 20 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d 20     /* First Mem 
28a53 61 64 64 72 65 73 73 20 66 6f 72 20 70 72 65 76  address for prev
28a54 69 6f 75 73 20 47 52 4f 55 50 20 42 59 20 2a 2f  ious GROUP BY */
28a55 0a 20 20 20 20 69 6e 74 20 69 55 73 65 46 6c 61  .    int iUseFla
28a56 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 20  g;       /* Mem 
28a57 61 64 64 72 65 73 73 20 68 6f 6c 64 69 6e 67 20  address holding 
28a58 66 6c 61 67 20 69 6e 64 69 63 61 74 69 6e 67 20  flag indicating 
28a59 74 68 61 74 20 61 74 20 6c 65 61 73 74 0a 20 20  that at least.  
28a5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28a5b 20 20 20 20 20 20 2a 2a 20 6f 6e 65 20 72 6f 77        ** one row
28a5c 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74 6f   of the input to
28a5d 20 74 68 65 20 61 67 67 72 65 67 61 74 6f 72 20   the aggregator 
28a5e 68 61 73 20 62 65 65 6e 0a 20 20 20 20 20 20 20  has been.       
28a5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28a60 20 2a 2a 20 70 72 6f 63 65 73 73 65 64 20 2a 2f   ** processed */
28a61 0a 20 20 20 20 69 6e 74 20 69 41 62 6f 72 74 46  .    int iAbortF
28a62 6c 61 67 3b 20 20 20 20 20 2f 2a 20 4d 65 6d 20  lag;     /* Mem 
28a63 61 64 64 72 65 73 73 20 77 68 69 63 68 20 63 61  address which ca
28a64 75 73 65 73 20 71 75 65 72 79 20 61 62 6f 72 74  uses query abort
28a65 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a   if positive */.
28a66 20 20 20 20 69 6e 74 20 67 72 6f 75 70 42 79 53      int groupByS
28a67 6f 72 74 3b 20 20 20 20 2f 2a 20 52 6f 77 73 20  ort;    /* Rows 
28a68 63 6f 6d 65 20 66 72 6f 6d 20 73 6f 75 72 63 65  come from source
28a69 20 69 6e 20 47 52 4f 55 50 20 42 59 20 6f 72 64   in GROUP BY ord
28a6a 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 64  er */.    int ad
28a6b 64 72 45 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a  drEnd;        /*
28a6c 20 45 6e 64 20 6f 66 20 70 72 6f 63 65 73 73 69   End of processi
28a6d 6e 67 20 66 6f 72 20 74 68 69 73 20 53 45 4c 45  ng for this SELE
28a6e 43 54 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 65  CT */..    /* Re
28a6f 6d 6f 76 65 20 61 6e 79 20 61 6e 64 20 61 6c 6c  move any and all
28a70 20 61 6c 69 61 73 65 73 20 62 65 74 77 65 65 6e   aliases between
28a71 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
28a72 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 47  and the.    ** G
28a73 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a  ROUP BY clause..
28a74 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
28a75 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20  GroupBy ){.     
28a76 20 69 6e 74 20 6b 3b 20 20 20 20 20 20 20 20 20   int k;         
28a77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28a78 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
28a79 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  /.      struct E
28a7a 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
28a7b 74 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f  tem;  /* For loo
28a7c 70 69 6e 67 20 6f 76 65 72 20 65 78 70 72 65 73  ping over expres
28a7d 73 69 6f 6e 20 69 6e 20 61 20 6c 69 73 74 20 2a  sion in a list *
28a7e 2f 0a 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70  /..      for(k=p
28a7f 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c  ->pEList->nExpr,
28a80 20 70 49 74 65 6d 3d 70 2d 3e 70 45 4c 69 73 74   pItem=p->pEList
28a81 2d 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70  ->a; k>0; k--, p
28a82 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
28a83 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d   pItem->iAlias =
28a84 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
28a85 20 20 66 6f 72 28 6b 3d 70 47 72 6f 75 70 42 79    for(k=pGroupBy
28a86 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70  ->nExpr, pItem=p
28a87 47 72 6f 75 70 42 79 2d 3e 61 3b 20 6b 3e 30 3b  GroupBy->a; k>0;
28a88 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   k--, pItem++){.
28a89 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69          pItem->i
28a8a 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 20  Alias = 0;.     
28a8b 20 7d 0a 20 20 20 20 7d 0a 0a 20 0a 20 20 20 20   }.    }.. .    
28a8c 2f 2a 20 43 72 65 61 74 65 20 61 20 6c 61 62 65  /* Create a labe
28a8d 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 77 68 65  l to jump to whe
28a8e 6e 20 77 65 20 77 61 6e 74 20 74 6f 20 61 62 6f  n we want to abo
28a8f 72 74 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a  rt the query */.
28a90 20 20 20 20 61 64 64 72 45 6e 64 20 3d 20 73 71      addrEnd = sq
28a91 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
28a92 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  el(v);..    /* C
28a93 6f 6e 76 65 72 74 20 54 4b 5f 43 4f 4c 55 4d 4e  onvert TK_COLUMN
28a94 20 6e 6f 64 65 73 20 69 6e 74 6f 20 54 4b 5f 41   nodes into TK_A
28a95 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 6d 61  GG_COLUMN and ma
28a96 6b 65 20 65 6e 74 72 69 65 73 20 69 6e 0a 20 20  ke entries in.  
28a97 20 20 2a 2a 20 73 41 67 67 49 6e 66 6f 20 66 6f    ** sAggInfo fo
28a98 72 20 61 6c 6c 20 54 4b 5f 41 47 47 5f 46 55 4e  r all TK_AGG_FUN
28a99 43 54 49 4f 4e 20 6e 6f 64 65 73 20 69 6e 20 65  CTION nodes in e
28a9a 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68  xpressions of th
28a9b 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20  e.    ** SELECT 
28a9c 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a  statement..    *
28a9d 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e  /.    memset(&sN
28a9e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
28a9f 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72  ));.    sNC.pPar
28aa0 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20  se = pParse;.   
28aa1 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20   sNC.pSrcList = 
28aa2 70 54 61 62 4c 69 73 74 3b 0a 20 20 20 20 73 4e  pTabList;.    sN
28aa3 43 2e 70 41 67 67 49 6e 66 6f 20 3d 20 26 73 41  C.pAggInfo = &sA
28aa4 67 67 49 6e 66 6f 3b 0a 20 20 20 20 73 41 67 67  ggInfo;.    sAgg
28aa5 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c  Info.nSortingCol
28aa6 75 6d 6e 20 3d 20 70 47 72 6f 75 70 42 79 20 3f  umn = pGroupBy ?
28aa7 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
28aa8 2b 31 20 3a 20 30 3b 0a 20 20 20 20 73 41 67 67  +1 : 0;.    sAgg
28aa9 49 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20 3d 20  Info.pGroupBy = 
28aaa 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 73 71  pGroupBy;.    sq
28aab 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
28aac 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 45  AggList(&sNC, pE
28aad 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  List);.    sqlit
28aae 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
28aaf 4c 69 73 74 28 26 73 4e 43 2c 20 70 4f 72 64 65  List(&sNC, pOrde
28ab0 72 42 79 29 3b 0a 20 20 20 20 69 66 28 20 70 48  rBy);.    if( pH
28ab1 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 73  aving ){.      s
28ab2 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
28ab3 65 41 67 67 72 65 67 61 74 65 73 28 26 73 4e 43  eAggregates(&sNC
28ab4 2c 20 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20  , pHaving);.    
28ab5 7d 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e  }.    sAggInfo.n
28ab6 41 63 63 75 6d 75 6c 61 74 6f 72 20 3d 20 73 41  Accumulator = sA
28ab7 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 0a  ggInfo.nColumn;.
28ab8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
28ab9 41 67 67 49 6e 66 6f 2e 6e 46 75 6e 63 3b 20 69  AggInfo.nFunc; i
28aba 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
28abb 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
28abc 72 74 79 28 73 41 67 67 49 6e 66 6f 2e 61 46 75  rty(sAggInfo.aFu
28abd 6e 63 5b 69 5d 2e 70 45 78 70 72 2c 20 45 50 5f  nc[i].pExpr, EP_
28abe 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20  xIsSelect) );.  
28abf 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41      sqlite3ExprA
28ac0 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73  nalyzeAggList(&s
28ac1 4e 43 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46 75  NC, sAggInfo.aFu
28ac2 6e 63 5b 69 5d 2e 70 45 78 70 72 2d 3e 78 2e 70  nc[i].pExpr->x.p
28ac3 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20  List);.    }.   
28ac4 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
28ac5 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 73 65 6c  ailed ) goto sel
28ac6 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a  ect_end;..    /*
28ac7 20 50 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   Processing for 
28ac8 61 67 67 72 65 67 61 74 65 73 20 77 69 74 68 20  aggregates with 
28ac9 47 52 4f 55 50 20 42 59 20 69 73 20 76 65 72 79  GROUP BY is very
28aca 20 64 69 66 66 65 72 65 6e 74 20 61 6e 64 0a 20   different and. 
28acb 20 20 20 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20     ** much more 
28acc 63 6f 6d 70 6c 65 78 20 74 68 61 6e 20 61 67 67  complex than agg
28acd 72 65 67 61 74 65 73 20 77 69 74 68 6f 75 74 20  regates without 
28ace 61 20 47 52 4f 55 50 20 42 59 2e 0a 20 20 20 20  a GROUP BY..    
28acf 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75  */.    if( pGrou
28ad0 70 42 79 20 29 7b 0a 20 20 20 20 20 20 4b 65 79  pBy ){.      Key
28ad1 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
28ad2 20 2f 2a 20 4b 65 79 69 6e 67 20 69 6e 66 6f 72   /* Keying infor
28ad3 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 67  mation for the g
28ad4 72 6f 75 70 20 62 79 20 63 6c 61 75 73 65 20 2a  roup by clause *
28ad5 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 20  /.      int j1; 
28ad6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
28ad7 2d 76 73 2d 42 20 63 6f 6d 70 61 72 69 73 69 6f  -vs-B comparisio
28ad8 6e 20 6a 75 6d 70 20 2a 2f 0a 20 20 20 20 20 20  n jump */.      
28ad9 69 6e 74 20 61 64 64 72 4f 75 74 70 75 74 52 6f  int addrOutputRo
28ada 77 3b 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20  w;  /* Start of 
28adb 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
28adc 6f 75 74 70 75 74 73 20 61 20 72 65 73 75 6c 74  outputs a result
28add 20 72 6f 77 20 2a 2f 0a 20 20 20 20 20 20 69 6e   row */.      in
28ade 74 20 72 65 67 4f 75 74 70 75 74 52 6f 77 3b 20  t regOutputRow; 
28adf 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 72    /* Return addr
28ae0 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72  ess register for
28ae1 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69   output subrouti
28ae2 6e 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ne */.      int 
28ae3 61 64 64 72 53 65 74 41 62 6f 72 74 3b 20 20 20  addrSetAbort;   
28ae4 2f 2a 20 53 65 74 20 74 68 65 20 61 62 6f 72 74  /* Set the abort
28ae5 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e   flag and return
28ae6 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
28ae7 64 72 54 6f 70 4f 66 4c 6f 6f 70 3b 20 20 2f 2a  drTopOfLoop;  /*
28ae8 20 54 6f 70 20 6f 66 20 74 68 65 20 69 6e 70 75   Top of the inpu
28ae9 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20  t loop */.      
28aea 69 6e 74 20 61 64 64 72 53 6f 72 74 69 6e 67 49  int addrSortingI
28aeb 64 78 3b 20 2f 2a 20 54 68 65 20 4f 50 5f 4f 70  dx; /* The OP_Op
28aec 65 6e 45 70 68 65 6d 65 72 61 6c 20 66 6f 72 20  enEphemeral for 
28aed 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  the sorting inde
28aee 78 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  x */.      int a
28aef 64 64 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f  ddrReset;      /
28af0 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 66 6f 72  * Subroutine for
28af1 20 72 65 73 65 74 74 69 6e 67 20 74 68 65 20 61   resetting the a
28af2 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20  ccumulator */.  
28af3 20 20 20 20 69 6e 74 20 72 65 67 52 65 73 65 74      int regReset
28af4 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72  ;       /* Retur
28af5 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73 74  n address regist
28af6 65 72 20 66 6f 72 20 72 65 73 65 74 20 73 75 62  er for reset sub
28af7 72 6f 75 74 69 6e 65 20 2a 2f 0a 0a 20 20 20 20  routine */..    
28af8 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
28af9 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75   a GROUP BY clau
28afa 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65 64  se we might need
28afb 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78   a sorting index
28afc 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 69 6d 70   to.      ** imp
28afd 6c 65 6d 65 6e 74 20 69 74 2e 20 20 41 6c 6c 6f  lement it.  Allo
28afe 63 61 74 65 20 74 68 61 74 20 73 6f 72 74 69 6e  cate that sortin
28aff 67 20 69 6e 64 65 78 20 6e 6f 77 2e 20 20 49 66  g index now.  If
28b00 20 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 20 20   it turns out.  
28b01 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64      ** that we d
28b02 6f 20 6e 6f 74 20 6e 65 65 64 20 69 74 20 61 66  o not need it af
28b03 74 65 72 20 61 6c 6c 2c 20 74 68 65 20 4f 70 65  ter all, the Ope
28b04 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
28b05 75 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20  uction.      ** 
28b06 77 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74 65  will be converte
28b07 64 20 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e 20 20  d into a Noop.  
28b08 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
28b09 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
28b0a 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Idx = pParse->nT
28b0b 61 62 2b 2b 3b 0a 20 20 20 20 20 20 70 4b 65 79  ab++;.      pKey
28b0c 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72  Info = keyInfoFr
28b0d 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
28b0e 65 2c 20 70 47 72 6f 75 70 42 79 29 3b 0a 20 20  e, pGroupBy);.  
28b0f 20 20 20 20 61 64 64 72 53 6f 72 74 69 6e 67 49      addrSortingI
28b10 64 78 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dx = sqlite3Vdbe
28b11 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65  AddOp4(v, OP_Ope
28b12 6e 45 70 68 65 6d 65 72 61 6c 2c 20 0a 20 20 20  nEphemeral, .   
28b13 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e         sAggInfo.
28b14 73 6f 72 74 69 6e 67 49 64 78 2c 20 73 41 67 67  sortingIdx, sAgg
28b15 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c  Info.nSortingCol
28b16 75 6d 6e 2c 20 0a 20 20 20 20 20 20 20 20 20 20  umn, .          
28b17 30 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e  0, (char*)pKeyIn
28b18 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  fo, P4_KEYINFO_H
28b19 41 4e 44 4f 46 46 29 3b 0a 0a 20 20 20 20 20 20  ANDOFF);..      
28b1a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65  /* Initialize me
28b1b 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 75  mory locations u
28b1c 73 65 64 20 62 79 20 47 52 4f 55 50 20 42 59 20  sed by GROUP BY 
28b1d 61 67 67 72 65 67 61 74 65 20 70 72 6f 63 65 73  aggregate proces
28b1e 73 69 6e 67 0a 20 20 20 20 20 20 2a 2f 0a 20 20  sing.      */.  
28b1f 20 20 20 20 69 55 73 65 46 6c 61 67 20 3d 20 2b      iUseFlag = +
28b20 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
28b21 20 20 20 20 20 69 41 62 6f 72 74 46 6c 61 67 20       iAbortFlag 
28b22 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
28b23 3b 0a 20 20 20 20 20 20 72 65 67 4f 75 74 70 75  ;.      regOutpu
28b24 74 52 6f 77 20 3d 20 2b 2b 70 50 61 72 73 65 2d  tRow = ++pParse-
28b25 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64  >nMem;.      add
28b26 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71 6c  rOutputRow = sql
28b27 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
28b28 6c 28 76 29 3b 0a 20 20 20 20 20 20 72 65 67 52  l(v);.      regR
28b29 65 73 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  eset = ++pParse-
28b2a 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64  >nMem;.      add
28b2b 72 52 65 73 65 74 20 3d 20 73 71 6c 69 74 65 33  rReset = sqlite3
28b2c 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
28b2d 3b 0a 20 20 20 20 20 20 69 41 4d 65 6d 20 3d 20  ;.      iAMem = 
28b2e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31  pParse->nMem + 1
28b2f 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
28b30 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79  nMem += pGroupBy
28b31 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 69  ->nExpr;.      i
28b32 42 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e  BMem = pParse->n
28b33 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70  Mem + 1;.      p
28b34 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
28b35 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a  GroupBy->nExpr;.
28b36 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28b37 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
28b38 74 65 67 65 72 2c 20 30 2c 20 69 41 62 6f 72 74  teger, 0, iAbort
28b39 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62  Flag);.      Vdb
28b3a 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 6c  eComment((v, "cl
28b3b 65 61 72 20 61 62 6f 72 74 20 66 6c 61 67 22 29  ear abort flag")
28b3c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
28b3d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28b3e 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 55 73  _Integer, 0, iUs
28b3f 65 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64  eFlag);.      Vd
28b40 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69  beComment((v, "i
28b41 6e 64 69 63 61 74 65 20 61 63 63 75 6d 75 6c 61  ndicate accumula
28b42 74 6f 72 20 65 6d 70 74 79 22 29 29 3b 0a 0a 20  tor empty"));.. 
28b43 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 61 20       /* Begin a 
28b44 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 65  loop that will e
28b45 78 74 72 61 63 74 20 61 6c 6c 20 73 6f 75 72 63  xtract all sourc
28b46 65 20 72 6f 77 73 20 69 6e 20 47 52 4f 55 50 20  e rows in GROUP 
28b47 42 59 20 6f 72 64 65 72 2e 0a 20 20 20 20 20 20  BY order..      
28b48 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 69 6e  ** This might in
28b49 76 6f 6c 76 65 20 74 77 6f 20 73 65 70 61 72 61  volve two separa
28b4a 74 65 20 6c 6f 6f 70 73 20 77 69 74 68 20 61 6e  te loops with an
28b4b 20 4f 50 5f 53 6f 72 74 20 69 6e 20 62 65 74 77   OP_Sort in betw
28b4c 65 65 6e 2c 20 6f 72 0a 20 20 20 20 20 20 2a 2a  een, or.      **
28b4d 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 73   it might be a s
28b4e 69 6e 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74 20  ingle loop that 
28b4f 75 73 65 73 20 61 6e 20 69 6e 64 65 78 20 74 6f  uses an index to
28b50 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61   extract informa
28b51 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e  tion.      ** in
28b52 20 74 68 65 20 72 69 67 68 74 20 6f 72 64 65 72   the right order
28b53 20 74 6f 20 62 65 67 69 6e 20 77 69 74 68 2e 0a   to begin with..
28b54 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
28b55 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28b56 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
28b57 67 52 65 73 65 74 2c 20 61 64 64 72 52 65 73 65  gReset, addrRese
28b58 74 29 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f  t);.      pWInfo
28b59 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42   = sqlite3WhereB
28b5a 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61  egin(pParse, pTa
28b5b 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26  bList, pWhere, &
28b5c 70 47 72 6f 75 70 42 79 2c 20 30 2c 20 30 29 3b  pGroupBy, 0, 0);
28b5d 0a 20 20 20 20 20 20 69 66 28 20 70 57 49 6e 66  .      if( pWInf
28b5e 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65  o==0 ) goto sele
28b5f 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 69 66  ct_end;.      if
28b60 28 20 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b  ( pGroupBy==0 ){
28b61 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
28b62 6f 70 74 69 6d 69 7a 65 72 20 69 73 20 61 62 6c  optimizer is abl
28b63 65 20 74 6f 20 64 65 6c 69 76 65 72 20 72 6f 77  e to deliver row
28b64 73 20 69 6e 20 67 72 6f 75 70 20 62 79 20 6f 72  s in group by or
28b65 64 65 72 20 73 6f 0a 20 20 20 20 20 20 20 20 2a  der so.        *
28b66 2a 20 77 65 20 64 6f 20 6e 6f 74 20 68 61 76 65  * we do not have
28b67 20 74 6f 20 73 6f 72 74 2e 20 20 54 68 65 20 4f   to sort.  The O
28b68 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
28b69 74 61 62 6c 65 20 77 69 6c 6c 20 62 65 0a 20 20  table will be.  
28b6a 20 20 20 20 20 20 2a 2a 20 63 61 6e 63 65 6c 6c        ** cancell
28b6b 65 64 20 6c 61 74 65 72 20 62 65 63 61 75 73 65  ed later because
28b6c 20 77 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74   we still need t
28b6d 6f 20 75 73 65 20 74 68 65 20 70 4b 65 79 49 6e  o use the pKeyIn
28b6e 66 6f 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  fo.        */.  
28b6f 20 20 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d        pGroupBy =
28b70 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20   p->pGroupBy;.  
28b71 20 20 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72        groupBySor
28b72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  t = 0;.      }el
28b73 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52  se{.        /* R
28b74 6f 77 73 20 61 72 65 20 63 6f 6d 69 6e 67 20 6f  ows are coming o
28b75 75 74 20 69 6e 20 75 6e 64 65 74 65 72 6d 69 6e  ut in undetermin
28b76 65 64 20 6f 72 64 65 72 2e 20 20 57 65 20 68 61  ed order.  We ha
28b77 76 65 20 74 6f 20 70 75 73 68 0a 20 20 20 20 20  ve to push.     
28b78 20 20 20 2a 2a 20 65 61 63 68 20 72 6f 77 20 69     ** each row i
28b79 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e  nto a sorting in
28b7a 64 65 78 2c 20 74 65 72 6d 69 6e 61 74 65 20 74  dex, terminate t
28b7b 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 2c 0a 20  he first loop,. 
28b7c 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 6c         ** then l
28b7d 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 73 6f 72  oop over the sor
28b7e 74 69 6e 67 20 69 6e 64 65 78 20 69 6e 20 6f 72  ting index in or
28b7f 64 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 6f  der to get the o
28b80 75 74 70 75 74 0a 20 20 20 20 20 20 20 20 2a 2a  utput.        **
28b81 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
28b82 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
28b83 20 20 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b      int regBase;
28b84 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 67  .        int reg
28b85 52 65 63 6f 72 64 3b 0a 20 20 20 20 20 20 20 20  Record;.        
28b86 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 20 20 20 20  int nCol;.      
28b87 20 20 69 6e 74 20 6e 47 72 6f 75 70 42 79 3b 0a    int nGroupBy;.
28b88 0a 20 20 20 20 20 20 20 20 67 72 6f 75 70 42 79  .        groupBy
28b89 53 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  Sort = 1;.      
28b8a 20 20 6e 47 72 6f 75 70 42 79 20 3d 20 70 47 72    nGroupBy = pGr
28b8b 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20  oupBy->nExpr;.  
28b8c 20 20 20 20 20 20 6e 43 6f 6c 20 3d 20 6e 47 72        nCol = nGr
28b8d 6f 75 70 42 79 20 2b 20 31 3b 0a 20 20 20 20 20  oupBy + 1;.     
28b8e 20 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b     j = nGroupBy+
28b8f 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  1;.        for(i
28b90 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e  =0; i<sAggInfo.n
28b91 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
28b92 20 20 20 20 20 20 20 20 69 66 28 20 73 41 67 67          if( sAgg
28b93 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 2e 69 53 6f  Info.aCol[i].iSo
28b94 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b  rterColumn>=j ){
28b95 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f  .            nCo
28b96 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  l++;.           
28b97 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20   j++;.          
28b98 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
28b99 20 20 20 20 72 65 67 42 61 73 65 20 3d 20 73 71      regBase = sq
28b9a 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
28b9b 65 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 29 3b  e(pParse, nCol);
28b9c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28b9d 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74  ExprCodeExprList
28b9e 28 70 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42  (pParse, pGroupB
28b9f 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0a  y, regBase, 0);.
28ba0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28ba1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28ba2 53 65 71 75 65 6e 63 65 2c 20 73 41 67 67 49 6e  Sequence, sAggIn
28ba3 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 72 65  fo.sortingIdx,re
28ba4 67 42 61 73 65 2b 6e 47 72 6f 75 70 42 79 29 3b  gBase+nGroupBy);
28ba5 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 6e 47 72  .        j = nGr
28ba6 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20 20 20 20  oupBy+1;.       
28ba7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67   for(i=0; i<sAgg
28ba8 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Info.nColumn; i+
28ba9 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74  +){.          st
28baa 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c  ruct AggInfo_col
28bab 20 2a 70 43 6f 6c 20 3d 20 26 73 41 67 67 49 6e   *pCol = &sAggIn
28bac 66 6f 2e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20  fo.aCol[i];.    
28bad 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e        if( pCol->
28bae 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a  iSorterColumn>=j
28baf 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
28bb0 69 6e 74 20 72 31 20 3d 20 6a 20 2b 20 72 65 67  int r1 = j + reg
28bb1 42 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Base;.          
28bb2 20 20 69 6e 74 20 72 32 3b 0a 0a 20 20 20 20 20    int r2;..     
28bb3 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69         r2 = sqli
28bb4 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
28bb5 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 0a 20 20  lumn(pParse, .  
28bb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28bb7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
28bb8 6c 2d 3e 70 54 61 62 2c 20 70 43 6f 6c 2d 3e 69  l->pTab, pCol->i
28bb9 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 2d 3e 69 54  Column, pCol->iT
28bba 61 62 6c 65 2c 20 72 31 2c 20 30 29 3b 0a 20 20  able, r1, 0);.  
28bbb 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 31            if( r1
28bbc 21 3d 72 32 20 29 7b 0a 20 20 20 20 20 20 20 20  !=r2 ){.        
28bbd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28bbe 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
28bbf 6f 70 79 2c 20 72 32 2c 20 72 31 29 3b 0a 20 20  opy, r2, r1);.  
28bc0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
28bc1 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20          j++;.   
28bc2 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
28bc3 20 7d 0a 20 20 20 20 20 20 20 20 72 65 67 52 65   }.        regRe
28bc4 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33 47 65  cord = sqlite3Ge
28bc5 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
28bc6 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
28bc7 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
28bc8 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
28bc9 67 42 61 73 65 2c 20 6e 43 6f 6c 2c 20 72 65 67  gBase, nCol, reg
28bca 52 65 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20  Record);.       
28bcb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28bcc 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p2(v, OP_IdxInse
28bcd 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72  rt, sAggInfo.sor
28bce 74 69 6e 67 49 64 78 2c 20 72 65 67 52 65 63 6f  tingIdx, regReco
28bcf 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  rd);.        sql
28bd0 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
28bd1 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 65  eg(pParse, regRe
28bd2 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73  cord);.        s
28bd3 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
28bd4 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
28bd5 65 67 42 61 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20  egBase, nCol);. 
28bd6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
28bd7 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
28bd8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28bd9 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28bda 53 6f 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73  Sort, sAggInfo.s
28bdb 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72 45  ortingIdx, addrE
28bdc 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62  nd);.        Vdb
28bdd 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 52  eComment((v, "GR
28bde 4f 55 50 20 42 59 20 73 6f 72 74 22 29 29 3b 0a  OUP BY sort"));.
28bdf 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f          sAggInfo
28be0 2e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 3d  .useSortingIdx =
28be1 20 31 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   1;.      }..   
28be2 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74     /* Evaluate t
28be3 68 65 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50  he current GROUP
28be4 20 42 59 20 74 65 72 6d 73 20 61 6e 64 20 73 74   BY terms and st
28be5 6f 72 65 20 69 6e 20 62 30 2c 20 62 31 2c 20 62  ore in b0, b1, b
28be6 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 28 62  2....      ** (b
28be7 30 20 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61  0 is memory loca
28be8 74 69 6f 6e 20 69 42 4d 65 6d 2b 30 2c 20 62 31  tion iBMem+0, b1
28be9 20 69 73 20 69 42 4d 65 6d 2b 31 2c 20 61 6e 64   is iBMem+1, and
28bea 20 73 6f 20 66 6f 72 74 68 29 0a 20 20 20 20 20   so forth).     
28beb 20 2a 2a 20 54 68 65 6e 20 63 6f 6d 70 61 72 65   ** Then compare
28bec 20 74 68 65 20 63 75 72 72 65 6e 74 20 47 52 4f   the current GRO
28bed 55 50 20 42 59 20 74 65 72 6d 73 20 61 67 61 69  UP BY terms agai
28bee 6e 73 74 20 74 68 65 20 47 52 4f 55 50 20 42 59  nst the GROUP BY
28bef 20 74 65 72 6d 73 0a 20 20 20 20 20 20 2a 2a 20   terms.      ** 
28bf0 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75  from the previou
28bf1 73 20 72 6f 77 20 63 75 72 72 65 6e 74 6c 79 20  s row currently 
28bf2 73 74 6f 72 65 64 20 69 6e 20 61 30 2c 20 61 31  stored in a0, a1
28bf3 2c 20 61 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f  , a2....      */
28bf4 0a 20 20 20 20 20 20 61 64 64 72 54 6f 70 4f 66  .      addrTopOf
28bf5 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  Loop = sqlite3Vd
28bf6 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
28bf7 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
28bf8 20 6a 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78   j<pGroupBy->nEx
28bf9 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; j++){.      
28bfa 20 20 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72    if( groupBySor
28bfb 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  t ){.          s
28bfc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
28bfd 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73  (v, OP_Column, s
28bfe 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49  AggInfo.sortingI
28bff 64 78 2c 20 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b  dx, j, iBMem+j);
28c00 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
28c01 20 20 20 20 20 20 20 20 20 20 73 41 67 67 49 6e            sAggIn
28c02 66 6f 2e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20  fo.directMode = 
28c03 31 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  1;.          sql
28c04 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
28c05 72 73 65 2c 20 70 47 72 6f 75 70 42 79 2d 3e 61  rse, pGroupBy->a
28c06 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 42 4d 65 6d  [j].pExpr, iBMem
28c07 2b 6a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  +j);.        }. 
28c08 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
28c09 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
28c0a 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 69 41  , OP_Compare, iA
28c0b 4d 65 6d 2c 20 69 42 4d 65 6d 2c 20 70 47 72 6f  Mem, iBMem, pGro
28c0c 75 70 42 79 2d 3e 6e 45 78 70 72 2c 0a 20 20 20  upBy->nExpr,.   
28c0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28c0e 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
28c0f 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e  eyInfo, P4_KEYIN
28c10 46 4f 29 3b 0a 20 20 20 20 20 20 6a 31 20 3d 20  FO);.      j1 = 
28c11 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
28c12 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20  ntAddr(v);.     
28c13 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28c14 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a  p3(v, OP_Jump, j
28c15 31 2b 31 2c 20 30 2c 20 6a 31 2b 31 29 3b 0a 0a  1+1, 0, j1+1);..
28c16 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
28c17 65 20 63 6f 64 65 20 74 68 61 74 20 72 75 6e 73  e code that runs
28c18 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 47 52   whenever the GR
28c19 4f 55 50 20 42 59 20 63 68 61 6e 67 65 73 2e 0a  OUP BY changes..
28c1a 20 20 20 20 20 20 2a 2a 20 43 68 61 6e 67 65 73        ** Changes
28c1b 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59   in the GROUP BY
28c1c 20 61 72 65 20 64 65 74 65 63 74 65 64 20 62 79   are detected by
28c1d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 6f   the previous co
28c1e 64 65 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 63  de.      ** bloc
28c1f 6b 2e 20 20 49 66 20 74 68 65 72 65 20 77 65 72  k.  If there wer
28c20 65 20 6e 6f 20 63 68 61 6e 67 65 73 2c 20 74 68  e no changes, th
28c21 69 73 20 62 6c 6f 63 6b 20 69 73 20 73 6b 69 70  is block is skip
28c22 70 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ped..      **.  
28c23 20 20 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65      ** This code
28c24 20 63 6f 70 69 65 73 20 63 75 72 72 65 6e 74 20   copies current 
28c25 67 72 6f 75 70 20 62 79 20 74 65 72 6d 73 20 69  group by terms i
28c26 6e 20 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e 0a 20  n b0,b1,b2,.... 
28c27 20 20 20 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20       ** over to 
28c28 61 30 2c 61 31 2c 61 32 2e 20 20 49 74 20 74 68  a0,a1,a2.  It th
28c29 65 6e 20 63 61 6c 6c 73 20 74 68 65 20 6f 75 74  en calls the out
28c2a 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65 0a 20  put subroutine. 
28c2b 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65       ** and rese
28c2c 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65  ts the aggregate
28c2d 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 72 65 67   accumulator reg
28c2e 69 73 74 65 72 73 20 69 6e 20 70 72 65 70 61 72  isters in prepar
28c2f 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 66  ation.      ** f
28c30 6f 72 20 74 68 65 20 6e 65 78 74 20 47 52 4f 55  or the next GROU
28c31 50 20 42 59 20 62 61 74 63 68 2e 0a 20 20 20 20  P BY batch..    
28c32 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
28c33 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70  e3ExprCodeMove(p
28c34 50 61 72 73 65 2c 20 69 42 4d 65 6d 2c 20 69 41  Parse, iBMem, iA
28c35 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e  Mem, pGroupBy->n
28c36 45 78 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c  Expr);.      sql
28c37 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28c38 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f  , OP_Gosub, regO
28c39 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72 4f 75  utputRow, addrOu
28c3a 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20  tputRow);.      
28c3b 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
28c3c 22 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 22  "output one row"
28c3d 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
28c3e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28c3f 50 5f 49 66 50 6f 73 2c 20 69 41 62 6f 72 74 46  P_IfPos, iAbortF
28c40 6c 61 67 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20  lag, addrEnd);. 
28c41 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
28c42 28 28 76 2c 20 22 63 68 65 63 6b 20 61 62 6f 72  ((v, "check abor
28c43 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20  t flag"));.     
28c44 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28c45 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
28c46 72 65 67 52 65 73 65 74 2c 20 61 64 64 72 52 65  regReset, addrRe
28c47 73 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65  set);.      Vdbe
28c48 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 73  Comment((v, "res
28c49 65 74 20 61 63 63 75 6d 75 6c 61 74 6f 72 22 29  et accumulator")
28c4a 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64  );..      /* Upd
28c4b 61 74 65 20 74 68 65 20 61 67 67 72 65 67 61 74  ate the aggregat
28c4c 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 62  e accumulators b
28c4d 61 73 65 64 20 6f 6e 20 74 68 65 20 63 6f 6e 74  ased on the cont
28c4e 65 6e 74 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20  ent of.      ** 
28c4f 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a  the current row.
28c50 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
28c51 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
28c52 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20  re(v, j1);.     
28c53 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74   updateAccumulat
28c54 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67  or(pParse, &sAgg
28c55 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c  Info);.      sql
28c56 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28c57 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
28c58 20 69 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20   iUseFlag);.    
28c59 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
28c5a 2c 20 22 69 6e 64 69 63 61 74 65 20 64 61 74 61  , "indicate data
28c5b 20 69 6e 20 61 63 63 75 6d 75 6c 61 74 6f 72 22   in accumulator"
28c5c 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 45 6e  ));..      /* En
28c5d 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20  d of the loop.  
28c5e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
28c5f 20 67 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a   groupBySort ){.
28c60 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28c61 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28c62 4e 65 78 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73  Next, sAggInfo.s
28c63 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72 54  ortingIdx, addrT
28c64 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 20  opOfLoop);.     
28c65 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
28c66 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
28c67 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  pWInfo);.       
28c68 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
28c69 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72  geToNoop(v, addr
28c6a 53 6f 72 74 69 6e 67 49 64 78 2c 20 31 29 3b 0a  SortingIdx, 1);.
28c6b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
28c6c 2a 20 4f 75 74 70 75 74 20 74 68 65 20 66 69 6e  * Output the fin
28c6d 61 6c 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74  al row of result
28c6e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
28c6f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28c70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
28c71 65 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64  egOutputRow, add
28c72 72 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20  rOutputRow);.   
28c73 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
28c74 76 2c 20 22 6f 75 74 70 75 74 20 66 69 6e 61 6c  v, "output final
28c75 20 72 6f 77 22 29 29 3b 0a 0a 20 20 20 20 20 20   row"));..      
28c76 2f 2a 20 4a 75 6d 70 20 6f 76 65 72 20 74 68 65  /* Jump over the
28c77 20 73 75 62 72 6f 75 74 69 6e 65 73 0a 20 20 20   subroutines.   
28c78 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
28c79 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28c7a 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64   OP_Goto, 0, add
28c7b 72 45 6e 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  rEnd);..      /*
28c7c 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72   Generate a subr
28c7d 6f 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70  outine that outp
28c7e 75 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  uts a single row
28c7f 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 0a 20   of the result. 
28c80 20 20 20 20 20 2a 2a 20 73 65 74 2e 20 20 54 68       ** set.  Th
28c81 69 73 20 73 75 62 72 6f 75 74 69 6e 65 20 66 69  is subroutine fi
28c82 72 73 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65  rst looks at the
28c83 20 69 55 73 65 46 6c 61 67 2e 20 20 49 66 20 69   iUseFlag.  If i
28c84 55 73 65 46 6c 61 67 0a 20 20 20 20 20 20 2a 2a  UseFlag.      **
28c85 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
28c86 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20   equal to zero, 
28c87 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69  the subroutine i
28c88 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20  s a no-op.  If. 
28c89 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63       ** the proc
28c8a 65 73 73 69 6e 67 20 63 61 6c 6c 73 20 66 6f 72  essing calls for
28c8b 20 74 68 65 20 71 75 65 72 79 20 74 6f 20 61 62   the query to ab
28c8c 6f 72 74 2c 20 74 68 69 73 20 73 75 62 72 6f 75  ort, this subrou
28c8d 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e  tine.      ** in
28c8e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 69 41 62  crements the iAb
28c8f 6f 72 74 46 6c 61 67 20 6d 65 6d 6f 72 79 20 6c  ortFlag memory l
28c90 6f 63 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 72  ocation before r
28c91 65 74 75 72 6e 69 6e 67 20 69 6e 0a 20 20 20 20  eturning in.    
28c92 20 20 2a 2a 20 6f 72 64 65 72 20 74 6f 20 73 69    ** order to si
28c93 67 6e 61 6c 20 74 68 65 20 63 61 6c 6c 65 72 20  gnal the caller 
28c94 74 6f 20 61 62 6f 72 74 2e 0a 20 20 20 20 20 20  to abort..      
28c95 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 53 65 74  */.      addrSet
28c96 41 62 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56  Abort = sqlite3V
28c97 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
28c98 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
28c99 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28c9a 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 41 62  _Integer, 1, iAb
28c9b 6f 72 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20  ortFlag);.      
28c9c 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
28c9d 22 73 65 74 20 61 62 6f 72 74 20 66 6c 61 67 22  "set abort flag"
28c9e 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
28c9f 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
28ca0 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74  P_Return, regOut
28ca1 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 73  putRow);.      s
28ca2 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
28ca3 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 4f 75  eLabel(v, addrOu
28ca4 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20  tputRow);.      
28ca5 61 64 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20  addrOutputRow = 
28ca6 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
28ca7 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20  ntAddr(v);.     
28ca8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28ca9 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p2(v, OP_IfPos, 
28caa 69 55 73 65 46 6c 61 67 2c 20 61 64 64 72 4f 75  iUseFlag, addrOu
28cab 74 70 75 74 52 6f 77 2b 32 29 3b 0a 20 20 20 20  tputRow+2);.    
28cac 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
28cad 2c 20 22 47 72 6f 75 70 62 79 20 72 65 73 75 6c  , "Groupby resul
28cae 74 20 67 65 6e 65 72 61 74 6f 72 20 65 6e 74 72  t generator entr
28caf 79 20 70 6f 69 6e 74 22 29 29 3b 0a 20 20 20 20  y point"));.    
28cb0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28cb1 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e  Op1(v, OP_Return
28cb2 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b  , regOutputRow);
28cb3 0a 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41  .      finalizeA
28cb4 67 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72  ggFunctions(pPar
28cb5 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
28cb6 20 20 20 20 20 20 69 66 28 20 70 48 61 76 69 6e        if( pHavin
28cb7 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  g ){.        sql
28cb8 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
28cb9 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c  pParse, pHaving,
28cba 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 31   addrOutputRow+1
28cbb 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
28cbc 55 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ULL);.      }.  
28cbd 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
28cbe 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
28cbf 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20  ->pEList, 0, 0, 
28cc0 70 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20 20  pOrderBy,.      
28cc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28cc2 64 69 73 74 69 6e 63 74 2c 20 70 44 65 73 74 2c  distinct, pDest,
28cc3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28cc4 20 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75         addrOutpu
28cc5 74 52 6f 77 2b 31 2c 20 61 64 64 72 53 65 74 41  tRow+1, addrSetA
28cc6 62 6f 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  bort);.      sql
28cc7 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
28cc8 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67  , OP_Return, reg
28cc9 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
28cca 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
28ccb 2c 20 22 65 6e 64 20 67 72 6f 75 70 62 79 20 72  , "end groupby r
28ccc 65 73 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 22  esult generator"
28ccd 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65  ));..      /* Ge
28cce 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74  nerate a subrout
28ccf 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65  ine that will re
28cd0 73 65 74 20 74 68 65 20 67 72 6f 75 70 2d 62 79  set the group-by
28cd1 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 20 20 20   accumulator.   
28cd2 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
28cd3 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
28cd4 62 65 6c 28 76 2c 20 61 64 64 72 52 65 73 65 74  bel(v, addrReset
28cd5 29 3b 0a 20 20 20 20 20 20 72 65 73 65 74 41 63  );.      resetAc
28cd6 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
28cd7 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
28cd8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28cd9 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
28cda 72 6e 2c 20 72 65 67 52 65 73 65 74 29 3b 0a 20  rn, regReset);. 
28cdb 20 20 20 20 0a 20 20 20 20 7d 20 2f 2a 20 65 6e      .    } /* en
28cdc 64 69 66 20 70 47 72 6f 75 70 42 79 20 2a 2f 0a  dif pGroupBy */.
28cdd 20 20 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 20      else {.     
28cde 20 45 78 70 72 4c 69 73 74 20 2a 70 44 65 6c 20   ExprList *pDel 
28cdf 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 0;.#ifndef SQL
28ce0 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f  ITE_OMIT_BTREECO
28ce1 55 4e 54 0a 20 20 20 20 20 20 54 61 62 6c 65 20  UNT.      Table 
28ce2 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28  *pTab;.      if(
28ce3 20 28 70 54 61 62 20 3d 20 69 73 53 69 6d 70 6c   (pTab = isSimpl
28ce4 65 43 6f 75 6e 74 28 70 2c 20 26 73 41 67 67 49  eCount(p, &sAggI
28ce5 6e 66 6f 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  nfo))!=0 ){.    
28ce6 20 20 20 20 2f 2a 20 49 66 20 69 73 53 69 6d 70      /* If isSimp
28ce7 6c 65 43 6f 75 6e 74 28 29 20 72 65 74 75 72 6e  leCount() return
28ce8 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
28ce9 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
28cea 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 20 20 2a  , then.        *
28ceb 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  * the SQL statem
28cec 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ent is of the fo
28ced 72 6d 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  rm:.        **. 
28cee 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45         **   SELE
28cef 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  CT count(*) FROM
28cf0 20 3c 74 62 6c 3e 0a 20 20 20 20 20 20 20 20 2a   <tbl>.        *
28cf1 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 68 65  *.        ** whe
28cf2 72 65 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  re the Table str
28cf3 75 63 74 75 72 65 20 72 65 74 75 72 6e 65 64 20  ucture returned 
28cf4 72 65 70 72 65 73 65 6e 74 73 20 74 61 62 6c 65  represents table
28cf5 20 3c 74 62 6c 3e 2e 0a 20 20 20 20 20 20 20 20   <tbl>..        
28cf6 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68  **.        ** Th
28cf7 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  is statement is 
28cf8 73 6f 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 69  so common that i
28cf9 74 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 73  t is optimized s
28cfa 70 65 63 69 61 6c 6c 79 2e 20 54 68 65 0a 20 20  pecially. The.  
28cfb 20 20 20 20 20 20 2a 2a 20 4f 50 5f 43 6f 75 6e        ** OP_Coun
28cfc 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73  t instruction is
28cfd 20 65 78 65 63 75 74 65 64 20 65 69 74 68 65 72   executed either
28cfe 20 6f 6e 20 74 68 65 20 69 6e 74 6b 65 79 20 74   on the intkey t
28cff 61 62 6c 65 20 74 68 61 74 0a 20 20 20 20 20 20  able that.      
28d00 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68    ** contains th
28d01 65 20 64 61 74 61 20 66 6f 72 20 74 61 62 6c 65  e data for table
28d02 20 3c 74 62 6c 3e 20 6f 72 20 6f 6e 20 6f 6e 65   <tbl> or on one
28d03 20 6f 66 20 69 74 73 20 69 6e 64 65 78 65 73 2e   of its indexes.
28d04 20 49 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69   It.        ** i
28d05 73 20 62 65 74 74 65 72 20 74 6f 20 65 78 65 63  s better to exec
28d06 75 74 65 20 74 68 65 20 6f 70 20 6f 6e 20 61 6e  ute the op on an
28d07 20 69 6e 64 65 78 2c 20 61 73 20 69 6e 64 65 78   index, as index
28d08 65 73 20 61 72 65 20 61 6c 6d 6f 73 74 0a 20 20  es are almost.  
28d09 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20        ** always 
28d0a 73 70 72 65 61 64 20 61 63 72 6f 73 73 20 6c 65  spread across le
28d0b 73 73 20 70 61 67 65 73 20 74 68 61 6e 20 74 68  ss pages than th
28d0c 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  eir correspondin
28d0d 67 20 74 61 62 6c 65 73 2e 0a 20 20 20 20 20 20  g tables..      
28d0e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e    */.        con
28d0f 73 74 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c  st int iDb = sql
28d10 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
28d11 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
28d12 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
28d13 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
28d14 69 43 73 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iCsr = pParse->n
28d15 54 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 43 75  Tab++;     /* Cu
28d16 72 73 6f 72 20 74 6f 20 73 63 61 6e 20 62 2d 74  rsor to scan b-t
28d17 72 65 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 49  ree */.        I
28d18 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
28d19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d1a 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
28d1b 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20  variable */.    
28d1c 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
28d1d 79 49 6e 66 6f 20 3d 20 30 3b 20 20 20 20 20 20  yInfo = 0;      
28d1e 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 69           /* Keyi
28d1f 6e 66 6f 20 66 6f 72 20 73 63 61 6e 6e 65 64 20  nfo for scanned 
28d20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20 20 20  index */.       
28d21 20 49 6e 64 65 78 20 2a 70 42 65 73 74 20 3d 20   Index *pBest = 
28d22 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
28d23 20 20 20 20 20 20 2f 2a 20 42 65 73 74 20 69 6e        /* Best in
28d24 64 65 78 20 66 6f 75 6e 64 20 73 6f 20 66 61 72  dex found so far
28d25 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20   */.        int 
28d26 69 52 6f 6f 74 20 3d 20 70 54 61 62 2d 3e 74 6e  iRoot = pTab->tn
28d27 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
28d28 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66   /* Root page of
28d29 20 73 63 61 6e 6e 65 64 20 62 2d 74 72 65 65 20   scanned b-tree 
28d2a 2a 2f 0a 0a 20 20 20 20 20 20 20 20 73 71 6c 69  */..        sqli
28d2b 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
28d2c 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
28d2d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
28d2e 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
28d2f 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e  e, iDb, pTab->tn
28d30 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  um, 0, pTab->zNa
28d31 6d 65 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  me);..        /*
28d32 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 20   Search for the 
28d33 69 6e 64 65 78 20 74 68 61 74 20 68 61 73 20 74  index that has t
28d34 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20  he least amount 
28d35 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 0a 20  of columns. If. 
28d36 20 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20         ** there 
28d37 69 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65 78  is such an index
28d38 2c 20 61 6e 64 20 69 74 20 68 61 73 20 6c 65 73  , and it has les
28d39 73 20 63 6f 6c 75 6d 6e 73 20 74 68 61 6e 20 74  s columns than t
28d3a 68 65 20 74 61 62 6c 65 0a 20 20 20 20 20 20 20  he table.       
28d3b 20 2a 2a 20 64 6f 65 73 2c 20 74 68 65 6e 20 77   ** does, then w
28d3c 65 20 63 61 6e 20 61 73 73 75 6d 65 20 74 68 61  e can assume tha
28d3d 74 20 69 74 20 63 6f 6e 73 75 6d 65 73 20 6c 65  t it consumes le
28d3e 73 73 20 73 70 61 63 65 20 6f 6e 20 64 69 73 6b  ss space on disk
28d3f 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20   and.        ** 
28d40 77 69 6c 6c 20 74 68 65 72 65 66 6f 72 65 20 62  will therefore b
28d41 65 20 63 68 65 61 70 65 72 20 74 6f 20 73 63 61  e cheaper to sca
28d42 6e 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  n to determine t
28d43 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 2e  he query result.
28d44 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20 74  .        ** In t
28d45 68 69 73 20 63 61 73 65 20 73 65 74 20 69 52 6f  his case set iRo
28d46 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 70  ot to the root p
28d47 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
28d48 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65 0a 20  e index b-tree. 
28d49 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 70 4b         ** and pK
28d4a 65 79 49 6e 66 6f 20 74 6f 20 74 68 65 20 4b 65  eyInfo to the Ke
28d4b 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
28d4c 72 65 71 75 69 72 65 64 20 74 6f 20 6e 61 76 69  required to navi
28d4d 67 61 74 65 20 74 68 65 0a 20 20 20 20 20 20 20  gate the.       
28d4e 20 2a 2a 20 69 6e 64 65 78 2e 0a 20 20 20 20 20   ** index..     
28d4f 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
28d50 20 49 6e 20 70 72 61 63 74 69 63 65 20 74 68 65   In practice the
28d51 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
28d52 72 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75  re will not be u
28d53 73 65 64 2e 20 49 74 20 69 73 20 6f 6e 6c 79 20  sed. It is only 
28d54 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 73 73  .        ** pass
28d55 65 64 20 74 6f 20 6b 65 65 70 20 4f 50 5f 4f 70  ed to keep OP_Op
28d56 65 6e 52 65 61 64 20 68 61 70 70 79 2e 0a 20 20  enRead happy..  
28d57 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
28d58 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
28d59 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
28d5a 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
28d5b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
28d5c 70 42 65 73 74 20 7c 7c 20 70 49 64 78 2d 3e 6e  pBest || pIdx->n
28d5d 43 6f 6c 75 6d 6e 3c 70 42 65 73 74 2d 3e 6e 43  Column<pBest->nC
28d5e 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
28d5f 20 20 20 20 20 70 42 65 73 74 20 3d 20 70 49 64       pBest = pId
28d60 78 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  x;.          }. 
28d61 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
28d62 20 69 66 28 20 70 42 65 73 74 20 26 26 20 70 42   if( pBest && pB
28d63 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70 54 61  est->nColumn<pTa
28d64 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
28d65 20 20 20 20 20 69 52 6f 6f 74 20 3d 20 70 42 65       iRoot = pBe
28d66 73 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20  st->tnum;.      
28d67 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73      pKeyInfo = s
28d68 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
28d69 66 6f 28 70 50 61 72 73 65 2c 20 70 42 65 73 74  fo(pParse, pBest
28d6a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  );.        }..  
28d6b 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20        /* Open a 
28d6c 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72  read-only cursor
28d6d 2c 20 65 78 65 63 75 74 65 20 74 68 65 20 4f 50  , execute the OP
28d6e 5f 43 6f 75 6e 74 2c 20 63 6c 6f 73 65 20 74 68  _Count, close th
28d6f 65 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 20  e cursor. */.   
28d70 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
28d71 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65  AddOp3(v, OP_Ope
28d72 6e 52 65 61 64 2c 20 69 43 73 72 2c 20 69 52 6f  nRead, iCsr, iRo
28d73 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  ot, iDb);.      
28d74 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 20 29    if( pKeyInfo )
28d75 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
28d76 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
28d77 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70  v, -1, (char *)p
28d78 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  KeyInfo, P4_KEYI
28d79 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
28d7a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
28d7b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28d7c 32 28 76 2c 20 4f 50 5f 43 6f 75 6e 74 2c 20 69  2(v, OP_Count, i
28d7d 43 73 72 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46  Csr, sAggInfo.aF
28d7e 75 6e 63 5b 30 5d 2e 69 4d 65 6d 29 3b 0a 20 20  unc[0].iMem);.  
28d7f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28d80 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
28d81 6f 73 65 2c 20 69 43 73 72 29 3b 0a 20 20 20 20  ose, iCsr);.    
28d82 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
28d83 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54  * SQLITE_OMIT_BT
28d84 52 45 45 43 4f 55 4e 54 20 2a 2f 0a 20 20 20 20  REECOUNT */.    
28d85 20 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43    {.        /* C
28d86 68 65 63 6b 20 69 66 20 74 68 65 20 71 75 65 72  heck if the quer
28d87 79 20 69 73 20 6f 66 20 6f 6e 65 20 6f 66 20 74  y is of one of t
28d88 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72  he following for
28d89 6d 73 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ms:.        **. 
28d8a 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45         **   SELE
28d8b 43 54 20 6d 69 6e 28 78 29 20 46 52 4f 4d 20 2e  CT min(x) FROM .
28d8c 2e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  ...        **   
28d8d 53 45 4c 45 43 54 20 6d 61 78 28 78 29 20 46 52  SELECT max(x) FR
28d8e 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 20 20 2a  OM ....        *
28d8f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20  *.        ** If 
28d90 69 74 20 69 73 2c 20 74 68 65 6e 20 61 73 6b 20  it is, then ask 
28d91 74 68 65 20 63 6f 64 65 20 69 6e 20 77 68 65 72  the code in wher
28d92 65 2e 63 20 74 6f 20 61 74 74 65 6d 70 74 20 74  e.c to attempt t
28d93 6f 20 73 6f 72 74 20 72 65 73 75 6c 74 73 0a 20  o sort results. 
28d94 20 20 20 20 20 20 20 2a 2a 20 61 73 20 69 66 20         ** as if 
28d95 74 68 65 72 65 20 77 61 73 20 61 6e 20 22 4f 52  there was an "OR
28d96 44 45 52 20 4f 4e 20 78 22 20 6f 72 20 22 4f 52  DER ON x" or "OR
28d97 44 45 52 20 4f 4e 20 78 20 44 45 53 43 22 20 63  DER ON x DESC" c
28d98 6c 61 75 73 65 2e 20 0a 20 20 20 20 20 20 20 20  lause. .        
28d99 2a 2a 20 49 66 20 77 68 65 72 65 2e 63 20 69 73  ** If where.c is
28d9a 20 61 62 6c 65 20 74 6f 20 70 72 6f 64 75 63 65   able to produce
28d9b 20 72 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20   results sorted 
28d9c 69 6e 20 74 68 69 73 20 6f 72 64 65 72 2c 20 74  in this order, t
28d9d 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  hen.        ** a
28d9e 64 64 20 76 64 62 65 20 63 6f 64 65 20 74 6f 20  dd vdbe code to 
28d9f 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
28da0 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 6f 70   processing loop
28da1 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 20 20   after the .    
28da2 20 20 20 20 2a 2a 20 66 69 72 73 74 20 69 74 65      ** first ite
28da3 72 61 74 69 6f 6e 20 28 73 69 6e 63 65 20 74 68  ration (since th
28da4 65 20 66 69 72 73 74 20 69 74 65 72 61 74 69 6f  e first iteratio
28da5 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 73  n of the loop is
28da6 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 75 61   .        ** gua
28da7 72 61 6e 74 65 65 64 20 74 6f 20 6f 70 65 72 61  ranteed to opera
28da8 74 65 20 6f 6e 20 74 68 65 20 72 6f 77 20 77 69  te on the row wi
28da9 74 68 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f  th the minimum o
28daa 72 20 6d 61 78 69 6d 75 6d 20 0a 20 20 20 20 20  r maximum .     
28dab 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 78     ** value of x
28dac 2c 20 74 68 65 20 6f 6e 6c 79 20 72 6f 77 20 72  , the only row r
28dad 65 71 75 69 72 65 64 29 2e 0a 20 20 20 20 20 20  equired)..      
28dae 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
28daf 41 20 73 70 65 63 69 61 6c 20 66 6c 61 67 20 6d  A special flag m
28db0 75 73 74 20 62 65 20 70 61 73 73 65 64 20 74 6f  ust be passed to
28db1 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
28db2 69 6e 28 29 20 74 6f 20 73 6c 69 67 68 74 6c 79  in() to slightly
28db3 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 69  .        ** modi
28db4 66 79 20 62 65 68 61 76 69 6f 75 72 20 61 73 20  fy behaviour as 
28db5 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 20 20 20  follows:.       
28db6 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   **.        **  
28db7 20 2b 20 49 66 20 74 68 65 20 71 75 65 72 79 20   + If the query 
28db8 69 73 20 61 20 22 53 45 4c 45 43 54 20 6d 69 6e  is a "SELECT min
28db9 28 78 29 22 2c 20 74 68 65 6e 20 74 68 65 20 6c  (x)", then the l
28dba 6f 6f 70 20 63 6f 64 65 64 20 62 79 0a 20 20 20  oop coded by.   
28dbb 20 20 20 20 20 2a 2a 20 20 20 20 20 77 68 65 72       **     wher
28dbc 65 2e 63 20 73 68 6f 75 6c 64 20 6e 6f 74 20 69  e.c should not i
28dbd 74 65 72 61 74 65 20 6f 76 65 72 20 61 6e 79 20  terate over any 
28dbe 76 61 6c 75 65 73 20 77 69 74 68 20 61 20 4e 55  values with a NU
28dbf 4c 4c 20 76 61 6c 75 65 0a 20 20 20 20 20 20 20  LL value.       
28dc0 20 2a 2a 20 20 20 20 20 66 6f 72 20 78 2e 0a 20   **     for x.. 
28dc1 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
28dc2 20 20 2a 2a 20 20 20 2b 20 54 68 65 20 6f 70 74    **   + The opt
28dc3 69 6d 69 7a 65 72 20 63 6f 64 65 20 69 6e 20 77  imizer code in w
28dc4 68 65 72 65 2e 63 20 28 74 68 65 20 74 68 69 6e  here.c (the thin
28dc5 67 20 74 68 61 74 20 64 65 63 69 64 65 73 20 77  g that decides w
28dc6 68 69 63 68 0a 20 20 20 20 20 20 20 20 2a 2a 20  hich.        ** 
28dc7 20 20 20 20 69 6e 64 65 78 20 6f 72 20 69 6e 64      index or ind
28dc8 69 63 65 73 20 74 6f 20 75 73 65 29 20 73 68 6f  ices to use) sho
28dc9 75 6c 64 20 70 6c 61 63 65 20 61 20 64 69 66 66  uld place a diff
28dca 65 72 65 6e 74 20 70 72 69 6f 72 69 74 79 20 6f  erent priority o
28dcb 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  n .        **   
28dcc 20 20 73 61 74 69 73 66 79 69 6e 67 20 74 68 65    satisfying the
28dcd 20 27 4f 52 44 45 52 20 42 59 27 20 63 6c 61 75   'ORDER BY' clau
28dce 73 65 20 74 68 61 6e 20 69 74 20 64 6f 65 73 20  se than it does 
28dcf 69 6e 20 6f 74 68 65 72 20 63 61 73 65 73 2e 0a  in other cases..
28dd0 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 52          **     R
28dd1 65 66 65 72 20 74 6f 20 63 6f 64 65 20 61 6e 64  efer to code and
28dd2 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 77 68 65   comments in whe
28dd3 72 65 2e 63 20 66 6f 72 20 64 65 74 61 69 6c 73  re.c for details
28dd4 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
28dd5 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
28dd6 4d 69 6e 4d 61 78 20 3d 20 30 3b 0a 20 20 20 20  MinMax = 0;.    
28dd7 20 20 20 20 75 38 20 66 6c 61 67 20 3d 20 6d 69      u8 flag = mi
28dd8 6e 4d 61 78 51 75 65 72 79 28 70 29 3b 0a 20 20  nMaxQuery(p);.  
28dd9 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 20 29        if( flag )
28dda 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
28ddb 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
28ddc 65 72 74 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e  erty(p->pEList->
28ddd 61 5b 30 5d 2e 70 45 78 70 72 2c 20 45 50 5f 78  a[0].pExpr, EP_x
28dde 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
28ddf 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78 20 3d         pMinMax =
28de0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
28de1 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73  Dup(db, p->pELis
28de2 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 78  t->a[0].pExpr->x
28de3 2e 70 4c 69 73 74 2c 30 29 3b 0a 20 20 20 20 20  .pList,0);.     
28de4 20 20 20 20 20 70 44 65 6c 20 3d 20 70 4d 69 6e       pDel = pMin
28de5 4d 61 78 3b 0a 20 20 20 20 20 20 20 20 20 20 69  Max;.          i
28de6 66 28 20 70 4d 69 6e 4d 61 78 20 26 26 20 21 64  f( pMinMax && !d
28de7 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
28de8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
28de9 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 73 6f 72  MinMax->a[0].sor
28dea 74 4f 72 64 65 72 20 3d 20 66 6c 61 67 21 3d 57  tOrder = flag!=W
28deb 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
28dec 20 3f 31 3a 30 3b 0a 20 20 20 20 20 20 20 20 20   ?1:0;.         
28ded 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d     pMinMax->a[0]
28dee 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f  .pExpr->op = TK_
28def 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20  COLUMN;.        
28df0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
28df1 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
28df2 20 63 61 73 65 20 72 75 6e 73 20 69 66 20 74 68   case runs if th
28df3 65 20 61 67 67 72 65 67 61 74 65 20 68 61 73 20  e aggregate has 
28df4 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  no GROUP BY clau
28df5 73 65 2e 20 20 54 68 65 0a 20 20 20 20 20 20 20  se.  The.       
28df6 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 69   ** processing i
28df7 73 20 6d 75 63 68 20 73 69 6d 70 6c 65 72 20 73  s much simpler s
28df8 69 6e 63 65 20 74 68 65 72 65 20 69 73 20 6f 6e  ince there is on
28df9 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 0a  ly a single row.
28dfa 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 6f 75          ** of ou
28dfb 74 70 75 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f  tput..        */
28dfc 0a 20 20 20 20 20 20 20 20 72 65 73 65 74 41 63  .        resetAc
28dfd 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
28dfe 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
28dff 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73        pWInfo = s
28e00 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
28e01 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
28e02 74 2c 20 70 57 68 65 72 65 2c 20 26 70 4d 69 6e  t, pWhere, &pMin
28e03 4d 61 78 2c 20 66 6c 61 67 2c 20 30 29 3b 0a 20  Max, flag, 0);. 
28e04 20 20 20 20 20 20 20 69 66 28 20 70 57 49 6e 66         if( pWInf
28e05 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  o==0 ){.        
28e06 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
28e07 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c  tDelete(db, pDel
28e08 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
28e09 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
28e0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
28e0b 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f  updateAccumulato
28e0c 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49  r(pParse, &sAggI
28e0d 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66  nfo);.        if
28e0e 28 20 21 70 4d 69 6e 4d 61 78 20 26 26 20 66 6c  ( !pMinMax && fl
28e0f 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ag ){.          
28e10 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28e11 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
28e12 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29   pWInfo->iBreak)
28e13 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64 62 65  ;.          Vdbe
28e14 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 28  Comment((v, "%s(
28e15 29 20 62 79 20 69 6e 64 65 78 22 2c 0a 20 20 20  ) by index",.   
28e16 20 20 20 20 20 20 20 20 20 20 20 20 20 28 66 6c               (fl
28e17 61 67 3d 3d 57 48 45 52 45 5f 4f 52 44 45 52 42  ag==WHERE_ORDERB
28e18 59 5f 4d 49 4e 3f 22 6d 69 6e 22 3a 22 6d 61 78  Y_MIN?"min":"max
28e19 22 29 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ")));.        }.
28e1a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57          sqlite3W
28e1b 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
28e1c 0a 20 20 20 20 20 20 20 20 66 69 6e 61 6c 69 7a  .        finaliz
28e1d 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28 70 50  eAggFunctions(pP
28e1e 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29  arse, &sAggInfo)
28e1f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
28e20 20 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20   pOrderBy = 0;. 
28e21 20 20 20 20 20 69 66 28 20 70 48 61 76 69 6e 67       if( pHaving
28e22 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
28e23 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
28e24 50 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c 20  Parse, pHaving, 
28e25 61 64 64 72 45 6e 64 2c 20 53 51 4c 49 54 45 5f  addrEnd, SQLITE_
28e26 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
28e27 20 20 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63     }.      selec
28e28 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73  tInnerLoop(pPars
28e29 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c  e, p, p->pEList,
28e2a 20 30 2c 20 30 2c 20 30 2c 20 2d 31 2c 20 0a 20   0, 0, 0, -1, . 
28e2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28e2c 20 20 20 20 20 70 44 65 73 74 2c 20 61 64 64 72       pDest, addr
28e2d 45 6e 64 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20  End, addrEnd);. 
28e2e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
28e2f 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
28e30 44 65 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Del);.    }.    
28e31 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
28e32 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 45  veLabel(v, addrE
28e33 6e 64 29 3b 0a 20 20 20 20 0a 20 20 7d 20 2f 2a  nd);.    .  } /*
28e34 20 65 6e 64 69 66 20 61 67 67 72 65 67 61 74 65   endif aggregate
28e35 20 71 75 65 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20   query */..  /* 
28e36 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  If there is an O
28e37 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20  RDER BY clause, 
28e38 74 68 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 20  then we need to 
28e39 73 6f 72 74 20 74 68 65 20 72 65 73 75 6c 74 73  sort the results
28e3a 0a 20 20 2a 2a 20 61 6e 64 20 73 65 6e 64 20 74  .  ** and send t
28e3b 68 65 6d 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  hem to the callb
28e3c 61 63 6b 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a  ack one by one..
28e3d 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65    */.  if( pOrde
28e3e 72 42 79 20 29 7b 0a 20 20 20 20 67 65 6e 65 72  rBy ){.    gener
28e3f 61 74 65 53 6f 72 74 54 61 69 6c 28 70 50 61 72  ateSortTail(pPar
28e40 73 65 2c 20 70 2c 20 76 2c 20 70 45 4c 69 73 74  se, p, v, pEList
28e41 2d 3e 6e 45 78 70 72 2c 20 70 44 65 73 74 29 3b  ->nExpr, pDest);
28e42 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  .  }..  /* Jump 
28e43 68 65 72 65 20 74 6f 20 73 6b 69 70 20 74 68 69  here to skip thi
28e44 73 20 71 75 65 72 79 0a 20 20 2a 2f 0a 20 20 73  s query.  */.  s
28e45 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
28e46 65 4c 61 62 65 6c 28 76 2c 20 69 45 6e 64 29 3b  eLabel(v, iEnd);
28e47 0a 0a 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43  ..  /* The SELEC
28e48 54 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  T was successful
28e49 6c 79 20 63 6f 64 65 64 2e 20 20 20 53 65 74 20  ly coded.   Set 
28e4a 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
28e4b 74 6f 20 30 0a 20 20 2a 2a 20 74 6f 20 69 6e 64  to 0.  ** to ind
28e4c 69 63 61 74 65 20 6e 6f 20 65 72 72 6f 72 73 2e  icate no errors.
28e4d 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 30 3b 0a  .  */.  rc = 0;.
28e4e 0a 20 20 2f 2a 20 43 6f 6e 74 72 6f 6c 20 6a 75  .  /* Control ju
28e4f 6d 70 73 20 74 6f 20 68 65 72 65 20 69 66 20 61  mps to here if a
28e50 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75  n error is encou
28e51 6e 74 65 72 65 64 20 61 62 6f 76 65 2c 20 6f 72  ntered above, or
28e52 20 75 70 6f 6e 0a 20 20 2a 2a 20 73 75 63 63 65   upon.  ** succe
28e53 73 73 66 75 6c 20 63 6f 64 69 6e 67 20 6f 66 20  ssful coding of 
28e54 74 68 65 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2f  the SELECT..  */
28e55 0a 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 0a 20 20  .select_end:..  
28e56 2f 2a 20 49 64 65 6e 74 69 66 79 20 63 6f 6c 75  /* Identify colu
28e57 6d 6e 20 6e 61 6d 65 73 20 69 66 20 72 65 73 75  mn names if resu
28e58 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43  lts of the SELEC
28e59 54 20 61 72 65 20 74 6f 20 62 65 20 6f 75 74 70  T are to be outp
28e5a 75 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  ut..  */.  if( r
28e5b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
28e5c 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
28e5d 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20  T_Output ){.    
28e5e 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61  generateColumnNa
28e5f 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  mes(pParse, pTab
28e60 4c 69 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 20  List, pEList);. 
28e61 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46   }..  sqlite3DbF
28e62 72 65 65 28 64 62 2c 20 73 41 67 67 49 6e 66 6f  ree(db, sAggInfo
28e63 2e 61 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65  .aCol);.  sqlite
28e64 33 44 62 46 72 65 65 28 64 62 2c 20 73 41 67 67  3DbFree(db, sAgg
28e65 49 6e 66 6f 2e 61 46 75 6e 63 29 3b 0a 20 20 72  Info.aFunc);.  r
28e66 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
28e67 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
28e68 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a  DEBUG)./*.******
28e69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
28e6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
28e6f 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
28e70 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
28e71 67 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 64  g only.  The cod
28e72 65 0a 2a 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77  e.** that follow
28e73 73 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 65 61  s does not appea
28e74 72 20 69 6e 20 6e 6f 72 6d 61 6c 20 62 75 69 6c  r in normal buil
28e75 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ds..**.** These 
28e76 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
28e77 64 20 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 74  d to print out t
28e78 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6c  he content of al
28e79 6c 20 6f 72 20 70 61 72 74 20 6f 66 20 61 20 0a  l or part of a .
28e7a 2a 2a 20 70 61 72 73 65 20 73 74 72 75 63 74 75  ** parse structu
28e7b 72 65 73 20 73 75 63 68 20 61 73 20 53 65 6c 65  res such as Sele
28e7c 63 74 20 6f 72 20 45 78 70 72 2e 20 20 53 75 63  ct or Expr.  Suc
28e7d 68 20 70 72 69 6e 74 6f 75 74 73 20 61 72 65 20  h printouts are 
28e7e 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 68 65  useful.** for he
28e7f 6c 70 69 6e 67 20 74 6f 20 75 6e 64 65 72 73 74  lping to underst
28e80 61 6e 64 20 77 68 61 74 20 69 73 20 68 61 70 70  and what is happ
28e81 65 6e 69 6e 67 20 69 6e 73 69 64 65 20 74 68 65  ening inside the
28e82 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 0a   code generator.
28e83 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 65 78  ** during the ex
28e84 65 63 75 74 69 6f 6e 20 6f 66 20 63 6f 6d 70 6c  ecution of compl
28e85 65 78 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ex SELECT statem
28e86 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ents..**.** Thes
28e87 65 20 72 6f 75 74 69 6e 65 20 61 72 65 20 6e 6f  e routine are no
28e88 74 20 63 61 6c 6c 65 64 20 61 6e 79 77 68 65 72  t called anywher
28e89 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  e from within th
28e8a 65 20 6e 6f 72 6d 61 6c 0a 2a 2a 20 63 6f 64 65  e normal.** code
28e8b 20 62 61 73 65 2e 20 20 54 68 65 6e 20 61 72 65   base.  Then are
28e8c 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
28e8d 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
28e8e 69 6e 20 74 68 65 20 64 65 62 75 67 67 65 72 0a  in the debugger.
28e8f 2a 2a 20 6f 72 20 66 72 6f 6d 20 74 65 6d 70 6f  ** or from tempo
28e90 72 61 72 79 20 22 70 72 69 6e 74 66 22 20 73 74  rary "printf" st
28e91 61 74 65 6d 65 6e 74 73 20 69 6e 73 65 72 74 65  atements inserte
28e92 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e  d for debugging.
28e93 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
28e94 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
28e95 72 69 6e 74 45 78 70 72 28 45 78 70 72 20 2a 70  rintExpr(Expr *p
28e96 29 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65  ){.  if( p->toke
28e97 6e 2e 7a 20 26 26 20 70 2d 3e 74 6f 6b 65 6e 2e  n.z && p->token.
28e98 6e 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  n>0 ){.    sqlit
28e99 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28  e3DebugPrintf("(
28e9a 25 2e 2a 73 22 2c 20 70 2d 3e 74 6f 6b 65 6e 2e  %.*s", p->token.
28e9b 6e 2c 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a  n, p->token.z);.
28e9c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
28e9d 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
28e9e 22 28 25 64 22 2c 20 70 2d 3e 6f 70 29 3b 0a 20  "(%d", p->op);. 
28e9f 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4c 65 66   }.  if( p->pLef
28ea0 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
28ea1 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 22 29  DebugPrintf(" ")
28ea2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69  ;.    sqlite3Pri
28ea3 6e 74 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 29  ntExpr(p->pLeft)
28ea4 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
28ea5 52 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c  Right ){.    sql
28ea6 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
28ea7 22 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  " ");.    sqlite
28ea8 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 52  3PrintExpr(p->pR
28ea9 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  ight);.  }.  sql
28eaa 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
28eab 22 29 22 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ")");.}.SQLITE_P
28eac 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
28ead 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74  te3PrintExprList
28eae 28 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74  (ExprList *pList
28eaf 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
28eb0 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  r(i=0; i<pList->
28eb1 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
28eb2 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70   sqlite3PrintExp
28eb3 72 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  r(pList->a[i].pE
28eb4 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20 69 3c  xpr);.    if( i<
28eb5 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 29  pList->nExpr-1 )
28eb6 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
28eb7 65 62 75 67 50 72 69 6e 74 66 28 22 2c 20 22 29  ebugPrintf(", ")
28eb8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51  ;.    }.  }.}.SQ
28eb9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
28eba 64 20 73 71 6c 69 74 65 33 50 72 69 6e 74 53 65  d sqlite3PrintSe
28ebb 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70 2c 20  lect(Select *p, 
28ebc 69 6e 74 20 69 6e 64 65 6e 74 29 7b 0a 20 20 73  int indent){.  s
28ebd 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
28ebe 66 28 22 25 2a 73 53 45 4c 45 43 54 28 25 70 29  f("%*sSELECT(%p)
28ebf 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 2c 20   ", indent, "", 
28ec0 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 72 69  p);.  sqlite3Pri
28ec1 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 45  ntExprList(p->pE
28ec2 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
28ec3 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22  DebugPrintf("\n"
28ec4 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53 72 63  );.  if( p->pSrc
28ec5 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 50   ){.    char *zP
28ec6 72 65 66 69 78 3b 0a 20 20 20 20 69 6e 74 20 69  refix;.    int i
28ec7 3b 0a 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20  ;.    zPrefix = 
28ec8 22 46 52 4f 4d 22 3b 0a 20 20 20 20 66 6f 72 28  "FROM";.    for(
28ec9 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e  i=0; i<p->pSrc->
28eca 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSrc; i++){.    
28ecb 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
28ecc 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
28ecd 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20  p->pSrc->a[i];. 
28ece 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
28ecf 67 50 72 69 6e 74 66 28 22 25 2a 73 20 22 2c 20  gPrintf("%*s ", 
28ed0 69 6e 64 65 6e 74 2b 36 2c 20 7a 50 72 65 66 69  indent+6, zPrefi
28ed1 78 29 3b 0a 20 20 20 20 20 20 7a 50 72 65 66 69  x);.      zPrefi
28ed2 78 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 69 66  x = "";.      if
28ed3 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  ( pItem->pSelect
28ed4 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
28ed5 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
28ed6 28 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 73  (\n");.        s
28ed7 71 6c 69 74 65 33 50 72 69 6e 74 53 65 6c 65 63  qlite3PrintSelec
28ed8 74 28 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  t(pItem->pSelect
28ed9 2c 20 69 6e 64 65 6e 74 2b 31 30 29 3b 0a 20 20  , indent+10);.  
28eda 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62        sqlite3Deb
28edb 75 67 50 72 69 6e 74 66 28 22 25 2a 73 29 22 2c  ugPrintf("%*s)",
28edc 20 69 6e 64 65 6e 74 2b 38 2c 20 22 22 29 3b 0a   indent+8, "");.
28edd 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
28ede 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 7b 0a  pItem->zName ){.
28edf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
28ee0 65 62 75 67 50 72 69 6e 74 66 28 22 25 73 22 2c  ebugPrintf("%s",
28ee1 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a   pItem->zName);.
28ee2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
28ee3 28 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 29 7b  ( pItem->pTab ){
28ee4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28ee5 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 74 61  DebugPrintf("(ta
28ee6 62 6c 65 3a 20 25 73 29 22 2c 20 70 49 74 65 6d  ble: %s)", pItem
28ee7 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  ->pTab->zName);.
28ee8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
28ee9 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20  ( pItem->zAlias 
28eea 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
28eeb 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
28eec 41 53 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a  AS %s", pItem->z
28eed 41 6c 69 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a  Alias);.      }.
28eee 20 20 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 70        if( i<p->p
28eef 53 72 63 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20  Src->nSrc-1 ){. 
28ef0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
28ef1 62 75 67 50 72 69 6e 74 66 28 22 2c 22 29 3b 0a  bugPrintf(",");.
28ef2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
28ef3 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
28ef4 28 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 20 20  ("\n");.    }.  
28ef5 7d 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72  }.  if( p->pWher
28ef6 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
28ef7 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73  DebugPrintf("%*s
28ef8 20 57 48 45 52 45 20 22 2c 20 69 6e 64 65 6e 74   WHERE ", indent
28ef9 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  , "");.    sqlit
28efa 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70  e3PrintExpr(p->p
28efb 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69  Where);.    sqli
28efc 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
28efd 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  \n");.  }.  if( 
28efe 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20  p->pGroupBy ){. 
28eff 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
28f00 72 69 6e 74 66 28 22 25 2a 73 20 47 52 4f 55 50  rintf("%*s GROUP
28f01 20 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22   BY ", indent, "
28f02 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ");.    sqlite3P
28f03 72 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e  rintExprList(p->
28f04 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 73  pGroupBy);.    s
28f05 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
28f06 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69  f("\n");.  }.  i
28f07 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 29 7b  f( p->pHaving ){
28f08 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
28f09 67 50 72 69 6e 74 66 28 22 25 2a 73 20 48 41 56  gPrintf("%*s HAV
28f0a 49 4e 47 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22  ING ", indent, "
28f0b 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ");.    sqlite3P
28f0c 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 48 61 76  rintExpr(p->pHav
28f0d 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ing);.    sqlite
28f0e 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e  3DebugPrintf("\n
28f0f 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ");.  }.  if( p-
28f10 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
28f11 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
28f12 6e 74 66 28 22 25 2a 73 20 4f 52 44 45 52 20 42  ntf("%*s ORDER B
28f13 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29  Y ", indent, "")
28f14 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69  ;.    sqlite3Pri
28f15 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f  ntExprList(p->pO
28f16 72 64 65 72 42 79 29 3b 0a 20 20 20 20 73 71 6c  rderBy);.    sql
28f17 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
28f18 22 5c 6e 22 29 3b 0a 20 20 7d 0a 7d 0a 2f 2a 20  "\n");.  }.}./* 
28f19 45 6e 64 20 6f 66 20 74 68 65 20 73 74 72 75 63  End of the struc
28f1a 74 75 72 65 20 64 65 62 75 67 20 70 72 69 6e 74  ture debug print
28f1b 69 6e 67 20 63 6f 64 65 0a 2a 2a 2a 2a 2a 2a 2a  ing code.*******
28f1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f20 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f  ******/.#endif /
28f21 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  * defined(SQLITE
28f22 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65  _TEST) || define
28f23 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
28f24 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
28f25 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 65 6c 65 63  *** End of selec
28f26 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
28f27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f29 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
28f2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74  *** Begin file t
28f2b 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  able.c *********
28f2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f2e 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
28f2f 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
28f30 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
28f31 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
28f32 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
28f33 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
28f34 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
28f35 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
28f36 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
28f37 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
28f38 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
28f39 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
28f3a 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
28f3b 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
28f3c 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
28f3d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
28f3e 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
28f3f 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
28f40 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
28f41 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
28f42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
28f46 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
28f47 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   the sqlite3_get
28f48 5f 74 61 62 6c 65 28 29 20 61 6e 64 20 73 71 6c  _table() and sql
28f49 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
28f4a 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72  ).** interface r
28f4b 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 73 65 20  outines.  These 
28f4c 61 72 65 20 6a 75 73 74 20 77 72 61 70 70 65 72  are just wrapper
28f4d 73 20 61 72 6f 75 6e 64 20 74 68 65 20 6d 61 69  s around the mai
28f4e 6e 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72  n.** interface r
28f4f 6f 75 74 69 6e 65 20 6f 66 20 73 71 6c 69 74 65  outine of sqlite
28f50 33 5f 65 78 65 63 28 29 2e 0a 2a 2a 0a 2a 2a 20  3_exec()..**.** 
28f51 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
28f52 72 65 20 69 6e 20 61 20 73 65 70 61 72 61 74 65  re in a separate
28f53 20 66 69 6c 65 73 20 73 6f 20 74 68 61 74 20 74   files so that t
28f54 68 65 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  hey will not be 
28f55 6c 69 6e 6b 65 64 0a 2a 2a 20 69 66 20 74 68 65  linked.** if the
28f56 79 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e 0a  y are not used..
28f57 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 61 62 6c 65  **.** $Id: table
28f58 2e 63 2c 76 20 31 2e 33 39 20 32 30 30 39 2f 30  .c,v 1.39 2009/0
28f59 31 2f 31 39 20 32 30 3a 34 39 3a 31 30 20 64 72  1/19 20:49:10 dr
28f5a 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e  h Exp $.*/..#ifn
28f5b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28f5c 47 45 54 5f 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a  GET_TABLE../*.**
28f5d 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
28f5e 69 73 20 75 73 65 64 20 74 6f 20 70 61 73 73 20  is used to pass 
28f5f 64 61 74 61 20 66 72 6f 6d 20 73 71 6c 69 74 65  data from sqlite
28f60 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 74 68  3_get_table() th
28f61 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20  rough.** to the 
28f62 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
28f63 6e 20 69 73 20 75 73 65 73 20 74 6f 20 62 75 69  n is uses to bui
28f64 6c 64 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a  ld the result..*
28f65 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
28f66 20 54 61 62 52 65 73 75 6c 74 20 7b 0a 20 20 63   TabResult {.  c
28f67 68 61 72 20 2a 2a 61 7a 52 65 73 75 6c 74 3b 0a  har **azResult;.
28f68 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b    char *zErrMsg;
28f69 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 3b 0a  .  int nResult;.
28f6a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20    int nAlloc;.  
28f6b 69 6e 74 20 6e 52 6f 77 3b 0a 20 20 69 6e 74 20  int nRow;.  int 
28f6c 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e  nColumn;.  int n
28f6d 44 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Data;.  int rc;.
28f6e 7d 20 54 61 62 52 65 73 75 6c 74 3b 0a 0a 2f 2a  } TabResult;../*
28f6f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
28f70 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   is called once 
28f71 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e 20  for each row in 
28f72 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
28f73 2e 20 20 49 74 73 20 6a 6f 62 0a 2a 2a 20 69 73  .  Its job.** is
28f74 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20   to fill in the 
28f75 54 61 62 52 65 73 75 6c 74 20 73 74 72 75 63 74  TabResult struct
28f76 75 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c  ure appropriatel
28f77 79 2c 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 65  y, allocating ne
28f78 77 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 73 20 6e  w.** memory as n
28f79 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61  ecessary..*/.sta
28f7a 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  tic int sqlite3_
28f7b 67 65 74 5f 74 61 62 6c 65 5f 63 62 28 76 6f 69  get_table_cb(voi
28f7c 64 20 2a 70 41 72 67 2c 20 69 6e 74 20 6e 43 6f  d *pArg, int nCo
28f7d 6c 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20  l, char **argv, 
28f7e 63 68 61 72 20 2a 2a 63 6f 6c 76 29 7b 0a 20 20  char **colv){.  
28f7f 54 61 62 52 65 73 75 6c 74 20 2a 70 20 3d 20 28  TabResult *p = (
28f80 54 61 62 52 65 73 75 6c 74 2a 29 70 41 72 67 3b  TabResult*)pArg;
28f81 0a 20 20 69 6e 74 20 6e 65 65 64 3b 0a 20 20 69  .  int need;.  i
28f82 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b  nt i;.  char *z;
28f83 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
28f84 20 74 68 65 72 65 20 69 73 20 65 6e 6f 75 67 68   there is enough
28f85 20 73 70 61 63 65 20 69 6e 20 70 2d 3e 61 7a 52   space in p->azR
28f86 65 73 75 6c 74 20 74 6f 20 68 6f 6c 64 20 65 76  esult to hold ev
28f87 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 65  erything.  ** we
28f88 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65   need to remembe
28f89 72 20 66 72 6f 6d 20 74 68 69 73 20 69 6e 76 6f  r from this invo
28f8a 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 61  cation of the ca
28f8b 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  llback..  */.  i
28f8c 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 26 26  f( p->nRow==0 &&
28f8d 20 61 72 67 76 21 3d 30 20 29 7b 0a 20 20 20 20   argv!=0 ){.    
28f8e 6e 65 65 64 20 3d 20 6e 43 6f 6c 2a 32 3b 0a 20  need = nCol*2;. 
28f8f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 65 64   }else{.    need
28f90 20 3d 20 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69   = nCol;.  }.  i
28f91 66 28 20 70 2d 3e 6e 44 61 74 61 20 2b 20 6e 65  f( p->nData + ne
28f92 65 64 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20  ed >= p->nAlloc 
28f93 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a  ){.    char **az
28f94 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c  New;.    p->nAll
28f95 6f 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32  oc = p->nAlloc*2
28f96 20 2b 20 6e 65 65 64 20 2b 20 31 3b 0a 20 20 20   + need + 1;.   
28f97 20 61 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   azNew = sqlite3
28f98 5f 72 65 61 6c 6c 6f 63 28 20 70 2d 3e 61 7a 52  _realloc( p->azR
28f99 65 73 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68  esult, sizeof(ch
28f9a 61 72 2a 29 2a 70 2d 3e 6e 41 6c 6c 6f 63 20 29  ar*)*p->nAlloc )
28f9b 3b 0a 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d  ;.    if( azNew=
28f9c 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63  =0 ) goto malloc
28f9d 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 70 2d 3e  _failed;.    p->
28f9e 61 7a 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77  azResult = azNew
28f9f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
28fa0 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74  his is the first
28fa1 20 72 6f 77 2c 20 74 68 65 6e 20 67 65 6e 65 72   row, then gener
28fa2 61 74 65 20 61 6e 20 65 78 74 72 61 20 72 6f 77  ate an extra row
28fa3 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 2a 2a   containing.  **
28fa4 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c   the names of al
28fa5 6c 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a  l columns..  */.
28fa6 20 20 69 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30    if( p->nRow==0
28fa7 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 6f 6c 75   ){.    p->nColu
28fa8 6d 6e 20 3d 20 6e 43 6f 6c 3b 0a 20 20 20 20 66  mn = nCol;.    f
28fa9 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
28faa 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20  i++){.      z = 
28fab 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
28fac 22 25 73 22 2c 20 63 6f 6c 76 5b 69 5d 29 3b 0a  "%s", colv[i]);.
28fad 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29        if( z==0 )
28fae 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69   goto malloc_fai
28faf 6c 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a  led;.      p->az
28fb0 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b  Result[p->nData+
28fb1 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20  +] = z;.    }.  
28fb2 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6e 43 6f  }else if( p->nCo
28fb3 6c 75 6d 6e 21 3d 6e 43 6f 6c 20 29 7b 0a 20 20  lumn!=nCol ){.  
28fb4 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
28fb5 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
28fb6 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c  p->zErrMsg = sql
28fb7 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
28fb8 20 20 20 20 20 22 73 71 6c 69 74 65 33 5f 67 65       "sqlite3_ge
28fb9 74 5f 74 61 62 6c 65 28 29 20 63 61 6c 6c 65 64  t_table() called
28fba 20 77 69 74 68 20 74 77 6f 20 6f 72 20 6d 6f 72   with two or mor
28fbb 65 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 71  e incompatible q
28fbc 75 65 72 69 65 73 22 0a 20 20 20 20 29 3b 0a 20  ueries".    );. 
28fbd 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
28fbe 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74  E_ERROR;.    ret
28fbf 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 1;.  }..  /*
28fc0 20 43 6f 70 79 20 6f 76 65 72 20 74 68 65 20 72   Copy over the r
28fc1 6f 77 20 64 61 74 61 0a 20 20 2a 2f 0a 20 20 69  ow data.  */.  i
28fc2 66 28 20 61 72 67 76 21 3d 30 20 29 7b 0a 20 20  f( argv!=0 ){.  
28fc3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
28fc4 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
28fc5 66 28 20 61 72 67 76 5b 69 5d 3d 3d 30 20 29 7b  f( argv[i]==0 ){
28fc6 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 30 3b 0a  .        z = 0;.
28fc7 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
28fc8 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c       int n = sql
28fc9 69 74 65 33 53 74 72 6c 65 6e 33 30 28 61 72 67  ite3Strlen30(arg
28fca 76 5b 69 5d 29 2b 31 3b 0a 20 20 20 20 20 20 20  v[i])+1;.       
28fcb 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c   z = sqlite3_mal
28fcc 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20  loc( n );.      
28fcd 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74    if( z==0 ) got
28fce 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b  o malloc_failed;
28fcf 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
28fd0 7a 2c 20 61 72 67 76 5b 69 5d 2c 20 6e 29 3b 0a  z, argv[i], n);.
28fd1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
28fd2 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61  >azResult[p->nDa
28fd3 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d  ta++] = z;.    }
28fd4 0a 20 20 20 20 70 2d 3e 6e 52 6f 77 2b 2b 3b 0a  .    p->nRow++;.
28fd5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
28fd6 0a 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3a 0a  .malloc_failed:.
28fd7 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
28fd8 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 65 74 75 72 6e  _NOMEM;.  return
28fd9 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65   1;.}../*.** Que
28fda 72 79 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ry the database.
28fdb 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66    But instead of
28fdc 20 69 6e 76 6f 6b 69 6e 67 20 61 20 63 61 6c 6c   invoking a call
28fdd 62 61 63 6b 20 66 6f 72 20 65 61 63 68 20 72 6f  back for each ro
28fde 77 2c 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66  w,.** malloc() f
28fdf 6f 72 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64  or space to hold
28fe0 20 74 68 65 20 72 65 73 75 6c 74 20 61 6e 64 20   the result and 
28fe1 72 65 74 75 72 6e 20 74 68 65 20 65 6e 74 69 72  return the entir
28fe2 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61 74 20  e results.** at 
28fe3 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f  the conclusion o
28fe4 66 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a  f the call..**.*
28fe5 2a 20 54 68 65 20 72 65 73 75 6c 74 20 74 68 61  * The result tha
28fe6 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  t is written to 
28fe7 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 20 69 73 20  ***pazResult is 
28fe8 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  held in memory o
28fe9 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
28fea 6d 61 6c 6c 6f 63 28 29 2e 20 20 42 75 74 20 74  malloc().  But t
28feb 68 65 20 63 61 6c 6c 65 72 20 63 61 6e 6e 6f 74  he caller cannot
28fec 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72   free this memor
28fed 79 20 64 69 72 65 63 74 6c 79 2e 20 20 0a 2a 2a  y directly.  .**
28fee 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 65 6e   Instead, the en
28fef 74 69 72 65 20 74 61 62 6c 65 20 73 68 6f 75 6c  tire table shoul
28ff0 64 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73  d be passed to s
28ff1 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
28ff2 65 28 29 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  e() when.** the 
28ff3 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72  calling procedur
28ff4 65 20 69 73 20 66 69 6e 69 73 68 65 64 20 75 73  e is finished us
28ff5 69 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54  ing it..*/.SQLIT
28ff6 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
28ff7 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 73  3_get_table(.  s
28ff8 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
28ff9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
28ffa 65 20 64 61 74 61 62 61 73 65 20 6f 6e 20 77 68  e database on wh
28ffb 69 63 68 20 74 68 65 20 53 51 4c 20 65 78 65 63  ich the SQL exec
28ffc 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  utes */.  const 
28ffd 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
28ffe 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c        /* The SQL
28fff 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20   to be executed 
29000 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a  */.  char ***paz
29001 52 65 73 75 6c 74 2c 20 20 20 20 20 20 20 20 20  Result,         
29002 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65   /* Write the re
29003 73 75 6c 74 20 74 61 62 6c 65 20 68 65 72 65 20  sult table here 
29004 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c  */.  int *pnRow,
29005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29006 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75   /* Write the nu
29007 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
29008 74 68 65 20 72 65 73 75 6c 74 20 68 65 72 65 20  the result here 
29009 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75  */.  int *pnColu
2900a 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mn,             
2900b 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75   /* Write the nu
2900c 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2900d 6f 66 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a  of result here *
2900e 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
2900f 4d 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Msg             
29010 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72 20 6d  /* Write error m
29011 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a  essages here */.
29012 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54  ){.  int rc;.  T
29013 61 62 52 65 73 75 6c 74 20 72 65 73 3b 0a 0a 20  abResult res;.. 
29014 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20 30 3b   *pazResult = 0;
29015 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20  .  if( pnColumn 
29016 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b  ) *pnColumn = 0;
29017 0a 20 20 69 66 28 20 70 6e 52 6f 77 20 29 20 2a  .  if( pnRow ) *
29018 70 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 69 66 28  pnRow = 0;.  if(
29019 20 70 7a 45 72 72 4d 73 67 20 29 20 2a 70 7a 45   pzErrMsg ) *pzE
2901a 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 72 65 73  rrMsg = 0;.  res
2901b 2e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  .zErrMsg = 0;.  
2901c 72 65 73 2e 6e 52 65 73 75 6c 74 20 3d 20 30 3b  res.nResult = 0;
2901d 0a 20 20 72 65 73 2e 6e 52 6f 77 20 3d 20 30 3b  .  res.nRow = 0;
2901e 0a 20 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e 20 3d  .  res.nColumn =
2901f 20 30 3b 0a 20 20 72 65 73 2e 6e 44 61 74 61 20   0;.  res.nData 
29020 3d 20 31 3b 0a 20 20 72 65 73 2e 6e 41 6c 6c 6f  = 1;.  res.nAllo
29021 63 20 3d 20 32 30 3b 0a 20 20 72 65 73 2e 72 63  c = 20;.  res.rc
29022 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
29023 72 65 73 2e 61 7a 52 65 73 75 6c 74 20 3d 20 73  res.azResult = s
29024 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
29025 7a 65 6f 66 28 63 68 61 72 2a 29 2a 72 65 73 2e  zeof(char*)*res.
29026 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 69 66 28 20  nAlloc );.  if( 
29027 72 65 73 2e 61 7a 52 65 73 75 6c 74 3d 3d 30 20  res.azResult==0 
29028 29 7b 0a 20 20 20 20 20 64 62 2d 3e 65 72 72 43  ){.     db->errC
29029 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  ode = SQLITE_NOM
2902a 45 4d 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20  EM;.     return 
2902b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2902c 7d 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74  }.  res.azResult
2902d 5b 30 5d 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20  [0] = 0;.  rc = 
2902e 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
2902f 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 5f 67   zSql, sqlite3_g
29030 65 74 5f 74 61 62 6c 65 5f 63 62 2c 20 26 72 65  et_table_cb, &re
29031 73 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20  s, pzErrMsg);.  
29032 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 72  assert( sizeof(r
29033 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3e  es.azResult[0])>
29034 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e 6e 44 61  = sizeof(res.nDa
29035 74 61 29 20 29 3b 0a 20 20 72 65 73 2e 61 7a 52  ta) );.  res.azR
29036 65 73 75 6c 74 5b 30 5d 20 3d 20 53 51 4c 49 54  esult[0] = SQLIT
29037 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 73  E_INT_TO_PTR(res
29038 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28 20 28  .nData);.  if( (
29039 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45  rc&0xff)==SQLITE
2903a 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 73 71  _ABORT ){.    sq
2903b 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
2903c 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31  (&res.azResult[1
2903d 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 2e  ]);.    if( res.
2903e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
2903f 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b   if( pzErrMsg ){
29040 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29041 5f 66 72 65 65 28 2a 70 7a 45 72 72 4d 73 67 29  _free(*pzErrMsg)
29042 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72  ;.        *pzErr
29043 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Msg = sqlite3_mp
29044 72 69 6e 74 66 28 22 25 73 22 2c 72 65 73 2e 7a  rintf("%s",res.z
29045 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d  ErrMsg);.      }
29046 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
29047 72 65 65 28 72 65 73 2e 7a 45 72 72 4d 73 67 29  ree(res.zErrMsg)
29048 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e  ;.    }.    db->
29049 65 72 72 43 6f 64 65 20 3d 20 72 65 73 2e 72 63  errCode = res.rc
2904a 3b 20 20 2f 2a 20 41 73 73 75 6d 65 20 33 32 2d  ;  /* Assume 32-
2904b 62 69 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 69  bit assignment i
2904c 73 20 61 74 6f 6d 69 63 20 2a 2f 0a 20 20 20 20  s atomic */.    
2904d 72 65 74 75 72 6e 20 72 65 73 2e 72 63 3b 0a 20  return res.rc;. 
2904e 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
2904f 65 28 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a  e(res.zErrMsg);.
29050 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
29051 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
29052 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72  e3_free_table(&r
29053 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b  es.azResult[1]);
29054 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
29055 20 20 7d 0a 20 20 69 66 28 20 72 65 73 2e 6e 41    }.  if( res.nA
29056 6c 6c 6f 63 3e 72 65 73 2e 6e 44 61 74 61 20 29  lloc>res.nData )
29057 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e  {.    char **azN
29058 65 77 3b 0a 20 20 20 20 61 7a 4e 65 77 20 3d 20  ew;.    azNew = 
29059 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
2905a 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 2c 20 73   res.azResult, s
2905b 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 28 72 65  izeof(char*)*(re
2905c 73 2e 6e 44 61 74 61 2b 31 29 20 29 3b 0a 20 20  s.nData+1) );.  
2905d 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29    if( azNew==0 )
2905e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
2905f 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73 2e  free_table(&res.
29060 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20  azResult[1]);.  
29061 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20      db->errCode 
29062 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
29063 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
29064 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
29065 0a 20 20 20 20 72 65 73 2e 6e 41 6c 6c 6f 63 20  .    res.nAlloc 
29066 3d 20 72 65 73 2e 6e 44 61 74 61 2b 31 3b 0a 20  = res.nData+1;. 
29067 20 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 20     res.azResult 
29068 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a 20 20 2a  = azNew;.  }.  *
29069 70 61 7a 52 65 73 75 6c 74 20 3d 20 26 72 65 73  pazResult = &res
2906a 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 3b 0a 20 20  .azResult[1];.  
2906b 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20 29 20 2a  if( pnColumn ) *
2906c 70 6e 43 6f 6c 75 6d 6e 20 3d 20 72 65 73 2e 6e  pnColumn = res.n
2906d 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 70 6e  Column;.  if( pn
2906e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20 72  Row ) *pnRow = r
2906f 65 73 2e 6e 52 6f 77 3b 0a 20 20 72 65 74 75 72  es.nRow;.  retur
29070 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
29071 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 65 65  his routine free
29072 73 20 74 68 65 20 73 70 61 63 65 20 74 68 65 20  s the space the 
29073 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
29074 65 28 29 20 6d 61 6c 6c 6f 63 65 64 2e 0a 2a 2f  e() malloced..*/
29075 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
29076 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
29077 62 6c 65 28 0a 20 20 63 68 61 72 20 2a 2a 61 7a  ble(.  char **az
29078 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20 20  Result          
29079 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65 74 75    /* Result retu
2907a 72 6e 65 64 20 66 72 6f 6d 20 66 72 6f 6d 20 73  rned from from s
2907b 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
2907c 28 29 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61  () */.){.  if( a
2907d 7a 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20 69  zResult ){.    i
2907e 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20 61 7a 52  nt i, n;.    azR
2907f 65 73 75 6c 74 2d 2d 3b 0a 20 20 20 20 61 73 73  esult--;.    ass
29080 65 72 74 28 20 61 7a 52 65 73 75 6c 74 21 3d 30  ert( azResult!=0
29081 20 29 3b 0a 20 20 20 20 6e 20 3d 20 53 51 4c 49   );.    n = SQLI
29082 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 61 7a  TE_PTR_TO_INT(az
29083 52 65 73 75 6c 74 5b 30 5d 29 3b 0a 20 20 20 20  Result[0]);.    
29084 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 3b 20 69 2b  for(i=1; i<n; i+
29085 2b 29 7b 20 69 66 28 20 61 7a 52 65 73 75 6c 74  +){ if( azResult
29086 5b 69 5d 20 29 20 73 71 6c 69 74 65 33 5f 66 72  [i] ) sqlite3_fr
29087 65 65 28 61 7a 52 65 73 75 6c 74 5b 69 5d 29 3b  ee(azResult[i]);
29088 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
29089 72 65 65 28 61 7a 52 65 73 75 6c 74 29 3b 0a 20  ree(azResult);. 
2908a 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20   }.}..#endif /* 
2908b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f  SQLITE_OMIT_GET_
2908c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  TABLE */../*****
2908d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2908e 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   table.c *******
2908f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29091 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
29092 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
29093 66 69 6c 65 20 74 72 69 67 67 65 72 2e 63 20 2a  file trigger.c *
29094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29095 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29096 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 0a  ********/./*.**.
29097 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
29098 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
29099 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2909a 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2909b 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2909c 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2909d 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2909e 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2909f 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
290a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
290a1 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
290a2 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
290a3 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
290a4 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
290a5 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
290a6 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
290a7 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
290a8 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
290a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
290aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
290ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
290ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
290ad 2a 0a 2a 2a 20 24 49 64 3a 20 74 72 69 67 67 65  *.** $Id: trigge
290ae 72 2e 63 2c 76 20 31 2e 31 33 35 20 32 30 30 39  r.c,v 1.135 2009
290af 2f 30 32 2f 32 38 20 31 30 3a 34 37 3a 34 32 20  /02/28 10:47:42 
290b0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
290b1 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  $.*/..#ifndef SQ
290b2 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
290b3 52 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  R./*.** Delete a
290b4 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
290b5 54 72 69 67 67 65 72 53 74 65 70 20 73 74 72 75  TriggerStep stru
290b6 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ctures..*/.SQLIT
290b7 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
290b8 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
290b9 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 20  gerStep(sqlite3 
290ba 2a 64 62 2c 20 54 72 69 67 67 65 72 53 74 65 70  *db, TriggerStep
290bb 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 29 7b   *pTriggerStep){
290bc 0a 20 20 77 68 69 6c 65 28 20 70 54 72 69 67 67  .  while( pTrigg
290bd 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 54 72  erStep ){.    Tr
290be 69 67 67 65 72 53 74 65 70 20 2a 20 70 54 6d 70  iggerStep * pTmp
290bf 20 3d 20 70 54 72 69 67 67 65 72 53 74 65 70 3b   = pTriggerStep;
290c0 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
290c1 70 20 3d 20 70 54 72 69 67 67 65 72 53 74 65 70  p = pTriggerStep
290c2 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 20 20 69 66  ->pNext;..    if
290c3 28 20 70 54 6d 70 2d 3e 74 61 72 67 65 74 2e 64  ( pTmp->target.d
290c4 79 6e 20 29 20 73 71 6c 69 74 65 33 44 62 46 72  yn ) sqlite3DbFr
290c5 65 65 28 64 62 2c 20 28 63 68 61 72 2a 29 70 54  ee(db, (char*)pT
290c6 6d 70 2d 3e 74 61 72 67 65 74 2e 7a 29 3b 0a 20  mp->target.z);. 
290c7 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
290c8 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70  lete(db, pTmp->p
290c9 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69  Where);.    sqli
290ca 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
290cb 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 45 78 70  e(db, pTmp->pExp
290cc 72 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  rList);.    sqli
290cd 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
290ce 64 62 2c 20 70 54 6d 70 2d 3e 70 53 65 6c 65 63  db, pTmp->pSelec
290cf 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  t);.    sqlite3I
290d0 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
290d1 70 54 6d 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a  pTmp->pIdList);.
290d2 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
290d3 65 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20  ee(db, pTmp);.  
290d4 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  }.}../*.** Given
290d5 20 74 61 62 6c 65 20 70 54 61 62 2c 20 72 65 74   table pTab, ret
290d6 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c  urn a list of al
290d7 6c 20 74 68 65 20 74 72 69 67 67 65 72 73 20 61  l the triggers a
290d8 74 74 61 63 68 65 64 20 74 6f 20 0a 2a 2a 20 74  ttached to .** t
290d9 68 65 20 74 61 62 6c 65 2e 20 54 68 65 20 6c 69  he table. The li
290da 73 74 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20  st is connected 
290db 62 79 20 54 72 69 67 67 65 72 2e 70 4e 65 78 74  by Trigger.pNext
290dc 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 53 51   pointers..*/.SQ
290dd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69  LITE_PRIVATE Tri
290de 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69  gger *sqlite3Tri
290df 67 67 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a  ggerList(Parse *
290e0 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
290e1 54 61 62 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a  Tab){.  Schema *
290e2 20 63 6f 6e 73 74 20 70 54 6d 70 53 63 68 65 6d   const pTmpSchem
290e3 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  a = pParse->db->
290e4 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 0a  aDb[1].pSchema;.
290e5 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69 73 74    Trigger *pList
290e6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
290e7 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
290e8 66 20 74 72 69 67 67 65 72 73 20 74 6f 20 72 65  f triggers to re
290e9 74 75 72 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70  turn */..  if( p
290ea 54 6d 70 53 63 68 65 6d 61 21 3d 70 54 61 62 2d  TmpSchema!=pTab-
290eb 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  >pSchema ){.    
290ec 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20  HashElem *p;.   
290ed 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73   for(p=sqliteHas
290ee 68 46 69 72 73 74 28 26 70 54 6d 70 53 63 68 65  hFirst(&pTmpSche
290ef 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 3b 20 70  ma->trigHash); p
290f0 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; p=sqliteHashNe
290f1 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 54 72  xt(p)){.      Tr
290f2 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d 20 28  igger *pTrig = (
290f3 54 72 69 67 67 65 72 20 2a 29 73 71 6c 69 74 65  Trigger *)sqlite
290f4 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20  HashData(p);.   
290f5 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 54     if( pTrig->pT
290f6 61 62 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  abSchema==pTab->
290f7 70 53 63 68 65 6d 61 0a 20 20 20 20 20 20 20 26  pSchema.       &
290f8 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49  & 0==sqlite3StrI
290f9 43 6d 70 28 70 54 72 69 67 2d 3e 74 61 62 6c 65  Cmp(pTrig->table
290fa 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 0a  , pTab->zName) .
290fb 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
290fc 20 70 54 72 69 67 2d 3e 70 4e 65 78 74 20 3d 20   pTrig->pNext = 
290fd 28 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 20 3a  (pList ? pList :
290fe 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 29   pTab->pTrigger)
290ff 3b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 20  ;.        pList 
29100 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 20 20 7d  = pTrig;.      }
29101 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
29102 74 75 72 6e 20 28 70 4c 69 73 74 20 3f 20 70 4c  turn (pList ? pL
29103 69 73 74 20 3a 20 70 54 61 62 2d 3e 70 54 72 69  ist : pTab->pTri
29104 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  gger);.}../*.** 
29105 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 62  This is called b
29106 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68 65  y the parser whe
29107 6e 20 69 74 20 73 65 65 73 20 61 20 43 52 45 41  n it sees a CREA
29108 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
29109 6d 65 6e 74 0a 2a 2a 20 75 70 20 74 6f 20 74 68  ment.** up to th
2910a 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 65 20 42  e point of the B
2910b 45 47 49 4e 20 62 65 66 6f 72 65 20 74 68 65 20  EGIN before the 
2910c 74 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73 2e  trigger actions.
2910d 20 20 41 20 54 72 69 67 67 65 72 0a 2a 2a 20 73    A Trigger.** s
2910e 74 72 75 63 74 75 72 65 20 69 73 20 67 65 6e 65  tructure is gene
2910f 72 61 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74  rated based on t
29110 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  he information a
29111 76 61 69 6c 61 62 6c 65 20 61 6e 64 20 73 74 6f  vailable and sto
29112 72 65 64 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65  red.** in pParse
29113 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 2e 20 20  ->pNewTrigger.  
29114 41 66 74 65 72 20 74 68 65 20 74 72 69 67 67 65  After the trigge
29115 72 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62  r actions have b
29116 65 65 6e 20 70 61 72 73 65 64 2c 20 74 68 65 0a  een parsed, the.
29117 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  ** sqlite3Finish
29118 54 72 69 67 67 65 72 28 29 20 66 75 6e 63 74 69  Trigger() functi
29119 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
2911a 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 74 72 69  complete the tri
2911b 67 67 65 72 0a 2a 2a 20 63 6f 6e 73 74 72 75 63  gger.** construc
2911c 74 69 6f 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2f  tion process..*/
2911d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2911e 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
2911f 6e 54 72 69 67 67 65 72 28 0a 20 20 50 61 72 73  nTrigger(.  Pars
29120 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
29121 2f 2a 20 54 68 65 20 70 61 72 73 65 20 63 6f 6e  /* The parse con
29122 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41  text of the CREA
29123 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
29124 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  ment */.  Token 
29125 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a  *pName1,      /*
29126 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
29127 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 54 6f   trigger */.  To
29128 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20  ken *pName2,    
29129 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66    /* The name of
2912a 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a   the trigger */.
2912b 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20    int tr_tm,    
2912c 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
2912d 54 4b 5f 42 45 46 4f 52 45 2c 20 54 4b 5f 41 46  TK_BEFORE, TK_AF
2912e 54 45 52 2c 20 54 4b 5f 49 4e 53 54 45 41 44 20  TER, TK_INSTEAD 
2912f 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
29130 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
29131 6f 66 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b  of TK_INSERT, TK
29132 5f 55 50 44 41 54 45 2c 20 54 4b 5f 44 45 4c 45  _UPDATE, TK_DELE
29133 54 45 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a  TE */.  IdList *
29134 70 43 6f 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20 63  pColumns,   /* c
29135 6f 6c 75 6d 6e 20 6c 69 73 74 20 69 66 20 74 68  olumn list if th
29136 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45 20  is is an UPDATE 
29137 4f 46 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  OF trigger */.  
29138 53 72 63 4c 69 73 74 20 2a 70 54 61 62 6c 65 4e  SrcList *pTableN
29139 61 6d 65 2c 2f 2a 20 54 68 65 20 6e 61 6d 65 20  ame,/* The name 
2913a 6f 66 20 74 68 65 20 74 61 62 6c 65 2f 76 69 65  of the table/vie
2913b 77 20 74 68 65 20 74 72 69 67 67 65 72 20 61 70  w the trigger ap
2913c 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 45 78  plies to */.  Ex
2913d 70 72 20 2a 70 57 68 65 6e 2c 20 20 20 20 20 20  pr *pWhen,      
2913e 20 20 2f 2a 20 57 48 45 4e 20 63 6c 61 75 73 65    /* WHEN clause
2913f 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70   */.  int isTemp
29140 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ,         /* Tru
29141 65 20 69 66 20 74 68 65 20 54 45 4d 50 4f 52 41  e if the TEMPORA
29142 52 59 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72  RY keyword is pr
29143 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  esent */.  int n
29144 6f 45 72 72 20 20 20 20 20 20 20 20 20 20 20 2f  oErr           /
29145 2a 20 53 75 70 70 72 65 73 73 20 65 72 72 6f 72  * Suppress error
29146 73 20 69 66 20 74 68 65 20 74 72 69 67 67 65 72  s if the trigger
29147 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
29148 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 20  */.){.  Trigger 
29149 2a 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20  *pTrigger = 0;. 
2914a 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
2914b 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b  char *zName = 0;
2914c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2914d 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 2a  of the trigger *
2914e 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2914f 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
29150 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
29151 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
29152 61 74 61 62 61 73 65 20 74 6f 20 73 74 6f 72 65  atabase to store
29153 20 74 68 65 20 74 72 69 67 67 65 72 20 69 6e 20   the trigger in 
29154 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
29155 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
29156 54 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  The unqualified 
29157 64 62 20 6e 61 6d 65 20 2a 2f 0a 20 20 44 62 46  db name */.  DbF
29158 69 78 65 72 20 73 46 69 78 3b 0a 20 20 69 6e 74  ixer sFix;.  int
29159 20 69 54 61 62 44 62 3b 0a 0a 20 20 61 73 73 65   iTabDb;..  asse
2915a 72 74 28 20 70 4e 61 6d 65 31 21 3d 30 20 29 3b  rt( pName1!=0 );
2915b 20 20 20 2f 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20     /* pName1->z 
2915c 6d 69 67 68 74 20 62 65 20 4e 55 4c 4c 2c 20 62  might be NULL, b
2915d 75 74 20 6e 6f 74 20 70 4e 61 6d 65 31 20 69 74  ut not pName1 it
2915e 73 65 6c 66 20 2a 2f 0a 20 20 61 73 73 65 72 74  self */.  assert
2915f 28 20 70 4e 61 6d 65 32 21 3d 30 20 29 3b 0a 20  ( pName2!=0 );. 
29160 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f   assert( op==TK_
29161 49 4e 53 45 52 54 20 7c 7c 20 6f 70 3d 3d 54 4b  INSERT || op==TK
29162 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54  _UPDATE || op==T
29163 4b 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 61 73  K_DELETE );.  as
29164 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70  sert( op>0 && op
29165 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28 20 69  <0xff );.  if( i
29166 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20  sTemp ){.    /* 
29167 49 66 20 54 45 4d 50 20 77 61 73 20 73 70 65 63  If TEMP was spec
29168 69 66 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20  ified, then the 
29169 74 72 69 67 67 65 72 20 6e 61 6d 65 20 6d 61 79  trigger name may
2916a 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65   not be qualifie
2916b 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4e  d. */.    if( pN
2916c 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20  ame2->n>0 ){.   
2916d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2916e 73 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70  sg(pParse, "temp
2916f 6f 72 61 72 79 20 74 72 69 67 67 65 72 20 6d 61  orary trigger ma
29170 79 20 6e 6f 74 20 68 61 76 65 20 71 75 61 6c 69  y not have quali
29171 66 69 65 64 20 6e 61 6d 65 22 29 3b 0a 20 20 20  fied name");.   
29172 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
29173 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20  cleanup;.    }. 
29174 20 20 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20     iDb = 1;.    
29175 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 31 3b 0a  pName = pName1;.
29176 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
29177 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 64  Figure out the d
29178 62 20 74 68 61 74 20 74 68 65 20 74 68 65 20 74  b that the the t
29179 72 69 67 67 65 72 20 77 69 6c 6c 20 62 65 20 63  rigger will be c
2917a 72 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 20  reated in */.   
2917b 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77   iDb = sqlite3Tw
2917c 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65  oPartName(pParse
2917d 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
2917e 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69  , &pName);.    i
2917f 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20  f( iDb<0 ){.    
29180 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
29181 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
29182 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74  }..  /* If the t
29183 72 69 67 67 65 72 20 6e 61 6d 65 20 77 61 73 20  rigger name was 
29184 75 6e 71 75 61 6c 69 66 69 65 64 2c 20 61 6e 64  unqualified, and
29185 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20   the table is a 
29186 74 65 6d 70 20 74 61 62 6c 65 2c 0a 20 20 2a 2a  temp table,.  **
29187 20 74 68 65 6e 20 73 65 74 20 69 44 62 20 74 6f   then set iDb to
29188 20 31 20 74 6f 20 63 72 65 61 74 65 20 74 68 65   1 to create the
29189 20 74 72 69 67 67 65 72 20 69 6e 20 74 68 65 20   trigger in the 
2918a 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
2918b 73 65 2e 0a 20 20 2a 2a 20 49 66 20 73 71 6c 69  se..  ** If sqli
2918c 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70  te3SrcListLookup
2918d 28 29 20 72 65 74 75 72 6e 73 20 30 2c 20 69 6e  () returns 0, in
2918e 64 69 63 61 74 69 6e 67 20 74 68 65 20 74 61 62  dicating the tab
2918f 6c 65 20 64 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a  le does not.  **
29190 20 65 78 69 73 74 2c 20 74 68 65 20 65 72 72 6f   exist, the erro
29191 72 20 69 73 20 63 61 75 67 68 74 20 62 79 20 74  r is caught by t
29192 68 65 20 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a  he block below..
29193 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62    */.  if( !pTab
29194 6c 65 4e 61 6d 65 20 7c 7c 20 64 62 2d 3e 6d 61  leName || db->ma
29195 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
29196 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
29197 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54  leanup;.  }.  pT
29198 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  ab = sqlite3SrcL
29199 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  istLookup(pParse
2919a 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  , pTableName);. 
2919b 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d   if( pName2->n==
2919c 30 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61  0 && pTab && pTa
2919d 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e  b->pSchema==db->
2919e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
2919f 7b 0a 20 20 20 20 69 44 62 20 3d 20 31 3b 0a 20  {.    iDb = 1;. 
291a0 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20   }..  /* Ensure 
291a1 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d  the table name m
291a2 61 74 63 68 65 73 20 64 61 74 61 62 61 73 65 20  atches database 
291a3 6e 61 6d 65 20 61 6e 64 20 74 68 61 74 20 74 68  name and that th
291a4 65 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a  e table exists *
291a5 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  /.  if( db->mall
291a6 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20  ocFailed ) goto 
291a7 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
291a8 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
291a9 65 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29  eName->nSrc==1 )
291aa 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46  ;.  if( sqlite3F
291ab 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50  ixInit(&sFix, pP
291ac 61 72 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67  arse, iDb, "trig
291ad 67 65 72 22 2c 20 70 4e 61 6d 65 29 20 26 26 20  ger", pName) && 
291ae 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 69  .      sqlite3Fi
291af 78 53 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20  xSrcList(&sFix, 
291b0 70 54 61 62 6c 65 4e 61 6d 65 29 20 29 7b 0a 20  pTableName) ){. 
291b1 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
291b2 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70  cleanup;.  }.  p
291b3 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
291b4 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
291b5 65 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a  e, pTableName);.
291b6 20 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20    if( !pTab ){. 
291b7 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
291b8 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20  does not exist. 
291b9 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  */.    goto trig
291ba 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
291bb 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
291bc 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71  (pTab) ){.    sq
291bd 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
291be 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72  arse, "cannot cr
291bf 65 61 74 65 20 74 72 69 67 67 65 72 73 20 6f 6e  eate triggers on
291c0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 22   virtual tables"
291c1 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  );.    goto trig
291c2 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
291c3 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
291c4 74 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61  t the trigger na
291c5 6d 65 20 69 73 20 6e 6f 74 20 72 65 73 65 72 76  me is not reserv
291c6 65 64 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 74  ed and that no t
291c7 72 69 67 67 65 72 20 6f 66 20 74 68 65 0a 20 20  rigger of the.  
291c8 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e 61 6d  ** specified nam
291c9 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 7a 4e  e exists */.  zN
291ca 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
291cb 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
291cc 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 4e  Name);.  if( !zN
291cd 61 6d 65 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b  ame || SQLITE_OK
291ce 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62  !=sqlite3CheckOb
291cf 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  jectName(pParse,
291d0 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67   zName) ){.    g
291d1 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
291d2 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  nup;.  }.  if( s
291d3 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
291d4 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53  (db->aDb[iDb].pS
291d5 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29  chema->trigHash)
291d6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
291d7 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 73          zName, s
291d8 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
291d9 4e 61 6d 65 29 29 20 29 7b 0a 20 20 20 20 69 66  Name)) ){.    if
291da 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20  ( !noErr ){.    
291db 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
291dc 67 28 70 50 61 72 73 65 2c 20 22 74 72 69 67 67  g(pParse, "trigg
291dd 65 72 20 25 54 20 61 6c 72 65 61 64 79 20 65 78  er %T already ex
291de 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20  ists", pName);. 
291df 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 74 72     }.    goto tr
291e0 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
291e1 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20   }..  /* Do not 
291e2 63 72 65 61 74 65 20 61 20 74 72 69 67 67 65 72  create a trigger
291e3 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 61 62   on a system tab
291e4 6c 65 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  le */.  if( sqli
291e5 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62  te3StrNICmp(pTab
291e6 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  ->zName, "sqlite
291e7 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20  _", 7)==0 ){.   
291e8 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
291e9 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
291ea 20 63 72 65 61 74 65 20 74 72 69 67 67 65 72 20   create trigger 
291eb 6f 6e 20 73 79 73 74 65 6d 20 74 61 62 6c 65 22  on system table"
291ec 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  );.    pParse->n
291ed 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20  Err++;.    goto 
291ee 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
291ef 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 4e 53 54 45  .  }..  /* INSTE
291f0 41 44 20 6f 66 20 74 72 69 67 67 65 72 73 20 61  AD of triggers a
291f1 72 65 20 6f 6e 6c 79 20 66 6f 72 20 76 69 65 77  re only for view
291f2 73 20 61 6e 64 20 76 69 65 77 73 20 6f 6e 6c 79  s and views only
291f3 20 73 75 70 70 6f 72 74 20 49 4e 53 54 45 41 44   support INSTEAD
291f4 0a 20 20 2a 2a 20 6f 66 20 74 72 69 67 67 65 72  .  ** of trigger
291f5 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  s..  */.  if( pT
291f6 61 62 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 74  ab->pSelect && t
291f7 72 5f 74 6d 21 3d 54 4b 5f 49 4e 53 54 45 41 44  r_tm!=TK_INSTEAD
291f8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
291f9 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
291fa 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 25  "cannot create %
291fb 73 20 74 72 69 67 67 65 72 20 6f 6e 20 76 69 65  s trigger on vie
291fc 77 3a 20 25 53 22 2c 20 0a 20 20 20 20 20 20 20  w: %S", .       
291fd 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f 42 45   (tr_tm == TK_BE
291fe 46 4f 52 45 29 3f 22 42 45 46 4f 52 45 22 3a 22  FORE)?"BEFORE":"
291ff 41 46 54 45 52 22 2c 20 70 54 61 62 6c 65 4e 61  AFTER", pTableNa
29200 6d 65 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f  me, 0);.    goto
29201 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
29202 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 54 61  ;.  }.  if( !pTa
29203 62 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 74 72  b->pSelect && tr
29204 5f 74 6d 3d 3d 54 4b 5f 49 4e 53 54 45 41 44 20  _tm==TK_INSTEAD 
29205 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
29206 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
29207 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 49 4e  cannot create IN
29208 53 54 45 41 44 20 4f 46 22 0a 20 20 20 20 20 20  STEAD OF".      
29209 20 20 22 20 74 72 69 67 67 65 72 20 6f 6e 20 74    " trigger on t
2920a 61 62 6c 65 3a 20 25 53 22 2c 20 70 54 61 62 6c  able: %S", pTabl
2920b 65 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 67  eName, 0);.    g
2920c 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2920d 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 54 61 62 44  nup;.  }.  iTabD
2920e 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2920f 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
29210 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69  b->pSchema);..#i
29211 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29212 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
29213 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65    {.    int code
29214 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
29215 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 63 6f  _TRIGGER;.    co
29216 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
29217 64 62 2d 3e 61 44 62 5b 69 54 61 62 44 62 5d 2e  db->aDb[iTabDb].
29218 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74  zName;.    const
29219 20 63 68 61 72 20 2a 7a 44 62 54 72 69 67 20 3d   char *zDbTrig =
2921a 20 69 73 54 65 6d 70 20 3f 20 64 62 2d 3e 61 44   isTemp ? db->aD
2921b 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3a 20 7a 44 62  b[1].zName : zDb
2921c 3b 0a 20 20 20 20 69 66 28 20 69 54 61 62 44 62  ;.    if( iTabDb
2921d 3d 3d 31 20 7c 7c 20 69 73 54 65 6d 70 20 29 20  ==1 || isTemp ) 
2921e 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
2921f 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47 45  EATE_TEMP_TRIGGE
29220 52 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  R;.    if( sqlit
29221 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
29222 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c  se, code, zName,
29223 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44   pTab->zName, zD
29224 62 54 72 69 67 29 20 29 7b 0a 20 20 20 20 20 20  bTrig) ){.      
29225 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
29226 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20  anup;.    }.    
29227 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
29228 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
29229 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45  ITE_INSERT, SCHE
2922a 4d 41 5f 54 41 42 4c 45 28 69 54 61 62 44 62 29  MA_TABLE(iTabDb)
2922b 2c 30 2c 7a 44 62 29 29 7b 0a 20 20 20 20 20 20  ,0,zDb)){.      
2922c 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
2922d 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  anup;.    }.  }.
2922e 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 4e 53  #endif..  /* INS
2922f 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 73  TEAD OF triggers
29230 20 63 61 6e 20 6f 6e 6c 79 20 61 70 70 65 61 72   can only appear
29231 20 6f 6e 20 76 69 65 77 73 20 61 6e 64 20 42 45   on views and BE
29232 46 4f 52 45 20 74 72 69 67 67 65 72 73 0a 20 20  FORE triggers.  
29233 2a 2a 20 63 61 6e 6e 6f 74 20 61 70 70 65 61 72  ** cannot appear
29234 20 6f 6e 20 76 69 65 77 73 2e 20 20 53 6f 20 77   on views.  So w
29235 65 20 6d 69 67 68 74 20 61 73 20 77 65 6c 6c 20  e might as well 
29236 74 72 61 6e 73 6c 61 74 65 20 65 76 65 72 79 0a  translate every.
29237 20 20 2a 2a 20 49 4e 53 54 45 41 44 20 4f 46 20    ** INSTEAD OF 
29238 74 72 69 67 67 65 72 20 69 6e 74 6f 20 61 20 42  trigger into a B
29239 45 46 4f 52 45 20 74 72 69 67 67 65 72 2e 20 20  EFORE trigger.  
2923a 49 74 20 73 69 6d 70 6c 69 66 69 65 73 20 63 6f  It simplifies co
2923b 64 65 0a 20 20 2a 2a 20 65 6c 73 65 77 68 65 72  de.  ** elsewher
2923c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 20 28 74 72  e..  */.  if (tr
2923d 5f 74 6d 20 3d 3d 20 54 4b 5f 49 4e 53 54 45 41  _tm == TK_INSTEA
2923e 44 29 7b 0a 20 20 20 20 74 72 5f 74 6d 20 3d 20  D){.    tr_tm = 
2923f 54 4b 5f 42 45 46 4f 52 45 3b 0a 20 20 7d 0a 0a  TK_BEFORE;.  }..
29240 20 20 2f 2a 20 42 75 69 6c 64 20 74 68 65 20 54    /* Build the T
29241 72 69 67 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f  rigger object */
29242 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 28 54  .  pTrigger = (T
29243 72 69 67 67 65 72 2a 29 73 71 6c 69 74 65 33 44  rigger*)sqlite3D
29244 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
29245 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 29 29  sizeof(Trigger))
29246 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
29247 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69 67 67  ==0 ) goto trigg
29248 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70 54  er_cleanup;.  pT
29249 72 69 67 67 65 72 2d 3e 6e 61 6d 65 20 3d 20 7a  rigger->name = z
2924a 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  Name;.  zName = 
2924b 30 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 74  0;.  pTrigger->t
2924c 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62  able = sqlite3Db
2924d 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c  StrDup(db, pTabl
2924e 65 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  eName->a[0].zNam
2924f 65 29 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e  e);.  pTrigger->
29250 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44  pSchema = db->aD
29251 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a  b[iDb].pSchema;.
29252 20 20 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62    pTrigger->pTab
29253 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70  Schema = pTab->p
29254 53 63 68 65 6d 61 3b 0a 20 20 70 54 72 69 67 67  Schema;.  pTrigg
29255 65 72 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b  er->op = (u8)op;
29256 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 74 72 5f  .  pTrigger->tr_
29257 74 6d 20 3d 20 74 72 5f 74 6d 3d 3d 54 4b 5f 42  tm = tr_tm==TK_B
29258 45 46 4f 52 45 20 3f 20 54 52 49 47 47 45 52 5f  EFORE ? TRIGGER_
29259 42 45 46 4f 52 45 20 3a 20 54 52 49 47 47 45 52  BEFORE : TRIGGER
2925a 5f 41 46 54 45 52 3b 0a 20 20 70 54 72 69 67 67  _AFTER;.  pTrigg
2925b 65 72 2d 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69  er->pWhen = sqli
2925c 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2925d 57 68 65 6e 2c 20 45 58 50 52 44 55 50 5f 52 45  When, EXPRDUP_RE
2925e 44 55 43 45 29 3b 0a 20 20 70 54 72 69 67 67 65  DUCE);.  pTrigge
2925f 72 2d 3e 70 43 6f 6c 75 6d 6e 73 20 3d 20 73 71  r->pColumns = sq
29260 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 64  lite3IdListDup(d
29261 62 2c 20 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20  b, pColumns);.  
29262 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79  sqlite3TokenCopy
29263 28 64 62 2c 20 26 70 54 72 69 67 67 65 72 2d 3e  (db, &pTrigger->
29264 6e 61 6d 65 54 6f 6b 65 6e 2c 70 4e 61 6d 65 29  nameToken,pName)
29265 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
29266 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3d  se->pNewTrigger=
29267 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  =0 );.  pParse->
29268 70 4e 65 77 54 72 69 67 67 65 72 20 3d 20 70 54  pNewTrigger = pT
29269 72 69 67 67 65 72 3b 0a 0a 74 72 69 67 67 65 72  rigger;..trigger
2926a 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69  _cleanup:.  sqli
2926b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
2926c 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ame);.  sqlite3S
2926d 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
2926e 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20   pTableName);.  
2926f 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
29270 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 73  ete(db, pColumns
29271 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
29272 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 6e  Delete(db, pWhen
29273 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  );.  if( !pParse
29274 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 29 7b  ->pNewTrigger ){
29275 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
29276 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 54  teTrigger(db, pT
29277 72 69 67 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65  rigger);.  }else
29278 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
29279 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
2927a 72 3d 3d 70 54 72 69 67 67 65 72 20 29 3b 0a 20  r==pTrigger );. 
2927b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
2927c 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2927d 65 64 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20  ed after all of 
2927e 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74 69  the trigger acti
2927f 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70 61  ons have been pa
29280 72 73 65 64 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  rsed.** in order
29281 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
29282 20 70 72 6f 63 65 73 73 20 6f 66 20 62 75 69 6c   process of buil
29283 64 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72  ding the trigger
29284 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
29285 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
29286 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 0a 20  FinishTrigger(. 
29287 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
29288 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
29289 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
2928a 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
2928b 65 70 4c 69 73 74 2c 20 2f 2a 20 54 68 65 20 74  epList, /* The t
2928c 72 69 67 67 65 72 65 64 20 70 72 6f 67 72 61 6d  riggered program
2928d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 6c   */.  Token *pAl
2928e 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l             /*
2928f 20 54 6f 6b 65 6e 20 74 68 61 74 20 64 65 73 63   Token that desc
29290 72 69 62 65 73 20 74 68 65 20 63 6f 6d 70 6c 65  ribes the comple
29291 74 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45  te CREATE TRIGGE
29292 52 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65  R */.){.  Trigge
29293 72 20 2a 70 54 72 69 67 20 3d 20 70 50 61 72 73  r *pTrig = pPars
29294 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3b 20  e->pNewTrigger; 
29295 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 62 65     /* Trigger be
29296 69 6e 67 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a  ing finished */.
29297 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
29298 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29299 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
2929a 6d 65 20 6f 66 20 74 72 69 67 67 65 72 20 2a 2f  me of trigger */
2929b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2929c 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20   pParse->db;    
2929d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2929e 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
2929f 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a 20   DbFixer sFix;. 
292a0 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
292a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292a2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
292a3 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
292a4 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a   the trigger */.
292a5 0a 20 20 70 54 72 69 67 20 3d 20 70 50 61 72 73  .  pTrig = pPars
292a6 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3b 0a  e->pNewTrigger;.
292a7 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72    pParse->pNewTr
292a8 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 66 28  igger = 0;.  if(
292a9 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
292aa 20 21 70 54 72 69 67 20 29 20 67 6f 74 6f 20 74   !pTrig ) goto t
292ab 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65  riggerfinish_cle
292ac 61 6e 75 70 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  anup;.  zName = 
292ad 70 54 72 69 67 2d 3e 6e 61 6d 65 3b 0a 20 20 69  pTrig->name;.  i
292ae 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
292af 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
292b0 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 63  ->db, pTrig->pSc
292b1 68 65 6d 61 29 3b 0a 20 20 70 54 72 69 67 2d 3e  hema);.  pTrig->
292b2 73 74 65 70 5f 6c 69 73 74 20 3d 20 70 53 74 65  step_list = pSte
292b3 70 4c 69 73 74 3b 0a 20 20 77 68 69 6c 65 28 20  pList;.  while( 
292b4 70 53 74 65 70 4c 69 73 74 20 29 7b 0a 20 20 20  pStepList ){.   
292b5 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 54 72 69   pStepList->pTri
292b6 67 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 70  g = pTrig;.    p
292b7 53 74 65 70 4c 69 73 74 20 3d 20 70 53 74 65 70  StepList = pStep
292b8 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  List->pNext;.  }
292b9 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
292ba 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61  xInit(&sFix, pPa
292bb 72 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67 67  rse, iDb, "trigg
292bc 65 72 22 2c 20 26 70 54 72 69 67 2d 3e 6e 61 6d  er", &pTrig->nam
292bd 65 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20 20  eToken) .       
292be 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69 78     && sqlite3Fix
292bf 54 72 69 67 67 65 72 53 74 65 70 28 26 73 46 69  TriggerStep(&sFi
292c0 78 2c 20 70 54 72 69 67 2d 3e 73 74 65 70 5f 6c  x, pTrig->step_l
292c1 69 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ist) ){.    goto
292c2 20 74 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63   triggerfinish_c
292c3 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
292c4 2a 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20  * if we are not 
292c5 69 6e 69 74 69 61 6c 69 7a 69 6e 67 2c 20 61 6e  initializing, an
292c6 64 20 74 68 69 73 20 74 72 69 67 67 65 72 20 69  d this trigger i
292c7 73 20 6e 6f 74 20 6f 6e 20 61 20 54 45 4d 50 20  s not on a TEMP 
292c8 74 61 62 6c 65 2c 20 0a 20 20 2a 2a 20 62 75 69  table, .  ** bui
292c9 6c 64 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  ld the sqlite_ma
292ca 73 74 65 72 20 65 6e 74 72 79 0a 20 20 2a 2f 0a  ster entry.  */.
292cb 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
292cc 62 75 73 79 20 29 7b 0a 20 20 20 20 56 64 62 65  busy ){.    Vdbe
292cd 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a   *v;.    char *z
292ce 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61  ;..    /* Make a
292cf 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73  n entry in the s
292d0 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
292d1 6c 65 20 2a 2f 0a 20 20 20 20 76 20 3d 20 73 71  le */.    v = sq
292d2 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
292d3 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d  rse);.    if( v=
292d4 3d 30 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65  =0 ) goto trigge
292d5 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b  rfinish_cleanup;
292d6 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
292d7 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
292d8 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
292d9 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  .    z = sqlite3
292da 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
292db 68 61 72 2a 29 70 41 6c 6c 2d 3e 7a 2c 20 70 41  har*)pAll->z, pA
292dc 6c 6c 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 6c 69  ll->n);.    sqli
292dd 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
292de 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 22 49  Parse,.       "I
292df 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e 25 73  NSERT INTO %Q.%s
292e0 20 56 41 4c 55 45 53 28 27 74 72 69 67 67 65 72   VALUES('trigger
292e1 27 2c 25 51 2c 25 51 2c 30 2c 27 43 52 45 41 54  ',%Q,%Q,0,'CREAT
292e2 45 20 54 52 49 47 47 45 52 20 25 71 27 29 22 2c  E TRIGGER %q')",
292e3 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b  .       db->aDb[
292e4 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45  iDb].zName, SCHE
292e5 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a  MA_TABLE(iDb), z
292e6 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 70 54 72  Name,.       pTr
292e7 69 67 2d 3e 74 61 62 6c 65 2c 20 7a 29 3b 0a 20  ig->table, z);. 
292e8 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
292e9 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c  (db, z);.    sql
292ea 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
292eb 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
292ec 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
292ed 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65  dOp4(v, OP_Parse
292ee 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20  Schema, iDb, 0, 
292ef 30 2c 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74  0, sqlite3MPrint
292f0 66 28 0a 20 20 20 20 20 20 20 20 64 62 2c 20 22  f(.        db, "
292f1 74 79 70 65 3d 27 74 72 69 67 67 65 72 27 20 41  type='trigger' A
292f2 4e 44 20 6e 61 6d 65 3d 27 25 71 27 22 2c 20 7a  ND name='%q'", z
292f3 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49  Name), P4_DYNAMI
292f4 43 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20  C.    );.  }..  
292f5 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
292f6 79 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  y ){.    Trigger
292f7 20 2a 70 4c 69 6e 6b 20 3d 20 70 54 72 69 67 3b   *pLink = pTrig;
292f8 0a 20 20 20 20 48 61 73 68 20 2a 70 48 61 73 68  .    Hash *pHash
292f9 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
292fa 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61  .pSchema->trigHa
292fb 73 68 3b 0a 20 20 20 20 70 54 72 69 67 20 3d 20  sh;.    pTrig = 
292fc 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
292fd 74 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20  t(pHash, zName, 
292fe 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
292ff 7a 4e 61 6d 65 29 2c 20 70 54 72 69 67 29 3b 0a  zName), pTrig);.
29300 20 20 20 20 69 66 28 20 70 54 72 69 67 20 29 7b      if( pTrig ){
29301 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
29302 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
29303 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 69 6e 6b   }else if( pLink
29304 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 4c 69 6e 6b  ->pSchema==pLink
29305 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 29 7b 0a  ->pTabSchema ){.
29306 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
29307 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d  b;.      int n =
29308 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
29309 28 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65 29 20 2b  (pLink->table) +
2930a 20 31 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   1;.      pTab =
2930b 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
2930c 28 26 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68  (&pLink->pTabSch
2930d 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 4c  ema->tblHash, pL
2930e 69 6e 6b 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a  ink->table, n);.
2930f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
29310 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ab!=0 );.      p
29311 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 54  Link->pNext = pT
29312 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a 20 20  ab->pTrigger;.  
29313 20 20 20 20 70 54 61 62 2d 3e 70 54 72 69 67 67      pTab->pTrigg
29314 65 72 20 3d 20 70 4c 69 6e 6b 3b 0a 20 20 20 20  er = pLink;.    
29315 7d 0a 20 20 7d 0a 0a 74 72 69 67 67 65 72 66 69  }.  }..triggerfi
29316 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  nish_cleanup:.  
29317 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
29318 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 29 3b  gger(db, pTrig);
29319 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 72  .  assert( !pPar
2931a 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20  se->pNewTrigger 
2931b 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65  );.  sqlite3Dele
2931c 74 65 54 72 69 67 67 65 72 53 74 65 70 28 64 62  teTriggerStep(db
2931d 2c 20 70 53 74 65 70 4c 69 73 74 29 3b 0a 7d 0a  , pStepList);.}.
2931e 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f  ./*.** Make a co
2931f 70 79 20 6f 66 20 61 6c 6c 20 63 6f 6d 70 6f 6e  py of all compon
29320 65 6e 74 73 20 6f 66 20 74 68 65 20 67 69 76 65  ents of the give
29321 6e 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 20  n trigger step. 
29322 20 54 68 69 73 20 68 61 73 0a 2a 2a 20 74 68 65   This has.** the
29323 20 65 66 66 65 63 74 20 6f 66 20 63 6f 70 79 69   effect of copyi
29324 6e 67 20 61 6c 6c 20 45 78 70 72 2e 74 6f 6b 65  ng all Expr.toke
29325 6e 2e 7a 20 76 61 6c 75 65 73 20 69 6e 74 6f 20  n.z values into 
29326 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a  memory obtained.
29327 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ** from sqlite3_
29328 6d 61 6c 6c 6f 63 28 29 2e 20 20 41 73 20 69 6e  malloc().  As in
29329 69 74 69 61 6c 6c 79 20 63 72 65 61 74 65 64 2c  itially created,
2932a 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e   the Expr.token.
2932b 7a 20 76 61 6c 75 65 73 0a 2a 2a 20 61 6c 6c 20  z values.** all 
2932c 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69 6e 70  point to the inp
2932d 75 74 20 73 74 72 69 6e 67 20 74 68 61 74 20 77  ut string that w
2932e 61 73 20 66 65 64 20 74 6f 20 74 68 65 20 70 61  as fed to the pa
2932f 72 73 65 72 2e 20 20 42 75 74 20 74 68 61 74 0a  rser.  But that.
29330 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 65 70 68  ** string is eph
29331 65 6d 65 72 61 6c 20 2d 20 69 74 20 77 69 6c 6c  emeral - it will
29332 20 67 6f 20 61 77 61 79 20 61 73 20 73 6f 6f 6e   go away as soon
29333 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f   as the sqlite3_
29334 65 78 65 63 28 29 0a 2a 2a 20 63 61 6c 6c 20 74  exec().** call t
29335 68 61 74 20 73 74 61 72 74 65 64 20 74 68 65 20  hat started the 
29336 70 61 72 73 65 72 20 65 78 69 74 73 2e 20 20 54  parser exits.  T
29337 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65  his routine make
29338 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 0a 2a  s a persistent.*
29339 2a 20 63 6f 70 79 20 6f 66 20 61 6c 6c 20 74 68  * copy of all th
2933a 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 73  e Expr.token.z s
2933b 74 72 69 6e 67 73 20 73 6f 20 74 68 61 74 20 74  trings so that t
2933c 68 65 20 54 72 69 67 67 65 72 53 74 65 70 20 73  he TriggerStep s
2933d 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 69 6c 6c  tructure.** will
2933e 20 62 65 20 76 61 6c 69 64 20 65 76 65 6e 20 61   be valid even a
2933f 66 74 65 72 20 74 68 65 20 73 71 6c 69 74 65 33  fter the sqlite3
29340 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 72 65 74  _exec() call ret
29341 75 72 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  urns..*/.static 
29342 76 6f 69 64 20 73 71 6c 69 74 65 50 65 72 73 69  void sqlitePersi
29343 73 74 54 72 69 67 67 65 72 53 74 65 70 28 73 71  stTriggerStep(sq
29344 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67  lite3 *db, Trigg
29345 65 72 53 74 65 70 20 2a 70 29 7b 0a 20 20 69 66  erStep *p){.  if
29346 28 20 70 2d 3e 74 61 72 67 65 74 2e 7a 20 29 7b  ( p->target.z ){
29347 0a 20 20 20 20 70 2d 3e 74 61 72 67 65 74 2e 7a  .    p->target.z
29348 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 44   = (u8*)sqlite3D
29349 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68  bStrNDup(db, (ch
2934a 61 72 2a 29 70 2d 3e 74 61 72 67 65 74 2e 7a 2c  ar*)p->target.z,
2934b 20 70 2d 3e 74 61 72 67 65 74 2e 6e 29 3b 0a 20   p->target.n);. 
2934c 20 20 20 70 2d 3e 74 61 72 67 65 74 2e 64 79 6e     p->target.dyn
2934d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 1;.  }.  if( 
2934e 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  p->pSelect ){.  
2934f 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 20 3d    Select *pNew =
29350 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
29351 70 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65 63 74  p(db, p->pSelect
29352 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
29353 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
29354 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20  , p->pSelect);. 
29355 20 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20     p->pSelect = 
29356 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 66 28 20  pNew;.  }.  if( 
29357 70 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20  p->pWhere ){.   
29358 20 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71   Expr *pNew = sq
29359 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2935a 20 70 2d 3e 70 57 68 65 72 65 2c 20 45 58 50 52   p->pWhere, EXPR
2935b 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2935c 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2935d 74 65 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65  te(db, p->pWhere
2935e 29 3b 0a 20 20 20 20 70 2d 3e 70 57 68 65 72 65  );.    p->pWhere
2935f 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69   = pNew;.  }.  i
29360 66 28 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 20  f( p->pExprList 
29361 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  ){.    ExprList 
29362 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45  *pNew = sqlite3E
29363 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
29364 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20 31 29 3b  ->pExprList, 1);
29365 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
29366 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
29367 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20  ->pExprList);.  
29368 20 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d    p->pExprList =
29369 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 66 28   pNew;.  }.  if(
2936a 20 70 2d 3e 70 49 64 4c 69 73 74 20 29 7b 0a 20   p->pIdList ){. 
2936b 20 20 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 20     IdList *pNew 
2936c 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44  = sqlite3IdListD
2936d 75 70 28 64 62 2c 20 70 2d 3e 70 49 64 4c 69 73  up(db, p->pIdLis
2936e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  t);.    sqlite3I
2936f 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
29370 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a 20 20 20  p->pIdList);.   
29371 20 70 2d 3e 70 49 64 4c 69 73 74 20 3d 20 70 4e   p->pIdList = pN
29372 65 77 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ew;.  }.}../*.**
29373 20 54 75 72 6e 20 61 20 53 45 4c 45 43 54 20 73   Turn a SELECT s
29374 74 61 74 65 6d 65 6e 74 20 28 74 68 61 74 20 74  tatement (that t
29375 68 65 20 70 53 65 6c 65 63 74 20 70 61 72 61 6d  he pSelect param
29376 65 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 29 20  eter points to) 
29377 69 6e 74 6f 0a 2a 2a 20 61 20 74 72 69 67 67 65  into.** a trigge
29378 72 20 73 74 65 70 2e 20 20 52 65 74 75 72 6e 20  r step.  Return 
29379 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54  a pointer to a T
2937a 72 69 67 67 65 72 53 74 65 70 20 73 74 72 75 63  riggerStep struc
2937b 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
2937c 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
2937d 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
2937e 74 20 66 69 6e 64 73 20 61 20 53 45 4c 45 43 54  t finds a SELECT
2937f 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 0a 2a 2a   statement in.**
29380 20 62 6f 64 79 20 6f 66 20 61 20 54 52 49 47 47   body of a TRIGG
29381 45 52 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ER.  .*/.SQLITE_
29382 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72 53  PRIVATE TriggerS
29383 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67  tep *sqlite3Trig
29384 67 65 72 53 65 6c 65 63 74 53 74 65 70 28 73 71  gerSelectStep(sq
29385 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63  lite3 *db, Selec
29386 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 54  t *pSelect){.  T
29387 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72 69  riggerStep *pTri
29388 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74  ggerStep = sqlit
29389 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2938a 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65  b, sizeof(Trigge
2938b 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28 20 70  rStep));.  if( p
2938c 54 72 69 67 67 65 72 53 74 65 70 3d 3d 30 20 29  TriggerStep==0 )
2938d 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65   {.    sqlite3Se
2938e 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2938f 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74  Select);.    ret
29390 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54  urn 0;.  }..  pT
29391 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d  riggerStep->op =
29392 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20 70 54   TK_SELECT;.  pT
29393 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
29394 65 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20  ect = pSelect;. 
29395 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
29396 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66 61 75  rconf = OE_Defau
29397 6c 74 3b 0a 20 20 73 71 6c 69 74 65 50 65 72 73  lt;.  sqlitePers
29398 69 73 74 54 72 69 67 67 65 72 53 74 65 70 28 64  istTriggerStep(d
29399 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29  b, pTriggerStep)
2939a 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69  ;..  return pTri
2939b 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a  ggerStep;.}../*.
2939c 2a 2a 20 42 75 69 6c 64 20 61 20 74 72 69 67 67  ** Build a trigg
2939d 65 72 20 73 74 65 70 20 6f 75 74 20 6f 66 20 61  er step out of a
2939e 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65  n INSERT stateme
2939f 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  nt.  Return a po
293a0 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  inter.** to the 
293a1 6e 65 77 20 74 72 69 67 67 65 72 20 73 74 65 70  new trigger step
293a2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ..**.** The pars
293a3 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
293a4 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65  utine when it se
293a5 65 73 20 61 6e 20 49 4e 53 45 52 54 20 69 6e 73  es an INSERT ins
293a6 69 64 65 20 74 68 65 0a 2a 2a 20 62 6f 64 79 20  ide the.** body 
293a7 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a 2a 2f  of a trigger..*/
293a8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
293a9 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c  TriggerStep *sql
293aa 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72  ite3TriggerInser
293ab 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33  tStep(.  sqlite3
293ac 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20   *db,        /* 
293ad 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
293ae 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b  nection */.  Tok
293af 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20  en *pTableName, 
293b0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
293b1 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
293b2 20 77 65 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20   we insert */.  
293b3 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c  IdList *pColumn,
293b4 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63      /* List of c
293b5 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 61 62 6c 65  olumns in pTable
293b6 4e 61 6d 65 20 74 6f 20 69 6e 73 65 72 74 20 69  Name to insert i
293b7 6e 74 6f 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  nto */.  ExprLis
293b8 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 2f 2a 20  t *pEList,   /* 
293b9 54 68 65 20 56 41 4c 55 45 20 63 6c 61 75 73 65  The VALUE clause
293ba 3a 20 61 20 6c 69 73 74 20 6f 66 20 76 61 6c 75  : a list of valu
293bb 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  es to be inserte
293bc 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  d */.  Select *p
293bd 53 65 6c 65 63 74 2c 20 20 20 20 2f 2a 20 41 20  Select,    /* A 
293be 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
293bf 20 74 68 61 74 20 73 75 70 70 6c 69 65 73 20 76   that supplies v
293c0 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6f  alues */.  int o
293c1 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 2f  rconf          /
293c2 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61  * The conflict a
293c3 6c 67 6f 72 69 74 68 6d 20 28 4f 45 5f 41 62 6f  lgorithm (OE_Abo
293c4 72 74 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20  rt, OE_Replace, 
293c5 65 74 63 2e 29 20 2a 2f 0a 29 7b 0a 20 20 54 72  etc.) */.){.  Tr
293c6 69 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67  iggerStep *pTrig
293c7 67 65 72 53 74 65 70 3b 0a 0a 20 20 61 73 73 65  gerStep;..  asse
293c8 72 74 28 70 45 4c 69 73 74 20 3d 3d 20 30 20 7c  rt(pEList == 0 |
293c9 7c 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 3b  | pSelect == 0);
293ca 0a 20 20 61 73 73 65 72 74 28 70 45 4c 69 73 74  .  assert(pEList
293cb 20 21 3d 20 30 20 7c 7c 20 70 53 65 6c 65 63 74   != 0 || pSelect
293cc 20 21 3d 20 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c   != 0 || db->mal
293cd 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 0a 20 20 70  locFailed);..  p
293ce 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71  TriggerStep = sq
293cf 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
293d0 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69  o(db, sizeof(Tri
293d1 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66  ggerStep));.  if
293d2 28 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29  ( pTriggerStep )
293d3 7b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  {.    pTriggerSt
293d4 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 53 45  ep->op = TK_INSE
293d5 52 54 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  RT;.    pTrigger
293d6 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20  Step->pSelect = 
293d7 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70 54 72  pSelect;.    pTr
293d8 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65  iggerStep->targe
293d9 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61 6d 65  t  = *pTableName
293da 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
293db 65 70 2d 3e 70 49 64 4c 69 73 74 20 3d 20 70 43  ep->pIdList = pC
293dc 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 54 72 69 67  olumn;.    pTrig
293dd 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  gerStep->pExprLi
293de 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 20  st = pEList;.   
293df 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
293e0 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a  rconf = orconf;.
293e1 20 20 20 20 73 71 6c 69 74 65 50 65 72 73 69 73      sqlitePersis
293e2 74 54 72 69 67 67 65 72 53 74 65 70 28 64 62 2c  tTriggerStep(db,
293e3 20 70 54 72 69 67 67 65 72 53 74 65 70 29 3b 0a   pTriggerStep);.
293e4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
293e5 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
293e6 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20  (db, pColumn);. 
293e7 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
293e8 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c  stDelete(db, pEL
293e9 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
293ea 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
293eb 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 7d 0a  , pSelect);.  }.
293ec 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67  .  return pTrigg
293ed 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  erStep;.}../*.**
293ee 20 43 6f 6e 73 74 72 75 63 74 20 61 20 74 72 69   Construct a tri
293ef 67 67 65 72 20 73 74 65 70 20 74 68 61 74 20 69  gger step that i
293f0 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 55 50 44  mplements an UPD
293f1 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  ATE statement an
293f2 64 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f  d return.** a po
293f3 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74 72  inter to that tr
293f4 69 67 67 65 72 20 73 74 65 70 2e 20 20 54 68 65  igger step.  The
293f5 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68   parser calls th
293f6 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
293f7 69 74 0a 2a 2a 20 73 65 65 73 20 61 6e 20 55 50  it.** sees an UP
293f8 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69  DATE statement i
293f9 6e 73 69 64 65 20 74 68 65 20 62 6f 64 79 20 6f  nside the body o
293fa 66 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47  f a CREATE TRIGG
293fb 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ER..*/.SQLITE_PR
293fc 49 56 41 54 45 20 54 72 69 67 67 65 72 53 74 65  IVATE TriggerSte
293fd 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65  p *sqlite3Trigge
293fe 72 55 70 64 61 74 65 53 74 65 70 28 0a 20 20 73  rUpdateStep(.  s
293ff 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
29400 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
29401 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
29402 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c  /.  Token *pTabl
29403 65 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65  eName,   /* Name
29404 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
29405 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20   be updated */. 
29406 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
29407 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 53 45 54  t,    /* The SET
29408 20 63 6c 61 75 73 65 3a 20 6c 69 73 74 20 6f 66   clause: list of
29409 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 6e 65 77 20   column and new 
2940a 76 61 6c 75 65 73 20 2a 2f 0a 20 20 45 78 70 72  values */.  Expr
2940b 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20   *pWhere,       
2940c 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
2940d 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  ause */.  int or
2940e 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20 2f  conf           /
2940f 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61  * The conflict a
29410 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62  lgorithm. (OE_Ab
29411 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20  ort, OE_Ignore, 
29412 65 74 63 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  etc) */.){.  Tri
29413 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
29414 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65 33  erStep = sqlite3
29415 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
29416 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53   sizeof(TriggerS
29417 74 65 70 29 29 3b 0a 20 20 69 66 28 20 70 54 72  tep));.  if( pTr
29418 69 67 67 65 72 53 74 65 70 3d 3d 30 20 29 7b 0a  iggerStep==0 ){.
29419 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2941a 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2941b 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 73 71 6c  EList);.     sql
2941c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2941d 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 20 20  b, pWhere);.    
2941e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
2941f 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
29420 6f 70 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b 0a  op = TK_UPDATE;.
29421 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
29422 74 61 72 67 65 74 20 20 3d 20 2a 70 54 61 62 6c  target  = *pTabl
29423 65 4e 61 6d 65 3b 0a 20 20 70 54 72 69 67 67 65  eName;.  pTrigge
29424 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74  rStep->pExprList
29425 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70 54 72   = pEList;.  pTr
29426 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72  iggerStep->pWher
29427 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 70 54  e = pWhere;.  pT
29428 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f  riggerStep->orco
29429 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 73  nf = orconf;.  s
2942a 71 6c 69 74 65 50 65 72 73 69 73 74 54 72 69 67  qlitePersistTrig
2942b 67 65 72 53 74 65 70 28 64 62 2c 20 70 54 72 69  gerStep(db, pTri
2942c 67 67 65 72 53 74 65 70 29 3b 0a 0a 20 20 72 65  ggerStep);..  re
2942d 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
2942e 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73  p;.}../*.** Cons
2942f 74 72 75 63 74 20 61 20 74 72 69 67 67 65 72 20  truct a trigger 
29430 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65 6d  step that implem
29431 65 6e 74 73 20 61 20 44 45 4c 45 54 45 20 73 74  ents a DELETE st
29432 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 74 75  atement and retu
29433 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
29434 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 20  to that trigger 
29435 73 74 65 70 2e 20 20 54 68 65 20 70 61 72 73 65  step.  The parse
29436 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
29437 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20  tine when it.** 
29438 73 65 65 73 20 61 20 44 45 4c 45 54 45 20 73 74  sees a DELETE st
29439 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74  atement inside t
2943a 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45  he body of a CRE
2943b 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a  ATE TRIGGER..*/.
2943c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
2943d 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
2943e 74 65 33 54 72 69 67 67 65 72 44 65 6c 65 74 65  te3TriggerDelete
2943f 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20  Step(.  sqlite3 
29440 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
29441 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
29442 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ection */.  Toke
29443 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20  n *pTableName,  
29444 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
29445 20 66 72 6f 6d 20 77 68 69 63 68 20 72 6f 77 73   from which rows
29446 20 61 72 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a   are deleted */.
29447 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20    Expr *pWhere  
29448 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
29449 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2944a 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65  .){.  TriggerSte
2944b 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 20  p *pTriggerStep 
2944c 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2944d 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2944e 28 54 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a  (TriggerStep));.
2944f 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74    if( pTriggerSt
29450 65 70 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ep==0 ){.    sql
29451 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
29452 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 20 20  b, pWhere);.    
29453 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
29454 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
29455 70 20 3d 20 54 4b 5f 44 45 4c 45 54 45 3b 0a 20  p = TK_DELETE;. 
29456 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74   pTriggerStep->t
29457 61 72 67 65 74 20 20 3d 20 2a 70 54 61 62 6c 65  arget  = *pTable
29458 4e 61 6d 65 3b 0a 20 20 70 54 72 69 67 67 65 72  Name;.  pTrigger
29459 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d 20 70  Step->pWhere = p
2945a 57 68 65 72 65 3b 0a 20 20 70 54 72 69 67 67 65  Where;.  pTrigge
2945b 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20  rStep->orconf = 
2945c 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20 73 71  OE_Default;.  sq
2945d 6c 69 74 65 50 65 72 73 69 73 74 54 72 69 67 67  litePersistTrigg
2945e 65 72 53 74 65 70 28 64 62 2c 20 70 54 72 69 67  erStep(db, pTrig
2945f 67 65 72 53 74 65 70 29 3b 0a 0a 20 20 72 65 74  gerStep);..  ret
29460 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70  urn pTriggerStep
29461 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 63 75  ;.}../* .** Recu
29462 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61  rsively delete a
29463 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75   Trigger structu
29464 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  re.*/.SQLITE_PRI
29465 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
29466 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73  3DeleteTrigger(s
29467 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67  qlite3 *db, Trig
29468 67 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a  ger *pTrigger){.
29469 20 20 69 66 28 20 70 54 72 69 67 67 65 72 3d 3d    if( pTrigger==
2946a 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  0 ) return;.  sq
2946b 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
2946c 65 72 53 74 65 70 28 64 62 2c 20 70 54 72 69 67  erStep(db, pTrig
2946d 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74 29 3b  ger->step_list);
2946e 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2946f 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e  (db, pTrigger->n
29470 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
29471 62 46 72 65 65 28 64 62 2c 20 70 54 72 69 67 67  bFree(db, pTrigg
29472 65 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 20 73 71  er->table);.  sq
29473 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
29474 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 57  db, pTrigger->pW
29475 68 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 49  hen);.  sqlite3I
29476 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
29477 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d  pTrigger->pColum
29478 6e 73 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67  ns);.  if( pTrig
29479 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e 2e 64  ger->nameToken.d
2947a 79 6e 20 29 20 73 71 6c 69 74 65 33 44 62 46 72  yn ) sqlite3DbFr
2947b 65 65 28 64 62 2c 20 28 63 68 61 72 2a 29 70 54  ee(db, (char*)pT
2947c 72 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65  rigger->nameToke
2947d 6e 2e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  n.z);.  sqlite3D
2947e 62 46 72 65 65 28 64 62 2c 20 70 54 72 69 67 67  bFree(db, pTrigg
2947f 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  er);.}../*.** Th
29480 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
29481 61 6c 6c 65 64 20 74 6f 20 64 72 6f 70 20 61 20  alled to drop a 
29482 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74 68 65  trigger from the
29483 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
29484 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61  . .**.** This ma
29485 79 20 62 65 20 63 61 6c 6c 65 64 20 64 69 72 65  y be called dire
29486 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 70 61  ctly from the pa
29487 72 73 65 72 20 61 6e 64 20 74 68 65 72 65 66 6f  rser and therefo
29488 72 65 20 69 64 65 6e 74 69 66 69 65 73 0a 2a 2a  re identifies.**
29489 20 74 68 65 20 74 72 69 67 67 65 72 20 62 79 20   the trigger by 
2948a 6e 61 6d 65 2e 20 20 54 68 65 20 73 71 6c 69 74  name.  The sqlit
2948b 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72  e3DropTriggerPtr
2948c 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  () routine does 
2948d 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6a 6f 62 20  the.** same job 
2948e 61 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  as this routine 
2948f 65 78 63 65 70 74 20 69 74 20 74 61 6b 65 73 20  except it takes 
29490 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
29491 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 6e 73 74   trigger.** inst
29492 65 61 64 20 6f 66 20 74 68 65 20 74 72 69 67 67  ead of the trigg
29493 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53 51 4c  er name..**/.SQL
29494 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
29495 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
29496 67 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  ger(Parse *pPars
29497 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d  e, SrcList *pNam
29498 65 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20  e, int noErr){. 
29499 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
2949a 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b  er = 0;.  int i;
2949b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2949c 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  Db;.  const char
2949d 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e   *zName;.  int n
2949e 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65 33 20  Name;.  sqlite3 
2949f 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
294a0 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  ;..  if( db->mal
294a1 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f  locFailed ) goto
294a2 20 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c   drop_trigger_cl
294a3 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 53 51 4c  eanup;.  if( SQL
294a4 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52  ITE_OK!=sqlite3R
294a5 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
294a6 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 72  ) ){.    goto dr
294a7 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  op_trigger_clean
294a8 75 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  up;.  }..  asser
294a9 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d  t( pName->nSrc==
294aa 31 20 29 3b 0a 20 20 7a 44 62 20 3d 20 70 4e 61  1 );.  zDb = pNa
294ab 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  me->a[0].zDataba
294ac 73 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 4e  se;.  zName = pN
294ad 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b  ame->a[0].zName;
294ae 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  nName = sqlit
294af 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
294b0 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f  );.  for(i=OMIT_
294b1 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44  TEMPDB; i<db->nD
294b2 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  b; i++){.    int
294b3 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31   j = (i<2) ? i^1
294b4 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68   : i;  /* Search
294b5 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49   TEMP before MAI
294b6 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62  N */.    if( zDb
294b7 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
294b8 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e  mp(db->aDb[j].zN
294b9 61 6d 65 2c 20 7a 44 62 29 20 29 20 63 6f 6e 74  ame, zDb) ) cont
294ba 69 6e 75 65 3b 0a 20 20 20 20 70 54 72 69 67 67  inue;.    pTrigg
294bb 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  er = sqlite3Hash
294bc 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 6a  Find(&(db->aDb[j
294bd 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  ].pSchema->trigH
294be 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  ash), zName, nNa
294bf 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 72  me);.    if( pTr
294c0 69 67 67 65 72 20 29 20 62 72 65 61 6b 3b 0a 20  igger ) break;. 
294c1 20 7d 0a 20 20 69 66 28 20 21 70 54 72 69 67 67   }.  if( !pTrigg
294c2 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e  er ){.    if( !n
294c3 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71  oErr ){.      sq
294c4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
294c5 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74  arse, "no such t
294c6 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70 4e 61  rigger: %S", pNa
294c7 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  me, 0);.    }.  
294c8 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67    goto drop_trig
294c9 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
294ca 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  .  sqlite3DropTr
294cb 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65 2c  iggerPtr(pParse,
294cc 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 64 72 6f   pTrigger);..dro
294cd 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  p_trigger_cleanu
294ce 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  p:.  sqlite3SrcL
294cf 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e  istDelete(db, pN
294d0 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ame);.}../*.** R
294d1 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
294d2 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  to the Table str
294d3 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 74  ucture for the t
294d4 61 62 6c 65 20 74 68 61 74 20 61 20 74 72 69 67  able that a trig
294d5 67 65 72 0a 2a 2a 20 69 73 20 73 65 74 20 6f 6e  ger.** is set on
294d6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61 62 6c  ..*/.static Tabl
294d7 65 20 2a 74 61 62 6c 65 4f 66 54 72 69 67 67 65  e *tableOfTrigge
294d8 72 28 54 72 69 67 67 65 72 20 2a 70 54 72 69 67  r(Trigger *pTrig
294d9 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  ger){.  int n = 
294da 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
294db 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 29  pTrigger->table)
294dc 20 2b 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73   + 1;.  return s
294dd 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
294de 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63  pTrigger->pTabSc
294df 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70  hema->tblHash, p
294e0 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20  Trigger->table, 
294e1 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72  n);.}.../*.** Dr
294e2 6f 70 20 61 20 74 72 69 67 67 65 72 20 67 69 76  op a trigger giv
294e3 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  en a pointer to 
294e4 74 68 61 74 20 74 72 69 67 67 65 72 2e 20 0a 2a  that trigger. .*
294e5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
294e6 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
294e7 70 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73  pTriggerPtr(Pars
294e8 65 20 2a 70 50 61 72 73 65 2c 20 54 72 69 67 67  e *pParse, Trigg
294e9 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20  er *pTrigger){. 
294ea 20 54 61 62 6c 65 20 20 20 2a 70 54 61 62 6c 65   Table   *pTable
294eb 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73  ;.  Vdbe *v;.  s
294ec 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
294ed 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
294ee 44 62 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c  Db;..  iDb = sql
294ef 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
294f0 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
294f1 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 29  rigger->pSchema)
294f2 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
294f3 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
294f4 62 20 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20  b );.  pTable = 
294f5 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70  tableOfTrigger(p
294f6 54 72 69 67 67 65 72 29 3b 0a 20 20 61 73 73 65  Trigger);.  asse
294f7 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 20 20  rt( pTable );.  
294f8 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
294f9 70 53 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65  pSchema==pTrigge
294fa 72 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 69 44  r->pSchema || iD
294fb 62 3d 3d 31 20 29 3b 0a 23 69 66 6e 64 65 66 20  b==1 );.#ifndef 
294fc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
294fd 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20  ORIZATION.  {.  
294fe 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c    int code = SQL
294ff 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52  ITE_DROP_TRIGGER
29500 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
29501 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
29502 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
29503 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
29504 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28   = SCHEMA_TABLE(
29505 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20 69 44  iDb);.    if( iD
29506 62 3d 3d 31 20 29 20 63 6f 64 65 20 3d 20 53 51  b==1 ) code = SQ
29507 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54  LITE_DROP_TEMP_T
29508 52 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20  RIGGER;.    if( 
29509 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2950a 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70  (pParse, code, p
2950b 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 70  Trigger->name, p
2950c 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44  Table->zName, zD
2950d 62 29 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69  b) ||.      sqli
2950e 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2950f 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
29510 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62  TE, zTab, 0, zDb
29511 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
29512 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  n;.    }.  }.#en
29513 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  dif..  /* Genera
29514 74 65 20 63 6f 64 65 20 74 6f 20 64 65 73 74 72  te code to destr
29515 6f 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  oy the database 
29516 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74 72  record of the tr
29517 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73  igger..  */.  as
29518 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20  sert( pTable!=0 
29519 29 3b 0a 20 20 69 66 28 20 28 76 20 3d 20 73 71  );.  if( (v = sq
2951a 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2951b 72 73 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  rse))!=0 ){.    
2951c 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 20 73 74  int base;.    st
2951d 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
2951e 70 4c 69 73 74 20 64 72 6f 70 54 72 69 67 67 65  pList dropTrigge
2951f 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20  r[] = {.      { 
29520 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 30  OP_Rewind,     0
29521 2c 20 41 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a  , ADDR(9),  0},.
29522 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
29523 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20  g8,    0, 1,    
29524 20 20 20 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a      0}, /* 1 */.
29525 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d        { OP_Colum
29526 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20  n,     0, 1,    
29527 20 20 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20      2},.      { 
29528 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32  OP_Ne,         2
29529 2c 20 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a  , ADDR(8),  1},.
2952a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
2952b 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20  g8,    0, 1,    
2952c 20 20 20 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74      0}, /* 4: "t
2952d 72 69 67 67 65 72 22 20 2a 2f 0a 20 20 20 20 20  rigger" */.     
2952e 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20   { OP_Column,   
2952f 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 32    0, 0,        2
29530 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
29531 2c 20 20 20 20 20 20 20 20 20 32 2c 20 41 44 44  ,         2, ADD
29532 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20  R(8),  1},.     
29533 20 7b 20 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20   { OP_Delete,   
29534 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30    0, 0,        0
29535 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
29536 78 74 2c 20 20 20 20 20 20 20 30 2c 20 41 44 44  xt,       0, ADD
29537 52 28 31 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20  R(1),  0}, /* 8 
29538 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73  */.    };..    s
29539 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
2953a 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
2953b 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 0, iDb);.    s
2953c 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72  qlite3OpenMaster
2953d 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
2953e 62 29 3b 0a 20 20 20 20 62 61 73 65 20 3d 20 73  b);.    base = s
2953f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
29540 69 73 74 28 76 2c 20 20 41 72 72 61 79 53 69 7a  ist(v,  ArraySiz
29541 65 28 64 72 6f 70 54 72 69 67 67 65 72 29 2c 20  e(dropTrigger), 
29542 64 72 6f 70 54 72 69 67 67 65 72 29 3b 0a 20 20  dropTrigger);.  
29543 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29544 6e 67 65 50 34 28 76 2c 20 62 61 73 65 2b 31 2c  ngeP4(v, base+1,
29545 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 2c   pTrigger->name,
29546 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
29547 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
29548 62 61 73 65 2b 34 2c 20 22 74 72 69 67 67 65 72  base+4, "trigger
29549 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
2954a 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
2954b 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
2954c 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
2954d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2954e 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20  _Close, 0, 0);. 
2954f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29550 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54  dOp4(v, OP_DropT
29551 72 69 67 67 65 72 2c 20 69 44 62 2c 20 30 2c 20  rigger, iDb, 0, 
29552 30 2c 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d  0, pTrigger->nam
29553 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  e, 0);.    if( p
29554 50 61 72 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29 7b  Parse->nMem<3 ){
29555 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
29556 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 7d 0a 20  Mem = 3;.    }. 
29557 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f   }.}../*.** Remo
29558 76 65 20 61 20 74 72 69 67 67 65 72 20 66 72 6f  ve a trigger fro
29559 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
2955a 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 2a  s of the sqlite*
2955b 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c   pointer..*/.SQL
2955c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2955d 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
2955e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73  dDeleteTrigger(s
2955f 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
29560 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  iDb, const char 
29561 2a 7a 4e 61 6d 65 29 7b 0a 20 20 48 61 73 68 20  *zName){.  Hash 
29562 2a 70 48 61 73 68 20 3d 20 26 28 64 62 2d 3e 61  *pHash = &(db->a
29563 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
29564 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20 54 72  >trigHash);.  Tr
29565 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
29566 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71  .  pTrigger = sq
29567 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
29568 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71  pHash, zName, sq
29569 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
2956a 61 6d 65 29 2c 20 30 29 3b 0a 20 20 69 66 28 20  ame), 0);.  if( 
2956b 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
2956c 69 66 28 20 70 54 72 69 67 67 65 72 2d 3e 70 53  if( pTrigger->pS
2956d 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d  chema==pTrigger-
2956e 3e 70 54 61 62 53 63 68 65 6d 61 20 29 7b 0a 20  >pTabSchema ){. 
2956f 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
29570 20 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65   = tableOfTrigge
29571 72 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20  r(pTrigger);.   
29572 20 20 20 54 72 69 67 67 65 72 20 2a 2a 70 70 3b     Trigger **pp;
29573 0a 20 20 20 20 20 20 66 6f 72 28 70 70 3d 26 70  .      for(pp=&p
29574 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 2a  Tab->pTrigger; *
29575 70 70 21 3d 70 54 72 69 67 67 65 72 3b 20 70 70  pp!=pTrigger; pp
29576 3d 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29  =&((*pp)->pNext)
29577 29 3b 0a 20 20 20 20 20 20 2a 70 70 20 3d 20 28  );.      *pp = (
29578 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  *pp)->pNext;.   
29579 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65   }.    sqlite3De
2957a 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20  leteTrigger(db, 
2957b 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 64  pTrigger);.    d
2957c 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
2957d 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
2957e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  ;.  }.}../*.** p
2957f 45 4c 69 73 74 20 69 73 20 74 68 65 20 53 45 54  EList is the SET
29580 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50   clause of an UP
29581 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  DATE statement. 
29582 20 45 61 63 68 20 65 6e 74 72 79 0a 2a 2a 20 69   Each entry.** i
29583 6e 20 70 45 4c 69 73 74 20 69 73 20 6f 66 20 74  n pEList is of t
29584 68 65 20 66 6f 72 6d 61 74 20 3c 69 64 3e 3d 3c  he format <id>=<
29585 65 78 70 72 3e 2e 20 20 49 66 20 61 6e 79 20 6f  expr>.  If any o
29586 66 20 74 68 65 20 65 6e 74 72 69 65 73 0a 2a 2a  f the entries.**
29587 20 69 6e 20 70 45 4c 69 73 74 20 68 61 76 65 20   in pEList have 
29588 61 6e 20 3c 69 64 3e 20 77 68 69 63 68 20 6d 61  an <id> which ma
29589 74 63 68 65 73 20 61 6e 20 69 64 65 6e 74 69 66  tches an identif
2958a 69 65 72 20 69 6e 20 70 49 64 4c 69 73 74 2c 0a  ier in pIdList,.
2958b 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 54  ** then return T
2958c 52 55 45 2e 20 20 49 66 20 70 49 64 4c 69 73 74  RUE.  If pIdList
2958d 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20  ==NULL, then it 
2958e 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 0a  is considered a.
2958f 2a 2a 20 77 69 6c 64 63 61 72 64 20 74 68 61 74  ** wildcard that
29590 20 6d 61 74 63 68 65 73 20 61 6e 79 74 68 69 6e   matches anythin
29591 67 2e 20 20 4c 69 6b 65 77 69 73 65 20 69 66 20  g.  Likewise if 
29592 70 45 4c 69 73 74 3d 3d 4e 55 4c 4c 20 74 68 65  pEList==NULL the
29593 6e 0a 2a 2a 20 69 74 20 6d 61 74 63 68 65 73 20  n.** it matches 
29594 61 6e 79 74 68 69 6e 67 20 73 6f 20 61 6c 77 61  anything so alwa
29595 79 73 20 72 65 74 75 72 6e 20 74 72 75 65 2e 20  ys return true. 
29596 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 6f 6e   Return false on
29597 6c 79 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69  ly.** if there i
29598 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73  s no match..*/.s
29599 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 43  tatic int checkC
2959a 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28 49 64 4c  olumnOverLap(IdL
2959b 69 73 74 20 2a 70 49 64 4c 69 73 74 2c 20 45 78  ist *pIdList, Ex
2959c 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 29 7b  prList *pEList){
2959d 0a 20 20 69 6e 74 20 65 3b 0a 20 20 69 66 28 20  .  int e;.  if( 
2959e 21 70 49 64 4c 69 73 74 20 7c 7c 20 21 70 45 4c  !pIdList || !pEL
2959f 69 73 74 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ist ) return 1;.
295a0 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c 70 45 4c    for(e=0; e<pEL
295a1 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65 2b 2b 29  ist->nExpr; e++)
295a2 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
295a3 33 49 64 4c 69 73 74 49 6e 64 65 78 28 70 49 64  3IdListIndex(pId
295a4 4c 69 73 74 2c 20 70 45 4c 69 73 74 2d 3e 61 5b  List, pEList->a[
295a5 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30 20 29 20 72  e].zName)>=0 ) r
295a6 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
295a7 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a  eturn 0; .}../*.
295a8 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74  ** Return a list
295a9 20 6f 66 20 61 6c 6c 20 74 72 69 67 67 65 72 73   of all triggers
295aa 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 69   on table pTab i
295ab 66 20 74 68 65 72 65 20 65 78 69 73 74 73 20 61  f there exists a
295ac 74 20 6c 65 61 73 74 0a 2a 2a 20 6f 6e 65 20 74  t least.** one t
295ad 72 69 67 67 65 72 20 74 68 61 74 20 6d 75 73 74  rigger that must
295ae 20 62 65 20 66 69 72 65 64 20 77 68 65 6e 20 61   be fired when a
295af 6e 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74  n operation of t
295b0 79 70 65 20 27 6f 70 27 20 69 73 20 0a 2a 2a 20  ype 'op' is .** 
295b1 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65  performed on the
295b2 20 74 61 62 6c 65 2c 20 61 6e 64 2c 20 69 66 20   table, and, if 
295b3 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 69  that operation i
295b4 73 20 61 6e 20 55 50 44 41 54 45 2c 20 69 66 20  s an UPDATE, if 
295b5 61 74 0a 2a 2a 20 6c 65 61 73 74 20 6f 6e 65 20  at.** least one 
295b6 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 69  of the columns i
295b7 6e 20 70 43 68 61 6e 67 65 73 20 69 73 20 62 65  n pChanges is be
295b8 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2f  ing modified..*/
295b9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
295ba 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33  Trigger *sqlite3
295bb 54 72 69 67 67 65 72 73 45 78 69 73 74 28 0a 20  TriggersExist(. 
295bc 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
295bd 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
295be 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  e context */.  T
295bf 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
295c0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
295c1 62 6c 65 20 74 68 65 20 63 6f 6e 74 61 69 6e 73  ble the contains
295c2 20 74 68 65 20 74 72 69 67 67 65 72 73 20 2a 2f   the triggers */
295c3 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
295c4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e             /* on
295c5 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20  e of TK_DELETE, 
295c6 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50  TK_INSERT, TK_UP
295c7 44 41 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69  DATE */.  ExprLi
295c8 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20  st *pChanges,   
295c9 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 74 68 61    /* Columns tha
295ca 74 20 63 68 61 6e 67 65 20 69 6e 20 61 6e 20 55  t change in an U
295cb 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
295cc 2a 2f 0a 20 20 69 6e 74 20 2a 70 4d 61 73 6b 20  */.  int *pMask 
295cd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
295ce 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 54 52 49  OUT: Mask of TRI
295cf 47 47 45 52 5f 42 45 46 4f 52 45 7c 54 52 49 47  GGER_BEFORE|TRIG
295d0 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 29 7b 0a  GER_AFTER */.){.
295d1 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30 3b 0a    int mask = 0;.
295d2 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69 73 74    Trigger *pList
295d3 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
295d4 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54  rList(pParse, pT
295d5 61 62 29 3b 0a 20 20 54 72 69 67 67 65 72 20 2a  ab);.  Trigger *
295d6 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  p;.  assert( pLi
295d7 73 74 3d 3d 30 20 7c 7c 20 49 73 56 69 72 74 75  st==0 || IsVirtu
295d8 61 6c 28 70 54 61 62 29 3d 3d 30 20 29 3b 0a 20  al(pTab)==0 );. 
295d9 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b 20 70 3b   for(p=pList; p;
295da 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
295db 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20    if( p->op==op 
295dc 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76  && checkColumnOv
295dd 65 72 4c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e  erLap(p->pColumn
295de 73 2c 20 70 43 68 61 6e 67 65 73 29 20 29 7b 0a  s, pChanges) ){.
295df 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 70 2d        mask |= p-
295e0 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a 20 20  >tr_tm;.    }.  
295e1 7d 0a 20 20 69 66 28 20 70 4d 61 73 6b 20 29 7b  }.  if( pMask ){
295e2 0a 20 20 20 20 2a 70 4d 61 73 6b 20 3d 20 6d 61  .    *pMask = ma
295e3 73 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  sk;.  }.  return
295e4 20 28 6d 61 73 6b 20 3f 20 70 4c 69 73 74 20 3a   (mask ? pList :
295e5 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   0);.}../*.** Co
295e6 6e 76 65 72 74 20 74 68 65 20 70 53 74 65 70 2d  nvert the pStep-
295e7 3e 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 69 6e  >target token in
295e8 74 6f 20 61 20 53 72 63 4c 69 73 74 20 61 6e 64  to a SrcList and
295e9 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
295ea 72 0a 2a 2a 20 74 6f 20 74 68 61 74 20 53 72 63  r.** to that Src
295eb 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  List..**.** This
295ec 20 72 6f 75 74 69 6e 65 20 61 64 64 73 20 61 20   routine adds a 
295ed 73 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73  specific databas
295ee 65 20 6e 61 6d 65 2c 20 69 66 20 6e 65 65 64 65  e name, if neede
295ef 64 2c 20 74 6f 20 74 68 65 20 74 61 72 67 65 74  d, to the target
295f0 20 77 68 65 6e 0a 2a 2a 20 66 6f 72 6d 69 6e 67   when.** forming
295f1 20 74 68 65 20 53 72 63 4c 69 73 74 2e 20 20 54   the SrcList.  T
295f2 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 74  his prevents a t
295f3 72 69 67 67 65 72 20 69 6e 20 6f 6e 65 20 64 61  rigger in one da
295f4 74 61 62 61 73 65 20 66 72 6f 6d 0a 2a 2a 20 72  tabase from.** r
295f5 65 66 65 72 72 69 6e 67 20 74 6f 20 61 20 74 61  eferring to a ta
295f6 72 67 65 74 20 69 6e 20 61 6e 6f 74 68 65 72 20  rget in another 
295f7 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 65 78  database.  An ex
295f8 63 65 70 74 69 6f 6e 20 69 73 20 77 68 65 6e 20  ception is when 
295f9 74 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 69  the.** trigger i
295fa 73 20 69 6e 20 54 45 4d 50 20 69 6e 20 77 68 69  s in TEMP in whi
295fb 63 68 20 63 61 73 65 20 69 74 20 63 61 6e 20 72  ch case it can r
295fc 65 66 65 72 20 74 6f 20 61 6e 79 20 6f 74 68 65  efer to any othe
295fd 72 20 64 61 74 61 62 61 73 65 20 69 74 0a 2a 2a  r database it.**
295fe 20 77 61 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69   wants..*/.stati
295ff 63 20 53 72 63 4c 69 73 74 20 2a 74 61 72 67 65  c SrcList *targe
29600 74 53 72 63 4c 69 73 74 28 0a 20 20 50 61 72 73  tSrcList(.  Pars
29601 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
29602 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
29603 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69  context */.  Tri
29604 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20  ggerStep *pStep 
29605 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
29606 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
29607 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 2a 2f 0a  target token */.
29608 29 7b 0a 20 20 54 6f 6b 65 6e 20 73 44 62 3b 20  ){.  Token sDb; 
29609 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 75 6d            /* Dum
2960a 6d 79 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  my database name
2960b 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
2960c 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
2960d 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
2960e 20 64 61 74 61 62 61 73 65 20 74 6f 20 75 73 65   database to use
2960f 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
29610 53 72 63 3b 20 20 20 20 20 20 20 2f 2a 20 53 72  Src;       /* Sr
29611 63 4c 69 73 74 20 74 6f 20 62 65 20 72 65 74 75  cList to be retu
29612 72 6e 65 64 20 2a 2f 0a 0a 20 20 69 44 62 20 3d  rned */..  iDb =
29613 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
29614 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
29615 2c 20 70 53 74 65 70 2d 3e 70 54 72 69 67 2d 3e  , pStep->pTrig->
29616 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20  pSchema);.  if( 
29617 69 44 62 3d 3d 30 20 7c 7c 20 69 44 62 3e 3d 32  iDb==0 || iDb>=2
29618 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
29619 69 44 62 3c 70 50 61 72 73 65 2d 3e 64 62 2d 3e  iDb<pParse->db->
2961a 6e 44 62 20 29 3b 0a 20 20 20 20 73 44 62 2e 7a  nDb );.    sDb.z
2961b 20 3d 20 28 75 38 2a 29 70 50 61 72 73 65 2d 3e   = (u8*)pParse->
2961c 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2961d 6d 65 3b 0a 20 20 20 20 73 44 62 2e 6e 20 3d 20  me;.    sDb.n = 
2961e 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2961f 28 63 68 61 72 2a 29 73 44 62 2e 7a 29 3b 0a 20  (char*)sDb.z);. 
29620 20 20 20 70 53 72 63 20 3d 20 73 71 6c 69 74 65     pSrc = sqlite
29621 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70  3SrcListAppend(p
29622 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 20 26 73  Parse->db, 0, &s
29623 44 62 2c 20 26 70 53 74 65 70 2d 3e 74 61 72 67  Db, &pStep->targ
29624 65 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  et);.  } else {.
29625 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c 69 74      pSrc = sqlit
29626 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
29627 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 20 26  pParse->db, 0, &
29628 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2c 20 30  pStep->target, 0
29629 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
2962a 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  pSrc;.}../*.** G
2962b 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64  enerate VDBE cod
2962c 65 20 66 6f 72 20 7a 65 72 6f 20 6f 72 20 6d 6f  e for zero or mo
2962d 72 65 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  re statements in
2962e 73 69 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66  side the body of
2962f 20 61 0a 2a 2a 20 74 72 69 67 67 65 72 2e 20 20   a.** trigger.  
29630 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
29631 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72 61  odeTriggerProgra
29632 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  m(.  Parse *pPar
29633 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  se,            /
29634 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
29635 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65  text */.  Trigge
29636 72 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73 74  rStep *pStepList
29637 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 73  ,   /* List of s
29638 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65  tatements inside
29639 20 74 68 65 20 74 72 69 67 67 65 72 20 62 6f 64   the trigger bod
2963a 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e  y */.  int orcon
2963b 66 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  fin             
2963c 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 61 6c 67   /* Conflict alg
2963d 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72  orithm. (OE_Abor
2963e 74 2c 20 65 74 63 29 20 2a 2f 20 20 0a 29 7b 0a  t, etc) */  .){.
2963f 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 20    TriggerStep * 
29640 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 70  pTriggerStep = p
29641 53 74 65 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20  StepList;.  int 
29642 6f 72 63 6f 6e 66 3b 0a 20 20 56 64 62 65 20 2a  orconf;.  Vdbe *
29643 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
29644 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e;.  sqlite3 *db
29645 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
29646 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67    assert( pTrigg
29647 65 72 53 74 65 70 21 3d 30 20 29 3b 0a 20 20 61  erStep!=0 );.  a
29648 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20  ssert( v!=0 );. 
29649 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2964a 70 32 28 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74  p2(v, OP_Context
2964b 50 75 73 68 2c 20 30 2c 20 30 29 3b 0a 20 20 56  Push, 0, 0);.  V
2964c 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2964d 62 65 67 69 6e 20 74 72 69 67 67 65 72 20 25 73  begin trigger %s
2964e 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 54  ", pStepList->pT
2964f 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 20 20 77  rig->name));.  w
29650 68 69 6c 65 28 20 70 54 72 69 67 67 65 72 53 74  hile( pTriggerSt
29651 65 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ep ){.    sqlite
29652 33 45 78 70 72 43 6c 65 61 72 43 6f 6c 75 6d 6e  3ExprClearColumn
29653 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 2d 31  Cache(pParse, -1
29654 29 3b 0a 20 20 20 20 6f 72 63 6f 6e 66 20 3d 20  );.    orconf = 
29655 28 6f 72 63 6f 6e 66 69 6e 20 3d 3d 20 4f 45 5f  (orconfin == OE_
29656 44 65 66 61 75 6c 74 29 3f 70 54 72 69 67 67 65  Default)?pTrigge
29657 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 3a 6f 72  rStep->orconf:or
29658 63 6f 6e 66 69 6e 3b 0a 20 20 20 20 70 50 61 72  confin;.    pPar
29659 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e 6f  se->trigStack->o
2965a 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a  rconf = orconf;.
2965b 20 20 20 20 73 77 69 74 63 68 28 20 70 54 72 69      switch( pTri
2965c 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 29 7b 0a  ggerStep->op ){.
2965d 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45        case TK_SE
2965e 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 20 20  LECT: {.        
2965f 53 65 6c 65 63 74 20 2a 73 73 20 3d 20 73 71 6c  Select *ss = sql
29660 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
29661 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e  , pTriggerStep->
29662 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20  pSelect, 0);.   
29663 20 20 20 20 20 69 66 28 20 73 73 20 29 7b 0a 20       if( ss ){. 
29664 20 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 44           SelectD
29665 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 20 20 20  est dest;..     
29666 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
29667 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
29668 2c 20 53 52 54 5f 44 69 73 63 61 72 64 2c 20 30  , SRT_Discard, 0
29669 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2966a 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
2966b 65 2c 20 73 73 2c 20 26 64 65 73 74 29 3b 0a 20  e, ss, &dest);. 
2966c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2966d 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
2966e 20 73 73 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a   ss);.        }.
2966f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
29670 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
29671 65 20 54 4b 5f 55 50 44 41 54 45 3a 20 7b 0a 20  e TK_UPDATE: {. 
29672 20 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a         SrcList *
29673 70 53 72 63 3b 0a 20 20 20 20 20 20 20 20 70 53  pSrc;.        pS
29674 72 63 20 3d 20 74 61 72 67 65 74 53 72 63 4c 69  rc = targetSrcLi
29675 73 74 28 70 50 61 72 73 65 2c 20 70 54 72 69 67  st(pParse, pTrig
29676 67 65 72 53 74 65 70 29 3b 0a 20 20 20 20 20 20  gerStep);.      
29677 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29678 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65 74 43  Op2(v, OP_ResetC
29679 6f 75 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  ount, 0, 0);.   
2967a 20 20 20 20 20 73 71 6c 69 74 65 33 55 70 64 61       sqlite3Upda
2967b 74 65 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c  te(pParse, pSrc,
2967c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2967d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2967e 44 75 70 28 64 62 2c 20 70 54 72 69 67 67 65 72  Dup(db, pTrigger
2967f 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c  Step->pExprList,
29680 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20   0), .          
29681 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
29682 72 44 75 70 28 64 62 2c 20 70 54 72 69 67 67 65  rDup(db, pTrigge
29683 72 53 74 65 70 2d 3e 70 57 68 65 72 65 2c 20 30  rStep->pWhere, 0
29684 29 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 20 20  ), orconf);.    
29685 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29686 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65  ddOp2(v, OP_Rese
29687 74 43 6f 75 6e 74 2c 20 31 2c 20 30 29 3b 0a 20  tCount, 1, 0);. 
29688 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
29689 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
2968a 20 54 4b 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20   TK_INSERT: {.  
2968b 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70        SrcList *p
2968c 53 72 63 3b 0a 20 20 20 20 20 20 20 20 70 53 72  Src;.        pSr
2968d 63 20 3d 20 74 61 72 67 65 74 53 72 63 4c 69 73  c = targetSrcLis
2968e 74 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  t(pParse, pTrigg
2968f 65 72 53 74 65 70 29 3b 0a 20 20 20 20 20 20 20  erStep);.       
29690 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29691 70 32 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f  p2(v, OP_ResetCo
29692 75 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  unt, 0, 0);.    
29693 20 20 20 20 73 71 6c 69 74 65 33 49 6e 73 65 72      sqlite3Inser
29694 74 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c 0a  t(pParse, pSrc,.
29695 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29696 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
29697 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
29698 45 78 70 72 4c 69 73 74 2c 20 30 29 2c 20 0a 20  ExprList, 0), . 
29699 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2969a 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54  SelectDup(db, pT
2969b 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
2969c 65 63 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20  ect, 0), .      
2969d 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73      sqlite3IdLis
2969e 74 44 75 70 28 64 62 2c 20 70 54 72 69 67 67 65  tDup(db, pTrigge
2969f 72 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 29 2c  rStep->pIdList),
296a0 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20   orconf);.      
296a1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
296a2 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65 74 43  Op2(v, OP_ResetC
296a3 6f 75 6e 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20  ount, 1, 0);.   
296a4 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
296a5 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54    }.      case T
296a6 4b 5f 44 45 4c 45 54 45 3a 20 7b 0a 20 20 20 20  K_DELETE: {.    
296a7 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72      SrcList *pSr
296a8 63 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  c;.        sqlit
296a9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
296aa 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 30  OP_ResetCount, 0
296ab 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 53  , 0);.        pS
296ac 72 63 20 3d 20 74 61 72 67 65 74 53 72 63 4c 69  rc = targetSrcLi
296ad 73 74 28 70 50 61 72 73 65 2c 20 70 54 72 69 67  st(pParse, pTrig
296ae 67 65 72 53 74 65 70 29 3b 0a 20 20 20 20 20 20  gerStep);.      
296af 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46    sqlite3DeleteF
296b0 72 6f 6d 28 70 50 61 72 73 65 2c 20 70 53 72 63  rom(pParse, pSrc
296b1 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
296b2 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
296b3 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
296b4 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57  pTriggerStep->pW
296b5 68 65 72 65 2c 20 30 29 29 3b 0a 20 20 20 20 20  here, 0));.     
296b6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
296b7 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65 74  dOp2(v, OP_Reset
296b8 43 6f 75 6e 74 2c 20 31 2c 20 30 29 3b 0a 20 20  Count, 1, 0);.  
296b9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
296ba 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75     }.      defau
296bb 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73 73 65  lt:.        asse
296bc 72 74 28 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20  rt(0);.    } .  
296bd 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d    pTriggerStep =
296be 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
296bf 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Next;.  }.  sqli
296c0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
296c1 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 2c 20   OP_ContextPop, 
296c2 30 2c 20 30 29 3b 0a 20 20 56 64 62 65 43 6f 6d  0, 0);.  VdbeCom
296c3 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 20 74 72  ment((v, "end tr
296c4 69 67 67 65 72 20 25 73 22 2c 20 70 53 74 65 70  igger %s", pStep
296c5 4c 69 73 74 2d 3e 70 54 72 69 67 2d 3e 6e 61 6d  List->pTrig->nam
296c6 65 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 30  e));..  return 0
296c7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
296c8 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64  is called to cod
296c9 65 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74  e FOR EACH ROW t
296ca 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 57  riggers..**.** W
296cb 68 65 6e 20 74 68 65 20 63 6f 64 65 20 74 68 61  hen the code tha
296cc 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  t this function 
296cd 67 65 6e 65 72 61 74 65 73 20 69 73 20 65 78 65  generates is exe
296ce 63 75 74 65 64 2c 20 74 68 65 20 66 6f 6c 6c 6f  cuted, the follo
296cf 77 69 6e 67 20 0a 2a 2a 20 6d 75 73 74 20 62 65  wing .** must be
296d0 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 31 2e 20   true:.**.** 1. 
296d1 4e 6f 20 63 75 72 73 6f 72 73 20 6d 61 79 20 62  No cursors may b
296d2 65 20 6f 70 65 6e 20 69 6e 20 74 68 65 20 6d 61  e open in the ma
296d3 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 28 42  in database.  (B
296d4 75 74 20 6e 65 77 49 64 78 20 61 6e 64 20 6f 6c  ut newIdx and ol
296d5 64 49 64 78 0a 2a 2a 20 20 20 20 63 61 6e 20 62  dIdx.**    can b
296d6 65 20 69 6e 64 69 63 65 73 20 6f 66 20 63 75 72  e indices of cur
296d7 73 6f 72 73 20 69 6e 20 74 65 6d 70 6f 72 61 72  sors in temporar
296d8 79 20 74 61 62 6c 65 73 2e 20 20 53 65 65 20 62  y tables.  See b
296d9 65 6c 6f 77 2e 29 0a 2a 2a 0a 2a 2a 20 32 2e 20  elow.).**.** 2. 
296da 49 66 20 74 68 65 20 74 72 69 67 67 65 72 73 20  If the triggers 
296db 62 65 69 6e 67 20 63 6f 64 65 64 20 61 72 65 20  being coded are 
296dc 4f 4e 20 49 4e 53 45 52 54 20 6f 72 20 4f 4e 20  ON INSERT or ON 
296dd 55 50 44 41 54 45 20 74 72 69 67 67 65 72 73 2c  UPDATE triggers,
296de 20 74 68 65 6e 0a 2a 2a 20 20 20 20 61 20 74 65   then.**    a te
296df 6d 70 6f 72 61 72 79 20 76 64 62 65 20 63 75 72  mporary vdbe cur
296e0 73 6f 72 20 28 69 6e 64 65 78 20 6e 65 77 49 64  sor (index newId
296e1 78 29 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  x) must be open 
296e2 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20 61 74 0a  and pointing at.
296e3 2a 2a 20 20 20 20 61 20 72 6f 77 20 63 6f 6e 74  **    a row cont
296e4 61 69 6e 69 6e 67 20 76 61 6c 75 65 73 20 74 6f  aining values to
296e5 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20   be substituted 
296e6 66 6f 72 20 6e 65 77 2e 2a 20 65 78 70 72 65 73  for new.* expres
296e7 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 2a 2a 20  sions in the.** 
296e8 20 20 20 74 72 69 67 67 65 72 20 70 72 6f 67 72     trigger progr
296e9 61 6d 28 73 29 2e 0a 2a 2a 0a 2a 2a 20 33 2e 20  am(s)..**.** 3. 
296ea 49 66 20 74 68 65 20 74 72 69 67 67 65 72 73 20  If the triggers 
296eb 62 65 69 6e 67 20 63 6f 64 65 64 20 61 72 65 20  being coded are 
296ec 4f 4e 20 44 45 4c 45 54 45 20 6f 72 20 4f 4e 20  ON DELETE or ON 
296ed 55 50 44 41 54 45 20 74 72 69 67 67 65 72 73 2c  UPDATE triggers,
296ee 20 74 68 65 6e 0a 2a 2a 20 20 20 20 61 20 74 65   then.**    a te
296ef 6d 70 6f 72 61 72 79 20 76 64 62 65 20 63 75 72  mporary vdbe cur
296f0 73 6f 72 20 28 69 6e 64 65 78 20 6f 6c 64 49 64  sor (index oldId
296f1 78 29 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  x) must be open 
296f2 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20 61 74 0a  and pointing at.
296f3 2a 2a 20 20 20 20 61 20 72 6f 77 20 63 6f 6e 74  **    a row cont
296f4 61 69 6e 69 6e 67 20 76 61 6c 75 65 73 20 74 6f  aining values to
296f5 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20   be substituted 
296f6 66 6f 72 20 6f 6c 64 2e 2a 20 65 78 70 72 65 73  for old.* expres
296f7 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 2a 2a 20  sions in the.** 
296f8 20 20 20 74 72 69 67 67 65 72 20 70 72 6f 67 72     trigger progr
296f9 61 6d 28 73 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  am(s)..**.** If 
296fa 74 68 65 79 20 61 72 65 20 6e 6f 74 20 4e 55 4c  they are not NUL
296fb 4c 2c 20 74 68 65 20 70 69 4f 6c 64 43 6f 6c 4d  L, the piOldColM
296fc 61 73 6b 20 61 6e 64 20 70 69 4e 65 77 43 6f 6c  ask and piNewCol
296fd 4d 61 73 6b 20 6f 75 74 70 75 74 20 76 61 72 69  Mask output vari
296fe 61 62 6c 65 73 0a 2a 2a 20 61 72 65 20 73 65 74  ables.** are set
296ff 20 74 6f 20 76 61 6c 75 65 73 20 74 68 61 74 20   to values that 
29700 64 65 73 63 72 69 62 65 20 74 68 65 20 63 6f 6c  describe the col
29701 75 6d 6e 73 20 75 73 65 64 20 62 79 20 74 68 65  umns used by the
29702 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
29703 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4c 44 2e 2a  .** in the OLD.*
29704 20 61 6e 64 20 4e 45 57 2e 2a 20 74 61 62 6c 65   and NEW.* table
29705 73 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  s respectively. 
29706 49 66 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 74  If column N of t
29707 68 65 20 0a 2a 2a 20 70 73 65 75 64 6f 2d 74 61  he .** pseudo-ta
29708 62 6c 65 20 69 73 20 72 65 61 64 20 61 74 20 6c  ble is read at l
29709 65 61 73 74 20 6f 6e 63 65 2c 20 74 68 65 20 63  east once, the c
2970a 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74  orresponding bit
2970b 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 0a 2a   of the output.*
2970c 2a 20 6d 61 73 6b 20 69 73 20 73 65 74 2e 20 49  * mask is set. I
2970d 66 20 61 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20  f a column with 
2970e 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72  an index greater
2970f 20 74 68 61 6e 20 33 32 20 69 73 20 72 65 61 64   than 32 is read
29710 2c 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20  , the.** output 
29711 6d 61 73 6b 20 69 73 20 73 65 74 20 74 6f 20 74  mask is set to t
29712 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65  he special value
29713 20 30 78 66 66 66 66 66 66 66 66 2e 0a 2a 2a 0a   0xffffffff..**.
29714 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
29715 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64  E int sqlite3Cod
29716 65 52 6f 77 54 72 69 67 67 65 72 28 0a 20 20 50  eRowTrigger(.  P
29717 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
29718 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e      /* Parse con
29719 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65  text */.  Trigge
2971a 72 20 2a 70 54 72 69 67 67 65 72 2c 20 20 20 2f  r *pTrigger,   /
2971b 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65  * List of trigge
2971c 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62  rs on table pTab
2971d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
2971e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
2971f 65 20 6f 66 20 54 4b 5f 55 50 44 41 54 45 2c 20  e of TK_UPDATE, 
29720 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 44 45  TK_INSERT, TK_DE
29721 4c 45 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69  LETE */.  ExprLi
29722 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f  st *pChanges,  /
29723 2a 20 43 68 61 6e 67 65 73 20 6c 69 73 74 20 66  * Changes list f
29724 6f 72 20 61 6e 79 20 55 50 44 41 54 45 20 4f 46  or any UPDATE OF
29725 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69   triggers */.  i
29726 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20  nt tr_tm,       
29727 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52      /* One of TR
29728 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 54 52  IGGER_BEFORE, TR
29729 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20  IGGER_AFTER */. 
2972a 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2972b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
2972c 6c 65 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67  le to code trigg
2972d 65 72 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e  ers from */.  in
2972e 74 20 6e 65 77 49 64 78 2c 20 20 20 20 20 20 20  t newIdx,       
2972f 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 69 63 65     /* The indice
29730 20 6f 66 20 74 68 65 20 22 6e 65 77 22 20 72 6f   of the "new" ro
29731 77 20 74 6f 20 61 63 63 65 73 73 20 2a 2f 0a 20  w to access */. 
29732 20 69 6e 74 20 6f 6c 64 49 64 78 2c 20 20 20 20   int oldIdx,    
29733 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
29734 69 63 65 20 6f 66 20 74 68 65 20 22 6f 6c 64 22  ice of the "old"
29735 20 72 6f 77 20 74 6f 20 61 63 63 65 73 73 20 2a   row to access *
29736 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 2c 20  /.  int orconf, 
29737 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43           /* ON C
29738 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a  ONFLICT policy *
29739 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75  /.  int ignoreJu
2973a 6d 70 2c 20 20 20 20 20 20 2f 2a 20 49 6e 73 74  mp,      /* Inst
2973b 72 75 63 74 69 6f 6e 20 74 6f 20 6a 75 6d 70 20  ruction to jump 
2973c 74 6f 20 66 6f 72 20 52 41 49 53 45 28 49 47 4e  to for RAISE(IGN
2973d 4f 52 45 29 20 2a 2f 0a 20 20 75 33 32 20 2a 70  ORE) */.  u32 *p
2973e 69 4f 6c 64 43 6f 6c 4d 61 73 6b 2c 20 20 20 2f  iOldColMask,   /
2973f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 63  * OUT: Mask of c
29740 6f 6c 75 6d 6e 73 20 75 73 65 64 20 66 72 6f 6d  olumns used from
29741 20 74 68 65 20 4f 4c 44 2e 2a 20 74 61 62 6c 65   the OLD.* table
29742 20 2a 2f 0a 20 20 75 33 32 20 2a 70 69 4e 65 77   */.  u32 *piNew
29743 43 6f 6c 4d 61 73 6b 20 20 20 20 2f 2a 20 4f 55  ColMask    /* OU
29744 54 3a 20 4d 61 73 6b 20 6f 66 20 63 6f 6c 75 6d  T: Mask of colum
29745 6e 73 20 75 73 65 64 20 66 72 6f 6d 20 74 68 65  ns used from the
29746 20 4e 45 57 2e 2a 20 74 61 62 6c 65 20 2a 2f 0a   NEW.* table */.
29747 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b  ){.  Trigger *p;
29748 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
29749 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 54   pParse->db;.  T
2974a 72 69 67 67 65 72 53 74 61 63 6b 20 74 72 69 67  riggerStack trig
2974b 53 74 61 63 6b 45 6e 74 72 79 3b 0a 0a 20 20 74  StackEntry;..  t
2974c 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c  rigStackEntry.ol
2974d 64 43 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a 20 20  dColMask = 0;.  
2974e 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e  trigStackEntry.n
2974f 65 77 43 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a 0a  ewColMask = 0;..
29750 20 20 61 73 73 65 72 74 28 6f 70 20 3d 3d 20 54    assert(op == T
29751 4b 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 20 3d  K_UPDATE || op =
29752 3d 20 54 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f  = TK_INSERT || o
29753 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 3b  p == TK_DELETE);
29754 0a 20 20 61 73 73 65 72 74 28 74 72 5f 74 6d 20  .  assert(tr_tm 
29755 3d 3d 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  == TRIGGER_BEFOR
29756 45 20 7c 7c 20 74 72 5f 74 6d 20 3d 3d 20 54 52  E || tr_tm == TR
29757 49 47 47 45 52 5f 41 46 54 45 52 20 29 3b 0a 0a  IGGER_AFTER );..
29758 20 20 61 73 73 65 72 74 28 6e 65 77 49 64 78 20    assert(newIdx 
29759 21 3d 20 2d 31 20 7c 7c 20 6f 6c 64 49 64 78 20  != -1 || oldIdx 
2975a 21 3d 20 2d 31 29 3b 0a 0a 20 20 66 6f 72 28 70  != -1);..  for(p
2975b 3d 70 54 72 69 67 67 65 72 3b 20 70 3b 20 70 3d  =pTrigger; p; p=
2975c 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  p->pNext){.    i
2975d 6e 74 20 66 69 72 65 5f 74 68 69 73 20 3d 20 30  nt fire_this = 0
2975e 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65 72 6d  ;..    /* Determ
2975f 69 6e 65 20 77 68 65 74 68 65 72 20 77 65 20 73  ine whether we s
29760 68 6f 75 6c 64 20 63 6f 64 65 20 74 68 69 73 20  hould code this 
29761 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 69  trigger */.    i
29762 66 28 20 0a 20 20 20 20 20 20 70 2d 3e 6f 70 3d  f( .      p->op=
29763 3d 6f 70 20 26 26 20 0a 20 20 20 20 20 20 70 2d  =op && .      p-
29764 3e 74 72 5f 74 6d 3d 3d 74 72 5f 74 6d 20 26 26  >tr_tm==tr_tm &&
29765 20 0a 20 20 20 20 20 20 28 70 2d 3e 70 53 63 68   .      (p->pSch
29766 65 6d 61 3d 3d 70 2d 3e 70 54 61 62 53 63 68 65  ema==p->pTabSche
29767 6d 61 20 7c 7c 20 70 2d 3e 70 53 63 68 65 6d 61  ma || p->pSchema
29768 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ==db->aDb[1].pSc
29769 68 65 6d 61 29 20 26 26 0a 20 20 20 20 20 20 28  hema) &&.      (
2976a 6f 70 21 3d 54 4b 5f 55 50 44 41 54 45 7c 7c 21  op!=TK_UPDATE||!
2976b 70 2d 3e 70 43 6f 6c 75 6d 6e 73 7c 7c 63 68 65  p->pColumns||che
2976c 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70 28  ckColumnOverLap(
2976d 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 70 43 68 61  p->pColumns,pCha
2976e 6e 67 65 73 29 29 0a 20 20 20 20 29 7b 0a 20 20  nges)).    ){.  
2976f 20 20 20 20 54 72 69 67 67 65 72 53 74 61 63 6b      TriggerStack
29770 20 2a 70 53 3b 20 20 20 20 20 20 2f 2a 20 50 6f   *pS;      /* Po
29771 69 6e 74 65 72 20 74 6f 20 74 72 69 67 67 65 72  inter to trigger
29772 2d 73 74 61 63 6b 20 65 6e 74 72 79 20 2a 2f 0a  -stack entry */.
29773 20 20 20 20 20 20 66 6f 72 28 70 53 3d 70 50 61        for(pS=pPa
29774 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 20  rse->trigStack; 
29775 70 53 20 26 26 20 70 21 3d 70 53 2d 3e 70 54 72  pS && p!=pS->pTr
29776 69 67 67 65 72 3b 20 70 53 3d 70 53 2d 3e 70 4e  igger; pS=pS->pN
29777 65 78 74 29 7b 7d 0a 20 20 20 20 20 20 69 66 28  ext){}.      if(
29778 20 21 70 53 20 29 7b 0a 20 20 20 20 20 20 20 20   !pS ){.        
29779 66 69 72 65 5f 74 68 69 73 20 3d 20 31 3b 0a 20  fire_this = 1;. 
2977a 20 20 20 20 20 7d 0a 23 69 66 20 30 20 20 20 20       }.#if 0    
2977b 2f 2a 20 47 69 76 65 20 6e 6f 20 77 61 72 6e 69  /* Give no warni
2977c 6e 67 20 66 6f 72 20 72 65 63 75 72 73 69 76 65  ng for recursive
2977d 20 74 72 69 67 67 65 72 73 2e 20 20 4a 75 73 74   triggers.  Just
2977e 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 65 6d 20   do not do them 
2977f 2a 2f 0a 20 20 20 20 20 20 65 6c 73 65 7b 0a 20  */.      else{. 
29780 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
29781 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
29782 72 65 63 75 72 73 69 76 65 20 74 72 69 67 67 65  recursive trigge
29783 72 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64  rs not supported
29784 20 28 25 73 29 22 2c 0a 20 20 20 20 20 20 20 20   (%s)",.        
29785 20 20 20 20 70 2d 3e 6e 61 6d 65 29 3b 0a 20 20      p->name);.  
29786 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
29787 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
29788 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a   }.#endif.    }.
29789 20 0a 20 20 20 20 69 66 28 20 66 69 72 65 5f 74   .    if( fire_t
2978a 68 69 73 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  his ){.      int
2978b 20 65 6e 64 54 72 69 67 67 65 72 3b 0a 20 20 20   endTrigger;.   
2978c 20 20 20 45 78 70 72 20 2a 20 77 68 65 6e 45 78     Expr * whenEx
2978d 70 72 3b 0a 20 20 20 20 20 20 41 75 74 68 43 6f  pr;.      AuthCo
2978e 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a  ntext sContext;.
2978f 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78        NameContex
29790 74 20 73 4e 43 3b 0a 0a 23 69 66 6e 64 65 66 20  t sNC;..#ifndef 
29791 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
29792 45 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  E.      sqlite3V
29793 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65  dbeAddOp4(pParse
29794 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 54 72 61 63  ->pVdbe, OP_Trac
29795 65 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20  e, 0, 0, 0,.    
29796 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29797 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e      sqlite3MPrin
29798 74 66 28 64 62 2c 20 22 2d 2d 20 54 52 49 47 47  tf(db, "-- TRIGG
29799 45 52 20 25 73 22 2c 20 70 2d 3e 6e 61 6d 65 29  ER %s", p->name)
2979a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2979b 20 20 20 20 20 20 20 20 20 20 50 34 5f 44 59 4e            P4_DYN
2979c 41 4d 49 43 29 3b 0a 23 65 6e 64 69 66 0a 20 20  AMIC);.#endif.  
2979d 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c      memset(&sNC,
2979e 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
2979f 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72  ;.      sNC.pPar
297a0 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 0a 20 20  se = pParse;..  
297a1 20 20 20 20 2f 2a 20 50 75 73 68 20 61 6e 20 65      /* Push an e
297a2 6e 74 72 79 20 6f 6e 20 74 6f 20 74 68 65 20 74  ntry on to the t
297a3 72 69 67 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a  rigger stack */.
297a4 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b 45        trigStackE
297a5 6e 74 72 79 2e 70 54 72 69 67 67 65 72 20 3d 20  ntry.pTrigger = 
297a6 70 3b 0a 20 20 20 20 20 20 74 72 69 67 53 74 61  p;.      trigSta
297a7 63 6b 45 6e 74 72 79 2e 6e 65 77 49 64 78 20 3d  ckEntry.newIdx =
297a8 20 6e 65 77 49 64 78 3b 0a 20 20 20 20 20 20 74   newIdx;.      t
297a9 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c  rigStackEntry.ol
297aa 64 49 64 78 20 3d 20 6f 6c 64 49 64 78 3b 0a 20  dIdx = oldIdx;. 
297ab 20 20 20 20 20 74 72 69 67 53 74 61 63 6b 45 6e       trigStackEn
297ac 74 72 79 2e 70 54 61 62 20 3d 20 70 54 61 62 3b  try.pTab = pTab;
297ad 0a 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b  .      trigStack
297ae 45 6e 74 72 79 2e 70 4e 65 78 74 20 3d 20 70 50  Entry.pNext = pP
297af 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b  arse->trigStack;
297b0 0a 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b  .      trigStack
297b1 45 6e 74 72 79 2e 69 67 6e 6f 72 65 4a 75 6d 70  Entry.ignoreJump
297b2 20 3d 20 69 67 6e 6f 72 65 4a 75 6d 70 3b 0a 20   = ignoreJump;. 
297b3 20 20 20 20 20 70 50 61 72 73 65 2d 3e 74 72 69       pParse->tri
297b4 67 53 74 61 63 6b 20 3d 20 26 74 72 69 67 53 74  gStack = &trigSt
297b5 61 63 6b 45 6e 74 72 79 3b 0a 20 20 20 20 20 20  ackEntry;.      
297b6 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
297b7 78 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 26  xtPush(pParse, &
297b8 73 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 6e 61 6d  sContext, p->nam
297b9 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 63 6f  e);..      /* co
297ba 64 65 20 74 68 65 20 57 48 45 4e 20 63 6c 61 75  de the WHEN clau
297bb 73 65 20 2a 2f 0a 20 20 20 20 20 20 65 6e 64 54  se */.      endT
297bc 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33  rigger = sqlite3
297bd 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 70 50  VdbeMakeLabel(pP
297be 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20  arse->pVdbe);.  
297bf 20 20 20 20 77 68 65 6e 45 78 70 72 20 3d 20 73      whenExpr = s
297c0 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
297c1 2c 20 70 2d 3e 70 57 68 65 6e 2c 20 30 29 3b 0a  , p->pWhen, 0);.
297c2 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61        if( db->ma
297c3 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73 71  llocFailed || sq
297c4 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
297c5 4e 61 6d 65 73 28 26 73 4e 43 2c 20 77 68 65 6e  Names(&sNC, when
297c6 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20  Expr) ){.       
297c7 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61   pParse->trigSta
297c8 63 6b 20 3d 20 74 72 69 67 53 74 61 63 6b 45 6e  ck = trigStackEn
297c9 74 72 79 2e 70 4e 65 78 74 3b 0a 20 20 20 20 20  try.pNext;.     
297ca 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
297cb 6c 65 74 65 28 64 62 2c 20 77 68 65 6e 45 78 70  lete(db, whenExp
297cc 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  r);.        retu
297cd 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
297ce 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
297cf 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 77  fFalse(pParse, w
297d0 68 65 6e 45 78 70 72 2c 20 65 6e 64 54 72 69 67  henExpr, endTrig
297d1 67 65 72 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  ger, SQLITE_JUMP
297d2 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73  IFNULL);.      s
297d3 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
297d4 28 64 62 2c 20 77 68 65 6e 45 78 70 72 29 3b 0a  (db, whenExpr);.
297d5 0a 20 20 20 20 20 20 63 6f 64 65 54 72 69 67 67  .      codeTrigg
297d6 65 72 50 72 6f 67 72 61 6d 28 70 50 61 72 73 65  erProgram(pParse
297d7 2c 20 70 2d 3e 73 74 65 70 5f 6c 69 73 74 2c 20  , p->step_list, 
297d8 6f 72 63 6f 6e 66 29 3b 20 0a 0a 20 20 20 20 20  orconf); ..     
297d9 20 2f 2a 20 50 6f 70 20 74 68 65 20 65 6e 74 72   /* Pop the entr
297da 79 20 6f 66 66 20 74 68 65 20 74 72 69 67 67 65  y off the trigge
297db 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20 20 20  r stack */.     
297dc 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61   pParse->trigSta
297dd 63 6b 20 3d 20 74 72 69 67 53 74 61 63 6b 45 6e  ck = trigStackEn
297de 74 72 79 2e 70 4e 65 78 74 3b 0a 20 20 20 20 20  try.pNext;.     
297df 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
297e0 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78 74  extPop(&sContext
297e1 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
297e2 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
297e3 6c 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  l(pParse->pVdbe,
297e4 20 65 6e 64 54 72 69 67 67 65 72 29 3b 0a 20 20   endTrigger);.  
297e5 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 69    }.  }.  if( pi
297e6 4f 6c 64 43 6f 6c 4d 61 73 6b 20 29 20 2a 70 69  OldColMask ) *pi
297e7 4f 6c 64 43 6f 6c 4d 61 73 6b 20 7c 3d 20 74 72  OldColMask |= tr
297e8 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c 64  igStackEntry.old
297e9 43 6f 6c 4d 61 73 6b 3b 0a 20 20 69 66 28 20 70  ColMask;.  if( p
297ea 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 29 20 2a 70  iNewColMask ) *p
297eb 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 7c 3d 20 74  iNewColMask |= t
297ec 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e 65  rigStackEntry.ne
297ed 77 43 6f 6c 4d 61 73 6b 3b 0a 20 20 72 65 74 75  wColMask;.  retu
297ee 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 0;.}.#endif /
297ef 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
297f0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20  E_OMIT_TRIGGER) 
297f1 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
297f2 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 72 69 67 67  *** End of trigg
297f3 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
297f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297f6 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
297f7 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75  *** Begin file u
297f8 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  pdate.c ********
297f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297fb 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
297fc 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
297fd 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
297fe 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
297ff 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
29800 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
29801 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
29802 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
29803 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
29804 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
29805 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
29806 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
29807 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
29808 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
29809 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2980a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2980b 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2980c 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2980d 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2980e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2980f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29811 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29812 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
29813 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
29814 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73   C code routines
29815 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64   that are called
29816 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a   by the parser.*
29817 2a 20 74 6f 20 68 61 6e 64 6c 65 20 55 50 44 41  * to handle UPDA
29818 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  TE statements..*
29819 2a 0a 2a 2a 20 24 49 64 3a 20 75 70 64 61 74 65  *.** $Id: update
2981a 2e 63 2c 76 20 31 2e 31 39 36 20 32 30 30 39 2f  .c,v 1.196 2009/
2981b 30 32 2f 32 38 20 31 30 3a 34 37 3a 34 32 20 64  02/28 10:47:42 d
2981c 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
2981d 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
2981e 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2981f 54 41 42 4c 45 0a 2f 2a 20 46 6f 72 77 61 72 64  TABLE./* Forward
29820 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a   declaration */.
29821 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61  static void upda
29822 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0a  teVirtualTable(.
29823 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
29824 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
29825 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
29826 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
29827 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  ,       /* The v
29828 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20  irtual table to 
29829 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20  be modified */. 
2982a 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2982b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
2982c 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
2982d 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
2982e 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75  es,  /* The colu
2982f 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e  mns to change in
29830 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74   the UPDATE stat
29831 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  ement */.  Expr 
29832 2a 70 52 6f 77 69 64 45 78 70 72 2c 20 20 20 20  *pRowidExpr,    
29833 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73  /* Expression us
29834 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20  ed to recompute 
29835 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  the rowid */.  i
29836 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20  nt *aXRef,      
29837 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66      /* Mapping f
29838 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70  rom columns of p
29839 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69  Tab to entries i
2983a 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20  n pChanges */.  
2983b 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20  Expr *pWhere    
2983c 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c       /* WHERE cl
2983d 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41  ause of the UPDA
2983e 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  TE statement */.
2983f 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  );.#endif /* SQL
29840 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
29841 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  TABLE */../*.** 
29842 54 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  The most recentl
29843 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74  y coded instruct
29844 69 6f 6e 20 77 61 73 20 61 6e 20 4f 50 5f 43 6f  ion was an OP_Co
29845 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65  lumn to retrieve
29846 20 74 68 65 0a 2a 2a 20 69 2d 74 68 20 63 6f 6c   the.** i-th col
29847 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 70 54 61  umn of table pTa
29848 62 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  b. This routine 
29849 73 65 74 73 20 74 68 65 20 50 34 20 70 61 72 61  sets the P4 para
2984a 6d 65 74 65 72 20 6f 66 20 74 68 65 20 0a 2a 2a  meter of the .**
2984b 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74 68   OP_Column to th
2984c 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2c  e default value,
2984d 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54   if any..**.** T
2984e 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2984f 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20   of a column is 
29850 73 70 65 63 69 66 69 65 64 20 62 79 20 61 20 44  specified by a D
29851 45 46 41 55 4c 54 20 63 6c 61 75 73 65 20 69 6e  EFAULT clause in
29852 20 74 68 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20   the .** column 
29853 64 65 66 69 6e 69 74 69 6f 6e 2e 20 54 68 69 73  definition. This
29854 20 77 61 73 20 65 69 74 68 65 72 20 73 75 70 70   was either supp
29855 6c 69 65 64 20 62 79 20 74 68 65 20 75 73 65 72  lied by the user
29856 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 0a   when the table.
29857 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2c 20  ** was created, 
29858 6f 72 20 61 64 64 65 64 20 6c 61 74 65 72 20 74  or added later t
29859 6f 20 74 68 65 20 74 61 62 6c 65 20 64 65 66 69  o the table defi
2985a 6e 69 74 69 6f 6e 20 62 79 20 61 6e 20 41 4c 54  nition by an ALT
2985b 45 52 20 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d 6d  ER TABLE.** comm
2985c 61 6e 64 2e 20 49 66 20 74 68 65 20 6c 61 74 74  and. If the latt
2985d 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  er, then the row
2985e 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20  -records in the 
2985f 74 61 62 6c 65 20 62 74 72 65 65 20 6f 6e 20 64  table btree on d
29860 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 63  isk.** may not c
29861 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20 66  ontain a value f
29862 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e  or the column an
29863 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  d the default va
29864 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66 72  lue, taken.** fr
29865 6f 6d 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  om the P4 parame
29866 74 65 72 20 6f 66 20 74 68 65 20 4f 50 5f 43 6f  ter of the OP_Co
29867 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e  lumn instruction
29868 2c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  , is returned in
29869 73 74 65 61 64 2e 0a 2a 2a 20 49 66 20 74 68 65  stead..** If the
2986a 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e 20 61 6c   former, then al
2986b 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 61 72  l row-records ar
2986c 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  e guaranteed to 
2986d 69 6e 63 6c 75 64 65 20 61 20 76 61 6c 75 65 0a  include a value.
2986e 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ** for the colum
2986f 6e 20 61 6e 64 20 74 68 65 20 50 34 20 76 61 6c  n and the P4 val
29870 75 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  ue is not requir
29871 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e  ed..**.** Column
29872 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 63 72 65   definitions cre
29873 61 74 65 64 20 62 79 20 61 6e 20 41 4c 54 45 52  ated by an ALTER
29874 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 6d   TABLE command m
29875 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 0a 2a 2a  ay only have .**
29876 20 6c 69 74 65 72 61 6c 20 64 65 66 61 75 6c 74   literal default
29877 20 76 61 6c 75 65 73 20 73 70 65 63 69 66 69 65   values specifie
29878 64 3a 20 61 20 6e 75 6d 62 65 72 2c 20 6e 75 6c  d: a number, nul
29879 6c 20 6f 72 20 61 20 73 74 72 69 6e 67 2e 20 28  l or a string. (
2987a 49 66 20 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f 6d  If a more.** com
2987b 70 6c 69 63 61 74 65 64 20 64 65 66 61 75 6c 74  plicated default
2987c 20 65 78 70 72 65 73 73 69 6f 6e 20 76 61 6c 75   expression valu
2987d 65 20 77 61 73 20 70 72 6f 76 69 64 65 64 2c 20  e was provided, 
2987e 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 20  it is evaluated 
2987f 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 4c 54  .** when the ALT
29880 45 52 20 54 41 42 4c 45 20 69 73 20 65 78 65 63  ER TABLE is exec
29881 75 74 65 64 20 61 6e 64 20 6f 6e 65 20 6f 66 20  uted and one of 
29882 74 68 65 20 6c 69 74 65 72 61 6c 20 76 61 6c 75  the literal valu
29883 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e  es written.** in
29884 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  to the sqlite_ma
29885 73 74 65 72 20 74 61 62 6c 65 2e 29 0a 2a 2a 0a  ster table.).**.
29886 2a 2a 20 54 68 65 72 65 66 6f 72 65 2c 20 74 68  ** Therefore, th
29887 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69  e P4 parameter i
29888 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20  s only required 
29889 69 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  if the default v
2988a 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20  alue for.** the 
2988b 63 6f 6c 75 6d 6e 20 69 73 20 61 20 6c 69 74 65  column is a lite
2988c 72 61 6c 20 6e 75 6d 62 65 72 2c 20 73 74 72 69  ral number, stri
2988d 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20 54 68 65 20  ng or null. The 
2988e 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d  sqlite3ValueFrom
2988f 45 78 70 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69  Expr().** functi
29890 6f 6e 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66  on is capable of
29891 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68   transforming th
29892 65 73 65 20 74 79 70 65 73 20 6f 66 20 65 78 70  ese types of exp
29893 72 65 73 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a  ressions into.**
29894 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
29895 62 6a 65 63 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  bjects..*/.SQLIT
29896 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
29897 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
29898 75 6c 74 28 56 64 62 65 20 2a 76 2c 20 54 61 62  ult(Vdbe *v, Tab
29899 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 69 29  le *pTab, int i)
2989a 7b 0a 20 20 69 66 28 20 70 54 61 62 20 26 26 20  {.  if( pTab && 
2989b 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29  !pTab->pSelect )
2989c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  {.    sqlite3_va
2989d 6c 75 65 20 2a 70 56 61 6c 75 65 3b 0a 20 20 20  lue *pValue;.   
2989e 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 73 71   u8 enc = ENC(sq
2989f 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 29 3b  lite3VdbeDb(v));
298a0 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  .    Column *pCo
298a1 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  l = &pTab->aCol[
298a2 69 5d 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  i];.    VdbeComm
298a3 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73 22 2c  ent((v, "%s.%s",
298a4 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 43   pTab->zName, pC
298a5 6f 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  ol->zName));.   
298a6 20 61 73 73 65 72 74 28 20 69 3c 70 54 61 62 2d   assert( i<pTab-
298a7 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 73 71 6c  >nCol );.    sql
298a8 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
298a9 72 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28  r(sqlite3VdbeDb(
298aa 76 29 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 2c  v), pCol->pDflt,
298ab 20 65 6e 63 2c 20 0a 20 20 20 20 20 20 20 20 20   enc, .         
298ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
298ad 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2c 20  pCol->affinity, 
298ae 26 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 69 66  &pValue);.    if
298af 28 20 70 56 61 6c 75 65 20 29 7b 0a 20 20 20 20  ( pValue ){.    
298b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
298b1 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f  ngeP4(v, -1, (co
298b2 6e 73 74 20 63 68 61 72 20 2a 29 70 56 61 6c 75  nst char *)pValu
298b3 65 2c 20 50 34 5f 4d 45 4d 29 3b 0a 20 20 20 20  e, P4_MEM);.    
298b4 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  }.  }.}../*.** P
298b5 72 6f 63 65 73 73 20 61 6e 20 55 50 44 41 54 45  rocess an UPDATE
298b6 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
298b7 2a 20 20 20 55 50 44 41 54 45 20 4f 52 20 49 47  *   UPDATE OR IG
298b8 4e 4f 52 45 20 74 61 62 6c 65 5f 77 78 79 7a 20  NORE table_wxyz 
298b9 53 45 54 20 61 3d 62 2c 20 63 3d 64 20 57 48 45  SET a=b, c=d WHE
298ba 52 45 20 65 3c 35 20 41 4e 44 20 66 20 4e 4f 54  RE e<5 AND f NOT
298bb 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20   NULL;.**       
298bc 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f 5f     \_______/ \__
298bd 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f  ______/     \___
298be 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f  ___/       \____
298bf 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 20  ____________/.* 
298c0 20 20 20 20 20 20 20 20 20 20 20 6f 6e 45 72 72             onErr
298c1 6f 72 20 20 20 70 54 61 62 4c 69 73 74 20 20 20  or   pTabList   
298c2 20 20 20 70 43 68 61 6e 67 65 73 20 20 20 20 20     pChanges     
298c3 20 20 20 20 20 20 20 20 70 57 68 65 72 65 0a 2a          pWhere.*
298c4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
298c5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 70 64   void sqlite3Upd
298c6 61 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ate(.  Parse *pP
298c7 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  arse,         /*
298c8 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
298c9 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
298ca 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 20   *pTabList,     
298cb 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69 6e 20  /* The table in 
298cc 77 68 69 63 68 20 77 65 20 73 68 6f 75 6c 64 20  which we should 
298cd 63 68 61 6e 67 65 20 74 68 69 6e 67 73 20 2a 2f  change things */
298ce 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68  .  ExprList *pCh
298cf 61 6e 67 65 73 2c 20 20 20 20 2f 2a 20 54 68 69  anges,    /* Thi
298d0 6e 67 73 20 74 6f 20 62 65 20 63 68 61 6e 67 65  ngs to be change
298d1 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  d */.  Expr *pWh
298d2 65 72 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ere,          /*
298d3 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
298d4 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
298d5 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
298d6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
298d7 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e  ow to handle con
298d8 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a  straint errors *
298d9 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  /.){.  int i, j;
298da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
298db 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
298dc 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
298dd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
298de 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 75 70  e table to be up
298df 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61  dated */.  int a
298e0 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ddr = 0;        
298e1 20 20 2f 2a 20 56 44 42 45 20 69 6e 73 74 72 75    /* VDBE instru
298e2 63 74 69 6f 6e 20 61 64 64 72 65 73 73 20 6f 66  ction address of
298e3 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
298e4 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68 65 72  e loop */.  Wher
298e5 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20  eInfo *pWInfo;  
298e6 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
298e7 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 52  n about the WHER
298e8 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 56 64  E clause */.  Vd
298e9 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
298ea 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74       /* The virt
298eb 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67  ual database eng
298ec 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  ine */.  Index *
298ed 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  pIdx;           
298ee 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
298ef 76 65 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20  ver indices */. 
298f0 20 69 6e 74 20 6e 49 64 78 3b 20 20 20 20 20 20   int nIdx;      
298f1 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
298f2 72 20 6f 66 20 69 6e 64 69 63 65 73 20 74 68 61  r of indices tha
298f3 74 20 6e 65 65 64 20 75 70 64 61 74 69 6e 67 20  t need updating 
298f4 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20  */.  int iCur;  
298f5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
298f6 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65  DBE Cursor numbe
298f7 72 20 6f 66 20 70 54 61 62 20 2a 2f 0a 20 20 73  r of pTab */.  s
298f8 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
298f9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
298fa 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20  abase structure 
298fb 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64  */.  int *aRegId
298fc 78 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f  x = 0;      /* O
298fd 6e 65 20 72 65 67 69 73 74 65 72 20 61 73 73 69  ne register assi
298fe 67 6e 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64  gned to each ind
298ff 65 78 20 74 6f 20 62 65 20 75 70 64 61 74 65 64  ex to be updated
29900 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66   */.  int *aXRef
29901 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
29902 61 58 52 65 66 5b 69 5d 20 69 73 20 74 68 65 20  aXRef[i] is the 
29903 69 6e 64 65 78 20 69 6e 20 70 43 68 61 6e 67 65  index in pChange
29904 73 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 0a 20 20  s->a[] of the.  
29905 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29906 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70         ** an exp
29907 72 65 73 73 69 6f 6e 20 66 6f 72 20 74 68 65 20  ression for the 
29908 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  i-th column of t
29909 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 20 20  he table..      
2990a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2990b 20 20 20 2a 2a 20 61 58 52 65 66 5b 69 5d 3d 3d     ** aXRef[i]==
2990c 2d 31 20 69 66 20 74 68 65 20 69 2d 74 68 20 63  -1 if the i-th c
2990d 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 63 68 61  olumn is not cha
2990e 6e 67 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 63  nged. */.  int c
2990f 68 6e 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20  hngRowid;       
29910 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
29911 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69   record number i
29912 73 20 62 65 69 6e 67 20 63 68 61 6e 67 65 64 20  s being changed 
29913 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69  */.  Expr *pRowi
29914 64 45 78 70 72 20 3d 20 30 3b 20 20 2f 2a 20 45  dExpr = 0;  /* E
29915 78 70 72 65 73 73 69 6f 6e 20 64 65 66 69 6e 69  xpression defini
29916 6e 67 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  ng the new recor
29917 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e  d number */.  in
29918 74 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 20 20  t openAll = 0;  
29919 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2991a 61 6c 6c 20 69 6e 64 69 63 65 73 20 6e 65 65 64  all indices need
2991b 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f   to be opened */
2991c 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20 73  .  AuthContext s
2991d 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 54 68 65  Context;  /* The
2991e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
2991f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65  ontext */.  Name
29920 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
29921 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 2d 63     /* The name-c
29922 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 6f 6c 76  ontext to resolv
29923 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  e expressions in
29924 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
29925 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29926 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  Database contain
29927 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 62 65  ing the table be
29928 69 6e 67 20 75 70 64 61 74 65 64 20 2a 2f 0a 20  ing updated */. 
29929 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20   int j1;        
2992a 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2992b 73 73 65 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73  sses of jump ins
2992c 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  tructions */.  i
2992d 6e 74 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20  nt okOnePass;   
2992e 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
2992f 72 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72  r one-pass algor
29930 69 74 68 6d 20 77 69 74 68 6f 75 74 20 74 68 65  ithm without the
29931 20 46 49 46 4f 20 2a 2f 0a 0a 23 69 66 6e 64 65   FIFO */..#ifnde
29932 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
29933 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69  IGGER.  int isVi
29934 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ew;             
29935 20 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74       /* Trying t
29936 6f 20 75 70 64 61 74 65 20 61 20 76 69 65 77 20  o update a view 
29937 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
29938 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 20  rigger;         
29939 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69    /* List of tri
2993a 67 67 65 72 73 20 6f 6e 20 70 54 61 62 2c 20 69  ggers on pTab, i
2993b 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 23 65  f required */.#e
2993c 6e 64 69 66 0a 20 20 69 6e 74 20 69 42 65 67 69  ndif.  int iBegi
2993d 6e 41 66 74 65 72 54 72 69 67 67 65 72 20 3d 20  nAfterTrigger = 
2993e 30 3b 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f  0;  /* Address o
2993f 66 20 61 66 74 65 72 20 74 72 69 67 67 65 72 20  f after trigger 
29940 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74  program */.  int
29941 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 67 65   iEndAfterTrigge
29942 72 20 3d 20 30 3b 20 20 20 20 2f 2a 20 45 78 69  r = 0;    /* Exi
29943 74 20 6f 66 20 61 66 74 65 72 20 74 72 69 67 67  t of after trigg
29944 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  er program */.  
29945 69 6e 74 20 69 42 65 67 69 6e 42 65 66 6f 72 65  int iBeginBefore
29946 54 72 69 67 67 65 72 20 3d 20 30 3b 20 2f 2a 20  Trigger = 0; /* 
29947 41 64 64 72 65 73 73 20 6f 66 20 62 65 66 6f 72  Address of befor
29948 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  e trigger progra
29949 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 42  m */.  int iEndB
2994a 65 66 6f 72 65 54 72 69 67 67 65 72 20 3d 20 30  eforeTrigger = 0
2994b 3b 20 20 20 2f 2a 20 45 78 69 74 20 6f 66 20 62  ;   /* Exit of b
2994c 65 66 6f 72 65 20 74 72 69 67 67 65 72 20 70 72  efore trigger pr
2994d 6f 67 72 61 6d 20 2a 2f 0a 20 20 75 33 32 20 6f  ogram */.  u32 o
2994e 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 3d 20 30 3b  ld_col_mask = 0;
2994f 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
29950 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73  of OLD.* columns
29951 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 75 33 32   in use */.  u32
29952 20 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 20 3d 20   new_col_mask = 
29953 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73  0;        /* Mas
29954 6b 20 6f 66 20 4e 45 57 2e 2a 20 63 6f 6c 75 6d  k of NEW.* colum
29955 6e 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a 20 20  ns in use */..  
29956 69 6e 74 20 6e 65 77 49 64 78 20 20 20 20 20 20  int newIdx      
29957 3d 20 2d 31 3b 20 20 2f 2a 20 69 6e 64 65 78 20  = -1;  /* index 
29958 6f 66 20 74 72 69 67 67 65 72 20 22 6e 65 77 22  of trigger "new"
29959 20 74 65 6d 70 20 74 61 62 6c 65 20 20 20 20 20   temp table     
2995a 20 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64    */.  int oldId
2995b 78 20 20 20 20 20 20 3d 20 2d 31 3b 20 20 2f 2a  x      = -1;  /*
2995c 20 69 6e 64 65 78 20 6f 66 20 74 72 69 67 67 65   index of trigge
2995d 72 20 22 6f 6c 64 22 20 74 65 6d 70 20 74 61 62  r "old" temp tab
2995e 6c 65 20 20 20 20 20 20 20 2a 2f 0a 0a 20 20 2f  le       */..  /
2995f 2a 20 52 65 67 69 73 74 65 72 20 41 6c 6c 6f 63  * Register Alloc
29960 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ations */.  int 
29961 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b  regRowCount = 0;
29962 20 20 20 2f 2a 20 41 20 63 6f 75 6e 74 20 6f 66     /* A count of
29963 20 72 6f 77 73 20 63 68 61 6e 67 65 64 20 2a 2f   rows changed */
29964 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77  .  int regOldRow
29965 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  id;       /* The
29966 20 6f 6c 64 20 72 6f 77 69 64 20 2a 2f 0a 20 20   old rowid */.  
29967 69 6e 74 20 72 65 67 4e 65 77 52 6f 77 69 64 3b  int regNewRowid;
29968 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
29969 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  w rowid */.  int
2996a 20 72 65 67 44 61 74 61 3b 20 20 20 20 20 20 20   regData;       
2996b 20 20 20 20 2f 2a 20 4e 65 77 20 64 61 74 61 20      /* New data 
2996c 66 6f 72 20 74 68 65 20 72 6f 77 20 2a 2f 0a 20  for the row */. 
2996d 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74 20 3d   int regRowSet =
2996e 20 30 3b 20 20 20 20 20 2f 2a 20 52 6f 77 73 65   0;     /* Rowse
2996f 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 62 65 20  t of rows to be 
29970 75 70 64 61 74 65 64 20 2a 2f 0a 0a 20 20 73 43  updated */..  sC
29971 6f 6e 74 65 78 74 2e 70 50 61 72 73 65 20 3d 20  ontext.pParse = 
29972 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  0;.  db = pParse
29973 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72  ->db;.  if( pPar
29974 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
29975 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
29976 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
29977 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
29978 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d  ssert( pTabList-
29979 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f  >nSrc==1 );..  /
2997a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  * Locate the tab
2997b 6c 65 20 77 68 69 63 68 20 77 65 20 77 61 6e 74  le which we want
2997c 20 74 6f 20 75 70 64 61 74 65 2e 20 0a 20 20 2a   to update. .  *
2997d 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  /.  pTab = sqlit
2997e 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
2997f 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
29980 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  );.  if( pTab==0
29981 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63   ) goto update_c
29982 6c 65 61 6e 75 70 3b 0a 20 20 69 44 62 20 3d 20  leanup;.  iDb = 
29983 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
29984 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
29985 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
29986 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
29987 74 20 69 66 20 77 65 20 68 61 76 65 20 61 6e 79  t if we have any
29988 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 69 66   triggers and if
29989 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
2998a 0a 20 20 2a 2a 20 75 70 64 61 74 65 64 20 69 73  .  ** updated is
2998b 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66   a view.  */.#if
2998c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2998d 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72 69 67  _TRIGGER.  pTrig
2998e 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ger = sqlite3Tri
2998f 67 67 65 72 73 45 78 69 73 74 28 70 50 61 72 73  ggersExist(pPars
29990 65 2c 20 70 54 61 62 2c 20 54 4b 5f 55 50 44 41  e, pTab, TK_UPDA
29991 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 30 29  TE, pChanges, 0)
29992 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54 61  ;.  isView = pTa
29993 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23  b->pSelect!=0;.#
29994 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70 54  else.# define pT
29995 72 69 67 67 65 72 20 30 0a 23 20 64 65 66 69 6e  rigger 0.# defin
29996 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
29997 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
29998 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65  OMIT_VIEW.# unde
29999 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e  f isView.# defin
2999a 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69  e isView 0.#endi
2999b 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  f..  if( sqlite3
2999c 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73  IsReadOnly(pPars
2999d 65 2c 20 70 54 61 62 2c 20 28 70 54 72 69 67 67  e, pTab, (pTrigg
2999e 65 72 3f 31 3a 30 29 29 20 29 7b 0a 20 20 20 20  er?1:0)) ){.    
2999f 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
299a0 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  nup;.  }.  if( s
299a1 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
299a2 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
299a3 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f   pTab) ){.    go
299a4 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
299a5 70 3b 0a 20 20 7d 0a 20 20 61 58 52 65 66 20 3d  p;.  }.  aXRef =
299a6 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
299a7 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
299a8 6e 74 29 20 2a 20 70 54 61 62 2d 3e 6e 43 6f 6c  nt) * pTab->nCol
299a9 20 29 3b 0a 20 20 69 66 28 20 61 58 52 65 66 3d   );.  if( aXRef=
299aa 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  =0 ) goto update
299ab 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 66 6f 72 28  _cleanup;.  for(
299ac 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
299ad 6c 3b 20 69 2b 2b 29 20 61 58 52 65 66 5b 69 5d  l; i++) aXRef[i]
299ae 20 3d 20 2d 31 3b 0a 0a 20 20 2f 2a 20 49 66 20   = -1;..  /* If 
299af 74 68 65 72 65 20 61 72 65 20 46 4f 52 20 45 41  there are FOR EA
299b0 43 48 20 52 4f 57 20 74 72 69 67 67 65 72 73 2c  CH ROW triggers,
299b1 20 61 6c 6c 6f 63 61 74 65 20 63 75 72 73 6f 72   allocate cursor
299b2 73 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73  s for the.  ** s
299b3 70 65 63 69 61 6c 20 4f 4c 44 20 61 6e 64 20 4e  pecial OLD and N
299b4 45 57 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20  EW tables.  */. 
299b5 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b   if( pTrigger ){
299b6 0a 20 20 20 20 6e 65 77 49 64 78 20 3d 20 70 50  .    newIdx = pP
299b7 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
299b8 20 20 6f 6c 64 49 64 78 20 3d 20 70 50 61 72 73    oldIdx = pPars
299b9 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a  e->nTab++;.  }..
299ba 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20    /* Allocate a 
299bb 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20  cursors for the 
299bc 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 74 61  main database ta
299bd 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c 6c 20  ble and for all 
299be 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 20 54 68  indices..  ** Th
299bf 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20  e index cursors 
299c0 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 75 73 65  might not be use
299c1 64 2c 20 62 75 74 20 69 66 20 74 68 65 79 20 61  d, but if they a
299c2 72 65 20 75 73 65 64 20 74 68 65 79 0a 20 20 2a  re used they.  *
299c3 2a 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 72 20  * need to occur 
299c4 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20  right after the 
299c5 64 61 74 61 62 61 73 65 20 63 75 72 73 6f 72 2e  database cursor.
299c6 20 20 53 6f 20 67 6f 20 61 68 65 61 64 20 61 6e    So go ahead an
299c7 64 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20  d.  ** allocate 
299c8 65 6e 6f 75 67 68 20 73 70 61 63 65 2c 20 6a 75  enough space, ju
299c9 73 74 20 69 6e 20 63 61 73 65 2e 0a 20 20 2a 2f  st in case..  */
299ca 0a 20 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30  .  pTabList->a[0
299cb 5d 2e 69 43 75 72 73 6f 72 20 3d 20 69 43 75 72  ].iCursor = iCur
299cc 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
299cd 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54  +;.  for(pIdx=pT
299ce 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
299cf 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
299d0 78 74 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  xt){.    pParse-
299d1 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20  >nTab++;.  }..  
299d2 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
299d3 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 2a  e name-context *
299d4 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c  /.  memset(&sNC,
299d5 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
299d6 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d  ;.  sNC.pParse =
299d7 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70   pParse;.  sNC.p
299d8 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69  SrcList = pTabLi
299d9 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76  st;..  /* Resolv
299da 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  e the column nam
299db 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20 65 78  es in all the ex
299dc 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65  pressions of the
299dd 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 55 50 44  .  ** of the UPD
299de 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ATE statement.  
299df 41 6c 73 6f 20 66 69 6e 64 20 74 68 65 20 63 6f  Also find the co
299e0 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20  lumn index.  ** 
299e1 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
299e2 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 69 6e  to be updated in
299e3 20 74 68 65 20 70 43 68 61 6e 67 65 73 20 61 72   the pChanges ar
299e4 72 61 79 2e 20 20 46 6f 72 20 65 61 63 68 0a 20  ray.  For each. 
299e5 20 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65   ** column to be
299e6 20 75 70 64 61 74 65 64 2c 20 6d 61 6b 65 20 73   updated, make s
299e7 75 72 65 20 77 65 20 68 61 76 65 20 61 75 74 68  ure we have auth
299e8 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63 68 61  orization to cha
299e9 6e 67 65 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f  nge.  ** that co
299ea 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 6e  lumn..  */.  chn
299eb 67 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20 66 6f  gRowid = 0;.  fo
299ec 72 28 69 3d 30 3b 20 69 3c 70 43 68 61 6e 67 65  r(i=0; i<pChange
299ed 73 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  s->nExpr; i++){.
299ee 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
299ef 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
299f0 26 73 4e 43 2c 20 70 43 68 61 6e 67 65 73 2d 3e  &sNC, pChanges->
299f1 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20  a[i].pExpr) ){. 
299f2 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65       goto update
299f3 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
299f4 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
299f5 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  Tab->nCol; j++){
299f6 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
299f7 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e  e3StrICmp(pTab->
299f8 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70  aCol[j].zName, p
299f9 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e  Changes->a[i].zN
299fa 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ame)==0 ){.     
299fb 20 20 20 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e     if( j==pTab->
299fc 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  iPKey ){.       
299fd 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 31     chngRowid = 1
299fe 3b 0a 20 20 20 20 20 20 20 20 20 20 70 52 6f 77  ;.          pRow
299ff 69 64 45 78 70 72 20 3d 20 70 43 68 61 6e 67 65  idExpr = pChange
29a00 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  s->a[i].pExpr;. 
29a01 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
29a02 20 61 58 52 65 66 5b 6a 5d 20 3d 20 69 3b 0a 20   aXRef[j] = i;. 
29a03 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
29a04 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
29a05 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f  if( j>=pTab->nCo
29a06 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  l ){.      if( s
29a07 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70 43  qlite3IsRowid(pC
29a08 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61  hanges->a[i].zNa
29a09 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63  me) ){.        c
29a0a 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20  hngRowid = 1;.  
29a0b 20 20 20 20 20 20 70 52 6f 77 69 64 45 78 70 72        pRowidExpr
29a0c 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69   = pChanges->a[i
29a0d 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 7d  ].pExpr;.      }
29a0e 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
29a0f 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
29a10 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63  arse, "no such c
29a11 6f 6c 75 6d 6e 3a 20 25 73 22 2c 20 70 43 68 61  olumn: %s", pCha
29a12 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  nges->a[i].zName
29a13 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
29a14 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
29a15 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69        }.    }.#i
29a16 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29a17 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
29a18 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20      {.      int 
29a19 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  rc;.      rc = s
29a1a 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
29a1b 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 55  pParse, SQLITE_U
29a1c 50 44 41 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61  PDATE, pTab->zNa
29a1d 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  me,.            
29a1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
29a1f 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  Tab->aCol[j].zNa
29a20 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  me, db->aDb[iDb]
29a21 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69  .zName);.      i
29a22 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45  f( rc==SQLITE_DE
29a23 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  NY ){.        go
29a24 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
29a25 70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  p;.      }else i
29a26 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47  f( rc==SQLITE_IG
29a27 4e 4f 52 45 20 29 7b 0a 20 20 20 20 20 20 20 20  NORE ){.        
29a28 61 58 52 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a 20  aXRef[j] = -1;. 
29a29 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
29a2a 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c  dif.  }..  /* Al
29a2b 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f  locate memory fo
29a2c 72 20 74 68 65 20 61 72 72 61 79 20 61 52 65 67  r the array aReg
29a2d 49 64 78 5b 5d 2e 20 20 54 68 65 72 65 20 69 73  Idx[].  There is
29a2e 20 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20 74 68   one entry in th
29a2f 65 0a 20 20 2a 2a 20 61 72 72 61 79 20 66 6f 72  e.  ** array for
29a30 20 65 61 63 68 20 69 6e 64 65 78 20 61 73 73 6f   each index asso
29a31 63 69 61 74 65 64 20 77 69 74 68 20 74 61 62 6c  ciated with tabl
29a32 65 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e  e being updated.
29a33 20 20 46 69 6c 6c 20 69 6e 0a 20 20 2a 2a 20 74    Fill in.  ** t
29a34 68 65 20 76 61 6c 75 65 20 77 69 74 68 20 61 20  he value with a 
29a35 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20  register number 
29a36 66 6f 72 20 69 6e 64 69 63 65 73 20 74 68 61 74  for indices that
29a37 20 61 72 65 20 74 6f 20 62 65 20 75 73 65 64 0a   are to be used.
29a38 20 20 2a 2a 20 61 6e 64 20 77 69 74 68 20 7a 65    ** and with ze
29a39 72 6f 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e  ro for unused in
29a3a 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f  dices..  */.  fo
29a3b 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78 3d 70  r(nIdx=0, pIdx=p
29a3c 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
29a3d 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
29a3e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20  ext, nIdx++){}. 
29a3f 20 69 66 28 20 6e 49 64 78 3e 30 20 29 7b 0a 20   if( nIdx>0 ){. 
29a40 20 20 20 61 52 65 67 49 64 78 20 3d 20 73 71 6c     aRegIdx = sql
29a41 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
29a42 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e 64 65 78  db, sizeof(Index
29a43 2a 29 20 2a 20 6e 49 64 78 20 29 3b 0a 20 20 20  *) * nIdx );.   
29a44 20 69 66 28 20 61 52 65 67 49 64 78 3d 3d 30 20   if( aRegIdx==0 
29a45 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c  ) goto update_cl
29a46 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 66 6f 72  eanup;.  }.  for
29a47 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d  (j=0, pIdx=pTab-
29a48 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
29a49 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c  Idx=pIdx->pNext,
29a4a 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72   j++){.    int r
29a4b 65 67 3b 0a 20 20 20 20 69 66 28 20 63 68 6e 67  eg;.    if( chng
29a4c 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72  Rowid ){.      r
29a4d 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  eg = ++pParse->n
29a4e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Mem;.    }else{.
29a4f 20 20 20 20 20 20 72 65 67 20 3d 20 30 3b 0a 20        reg = 0;. 
29a50 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
29a51 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
29a52 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
29a53 20 61 58 52 65 66 5b 70 49 64 78 2d 3e 61 69 43   aXRef[pIdx->aiC
29a54 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 7b 0a  olumn[i]]>=0 ){.
29a55 20 20 20 20 20 20 20 20 20 20 72 65 67 20 3d 20            reg = 
29a56 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
29a57 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
29a58 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29a59 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 52 65   }.    }.    aRe
29a5a 67 49 64 78 5b 6a 5d 20 3d 20 72 65 67 3b 0a 20  gIdx[j] = reg;. 
29a5b 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74   }..  /* Allocat
29a5c 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 72 65 67  e a block of reg
29a5d 69 73 74 65 72 20 75 73 65 64 20 74 6f 20 73 74  ister used to st
29a5e 6f 72 65 20 74 68 65 20 63 68 61 6e 67 65 20 72  ore the change r
29a5f 65 63 6f 72 64 0a 20 20 2a 2a 20 73 65 6e 74 20  ecord.  ** sent 
29a60 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  to sqlite3Genera
29a61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
29a62 6b 73 28 29 2e 20 20 54 68 65 72 65 20 61 72 65  ks().  There are
29a63 20 65 69 74 68 65 72 0a 20 20 2a 2a 20 6f 6e 65   either.  ** one
29a64 20 6f 72 20 74 77 6f 20 72 65 67 69 73 74 65 72   or two register
29a65 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68  s for holding th
29a66 65 20 72 6f 77 69 64 2e 20 20 4f 6e 65 20 72 6f  e rowid.  One ro
29a67 77 69 64 20 72 65 67 69 73 74 65 72 0a 20 20 2a  wid register.  *
29a68 2a 20 69 73 20 75 73 65 64 20 69 66 20 63 68 6e  * is used if chn
29a69 67 52 6f 77 69 64 20 69 73 20 66 61 6c 73 65 20  gRowid is false 
29a6a 61 6e 64 20 74 77 6f 20 61 72 65 20 75 73 65 64  and two are used
29a6b 20 69 66 20 63 68 6e 67 52 6f 77 69 64 20 69 73   if chngRowid is
29a6c 0a 20 20 2a 2a 20 74 72 75 65 2e 20 20 46 6f 6c  .  ** true.  Fol
29a6d 6c 6f 77 69 6e 67 20 74 68 65 73 65 20 61 72 65  lowing these are
29a6e 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 72 65 67 69   pTab->nCol regi
29a6f 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6c  ster holding col
29a70 75 6d 6e 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20  umn.  ** data.. 
29a71 20 2a 2f 0a 20 20 72 65 67 4f 6c 64 52 6f 77 69   */.  regOldRowi
29a72 64 20 3d 20 72 65 67 4e 65 77 52 6f 77 69 64 20  d = regNewRowid 
29a73 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b  = pParse->nMem +
29a74 20 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d   1;.  pParse->nM
29a75 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c  em += pTab->nCol
29a76 20 2b 20 31 3b 0a 20 20 69 66 28 20 63 68 6e 67   + 1;.  if( chng
29a77 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 72 65 67  Rowid ){.    reg
29a78 4e 65 77 52 6f 77 69 64 2b 2b 3b 0a 20 20 20 20  NewRowid++;.    
29a79 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a  pParse->nMem++;.
29a7a 20 20 7d 0a 20 20 72 65 67 44 61 74 61 20 3d 20    }.  regData = 
29a7b 72 65 67 4e 65 77 52 6f 77 69 64 2b 31 3b 0a 20  regNewRowid+1;. 
29a7c 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e  ..  /* Begin gen
29a7d 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20  erating code..  
29a7e 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  */.  v = sqlite3
29a7f 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
29a80 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f  .  if( v==0 ) go
29a81 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
29a82 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  p;.  if( pParse-
29a83 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c  >nested==0 ) sql
29a84 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61  ite3VdbeCountCha
29a85 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74  nges(v);.  sqlit
29a86 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
29a87 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c  ation(pParse, 1,
29a88 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20   iDb);..#ifndef 
29a89 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
29a8a 55 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 56 69  UALTABLE.  /* Vi
29a8b 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 75 73  rtual tables mus
29a8c 74 20 62 65 20 68 61 6e 64 6c 65 64 20 73 65 70  t be handled sep
29a8d 61 72 61 74 65 6c 79 20 2a 2f 0a 20 20 69 66 28  arately */.  if(
29a8e 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
29a8f 20 29 7b 0a 20 20 20 20 75 70 64 61 74 65 56 69   ){.    updateVi
29a90 72 74 75 61 6c 54 61 62 6c 65 28 70 50 61 72 73  rtualTable(pPars
29a91 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 54 61  e, pTabList, pTa
29a92 62 2c 20 70 43 68 61 6e 67 65 73 2c 20 70 52 6f  b, pChanges, pRo
29a93 77 69 64 45 78 70 72 2c 20 61 58 52 65 66 2c 0a  widExpr, aXRef,.
29a94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29a95 20 20 20 20 20 20 20 70 57 68 65 72 65 29 3b 0a         pWhere);.
29a96 20 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a      pWhere = 0;.
29a97 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20 30      pTabList = 0
29a98 3b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74  ;.    goto updat
29a99 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23  e_cleanup;.  }.#
29a9a 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 74 61 72  endif..  /* Star
29a9b 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74 65  t the view conte
29a9c 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  xt.  */.  if( is
29a9d 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  View ){.    sqli
29a9e 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
29a9f 73 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f 6e  sh(pParse, &sCon
29aa0 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  text, pTab->zNam
29aa1 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65  e);.  }..  /* Ge
29aa2 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64 65 20  nerate the code 
29aa3 66 6f 72 20 74 72 69 67 67 65 72 73 2e 0a 20 20  for triggers..  
29aa4 2a 2f 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  */.  if( pTrigge
29aa5 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 47 6f  r ){.    int iGo
29aa6 74 6f 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61  to;..    /* Crea
29aa7 74 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73  te pseudo-tables
29aa8 20 66 6f 72 20 4e 45 57 20 61 6e 64 20 4f 4c 44   for NEW and OLD
29aa9 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
29aaa 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
29aab 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20   OP_OpenPseudo, 
29aac 6f 6c 64 49 64 78 2c 20 30 2c 20 70 54 61 62 2d  oldIdx, 0, pTab-
29aad 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71 6c 69  >nCol);.    sqli
29aae 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
29aaf 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20   OP_OpenPseudo, 
29ab0 6e 65 77 49 64 78 2c 20 30 2c 20 70 54 61 62 2d  newIdx, 0, pTab-
29ab1 3e 6e 43 6f 6c 29 3b 0a 0a 20 20 20 20 69 47 6f  >nCol);..    iGo
29ab2 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  to = sqlite3Vdbe
29ab3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
29ab4 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 64  o, 0, 0);.    ad
29ab5 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
29ab6 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
29ab7 20 20 69 42 65 67 69 6e 42 65 66 6f 72 65 54 72    iBeginBeforeTr
29ab8 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56  igger = sqlite3V
29ab9 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
29aba 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
29abb 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
29abc 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
29abd 72 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43  r, TK_UPDATE, pC
29abe 68 61 6e 67 65 73 2c 20 0a 20 20 20 20 20 20 20  hanges, .       
29abf 20 20 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52     TRIGGER_BEFOR
29ac0 45 2c 20 70 54 61 62 2c 20 6e 65 77 49 64 78 2c  E, pTab, newIdx,
29ac1 20 6f 6c 64 49 64 78 2c 20 6f 6e 45 72 72 6f 72   oldIdx, onError
29ac2 2c 20 61 64 64 72 2c 20 0a 20 20 20 20 20 20 20  , addr, .       
29ac3 20 20 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b     &old_col_mask
29ac4 2c 20 26 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 29  , &new_col_mask)
29ac5 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75   ){.      goto u
29ac6 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
29ac7 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 42 65 66     }.    iEndBef
29ac8 6f 72 65 54 72 69 67 67 65 72 20 3d 20 73 71 6c  oreTrigger = sql
29ac9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29aca 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29  , OP_Goto, 0, 0)
29acb 3b 0a 20 20 20 20 69 42 65 67 69 6e 41 66 74 65  ;.    iBeginAfte
29acc 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  rTrigger = sqlit
29acd 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
29ace 72 28 76 29 3b 0a 20 20 20 20 69 66 28 20 73 71  r(v);.    if( sq
29acf 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
29ad0 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69  ger(pParse, pTri
29ad1 67 67 65 72 2c 20 54 4b 5f 55 50 44 41 54 45 2c  gger, TK_UPDATE,
29ad2 20 70 43 68 61 6e 67 65 73 2c 20 0a 20 20 20 20   pChanges, .    
29ad3 20 20 20 20 20 20 54 52 49 47 47 45 52 5f 41 46        TRIGGER_AF
29ad4 54 45 52 2c 20 70 54 61 62 2c 20 6e 65 77 49 64  TER, pTab, newId
29ad5 78 2c 20 6f 6c 64 49 64 78 2c 20 6f 6e 45 72 72  x, oldIdx, onErr
29ad6 6f 72 2c 20 61 64 64 72 2c 20 0a 20 20 20 20 20  or, addr, .     
29ad7 20 20 20 20 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61       &old_col_ma
29ad8 73 6b 2c 20 26 6e 65 77 5f 63 6f 6c 5f 6d 61 73  sk, &new_col_mas
29ad9 6b 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  k) ){.      goto
29ada 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
29adb 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 41  .    }.    iEndA
29adc 66 74 65 72 54 72 69 67 67 65 72 20 3d 20 73 71  fterTrigger = sq
29add 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29ade 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30  v, OP_Goto, 0, 0
29adf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
29ae0 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 47  beJumpHere(v, iG
29ae1 6f 74 6f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  oto);.  }..  /* 
29ae2 49 66 20 77 65 20 61 72 65 20 74 72 79 69 6e 67  If we are trying
29ae3 20 74 6f 20 75 70 64 61 74 65 20 61 20 76 69 65   to update a vie
29ae4 77 2c 20 72 65 61 6c 69 7a 65 20 74 68 61 74 20  w, realize that 
29ae5 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61  view into.  ** a
29ae6 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
29ae7 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69  ..  */.#if !defi
29ae8 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
29ae9 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65  VIEW) && !define
29aea 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
29aeb 49 47 47 45 52 29 0a 20 20 69 66 28 20 69 73 56  IGGER).  if( isV
29aec 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  iew ){.    sqlit
29aed 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65  e3MaterializeVie
29aee 77 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  w(pParse, pTab, 
29aef 70 57 68 65 72 65 2c 20 69 43 75 72 29 3b 0a 20  pWhere, iCur);. 
29af0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
29af1 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75  Resolve the colu
29af2 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20  mn names in all 
29af3 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
29af4 69 6e 20 74 68 65 0a 20 20 2a 2a 20 57 48 45 52  in the.  ** WHER
29af5 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20  E clause..  */. 
29af6 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
29af7 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
29af8 43 2c 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20  C, pWhere) ){.  
29af9 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
29afa 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
29afb 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 62   Begin the datab
29afc 61 73 65 20 73 63 61 6e 0a 20 20 2a 2f 0a 20 20  ase scan.  */.  
29afd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29afe 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
29aff 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20   regOldRowid);. 
29b00 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
29b01 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72  3WhereBegin(pPar
29b02 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57  se, pTabList, pW
29b03 68 65 72 65 2c 20 30 2c 0a 20 20 20 20 20 20 20  here, 0,.       
29b04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b05 20 20 20 20 20 20 57 48 45 52 45 5f 4f 4e 45 50        WHERE_ONEP
29b06 41 53 53 5f 44 45 53 49 52 45 44 2c 20 30 29 3b  ASS_DESIRED, 0);
29b07 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30  .  if( pWInfo==0
29b08 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63   ) goto update_c
29b09 6c 65 61 6e 75 70 3b 0a 20 20 6f 6b 4f 6e 65 50  leanup;.  okOneP
29b0a 61 73 73 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b  ass = pWInfo->ok
29b0b 4f 6e 65 50 61 73 73 3b 0a 0a 20 20 2f 2a 20 52  OnePass;..  /* R
29b0c 65 6d 65 6d 62 65 72 20 74 68 65 20 72 6f 77 69  emember the rowi
29b0d 64 20 6f 66 20 65 76 65 72 79 20 69 74 65 6d 20  d of every item 
29b0e 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20  to be updated.. 
29b0f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
29b10 65 41 64 64 4f 70 32 28 76 2c 20 49 73 56 69 72  eAddOp2(v, IsVir
29b11 74 75 61 6c 28 70 54 61 62 29 3f 4f 50 5f 56 52  tual(pTab)?OP_VR
29b12 6f 77 69 64 3a 4f 50 5f 52 6f 77 69 64 2c 20 69  owid:OP_Rowid, i
29b13 43 75 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  Cur, regOldRowid
29b14 29 3b 0a 20 20 69 66 28 20 21 6f 6b 4f 6e 65 50  );.  if( !okOneP
29b15 61 73 73 20 29 7b 0a 20 20 20 20 72 65 67 52 6f  ass ){.    regRo
29b16 77 53 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  wSet = ++pParse-
29b17 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
29b18 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29b19 4f 50 5f 52 6f 77 53 65 74 41 64 64 2c 20 72 65  OP_RowSetAdd, re
29b1a 67 52 6f 77 53 65 74 2c 20 72 65 67 4f 6c 64 52  gRowSet, regOldR
29b1b 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  owid);.  }..  /*
29b1c 20 45 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   End the databas
29b1d 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 2a  e scan loop..  *
29b1e 2f 0a 20 20 73 71 6c 69 74 65 33 57 68 65 72 65  /.  sqlite3Where
29b1f 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 0a 20 20  End(pWInfo);..  
29b20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
29b21 65 20 63 6f 75 6e 74 20 6f 66 20 75 70 64 61 74  e count of updat
29b22 65 64 20 72 6f 77 73 0a 20 20 2a 2f 0a 20 20 69  ed rows.  */.  i
29b23 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  f( db->flags & S
29b24 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
29b25 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67  && !pParse->trig
29b26 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 72 65 67  Stack ){.    reg
29b27 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61  RowCount = ++pPa
29b28 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
29b29 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29b2a 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
29b2b 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b  0, regRowCount);
29b2c 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 56  .  }..  if( !isV
29b2d 69 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61  iew && !IsVirtua
29b2e 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 2f  l(pTab) ){.    /
29b2f 2a 20 0a 20 20 20 20 2a 2a 20 4f 70 65 6e 20 65  * .    ** Open e
29b30 76 65 72 79 20 69 6e 64 65 78 20 74 68 61 74 20  very index that 
29b31 6e 65 65 64 73 20 75 70 64 61 74 69 6e 67 2e 20  needs updating. 
29b32 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e   Note that if an
29b33 79 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 63  y.    ** index c
29b34 6f 75 6c 64 20 70 6f 74 65 6e 74 69 61 6c 6c 79  ould potentially
29b35 20 69 6e 76 6f 6b 65 20 61 20 52 45 50 4c 41 43   invoke a REPLAC
29b36 45 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c  E conflict resol
29b37 75 74 69 6f 6e 20 0a 20 20 20 20 2a 2a 20 61 63  ution .    ** ac
29b38 74 69 6f 6e 2c 20 74 68 65 6e 20 77 65 20 6e 65  tion, then we ne
29b39 65 64 20 74 6f 20 6f 70 65 6e 20 61 6c 6c 20 69  ed to open all i
29b3a 6e 64 69 63 65 73 20 62 65 63 61 75 73 65 20 77  ndices because w
29b3b 65 20 6d 69 67 68 74 20 6e 65 65 64 0a 20 20 20  e might need.   
29b3c 20 2a 2a 20 74 6f 20 62 65 20 64 65 6c 65 74 69   ** to be deleti
29b3d 6e 67 20 73 6f 6d 65 20 72 65 63 6f 72 64 73 2e  ng some records.
29b3e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
29b3f 21 6f 6b 4f 6e 65 50 61 73 73 20 29 20 73 71 6c  !okOnePass ) sql
29b40 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
29b41 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c  arse, iCur, iDb,
29b42 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 72   pTab, OP_OpenWr
29b43 69 74 65 29 3b 20 0a 20 20 20 20 69 66 28 20 6f  ite); .    if( o
29b44 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
29b45 63 65 20 29 7b 0a 20 20 20 20 20 20 6f 70 65 6e  ce ){.      open
29b46 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  All = 1;.    }el
29b47 73 65 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c  se{.      openAl
29b48 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72  l = 0;.      for
29b49 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
29b4a 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
29b4b 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
29b4c 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f       if( pIdx->o
29b4d 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
29b4e 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ce ){.          
29b4f 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20  openAll = 1;.   
29b50 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
29b51 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
29b52 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
29b53 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
29b54 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
29b55 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b  =pIdx->pNext, i+
29b56 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6f 70  +){.      if( op
29b57 65 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78  enAll || aRegIdx
29b58 5b 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  [i]>0 ){.       
29b59 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
29b5a 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
29b5b 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64  info(pParse, pId
29b5c 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  x);.        sqli
29b5d 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
29b5e 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69   OP_OpenWrite, i
29b5f 43 75 72 2b 69 2b 31 2c 20 70 49 64 78 2d 3e 74  Cur+i+1, pIdx->t
29b60 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20  num, iDb,.      
29b61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b62 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34   (char*)pKey, P4
29b63 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
29b64 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
29b65 74 28 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3e  t( pParse->nTab>
29b66 69 43 75 72 2b 69 2b 31 20 29 3b 0a 20 20 20 20  iCur+i+1 );.    
29b67 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
29b68 0a 20 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b 20  .  /* Jump back 
29b69 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 20 69 66  to this point if
29b6a 20 61 20 74 72 69 67 67 65 72 20 65 6e 63 6f 75   a trigger encou
29b6b 6e 74 65 72 73 20 61 6e 20 49 47 4e 4f 52 45 20  nters an IGNORE 
29b6c 63 6f 6e 73 74 72 61 69 6e 74 2e 20 2a 2f 0a 20  constraint. */. 
29b6d 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b   if( pTrigger ){
29b6e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29b6f 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
29b70 61 64 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  addr);.  }..  /*
29b71 20 54 6f 70 20 6f 66 20 74 68 65 20 75 70 64 61   Top of the upda
29b72 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28  te loop */.  if(
29b73 20 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20   okOnePass ){.  
29b74 20 20 69 6e 74 20 61 31 20 3d 20 73 71 6c 69 74    int a1 = sqlit
29b75 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
29b76 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 4f  OP_NotNull, regO
29b77 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 64  ldRowid);.    ad
29b78 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
29b79 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74  AddOp0(v, OP_Got
29b7a 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  o);.    sqlite3V
29b7b 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
29b7c 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
29b7d 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
29b7e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
29b7f 52 6f 77 53 65 74 52 65 61 64 2c 20 72 65 67 52  RowSetRead, regR
29b80 6f 77 53 65 74 2c 20 30 2c 20 72 65 67 4f 6c 64  owSet, 0, regOld
29b81 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 69  Rowid);.  }..  i
29b82 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
29b83 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b     int regRowid;
29b84 0a 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77 3b  .    int regRow;
29b85 0a 20 20 20 20 69 6e 74 20 72 65 67 43 6f 6c 73  .    int regCols
29b86 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 63  ;..    /* Make c
29b87 75 72 73 6f 72 20 69 43 75 72 20 70 6f 69 6e 74  ursor iCur point
29b88 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 74   to the record t
29b89 68 61 74 20 69 73 20 62 65 69 6e 67 20 75 70 64  hat is being upd
29b8a 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ated..    */.   
29b8b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29b8c 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
29b8d 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20  ts, iCur, addr, 
29b8e 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20  regOldRowid);.. 
29b8f 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74     /* Generate t
29b90 68 65 20 4f 4c 44 20 74 61 62 6c 65 0a 20 20 20  he OLD table.   
29b91 20 2a 2f 0a 20 20 20 20 72 65 67 52 6f 77 69 64   */.    regRowid
29b92 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
29b93 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
29b94 20 20 72 65 67 52 6f 77 20 3d 20 73 71 6c 69 74    regRow = sqlit
29b95 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
29b96 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rse);.    sqlite
29b97 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29b98 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72  P_Rowid, iCur, r
29b99 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66  egRowid);.    if
29b9a 28 20 21 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20  ( !old_col_mask 
29b9b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
29b9c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29b9d 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77  _Null, 0, regRow
29b9e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
29b9f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29ba0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44  ddOp2(v, OP_RowD
29ba1 61 74 61 2c 20 69 43 75 72 2c 20 72 65 67 52 6f  ata, iCur, regRo
29ba2 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  w);.    }.    sq
29ba3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
29ba4 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6f 6c  v, OP_Insert, ol
29ba5 64 49 64 78 2c 20 72 65 67 52 6f 77 2c 20 72 65  dIdx, regRow, re
29ba6 67 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a  gRowid);..    /*
29ba7 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 4e 45   Generate the NE
29ba8 57 20 74 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20  W table.    */. 
29ba9 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64     if( chngRowid
29baa 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
29bab 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68  3ExprCodeAndCach
29bac 65 28 70 50 61 72 73 65 2c 20 70 52 6f 77 69 64  e(pParse, pRowid
29bad 45 78 70 72 2c 20 72 65 67 52 6f 77 69 64 29 3b  Expr, regRowid);
29bae 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29baf 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d  beAddOp1(v, OP_M
29bb0 75 73 74 42 65 49 6e 74 2c 20 72 65 67 52 6f 77  ustBeInt, regRow
29bb1 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  id);.    }else{.
29bb2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29bb3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
29bb4 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67 52 6f  wid, iCur, regRo
29bb5 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  wid);.    }.    
29bb6 72 65 67 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65  regCols = sqlite
29bb7 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
29bb8 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  arse, pTab->nCol
29bb9 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
29bba 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b  i<pTab->nCol; i+
29bbb 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 3d  +){.      if( i=
29bbc 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
29bbd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29bbe 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29bbf 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 43 6f 6c 73  Null, 0, regCols
29bc0 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e  +i);.        con
29bc1 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  tinue;.      }. 
29bc2 20 20 20 20 20 6a 20 3d 20 61 58 52 65 66 5b 69       j = aXRef[i
29bc3 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77  ];.      if( new
29bc4 5f 63 6f 6c 5f 6d 61 73 6b 26 28 28 75 33 32 29  _col_mask&((u32)
29bc5 31 3c 3c 69 29 20 7c 7c 20 6e 65 77 5f 63 6f 6c  1<<i) || new_col
29bc6 5f 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66  _mask==0xfffffff
29bc7 66 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  f ){.        if(
29bc8 20 6a 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20   j<0 ){.        
29bc9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29bca 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
29bcb 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67 43 6f  , iCur, i, regCo
29bcc 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 20  ls+i);.         
29bcd 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
29bce 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69  fault(v, pTab, i
29bcf 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
29bd0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
29bd1 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61  te3ExprCodeAndCa
29bd2 63 68 65 28 70 50 61 72 73 65 2c 20 70 43 68 61  che(pParse, pCha
29bd3 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  nges->a[j].pExpr
29bd4 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20  , regCols+i);.  
29bd5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
29bd6 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
29bd7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29bd8 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65  , OP_Null, 0, re
29bd9 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20  gCols+i);.      
29bda 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
29bdb 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
29bdc 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
29bdd 72 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e  regCols, pTab->n
29bde 43 6f 6c 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20  Col, regRow);.  
29bdf 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
29be0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61  .      sqlite3Ta
29be1 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76  bleAffinityStr(v
29be2 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73  , pTab);.      s
29be3 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
29be4 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
29be5 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70  arse, regCols, p
29be6 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20  Tab->nCol);.    
29be7 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  }.    sqlite3Rel
29be8 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
29be9 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70  arse, regCols, p
29bea 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20  Tab->nCol);.    
29beb 2f 2a 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  /* if( pParse->n
29bec 45 72 72 20 29 20 67 6f 74 6f 20 75 70 64 61 74  Err ) goto updat
29bed 65 5f 63 6c 65 61 6e 75 70 3b 20 2a 2f 0a 20 20  e_cleanup; */.  
29bee 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29bef 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
29bf0 2c 20 6e 65 77 49 64 78 2c 20 72 65 67 52 6f 77  , newIdx, regRow
29bf1 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
29bf2 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
29bf3 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
29bf4 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  egRowid);.    sq
29bf5 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
29bf6 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
29bf7 6f 77 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  ow);..    sqlite
29bf8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29bf9 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65 67 69  P_Goto, 0, iBegi
29bfa 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 29 3b  nBeforeTrigger);
29bfb 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29bfc 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 45 6e 64  JumpHere(v, iEnd
29bfd 42 65 66 6f 72 65 54 72 69 67 67 65 72 29 3b 0a  BeforeTrigger);.
29bfe 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 56 69    }..  if( !isVi
29bff 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61 6c  ew && !IsVirtual
29c00 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 2f 2a  (pTab) ){.    /*
29c01 20 4c 6f 6f 70 20 6f 76 65 72 20 65 76 65 72 79   Loop over every
29c02 20 72 65 63 6f 72 64 20 74 68 61 74 20 6e 65 65   record that nee
29c03 64 73 20 75 70 64 61 74 69 6e 67 2e 20 20 57 65  ds updating.  We
29c04 20 68 61 76 65 20 74 6f 20 6c 6f 61 64 0a 20 20   have to load.  
29c05 20 20 2a 2a 20 74 68 65 20 6f 6c 64 20 64 61 74    ** the old dat
29c06 61 20 66 6f 72 20 65 61 63 68 20 72 65 63 6f 72  a for each recor
29c07 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  d to be updated 
29c08 62 65 63 61 75 73 65 20 73 6f 6d 65 20 63 6f 6c  because some col
29c09 75 6d 6e 73 0a 20 20 20 20 2a 2a 20 6d 69 67 68  umns.    ** migh
29c0a 74 20 6e 6f 74 20 63 68 61 6e 67 65 20 61 6e 64  t not change and
29c0b 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f   we will need to
29c0c 20 63 6f 70 79 20 74 68 65 20 6f 6c 64 20 76 61   copy the old va
29c0d 6c 75 65 2e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f  lue..    ** Also
29c0e 2c 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69  , the old data i
29c0f 73 20 6e 65 65 64 65 64 20 74 6f 20 64 65 6c 65  s needed to dele
29c10 74 65 20 74 68 65 20 6f 6c 64 20 69 6e 64 65 78  te the old index
29c11 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 2a 2a   entries..    **
29c12 20 53 6f 20 6d 61 6b 65 20 74 68 65 20 63 75 72   So make the cur
29c13 73 6f 72 20 70 6f 69 6e 74 20 61 74 20 74 68 65  sor point at the
29c14 20 6f 6c 64 20 72 65 63 6f 72 64 2e 0a 20 20 20   old record..   
29c15 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
29c16 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
29c17 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c  NotExists, iCur,
29c18 20 61 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77   addr, regOldRow
29c19 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  id);..    /* If 
29c1a 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
29c1b 72 20 77 69 6c 6c 20 63 68 61 6e 67 65 2c 20 70  r will change, p
29c1c 75 73 68 20 74 68 65 20 72 65 63 6f 72 64 20 6e  ush the record n
29c1d 75 6d 62 65 72 20 61 73 20 69 74 0a 20 20 20 20  umber as it.    
29c1e 2a 2a 20 77 69 6c 6c 20 62 65 20 61 66 74 65 72  ** will be after
29c1f 20 74 68 65 20 75 70 64 61 74 65 2e 20 28 54 68   the update. (Th
29c20 65 20 6f 6c 64 20 72 65 63 6f 72 64 20 6e 75 6d  e old record num
29c21 62 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ber is currently
29c22 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 6f 70 20 6f  .    ** on top o
29c23 66 20 74 68 65 20 73 74 61 63 6b 2e 29 0a 20 20  f the stack.).  
29c24 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 68 6e    */.    if( chn
29c25 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  gRowid ){.      
29c26 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
29c27 70 50 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78  pParse, pRowidEx
29c28 70 72 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29  pr, regNewRowid)
29c29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29c2a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
29c2b 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67 4e 65  MustBeInt, regNe
29c2c 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a  wRowid);.    }..
29c2d 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 6e      /* Compute n
29c2e 65 77 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  ew data for this
29c2f 20 72 65 63 6f 72 64 2e 20 20 0a 20 20 20 20 2a   record.  .    *
29c30 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
29c31 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
29c32 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  ){.      if( i==
29c33 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
29c34 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29c35 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
29c36 75 6c 6c 2c 20 30 2c 20 72 65 67 44 61 74 61 2b  ull, 0, regData+
29c37 69 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74  i);.        cont
29c38 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
29c39 20 20 20 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d      j = aXRef[i]
29c3a 3b 0a 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20  ;.      if( j<0 
29c3b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
29c3c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
29c3d 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c  OP_Column, iCur,
29c3e 20 69 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a   i, regData+i);.
29c3f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
29c40 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20  olumnDefault(v, 
29c41 70 54 61 62 2c 20 69 29 3b 0a 20 20 20 20 20 20  pTab, i);.      
29c42 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
29c43 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
29c44 50 61 72 73 65 2c 20 70 43 68 61 6e 67 65 73 2d  Parse, pChanges-
29c45 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67  >a[j].pExpr, reg
29c46 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 7d  Data+i);.      }
29c47 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
29c48 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65  o constraint che
29c49 63 6b 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  cks.    */.    s
29c4a 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f  qlite3GenerateCo
29c4b 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 70  nstraintChecks(p
29c4c 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
29c4d 72 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 0a  r, regNewRowid,.
29c4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c50 20 20 20 20 61 52 65 67 49 64 78 2c 20 63 68 6e      aRegIdx, chn
29c51 67 52 6f 77 69 64 2c 20 31 2c 0a 20 20 20 20 20  gRowid, 1,.     
29c52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
29c54 6e 45 72 72 6f 72 2c 20 61 64 64 72 29 3b 0a 0a  nError, addr);..
29c55 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68      /* Delete th
29c56 65 20 6f 6c 64 20 69 6e 64 69 63 65 73 20 66 6f  e old indices fo
29c57 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65  r the current re
29c58 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  cord..    */.   
29c59 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
29c5a 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
29c5b 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 30  tExists, iCur, 0
29c5c 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a  , regOldRowid);.
29c5d 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
29c5e 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
29c5f 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
29c60 69 43 75 72 2c 20 61 52 65 67 49 64 78 29 3b 0a  iCur, aRegIdx);.
29c61 0a 20 20 20 20 2f 2a 20 49 66 20 63 68 61 6e 67  .    /* If chang
29c62 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 6e  ing the record n
29c63 75 6d 62 65 72 2c 20 64 65 6c 65 74 65 20 74 68  umber, delete th
29c64 65 20 6f 6c 64 20 72 65 63 6f 72 64 2e 0a 20 20  e old record..  
29c65 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 68 6e    */.    if( chn
29c66 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  gRowid ){.      
29c67 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29c68 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20  2(v, OP_Delete, 
29c69 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  iCur, 0);.    }.
29c6a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
29c6b 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
29c6c 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74  .    /* Create t
29c6d 68 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74  he new index ent
29c6e 72 69 65 73 20 61 6e 64 20 74 68 65 20 6e 65 77  ries and the new
29c6f 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a   record..    */.
29c70 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c      sqlite3Compl
29c71 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 70 50 61  eteInsertion(pPa
29c72 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c  rse, pTab, iCur,
29c73 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 20 0a 20   regNewRowid, . 
29c74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c75 20 20 20 20 20 20 20 20 20 20 20 20 61 52 65 67              aReg
29c76 49 64 78 2c 20 31 2c 20 2d 31 2c 20 30 29 3b 0a  Idx, 1, -1, 0);.
29c77 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d    }..  /* Increm
29c78 65 6e 74 20 74 68 65 20 72 6f 77 20 63 6f 75 6e  ent the row coun
29c79 74 65 72 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ter .  */.  if( 
29c7a 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
29c7b 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20  TE_CountRows && 
29c7c 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  !pParse->trigSta
29c7d 63 6b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ck){.    sqlite3
29c7e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29c7f 5f 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43  _AddImm, regRowC
29c80 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20  ount, 1);.  }.. 
29c81 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
29c82 20 74 72 69 67 67 65 72 73 2c 20 63 6c 6f 73 65   triggers, close
29c83 20 61 6c 6c 20 74 68 65 20 63 75 72 73 6f 72 73   all the cursors
29c84 20 61 66 74 65 72 20 65 61 63 68 20 69 74 65 72   after each iter
29c85 61 74 69 6f 6e 0a 20 20 2a 2a 20 74 68 72 6f 75  ation.  ** throu
29c86 67 68 20 74 68 65 20 6c 6f 6f 70 2e 20 20 54 68  gh the loop.  Th
29c87 65 20 66 69 72 65 20 74 68 65 20 61 66 74 65 72  e fire the after
29c88 20 74 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a   triggers..  */.
29c89 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29    if( pTrigger )
29c8a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
29c8b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
29c8c 74 6f 2c 20 30 2c 20 69 42 65 67 69 6e 41 66 74  to, 0, iBeginAft
29c8d 65 72 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  erTrigger);.    
29c8e 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
29c8f 65 72 65 28 76 2c 20 69 45 6e 64 41 66 74 65 72  ere(v, iEndAfter
29c90 54 72 69 67 67 65 72 29 3b 0a 20 20 7d 0a 0a 20  Trigger);.  }.. 
29c91 20 2f 2a 20 52 65 70 65 61 74 20 74 68 65 20 61   /* Repeat the a
29c92 62 6f 76 65 20 77 69 74 68 20 74 68 65 20 6e 65  bove with the ne
29c93 78 74 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20  xt record to be 
29c94 75 70 64 61 74 65 64 2c 20 75 6e 74 69 6c 0a 20  updated, until. 
29c95 20 2a 2a 20 61 6c 6c 20 72 65 63 6f 72 64 20 73   ** all record s
29c96 65 6c 65 63 74 65 64 20 62 79 20 74 68 65 20 57  elected by the W
29c97 48 45 52 45 20 63 6c 61 75 73 65 20 68 61 76 65  HERE clause have
29c98 20 62 65 65 6e 20 75 70 64 61 74 65 64 2e 0a 20   been updated.. 
29c99 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
29c9a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
29c9b 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20  to, 0, addr);.  
29c9c 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
29c9d 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20  ere(v, addr);.. 
29c9e 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61   /* Close all ta
29c9f 62 6c 65 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  bles */.  for(i=
29ca0 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
29ca1 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
29ca2 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b  =pIdx->pNext, i+
29ca3 2b 29 7b 0a 20 20 20 20 69 66 28 20 6f 70 65 6e  +){.    if( open
29ca4 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b 69  All || aRegIdx[i
29ca5 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ]>0 ){.      sql
29ca6 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29ca7 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72  , OP_Close, iCur
29ca8 2b 69 2b 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  +i+1, 0);.    }.
29ca9 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
29caa 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
29cab 6f 73 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20  ose, iCur, 0);. 
29cac 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b   if( pTrigger ){
29cad 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29cae 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
29caf 73 65 2c 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a  se, newIdx, 0);.
29cb0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29cb1 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
29cb2 65 2c 20 6f 6c 64 49 64 78 2c 20 30 29 3b 0a 20  e, oldIdx, 0);. 
29cb3 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65   }..  /*.  ** Re
29cb4 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
29cb5 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 65 72  of rows that wer
29cb6 65 20 63 68 61 6e 67 65 64 2e 20 49 66 20 74 68  e changed. If th
29cb7 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20  is routine is . 
29cb8 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63   ** generating c
29cb9 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20 61  ode because of a
29cba 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
29cbb 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20 64  NestedParse(), d
29cbc 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b  o not.  ** invok
29cbd 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
29cbe 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  unction..  */.  
29cbf 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
29cc0 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
29cc1 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69   && !pParse->tri
29cc2 67 53 74 61 63 6b 20 26 26 20 70 50 61 72 73 65  gStack && pParse
29cc3 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20  ->nested==0 ){. 
29cc4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29cc5 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
29cc6 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75 6e  tRow, regRowCoun
29cc7 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  t, 1);.    sqlit
29cc8 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
29cc9 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
29cca 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
29ccb 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
29ccc 4e 41 4d 45 2c 20 22 72 6f 77 73 20 75 70 64 61  NAME, "rows upda
29ccd 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ted", SQLITE_STA
29cce 54 49 43 29 3b 0a 20 20 7d 0a 0a 75 70 64 61 74  TIC);.  }..updat
29ccf 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c  e_cleanup:.  sql
29cd0 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
29cd1 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20  op(&sContext);. 
29cd2 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
29cd3 62 2c 20 61 52 65 67 49 64 78 29 3b 0a 20 20 73  b, aRegIdx);.  s
29cd4 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
29cd5 20 61 58 52 65 66 29 3b 0a 20 20 73 71 6c 69 74   aXRef);.  sqlit
29cd6 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
29cd7 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20  db, pTabList);. 
29cd8 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
29cd9 44 65 6c 65 74 65 28 64 62 2c 20 70 43 68 61 6e  Delete(db, pChan
29cda 67 65 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ges);.  sqlite3E
29cdb 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57  xprDelete(db, pW
29cdc 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b  here);.  return;
29cdd 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
29cde 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
29cdf 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ABLE./*.** Gener
29ce0 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 6e 20  ate code for an 
29ce1 55 50 44 41 54 45 20 6f 66 20 61 20 76 69 72 74  UPDATE of a virt
29ce2 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ual table..**.**
29ce3 20 54 68 65 20 73 74 72 61 74 65 67 79 20 69 73   The strategy is
29ce4 20 74 68 61 74 20 77 65 20 63 72 65 61 74 65 20   that we create 
29ce5 61 6e 20 65 70 68 65 6d 65 72 69 61 6c 20 74 61  an ephemerial ta
29ce6 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ble that contain
29ce7 73 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 72 6f  s.** for each ro
29ce8 77 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 3a  w to be changed:
29ce9 0a 2a 2a 0a 2a 2a 20 20 20 28 41 29 20 20 54 68  .**.**   (A)  Th
29cea 65 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 69 64  e original rowid
29ceb 20 6f 66 20 74 68 61 74 20 72 6f 77 2e 0a 2a 2a   of that row..**
29cec 20 20 20 28 42 29 20 20 54 68 65 20 72 65 76 69     (B)  The revi
29ced 73 65 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68  sed rowid for th
29cee 65 20 72 6f 77 2e 20 28 6e 6f 74 65 31 29 0a 2a  e row. (note1).*
29cef 2a 20 20 20 28 43 29 20 20 54 68 65 20 63 6f 6e  *   (C)  The con
29cf0 74 65 6e 74 20 6f 66 20 65 76 65 72 79 20 63 6f  tent of every co
29cf1 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 2e  lumn in the row.
29cf2 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 77 65 20 6c  .**.** Then we l
29cf3 6f 6f 70 20 6f 76 65 72 20 74 68 69 73 20 65 70  oop over this ep
29cf4 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 61 6e  hemeral table an
29cf5 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  d for each row i
29cf6 6e 0a 2a 2a 20 74 68 65 20 65 70 68 65 72 6d 65  n.** the epherme
29cf7 72 61 6c 20 74 61 62 6c 65 20 63 61 6c 6c 20 56  ral table call V
29cf8 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  Update..**.** Wh
29cf9 65 6e 20 66 69 6e 69 73 68 65 64 2c 20 64 72 6f  en finished, dro
29cfa 70 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  p the ephemeral 
29cfb 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 6e 6f  table..**.** (no
29cfc 74 65 31 29 20 41 63 74 75 61 6c 6c 79 2c 20 69  te1) Actually, i
29cfd 66 20 77 65 20 6b 6e 6f 77 20 69 6e 20 61 64 76  f we know in adv
29cfe 61 6e 63 65 20 74 68 61 74 20 28 41 29 20 69 73  ance that (A) is
29cff 20 61 6c 77 61 79 73 20 74 68 65 20 73 61 6d 65   always the same
29d00 0a 2a 2a 20 61 73 20 28 42 29 20 77 65 20 6f 6e  .** as (B) we on
29d01 6c 79 20 73 74 6f 72 65 20 28 41 29 2c 20 74 68  ly store (A), th
29d02 65 6e 20 64 75 70 6c 69 63 61 74 65 20 28 41 29  en duplicate (A)
29d03 20 77 68 65 6e 20 70 75 6c 6c 69 6e 67 0a 2a 2a   when pulling.**
29d04 20 69 74 20 6f 75 74 20 6f 66 20 74 68 65 20 65   it out of the e
29d05 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 62  phemeral table b
29d06 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 56 55  efore calling VU
29d07 70 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  pdate..*/.static
29d08 20 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74   void updateVirt
29d09 75 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73  ualTable(.  Pars
29d0a 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
29d0b 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
29d0c 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
29d0d 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
29d0e 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
29d0f 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64   table to be mod
29d10 69 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65  ified */.  Table
29d11 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
29d12 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74  /* The virtual t
29d13 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
29d14 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f  st *pChanges,  /
29d15 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f  * The columns to
29d16 20 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55   change in the U
29d17 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
29d18 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69  */.  Expr *pRowi
29d19 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70  d,        /* Exp
29d1a 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20  ression used to 
29d1b 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f  recompute the ro
29d1c 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58  wid */.  int *aX
29d1d 52 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Ref,          /*
29d1e 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f   Mapping from co
29d1f 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f  lumns of pTab to
29d20 20 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61   entries in pCha
29d21 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a  nges */.  Expr *
29d22 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20 2f  pWhere         /
29d23 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  * WHERE clause o
29d24 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
29d25 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56  tement */.){.  V
29d26 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
29d27 3e 70 56 64 62 65 3b 20 20 2f 2a 20 56 69 72 74  >pVdbe;  /* Virt
29d28 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65  ual machine unde
29d29 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a  r construction *
29d2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
29d2b 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a  List = 0;     /*
29d2c 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   The result set 
29d2d 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  of the SELECT st
29d2e 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c  atement */.  Sel
29d2f 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30  ect *pSelect = 0
29d30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45  ;      /* The SE
29d31 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
29d32 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  /.  Expr *pExpr;
29d33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29d34 20 54 65 6d 70 6f 72 61 72 79 20 65 78 70 72 65   Temporary expre
29d35 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 65  ssion */.  int e
29d36 70 68 65 6d 54 61 62 3b 20 20 20 20 20 20 20 20  phemTab;        
29d37 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 6f       /* Table ho
29d38 6c 64 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  lding the result
29d39 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a   of the SELECT *
29d3a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
29d3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29d3c 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
29d3d 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20  .  int addr;    
29d3e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29d3f 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f  Address of top o
29d40 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  f loop */.  int 
29d41 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20  iReg;           
29d42 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72        /* First r
29d43 65 67 69 73 74 65 72 20 69 6e 20 73 65 74 20 70  egister in set p
29d44 61 73 73 65 64 20 74 6f 20 4f 50 5f 56 55 70 64  assed to OP_VUpd
29d45 61 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ate */.  sqlite3
29d46 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
29d47 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63  b; /* Database c
29d48 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
29d49 6f 6e 73 74 20 63 68 61 72 20 2a 70 56 74 61 62  onst char *pVtab
29d4a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
29d4b 70 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 53  pTab->pVtab;.  S
29d4c 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a  electDest dest;.
29d4d 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20  .  /* Construct 
29d4e 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
29d4f 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20 66  ment that will f
29d50 69 6e 64 20 74 68 65 20 6e 65 77 20 76 61 6c 75  ind the new valu
29d51 65 73 20 66 6f 72 0a 20 20 2a 2a 20 61 6c 6c 20  es for.  ** all 
29d52 75 70 64 61 74 65 64 20 72 6f 77 73 2e 20 0a 20  updated rows. . 
29d53 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 73   */.  pEList = s
29d54 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
29d55 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20  pend(pParse, 0, 
29d56 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29d57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29d58 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49    sqlite3CreateI
29d59 64 45 78 70 72 28 70 50 61 72 73 65 2c 20 22 5f  dExpr(pParse, "_
29d5a 72 6f 77 69 64 5f 22 29 2c 20 30 29 3b 0a 20 20  rowid_"), 0);.  
29d5b 69 66 28 20 70 52 6f 77 69 64 20 29 7b 0a 20 20  if( pRowid ){.  
29d5c 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74    pEList = sqlit
29d5d 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
29d5e 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c  (pParse, pEList,
29d5f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29d61 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
29d62 75 70 28 64 62 2c 20 70 52 6f 77 69 64 2c 20 30  up(db, pRowid, 0
29d63 29 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ), 0);.  }.  ass
29d64 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79  ert( pTab->iPKey
29d65 3c 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  <0 );.  for(i=0;
29d66 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
29d67 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 58 52  ++){.    if( aXR
29d68 65 66 5b 69 5d 3e 3d 30 20 29 7b 0a 20 20 20 20  ef[i]>=0 ){.    
29d69 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65    pExpr = sqlite
29d6a 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 43 68  3ExprDup(db, pCh
29d6b 61 6e 67 65 73 2d 3e 61 5b 61 58 52 65 66 5b 69  anges->a[aXRef[i
29d6c 5d 5d 2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20  ]].pExpr, 0);.  
29d6d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
29d6e 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 43 72  Expr = sqlite3Cr
29d6f 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73  eateIdExpr(pPars
29d70 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
29d71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  .zName);.    }. 
29d72 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69     pEList = sqli
29d73 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
29d74 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  d(pParse, pEList
29d75 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d  , pExpr, 0);.  }
29d76 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  .  pSelect = sql
29d77 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50  ite3SelectNew(pP
29d78 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 53  arse, pEList, pS
29d79 72 63 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30  rc, pWhere, 0, 0
29d7a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
29d7b 20 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68   .  /* Create th
29d7c 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
29d7d 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 68 65  e into which the
29d7e 20 75 70 64 61 74 65 20 72 65 73 75 6c 74 73 20   update results 
29d7f 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 73 74 6f  will.  ** be sto
29d80 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  red..  */.  asse
29d81 72 74 28 20 76 20 29 3b 0a 20 20 65 70 68 65 6d  rt( v );.  ephem
29d82 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Tab = pParse->nT
29d83 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56  ab++;.  sqlite3V
29d84 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29d85 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 65  OpenEphemeral, e
29d86 70 68 65 6d 54 61 62 2c 20 70 54 61 62 2d 3e 6e  phemTab, pTab->n
29d87 43 6f 6c 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30  Col+1+(pRowid!=0
29d88 29 29 3b 0a 0a 20 20 2f 2a 20 66 69 6c 6c 20 74  ));..  /* fill t
29d89 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
29d8a 6c 65 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  le .  */.  sqlit
29d8b 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
29d8c 28 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c  (&dest, SRT_Tabl
29d8d 65 2c 20 65 70 68 65 6d 54 61 62 29 3b 0a 20 20  e, ephemTab);.  
29d8e 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
29d8f 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26  arse, pSelect, &
29d90 64 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e  dest);..  /* Gen
29d91 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 63  erate code to sc
29d92 61 6e 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  an the ephemeral
29d93 20 74 61 62 6c 65 20 61 6e 64 20 63 61 6c 6c 20   table and call 
29d94 56 55 70 64 61 74 65 2e 20 2a 2f 0a 20 20 69 52  VUpdate. */.  iR
29d95 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  eg = ++pParse->n
29d96 4d 65 6d 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  Mem;.  pParse->n
29d97 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f  Mem += pTab->nCo
29d98 6c 2b 31 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  l+1;.  sqlite3Vd
29d99 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
29d9a 65 77 69 6e 64 2c 20 65 70 68 65 6d 54 61 62 2c  ewind, ephemTab,
29d9b 20 30 29 3b 0a 20 20 61 64 64 72 20 3d 20 73 71   0);.  addr = sq
29d9c 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
29d9d 41 64 64 72 28 76 29 3b 0a 20 20 73 71 6c 69 74  Addr(v);.  sqlit
29d9e 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
29d9f 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 65 70 68 65  OP_Column,  ephe
29da0 6d 54 61 62 2c 20 30 2c 20 69 52 65 67 29 3b 0a  mTab, 0, iReg);.
29da1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29da2 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
29da3 2c 20 65 70 68 65 6d 54 61 62 2c 20 28 70 52 6f  , ephemTab, (pRo
29da4 77 69 64 3f 31 3a 30 29 2c 20 69 52 65 67 2b 31  wid?1:0), iReg+1
29da5 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
29da6 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
29da7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
29da8 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
29da9 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20  lumn, ephemTab, 
29daa 69 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29 2c  i+1+(pRowid!=0),
29dab 20 69 52 65 67 2b 32 2b 69 29 3b 0a 20 20 7d 0a   iReg+2+i);.  }.
29dac 20 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b    sqlite3VtabMak
29dad 65 57 72 69 74 61 62 6c 65 28 70 50 61 72 73 65  eWritable(pParse
29dae 2c 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 74  , pTab);.  sqlit
29daf 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
29db0 4f 50 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 70  OP_VUpdate, 0, p
29db1 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 69 52 65  Tab->nCol+2, iRe
29db2 67 2c 20 70 56 74 61 62 2c 20 50 34 5f 56 54 41  g, pVtab, P4_VTA
29db3 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  B);.  sqlite3Vdb
29db4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
29db5 78 74 2c 20 65 70 68 65 6d 54 61 62 2c 20 61 64  xt, ephemTab, ad
29db6 64 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  dr);.  sqlite3Vd
29db7 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
29db8 64 72 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  dr-1);.  sqlite3
29db9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29dba 5f 43 6c 6f 73 65 2c 20 65 70 68 65 6d 54 61 62  _Close, ephemTab
29dbb 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61  , 0);..  /* Clea
29dbc 6e 75 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  nup */.  sqlite3
29dbd 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
29dbe 20 70 53 65 6c 65 63 74 29 3b 20 20 0a 7d 0a 23   pSelect);  .}.#
29dbf 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
29dc0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
29dc1 45 20 2a 2f 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75  E */../* Make su
29dc2 72 65 20 22 69 73 56 69 65 77 22 20 67 65 74 73  re "isView" gets
29dc3 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 63 61   undefined in ca
29dc4 73 65 20 74 68 69 73 20 66 69 6c 65 20 62 65 63  se this file bec
29dc5 6f 6d 65 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20  omes part of.** 
29dc6 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
29dc7 20 2d 20 73 6f 20 74 68 61 74 20 73 75 62 73 65   - so that subse
29dc8 71 75 65 6e 74 20 66 69 6c 65 73 20 64 6f 20 6e  quent files do n
29dc9 6f 74 20 73 65 65 20 69 73 56 69 65 77 20 61 73  ot see isView as
29dca 20 61 0a 2a 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a   a.** macro. */.
29dcb 23 75 6e 64 65 66 20 69 73 56 69 65 77 0a 0a 2f  #undef isView../
29dcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
29dcd 6e 64 20 6f 66 20 75 70 64 61 74 65 2e 63 20 2a  nd of update.c *
29dce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29dcf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
29dd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
29dd2 65 67 69 6e 20 66 69 6c 65 20 76 61 63 75 75 6d  egin file vacuum
29dd3 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
29dd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29dd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
29dd6 2a 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20  *.** 2003 April 
29dd7 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
29dd8 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
29dd9 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
29dda 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
29ddb 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
29ddc 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
29ddd 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
29dde 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
29ddf 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
29de0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
29de1 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
29de2 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
29de3 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
29de4 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
29de5 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
29de6 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
29de7 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
29de8 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
29de9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29dea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29deb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29dec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ded 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
29dee 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
29def 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
29df0 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64  e VACUUM command
29df1 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20  ..**.** Most of 
29df2 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  the code in this
29df3 20 66 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69   file may be omi
29df4 74 74 65 64 20 62 79 20 64 65 66 69 6e 69 6e 67  tted by defining
29df5 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f   the.** SQLITE_O
29df6 4d 49 54 5f 56 41 43 55 55 4d 20 6d 61 63 72 6f  MIT_VACUUM macro
29df7 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 61 63  ..**.** $Id: vac
29df8 75 75 6d 2e 63 2c 76 20 31 2e 38 36 20 32 30 30  uum.c,v 1.86 200
29df9 39 2f 30 32 2f 30 33 20 31 36 3a 35 31 3a 32 35  9/02/03 16:51:25
29dfa 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
29dfb 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69   $.*/..#if !defi
29dfc 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
29dfd 56 41 43 55 55 4d 29 20 26 26 20 21 64 65 66 69  VACUUM) && !defi
29dfe 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
29dff 41 54 54 41 43 48 29 0a 2f 2a 0a 2a 2a 20 45 78  ATTACH)./*.** Ex
29e00 65 63 75 74 65 20 7a 53 71 6c 20 6f 6e 20 64 61  ecute zSql on da
29e01 74 61 62 61 73 65 20 64 62 2e 20 52 65 74 75 72  tabase db. Retur
29e02 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e  n an error code.
29e03 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65  .*/.static int e
29e04 78 65 63 53 71 6c 28 73 71 6c 69 74 65 33 20 2a  xecSql(sqlite3 *
29e05 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
29e06 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33  zSql){.  sqlite3
29e07 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20  _stmt *pStmt;.  
29e08 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20  if( !zSql ){.   
29e09 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
29e0a 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
29e0b 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
29e0c 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a  e3_prepare(db, z
29e0d 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
29e0e 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
29e0f 6e 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  n sqlite3_errcod
29e10 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20 77 68 69  e(db);.  }.  whi
29e11 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  le( SQLITE_ROW==
29e12 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
29e13 6d 74 29 20 29 7b 7d 0a 20 20 72 65 74 75 72 6e  mt) ){}.  return
29e14 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
29e15 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a  e(pStmt);.}../*.
29e16 2a 2a 20 45 78 65 63 75 74 65 20 7a 53 71 6c 20  ** Execute zSql 
29e17 6f 6e 20 64 61 74 61 62 61 73 65 20 64 62 2e 20  on database db. 
29e18 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65  The statement re
29e19 74 75 72 6e 73 20 65 78 61 63 74 6c 79 0a 2a 2a  turns exactly.**
29e1a 20 6f 6e 65 20 63 6f 6c 75 6d 6e 2e 20 45 78 65   one column. Exe
29e1b 63 75 74 65 20 74 68 69 73 20 61 73 20 53 51 4c  cute this as SQL
29e1c 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   on the same dat
29e1d 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  abase..*/.static
29e1e 20 69 6e 74 20 65 78 65 63 45 78 65 63 53 71 6c   int execExecSql
29e1f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
29e20 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b  nst char *zSql){
29e21 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
29e22 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63  *pStmt;.  int rc
29e23 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
29e24 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53  3_prepare(db, zS
29e25 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20  ql, -1, &pStmt, 
29e26 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  0);.  if( rc!=SQ
29e27 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
29e28 20 72 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 53   rc;..  while( S
29e29 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
29e2a 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
29e2b 7b 0a 20 20 20 20 72 63 20 3d 20 65 78 65 63 53  {.    rc = execS
29e2c 71 6c 28 64 62 2c 20 28 63 68 61 72 2a 29 73 71  ql(db, (char*)sq
29e2d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
29e2e 74 28 70 53 74 6d 74 2c 20 30 29 29 3b 0a 20 20  t(pStmt, 0));.  
29e2f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
29e30 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
29e31 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
29e32 74 6d 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  tmt);.      retu
29e33 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
29e34 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
29e35 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
29e36 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  t);.}../*.** The
29e37 20 6e 6f 6e 2d 73 74 61 6e 64 61 72 64 20 56 41   non-standard VA
29e38 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 69 73 20  CUUM command is 
29e39 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70  used to clean up
29e3a 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a   the database,.*
29e3b 2a 20 63 6f 6c 6c 61 70 73 65 20 66 72 65 65 20  * collapse free 
29e3c 73 70 61 63 65 2c 20 65 74 63 2e 20 20 49 74 20  space, etc.  It 
29e3d 69 73 20 6d 6f 64 65 6c 6c 65 64 20 61 66 74 65  is modelled afte
29e3e 72 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d  r the VACUUM com
29e3f 6d 61 6e 64 0a 2a 2a 20 69 6e 20 50 6f 73 74 67  mand.** in Postg
29e40 72 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  reSQL..**.** In 
29e41 76 65 72 73 69 6f 6e 20 31 2e 30 2e 78 20 6f 66  version 1.0.x of
29e42 20 53 51 4c 69 74 65 2c 20 74 68 65 20 56 41 43   SQLite, the VAC
29e43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 77 6f 75 6c  UUM command woul
29e44 64 20 63 61 6c 6c 0a 2a 2a 20 67 64 62 6d 5f 72  d call.** gdbm_r
29e45 65 6f 72 67 61 6e 69 7a 65 28 29 20 6f 6e 20 61  eorganize() on a
29e46 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ll the database 
29e47 74 61 62 6c 65 73 2e 20 20 42 75 74 20 62 65 67  tables.  But beg
29e48 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 32  inning.** with 2
29e49 2e 30 2e 30 2c 20 53 51 4c 69 74 65 20 6e 6f 20  .0.0, SQLite no 
29e4a 6c 6f 6e 67 65 72 20 75 73 65 73 20 47 44 42 4d  longer uses GDBM
29e4b 20 73 6f 20 74 68 69 73 20 63 6f 6d 6d 61 6e 64   so this command
29e4c 20 68 61 73 0a 2a 2a 20 62 65 63 6f 6d 65 20 61   has.** become a
29e4d 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
29e4e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
29e4f 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72  qlite3Vacuum(Par
29e50 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 56  se *pParse){.  V
29e51 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
29e52 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
29e53 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
29e54 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29e55 32 28 76 2c 20 4f 50 5f 56 61 63 75 75 6d 2c 20  2(v, OP_Vacuum, 
29e56 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  0, 0);.  }.  ret
29e57 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  urn;.}../*.** Th
29e58 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65  is routine imple
29e59 6d 65 6e 74 73 20 74 68 65 20 4f 50 5f 56 61 63  ments the OP_Vac
29e5a 75 75 6d 20 6f 70 63 6f 64 65 20 6f 66 20 74 68  uum opcode of th
29e5b 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54  e VDBE..*/.SQLIT
29e5c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
29e5d 6c 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 63  lite3RunVacuum(c
29e5e 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 20  har **pzErrMsg, 
29e5f 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
29e60 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
29e61 4f 4b 3b 20 20 20 20 20 2f 2a 20 52 65 74 75 72  OK;     /* Retur
29e62 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 65 72 76  n code from serv
29e63 69 63 65 20 72 6f 75 74 69 6e 65 73 20 2a 2f 0a  ice routines */.
29e64 20 20 42 74 72 65 65 20 2a 70 4d 61 69 6e 3b 20    Btree *pMain; 
29e65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
29e66 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20   database being 
29e67 76 61 63 75 75 6d 65 64 20 2a 2f 0a 20 20 50 61  vacuumed */.  Pa
29e68 67 65 72 20 2a 70 4d 61 69 6e 50 61 67 65 72 3b  ger *pMainPager;
29e69 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 66        /* Pager f
29e6a 6f 72 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  or database bein
29e6b 67 20 76 61 63 75 75 6d 65 64 20 2a 2f 0a 20 20  g vacuumed */.  
29e6c 42 74 72 65 65 20 2a 70 54 65 6d 70 3b 20 20 20  Btree *pTemp;   
29e6d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
29e6e 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
29e6f 65 20 77 65 20 76 61 63 75 75 6d 20 69 6e 74 6f  e we vacuum into
29e70 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c   */.  char *zSql
29e71 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
29e72 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
29e73 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 66  */.  int saved_f
29e74 6c 61 67 73 3b 20 20 20 20 20 20 20 20 2f 2a 20  lags;        /* 
29e75 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 74  Saved value of t
29e76 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a  he db->flags */.
29e77 20 20 69 6e 74 20 73 61 76 65 64 5f 6e 43 68 61    int saved_nCha
29e78 6e 67 65 3b 20 20 20 20 20 20 2f 2a 20 53 61 76  nge;      /* Sav
29e79 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e  ed value of db->
29e7a 6e 43 68 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74  nChange */.  int
29e7b 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61   saved_nTotalCha
29e7c 6e 67 65 3b 20 2f 2a 20 53 61 76 65 64 20 76 61  nge; /* Saved va
29e7d 6c 75 65 20 6f 66 20 64 62 2d 3e 6e 54 6f 74 61  lue of db->nTota
29e7e 6c 43 68 61 6e 67 65 20 2a 2f 0a 20 20 44 62 20  lChange */.  Db 
29e7f 2a 70 44 62 20 3d 20 30 3b 20 20 20 20 20 20 20  *pDb = 0;       
29e80 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
29e81 20 74 6f 20 64 65 74 61 63 68 20 61 74 20 65 6e   to detach at en
29e82 64 20 6f 66 20 76 61 63 75 75 6d 20 2a 2f 0a 20  d of vacuum */. 
29e83 20 69 6e 74 20 69 73 4d 65 6d 44 62 3b 20 20 20   int isMemDb;   
29e84 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
29e85 20 69 73 20 76 61 63 75 75 6d 69 6e 67 20 61 20   is vacuuming a 
29e86 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73  :memory: databas
29e87 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 3b  e */.  int nRes;
29e88 0a 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74  ..  if( !db->aut
29e89 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73  oCommit ){.    s
29e8a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
29e8b 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63  pzErrMsg, db, "c
29e8c 61 6e 6e 6f 74 20 56 41 43 55 55 4d 20 66 72 6f  annot VACUUM fro
29e8d 6d 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73  m within a trans
29e8e 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 72 65  action");.    re
29e8f 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
29e90 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76  R;.  }..  /* Sav
29e91 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  e the current va
29e92 6c 75 65 20 6f 66 20 74 68 65 20 77 72 69 74 65  lue of the write
29e93 2d 73 63 68 65 6d 61 20 66 6c 61 67 20 62 65 66  -schema flag bef
29e94 6f 72 65 20 73 65 74 74 69 6e 67 20 69 74 2e 20  ore setting it. 
29e95 2a 2f 0a 20 20 73 61 76 65 64 5f 66 6c 61 67 73  */.  saved_flags
29e96 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20   = db->flags;.  
29e97 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 20 3d 20  saved_nChange = 
29e98 64 62 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 73  db->nChange;.  s
29e99 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61 6e 67  aved_nTotalChang
29e9a 65 20 3d 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68  e = db->nTotalCh
29e9b 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 66 6c 61 67  ange;.  db->flag
29e9c 73 20 7c 3d 20 53 51 4c 49 54 45 5f 57 72 69 74  s |= SQLITE_Writ
29e9d 65 53 63 68 65 6d 61 20 7c 20 53 51 4c 49 54 45  eSchema | SQLITE
29e9e 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 3b 0a 0a  _IgnoreChecks;..
29e9f 20 20 70 4d 61 69 6e 20 3d 20 64 62 2d 3e 61 44    pMain = db->aD
29ea0 62 5b 30 5d 2e 70 42 74 3b 0a 20 20 70 4d 61 69  b[0].pBt;.  pMai
29ea1 6e 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  nPager = sqlite3
29ea2 42 74 72 65 65 50 61 67 65 72 28 70 4d 61 69 6e  BtreePager(pMain
29ea3 29 3b 0a 20 20 69 73 4d 65 6d 44 62 20 3d 20 73  );.  isMemDb = s
29ea4 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28  qlite3PagerFile(
29ea5 70 4d 61 69 6e 50 61 67 65 72 29 2d 3e 70 4d 65  pMainPager)->pMe
29ea6 74 68 6f 64 73 3d 3d 30 3b 0a 0a 20 20 2f 2a 20  thods==0;..  /* 
29ea7 41 74 74 61 63 68 20 74 68 65 20 74 65 6d 70 6f  Attach the tempo
29ea8 72 61 72 79 20 64 61 74 61 62 61 73 65 20 61 73  rary database as
29ea9 20 27 76 61 63 75 75 6d 5f 64 62 27 2e 20 54 68   'vacuum_db'. Th
29eaa 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 70 72  e synchronous pr
29eab 61 67 6d 61 0a 20 20 2a 2a 20 63 61 6e 20 62 65  agma.  ** can be
29eac 20 73 65 74 20 74 6f 20 27 6f 66 66 27 20 66 6f   set to 'off' fo
29ead 72 20 74 68 69 73 20 66 69 6c 65 2c 20 61 73 20  r this file, as 
29eae 69 74 20 69 73 20 6e 6f 74 20 72 65 63 6f 76 65  it is not recove
29eaf 72 65 64 20 69 66 20 61 20 63 72 61 73 68 0a 20  red if a crash. 
29eb0 20 2a 2a 20 6f 63 63 75 72 73 20 61 6e 79 77 61   ** occurs anywa
29eb1 79 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74 79  y. The integrity
29eb2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
29eb3 20 69 73 20 6d 61 69 6e 74 61 69 6e 65 64 20 62   is maintained b
29eb4 79 20 61 0a 20 20 2a 2a 20 28 70 6f 73 73 69 62  y a.  ** (possib
29eb5 6c 79 20 73 79 6e 63 68 72 6f 6e 6f 75 73 29 20  ly synchronous) 
29eb6 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
29eb7 65 64 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64  ed on the main d
29eb8 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 20  atabase before. 
29eb9 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65   ** sqlite3Btree
29eba 43 6f 70 79 46 69 6c 65 28 29 20 69 73 20 63 61  CopyFile() is ca
29ebb 6c 6c 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  lled..  **.  ** 
29ebc 41 6e 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e 20  An optimisation 
29ebd 77 6f 75 6c 64 20 62 65 20 74 6f 20 75 73 65 20  would be to use 
29ebe 61 20 6e 6f 6e 2d 6a 6f 75 72 6e 61 6c 65 64 20  a non-journaled 
29ebf 70 61 67 65 72 2e 0a 20 20 2a 2a 20 28 4c 61 74  pager..  ** (Lat
29ec0 65 72 3a 29 20 49 20 74 72 69 65 64 20 73 65 74  er:) I tried set
29ec1 74 69 6e 67 20 22 50 52 41 47 4d 41 20 76 61 63  ting "PRAGMA vac
29ec2 75 75 6d 5f 64 62 2e 6a 6f 75 72 6e 61 6c 5f 6d  uum_db.journal_m
29ec3 6f 64 65 3d 4f 46 46 22 20 62 75 74 0a 20 20 2a  ode=OFF" but.  *
29ec4 2a 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20  * that actually 
29ec5 6d 61 64 65 20 74 68 65 20 56 41 43 55 55 4d 20  made the VACUUM 
29ec6 72 75 6e 20 73 6c 6f 77 65 72 2e 20 20 56 65 72  run slower.  Ver
29ec7 79 20 6c 69 74 74 6c 65 20 6a 6f 75 72 6e 61 6c  y little journal
29ec8 6c 69 6e 67 0a 20 20 2a 2a 20 61 63 74 75 61 6c  ling.  ** actual
29ec9 6c 79 20 6f 63 63 75 72 73 20 77 68 65 6e 20 64  ly occurs when d
29eca 6f 69 6e 67 20 61 20 76 61 63 75 75 6d 20 73 69  oing a vacuum si
29ecb 6e 63 65 20 74 68 65 20 76 61 63 75 75 6d 5f 64  nce the vacuum_d
29ecc 62 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 0a 20  b is initially. 
29ecd 20 2a 2a 20 65 6d 70 74 79 2e 20 20 4f 6e 6c 79   ** empty.  Only
29ece 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
29ecf 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e 20  der is written. 
29ed0 20 41 70 70 61 72 65 6e 74 6c 79 20 69 74 20 74   Apparently it t
29ed1 61 6b 65 73 20 6d 6f 72 65 0a 20 20 2a 2a 20 74  akes more.  ** t
29ed2 69 6d 65 20 74 6f 20 70 61 72 73 65 20 61 6e 64  ime to parse and
29ed3 20 72 75 6e 20 74 68 65 20 50 52 41 47 4d 41 20   run the PRAGMA 
29ed4 74 6f 20 74 75 72 6e 20 6a 6f 75 72 6e 61 6c 6c  to turn journall
29ed5 69 6e 67 20 6f 66 66 20 74 68 61 6e 20 69 74 20  ing off than it 
29ed6 64 6f 65 73 0a 20 20 2a 2a 20 74 6f 20 77 72 69  does.  ** to wri
29ed7 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  te the journal h
29ed8 65 61 64 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f  eader file..  */
29ed9 0a 20 20 7a 53 71 6c 20 3d 20 22 41 54 54 41 43  .  zSql = "ATTAC
29eda 48 20 27 27 20 41 53 20 76 61 63 75 75 6d 5f 64  H '' AS vacuum_d
29edb 62 3b 22 3b 0a 20 20 72 63 20 3d 20 65 78 65 63  b;";.  rc = exec
29edc 53 71 6c 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20  Sql(db, zSql);. 
29edd 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
29ede 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
29edf 5f 76 61 63 75 75 6d 3b 0a 20 20 70 44 62 20 3d  _vacuum;.  pDb =
29ee0 20 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44   &db->aDb[db->nD
29ee1 62 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  b-1];.  assert( 
29ee2 73 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b 64  strcmp(db->aDb[d
29ee3 62 2d 3e 6e 44 62 2d 31 5d 2e 7a 4e 61 6d 65 2c  b->nDb-1].zName,
29ee4 22 76 61 63 75 75 6d 5f 64 62 22 29 3d 3d 30 20  "vacuum_db")==0 
29ee5 29 3b 0a 20 20 70 54 65 6d 70 20 3d 20 64 62 2d  );.  pTemp = db-
29ee6 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 2e  >aDb[db->nDb-1].
29ee7 70 42 74 3b 0a 0a 20 20 6e 52 65 73 20 3d 20 73  pBt;..  nRes = s
29ee8 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 65  qlite3BtreeGetRe
29ee9 73 65 72 76 65 28 70 4d 61 69 6e 29 3b 0a 0a 20  serve(pMain);.. 
29eea 20 2f 2a 20 41 20 56 41 43 55 55 4d 20 63 61 6e   /* A VACUUM can
29eeb 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 70  not change the p
29eec 61 67 65 73 69 7a 65 20 6f 66 20 61 6e 20 65 6e  agesize of an en
29eed 63 72 79 70 74 65 64 20 64 61 74 61 62 61 73 65  crypted database
29eee 2e 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  . */.#ifdef SQLI
29eef 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69  TE_HAS_CODEC.  i
29ef0 66 28 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73  f( db->nextPages
29ef1 69 7a 65 20 29 7b 0a 20 20 20 20 65 78 74 65 72  ize ){.    exter
29ef2 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  n void sqlite3Co
29ef3 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65  decGetKey(sqlite
29ef4 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c  3*, int, void**,
29ef5 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20   int*);.    int 
29ef6 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a  nKey;.    char *
29ef7 7a 4b 65 79 3b 0a 20 20 20 20 73 71 6c 69 74 65  zKey;.    sqlite
29ef8 33 43 6f 64 65 63 47 65 74 4b 65 79 28 64 62 2c  3CodecGetKey(db,
29ef9 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b 65   0, (void**)&zKe
29efa 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 20 20 69  y, &nKey);.    i
29efb 66 28 20 6e 4b 65 79 20 29 20 64 62 2d 3e 6e 65  f( nKey ) db->ne
29efc 78 74 50 61 67 65 73 69 7a 65 20 3d 20 30 3b 0a  xtPagesize = 0;.
29efd 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
29efe 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  ( sqlite3BtreeSe
29eff 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 2c  tPageSize(pTemp,
29f00 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
29f01 50 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 29 2c  PageSize(pMain),
29f02 20 6e 52 65 73 29 0a 20 20 20 7c 7c 20 28 21 69   nRes).   || (!i
29f03 73 4d 65 6d 44 62 20 26 26 20 73 71 6c 69 74 65  sMemDb && sqlite
29f04 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a  3BtreeSetPageSiz
29f05 65 28 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78  e(pTemp, db->nex
29f06 74 50 61 67 65 73 69 7a 65 2c 20 6e 52 65 73 29  tPagesize, nRes)
29f07 29 0a 20 20 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  ).   || db->mall
29f08 6f 63 46 61 69 6c 65 64 20 0a 20 20 29 7b 0a 20  ocFailed .  ){. 
29f09 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
29f0a 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65  OMEM;.    goto e
29f0b 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
29f0c 7d 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c  }.  rc = execSql
29f0d 28 64 62 2c 20 22 50 52 41 47 4d 41 20 76 61 63  (db, "PRAGMA vac
29f0e 75 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e 6f  uum_db.synchrono
29f0f 75 73 3d 4f 46 46 22 29 3b 0a 20 20 69 66 28 20  us=OFF");.  if( 
29f10 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
29f11 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  .    goto end_of
29f12 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23 69  _vacuum;.  }..#i
29f13 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29f14 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 73  T_AUTOVACUUM.  s
29f15 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
29f16 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 2c 20  toVacuum(pTemp, 
29f17 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 3e  db->nextAutovac>
29f18 3d 30 20 3f 20 64 62 2d 3e 6e 65 78 74 41 75 74  =0 ? db->nextAut
29f19 6f 76 61 63 20 3a 0a 20 20 20 20 20 20 20 20 20  ovac :.         
29f1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f1c 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
29f1d 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69  tAutoVacuum(pMai
29f1e 6e 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  n));.#endif..  /
29f1f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61  * Begin a transa
29f20 63 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d 20  ction */.  rc = 
29f21 65 78 65 63 53 71 6c 28 64 62 2c 20 22 42 45 47  execSql(db, "BEG
29f22 49 4e 20 45 58 43 4c 55 53 49 56 45 3b 22 29 3b  IN EXCLUSIVE;");
29f23 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
29f24 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
29f25 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a  of_vacuum;..  /*
29f26 20 51 75 65 72 79 20 74 68 65 20 73 63 68 65 6d   Query the schem
29f27 61 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61  a of the main da
29f28 74 61 62 61 73 65 2e 20 43 72 65 61 74 65 20 61  tabase. Create a
29f29 20 6d 69 72 72 6f 72 20 73 63 68 65 6d 61 0a 20   mirror schema. 
29f2a 20 2a 2a 20 69 6e 20 74 68 65 20 74 65 6d 70 6f   ** in the tempo
29f2b 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20  rary database.. 
29f2c 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45   */.  rc = execE
29f2d 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20  xecSql(db, .    
29f2e 20 20 22 53 45 4c 45 43 54 20 27 43 52 45 41 54    "SELECT 'CREAT
29f2f 45 20 54 41 42 4c 45 20 76 61 63 75 75 6d 5f 64  E TABLE vacuum_d
29f30 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71  b.' || substr(sq
29f31 6c 2c 31 34 29 20 22 0a 20 20 20 20 20 20 22 20  l,14) ".      " 
29f32 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73   FROM sqlite_mas
29f33 74 65 72 20 57 48 45 52 45 20 74 79 70 65 3d 27  ter WHERE type='
29f34 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65 21  table' AND name!
29f35 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  ='sqlite_sequenc
29f36 65 27 22 0a 20 20 20 20 20 20 22 20 20 20 41 4e  e'".      "   AN
29f37 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 20 20  D rootpage>0".  
29f38 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
29f39 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
29f3a 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72  d_of_vacuum;.  r
29f3b 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28  c = execExecSql(
29f3c 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45  db, .      "SELE
29f3d 43 54 20 27 43 52 45 41 54 45 20 49 4e 44 45 58  CT 'CREATE INDEX
29f3e 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
29f3f 73 75 62 73 74 72 28 73 71 6c 2c 31 34 29 22 0a  substr(sql,14)".
29f40 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71        "  FROM sq
29f41 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52  lite_master WHER
29f42 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45 41  E sql LIKE 'CREA
29f43 54 45 20 49 4e 44 45 58 20 25 27 20 22 29 3b 0a  TE INDEX %' ");.
29f44 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
29f45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
29f46 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d  f_vacuum;.  rc =
29f47 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c   execExecSql(db,
29f48 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20   .      "SELECT 
29f49 27 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49  'CREATE UNIQUE I
29f4a 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e 27  NDEX vacuum_db.'
29f4b 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 32   || substr(sql,2
29f4c 31 29 20 22 0a 20 20 20 20 20 20 22 20 20 46 52  1) ".      "  FR
29f4d 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
29f4e 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45 20   WHERE sql LIKE 
29f4f 27 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49  'CREATE UNIQUE I
29f50 4e 44 45 58 20 25 27 22 29 3b 0a 20 20 69 66 28  NDEX %'");.  if(
29f51 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
29f52 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
29f53 75 75 6d 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20  uum;..  /* Loop 
29f54 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62 6c  through the tabl
29f55 65 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  es in the main d
29f56 61 74 61 62 61 73 65 2e 20 46 6f 72 20 65 61 63  atabase. For eac
29f57 68 2c 20 64 6f 0a 20 20 2a 2a 20 61 6e 20 22 49  h, do.  ** an "I
29f58 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75  NSERT INTO vacuu
29f59 6d 5f 64 62 2e 78 78 78 20 53 45 4c 45 43 54 20  m_db.xxx SELECT 
29f5a 2a 20 46 52 4f 4d 20 78 78 78 3b 22 20 74 6f 20  * FROM xxx;" to 
29f5b 63 6f 70 79 0a 20 20 2a 2a 20 74 68 65 20 63 6f  copy.  ** the co
29f5c 6e 74 65 6e 74 73 20 74 6f 20 74 68 65 20 74 65  ntents to the te
29f5d 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
29f5e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78  ..  */.  rc = ex
29f5f 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20  ecExecSql(db, . 
29f60 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 49 4e       "SELECT 'IN
29f61 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d  SERT INTO vacuum
29f62 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e  _db.' || quote(n
29f63 61 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c 7c  ame) ".      "||
29f64 20 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   ' SELECT * FROM
29f65 20 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65   ' || quote(name
29f66 29 20 7c 7c 20 27 3b 27 22 0a 20 20 20 20 20 20  ) || ';'".      
29f67 22 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73  "FROM sqlite_mas
29f68 74 65 72 20 22 0a 20 20 20 20 20 20 22 57 48 45  ter ".      "WHE
29f69 52 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65  RE type = 'table
29f6a 27 20 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c  ' AND name!='sql
29f6b 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22 0a  ite_sequence' ".
29f6c 20 20 20 20 20 20 22 20 20 41 4e 44 20 72 6f 6f        "  AND roo
29f6d 74 70 61 67 65 3e 30 22 0a 0a 20 20 29 3b 0a 20  tpage>0"..  );. 
29f6e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
29f6f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
29f70 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 43  _vacuum;..  /* C
29f71 6f 70 79 20 6f 76 65 72 20 74 68 65 20 73 65 71  opy over the seq
29f72 75 65 6e 63 65 20 74 61 62 6c 65 0a 20 20 2a 2f  uence table.  */
29f73 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63  .  rc = execExec
29f74 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22  Sql(db, .      "
29f75 53 45 4c 45 43 54 20 27 44 45 4c 45 54 45 20 46  SELECT 'DELETE F
29f76 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 27 20  ROM vacuum_db.' 
29f77 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c  || quote(name) |
29f78 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20 22 46  | ';' ".      "F
29f79 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73 71  ROM vacuum_db.sq
29f7a 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52  lite_master WHER
29f7b 45 20 6e 61 6d 65 3d 27 73 71 6c 69 74 65 5f 73  E name='sqlite_s
29f7c 65 71 75 65 6e 63 65 27 20 22 0a 20 20 29 3b 0a  equence' ".  );.
29f7d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
29f7e 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
29f7f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d  f_vacuum;.  rc =
29f80 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c   execExecSql(db,
29f81 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20   .      "SELECT 
29f82 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63  'INSERT INTO vac
29f83 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74  uum_db.' || quot
29f84 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20 20 20  e(name) ".      
29f85 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a 20 46  "|| ' SELECT * F
29f86 52 4f 4d 20 27 20 7c 7c 20 71 75 6f 74 65 28 6e  ROM ' || quote(n
29f87 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a 20 20  ame) || ';' ".  
29f88 20 20 20 20 22 46 52 4f 4d 20 76 61 63 75 75 6d      "FROM vacuum
29f89 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65  _db.sqlite_maste
29f8a 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 3d 27 73  r WHERE name=='s
29f8b 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 3b  qlite_sequence';
29f8c 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21  ".  );.  if( rc!
29f8d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
29f8e 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
29f8f 0a 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65  ...  /* Copy the
29f90 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77 73   triggers, views
29f91 2c 20 61 6e 64 20 76 69 72 74 75 61 6c 20 74 61  , and virtual ta
29f92 62 6c 65 73 20 66 72 6f 6d 20 74 68 65 20 6d 61  bles from the ma
29f93 69 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  in database.  **
29f94 20 6f 76 65 72 20 74 6f 20 74 68 65 20 74 65 6d   over to the tem
29f95 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2e  porary database.
29f96 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 73 65 20    None of these 
29f97 6f 62 6a 65 63 74 73 20 68 61 73 20 61 6e 79 0a  objects has any.
29f98 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20    ** associated 
29f99 73 74 6f 72 61 67 65 2c 20 73 6f 20 61 6c 6c 20  storage, so all 
29f9a 77 65 20 68 61 76 65 20 74 6f 20 64 6f 20 69 73  we have to do is
29f9b 20 63 6f 70 79 20 74 68 65 69 72 20 65 6e 74 72   copy their entr
29f9c 69 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68  ies.  ** from th
29f9d 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  e SQLITE_MASTER 
29f9e 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  table..  */.  rc
29f9f 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 0a 20   = execSql(db,. 
29fa0 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54       "INSERT INT
29fa1 4f 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69  O vacuum_db.sqli
29fa2 74 65 5f 6d 61 73 74 65 72 20 22 0a 20 20 20 20  te_master ".    
29fa3 20 20 22 20 20 53 45 4c 45 43 54 20 74 79 70 65    "  SELECT type
29fa4 2c 20 6e 61 6d 65 2c 20 74 62 6c 5f 6e 61 6d 65  , name, tbl_name
29fa5 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 22  , rootpage, sql"
29fa6 0a 20 20 20 20 20 20 22 20 20 20 20 46 52 4f 4d  .      "    FROM
29fa7 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a   sqlite_master".
29fa8 20 20 20 20 20 20 22 20 20 20 57 48 45 52 45 20        "   WHERE 
29fa9 74 79 70 65 3d 27 76 69 65 77 27 20 4f 52 20 74  type='view' OR t
29faa 79 70 65 3d 27 74 72 69 67 67 65 72 27 22 0a 20  ype='trigger'". 
29fab 20 20 20 20 20 22 20 20 20 20 20 20 4f 52 20 28       "      OR (
29fac 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e 44  type='table' AND
29fad 20 72 6f 6f 74 70 61 67 65 3d 30 29 22 0a 20 20   rootpage=0)".  
29fae 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f  );.  if( rc ) go
29faf 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
29fb0 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  ;..  /* At this 
29fb1 70 6f 69 6e 74 2c 20 75 6e 6c 65 73 73 20 74 68  point, unless th
29fb2 65 20 6d 61 69 6e 20 64 62 20 77 61 73 20 63 6f  e main db was co
29fb3 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2c 20  mpletely empty, 
29fb4 74 68 65 72 65 20 69 73 20 6e 6f 77 20 61 0a 20  there is now a. 
29fb5 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
29fb6 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75  open on the vacu
29fb7 75 6d 20 64 61 74 61 62 61 73 65 2c 20 62 75 74  um database, but
29fb8 20 6e 6f 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e   not on the main
29fb9 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20   database..  ** 
29fba 4f 70 65 6e 20 61 20 62 74 72 65 65 20 6c 65 76  Open a btree lev
29fbb 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  el transaction o
29fbc 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
29fbd 61 73 65 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73  ase. This allows
29fbe 20 61 0a 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20   a.  ** call to 
29fbf 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79  sqlite3BtreeCopy
29fc0 46 69 6c 65 28 29 2e 20 54 68 65 20 6d 61 69 6e  File(). The main
29fc1 20 64 61 74 61 62 61 73 65 20 62 74 72 65 65 20   database btree 
29fc2 6c 65 76 65 6c 0a 20 20 2a 2a 20 74 72 61 6e 73  level.  ** trans
29fc3 61 63 74 69 6f 6e 20 69 73 20 74 68 65 6e 20 63  action is then c
29fc4 6f 6d 6d 69 74 74 65 64 2c 20 73 6f 20 74 68 65  ommitted, so the
29fc5 20 53 51 4c 20 6c 65 76 65 6c 20 6e 65 76 65 72   SQL level never
29fc6 20 6b 6e 6f 77 73 20 69 74 20 77 61 73 0a 20 20   knows it was.  
29fc7 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  ** opened for wr
29fc8 69 74 69 6e 67 2e 20 54 68 69 73 20 77 61 79 2c  iting. This way,
29fc9 20 74 68 65 20 53 51 4c 20 74 72 61 6e 73 61 63   the SQL transac
29fca 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65  tion used to cre
29fcb 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 74 65 6d  ate the.  ** tem
29fcc 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20  porary database 
29fcd 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62  never needs to b
29fce 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 2a  e committed..  *
29fcf 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
29fd0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 33 32  TE_OK ){.    u32
29fd1 20 6d 65 74 61 3b 0a 20 20 20 20 69 6e 74 20 69   meta;.    int i
29fd2 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 61  ;..    /* This a
29fd3 72 72 61 79 20 64 65 74 65 72 6d 69 6e 65 73 20  rray determines 
29fd4 77 68 69 63 68 20 6d 65 74 61 20 6d 65 74 61 20  which meta meta 
29fd5 76 61 6c 75 65 73 20 61 72 65 20 70 72 65 73 65  values are prese
29fd6 72 76 65 64 20 69 6e 20 74 68 65 0a 20 20 20 20  rved in the.    
29fd7 2a 2a 20 76 61 63 75 75 6d 2e 20 20 45 76 65 6e  ** vacuum.  Even
29fd8 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65   entries are the
29fd9 20 6d 65 74 61 20 76 61 6c 75 65 20 6e 75 6d 62   meta value numb
29fda 65 72 20 61 6e 64 20 6f 64 64 20 65 6e 74 72 69  er and odd entri
29fdb 65 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 61 6e  es.    ** are an
29fdc 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 61 70   increment to ap
29fdd 70 6c 79 20 74 6f 20 74 68 65 20 6d 65 74 61 20  ply to the meta 
29fde 76 61 6c 75 65 20 61 66 74 65 72 20 74 68 65 20  value after the 
29fdf 76 61 63 75 75 6d 2e 0a 20 20 20 20 2a 2a 20 54  vacuum..    ** T
29fe0 68 65 20 69 6e 63 72 65 6d 65 6e 74 20 69 73 20  he increment is 
29fe1 75 73 65 64 20 74 6f 20 69 6e 63 72 65 61 73 65  used to increase
29fe2 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
29fe3 69 65 20 73 6f 20 74 68 61 74 20 6f 74 68 65 72  ie so that other
29fe4 0a 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69  .    ** connecti
29fe5 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ons to the same 
29fe6 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 6b 6e  database will kn
29fe7 6f 77 20 74 6f 20 72 65 72 65 61 64 20 74 68 65  ow to reread the
29fe8 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a   schema..    */.
29fe9 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
29fea 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
29feb 43 6f 70 79 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Copy[] = {.     
29fec 20 20 31 2c 20 31 2c 20 20 20 20 2f 2a 20 41 64    1, 1,    /* Ad
29fed 64 20 6f 6e 65 20 74 6f 20 74 68 65 20 6f 6c 64  d one to the old
29fee 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a   schema cookie *
29fef 2f 0a 20 20 20 20 20 20 20 33 2c 20 30 2c 20 20  /.       3, 0,  
29ff0 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74 68    /* Preserve th
29ff1 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63  e default page c
29ff2 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20  ache size */.   
29ff3 20 20 20 20 35 2c 20 30 2c 20 20 20 20 2f 2a 20      5, 0,    /* 
29ff4 50 72 65 73 65 72 76 65 20 74 68 65 20 64 65 66  Preserve the def
29ff5 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69  ault text encodi
29ff6 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 36 2c 20  ng */.       6, 
29ff7 30 2c 20 20 20 20 2f 2a 20 50 72 65 73 65 72 76  0,    /* Preserv
29ff8 65 20 74 68 65 20 75 73 65 72 20 76 65 72 73 69  e the user versi
29ff9 6f 6e 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20  on */.    };..  
29ffa 20 20 61 73 73 65 72 74 28 20 31 3d 3d 73 71 6c    assert( 1==sql
29ffb 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
29ffc 6e 73 28 70 54 65 6d 70 29 20 29 3b 0a 20 20 20  ns(pTemp) );.   
29ffd 20 61 73 73 65 72 74 28 20 31 3d 3d 73 71 6c 69   assert( 1==sqli
29ffe 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
29fff 73 28 70 4d 61 69 6e 29 20 29 3b 0a 0a 20 20 20  s(pMain) );..   
2a000 20 2f 2a 20 43 6f 70 79 20 42 74 72 65 65 20 6d   /* Copy Btree m
2a001 65 74 61 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  eta values */.  
2a002 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72    for(i=0; i<Arr
2a003 61 79 53 69 7a 65 28 61 43 6f 70 79 29 3b 20 69  aySize(aCopy); i
2a004 2b 3d 32 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  +=2){.      rc =
2a005 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
2a006 4d 65 74 61 28 70 4d 61 69 6e 2c 20 61 43 6f 70  Meta(pMain, aCop
2a007 79 5b 69 5d 2c 20 26 6d 65 74 61 29 3b 0a 20 20  y[i], &meta);.  
2a008 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2a009 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
2a00a 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20  _of_vacuum;.    
2a00b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
2a00c 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 54  reeUpdateMeta(pT
2a00d 65 6d 70 2c 20 61 43 6f 70 79 5b 69 5d 2c 20 6d  emp, aCopy[i], m
2a00e 65 74 61 2b 61 43 6f 70 79 5b 69 2b 31 5d 29 3b  eta+aCopy[i+1]);
2a00f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
2a010 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2a011 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2a012 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73     }..    rc = s
2a013 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46  qlite3BtreeCopyF
2a014 69 6c 65 28 70 4d 61 69 6e 2c 20 70 54 65 6d 70  ile(pMain, pTemp
2a015 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
2a016 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2a017 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2a018 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
2a019 74 72 65 65 43 6f 6d 6d 69 74 28 70 54 65 6d 70  treeCommit(pTemp
2a01a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
2a01b 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2a01c 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 23  end_of_vacuum;.#
2a01d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2a01e 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
2a01f 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
2a020 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69  tAutoVacuum(pMai
2a021 6e 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  n, sqlite3BtreeG
2a022 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54 65  etAutoVacuum(pTe
2a023 6d 70 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  mp));.#endif.  }
2a024 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
2a025 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
2a026 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  = sqlite3BtreeSe
2a027 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 2c  tPageSize(pMain,
2a028 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
2a029 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 29 2c  PageSize(pTemp),
2a02a 20 6e 52 65 73 29 3b 0a 20 20 7d 0a 0a 65 6e 64   nRes);.  }..end
2a02b 5f 6f 66 5f 76 61 63 75 75 6d 3a 0a 20 20 2f 2a  _of_vacuum:.  /*
2a02c 20 52 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69   Restore the ori
2a02d 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 64  ginal value of d
2a02e 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 64 62  b->flags */.  db
2a02f 2d 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64 5f  ->flags = saved_
2a030 66 6c 61 67 73 3b 0a 20 20 64 62 2d 3e 6e 43 68  flags;.  db->nCh
2a031 61 6e 67 65 20 3d 20 73 61 76 65 64 5f 6e 43 68  ange = saved_nCh
2a032 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74  ange;.  db->nTot
2a033 61 6c 43 68 61 6e 67 65 20 3d 20 73 61 76 65 64  alChange = saved
2a034 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 0a  _nTotalChange;..
2a035 20 20 2f 2a 20 43 75 72 72 65 6e 74 6c 79 20 74    /* Currently t
2a036 68 65 72 65 20 69 73 20 61 6e 20 53 51 4c 20 6c  here is an SQL l
2a037 65 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e  evel transaction
2a038 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61 63   open on the vac
2a039 75 75 6d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  uum.  ** databas
2a03a 65 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20  e. No locks are 
2a03b 68 65 6c 64 20 6f 6e 20 61 6e 79 20 6f 74 68 65  held on any othe
2a03c 72 20 66 69 6c 65 73 20 28 73 69 6e 63 65 20 74  r files (since t
2a03d 68 65 20 6d 61 69 6e 20 66 69 6c 65 0a 20 20 2a  he main file.  *
2a03e 2a 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 20  * was committed 
2a03f 61 74 20 74 68 65 20 62 74 72 65 65 20 6c 65 76  at the btree lev
2a040 65 6c 29 2e 20 53 6f 20 69 74 20 73 61 66 65 20  el). So it safe 
2a041 74 6f 20 65 6e 64 20 74 68 65 20 74 72 61 6e 73  to end the trans
2a042 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 62 79 20 6d  action.  ** by m
2a043 61 6e 75 61 6c 6c 79 20 73 65 74 74 69 6e 67 20  anually setting 
2a044 74 68 65 20 61 75 74 6f 43 6f 6d 6d 69 74 20 66  the autoCommit f
2a045 6c 61 67 20 74 6f 20 74 72 75 65 20 61 6e 64 20  lag to true and 
2a046 64 65 74 61 63 68 69 6e 67 20 74 68 65 0a 20 20  detaching the.  
2a047 2a 2a 20 76 61 63 75 75 6d 20 64 61 74 61 62 61  ** vacuum databa
2a048 73 65 2e 20 54 68 65 20 76 61 63 75 75 6d 5f 64  se. The vacuum_d
2a049 62 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  b journal file i
2a04a 73 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74  s deleted when t
2a04b 68 65 20 70 61 67 65 72 0a 20 20 2a 2a 20 69 73  he pager.  ** is
2a04c 20 63 6c 6f 73 65 64 20 62 79 20 74 68 65 20 44   closed by the D
2a04d 45 54 41 43 48 2e 0a 20 20 2a 2f 0a 20 20 64 62  ETACH..  */.  db
2a04e 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31  ->autoCommit = 1
2a04f 3b 0a 0a 20 20 69 66 28 20 70 44 62 20 29 7b 0a  ;..  if( pDb ){.
2a050 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
2a051 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b  Close(pDb->pBt);
2a052 0a 20 20 20 20 70 44 62 2d 3e 70 42 74 20 3d 20  .    pDb->pBt = 
2a053 30 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68  0;.    pDb->pSch
2a054 65 6d 61 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  ema = 0;.  }..  
2a055 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
2a056 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30  rnalSchema(db, 0
2a057 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
2a058 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51  .}.#endif  /* SQ
2a059 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
2a05a 20 26 26 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   && SQLITE_OMIT_
2a05b 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ATTACH */../****
2a05c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2a05d 66 20 76 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a  f vacuum.c *****
2a05e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a05f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2a061 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2a062 20 66 69 6c 65 20 76 74 61 62 2e 63 20 2a 2a 2a   file vtab.c ***
2a063 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a064 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a065 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
2a066 20 32 30 30 36 20 4a 75 6e 65 20 31 30 0a 2a 2a   2006 June 10.**
2a067 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2a068 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2a069 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2a06a 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2a06b 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2a06c 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2a06d 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2a06e 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2a06f 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2a070 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2a071 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2a072 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2a073 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2a074 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2a075 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2a076 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2a077 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2a078 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2a079 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a07a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a07b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a07c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
2a07d 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2a07e 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
2a07f 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 76  help implement v
2a080 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a  irtual tables..*
2a081 2a 0a 2a 2a 20 24 49 64 3a 20 76 74 61 62 2e 63  *.** $Id: vtab.c
2a082 2c 76 20 31 2e 38 34 20 32 30 30 39 2f 30 33 2f  ,v 1.84 2009/03/
2a083 32 34 20 31 35 3a 30 38 3a 31 30 20 64 72 68 20  24 15:08:10 drh 
2a084 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
2a085 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2a086 54 55 41 4c 54 41 42 4c 45 0a 0a 73 74 61 74 69  TUALTABLE..stati
2a087 63 20 69 6e 74 20 63 72 65 61 74 65 4d 6f 64 75  c int createModu
2a088 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  le(.  sqlite3 *d
2a089 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
2a08a 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2a08b 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c  e in which modul
2a08c 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20  e is registered 
2a08d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2a08e 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
2a08f 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73       /* Name ass
2a090 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f  igned to this mo
2a091 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  dule */.  const 
2a092 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
2a093 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65  pModule,  /* The
2a094 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74   definition of t
2a095 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76  he module */.  v
2a096 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20  oid *pAux,      
2a097 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a098 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  * Context pointe
2a099 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43  r for xCreate/xC
2a09a 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64  onnect */.  void
2a09b 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69   (*xDestroy)(voi
2a09c 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a 20 4d  d *)        /* M
2a09d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72  odule destructor
2a09e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 20 7b   function */.) {
2a09f 0a 20 20 69 6e 74 20 72 63 2c 20 6e 4e 61 6d 65  .  int rc, nName
2a0a0 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  ;.  Module *pMod
2a0a1 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
2a0a2 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2a0a3 65 78 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73  ex);.  nName = s
2a0a4 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2a0a5 4e 61 6d 65 29 3b 0a 20 20 70 4d 6f 64 20 3d 20  Name);.  pMod = 
2a0a6 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  (Module *)sqlite
2a0a7 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2a0a8 20 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65 29 20   sizeof(Module) 
2a0a9 2b 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0a 20 20  + nName + 1);.  
2a0aa 69 66 28 20 70 4d 6f 64 20 29 7b 0a 20 20 20 20  if( pMod ){.    
2a0ab 4d 6f 64 75 6c 65 20 2a 70 44 65 6c 3b 0a 20 20  Module *pDel;.  
2a0ac 20 20 63 68 61 72 20 2a 7a 43 6f 70 79 20 3d 20    char *zCopy = 
2a0ad 28 63 68 61 72 20 2a 29 28 26 70 4d 6f 64 5b 31  (char *)(&pMod[1
2a0ae 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a  ]);.    memcpy(z
2a0af 43 6f 70 79 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Copy, zName, nNa
2a0b0 6d 65 2b 31 29 3b 0a 20 20 20 20 70 4d 6f 64 2d  me+1);.    pMod-
2a0b1 3e 7a 4e 61 6d 65 20 3d 20 7a 43 6f 70 79 3b 0a  >zName = zCopy;.
2a0b2 20 20 20 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c      pMod->pModul
2a0b3 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a 20 20 20  e = pModule;.   
2a0b4 20 70 4d 6f 64 2d 3e 70 41 75 78 20 3d 20 70 41   pMod->pAux = pA
2a0b5 75 78 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 78 44  ux;.    pMod->xD
2a0b6 65 73 74 72 6f 79 20 3d 20 78 44 65 73 74 72 6f  estroy = xDestro
2a0b7 79 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 28 4d  y;.    pDel = (M
2a0b8 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 48  odule *)sqlite3H
2a0b9 61 73 68 49 6e 73 65 72 74 28 26 64 62 2d 3e 61  ashInsert(&db->a
2a0ba 4d 6f 64 75 6c 65 2c 20 7a 43 6f 70 79 2c 20 6e  Module, zCopy, n
2a0bb 4e 61 6d 65 2c 20 28 76 6f 69 64 2a 29 70 4d 6f  Name, (void*)pMo
2a0bc 64 29 3b 0a 20 20 20 20 69 66 28 20 70 44 65 6c  d);.    if( pDel
2a0bd 20 26 26 20 70 44 65 6c 2d 3e 78 44 65 73 74 72   && pDel->xDestr
2a0be 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 44 65 6c  oy ){.      pDel
2a0bf 2d 3e 78 44 65 73 74 72 6f 79 28 70 44 65 6c 2d  ->xDestroy(pDel-
2a0c0 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20  >pAux);.    }.  
2a0c1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2a0c2 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 69  db, pDel);.    i
2a0c3 66 28 20 70 44 65 6c 3d 3d 70 4d 6f 64 20 29 7b  f( pDel==pMod ){
2a0c4 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
2a0c5 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
2a0c6 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65   }.    sqlite3Re
2a0c7 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
2a0c8 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  a(db, 0);.  }els
2a0c9 65 20 69 66 28 20 78 44 65 73 74 72 6f 79 20 29  e if( xDestroy )
2a0ca 7b 0a 20 20 20 20 78 44 65 73 74 72 6f 79 28 70  {.    xDestroy(p
2a0cb 41 75 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  Aux);.  }.  rc =
2a0cc 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
2a0cd 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a  db, SQLITE_OK);.
2a0ce 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2a0cf 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
2a0d0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2a0d1 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61  .../*.** Externa
2a0d2 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75  l API function u
2a0d3 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
2a0d4 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  new virtual-tabl
2a0d5 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c  e module..*/.SQL
2a0d6 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2a0d7 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
2a0d8 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2a0d9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2a0da 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2a0db 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65   in which module
2a0dc 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a   is registered *
2a0dd 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2a0de 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20  zName,          
2a0df 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69      /* Name assi
2a0e0 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64  gned to this mod
2a0e1 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
2a0e2 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
2a0e3 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20  Module,  /* The 
2a0e4 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68  definition of th
2a0e5 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f  e module */.  vo
2a0e6 69 64 20 2a 70 41 75 78 20 20 20 20 20 20 20 20  id *pAux        
2a0e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a0e8 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   Context pointer
2a0e9 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
2a0ea 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 72 65  nnect */.){.  re
2a0eb 74 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c  turn createModul
2a0ec 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f  e(db, zName, pMo
2a0ed 64 75 6c 65 2c 20 70 41 75 78 2c 20 30 29 3b 0a  dule, pAux, 0);.
2a0ee 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61  }../*.** Externa
2a0ef 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75  l API function u
2a0f0 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
2a0f1 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  new virtual-tabl
2a0f2 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c  e module..*/.SQL
2a0f3 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2a0f4 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
2a0f5 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  e_v2(.  sqlite3 
2a0f6 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2a0f7 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2a0f8 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64  ase in which mod
2a0f9 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65  ule is registere
2a0fa 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
2a0fb 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
2a0fc 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61         /* Name a
2a0fd 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20  ssigned to this 
2a0fe 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
2a0ff 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
2a100 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54   *pModule,  /* T
2a101 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  he definition of
2a102 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20   the module */. 
2a103 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20   void *pAux,    
2a104 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a105 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e   /* Context poin
2a106 74 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f  ter for xCreate/
2a107 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f  xConnect */.  vo
2a108 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76  id (*xDestroy)(v
2a109 6f 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a  oid *)        /*
2a10a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74   Module destruct
2a10b 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  or function */.)
2a10c 7b 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74  {.  return creat
2a10d 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d  eModule(db, zNam
2a10e 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78  e, pModule, pAux
2a10f 2c 20 78 44 65 73 74 72 6f 79 29 3b 0a 7d 0a 0a  , xDestroy);.}..
2a110 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 76  /*.** Lock the v
2a111 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 6f 20  irtual table so 
2a112 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62  that it cannot b
2a113 65 20 64 69 73 63 6f 6e 6e 65 63 74 65 64 2e 0a  e disconnected..
2a114 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73 74 2e 20 20  ** Locks nest.  
2a115 45 76 65 72 79 20 6c 6f 63 6b 20 73 68 6f 75 6c  Every lock shoul
2a116 64 20 68 61 76 65 20 61 20 63 6f 72 72 65 73 70  d have a corresp
2a117 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63 6b 2e 0a 2a  onding unlock..*
2a118 2a 20 49 66 20 61 6e 20 75 6e 6c 6f 63 6b 20 69  * If an unlock i
2a119 73 20 6f 6d 69 74 74 65 64 2c 20 72 65 73 6f 75  s omitted, resou
2a11a 72 63 65 73 20 6c 65 61 6b 73 20 77 69 6c 6c 20  rces leaks will 
2a11b 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 49  occur.  .**.** I
2a11c 66 20 61 20 64 69 73 63 6f 6e 6e 65 63 74 20 69  f a disconnect i
2a11d 73 20 61 74 74 65 6d 70 74 65 64 20 77 68 69 6c  s attempted whil
2a11e 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
2a11f 65 20 69 73 20 6c 6f 63 6b 65 64 2c 0a 2a 2a 20  e is locked,.** 
2a120 74 68 65 20 64 69 73 63 6f 6e 6e 65 63 74 20 69  the disconnect i
2a121 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
2a122 20 61 6c 6c 20 6c 6f 63 6b 73 20 68 61 76 65 20   all locks have 
2a123 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f  been removed..*/
2a124 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2a125 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
2a126 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61  Lock(sqlite3_vta
2a127 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 70 56 74  b *pVtab){.  pVt
2a128 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f  ab->nRef++;.}../
2a129 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 61 20 76 69  *.** Unlock a vi
2a12a 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 57 68  rtual table.  Wh
2a12b 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b  en the last lock
2a12c 20 69 73 20 72 65 6d 6f 76 65 64 2c 0a 2a 2a 20   is removed,.** 
2a12d 64 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 20 76  disconnect the v
2a12e 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  irtual table..*/
2a12f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2a130 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
2a131 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 20 2a  Unlock(sqlite3 *
2a132 64 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62  db, sqlite3_vtab
2a133 20 2a 70 56 74 61 62 29 7b 0a 20 20 70 56 74 61   *pVtab){.  pVta
2a134 62 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73  b->nRef--;.  ass
2a135 65 72 74 28 64 62 29 3b 0a 20 20 61 73 73 65 72  ert(db);.  asser
2a136 74 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  t( sqlite3Safety
2a137 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 3b 0a 20  CheckOk(db) );. 
2a138 20 69 66 28 20 70 56 74 61 62 2d 3e 6e 52 65 66   if( pVtab->nRef
2a139 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ==0 ){.    if( d
2a13a 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45  b->magic==SQLITE
2a13b 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20  _MAGIC_BUSY ){. 
2a13c 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
2a13d 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
2a13e 0a 20 20 20 20 20 20 70 56 74 61 62 2d 3e 70 4d  .      pVtab->pM
2a13f 6f 64 75 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65  odule->xDisconne
2a140 63 74 28 70 56 74 61 62 29 3b 0a 20 20 20 20 20  ct(pVtab);.     
2a141 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2a142 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20  fetyOn(db);.    
2a143 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70  } else {.      p
2a144 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  Vtab->pModule->x
2a145 44 69 73 63 6f 6e 6e 65 63 74 28 70 56 74 61 62  Disconnect(pVtab
2a146 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
2a147 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20  /*.** Clear any 
2a148 61 6e 64 20 61 6c 6c 20 76 69 72 74 75 61 6c 2d  and all virtual-
2a149 74 61 62 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f  table informatio
2a14a 6e 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65  n from the Table
2a14b 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 54 68 69 73   record..** This
2a14c 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2a14d 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ed, for example,
2a14e 20 6a 75 73 74 20 62 65 66 6f 72 65 20 64 65 6c   just before del
2a14f 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 0a  eting the Table.
2a150 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51  ** record..*/.SQ
2a151 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2a152 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65  d sqlite3VtabCle
2a153 61 72 28 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20  ar(Table *p){.  
2a154 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
2a155 74 61 62 20 3d 20 70 2d 3e 70 56 74 61 62 3b 0a  tab = p->pVtab;.
2a156 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
2a157 61 20 3d 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a  a = p->pSchema;.
2a158 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2a159 70 53 63 68 65 6d 61 20 3f 20 70 53 63 68 65 6d  pSchema ? pSchem
2a15a 61 2d 3e 64 62 20 3a 20 30 3b 0a 20 20 69 66 28  a->db : 0;.  if(
2a15b 20 70 56 74 61 62 20 29 7b 0a 20 20 20 20 61 73   pVtab ){.    as
2a15c 73 65 72 74 28 20 70 2d 3e 70 4d 6f 64 20 26 26  sert( p->pMod &&
2a15d 20 70 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c   p->pMod->pModul
2a15e 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  e );.    sqlite3
2a15f 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70  VtabUnlock(db, p
2a160 56 74 61 62 29 3b 0a 20 20 20 20 70 2d 3e 70 56  Vtab);.    p->pV
2a161 74 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  tab = 0;.  }.  i
2a162 66 28 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  f( p->azModuleAr
2a163 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  g ){.    int i;.
2a164 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2a165 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 69 2b  ->nModuleArg; i+
2a166 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
2a167 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
2a168 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b 0a  zModuleArg[i]);.
2a169 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2a16a 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
2a16b 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 7d  zModuleArg);.  }
2a16c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
2a16d 6e 65 77 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d  new module argum
2a16e 65 6e 74 20 74 6f 20 70 54 61 62 6c 65 2d 3e 61  ent to pTable->a
2a16f 7a 4d 6f 64 75 6c 65 41 72 67 5b 5d 2e 0a 2a 2a  zModuleArg[]..**
2a170 20 54 68 65 20 73 74 72 69 6e 67 20 69 73 20 6e   The string is n
2a171 6f 74 20 63 6f 70 69 65 64 20 2d 20 74 68 65 20  ot copied - the 
2a172 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f 72 65  pointer is store
2a173 64 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72 69 6e  d.  The.** strin
2a174 67 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  g will be freed 
2a175 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68  automatically wh
2a176 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73 0a  en the table is.
2a177 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73  ** deleted..*/.s
2a178 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 4d 6f  tatic void addMo
2a179 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 73 71 6c  duleArgument(sql
2a17a 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20  ite3 *db, Table 
2a17b 2a 70 54 61 62 6c 65 2c 20 63 68 61 72 20 2a 7a  *pTable, char *z
2a17c 41 72 67 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20  Arg){.  int i = 
2a17d 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41  pTable->nModuleA
2a17e 72 67 2b 2b 3b 0a 20 20 69 6e 74 20 6e 42 79 74  rg++;.  int nByt
2a17f 65 73 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72  es = sizeof(char
2a180 20 2a 29 2a 28 31 2b 70 54 61 62 6c 65 2d 3e 6e   *)*(1+pTable->n
2a181 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 63 68  ModuleArg);.  ch
2a182 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67  ar **azModuleArg
2a183 3b 0a 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 20  ;.  azModuleArg 
2a184 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
2a185 6f 63 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61  oc(db, pTable->a
2a186 7a 4d 6f 64 75 6c 65 41 72 67 2c 20 6e 42 79 74  zModuleArg, nByt
2a187 65 73 29 3b 0a 20 20 69 66 28 20 61 7a 4d 6f 64  es);.  if( azMod
2a188 75 6c 65 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20  uleArg==0 ){.   
2a189 20 69 6e 74 20 6a 3b 0a 20 20 20 20 66 6f 72 28   int j;.    for(
2a18a 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a  j=0; j<i; j++){.
2a18b 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2a18c 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ree(db, pTable->
2a18d 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 6a 5d 29 3b  azModuleArg[j]);
2a18e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2a18f 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 41 72  e3DbFree(db, zAr
2a190 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  g);.    sqlite3D
2a191 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65  bFree(db, pTable
2a192 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a  ->azModuleArg);.
2a193 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64      pTable->nMod
2a194 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65  uleArg = 0;.  }e
2a195 6c 73 65 7b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c  lse{.    azModul
2a196 65 41 72 67 5b 69 5d 20 3d 20 7a 41 72 67 3b 0a  eArg[i] = zArg;.
2a197 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b      azModuleArg[
2a198 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  i+1] = 0;.  }.  
2a199 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
2a19a 41 72 67 20 3d 20 61 7a 4d 6f 64 75 6c 65 41 72  Arg = azModuleAr
2a19b 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  g;.}../*.** The 
2a19c 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
2a19d 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
2a19e 74 20 66 69 72 73 74 20 73 65 65 73 20 61 20 43  t first sees a C
2a19f 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
2a1a0 42 4c 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  BLE.** statement
2a1a1 2e 20 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61  .  The module na
2a1a2 6d 65 20 68 61 73 20 62 65 65 6e 20 70 61 72 73  me has been pars
2a1a3 65 64 2c 20 62 75 74 20 74 68 65 20 6f 70 74 69  ed, but the opti
2a1a4 6f 6e 61 6c 20 6c 69 73 74 0a 2a 2a 20 6f 66 20  onal list.** of 
2a1a5 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20  parameters that 
2a1a6 66 6f 6c 6c 6f 77 20 74 68 65 20 6d 6f 64 75 6c  follow the modul
2a1a7 65 20 6e 61 6d 65 20 61 72 65 20 73 74 69 6c 6c  e name are still
2a1a8 20 70 65 6e 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c   pending..*/.SQL
2a1a9 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2a1aa 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
2a1ab 6e 50 61 72 73 65 28 0a 20 20 50 61 72 73 65 20  nParse(.  Parse 
2a1ac 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2a1ad 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2a1ae 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
2a1af 4e 61 6d 65 31 2c 20 20 20 20 20 20 20 20 2f 2a  Name1,        /*
2a1b0 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74 61 62   Name of new tab
2a1b1 6c 65 2c 20 6f 72 20 64 61 74 61 62 61 73 65 20  le, or database 
2a1b2 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  name */.  Token 
2a1b3 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 20 20  *pName2,        
2a1b4 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74  /* Name of new t
2a1b5 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  able or NULL */.
2a1b6 20 20 54 6f 6b 65 6e 20 2a 70 4d 6f 64 75 6c 65    Token *pModule
2a1b7 4e 61 6d 65 20 20 20 20 2f 2a 20 4e 61 6d 65 20  Name    /* Name 
2a1b8 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 66 6f  of the module fo
2a1b9 72 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  r the virtual ta
2a1ba 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ble */.){.  int 
2a1bb 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
2a1bc 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2a1bd 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 62  e the table is b
2a1be 65 69 6e 67 20 63 72 65 61 74 65 64 20 69 6e 20  eing created in 
2a1bf 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2a1c0 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  le;        /* Th
2a1c1 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61  e new virtual ta
2a1c2 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ble */.  sqlite3
2a1c3 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f   *db;          /
2a1c4 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2a1c5 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20  ction */..  if( 
2a1c6 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67  pParse->db->flag
2a1c7 73 20 26 20 53 51 4c 49 54 45 5f 53 68 61 72 65  s & SQLITE_Share
2a1c8 64 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71  dCache ){.    sq
2a1c9 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2a1ca 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 75 73  arse, "Cannot us
2a1cb 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  e virtual tables
2a1cc 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63 68 65   in shared-cache
2a1cd 20 6d 6f 64 65 22 29 3b 0a 20 20 20 20 72 65 74   mode");.    ret
2a1ce 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  urn;.  }..  sqli
2a1cf 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70 50  te3StartTable(pP
2a1d0 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
2a1d1 61 6d 65 32 2c 20 30 2c 20 30 2c 20 31 2c 20 30  ame2, 0, 0, 1, 0
2a1d2 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 70 50  );.  pTable = pP
2a1d3 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
2a1d4 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
2a1d5 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
2a1d6 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
2a1d7 65 72 74 28 20 30 3d 3d 70 54 61 62 6c 65 2d 3e  ert( 0==pTable->
2a1d8 70 49 6e 64 65 78 20 29 3b 0a 0a 20 20 64 62 20  pIndex );..  db 
2a1d9 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2a1da 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
2a1db 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
2a1dc 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 29 3b  Table->pSchema);
2a1dd 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
2a1de 30 20 29 3b 0a 0a 20 20 70 54 61 62 6c 65 2d 3e  0 );..  pTable->
2a1df 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 56  tabFlags |= TF_V
2a1e0 69 72 74 75 61 6c 3b 0a 20 20 70 54 61 62 6c 65  irtual;.  pTable
2a1e1 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20 30  ->nModuleArg = 0
2a1e2 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67  ;.  addModuleArg
2a1e3 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c 65  ument(db, pTable
2a1e4 2c 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  , sqlite3NameFro
2a1e5 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4d 6f 64 75  mToken(db, pModu
2a1e6 6c 65 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d  leName));.  addM
2a1e7 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62  oduleArgument(db
2a1e8 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74 65  , pTable, sqlite
2a1e9 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 64 62  3DbStrDup(db, db
2a1ea 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2a1eb 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41  ));.  addModuleA
2a1ec 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62  rgument(db, pTab
2a1ed 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72  le, sqlite3DbStr
2a1ee 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  Dup(db, pTable->
2a1ef 7a 4e 61 6d 65 29 29 3b 0a 20 20 70 50 61 72 73  zName));.  pPars
2a1f0 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20  e->sNameToken.n 
2a1f1 3d 20 28 69 6e 74 29 28 26 70 4d 6f 64 75 6c 65  = (int)(&pModule
2a1f2 4e 61 6d 65 2d 3e 7a 5b 70 4d 6f 64 75 6c 65 4e  Name->z[pModuleN
2a1f3 61 6d 65 2d 3e 6e 5d 20 2d 20 70 4e 61 6d 65 31  ame->n] - pName1
2a1f4 2d 3e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ->z);..#ifndef S
2a1f5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2a1f6 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 43 72  RIZATION.  /* Cr
2a1f7 65 61 74 69 6e 67 20 61 20 76 69 72 74 75 61 6c  eating a virtual
2a1f8 20 74 61 62 6c 65 20 69 6e 76 6f 6b 65 73 20 74   table invokes t
2a1f9 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  he authorization
2a1fa 20 63 61 6c 6c 62 61 63 6b 20 74 77 69 63 65 2e   callback twice.
2a1fb 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
2a1fc 69 6e 76 6f 63 61 74 69 6f 6e 2c 20 74 6f 20 6f  invocation, to o
2a1fd 62 74 61 69 6e 20 70 65 72 6d 69 73 73 69 6f 6e  btain permission
2a1fe 20 74 6f 20 49 4e 53 45 52 54 20 61 20 72 6f 77   to INSERT a row
2a1ff 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73   into the.  ** s
2a200 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
2a201 6c 65 2c 20 68 61 73 20 61 6c 72 65 61 64 79 20  le, has already 
2a202 62 65 65 6e 20 6d 61 64 65 20 62 79 20 73 71 6c  been made by sql
2a203 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 29  ite3StartTable()
2a204 2e 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e  ..  ** The secon
2a205 64 20 63 61 6c 6c 2c 20 74 6f 20 6f 62 74 61 69  d call, to obtai
2a206 6e 20 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20  n permission to 
2a207 63 72 65 61 74 65 20 74 68 65 20 74 61 62 6c 65  create the table
2a208 2c 20 69 73 20 6d 61 64 65 20 6e 6f 77 2e 0a 20  , is made now.. 
2a209 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65   */.  if( pTable
2a20a 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 29 7b  ->azModuleArg ){
2a20b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68  .    sqlite3Auth
2a20c 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
2a20d 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 54 41 42  LITE_CREATE_VTAB
2a20e 4c 45 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d  LE, pTable->zNam
2a20f 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
2a210 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
2a211 41 72 67 5b 30 5d 2c 20 70 50 61 72 73 65 2d 3e  Arg[0], pParse->
2a212 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2a213 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  me);.  }.#endif.
2a214 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2a215 75 74 69 6e 65 20 74 61 6b 65 73 20 74 68 65 20  utine takes the 
2a216 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20  module argument 
2a217 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 61 63  that has been ac
2a218 63 75 6d 75 6c 61 74 69 6e 67 0a 2a 2a 20 69 6e  cumulating.** in
2a219 20 70 50 61 72 73 65 2d 3e 7a 41 72 67 5b 5d 20   pParse->zArg[] 
2a21a 61 6e 64 20 61 70 70 65 6e 64 73 20 69 74 20 74  and appends it t
2a21b 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 72  o the list of ar
2a21c 67 75 6d 65 6e 74 73 20 6f 6e 20 74 68 65 0a 2a  guments on the.*
2a21d 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * virtual table 
2a21e 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20  currently under 
2a21f 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20  construction in 
2a220 70 50 61 72 73 65 2d 3e 70 54 61 62 6c 65 2e 0a  pParse->pTable..
2a221 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
2a222 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62  ddArgumentToVtab
2a223 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
2a224 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 73  .  if( pParse->s
2a225 41 72 67 2e 7a 20 26 26 20 70 50 61 72 73 65 2d  Arg.z && pParse-
2a226 3e 70 4e 65 77 54 61 62 6c 65 20 29 7b 0a 20 20  >pNewTable ){.  
2a227 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
2a228 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  = (const char*)p
2a229 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 3b 0a 20  Parse->sArg.z;. 
2a22a 20 20 20 69 6e 74 20 6e 20 3d 20 70 50 61 72 73     int n = pPars
2a22b 65 2d 3e 73 41 72 67 2e 6e 3b 0a 20 20 20 20 73  e->sArg.n;.    s
2a22c 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2a22d 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 61 64 64  rse->db;.    add
2a22e 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64  ModuleArgument(d
2a22f 62 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  b, pParse->pNewT
2a230 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53  able, sqlite3DbS
2a231 74 72 4e 44 75 70 28 64 62 2c 20 7a 2c 20 6e 29  trNDup(db, z, n)
2a232 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2a233 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
2a234 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 66   this routine af
2a235 74 65 72 20 74 68 65 20 43 52 45 41 54 45 20 56  ter the CREATE V
2a236 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
2a237 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65  tement.** has be
2a238 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 20 70 61  en completely pa
2a239 72 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rsed..*/.SQLITE_
2a23a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2a23b 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61  ite3VtabFinishPa
2a23c 72 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  rse(Parse *pPars
2a23d 65 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b  e, Token *pEnd){
2a23e 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
2a23f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2a240 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72  ble being constr
2a241 75 63 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  ucted */.  sqlit
2a242 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f  e3 *db;        /
2a243 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
2a244 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
2a245 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 3b 20 20 20  har *zModule;   
2a246 20 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65     /* The module
2a247 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
2a248 6c 65 3a 20 55 53 49 4e 47 20 6d 6f 64 75 6c 65  le: USING module
2a249 6e 61 6d 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65  name */.  Module
2a24a 20 2a 70 4d 6f 64 20 3d 20 30 3b 0a 0a 20 20 61   *pMod = 0;..  a
2a24b 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62  ddArgumentToVtab
2a24c 28 70 50 61 72 73 65 29 3b 0a 20 20 70 50 61 72  (pParse);.  pPar
2a24d 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a  se->sArg.z = 0;.
2a24e 0a 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 74 68 65  .  /* Lookup the
2a24f 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 2e 20 2a 2f   module name. */
2a250 0a 20 20 70 54 61 62 20 3d 20 70 50 61 72 73 65  .  pTab = pParse
2a251 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69  ->pNewTable;.  i
2a252 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65 74  f( pTab==0 ) ret
2a253 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72  urn;.  db = pPar
2a254 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 54  se->db;.  if( pT
2a255 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3c 31  ab->nModuleArg<1
2a256 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 4d 6f   ) return;.  zMo
2a257 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d  dule = pTab->azM
2a258 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 70  oduleArg[0];.  p
2a259 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 2a 29 73  Mod = (Module*)s
2a25a 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
2a25b 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4d 6f  db->aModule, zMo
2a25c 64 75 6c 65 2c 0a 20 20 20 20 20 20 20 20 20 20  dule,.          
2a25d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a25e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
2a25f 74 72 6c 65 6e 33 30 28 7a 4d 6f 64 75 6c 65 29  trlen30(zModule)
2a260 29 3b 0a 20 20 70 54 61 62 2d 3e 70 4d 6f 64 20  );.  pTab->pMod 
2a261 3d 20 70 4d 6f 64 3b 0a 20 20 0a 20 20 2f 2a 20  = pMod;.  .  /* 
2a262 49 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49  If the CREATE VI
2a263 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
2a264 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 65  ement is being e
2a265 6e 74 65 72 65 64 20 66 6f 72 20 74 68 65 0a 20  ntered for the. 
2a266 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 28   ** first time (
2a267 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69  in other words i
2a268 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2a269 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20  ble is actually 
2a26a 62 65 69 6e 67 0a 20 20 2a 2a 20 63 72 65 61 74  being.  ** creat
2a26b 65 64 20 6e 6f 77 20 69 6e 73 74 65 61 64 20 6f  ed now instead o
2a26c 66 20 6a 75 73 74 20 62 65 69 6e 67 20 72 65 61  f just being rea
2a26d 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 5f  d out of sqlite_
2a26e 6d 61 73 74 65 72 29 20 74 68 65 6e 0a 20 20 2a  master) then.  *
2a26f 2a 20 64 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20  * do additional 
2a270 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 77  initialization w
2a271 6f 72 6b 20 61 6e 64 20 73 74 6f 72 65 20 74 68  ork and store th
2a272 65 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74  e statement text
2a273 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c  .  ** in the sql
2a274 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2a275 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  ..  */.  if( !db
2a276 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2a277 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a     char *zStmt;.
2a278 20 20 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65      char *zWhere
2a279 3b 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20  ;.    int iDb;. 
2a27a 20 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 20     Vdbe *v;..   
2a27b 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
2a27c 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
2a27d 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54   the CREATE VIRT
2a27e 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d  UAL TABLE statem
2a27f 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ent */.    if( p
2a280 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 50 61  End ){.      pPa
2a281 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
2a282 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e  n = (int)(pEnd->
2a283 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  z - pParse->sNam
2a284 65 54 6f 6b 65 6e 2e 7a 29 20 2b 20 70 45 6e 64  eToken.z) + pEnd
2a285 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  ->n;.    }.    z
2a286 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50  Stmt = sqlite3MP
2a287 72 69 6e 74 66 28 64 62 2c 20 22 43 52 45 41 54  rintf(db, "CREAT
2a288 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  E VIRTUAL TABLE 
2a289 25 54 22 2c 20 26 70 50 61 72 73 65 2d 3e 73 4e  %T", &pParse->sN
2a28a 61 6d 65 54 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20  ameToken);..    
2a28b 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 68  /* A slot for th
2a28c 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c 72  e record has alr
2a28d 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
2a28e 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ted in the .    
2a28f 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  ** SQLITE_MASTER
2a290 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73 74   table.  We just
2a291 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65 20   need to update 
2a292 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20 61  that slot with a
2a293 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e  ll.    ** the in
2a294 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76 65 20  formation we've 
2a295 63 6f 6c 6c 65 63 74 65 64 2e 20 20 0a 20 20 20  collected.  .   
2a296 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 56   **.    ** The V
2a297 4d 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  M register numbe
2a298 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77  r pParse->regRow
2a299 69 64 20 68 6f 6c 64 73 20 74 68 65 20 72 6f 77  id holds the row
2a29a 69 64 20 6f 66 20 61 6e 0a 20 20 20 20 2a 2a 20  id of an.    ** 
2a29b 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73 71 6c  entry in the sql
2a29c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2a29d 20 74 68 74 20 77 61 73 20 63 72 65 61 74 65 64   tht was created
2a29e 20 66 6f 72 20 74 68 69 73 20 76 74 61 62 0a 20   for this vtab. 
2a29f 20 20 20 2a 2a 20 62 79 20 73 71 6c 69 74 65 33     ** by sqlite3
2a2a0 53 74 61 72 74 54 61 62 6c 65 28 29 2e 0a 20 20  StartTable()..  
2a2a1 20 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73    */.    iDb = s
2a2a2 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2a2a3 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
2a2a4 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69  chema);.    sqli
2a2a5 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
2a2a6 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50  Parse,.      "UP
2a2a7 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20 20 20  DATE %Q.%s ".   
2a2a8 20 20 20 20 20 20 22 53 45 54 20 74 79 70 65 3d        "SET type=
2a2a9 27 74 61 62 6c 65 27 2c 20 6e 61 6d 65 3d 25 51  'table', name=%Q
2a2aa 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72  , tbl_name=%Q, r
2a2ab 6f 6f 74 70 61 67 65 3d 30 2c 20 73 71 6c 3d 25  ootpage=0, sql=%
2a2ac 51 20 22 0a 20 20 20 20 20 20 20 22 57 48 45 52  Q ".       "WHER
2a2ad 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20  E rowid=#%d",.  
2a2ae 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d      db->aDb[iDb]
2a2af 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54  .zName, SCHEMA_T
2a2b0 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20  ABLE(iDb),.     
2a2b1 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20   pTab->zName,.  
2a2b2 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
2a2b3 0a 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20  .      zStmt,.  
2a2b4 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52      pParse->regR
2a2b5 6f 77 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20  owid.    );.    
2a2b6 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2a2b7 2c 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 76 20  , zStmt);.    v 
2a2b8 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2a2b9 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71  (pParse);.    sq
2a2ba 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
2a2bb 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
2a2bc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a2bd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70  AddOp2(v, OP_Exp
2a2be 69 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ire, 0, 0);.    
2a2bf 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  zWhere = sqlite3
2a2c0 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d  MPrintf(db, "nam
2a2c1 65 3d 27 25 71 27 22 2c 20 70 54 61 62 2d 3e 7a  e='%q'", pTab->z
2a2c2 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Name);.    sqlit
2a2c3 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2a2c4 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20  OP_ParseSchema, 
2a2c5 69 44 62 2c 20 31 2c 20 30 2c 20 7a 57 68 65 72  iDb, 1, 0, zWher
2a2c6 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a  e, P4_DYNAMIC);.
2a2c7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a2c8 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 43 72 65  ddOp4(v, OP_VCre
2a2c9 61 74 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  ate, iDb, 0, 0, 
2a2ca 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a2cb 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e            pTab->
2a2cc 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74  zName, sqlite3St
2a2cd 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e 61  rlen30(pTab->zNa
2a2ce 6d 65 29 20 2b 20 31 29 3b 0a 20 20 7d 0a 0a 20  me) + 1);.  }.. 
2a2cf 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 72 65   /* If we are re
2a2d0 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69  reading the sqli
2a2d1 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
2a2d2 63 72 65 61 74 65 20 74 68 65 20 69 6e 2d 6d 65  create the in-me
2a2d3 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 63 6f 72 64  mory.  ** record
2a2d4 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 49   of the table. I
2a2d5 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73  f the module has
2a2d6 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65   already been re
2a2d7 67 69 73 74 65 72 65 64 2c 0a 20 20 2a 2a 20 61  gistered,.  ** a
2a2d8 6c 73 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 6f  lso call the xCo
2a2d9 6e 6e 65 63 74 20 6d 65 74 68 6f 64 20 68 65 72  nnect method her
2a2da 65 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 7b  e..  */.  else {
2a2db 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64  .    Table *pOld
2a2dc 3b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53  ;.    Schema *pS
2a2dd 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53  chema = pTab->pS
2a2de 63 68 65 6d 61 3b 0a 20 20 20 20 63 6f 6e 73 74  chema;.    const
2a2df 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70   char *zName = p
2a2e0 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
2a2e1 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  int nName = sqli
2a2e2 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
2a2e3 65 29 20 2b 20 31 3b 0a 20 20 20 20 70 4f 6c 64  e) + 1;.    pOld
2a2e4 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e   = sqlite3HashIn
2a2e5 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74  sert(&pSchema->t
2a2e6 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  blHash, zName, n
2a2e7 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  Name, pTab);.   
2a2e8 20 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20   if( pOld ){.   
2a2e9 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2a2ea 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 61  led = 1;.      a
2a2eb 73 73 65 72 74 28 20 70 54 61 62 3d 3d 70 4f 6c  ssert( pTab==pOl
2a2ec 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20  d );  /* Malloc 
2a2ed 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64  must have failed
2a2ee 20 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65   inside HashInse
2a2ef 72 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 72 65  rt() */.      re
2a2f0 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
2a2f1 70 53 63 68 65 6d 61 2d 3e 64 62 20 3d 20 70 50  pSchema->db = pP
2a2f2 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 50  arse->db;.    pP
2a2f3 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
2a2f4 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
2a2f5 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
2a2f6 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2a2f7 77 68 65 6e 20 69 74 20 73 65 65 73 20 74 68 65  when it sees the
2a2f8 20 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20   first token.** 
2a2f9 6f 66 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74  of an argument t
2a2fa 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  o the module nam
2a2fb 65 20 69 6e 20 61 20 43 52 45 41 54 45 20 56 49  e in a CREATE VI
2a2fc 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
2a2fd 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ement..*/.SQLITE
2a2fe 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2a2ff 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69 74  lite3VtabArgInit
2a300 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
2a301 0a 20 20 61 64 64 41 72 67 75 6d 65 6e 74 54 6f  .  addArgumentTo
2a302 56 74 61 62 28 70 50 61 72 73 65 29 3b 0a 20 20  Vtab(pParse);.  
2a303 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 3d  pParse->sArg.z =
2a304 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41   0;.  pParse->sA
2a305 72 67 2e 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  rg.n = 0;.}../*.
2a306 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
2a307 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
2a308 20 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20   for each token 
2a309 61 66 74 65 72 20 74 68 65 20 66 69 72 73 74 20  after the first 
2a30a 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 61 6e 20 61  token.** in an a
2a30b 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d  rgument to the m
2a30c 6f 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20  odule name in a 
2a30d 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
2a30e 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
2a30f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2a310 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2a311 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 73  abArgExtend(Pars
2a312 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
2a313 20 2a 70 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70   *p){.  Token *p
2a314 41 72 67 20 3d 20 26 70 50 61 72 73 65 2d 3e 73  Arg = &pParse->s
2a315 41 72 67 3b 0a 20 20 69 66 28 20 70 41 72 67 2d  Arg;.  if( pArg-
2a316 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 41 72  >z==0 ){.    pAr
2a317 67 2d 3e 7a 20 3d 20 70 2d 3e 7a 3b 0a 20 20 20  g->z = p->z;.   
2a318 20 70 41 72 67 2d 3e 6e 20 3d 20 70 2d 3e 6e 3b   pArg->n = p->n;
2a319 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
2a31a 73 65 72 74 28 70 41 72 67 2d 3e 7a 20 3c 20 70  sert(pArg->z < p
2a31b 2d 3e 7a 29 3b 0a 20 20 20 20 70 41 72 67 2d 3e  ->z);.    pArg->
2a31c 6e 20 3d 20 28 69 6e 74 29 28 26 70 2d 3e 7a 5b  n = (int)(&p->z[
2a31d 70 2d 3e 6e 5d 20 2d 20 70 41 72 67 2d 3e 7a 29  p->n] - pArg->z)
2a31e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
2a31f 6e 76 6f 6b 65 20 61 20 76 69 72 74 75 61 6c 20  nvoke a virtual 
2a320 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f  table constructo
2a321 72 20 28 65 69 74 68 65 72 20 78 43 72 65 61 74  r (either xCreat
2a322 65 20 6f 72 20 78 43 6f 6e 6e 65 63 74 29 2e 20  e or xConnect). 
2a323 54 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74  The.** pointer t
2a324 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74  o the function t
2a325 6f 20 69 6e 76 6f 6b 65 20 69 73 20 70 61 73 73  o invoke is pass
2a326 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68  ed as the fourth
2a327 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f   parameter.** to
2a328 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e   this procedure.
2a329 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
2a32a 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74  tabCallConstruct
2a32b 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  or(.  sqlite3 *d
2a32c 62 2c 20 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  b, .  Table *pTa
2a32d 62 2c 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f  b,.  Module *pMo
2a32e 64 2c 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 73  d,.  int (*xCons
2a32f 74 72 75 63 74 29 28 73 71 6c 69 74 65 33 2a 2c  truct)(sqlite3*,
2a330 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
2a331 63 68 61 72 2a 63 6f 6e 73 74 2a 2c 73 71 6c 69  char*const*,sqli
2a332 74 65 33 5f 76 74 61 62 2a 2a 2c 63 68 61 72 2a  te3_vtab**,char*
2a333 2a 29 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  *),.  char **pzE
2a334 72 72 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  rr.){.  int rc;.
2a335 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 73 71 6c    int rc2;.  sql
2a336 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
2a337 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68   = 0;.  const ch
2a338 61 72 20 2a 63 6f 6e 73 74 2a 61 7a 41 72 67 20  ar *const*azArg 
2a339 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  = (const char *c
2a33a 6f 6e 73 74 2a 29 70 54 61 62 2d 3e 61 7a 4d 6f  onst*)pTab->azMo
2a33b 64 75 6c 65 41 72 67 3b 0a 20 20 69 6e 74 20 6e  duleArg;.  int n
2a33c 41 72 67 20 3d 20 70 54 61 62 2d 3e 6e 4d 6f 64  Arg = pTab->nMod
2a33d 75 6c 65 41 72 67 3b 0a 20 20 63 68 61 72 20 2a  uleArg;.  char *
2a33e 7a 45 72 72 20 3d 20 30 3b 0a 20 20 63 68 61 72  zErr = 0;.  char
2a33f 20 2a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 3d 20   *zModuleName = 
2a340 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2a341 62 2c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a  b, "%s", pTab->z
2a342 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 21 7a  Name);..  if( !z
2a343 4d 6f 64 75 6c 65 4e 61 6d 65 20 29 7b 0a 20 20  ModuleName ){.  
2a344 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2a345 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61 73  NOMEM;.  }..  as
2a346 73 65 72 74 28 20 21 64 62 2d 3e 70 56 54 61 62  sert( !db->pVTab
2a347 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78 43   );.  assert( xC
2a348 6f 6e 73 74 72 75 63 74 20 29 3b 0a 0a 20 20 64  onstruct );..  d
2a349 62 2d 3e 70 56 54 61 62 20 3d 20 70 54 61 62 3b  b->pVTab = pTab;
2a34a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53  .  rc = sqlite3S
2a34b 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
2a34c 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
2a34d 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20  TE_OK );.  rc = 
2a34e 78 43 6f 6e 73 74 72 75 63 74 28 64 62 2c 20 70  xConstruct(db, p
2a34f 4d 6f 64 2d 3e 70 41 75 78 2c 20 6e 41 72 67 2c  Mod->pAux, nArg,
2a350 20 61 7a 41 72 67 2c 20 26 70 56 74 61 62 2c 20   azArg, &pVtab, 
2a351 26 7a 45 72 72 29 3b 0a 20 20 72 63 32 20 3d 20  &zErr);.  rc2 = 
2a352 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
2a353 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  db);.  if( rc==S
2a354 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 56 74 61  QLITE_OK && pVta
2a355 62 20 29 7b 0a 20 20 20 20 70 56 74 61 62 2d 3e  b ){.    pVtab->
2a356 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d 3e  pModule = pMod->
2a357 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56 74  pModule;.    pVt
2a358 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  ab->nRef = 1;.  
2a359 20 20 70 54 61 62 2d 3e 70 56 74 61 62 20 3d 20    pTab->pVtab = 
2a35a 70 56 74 61 62 3b 0a 20 20 7d 0a 0a 20 20 69 66  pVtab;.  }..  if
2a35b 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20  ( SQLITE_OK!=rc 
2a35c 29 7b 0a 20 20 20 20 69 66 28 20 7a 45 72 72 3d  ){.    if( zErr=
2a35d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  =0 ){.      *pzE
2a35e 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  rr = sqlite3MPri
2a35f 6e 74 66 28 64 62 2c 20 22 76 74 61 62 6c 65 20  ntf(db, "vtable 
2a360 63 6f 6e 73 74 72 75 63 74 6f 72 20 66 61 69 6c  constructor fail
2a361 65 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65  ed: %s", zModule
2a362 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Name);.    }else
2a363 20 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20   {.      *pzErr 
2a364 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2a365 28 64 62 2c 20 22 25 73 22 2c 20 7a 45 72 72 29  (db, "%s", zErr)
2a366 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
2a367 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b  bFree(db, zErr);
2a368 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
2a369 66 28 20 64 62 2d 3e 70 56 54 61 62 20 29 7b 0a  f( db->pVTab ){.
2a36a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2a36b 7a 46 6f 72 6d 61 74 20 3d 20 22 76 74 61 62 6c  zFormat = "vtabl
2a36c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 64 69  e constructor di
2a36d 64 20 6e 6f 74 20 64 65 63 6c 61 72 65 20 73 63  d not declare sc
2a36e 68 65 6d 61 3a 20 25 73 22 3b 0a 20 20 20 20 2a  hema: %s";.    *
2a36f 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d  pzErr = sqlite3M
2a370 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d  Printf(db, zForm
2a371 61 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  at, pTab->zName)
2a372 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
2a373 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 20 0a 20 20  E_ERROR;.  } .  
2a374 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2a375 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 63  K ){.    rc = rc
2a376 32 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 56 54  2;.  }.  db->pVT
2a377 61 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  ab = 0;.  sqlite
2a378 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 6f 64  3DbFree(db, zMod
2a379 75 6c 65 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20  uleName);..  /* 
2a37a 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 65  If everything we
2a37b 6e 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  nt according to 
2a37c 70 6c 61 6e 2c 20 6c 6f 6f 70 20 74 68 72 6f 75  plan, loop throu
2a37d 67 68 20 74 68 65 20 63 6f 6c 75 6d 6e 73 0a 20  gh the columns. 
2a37e 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65   ** of the table
2a37f 20 74 6f 20 73 65 65 20 69 66 20 61 6e 79 20 6f   to see if any o
2a380 66 20 74 68 65 6d 20 63 6f 6e 74 61 69 6e 20 74  f them contain t
2a381 68 65 20 74 6f 6b 65 6e 20 22 68 69 64 64 65 6e  he token "hidden
2a382 22 2e 0a 20 20 2a 2a 20 49 66 20 73 6f 2c 20 73  "..  ** If so, s
2a383 65 74 20 74 68 65 20 43 6f 6c 75 6d 6e 2e 69 73  et the Column.is
2a384 48 69 64 64 65 6e 20 66 6c 61 67 20 61 6e 64 20  Hidden flag and 
2a385 72 65 6d 6f 76 65 20 74 68 65 20 74 6f 6b 65 6e  remove the token
2a386 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 74   from.  ** the t
2a387 79 70 65 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f  ype string..  */
2a388 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2a389 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20  E_OK ){.    int 
2a38a 69 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69 43  iCol;.    for(iC
2a38b 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d  ol=0; iCol<pTab-
2a38c 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a  >nCol; iCol++){.
2a38d 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70        char *zTyp
2a38e 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  e = pTab->aCol[i
2a38f 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20  Col].zType;.    
2a390 20 20 69 6e 74 20 6e 54 79 70 65 3b 0a 20 20 20    int nType;.   
2a391 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20     int i = 0;.  
2a392 20 20 20 20 69 66 28 20 21 7a 54 79 70 65 20 29      if( !zType )
2a393 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2a394 20 6e 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33   nType = sqlite3
2a395 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29 3b  Strlen30(zType);
2a396 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
2a397 65 33 53 74 72 4e 49 43 6d 70 28 22 68 69 64 64  e3StrNICmp("hidd
2a398 65 6e 22 2c 20 7a 54 79 70 65 2c 20 36 29 20 7c  en", zType, 6) |
2a399 7c 20 28 7a 54 79 70 65 5b 36 5d 20 26 26 20 7a  | (zType[6] && z
2a39a 54 79 70 65 5b 36 5d 21 3d 27 20 27 29 20 29 7b  Type[6]!=' ') ){
2a39b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
2a39c 3b 20 69 3c 6e 54 79 70 65 3b 20 69 2b 2b 29 7b  ; i<nType; i++){
2a39d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28  .          if( (
2a39e 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43  0==sqlite3StrNIC
2a39f 6d 70 28 22 20 68 69 64 64 65 6e 22 2c 20 26 7a  mp(" hidden", &z
2a3a0 54 79 70 65 5b 69 5d 2c 20 37 29 29 0a 20 20 20  Type[i], 7)).   
2a3a1 20 20 20 20 20 20 20 20 26 26 20 28 7a 54 79 70          && (zTyp
2a3a2 65 5b 69 2b 37 5d 3d 3d 27 5c 30 27 20 7c 7c 20  e[i+7]=='\0' || 
2a3a3 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 20 27 29  zType[i+7]==' ')
2a3a4 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20  .          ){.  
2a3a5 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20            i++;. 
2a3a6 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2a3a7 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2a3a8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2a3a9 20 20 20 20 20 20 69 66 28 20 69 3c 6e 54 79 70        if( i<nTyp
2a3aa 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  e ){.        int
2a3ab 20 6a 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   j;.        int 
2a3ac 6e 44 65 6c 20 3d 20 36 20 2b 20 28 7a 54 79 70  nDel = 6 + (zTyp
2a3ad 65 5b 69 2b 36 5d 20 3f 20 31 20 3a 20 30 29 3b  e[i+6] ? 1 : 0);
2a3ae 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 69  .        for(j=i
2a3af 3b 20 28 6a 2b 6e 44 65 6c 29 3c 3d 6e 54 79 70  ; (j+nDel)<=nTyp
2a3b0 65 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  e; j++){.       
2a3b1 20 20 20 7a 54 79 70 65 5b 6a 5d 20 3d 20 7a 54     zType[j] = zT
2a3b2 79 70 65 5b 6a 2b 6e 44 65 6c 5d 3b 0a 20 20 20  ype[j+nDel];.   
2a3b3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
2a3b4 66 28 20 7a 54 79 70 65 5b 69 5d 3d 3d 27 5c 30  f( zType[i]=='\0
2a3b5 27 20 26 26 20 69 3e 30 20 29 7b 0a 20 20 20 20  ' && i>0 ){.    
2a3b6 20 20 20 20 20 20 61 73 73 65 72 74 28 7a 54 79        assert(zTy
2a3b7 70 65 5b 69 2d 31 5d 3d 3d 27 20 27 29 3b 0a 20  pe[i-1]==' ');. 
2a3b8 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 5b 69           zType[i
2a3b9 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  -1] = '\0';.    
2a3ba 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 54      }.        pT
2a3bb 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69  ab->aCol[iCol].i
2a3bc 73 48 69 64 64 65 6e 20 3d 20 31 3b 0a 20 20 20  sHidden = 1;.   
2a3bd 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
2a3be 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2a3bf 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2a3c0 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79  on is invoked by
2a3c1 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63   the parser to c
2a3c2 61 6c 6c 20 74 68 65 20 78 43 6f 6e 6e 65 63 74  all the xConnect
2a3c3 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20  () method.** of 
2a3c4 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2a3c5 65 20 70 54 61 62 2e 20 49 66 20 61 6e 20 65 72  e pTab. If an er
2a3c6 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 65  ror occurs, an e
2a3c7 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
2a3c8 75 72 6e 65 64 20 0a 2a 2a 20 61 6e 64 20 61 6e  urned .** and an
2a3c9 20 65 72 72 6f 72 20 6c 65 66 74 20 69 6e 20 70   error left in p
2a3ca 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  Parse..**.** Thi
2a3cb 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
2a3cc 70 20 69 66 20 74 61 62 6c 65 20 70 54 61 62 20  p if table pTab 
2a3cd 69 73 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c  is not a virtual
2a3ce 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
2a3cf 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2a3d0 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e  lite3VtabCallCon
2a3d1 6e 65 63 74 28 50 61 72 73 65 20 2a 70 50 61 72  nect(Parse *pPar
2a3d2 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  se, Table *pTab)
2a3d3 7b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  {.  Module *pMod
2a3d4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
2a3d5 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 21  ITE_OK;..  if( !
2a3d6 70 54 61 62 20 7c 7c 20 28 70 54 61 62 2d 3e 74  pTab || (pTab->t
2a3d7 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72  abFlags & TF_Vir
2a3d8 74 75 61 6c 29 3d 3d 30 20 7c 7c 20 70 54 61 62  tual)==0 || pTab
2a3d9 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20 72  ->pVtab ){.    r
2a3da 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2a3db 0a 20 20 7d 0a 0a 20 20 70 4d 6f 64 20 3d 20 70  .  }..  pMod = p
2a3dc 54 61 62 2d 3e 70 4d 6f 64 3b 0a 20 20 69 66 28  Tab->pMod;.  if(
2a3dd 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20 20 63 6f   !pMod ){.    co
2a3de 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c  nst char *zModul
2a3df 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75  e = pTab->azModu
2a3e0 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 20 20 73 71  leArg[0];.    sq
2a3e1 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2a3e2 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 6d  arse, "no such m
2a3e3 6f 64 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64  odule: %s", zMod
2a3e4 75 6c 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ule);.    rc = S
2a3e5 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2a3e6 20 65 6c 73 65 20 7b 0a 20 20 20 20 63 68 61 72   else {.    char
2a3e7 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 20 20   *zErr = 0;.    
2a3e8 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2a3e9 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 72 63  arse->db;.    rc
2a3ea 20 3d 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74   = vtabCallConst
2a3eb 72 75 63 74 6f 72 28 64 62 2c 20 70 54 61 62 2c  ructor(db, pTab,
2a3ec 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f   pMod, pMod->pMo
2a3ed 64 75 6c 65 2d 3e 78 43 6f 6e 6e 65 63 74 2c 20  dule->xConnect, 
2a3ee 26 7a 45 72 72 29 3b 0a 20 20 20 20 69 66 28 20  &zErr);.    if( 
2a3ef 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2a3f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2a3f1 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2a3f2 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  %s", zErr);.    
2a3f3 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
2a3f4 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
2a3f5 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
2a3f6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68  .}../*.** Add th
2a3f7 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2a3f8 70 56 74 61 62 20 74 6f 20 74 68 65 20 61 72 72  pVtab to the arr
2a3f9 61 79 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  ay sqlite3.aVTra
2a3fa 6e 73 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ns[]..*/.static 
2a3fb 69 6e 74 20 61 64 64 54 6f 56 54 72 61 6e 73 28  int addToVTrans(
2a3fc 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73 71 6c  sqlite3 *db, sql
2a3fd 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
2a3fe 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 41  ){.  const int A
2a3ff 52 52 41 59 5f 49 4e 43 52 20 3d 20 35 3b 0a 0a  RRAY_INCR = 5;..
2a400 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20 73 71    /* Grow the sq
2a401 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72  lite3.aVTrans ar
2a402 72 61 79 20 69 66 20 72 65 71 75 69 72 65 64 20  ray if required 
2a403 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 6e 56  */.  if( (db->nV
2a404 54 72 61 6e 73 25 41 52 52 41 59 5f 49 4e 43 52  Trans%ARRAY_INCR
2a405 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
2a406 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61  te3_vtab **aVTra
2a407 6e 73 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74  ns;.    int nByt
2a408 65 73 20 3d 20 73 69 7a 65 6f 66 28 73 71 6c 69  es = sizeof(sqli
2a409 74 65 33 5f 76 74 61 62 20 2a 29 20 2a 20 28 64  te3_vtab *) * (d
2a40a 62 2d 3e 6e 56 54 72 61 6e 73 20 2b 20 41 52 52  b->nVTrans + ARR
2a40b 41 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20 61 56  AY_INCR);.    aV
2a40c 54 72 61 6e 73 20 3d 20 73 71 6c 69 74 65 33 44  Trans = sqlite3D
2a40d 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 28 76 6f  bRealloc(db, (vo
2a40e 69 64 20 2a 29 64 62 2d 3e 61 56 54 72 61 6e 73  id *)db->aVTrans
2a40f 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69  , nBytes);.    i
2a410 66 28 20 21 61 56 54 72 61 6e 73 20 29 7b 0a 20  f( !aVTrans ){. 
2a411 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2a412 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
2a413 20 20 20 20 6d 65 6d 73 65 74 28 26 61 56 54 72      memset(&aVTr
2a414 61 6e 73 5b 64 62 2d 3e 6e 56 54 72 61 6e 73 5d  ans[db->nVTrans]
2a415 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69  , 0, sizeof(sqli
2a416 74 65 33 5f 76 74 61 62 20 2a 29 2a 41 52 52 41  te3_vtab *)*ARRA
2a417 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20 64 62 2d  Y_INCR);.    db-
2a418 3e 61 56 54 72 61 6e 73 20 3d 20 61 56 54 72 61  >aVTrans = aVTra
2a419 6e 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64  ns;.  }..  /* Ad
2a41a 64 20 70 56 74 61 62 20 74 6f 20 74 68 65 20 65  d pVtab to the e
2a41b 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 61 56  nd of sqlite3.aV
2a41c 54 72 61 6e 73 20 2a 2f 0a 20 20 64 62 2d 3e 61  Trans */.  db->a
2a41d 56 54 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72 61  VTrans[db->nVTra
2a41e 6e 73 2b 2b 5d 20 3d 20 70 56 74 61 62 3b 0a 20  ns++] = pVtab;. 
2a41f 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b   sqlite3VtabLock
2a420 28 70 56 74 61 62 29 3b 0a 20 20 72 65 74 75 72  (pVtab);.  retur
2a421 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2a422 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2a423 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62  ion is invoked b
2a424 79 20 74 68 65 20 76 64 62 65 20 74 6f 20 63 61  y the vdbe to ca
2a425 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65 20 6d  ll the xCreate m
2a426 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20  ethod.** of the 
2a427 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61  virtual table na
2a428 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61  med zTab in data
2a429 62 61 73 65 20 69 44 62 2e 20 0a 2a 2a 0a 2a 2a  base iDb. .**.**
2a42a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
2a42b 75 72 73 2c 20 2a 70 7a 45 72 72 20 69 73 20 73  urs, *pzErr is s
2a42c 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 6e 20 61  et to point an a
2a42d 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61  n English langua
2a42e 67 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 69 6f  ge.** descriptio
2a42f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 61  n of the error a
2a430 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 58 58 58  nd an SQLITE_XXX
2a431 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
2a432 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 6e 20 74  eturned..** In t
2a433 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c  his case the cal
2a434 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71  ler must call sq
2a435 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2a436 29 20 6f 6e 20 2a 70 7a 45 72 72 2e 0a 2a 2f 0a  ) on *pzErr..*/.
2a437 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a438 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
2a439 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  llCreate(sqlite3
2a43a 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63   *db, int iDb, c
2a43b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c  onst char *zTab,
2a43c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b 0a   char **pzErr){.
2a43d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
2a43e 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70  E_OK;.  Table *p
2a43f 54 61 62 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70  Tab;.  Module *p
2a440 4d 6f 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  Mod;.  const cha
2a441 72 20 2a 7a 4d 6f 64 75 6c 65 3b 0a 0a 20 20 70  r *zModule;..  p
2a442 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Tab = sqlite3Fin
2a443 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c  dTable(db, zTab,
2a444 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2a445 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28 70  ame);.  assert(p
2a446 54 61 62 20 26 26 20 28 70 54 61 62 2d 3e 74 61  Tab && (pTab->ta
2a447 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
2a448 75 61 6c 29 21 3d 30 20 26 26 20 21 70 54 61 62  ual)!=0 && !pTab
2a449 2d 3e 70 56 74 61 62 29 3b 0a 20 20 70 4d 6f 64  ->pVtab);.  pMod
2a44a 20 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 3b 0a 20   = pTab->pMod;. 
2a44b 20 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62 2d   zModule = pTab-
2a44c 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b  >azModuleArg[0];
2a44d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6d 6f  ..  /* If the mo
2a44e 64 75 6c 65 20 68 61 73 20 62 65 65 6e 20 72 65  dule has been re
2a44f 67 69 73 74 65 72 65 64 20 61 6e 64 20 69 6e 63  gistered and inc
2a450 6c 75 64 65 73 20 61 20 43 72 65 61 74 65 20 6d  ludes a Create m
2a451 65 74 68 6f 64 2c 20 0a 20 20 2a 2a 20 69 6e 76  ethod, .  ** inv
2a452 6f 6b 65 20 69 74 20 6e 6f 77 2e 20 49 66 20 74  oke it now. If t
2a453 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 6e 6f  he module has no
2a454 74 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  t been registere
2a455 64 2c 20 72 65 74 75 72 6e 20 61 6e 20 0a 20 20  d, return an .  
2a456 2a 2a 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 77  ** error. Otherw
2a457 69 73 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  ise, do nothing.
2a458 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4d 6f  .  */.  if( !pMo
2a459 64 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20  d ){.    *pzErr 
2a45a 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2a45b 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 6d 6f  (db, "no such mo
2a45c 64 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64 75  dule: %s", zModu
2a45d 6c 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  le);.    rc = SQ
2a45e 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65  LITE_ERROR;.  }e
2a45f 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 76 74  lse{.    rc = vt
2a460 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f  abCallConstructo
2a461 72 28 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64  r(db, pTab, pMod
2a462 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d  , pMod->pModule-
2a463 3e 78 43 72 65 61 74 65 2c 20 70 7a 45 72 72 29  >xCreate, pzErr)
2a464 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
2a465 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 54  =SQLITE_OK && pT
2a466 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20  ab->pVtab ){.   
2a467 20 20 20 72 63 20 3d 20 61 64 64 54 6f 56 54 72     rc = addToVTr
2a468 61 6e 73 28 64 62 2c 20 70 54 61 62 2d 3e 70 56  ans(db, pTab->pV
2a469 74 61 62 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  tab);.  }..  ret
2a46a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2a46b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2a46c 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68  s used to set th
2a46d 65 20 73 63 68 65 6d 61 20 6f 66 20 61 20 76 69  e schema of a vi
2a46e 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 49 74  rtual table.  It
2a46f 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 76 61 6c 69   is only.** vali
2a470 64 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66  d to call this f
2a471 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 20 77 69 74  unction from wit
2a472 68 69 6e 20 74 68 65 20 78 43 72 65 61 74 65 28  hin the xCreate(
2a473 29 20 6f 72 20 78 43 6f 6e 6e 65 63 74 28 29 20  ) or xConnect() 
2a474 6f 66 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20  of a.** virtual 
2a475 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f  table module..*/
2a476 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a477 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
2a478 76 74 61 62 28 73 71 6c 69 74 65 33 20 2a 64 62  vtab(sqlite3 *db
2a479 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43  , const char *zC
2a47a 72 65 61 74 65 54 61 62 6c 65 29 7b 0a 20 20 50  reateTable){.  P
2a47b 61 72 73 65 20 73 50 61 72 73 65 3b 0a 0a 20 20  arse sParse;..  
2a47c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2a47d 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  OK;.  Table *pTa
2a47e 62 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20  b;.  char *zErr 
2a47f 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  = 0;..  sqlite3_
2a480 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2a481 6d 75 74 65 78 29 3b 0a 20 20 70 54 61 62 20 3d  mutex);.  pTab =
2a482 20 64 62 2d 3e 70 56 54 61 62 3b 0a 20 20 69 66   db->pVTab;.  if
2a483 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 73  ( !pTab ){.    s
2a484 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2a485 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30  SQLITE_MISUSE, 0
2a486 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
2a487 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
2a488 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72  utex);.    retur
2a489 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
2a48a 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 28 70  .  }.  assert((p
2a48b 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
2a48c 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 20 26  TF_Virtual)!=0 &
2a48d 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d 3d 30 20  & pTab->nCol==0 
2a48e 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 3d 3d 30  && pTab->aCol==0
2a48f 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 50  );..  memset(&sP
2a490 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  arse, 0, sizeof(
2a491 50 61 72 73 65 29 29 3b 0a 20 20 73 50 61 72 73  Parse));.  sPars
2a492 65 2e 64 65 63 6c 61 72 65 56 74 61 62 20 3d 20  e.declareVtab = 
2a493 31 3b 0a 20 20 73 50 61 72 73 65 2e 64 62 20 3d  1;.  sParse.db =
2a494 20 64 62 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20   db;..  if( .   
2a495 20 20 20 53 51 4c 49 54 45 5f 4f 4b 20 3d 3d 20     SQLITE_OK == 
2a496 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
2a497 28 26 73 50 61 72 73 65 2c 20 7a 43 72 65 61 74  (&sParse, zCreat
2a498 65 54 61 62 6c 65 2c 20 26 7a 45 72 72 29 20 26  eTable, &zErr) &
2a499 26 20 0a 20 20 20 20 20 20 73 50 61 72 73 65 2e  & .      sParse.
2a49a 70 4e 65 77 54 61 62 6c 65 20 26 26 20 0a 20 20  pNewTable && .  
2a49b 20 20 20 20 21 73 50 61 72 73 65 2e 70 4e 65 77      !sParse.pNew
2a49c 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20 26  Table->pSelect &
2a49d 26 20 0a 20 20 20 20 20 20 28 73 50 61 72 73 65  & .      (sParse
2a49e 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 74 61 62 46  .pNewTable->tabF
2a49f 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61  lags & TF_Virtua
2a4a0 6c 29 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20 70  l)==0.  ){.    p
2a4a1 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 73 50 61 72  Tab->aCol = sPar
2a4a2 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43  se.pNewTable->aC
2a4a3 6f 6c 3b 0a 20 20 20 20 70 54 61 62 2d 3e 6e 43  ol;.    pTab->nC
2a4a4 6f 6c 20 3d 20 73 50 61 72 73 65 2e 70 4e 65 77  ol = sParse.pNew
2a4a5 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20 20 20  Table->nCol;.   
2a4a6 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c   sParse.pNewTabl
2a4a7 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20  e->nCol = 0;.   
2a4a8 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c   sParse.pNewTabl
2a4a9 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20  e->aCol = 0;.   
2a4aa 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b 0a   db->pVTab = 0;.
2a4ab 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 73    } else {.    s
2a4ac 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2a4ad 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 7a 45  SQLITE_ERROR, zE
2a4ae 72 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rr);.    sqlite3
2a4af 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
2a4b0 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
2a4b1 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73  E_ERROR;.  }.  s
2a4b2 50 61 72 73 65 2e 64 65 63 6c 61 72 65 56 74 61  Parse.declareVta
2a4b3 62 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 73 50  b = 0;..  if( sP
2a4b4 61 72 73 65 2e 70 56 64 62 65 20 29 7b 0a 20 20  arse.pVdbe ){.  
2a4b5 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
2a4b6 61 6c 69 7a 65 28 73 50 61 72 73 65 2e 70 56 64  alize(sParse.pVd
2a4b7 62 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  be);.  }.  sqlit
2a4b8 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 73 50  e3DeleteTable(sP
2a4b9 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 29 3b  arse.pNewTable);
2a4ba 0a 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61  .  sParse.pNewTa
2a4bb 62 6c 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  ble = 0;..  asse
2a4bc 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72  rt( (rc&0xff)==r
2a4bd 63 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  c );.  rc = sqli
2a4be 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
2a4bf 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
2a4c0 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
2a4c1 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
2a4c2 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
2a4c3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76   function is inv
2a4c4 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64 62 65  oked by the vdbe
2a4c5 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 44 65   to call the xDe
2a4c6 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20  stroy method.** 
2a4c7 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
2a4c8 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62 20  able named zTab 
2a4c9 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e  in database iDb.
2a4ca 20 54 68 69 73 20 6f 63 63 75 72 73 0a 2a 2a 20   This occurs.** 
2a4cb 77 68 65 6e 20 61 20 44 52 4f 50 20 54 41 42 4c  when a DROP TABL
2a4cc 45 20 69 73 20 6d 65 6e 74 69 6f 6e 65 64 2e 0a  E is mentioned..
2a4cd 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20  **.** This call 
2a4ce 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 7a 54  is a no-op if zT
2a4cf 61 62 20 69 73 20 6e 6f 74 20 61 20 76 69 72 74  ab is not a virt
2a4d0 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ual table..*/.SQ
2a4d1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2a4d2 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c   sqlite3VtabCall
2a4d3 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 20  Destroy(sqlite3 
2a4d4 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f  *db, int iDb, co
2a4d5 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 29 0a  nst char *zTab).
2a4d6 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
2a4d7 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20  ITE_OK;.  Table 
2a4d8 2a 70 54 61 62 3b 0a 0a 20 20 70 54 61 62 20 3d  *pTab;..  pTab =
2a4d9 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
2a4da 65 28 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e  e(db, zTab, db->
2a4db 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
2a4dc 0a 20 20 61 73 73 65 72 74 28 70 54 61 62 29 3b  .  assert(pTab);
2a4dd 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 56 74  .  if( pTab->pVt
2a4de 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 28 2a  ab ){.    int (*
2a4df 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65  xDestroy)(sqlite
2a4e0 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 20 3d  3_vtab *pVTab) =
2a4e1 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f   pTab->pMod->pMo
2a4e2 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79 3b 0a  dule->xDestroy;.
2a4e3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2a4e4 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
2a4e5 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
2a4e6 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
2a4e7 69 66 28 20 78 44 65 73 74 72 6f 79 20 29 7b 0a  if( xDestroy ){.
2a4e8 20 20 20 20 20 20 72 63 20 3d 20 78 44 65 73 74        rc = xDest
2a4e9 72 6f 79 28 70 54 61 62 2d 3e 70 56 74 61 62 29  roy(pTab->pVtab)
2a4ea 3b 0a 20 20 20 20 7d 0a 20 20 20 20 28 76 6f 69  ;.    }.    (voi
2a4eb 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2a4ec 6e 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72  n(db);.    if( r
2a4ed 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2a4ee 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
2a4ef 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
2a4f0 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29 7b  ->nVTrans; i++){
2a4f1 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
2a4f2 3e 61 56 54 72 61 6e 73 5b 69 5d 3d 3d 70 54 61  >aVTrans[i]==pTa
2a4f3 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20  b->pVtab ){.    
2a4f4 20 20 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e        db->aVTran
2a4f5 73 5b 69 5d 20 3d 20 64 62 2d 3e 61 56 54 72 61  s[i] = db->aVTra
2a4f6 6e 73 5b 2d 2d 64 62 2d 3e 6e 56 54 72 61 6e 73  ns[--db->nVTrans
2a4f7 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ];.          bre
2a4f8 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
2a4f9 20 20 20 20 7d 0a 20 20 20 20 20 20 70 54 61 62      }.      pTab
2a4fa 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20 20 20  ->pVtab = 0;.   
2a4fb 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
2a4fc 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
2a4fd 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76 6f  is function invo
2a4fe 6b 65 73 20 65 69 74 68 65 72 20 74 68 65 20 78  kes either the x
2a4ff 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 78 43 6f 6d  Rollback or xCom
2a500 6d 69 74 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66  mit method.** of
2a501 20 65 61 63 68 20 6f 66 20 74 68 65 20 76 69 72   each of the vir
2a502 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74  tual tables in t
2a503 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  he sqlite3.aVTra
2a504 6e 73 20 61 72 72 61 79 2e 20 54 68 65 20 6d 65  ns array. The me
2a505 74 68 6f 64 0a 2a 2a 20 63 61 6c 6c 65 64 20 69  thod.** called i
2a506 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  s identified by 
2a507 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
2a508 65 6e 74 2c 20 22 6f 66 66 73 65 74 22 2c 20 77  ent, "offset", w
2a509 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6f  hich is.** the o
2a50a 66 66 73 65 74 20 6f 66 20 74 68 65 20 6d 65 74  ffset of the met
2a50b 68 6f 64 20 74 6f 20 63 61 6c 6c 20 69 6e 20 74  hod to call in t
2a50c 68 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  he sqlite3_modul
2a50d 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  e structure..**.
2a50e 2a 2a 20 54 68 65 20 61 72 72 61 79 20 69 73 20  ** The array is 
2a50f 63 6c 65 61 72 65 64 20 61 66 74 65 72 20 69 6e  cleared after in
2a510 76 6f 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c 62  voking the callb
2a511 61 63 6b 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63  acks. .*/.static
2a512 20 76 6f 69 64 20 63 61 6c 6c 46 69 6e 61 6c 69   void callFinali
2a513 73 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ser(sqlite3 *db,
2a514 20 69 6e 74 20 6f 66 66 73 65 74 29 7b 0a 20 20   int offset){.  
2a515 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 64 62 2d  int i;.  if( db-
2a516 3e 61 56 54 72 61 6e 73 20 29 7b 0a 20 20 20 20  >aVTrans ){.    
2a517 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
2a518 56 54 72 61 6e 73 20 26 26 20 64 62 2d 3e 61 56  VTrans && db->aV
2a519 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  Trans[i]; i++){.
2a51a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
2a51b 61 62 20 2a 70 56 74 61 62 20 3d 20 64 62 2d 3e  ab *pVtab = db->
2a51c 61 56 54 72 61 6e 73 5b 69 5d 3b 0a 20 20 20 20  aVTrans[i];.    
2a51d 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c 69 74    int (*x)(sqlit
2a51e 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20 20  e3_vtab *);.    
2a51f 20 20 78 20 3d 20 2a 28 69 6e 74 20 28 2a 2a 29    x = *(int (**)
2a520 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29  (sqlite3_vtab *)
2a521 29 28 28 63 68 61 72 20 2a 29 70 56 74 61 62 2d  )((char *)pVtab-
2a522 3e 70 4d 6f 64 75 6c 65 20 2b 20 6f 66 66 73 65  >pModule + offse
2a523 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 78 20  t);.      if( x 
2a524 29 20 78 28 70 56 74 61 62 29 3b 0a 20 20 20 20  ) x(pVtab);.    
2a525 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
2a526 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b 0a  ock(db, pVtab);.
2a527 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2a528 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e  3DbFree(db, db->
2a529 61 56 54 72 61 6e 73 29 3b 0a 20 20 20 20 64 62  aVTrans);.    db
2a52a 2d 3e 6e 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20  ->nVTrans = 0;. 
2a52b 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d     db->aVTrans =
2a52c 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
2a52d 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 53 79 6e   Invoke the xSyn
2a52e 63 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20  c method of all 
2a52f 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69  virtual tables i
2a530 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56  n the sqlite3.aV
2a531 54 72 61 6e 73 0a 2a 2a 20 61 72 72 61 79 2e 20  Trans.** array. 
2a532 52 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  Return the error
2a533 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 66 69   code for the fi
2a534 72 73 74 20 65 72 72 6f 72 20 74 68 61 74 20 6f  rst error that o
2a535 63 63 75 72 73 2c 20 6f 72 0a 2a 2a 20 53 51 4c  ccurs, or.** SQL
2a536 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 20 78 53  ITE_OK if all xS
2a537 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  ync operations a
2a538 72 65 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a  re successful..*
2a539 2a 0a 2a 2a 20 53 65 74 20 2a 70 7a 45 72 72 6d  *.** Set *pzErrm
2a53a 73 67 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61  sg to point to a
2a53b 20 62 75 66 66 65 72 20 74 68 61 74 20 73 68 6f   buffer that sho
2a53c 75 6c 64 20 62 65 20 72 65 6c 65 61 73 65 64 20  uld be released 
2a53d 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65  using .** sqlite
2a53e 33 44 62 46 72 65 65 28 29 20 63 6f 6e 74 61 69  3DbFree() contai
2a53f 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65  ning an error me
2a540 73 73 61 67 65 2c 20 69 66 20 6f 6e 65 20 69 73  ssage, if one is
2a541 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53   available..*/.S
2a542 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2a543 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e  t sqlite3VtabSyn
2a544 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  c(sqlite3 *db, c
2a545 68 61 72 20 2a 2a 70 7a 45 72 72 6d 73 67 29 7b  har **pzErrmsg){
2a546 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2a547 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
2a548 20 20 69 6e 74 20 72 63 73 61 66 65 74 79 3b 0a    int rcsafety;.
2a549 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
2a54a 2a 61 56 54 72 61 6e 73 20 3d 20 64 62 2d 3e 61  *aVTrans = db->a
2a54b 56 54 72 61 6e 73 3b 0a 0a 20 20 72 63 20 3d 20  VTrans;..  rc = 
2a54c 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
2a54d 28 64 62 29 3b 0a 20 20 64 62 2d 3e 61 56 54 72  (db);.  db->aVTr
2a54e 61 6e 73 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  ans = 0;.  for(i
2a54f 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
2a550 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 56 54 72 61  K && i<db->nVTra
2a551 6e 73 20 26 26 20 61 56 54 72 61 6e 73 5b 69 5d  ns && aVTrans[i]
2a552 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
2a553 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20  te3_vtab *pVtab 
2a554 3d 20 61 56 54 72 61 6e 73 5b 69 5d 3b 0a 20 20  = aVTrans[i];.  
2a555 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c 69 74    int (*x)(sqlit
2a556 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20 20  e3_vtab *);.    
2a557 78 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75  x = pVtab->pModu
2a558 6c 65 2d 3e 78 53 79 6e 63 3b 0a 20 20 20 20 69  le->xSync;.    i
2a559 66 28 20 78 20 29 7b 0a 20 20 20 20 20 20 72 63  f( x ){.      rc
2a55a 20 3d 20 78 28 70 56 74 61 62 29 3b 0a 20 20 20   = x(pVtab);.   
2a55b 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2a55c 28 64 62 2c 20 2a 70 7a 45 72 72 6d 73 67 29 3b  (db, *pzErrmsg);
2a55d 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 6d 73 67  .      *pzErrmsg
2a55e 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73   = pVtab->zErrMs
2a55f 67 3b 0a 20 20 20 20 20 20 70 56 74 61 62 2d 3e  g;.      pVtab->
2a560 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
2a561 20 7d 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 56 54   }.  }.  db->aVT
2a562 72 61 6e 73 20 3d 20 61 56 54 72 61 6e 73 3b 0a  rans = aVTrans;.
2a563 20 20 72 63 73 61 66 65 74 79 20 3d 20 73 71 6c    rcsafety = sql
2a564 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
2a565 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
2a566 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
2a567 20 3d 20 72 63 73 61 66 65 74 79 3b 0a 20 20 7d   = rcsafety;.  }
2a568 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2a569 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
2a56a 65 20 78 52 6f 6c 6c 62 61 63 6b 20 6d 65 74 68  e xRollback meth
2a56b 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61  od of all virtua
2a56c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  l tables in the 
2a56d 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 61 56 54 72  .** sqlite3.aVTr
2a56e 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 6e 20  ans array. Then 
2a56f 63 6c 65 61 72 20 74 68 65 20 61 72 72 61 79 20  clear the array 
2a570 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54  itself..*/.SQLIT
2a571 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2a572 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
2a573 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  k(sqlite3 *db){.
2a574 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28    callFinaliser(
2a575 64 62 2c 20 6f 66 66 73 65 74 6f 66 28 73 71 6c  db, offsetof(sql
2a576 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78 52 6f 6c  ite3_module,xRol
2a577 6c 62 61 63 6b 29 29 3b 0a 20 20 72 65 74 75 72  lback));.  retur
2a578 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2a579 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
2a57a 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68 6f 64 20   xCommit method 
2a57b 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74  of all virtual t
2a57c 61 62 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a  ables in the .**
2a57d 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73   sqlite3.aVTrans
2a57e 20 61 72 72 61 79 2e 20 54 68 65 6e 20 63 6c 65   array. Then cle
2a57f 61 72 20 74 68 65 20 61 72 72 61 79 20 69 74 73  ar the array its
2a580 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  elf..*/.SQLITE_P
2a581 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2a582 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c  e3VtabCommit(sql
2a583 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c  ite3 *db){.  cal
2a584 6c 46 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f  lFinaliser(db, o
2a585 66 66 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f  ffsetof(sqlite3_
2a586 6d 6f 64 75 6c 65 2c 78 43 6f 6d 6d 69 74 29 29  module,xCommit))
2a587 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2a588 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
2a589 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2a58a 62 6c 65 20 70 56 74 61 62 20 73 75 70 70 6f 72  ble pVtab suppor
2a58b 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ts the transacti
2a58c 6f 6e 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  on interface.** 
2a58d 28 78 42 65 67 69 6e 2f 78 52 6f 6c 6c 62 61 63  (xBegin/xRollbac
2a58e 6b 2f 78 43 6f 6d 6d 69 74 20 61 6e 64 20 6f 70  k/xCommit and op
2a58f 74 69 6f 6e 61 6c 6c 79 20 78 53 79 6e 63 29 20  tionally xSync) 
2a590 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f  and a transactio
2a591 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 75 72 72  n is.** not curr
2a592 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 69 6e 76 6f  ently open, invo
2a593 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65  ke the xBegin me
2a594 74 68 6f 64 20 6e 6f 77 2e 0a 2a 2a 0a 2a 2a 20  thod now..**.** 
2a595 49 66 20 74 68 65 20 78 42 65 67 69 6e 20 63 61  If the xBegin ca
2a596 6c 6c 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ll is successful
2a597 2c 20 70 6c 61 63 65 20 74 68 65 20 73 71 6c 69  , place the sqli
2a598 74 65 33 5f 76 74 61 62 20 70 6f 69 6e 74 65 72  te3_vtab pointer
2a599 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74  .** in the sqlit
2a59a 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79  e3.aVTrans array
2a59b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2a59c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2a59d 74 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33  tabBegin(sqlite3
2a59e 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f 76 74   *db, sqlite3_vt
2a59f 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 69 6e  ab *pVtab){.  in
2a5a0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
2a5a1 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
2a5a2 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
2a5a3 65 3b 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c  e;..  /* Special
2a5a4 20 63 61 73 65 3a 20 49 66 20 64 62 2d 3e 61 56   case: If db->aV
2a5a5 54 72 61 6e 73 20 69 73 20 4e 55 4c 4c 20 61 6e  Trans is NULL an
2a5a6 64 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20 69 73  d db->nVTrans is
2a5a7 20 67 72 65 61 74 65 72 0a 20 20 2a 2a 20 74 68   greater.  ** th
2a5a8 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  an zero, then th
2a5a9 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62  is function is b
2a5aa 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d  eing called from
2a5ab 20 77 69 74 68 69 6e 20 61 0a 20 20 2a 2a 20 76   within a.  ** v
2a5ac 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 78 53  irtual module xS
2a5ad 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 2e 20  ync() callback. 
2a5ae 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f  It is illegal to
2a5af 20 77 72 69 74 65 20 74 6f 20 0a 20 20 2a 2a 20   write to .  ** 
2a5b0 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74  virtual module t
2a5b1 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 63 61  ables in this ca
2a5b2 73 65 2c 20 73 6f 20 72 65 74 75 72 6e 20 53 51  se, so return SQ
2a5b3 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 20 20 2a  LITE_MISUSE..  *
2a5b4 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  /.  if( sqlite3V
2a5b5 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 29 7b  tabInSync(db) ){
2a5b6 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2a5b7 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 20  TE_LOCKED;.  }. 
2a5b8 20 69 66 28 20 21 70 56 74 61 62 20 29 7b 0a 20   if( !pVtab ){. 
2a5b9 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2a5ba 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20 70 4d 6f 64  _OK;.  } .  pMod
2a5bb 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f  ule = pVtab->pMo
2a5bc 64 75 6c 65 3b 0a 0a 20 20 69 66 28 20 70 4d 6f  dule;..  if( pMo
2a5bd 64 75 6c 65 2d 3e 78 42 65 67 69 6e 20 29 7b 0a  dule->xBegin ){.
2a5be 20 20 20 20 69 6e 74 20 69 3b 0a 0a 0a 20 20 20      int i;...   
2a5bf 20 2f 2a 20 49 66 20 70 56 74 61 62 20 69 73 20   /* If pVtab is 
2a5c0 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 61  already in the a
2a5c1 56 54 72 61 6e 73 20 61 72 72 61 79 2c 20 72 65  VTrans array, re
2a5c2 74 75 72 6e 20 65 61 72 6c 79 20 2a 2f 0a 20 20  turn early */.  
2a5c3 20 20 66 6f 72 28 69 3d 30 3b 20 28 69 3c 64 62    for(i=0; (i<db
2a5c4 2d 3e 6e 56 54 72 61 6e 73 29 20 26 26 20 30 21  ->nVTrans) && 0!
2a5c5 3d 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3b  =db->aVTrans[i];
2a5c6 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
2a5c7 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3d   db->aVTrans[i]=
2a5c8 3d 70 56 74 61 62 20 29 7b 0a 20 20 20 20 20 20  =pVtab ){.      
2a5c9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2a5ca 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OK;.      }.    
2a5cb 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  }..    /* Invoke
2a5cc 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65 74 68   the xBegin meth
2a5cd 6f 64 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70  od */.    rc = p
2a5ce 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 28 70  Module->xBegin(p
2a5cf 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20 72  Vtab);.    if( r
2a5d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2a5d1 20 20 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f        rc = addTo
2a5d2 56 54 72 61 6e 73 28 64 62 2c 20 70 56 74 61 62  VTrans(db, pVtab
2a5d3 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
2a5d4 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
2a5d5 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72  ** The first par
2a5d6 61 6d 65 74 65 72 20 28 70 44 65 66 29 20 69 73  ameter (pDef) is
2a5d7 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c   a function impl
2a5d8 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65  ementation.  The
2a5d9 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  .** second param
2a5da 65 74 65 72 20 28 70 45 78 70 72 29 20 69 73 20  eter (pExpr) is 
2a5db 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
2a5dc 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
2a5dd 69 6f 6e 2e 0a 2a 2a 20 49 66 20 70 45 78 70 72  ion..** If pExpr
2a5de 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   is a column in 
2a5df 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c  a virtual table,
2a5e0 20 74 68 65 6e 20 6c 65 74 20 74 68 65 20 76 69   then let the vi
2a5e1 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69  rtual.** table i
2a5e2 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61  mplementation ha
2a5e3 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74  ve an opportunit
2a5e4 79 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68  y to overload th
2a5e5 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  e function..**.*
2a5e6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2a5e7 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 77 20  s used to allow 
2a5e8 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d  virtual table im
2a5e9 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f  plementations to
2a5ea 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64 20 4d 41 54  .** overload MAT
2a5eb 43 48 2c 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20  CH, LIKE, GLOB, 
2a5ec 61 6e 64 20 52 45 47 45 58 50 20 6f 70 65 72 61  and REGEXP opera
2a5ed 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  tors..**.** Retu
2a5ee 72 6e 20 65 69 74 68 65 72 20 74 68 65 20 70 44  rn either the pD
2a5ef 65 66 20 61 72 67 75 6d 65 6e 74 20 28 69 6e 64  ef argument (ind
2a5f0 69 63 61 74 69 6e 67 20 6e 6f 20 63 68 61 6e 67  icating no chang
2a5f1 65 29 20 6f 72 20 61 20 0a 2a 2a 20 6e 65 77 20  e) or a .** new 
2a5f2 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
2a5f3 65 20 74 68 61 74 20 69 73 20 6d 61 72 6b 65 64  e that is marked
2a5f4 20 61 73 20 65 70 68 65 6d 65 72 61 6c 20 75 73   as ephemeral us
2a5f5 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54  ing the.** SQLIT
2a5f6 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 66 6c 61  E_FUNC_EPHEM fla
2a5f7 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
2a5f8 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71  VATE FuncDef *sq
2a5f9 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61  lite3VtabOverloa
2a5fa 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c  dFunction(.  sql
2a5fb 69 74 65 33 20 2a 64 62 2c 20 20 20 20 2f 2a 20  ite3 *db,    /* 
2a5fc 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2a5fd 69 6f 6e 20 66 6f 72 20 72 65 70 6f 72 74 69 6e  ion for reportin
2a5fe 67 20 6d 61 6c 6c 6f 63 20 70 72 6f 62 6c 65 6d  g malloc problem
2a5ff 73 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a  s */.  FuncDef *
2a600 70 44 65 66 2c 20 20 2f 2a 20 46 75 6e 63 74 69  pDef,  /* Functi
2a601 6f 6e 20 74 6f 20 70 6f 73 73 69 62 6c 79 20 6f  on to possibly o
2a602 76 65 72 6c 6f 61 64 20 2a 2f 0a 20 20 69 6e 74  verload */.  int
2a603 20 6e 41 72 67 2c 20 20 20 20 20 20 20 2f 2a 20   nArg,       /* 
2a604 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  Number of argume
2a605 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74  nts to the funct
2a606 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ion */.  Expr *p
2a607 45 78 70 72 20 20 20 20 20 2f 2a 20 46 69 72 73  Expr     /* Firs
2a608 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
2a609 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  e function */.){
2a60a 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
2a60b 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
2a60c 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33  pVtab;.  sqlite3
2a60d 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20  _module *pMod;. 
2a60e 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
2a60f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2a610 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
2a611 65 2a 2a 29 20 3d 20 30 3b 0a 20 20 76 6f 69 64  e**) = 0;.  void
2a612 20 2a 70 41 72 67 20 3d 20 30 3b 0a 20 20 46 75   *pArg = 0;.  Fu
2a613 6e 63 44 65 66 20 2a 70 4e 65 77 3b 0a 20 20 69  ncDef *pNew;.  i
2a614 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 68 61  nt rc = 0;.  cha
2a615 72 20 2a 7a 4c 6f 77 65 72 4e 61 6d 65 3b 0a 20  r *zLowerName;. 
2a616 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
2a617 7a 3b 0a 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  z;...  /* Check 
2a618 74 6f 20 73 65 65 20 74 68 65 20 6c 65 66 74 20  to see the left 
2a619 6f 70 65 72 61 6e 64 20 69 73 20 61 20 63 6f 6c  operand is a col
2a61a 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c  umn in a virtual
2a61b 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20   table */.  if( 
2a61c 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
2a61d 6e 20 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45  n pDef;.  if( pE
2a61e 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
2a61f 4d 4e 20 29 20 72 65 74 75 72 6e 20 70 44 65 66  MN ) return pDef
2a620 3b 0a 20 20 70 54 61 62 20 3d 20 70 45 78 70 72  ;.  pTab = pExpr
2a621 2d 3e 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54  ->pTab;.  if( pT
2a622 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70  ab==0 ) return p
2a623 44 65 66 3b 0a 20 20 69 66 28 20 28 70 54 61 62  Def;.  if( (pTab
2a624 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
2a625 56 69 72 74 75 61 6c 29 3d 3d 30 20 29 20 72 65  Virtual)==0 ) re
2a626 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70 56 74  turn pDef;.  pVt
2a627 61 62 20 3d 20 70 54 61 62 2d 3e 70 56 74 61 62  ab = pTab->pVtab
2a628 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61  ;.  assert( pVta
2a629 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  b!=0 );.  assert
2a62a 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  ( pVtab->pModule
2a62b 21 3d 30 20 29 3b 0a 20 20 70 4d 6f 64 20 3d 20  !=0 );.  pMod = 
2a62c 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20  (sqlite3_module 
2a62d 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  *)pVtab->pModule
2a62e 3b 0a 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 46  ;.  if( pMod->xF
2a62f 69 6e 64 46 75 6e 63 74 69 6f 6e 3d 3d 30 20 29  indFunction==0 )
2a630 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 0a   return pDef;. .
2a631 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 78 46    /* Call the xF
2a632 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68  indFunction meth
2a633 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61  od on the virtua
2a634 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  l table implemen
2a635 74 61 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 73  tation.  ** to s
2a636 65 65 20 69 66 20 74 68 65 20 69 6d 70 6c 65 6d  ee if the implem
2a637 65 6e 74 61 74 69 6f 6e 20 77 61 6e 74 73 20 74  entation wants t
2a638 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 69 73 20  o overload this 
2a639 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2f 0a 20  function .  */. 
2a63a 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 3d 20 73 71   zLowerName = sq
2a63b 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2a63c 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pDef->zName);.
2a63d 20 20 69 66 28 20 7a 4c 6f 77 65 72 4e 61 6d 65    if( zLowerName
2a63e 20 29 7b 0a 20 20 20 20 66 6f 72 28 7a 3d 28 75   ){.    for(z=(u
2a63f 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 4c  nsigned char*)zL
2a640 6f 77 65 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b  owerName; *z; z+
2a641 2b 29 7b 0a 20 20 20 20 20 20 2a 7a 20 3d 20 73  +){.      *z = s
2a642 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
2a643 65 72 5b 2a 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20  er[*z];.    }.  
2a644 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 46 69    rc = pMod->xFi
2a645 6e 64 46 75 6e 63 74 69 6f 6e 28 70 56 74 61 62  ndFunction(pVtab
2a646 2c 20 6e 41 72 67 2c 20 7a 4c 6f 77 65 72 4e 61  , nArg, zLowerNa
2a647 6d 65 2c 20 26 78 46 75 6e 63 2c 20 26 70 41 72  me, &xFunc, &pAr
2a648 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  g);.    sqlite3D
2a649 62 46 72 65 65 28 64 62 2c 20 7a 4c 6f 77 65 72  bFree(db, zLower
2a64a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Name);.    if( p
2a64b 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 29 7b  Vtab->zErrMsg ){
2a64c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2a64d 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 25 73 22  ror(db, rc, "%s"
2a64e 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  , pVtab->zErrMsg
2a64f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2a650 44 62 46 72 65 65 28 64 62 2c 20 70 56 74 61 62  DbFree(db, pVtab
2a651 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
2a652 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67    pVtab->zErrMsg
2a653 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
2a654 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20    if( rc==0 ){. 
2a655 20 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a     return pDef;.
2a656 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65    }..  /* Create
2a657 20 61 20 6e 65 77 20 65 70 68 65 6d 65 72 61 6c   a new ephemeral
2a658 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
2a659 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6f 76 65  tion for the ove
2a65a 72 6c 6f 61 64 65 64 0a 20 20 2a 2a 20 66 75 6e  rloaded.  ** fun
2a65b 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e 65 77 20  ction */.  pNew 
2a65c 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2a65d 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2a65e 28 2a 70 4e 65 77 29 0a 20 20 20 20 20 20 20 20  (*pNew).        
2a65f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a660 20 20 20 20 20 2b 20 73 71 6c 69 74 65 33 53 74       + sqlite3St
2a661 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a 4e 61  rlen30(pDef->zNa
2a662 6d 65 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65  me) );.  if( pNe
2a663 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  w==0 ){.    retu
2a664 72 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 20 20 2a  rn pDef;.  }.  *
2a665 70 4e 65 77 20 3d 20 2a 70 44 65 66 3b 0a 20 20  pNew = *pDef;.  
2a666 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63  pNew->zName = (c
2a667 68 61 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a  har *)&pNew[1];.
2a668 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a    memcpy(pNew->z
2a669 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d  Name, pDef->zNam
2a66a 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
2a66b 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 2b  30(pDef->zName)+
2a66c 31 29 3b 0a 20 20 70 4e 65 77 2d 3e 78 46 75 6e  1);.  pNew->xFun
2a66d 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 4e 65  c = xFunc;.  pNe
2a66e 77 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20 70  w->pUserData = p
2a66f 41 72 67 3b 0a 20 20 70 4e 65 77 2d 3e 66 6c 61  Arg;.  pNew->fla
2a670 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 46 55 4e  gs |= SQLITE_FUN
2a671 43 5f 45 50 48 45 4d 3b 0a 20 20 72 65 74 75 72  C_EPHEM;.  retur
2a672 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
2a673 20 4d 61 6b 65 20 73 75 72 65 20 76 69 72 74 75   Make sure virtu
2a674 61 6c 20 74 61 62 6c 65 20 70 54 61 62 20 69 73  al table pTab is
2a675 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68   contained in th
2a676 65 20 70 50 61 72 73 65 2d 3e 61 70 56 69 72 74  e pParse->apVirt
2a677 75 61 6c 4c 6f 63 6b 5b 5d 0a 2a 2a 20 61 72 72  ualLock[].** arr
2a678 61 79 20 73 6f 20 74 68 61 74 20 61 6e 20 4f 50  ay so that an OP
2a679 5f 56 42 65 67 69 6e 20 77 69 6c 6c 20 67 65 74  _VBegin will get
2a67a 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 69   generated for i
2a67b 74 2e 20 20 41 64 64 20 70 54 61 62 20 74 6f 20  t.  Add pTab to 
2a67c 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 69 66 20  the.** array if 
2a67d 69 74 20 69 73 20 6d 69 73 73 69 6e 67 2e 20 20  it is missing.  
2a67e 49 66 20 70 54 61 62 20 69 73 20 61 6c 72 65 61  If pTab is alrea
2a67f 64 79 20 69 6e 20 74 68 65 20 61 72 72 61 79 2c  dy in the array,
2a680 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
2a681 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
2a682 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2a683 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d  oid sqlite3VtabM
2a684 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73  akeWritable(Pars
2a685 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
2a686 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20 69   *pTab){.  int i
2a687 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 49  , n;.  assert( I
2a688 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2a689 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2a68a 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b  Parse->nVtabLock
2a68b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2a68c 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 61 70  pTab==pParse->ap
2a68d 56 74 61 62 4c 6f 63 6b 5b 69 5d 20 29 20 72 65  VtabLock[i] ) re
2a68e 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20  turn;.  }.  n = 
2a68f 28 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f  (pParse->nVtabLo
2a690 63 6b 2b 31 29 2a 73 69 7a 65 6f 66 28 70 50 61  ck+1)*sizeof(pPa
2a691 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b  rse->apVtabLock[
2a692 30 5d 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 61  0]);.  pParse->a
2a693 70 56 74 61 62 4c 6f 63 6b 20 3d 20 73 71 6c 69  pVtabLock = sqli
2a694 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 50 61 72  te3_realloc(pPar
2a695 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 2c 20  se->apVtabLock, 
2a696 6e 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  n);.  if( pParse
2a697 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 20 29 7b 0a  ->apVtabLock ){.
2a698 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 74      pParse->apVt
2a699 61 62 4c 6f 63 6b 5b 70 50 61 72 73 65 2d 3e 6e  abLock[pParse->n
2a69a 56 74 61 62 4c 6f 63 6b 2b 2b 5d 20 3d 20 70 54  VtabLock++] = pT
2a69b 61 62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ab;.  }else{.   
2a69c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
2a69d 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2a69e 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20   }.}..#endif /* 
2a69f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2a6a0 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a  UALTABLE */../**
2a6a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2a6a2 20 6f 66 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a   of vtab.c *****
2a6a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2a6a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2a6a7 69 6e 20 66 69 6c 65 20 77 68 65 72 65 2e 63 20  in file where.c 
2a6a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2a6ab 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
2a6ac 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
2a6ad 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2a6ae 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2a6af 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2a6b0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2a6b1 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2a6b2 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2a6b3 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2a6b4 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2a6b5 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2a6b6 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2a6b7 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2a6b8 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2a6b9 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2a6ba 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2a6bb 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2a6bc 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2a6bd 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2a6be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6c2 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  ***.** This modu
2a6c3 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
2a6c4 64 65 20 74 68 61 74 20 67 65 6e 65 72 61 74 65  de that generate
2a6c5 73 20 56 44 42 45 20 63 6f 64 65 20 75 73 65 64  s VDBE code used
2a6c6 20 74 6f 20 70 72 6f 63 65 73 73 0a 2a 2a 20 74   to process.** t
2a6c7 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2a6c8 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  of SQL statement
2a6c9 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  s.  This module 
2a6ca 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
2a6cb 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67  or.** generating
2a6cc 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 6c   the code that l
2a6cd 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 20 74  oops through a t
2a6ce 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  able looking for
2a6cf 20 61 70 70 6c 69 63 61 62 6c 65 0a 2a 2a 20 72   applicable.** r
2a6d0 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 20 61 72  ows.  Indices ar
2a6d1 65 20 73 65 6c 65 63 74 65 64 20 61 6e 64 20 75  e selected and u
2a6d2 73 65 64 20 74 6f 20 73 70 65 65 64 20 74 68 65  sed to speed the
2a6d3 20 73 65 61 72 63 68 20 77 68 65 6e 20 64 6f 69   search when doi
2a6d4 6e 67 0a 2a 2a 20 73 6f 20 69 73 20 61 70 70 6c  ng.** so is appl
2a6d5 69 63 61 62 6c 65 2e 20 20 42 65 63 61 75 73 65  icable.  Because
2a6d6 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
2a6d7 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
2a6d8 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 69 6e 64  selecting.** ind
2a6d9 69 63 65 73 2c 20 79 6f 75 20 6d 69 67 68 74 20  ices, you might 
2a6da 61 6c 73 6f 20 74 68 69 6e 6b 20 6f 66 20 74 68  also think of th
2a6db 69 73 20 6d 6f 64 75 6c 65 20 61 73 20 74 68 65  is module as the
2a6dc 20 22 71 75 65 72 79 20 6f 70 74 69 6d 69 7a 65   "query optimize
2a6dd 72 22 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77  r"..**.** $Id: w
2a6de 68 65 72 65 2e 63 2c 76 20 31 2e 33 37 39 20 32  here.c,v 1.379 2
2a6df 30 30 39 2f 30 33 2f 32 39 20 30 30 3a 31 35 3a  009/03/29 00:15:
2a6e0 35 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  54 drh Exp $.*/.
2a6e1 0a 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f 75 74  ./*.** Trace out
2a6e2 70 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a 23 69  put macros.*/.#i
2a6e3 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
2a6e4 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65  _TEST) || define
2a6e5 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
2a6e6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a6e7 6e 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 54  nt sqlite3WhereT
2a6e8 72 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66  race = 0;.#endif
2a6e9 0a 23 69 66 20 30 0a 23 20 64 65 66 69 6e 65 20  .#if 0.# define 
2a6ea 57 48 45 52 45 54 52 41 43 45 28 58 29 20 20 69  WHERETRACE(X)  i
2a6eb 66 28 73 71 6c 69 74 65 33 57 68 65 72 65 54 72  f(sqlite3WhereTr
2a6ec 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75  ace) sqlite3Debu
2a6ed 67 50 72 69 6e 74 66 20 58 0a 23 65 6c 73 65 0a  gPrintf X.#else.
2a6ee 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 54 52  # define WHERETR
2a6ef 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f  ACE(X).#endif../
2a6f0 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65  * Forward refere
2a6f1 6e 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  nce.*/.typedef s
2a6f2 74 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73  truct WhereClaus
2a6f3 65 20 57 68 65 72 65 43 6c 61 75 73 65 3b 0a 74  e WhereClause;.t
2a6f4 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68  ypedef struct Wh
2a6f5 65 72 65 4d 61 73 6b 53 65 74 20 57 68 65 72 65  ereMaskSet Where
2a6f6 4d 61 73 6b 53 65 74 3b 0a 74 79 70 65 64 65 66  MaskSet;.typedef
2a6f7 20 73 74 72 75 63 74 20 57 68 65 72 65 4f 72 49   struct WhereOrI
2a6f8 6e 66 6f 20 57 68 65 72 65 4f 72 49 6e 66 6f 3b  nfo WhereOrInfo;
2a6f9 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2a6fa 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 57 68 65  WhereAndInfo Whe
2a6fb 72 65 41 6e 64 49 6e 66 6f 3b 0a 74 79 70 65 64  reAndInfo;.typed
2a6fc 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 43  ef struct WhereC
2a6fd 6f 73 74 20 57 68 65 72 65 43 6f 73 74 3b 0a 0a  ost WhereCost;..
2a6fe 2f 2a 0a 2a 2a 20 54 68 65 20 71 75 65 72 79 20  /*.** The query 
2a6ff 67 65 6e 65 72 61 74 6f 72 20 75 73 65 73 20 61  generator uses a
2a700 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 73 74 61  n array of insta
2a701 6e 63 65 73 20 6f 66 20 74 68 69 73 20 73 74 72  nces of this str
2a702 75 63 74 75 72 65 20 74 6f 0a 2a 2a 20 68 65 6c  ucture to.** hel
2a703 70 20 69 74 20 61 6e 61 6c 79 7a 65 20 74 68 65  p it analyze the
2a704 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20   subexpressions 
2a705 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
2a706 75 73 65 2e 20 20 45 61 63 68 20 57 48 45 52 45  use.  Each WHERE
2a707 0a 2a 2a 20 63 6c 61 75 73 65 20 73 75 62 65 78  .** clause subex
2a708 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61  pression is sepa
2a709 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f  rated from the o
2a70a 74 68 65 72 73 20 62 79 20 41 4e 44 20 6f 70 65  thers by AND ope
2a70b 72 61 74 6f 72 73 2e 0a 2a 2a 20 28 4e 6f 74 65  rators..** (Note
2a70c 3a 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 20  : the same data 
2a70d 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 73  structure is als
2a70e 6f 20 72 65 75 73 65 64 20 74 6f 20 68 6f 6c 64  o reused to hold
2a70f 20 61 20 67 72 6f 75 70 20 6f 66 20 74 65 72 6d   a group of term
2a710 73 0a 2a 2a 20 73 65 70 61 72 61 74 65 64 20 62  s.** separated b
2a711 79 20 4f 52 20 6f 70 65 72 61 74 6f 72 73 2e 20  y OR operators. 
2a712 20 42 75 74 20 61 74 20 74 68 65 20 74 6f 70 2d   But at the top-
2a713 6c 65 76 65 6c 2c 20 65 76 65 72 79 74 68 69 6e  level, everythin
2a714 67 20 69 73 20 41 4e 44 0a 2a 2a 20 73 65 70 61  g is AND.** sepa
2a715 72 61 74 65 64 2e 29 0a 2a 2a 0a 2a 2a 20 41 6c  rated.).**.** Al
2a716 6c 20 57 68 65 72 65 54 65 72 6d 73 20 61 72 65  l WhereTerms are
2a717 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20   collected into 
2a718 61 20 73 69 6e 67 6c 65 20 57 68 65 72 65 43 6c  a single WhereCl
2a719 61 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 20  ause structure. 
2a71a 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69   .** The followi
2a71b 6e 67 20 69 64 65 6e 74 69 74 79 20 68 6f 6c 64  ng identity hold
2a71c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
2a71d 57 68 65 72 65 54 65 72 6d 2e 70 57 43 2d 3e 61  WhereTerm.pWC->a
2a71e 5b 57 68 65 72 65 54 65 72 6d 2e 69 64 78 5d 20  [WhereTerm.idx] 
2a71f 3d 3d 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a 0a  == WhereTerm.**.
2a720 2a 2a 20 57 68 65 6e 20 61 20 74 65 72 6d 20 69  ** When a term i
2a721 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
2a722 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  *.**            
2a723 20 20 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 0a    X <op> <expr>.
2a724 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73  **.** where X is
2a725 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61   a column name a
2a726 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f  nd <op> is one o
2a727 66 20 63 65 72 74 61 69 6e 20 6f 70 65 72 61 74  f certain operat
2a728 6f 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 57 68 65  ors,.** then Whe
2a729 72 65 54 65 72 6d 2e 6c 65 66 74 43 75 72 73 6f  reTerm.leftCurso
2a72a 72 20 61 6e 64 20 57 68 65 72 65 54 65 72 6d 2e  r and WhereTerm.
2a72b 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 72 65 63  u.leftColumn rec
2a72c 6f 72 64 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f  ord the.** curso
2a72d 72 20 6e 75 6d 62 65 72 20 61 6e 64 20 63 6f 6c  r number and col
2a72e 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 58  umn number for X
2a72f 2e 20 20 57 68 65 72 65 54 65 72 6d 2e 65 4f 70  .  WhereTerm.eOp
2a730 65 72 61 74 6f 72 20 72 65 63 6f 72 64 73 0a 2a  erator records.*
2a731 2a 20 74 68 65 20 3c 6f 70 3e 20 75 73 69 6e 67  * the <op> using
2a732 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64   a bitmask encod
2a733 69 6e 67 20 64 65 66 69 6e 65 64 20 62 79 20 57  ing defined by W
2a734 4f 5f 78 78 78 20 62 65 6c 6f 77 2e 20 20 54 68  O_xxx below.  Th
2a735 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 20 62 69  e.** use of a bi
2a736 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 66  tmask encoding f
2a737 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  or the operator 
2a738 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 73 65 61  allows us to sea
2a739 72 63 68 0a 2a 2a 20 71 75 69 63 6b 6c 79 20 66  rch.** quickly f
2a73a 6f 72 20 74 65 72 6d 73 20 74 68 61 74 20 6d 61  or terms that ma
2a73b 74 63 68 20 61 6e 79 20 6f 66 20 73 65 76 65 72  tch any of sever
2a73c 61 6c 20 64 69 66 66 65 72 65 6e 74 20 6f 70 65  al different ope
2a73d 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 20  rators..**.** A 
2a73e 57 68 65 72 65 54 65 72 6d 20 6d 69 67 68 74 20  WhereTerm might 
2a73f 61 6c 73 6f 20 62 65 20 74 77 6f 20 6f 72 20 6d  also be two or m
2a740 6f 72 65 20 73 75 62 74 65 72 6d 73 20 63 6f 6e  ore subterms con
2a741 6e 65 63 74 65 64 20 62 79 20 4f 52 3a 0a 2a 2a  nected by OR:.**
2a742 0a 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31 2e  .**         (t1.
2a743 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20 4f  X <op> <expr>) O
2a744 52 20 28 74 31 2e 59 20 3c 6f 70 3e 20 3c 65 78  R (t1.Y <op> <ex
2a745 70 72 3e 29 20 4f 52 20 2e 2e 2e 2e 0a 2a 2a 0a  pr>) OR .....**.
2a746 2a 2a 20 49 6e 20 74 68 69 73 20 73 65 63 6f 6e  ** In this secon
2a747 64 20 63 61 73 65 2c 20 77 74 46 6c 61 67 20 61  d case, wtFlag a
2a748 73 20 74 68 65 20 54 45 52 4d 5f 4f 52 49 4e 46  s the TERM_ORINF
2a749 4f 20 73 65 74 20 61 6e 64 20 65 4f 70 65 72 61  O set and eOpera
2a74a 74 6f 72 3d 3d 57 4f 5f 4f 52 0a 2a 2a 20 61 6e  tor==WO_OR.** an
2a74b 64 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e  d the WhereTerm.
2a74c 75 2e 70 4f 72 49 6e 66 6f 20 66 69 65 6c 64 20  u.pOrInfo field 
2a74d 70 6f 69 6e 74 73 20 74 6f 20 61 75 78 69 6c 69  points to auxili
2a74e 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ary information 
2a74f 74 68 61 74 0a 2a 2a 20 69 73 20 63 6f 6c 6c 65  that.** is colle
2a750 63 74 65 64 20 61 62 6f 75 74 20 74 68 65 0a 2a  cted about the.*
2a751 2a 0a 2a 2a 20 49 66 20 61 20 74 65 72 6d 20 69  *.** If a term i
2a752 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
2a753 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63  se does not matc
2a754 68 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20  h either of the 
2a755 74 77 6f 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20  two previous.** 
2a756 63 61 74 65 67 6f 72 69 65 73 2c 20 74 68 65 6e  categories, then
2a757 20 65 4f 70 65 72 61 74 6f 72 3d 3d 30 2e 20 20   eOperator==0.  
2a758 54 68 65 20 57 68 65 72 65 54 65 72 6d 2e 70 45  The WhereTerm.pE
2a759 78 70 72 20 66 69 65 6c 64 20 69 73 20 73 74 69  xpr field is sti
2a75a 6c 6c 20 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65  ll set.** to the
2a75b 20 6f 72 69 67 69 6e 61 6c 20 73 75 62 65 78 70   original subexp
2a75c 72 65 73 73 69 6f 6e 20 63 6f 6e 74 65 6e 74 20  ression content 
2a75d 61 6e 64 20 77 74 46 6c 61 67 73 20 69 73 20 73  and wtFlags is s
2a75e 65 74 20 75 70 20 61 70 70 72 6f 70 72 69 61 74  et up appropriat
2a75f 65 6c 79 0a 2a 2a 20 62 75 74 20 6e 6f 20 6f 74  ely.** but no ot
2a760 68 65 72 20 66 69 65 6c 64 73 20 69 6e 20 74 68  her fields in th
2a761 65 20 57 68 65 72 65 54 65 72 6d 20 6f 62 6a 65  e WhereTerm obje
2a762 63 74 20 61 72 65 20 6d 65 61 6e 69 6e 67 66 75  ct are meaningfu
2a763 6c 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 65 4f  l..**.** When eO
2a764 70 65 72 61 74 6f 72 21 3d 30 2c 20 70 72 65 72  perator!=0, prer
2a765 65 71 52 69 67 68 74 20 61 6e 64 20 70 72 65 72  eqRight and prer
2a766 65 71 41 6c 6c 20 72 65 63 6f 72 64 20 73 65 74  eqAll record set
2a767 73 20 6f 66 20 63 75 72 73 6f 72 20 6e 75 6d 62  s of cursor numb
2a768 65 72 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65 79  ers,.** but they
2a769 20 64 6f 20 73 6f 20 69 6e 64 69 72 65 63 74 6c   do so indirectl
2a76a 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 57 68 65  y.  A single Whe
2a76b 72 65 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74  reMaskSet struct
2a76c 75 72 65 20 74 72 61 6e 73 6c 61 74 65 73 0a 2a  ure translates.*
2a76d 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * cursor number 
2a76e 69 6e 74 6f 20 62 69 74 73 20 61 6e 64 20 74 68  into bits and th
2a76f 65 20 74 72 61 6e 73 6c 61 74 65 64 20 62 69 74  e translated bit
2a770 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
2a771 65 20 70 72 65 72 65 71 0a 2a 2a 20 66 69 65 6c  e prereq.** fiel
2a772 64 73 2e 20 20 54 68 65 20 74 72 61 6e 73 6c 61  ds.  The transla
2a773 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20  tion is used in 
2a774 6f 72 64 65 72 20 74 6f 20 6d 61 78 69 6d 69 7a  order to maximiz
2a775 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  e the number of.
2a776 2a 2a 20 62 69 74 73 20 74 68 61 74 20 77 69 6c  ** bits that wil
2a777 6c 20 66 69 74 20 69 6e 20 61 20 42 69 74 6d 61  l fit in a Bitma
2a778 73 6b 2e 20 20 54 68 65 20 56 44 42 45 20 63 75  sk.  The VDBE cu
2a779 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69 67  rsor numbers mig
2a77a 68 74 20 62 65 0a 2a 2a 20 73 70 72 65 61 64 20  ht be.** spread 
2a77b 6f 75 74 20 6f 76 65 72 20 74 68 65 20 6e 6f 6e  out over the non
2a77c 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 65  -negative intege
2a77d 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  rs.  For example
2a77e 2c 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  , the cursor.** 
2a77f 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62 65  numbers might be
2a780 20 33 2c 20 38 2c 20 39 2c 20 31 30 2c 20 32 30   3, 8, 9, 10, 20
2a781 2c 20 32 33 2c 20 34 31 2c 20 61 6e 64 20 34 35  , 23, 41, and 45
2a782 2e 20 20 54 68 65 20 57 68 65 72 65 4d 61 73 6b  .  The WhereMask
2a783 53 65 74 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65  Set.** translate
2a784 73 20 74 68 65 73 65 20 73 70 61 72 73 65 20 63  s these sparse c
2a785 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
2a786 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69  to consecutive i
2a787 6e 74 65 67 65 72 73 0a 2a 2a 20 62 65 67 69 6e  ntegers.** begin
2a788 6e 69 6e 67 20 77 69 74 68 20 30 20 69 6e 20 6f  ning with 0 in o
2a789 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 74 68 65  rder to make the
2a78a 20 62 65 73 74 20 70 6f 73 73 69 62 6c 65 20 75   best possible u
2a78b 73 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61  se of the availa
2a78c 62 6c 65 0a 2a 2a 20 62 69 74 73 20 69 6e 20 74  ble.** bits in t
2a78d 68 65 20 42 69 74 6d 61 73 6b 2e 20 20 53 6f 2c  he Bitmask.  So,
2a78e 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20   in the example 
2a78f 61 62 6f 76 65 2c 20 74 68 65 20 63 75 72 73 6f  above, the curso
2a790 72 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 6f 75  r numbers.** wou
2a791 6c 64 20 62 65 20 6d 61 70 70 65 64 20 69 6e 74  ld be mapped int
2a792 6f 20 69 6e 74 65 67 65 72 73 20 30 20 74 68 72  o integers 0 thr
2a793 6f 75 67 68 20 37 2e 0a 2a 2a 0a 2a 2a 20 54 68  ough 7..**.** Th
2a794 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  e number of term
2a795 73 20 69 6e 20 61 20 6a 6f 69 6e 20 69 73 20 6c  s in a join is l
2a796 69 6d 69 74 65 64 20 62 79 20 74 68 65 20 6e 75  imited by the nu
2a797 6d 62 65 72 20 6f 66 20 62 69 74 73 0a 2a 2a 20  mber of bits.** 
2a798 69 6e 20 70 72 65 72 65 71 52 69 67 68 74 20 61  in prereqRight a
2a799 6e 64 20 70 72 65 72 65 71 41 6c 6c 2e 20 20 54  nd prereqAll.  T
2a79a 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 36 34  he default is 64
2a79b 20 62 69 74 73 2c 20 68 65 6e 63 65 20 53 51 4c   bits, hence SQL
2a79c 69 74 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61  ite.** is only a
2a79d 62 6c 65 20 74 6f 20 70 72 6f 63 65 73 73 20 6a  ble to process j
2a79e 6f 69 6e 73 20 77 69 74 68 20 36 34 20 6f 72 20  oins with 64 or 
2a79f 66 65 77 65 72 20 74 61 62 6c 65 73 2e 0a 2a 2f  fewer tables..*/
2a7a0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2a7a1 57 68 65 72 65 54 65 72 6d 20 57 68 65 72 65 54  WhereTerm WhereT
2a7a2 65 72 6d 3b 0a 73 74 72 75 63 74 20 57 68 65 72  erm;.struct Wher
2a7a3 65 54 65 72 6d 20 7b 0a 20 20 45 78 70 72 20 2a  eTerm {.  Expr *
2a7a4 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
2a7a5 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
2a7a6 74 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  the subexpressio
2a7a7 6e 20 74 68 61 74 20 69 73 20 74 68 69 73 20 74  n that is this t
2a7a8 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61  erm */.  int iPa
2a7a9 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  rent;           
2a7aa 20 2f 2a 20 44 69 73 61 62 6c 65 20 70 57 43 2d   /* Disable pWC-
2a7ab 3e 61 5b 69 50 61 72 65 6e 74 5d 20 77 68 65 6e  >a[iParent] when
2a7ac 20 74 68 69 73 20 74 65 72 6d 20 64 69 73 61 62   this term disab
2a7ad 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 66  led */.  int lef
2a7ae 74 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20  tCursor;        
2a7af 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
2a7b0 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70  r of X in "X <op
2a7b1 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20 75  > <expr>" */.  u
2a7b2 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 6e 74 20 6c  nion {.    int l
2a7b3 65 66 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  eftColumn;      
2a7b4 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
2a7b5 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c  ber of X in "X <
2a7b6 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20  op> <expr>" */. 
2a7b7 20 20 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a     WhereOrInfo *
2a7b8 70 4f 72 49 6e 66 6f 3b 20 20 20 2f 2a 20 45 78  pOrInfo;   /* Ex
2a7b9 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  tra information 
2a7ba 69 66 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  if eOperator==WO
2a7bb 5f 4f 52 20 2a 2f 0a 20 20 20 20 57 68 65 72 65  _OR */.    Where
2a7bc 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66  AndInfo *pAndInf
2a7bd 6f 3b 20 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f  o; /* Extra info
2a7be 72 6d 61 74 69 6f 6e 20 69 66 20 65 4f 70 65 72  rmation if eOper
2a7bf 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 2a 2f 0a  ator==WO_AND */.
2a7c0 20 20 7d 20 75 3b 0a 20 20 75 31 36 20 65 4f 70    } u;.  u16 eOp
2a7c1 65 72 61 74 6f 72 3b 20 20 20 20 20 20 20 20 20  erator;         
2a7c2 20 2f 2a 20 41 20 57 4f 5f 78 78 20 76 61 6c 75   /* A WO_xx valu
2a7c3 65 20 64 65 73 63 72 69 62 69 6e 67 20 3c 6f 70  e describing <op
2a7c4 3e 20 2a 2f 0a 20 20 75 38 20 77 74 46 6c 61 67  > */.  u8 wtFlag
2a7c5 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s;             /
2a7c6 2a 20 54 45 52 4d 5f 78 78 78 20 62 69 74 20 66  * TERM_xxx bit f
2a7c7 6c 61 67 73 2e 20 20 53 65 65 20 62 65 6c 6f 77  lags.  See below
2a7c8 20 2a 2f 0a 20 20 75 38 20 6e 43 68 69 6c 64 3b   */.  u8 nChild;
2a7c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a7ca 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 69 6c 64   Number of child
2a7cb 72 65 6e 20 74 68 61 74 20 6d 75 73 74 20 64 69  ren that must di
2a7cc 73 61 62 6c 65 20 75 73 20 2a 2f 0a 20 20 57 68  sable us */.  Wh
2a7cd 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20  ereClause *pWC; 
2a7ce 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6c 61        /* The cla
2a7cf 75 73 65 20 74 68 69 73 20 74 65 72 6d 20 69 73  use this term is
2a7d0 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 42 69   part of */.  Bi
2a7d1 74 6d 61 73 6b 20 70 72 65 72 65 71 52 69 67 68  tmask prereqRigh
2a7d2 74 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b  t;    /* Bitmask
2a7d3 20 6f 66 20 74 61 62 6c 65 73 20 75 73 65 64 20   of tables used 
2a7d4 62 79 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  by pExpr->pRight
2a7d5 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72   */.  Bitmask pr
2a7d6 65 72 65 71 41 6c 6c 3b 20 20 20 20 20 20 2f 2a  ereqAll;      /*
2a7d7 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c   Bitmask of tabl
2a7d8 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  es referenced by
2a7d9 20 70 45 78 70 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pExpr */.};../*
2a7da 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
2a7db 65 73 20 6f 66 20 57 68 65 72 65 54 65 72 6d 2e  es of WhereTerm.
2a7dc 77 74 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  wtFlags.*/.#defi
2a7dd 6e 65 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20  ne TERM_DYNAMIC 
2a7de 20 20 20 30 78 30 31 20 20 20 2f 2a 20 4e 65 65     0x01   /* Nee
2a7df 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65  d to call sqlite
2a7e0 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2a7e1 70 45 78 70 72 29 20 2a 2f 0a 23 64 65 66 69 6e  pExpr) */.#defin
2a7e2 65 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 20  e TERM_VIRTUAL  
2a7e3 20 20 30 78 30 32 20 20 20 2f 2a 20 41 64 64 65    0x02   /* Adde
2a7e4 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a  d by the optimiz
2a7e5 65 72 2e 20 20 44 6f 20 6e 6f 74 20 63 6f 64 65  er.  Do not code
2a7e6 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d   */.#define TERM
2a7e7 5f 43 4f 44 45 44 20 20 20 20 20 20 30 78 30 34  _CODED      0x04
2a7e8 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72 6d 20     /* This term 
2a7e9 69 73 20 61 6c 72 65 61 64 79 20 63 6f 64 65 64  is already coded
2a7ea 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d   */.#define TERM
2a7eb 5f 43 4f 50 49 45 44 20 20 20 20 20 30 78 30 38  _COPIED     0x08
2a7ec 20 20 20 2f 2a 20 48 61 73 20 61 20 63 68 69 6c     /* Has a chil
2a7ed 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52  d */.#define TER
2a7ee 4d 5f 4f 52 49 4e 46 4f 20 20 20 20 20 30 78 31  M_ORINFO     0x1
2a7ef 30 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 66  0   /* Need to f
2a7f0 72 65 65 20 74 68 65 20 57 68 65 72 65 54 65 72  ree the WhereTer
2a7f1 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 6f 62 6a 65  m.u.pOrInfo obje
2a7f2 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  ct */.#define TE
2a7f3 52 4d 5f 41 4e 44 49 4e 46 4f 20 20 20 20 30 78  RM_ANDINFO    0x
2a7f4 32 30 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20  20   /* Need to 
2a7f5 66 72 65 65 20 74 68 65 20 57 68 65 72 65 54 65  free the WhereTe
2a7f6 72 6d 2e 75 2e 70 41 6e 64 49 6e 66 6f 20 6f 62  rm.u.pAndInfo ob
2a7f7 6a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52  j */.#define TER
2a7f8 4d 5f 4f 52 5f 4f 4b 20 20 20 20 20 20 30 78 34  M_OR_OK      0x4
2a7f9 30 20 20 20 2f 2a 20 55 73 65 64 20 64 75 72 69  0   /* Used duri
2a7fa 6e 67 20 4f 52 2d 63 6c 61 75 73 65 20 70 72 6f  ng OR-clause pro
2a7fb 63 65 73 73 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a  cessing */../*.*
2a7fc 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
2a7fd 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
2a7fe 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61  tructure holds a
2a7ff 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ll information a
2a800 62 6f 75 74 20 61 0a 2a 2a 20 57 48 45 52 45 20  bout a.** WHERE 
2a801 63 6c 61 75 73 65 2e 20 20 4d 6f 73 74 6c 79 20  clause.  Mostly 
2a802 74 68 69 73 20 69 73 20 61 20 63 6f 6e 74 61 69  this is a contai
2a803 6e 65 72 20 66 6f 72 20 6f 6e 65 20 6f 72 20 6d  ner for one or m
2a804 6f 72 65 20 57 68 65 72 65 54 65 72 6d 73 2e 0a  ore WhereTerms..
2a805 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 43  */.struct WhereC
2a806 6c 61 75 73 65 20 7b 0a 20 20 50 61 72 73 65 20  lause {.  Parse 
2a807 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20  *pParse;        
2a808 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2a809 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
2a80a 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2a80b 6b 53 65 74 3b 20 20 2f 2a 20 4d 61 70 70 69 6e  kSet;  /* Mappin
2a80c 67 20 6f 66 20 74 61 62 6c 65 20 63 75 72 73 6f  g of table curso
2a80d 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62 69 74  r numbers to bit
2a80e 6d 61 73 6b 73 20 2a 2f 0a 20 20 75 38 20 6f 70  masks */.  u8 op
2a80f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2a810 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 6f 70 65      /* Split ope
2a811 72 61 74 6f 72 2e 20 20 54 4b 5f 41 4e 44 20 6f  rator.  TK_AND o
2a812 72 20 54 4b 5f 4f 52 20 2a 2f 0a 20 20 69 6e 74  r TK_OR */.  int
2a813 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20   nTerm;         
2a814 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2a815 6f 66 20 74 65 72 6d 73 20 2a 2f 0a 20 20 69 6e  of terms */.  in
2a816 74 20 6e 53 6c 6f 74 3b 20 20 20 20 20 20 20 20  t nSlot;        
2a817 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2a818 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
2a819 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  [] */.  WhereTer
2a81a 6d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20  m *a;           
2a81b 20 2f 2a 20 45 61 63 68 20 61 5b 5d 20 64 65 73   /* Each a[] des
2a81c 63 72 69 62 65 73 20 61 20 74 65 72 6d 20 6f 66  cribes a term of
2a81d 20 74 68 65 20 57 48 45 52 45 20 63 6c 75 61 73   the WHERE cluas
2a81e 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  e */.  WhereTerm
2a81f 20 61 53 74 61 74 69 63 5b 34 5d 3b 20 20 20 20   aStatic[4];    
2a820 2f 2a 20 49 6e 69 74 69 61 6c 20 73 74 61 74 69  /* Initial stati
2a821 63 20 73 70 61 63 65 20 66 6f 72 20 61 5b 5d 20  c space for a[] 
2a822 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57  */.};../*.** A W
2a823 68 65 72 65 54 65 72 6d 20 77 69 74 68 20 65 4f  hereTerm with eO
2a824 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 68  perator==WO_OR h
2a825 61 73 20 69 74 73 20 75 2e 70 4f 72 49 6e 66 6f  as its u.pOrInfo
2a826 20 70 6f 69 6e 74 65 72 20 73 65 74 20 74 6f 0a   pointer set to.
2a827 2a 2a 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  ** a dynamically
2a828 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 73 74 61   allocated insta
2a829 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
2a82a 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
2a82b 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4f  */.struct WhereO
2a82c 72 49 6e 66 6f 20 7b 0a 20 20 57 68 65 72 65 43  rInfo {.  WhereC
2a82d 6c 61 75 73 65 20 77 63 3b 20 20 20 20 20 20 20  lause wc;       
2a82e 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74     /* Decomposit
2a82f 69 6f 6e 20 69 6e 74 6f 20 73 75 62 74 65 72 6d  ion into subterm
2a830 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 69  s */.  Bitmask i
2a831 6e 64 65 78 61 62 6c 65 3b 20 20 20 20 20 20 20  ndexable;       
2a832 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 61 6c  /* Bitmask of al
2a833 6c 20 69 6e 64 65 78 61 62 6c 65 20 74 61 62 6c  l indexable tabl
2a834 65 73 20 69 6e 20 74 68 65 20 63 6c 61 75 73 65  es in the clause
2a835 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
2a836 57 68 65 72 65 54 65 72 6d 20 77 69 74 68 20 65  WhereTerm with e
2a837 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44  Operator==WO_AND
2a838 20 68 61 73 20 69 74 73 20 75 2e 70 41 6e 64 49   has its u.pAndI
2a839 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73 65 74 20  nfo pointer set 
2a83a 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d 69 63 61  to.** a dynamica
2a83b 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  lly allocated in
2a83c 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
2a83d 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
2a83e 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  e..*/.struct Whe
2a83f 72 65 41 6e 64 49 6e 66 6f 20 7b 0a 20 20 57 68  reAndInfo {.  Wh
2a840 65 72 65 43 6c 61 75 73 65 20 77 63 3b 20 20 20  ereClause wc;   
2a841 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 75         /* The su
2a842 62 65 78 70 72 65 73 73 69 6f 6e 20 62 72 6f 6b  bexpression brok
2a843 65 6e 20 6f 75 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  en out */.};../*
2a844 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
2a845 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2a846 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
2a847 20 74 72 61 63 6b 20 6f 66 20 61 20 6d 61 70 70   track of a mapp
2a848 69 6e 67 0a 2a 2a 20 62 65 74 77 65 65 6e 20 56  ing.** between V
2a849 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2a84a 72 73 20 61 6e 64 20 62 69 74 73 20 6f 66 20 74  rs and bits of t
2a84b 68 65 20 62 69 74 6d 61 73 6b 73 20 69 6e 20 57  he bitmasks in W
2a84c 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20  hereTerm..**.** 
2a84d 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
2a84e 6e 75 6d 62 65 72 73 20 61 72 65 20 73 6d 61 6c  numbers are smal
2a84f 6c 20 69 6e 74 65 67 65 72 73 20 63 6f 6e 74 61  l integers conta
2a850 69 6e 65 64 20 69 6e 20 0a 2a 2a 20 53 72 63 4c  ined in .** SrcL
2a851 69 73 74 5f 69 74 65 6d 2e 69 43 75 72 73 6f 72  ist_item.iCursor
2a852 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65   and Expr.iTable
2a853 20 66 69 65 6c 64 73 2e 20 20 46 6f 72 20 61 6e   fields.  For an
2a854 79 20 67 69 76 65 6e 20 57 48 45 52 45 20 0a 2a  y given WHERE .*
2a855 2a 20 63 6c 61 75 73 65 2c 20 74 68 65 20 63 75  * clause, the cu
2a856 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69 67  rsor numbers mig
2a857 68 74 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74  ht not begin wit
2a858 68 20 30 20 61 6e 64 20 74 68 65 79 20 6d 69 67  h 0 and they mig
2a859 68 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 67 61  ht.** contain ga
2a85a 70 73 20 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ps in the number
2a85b 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 42  ing sequence.  B
2a85c 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61  ut we want to ma
2a85d 6b 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 75 73  ke maximum.** us
2a85e 65 20 6f 66 20 74 68 65 20 62 69 74 73 20 69 6e  e of the bits in
2a85f 20 6f 75 72 20 62 69 74 6d 61 73 6b 73 2e 20 20   our bitmasks.  
2a860 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 70  This structure p
2a861 72 6f 76 69 64 65 73 20 61 20 6d 61 70 70 69 6e  rovides a mappin
2a862 67 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 70  g.** from the sp
2a863 61 72 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  arse cursor numb
2a864 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75  ers into consecu
2a865 74 69 76 65 20 69 6e 74 65 67 65 72 73 20 62 65  tive integers be
2a866 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20  ginning.** with 
2a867 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 57 68 65 72  0..**.** If Wher
2a868 65 4d 61 73 6b 53 65 74 2e 69 78 5b 41 5d 3d 3d  eMaskSet.ix[A]==
2a869 42 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  B it means that 
2a86a 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f 66 20  The A-th bit of 
2a86b 61 20 42 69 74 6d 61 73 6b 0a 2a 2a 20 63 6f 72  a Bitmask.** cor
2a86c 72 65 73 70 6f 6e 64 73 20 56 44 42 45 20 63 75  responds VDBE cu
2a86d 72 73 6f 72 20 6e 75 6d 62 65 72 20 42 2e 20 20  rsor number B.  
2a86e 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f 66 20  The A-th bit of 
2a86f 61 20 62 69 74 6d 61 73 6b 20 69 73 20 31 3c 3c  a bitmask is 1<<
2a870 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61  A..**.** For exa
2a871 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 57 48 45  mple, if the WHE
2a872 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 65 73  RE clause expres
2a873 73 69 6f 6e 20 75 73 65 64 20 74 68 65 73 65 20  sion used these 
2a874 56 44 42 45 0a 2a 2a 20 63 75 72 73 6f 72 73 3a  VDBE.** cursors:
2a875 20 20 34 2c 20 35 2c 20 38 2c 20 32 39 2c 20 35    4, 5, 8, 29, 5
2a876 37 2c 20 37 33 2e 20 20 54 68 65 6e 20 74 68 65  7, 73.  Then the
2a877 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 73    WhereMaskSet s
2a878 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 6f 75 6c  tructure.** woul
2a879 64 20 6d 61 70 20 74 68 6f 73 65 20 63 75 72 73  d map those curs
2a87a 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20  or numbers into 
2a87b 62 69 74 73 20 30 20 74 68 72 6f 75 67 68 20 35  bits 0 through 5
2a87c 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
2a87d 74 20 74 68 65 20 6d 61 70 70 69 6e 67 20 69 73  t the mapping is
2a87e 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
2a87f 20 6f 72 64 65 72 65 64 2e 20 20 49 6e 20 74 68   ordered.  In th
2a880 65 20 65 78 61 6d 70 6c 65 0a 2a 2a 20 61 62 6f  e example.** abo
2a881 76 65 2c 20 74 68 65 20 6d 61 70 70 69 6e 67 20  ve, the mapping 
2a882 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65 20 74 68  might go like th
2a883 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d 3e 31 2c  is:  4->3, 5->1,
2a884 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c 0a 2a 2a   8->2, 29->0,.**
2a885 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34 2e 20 20   57->5, 73->4.  
2a886 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39 20 6f 74  Or one of 719 ot
2a887 68 65 72 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  her combinations
2a888 20 6d 69 67 68 74 20 62 65 20 75 73 65 64 2e 20   might be used. 
2a889 49 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 72  It.** does not r
2a88a 65 61 6c 6c 79 20 6d 61 74 74 65 72 2e 20 20 57  eally matter.  W
2a88b 68 61 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  hat is important
2a88c 20 69 73 20 74 68 61 74 20 73 70 61 72 73 65 20   is that sparse 
2a88d 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72  cursor.** number
2a88e 73 20 61 6c 6c 20 67 65 74 20 6d 61 70 70 65 64  s all get mapped
2a88f 20 69 6e 74 6f 20 62 69 74 20 6e 75 6d 62 65 72   into bit number
2a890 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
2a891 68 20 30 20 61 6e 64 20 63 6f 6e 74 61 69 6e 0a  h 0 and contain.
2a892 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a 2a 2f 0a 73  ** no gaps..*/.s
2a893 74 72 75 63 74 20 57 68 65 72 65 4d 61 73 6b 53  truct WhereMaskS
2a894 65 74 20 7b 0a 20 20 69 6e 74 20 6e 3b 20 20 20  et {.  int n;   
2a895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a896 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2a897 66 20 61 73 73 69 67 6e 65 64 20 63 75 72 73 6f  f assigned curso
2a898 72 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e  r values */.  in
2a899 74 20 69 78 5b 42 4d 53 5d 3b 20 20 20 20 20 20  t ix[BMS];      
2a89a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2a89b 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74  ursor assigned t
2a89c 6f 20 65 61 63 68 20 62 69 74 20 2a 2f 0a 7d 3b  o each bit */.};
2a89d 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 43  ../*.** A WhereC
2a89e 6f 73 74 20 6f 62 6a 65 63 74 20 72 65 63 6f 72  ost object recor
2a89f 64 73 20 61 20 6c 6f 6f 6b 75 70 20 73 74 72 61  ds a lookup stra
2a8a0 74 65 67 79 20 61 6e 64 20 74 68 65 20 65 73 74  tegy and the est
2a8a1 69 6d 61 74 65 64 0a 2a 2a 20 63 6f 73 74 20 6f  imated.** cost o
2a8a2 66 20 70 75 72 73 75 69 6e 67 20 74 68 61 74 20  f pursuing that 
2a8a3 73 74 72 61 74 65 67 79 2e 0a 2a 2f 0a 73 74 72  strategy..*/.str
2a8a4 75 63 74 20 57 68 65 72 65 43 6f 73 74 20 7b 0a  uct WhereCost {.
2a8a5 20 20 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e    WherePlan plan
2a8a6 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b  ;    /* The look
2a8a7 75 70 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20  up strategy */. 
2a8a8 20 64 6f 75 62 6c 65 20 72 43 6f 73 74 3b 20 20   double rCost;  
2a8a9 20 20 20 20 2f 2a 20 4f 76 65 72 61 6c 6c 20 63      /* Overall c
2a8aa 6f 73 74 20 6f 66 20 70 75 72 73 75 69 6e 67 20  ost of pursuing 
2a8ab 74 68 69 73 20 73 65 61 72 63 68 20 73 74 72 61  this search stra
2a8ac 74 65 67 79 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  tegy */.  double
2a8ad 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 2f 2a 20   nRow;       /* 
2a8ae 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 72  Estimated number
2a8af 20 6f 66 20 6f 75 74 70 75 74 20 72 6f 77 73 20   of output rows 
2a8b0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74  */.};../*.** Bit
2a8b1 6d 61 73 6b 73 20 66 6f 72 20 74 68 65 20 6f 70  masks for the op
2a8b2 65 72 61 74 6f 72 73 20 74 68 61 74 20 69 6e 64  erators that ind
2a8b3 69 63 65 73 20 61 72 65 20 61 62 6c 65 20 74 6f  ices are able to
2a8b4 20 65 78 70 6c 6f 69 74 2e 20 20 41 6e 0a 2a 2a   exploit.  An.**
2a8b5 20 4f 52 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69   OR-ed combinati
2a8b6 6f 6e 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75  on of these valu
2a8b7 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 77  es can be used w
2a8b8 68 65 6e 20 73 65 61 72 63 68 69 6e 67 20 66 6f  hen searching fo
2a8b9 72 0a 2a 2a 20 74 65 72 6d 73 20 69 6e 20 74 68  r.** terms in th
2a8ba 65 20 77 68 65 72 65 20 63 6c 61 75 73 65 2e 0a  e where clause..
2a8bb 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 49 4e  */.#define WO_IN
2a8bc 20 20 20 20 20 30 78 30 30 31 0a 23 64 65 66 69       0x001.#defi
2a8bd 6e 65 20 57 4f 5f 45 51 20 20 20 20 20 30 78 30  ne WO_EQ     0x0
2a8be 30 32 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c 54  02.#define WO_LT
2a8bf 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b       (WO_EQ<<(TK
2a8c0 5f 4c 54 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66  _LT-TK_EQ)).#def
2a8c1 69 6e 65 20 57 4f 5f 4c 45 20 20 20 20 20 28 57  ine WO_LE     (W
2a8c2 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 45 2d 54 4b 5f  O_EQ<<(TK_LE-TK_
2a8c3 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f  EQ)).#define WO_
2a8c4 47 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28  GT     (WO_EQ<<(
2a8c5 54 4b 5f 47 54 2d 54 4b 5f 45 51 29 29 0a 23 64  TK_GT-TK_EQ)).#d
2a8c6 65 66 69 6e 65 20 57 4f 5f 47 45 20 20 20 20 20  efine WO_GE     
2a8c7 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 45 2d 54  (WO_EQ<<(TK_GE-T
2a8c8 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57  K_EQ)).#define W
2a8c9 4f 5f 4d 41 54 43 48 20 20 30 78 30 34 30 0a 23  O_MATCH  0x040.#
2a8ca 64 65 66 69 6e 65 20 57 4f 5f 49 53 4e 55 4c 4c  define WO_ISNULL
2a8cb 20 30 78 30 38 30 0a 23 64 65 66 69 6e 65 20 57   0x080.#define W
2a8cc 4f 5f 4f 52 20 20 20 20 20 30 78 31 30 30 20 20  O_OR     0x100  
2a8cd 20 20 20 20 20 2f 2a 20 54 77 6f 20 6f 72 20 6d       /* Two or m
2a8ce 6f 72 65 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64  ore OR-connected
2a8cf 20 74 65 72 6d 73 20 2a 2f 0a 23 64 65 66 69 6e   terms */.#defin
2a8d0 65 20 57 4f 5f 41 4e 44 20 20 20 20 30 78 32 30  e WO_AND    0x20
2a8d1 30 20 20 20 20 20 20 20 2f 2a 20 54 77 6f 20 6f  0       /* Two o
2a8d2 72 20 6d 6f 72 65 20 41 4e 44 2d 63 6f 6e 6e 65  r more AND-conne
2a8d3 63 74 65 64 20 74 65 72 6d 73 20 2a 2f 0a 0a 23  cted terms */..#
2a8d4 64 65 66 69 6e 65 20 57 4f 5f 41 4c 4c 20 20 20  define WO_ALL   
2a8d5 20 30 78 66 66 66 20 20 20 20 20 20 20 2f 2a 20   0xfff       /* 
2a8d6 4d 61 73 6b 20 6f 66 20 61 6c 6c 20 70 6f 73 73  Mask of all poss
2a8d7 69 62 6c 65 20 57 4f 5f 2a 20 76 61 6c 75 65 73  ible WO_* values
2a8d8 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 53   */.#define WO_S
2a8d9 49 4e 47 4c 45 20 30 78 30 66 66 20 20 20 20 20  INGLE 0x0ff     
2a8da 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c 6c    /* Mask of all
2a8db 20 6e 6f 6e 2d 63 6f 6d 70 6f 75 6e 64 20 57 4f   non-compound WO
2a8dc 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 0a 2f 2a  _* values */../*
2a8dd 0a 2a 2a 20 56 61 6c 75 65 20 66 6f 72 20 77 73  .** Value for ws
2a8de 46 6c 61 67 73 20 72 65 74 75 72 6e 65 64 20 62  Flags returned b
2a8df 79 20 62 65 73 74 49 6e 64 65 78 28 29 20 61 6e  y bestIndex() an
2a8e0 64 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 57  d stored in.** W
2a8e1 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67  hereLevel.wsFlag
2a8e2 73 2e 20 20 54 68 65 73 65 20 66 6c 61 67 73 20  s.  These flags 
2a8e3 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 20  determine which 
2a8e4 73 65 61 72 63 68 0a 2a 2a 20 73 74 72 61 74 65  search.** strate
2a8e5 67 69 65 73 20 61 72 65 20 61 70 70 72 6f 70 72  gies are appropr
2a8e6 69 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  iate..**.** The 
2a8e7 6c 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e  least significan
2a8e8 74 20 31 32 20 62 69 74 73 20 69 73 20 72 65 73  t 12 bits is res
2a8e9 65 72 76 65 64 20 61 73 20 61 20 6d 61 73 6b 20  erved as a mask 
2a8ea 66 6f 72 20 57 4f 5f 20 76 61 6c 75 65 73 20 61  for WO_ values a
2a8eb 62 6f 76 65 2e 0a 2a 2a 20 54 68 65 20 57 68 65  bove..** The Whe
2a8ec 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 20  reLevel.wsFlags 
2a8ed 66 69 65 6c 64 20 69 73 20 75 73 75 61 6c 6c 79  field is usually
2a8ee 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f   set to WO_IN|WO
2a8ef 5f 45 51 7c 57 4f 5f 49 53 4e 55 4c 4c 2e 0a 2a  _EQ|WO_ISNULL..*
2a8f0 2a 20 42 75 74 20 69 66 20 74 68 65 20 74 61 62  * But if the tab
2a8f1 6c 65 20 69 73 20 74 68 65 20 72 69 67 68 74 20  le is the right 
2a8f2 74 61 62 6c 65 20 6f 66 20 61 20 6c 65 66 74 20  table of a left 
2a8f3 6a 6f 69 6e 2c 20 57 68 65 72 65 4c 65 76 65 6c  join, WhereLevel
2a8f4 2e 77 73 46 6c 61 67 73 0a 2a 2a 20 69 73 20 73  .wsFlags.** is s
2a8f5 65 74 20 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45  et to WO_IN|WO_E
2a8f6 51 2e 20 20 54 68 65 20 57 68 65 72 65 4c 65 76  Q.  The WhereLev
2a8f7 65 6c 2e 77 73 46 6c 61 67 73 20 66 69 65 6c 64  el.wsFlags field
2a8f8 20 63 61 6e 20 74 68 65 6e 20 62 65 20 75 73 65   can then be use
2a8f9 64 20 61 73 0a 2a 2a 20 74 68 65 20 22 6f 70 22  d as.** the "op"
2a8fa 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 66 69   parameter to fi
2a8fb 6e 64 54 65 72 6d 20 77 68 65 6e 20 77 65 20 61  ndTerm when we a
2a8fc 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 65 71 75  re resolving equ
2a8fd 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2a8fe 73 2e 0a 2a 2a 20 49 53 4e 55 4c 4c 20 63 6f 6e  s..** ISNULL con
2a8ff 73 74 72 61 69 6e 74 73 20 77 69 6c 6c 20 74 68  straints will th
2a900 65 6e 20 6e 6f 74 20 62 65 20 75 73 65 64 20 6f  en not be used o
2a901 6e 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  n the right tabl
2a902 65 20 6f 66 20 61 20 6c 65 66 74 0a 2a 2a 20 6a  e of a left.** j
2a903 6f 69 6e 2e 20 20 54 69 63 6b 65 74 73 20 23 32  oin.  Tickets #2
2a904 31 37 37 20 61 6e 64 20 23 32 31 38 39 2e 0a 2a  177 and #2189..*
2a905 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2a906 52 4f 57 49 44 5f 45 51 20 20 20 20 20 30 78 30  ROWID_EQ     0x0
2a907 30 30 30 31 30 30 30 20 20 2f 2a 20 72 6f 77 69  0001000  /* rowi
2a908 64 3d 45 58 50 52 20 6f 72 20 72 6f 77 69 64 20  d=EXPR or rowid 
2a909 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66  IN (...) */.#def
2a90a 69 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f  ine WHERE_ROWID_
2a90b 52 41 4e 47 45 20 20 30 78 30 30 30 30 32 30 30  RANGE  0x0000200
2a90c 30 20 20 2f 2a 20 72 6f 77 69 64 3c 45 58 50 52  0  /* rowid<EXPR
2a90d 20 61 6e 64 2f 6f 72 20 72 6f 77 69 64 3e 45 58   and/or rowid>EX
2a90e 50 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  PR */.#define WH
2a90f 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 20 20 20  ERE_COLUMN_EQ   
2a910 20 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20   0x00010000  /* 
2a911 78 3d 45 58 50 52 20 6f 72 20 78 20 49 4e 20 28  x=EXPR or x IN (
2a912 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ...) */.#define 
2a913 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e  WHERE_COLUMN_RAN
2a914 47 45 20 30 78 30 30 30 32 30 30 30 30 20 20 2f  GE 0x00020000  /
2a915 2a 20 78 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20  * x<EXPR and/or 
2a916 78 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66 69 6e  x>EXPR */.#defin
2a917 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49  e WHERE_COLUMN_I
2a918 4e 20 20 20 20 30 78 30 30 30 34 30 30 30 30 20  N    0x00040000 
2a919 20 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e 29 20 2a   /* x IN (...) *
2a91a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2a91b 49 4e 44 45 58 45 44 20 20 20 20 20 20 30 78 30  INDEXED      0x0
2a91c 30 30 37 30 30 30 30 20 20 2f 2a 20 41 6e 79 74  0070000  /* Anyt
2a91d 68 69 6e 67 20 74 68 61 74 20 75 73 65 73 20 61  hing that uses a
2a91e 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69  n index */.#defi
2a91f 6e 65 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45  ne WHERE_IN_ABLE
2a920 20 20 20 20 20 20 30 78 30 30 30 37 31 30 30 30        0x00071000
2a921 20 20 2f 2a 20 41 62 6c 65 20 74 6f 20 73 75 70    /* Able to sup
2a922 70 6f 72 74 20 61 6e 20 49 4e 20 6f 70 65 72 61  port an IN opera
2a923 74 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  tor */.#define W
2a924 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 20  HERE_TOP_LIMIT  
2a925 20 20 30 78 30 30 31 30 30 30 30 30 20 20 2f 2a    0x00100000  /*
2a926 20 78 3c 45 58 50 52 20 6f 72 20 78 3c 3d 45 58   x<EXPR or x<=EX
2a927 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f  PR constraint */
2a928 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 42  .#define WHERE_B
2a929 54 4d 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 30  TM_LIMIT    0x00
2a92a 32 30 30 30 30 30 20 20 2f 2a 20 78 3e 45 58 50  200000  /* x>EXP
2a92b 52 20 6f 72 20 78 3e 3d 45 58 50 52 20 63 6f 6e  R or x>=EXPR con
2a92c 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69  straint */.#defi
2a92d 6e 65 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c  ne WHERE_IDX_ONL
2a92e 59 20 20 20 20 20 30 78 30 30 38 30 30 30 30 30  Y     0x00800000
2a92f 20 20 2f 2a 20 55 73 65 20 69 6e 64 65 78 20 6f    /* Use index o
2a930 6e 6c 79 20 2d 20 6f 6d 69 74 20 74 61 62 6c 65  nly - omit table
2a931 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
2a932 45 5f 4f 52 44 45 52 42 59 20 20 20 20 20 20 30  E_ORDERBY      0
2a933 78 30 31 30 30 30 30 30 30 20 20 2f 2a 20 4f 75  x01000000  /* Ou
2a934 74 70 75 74 20 77 69 6c 6c 20 61 70 70 65 61 72  tput will appear
2a935 20 69 6e 20 63 6f 72 72 65 63 74 20 6f 72 64 65   in correct orde
2a936 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  r */.#define WHE
2a937 52 45 5f 52 45 56 45 52 53 45 20 20 20 20 20 20  RE_REVERSE      
2a938 30 78 30 32 30 30 30 30 30 30 20 20 2f 2a 20 53  0x02000000  /* S
2a939 63 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f  can in reverse o
2a93a 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rder */.#define 
2a93b 57 48 45 52 45 5f 55 4e 49 51 55 45 20 20 20 20  WHERE_UNIQUE    
2a93c 20 20 20 30 78 30 34 30 30 30 30 30 30 20 20 2f     0x04000000  /
2a93d 2a 20 53 65 6c 65 63 74 73 20 6e 6f 20 6d 6f 72  * Selects no mor
2a93e 65 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 2a  e than one row *
2a93f 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2a940 56 49 52 54 55 41 4c 54 41 42 4c 45 20 30 78 30  VIRTUALTABLE 0x0
2a941 38 30 30 30 30 30 30 20 20 2f 2a 20 55 73 65 20  8000000  /* Use 
2a942 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 70 72  virtual-table pr
2a943 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 23 64 65 66  ocessing */.#def
2a944 69 6e 65 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f  ine WHERE_MULTI_
2a945 4f 52 20 20 20 20 20 30 78 31 30 30 30 30 30 30  OR     0x1000000
2a946 30 20 20 2f 2a 20 4f 52 20 75 73 69 6e 67 20 6d  0  /* OR using m
2a947 75 6c 74 69 70 6c 65 20 69 6e 64 69 63 65 73 20  ultiple indices 
2a948 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  */../*.** Initia
2a949 6c 69 7a 65 20 61 20 70 72 65 61 6c 6c 6f 63 61  lize a prealloca
2a94a 74 65 64 20 57 68 65 72 65 43 6c 61 75 73 65 20  ted WhereClause 
2a94b 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
2a94c 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43  atic void whereC
2a94d 6c 61 75 73 65 49 6e 69 74 28 0a 20 20 57 68 65  lauseInit(.  Whe
2a94e 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
2a94f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 68 65        /* The Whe
2a950 72 65 43 6c 61 75 73 65 20 74 6f 20 62 65 20 69  reClause to be i
2a951 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
2a952 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2a953 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2a954 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
2a955 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65  */.  WhereMaskSe
2a956 74 20 2a 70 4d 61 73 6b 53 65 74 20 20 20 2f 2a  t *pMaskSet   /*
2a957 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61   Mapping from ta
2a958 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ble cursor numbe
2a959 72 73 20 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a  rs to bitmasks *
2a95a 2f 0a 29 7b 0a 20 20 70 57 43 2d 3e 70 50 61 72  /.){.  pWC->pPar
2a95b 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70  se = pParse;.  p
2a95c 57 43 2d 3e 70 4d 61 73 6b 53 65 74 20 3d 20 70  WC->pMaskSet = p
2a95d 4d 61 73 6b 53 65 74 3b 0a 20 20 70 57 43 2d 3e  MaskSet;.  pWC->
2a95e 6e 54 65 72 6d 20 3d 20 30 3b 0a 20 20 70 57 43  nTerm = 0;.  pWC
2a95f 2d 3e 6e 53 6c 6f 74 20 3d 20 41 72 72 61 79 53  ->nSlot = ArrayS
2a960 69 7a 65 28 70 57 43 2d 3e 61 53 74 61 74 69 63  ize(pWC->aStatic
2a961 29 3b 0a 20 20 70 57 43 2d 3e 61 20 3d 20 70 57  );.  pWC->a = pW
2a962 43 2d 3e 61 53 74 61 74 69 63 3b 0a 7d 0a 0a 2f  C->aStatic;.}../
2a963 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65  * Forward refere
2a964 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nce */.static vo
2a965 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c  id whereClauseCl
2a966 65 61 72 28 57 68 65 72 65 43 6c 61 75 73 65 2a  ear(WhereClause*
2a967 29 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f  );../*.** Deallo
2a968 63 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  cate all memory 
2a969 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2a96a 61 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 6f 62  a WhereOrInfo ob
2a96b 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ject..*/.static 
2a96c 76 6f 69 64 20 77 68 65 72 65 4f 72 49 6e 66 6f  void whereOrInfo
2a96d 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a  Delete(sqlite3 *
2a96e 64 62 2c 20 57 68 65 72 65 4f 72 49 6e 66 6f 20  db, WhereOrInfo 
2a96f 2a 70 29 7b 0a 20 20 77 68 65 72 65 43 6c 61 75  *p){.  whereClau
2a970 73 65 43 6c 65 61 72 28 26 70 2d 3e 77 63 29 3b  seClear(&p->wc);
2a971 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2a972 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (db, p);.}../*.*
2a973 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c  * Deallocate all
2a974 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74   memory associat
2a975 65 64 20 77 69 74 68 20 61 20 57 68 65 72 65 41  ed with a WhereA
2a976 6e 64 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a  ndInfo object..*
2a977 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68  /.static void wh
2a978 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74 65  ereAndInfoDelete
2a979 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68  (sqlite3 *db, Wh
2a97a 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 29 7b 0a  ereAndInfo *p){.
2a97b 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65    whereClauseCle
2a97c 61 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71  ar(&p->wc);.  sq
2a97d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2a97e 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61  p);.}../*.** Dea
2a97f 6c 6c 6f 63 61 74 65 20 61 20 57 68 65 72 65 43  llocate a WhereC
2a980 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65 2e  lause structure.
2a981 20 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73    The WhereClaus
2a982 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69  e structure.** i
2a983 74 73 65 6c 66 20 69 73 20 6e 6f 74 20 66 72 65  tself is not fre
2a984 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ed.  This routin
2a985 65 20 69 73 20 74 68 65 20 69 6e 76 65 72 73 65  e is the inverse
2a986 20 6f 66 20 77 68 65 72 65 43 6c 61 75 73 65 49   of whereClauseI
2a987 6e 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  nit()..*/.static
2a988 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73   void whereClaus
2a989 65 43 6c 65 61 72 28 57 68 65 72 65 43 6c 61 75  eClear(WhereClau
2a98a 73 65 20 2a 70 57 43 29 7b 0a 20 20 69 6e 74 20  se *pWC){.  int 
2a98b 69 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  i;.  WhereTerm *
2a98c 61 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  a;.  sqlite3 *db
2a98d 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e   = pWC->pParse->
2a98e 64 62 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d  db;.  for(i=pWC-
2a98f 3e 6e 54 65 72 6d 2d 31 2c 20 61 3d 70 57 43 2d  >nTerm-1, a=pWC-
2a990 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61  >a; i>=0; i--, a
2a991 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 2d 3e  ++){.    if( a->
2a992 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 44  wtFlags & TERM_D
2a993 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20  YNAMIC ){.      
2a994 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2a995 65 28 64 62 2c 20 61 2d 3e 70 45 78 70 72 29 3b  e(db, a->pExpr);
2a996 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 61  .    }.    if( a
2a997 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
2a998 5f 4f 52 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20  _ORINFO ){.     
2a999 20 77 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65   whereOrInfoDele
2a99a 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70 4f 72 49  te(db, a->u.pOrI
2a99b 6e 66 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  nfo);.    }else 
2a99c 69 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 20 26  if( a->wtFlags &
2a99d 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 20 29 7b   TERM_ANDINFO ){
2a99e 0a 20 20 20 20 20 20 77 68 65 72 65 41 6e 64 49  .      whereAndI
2a99f 6e 66 6f 44 65 6c 65 74 65 28 64 62 2c 20 61 2d  nfoDelete(db, a-
2a9a0 3e 75 2e 70 41 6e 64 49 6e 66 6f 29 3b 0a 20 20  >u.pAndInfo);.  
2a9a1 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 57    }.  }.  if( pW
2a9a2 43 2d 3e 61 21 3d 70 57 43 2d 3e 61 53 74 61 74  C->a!=pWC->aStat
2a9a3 69 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ic ){.    sqlite
2a9a4 33 44 62 46 72 65 65 28 64 62 2c 20 70 57 43 2d  3DbFree(db, pWC-
2a9a5 3e 61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  >a);.  }.}../*.*
2a9a6 2a 20 41 64 64 20 61 20 73 69 6e 67 6c 65 20 6e  * Add a single n
2a9a7 65 77 20 57 68 65 72 65 54 65 72 6d 20 65 6e 74  ew WhereTerm ent
2a9a8 72 79 20 74 6f 20 74 68 65 20 57 68 65 72 65 43  ry to the WhereC
2a9a9 6c 61 75 73 65 20 6f 62 6a 65 63 74 20 70 57 43  lause object pWC
2a9aa 2e 0a 2a 2a 20 54 68 65 20 6e 65 77 20 57 68 65  ..** The new Whe
2a9ab 72 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 69 73  reTerm object is
2a9ac 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f   constructed fro
2a9ad 6d 20 45 78 70 72 20 70 20 61 6e 64 20 77 69 74  m Expr p and wit
2a9ae 68 20 77 74 46 6c 61 67 73 2e 0a 2a 2a 20 54 68  h wtFlags..** Th
2a9af 65 20 69 6e 64 65 78 20 69 6e 20 70 57 43 2d 3e  e index in pWC->
2a9b0 61 5b 5d 20 6f 66 20 74 68 65 20 6e 65 77 20 57  a[] of the new W
2a9b1 68 65 72 65 54 65 72 6d 20 69 73 20 72 65 74 75  hereTerm is retu
2a9b2 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e  rned on success.
2a9b3 0a 2a 2a 20 30 20 69 73 20 72 65 74 75 72 6e 65  .** 0 is returne
2a9b4 64 20 69 66 20 74 68 65 20 6e 65 77 20 57 68 65  d if the new Whe
2a9b5 72 65 54 65 72 6d 20 63 6f 75 6c 64 20 6e 6f 74  reTerm could not
2a9b6 20 62 65 20 61 64 64 65 64 20 64 75 65 20 74 6f   be added due to
2a9b7 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c   a memory.** all
2a9b8 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 20 20  ocation error.  
2a9b9 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  The memory alloc
2a9ba 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 77 69  ation failure wi
2a9bb 6c 6c 20 62 65 20 72 65 63 6f 72 64 65 64 20 69  ll be recorded i
2a9bc 6e 0a 2a 2a 20 74 68 65 20 64 62 2d 3e 6d 61 6c  n.** the db->mal
2a9bd 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 73  locFailed flag s
2a9be 6f 20 74 68 61 74 20 68 69 67 68 65 72 2d 6c 65  o that higher-le
2a9bf 76 65 6c 20 66 75 6e 63 74 69 6f 6e 73 20 63 61  vel functions ca
2a9c0 6e 20 64 65 74 65 63 74 20 69 74 2e 0a 2a 2a 0a  n detect it..**.
2a9c1 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2a9c2 77 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68  will increase th
2a9c3 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 57  e size of the pW
2a9c4 43 2d 3e 61 5b 5d 20 61 72 72 61 79 20 61 73 20  C->a[] array as 
2a9c5 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a  necessary..**.**
2a9c6 20 49 66 20 74 68 65 20 77 74 46 6c 61 67 73 20   If the wtFlags 
2a9c7 61 72 67 75 6d 65 6e 74 20 69 6e 63 6c 75 64 65  argument include
2a9c8 73 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 2c 20  s TERM_DYNAMIC, 
2a9c9 74 68 65 6e 20 72 65 73 70 6f 6e 73 69 62 69 6c  then responsibil
2a9ca 69 74 79 0a 2a 2a 20 66 6f 72 20 66 72 65 65 69  ity.** for freei
2a9cb 6e 67 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  ng the expressio
2a9cc 6e 20 70 20 69 73 20 61 73 73 75 6d 65 64 20 62  n p is assumed b
2a9cd 79 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73  y the WhereClaus
2a9ce 65 20 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a  e object pWC..**
2a9cf 20 54 68 69 73 20 69 73 20 74 72 75 65 20 65 76   This is true ev
2a9d0 65 6e 20 69 66 20 74 68 69 73 20 72 6f 75 74 69  en if this routi
2a9d1 6e 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f  ne fails to allo
2a9d2 63 61 74 65 20 61 20 6e 65 77 20 57 68 65 72 65  cate a new Where
2a9d3 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e  Term..**.** WARN
2a9d4 49 4e 47 3a 20 20 54 68 69 73 20 72 6f 75 74 69  ING:  This routi
2a9d5 6e 65 20 6d 69 67 68 74 20 72 65 61 6c 6c 6f 63  ne might realloc
2a9d6 61 74 65 20 74 68 65 20 73 70 61 63 65 20 75 73  ate the space us
2a9d7 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 57  ed to store.** W
2a9d8 68 65 72 65 54 65 72 6d 73 2e 20 20 41 6c 6c 20  hereTerms.  All 
2a9d9 70 6f 69 6e 74 65 72 73 20 74 6f 20 57 68 65 72  pointers to Wher
2a9da 65 54 65 72 6d 73 20 73 68 6f 75 6c 64 20 62 65  eTerms should be
2a9db 20 69 6e 76 61 6c 69 64 61 74 65 64 20 61 66 74   invalidated aft
2a9dc 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68  er.** calling th
2a9dd 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 53 75 63  is routine.  Suc
2a9de 68 20 70 6f 69 6e 74 65 72 73 20 6d 61 79 20 62  h pointers may b
2a9df 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20  e reinitialized 
2a9e0 62 79 20 72 65 66 65 72 65 6e 63 69 6e 67 0a 2a  by referencing.*
2a9e1 2a 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20 61  * the pWC->a[] a
2a9e2 72 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rray..*/.static 
2a9e3 69 6e 74 20 77 68 65 72 65 43 6c 61 75 73 65 49  int whereClauseI
2a9e4 6e 73 65 72 74 28 57 68 65 72 65 43 6c 61 75 73  nsert(WhereClaus
2a9e5 65 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 2c  e *pWC, Expr *p,
2a9e6 20 75 38 20 77 74 46 6c 61 67 73 29 7b 0a 20 20   u8 wtFlags){.  
2a9e7 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
2a9e8 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 69  ;.  int idx;.  i
2a9e9 66 28 20 70 57 43 2d 3e 6e 54 65 72 6d 3e 3d 70  f( pWC->nTerm>=p
2a9ea 57 43 2d 3e 6e 53 6c 6f 74 20 29 7b 0a 20 20 20  WC->nSlot ){.   
2a9eb 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 6c 64   WhereTerm *pOld
2a9ec 20 3d 20 70 57 43 2d 3e 61 3b 0a 20 20 20 20 73   = pWC->a;.    s
2a9ed 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 57 43  qlite3 *db = pWC
2a9ee 2d 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  ->pParse->db;.  
2a9ef 20 20 70 57 43 2d 3e 61 20 3d 20 73 71 6c 69 74    pWC->a = sqlit
2a9f0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
2a9f1 2c 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b  , sizeof(pWC->a[
2a9f2 30 5d 29 2a 70 57 43 2d 3e 6e 53 6c 6f 74 2a 32  0])*pWC->nSlot*2
2a9f3 20 29 3b 0a 20 20 20 20 69 66 28 20 70 57 43 2d   );.    if( pWC-
2a9f4 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  >a==0 ){.      i
2a9f5 66 28 20 77 74 46 6c 61 67 73 20 26 20 54 45 52  f( wtFlags & TER
2a9f6 4d 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20  M_DYNAMIC ){.   
2a9f7 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2a9f8 44 65 6c 65 74 65 28 64 62 2c 20 70 29 3b 0a 20  Delete(db, p);. 
2a9f9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 57 43       }.      pWC
2a9fa 2d 3e 61 20 3d 20 70 4f 6c 64 3b 0a 20 20 20 20  ->a = pOld;.    
2a9fb 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
2a9fc 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 43  }.    memcpy(pWC
2a9fd 2d 3e 61 2c 20 70 4f 6c 64 2c 20 73 69 7a 65 6f  ->a, pOld, sizeo
2a9fe 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43  f(pWC->a[0])*pWC
2a9ff 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69 66  ->nTerm);.    if
2aa00 28 20 70 4f 6c 64 21 3d 70 57 43 2d 3e 61 53 74  ( pOld!=pWC->aSt
2aa01 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 73 71  atic ){.      sq
2aa02 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2aa03 70 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pOld);.    }.   
2aa04 20 70 57 43 2d 3e 6e 53 6c 6f 74 20 3d 20 73 71   pWC->nSlot = sq
2aa05 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
2aa06 65 28 64 62 2c 20 70 57 43 2d 3e 61 29 2f 73 69  e(db, pWC->a)/si
2aa07 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 3b  zeof(pWC->a[0]);
2aa08 0a 20 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26  .  }.  pTerm = &
2aa09 70 57 43 2d 3e 61 5b 69 64 78 20 3d 20 70 57 43  pWC->a[idx = pWC
2aa0a 2d 3e 6e 54 65 72 6d 2b 2b 5d 3b 0a 20 20 70 54  ->nTerm++];.  pT
2aa0b 65 72 6d 2d 3e 70 45 78 70 72 20 3d 20 70 3b 0a  erm->pExpr = p;.
2aa0c 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73    pTerm->wtFlags
2aa0d 20 3d 20 77 74 46 6c 61 67 73 3b 0a 20 20 70 54   = wtFlags;.  pT
2aa0e 65 72 6d 2d 3e 70 57 43 20 3d 20 70 57 43 3b 0a  erm->pWC = pWC;.
2aa0f 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74    pTerm->iParent
2aa10 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20   = -1;.  return 
2aa11 69 64 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  idx;.}../*.** Th
2aa12 69 73 20 72 6f 75 74 69 6e 65 20 69 64 65 6e 74  is routine ident
2aa13 69 66 69 65 73 20 73 75 62 65 78 70 72 65 73 73  ifies subexpress
2aa14 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52  ions in the WHER
2aa15 45 20 63 6c 61 75 73 65 20 77 68 65 72 65 0a 2a  E clause where.*
2aa16 2a 20 65 61 63 68 20 73 75 62 65 78 70 72 65 73  * each subexpres
2aa17 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65  sion is separate
2aa18 64 20 62 79 20 74 68 65 20 41 4e 44 20 6f 70 65  d by the AND ope
2aa19 72 61 74 6f 72 20 6f 72 20 73 6f 6d 65 20 6f 74  rator or some ot
2aa1a 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  her.** operator 
2aa1b 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
2aa1c 20 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 20 20   op parameter.  
2aa1d 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  The WhereClause 
2aa1e 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20  structure.** is 
2aa1f 66 69 6c 6c 65 64 20 77 69 74 68 20 70 6f 69 6e  filled with poin
2aa20 74 65 72 73 20 74 6f 20 73 75 62 65 78 70 72 65  ters to subexpre
2aa21 73 73 69 6f 6e 73 2e 20 20 46 6f 72 20 65 78 61  ssions.  For exa
2aa22 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 57  mple:.**.**    W
2aa23 48 45 52 45 20 20 61 3d 3d 27 68 65 6c 6c 6f 27  HERE  a=='hello'
2aa24 20 41 4e 44 20 63 6f 61 6c 65 73 63 65 28 62 2c   AND coalesce(b,
2aa25 31 31 29 3c 31 30 20 41 4e 44 20 28 63 2b 31 32  11)<10 AND (c+12
2aa26 21 3d 64 20 4f 52 20 63 3d 3d 32 32 29 0a 2a 2a  !=d OR c==22).**
2aa27 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f             \____
2aa28 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f  ____/     \_____
2aa29 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20  __________/     
2aa2a 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  \_______________
2aa2b 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  _/.**           
2aa2c 20 73 6c 6f 74 5b 30 5d 20 20 20 20 20 20 20 20   slot[0]        
2aa2d 20 20 20 20 73 6c 6f 74 5b 31 5d 20 20 20 20 20      slot[1]     
2aa2e 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 32            slot[2
2aa2f 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67  ].**.** The orig
2aa30 69 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73  inal WHERE claus
2aa31 65 20 69 6e 20 70 45 78 70 72 20 69 73 20 75 6e  e in pExpr is un
2aa32 61 6c 74 65 72 65 64 2e 20 20 41 6c 6c 20 74 68  altered.  All th
2aa33 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 6f  is routine.** do
2aa34 65 73 20 69 73 20 6d 61 6b 65 20 73 6c 6f 74 5b  es is make slot[
2aa35 5d 20 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 20  ] entries point 
2aa36 74 6f 20 73 75 62 73 74 72 75 63 74 75 72 65 20  to substructure 
2aa37 77 69 74 68 69 6e 20 70 45 78 70 72 2e 0a 2a 2a  within pExpr..**
2aa38 0a 2a 2a 20 49 6e 20 74 68 65 20 70 72 65 76 69  .** In the previ
2aa39 6f 75 73 20 73 65 6e 74 65 6e 63 65 20 61 6e 64  ous sentence and
2aa3a 20 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d 2c   in the diagram,
2aa3b 20 22 73 6c 6f 74 5b 5d 22 20 72 65 66 65 72 73   "slot[]" refers
2aa3c 20 74 6f 0a 2a 2a 20 74 68 65 20 57 68 65 72 65   to.** the Where
2aa3d 43 6c 61 75 73 65 2e 61 5b 5d 20 61 72 72 61 79  Clause.a[] array
2aa3e 2e 20 20 54 68 65 20 73 6c 6f 74 5b 5d 20 61 72  .  The slot[] ar
2aa3f 72 61 79 20 67 72 6f 77 73 20 61 73 20 6e 65 65  ray grows as nee
2aa40 64 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 2a  ded to contain.*
2aa41 2a 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74  * all terms of t
2aa42 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2aa43 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2aa44 77 68 65 72 65 53 70 6c 69 74 28 57 68 65 72 65  whereSplit(Where
2aa45 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 78 70  Clause *pWC, Exp
2aa46 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6f 70  r *pExpr, int op
2aa47 29 7b 0a 20 20 70 57 43 2d 3e 6f 70 20 3d 20 28  ){.  pWC->op = (
2aa48 75 38 29 6f 70 3b 0a 20 20 69 66 28 20 70 45 78  u8)op;.  if( pEx
2aa49 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  pr==0 ) return;.
2aa4a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21    if( pExpr->op!
2aa4b 3d 6f 70 20 29 7b 0a 20 20 20 20 77 68 65 72 65  =op ){.    where
2aa4c 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
2aa4d 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d  , pExpr, 0);.  }
2aa4e 65 6c 73 65 7b 0a 20 20 20 20 77 68 65 72 65 53  else{.    whereS
2aa4f 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d  plit(pWC, pExpr-
2aa50 3e 70 4c 65 66 74 2c 20 6f 70 29 3b 0a 20 20 20  >pLeft, op);.   
2aa51 20 77 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c   whereSplit(pWC,
2aa52 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
2aa53 6f 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  op);.  }.}../*.*
2aa54 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 20  * Initialize an 
2aa55 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20  expression mask 
2aa56 73 65 74 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69  set.*/.#define i
2aa57 6e 69 74 4d 61 73 6b 53 65 74 28 50 29 20 20 6d  nitMaskSet(P)  m
2aa58 65 6d 73 65 74 28 50 2c 20 30 2c 20 73 69 7a 65  emset(P, 0, size
2aa59 6f 66 28 2a 50 29 29 0a 0a 2f 2a 0a 2a 2a 20 52  of(*P))../*.** R
2aa5a 65 74 75 72 6e 20 74 68 65 20 62 69 74 6d 61 73  eturn the bitmas
2aa5b 6b 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  k for the given 
2aa5c 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e 20 20  cursor number.  
2aa5d 52 65 74 75 72 6e 20 30 20 69 66 0a 2a 2a 20 69  Return 0 if.** i
2aa5e 43 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 69 6e  Cursor is not in
2aa5f 20 74 68 65 20 73 65 74 2e 0a 2a 2f 0a 73 74 61   the set..*/.sta
2aa60 74 69 63 20 42 69 74 6d 61 73 6b 20 67 65 74 4d  tic Bitmask getM
2aa61 61 73 6b 28 57 68 65 72 65 4d 61 73 6b 53 65 74  ask(WhereMaskSet
2aa62 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e 74 20   *pMaskSet, int 
2aa63 69 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74 20  iCursor){.  int 
2aa64 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
2aa65 70 4d 61 73 6b 53 65 74 2d 3e 6e 3b 20 69 2b 2b  pMaskSet->n; i++
2aa66 29 7b 0a 20 20 20 20 69 66 28 20 70 4d 61 73 6b  ){.    if( pMask
2aa67 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d 69 43 75 72  Set->ix[i]==iCur
2aa68 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 72 65 74  sor ){.      ret
2aa69 75 72 6e 20 28 28 42 69 74 6d 61 73 6b 29 31 29  urn ((Bitmask)1)
2aa6a 3c 3c 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  <<i;.    }.  }. 
2aa6b 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
2aa6c 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
2aa6d 20 6d 61 73 6b 20 66 6f 72 20 63 75 72 73 6f 72   mask for cursor
2aa6e 20 69 43 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20   iCursor..**.** 
2aa6f 54 68 65 72 65 20 69 73 20 6f 6e 65 20 63 75 72  There is one cur
2aa70 73 6f 72 20 70 65 72 20 74 61 62 6c 65 20 69 6e  sor per table in
2aa71 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2aa72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  .  The number of
2aa73 0a 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68  .** tables in th
2aa74 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
2aa75 20 6c 69 6d 69 74 65 64 20 62 79 20 61 20 74 65   limited by a te
2aa76 73 74 20 65 61 72 6c 79 20 69 6e 20 74 68 65 0a  st early in the.
2aa77 2a 2a 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  ** sqlite3WhereB
2aa78 65 67 69 6e 28 29 20 72 6f 75 74 69 6e 65 2e 20  egin() routine. 
2aa79 20 53 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74   So we know that
2aa7a 20 74 68 65 20 70 4d 61 73 6b 53 65 74 2d 3e 69   the pMaskSet->i
2aa7b 78 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c  x[].** array wil
2aa7c 6c 20 6e 65 76 65 72 20 6f 76 65 72 66 6c 6f 77  l never overflow
2aa7d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2aa7e 20 63 72 65 61 74 65 4d 61 73 6b 28 57 68 65 72   createMask(Wher
2aa7f 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
2aa80 65 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29  et, int iCursor)
2aa81 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 61 73  {.  assert( pMas
2aa82 6b 53 65 74 2d 3e 6e 20 3c 20 41 72 72 61 79 53  kSet->n < ArrayS
2aa83 69 7a 65 28 70 4d 61 73 6b 53 65 74 2d 3e 69 78  ize(pMaskSet->ix
2aa84 29 20 29 3b 0a 20 20 70 4d 61 73 6b 53 65 74 2d  ) );.  pMaskSet-
2aa85 3e 69 78 5b 70 4d 61 73 6b 53 65 74 2d 3e 6e 2b  >ix[pMaskSet->n+
2aa86 2b 5d 20 3d 20 69 43 75 72 73 6f 72 3b 0a 7d 0a  +] = iCursor;.}.
2aa87 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2aa88 69 6e 65 20 77 61 6c 6b 73 20 28 72 65 63 75 72  ine walks (recur
2aa89 73 69 76 65 6c 79 29 20 61 6e 20 65 78 70 72 65  sively) an expre
2aa8a 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 67  ssion tree and g
2aa8b 65 6e 65 72 61 74 65 73 0a 2a 2a 20 61 20 62 69  enerates.** a bi
2aa8c 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67  tmask indicating
2aa8d 20 77 68 69 63 68 20 74 61 62 6c 65 73 20 61 72   which tables ar
2aa8e 65 20 75 73 65 64 20 69 6e 20 74 68 61 74 20 65  e used in that e
2aa8f 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65  xpression.** tre
2aa90 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65  e..**.** In orde
2aa91 72 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69  r for this routi
2aa92 6e 65 20 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20  ne to work, the 
2aa93 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
2aa94 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a 20 70 72   must have.** pr
2aa95 65 76 69 6f 75 73 6c 79 20 69 6e 76 6f 6b 65 64  eviously invoked
2aa96 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2aa97 78 70 72 4e 61 6d 65 73 28 29 20 6f 6e 20 74 68  xprNames() on th
2aa98 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 53  e expression.  S
2aa99 65 65 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  ee.** the header
2aa9a 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 61 74   comment on that
2aa9b 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 64 64   routine for add
2aa9c 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2aa9d 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ion..** The sqli
2aa9e 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2aa9f 6d 65 73 28 29 20 72 6f 75 74 69 6e 65 73 20 6c  mes() routines l
2aaa0 6f 6f 6b 73 20 66 6f 72 20 63 6f 6c 75 6d 6e 20  ooks for column 
2aaa1 6e 61 6d 65 73 20 61 6e 64 0a 2a 2a 20 73 65 74  names and.** set
2aaa2 73 20 74 68 65 69 72 20 6f 70 63 6f 64 65 73 20  s their opcodes 
2aaa3 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64  to TK_COLUMN and
2aaa4 20 74 68 65 69 72 20 45 78 70 72 2e 69 54 61 62   their Expr.iTab
2aaa5 6c 65 20 66 69 65 6c 64 73 20 74 6f 0a 2a 2a 20  le fields to.** 
2aaa6 74 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  the VDBE cursor 
2aaa7 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61  number of the ta
2aaa8 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ble.  This routi
2aaa9 6e 65 20 6a 75 73 74 20 68 61 73 20 74 6f 0a 2a  ne just has to.*
2aaaa 2a 20 74 72 61 6e 73 6c 61 74 65 20 74 68 65 20  * translate the 
2aaab 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69  cursor numbers i
2aaac 6e 74 6f 20 62 69 74 6d 61 73 6b 20 76 61 6c 75  nto bitmask valu
2aaad 65 73 20 61 6e 64 20 4f 52 20 61 6c 6c 0a 2a 2a  es and OR all.**
2aaae 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20 74 6f   the bitmasks to
2aaaf 67 65 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  gether..*/.stati
2aab0 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 4c 69  c Bitmask exprLi
2aab1 73 74 54 61 62 6c 65 55 73 61 67 65 28 57 68 65  stTableUsage(Whe
2aab2 72 65 4d 61 73 6b 53 65 74 2a 2c 20 45 78 70 72  reMaskSet*, Expr
2aab3 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 42  List*);.static B
2aab4 69 74 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63  itmask exprSelec
2aab5 74 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72  tTableUsage(Wher
2aab6 65 4d 61 73 6b 53 65 74 2a 2c 20 53 65 6c 65 63  eMaskSet*, Selec
2aab7 74 2a 29 3b 0a 73 74 61 74 69 63 20 42 69 74 6d  t*);.static Bitm
2aab8 61 73 6b 20 65 78 70 72 54 61 62 6c 65 55 73 61  ask exprTableUsa
2aab9 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20  ge(WhereMaskSet 
2aaba 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 20  *pMaskSet, Expr 
2aabb 2a 70 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d  *p){.  Bitmask m
2aabc 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  ask = 0;.  if( p
2aabd 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
2aabe 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f    if( p->op==TK_
2aabf 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6d 61  COLUMN ){.    ma
2aac0 73 6b 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61  sk = getMask(pMa
2aac1 73 6b 53 65 74 2c 20 70 2d 3e 69 54 61 62 6c 65  skSet, p->iTable
2aac2 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 61  );.    return ma
2aac3 73 6b 3b 0a 20 20 7d 0a 20 20 6d 61 73 6b 20 3d  sk;.  }.  mask =
2aac4 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2aac5 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70 52 69  pMaskSet, p->pRi
2aac6 67 68 74 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d 20  ght);.  mask |= 
2aac7 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2aac8 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70 4c 65 66  MaskSet, p->pLef
2aac9 74 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61  t);.  if( ExprHa
2aaca 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  sProperty(p, EP_
2aacb 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
2aacc 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 53 65    mask |= exprSe
2aacd 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70  lectTableUsage(p
2aace 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 53  MaskSet, p->x.pS
2aacf 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  elect);.  }else{
2aad0 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2aad1 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
2aad2 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70  pMaskSet, p->x.p
2aad3 4c 69 73 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  List);.  }.  ret
2aad4 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74  urn mask;.}.stat
2aad5 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 4c  ic Bitmask exprL
2aad6 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 57 68  istTableUsage(Wh
2aad7 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2aad8 6b 53 65 74 2c 20 45 78 70 72 4c 69 73 74 20 2a  kSet, ExprList *
2aad9 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
2aada 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20  .  Bitmask mask 
2aadb 3d 20 30 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  = 0;.  if( pList
2aadc 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
2aadd 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
2aade 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 61 73   i++){.      mas
2aadf 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
2aae0 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c  age(pMaskSet, pL
2aae1 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
2aae2 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2aae3 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61  turn mask;.}.sta
2aae4 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72  tic Bitmask expr
2aae5 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65  SelectTableUsage
2aae6 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70  (WhereMaskSet *p
2aae7 4d 61 73 6b 53 65 74 2c 20 53 65 6c 65 63 74 20  MaskSet, Select 
2aae8 2a 70 53 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20  *pS){.  Bitmask 
2aae9 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 77 68 69 6c  mask = 0;.  whil
2aaea 65 28 20 70 53 20 29 7b 0a 20 20 20 20 6d 61 73  e( pS ){.    mas
2aaeb 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
2aaec 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2aaed 2c 20 70 53 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  , pS->pEList);. 
2aaee 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c     mask |= exprL
2aaef 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  istTableUsage(pM
2aaf0 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 47 72 6f  askSet, pS->pGro
2aaf1 75 70 42 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20  upBy);.    mask 
2aaf2 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65  |= exprListTable
2aaf3 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2aaf4 70 53 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20  pS->pOrderBy);. 
2aaf5 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54     mask |= exprT
2aaf6 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2aaf7 65 74 2c 20 70 53 2d 3e 70 57 68 65 72 65 29 3b  et, pS->pWhere);
2aaf8 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2aaf9 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
2aafa 6b 53 65 74 2c 20 70 53 2d 3e 70 48 61 76 69 6e  kSet, pS->pHavin
2aafb 67 29 3b 0a 20 20 20 20 70 53 20 3d 20 70 53 2d  g);.    pS = pS-
2aafc 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72  >pPrior;.  }.  r
2aafd 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f  eturn mask;.}../
2aafe 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
2aaff 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 6f 70   if the given op
2ab00 65 72 61 74 6f 72 20 69 73 20 6f 6e 65 20 6f 66  erator is one of
2ab01 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74   the operators t
2ab02 68 61 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65  hat is.** allowe
2ab03 64 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 61 62  d for an indexab
2ab04 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  le WHERE clause 
2ab05 74 65 72 6d 2e 20 20 54 68 65 20 61 6c 6c 6f 77  term.  The allow
2ab06 65 64 20 6f 70 65 72 61 74 6f 72 73 20 61 72 65  ed operators are
2ab07 0a 2a 2a 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3e  .** "=", "<", ">
2ab08 22 2c 20 22 3c 3d 22 2c 20 22 3e 3d 22 2c 20 61  ", "<=", ">=", a
2ab09 6e 64 20 22 49 4e 22 2e 0a 2a 2f 0a 73 74 61 74  nd "IN"..*/.stat
2ab0a 69 63 20 69 6e 74 20 61 6c 6c 6f 77 65 64 4f 70  ic int allowedOp
2ab0b 28 69 6e 74 20 6f 70 29 7b 0a 20 20 61 73 73 65  (int op){.  asse
2ab0c 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20  rt( TK_GT>TK_EQ 
2ab0d 26 26 20 54 4b 5f 47 54 3c 54 4b 5f 47 45 20 29  && TK_GT<TK_GE )
2ab0e 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c  ;.  assert( TK_L
2ab0f 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 54  T>TK_EQ && TK_LT
2ab10 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 65  <TK_GE );.  asse
2ab11 72 74 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45 51 20  rt( TK_LE>TK_EQ 
2ab12 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45 20 29  && TK_LE<TK_GE )
2ab13 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47  ;.  assert( TK_G
2ab14 45 3d 3d 54 4b 5f 45 51 2b 34 20 29 3b 0a 20 20  E==TK_EQ+4 );.  
2ab15 72 65 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f 49 4e  return op==TK_IN
2ab16 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51 20 26   || (op>=TK_EQ &
2ab17 26 20 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c 7c 20  & op<=TK_GE) || 
2ab18 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b 0a 7d  op==TK_ISNULL;.}
2ab19 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 74 77 6f  ../*.** Swap two
2ab1a 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 79 70 65   objects of type
2ab1b 20 54 59 50 45 2e 0a 2a 2f 0a 23 64 65 66 69 6e   TYPE..*/.#defin
2ab1c 65 20 53 57 41 50 28 54 59 50 45 2c 41 2c 42 29  e SWAP(TYPE,A,B)
2ab1d 20 7b 54 59 50 45 20 74 3d 41 3b 20 41 3d 42 3b   {TYPE t=A; A=B;
2ab1e 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   B=t;}../*.** Co
2ab1f 6d 6d 75 74 65 20 61 20 63 6f 6d 70 61 72 69 73  mmute a comparis
2ab20 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 45 78  on operator.  Ex
2ab21 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65  pressions of the
2ab22 20 66 6f 72 6d 20 22 58 20 6f 70 20 59 22 0a 2a   form "X op Y".*
2ab23 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  * are converted 
2ab24 69 6e 74 6f 20 22 59 20 6f 70 20 58 22 2e 0a 2a  into "Y op X"..*
2ab25 2a 0a 2a 2a 20 49 66 20 61 20 63 6f 6c 6c 61 74  *.** If a collat
2ab26 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20  ion sequence is 
2ab27 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2ab28 65 69 74 68 65 72 20 74 68 65 20 6c 65 66 74 20  either the left 
2ab29 6f 72 20 72 69 67 68 74 0a 2a 2a 20 73 69 64 65  or right.** side
2ab2a 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73   of the comparis
2ab2b 6f 6e 2c 20 69 74 20 72 65 6d 61 69 6e 73 20 61  on, it remains a
2ab2c 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2ab2d 68 65 20 73 61 6d 65 20 73 69 64 65 20 61 66 74  he same side aft
2ab2e 65 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 75 74  er.** the commut
2ab2f 61 74 69 6f 6e 2e 20 53 6f 20 22 59 20 63 6f 6c  ation. So "Y col
2ab30 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 58  late NOCASE op X
2ab31 22 20 62 65 63 6f 6d 65 73 20 0a 2a 2a 20 22 58  " becomes .** "X
2ab32 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20   collate NOCASE 
2ab33 6f 70 20 59 22 2e 20 54 68 69 73 20 69 73 20 62  op Y". This is b
2ab34 65 63 61 75 73 65 20 61 6e 79 20 63 6f 6c 6c 61  ecause any colla
2ab35 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 6e  tion sequence on
2ab36 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20 68 61 6e  .** the left han
2ab37 64 20 73 69 64 65 20 6f 66 20 61 20 63 6f 6d 70  d side of a comp
2ab38 61 72 69 73 6f 6e 20 6f 76 65 72 72 69 64 65 73  arison overrides
2ab39 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   any collation s
2ab3a 65 71 75 65 6e 63 65 20 0a 2a 2a 20 61 74 74 61  equence .** atta
2ab3b 63 68 65 64 20 74 6f 20 74 68 65 20 72 69 67 68  ched to the righ
2ab3c 74 2e 20 46 6f 72 20 74 68 65 20 73 61 6d 65 20  t. For the same 
2ab3d 72 65 61 73 6f 6e 20 74 68 65 20 45 50 5f 45 78  reason the EP_Ex
2ab3e 70 43 6f 6c 6c 61 74 65 20 66 6c 61 67 0a 2a 2a  pCollate flag.**
2ab3f 20 69 73 20 6e 6f 74 20 63 6f 6d 6d 75 74 65 64   is not commuted
2ab40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2ab41 20 65 78 70 72 43 6f 6d 6d 75 74 65 28 50 61 72   exprCommute(Par
2ab42 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
2ab43 20 2a 70 45 78 70 72 29 7b 0a 20 20 75 31 36 20   *pExpr){.  u16 
2ab44 65 78 70 52 69 67 68 74 20 3d 20 28 70 45 78 70  expRight = (pExp
2ab45 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73  r->pRight->flags
2ab46 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
2ab47 29 3b 0a 20 20 75 31 36 20 65 78 70 4c 65 66 74  );.  u16 expLeft
2ab48 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74   = (pExpr->pLeft
2ab49 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
2ab4a 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 61 73 73 65  Collate);.  asse
2ab4b 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 45  rt( allowedOp(pE
2ab4c 78 70 72 2d 3e 6f 70 29 20 26 26 20 70 45 78 70  xpr->op) && pExp
2ab4d 72 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20 29 3b 0a  r->op!=TK_IN );.
2ab4e 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d    pExpr->pRight-
2ab4f 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33  >pColl = sqlite3
2ab50 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
2ab51 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
2ab52 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65  t);.  pExpr->pLe
2ab53 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ft->pColl = sqli
2ab54 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
2ab55 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
2ab56 65 66 74 29 3b 0a 20 20 53 57 41 50 28 43 6f 6c  eft);.  SWAP(Col
2ab57 6c 53 65 71 2a 2c 70 45 78 70 72 2d 3e 70 52 69  lSeq*,pExpr->pRi
2ab58 67 68 74 2d 3e 70 43 6f 6c 6c 2c 70 45 78 70 72  ght->pColl,pExpr
2ab59 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 29 3b  ->pLeft->pColl);
2ab5a 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  .  pExpr->pRight
2ab5b 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72  ->flags = (pExpr
2ab5c 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20  ->pRight->flags 
2ab5d 26 20 7e 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  & ~EP_ExpCollate
2ab5e 29 20 7c 20 65 78 70 4c 65 66 74 3b 0a 20 20 70  ) | expLeft;.  p
2ab5f 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61  Expr->pLeft->fla
2ab60 67 73 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65  gs = (pExpr->pLe
2ab61 66 74 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f  ft->flags & ~EP_
2ab62 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78  ExpCollate) | ex
2ab63 70 52 69 67 68 74 3b 0a 20 20 53 57 41 50 28 45  pRight;.  SWAP(E
2ab64 78 70 72 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67  xpr*,pExpr->pRig
2ab65 68 74 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 29  ht,pExpr->pLeft)
2ab66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ;.  if( pExpr->o
2ab67 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20  p>=TK_GT ){.    
2ab68 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54  assert( TK_LT==T
2ab69 4b 5f 47 54 2b 32 20 29 3b 0a 20 20 20 20 61 73  K_GT+2 );.    as
2ab6a 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f  sert( TK_GE==TK_
2ab6b 4c 45 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65  LE+2 );.    asse
2ab6c 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20  rt( TK_GT>TK_EQ 
2ab6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 54  );.    assert( T
2ab6e 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b 0a 20 20  K_GT<TK_LE );.  
2ab6f 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
2ab70 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26 26 20 70 45  >op>=TK_GT && pE
2ab71 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47 45 20 29  xpr->op<=TK_GE )
2ab72 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20  ;.    pExpr->op 
2ab73 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70 2d 54 4b  = ((pExpr->op-TK
2ab74 5f 47 54 29 5e 32 29 2b 54 4b 5f 47 54 3b 0a 20  _GT)^2)+TK_GT;. 
2ab75 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e   }.}../*.** Tran
2ab76 73 6c 61 74 65 20 66 72 6f 6d 20 54 4b 5f 78 78  slate from TK_xx
2ab77 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 57 4f 5f   operator to WO_
2ab78 78 78 20 62 69 74 6d 61 73 6b 2e 0a 2a 2f 0a 73  xx bitmask..*/.s
2ab79 74 61 74 69 63 20 75 31 36 20 6f 70 65 72 61 74  tatic u16 operat
2ab7a 6f 72 4d 61 73 6b 28 69 6e 74 20 6f 70 29 7b 0a  orMask(int op){.
2ab7b 20 20 75 31 36 20 63 3b 0a 20 20 61 73 73 65 72    u16 c;.  asser
2ab7c 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29  t( allowedOp(op)
2ab7d 20 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b   );.  if( op==TK
2ab7e 5f 49 4e 20 29 7b 0a 20 20 20 20 63 20 3d 20 57  _IN ){.    c = W
2ab7f 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66  O_IN;.  }else if
2ab80 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
2ab81 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 53  ){.    c = WO_IS
2ab82 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  NULL;.  }else{. 
2ab83 20 20 20 61 73 73 65 72 74 28 20 28 57 4f 5f 45     assert( (WO_E
2ab84 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 20 3c  Q<<(op-TK_EQ)) <
2ab85 20 30 78 37 66 66 66 20 29 3b 0a 20 20 20 20 63   0x7fff );.    c
2ab86 20 3d 20 28 75 31 36 29 28 57 4f 5f 45 51 3c 3c   = (u16)(WO_EQ<<
2ab87 28 6f 70 2d 54 4b 5f 45 51 29 29 3b 0a 20 20 7d  (op-TK_EQ));.  }
2ab88 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54  .  assert( op!=T
2ab89 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d 3d 57  K_ISNULL || c==W
2ab8a 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 61 73  O_ISNULL );.  as
2ab8b 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 4e 20  sert( op!=TK_IN 
2ab8c 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20  || c==WO_IN );. 
2ab8d 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2ab8e 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51 20 29  EQ || c==WO_EQ )
2ab8f 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
2ab90 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f 5f 4c  TK_LT || c==WO_L
2ab91 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
2ab92 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d 3d 57  p!=TK_LE || c==W
2ab93 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73 65 72 74  O_LE );.  assert
2ab94 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 63  ( op!=TK_GT || c
2ab95 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20 61 73 73  ==WO_GT );.  ass
2ab96 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 45 20 7c  ert( op!=TK_GE |
2ab97 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20 20  | c==WO_GE );.  
2ab98 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a 0a  return c;.}../*.
2ab99 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20  ** Search for a 
2ab9a 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52  term in the WHER
2ab9b 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 69 73  E clause that is
2ab9c 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20   of the form "X 
2ab9d 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0a 2a 2a 20  <op> <expr>".** 
2ab9e 77 68 65 72 65 20 58 20 69 73 20 61 20 72 65 66  where X is a ref
2ab9f 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 69 43  erence to the iC
2aba0 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 69  olumn of table i
2aba1 43 75 72 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20  Cur and <op> is 
2aba2 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 57 4f  one of.** the WO
2aba3 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 63 6f 64  _xx operator cod
2aba4 65 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  es specified by 
2aba5 74 68 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72  the op parameter
2aba6 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  ..** Return a po
2aba7 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74 65 72  inter to the ter
2aba8 6d 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 20  m.  Return 0 if 
2aba9 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74  not found..*/.st
2abaa 61 74 69 63 20 57 68 65 72 65 54 65 72 6d 20 2a  atic WhereTerm *
2abab 66 69 6e 64 54 65 72 6d 28 0a 20 20 57 68 65 72  findTerm(.  Wher
2abac 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2abad 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
2abae 6c 61 75 73 65 20 74 6f 20 62 65 20 73 65 61 72  lause to be sear
2abaf 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ched */.  int iC
2abb0 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ur,             
2abb1 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
2abb2 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 69 6e 74   of LHS */.  int
2abb3 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20   iColumn,       
2abb4 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
2abb5 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20  ber of LHS */.  
2abb6 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2abb7 2c 20 20 20 20 20 2f 2a 20 52 48 53 20 6d 75 73  ,     /* RHS mus
2abb8 74 20 6e 6f 74 20 6f 76 65 72 6c 61 70 20 77 69  t not overlap wi
2abb9 74 68 20 74 68 69 73 20 6d 61 73 6b 20 2a 2f 0a  th this mask */.
2abba 20 20 75 33 32 20 6f 70 2c 20 20 20 20 20 20 20    u32 op,       
2abbb 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
2abbc 6f 66 20 57 4f 5f 78 78 20 76 61 6c 75 65 73 20  of WO_xx values 
2abbd 64 65 73 63 72 69 62 69 6e 67 20 6f 70 65 72 61  describing opera
2abbe 74 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  tor */.  Index *
2abbf 70 49 64 78 20 20 20 20 20 20 20 20 20 20 20 2f  pIdx           /
2abc0 2a 20 4d 75 73 74 20 62 65 20 63 6f 6d 70 61 74  * Must be compat
2abc1 69 62 6c 65 20 77 69 74 68 20 74 68 69 73 20 69  ible with this i
2abc2 6e 64 65 78 2c 20 69 66 20 6e 6f 74 20 4e 55 4c  ndex, if not NUL
2abc3 4c 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54  L */.){.  WhereT
2abc4 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e  erm *pTerm;.  in
2abc5 74 20 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 69  t k;.  assert( i
2abc6 43 75 72 3e 3d 30 20 29 3b 0a 20 20 6f 70 20 26  Cur>=0 );.  op &
2abc7 3d 20 57 4f 5f 41 4c 4c 3b 0a 20 20 66 6f 72 28  = WO_ALL;.  for(
2abc8 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6b 3d  pTerm=pWC->a, k=
2abc9 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6b 3b 20 6b  pWC->nTerm; k; k
2abca 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20  --, pTerm++){.  
2abcb 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66    if( pTerm->lef
2abcc 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 0a 20 20  tCursor==iCur.  
2abcd 20 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e       && (pTerm->
2abce 70 72 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f  prereqRight & no
2abcf 74 52 65 61 64 79 29 3d 3d 30 0a 20 20 20 20 20  tReady)==0.     
2abd0 20 20 26 26 20 70 54 65 72 6d 2d 3e 75 2e 6c 65    && pTerm->u.le
2abd1 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d  ftColumn==iColum
2abd2 6e 0a 20 20 20 20 20 20 20 26 26 20 28 70 54 65  n.       && (pTe
2abd3 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2abd4 6f 70 29 21 3d 30 0a 20 20 20 20 29 7b 0a 20 20  op)!=0.    ){.  
2abd5 20 20 20 20 69 66 28 20 70 49 64 78 20 26 26 20      if( pIdx && 
2abd6 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2abd7 21 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20  !=WO_ISNULL ){. 
2abd8 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20         Expr *pX 
2abd9 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a  = pTerm->pExpr;.
2abda 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
2abdb 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20  *pColl;.        
2abdc 63 68 61 72 20 69 64 78 61 66 66 3b 0a 20 20 20  char idxaff;.   
2abdd 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2abde 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73      Parse *pPars
2abdf 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b  e = pWC->pParse;
2abe0 0a 0a 20 20 20 20 20 20 20 20 69 64 78 61 66 66  ..        idxaff
2abe1 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d   = pIdx->pTable-
2abe2 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61  >aCol[iColumn].a
2abe3 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20  ffinity;.       
2abe4 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 6e 64   if( !sqlite3Ind
2abe5 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 70 58 2c  exAffinityOk(pX,
2abe6 20 69 64 78 61 66 66 29 20 29 20 63 6f 6e 74 69   idxaff) ) conti
2abe7 6e 75 65 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  nue;..        /*
2abe8 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20   Figure out the 
2abe9 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2abea 63 65 20 72 65 71 75 69 72 65 64 20 66 72 6f 6d  ce required from
2abeb 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 0a 20 20   an index for.  
2abec 20 20 20 20 20 20 2a 2a 20 69 74 20 74 6f 20 62        ** it to b
2abed 65 20 75 73 65 66 75 6c 20 66 6f 72 20 6f 70 74  e useful for opt
2abee 69 6d 69 73 69 6e 67 20 65 78 70 72 65 73 73 69  imising expressi
2abef 6f 6e 20 70 58 2e 20 53 74 6f 72 65 20 74 68 69  on pX. Store thi
2abf0 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c  s.        ** val
2abf1 75 65 20 69 6e 20 76 61 72 69 61 62 6c 65 20 70  ue in variable p
2abf2 43 6f 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f  Coll..        */
2abf3 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2abf4 70 58 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20  pX->pLeft);.    
2abf5 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
2abf6 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65  te3BinaryCompare
2abf7 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2abf8 70 58 2d 3e 70 4c 65 66 74 2c 20 70 58 2d 3e 70  pX->pLeft, pX->p
2abf9 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20  Right);.        
2abfa 61 73 73 65 72 74 28 70 43 6f 6c 6c 20 7c 7c 20  assert(pColl || 
2abfb 70 50 61 72 73 65 2d 3e 6e 45 72 72 29 3b 0a 0a  pParse->nErr);..
2abfc 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
2abfd 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
2abfe 6a 5d 21 3d 69 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b  j]!=iColumn; j++
2abff 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
2ac00 20 4e 45 56 45 52 28 6a 3e 3d 70 49 64 78 2d 3e   NEVER(j>=pIdx->
2ac01 6e 43 6f 6c 75 6d 6e 29 20 29 20 72 65 74 75 72  nColumn) ) retur
2ac02 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 0;.        }. 
2ac03 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c         if( pColl
2ac04 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
2ac05 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c  mp(pColl->zName,
2ac06 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d   pIdx->azColl[j]
2ac07 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
2ac08 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
2ac09 72 6e 20 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a  rn pTerm;.    }.
2ac0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2ac0b 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  }../* Forward re
2ac0c 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69  ference */.stati
2ac0d 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
2ac0e 7a 65 28 53 72 63 4c 69 73 74 2a 2c 20 57 68 65  ze(SrcList*, Whe
2ac0f 72 65 43 6c 61 75 73 65 2a 2c 20 69 6e 74 29 3b  reClause*, int);
2ac10 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 65 78 70  ../*.** Call exp
2ac11 72 41 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c 6c 20  rAnalyze on all 
2ac12 74 65 72 6d 73 20 69 6e 20 61 20 57 48 45 52 45  terms in a WHERE
2ac13 20 63 6c 61 75 73 65 2e 20 20 0a 2a 2a 0a 2a 2a   clause.  .**.**
2ac14 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2ac15 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 0a  exprAnalyzeAll(.
2ac16 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
2ac17 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 68  ist,       /* th
2ac18 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  e FROM clause */
2ac19 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2ac1a 70 57 43 20 20 20 20 20 20 20 20 20 2f 2a 20 74  pWC         /* t
2ac1b 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2ac1c 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a  to be analyzed *
2ac1d 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
2ac1e 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d  for(i=pWC->nTerm
2ac1f 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a  -1; i>=0; i--){.
2ac20 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28      exprAnalyze(
2ac21 70 54 61 62 4c 69 73 74 2c 20 70 57 43 2c 20 69  pTabList, pWC, i
2ac22 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65  );.  }.}..#ifnde
2ac23 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49  f SQLITE_OMIT_LI
2ac24 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a  KE_OPTIMIZATION.
2ac25 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
2ac26 65 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  ee if the given 
2ac27 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
2ac28 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 6f 70 65  LIKE or GLOB ope
2ac29 72 61 74 6f 72 20 74 68 61 74 0a 2a 2a 20 63 61  rator that.** ca
2ac2a 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 75  n be optimized u
2ac2b 73 69 6e 67 20 69 6e 65 71 75 61 6c 69 74 79 20  sing inequality 
2ac2c 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 52 65  constraints.  Re
2ac2d 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20  turn TRUE if it 
2ac2e 69 73 0a 2a 2a 20 73 6f 20 61 6e 64 20 66 61 6c  is.** so and fal
2ac2f 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a  se if not..**.**
2ac30 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68   In order for th
2ac31 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65  e operator to be
2ac32 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c 20 74 68   optimizible, th
2ac33 65 20 52 48 53 20 6d 75 73 74 20 62 65 20 61 20  e RHS must be a 
2ac34 73 74 72 69 6e 67 0a 2a 2a 20 6c 69 74 65 72 61  string.** litera
2ac35 6c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  l that does not 
2ac36 62 65 67 69 6e 20 77 69 74 68 20 61 20 77 69 6c  begin with a wil
2ac37 64 63 61 72 64 2e 20 20 0a 2a 2f 0a 73 74 61 74  dcard.  .*/.stat
2ac38 69 63 20 69 6e 74 20 69 73 4c 69 6b 65 4f 72 47  ic int isLikeOrG
2ac39 6c 6f 62 28 0a 20 20 50 61 72 73 65 20 2a 70 50  lob(.  Parse *pP
2ac3a 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73  arse,    /* Pars
2ac3b 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
2ac3c 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
2ac3d 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
2ac3e 2c 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74  ,      /* Test t
2ac3f 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  his expression *
2ac40 2f 0a 20 20 69 6e 74 20 2a 70 6e 50 61 74 74 65  /.  int *pnPatte
2ac41 72 6e 2c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  rn,   /* Number 
2ac42 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20  of non-wildcard 
2ac43 70 72 65 66 69 78 20 63 68 61 72 61 63 74 65 72  prefix character
2ac44 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 73 43  s */.  int *pisC
2ac45 6f 6d 70 6c 65 74 65 2c 20 2f 2a 20 54 72 75 65  omplete, /* True
2ac46 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 77 69 6c   if the only wil
2ac47 64 63 61 72 64 20 69 73 20 25 20 69 6e 20 74 68  dcard is % in th
2ac48 65 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72  e last character
2ac49 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f 43 61   */.  int *pnoCa
2ac4a 73 65 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  se      /* True 
2ac4b 69 66 20 75 70 70 65 72 63 61 73 65 20 69 73 20  if uppercase is 
2ac4c 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 6c 6f  equivalent to lo
2ac4d 77 65 72 63 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  wercase */.){.  
2ac4e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20  const char *z;  
2ac4f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
2ac50 72 69 6e 67 20 6f 6e 20 52 48 53 20 6f 66 20 4c  ring on RHS of L
2ac51 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a  IKE operator */.
2ac52 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20    Expr *pRight, 
2ac53 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 2f 2a 20  *pLeft;      /* 
2ac54 52 69 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73  Right and left s
2ac55 69 7a 65 20 6f 66 20 4c 49 4b 45 20 6f 70 65 72  ize of LIKE oper
2ac56 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ator */.  ExprLi
2ac57 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20  st *pList;      
2ac58 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
2ac59 6f 70 65 72 61 6e 64 73 20 74 6f 20 74 68 65 20  operands to the 
2ac5a 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f  LIKE operator */
2ac5b 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20  .  int c;       
2ac5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ac5d 20 4f 6e 65 20 63 68 61 72 61 63 74 65 72 20 69   One character i
2ac5e 6e 20 7a 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 63  n z[] */.  int c
2ac5f 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
2ac60 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2ac61 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20  of non-wildcard 
2ac62 70 72 65 66 69 78 20 63 68 61 72 61 63 74 65 72  prefix character
2ac63 73 20 2a 2f 0a 20 20 63 68 61 72 20 77 63 5b 33  s */.  char wc[3
2ac64 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
2ac65 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 63 68    /* Wildcard ch
2ac66 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 43 6f  aracters */.  Co
2ac67 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20  llSeq *pColl;   
2ac68 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c           /* Coll
2ac69 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
2ac6a 6f 72 20 4c 48 53 20 2a 2f 0a 20 20 73 71 6c 69  or LHS */.  sqli
2ac6b 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2ac6c 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61  ->db;  /* Databa
2ac6d 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2ac6e 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ..  if( !sqlite3
2ac6f 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 64  IsLikeFunction(d
2ac70 62 2c 20 70 45 78 70 72 2c 20 70 6e 6f 43 61 73  b, pExpr, pnoCas
2ac71 65 2c 20 77 63 29 20 29 7b 0a 20 20 20 20 72 65  e, wc) ){.    re
2ac72 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 69 66 64  turn 0;.  }.#ifd
2ac73 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ef SQLITE_EBCDIC
2ac74 0a 20 20 69 66 28 20 2a 70 6e 6f 43 61 73 65 20  .  if( *pnoCase 
2ac75 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64  ) return 0;.#end
2ac76 69 66 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78  if.  pList = pEx
2ac77 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 70  pr->x.pList;.  p
2ac78 52 69 67 68 74 20 3d 20 70 4c 69 73 74 2d 3e 61  Right = pList->a
2ac79 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28  [0].pExpr;.  if(
2ac7a 20 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f   pRight->op!=TK_
2ac7b 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 72 65  STRING ){.    re
2ac7c 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c  turn 0;.  }.  pL
2ac7d 65 66 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 31  eft = pList->a[1
2ac7e 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70  ].pExpr;.  if( p
2ac7f 4c 65 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  Left->op!=TK_COL
2ac80 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  UMN ){.    retur
2ac81 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 6c  n 0;.  }.  pColl
2ac82 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2ac83 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c  llSeq(pParse, pL
2ac84 65 66 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20  eft);.  assert( 
2ac85 70 43 6f 6c 6c 21 3d 30 20 7c 7c 20 70 4c 65 66  pColl!=0 || pLef
2ac86 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 29  t->iColumn==-1 )
2ac87 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d 30  ;.  if( pColl==0
2ac88 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 63 6f   ){.    /* No co
2ac89 6c 6c 61 74 69 6f 6e 20 69 73 20 64 65 66 69 6e  llation is defin
2ac8a 65 64 20 66 6f 72 20 74 68 65 20 52 4f 57 49 44  ed for the ROWID
2ac8b 2e 20 20 55 73 65 20 74 68 65 20 64 65 66 61 75  .  Use the defau
2ac8c 6c 74 2e 20 2a 2f 0a 20 20 20 20 70 43 6f 6c 6c  lt. */.    pColl
2ac8d 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c   = db->pDfltColl
2ac8e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 43 6f  ;.  }.  if( (pCo
2ac8f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54 45  ll->type!=SQLITE
2ac90 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 7c 7c 20  _COLL_BINARY || 
2ac91 2a 70 6e 6f 43 61 73 65 29 20 26 26 0a 20 20 20  *pnoCase) &&.   
2ac92 20 20 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65 21     (pColl->type!
2ac93 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43  =SQLITE_COLL_NOC
2ac94 41 53 45 20 7c 7c 20 21 2a 70 6e 6f 43 61 73 65  ASE || !*pnoCase
2ac95 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2ac96 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
2ac97 44 65 71 75 6f 74 65 45 78 70 72 28 70 52 69 67  DequoteExpr(pRig
2ac98 68 74 29 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72  ht);.  z = (char
2ac99 20 2a 29 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e   *)pRight->token
2ac9a 2e 7a 3b 0a 20 20 63 6e 74 20 3d 20 30 3b 0a 20  .z;.  cnt = 0;. 
2ac9b 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 77 68   if( z ){.    wh
2ac9c 69 6c 65 28 20 28 63 3d 7a 5b 63 6e 74 5d 29 21  ile( (c=z[cnt])!
2ac9d 3d 30 20 26 26 20 63 21 3d 77 63 5b 30 5d 20 26  =0 && c!=wc[0] &
2ac9e 26 20 63 21 3d 77 63 5b 31 5d 20 26 26 20 63 21  & c!=wc[1] && c!
2ac9f 3d 77 63 5b 32 5d 20 29 7b 20 63 6e 74 2b 2b 3b  =wc[2] ){ cnt++;
2aca0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 63 6e 74   }.  }.  if( cnt
2aca1 3d 3d 30 20 7c 7c 20 32 35 35 3d 3d 28 75 38 29  ==0 || 255==(u8)
2aca2 7a 5b 63 6e 74 2d 31 5d 20 29 7b 0a 20 20 20 20  z[cnt-1] ){.    
2aca3 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2aca4 2a 70 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 7a  *pisComplete = z
2aca5 5b 63 6e 74 5d 3d 3d 77 63 5b 30 5d 20 26 26 20  [cnt]==wc[0] && 
2aca6 7a 5b 63 6e 74 2b 31 5d 3d 3d 30 3b 0a 20 20 2a  z[cnt+1]==0;.  *
2aca7 70 6e 50 61 74 74 65 72 6e 20 3d 20 63 6e 74 3b  pnPattern = cnt;
2aca8 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
2aca9 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2acaa 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49  OMIT_LIKE_OPTIMI
2acab 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 23 69 66 6e  ZATION */...#ifn
2acac 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2acad 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a  VIRTUALTABLE./*.
2acae 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
2acaf 69 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  if the given exp
2acb0 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68  ression is of th
2acb1 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
2acb2 20 20 20 20 20 63 6f 6c 75 6d 6e 20 4d 41 54 43       column MATC
2acb3 48 20 65 78 70 72 0a 2a 2a 0a 2a 2a 20 49 66 20  H expr.**.** If 
2acb4 69 74 20 69 73 20 74 68 65 6e 20 72 65 74 75 72  it is then retur
2acb5 6e 20 54 52 55 45 2e 20 20 49 66 20 6e 6f 74 2c  n TRUE.  If not,
2acb6 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a   return FALSE..*
2acb7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4d  /.static int isM
2acb8 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20  atchOfColumn(.  
2acb9 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20  Expr *pExpr     
2acba 20 2f 2a 20 54 65 73 74 20 74 68 69 73 20 65 78   /* Test this ex
2acbb 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  pression */.){. 
2acbc 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2acbd 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  ;..  if( pExpr->
2acbe 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20  op!=TK_FUNCTION 
2acbf 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2acc0 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72  .  }.  if( pExpr
2acc1 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 35 20 7c 7c 0a  ->token.n!=5 ||.
2acc2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
2acc3 72 4e 49 43 6d 70 28 28 63 6f 6e 73 74 20 63 68  rNICmp((const ch
2acc4 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
2acc5 2e 7a 2c 22 6d 61 74 63 68 22 2c 35 29 21 3d 30  .z,"match",5)!=0
2acc6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2acc7 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20  ;.  }.  pList = 
2acc8 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a  pExpr->x.pList;.
2acc9 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78    if( pList->nEx
2acca 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74  pr!=2 ){.    ret
2accb 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
2accc 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78   pList->a[1].pEx
2accd 70 72 2d 3e 6f 70 20 21 3d 20 54 4b 5f 43 4f 4c  pr->op != TK_COL
2acce 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  UMN ){.    retur
2accf 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
2acd0 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  n 1;.}.#endif /*
2acd1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2acd2 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
2acd3 0a 2a 2a 20 49 66 20 74 68 65 20 70 42 61 73 65  .** If the pBase
2acd4 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 69 67   expression orig
2acd5 69 6e 61 74 65 64 20 69 6e 20 74 68 65 20 4f 4e  inated in the ON
2acd6 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
2acd7 20 6f 66 0a 2a 2a 20 61 20 6a 6f 69 6e 2c 20 74   of.** a join, t
2acd8 68 65 6e 20 74 72 61 6e 73 66 65 72 20 74 68 65  hen transfer the
2acd9 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d 61 72   appropriate mar
2acda 6b 69 6e 67 73 20 6f 76 65 72 20 74 6f 20 64 65  kings over to de
2acdb 72 69 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  rived..*/.static
2acdc 20 76 6f 69 64 20 74 72 61 6e 73 66 65 72 4a 6f   void transferJo
2acdd 69 6e 4d 61 72 6b 69 6e 67 73 28 45 78 70 72 20  inMarkings(Expr 
2acde 2a 70 44 65 72 69 76 65 64 2c 20 45 78 70 72 20  *pDerived, Expr 
2acdf 2a 70 42 61 73 65 29 7b 0a 20 20 70 44 65 72 69  *pBase){.  pDeri
2ace0 76 65 64 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 42  ved->flags |= pB
2ace1 61 73 65 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ase->flags & EP_
2ace2 46 72 6f 6d 4a 6f 69 6e 3b 0a 20 20 70 44 65 72  FromJoin;.  pDer
2ace3 69 76 65 64 2d 3e 69 52 69 67 68 74 4a 6f 69 6e  ived->iRightJoin
2ace4 54 61 62 6c 65 20 3d 20 70 42 61 73 65 2d 3e 69  Table = pBase->i
2ace5 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a  RightJoinTable;.
2ace6 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
2ace7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f  SQLITE_OMIT_OR_O
2ace8 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20  PTIMIZATION) && 
2ace9 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2acea 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 2f  OMIT_SUBQUERY)./
2aceb 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74  *.** Analyze a t
2acec 65 72 6d 20 74 68 61 74 20 63 6f 6e 73 69 73 74  erm that consist
2aced 73 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65  s of two or more
2acee 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a   OR-connected.**
2acef 20 73 75 62 74 65 72 6d 73 2e 20 20 53 6f 20 69   subterms.  So i
2acf0 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2e 2e 2e  n:.**.**     ...
2acf1 20 57 48 45 52 45 20 20 28 61 3d 35 29 20 41 4e   WHERE  (a=5) AN
2acf2 44 20 28 62 3d 37 20 4f 52 20 63 3d 39 20 4f 52  D (b=7 OR c=9 OR
2acf3 20 64 3d 31 33 29 20 41 4e 44 20 28 64 3d 31 33   d=13) AND (d=13
2acf4 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ).**            
2acf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5e 5e                ^^
2acf6 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e  ^^^^^^^^^^^^^^^^
2acf7 5e 5e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  ^^.**.** This ro
2acf8 75 74 69 6e 65 20 61 6e 61 6c 79 7a 65 73 20 74  utine analyzes t
2acf9 65 72 6d 73 20 73 75 63 68 20 61 73 20 74 68 65  erms such as the
2acfa 20 6d 69 64 64 6c 65 20 74 65 72 6d 20 69 6e 20   middle term in 
2acfb 74 68 65 20 61 62 6f 76 65 20 65 78 61 6d 70 6c  the above exampl
2acfc 65 2e 0a 2a 2a 20 41 20 57 68 65 72 65 4f 72 54  e..** A WhereOrT
2acfd 65 72 6d 20 6f 62 6a 65 63 74 20 69 73 20 63 6f  erm object is co
2acfe 6d 70 75 74 65 64 20 61 6e 64 20 61 74 74 61 63  mputed and attac
2acff 68 65 64 20 74 6f 20 74 68 65 20 74 65 72 6d 20  hed to the term 
2ad00 75 6e 64 65 72 0a 2a 2a 20 61 6e 61 6c 79 73 69  under.** analysi
2ad01 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  s, regardless of
2ad02 20 74 68 65 20 6f 75 74 63 6f 6d 65 20 6f 66 20   the outcome of 
2ad03 74 68 65 20 61 6e 61 6c 79 73 69 73 2e 20 20 48  the analysis.  H
2ad04 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ence:.**.**     
2ad05 57 68 65 72 65 54 65 72 6d 2e 77 74 46 6c 61 67  WhereTerm.wtFlag
2ad06 73 20 20 20 7c 3d 20 20 54 45 52 4d 5f 4f 52 49  s   |=  TERM_ORI
2ad07 4e 46 4f 0a 2a 2a 20 20 20 20 20 57 68 65 72 65  NFO.**     Where
2ad08 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 20  Term.u.pOrInfo  
2ad09 3d 20 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  =  a dynamically
2ad0a 20 61 6c 6c 6f 63 61 74 65 64 20 57 68 65 72 65   allocated Where
2ad0b 4f 72 54 65 72 6d 20 6f 62 6a 65 63 74 0a 2a 2a  OrTerm object.**
2ad0c 0a 2a 2a 20 54 68 65 20 74 65 72 6d 20 62 65 69  .** The term bei
2ad0d 6e 67 20 61 6e 61 6c 79 7a 65 64 20 6d 75 73 74  ng analyzed must
2ad0e 20 68 61 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72   have two or mor
2ad0f 65 20 6f 66 20 4f 52 2d 63 6f 6e 6e 65 63 74 65  e of OR-connecte
2ad10 64 20 73 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 41  d subterms..** A
2ad11 20 73 69 6e 67 6c 65 20 73 75 62 74 65 72 6d 20   single subterm 
2ad12 6d 69 67 68 74 20 62 65 20 61 20 73 65 74 20 6f  might be a set o
2ad13 66 20 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20  f AND-connected 
2ad14 73 75 62 2d 73 75 62 74 65 72 6d 73 2e 0a 2a 2a  sub-subterms..**
2ad15 20 45 78 61 6d 70 6c 65 73 20 6f 66 20 74 65 72   Examples of ter
2ad16 6d 73 20 75 6e 64 65 72 20 61 6e 61 6c 79 73 69  ms under analysi
2ad17 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 41 29  s:.**.**     (A)
2ad18 20 20 20 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f       t1.x=t2.y O
2ad19 52 20 74 31 2e 78 3d 74 32 2e 7a 20 4f 52 20 74  R t1.x=t2.z OR t
2ad1a 31 2e 79 3d 31 35 20 4f 52 20 74 31 2e 7a 3d 74  1.y=15 OR t1.z=t
2ad1b 33 2e 61 2b 35 0a 2a 2a 20 20 20 20 20 28 42 29  3.a+5.**     (B)
2ad1c 20 20 20 20 20 78 3d 65 78 70 72 31 20 4f 52 20       x=expr1 OR 
2ad1d 65 78 70 72 32 3d 78 20 4f 52 20 78 3d 65 78 70  expr2=x OR x=exp
2ad1e 72 33 0a 2a 2a 20 20 20 20 20 28 43 29 20 20 20  r3.**     (C)   
2ad1f 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20 28    t1.x=t2.y OR (
2ad20 74 31 2e 78 3d 74 32 2e 7a 20 41 4e 44 20 74 31  t1.x=t2.z AND t1
2ad21 2e 79 3d 31 35 29 0a 2a 2a 20 20 20 20 20 28 44  .y=15).**     (D
2ad22 29 20 20 20 20 20 78 3d 65 78 70 72 31 20 4f 52  )     x=expr1 OR
2ad23 20 28 79 3e 31 31 20 41 4e 44 20 79 3c 32 32 20   (y>11 AND y<22 
2ad24 41 4e 44 20 7a 20 4c 49 4b 45 20 27 2a 68 65 6c  AND z LIKE '*hel
2ad25 6c 6f 2a 27 29 0a 2a 2a 20 20 20 20 20 28 45 29  lo*').**     (E)
2ad26 20 20 20 20 20 28 70 2e 61 3d 31 20 41 4e 44 20       (p.a=1 AND 
2ad27 71 2e 62 3d 32 20 41 4e 44 20 72 2e 63 3d 33 29  q.b=2 AND r.c=3)
2ad28 20 4f 52 20 28 70 2e 78 3d 34 20 41 4e 44 20 71   OR (p.x=4 AND q
2ad29 2e 79 3d 35 20 41 4e 44 20 72 2e 7a 3d 36 29 0a  .y=5 AND r.z=6).
2ad2a 2a 2a 0a 2a 2a 20 43 41 53 45 20 31 3a 0a 2a 2a  **.** CASE 1:.**
2ad2b 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 62 74 65  .** If all subte
2ad2c 72 6d 73 20 61 72 65 20 6f 66 20 74 68 65 20 66  rms are of the f
2ad2d 6f 72 6d 20 54 2e 43 3d 65 78 70 72 20 66 6f 72  orm T.C=expr for
2ad2e 20 73 6f 6d 65 20 73 69 6e 67 6c 65 20 63 6f 6c   some single col
2ad2f 75 6d 6e 20 6f 66 20 43 0a 2a 2a 20 61 20 73 69  umn of C.** a si
2ad30 6e 67 6c 65 20 74 61 62 6c 65 20 54 20 28 61 73  ngle table T (as
2ad31 20 73 68 6f 77 6e 20 69 6e 20 65 78 61 6d 70 6c   shown in exampl
2ad32 65 20 42 20 61 62 6f 76 65 29 20 74 68 65 6e 20  e B above) then 
2ad33 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72  create a new vir
2ad34 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20 74 68 61  tual.** term tha
2ad35 74 20 69 73 20 61 6e 20 65 71 75 69 76 61 6c 65  t is an equivale
2ad36 6e 74 20 49 4e 20 65 78 70 72 65 73 73 69 6f 6e  nt IN expression
2ad37 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
2ad38 73 2c 20 69 66 20 74 68 65 20 74 65 72 6d 0a 2a  s, if the term.*
2ad39 2a 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64  * being analyzed
2ad3a 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   is:.**.**      
2ad3b 78 20 3d 20 65 78 70 72 31 20 20 4f 52 20 20 65  x = expr1  OR  e
2ad3c 78 70 72 32 20 3d 20 78 20 20 4f 52 20 20 78 20  xpr2 = x  OR  x 
2ad3d 3d 20 65 78 70 72 33 0a 2a 2a 0a 2a 2a 20 74 68  = expr3.**.** th
2ad3e 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  en create a new 
2ad3f 76 69 72 74 75 61 6c 20 74 65 72 6d 20 6c 69 6b  virtual term lik
2ad40 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
2ad41 20 20 20 78 20 49 4e 20 28 65 78 70 72 31 2c 65     x IN (expr1,e
2ad42 78 70 72 32 2c 65 78 70 72 33 29 0a 2a 2a 0a 2a  xpr2,expr3).**.*
2ad43 2a 20 43 41 53 45 20 32 3a 0a 2a 2a 0a 2a 2a 20  * CASE 2:.**.** 
2ad44 49 66 20 61 6c 6c 20 73 75 62 74 65 72 6d 73 20  If all subterms 
2ad45 61 72 65 20 69 6e 64 65 78 61 62 6c 65 20 62 79  are indexable by
2ad46 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   a single table 
2ad47 54 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 0a 2a  T, then set.**.*
2ad48 2a 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e  *     WhereTerm.
2ad49 65 4f 70 65 72 61 74 6f 72 20 20 20 20 20 20 20  eOperator       
2ad4a 20 20 20 20 20 20 20 3d 20 20 57 4f 5f 4f 52 0a         =  WO_OR.
2ad4b 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65 72 6d  **     WhereTerm
2ad4c 2e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65  .u.pOrInfo->inde
2ad4d 78 61 62 6c 65 20 20 7c 3d 20 20 74 68 65 20 63  xable  |=  the c
2ad4e 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
2ad4f 20 74 61 62 6c 65 20 54 0a 2a 2a 0a 2a 2a 20 41   table T.**.** A
2ad50 20 73 75 62 74 65 72 6d 20 69 73 20 22 69 6e 64   subterm is "ind
2ad51 65 78 61 62 6c 65 22 20 69 66 20 69 74 20 69 73  exable" if it is
2ad52 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 20   of the form.** 
2ad53 22 54 2e 43 20 3c 6f 70 3e 20 3c 65 78 70 72 3e  "T.C <op> <expr>
2ad54 22 20 77 68 65 72 65 20 43 20 69 73 20 61 6e 79  " where C is any
2ad55 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65   column of table
2ad56 20 54 20 61 6e 64 20 0a 2a 2a 20 3c 6f 70 3e 20   T and .** <op> 
2ad57 69 73 20 6f 6e 65 20 6f 66 20 22 3d 22 2c 20 22  is one of "=", "
2ad58 3c 22 2c 20 22 3c 3d 22 2c 20 22 3e 22 2c 20 22  <", "<=", ">", "
2ad59 3e 3d 22 2c 20 22 49 53 20 4e 55 4c 4c 22 2c 20  >=", "IS NULL", 
2ad5a 6f 72 20 22 49 4e 22 2e 0a 2a 2a 20 41 20 73 75  or "IN"..** A su
2ad5b 62 74 65 72 6d 20 69 73 20 61 6c 73 6f 20 69 6e  bterm is also in
2ad5c 64 65 78 61 62 6c 65 20 69 66 20 69 74 20 69 73  dexable if it is
2ad5d 20 61 6e 20 41 4e 44 20 6f 66 20 74 77 6f 20 6f   an AND of two o
2ad5e 72 20 6d 6f 72 65 0a 2a 2a 20 73 75 62 73 75 62  r more.** subsub
2ad5f 74 65 72 6d 73 20 61 74 20 6c 65 61 73 74 20 6f  terms at least o
2ad60 6e 65 20 6f 66 20 77 68 69 63 68 20 69 73 20 69  ne of which is i
2ad61 6e 64 65 78 61 62 6c 65 2e 20 20 49 6e 64 65 78  ndexable.  Index
2ad62 61 62 6c 65 20 41 4e 44 20 0a 2a 2a 20 73 75 62  able AND .** sub
2ad63 74 65 72 6d 73 20 68 61 76 65 20 74 68 65 69 72  terms have their
2ad64 20 65 4f 70 65 72 61 74 6f 72 20 73 65 74 20 74   eOperator set t
2ad65 6f 20 57 4f 5f 41 4e 44 20 61 6e 64 20 74 68 65  o WO_AND and the
2ad66 79 20 68 61 76 65 0a 2a 2a 20 75 2e 70 41 6e 64  y have.** u.pAnd
2ad67 49 6e 66 6f 20 73 65 74 20 74 6f 20 61 20 64 79  Info set to a dy
2ad68 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
2ad69 74 65 64 20 57 68 65 72 65 41 6e 64 54 65 72 6d  ted WhereAndTerm
2ad6a 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 46   object..**.** F
2ad6b 72 6f 6d 20 61 6e 6f 74 68 65 72 20 70 6f 69 6e  rom another poin
2ad6c 74 20 6f 66 20 76 69 65 77 2c 20 22 69 6e 64 65  t of view, "inde
2ad6d 78 61 62 6c 65 22 20 6d 65 61 6e 73 20 74 68 61  xable" means tha
2ad6e 74 20 74 68 65 20 73 75 62 74 65 72 6d 20 63 6f  t the subterm co
2ad6f 75 6c 64 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c  uld.** potential
2ad70 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20  ly be used with 
2ad71 61 6e 20 69 6e 64 65 78 20 69 66 20 61 6e 20 61  an index if an a
2ad72 70 70 72 6f 70 72 69 61 74 65 20 69 6e 64 65 78  ppropriate index
2ad73 20 65 78 69 73 74 73 2e 0a 2a 2a 20 54 68 69 73   exists..** This
2ad74 20 61 6e 61 6c 79 73 69 73 20 64 6f 65 73 20 6e   analysis does n
2ad75 6f 74 20 63 6f 6e 73 69 64 65 72 20 77 68 65 74  ot consider whet
2ad76 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69  her or not the i
2ad77 6e 64 65 78 20 65 78 69 73 74 73 3b 20 74 68 61  ndex exists; tha
2ad78 74 0a 2a 2a 20 69 73 20 73 6f 6d 65 74 68 69 6e  t.** is somethin
2ad79 67 20 74 68 65 20 62 65 73 74 49 6e 64 65 78 28  g the bestIndex(
2ad7a 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64  ) routine will d
2ad7b 65 74 65 72 6d 69 6e 65 2e 20 20 54 68 69 73 20  etermine.  This 
2ad7c 61 6e 61 6c 79 73 69 73 0a 2a 2a 20 6f 6e 6c 79  analysis.** only
2ad7d 20 6c 6f 6f 6b 73 20 61 74 20 77 68 65 74 68 65   looks at whethe
2ad7e 72 20 73 75 62 74 65 72 6d 73 20 61 70 70 72 6f  r subterms appro
2ad7f 70 72 69 61 74 65 20 66 6f 72 20 69 6e 64 65 78  priate for index
2ad80 69 6e 67 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a  ing exist..**.**
2ad81 20 41 6c 6c 20 65 78 61 6d 70 6c 65 73 20 41 20   All examples A 
2ad82 74 68 72 6f 75 67 68 20 45 20 61 62 6f 76 65 20  through E above 
2ad83 61 6c 6c 20 73 61 74 69 73 66 79 20 63 61 73 65  all satisfy case
2ad84 20 32 2e 20 20 42 75 74 20 69 66 20 61 20 74 65   2.  But if a te
2ad85 72 6d 0a 2a 2a 20 61 6c 73 6f 20 73 74 61 74 69  rm.** also stati
2ad86 73 66 69 65 73 20 63 61 73 65 20 31 20 28 73 75  sfies case 1 (su
2ad87 63 68 20 61 73 20 42 29 20 77 65 20 6b 6e 6f 77  ch as B) we know
2ad88 20 74 68 61 74 20 74 68 65 20 6f 70 74 69 6d 69   that the optimi
2ad89 7a 65 72 20 77 69 6c 6c 0a 2a 2a 20 61 6c 77 61  zer will.** alwa
2ad8a 79 73 20 70 72 65 66 65 72 20 63 61 73 65 20 31  ys prefer case 1
2ad8b 2c 20 73 6f 20 69 6e 20 74 68 61 74 20 63 61 73  , so in that cas
2ad8c 65 20 77 65 20 70 72 65 74 65 6e 64 20 74 68 61  e we pretend tha
2ad8d 74 20 63 61 73 65 20 32 20 69 73 20 6e 6f 74 0a  t case 2 is not.
2ad8e 2a 2a 20 73 61 74 69 73 66 69 65 64 2e 0a 2a 2a  ** satisfied..**
2ad8f 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20 62 65 20  .** It might be 
2ad90 74 68 65 20 63 61 73 65 20 74 68 61 74 20 6d 75  the case that mu
2ad91 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 72  ltiple tables ar
2ad92 65 20 69 6e 64 65 78 61 62 6c 65 2e 20 20 46 6f  e indexable.  Fo
2ad93 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 28 45  r example,.** (E
2ad94 29 20 61 62 6f 76 65 20 69 73 20 69 6e 64 65 78  ) above is index
2ad95 61 62 6c 65 20 6f 6e 20 74 61 62 6c 65 73 20 50  able on tables P
2ad96 2c 20 51 2c 20 61 6e 64 20 52 2e 0a 2a 2a 0a 2a  , Q, and R..**.*
2ad97 2a 20 54 65 72 6d 73 20 74 68 61 74 20 73 61 74  * Terms that sat
2ad98 69 73 66 79 20 63 61 73 65 20 32 20 61 72 65 20  isfy case 2 are 
2ad99 63 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20 6c  candidates for l
2ad9a 6f 6f 6b 75 70 20 62 79 20 75 73 69 6e 67 0a 2a  ookup by using.*
2ad9b 2a 20 73 65 70 61 72 61 74 65 20 69 6e 64 69 63  * separate indic
2ad9c 65 73 20 74 6f 20 66 69 6e 64 20 72 6f 77 69 64  es to find rowid
2ad9d 73 20 66 6f 72 20 65 61 63 68 20 73 75 62 74 65  s for each subte
2ad9e 72 6d 20 61 6e 64 20 63 6f 6d 70 6f 73 69 6e 67  rm and composing
2ad9f 0a 2a 2a 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66  .** the union of
2ada0 20 61 6c 6c 20 72 6f 77 69 64 73 20 75 73 69 6e   all rowids usin
2ada1 67 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63  g a RowSet objec
2ada2 74 2e 20 20 54 68 69 73 20 69 73 20 73 69 6d 69  t.  This is simi
2ada3 6c 61 72 0a 2a 2a 20 74 6f 20 22 62 69 74 6d 61  lar.** to "bitma
2ada4 70 20 69 6e 64 69 63 65 73 22 20 69 6e 20 6f 74  p indices" in ot
2ada5 68 65 72 20 64 61 74 61 62 61 73 65 20 65 6e 67  her database eng
2ada6 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 54 48 45  ines..**.** OTHE
2ada7 52 57 49 53 45 3a 0a 2a 2a 0a 2a 2a 20 49 66 20  RWISE:.**.** If 
2ada8 6e 65 69 74 68 65 72 20 63 61 73 65 20 31 20 6e  neither case 1 n
2ada9 6f 72 20 63 61 73 65 20 32 20 61 70 70 6c 79 2c  or case 2 apply,
2adaa 20 74 68 65 6e 20 6c 65 61 76 65 20 74 68 65 20   then leave the 
2adab 65 4f 70 65 72 61 74 6f 72 20 73 65 74 20 74 6f  eOperator set to
2adac 0a 2a 2a 20 7a 65 72 6f 2e 20 20 54 68 69 73 20  .** zero.  This 
2adad 74 65 72 6d 20 69 73 20 6e 6f 74 20 75 73 65 66  term is not usef
2adae 75 6c 20 66 6f 72 20 73 65 61 72 63 68 2e 0a 2a  ul for search..*
2adaf 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  /.static void ex
2adb0 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28  prAnalyzeOrTerm(
2adb1 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
2adb2 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2adb3 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2adb4 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
2adb5 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f   *pWC,         /
2adb6 2a 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 57  * the complete W
2adb7 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2adb8 20 69 6e 74 20 69 64 78 54 65 72 6d 20 20 20 20   int idxTerm    
2adb9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2adba 64 65 78 20 6f 66 20 74 68 65 20 4f 52 2d 74 65  dex of the OR-te
2adbb 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  rm to be analyze
2adbc 64 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20  d */.){.  Parse 
2adbd 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70  *pParse = pWC->p
2adbe 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20  Parse;          
2adbf 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
2adc0 65 78 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ext */.  sqlite3
2adc1 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2adc2 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
2adc3 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2adc4 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 57 68 65  nection */.  Whe
2adc5 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 20 3d 20  reTerm *pTerm = 
2adc6 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
2adc7 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 65 72 6d  ;    /* The term
2adc8 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20   to be analyzed 
2adc9 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
2adca 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b   = pTerm->pExpr;
2adcb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2adcc 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  The expression o
2adcd 66 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20 20  f the term */.  
2adce 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
2adcf 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d  askSet = pWC->pM
2add0 61 73 6b 53 65 74 3b 20 2f 2a 20 54 61 62 6c 65  askSet; /* Table
2add1 20 75 73 65 20 6d 61 73 6b 73 20 2a 2f 0a 20 20   use masks */.  
2add2 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
2add3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2add4 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2add5 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68  counters */.  Wh
2add6 65 72 65 43 6c 61 75 73 65 20 2a 70 4f 72 57 63  ereClause *pOrWc
2add7 3b 20 20 20 20 20 20 20 2f 2a 20 42 72 65 61 6b  ;       /* Break
2add8 75 70 20 6f 66 20 70 54 65 72 6d 20 69 6e 74 6f  up of pTerm into
2add9 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 57   subterms */.  W
2adda 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72  hereTerm *pOrTer
2addb 6d 3b 20 20 20 20 20 20 20 2f 2a 20 41 20 53 75  m;       /* A Su
2addc 62 2d 74 65 72 6d 20 77 69 74 68 69 6e 20 74 68  b-term within th
2addd 65 20 70 4f 72 57 63 20 2a 2f 0a 20 20 57 68 65  e pOrWc */.  Whe
2adde 72 65 4f 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66  reOrInfo *pOrInf
2addf 6f 3b 20 20 20 20 20 2f 2a 20 41 64 64 69 74 69  o;     /* Additi
2ade0 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
2ade1 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2ade2 20 70 54 65 72 6d 20 2a 2f 0a 20 20 42 69 74 6d   pTerm */.  Bitm
2ade3 61 73 6b 20 63 68 6e 67 54 6f 49 4e 3b 20 20 20  ask chngToIN;   
2ade4 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 73 20        /* Tables 
2ade5 74 68 61 74 20 6d 69 67 68 74 20 73 61 74 69 73  that might satis
2ade6 66 79 20 63 61 73 65 20 31 20 2a 2f 0a 20 20 42  fy case 1 */.  B
2ade7 69 74 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c 65  itmask indexable
2ade8 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c  ;        /* Tabl
2ade9 65 73 20 74 68 61 74 20 61 72 65 20 69 6e 64 65  es that are inde
2adea 78 61 62 6c 65 2c 20 73 61 74 69 73 66 79 69 6e  xable, satisfyin
2adeb 67 20 63 61 73 65 20 32 20 2a 2f 0a 0a 20 20 2f  g case 2 */..  /
2adec 2a 0a 20 20 2a 2a 20 42 72 65 61 6b 20 74 68 65  *.  ** Break the
2aded 20 4f 52 20 63 6c 61 75 73 65 20 69 6e 74 6f 20   OR clause into 
2adee 69 74 73 20 73 65 70 61 72 61 74 65 20 73 75 62  its separate sub
2adef 74 65 72 6d 73 2e 20 20 54 68 65 20 73 75 62 74  terms.  The subt
2adf0 65 72 6d 73 20 61 72 65 0a 20 20 2a 2a 20 73 74  erms are.  ** st
2adf1 6f 72 65 64 20 69 6e 20 61 20 57 68 65 72 65 43  ored in a WhereC
2adf2 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65 20  lause structure 
2adf3 63 6f 6e 74 61 69 6e 69 6e 67 20 77 69 74 68 69  containing withi
2adf4 6e 20 74 68 65 20 57 68 65 72 65 4f 72 49 6e 66  n the WhereOrInf
2adf5 6f 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 20 74 68  o.  ** object th
2adf6 61 74 20 69 73 20 61 74 74 61 63 68 65 64 20 74  at is attached t
2adf7 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 4f  o the original O
2adf8 52 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 0a 20  R clause term.. 
2adf9 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70   */.  assert( (p
2adfa 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2adfb 28 54 45 52 4d 5f 44 59 4e 41 4d 49 43 7c 54 45  (TERM_DYNAMIC|TE
2adfc 52 4d 5f 4f 52 49 4e 46 4f 7c 54 45 52 4d 5f 41  RM_ORINFO|TERM_A
2adfd 4e 44 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20  NDINFO))==0 );. 
2adfe 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
2adff 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 70  op==TK_OR );.  p
2ae00 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 20  Term->u.pOrInfo 
2ae01 3d 20 70 4f 72 49 6e 66 6f 20 3d 20 73 71 6c 69  = pOrInfo = sqli
2ae02 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2ae03 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 72 49  db, sizeof(*pOrI
2ae04 6e 66 6f 29 29 3b 0a 20 20 69 66 28 20 70 4f 72  nfo));.  if( pOr
2ae05 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e  Info==0 ) return
2ae06 3b 0a 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61  ;.  pTerm->wtFla
2ae07 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52 49 4e 46  gs |= TERM_ORINF
2ae08 4f 3b 0a 20 20 70 4f 72 57 63 20 3d 20 26 70 4f  O;.  pOrWc = &pO
2ae09 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 77 68 65  rInfo->wc;.  whe
2ae0a 72 65 43 6c 61 75 73 65 49 6e 69 74 28 70 4f 72  reClauseInit(pOr
2ae0b 57 63 2c 20 70 57 43 2d 3e 70 50 61 72 73 65 2c  Wc, pWC->pParse,
2ae0c 20 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 77 68   pMaskSet);.  wh
2ae0d 65 72 65 53 70 6c 69 74 28 70 4f 72 57 63 2c 20  ereSplit(pOrWc, 
2ae0e 70 45 78 70 72 2c 20 54 4b 5f 4f 52 29 3b 0a 20  pExpr, TK_OR);. 
2ae0f 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28   exprAnalyzeAll(
2ae10 70 53 72 63 2c 20 70 4f 72 57 63 29 3b 0a 20 20  pSrc, pOrWc);.  
2ae11 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2ae12 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
2ae13 20 61 73 73 65 72 74 28 20 70 4f 72 57 63 2d 3e   assert( pOrWc->
2ae14 6e 54 65 72 6d 3e 3d 32 20 29 3b 0a 0a 20 20 2f  nTerm>=2 );..  /
2ae15 2a 0a 20 20 2a 2a 20 43 6f 6d 70 75 74 65 20 74  *.  ** Compute t
2ae16 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c 65 73  he set of tables
2ae17 20 74 68 61 74 20 6d 69 67 68 74 20 73 61 74 69   that might sati
2ae18 73 66 79 20 63 61 73 65 73 20 31 20 6f 72 20 32  sfy cases 1 or 2
2ae19 2e 0a 20 20 2a 2f 0a 20 20 69 6e 64 65 78 61 62  ..  */.  indexab
2ae1a 6c 65 20 3d 20 63 68 6e 67 54 6f 49 4e 20 3d 20  le = chngToIN = 
2ae1b 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 66  ~(Bitmask)0;.  f
2ae1c 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72  or(i=pOrWc->nTer
2ae1d 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72  m-1, pOrTerm=pOr
2ae1e 57 63 2d 3e 61 3b 20 69 3e 3d 30 20 26 26 20 69  Wc->a; i>=0 && i
2ae1f 6e 64 65 78 61 62 6c 65 3b 20 69 2d 2d 2c 20 70  ndexable; i--, p
2ae20 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69  OrTerm++){.    i
2ae21 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e 65 4f 70  f( (pOrTerm->eOp
2ae22 65 72 61 74 6f 72 20 26 20 57 4f 5f 53 49 4e 47  erator & WO_SING
2ae23 4c 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  LE)==0 ){.      
2ae24 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 41  WhereAndInfo *pA
2ae25 6e 64 49 6e 66 6f 3b 0a 20 20 20 20 20 20 61 73  ndInfo;.      as
2ae26 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65  sert( pOrTerm->e
2ae27 4f 70 65 72 61 74 6f 72 3d 3d 30 20 29 3b 0a 20  Operator==0 );. 
2ae28 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4f       assert( (pO
2ae29 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2ae2a 20 28 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 7c 54   (TERM_ANDINFO|T
2ae2b 45 52 4d 5f 4f 52 49 4e 46 4f 29 29 3d 3d 30 20  ERM_ORINFO))==0 
2ae2c 29 3b 0a 20 20 20 20 20 20 63 68 6e 67 54 6f 49  );.      chngToI
2ae2d 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 41 6e  N = 0;.      pAn
2ae2e 64 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44  dInfo = sqlite3D
2ae2f 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
2ae30 69 7a 65 6f 66 28 2a 70 41 6e 64 49 6e 66 6f 29  izeof(*pAndInfo)
2ae31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 6e  );.      if( pAn
2ae32 64 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20  dInfo ){.       
2ae33 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 41   WhereClause *pA
2ae34 6e 64 57 43 3b 0a 20 20 20 20 20 20 20 20 57 68  ndWC;.        Wh
2ae35 65 72 65 54 65 72 6d 20 2a 70 41 6e 64 54 65 72  ereTerm *pAndTer
2ae36 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a  m;.        int j
2ae37 3b 0a 20 20 20 20 20 20 20 20 42 69 74 6d 61 73  ;.        Bitmas
2ae38 6b 20 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  k b = 0;.       
2ae39 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64   pOrTerm->u.pAnd
2ae3a 49 6e 66 6f 20 3d 20 70 41 6e 64 49 6e 66 6f 3b  Info = pAndInfo;
2ae3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d  .        pOrTerm
2ae3c 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52  ->wtFlags |= TER
2ae3d 4d 5f 41 4e 44 49 4e 46 4f 3b 0a 20 20 20 20 20  M_ANDINFO;.     
2ae3e 20 20 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65     pOrTerm->eOpe
2ae3f 72 61 74 6f 72 20 3d 20 57 4f 5f 41 4e 44 3b 0a  rator = WO_AND;.
2ae40 20 20 20 20 20 20 20 20 70 41 6e 64 57 43 20 3d          pAndWC =
2ae41 20 26 70 41 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a   &pAndInfo->wc;.
2ae42 20 20 20 20 20 20 20 20 77 68 65 72 65 43 6c 61          whereCla
2ae43 75 73 65 49 6e 69 74 28 70 41 6e 64 57 43 2c 20  useInit(pAndWC, 
2ae44 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61  pWC->pParse, pMa
2ae45 73 6b 53 65 74 29 3b 0a 20 20 20 20 20 20 20 20  skSet);.        
2ae46 77 68 65 72 65 53 70 6c 69 74 28 70 41 6e 64 57  whereSplit(pAndW
2ae47 43 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70  C, pOrTerm->pExp
2ae48 72 2c 20 54 4b 5f 41 4e 44 29 3b 0a 20 20 20 20  r, TK_AND);.    
2ae49 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41      exprAnalyzeA
2ae4a 6c 6c 28 70 53 72 63 2c 20 70 41 6e 64 57 43 29  ll(pSrc, pAndWC)
2ae4b 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
2ae4c 73 65 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se( db->mallocFa
2ae4d 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  iled );.        
2ae4e 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
2ae4f 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
2ae50 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 41 6e 64     for(j=0, pAnd
2ae51 54 65 72 6d 3d 70 41 6e 64 57 43 2d 3e 61 3b 20  Term=pAndWC->a; 
2ae52 6a 3c 70 41 6e 64 57 43 2d 3e 6e 54 65 72 6d 3b  j<pAndWC->nTerm;
2ae53 20 6a 2b 2b 2c 20 70 41 6e 64 54 65 72 6d 2b 2b   j++, pAndTerm++
2ae54 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
2ae55 73 73 65 72 74 28 20 70 41 6e 64 54 65 72 6d 2d  ssert( pAndTerm-
2ae56 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20  >pExpr );.      
2ae57 20 20 20 20 20 20 69 66 28 20 61 6c 6c 6f 77 65        if( allowe
2ae58 64 4f 70 28 70 41 6e 64 54 65 72 6d 2d 3e 70 45  dOp(pAndTerm->pE
2ae59 78 70 72 2d 3e 6f 70 29 20 29 7b 0a 20 20 20 20  xpr->op) ){.    
2ae5a 20 20 20 20 20 20 20 20 20 20 62 20 7c 3d 20 67            b |= g
2ae5b 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  etMask(pMaskSet,
2ae5c 20 70 41 6e 64 54 65 72 6d 2d 3e 6c 65 66 74 43   pAndTerm->leftC
2ae5d 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20  ursor);.        
2ae5e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2ae5f 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
2ae60 20 20 20 20 69 6e 64 65 78 61 62 6c 65 20 26 3d      indexable &=
2ae61 20 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   b;.      }.    
2ae62 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72  }else if( pOrTer
2ae63 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
2ae64 4d 5f 43 4f 50 49 45 44 20 29 7b 0a 20 20 20 20  M_COPIED ){.    
2ae65 20 20 2f 2a 20 53 6b 69 70 20 74 68 69 73 20 74    /* Skip this t
2ae66 65 72 6d 20 66 6f 72 20 6e 6f 77 2e 20 20 57 65  erm for now.  We
2ae67 20 72 65 76 69 73 69 74 20 69 74 20 77 68 65 6e   revisit it when
2ae68 20 77 65 20 70 72 6f 63 65 73 73 20 74 68 65 0a   we process the.
2ae69 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 73 70        ** corresp
2ae6a 6f 6e 64 69 6e 67 20 54 45 52 4d 5f 56 49 52 54  onding TERM_VIRT
2ae6b 55 41 4c 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20  UAL term */.    
2ae6c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 42 69 74  }else{.      Bit
2ae6d 6d 61 73 6b 20 62 3b 0a 20 20 20 20 20 20 62 20  mask b;.      b 
2ae6e 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53  = getMask(pMaskS
2ae6f 65 74 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66  et, pOrTerm->lef
2ae70 74 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20  tCursor);.      
2ae71 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46  if( pOrTerm->wtF
2ae72 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54  lags & TERM_VIRT
2ae73 55 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 57  UAL ){.        W
2ae74 68 65 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72  hereTerm *pOther
2ae75 20 3d 20 26 70 4f 72 57 63 2d 3e 61 5b 70 4f 72   = &pOrWc->a[pOr
2ae76 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a  Term->iParent];.
2ae77 20 20 20 20 20 20 20 20 62 20 7c 3d 20 67 65 74          b |= get
2ae78 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
2ae79 4f 74 68 65 72 2d 3e 6c 65 66 74 43 75 72 73 6f  Other->leftCurso
2ae7a 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
2ae7b 20 20 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62    indexable &= b
2ae7c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 54  ;.      if( pOrT
2ae7d 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d  erm->eOperator!=
2ae7e 57 4f 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 20  WO_EQ ){.       
2ae7f 20 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20   chngToIN = 0;. 
2ae80 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2ae81 20 20 20 20 63 68 6e 67 54 6f 49 4e 20 26 3d 20      chngToIN &= 
2ae82 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  b;.      }.    }
2ae83 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
2ae84 52 65 63 6f 72 64 20 74 68 65 20 73 65 74 20 6f  Record the set o
2ae85 66 20 74 61 62 6c 65 73 20 74 68 61 74 20 73 61  f tables that sa
2ae86 74 69 73 66 79 20 63 61 73 65 20 32 2e 20 20 54  tisfy case 2.  T
2ae87 68 65 20 73 65 74 20 6d 69 67 68 74 20 62 65 0a  he set might be.
2ae88 20 20 2a 2a 20 65 6d 70 74 79 2e 0a 20 20 2a 2f    ** empty..  */
2ae89 0a 20 20 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65  .  pOrInfo->inde
2ae8a 78 61 62 6c 65 20 3d 20 69 6e 64 65 78 61 62 6c  xable = indexabl
2ae8b 65 3b 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65  e;.  pTerm->eOpe
2ae8c 72 61 74 6f 72 20 3d 20 69 6e 64 65 78 61 62 6c  rator = indexabl
2ae8d 65 3d 3d 30 20 3f 20 30 20 3a 20 57 4f 5f 4f 52  e==0 ? 0 : WO_OR
2ae8e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 68 6e  ;..  /*.  ** chn
2ae8f 67 54 6f 49 4e 20 68 6f 6c 64 73 20 61 20 73 65  gToIN holds a se
2ae90 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74  t of tables that
2ae91 20 2a 6d 69 67 68 74 2a 20 73 61 74 69 73 66 79   *might* satisfy
2ae92 20 63 61 73 65 20 31 2e 20 20 42 75 74 0a 20 20   case 1.  But.  
2ae93 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f  ** we have to do
2ae94 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c   some additional
2ae95 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 73 65 65   checking to see
2ae96 20 69 66 20 63 61 73 65 20 31 20 72 65 61 6c 6c   if case 1 reall
2ae97 79 0a 20 20 2a 2a 20 69 73 20 73 61 74 69 73 66  y.  ** is satisf
2ae98 69 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ied..  */.  if( 
2ae99 63 68 6e 67 54 6f 49 4e 20 29 7b 0a 20 20 20 20  chngToIN ){.    
2ae9a 69 6e 74 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e  int okToChngToIN
2ae9b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75   = 0;     /* Tru
2ae9c 65 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73  e if the convers
2ae9d 69 6f 6e 20 74 6f 20 49 4e 20 69 73 20 76 61 6c  ion to IN is val
2ae9e 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  id */.    int iC
2ae9f 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 20 20  olumn = -1;     
2aea0 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e      /* Column in
2aea1 64 65 78 20 6f 6e 20 6c 68 73 20 6f 66 20 49 4e  dex on lhs of IN
2aea2 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20   operator */.   
2aea3 20 69 6e 74 20 69 43 75 72 73 6f 72 20 3d 20 2d   int iCursor = -
2aea4 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  1;         /* Ta
2aea5 62 6c 65 20 63 75 72 73 6f 72 20 63 6f 6d 6d 6f  ble cursor commo
2aea6 6e 20 74 6f 20 61 6c 6c 20 74 65 72 6d 73 20 2a  n to all terms *
2aea7 2f 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 30 3b  /.    int j = 0;
2aea8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aea9 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
2aeaa 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 61 72 63  */..    /* Searc
2aeab 68 20 66 6f 72 20 61 20 74 61 62 6c 65 20 61 6e  h for a table an
2aeac 64 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70  d column that ap
2aead 70 65 61 72 73 20 6f 6e 20 6f 6e 65 20 73 69 64  pears on one sid
2aeae 65 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  e or the.    ** 
2aeaf 6f 74 68 65 72 20 6f 66 20 74 68 65 20 3d 3d 20  other of the == 
2aeb0 6f 70 65 72 61 74 6f 72 20 69 6e 20 65 76 65 72  operator in ever
2aeb1 79 20 73 75 62 74 65 72 6d 2e 20 20 54 68 61 74  y subterm.  That
2aeb2 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
2aeb3 6e 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65  n.    ** will be
2aeb4 20 72 65 63 6f 72 64 65 64 20 69 6e 20 69 43 75   recorded in iCu
2aeb5 72 73 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d 6e  rsor and iColumn
2aeb6 2e 20 20 54 68 65 72 65 20 6d 69 67 68 74 20 6e  .  There might n
2aeb7 6f 74 20 62 65 20 61 6e 79 0a 20 20 20 20 2a 2a  ot be any.    **
2aeb8 20 73 75 63 68 20 74 61 62 6c 65 20 61 6e 64 20   such table and 
2aeb9 63 6f 6c 75 6d 6e 2e 20 20 53 65 74 20 6f 6b 54  column.  Set okT
2aeba 6f 43 68 6e 67 54 6f 49 4e 20 69 66 20 61 6e 20  oChngToIN if an 
2aebb 61 70 70 72 6f 70 72 69 61 74 65 20 74 61 62 6c  appropriate tabl
2aebc 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 6c  e.    ** and col
2aebd 75 6d 6e 20 69 73 20 66 6f 75 6e 64 20 62 75 74  umn is found but
2aebe 20 6c 65 61 76 65 20 6f 6b 54 6f 43 68 6e 67 54   leave okToChngT
2aebf 6f 49 4e 20 66 61 6c 73 65 20 69 66 20 6e 6f 74  oIN false if not
2aec0 20 66 6f 75 6e 64 2e 0a 20 20 20 20 2a 2f 0a 20   found..    */. 
2aec1 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 32 20     for(j=0; j<2 
2aec2 26 26 20 21 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e  && !okToChngToIN
2aec3 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 4f  ; j++){.      pO
2aec4 72 54 65 72 6d 20 3d 20 70 4f 72 57 63 2d 3e 61  rTerm = pOrWc->a
2aec5 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4f  ;.      for(i=pO
2aec6 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e  rWc->nTerm-1; i>
2aec7 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d  =0; i--, pOrTerm
2aec8 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  ++){.        ass
2aec9 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f  ert( pOrTerm->eO
2aeca 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29  perator==WO_EQ )
2aecb 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65 72  ;.        pOrTer
2aecc 6d 2d 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54  m->wtFlags &= ~T
2aecd 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20  ERM_OR_OK;.     
2aece 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e     if( pOrTerm->
2aecf 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 6f 6c  leftCursor==iCol
2aed0 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  umn ) continue;.
2aed1 20 20 20 20 20 20 20 20 69 66 28 20 28 63 68 6e          if( (chn
2aed2 67 54 6f 49 4e 20 26 20 67 65 74 4d 61 73 6b 28  gToIN & getMask(
2aed3 70 4d 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72  pMaskSet, pOrTer
2aed4 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 29 3d  m->leftCursor))=
2aed5 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
2aed6 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d         iColumn =
2aed7 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74   pOrTerm->u.left
2aed8 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20  Column;.        
2aed9 69 43 75 72 73 6f 72 20 3d 20 70 4f 72 54 65 72  iCursor = pOrTer
2aeda 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3b 0a 20  m->leftCursor;. 
2aedb 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2aedc 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2aedd 69 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  i<0 ){.        a
2aede 73 73 65 72 74 28 20 6a 3d 3d 31 20 29 3b 0a 20  ssert( j==1 );. 
2aedf 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28         assert( (
2aee0 63 68 6e 67 54 6f 49 4e 26 28 63 68 6e 67 54 6f  chngToIN&(chngTo
2aee1 49 4e 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20  IN-1))==0 );.   
2aee2 20 20 20 20 20 61 73 73 65 72 74 28 20 63 68 6e       assert( chn
2aee3 67 54 6f 49 4e 3d 3d 67 65 74 4d 61 73 6b 28 70  gToIN==getMask(p
2aee4 4d 61 73 6b 53 65 74 2c 20 69 43 6f 6c 75 6d 6e  MaskSet, iColumn
2aee5 29 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ) );.        bre
2aee6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2aee7 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d    okToChngToIN =
2aee8 20 31 3b 0a 20 20 20 20 20 20 66 6f 72 28 3b 20   1;.      for(; 
2aee9 69 3e 3d 30 20 26 26 20 6f 6b 54 6f 43 68 6e 67  i>=0 && okToChng
2aeea 54 6f 49 4e 3b 20 69 2d 2d 2c 20 70 4f 72 54 65  ToIN; i--, pOrTe
2aeeb 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61  rm++){.        a
2aeec 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e  ssert( pOrTerm->
2aeed 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51  eOperator==WO_EQ
2aeee 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
2aeef 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2aef0 73 6f 72 21 3d 69 43 75 72 73 6f 72 20 29 7b 0a  sor!=iCursor ){.
2aef1 20 20 20 20 20 20 20 20 20 20 70 4f 72 54 65 72            pOrTer
2aef2 6d 2d 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54  m->wtFlags &= ~T
2aef3 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20  ERM_OR_OK;.     
2aef4 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72     }else if( pOr
2aef5 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75  Term->u.leftColu
2aef6 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  mn!=iColumn ){. 
2aef7 20 20 20 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e           okToChn
2aef8 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20  gToIN = 0;.     
2aef9 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2aefa 20 20 20 20 69 6e 74 20 61 66 66 4c 65 66 74 2c      int affLeft,
2aefb 20 61 66 66 52 69 67 68 74 3b 0a 20 20 20 20 20   affRight;.     
2aefc 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72       /* If the r
2aefd 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69  ight-hand side i
2aefe 73 20 61 6c 73 6f 20 61 20 63 6f 6c 75 6d 6e 2c  s also a column,
2aeff 20 74 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69   then the affini
2af00 74 69 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a  ties.          *
2af01 2a 20 6f 66 20 62 6f 74 68 20 72 69 67 68 74 20  * of both right 
2af02 61 6e 64 20 6c 65 66 74 20 73 69 64 65 73 20 6d  and left sides m
2af03 75 73 74 20 62 65 20 73 75 63 68 20 74 68 61 74  ust be such that
2af04 20 6e 6f 20 74 79 70 65 0a 20 20 20 20 20 20 20   no type.       
2af05 20 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e     ** conversion
2af06 73 20 61 72 65 20 72 65 71 75 69 72 65 64 20 6f  s are required o
2af07 6e 20 74 68 65 20 72 69 67 68 74 2e 20 20 28 54  n the right.  (T
2af08 69 63 6b 65 74 20 23 32 32 34 39 29 0a 20 20 20  icket #2249).   
2af09 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2af0a 20 20 20 20 61 66 66 52 69 67 68 74 20 3d 20 73      affRight = s
2af0b 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
2af0c 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70  ty(pOrTerm->pExp
2af0d 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20  r->pRight);.    
2af0e 20 20 20 20 20 20 61 66 66 4c 65 66 74 20 3d 20        affLeft = 
2af0f 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
2af10 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78  ity(pOrTerm->pEx
2af11 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20  pr->pLeft);.    
2af12 20 20 20 20 20 20 69 66 28 20 61 66 66 52 69 67        if( affRig
2af13 68 74 21 3d 30 20 26 26 20 61 66 66 52 69 67 68  ht!=0 && affRigh
2af14 74 21 3d 61 66 66 4c 65 66 74 20 29 7b 0a 20 20  t!=affLeft ){.  
2af15 20 20 20 20 20 20 20 20 20 20 6f 6b 54 6f 43 68            okToCh
2af16 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20  ngToIN = 0;.    
2af17 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2af18 20 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d           pOrTerm
2af19 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52  ->wtFlags |= TER
2af1a 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
2af1b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
2af1c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2af1d 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69    /* At this poi
2af1e 6e 74 2c 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e  nt, okToChngToIN
2af1f 20 69 73 20 74 72 75 65 20 69 66 20 6f 72 69 67   is true if orig
2af20 69 6e 61 6c 20 70 54 65 72 6d 20 73 61 74 69 73  inal pTerm satis
2af21 66 69 65 73 0a 20 20 20 20 2a 2a 20 63 61 73 65  fies.    ** case
2af22 20 31 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73   1.  In that cas
2af23 65 2c 20 63 6f 6e 73 74 72 75 63 74 20 61 20 6e  e, construct a n
2af24 65 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d 20  ew virtual term 
2af25 74 68 61 74 20 69 73 20 0a 20 20 20 20 2a 2a 20  that is .    ** 
2af26 70 54 65 72 6d 20 63 6f 6e 76 65 72 74 65 64 20  pTerm converted 
2af27 69 6e 74 6f 20 61 6e 20 49 4e 20 6f 70 65 72 61  into an IN opera
2af28 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  tor..    */.    
2af29 69 66 28 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e  if( okToChngToIN
2af2a 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
2af2b 70 44 75 70 3b 20 20 20 20 20 20 20 20 20 20 20  pDup;           
2af2c 20 2f 2a 20 41 20 74 72 61 6e 73 69 65 6e 74 20   /* A transient 
2af2d 64 75 70 6c 69 63 61 74 65 20 65 78 70 72 65 73  duplicate expres
2af2e 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78  sion */.      Ex
2af2f 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  prList *pList = 
2af30 30 3b 20 20 20 2f 2a 20 54 68 65 20 52 48 53 20  0;   /* The RHS 
2af31 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74  of the IN operat
2af32 6f 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  or */.      Expr
2af33 20 2a 70 4c 65 66 74 20 3d 20 30 3b 20 20 20 20   *pLeft = 0;    
2af34 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66     /* The LHS of
2af35 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
2af36 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
2af37 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  pNew;           
2af38 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65   /* The complete
2af39 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a   IN operator */.
2af3a 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4f 72  .      for(i=pOr
2af3b 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72  Wc->nTerm-1, pOr
2af3c 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b 20 69  Term=pOrWc->a; i
2af3d 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72  >=0; i--, pOrTer
2af3e 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  m++){.        if
2af3f 28 20 28 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c  ( (pOrTerm->wtFl
2af40 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b  ags & TERM_OR_OK
2af41 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )==0 ) continue;
2af42 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2af43 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2af44 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20  tor==WO_EQ );.  
2af45 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
2af46 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2af47 72 3d 3d 69 43 75 72 73 6f 72 20 29 3b 0a 20 20  r==iCursor );.  
2af48 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
2af49 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  rTerm->u.leftCol
2af4a 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29 3b 0a  umn==iColumn );.
2af4b 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73          pDup = s
2af4c 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2af4d 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  , pOrTerm->pExpr
2af4e 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20  ->pRight, 0);.  
2af4f 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71        pList = sq
2af50 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2af51 65 6e 64 28 70 57 43 2d 3e 70 50 61 72 73 65 2c  end(pWC->pParse,
2af52 20 70 4c 69 73 74 2c 20 70 44 75 70 2c 20 30 29   pList, pDup, 0)
2af53 3b 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 20  ;.        pLeft 
2af54 3d 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  = pOrTerm->pExpr
2af55 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 7d  ->pLeft;.      }
2af56 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2af57 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 20 20 20  Left!=0 );.     
2af58 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
2af59 78 70 72 44 75 70 28 64 62 2c 20 70 4c 65 66 74  xprDup(db, pLeft
2af5a 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4e 65 77  , 0);.      pNew
2af5b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2af5c 62 2c 20 54 4b 5f 49 4e 2c 20 70 44 75 70 2c 20  b, TK_IN, pDup, 
2af5d 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  0, 0);.      if(
2af5e 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20   pNew ){.       
2af5f 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20   int idxNew;.   
2af60 20 20 20 20 20 74 72 61 6e 73 66 65 72 4a 6f 69       transferJoi
2af61 6e 4d 61 72 6b 69 6e 67 73 28 70 4e 65 77 2c 20  nMarkings(pNew, 
2af62 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  pExpr);.        
2af63 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2af64 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
2af65 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2af66 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e          pNew->x.
2af67 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  pList = pList;. 
2af68 20 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20         idxNew = 
2af69 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
2af6a 74 28 70 57 43 2c 20 70 4e 65 77 2c 20 54 45 52  t(pWC, pNew, TER
2af6b 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2af6c 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20  YNAMIC);.       
2af6d 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65   testcase( idxNe
2af6e 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  w==0 );.        
2af6f 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63  exprAnalyze(pSrc
2af70 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a  , pWC, idxNew);.
2af71 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20          pTerm = 
2af72 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
2af73 3b 0a 20 20 20 20 20 20 20 20 70 57 43 2d 3e 61  ;.        pWC->a
2af74 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74  [idxNew].iParent
2af75 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
2af76 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
2af77 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  d = 1;.      }el
2af78 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2af79 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2af7a 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
2af7b 20 20 20 20 7d 0a 20 20 20 20 20 20 70 54 65 72      }.      pTer
2af7c 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30  m->eOperator = 0
2af7d 3b 20 20 2f 2a 20 63 61 73 65 20 31 20 74 72 75  ;  /* case 1 tru
2af7e 6d 70 73 20 63 61 73 65 20 32 20 2a 2f 0a 20 20  mps case 2 */.  
2af7f 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
2af80 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
2af81 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  _OR_OPTIMIZATION
2af82 20 26 26 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   && !SQLITE_OMIT
2af83 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 0a 2f  _SUBQUERY */.../
2af84 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74  *.** The input t
2af85 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
2af86 73 20 61 6e 20 57 68 65 72 65 54 65 72 6d 20 73  s an WhereTerm s
2af87 74 72 75 63 74 75 72 65 20 77 69 74 68 20 6f 6e  tructure with on
2af88 6c 79 20 74 68 65 0a 2a 2a 20 22 70 45 78 70 72  ly the.** "pExpr
2af89 22 20 66 69 65 6c 64 20 66 69 6c 6c 65 64 20 69  " field filled i
2af8a 6e 2e 20 20 54 68 65 20 6a 6f 62 20 6f 66 20 74  n.  The job of t
2af8b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
2af8c 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a 2a 2a  o analyze the.**
2af8d 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 61   subexpression a
2af8e 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 6c 6c 20  nd populate all 
2af8f 74 68 65 20 6f 74 68 65 72 20 66 69 65 6c 64 73  the other fields
2af90 20 6f 66 20 74 68 65 20 57 68 65 72 65 54 65 72   of the WhereTer
2af91 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  m.** structure..
2af92 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
2af93 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68  ression is of th
2af94 65 20 66 6f 72 6d 20 22 3c 65 78 70 72 3e 20 3c  e form "<expr> <
2af95 6f 70 3e 20 58 22 20 69 74 20 67 65 74 73 20 63  op> X" it gets c
2af96 6f 6d 6d 75 74 65 64 0a 2a 2a 20 74 6f 20 74 68  ommuted.** to th
2af97 65 20 73 74 61 6e 64 61 72 64 20 66 6f 72 6d 20  e standard form 
2af98 6f 66 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72  of "X <op> <expr
2af99 3e 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  >"..**.** If the
2af9a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
2af9b 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f  f the form "X <o
2af9c 70 3e 20 59 22 20 77 68 65 72 65 20 62 6f 74 68  p> Y" where both
2af9d 20 58 20 61 6e 64 20 59 20 61 72 65 0a 2a 2a 20   X and Y are.** 
2af9e 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 74 68  columns, then th
2af9f 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65  e original expre
2afa0 73 73 69 6f 6e 20 69 73 20 75 6e 63 68 61 6e 67  ssion is unchang
2afa1 65 64 20 61 6e 64 20 61 20 6e 65 77 20 76 69 72  ed and a new vir
2afa2 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20 6f 66 20  tual.** term of 
2afa3 74 68 65 20 66 6f 72 6d 20 22 59 20 3c 6f 70 3e  the form "Y <op>
2afa4 20 58 22 20 69 73 20 61 64 64 65 64 20 74 6f 20   X" is added to 
2afa5 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2afa6 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64   and.** analyzed
2afa7 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20 54 68   separately.  Th
2afa8 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 20  e original term 
2afa9 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 54  is marked with T
2afaa 45 52 4d 5f 43 4f 50 49 45 44 0a 2a 2a 20 61 6e  ERM_COPIED.** an
2afab 64 20 74 68 65 20 6e 65 77 20 74 65 72 6d 20 69  d the new term i
2afac 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45  s marked with TE
2afad 52 4d 5f 44 59 4e 41 4d 49 43 20 28 62 65 63 61  RM_DYNAMIC (beca
2afae 75 73 65 20 69 74 27 73 20 70 45 78 70 72 0a 2a  use it's pExpr.*
2afaf 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72  * needs to be fr
2afb0 65 65 64 20 77 69 74 68 20 74 68 65 20 57 68 65  eed with the Whe
2afb1 72 65 43 6c 61 75 73 65 29 20 61 6e 64 20 54 45  reClause) and TE
2afb2 52 4d 5f 56 49 52 54 55 41 4c 20 28 62 65 63 61  RM_VIRTUAL (beca
2afb3 75 73 65 20 69 74 0a 2a 2a 20 69 73 20 61 20 63  use it.** is a c
2afb4 6f 6d 6d 75 74 65 64 20 63 6f 70 79 20 6f 66 20  ommuted copy of 
2afb5 61 20 70 72 69 6f 72 20 74 65 72 6d 2e 29 20 20  a prior term.)  
2afb6 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72  The original ter
2afb7 6d 20 68 61 73 20 6e 43 68 69 6c 64 3d 31 0a 2a  m has nChild=1.*
2afb8 2a 20 61 6e 64 20 74 68 65 20 63 6f 70 79 20 68  * and the copy h
2afb9 61 73 20 69 64 78 50 61 72 65 6e 74 20 73 65 74  as idxParent set
2afba 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
2afbb 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65   the original te
2afbc 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rm..*/.static vo
2afbd 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 0a  id exprAnalyze(.
2afbe 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
2afbf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74              /* t
2afc0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
2afc1 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2afc2 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f 2a  *pWC,         /*
2afc3 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2afc4 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 54 65  e */.  int idxTe
2afc5 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rm              
2afc6 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
2afc7 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c   term to be anal
2afc8 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 57 68 65  yzed */.){.  Whe
2afc9 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
2afca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2afcb 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   The term to be 
2afcc 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 57 68  analyzed */.  Wh
2afcd 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2afce 6b 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f  kSet;          /
2afcf 2a 20 53 65 74 20 6f 66 20 74 61 62 6c 65 20 69  * Set of table i
2afd0 6e 64 65 78 20 6d 61 73 6b 73 20 2a 2f 0a 20 20  ndex masks */.  
2afd1 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20  Expr *pExpr;    
2afd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2afd3 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
2afd4 6f 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  on to be analyze
2afd5 64 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70  d */.  Bitmask p
2afd6 72 65 72 65 71 4c 65 66 74 3b 20 20 20 20 20 20  rereqLeft;      
2afd7 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 72 65          /* Prere
2afd8 71 75 65 73 69 74 65 73 20 6f 66 20 74 68 65 20  quesites of the 
2afd9 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 2a 2f 0a  pExpr->pLeft */.
2afda 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71    Bitmask prereq
2afdb 41 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  All;            
2afdc 20 20 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69     /* Prerequesi
2afdd 74 65 73 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a  tes of pExpr */.
2afde 20 20 42 69 74 6d 61 73 6b 20 65 78 74 72 61 52    Bitmask extraR
2afdf 69 67 68 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ight = 0;.  int 
2afe0 6e 50 61 74 74 65 72 6e 3b 0a 20 20 69 6e 74 20  nPattern;.  int 
2afe1 69 73 43 6f 6d 70 6c 65 74 65 3b 0a 20 20 69 6e  isComplete;.  in
2afe2 74 20 6e 6f 43 61 73 65 3b 0a 20 20 69 6e 74 20  t noCase;.  int 
2afe3 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
2afe4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2afe5 54 6f 70 2d 6c 65 76 65 6c 20 6f 70 65 72 61 74  Top-level operat
2afe6 6f 72 2e 20 20 70 45 78 70 72 2d 3e 6f 70 20 2a  or.  pExpr->op *
2afe7 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  /.  Parse *pPars
2afe8 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b  e = pWC->pParse;
2afe9 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2afea 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73 71 6c  context */.  sql
2afeb 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2afec 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 2f 2a  e->db;        /*
2afed 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2afee 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 64  tion */..  if( d
2afef 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2aff0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
2aff1 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57   }.  pTerm = &pW
2aff2 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
2aff3 20 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d   pMaskSet = pWC-
2aff4 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70 45 78  >pMaskSet;.  pEx
2aff5 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pr = pTerm->pExp
2aff6 72 3b 0a 20 20 70 72 65 72 65 71 4c 65 66 74 20  r;.  prereqLeft 
2aff7 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2aff8 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
2aff9 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 6f 70 20 3d  ->pLeft);.  op =
2affa 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66   pExpr->op;.  if
2affb 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20  ( op==TK_IN ){. 
2affc 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
2affd 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20  ->pRight==0 );. 
2affe 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
2afff 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2b000 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
2b001 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72       pTerm->prer
2b002 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 53 65  eqRight = exprSe
2b003 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70  lectTableUsage(p
2b004 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e  MaskSet, pExpr->
2b005 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  x.pSelect);.    
2b006 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 65  }else{.      pTe
2b007 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
2b008 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  = exprListTableU
2b009 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2b00a 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a  Expr->x.pList);.
2b00b 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
2b00c 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
2b00d 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72  ){.    pTerm->pr
2b00e 65 72 65 71 52 69 67 68 74 20 3d 20 30 3b 0a 20  ereqRight = 0;. 
2b00f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 65 72   }else{.    pTer
2b010 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  m->prereqRight =
2b011 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2b012 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d  pMaskSet, pExpr-
2b013 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20  >pRight);.  }.  
2b014 70 72 65 72 65 71 41 6c 6c 20 3d 20 65 78 70 72  prereqAll = expr
2b015 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2b016 53 65 74 2c 20 70 45 78 70 72 29 3b 0a 20 20 69  Set, pExpr);.  i
2b017 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
2b018 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f  ty(pExpr, EP_Fro
2b019 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 42 69  mJoin) ){.    Bi
2b01a 74 6d 61 73 6b 20 78 20 3d 20 67 65 74 4d 61 73  tmask x = getMas
2b01b 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70  k(pMaskSet, pExp
2b01c 72 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  r->iRightJoinTab
2b01d 6c 65 29 3b 0a 20 20 20 20 70 72 65 72 65 71 41  le);.    prereqA
2b01e 6c 6c 20 7c 3d 20 78 3b 0a 20 20 20 20 65 78 74  ll |= x;.    ext
2b01f 72 61 52 69 67 68 74 20 3d 20 78 2d 31 3b 20 20  raRight = x-1;  
2b020 2f 2a 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72  /* ON clause ter
2b021 6d 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73  ms may not be us
2b022 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78  ed with an index
2b023 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b024 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 6c 65          ** on le
2b025 66 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45  ft table of a LE
2b026 46 54 20 4a 4f 49 4e 2e 20 20 54 69 63 6b 65 74  FT JOIN.  Ticket
2b027 20 23 33 30 31 35 20 2a 2f 0a 20 20 7d 0a 20 20   #3015 */.  }.  
2b028 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c  pTerm->prereqAll
2b029 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20   = prereqAll;.  
2b02a 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
2b02b 72 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d  r = -1;.  pTerm-
2b02c 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a 20  >iParent = -1;. 
2b02d 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2b02e 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 61 6c 6c  r = 0;.  if( all
2b02f 6f 77 65 64 4f 70 28 6f 70 29 20 26 26 20 28 70  owedOp(op) && (p
2b030 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
2b031 74 20 26 20 70 72 65 72 65 71 4c 65 66 74 29 3d  t & prereqLeft)=
2b032 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  =0 ){.    Expr *
2b033 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70  pLeft = pExpr->p
2b034 4c 65 66 74 3b 0a 20 20 20 20 45 78 70 72 20 2a  Left;.    Expr *
2b035 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
2b036 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20  pRight;.    if( 
2b037 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pLeft->op==TK_CO
2b038 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 70 54  LUMN ){.      pT
2b039 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20  erm->leftCursor 
2b03a 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b  = pLeft->iTable;
2b03b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 75 2e  .      pTerm->u.
2b03c 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65  leftColumn = pLe
2b03d 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  ft->iColumn;.   
2b03e 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61     pTerm->eOpera
2b03f 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61  tor = operatorMa
2b040 73 6b 28 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20  sk(op);.    }.  
2b041 20 20 69 66 28 20 70 52 69 67 68 74 20 26 26 20    if( pRight && 
2b042 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 43  pRight->op==TK_C
2b043 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 57  OLUMN ){.      W
2b044 68 65 72 65 54 65 72 6d 20 2a 70 4e 65 77 3b 0a  hereTerm *pNew;.
2b045 20 20 20 20 20 20 45 78 70 72 20 2a 70 44 75 70        Expr *pDup
2b046 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72  ;.      if( pTer
2b047 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3e 3d 30  m->leftCursor>=0
2b048 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
2b049 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  idxNew;.        
2b04a 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
2b04b 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2c  prDup(db, pExpr,
2b04c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
2b04d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2b04e 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  d ){.          s
2b04f 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2b050 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20  (db, pDup);.    
2b051 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2b052 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2b053 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c  idxNew = whereCl
2b054 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
2b055 70 44 75 70 2c 20 54 45 52 4d 5f 56 49 52 54 55  pDup, TERM_VIRTU
2b056 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
2b057 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 64  ;.        if( id
2b058 78 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  xNew==0 ) return
2b059 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  ;.        pNew =
2b05a 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d   &pWC->a[idxNew]
2b05b 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
2b05c 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
2b05d 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d  m;.        pTerm
2b05e 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2b05f 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  rm];.        pTe
2b060 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a  rm->nChild = 1;.
2b061 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77          pTerm->w
2b062 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43  tFlags |= TERM_C
2b063 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 7d 65 6c  OPIED;.      }el
2b064 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 75 70  se{.        pDup
2b065 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
2b066 20 20 70 4e 65 77 20 3d 20 70 54 65 72 6d 3b 0a    pNew = pTerm;.
2b067 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 78        }.      ex
2b068 70 72 43 6f 6d 6d 75 74 65 28 70 50 61 72 73 65  prCommute(pParse
2b069 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 70  , pDup);.      p
2b06a 4c 65 66 74 20 3d 20 70 44 75 70 2d 3e 70 4c 65  Left = pDup->pLe
2b06b 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  ft;.      pNew->
2b06c 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65  leftCursor = pLe
2b06d 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20  ft->iTable;.    
2b06e 20 20 70 4e 65 77 2d 3e 75 2e 6c 65 66 74 43 6f    pNew->u.leftCo
2b06f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43  lumn = pLeft->iC
2b070 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65  olumn;.      pNe
2b071 77 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  w->prereqRight =
2b072 20 70 72 65 72 65 71 4c 65 66 74 3b 0a 20 20 20   prereqLeft;.   
2b073 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 41     pNew->prereqA
2b074 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a  ll = prereqAll;.
2b075 20 20 20 20 20 20 70 4e 65 77 2d 3e 65 4f 70 65        pNew->eOpe
2b076 72 61 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72  rator = operator
2b077 4d 61 73 6b 28 70 44 75 70 2d 3e 6f 70 29 3b 0a  Mask(pDup->op);.
2b078 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 6e 64      }.  }..#ifnd
2b079 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
2b07a 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54  ETWEEN_OPTIMIZAT
2b07b 49 4f 4e 0a 20 20 2f 2a 20 49 66 20 61 20 74 65  ION.  /* If a te
2b07c 72 6d 20 69 73 20 74 68 65 20 42 45 54 57 45 45  rm is the BETWEE
2b07d 4e 20 6f 70 65 72 61 74 6f 72 2c 20 63 72 65 61  N operator, crea
2b07e 74 65 20 74 77 6f 20 6e 65 77 20 76 69 72 74 75  te two new virtu
2b07f 61 6c 20 74 65 72 6d 73 0a 20 20 2a 2a 20 74 68  al terms.  ** th
2b080 61 74 20 64 65 66 69 6e 65 20 74 68 65 20 72 61  at define the ra
2b081 6e 67 65 20 74 68 61 74 20 74 68 65 20 42 45 54  nge that the BET
2b082 57 45 45 4e 20 69 6d 70 6c 65 6d 65 6e 74 73 2e  WEEN implements.
2b083 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20    For example:. 
2b084 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 61 20   **.  **      a 
2b085 42 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 0a  BETWEEN b AND c.
2b086 20 20 2a 2a 0a 20 20 2a 2a 20 69 73 20 63 6f 6e    **.  ** is con
2b087 76 65 72 74 65 64 20 69 6e 74 6f 3a 0a 20 20 2a  verted into:.  *
2b088 2a 0a 20 20 2a 2a 20 20 20 20 20 20 28 61 20 42  *.  **      (a B
2b089 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 29 20  ETWEEN b AND c) 
2b08a 41 4e 44 20 28 61 3e 3d 62 29 20 41 4e 44 20 28  AND (a>=b) AND (
2b08b 61 3c 3d 63 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  a<=c).  **.  ** 
2b08c 54 68 65 20 74 77 6f 20 6e 65 77 20 74 65 72 6d  The two new term
2b08d 73 20 61 72 65 20 61 64 64 65 64 20 6f 6e 74 6f  s are added onto
2b08e 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
2b08f 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65  WhereClause obje
2b090 63 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 65 77  ct..  ** The new
2b091 20 74 65 72 6d 73 20 61 72 65 20 22 64 79 6e 61   terms are "dyna
2b092 6d 69 63 22 20 61 6e 64 20 61 72 65 20 63 68 69  mic" and are chi
2b093 6c 64 72 65 6e 20 6f 66 20 74 68 65 20 6f 72 69  ldren of the ori
2b094 67 69 6e 61 6c 20 42 45 54 57 45 45 4e 0a 20 20  ginal BETWEEN.  
2b095 2a 2a 20 74 65 72 6d 2e 20 20 54 68 61 74 20 6d  ** term.  That m
2b096 65 61 6e 73 20 74 68 61 74 20 69 66 20 74 68 65  eans that if the
2b097 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69 73   BETWEEN term is
2b098 20 63 6f 64 65 64 2c 20 74 68 65 20 63 68 69 6c   coded, the chil
2b099 64 72 65 6e 20 61 72 65 0a 20 20 2a 2a 20 73 6b  dren are.  ** sk
2b09a 69 70 70 65 64 2e 20 20 4f 72 2c 20 69 66 20 74  ipped.  Or, if t
2b09b 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72 65 20  he children are 
2b09c 73 61 74 69 73 66 69 65 64 20 62 79 20 61 6e 20  satisfied by an 
2b09d 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 69 67 69  index, the origi
2b09e 6e 61 6c 0a 20 20 2a 2a 20 42 45 54 57 45 45 4e  nal.  ** BETWEEN
2b09f 20 74 65 72 6d 20 69 73 20 73 6b 69 70 70 65 64   term is skipped
2b0a0 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ..  */.  else if
2b0a1 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2b0a2 42 45 54 57 45 45 4e 20 26 26 20 70 57 43 2d 3e  BETWEEN && pWC->
2b0a3 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a 20 20  op==TK_AND ){.  
2b0a4 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2b0a5 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2b0a6 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  st;.    int i;. 
2b0a7 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
2b0a8 75 38 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47  u8 ops[] = {TK_G
2b0a9 45 2c 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 20 61  E, TK_LE};.    a
2b0aa 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20  ssert( pList!=0 
2b0ab 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2b0ac 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29  List->nExpr==2 )
2b0ad 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
2b0ae 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  <2; i++){.      
2b0af 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a  Expr *pNewExpr;.
2b0b0 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77        int idxNew
2b0b1 3b 0a 20 20 20 20 20 20 70 4e 65 77 45 78 70 72  ;.      pNewExpr
2b0b2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2b0b3 62 2c 20 6f 70 73 5b 69 5d 2c 20 73 71 6c 69 74  b, ops[i], sqlit
2b0b4 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 45  e3ExprDup(db, pE
2b0b5 78 70 72 2d 3e 70 4c 65 66 74 2c 20 30 29 2c 0a  xpr->pLeft, 0),.
2b0b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b0b7 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2b0b8 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2b0b9 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
2b0ba 72 2c 20 30 29 2c 20 30 29 3b 0a 20 20 20 20 20  r, 0), 0);.     
2b0bb 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43   idxNew = whereC
2b0bc 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
2b0bd 20 70 4e 65 77 45 78 70 72 2c 20 54 45 52 4d 5f   pNewExpr, TERM_
2b0be 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e  VIRTUAL|TERM_DYN
2b0bf 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 74 65 73  AMIC);.      tes
2b0c0 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d 3d 30  tcase( idxNew==0
2b0c1 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e   );.      exprAn
2b0c2 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c  alyze(pSrc, pWC,
2b0c3 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 20 20 20   idxNew);.      
2b0c4 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
2b0c5 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20  idxTerm];.      
2b0c6 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e 69  pWC->a[idxNew].i
2b0c7 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d  Parent = idxTerm
2b0c8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 65 72  ;.    }.    pTer
2b0c9 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20  m->nChild = 2;. 
2b0ca 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
2b0cb 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e  ITE_OMIT_BETWEEN
2b0cc 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
2b0cd 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
2b0ce 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
2b0cf 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20 21  TIMIZATION) && !
2b0d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2b0d1 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20  MIT_SUBQUERY).  
2b0d2 2f 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74 65 72  /* Analyze a ter
2b0d3 6d 20 74 68 61 74 20 69 73 20 63 6f 6d 70 6f 73  m that is compos
2b0d4 65 64 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72  ed of two or mor
2b0d5 65 20 73 75 62 74 65 72 6d 73 20 63 6f 6e 6e 65  e subterms conne
2b0d6 63 74 65 64 20 62 79 0a 20 20 2a 2a 20 61 6e 20  cted by.  ** an 
2b0d7 4f 52 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a  OR operator..  *
2b0d8 2f 0a 20 20 65 6c 73 65 20 69 66 28 20 70 45 78  /.  else if( pEx
2b0d9 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 29 7b  pr->op==TK_OR ){
2b0da 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 57 43  .    assert( pWC
2b0db 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a  ->op==TK_AND );.
2b0dc 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 4f      exprAnalyzeO
2b0dd 72 54 65 72 6d 28 70 53 72 63 2c 20 70 57 43 2c  rTerm(pSrc, pWC,
2b0de 20 69 64 78 54 65 72 6d 29 3b 0a 20 20 7d 0a 23   idxTerm);.  }.#
2b0df 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2b0e0 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2b0e1 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  TION */..#ifndef
2b0e2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b   SQLITE_OMIT_LIK
2b0e3 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20  E_OPTIMIZATION. 
2b0e4 20 2f 2a 20 41 64 64 20 63 6f 6e 73 74 72 61 69   /* Add constrai
2b0e5 6e 74 73 20 74 6f 20 72 65 64 75 63 65 20 74 68  nts to reduce th
2b0e6 65 20 73 65 61 72 63 68 20 73 70 61 63 65 20 6f  e search space o
2b0e7 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  n a LIKE or GLOB
2b0e8 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a  .  ** operator..
2b0e9 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 6c 69 6b 65    **.  ** A like
2b0ea 20 70 61 74 74 65 72 6e 20 6f 66 20 74 68 65 20   pattern of the 
2b0eb 66 6f 72 6d 20 22 78 20 4c 49 4b 45 20 27 61 62  form "x LIKE 'ab
2b0ec 63 25 27 22 20 69 73 20 63 68 61 6e 67 65 64 20  c%'" is changed 
2b0ed 69 6e 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 73  into constraints
2b0ee 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
2b0ef 20 20 20 20 78 3e 3d 27 61 62 63 27 20 41 4e 44      x>='abc' AND
2b0f0 20 78 3c 27 61 62 64 27 20 41 4e 44 20 78 20 4c   x<'abd' AND x L
2b0f1 49 4b 45 20 27 61 62 63 25 27 0a 20 20 2a 2a 0a  IKE 'abc%'.  **.
2b0f2 20 20 2a 2a 20 54 68 65 20 6c 61 73 74 20 63 68    ** The last ch
2b0f3 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 70  aracter of the p
2b0f4 72 65 66 69 78 20 22 61 62 63 22 20 69 73 20 69  refix "abc" is i
2b0f5 6e 63 72 65 6d 65 6e 74 65 64 20 74 6f 20 66 6f  ncremented to fo
2b0f6 72 6d 20 74 68 65 0a 20 20 2a 2a 20 74 65 72 6d  rm the.  ** term
2b0f7 69 6e 61 74 69 6f 6e 20 63 6f 6e 64 69 74 69 6f  ination conditio
2b0f8 6e 20 22 61 62 64 22 2e 0a 20 20 2a 2f 0a 20 20  n "abd"..  */.  
2b0f9 69 66 28 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62  if( isLikeOrGlob
2b0fa 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
2b0fb 26 6e 50 61 74 74 65 72 6e 2c 20 26 69 73 43 6f  &nPattern, &isCo
2b0fc 6d 70 6c 65 74 65 2c 20 26 6e 6f 43 61 73 65 29  mplete, &noCase)
2b0fd 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 57 43  .         && pWC
2b0fe 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a  ->op==TK_AND ){.
2b0ff 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c      Expr *pLeft,
2b100 20 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 45 78   *pRight;.    Ex
2b101 70 72 20 2a 70 53 74 72 31 2c 20 2a 70 53 74 72  pr *pStr1, *pStr
2b102 32 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65  2;.    Expr *pNe
2b103 77 45 78 70 72 31 2c 20 2a 70 4e 65 77 45 78 70  wExpr1, *pNewExp
2b104 72 32 3b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e  r2;.    int idxN
2b105 65 77 31 2c 20 69 64 78 4e 65 77 32 3b 0a 0a 20  ew1, idxNew2;.. 
2b106 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72     pLeft = pExpr
2b107 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  ->x.pList->a[1].
2b108 70 45 78 70 72 3b 0a 20 20 20 20 70 52 69 67 68  pExpr;.    pRigh
2b109 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2b10a 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
2b10b 20 20 20 20 70 53 74 72 31 20 3d 20 73 71 6c 69      pStr1 = sqli
2b10c 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2b10d 20 54 4b 5f 53 54 52 49 4e 47 2c 20 30 2c 20 30   TK_STRING, 0, 0
2b10e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 53  , 0);.    if( pS
2b10f 74 72 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tr1 ){.      sql
2b110 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62  ite3TokenCopy(db
2b111 2c 20 26 70 53 74 72 31 2d 3e 74 6f 6b 65 6e 2c  , &pStr1->token,
2b112 20 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29   &pRight->token)
2b113 3b 0a 20 20 20 20 20 20 70 53 74 72 31 2d 3e 74  ;.      pStr1->t
2b114 6f 6b 65 6e 2e 6e 20 3d 20 6e 50 61 74 74 65 72  oken.n = nPatter
2b115 6e 3b 0a 20 20 20 20 20 20 70 53 74 72 31 2d 3e  n;.      pStr1->
2b116 66 6c 61 67 73 20 3d 20 45 50 5f 44 65 71 75 6f  flags = EP_Dequo
2b117 74 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ted;.    }.    p
2b118 53 74 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78  Str2 = sqlite3Ex
2b119 70 72 44 75 70 28 64 62 2c 20 70 53 74 72 31 2c  prDup(db, pStr1,
2b11a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 21 64 62   0);.    if( !db
2b11b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2b11c 7b 0a 20 20 20 20 20 20 75 38 20 63 2c 20 2a 70  {.      u8 c, *p
2b11d 43 3b 0a 20 20 20 20 20 20 2f 2a 20 61 73 73 65  C;.      /* asse
2b11e 72 74 28 20 70 53 74 72 32 2d 3e 74 6f 6b 65 6e  rt( pStr2->token
2b11f 2e 64 79 6e 20 29 3b 20 2a 2f 0a 20 20 20 20 20  .dyn ); */.     
2b120 20 70 43 20 3d 20 28 75 38 2a 29 26 70 53 74 72   pC = (u8*)&pStr
2b121 32 2d 3e 74 6f 6b 65 6e 2e 7a 5b 6e 50 61 74 74  2->token.z[nPatt
2b122 65 72 6e 2d 31 5d 3b 0a 20 20 20 20 20 20 63 20  ern-1];.      c 
2b123 3d 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 66 28  = *pC;.      if(
2b124 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 20   noCase ){.     
2b125 20 20 20 69 66 28 20 63 3d 3d 27 40 27 20 29 20     if( c=='@' ) 
2b126 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 30 3b 0a  isComplete = 0;.
2b127 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69          c = sqli
2b128 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
2b129 63 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c];.      }.    
2b12a 20 20 2a 70 43 20 3d 20 63 20 2b 20 31 3b 0a 20    *pC = c + 1;. 
2b12b 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 45 78 70     }.    pNewExp
2b12c 72 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  r1 = sqlite3PExp
2b12d 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 47 45 2c  r(pParse, TK_GE,
2b12e 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2b12f 64 62 2c 70 4c 65 66 74 2c 30 29 2c 70 53 74 72  db,pLeft,0),pStr
2b130 31 2c 30 29 3b 0a 20 20 20 20 69 64 78 4e 65 77  1,0);.    idxNew
2b131 31 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49  1 = whereClauseI
2b132 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45  nsert(pWC, pNewE
2b133 78 70 72 31 2c 20 54 45 52 4d 5f 56 49 52 54 55  xpr1, TERM_VIRTU
2b134 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
2b135 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2b136 69 64 78 4e 65 77 31 3d 3d 30 20 29 3b 0a 20 20  idxNew1==0 );.  
2b137 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53    exprAnalyze(pS
2b138 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 31  rc, pWC, idxNew1
2b139 29 3b 0a 20 20 20 20 70 4e 65 77 45 78 70 72 32  );.    pNewExpr2
2b13a 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2b13b 70 50 61 72 73 65 2c 20 54 4b 5f 4c 54 2c 20 73  pParse, TK_LT, s
2b13c 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2b13d 2c 70 4c 65 66 74 2c 30 29 2c 70 53 74 72 32 2c  ,pLeft,0),pStr2,
2b13e 30 29 3b 0a 20 20 20 20 69 64 78 4e 65 77 32 20  0);.    idxNew2 
2b13f 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
2b140 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70  ert(pWC, pNewExp
2b141 72 32 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  r2, TERM_VIRTUAL
2b142 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
2b143 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 64      testcase( id
2b144 78 4e 65 77 32 3d 3d 30 20 29 3b 0a 20 20 20 20  xNew2==0 );.    
2b145 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63  exprAnalyze(pSrc
2b146 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 32 29 3b  , pWC, idxNew2);
2b147 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57  .    pTerm = &pW
2b148 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
2b149 20 20 20 69 66 28 20 69 73 43 6f 6d 70 6c 65 74     if( isComplet
2b14a 65 20 29 7b 0a 20 20 20 20 20 20 70 57 43 2d 3e  e ){.      pWC->
2b14b 61 5b 69 64 78 4e 65 77 31 5d 2e 69 50 61 72 65  a[idxNew1].iPare
2b14c 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20  nt = idxTerm;.  
2b14d 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65      pWC->a[idxNe
2b14e 77 32 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64  w2].iParent = id
2b14f 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65  xTerm;.      pTe
2b150 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a  rm->nChild = 2;.
2b151 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
2b152 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2b153 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  LIKE_OPTIMIZATIO
2b154 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  N */..#ifndef SQ
2b155 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2b156 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 41 64 64 20  LTABLE.  /* Add 
2b157 61 20 57 4f 5f 4d 41 54 43 48 20 61 75 78 69 6c  a WO_MATCH auxil
2b158 69 61 72 79 20 74 65 72 6d 20 74 6f 20 74 68 65  iary term to the
2b159 20 63 6f 6e 73 74 72 61 69 6e 74 20 73 65 74 20   constraint set 
2b15a 69 66 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72  if the.  ** curr
2b15b 65 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ent expression i
2b15c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20  s of the form:  
2b15d 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78 70  column MATCH exp
2b15e 72 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6e 66  r..  ** This inf
2b15f 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
2b160 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e 64   by the xBestInd
2b161 65 78 20 6d 65 74 68 6f 64 73 20 6f 66 0a 20 20  ex methods of.  
2b162 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
2b163 73 2e 20 20 54 68 65 20 6e 61 74 69 76 65 20 71  s.  The native q
2b164 75 65 72 79 20 6f 70 74 69 6d 69 7a 65 72 20 64  uery optimizer d
2b165 6f 65 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 0a  oes not attempt.
2b166 20 20 2a 2a 20 74 6f 20 64 6f 20 61 6e 79 74 68    ** to do anyth
2b167 69 6e 67 20 77 69 74 68 20 4d 41 54 43 48 20 66  ing with MATCH f
2b168 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20  unctions..  */. 
2b169 20 69 66 28 20 69 73 4d 61 74 63 68 4f 66 43 6f   if( isMatchOfCo
2b16a 6c 75 6d 6e 28 70 45 78 70 72 29 20 29 7b 0a 20  lumn(pExpr) ){. 
2b16b 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20     int idxNew;. 
2b16c 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c     Expr *pRight,
2b16d 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 57 68 65   *pLeft;.    Whe
2b16e 72 65 54 65 72 6d 20 2a 70 4e 65 77 54 65 72 6d  reTerm *pNewTerm
2b16f 3b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 70 72  ;.    Bitmask pr
2b170 65 72 65 71 43 6f 6c 75 6d 6e 2c 20 70 72 65 72  ereqColumn, prer
2b171 65 71 45 78 70 72 3b 0a 0a 20 20 20 20 70 52 69  eqExpr;..    pRi
2b172 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ght = pExpr->x.p
2b173 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2b174 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45  ;.    pLeft = pE
2b175 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b  xpr->x.pList->a[
2b176 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 72  1].pExpr;.    pr
2b177 65 72 65 71 45 78 70 72 20 3d 20 65 78 70 72 54  ereqExpr = exprT
2b178 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2b179 65 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20  et, pRight);.   
2b17a 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 20 3d 20   prereqColumn = 
2b17b 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2b17c 4d 61 73 6b 53 65 74 2c 20 70 4c 65 66 74 29 3b  MaskSet, pLeft);
2b17d 0a 20 20 20 20 69 66 28 20 28 70 72 65 72 65 71  .    if( (prereq
2b17e 45 78 70 72 20 26 20 70 72 65 72 65 71 43 6f 6c  Expr & prereqCol
2b17f 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  umn)==0 ){.     
2b180 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b   Expr *pNewExpr;
2b181 0a 20 20 20 20 20 20 70 4e 65 77 45 78 70 72 20  .      pNewExpr 
2b182 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  = sqlite3Expr(db
2b183 2c 20 54 4b 5f 4d 41 54 43 48 2c 20 30 2c 20 73  , TK_MATCH, 0, s
2b184 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2b185 2c 20 70 52 69 67 68 74 2c 20 30 29 2c 20 30 29  , pRight, 0), 0)
2b186 3b 0a 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d  ;.      idxNew =
2b187 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65   whereClauseInse
2b188 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72  rt(pWC, pNewExpr
2b189 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54  , TERM_VIRTUAL|T
2b18a 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ERM_DYNAMIC);.  
2b18b 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 64      testcase( id
2b18c 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20  xNew==0 );.     
2b18d 20 70 4e 65 77 54 65 72 6d 20 3d 20 26 70 57 43   pNewTerm = &pWC
2b18e 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20  ->a[idxNew];.   
2b18f 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65     pNewTerm->pre
2b190 72 65 71 52 69 67 68 74 20 3d 20 70 72 65 72 65  reqRight = prere
2b191 71 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4e 65  qExpr;.      pNe
2b192 77 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  wTerm->leftCurso
2b193 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c  r = pLeft->iTabl
2b194 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72  e;.      pNewTer
2b195 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20  m->u.leftColumn 
2b196 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e  = pLeft->iColumn
2b197 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ;.      pNewTerm
2b198 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f  ->eOperator = WO
2b199 5f 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 70 4e  _MATCH;.      pN
2b19a 65 77 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20  ewTerm->iParent 
2b19b 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20  = idxTerm;.     
2b19c 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
2b19d 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20  [idxTerm];.     
2b19e 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d   pTerm->nChild =
2b19f 20 31 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d   1;.      pTerm-
2b1a0 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
2b1a1 5f 43 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 70  _COPIED;.      p
2b1a2 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 41  NewTerm->prereqA
2b1a3 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72  ll = pTerm->prer
2b1a4 65 71 41 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d  eqAll;.    }.  }
2b1a5 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2b1a6 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2b1a7 42 4c 45 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65  BLE */..  /* Pre
2b1a8 76 65 6e 74 20 4f 4e 20 63 6c 61 75 73 65 20 74  vent ON clause t
2b1a9 65 72 6d 73 20 6f 66 20 61 20 4c 45 46 54 20 4a  erms of a LEFT J
2b1aa 4f 49 4e 20 66 72 6f 6d 20 62 65 69 6e 67 20 75  OIN from being u
2b1ab 73 65 64 20 74 6f 20 64 72 69 76 65 0a 20 20 2a  sed to drive.  *
2b1ac 2a 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 20 74  * an index for t
2b1ad 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66  ables to the lef
2b1ae 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20  t of the join.. 
2b1af 20 2a 2f 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65   */.  pTerm->pre
2b1b0 72 65 71 52 69 67 68 74 20 7c 3d 20 65 78 74 72  reqRight |= extr
2b1b1 61 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  aRight;.}../*.**
2b1b2 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
2b1b3 61 6e 79 20 6f 66 20 74 68 65 20 65 78 70 72 65  any of the expre
2b1b4 73 73 69 6f 6e 73 20 69 6e 20 70 4c 69 73 74 2d  ssions in pList-
2b1b5 3e 61 5b 69 46 69 72 73 74 2e 2e 2e 5d 20 63 6f  >a[iFirst...] co
2b1b6 6e 74 61 69 6e 0a 2a 2a 20 61 20 72 65 66 65 72  ntain.** a refer
2b1b7 65 6e 63 65 20 74 6f 20 61 6e 79 20 74 61 62 6c  ence to any tabl
2b1b8 65 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65  e other than the
2b1b9 20 69 42 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f   iBase table..*/
2b1ba 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 66 65  .static int refe
2b1bb 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65  rencesOtherTable
2b1bc 73 28 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  s(.  ExprList *p
2b1bd 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 2f  List,          /
2b1be 2a 20 53 65 61 72 63 68 20 65 78 70 72 65 73 73  * Search express
2b1bf 69 6f 6e 73 20 69 6e 20 74 68 73 20 6c 69 73 74  ions in ths list
2b1c0 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53   */.  WhereMaskS
2b1c1 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 20 20  et *pMaskSet,   
2b1c2 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20  /* Mapping from 
2b1c3 74 61 62 6c 65 73 20 74 6f 20 62 69 74 6d 61 70  tables to bitmap
2b1c4 73 20 2a 2f 0a 20 20 69 6e 74 20 69 46 69 72 73  s */.  int iFirs
2b1c5 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
2b1c6 20 2f 2a 20 42 65 20 73 65 61 72 63 68 69 6e 67   /* Be searching
2b1c7 20 77 69 74 68 20 74 68 65 20 69 46 69 72 73 74   with the iFirst
2b1c8 2d 74 68 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  -th expression *
2b1c9 2f 0a 20 20 69 6e 74 20 69 42 61 73 65 20 20 20  /.  int iBase   
2b1ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b1cb 20 49 67 6e 6f 72 65 20 72 65 66 65 72 65 6e 63   Ignore referenc
2b1cc 65 73 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65  es to this table
2b1cd 20 2a 2f 0a 29 7b 0a 20 20 42 69 74 6d 61 73 6b   */.){.  Bitmask
2b1ce 20 61 6c 6c 6f 77 65 64 20 3d 20 7e 67 65 74 4d   allowed = ~getM
2b1cf 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 69 42  ask(pMaskSet, iB
2b1d0 61 73 65 29 3b 0a 20 20 77 68 69 6c 65 28 20 69  ase);.  while( i
2b1d1 46 69 72 73 74 3c 70 4c 69 73 74 2d 3e 6e 45 78  First<pList->nEx
2b1d2 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28 65  pr ){.    if( (e
2b1d3 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
2b1d4 61 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61  askSet, pList->a
2b1d5 5b 69 46 69 72 73 74 2b 2b 5d 2e 70 45 78 70 72  [iFirst++].pExpr
2b1d6 29 26 61 6c 6c 6f 77 65 64 29 21 3d 30 20 29 7b  )&allowed)!=0 ){
2b1d7 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2b1d8 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2b1d9 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
2b1da 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65   This routine de
2b1db 63 69 64 65 73 20 69 66 20 70 49 64 78 20 63 61  cides if pIdx ca
2b1dc 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 61 74  n be used to sat
2b1dd 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 42  isfy the ORDER B
2b1de 59 0a 2a 2a 20 63 6c 61 75 73 65 2e 20 20 49 66  Y.** clause.  If
2b1df 20 69 74 20 63 61 6e 2c 20 69 74 20 72 65 74 75   it can, it retu
2b1e0 72 6e 73 20 31 2e 20 20 49 66 20 70 49 64 78 20  rns 1.  If pIdx 
2b1e1 63 61 6e 6e 6f 74 20 73 61 74 69 73 66 79 20 74  cannot satisfy t
2b1e2 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 63  he.** ORDER BY c
2b1e3 6c 61 75 73 65 2c 20 74 68 69 73 20 72 6f 75 74  lause, this rout
2b1e4 69 6e 65 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a  ine returns 0..*
2b1e5 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73  *.** pOrderBy is
2b1e6 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
2b1e7 75 73 65 20 66 72 6f 6d 20 61 20 53 45 4c 45 43  use from a SELEC
2b1e8 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 54  T statement.  pT
2b1e9 61 62 20 69 73 20 74 68 65 0a 2a 2a 20 6c 65 66  ab is the.** lef
2b1ea 74 2d 6d 6f 73 74 20 74 61 62 6c 65 20 69 6e 20  t-most table in 
2b1eb 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2b1ec 6f 66 20 74 68 61 74 20 73 61 6d 65 20 53 45 4c  of that same SEL
2b1ed 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  ECT statement an
2b1ee 64 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 68  d.** the table h
2b1ef 61 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62  as a cursor numb
2b1f0 65 72 20 6f 66 20 22 62 61 73 65 22 2e 20 20 70  er of "base".  p
2b1f1 49 64 78 20 69 73 20 61 6e 20 69 6e 64 65 78 20  Idx is an index 
2b1f2 6f 6e 20 70 54 61 62 2e 0a 2a 2a 0a 2a 2a 20 6e  on pTab..**.** n
2b1f3 45 71 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d  EqCol is the num
2b1f4 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f  ber of columns o
2b1f5 66 20 70 49 64 78 20 74 68 61 74 20 61 72 65 20  f pIdx that are 
2b1f6 75 73 65 64 20 61 73 20 65 71 75 61 6c 69 74 79  used as equality
2b1f7 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  .** constraints.
2b1f8 20 20 41 6e 79 20 6f 66 20 74 68 65 73 65 20 63    Any of these c
2b1f9 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 20 6d 69  olumns may be mi
2b1fa 73 73 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 4f  ssing from the O
2b1fb 52 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73  RDER BY.** claus
2b1fc 65 20 61 6e 64 20 74 68 65 20 6d 61 74 63 68 20  e and the match 
2b1fd 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 61 20 73  can still be a s
2b1fe 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c  uccess..**.** Al
2b1ff 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f  l terms of the O
2b200 52 44 45 52 20 42 59 20 74 68 61 74 20 6d 61 74  RDER BY that mat
2b201 63 68 20 61 67 61 69 6e 73 74 20 74 68 65 20 69  ch against the i
2b202 6e 64 65 78 20 6d 75 73 74 20 62 65 20 65 69 74  ndex must be eit
2b203 68 65 72 0a 2a 2a 20 41 53 43 20 6f 72 20 44 45  her.** ASC or DE
2b204 53 43 2e 20 20 28 54 65 72 6d 73 20 6f 66 20 74  SC.  (Terms of t
2b205 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2b206 73 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  se past the end 
2b207 6f 66 20 61 20 55 4e 49 51 55 45 0a 2a 2a 20 69  of a UNIQUE.** i
2b208 6e 64 65 78 20 64 6f 20 6e 6f 74 20 6e 65 65 64  ndex do not need
2b209 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 69 73   to satisfy this
2b20a 20 63 6f 6e 73 74 72 61 69 6e 74 2e 29 20 20 54   constraint.)  T
2b20b 68 65 20 2a 70 62 52 65 76 20 76 61 6c 75 65 20  he *pbRev value 
2b20c 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 31 20 69  is.** set to 1 i
2b20d 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
2b20e 6c 61 75 73 65 20 69 73 20 61 6c 6c 20 44 45 53  lause is all DES
2b20f 43 20 61 6e 64 20 69 74 20 69 73 20 73 65 74 20  C and it is set 
2b210 74 6f 20 30 20 69 66 0a 2a 2a 20 74 68 65 20 4f  to 0 if.** the O
2b211 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69  RDER BY clause i
2b212 73 20 61 6c 6c 20 41 53 43 2e 0a 2a 2f 0a 73 74  s all ASC..*/.st
2b213 61 74 69 63 20 69 6e 74 20 69 73 53 6f 72 74 69  atic int isSorti
2b214 6e 67 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65  ngIndex(.  Parse
2b215 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2b216 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2b217 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
2b218 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2b219 74 2c 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72  t, /* Mapping fr
2b21a 6f 6d 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20  om table cursor 
2b21b 6e 75 6d 62 65 72 73 20 74 6f 20 62 69 74 6d 61  numbers to bitma
2b21c 70 73 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70  ps */.  Index *p
2b21d 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Idx,            
2b21e 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 77 65 20  /* The index we 
2b21f 61 72 65 20 74 65 73 74 69 6e 67 20 2a 2f 0a 20  are testing */. 
2b220 20 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20 20   int base,      
2b221 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
2b222 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  or number for th
2b223 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 73 6f  e table to be so
2b224 72 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69  rted */.  ExprLi
2b225 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
2b226 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
2b227 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  Y clause */.  in
2b228 74 20 6e 45 71 43 6f 6c 2c 20 20 20 20 20 20 20  t nEqCol,       
2b229 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2b22a 6f 66 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73  of index columns
2b22b 20 77 69 74 68 20 3d 3d 20 63 6f 6e 73 74 72 61   with == constra
2b22c 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ints */.  int *p
2b22d 62 52 65 76 20 20 20 20 20 20 20 20 20 20 20 20  bRev            
2b22e 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 69 66    /* Set to 1 if
2b22f 20 4f 52 44 45 52 20 42 59 20 69 73 20 44 45 53   ORDER BY is DES
2b230 43 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  C */.){.  int i,
2b231 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
2b232 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2b233 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
2b234 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 3d 20  int sortOrder = 
2b235 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2b236 2f 2a 20 58 4f 52 20 6f 66 20 69 6e 64 65 78 20  /* XOR of index 
2b237 61 6e 64 20 4f 52 44 45 52 20 42 59 20 73 6f 72  and ORDER BY sor
2b238 74 20 64 69 72 65 63 74 69 6f 6e 20 2a 2f 0a 20  t direction */. 
2b239 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20   int nTerm;     
2b23a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b23b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4f 52   /* Number of OR
2b23c 44 45 52 20 42 59 20 74 65 72 6d 73 20 2a 2f 0a  DER BY terms */.
2b23d 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2b23e 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 3b 20 20  t_item *pTerm;  
2b23f 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 74    /* A term of t
2b240 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2b241 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  se */.  sqlite3 
2b242 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2b243 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72  ;..  assert( pOr
2b244 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 6e 54  derBy!=0 );.  nT
2b245 65 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  erm = pOrderBy->
2b246 6e 45 78 70 72 3b 0a 20 20 61 73 73 65 72 74 28  nExpr;.  assert(
2b247 20 6e 54 65 72 6d 3e 30 20 29 3b 0a 0a 20 20 2f   nTerm>0 );..  /
2b248 2a 20 4d 61 74 63 68 20 74 65 72 6d 73 20 6f 66  * Match terms of
2b249 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2b24a 61 75 73 65 20 61 67 61 69 6e 73 74 20 63 6f 6c  ause against col
2b24b 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65  umns of.  ** the
2b24c 20 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a   index..  **.  *
2b24d 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 6e 64 69  * Note that indi
2b24e 63 65 73 20 68 61 76 65 20 70 49 64 78 2d 3e 6e  ces have pIdx->n
2b24f 43 6f 6c 75 6d 6e 20 72 65 67 75 6c 61 72 20 63  Column regular c
2b250 6f 6c 75 6d 6e 73 20 70 6c 75 73 0a 20 20 2a 2a  olumns plus.  **
2b251 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20   one additional 
2b252 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e  column containin
2b253 67 20 74 68 65 20 72 6f 77 69 64 2e 20 20 54 68  g the rowid.  Th
2b254 65 20 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 0a 20  e rowid column. 
2b255 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78   ** of the index
2b256 20 69 73 20 61 6c 73 6f 20 61 6c 6c 6f 77 65 64   is also allowed
2b257 20 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73   to match agains
2b258 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20  t the ORDER BY. 
2b259 20 2a 2a 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f   ** clause..  */
2b25a 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54  .  for(i=j=0, pT
2b25b 65 72 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b  erm=pOrderBy->a;
2b25c 20 6a 3c 6e 54 65 72 6d 20 26 26 20 69 3c 3d 70   j<nTerm && i<=p
2b25d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
2b25e 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45  +){.    Expr *pE
2b25f 78 70 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  xpr;       /* Th
2b260 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20  e expression of 
2b261 74 68 65 20 4f 52 44 45 52 20 42 59 20 70 54 65  the ORDER BY pTe
2b262 72 6d 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65  rm */.    CollSe
2b263 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20  q *pColl;    /* 
2b264 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  The collating se
2b265 71 75 65 6e 63 65 20 6f 66 20 70 45 78 70 72 20  quence of pExpr 
2b266 2a 2f 0a 20 20 20 20 69 6e 74 20 74 65 72 6d 53  */.    int termS
2b267 6f 72 74 4f 72 64 65 72 3b 20 2f 2a 20 53 6f 72  ortOrder; /* Sor
2b268 74 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73  t order for this
2b269 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74   term */.    int
2b26a 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   iColumn;       
2b26b 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 6f 6c 75  /* The i-th colu
2b26c 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  mn of the index.
2b26d 20 20 2d 31 20 66 6f 72 20 72 6f 77 69 64 20 2a    -1 for rowid *
2b26e 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74 4f  /.    int iSortO
2b26f 72 64 65 72 3b 20 20 20 20 2f 2a 20 31 20 66 6f  rder;    /* 1 fo
2b270 72 20 44 45 53 43 2c 20 30 20 66 6f 72 20 41 53  r DESC, 0 for AS
2b271 43 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 69 6e  C on the i-th in
2b272 64 65 78 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20  dex term */.    
2b273 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
2b274 6c 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  l; /* Name of th
2b275 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
2b276 65 6e 63 65 20 66 6f 72 20 69 2d 74 68 20 69 6e  ence for i-th in
2b277 64 65 78 20 74 65 72 6d 20 2a 2f 0a 0a 20 20 20  dex term */..   
2b278 20 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e   pExpr = pTerm->
2b279 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70  pExpr;.    if( p
2b27a 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  Expr->op!=TK_COL
2b27b 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54  UMN || pExpr->iT
2b27c 61 62 6c 65 21 3d 62 61 73 65 20 29 7b 0a 20 20  able!=base ){.  
2b27d 20 20 20 20 2f 2a 20 43 61 6e 20 6e 6f 74 20 75      /* Can not u
2b27e 73 65 20 61 6e 20 69 6e 64 65 78 20 73 6f 72 74  se an index sort
2b27f 20 6f 6e 20 61 6e 79 74 68 69 6e 67 20 74 68 61   on anything tha
2b280 74 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d  t is not a colum
2b281 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a  n in the.      *
2b282 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 61 62 6c  * left-most tabl
2b283 65 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  e of the FROM cl
2b284 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 62 72  ause */.      br
2b285 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  eak;.    }.    p
2b286 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
2b287 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
2b288 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66  , pExpr);.    if
2b289 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
2b28a 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44    pColl = db->pD
2b28b 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20  fltColl;.    }. 
2b28c 20 20 20 69 66 28 20 69 3c 70 49 64 78 2d 3e 6e     if( i<pIdx->n
2b28d 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
2b28e 69 43 6f 6c 75 6d 6e 20 3d 20 70 49 64 78 2d 3e  iColumn = pIdx->
2b28f 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20  aiColumn[i];.   
2b290 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d     if( iColumn==
2b291 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 69 50  pIdx->pTable->iP
2b292 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69  Key ){.        i
2b293 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20  Column = -1;.   
2b294 20 20 20 7d 0a 20 20 20 20 20 20 69 53 6f 72 74     }.      iSort
2b295 4f 72 64 65 72 20 3d 20 70 49 64 78 2d 3e 61 53  Order = pIdx->aS
2b296 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20  ortOrder[i];.   
2b297 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d     zColl = pIdx-
2b298 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20  >azColl[i];.    
2b299 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 43 6f  }else{.      iCo
2b29a 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20  lumn = -1;.     
2b29b 20 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b   iSortOrder = 0;
2b29c 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70  .      zColl = p
2b29d 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  Coll->zName;.   
2b29e 20 7d 0a 20 20 20 20 69 66 28 20 70 45 78 70 72   }.    if( pExpr
2b29f 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75  ->iColumn!=iColu
2b2a0 6d 6e 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72  mn || sqlite3Str
2b2a1 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  ICmp(pColl->zNam
2b2a2 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20  e, zColl) ){.   
2b2a3 20 20 20 2f 2a 20 54 65 72 6d 20 6a 20 6f 66 20     /* Term j of 
2b2a4 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2b2a5 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  use does not mat
2b2a6 63 68 20 63 6f 6c 75 6d 6e 20 69 20 6f 66 20 74  ch column i of t
2b2a7 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  he index */.    
2b2a8 20 20 69 66 28 20 69 3c 6e 45 71 43 6f 6c 20 29    if( i<nEqCol )
2b2a9 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
2b2aa 61 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20  an index column 
2b2ab 74 68 61 74 20 69 73 20 63 6f 6e 73 74 72 61 69  that is constrai
2b2ac 6e 65 64 20 62 79 20 3d 3d 20 66 61 69 6c 73 20  ned by == fails 
2b2ad 74 6f 20 6d 61 74 63 68 20 61 6e 0a 20 20 20 20  to match an.    
2b2ae 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20      ** ORDER BY 
2b2af 74 65 72 6d 2c 20 74 68 61 74 20 69 73 20 4f 4b  term, that is OK
2b2b0 2e 20 20 4a 75 73 74 20 69 67 6e 6f 72 65 20 74  .  Just ignore t
2b2b1 68 61 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  hat column of th
2b2b2 65 20 69 6e 64 65 78 0a 20 20 20 20 20 20 20 20  e index.        
2b2b3 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69  */.        conti
2b2b4 6e 75 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  nue;.      }else
2b2b5 20 69 66 28 20 69 3d 3d 70 49 64 78 2d 3e 6e 43   if( i==pIdx->nC
2b2b6 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
2b2b7 20 2f 2a 20 49 6e 64 65 78 20 63 6f 6c 75 6d 6e   /* Index column
2b2b8 20 69 20 69 73 20 74 68 65 20 72 6f 77 69 64 2e   i is the rowid.
2b2b9 20 20 41 6c 6c 20 6f 74 68 65 72 20 74 65 72 6d    All other term
2b2ba 73 20 6d 61 74 63 68 2e 20 2a 2f 0a 20 20 20 20  s match. */.    
2b2bb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b2bc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2b2bd 2f 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63  /* If an index c
2b2be 6f 6c 75 6d 6e 20 66 61 69 6c 73 20 74 6f 20 6d  olumn fails to m
2b2bf 61 74 63 68 20 61 6e 64 20 69 73 20 6e 6f 74 20  atch and is not 
2b2c0 63 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d  constrained by =
2b2c1 3d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  =.        ** the
2b2c2 6e 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e  n the index cann
2b2c3 6f 74 20 73 61 74 69 73 66 79 20 74 68 65 20 4f  ot satisfy the O
2b2c4 52 44 45 52 20 42 59 20 63 6f 6e 73 74 72 61 69  RDER BY constrai
2b2c5 6e 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  nt..        */. 
2b2c6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
2b2c7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2b2c8 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
2b2c9 3e 61 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29  >aSortOrder!=0 )
2b2ca 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
2b2cb 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d  erm->sortOrder==
2b2cc 30 20 7c 7c 20 70 54 65 72 6d 2d 3e 73 6f 72 74  0 || pTerm->sort
2b2cd 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20  Order==1 );.    
2b2ce 61 73 73 65 72 74 28 20 69 53 6f 72 74 4f 72 64  assert( iSortOrd
2b2cf 65 72 3d 3d 30 20 7c 7c 20 69 53 6f 72 74 4f 72  er==0 || iSortOr
2b2d0 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65  der==1 );.    te
2b2d1 72 6d 53 6f 72 74 4f 72 64 65 72 20 3d 20 69 53  rmSortOrder = iS
2b2d2 6f 72 74 4f 72 64 65 72 20 5e 20 70 54 65 72 6d  ortOrder ^ pTerm
2b2d3 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  ->sortOrder;.   
2b2d4 20 69 66 28 20 69 3e 6e 45 71 43 6f 6c 20 29 7b   if( i>nEqCol ){
2b2d5 0a 20 20 20 20 20 20 69 66 28 20 74 65 72 6d 53  .      if( termS
2b2d6 6f 72 74 4f 72 64 65 72 21 3d 73 6f 72 74 4f 72  ortOrder!=sortOr
2b2d7 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  der ){.        /
2b2d8 2a 20 49 6e 64 69 63 65 73 20 63 61 6e 20 6f 6e  * Indices can on
2b2d9 6c 79 20 62 65 20 75 73 65 64 20 69 66 20 61 6c  ly be used if al
2b2da 6c 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73  l ORDER BY terms
2b2db 20 70 61 73 74 20 74 68 65 0a 20 20 20 20 20 20   past the.      
2b2dc 20 20 2a 2a 20 65 71 75 61 6c 69 74 79 20 63 6f    ** equality co
2b2dd 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c  nstraints are al
2b2de 6c 20 65 69 74 68 65 72 20 44 45 53 43 20 6f 72  l either DESC or
2b2df 20 41 53 43 2e 20 2a 2f 0a 20 20 20 20 20 20 20   ASC. */.       
2b2e0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
2b2e1 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
2b2e2 20 20 20 20 73 6f 72 74 4f 72 64 65 72 20 3d 20      sortOrder = 
2b2e3 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 0a 20  termSortOrder;. 
2b2e4 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20     }.    j++;.  
2b2e5 20 20 70 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 69    pTerm++;.    i
2b2e6 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20  f( iColumn<0 && 
2b2e7 21 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72  !referencesOther
2b2e8 54 61 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c  Tables(pOrderBy,
2b2e9 20 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61   pMaskSet, j, ba
2b2ea 73 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  se) ){.      /* 
2b2eb 49 66 20 74 68 65 20 69 6e 64 65 78 65 64 20 63  If the indexed c
2b2ec 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 70 72 69  olumn is the pri
2b2ed 6d 61 72 79 20 6b 65 79 20 61 6e 64 20 65 76 65  mary key and eve
2b2ee 72 79 74 68 69 6e 67 20 6d 61 74 63 68 65 73 0a  rything matches.
2b2ef 20 20 20 20 20 20 2a 2a 20 73 6f 20 66 61 72 20        ** so far 
2b2f0 61 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20  and none of the 
2b2f1 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74  ORDER BY terms t
2b2f2 6f 20 74 68 65 20 72 69 67 68 74 20 72 65 66 65  o the right refe
2b2f3 72 65 6e 63 65 20 6f 74 68 65 72 0a 20 20 20 20  rence other.    
2b2f4 20 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74    ** tables in t
2b2f5 68 65 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 77 65  he join, then we
2b2f6 20 61 72 65 20 61 73 73 75 72 65 64 20 74 68 61   are assured tha
2b2f7 74 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 20  t the index can 
2b2f8 62 65 20 75 73 65 64 20 0a 20 20 20 20 20 20 2a  be used .      *
2b2f9 2a 20 74 6f 20 73 6f 72 74 20 62 65 63 61 75 73  * to sort becaus
2b2fa 65 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65  e the primary ke
2b2fb 79 20 69 73 20 75 6e 69 71 75 65 20 61 6e 64 20  y is unique and 
2b2fc 73 6f 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 6f  so none of the o
2b2fd 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 63 6f  ther.      ** co
2b2fe 6c 75 6d 6e 73 20 77 69 6c 6c 20 6d 61 6b 65 20  lumns will make 
2b2ff 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65 0a 20  any difference. 
2b300 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a 20       */.      j 
2b301 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20  = nTerm;.    }. 
2b302 20 7d 0a 0a 20 20 2a 70 62 52 65 76 20 3d 20 73   }..  *pbRev = s
2b303 6f 72 74 4f 72 64 65 72 21 3d 30 3b 0a 20 20 69  ortOrder!=0;.  i
2b304 66 28 20 6a 3e 3d 6e 54 65 72 6d 20 29 7b 0a 20  f( j>=nTerm ){. 
2b305 20 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20     /* All terms 
2b306 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
2b307 63 6c 61 75 73 65 20 61 72 65 20 63 6f 76 65 72  clause are cover
2b308 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78  ed by this index
2b309 20 73 6f 0a 20 20 20 20 2a 2a 20 74 68 69 73 20   so.    ** this 
2b30a 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65  index can be use
2b30b 64 20 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20 2a  d for sorting. *
2b30c 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  /.    return 1;.
2b30d 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 2d 3e    }.  if( pIdx->
2b30e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
2b30f 20 26 26 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f   && i==pIdx->nCo
2b310 6c 75 6d 6e 0a 20 20 20 20 20 20 26 26 20 21 72  lumn.      && !r
2b311 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61  eferencesOtherTa
2b312 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20 70  bles(pOrderBy, p
2b313 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65  MaskSet, j, base
2b314 29 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20  ) ){.    /* All 
2b315 74 65 72 6d 73 20 6f 66 20 74 68 69 73 20 69 6e  terms of this in
2b316 64 65 78 20 6d 61 74 63 68 20 73 6f 6d 65 20 70  dex match some p
2b317 72 65 66 69 78 20 6f 66 20 74 68 65 20 4f 52 44  refix of the ORD
2b318 45 52 20 42 59 20 63 6c 61 75 73 65 0a 20 20 20  ER BY clause.   
2b319 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64 65   ** and the inde
2b31a 78 20 69 73 20 55 4e 49 51 55 45 20 61 6e 64 20  x is UNIQUE and 
2b31b 6e 6f 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20  no terms on the 
2b31c 74 61 69 6c 20 6f 66 20 74 68 65 20 4f 52 44 45  tail of the ORDE
2b31d 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75  R BY.    ** clau
2b31e 73 65 20 72 65 66 65 72 65 6e 63 65 20 6f 74 68  se reference oth
2b31f 65 72 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a  er tables in a j
2b320 6f 69 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73  oin.  If this is
2b321 20 61 6c 6c 20 74 72 75 65 20 74 68 65 6e 0a 20   all true then. 
2b322 20 20 20 2a 2a 20 74 68 65 20 6f 72 64 65 72 20     ** the order 
2b323 62 79 20 63 6c 61 75 73 65 20 69 73 20 73 75 70  by clause is sup
2b324 65 72 66 6c 75 6f 75 73 2e 20 2a 2f 0a 20 20 20  erfluous. */.   
2b325 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
2b326 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
2b327 0a 2a 2a 20 43 68 65 63 6b 20 74 61 62 6c 65 20  .** Check table 
2b328 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 4f 52  to see if the OR
2b329 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e  DER BY clause in
2b32a 20 70 4f 72 64 65 72 42 79 20 63 61 6e 20 62 65   pOrderBy can be
2b32b 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79   satisfied.** by
2b32c 20 73 6f 72 74 69 6e 67 20 69 6e 20 6f 72 64 65   sorting in orde
2b32d 72 20 6f 66 20 52 4f 57 49 44 2e 20 20 52 65 74  r of ROWID.  Ret
2b32e 75 72 6e 20 74 72 75 65 20 69 66 20 73 6f 20 61  urn true if so a
2b32f 6e 64 20 73 65 74 20 2a 70 62 52 65 76 20 74 6f  nd set *pbRev to
2b330 20 62 65 0a 2a 2a 20 74 72 75 65 20 66 6f 72 20   be.** true for 
2b331 72 65 76 65 72 73 65 20 52 4f 57 49 44 20 61 6e  reverse ROWID an
2b332 64 20 66 61 6c 73 65 20 66 6f 72 20 66 6f 72 77  d false for forw
2b333 61 72 64 20 52 4f 57 49 44 20 6f 72 64 65 72 2e  ard ROWID order.
2b334 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2b335 6f 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28 0a  ortableByRowid(.
2b336 20 20 69 6e 74 20 62 61 73 65 2c 20 20 20 20 20    int base,     
2b337 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
2b338 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
2b339 61 62 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65  able to be sorte
2b33a 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  d */.  ExprList 
2b33b 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f  *pOrderBy,     /
2b33c 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
2b33d 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65  lause */.  Where
2b33e 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2b33f 74 2c 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72  t, /* Mapping fr
2b340 6f 6d 20 74 61 62 6c 65 20 63 75 72 73 6f 72 73  om table cursors
2b341 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0a 20   to bitmaps */. 
2b342 20 69 6e 74 20 2a 70 62 52 65 76 20 20 20 20 20   int *pbRev     
2b343 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20           /* Set 
2b344 74 6f 20 31 20 69 66 20 4f 52 44 45 52 20 42 59  to 1 if ORDER BY
2b345 20 69 73 20 44 45 53 43 20 2a 2f 0a 29 7b 0a 20   is DESC */.){. 
2b346 20 45 78 70 72 20 2a 70 3b 0a 0a 20 20 61 73 73   Expr *p;..  ass
2b347 65 72 74 28 20 70 4f 72 64 65 72 42 79 21 3d 30  ert( pOrderBy!=0
2b348 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
2b349 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e 30 20  rderBy->nExpr>0 
2b34a 29 3b 0a 20 20 70 20 3d 20 70 4f 72 64 65 72 42  );.  p = pOrderB
2b34b 79 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20  y->a[0].pExpr;. 
2b34c 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43   if( p->op==TK_C
2b34d 4f 4c 55 4d 4e 20 26 26 20 70 2d 3e 69 54 61 62  OLUMN && p->iTab
2b34e 6c 65 3d 3d 62 61 73 65 20 26 26 20 70 2d 3e 69  le==base && p->i
2b34f 43 6f 6c 75 6d 6e 3d 3d 2d 31 0a 20 20 20 20 26  Column==-1.    &
2b350 26 20 21 72 65 66 65 72 65 6e 63 65 73 4f 74 68  & !referencesOth
2b351 65 72 54 61 62 6c 65 73 28 70 4f 72 64 65 72 42  erTables(pOrderB
2b352 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20 31 2c 20  y, pMaskSet, 1, 
2b353 62 61 73 65 29 20 29 7b 0a 20 20 20 20 2a 70 62  base) ){.    *pb
2b354 52 65 76 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  Rev = pOrderBy->
2b355 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a  a[0].sortOrder;.
2b356 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2b357 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2b358 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61  ./*.** Prepare a
2b359 20 63 72 75 64 65 20 65 73 74 69 6d 61 74 65 20   crude estimate 
2b35a 6f 66 20 74 68 65 20 6c 6f 67 61 72 69 74 68 6d  of the logarithm
2b35b 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 76 61   of the input va
2b35c 6c 75 65 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75  lue..** The resu
2b35d 6c 74 73 20 6e 65 65 64 20 6e 6f 74 20 62 65 20  lts need not be 
2b35e 65 78 61 63 74 2e 20 20 54 68 69 73 20 69 73 20  exact.  This is 
2b35f 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 65 73  only used for es
2b360 74 69 6d 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20  timating.** the 
2b361 74 6f 74 61 6c 20 63 6f 73 74 20 6f 66 20 70 65  total cost of pe
2b362 72 66 6f 72 6d 69 6e 67 20 6f 70 65 72 61 74 69  rforming operati
2b363 6f 6e 73 20 77 69 74 68 20 4f 28 6c 6f 67 4e 29  ons with O(logN)
2b364 20 6f 72 20 4f 28 4e 6c 6f 67 4e 29 0a 2a 2a 20   or O(NlogN).** 
2b365 63 6f 6d 70 6c 65 78 69 74 79 2e 20 20 42 65 63  complexity.  Bec
2b366 61 75 73 65 20 4e 20 69 73 20 6a 75 73 74 20 61  ause N is just a
2b367 20 67 75 65 73 73 2c 20 69 74 20 69 73 20 6e 6f   guess, it is no
2b368 20 67 72 65 61 74 20 74 72 61 67 65 64 79 20 69   great tragedy i
2b369 66 0a 2a 2a 20 6c 6f 67 4e 20 69 73 20 61 20 6c  f.** logN is a l
2b36a 69 74 74 6c 65 20 6f 66 66 2e 0a 2a 2f 0a 73 74  ittle off..*/.st
2b36b 61 74 69 63 20 64 6f 75 62 6c 65 20 65 73 74 4c  atic double estL
2b36c 6f 67 28 64 6f 75 62 6c 65 20 4e 29 7b 0a 20 20  og(double N){.  
2b36d 64 6f 75 62 6c 65 20 6c 6f 67 4e 20 3d 20 31 3b  double logN = 1;
2b36e 0a 20 20 64 6f 75 62 6c 65 20 78 20 3d 20 31 30  .  double x = 10
2b36f 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 78 20 29  ;.  while( N>x )
2b370 7b 0a 20 20 20 20 6c 6f 67 4e 20 2b 3d 20 31 3b  {.    logN += 1;
2b371 0a 20 20 20 20 78 20 2a 3d 20 31 30 3b 0a 20 20  .    x *= 10;.  
2b372 7d 0a 20 20 72 65 74 75 72 6e 20 6c 6f 67 4e 3b  }.  return logN;
2b373 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 72 6f  .}../*.** Two ro
2b374 75 74 69 6e 65 73 20 66 6f 72 20 70 72 69 6e 74  utines for print
2b375 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ing the content 
2b376 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 69 6e  of an sqlite3_in
2b377 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 73 74 72 75  dex_info.** stru
2b378 63 74 75 72 65 2e 20 20 55 73 65 64 20 66 6f 72  cture.  Used for
2b379 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62   testing and deb
2b37a 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 49 66  ugging only.  If
2b37b 20 6e 65 69 74 68 65 72 0a 2a 2a 20 53 51 4c 49   neither.** SQLI
2b37c 54 45 5f 54 45 53 54 20 6f 72 20 53 51 4c 49 54  TE_TEST or SQLIT
2b37d 45 5f 44 45 42 55 47 20 61 72 65 20 64 65 66 69  E_DEBUG are defi
2b37e 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 73 65 20  ned, then these 
2b37f 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20  routines.** are 
2b380 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 20 21  no-ops..*/.#if !
2b381 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2b382 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2b383 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
2b384 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69  ITE_DEBUG).stati
2b385 63 20 76 6f 69 64 20 54 52 41 43 45 5f 49 44 58  c void TRACE_IDX
2b386 5f 49 4e 50 55 54 53 28 73 71 6c 69 74 65 33 5f  _INPUTS(sqlite3_
2b387 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a  index_info *p){.
2b388 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21    int i;.  if( !
2b389 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63  sqlite3WhereTrac
2b38a 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  e ) return;.  fo
2b38b 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e  r(i=0; i<p->nCon
2b38c 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20  straint; i++){. 
2b38d 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2b38e 72 69 6e 74 66 28 22 20 20 63 6f 6e 73 74 72 61  rintf("  constra
2b38f 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64 20  int[%d]: col=%d 
2b390 74 65 72 6d 69 64 3d 25 64 20 6f 70 3d 25 64 20  termid=%d op=%d 
2b391 75 73 61 62 6c 65 64 3d 25 64 5c 6e 22 2c 0a 20  usabled=%d\n",. 
2b392 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20        i,.       
2b393 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
2b394 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  ].iColumn,.     
2b395 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74    p->aConstraint
2b396 5b 69 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 2c  [i].iTermOffset,
2b397 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
2b398 74 72 61 69 6e 74 5b 69 5d 2e 6f 70 2c 0a 20 20  traint[i].op,.  
2b399 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61       p->aConstra
2b39a 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65 29 3b 0a  int[i].usable);.
2b39b 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
2b39c 3c 70 2d 3e 6e 4f 72 64 65 72 42 79 3b 20 69 2b  <p->nOrderBy; i+
2b39d 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  +){.    sqlite3D
2b39e 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 6f 72  ebugPrintf("  or
2b39f 64 65 72 62 79 5b 25 64 5d 3a 20 63 6f 6c 3d 25  derby[%d]: col=%
2b3a0 64 20 64 65 73 63 3d 25 64 5c 6e 22 2c 0a 20 20  d desc=%d\n",.  
2b3a1 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20 70       i,.       p
2b3a2 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43  ->aOrderBy[i].iC
2b3a3 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 70 2d  olumn,.       p-
2b3a4 3e 61 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73  >aOrderBy[i].des
2b3a5 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  c);.  }.}.static
2b3a6 20 76 6f 69 64 20 54 52 41 43 45 5f 49 44 58 5f   void TRACE_IDX_
2b3a7 4f 55 54 50 55 54 53 28 73 71 6c 69 74 65 33 5f  OUTPUTS(sqlite3_
2b3a8 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a  index_info *p){.
2b3a9 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21    int i;.  if( !
2b3aa 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63  sqlite3WhereTrac
2b3ab 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  e ) return;.  fo
2b3ac 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e  r(i=0; i<p->nCon
2b3ad 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20  straint; i++){. 
2b3ae 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2b3af 72 69 6e 74 66 28 22 20 20 75 73 61 67 65 5b 25  rintf("  usage[%
2b3b0 64 5d 3a 20 61 72 67 76 49 64 78 3d 25 64 20 6f  d]: argvIdx=%d o
2b3b1 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20  mit=%d\n",.     
2b3b2 20 20 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61    i,.       p->a
2b3b3 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
2b3b4 69 5d 2e 61 72 67 76 49 6e 64 65 78 2c 0a 20 20  i].argvIndex,.  
2b3b5 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61       p->aConstra
2b3b6 69 6e 74 55 73 61 67 65 5b 69 5d 2e 6f 6d 69 74  intUsage[i].omit
2b3b7 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2b3b8 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 69  DebugPrintf("  i
2b3b9 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c 20 70 2d 3e  dxNum=%d\n", p->
2b3ba 69 64 78 4e 75 6d 29 3b 0a 20 20 73 71 6c 69 74  idxNum);.  sqlit
2b3bb 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
2b3bc 20 69 64 78 53 74 72 3d 25 73 5c 6e 22 2c 20 70   idxStr=%s\n", p
2b3bd 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 73 71 6c  ->idxStr);.  sql
2b3be 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2b3bf 22 20 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  "  orderByConsum
2b3c0 65 64 3d 25 64 5c 6e 22 2c 20 70 2d 3e 6f 72 64  ed=%d\n", p->ord
2b3c1 65 72 42 79 43 6f 6e 73 75 6d 65 64 29 3b 0a 20  erByConsumed);. 
2b3c2 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2b3c3 6e 74 66 28 22 20 20 65 73 74 69 6d 61 74 65 64  ntf("  estimated
2b3c4 43 6f 73 74 3d 25 67 5c 6e 22 2c 20 70 2d 3e 65  Cost=%g\n", p->e
2b3c5 73 74 69 6d 61 74 65 64 43 6f 73 74 29 3b 0a 7d  stimatedCost);.}
2b3c6 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54  .#else.#define T
2b3c7 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28  RACE_IDX_INPUTS(
2b3c8 41 29 0a 23 64 65 66 69 6e 65 20 54 52 41 43 45  A).#define TRACE
2b3c9 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 41 29 0a  _IDX_OUTPUTS(A).
2b3ca 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
2b3cb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2b3cc 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43  UALTABLE./*.** C
2b3cd 6f 6d 70 75 74 65 20 74 68 65 20 62 65 73 74 20  ompute the best 
2b3ce 69 6e 64 65 78 20 66 6f 72 20 61 20 76 69 72 74  index for a virt
2b3cf 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ual table..**.**
2b3d0 20 54 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   The best index 
2b3d1 69 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74  is computed by t
2b3d2 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65  he xBestIndex me
2b3d3 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74  thod of the virt
2b3d4 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 6d 6f 64  ual.** table mod
2b3d5 75 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ule.  This routi
2b3d6 6e 65 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73  ne is really jus
2b3d7 74 20 61 20 77 72 61 70 70 65 72 20 74 68 61 74  t a wrapper that
2b3d8 20 73 65 74 73 20 75 70 0a 2a 2a 20 74 68 65 20   sets up.** the 
2b3d9 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2b3da 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61  fo structure tha
2b3db 74 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  t is used to com
2b3dc 6d 75 6e 69 63 61 74 65 20 77 69 74 68 0a 2a 2a  municate with.**
2b3dd 20 78 42 65 73 74 49 6e 64 65 78 2e 0a 2a 2a 0a   xBestIndex..**.
2b3de 2a 2a 20 49 6e 20 61 20 6a 6f 69 6e 2c 20 74 68  ** In a join, th
2b3df 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  is routine might
2b3e0 20 62 65 20 63 61 6c 6c 65 64 20 6d 75 6c 74 69   be called multi
2b3e1 70 6c 65 20 74 69 6d 65 73 20 66 6f 72 20 74 68  ple times for th
2b3e2 65 0a 2a 2a 20 73 61 6d 65 20 76 69 72 74 75 61  e.** same virtua
2b3e3 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 73 71  l table.  The sq
2b3e4 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2b3e5 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72   structure is cr
2b3e6 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 69 6e 69  eated.** and ini
2b3e7 74 69 61 6c 69 7a 65 64 20 6f 6e 20 74 68 65 20  tialized on the 
2b3e8 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69 6f 6e  first invocation
2b3e9 20 61 6e 64 20 72 65 75 73 65 64 20 6f 6e 20 61   and reused on a
2b3ea 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  ll subsequent.**
2b3eb 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 20 20 54   invocations.  T
2b3ec 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2b3ed 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  _info structure 
2b3ee 69 73 20 61 6c 73 6f 20 75 73 65 64 20 77 68 65  is also used whe
2b3ef 6e 0a 2a 2a 20 63 6f 64 65 20 69 73 20 67 65 6e  n.** code is gen
2b3f0 65 72 61 74 65 64 20 74 6f 20 61 63 63 65 73 73  erated to access
2b3f1 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2b3f2 6c 65 2e 20 20 54 68 65 20 77 68 65 72 65 49 6e  le.  The whereIn
2b3f3 66 6f 44 65 6c 65 74 65 28 29 20 0a 2a 2a 20 72  foDelete() .** r
2b3f4 6f 75 74 69 6e 65 20 74 61 6b 65 73 20 63 61 72  outine takes car
2b3f5 65 20 6f 66 20 66 72 65 65 69 6e 67 20 74 68 65  e of freeing the
2b3f6 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2b3f7 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 61 66  nfo structure af
2b3f8 74 65 72 0a 2a 2a 20 65 76 65 72 79 62 6f 64 79  ter.** everybody
2b3f9 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69   has finished wi
2b3fa 74 68 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  th it..*/.static
2b3fb 20 64 6f 75 62 6c 65 20 62 65 73 74 56 69 72 74   double bestVirt
2b3fc 75 61 6c 49 6e 64 65 78 28 0a 20 20 50 61 72 73  ualIndex(.  Pars
2b3fd 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2b3fe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2b3ff 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
2b400 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  t */.  WhereClau
2b401 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20  se *pWC,        
2b402 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
2b403 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73  RE clause */.  s
2b404 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2b405 65 6d 20 2a 70 53 72 63 2c 20 20 20 20 20 2f 2a  em *pSrc,     /*
2b406 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
2b407 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20   term to search 
2b408 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
2b409 52 65 61 64 79 2c 20 20 20 20 20 20 20 20 20 20  Ready,          
2b40a 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63      /* Mask of c
2b40b 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20  ursors that are 
2b40c 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f  not available */
2b40d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
2b40e 64 65 72 42 79 2c 20 20 20 20 20 20 20 20 20 20  derBy,          
2b40f 20 20 2f 2a 20 54 68 65 20 6f 72 64 65 72 20 62    /* The order b
2b410 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  y clause */.  in
2b411 74 20 6f 72 64 65 72 42 79 55 73 61 62 6c 65 2c  t orderByUsable,
2b412 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b413 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20 70  True if we can p
2b414 6f 74 65 6e 74 69 61 6c 20 73 6f 72 74 20 2a 2f  otential sort */
2b415 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  .  sqlite3_index
2b416 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78 49 6e 66  _info **ppIdxInf
2b417 6f 20 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f 72  o /* Index infor
2b418 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20 74 6f  mation passed to
2b419 20 78 42 65 73 74 49 6e 64 65 78 20 2a 2f 0a 29   xBestIndex */.)
2b41a 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  {.  Table *pTab 
2b41b 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20 20  = pSrc->pTab;.  
2b41c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
2b41d 74 61 62 20 3d 20 70 54 61 62 2d 3e 70 56 74 61  tab = pTab->pVta
2b41e 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64  b;.  sqlite3_ind
2b41f 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66  ex_info *pIdxInf
2b420 6f 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69  o;.  struct sqli
2b421 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
2b422 61 69 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b 0a  aint *pIdxCons;.
2b423 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
2b424 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 2a  _index_orderby *
2b425 70 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 73  pIdxOrderBy;.  s
2b426 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2b427 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75  dex_constraint_u
2b428 73 61 67 65 20 2a 70 55 73 61 67 65 3b 0a 20 20  sage *pUsage;.  
2b429 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
2b42a 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
2b42b 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20  int nOrderBy;.  
2b42c 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 66  int rc;..  /* If
2b42d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2b42e 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2b42f 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70  e has not been p
2b430 72 65 76 69 6f 75 73 6c 79 0a 20 20 2a 2a 20 61  reviously.  ** a
2b431 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 6e 69  llocated and ini
2b432 74 69 61 6c 69 7a 65 64 20 66 6f 72 20 74 68 69  tialized for thi
2b433 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c  s virtual table,
2b434 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 0a 20   then allocate. 
2b435 20 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69   ** and initiali
2b436 7a 65 20 69 74 20 6e 6f 77 0a 20 20 2a 2f 0a 20  ze it now.  */. 
2b437 20 70 49 64 78 49 6e 66 6f 20 3d 20 2a 70 70 49   pIdxInfo = *ppI
2b438 64 78 49 6e 66 6f 3b 0a 20 20 69 66 28 20 70 49  dxInfo;.  if( pI
2b439 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20  dxInfo==0 ){.   
2b43a 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 20 20   int nTerm;.    
2b43b 57 48 45 52 45 54 52 41 43 45 28 28 22 52 65 63  WHERETRACE(("Rec
2b43c 6f 6d 70 75 74 69 6e 67 20 69 6e 64 65 78 20 69  omputing index i
2b43d 6e 66 6f 20 66 6f 72 20 25 73 2e 2e 2e 5c 6e 22  nfo for %s...\n"
2b43e 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b  , pTab->zName));
2b43f 0a 0a 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 74  ..    /* Count t
2b440 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73  he number of pos
2b441 73 69 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75  sible WHERE clau
2b442 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72  se constraints r
2b443 65 66 65 72 72 69 6e 67 0a 20 20 20 20 2a 2a 20  eferring.    ** 
2b444 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c 20  to this virtual 
2b445 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72  table */.    for
2b446 28 69 3d 6e 54 65 72 6d 3d 30 2c 20 70 54 65 72  (i=nTerm=0, pTer
2b447 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d  m=pWC->a; i<pWC-
2b448 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65  >nTerm; i++, pTe
2b449 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  rm++){.      if(
2b44a 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73   pTerm->leftCurs
2b44b 6f 72 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72  or != pSrc->iCur
2b44c 73 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  sor ) continue;.
2b44d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
2b44e 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26  Term->eOperator&
2b44f 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  (pTerm->eOperato
2b450 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  r-1))==0 );.    
2b451 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
2b452 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
2b453 5f 49 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _IN );.      tes
2b454 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
2b455 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55  perator==WO_ISNU
2b456 4c 4c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  LL );.      if( 
2b457 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2b458 20 26 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e   & (WO_IN|WO_ISN
2b459 55 4c 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ULL) ) continue;
2b45a 0a 20 20 20 20 20 20 6e 54 65 72 6d 2b 2b 3b 0a  .      nTerm++;.
2b45b 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
2b45c 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2b45d 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  ause contains on
2b45e 6c 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  ly columns in th
2b45f 65 20 63 75 72 72 65 6e 74 20 0a 20 20 20 20 2a  e current .    *
2b460 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * virtual table 
2b461 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 73 70  then allocate sp
2b462 61 63 65 20 66 6f 72 20 74 68 65 20 61 4f 72 64  ace for the aOrd
2b463 65 72 42 79 20 70 61 72 74 20 6f 66 0a 20 20 20  erBy part of.   
2b464 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f   ** the sqlite3_
2b465 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
2b466 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ture..    */.   
2b467 20 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20   nOrderBy = 0;. 
2b468 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
2b469 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  ){.      for(i=0
2b46a 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ; i<pOrderBy->nE
2b46b 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
2b46c 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d     Expr *pExpr =
2b46d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e   pOrderBy->a[i].
2b46e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 69  pExpr;.        i
2b46f 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  f( pExpr->op!=TK
2b470 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72  _COLUMN || pExpr
2b471 2d 3e 69 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e  ->iTable!=pSrc->
2b472 69 43 75 72 73 6f 72 20 29 20 62 72 65 61 6b 3b  iCursor ) break;
2b473 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2b474 66 28 20 69 3d 3d 70 4f 72 64 65 72 42 79 2d 3e  f( i==pOrderBy->
2b475 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20  nExpr ){.       
2b476 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64   nOrderBy = pOrd
2b477 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  erBy->nExpr;.   
2b478 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2b479 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
2b47a 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2b47b 66 6f 20 73 74 72 75 63 74 75 72 65 0a 20 20 20  fo structure.   
2b47c 20 2a 2f 0a 20 20 20 20 70 49 64 78 49 6e 66 6f   */.    pIdxInfo
2b47d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2b47e 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64  ocZero(pParse->d
2b47f 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 64 78 49  b, sizeof(*pIdxI
2b480 6e 66 6f 29 0a 20 20 20 20 20 20 20 20 20 20 20  nfo).           
2b481 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b482 20 20 2b 20 28 73 69 7a 65 6f 66 28 2a 70 49 64    + (sizeof(*pId
2b483 78 43 6f 6e 73 29 20 2b 20 73 69 7a 65 6f 66 28  xCons) + sizeof(
2b484 2a 70 55 73 61 67 65 29 29 2a 6e 54 65 72 6d 0a  *pUsage))*nTerm.
2b485 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b486 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 73               + s
2b487 69 7a 65 6f 66 28 2a 70 49 64 78 4f 72 64 65 72  izeof(*pIdxOrder
2b488 42 79 29 2a 6e 4f 72 64 65 72 42 79 20 29 3b 0a  By)*nOrderBy );.
2b489 20 20 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f      if( pIdxInfo
2b48a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
2b48b 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2b48c 72 73 65 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d  rse, "out of mem
2b48d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ory");.      /* 
2b48e 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73  (double)0 In cas
2b48f 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  e of SQLITE_OMIT
2b490 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e  _FLOATING_POINT.
2b491 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  .. */.      retu
2b492 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20  rn (double)0;.  
2b493 20 20 7d 0a 20 20 20 20 2a 70 70 49 64 78 49 6e    }.    *ppIdxIn
2b494 66 6f 20 3d 20 70 49 64 78 49 6e 66 6f 3b 0a 0a  fo = pIdxInfo;..
2b495 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a      /* Initializ
2b496 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65 2e  e the structure.
2b497 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e    The sqlite3_in
2b498 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2b499 72 65 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20  re contains.    
2b49a 2a 2a 20 6d 61 6e 79 20 66 69 65 6c 64 73 20 74  ** many fields t
2b49b 68 61 74 20 61 72 65 20 64 65 63 6c 61 72 65 64  hat are declared
2b49c 20 22 63 6f 6e 73 74 22 20 74 6f 20 70 72 65 76   "const" to prev
2b49d 65 6e 74 20 78 42 65 73 74 49 6e 64 65 78 20 66  ent xBestIndex f
2b49e 72 6f 6d 0a 20 20 20 20 2a 2a 20 63 68 61 6e 67  rom.    ** chang
2b49f 69 6e 67 20 74 68 65 6d 2e 20 20 57 65 20 68 61  ing them.  We ha
2b4a0 76 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20 66 75  ve to do some fu
2b4a1 6e 6b 79 20 63 61 73 74 69 6e 67 20 69 6e 20 6f  nky casting in o
2b4a2 72 64 65 72 20 74 6f 0a 20 20 20 20 2a 2a 20 69  rder to.    ** i
2b4a3 6e 69 74 69 61 6c 69 7a 65 20 74 68 6f 73 65 20  nitialize those 
2b4a4 66 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  fields..    */. 
2b4a5 20 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 28 73     pIdxCons = (s
2b4a6 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2b4a7 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 29  dex_constraint*)
2b4a8 26 70 49 64 78 49 6e 66 6f 5b 31 5d 3b 0a 20 20  &pIdxInfo[1];.  
2b4a9 20 20 70 49 64 78 4f 72 64 65 72 42 79 20 3d 20    pIdxOrderBy = 
2b4aa 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  (struct sqlite3_
2b4ab 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a 29 26  index_orderby*)&
2b4ac 70 49 64 78 43 6f 6e 73 5b 6e 54 65 72 6d 5d 3b  pIdxCons[nTerm];
2b4ad 0a 20 20 20 20 70 55 73 61 67 65 20 3d 20 28 73  .    pUsage = (s
2b4ae 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2b4af 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75  dex_constraint_u
2b4b0 73 61 67 65 2a 29 26 70 49 64 78 4f 72 64 65 72  sage*)&pIdxOrder
2b4b1 42 79 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20  By[nOrderBy];.  
2b4b2 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e    *(int*)&pIdxIn
2b4b3 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20  fo->nConstraint 
2b4b4 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 2a 28 69  = nTerm;.    *(i
2b4b5 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e  nt*)&pIdxInfo->n
2b4b6 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72 64 65 72  OrderBy = nOrder
2b4b7 42 79 3b 0a 20 20 20 20 2a 28 73 74 72 75 63 74  By;.    *(struct
2b4b8 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
2b4b9 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64  onstraint**)&pId
2b4ba 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
2b4bb 6e 74 20 3d 20 70 49 64 78 43 6f 6e 73 3b 0a 20  nt = pIdxCons;. 
2b4bc 20 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69     *(struct sqli
2b4bd 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62  te3_index_orderb
2b4be 79 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61  y**)&pIdxInfo->a
2b4bf 4f 72 64 65 72 42 79 20 3d 20 70 49 64 78 4f 72  OrderBy = pIdxOr
2b4c0 64 65 72 42 79 3b 0a 20 20 20 20 2a 28 73 74 72  derBy;.    *(str
2b4c1 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2b4c2 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
2b4c3 67 65 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  ge**)&pIdxInfo->
2b4c4 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
2b4c5 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   =.             
2b4c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4c9 20 20 20 20 20 20 20 20 70 55 73 61 67 65 3b 0a          pUsage;.
2b4ca 0a 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20  .    for(i=j=0, 
2b4cb 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c  pTerm=pWC->a; i<
2b4cc 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c  pWC->nTerm; i++,
2b4cd 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20   pTerm++){.     
2b4ce 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
2b4cf 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e  Cursor != pSrc->
2b4d0 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e  iCursor ) contin
2b4d1 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ue;.      assert
2b4d2 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  ( (pTerm->eOpera
2b4d3 74 6f 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65  tor&(pTerm->eOpe
2b4d4 72 61 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a  rator-1))==0 );.
2b4d5 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b4d6 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2b4d7 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 20  ==WO_IN );.     
2b4d8 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
2b4d9 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2b4da 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  ISNULL );.      
2b4db 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
2b4dc 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f  ator & (WO_IN|WO
2b4dd 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69  _ISNULL) ) conti
2b4de 6e 75 65 3b 0a 20 20 20 20 20 20 70 49 64 78 43  nue;.      pIdxC
2b4df 6f 6e 73 5b 6a 5d 2e 69 43 6f 6c 75 6d 6e 20 3d  ons[j].iColumn =
2b4e0 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f   pTerm->u.leftCo
2b4e1 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 49 64 78  lumn;.      pIdx
2b4e2 43 6f 6e 73 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66  Cons[j].iTermOff
2b4e3 73 65 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 70  set = i;.      p
2b4e4 49 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d 20  IdxCons[j].op = 
2b4e5 28 75 38 29 70 54 65 72 6d 2d 3e 65 4f 70 65 72  (u8)pTerm->eOper
2b4e6 61 74 6f 72 3b 0a 20 20 20 20 20 20 2f 2a 20 54  ator;.      /* T
2b4e7 68 65 20 64 69 72 65 63 74 20 61 73 73 69 67 6e  he direct assign
2b4e8 6d 65 6e 74 20 69 6e 20 74 68 65 20 70 72 65 76  ment in the prev
2b4e9 69 6f 75 73 20 6c 69 6e 65 20 69 73 20 70 6f 73  ious line is pos
2b4ea 73 69 62 6c 65 20 6f 6e 6c 79 20 62 65 63 61 75  sible only becau
2b4eb 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  se.      ** the 
2b4ec 57 4f 5f 20 61 6e 64 20 53 51 4c 49 54 45 5f 49  WO_ and SQLITE_I
2b4ed 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
2b4ee 20 63 6f 64 65 73 20 61 72 65 20 69 64 65 6e 74   codes are ident
2b4ef 69 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 20  ical.  The.     
2b4f0 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73   ** following as
2b4f1 73 65 72 74 73 20 76 65 72 69 66 79 20 74 68 69  serts verify thi
2b4f2 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 20 20 20  s fact. */.     
2b4f3 20 61 73 73 65 72 74 28 20 57 4f 5f 45 51 3d 3d   assert( WO_EQ==
2b4f4 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
2b4f5 53 54 52 41 49 4e 54 5f 45 51 20 29 3b 0a 20 20  STRAINT_EQ );.  
2b4f6 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4c      assert( WO_L
2b4f7 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  T==SQLITE_INDEX_
2b4f8 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 29 3b  CONSTRAINT_LT );
2b4f9 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 57  .      assert( W
2b4fa 4f 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  O_LE==SQLITE_IND
2b4fb 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45  EX_CONSTRAINT_LE
2b4fc 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2b4fd 28 20 57 4f 5f 47 54 3d 3d 53 51 4c 49 54 45 5f  ( WO_GT==SQLITE_
2b4fe 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
2b4ff 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _GT );.      ass
2b500 65 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51 4c 49  ert( WO_GE==SQLI
2b501 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
2b502 49 4e 54 5f 47 45 20 29 3b 0a 20 20 20 20 20 20  INT_GE );.      
2b503 61 73 73 65 72 74 28 20 57 4f 5f 4d 41 54 43 48  assert( WO_MATCH
2b504 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
2b505 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20  ONSTRAINT_MATCH 
2b506 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2b507 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2b508 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54  r & (WO_EQ|WO_LT
2b509 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f  |WO_LE|WO_GT|WO_
2b50a 47 45 7c 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a  GE|WO_MATCH) );.
2b50b 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 7d        j++;.    }
2b50c 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2b50d 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a  nOrderBy; i++){.
2b50e 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70        Expr *pExp
2b50f 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  r = pOrderBy->a[
2b510 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
2b511 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 69  pIdxOrderBy[i].i
2b512 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e  Column = pExpr->
2b513 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70  iColumn;.      p
2b514 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65  IdxOrderBy[i].de
2b515 73 63 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  sc = pOrderBy->a
2b516 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20  [i].sortOrder;. 
2b517 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
2b518 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68  t this point, th
2b519 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2b51a 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  info structure t
2b51b 68 61 74 20 70 49 64 78 49 6e 66 6f 20 70 6f 69  hat pIdxInfo poi
2b51c 6e 74 73 0a 20 20 2a 2a 20 74 6f 20 77 69 6c 6c  nts.  ** to will
2b51d 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69   have been initi
2b51e 61 6c 69 7a 65 64 2c 20 65 69 74 68 65 72 20 64  alized, either d
2b51f 75 72 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e  uring the curren
2b520 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 72 0a  t invocation or.
2b521 20 20 2a 2a 20 64 75 72 69 6e 67 20 73 6f 6d 65    ** during some
2b522 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f   prior invocatio
2b523 6e 2e 20 20 4e 6f 77 20 77 65 20 6a 75 73 74 20  n.  Now we just 
2b524 68 61 76 65 20 74 6f 20 63 75 73 74 6f 6d 69 7a  have to customiz
2b525 65 20 74 68 65 0a 20 20 2a 2a 20 64 65 74 61 69  e the.  ** detai
2b526 6c 73 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66  ls of pIdxInfo f
2b527 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 69  or the current i
2b528 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20 70 61  nvocation and pa
2b529 73 73 20 69 74 20 74 6f 0a 20 20 2a 2a 20 78 42  ss it to.  ** xB
2b52a 65 73 74 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a 0a  estIndex..  */..
2b52b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20    /* The module 
2b52c 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 64 65 66  name must be def
2b52d 69 6e 65 64 2e 20 41 6c 73 6f 2c 20 62 79 20 74  ined. Also, by t
2b52e 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20  his point there 
2b52f 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 61 20 70  must.  ** be a p
2b530 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c  ointer to an sql
2b531 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
2b532 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20  ure. Otherwise. 
2b533 20 2a 2a 20 73 71 6c 69 74 65 33 56 69 65 77 47   ** sqlite3ViewG
2b534 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20  etColumnNames() 
2b535 77 6f 75 6c 64 20 68 61 76 65 20 70 69 63 6b 65  would have picke
2b536 64 20 75 70 20 74 68 65 20 65 72 72 6f 72 2e 20  d up the error. 
2b537 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2b538 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  pTab->azModuleAr
2b539 67 20 26 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64  g && pTab->azMod
2b53a 75 6c 65 41 72 67 5b 30 5d 20 29 3b 0a 20 20 61  uleArg[0] );.  a
2b53b 73 73 65 72 74 28 20 70 56 74 61 62 20 29 3b 0a  ssert( pVtab );.
2b53c 23 69 66 20 30 0a 20 20 69 66 28 20 70 54 61 62  #if 0.  if( pTab
2b53d 2d 3e 70 56 74 61 62 3d 3d 30 20 29 7b 0a 20 20  ->pVtab==0 ){.  
2b53e 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2b53f 67 28 70 50 61 72 73 65 2c 20 22 75 6e 64 65 66  g(pParse, "undef
2b540 69 6e 65 64 20 6d 6f 64 75 6c 65 20 25 73 20 66  ined module %s f
2b541 6f 72 20 74 61 62 6c 65 20 25 73 22 2c 0a 20 20  or table %s",.  
2b542 20 20 20 20 20 20 70 54 61 62 2d 3e 61 7a 4d 6f        pTab->azMo
2b543 64 75 6c 65 41 72 67 5b 30 5d 2c 20 70 54 61 62  duleArg[0], pTab
2b544 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ->zName);.    re
2b545 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 23 65  turn 0.0;.  }.#e
2b546 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 65 74 20 74  ndif..  /* Set t
2b547 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  he aConstraint[]
2b548 2e 75 73 61 62 6c 65 20 66 69 65 6c 64 73 20 61  .usable fields a
2b549 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6c  nd initialize al
2b54a 6c 20 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 76  l .  ** output v
2b54b 61 72 69 61 62 6c 65 73 20 74 6f 20 7a 65 72 6f  ariables to zero
2b54c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 43 6f 6e  ..  **.  ** aCon
2b54d 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65  straint[].usable
2b54e 20 69 73 20 74 72 75 65 20 66 6f 72 20 63 6f 6e   is true for con
2b54f 73 74 72 61 69 6e 74 73 20 77 68 65 72 65 20 74  straints where t
2b550 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 20 20  he right-hand.  
2b551 2a 2a 20 73 69 64 65 20 63 6f 6e 74 61 69 6e 73  ** side contains
2b552 20 6f 6e 6c 79 20 72 65 66 65 72 65 6e 63 65 73   only references
2b553 20 74 6f 20 74 61 62 6c 65 73 20 74 6f 20 74 68   to tables to th
2b554 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 63 75  e left of the cu
2b555 72 72 65 6e 74 0a 20 20 2a 2a 20 74 61 62 6c 65  rrent.  ** table
2b556 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
2b557 73 2c 20 69 66 20 74 68 65 20 63 6f 6e 73 74 72  s, if the constr
2b558 61 69 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66  aint is of the f
2b559 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  orm:.  **.  **  
2b55a 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
2b55b 3d 20 65 78 70 72 0a 20 20 2a 2a 0a 20 20 2a 2a  = expr.  **.  **
2b55c 20 61 6e 64 20 77 65 20 61 72 65 20 65 76 61 6c   and we are eval
2b55d 75 61 74 69 6e 67 20 61 20 6a 6f 69 6e 2c 20 74  uating a join, t
2b55e 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69  hen the constrai
2b55f 6e 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20 69 73 20  nt on column is 
2b560 0a 20 20 2a 2a 20 6f 6e 6c 79 20 76 61 6c 69 64  .  ** only valid
2b561 20 69 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 72   if all tables r
2b562 65 66 65 72 65 6e 63 65 64 20 69 6e 20 65 78 70  eferenced in exp
2b563 72 20 6f 63 63 75 72 20 74 6f 20 74 68 65 20 6c  r occur to the l
2b564 65 66 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  eft.  ** of the 
2b565 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
2b566 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a 0a 20 20   column..  **.  
2b567 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69  ** The aConstrai
2b568 6e 74 73 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74  nts[] array cont
2b569 61 69 6e 73 20 65 6e 74 72 69 65 73 20 66 6f 72  ains entries for
2b56a 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73   all constraints
2b56b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 63 75 72  .  ** on the cur
2b56c 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 54 68 61  rent table.  Tha
2b56d 74 20 77 61 79 20 77 65 20 6f 6e 6c 79 20 68 61  t way we only ha
2b56e 76 65 20 74 6f 20 63 6f 6d 70 75 74 65 20 69 74  ve to compute it
2b56f 20 6f 6e 63 65 0a 20 20 2a 2a 20 65 76 65 6e 20   once.  ** even 
2b570 74 68 6f 75 67 68 20 77 65 20 6d 69 67 68 74 20  though we might 
2b571 74 72 79 20 74 6f 20 70 69 63 6b 20 74 68 65 20  try to pick the 
2b572 62 65 73 74 20 69 6e 64 65 78 20 6d 75 6c 74 69  best index multi
2b573 70 6c 65 20 74 69 6d 65 73 2e 0a 20 20 2a 2a 20  ple times..  ** 
2b574 46 6f 72 20 65 61 63 68 20 61 74 74 65 6d 70 74  For each attempt
2b575 20 61 74 20 70 69 63 6b 69 6e 67 20 61 6e 20 69   at picking an i
2b576 6e 64 65 78 2c 20 74 68 65 20 6f 72 64 65 72 20  ndex, the order 
2b577 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
2b578 0a 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69 67 68 74  .  ** join might
2b579 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 73 6f   be different so
2b57a 20 77 65 20 68 61 76 65 20 74 6f 20 72 65 63 6f   we have to reco
2b57b 6d 70 75 74 65 20 74 68 65 20 75 73 61 62 6c 65  mpute the usable
2b57c 20 66 6c 61 67 0a 20 20 2a 2a 20 65 61 63 68 20   flag.  ** each 
2b57d 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 70 49 64  time..  */.  pId
2b57e 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 74  xCons = *(struct
2b57f 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
2b580 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64  onstraint**)&pId
2b581 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
2b582 6e 74 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 70  nt;.  pUsage = p
2b583 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
2b584 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 66 6f 72  aintUsage;.  for
2b585 28 69 3d 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f  (i=0; i<pIdxInfo
2b586 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69  ->nConstraint; i
2b587 2b 2b 2c 20 70 49 64 78 43 6f 6e 73 2b 2b 29 7b  ++, pIdxCons++){
2b588 0a 20 20 20 20 6a 20 3d 20 70 49 64 78 43 6f 6e  .    j = pIdxCon
2b589 73 2d 3e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a  s->iTermOffset;.
2b58a 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
2b58b 2d 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70 49 64 78  ->a[j];.    pIdx
2b58c 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 20 3d 20 20  Cons->usable =  
2b58d 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69  (pTerm->prereqRi
2b58e 67 68 74 20 26 20 6e 6f 74 52 65 61 64 79 29 3d  ght & notReady)=
2b58f 3d 30 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 6d  =0 ?1:0;.  }.  m
2b590 65 6d 73 65 74 28 70 55 73 61 67 65 2c 20 30 2c  emset(pUsage, 0,
2b591 20 73 69 7a 65 6f 66 28 70 55 73 61 67 65 5b 30   sizeof(pUsage[0
2b592 5d 29 2a 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f  ])*pIdxInfo->nCo
2b593 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 69 66 28  nstraint);.  if(
2b594 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54   pIdxInfo->needT
2b595 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20  oFreeIdxStr ){. 
2b596 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2b597 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72  pIdxInfo->idxStr
2b598 29 3b 0a 20 20 7d 0a 20 20 70 49 64 78 49 6e 66  );.  }.  pIdxInf
2b599 6f 2d 3e 69 64 78 53 74 72 20 3d 20 30 3b 0a 20  o->idxStr = 0;. 
2b59a 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 4e 75   pIdxInfo->idxNu
2b59b 6d 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66  m = 0;.  pIdxInf
2b59c 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  o->needToFreeIdx
2b59d 53 74 72 20 3d 20 30 3b 0a 20 20 70 49 64 78 49  Str = 0;.  pIdxI
2b59e 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73  nfo->orderByCons
2b59f 75 6d 65 64 20 3d 20 30 3b 0a 20 20 2f 2a 20 28  umed = 0;.  /* (
2b5a0 28 64 6f 75 62 6c 65 29 32 29 20 49 6e 20 63 61  (double)2) In ca
2b5a1 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
2b5a2 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
2b5a3 2e 2e 2e 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66  ... */.  pIdxInf
2b5a4 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
2b5a5 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42   = SQLITE_BIG_DB
2b5a6 4c 20 2f 20 28 28 64 6f 75 62 6c 65 29 32 29 3b  L / ((double)2);
2b5a7 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 49  .  nOrderBy = pI
2b5a8 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79  dxInfo->nOrderBy
2b5a9 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f  ;.  if( pIdxInfo
2b5aa 2d 3e 6e 4f 72 64 65 72 42 79 20 26 26 20 21 6f  ->nOrderBy && !o
2b5ab 72 64 65 72 42 79 55 73 61 62 6c 65 20 29 7b 0a  rderByUsable ){.
2b5ac 20 20 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78      *(int*)&pIdx
2b5ad 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d  Info->nOrderBy =
2b5ae 20 30 3b 0a 20 20 7d 0a 0a 20 20 28 76 6f 69 64   0;.  }..  (void
2b5af 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
2b5b0 66 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20  f(pParse->db);. 
2b5b1 20 57 48 45 52 45 54 52 41 43 45 28 28 22 78 42   WHERETRACE(("xB
2b5b2 65 73 74 49 6e 64 65 78 20 66 6f 72 20 25 73 5c  estIndex for %s\
2b5b3 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  n", pTab->zName)
2b5b4 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 49  );.  TRACE_IDX_I
2b5b5 4e 50 55 54 53 28 70 49 64 78 49 6e 66 6f 29 3b  NPUTS(pIdxInfo);
2b5b6 0a 20 20 72 63 20 3d 20 70 56 74 61 62 2d 3e 70  .  rc = pVtab->p
2b5b7 4d 6f 64 75 6c 65 2d 3e 78 42 65 73 74 49 6e 64  Module->xBestInd
2b5b8 65 78 28 70 56 74 61 62 2c 20 70 49 64 78 49 6e  ex(pVtab, pIdxIn
2b5b9 66 6f 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58  fo);.  TRACE_IDX
2b5ba 5f 4f 55 54 50 55 54 53 28 70 49 64 78 49 6e 66  _OUTPUTS(pIdxInf
2b5bb 6f 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69  o);.  (void)sqli
2b5bc 74 65 33 53 61 66 65 74 79 4f 6e 28 70 50 61 72  te3SafetyOn(pPar
2b5bd 73 65 2d 3e 64 62 29 3b 0a 0a 20 20 69 66 28 20  se->db);..  if( 
2b5be 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2b5bf 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
2b5c0 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ITE_NOMEM ){.   
2b5c1 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d     pParse->db->m
2b5c2 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2b5c3 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21  .    }else if( !
2b5c4 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 29  pVtab->zErrMsg )
2b5c5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2b5c6 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2b5c7 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72  "%s", sqlite3Err
2b5c8 53 74 72 28 72 63 29 29 3b 0a 20 20 20 20 7d 65  Str(rc));.    }e
2b5c9 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
2b5ca 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2b5cb 65 2c 20 22 25 73 22 2c 20 70 56 74 61 62 2d 3e  e, "%s", pVtab->
2b5cc 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a  zErrMsg);.    }.
2b5cd 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
2b5ce 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ree(pParse->db, 
2b5cf 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b  pVtab->zErrMsg);
2b5d0 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  .  pVtab->zErrMs
2b5d1 67 20 3d 20 30 3b 0a 0a 20 20 66 6f 72 28 69 3d  g = 0;..  for(i=
2b5d2 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e  0; i<pIdxInfo->n
2b5d3 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29  Constraint; i++)
2b5d4 7b 0a 20 20 20 20 69 66 28 20 21 70 49 64 78 49  {.    if( !pIdxI
2b5d5 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
2b5d6 5b 69 5d 2e 75 73 61 62 6c 65 20 26 26 20 70 55  [i].usable && pU
2b5d7 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65  sage[i].argvInde
2b5d8 78 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  x>0 ){.      sql
2b5d9 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2b5da 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
2b5db 22 74 61 62 6c 65 20 25 73 3a 20 78 42 65 73 74  "table %s: xBest
2b5dc 49 6e 64 65 78 20 72 65 74 75 72 6e 65 64 20 61  Index returned a
2b5dd 6e 20 69 6e 76 61 6c 69 64 20 70 6c 61 6e 22 2c  n invalid plan",
2b5de 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
2b5df 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29       /* (double)
2b5e0 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c  0 In case of SQL
2b5e1 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
2b5e2 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20  G_POINT... */.  
2b5e3 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62      return (doub
2b5e4 6c 65 29 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  le)0;.    }.  }.
2b5e5 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49  .  *(int*)&pIdxI
2b5e6 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20  nfo->nOrderBy = 
2b5e7 6e 4f 72 64 65 72 42 79 3b 0a 20 20 72 65 74 75  nOrderBy;.  retu
2b5e8 72 6e 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  rn pIdxInfo->est
2b5e9 69 6d 61 74 65 64 43 6f 73 74 3b 0a 7d 0a 23 65  imatedCost;.}.#e
2b5ea 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2b5eb 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2b5ec 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20   */../*.** Find 
2b5ed 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66  the query plan f
2b5ee 6f 72 20 61 63 63 65 73 73 69 6e 67 20 61 20 70  or accessing a p
2b5ef 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e  articular table.
2b5f0 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 62    Write the.** b
2b5f1 65 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 61  est query plan a
2b5f2 6e 64 20 69 74 73 20 63 6f 73 74 20 69 6e 74 6f  nd its cost into
2b5f3 20 74 68 65 20 57 68 65 72 65 43 6f 73 74 20 6f   the WhereCost o
2b5f4 62 6a 65 63 74 20 73 75 70 70 6c 69 65 64 20 61  bject supplied a
2b5f5 73 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61  s the.** last pa
2b5f6 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  rameter..**.** T
2b5f7 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 70  he lowest cost p
2b5f8 6c 61 6e 20 77 69 6e 73 2e 20 20 54 68 65 20 63  lan wins.  The c
2b5f9 6f 73 74 20 69 73 20 61 6e 20 65 73 74 69 6d 61  ost is an estima
2b5fa 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e 74  te of the amount
2b5fb 20 6f 66 0a 2a 2a 20 43 50 55 20 61 6e 64 20 64   of.** CPU and d
2b5fc 69 73 6b 20 49 2f 4f 20 6e 65 65 64 20 74 6f 20  isk I/O need to 
2b5fd 70 72 6f 63 65 73 73 20 74 68 65 20 72 65 71 75  process the requ
2b5fe 65 73 74 20 75 73 69 6e 67 20 74 68 65 20 73 65  est using the se
2b5ff 6c 65 63 74 65 64 20 70 6c 61 6e 2e 0a 2a 2a 20  lected plan..** 
2b600 46 61 63 74 6f 72 73 20 74 68 61 74 20 69 6e 66  Factors that inf
2b601 6c 75 65 6e 63 65 20 63 6f 73 74 20 69 6e 63 6c  luence cost incl
2b602 75 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  ude:.**.**    * 
2b603 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 20 6e   The estimated n
2b604 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68  umber of rows th
2b605 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 72 69  at will be retri
2b606 65 76 65 64 2e 20 20 28 54 68 65 0a 2a 2a 20 20  eved.  (The.**  
2b607 20 20 20 20 20 66 65 77 65 72 20 74 68 65 20 62       fewer the b
2b608 65 74 74 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 20 20  etter.).**.**   
2b609 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e   *  Whether or n
2b60a 6f 74 20 73 6f 72 74 69 6e 67 20 6d 75 73 74 20  ot sorting must 
2b60b 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  occur..**.**    
2b60c 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f  *  Whether or no
2b60d 74 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20  t there must be 
2b60e 73 65 70 61 72 61 74 65 20 6c 6f 6f 6b 75 70 73  separate lookups
2b60f 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   in the.**      
2b610 20 69 6e 64 65 78 20 61 6e 64 20 69 6e 20 74 68   index and in th
2b611 65 20 6d 61 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a  e main table..**
2b612 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 77 61 73  .** If there was
2b613 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63   an INDEXED BY c
2b614 6c 61 75 73 65 20 28 70 53 72 63 2d 3e 70 49 6e  lause (pSrc->pIn
2b615 64 65 78 29 20 61 74 74 61 63 68 65 64 20 74 6f  dex) attached to
2b616 20 74 68 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a   the table in.**
2b617 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
2b618 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  nt, then this fu
2b619 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 63 6f 6e 73  nction only cons
2b61a 69 64 65 72 73 20 70 6c 61 6e 73 20 75 73 69 6e  iders plans usin
2b61b 67 20 74 68 65 20 0a 2a 2a 20 6e 61 6d 65 64 20  g the .** named 
2b61c 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 73 75 63  index. If no suc
2b61d 68 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64 2c  h plan is found,
2b61e 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
2b61f 65 64 20 63 6f 73 74 20 69 73 0a 2a 2a 20 53 51  ed cost is.** SQ
2b620 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2e 20 49 66  LITE_BIG_DBL. If
2b621 20 61 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64   a plan is found
2b622 20 74 68 61 74 20 75 73 65 73 20 74 68 65 20 6e   that uses the n
2b623 61 6d 65 64 20 69 6e 64 65 78 2c 20 0a 2a 2a 20  amed index, .** 
2b624 74 68 65 6e 20 74 68 65 20 63 6f 73 74 20 69 73  then the cost is
2b625 20 63 61 6c 63 75 6c 61 74 65 64 20 69 6e 20 74   calculated in t
2b626 68 65 20 75 73 75 61 6c 20 77 61 79 2e 0a 2a 2a  he usual way..**
2b627 0a 2a 2a 20 49 66 20 61 20 4e 4f 54 20 49 4e 44  .** If a NOT IND
2b628 45 58 45 44 20 63 6c 61 75 73 65 20 28 70 53 72  EXED clause (pSr
2b629 63 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 21 3d 30  c->notIndexed!=0
2b62a 29 20 77 61 73 20 61 74 74 61 63 68 65 64 20 74  ) was attached t
2b62b 6f 20 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20  o the table .** 
2b62c 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  in the SELECT st
2b62d 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 6e 6f  atement, then no
2b62e 20 69 6e 64 65 78 65 73 20 61 72 65 20 63 6f 6e   indexes are con
2b62f 73 69 64 65 72 65 64 2e 20 48 6f 77 65 76 65 72  sidered. However
2b630 2c 20 74 68 65 20 0a 2a 2a 20 73 65 6c 65 63 74  , the .** select
2b631 65 64 20 70 6c 61 6e 20 6d 61 79 20 73 74 69 6c  ed plan may stil
2b632 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65  l take advantage
2b633 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20 62   of the tables b
2b634 75 69 6c 74 2d 69 6e 20 72 6f 77 69 64 0a 2a 2a  uilt-in rowid.**
2b635 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69   index..*/.stati
2b636 63 20 76 6f 69 64 20 62 65 73 74 49 6e 64 65 78  c void bestIndex
2b637 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2b638 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
2b639 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
2b63a 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72  ontext */.  Wher
2b63b 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2b63c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
2b63d 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2b63e 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2b63f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20  item *pSrc,  /* 
2b640 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
2b641 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a  term to search *
2b642 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52  /.  Bitmask notR
2b643 65 61 64 79 2c 20 20 20 20 20 20 20 20 20 20 20  eady,           
2b644 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f  /* Mask of curso
2b645 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20  rs that are not 
2b646 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45  available */.  E
2b647 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
2b648 79 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  y,         /* Th
2b649 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2b64a 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73 74  e */.  WhereCost
2b64b 20 2a 70 43 6f 73 74 20 20 20 20 20 20 20 20 20   *pCost         
2b64c 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73     /* Lowest cos
2b64d 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a  t query plan */.
2b64e 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  ){.  WhereTerm *
2b64f 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  pTerm;          
2b650 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74 65 72   /* A single ter
2b651 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  m of the WHERE c
2b652 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
2b653 43 75 72 20 3d 20 70 53 72 63 2d 3e 69 43 75 72  Cur = pSrc->iCur
2b654 73 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75  sor;   /* The cu
2b655 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c  rsor of the tabl
2b656 65 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64  e to be accessed
2b657 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50 72   */.  Index *pPr
2b658 6f 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  obe;            
2b659 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 77 65    /* An index we
2b65a 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20   are evaluating 
2b65b 2a 2f 0a 20 20 69 6e 74 20 72 65 76 3b 20 20 20  */.  int rev;   
2b65c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b65d 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 63 61 6e   /* True to scan
2b65e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65   in reverse orde
2b65f 72 20 2a 2f 0a 20 20 69 6e 74 20 77 73 46 6c 61  r */.  int wsFla
2b660 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
2b661 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f     /* Flags asso
2b662 63 69 61 74 65 64 20 77 69 74 68 20 70 50 72 6f  ciated with pPro
2b663 62 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 3b  be */.  int nEq;
2b664 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b665 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2b666 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72   == or IN constr
2b667 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 65  aints */.  int e
2b668 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20  qTermMask;      
2b669 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
2b66a 66 20 76 61 6c 69 64 20 65 71 75 61 6c 69 74 79  f valid equality
2b66b 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f 0a 20 20   operators */.  
2b66c 64 6f 75 62 6c 65 20 63 6f 73 74 3b 20 20 20 20  double cost;    
2b66d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2b66e 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 70 50 72  ost of using pPr
2b66f 6f 62 65 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20  obe */.  double 
2b670 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20  nRow;           
2b671 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65       /* Estimate
2b672 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  d number of rows
2b673 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a   in result set *
2b674 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
2b675 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b676 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
2b677 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61 73  */.  Bitmask mas
2b678 6b 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  kSrc;           
2b679 20 2f 2a 20 42 69 74 6d 61 73 6b 20 66 6f 72 20   /* Bitmask for 
2b67a 74 68 65 20 70 53 72 63 20 74 61 62 6c 65 20 2a  the pSrc table *
2b67b 2f 0a 0a 20 20 57 48 45 52 45 54 52 41 43 45 28  /..  WHERETRACE(
2b67c 28 22 62 65 73 74 49 6e 64 65 78 3a 20 74 62 6c  ("bestIndex: tbl
2b67d 3d 25 73 20 6e 6f 74 52 65 61 64 79 3d 25 6c 6c  =%s notReady=%ll
2b67e 78 5c 6e 22 2c 20 70 53 72 63 2d 3e 70 54 61 62  x\n", pSrc->pTab
2b67f 2d 3e 7a 4e 61 6d 65 2c 6e 6f 74 52 65 61 64 79  ->zName,notReady
2b680 29 29 3b 0a 20 20 70 50 72 6f 62 65 20 3d 20 70  ));.  pProbe = p
2b681 53 72 63 2d 3e 70 54 61 62 2d 3e 70 49 6e 64 65  Src->pTab->pInde
2b682 78 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e  x;.  if( pSrc->n
2b683 6f 74 49 6e 64 65 78 65 64 20 29 7b 0a 20 20 20  otIndexed ){.   
2b684 20 70 50 72 6f 62 65 20 3d 20 30 3b 0a 20 20 7d   pProbe = 0;.  }
2b685 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61  ..  /* If the ta
2b686 62 6c 65 20 68 61 73 20 6e 6f 20 69 6e 64 69 63  ble has no indic
2b687 65 73 20 61 6e 64 20 74 68 65 72 65 20 61 72 65  es and there are
2b688 20 6e 6f 20 74 65 72 6d 73 20 69 6e 20 74 68 65   no terms in the
2b689 20 77 68 65 72 65 0a 20 20 2a 2a 20 63 6c 61 75   where.  ** clau
2b68a 73 65 20 74 68 61 74 20 72 65 66 65 72 20 74 6f  se that refer to
2b68b 20 74 68 65 20 52 4f 57 49 44 2c 20 74 68 65 6e   the ROWID, then
2b68c 20 77 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62   we will never b
2b68d 65 20 61 62 6c 65 20 74 6f 20 64 6f 0a 20 20 2a  e able to do.  *
2b68e 2a 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72  * anything other
2b68f 20 74 68 61 6e 20 61 20 66 75 6c 6c 20 74 61 62   than a full tab
2b690 6c 65 20 73 63 61 6e 20 6f 6e 20 74 68 69 73 20  le scan on this 
2b691 74 61 62 6c 65 2e 20 20 57 65 20 6d 69 67 68 74  table.  We might
2b692 20 61 73 0a 20 20 2a 2a 20 77 65 6c 6c 20 70 75   as.  ** well pu
2b693 74 20 69 74 20 66 69 72 73 74 20 69 6e 20 74 68  t it first in th
2b694 65 20 6a 6f 69 6e 20 6f 72 64 65 72 2e 20 20 54  e join order.  T
2b695 68 61 74 20 77 61 79 2c 20 70 65 72 68 61 70 73  hat way, perhaps
2b696 20 69 74 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20   it can be.  ** 
2b697 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 6f 74  referenced by ot
2b698 68 65 72 20 74 61 62 6c 65 73 20 69 6e 20 74 68  her tables in th
2b699 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 6d  e join..  */.  m
2b69a 65 6d 73 65 74 28 70 43 6f 73 74 2c 20 30 2c 20  emset(pCost, 0, 
2b69b 73 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b  sizeof(*pCost));
2b69c 0a 20 20 69 66 28 20 70 50 72 6f 62 65 3d 3d 30  .  if( pProbe==0
2b69d 20 26 26 0a 20 20 20 20 20 66 69 6e 64 54 65 72   &&.     findTer
2b69e 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c  m(pWC, iCur, -1,
2b69f 20 30 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c   0, WO_EQ|WO_IN|
2b6a0 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47  WO_LT|WO_LE|WO_G
2b6a1 54 7c 57 4f 5f 47 45 2c 30 29 3d 3d 30 20 26 26  T|WO_GE,0)==0 &&
2b6a2 0a 20 20 20 20 20 28 70 4f 72 64 65 72 42 79 3d  .     (pOrderBy=
2b6a3 3d 30 20 7c 7c 20 21 73 6f 72 74 61 62 6c 65 42  =0 || !sortableB
2b6a4 79 52 6f 77 69 64 28 69 43 75 72 2c 20 70 4f 72  yRowid(iCur, pOr
2b6a5 64 65 72 42 79 2c 20 70 57 43 2d 3e 70 4d 61 73  derBy, pWC->pMas
2b6a6 6b 53 65 74 2c 20 26 72 65 76 29 29 20 29 7b 0a  kSet, &rev)) ){.
2b6a7 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2b6a8 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d    pCost->rCost =
2b6a9 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 3b   SQLITE_BIG_DBL;
2b6aa 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
2b6ab 20 61 20 72 6f 77 69 64 3d 45 58 50 52 20 6f 72   a rowid=EXPR or
2b6ac 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29 20   rowid IN (...) 
2b6ad 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 49 66 20  constraints. If 
2b6ae 74 68 65 72 65 20 77 61 73 0a 20 20 2a 2a 20 61  there was.  ** a
2b6af 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  n INDEXED BY cla
2b6b0 75 73 65 20 61 74 74 61 63 68 65 64 20 74 6f 20  use attached to 
2b6b1 74 68 69 73 20 74 61 62 6c 65 2c 20 73 6b 69 70  this table, skip
2b6b2 20 74 68 69 73 20 73 74 65 70 2e 0a 20 20 2a 2f   this step..  */
2b6b3 0a 20 20 69 66 28 20 21 70 53 72 63 2d 3e 70 49  .  if( !pSrc->pI
2b6b4 6e 64 65 78 20 29 7b 0a 20 20 20 20 70 54 65 72  ndex ){.    pTer
2b6b5 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43  m = findTerm(pWC
2b6b6 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52  , iCur, -1, notR
2b6b7 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49  eady, WO_EQ|WO_I
2b6b8 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  N, 0);.    if( p
2b6b9 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 45 78  Term ){.      Ex
2b6ba 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20 20  pr *pExpr;.     
2b6bb 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46   pCost->plan.wsF
2b6bc 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 52 4f 57  lags = WHERE_ROW
2b6bd 49 44 5f 45 51 3b 0a 20 20 20 20 20 20 69 66 28  ID_EQ;.      if(
2b6be 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2b6bf 72 20 26 20 57 4f 5f 45 51 20 29 7b 0a 20 20 20  r & WO_EQ ){.   
2b6c0 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 3d 3d 20       /* Rowid== 
2b6c1 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 62 65  is always the be
2b6c2 73 74 20 70 69 63 6b 2e 20 20 4c 6f 6f 6b 20 6e  st pick.  Look n
2b6c3 6f 20 66 75 72 74 68 65 72 2e 20 20 42 65 63 61  o further.  Beca
2b6c4 75 73 65 20 6f 6e 6c 79 0a 20 20 20 20 20 20 20  use only.       
2b6c5 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 72 6f 77   ** a single row
2b6c6 20 69 73 20 67 65 6e 65 72 61 74 65 64 2c 20 6f   is generated, o
2b6c7 75 74 70 75 74 20 69 73 20 61 6c 77 61 79 73 20  utput is always 
2b6c8 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
2b6c9 2a 2f 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74  */.        pCost
2b6ca 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d  ->plan.wsFlags =
2b6cb 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20   WHERE_ROWID_EQ 
2b6cc 7c 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b 0a  | WHERE_UNIQUE;.
2b6cd 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70          pCost->p
2b6ce 6c 61 6e 2e 6e 45 71 20 3d 20 31 3b 0a 20 20 20  lan.nEq = 1;.   
2b6cf 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
2b6d0 28 22 2e 2e 2e 20 62 65 73 74 20 69 73 20 72 6f  ("... best is ro
2b6d1 77 69 64 5c 6e 22 29 29 3b 0a 20 20 20 20 20 20  wid\n"));.      
2b6d2 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d    pCost->rCost =
2b6d3 20 30 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73   0;.        pCos
2b6d4 74 2d 3e 6e 52 6f 77 20 3d 20 31 3b 0a 20 20 20  t->nRow = 1;.   
2b6d5 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2b6d6 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 45 78     }else if( !Ex
2b6d7 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 28 70  prHasProperty((p
2b6d8 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45  Expr = pTerm->pE
2b6d9 78 70 72 29 2c 20 45 50 5f 78 49 73 53 65 6c 65  xpr), EP_xIsSele
2b6da 63 74 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  ct) .           
2b6db 20 20 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 4c    && pExpr->x.pL
2b6dc 69 73 74 20 0a 20 20 20 20 20 20 29 7b 0a 20 20  ist .      ){.  
2b6dd 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 49        /* Rowid I
2b6de 4e 20 28 4c 49 53 54 29 3a 20 63 6f 73 74 20 69  N (LIST): cost i
2b6df 73 20 4e 6c 6f 67 4e 20 77 68 65 72 65 20 4e 20  s NlogN where N 
2b6e0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2b6e1 20 6c 69 73 74 0a 20 20 20 20 20 20 20 20 2a 2a   list.        **
2b6e2 20 65 6c 65 6d 65 6e 74 73 2e 20 20 2a 2f 0a 20   elements.  */. 
2b6e3 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72 43         pCost->rC
2b6e4 6f 73 74 20 3d 20 70 43 6f 73 74 2d 3e 6e 52 6f  ost = pCost->nRo
2b6e5 77 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  w = pExpr->x.pLi
2b6e6 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20  st->nExpr;.     
2b6e7 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20     pCost->rCost 
2b6e8 2a 3d 20 65 73 74 4c 6f 67 28 70 43 6f 73 74 2d  *= estLog(pCost-
2b6e9 3e 72 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 7d  >rCost);.      }
2b6ea 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
2b6eb 20 52 6f 77 69 64 20 49 4e 20 28 53 45 4c 45 43   Rowid IN (SELEC
2b6ec 54 29 3a 20 63 6f 73 74 20 69 73 20 4e 6c 6f 67  T): cost is Nlog
2b6ed 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  N where N is the
2b6ee 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a   number of rows.
2b6ef 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68          ** in th
2b6f0 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
2b6f1 69 6e 6e 65 72 20 73 65 6c 65 63 74 2e 20 20 57  inner select.  W
2b6f2 65 20 68 61 76 65 20 6e 6f 20 77 61 79 20 74 6f  e have no way to
2b6f3 20 65 73 74 69 6d 61 74 65 0a 20 20 20 20 20 20   estimate.      
2b6f4 20 20 2a 2a 20 74 68 61 74 20 76 61 6c 75 65 20    ** that value 
2b6f5 73 6f 20 6d 61 6b 65 20 61 20 77 69 6c 64 20 67  so make a wild g
2b6f6 75 65 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20  uess. */.       
2b6f7 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 31   pCost->nRow = 1
2b6f8 30 30 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73  00;.        pCos
2b6f9 74 2d 3e 72 43 6f 73 74 20 3d 20 32 30 30 3b 0a  t->rCost = 200;.
2b6fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 57 48        }.      WH
2b6fb 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 72  ERETRACE(("... r
2b6fc 6f 77 69 64 20 49 4e 20 63 6f 73 74 3a 20 25 2e  owid IN cost: %.
2b6fd 39 67 5c 6e 22 2c 20 70 43 6f 73 74 2d 3e 72 43  9g\n", pCost->rC
2b6fe 6f 73 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20 0a  ost));.    }.  .
2b6ff 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 20      /* Estimate 
2b700 74 68 65 20 63 6f 73 74 20 6f 66 20 61 20 74 61  the cost of a ta
2b701 62 6c 65 20 73 63 61 6e 2e 20 20 49 66 20 77 65  ble scan.  If we
2b702 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77   do not know how
2b703 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 65 6e 74   many.    ** ent
2b704 72 69 65 73 20 61 72 65 20 69 6e 20 74 68 65 20  ries are in the 
2b705 74 61 62 6c 65 2c 20 75 73 65 20 31 20 6d 69 6c  table, use 1 mil
2b706 6c 69 6f 6e 20 61 73 20 61 20 67 75 65 73 73 2e  lion as a guess.
2b707 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f 73 74  .    */.    cost
2b708 20 3d 20 70 50 72 6f 62 65 20 3f 20 70 50 72 6f   = pProbe ? pPro
2b709 62 65 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 20  be->aiRowEst[0] 
2b70a 3a 20 31 30 30 30 30 30 30 3b 0a 20 20 20 20 57  : 1000000;.    W
2b70b 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
2b70c 74 61 62 6c 65 20 73 63 61 6e 20 62 61 73 65 20  table scan base 
2b70d 63 6f 73 74 3a 20 25 2e 39 67 5c 6e 22 2c 20 63  cost: %.9g\n", c
2b70e 6f 73 74 29 29 3b 0a 20 20 20 20 77 73 46 6c 61  ost));.    wsFla
2b70f 67 73 20 3d 20 57 48 45 52 45 5f 52 4f 57 49 44  gs = WHERE_ROWID
2b710 5f 52 41 4e 47 45 3b 0a 20 20 0a 20 20 20 20 2f  _RANGE;.  .    /
2b711 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6e 73  * Check for cons
2b712 74 72 61 69 6e 74 73 20 6f 6e 20 61 20 72 61 6e  traints on a ran
2b713 67 65 20 6f 66 20 72 6f 77 69 64 73 20 69 6e 20  ge of rowids in 
2b714 61 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a 20 20  a table scan..  
2b715 20 20 2a 2f 0a 20 20 20 20 70 54 65 72 6d 20 3d    */.    pTerm =
2b716 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2b717 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64  Cur, -1, notRead
2b718 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57  y, WO_LT|WO_LE|W
2b719 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a  O_GT|WO_GE, 0);.
2b71a 20 20 20 20 69 66 28 20 70 54 65 72 6d 20 29 7b      if( pTerm ){
2b71b 0a 20 20 20 20 20 20 69 66 28 20 66 69 6e 64 54  .      if( findT
2b71c 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d  erm(pWC, iCur, -
2b71d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f  1, notReady, WO_
2b71e 4c 54 7c 57 4f 5f 4c 45 2c 20 30 29 20 29 7b 0a  LT|WO_LE, 0) ){.
2b71f 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2b720 7c 3d 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d  |= WHERE_TOP_LIM
2b721 49 54 3b 0a 20 20 20 20 20 20 20 20 63 6f 73 74  IT;.        cost
2b722 20 2f 3d 20 33 3b 20 20 2f 2a 20 47 75 65 73 73   /= 3;  /* Guess
2b723 20 74 68 61 74 20 72 6f 77 69 64 3c 45 58 50 52   that rowid<EXPR
2b724 20 65 6c 69 6d 69 6e 61 74 65 73 20 74 77 6f 2d   eliminates two-
2b725 74 68 69 72 64 73 20 6f 66 20 72 6f 77 73 20 2a  thirds of rows *
2b726 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  /.      }.      
2b727 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57 43  if( findTerm(pWC
2b728 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52  , iCur, -1, notR
2b729 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47  eady, WO_GT|WO_G
2b72a 45 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20  E, 0) ){.       
2b72b 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2b72c 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20  E_BTM_LIMIT;.   
2b72d 20 20 20 20 20 63 6f 73 74 20 2f 3d 20 33 3b 20       cost /= 3; 
2b72e 20 2f 2a 20 47 75 65 73 73 20 74 68 61 74 20 72   /* Guess that r
2b72f 6f 77 69 64 3e 45 58 50 52 20 65 6c 69 6d 69 6e  owid>EXPR elimin
2b730 61 74 65 73 20 74 77 6f 2d 74 68 69 72 64 73 20  ates two-thirds 
2b731 6f 66 20 72 6f 77 73 20 2a 2f 0a 20 20 20 20 20  of rows */.     
2b732 20 7d 0a 20 20 20 20 20 20 57 48 45 52 45 54 52   }.      WHERETR
2b733 41 43 45 28 28 22 2e 2e 2e 20 72 6f 77 69 64 20  ACE(("... rowid 
2b734 72 61 6e 67 65 20 72 65 64 75 63 65 73 20 63 6f  range reduces co
2b735 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63  st to %.9g\n", c
2b736 6f 73 74 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ost));.    }else
2b737 7b 0a 20 20 20 20 20 20 77 73 46 6c 61 67 73 20  {.      wsFlags 
2b738 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  = 0;.    }.    n
2b739 52 6f 77 20 3d 20 63 6f 73 74 3b 0a 20 20 0a 20  Row = cost;.  . 
2b73a 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62     /* If the tab
2b73b 6c 65 20 73 63 61 6e 20 64 6f 65 73 20 6e 6f 74  le scan does not
2b73c 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 44   satisfy the ORD
2b73d 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 69 6e  ER BY clause, in
2b73e 63 72 65 61 73 65 0a 20 20 20 20 2a 2a 20 74 68  crease.    ** th
2b73f 65 20 63 6f 73 74 20 62 79 20 4e 6c 6f 67 4e 20  e cost by NlogN 
2b740 74 6f 20 63 6f 76 65 72 20 74 68 65 20 65 78 70  to cover the exp
2b741 65 6e 73 65 20 6f 66 20 73 6f 72 74 69 6e 67 2e  ense of sorting.
2b742 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72 64   */.    if( pOrd
2b743 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69 66  erBy ){.      if
2b744 28 20 73 6f 72 74 61 62 6c 65 42 79 52 6f 77 69  ( sortableByRowi
2b745 64 28 69 43 75 72 2c 20 70 4f 72 64 65 72 42 79  d(iCur, pOrderBy
2b746 2c 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c  , pWC->pMaskSet,
2b747 20 26 72 65 76 29 20 29 7b 0a 20 20 20 20 20 20   &rev) ){.      
2b748 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2b749 52 45 5f 4f 52 44 45 52 42 59 7c 57 48 45 52 45  RE_ORDERBY|WHERE
2b74a 5f 52 4f 57 49 44 5f 52 41 4e 47 45 3b 0a 20 20  _ROWID_RANGE;.  
2b74b 20 20 20 20 20 20 69 66 28 20 72 65 76 20 29 7b        if( rev ){
2b74c 0a 20 20 20 20 20 20 20 20 20 20 77 73 46 6c 61  .          wsFla
2b74d 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56 45  gs |= WHERE_REVE
2b74e 52 53 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  RSE;.        }. 
2b74f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2b750 20 20 20 20 63 6f 73 74 20 2b 3d 20 63 6f 73 74      cost += cost
2b751 2a 65 73 74 4c 6f 67 28 63 6f 73 74 29 3b 0a 20  *estLog(cost);. 
2b752 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43         WHERETRAC
2b753 45 28 28 22 2e 2e 2e 20 73 6f 72 74 69 6e 67 20  E(("... sorting 
2b754 69 6e 63 72 65 61 73 65 73 20 63 6f 73 74 20 74  increases cost t
2b755 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29  o %.9g\n", cost)
2b756 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2b757 65 6c 73 65 20 69 66 28 20 70 50 61 72 73 65 2d  else if( pParse-
2b758 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
2b759 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72  ITE_ReverseOrder
2b75a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 6f 72   ){.      /* For
2b75b 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 65 73   application tes
2b75c 74 69 6e 67 2c 20 72 61 6e 64 6f 6d 6c 79 20 72  ting, randomly r
2b75d 65 76 65 72 73 65 20 74 68 65 20 6f 75 74 70 75  everse the outpu
2b75e 74 20 6f 72 64 65 72 20 66 6f 72 0a 20 20 20 20  t order for.    
2b75f 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74    ** SELECT stat
2b760 65 6d 65 6e 74 73 20 74 68 61 74 20 6f 6d 69 74  ements that omit
2b761 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2b762 61 75 73 65 2e 20 20 54 68 69 73 20 77 69 6c 6c  ause.  This will
2b763 20 68 65 6c 70 0a 20 20 20 20 20 20 2a 2a 20 74   help.      ** t
2b764 6f 20 66 69 6e 64 20 63 61 73 65 73 20 77 68 65  o find cases whe
2b765 72 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  re.      */.    
2b766 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2b767 52 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 20 20  RE_REVERSE;.    
2b768 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 65 6d 62  }..    /* Rememb
2b769 65 72 20 74 68 69 73 20 63 61 73 65 20 69 66 20  er this case if 
2b76a 69 74 20 69 73 20 74 68 65 20 62 65 73 74 20 73  it is the best s
2b76b 6f 20 66 61 72 20 2a 2f 0a 20 20 20 20 69 66 28  o far */.    if(
2b76c 20 63 6f 73 74 3c 70 43 6f 73 74 2d 3e 72 43 6f   cost<pCost->rCo
2b76d 73 74 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 73  st ){.      pCos
2b76e 74 2d 3e 72 43 6f 73 74 20 3d 20 63 6f 73 74 3b  t->rCost = cost;
2b76f 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 6e 52  .      pCost->nR
2b770 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20 20 20  ow = nRow;.     
2b771 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46   pCost->plan.wsF
2b772 6c 61 67 73 20 3d 20 77 73 46 6c 61 67 73 3b 0a  lags = wsFlags;.
2b773 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 6e 64      }.  }..#ifnd
2b774 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  ef SQLITE_OMIT_O
2b775 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20  R_OPTIMIZATION. 
2b776 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61   /* Search for a
2b777 6e 20 4f 52 2d 63 6c 61 75 73 65 20 74 68 61 74  n OR-clause that
2b778 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
2b779 6c 6f 6f 6b 20 75 70 20 74 68 65 20 74 61 62 6c  look up the tabl
2b77a 65 2e 0a 20 20 2a 2f 0a 20 20 6d 61 73 6b 53 72  e..  */.  maskSr
2b77b 63 20 3d 20 67 65 74 4d 61 73 6b 28 70 57 43 2d  c = getMask(pWC-
2b77c 3e 70 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29  >pMaskSet, iCur)
2b77d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 54 65  ;.  for(i=0, pTe
2b77e 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43  rm=pWC->a; i<pWC
2b77f 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54  ->nTerm; i++, pT
2b780 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 57 68 65 72  erm++){.    Wher
2b781 65 43 6c 61 75 73 65 20 74 65 6d 70 57 43 3b 0a  eClause tempWC;.
2b782 20 20 20 20 74 65 6d 70 57 43 20 3d 20 2a 70 57      tempWC = *pW
2b783 43 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  C;.    if( pTerm
2b784 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2b785 4f 52 20 0a 20 20 20 20 20 20 20 20 26 26 20 28  OR .        && (
2b786 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c  (pTerm->prereqAl
2b787 6c 20 26 20 7e 6d 61 73 6b 53 72 63 29 20 26 20  l & ~maskSrc) & 
2b788 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20 20 20  notReady)==0.   
2b789 20 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e       && (pTerm->
2b78a 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78  u.pOrInfo->index
2b78b 61 62 6c 65 20 26 20 6d 61 73 6b 53 72 63 29 21  able & maskSrc)!
2b78c 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72  =0 ){.      Wher
2b78d 65 43 6c 61 75 73 65 20 2a 70 4f 72 57 43 20 3d  eClause *pOrWC =
2b78e 20 26 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e   &pTerm->u.pOrIn
2b78f 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 57 68  fo->wc;.      Wh
2b790 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d  ereTerm *pOrTerm
2b791 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
2b792 20 20 20 20 20 69 6e 74 20 73 6f 72 74 61 62 6c       int sortabl
2b793 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 75  e = 0;.      dou
2b794 62 6c 65 20 72 54 6f 74 61 6c 20 3d 20 30 3b 0a  ble rTotal = 0;.
2b795 20 20 20 20 20 20 6e 52 6f 77 20 3d 20 30 3b 0a        nRow = 0;.
2b796 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70        for(j=0, p
2b797 4f 72 54 65 72 6d 3d 70 4f 72 57 43 2d 3e 61 3b  OrTerm=pOrWC->a;
2b798 20 6a 3c 70 4f 72 57 43 2d 3e 6e 54 65 72 6d 3b   j<pOrWC->nTerm;
2b799 20 6a 2b 2b 2c 20 70 4f 72 54 65 72 6d 2b 2b 29   j++, pOrTerm++)
2b79a 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 43  {.        WhereC
2b79b 6f 73 74 20 73 54 65 72 6d 43 6f 73 74 3b 0a 20  ost sTermCost;. 
2b79c 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43         WHERETRAC
2b79d 45 28 28 22 2e 2e 2e 20 4d 75 6c 74 69 2d 69 6e  E(("... Multi-in
2b79e 64 65 78 20 4f 52 20 74 65 73 74 69 6e 67 20 66  dex OR testing f
2b79f 6f 72 20 74 65 72 6d 20 25 64 20 6f 66 20 25 64  or term %d of %d
2b7a0 2e 2e 2e 2e 5c 6e 22 2c 20 6a 2c 69 29 29 3b 0a  ....\n", j,i));.
2b7a1 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 72 54          if( pOrT
2b7a2 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
2b7a3 57 4f 5f 41 4e 44 20 29 7b 0a 20 20 20 20 20 20  WO_AND ){.      
2b7a4 20 20 20 20 57 68 65 72 65 43 6c 61 75 73 65 20      WhereClause 
2b7a5 2a 70 41 6e 64 57 43 20 3d 20 26 70 4f 72 54 65  *pAndWC = &pOrTe
2b7a6 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 2d 3e  rm->u.pAndInfo->
2b7a7 77 63 3b 0a 20 20 20 20 20 20 20 20 20 20 62 65  wc;.          be
2b7a8 73 74 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  stIndex(pParse, 
2b7a9 70 41 6e 64 57 43 2c 20 70 53 72 63 2c 20 6e 6f  pAndWC, pSrc, no
2b7aa 74 52 65 61 64 79 2c 20 30 2c 20 26 73 54 65 72  tReady, 0, &sTer
2b7ab 6d 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20  mCost);.        
2b7ac 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72  }else if( pOrTer
2b7ad 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69  m->leftCursor==i
2b7ae 43 75 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Cur ){.         
2b7af 20 74 65 6d 70 57 43 2e 61 20 3d 20 70 4f 72 54   tempWC.a = pOrT
2b7b0 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 74  erm;.          t
2b7b1 65 6d 70 57 43 2e 6e 54 65 72 6d 20 3d 20 31 3b  empWC.nTerm = 1;
2b7b2 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74 49  .          bestI
2b7b3 6e 64 65 78 28 70 50 61 72 73 65 2c 20 26 74 65  ndex(pParse, &te
2b7b4 6d 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52  mpWC, pSrc, notR
2b7b5 65 61 64 79 2c 20 30 2c 20 26 73 54 65 72 6d 43  eady, 0, &sTermC
2b7b6 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  ost);.        }e
2b7b7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
2b7b8 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2b7b9 20 7d 0a 20 20 20 20 20 20 20 20 72 54 6f 74 61   }.        rTota
2b7ba 6c 20 2b 3d 20 73 54 65 72 6d 43 6f 73 74 2e 72  l += sTermCost.r
2b7bb 43 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 6e 52  Cost;.        nR
2b7bc 6f 77 20 2b 3d 20 73 54 65 72 6d 43 6f 73 74 2e  ow += sTermCost.
2b7bd 6e 52 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66  nRow;.        if
2b7be 28 20 72 54 6f 74 61 6c 3e 3d 70 43 6f 73 74 2d  ( rTotal>=pCost-
2b7bf 3e 72 43 6f 73 74 20 29 20 62 72 65 61 6b 3b 0a  >rCost ) break;.
2b7c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2b7c1 28 20 70 4f 72 64 65 72 42 79 21 3d 30 20 29 7b  ( pOrderBy!=0 ){
2b7c2 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 6f 72  .        if( sor
2b7c3 74 61 62 6c 65 42 79 52 6f 77 69 64 28 69 43 75  tableByRowid(iCu
2b7c4 72 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 57 43  r, pOrderBy, pWC
2b7c5 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 26 72 65 76  ->pMaskSet, &rev
2b7c6 29 20 26 26 20 21 72 65 76 20 29 7b 0a 20 20 20  ) && !rev ){.   
2b7c7 20 20 20 20 20 20 20 73 6f 72 74 61 62 6c 65 20         sortable 
2b7c8 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 1;.        }el
2b7c9 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 54  se{.          rT
2b7ca 6f 74 61 6c 20 2b 3d 20 6e 52 6f 77 2a 65 73 74  otal += nRow*est
2b7cb 4c 6f 67 28 6e 52 6f 77 29 3b 0a 20 20 20 20 20  Log(nRow);.     
2b7cc 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
2b7cd 28 22 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e  ("... sorting in
2b7ce 63 72 65 61 73 65 73 20 4f 52 20 63 6f 73 74 20  creases OR cost 
2b7cf 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 72 54 6f 74  to %.9g\n", rTot
2b7d0 61 6c 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  al));.        }.
2b7d1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 57 48        }.      WH
2b7d2 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 6d  ERETRACE(("... m
2b7d3 75 6c 74 69 2d 69 6e 64 65 78 20 4f 52 20 63 6f  ulti-index OR co
2b7d4 73 74 3d 25 2e 39 67 20 6e 72 6f 77 3d 25 2e 39  st=%.9g nrow=%.9
2b7d5 67 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  g\n",.          
2b7d6 20 20 20 20 20 20 20 20 72 54 6f 74 61 6c 2c 20          rTotal, 
2b7d7 6e 52 6f 77 29 29 3b 0a 20 20 20 20 20 20 69 66  nRow));.      if
2b7d8 28 20 72 54 6f 74 61 6c 3c 70 43 6f 73 74 2d 3e  ( rTotal<pCost->
2b7d9 72 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 20  rCost ){.       
2b7da 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20   pCost->rCost = 
2b7db 72 54 6f 74 61 6c 3b 0a 20 20 20 20 20 20 20 20  rTotal;.        
2b7dc 70 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52  pCost->nRow = nR
2b7dd 6f 77 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73  ow;.        pCos
2b7de 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  t->plan.wsFlags 
2b7df 3d 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52  = WHERE_MULTI_OR
2b7e0 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d  ;.        pCost-
2b7e1 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d 20 3d 20  >plan.u.pTerm = 
2b7e2 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 69  pTerm;.        i
2b7e3 66 28 20 73 6f 72 74 61 62 6c 65 20 29 7b 0a 20  f( sortable ){. 
2b7e4 20 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e           pCost->
2b7e5 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57  plan.wsFlags = W
2b7e6 48 45 52 45 5f 4f 52 44 45 52 42 59 7c 57 48 45  HERE_ORDERBY|WHE
2b7e7 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0a 20 20 20  RE_MULTI_OR;.   
2b7e8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2b7e9 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20     }.  }.#endif 
2b7ea 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  /* SQLITE_OMIT_O
2b7eb 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a  R_OPTIMIZATION *
2b7ec 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70  /..  /* If the p
2b7ed 53 72 63 20 74 61 62 6c 65 20 69 73 20 74 68 65  Src table is the
2b7ee 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
2b7ef 61 20 4c 45 46 54 20 4a 4f 49 4e 20 74 68 65 6e  a LEFT JOIN then
2b7f0 20 77 65 20 6d 61 79 20 6e 6f 74 0a 20 20 2a 2a   we may not.  **
2b7f1 20 75 73 65 20 61 6e 20 69 6e 64 65 78 20 74 6f   use an index to
2b7f2 20 73 61 74 69 73 66 79 20 49 53 20 4e 55 4c 4c   satisfy IS NULL
2b7f3 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20   constraints on 
2b7f4 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68 69  that table.  Thi
2b7f5 73 20 69 73 0a 20 20 2a 2a 20 62 65 63 61 75 73  s is.  ** becaus
2b7f6 65 20 63 6f 6c 75 6d 6e 73 20 6d 69 67 68 74 20  e columns might 
2b7f7 65 6e 64 20 75 70 20 62 65 69 6e 67 20 4e 55 4c  end up being NUL
2b7f8 4c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 64  L if the table d
2b7f9 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 2d 0a  oes not match -.
2b7fa 20 20 2a 2a 20 61 20 63 69 72 63 75 6d 73 74 61    ** a circumsta
2b7fb 6e 63 65 20 77 68 69 63 68 20 74 68 65 20 69 6e  nce which the in
2b7fc 64 65 78 20 63 61 6e 6e 6f 74 20 68 65 6c 70 20  dex cannot help 
2b7fd 75 73 20 64 69 73 63 6f 76 65 72 2e 20 20 54 69  us discover.  Ti
2b7fe 63 6b 65 74 20 23 32 31 37 37 2e 0a 20 20 2a 2f  cket #2177..  */
2b7ff 0a 20 20 69 66 28 20 28 70 53 72 63 2d 3e 6a 6f  .  if( (pSrc->jo
2b800 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54  intype & JT_LEFT
2b801 29 21 3d 30 20 29 7b 0a 20 20 20 20 65 71 54 65  )!=0 ){.    eqTe
2b802 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57  rmMask = WO_EQ|W
2b803 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  O_IN;.  }else{. 
2b804 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20     eqTermMask = 
2b805 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 49  WO_EQ|WO_IN|WO_I
2b806 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  SNULL;.  }..  /*
2b807 20 4c 6f 6f 6b 20 61 74 20 65 61 63 68 20 69 6e   Look at each in
2b808 64 65 78 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  dex..  */.  if( 
2b809 70 53 72 63 2d 3e 70 49 6e 64 65 78 20 29 7b 0a  pSrc->pIndex ){.
2b80a 20 20 20 20 70 50 72 6f 62 65 20 3d 20 70 53 72      pProbe = pSr
2b80b 63 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 7d 0a 20  c->pIndex;.  }. 
2b80c 20 66 6f 72 28 3b 20 70 50 72 6f 62 65 3b 20 70   for(; pProbe; p
2b80d 50 72 6f 62 65 3d 28 70 53 72 63 2d 3e 70 49 6e  Probe=(pSrc->pIn
2b80e 64 65 78 20 3f 20 30 20 3a 20 70 50 72 6f 62 65  dex ? 0 : pProbe
2b80f 2d 3e 70 4e 65 78 74 29 29 7b 0a 20 20 20 20 64  ->pNext)){.    d
2b810 6f 75 62 6c 65 20 69 6e 4d 75 6c 74 69 70 6c 69  ouble inMultipli
2b811 65 72 20 3d 20 31 3b 20 20 2f 2a 20 4e 75 6d 62  er = 1;  /* Numb
2b812 65 72 20 6f 66 20 65 71 75 61 6c 69 74 79 20 6c  er of equality l
2b813 6f 6f 6b 2d 75 70 73 20 6e 65 65 64 65 64 20 2a  ook-ups needed *
2b814 2f 0a 20 20 20 20 69 6e 74 20 69 6e 4d 75 6c 74  /.    int inMult
2b815 49 73 45 73 74 20 3d 20 30 3b 20 20 20 20 20 20  IsEst = 0;      
2b816 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 4d 75 6c  /* True if inMul
2b817 74 69 70 6c 69 65 72 20 69 73 20 61 6e 20 65 73  tiplier is an es
2b818 74 69 6d 61 74 65 20 2a 2f 0a 0a 20 20 20 20 57  timate */..    W
2b819 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
2b81a 69 6e 64 65 78 20 25 73 3a 5c 6e 22 2c 20 70 50  index %s:\n", pP
2b81b 72 6f 62 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a  robe->zName));..
2b81c 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65      /* Count the
2b81d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
2b81e 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20  ns in the index 
2b81f 74 68 61 74 20 61 72 65 20 73 61 74 69 73 66 69  that are satisfi
2b820 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 78 3d 45  ed.    ** by x=E
2b821 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  XPR constraints 
2b822 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 63 6f  or x IN (...) co
2b823 6e 73 74 72 61 69 6e 74 73 2e 20 20 46 6f 72 20  nstraints.  For 
2b824 61 20 74 65 72 6d 0a 20 20 20 20 2a 2a 20 6f 66  a term.    ** of
2b825 20 74 68 65 20 66 6f 72 6d 20 78 3d 45 58 50 52   the form x=EXPR
2b826 20 77 65 20 6f 6e 6c 79 20 68 61 76 65 20 74 6f   we only have to
2b827 20 64 6f 20 61 20 73 69 6e 67 6c 65 20 62 69 6e   do a single bin
2b828 61 72 79 20 73 65 61 72 63 68 2e 0a 20 20 20 20  ary search..    
2b829 2a 2a 20 42 75 74 20 66 6f 72 20 78 20 49 4e 20  ** But for x IN 
2b82a 28 2e 2e 2e 29 20 77 65 20 68 61 76 65 20 74 6f  (...) we have to
2b82b 20 64 6f 20 61 20 6e 75 6d 62 65 72 20 6f 66 20   do a number of 
2b82c 62 69 6e 61 72 79 20 73 65 61 72 63 68 65 64 0a  binary searched.
2b82d 20 20 20 20 2a 2a 20 65 71 75 61 6c 20 74 6f 20      ** equal to 
2b82e 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  the number of en
2b82f 74 72 69 65 73 20 6f 6e 20 74 68 65 20 52 48 53  tries on the RHS
2b830 20 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61   of the IN opera
2b831 74 6f 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  tor..    ** The 
2b832 69 6e 4d 75 6c 74 69 70 6c 65 72 20 76 61 72 69  inMultipler vari
2b833 61 62 6c 65 20 77 69 74 68 20 74 72 79 20 74 6f  able with try to
2b834 20 65 73 74 69 6d 61 74 65 20 74 68 65 20 6e 75   estimate the nu
2b835 6d 62 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 62  mber of.    ** b
2b836 69 6e 61 72 79 20 73 65 61 72 63 68 65 73 20 6e  inary searches n
2b837 65 65 64 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  eeded..    */.  
2b838 20 20 77 73 46 6c 61 67 73 20 3d 20 30 3b 0a 20    wsFlags = 0;. 
2b839 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
2b83a 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  robe->nColumn; i
2b83b 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a  ++){.      int j
2b83c 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c   = pProbe->aiCol
2b83d 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 70 54  umn[i];.      pT
2b83e 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
2b83f 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74  WC, iCur, j, not
2b840 52 65 61 64 79 2c 20 65 71 54 65 72 6d 4d 61 73  Ready, eqTermMas
2b841 6b 2c 20 70 50 72 6f 62 65 29 3b 0a 20 20 20 20  k, pProbe);.    
2b842 20 20 69 66 28 20 70 54 65 72 6d 3d 3d 30 20 29    if( pTerm==0 )
2b843 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 77 73   break;.      ws
2b844 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43  Flags |= WHERE_C
2b845 4f 4c 55 4d 4e 5f 45 51 3b 0a 20 20 20 20 20 20  OLUMN_EQ;.      
2b846 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
2b847 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a  ator & WO_IN ){.
2b848 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
2b849 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  xpr = pTerm->pEx
2b84a 70 72 3b 0a 20 20 20 20 20 20 20 20 77 73 46 6c  pr;.        wsFl
2b84b 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c  ags |= WHERE_COL
2b84c 55 4d 4e 5f 49 4e 3b 0a 20 20 20 20 20 20 20 20  UMN_IN;.        
2b84d 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
2b84e 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
2b84f 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
2b850 20 20 20 20 20 20 69 6e 4d 75 6c 74 69 70 6c 69        inMultipli
2b851 65 72 20 2a 3d 20 32 35 3b 0a 20 20 20 20 20 20  er *= 25;.      
2b852 20 20 20 20 69 6e 4d 75 6c 74 49 73 45 73 74 20      inMultIsEst 
2b853 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 1;.        }el
2b854 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e  se if( pExpr->x.
2b855 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20  pList ){.       
2b856 20 20 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20     inMultiplier 
2b857 2a 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  *= pExpr->x.pLis
2b858 74 2d 3e 6e 45 78 70 72 20 2b 20 31 3b 0a 20 20  t->nExpr + 1;.  
2b859 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2b85a 20 20 20 20 7d 0a 20 20 20 20 6e 52 6f 77 20 3d      }.    nRow =
2b85b 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73   pProbe->aiRowEs
2b85c 74 5b 69 5d 20 2a 20 69 6e 4d 75 6c 74 69 70 6c  t[i] * inMultipl
2b85d 69 65 72 3b 0a 20 20 20 20 2f 2a 20 49 66 20 69  ier;.    /* If i
2b85e 6e 4d 75 6c 74 69 70 6c 69 65 72 20 69 73 20 61  nMultiplier is a
2b85f 6e 20 65 73 74 69 6d 61 74 65 20 61 6e 64 20 74  n estimate and t
2b860 68 61 74 20 65 73 74 69 6d 61 74 65 20 72 65 73  hat estimate res
2b861 75 6c 74 73 20 69 6e 20 61 6e 0a 20 20 20 20 2a  ults in an.    *
2b862 2a 20 6e 52 6f 77 20 69 74 20 74 68 61 74 20 69  * nRow it that i
2b863 73 20 6d 6f 72 65 20 74 68 61 6e 20 68 61 6c 66  s more than half
2b864 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
2b865 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 0a 20 20  in the table,.  
2b866 20 20 2a 2a 20 74 68 65 6e 20 72 65 64 75 63 65    ** then reduce
2b867 20 69 6e 4d 75 6c 74 69 70 6c 65 72 20 2a 2f 0a   inMultipler */.
2b868 20 20 20 20 69 66 28 20 69 6e 4d 75 6c 74 49 73      if( inMultIs
2b869 45 73 74 20 26 26 20 6e 52 6f 77 2a 32 20 3e 20  Est && nRow*2 > 
2b86a 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73 74  pProbe->aiRowEst
2b86b 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 6e 52 6f  [0] ){.      nRo
2b86c 77 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f  w = pProbe->aiRo
2b86d 77 45 73 74 5b 30 5d 2f 32 3b 0a 20 20 20 20 20  wEst[0]/2;.     
2b86e 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 3d 20   inMultiplier = 
2b86f 6e 52 6f 77 2f 70 50 72 6f 62 65 2d 3e 61 69 52  nRow/pProbe->aiR
2b870 6f 77 45 73 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a  owEst[i];.    }.
2b871 20 20 20 20 63 6f 73 74 20 3d 20 6e 52 6f 77 20      cost = nRow 
2b872 2b 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 2a 65  + inMultiplier*e
2b873 73 74 4c 6f 67 28 70 50 72 6f 62 65 2d 3e 61 69  stLog(pProbe->ai
2b874 52 6f 77 45 73 74 5b 30 5d 29 3b 0a 20 20 20 20  RowEst[0]);.    
2b875 6e 45 71 20 3d 20 69 3b 0a 20 20 20 20 69 66 28  nEq = i;.    if(
2b876 20 70 50 72 6f 62 65 2d 3e 6f 6e 45 72 72 6f 72   pProbe->onError
2b877 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 28 77 73  !=OE_None && (ws
2b878 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43 4f  Flags & WHERE_CO
2b879 4c 55 4d 4e 5f 49 4e 29 3d 3d 30 0a 20 20 20 20  LUMN_IN)==0.    
2b87a 20 20 20 20 20 26 26 20 6e 45 71 3d 3d 70 50 72       && nEq==pPr
2b87b 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  obe->nColumn ){.
2b87c 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2b87d 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b 0a 20   WHERE_UNIQUE;. 
2b87e 20 20 20 7d 0a 20 20 20 20 57 48 45 52 45 54 52     }.    WHERETR
2b87f 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 6e 45 71  ACE(("...... nEq
2b880 3d 25 64 20 69 6e 4d 75 6c 74 3d 25 2e 39 67 20  =%d inMult=%.9g 
2b881 6e 52 6f 77 3d 25 2e 39 67 20 63 6f 73 74 3d 25  nRow=%.9g cost=%
2b882 2e 39 67 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  .9g\n",.        
2b883 20 20 20 20 20 20 20 20 6e 45 71 2c 20 69 6e 4d          nEq, inM
2b884 75 6c 74 69 70 6c 69 65 72 2c 20 6e 52 6f 77 2c  ultiplier, nRow,
2b885 20 63 6f 73 74 29 29 3b 0a 0a 20 20 20 20 2f 2a   cost));..    /*
2b886 20 4c 6f 6f 6b 20 66 6f 72 20 72 61 6e 67 65 20   Look for range 
2b887 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 41 73  constraints.  As
2b888 73 75 6d 65 20 74 68 61 74 20 65 61 63 68 20 72  sume that each r
2b889 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a  ange constraint.
2b88a 20 20 20 20 2a 2a 20 6d 61 6b 65 73 20 74 68 65      ** makes the
2b88b 20 73 65 61 72 63 68 20 73 70 61 63 65 20 31 2f   search space 1/
2b88c 33 72 64 20 73 6d 61 6c 6c 65 72 2e 0a 20 20 20  3rd smaller..   
2b88d 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 45 71 3c   */.    if( nEq<
2b88e 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20  pProbe->nColumn 
2b88f 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d  ){.      int j =
2b890 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d   pProbe->aiColum
2b891 6e 5b 6e 45 71 5d 3b 0a 20 20 20 20 20 20 70 54  n[nEq];.      pT
2b892 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
2b893 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74  WC, iCur, j, not
2b894 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f  Ready, WO_LT|WO_
2b895 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20  LE|WO_GT|WO_GE, 
2b896 70 50 72 6f 62 65 29 3b 0a 20 20 20 20 20 20 69  pProbe);.      i
2b897 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20 20 20  f( pTerm ){.    
2b898 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2b899 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47  HERE_COLUMN_RANG
2b89a 45 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  E;.        if( f
2b89b 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2b89c 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, j, notReady, 
2b89d 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 70 50 72  WO_LT|WO_LE, pPr
2b89e 6f 62 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  obe) ){.        
2b89f 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2b8a0 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20  RE_TOP_LIMIT;.  
2b8a1 20 20 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20          cost /= 
2b8a2 33 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 52 6f  3;.          nRo
2b8a3 77 20 2f 3d 20 33 3b 0a 20 20 20 20 20 20 20 20  w /= 3;.        
2b8a4 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 69  }.        if( fi
2b8a5 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2b8a6 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57  , j, notReady, W
2b8a7 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 70 50 72 6f  O_GT|WO_GE, pPro
2b8a8 62 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  be) ){.         
2b8a9 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2b8aa 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20  E_BTM_LIMIT;.   
2b8ab 20 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20 33         cost /= 3
2b8ac 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 52 6f 77  ;.          nRow
2b8ad 20 2f 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 7d   /= 3;.        }
2b8ae 0a 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52  .        WHERETR
2b8af 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 72 61 6e  ACE(("...... ran
2b8b0 67 65 20 72 65 64 75 63 65 73 20 6e 52 6f 77 20  ge reduces nRow 
2b8b1 74 6f 20 25 2e 39 67 20 61 6e 64 20 63 6f 73 74  to %.9g and cost
2b8b2 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 0a 20 20 20   to %.9g\n",.   
2b8b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8b4 20 6e 52 6f 77 2c 20 63 6f 73 74 29 29 3b 0a 20   nRow, cost));. 
2b8b5 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2b8b6 20 20 2f 2a 20 41 64 64 20 74 68 65 20 61 64 64    /* Add the add
2b8b7 69 74 69 6f 6e 61 6c 20 63 6f 73 74 20 6f 66 20  itional cost of 
2b8b8 73 6f 72 74 69 6e 67 20 69 66 20 74 68 61 74 20  sorting if that 
2b8b9 69 73 20 61 20 66 61 63 74 6f 72 2e 0a 20 20 20  is a factor..   
2b8ba 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72 64   */.    if( pOrd
2b8bb 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69 66  erBy ){.      if
2b8bc 28 20 28 77 73 46 6c 61 67 73 20 26 20 57 48 45  ( (wsFlags & WHE
2b8bd 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29 3d 3d 30  RE_COLUMN_IN)==0
2b8be 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 69   &&.           i
2b8bf 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 70 50  sSortingIndex(pP
2b8c0 61 72 73 65 2c 70 57 43 2d 3e 70 4d 61 73 6b 53  arse,pWC->pMaskS
2b8c1 65 74 2c 70 50 72 6f 62 65 2c 69 43 75 72 2c 70  et,pProbe,iCur,p
2b8c2 4f 72 64 65 72 42 79 2c 6e 45 71 2c 26 72 65 76  OrderBy,nEq,&rev
2b8c3 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ) ){.        if(
2b8c4 20 77 73 46 6c 61 67 73 3d 3d 30 20 29 7b 0a 20   wsFlags==0 ){. 
2b8c5 20 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73           wsFlags
2b8c6 20 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f   = WHERE_COLUMN_
2b8c7 52 41 4e 47 45 3b 0a 20 20 20 20 20 20 20 20 7d  RANGE;.        }
2b8c8 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73  .        wsFlags
2b8c9 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44 45 52 42   |= WHERE_ORDERB
2b8ca 59 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  Y;.        if( r
2b8cb 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ev ){.          
2b8cc 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45  wsFlags |= WHERE
2b8cd 5f 52 45 56 45 52 53 45 3b 0a 20 20 20 20 20 20  _REVERSE;.      
2b8ce 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
2b8cf 0a 20 20 20 20 20 20 20 20 63 6f 73 74 20 2b 3d  .        cost +=
2b8d0 20 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f 73   cost*estLog(cos
2b8d1 74 29 3b 0a 20 20 20 20 20 20 20 20 57 48 45 52  t);.        WHER
2b8d2 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20  ETRACE(("...... 
2b8d3 6f 72 64 65 72 62 79 20 69 6e 63 72 65 61 73 65  orderby increase
2b8d4 73 20 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e  s cost to %.9g\n
2b8d5 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20 20 20  ", cost));.     
2b8d6 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
2b8d7 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
2b8d8 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 76 65  gs & SQLITE_Reve
2b8d9 72 73 65 4f 72 64 65 72 20 29 7b 0a 20 20 20 20  rseOrder ){.    
2b8da 20 20 2f 2a 20 46 6f 72 20 61 70 70 6c 69 63 61    /* For applica
2b8db 74 69 6f 6e 20 74 65 73 74 69 6e 67 2c 20 72 61  tion testing, ra
2b8dc 6e 64 6f 6d 6c 79 20 72 65 76 65 72 73 65 20 74  ndomly reverse t
2b8dd 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72 20  he output order 
2b8de 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c  for.      ** SEL
2b8df 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74  ECT statements t
2b8e0 68 61 74 20 6f 6d 69 74 20 74 68 65 20 4f 52 44  hat omit the ORD
2b8e1 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 54  ER BY clause.  T
2b8e2 68 69 73 20 77 69 6c 6c 20 68 65 6c 70 0a 20 20  his will help.  
2b8e3 20 20 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20 63      ** to find c
2b8e4 61 73 65 73 20 77 68 65 72 65 0a 20 20 20 20 20  ases where.     
2b8e5 20 2a 2f 0a 20 20 20 20 20 20 77 73 46 6c 61 67   */.      wsFlag
2b8e6 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56 45 52  s |= WHERE_REVER
2b8e7 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  SE;.    }..    /
2b8e8 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
2b8e9 66 20 77 65 20 63 61 6e 20 67 65 74 20 61 77 61  f we can get awa
2b8ea 79 20 77 69 74 68 20 75 73 69 6e 67 20 6a 75 73  y with using jus
2b8eb 74 20 74 68 65 20 69 6e 64 65 78 20 77 69 74 68  t the index with
2b8ec 6f 75 74 0a 20 20 20 20 2a 2a 20 65 76 65 72 20  out.    ** ever 
2b8ed 72 65 61 64 69 6e 67 20 74 68 65 20 74 61 62 6c  reading the tabl
2b8ee 65 2e 20 20 49 66 20 74 68 61 74 20 69 73 20 74  e.  If that is t
2b8ef 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 68 61  he case, then ha
2b8f0 6c 76 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 63  lve the.    ** c
2b8f1 6f 73 74 20 6f 66 20 74 68 69 73 20 69 6e 64 65  ost of this inde
2b8f2 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  x..    */.    if
2b8f3 28 20 77 73 46 6c 61 67 73 20 26 26 20 70 53 72  ( wsFlags && pSr
2b8f4 63 2d 3e 63 6f 6c 55 73 65 64 20 3c 20 28 28 28  c->colUsed < (((
2b8f5 42 69 74 6d 61 73 6b 29 31 29 3c 3c 28 42 4d 53  Bitmask)1)<<(BMS
2b8f6 2d 31 29 29 20 29 7b 0a 20 20 20 20 20 20 42 69  -1)) ){.      Bi
2b8f7 74 6d 61 73 6b 20 6d 20 3d 20 70 53 72 63 2d 3e  tmask m = pSrc->
2b8f8 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20 20 69  colUsed;.      i
2b8f9 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt j;.      for(
2b8fa 6a 3d 30 3b 20 6a 3c 70 50 72 6f 62 65 2d 3e 6e  j=0; j<pProbe->n
2b8fb 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20  Column; j++){.  
2b8fc 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 70 50        int x = pP
2b8fd 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a  robe->aiColumn[j
2b8fe 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78  ];.        if( x
2b8ff 3c 42 4d 53 2d 31 20 29 7b 0a 20 20 20 20 20 20  <BMS-1 ){.      
2b900 20 20 20 20 6d 20 26 3d 20 7e 28 28 28 42 69 74      m &= ~(((Bit
2b901 6d 61 73 6b 29 31 29 3c 3c 78 29 3b 0a 20 20 20  mask)1)<<x);.   
2b902 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2b903 20 20 20 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b       if( m==0 ){
2b904 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73  .        wsFlags
2b905 20 7c 3d 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e   |= WHERE_IDX_ON
2b906 4c 59 3b 0a 20 20 20 20 20 20 20 20 63 6f 73 74  LY;.        cost
2b907 20 2f 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 57   /= 2;.        W
2b908 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e  HERETRACE(("....
2b909 2e 2e 20 69 64 78 2d 6f 6e 6c 79 20 72 65 64 75  .. idx-only redu
2b90a 63 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39 67  ces cost to %.9g
2b90b 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20  \n", cost));.   
2b90c 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2b90d 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 64 65 78  /* If this index
2b90e 20 68 61 73 20 61 63 68 69 65 76 65 64 20 74 68   has achieved th
2b90f 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 73 6f  e lowest cost so
2b910 20 66 61 72 2c 20 74 68 65 6e 20 75 73 65 20 69   far, then use i
2b911 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  t..    */.    if
2b912 28 20 77 73 46 6c 61 67 73 21 3d 30 20 26 26 20  ( wsFlags!=0 && 
2b913 63 6f 73 74 20 3c 20 70 43 6f 73 74 2d 3e 72 43  cost < pCost->rC
2b914 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 70 43 6f  ost ){.      pCo
2b915 73 74 2d 3e 72 43 6f 73 74 20 3d 20 63 6f 73 74  st->rCost = cost
2b916 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 6e  ;.      pCost->n
2b917 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20 20  Row = nRow;.    
2b918 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
2b919 46 6c 61 67 73 20 3d 20 77 73 46 6c 61 67 73 3b  Flags = wsFlags;
2b91a 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c  .      pCost->pl
2b91b 61 6e 2e 6e 45 71 20 3d 20 6e 45 71 3b 0a 20 20  an.nEq = nEq;.  
2b91c 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 73      assert( pCos
2b91d 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  t->plan.wsFlags 
2b91e 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20  & WHERE_INDEXED 
2b91f 29 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  );.      pCost->
2b920 70 6c 61 6e 2e 75 2e 70 49 64 78 20 3d 20 70 50  plan.u.pIdx = pP
2b921 72 6f 62 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  robe;.    }.  }.
2b922 0a 20 20 2f 2a 20 52 65 70 6f 72 74 20 74 68 65  .  /* Report the
2b923 20 62 65 73 74 20 72 65 73 75 6c 74 0a 20 20 2a   best result.  *
2b924 2f 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e  /.  pCost->plan.
2b925 77 73 46 6c 61 67 73 20 7c 3d 20 65 71 54 65 72  wsFlags |= eqTer
2b926 6d 4d 61 73 6b 3b 0a 20 20 57 48 45 52 45 54 52  mMask;.  WHERETR
2b927 41 43 45 28 28 22 62 65 73 74 20 69 6e 64 65 78  ACE(("best index
2b928 20 69 73 20 25 73 2c 20 63 6f 73 74 3d 25 2e 39   is %s, cost=%.9
2b929 67 2c 20 6e 72 6f 77 3d 25 2e 39 67 2c 20 77 73  g, nrow=%.9g, ws
2b92a 46 6c 61 67 73 3d 25 78 2c 20 6e 45 71 3d 25 64  Flags=%x, nEq=%d
2b92b 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 28 70 43  \n",.        (pC
2b92c 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  ost->plan.wsFlag
2b92d 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
2b92e 44 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20 20  D)!=0 ?.        
2b92f 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e       pCost->plan
2b930 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 3a  .u.pIdx->zName :
2b931 20 22 28 6e 6f 6e 65 29 22 2c 20 70 43 6f 73 74   "(none)", pCost
2b932 2d 3e 6e 52 6f 77 2c 0a 20 20 20 20 20 20 20 20  ->nRow,.        
2b933 70 43 6f 73 74 2d 3e 72 43 6f 73 74 2c 20 70 43  pCost->rCost, pC
2b934 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  ost->plan.wsFlag
2b935 73 2c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e  s, pCost->plan.n
2b936 45 71 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  Eq));.}.../*.** 
2b937 44 69 73 61 62 6c 65 20 61 20 74 65 72 6d 20 69  Disable a term i
2b938 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
2b939 73 65 2e 20 20 45 78 63 65 70 74 2c 20 64 6f 20  se.  Except, do 
2b93a 6e 6f 74 20 64 69 73 61 62 6c 65 20 74 68 65 20  not disable the 
2b93b 74 65 72 6d 0a 2a 2a 20 69 66 20 69 74 20 63 6f  term.** if it co
2b93c 6e 74 72 6f 6c 73 20 61 20 4c 45 46 54 20 4f 55  ntrols a LEFT OU
2b93d 54 45 52 20 4a 4f 49 4e 20 61 6e 64 20 69 74 20  TER JOIN and it 
2b93e 64 69 64 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74  did not originat
2b93f 65 20 69 6e 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f  e in the ON.** o
2b940 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f  r USING clause o
2b941 66 20 74 68 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a  f that join..**.
2b942 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20  ** Consider the 
2b943 74 65 72 6d 20 74 32 2e 7a 3d 27 6f 6b 27 20 69  term t2.z='ok' i
2b944 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
2b945 71 75 65 72 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  queries:.**.**  
2b946 20 28 31 29 20 20 53 45 4c 45 43 54 20 2a 20 46   (1)  SELECT * F
2b947 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e  ROM t1 LEFT JOIN
2b948 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78   t2 ON t1.a=t2.x
2b949 20 57 48 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27   WHERE t2.z='ok'
2b94a 0a 2a 2a 20 20 20 28 32 29 20 20 53 45 4c 45 43  .**   (2)  SELEC
2b94b 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54  T * FROM t1 LEFT
2b94c 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61   JOIN t2 ON t1.a
2b94d 3d 74 32 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27  =t2.x AND t2.z='
2b94e 6f 6b 27 0a 2a 2a 20 20 20 28 33 29 20 20 53 45  ok'.**   (3)  SE
2b94f 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20  LECT * FROM t1, 
2b950 74 32 20 57 48 45 52 45 20 74 31 2e 61 3d 74 32  t2 WHERE t1.a=t2
2b951 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27  .x AND t2.z='ok'
2b952 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 32 2e 7a 3d  .**.** The t2.z=
2b953 27 6f 6b 27 20 69 73 20 64 69 73 61 62 6c 65 64  'ok' is disabled
2b954 20 69 6e 20 74 68 65 20 69 6e 20 28 32 29 20 62   in the in (2) b
2b955 65 63 61 75 73 65 20 69 74 20 6f 72 69 67 69 6e  ecause it origin
2b956 61 74 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 4f  ates.** in the O
2b957 4e 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20 74  N clause.  The t
2b958 65 72 6d 20 69 73 20 64 69 73 61 62 6c 65 64 20  erm is disabled 
2b959 69 6e 20 28 33 29 20 62 65 63 61 75 73 65 20 69  in (3) because i
2b95a 74 20 69 73 20 6e 6f 74 20 70 61 72 74 0a 2a 2a  t is not part.**
2b95b 20 6f 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52   of a LEFT OUTER
2b95c 20 4a 4f 49 4e 2e 20 20 49 6e 20 28 31 29 2c 20   JOIN.  In (1), 
2b95d 74 68 65 20 74 65 72 6d 20 69 73 20 6e 6f 74 20  the term is not 
2b95e 64 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  disabled..**.** 
2b95f 44 69 73 61 62 6c 69 6e 67 20 61 20 74 65 72 6d  Disabling a term
2b960 20 63 61 75 73 65 73 20 74 68 61 74 20 74 65 72   causes that ter
2b961 6d 20 74 6f 20 6e 6f 74 20 62 65 20 74 65 73 74  m to not be test
2b962 65 64 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20  ed in the inner 
2b963 6c 6f 6f 70 0a 2a 2a 20 6f 66 20 74 68 65 20 6a  loop.** of the j
2b964 6f 69 6e 2e 20 20 44 69 73 61 62 6c 69 6e 67 20  oin.  Disabling 
2b965 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  is an optimizati
2b966 6f 6e 2e 20 20 57 68 65 6e 20 74 65 72 6d 73 20  on.  When terms 
2b967 61 72 65 20 73 61 74 69 73 66 69 65 64 0a 2a 2a  are satisfied.**
2b968 20 62 79 20 69 6e 64 69 63 65 73 2c 20 77 65 20   by indices, we 
2b969 64 69 73 61 62 6c 65 20 74 68 65 6d 20 74 6f 20  disable them to 
2b96a 70 72 65 76 65 6e 74 20 72 65 64 75 6e 64 61 6e  prevent redundan
2b96b 74 20 74 65 73 74 73 20 69 6e 20 74 68 65 20 69  t tests in the i
2b96c 6e 6e 65 72 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57  nner.** loop.  W
2b96d 65 20 77 6f 75 6c 64 20 67 65 74 20 74 68 65 20  e would get the 
2b96e 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74 73 20  correct results 
2b96f 69 66 20 6e 6f 74 68 69 6e 67 20 77 65 72 65 20  if nothing were 
2b970 65 76 65 72 20 64 69 73 61 62 6c 65 64 2c 0a 2a  ever disabled,.*
2b971 2a 20 62 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68  * but joins migh
2b972 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73  t run a little s
2b973 6c 6f 77 65 72 2e 20 20 54 68 65 20 74 72 69 63  lower.  The tric
2b974 6b 20 69 73 20 74 6f 20 64 69 73 61 62 6c 65 20  k is to disable 
2b975 61 73 20 6d 75 63 68 0a 2a 2a 20 61 73 20 77 65  as much.** as we
2b976 20 63 61 6e 20 77 69 74 68 6f 75 74 20 64 69 73   can without dis
2b977 61 62 6c 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e  abling too much.
2b978 20 20 49 66 20 77 65 20 64 69 73 61 62 6c 65 64    If we disabled
2b979 20 69 6e 20 28 31 29 2c 20 77 65 27 64 20 67 65   in (1), we'd ge
2b97a 74 0a 2a 2a 20 74 68 65 20 77 72 6f 6e 67 20 61  t.** the wrong a
2b97b 6e 73 77 65 72 2e 20 20 53 65 65 20 74 69 63 6b  nswer.  See tick
2b97c 65 74 20 23 38 31 33 2e 0a 2a 2f 0a 73 74 61 74  et #813..*/.stat
2b97d 69 63 20 76 6f 69 64 20 64 69 73 61 62 6c 65 54  ic void disableT
2b97e 65 72 6d 28 57 68 65 72 65 4c 65 76 65 6c 20 2a  erm(WhereLevel *
2b97f 70 4c 65 76 65 6c 2c 20 57 68 65 72 65 54 65 72  pLevel, WhereTer
2b980 6d 20 2a 70 54 65 72 6d 29 7b 0a 20 20 69 66 28  m *pTerm){.  if(
2b981 20 70 54 65 72 6d 0a 20 20 20 20 20 20 26 26 20   pTerm.      && 
2b982 41 4c 57 41 59 53 28 28 70 54 65 72 6d 2d 3e 77  ALWAYS((pTerm->w
2b983 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f  tFlags & TERM_CO
2b984 44 45 44 29 3d 3d 30 29 0a 20 20 20 20 20 20 26  DED)==0).      &
2b985 26 20 28 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  & (pLevel->iLeft
2b986 4a 6f 69 6e 3d 3d 30 20 7c 7c 20 45 78 70 72 48  Join==0 || ExprH
2b987 61 73 50 72 6f 70 65 72 74 79 28 70 54 65 72 6d  asProperty(pTerm
2b988 2d 3e 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d  ->pExpr, EP_From
2b989 4a 6f 69 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20  Join)).  ){.    
2b98a 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c  pTerm->wtFlags |
2b98b 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20  = TERM_CODED;.  
2b98c 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 69 50 61    if( pTerm->iPa
2b98d 72 65 6e 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20  rent>=0 ){.     
2b98e 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 74 68   WhereTerm *pOth
2b98f 65 72 20 3d 20 26 70 54 65 72 6d 2d 3e 70 57 43  er = &pTerm->pWC
2b990 2d 3e 61 5b 70 54 65 72 6d 2d 3e 69 50 61 72 65  ->a[pTerm->iPare
2b991 6e 74 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 28  nt];.      if( (
2b992 2d 2d 70 4f 74 68 65 72 2d 3e 6e 43 68 69 6c 64  --pOther->nChild
2b993 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
2b994 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
2b995 65 6c 2c 20 70 4f 74 68 65 72 29 3b 0a 20 20 20  el, pOther);.   
2b996 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d     }.    }.  }.}
2b997 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 6c 79 20 74 68  ../*.** Apply th
2b998 65 20 61 66 66 69 6e 69 74 69 65 73 20 61 73 73  e affinities ass
2b999 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
2b99a 20 66 69 72 73 74 20 6e 20 63 6f 6c 75 6d 6e 73   first n columns
2b99b 20 6f 66 20 69 6e 64 65 78 0a 2a 2a 20 70 49 64   of index.** pId
2b99c 78 20 74 6f 20 74 68 65 20 76 61 6c 75 65 73 20  x to the values 
2b99d 69 6e 20 74 68 65 20 6e 20 72 65 67 69 73 74 65  in the n registe
2b99e 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 62  rs starting at b
2b99f 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ase..*/.static v
2b9a0 6f 69 64 20 63 6f 64 65 41 70 70 6c 79 41 66 66  oid codeApplyAff
2b9a1 69 6e 69 74 79 28 50 61 72 73 65 20 2a 70 50 61  inity(Parse *pPa
2b9a2 72 73 65 2c 20 69 6e 74 20 62 61 73 65 2c 20 69  rse, int base, i
2b9a3 6e 74 20 6e 2c 20 49 6e 64 65 78 20 2a 70 49 64  nt n, Index *pId
2b9a4 78 29 7b 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b  x){.  if( n>0 ){
2b9a5 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70  .    Vdbe *v = p
2b9a6 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2b9a7 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
2b9a8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2b9a9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66  eAddOp2(v, OP_Af
2b9aa 66 69 6e 69 74 79 2c 20 62 61 73 65 2c 20 6e 29  finity, base, n)
2b9ab 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6e 64  ;.    sqlite3Ind
2b9ac 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  exAffinityStr(v,
2b9ad 20 70 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69   pIdx);.    sqli
2b9ae 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
2b9af 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
2b9b0 65 2c 20 62 61 73 65 2c 20 6e 29 3b 0a 20 20 7d  e, base, n);.  }
2b9b1 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  .}.../*.** Gener
2b9b2 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73  ate code for a s
2b9b3 69 6e 67 6c 65 20 65 71 75 61 6c 69 74 79 20 74  ingle equality t
2b9b4 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45  erm of the WHERE
2b9b5 20 63 6c 61 75 73 65 2e 20 20 41 6e 20 65 71 75   clause.  An equ
2b9b6 61 6c 69 74 79 0a 2a 2a 20 74 65 72 6d 20 63 61  ality.** term ca
2b9b7 6e 20 62 65 20 65 69 74 68 65 72 20 58 3d 65 78  n be either X=ex
2b9b8 70 72 20 6f 72 20 58 20 49 4e 20 28 2e 2e 2e 29  pr or X IN (...)
2b9b9 2e 20 20 20 70 54 65 72 6d 20 69 73 20 74 68 65  .   pTerm is the
2b9ba 20 74 65 72 6d 20 74 6f 20 62 65 20 0a 2a 2a 20   term to be .** 
2b9bb 63 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  coded..**.** The
2b9bc 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 66   current value f
2b9bd 6f 72 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  or the constrain
2b9be 74 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67  t is left in reg
2b9bf 69 73 74 65 72 20 69 52 65 67 2e 0a 2a 2a 0a 2a  ister iReg..**.*
2b9c0 2a 20 46 6f 72 20 61 20 63 6f 6e 73 74 72 61 69  * For a constrai
2b9c1 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58  nt of the form X
2b9c2 3d 65 78 70 72 2c 20 74 68 65 20 65 78 70 72 65  =expr, the expre
2b9c3 73 73 69 6f 6e 20 69 73 20 65 76 61 6c 75 61 74  ssion is evaluat
2b9c4 65 64 20 61 6e 64 20 69 74 73 0a 2a 2a 20 72 65  ed and its.** re
2b9c5 73 75 6c 74 20 69 73 20 6c 65 66 74 20 6f 6e 20  sult is left on 
2b9c6 74 68 65 20 73 74 61 63 6b 2e 20 20 46 6f 72 20  the stack.  For 
2b9c7 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74  constraints of t
2b9c8 68 65 20 66 6f 72 6d 20 58 20 49 4e 20 28 2e 2e  he form X IN (..
2b9c9 2e 29 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  .).** this routi
2b9ca 6e 65 20 73 65 74 73 20 75 70 20 61 20 6c 6f 6f  ne sets up a loo
2b9cb 70 20 74 68 61 74 20 77 69 6c 6c 20 69 74 65 72  p that will iter
2b9cc 61 74 65 20 6f 76 65 72 20 61 6c 6c 20 76 61 6c  ate over all val
2b9cd 75 65 73 20 6f 66 20 58 2e 0a 2a 2f 0a 73 74 61  ues of X..*/.sta
2b9ce 74 69 63 20 69 6e 74 20 63 6f 64 65 45 71 75 61  tic int codeEqua
2b9cf 6c 69 74 79 54 65 72 6d 28 0a 20 20 50 61 72 73  lityTerm(.  Pars
2b9d0 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2b9d1 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
2b9d2 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72  ontext */.  Wher
2b9d3 65 54 65 72 6d 20 2a 70 54 65 72 6d 2c 20 20 20  eTerm *pTerm,   
2b9d4 2f 2a 20 54 68 65 20 74 65 72 6d 20 6f 66 20 74  /* The term of t
2b9d5 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2b9d6 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20  to be coded */. 
2b9d7 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
2b9d8 76 65 6c 2c 20 2f 2a 20 57 68 65 6e 20 6c 65 76  vel, /* When lev
2b9d9 65 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  el of the FROM c
2b9da 6c 61 75 73 65 20 77 65 20 61 72 65 20 77 6f 72  lause we are wor
2b9db 6b 69 6e 67 20 6f 6e 20 2a 2f 0a 20 20 69 6e 74  king on */.  int
2b9dc 20 69 54 61 72 67 65 74 20 20 20 20 20 20 20 20   iTarget        
2b9dd 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c   /* Attempt to l
2b9de 65 61 76 65 20 72 65 73 75 6c 74 73 20 69 6e 20  eave results in 
2b9df 74 68 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f  this register */
2b9e0 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 58 20 3d  .){.  Expr *pX =
2b9e1 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20   pTerm->pExpr;. 
2b9e2 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2b9e3 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
2b9e4 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20  iReg;           
2b9e5 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
2b9e6 65 72 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c  er holding resul
2b9e7 74 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ts */..  assert(
2b9e8 20 69 54 61 72 67 65 74 3e 30 20 29 3b 0a 20 20   iTarget>0 );.  
2b9e9 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 45  if( pX->op==TK_E
2b9ea 51 20 29 7b 0a 20 20 20 20 69 52 65 67 20 3d 20  Q ){.    iReg = 
2b9eb 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
2b9ec 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 58  arget(pParse, pX
2b9ed 2d 3e 70 52 69 67 68 74 2c 20 69 54 61 72 67 65  ->pRight, iTarge
2b9ee 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  t);.  }else if( 
2b9ef 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c  pX->op==TK_ISNUL
2b9f0 4c 20 29 7b 0a 20 20 20 20 69 52 65 67 20 3d 20  L ){.    iReg = 
2b9f1 69 54 61 72 67 65 74 3b 0a 20 20 20 20 73 71 6c  iTarget;.    sql
2b9f2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b9f3 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52  , OP_Null, 0, iR
2b9f4 65 67 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  eg);.#ifndef SQL
2b9f5 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2b9f6 59 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  Y.  }else{.    i
2b9f7 6e 74 20 65 54 79 70 65 3b 0a 20 20 20 20 69 6e  nt eType;.    in
2b9f8 74 20 69 54 61 62 3b 0a 20 20 20 20 73 74 72 75  t iTab;.    stru
2b9f9 63 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a  ct InLoop *pIn;.
2b9fa 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 58 2d  .    assert( pX-
2b9fb 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20  >op==TK_IN );.  
2b9fc 20 20 69 52 65 67 20 3d 20 69 54 61 72 67 65 74    iReg = iTarget
2b9fd 3b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 73 71  ;.    eType = sq
2b9fe 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78  lite3FindInIndex
2b9ff 28 70 50 61 72 73 65 2c 20 70 58 2c 20 30 29 3b  (pParse, pX, 0);
2ba00 0a 20 20 20 20 69 54 61 62 20 3d 20 70 58 2d 3e  .    iTab = pX->
2ba01 69 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69  iTable;.    sqli
2ba02 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2ba03 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62   OP_Rewind, iTab
2ba04 2c 20 30 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  , 0);.    VdbeCo
2ba05 6d 6d 65 6e 74 28 28 76 2c 20 22 25 2e 2a 73 22  mment((v, "%.*s"
2ba06 2c 20 70 58 2d 3e 73 70 61 6e 2e 6e 2c 20 70 58  , pX->span.n, pX
2ba07 2d 3e 73 70 61 6e 2e 7a 29 29 3b 0a 20 20 20 20  ->span.z));.    
2ba08 61 73 73 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e  assert( pLevel->
2ba09 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2ba0a 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 29 3b 0a  HERE_IN_ABLE );.
2ba0b 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2ba0c 75 2e 69 6e 2e 6e 49 6e 3d 3d 30 20 29 7b 0a 20  u.in.nIn==0 ){. 
2ba0d 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64       pLevel->add
2ba0e 72 4e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  rNxt = sqlite3Vd
2ba0f 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2ba10 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c      }.    pLevel
2ba11 2d 3e 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a 20 20  ->u.in.nIn++;.  
2ba12 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61    pLevel->u.in.a
2ba13 49 6e 4c 6f 6f 70 20 3d 0a 20 20 20 20 20 20 20  InLoop =.       
2ba14 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
2ba15 4f 72 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64  OrFree(pParse->d
2ba16 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  b, pLevel->u.in.
2ba17 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20 20  aInLoop,.       
2ba18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ba19 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c         sizeof(pL
2ba1a 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f  evel->u.in.aInLo
2ba1b 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e 75  op[0])*pLevel->u
2ba1c 2e 69 6e 2e 6e 49 6e 29 3b 0a 20 20 20 20 70 49  .in.nIn);.    pI
2ba1d 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  n = pLevel->u.in
2ba1e 2e 61 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69 66  .aInLoop;.    if
2ba1f 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 20 20 70  ( pIn ){.      p
2ba20 49 6e 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e  In += pLevel->u.
2ba21 69 6e 2e 6e 49 6e 20 2d 20 31 3b 0a 20 20 20 20  in.nIn - 1;.    
2ba22 20 20 70 49 6e 2d 3e 69 43 75 72 20 3d 20 69 54    pIn->iCur = iT
2ba23 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54  ab;.      if( eT
2ba24 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f  ype==IN_INDEX_RO
2ba25 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70  WID ){.        p
2ba26 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20  In->addrInTop = 
2ba27 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ba28 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
2ba29 54 61 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  Tab, iReg);.    
2ba2a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2ba2b 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20   pIn->addrInTop 
2ba2c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2ba2d 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2ba2e 2c 20 69 54 61 62 2c 20 30 2c 20 69 52 65 67 29  , iTab, 0, iReg)
2ba2f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2ba30 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ba31 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  1(v, OP_IsNull, 
2ba32 69 52 65 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65  iReg);.    }else
2ba33 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
2ba34 75 2e 69 6e 2e 6e 49 6e 20 3d 20 30 3b 0a 20 20  u.in.nIn = 0;.  
2ba35 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
2ba36 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2ba37 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 72  vel, pTerm);.  r
2ba38 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f  eturn iReg;.}../
2ba39 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2ba3a 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61  de that will eva
2ba3b 6c 75 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e 64  luate all == and
2ba3c 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   IN constraints 
2ba3d 66 6f 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e  for an.** index.
2ba3e 20 20 54 68 65 20 76 61 6c 75 65 73 20 66 6f 72    The values for
2ba3f 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73   all constraints
2ba40 20 61 72 65 20 6c 65 66 74 20 6f 6e 20 74 68 65   are left on the
2ba41 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f   stack..**.** Fo
2ba42 72 20 65 78 61 6d 70 6c 65 2c 20 63 6f 6e 73 69  r example, consi
2ba43 64 65 72 20 74 61 62 6c 65 20 74 31 28 61 2c 62  der table t1(a,b
2ba44 2c 63 2c 64 2c 65 2c 66 29 20 77 69 74 68 20 69  ,c,d,e,f) with i
2ba45 6e 64 65 78 20 69 31 28 61 2c 62 2c 63 29 2e 0a  ndex i1(a,b,c)..
2ba46 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20 57  ** Suppose the W
2ba47 48 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 74  HERE clause is t
2ba48 68 69 73 3a 20 20 61 3d 3d 35 20 41 4e 44 20 62  his:  a==5 AND b
2ba49 20 49 4e 20 28 31 2c 32 2c 33 29 20 41 4e 44 20   IN (1,2,3) AND 
2ba4a 63 3e 35 20 41 4e 44 20 63 3c 31 30 0a 2a 2a 20  c>5 AND c<10.** 
2ba4b 54 68 65 20 69 6e 64 65 78 20 68 61 73 20 61 73  The index has as
2ba4c 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 65   many as three e
2ba4d 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2ba4e 6e 74 73 2c 20 62 75 74 20 69 6e 20 74 68 69 73  nts, but in this
2ba4f 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  .** example, the
2ba50 20 74 68 69 72 64 20 22 63 22 20 76 61 6c 75 65   third "c" value
2ba51 20 69 73 20 61 6e 20 69 6e 65 71 75 61 6c 69 74   is an inequalit
2ba52 79 2e 20 20 53 6f 20 6f 6e 6c 79 20 74 77 6f 20  y.  So only two 
2ba53 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  .** constraints 
2ba54 61 72 65 20 63 6f 64 65 64 2e 20 20 54 68 69 73  are coded.  This
2ba55 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 67 65   routine will ge
2ba56 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65  nerate code to e
2ba57 76 61 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d 35 20  valuate.** a==5 
2ba58 61 6e 64 20 62 20 49 4e 20 28 31 2c 32 2c 33 29  and b IN (1,2,3)
2ba59 2e 20 20 54 68 65 20 63 75 72 72 65 6e 74 20 76  .  The current v
2ba5a 61 6c 75 65 73 20 66 6f 72 20 61 20 61 6e 64 20  alues for a and 
2ba5b 62 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64  b will be stored
2ba5c 0a 2a 2a 20 69 6e 20 63 6f 6e 73 65 63 75 74 69  .** in consecuti
2ba5d 76 65 20 72 65 67 69 73 74 65 72 73 20 61 6e 64  ve registers and
2ba5e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
2ba5f 65 20 66 69 72 73 74 20 72 65 67 69 73 74 65 72  e first register
2ba60 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
2ba61 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70  .** In the examp
2ba62 6c 65 20 61 62 6f 76 65 20 6e 45 71 3d 3d 32 2e  le above nEq==2.
2ba63 20 20 42 75 74 20 74 68 69 73 20 73 75 62 72 6f    But this subro
2ba64 75 74 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20  utine works for 
2ba65 61 6e 79 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  any value.** of 
2ba66 6e 45 71 20 69 6e 63 6c 75 64 69 6e 67 20 30 2e  nEq including 0.
2ba67 20 20 49 66 20 6e 45 71 3d 3d 30 2c 20 74 68 69    If nEq==0, thi
2ba68 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 61  s routine is nea
2ba69 72 6c 79 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20  rly a no-op..** 
2ba6a 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 69  The only thing i
2ba6b 74 20 64 6f 65 73 20 69 73 20 61 6c 6c 6f 63 61  t does is alloca
2ba6c 74 65 20 74 68 65 20 70 4c 65 76 65 6c 2d 3e 69  te the pLevel->i
2ba6d 4d 65 6d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  Mem memory cell.
2ba6e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2ba6f 69 6e 65 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63  ine always alloc
2ba70 61 74 65 73 20 61 74 20 6c 65 61 73 74 20 6f 6e  ates at least on
2ba71 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e  e memory cell an
2ba72 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65  d returns.** the
2ba73 20 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20 6d   index of that m
2ba74 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 54 68 65 20  emory cell. The 
2ba75 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 63 61 6c  code that.** cal
2ba76 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2ba77 77 69 6c 6c 20 75 73 65 20 74 68 61 74 20 6d 65  will use that me
2ba78 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 74 6f  mory cell to sto
2ba79 72 65 20 74 68 65 20 74 65 72 6d 69 6e 61 74 69  re the terminati
2ba7a 6f 6e 0a 2a 2a 20 6b 65 79 20 76 61 6c 75 65 20  on.** key value 
2ba7b 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 49 66  of the loop.  If
2ba7c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 49 4e 20   one or more IN 
2ba7d 6f 70 65 72 61 74 6f 72 73 20 61 70 70 65 61 72  operators appear
2ba7e 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72  , then.** this r
2ba7f 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
2ba80 20 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e   an additional n
2ba81 45 71 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  Eq memory cells 
2ba82 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  for internal.** 
2ba83 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  use..*/.static i
2ba84 6e 74 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c 69  nt codeAllEquali
2ba85 74 79 54 65 72 6d 73 28 0a 20 20 50 61 72 73 65  tyTerms(.  Parse
2ba86 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2ba87 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2ba88 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65  ext */.  WhereLe
2ba89 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 20 20 2f  vel *pLevel,   /
2ba8a 2a 20 57 68 69 63 68 20 6e 65 73 74 65 64 20 6c  * Which nested l
2ba8b 6f 6f 70 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  oop of the FROM 
2ba8c 77 65 20 61 72 65 20 63 6f 64 69 6e 67 20 2a 2f  we are coding */
2ba8d 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2ba8e 70 57 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  pWC,     /* The 
2ba8f 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2ba90 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
2ba91 64 79 2c 20 20 20 20 20 2f 2a 20 57 68 69 63 68  dy,     /* Which
2ba92 20 70 61 72 74 73 20 6f 66 20 46 52 4f 4d 20 68   parts of FROM h
2ba93 61 76 65 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  ave not yet been
2ba94 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
2ba95 6e 45 78 74 72 61 52 65 67 20 20 20 20 20 20 20  nExtraReg       
2ba96 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
2ba97 78 74 72 61 20 72 65 67 69 73 74 65 72 73 20 74  xtra registers t
2ba98 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 29 7b  o allocate */.){
2ba99 0a 20 20 69 6e 74 20 6e 45 71 20 3d 20 70 4c 65  .  int nEq = pLe
2ba9a 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 3b 20 20  vel->plan.nEq;  
2ba9b 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   /* The number o
2ba9c 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74  f == or IN const
2ba9d 72 61 69 6e 74 73 20 74 6f 20 63 6f 64 65 20 2a  raints to code *
2ba9e 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  /.  Vdbe *v = pP
2ba9f 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20  arse->pVdbe;    
2baa0 20 20 2f 2a 20 54 68 65 20 76 6d 20 75 6e 64 65    /* The vm unde
2baa1 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a  r construction *
2baa2 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  /.  Index *pIdx;
2baa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2baa4 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 62    /* The index b
2baa5 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68  eing used for th
2baa6 69 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  is loop */.  int
2baa7 20 69 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e   iCur = pLevel->
2baa8 69 54 61 62 43 75 72 3b 20 20 20 2f 2a 20 54 68  iTabCur;   /* Th
2baa9 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20  e cursor of the 
2baaa 74 61 62 6c 65 20 2a 2f 0a 20 20 57 68 65 72 65  table */.  Where
2baab 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20  Term *pTerm;    
2baac 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69           /* A si
2baad 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20  ngle constraint 
2baae 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b  term */.  int j;
2baaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bab0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2bab1 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
2bab2 20 72 65 67 42 61 73 65 3b 20 20 20 20 20 20 20   regBase;       
2bab3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61             /* Ba
2bab4 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  se register */. 
2bab5 20 69 6e 74 20 6e 52 65 67 3b 20 20 20 20 20 20   int nReg;      
2bab6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2bab7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69  * Number of regi
2bab8 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74  sters to allocat
2bab9 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20  e */..  /* This 
2baba 6d 6f 64 75 6c 65 20 69 73 20 6f 6e 6c 79 20 63  module is only c
2babb 61 6c 6c 65 64 20 6f 6e 20 71 75 65 72 79 20 70  alled on query p
2babc 6c 61 6e 73 20 74 68 61 74 20 75 73 65 20 61 6e  lans that use an
2babd 20 69 6e 64 65 78 2e 20 2a 2f 0a 20 20 61 73 73   index. */.  ass
2babe 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  ert( pLevel->pla
2babf 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2bac0 45 5f 49 4e 44 45 58 45 44 20 29 3b 0a 20 20 70  E_INDEXED );.  p
2bac1 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  Idx = pLevel->pl
2bac2 61 6e 2e 75 2e 70 49 64 78 3b 0a 0a 20 20 2f 2a  an.u.pIdx;..  /*
2bac3 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
2bac4 6d 61 6e 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  many memory cell
2bac5 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 74  s we will need t
2bac6 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 74 68 65  hen allocate the
2bac7 6d 2e 0a 20 20 2a 2f 0a 20 20 72 65 67 42 61 73  m..  */.  regBas
2bac8 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  e = pParse->nMem
2bac9 20 2b 20 31 3b 0a 20 20 6e 52 65 67 20 3d 20 70   + 1;.  nReg = p
2baca 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 20  Level->plan.nEq 
2bacb 2b 20 6e 45 78 74 72 61 52 65 67 3b 0a 20 20 70  + nExtraReg;.  p
2bacc 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e  Parse->nMem += n
2bacd 52 65 67 3b 0a 0a 20 20 2f 2a 20 45 76 61 6c 75  Reg;..  /* Evalu
2bace 61 74 65 20 74 68 65 20 65 71 75 61 6c 69 74 79  ate the equality
2bacf 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a   constraints.  *
2bad0 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 64 78  /.  assert( pIdx
2bad1 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6e 45 71 20 29  ->nColumn>=nEq )
2bad2 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e  ;.  for(j=0; j<n
2bad3 45 71 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e  Eq; j++){.    in
2bad4 74 20 72 31 3b 0a 20 20 20 20 69 6e 74 20 6b 20  t r1;.    int k 
2bad5 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
2bad6 5b 6a 5d 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d  [j];.    pTerm =
2bad7 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2bad8 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79  Cur, k, notReady
2bad9 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  , pLevel->plan.w
2bada 73 46 6c 61 67 73 2c 20 70 49 64 78 29 3b 0a 20  sFlags, pIdx);. 
2badb 20 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 65     if( NEVER(pTe
2badc 72 6d 3d 3d 30 29 20 29 20 62 72 65 61 6b 3b 0a  rm==0) ) break;.
2badd 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65      assert( (pTe
2bade 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
2badf 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 20 29 3b 0a  RM_CODED)==0 );.
2bae0 20 20 20 20 72 31 20 3d 20 63 6f 64 65 45 71 75      r1 = codeEqu
2bae1 61 6c 69 74 79 54 65 72 6d 28 70 50 61 72 73 65  alityTerm(pParse
2bae2 2c 20 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c 2c  , pTerm, pLevel,
2bae3 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20   regBase+j);.   
2bae4 20 69 66 28 20 72 31 21 3d 72 65 67 42 61 73 65   if( r1!=regBase
2bae5 2b 6a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  +j ){.      if( 
2bae6 6e 52 65 67 3d 3d 31 20 29 7b 0a 20 20 20 20 20  nReg==1 ){.     
2bae7 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2bae8 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2bae9 20 72 65 67 42 61 73 65 29 3b 0a 20 20 20 20 20   regBase);.     
2baea 20 20 20 72 65 67 42 61 73 65 20 3d 20 72 31 3b     regBase = r1;
2baeb 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2baec 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2baed 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
2baee 6f 70 79 2c 20 72 31 2c 20 72 65 67 42 61 73 65  opy, r1, regBase
2baef 2b 6a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  +j);.      }.   
2baf0 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28   }.    testcase(
2baf1 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2baf2 72 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b  r & WO_ISNULL );
2baf3 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
2baf4 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2baf5 26 20 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 69  & WO_IN );.    i
2baf6 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72  f( (pTerm->eOper
2baf7 61 74 6f 72 20 26 20 28 57 4f 5f 49 53 4e 55 4c  ator & (WO_ISNUL
2baf8 4c 7c 57 4f 5f 49 4e 29 29 3d 3d 30 20 29 7b 0a  L|WO_IN))==0 ){.
2baf9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2bafa 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73  eAddOp2(v, OP_Is
2bafb 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6a 2c  Null, regBase+j,
2bafc 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b   pLevel->addrBrk
2bafd 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
2bafe 65 74 75 72 6e 20 72 65 67 42 61 73 65 3b 0a 7d  eturn regBase;.}
2baff 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
2bb00 52 55 45 20 69 66 20 74 68 65 20 57 68 65 72 65  RUE if the Where
2bb01 43 6c 61 75 73 65 20 70 57 43 20 63 6f 6e 74 61  Clause pWC conta
2bb02 69 6e 73 20 6e 6f 20 74 65 72 6d 73 20 74 68 61  ins no terms tha
2bb03 74 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 76 69 72  t.** are not vir
2bb04 74 75 61 6c 20 61 6e 64 20 77 68 69 63 68 20 68  tual and which h
2bb05 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 63 6f 64  ave not been cod
2bb06 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 70 75 74  ed..**.** To put
2bb07 20 69 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c   it another way,
2bb08 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   return TRUE if 
2bb09 6e 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 57 48  no additional WH
2bb0a 45 52 45 20 63 6c 61 75 73 65 73 0a 2a 2a 20 74  ERE clauses.** t
2bb0b 65 73 74 73 20 61 72 65 20 72 65 71 75 69 72 65  ests are require
2bb0c 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 73  d in order to es
2bb0d 74 61 62 6c 69 73 68 20 74 68 61 74 20 74 68 65  tablish that the
2bb0e 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a 20   current row.** 
2bb0f 73 68 6f 75 6c 64 20 67 6f 20 74 6f 20 6f 75 74  should go to out
2bb10 70 75 74 20 61 6e 64 20 72 65 74 75 72 6e 20 46  put and return F
2bb11 41 4c 53 45 20 69 66 20 74 68 65 72 65 20 61 72  ALSE if there ar
2bb12 65 20 73 6f 6d 65 20 74 65 72 6d 73 20 6f 66 0a  e some terms of.
2bb13 2a 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  ** the WHERE cla
2bb14 75 73 65 20 74 68 61 74 20 6e 65 65 64 20 74 6f  use that need to
2bb15 20 62 65 20 76 61 6c 69 64 61 74 65 64 20 62 65   be validated be
2bb16 66 6f 72 65 20 6f 75 74 70 75 74 69 6e 67 20 74  fore outputing t
2bb17 68 65 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69  he row..*/.stati
2bb18 63 20 69 6e 74 20 77 68 65 72 65 52 6f 77 52 65  c int whereRowRe
2bb19 61 64 79 46 6f 72 4f 75 74 70 75 74 28 57 68 65  adyForOutput(Whe
2bb1a 72 65 43 6c 61 75 73 65 20 2a 70 57 43 29 7b 0a  reClause *pWC){.
2bb1b 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
2bb1c 72 6d 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 0a 20  rm;.  int j;. . 
2bb1d 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e   for(pTerm=pWC->
2bb1e 61 2c 20 6a 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b  a, j=pWC->nTerm;
2bb1f 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70 54 65 72 6d   j>0; j--, pTerm
2bb20 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 70 54  ++){.    if( (pT
2bb21 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28  erm->wtFlags & (
2bb22 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
2bb23 4d 5f 43 4f 44 45 44 29 29 3d 3d 30 20 29 20 72  M_CODED))==0 ) r
2bb24 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72  eturn 0;.  }.  r
2bb25 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
2bb26 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2bb27 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f 66  for the start of
2bb28 20 74 68 65 20 69 4c 65 76 65 6c 2d 74 68 20 6c   the iLevel-th l
2bb29 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45 52 45  oop in the WHERE
2bb2a 20 63 6c 61 75 73 65 0a 2a 2a 20 69 6d 70 6c 65   clause.** imple
2bb2b 6d 65 6e 74 61 74 69 6f 6e 20 64 65 73 63 72 69  mentation descri
2bb2c 62 65 64 20 62 79 20 70 57 49 6e 66 6f 2e 0a 2a  bed by pWInfo..*
2bb2d 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b  /.static Bitmask
2bb2e 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72   codeOneLoopStar
2bb2f 74 28 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  t(.  WhereInfo *
2bb30 70 57 49 6e 66 6f 2c 20 20 20 2f 2a 20 43 6f 6d  pWInfo,   /* Com
2bb31 70 6c 65 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  plete informatio
2bb32 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 52  n about the WHER
2bb33 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  E clause */.  in
2bb34 74 20 69 4c 65 76 65 6c 2c 20 20 20 20 20 20 20  t iLevel,       
2bb35 20 20 20 2f 2a 20 57 68 69 63 68 20 6c 65 76 65     /* Which leve
2bb36 6c 20 6f 66 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  l of pWInfo->a[]
2bb37 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 64 65 64   should be coded
2bb38 20 2a 2f 0a 20 20 75 38 20 77 63 74 72 6c 46 6c   */.  u8 wctrlFl
2bb39 61 67 73 2c 20 20 20 20 20 20 20 2f 2a 20 4f 6e  ags,       /* On
2bb3a 65 20 6f 66 20 74 68 65 20 57 48 45 52 45 5f 2a  e of the WHERE_*
2bb3b 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20 69   flags defined i
2bb3c 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f  n sqliteInt.h */
2bb3d 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65  .  Bitmask notRe
2bb3e 61 64 79 20 20 20 20 20 2f 2a 20 57 68 69 63 68  ady     /* Which
2bb3f 20 74 61 62 6c 65 73 20 61 72 65 20 63 75 72 72   tables are curr
2bb40 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 20  ently available 
2bb41 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6a 2c 20 6b  */.){.  int j, k
2bb42 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2bb43 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
2bb44 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20  .  int iCur;    
2bb45 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
2bb46 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
2bb47 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
2bb48 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20  t addrNxt;      
2bb49 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 6a     /* Where to j
2bb4a 75 6d 70 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  ump to continue 
2bb4b 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e  with the next IN
2bb4c 20 63 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f   case */.  int o
2bb4d 6d 69 74 54 61 62 6c 65 3b 20 20 20 20 20 20 20  mitTable;       
2bb4e 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 75 73  /* True if we us
2bb4f 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 6c 79  e the index only
2bb50 20 2a 2f 0a 20 20 69 6e 74 20 62 52 65 76 3b 20   */.  int bRev; 
2bb51 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
2bb52 75 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f  ue if we need to
2bb53 20 73 63 61 6e 20 69 6e 20 72 65 76 65 72 73 65   scan in reverse
2bb54 20 6f 72 64 65 72 20 2a 2f 0a 20 20 57 68 65 72   order */.  Wher
2bb55 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20  eLevel *pLevel; 
2bb56 20 2f 2a 20 54 68 65 20 77 68 65 72 65 20 6c 65   /* The where le
2bb57 76 65 6c 20 74 6f 20 62 65 20 63 6f 64 65 64 20  vel to be coded 
2bb58 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
2bb59 20 2a 70 57 43 3b 20 20 20 20 2f 2a 20 44 65 63   *pWC;    /* Dec
2bb5a 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68  omposition of th
2bb5b 65 20 65 6e 74 69 72 65 20 57 48 45 52 45 20 63  e entire WHERE c
2bb5c 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65  lause */.  Where
2bb5d 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20  Term *pTerm;    
2bb5e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
2bb5f 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
2bb60 6d 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50  m */.  Parse *pP
2bb61 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  arse;           
2bb62 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2bb63 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 56  g context */.  V
2bb64 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
2bb65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2bb66 2a 20 54 68 65 20 70 72 65 70 61 72 65 64 20 73  * The prepared s
2bb67 74 6d 74 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tmt under constr
2bb68 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72  uctions */.  str
2bb69 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2bb6a 20 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20   *pTabItem;  /* 
2bb6b 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
2bb6c 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a   being coded */.
2bb6d 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b 20 20    int addrBrk;  
2bb6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb6f 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2bb70 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74  o break out of t
2bb71 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
2bb72 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 20   addrCont;      
2bb73 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bb74 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e  Jump here to con
2bb75 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20  tinue with next 
2bb76 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72  cycle */.  int r
2bb77 65 67 52 6f 77 53 65 74 3b 20 20 20 20 20 20 20  egRowSet;       
2bb78 2f 2a 20 57 72 69 74 65 20 72 6f 77 69 64 73 20  /* Write rowids 
2bb79 74 6f 20 74 68 69 73 20 52 6f 77 53 65 74 20 69  to this RowSet i
2bb7a 66 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 2a  f non-negative *
2bb7b 2f 0a 20 20 69 6e 74 20 63 6f 64 65 52 6f 77 53  /.  int codeRowS
2bb7c 65 74 45 61 72 6c 79 3b 20 2f 2a 20 54 72 75 65  etEarly; /* True
2bb7d 20 69 66 20 69 6e 64 65 78 20 66 75 6c 6c 79 20   if index fully 
2bb7e 63 6f 6e 73 74 72 61 69 6e 73 20 74 68 65 20 73  constrains the s
2bb7f 65 61 72 63 68 20 2a 2f 0a 20 20 0a 0a 20 20 70  earch */.  ..  p
2bb80 50 61 72 73 65 20 3d 20 70 57 49 6e 66 6f 2d 3e  Parse = pWInfo->
2bb81 70 50 61 72 73 65 3b 0a 20 20 76 20 3d 20 70 50  pParse;.  v = pP
2bb82 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 70  arse->pVdbe;.  p
2bb83 57 43 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 57 43  WC = pWInfo->pWC
2bb84 3b 0a 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57  ;.  pLevel = &pW
2bb85 49 6e 66 6f 2d 3e 61 5b 69 4c 65 76 65 6c 5d 3b  Info->a[iLevel];
2bb86 0a 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70  .  pTabItem = &p
2bb87 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 2d  WInfo->pTabList-
2bb88 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
2bb89 5d 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61 62  ];.  iCur = pTab
2bb8a 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20  Item->iCursor;. 
2bb8b 20 62 52 65 76 20 3d 20 28 70 4c 65 76 65 6c 2d   bRev = (pLevel-
2bb8c 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2bb8d 57 48 45 52 45 5f 52 45 56 45 52 53 45 29 21 3d  WHERE_REVERSE)!=
2bb8e 30 3b 0a 20 20 6f 6d 69 74 54 61 62 6c 65 20 3d  0;.  omitTable =
2bb8f 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
2bb90 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
2bb91 44 58 5f 4f 4e 4c 59 29 21 3d 30 3b 0a 20 20 72  DX_ONLY)!=0;.  r
2bb92 65 67 52 6f 77 53 65 74 20 3d 20 70 57 49 6e 66  egRowSet = pWInf
2bb93 6f 2d 3e 72 65 67 52 6f 77 53 65 74 3b 0a 20 20  o->regRowSet;.  
2bb94 63 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20  codeRowSetEarly 
2bb95 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 72 65 61 74  = 0;..  /* Creat
2bb96 65 20 6c 61 62 65 6c 73 20 66 6f 72 20 74 68 65  e labels for the
2bb97 20 22 62 72 65 61 6b 22 20 61 6e 64 20 22 63 6f   "break" and "co
2bb98 6e 74 69 6e 75 65 22 20 69 6e 73 74 72 75 63 74  ntinue" instruct
2bb99 69 6f 6e 73 0a 20 20 2a 2a 20 66 6f 72 20 74 68  ions.  ** for th
2bb9a 65 20 63 75 72 72 65 6e 74 20 6c 6f 6f 70 2e 20  e current loop. 
2bb9b 20 4a 75 6d 70 20 74 6f 20 61 64 64 72 42 72 6b   Jump to addrBrk
2bb9c 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
2bb9d 20 61 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 4a 75   a loop..  ** Ju
2bb9e 6d 70 20 74 6f 20 63 6f 6e 74 20 74 6f 20 67 6f  mp to cont to go
2bb9f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
2bba0 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69  the next iterati
2bba1 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c  on of the.  ** l
2bba2 6f 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  oop..  **.  ** W
2bba3 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6e 20  hen there is an 
2bba4 49 4e 20 6f 70 65 72 61 74 6f 72 2c 20 77 65 20  IN operator, we 
2bba5 61 6c 73 6f 20 68 61 76 65 20 61 20 22 61 64 64  also have a "add
2bba6 72 4e 78 74 22 20 6c 61 62 65 6c 20 74 68 61 74  rNxt" label that
2bba7 0a 20 20 2a 2a 20 6d 65 61 6e 73 20 74 6f 20 63  .  ** means to c
2bba8 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65  ontinue with the
2bba9 20 6e 65 78 74 20 49 4e 20 76 61 6c 75 65 20 63   next IN value c
2bbaa 6f 6d 62 69 6e 61 74 69 6f 6e 2e 20 20 57 68 65  ombination.  Whe
2bbab 6e 0a 20 20 2a 2a 20 74 68 65 72 65 20 61 72 65  n.  ** there are
2bbac 20 6e 6f 20 49 4e 20 6f 70 65 72 61 74 6f 72 73   no IN operators
2bbad 20 69 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69   in the constrai
2bbae 6e 74 73 2c 20 74 68 65 20 22 61 64 64 72 4e 78  nts, the "addrNx
2bbaf 74 22 20 6c 61 62 65 6c 0a 20 20 2a 2a 20 69 73  t" label.  ** is
2bbb0 20 74 68 65 20 73 61 6d 65 20 61 73 20 22 61 64   the same as "ad
2bbb1 64 72 42 72 6b 22 2e 0a 20 20 2a 2f 0a 20 20 61  drBrk"..  */.  a
2bbb2 64 64 72 42 72 6b 20 3d 20 70 4c 65 76 65 6c 2d  ddrBrk = pLevel-
2bbb3 3e 61 64 64 72 42 72 6b 20 3d 20 70 4c 65 76 65  >addrBrk = pLeve
2bbb4 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20 73 71 6c  l->addrNxt = sql
2bbb5 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2bbb6 6c 28 76 29 3b 0a 20 20 61 64 64 72 43 6f 6e 74  l(v);.  addrCont
2bbb7 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 43   = pLevel->addrC
2bbb8 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ont = sqlite3Vdb
2bbb9 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a  eMakeLabel(v);..
2bbba 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
2bbbb 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
2bbbc 6f 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20  of a LEFT OUTER 
2bbbd 4a 4f 49 4e 2c 20 61 6c 6c 6f 63 61 74 65 20 61  JOIN, allocate a
2bbbe 6e 64 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69  nd.  ** initiali
2bbbf 7a 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  ze a memory cell
2bbc0 20 74 68 61 74 20 72 65 63 6f 72 64 73 20 69 66   that records if
2bbc1 20 74 68 69 73 20 74 61 62 6c 65 20 6d 61 74 63   this table matc
2bbc2 68 65 73 20 61 6e 79 0a 20 20 2a 2a 20 72 6f 77  hes any.  ** row
2bbc3 20 6f 66 20 74 68 65 20 6c 65 66 74 20 74 61 62   of the left tab
2bbc4 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a  le of the join..
2bbc5 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 65 76 65    */.  if( pLeve
2bbc6 6c 2d 3e 69 46 72 6f 6d 3e 30 20 26 26 20 28 70  l->iFrom>0 && (p
2bbc7 54 61 62 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74  TabItem[0].joint
2bbc8 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d  ype & JT_LEFT)!=
2bbc9 30 20 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  0 ){.    pLevel-
2bbca 3e 69 4c 65 66 74 4a 6f 69 6e 20 3d 20 2b 2b 70  >iLeftJoin = ++p
2bbcb 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2bbcc 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2bbcd 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2bbce 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65  , 0, pLevel->iLe
2bbcf 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 56 64 62  ftJoin);.    Vdb
2bbd0 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e  eComment((v, "in
2bbd1 69 74 20 4c 45 46 54 20 4a 4f 49 4e 20 6e 6f 2d  it LEFT JOIN no-
2bbd2 6d 61 74 63 68 20 66 6c 61 67 22 29 29 3b 0a 20  match flag"));. 
2bbd3 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
2bbd4 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2bbd5 41 42 4c 45 0a 20 20 69 66 28 20 20 28 70 4c 65  ABLE.  if(  (pLe
2bbd6 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2bbd7 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 41  s & WHERE_VIRTUA
2bbd8 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20  LTABLE)!=0 ){.  
2bbd9 20 20 2f 2a 20 43 61 73 65 20 30 3a 20 20 54 68    /* Case 0:  Th
2bbda 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72  e table is a vir
2bbdb 74 75 61 6c 2d 74 61 62 6c 65 2e 20 20 55 73 65  tual-table.  Use
2bbdc 20 74 68 65 20 56 46 69 6c 74 65 72 20 61 6e 64   the VFilter and
2bbdd 20 56 4e 65 78 74 0a 20 20 20 20 2a 2a 20 20 20   VNext.    **   
2bbde 20 20 20 20 20 20 20 74 6f 20 61 63 63 65 73 73         to access
2bbdf 20 74 68 65 20 64 61 74 61 2e 0a 20 20 20 20 2a   the data..    *
2bbe0 2f 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20  /.    int iReg; 
2bbe1 20 20 2f 2a 20 50 33 20 56 61 6c 75 65 20 66 6f    /* P3 Value fo
2bbe2 72 20 4f 50 5f 56 46 69 6c 74 65 72 20 2a 2f 0a  r OP_VFilter */.
2bbe3 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65      sqlite3_inde
2bbe4 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78  x_info *pVtabIdx
2bbe5 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
2bbe6 75 2e 70 56 74 61 62 49 64 78 3b 0a 20 20 20 20  u.pVtabIdx;.    
2bbe7 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20  int nConstraint 
2bbe8 3d 20 70 56 74 61 62 49 64 78 2d 3e 6e 43 6f 6e  = pVtabIdx->nCon
2bbe9 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 73 74 72  straint;.    str
2bbea 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2bbeb 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
2bbec 67 65 20 2a 61 55 73 61 67 65 20 3d 0a 20 20 20  ge *aUsage =.   
2bbed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbef 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56 74               pVt
2bbf0 61 62 49 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69  abIdx->aConstrai
2bbf1 6e 74 55 73 61 67 65 3b 0a 20 20 20 20 63 6f 6e  ntUsage;.    con
2bbf2 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  st struct sqlite
2bbf3 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2bbf4 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 20  nt *aConstraint 
2bbf5 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
2bbf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbf8 20 20 70 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e    pVtabIdx->aCon
2bbf9 73 74 72 61 69 6e 74 3b 0a 0a 20 20 20 20 69 52  straint;..    iR
2bbfa 65 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  eg = sqlite3GetT
2bbfb 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2bbfc 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b   nConstraint+2);
2bbfd 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73  .    pParse->dis
2bbfe 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a  ableColCache++;.
2bbff 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 3d      for(j=1; j<=
2bc00 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b  nConstraint; j++
2bc01 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 30  ){.      for(k=0
2bc02 3b 20 6b 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  ; k<nConstraint;
2bc03 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   k++){.        i
2bc04 66 28 20 61 55 73 61 67 65 5b 6b 5d 2e 61 72 67  f( aUsage[k].arg
2bc05 76 49 6e 64 65 78 3d 3d 6a 20 29 7b 0a 20 20 20  vIndex==j ){.   
2bc06 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d         int iTerm
2bc07 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6b   = aConstraint[k
2bc08 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20  ].iTermOffset;. 
2bc09 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2bc0a 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
2bc0b 43 6f 6c 43 61 63 68 65 20 29 3b 0a 20 20 20 20  ColCache );.    
2bc0c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2bc0d 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 57  rCode(pParse, pW
2bc0e 43 2d 3e 61 5b 69 54 65 72 6d 5d 2e 70 45 78 70  C->a[iTerm].pExp
2bc0f 72 2d 3e 70 52 69 67 68 74 2c 20 69 52 65 67 2b  r->pRight, iReg+
2bc10 6a 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  j+1);.          
2bc11 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
2bc12 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2bc13 66 28 20 6b 3d 3d 6e 43 6f 6e 73 74 72 61 69 6e  f( k==nConstrain
2bc14 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d  t ) break;.    }
2bc15 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
2bc16 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
2bc17 61 63 68 65 20 29 3b 0a 20 20 20 20 70 50 61 72  ache );.    pPar
2bc18 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
2bc19 63 68 65 2d 2d 3b 0a 20 20 20 20 73 71 6c 69 74  che--;.    sqlit
2bc1a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2bc1b 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 56 74 61  OP_Integer, pVta
2bc1c 62 49 64 78 2d 3e 69 64 78 4e 75 6d 2c 20 69 52  bIdx->idxNum, iR
2bc1d 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  eg);.    sqlite3
2bc1e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2bc1f 5f 49 6e 74 65 67 65 72 2c 20 6a 2d 31 2c 20 69  _Integer, j-1, i
2bc20 52 65 67 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  Reg+1);.    sqli
2bc21 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2bc22 20 4f 50 5f 56 46 69 6c 74 65 72 2c 20 69 43 75   OP_VFilter, iCu
2bc23 72 2c 20 61 64 64 72 42 72 6b 2c 20 69 52 65 67  r, addrBrk, iReg
2bc24 2c 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78 53  , pVtabIdx->idxS
2bc25 74 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  tr,.            
2bc26 20 20 20 20 20 20 20 20 20 20 70 56 74 61 62 49            pVtabI
2bc27 64 78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64  dx->needToFreeId
2bc28 78 53 74 72 20 3f 20 50 34 5f 4d 50 52 49 4e 54  xStr ? P4_MPRINT
2bc29 46 20 3a 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  F : P4_STATIC);.
2bc2a 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 6e 65      pVtabIdx->ne
2bc2b 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d  edToFreeIdxStr =
2bc2c 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b   0;.    for(j=0;
2bc2d 20 6a 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20   j<nConstraint; 
2bc2e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
2bc2f 61 55 73 61 67 65 5b 6a 5d 2e 6f 6d 69 74 20 29  aUsage[j].omit )
2bc30 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 54  {.        int iT
2bc31 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e  erm = aConstrain
2bc32 74 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74  t[j].iTermOffset
2bc33 3b 0a 20 20 20 20 20 20 20 20 64 69 73 61 62 6c  ;.        disabl
2bc34 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 26 70  eTerm(pLevel, &p
2bc35 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 29 3b 0a 20  WC->a[iTerm]);. 
2bc36 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2bc37 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50   pLevel->op = OP
2bc38 5f 56 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76  _VNext;.    pLev
2bc39 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20  el->p1 = iCur;. 
2bc3a 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20     pLevel->p2 = 
2bc3b 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2bc3c 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 63  ntAddr(v);.    c
2bc3d 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20 3d  odeRowSetEarly =
2bc3e 20 72 65 67 52 6f 77 53 65 74 3e 3d 30 20 3f 20   regRowSet>=0 ? 
2bc3f 77 68 65 72 65 52 6f 77 52 65 61 64 79 46 6f 72  whereRowReadyFor
2bc40 4f 75 74 70 75 74 28 70 57 43 29 20 3a 20 30 3b  Output(pWC) : 0;
2bc41 0a 20 20 20 20 69 66 28 20 63 6f 64 65 52 6f 77  .    if( codeRow
2bc42 53 65 74 45 61 72 6c 79 20 29 7b 0a 20 20 20 20  SetEarly ){.    
2bc43 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2bc44 4f 70 32 28 76 2c 20 4f 50 5f 56 52 6f 77 69 64  Op2(v, OP_VRowid
2bc45 2c 20 69 43 75 72 2c 20 69 52 65 67 29 3b 0a 20  , iCur, iReg);. 
2bc46 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2bc47 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
2bc48 53 65 74 41 64 64 2c 20 72 65 67 52 6f 77 53 65  SetAdd, regRowSe
2bc49 74 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d 0a  t, iReg);.    }.
2bc4a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2bc4b 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
2bc4c 73 65 2c 20 69 52 65 67 2c 20 6e 43 6f 6e 73 74  se, iReg, nConst
2bc4d 72 61 69 6e 74 2b 32 29 3b 0a 20 20 7d 65 6c 73  raint+2);.  }els
2bc4e 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
2bc4f 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2bc50 41 42 4c 45 20 2a 2f 0a 0a 20 20 69 66 28 20 70  ABLE */..  if( p
2bc51 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2bc52 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49  ags & WHERE_ROWI
2bc53 44 5f 45 51 20 29 7b 0a 20 20 20 20 2f 2a 20 43  D_EQ ){.    /* C
2bc54 61 73 65 20 31 3a 20 20 57 65 20 63 61 6e 20 64  ase 1:  We can d
2bc55 69 72 65 63 74 6c 79 20 72 65 66 65 72 65 6e 63  irectly referenc
2bc56 65 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 75  e a single row u
2bc57 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 20  sing an.    **  
2bc58 20 20 20 20 20 20 20 20 65 71 75 61 6c 69 74 79          equality
2bc59 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69   comparison agai
2bc5a 6e 73 74 20 74 68 65 20 52 4f 57 49 44 20 66 69  nst the ROWID fi
2bc5b 65 6c 64 2e 20 20 4f 72 0a 20 20 20 20 2a 2a 20  eld.  Or.    ** 
2bc5c 20 20 20 20 20 20 20 20 20 77 65 20 72 65 66 65           we refe
2bc5d 72 65 6e 63 65 20 6d 75 6c 74 69 70 6c 65 20 72  rence multiple r
2bc5e 6f 77 73 20 75 73 69 6e 67 20 61 20 22 72 6f 77  ows using a "row
2bc5f 69 64 20 49 4e 20 28 2e 2e 2e 29 22 0a 20 20 20  id IN (...)".   
2bc60 20 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e   **          con
2bc61 73 74 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20  struct..    */. 
2bc62 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 69     int r1;.    i
2bc63 6e 74 20 72 74 6d 70 20 3d 20 73 71 6c 69 74 65  nt rtmp = sqlite
2bc64 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2bc65 73 65 29 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d  se);.    pTerm =
2bc66 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2bc67 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64  Cur, -1, notRead
2bc68 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c 20  y, WO_EQ|WO_IN, 
2bc69 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
2bc6a 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20  pTerm!=0 );.    
2bc6b 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70  assert( pTerm->p
2bc6c 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 61  Expr!=0 );.    a
2bc6d 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 6c 65  ssert( pTerm->le
2bc6e 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29  ftCursor==iCur )
2bc6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d  ;.    assert( om
2bc70 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  itTable==0 );.  
2bc71 20 20 72 31 20 3d 20 63 6f 64 65 45 71 75 61 6c    r1 = codeEqual
2bc72 69 74 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20  ityTerm(pParse, 
2bc73 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 72  pTerm, pLevel, r
2bc74 74 6d 70 29 3b 0a 20 20 20 20 61 64 64 72 4e 78  tmp);.    addrNx
2bc75 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72  t = pLevel->addr
2bc76 4e 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Nxt;.    sqlite3
2bc77 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2bc78 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 31 2c 20  _MustBeInt, r1, 
2bc79 61 64 64 72 4e 78 74 29 3b 0a 20 20 20 20 73 71  addrNxt);.    sq
2bc7a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2bc7b 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
2bc7c 20 69 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20   iCur, addrNxt, 
2bc7d 72 31 29 3b 0a 20 20 20 20 63 6f 64 65 52 6f 77  r1);.    codeRow
2bc7e 53 65 74 45 61 72 6c 79 20 3d 20 28 70 57 43 2d  SetEarly = (pWC-
2bc7f 3e 6e 54 65 72 6d 3d 3d 31 20 26 26 20 72 65 67  >nTerm==1 && reg
2bc80 52 6f 77 53 65 74 3e 3d 30 29 20 3f 31 3a 30 3b  RowSet>=0) ?1:0;
2bc81 0a 20 20 20 20 69 66 28 20 63 6f 64 65 52 6f 77  .    if( codeRow
2bc82 53 65 74 45 61 72 6c 79 20 29 7b 0a 20 20 20 20  SetEarly ){.    
2bc83 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2bc84 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Op2(v, OP_RowSet
2bc85 41 64 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20  Add, regRowSet, 
2bc86 72 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  r1);.    }.    s
2bc87 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2bc88 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 74 6d  pReg(pParse, rtm
2bc89 70 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  p);.    VdbeComm
2bc8a 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a  ent((v, "pk"));.
2bc8b 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
2bc8c 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 7d 65 6c 73   OP_Noop;.  }els
2bc8d 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c  e if( pLevel->pl
2bc8e 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2bc8f 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 29  RE_ROWID_RANGE )
2bc90 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 32 3a  {.    /* Case 2:
2bc91 20 20 57 65 20 68 61 76 65 20 61 6e 20 69 6e 65    We have an ine
2bc92 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72 69 73  quality comparis
2bc93 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65 20 52  on against the R
2bc94 4f 57 49 44 20 66 69 65 6c 64 2e 0a 20 20 20 20  OWID field..    
2bc95 2a 2f 0a 20 20 20 20 69 6e 74 20 74 65 73 74 4f  */.    int testO
2bc96 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20  p = OP_Noop;.   
2bc97 20 69 6e 74 20 73 74 61 72 74 3b 0a 20 20 20 20   int start;.    
2bc98 69 6e 74 20 6d 65 6d 45 6e 64 56 61 6c 75 65 20  int memEndValue 
2bc99 3d 20 30 3b 0a 20 20 20 20 57 68 65 72 65 54 65  = 0;.    WhereTe
2bc9a 72 6d 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e  rm *pStart, *pEn
2bc9b 64 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  d;..    assert( 
2bc9c 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  omitTable==0 );.
2bc9d 20 20 20 20 70 53 74 61 72 74 20 3d 20 66 69 6e      pStart = fin
2bc9e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
2bc9f 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57   -1, notReady, W
2bca0 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a  O_GT|WO_GE, 0);.
2bca1 20 20 20 20 70 45 6e 64 20 3d 20 66 69 6e 64 54      pEnd = findT
2bca2 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d  erm(pWC, iCur, -
2bca3 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f  1, notReady, WO_
2bca4 4c 54 7c 57 4f 5f 4c 45 2c 20 30 29 3b 0a 20 20  LT|WO_LE, 0);.  
2bca5 20 20 69 66 28 20 62 52 65 76 20 29 7b 0a 20 20    if( bRev ){.  
2bca6 20 20 20 20 70 54 65 72 6d 20 3d 20 70 53 74 61      pTerm = pSta
2bca7 72 74 3b 0a 20 20 20 20 20 20 70 53 74 61 72 74  rt;.      pStart
2bca8 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 70   = pEnd;.      p
2bca9 45 6e 64 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20  End = pTerm;.   
2bcaa 20 7d 0a 20 20 20 20 69 66 28 20 70 53 74 61 72   }.    if( pStar
2bcab 74 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  t ){.      Expr 
2bcac 2a 70 58 3b 20 20 20 20 20 20 20 20 20 20 20 20  *pX;            
2bcad 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
2bcae 6f 6e 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  on that defines 
2bcaf 74 68 65 20 73 74 61 72 74 20 62 6f 75 6e 64 20  the start bound 
2bcb0 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 31 2c  */.      int r1,
2bcb1 20 72 54 65 6d 70 3b 20 20 20 20 20 20 20 20 2f   rTemp;        /
2bcb2 2a 20 52 65 67 69 73 74 65 72 73 20 66 6f 72 20  * Registers for 
2bcb3 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 74 61 72  holding the star
2bcb4 74 20 62 6f 75 6e 64 61 72 79 20 2a 2f 0a 0a 20  t boundary */.. 
2bcb5 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c       /* The foll
2bcb6 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 6d  owing constant m
2bcb7 61 70 73 20 54 4b 5f 78 78 20 63 6f 64 65 73 20  aps TK_xx codes 
2bcb8 69 6e 74 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69  into correspondi
2bcb9 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 73 65 65  ng .      ** see
2bcba 6b 20 6f 70 63 6f 64 65 73 2e 20 20 49 74 20 64  k opcodes.  It d
2bcbb 65 70 65 6e 64 73 20 6f 6e 20 61 20 70 61 72 74  epends on a part
2bcbc 69 63 75 6c 61 72 20 6f 72 64 65 72 69 6e 67 20  icular ordering 
2bcbd 6f 66 20 54 4b 5f 78 78 0a 20 20 20 20 20 20 2a  of TK_xx.      *
2bcbe 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 38  /.      const u8
2bcbf 20 61 4d 6f 76 65 4f 70 5b 5d 20 3d 20 7b 0a 20   aMoveOp[] = {. 
2bcc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f            /* TK_
2bcc1 47 54 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 47 74  GT */  OP_SeekGt
2bcc2 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,.           /* 
2bcc3 54 4b 5f 4c 45 20 2a 2f 20 20 4f 50 5f 53 65 65  TK_LE */  OP_See
2bcc4 6b 4c 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  kLe,.           
2bcc5 2f 2a 20 54 4b 5f 4c 54 20 2a 2f 20 20 4f 50 5f  /* TK_LT */  OP_
2bcc6 53 65 65 6b 4c 74 2c 0a 20 20 20 20 20 20 20 20  SeekLt,.        
2bcc7 20 20 20 2f 2a 20 54 4b 5f 47 45 20 2a 2f 20 20     /* TK_GE */  
2bcc8 4f 50 5f 53 65 65 6b 47 65 0a 20 20 20 20 20 20  OP_SeekGe.      
2bcc9 7d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  };.      assert(
2bcca 20 54 4b 5f 4c 45 3d 3d 54 4b 5f 47 54 2b 31 20   TK_LE==TK_GT+1 
2bccb 29 3b 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20  );      /* Make 
2bccc 73 75 72 65 20 74 68 65 20 6f 72 64 65 72 69 6e  sure the orderin
2bccd 67 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  g.. */.      ass
2bcce 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f 47  ert( TK_LT==TK_G
2bccf 54 2b 32 20 29 3b 20 20 20 20 20 20 2f 2a 20 20  T+2 );      /*  
2bcd0 2e 2e 2e 20 6f 66 20 74 68 65 20 54 4b 5f 78 78  ... of the TK_xx
2bcd1 20 76 61 6c 75 65 73 2e 2e 2e 20 2a 2f 0a 20 20   values... */.  
2bcd2 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47      assert( TK_G
2bcd3 45 3d 3d 54 4b 5f 47 54 2b 33 20 29 3b 20 20 20  E==TK_GT+3 );   
2bcd4 20 20 20 2f 2a 20 20 2e 2e 2e 20 69 73 20 63 6f     /*  ... is co
2bcd5 72 72 65 63 63 74 2e 20 2a 2f 0a 0a 20 20 20 20  rrecct. */..    
2bcd6 20 20 70 58 20 3d 20 70 53 74 61 72 74 2d 3e 70    pX = pStart->p
2bcd7 45 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65  Expr;.      asse
2bcd8 72 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 20  rt( pX!=0 );.   
2bcd9 20 20 20 61 73 73 65 72 74 28 20 70 53 74 61 72     assert( pStar
2bcda 74 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69  t->leftCursor==i
2bcdb 43 75 72 20 29 3b 0a 20 20 20 20 20 20 72 31 20  Cur );.      r1 
2bcdc 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
2bcdd 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 58  eTemp(pParse, pX
2bcde 2d 3e 70 52 69 67 68 74 2c 20 26 72 54 65 6d 70  ->pRight, &rTemp
2bcdf 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2bce0 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 61 4d  VdbeAddOp3(v, aM
2bce1 6f 76 65 4f 70 5b 70 58 2d 3e 6f 70 2d 54 4b 5f  oveOp[pX->op-TK_
2bce2 47 54 5d 2c 20 69 43 75 72 2c 20 61 64 64 72 42  GT], iCur, addrB
2bce3 72 6b 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 56  rk, r1);.      V
2bce4 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2bce5 70 6b 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  pk"));.      sql
2bce6 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2bce7 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
2bce8 73 65 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20  se, r1, 1);.    
2bce9 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2bcea 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2bceb 72 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 64 69  rTemp);.      di
2bcec 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
2bced 2c 20 70 53 74 61 72 74 29 3b 0a 20 20 20 20 7d  , pStart);.    }
2bcee 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2bcef 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2bcf0 20 62 52 65 76 20 3f 20 4f 50 5f 4c 61 73 74 20   bRev ? OP_Last 
2bcf1 3a 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 43 75  : OP_Rewind, iCu
2bcf2 72 2c 20 61 64 64 72 42 72 6b 29 3b 0a 20 20 20  r, addrBrk);.   
2bcf3 20 7d 0a 20 20 20 20 69 66 28 20 70 45 6e 64 20   }.    if( pEnd 
2bcf4 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
2bcf5 58 3b 0a 20 20 20 20 20 20 70 58 20 3d 20 70 45  X;.      pX = pE
2bcf6 6e 64 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  nd->pExpr;.     
2bcf7 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 20 29   assert( pX!=0 )
2bcf8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2bcf9 70 45 6e 64 2d 3e 6c 65 66 74 43 75 72 73 6f 72  pEnd->leftCursor
2bcfa 3d 3d 69 43 75 72 20 29 3b 0a 20 20 20 20 20 20  ==iCur );.      
2bcfb 6d 65 6d 45 6e 64 56 61 6c 75 65 20 3d 20 2b 2b  memEndValue = ++
2bcfc 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2bcfd 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2bcfe 6f 64 65 28 70 50 61 72 73 65 2c 20 70 58 2d 3e  ode(pParse, pX->
2bcff 70 52 69 67 68 74 2c 20 6d 65 6d 45 6e 64 56 61  pRight, memEndVa
2bd00 6c 75 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  lue);.      if( 
2bd01 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c  pX->op==TK_LT ||
2bd02 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 29   pX->op==TK_GT )
2bd03 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 4f 70  {.        testOp
2bd04 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 4c 65 20   = bRev ? OP_Le 
2bd05 3a 20 4f 50 5f 47 65 3b 0a 20 20 20 20 20 20 7d  : OP_Ge;.      }
2bd06 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65  else{.        te
2bd07 73 74 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50  stOp = bRev ? OP
2bd08 5f 4c 74 20 3a 20 4f 50 5f 47 74 3b 0a 20 20 20  _Lt : OP_Gt;.   
2bd09 20 20 20 7d 0a 20 20 20 20 20 20 64 69 73 61 62     }.      disab
2bd0a 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70  leTerm(pLevel, p
2bd0b 45 6e 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  End);.    }.    
2bd0c 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56  start = sqlite3V
2bd0d 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2bd0e 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f  );.    pLevel->o
2bd0f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72  p = bRev ? OP_Pr
2bd10 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20  ev : OP_Next;.  
2bd11 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69    pLevel->p1 = i
2bd12 43 75 72 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  Cur;.    pLevel-
2bd13 3e 70 32 20 3d 20 73 74 61 72 74 3b 0a 20 20 20  >p2 = start;.   
2bd14 20 70 4c 65 76 65 6c 2d 3e 70 35 20 3d 20 28 70   pLevel->p5 = (p
2bd15 53 74 61 72 74 3d 3d 30 20 26 26 20 70 45 6e 64  Start==0 && pEnd
2bd16 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 20 20 20 63  ==0) ?1:0;.    c
2bd17 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20 3d  odeRowSetEarly =
2bd18 20 72 65 67 52 6f 77 53 65 74 3e 3d 30 20 3f 20   regRowSet>=0 ? 
2bd19 77 68 65 72 65 52 6f 77 52 65 61 64 79 46 6f 72  whereRowReadyFor
2bd1a 4f 75 74 70 75 74 28 70 57 43 29 20 3a 20 30 3b  Output(pWC) : 0;
2bd1b 0a 20 20 20 20 69 66 28 20 63 6f 64 65 52 6f 77  .    if( codeRow
2bd1c 53 65 74 45 61 72 6c 79 20 7c 7c 20 74 65 73 74  SetEarly || test
2bd1d 4f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20  Op!=OP_Noop ){. 
2bd1e 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71       int r1 = sq
2bd1f 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2bd20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
2bd21 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2bd22 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43  (v, OP_Rowid, iC
2bd23 75 72 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 69  ur, r1);.      i
2bd24 66 28 20 74 65 73 74 4f 70 21 3d 4f 50 5f 4e 6f  f( testOp!=OP_No
2bd25 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  op ){.        sq
2bd26 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2bd27 76 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d 45 6e  v, testOp, memEn
2bd28 64 56 61 6c 75 65 2c 20 61 64 64 72 42 72 6b 2c  dValue, addrBrk,
2bd29 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r1);.        sq
2bd2a 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2bd2b 35 28 76 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  5(v, SQLITE_AFF_
2bd2c 4e 55 4d 45 52 49 43 20 7c 20 53 51 4c 49 54 45  NUMERIC | SQLITE
2bd2d 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2bd2e 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2bd2f 63 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20  codeRowSetEarly 
2bd30 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2bd31 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2bd32 4f 50 5f 52 6f 77 53 65 74 41 64 64 2c 20 72 65  OP_RowSetAdd, re
2bd33 67 52 6f 77 53 65 74 2c 20 72 31 29 3b 0a 20 20  gRowSet, r1);.  
2bd34 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2bd35 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2bd36 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
2bd37 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
2bd38 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2bd39 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 43  Flags & (WHERE_C
2bd3a 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52  OLUMN_RANGE|WHER
2bd3b 45 5f 43 4f 4c 55 4d 4e 5f 45 51 29 20 29 7b 0a  E_COLUMN_EQ) ){.
2bd3c 20 20 20 20 2f 2a 20 43 61 73 65 20 33 3a 20 41      /* Case 3: A
2bd3d 20 73 63 61 6e 20 75 73 69 6e 67 20 61 6e 20 69   scan using an i
2bd3e 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ndex..    **.   
2bd3f 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 65 20   **         The 
2bd40 57 48 45 52 45 20 63 6c 61 75 73 65 20 6d 61 79  WHERE clause may
2bd41 20 63 6f 6e 74 61 69 6e 20 7a 65 72 6f 20 6f 72   contain zero or
2bd42 20 6d 6f 72 65 20 65 71 75 61 6c 69 74 79 20 0a   more equality .
2bd43 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74      **         t
2bd44 65 72 6d 73 20 28 22 3d 3d 22 20 6f 72 20 22 49  erms ("==" or "I
2bd45 4e 22 20 6f 70 65 72 61 74 6f 72 73 29 20 74 68  N" operators) th
2bd46 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 20  at refer to the 
2bd47 4e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  N.    **        
2bd48 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
2bd49 6e 73 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  ns of the index.
2bd4a 20 49 74 20 6d 61 79 20 61 6c 73 6f 20 63 6f 6e   It may also con
2bd4b 74 61 69 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20  tain.    **     
2bd4c 20 20 20 20 69 6e 65 71 75 61 6c 69 74 79 20 63      inequality c
2bd4d 6f 6e 73 74 72 61 69 6e 74 73 20 28 3e 2c 20 3c  onstraints (>, <
2bd4e 2c 20 3e 3d 20 6f 72 20 3c 3d 29 20 6f 6e 20 74  , >= or <=) on t
2bd4f 68 65 20 69 6e 64 65 78 65 64 0a 20 20 20 20 2a  he indexed.    *
2bd50 2a 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e  *         column
2bd51 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 65 6c   that immediatel
2bd52 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 4e 20  y follows the N 
2bd53 65 71 75 61 6c 69 74 69 65 73 2e 20 4f 6e 6c 79  equalities. Only
2bd54 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20   .    **        
2bd55 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
2bd56 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 61 6e  column can be an
2bd57 20 69 6e 65 71 75 61 6c 69 74 79 20 2d 20 74 68   inequality - th
2bd58 65 20 72 65 73 74 20 6d 75 73 74 0a 20 20 20 20  e rest must.    
2bd59 2a 2a 20 20 20 20 20 20 20 20 20 75 73 65 20 74  **         use t
2bd5a 68 65 20 22 3d 3d 22 20 61 6e 64 20 22 49 4e 22  he "==" and "IN"
2bd5b 20 6f 70 65 72 61 74 6f 72 73 2e 20 46 6f 72 20   operators. For 
2bd5c 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20  example, if the 
2bd5d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2bd5e 69 6e 64 65 78 20 69 73 20 6f 6e 20 28 78 2c 79  index is on (x,y
2bd5f 2c 7a 29 2c 20 74 68 65 6e 20 74 68 65 20 66 6f  ,z), then the fo
2bd60 6c 6c 6f 77 69 6e 67 20 63 6c 61 75 73 65 73 20  llowing clauses 
2bd61 61 72 65 20 61 6c 6c 20 0a 20 20 20 20 2a 2a 20  are all .    ** 
2bd62 20 20 20 20 20 20 20 20 6f 70 74 69 6d 69 7a 65          optimize
2bd63 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d:.    **.    **
2bd64 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35 0a              x=5.
2bd65 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2bd66 20 20 78 3d 35 20 41 4e 44 20 79 3d 31 30 0a 20    x=5 AND y=10. 
2bd67 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
2bd68 20 78 3d 35 20 41 4e 44 20 79 3c 31 30 0a 20 20   x=5 AND y<10.  
2bd69 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
2bd6a 78 3d 35 20 41 4e 44 20 79 3e 35 20 41 4e 44 20  x=5 AND y>5 AND 
2bd6b 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20  y<10.    **     
2bd6c 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79         x=5 AND y
2bd6d 3d 35 20 41 4e 44 20 7a 3c 3d 31 30 0a 20 20 20  =5 AND z<=10.   
2bd6e 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
2bd6f 20 20 20 54 68 65 20 7a 3c 31 30 20 74 65 72 6d     The z<10 term
2bd70 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2bd71 67 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64  g cannot be used
2bd72 2c 20 6f 6e 6c 79 0a 20 20 20 20 2a 2a 20 20 20  , only.    **   
2bd73 20 20 20 20 20 20 74 68 65 20 78 3d 35 20 74 65        the x=5 te
2bd74 72 6d 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  rm:.    **.    *
2bd75 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35  *            x=5
2bd76 20 41 4e 44 20 7a 3c 31 30 0a 20 20 20 20 2a 2a   AND z<10.    **
2bd77 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2bd78 4e 20 6d 61 79 20 62 65 20 7a 65 72 6f 20 69 66  N may be zero if
2bd79 20 74 68 65 72 65 20 61 72 65 20 69 6e 65 71 75   there are inequ
2bd7a 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2bd7b 73 2e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  s..    **       
2bd7c 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e    If there are n
2bd7d 6f 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  o inequality con
2bd7e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 6e 20 4e  straints, then N
2bd7f 20 69 73 20 61 74 0a 20 20 20 20 2a 2a 20 20 20   is at.    **   
2bd80 20 20 20 20 20 20 6c 65 61 73 74 20 6f 6e 65 2e        least one.
2bd81 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2bd82 20 20 20 20 20 20 20 54 68 69 73 20 63 61 73 65         This case
2bd83 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 77 68   is also used wh
2bd84 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  en there are no 
2bd85 57 48 45 52 45 20 63 6c 61 75 73 65 0a 20 20 20  WHERE clause.   
2bd86 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e 73   **         cons
2bd87 74 72 61 69 6e 74 73 20 62 75 74 20 61 6e 20 69  traints but an i
2bd88 6e 64 65 78 20 69 73 20 73 65 6c 65 63 74 65 64  ndex is selected
2bd89 20 61 6e 79 77 61 79 2c 20 69 6e 20 6f 72 64 65   anyway, in orde
2bd8a 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  r.    **        
2bd8b 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20 6f 75   to force the ou
2bd8c 74 70 75 74 20 6f 72 64 65 72 20 74 6f 20 63 6f  tput order to co
2bd8d 6e 66 6f 72 6d 20 74 6f 20 61 6e 20 4f 52 44 45  nform to an ORDE
2bd8e 52 20 42 59 2e 0a 20 20 20 20 2a 2f 20 20 0a 20  R BY..    */  . 
2bd8f 20 20 20 69 6e 74 20 61 53 74 61 72 74 4f 70 5b     int aStartOp[
2bd90 5d 20 3d 20 7b 0a 20 20 20 20 20 20 30 2c 0a 20  ] = {.      0,. 
2bd91 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 4f 50       0,.      OP
2bd92 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 20 20 20  _Rewind,        
2bd93 20 20 20 2f 2a 20 32 3a 20 28 21 73 74 61 72 74     /* 2: (!start
2bd94 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20  _constraints && 
2bd95 73 74 61 72 74 45 71 20 26 26 20 20 21 62 52 65  startEq &&  !bRe
2bd96 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 4c  v) */.      OP_L
2bd97 61 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ast,            
2bd98 20 2f 2a 20 33 3a 20 28 21 73 74 61 72 74 5f 63   /* 3: (!start_c
2bd99 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20 73 74  onstraints && st
2bd9a 61 72 74 45 71 20 26 26 20 20 20 62 52 65 76 29  artEq &&   bRev)
2bd9b 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65   */.      OP_See
2bd9c 6b 47 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  kGt,           /
2bd9d 2a 20 34 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73  * 4: (start_cons
2bd9e 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 74 61  traints  && !sta
2bd9f 72 74 45 71 20 26 26 20 21 62 52 65 76 29 20 2a  rtEq && !bRev) *
2bda0 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 4c  /.      OP_SeekL
2bda1 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t,           /* 
2bda2 35 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72  5: (start_constr
2bda3 61 69 6e 74 73 20 20 26 26 20 21 73 74 61 72 74  aints  && !start
2bda4 45 71 20 26 26 20 20 62 52 65 76 29 20 2a 2f 0a  Eq &&  bRev) */.
2bda5 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 47 65 2c        OP_SeekGe,
2bda6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 36 3a             /* 6:
2bda7 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69   (start_constrai
2bda8 6e 74 73 20 20 26 26 20 20 73 74 61 72 74 45 71  nts  &&  startEq
2bda9 20 26 26 20 21 62 52 65 76 29 20 2a 2f 0a 20 20   && !bRev) */.  
2bdaa 20 20 20 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20      OP_SeekLe   
2bdab 20 20 20 20 20 20 20 20 20 2f 2a 20 37 3a 20 28           /* 7: (
2bdac 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74  start_constraint
2bdad 73 20 20 26 26 20 20 73 74 61 72 74 45 71 20 26  s  &&  startEq &
2bdae 26 20 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20  &  bRev) */.    
2bdaf 7d 3b 0a 20 20 20 20 69 6e 74 20 61 45 6e 64 4f  };.    int aEndO
2bdb0 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 4f 50  p[] = {.      OP
2bdb1 5f 4e 6f 6f 70 2c 20 20 20 20 20 20 20 20 20 20  _Noop,          
2bdb2 20 20 20 2f 2a 20 30 3a 20 28 21 65 6e 64 5f 63     /* 0: (!end_c
2bdb3 6f 6e 73 74 72 61 69 6e 74 73 29 20 2a 2f 0a 20  onstraints) */. 
2bdb4 20 20 20 20 20 4f 50 5f 49 64 78 47 45 2c 20 20       OP_IdxGE,  
2bdb5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20            /* 1: 
2bdb6 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73  (end_constraints
2bdb7 20 26 26 20 21 62 52 65 76 29 20 2a 2f 0a 20 20   && !bRev) */.  
2bdb8 20 20 20 20 4f 50 5f 49 64 78 4c 54 20 20 20 20      OP_IdxLT    
2bdb9 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 28           /* 2: (
2bdba 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20  end_constraints 
2bdbb 26 26 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20  && bRev) */.    
2bdbc 7d 3b 0a 20 20 20 20 69 6e 74 20 6e 45 71 20 3d  };.    int nEq =
2bdbd 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45   pLevel->plan.nE
2bdbe 71 3b 0a 20 20 20 20 69 6e 74 20 69 73 4d 69 6e  q;.    int isMin
2bdbf 51 75 65 72 79 20 3d 20 30 3b 20 20 20 20 20 20  Query = 0;      
2bdc0 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
2bdc1 73 20 61 6e 20 6f 70 74 69 6d 69 7a 65 64 20 53  s an optimized S
2bdc2 45 4c 45 43 54 20 6d 69 6e 28 78 29 2e 2e 20 2a  ELECT min(x).. *
2bdc3 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 42 61 73  /.    int regBas
2bdc4 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
2bdc5 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73     /* Base regis
2bdc6 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6e 73  ter holding cons
2bdc7 74 72 61 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f  traint values */
2bdc8 0a 20 20 20 20 69 6e 74 20 72 31 3b 20 20 20 20  .    int r1;    
2bdc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdca 20 20 2f 2a 20 54 65 6d 70 20 72 65 67 69 73 74    /* Temp regist
2bdcb 65 72 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 54  er */.    WhereT
2bdcc 65 72 6d 20 2a 70 52 61 6e 67 65 53 74 61 72 74  erm *pRangeStart
2bdcd 20 3d 20 30 3b 20 20 2f 2a 20 49 6e 65 71 75 61   = 0;  /* Inequa
2bdce 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20  lity constraint 
2bdcf 61 74 20 72 61 6e 67 65 20 73 74 61 72 74 20 2a  at range start *
2bdd0 2f 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20  /.    WhereTerm 
2bdd1 2a 70 52 61 6e 67 65 45 6e 64 20 3d 20 30 3b 20  *pRangeEnd = 0; 
2bdd2 20 20 20 2f 2a 20 49 6e 65 71 75 61 6c 69 74 79     /* Inequality
2bdd3 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 72   constraint at r
2bdd4 61 6e 67 65 20 65 6e 64 20 2a 2f 0a 20 20 20 20  ange end */.    
2bdd5 69 6e 74 20 73 74 61 72 74 45 71 3b 20 20 20 20  int startEq;    
2bdd6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bdd7 54 72 75 65 20 69 66 20 72 61 6e 67 65 20 73 74  True if range st
2bdd8 61 72 74 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20  art uses ==, >= 
2bdd9 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e 74  or <= */.    int
2bdda 20 65 6e 64 45 71 3b 20 20 20 20 20 20 20 20 20   endEq;         
2bddb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
2bddc 65 20 69 66 20 72 61 6e 67 65 20 65 6e 64 20 75  e if range end u
2bddd 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d  ses ==, >= or <=
2bdde 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 74 61 72   */.    int star
2bddf 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3b 20 20  t_constraints;  
2bde0 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66       /* Start of
2bde1 20 72 61 6e 67 65 20 69 73 20 63 6f 6e 73 74 72   range is constr
2bde2 61 69 6e 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74  ained */.    int
2bde3 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20   nConstraint;   
2bde4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2bde5 62 65 72 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e  ber of constrain
2bde6 74 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 49  t terms */.    I
2bde7 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
2bde8 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78      /* The index
2bde9 20 77 65 20 77 69 6c 6c 20 62 65 20 75 73 69 6e   we will be usin
2bdea 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 49 64  g */.    int iId
2bdeb 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 2f 2a  xCur;         /*
2bdec 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
2bded 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 2a   for the index *
2bdee 2f 0a 20 20 20 20 69 6e 74 20 6e 45 78 74 72 61  /.    int nExtra
2bdef 52 65 67 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75  Reg = 0;   /* Nu
2bdf0 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 72 65  mber of extra re
2bdf1 67 69 73 74 65 72 73 20 6e 65 65 64 65 64 20 2a  gisters needed *
2bdf2 2f 0a 20 20 20 20 69 6e 74 20 6f 70 3b 20 20 20  /.    int op;   
2bdf3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2bdf4 73 74 72 75 63 74 69 6f 6e 20 6f 70 63 6f 64 65  struction opcode
2bdf5 20 2a 2f 0a 0a 20 20 20 20 70 49 64 78 20 3d 20   */..    pIdx = 
2bdf6 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
2bdf7 49 64 78 3b 0a 20 20 20 20 69 49 64 78 43 75 72  Idx;.    iIdxCur
2bdf8 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43   = pLevel->iIdxC
2bdf9 75 72 3b 0a 20 20 20 20 6b 20 3d 20 70 49 64 78  ur;.    k = pIdx
2bdfa 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b  ->aiColumn[nEq];
2bdfb 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 66       /* Column f
2bdfc 6f 72 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  or inequality co
2bdfd 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 0a 20 20  nstraints */..  
2bdfe 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6c 6f 6f    /* If this loo
2bdff 70 20 73 61 74 69 73 66 69 65 73 20 61 20 73 6f  p satisfies a so
2be00 72 74 20 6f 72 64 65 72 20 28 70 4f 72 64 65 72  rt order (pOrder
2be01 42 79 29 20 72 65 71 75 65 73 74 20 74 68 61 74  By) request that
2be02 20 0a 20 20 20 20 2a 2a 20 77 61 73 20 70 61 73   .    ** was pas
2be03 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63  sed to this func
2be04 74 69 6f 6e 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  tion to implemen
2be05 74 20 61 20 22 53 45 4c 45 43 54 20 6d 69 6e 28  t a "SELECT min(
2be06 78 29 20 2e 2e 2e 22 20 0a 20 20 20 20 2a 2a 20  x) ..." .    ** 
2be07 71 75 65 72 79 2c 20 74 68 65 6e 20 74 68 65 20  query, then the 
2be08 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 6e 6c 79  caller will only
2be09 20 61 6c 6c 6f 77 20 74 68 65 20 6c 6f 6f 70 20   allow the loop 
2be0a 74 6f 20 72 75 6e 20 66 6f 72 0a 20 20 20 20 2a  to run for.    *
2be0b 2a 20 61 20 73 69 6e 67 6c 65 20 69 74 65 72 61  * a single itera
2be0c 74 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73  tion. This means
2be0d 20 74 68 61 74 20 74 68 65 20 66 69 72 73 74 20   that the first 
2be0e 72 6f 77 20 72 65 74 75 72 6e 65 64 0a 20 20 20  row returned.   
2be0f 20 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 68   ** should not h
2be10 61 76 65 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65  ave a NULL value
2be11 20 73 74 6f 72 65 64 20 69 6e 20 27 78 27 2e 20   stored in 'x'. 
2be12 49 66 20 63 6f 6c 75 6d 6e 20 27 78 27 20 69 73  If column 'x' is
2be13 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 72 73  .    ** the firs
2be14 74 20 6f 6e 65 20 61 66 74 65 72 20 74 68 65 20  t one after the 
2be15 6e 45 71 20 65 71 75 61 6c 69 74 79 20 63 6f 6e  nEq equality con
2be16 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20  straints in the 
2be17 69 6e 64 65 78 2c 0a 20 20 20 20 2a 2a 20 74 68  index,.    ** th
2be18 69 73 20 72 65 71 75 69 72 65 73 20 73 6f 6d 65  is requires some
2be19 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e   special handlin
2be1a 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  g..    */.    if
2be1b 28 20 28 77 63 74 72 6c 46 6c 61 67 73 26 57 48  ( (wctrlFlags&WH
2be1c 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 29  ERE_ORDERBY_MIN)
2be1d 21 3d 30 0a 20 20 20 20 20 26 26 20 28 70 4c 65  !=0.     && (pLe
2be1e 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2be1f 73 26 57 48 45 52 45 5f 4f 52 44 45 52 42 59 29  s&WHERE_ORDERBY)
2be20 0a 20 20 20 20 20 26 26 20 28 70 49 64 78 2d 3e  .     && (pIdx->
2be21 6e 43 6f 6c 75 6d 6e 3e 6e 45 71 29 0a 20 20 20  nColumn>nEq).   
2be22 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 73 73   ){.      /* ass
2be23 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e  ert( pOrderBy->n
2be24 45 78 70 72 3d 3d 31 20 29 3b 20 2a 2f 0a 20 20  Expr==1 ); */.  
2be25 20 20 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70      /* assert( p
2be26 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 70 45  OrderBy->a[0].pE
2be27 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 49  xpr->iColumn==pI
2be28 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71  dx->aiColumn[nEq
2be29 5d 20 29 3b 20 2a 2f 0a 20 20 20 20 20 20 69 73  ] ); */.      is
2be2a 4d 69 6e 51 75 65 72 79 20 3d 20 31 3b 0a 20 20  MinQuery = 1;.  
2be2b 20 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d 20      nExtraReg = 
2be2c 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  1;.    }..    /*
2be2d 20 46 69 6e 64 20 61 6e 79 20 69 6e 65 71 75 61   Find any inequa
2be2e 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20  lity constraint 
2be2f 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73 74  terms for the st
2be30 61 72 74 20 61 6e 64 20 65 6e 64 20 0a 20 20 20  art and end .   
2be31 20 2a 2a 20 6f 66 20 74 68 65 20 72 61 6e 67 65   ** of the range
2be32 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  . .    */.    if
2be33 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
2be34 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 54  sFlags & WHERE_T
2be35 4f 50 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20  OP_LIMIT ){.    
2be36 20 20 70 52 61 6e 67 65 45 6e 64 20 3d 20 66 69    pRangeEnd = fi
2be37 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2be38 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28  , k, notReady, (
2be39 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 29 2c 20 70 49  WO_LT|WO_LE), pI
2be3a 64 78 29 3b 0a 20 20 20 20 20 20 6e 45 78 74 72  dx);.      nExtr
2be3b 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  aReg = 1;.    }.
2be3c 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2be3d 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2be3e 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 29  HERE_BTM_LIMIT )
2be3f 7b 0a 20 20 20 20 20 20 70 52 61 6e 67 65 53 74  {.      pRangeSt
2be40 61 72 74 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  art = findTerm(p
2be41 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74  WC, iCur, k, not
2be42 52 65 61 64 79 2c 20 28 57 4f 5f 47 54 7c 57 4f  Ready, (WO_GT|WO
2be43 5f 47 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20  _GE), pIdx);.   
2be44 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d 20 31     nExtraReg = 1
2be45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2be46 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
2be47 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20 63 6f   evaluate all co
2be48 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 75  nstraint terms u
2be49 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e 0a 20 20  sing == or IN.  
2be4a 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74    ** and store t
2be4b 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68 6f  he values of tho
2be4c 73 65 20 74 65 72 6d 73 20 69 6e 20 61 6e 20 61  se terms in an a
2be4d 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65 72  rray of register
2be4e 73 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e  s.    ** startin
2be4f 67 20 61 74 20 72 65 67 42 61 73 65 2e 0a 20 20  g at regBase..  
2be50 20 20 2a 2f 0a 20 20 20 20 72 65 67 42 61 73 65    */.    regBase
2be51 20 3d 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c 69   = codeAllEquali
2be52 74 79 54 65 72 6d 73 28 70 50 61 72 73 65 2c 20  tyTerms(pParse, 
2be53 70 4c 65 76 65 6c 2c 20 70 57 43 2c 20 6e 6f 74  pLevel, pWC, not
2be54 52 65 61 64 79 2c 20 6e 45 78 74 72 61 52 65 67  Ready, nExtraReg
2be55 29 3b 0a 20 20 20 20 61 64 64 72 4e 78 74 20 3d  );.    addrNxt =
2be56 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74   pLevel->addrNxt
2be57 3b 0a 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  ;...    /* If we
2be58 20 61 72 65 20 64 6f 69 6e 67 20 61 20 72 65 76   are doing a rev
2be59 65 72 73 65 20 6f 72 64 65 72 20 73 63 61 6e 20  erse order scan 
2be5a 6f 6e 20 61 6e 20 61 73 63 65 6e 64 69 6e 67 20  on an ascending 
2be5b 69 6e 64 65 78 2c 20 6f 72 0a 20 20 20 20 2a 2a  index, or.    **
2be5c 20 61 20 66 6f 72 77 61 72 64 20 6f 72 64 65 72   a forward order
2be5d 20 73 63 61 6e 20 6f 6e 20 61 20 64 65 73 63 65   scan on a desce
2be5e 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20 69 6e 74  nding index, int
2be5f 65 72 63 68 61 6e 67 65 20 74 68 65 20 0a 20 20  erchange the .  
2be60 20 20 2a 2a 20 73 74 61 72 74 20 61 6e 64 20 65    ** start and e
2be61 6e 64 20 74 65 72 6d 73 20 28 70 52 61 6e 67 65  nd terms (pRange
2be62 53 74 61 72 74 20 61 6e 64 20 70 52 61 6e 67 65  Start and pRange
2be63 45 6e 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  End)..    */.   
2be64 20 69 66 28 20 62 52 65 76 3d 3d 28 70 49 64 78   if( bRev==(pIdx
2be65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 45 71  ->aSortOrder[nEq
2be66 5d 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43  ]==SQLITE_SO_ASC
2be67 29 20 29 7b 0a 20 20 20 20 20 20 53 57 41 50 28  ) ){.      SWAP(
2be68 57 68 65 72 65 54 65 72 6d 20 2a 2c 20 70 52 61  WhereTerm *, pRa
2be69 6e 67 65 45 6e 64 2c 20 70 52 61 6e 67 65 53 74  ngeEnd, pRangeSt
2be6a 61 72 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  art);.    }..   
2be6b 20 74 65 73 74 63 61 73 65 28 20 70 52 61 6e 67   testcase( pRang
2be6c 65 53 74 61 72 74 20 26 26 20 70 52 61 6e 67 65  eStart && pRange
2be6d 53 74 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72  Start->eOperator
2be6e 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20 20 20   & WO_LE );.    
2be6f 74 65 73 74 63 61 73 65 28 20 70 52 61 6e 67 65  testcase( pRange
2be70 53 74 61 72 74 20 26 26 20 70 52 61 6e 67 65 53  Start && pRangeS
2be71 74 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20  tart->eOperator 
2be72 26 20 57 4f 5f 47 45 20 29 3b 0a 20 20 20 20 74  & WO_GE );.    t
2be73 65 73 74 63 61 73 65 28 20 70 52 61 6e 67 65 45  estcase( pRangeE
2be74 6e 64 20 26 26 20 70 52 61 6e 67 65 45 6e 64 2d  nd && pRangeEnd-
2be75 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
2be76 4c 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  LE );.    testca
2be77 73 65 28 20 70 52 61 6e 67 65 45 6e 64 20 26 26  se( pRangeEnd &&
2be78 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65   pRangeEnd->eOpe
2be79 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20 29 3b  rator & WO_GE );
2be7a 0a 20 20 20 20 73 74 61 72 74 45 71 20 3d 20 21  .    startEq = !
2be7b 70 52 61 6e 67 65 53 74 61 72 74 20 7c 7c 20 70  pRangeStart || p
2be7c 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65  RangeStart->eOpe
2be7d 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57  rator & (WO_LE|W
2be7e 4f 5f 47 45 29 3b 0a 20 20 20 20 65 6e 64 45 71  O_GE);.    endEq
2be7f 20 3d 20 20 20 21 70 52 61 6e 67 65 45 6e 64 20   =   !pRangeEnd 
2be80 7c 7c 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f  || pRangeEnd->eO
2be81 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c 45  perator & (WO_LE
2be82 7c 57 4f 5f 47 45 29 3b 0a 20 20 20 20 73 74 61  |WO_GE);.    sta
2be83 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d  rt_constraints =
2be84 20 70 52 61 6e 67 65 53 74 61 72 74 20 7c 7c 20   pRangeStart || 
2be85 6e 45 71 3e 30 3b 0a 0a 20 20 20 20 2f 2a 20 53  nEq>0;..    /* S
2be86 65 65 6b 20 74 68 65 20 69 6e 64 65 78 20 63 75  eek the index cu
2be87 72 73 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72  rsor to the star
2be88 74 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2e 20  t of the range. 
2be89 2a 2f 0a 20 20 20 20 6e 43 6f 6e 73 74 72 61 69  */.    nConstrai
2be8a 6e 74 20 3d 20 6e 45 71 3b 0a 20 20 20 20 69 66  nt = nEq;.    if
2be8b 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 29 7b  ( pRangeStart ){
2be8c 0a 20 20 20 20 20 20 69 6e 74 20 64 63 63 20 3d  .      int dcc =
2be8d 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
2be8e 43 6f 6c 43 61 63 68 65 3b 0a 20 20 20 20 20 20  ColCache;.      
2be8f 69 66 28 20 70 52 61 6e 67 65 45 6e 64 20 29 7b  if( pRangeEnd ){
2be90 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
2be91 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65  >disableColCache
2be92 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
2be93 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2be94 65 28 70 50 61 72 73 65 2c 20 70 52 61 6e 67 65  e(pParse, pRange
2be95 53 74 61 72 74 2d 3e 70 45 78 70 72 2d 3e 70 52  Start->pExpr->pR
2be96 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b 6e 45  ight, regBase+nE
2be97 71 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  q);.      pParse
2be98 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
2be99 65 20 3d 20 64 63 63 3b 0a 20 20 20 20 20 20 73  e = dcc;.      s
2be9a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2be9b 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72  (v, OP_IsNull, r
2be9c 65 67 42 61 73 65 2b 6e 45 71 2c 20 61 64 64 72  egBase+nEq, addr
2be9d 4e 78 74 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e  Nxt);.      nCon
2be9e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d  straint++;.    }
2be9f 65 6c 73 65 20 69 66 28 20 69 73 4d 69 6e 51 75  else if( isMinQu
2bea0 65 72 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ery ){.      sql
2bea1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2bea2 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65  , OP_Null, 0, re
2bea3 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20  gBase+nEq);.    
2bea4 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b    nConstraint++;
2bea5 0a 20 20 20 20 20 20 73 74 61 72 74 45 71 20 3d  .      startEq =
2bea6 20 30 3b 0a 20 20 20 20 20 20 73 74 61 72 74 5f   0;.      start_
2bea7 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20 31 3b  constraints = 1;
2bea8 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f 64 65 41  .    }.    codeA
2bea9 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61  pplyAffinity(pPa
2beaa 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43  rse, regBase, nC
2beab 6f 6e 73 74 72 61 69 6e 74 2c 20 70 49 64 78 29  onstraint, pIdx)
2beac 3b 0a 20 20 20 20 6f 70 20 3d 20 61 53 74 61 72  ;.    op = aStar
2bead 74 4f 70 5b 28 73 74 61 72 74 5f 63 6f 6e 73 74  tOp[(start_const
2beae 72 61 69 6e 74 73 3c 3c 32 29 20 2b 20 28 73 74  raints<<2) + (st
2beaf 61 72 74 45 71 3c 3c 31 29 20 2b 20 62 52 65 76  artEq<<1) + bRev
2beb0 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f  ];.    assert( o
2beb1 70 21 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74  p!=0 );.    test
2beb2 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 52 65 77  case( op==OP_Rew
2beb3 69 6e 64 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ind );.    testc
2beb4 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4c 61 73 74  ase( op==OP_Last
2beb5 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2beb6 28 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47 74 20  ( op==OP_SeekGt 
2beb7 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2beb8 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29   op==OP_SeekGe )
2beb9 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2beba 6f 70 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b  op==OP_SeekLe );
2bebb 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f  .    testcase( o
2bebc 70 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 29 3b 0a  p==OP_SeekLt );.
2bebd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2bebe 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 49 64  ddOp4(v, op, iId
2bebf 78 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72  xCur, addrNxt, r
2bec0 65 67 42 61 73 65 2c 20 0a 20 20 20 20 20 20 20  egBase, .       
2bec1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
2bec2 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
2bec3 28 6e 43 6f 6e 73 74 72 61 69 6e 74 29 2c 20 50  (nConstraint), P
2bec4 34 5f 49 4e 54 33 32 29 3b 0a 0a 20 20 20 20 2f  4_INT32);..    /
2bec5 2a 20 4c 6f 61 64 20 74 68 65 20 76 61 6c 75 65  * Load the value
2bec6 20 66 6f 72 20 74 68 65 20 69 6e 65 71 75 61 6c   for the inequal
2bec7 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61  ity constraint a
2bec8 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
2bec9 0a 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 28 69  .    ** range (i
2beca 66 20 61 6e 79 29 2e 0a 20 20 20 20 2a 2f 0a 20  f any)..    */. 
2becb 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d     nConstraint =
2becc 20 6e 45 71 3b 0a 20 20 20 20 69 66 28 20 70 52   nEq;.    if( pR
2becd 61 6e 67 65 45 6e 64 20 29 7b 0a 20 20 20 20 20  angeEnd ){.     
2bece 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2becf 28 70 50 61 72 73 65 2c 20 70 52 61 6e 67 65 45  (pParse, pRangeE
2bed0 6e 64 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  nd->pExpr->pRigh
2bed1 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b  t, regBase+nEq);
2bed2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2bed3 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2bed4 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b 6e  sNull, regBase+n
2bed5 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20 20  Eq, addrNxt);.  
2bed6 20 20 20 20 63 6f 64 65 41 70 70 6c 79 41 66 66      codeApplyAff
2bed7 69 6e 69 74 79 28 70 50 61 72 73 65 2c 20 72 65  inity(pParse, re
2bed8 67 42 61 73 65 2c 20 6e 45 71 2b 31 2c 20 70 49  gBase, nEq+1, pI
2bed9 64 78 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e 73  dx);.      nCons
2beda 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a  traint++;.    }.
2bedb 0a 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74  .    /* Top of t
2bedc 68 65 20 6c 6f 6f 70 20 62 6f 64 79 20 2a 2f 0a  he loop body */.
2bedd 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d      pLevel->p2 =
2bede 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2bedf 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 20  entAddr(v);..   
2bee0 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65   /* Check if the
2bee1 20 69 6e 64 65 78 20 63 75 72 73 6f 72 20 69 73   index cursor is
2bee2 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
2bee3 20 74 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a 20   the range. */. 
2bee4 20 20 20 6f 70 20 3d 20 61 45 6e 64 4f 70 5b 28     op = aEndOp[(
2bee5 70 52 61 6e 67 65 45 6e 64 20 7c 7c 20 6e 45 71  pRangeEnd || nEq
2bee6 29 20 2a 20 28 31 20 2b 20 62 52 65 76 29 5d 3b  ) * (1 + bRev)];
2bee7 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f  .    testcase( o
2bee8 70 3d 3d 4f 50 5f 4e 6f 6f 70 20 29 3b 0a 20 20  p==OP_Noop );.  
2bee9 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2beea 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20  OP_IdxGE );.    
2beeb 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
2beec 5f 49 64 78 4c 54 20 29 3b 0a 20 20 20 20 69 66  _IdxLT );.    if
2beed 28 20 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b  ( op!=OP_Noop ){
2beee 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2beef 62 65 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20  beAddOp4(v, op, 
2bef0 69 49 64 78 43 75 72 2c 20 61 64 64 72 4e 78 74  iIdxCur, addrNxt
2bef1 2c 20 72 65 67 42 61 73 65 2c 0a 20 20 20 20 20  , regBase,.     
2bef2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bef3 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f     SQLITE_INT_TO
2bef4 5f 50 54 52 28 6e 43 6f 6e 73 74 72 61 69 6e 74  _PTR(nConstraint
2bef5 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  ), P4_INT32);.  
2bef6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2bef7 68 61 6e 67 65 50 35 28 76 2c 20 65 6e 64 45 71  hangeP5(v, endEq
2bef8 21 3d 62 52 65 76 20 3f 31 3a 30 29 3b 0a 20 20  !=bRev ?1:0);.  
2bef9 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
2befa 68 65 72 65 20 61 72 65 20 69 6e 65 71 75 61 6c  here are inequal
2befb 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c  ity constraints,
2befc 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   check that the 
2befd 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20  value.    ** of 
2befe 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  the table column
2beff 20 74 68 61 74 20 74 68 65 20 69 6e 65 71 75 61   that the inequa
2bf00 6c 69 74 79 20 63 6f 6e 74 72 61 69 6e 73 20 69  lity contrains i
2bf01 73 20 6e 6f 74 20 4e 55 4c 4c 2e 0a 20 20 20 20  s not NULL..    
2bf02 2a 2a 20 49 66 20 69 74 20 69 73 2c 20 6a 75 6d  ** If it is, jum
2bf03 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74  p to the next it
2bf04 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  eration of the l
2bf05 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  oop..    */.    
2bf06 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
2bf07 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
2bf08 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c      testcase( pL
2bf09 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2bf0a 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c  gs & WHERE_BTM_L
2bf0b 49 4d 49 54 20 29 3b 0a 20 20 20 20 74 65 73 74  IMIT );.    test
2bf0c 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c  case( pLevel->pl
2bf0d 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2bf0e 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 3b 0a  RE_TOP_LIMIT );.
2bf0f 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2bf10 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 28  plan.wsFlags & (
2bf11 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 7c  WHERE_BTM_LIMIT|
2bf12 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 29  WHERE_TOP_LIMIT)
2bf13 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2bf14 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2bf15 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75  P_Column, iIdxCu
2bf16 72 2c 20 6e 45 71 2c 20 72 31 29 3b 0a 20 20 20  r, nEq, r1);.   
2bf17 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2bf18 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c  dOp2(v, OP_IsNul
2bf19 6c 2c 20 72 31 2c 20 61 64 64 72 43 6f 6e 74 29  l, r1, addrCont)
2bf1a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2bf1b 53 65 65 6b 20 74 68 65 20 74 61 62 6c 65 20 63  Seek the table c
2bf1c 75 72 73 6f 72 2c 20 69 66 20 72 65 71 75 69 72  ursor, if requir
2bf1d 65 64 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c  ed */.    disabl
2bf1e 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52  eTerm(pLevel, pR
2bf1f 61 6e 67 65 53 74 61 72 74 29 3b 0a 20 20 20 20  angeStart);.    
2bf20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
2bf21 65 6c 2c 20 70 52 61 6e 67 65 45 6e 64 29 3b 0a  el, pRangeEnd);.
2bf22 20 20 20 20 63 6f 64 65 52 6f 77 53 65 74 45 61      codeRowSetEa
2bf23 72 6c 79 20 3d 20 72 65 67 52 6f 77 53 65 74 3e  rly = regRowSet>
2bf24 3d 30 20 3f 20 77 68 65 72 65 52 6f 77 52 65 61  =0 ? whereRowRea
2bf25 64 79 46 6f 72 4f 75 74 70 75 74 28 70 57 43 29  dyForOutput(pWC)
2bf26 20 3a 20 30 3b 0a 20 20 20 20 69 66 28 20 21 6f   : 0;.    if( !o
2bf27 6d 69 74 54 61 62 6c 65 20 7c 7c 20 63 6f 64 65  mitTable || code
2bf28 52 6f 77 53 65 74 45 61 72 6c 79 20 29 7b 0a 20  RowSetEarly ){. 
2bf29 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2bf2a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
2bf2b 52 6f 77 69 64 2c 20 69 49 64 78 43 75 72 2c 20  Rowid, iIdxCur, 
2bf2c 72 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63  r1);.      if( c
2bf2d 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79 20 29  odeRowSetEarly )
2bf2e 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2bf2f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2bf30 50 5f 52 6f 77 53 65 74 41 64 64 2c 20 72 65 67  P_RowSetAdd, reg
2bf31 52 6f 77 53 65 74 2c 20 72 31 29 3b 0a 20 20 20  RowSet, r1);.   
2bf32 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2bf33 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2bf34 4f 70 32 28 76 2c 20 4f 50 5f 53 65 65 6b 2c 20  Op2(v, OP_Seek, 
2bf35 69 43 75 72 2c 20 72 31 29 3b 20 20 2f 2a 20 44  iCur, r1);  /* D
2bf36 65 66 65 72 72 65 64 20 73 65 65 6b 20 2a 2f 0a  eferred seek */.
2bf37 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2bf38 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2bf39 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2bf3a 72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 63  r1);..    /* Rec
2bf3b 6f 72 64 20 74 68 65 20 69 6e 73 74 72 75 63 74  ord the instruct
2bf3c 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65 72 6d  ion used to term
2bf3d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20  inate the loop. 
2bf3e 44 69 73 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20  Disable .    ** 
2bf3f 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
2bf40 6d 73 20 6d 61 64 65 20 72 65 64 75 6e 64 61 6e  ms made redundan
2bf41 74 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 72  t by the index r
2bf42 61 6e 67 65 20 73 63 61 6e 2e 0a 20 20 20 20 2a  ange scan..    *
2bf43 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70  /.    pLevel->op
2bf44 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72 65   = bRev ? OP_Pre
2bf45 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20  v : OP_Next;.   
2bf46 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 49   pLevel->p1 = iI
2bf47 64 78 43 75 72 3b 0a 20 20 7d 65 6c 73 65 0a 0a  dxCur;.  }else..
2bf48 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2bf49 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54  MIT_OR_OPTIMIZAT
2bf4a 49 4f 4e 0a 20 20 69 66 28 20 70 4c 65 76 65 6c  ION.  if( pLevel
2bf4b 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2bf4c 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20   WHERE_MULTI_OR 
2bf4d 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 34  ){.    /* Case 4
2bf4e 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 73  :  Two or more s
2bf4f 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65 78 65  eparately indexe
2bf50 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65  d terms connecte
2bf51 64 20 62 79 20 4f 52 0a 20 20 20 20 2a 2a 0a 20  d by OR.    **. 
2bf52 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20     ** Example:. 
2bf53 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 43     **.    **   C
2bf54 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
2bf55 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 2a 2a 20  ,b,c,d);.    ** 
2bf56 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69    CREATE INDEX i
2bf57 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 20 20 20  1 ON t1(a);.    
2bf58 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44 45  **   CREATE INDE
2bf59 58 20 69 32 20 4f 4e 20 74 31 28 62 29 3b 0a 20  X i2 ON t1(b);. 
2bf5a 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49     **   CREATE I
2bf5b 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 28 63 29  NDEX i3 ON t1(c)
2bf5c 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ;.    **.    ** 
2bf5d 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2bf5e 74 31 20 57 48 45 52 45 20 61 3d 35 20 4f 52 20  t1 WHERE a=5 OR 
2bf5f 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 41 4e 44  b=7 OR (c=11 AND
2bf60 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a 0a 20 20   d=13).    **.  
2bf61 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 78 61 6d    ** In the exam
2bf62 70 6c 65 2c 20 74 68 65 72 65 20 61 72 65 20 74  ple, there are t
2bf63 68 72 65 65 20 69 6e 64 65 78 65 64 20 74 65 72  hree indexed ter
2bf64 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ms connected by 
2bf65 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 74  OR..    ** The t
2bf66 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69  op of the loop i
2bf67 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79  s constructed by
2bf68 20 63 72 65 61 74 69 6e 67 20 61 20 52 6f 77 53   creating a RowS
2bf69 65 74 20 6f 62 6a 65 63 74 0a 20 20 20 20 2a 2a  et object.    **
2bf6a 20 61 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 20   and populating 
2bf6b 69 74 2e 20 20 54 68 65 6e 20 6c 6f 6f 70 69 6e  it.  Then loopin
2bf6c 67 20 6f 76 65 72 20 65 6c 65 6d 65 6e 74 73 20  g over elements 
2bf6d 6f 66 20 74 68 65 20 72 6f 77 73 65 74 2e 0a 20  of the rowset.. 
2bf6e 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
2bf6f 20 20 20 20 4e 75 6c 6c 20 31 0a 20 20 20 20 2a      Null 1.    *
2bf70 2a 20 20 20 20 20 20 20 20 23 20 66 69 6c 6c 20  *        # fill 
2bf71 52 6f 77 53 65 74 20 31 20 77 69 74 68 20 65 6e  RowSet 1 with en
2bf72 74 72 69 65 73 20 77 68 65 72 65 20 61 3d 35 20  tries where a=5 
2bf73 75 73 69 6e 67 20 69 31 0a 20 20 20 20 2a 2a 20  using i1.    ** 
2bf74 20 20 20 20 20 20 20 23 20 66 69 6c 6c 20 52 6f         # fill Ro
2bf75 77 73 65 74 20 31 20 77 69 74 68 20 65 6e 74 72  wset 1 with entr
2bf76 69 65 73 20 77 68 65 72 65 20 62 3d 37 20 75 73  ies where b=7 us
2bf77 69 6e 67 20 69 32 0a 20 20 20 20 2a 2a 20 20 20  ing i2.    **   
2bf78 20 20 20 20 20 23 20 66 69 6c 6c 20 52 6f 77 73       # fill Rows
2bf79 65 74 20 31 20 77 69 74 68 20 65 6e 74 72 69 65  et 1 with entrie
2bf7a 73 20 77 68 65 72 65 20 63 3d 31 31 20 61 6e 64  s where c=11 and
2bf7b 20 64 3d 31 33 20 69 33 20 61 6e 64 20 74 31 0a   d=13 i3 and t1.
2bf7c 20 20 20 20 2a 2a 20 20 20 20 20 41 3a 20 52 6f      **     A: Ro
2bf7d 77 53 65 74 52 65 61 64 20 31 2c 20 42 2c 20 32  wSetRead 1, B, 2
2bf7e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 53  .    **        S
2bf7f 65 65 6b 20 20 20 20 20 20 20 69 2c 20 32 0a 20  eek       i, 2. 
2bf80 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
2bf81 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6c   bottom of the l
2bf82 6f 6f 70 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74  oop looks like t
2bf83 68 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  his:.    **.    
2bf84 2a 2a 20 20 20 20 20 20 20 20 47 6f 74 6f 20 20  **        Goto  
2bf85 20 20 20 20 20 30 2c 20 41 0a 20 20 20 20 2a 2a       0, A.    **
2bf86 20 20 20 20 20 42 3a 0a 20 20 20 20 2a 2f 0a 20       B:.    */. 
2bf87 20 20 20 69 6e 74 20 72 65 67 4f 72 52 6f 77 73     int regOrRows
2bf88 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 67  et;       /* Reg
2bf89 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68  ister holding th
2bf8a 65 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20  e RowSet object 
2bf8b 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 4e 65  */.    int regNe
2bf8c 78 74 52 6f 77 69 64 3b 20 20 20 20 20 20 2f 2a  xtRowid;      /*
2bf8d 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
2bf8e 67 20 6e 65 78 74 20 72 6f 77 69 64 20 2a 2f 0a  g next rowid */.
2bf8f 20 20 20 20 57 68 65 72 65 43 6c 61 75 73 65 20      WhereClause 
2bf90 2a 70 4f 72 57 63 3b 20 20 20 20 2f 2a 20 54 68  *pOrWc;    /* Th
2bf91 65 20 4f 52 2d 63 6c 61 75 73 65 20 62 72 6f 6b  e OR-clause brok
2bf92 65 6e 20 6f 75 74 20 69 6e 74 6f 20 73 75 62 74  en out into subt
2bf93 65 72 6d 73 20 2a 2f 0a 20 20 20 20 57 68 65 72  erms */.    Wher
2bf94 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 20  eTerm *pOrTerm; 
2bf95 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 73     /* A single s
2bf96 75 62 74 65 72 6d 20 77 69 74 68 69 6e 20 74 68  ubterm within th
2bf97 65 20 4f 52 2d 63 6c 61 75 73 65 20 2a 2f 0a 20  e OR-clause */. 
2bf98 20 20 20 53 72 63 4c 69 73 74 20 6f 6e 65 54 61     SrcList oneTa
2bf99 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f  b;        /* Sho
2bf9a 72 74 65 6e 65 64 20 74 61 62 6c 65 20 6c 69 73  rtened table lis
2bf9b 74 20 2a 2f 0a 20 20 20 0a 20 20 20 20 70 54 65  t */.   .    pTe
2bf9c 72 6d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  rm = pLevel->pla
2bf9d 6e 2e 75 2e 70 54 65 72 6d 3b 0a 20 20 20 20 61  n.u.pTerm;.    a
2bf9e 73 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20  ssert( pTerm!=0 
2bf9f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2bfa0 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2bfa1 3d 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61 73  =WO_OR );.    as
2bfa2 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74  sert( (pTerm->wt
2bfa3 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49  Flags & TERM_ORI
2bfa4 4e 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20 70  NFO)!=0 );.    p
2bfa5 4f 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75  OrWc = &pTerm->u
2bfa6 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20  .pOrInfo->wc;.  
2bfa7 20 20 63 6f 64 65 52 6f 77 53 65 74 45 61 72 6c    codeRowSetEarl
2bfa8 79 20 3d 20 28 72 65 67 52 6f 77 53 65 74 3e 3d  y = (regRowSet>=
2bfa9 30 20 26 26 20 70 57 43 2d 3e 6e 54 65 72 6d 3d  0 && pWC->nTerm=
2bfaa 3d 31 29 20 3f 31 3a 30 3b 0a 0a 20 20 20 20 69  =1) ?1:0;..    i
2bfab 66 28 20 63 6f 64 65 52 6f 77 53 65 74 45 61 72  f( codeRowSetEar
2bfac 6c 79 20 29 7b 0a 20 20 20 20 20 20 72 65 67 4f  ly ){.      regO
2bfad 72 52 6f 77 73 65 74 20 3d 20 72 65 67 52 6f 77  rRowset = regRow
2bfae 53 65 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Set;.    }else{.
2bfaf 20 20 20 20 20 20 72 65 67 4f 72 52 6f 77 73 65        regOrRowse
2bfb0 74 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  t = sqlite3GetTe
2bfb1 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2bfb2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2bfb3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2bfb4 6c 2c 20 30 2c 20 72 65 67 4f 72 52 6f 77 73 65  l, 0, regOrRowse
2bfb5 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6e  t);.    }.    on
2bfb6 65 54 61 62 2e 6e 53 72 63 20 3d 20 31 3b 0a 20  eTab.nSrc = 1;. 
2bfb7 20 20 20 6f 6e 65 54 61 62 2e 6e 41 6c 6c 6f 63     oneTab.nAlloc
2bfb8 20 3d 20 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62   = 1;.    oneTab
2bfb9 2e 61 5b 30 5d 20 3d 20 2a 70 54 61 62 49 74 65  .a[0] = *pTabIte
2bfba 6d 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20  m;.    for(j=0, 
2bfbb 70 4f 72 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61  pOrTerm=pOrWc->a
2bfbc 3b 20 6a 3c 70 4f 72 57 63 2d 3e 6e 54 65 72 6d  ; j<pOrWc->nTerm
2bfbd 3b 20 6a 2b 2b 2c 20 70 4f 72 54 65 72 6d 2b 2b  ; j++, pOrTerm++
2bfbe 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 49 6e  ){.      WhereIn
2bfbf 66 6f 20 2a 70 53 75 62 57 49 6e 66 6f 3b 0a 20  fo *pSubWInfo;. 
2bfc0 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d       if( pOrTerm
2bfc1 2d 3e 6c 65 66 74 43 75 72 73 6f 72 21 3d 69 43  ->leftCursor!=iC
2bfc2 75 72 20 26 26 20 70 4f 72 54 65 72 6d 2d 3e 65  ur && pOrTerm->e
2bfc3 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 41 4e 44  Operator!=WO_AND
2bfc4 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2bfc5 20 20 20 70 53 75 62 57 49 6e 66 6f 20 3d 20 73     pSubWInfo = s
2bfc6 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2bfc7 28 70 50 61 72 73 65 2c 20 26 6f 6e 65 54 61 62  (pParse, &oneTab
2bfc8 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  , pOrTerm->pExpr
2bfc9 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2bfca 20 20 20 20 20 20 20 20 20 20 20 20 20 57 48 45               WHE
2bfcb 52 45 5f 46 49 4c 4c 5f 52 4f 57 53 45 54 20 7c  RE_FILL_ROWSET |
2bfcc 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e   WHERE_OMIT_OPEN
2bfcd 20 7c 20 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c   | WHERE_OMIT_CL
2bfce 4f 53 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20  OSE,.           
2bfcf 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67               reg
2bfd0 4f 72 52 6f 77 73 65 74 29 3b 0a 20 20 20 20 20  OrRowset);.     
2bfd1 20 69 66 28 20 70 53 75 62 57 49 6e 66 6f 20 29   if( pSubWInfo )
2bfd2 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2bfd3 33 57 68 65 72 65 45 6e 64 28 70 53 75 62 57 49  3WhereEnd(pSubWI
2bfd4 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  nfo);.      }.  
2bfd5 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2bfd6 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2bfd7 76 2c 20 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20  v, addrCont);.  
2bfd8 20 20 69 66 28 20 21 63 6f 64 65 52 6f 77 53 65    if( !codeRowSe
2bfd9 74 45 61 72 6c 79 20 29 7b 0a 20 20 20 20 20 20  tEarly ){.      
2bfda 72 65 67 4e 65 78 74 52 6f 77 69 64 20 3d 20 73  regNextRowid = s
2bfdb 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2bfdc 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2bfdd 61 64 64 72 43 6f 6e 74 20 3d 20 0a 20 20 20 20  addrCont = .    
2bfde 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2bfdf 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 6f 77  AddOp3(v, OP_Row
2bfe0 53 65 74 52 65 61 64 2c 20 72 65 67 4f 72 52 6f  SetRead, regOrRo
2bfe1 77 73 65 74 2c 61 64 64 72 42 72 6b 2c 72 65 67  wset,addrBrk,reg
2bfe2 4e 65 78 74 52 6f 77 69 64 29 3b 0a 20 20 20 20  NextRowid);.    
2bfe3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2bfe4 4f 70 32 28 76 2c 20 4f 50 5f 53 65 65 6b 2c 20  Op2(v, OP_Seek, 
2bfe5 69 43 75 72 2c 20 72 65 67 4e 65 78 74 52 6f 77  iCur, regNextRow
2bfe6 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
2bfe7 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2bfe8 28 70 50 61 72 73 65 2c 20 72 65 67 4e 65 78 74  (pParse, regNext
2bfe9 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 2f 2a  Rowid);.      /*
2bfea 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2bfeb 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2bfec 65 67 4f 72 52 6f 77 73 65 74 29 3b 20 2f 2f 20  egOrRowset); // 
2bfed 50 72 65 73 65 72 76 65 20 74 68 65 20 52 6f 77  Preserve the Row
2bfee 53 65 74 20 2a 2f 0a 20 20 20 20 20 20 70 4c 65  Set */.      pLe
2bfef 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 47 6f 74  vel->op = OP_Got
2bff0 6f 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  o;.      pLevel-
2bff1 3e 70 32 20 3d 20 61 64 64 72 43 6f 6e 74 3b 0a  >p2 = addrCont;.
2bff2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2bff3 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50   pLevel->op = OP
2bff4 5f 4e 6f 6f 70 3b 0a 20 20 20 20 7d 0a 20 20 20  _Noop;.    }.   
2bff5 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2bff6 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 7d  vel, pTerm);.  }
2bff7 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
2bff8 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
2bff9 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20  TIMIZATION */.. 
2bffa 20 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 35   {.    /* Case 5
2bffb 3a 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 75  :  There is no u
2bffc 73 61 62 6c 65 20 69 6e 64 65 78 2e 20 20 57 65  sable index.  We
2bffd 20 6d 75 73 74 20 64 6f 20 61 20 63 6f 6d 70 6c   must do a compl
2bffe 65 74 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ete.    **      
2bfff 20 20 20 20 73 63 61 6e 20 6f 66 20 74 68 65 20      scan of the 
2c000 65 6e 74 69 72 65 20 74 61 62 6c 65 2e 0a 20 20  entire table..  
2c001 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20    */.    static 
2c002 63 6f 6e 73 74 20 75 38 20 61 53 74 65 70 5b 5d  const u8 aStep[]
2c003 20 3d 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 4f 50   = { OP_Next, OP
2c004 5f 50 72 65 76 20 7d 3b 0a 20 20 20 20 73 74 61  _Prev };.    sta
2c005 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 53 74  tic const u8 aSt
2c006 61 72 74 5b 5d 20 3d 20 7b 20 4f 50 5f 52 65 77  art[] = { OP_Rew
2c007 69 6e 64 2c 20 4f 50 5f 4c 61 73 74 20 7d 3b 0a  ind, OP_Last };.
2c008 20 20 20 20 61 73 73 65 72 74 28 20 62 52 65 76      assert( bRev
2c009 3d 3d 30 20 7c 7c 20 62 52 65 76 3d 3d 31 20 29  ==0 || bRev==1 )
2c00a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d  ;.    assert( om
2c00b 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  itTable==0 );.  
2c00c 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 61    pLevel->op = a
2c00d 53 74 65 70 5b 62 52 65 76 5d 3b 0a 20 20 20 20  Step[bRev];.    
2c00e 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75  pLevel->p1 = iCu
2c00f 72 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  r;.    pLevel->p
2c010 32 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56  2 = 1 + sqlite3V
2c011 64 62 65 41 64 64 4f 70 32 28 76 2c 20 61 53 74  dbeAddOp2(v, aSt
2c012 61 72 74 5b 62 52 65 76 5d 2c 20 69 43 75 72 2c  art[bRev], iCur,
2c013 20 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 70   addrBrk);.    p
2c014 4c 65 76 65 6c 2d 3e 70 35 20 3d 20 53 51 4c 49  Level->p5 = SQLI
2c015 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55  TE_STMTSTATUS_FU
2c016 4c 4c 53 43 41 4e 5f 53 54 45 50 3b 0a 20 20 20  LLSCAN_STEP;.   
2c017 20 63 6f 64 65 52 6f 77 53 65 74 45 61 72 6c 79   codeRowSetEarly
2c018 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 6f 74 52   = 0;.  }.  notR
2c019 65 61 64 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b  eady &= ~getMask
2c01a 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20  (pWC->pMaskSet, 
2c01b 69 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 49 6e 73  iCur);..  /* Ins
2c01c 65 72 74 20 63 6f 64 65 20 74 6f 20 74 65 73 74  ert code to test
2c01d 20 65 76 65 72 79 20 73 75 62 65 78 70 72 65 73   every subexpres
2c01e 73 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65  sion that can be
2c01f 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 20 20 2a 2a   completely.  **
2c020 20 63 6f 6d 70 75 74 65 64 20 75 73 69 6e 67 20   computed using 
2c021 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 20  the current set 
2c022 6f 66 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a  of tables..  */.
2c023 20 20 6b 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70    k = 0;.  for(p
2c024 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 70  Term=pWC->a, j=p
2c025 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 3e 30 3b 20  WC->nTerm; j>0; 
2c026 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20  j--, pTerm++){. 
2c027 20 20 20 45 78 70 72 20 2a 70 45 3b 0a 20 20 20     Expr *pE;.   
2c028 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
2c029 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
2c02a 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20  _VIRTUAL );.    
2c02b 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
2c02c 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2c02d 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 69 66 28  CODED );.    if(
2c02e 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2c02f 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c  & (TERM_VIRTUAL|
2c030 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f  TERM_CODED) ) co
2c031 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
2c032 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c  (pTerm->prereqAl
2c033 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21 3d 30  l & notReady)!=0
2c034 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2c035 20 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78   pE = pTerm->pEx
2c036 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  pr;.    assert( 
2c037 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  pE!=0 );.    if(
2c038 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
2c039 69 6e 20 26 26 20 21 45 78 70 72 48 61 73 50 72  in && !ExprHasPr
2c03a 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72  operty(pE, EP_Fr
2c03b 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20  omJoin) ){.     
2c03c 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
2c03d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73  .    pParse->dis
2c03e 61 62 6c 65 43 6f 6c 43 61 63 68 65 20 2b 3d 20  ableColCache += 
2c03f 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  k;.    sqlite3Ex
2c040 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
2c041 2c 20 70 45 2c 20 61 64 64 72 43 6f 6e 74 2c 20  , pE, addrCont, 
2c042 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
2c043 4c 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  L);.    pParse->
2c044 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 20  disableColCache 
2c045 2d 3d 20 6b 3b 0a 20 20 20 20 6b 20 3d 20 31 3b  -= k;.    k = 1;
2c046 0a 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c  .    pTerm->wtFl
2c047 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45  ags |= TERM_CODE
2c048 44 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72  D;.  }..  /* For
2c049 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f   a LEFT OUTER JO
2c04a 49 4e 2c 20 67 65 6e 65 72 61 74 65 20 63 6f 64  IN, generate cod
2c04b 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65 63 6f  e that will reco
2c04c 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  rd the fact that
2c04d 0a 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f  .  ** at least o
2c04e 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 69  ne row of the ri
2c04f 67 68 74 20 74 61 62 6c 65 20 68 61 73 20 6d 61  ght table has ma
2c050 74 63 68 65 64 20 74 68 65 20 6c 65 66 74 20 74  tched the left t
2c051 61 62 6c 65 2e 20 20 0a 20 20 2a 2f 0a 20 20 69  able.  .  */.  i
2c052 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  f( pLevel->iLeft
2c053 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 70 4c 65 76  Join ){.    pLev
2c054 65 6c 2d 3e 61 64 64 72 46 69 72 73 74 20 3d 20  el->addrFirst = 
2c055 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2c056 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73  ntAddr(v);.    s
2c057 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c058 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2c059 31 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  1, pLevel->iLeft
2c05a 4a 6f 69 6e 29 3b 0a 20 20 20 20 56 64 62 65 43  Join);.    VdbeC
2c05b 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 63 6f  omment((v, "reco
2c05c 72 64 20 4c 45 46 54 20 4a 4f 49 4e 20 68 69 74  rd LEFT JOIN hit
2c05d 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "));.    sqlite3
2c05e 45 78 70 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43  ExprClearColumnC
2c05f 61 63 68 65 28 70 50 61 72 73 65 2c 20 70 4c 65  ache(pParse, pLe
2c060 76 65 6c 2d 3e 69 54 61 62 43 75 72 29 3b 0a 20  vel->iTabCur);. 
2c061 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c     sqlite3ExprCl
2c062 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70  earColumnCache(p
2c063 50 61 72 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69  Parse, pLevel->i
2c064 49 64 78 43 75 72 29 3b 0a 20 20 20 20 66 6f 72  IdxCur);.    for
2c065 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a  (pTerm=pWC->a, j
2c066 3d 30 3b 20 6a 3c 70 57 43 2d 3e 6e 54 65 72 6d  =0; j<pWC->nTerm
2c067 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; j++, pTerm++){
2c068 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2c069 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2c06a 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29  & TERM_VIRTUAL )
2c06b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2c06c 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  ( pTerm->wtFlags
2c06d 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 20 29 3b   & TERM_CODED );
2c06e 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
2c06f 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52  ->wtFlags & (TER
2c070 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43  M_VIRTUAL|TERM_C
2c071 4f 44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65  ODED) ) continue
2c072 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 54 65  ;.      if( (pTe
2c073 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20  rm->prereqAll & 
2c074 6e 6f 74 52 65 61 64 79 29 21 3d 30 20 29 20 63  notReady)!=0 ) c
2c075 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 61  ontinue;.      a
2c076 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70 45  ssert( pTerm->pE
2c077 78 70 72 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  xpr );.      sql
2c078 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2c079 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2d 3e 70  pParse, pTerm->p
2c07a 45 78 70 72 2c 20 61 64 64 72 43 6f 6e 74 2c 20  Expr, addrCont, 
2c07b 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
2c07c 4c 29 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  L);.      pTerm-
2c07d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
2c07e 5f 43 4f 44 45 44 3b 0a 20 20 20 20 7d 0a 20 20  _CODED;.    }.  
2c07f 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20  }..  /*.  ** If 
2c080 69 74 20 77 61 73 20 72 65 71 75 65 73 74 65 64  it was requested
2c081 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65   to store the re
2c082 73 75 6c 74 73 20 69 6e 20 61 20 72 6f 77 73 65  sults in a rowse
2c083 74 20 61 6e 64 20 74 68 61 74 20 68 61 73 0a 20  t and that has. 
2c084 20 2a 2a 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   ** not already 
2c085 62 65 65 6e 20 64 6f 2c 20 74 68 65 6e 20 64 6f  been do, then do
2c086 20 73 6f 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20   so now..  */.  
2c087 69 66 28 20 72 65 67 52 6f 77 53 65 74 3e 3d 30  if( regRowSet>=0
2c088 20 26 26 20 21 63 6f 64 65 52 6f 77 53 65 74 45   && !codeRowSetE
2c089 61 72 6c 79 20 29 7b 0a 20 20 20 20 69 6e 74 20  arly ){.    int 
2c08a 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
2c08b 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
2c08c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2c08d 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2c08e 0a 20 20 20 20 69 66 28 20 20 28 70 4c 65 76 65  .    if(  (pLeve
2c08f 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2c090 26 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  & WHERE_VIRTUALT
2c091 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  ABLE)!=0 ){.    
2c092 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c093 4f 70 32 28 76 2c 20 4f 50 5f 56 52 6f 77 69 64  Op2(v, OP_VRowid
2c094 2c 20 69 43 75 72 2c 20 72 31 29 3b 0a 20 20 20  , iCur, r1);.   
2c095 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
2c096 20 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65    {.      sqlite
2c097 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c098 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72  P_Rowid, iCur, r
2c099 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  1);.    }.    sq
2c09a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c09b 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 2c  v, OP_RowSetAdd,
2c09c 20 72 65 67 52 6f 77 53 65 74 2c 20 72 31 29 3b   regRowSet, r1);
2c09d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
2c09e 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2c09f 65 2c 20 72 31 29 3b 0a 20 20 7d 0a 0a 20 20 72  e, r1);.  }..  r
2c0a0 65 74 75 72 6e 20 6e 6f 74 52 65 61 64 79 3b 0a  eturn notReady;.
2c0a1 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  }..#if defined(S
2c0a2 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a  QLITE_TEST)./*.*
2c0a3 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2c0a4 76 61 72 69 61 62 6c 65 20 68 6f 6c 64 73 20 61  variable holds a
2c0a5 20 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f   text descriptio
2c0a6 6e 20 6f 66 20 71 75 65 72 79 20 70 6c 61 6e 20  n of query plan 
2c0a7 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 62 79 20  generated.** by 
2c0a8 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
2c0a9 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 57  call to sqlite3W
2c0aa 68 65 72 65 42 65 67 69 6e 28 29 2e 20 20 45 61  hereBegin().  Ea
2c0ab 63 68 20 63 61 6c 6c 20 74 6f 20 57 68 65 72 65  ch call to Where
2c0ac 42 65 67 69 6e 0a 2a 2a 20 6f 76 65 72 77 72 69  Begin.** overwri
2c0ad 74 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 73  tes the previous
2c0ae 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74  .  This informat
2c0af 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20  ion is used for 
2c0b0 74 65 73 74 69 6e 67 20 61 6e 64 0a 2a 2a 20 61  testing and.** a
2c0b1 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f  nalysis only..*/
2c0b2 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72  .SQLITE_API char
2c0b3 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70   sqlite3_query_p
2c0b4 6c 61 6e 5b 42 4d 53 2a 32 2a 34 30 5d 3b 20 20  lan[BMS*2*40];  
2c0b5 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6a  /* Text of the j
2c0b6 6f 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  oin */.static in
2c0b7 74 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 20 20 20  t nQPlan = 0;   
2c0b8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
2c0b9 78 74 20 66 72 65 65 20 73 6c 6f 77 20 69 6e 20  xt free slow in 
2c0ba 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 5d 20 2a 2f  _query_plan[] */
2c0bb 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
2c0bc 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 2f 2a 0a  TE_TEST */.../*.
2c0bd 2a 2a 20 46 72 65 65 20 61 20 57 68 65 72 65 49  ** Free a WhereI
2c0be 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2f  nfo structure.*/
2c0bf 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65  .static void whe
2c0c0 72 65 49 6e 66 6f 46 72 65 65 28 73 71 6c 69 74  reInfoFree(sqlit
2c0c1 65 33 20 2a 64 62 2c 20 57 68 65 72 65 49 6e 66  e3 *db, WhereInf
2c0c2 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 69 66  o *pWInfo){.  if
2c0c3 28 20 70 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20  ( pWInfo ){.    
2c0c4 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
2c0c5 3d 30 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c  =0; i<pWInfo->nL
2c0c6 65 76 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  evel; i++){.    
2c0c7 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
2c0c8 69 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 57  info *pInfo = pW
2c0c9 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78 49  Info->a[i].pIdxI
2c0ca 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70  nfo;.      if( p
2c0cb 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  Info ){.        
2c0cc 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e 6e  assert( pInfo->n
2c0cd 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 3d  eedToFreeIdxStr=
2c0ce 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  =0 || db->malloc
2c0cf 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20  Failed );.      
2c0d0 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 65 65    if( pInfo->nee
2c0d1 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b  dToFreeIdxStr ){
2c0d2 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2c0d3 65 33 5f 66 72 65 65 28 70 49 6e 66 6f 2d 3e 69  e3_free(pInfo->i
2c0d4 64 78 53 74 72 29 3b 0a 20 20 20 20 20 20 20 20  dxStr);.        
2c0d5 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
2c0d6 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 6e 66  3DbFree(db, pInf
2c0d7 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  o);.      }.    
2c0d8 7d 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75 73  }.    whereClaus
2c0d9 65 43 6c 65 61 72 28 70 57 49 6e 66 6f 2d 3e 70  eClear(pWInfo->p
2c0da 57 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  WC);.    sqlite3
2c0db 44 62 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66  DbFree(db, pWInf
2c0dc 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a  o);.  }.}.../*.*
2c0dd 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 62  * Generate the b
2c0de 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
2c0df 6c 6f 6f 70 20 75 73 65 64 20 66 6f 72 20 57 48  loop used for WH
2c0e0 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65  ERE clause proce
2c0e1 73 73 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 72 65  ssing..** The re
2c0e2 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 61 20  turn value is a 
2c0e3 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 70  pointer to an op
2c0e4 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 74  aque structure t
2c0e5 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  hat contains.** 
2c0e6 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64  information need
2c0e7 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20  ed to terminate 
2c0e8 74 68 65 20 6c 6f 6f 70 2e 20 20 4c 61 74 65 72  the loop.  Later
2c0e9 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f  , the calling ro
2c0ea 75 74 69 6e 65 0a 2a 2a 20 73 68 6f 75 6c 64 20  utine.** should 
2c0eb 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 57 68  invoke sqlite3Wh
2c0ec 65 72 65 45 6e 64 28 29 20 77 69 74 68 20 74 68  ereEnd() with th
2c0ed 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
2c0ee 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  f this function.
2c0ef 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63  ** in order to c
2c0f0 6f 6d 70 6c 65 74 65 20 74 68 65 20 57 48 45 52  omplete the WHER
2c0f1 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73  E clause process
2c0f2 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ing..**.** If an
2c0f3 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
2c0f4 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
2c0f5 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  rns NULL..**.** 
2c0f6 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69  The basic idea i
2c0f7 73 20 74 6f 20 64 6f 20 61 20 6e 65 73 74 65 64  s to do a nested
2c0f8 20 6c 6f 6f 70 2c 20 6f 6e 65 20 6c 6f 6f 70 20   loop, one loop 
2c0f9 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 69  for each table i
2c0fa 6e 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c  n.** the FROM cl
2c0fb 61 75 73 65 20 6f 66 20 61 20 73 65 6c 65 63 74  ause of a select
2c0fc 2e 20 20 28 49 4e 53 45 52 54 20 61 6e 64 20 55  .  (INSERT and U
2c0fd 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73  PDATE statements
2c0fe 20 61 72 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65   are the.** same
2c0ff 20 61 73 20 61 20 53 45 4c 45 43 54 20 77 69 74   as a SELECT wit
2c100 68 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20  h only a single 
2c101 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f  table in the FRO
2c102 4d 20 63 6c 61 75 73 65 2e 29 20 20 46 6f 72 0a  M clause.)  For.
2c103 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74  ** example, if t
2c104 68 65 20 53 51 4c 20 69 73 20 74 68 69 73 3a 0a  he SQL is this:.
2c105 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 45 4c 45  **.**       SELE
2c106 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32  CT * FROM t1, t2
2c107 2c 20 74 33 20 57 48 45 52 45 20 2e 2e 2e 3b 0a  , t3 WHERE ...;.
2c108 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 63  **.** Then the c
2c109 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 69 73  ode generated is
2c10a 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 6c 69   conceptually li
2c10b 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ke the following
2c10c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 6f 72  :.**.**      for
2c10d 65 61 63 68 20 72 6f 77 31 20 69 6e 20 74 31 20  each row1 in t1 
2c10e 64 6f 20 20 20 20 20 20 20 5c 20 20 20 20 43 6f  do       \    Co
2c10f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20  de generated.** 
2c110 20 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 72         foreach r
2c111 6f 77 32 20 69 6e 20 74 32 20 64 6f 20 20 20 20  ow2 in t2 do    
2c112 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74 65 33    |-- by sqlite3
2c113 57 68 65 72 65 42 65 67 69 6e 28 29 0a 2a 2a 20  WhereBegin().** 
2c114 20 20 20 20 20 20 20 20 20 66 6f 72 65 61 63 68           foreach
2c115 20 72 6f 77 33 20 69 6e 20 74 33 20 64 6f 20 20   row3 in t3 do  
2c116 20 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   /.**           
2c117 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20   ....**         
2c118 20 65 6e 64 20 20 20 20 20 20 20 20 20 20 20 20   end            
2c119 20 20 20 20 20 20 20 20 20 5c 20 20 20 20 43 6f           \    Co
2c11a 64 65 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20  de generated.** 
2c11b 20 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20         end      
2c11c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c11d 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74 65 33    |-- by sqlite3
2c11e 57 68 65 72 65 45 6e 64 28 29 0a 2a 2a 20 20 20  WhereEnd().**   
2c11f 20 20 20 65 6e 64 20 20 20 20 20 20 20 20 20 20     end          
2c120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c121 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
2c122 20 74 68 65 20 6c 6f 6f 70 73 20 6d 69 67 68 74   the loops might
2c123 20 6e 6f 74 20 62 65 20 6e 65 73 74 65 64 20 69   not be nested i
2c124 6e 20 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77  n the order in w
2c125 68 69 63 68 20 74 68 65 79 0a 2a 2a 20 61 70 70  hich they.** app
2c126 65 61 72 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ear in the FROM 
2c127 63 6c 61 75 73 65 20 69 66 20 61 20 64 69 66 66  clause if a diff
2c128 65 72 65 6e 74 20 6f 72 64 65 72 20 69 73 20 62  erent order is b
2c129 65 74 74 65 72 20 61 62 6c 65 20 74 6f 20 6d 61  etter able to ma
2c12a 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 69 6e 64  ke.** use of ind
2c12b 69 63 65 73 2e 20 20 4e 6f 74 65 20 61 6c 73 6f  ices.  Note also
2c12c 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 49   that when the I
2c12d 4e 20 6f 70 65 72 61 74 6f 72 20 61 70 70 65 61  N operator appea
2c12e 72 73 20 69 6e 0a 2a 2a 20 74 68 65 20 57 48 45  rs in.** the WHE
2c12f 52 45 20 63 6c 61 75 73 65 2c 20 69 74 20 6d 69  RE clause, it mi
2c130 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61 64  ght result in ad
2c131 64 69 74 69 6f 6e 61 6c 20 6e 65 73 74 65 64 20  ditional nested 
2c132 6c 6f 6f 70 73 20 66 6f 72 0a 2a 2a 20 73 63 61  loops for.** sca
2c133 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68 20 61 6c  nning through al
2c134 6c 20 76 61 6c 75 65 73 20 6f 6e 20 74 68 65 20  l values on the 
2c135 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
2c136 6f 66 20 74 68 65 20 49 4e 2e 0a 2a 2a 0a 2a 2a  of the IN..**.**
2c137 20 54 68 65 72 65 20 61 72 65 20 42 74 72 65 65   There are Btree
2c138 20 63 75 72 73 6f 72 73 20 61 73 73 6f 63 69 61   cursors associa
2c139 74 65 64 20 77 69 74 68 20 65 61 63 68 20 74 61  ted with each ta
2c13a 62 6c 65 2e 20 20 74 31 20 75 73 65 73 20 63 75  ble.  t1 uses cu
2c13b 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 70  rsor.** number p
2c13c 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43  TabList->a[0].iC
2c13d 75 72 73 6f 72 2e 20 20 74 32 20 75 73 65 73 20  ursor.  t2 uses 
2c13e 74 68 65 20 63 75 72 73 6f 72 20 70 54 61 62 4c  the cursor pTabL
2c13f 69 73 74 2d 3e 61 5b 31 5d 2e 69 43 75 72 73 6f  ist->a[1].iCurso
2c140 72 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20 66 6f 72  r..** And so for
2c141 74 68 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  th.  This routin
2c142 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65  e generates code
2c143 20 74 6f 20 6f 70 65 6e 20 74 68 6f 73 65 20 56   to open those V
2c144 44 42 45 20 63 75 72 73 6f 72 73 0a 2a 2a 20 61  DBE cursors.** a
2c145 6e 64 20 73 71 6c 69 74 65 33 57 68 65 72 65 45  nd sqlite3WhereE
2c146 6e 64 28 29 20 67 65 6e 65 72 61 74 65 73 20 74  nd() generates t
2c147 68 65 20 63 6f 64 65 20 74 6f 20 63 6c 6f 73 65  he code to close
2c148 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   them..**.** The
2c149 20 63 6f 64 65 20 74 68 61 74 20 73 71 6c 69 74   code that sqlit
2c14a 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 67  e3WhereBegin() g
2c14b 65 6e 65 72 61 74 65 73 20 6c 65 61 76 65 73 20  enerates leaves 
2c14c 74 68 65 20 63 75 72 73 6f 72 73 20 6e 61 6d 65  the cursors name
2c14d 64 0a 2a 2a 20 69 6e 20 70 54 61 62 4c 69 73 74  d.** in pTabList
2c14e 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65   pointing at the
2c14f 69 72 20 61 70 70 72 6f 70 72 69 61 74 65 20 65  ir appropriate e
2c150 6e 74 72 69 65 73 2e 20 20 54 68 65 20 5b 2e 2e  ntries.  The [..
2c151 2e 5d 20 63 6f 64 65 0a 2a 2a 20 63 61 6e 20 75  .] code.** can u
2c152 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61 6e 64  se OP_Column and
2c153 20 4f 50 5f 52 6f 77 69 64 20 6f 70 63 6f 64 65   OP_Rowid opcode
2c154 73 20 6f 6e 20 74 68 65 73 65 20 63 75 72 73 6f  s on these curso
2c155 72 73 20 74 6f 20 65 78 74 72 61 63 74 0a 2a 2a  rs to extract.**
2c156 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 76   data from the v
2c157 61 72 69 6f 75 73 20 74 61 62 6c 65 73 20 6f 66  arious tables of
2c158 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a   the loop..**.**
2c159 20 49 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   If the WHERE cl
2c15a 61 75 73 65 20 69 73 20 65 6d 70 74 79 2c 20 74  ause is empty, t
2c15b 68 65 20 66 6f 72 65 61 63 68 20 6c 6f 6f 70 73  he foreach loops
2c15c 20 6d 75 73 74 20 65 61 63 68 20 73 63 61 6e 20   must each scan 
2c15d 74 68 65 69 72 0a 2a 2a 20 65 6e 74 69 72 65 20  their.** entire 
2c15e 74 61 62 6c 65 73 2e 20 20 54 68 75 73 20 61 20  tables.  Thus a 
2c15f 74 68 72 65 65 2d 77 61 79 20 6a 6f 69 6e 20 69  three-way join i
2c160 73 20 61 6e 20 4f 28 4e 5e 33 29 20 6f 70 65 72  s an O(N^3) oper
2c161 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 0a 2a  ation.  But if.*
2c162 2a 20 74 68 65 20 74 61 62 6c 65 73 20 68 61 76  * the tables hav
2c163 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20 74 68  e indices and th
2c164 65 72 65 20 61 72 65 20 74 65 72 6d 73 20 69 6e  ere are terms in
2c165 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2c166 65 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 20  e that.** refer 
2c167 74 6f 20 74 68 6f 73 65 20 69 6e 64 69 63 65 73  to those indices
2c168 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20 74 61 62  , a complete tab
2c169 6c 65 20 73 63 61 6e 20 63 61 6e 20 62 65 20 61  le scan can be a
2c16a 76 6f 69 64 65 64 20 61 6e 64 20 74 68 65 0a 2a  voided and the.*
2c16b 2a 20 63 6f 64 65 20 77 69 6c 6c 20 72 75 6e 20  * code will run 
2c16c 6d 75 63 68 20 66 61 73 74 65 72 2e 20 20 4d 6f  much faster.  Mo
2c16d 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 20 6f  st of the work o
2c16e 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
2c16f 73 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 74 6f  s checking.** to
2c170 20 73 65 65 20 69 66 20 74 68 65 72 65 20 61 72   see if there ar
2c171 65 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 63  e indices that c
2c172 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 70  an be used to sp
2c173 65 65 64 20 75 70 20 74 68 65 20 6c 6f 6f 70 2e  eed up the loop.
2c174 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20 6f 66 20  .**.** Terms of 
2c175 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2c176 20 61 72 65 20 61 6c 73 6f 20 75 73 65 64 20 74   are also used t
2c177 6f 20 6c 69 6d 69 74 20 77 68 69 63 68 20 72 6f  o limit which ro
2c178 77 73 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 6d  ws actually.** m
2c179 61 6b 65 20 69 74 20 74 6f 20 74 68 65 20 22 2e  ake it to the ".
2c17a 2e 2e 22 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  .." in the middl
2c17b 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20  e of the loop.  
2c17c 41 66 74 65 72 20 65 61 63 68 20 22 66 6f 72 65  After each "fore
2c17d 61 63 68 22 2c 0a 2a 2a 20 74 65 72 6d 73 20 6f  ach",.** terms o
2c17e 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2c17f 73 65 20 74 68 61 74 20 75 73 65 20 6f 6e 6c 79  se that use only
2c180 20 74 65 72 6d 73 20 69 6e 20 74 68 61 74 20 6c   terms in that l
2c181 6f 6f 70 20 61 6e 64 20 6f 75 74 65 72 0a 2a 2a  oop and outer.**
2c182 20 6c 6f 6f 70 73 20 61 72 65 20 65 76 61 6c 75   loops are evalu
2c183 61 74 65 64 20 61 6e 64 20 69 66 20 66 61 6c 73  ated and if fals
2c184 65 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65  e a jump is made
2c185 20 61 72 6f 75 6e 64 20 61 6c 6c 20 73 75 62 73   around all subs
2c186 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 6e 65 72 20  equent.** inner 
2c187 6c 6f 6f 70 73 20 28 6f 72 20 61 72 6f 75 6e 64  loops (or around
2c188 20 74 68 65 20 22 2e 2e 2e 22 20 69 66 20 74 68   the "..." if th
2c189 65 20 74 65 73 74 20 6f 63 63 75 72 73 20 77 69  e test occurs wi
2c18a 74 68 69 6e 20 74 68 65 20 69 6e 6e 65 72 2d 0a  thin the inner-.
2c18b 2a 2a 20 6d 6f 73 74 20 6c 6f 6f 70 29 0a 2a 2a  ** most loop).**
2c18c 0a 2a 2a 20 4f 55 54 45 52 20 4a 4f 49 4e 53 0a  .** OUTER JOINS.
2c18d 2a 2a 0a 2a 2a 20 41 6e 20 6f 75 74 65 72 20 6a  **.** An outer j
2c18e 6f 69 6e 20 6f 66 20 74 61 62 6c 65 73 20 74 31  oin of tables t1
2c18f 20 61 6e 64 20 74 32 20 69 73 20 63 6f 6e 63 65   and t2 is conce
2c190 70 74 61 6c 6c 79 20 63 6f 64 65 64 20 61 73 20  ptally coded as 
2c191 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
2c192 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69    foreach row1 i
2c193 6e 20 74 31 20 64 6f 0a 2a 2a 20 20 20 20 20 20  n t1 do.**      
2c194 66 6c 61 67 20 3d 20 30 0a 2a 2a 20 20 20 20 20  flag = 0.**     
2c195 20 66 6f 72 65 61 63 68 20 72 6f 77 32 20 69 6e   foreach row2 in
2c196 20 74 32 20 64 6f 0a 2a 2a 20 20 20 20 20 20 20   t2 do.**       
2c197 20 73 74 61 72 74 3a 0a 2a 2a 20 20 20 20 20 20   start:.**      
2c198 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20      ....**      
2c199 20 20 20 20 66 6c 61 67 20 3d 20 31 0a 2a 2a 20      flag = 1.** 
2c19a 20 20 20 20 20 65 6e 64 0a 2a 2a 20 20 20 20 20       end.**     
2c19b 20 69 66 20 66 6c 61 67 3d 3d 30 20 74 68 65 6e   if flag==0 then
2c19c 0a 2a 2a 20 20 20 20 20 20 20 20 6d 6f 76 65 20  .**        move 
2c19d 74 68 65 20 72 6f 77 32 20 63 75 72 73 6f 72 20  the row2 cursor 
2c19e 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 0a 2a 2a  to a null row.**
2c19f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 74 61          goto sta
2c1a0 72 74 0a 2a 2a 20 20 20 20 20 20 66 69 0a 2a 2a  rt.**      fi.**
2c1a1 20 20 20 20 65 6e 64 0a 2a 2a 0a 2a 2a 20 4f 52      end.**.** OR
2c1a2 44 45 52 20 42 59 20 43 4c 41 55 53 45 20 50 52  DER BY CLAUSE PR
2c1a3 4f 43 45 53 53 49 4e 47 0a 2a 2a 0a 2a 2a 20 2a  OCESSING.**.** *
2c1a4 70 70 4f 72 64 65 72 42 79 20 69 73 20 61 20 70  ppOrderBy is a p
2c1a5 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4f 52  ointer to the OR
2c1a6 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 6f 66  DER BY clause of
2c1a7 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2c1a8 65 6e 74 2c 0a 2a 2a 20 69 66 20 74 68 65 72 65  ent,.** if there
2c1a9 20 69 73 20 6f 6e 65 2e 20 20 49 66 20 74 68 65   is one.  If the
2c1aa 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42  re is no ORDER B
2c1ab 59 20 63 6c 61 75 73 65 20 6f 72 20 69 66 20 74  Y clause or if t
2c1ac 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69  his routine.** i
2c1ad 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 6e  s called from an
2c1ae 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
2c1af 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  E statement, the
2c1b0 6e 20 70 70 4f 72 64 65 72 42 79 20 69 73 20 4e  n ppOrderBy is N
2c1b1 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ULL..**.** If an
2c1b2 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73   index can be us
2c1b3 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e  ed so that the n
2c1b4 61 74 75 72 61 6c 20 6f 75 74 70 75 74 20 6f 72  atural output or
2c1b5 64 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  der of the table
2c1b6 0a 2a 2a 20 73 63 61 6e 20 69 73 20 63 6f 72 72  .** scan is corr
2c1b7 65 63 74 20 66 6f 72 20 74 68 65 20 4f 52 44 45  ect for the ORDE
2c1b8 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65  R BY clause, the
2c1b9 6e 20 74 68 61 74 20 69 6e 64 65 78 20 69 73 20  n that index is 
2c1ba 75 73 65 64 20 61 6e 64 0a 2a 2a 20 2a 70 70 4f  used and.** *ppO
2c1bb 72 64 65 72 42 79 20 69 73 20 73 65 74 20 74 6f  rderBy is set to
2c1bc 20 4e 55 4c 4c 2e 20 20 54 68 69 73 20 69 73 20   NULL.  This is 
2c1bd 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  an optimization 
2c1be 74 68 61 74 20 70 72 65 76 65 6e 74 73 20 61 6e  that prevents an
2c1bf 0a 2a 2a 20 75 6e 6e 65 63 65 73 73 61 72 79 20  .** unnecessary 
2c1c0 73 6f 72 74 20 6f 66 20 74 68 65 20 72 65 73 75  sort of the resu
2c1c1 6c 74 20 73 65 74 20 69 66 20 61 6e 20 69 6e 64  lt set if an ind
2c1c2 65 78 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  ex appropriate f
2c1c3 6f 72 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20  or the.** ORDER 
2c1c4 42 59 20 63 6c 61 75 73 65 20 61 6c 72 65 61 64  BY clause alread
2c1c5 79 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20  y exists..**.** 
2c1c6 49 66 20 74 68 65 20 77 68 65 72 65 20 63 6c 61  If the where cla
2c1c7 75 73 65 20 6c 6f 6f 70 73 20 63 61 6e 6e 6f 74  use loops cannot
2c1c8 20 62 65 20 61 72 72 61 6e 67 65 64 20 74 6f 20   be arranged to 
2c1c9 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f 72 72  provide the corr
2c1ca 65 63 74 0a 2a 2a 20 6f 75 74 70 75 74 20 6f 72  ect.** output or
2c1cb 64 65 72 2c 20 74 68 65 6e 20 74 68 65 20 2a 70  der, then the *p
2c1cc 70 4f 72 64 65 72 42 79 20 69 73 20 75 6e 63 68  pOrderBy is unch
2c1cd 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  anged..*/.SQLITE
2c1ce 5f 50 52 49 56 41 54 45 20 57 68 65 72 65 49 6e  _PRIVATE WhereIn
2c1cf 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65  fo *sqlite3Where
2c1d0 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a  Begin(.  Parse *
2c1d1 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
2c1d2 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2c1d3 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
2c1d4 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20  t *pTabList,    
2c1d5 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c  /* A list of all
2c1d6 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 73 63   tables to be sc
2c1d7 61 6e 6e 65 64 20 2a 2f 0a 20 20 45 78 70 72 20  anned */.  Expr 
2c1d8 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20  *pWhere,        
2c1d9 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
2c1da 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ause */.  ExprLi
2c1db 73 74 20 2a 2a 70 70 4f 72 64 65 72 42 79 2c 20  st **ppOrderBy, 
2c1dc 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 63  /* An ORDER BY c
2c1dd 6c 61 75 73 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a  lause, or NULL *
2c1de 2f 0a 20 20 75 38 20 77 63 74 72 6c 46 6c 61 67  /.  u8 wctrlFlag
2c1df 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65  s,        /* One
2c1e0 20 6f 66 20 74 68 65 20 57 48 45 52 45 5f 2a 20   of the WHERE_* 
2c1e1 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20 69 6e  flags defined in
2c1e2 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f 0a   sqliteInt.h */.
2c1e3 20 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74 20    int regRowSet 
2c1e4 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
2c1e5 74 65 72 20 68 6f 6c 64 20 52 6f 77 53 65 74 20  ter hold RowSet 
2c1e6 69 66 20 57 48 45 52 45 5f 46 49 4c 4c 5f 52 4f  if WHERE_FILL_RO
2c1e7 57 53 45 54 20 69 73 20 73 65 74 20 2a 2f 0a 29  WSET is set */.)
2c1e8 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
2c1e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c1ea 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
2c1eb 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 57 49 6e  /.  int nByteWIn
2c1ec 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  fo;            /
2c1ed 2a 20 4e 75 6d 2e 20 62 79 74 65 73 20 61 6c 6c  * Num. bytes all
2c1ee 6f 63 61 74 65 64 20 66 6f 72 20 57 68 65 72 65  ocated for Where
2c1ef 49 6e 66 6f 20 73 74 72 75 63 74 20 2a 2f 0a 20  Info struct */. 
2c1f0 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e   WhereInfo *pWIn
2c1f1 66 6f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57  fo;         /* W
2c1f2 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72  ill become the r
2c1f3 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
2c1f4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  his function */.
2c1f5 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2c1f6 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 2f 2a 20  se->pVdbe;   /* 
2c1f7 54 68 65 20 76 69 72 74 75 61 6c 20 64 61 74 61  The virtual data
2c1f8 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a 20  base engine */. 
2c1f9 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
2c1fa 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  y;          /* C
2c1fb 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20  ursors that are 
2c1fc 6e 6f 74 20 79 65 74 20 70 6f 73 69 74 69 6f 6e  not yet position
2c1fd 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73  ed */.  WhereMas
2c1fe 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20  kSet *pMaskSet; 
2c1ff 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
2c200 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20 2a 2f  sion mask set */
2c201 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2c202 70 57 43 3b 20 20 20 20 20 20 20 20 20 20 20 20  pWC;            
2c203 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74     /* Decomposit
2c204 69 6f 6e 20 6f 66 20 74 68 65 20 57 48 45 52 45  ion of the WHERE
2c205 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
2c206 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2c207 20 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20   *pTabItem;  /* 
2c208 41 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66  A single entry f
2c209 72 6f 6d 20 70 54 61 62 4c 69 73 74 20 2a 2f 0a  rom pTabList */.
2c20a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c    WhereLevel *pL
2c20b 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  evel;           
2c20c 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 6c 65    /* A single le
2c20d 76 65 6c 20 69 6e 20 74 68 65 20 70 57 49 6e 66  vel in the pWInf
2c20e 6f 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  o list */.  int 
2c20f 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20  iFrom;          
2c210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
2c211 69 72 73 74 20 75 6e 75 73 65 64 20 46 52 4f 4d  irst unused FROM
2c212 20 63 6c 61 75 73 65 20 65 6c 65 6d 65 6e 74 20   clause element 
2c213 2a 2f 0a 20 20 69 6e 74 20 61 6e 64 46 6c 61 67  */.  int andFlag
2c214 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
2c215 2f 2a 20 41 4e 44 2d 65 64 20 63 6f 6d 62 69 6e  /* AND-ed combin
2c216 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 70 57 43  ation of all pWC
2c217 2d 3e 61 5b 5d 2e 77 74 46 6c 61 67 73 20 2a 2f  ->a[].wtFlags */
2c218 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
2c219 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c21a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2c21b 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69  tion */.  ExprLi
2c21c 73 74 20 2a 70 4f 72 64 65 72 42 79 20 3d 20 30  st *pOrderBy = 0
2c21d 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62  ;..  /* The numb
2c21e 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
2c21f 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2c220 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20 74 68  is limited by th
2c221 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a  e number of.  **
2c222 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d 61   bits in a Bitma
2c223 73 6b 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  sk .  */.  if( p
2c224 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 42 4d  TabList->nSrc>BM
2c225 53 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  S ){.    sqlite3
2c226 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2c227 20 22 61 74 20 6d 6f 73 74 20 25 64 20 74 61 62   "at most %d tab
2c228 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 22 2c 20  les in a join", 
2c229 42 4d 53 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  BMS);.    return
2c22a 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70   0;.  }..  if( p
2c22b 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2c22c 70 4f 72 64 65 72 42 79 20 3d 20 2a 70 70 4f 72  pOrderBy = *ppOr
2c22d 64 65 72 42 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  derBy;.  }..  /*
2c22e 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e   Allocate and in
2c22f 69 74 69 61 6c 69 7a 65 20 74 68 65 20 57 68 65  itialize the Whe
2c230 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  reInfo structure
2c231 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d   that will becom
2c232 65 20 74 68 65 0a 20 20 2a 2a 20 72 65 74 75 72  e the.  ** retur
2c233 6e 20 76 61 6c 75 65 2e 20 41 20 73 69 6e 67 6c  n value. A singl
2c234 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  e allocation is 
2c235 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
2c236 65 20 57 68 65 72 65 49 6e 66 6f 0a 20 20 2a 2a  e WhereInfo.  **
2c237 20 73 74 72 75 63 74 2c 20 74 68 65 20 63 6f 6e   struct, the con
2c238 74 65 6e 74 73 20 6f 66 20 57 68 65 72 65 49 6e  tents of WhereIn
2c239 66 6f 2e 61 5b 5d 2c 20 74 68 65 20 57 68 65 72  fo.a[], the Wher
2c23a 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72  eClause structur
2c23b 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 57  e.  ** and the W
2c23c 68 65 72 65 4d 61 73 6b 53 65 74 20 73 74 72 75  hereMaskSet stru
2c23d 63 74 75 72 65 2e 20 53 69 6e 63 65 20 57 68 65  cture. Since Whe
2c23e 72 65 43 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e  reClause contain
2c23f 73 20 61 6e 20 38 2d 62 79 74 65 0a 20 20 2a 2a  s an 8-byte.  **
2c240 20 66 69 65 6c 64 20 28 74 79 70 65 20 42 69 74   field (type Bit
2c241 6d 61 73 6b 29 20 69 74 20 6d 75 73 74 20 62 65  mask) it must be
2c242 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 38   aligned on an 8
2c243 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f  -byte boundary o
2c244 6e 0a 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68  n.  ** some arch
2c245 69 74 65 63 74 75 72 65 73 2e 20 48 65 6e 63 65  itectures. Hence
2c246 20 74 68 65 20 52 4f 55 4e 44 38 28 29 20 62 65   the ROUND8() be
2c247 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 64 62 20 3d  low..  */.  db =
2c248 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 6e   pParse->db;.  n
2c249 42 79 74 65 57 49 6e 66 6f 20 3d 20 52 4f 55 4e  ByteWInfo = ROUN
2c24a 44 38 28 73 69 7a 65 6f 66 28 57 68 65 72 65 49  D8(sizeof(WhereI
2c24b 6e 66 6f 29 2b 28 70 54 61 62 4c 69 73 74 2d 3e  nfo)+(pTabList->
2c24c 6e 53 72 63 2d 31 29 2a 73 69 7a 65 6f 66 28 57  nSrc-1)*sizeof(W
2c24d 68 65 72 65 4c 65 76 65 6c 29 29 3b 0a 20 20 70  hereLevel));.  p
2c24e 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44  WInfo = sqlite3D
2c24f 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2c250 0a 20 20 20 20 20 20 6e 42 79 74 65 57 49 6e 66  .      nByteWInf
2c251 6f 20 2b 20 0a 20 20 20 20 20 20 73 69 7a 65 6f  o + .      sizeo
2c252 66 28 57 68 65 72 65 43 6c 61 75 73 65 29 20 2b  f(WhereClause) +
2c253 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 57 68  .      sizeof(Wh
2c254 65 72 65 4d 61 73 6b 53 65 74 29 0a 20 20 29 3b  ereMaskSet).  );
2c255 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2c256 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
2c257 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72  oto whereBeginEr
2c258 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 57 49 6e 66  ror;.  }.  pWInf
2c259 6f 2d 3e 6e 4c 65 76 65 6c 20 3d 20 70 54 61 62  o->nLevel = pTab
2c25a 4c 69 73 74 2d 3e 6e 53 72 63 3b 0a 20 20 70 57  List->nSrc;.  pW
2c25b 49 6e 66 6f 2d 3e 70 50 61 72 73 65 20 3d 20 70  Info->pParse = p
2c25c 50 61 72 73 65 3b 0a 20 20 70 57 49 6e 66 6f 2d  Parse;.  pWInfo-
2c25d 3e 70 54 61 62 4c 69 73 74 20 3d 20 70 54 61 62  >pTabList = pTab
2c25e 4c 69 73 74 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e  List;.  pWInfo->
2c25f 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33  iBreak = sqlite3
2c260 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2c261 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 72 65 67 52  ;.  pWInfo->regR
2c262 6f 77 53 65 74 20 3d 20 28 77 63 74 72 6c 46 6c  owSet = (wctrlFl
2c263 61 67 73 20 26 20 57 48 45 52 45 5f 46 49 4c 4c  ags & WHERE_FILL
2c264 5f 52 4f 57 53 45 54 29 20 3f 20 72 65 67 52 6f  _ROWSET) ? regRo
2c265 77 53 65 74 20 3a 20 2d 31 3b 0a 20 20 70 57 49  wSet : -1;.  pWI
2c266 6e 66 6f 2d 3e 70 57 43 20 3d 20 70 57 43 20 3d  nfo->pWC = pWC =
2c267 20 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 29   (WhereClause *)
2c268 26 28 28 75 38 20 2a 29 70 57 49 6e 66 6f 29 5b  &((u8 *)pWInfo)[
2c269 6e 42 79 74 65 57 49 6e 66 6f 5d 3b 0a 20 20 70  nByteWInfo];.  p
2c26a 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67  WInfo->wctrlFlag
2c26b 73 20 3d 20 77 63 74 72 6c 46 6c 61 67 73 3b 0a  s = wctrlFlags;.
2c26c 20 20 70 4d 61 73 6b 53 65 74 20 3d 20 28 57 68    pMaskSet = (Wh
2c26d 65 72 65 4d 61 73 6b 53 65 74 2a 29 26 70 57 43  ereMaskSet*)&pWC
2c26e 5b 31 5d 3b 0a 0a 20 20 2f 2a 20 53 70 6c 69 74  [1];..  /* Split
2c26f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2c270 65 20 69 6e 74 6f 20 73 65 70 61 72 61 74 65 20  e into separate 
2c271 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77  subexpressions w
2c272 68 65 72 65 20 65 61 63 68 0a 20 20 2a 2a 20 73  here each.  ** s
2c273 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  ubexpression is 
2c274 73 65 70 61 72 61 74 65 64 20 62 79 20 61 6e 20  separated by an 
2c275 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20  AND operator..  
2c276 2a 2f 0a 20 20 69 6e 69 74 4d 61 73 6b 53 65 74  */.  initMaskSet
2c277 28 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 77 68  (pMaskSet);.  wh
2c278 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 70 57  ereClauseInit(pW
2c279 43 2c 20 70 50 61 72 73 65 2c 20 70 4d 61 73 6b  C, pParse, pMask
2c27a 53 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Set);.  sqlite3E
2c27b 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73  xprCodeConstants
2c27c 28 70 50 61 72 73 65 2c 20 70 57 68 65 72 65 29  (pParse, pWhere)
2c27d 3b 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28 70  ;.  whereSplit(p
2c27e 57 43 2c 20 70 57 68 65 72 65 2c 20 54 4b 5f 41  WC, pWhere, TK_A
2c27f 4e 44 29 3b 0a 20 20 20 20 0a 20 20 2f 2a 20 53  ND);.    .  /* S
2c280 70 65 63 69 61 6c 20 63 61 73 65 3a 20 61 20 57  pecial case: a W
2c281 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74  HERE clause that
2c282 20 69 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20 45   is constant.  E
2c283 76 61 6c 75 61 74 65 20 74 68 65 0a 20 20 2a 2a  valuate the.  **
2c284 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20   expression and 
2c285 65 69 74 68 65 72 20 6a 75 6d 70 20 6f 76 65 72  either jump over
2c286 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65   all of the code
2c287 20 6f 72 20 66 61 6c 6c 20 74 68 72 75 2e 0a 20   or fall thru.. 
2c288 20 2a 2f 0a 20 20 69 66 28 20 70 57 68 65 72 65   */.  if( pWhere
2c289 20 26 26 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e   && (pTabList->n
2c28a 53 72 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  Src==0 || sqlite
2c28b 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
2c28c 6f 74 4a 6f 69 6e 28 70 57 68 65 72 65 29 29 20  otJoin(pWhere)) 
2c28d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
2c28e 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
2c28f 2c 20 70 57 68 65 72 65 2c 20 70 57 49 6e 66 6f  , pWhere, pWInfo
2c290 2d 3e 69 42 72 65 61 6b 2c 20 53 51 4c 49 54 45  ->iBreak, SQLITE
2c291 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2c292 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20    pWhere = 0;.  
2c293 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 61  }..  /* Assign a
2c294 20 62 69 74 20 66 72 6f 6d 20 74 68 65 20 62 69   bit from the bi
2c295 74 6d 61 73 6b 20 74 6f 20 65 76 65 72 79 20 74  tmask to every t
2c296 65 72 6d 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  erm in the FROM 
2c297 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a  clause..  **.  *
2c298 2a 20 57 68 65 6e 20 61 73 73 69 67 6e 69 6e 67  * When assigning
2c299 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73 20   bitmask values 
2c29a 74 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 63  to FROM clause c
2c29b 75 72 73 6f 72 73 2c 20 69 74 20 6d 75 73 74 20  ursors, it must 
2c29c 62 65 0a 20 20 2a 2a 20 74 68 65 20 63 61 73 65  be.  ** the case
2c29d 20 74 68 61 74 20 69 66 20 58 20 69 73 20 74 68   that if X is th
2c29e 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68  e bitmask for th
2c29f 65 20 4e 2d 74 68 20 46 52 4f 4d 20 63 6c 61 75  e N-th FROM clau
2c2a0 73 65 20 74 65 72 6d 20 74 68 65 6e 0a 20 20 2a  se term then.  *
2c2a1 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f  * the bitmask fo
2c2a2 72 20 61 6c 6c 20 46 52 4f 4d 20 63 6c 61 75 73  r all FROM claus
2c2a3 65 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20 6c  e terms to the l
2c2a4 65 66 74 20 6f 66 20 74 68 65 20 4e 2d 74 68 20  eft of the N-th 
2c2a5 74 65 72 6d 0a 20 20 2a 2a 20 69 73 20 28 58 2d  term.  ** is (X-
2c2a6 31 29 2e 20 20 20 41 6e 20 65 78 70 72 65 73 73  1).   An express
2c2a7 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20  ion from the ON 
2c2a8 63 6c 61 75 73 65 20 6f 66 20 61 20 4c 45 46 54  clause of a LEFT
2c2a9 20 4a 4f 49 4e 20 63 61 6e 20 75 73 65 0a 20 20   JOIN can use.  
2c2aa 2a 2a 20 69 74 73 20 45 78 70 72 2e 69 52 69 67  ** its Expr.iRig
2c2ab 68 74 4a 6f 69 6e 54 61 62 6c 65 20 76 61 6c 75  htJoinTable valu
2c2ac 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 62 69  e to find the bi
2c2ad 74 6d 61 73 6b 20 6f 66 20 74 68 65 20 72 69 67  tmask of the rig
2c2ae 68 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 66  ht table.  ** of
2c2af 20 74 68 65 20 6a 6f 69 6e 2e 20 20 53 75 62 74   the join.  Subt
2c2b0 72 61 63 74 69 6e 67 20 6f 6e 65 20 66 72 6f 6d  racting one from
2c2b1 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65   the right table
2c2b2 20 62 69 74 6d 61 73 6b 20 67 69 76 65 73 20 61   bitmask gives a
2c2b3 0a 20 20 2a 2a 20 62 69 74 6d 61 73 6b 20 66 6f  .  ** bitmask fo
2c2b4 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20  r all tables to 
2c2b5 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20  the left of the 
2c2b6 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 69 6e 67 20 74  join.  Knowing t
2c2b7 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20  he bitmask.  ** 
2c2b8 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74  for all tables t
2c2b9 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 61 20  o the left of a 
2c2ba 6c 65 66 74 20 6a 6f 69 6e 20 69 73 20 69 6d 70  left join is imp
2c2bb 6f 72 74 61 6e 74 2e 20 20 54 69 63 6b 65 74 20  ortant.  Ticket 
2c2bc 23 33 30 31 35 2e 0a 20 20 2a 2f 0a 20 20 66 6f  #3015..  */.  fo
2c2bd 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73  r(i=0; i<pTabLis
2c2be 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  t->nSrc; i++){. 
2c2bf 20 20 20 63 72 65 61 74 65 4d 61 73 6b 28 70 4d     createMask(pM
2c2c0 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74  askSet, pTabList
2c2c1 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b  ->a[i].iCursor);
2c2c2 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45  .  }.#ifndef NDE
2c2c3 42 55 47 0a 20 20 7b 0a 20 20 20 20 42 69 74 6d  BUG.  {.    Bitm
2c2c4 61 73 6b 20 74 6f 54 68 65 4c 65 66 74 20 3d 20  ask toTheLeft = 
2c2c5 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
2c2c6 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2c2c7 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 69  ; i++){.      Bi
2c2c8 74 6d 61 73 6b 20 6d 20 3d 20 67 65 74 4d 61 73  tmask m = getMas
2c2c9 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62  k(pMaskSet, pTab
2c2ca 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73  List->a[i].iCurs
2c2cb 6f 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  or);.      asser
2c2cc 74 28 20 28 6d 2d 31 29 3d 3d 74 6f 54 68 65 4c  t( (m-1)==toTheL
2c2cd 65 66 74 20 29 3b 0a 20 20 20 20 20 20 74 6f 54  eft );.      toT
2c2ce 68 65 4c 65 66 74 20 7c 3d 20 6d 3b 0a 20 20 20  heLeft |= m;.   
2c2cf 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
2c2d0 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 61 6c 6c 20   /* Analyze all 
2c2d1 6f 66 20 74 68 65 20 73 75 62 65 78 70 72 65 73  of the subexpres
2c2d2 73 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61  sions.  Note tha
2c2d3 74 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 29 20  t exprAnalyze() 
2c2d4 6d 69 67 68 74 0a 20 20 2a 2a 20 61 64 64 20 6e  might.  ** add n
2c2d5 65 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73  ew virtual terms
2c2d6 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66   onto the end of
2c2d7 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2c2d8 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a 20 20  e.  We do not.  
2c2d9 2a 2a 20 77 61 6e 74 20 74 6f 20 61 6e 61 6c 79  ** want to analy
2c2da 7a 65 20 74 68 65 73 65 20 76 69 72 74 75 61 6c  ze these virtual
2c2db 20 74 65 72 6d 73 2c 20 73 6f 20 73 74 61 72 74   terms, so start
2c2dc 20 61 6e 61 6c 79 7a 69 6e 67 20 61 74 20 74 68   analyzing at th
2c2dd 65 20 65 6e 64 0a 20 20 2a 2a 20 61 6e 64 20 77  e end.  ** and w
2c2de 6f 72 6b 20 66 6f 72 77 61 72 64 20 73 6f 20 74  ork forward so t
2c2df 68 61 74 20 74 68 65 20 61 64 64 65 64 20 76 69  hat the added vi
2c2e0 72 74 75 61 6c 20 74 65 72 6d 73 20 61 72 65 20  rtual terms are 
2c2e1 6e 65 76 65 72 20 70 72 6f 63 65 73 73 65 64 2e  never processed.
2c2e2 0a 20 20 2a 2f 0a 20 20 65 78 70 72 41 6e 61 6c  .  */.  exprAnal
2c2e3 79 7a 65 41 6c 6c 28 70 54 61 62 4c 69 73 74 2c  yzeAll(pTabList,
2c2e4 20 70 57 43 29 3b 0a 20 20 69 66 28 20 64 62 2d   pWC);.  if( db-
2c2e5 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2c2e6 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42  .    goto whereB
2c2e7 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a  eginError;.  }..
2c2e8 20 20 2f 2a 20 43 68 6f 73 65 20 74 68 65 20 62    /* Chose the b
2c2e9 65 73 74 20 69 6e 64 65 78 20 74 6f 20 75 73 65  est index to use
2c2ea 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20   for each table 
2c2eb 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2c2ec 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  se..  **.  ** Th
2c2ed 69 73 20 6c 6f 6f 70 20 66 69 6c 6c 73 20 69 6e  is loop fills in
2c2ee 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
2c2ef 69 65 6c 64 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ields:.  **.  **
2c2f0 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70     pWInfo->a[].p
2c2f1 49 64 78 20 20 20 20 20 20 54 68 65 20 69 6e 64  Idx      The ind
2c2f2 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68  ex to use for th
2c2f3 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  is level of the 
2c2f4 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 20 20 70 57 49  loop..  **   pWI
2c2f5 6e 66 6f 2d 3e 61 5b 5d 2e 77 73 46 6c 61 67 73  nfo->a[].wsFlags
2c2f6 20 20 20 57 48 45 52 45 5f 78 78 78 20 66 6c 61     WHERE_xxx fla
2c2f7 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  gs associated wi
2c2f8 74 68 20 70 49 64 78 0a 20 20 2a 2a 20 20 20 70  th pIdx.  **   p
2c2f9 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 6e 45 71 20 20  WInfo->a[].nEq  
2c2fa 20 20 20 20 20 54 68 65 20 6e 75 6d 62 65 72 20       The number 
2c2fb 6f 66 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e  of == and IN con
2c2fc 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 20 20  straints.  **   
2c2fd 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f  pWInfo->a[].iFro
2c2fe 6d 20 20 20 20 20 57 68 69 63 68 20 74 65 72 6d  m     Which term
2c2ff 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
2c300 75 73 65 20 69 73 20 62 65 69 6e 67 20 63 6f 64  use is being cod
2c301 65 64 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  ed.  **   pWInfo
2c302 2d 3e 61 5b 5d 2e 69 54 61 62 43 75 72 20 20 20  ->a[].iTabCur   
2c303 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
2c304 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
2c305 20 74 61 62 6c 65 0a 20 20 2a 2a 20 20 20 70 57   table.  **   pW
2c306 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 49 64 78 43 75  Info->a[].iIdxCu
2c307 72 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72  r   The VDBE cur
2c308 73 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65  sor for the inde
2c309 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  x.  **   pWInfo-
2c30a 3e 61 5b 5d 2e 70 54 65 72 6d 20 20 20 20 20 57  >a[].pTerm     W
2c30b 68 65 6e 20 77 73 46 6c 61 67 73 3d 3d 57 4f 5f  hen wsFlags==WO_
2c30c 4f 52 2c 20 74 68 65 20 4f 52 2d 63 6c 61 75 73  OR, the OR-claus
2c30d 65 20 74 65 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a  e term.  **.  **
2c30e 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20   This loop also 
2c30f 66 69 67 75 72 65 73 20 6f 75 74 20 74 68 65 20  figures out the 
2c310 6e 65 73 74 69 6e 67 20 6f 72 64 65 72 20 6f 66  nesting order of
2c311 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
2c312 52 4f 4d 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e  ROM.  ** clause.
2c313 0a 20 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79  .  */.  notReady
2c314 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a   = ~(Bitmask)0;.
2c315 20 20 70 54 61 62 49 74 65 6d 20 3d 20 70 54 61    pTabItem = pTa
2c316 62 4c 69 73 74 2d 3e 61 3b 0a 20 20 70 4c 65 76  bList->a;.  pLev
2c317 65 6c 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 3b 0a  el = pWInfo->a;.
2c318 20 20 61 6e 64 46 6c 61 67 73 20 3d 20 7e 30 3b    andFlags = ~0;
2c319 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22  .  WHERETRACE(("
2c31a 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 53 74  *** Optimizer St
2c31b 61 72 74 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20  art ***\n"));.  
2c31c 66 6f 72 28 69 3d 69 46 72 6f 6d 3d 30 2c 20 70  for(i=iFrom=0, p
2c31d 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b  Level=pWInfo->a;
2c31e 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   i<pTabList->nSr
2c31f 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b  c; i++, pLevel++
2c320 29 7b 0a 20 20 20 20 57 68 65 72 65 43 6f 73 74  ){.    WhereCost
2c321 20 62 65 73 74 50 6c 61 6e 3b 20 20 20 20 20 20   bestPlan;      
2c322 20 20 20 2f 2a 20 4d 6f 73 74 20 65 66 66 69 63     /* Most effic
2c323 69 65 6e 74 20 70 6c 61 6e 20 73 65 65 6e 20 73  ient plan seen s
2c324 6f 20 66 61 72 20 2a 2f 0a 20 20 20 20 49 6e 64  o far */.    Ind
2c325 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
2c326 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2c327 78 20 66 6f 72 20 46 52 4f 4d 20 74 61 62 6c 65  x for FROM table
2c328 20 61 74 20 70 54 61 62 49 74 65 6d 20 2a 2f 0a   at pTabItem */.
2c329 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20      int j;      
2c32a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c32b 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
2c32c 76 65 72 20 46 52 4f 4d 20 74 61 62 6c 65 73 20  ver FROM tables 
2c32d 2a 2f 0a 20 20 20 20 69 6e 74 20 62 65 73 74 4a  */.    int bestJ
2c32e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2c32f 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20     /* The value 
2c330 6f 66 20 6a 20 2a 2f 0a 20 20 20 20 42 69 74 6d  of j */.    Bitm
2c331 61 73 6b 20 6d 3b 20 20 20 20 20 20 20 20 20 20  ask m;          
2c332 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61          /* Bitma
2c333 73 6b 20 76 61 6c 75 65 20 66 6f 72 20 6a 20 6f  sk value for j o
2c334 72 20 62 65 73 74 4a 20 2a 2f 0a 20 20 20 20 69  r bestJ */.    i
2c335 6e 74 20 6f 6e 63 65 20 3d 20 30 3b 20 20 20 20  nt once = 0;    
2c336 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
2c337 75 65 20 77 68 65 6e 20 66 69 72 73 74 20 74 61  ue when first ta
2c338 62 6c 65 20 69 73 20 73 65 65 6e 20 2a 2f 0a 0a  ble is seen */..
2c339 20 20 20 20 6d 65 6d 73 65 74 28 26 62 65 73 74      memset(&best
2c33a 50 6c 61 6e 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Plan, 0, sizeof(
2c33b 62 65 73 74 50 6c 61 6e 29 29 3b 0a 20 20 20 20  bestPlan));.    
2c33c 62 65 73 74 50 6c 61 6e 2e 72 43 6f 73 74 20 3d  bestPlan.rCost =
2c33d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 3b   SQLITE_BIG_DBL;
2c33e 0a 20 20 20 20 66 6f 72 28 6a 3d 69 46 72 6f 6d  .    for(j=iFrom
2c33f 2c 20 70 54 61 62 49 74 65 6d 3d 26 70 54 61 62  , pTabItem=&pTab
2c340 4c 69 73 74 2d 3e 61 5b 6a 5d 3b 20 6a 3c 70 54  List->a[j]; j<pT
2c341 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 6a 2b  abList->nSrc; j+
2c342 2b 2c 20 70 54 61 62 49 74 65 6d 2b 2b 29 7b 0a  +, pTabItem++){.
2c343 20 20 20 20 20 20 69 6e 74 20 64 6f 4e 6f 74 52        int doNotR
2c344 65 6f 72 64 65 72 3b 20 20 2f 2a 20 54 72 75 65  eorder;  /* True
2c345 20 69 66 20 74 68 69 73 20 74 61 62 6c 65 20 73   if this table s
2c346 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 6f  hould not be reo
2c347 72 64 65 72 65 64 20 2a 2f 0a 20 20 20 20 20 20  rdered */.      
2c348 57 68 65 72 65 43 6f 73 74 20 73 43 6f 73 74 3b  WhereCost sCost;
2c349 20 20 20 2f 2a 20 43 6f 73 74 20 69 6e 66 6f 72     /* Cost infor
2c34a 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 62 65 73 74  mation from best
2c34b 49 6e 64 65 78 28 29 20 2a 2f 0a 0a 20 20 20 20  Index() */..    
2c34c 20 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 3d    doNotReorder =
2c34d 20 20 28 70 54 61 62 49 74 65 6d 2d 3e 6a 6f 69    (pTabItem->joi
2c34e 6e 74 79 70 65 20 26 20 28 4a 54 5f 4c 45 46 54  ntype & (JT_LEFT
2c34f 7c 4a 54 5f 43 52 4f 53 53 29 29 21 3d 30 3b 0a  |JT_CROSS))!=0;.
2c350 20 20 20 20 20 20 69 66 28 20 6f 6e 63 65 20 26        if( once &
2c351 26 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29  & doNotReorder )
2c352 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 6d 20   break;.      m 
2c353 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53  = getMask(pMaskS
2c354 65 74 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  et, pTabItem->iC
2c355 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66  ursor);.      if
2c356 28 20 28 6d 20 26 20 6e 6f 74 52 65 61 64 79 29  ( (m & notReady)
2c357 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
2c358 66 28 20 6a 3d 3d 69 46 72 6f 6d 20 29 20 69 46  f( j==iFrom ) iF
2c359 72 6f 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 63  rom++;.        c
2c35a 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
2c35b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2c35c 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 20 29 3b  TabItem->pTab );
2c35d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c35e 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2c35f 45 0a 20 20 20 20 20 20 69 66 28 20 49 73 56 69  E.      if( IsVi
2c360 72 74 75 61 6c 28 70 54 61 62 49 74 65 6d 2d 3e  rtual(pTabItem->
2c361 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20  pTab) ){.       
2c362 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2c363 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 2f  nfo *pVtabIdx; /
2c364 2a 20 43 75 72 72 65 6e 74 20 76 69 72 74 75 61  * Current virtua
2c365 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20  l index */.     
2c366 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
2c367 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78 49 6e 66  _info **ppIdxInf
2c368 6f 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 6a  o = &pWInfo->a[j
2c369 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20 20 20  ].pIdxInfo;.    
2c36a 20 20 20 20 73 43 6f 73 74 2e 72 43 6f 73 74 20      sCost.rCost 
2c36b 3d 20 62 65 73 74 56 69 72 74 75 61 6c 49 6e 64  = bestVirtualInd
2c36c 65 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20  ex(pParse, pWC, 
2c36d 70 54 61 62 49 74 65 6d 2c 20 6e 6f 74 52 65 61  pTabItem, notRea
2c36e 64 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  dy,.            
2c36f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c370 20 20 20 20 20 20 20 20 20 20 20 70 70 4f 72 64             ppOrd
2c371 65 72 42 79 20 3f 20 2a 70 70 4f 72 64 65 72 42  erBy ? *ppOrderB
2c372 79 20 3a 20 30 2c 20 69 3d 3d 30 2c 0a 20 20 20  y : 0, i==0,.   
2c373 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c374 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c375 20 20 20 20 70 70 49 64 78 49 6e 66 6f 29 3b 0a      ppIdxInfo);.
2c376 20 20 20 20 20 20 20 20 73 43 6f 73 74 2e 70 6c          sCost.pl
2c377 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45  an.wsFlags = WHE
2c378 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 3b  RE_VIRTUALTABLE;
2c379 0a 20 20 20 20 20 20 20 20 73 43 6f 73 74 2e 70  .        sCost.p
2c37a 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64 78 20 3d  lan.u.pVtabIdx =
2c37b 20 70 56 74 61 62 49 64 78 20 3d 20 2a 70 70 49   pVtabIdx = *ppI
2c37c 64 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20  dxInfo;.        
2c37d 69 66 28 20 70 56 74 61 62 49 64 78 20 26 26 20  if( pVtabIdx && 
2c37e 70 56 74 61 62 49 64 78 2d 3e 6f 72 64 65 72 42  pVtabIdx->orderB
2c37f 79 43 6f 6e 73 75 6d 65 64 20 29 7b 0a 20 20 20  yConsumed ){.   
2c380 20 20 20 20 20 20 20 73 43 6f 73 74 2e 70 6c 61         sCost.pla
2c381 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45 52  n.wsFlags = WHER
2c382 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 7c  E_VIRTUALTABLE |
2c383 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b 0a   WHERE_ORDERBY;.
2c384 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c385 20 20 73 43 6f 73 74 2e 70 6c 61 6e 2e 6e 45 71    sCost.plan.nEq
2c386 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 2f 2a   = 0;.        /*
2c387 20 28 64 6f 75 62 6c 65 29 32 20 49 6e 20 63 61   (double)2 In ca
2c388 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
2c389 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
2c38a 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ... */.        i
2c38b 66 28 20 28 53 51 4c 49 54 45 5f 42 49 47 5f 44  f( (SQLITE_BIG_D
2c38c 42 4c 2f 28 28 64 6f 75 62 6c 65 29 32 29 29 3c  BL/((double)2))<
2c38d 73 43 6f 73 74 2e 72 43 6f 73 74 20 29 7b 0a 20  sCost.rCost ){. 
2c38e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2c38f 63 6f 73 74 20 69 73 20 6e 6f 74 20 61 6c 6c 6f  cost is not allo
2c390 77 65 64 20 74 6f 20 62 65 20 6c 61 72 67 65 72  wed to be larger
2c391 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 42 49 47   than SQLITE_BIG
2c392 5f 44 42 4c 20 28 74 68 65 0a 20 20 20 20 20 20  _DBL (the.      
2c393 20 20 20 20 2a 2a 20 69 6e 69 74 61 6c 20 76 61      ** inital va
2c394 6c 75 65 20 6f 66 20 6c 6f 77 65 73 74 43 6f 73  lue of lowestCos
2c395 74 20 69 6e 20 74 68 69 73 20 6c 6f 6f 70 2e 20  t in this loop. 
2c396 49 66 20 69 74 20 69 73 2c 20 74 68 65 6e 0a 20  If it is, then. 
2c397 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20           ** the 
2c398 28 63 6f 73 74 3c 6c 6f 77 65 73 74 43 6f 73 74  (cost<lowestCost
2c399 29 20 74 65 73 74 20 62 65 6c 6f 77 20 77 69 6c  ) test below wil
2c39a 6c 20 6e 65 76 65 72 20 62 65 20 74 72 75 65 2e  l never be true.
2c39b 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 20 0a 20  .          */ . 
2c39c 20 20 20 20 20 20 20 20 20 2f 2a 20 28 64 6f 75           /* (dou
2c39d 62 6c 65 29 32 20 49 6e 20 63 61 73 65 20 6f 66  ble)2 In case of
2c39e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
2c39f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a  ATING_POINT... *
2c3a0 2f 0a 20 20 20 20 20 20 20 20 20 20 73 43 6f 73  /.          sCos
2c3a1 74 2e 72 43 6f 73 74 20 3d 20 28 53 51 4c 49 54  t.rCost = (SQLIT
2c3a2 45 5f 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62  E_BIG_DBL/((doub
2c3a3 6c 65 29 32 29 29 3b 0a 20 20 20 20 20 20 20 20  le)2));.        
2c3a4 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 0a 23  }.      }else .#
2c3a5 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20  endif.      {.  
2c3a6 20 20 20 20 20 20 62 65 73 74 49 6e 64 65 78 28        bestIndex(
2c3a7 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 54 61  pParse, pWC, pTa
2c3a8 62 49 74 65 6d 2c 20 6e 6f 74 52 65 61 64 79 2c  bItem, notReady,
2c3a9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c3aa 20 20 20 28 69 3d 3d 30 20 26 26 20 70 70 4f 72     (i==0 && ppOr
2c3ab 64 65 72 42 79 29 20 3f 20 2a 70 70 4f 72 64 65  derBy) ? *ppOrde
2c3ac 72 42 79 20 3a 20 30 2c 20 26 73 43 6f 73 74 29  rBy : 0, &sCost)
2c3ad 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c3ae 69 66 28 20 6f 6e 63 65 3d 3d 30 20 7c 7c 20 73  if( once==0 || s
2c3af 43 6f 73 74 2e 72 43 6f 73 74 3c 62 65 73 74 50  Cost.rCost<bestP
2c3b0 6c 61 6e 2e 72 43 6f 73 74 20 29 7b 0a 20 20 20  lan.rCost ){.   
2c3b1 20 20 20 20 20 6f 6e 63 65 20 3d 20 31 3b 0a 20       once = 1;. 
2c3b2 20 20 20 20 20 20 20 62 65 73 74 50 6c 61 6e 20         bestPlan 
2c3b3 3d 20 73 43 6f 73 74 3b 0a 20 20 20 20 20 20 20  = sCost;.       
2c3b4 20 62 65 73 74 4a 20 3d 20 6a 3b 0a 20 20 20 20   bestJ = j;.    
2c3b5 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 64 6f    }.      if( do
2c3b6 4e 6f 74 52 65 6f 72 64 65 72 20 29 20 62 72 65  NotReorder ) bre
2c3b7 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  ak;.    }.    as
2c3b8 73 65 72 74 28 20 6f 6e 63 65 20 29 3b 0a 20 20  sert( once );.  
2c3b9 20 20 61 73 73 65 72 74 28 20 6e 6f 74 52 65 61    assert( notRea
2c3ba 64 79 20 26 20 67 65 74 4d 61 73 6b 28 70 4d 61  dy & getMask(pMa
2c3bb 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d  skSet, pTabList-
2c3bc 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72 73 6f  >a[bestJ].iCurso
2c3bd 72 29 20 29 3b 0a 20 20 20 20 57 48 45 52 45 54  r) );.    WHERET
2c3be 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d  RACE(("*** Optim
2c3bf 69 7a 65 72 20 73 65 6c 65 63 74 73 20 74 61 62  izer selects tab
2c3c0 6c 65 20 25 64 20 66 6f 72 20 6c 6f 6f 70 20 25  le %d for loop %
2c3c1 64 5c 6e 22 2c 20 62 65 73 74 4a 2c 0a 20 20 20  d\n", bestJ,.   
2c3c2 20 20 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 70          pLevel-p
2c3c3 57 49 6e 66 6f 2d 3e 61 29 29 3b 0a 20 20 20 20  WInfo->a));.    
2c3c4 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e 70 6c  if( (bestPlan.pl
2c3c5 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2c3c6 52 45 5f 4f 52 44 45 52 42 59 29 21 3d 30 20 29  RE_ORDERBY)!=0 )
2c3c7 7b 0a 20 20 20 20 20 20 2a 70 70 4f 72 64 65 72  {.      *ppOrder
2c3c8 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  By = 0;.    }.  
2c3c9 20 20 61 6e 64 46 6c 61 67 73 20 26 3d 20 62 65    andFlags &= be
2c3ca 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c  stPlan.plan.wsFl
2c3cb 61 67 73 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  ags;.    pLevel-
2c3cc 3e 70 6c 61 6e 20 3d 20 62 65 73 74 50 6c 61 6e  >plan = bestPlan
2c3cd 2e 70 6c 61 6e 3b 0a 20 20 20 20 69 66 28 20 62  .plan;.    if( b
2c3ce 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46  estPlan.plan.wsF
2c3cf 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44  lags & WHERE_IND
2c3d0 45 58 45 44 20 29 7b 0a 20 20 20 20 20 20 70 4c  EXED ){.      pL
2c3d1 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20  evel->iIdxCur = 
2c3d2 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
2c3d3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2c3d4 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
2c3d5 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20   = -1;.    }.   
2c3d6 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65   notReady &= ~ge
2c3d7 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
2c3d8 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
2c3d9 4a 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  J].iCursor);.   
2c3da 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 20 3d   pLevel->iFrom =
2c3db 20 28 75 38 29 62 65 73 74 4a 3b 0a 0a 20 20 20   (u8)bestJ;..   
2c3dc 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 69   /* Check that i
2c3dd 66 20 74 68 65 20 74 61 62 6c 65 20 73 63 61 6e  f the table scan
2c3de 6e 65 64 20 62 79 20 74 68 69 73 20 6c 6f 6f 70  ned by this loop
2c3df 20 69 74 65 72 61 74 69 6f 6e 20 68 61 64 20 61   iteration had a
2c3e0 6e 0a 20 20 20 20 2a 2a 20 49 4e 44 45 58 45 44  n.    ** INDEXED
2c3e1 20 42 59 20 63 6c 61 75 73 65 20 61 74 74 61 63   BY clause attac
2c3e2 68 65 64 20 74 6f 20 69 74 2c 20 74 68 61 74 20  hed to it, that 
2c3e3 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20  the named index 
2c3e4 69 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20  is being.    ** 
2c3e5 75 73 65 64 20 66 6f 72 20 74 68 65 20 73 63 61  used for the sca
2c3e6 6e 2e 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20  n. If not, then 
2c3e7 71 75 65 72 79 20 63 6f 6d 70 69 6c 61 74 69 6f  query compilatio
2c3e8 6e 20 68 61 73 20 66 61 69 6c 65 64 2e 0a 20 20  n has failed..  
2c3e9 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65    ** Return an e
2c3ea 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rror..    */.   
2c3eb 20 70 49 64 78 20 3d 20 70 54 61 62 4c 69 73 74   pIdx = pTabList
2c3ec 2d 3e 61 5b 62 65 73 74 4a 5d 2e 70 49 6e 64 65  ->a[bestJ].pInde
2c3ed 78 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 20  x;.    if( pIdx 
2c3ee 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 62 65  ){.      if( (be
2c3ef 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c  stPlan.plan.wsFl
2c3f0 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45  ags & WHERE_INDE
2c3f1 58 45 44 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  XED)==0 ){.     
2c3f2 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2c3f3 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
2c3f4 6f 74 20 75 73 65 20 69 6e 64 65 78 3a 20 25 73  ot use index: %s
2c3f5 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b  ", pIdx->zName);
2c3f6 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 77 68  .        goto wh
2c3f7 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20  ereBeginError;. 
2c3f8 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2c3f9 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 49 4e 44      /* If an IND
2c3fa 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 69  EXED BY clause i
2c3fb 73 20 75 73 65 64 2c 20 74 68 65 20 62 65 73 74  s used, the best
2c3fc 49 6e 64 65 78 28 29 20 66 75 6e 63 74 69 6f 6e  Index() function
2c3fd 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 67   is.        ** g
2c3fe 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 6e  uaranteed to fin
2c3ff 64 20 74 68 65 20 69 6e 64 65 78 20 73 70 65 63  d the index spec
2c400 69 66 69 65 64 20 69 6e 20 74 68 65 20 49 4e 44  ified in the IND
2c401 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 0a 20  EXED BY clause. 
2c402 20 20 20 20 20 20 20 2a 2a 20 69 66 20 69 74 20         ** if it 
2c403 66 69 6e 64 20 61 6e 20 69 6e 64 65 78 20 61 74  find an index at
2c404 20 61 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20   all. */.       
2c405 20 61 73 73 65 72 74 28 20 62 65 73 74 50 6c 61   assert( bestPla
2c406 6e 2e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 70  n.plan.u.pIdx==p
2c407 49 64 78 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Idx );.      }. 
2c408 20 20 20 7d 0a 20 20 7d 0a 20 20 57 48 45 52 45     }.  }.  WHERE
2c409 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70 74 69  TRACE(("*** Opti
2c40a 6d 69 7a 65 72 20 46 69 6e 69 73 68 65 64 20 2a  mizer Finished *
2c40b 2a 2a 5c 6e 22 29 29 3b 0a 20 20 69 66 28 20 64  **\n"));.  if( d
2c40c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2c40d 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72  ){.    goto wher
2c40e 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d  eBeginError;.  }
2c40f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 6f  ..  /* If the to
2c410 74 61 6c 20 71 75 65 72 79 20 6f 6e 6c 79 20 73  tal query only s
2c411 65 6c 65 63 74 73 20 61 20 73 69 6e 67 6c 65 20  elects a single 
2c412 72 6f 77 2c 20 74 68 65 6e 20 74 68 65 20 4f 52  row, then the OR
2c413 44 45 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75  DER BY.  ** clau
2c414 73 65 20 69 73 20 69 72 72 65 6c 65 76 61 6e 74  se is irrelevant
2c415 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 61 6e  ..  */.  if( (an
2c416 64 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 55  dFlags & WHERE_U
2c417 4e 49 51 55 45 29 21 3d 30 20 26 26 20 70 70 4f  NIQUE)!=0 && ppO
2c418 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 2a 70  rderBy ){.    *p
2c419 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  pOrderBy = 0;.  
2c41a 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  }..  /* If the c
2c41b 61 6c 6c 65 72 20 69 73 20 61 6e 20 55 50 44 41  aller is an UPDA
2c41c 54 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61  TE or DELETE sta
2c41d 74 65 6d 65 6e 74 20 74 68 61 74 20 69 73 20 72  tement that is r
2c41e 65 71 75 65 73 74 69 6e 67 0a 20 20 2a 2a 20 74  equesting.  ** t
2c41f 6f 20 75 73 65 20 61 20 6f 6e 65 2d 70 61 73 73  o use a one-pass
2c420 20 61 6c 67 6f 72 69 74 68 6d 2c 20 64 65 74 65   algorithm, dete
2c421 72 6d 69 6e 65 20 69 66 20 74 68 69 73 20 69 73  rmine if this is
2c422 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 20 20   appropriate..  
2c423 2a 2a 20 54 68 65 20 6f 6e 65 2d 70 61 73 73 20  ** The one-pass 
2c424 61 6c 67 6f 72 69 74 68 6d 20 6f 6e 6c 79 20 77  algorithm only w
2c425 6f 72 6b 73 20 69 66 20 74 68 65 20 57 48 45 52  orks if the WHER
2c426 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61  E clause constra
2c427 69 6e 74 73 0a 20 20 2a 2a 20 74 68 65 20 73 74  ints.  ** the st
2c428 61 74 65 6d 65 6e 74 20 74 6f 20 75 70 64 61 74  atement to updat
2c429 65 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 2e 0a  e a single row..
2c42a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28    */.  assert( (
2c42b 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
2c42c 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52  RE_ONEPASS_DESIR
2c42d 45 44 29 3d 3d 30 20 7c 7c 20 70 57 49 6e 66 6f  ED)==0 || pWInfo
2c42e 2d 3e 6e 4c 65 76 65 6c 3d 3d 31 20 29 3b 0a 20  ->nLevel==1 );. 
2c42f 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73   if( (wctrlFlags
2c430 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53   & WHERE_ONEPASS
2c431 5f 44 45 53 49 52 45 44 29 21 3d 30 20 26 26 20  _DESIRED)!=0 && 
2c432 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48 45 52  (andFlags & WHER
2c433 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 29 7b 0a  E_UNIQUE)!=0 ){.
2c434 20 20 20 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e      pWInfo->okOn
2c435 65 50 61 73 73 20 3d 20 31 3b 0a 20 20 20 20 70  ePass = 1;.    p
2c436 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 70 6c 61 6e  WInfo->a[0].plan
2c437 2e 77 73 46 6c 61 67 73 20 26 3d 20 7e 57 48 45  .wsFlags &= ~WHE
2c438 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 7d  RE_IDX_ONLY;.  }
2c439 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 6c 6c 20  ..  /* Open all 
2c43a 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 70 54  tables in the pT
2c43b 61 62 4c 69 73 74 20 61 6e 64 20 61 6e 79 20 69  abList and any i
2c43c 6e 64 69 63 65 73 20 73 65 6c 65 63 74 65 64 20  ndices selected 
2c43d 66 6f 72 0a 20 20 2a 2a 20 73 65 61 72 63 68 69  for.  ** searchi
2c43e 6e 67 20 74 68 6f 73 65 20 74 61 62 6c 65 73 2e  ng those tables.
2c43f 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43  .  */.  sqlite3C
2c440 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
2c441 70 50 61 72 73 65 2c 20 2d 31 29 3b 20 2f 2a 20  pParse, -1); /* 
2c442 49 6e 73 65 72 74 20 74 68 65 20 63 6f 6f 6b 69  Insert the cooki
2c443 65 20 76 65 72 69 66 69 65 72 20 47 6f 74 6f 20  e verifier Goto 
2c444 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c  */.  for(i=0, pL
2c445 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20  evel=pWInfo->a; 
2c446 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2c447 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29  ; i++, pLevel++)
2c448 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  {.    Table *pTa
2c449 62 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  b;     /* Table 
2c44a 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69  to open */.    i
2c44b 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
2c44c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61  /* Index of data
2c44d 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
2c44e 74 61 62 6c 65 2f 69 6e 64 65 78 20 2a 2f 0a 0a  table/index */..
2c44f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2c450 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20  MIT_EXPLAIN.    
2c451 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c  if( pParse->expl
2c452 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  ain==2 ){.      
2c453 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20 20 20 20  char *zMsg;.    
2c454 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2c455 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
2c456 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76  pTabList->a[pLev
2c457 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20  el->iFrom];.    
2c458 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
2c459 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 54 41 42  MPrintf(db, "TAB
2c45a 4c 45 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a  LE %s", pItem->z
2c45b 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Name);.      if(
2c45c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29   pItem->zAlias )
2c45d 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  {.        zMsg =
2c45e 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
2c45f 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 41  (db, zMsg, "%s A
2c460 53 20 25 73 22 2c 20 7a 4d 73 67 2c 20 70 49 74  S %s", zMsg, pIt
2c461 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20  em->zAlias);.   
2c462 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28     }.      if( (
2c463 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2c464 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44  lags & WHERE_IND
2c465 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20  EXED)!=0 ){.    
2c466 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
2c467 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a  e3MAppendf(db, z
2c468 4d 73 67 2c 20 22 25 73 20 57 49 54 48 20 49 4e  Msg, "%s WITH IN
2c469 44 45 58 20 25 73 22 2c 0a 20 20 20 20 20 20 20  DEX %s",.       
2c46a 20 20 20 20 7a 4d 73 67 2c 20 70 4c 65 76 65 6c      zMsg, pLevel
2c46b 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a  ->plan.u.pIdx->z
2c46c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Name);.      }el
2c46d 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70  se if( pLevel->p
2c46e 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2c46f 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 29 7b 0a  ERE_MULTI_OR ){.
2c470 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
2c471 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
2c472 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 56 49 41  b, zMsg, "%s VIA
2c473 20 4d 55 4c 54 49 2d 49 4e 44 45 58 20 55 4e 49   MULTI-INDEX UNI
2c474 4f 4e 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20  ON", zMsg);.    
2c475 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76    }else if( pLev
2c476 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2c477 20 26 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f   & (WHERE_ROWID_
2c478 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52  EQ|WHERE_ROWID_R
2c479 41 4e 47 45 29 20 29 7b 0a 20 20 20 20 20 20 20  ANGE) ){.       
2c47a 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
2c47b 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
2c47c 2c 20 22 25 73 20 55 53 49 4e 47 20 50 52 49 4d  , "%s USING PRIM
2c47d 41 52 59 20 4b 45 59 22 2c 20 7a 4d 73 67 29 3b  ARY KEY", zMsg);
2c47e 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  .      }.#ifndef
2c47f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2c480 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20  TUALTABLE.      
2c481 65 6c 73 65 20 69 66 28 20 28 70 4c 65 76 65 6c  else if( (pLevel
2c482 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2c483 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41   WHERE_VIRTUALTA
2c484 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  BLE)!=0 ){.     
2c485 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
2c486 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 20  _info *pVtabIdx 
2c487 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  = pLevel->plan.u
2c488 2e 70 56 74 61 62 49 64 78 3b 0a 20 20 20 20 20  .pVtabIdx;.     
2c489 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65     zMsg = sqlite
2c48a 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d  3MAppendf(db, zM
2c48b 73 67 2c 20 22 25 73 20 56 49 52 54 55 41 4c 20  sg, "%s VIRTUAL 
2c48c 54 41 42 4c 45 20 49 4e 44 45 58 20 25 64 3a 25  TABLE INDEX %d:%
2c48d 73 22 2c 20 7a 4d 73 67 2c 0a 20 20 20 20 20 20  s", zMsg,.      
2c48e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56                pV
2c48f 74 61 62 49 64 78 2d 3e 69 64 78 4e 75 6d 2c 20  tabIdx->idxNum, 
2c490 70 56 74 61 62 49 64 78 2d 3e 69 64 78 53 74 72  pVtabIdx->idxStr
2c491 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
2c492 66 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76  f.      if( pLev
2c493 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2c494 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59   & WHERE_ORDERBY
2c495 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67   ){.        zMsg
2c496 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
2c497 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
2c498 20 4f 52 44 45 52 20 42 59 22 2c 20 7a 4d 73 67   ORDER BY", zMsg
2c499 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2c49a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c49b 70 34 28 76 2c 20 4f 50 5f 45 78 70 6c 61 69 6e  p4(v, OP_Explain
2c49c 2c 20 69 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72  , i, pLevel->iFr
2c49d 6f 6d 2c 20 30 2c 20 7a 4d 73 67 2c 20 50 34 5f  om, 0, zMsg, P4_
2c49e 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a  DYNAMIC);.    }.
2c49f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2c4a0 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f  _OMIT_EXPLAIN */
2c4a1 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20  .    pTabItem = 
2c4a2 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65  &pTabList->a[pLe
2c4a3 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20  vel->iFrom];.   
2c4a4 20 70 54 61 62 20 3d 20 70 54 61 62 49 74 65 6d   pTab = pTabItem
2c4a5 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 44 62 20  ->pTab;.    iDb 
2c4a6 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2c4a7 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
2c4a8 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
2c4a9 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 61 62  );.    if( (pTab
2c4aa 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
2c4ab 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c  Ephemeral)!=0 ||
2c4ac 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
2c4ad 20 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64   continue;.#ifnd
2c4ae 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2c4af 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
2c4b0 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
2c4b1 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2c4b2 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21  E_VIRTUALTABLE)!
2c4b3 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =0 ){.      int 
2c4b4 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d  iCur = pTabItem-
2c4b5 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  >iCursor;.      
2c4b6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c4b7 34 28 76 2c 20 4f 50 5f 56 4f 70 65 6e 2c 20 69  4(v, OP_VOpen, i
2c4b8 43 75 72 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  Cur, 0, 0,.     
2c4b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4ba 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29     (const char*)
2c4bb 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f  pTab->pVtab, P4_
2c4bc 56 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65  VTAB);.    }else
2c4bd 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
2c4be 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
2c4bf 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44  Flags & WHERE_ID
2c4c0 58 5f 4f 4e 4c 59 29 3d 3d 30 0a 20 20 20 20 20  X_ONLY)==0.     
2c4c1 20 20 20 20 26 26 20 28 77 63 74 72 6c 46 6c 61      && (wctrlFla
2c4c2 67 73 20 26 20 57 48 45 52 45 5f 4f 4d 49 54 5f  gs & WHERE_OMIT_
2c4c3 4f 50 45 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20  OPEN)==0 ){.    
2c4c4 20 20 69 6e 74 20 6f 70 20 3d 20 70 57 49 6e 66    int op = pWInf
2c4c5 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3f 20 4f  o->okOnePass ? O
2c4c6 50 5f 4f 70 65 6e 57 72 69 74 65 20 3a 20 4f 50  P_OpenWrite : OP
2c4c7 5f 4f 70 65 6e 52 65 61 64 3b 0a 20 20 20 20 20  _OpenRead;.     
2c4c8 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
2c4c9 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 49 74  e(pParse, pTabIt
2c4ca 65 6d 2d 3e 69 43 75 72 73 6f 72 2c 20 69 44 62  em->iCursor, iDb
2c4cb 2c 20 70 54 61 62 2c 20 6f 70 29 3b 0a 20 20 20  , pTab, op);.   
2c4cc 20 20 20 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e     if( !pWInfo->
2c4cd 6f 6b 4f 6e 65 50 61 73 73 20 26 26 20 70 54 61  okOnePass && pTa
2c4ce 62 2d 3e 6e 43 6f 6c 3c 42 4d 53 20 29 7b 0a 20  b->nCol<BMS ){. 
2c4cf 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62         Bitmask b
2c4d0 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 63 6f 6c   = pTabItem->col
2c4d1 55 73 65 64 3b 0a 20 20 20 20 20 20 20 20 69 6e  Used;.        in
2c4d2 74 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  t n = 0;.       
2c4d3 20 66 6f 72 28 3b 20 62 3b 20 62 3d 62 3e 3e 31   for(; b; b=b>>1
2c4d4 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20  , n++){}.       
2c4d5 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2c4d6 67 65 50 34 28 76 2c 20 73 71 6c 69 74 65 33 56  geP4(v, sqlite3V
2c4d7 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2c4d8 29 2d 31 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  )-1, SQLITE_INT_
2c4d9 54 4f 5f 50 54 52 28 6e 29 2c 20 50 34 5f 49 4e  TO_PTR(n), P4_IN
2c4da 54 33 32 29 3b 0a 20 20 20 20 20 20 20 20 61 73  T32);.        as
2c4db 73 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d 3e 6e  sert( n<=pTab->n
2c4dc 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Col );.      }. 
2c4dd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2c4de 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
2c4df 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
2c4e0 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61  ab->tnum, 0, pTa
2c4e1 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  b->zName);.    }
2c4e2 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 54 61  .    pLevel->iTa
2c4e3 62 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d  bCur = pTabItem-
2c4e4 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66  >iCursor;.    if
2c4e5 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
2c4e6 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2c4e7 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20  INDEXED)!=0 ){. 
2c4e8 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 78 20       Index *pIx 
2c4e9 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  = pLevel->plan.u
2c4ea 2e 70 49 64 78 3b 0a 20 20 20 20 20 20 4b 65 79  .pIdx;.      Key
2c4eb 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c  Info *pKey = sql
2c4ec 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
2c4ed 28 70 50 61 72 73 65 2c 20 70 49 78 29 3b 0a 20  (pParse, pIx);. 
2c4ee 20 20 20 20 20 69 6e 74 20 69 49 64 78 43 75 72       int iIdxCur
2c4ef 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43   = pLevel->iIdxC
2c4f0 75 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ur;.      assert
2c4f1 28 20 70 49 78 2d 3e 70 53 63 68 65 6d 61 3d 3d  ( pIx->pSchema==
2c4f2 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
2c4f3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
2c4f4 49 64 78 43 75 72 3e 3d 30 20 29 3b 0a 20 20 20  IdxCur>=0 );.   
2c4f5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c4f6 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52  dOp4(v, OP_OpenR
2c4f7 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49  ead, iIdxCur, pI
2c4f8 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20  x->tnum, iDb,.  
2c4f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4fa 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
2c4fb 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
2c4fc 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 56 64  NDOFF);.      Vd
2c4fd 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
2c4fe 73 22 2c 20 70 49 78 2d 3e 7a 4e 61 6d 65 29 29  s", pIx->zName))
2c4ff 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2c500 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
2c501 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
2c502 3b 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e  ;.  }.  pWInfo->
2c503 69 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  iTop = sqlite3Vd
2c504 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2c505 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ;..  /* Generate
2c506 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 6f 20   the code to do 
2c507 74 68 65 20 73 65 61 72 63 68 2e 20 20 45 61 63  the search.  Eac
2c508 68 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74  h iteration of t
2c509 68 65 20 66 6f 72 0a 20 20 2a 2a 20 6c 6f 6f 70  he for.  ** loop
2c50a 20 62 65 6c 6f 77 20 67 65 6e 65 72 61 74 65 73   below generates
2c50b 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67   code for a sing
2c50c 6c 65 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 6f  le nested loop o
2c50d 66 20 74 68 65 20 56 4d 0a 20 20 2a 2a 20 70 72  f the VM.  ** pr
2c50e 6f 67 72 61 6d 2e 0a 20 20 2a 2f 0a 20 20 6e 6f  ogram..  */.  no
2c50f 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61  tReady = ~(Bitma
2c510 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  sk)0;.  for(i=0;
2c511 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   i<pTabList->nSr
2c512 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 74  c; i++){.    not
2c513 52 65 61 64 79 20 3d 20 63 6f 64 65 4f 6e 65 4c  Ready = codeOneL
2c514 6f 6f 70 53 74 61 72 74 28 70 57 49 6e 66 6f 2c  oopStart(pWInfo,
2c515 20 69 2c 20 77 63 74 72 6c 46 6c 61 67 73 2c 20   i, wctrlFlags, 
2c516 6e 6f 74 52 65 61 64 79 29 3b 0a 20 20 20 20 70  notReady);.    p
2c517 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65  WInfo->iContinue
2c518 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e   = pWInfo->a[i].
2c519 61 64 64 72 43 6f 6e 74 3b 0a 20 20 7d 0a 0a 23  addrCont;.  }..#
2c51a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
2c51b 54 20 20 2f 2a 20 46 6f 72 20 74 65 73 74 69 6e  T  /* For testin
2c51c 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20  g and debugging 
2c51d 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 2f 2a  use only */.  /*
2c51e 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 20 71   Record in the q
2c51f 75 65 72 79 20 70 6c 61 6e 20 69 6e 66 6f 72 6d  uery plan inform
2c520 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
2c521 63 75 72 72 65 6e 74 20 74 61 62 6c 65 0a 20 20  current table.  
2c522 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  ** and the index
2c523 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
2c524 69 74 20 28 69 66 20 61 6e 79 29 2e 20 20 49 66  it (if any).  If
2c525 20 74 68 65 20 74 61 62 6c 65 20 69 74 73 65 6c   the table itsel
2c526 66 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 75 73  f.  ** is not us
2c527 65 64 2c 20 69 74 73 20 6e 61 6d 65 20 69 73 20  ed, its name is 
2c528 6a 75 73 74 20 27 7b 7d 27 2e 20 20 49 66 20 6e  just '{}'.  If n
2c529 6f 20 69 6e 64 65 78 20 69 73 20 75 73 65 64 0a  o index is used.
2c52a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 69    ** the index i
2c52b 73 20 6c 69 73 74 65 64 20 61 73 20 22 7b 7d 22  s listed as "{}"
2c52c 2e 20 20 49 66 20 74 68 65 20 70 72 69 6d 61 72  .  If the primar
2c52d 79 20 6b 65 79 20 69 73 20 75 73 65 64 20 74 68  y key is used th
2c52e 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6e 61 6d  e.  ** index nam
2c52f 65 20 69 73 20 27 2a 27 2e 0a 20 20 2a 2f 0a 20  e is '*'..  */. 
2c530 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
2c531 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29  List->nSrc; i++)
2c532 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20  {.    char *z;. 
2c533 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 70 4c     int n;.    pL
2c534 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e  evel = &pWInfo->
2c535 61 5b 69 5d 3b 0a 20 20 20 20 70 54 61 62 49 74  a[i];.    pTabIt
2c536 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e  em = &pTabList->
2c537 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d  a[pLevel->iFrom]
2c538 3b 0a 20 20 20 20 7a 20 3d 20 70 54 61 62 49 74  ;.    z = pTabIt
2c539 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20  em->zAlias;.    
2c53a 69 66 28 20 7a 3d 3d 30 20 29 20 7a 20 3d 20 70  if( z==0 ) z = p
2c53b 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a  TabItem->pTab->z
2c53c 4e 61 6d 65 3b 0a 20 20 20 20 6e 20 3d 20 73 71  Name;.    n = sq
2c53d 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
2c53e 3b 0a 20 20 20 20 69 66 28 20 6e 2b 6e 51 50 6c  ;.    if( n+nQPl
2c53f 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c 69  an < sizeof(sqli
2c540 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29 2d  te3_query_plan)-
2c541 31 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  10 ){.      if( 
2c542 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2c543 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
2c544 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 20  _ONLY ){.       
2c545 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
2c546 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
2c547 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b 0a 20  an], "{}", 2);. 
2c548 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d         nQPlan +=
2c549 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   2;.      }else{
2c54a 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
2c54b 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70  &sqlite3_query_p
2c54c 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 7a 2c 20  lan[nQPlan], z, 
2c54d 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c  n);.        nQPl
2c54e 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 7d  an += n;.      }
2c54f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 71  .      sqlite3_q
2c550 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
2c551 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 7d  ++] = ' ';.    }
2c552 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
2c553 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2c554 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49  ags & WHERE_ROWI
2c555 44 5f 45 51 20 29 3b 0a 20 20 20 20 74 65 73 74  D_EQ );.    test
2c556 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c  case( pLevel->pl
2c557 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2c558 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 29  RE_ROWID_RANGE )
2c559 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  ;.    if( pLevel
2c55a 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2c55b 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51   (WHERE_ROWID_EQ
2c55c 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e  |WHERE_ROWID_RAN
2c55d 47 45 29 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  GE) ){.      mem
2c55e 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65  cpy(&sqlite3_que
2c55f 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c  ry_plan[nQPlan],
2c560 20 22 2a 20 22 2c 20 32 29 3b 0a 20 20 20 20 20   "* ", 2);.     
2c561 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20   nQPlan += 2;.  
2c562 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 4c 65    }else if( (pLe
2c563 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2c564 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
2c565 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e  D)!=0 ){.      n
2c566 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2c567 33 30 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  30(pLevel->plan.
2c568 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a  u.pIdx->zName);.
2c569 20 20 20 20 20 20 69 66 28 20 6e 2b 6e 51 50 6c        if( n+nQPl
2c56a 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c 69  an < sizeof(sqli
2c56b 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29 2d  te3_query_plan)-
2c56c 32 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  2 ){.        mem
2c56d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65  cpy(&sqlite3_que
2c56e 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c  ry_plan[nQPlan],
2c56f 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
2c570 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b  pIdx->zName, n);
2c571 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  .        nQPlan 
2c572 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 73 71  += n;.        sq
2c573 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
2c574 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27  [nQPlan++] = ' '
2c575 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2c576 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  lse{.      memcp
2c577 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79  y(&sqlite3_query
2c578 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22  _plan[nQPlan], "
2c579 7b 7d 20 22 2c 20 33 29 3b 0a 20 20 20 20 20 20  {} ", 3);.      
2c57a 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b 0a 20 20 20  nQPlan += 3;.   
2c57b 20 7d 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20   }.  }.  while( 
2c57c 6e 51 50 6c 61 6e 3e 30 20 26 26 20 73 71 6c 69  nQPlan>0 && sqli
2c57d 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e  te3_query_plan[n
2c57e 51 50 6c 61 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b  QPlan-1]==' ' ){
2c57f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75 65  .    sqlite3_que
2c580 72 79 5f 70 6c 61 6e 5b 2d 2d 6e 51 50 6c 61 6e  ry_plan[--nQPlan
2c581 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  ] = 0;.  }.  sql
2c582 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
2c583 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20 6e  nQPlan] = 0;.  n
2c584 51 50 6c 61 6e 20 3d 20 30 3b 0a 23 65 6e 64 69  QPlan = 0;.#endi
2c585 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  f /* SQLITE_TEST
2c586 20 2f 2f 20 54 65 73 74 69 6e 67 20 61 6e 64 20   // Testing and 
2c587 64 65 62 75 67 67 69 6e 67 20 75 73 65 20 6f 6e  debugging use on
2c588 6c 79 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 63 6f  ly */..  /* Reco
2c589 72 64 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74  rd the continuat
2c58a 69 6f 6e 20 61 64 64 72 65 73 73 20 69 6e 20 74  ion address in t
2c58b 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72  he WhereInfo str
2c58c 75 63 74 75 72 65 2e 20 20 54 68 65 6e 0a 20 20  ucture.  Then.  
2c58d 2a 2a 20 63 6c 65 61 6e 20 75 70 20 61 6e 64 20  ** clean up and 
2c58e 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 72  return..  */.  r
2c58f 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b 0a 0a 20  eturn pWInfo;.. 
2c590 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66   /* Jump here if
2c591 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a 2f   malloc fails */
2c592 0a 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72  .whereBeginError
2c593 3a 0a 20 20 77 68 65 72 65 49 6e 66 6f 46 72 65  :.  whereInfoFre
2c594 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20  e(db, pWInfo);. 
2c595 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
2c596 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65  .** Generate the
2c597 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52   end of the WHER
2c598 45 20 6c 6f 6f 70 2e 20 20 53 65 65 20 63 6f 6d  E loop.  See com
2c599 6d 65 6e 74 73 20 6f 6e 20 0a 2a 2a 20 73 71 6c  ments on .** sql
2c59a 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2c59b 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
2c59c 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
2c59d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2c59e 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72 65  oid sqlite3Where
2c59f 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 20 2a 70  End(WhereInfo *p
2c5a0 57 49 6e 66 6f 29 7b 0a 20 20 50 61 72 73 65 20  WInfo){.  Parse 
2c5a1 2a 70 50 61 72 73 65 20 3d 20 70 57 49 6e 66 6f  *pParse = pWInfo
2c5a2 2d 3e 70 50 61 72 73 65 3b 0a 20 20 56 64 62 65  ->pParse;.  Vdbe
2c5a3 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2c5a4 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  dbe;.  int i;.  
2c5a5 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2c5a6 65 6c 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  el;.  SrcList *p
2c5a7 54 61 62 4c 69 73 74 20 3d 20 70 57 49 6e 66 6f  TabList = pWInfo
2c5a8 2d 3e 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 71  ->pTabList;.  sq
2c5a9 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2c5aa 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 47 65  se->db;..  /* Ge
2c5ab 6e 65 72 61 74 65 20 6c 6f 6f 70 20 74 65 72 6d  nerate loop term
2c5ac 69 6e 61 74 69 6f 6e 20 63 6f 64 65 2e 0a 20 20  ination code..  
2c5ad 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  */.  sqlite3Expr
2c5ae 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61 63 68 65  ClearColumnCache
2c5af 28 70 50 61 72 73 65 2c 20 2d 31 29 3b 0a 20 20  (pParse, -1);.  
2c5b0 66 6f 72 28 69 3d 70 54 61 62 4c 69 73 74 2d 3e  for(i=pTabList->
2c5b1 6e 53 72 63 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  nSrc-1; i>=0; i-
2c5b2 2d 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d  -){.    pLevel =
2c5b3 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a   &pWInfo->a[i];.
2c5b4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2c5b5 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70  esolveLabel(v, p
2c5b6 4c 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 29  Level->addrCont)
2c5b7 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  ;.    if( pLevel
2c5b8 2d 3e 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b  ->op!=OP_Noop ){
2c5b9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c5ba 62 65 41 64 64 4f 70 32 28 76 2c 20 70 4c 65 76  beAddOp2(v, pLev
2c5bb 65 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e  el->op, pLevel->
2c5bc 70 31 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b  p1, pLevel->p2);
2c5bd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c5be 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70 4c  beChangeP5(v, pL
2c5bf 65 76 65 6c 2d 3e 70 35 29 3b 0a 20 20 20 20 7d  evel->p5);.    }
2c5c0 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
2c5c1 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2c5c2 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 26 26  WHERE_IN_ABLE &&
2c5c3 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
2c5c4 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 74 72  n>0 ){.      str
2c5c5 75 63 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b  uct InLoop *pIn;
2c5c6 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  .      int j;.  
2c5c7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2c5c8 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70  esolveLabel(v, p
2c5c9 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 29 3b  Level->addrNxt);
2c5ca 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 4c 65  .      for(j=pLe
2c5cb 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2c 20 70  vel->u.in.nIn, p
2c5cc 49 6e 3d 26 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  In=&pLevel->u.in
2c5cd 2e 61 49 6e 4c 6f 6f 70 5b 6a 2d 31 5d 3b 20 6a  .aInLoop[j-1]; j
2c5ce 3e 30 3b 20 6a 2d 2d 2c 20 70 49 6e 2d 2d 29 7b  >0; j--, pIn--){
2c5cf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c5d0 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2c5d1 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 2b 31  pIn->addrInTop+1
2c5d2 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2c5d3 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c5d4 4f 50 5f 4e 65 78 74 2c 20 70 49 6e 2d 3e 69 43  OP_Next, pIn->iC
2c5d5 75 72 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54  ur, pIn->addrInT
2c5d6 6f 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  op);.        sql
2c5d7 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2c5d8 28 76 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54  (v, pIn->addrInT
2c5d9 6f 70 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  op-1);.      }. 
2c5da 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2c5db 65 65 28 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75  ee(db, pLevel->u
2c5dc 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 29 3b 0a 20 20  .in.aInLoop);.  
2c5dd 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2c5de 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2c5df 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42  v, pLevel->addrB
2c5e0 72 6b 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65  rk);.    if( pLe
2c5e1 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29  vel->iLeftJoin )
2c5e2 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  {.      int addr
2c5e3 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
2c5e4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2c5e5 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 70 4c  (v, OP_IfPos, pL
2c5e6 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29  evel->iLeftJoin)
2c5e7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c5e8 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2c5e9 4e 75 6c 6c 52 6f 77 2c 20 70 54 61 62 4c 69 73  NullRow, pTabLis
2c5ea 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29  t->a[i].iCursor)
2c5eb 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76  ;.      if( pLev
2c5ec 65 6c 2d 3e 69 49 64 78 43 75 72 3e 3d 30 20 29  el->iIdxCur>=0 )
2c5ed 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2c5ee 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2c5ef 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65  P_NullRow, pLeve
2c5f0 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20  l->iIdxCur);.   
2c5f1 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
2c5f2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c5f3 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 4c 65 76  OP_Goto, 0, pLev
2c5f4 65 6c 2d 3e 61 64 64 72 46 69 72 73 74 29 3b 0a  el->addrFirst);.
2c5f5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c5f6 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2c5f7 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  r);.    }.  }.. 
2c5f8 20 2f 2a 20 54 68 65 20 22 62 72 65 61 6b 22 20   /* The "break" 
2c5f9 70 6f 69 6e 74 20 69 73 20 68 65 72 65 2c 20 6a  point is here, j
2c5fa 75 73 74 20 70 61 73 74 20 74 68 65 20 65 6e 64  ust past the end
2c5fb 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 6c 6f   of the outer lo
2c5fc 6f 70 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74 2e  op..  ** Set it.
2c5fd 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
2c5fe 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2c5ff 76 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61  v, pWInfo->iBrea
2c600 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20  k);..  /* Close 
2c601 61 6c 6c 20 6f 66 20 74 68 65 20 63 75 72 73 6f  all of the curso
2c602 72 73 20 74 68 61 74 20 77 65 72 65 20 6f 70 65  rs that were ope
2c603 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 57 68  ned by sqlite3Wh
2c604 65 72 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a 20  ereBegin..  */. 
2c605 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c   for(i=0, pLevel
2c606 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54  =pWInfo->a; i<pT
2c607 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
2c608 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20  +, pLevel++){.  
2c609 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2c60a 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 20  _item *pTabItem 
2c60b 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  = &pTabList->a[p
2c60c 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20  Level->iFrom];. 
2c60d 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
2c60e 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b   pTabItem->pTab;
2c60f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61  .    assert( pTa
2c610 62 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  b!=0 );.    if( 
2c611 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20  (pTab->tabFlags 
2c612 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 21  & TF_Ephemeral)!
2c613 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c  =0 || pTab->pSel
2c614 65 63 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ect ) continue;.
2c615 20 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d      if( (pWInfo-
2c616 3e 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48  >wctrlFlags & WH
2c617 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29 3d  ERE_OMIT_CLOSE)=
2c618 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
2c619 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61  !pWInfo->okOnePa
2c61a 73 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70  ss && (pLevel->p
2c61b 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2c61c 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30  ERE_IDX_ONLY)==0
2c61d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2c61e 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2c61f 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 54 61 62 49   OP_Close, pTabI
2c620 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20  tem->iCursor);. 
2c621 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2c622 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
2c623 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
2c624 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20  NDEXED)!=0 ){.  
2c625 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c626 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
2c627 6f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64  ose, pLevel->iId
2c628 78 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  xCur);.      }. 
2c629 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
2c62a 74 68 69 73 20 73 63 61 6e 20 75 73 65 73 20 61  this scan uses a
2c62b 6e 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f  n index, make co
2c62c 64 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73  de substitutions
2c62d 20 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20   to read data.  
2c62e 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e    ** from the in
2c62f 64 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63  dex in preferenc
2c630 65 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20  e to the table. 
2c631 53 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20  Sometimes, this 
2c632 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65  means.    ** the
2c633 20 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65   table need neve
2c634 72 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20  r be read from. 
2c635 54 68 69 73 20 69 73 20 61 20 70 65 72 66 6f 72  This is a perfor
2c636 6d 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20  mance boost,.   
2c637 20 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65 20   ** as the vdbe 
2c638 6c 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69  level waits unti
2c639 6c 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 72  l the table is r
2c63a 65 61 64 20 62 65 66 6f 72 65 20 61 63 74 75 61  ead before actua
2c63b 6c 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69  lly.    ** seeki
2c63c 6e 67 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  ng the table cur
2c63d 73 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72  sor to the recor
2c63e 64 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  d corresponding 
2c63f 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20  to the current. 
2c640 20 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69     ** position i
2c641 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20  n the index..   
2c642 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c   ** .    ** Call
2c643 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65  s to the code ge
2c644 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65  nerator in betwe
2c645 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  en sqlite3WhereB
2c646 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20  egin and.    ** 
2c647 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20  sqlite3WhereEnd 
2c648 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65  will have create
2c649 64 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65  d code that refe
2c64a 72 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65  rences the table
2c64b 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79  .    ** directly
2c64c 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61  .  This loop sca
2c64d 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65  ns all that code
2c64e 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63   looking for opc
2c64f 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74  odes.    ** that
2c650 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 74   reference the t
2c651 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74  able and convert
2c652 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f  s them into opco
2c653 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  des that.    ** 
2c654 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e  reference the in
2c655 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  dex..    */.    
2c656 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
2c657 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2c658 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b  E_INDEXED)!=0 ){
2c659 0a 20 20 20 20 20 20 69 6e 74 20 6b 2c 20 6a 2c  .      int k, j,
2c65a 20 6c 61 73 74 3b 0a 20 20 20 20 20 20 56 64 62   last;.      Vdb
2c65b 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20  eOp *pOp;.      
2c65c 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 70 4c  Index *pIdx = pL
2c65d 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64  evel->plan.u.pId
2c65e 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 75 73 65  x;.      int use
2c65f 49 6e 64 65 78 4f 6e 6c 79 20 3d 20 70 4c 65 76  IndexOnly = pLev
2c660 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2c661 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c   & WHERE_IDX_ONL
2c662 59 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  Y;..      assert
2c663 28 20 70 49 64 78 21 3d 30 20 29 3b 0a 20 20 20  ( pIdx!=0 );.   
2c664 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33     pOp = sqlite3
2c665 56 64 62 65 47 65 74 4f 70 28 76 2c 20 70 57 49  VdbeGetOp(v, pWI
2c666 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20 20 20 20  nfo->iTop);.    
2c667 20 20 6c 61 73 74 20 3d 20 73 71 6c 69 74 65 33    last = sqlite3
2c668 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2c669 76 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d  v);.      for(k=
2c66a 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20 6b 3c  pWInfo->iTop; k<
2c66b 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70 2b 2b  last; k++, pOp++
2c66c 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
2c66d 4f 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c 2d 3e  Op->p1!=pLevel->
2c66e 69 54 61 62 43 75 72 20 29 20 63 6f 6e 74 69 6e  iTabCur ) contin
2c66f 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ue;.        if( 
2c670 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
2c671 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
2c672 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
2c673 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b  Idx->nColumn; j+
2c674 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
2c675 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 70 49 64  if( pOp->p2==pId
2c676 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 20 29  x->aiColumn[j] )
2c677 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2c678 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b 0a 20 20 20  pOp->p2 = j;.   
2c679 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e             pOp->
2c67a 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64  p1 = pLevel->iId
2c67b 78 43 75 72 3b 0a 20 20 20 20 20 20 20 20 20 20  xCur;.          
2c67c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2c67d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c67e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 61     }.          a
2c67f 73 73 65 72 74 28 21 75 73 65 49 6e 64 65 78 4f  ssert(!useIndexO
2c680 6e 6c 79 20 7c 7c 20 6a 3c 70 49 64 78 2d 3e 6e  nly || j<pIdx->n
2c681 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20  Column);.       
2c682 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
2c683 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 69 64  opcode==OP_Rowid
2c684 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   ){.          pO
2c685 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e  p->p1 = pLevel->
2c686 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20  iIdxCur;.       
2c687 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d     pOp->opcode =
2c688 20 4f 50 5f 49 64 78 52 6f 77 69 64 3b 0a 20 20   OP_IdxRowid;.  
2c689 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2c68a 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
2c68b 4e 75 6c 6c 52 6f 77 20 26 26 20 75 73 65 49 6e  NullRow && useIn
2c68c 64 65 78 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20  dexOnly ){.     
2c68d 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65       pOp->opcode
2c68e 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20   = OP_Noop;.    
2c68f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2c690 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69    }.  }..  /* Fi
2c691 6e 61 6c 20 63 6c 65 61 6e 75 70 0a 20 20 2a 2f  nal cleanup.  */
2c692 0a 20 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65  .  whereInfoFree
2c693 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20 20  (db, pWInfo);.  
2c694 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  return;.}../****
2c695 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2c696 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a  f where.c ******
2c697 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c698 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c699 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2c69a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2c69b 20 66 69 6c 65 20 70 61 72 73 65 2e 63 20 2a 2a   file parse.c **
2c69c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c69d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c69e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 72  *********/./* Dr
2c69f 69 76 65 72 20 74 65 6d 70 6c 61 74 65 20 66 6f  iver template fo
2c6a0 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70 61 72 73  r the LEMON pars
2c6a1 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a  er generator..**
2c6a2 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2c6a3 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2c6a4 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2c6a5 6f 64 65 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74  ode..*/./* First
2c6a6 20 6f 66 66 2c 20 63 6f 64 65 20 69 73 20 69 6e   off, code is in
2c6a7 63 6c 75 64 65 64 20 74 68 61 74 20 66 6f 6c 6c  cluded that foll
2c6a8 6f 77 73 20 74 68 65 20 22 69 6e 63 6c 75 64 65  ows the "include
2c6a9 22 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a  " declaration.**
2c6aa 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 67 72   in the input gr
2c6ab 61 6d 6d 61 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a  ammar file. */..
2c6ac 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
2c6ad 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
2c6ae 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f 72  ture holds infor
2c6af 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
2c6b0 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  .** LIMIT clause
2c6b1 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
2c6b2 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63  tement..*/.struc
2c6b3 74 20 4c 69 6d 69 74 56 61 6c 20 7b 0a 20 20 45  t LimitVal {.  E
2c6b4 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20  xpr *pLimit;    
2c6b5 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 65 78 70  /* The LIMIT exp
2c6b6 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69  ression.  NULL i
2c6b7 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69  f there is no li
2c6b8 6d 69 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  mit */.  Expr *p
2c6b9 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 54 68 65  Offset;   /* The
2c6ba 20 4f 46 46 53 45 54 20 65 78 70 72 65 73 73 69   OFFSET expressi
2c6bb 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66 20 74 68 65  on.  NULL if the
2c6bc 72 65 20 69 73 20 6e 6f 6e 65 20 2a 2f 0a 7d 3b  re is none */.};
2c6bd 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
2c6be 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
2c6bf 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
2c6c0 20 73 74 6f 72 65 20 74 68 65 20 4c 49 4b 45 2c   store the LIKE,
2c6c1 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f 54 20 4c 49  .** GLOB, NOT LI
2c6c2 4b 45 2c 20 61 6e 64 20 4e 4f 54 20 47 4c 4f 42  KE, and NOT GLOB
2c6c3 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a 73   operators..*/.s
2c6c4 74 72 75 63 74 20 4c 69 6b 65 4f 70 20 7b 0a 20  truct LikeOp {. 
2c6c5 20 54 6f 6b 65 6e 20 65 4f 70 65 72 61 74 6f 72   Token eOperator
2c6c6 3b 20 20 2f 2a 20 22 6c 69 6b 65 22 20 6f 72 20  ;  /* "like" or 
2c6c7 22 67 6c 6f 62 22 20 6f 72 20 22 72 65 67 65 78  "glob" or "regex
2c6c8 70 22 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 74 3b  p" */.  int not;
2c6c9 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2c6ca 20 69 66 20 74 68 65 20 4e 4f 54 20 6b 65 79 77   if the NOT keyw
2c6cb 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
2c6cc 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
2c6cd 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
2c6ce 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
2c6cf 72 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65  re describes the
2c6d0 20 65 76 65 6e 74 20 6f 66 20 61 0a 2a 2a 20 54   event of a.** T
2c6d1 52 49 47 47 45 52 2e 20 20 22 61 22 20 69 73 20  RIGGER.  "a" is 
2c6d2 74 68 65 20 65 76 65 6e 74 20 74 79 70 65 2c 20  the event type, 
2c6d3 6f 6e 65 20 6f 66 20 54 4b 5f 55 50 44 41 54 45  one of TK_UPDATE
2c6d4 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 0a 2a 2a 20  , TK_INSERT,.** 
2c6d5 54 4b 5f 44 45 4c 45 54 45 2c 20 6f 72 20 54 4b  TK_DELETE, or TK
2c6d6 5f 49 4e 53 54 45 41 44 2e 20 20 49 66 20 74 68  _INSTEAD.  If th
2c6d7 65 20 65 76 65 6e 74 20 69 73 20 6f 66 20 74 68  e event is of th
2c6d8 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
2c6d9 20 20 55 50 44 41 54 45 20 4f 4e 20 28 61 2c 62    UPDATE ON (a,b
2c6da 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74  ,c).**.** Then t
2c6db 68 65 20 22 62 22 20 49 64 4c 69 73 74 20 72 65  he "b" IdList re
2c6dc 63 6f 72 64 73 20 74 68 65 20 6c 69 73 74 20 22  cords the list "
2c6dd 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73 74 72 75 63  a,b,c"..*/.struc
2c6de 74 20 54 72 69 67 45 76 65 6e 74 20 7b 20 69 6e  t TrigEvent { in
2c6df 74 20 61 3b 20 49 64 4c 69 73 74 20 2a 20 62 3b  t a; IdList * b;
2c6e0 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e   };../*.** An in
2c6e1 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
2c6e2 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 74  tructure holds t
2c6e3 68 65 20 41 54 54 41 43 48 20 6b 65 79 20 61 6e  he ATTACH key an
2c6e4 64 20 74 68 65 20 6b 65 79 20 74 79 70 65 2e 0a  d the key type..
2c6e5 2a 2f 0a 73 74 72 75 63 74 20 41 74 74 61 63 68  */.struct Attach
2c6e6 4b 65 79 20 7b 20 69 6e 74 20 74 79 70 65 3b 20  Key { int type; 
2c6e7 20 54 6f 6b 65 6e 20 6b 65 79 3b 20 7d 3b 0a 0a   Token key; };..
2c6e8 2f 2a 20 4e 65 78 74 20 69 73 20 61 6c 6c 20 74  /* Next is all t
2c6e9 6f 6b 65 6e 20 76 61 6c 75 65 73 2c 20 69 6e 20  oken values, in 
2c6ea 61 20 66 6f 72 6d 20 73 75 69 74 61 62 6c 65 20  a form suitable 
2c6eb 66 6f 72 20 75 73 65 20 62 79 20 6d 61 6b 65 68  for use by makeh
2c6ec 65 61 64 65 72 73 2e 0a 2a 2a 20 54 68 69 73 20  eaders..** This 
2c6ed 73 65 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  section will be 
2c6ee 6e 75 6c 6c 20 75 6e 6c 65 73 73 20 6c 65 6d 6f  null unless lemo
2c6ef 6e 20 69 73 20 72 75 6e 20 77 69 74 68 20 74 68  n is run with th
2c6f0 65 20 2d 6d 20 73 77 69 74 63 68 2e 0a 2a 2f 0a  e -m switch..*/.
2c6f1 2f 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  /* .** These con
2c6f2 73 74 61 6e 74 73 20 28 61 6c 6c 20 67 65 6e 65  stants (all gene
2c6f3 72 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61  rated automatica
2c6f4 6c 6c 79 20 62 79 20 74 68 65 20 70 61 72 73 65  lly by the parse
2c6f5 72 20 67 65 6e 65 72 61 74 6f 72 29 0a 2a 2a 20  r generator).** 
2c6f6 73 70 65 63 69 66 79 20 74 68 65 20 76 61 72 69  specify the vari
2c6f7 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 74 6f 6b  ous kinds of tok
2c6f8 65 6e 73 20 28 74 65 72 6d 69 6e 61 6c 73 29 20  ens (terminals) 
2c6f9 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 0a  that the parser.
2c6fa 2a 2a 20 75 6e 64 65 72 73 74 61 6e 64 73 2e 20  ** understands. 
2c6fb 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 79 6d 62  .**.** Each symb
2c6fc 6f 6c 20 68 65 72 65 20 69 73 20 61 20 74 65 72  ol here is a ter
2c6fd 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 20 69 6e 20  minal symbol in 
2c6fe 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2f 0a  the grammar..*/.
2c6ff 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
2c700 20 49 4e 54 45 52 46 41 43 45 20 6d 61 63 72 6f   INTERFACE macro
2c701 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a   is defined..*/.
2c702 23 69 66 6e 64 65 66 20 49 4e 54 45 52 46 41 43  #ifndef INTERFAC
2c703 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 54 45 52  E.# define INTER
2c704 46 41 43 45 20 31 0a 23 65 6e 64 69 66 0a 2f 2a  FACE 1.#endif./*
2c705 20 54 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20   The next thing 
2c706 69 6e 63 6c 75 64 65 64 20 69 73 20 73 65 72 69  included is seri
2c707 65 73 20 6f 66 20 64 65 66 69 6e 65 73 20 77 68  es of defines wh
2c708 69 63 68 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 76  ich control.** v
2c709 61 72 69 6f 75 73 20 61 73 70 65 63 74 73 20 6f  arious aspects o
2c70a 66 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20  f the generated 
2c70b 70 61 72 73 65 72 2e 0a 2a 2a 20 20 20 20 59 59  parser..**    YY
2c70c 43 4f 44 45 54 59 50 45 20 20 20 20 20 20 20 20  CODETYPE        
2c70d 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
2c70e 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69  e used for stori
2c70f 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20  ng terminal.**  
2c710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c711 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d       and nonterm
2c712 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22  inal numbers.  "
2c713 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20 69  unsigned char" i
2c714 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
2c715 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20             used 
2c716 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77  if there are few
2c717 65 72 20 74 68 61 6e 20 32 35 30 20 74 65 72 6d  er than 250 term
2c718 69 6e 61 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20  inals.**        
2c719 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
2c71a 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 2e  nd nonterminals.
2c71b 20 20 22 69 6e 74 22 20 69 73 20 75 73 65 64 20    "int" is used 
2c71c 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20  otherwise..**   
2c71d 20 59 59 4e 4f 43 4f 44 45 20 20 20 20 20 20 20   YYNOCODE       
2c71e 20 20 20 20 69 73 20 61 20 6e 75 6d 62 65 72 20      is a number 
2c71f 6f 66 20 74 79 70 65 20 59 59 43 4f 44 45 54 59  of type YYCODETY
2c720 50 45 20 77 68 69 63 68 20 63 6f 72 72 65 73 70  PE which corresp
2c721 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  onds.**         
2c722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
2c723 20 6e 6f 20 6c 65 67 61 6c 20 74 65 72 6d 69 6e   no legal termin
2c724 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61  al or nontermina
2c725 6c 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 0a  l number.  This.
2c726 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2c727 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20           number 
2c728 69 73 20 75 73 65 64 20 74 6f 20 66 69 6c 6c 20  is used to fill 
2c729 69 6e 20 65 6d 70 74 79 20 73 6c 6f 74 73 20 6f  in empty slots o
2c72a 66 20 74 68 65 20 68 61 73 68 20 0a 2a 2a 20 20  f the hash .**  
2c72b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c72c 20 20 20 20 20 74 61 62 6c 65 2e 0a 2a 2a 20 20       table..**  
2c72d 20 20 59 59 46 41 4c 4c 42 41 43 4b 20 20 20 20    YYFALLBACK    
2c72e 20 20 20 20 20 49 66 20 64 65 66 69 6e 65 64 2c       If defined,
2c72f 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   this indicates 
2c730 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  that one or more
2c731 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20 20 20 20 20   tokens.**      
2c732 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c733 20 68 61 76 65 20 66 61 6c 6c 2d 62 61 63 6b 20   have fall-back 
2c734 76 61 6c 75 65 73 20 77 68 69 63 68 20 73 68 6f  values which sho
2c735 75 6c 64 20 62 65 20 75 73 65 64 20 69 66 20 74  uld be used if t
2c736 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
2c737 20 20 20 20 20 20 20 20 20 20 20 20 6f 72 69 67              orig
2c738 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68  inal value of th
2c739 65 20 74 6f 6b 65 6e 20 77 69 6c 6c 20 6e 6f 74  e token will not
2c73a 20 70 61 72 73 65 2e 0a 2a 2a 20 20 20 20 59 59   parse..**    YY
2c73b 41 43 54 49 4f 4e 54 59 50 45 20 20 20 20 20 20  ACTIONTYPE      
2c73c 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
2c73d 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69  e used for stori
2c73e 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20  ng terminal.**  
2c73f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c740 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d       and nonterm
2c741 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22  inal numbers.  "
2c742 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20 69  unsigned char" i
2c743 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
2c744 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20             used 
2c745 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77  if there are few
2c746 65 72 20 74 68 61 6e 20 32 35 30 20 72 75 6c 65  er than 250 rule
2c747 73 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  s and.**        
2c748 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2c749 74 61 74 65 73 20 63 6f 6d 62 69 6e 65 64 2e 20  tates combined. 
2c74a 20 22 69 6e 74 22 20 69 73 20 75 73 65 64 20 6f   "int" is used o
2c74b 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20 20  therwise..**    
2c74c 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b  sqlite3ParserTOK
2c74d 45 4e 54 59 50 45 20 20 20 20 20 69 73 20 74 68  ENTYPE     is th
2c74e 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64  e data type used
2c74f 20 66 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e   for minor token
2c750 73 20 67 69 76 65 6e 20 0a 2a 2a 20 20 20 20 20  s given .**     
2c751 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c752 20 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68    directly to th
2c753 65 20 70 61 72 73 65 72 20 66 72 6f 6d 20 74 68  e parser from th
2c754 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2a 20  e tokenizer..** 
2c755 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 20     YYMINORTYPE  
2c756 20 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74        is the dat
2c757 61 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20  a type used for 
2c758 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73  all minor tokens
2c759 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
2c75a 20 20 20 20 20 20 20 20 20 20 20 54 68 69 73 20             This 
2c75b 69 73 20 74 79 70 69 63 61 6c 6c 79 20 61 20 75  is typically a u
2c75c 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 74 79 70  nion of many typ
2c75d 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20  es, one of.**   
2c75e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c75f 20 20 20 20 77 68 69 63 68 20 69 73 20 73 71 6c      which is sql
2c760 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54  ite3ParserTOKENT
2c761 59 50 45 2e 20 20 54 68 65 20 65 6e 74 72 79 20  YPE.  The entry 
2c762 69 6e 20 74 68 65 20 75 6e 69 6f 6e 0a 2a 2a 20  in the union.** 
2c763 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c764 20 20 20 20 20 20 66 6f 72 20 62 61 73 65 20 74        for base t
2c765 6f 6b 65 6e 73 20 69 73 20 63 61 6c 6c 65 64 20  okens is called 
2c766 22 79 79 30 22 2e 0a 2a 2a 20 20 20 20 59 59 53  "yy0"..**    YYS
2c767 54 41 43 4b 44 45 50 54 48 20 20 20 20 20 20 20  TACKDEPTH       
2c768 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64  is the maximum d
2c769 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73  epth of the pars
2c76a 65 72 27 73 20 73 74 61 63 6b 2e 20 20 49 66 0a  er's stack.  If.
2c76b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2c76c 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 74 68           zero th
2c76d 65 20 73 74 61 63 6b 20 69 73 20 64 79 6e 61 6d  e stack is dynam
2c76e 69 63 61 6c 6c 79 20 73 69 7a 65 64 20 75 73 69  ically sized usi
2c76f 6e 67 20 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20  ng realloc().** 
2c770 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
2c771 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20 41 20  ARG_SDECL     A 
2c772 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 20  static variable 
2c773 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20  declaration for 
2c774 74 68 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d  the %extra_argum
2c775 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  ent.**    sqlite
2c776 33 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c  3ParserARG_PDECL
2c777 20 20 20 20 20 41 20 70 61 72 61 6d 65 74 65 72       A parameter
2c778 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72   declaration for
2c779 20 74 68 65 20 25 65 78 74 72 61 5f 61 72 67 75   the %extra_argu
2c77a 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74  ment.**    sqlit
2c77b 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52  e3ParserARG_STOR
2c77c 45 20 20 20 20 20 43 6f 64 65 20 74 6f 20 73 74  E     Code to st
2c77d 6f 72 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d  ore %extra_argum
2c77e 65 6e 74 20 69 6e 74 6f 20 79 79 70 50 61 72 73  ent into yypPars
2c77f 65 72 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  er.**    sqlite3
2c780 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 20  ParserARG_FETCH 
2c781 20 20 20 20 43 6f 64 65 20 74 6f 20 65 78 74 72      Code to extr
2c782 61 63 74 20 25 65 78 74 72 61 5f 61 72 67 75 6d  act %extra_argum
2c783 65 6e 74 20 66 72 6f 6d 20 79 79 70 50 61 72 73  ent from yypPars
2c784 65 72 0a 2a 2a 20 20 20 20 59 59 4e 53 54 41 54  er.**    YYNSTAT
2c785 45 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20  E           the 
2c786 63 6f 6d 62 69 6e 65 64 20 6e 75 6d 62 65 72 20  combined number 
2c787 6f 66 20 73 74 61 74 65 73 2e 0a 2a 2a 20 20 20  of states..**   
2c788 20 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20   YYNRULE        
2c789 20 20 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f      the number o
2c78a 66 20 72 75 6c 65 73 20 69 6e 20 74 68 65 20 67  f rules in the g
2c78b 72 61 6d 6d 61 72 0a 2a 2a 20 20 20 20 59 59 45  rammar.**    YYE
2c78c 52 52 4f 52 53 59 4d 42 4f 4c 20 20 20 20 20 20  RRORSYMBOL      
2c78d 69 73 20 74 68 65 20 63 6f 64 65 20 6e 75 6d 62  is the code numb
2c78e 65 72 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  er of the error 
2c78f 73 79 6d 62 6f 6c 2e 20 20 49 66 20 6e 6f 74 0a  symbol.  If not.
2c790 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2c791 20 20 20 20 20 20 20 20 20 64 65 66 69 6e 65 64           defined
2c792 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 20 65 72 72  , then do no err
2c793 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a  or processing..*
2c794 2f 0a 23 64 65 66 69 6e 65 20 59 59 43 4f 44 45  /.#define YYCODE
2c795 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68  TYPE unsigned sh
2c796 6f 72 74 20 69 6e 74 0a 23 64 65 66 69 6e 65 20  ort int.#define 
2c797 59 59 4e 4f 43 4f 44 45 20 32 35 32 0a 23 64 65  YYNOCODE 252.#de
2c798 66 69 6e 65 20 59 59 41 43 54 49 4f 4e 54 59 50  fine YYACTIONTYP
2c799 45 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74  E unsigned short
2c79a 20 69 6e 74 0a 23 64 65 66 69 6e 65 20 59 59 57   int.#define YYW
2c79b 49 4c 44 43 41 52 44 20 36 35 0a 23 64 65 66 69  ILDCARD 65.#defi
2c79c 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  ne sqlite3Parser
2c79d 54 4f 4b 45 4e 54 59 50 45 20 54 6f 6b 65 6e 0a  TOKENTYPE Token.
2c79e 74 79 70 65 64 65 66 20 75 6e 69 6f 6e 20 7b 0a  typedef union {.
2c79f 20 20 69 6e 74 20 79 79 69 6e 69 74 3b 0a 20 20    int yyinit;.  
2c7a0 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b  sqlite3ParserTOK
2c7a1 45 4e 54 59 50 45 20 79 79 30 3b 0a 20 20 45 78  ENTYPE yy0;.  Ex
2c7a2 70 72 2a 20 79 79 37 32 3b 0a 20 20 54 72 69 67  pr* yy72;.  Trig
2c7a3 67 65 72 53 74 65 70 2a 20 79 79 31 34 35 3b 0a  gerStep* yy145;.
2c7a4 20 20 45 78 70 72 4c 69 73 74 2a 20 79 79 31 34    ExprList* yy14
2c7a5 38 3b 0a 20 20 53 72 63 4c 69 73 74 2a 20 79 79  8;.  SrcList* yy
2c7a6 31 38 35 3b 0a 20 20 69 6e 74 20 79 79 31 39 34  185;.  int yy194
2c7a7 3b 0a 20 20 53 65 6c 65 63 74 2a 20 79 79 32 34  ;.  Select* yy24
2c7a8 33 3b 0a 20 20 49 64 4c 69 73 74 2a 20 79 79 32  3;.  IdList* yy2
2c7a9 35 34 3b 0a 20 20 73 74 72 75 63 74 20 54 72 69  54;.  struct Tri
2c7aa 67 45 76 65 6e 74 20 79 79 33 33 32 3b 0a 20 20  gEvent yy332;.  
2c7ab 73 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20  struct LimitVal 
2c7ac 79 79 33 35 34 3b 0a 20 20 73 74 72 75 63 74 20  yy354;.  struct 
2c7ad 4c 69 6b 65 4f 70 20 79 79 33 39 32 3b 0a 20 20  LikeOp yy392;.  
2c7ae 73 74 72 75 63 74 20 7b 69 6e 74 20 76 61 6c 75  struct {int valu
2c7af 65 3b 20 69 6e 74 20 6d 61 73 6b 3b 7d 20 79 79  e; int mask;} yy
2c7b0 34 39 37 3b 0a 7d 20 59 59 4d 49 4e 4f 52 54 59  497;.} YYMINORTY
2c7b1 50 45 3b 0a 23 69 66 6e 64 65 66 20 59 59 53 54  PE;.#ifndef YYST
2c7b2 41 43 4b 44 45 50 54 48 0a 23 64 65 66 69 6e 65  ACKDEPTH.#define
2c7b3 20 59 59 53 54 41 43 4b 44 45 50 54 48 20 31 30   YYSTACKDEPTH 10
2c7b4 30 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  0.#endif.#define
2c7b5 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2c7b6 47 5f 53 44 45 43 4c 20 50 61 72 73 65 20 2a 70  G_SDECL Parse *p
2c7b7 50 61 72 73 65 3b 0a 23 64 65 66 69 6e 65 20 73  Parse;.#define s
2c7b8 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
2c7b9 50 44 45 43 4c 20 2c 50 61 72 73 65 20 2a 70 50  PDECL ,Parse *pP
2c7ba 61 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  arse.#define sql
2c7bb 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
2c7bc 54 43 48 20 50 61 72 73 65 20 2a 70 50 61 72 73  TCH Parse *pPars
2c7bd 65 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 70  e = yypParser->p
2c7be 50 61 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71  Parse.#define sq
2c7bf 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
2c7c0 54 4f 52 45 20 79 79 70 50 61 72 73 65 72 2d 3e  TORE yypParser->
2c7c1 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 0a  pParse = pParse.
2c7c2 23 64 65 66 69 6e 65 20 59 59 4e 53 54 41 54 45  #define YYNSTATE
2c7c3 20 36 31 33 0a 23 64 65 66 69 6e 65 20 59 59 4e   613.#define YYN
2c7c4 52 55 4c 45 20 33 32 31 0a 23 64 65 66 69 6e 65  RULE 321.#define
2c7c5 20 59 59 46 41 4c 4c 42 41 43 4b 20 31 0a 23 64   YYFALLBACK 1.#d
2c7c6 65 66 69 6e 65 20 59 59 5f 4e 4f 5f 41 43 54 49  efine YY_NO_ACTI
2c7c7 4f 4e 20 20 20 20 20 20 28 59 59 4e 53 54 41 54  ON      (YYNSTAT
2c7c8 45 2b 59 59 4e 52 55 4c 45 2b 32 29 0a 23 64 65  E+YYNRULE+2).#de
2c7c9 66 69 6e 65 20 59 59 5f 41 43 43 45 50 54 5f 41  fine YY_ACCEPT_A
2c7ca 43 54 49 4f 4e 20 20 28 59 59 4e 53 54 41 54 45  CTION  (YYNSTATE
2c7cb 2b 59 59 4e 52 55 4c 45 2b 31 29 0a 23 64 65 66  +YYNRULE+1).#def
2c7cc 69 6e 65 20 59 59 5f 45 52 52 4f 52 5f 41 43 54  ine YY_ERROR_ACT
2c7cd 49 4f 4e 20 20 20 28 59 59 4e 53 54 41 54 45 2b  ION   (YYNSTATE+
2c7ce 59 59 4e 52 55 4c 45 29 0a 0a 2f 2a 20 54 68 65  YYNRULE)../* The
2c7cf 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20 63 6f 6e   yyzerominor con
2c7d0 73 74 61 6e 74 20 69 73 20 75 73 65 64 20 74 6f  stant is used to
2c7d1 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 73 74   initialize inst
2c7d2 61 6e 63 65 73 20 6f 66 0a 2a 2a 20 59 59 4d 49  ances of.** YYMI
2c7d3 4e 4f 52 54 59 50 45 20 6f 62 6a 65 63 74 73 20  NORTYPE objects 
2c7d4 74 6f 20 7a 65 72 6f 2e 20 2a 2f 0a 73 74 61 74  to zero. */.stat
2c7d5 69 63 20 63 6f 6e 73 74 20 59 59 4d 49 4e 4f 52  ic const YYMINOR
2c7d6 54 59 50 45 20 79 79 7a 65 72 6f 6d 69 6e 6f 72  TYPE yyzerominor
2c7d7 20 3d 20 7b 20 30 20 7d 3b 0a 0a 0a 2f 2a 20 4e   = { 0 };.../* N
2c7d8 65 78 74 20 61 72 65 20 74 68 65 20 74 61 62 6c  ext are the tabl
2c7d9 65 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  es used to deter
2c7da 6d 69 6e 65 20 77 68 61 74 20 61 63 74 69 6f 6e  mine what action
2c7db 20 74 6f 20 74 61 6b 65 20 62 61 73 65 64 20 6f   to take based o
2c7dc 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  n the.** current
2c7dd 20 73 74 61 74 65 20 61 6e 64 20 6c 6f 6f 6b 61   state and looka
2c7de 68 65 61 64 20 74 6f 6b 65 6e 2e 20 20 54 68 65  head token.  The
2c7df 73 65 20 74 61 62 6c 65 73 20 61 72 65 20 75 73  se tables are us
2c7e0 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a  ed to implement.
2c7e1 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ** functions tha
2c7e2 74 20 74 61 6b 65 20 61 20 73 74 61 74 65 20 6e  t take a state n
2c7e3 75 6d 62 65 72 20 61 6e 64 20 6c 6f 6f 6b 61 68  umber and lookah
2c7e4 65 61 64 20 76 61 6c 75 65 20 61 6e 64 20 72 65  ead value and re
2c7e5 74 75 72 6e 20 61 6e 0a 2a 2a 20 61 63 74 69 6f  turn an.** actio
2c7e6 6e 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2a 0a  n integer.  .**.
2c7e7 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20 61  ** Suppose the a
2c7e8 63 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 69 73  ction integer is
2c7e9 20 4e 2e 20 20 54 68 65 6e 20 74 68 65 20 61 63   N.  Then the ac
2c7ea 74 69 6f 6e 20 69 73 20 64 65 74 65 72 6d 69 6e  tion is determin
2c7eb 65 64 20 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73  ed as.** follows
2c7ec 0a 2a 2a 0a 2a 2a 20 20 20 30 20 3c 3d 20 4e 20  .**.**   0 <= N 
2c7ed 3c 20 59 59 4e 53 54 41 54 45 20 20 20 20 20 20  < YYNSTATE      
2c7ee 20 20 20 20 20 20 20 20 20 20 20 20 53 68 69 66              Shif
2c7ef 74 20 4e 2e 20 20 54 68 61 74 20 69 73 2c 20 70  t N.  That is, p
2c7f0 75 73 68 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61  ush the lookahea
2c7f1 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
2c7f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7f3 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2c7f4 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61  onto the stack a
2c7f5 6e 64 20 67 6f 74 6f 20 73 74 61 74 65 20 4e 2e  nd goto state N.
2c7f6 0a 2a 2a 0a 2a 2a 20 20 20 59 59 4e 53 54 41 54  .**.**   YYNSTAT
2c7f7 45 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54  E <= N < YYNSTAT
2c7f8 45 2b 59 59 4e 52 55 4c 45 20 20 20 52 65 64 75  E+YYNRULE   Redu
2c7f9 63 65 20 62 79 20 72 75 6c 65 20 4e 2d 59 59 4e  ce by rule N-YYN
2c7fa 53 54 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e  STATE..**.**   N
2c7fb 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e   == YYNSTATE+YYN
2c7fc 52 55 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  RULE            
2c7fd 20 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f 72    A syntax error
2c7fe 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 2a   has occurred..*
2c7ff 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53  *.**   N == YYNS
2c800 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 20 20  TATE+YYNRULE+1  
2c801 20 20 20 20 20 20 20 20 20 20 54 68 65 20 70 61            The pa
2c802 72 73 65 72 20 61 63 63 65 70 74 73 20 69 74 73  rser accepts its
2c803 20 69 6e 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20   input..**.**   
2c804 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59  N == YYNSTATE+YY
2c805 4e 52 55 4c 45 2b 32 20 20 20 20 20 20 20 20 20  NRULE+2         
2c806 20 20 20 4e 6f 20 73 75 63 68 20 61 63 74 69 6f     No such actio
2c807 6e 2e 20 20 44 65 6e 6f 74 65 73 20 75 6e 75 73  n.  Denotes unus
2c808 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ed.**           
2c809 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c80a 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 73             slots
2c80b 20 69 6e 20 74 68 65 20 79 79 5f 61 63 74 69 6f   in the yy_actio
2c80c 6e 5b 5d 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  n[] table..**.**
2c80d 20 54 68 65 20 61 63 74 69 6f 6e 20 74 61 62 6c   The action tabl
2c80e 65 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64  e is constructed
2c80f 20 61 73 20 61 20 73 69 6e 67 6c 65 20 6c 61 72   as a single lar
2c810 67 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 79  ge table named y
2c811 79 5f 61 63 74 69 6f 6e 5b 5d 2e 0a 2a 2a 20 47  y_action[]..** G
2c812 69 76 65 6e 20 73 74 61 74 65 20 53 20 61 6e 64  iven state S and
2c813 20 6c 6f 6f 6b 61 68 65 61 64 20 58 2c 20 74 68   lookahead X, th
2c814 65 20 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70  e action is comp
2c815 75 74 65 64 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20  uted as.**.**   
2c816 20 20 20 79 79 5f 61 63 74 69 6f 6e 5b 20 79 79     yy_action[ yy
2c817 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 20 2b  _shift_ofst[S] +
2c818 20 58 20 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   X ].**.** If th
2c819 65 20 69 6e 64 65 78 20 76 61 6c 75 65 20 79 79  e index value yy
2c81a 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58  _shift_ofst[S]+X
2c81b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
2c81c 20 6f 72 20 69 66 20 74 68 65 20 76 61 6c 75 65   or if the value
2c81d 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64  .** yy_lookahead
2c81e 5b 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53  [yy_shift_ofst[S
2c81f 5d 2b 58 5d 20 69 73 20 6e 6f 74 20 65 71 75 61  ]+X] is not equa
2c820 6c 20 74 6f 20 58 20 6f 72 20 69 66 20 79 79 5f  l to X or if yy_
2c821 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 0a 2a 2a  shift_ofst[S].**
2c822 20 69 73 20 65 71 75 61 6c 20 74 6f 20 59 59 5f   is equal to YY_
2c823 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2c 20  SHIFT_USE_DFLT, 
2c824 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
2c825 65 20 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  e action is not 
2c826 69 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  in the table.** 
2c827 61 6e 64 20 74 68 61 74 20 79 79 5f 64 65 66 61  and that yy_defa
2c828 75 6c 74 5b 53 5d 20 73 68 6f 75 6c 64 20 62 65  ult[S] should be
2c829 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 20 20   used instead.  
2c82a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 75  .**.** The formu
2c82b 6c 61 20 61 62 6f 76 65 20 69 73 20 66 6f 72 20  la above is for 
2c82c 63 6f 6d 70 75 74 69 6e 67 20 74 68 65 20 61 63  computing the ac
2c82d 74 69 6f 6e 20 77 68 65 6e 20 74 68 65 20 6c 6f  tion when the lo
2c82e 6f 6b 61 68 65 61 64 20 69 73 0a 2a 2a 20 61 20  okahead is.** a 
2c82f 74 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 2e  terminal symbol.
2c830 20 20 49 66 20 74 68 65 20 6c 6f 6f 6b 61 68 65    If the lookahe
2c831 61 64 20 69 73 20 61 20 6e 6f 6e 2d 74 65 72 6d  ad is a non-term
2c832 69 6e 61 6c 20 28 61 73 20 6f 63 63 75 72 73 20  inal (as occurs 
2c833 61 66 74 65 72 0a 2a 2a 20 61 20 72 65 64 75 63  after.** a reduc
2c834 65 20 61 63 74 69 6f 6e 29 20 74 68 65 6e 20 74  e action) then t
2c835 68 65 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73  he yy_reduce_ofs
2c836 74 5b 5d 20 61 72 72 61 79 20 69 73 20 75 73 65  t[] array is use
2c837 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a  d in place of.**
2c838 20 74 68 65 20 79 79 5f 73 68 69 66 74 5f 6f 66   the yy_shift_of
2c839 73 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 59  st[] array and Y
2c83a 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c  Y_REDUCE_USE_DFL
2c83b 54 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61  T is used in pla
2c83c 63 65 20 6f 66 0a 2a 2a 20 59 59 5f 53 48 49 46  ce of.** YY_SHIF
2c83d 54 5f 55 53 45 5f 44 46 4c 54 2e 0a 2a 2a 0a 2a  T_USE_DFLT..**.*
2c83e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2c83f 61 72 65 20 74 68 65 20 74 61 62 6c 65 73 20 67  are the tables g
2c840 65 6e 65 72 61 74 65 64 20 69 6e 20 74 68 69 73  enerated in this
2c841 20 73 65 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20   section:.**.** 
2c842 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 20 20 20   yy_action[]    
2c843 20 20 20 20 41 20 73 69 6e 67 6c 65 20 74 61 62      A single tab
2c844 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c  le containing al
2c845 6c 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 79  l actions..**  y
2c846 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 20 20  y_lookahead[]   
2c847 20 20 41 20 74 61 62 6c 65 20 63 6f 6e 74 61 69    A table contai
2c848 6e 69 6e 67 20 74 68 65 20 6c 6f 6f 6b 61 68 65  ning the lookahe
2c849 61 64 20 66 6f 72 20 65 61 63 68 20 65 6e 74 72  ad for each entr
2c84a 79 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  y in.**         
2c84b 20 20 20 20 20 20 20 20 20 20 20 20 79 79 5f 61              yy_a
2c84c 63 74 69 6f 6e 2e 20 20 55 73 65 64 20 74 6f 20  ction.  Used to 
2c84d 64 65 74 65 63 74 20 68 61 73 68 20 63 6f 6c 6c  detect hash coll
2c84e 69 73 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 73  isions..**  yy_s
2c84f 68 69 66 74 5f 6f 66 73 74 5b 5d 20 20 20 20 46  hift_ofst[]    F
2c850 6f 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74  or each state, t
2c851 68 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79  he offset into y
2c852 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20  y_action for.** 
2c853 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c854 20 20 20 20 73 68 69 66 74 69 6e 67 20 74 65 72      shifting ter
2c855 6d 69 6e 61 6c 73 2e 0a 2a 2a 20 20 79 79 5f 72  minals..**  yy_r
2c856 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20 20 20 46  educe_ofst[]   F
2c857 6f 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74  or each state, t
2c858 68 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79  he offset into y
2c859 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20  y_action for.** 
2c85a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c85b 20 20 20 20 73 68 69 66 74 69 6e 67 20 6e 6f 6e      shifting non
2c85c 2d 74 65 72 6d 69 6e 61 6c 73 20 61 66 74 65 72  -terminals after
2c85d 20 61 20 72 65 64 75 63 65 2e 0a 2a 2a 20 20 79   a reduce..**  y
2c85e 79 5f 64 65 66 61 75 6c 74 5b 5d 20 20 20 20 20  y_default[]     
2c85f 20 20 44 65 66 61 75 6c 74 20 61 63 74 69 6f 6e    Default action
2c860 20 66 6f 72 20 65 61 63 68 20 73 74 61 74 65 2e   for each state.
2c861 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
2c862 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 79 79   YYACTIONTYPE yy
2c863 5f 61 63 74 69 6f 6e 5b 5d 20 3d 20 7b 0a 20 2f  _action[] = {. /
2c864 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 33 30 34  *     0 */   304
2c865 2c 20 20 39 33 35 2c 20 20 31 37 36 2c 20 20 36  ,  935,  176,  6
2c866 31 32 2c 20 20 20 20 32 2c 20 20 31 35 30 2c 20  12,    2,  150, 
2c867 20 32 31 34 2c 20 20 34 33 38 2c 20 20 20 32 34   214,  438,   24
2c868 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 20 31  ,   24,. /*    1
2c869 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34  0 */    24,   24
2c86a 2c 20 20 34 38 37 2c 20 20 20 32 36 2c 20 20 20  ,  487,   26,   
2c86b 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  26,   26,   26, 
2c86c 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38    27,   27,   28
2c86d 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
2c86e 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39    28,   28,   29
2c86f 2c 20 20 32 31 36 2c 20 20 34 31 32 2c 20 20 34  ,  216,  412,  4
2c870 31 33 2c 20 20 32 31 32 2c 20 20 34 31 32 2c 20  13,  212,  412, 
2c871 20 34 31 33 2c 20 20 34 34 35 2c 0a 20 2f 2a 20   413,  445,. /* 
2c872 20 20 20 33 30 20 2a 2f 20 20 20 34 35 31 2c 20     30 */   451, 
2c873 20 20 33 31 2c 20 20 20 32 36 2c 20 20 20 32 36    31,   26,   26
2c874 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2c875 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20  27,   27,   28, 
2c876 20 20 32 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20    28,. /*    40 
2c877 2a 2f 20 20 20 20 32 38 2c 20 20 20 32 39 2c 20  */    28,   29, 
2c878 20 32 31 36 2c 20 20 20 33 30 2c 20 20 34 38 32   216,   30,  482
2c879 2c 20 20 20 33 32 2c 20 20 31 33 34 2c 20 20 20  ,   32,  134,   
2c87a 32 33 2c 20 20 20 32 32 2c 20 20 33 30 38 2c 0a  23,   22,  308,.
2c87b 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 34   /*    50 */   4
2c87c 35 35 2c 20 20 34 35 36 2c 20 20 34 35 32 2c 20  55,  456,  452, 
2c87d 20 34 35 32 2c 20 20 20 32 35 2c 20 20 20 32 35   452,   25,   25
2c87e 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20  ,   24,   24,   
2c87f 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20  24,   24,. /*   
2c880 20 36 30 20 2a 2f 20 20 20 34 33 35 2c 20 20 20   60 */   435,   
2c881 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  26,   26,   26, 
2c882 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37    26,   27,   27
2c883 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20  ,   28,   28,   
2c884 32 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  28,. /*    70 */
2c885 20 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20 33      29,  216,  3
2c886 30 34 2c 20 20 32 31 36 2c 20 20 33 31 31 2c 20  04,  216,  311, 
2c887 20 34 33 38 2c 20 20 35 31 31 2c 20 20 34 38 39   438,  511,  489
2c888 2c 20 20 20 34 35 2c 20 20 20 32 36 2c 0a 20 2f  ,   45,   26,. /
2c889 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20 32 36  *    80 */    26
2c88a 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2c88b 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20  27,   27,   28, 
2c88c 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39    28,   28,   29
2c88d 2c 20 20 32 31 36 2c 0a 20 2f 2a 20 20 20 20 39  ,  216,. /*    9
2c88e 30 20 2a 2f 20 20 20 34 31 32 2c 20 20 34 31 33  0 */   412,  413
2c88f 2c 20 20 34 31 35 2c 20 20 34 31 36 2c 20 20 31  ,  415,  416,  1
2c890 35 36 2c 20 20 34 31 35 2c 20 20 34 31 36 2c 20  56,  415,  416, 
2c891 20 33 36 30 2c 20 20 33 36 33 2c 20 20 33 36 34   360,  363,  364
2c892 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
2c893 20 33 31 31 2c 20 20 34 34 35 2c 20 20 34 35 31   311,  445,  451
2c894 2c 20 20 33 38 35 2c 20 20 35 31 33 2c 20 20 20  ,  385,  513,   
2c895 32 31 2c 20 20 31 38 36 2c 20 20 34 39 34 2c 20  21,  186,  494, 
2c896 20 33 36 35 2c 20 20 20 32 37 2c 0a 20 2f 2a 20   365,   27,. /* 
2c897 20 20 31 31 30 20 2a 2f 20 20 20 20 32 37 2c 20    110 */    27, 
2c898 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38    28,   28,   28
2c899 2c 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20 34  ,   29,  216,  4
2c89a 31 32 2c 20 20 34 31 33 2c 20 20 34 31 34 2c 20  12,  413,  414, 
2c89b 20 20 32 33 2c 0a 20 2f 2a 20 20 20 31 32 30 20    23,. /*   120 
2c89c 2a 2f 20 20 20 20 32 32 2c 20 20 33 30 38 2c 20  */    22,  308, 
2c89d 20 34 35 35 2c 20 20 34 35 36 2c 20 20 34 35 32   455,  456,  452
2c89e 2c 20 20 34 35 32 2c 20 20 20 32 35 2c 20 20 20  ,  452,   25,   
2c89f 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 0a  25,   24,   24,.
2c8a0 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 20   /*   130 */    
2c8a1 32 34 2c 20 20 20 32 34 2c 20 20 35 35 34 2c 20  24,   24,  554, 
2c8a2 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36    26,   26,   26
2c8a3 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20  ,   26,   27,   
2c8a4 32 37 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20  27,   28,. /*   
2c8a5 31 34 30 20 2a 2f 20 20 20 20 32 38 2c 20 20 20  140 */    28,   
2c8a6 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36 2c 20  28,   29,  216, 
2c8a7 20 33 30 34 2c 20 20 32 32 38 2c 20 20 35 30 33   304,  228,  503
2c8a8 2c 20 20 31 33 35 2c 20 20 34 36 37 2c 20 20 32  ,  135,  467,  2
2c8a9 31 38 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  18,. /*   150 */
2c8aa 20 20 20 34 33 38 2c 20 20 31 34 35 2c 20 20 31     438,  145,  1
2c8ab 33 32 2c 20 20 32 35 36 2c 20 20 33 35 38 2c 20  32,  256,  358, 
2c8ac 20 32 36 31 2c 20 20 33 35 39 2c 20 20 31 35 33   261,  359,  153
2c8ad 2c 20 20 34 31 35 2c 20 20 34 31 36 2c 0a 20 2f  ,  415,  416,. /
2c8ae 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 35 35 33  *   160 */   553
2c8af 2c 20 20 35 32 38 2c 20 20 33 33 31 2c 20 20 20  ,  528,  331,   
2c8b0 33 30 2c 20 20 32 36 35 2c 20 20 20 33 32 2c 20  30,  265,   32, 
2c8b1 20 31 33 34 2c 20 20 35 35 32 2c 20 20 35 39 32   134,  552,  592
2c8b2 2c 20 20 35 39 33 2c 0a 20 2f 2a 20 20 20 31 37  ,  593,. /*   17
2c8b3 30 20 2a 2f 20 20 20 32 33 30 2c 20 20 32 32 38  0 */   230,  228
2c8b4 2c 20 20 34 38 39 2c 20 20 34 34 35 2c 20 20 34  ,  489,  445,  4
2c8b5 35 31 2c 20 20 20 35 37 2c 20 20 35 30 35 2c 20  51,   57,  505, 
2c8b6 20 33 32 38 2c 20 20 31 33 32 2c 20 20 32 35 36   328,  132,  256
2c8b7 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
2c8b8 20 33 35 38 2c 20 20 32 36 31 2c 20 20 33 35 39   358,  261,  359
2c8b9 2c 20 20 31 35 33 2c 20 20 34 31 35 2c 20 20 34  ,  153,  415,  4
2c8ba 31 36 2c 20 20 34 33 34 2c 20 20 20 37 38 2c 20  16,  434,   78, 
2c8bb 20 34 30 37 2c 20 20 34 30 34 2c 0a 20 2f 2a 20   407,  404,. /* 
2c8bc 20 20 31 39 30 20 2a 2f 20 20 20 32 36 35 2c 20    190 */   265, 
2c8bd 20 20 32 33 2c 20 20 20 32 32 2c 20 20 33 30 38    23,   22,  308
2c8be 2c 20 20 34 35 35 2c 20 20 34 35 36 2c 20 20 34  ,  455,  456,  4
2c8bf 35 32 2c 20 20 34 35 32 2c 20 20 20 32 35 2c 20  52,  452,   25, 
2c8c0 20 20 32 35 2c 0a 20 2f 2a 20 20 20 32 30 30 20    25,. /*   200 
2c8c1 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20  */    24,   24, 
2c8c2 20 20 32 34 2c 20 20 20 32 34 2c 20 20 34 37 36    24,   24,  476
2c8c3 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2c8c4 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 0a  26,   26,   27,.
2c8c5 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 20   /*   210 */    
2c8c6 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20  27,   28,   28, 
2c8c7 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36    28,   29,  216
2c8c8 2c 20 20 33 30 34 2c 20 20 35 37 32 2c 20 20 34  ,  304,  572,  4
2c8c9 33 38 2c 20 20 35 34 36 2c 0a 20 2f 2a 20 20 20  38,  546,. /*   
2c8ca 32 32 30 20 2a 2f 20 20 20 34 37 37 2c 20 20 31  220 */   477,  1
2c8cb 32 37 2c 20 20 35 34 37 2c 20 20 35 39 36 2c 20  27,  547,  596, 
2c8cc 20 20 33 30 2c 20 20 33 33 31 2c 20 20 20 33 32    30,  331,   32
2c8cd 2c 20 20 31 33 34 2c 20 20 33 34 35 2c 20 20 32  ,  134,  345,  2
2c8ce 31 34 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  14,. /*   230 */
2c8cf 20 20 20 34 32 38 2c 20 20 20 36 33 2c 20 20 33     428,   63,  3
2c8d0 33 31 2c 20 20 33 35 35 2c 20 20 34 31 34 2c 20  31,  355,  414, 
2c8d1 20 34 37 38 2c 20 20 32 34 31 2c 20 20 33 34 31   478,  241,  341
2c8d2 2c 20 20 34 31 34 2c 20 20 33 34 32 2c 0a 20 2f  ,  414,  342,. /
2c8d3 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 33 32 38  *   240 */   328
2c8d4 2c 20 20 33 38 37 2c 20 20 31 39 33 2c 20 20 35  ,  387,  193,  5
2c8d5 39 35 2c 20 20 35 39 34 2c 20 20 34 34 35 2c 20  95,  594,  445, 
2c8d6 20 34 35 31 2c 20 20 33 32 38 2c 20 20 32 39 39   451,  328,  299
2c8d7 2c 20 20 34 33 34 2c 0a 20 2f 2a 20 20 20 32 35  ,  434,. /*   25
2c8d8 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 34 36 39  0 */    85,  469
2c8d9 2c 20 20 35 34 35 2c 20 20 32 30 30 2c 20 20 31  ,  545,  200,  1
2c8da 39 30 2c 20 20 35 35 35 2c 20 20 34 33 34 2c 20  90,  555,  434, 
2c8db 20 20 37 38 2c 20 20 33 30 39 2c 20 20 34 36 32    78,  309,  462
2c8dc 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
2c8dd 20 34 36 33 2c 20 20 35 37 31 2c 20 20 34 37 31   463,  571,  471
2c8de 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 33  ,   23,   22,  3
2c8df 30 38 2c 20 20 34 35 35 2c 20 20 34 35 36 2c 20  08,  455,  456, 
2c8e0 20 34 35 32 2c 20 20 34 35 32 2c 0a 20 2f 2a 20   452,  452,. /* 
2c8e1 20 20 32 37 30 20 2a 2f 20 20 20 20 32 35 2c 20    270 */    25, 
2c8e2 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34    25,   24,   24
2c8e3 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 34  ,   24,   24,  4
2c8e4 33 35 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  35,   26,   26, 
2c8e5 20 20 32 36 2c 0a 20 2f 2a 20 20 20 32 38 30 20    26,. /*   280 
2c8e6 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c 20  */    26,   27, 
2c8e7 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38    27,   28,   28
2c8e8 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32  ,   28,   29,  2
2c8e9 31 36 2c 20 20 33 30 34 2c 20 20 33 34 37 2c 0a  16,  304,  347,.
2c8ea 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 32   /*   290 */   2
2c8eb 32 31 2c 20 20 31 39 31 2c 20 20 35 31 32 2c 20  21,  191,  512, 
2c8ec 20 34 32 39 2c 20 20 35 34 34 2c 20 20 33 33 31   429,  544,  331
2c8ed 2c 20 20 34 34 30 2c 20 20 32 33 34 2c 20 20 33  ,  440,  234,  3
2c8ee 34 35 2c 20 20 34 33 30 2c 0a 20 2f 2a 20 20 20  45,  430,. /*   
2c8ef 33 30 30 20 2a 2f 20 20 20 33 32 34 2c 20 20 34  300 */   324,  4
2c8f0 30 39 2c 20 20 35 34 31 2c 20 20 33 34 34 2c 20  09,  541,  344, 
2c8f1 20 35 39 31 2c 20 20 32 31 37 2c 20 20 32 31 33   591,  217,  213
2c8f2 2c 20 20 35 34 31 2c 20 20 31 31 32 2c 20 20 33  ,  541,  112,  3
2c8f3 33 31 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f  31,. /*   310 */
2c8f4 20 20 20 33 32 38 2c 20 20 33 38 38 2c 20 20 32     328,  388,  2
2c8f5 38 32 2c 20 20 32 38 31 2c 20 20 32 31 31 2c 20  82,  281,  211, 
2c8f6 20 20 32 39 2c 20 20 32 31 36 2c 20 20 34 34 35    29,  216,  445
2c8f7 2c 20 20 34 35 31 2c 20 20 34 33 34 2c 0a 20 2f  ,  451,  434,. /
2c8f8 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 20 37 39  *   320 */    79
2c8f9 2c 20 20 32 31 37 2c 20 20 32 31 34 2c 20 20 33  ,  217,  214,  3
2c8fa 33 34 2c 20 20 33 32 38 2c 20 20 34 35 38 2c 20  34,  328,  458, 
2c8fb 20 34 35 38 2c 20 20 34 34 32 2c 20 20 34 34 32   458,  442,  442
2c8fc 2c 20 20 34 34 32 2c 0a 20 2f 2a 20 20 20 33 33  ,  442,. /*   33
2c8fd 30 20 2a 2f 20 20 20 35 37 31 2c 20 20 32 37 30  0 */   571,  270
2c8fe 2c 20 20 20 20 34 2c 20 20 34 33 34 2c 20 20 20  ,    4,  434,   
2c8ff 38 35 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20  85,   23,   22, 
2c900 20 33 30 38 2c 20 20 34 35 35 2c 20 20 34 35 36   308,  455,  456
2c901 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20  ,. /*   340 */  
2c902 20 34 35 32 2c 20 20 34 35 32 2c 20 20 20 32 35   452,  452,   25
2c903 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20  ,   25,   24,   
2c904 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20  24,   24,   24, 
2c905 20 34 33 35 2c 20 20 20 32 36 2c 0a 20 2f 2a 20   435,   26,. /* 
2c906 20 20 33 35 30 20 2a 2f 20 20 20 20 32 36 2c 20    350 */    26, 
2c907 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37    26,   26,   27
2c908 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20  ,   27,   28,   
2c909 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20  28,   28,   29, 
2c90a 20 32 31 36 2c 0a 20 2f 2a 20 20 20 33 36 30 20   216,. /*   360 
2c90b 2a 2f 20 20 20 33 30 34 2c 20 20 35 31 34 2c 20  */   304,  514, 
2c90c 20 31 35 36 2c 20 20 33 33 31 2c 20 20 34 38 38   156,  331,  488
2c90d 2c 20 20 33 36 30 2c 20 20 33 36 33 2c 20 20 33  ,  360,  363,  3
2c90e 36 34 2c 20 20 33 33 31 2c 20 20 33 35 33 2c 0a  64,  331,  353,.
2c90f 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 34   /*   370 */   4
2c910 33 31 2c 20 20 32 34 37 2c 20 20 33 39 35 2c 20  31,  247,  395, 
2c911 20 32 37 34 2c 20 20 32 32 30 2c 20 20 32 37 32   274,  220,  272
2c912 2c 20 20 33 36 35 2c 20 20 33 35 32 2c 20 20 33  ,  365,  352,  3
2c913 32 38 2c 20 20 33 33 31 2c 0a 20 2f 2a 20 20 20  28,  331,. /*   
2c914 33 38 30 20 2a 2f 20 20 20 33 38 38 2c 20 20 32  380 */   388,  2
2c915 38 32 2c 20 20 32 38 31 2c 20 20 33 32 38 2c 20  82,  281,  328, 
2c916 20 34 31 32 2c 20 20 34 31 33 2c 20 20 33 30 37   412,  413,  307
2c917 2c 20 20 34 33 34 2c 20 20 20 39 33 2c 20 20 34  ,  434,   93,  4
2c918 34 35 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f  45,. /*   390 */
2c919 20 20 20 34 35 31 2c 20 20 32 31 34 2c 20 20 34     451,  214,  4
2c91a 33 34 2c 20 20 20 39 33 2c 20 20 33 32 38 2c 20  34,   93,  328, 
2c91b 20 35 33 30 2c 20 20 31 35 30 2c 20 20 20 20 31   530,  150,    1
2c91c 2c 20 20 34 33 38 2c 20 20 34 30 33 2c 0a 20 2f  ,  438,  403,. /
2c91d 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 34 36 38  *   400 */   468
2c91e 2c 20 20 34 31 32 2c 20 20 34 31 33 2c 20 20 34  ,  412,  413,  4
2c91f 33 34 2c 20 20 20 37 38 2c 20 20 20 34 30 2c 20  34,   78,   40, 
2c920 20 32 31 30 2c 20 20 20 32 33 2c 20 20 20 32 32   210,   23,   22
2c921 2c 20 20 33 30 38 2c 0a 20 2f 2a 20 20 20 34 31  ,  308,. /*   41
2c922 30 20 2a 2f 20 20 20 34 35 35 2c 20 20 34 35 36  0 */   455,  456
2c923 2c 20 20 34 35 32 2c 20 20 34 35 32 2c 20 20 20  ,  452,  452,   
2c924 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20  25,   25,   24, 
2c925 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34    24,   24,   24
2c926 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20  ,. /*   420 */  
2c927 20 31 39 34 2c 20 20 20 32 36 2c 20 20 20 32 36   194,   26,   26
2c928 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2c929 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20  27,   27,   28, 
2c92a 20 20 32 38 2c 20 20 20 32 38 2c 0a 20 2f 2a 20    28,   28,. /* 
2c92b 20 20 34 33 30 20 2a 2f 20 20 20 20 32 39 2c 20    430 */    29, 
2c92c 20 32 31 36 2c 20 20 33 30 34 2c 20 20 33 31 39   216,  304,  319
2c92d 2c 20 20 33 33 31 2c 20 20 35 39 36 2c 20 20 35  ,  331,  596,  5
2c92e 30 37 2c 20 20 35 33 35 2c 20 20 33 32 30 2c 20  07,  535,  320, 
2c92f 20 31 37 39 2c 0a 20 2f 2a 20 20 20 34 34 30 20   179,. /*   440 
2c930 2a 2f 20 20 20 34 33 35 2c 20 20 34 38 39 2c 20  */   435,  489, 
2c931 20 34 36 37 2c 20 20 31 35 36 2c 20 20 31 39 32   467,  156,  192
2c932 2c 20 20 33 34 39 2c 20 20 33 36 30 2c 20 20 33  ,  349,  360,  3
2c933 36 33 2c 20 20 33 36 34 2c 20 20 33 32 38 2c 0a  63,  364,  328,.
2c934 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 34   /*   450 */   4
2c935 31 34 2c 20 20 35 33 36 2c 20 20 34 31 35 2c 20  14,  536,  415, 
2c936 20 34 31 36 2c 20 20 34 33 35 2c 20 20 35 39 35   416,  435,  595
2c937 2c 20 20 35 34 36 2c 20 20 33 36 35 2c 20 20 34  ,  546,  365,  4
2c938 33 34 2c 20 20 20 37 31 2c 0a 20 2f 2a 20 20 20  34,   71,. /*   
2c939 34 36 30 20 2a 2f 20 20 20 33 37 38 2c 20 20 34  460 */   378,  4
2c93a 34 35 2c 20 20 34 35 31 2c 20 20 32 30 38 2c 20  45,  451,  208, 
2c93b 20 32 34 30 2c 20 20 33 38 31 2c 20 20 34 33 38   240,  381,  438
2c93c 2c 20 20 35 35 36 2c 20 20 20 36 33 2c 20 20 34  ,  556,   63,  4
2c93d 31 35 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f  15,. /*   470 */
2c93e 20 20 20 34 31 36 2c 20 20 34 31 34 2c 20 20 20     416,  414,   
2c93f 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20  28,   28,   28, 
2c940 20 20 32 39 2c 20 20 32 31 36 2c 20 20 20 34 33    29,  216,   43
2c941 2c 20 20 34 33 35 2c 20 20 20 32 33 2c 0a 20 2f  ,  435,   23,. /
2c942 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 20 32 32  *   480 */    22
2c943 2c 20 20 33 30 38 2c 20 20 34 35 35 2c 20 20 34  ,  308,  455,  4
2c944 35 36 2c 20 20 34 35 32 2c 20 20 34 35 32 2c 20  56,  452,  452, 
2c945 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34    25,   25,   24
2c946 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 34 39  ,   24,. /*   49
2c947 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34  0 */    24,   24
2c948 2c 20 20 32 37 36 2c 20 20 20 32 36 2c 20 20 20  ,  276,   26,   
2c949 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  26,   26,   26, 
2c94a 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38    27,   27,   28
2c94b 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20  ,. /*   500 */  
2c94c 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39    28,   28,   29
2c94d 2c 20 20 32 31 36 2c 20 20 33 30 34 2c 20 20 33  ,  216,  304,  3
2c94e 35 34 2c 20 20 32 30 39 2c 20 20 34 31 34 2c 20  54,  209,  414, 
2c94f 20 35 31 30 2c 20 20 34 31 32 2c 0a 20 2f 2a 20   510,  412,. /* 
2c950 20 20 35 31 30 20 2a 2f 20 20 20 34 31 33 2c 20    510 */   413, 
2c951 20 31 33 35 2c 20 20 34 32 36 2c 20 20 33 33 31   135,  426,  331
2c952 2c 20 20 34 31 32 2c 20 20 34 31 33 2c 20 20 33  ,  412,  413,  3
2c953 39 38 2c 20 20 32 31 37 2c 20 20 20 36 36 2c 20  98,  217,   66, 
2c954 20 33 33 33 2c 0a 20 2f 2a 20 20 20 35 32 30 20   333,. /*   520 
2c955 2a 2f 20 20 20 33 32 38 2c 20 20 35 36 34 2c 20  */   328,  564, 
2c956 20 34 31 32 2c 20 20 34 31 33 2c 20 20 20 33 30   412,  413,   30
2c957 2c 20 20 35 36 33 2c 20 20 20 33 32 2c 20 20 31  ,  563,   32,  1
2c958 33 34 2c 20 20 33 32 38 2c 20 20 34 33 34 2c 0a  34,  328,  434,.
2c959 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 20   /*   530 */    
2c95a 20 38 2c 20 20 35 34 36 2c 20 20 34 38 34 2c 20   8,  546,  484, 
2c95b 20 34 34 35 2c 20 20 34 35 31 2c 20 20 34 30 30   445,  451,  400
2c95c 2c 20 20 34 38 39 2c 20 20 34 33 34 2c 20 20 20  ,  489,  434,   
2c95d 37 32 2c 20 20 33 37 37 2c 0a 20 2f 2a 20 20 20  72,  377,. /*   
2c95e 35 34 30 20 2a 2f 20 20 20 34 33 35 2c 20 20 34  540 */   435,  4
2c95f 38 35 2c 20 20 36 30 33 2c 20 20 20 36 33 2c 20  85,  603,   63, 
2c960 20 35 39 38 2c 20 20 34 39 34 2c 20 20 34 31 34   598,  494,  414
2c961 2c 20 20 33 37 32 2c 20 20 34 36 39 2c 20 20 33  ,  372,  469,  3
2c962 33 37 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f  37,. /*   550 */
2c963 20 20 20 32 30 30 2c 20 20 20 32 33 2c 20 20 20     200,   23,   
2c964 32 32 2c 20 20 33 30 38 2c 20 20 34 35 35 2c 20  22,  308,  455, 
2c965 20 34 35 36 2c 20 20 34 35 32 2c 20 20 34 35 32   456,  452,  452
2c966 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 0a 20 2f  ,   25,   25,. /
2c967 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 20 32 34  *   560 */    24
2c968 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20  ,   24,   24,   
2c969 32 34 2c 20 20 33 38 36 2c 20 20 20 32 36 2c 20  24,  386,   26, 
2c96a 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36    26,   26,   26
2c96b 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 35 37  ,   27,. /*   57
2c96c 30 20 2a 2f 20 20 20 20 32 37 2c 20 20 20 32 38  0 */    27,   28
2c96d 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20  ,   28,   28,   
2c96e 32 39 2c 20 20 32 31 36 2c 20 20 33 30 34 2c 20  29,  216,  304, 
2c96f 20 34 31 35 2c 20 20 34 31 36 2c 20 20 34 37 36   415,  416,  476
2c970 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20  ,. /*   580 */  
2c971 20 35 32 37 2c 20 20 20 36 30 2c 20 20 34 31 35   527,   60,  415
2c972 2c 20 20 34 31 36 2c 20 20 32 32 32 2c 20 20 34  ,  416,  222,  4
2c973 30 32 2c 20 20 33 38 39 2c 20 20 34 39 38 2c 20  02,  389,  498, 
2c974 20 31 38 35 2c 20 20 33 33 31 2c 0a 20 2f 2a 20   185,  331,. /* 
2c975 20 20 35 39 30 20 2a 2f 20 20 20 34 31 35 2c 20    590 */   415, 
2c976 20 34 31 36 2c 20 20 33 38 35 2c 20 20 32 34 32   416,  385,  242
2c977 2c 20 20 33 33 31 2c 20 20 34 37 37 2c 20 20 33  ,  331,  477,  3
2c978 33 31 2c 20 20 35 30 36 2c 20 20 34 35 33 2c 20  31,  506,  453, 
2c979 20 33 33 36 2c 0a 20 2f 2a 20 20 20 36 30 30 20   336,. /*   600 
2c97a 2a 2f 20 20 20 34 36 32 2c 20 20 34 36 33 2c 20  */   462,  463, 
2c97b 20 34 31 34 2c 20 20 34 39 30 2c 20 20 33 32 38   414,  490,  328
2c97c 2c 20 20 34 34 35 2c 20 20 34 35 31 2c 20 20 34  ,  445,  451,  4
2c97d 31 34 2c 20 20 34 39 31 2c 20 20 33 32 38 2c 0a  14,  491,  328,.
2c97e 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 34   /*   610 */   4
2c97f 37 38 2c 20 20 33 32 38 2c 20 20 35 31 38 2c 20  78,  328,  518, 
2c980 20 34 33 34 2c 20 20 20 36 37 2c 20 20 36 31 33   434,   67,  613
2c981 2c 20 20 34 30 37 2c 20 20 34 30 34 2c 20 20 34  ,  407,  404,  4
2c982 33 34 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20  34,   76,. /*   
2c983 36 32 30 20 2a 2f 20 20 20 34 33 34 2c 20 20 20  620 */   434,   
2c984 39 37 2c 20 20 35 31 39 2c 20 20 20 32 33 2c 20  97,  519,   23, 
2c985 20 20 32 32 2c 20 20 33 30 38 2c 20 20 34 35 35    22,  308,  455
2c986 2c 20 20 34 35 36 2c 20 20 34 35 32 2c 20 20 34  ,  456,  452,  4
2c987 35 32 2c 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f  52,. /*   630 */
2c988 20 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20      25,   25,   
2c989 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20  24,   24,   24, 
2c98a 20 20 32 34 2c 20 20 33 33 31 2c 20 20 20 32 36    24,  331,   26
2c98b 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 0a 20 2f  ,   26,   26,. /
2c98c 2a 20 20 20 36 34 30 20 2a 2f 20 20 20 20 32 36  *   640 */    26
2c98d 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20  ,   27,   27,   
2c98e 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20  28,   28,   28, 
2c98f 20 20 32 39 2c 20 20 32 31 36 2c 20 20 33 30 34    29,  216,  304
2c990 2c 20 20 33 33 31 2c 0a 20 2f 2a 20 20 20 36 35  ,  331,. /*   65
2c991 30 20 2a 2f 20 20 20 33 31 30 2c 20 20 33 32 38  0 */   310,  328
2c992 2c 20 20 32 36 38 2c 20 20 33 36 38 2c 20 20 20  ,  268,  368,   
2c993 36 34 2c 20 20 33 33 31 2c 20 20 33 39 37 2c 20  64,  331,  397, 
2c994 20 34 33 39 2c 20 20 34 33 38 2c 20 20 32 33 33   439,  438,  233
2c995 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20  ,. /*   660 */  
2c996 20 34 33 34 2c 20 20 20 39 36 2c 20 20 32 31 37   434,   96,  217
2c997 2c 20 20 20 31 36 2c 20 20 33 32 38 2c 20 20 31  ,   16,  328,  1
2c998 38 33 2c 20 20 33 33 31 2c 20 20 34 35 39 2c 20  83,  331,  459, 
2c999 20 34 36 37 2c 20 20 32 31 38 2c 0a 20 2f 2a 20   467,  218,. /* 
2c99a 20 20 36 37 30 20 2a 2f 20 20 20 33 32 38 2c 20    670 */   328, 
2c99b 20 34 34 36 2c 20 20 34 34 37 2c 20 20 34 33 34   446,  447,  434
2c99c 2c 20 20 31 30 31 2c 20 20 32 31 34 2c 20 20 31  ,  101,  214,  1
2c99d 35 34 2c 20 20 34 34 35 2c 20 20 34 35 31 2c 20  54,  445,  451, 
2c99e 20 34 33 34 2c 0a 20 2f 2a 20 20 20 36 38 30 20   434,. /*   680 
2c99f 2a 2f 20 20 20 20 39 39 2c 20 20 33 32 38 2c 20  */    99,  328, 
2c9a0 20 34 36 34 2c 20 20 31 35 34 2c 20 20 35 36 36   464,  154,  566
2c9a1 2c 20 20 34 36 36 2c 20 20 33 33 34 2c 20 20 33  ,  466,  334,  3
2c9a2 39 36 2c 20 20 34 35 38 2c 20 20 34 35 38 2c 0a  96,  458,  458,.
2c9a3 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20 20 34   /*   690 */   4
2c9a4 33 34 2c 20 20 31 30 34 2c 20 20 34 34 39 2c 20  34,  104,  449, 
2c9a5 20 34 35 30 2c 20 20 32 30 35 2c 20 20 20 32 33   450,  205,   23
2c9a6 2c 20 20 20 32 32 2c 20 20 33 30 38 2c 20 20 34  ,   22,  308,  4
2c9a7 35 35 2c 20 20 34 35 36 2c 0a 20 2f 2a 20 20 20  55,  456,. /*   
2c9a8 37 30 30 20 2a 2f 20 20 20 34 35 32 2c 20 20 34  700 */   452,  4
2c9a9 35 32 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20  52,   25,   25, 
2c9aa 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34    24,   24,   24
2c9ab 2c 20 20 20 32 34 2c 20 20 33 33 31 2c 20 20 20  ,   24,  331,   
2c9ac 32 36 2c 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f  26,. /*   710 */
2c9ad 20 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20      26,   26,   
2c9ae 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20  26,   27,   27, 
2c9af 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38    28,   28,   28
2c9b0 2c 20 20 20 32 39 2c 20 20 32 31 36 2c 0a 20 2f  ,   29,  216,. /
2c9b1 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 33 30 34  *   720 */   304
2c9b2 2c 20 20 34 34 38 2c 20 20 33 33 31 2c 20 20 33  ,  448,  331,  3
2c9b3 32 38 2c 20 20 33 39 30 2c 20 20 20 35 36 2c 20  28,  390,   56, 
2c9b4 20 34 33 38 2c 20 20 33 33 31 2c 20 20 34 33 35   438,  331,  435
2c9b5 2c 20 20 33 33 31 2c 0a 20 2f 2a 20 20 20 37 33  ,  331,. /*   73
2c9b6 30 20 2a 2f 20 20 20 32 34 38 2c 20 20 33 30 36  0 */   248,  306
2c9b7 2c 20 20 34 33 34 2c 20 20 31 30 35 2c 20 20 35  ,  434,  105,  5
2c9b8 31 36 2c 20 20 35 31 37 2c 20 20 34 37 32 2c 20  16,  517,  472, 
2c9b9 20 33 32 38 2c 20 20 33 30 36 2c 20 20 20 33 39   328,  306,   39
2c9ba 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20  ,. /*   740 */  
2c9bb 20 33 33 31 2c 20 20 20 34 31 2c 20 20 33 32 38   331,   41,  328
2c9bc 2c 20 20 32 36 35 2c 20 20 33 32 38 2c 20 20 34  ,  265,  328,  4
2c9bd 31 34 2c 20 20 34 33 34 2c 20 20 31 32 36 2c 20  14,  434,  126, 
2c9be 20 33 33 39 2c 20 20 34 34 35 2c 0a 20 2f 2a 20   339,  445,. /* 
2c9bf 20 20 37 35 30 20 2a 2f 20 20 20 34 35 31 2c 20    750 */   451, 
2c9c0 20 34 33 34 2c 20 20 31 32 38 2c 20 20 34 33 34   434,  128,  434
2c9c1 2c 20 20 20 35 39 2c 20 20 33 32 38 2c 20 20 33  ,   59,  328,  3
2c9c2 33 34 2c 20 20 32 35 30 2c 20 20 34 35 38 2c 20  34,  250,  458, 
2c9c3 20 34 35 38 2c 0a 20 2f 2a 20 20 20 37 36 30 20   458,. /*   760 
2c9c4 2a 2f 20 20 20 31 39 36 2c 20 20 35 36 39 2c 20  */   196,  569, 
2c9c5 20 35 31 37 2c 20 20 35 36 38 2c 20 20 34 33 34   517,  568,  434
2c9c6 2c 20 20 31 30 32 2c 20 20 34 38 33 2c 20 20 20  ,  102,  483,   
2c9c7 32 33 2c 20 20 20 32 32 2c 20 20 33 30 38 2c 0a  23,   22,  308,.
2c9c8 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20 20 34   /*   770 */   4
2c9c9 35 35 2c 20 20 34 35 36 2c 20 20 34 35 32 2c 20  55,  456,  452, 
2c9ca 20 34 35 32 2c 20 20 20 32 35 2c 20 20 20 32 35   452,   25,   25
2c9cb 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20  ,   24,   24,   
2c9cc 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20  24,   24,. /*   
2c9cd 37 38 30 20 2a 2f 20 20 20 33 33 31 2c 20 20 20  780 */   331,   
2c9ce 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  26,   26,   26, 
2c9cf 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37    26,   27,   27
2c9d0 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20  ,   28,   28,   
2c9d1 32 38 2c 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f  28,. /*   790 */
2c9d2 20 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20 33      29,  216,  3
2c9d3 30 34 2c 20 20 33 33 31 2c 20 20 34 33 35 2c 20  04,  331,  435, 
2c9d4 20 33 32 38 2c 20 20 35 36 32 2c 20 20 33 37 35   328,  562,  375
2c9d5 2c 20 20 35 36 32 2c 20 20 33 33 31 2c 0a 20 2f  ,  562,  331,. /
2c9d6 2a 20 20 20 38 30 30 20 2a 2f 20 20 20 32 36 34  *   800 */   264
2c9d7 2c 20 20 38 35 38 2c 20 20 36 30 36 2c 20 20 33  ,  858,  606,  3
2c9d8 30 33 2c 20 20 34 33 34 2c 20 20 20 37 37 2c 20  03,  434,   77, 
2c9d9 20 31 39 37 2c 20 20 31 33 37 2c 20 20 33 32 38   197,  137,  328
2c9da 2c 20 20 34 30 36 2c 0a 20 2f 2a 20 20 20 38 31  ,  406,. /*   81
2c9db 30 20 2a 2f 20 20 20 20 20 32 2c 20 20 33 33 31  0 */     2,  331
2c9dc 2c 20 20 31 37 38 2c 20 20 32 31 35 2c 20 20 33  ,  178,  215,  3
2c9dd 32 38 2c 20 20 34 31 34 2c 20 20 34 38 36 2c 20  28,  414,  486, 
2c9de 20 34 33 34 2c 20 20 31 30 30 2c 20 20 31 33 36   434,  100,  136
2c9df 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20  ,. /*   820 */  
2c9e0 20 20 31 38 2c 20 20 34 34 35 2c 20 20 34 35 31    18,  445,  451
2c9e1 2c 20 20 34 33 34 2c 20 20 20 36 38 2c 20 20 33  ,  434,   68,  3
2c9e2 33 34 2c 20 20 33 32 38 2c 20 20 34 35 38 2c 20  34,  328,  458, 
2c9e3 20 34 35 38 2c 20 20 36 31 30 2c 0a 20 2f 2a 20   458,  610,. /* 
2c9e4 20 20 38 33 30 20 2a 2f 20 20 20 39 32 36 2c 20    830 */   926, 
2c9e5 20 34 36 30 2c 20 20 39 32 36 2c 20 20 20 36 35   460,  926,   65
2c9e6 2c 20 20 34 31 34 2c 20 20 34 33 34 2c 20 20 20  ,  414,  434,   
2c9e7 39 38 2c 20 20 33 31 34 2c 20 20 35 32 38 2c 20  98,  314,  528, 
2c9e8 20 20 32 33 2c 0a 20 2f 2a 20 20 20 38 34 30 20    23,. /*   840 
2c9e9 2a 2f 20 20 20 20 32 32 2c 20 20 33 30 38 2c 20  */    22,  308, 
2c9ea 20 34 35 35 2c 20 20 34 35 36 2c 20 20 34 35 32   455,  456,  452
2c9eb 2c 20 20 34 35 32 2c 20 20 20 32 35 2c 20 20 20  ,  452,   25,   
2c9ec 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 0a  25,   24,   24,.
2c9ed 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20 20 20   /*   850 */    
2c9ee 32 34 2c 20 20 20 32 34 2c 20 20 33 33 31 2c 20  24,   24,  331, 
2c9ef 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36    26,   26,   26
2c9f0 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20  ,   26,   27,   
2c9f1 32 37 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20  27,   28,. /*   
2c9f2 38 36 30 20 2a 2f 20 20 20 20 32 38 2c 20 20 20  860 */    28,   
2c9f3 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36 2c 20  28,   29,  216, 
2c9f4 20 33 30 34 2c 20 20 33 33 31 2c 20 20 33 35 30   304,  331,  350
2c9f5 2c 20 20 33 32 38 2c 20 20 32 36 34 2c 20 20 31  ,  328,  264,  1
2c9f6 35 35 2c 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f  55,. /*   870 */
2c9f7 20 20 20 32 36 34 2c 20 20 33 33 31 2c 20 20 36     264,  331,  6
2c9f8 30 37 2c 20 20 33 33 31 2c 20 20 32 30 31 2c 20  07,  331,  201, 
2c9f9 20 31 33 37 2c 20 20 34 33 34 2c 20 20 31 32 39   137,  434,  129
2c9fa 2c 20 20 34 33 33 2c 20 20 35 37 34 2c 0a 20 2f  ,  433,  574,. /
2c9fb 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 33 32 38  *   880 */   328
2c9fc 2c 20 20 34 31 34 2c 20 20 34 33 32 2c 20 20 34  ,  414,  432,  4
2c9fd 31 34 2c 20 20 33 33 31 2c 20 20 34 31 34 2c 20  14,  331,  414, 
2c9fe 20 33 32 38 2c 20 20 34 39 38 2c 20 20 33 32 38   328,  498,  328
2c9ff 2c 20 20 34 33 34 2c 0a 20 2f 2a 20 20 20 38 39  ,  434,. /*   89
2ca00 30 20 2a 2f 20 20 20 31 33 30 2c 20 20 34 39 38  0 */   130,  498
2ca01 2c 20 20 35 33 39 2c 20 20 34 34 35 2c 20 20 34  ,  539,  445,  4
2ca02 35 31 2c 20 20 34 33 34 2c 20 20 31 33 31 2c 20  51,  434,  131, 
2ca03 20 34 33 34 2c 20 20 20 36 39 2c 20 20 33 32 38   434,   69,  328
2ca04 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20  ,. /*   900 */  
2ca05 20 33 36 32 2c 20 20 33 33 34 2c 20 20 34 31 34   362,  334,  414
2ca06 2c 20 20 34 35 38 2c 20 20 34 35 38 2c 20 20 32  ,  458,  458,  2
2ca07 32 39 2c 20 20 34 31 34 2c 20 20 33 31 35 2c 20  29,  414,  315, 
2ca08 20 34 33 34 2c 20 20 20 38 30 2c 0a 20 2f 2a 20   434,   80,. /* 
2ca09 20 20 39 31 30 20 2a 2f 20 20 20 32 35 39 2c 20    910 */   259, 
2ca0a 20 20 32 33 2c 20 20 20 33 33 2c 20 20 33 30 38    23,   33,  308
2ca0b 2c 20 20 34 35 35 2c 20 20 34 35 36 2c 20 20 34  ,  455,  456,  4
2ca0c 35 32 2c 20 20 34 35 32 2c 20 20 20 32 35 2c 20  52,  452,   25, 
2ca0d 20 20 32 35 2c 0a 20 2f 2a 20 20 20 39 32 30 20    25,. /*   920 
2ca0e 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20  */    24,   24, 
2ca0f 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33 31    24,   24,  331
2ca10 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20  ,   26,   26,   
2ca11 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 0a  26,   26,   27,.
2ca12 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20 20 20   /*   930 */    
2ca13 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20  27,   28,   28, 
2ca14 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36    28,   29,  216
2ca15 2c 20 20 33 30 34 2c 20 20 33 33 31 2c 20 20 32  ,  304,  331,  2
2ca16 36 34 2c 20 20 33 32 38 2c 0a 20 2f 2a 20 20 20  64,  328,. /*   
2ca17 39 34 30 20 2a 2f 20 20 20 31 36 34 2c 20 20 32  940 */   164,  2
2ca18 36 34 2c 20 20 33 35 36 2c 20 20 33 33 31 2c 20  64,  356,  331, 
2ca19 20 34 31 37 2c 20 20 34 31 38 2c 20 20 34 31 39   417,  418,  419
2ca1a 2c 20 20 35 32 35 2c 20 20 34 33 34 2c 20 20 20  ,  525,  434,   
2ca1b 38 31 2c 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f  81,. /*   950 */
2ca1c 20 20 20 33 37 36 2c 20 20 35 34 32 2c 20 20 33     376,  542,  3
2ca1d 32 38 2c 20 20 34 31 34 2c 20 20 33 31 38 2c 20  28,  414,  318, 
2ca1e 20 33 33 31 2c 20 20 34 31 34 2c 20 20 35 33 38   331,  414,  538
2ca1f 2c 20 20 33 32 38 2c 20 20 34 36 35 2c 0a 20 2f  ,  328,  465,. /
2ca20 2a 20 20 20 39 36 30 20 2a 2f 20 20 20 34 36 35  *   960 */   465
2ca21 2c 20 20 34 33 34 2c 20 20 20 37 30 2c 20 20 33  ,  434,   70,  3
2ca22 32 32 2c 20 20 31 37 35 2c 20 20 34 34 35 2c 20  22,  175,  445, 
2ca23 20 34 35 31 2c 20 20 34 33 34 2c 20 20 20 38 32   451,  434,   82
2ca24 2c 20 20 34 33 33 2c 0a 20 2f 2a 20 20 20 39 37  ,  433,. /*   97
2ca25 30 20 2a 2f 20 20 20 33 32 38 2c 20 20 36 31 30  0 */   328,  610
2ca26 2c 20 20 39 32 35 2c 20 20 34 33 32 2c 20 20 39  ,  925,  432,  9
2ca27 32 35 2c 20 20 33 31 36 2c 20 20 35 34 30 2c 20  25,  316,  540, 
2ca28 20 34 39 34 2c 20 20 33 31 37 2c 20 20 34 33 34   494,  317,  434
2ca29 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20  ,. /*   980 */  
2ca2a 20 20 38 33 2c 20 20 35 32 32 2c 20 20 35 32 33    83,  522,  523
2ca2b 2c 20 20 35 38 37 2c 20 20 20 32 32 2c 20 20 33  ,  587,   22,  3
2ca2c 30 38 2c 20 20 34 35 35 2c 20 20 34 35 36 2c 20  08,  455,  456, 
2ca2d 20 34 35 32 2c 20 20 34 35 32 2c 0a 20 2f 2a 20   452,  452,. /* 
2ca2e 20 20 39 39 30 20 2a 2f 20 20 20 20 32 35 2c 20    990 */    25, 
2ca2f 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34    25,   24,   24
2ca30 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33  ,   24,   24,  3
2ca31 33 31 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20  31,   26,   26, 
2ca32 20 20 32 36 2c 0a 20 2f 2a 20 20 31 30 30 30 20    26,. /*  1000 
2ca33 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c 20  */    26,   27, 
2ca34 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38    27,   28,   28
2ca35 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32  ,   28,   29,  2
2ca36 31 36 2c 20 20 33 30 34 2c 20 20 33 33 31 2c 0a  16,  304,  331,.
2ca37 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20 20 32   /*  1010 */   2
2ca38 30 39 2c 20 20 33 32 38 2c 20 20 32 35 37 2c 20  09,  328,  257, 
2ca39 20 32 34 39 2c 20 20 36 30 37 2c 20 20 33 33 31   249,  607,  331
2ca3a 2c 20 20 32 35 32 2c 20 20 32 30 32 2c 20 20 32  ,  252,  202,  2
2ca3b 30 33 2c 20 20 32 30 34 2c 0a 20 2f 2a 20 20 31  03,  204,. /*  1
2ca3c 30 32 30 20 2a 2f 20 20 20 34 33 34 2c 20 20 20  020 */   434,   
2ca3d 38 34 2c 20 20 32 39 30 2c 20 20 32 35 38 2c 20  84,  290,  258, 
2ca3e 20 33 32 38 2c 20 20 35 33 32 2c 20 20 35 33 31   328,  532,  531
2ca3f 2c 20 20 33 33 31 2c 20 20 34 31 34 2c 20 20 33  ,  331,  414,  3
2ca40 39 34 2c 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f  94,. /*  1030 */
2ca41 20 20 20 33 32 38 2c 20 20 33 33 31 2c 20 20 35     328,  331,  5
2ca42 33 33 2c 20 20 34 33 34 2c 20 20 20 38 36 2c 20  33,  434,   86, 
2ca43 20 33 33 32 2c 20 20 31 34 31 2c 20 20 34 34 35   332,  141,  445
2ca44 2c 20 20 34 35 31 2c 20 20 34 33 34 2c 0a 20 2f  ,  451,  434,. /
2ca45 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 20 38 37  *  1040 */    87
2ca46 2c 20 20 20 34 37 2c 20 20 33 32 38 2c 20 20 34  ,   47,  328,  4
2ca47 39 36 2c 20 20 34 33 35 2c 20 20 31 35 31 2c 20  96,  435,  151, 
2ca48 20 33 32 38 2c 20 20 35 33 37 2c 20 20 34 31 30   328,  537,  410
2ca49 2c 20 20 31 35 35 2c 0a 20 2f 2a 20 20 31 30 35  ,  155,. /*  105
2ca4a 30 20 2a 2f 20 20 20 34 31 34 2c 20 20 34 33 34  0 */   414,  434
2ca4b 2c 20 20 20 38 38 2c 20 20 35 33 34 2c 20 20 34  ,   88,  534,  4
2ca4c 39 34 2c 20 20 34 33 34 2c 20 20 20 37 33 2c 20  94,  434,   73, 
2ca4d 20 33 30 38 2c 20 20 34 35 35 2c 20 20 34 35 36   308,  455,  456
2ca4e 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20  ,. /*  1060 */  
2ca4f 20 34 35 32 2c 20 20 34 35 32 2c 20 20 20 32 35   452,  452,   25
2ca50 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20  ,   25,   24,   
2ca51 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20  24,   24,   24, 
2ca52 20 33 38 36 2c 20 20 20 32 36 2c 0a 20 2f 2a 20   386,   26,. /* 
2ca53 20 31 30 37 30 20 2a 2f 20 20 20 20 32 36 2c 20   1070 */    26, 
2ca54 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37    26,   26,   27
2ca55 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20  ,   27,   28,   
2ca56 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20  28,   28,   29, 
2ca57 20 32 31 36 2c 0a 20 2f 2a 20 20 31 30 38 30 20   216,. /*  1080 
2ca58 2a 2f 20 20 20 20 33 35 2c 20 20 33 33 38 2c 20  */    35,  338, 
2ca59 20 32 38 36 2c 20 20 20 20 33 2c 20 20 33 33 31   286,    3,  331
2ca5a 2c 20 20 34 31 31 2c 20 20 33 33 31 2c 20 20 33  ,  411,  331,  3
2ca5b 32 37 2c 20 20 34 31 33 2c 20 20 34 32 33 2c 0a  27,  413,  423,.
2ca5c 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20 20 33   /*  1090 */   3
2ca5d 38 32 2c 20 20 34 32 32 2c 20 20 32 37 36 2c 20  82,  422,  276, 
2ca5e 20 32 35 34 2c 20 20 34 32 30 2c 20 20 20 33 35   254,  420,   35
2ca5f 2c 20 20 33 33 38 2c 20 20 33 33 35 2c 20 20 20  ,  338,  335,   
2ca60 20 33 2c 20 20 33 32 38 2c 0a 20 2f 2a 20 20 31   3,  328,. /*  1
2ca61 31 30 30 20 2a 2f 20 20 20 34 31 34 2c 20 20 33  100 */   414,  3
2ca62 32 38 2c 20 20 33 32 37 2c 20 20 34 31 33 2c 20  28,  327,  413, 
2ca63 20 34 31 34 2c 20 20 33 31 33 2c 20 20 32 37 36   414,  313,  276
2ca64 2c 20 20 34 31 34 2c 20 20 34 33 34 2c 20 20 20  ,  414,  434,   
2ca65 37 34 2c 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f  74,. /*  1110 */
2ca66 20 20 20 34 33 34 2c 20 20 20 38 39 2c 20 20 33     434,   89,  3
2ca67 33 35 2c 20 20 33 33 31 2c 20 20 34 32 31 2c 20  35,  331,  421, 
2ca68 20 33 34 30 2c 20 20 34 32 34 2c 20 20 33 33 31   340,  424,  331
2ca69 2c 20 20 34 32 35 2c 20 20 33 33 31 2c 0a 20 2f  ,  425,  331,. /
2ca6a 2a 20 20 31 31 32 30 20 2a 2f 20 20 20 32 36 37  *  1120 */   267
2ca6b 2c 20 20 34 31 34 2c 20 20 31 35 35 2c 20 20 31  ,  414,  155,  1
2ca6c 34 32 2c 20 20 32 38 39 2c 20 20 34 37 31 2c 20  42,  289,  471, 
2ca6d 20 32 38 37 2c 20 20 33 32 35 2c 20 20 33 32 38   287,  325,  328
2ca6e 2c 20 20 33 32 36 2c 0a 20 2f 2a 20 20 31 31 33  ,  326,. /*  113
2ca6f 30 20 2a 2f 20 20 20 33 34 30 2c 20 20 34 31 34  0 */   340,  414
2ca70 2c 20 20 33 32 38 2c 20 20 34 31 34 2c 20 20 33  ,  328,  414,  3
2ca71 32 38 2c 20 20 35 38 31 2c 20 20 32 37 36 2c 20  28,  581,  276, 
2ca72 20 34 33 34 2c 20 20 20 39 30 2c 20 20 31 34 34   434,   90,  144
2ca73 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20  ,. /*  1140 */  
2ca74 20 34 37 31 2c 20 20 34 33 34 2c 20 20 20 39 31   471,  434,   91
2ca75 2c 20 20 34 33 34 2c 20 20 20 39 32 2c 20 20 20  ,  434,   92,   
2ca76 33 38 2c 20 20 20 33 37 2c 20 20 20 31 35 2c 20  38,   37,   15, 
2ca77 20 33 33 31 2c 20 20 32 37 37 2c 0a 20 2f 2a 20   331,  277,. /* 
2ca78 20 31 31 35 30 20 2a 2f 20 20 20 31 33 33 2c 20   1150 */   133, 
2ca79 20 34 31 34 2c 20 20 34 34 31 2c 20 20 32 37 36   414,  441,  276
2ca7a 2c 20 20 20 33 36 2c 20 20 33 32 39 2c 20 20 33  ,   36,  329,  3
2ca7b 33 30 2c 20 20 20 20 36 2c 20 20 34 33 36 2c 20  30,    6,  436, 
2ca7c 20 34 34 30 2c 0a 20 2f 2a 20 20 31 31 36 30 20   440,. /*  1160 
2ca7d 2a 2f 20 20 20 20 33 38 2c 20 20 20 33 37 2c 20  */    38,   37, 
2ca7e 20 35 30 34 2c 20 20 33 32 38 2c 20 20 32 30 36   504,  328,  206
2ca7f 2c 20 20 34 31 34 2c 20 20 20 34 37 2c 20 20 34  ,  414,   47,  4
2ca80 31 34 2c 20 20 34 31 34 2c 20 20 20 33 36 2c 0a  14,  414,   36,.
2ca81 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20 20 33   /*  1170 */   3
2ca82 32 39 2c 20 20 33 33 30 2c 20 20 34 33 34 2c 20  29,  330,  434, 
2ca83 20 20 37 35 2c 20 20 34 34 30 2c 20 20 31 39 38    75,  440,  198
2ca84 2c 20 20 33 36 37 2c 20 20 32 31 34 2c 20 20 31  ,  367,  214,  1
2ca85 35 35 2c 20 20 35 38 33 2c 0a 20 2f 2a 20 20 31  55,  583,. /*  1
2ca86 31 38 30 20 2a 2f 20 20 20 32 33 35 2c 20 20 32  180 */   235,  2
2ca87 33 36 2c 20 20 32 33 37 2c 20 20 31 34 33 2c 20  36,  237,  143, 
2ca88 20 32 33 39 2c 20 20 33 34 38 2c 20 20 33 34 33   239,  348,  343
2ca89 2c 20 20 35 38 30 2c 20 20 20 34 36 2c 20 20 32  ,  580,   46,  2
2ca8a 34 33 2c 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f  43,. /*  1190 */
2ca8b 20 20 20 34 34 32 2c 20 20 34 34 32 2c 20 20 34     442,  442,  4
2ca8c 34 32 2c 20 20 34 34 33 2c 20 20 34 34 34 2c 20  42,  443,  444, 
2ca8d 20 20 20 39 2c 20 20 35 38 34 2c 20 20 32 37 36     9,  584,  276
2ca8e 2c 20 20 34 33 35 2c 20 20 20 20 37 2c 0a 20 2f  ,  435,    7,. /
2ca8f 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 31 37 32  *  1200 */   172
2ca90 2c 20 20 34 31 34 2c 20 20 32 39 34 2c 20 20 33  ,  414,  294,  3
2ca91 33 31 2c 20 20 32 38 38 2c 20 20 34 34 32 2c 20  31,  288,  442, 
2ca92 20 34 34 32 2c 20 20 34 34 32 2c 20 20 34 34 33   442,  442,  443
2ca93 2c 20 20 34 34 34 2c 0a 20 2f 2a 20 20 31 32 31  ,  444,. /*  121
2ca94 30 20 2a 2f 20 20 20 20 20 39 2c 20 20 32 39 35  0 */     9,  295
2ca95 2c 20 20 34 31 34 2c 20 20 20 33 35 2c 20 20 33  ,  414,   35,  3
2ca96 33 38 2c 20 20 35 36 37 2c 20 20 20 20 33 2c 20  38,  567,    3, 
2ca97 20 31 35 35 2c 20 20 33 32 38 2c 20 20 34 38 30   155,  328,  480
2ca98 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20  ,. /*  1220 */  
2ca99 20 33 32 37 2c 20 20 34 31 33 2c 20 20 33 33 31   327,  413,  331
2ca9a 2c 20 20 31 37 30 2c 20 20 32 37 36 2c 20 20 35  ,  170,  276,  5
2ca9b 37 31 2c 20 20 32 34 34 2c 20 20 34 33 34 2c 20  71,  244,  434, 
2ca9c 20 20 31 37 2c 20 20 31 36 39 2c 0a 20 2f 2a 20    17,  169,. /* 
2ca9d 20 31 32 33 30 20 2a 2f 20 20 20 33 33 35 2c 20   1230 */   335, 
2ca9e 20 20 31 39 2c 20 20 31 37 31 2c 20 20 32 34 35    19,  171,  245
2ca9f 2c 20 20 34 31 34 2c 20 20 34 31 32 2c 20 20 34  ,  414,  412,  4
2caa0 31 33 2c 20 20 33 32 38 2c 20 20 33 33 31 2c 20  13,  328,  331, 
2caa1 20 34 31 34 2c 0a 20 2f 2a 20 20 31 32 34 30 20   414,. /*  1240 
2caa2 2a 2f 20 20 20 35 38 35 2c 20 20 32 37 36 2c 20  */   585,  276, 
2caa3 20 35 37 35 2c 20 20 35 37 36 2c 20 20 31 35 31   575,  576,  151
2caa4 2c 20 20 20 35 34 2c 20 20 34 33 34 2c 20 20 20  ,   54,  434,   
2caa5 39 34 2c 20 20 33 34 30 2c 20 20 34 39 33 2c 0a  94,  340,  493,.
2caa6 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20 20 34   /*  1250 */   4
2caa7 39 37 2c 20 20 34 39 39 2c 20 20 33 34 36 2c 20  97,  499,  346, 
2caa8 20 33 32 38 2c 20 20 32 37 36 2c 20 20 32 36 32   328,  276,  262
2caa9 2c 20 20 34 31 34 2c 20 20 35 32 39 2c 20 20 34  ,  414,  529,  4
2caaa 37 31 2c 20 20 32 36 33 2c 0a 20 2f 2a 20 20 31  71,  263,. /*  1
2caab 32 36 30 20 2a 2f 20 20 20 32 34 36 2c 20 20 20  260 */   246,   
2caac 32 30 2c 20 20 34 33 34 2c 20 20 20 39 35 2c 20  20,  434,   95, 
2caad 20 34 31 34 2c 20 20 34 31 34 2c 20 20 34 31 34   414,  414,  414
2caae 2c 20 20 32 33 31 2c 20 20 35 34 38 2c 20 20 34  ,  231,  548,  4
2caaf 31 34 2c 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f  14,. /*  1270 */
2cab0 20 20 20 34 31 34 2c 20 20 34 33 37 2c 20 20 34     414,  437,  4
2cab1 31 34 2c 20 20 34 37 31 2c 20 20 34 31 34 2c 20  14,  471,  414, 
2cab2 20 33 39 33 2c 20 20 32 39 31 2c 20 20 32 38 31   393,  291,  281
2cab3 2c 20 20 20 33 38 2c 20 20 20 33 37 2c 0a 20 2f  ,   38,   37,. /
2cab4 2a 20 20 31 32 38 30 20 2a 2f 20 20 20 35 38 38  *  1280 */   588
2cab5 2c 20 20 33 30 35 2c 20 20 20 35 34 2c 20 20 34  ,  305,   54,  4
2cab6 31 34 2c 20 20 32 33 32 2c 20 20 32 36 39 2c 20  14,  232,  269, 
2cab7 20 32 31 34 2c 20 20 20 33 36 2c 20 20 33 32 39   214,   36,  329
2cab8 2c 20 20 33 33 30 2c 0a 20 2f 2a 20 20 31 32 39  ,  330,. /*  129
2cab9 30 20 2a 2f 20 20 20 32 31 39 2c 20 20 32 37 31  0 */   219,  271
2caba 2c 20 20 34 34 30 2c 20 20 31 38 38 2c 20 20 31  ,  440,  188,  1
2cabb 38 39 2c 20 20 32 37 33 2c 20 20 32 37 35 2c 20  89,  273,  275, 
2cabc 20 32 39 32 2c 20 20 20 34 32 2c 20 20 32 38 30   292,   42,  280
2cabd 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20  ,. /*  1300 */  
2cabe 20 34 31 34 2c 20 20 33 38 30 2c 20 20 33 38 33   414,  380,  383
2cabf 2c 20 20 35 31 32 2c 20 20 33 38 34 2c 20 20 32  ,  512,  384,  2
2cac0 38 33 2c 20 20 34 31 34 2c 20 20 34 34 30 2c 20  83,  414,  440, 
2cac1 20 31 34 39 2c 20 20 33 39 39 2c 0a 20 2f 2a 20   149,  399,. /* 
2cac2 20 31 33 31 30 20 2a 2f 20 20 20 34 31 34 2c 20   1310 */   414, 
2cac3 20 34 31 34 2c 20 20 36 30 38 2c 20 20 36 30 35   414,  608,  605
2cac4 2c 20 20 34 31 34 2c 20 20 33 30 32 2c 20 20 34  ,  414,  302,  4
2cac5 31 34 2c 20 20 34 31 34 2c 20 20 32 38 34 2c 20  14,  414,  284, 
2cac6 20 34 31 34 2c 0a 20 2f 2a 20 20 31 33 32 30 20   414,. /*  1320 
2cac7 2a 2f 20 20 20 34 31 34 2c 20 20 32 38 35 2c 20  */   414,  285, 
2cac8 20 35 37 39 2c 20 20 34 34 32 2c 20 20 34 34 32   579,  442,  442
2cac9 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20 20 34  ,  442,  443,  4
2caca 34 34 2c 20 20 20 20 39 2c 20 20 20 34 38 2c 0a  44,    9,   48,.
2cacb 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20 20 35   /*  1330 */   5
2cacc 39 30 2c 20 20 32 39 33 2c 20 20 32 39 36 2c 20  90,  293,  296, 
2cacd 20 34 31 34 2c 20 20 32 39 37 2c 20 20 36 30 32   414,  297,  602
2cace 2c 20 20 34 31 34 2c 20 20 34 31 34 2c 20 20 34  ,  414,  414,  4
2cacf 34 32 2c 20 20 34 34 32 2c 0a 20 2f 2a 20 20 31  42,  442,. /*  1
2cad0 33 34 30 20 2a 2f 20 20 20 34 34 32 2c 20 20 32  340 */   442,  2
2cad1 35 31 2c 20 20 33 35 31 2c 20 20 34 38 31 2c 20  51,  351,  481, 
2cad2 20 31 37 37 2c 20 20 34 31 34 2c 20 20 34 31 34   177,  414,  414
2cad3 2c 20 20 34 31 34 2c 20 20 34 39 35 2c 20 20 34  ,  414,  495,  4
2cad4 31 34 2c 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f  14,. /*  1350 */
2cad5 20 20 20 34 31 34 2c 20 20 35 30 31 2c 20 20 32     414,  501,  2
2cad6 35 33 2c 20 20 32 35 35 2c 20 20 36 30 39 2c 20  53,  255,  609, 
2cad7 20 35 30 32 2c 20 20 33 35 37 2c 20 20 33 31 32   502,  357,  312
2cad8 2c 20 20 35 32 31 2c 20 20 35 31 35 2c 0a 20 2f  ,  521,  515,. /
2cad9 2a 20 20 31 33 36 30 20 2a 2f 20 20 20 31 30 37  *  1360 */   107
2cada 2c 20 20 35 32 35 2c 20 20 32 32 36 2c 20 20 32  ,  525,  226,  2
2cadb 32 35 2c 20 20 35 32 30 2c 20 20 32 36 30 2c 20  25,  520,  260, 
2cadc 20 33 36 39 2c 20 20 31 35 38 2c 20 20 32 32 37   369,  158,  227
2cadd 2c 20 20 35 32 34 2c 0a 20 2f 2a 20 20 31 33 37  ,  524,. /*  137
2cade 30 20 2a 2f 20 20 20 35 35 37 2c 20 20 35 32 36  0 */   557,  526
2cadf 2c 20 20 33 30 30 2c 20 20 35 34 39 2c 20 20 35  ,  300,  549,  5
2cae0 35 30 2c 20 20 35 35 31 2c 20 20 33 37 33 2c 20  50,  551,  373, 
2cae1 20 32 37 38 2c 20 20 31 35 39 2c 20 20 33 30 31   278,  159,  301
2cae2 2c 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f 20 20  ,. /*  1380 */  
2cae3 20 35 34 33 2c 20 20 33 37 31 2c 20 20 31 36 30   543,  371,  160
2cae4 2c 20 20 20 35 31 2c 20 20 32 30 37 2c 20 20 35  ,   51,  207,  5
2cae5 35 39 2c 20 20 35 36 30 2c 20 20 31 36 31 2c 20  59,  560,  161, 
2cae6 20 31 34 30 2c 20 20 33 37 39 2c 0a 20 2f 2a 20   140,  379,. /* 
2cae7 20 31 33 39 30 20 2a 2f 20 20 20 35 37 30 2c 20   1390 */   570, 
2cae8 20 31 36 33 2c 20 20 31 31 37 2c 20 20 33 39 31   163,  117,  391
2cae9 2c 20 20 31 38 31 2c 20 20 33 39 32 2c 20 20 31  ,  181,  392,  1
2caea 31 38 2c 20 20 31 31 39 2c 20 20 31 32 30 2c 20  18,  119,  120, 
2caeb 20 31 32 31 2c 0a 20 2f 2a 20 20 31 34 30 30 20   121,. /*  1400 
2caec 2a 2f 20 20 20 33 32 31 2c 20 20 31 32 33 2c 20  */   321,  123, 
2caed 20 35 37 37 2c 20 20 35 39 39 2c 20 20 36 30 30   577,  599,  600
2caee 2c 20 20 20 35 35 2c 20 20 20 35 38 2c 20 20 35  ,   55,   58,  5
2caef 38 36 2c 20 20 36 30 31 2c 20 20 36 30 34 2c 0a  86,  601,  604,.
2caf0 20 2f 2a 20 20 31 34 31 30 20 2a 2f 20 20 20 20   /*  1410 */    
2caf1 36 32 2c 20 20 33 32 33 2c 20 20 34 30 38 2c 20  62,  323,  408, 
2caf2 20 31 30 33 2c 20 20 32 32 34 2c 20 20 31 31 31   103,  224,  111
2caf3 2c 20 20 31 37 34 2c 20 20 32 33 38 2c 20 20 34  ,  174,  238,  4
2caf4 32 37 2c 20 20 31 39 39 2c 0a 20 2f 2a 20 20 31  27,  199,. /*  1
2caf5 34 32 30 20 2a 2f 20 20 20 36 35 34 2c 20 20 36  420 */   654,  6
2caf6 35 35 2c 20 20 36 35 36 2c 20 20 31 34 36 2c 20  55,  656,  146, 
2caf7 20 31 34 37 2c 20 20 34 35 34 2c 20 20 34 35 37   147,  454,  457
2caf8 2c 20 20 20 33 34 2c 20 20 31 38 32 2c 20 20 31  ,   34,  182,  1
2caf9 38 30 2c 0a 20 2f 2a 20 20 31 34 33 30 20 2a 2f  80,. /*  1430 */
2cafa 20 20 20 34 36 31 2c 20 20 34 37 30 2c 20 20 34     461,  470,  4
2cafb 37 33 2c 20 20 34 37 34 2c 20 20 31 39 35 2c 20  73,  474,  195, 
2cafc 20 20 20 35 2c 20 20 34 39 32 2c 20 20 34 37 35     5,  492,  475
2cafd 2c 20 20 31 34 38 2c 20 20 34 37 39 2c 0a 20 2f  ,  148,  479,. /
2cafe 2a 20 20 31 34 34 30 20 2a 2f 20 20 20 20 34 34  *  1440 */    44
2caff 2c 20 20 31 30 36 2c 20 20 20 31 30 2c 20 20 20  ,  106,   10,   
2cb00 31 31 2c 20 20 31 33 38 2c 20 20 35 30 38 2c 20  11,  138,  508, 
2cb01 20 35 30 39 2c 20 20 35 30 30 2c 20 20 32 32 33   509,  500,  223
2cb02 2c 20 20 20 34 39 2c 0a 20 2f 2a 20 20 31 34 35  ,   49,. /*  145
2cb03 30 20 2a 2f 20 20 20 33 36 31 2c 20 20 31 30 38  0 */   361,  108
2cb04 2c 20 20 31 35 32 2c 20 20 32 36 36 2c 20 20 31  ,  152,  266,  1
2cb05 30 39 2c 20 20 20 35 30 2c 20 20 31 31 30 2c 20  09,   50,  110, 
2cb06 20 31 35 37 2c 20 20 32 35 38 2c 20 20 33 37 30   157,  258,  370
2cb07 2c 0a 20 2f 2a 20 20 31 34 36 30 20 2a 2f 20 20  ,. /*  1460 */  
2cb08 20 31 38 34 2c 20 20 35 35 38 2c 20 20 31 33 39   184,  558,  139
2cb09 2c 20 20 31 31 33 2c 20 20 31 35 31 2c 20 20 31  ,  113,  151,  1
2cb0a 36 32 2c 20 20 32 37 39 2c 20 20 31 31 35 2c 20  62,  279,  115, 
2cb0b 20 33 37 34 2c 20 20 20 31 34 2c 0a 20 2f 2a 20   374,   14,. /* 
2cb0c 20 31 34 37 30 20 2a 2f 20 20 20 35 37 33 2c 20   1470 */   573, 
2cb0d 20 31 31 36 2c 20 20 31 36 35 2c 20 20 20 35 32   116,  165,   52
2cb0e 2c 20 20 20 31 32 2c 20 20 33 36 36 2c 20 20 31  ,   12,  366,  1
2cb0f 36 36 2c 20 20 20 35 33 2c 20 20 35 37 38 2c 20  66,   53,  578, 
2cb10 20 31 36 37 2c 0a 20 2f 2a 20 20 31 34 38 30 20   167,. /*  1480 
2cb11 2a 2f 20 20 20 35 38 32 2c 20 20 31 36 38 2c 20  */   582,  168, 
2cb12 20 31 31 34 2c 20 20 31 32 34 2c 20 20 31 32 32   114,  124,  122
2cb13 2c 20 20 35 36 31 2c 20 20 35 36 35 2c 20 20 20  ,  561,  565,   
2cb14 31 33 2c 20 20 20 36 31 2c 20 20 31 32 35 2c 0a  13,   61,  125,.
2cb15 20 2f 2a 20 20 31 34 39 30 20 2a 2f 20 20 20 35   /*  1490 */   5
2cb16 39 37 2c 20 20 31 37 33 2c 20 20 32 39 38 2c 20  97,  173,  298, 
2cb17 20 31 38 37 2c 20 20 35 38 39 2c 20 20 34 30 35   187,  589,  405
2cb18 2c 20 20 39 33 36 2c 20 20 36 31 31 2c 20 20 39  ,  936,  611,  9
2cb19 33 36 2c 20 20 39 33 36 2c 0a 20 2f 2a 20 20 31  36,  936,. /*  1
2cb1a 35 30 30 20 2a 2f 20 20 20 34 30 31 2c 0a 7d 3b  500 */   401,.};
2cb1b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59  .static const YY
2cb1c 43 4f 44 45 54 59 50 45 20 79 79 5f 6c 6f 6f 6b  CODETYPE yy_look
2cb1d 61 68 65 61 64 5b 5d 20 3d 20 7b 0a 20 2f 2a 20  ahead[] = {. /* 
2cb1e 20 20 20 20 30 20 2a 2f 20 20 20 20 31 39 2c 20      0 */    19, 
2cb1f 20 31 34 32 2c 20 20 31 34 33 2c 20 20 31 34 34   142,  143,  144
2cb20 2c 20 20 31 34 35 2c 20 20 20 32 34 2c 20 20 31  ,  145,   24,  1
2cb21 31 36 2c 20 20 20 32 36 2c 20 20 20 37 35 2c 20  16,   26,   75, 
2cb22 20 20 37 36 2c 0a 20 2f 2a 20 20 20 20 31 30 20    76,. /*    10 
2cb23 2a 2f 20 20 20 20 37 37 2c 20 20 20 37 38 2c 20  */    77,   78, 
2cb24 20 20 32 35 2c 20 20 20 38 30 2c 20 20 20 38 31    25,   80,   81
2cb25 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
2cb26 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 0a  84,   85,   86,.
2cb27 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 20   /*    20 */    
2cb28 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
2cb29 20 20 39 30 2c 20 20 20 32 36 2c 20 20 20 32 37    90,   26,   27
2cb2a 2c 20 20 31 36 30 2c 20 20 20 32 36 2c 20 20 20  ,  160,   26,   
2cb2b 32 37 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20  27,   48,. /*   
2cb2c 20 33 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 20   30 */    49,   
2cb2d 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  79,   80,   81, 
2cb2e 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2cb2f 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
2cb30 38 37 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f  87,. /*    40 */
2cb31 20 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20      88,   89,   
2cb32 39 30 2c 20 20 32 32 32 2c 20 20 32 32 33 2c 20  90,  222,  223, 
2cb33 20 32 32 34 2c 20 20 32 32 35 2c 20 20 20 36 36   224,  225,   66
2cb34 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f  ,   67,   68,. /
2cb35 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 20 36 39  *    50 */    69
2cb36 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
2cb37 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20  72,   73,   74, 
2cb38 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
2cb39 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20 20 36  ,   78,. /*    6
2cb3a 30 20 2a 2f 20 20 20 31 39 34 2c 20 20 20 38 30  0 */   194,   80
2cb3b 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
2cb3c 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
2cb3d 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
2cb3e 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20  ,. /*    70 */  
2cb3f 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39    89,   90,   19
2cb40 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 20  ,   90,   19,   
2cb41 39 34 2c 20 20 31 37 34 2c 20 20 20 32 35 2c 20  94,  174,   25, 
2cb42 20 20 32 35 2c 20 20 20 38 30 2c 0a 20 2f 2a 20    25,   80,. /* 
2cb43 20 20 20 38 30 20 2a 2f 20 20 20 20 38 31 2c 20     80 */    81, 
2cb44 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2cb45 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
2cb46 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
2cb47 20 20 39 30 2c 0a 20 2f 2a 20 20 20 20 39 30 20    90,. /*    90 
2cb48 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c 20  */    26,   27, 
2cb49 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20 39 36    94,   95,   96
2cb4a 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20  ,   94,   95,   
2cb4b 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 0a  99,  100,  101,.
2cb4c 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 20   /*   100 */    
2cb4d 31 39 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20  19,   48,   49, 
2cb4e 20 31 35 30 2c 20 20 31 37 34 2c 20 20 20 35 32   150,  174,   52
2cb4f 2c 20 20 31 31 39 2c 20 20 31 36 36 2c 20 20 31  ,  119,  166,  1
2cb50 31 30 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20  10,   84,. /*   
2cb51 31 31 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 20  110 */    85,   
2cb52 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20  86,   87,   88, 
2cb53 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 32 36    89,   90,   26
2cb54 2c 20 20 20 32 37 2c 20 20 31 36 35 2c 20 20 20  ,   27,  165,   
2cb55 36 36 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f  66,. /*   120 */
2cb56 20 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20      67,   68,   
2cb57 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
2cb58 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34    72,   73,   74
2cb59 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f  ,   75,   76,. /
2cb5a 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 20 37 37  *   130 */    77
2cb5b 2c 20 20 20 37 38 2c 20 20 31 38 36 2c 20 20 20  ,   78,  186,   
2cb5c 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
2cb5d 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
2cb5e 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 31 34  ,   86,. /*   14
2cb5f 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38  0 */    87,   88
2cb60 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20  ,   89,   90,   
2cb61 31 39 2c 20 20 20 39 30 2c 20 20 32 30 35 2c 20  19,   90,  205, 
2cb62 20 20 39 35 2c 20 20 20 38 34 2c 20 20 20 38 35    95,   84,   85
2cb63 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20  ,. /*   150 */  
2cb64 20 20 32 36 2c 20 20 20 39 36 2c 20 20 20 39 37    26,   96,   97
2cb65 2c 20 20 20 39 38 2c 20 20 20 39 39 2c 20 20 31  ,   98,   99,  1
2cb66 30 30 2c 20 20 31 30 31 2c 20 20 31 30 32 2c 20  00,  101,  102, 
2cb67 20 20 39 34 2c 20 20 20 39 35 2c 0a 20 2f 2a 20    94,   95,. /* 
2cb68 20 20 31 36 30 20 2a 2f 20 20 20 31 38 31 2c 20    160 */   181, 
2cb69 20 31 38 32 2c 20 20 31 35 30 2c 20 20 32 32 32   182,  150,  222
2cb6a 2c 20 20 31 30 39 2c 20 20 32 32 34 2c 20 20 32  ,  109,  224,  2
2cb6b 32 35 2c 20 20 31 38 38 2c 20 20 31 30 34 2c 20  25,  188,  104, 
2cb6c 20 31 30 35 2c 0a 20 2f 2a 20 20 20 31 37 30 20   105,. /*   170 
2cb6d 2a 2f 20 20 20 32 31 37 2c 20 20 20 39 30 2c 20  */   217,   90, 
2cb6e 20 31 32 30 2c 20 20 20 34 38 2c 20 20 20 34 39   120,   48,   49
2cb6f 2c 20 20 20 35 30 2c 20 20 20 38 36 2c 20 20 31  ,   50,   86,  1
2cb70 36 35 2c 20 20 20 39 37 2c 20 20 20 39 38 2c 0a  65,   97,   98,.
2cb71 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 20   /*   180 */    
2cb72 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20  99,  100,  101, 
2cb73 20 31 30 32 2c 20 20 20 39 34 2c 20 20 20 39 35   102,   94,   95
2cb74 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
2cb75 20 31 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20   1,    2,. /*   
2cb76 31 39 30 20 2a 2f 20 20 20 31 30 39 2c 20 20 20  190 */   109,   
2cb77 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20  66,   67,   68, 
2cb78 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
2cb79 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
2cb7a 37 34 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f  74,. /*   200 */
2cb7b 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20      75,   76,   
2cb7c 37 37 2c 20 20 20 37 38 2c 20 20 20 31 32 2c 20  77,   78,   12, 
2cb7d 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32    80,   81,   82
2cb7e 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f  ,   83,   84,. /
2cb7f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 20 38 35  *   210 */    85
2cb80 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
2cb81 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
2cb82 20 20 31 39 2c 20 20 20 31 31 2c 20 20 20 39 34    19,   11,   94
2cb83 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 32 32  ,  150,. /*   22
2cb84 30 20 2a 2f 20 20 20 20 32 38 2c 20 20 20 32 34  0 */    28,   24
2cb85 2c 20 20 31 38 36 2c 20 20 31 35 30 2c 20 20 32  ,  186,  150,  2
2cb86 32 32 2c 20 20 31 35 30 2c 20 20 32 32 34 2c 20  22,  150,  224, 
2cb87 20 32 32 35 2c 20 20 32 31 36 2c 20 20 31 31 36   225,  216,  116
2cb88 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20  ,. /*   230 */  
2cb89 20 31 36 31 2c 20 20 31 36 32 2c 20 20 31 35 30   161,  162,  150
2cb8a 2c 20 20 32 32 31 2c 20 20 31 36 35 2c 20 20 20  ,  221,  165,   
2cb8b 34 33 2c 20 20 31 39 35 2c 20 20 20 34 35 2c 20  43,  195,   45, 
2cb8c 20 31 36 35 2c 20 20 31 39 31 2c 0a 20 2f 2a 20   165,  191,. /* 
2cb8d 20 20 32 34 30 20 2a 2f 20 20 20 31 36 35 2c 20    240 */   165, 
2cb8e 20 31 32 38 2c 20 20 31 36 30 2c 20 20 31 37 30   128,  160,  170
2cb8f 2c 20 20 31 37 31 2c 20 20 20 34 38 2c 20 20 20  ,  171,   48,   
2cb90 34 39 2c 20 20 31 36 35 2c 20 20 31 36 33 2c 20  49,  165,  163, 
2cb91 20 31 37 34 2c 0a 20 2f 2a 20 20 20 32 35 30 20   174,. /*   250 
2cb92 2a 2f 20 20 20 31 37 35 2c 20 20 31 36 36 2c 20  */   175,  166, 
2cb93 20 31 36 37 2c 20 20 31 36 38 2c 20 20 31 38 35   167,  168,  185
2cb94 2c 20 20 31 38 36 2c 20 20 31 37 34 2c 20 20 31  ,  186,  174,  1
2cb95 37 35 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 0a  75,  169,  170,.
2cb96 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 31   /*   260 */   1
2cb97 37 31 2c 20 20 20 35 35 2c 20 20 20 36 34 2c 20  71,   55,   64, 
2cb98 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
2cb99 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
2cb9a 37 31 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 20  71,   72,. /*   
2cb9b 32 37 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 20  270 */    73,   
2cb9c 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
2cb9d 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31 39 34    77,   78,  194
2cb9e 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
2cb9f 38 32 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f  82,. /*   280 */
2cba0 20 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20      83,   84,   
2cba1 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
2cba2 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
2cba3 2c 20 20 20 31 39 2c 20 20 32 31 34 2c 0a 20 2f  ,   19,  214,. /
2cba4 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 32 31 35  *   290 */   215
2cba5 2c 20 20 20 32 35 2c 20 20 20 39 34 2c 20 20 31  ,   25,   94,  1
2cba6 37 32 2c 20 20 31 37 33 2c 20 20 31 35 30 2c 20  72,  173,  150, 
2cba7 20 20 39 38 2c 20 20 31 34 38 2c 20 20 32 31 36    98,  148,  216
2cba8 2c 20 20 31 37 33 2c 0a 20 2f 2a 20 20 20 33 30  ,  173,. /*   30
2cba9 30 20 2a 2f 20 20 20 31 34 36 2c 20 20 31 34 37  0 */   146,  147
2cbaa 2c 20 20 31 38 31 2c 20 20 32 32 31 2c 20 20 32  ,  181,  221,  2
2cbab 33 31 2c 20 20 32 33 32 2c 20 20 31 35 32 2c 20  31,  232,  152, 
2cbac 20 31 38 31 2c 20 20 31 35 34 2c 20 20 31 35 30   181,  154,  150
2cbad 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20  ,. /*   310 */  
2cbae 20 31 36 35 2c 20 20 31 30 35 2c 20 20 31 30 36   165,  105,  106
2cbaf 2c 20 20 31 30 37 2c 20 20 31 36 30 2c 20 20 20  ,  107,  160,   
2cbb0 38 39 2c 20 20 20 39 30 2c 20 20 20 34 38 2c 20  89,   90,   48, 
2cbb1 20 20 34 39 2c 20 20 31 37 34 2c 0a 20 2f 2a 20    49,  174,. /* 
2cbb2 20 20 33 32 30 20 2a 2f 20 20 20 31 37 35 2c 20    320 */   175, 
2cbb3 20 32 33 32 2c 20 20 31 31 36 2c 20 20 31 31 32   232,  116,  112
2cbb4 2c 20 20 31 36 35 2c 20 20 31 31 34 2c 20 20 31  ,  165,  114,  1
2cbb5 31 35 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20  15,  129,  130, 
2cbb6 20 31 33 31 2c 0a 20 2f 2a 20 20 20 33 33 30 20   131,. /*   330 
2cbb7 2a 2f 20 20 20 20 35 35 2c 20 20 20 31 36 2c 20  */    55,   16, 
2cbb8 20 31 39 36 2c 20 20 31 37 34 2c 20 20 31 37 35   196,  174,  175
2cbb9 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20  ,   66,   67,   
2cbba 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a  68,   69,   70,.
2cbbb 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 20   /*   340 */    
2cbbc 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
2cbbd 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
2cbbe 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31  ,   77,   78,  1
2cbbf 39 34 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20  94,   80,. /*   
2cbc0 33 35 30 20 2a 2f 20 20 20 20 38 31 2c 20 20 20  350 */    81,   
2cbc1 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
2cbc2 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
2cbc3 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20  ,   88,   89,   
2cbc4 39 30 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f  90,. /*   360 */
2cbc5 20 20 20 20 31 39 2c 20 20 31 36 35 2c 20 20 20      19,  165,   
2cbc6 39 36 2c 20 20 31 35 30 2c 20 20 32 30 38 2c 20  96,  150,  208, 
2cbc7 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31    99,  100,  101
2cbc8 2c 20 20 31 35 30 2c 20 20 20 31 39 2c 0a 20 2f  ,  150,   19,. /
2cbc9 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 31 37 34  *   370 */   174
2cbca 2c 20 20 32 32 36 2c 20 20 32 31 38 2c 20 20 20  ,  226,  218,   
2cbcb 35 38 2c 20 20 32 31 35 2c 20 20 20 36 30 2c 20  58,  215,   60, 
2cbcc 20 31 31 30 2c 20 20 20 32 37 2c 20 20 31 36 35   110,   27,  165
2cbcd 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 33 38  ,  150,. /*   38
2cbce 30 20 2a 2f 20 20 20 31 30 35 2c 20 20 31 30 36  0 */   105,  106
2cbcf 2c 20 20 31 30 37 2c 20 20 31 36 35 2c 20 20 20  ,  107,  165,   
2cbd0 32 36 2c 20 20 20 32 37 2c 20 20 31 35 35 2c 20  26,   27,  155, 
2cbd1 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34 38   174,  175,   48
2cbd2 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20  ,. /*   390 */  
2cbd3 20 20 34 39 2c 20 20 31 31 36 2c 20 20 31 37 34    49,  116,  174
2cbd4 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 31  ,  175,  165,  1
2cbd5 38 33 2c 20 20 20 32 34 2c 20 20 20 32 32 2c 20  83,   24,   22, 
2cbd6 20 20 32 36 2c 20 20 32 34 35 2c 0a 20 2f 2a 20    26,  245,. /* 
2cbd7 20 20 34 30 30 20 2a 2f 20 20 20 31 36 36 2c 20    400 */   166, 
2cbd8 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 37 34    26,   27,  174
2cbd9 2c 20 20 31 37 35 2c 20 20 31 33 36 2c 20 20 31  ,  175,  136,  1
2cbda 36 30 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20  60,   66,   67, 
2cbdb 20 20 36 38 2c 0a 20 2f 2a 20 20 20 34 31 30 20    68,. /*   410 
2cbdc 2a 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20  */    69,   70, 
2cbdd 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
2cbde 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
2cbdf 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a  76,   77,   78,.
2cbe0 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 31   /*   420 */   1
2cbe1 36 30 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  60,   80,   81, 
2cbe2 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2cbe3 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
2cbe4 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20  87,   88,. /*   
2cbe5 34 33 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20  430 */    89,   
2cbe6 39 30 2c 20 20 20 31 39 2c 20 20 32 32 30 2c 20  90,   19,  220, 
2cbe7 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 32 33   150,  150,   23
2cbe8 2c 20 20 31 38 33 2c 20 20 32 32 30 2c 20 20 20  ,  183,  220,   
2cbe9 32 33 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f  23,. /*   440 */
2cbea 20 20 20 31 39 34 2c 20 20 20 32 35 2c 20 20 20     194,   25,   
2cbeb 38 34 2c 20 20 20 39 36 2c 20 20 31 36 30 2c 20  84,   96,  160, 
2cbec 20 32 31 36 2c 20 20 20 39 39 2c 20 20 31 30 30   216,   99,  100
2cbed 2c 20 20 31 30 31 2c 20 20 31 36 35 2c 0a 20 2f  ,  101,  165,. /
2cbee 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 31 36 35  *   450 */   165
2cbef 2c 20 20 31 38 33 2c 20 20 20 39 34 2c 20 20 20  ,  183,   94,   
2cbf0 39 35 2c 20 20 31 39 34 2c 20 20 31 37 30 2c 20  95,  194,  170, 
2cbf1 20 31 35 30 2c 20 20 31 31 30 2c 20 20 31 37 34   150,  110,  174
2cbf2 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 20 34 36  ,  175,. /*   46
2cbf3 30 20 2a 2f 20 20 20 32 32 39 2c 20 20 20 34 38  0 */   229,   48
2cbf4 2c 20 20 20 34 39 2c 20 20 32 33 36 2c 20 20 31  ,   49,  236,  1
2cbf5 35 38 2c 20 20 32 33 34 2c 20 20 20 39 34 2c 20  58,  234,   94, 
2cbf6 20 31 36 31 2c 20 20 31 36 32 2c 20 20 20 39 34   161,  162,   94
2cbf7 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20  ,. /*   470 */  
2cbf8 20 20 39 35 2c 20 20 31 36 35 2c 20 20 20 38 36    95,  165,   86
2cbf9 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20  ,   87,   88,   
2cbfa 38 39 2c 20 20 20 39 30 2c 20 20 31 33 36 2c 20  89,   90,  136, 
2cbfb 20 31 39 34 2c 20 20 20 36 36 2c 0a 20 2f 2a 20   194,   66,. /* 
2cbfc 20 20 34 38 30 20 2a 2f 20 20 20 20 36 37 2c 20    480 */    67, 
2cbfd 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30    68,   69,   70
2cbfe 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20  ,   71,   72,   
2cbff 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20  73,   74,   75, 
2cc00 20 20 37 36 2c 0a 20 2f 2a 20 20 20 34 39 30 20    76,. /*   490 
2cc01 2a 2f 20 20 20 20 37 37 2c 20 20 20 37 38 2c 20  */    77,   78, 
2cc02 20 31 35 30 2c 20 20 20 38 30 2c 20 20 20 38 31   150,   80,   81
2cc03 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
2cc04 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 0a  84,   85,   86,.
2cc05 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 20   /*   500 */    
2cc06 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
2cc07 20 20 39 30 2c 20 20 20 31 39 2c 20 20 31 35 30    90,   19,  150
2cc08 2c 20 20 31 36 30 2c 20 20 31 36 35 2c 20 20 20  ,  160,  165,   
2cc09 32 33 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 20  23,   26,. /*   
2cc0a 35 31 30 20 2a 2f 20 20 20 20 32 37 2c 20 20 20  510 */    27,   
2cc0b 39 35 2c 20 20 31 35 33 2c 20 20 31 35 30 2c 20  95,  153,  150, 
2cc0c 20 20 32 36 2c 20 20 20 32 37 2c 20 20 32 33 31    26,   27,  231
2cc0d 2c 20 20 32 33 32 2c 20 20 20 32 32 2c 20 20 20  ,  232,   22,   
2cc0e 31 39 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f  19,. /*   520 */
2cc0f 20 20 20 31 36 35 2c 20 20 20 32 39 2c 20 20 20     165,   29,   
2cc10 32 36 2c 20 20 20 32 37 2c 20 20 32 32 32 2c 20  26,   27,  222, 
2cc11 20 20 33 33 2c 20 20 32 32 34 2c 20 20 32 32 35    33,  224,  225
2cc12 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 0a 20 2f  ,  165,  174,. /
2cc13 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 31 37 35  *   530 */   175
2cc14 2c 20 20 31 35 30 2c 20 20 20 33 31 2c 20 20 20  ,  150,   31,   
2cc15 34 38 2c 20 20 20 34 39 2c 20 20 31 39 33 2c 20  48,   49,  193, 
2cc16 20 31 32 30 2c 20 20 31 37 34 2c 20 20 31 37 35   120,  174,  175
2cc17 2c 20 20 20 34 37 2c 0a 20 2f 2a 20 20 20 35 34  ,   47,. /*   54
2cc18 30 20 2a 2f 20 20 20 31 39 34 2c 20 20 20 34 30  0 */   194,   40
2cc19 2c 20 20 31 36 31 2c 20 20 31 36 32 2c 20 20 32  ,  161,  162,  2
2cc1a 34 32 2c 20 20 31 36 36 2c 20 20 31 36 35 2c 20  42,  166,  165, 
2cc1b 20 32 33 37 2c 20 20 31 36 36 2c 20 20 31 36 37   237,  166,  167
2cc1c 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20  ,. /*   550 */  
2cc1d 20 31 36 38 2c 20 20 20 36 36 2c 20 20 20 36 37   168,   66,   67
2cc1e 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
2cc1f 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
2cc20 20 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20    73,   74,. /* 
2cc21 20 20 35 36 30 20 2a 2f 20 20 20 20 37 35 2c 20    560 */    75, 
2cc22 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
2cc23 2c 20 20 32 31 38 2c 20 20 20 38 30 2c 20 20 20  ,  218,   80,   
2cc24 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
2cc25 20 20 38 34 2c 0a 20 2f 2a 20 20 20 35 37 30 20    84,. /*   570 
2cc26 2a 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20  */    85,   86, 
2cc27 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
2cc28 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 20  ,   90,   19,   
2cc29 39 34 2c 20 20 20 39 35 2c 20 20 20 31 32 2c 0a  94,   95,   12,.
2cc2a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 20   /*   580 */    
2cc2b 32 33 2c 20 20 32 33 35 2c 20 20 20 39 34 2c 20  23,  235,   94, 
2cc2c 20 20 39 35 2c 20 20 32 30 35 2c 20 20 32 34 33    95,  205,  243
2cc2d 2c 20 20 32 34 30 2c 20 20 31 35 30 2c 20 20 31  ,  240,  150,  1
2cc2e 39 36 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20  96,  150,. /*   
2cc2f 35 39 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 20  590 */    94,   
2cc30 39 35 2c 20 20 31 35 30 2c 20 20 31 39 38 2c 20  95,  150,  198, 
2cc31 20 31 35 30 2c 20 20 20 32 38 2c 20 20 31 35 30   150,   28,  150
2cc32 2c 20 20 20 32 33 2c 20 20 20 39 38 2c 20 20 31  ,   23,   98,  1
2cc33 36 39 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f  69,. /*   600 */
2cc34 20 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31     170,  171,  1
2cc35 36 35 2c 20 20 31 32 30 2c 20 20 31 36 35 2c 20  65,  120,  165, 
2cc36 20 20 34 38 2c 20 20 20 34 39 2c 20 20 31 36 35    48,   49,  165
2cc37 2c 20 20 31 32 30 2c 20 20 31 36 35 2c 0a 20 2f  ,  120,  165,. /
2cc38 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 20 34 33  *   610 */    43
2cc39 2c 20 20 31 36 35 2c 20 20 20 34 35 2c 20 20 31  ,  165,   45,  1
2cc3a 37 34 2c 20 20 31 37 35 2c 20 20 20 20 30 2c 20  74,  175,    0, 
2cc3b 20 20 20 31 2c 20 20 20 20 32 2c 20 20 31 37 34     1,    2,  174
2cc3c 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 20 36 32  ,  175,. /*   62
2cc3d 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35  0 */   174,  175
2cc3e 2c 20 20 20 35 35 2c 20 20 20 36 36 2c 20 20 20  ,   55,   66,   
2cc3f 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20  67,   68,   69, 
2cc40 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
2cc41 2c 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20  ,. /*   630 */  
2cc42 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35    73,   74,   75
2cc43 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
2cc44 37 38 2c 20 20 31 35 30 2c 20 20 20 38 30 2c 20  78,  150,   80, 
2cc45 20 20 38 31 2c 20 20 20 38 32 2c 0a 20 2f 2a 20    81,   82,. /* 
2cc46 20 20 36 34 30 20 2a 2f 20 20 20 20 38 33 2c 20    640 */    83, 
2cc47 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
2cc48 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20  ,   87,   88,   
2cc49 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20  89,   90,   19, 
2cc4a 20 31 35 30 2c 0a 20 2f 2a 20 20 20 36 35 30 20   150,. /*   650 
2cc4b 2a 2f 20 20 20 32 31 33 2c 20 20 31 36 35 2c 20  */   213,  165, 
2cc4c 20 20 32 33 2c 20 20 20 31 39 2c 20 20 20 32 32    23,   19,   22
2cc4d 2c 20 20 31 35 30 2c 20 20 20 39 37 2c 20 20 31  ,  150,   97,  1
2cc4e 36 36 2c 20 20 20 32 36 2c 20 20 32 31 37 2c 0a  66,   26,  217,.
2cc4f 20 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20 20 31   /*   660 */   1
2cc50 37 34 2c 20 20 31 37 35 2c 20 20 32 33 32 2c 20  74,  175,  232, 
2cc51 20 20 32 32 2c 20 20 31 36 35 2c 20 20 20 32 34    22,  165,   24
2cc52 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20 20  ,  150,   23,   
2cc53 38 34 2c 20 20 20 38 35 2c 0a 20 2f 2a 20 20 20  84,   85,. /*   
2cc54 36 37 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 20  670 */   165,   
2cc55 34 38 2c 20 20 20 34 39 2c 20 20 31 37 34 2c 20  48,   49,  174, 
2cc56 20 31 37 35 2c 20 20 31 31 36 2c 20 20 20 34 39   175,  116,   49
2cc57 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 31  ,   48,   49,  1
2cc58 37 34 2c 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f  74,. /*   680 */
2cc59 20 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 32     175,  165,  2
2cc5a 33 33 2c 20 20 20 34 39 2c 20 20 20 32 31 2c 20  33,   49,   21, 
2cc5b 20 32 33 33 2c 20 20 31 31 32 2c 20 20 31 32 38   233,  112,  128
2cc5c 2c 20 20 31 31 34 2c 20 20 31 31 35 2c 0a 20 2f  ,  114,  115,. /
2cc5d 2a 20 20 20 36 39 30 20 2a 2f 20 20 20 31 37 34  *   690 */   174
2cc5e 2c 20 20 31 37 35 2c 20 20 20 36 39 2c 20 20 20  ,  175,   69,   
2cc5f 37 30 2c 20 20 31 36 30 2c 20 20 20 36 36 2c 20  70,  160,   66, 
2cc60 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39    67,   68,   69
2cc61 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 37 30  ,   70,. /*   70
2cc62 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32  0 */    71,   72
2cc63 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20  ,   73,   74,   
2cc64 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
2cc65 20 20 37 38 2c 20 20 31 35 30 2c 20 20 20 38 30    78,  150,   80
2cc66 2c 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20  ,. /*   710 */  
2cc67 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
2cc68 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
2cc69 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20  86,   87,   88, 
2cc6a 20 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20    89,   90,. /* 
2cc6b 20 20 37 32 30 20 2a 2f 20 20 20 20 31 39 2c 20    720 */    19, 
2cc6c 20 20 39 38 2c 20 20 31 35 30 2c 20 20 31 36 35    98,  150,  165
2cc6d 2c 20 20 20 36 31 2c 20 20 20 32 34 2c 20 20 20  ,   61,   24,   
2cc6e 39 34 2c 20 20 31 35 30 2c 20 20 31 39 34 2c 20  94,  150,  194, 
2cc6f 20 31 35 30 2c 0a 20 2f 2a 20 20 20 37 33 30 20   150,. /*   730 
2cc70 2a 2f 20 20 20 31 35 30 2c 20 20 31 30 34 2c 20  */   150,  104, 
2cc71 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 39 30   174,  175,  190
2cc72 2c 20 20 31 39 31 2c 20 20 20 32 33 2c 20 20 31  ,  191,   23,  1
2cc73 36 35 2c 20 20 31 30 34 2c 20 20 31 33 35 2c 0a  65,  104,  135,.
2cc74 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20 20 31   /*   740 */   1
2cc75 35 30 2c 20 20 31 33 37 2c 20 20 31 36 35 2c 20  50,  137,  165, 
2cc76 20 31 30 39 2c 20 20 31 36 35 2c 20 20 31 36 35   109,  165,  165
2cc77 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32  ,  174,  175,  2
2cc78 32 38 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20  28,   48,. /*   
2cc79 37 35 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 31  750 */    49,  1
2cc7a 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c 20  74,  175,  174, 
2cc7b 20 31 37 35 2c 20 20 31 36 35 2c 20 20 31 31 32   175,  165,  112
2cc7c 2c 20 20 32 30 39 2c 20 20 31 31 34 2c 20 20 31  ,  209,  114,  1
2cc7d 31 35 2c 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f  15,. /*   760 */
2cc7e 20 20 20 31 36 30 2c 20 20 31 39 30 2c 20 20 31     160,  190,  1
2cc7f 39 31 2c 20 20 31 30 30 2c 20 20 31 37 34 2c 20  91,  100,  174, 
2cc80 20 31 37 35 2c 20 20 31 37 37 2c 20 20 20 36 36   175,  177,   66
2cc81 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f  ,   67,   68,. /
2cc82 2a 20 20 20 37 37 30 20 2a 2f 20 20 20 20 36 39  *   770 */    69
2cc83 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
2cc84 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20  72,   73,   74, 
2cc85 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
2cc86 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20 37 38  ,   78,. /*   78
2cc87 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 38 30  0 */   150,   80
2cc88 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
2cc89 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
2cc8a 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
2cc8b 2c 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20  ,. /*   790 */  
2cc8c 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39    89,   90,   19
2cc8d 2c 20 20 31 35 30 2c 20 20 31 39 34 2c 20 20 31  ,  150,  194,  1
2cc8e 36 35 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 20  65,  105,  106, 
2cc8f 20 31 30 37 2c 20 20 31 35 30 2c 0a 20 2f 2a 20   107,  150,. /* 
2cc90 20 20 38 30 30 20 2a 2f 20 20 20 31 35 30 2c 20    800 */   150, 
2cc91 20 31 33 38 2c 20 20 32 34 38 2c 20 20 32 34 39   138,  248,  249
2cc92 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32  ,  174,  175,  2
2cc93 30 36 2c 20 20 32 30 37 2c 20 20 31 36 35 2c 20  06,  207,  165, 
2cc94 20 31 34 34 2c 0a 20 2f 2a 20 20 20 38 31 30 20   144,. /*   810 
2cc95 2a 2f 20 20 20 31 34 35 2c 20 20 31 35 30 2c 20  */   145,  150, 
2cc96 20 20 32 33 2c 20 20 31 39 37 2c 20 20 31 36 35    23,  197,  165
2cc97 2c 20 20 31 36 35 2c 20 20 31 37 37 2c 20 20 31  ,  165,  177,  1
2cc98 37 34 2c 20 20 31 37 35 2c 20 20 31 35 30 2c 0a  74,  175,  150,.
2cc99 20 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20 20 32   /*   820 */   2
2cc9a 30 34 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20  04,   48,   49, 
2cc9b 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 31 32   174,  175,  112
2cc9c 2c 20 20 31 36 35 2c 20 20 31 31 34 2c 20 20 31  ,  165,  114,  1
2cc9d 31 35 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20  15,   22,. /*   
2cc9e 38 33 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 20  830 */    23,   
2cc9f 32 33 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20  23,   25,   25, 
2cca0 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35   165,  174,  175
2cca1 2c 20 20 31 38 37 2c 20 20 31 38 32 2c 20 20 20  ,  187,  182,   
2cca2 36 36 2c 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f  66,. /*   840 */
2cca3 20 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20      67,   68,   
2cca4 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
2cca5 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34    72,   73,   74
2cca6 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f  ,   75,   76,. /
2cca7 2a 20 20 20 38 35 30 20 2a 2f 20 20 20 20 37 37  *   850 */    77
2cca8 2c 20 20 20 37 38 2c 20 20 31 35 30 2c 20 20 20  ,   78,  150,   
2cca9 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
2ccaa 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
2ccab 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 38 36  ,   86,. /*   86
2ccac 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38  0 */    87,   88
2ccad 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20  ,   89,   90,   
2ccae 31 39 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  19,  150,  150, 
2ccaf 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 32 35   165,  150,   25
2ccb0 2c 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20  ,. /*   870 */  
2ccb1 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 36 35   150,  150,   65
2ccb2 2c 20 20 31 35 30 2c 20 20 32 30 36 2c 20 20 32  ,  150,  206,  2
2ccb3 30 37 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  07,  174,  175, 
2ccb4 20 31 31 33 2c 20 20 31 39 39 2c 0a 20 2f 2a 20   113,  199,. /* 
2ccb5 20 20 38 38 30 20 2a 2f 20 20 20 31 36 35 2c 20    880 */   165, 
2ccb6 20 31 36 35 2c 20 20 31 31 37 2c 20 20 31 36 35   165,  117,  165
2ccb7 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31  ,  150,  165,  1
2ccb8 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20  65,  150,  165, 
2ccb9 20 31 37 34 2c 0a 20 2f 2a 20 20 20 38 39 30 20   174,. /*   890 
2ccba 2a 2f 20 20 20 31 37 35 2c 20 20 31 35 30 2c 20  */   175,  150, 
2ccbb 20 31 36 36 2c 20 20 20 34 38 2c 20 20 20 34 39   166,   48,   49
2ccbc 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
2ccbd 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 0a  74,  175,  165,.
2ccbe 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20 20 31   /*   900 */   1
2ccbf 37 38 2c 20 20 31 31 32 2c 20 20 31 36 35 2c 20  78,  112,  165, 
2ccc0 20 31 31 34 2c 20 20 31 31 35 2c 20 20 31 38 37   114,  115,  187
2ccc1 2c 20 20 31 36 35 2c 20 20 31 38 37 2c 20 20 31  ,  165,  187,  1
2ccc2 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 20  74,  175,. /*   
2ccc3 39 31 30 20 2a 2f 20 20 20 31 37 37 2c 20 20 20  910 */   177,   
2ccc4 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20  66,   67,   68, 
2ccc5 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
2ccc6 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
2ccc7 37 34 2c 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f  74,. /*   920 */
2ccc8 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20      75,   76,   
2ccc9 37 37 2c 20 20 20 37 38 2c 20 20 31 35 30 2c 20  77,   78,  150, 
2ccca 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32    80,   81,   82
2cccb 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f  ,   83,   84,. /
2cccc 2a 20 20 20 39 33 30 20 2a 2f 20 20 20 20 38 35  *   930 */    85
2cccd 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
2ccce 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
2cccf 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31 35 30    19,  150,  150
2ccd0 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 39 34  ,  165,. /*   94
2ccd1 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 31 35 30  0 */    25,  150
2ccd2 2c 20 20 20 38 36 2c 20 20 31 35 30 2c 20 20 20  ,   86,  150,   
2ccd3 20 37 2c 20 20 20 20 38 2c 20 20 20 20 39 2c 20   7,    8,    9, 
2ccd4 20 31 30 33 2c 20 20 31 37 34 2c 20 20 31 37 35   103,  174,  175
2ccd5 2c 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20  ,. /*   950 */  
2ccd6 20 32 31 33 2c 20 20 31 36 36 2c 20 20 31 36 35   213,  166,  165
2ccd7 2c 20 20 31 36 35 2c 20 20 32 31 33 2c 20 20 31  ,  165,  213,  1
2ccd8 35 30 2c 20 20 31 36 35 2c 20 20 31 37 37 2c 20  50,  165,  177, 
2ccd9 20 31 36 35 2c 20 20 31 32 39 2c 0a 20 2f 2a 20   165,  129,. /* 
2ccda 20 20 39 36 30 20 2a 2f 20 20 20 31 33 30 2c 20    960 */   130, 
2ccdb 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 34 36   174,  175,  246
2ccdc 2c 20 20 32 34 37 2c 20 20 20 34 38 2c 20 20 20  ,  247,   48,   
2ccdd 34 39 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  49,  174,  175, 
2ccde 20 31 31 33 2c 0a 20 2f 2a 20 20 20 39 37 30 20   113,. /*   970 
2ccdf 2a 2f 20 20 20 31 36 35 2c 20 20 20 32 32 2c 20  */   165,   22, 
2cce0 20 20 32 33 2c 20 20 31 31 37 2c 20 20 20 32 35    23,  117,   25
2cce1 2c 20 20 31 38 37 2c 20 20 31 37 37 2c 20 20 31  ,  187,  177,  1
2cce2 36 36 2c 20 20 31 38 37 2c 20 20 31 37 34 2c 0a  66,  187,  174,.
2cce3 20 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20 20 31   /*   980 */   1
2cce4 37 35 2c 20 20 20 20 37 2c 20 20 20 20 38 2c 20  75,    7,    8, 
2cce5 20 31 39 39 2c 20 20 20 36 37 2c 20 20 20 36 38   199,   67,   68
2cce6 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
2cce7 37 31 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 20  71,   72,. /*   
2cce8 39 39 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 20  990 */    73,   
2cce9 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
2ccea 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31 35 30    77,   78,  150
2cceb 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20  ,   80,   81,   
2ccec 38 32 2c 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f  82,. /*  1000 */
2cced 20 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20      83,   84,   
2ccee 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
2ccef 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
2ccf0 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 0a 20 2f  ,   19,  150,. /
2ccf1 2a 20 20 31 30 31 30 20 2a 2f 20 20 20 31 36 30  *  1010 */   160
2ccf2 2c 20 20 31 36 35 2c 20 20 20 39 38 2c 20 20 31  ,  165,   98,  1
2ccf3 35 30 2c 20 20 20 36 35 2c 20 20 31 35 30 2c 20  50,   65,  150, 
2ccf4 20 32 30 35 2c 20 20 31 30 35 2c 20 20 31 30 36   205,  105,  106
2ccf5 2c 20 20 31 30 37 2c 0a 20 2f 2a 20 20 31 30 32  ,  107,. /*  102
2ccf6 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35  0 */   174,  175
2ccf7 2c 20 20 32 30 39 2c 20 20 31 30 39 2c 20 20 31  ,  209,  109,  1
2ccf8 36 35 2c 20 20 20 39 37 2c 20 20 20 39 38 2c 20  65,   97,   98, 
2ccf9 20 31 35 30 2c 20 20 31 36 35 2c 20 20 32 30 39   150,  165,  209
2ccfa 2c 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20  ,. /*  1030 */  
2ccfb 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 33 35   165,  150,   35
2ccfc 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
2ccfd 35 30 2c 20 20 20 20 36 2c 20 20 20 34 38 2c 20  50,    6,   48, 
2ccfe 20 20 34 39 2c 20 20 31 37 34 2c 0a 20 2f 2a 20    49,  174,. /* 
2ccff 20 31 30 34 30 20 2a 2f 20 20 20 31 37 35 2c 20   1040 */   175, 
2cd00 20 31 32 36 2c 20 20 31 36 35 2c 20 20 20 32 33   126,  165,   23
2cd01 2c 20 20 31 39 34 2c 20 20 20 32 35 2c 20 20 31  ,  194,   25,  1
2cd02 36 35 2c 20 20 20 32 33 2c 20 20 31 34 39 2c 20  65,   23,  149, 
2cd03 20 20 32 35 2c 0a 20 2f 2a 20 20 31 30 35 30 20    25,. /*  1050 
2cd04 2a 2f 20 20 20 31 36 35 2c 20 20 31 37 34 2c 20  */   165,  174, 
2cd05 20 31 37 35 2c 20 20 20 35 36 2c 20 20 31 36 36   175,   56,  166
2cd06 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
2cd07 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a  68,   69,   70,.
2cd08 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20 20 20   /*  1060 */    
2cd09 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
2cd0a 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
2cd0b 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 32  ,   77,   78,  2
2cd0c 31 38 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 31  18,   80,. /*  1
2cd0d 30 37 30 20 2a 2f 20 20 20 20 38 31 2c 20 20 20  070 */    81,   
2cd0e 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
2cd0f 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
2cd10 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20  ,   88,   89,   
2cd11 39 30 2c 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f  90,. /*  1080 */
2cd12 20 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 20      19,   20,   
2cd13 31 36 2c 20 20 20 32 32 2c 20 20 31 35 30 2c 20  16,   22,  150, 
2cd14 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 32 36   150,  150,   26
2cd15 2c 20 20 20 32 37 2c 20 20 31 35 30 2c 0a 20 2f  ,   27,  150,. /
2cd16 2a 20 20 31 30 39 30 20 2a 2f 20 20 20 32 34 30  *  1090 */   240
2cd17 2c 20 20 20 31 33 2c 20 20 31 35 30 2c 20 20 32  ,   13,  150,  2
2cd18 30 35 2c 20 20 31 34 39 2c 20 20 20 31 39 2c 20  05,  149,   19, 
2cd19 20 20 32 30 2c 20 20 20 33 36 2c 20 20 20 32 32    20,   36,   22
2cd1a 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 31 30  ,  165,. /*  110
2cd1b 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 36 35  0 */   165,  165
2cd1c 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31  ,   26,   27,  1
2cd1d 36 35 2c 20 20 31 30 38 2c 20 20 31 35 30 2c 20  65,  108,  150, 
2cd1e 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35   165,  174,  175
2cd1f 2c 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20  ,. /*  1110 */  
2cd20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 33 36   174,  175,   36
2cd21 2c 20 20 31 35 30 2c 20 20 31 34 39 2c 20 20 20  ,  150,  149,   
2cd22 35 34 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  54,  150,  150, 
2cd23 20 31 35 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20   150,  150,. /* 
2cd24 20 31 31 32 30 20 2a 2f 20 20 20 20 32 33 2c 20   1120 */    23, 
2cd25 20 31 36 35 2c 20 20 20 32 35 2c 20 20 31 35 31   165,   25,  151
2cd26 2c 20 20 20 35 38 2c 20 20 20 36 34 2c 20 20 20  ,   58,   64,   
2cd27 36 30 2c 20 20 31 34 39 2c 20 20 31 36 35 2c 20  60,  149,  165, 
2cd28 20 31 35 39 2c 0a 20 2f 2a 20 20 31 31 33 30 20   159,. /*  1130 
2cd29 2a 2f 20 20 20 20 35 34 2c 20 20 31 36 35 2c 20  */    54,  165, 
2cd2a 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
2cd2b 2c 20 20 31 39 33 2c 20 20 31 35 30 2c 20 20 31  ,  193,  150,  1
2cd2c 37 34 2c 20 20 31 37 35 2c 20 20 31 35 31 2c 0a  74,  175,  151,.
2cd2d 20 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20 20 20   /*  1140 */    
2cd2e 36 34 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  64,  174,  175, 
2cd2f 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 38 34   174,  175,   84
2cd30 2c 20 20 20 38 35 2c 20 20 20 32 32 2c 20 20 31  ,   85,   22,  1
2cd31 35 30 2c 20 20 31 39 33 2c 0a 20 2f 2a 20 20 31  50,  193,. /*  1
2cd32 31 35 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  150 */   150,  1
2cd33 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  65,  150,  150, 
2cd34 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35    93,   94,   95
2cd35 2c 20 20 20 32 35 2c 20 20 31 39 34 2c 20 20 20  ,   25,  194,   
2cd36 39 38 2c 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f  98,. /*  1160 */
2cd37 20 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20      84,   85,   
2cd38 38 36 2c 20 20 31 36 35 2c 20 20 31 36 30 2c 20  86,  165,  160, 
2cd39 20 31 36 35 2c 20 20 31 32 36 2c 20 20 31 36 35   165,  126,  165
2cd3a 2c 20 20 31 36 35 2c 20 20 20 39 33 2c 0a 20 2f  ,  165,   93,. /
2cd3b 2a 20 20 31 31 37 30 20 2a 2f 20 20 20 20 39 34  *  1170 */    94
2cd3c 2c 20 20 20 39 35 2c 20 20 31 37 34 2c 20 20 31  ,   95,  174,  1
2cd3d 37 35 2c 20 20 20 39 38 2c 20 20 20 20 35 2c 20  75,   98,    5, 
2cd3e 20 20 32 33 2c 20 20 31 31 36 2c 20 20 20 32 35    23,  116,   25
2cd3f 2c 20 20 31 39 33 2c 0a 20 2f 2a 20 20 31 31 38  ,  193,. /*  118
2cd40 30 20 2a 2f 20 20 20 20 31 30 2c 20 20 20 31 31  0 */    10,   11
2cd41 2c 20 20 20 31 32 2c 20 20 20 31 33 2c 20 20 20  ,   12,   13,   
2cd42 31 34 2c 20 20 31 32 33 2c 20 20 31 35 30 2c 20  14,  123,  150, 
2cd43 20 20 31 37 2c 20 20 31 32 34 2c 20 20 31 39 39    17,  124,  199
2cd44 2c 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20  ,. /*  1190 */  
2cd45 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31   129,  130,  131
2cd46 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20 20 31  ,  132,  133,  1
2cd47 33 34 2c 20 20 31 39 33 2c 20 20 31 35 30 2c 20  34,  193,  150, 
2cd48 20 31 39 34 2c 20 20 20 37 34 2c 0a 20 2f 2a 20   194,   74,. /* 
2cd49 20 31 32 30 30 20 2a 2f 20 20 20 20 33 30 2c 20   1200 */    30, 
2cd4a 20 31 36 35 2c 20 20 20 33 32 2c 20 20 31 35 30   165,   32,  150
2cd4b 2c 20 20 31 33 38 2c 20 20 31 32 39 2c 20 20 31  ,  138,  129,  1
2cd4c 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c 20  30,  131,  132, 
2cd4d 20 31 33 33 2c 0a 20 2f 2a 20 20 31 32 31 30 20   133,. /*  1210 
2cd4e 2a 2f 20 20 20 31 33 34 2c 20 20 20 34 31 2c 20  */   134,   41, 
2cd4f 20 31 36 35 2c 20 20 20 31 39 2c 20 20 20 32 30   165,   19,   20
2cd50 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 20  ,   23,   22,   
2cd51 32 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 0a  25,  165,  150,.
2cd52 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20 20 20   /*  1220 */    
2cd53 32 36 2c 20 20 20 32 37 2c 20 20 31 35 30 2c 20  26,   27,  150, 
2cd54 20 20 35 33 2c 20 20 31 35 30 2c 20 20 20 35 35    53,  150,   55
2cd55 2c 20 20 32 30 30 2c 20 20 31 37 34 2c 20 20 31  ,  200,  174,  1
2cd56 37 35 2c 20 20 20 35 39 2c 0a 20 2f 2a 20 20 31  75,   59,. /*  1
2cd57 32 33 30 20 2a 2f 20 20 20 20 33 36 2c 20 20 20  230 */    36,   
2cd58 32 32 2c 20 20 20 36 32 2c 20 20 32 30 31 2c 20  22,   62,  201, 
2cd59 20 31 36 35 2c 20 20 20 32 36 2c 20 20 20 32 37   165,   26,   27
2cd5a 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
2cd5b 36 35 2c 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f  65,. /*  1240 */
2cd5c 20 20 20 31 39 33 2c 20 20 31 35 30 2c 20 20 20     193,  150,   
2cd5d 32 33 2c 20 20 20 32 33 2c 20 20 20 32 35 2c 20  23,   23,   25, 
2cd5e 20 20 32 35 2c 20 20 31 37 34 2c 20 20 31 37 35    25,  174,  175
2cd5f 2c 20 20 20 35 34 2c 20 20 31 35 30 2c 0a 20 2f  ,   54,  150,. /
2cd60 2a 20 20 31 32 35 30 20 2a 2f 20 20 20 31 35 30  *  1250 */   150
2cd61 2c 20 20 31 35 30 2c 20 20 31 32 32 2c 20 20 31  ,  150,  122,  1
2cd62 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  65,  150,  150, 
2cd63 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 36 34   165,  150,   64
2cd64 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 32 36  ,  150,. /*  126
2cd65 30 20 2a 2f 20 20 20 32 30 32 2c 20 20 31 32 35  0 */   202,  125
2cd66 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
2cd67 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  65,  165,  165, 
2cd68 20 31 39 33 2c 20 20 31 35 30 2c 20 20 31 36 35   193,  150,  165
2cd69 2c 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20  ,. /*  1270 */  
2cd6a 20 31 36 35 2c 20 20 32 30 33 2c 20 20 31 36 35   165,  203,  165
2cd6b 2c 20 20 20 36 34 2c 20 20 31 36 35 2c 20 20 31  ,   64,  165,  1
2cd6c 30 35 2c 20 20 31 30 36 2c 20 20 31 30 37 2c 20  05,  106,  107, 
2cd6d 20 20 38 34 2c 20 20 20 38 35 2c 0a 20 2f 2a 20    84,   85,. /* 
2cd6e 20 31 32 38 30 20 2a 2f 20 20 20 20 32 33 2c 20   1280 */    23, 
2cd6f 20 31 31 31 2c 20 20 20 32 35 2c 20 20 31 36 35   111,   25,  165
2cd70 2c 20 20 31 39 33 2c 20 20 31 35 30 2c 20 20 31  ,  193,  150,  1
2cd71 31 36 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20  16,   93,   94, 
2cd72 20 20 39 35 2c 0a 20 2f 2a 20 20 31 32 39 30 20    95,. /*  1290 
2cd73 2a 2f 20 20 20 32 32 37 2c 20 20 31 35 30 2c 20  */   227,  150, 
2cd74 20 20 39 38 2c 20 20 20 38 34 2c 20 20 20 38 35    98,   84,   85
2cd75 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31  ,  150,  150,  1
2cd76 39 33 2c 20 20 31 33 35 2c 20 20 31 35 30 2c 0a  93,  135,  150,.
2cd77 20 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20 20 31   /*  1300 */   1
2cd78 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  65,  150,  150, 
2cd79 20 20 39 34 2c 20 20 31 35 30 2c 20 20 31 35 30    94,  150,  150
2cd7a 2c 20 20 31 36 35 2c 20 20 20 39 38 2c 20 20 31  ,  165,   98,  1
2cd7b 31 38 2c 20 20 31 33 39 2c 0a 20 2f 2a 20 20 31  18,  139,. /*  1
2cd7c 33 31 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31  310 */   165,  1
2cd7d 36 35 2c 20 20 20 32 33 2c 20 20 20 32 33 2c 20  65,   23,   23, 
2cd7e 20 31 36 35 2c 20 20 20 32 35 2c 20 20 31 36 35   165,   25,  165
2cd7f 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
2cd80 36 35 2c 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f  65,. /*  1320 */
2cd81 20 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31     165,  150,  1
2cd82 35 30 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20  50,  129,  130, 
2cd83 20 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33   131,  132,  133
2cd84 2c 20 20 31 33 34 2c 20 20 31 30 34 2c 0a 20 2f  ,  134,  104,. /
2cd85 2a 20 20 31 33 33 30 20 2a 2f 20 20 20 31 35 30  *  1330 */   150
2cd86 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31  ,  150,  150,  1
2cd87 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  65,  150,  150, 
2cd88 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 32 39   165,  165,  129
2cd89 2c 20 20 31 33 30 2c 0a 20 2f 2a 20 20 31 33 34  ,  130,. /*  134
2cd8a 30 20 2a 2f 20 20 20 31 33 31 2c 20 20 32 31 30  0 */   131,  210
2cd8b 2c 20 20 31 32 31 2c 20 20 31 35 37 2c 20 20 31  ,  121,  157,  1
2cd8c 35 37 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  57,  165,  165, 
2cd8d 20 31 36 35 2c 20 20 32 31 31 2c 20 20 31 36 35   165,  211,  165
2cd8e 2c 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20 20  ,. /*  1350 */  
2cd8f 20 31 36 35 2c 20 20 32 31 31 2c 20 20 32 31 30   165,  211,  210
2cd90 2c 20 20 32 31 30 2c 20 20 20 36 35 2c 20 20 32  ,  210,   65,  2
2cd91 31 31 2c 20 20 31 30 34 2c 20 20 20 34 36 2c 20  11,  104,   46, 
2cd92 20 31 38 34 2c 20 20 31 37 36 2c 0a 20 2f 2a 20   184,  176,. /* 
2cd93 20 31 33 36 30 20 2a 2f 20 20 20 20 32 32 2c 20   1360 */    22, 
2cd94 20 31 30 33 2c 20 20 20 39 30 2c 20 20 32 33 30   103,   90,  230
2cd95 2c 20 20 31 37 36 2c 20 20 31 37 36 2c 20 20 20  ,  176,  176,   
2cd96 31 38 2c 20 20 31 35 36 2c 20 20 32 33 30 2c 20  18,  156,  230, 
2cd97 20 31 37 38 2c 0a 20 2f 2a 20 20 31 33 37 30 20   178,. /*  1370 
2cd98 2a 2f 20 20 20 31 35 37 2c 20 20 31 37 36 2c 20  */   157,  176, 
2cd99 20 31 37 39 2c 20 20 31 37 36 2c 20 20 31 37 36   179,  176,  176
2cd9a 2c 20 20 31 37 36 2c 20 20 20 34 34 2c 20 20 32  ,  176,   44,  2
2cd9b 33 38 2c 20 20 31 35 36 2c 20 20 31 37 39 2c 0a  38,  156,  179,.
2cd9c 20 2f 2a 20 20 31 33 38 30 20 2a 2f 20 20 20 31   /*  1380 */   1
2cd9d 38 34 2c 20 20 31 35 37 2c 20 20 31 35 36 2c 20  84,  157,  156, 
2cd9e 20 31 33 35 2c 20 20 31 35 37 2c 20 20 31 35 37   135,  157,  157
2cd9f 2c 20 20 32 33 39 2c 20 20 31 35 36 2c 20 20 20  ,  239,  156,   
2cda0 36 36 2c 20 20 31 35 37 2c 0a 20 2f 2a 20 20 31  66,  157,. /*  1
2cda1 33 39 30 20 2a 2f 20 20 20 31 38 39 2c 20 20 31  390 */   189,  1
2cda2 38 39 2c 20 20 20 32 32 2c 20 20 31 35 37 2c 20  89,   22,  157, 
2cda3 20 32 31 39 2c 20 20 20 31 38 2c 20 20 31 39 32   219,   18,  192
2cda4 2c 20 20 31 39 32 2c 20 20 31 39 32 2c 20 20 31  ,  192,  192,  1
2cda5 39 32 2c 0a 20 2f 2a 20 20 31 34 30 30 20 2a 2f  92,. /*  1400 */
2cda6 20 20 20 31 35 37 2c 20 20 31 38 39 2c 20 20 31     157,  189,  1
2cda7 39 39 2c 20 20 20 33 39 2c 20 20 31 35 37 2c 20  99,   39,  157, 
2cda8 20 32 34 31 2c 20 20 32 34 31 2c 20 20 31 39 39   241,  241,  199
2cda9 2c 20 20 31 35 37 2c 20 20 20 33 37 2c 0a 20 2f  ,  157,   37,. /
2cdaa 2a 20 20 31 34 31 30 20 2a 2f 20 20 20 32 34 34  *  1410 */   244
2cdab 2c 20 20 32 35 30 2c 20 20 20 20 31 2c 20 20 31  ,  250,    1,  1
2cdac 36 34 2c 20 20 31 38 30 2c 20 20 31 38 30 2c 20  64,  180,  180, 
2cdad 20 32 34 37 2c 20 20 20 31 35 2c 20 20 20 32 33   247,   15,   23
2cdae 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 31 34 32  ,   22,. /*  142
2cdaf 30 20 2a 2f 20 20 20 31 31 38 2c 20 20 31 31 38  0 */   118,  118
2cdb0 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31  ,  118,  118,  1
2cdb1 31 38 2c 20 20 20 39 38 2c 20 20 31 31 33 2c 20  18,   98,  113, 
2cdb2 20 20 32 32 2c 20 20 20 32 32 2c 20 20 32 31 39    22,   22,  219
2cdb3 2c 0a 20 2f 2a 20 20 31 34 33 30 20 2a 2f 20 20  ,. /*  1430 */  
2cdb4 20 20 32 33 2c 20 20 20 32 33 2c 20 20 20 31 31    23,   23,   11
2cdb5 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 20  ,   23,   22,   
2cdb6 33 34 2c 20 20 31 32 30 2c 20 20 20 32 33 2c 20  34,  120,   23, 
2cdb7 20 20 32 35 2c 20 20 20 32 33 2c 0a 20 2f 2a 20    25,   23,. /* 
2cdb8 20 31 34 34 30 20 2a 2f 20 20 20 20 32 35 2c 20   1440 */    25, 
2cdb9 20 20 32 32 2c 20 20 20 32 35 2c 20 20 20 33 34    22,   25,   34
2cdba 2c 20 20 31 31 38 2c 20 20 20 32 33 2c 20 20 20  ,  118,   23,   
2cdbb 32 33 2c 20 20 20 32 37 2c 20 20 20 35 30 2c 20  23,   27,   50, 
2cdbc 20 20 32 32 2c 0a 20 2f 2a 20 20 31 34 35 30 20    22,. /*  1450 
2cdbd 2a 2f 20 20 20 20 35 30 2c 20 20 20 32 32 2c 20  */    50,   22, 
2cdbe 20 20 33 34 2c 20 20 20 32 33 2c 20 20 20 32 32    34,   23,   22
2cdbf 2c 20 20 20 32 32 2c 20 20 20 32 32 2c 20 20 31  ,   22,   22,  1
2cdc0 30 32 2c 20 20 31 30 39 2c 20 20 20 31 39 2c 0a  02,  109,   19,.
2cdc1 20 2f 2a 20 20 31 34 36 30 20 2a 2f 20 20 20 20   /*  1460 */    
2cdc2 32 34 2c 20 20 20 32 30 2c 20 20 20 33 38 2c 20  24,   20,   38, 
2cdc3 20 31 30 34 2c 20 20 20 32 35 2c 20 20 31 30 34   104,   25,  104
2cdc4 2c 20 20 31 33 38 2c 20 20 20 32 32 2c 20 20 20  ,  138,   22,   
2cdc5 34 32 2c 20 20 20 20 35 2c 0a 20 2f 2a 20 20 31  42,    5,. /*  1
2cdc6 34 37 30 20 2a 2f 20 20 20 20 20 31 2c 20 20 31  470 */     1,  1
2cdc7 30 38 2c 20 20 31 32 37 2c 20 20 20 37 34 2c 20  08,  127,   74, 
2cdc8 20 20 32 32 2c 20 20 20 35 30 2c 20 20 31 31 39    22,   50,  119
2cdc9 2c 20 20 20 37 34 2c 20 20 20 20 31 2c 20 20 20  ,   74,    1,   
2cdca 31 36 2c 0a 20 2f 2a 20 20 31 34 38 30 20 2a 2f  16,. /*  1480 */
2cdcb 20 20 20 20 32 30 2c 20 20 31 32 31 2c 20 20 20      20,  121,   
2cdcc 35 31 2c 20 20 31 30 38 2c 20 20 31 31 39 2c 20  51,  108,  119, 
2cdcd 20 20 35 37 2c 20 20 20 35 31 2c 20 20 20 32 32    57,   51,   22
2cdce 2c 20 20 20 31 36 2c 20 20 31 32 37 2c 0a 20 2f  ,   16,  127,. /
2cdcf 2a 20 20 31 34 39 30 20 2a 2f 20 20 20 20 32 33  *  1490 */    23
2cdd0 2c 20 20 20 31 35 2c 20 20 31 34 30 2c 20 20 20  ,   15,  140,   
2cdd1 32 32 2c 20 20 31 32 38 2c 20 20 20 20 33 2c 20  22,  128,    3, 
2cdd2 20 32 35 31 2c 20 20 20 20 34 2c 20 20 32 35 31   251,    4,  251
2cdd3 2c 20 20 32 35 31 2c 0a 20 2f 2a 20 20 31 35 30  ,  251,. /*  150
2cdd4 30 20 2a 2f 20 20 20 20 36 33 2c 0a 7d 3b 0a 23  0 */    63,.};.#
2cdd5 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f  define YY_SHIFT_
2cdd6 55 53 45 5f 44 46 4c 54 20 28 2d 31 31 31 29 0a  USE_DFLT (-111).
2cdd7 23 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54  #define YY_SHIFT
2cdd8 5f 4d 41 58 20 34 30 35 0a 73 74 61 74 69 63 20  _MAX 405.static 
2cdd9 63 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 73  const short yy_s
2cdda 68 69 66 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a  hift_ofst[] = {.
2cddb 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 31   /*     0 */   1
2cddc 38 37 2c 20 31 30 36 31 2c 20 31 31 37 30 2c 20  87, 1061, 1170, 
2cddd 31 30 36 31 2c 20 31 31 39 34 2c 20 31 31 39 34  1061, 1194, 1194
2cdde 2c 20 20 20 2d 32 2c 20 20 20 36 34 2c 20 20 2d  ,   -2,   64,  -
2cddf 31 39 2c 20 31 31 39 34 2c 0a 20 2f 2a 20 20 20  19, 1194,. /*   
2cde0 20 31 30 20 2a 2f 20 20 31 31 39 34 2c 20 31 31   10 */  1194, 11
2cde1 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20  94, 1194, 1194, 
2cde2 20 32 37 35 2c 20 20 33 35 38 2c 20 20 20 20 31   275,  358,    1
2cde3 2c 20 20 31 32 35 2c 20 31 30 37 36 2c 20 31 31  ,  125, 1076, 11
2cde4 39 34 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f  94,. /*    20 */
2cde5 20 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31    1194, 1194, 11
2cde6 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20  94, 1194, 1194, 
2cde7 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34  1194, 1194, 1194
2cde8 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 0a 20 2f  , 1194, 1194,. /
2cde9 2a 20 20 20 20 33 30 20 2a 2f 20 20 31 31 39 34  *    30 */  1194
2cdea 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31  , 1194, 1194, 11
2cdeb 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20  94, 1194, 1194, 
2cdec 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34  1194, 1194, 1194
2cded 2c 20 31 31 39 34 2c 0a 20 2f 2a 20 20 20 20 34  , 1194,. /*    4
2cdee 30 20 2a 2f 20 20 31 31 39 34 2c 20 31 31 39 34  0 */  1194, 1194
2cdef 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31  , 1194, 1194, 11
2cdf0 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20  94, 1194, 1194, 
2cdf1 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34  1194, 1194, 1194
2cdf2 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20  ,. /*    50 */  
2cdf3 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34  1194, 1194, 1194
2cdf4 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31  , 1194, 1194, 11
2cdf5 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20  94, 1194, 1194, 
2cdf6 31 31 39 34 2c 20 20 2d 34 38 2c 0a 20 2f 2a 20  1194,  -48,. /* 
2cdf7 20 20 20 36 30 20 2a 2f 20 20 20 32 30 36 2c 20     60 */   206, 
2cdf8 20 20 20 31 2c 20 20 20 20 31 2c 20 20 31 32 34     1,    1,  124
2cdf9 2c 20 20 35 38 34 2c 20 20 35 38 34 2c 20 2d 31  ,  584,  584, -1
2cdfa 31 30 2c 20 20 20 35 33 2c 20 20 31 39 37 2c 20  10,   53,  197, 
2cdfb 20 32 36 39 2c 0a 20 2f 2a 20 20 20 20 37 30 20   269,. /*    70 
2cdfc 2a 2f 20 20 20 33 34 31 2c 20 20 34 31 33 2c 20  */   341,  413, 
2cdfd 20 34 38 35 2c 20 20 35 35 37 2c 20 20 36 32 39   485,  557,  629
2cdfe 2c 20 20 37 30 31 2c 20 20 37 37 33 2c 20 20 38  ,  701,  773,  8
2cdff 34 35 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 0a  45,  773,  773,.
2ce00 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 37   /*    80 */   7
2ce01 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20  73,  773,  773, 
2ce02 20 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33   773,  773,  773
2ce03 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20 37  ,  773,  773,  7
2ce04 37 33 2c 20 20 37 37 33 2c 0a 20 2f 2a 20 20 20  73,  773,. /*   
2ce05 20 39 30 20 2a 2f 20 20 20 37 37 33 2c 20 20 37   90 */   773,  7
2ce06 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20  73,  773,  773, 
2ce07 20 37 37 33 2c 20 20 37 37 33 2c 20 20 39 31 37   773,  773,  917
2ce08 2c 20 20 39 38 39 2c 20 20 39 38 39 2c 20 20 2d  ,  989,  989,  -
2ce09 36 37 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  67,. /*   100 */
2ce0a 20 20 20 2d 36 37 2c 20 20 20 2d 31 2c 20 20 20     -67,   -1,   
2ce0b 2d 31 2c 20 20 20 35 35 2c 20 20 20 32 35 2c 20  -1,   55,   25, 
2ce0c 20 33 38 36 2c 20 20 20 20 31 2c 20 20 20 20 31   386,    1,    1
2ce0d 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 0a 20 2f  ,    1,    1,. /
2ce0e 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 20 20 31  *   110 */     1
2ce0f 2c 20 20 36 33 34 2c 20 20 36 36 33 2c 20 20 20  ,  634,  663,   
2ce10 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20   1,    1,    1, 
2ce11 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2ce12 2c 20 20 20 20 31 2c 0a 20 2f 2a 20 20 20 31 32  ,    1,. /*   12
2ce13 30 20 2a 2f 20 20 20 20 20 31 2c 20 20 20 20 31  0 */     1,    1
2ce14 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20  ,    1,    1,   
2ce15 20 31 2c 20 20 20 20 31 2c 20 20 32 32 36 2c 20   1,    1,  226, 
2ce16 20 31 32 34 2c 20 20 2d 31 37 2c 20 2d 31 31 31   124,  -17, -111
2ce17 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20  ,. /*   130 */  
2ce18 2d 31 31 31 2c 20 2d 31 31 31 2c 20 31 32 30 39  -111, -111, 1209
2ce19 2c 20 20 20 38 31 2c 20 20 33 37 35 2c 20 20 34  ,   81,  375,  4
2ce1a 38 33 2c 20 20 34 38 38 2c 20 20 34 39 36 2c 20  83,  488,  496, 
2ce1b 20 20 39 30 2c 20 20 35 36 37 2c 0a 20 2f 2a 20    90,  567,. /* 
2ce1c 20 20 31 34 30 20 2a 2f 20 20 20 35 36 37 2c 20    140 */   567, 
2ce1d 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2ce1e 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20  ,    1,    1,   
2ce1f 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20   1,    1,    1, 
2ce20 20 20 20 31 2c 0a 20 2f 2a 20 20 20 31 35 30 20     1,. /*   150 
2ce21 2a 2f 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20  */     1,    1, 
2ce22 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2ce23 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20  ,    1,    1,   
2ce24 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 0a   1,    1,    1,.
2ce25 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 20   /*   160 */    
2ce26 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20   1,    1,    1, 
2ce27 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31     1,    1,    1
2ce28 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20  ,    1,    1,   
2ce29 20 31 2c 20 20 20 20 31 2c 0a 20 2f 2a 20 20 20   1,    1,. /*   
2ce2a 31 37 30 20 2a 2f 20 20 20 20 20 31 2c 20 20 20  170 */     1,   
2ce2b 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20   1,    1,    1, 
2ce2c 20 38 30 37 2c 20 20 39 34 39 2c 20 20 36 31 35   807,  949,  615
2ce2d 2c 20 20 33 37 32 2c 20 20 33 37 32 2c 20 20 33  ,  372,  372,  3
2ce2e 37 32 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f  72,. /*   180 */
2ce2f 20 20 20 35 35 39 2c 20 20 31 31 33 2c 20 2d 31     559,  113, -1
2ce30 31 30 2c 20 2d 31 31 30 2c 20 2d 31 31 30 2c 20  10, -110, -110, 
2ce31 2d 31 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31  -111, -111, -111
2ce32 2c 20 20 31 39 38 2c 20 20 31 39 38 2c 0a 20 2f  ,  198,  198,. /
2ce33 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 32 36 36  *   190 */   266
2ce34 2c 20 20 33 34 37 2c 20 20 35 37 34 2c 20 20 36  ,  347,  574,  6
2ce35 34 34 2c 20 20 37 31 33 2c 20 20 31 39 32 2c 20  44,  713,  192, 
2ce36 20 37 38 39 2c 20 20 34 31 36 2c 20 20 39 33 37   789,  416,  937
2ce37 2c 20 20 38 35 36 2c 0a 20 2f 2a 20 20 20 32 30  ,  856,. /*   20
2ce38 30 20 2a 2f 20 20 20 36 33 32 2c 20 20 20 35 32  0 */   632,   52
2ce39 2c 20 20 39 39 37 2c 20 20 39 39 37 2c 20 20 39  ,  997,  997,  9
2ce3a 39 37 2c 20 20 32 31 31 2c 20 20 32 31 31 2c 20  97,  211,  211, 
2ce3b 20 34 39 32 2c 20 20 36 39 31 2c 20 20 32 31 31   492,  691,  211
2ce3c 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20  ,. /*   210 */  
2ce3d 20 32 31 31 2c 20 20 32 31 31 2c 20 20 32 31 31   211,  211,  211
2ce3e 2c 20 20 36 34 31 2c 20 20 37 36 35 2c 20 20 2d  ,  641,  765,  -
2ce3f 31 33 2c 20 20 31 32 34 2c 20 20 38 33 30 2c 20  13,  124,  830, 
2ce40 20 38 33 30 2c 20 20 36 30 34 2c 0a 20 2f 2a 20   830,  604,. /* 
2ce41 20 20 32 32 30 20 2a 2f 20 20 20 35 30 31 2c 20    220 */   501, 
2ce42 20 35 30 31 2c 20 20 33 35 30 2c 20 20 35 30 31   501,  350,  501
2ce43 2c 20 20 36 32 37 2c 20 20 35 30 31 2c 20 20 31  ,  627,  501,  1
2ce44 32 34 2c 20 20 35 30 31 2c 20 20 31 32 34 2c 20  24,  501,  124, 
2ce45 20 38 34 34 2c 0a 20 2f 2a 20 20 20 32 33 30 20   844,. /*   230 
2ce46 2a 2f 20 20 20 39 31 35 2c 20 20 33 35 30 2c 20  */   915,  350, 
2ce47 20 33 35 30 2c 20 20 39 31 35 2c 20 31 30 33 30   350,  915, 1030
2ce48 2c 20 31 30 33 30 2c 20 31 30 33 30 2c 20 31 30  , 1030, 1030, 10
2ce49 33 30 2c 20 31 30 37 38 2c 20 31 30 37 38 2c 0a  30, 1078, 1078,.
2ce4a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31 31   /*   240 */  11
2ce4b 33 32 2c 20 2d 31 31 30 2c 20 31 30 34 30 2c 20  32, -110, 1040, 
2ce4c 31 30 36 32 2c 20 31 30 36 34 2c 20 31 31 33 30  1062, 1064, 1130
2ce4d 2c 20 31 31 33 36 2c 20 31 31 36 33 2c 20 31 31  , 1136, 1163, 11
2ce4e 39 30 2c 20 31 31 39 30 2c 0a 20 2f 2a 20 20 20  90, 1190,. /*   
2ce4f 32 35 30 20 2a 2f 20 20 31 32 32 35 2c 20 31 32  250 */  1225, 12
2ce50 32 31 2c 20 31 32 32 35 2c 20 31 32 32 31 2c 20  21, 1225, 1221, 
2ce51 31 32 32 35 2c 20 31 32 32 31 2c 20 31 32 35 32  1225, 1221, 1252
2ce52 2c 20 31 32 35 32 2c 20 31 33 31 31 2c 20 31 32  , 1252, 1311, 12
2ce53 35 32 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  52,. /*   260 */
2ce54 20 20 31 32 35 38 2c 20 31 32 35 32 2c 20 31 33    1258, 1252, 13
2ce55 33 38 2c 20 31 32 37 32 2c 20 31 32 37 32 2c 20  38, 1272, 1272, 
2ce56 31 33 31 31 2c 20 31 32 35 32 2c 20 31 32 35 32  1311, 1252, 1252
2ce57 2c 20 31 32 35 32 2c 20 31 33 33 38 2c 0a 20 2f  , 1252, 1338,. /
2ce58 2a 20 20 20 32 37 30 20 2a 2f 20 20 31 33 34 38  *   270 */  1348
2ce59 2c 20 31 31 39 30 2c 20 31 33 34 38 2c 20 31 31  , 1190, 1348, 11
2ce5a 39 30 2c 20 31 33 34 38 2c 20 31 31 39 30 2c 20  90, 1348, 1190, 
2ce5b 31 31 39 30 2c 20 31 33 33 32 2c 20 31 32 34 38  1190, 1332, 1248
2ce5c 2c 20 31 33 34 38 2c 0a 20 2f 2a 20 20 20 32 38  , 1348,. /*   28
2ce5d 30 20 2a 2f 20 20 31 31 39 30 2c 20 31 33 32 32  0 */  1190, 1322
2ce5e 2c 20 31 33 32 32 2c 20 31 33 37 30 2c 20 31 30  , 1322, 1370, 10
2ce5f 34 30 2c 20 31 31 39 30 2c 20 31 33 37 37 2c 20  40, 1190, 1377, 
2ce60 31 33 37 37 2c 20 31 33 37 37 2c 20 31 33 37 37  1377, 1377, 1377
2ce61 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20  ,. /*   290 */  
2ce62 31 30 34 30 2c 20 31 33 32 32 2c 20 31 33 37 30  1040, 1322, 1370
2ce63 2c 20 31 31 39 30 2c 20 31 33 36 34 2c 20 31 33  , 1190, 1364, 13
2ce64 36 34 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20  64, 1190, 1190, 
2ce65 31 33 37 32 2c 20 2d 31 31 31 2c 0a 20 2f 2a 20  1372, -111,. /* 
2ce66 20 20 33 30 30 20 2a 2f 20 20 2d 31 31 31 2c 20    300 */  -111, 
2ce67 2d 31 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31  -111, -111, -111
2ce68 2c 20 20 36 32 33 2c 20 31 30 36 36 2c 20 20 39  ,  623, 1066,  9
2ce69 31 32 2c 20 20 33 31 35 2c 20 20 35 30 30 2c 20  12,  315,  500, 
2ce6a 20 38 30 38 2c 0a 20 2f 2a 20 20 20 33 31 30 20   808,. /*   310 
2ce6b 2a 2f 20 20 31 30 32 30 2c 20 20 39 31 34 2c 20  */  1020,  914, 
2ce6c 20 39 37 34 2c 20 20 39 32 38 2c 20 31 30 32 34   974,  928, 1024
2ce6d 2c 20 31 30 39 37 2c 20 31 31 35 33 2c 20 31 31  , 1097, 1153, 11
2ce6e 39 32 2c 20 31 32 31 39 2c 20 31 32 32 30 2c 0a  92, 1219, 1220,.
2ce6f 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 31 32   /*   320 */  12
2ce70 35 37 2c 20 31 31 32 35 2c 20 31 32 39 30 2c 20  57, 1125, 1290, 
2ce71 31 32 38 39 2c 20 31 34 31 31 2c 20 31 34 30 32  1289, 1411, 1402
2ce72 2c 20 31 33 39 35 2c 20 31 33 39 37 2c 20 31 33  , 1395, 1397, 13
2ce73 30 32 2c 20 31 33 30 33 2c 0a 20 2f 2a 20 20 20  02, 1303,. /*   
2ce74 33 33 30 20 2a 2f 20 20 31 33 30 34 2c 20 31 33  330 */  1304, 13
2ce75 30 35 2c 20 31 33 30 36 2c 20 31 33 32 37 2c 20  05, 1306, 1327, 
2ce76 31 33 31 33 2c 20 31 34 30 35 2c 20 31 34 30 37  1313, 1405, 1407
2ce77 2c 20 31 34 30 38 2c 20 31 34 30 36 2c 20 31 34  , 1408, 1406, 14
2ce78 32 31 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f  21,. /*   340 */
2ce79 20 20 31 34 31 32 2c 20 31 34 31 30 2c 20 31 34    1412, 1410, 14
2ce7a 31 33 2c 20 31 34 31 34 2c 20 31 34 31 36 2c 20  13, 1414, 1416, 
2ce7b 31 34 31 35 2c 20 31 34 30 31 2c 20 31 34 31 37  1415, 1401, 1417
2ce7c 2c 20 31 34 30 39 2c 20 31 34 31 35 2c 0a 20 2f  , 1409, 1415,. /
2ce7d 2a 20 20 20 33 35 30 20 2a 2f 20 20 31 33 31 36  *   350 */  1316
2ce7e 2c 20 31 34 31 39 2c 20 31 34 31 38 2c 20 31 34  , 1419, 1418, 14
2ce7f 32 30 2c 20 31 33 32 36 2c 20 31 34 32 32 2c 20  20, 1326, 1422, 
2ce80 31 34 32 33 2c 20 31 34 32 34 2c 20 31 33 39 38  1423, 1424, 1398
2ce81 2c 20 31 34 32 37 2c 0a 20 2f 2a 20 20 20 33 36  , 1427,. /*   36
2ce82 30 20 2a 2f 20 20 31 34 30 30 2c 20 31 34 32 39  0 */  1400, 1429
2ce83 2c 20 31 34 33 30 2c 20 31 34 33 32 2c 20 31 34  , 1430, 1432, 14
2ce84 33 33 2c 20 31 34 32 35 2c 20 31 34 33 34 2c 20  33, 1425, 1434, 
2ce85 31 33 35 35 2c 20 31 33 34 39 2c 20 31 34 34 30  1355, 1349, 1440
2ce86 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20  ,. /*   370 */  
2ce87 31 34 34 31 2c 20 31 34 33 36 2c 20 31 33 35 39  1441, 1436, 1359
2ce88 2c 20 31 34 32 36 2c 20 31 34 32 38 2c 20 31 34  , 1426, 1428, 14
2ce89 33 31 2c 20 31 34 33 39 2c 20 31 34 33 35 2c 20  31, 1439, 1435, 
2ce8a 31 33 32 38 2c 20 31 33 36 31 2c 0a 20 2f 2a 20  1328, 1361,. /* 
2ce8b 20 20 33 38 30 20 2a 2f 20 20 31 34 34 35 2c 20    380 */  1445, 
2ce8c 31 34 36 34 2c 20 31 34 36 39 2c 20 31 33 36 33  1464, 1469, 1363
2ce8d 2c 20 31 33 39 39 2c 20 31 34 30 33 2c 20 31 33  , 1399, 1403, 13
2ce8e 34 35 2c 20 31 34 35 32 2c 20 31 33 35 37 2c 20  45, 1452, 1357, 
2ce8f 31 34 37 37 2c 0a 20 2f 2a 20 20 20 33 39 30 20  1477,. /*   390 
2ce90 2a 2f 20 20 31 34 36 33 2c 20 31 33 36 30 2c 20  */  1463, 1360, 
2ce91 31 34 36 30 2c 20 31 33 36 35 2c 20 31 33 37 35  1460, 1365, 1375
2ce92 2c 20 31 33 36 32 2c 20 31 34 36 35 2c 20 31 33  , 1362, 1465, 13
2ce93 36 36 2c 20 31 34 36 37 2c 20 31 34 37 32 2c 0a  66, 1467, 1472,.
2ce94 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 31 34   /*   400 */  14
2ce95 33 37 2c 20 31 34 37 36 2c 20 31 33 35 32 2c 20  37, 1476, 1352, 
2ce96 31 34 37 31 2c 20 31 34 39 32 2c 20 31 34 39 33  1471, 1492, 1493
2ce97 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f  ,.};.#define YY_
2ce98 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20  REDUCE_USE_DFLT 
2ce99 28 2d 31 38 30 29 0a 23 64 65 66 69 6e 65 20 59  (-180).#define Y
2ce9a 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 33 30 33  Y_REDUCE_MAX 303
2ce9b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 68  .static const sh
2ce9c 6f 72 74 20 79 79 5f 72 65 64 75 63 65 5f 6f 66  ort yy_reduce_of
2ce9d 73 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20  st[] = {. /*    
2ce9e 20 30 20 2a 2f 20 20 2d 31 34 31 2c 20 20 20 38   0 */  -141,   8
2ce9f 32 2c 20 20 31 35 34 2c 20 20 32 38 34 2c 20 20  2,  154,  284,  
2cea0 20 31 32 2c 20 20 20 37 35 2c 20 20 20 36 39 2c   12,   75,   69,
2cea1 20 20 20 37 33 2c 20 20 2d 35 39 2c 20 20 31 34     73,  -59,  14
2cea2 35 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20  5,. /*    10 */ 
2cea3 20 20 31 35 39 2c 20 20 32 32 39 2c 20 20 32 31    159,  229,  21
2cea4 33 2c 20 20 32 31 38 2c 20 20 33 34 36 2c 20 20  3,  218,  346,  
2cea5 32 38 35 2c 20 20 33 30 36 2c 20 20 33 30 32 2c  285,  306,  302,
2cea6 20 20 33 35 35 2c 20 20 33 36 33 2c 0a 20 2f 2a    355,  363,. /*
2cea7 20 20 20 20 32 30 20 2a 2f 20 20 20 34 33 39 2c      20 */   439,
2cea8 20 20 34 34 34 2c 20 20 34 34 36 2c 20 20 34 38    444,  446,  48
2cea9 36 2c 20 20 34 39 39 2c 20 20 35 30 35 2c 20 20  6,  499,  505,  
2ceaa 35 31 36 2c 20 20 35 35 38 2c 20 20 35 37 32 2c  516,  558,  572,
2ceab 20 20 35 37 37 2c 0a 20 2f 2a 20 20 20 20 33 30    577,. /*    30
2ceac 20 2a 2f 20 20 20 35 37 39 2c 20 20 35 39 30 2c   */   579,  590,
2cead 20 20 36 33 30 2c 20 20 36 34 33 2c 20 20 36 34    630,  643,  64
2ceae 39 2c 20 20 36 36 31 2c 20 20 37 30 32 2c 20 20  9,  661,  702,  
2ceaf 37 31 35 2c 20 20 37 32 31 2c 20 20 37 32 33 2c  715,  721,  723,
2ceb0 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20  . /*    40 */   
2ceb1 37 33 34 2c 20 20 37 37 34 2c 20 20 37 38 37 2c  734,  774,  787,
2ceb2 20 20 37 39 33 2c 20 20 38 30 35 2c 20 20 38 34    793,  805,  84
2ceb3 36 2c 20 20 38 35 39 2c 20 20 38 36 35 2c 20 20  6,  859,  865,  
2ceb4 38 37 37 2c 20 20 38 38 31 2c 0a 20 2f 2a 20 20  877,  881,. /*  
2ceb5 20 20 35 30 20 2a 2f 20 20 20 39 33 34 2c 20 20    50 */   934,  
2ceb6 39 33 36 2c 20 20 39 36 33 2c 20 20 39 36 37 2c  936,  963,  967,
2ceb7 20 20 39 36 39 2c 20 20 39 39 38 2c 20 31 30 35    969,  998, 105
2ceb8 33 2c 20 31 30 37 32 2c 20 31 30 38 38 2c 20 2d  3, 1072, 1088, -
2ceb9 31 37 39 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a  179,. /*    60 *
2ceba 2f 20 20 20 38 35 30 2c 20 20 33 34 32 2c 20 20  /   850,  342,  
2cebb 33 38 31 2c 20 20 20 38 35 2c 20 20 20 38 39 2c  381,   85,   89,
2cebc 20 20 34 33 30 2c 20 20 36 30 30 2c 20 20 20 20    430,  600,    
2cebd 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 0a 20  2,    2,    2,. 
2cebe 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 20 20  /*    70 */     
2cebf 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20  2,    2,    2,  
2cec0 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c    2,    2,    2,
2cec1 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20      2,    2,    
2cec2 32 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20 20  2,    2,. /*    
2cec3 38 30 20 2a 2f 20 20 20 20 20 32 2c 20 20 20 20  80 */     2,    
2cec4 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20  2,    2,    2,  
2cec5 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c    2,    2,    2,
2cec6 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20      2,    2,    
2cec7 32 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20  2,. /*    90 */ 
2cec8 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20      2,    2,    
2cec9 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20  2,    2,    2,  
2ceca 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c    2,    2,    2,
2cecb 20 20 20 20 32 2c 20 20 20 20 32 2c 0a 20 2f 2a      2,    2,. /*
2cecc 20 20 20 31 30 30 20 2a 2f 20 20 20 20 20 32 2c     100 */     2,
2cecd 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 31 32      2,    2,  12
2cece 31 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20  1,    2,    2,  
2cecf 34 33 37 2c 20 20 36 35 30 2c 20 20 37 31 38 2c  437,  650,  718,
2ced0 20 20 37 32 30 2c 0a 20 2f 2a 20 20 20 31 31 30    720,. /*   110
2ced1 20 2a 2f 20 20 20 37 38 38 2c 20 20 2d 32 31 2c   */   788,  -21,
2ced2 20 20 32 33 31 2c 20 20 39 35 36 2c 20 20 37 33    231,  956,  73
2ced3 37 2c 20 20 37 39 31 2c 20 20 2d 34 37 2c 20 20  7,  791,  -47,  
2ced4 37 34 31 2c 20 20 39 34 32 2c 20 20 39 38 36 2c  741,  942,  986,
2ced5 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 31  . /*   120 */  1
2ced6 30 30 33 2c 20 31 30 34 37 2c 20 31 30 37 34 2c  003, 1047, 1074,
2ced7 20 31 30 39 31 2c 20 20 34 34 32 2c 20 31 31 30   1091,  442, 110
2ced8 34 2c 20 20 20 20 32 2c 20 20 33 38 32 2c 20 20  4,    2,  382,  
2ced9 20 20 32 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20    2,    2,. /*  
2ceda 20 31 33 30 20 2a 2f 20 20 20 20 20 32 2c 20 20   130 */     2,  
2cedb 20 20 32 2c 20 20 31 39 36 2c 20 20 31 32 36 2c    2,  196,  126,
2cedc 20 20 35 38 30 2c 20 20 36 36 39 2c 20 20 37 31    580,  669,  71
2cedd 36 2c 20 20 38 36 33 2c 20 20 38 38 35 2c 20 20  6,  863,  885,  
2cede 35 34 34 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a  544,. /*   140 *
2cedf 2f 20 20 20 35 37 31 2c 20 20 39 33 35 2c 20 20  /   571,  935,  
2cee0 39 33 39 2c 20 20 39 36 36 2c 20 20 39 36 38 2c  939,  966,  968,
2cee1 20 31 30 30 30 2c 20 20 38 38 35 2c 20 31 30 30   1000,  885, 100
2cee2 32 2c 20 31 30 33 36 2c 20 31 30 36 39 2c 0a 20  2, 1036, 1069,. 
2cee3 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 31 30 39  /*   150 */  109
2cee4 39 2c 20 31 31 30 30 2c 20 31 31 30 31 2c 20 31  9, 1100, 1101, 1
2cee5 31 30 35 2c 20 31 31 30 37 2c 20 31 31 30 39 2c  105, 1107, 1109,
2cee6 20 31 31 31 38 2c 20 31 31 33 35 2c 20 31 31 34   1118, 1135, 114
2cee7 31 2c 20 31 31 34 35 2c 0a 20 2f 2a 20 20 20 31  1, 1145,. /*   1
2cee8 36 30 20 2a 2f 20 20 31 31 34 36 2c 20 31 31 34  60 */  1146, 114
2cee9 39 2c 20 31 31 35 31 2c 20 31 31 35 32 2c 20 31  9, 1151, 1152, 1
2ceea 31 35 34 2c 20 31 31 35 35 2c 20 31 31 36 38 2c  154, 1155, 1168,
2ceeb 20 31 31 37 31 2c 20 31 31 37 32 2c 20 31 31 38   1171, 1172, 118
2ceec 30 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20  0,. /*   170 */ 
2ceed 20 31 31 38 31 2c 20 31 31 38 32 2c 20 31 31 38   1181, 1182, 118
2ceee 34 2c 20 31 31 38 35 2c 20 20 35 35 34 2c 20 20  4, 1185,  554,  
2ceef 35 35 34 2c 20 20 36 36 35 2c 20 20 33 37 39 2c  554,  665,  379,
2cef0 20 20 38 31 31 2c 20 20 38 38 38 2c 0a 20 2f 2a    811,  888,. /*
2cef1 20 20 20 31 38 30 20 2a 2f 20 20 2d 31 33 34 2c     180 */  -134,
2cef2 20 20 32 34 36 2c 20 20 32 36 30 2c 20 20 35 33    246,  260,  53
2cef3 34 2c 20 31 30 30 34 2c 20 20 36 31 36 2c 20 20  4, 1004,  616,  
2cef4 36 36 38 2c 20 20 37 31 37 2c 20 20 2d 39 38 2c  668,  717,  -98,
2cef5 20 20 2d 37 30 2c 0a 20 2f 2a 20 20 20 31 39 30    -70,. /*   190
2cef6 20 2a 2f 20 20 20 2d 35 34 2c 20 20 20 33 36 2c   */   -54,   36,
2cef7 20 20 20 34 31 2c 20 20 20 34 31 2c 20 20 20 34     41,   41,   4
2cef8 31 2c 20 20 20 34 38 2c 20 20 20 34 31 2c 20 20  1,   48,   41,  
2cef9 31 35 36 2c 20 20 31 34 39 2c 20 20 31 33 36 2c  156,  149,  136,
2cefa 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20  . /*   200 */   
2cefb 32 33 34 2c 20 20 31 35 36 2c 20 20 32 31 32 2c  234,  156,  212,
2cefc 20 20 32 35 34 2c 20 20 32 36 38 2c 20 20 20 34    254,  268,   4
2cefd 31 2c 20 20 20 34 31 2c 20 20 32 32 37 2c 20 20  1,   41,  227,  
2cefe 33 31 30 2c 20 20 20 34 31 2c 0a 20 2f 2a 20 20  310,   41,. /*  
2ceff 20 32 31 30 20 2a 2f 20 20 20 20 34 31 2c 20 20   210 */    41,  
2cf00 20 34 31 2c 20 20 20 34 31 2c 20 20 33 35 39 2c   41,   41,  359,
2cf01 20 20 33 39 32 2c 20 20 33 39 35 2c 20 20 34 39    392,  395,  49
2cf02 31 2c 20 20 34 34 39 2c 20 20 34 35 32 2c 20 20  1,  449,  452,  
2cf03 35 32 30 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a  520,. /*   220 *
2cf04 2f 20 20 20 35 38 39 2c 20 20 36 33 39 2c 20 20  /   589,  639,  
2cf05 35 34 38 2c 20 20 37 33 33 2c 20 20 36 35 36 2c  548,  733,  656,
2cf06 20 20 37 38 30 2c 20 20 37 32 36 2c 20 20 37 39    780,  726,  79
2cf07 39 2c 20 20 37 38 35 2c 20 20 37 32 32 2c 0a 20  9,  785,  722,. 
2cf08 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 36 38  /*   230 */   68
2cf09 30 2c 20 20 38 31 33 2c 20 20 38 32 30 2c 20 20  0,  813,  820,  
2cf0a 37 38 34 2c 20 20 38 39 39 2c 20 20 39 34 35 2c  784,  899,  945,
2cf0b 20 20 39 36 35 2c 20 20 39 37 38 2c 20 20 39 37    965,  978,  97
2cf0c 32 2c 20 20 39 38 38 2c 0a 20 2f 2a 20 20 20 32  2,  988,. /*   2
2cf0d 34 30 20 2a 2f 20 20 20 39 37 30 2c 20 20 39 36  40 */   970,  96
2cf0e 34 2c 20 20 39 39 30 2c 20 31 30 32 36 2c 20 31  4,  990, 1026, 1
2cf0f 30 33 32 2c 20 31 30 35 38 2c 20 31 30 36 38 2c  032, 1058, 1068,
2cf10 20 31 30 36 33 2c 20 31 31 38 36 2c 20 31 31 38   1063, 1186, 118
2cf11 37 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20  7,. /*   250 */ 
2cf12 20 31 31 33 31 2c 20 31 31 33 37 2c 20 31 31 34   1131, 1137, 114
2cf13 32 2c 20 31 31 34 30 2c 20 31 31 34 33 2c 20 31  2, 1140, 1143, 1
2cf14 31 34 34 2c 20 31 31 38 33 2c 20 31 31 38 38 2c  144, 1183, 1188,
2cf15 20 31 31 37 34 2c 20 31 31 38 39 2c 0a 20 2f 2a   1174, 1189,. /*
2cf16 20 20 20 32 36 30 20 2a 2f 20 20 31 31 39 31 2c     260 */  1191,
2cf17 20 31 31 39 35 2c 20 31 31 39 33 2c 20 31 31 33   1195, 1193, 113
2cf18 33 2c 20 31 31 33 38 2c 20 31 31 39 36 2c 20 31  3, 1138, 1196, 1
2cf19 31 39 37 2c 20 31 31 39 38 2c 20 31 31 39 39 2c  197, 1198, 1199,
2cf1a 20 31 32 30 30 2c 0a 20 2f 2a 20 20 20 32 37 30   1200,. /*   270
2cf1b 20 2a 2f 20 20 31 32 31 31 2c 20 31 32 31 33 2c   */  1211, 1213,
2cf1c 20 31 32 32 32 2c 20 31 32 32 34 2c 20 31 32 32   1222, 1224, 122
2cf1d 36 2c 20 31 32 32 37 2c 20 31 32 32 38 2c 20 31  6, 1227, 1228, 1
2cf1e 31 33 39 2c 20 31 31 34 37 2c 20 31 32 33 31 2c  139, 1147, 1231,
2cf1f 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 31  . /*   280 */  1
2cf20 32 33 32 2c 20 31 32 30 31 2c 20 31 32 30 32 2c  232, 1201, 1202,
2cf21 20 31 31 37 35 2c 20 31 32 30 33 2c 20 31 32 33   1175, 1203, 123
2cf22 36 2c 20 31 32 30 34 2c 20 31 32 30 35 2c 20 31  6, 1204, 1205, 1
2cf23 32 30 36 2c 20 31 32 30 37 2c 0a 20 2f 2a 20 20  206, 1207,. /*  
2cf24 20 32 39 30 20 2a 2f 20 20 31 32 30 38 2c 20 31   290 */  1208, 1
2cf25 32 31 32 2c 20 31 32 31 30 2c 20 31 32 34 33 2c  212, 1210, 1243,
2cf26 20 31 31 36 34 2c 20 31 31 36 35 2c 20 31 32 34   1164, 1165, 124
2cf27 37 2c 20 31 32 35 31 2c 20 31 31 36 36 2c 20 31  7, 1251, 1166, 1
2cf28 32 34 39 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a  249,. /*   300 *
2cf29 2f 20 20 31 32 33 34 2c 20 31 32 33 35 2c 20 31  /  1234, 1235, 1
2cf2a 31 36 39 2c 20 31 31 36 31 2c 0a 7d 3b 0a 73 74  169, 1161,.};.st
2cf2b 61 74 69 63 20 63 6f 6e 73 74 20 59 59 41 43 54  atic const YYACT
2cf2c 49 4f 4e 54 59 50 45 20 79 79 5f 64 65 66 61 75  IONTYPE yy_defau
2cf2d 6c 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20  lt[] = {. /*    
2cf2e 20 30 20 2a 2f 20 20 20 36 31 38 2c 20 20 38 35   0 */   618,  85
2cf2f 33 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  3,  934,  934,  
2cf30 38 35 33 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  853,  934,  934,
2cf31 20 20 38 38 30 2c 20 20 37 34 31 2c 20 20 38 35    880,  741,  85
2cf32 31 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20  1,. /*    10 */ 
2cf33 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf34 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf35 38 38 30 2c 20 20 39 33 34 2c 20 20 39 30 39 2c  880,  934,  909,
2cf36 20 20 39 33 34 2c 20 20 39 33 34 2c 0a 20 2f 2a    934,  934,. /*
2cf37 20 20 20 20 32 30 20 2a 2f 20 20 20 39 33 34 2c      20 */   934,
2cf38 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf39 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf3a 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf3b 20 20 39 33 34 2c 0a 20 2f 2a 20 20 20 20 33 30    934,. /*    30
2cf3c 20 2a 2f 20 20 20 39 33 34 2c 20 20 39 33 34 2c   */   934,  934,
2cf3d 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf3e 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf3f 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf40 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20  . /*    40 */   
2cf41 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf42 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf43 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf44 39 33 34 2c 20 20 39 33 34 2c 0a 20 2f 2a 20 20  934,  934,. /*  
2cf45 20 20 35 30 20 2a 2f 20 20 20 39 33 34 2c 20 20    50 */   934,  
2cf46 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf47 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf48 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf49 38 32 35 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a  825,. /*    60 *
2cf4a 2f 20 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  /   934,  934,  
2cf4b 39 33 34 2c 20 20 36 35 37 2c 20 20 38 38 30 2c  934,  657,  880,
2cf4c 20 20 38 38 30 2c 20 20 37 34 35 2c 20 20 37 37    880,  745,  77
2cf4d 36 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 0a 20  6,  934,  934,. 
2cf4e 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 39 33  /*    70 */   93
2cf4f 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf50 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf51 20 20 37 37 37 2c 20 20 39 33 34 2c 20 20 38 35    777,  934,  85
2cf52 35 2c 20 20 38 35 30 2c 0a 20 2f 2a 20 20 20 20  5,  850,. /*    
2cf53 38 30 20 2a 2f 20 20 20 38 34 36 2c 20 20 38 34  80 */   846,  84
2cf54 38 2c 20 20 38 34 37 2c 20 20 38 35 34 2c 20 20  8,  847,  854,  
2cf55 37 37 38 2c 20 20 37 36 37 2c 20 20 37 37 34 2c  778,  767,  774,
2cf56 20 20 37 38 31 2c 20 20 37 35 36 2c 20 20 38 39    781,  756,  89
2cf57 33 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20  3,. /*    90 */ 
2cf58 20 20 37 38 33 2c 20 20 37 38 34 2c 20 20 37 39    783,  784,  79
2cf59 30 2c 20 20 37 39 31 2c 20 20 39 31 30 2c 20 20  0,  791,  910,  
2cf5a 39 30 38 2c 20 20 38 31 33 2c 20 20 38 31 32 2c  908,  813,  812,
2cf5b 20 20 38 33 31 2c 20 20 38 31 35 2c 0a 20 2f 2a    831,  815,. /*
2cf5c 20 20 20 31 30 30 20 2a 2f 20 20 20 38 33 37 2c     100 */   837,
2cf5d 20 20 38 31 34 2c 20 20 38 32 34 2c 20 20 36 34    814,  824,  64
2cf5e 39 2c 20 20 38 31 36 2c 20 20 38 31 37 2c 20 20  9,  816,  817,  
2cf5f 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf60 20 20 39 33 34 2c 0a 20 2f 2a 20 20 20 31 31 30    934,. /*   110
2cf61 20 2a 2f 20 20 20 39 33 34 2c 20 20 37 31 30 2c   */   934,  710,
2cf62 20 20 36 34 34 2c 20 20 39 33 34 2c 20 20 39 33    644,  934,  93
2cf63 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf64 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf65 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20  . /*   120 */   
2cf66 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf67 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf68 34 2c 20 20 38 31 38 2c 20 20 39 33 34 2c 20 20  4,  818,  934,  
2cf69 38 31 39 2c 20 20 38 33 32 2c 0a 20 2f 2a 20 20  819,  832,. /*  
2cf6a 20 31 33 30 20 2a 2f 20 20 20 38 33 33 2c 20 20   130 */   833,  
2cf6b 38 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  834,  934,  934,
2cf6c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf6d 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf6e 39 33 34 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a  934,. /*   140 *
2cf6f 2f 20 20 20 39 33 34 2c 20 20 36 32 34 2c 20 20  /   934,  624,  
2cf70 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf71 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf72 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 0a 20  4,  934,  934,. 
2cf73 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 39 33  /*   150 */   93
2cf74 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf75 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf76 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf77 34 2c 20 20 39 33 34 2c 0a 20 2f 2a 20 20 20 31  4,  934,. /*   1
2cf78 36 30 20 2a 2f 20 20 20 39 33 34 2c 20 20 39 33  60 */   934,  93
2cf79 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf7a 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cf7b 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 38 36    934,  934,  86
2cf7c 36 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20  6,. /*   170 */ 
2cf7d 20 20 39 33 34 2c 20 20 39 31 33 2c 20 20 39 31    934,  913,  91
2cf7e 35 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  5,  934,  934,  
2cf7f 39 33 34 2c 20 20 36 31 38 2c 20 20 37 34 31 2c  934,  618,  741,
2cf80 20 20 37 34 31 2c 20 20 37 34 31 2c 0a 20 2f 2a    741,  741,. /*
2cf81 20 20 20 31 38 30 20 2a 2f 20 20 20 39 33 34 2c     180 */   934,
2cf82 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf83 34 2c 20 20 39 33 34 2c 20 20 37 33 35 2c 20 20  4,  934,  735,  
2cf84 37 34 35 2c 20 20 39 32 37 2c 20 20 39 33 34 2c  745,  927,  934,
2cf85 20 20 39 33 34 2c 0a 20 2f 2a 20 20 20 31 39 30    934,. /*   190
2cf86 20 2a 2f 20 20 20 37 30 31 2c 20 20 39 33 34 2c   */   701,  934,
2cf87 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cf88 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf89 39 33 34 2c 20 20 36 32 36 2c 20 20 37 33 33 2c  934,  626,  733,
2cf8a 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20  . /*   200 */   
2cf8b 36 35 39 2c 20 20 37 34 33 2c 20 20 39 33 34 2c  659,  743,  934,
2cf8c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 36 34    934,  934,  64
2cf8d 36 2c 20 20 37 32 32 2c 20 20 38 38 36 2c 20 20  6,  722,  886,  
2cf8e 39 33 34 2c 20 20 39 30 30 2c 0a 20 2f 2a 20 20  934,  900,. /*  
2cf8f 20 32 31 30 20 2a 2f 20 20 20 38 39 38 2c 20 20   210 */   898,  
2cf90 37 32 34 2c 20 20 37 38 36 2c 20 20 39 33 34 2c  724,  786,  934,
2cf91 20 20 37 33 33 2c 20 20 37 34 32 2c 20 20 39 33    733,  742,  93
2cf92 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cf93 38 34 39 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a  849,. /*   220 *
2cf94 2f 20 20 20 37 37 30 2c 20 20 37 37 30 2c 20 20  /   770,  770,  
2cf95 37 35 38 2c 20 20 37 37 30 2c 20 20 36 38 30 2c  758,  770,  680,
2cf96 20 20 37 37 30 2c 20 20 39 33 34 2c 20 20 37 37    770,  934,  77
2cf97 30 2c 20 20 39 33 34 2c 20 20 36 38 33 2c 0a 20  0,  934,  683,. 
2cf98 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 37 38  /*   230 */   78
2cf99 30 2c 20 20 37 35 38 2c 20 20 37 35 38 2c 20 20  0,  758,  758,  
2cf9a 37 38 30 2c 20 20 36 32 33 2c 20 20 36 32 33 2c  780,  623,  623,
2cf9b 20 20 36 32 33 2c 20 20 36 32 33 2c 20 20 36 33    623,  623,  63
2cf9c 34 2c 20 20 36 33 34 2c 0a 20 2f 2a 20 20 20 32  4,  634,. /*   2
2cf9d 34 30 20 2a 2f 20 20 20 37 30 30 2c 20 20 39 33  40 */   700,  93
2cf9e 34 2c 20 20 37 38 30 2c 20 20 37 37 31 2c 20 20  4,  780,  771,  
2cf9f 37 37 33 2c 20 20 37 36 33 2c 20 20 37 37 35 2c  773,  763,  775,
2cfa0 20 20 39 33 34 2c 20 20 37 34 39 2c 20 20 37 34    934,  749,  74
2cfa1 39 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20  9,. /*   250 */ 
2cfa2 20 20 37 35 37 2c 20 20 37 36 32 2c 20 20 37 35    757,  762,  75
2cfa3 37 2c 20 20 37 36 32 2c 20 20 37 35 37 2c 20 20  7,  762,  757,  
2cfa4 37 36 32 2c 20 20 37 31 32 2c 20 20 37 31 32 2c  762,  712,  712,
2cfa5 20 20 36 39 37 2c 20 20 37 31 32 2c 0a 20 2f 2a    697,  712,. /*
2cfa6 20 20 20 32 36 30 20 2a 2f 20 20 20 36 38 33 2c     260 */   683,
2cfa7 20 20 37 31 32 2c 20 20 38 35 39 2c 20 20 38 36    712,  859,  86
2cfa8 33 2c 20 20 38 36 33 2c 20 20 36 39 37 2c 20 20  3,  863,  697,  
2cfa9 37 31 32 2c 20 20 37 31 32 2c 20 20 37 31 32 2c  712,  712,  712,
2cfaa 20 20 38 35 39 2c 0a 20 2f 2a 20 20 20 32 37 30    859,. /*   270
2cfab 20 2a 2f 20 20 20 36 34 31 2c 20 20 37 34 39 2c   */   641,  749,
2cfac 20 20 36 34 31 2c 20 20 37 34 39 2c 20 20 36 34    641,  749,  64
2cfad 31 2c 20 20 37 34 39 2c 20 20 37 34 39 2c 20 20  1,  749,  749,  
2cfae 38 39 30 2c 20 20 38 39 32 2c 20 20 36 34 31 2c  890,  892,  641,
2cfaf 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20  . /*   280 */   
2cfb0 37 34 39 2c 20 20 37 31 34 2c 20 20 37 31 34 2c  749,  714,  714,
2cfb1 20 20 37 39 32 2c 20 20 37 38 30 2c 20 20 37 34    792,  780,  74
2cfb2 39 2c 20 20 37 32 31 2c 20 20 37 32 31 2c 20 20  9,  721,  721,  
2cfb3 37 32 31 2c 20 20 37 32 31 2c 0a 20 2f 2a 20 20  721,  721,. /*  
2cfb4 20 32 39 30 20 2a 2f 20 20 20 37 38 30 2c 20 20   290 */   780,  
2cfb5 37 31 34 2c 20 20 37 39 32 2c 20 20 37 34 39 2c  714,  792,  749,
2cfb6 20 20 39 31 32 2c 20 20 39 31 32 2c 20 20 37 34    912,  912,  74
2cfb7 39 2c 20 20 37 34 39 2c 20 20 39 32 30 2c 20 20  9,  749,  920,  
2cfb8 36 36 37 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a  667,. /*   300 *
2cfb9 2f 20 20 20 36 38 35 2c 20 20 36 38 35 2c 20 20  /   685,  685,  
2cfba 39 32 37 2c 20 20 39 33 32 2c 20 20 39 33 34 2c  927,  932,  934,
2cfbb 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cfbc 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 0a 20  4,  934,  934,. 
2cfbd 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 39 33  /*   310 */   93
2cfbe 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cfbf 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cfc0 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cfc1 34 2c 20 20 39 33 34 2c 0a 20 2f 2a 20 20 20 33  4,  934,. /*   3
2cfc2 32 30 20 2a 2f 20 20 20 39 33 34 2c 20 20 38 37  20 */   934,  87
2cfc3 33 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  3,  934,  934,  
2cfc4 39 33 34 2c 20 20 36 33 32 2c 20 20 39 33 34 2c  934,  632,  934,
2cfc5 20 20 36 35 31 2c 20 20 37 39 39 2c 20 20 38 30    651,  799,  80
2cfc6 34 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20  4,. /*   330 */ 
2cfc7 20 20 38 30 30 2c 20 20 39 33 34 2c 20 20 38 30    800,  934,  80
2cfc8 31 2c 20 20 39 33 34 2c 20 20 37 32 37 2c 20 20  1,  934,  727,  
2cfc9 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cfca 20 20 39 33 34 2c 20 20 39 33 34 2c 0a 20 2f 2a    934,  934,. /*
2cfcb 20 20 20 33 34 30 20 2a 2f 20 20 20 39 33 34 2c     340 */   934,
2cfcc 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cfcd 34 2c 20 20 39 33 34 2c 20 20 38 35 32 2c 20 20  4,  934,  852,  
2cfce 39 33 34 2c 20 20 37 36 34 2c 20 20 39 33 34 2c  934,  764,  934,
2cfcf 20 20 37 37 32 2c 0a 20 2f 2a 20 20 20 33 35 30    772,. /*   350
2cfd0 20 2a 2f 20 20 20 39 33 34 2c 20 20 39 33 34 2c   */   934,  934,
2cfd1 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cfd2 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cfd3 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cfd4 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20  . /*   360 */   
2cfd5 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cfd6 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cfd7 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cfd8 39 33 34 2c 20 20 39 33 34 2c 0a 20 2f 2a 20 20  934,  934,. /*  
2cfd9 20 33 37 30 20 2a 2f 20 20 20 39 33 34 2c 20 20   370 */   934,  
2cfda 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cfdb 20 20 39 33 34 2c 20 20 38 38 38 2c 20 20 38 38    934,  888,  88
2cfdc 39 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  9,  934,  934,  
2cfdd 39 33 34 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a  934,. /*   380 *
2cfde 2f 20 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  /   934,  934,  
2cfdf 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cfe0 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cfe1 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 0a 20  4,  934,  934,. 
2cfe2 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 39 33  /*   390 */   93
2cfe3 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20  4,  934,  934,  
2cfe4 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33 34 2c  934,  934,  934,
2cfe5 20 20 39 33 34 2c 20 20 39 33 34 2c 20 20 39 33    934,  934,  93
2cfe6 34 2c 20 20 39 33 34 2c 0a 20 2f 2a 20 20 20 34  4,  934,. /*   4
2cfe7 30 30 20 2a 2f 20 20 20 39 31 39 2c 20 20 39 33  00 */   919,  93
2cfe8 34 2c 20 20 39 33 34 2c 20 20 39 32 32 2c 20 20  4,  934,  922,  
2cfe9 36 31 39 2c 20 20 39 33 34 2c 20 20 36 31 34 2c  619,  934,  614,
2cfea 20 20 36 31 36 2c 20 20 36 31 37 2c 20 20 36 32    616,  617,  62
2cfeb 31 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20  1,. /*   410 */ 
2cfec 20 20 36 32 32 2c 20 20 36 32 35 2c 20 20 36 35    622,  625,  65
2cfed 31 2c 20 20 36 35 32 2c 20 20 36 35 34 2c 20 20  1,  652,  654,  
2cfee 36 35 35 2c 20 20 36 35 36 2c 20 20 36 32 37 2c  655,  656,  627,
2cfef 20 20 36 32 38 2c 20 20 36 32 39 2c 0a 20 2f 2a    628,  629,. /*
2cff0 20 20 20 34 32 30 20 2a 2f 20 20 20 36 33 30 2c     420 */   630,
2cff1 20 20 36 33 31 2c 20 20 36 33 33 2c 20 20 36 33    631,  633,  63
2cff2 37 2c 20 20 36 33 35 2c 20 20 36 33 36 2c 20 20  7,  635,  636,  
2cff3 36 33 38 2c 20 20 36 34 35 2c 20 20 36 34 37 2c  638,  645,  647,
2cff4 20 20 36 36 36 2c 0a 20 2f 2a 20 20 20 34 33 30    666,. /*   430
2cff5 20 2a 2f 20 20 20 36 36 38 2c 20 20 36 37 30 2c   */   668,  670,
2cff6 20 20 37 33 31 2c 20 20 37 33 32 2c 20 20 37 39    731,  732,  79
2cff7 36 2c 20 20 37 32 35 2c 20 20 37 32 36 2c 20 20  6,  725,  726,  
2cff8 37 33 30 2c 20 20 36 35 33 2c 20 20 38 30 37 2c  730,  653,  807,
2cff9 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20  . /*   440 */   
2cffa 37 39 38 2c 20 20 38 30 32 2c 20 20 38 30 33 2c  798,  802,  803,
2cffb 20 20 38 30 35 2c 20 20 38 30 36 2c 20 20 38 32    805,  806,  82
2cffc 30 2c 20 20 38 32 31 2c 20 20 38 32 33 2c 20 20  0,  821,  823,  
2cffd 38 32 39 2c 20 20 38 33 36 2c 0a 20 2f 2a 20 20  829,  836,. /*  
2cffe 20 34 35 30 20 2a 2f 20 20 20 38 33 39 2c 20 20   450 */   839,  
2cfff 38 32 32 2c 20 20 38 32 37 2c 20 20 38 32 38 2c  822,  827,  828,
2d000 20 20 38 33 30 2c 20 20 38 33 35 2c 20 20 38 33    830,  835,  83
2d001 38 2c 20 20 37 32 38 2c 20 20 37 32 39 2c 20 20  8,  728,  729,  
2d002 38 34 32 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a  842,. /*   460 *
2d003 2f 20 20 20 36 36 30 2c 20 20 36 36 31 2c 20 20  /   660,  661,  
2d004 36 36 34 2c 20 20 36 36 35 2c 20 20 38 37 36 2c  664,  665,  876,
2d005 20 20 38 37 38 2c 20 20 38 37 37 2c 20 20 38 37    878,  877,  87
2d006 39 2c 20 20 36 36 33 2c 20 20 36 36 32 2c 0a 20  9,  663,  662,. 
2d007 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20 38 30  /*   470 */   80
2d008 38 2c 20 20 38 31 31 2c 20 20 38 34 34 2c 20 20  8,  811,  844,  
2d009 38 34 35 2c 20 20 39 30 31 2c 20 20 39 30 32 2c  845,  901,  902,
2d00a 20 20 39 30 33 2c 20 20 39 30 34 2c 20 20 39 30    903,  904,  90
2d00b 35 2c 20 20 38 34 30 2c 0a 20 2f 2a 20 20 20 34  5,  840,. /*   4
2d00c 38 30 20 2a 2f 20 20 20 37 35 30 2c 20 20 38 34  80 */   750,  84
2d00d 33 2c 20 20 38 32 36 2c 20 20 37 36 35 2c 20 20  3,  826,  765,  
2d00e 37 36 38 2c 20 20 37 36 39 2c 20 20 37 36 36 2c  768,  769,  766,
2d00f 20 20 37 33 34 2c 20 20 37 34 34 2c 20 20 37 35    734,  744,  75
2d010 32 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20  2,. /*   490 */ 
2d011 20 20 37 35 33 2c 20 20 37 35 34 2c 20 20 37 35    753,  754,  75
2d012 35 2c 20 20 37 33 39 2c 20 20 37 34 30 2c 20 20  5,  739,  740,  
2d013 37 34 36 2c 20 20 37 36 31 2c 20 20 37 39 34 2c  746,  761,  794,
2d014 20 20 37 39 35 2c 20 20 37 35 39 2c 0a 20 2f 2a    795,  759,. /*
2d015 20 20 20 35 30 30 20 2a 2f 20 20 20 37 36 30 2c     500 */   760,
2d016 20 20 37 34 37 2c 20 20 37 34 38 2c 20 20 37 33    747,  748,  73
2d017 36 2c 20 20 37 33 37 2c 20 20 37 33 38 2c 20 20  6,  737,  738,  
2d018 38 34 31 2c 20 20 37 39 37 2c 20 20 38 30 39 2c  841,  797,  809,
2d019 20 20 38 31 30 2c 0a 20 2f 2a 20 20 20 35 31 30    810,. /*   510
2d01a 20 2a 2f 20 20 20 36 37 31 2c 20 20 36 37 32 2c   */   671,  672,
2d01b 20 20 38 30 34 2c 20 20 36 37 33 2c 20 20 36 37    804,  673,  67
2d01c 34 2c 20 20 36 37 35 2c 20 20 37 31 33 2c 20 20  4,  675,  713,  
2d01d 37 31 36 2c 20 20 37 31 37 2c 20 20 37 31 38 2c  716,  717,  718,
2d01e 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20  . /*   520 */   
2d01f 36 37 36 2c 20 20 36 39 35 2c 20 20 36 39 38 2c  676,  695,  698,
2d020 20 20 36 39 39 2c 20 20 36 37 37 2c 20 20 36 38    699,  677,  68
2d021 34 2c 20 20 36 37 38 2c 20 20 36 37 39 2c 20 20  4,  678,  679,  
2d022 36 38 36 2c 20 20 36 38 37 2c 0a 20 2f 2a 20 20  686,  687,. /*  
2d023 20 35 33 30 20 2a 2f 20 20 20 36 38 38 2c 20 20   530 */   688,  
2d024 36 39 31 2c 20 20 36 39 32 2c 20 20 36 39 33 2c  691,  692,  693,
2d025 20 20 36 39 34 2c 20 20 36 38 39 2c 20 20 36 39    694,  689,  69
2d026 30 2c 20 20 38 36 30 2c 20 20 38 36 31 2c 20 20  0,  860,  861,  
2d027 38 36 34 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a  864,. /*   540 *
2d028 2f 20 20 20 38 36 32 2c 20 20 36 38 31 2c 20 20  /   862,  681,  
2d029 36 38 32 2c 20 20 36 39 36 2c 20 20 36 36 39 2c  682,  696,  669,
2d02a 20 20 36 35 38 2c 20 20 36 35 30 2c 20 20 37 30    658,  650,  70
2d02b 32 2c 20 20 37 30 35 2c 20 20 37 30 36 2c 0a 20  2,  705,  706,. 
2d02c 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20 37 30  /*   550 */   70
2d02d 37 2c 20 20 37 30 38 2c 20 20 37 30 39 2c 20 20  7,  708,  709,  
2d02e 37 31 31 2c 20 20 37 30 33 2c 20 20 37 30 34 2c  711,  703,  704,
2d02f 20 20 36 34 38 2c 20 20 36 33 39 2c 20 20 36 34    648,  639,  64
2d030 32 2c 20 20 37 35 31 2c 0a 20 2f 2a 20 20 20 35  2,  751,. /*   5
2d031 36 30 20 2a 2f 20 20 20 38 38 32 2c 20 20 38 39  60 */   882,  89
2d032 31 2c 20 20 38 38 37 2c 20 20 38 38 33 2c 20 20  1,  887,  883,  
2d033 38 38 34 2c 20 20 38 38 35 2c 20 20 36 34 33 2c  884,  885,  643,
2d034 20 20 38 35 36 2c 20 20 38 35 37 2c 20 20 37 31    856,  857,  71
2d035 35 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20  5,. /*   570 */ 
2d036 20 20 37 38 38 2c 20 20 37 38 39 2c 20 20 38 38    788,  789,  88
2d037 31 2c 20 20 38 39 34 2c 20 20 38 39 36 2c 20 20  1,  894,  896,  
2d038 37 39 33 2c 20 20 38 39 37 2c 20 20 38 39 39 2c  793,  897,  899,
2d039 20 20 38 39 35 2c 20 20 39 32 34 2c 0a 20 2f 2a    895,  924,. /*
2d03a 20 20 20 35 38 30 20 2a 2f 20 20 20 36 34 30 2c     580 */   640,
2d03b 20 20 37 31 39 2c 20 20 37 32 30 2c 20 20 37 32    719,  720,  72
2d03c 33 2c 20 20 38 36 35 2c 20 20 39 30 36 2c 20 20  3,  865,  906,  
2d03d 37 37 39 2c 20 20 37 38 32 2c 20 20 37 38 35 2c  779,  782,  785,
2d03e 20 20 37 38 37 2c 0a 20 2f 2a 20 20 20 35 39 30    787,. /*   590
2d03f 20 2a 2f 20 20 20 38 36 37 2c 20 20 38 36 38 2c   */   867,  868,
2d040 20 20 38 36 39 2c 20 20 38 37 30 2c 20 20 38 37    869,  870,  87
2d041 31 2c 20 20 38 37 34 2c 20 20 38 37 35 2c 20 20  1,  874,  875,  
2d042 38 37 32 2c 20 20 39 30 37 2c 20 20 39 31 31 2c  872,  907,  911,
2d043 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20  . /*   600 */   
2d044 39 31 34 2c 20 20 39 31 36 2c 20 20 39 31 37 2c  914,  916,  917,
2d045 20 20 39 31 38 2c 20 20 39 32 31 2c 20 20 39 32    918,  921,  92
2d046 33 2c 20 20 39 32 38 2c 20 20 39 32 39 2c 20 20  3,  928,  929,  
2d047 39 33 30 2c 20 20 39 33 33 2c 0a 20 2f 2a 20 20  930,  933,. /*  
2d048 20 36 31 30 20 2a 2f 20 20 20 39 33 31 2c 20 20   610 */   931,  
2d049 36 32 30 2c 20 20 36 31 35 2c 0a 7d 3b 0a 23 64  620,  615,.};.#d
2d04a 65 66 69 6e 65 20 59 59 5f 53 5a 5f 41 43 54 54  efine YY_SZ_ACTT
2d04b 41 42 20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28  AB (int)(sizeof(
2d04c 79 79 5f 61 63 74 69 6f 6e 29 2f 73 69 7a 65 6f  yy_action)/sizeo
2d04d 66 28 79 79 5f 61 63 74 69 6f 6e 5b 30 5d 29 29  f(yy_action[0]))
2d04e 0a 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 61  ../* The next ta
2d04f 62 6c 65 20 6d 61 70 73 20 74 6f 6b 65 6e 73 20  ble maps tokens 
2d050 69 6e 74 6f 20 66 61 6c 6c 62 61 63 6b 20 74 6f  into fallback to
2d051 6b 65 6e 73 2e 20 20 49 66 20 61 20 63 6f 6e 73  kens.  If a cons
2d052 74 72 75 63 74 0a 2a 2a 20 6c 69 6b 65 20 74 68  truct.** like th
2d053 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 20  e following:.** 
2d054 0a 2a 2a 20 20 20 20 20 20 25 66 61 6c 6c 62 61  .**      %fallba
2d055 63 6b 20 49 44 20 58 20 59 20 5a 2e 0a 2a 2a 0a  ck ID X Y Z..**.
2d056 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68  ** appears in th
2d057 65 20 67 72 61 6d 6d 61 72 2c 20 74 68 65 6e 20  e grammar, then 
2d058 49 44 20 62 65 63 6f 6d 65 73 20 61 20 66 61 6c  ID becomes a fal
2d059 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 66 6f 72 20  lback token for 
2d05a 58 2c 20 59 2c 0a 2a 2a 20 61 6e 64 20 5a 2e 20  X, Y,.** and Z. 
2d05b 20 57 68 65 6e 65 76 65 72 20 6f 6e 65 20 6f 66   Whenever one of
2d05c 20 74 68 65 20 74 6f 6b 65 6e 73 20 58 2c 20 59   the tokens X, Y
2d05d 2c 20 6f 72 20 5a 20 69 73 20 69 6e 70 75 74 20  , or Z is input 
2d05e 74 6f 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  to the parser.**
2d05f 20 62 75 74 20 69 74 20 64 6f 65 73 20 6e 6f 74   but it does not
2d060 20 70 61 72 73 65 2c 20 74 68 65 20 74 79 70 65   parse, the type
2d061 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 73   of the token is
2d062 20 63 68 61 6e 67 65 64 20 74 6f 20 49 44 20 61   changed to ID a
2d063 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20  nd.** the parse 
2d064 69 73 20 72 65 74 72 69 65 64 20 62 65 66 6f 72  is retried befor
2d065 65 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 68  e an error is th
2d066 72 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rown..*/.#ifdef 
2d067 59 59 46 41 4c 4c 42 41 43 4b 0a 73 74 61 74 69  YYFALLBACK.stati
2d068 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54 59  c const YYCODETY
2d069 50 45 20 79 79 46 61 6c 6c 62 61 63 6b 5b 5d 20  PE yyFallback[] 
2d06a 3d 20 7b 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20  = {.    0,  /*  
2d06b 20 20 20 20 20 20 20 20 24 20 3d 3e 20 6e 6f 74          $ => not
2d06c 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2d06d 2f 2a 20 20 20 20 20 20 20 53 45 4d 49 20 3d 3e  /*       SEMI =>
2d06e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
2d06f 36 2c 20 20 2f 2a 20 20 20 20 45 58 50 4c 41 49  6,  /*    EXPLAI
2d070 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  N => ID */.   26
2d071 2c 20 20 2f 2a 20 20 20 20 20 20 51 55 45 52 59  ,  /*      QUERY
2d072 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2d073 20 20 2f 2a 20 20 20 20 20 20 20 50 4c 41 4e 20    /*       PLAN 
2d074 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2d075 20 2f 2a 20 20 20 20 20 20 42 45 47 49 4e 20 3d   /*      BEGIN =
2d076 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
2d077 2f 2a 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 3d  /* TRANSACTION =
2d078 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
2d079 32 36 2c 20 20 2f 2a 20 20 20 44 45 46 45 52 52  26,  /*   DEFERR
2d07a 45 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ED => ID */.   2
2d07b 36 2c 20 20 2f 2a 20 20 49 4d 4d 45 44 49 41 54  6,  /*  IMMEDIAT
2d07c 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  E => ID */.   26
2d07d 2c 20 20 2f 2a 20 20 45 58 43 4c 55 53 49 56 45  ,  /*  EXCLUSIVE
2d07e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c   => ID */.    0,
2d07f 20 20 2f 2a 20 20 20 20 20 43 4f 4d 4d 49 54 20    /*     COMMIT 
2d080 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2d081 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
2d082 45 4e 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  END => ID */.   
2d083 32 36 2c 20 20 2f 2a 20 20 20 52 4f 4c 4c 42 41  26,  /*   ROLLBA
2d084 43 4b 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  CK => ID */.   2
2d085 36 2c 20 20 2f 2a 20 20 53 41 56 45 50 4f 49 4e  6,  /*  SAVEPOIN
2d086 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  T => ID */.   26
2d087 2c 20 20 2f 2a 20 20 20 20 52 45 4c 45 41 53 45  ,  /*    RELEASE
2d088 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c   => ID */.    0,
2d089 20 20 2f 2a 20 20 20 20 20 20 20 20 20 54 4f 20    /*         TO 
2d08a 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2d08b 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 54 41    0,  /*      TA
2d08c 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  BLE => nothing *
2d08d 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2d08e 20 43 52 45 41 54 45 20 3d 3e 20 6e 6f 74 68 69   CREATE => nothi
2d08f 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ng */.   26,  /*
2d090 20 20 20 20 20 20 20 20 20 49 46 20 3d 3e 20 49           IF => I
2d091 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  D */.    0,  /* 
2d092 20 20 20 20 20 20 20 4e 4f 54 20 3d 3e 20 6e 6f         NOT => no
2d093 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
2d094 20 2f 2a 20 20 20 20 20 45 58 49 53 54 53 20 3d   /*     EXISTS =
2d095 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
2d096 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 54 45  26,  /*       TE
2d097 4d 50 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20  MP => ID */.    
2d098 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c  0,  /*         L
2d099 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  P => nothing */.
2d09a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2d09b 20 20 20 52 50 20 3d 3e 20 6e 6f 74 68 69 6e 67     RP => nothing
2d09c 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2d09d 20 20 20 20 20 20 20 41 53 20 3d 3e 20 6e 6f 74         AS => not
2d09e 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2d09f 2f 2a 20 20 20 20 20 20 43 4f 4d 4d 41 20 3d 3e  /*      COMMA =>
2d0a0 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
2d0a1 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49  0,  /*         I
2d0a2 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  D => nothing */.
2d0a3 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 49 4e      0,  /*    IN
2d0a4 44 45 58 45 44 20 3d 3e 20 6e 6f 74 68 69 6e 67  DEXED => nothing
2d0a5 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2d0a6 20 20 20 20 41 42 4f 52 54 20 3d 3e 20 49 44 20      ABORT => ID 
2d0a7 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2d0a8 20 20 20 41 46 54 45 52 20 3d 3e 20 49 44 20 2a     AFTER => ID *
2d0a9 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2d0aa 41 4e 41 4c 59 5a 45 20 3d 3e 20 49 44 20 2a 2f  ANALYZE => ID */
2d0ab 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2d0ac 20 20 20 41 53 43 20 3d 3e 20 49 44 20 2a 2f 0a     ASC => ID */.
2d0ad 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 41     26,  /*     A
2d0ae 54 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20  TTACH => ID */. 
2d0af 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 42 45    26,  /*     BE
2d0b0 46 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  FORE => ID */.  
2d0b1 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
2d0b2 20 42 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20   BY => ID */.   
2d0b3 32 36 2c 20 20 2f 2a 20 20 20 20 43 41 53 43 41  26,  /*    CASCA
2d0b4 44 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  DE => ID */.   2
2d0b5 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 43 41 53  6,  /*       CAS
2d0b6 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  T => ID */.   26
2d0b7 2c 20 20 2f 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57  ,  /*   COLUMNKW
2d0b8 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2d0b9 20 20 2f 2a 20 20 20 43 4f 4e 46 4c 49 43 54 20    /*   CONFLICT 
2d0ba 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2d0bb 20 2f 2a 20 20 20 44 41 54 41 42 41 53 45 20 3d   /*   DATABASE =
2d0bc 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2d0bd 2f 2a 20 20 20 20 20 20 20 44 45 53 43 20 3d 3e  /*       DESC =>
2d0be 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2d0bf 2a 20 20 20 20 20 44 45 54 41 43 48 20 3d 3e 20  *     DETACH => 
2d0c0 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2d0c1 20 20 20 20 20 20 20 45 41 43 48 20 3d 3e 20 49         EACH => I
2d0c2 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2d0c3 20 20 20 20 20 20 46 41 49 4c 20 3d 3e 20 49 44        FAIL => ID
2d0c4 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2d0c5 20 20 20 20 20 20 46 4f 52 20 3d 3e 20 49 44 20        FOR => ID 
2d0c6 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2d0c7 20 20 49 47 4e 4f 52 45 20 3d 3e 20 49 44 20 2a    IGNORE => ID *
2d0c8 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 49 4e  /.   26,  /*  IN
2d0c9 49 54 49 41 4c 4c 59 20 3d 3e 20 49 44 20 2a 2f  ITIALLY => ID */
2d0ca 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 49  .   26,  /*    I
2d0cb 4e 53 54 45 41 44 20 3d 3e 20 49 44 20 2a 2f 0a  NSTEAD => ID */.
2d0cc 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 4c 49     26,  /*    LI
2d0cd 4b 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 20  KE_KW => ID */. 
2d0ce 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 4d    26,  /*      M
2d0cf 41 54 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  ATCH => ID */.  
2d0d0 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
2d0d1 4b 45 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  KEY => ID */.   
2d0d2 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20  26,  /*         
2d0d3 4f 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  OF => ID */.   2
2d0d4 36 2c 20 20 2f 2a 20 20 20 20 20 4f 46 46 53 45  6,  /*     OFFSE
2d0d5 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  T => ID */.   26
2d0d6 2c 20 20 2f 2a 20 20 20 20 20 50 52 41 47 4d 41  ,  /*     PRAGMA
2d0d7 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2d0d8 20 20 2f 2a 20 20 20 20 20 20 52 41 49 53 45 20    /*      RAISE 
2d0d9 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2d0da 20 2f 2a 20 20 20 20 52 45 50 4c 41 43 45 20 3d   /*    REPLACE =
2d0db 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2d0dc 2f 2a 20 20 20 52 45 53 54 52 49 43 54 20 3d 3e  /*   RESTRICT =>
2d0dd 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2d0de 2a 20 20 20 20 20 20 20 20 52 4f 57 20 3d 3e 20  *        ROW => 
2d0df 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2d0e0 20 20 20 20 54 52 49 47 47 45 52 20 3d 3e 20 49      TRIGGER => I
2d0e1 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2d0e2 20 20 20 20 56 41 43 55 55 4d 20 3d 3e 20 49 44      VACUUM => ID
2d0e3 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2d0e4 20 20 20 20 20 56 49 45 57 20 3d 3e 20 49 44 20       VIEW => ID 
2d0e5 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2d0e6 20 56 49 52 54 55 41 4c 20 3d 3e 20 49 44 20 2a   VIRTUAL => ID *
2d0e7 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2d0e8 52 45 49 4e 44 45 58 20 3d 3e 20 49 44 20 2a 2f  REINDEX => ID */
2d0e9 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2d0ea 52 45 4e 41 4d 45 20 3d 3e 20 49 44 20 2a 2f 0a  RENAME => ID */.
2d0eb 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43 54 49     26,  /*   CTI
2d0ec 4d 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 20  ME_KW => ID */. 
2d0ed 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2d0ee 20 41 4e 59 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   ANY => nothing 
2d0ef 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d0f0 20 20 20 20 20 20 4f 52 20 3d 3e 20 6e 6f 74 68        OR => noth
2d0f1 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d0f2 2a 20 20 20 20 20 20 20 20 41 4e 44 20 3d 3e 20  *        AND => 
2d0f3 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d0f4 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49 53  ,  /*         IS
2d0f5 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d0f6 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 42 45 54     0,  /*    BET
2d0f7 57 45 45 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  WEEN => nothing 
2d0f8 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d0f9 20 20 20 20 20 20 49 4e 20 3d 3e 20 6e 6f 74 68        IN => noth
2d0fa 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d0fb 2a 20 20 20 20 20 49 53 4e 55 4c 4c 20 3d 3e 20  *     ISNULL => 
2d0fc 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d0fd 2c 20 20 2f 2a 20 20 20 20 4e 4f 54 4e 55 4c 4c  ,  /*    NOTNULL
2d0fe 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d0ff 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2d100 20 20 4e 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    NE => nothing 
2d101 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d102 20 20 20 20 20 20 45 51 20 3d 3e 20 6e 6f 74 68        EQ => noth
2d103 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d104 2a 20 20 20 20 20 20 20 20 20 47 54 20 3d 3e 20  *         GT => 
2d105 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d106 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c 45  ,  /*         LE
2d107 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d108 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2d109 20 20 4c 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    LT => nothing 
2d10a 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d10b 20 20 20 20 20 20 47 45 20 3d 3e 20 6e 6f 74 68        GE => noth
2d10c 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d10d 2a 20 20 20 20 20 45 53 43 41 50 45 20 3d 3e 20  *     ESCAPE => 
2d10e 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d10f 2c 20 20 2f 2a 20 20 20 20 20 42 49 54 41 4e 44  ,  /*     BITAND
2d110 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d111 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 42     0,  /*      B
2d112 49 54 4f 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ITOR => nothing 
2d113 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d114 20 20 4c 53 48 49 46 54 20 3d 3e 20 6e 6f 74 68    LSHIFT => noth
2d115 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d116 2a 20 20 20 20 20 52 53 48 49 46 54 20 3d 3e 20  *     RSHIFT => 
2d117 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d118 2c 20 20 2f 2a 20 20 20 20 20 20 20 50 4c 55 53  ,  /*       PLUS
2d119 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d11a 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 4d     0,  /*      M
2d11b 49 4e 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  INUS => nothing 
2d11c 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d11d 20 20 20 20 53 54 41 52 20 3d 3e 20 6e 6f 74 68      STAR => noth
2d11e 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d11f 2a 20 20 20 20 20 20 53 4c 41 53 48 20 3d 3e 20  *      SLASH => 
2d120 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d121 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 52 45 4d  ,  /*        REM
2d122 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d123 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 43 4f     0,  /*     CO
2d124 4e 43 41 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NCAT => nothing 
2d125 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d126 20 43 4f 4c 4c 41 54 45 20 3d 3e 20 6e 6f 74 68   COLLATE => noth
2d127 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d128 2a 20 20 20 20 20 55 4d 49 4e 55 53 20 3d 3e 20  *     UMINUS => 
2d129 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d12a 2c 20 20 2f 2a 20 20 20 20 20 20 55 50 4c 55 53  ,  /*      UPLUS
2d12b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d12c 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 42 49     0,  /*     BI
2d12d 54 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  TNOT => nothing 
2d12e 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d12f 20 20 53 54 52 49 4e 47 20 3d 3e 20 6e 6f 74 68    STRING => noth
2d130 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d131 2a 20 20 20 20 4a 4f 49 4e 5f 4b 57 20 3d 3e 20  *    JOIN_KW => 
2d132 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d133 2c 20 20 2f 2a 20 43 4f 4e 53 54 52 41 49 4e 54  ,  /* CONSTRAINT
2d134 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d135 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 44 45 46     0,  /*    DEF
2d136 41 55 4c 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  AULT => nothing 
2d137 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d138 20 20 20 20 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68      NULL => noth
2d139 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d13a 2a 20 20 20 20 50 52 49 4d 41 52 59 20 3d 3e 20  *    PRIMARY => 
2d13b 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d13c 2c 20 20 2f 2a 20 20 20 20 20 55 4e 49 51 55 45  ,  /*     UNIQUE
2d13d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d13e 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 43     0,  /*      C
2d13f 48 45 43 4b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  HECK => nothing 
2d140 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 52 45  */.    0,  /* RE
2d141 46 45 52 45 4e 43 45 53 20 3d 3e 20 6e 6f 74 68  FERENCES => noth
2d142 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d143 2a 20 20 20 41 55 54 4f 49 4e 43 52 20 3d 3e 20  *   AUTOINCR => 
2d144 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d145 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4f 4e  ,  /*         ON
2d146 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d147 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 44 45     0,  /*     DE
2d148 4c 45 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LETE => nothing 
2d149 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d14a 20 20 55 50 44 41 54 45 20 3d 3e 20 6e 6f 74 68    UPDATE => noth
2d14b 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d14c 2a 20 20 20 20 20 49 4e 53 45 52 54 20 3d 3e 20  *     INSERT => 
2d14d 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d14e 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 53 45 54  ,  /*        SET
2d14f 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d150 20 20 20 30 2c 20 20 2f 2a 20 44 45 46 45 52 52     0,  /* DEFERR
2d151 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ABLE => nothing 
2d152 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d153 20 46 4f 52 45 49 47 4e 20 3d 3e 20 6e 6f 74 68   FOREIGN => noth
2d154 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d155 2a 20 20 20 20 20 20 20 44 52 4f 50 20 3d 3e 20  *       DROP => 
2d156 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d157 2c 20 20 2f 2a 20 20 20 20 20 20 55 4e 49 4f 4e  ,  /*      UNION
2d158 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d159 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2d15a 20 41 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   ALL => nothing 
2d15b 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d15c 20 20 45 58 43 45 50 54 20 3d 3e 20 6e 6f 74 68    EXCEPT => noth
2d15d 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d15e 2a 20 20 49 4e 54 45 52 53 45 43 54 20 3d 3e 20  *  INTERSECT => 
2d15f 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d160 2c 20 20 2f 2a 20 20 20 20 20 53 45 4c 45 43 54  ,  /*     SELECT
2d161 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d162 20 20 20 30 2c 20 20 2f 2a 20 20 20 44 49 53 54     0,  /*   DIST
2d163 49 4e 43 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  INCT => nothing 
2d164 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d165 20 20 20 20 20 44 4f 54 20 3d 3e 20 6e 6f 74 68       DOT => noth
2d166 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d167 2a 20 20 20 20 20 20 20 46 52 4f 4d 20 3d 3e 20  *       FROM => 
2d168 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d169 2c 20 20 2f 2a 20 20 20 20 20 20 20 4a 4f 49 4e  ,  /*       JOIN
2d16a 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d16b 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 55     0,  /*      U
2d16c 53 49 4e 47 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  SING => nothing 
2d16d 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d16e 20 20 20 4f 52 44 45 52 20 3d 3e 20 6e 6f 74 68     ORDER => noth
2d16f 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d170 2a 20 20 20 20 20 20 47 52 4f 55 50 20 3d 3e 20  *      GROUP => 
2d171 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d172 2c 20 20 2f 2a 20 20 20 20 20 48 41 56 49 4e 47  ,  /*     HAVING
2d173 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d174 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 4c     0,  /*      L
2d175 49 4d 49 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  IMIT => nothing 
2d176 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d177 20 20 20 57 48 45 52 45 20 3d 3e 20 6e 6f 74 68     WHERE => noth
2d178 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d179 2a 20 20 20 20 20 20 20 49 4e 54 4f 20 3d 3e 20  *       INTO => 
2d17a 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d17b 2c 20 20 2f 2a 20 20 20 20 20 56 41 4c 55 45 53  ,  /*     VALUES
2d17c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d17d 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 49 4e 54     0,  /*    INT
2d17e 45 47 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  EGER => nothing 
2d17f 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d180 20 20 20 46 4c 4f 41 54 20 3d 3e 20 6e 6f 74 68     FLOAT => noth
2d181 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d182 2a 20 20 20 20 20 20 20 42 4c 4f 42 20 3d 3e 20  *       BLOB => 
2d183 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d184 2c 20 20 2f 2a 20 20 20 52 45 47 49 53 54 45 52  ,  /*   REGISTER
2d185 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d186 20 20 20 30 2c 20 20 2f 2a 20 20 20 56 41 52 49     0,  /*   VARI
2d187 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ABLE => nothing 
2d188 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d189 20 20 20 20 43 41 53 45 20 3d 3e 20 6e 6f 74 68      CASE => noth
2d18a 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d18b 2a 20 20 20 20 20 20 20 57 48 45 4e 20 3d 3e 20  *       WHEN => 
2d18c 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d18d 2c 20 20 2f 2a 20 20 20 20 20 20 20 54 48 45 4e  ,  /*       THEN
2d18e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2d18f 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2d190 45 4c 53 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ELSE => nothing 
2d191 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2d192 20 20 20 49 4e 44 45 58 20 3d 3e 20 6e 6f 74 68     INDEX => noth
2d193 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2d194 2a 20 20 20 20 20 20 41 4c 54 45 52 20 3d 3e 20  *      ALTER => 
2d195 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2d196 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 44 44  ,  /*        ADD
2d197 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 7d   => nothing */.}
2d198 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 46 41  ;.#endif /* YYFA
2d199 4c 4c 42 41 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68  LLBACK */../* Th
2d19a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
2d19b 63 74 75 72 65 20 72 65 70 72 65 73 65 6e 74 73  cture represents
2d19c 20 61 20 73 69 6e 67 6c 65 20 65 6c 65 6d 65 6e   a single elemen
2d19d 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 72 73  t of the.** pars
2d19e 65 72 27 73 20 73 74 61 63 6b 2e 20 20 49 6e 66  er's stack.  Inf
2d19f 6f 72 6d 61 74 69 6f 6e 20 73 74 6f 72 65 64 20  ormation stored 
2d1a0 69 6e 63 6c 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20  includes:.**.** 
2d1a1 20 20 2b 20 20 54 68 65 20 73 74 61 74 65 20 6e    +  The state n
2d1a2 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70 61  umber for the pa
2d1a3 72 73 65 72 20 61 74 20 74 68 69 73 20 6c 65 76  rser at this lev
2d1a4 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  el of the stack.
2d1a5 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20  .**.**   +  The 
2d1a6 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b  value of the tok
2d1a7 65 6e 20 73 74 6f 72 65 64 20 61 74 20 74 68 69  en stored at thi
2d1a8 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 73  s level of the s
2d1a9 74 61 63 6b 2e 0a 2a 2a 20 20 20 20 20 20 28 49  tack..**      (I
2d1aa 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74  n other words, t
2d1ab 68 65 20 22 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e  he "major" token
2d1ac 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68  .).**.**   +  Th
2d1ad 65 20 73 65 6d 61 6e 74 69 63 20 76 61 6c 75 65  e semantic value
2d1ae 20 73 74 6f 72 65 64 20 61 74 20 74 68 69 73 20   stored at this 
2d1af 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61  level of the sta
2d1b0 63 6b 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20  ck.  This is.** 
2d1b1 20 20 20 20 20 74 68 65 20 69 6e 66 6f 72 6d 61       the informa
2d1b2 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65  tion used by the
2d1b3 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73   action routines
2d1b4 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e   in the grammar.
2d1b5 0a 2a 2a 20 20 20 20 20 20 49 74 20 69 73 20 73  .**      It is s
2d1b6 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20  ometimes called 
2d1b7 74 68 65 20 22 6d 69 6e 6f 72 22 20 74 6f 6b 65  the "minor" toke
2d1b8 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 79 79 53  n..*/.struct yyS
2d1b9 74 61 63 6b 45 6e 74 72 79 20 7b 0a 20 20 59 59  tackEntry {.  YY
2d1ba 41 43 54 49 4f 4e 54 59 50 45 20 73 74 61 74 65  ACTIONTYPE state
2d1bb 6e 6f 3b 20 20 2f 2a 20 54 68 65 20 73 74 61 74  no;  /* The stat
2d1bc 65 2d 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59  e-number */.  YY
2d1bd 43 4f 44 45 54 59 50 45 20 6d 61 6a 6f 72 3b 20  CODETYPE major; 
2d1be 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f       /* The majo
2d1bf 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 20  r token value.  
2d1c0 54 68 69 73 20 69 73 20 74 68 65 20 63 6f 64 65  This is the code
2d1c1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d1c2 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d            ** num
2d1c3 62 65 72 20 66 6f 72 20 74 68 65 20 74 6f 6b 65  ber for the toke
2d1c4 6e 20 61 74 20 74 68 69 73 20 73 74 61 63 6b 20  n at this stack 
2d1c5 6c 65 76 65 6c 20 2a 2f 0a 20 20 59 59 4d 49 4e  level */.  YYMIN
2d1c6 4f 52 54 59 50 45 20 6d 69 6e 6f 72 3b 20 20 20  ORTYPE minor;   
2d1c7 20 20 2f 2a 20 54 68 65 20 75 73 65 72 2d 73 75    /* The user-su
2d1c8 70 70 6c 69 65 64 20 6d 69 6e 6f 72 20 74 6f 6b  pplied minor tok
2d1c9 65 6e 20 76 61 6c 75 65 2e 20 20 54 68 69 73 0a  en value.  This.
2d1ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d1cb 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 74           ** is t
2d1cc 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
2d1cd 74 6f 6b 65 6e 20 20 2a 2f 0a 7d 3b 0a 74 79 70  token  */.};.typ
2d1ce 65 64 65 66 20 73 74 72 75 63 74 20 79 79 53 74  edef struct yySt
2d1cf 61 63 6b 45 6e 74 72 79 20 79 79 53 74 61 63 6b  ackEntry yyStack
2d1d0 45 6e 74 72 79 3b 0a 0a 2f 2a 20 54 68 65 20 73  Entry;../* The s
2d1d1 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 72 73  tate of the pars
2d1d2 65 72 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79  er is completely
2d1d3 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 61 6e   contained in an
2d1d4 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20   instance of.** 
2d1d5 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2d1d6 72 75 63 74 75 72 65 20 2a 2f 0a 73 74 72 75 63  ructure */.struc
2d1d7 74 20 79 79 50 61 72 73 65 72 20 7b 0a 20 20 69  t yyParser {.  i
2d1d8 6e 74 20 79 79 69 64 78 3b 20 20 20 20 20 20 20  nt yyidx;       
2d1d9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d1da 49 6e 64 65 78 20 6f 66 20 74 6f 70 20 65 6c 65  Index of top ele
2d1db 6d 65 6e 74 20 69 6e 20 73 74 61 63 6b 20 2a 2f  ment in stack */
2d1dc 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d  .#ifdef YYTRACKM
2d1dd 41 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 69  AXSTACKDEPTH.  i
2d1de 6e 74 20 79 79 69 64 78 4d 61 78 3b 20 20 20 20  nt yyidxMax;    
2d1df 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d1e0 4d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  Maximum value of
2d1e1 20 79 79 69 64 78 20 2a 2f 0a 23 65 6e 64 69 66   yyidx */.#endif
2d1e2 0a 20 20 69 6e 74 20 79 79 65 72 72 63 6e 74 3b  .  int yyerrcnt;
2d1e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d1e4 20 2f 2a 20 53 68 69 66 74 73 20 6c 65 66 74 20   /* Shifts left 
2d1e5 62 65 66 6f 72 65 20 6f 75 74 20 6f 66 20 74 68  before out of th
2d1e6 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c  e error */.  sql
2d1e7 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 44  ite3ParserARG_SD
2d1e8 45 43 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  ECL             
2d1e9 20 20 20 2f 2a 20 41 20 70 6c 61 63 65 20 74 6f     /* A place to
2d1ea 20 68 6f 6c 64 20 25 65 78 74 72 61 5f 61 72 67   hold %extra_arg
2d1eb 75 6d 65 6e 74 20 2a 2f 0a 23 69 66 20 59 59 53  ument */.#if YYS
2d1ec 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 69  TACKDEPTH<=0.  i
2d1ed 6e 74 20 79 79 73 74 6b 73 7a 3b 20 20 20 20 20  nt yystksz;     
2d1ee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d1ef 43 75 72 72 65 6e 74 20 73 69 64 65 20 6f 66 20  Current side of 
2d1f0 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 79  the stack */.  y
2d1f1 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 73  yStackEntry *yys
2d1f2 74 61 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  tack;        /* 
2d1f3 54 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61  The parser's sta
2d1f4 63 6b 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 79 79  ck */.#else.  yy
2d1f5 53 74 61 63 6b 45 6e 74 72 79 20 79 79 73 74 61  StackEntry yysta
2d1f6 63 6b 5b 59 59 53 54 41 43 4b 44 45 50 54 48 5d  ck[YYSTACKDEPTH]
2d1f7 3b 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72  ;  /* The parser
2d1f8 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6e 64  's stack */.#end
2d1f9 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  if.};.typedef st
2d1fa 72 75 63 74 20 79 79 50 61 72 73 65 72 20 79 79  ruct yyParser yy
2d1fb 50 61 72 73 65 72 3b 0a 0a 23 69 66 6e 64 65 66  Parser;..#ifndef
2d1fc 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 46   NDEBUG.static F
2d1fd 49 4c 45 20 2a 79 79 54 72 61 63 65 46 49 4c 45  ILE *yyTraceFILE
2d1fe 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 63 68 61   = 0;.static cha
2d1ff 72 20 2a 79 79 54 72 61 63 65 50 72 6f 6d 70 74  r *yyTracePrompt
2d200 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   = 0;.#endif /* 
2d201 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64  NDEBUG */..#ifnd
2d202 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 0a 2a 2a  ef NDEBUG./* .**
2d203 20 54 75 72 6e 20 70 61 72 73 65 72 20 74 72 61   Turn parser tra
2d204 63 69 6e 67 20 6f 6e 20 62 79 20 67 69 76 69 6e  cing on by givin
2d205 67 20 61 20 73 74 72 65 61 6d 20 74 6f 20 77 68  g a stream to wh
2d206 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68 65  ich to write the
2d207 20 74 72 61 63 65 0a 2a 2a 20 61 6e 64 20 61 20   trace.** and a 
2d208 70 72 6f 6d 70 74 20 74 6f 20 70 72 65 66 61 63  prompt to prefac
2d209 65 20 65 61 63 68 20 74 72 61 63 65 20 6d 65 73  e each trace mes
2d20a 73 61 67 65 2e 20 20 54 72 61 63 69 6e 67 20 69  sage.  Tracing i
2d20b 73 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2a 20  s turned off.** 
2d20c 62 79 20 6d 61 6b 69 6e 67 20 65 69 74 68 65 72  by making either
2d20d 20 61 72 67 75 6d 65 6e 74 20 4e 55 4c 4c 20 0a   argument NULL .
2d20e 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a  **.** Inputs:.**
2d20f 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20   <ul>.** <li> A 
2d210 46 49 4c 45 2a 20 74 6f 20 77 68 69 63 68 20 74  FILE* to which t
2d211 72 61 63 65 20 6f 75 74 70 75 74 20 73 68 6f 75  race output shou
2d212 6c 64 20 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a  ld be written..*
2d213 2a 20 20 20 20 20 20 49 66 20 4e 55 4c 4c 2c 20  *      If NULL, 
2d214 74 68 65 6e 20 74 72 61 63 69 6e 67 20 69 73 20  then tracing is 
2d215 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c  turned off..** <
2d216 6c 69 3e 20 41 20 70 72 65 66 69 78 20 73 74 72  li> A prefix str
2d217 69 6e 67 20 77 72 69 74 74 65 6e 20 61 74 20 74  ing written at t
2d218 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
2d219 65 76 65 72 79 0a 2a 2a 20 20 20 20 20 20 6c 69  every.**      li
2d21a 6e 65 20 6f 66 20 74 72 61 63 65 20 6f 75 74 70  ne of trace outp
2d21b 75 74 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 74 68  ut.  If NULL, th
2d21c 65 6e 20 74 72 61 63 69 6e 67 20 69 73 0a 2a 2a  en tracing is.**
2d21d 20 20 20 20 20 20 74 75 72 6e 65 64 20 6f 66 66        turned off
2d21e 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  ..** </ul>.**.**
2d21f 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 4e 6f 6e   Outputs:.** Non
2d220 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2d221 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2d222 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c  3ParserTrace(FIL
2d223 45 20 2a 54 72 61 63 65 46 49 4c 45 2c 20 63 68  E *TraceFILE, ch
2d224 61 72 20 2a 7a 54 72 61 63 65 50 72 6f 6d 70 74  ar *zTracePrompt
2d225 29 7b 0a 20 20 79 79 54 72 61 63 65 46 49 4c 45  ){.  yyTraceFILE
2d226 20 3d 20 54 72 61 63 65 46 49 4c 45 3b 0a 20 20   = TraceFILE;.  
2d227 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20  yyTracePrompt = 
2d228 7a 54 72 61 63 65 50 72 6f 6d 70 74 3b 0a 20 20  zTracePrompt;.  
2d229 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 3d  if( yyTraceFILE=
2d22a 3d 30 20 29 20 79 79 54 72 61 63 65 50 72 6f 6d  =0 ) yyTraceProm
2d22b 70 74 20 3d 20 30 3b 0a 20 20 65 6c 73 65 20 69  pt = 0;.  else i
2d22c 66 28 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74  f( yyTracePrompt
2d22d 3d 3d 30 20 29 20 79 79 54 72 61 63 65 46 49 4c  ==0 ) yyTraceFIL
2d22e 45 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  E = 0;.}.#endif 
2d22f 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69  /* NDEBUG */..#i
2d230 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20  fndef NDEBUG./* 
2d231 46 6f 72 20 74 72 61 63 69 6e 67 20 73 68 69 66  For tracing shif
2d232 74 73 2c 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ts, the names of
2d233 20 61 6c 6c 20 74 65 72 6d 69 6e 61 6c 73 20 61   all terminals a
2d234 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 0a  nd nonterminals.
2d235 2a 2a 20 61 72 65 20 72 65 71 75 69 72 65 64 2e  ** are required.
2d236 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
2d237 74 61 62 6c 65 20 73 75 70 70 6c 69 65 73 20 74  table supplies t
2d238 68 65 73 65 20 6e 61 6d 65 73 20 2a 2f 0a 73 74  hese names */.st
2d239 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2d23a 2a 63 6f 6e 73 74 20 79 79 54 6f 6b 65 6e 4e 61  *const yyTokenNa
2d23b 6d 65 5b 5d 20 3d 20 7b 20 0a 20 20 22 24 22 2c  me[] = { .  "$",
2d23c 20 20 20 20 20 20 20 20 20 20 20 20 20 22 53 45               "SE
2d23d 4d 49 22 2c 20 20 20 20 20 20 20 20 20 20 22 45  MI",          "E
2d23e 58 50 4c 41 49 4e 22 2c 20 20 20 20 20 20 20 22  XPLAIN",       "
2d23f 51 55 45 52 59 22 2c 20 20 20 20 20 20 20 0a 20  QUERY",       . 
2d240 20 22 50 4c 41 4e 22 2c 20 20 20 20 20 20 20 20   "PLAN",        
2d241 20 20 22 42 45 47 49 4e 22 2c 20 20 20 20 20 20    "BEGIN",      
2d242 20 20 20 22 54 52 41 4e 53 41 43 54 49 4f 4e 22     "TRANSACTION"
2d243 2c 20 20 20 22 44 45 46 45 52 52 45 44 22 2c 20  ,   "DEFERRED", 
2d244 20 20 20 0a 20 20 22 49 4d 4d 45 44 49 41 54 45     .  "IMMEDIATE
2d245 22 2c 20 20 20 20 20 22 45 58 43 4c 55 53 49 56  ",     "EXCLUSIV
2d246 45 22 2c 20 20 20 20 20 22 43 4f 4d 4d 49 54 22  E",     "COMMIT"
2d247 2c 20 20 20 20 20 20 20 20 22 45 4e 44 22 2c 20  ,        "END", 
2d248 20 20 20 20 20 20 20 20 0a 20 20 22 52 4f 4c 4c          .  "ROLL
2d249 42 41 43 4b 22 2c 20 20 20 20 20 20 22 53 41 56  BACK",      "SAV
2d24a 45 50 4f 49 4e 54 22 2c 20 20 20 20 20 22 52 45  EPOINT",     "RE
2d24b 4c 45 41 53 45 22 2c 20 20 20 20 20 20 20 22 54  LEASE",       "T
2d24c 4f 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20  O",          .  
2d24d 22 54 41 42 4c 45 22 2c 20 20 20 20 20 20 20 20  "TABLE",        
2d24e 20 22 43 52 45 41 54 45 22 2c 20 20 20 20 20 20   "CREATE",      
2d24f 20 20 22 49 46 22 2c 20 20 20 20 20 20 20 20 20    "IF",         
2d250 20 20 20 22 4e 4f 54 22 2c 20 20 20 20 20 20 20     "NOT",       
2d251 20 20 0a 20 20 22 45 58 49 53 54 53 22 2c 20 20    .  "EXISTS",  
2d252 20 20 20 20 20 20 22 54 45 4d 50 22 2c 20 20 20        "TEMP",   
2d253 20 20 20 20 20 20 20 22 4c 50 22 2c 20 20 20 20         "LP",    
2d254 20 20 20 20 20 20 20 20 22 52 50 22 2c 20 20 20          "RP",   
2d255 20 20 20 20 20 20 20 0a 20 20 22 41 53 22 2c 20         .  "AS", 
2d256 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4d 4d             "COMM
2d257 41 22 2c 20 20 20 20 20 20 20 20 20 22 49 44 22  A",         "ID"
2d258 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e  ,            "IN
2d259 44 45 58 45 44 22 2c 20 20 20 20 20 0a 20 20 22  DEXED",     .  "
2d25a 41 42 4f 52 54 22 2c 20 20 20 20 20 20 20 20 20  ABORT",         
2d25b 22 41 46 54 45 52 22 2c 20 20 20 20 20 20 20 20  "AFTER",        
2d25c 20 22 41 4e 41 4c 59 5a 45 22 2c 20 20 20 20 20   "ANALYZE",     
2d25d 20 20 22 41 53 43 22 2c 20 20 20 20 20 20 20 20    "ASC",        
2d25e 20 0a 20 20 22 41 54 54 41 43 48 22 2c 20 20 20   .  "ATTACH",   
2d25f 20 20 20 20 20 22 42 45 46 4f 52 45 22 2c 20 20       "BEFORE",  
2d260 20 20 20 20 20 20 22 42 59 22 2c 20 20 20 20 20        "BY",     
2d261 20 20 20 20 20 20 20 22 43 41 53 43 41 44 45 22         "CASCADE"
2d262 2c 20 20 20 20 20 0a 20 20 22 43 41 53 54 22 2c  ,     .  "CAST",
2d263 20 20 20 20 20 20 20 20 20 20 22 43 4f 4c 55 4d            "COLUM
2d264 4e 4b 57 22 2c 20 20 20 20 20 20 22 43 4f 4e 46  NKW",      "CONF
2d265 4c 49 43 54 22 2c 20 20 20 20 20 20 22 44 41 54  LICT",      "DAT
2d266 41 42 41 53 45 22 2c 20 20 20 20 0a 20 20 22 44  ABASE",    .  "D
2d267 45 53 43 22 2c 20 20 20 20 20 20 20 20 20 20 22  ESC",          "
2d268 44 45 54 41 43 48 22 2c 20 20 20 20 20 20 20 20  DETACH",        
2d269 22 45 41 43 48 22 2c 20 20 20 20 20 20 20 20 20  "EACH",         
2d26a 20 22 46 41 49 4c 22 2c 20 20 20 20 20 20 20 20   "FAIL",        
2d26b 0a 20 20 22 46 4f 52 22 2c 20 20 20 20 20 20 20  .  "FOR",       
2d26c 20 20 20 20 22 49 47 4e 4f 52 45 22 2c 20 20 20      "IGNORE",   
2d26d 20 20 20 20 20 22 49 4e 49 54 49 41 4c 4c 59 22       "INITIALLY"
2d26e 2c 20 20 20 20 20 22 49 4e 53 54 45 41 44 22 2c  ,     "INSTEAD",
2d26f 20 20 20 20 20 0a 20 20 22 4c 49 4b 45 5f 4b 57       .  "LIKE_KW
2d270 22 2c 20 20 20 20 20 20 20 22 4d 41 54 43 48 22  ",       "MATCH"
2d271 2c 20 20 20 20 20 20 20 20 20 22 4b 45 59 22 2c  ,         "KEY",
2d272 20 20 20 20 20 20 20 20 20 20 20 22 4f 46 22 2c             "OF",
2d273 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 4f 46            .  "OF
2d274 46 53 45 54 22 2c 20 20 20 20 20 20 20 20 22 50  FSET",        "P
2d275 52 41 47 4d 41 22 2c 20 20 20 20 20 20 20 20 22  RAGMA",        "
2d276 52 41 49 53 45 22 2c 20 20 20 20 20 20 20 20 20  RAISE",         
2d277 22 52 45 50 4c 41 43 45 22 2c 20 20 20 20 20 0a  "REPLACE",     .
2d278 20 20 22 52 45 53 54 52 49 43 54 22 2c 20 20 20    "RESTRICT",   
2d279 20 20 20 22 52 4f 57 22 2c 20 20 20 20 20 20 20     "ROW",       
2d27a 20 20 20 20 22 54 52 49 47 47 45 52 22 2c 20 20      "TRIGGER",  
2d27b 20 20 20 20 20 22 56 41 43 55 55 4d 22 2c 20 20       "VACUUM",  
2d27c 20 20 20 20 0a 20 20 22 56 49 45 57 22 2c 20 20      .  "VIEW",  
2d27d 20 20 20 20 20 20 20 20 22 56 49 52 54 55 41 4c          "VIRTUAL
2d27e 22 2c 20 20 20 20 20 20 20 22 52 45 49 4e 44 45  ",       "REINDE
2d27f 58 22 2c 20 20 20 20 20 20 20 22 52 45 4e 41 4d  X",       "RENAM
2d280 45 22 2c 20 20 20 20 20 20 0a 20 20 22 43 54 49  E",      .  "CTI
2d281 4d 45 5f 4b 57 22 2c 20 20 20 20 20 20 22 41 4e  ME_KW",      "AN
2d282 59 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 4f  Y",           "O
2d283 52 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  R",            "
2d284 41 4e 44 22 2c 20 20 20 20 20 20 20 20 20 0a 20  AND",         . 
2d285 20 22 49 53 22 2c 20 20 20 20 20 20 20 20 20 20   "IS",          
2d286 20 20 22 42 45 54 57 45 45 4e 22 2c 20 20 20 20    "BETWEEN",    
2d287 20 20 20 22 49 4e 22 2c 20 20 20 20 20 20 20 20     "IN",        
2d288 20 20 20 20 22 49 53 4e 55 4c 4c 22 2c 20 20 20      "ISNULL",   
2d289 20 20 20 0a 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c     .  "NOTNULL",
2d28a 20 20 20 20 20 20 20 22 4e 45 22 2c 20 20 20 20         "NE",    
2d28b 20 20 20 20 20 20 20 20 22 45 51 22 2c 20 20 20          "EQ",   
2d28c 20 20 20 20 20 20 20 20 20 22 47 54 22 2c 20 20           "GT",  
2d28d 20 20 20 20 20 20 20 20 0a 20 20 22 4c 45 22 2c          .  "LE",
2d28e 20 20 20 20 20 20 20 20 20 20 20 20 22 4c 54 22              "LT"
2d28f 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 47 45  ,            "GE
2d290 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 45  ",            "E
2d291 53 43 41 50 45 22 2c 20 20 20 20 20 20 0a 20 20  SCAPE",      .  
2d292 22 42 49 54 41 4e 44 22 2c 20 20 20 20 20 20 20  "BITAND",       
2d293 20 22 42 49 54 4f 52 22 2c 20 20 20 20 20 20 20   "BITOR",       
2d294 20 20 22 4c 53 48 49 46 54 22 2c 20 20 20 20 20    "LSHIFT",     
2d295 20 20 20 22 52 53 48 49 46 54 22 2c 20 20 20 20     "RSHIFT",    
2d296 20 20 0a 20 20 22 50 4c 55 53 22 2c 20 20 20 20    .  "PLUS",    
2d297 20 20 20 20 20 20 22 4d 49 4e 55 53 22 2c 20 20        "MINUS",  
2d298 20 20 20 20 20 20 20 22 53 54 41 52 22 2c 20 20         "STAR",  
2d299 20 20 20 20 20 20 20 20 22 53 4c 41 53 48 22 2c          "SLASH",
2d29a 20 20 20 20 20 20 20 0a 20 20 22 52 45 4d 22 2c         .  "REM",
2d29b 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4e 43             "CONC
2d29c 41 54 22 2c 20 20 20 20 20 20 20 20 22 43 4f 4c  AT",        "COL
2d29d 4c 41 54 45 22 2c 20 20 20 20 20 20 20 22 55 4d  LATE",       "UM
2d29e 49 4e 55 53 22 2c 20 20 20 20 20 20 0a 20 20 22  INUS",      .  "
2d29f 55 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20 20  UPLUS",         
2d2a0 22 42 49 54 4e 4f 54 22 2c 20 20 20 20 20 20 20  "BITNOT",       
2d2a1 20 22 53 54 52 49 4e 47 22 2c 20 20 20 20 20 20   "STRING",      
2d2a2 20 20 22 4a 4f 49 4e 5f 4b 57 22 2c 20 20 20 20    "JOIN_KW",    
2d2a3 20 0a 20 20 22 43 4f 4e 53 54 52 41 49 4e 54 22   .  "CONSTRAINT"
2d2a4 2c 20 20 20 20 22 44 45 46 41 55 4c 54 22 2c 20  ,    "DEFAULT", 
2d2a5 20 20 20 20 20 20 22 4e 55 4c 4c 22 2c 20 20 20        "NULL",   
2d2a6 20 20 20 20 20 20 20 22 50 52 49 4d 41 52 59 22         "PRIMARY"
2d2a7 2c 20 20 20 20 20 0a 20 20 22 55 4e 49 51 55 45  ,     .  "UNIQUE
2d2a8 22 2c 20 20 20 20 20 20 20 20 22 43 48 45 43 4b  ",        "CHECK
2d2a9 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 46 45  ",         "REFE
2d2aa 52 45 4e 43 45 53 22 2c 20 20 20 20 22 41 55 54  RENCES",    "AUT
2d2ab 4f 49 4e 43 52 22 2c 20 20 20 20 0a 20 20 22 4f  OINCR",    .  "O
2d2ac 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  N",            "
2d2ad 44 45 4c 45 54 45 22 2c 20 20 20 20 20 20 20 20  DELETE",        
2d2ae 22 55 50 44 41 54 45 22 2c 20 20 20 20 20 20 20  "UPDATE",       
2d2af 20 22 49 4e 53 45 52 54 22 2c 20 20 20 20 20 20   "INSERT",      
2d2b0 0a 20 20 22 53 45 54 22 2c 20 20 20 20 20 20 20  .  "SET",       
2d2b1 20 20 20 20 22 44 45 46 45 52 52 41 42 4c 45 22      "DEFERRABLE"
2d2b2 2c 20 20 20 20 22 46 4f 52 45 49 47 4e 22 2c 20  ,    "FOREIGN", 
2d2b3 20 20 20 20 20 20 22 44 52 4f 50 22 2c 20 20 20        "DROP",   
2d2b4 20 20 20 20 20 0a 20 20 22 55 4e 49 4f 4e 22 2c       .  "UNION",
2d2b5 20 20 20 20 20 20 20 20 20 22 41 4c 4c 22 2c 20           "ALL", 
2d2b6 20 20 20 20 20 20 20 20 20 20 22 45 58 43 45 50            "EXCEP
2d2b7 54 22 2c 20 20 20 20 20 20 20 20 22 49 4e 54 45  T",        "INTE
2d2b8 52 53 45 43 54 22 2c 20 20 20 0a 20 20 22 53 45  RSECT",   .  "SE
2d2b9 4c 45 43 54 22 2c 20 20 20 20 20 20 20 20 22 44  LECT",        "D
2d2ba 49 53 54 49 4e 43 54 22 2c 20 20 20 20 20 20 22  ISTINCT",      "
2d2bb 44 4f 54 22 2c 20 20 20 20 20 20 20 20 20 20 20  DOT",           
2d2bc 22 46 52 4f 4d 22 2c 20 20 20 20 20 20 20 20 0a  "FROM",        .
2d2bd 20 20 22 4a 4f 49 4e 22 2c 20 20 20 20 20 20 20    "JOIN",       
2d2be 20 20 20 22 55 53 49 4e 47 22 2c 20 20 20 20 20     "USING",     
2d2bf 20 20 20 20 22 4f 52 44 45 52 22 2c 20 20 20 20      "ORDER",    
2d2c0 20 20 20 20 20 22 47 52 4f 55 50 22 2c 20 20 20       "GROUP",   
2d2c1 20 20 20 20 0a 20 20 22 48 41 56 49 4e 47 22 2c      .  "HAVING",
2d2c2 20 20 20 20 20 20 20 20 22 4c 49 4d 49 54 22 2c          "LIMIT",
2d2c3 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45 22           "WHERE"
2d2c4 2c 20 20 20 20 20 20 20 20 20 22 49 4e 54 4f 22  ,         "INTO"
2d2c5 2c 20 20 20 20 20 20 20 20 0a 20 20 22 56 41 4c  ,        .  "VAL
2d2c6 55 45 53 22 2c 20 20 20 20 20 20 20 20 22 49 4e  UES",        "IN
2d2c7 54 45 47 45 52 22 2c 20 20 20 20 20 20 20 22 46  TEGER",       "F
2d2c8 4c 4f 41 54 22 2c 20 20 20 20 20 20 20 20 20 22  LOAT",         "
2d2c9 42 4c 4f 42 22 2c 20 20 20 20 20 20 20 20 0a 20  BLOB",        . 
2d2ca 20 22 52 45 47 49 53 54 45 52 22 2c 20 20 20 20   "REGISTER",    
2d2cb 20 20 22 56 41 52 49 41 42 4c 45 22 2c 20 20 20    "VARIABLE",   
2d2cc 20 20 20 22 43 41 53 45 22 2c 20 20 20 20 20 20     "CASE",      
2d2cd 20 20 20 20 22 57 48 45 4e 22 2c 20 20 20 20 20      "WHEN",     
2d2ce 20 20 20 0a 20 20 22 54 48 45 4e 22 2c 20 20 20     .  "THEN",   
2d2cf 20 20 20 20 20 20 20 22 45 4c 53 45 22 2c 20 20         "ELSE",  
2d2d0 20 20 20 20 20 20 20 20 22 49 4e 44 45 58 22 2c          "INDEX",
2d2d1 20 20 20 20 20 20 20 20 20 22 41 4c 54 45 52 22           "ALTER"
2d2d2 2c 20 20 20 20 20 20 20 0a 20 20 22 41 44 44 22  ,       .  "ADD"
2d2d3 2c 20 20 20 20 20 20 20 20 20 20 20 22 65 72 72  ,           "err
2d2d4 6f 72 22 2c 20 20 20 20 20 20 20 20 20 22 69 6e  or",         "in
2d2d5 70 75 74 22 2c 20 20 20 20 20 20 20 20 20 22 63  put",         "c
2d2d6 6d 64 6c 69 73 74 22 2c 20 20 20 20 20 0a 20 20  mdlist",     .  
2d2d7 22 65 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20  "ecmd",         
2d2d8 20 22 65 78 70 6c 61 69 6e 22 2c 20 20 20 20 20   "explain",     
2d2d9 20 20 22 63 6d 64 78 22 2c 20 20 20 20 20 20 20    "cmdx",       
2d2da 20 20 20 22 63 6d 64 22 2c 20 20 20 20 20 20 20     "cmd",       
2d2db 20 20 0a 20 20 22 74 72 61 6e 73 74 79 70 65 22    .  "transtype"
2d2dc 2c 20 20 20 20 20 22 74 72 61 6e 73 5f 6f 70 74  ,     "trans_opt
2d2dd 22 2c 20 20 20 20 20 22 6e 6d 22 2c 20 20 20 20  ",     "nm",    
2d2de 20 20 20 20 20 20 20 20 22 73 61 76 65 70 6f 69          "savepoi
2d2df 6e 74 5f 6f 70 74 22 2c 0a 20 20 22 63 72 65 61  nt_opt",.  "crea
2d2e0 74 65 5f 74 61 62 6c 65 22 2c 20 20 22 63 72 65  te_table",  "cre
2d2e1 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c  ate_table_args",
2d2e2 20 20 22 63 72 65 61 74 65 6b 77 22 2c 20 20 20    "createkw",   
2d2e3 20 20 20 22 74 65 6d 70 22 2c 20 20 20 20 20 20     "temp",      
2d2e4 20 20 0a 20 20 22 69 66 6e 6f 74 65 78 69 73 74    .  "ifnotexist
2d2e5 73 22 2c 20 20 20 22 64 62 6e 6d 22 2c 20 20 20  s",   "dbnm",   
2d2e6 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 6c 69         "columnli
2d2e7 73 74 22 2c 20 20 20 20 22 63 6f 6e 73 6c 69 73  st",    "conslis
2d2e8 74 5f 6f 70 74 22 2c 0a 20 20 22 73 65 6c 65 63  t_opt",.  "selec
2d2e9 74 22 2c 20 20 20 20 20 20 20 20 22 63 6f 6c 75  t",        "colu
2d2ea 6d 6e 22 2c 20 20 20 20 20 20 20 20 22 63 6f 6c  mn",        "col
2d2eb 75 6d 6e 69 64 22 2c 20 20 20 20 20 20 22 74 79  umnid",      "ty
2d2ec 70 65 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  pe",        .  "
2d2ed 63 61 72 67 6c 69 73 74 22 2c 20 20 20 20 20 20  carglist",      
2d2ee 22 69 64 22 2c 20 20 20 20 20 20 20 20 20 20 20  "id",           
2d2ef 20 22 69 64 73 22 2c 20 20 20 20 20 20 20 20 20   "ids",         
2d2f0 20 20 22 74 79 70 65 74 6f 6b 65 6e 22 2c 20 20    "typetoken",  
2d2f1 20 0a 20 20 22 74 79 70 65 6e 61 6d 65 22 2c 20   .  "typename", 
2d2f2 20 20 20 20 20 22 73 69 67 6e 65 64 22 2c 20 20       "signed",  
2d2f3 20 20 20 20 20 20 22 70 6c 75 73 5f 6e 75 6d 22        "plus_num"
2d2f4 2c 20 20 20 20 20 20 22 6d 69 6e 75 73 5f 6e 75  ,      "minus_nu
2d2f5 6d 22 2c 20 20 20 0a 20 20 22 63 61 72 67 22 2c  m",   .  "carg",
2d2f6 20 20 20 20 20 20 20 20 20 20 22 63 63 6f 6e 73            "ccons
2d2f7 22 2c 20 20 20 20 20 20 20 20 20 22 74 65 72 6d  ",         "term
2d2f8 22 2c 20 20 20 20 20 20 20 20 20 20 22 65 78 70  ",          "exp
2d2f9 72 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 6f  r",        .  "o
2d2fa 6e 63 6f 6e 66 22 2c 20 20 20 20 20 20 20 20 22  nconf",        "
2d2fb 73 6f 72 74 6f 72 64 65 72 22 2c 20 20 20 20 20  sortorder",     
2d2fc 22 61 75 74 6f 69 6e 63 22 2c 20 20 20 20 20 20  "autoinc",      
2d2fd 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 22 2c 20   "idxlist_opt", 
2d2fe 0a 20 20 22 72 65 66 61 72 67 73 22 2c 20 20 20  .  "refargs",   
2d2ff 20 20 20 20 22 64 65 66 65 72 5f 73 75 62 63 6c      "defer_subcl
2d300 61 75 73 65 22 2c 20 20 22 72 65 66 61 72 67 22  ause",  "refarg"
2d301 2c 20 20 20 20 20 20 20 20 22 72 65 66 61 63 74  ,        "refact
2d302 22 2c 20 20 20 20 20 20 0a 20 20 22 69 6e 69 74  ",      .  "init
2d303 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
2d304 70 74 22 2c 20 20 22 63 6f 6e 73 6c 69 73 74 22  pt",  "conslist"
2d305 2c 20 20 20 20 20 20 22 74 63 6f 6e 73 22 2c 20  ,      "tcons", 
2d306 20 20 20 20 20 20 20 20 22 69 64 78 6c 69 73 74          "idxlist
2d307 22 2c 20 20 20 20 20 0a 20 20 22 64 65 66 65 72  ",     .  "defer
2d308 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c  _subclause_opt",
2d309 20 20 22 6f 72 63 6f 6e 66 22 2c 20 20 20 20 20    "orconf",     
2d30a 20 20 20 22 72 65 73 6f 6c 76 65 74 79 70 65 22     "resolvetype"
2d30b 2c 20 20 20 22 72 61 69 73 65 74 79 70 65 22 2c  ,   "raisetype",
2d30c 20 20 20 0a 20 20 22 69 66 65 78 69 73 74 73 22     .  "ifexists"
2d30d 2c 20 20 20 20 20 20 22 66 75 6c 6c 6e 61 6d 65  ,      "fullname
2d30e 22 2c 20 20 20 20 20 20 22 6f 6e 65 73 65 6c 65  ",      "onesele
2d30f 63 74 22 2c 20 20 20 20 20 22 6d 75 6c 74 69 73  ct",     "multis
2d310 65 6c 65 63 74 5f 6f 70 22 2c 0a 20 20 22 64 69  elect_op",.  "di
2d311 73 74 69 6e 63 74 22 2c 20 20 20 20 20 20 22 73  stinct",      "s
2d312 65 6c 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22  elcollist",    "
2d313 66 72 6f 6d 22 2c 20 20 20 20 20 20 20 20 20 20  from",          
2d314 22 77 68 65 72 65 5f 6f 70 74 22 2c 20 20 20 0a  "where_opt",   .
2d315 20 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 22 2c    "groupby_opt",
2d316 20 20 20 22 68 61 76 69 6e 67 5f 6f 70 74 22 2c     "having_opt",
2d317 20 20 20 20 22 6f 72 64 65 72 62 79 5f 6f 70 74      "orderby_opt
2d318 22 2c 20 20 20 22 6c 69 6d 69 74 5f 6f 70 74 22  ",   "limit_opt"
2d319 2c 20 20 20 0a 20 20 22 73 63 6c 70 22 2c 20 20  ,   .  "sclp",  
2d31a 20 20 20 20 20 20 20 20 22 61 73 22 2c 20 20 20          "as",   
2d31b 20 20 20 20 20 20 20 20 20 22 73 65 6c 74 61 62           "seltab
2d31c 6c 69 73 74 22 2c 20 20 20 20 22 73 74 6c 5f 70  list",    "stl_p
2d31d 72 65 66 69 78 22 2c 20 20 0a 20 20 22 6a 6f 69  refix",  .  "joi
2d31e 6e 6f 70 22 2c 20 20 20 20 20 20 20 20 22 69 6e  nop",        "in
2d31f 64 65 78 65 64 5f 6f 70 74 22 2c 20 20 20 22 6f  dexed_opt",   "o
2d320 6e 5f 6f 70 74 22 2c 20 20 20 20 20 20 20 20 22  n_opt",        "
2d321 75 73 69 6e 67 5f 6f 70 74 22 2c 20 20 20 0a 20  using_opt",   . 
2d322 20 22 6a 6f 69 6e 6f 70 32 22 2c 20 20 20 20 20   "joinop2",     
2d323 20 20 22 69 6e 73 63 6f 6c 6c 69 73 74 22 2c 20    "inscollist", 
2d324 20 20 20 22 73 6f 72 74 6c 69 73 74 22 2c 20 20     "sortlist",  
2d325 20 20 20 20 22 73 6f 72 74 69 74 65 6d 22 2c 20      "sortitem", 
2d326 20 20 20 0a 20 20 22 6e 65 78 70 72 6c 69 73 74     .  "nexprlist
2d327 22 2c 20 20 20 20 20 22 73 65 74 6c 69 73 74 22  ",     "setlist"
2d328 2c 20 20 20 20 20 20 20 22 69 6e 73 65 72 74 5f  ,       "insert_
2d329 63 6d 64 22 2c 20 20 20 20 22 69 6e 73 63 6f 6c  cmd",    "inscol
2d32a 6c 69 73 74 5f 6f 70 74 22 2c 0a 20 20 22 69 74  list_opt",.  "it
2d32b 65 6d 6c 69 73 74 22 2c 20 20 20 20 20 20 22 65  emlist",      "e
2d32c 78 70 72 6c 69 73 74 22 2c 20 20 20 20 20 20 22  xprlist",      "
2d32d 6c 69 6b 65 6f 70 22 2c 20 20 20 20 20 20 20 20  likeop",        
2d32e 22 65 73 63 61 70 65 22 2c 20 20 20 20 20 20 0a  "escape",      .
2d32f 20 20 22 62 65 74 77 65 65 6e 5f 6f 70 22 2c 20    "between_op", 
2d330 20 20 20 22 69 6e 5f 6f 70 22 2c 20 20 20 20 20     "in_op",     
2d331 20 20 20 20 22 63 61 73 65 5f 6f 70 65 72 61 6e      "case_operan
2d332 64 22 2c 20 20 22 63 61 73 65 5f 65 78 70 72 6c  d",  "case_exprl
2d333 69 73 74 22 2c 0a 20 20 22 63 61 73 65 5f 65 6c  ist",.  "case_el
2d334 73 65 22 2c 20 20 20 20 20 22 75 6e 69 71 75 65  se",     "unique
2d335 66 6c 61 67 22 2c 20 20 20 20 22 63 6f 6c 6c 61  flag",    "colla
2d336 74 65 22 2c 20 20 20 20 20 20 20 22 6e 6d 6e 75  te",       "nmnu
2d337 6d 22 2c 20 20 20 20 20 20 20 0a 20 20 22 70 6c  m",       .  "pl
2d338 75 73 5f 6f 70 74 22 2c 20 20 20 20 20 20 22 6e  us_opt",      "n
2d339 75 6d 62 65 72 22 2c 20 20 20 20 20 20 20 20 22  umber",        "
2d33a 74 72 69 67 67 65 72 5f 64 65 63 6c 22 2c 20 20  trigger_decl",  
2d33b 22 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73  "trigger_cmd_lis
2d33c 74 22 2c 0a 20 20 22 74 72 69 67 67 65 72 5f 74  t",.  "trigger_t
2d33d 69 6d 65 22 2c 20 20 22 74 72 69 67 67 65 72 5f  ime",  "trigger_
2d33e 65 76 65 6e 74 22 2c 20 20 22 66 6f 72 65 61 63  event",  "foreac
2d33f 68 5f 63 6c 61 75 73 65 22 2c 20 20 22 77 68 65  h_clause",  "whe
2d340 6e 5f 63 6c 61 75 73 65 22 2c 20 0a 20 20 22 74  n_clause", .  "t
2d341 72 69 67 67 65 72 5f 63 6d 64 22 2c 20 20 20 22  rigger_cmd",   "
2d342 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 22  database_kw_opt"
2d343 2c 20 20 22 6b 65 79 5f 6f 70 74 22 2c 20 20 20  ,  "key_opt",   
2d344 20 20 20 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f      "add_column_
2d345 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 20 22 6b 77  fullname",.  "kw
2d346 63 6f 6c 75 6d 6e 5f 6f 70 74 22 2c 20 20 22 63  column_opt",  "c
2d347 72 65 61 74 65 5f 76 74 61 62 22 2c 20 20 20 22  reate_vtab",   "
2d348 76 74 61 62 61 72 67 6c 69 73 74 22 2c 20 20 20  vtabarglist",   
2d349 22 76 74 61 62 61 72 67 22 2c 20 20 20 20 20 0a  "vtabarg",     .
2d34a 20 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 22    "vtabargtoken"
2d34b 2c 20 20 22 6c 70 22 2c 20 20 20 20 20 20 20 20  ,  "lp",        
2d34c 20 20 20 20 22 61 6e 79 6c 69 73 74 22 2c 20 20      "anylist",  
2d34d 20 20 20 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a     .};.#endif /*
2d34e 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e   NDEBUG */..#ifn
2d34f 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f  def NDEBUG./* Fo
2d350 72 20 74 72 61 63 69 6e 67 20 72 65 64 75 63 65  r tracing reduce
2d351 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 6e 61   actions, the na
2d352 6d 65 73 20 6f 66 20 61 6c 6c 20 72 75 6c 65 73  mes of all rules
2d353 20 61 72 65 20 72 65 71 75 69 72 65 64 2e 0a 2a   are required..*
2d354 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
2d355 68 61 72 20 2a 63 6f 6e 73 74 20 79 79 52 75 6c  har *const yyRul
2d356 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 2f 2a 20  eName[] = {. /* 
2d357 20 20 30 20 2a 2f 20 22 69 6e 70 75 74 20 3a 3a    0 */ "input ::
2d358 3d 20 63 6d 64 6c 69 73 74 22 2c 0a 20 2f 2a 20  = cmdlist",. /* 
2d359 20 20 31 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20    1 */ "cmdlist 
2d35a 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d 64  ::= cmdlist ecmd
2d35b 22 2c 0a 20 2f 2a 20 20 20 32 20 2a 2f 20 22 63  ",. /*   2 */ "c
2d35c 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d 64 22  mdlist ::= ecmd"
2d35d 2c 0a 20 2f 2a 20 20 20 33 20 2a 2f 20 22 65 63  ,. /*   3 */ "ec
2d35e 6d 64 20 3a 3a 3d 20 53 45 4d 49 22 2c 0a 20 2f  md ::= SEMI",. /
2d35f 2a 20 20 20 34 20 2a 2f 20 22 65 63 6d 64 20 3a  *   4 */ "ecmd :
2d360 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64 78 20  := explain cmdx 
2d361 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 35 20 2a  SEMI",. /*   5 *
2d362 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 22 2c  / "explain ::=",
2d363 0a 20 2f 2a 20 20 20 36 20 2a 2f 20 22 65 78 70  . /*   6 */ "exp
2d364 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e  lain ::= EXPLAIN
2d365 22 2c 0a 20 2f 2a 20 20 20 37 20 2a 2f 20 22 65  ",. /*   7 */ "e
2d366 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41  xplain ::= EXPLA
2d367 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 22 2c 0a  IN QUERY PLAN",.
2d368 20 2f 2a 20 20 20 38 20 2a 2f 20 22 63 6d 64 78   /*   8 */ "cmdx
2d369 20 3a 3a 3d 20 63 6d 64 22 2c 0a 20 2f 2a 20 20   ::= cmd",. /*  
2d36a 20 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 42   9 */ "cmd ::= B
2d36b 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74  EGIN transtype t
2d36c 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20  rans_opt",. /*  
2d36d 31 30 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74  10 */ "trans_opt
2d36e 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 31 20 2a   ::=",. /*  11 *
2d36f 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d  / "trans_opt ::=
2d370 20 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c 0a 20   TRANSACTION",. 
2d371 2f 2a 20 20 31 32 20 2a 2f 20 22 74 72 61 6e 73  /*  12 */ "trans
2d372 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43  _opt ::= TRANSAC
2d373 54 49 4f 4e 20 6e 6d 22 2c 0a 20 2f 2a 20 20 31  TION nm",. /*  1
2d374 33 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20  3 */ "transtype 
2d375 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 34 20 2a 2f  ::=",. /*  14 */
2d376 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20   "transtype ::= 
2d377 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20  DEFERRED",. /*  
2d378 31 35 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65  15 */ "transtype
2d379 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 22 2c   ::= IMMEDIATE",
2d37a 0a 20 2f 2a 20 20 31 36 20 2a 2f 20 22 74 72 61  . /*  16 */ "tra
2d37b 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43 4c 55  nstype ::= EXCLU
2d37c 53 49 56 45 22 2c 0a 20 2f 2a 20 20 31 37 20 2a  SIVE",. /*  17 *
2d37d 2f 20 22 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49  / "cmd ::= COMMI
2d37e 54 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f  T trans_opt",. /
2d37f 2a 20 20 31 38 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  18 */ "cmd ::
2d380 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 22  = END trans_opt"
2d381 2c 0a 20 2f 2a 20 20 31 39 20 2a 2f 20 22 63 6d  ,. /*  19 */ "cm
2d382 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74  d ::= ROLLBACK t
2d383 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20  rans_opt",. /*  
2d384 32 30 20 2a 2f 20 22 73 61 76 65 70 6f 69 6e 74  20 */ "savepoint
2d385 5f 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49  _opt ::= SAVEPOI
2d386 4e 54 22 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f 20  NT",. /*  21 */ 
2d387 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a  "savepoint_opt :
2d388 3a 3d 22 2c 0a 20 2f 2a 20 20 32 32 20 2a 2f 20  :=",. /*  22 */ 
2d389 22 63 6d 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49  "cmd ::= SAVEPOI
2d38a 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 33 20  NT nm",. /*  23 
2d38b 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 4c 45  */ "cmd ::= RELE
2d38c 41 53 45 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70  ASE savepoint_op
2d38d 74 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 34 20 2a  t nm",. /*  24 *
2d38e 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42  / "cmd ::= ROLLB
2d38f 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f  ACK trans_opt TO
2d390 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e   savepoint_opt n
2d391 6d 22 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22  m",. /*  25 */ "
2d392 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74  cmd ::= create_t
2d393 61 62 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c  able create_tabl
2d394 65 5f 61 72 67 73 22 2c 0a 20 2f 2a 20 20 32 36  e_args",. /*  26
2d395 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c   */ "create_tabl
2d396 65 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74  e ::= createkw t
2d397 65 6d 70 20 54 41 42 4c 45 20 69 66 6e 6f 74 65  emp TABLE ifnote
2d398 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a  xists nm dbnm",.
2d399 20 2f 2a 20 20 32 37 20 2a 2f 20 22 63 72 65 61   /*  27 */ "crea
2d39a 74 65 6b 77 20 3a 3a 3d 20 43 52 45 41 54 45 22  tekw ::= CREATE"
2d39b 2c 0a 20 2f 2a 20 20 32 38 20 2a 2f 20 22 69 66  ,. /*  28 */ "if
2d39c 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a  notexists ::=",.
2d39d 20 2f 2a 20 20 32 39 20 2a 2f 20 22 69 66 6e 6f   /*  29 */ "ifno
2d39e 74 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e  texists ::= IF N
2d39f 4f 54 20 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20  OT EXISTS",. /* 
2d3a0 20 33 30 20 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d   30 */ "temp ::=
2d3a1 20 54 45 4d 50 22 2c 0a 20 2f 2a 20 20 33 31 20   TEMP",. /*  31 
2d3a2 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 22 2c 0a 20  */ "temp ::=",. 
2d3a3 2f 2a 20 20 33 32 20 2a 2f 20 22 63 72 65 61 74  /*  32 */ "creat
2d3a4 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d  e_table_args ::=
2d3a5 20 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63   LP columnlist c
2d3a6 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 52 50 22 2c  onslist_opt RP",
2d3a7 0a 20 2f 2a 20 20 33 33 20 2a 2f 20 22 63 72 65  . /*  33 */ "cre
2d3a8 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a  ate_table_args :
2d3a9 3a 3d 20 41 53 20 73 65 6c 65 63 74 22 2c 0a 20  := AS select",. 
2d3aa 2f 2a 20 20 33 34 20 2a 2f 20 22 63 6f 6c 75 6d  /*  34 */ "colum
2d3ab 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e  nlist ::= column
2d3ac 6c 69 73 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d  list COMMA colum
2d3ad 6e 22 2c 0a 20 2f 2a 20 20 33 35 20 2a 2f 20 22  n",. /*  35 */ "
2d3ae 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63  columnlist ::= c
2d3af 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20 33 36 20  olumn",. /*  36 
2d3b0 2a 2f 20 22 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63  */ "column ::= c
2d3b1 6f 6c 75 6d 6e 69 64 20 74 79 70 65 20 63 61 72  olumnid type car
2d3b2 67 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 33 37 20  glist",. /*  37 
2d3b3 2a 2f 20 22 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d  */ "columnid ::=
2d3b4 20 6e 6d 22 2c 0a 20 2f 2a 20 20 33 38 20 2a 2f   nm",. /*  38 */
2d3b5 20 22 69 64 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f   "id ::= ID",. /
2d3b6 2a 20 20 33 39 20 2a 2f 20 22 69 64 20 3a 3a 3d  *  39 */ "id ::=
2d3b7 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 20   INDEXED",. /*  
2d3b8 34 30 20 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49  40 */ "ids ::= I
2d3b9 44 7c 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20  D|STRING",. /*  
2d3ba 34 31 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 69 64  41 */ "nm ::= id
2d3bb 22 2c 0a 20 2f 2a 20 20 34 32 20 2a 2f 20 22 6e  ",. /*  42 */ "n
2d3bc 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20  m ::= STRING",. 
2d3bd 2f 2a 20 20 34 33 20 2a 2f 20 22 6e 6d 20 3a 3a  /*  43 */ "nm ::
2d3be 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20  = JOIN_KW",. /* 
2d3bf 20 34 34 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d   44 */ "type ::=
2d3c0 22 2c 0a 20 2f 2a 20 20 34 35 20 2a 2f 20 22 74  ",. /*  45 */ "t
2d3c1 79 70 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65  ype ::= typetoke
2d3c2 6e 22 2c 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22  n",. /*  46 */ "
2d3c3 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
2d3c4 70 65 6e 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 37  pename",. /*  47
2d3c5 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a   */ "typetoken :
2d3c6 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73  := typename LP s
2d3c7 69 67 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20  igned RP",. /*  
2d3c8 34 38 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e  48 */ "typetoken
2d3c9 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50   ::= typename LP
2d3ca 20 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69   signed COMMA si
2d3cb 67 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34  gned RP",. /*  4
2d3cc 39 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a  9 */ "typename :
2d3cd 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 20 35 30  := ids",. /*  50
2d3ce 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a   */ "typename ::
2d3cf 3d 20 74 79 70 65 6e 61 6d 65 20 69 64 73 22 2c  = typename ids",
2d3d0 0a 20 2f 2a 20 20 35 31 20 2a 2f 20 22 73 69 67  . /*  51 */ "sig
2d3d1 6e 65 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d  ned ::= plus_num
2d3d2 22 2c 0a 20 2f 2a 20 20 35 32 20 2a 2f 20 22 73  ",. /*  52 */ "s
2d3d3 69 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f  igned ::= minus_
2d3d4 6e 75 6d 22 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f  num",. /*  53 */
2d3d5 20 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63   "carglist ::= c
2d3d6 61 72 67 6c 69 73 74 20 63 61 72 67 22 2c 0a 20  arglist carg",. 
2d3d7 2f 2a 20 20 35 34 20 2a 2f 20 22 63 61 72 67 6c  /*  54 */ "cargl
2d3d8 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 35  ist ::=",. /*  5
2d3d9 35 20 2a 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43  5 */ "carg ::= C
2d3da 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f  ONSTRAINT nm cco
2d3db 6e 73 22 2c 0a 20 2f 2a 20 20 35 36 20 2a 2f 20  ns",. /*  56 */ 
2d3dc 22 63 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22  "carg ::= ccons"
2d3dd 2c 0a 20 2f 2a 20 20 35 37 20 2a 2f 20 22 63 63  ,. /*  57 */ "cc
2d3de 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
2d3df 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 35 38 20 2a  term",. /*  58 *
2d3e0 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  / "ccons ::= DEF
2d3e1 41 55 4c 54 20 4c 50 20 65 78 70 72 20 52 50 22  AULT LP expr RP"
2d3e2 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f 20 22 63 63  ,. /*  59 */ "cc
2d3e3 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
2d3e4 50 4c 55 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20  PLUS term",. /* 
2d3e5 20 36 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   60 */ "ccons ::
2d3e6 3d 20 44 45 46 41 55 4c 54 20 4d 49 4e 55 53 20  = DEFAULT MINUS 
2d3e7 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 31 20 2a  term",. /*  61 *
2d3e8 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  / "ccons ::= DEF
2d3e9 41 55 4c 54 20 69 64 22 2c 0a 20 2f 2a 20 20 36  AULT id",. /*  6
2d3ea 32 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  2 */ "ccons ::= 
2d3eb 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f  NULL onconf",. /
2d3ec 2a 20 20 36 33 20 2a 2f 20 22 63 63 6f 6e 73 20  *  63 */ "ccons 
2d3ed 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63  ::= NOT NULL onc
2d3ee 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 34 20 2a 2f  onf",. /*  64 */
2d3ef 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d   "ccons ::= PRIM
2d3f0 41 52 59 20 4b 45 59 20 73 6f 72 74 6f 72 64 65  ARY KEY sortorde
2d3f1 72 20 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63  r onconf autoinc
2d3f2 22 2c 0a 20 2f 2a 20 20 36 35 20 2a 2f 20 22 63  ",. /*  65 */ "c
2d3f3 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20  cons ::= UNIQUE 
2d3f4 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 36  onconf",. /*  66
2d3f5 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43   */ "ccons ::= C
2d3f6 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50 22  HECK LP expr RP"
2d3f7 2c 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22 63 63  ,. /*  67 */ "cc
2d3f8 6f 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43  ons ::= REFERENC
2d3f9 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70  ES nm idxlist_op
2d3fa 74 20 72 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20  t refargs",. /* 
2d3fb 20 36 38 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   68 */ "ccons ::
2d3fc 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  = defer_subclaus
2d3fd 65 22 2c 0a 20 2f 2a 20 20 36 39 20 2a 2f 20 22  e",. /*  69 */ "
2d3fe 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54  ccons ::= COLLAT
2d3ff 45 20 69 64 73 22 2c 0a 20 2f 2a 20 20 37 30 20  E ids",. /*  70 
2d400 2a 2f 20 22 61 75 74 6f 69 6e 63 20 3a 3a 3d 22  */ "autoinc ::="
2d401 2c 0a 20 2f 2a 20 20 37 31 20 2a 2f 20 22 61 75  ,. /*  71 */ "au
2d402 74 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e  toinc ::= AUTOIN
2d403 43 52 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f 20  CR",. /*  72 */ 
2d404 22 72 65 66 61 72 67 73 20 3a 3a 3d 22 2c 0a 20  "refargs ::=",. 
2d405 2f 2a 20 20 37 33 20 2a 2f 20 22 72 65 66 61 72  /*  73 */ "refar
2d406 67 73 20 3a 3a 3d 20 72 65 66 61 72 67 73 20 72  gs ::= refargs r
2d407 65 66 61 72 67 22 2c 0a 20 2f 2a 20 20 37 34 20  efarg",. /*  74 
2d408 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4d  */ "refarg ::= M
2d409 41 54 43 48 20 6e 6d 22 2c 0a 20 2f 2a 20 20 37  ATCH nm",. /*  7
2d40a 35 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d  5 */ "refarg ::=
2d40b 20 4f 4e 20 44 45 4c 45 54 45 20 72 65 66 61 63   ON DELETE refac
2d40c 74 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22  t",. /*  76 */ "
2d40d 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50  refarg ::= ON UP
2d40e 44 41 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f  DATE refact",. /
2d40f 2a 20 20 37 37 20 2a 2f 20 22 72 65 66 61 72 67  *  77 */ "refarg
2d410 20 3a 3a 3d 20 4f 4e 20 49 4e 53 45 52 54 20 72   ::= ON INSERT r
2d411 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37 38 20  efact",. /*  78 
2d412 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53  */ "refact ::= S
2d413 45 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 20 37  ET NULL",. /*  7
2d414 39 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d  9 */ "refact ::=
2d415 20 53 45 54 20 44 45 46 41 55 4c 54 22 2c 0a 20   SET DEFAULT",. 
2d416 2f 2a 20 20 38 30 20 2a 2f 20 22 72 65 66 61 63  /*  80 */ "refac
2d417 74 20 3a 3a 3d 20 43 41 53 43 41 44 45 22 2c 0a  t ::= CASCADE",.
2d418 20 2f 2a 20 20 38 31 20 2a 2f 20 22 72 65 66 61   /*  81 */ "refa
2d419 63 74 20 3a 3a 3d 20 52 45 53 54 52 49 43 54 22  ct ::= RESTRICT"
2d41a 2c 0a 20 2f 2a 20 20 38 32 20 2a 2f 20 22 64 65  ,. /*  82 */ "de
2d41b 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
2d41c 3d 20 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45  = NOT DEFERRABLE
2d41d 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
2d41e 72 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 38  red_opt",. /*  8
2d41f 33 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63  3 */ "defer_subc
2d420 6c 61 75 73 65 20 3a 3a 3d 20 44 45 46 45 52 52  lause ::= DEFERR
2d421 41 42 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72  ABLE init_deferr
2d422 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c 0a 20 2f  ed_pred_opt",. /
2d423 2a 20 20 38 34 20 2a 2f 20 22 69 6e 69 74 5f 64  *  84 */ "init_d
2d424 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
2d425 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38 35 20 2a   ::=",. /*  85 *
2d426 2f 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64  / "init_deferred
2d427 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e  _pred_opt ::= IN
2d428 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44  ITIALLY DEFERRED
2d429 22 2c 0a 20 2f 2a 20 20 38 36 20 2a 2f 20 22 69  ",. /*  86 */ "i
2d42a 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
2d42b 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41  d_opt ::= INITIA
2d42c 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a  LLY IMMEDIATE",.
2d42d 20 2f 2a 20 20 38 37 20 2a 2f 20 22 63 6f 6e 73   /*  87 */ "cons
2d42e 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  list_opt ::=",. 
2d42f 2f 2a 20 20 38 38 20 2a 2f 20 22 63 6f 6e 73 6c  /*  88 */ "consl
2d430 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d  ist_opt ::= COMM
2d431 41 20 63 6f 6e 73 6c 69 73 74 22 2c 0a 20 2f 2a  A conslist",. /*
2d432 20 20 38 39 20 2a 2f 20 22 63 6f 6e 73 6c 69 73    89 */ "conslis
2d433 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43  t ::= conslist C
2d434 4f 4d 4d 41 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a  OMMA tcons",. /*
2d435 20 20 39 30 20 2a 2f 20 22 63 6f 6e 73 6c 69 73    90 */ "conslis
2d436 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74  t ::= conslist t
2d437 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39 31 20 2a  cons",. /*  91 *
2d438 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20  / "conslist ::= 
2d439 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39 32 20  tcons",. /*  92 
2d43a 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f  */ "tcons ::= CO
2d43b 4e 53 54 52 41 49 4e 54 20 6e 6d 22 2c 0a 20 2f  NSTRAINT nm",. /
2d43c 2a 20 20 39 33 20 2a 2f 20 22 74 63 6f 6e 73 20  *  93 */ "tcons 
2d43d 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ::= PRIMARY KEY 
2d43e 4c 50 20 69 64 78 6c 69 73 74 20 61 75 74 6f 69  LP idxlist autoi
2d43f 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20  nc RP onconf",. 
2d440 2f 2a 20 20 39 34 20 2a 2f 20 22 74 63 6f 6e 73  /*  94 */ "tcons
2d441 20 3a 3a 3d 20 55 4e 49 51 55 45 20 4c 50 20 69   ::= UNIQUE LP i
2d442 64 78 6c 69 73 74 20 52 50 20 6f 6e 63 6f 6e 66  dxlist RP onconf
2d443 22 2c 0a 20 2f 2a 20 20 39 35 20 2a 2f 20 22 74  ",. /*  95 */ "t
2d444 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c  cons ::= CHECK L
2d445 50 20 65 78 70 72 20 52 50 20 6f 6e 63 6f 6e 66  P expr RP onconf
2d446 22 2c 0a 20 2f 2a 20 20 39 36 20 2a 2f 20 22 74  ",. /*  96 */ "t
2d447 63 6f 6e 73 20 3a 3a 3d 20 46 4f 52 45 49 47 4e  cons ::= FOREIGN
2d448 20 4b 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20   KEY LP idxlist 
2d449 52 50 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d  RP REFERENCES nm
2d44a 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66   idxlist_opt ref
2d44b 61 72 67 73 20 64 65 66 65 72 5f 73 75 62 63 6c  args defer_subcl
2d44c 61 75 73 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20  ause_opt",. /*  
2d44d 39 37 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62  97 */ "defer_sub
2d44e 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 22 2c  clause_opt ::=",
2d44f 0a 20 2f 2a 20 20 39 38 20 2a 2f 20 22 64 65 66  . /*  98 */ "def
2d450 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
2d451 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c   ::= defer_subcl
2d452 61 75 73 65 22 2c 0a 20 2f 2a 20 20 39 39 20 2a  ause",. /*  99 *
2d453 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 22 2c 0a  / "onconf ::=",.
2d454 20 2f 2a 20 31 30 30 20 2a 2f 20 22 6f 6e 63 6f   /* 100 */ "onco
2d455 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49  nf ::= ON CONFLI
2d456 43 54 20 72 65 73 6f 6c 76 65 74 79 70 65 22 2c  CT resolvetype",
2d457 0a 20 2f 2a 20 31 30 31 20 2a 2f 20 22 6f 72 63  . /* 101 */ "orc
2d458 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30  onf ::=",. /* 10
2d459 32 20 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d  2 */ "orconf ::=
2d45a 20 4f 52 20 72 65 73 6f 6c 76 65 74 79 70 65 22   OR resolvetype"
2d45b 2c 0a 20 2f 2a 20 31 30 33 20 2a 2f 20 22 72 65  ,. /* 103 */ "re
2d45c 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 72 61  solvetype ::= ra
2d45d 69 73 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30  isetype",. /* 10
2d45e 34 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70  4 */ "resolvetyp
2d45f 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 22 2c 0a 20  e ::= IGNORE",. 
2d460 2f 2a 20 31 30 35 20 2a 2f 20 22 72 65 73 6f 6c  /* 105 */ "resol
2d461 76 65 74 79 70 65 20 3a 3a 3d 20 52 45 50 4c 41  vetype ::= REPLA
2d462 43 45 22 2c 0a 20 2f 2a 20 31 30 36 20 2a 2f 20  CE",. /* 106 */ 
2d463 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41  "cmd ::= DROP TA
2d464 42 4c 45 20 69 66 65 78 69 73 74 73 20 66 75 6c  BLE ifexists ful
2d465 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31 30 37 20  lname",. /* 107 
2d466 2a 2f 20 22 69 66 65 78 69 73 74 73 20 3a 3a 3d  */ "ifexists ::=
2d467 20 49 46 20 45 58 49 53 54 53 22 2c 0a 20 2f 2a   IF EXISTS",. /*
2d468 20 31 30 38 20 2a 2f 20 22 69 66 65 78 69 73 74   108 */ "ifexist
2d469 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 39 20  s ::=",. /* 109 
2d46a 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61  */ "cmd ::= crea
2d46b 74 65 6b 77 20 74 65 6d 70 20 56 49 45 57 20 69  tekw temp VIEW i
2d46c 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62  fnotexists nm db
2d46d 6e 6d 20 41 53 20 73 65 6c 65 63 74 22 2c 0a 20  nm AS select",. 
2d46e 2f 2a 20 31 31 30 20 2a 2f 20 22 63 6d 64 20 3a  /* 110 */ "cmd :
2d46f 3a 3d 20 44 52 4f 50 20 56 49 45 57 20 69 66 65  := DROP VIEW ife
2d470 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c  xists fullname",
2d471 0a 20 2f 2a 20 31 31 31 20 2a 2f 20 22 63 6d 64  . /* 111 */ "cmd
2d472 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f   ::= select",. /
2d473 2a 20 31 31 32 20 2a 2f 20 22 73 65 6c 65 63 74  * 112 */ "select
2d474 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 22 2c   ::= oneselect",
2d475 0a 20 2f 2a 20 31 31 33 20 2a 2f 20 22 73 65 6c  . /* 113 */ "sel
2d476 65 63 74 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d  ect ::= select m
2d477 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e  ultiselect_op on
2d478 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31  eselect",. /* 11
2d479 34 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63  4 */ "multiselec
2d47a 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 22 2c  t_op ::= UNION",
2d47b 0a 20 2f 2a 20 31 31 35 20 2a 2f 20 22 6d 75 6c  . /* 115 */ "mul
2d47c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20  tiselect_op ::= 
2d47d 55 4e 49 4f 4e 20 41 4c 4c 22 2c 0a 20 2f 2a 20  UNION ALL",. /* 
2d47e 31 31 36 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c  116 */ "multisel
2d47f 65 63 74 5f 6f 70 20 3a 3a 3d 20 45 58 43 45 50  ect_op ::= EXCEP
2d480 54 7c 49 4e 54 45 52 53 45 43 54 22 2c 0a 20 2f  T|INTERSECT",. /
2d481 2a 20 31 31 37 20 2a 2f 20 22 6f 6e 65 73 65 6c  * 117 */ "onesel
2d482 65 63 74 20 3a 3a 3d 20 53 45 4c 45 43 54 20 64  ect ::= SELECT d
2d483 69 73 74 69 6e 63 74 20 73 65 6c 63 6f 6c 6c 69  istinct selcolli
2d484 73 74 20 66 72 6f 6d 20 77 68 65 72 65 5f 6f 70  st from where_op
2d485 74 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 68 61  t groupby_opt ha
2d486 76 69 6e 67 5f 6f 70 74 20 6f 72 64 65 72 62 79  ving_opt orderby
2d487 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70 74 22 2c  _opt limit_opt",
2d488 0a 20 2f 2a 20 31 31 38 20 2a 2f 20 22 64 69 73  . /* 118 */ "dis
2d489 74 69 6e 63 74 20 3a 3a 3d 20 44 49 53 54 49 4e  tinct ::= DISTIN
2d48a 43 54 22 2c 0a 20 2f 2a 20 31 31 39 20 2a 2f 20  CT",. /* 119 */ 
2d48b 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 41 4c  "distinct ::= AL
2d48c 4c 22 2c 0a 20 2f 2a 20 31 32 30 20 2a 2f 20 22  L",. /* 120 */ "
2d48d 64 69 73 74 69 6e 63 74 20 3a 3a 3d 22 2c 0a 20  distinct ::=",. 
2d48e 2f 2a 20 31 32 31 20 2a 2f 20 22 73 63 6c 70 20  /* 121 */ "sclp 
2d48f 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73 74 20 43  ::= selcollist C
2d490 4f 4d 4d 41 22 2c 0a 20 2f 2a 20 31 32 32 20 2a  OMMA",. /* 122 *
2d491 2f 20 22 73 63 6c 70 20 3a 3a 3d 22 2c 0a 20 2f  / "sclp ::=",. /
2d492 2a 20 31 32 33 20 2a 2f 20 22 73 65 6c 63 6f 6c  * 123 */ "selcol
2d493 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 65 78  list ::= sclp ex
2d494 70 72 20 61 73 22 2c 0a 20 2f 2a 20 31 32 34 20  pr as",. /* 124 
2d495 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a  */ "selcollist :
2d496 3a 3d 20 73 63 6c 70 20 53 54 41 52 22 2c 0a 20  := sclp STAR",. 
2d497 2f 2a 20 31 32 35 20 2a 2f 20 22 73 65 6c 63 6f  /* 125 */ "selco
2d498 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 6e  llist ::= sclp n
2d499 6d 20 44 4f 54 20 53 54 41 52 22 2c 0a 20 2f 2a  m DOT STAR",. /*
2d49a 20 31 32 36 20 2a 2f 20 22 61 73 20 3a 3a 3d 20   126 */ "as ::= 
2d49b 41 53 20 6e 6d 22 2c 0a 20 2f 2a 20 31 32 37 20  AS nm",. /* 127 
2d49c 2a 2f 20 22 61 73 20 3a 3a 3d 20 69 64 73 22 2c  */ "as ::= ids",
2d49d 0a 20 2f 2a 20 31 32 38 20 2a 2f 20 22 61 73 20  . /* 128 */ "as 
2d49e 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 39 20 2a 2f  ::=",. /* 129 */
2d49f 20 22 66 72 6f 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a   "from ::=",. /*
2d4a0 20 31 33 30 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a   130 */ "from ::
2d4a1 3d 20 46 52 4f 4d 20 73 65 6c 74 61 62 6c 69 73  = FROM seltablis
2d4a2 74 22 2c 0a 20 2f 2a 20 31 33 31 20 2a 2f 20 22  t",. /* 131 */ "
2d4a3 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20 73  stl_prefix ::= s
2d4a4 65 6c 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f 70  eltablist joinop
2d4a5 22 2c 0a 20 2f 2a 20 31 33 32 20 2a 2f 20 22 73  ",. /* 132 */ "s
2d4a6 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 22 2c 0a  tl_prefix ::=",.
2d4a7 20 2f 2a 20 31 33 33 20 2a 2f 20 22 73 65 6c 74   /* 133 */ "selt
2d4a8 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70  ablist ::= stl_p
2d4a9 72 65 66 69 78 20 6e 6d 20 64 62 6e 6d 20 61 73  refix nm dbnm as
2d4aa 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 6f 6e 5f   indexed_opt on_
2d4ab 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a  opt using_opt",.
2d4ac 20 2f 2a 20 31 33 34 20 2a 2f 20 22 73 65 6c 74   /* 134 */ "selt
2d4ad 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70  ablist ::= stl_p
2d4ae 72 65 66 69 78 20 4c 50 20 73 65 6c 65 63 74 20  refix LP select 
2d4af 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69  RP as on_opt usi
2d4b0 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 35  ng_opt",. /* 135
2d4b1 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20   */ "seltablist 
2d4b2 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c  ::= stl_prefix L
2d4b3 50 20 73 65 6c 74 61 62 6c 69 73 74 20 52 50 20  P seltablist RP 
2d4b4 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f  as on_opt using_
2d4b5 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 36 20 2a 2f  opt",. /* 136 */
2d4b6 20 22 64 62 6e 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a   "dbnm ::=",. /*
2d4b7 20 31 33 37 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a   137 */ "dbnm ::
2d4b8 3d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31  = DOT nm",. /* 1
2d4b9 33 38 20 2a 2f 20 22 66 75 6c 6c 6e 61 6d 65 20  38 */ "fullname 
2d4ba 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f  ::= nm dbnm",. /
2d4bb 2a 20 31 33 39 20 2a 2f 20 22 6a 6f 69 6e 6f 70  * 139 */ "joinop
2d4bc 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 22   ::= COMMA|JOIN"
2d4bd 2c 0a 20 2f 2a 20 31 34 30 20 2a 2f 20 22 6a 6f  ,. /* 140 */ "jo
2d4be 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57  inop ::= JOIN_KW
2d4bf 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 31 20   JOIN",. /* 141 
2d4c0 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a  */ "joinop ::= J
2d4c1 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 22 2c  OIN_KW nm JOIN",
2d4c2 0a 20 2f 2a 20 31 34 32 20 2a 2f 20 22 6a 6f 69  . /* 142 */ "joi
2d4c3 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20  nop ::= JOIN_KW 
2d4c4 6e 6d 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a  nm nm JOIN",. /*
2d4c5 20 31 34 33 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20   143 */ "on_opt 
2d4c6 3a 3a 3d 20 4f 4e 20 65 78 70 72 22 2c 0a 20 2f  ::= ON expr",. /
2d4c7 2a 20 31 34 34 20 2a 2f 20 22 6f 6e 5f 6f 70 74  * 144 */ "on_opt
2d4c8 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 35 20 2a   ::=",. /* 145 *
2d4c9 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a  / "indexed_opt :
2d4ca 3a 3d 22 2c 0a 20 2f 2a 20 31 34 36 20 2a 2f 20  :=",. /* 146 */ 
2d4cb 22 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d  "indexed_opt ::=
2d4cc 20 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 22 2c   INDEXED BY nm",
2d4cd 0a 20 2f 2a 20 31 34 37 20 2a 2f 20 22 69 6e 64  . /* 147 */ "ind
2d4ce 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54  exed_opt ::= NOT
2d4cf 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 31   INDEXED",. /* 1
2d4d0 34 38 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74  48 */ "using_opt
2d4d1 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69 6e   ::= USING LP in
2d4d2 73 63 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f  scollist RP",. /
2d4d3 2a 20 31 34 39 20 2a 2f 20 22 75 73 69 6e 67 5f  * 149 */ "using_
2d4d4 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35  opt ::=",. /* 15
2d4d5 30 20 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70  0 */ "orderby_op
2d4d6 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 31 20  t ::=",. /* 151 
2d4d7 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20  */ "orderby_opt 
2d4d8 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20 73 6f 72  ::= ORDER BY sor
2d4d9 74 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 35 32 20  tlist",. /* 152 
2d4da 2a 2f 20 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d  */ "sortlist ::=
2d4db 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20   sortlist COMMA 
2d4dc 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64  sortitem sortord
2d4dd 65 72 22 2c 0a 20 2f 2a 20 31 35 33 20 2a 2f 20  er",. /* 153 */ 
2d4de 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f  "sortlist ::= so
2d4df 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72  rtitem sortorder
2d4e0 22 2c 0a 20 2f 2a 20 31 35 34 20 2a 2f 20 22 73  ",. /* 154 */ "s
2d4e1 6f 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72  ortitem ::= expr
2d4e2 22 2c 0a 20 2f 2a 20 31 35 35 20 2a 2f 20 22 73  ",. /* 155 */ "s
2d4e3 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43  ortorder ::= ASC
2d4e4 22 2c 0a 20 2f 2a 20 31 35 36 20 2a 2f 20 22 73  ",. /* 156 */ "s
2d4e5 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53  ortorder ::= DES
2d4e6 43 22 2c 0a 20 2f 2a 20 31 35 37 20 2a 2f 20 22  C",. /* 157 */ "
2d4e7 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 22 2c 0a  sortorder ::=",.
2d4e8 20 2f 2a 20 31 35 38 20 2a 2f 20 22 67 72 6f 75   /* 158 */ "grou
2d4e9 70 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  pby_opt ::=",. /
2d4ea 2a 20 31 35 39 20 2a 2f 20 22 67 72 6f 75 70 62  * 159 */ "groupb
2d4eb 79 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f 55 50 20  y_opt ::= GROUP 
2d4ec 42 59 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a 20  BY nexprlist",. 
2d4ed 2f 2a 20 31 36 30 20 2a 2f 20 22 68 61 76 69 6e  /* 160 */ "havin
2d4ee 67 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  g_opt ::=",. /* 
2d4ef 31 36 31 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f  161 */ "having_o
2d4f0 70 74 20 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78  pt ::= HAVING ex
2d4f1 70 72 22 2c 0a 20 2f 2a 20 31 36 32 20 2a 2f 20  pr",. /* 162 */ 
2d4f2 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 22 2c  "limit_opt ::=",
2d4f3 0a 20 2f 2a 20 31 36 33 20 2a 2f 20 22 6c 69 6d  . /* 163 */ "lim
2d4f4 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
2d4f5 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 34 20   expr",. /* 164 
2d4f6 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a  */ "limit_opt ::
2d4f7 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46  = LIMIT expr OFF
2d4f8 53 45 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  SET expr",. /* 1
2d4f9 36 35 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74  65 */ "limit_opt
2d4fa 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20   ::= LIMIT expr 
2d4fb 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a  COMMA expr",. /*
2d4fc 20 31 36 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   166 */ "cmd ::=
2d4fd 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c   DELETE FROM ful
2d4fe 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70  lname indexed_op
2d4ff 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f  t where_opt",. /
2d500 2a 20 31 36 37 20 2a 2f 20 22 77 68 65 72 65 5f  * 167 */ "where_
2d501 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36  opt ::=",. /* 16
2d502 38 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20  8 */ "where_opt 
2d503 3a 3a 3d 20 57 48 45 52 45 20 65 78 70 72 22 2c  ::= WHERE expr",
2d504 0a 20 2f 2a 20 31 36 39 20 2a 2f 20 22 63 6d 64  . /* 169 */ "cmd
2d505 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f   ::= UPDATE orco
2d506 6e 66 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65  nf fullname inde
2d507 78 65 64 5f 6f 70 74 20 53 45 54 20 73 65 74 6c  xed_opt SET setl
2d508 69 73 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a  ist where_opt",.
2d509 20 2f 2a 20 31 37 30 20 2a 2f 20 22 73 65 74 6c   /* 170 */ "setl
2d50a 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20  ist ::= setlist 
2d50b 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72  COMMA nm EQ expr
2d50c 22 2c 0a 20 2f 2a 20 31 37 31 20 2a 2f 20 22 73  ",. /* 171 */ "s
2d50d 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45 51  etlist ::= nm EQ
2d50e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 32 20   expr",. /* 172 
2d50f 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  */ "cmd ::= inse
2d510 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
2d511 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
2d512 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74  opt VALUES LP it
2d513 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  emlist RP",. /* 
2d514 31 37 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  173 */ "cmd ::= 
2d515 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
2d516 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c  fullname inscoll
2d517 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 22 2c  ist_opt select",
2d518 0a 20 2f 2a 20 31 37 34 20 2a 2f 20 22 63 6d 64  . /* 174 */ "cmd
2d519 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
2d51a 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e  INTO fullname in
2d51b 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46  scollist_opt DEF
2d51c 41 55 4c 54 20 56 41 4c 55 45 53 22 2c 0a 20 2f  AULT VALUES",. /
2d51d 2a 20 31 37 35 20 2a 2f 20 22 69 6e 73 65 72 74  * 175 */ "insert
2d51e 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53 45 52 54 20  _cmd ::= INSERT 
2d51f 6f 72 63 6f 6e 66 22 2c 0a 20 2f 2a 20 31 37 36  orconf",. /* 176
2d520 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64 20   */ "insert_cmd 
2d521 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f  ::= REPLACE",. /
2d522 2a 20 31 37 37 20 2a 2f 20 22 69 74 65 6d 6c 69  * 177 */ "itemli
2d523 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69 73 74 20  st ::= itemlist 
2d524 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a  COMMA expr",. /*
2d525 20 31 37 38 20 2a 2f 20 22 69 74 65 6d 6c 69 73   178 */ "itemlis
2d526 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a  t ::= expr",. /*
2d527 20 31 37 39 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c   179 */ "inscoll
2d528 69 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ist_opt ::=",. /
2d529 2a 20 31 38 30 20 2a 2f 20 22 69 6e 73 63 6f 6c  * 180 */ "inscol
2d52a 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20  list_opt ::= LP 
2d52b 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 22 2c 0a  inscollist RP",.
2d52c 20 2f 2a 20 31 38 31 20 2a 2f 20 22 69 6e 73 63   /* 181 */ "insc
2d52d 6f 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f  ollist ::= insco
2d52e 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 22 2c  llist COMMA nm",
2d52f 0a 20 2f 2a 20 31 38 32 20 2a 2f 20 22 69 6e 73  . /* 182 */ "ins
2d530 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e 6d 22 2c  collist ::= nm",
2d531 0a 20 2f 2a 20 31 38 33 20 2a 2f 20 22 65 78 70  . /* 183 */ "exp
2d532 72 20 3a 3a 3d 20 74 65 72 6d 22 2c 0a 20 2f 2a  r ::= term",. /*
2d533 20 31 38 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a   184 */ "expr ::
2d534 3d 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  = LP expr RP",. 
2d535 2f 2a 20 31 38 35 20 2a 2f 20 22 74 65 72 6d 20  /* 185 */ "term 
2d536 3a 3a 3d 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 31  ::= NULL",. /* 1
2d537 38 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  86 */ "expr ::= 
2d538 69 64 22 2c 0a 20 2f 2a 20 31 38 37 20 2a 2f 20  id",. /* 187 */ 
2d539 22 65 78 70 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  "expr ::= JOIN_K
2d53a 57 22 2c 0a 20 2f 2a 20 31 38 38 20 2a 2f 20 22  W",. /* 188 */ "
2d53b 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20  expr ::= nm DOT 
2d53c 6e 6d 22 2c 0a 20 2f 2a 20 31 38 39 20 2a 2f 20  nm",. /* 189 */ 
2d53d 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54  "expr ::= nm DOT
2d53e 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a   nm DOT nm",. /*
2d53f 20 31 39 30 20 2a 2f 20 22 74 65 72 6d 20 3a 3a   190 */ "term ::
2d540 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c  = INTEGER|FLOAT|
2d541 42 4c 4f 42 22 2c 0a 20 2f 2a 20 31 39 31 20 2a  BLOB",. /* 191 *
2d542 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 53 54 52 49  / "term ::= STRI
2d543 4e 47 22 2c 0a 20 2f 2a 20 31 39 32 20 2a 2f 20  NG",. /* 192 */ 
2d544 22 65 78 70 72 20 3a 3a 3d 20 52 45 47 49 53 54  "expr ::= REGIST
2d545 45 52 22 2c 0a 20 2f 2a 20 31 39 33 20 2a 2f 20  ER",. /* 193 */ 
2d546 22 65 78 70 72 20 3a 3a 3d 20 56 41 52 49 41 42  "expr ::= VARIAB
2d547 4c 45 22 2c 0a 20 2f 2a 20 31 39 34 20 2a 2f 20  LE",. /* 194 */ 
2d548 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43  "expr ::= expr C
2d549 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a  OLLATE ids",. /*
2d54a 20 31 39 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a   195 */ "expr ::
2d54b 3d 20 43 41 53 54 20 4c 50 20 65 78 70 72 20 41  = CAST LP expr A
2d54c 53 20 74 79 70 65 74 6f 6b 65 6e 20 52 50 22 2c  S typetoken RP",
2d54d 0a 20 2f 2a 20 31 39 36 20 2a 2f 20 22 65 78 70  . /* 196 */ "exp
2d54e 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73 74  r ::= ID LP dist
2d54f 69 6e 63 74 20 65 78 70 72 6c 69 73 74 20 52 50  inct exprlist RP
2d550 22 2c 0a 20 2f 2a 20 31 39 37 20 2a 2f 20 22 65  ",. /* 197 */ "e
2d551 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54  xpr ::= ID LP ST
2d552 41 52 20 52 50 22 2c 0a 20 2f 2a 20 31 39 38 20  AR RP",. /* 198 
2d553 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 43 54 49  */ "term ::= CTI
2d554 4d 45 5f 4b 57 22 2c 0a 20 2f 2a 20 31 39 39 20  ME_KW",. /* 199 
2d555 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
2d556 72 20 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a  r AND expr",. /*
2d557 20 32 30 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a   200 */ "expr ::
2d558 3d 20 65 78 70 72 20 4f 52 20 65 78 70 72 22 2c  = expr OR expr",
2d559 0a 20 2f 2a 20 32 30 31 20 2a 2f 20 22 65 78 70  . /* 201 */ "exp
2d55a 72 20 3a 3a 3d 20 65 78 70 72 20 4c 54 7c 47 54  r ::= expr LT|GT
2d55b 7c 47 45 7c 4c 45 20 65 78 70 72 22 2c 0a 20 2f  |GE|LE expr",. /
2d55c 2a 20 32 30 32 20 2a 2f 20 22 65 78 70 72 20 3a  * 202 */ "expr :
2d55d 3a 3d 20 65 78 70 72 20 45 51 7c 4e 45 20 65 78  := expr EQ|NE ex
2d55e 70 72 22 2c 0a 20 2f 2a 20 32 30 33 20 2a 2f 20  pr",. /* 203 */ 
2d55f 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42  "expr ::= expr B
2d560 49 54 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49  ITAND|BITOR|LSHI
2d561 46 54 7c 52 53 48 49 46 54 20 65 78 70 72 22 2c  FT|RSHIFT expr",
2d562 0a 20 2f 2a 20 32 30 34 20 2a 2f 20 22 65 78 70  . /* 204 */ "exp
2d563 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c  r ::= expr PLUS|
2d564 4d 49 4e 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a  MINUS expr",. /*
2d565 20 32 30 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a   205 */ "expr ::
2d566 3d 20 65 78 70 72 20 53 54 41 52 7c 53 4c 41 53  = expr STAR|SLAS
2d567 48 7c 52 45 4d 20 65 78 70 72 22 2c 0a 20 2f 2a  H|REM expr",. /*
2d568 20 32 30 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a   206 */ "expr ::
2d569 3d 20 65 78 70 72 20 43 4f 4e 43 41 54 20 65 78  = expr CONCAT ex
2d56a 70 72 22 2c 0a 20 2f 2a 20 32 30 37 20 2a 2f 20  pr",. /* 207 */ 
2d56b 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45  "likeop ::= LIKE
2d56c 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 38 20 2a 2f  _KW",. /* 208 */
2d56d 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54   "likeop ::= NOT
2d56e 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32   LIKE_KW",. /* 2
2d56f 30 39 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a  09 */ "likeop ::
2d570 3d 20 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32 31  = MATCH",. /* 21
2d571 30 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d  0 */ "likeop ::=
2d572 20 4e 4f 54 20 4d 41 54 43 48 22 2c 0a 20 2f 2a   NOT MATCH",. /*
2d573 20 32 31 31 20 2a 2f 20 22 65 73 63 61 70 65 20   211 */ "escape 
2d574 3a 3a 3d 20 45 53 43 41 50 45 20 65 78 70 72 22  ::= ESCAPE expr"
2d575 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f 20 22 65 73  ,. /* 212 */ "es
2d576 63 61 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  cape ::=",. /* 2
2d577 31 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  13 */ "expr ::= 
2d578 65 78 70 72 20 6c 69 6b 65 6f 70 20 65 78 70 72  expr likeop expr
2d579 20 65 73 63 61 70 65 22 2c 0a 20 2f 2a 20 32 31   escape",. /* 21
2d57a 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  4 */ "expr ::= e
2d57b 78 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55  xpr ISNULL|NOTNU
2d57c 4c 4c 22 2c 0a 20 2f 2a 20 32 31 35 20 2a 2f 20  LL",. /* 215 */ 
2d57d 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49  "expr ::= expr I
2d57e 53 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 36  S NULL",. /* 216
2d57f 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2d580 70 72 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f  pr NOT NULL",. /
2d581 2a 20 32 31 37 20 2a 2f 20 22 65 78 70 72 20 3a  * 217 */ "expr :
2d582 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f 54 20 4e  := expr IS NOT N
2d583 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 38 20 2a 2f  ULL",. /* 218 */
2d584 20 22 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65   "expr ::= NOT e
2d585 78 70 72 22 2c 0a 20 2f 2a 20 32 31 39 20 2a 2f  xpr",. /* 219 */
2d586 20 22 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f   "expr ::= BITNO
2d587 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 30  T expr",. /* 220
2d588 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4d 49   */ "expr ::= MI
2d589 4e 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  NUS expr",. /* 2
2d58a 32 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  21 */ "expr ::= 
2d58b 50 4c 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20  PLUS expr",. /* 
2d58c 32 32 32 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f  222 */ "between_
2d58d 6f 70 20 3a 3a 3d 20 42 45 54 57 45 45 4e 22 2c  op ::= BETWEEN",
2d58e 0a 20 2f 2a 20 32 32 33 20 2a 2f 20 22 62 65 74  . /* 223 */ "bet
2d58f 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20  ween_op ::= NOT 
2d590 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32 32  BETWEEN",. /* 22
2d591 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  4 */ "expr ::= e
2d592 78 70 72 20 62 65 74 77 65 65 6e 5f 6f 70 20 65  xpr between_op e
2d593 78 70 72 20 41 4e 44 20 65 78 70 72 22 2c 0a 20  xpr AND expr",. 
2d594 2f 2a 20 32 32 35 20 2a 2f 20 22 69 6e 5f 6f 70  /* 225 */ "in_op
2d595 20 3a 3a 3d 20 49 4e 22 2c 0a 20 2f 2a 20 32 32   ::= IN",. /* 22
2d596 36 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20  6 */ "in_op ::= 
2d597 4e 4f 54 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 37  NOT IN",. /* 227
2d598 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2d599 70 72 20 69 6e 5f 6f 70 20 4c 50 20 65 78 70 72  pr in_op LP expr
2d59a 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32  list RP",. /* 22
2d59b 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c  8 */ "expr ::= L
2d59c 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f  P select RP",. /
2d59d 2a 20 32 32 39 20 2a 2f 20 22 65 78 70 72 20 3a  * 229 */ "expr :
2d59e 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50  := expr in_op LP
2d59f 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a   select RP",. /*
2d5a0 20 32 33 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a   230 */ "expr ::
2d5a1 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20  = expr in_op nm 
2d5a2 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32 33 31 20 2a  dbnm",. /* 231 *
2d5a3 2f 20 22 65 78 70 72 20 3a 3a 3d 20 45 58 49 53  / "expr ::= EXIS
2d5a4 54 53 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22  TS LP select RP"
2d5a5 2c 0a 20 2f 2a 20 32 33 32 20 2a 2f 20 22 65 78  ,. /* 232 */ "ex
2d5a6 70 72 20 3a 3a 3d 20 43 41 53 45 20 63 61 73 65  pr ::= CASE case
2d5a7 5f 6f 70 65 72 61 6e 64 20 63 61 73 65 5f 65 78  _operand case_ex
2d5a8 70 72 6c 69 73 74 20 63 61 73 65 5f 65 6c 73 65  prlist case_else
2d5a9 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 33 33 20 2a   END",. /* 233 *
2d5aa 2f 20 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74  / "case_exprlist
2d5ab 20 3a 3a 3d 20 63 61 73 65 5f 65 78 70 72 6c 69   ::= case_exprli
2d5ac 73 74 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  st WHEN expr THE
2d5ad 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 34  N expr",. /* 234
2d5ae 20 2a 2f 20 22 63 61 73 65 5f 65 78 70 72 6c 69   */ "case_exprli
2d5af 73 74 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72  st ::= WHEN expr
2d5b0 20 54 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a   THEN expr",. /*
2d5b1 20 32 33 35 20 2a 2f 20 22 63 61 73 65 5f 65 6c   235 */ "case_el
2d5b2 73 65 20 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72  se ::= ELSE expr
2d5b3 22 2c 0a 20 2f 2a 20 32 33 36 20 2a 2f 20 22 63  ",. /* 236 */ "c
2d5b4 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 22 2c 0a 20  ase_else ::=",. 
2d5b5 2f 2a 20 32 33 37 20 2a 2f 20 22 63 61 73 65 5f  /* 237 */ "case_
2d5b6 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70 72  operand ::= expr
2d5b7 22 2c 0a 20 2f 2a 20 32 33 38 20 2a 2f 20 22 63  ",. /* 238 */ "c
2d5b8 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 22  ase_operand ::="
2d5b9 2c 0a 20 2f 2a 20 32 33 39 20 2a 2f 20 22 65 78  ,. /* 239 */ "ex
2d5ba 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72  prlist ::= nexpr
2d5bb 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 34 30 20 2a  list",. /* 240 *
2d5bc 2f 20 22 65 78 70 72 6c 69 73 74 20 3a 3a 3d 22  / "exprlist ::="
2d5bd 2c 0a 20 2f 2a 20 32 34 31 20 2a 2f 20 22 6e 65  ,. /* 241 */ "ne
2d5be 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70  xprlist ::= nexp
2d5bf 72 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72  rlist COMMA expr
2d5c0 22 2c 0a 20 2f 2a 20 32 34 32 20 2a 2f 20 22 6e  ",. /* 242 */ "n
2d5c1 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 65 78 70  exprlist ::= exp
2d5c2 72 22 2c 0a 20 2f 2a 20 32 34 33 20 2a 2f 20 22  r",. /* 243 */ "
2d5c3 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  cmd ::= createkw
2d5c4 20 75 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45   uniqueflag INDE
2d5c5 58 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  X ifnotexists nm
2d5c6 20 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69   dbnm ON nm LP i
2d5c7 64 78 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  dxlist RP",. /* 
2d5c8 32 34 34 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c  244 */ "uniquefl
2d5c9 61 67 20 3a 3a 3d 20 55 4e 49 51 55 45 22 2c 0a  ag ::= UNIQUE",.
2d5ca 20 2f 2a 20 32 34 35 20 2a 2f 20 22 75 6e 69 71   /* 245 */ "uniq
2d5cb 75 65 66 6c 61 67 20 3a 3a 3d 22 2c 0a 20 2f 2a  ueflag ::=",. /*
2d5cc 20 32 34 36 20 2a 2f 20 22 69 64 78 6c 69 73 74   246 */ "idxlist
2d5cd 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  _opt ::=",. /* 2
2d5ce 34 37 20 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f  47 */ "idxlist_o
2d5cf 70 74 20 3a 3a 3d 20 4c 50 20 69 64 78 6c 69 73  pt ::= LP idxlis
2d5d0 74 20 52 50 22 2c 0a 20 2f 2a 20 32 34 38 20 2a  t RP",. /* 248 *
2d5d1 2f 20 22 69 64 78 6c 69 73 74 20 3a 3a 3d 20 69  / "idxlist ::= i
2d5d2 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20  dxlist COMMA nm 
2d5d3 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65  collate sortorde
2d5d4 72 22 2c 0a 20 2f 2a 20 32 34 39 20 2a 2f 20 22  r",. /* 249 */ "
2d5d5 69 64 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 63  idxlist ::= nm c
2d5d6 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72  ollate sortorder
2d5d7 22 2c 0a 20 2f 2a 20 32 35 30 20 2a 2f 20 22 63  ",. /* 250 */ "c
2d5d8 6f 6c 6c 61 74 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  ollate ::=",. /*
2d5d9 20 32 35 31 20 2a 2f 20 22 63 6f 6c 6c 61 74 65   251 */ "collate
2d5da 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73   ::= COLLATE ids
2d5db 22 2c 0a 20 2f 2a 20 32 35 32 20 2a 2f 20 22 63  ",. /* 252 */ "c
2d5dc 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45  md ::= DROP INDE
2d5dd 58 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  X ifexists fulln
2d5de 61 6d 65 22 2c 0a 20 2f 2a 20 32 35 33 20 2a 2f  ame",. /* 253 */
2d5df 20 22 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d   "cmd ::= VACUUM
2d5e0 22 2c 0a 20 2f 2a 20 32 35 34 20 2a 2f 20 22 63  ",. /* 254 */ "c
2d5e1 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d  md ::= VACUUM nm
2d5e2 22 2c 0a 20 2f 2a 20 32 35 35 20 2a 2f 20 22 63  ",. /* 255 */ "c
2d5e3 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
2d5e4 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c   dbnm EQ nmnum",
2d5e5 0a 20 2f 2a 20 32 35 36 20 2a 2f 20 22 63 6d 64  . /* 256 */ "cmd
2d5e6 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
2d5e7 62 6e 6d 20 45 51 20 4f 4e 22 2c 0a 20 2f 2a 20  bnm EQ ON",. /* 
2d5e8 32 35 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  257 */ "cmd ::= 
2d5e9 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45  PRAGMA nm dbnm E
2d5ea 51 20 44 45 4c 45 54 45 22 2c 0a 20 2f 2a 20 32  Q DELETE",. /* 2
2d5eb 35 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  58 */ "cmd ::= P
2d5ec 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
2d5ed 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a   minus_num",. /*
2d5ee 20 32 35 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   259 */ "cmd ::=
2d5ef 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
2d5f0 4c 50 20 6e 6d 6e 75 6d 20 52 50 22 2c 0a 20 2f  LP nmnum RP",. /
2d5f1 2a 20 32 36 30 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 260 */ "cmd ::
2d5f2 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
2d5f3 22 2c 0a 20 2f 2a 20 32 36 31 20 2a 2f 20 22 6e  ",. /* 261 */ "n
2d5f4 6d 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75  mnum ::= plus_nu
2d5f5 6d 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22  m",. /* 262 */ "
2d5f6 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20  nmnum ::= nm",. 
2d5f7 2f 2a 20 32 36 33 20 2a 2f 20 22 70 6c 75 73 5f  /* 263 */ "plus_
2d5f8 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74  num ::= plus_opt
2d5f9 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 36   number",. /* 26
2d5fa 34 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20  4 */ "minus_num 
2d5fb 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72  ::= MINUS number
2d5fc 22 2c 0a 20 2f 2a 20 32 36 35 20 2a 2f 20 22 6e  ",. /* 265 */ "n
2d5fd 75 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45  umber ::= INTEGE
2d5fe 52 7c 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 36  R|FLOAT",. /* 26
2d5ff 36 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a  6 */ "plus_opt :
2d600 3a 3d 20 50 4c 55 53 22 2c 0a 20 2f 2a 20 32 36  := PLUS",. /* 26
2d601 37 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a  7 */ "plus_opt :
2d602 3a 3d 22 2c 0a 20 2f 2a 20 32 36 38 20 2a 2f 20  :=",. /* 268 */ 
2d603 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b  "cmd ::= createk
2d604 77 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20 42  w trigger_decl B
2d605 45 47 49 4e 20 74 72 69 67 67 65 72 5f 63 6d 64  EGIN trigger_cmd
2d606 5f 6c 69 73 74 20 45 4e 44 22 2c 0a 20 2f 2a 20  _list END",. /* 
2d607 32 36 39 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  269 */ "trigger_
2d608 64 65 63 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52  decl ::= temp TR
2d609 49 47 47 45 52 20 69 66 6e 6f 74 65 78 69 73 74  IGGER ifnotexist
2d60a 73 20 6e 6d 20 64 62 6e 6d 20 74 72 69 67 67 65  s nm dbnm trigge
2d60b 72 5f 74 69 6d 65 20 74 72 69 67 67 65 72 5f 65  r_time trigger_e
2d60c 76 65 6e 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65  vent ON fullname
2d60d 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20   foreach_clause 
2d60e 77 68 65 6e 5f 63 6c 61 75 73 65 22 2c 0a 20 2f  when_clause",. /
2d60f 2a 20 32 37 30 20 2a 2f 20 22 74 72 69 67 67 65  * 270 */ "trigge
2d610 72 5f 74 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52  r_time ::= BEFOR
2d611 45 22 2c 0a 20 2f 2a 20 32 37 31 20 2a 2f 20 22  E",. /* 271 */ "
2d612 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
2d613 20 41 46 54 45 52 22 2c 0a 20 2f 2a 20 32 37 32   AFTER",. /* 272
2d614 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d   */ "trigger_tim
2d615 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46  e ::= INSTEAD OF
2d616 22 2c 0a 20 2f 2a 20 32 37 33 20 2a 2f 20 22 74  ",. /* 273 */ "t
2d617 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 22  rigger_time ::="
2d618 2c 0a 20 2f 2a 20 32 37 34 20 2a 2f 20 22 74 72  ,. /* 274 */ "tr
2d619 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20  igger_event ::= 
2d61a 44 45 4c 45 54 45 7c 49 4e 53 45 52 54 22 2c 0a  DELETE|INSERT",.
2d61b 20 2f 2a 20 32 37 35 20 2a 2f 20 22 74 72 69 67   /* 275 */ "trig
2d61c 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50  ger_event ::= UP
2d61d 44 41 54 45 22 2c 0a 20 2f 2a 20 32 37 36 20 2a  DATE",. /* 276 *
2d61e 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74  / "trigger_event
2d61f 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f 46 20 69   ::= UPDATE OF i
2d620 6e 73 63 6f 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20  nscollist",. /* 
2d621 32 37 37 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f  277 */ "foreach_
2d622 63 6c 61 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  clause ::=",. /*
2d623 20 32 37 38 20 2a 2f 20 22 66 6f 72 65 61 63 68   278 */ "foreach
2d624 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20  _clause ::= FOR 
2d625 45 41 43 48 20 52 4f 57 22 2c 0a 20 2f 2a 20 32  EACH ROW",. /* 2
2d626 37 39 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75  79 */ "when_clau
2d627 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 30  se ::=",. /* 280
2d628 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65   */ "when_clause
2d629 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 22 2c   ::= WHEN expr",
2d62a 0a 20 2f 2a 20 32 38 31 20 2a 2f 20 22 74 72 69  . /* 281 */ "tri
2d62b 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a  gger_cmd_list ::
2d62c 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  = trigger_cmd_li
2d62d 73 74 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53  st trigger_cmd S
2d62e 45 4d 49 22 2c 0a 20 2f 2a 20 32 38 32 20 2a 2f  EMI",. /* 282 */
2d62f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69   "trigger_cmd_li
2d630 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63  st ::= trigger_c
2d631 6d 64 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 32 38  md SEMI",. /* 28
2d632 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d  3 */ "trigger_cm
2d633 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63  d ::= UPDATE orc
2d634 6f 6e 66 20 6e 6d 20 53 45 54 20 73 65 74 6c 69  onf nm SET setli
2d635 73 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20  st where_opt",. 
2d636 2f 2a 20 32 38 34 20 2a 2f 20 22 74 72 69 67 67  /* 284 */ "trigg
2d637 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72  er_cmd ::= inser
2d638 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69 6e  t_cmd INTO nm in
2d639 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c  scollist_opt VAL
2d63a 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20  UES LP itemlist 
2d63b 52 50 22 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f 20  RP",. /* 285 */ 
2d63c 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d  "trigger_cmd ::=
2d63d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
2d63e 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f   nm inscollist_o
2d63f 70 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  pt select",. /* 
2d640 32 38 36 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  286 */ "trigger_
2d641 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46  cmd ::= DELETE F
2d642 52 4f 4d 20 6e 6d 20 77 68 65 72 65 5f 6f 70 74  ROM nm where_opt
2d643 22 2c 0a 20 2f 2a 20 32 38 37 20 2a 2f 20 22 74  ",. /* 287 */ "t
2d644 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 73  rigger_cmd ::= s
2d645 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32 38 38 20  elect",. /* 288 
2d646 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52 41 49  */ "expr ::= RAI
2d647 53 45 20 4c 50 20 49 47 4e 4f 52 45 20 52 50 22  SE LP IGNORE RP"
2d648 2c 0a 20 2f 2a 20 32 38 39 20 2a 2f 20 22 65 78  ,. /* 289 */ "ex
2d649 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20  pr ::= RAISE LP 
2d64a 72 61 69 73 65 74 79 70 65 20 43 4f 4d 4d 41 20  raisetype COMMA 
2d64b 6e 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 39 30 20  nm RP",. /* 290 
2d64c 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a  */ "raisetype ::
2d64d 3d 20 52 4f 4c 4c 42 41 43 4b 22 2c 0a 20 2f 2a  = ROLLBACK",. /*
2d64e 20 32 39 31 20 2a 2f 20 22 72 61 69 73 65 74 79   291 */ "raisety
2d64f 70 65 20 3a 3a 3d 20 41 42 4f 52 54 22 2c 0a 20  pe ::= ABORT",. 
2d650 2f 2a 20 32 39 32 20 2a 2f 20 22 72 61 69 73 65  /* 292 */ "raise
2d651 74 79 70 65 20 3a 3a 3d 20 46 41 49 4c 22 2c 0a  type ::= FAIL",.
2d652 20 2f 2a 20 32 39 33 20 2a 2f 20 22 63 6d 64 20   /* 293 */ "cmd 
2d653 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47 47 45 52  ::= DROP TRIGGER
2d654 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
2d655 6d 65 22 2c 0a 20 2f 2a 20 32 39 34 20 2a 2f 20  me",. /* 294 */ 
2d656 22 63 6d 64 20 3a 3a 3d 20 41 54 54 41 43 48 20  "cmd ::= ATTACH 
2d657 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
2d658 65 78 70 72 20 41 53 20 65 78 70 72 20 6b 65 79  expr AS expr key
2d659 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 39 35 20 2a  _opt",. /* 295 *
2d65a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 54 41 43  / "cmd ::= DETAC
2d65b 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70  H database_kw_op
2d65c 74 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 39 36  t expr",. /* 296
2d65d 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d   */ "key_opt ::=
2d65e 22 2c 0a 20 2f 2a 20 32 39 37 20 2a 2f 20 22 6b  ",. /* 297 */ "k
2d65f 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45 59 20 65  ey_opt ::= KEY e
2d660 78 70 72 22 2c 0a 20 2f 2a 20 32 39 38 20 2a 2f  xpr",. /* 298 */
2d661 20 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70   "database_kw_op
2d662 74 20 3a 3a 3d 20 44 41 54 41 42 41 53 45 22 2c  t ::= DATABASE",
2d663 0a 20 2f 2a 20 32 39 39 20 2a 2f 20 22 64 61 74  . /* 299 */ "dat
2d664 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d  abase_kw_opt ::=
2d665 22 2c 0a 20 2f 2a 20 33 30 30 20 2a 2f 20 22 63  ",. /* 300 */ "c
2d666 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 22 2c  md ::= REINDEX",
2d667 0a 20 2f 2a 20 33 30 31 20 2a 2f 20 22 63 6d 64  . /* 301 */ "cmd
2d668 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 6e 6d 20   ::= REINDEX nm 
2d669 64 62 6e 6d 22 2c 0a 20 2f 2a 20 33 30 32 20 2a  dbnm",. /* 302 *
2d66a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59  / "cmd ::= ANALY
2d66b 5a 45 22 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20  ZE",. /* 303 */ 
2d66c 22 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45  "cmd ::= ANALYZE
2d66d 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 33   nm dbnm",. /* 3
2d66e 30 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41  04 */ "cmd ::= A
2d66f 4c 54 45 52 20 54 41 42 4c 45 20 66 75 6c 6c 6e  LTER TABLE fulln
2d670 61 6d 65 20 52 45 4e 41 4d 45 20 54 4f 20 6e 6d  ame RENAME TO nm
2d671 22 2c 0a 20 2f 2a 20 33 30 35 20 2a 2f 20 22 63  ",. /* 305 */ "c
2d672 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42  md ::= ALTER TAB
2d673 4c 45 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75  LE add_column_fu
2d674 6c 6c 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f 6c  llname ADD kwcol
2d675 75 6d 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 22 2c  umn_opt column",
2d676 0a 20 2f 2a 20 33 30 36 20 2a 2f 20 22 61 64 64  . /* 306 */ "add
2d677 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65  _column_fullname
2d678 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a   ::= fullname",.
2d679 20 2f 2a 20 33 30 37 20 2a 2f 20 22 6b 77 63 6f   /* 307 */ "kwco
2d67a 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  lumn_opt ::=",. 
2d67b 2f 2a 20 33 30 38 20 2a 2f 20 22 6b 77 63 6f 6c  /* 308 */ "kwcol
2d67c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55  umn_opt ::= COLU
2d67d 4d 4e 4b 57 22 2c 0a 20 2f 2a 20 33 30 39 20 2a  MNKW",. /* 309 *
2d67e 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74  / "cmd ::= creat
2d67f 65 5f 76 74 61 62 22 2c 0a 20 2f 2a 20 33 31 30  e_vtab",. /* 310
2d680 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65   */ "cmd ::= cre
2d681 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74 61 62  ate_vtab LP vtab
2d682 61 72 67 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  arglist RP",. /*
2d683 20 33 31 31 20 2a 2f 20 22 63 72 65 61 74 65 5f   311 */ "create_
2d684 76 74 61 62 20 3a 3a 3d 20 63 72 65 61 74 65 6b  vtab ::= createk
2d685 77 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  w VIRTUAL TABLE 
2d686 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e 6d  nm dbnm USING nm
2d687 22 2c 0a 20 2f 2a 20 33 31 32 20 2a 2f 20 22 76  ",. /* 312 */ "v
2d688 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
2d689 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33 31 33  tabarg",. /* 313
2d68a 20 2a 2f 20 22 76 74 61 62 61 72 67 6c 69 73 74   */ "vtabarglist
2d68b 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c 69 73 74   ::= vtabarglist
2d68c 20 43 4f 4d 4d 41 20 76 74 61 62 61 72 67 22 2c   COMMA vtabarg",
2d68d 0a 20 2f 2a 20 33 31 34 20 2a 2f 20 22 76 74 61  . /* 314 */ "vta
2d68e 62 61 72 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33  barg ::=",. /* 3
2d68f 31 35 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a  15 */ "vtabarg :
2d690 3a 3d 20 76 74 61 62 61 72 67 20 76 74 61 62 61  := vtabarg vtaba
2d691 72 67 74 6f 6b 65 6e 22 2c 0a 20 2f 2a 20 33 31  rgtoken",. /* 31
2d692 36 20 2a 2f 20 22 76 74 61 62 61 72 67 74 6f 6b  6 */ "vtabargtok
2d693 65 6e 20 3a 3a 3d 20 41 4e 59 22 2c 0a 20 2f 2a  en ::= ANY",. /*
2d694 20 33 31 37 20 2a 2f 20 22 76 74 61 62 61 72 67   317 */ "vtabarg
2d695 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79  token ::= lp any
2d696 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 31  list RP",. /* 31
2d697 38 20 2a 2f 20 22 6c 70 20 3a 3a 3d 20 4c 50 22  8 */ "lp ::= LP"
2d698 2c 0a 20 2f 2a 20 33 31 39 20 2a 2f 20 22 61 6e  ,. /* 319 */ "an
2d699 79 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  ylist ::=",. /* 
2d69a 33 32 30 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20  320 */ "anylist 
2d69b 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59 22  ::= anylist ANY"
2d69c 2c 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  ,.};.#endif /* N
2d69d 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 20 59  DEBUG */...#if Y
2d69e 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 2f  YSTACKDEPTH<=0./
2d69f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 69 6e 63 72  *.** Try to incr
2d6a0 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ease the size of
2d6a1 20 74 68 65 20 70 61 72 73 65 72 20 73 74 61 63   the parser stac
2d6a2 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
2d6a3 64 20 79 79 47 72 6f 77 53 74 61 63 6b 28 79 79  d yyGrowStack(yy
2d6a4 50 61 72 73 65 72 20 2a 70 29 7b 0a 20 20 69 6e  Parser *p){.  in
2d6a5 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20 79 79 53  t newSize;.  yyS
2d6a6 74 61 63 6b 45 6e 74 72 79 20 2a 70 4e 65 77 3b  tackEntry *pNew;
2d6a7 0a 0a 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 2d  ..  newSize = p-
2d6a8 3e 79 79 73 74 6b 73 7a 2a 32 20 2b 20 31 30 30  >yystksz*2 + 100
2d6a9 3b 0a 20 20 70 4e 65 77 20 3d 20 72 65 61 6c 6c  ;.  pNew = reall
2d6aa 6f 63 28 70 2d 3e 79 79 73 74 61 63 6b 2c 20 6e  oc(p->yystack, n
2d6ab 65 77 53 69 7a 65 2a 73 69 7a 65 6f 66 28 70 4e  ewSize*sizeof(pN
2d6ac 65 77 5b 30 5d 29 29 3b 0a 20 20 69 66 28 20 70  ew[0]));.  if( p
2d6ad 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d 3e 79 79  New ){.    p->yy
2d6ae 73 74 61 63 6b 20 3d 20 70 4e 65 77 3b 0a 20 20  stack = pNew;.  
2d6af 20 20 70 2d 3e 79 79 73 74 6b 73 7a 20 3d 20 6e    p->yystksz = n
2d6b0 65 77 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20  ewSize;.#ifndef 
2d6b1 4e 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 79  NDEBUG.    if( y
2d6b2 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
2d6b3 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
2d6b4 61 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63 6b  aceFILE,"%sStack
2d6b5 20 67 72 6f 77 73 20 74 6f 20 25 64 20 65 6e 74   grows to %d ent
2d6b6 72 69 65 73 21 5c 6e 22 2c 0a 20 20 20 20 20 20  ries!\n",.      
2d6b7 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50          yyTraceP
2d6b8 72 6f 6d 70 74 2c 20 70 2d 3e 79 79 73 74 6b 73  rompt, p->yystks
2d6b9 7a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  z);.    }.#endif
2d6ba 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
2d6bb 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  * .** This funct
2d6bc 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ion allocates a 
2d6bd 6e 65 77 20 70 61 72 73 65 72 2e 0a 2a 2a 20 54  new parser..** T
2d6be 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74  he only argument
2d6bf 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2d6c0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 63   a function whic
2d6c1 68 20 77 6f 72 6b 73 20 6c 69 6b 65 0a 2a 2a 20  h works like.** 
2d6c2 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 49 6e  malloc..**.** In
2d6c3 70 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74  puts:.** A point
2d6c4 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  er to the functi
2d6c5 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63  on used to alloc
2d6c6 61 74 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a  ate memory..**.*
2d6c7 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 41 20  * Outputs:.** A 
2d6c8 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 61 72  pointer to a par
2d6c9 73 65 72 2e 20 20 54 68 69 73 20 70 6f 69 6e 74  ser.  This point
2d6ca 65 72 20 69 73 20 75 73 65 64 20 69 6e 20 73 75  er is used in su
2d6cb 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 0a 2a  bsequent calls.*
2d6cc 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 72 73  * to sqlite3Pars
2d6cd 65 72 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61  er and sqlite3Pa
2d6ce 72 73 65 72 46 72 65 65 2e 0a 2a 2f 0a 53 51 4c  rserFree..*/.SQL
2d6cf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d6d0 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72 41   *sqlite3ParserA
2d6d1 6c 6c 6f 63 28 76 6f 69 64 20 2a 28 2a 6d 61 6c  lloc(void *(*mal
2d6d2 6c 6f 63 50 72 6f 63 29 28 73 69 7a 65 5f 74 29  locProc)(size_t)
2d6d3 29 7b 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70  ){.  yyParser *p
2d6d4 50 61 72 73 65 72 3b 0a 20 20 70 50 61 72 73 65  Parser;.  pParse
2d6d5 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 28  r = (yyParser*)(
2d6d6 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 20 28 73  *mallocProc)( (s
2d6d7 69 7a 65 5f 74 29 73 69 7a 65 6f 66 28 79 79 50  ize_t)sizeof(yyP
2d6d8 61 72 73 65 72 29 20 29 3b 0a 20 20 69 66 28 20  arser) );.  if( 
2d6d9 70 50 61 72 73 65 72 20 29 7b 0a 20 20 20 20 70  pParser ){.    p
2d6da 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20  Parser->yyidx = 
2d6db 2d 31 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41  -1;.#ifdef YYTRA
2d6dc 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a  CKMAXSTACKDEPTH.
2d6dd 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79 69      pParser->yyi
2d6de 64 78 4d 61 78 20 3d 20 30 3b 0a 23 65 6e 64 69  dxMax = 0;.#endi
2d6df 66 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50  f.#if YYSTACKDEP
2d6e0 54 48 3c 3d 30 0a 20 20 20 20 70 50 61 72 73 65  TH<=0.    pParse
2d6e1 72 2d 3e 79 79 73 74 61 63 6b 20 3d 20 4e 55 4c  r->yystack = NUL
2d6e2 4c 3b 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e  L;.    pParser->
2d6e3 79 79 73 74 6b 73 7a 20 3d 20 30 3b 0a 20 20 20  yystksz = 0;.   
2d6e4 20 79 79 47 72 6f 77 53 74 61 63 6b 28 70 50 61   yyGrowStack(pPa
2d6e5 72 73 65 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20  rser);.#endif.  
2d6e6 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73  }.  return pPars
2d6e7 65 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f  er;.}../* The fo
2d6e8 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llowing function
2d6e9 20 64 65 6c 65 74 65 73 20 74 68 65 20 76 61 6c   deletes the val
2d6ea 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ue associated wi
2d6eb 74 68 20 61 0a 2a 2a 20 73 79 6d 62 6f 6c 2e 20  th a.** symbol. 
2d6ec 20 54 68 65 20 73 79 6d 62 6f 6c 20 63 61 6e 20   The symbol can 
2d6ed 62 65 20 65 69 74 68 65 72 20 61 20 74 65 72 6d  be either a term
2d6ee 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69  inal or nontermi
2d6ef 6e 61 6c 2e 0a 2a 2a 20 22 79 79 6d 61 6a 6f 72  nal..** "yymajor
2d6f0 22 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 20  " is the symbol 
2d6f1 63 6f 64 65 2c 20 61 6e 64 20 22 79 79 70 6d 69  code, and "yypmi
2d6f2 6e 6f 72 22 20 69 73 20 61 20 70 6f 69 6e 74 65  nor" is a pointe
2d6f3 72 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75  r to.** the valu
2d6f4 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
2d6f5 64 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28  d yy_destructor(
2d6f6 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70  .  yyParser *yyp
2d6f7 50 61 72 73 65 72 2c 20 20 20 20 2f 2a 20 54 68  Parser,    /* Th
2d6f8 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 59 59  e parser */.  YY
2d6f9 43 4f 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72  CODETYPE yymajor
2d6fa 2c 20 20 20 20 20 2f 2a 20 54 79 70 65 20 63 6f  ,     /* Type co
2d6fb 64 65 20 66 6f 72 20 6f 62 6a 65 63 74 20 74 6f  de for object to
2d6fc 20 64 65 73 74 72 6f 79 20 2a 2f 0a 20 20 59 59   destroy */.  YY
2d6fd 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 6d 69  MINORTYPE *yypmi
2d6fe 6e 6f 72 20 20 20 2f 2a 20 54 68 65 20 6f 62 6a  nor   /* The obj
2d6ff 65 63 74 20 74 6f 20 62 65 20 64 65 73 74 72 6f  ect to be destro
2d700 79 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  yed */.){.  sqli
2d701 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
2d702 43 48 3b 0a 20 20 73 77 69 74 63 68 28 20 79 79  CH;.  switch( yy
2d703 6d 61 6a 6f 72 20 29 7b 0a 20 20 20 20 2f 2a 20  major ){.    /* 
2d704 48 65 72 65 20 69 73 20 69 6e 73 65 72 74 65 64  Here is inserted
2d705 20 74 68 65 20 61 63 74 69 6f 6e 73 20 77 68 69   the actions whi
2d706 63 68 20 74 61 6b 65 20 70 6c 61 63 65 20 77 68  ch take place wh
2d707 65 6e 20 61 0a 20 20 20 20 2a 2a 20 74 65 72 6d  en a.    ** term
2d708 69 6e 61 6c 20 6f 72 20 6e 6f 6e 2d 74 65 72 6d  inal or non-term
2d709 69 6e 61 6c 20 69 73 20 64 65 73 74 72 6f 79 65  inal is destroye
2d70a 64 2e 20 20 54 68 69 73 20 63 61 6e 20 68 61 70  d.  This can hap
2d70b 70 65 6e 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20  pen.    ** when 
2d70c 74 68 65 20 73 79 6d 62 6f 6c 20 69 73 20 70 6f  the symbol is po
2d70d 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74  pped from the st
2d70e 61 63 6b 20 64 75 72 69 6e 67 20 61 0a 20 20 20  ack during a.   
2d70f 20 2a 2a 20 72 65 64 75 63 65 20 6f 72 20 64 75   ** reduce or du
2d710 72 69 6e 67 20 65 72 72 6f 72 20 70 72 6f 63 65  ring error proce
2d711 73 73 69 6e 67 20 6f 72 20 77 68 65 6e 20 61 20  ssing or when a 
2d712 70 61 72 73 65 72 20 69 73 20 0a 20 20 20 20 2a  parser is .    *
2d713 2a 20 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65  * being destroye
2d714 64 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 66  d before it is f
2d715 69 6e 69 73 68 65 64 20 70 61 72 73 69 6e 67 2e  inished parsing.
2d716 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e  .    **.    ** N
2d717 6f 74 65 3a 20 64 75 72 69 6e 67 20 61 20 72 65  ote: during a re
2d718 64 75 63 65 2c 20 74 68 65 20 6f 6e 6c 79 20 73  duce, the only s
2d719 79 6d 62 6f 6c 73 20 64 65 73 74 72 6f 79 65 64  ymbols destroyed
2d71a 20 61 72 65 20 74 68 6f 73 65 0a 20 20 20 20 2a   are those.    *
2d71b 2a 20 77 68 69 63 68 20 61 70 70 65 61 72 20 6f  * which appear o
2d71c 6e 20 74 68 65 20 52 48 53 20 6f 66 20 74 68 65  n the RHS of the
2d71d 20 72 75 6c 65 2c 20 62 75 74 20 77 68 69 63 68   rule, but which
2d71e 20 61 72 65 20 6e 6f 74 20 75 73 65 64 0a 20 20   are not used.  
2d71f 20 20 2a 2a 20 69 6e 73 69 64 65 20 74 68 65 20    ** inside the 
2d720 43 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2f 0a 20  C code..    */. 
2d721 20 20 20 63 61 73 65 20 31 36 30 3a 20 2f 2a 20     case 160: /* 
2d722 73 65 6c 65 63 74 20 2a 2f 0a 20 20 20 20 63 61  select */.    ca
2d723 73 65 20 31 39 34 3a 20 2f 2a 20 6f 6e 65 73 65  se 194: /* onese
2d724 6c 65 63 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  lect */.{.sqlite
2d725 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50  3SelectDelete(pP
2d726 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69  arse->db, (yypmi
2d727 6e 6f 72 2d 3e 79 79 32 34 33 29 29 3b 0a 7d 0a  nor->yy243));.}.
2d728 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d729 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 74 65   case 174: /* te
2d72a 72 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31  rm */.    case 1
2d72b 37 35 3a 20 2f 2a 20 65 78 70 72 20 2a 2f 0a 20  75: /* expr */. 
2d72c 20 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20     case 199: /* 
2d72d 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 20 20 20  where_opt */.   
2d72e 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20 68 61   case 201: /* ha
2d72f 76 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  ving_opt */.    
2d730 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 6f 6e 5f  case 210: /* on_
2d731 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
2d732 32 31 35 3a 20 2f 2a 20 73 6f 72 74 69 74 65 6d  215: /* sortitem
2d733 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 33   */.    case 223
2d734 3a 20 2f 2a 20 65 73 63 61 70 65 20 2a 2f 0a 20  : /* escape */. 
2d735 20 20 20 63 61 73 65 20 32 32 36 3a 20 2f 2a 20     case 226: /* 
2d736 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 2a 2f 0a  case_operand */.
2d737 20 20 20 20 63 61 73 65 20 32 32 38 3a 20 2f 2a      case 228: /*
2d738 20 63 61 73 65 5f 65 6c 73 65 20 2a 2f 0a 20 20   case_else */.  
2d739 20 20 63 61 73 65 20 32 33 39 3a 20 2f 2a 20 77    case 239: /* w
2d73a 68 65 6e 5f 63 6c 61 75 73 65 20 2a 2f 0a 20 20  hen_clause */.  
2d73b 20 20 63 61 73 65 20 32 34 32 3a 20 2f 2a 20 6b    case 242: /* k
2d73c 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c 69  ey_opt */.{.sqli
2d73d 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50  te3ExprDelete(pP
2d73e 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69  arse->db, (yypmi
2d73f 6e 6f 72 2d 3e 79 79 37 32 29 29 3b 0a 7d 0a 20  nor->yy72));.}. 
2d740 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d741 63 61 73 65 20 31 37 39 3a 20 2f 2a 20 69 64 78  case 179: /* idx
2d742 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  list_opt */.    
2d743 63 61 73 65 20 31 38 37 3a 20 2f 2a 20 69 64 78  case 187: /* idx
2d744 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
2d745 20 31 39 37 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c   197: /* selcoll
2d746 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
2d747 32 30 30 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f  200: /* groupby_
2d748 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
2d749 32 30 32 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f  202: /* orderby_
2d74a 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
2d74b 32 30 34 3a 20 2f 2a 20 73 63 6c 70 20 2a 2f 0a  204: /* sclp */.
2d74c 20 20 20 20 63 61 73 65 20 32 31 34 3a 20 2f 2a      case 214: /*
2d74d 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20   sortlist */.   
2d74e 20 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 6e 65   case 216: /* ne
2d74f 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63  xprlist */.    c
2d750 61 73 65 20 32 31 37 3a 20 2f 2a 20 73 65 74 6c  ase 217: /* setl
2d751 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
2d752 32 32 30 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74  220: /* itemlist
2d753 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 31   */.    case 221
2d754 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 2a 2f  : /* exprlist */
2d755 0a 20 20 20 20 63 61 73 65 20 32 32 37 3a 20 2f  .    case 227: /
2d756 2a 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20  * case_exprlist 
2d757 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72  */.{.sqlite3Expr
2d758 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
2d759 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72  e->db, (yypminor
2d75a 2d 3e 79 79 31 34 38 29 29 3b 0a 7d 0a 20 20 20  ->yy148));.}.   
2d75b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
2d75c 73 65 20 31 39 33 3a 20 2f 2a 20 66 75 6c 6c 6e  se 193: /* fulln
2d75d 61 6d 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ame */.    case 
2d75e 31 39 38 3a 20 2f 2a 20 66 72 6f 6d 20 2a 2f 0a  198: /* from */.
2d75f 20 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f 2a      case 206: /*
2d760 20 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 20   seltablist */. 
2d761 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20     case 207: /* 
2d762 73 74 6c 5f 70 72 65 66 69 78 20 2a 2f 0a 7b 0a  stl_prefix */.{.
2d763 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
2d764 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2d765 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 38   (yypminor->yy18
2d766 35 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  5));.}.      bre
2d767 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 31 31  ak;.    case 211
2d768 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 2a  : /* using_opt *
2d769 2f 0a 20 20 20 20 63 61 73 65 20 32 31 33 3a 20  /.    case 213: 
2d76a 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f  /* inscollist */
2d76b 0a 20 20 20 20 63 61 73 65 20 32 31 39 3a 20 2f  .    case 219: /
2d76c 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  * inscollist_opt
2d76d 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c   */.{.sqlite3IdL
2d76e 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
2d76f 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
2d770 3e 79 79 32 35 34 29 29 3b 0a 7d 0a 20 20 20 20  >yy254));.}.    
2d771 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
2d772 65 20 32 33 35 3a 20 2f 2a 20 74 72 69 67 67 65  e 235: /* trigge
2d773 72 5f 63 6d 64 5f 6c 69 73 74 20 2a 2f 0a 20 20  r_cmd_list */.  
2d774 20 20 63 61 73 65 20 32 34 30 3a 20 2f 2a 20 74    case 240: /* t
2d775 72 69 67 67 65 72 5f 63 6d 64 20 2a 2f 0a 7b 0a  rigger_cmd */.{.
2d776 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
2d777 67 67 65 72 53 74 65 70 28 70 50 61 72 73 65 2d  ggerStep(pParse-
2d778 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e  >db, (yypminor->
2d779 79 79 31 34 35 29 29 3b 0a 7d 0a 20 20 20 20 20  yy145));.}.     
2d77a 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
2d77b 20 32 33 37 3a 20 2f 2a 20 74 72 69 67 67 65 72   237: /* trigger
2d77c 5f 65 76 65 6e 74 20 2a 2f 0a 7b 0a 73 71 6c 69  _event */.{.sqli
2d77d 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
2d77e 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70  pParse->db, (yyp
2d77f 6d 69 6e 6f 72 2d 3e 79 79 33 33 32 29 2e 62 29  minor->yy332).b)
2d780 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
2d781 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 62  .    default:  b
2d782 72 65 61 6b 3b 20 20 20 2f 2a 20 49 66 20 6e 6f  reak;   /* If no
2d783 20 64 65 73 74 72 75 63 74 6f 72 20 61 63 74 69   destructor acti
2d784 6f 6e 20 73 70 65 63 69 66 69 65 64 3a 20 64 6f  on specified: do
2d785 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a   nothing */.  }.
2d786 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74 68 65  }../*.** Pop the
2d787 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20   parser's stack 
2d788 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  once..**.** If t
2d789 68 65 72 65 20 69 73 20 61 20 64 65 73 74 72 75  here is a destru
2d78a 63 74 6f 72 20 72 6f 75 74 69 6e 65 20 61 73 73  ctor routine ass
2d78b 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
2d78c 20 74 6f 6b 65 6e 20 77 68 69 63 68 0a 2a 2a 20   token which.** 
2d78d 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74  is popped from t
2d78e 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20 63  he stack, then c
2d78f 61 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 65  all it..**.** Re
2d790 74 75 72 6e 20 74 68 65 20 6d 61 6a 6f 72 20 74  turn the major t
2d791 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20  oken number for 
2d792 74 68 65 20 73 79 6d 62 6f 6c 20 70 6f 70 70 65  the symbol poppe
2d793 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
2d794 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73   yy_pop_parser_s
2d795 74 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a 70  tack(yyParser *p
2d796 50 61 72 73 65 72 29 7b 0a 20 20 59 59 43 4f 44  Parser){.  YYCOD
2d797 45 54 59 50 45 20 79 79 6d 61 6a 6f 72 3b 0a 20  ETYPE yymajor;. 
2d798 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79   yyStackEntry *y
2d799 79 74 6f 73 20 3d 20 26 70 50 61 72 73 65 72 2d  ytos = &pParser-
2d79a 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65 72  >yystack[pParser
2d79b 2d 3e 79 79 69 64 78 5d 3b 0a 0a 20 20 69 66 28  ->yyidx];..  if(
2d79c 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3c   pParser->yyidx<
2d79d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69  0 ) return 0;.#i
2d79e 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
2d79f 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 26  f( yyTraceFILE &
2d7a0 26 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  & pParser->yyidx
2d7a1 3e 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  >=0 ){.    fprin
2d7a2 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
2d7a3 25 73 50 6f 70 70 69 6e 67 20 25 73 5c 6e 22 2c  %sPopping %s\n",
2d7a4 0a 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72  .      yyTracePr
2d7a5 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79 79 54 6f  ompt,.      yyTo
2d7a6 6b 65 6e 4e 61 6d 65 5b 79 79 74 6f 73 2d 3e 6d  kenName[yytos->m
2d7a7 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64  ajor]);.  }.#end
2d7a8 69 66 0a 20 20 79 79 6d 61 6a 6f 72 20 3d 20 79  if.  yymajor = y
2d7a9 79 74 6f 73 2d 3e 6d 61 6a 6f 72 3b 0a 20 20 79  ytos->major;.  y
2d7aa 79 5f 64 65 73 74 72 75 63 74 6f 72 28 70 50 61  y_destructor(pPa
2d7ab 72 73 65 72 2c 20 79 79 6d 61 6a 6f 72 2c 20 26  rser, yymajor, &
2d7ac 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72 29 3b 0a 20  yytos->minor);. 
2d7ad 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d   pParser->yyidx-
2d7ae 2d 3b 0a 20 20 72 65 74 75 72 6e 20 79 79 6d 61  -;.  return yyma
2d7af 6a 6f 72 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44  jor;.}../* .** D
2d7b0 65 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 64 65  eallocate and de
2d7b1 73 74 72 6f 79 20 61 20 70 61 72 73 65 72 2e 20  stroy a parser. 
2d7b2 20 44 65 73 74 72 75 63 74 6f 72 73 20 61 72 65   Destructors are
2d7b3 20 61 6c 6c 20 63 61 6c 6c 65 64 20 66 6f 72 0a   all called for.
2d7b4 2a 2a 20 61 6c 6c 20 73 74 61 63 6b 20 65 6c 65  ** all stack ele
2d7b5 6d 65 6e 74 73 20 62 65 66 6f 72 65 20 73 68 75  ments before shu
2d7b6 74 74 69 6e 67 20 74 68 65 20 70 61 72 73 65 72  tting the parser
2d7b7 20 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70   down..**.** Inp
2d7b8 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  uts:.** <ul>.** 
2d7b9 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20  <li>  A pointer 
2d7ba 74 6f 20 74 68 65 20 70 61 72 73 65 72 2e 20 20  to the parser.  
2d7bb 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 61  This should be a
2d7bc 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20   pointer.**     
2d7bd 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20    obtained from 
2d7be 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c  sqlite3ParserAll
2d7bf 6f 63 2e 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20 70  oc..** <li>  A p
2d7c0 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
2d7c1 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63  tion used to rec
2d7c2 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 6f 62 74 61  laim memory obta
2d7c3 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 66 72  ined.**       fr
2d7c4 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 3c 2f  om malloc..** </
2d7c5 75 6c 3e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ul>.*/.SQLITE_PR
2d7c6 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2d7c7 65 33 50 61 72 73 65 72 46 72 65 65 28 0a 20 20  e3ParserFree(.  
2d7c8 76 6f 69 64 20 2a 70 2c 20 20 20 20 20 20 20 20  void *p,        
2d7c9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2d7ca 68 65 20 70 61 72 73 65 72 20 74 6f 20 62 65 20  he parser to be 
2d7cb 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 76 6f 69  deleted */.  voi
2d7cc 64 20 28 2a 66 72 65 65 50 72 6f 63 29 28 76 6f  d (*freeProc)(vo
2d7cd 69 64 2a 29 20 20 20 20 20 2f 2a 20 46 75 6e 63  id*)     /* Func
2d7ce 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63  tion used to rec
2d7cf 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29  laim memory */.)
2d7d0 7b 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70 50  {.  yyParser *pP
2d7d1 61 72 73 65 72 20 3d 20 28 79 79 50 61 72 73 65  arser = (yyParse
2d7d2 72 2a 29 70 3b 0a 20 20 69 66 28 20 70 50 61 72  r*)p;.  if( pPar
2d7d3 73 65 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ser==0 ) return;
2d7d4 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73 65  .  while( pParse
2d7d5 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79  r->yyidx>=0 ) yy
2d7d6 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63  _pop_parser_stac
2d7d7 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 69 66 20  k(pParser);.#if 
2d7d8 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a  YYSTACKDEPTH<=0.
2d7d9 20 20 66 72 65 65 28 70 50 61 72 73 65 72 2d 3e    free(pParser->
2d7da 79 79 73 74 61 63 6b 29 3b 0a 23 65 6e 64 69 66  yystack);.#endif
2d7db 0a 20 20 28 2a 66 72 65 65 50 72 6f 63 29 28 28  .  (*freeProc)((
2d7dc 76 6f 69 64 2a 29 70 50 61 72 73 65 72 29 3b 0a  void*)pParser);.
2d7dd 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2d7de 74 68 65 20 70 65 61 6b 20 64 65 70 74 68 20 6f  the peak depth o
2d7df 66 20 74 68 65 20 73 74 61 63 6b 20 66 6f 72 20  f the stack for 
2d7e0 61 20 70 61 72 73 65 72 2e 0a 2a 2f 0a 23 69 66  a parser..*/.#if
2d7e1 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54  def YYTRACKMAXST
2d7e2 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 5f  ACKDEPTH.SQLITE_
2d7e3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2d7e4 74 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65  te3ParserStackPe
2d7e5 61 6b 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 79  ak(void *p){.  y
2d7e6 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
2d7e7 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b   = (yyParser*)p;
2d7e8 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65  .  return pParse
2d7e9 72 2d 3e 79 79 69 64 78 4d 61 78 3b 0a 7d 0a 23  r->yyidxMax;.}.#
2d7ea 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  endif../*.** Fin
2d7eb 64 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  d the appropriat
2d7ec 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 70  e action for a p
2d7ed 61 72 73 65 72 20 67 69 76 65 6e 20 74 68 65 20  arser given the 
2d7ee 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b  terminal.** look
2d7ef 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c 6f  -ahead token iLo
2d7f0 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49  okAhead..**.** I
2d7f1 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  f the look-ahead
2d7f2 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43 4f   token is YYNOCO
2d7f3 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 74  DE, then check t
2d7f4 6f 20 73 65 65 20 69 66 20 74 68 65 20 61 63 74  o see if the act
2d7f5 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70 65  ion is.** indepe
2d7f6 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f 6f  ndent of the loo
2d7f7 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69 74 20  k-ahead.  If it 
2d7f8 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 61  is, return the a
2d7f9 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73 65  ction, otherwise
2d7fa 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e 4f  .** return YY_NO
2d7fb 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74  _ACTION..*/.stat
2d7fc 69 63 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f 73  ic int yy_find_s
2d7fd 68 69 66 74 5f 61 63 74 69 6f 6e 28 0a 20 20 79  hift_action(.  y
2d7fe 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
2d7ff 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
2d800 70 61 72 73 65 72 20 2a 2f 0a 20 20 59 59 43 4f  parser */.  YYCO
2d801 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65 61  DETYPE iLookAhea
2d802 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f  d     /* The loo
2d803 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a 2f  k-ahead token */
2d804 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
2d805 6e 74 20 73 74 61 74 65 6e 6f 20 3d 20 70 50 61  nt stateno = pPa
2d806 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 70 50  rser->yystack[pP
2d807 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74  arser->yyidx].st
2d808 61 74 65 6e 6f 3b 0a 20 0a 20 20 69 66 28 20 73  ateno;. .  if( s
2d809 74 61 74 65 6e 6f 3e 59 59 5f 53 48 49 46 54 5f  tateno>YY_SHIFT_
2d80a 4d 41 58 20 7c 7c 20 28 69 20 3d 20 79 79 5f 73  MAX || (i = yy_s
2d80b 68 69 66 74 5f 6f 66 73 74 5b 73 74 61 74 65 6e  hift_ofst[staten
2d80c 6f 5d 29 3d 3d 59 59 5f 53 48 49 46 54 5f 55 53  o])==YY_SHIFT_US
2d80d 45 5f 44 46 4c 54 20 29 7b 0a 20 20 20 20 72 65  E_DFLT ){.    re
2d80e 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b  turn yy_default[
2d80f 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 20 20  stateno];.  }.  
2d810 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68 65  assert( iLookAhe
2d811 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b 0a  ad!=YYNOCODE );.
2d812 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61    i += iLookAhea
2d813 64 3b 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c 20  d;.  if( i<0 || 
2d814 69 3e 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42 20  i>=YY_SZ_ACTTAB 
2d815 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  || yy_lookahead[
2d816 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29  i]!=iLookAhead )
2d817 7b 0a 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41  {.    if( iLookA
2d818 68 65 61 64 3e 30 20 29 7b 0a 23 69 66 64 65 66  head>0 ){.#ifdef
2d819 20 59 59 46 41 4c 4c 42 41 43 4b 0a 20 20 20 20   YYFALLBACK.    
2d81a 20 20 59 59 43 4f 44 45 54 59 50 45 20 69 46 61    YYCODETYPE iFa
2d81b 6c 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20  llback;         
2d81c 20 20 20 2f 2a 20 46 61 6c 6c 62 61 63 6b 20 74     /* Fallback t
2d81d 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66  oken */.      if
2d81e 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3c 73 69 7a  ( iLookAhead<siz
2d81f 65 6f 66 28 79 79 46 61 6c 6c 62 61 63 6b 29 2f  eof(yyFallback)/
2d820 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61 63  sizeof(yyFallbac
2d821 6b 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 20 20  k[0]).          
2d822 20 20 20 26 26 20 28 69 46 61 6c 6c 62 61 63 6b     && (iFallback
2d823 20 3d 20 79 79 46 61 6c 6c 62 61 63 6b 5b 69 4c   = yyFallback[iL
2d824 6f 6f 6b 41 68 65 61 64 5d 29 21 3d 30 20 29 7b  ookAhead])!=0 ){
2d825 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2d826 20 20 20 20 20 20 20 20 69 66 28 20 79 79 54 72          if( yyTr
2d827 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20  aceFILE ){.     
2d828 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
2d829 72 61 63 65 46 49 4c 45 2c 20 22 25 73 46 41 4c  raceFILE, "%sFAL
2d82a 4c 42 41 43 4b 20 25 73 20 3d 3e 20 25 73 5c 6e  LBACK %s => %s\n
2d82b 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
2d82c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 79  yyTracePrompt, y
2d82d 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b  yTokenName[iLook
2d82e 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e  Ahead], yyTokenN
2d82f 61 6d 65 5b 69 46 61 6c 6c 62 61 63 6b 5d 29 3b  ame[iFallback]);
2d830 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  .        }.#endi
2d831 66 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  f.        return
2d832 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61   yy_find_shift_a
2d833 63 74 69 6f 6e 28 70 50 61 72 73 65 72 2c 20 69  ction(pParser, i
2d834 46 61 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20 20  Fallback);.     
2d835 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66   }.#endif.#ifdef
2d836 20 59 59 57 49 4c 44 43 41 52 44 0a 20 20 20 20   YYWILDCARD.    
2d837 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20    {.        int 
2d838 6a 20 3d 20 69 20 2d 20 69 4c 6f 6f 6b 41 68 65  j = i - iLookAhe
2d839 61 64 20 2b 20 59 59 57 49 4c 44 43 41 52 44 3b  ad + YYWILDCARD;
2d83a 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 3d  .        if( j>=
2d83b 30 20 26 26 20 6a 3c 59 59 5f 53 5a 5f 41 43 54  0 && j<YY_SZ_ACT
2d83c 54 41 42 20 26 26 20 79 79 5f 6c 6f 6f 6b 61 68  TAB && yy_lookah
2d83d 65 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c 44 43 41  ead[j]==YYWILDCA
2d83e 52 44 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44  RD ){.#ifndef ND
2d83f 45 42 55 47 0a 20 20 20 20 20 20 20 20 20 20 69  EBUG.          i
2d840 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
2d841 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 70  {.            fp
2d842 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2d843 45 2c 20 22 25 73 57 49 4c 44 43 41 52 44 20 25  E, "%sWILDCARD %
2d844 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20  s => %s\n",.    
2d845 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72 61             yyTra
2d846 63 65 50 72 6f 6d 70 74 2c 20 79 79 54 6f 6b 65  cePrompt, yyToke
2d847 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61 64  nName[iLookAhead
2d848 5d 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 59  ], yyTokenName[Y
2d849 59 57 49 4c 44 43 41 52 44 5d 29 3b 0a 20 20 20  YWILDCARD]);.   
2d84a 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20         }.#endif 
2d84b 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20 20 20  /* NDEBUG */.   
2d84c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 79 79         return yy
2d84d 5f 61 63 74 69 6f 6e 5b 6a 5d 3b 0a 20 20 20 20  _action[j];.    
2d84e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65      }.      }.#e
2d84f 6e 64 69 66 20 2f 2a 20 59 59 57 49 4c 44 43 41  ndif /* YYWILDCA
2d850 52 44 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  RD */.    }.    
2d851 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c  return yy_defaul
2d852 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 65  t[stateno];.  }e
2d853 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
2d854 79 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b 0a 20 20  yy_action[i];.  
2d855 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  }.}../*.** Find 
2d856 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
2d857 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 70 61 72  action for a par
2d858 73 65 72 20 67 69 76 65 6e 20 74 68 65 20 6e 6f  ser given the no
2d859 6e 2d 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f  n-terminal.** lo
2d85a 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69  ok-ahead token i
2d85b 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a  LookAhead..**.**
2d85c 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65   If the look-ahe
2d85d 61 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f  ad token is YYNO
2d85e 43 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b  CODE, then check
2d85f 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 61   to see if the a
2d860 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65  ction is.** inde
2d861 70 65 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c  pendent of the l
2d862 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69  ook-ahead.  If i
2d863 74 20 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65  t is, return the
2d864 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69   action, otherwi
2d865 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f  se.** return YY_
2d866 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74  NO_ACTION..*/.st
2d867 61 74 69 63 20 69 6e 74 20 79 79 5f 66 69 6e 64  atic int yy_find
2d868 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a  _reduce_action(.
2d869 20 20 69 6e 74 20 73 74 61 74 65 6e 6f 2c 20 20    int stateno,  
2d86a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2d86b 75 72 72 65 6e 74 20 73 74 61 74 65 20 6e 75 6d  urrent state num
2d86c 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54  ber */.  YYCODET
2d86d 59 50 45 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20  YPE iLookAhead  
2d86e 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61     /* The look-a
2d86f 68 65 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b  head token */.){
2d870 0a 20 20 69 6e 74 20 69 3b 0a 23 69 66 64 65 66  .  int i;.#ifdef
2d871 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20   YYERRORSYMBOL. 
2d872 20 69 66 28 20 73 74 61 74 65 6e 6f 3e 59 59 5f   if( stateno>YY_
2d873 52 45 44 55 43 45 5f 4d 41 58 20 29 7b 0a 20 20  REDUCE_MAX ){.  
2d874 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
2d875 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
2d876 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74  }.#else.  assert
2d877 28 20 73 74 61 74 65 6e 6f 3c 3d 59 59 5f 52 45  ( stateno<=YY_RE
2d878 44 55 43 45 5f 4d 41 58 20 29 3b 0a 23 65 6e 64  DUCE_MAX );.#end
2d879 69 66 0a 20 20 69 20 3d 20 79 79 5f 72 65 64 75  if.  i = yy_redu
2d87a 63 65 5f 6f 66 73 74 5b 73 74 61 74 65 6e 6f 5d  ce_ofst[stateno]
2d87b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 21 3d 59  ;.  assert( i!=Y
2d87c 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c  Y_REDUCE_USE_DFL
2d87d 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  T );.  assert( i
2d87e 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43  LookAhead!=YYNOC
2d87f 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c  ODE );.  i += iL
2d880 6f 6f 6b 41 68 65 61 64 3b 0a 23 69 66 64 65 66  ookAhead;.#ifdef
2d881 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20   YYERRORSYMBOL. 
2d882 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59   if( i<0 || i>=Y
2d883 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79  Y_SZ_ACTTAB || y
2d884 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d  y_lookahead[i]!=
2d885 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20  iLookAhead ){.  
2d886 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
2d887 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
2d888 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74  }.#else.  assert
2d889 28 20 69 3e 3d 30 20 26 26 20 69 3c 59 59 5f 53  ( i>=0 && i<YY_S
2d88a 5a 5f 41 43 54 54 41 42 20 29 3b 0a 20 20 61 73  Z_ACTTAB );.  as
2d88b 73 65 72 74 28 20 79 79 5f 6c 6f 6f 6b 61 68 65  sert( yy_lookahe
2d88c 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41 68 65 61  ad[i]==iLookAhea
2d88d 64 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  d );.#endif.  re
2d88e 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69  turn yy_action[i
2d88f 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ];.}../*.** The 
2d890 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
2d891 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20 74  e is called if t
2d892 68 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f  he stack overflo
2d893 77 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ws..*/.static vo
2d894 69 64 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c  id yyStackOverfl
2d895 6f 77 28 79 79 50 61 72 73 65 72 20 2a 79 79 70  ow(yyParser *yyp
2d896 50 61 72 73 65 72 2c 20 59 59 4d 49 4e 4f 52 54  Parser, YYMINORT
2d897 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72 29 7b 0a  YPE *yypMinor){.
2d898 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
2d899 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 20 79 79  ARG_FETCH;.   yy
2d89a 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d  pParser->yyidx--
2d89b 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
2d89c 0a 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46  .   if( yyTraceF
2d89d 49 4c 45 20 29 7b 0a 20 20 20 20 20 66 70 72 69  ILE ){.     fpri
2d89e 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
2d89f 22 25 73 53 74 61 63 6b 20 4f 76 65 72 66 6c 6f  "%sStack Overflo
2d8a0 77 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  w!\n",yyTracePro
2d8a1 6d 70 74 29 3b 0a 20 20 20 7d 0a 23 65 6e 64 69  mpt);.   }.#endi
2d8a2 66 0a 20 20 20 77 68 69 6c 65 28 20 79 79 70 50  f.   while( yypP
2d8a3 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20  arser->yyidx>=0 
2d8a4 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f  ) yy_pop_parser_
2d8a5 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29  stack(yypParser)
2d8a6 3b 0a 20 20 20 2f 2a 20 48 65 72 65 20 63 6f 64  ;.   /* Here cod
2d8a7 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77 68  e is inserted wh
2d8a8 69 63 68 20 77 69 6c 6c 20 65 78 65 63 75 74 65  ich will execute
2d8a9 20 69 66 20 74 68 65 20 70 61 72 73 65 72 0a 20   if the parser. 
2d8aa 20 20 2a 2a 20 73 74 61 63 6b 20 65 76 65 72 79    ** stack every
2d8ab 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a 0a 20   overflows */.. 
2d8ac 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2d8ad 52 28 79 79 70 4d 69 6e 6f 72 29 3b 20 2f 2a 20  R(yypMinor); /* 
2d8ae 53 69 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d  Silence some com
2d8af 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 2a  piler warnings *
2d8b0 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  /.  sqlite3Error
2d8b1 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 61 72  Msg(pParse, "par
2d8b2 73 65 72 20 73 74 61 63 6b 20 6f 76 65 72 66 6c  ser stack overfl
2d8b3 6f 77 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  ow");.  pParse->
2d8b4 70 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a  parseError = 1;.
2d8b5 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
2d8b6 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
2d8b7 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
2d8b8 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
2d8b9 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 20  ra_argument var 
2d8ba 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66  */.}../*.** Perf
2d8bb 6f 72 6d 20 61 20 73 68 69 66 74 20 61 63 74 69  orm a shift acti
2d8bc 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
2d8bd 69 64 20 79 79 5f 73 68 69 66 74 28 0a 20 20 79  id yy_shift(.  y
2d8be 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2d8bf 65 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er,          /* 
2d8c0 54 68 65 20 70 61 72 73 65 72 20 74 6f 20 62 65  The parser to be
2d8c1 20 73 68 69 66 74 65 64 20 2a 2f 0a 20 20 69 6e   shifted */.  in
2d8c2 74 20 79 79 4e 65 77 53 74 61 74 65 2c 20 20 20  t yyNewState,   
2d8c3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2d8c4 68 65 20 6e 65 77 20 73 74 61 74 65 20 74 6f 20  he new state to 
2d8c5 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69 6e  shift in */.  in
2d8c6 74 20 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20 20  t yyMajor,      
2d8c7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2d8c8 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 74  he major token t
2d8c9 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20  o shift in */.  
2d8ca 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70  YYMINORTYPE *yyp
2d8cb 4d 69 6e 6f 72 20 20 20 20 20 20 20 20 20 2f 2a  Minor         /*
2d8cc 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
2d8cd 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73  minor token to s
2d8ce 68 69 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20  hift in */.){.  
2d8cf 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79  yyStackEntry *yy
2d8d0 74 6f 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72  tos;.  yypParser
2d8d1 2d 3e 79 79 69 64 78 2b 2b 3b 0a 23 69 66 64 65  ->yyidx++;.#ifde
2d8d2 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43  f YYTRACKMAXSTAC
2d8d3 4b 44 45 50 54 48 0a 20 20 69 66 28 20 79 79 70  KDEPTH.  if( yyp
2d8d4 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 79 79  Parser->yyidx>yy
2d8d5 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61  pParser->yyidxMa
2d8d6 78 20 29 7b 0a 20 20 20 20 79 79 70 50 61 72 73  x ){.    yypPars
2d8d7 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20 79  er->yyidxMax = y
2d8d8 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b  ypParser->yyidx;
2d8d9 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20  .  }.#endif.#if 
2d8da 59 59 53 54 41 43 4b 44 45 50 54 48 3e 30 20 0a  YYSTACKDEPTH>0 .
2d8db 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
2d8dc 3e 79 79 69 64 78 3e 3d 59 59 53 54 41 43 4b 44  >yyidx>=YYSTACKD
2d8dd 45 50 54 48 20 29 7b 0a 20 20 20 20 79 79 53 74  EPTH ){.    yySt
2d8de 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50  ackOverflow(yypP
2d8df 61 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29  arser, yypMinor)
2d8e0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
2d8e1 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 79 79  }.#else.  if( yy
2d8e2 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d  pParser->yyidx>=
2d8e3 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b  yypParser->yystk
2d8e4 73 7a 20 29 7b 0a 20 20 20 20 79 79 47 72 6f 77  sz ){.    yyGrow
2d8e5 53 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29  Stack(yypParser)
2d8e6 3b 0a 20 20 20 20 69 66 28 20 79 79 70 50 61 72  ;.    if( yypPar
2d8e7 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50  ser->yyidx>=yypP
2d8e8 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29  arser->yystksz )
2d8e9 7b 0a 20 20 20 20 20 20 79 79 53 74 61 63 6b 4f  {.      yyStackO
2d8ea 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73 65  verflow(yypParse
2d8eb 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20  r, yypMinor);.  
2d8ec 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2d8ed 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79  }.  }.#endif.  y
2d8ee 79 74 6f 73 20 3d 20 26 79 79 70 50 61 72 73 65  ytos = &yypParse
2d8ef 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61  r->yystack[yypPa
2d8f0 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20  rser->yyidx];.  
2d8f1 79 79 74 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d  yytos->stateno =
2d8f2 20 28 59 59 41 43 54 49 4f 4e 54 59 50 45 29 79   (YYACTIONTYPE)y
2d8f3 79 4e 65 77 53 74 61 74 65 3b 0a 20 20 79 79 74  yNewState;.  yyt
2d8f4 6f 73 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59 59 43  os->major = (YYC
2d8f5 4f 44 45 54 59 50 45 29 79 79 4d 61 6a 6f 72 3b  ODETYPE)yyMajor;
2d8f6 0a 20 20 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72 20  .  yytos->minor 
2d8f7 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b 0a 23 69 66  = *yypMinor;.#if
2d8f8 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66  ndef NDEBUG.  if
2d8f9 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 26 26  ( yyTraceFILE &&
2d8fa 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
2d8fb 78 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  x>0 ){.    int i
2d8fc 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79  ;.    fprintf(yy
2d8fd 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 68 69  TraceFILE,"%sShi
2d8fe 66 74 20 25 64 5c 6e 22 2c 79 79 54 72 61 63 65  ft %d\n",yyTrace
2d8ff 50 72 6f 6d 70 74 2c 79 79 4e 65 77 53 74 61 74  Prompt,yyNewStat
2d900 65 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  e);.    fprintf(
2d901 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53  yyTraceFILE,"%sS
2d902 74 61 63 6b 3a 22 2c 79 79 54 72 61 63 65 50 72  tack:",yyTracePr
2d903 6f 6d 70 74 29 3b 0a 20 20 20 20 66 6f 72 28 69  ompt);.    for(i
2d904 3d 31 3b 20 69 3c 3d 79 79 70 50 61 72 73 65 72  =1; i<=yypParser
2d905 2d 3e 79 79 69 64 78 3b 20 69 2b 2b 29 0a 20 20  ->yyidx; i++).  
2d906 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
2d907 61 63 65 46 49 4c 45 2c 22 20 25 73 22 2c 79 79  aceFILE," %s",yy
2d908 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 70 50 61 72  TokenName[yypPar
2d909 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 69 5d 2e  ser->yystack[i].
2d90a 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20 20 66 70 72  major]);.    fpr
2d90b 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
2d90c 2c 22 5c 6e 22 29 3b 0a 20 20 7d 0a 23 65 6e 64  ,"\n");.  }.#end
2d90d 69 66 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c  if.}../* The fol
2d90e 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 63 6f 6e  lowing table con
2d90f 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tains informatio
2d910 6e 20 61 62 6f 75 74 20 65 76 65 72 79 20 72 75  n about every ru
2d911 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 75 73  le that.** is us
2d912 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 72 65  ed during the re
2d913 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  duce..*/.static 
2d914 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20  const struct {. 
2d915 20 59 59 43 4f 44 45 54 59 50 45 20 6c 68 73 3b   YYCODETYPE lhs;
2d916 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 6d 62           /* Symb
2d917 6f 6c 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68  ol on the left-h
2d918 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20  and side of the 
2d919 72 75 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  rule */.  unsign
2d91a 65 64 20 63 68 61 72 20 6e 72 68 73 3b 20 20 20  ed char nrhs;   
2d91b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
2d91c 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 73  ight-hand side s
2d91d 79 6d 62 6f 6c 73 20 69 6e 20 74 68 65 20 72 75  ymbols in the ru
2d91e 6c 65 20 2a 2f 0a 7d 20 79 79 52 75 6c 65 49 6e  le */.} yyRuleIn
2d91f 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 31 34 32  fo[] = {.  { 142
2d920 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20  , 1 },.  { 143, 
2d921 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31 20  2 },.  { 143, 1 
2d922 7d 2c 0a 20 20 7b 20 31 34 34 2c 20 31 20 7d 2c  },.  { 144, 1 },
2d923 0a 20 20 7b 20 31 34 34 2c 20 33 20 7d 2c 0a 20  .  { 144, 3 },. 
2d924 20 7b 20 31 34 35 2c 20 30 20 7d 2c 0a 20 20 7b   { 145, 0 },.  {
2d925 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   145, 1 },.  { 1
2d926 34 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 36  45, 3 },.  { 146
2d927 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
2d928 33 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 30 20  3 },.  { 149, 0 
2d929 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 31 20 7d 2c  },.  { 149, 1 },
2d92a 0a 20 20 7b 20 31 34 39 2c 20 32 20 7d 2c 0a 20  .  { 149, 2 },. 
2d92b 20 7b 20 31 34 38 2c 20 30 20 7d 2c 0a 20 20 7b   { 148, 0 },.  {
2d92c 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   148, 1 },.  { 1
2d92d 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38  48, 1 },.  { 148
2d92e 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
2d92f 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20  2 },.  { 147, 2 
2d930 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c  },.  { 147, 2 },
2d931 0a 20 20 7b 20 31 35 31 2c 20 31 20 7d 2c 0a 20  .  { 151, 1 },. 
2d932 20 7b 20 31 35 31 2c 20 30 20 7d 2c 0a 20 20 7b   { 151, 0 },.  {
2d933 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   147, 2 },.  { 1
2d934 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 3 },.  { 147
2d935 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 5 },.  { 147, 
2d936 32 20 7d 2c 0a 20 20 7b 20 31 35 32 2c 20 36 20  2 },.  { 152, 6 
2d937 7d 2c 0a 20 20 7b 20 31 35 34 2c 20 31 20 7d 2c  },.  { 154, 1 },
2d938 0a 20 20 7b 20 31 35 36 2c 20 30 20 7d 2c 0a 20  .  { 156, 0 },. 
2d939 20 7b 20 31 35 36 2c 20 33 20 7d 2c 0a 20 20 7b   { 156, 3 },.  {
2d93a 20 31 35 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   155, 1 },.  { 1
2d93b 35 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 33  55, 0 },.  { 153
2d93c 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20  , 4 },.  { 153, 
2d93d 32 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 33 20  2 },.  { 158, 3 
2d93e 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 31 20 7d 2c  },.  { 158, 1 },
2d93f 0a 20 20 7b 20 31 36 31 2c 20 33 20 7d 2c 0a 20  .  { 161, 3 },. 
2d940 20 7b 20 31 36 32 2c 20 31 20 7d 2c 0a 20 20 7b   { 162, 1 },.  {
2d941 20 31 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   165, 1 },.  { 1
2d942 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 36  65, 1 },.  { 166
2d943 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20  , 1 },.  { 150, 
2d944 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20  1 },.  { 150, 1 
2d945 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c  },.  { 150, 1 },
2d946 0a 20 20 7b 20 31 36 33 2c 20 30 20 7d 2c 0a 20  .  { 163, 0 },. 
2d947 20 7b 20 31 36 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 163, 1 },.  {
2d948 20 31 36 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   167, 1 },.  { 1
2d949 36 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36 37  67, 4 },.  { 167
2d94a 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20  , 6 },.  { 168, 
2d94b 31 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 32 20  1 },.  { 168, 2 
2d94c 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c  },.  { 169, 1 },
2d94d 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20  .  { 169, 1 },. 
2d94e 20 7b 20 31 36 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 164, 2 },.  {
2d94f 20 31 36 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   164, 0 },.  { 1
2d950 37 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 32  72, 3 },.  { 172
2d951 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 1 },.  { 173, 
2d952 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20  2 },.  { 173, 4 
2d953 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c  },.  { 173, 3 },
2d954 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20  .  { 173, 3 },. 
2d955 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b   { 173, 2 },.  {
2d956 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   173, 2 },.  { 1
2d957 37 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 33  73, 3 },.  { 173
2d958 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 5 },.  { 173, 
2d959 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20  2 },.  { 173, 4 
2d95a 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d 2c  },.  { 173, 4 },
2d95b 0a 20 20 7b 20 31 37 33 2c 20 31 20 7d 2c 0a 20  .  { 173, 1 },. 
2d95c 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b   { 173, 2 },.  {
2d95d 20 31 37 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   178, 0 },.  { 1
2d95e 37 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 30  78, 1 },.  { 180
2d95f 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20  , 0 },.  { 180, 
2d960 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 32 20  2 },.  { 182, 2 
2d961 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c  },.  { 182, 3 },
2d962 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20  .  { 182, 3 },. 
2d963 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20 20 7b   { 182, 3 },.  {
2d964 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   183, 2 },.  { 1
2d965 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 33  83, 2 },.  { 183
2d966 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20  , 1 },.  { 183, 
2d967 31 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 33 20  1 },.  { 181, 3 
2d968 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 32 20 7d 2c  },.  { 181, 2 },
2d969 0a 20 20 7b 20 31 38 34 2c 20 30 20 7d 2c 0a 20  .  { 184, 0 },. 
2d96a 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 184, 2 },.  {
2d96b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   184, 2 },.  { 1
2d96c 35 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 39  59, 0 },.  { 159
2d96d 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20  , 2 },.  { 185, 
2d96e 33 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 32 20  3 },.  { 185, 2 
2d96f 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 31 20 7d 2c  },.  { 185, 1 },
2d970 0a 20 20 7b 20 31 38 36 2c 20 32 20 7d 2c 0a 20  .  { 186, 2 },. 
2d971 20 7b 20 31 38 36 2c 20 37 20 7d 2c 0a 20 20 7b   { 186, 7 },.  {
2d972 20 31 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   186, 5 },.  { 1
2d973 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 36  86, 5 },.  { 186
2d974 2c 20 31 30 20 7d 2c 0a 20 20 7b 20 31 38 38 2c  , 10 },.  { 188,
2d975 20 30 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 31   0 },.  { 188, 1
2d976 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 30 20 7d   },.  { 176, 0 }
2d977 2c 0a 20 20 7b 20 31 37 36 2c 20 33 20 7d 2c 0a  ,.  { 176, 3 },.
2d978 20 20 7b 20 31 38 39 2c 20 30 20 7d 2c 0a 20 20    { 189, 0 },.  
2d979 7b 20 31 38 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 189, 2 },.  { 
2d97a 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39  190, 1 },.  { 19
2d97b 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c  0, 1 },.  { 190,
2d97c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34   1 },.  { 147, 4
2d97d 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 32 20 7d   },.  { 192, 2 }
2d97e 2c 0a 20 20 7b 20 31 39 32 2c 20 30 20 7d 2c 0a  ,.  { 192, 0 },.
2d97f 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c 0a 20 20    { 147, 8 },.  
2d980 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 147, 4 },.  { 
2d981 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  147, 1 },.  { 16
2d982 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 30 2c  0, 1 },.  { 160,
2d983 20 33 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 31   3 },.  { 195, 1
2d984 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 32 20 7d   },.  { 195, 2 }
2d985 2c 0a 20 20 7b 20 31 39 35 2c 20 31 20 7d 2c 0a  ,.  { 195, 1 },.
2d986 20 20 7b 20 31 39 34 2c 20 39 20 7d 2c 0a 20 20    { 194, 9 },.  
2d987 7b 20 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 196, 1 },.  { 
2d988 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39  196, 1 },.  { 19
2d989 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 34 2c  6, 0 },.  { 204,
2d98a 20 32 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 30   2 },.  { 204, 0
2d98b 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 33 20 7d   },.  { 197, 3 }
2d98c 2c 0a 20 20 7b 20 31 39 37 2c 20 32 20 7d 2c 0a  ,.  { 197, 2 },.
2d98d 20 20 7b 20 31 39 37 2c 20 34 20 7d 2c 0a 20 20    { 197, 4 },.  
2d98e 7b 20 32 30 35 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 205, 2 },.  { 
2d98f 32 30 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 30  205, 1 },.  { 20
2d990 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38 2c  5, 0 },.  { 198,
2d991 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 32   0 },.  { 198, 2
2d992 20 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 32 20 7d   },.  { 207, 2 }
2d993 2c 0a 20 20 7b 20 32 30 37 2c 20 30 20 7d 2c 0a  ,.  { 207, 0 },.
2d994 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20    { 206, 7 },.  
2d995 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20  { 206, 7 },.  { 
2d996 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 31 35  206, 7 },.  { 15
2d997 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 37 2c  7, 0 },.  { 157,
2d998 20 32 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20 32   2 },.  { 193, 2
2d999 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 31 20 7d   },.  { 208, 1 }
2d99a 2c 0a 20 20 7b 20 32 30 38 2c 20 32 20 7d 2c 0a  ,.  { 208, 2 },.
2d99b 20 20 7b 20 32 30 38 2c 20 33 20 7d 2c 0a 20 20    { 208, 3 },.  
2d99c 7b 20 32 30 38 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 208, 4 },.  { 
2d99d 32 31 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31  210, 2 },.  { 21
2d99e 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39 2c  0, 0 },.  { 209,
2d99f 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 33   0 },.  { 209, 3
2d9a0 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 32 20 7d   },.  { 209, 2 }
2d9a1 2c 0a 20 20 7b 20 32 31 31 2c 20 34 20 7d 2c 0a  ,.  { 211, 4 },.
2d9a2 20 20 7b 20 32 31 31 2c 20 30 20 7d 2c 0a 20 20    { 211, 0 },.  
2d9a3 7b 20 32 30 32 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 202, 0 },.  { 
2d9a4 32 30 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31  202, 3 },.  { 21
2d9a5 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31 34 2c  4, 4 },.  { 214,
2d9a6 20 32 20 7d 2c 0a 20 20 7b 20 32 31 35 2c 20 31   2 },.  { 215, 1
2d9a7 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20 7d   },.  { 177, 1 }
2d9a8 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20 7d 2c 0a  ,.  { 177, 1 },.
2d9a9 20 20 7b 20 31 37 37 2c 20 30 20 7d 2c 0a 20 20    { 177, 0 },.  
2d9aa 7b 20 32 30 30 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 200, 0 },.  { 
2d9ab 32 30 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30  200, 3 },.  { 20
2d9ac 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 31 2c  1, 0 },.  { 201,
2d9ad 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 30   2 },.  { 203, 0
2d9ae 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 32 20 7d   },.  { 203, 2 }
2d9af 2c 0a 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a  ,.  { 203, 4 },.
2d9b0 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20    { 203, 4 },.  
2d9b1 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 147, 5 },.  { 
2d9b2 31 39 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39  199, 0 },.  { 19
2d9b3 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  9, 2 },.  { 147,
2d9b4 20 37 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 35   7 },.  { 217, 5
2d9b5 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 33 20 7d   },.  { 217, 3 }
2d9b6 2c 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c 0a  ,.  { 147, 8 },.
2d9b7 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20    { 147, 5 },.  
2d9b8 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20  { 147, 6 },.  { 
2d9b9 32 31 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31  218, 2 },.  { 21
2d9ba 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 30 2c  8, 1 },.  { 220,
2d9bb 20 33 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 31   3 },.  { 220, 1
2d9bc 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 30 20 7d   },.  { 219, 0 }
2d9bd 2c 0a 20 20 7b 20 32 31 39 2c 20 33 20 7d 2c 0a  ,.  { 219, 3 },.
2d9be 20 20 7b 20 32 31 33 2c 20 33 20 7d 2c 0a 20 20    { 213, 3 },.  
2d9bf 7b 20 32 31 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 213, 1 },.  { 
2d9c0 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  175, 1 },.  { 17
2d9c1 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34 2c  5, 3 },.  { 174,
2d9c2 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31   1 },.  { 175, 1
2d9c3 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d   },.  { 175, 1 }
2d9c4 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
2d9c5 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20    { 175, 5 },.  
2d9c6 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 174, 1 },.  { 
2d9c7 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  174, 1 },.  { 17
2d9c8 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 1 },.  { 175,
2d9c9 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   1 },.  { 175, 3
2d9ca 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 36 20 7d   },.  { 175, 6 }
2d9cb 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a  ,.  { 175, 5 },.
2d9cc 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20    { 175, 4 },.  
2d9cd 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 174, 1 },.  { 
2d9ce 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  175, 3 },.  { 17
2d9cf 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 3 },.  { 175,
2d9d0 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   3 },.  { 175, 3
2d9d1 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d   },.  { 175, 3 }
2d9d2 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
2d9d3 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
2d9d4 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
2d9d5 32 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32  222, 1 },.  { 22
2d9d6 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 32 2c  2, 2 },.  { 222,
2d9d7 20 31 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 32   1 },.  { 222, 2
2d9d8 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 32 20 7d   },.  { 223, 2 }
2d9d9 2c 0a 20 20 7b 20 32 32 33 2c 20 30 20 7d 2c 0a  ,.  { 223, 0 },.
2d9da 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20    { 175, 4 },.  
2d9db 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 175, 2 },.  { 
2d9dc 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  175, 3 },.  { 17
2d9dd 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 3 },.  { 175,
2d9de 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32   4 },.  { 175, 2
2d9df 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d   },.  { 175, 2 }
2d9e0 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a  ,.  { 175, 2 },.
2d9e1 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20    { 175, 2 },.  
2d9e2 7b 20 32 32 34 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 224, 1 },.  { 
2d9e3 32 32 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  224, 2 },.  { 17
2d9e4 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 35 2c  5, 5 },.  { 225,
2d9e5 20 31 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 32   1 },.  { 225, 2
2d9e6 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d   },.  { 175, 5 }
2d9e7 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
2d9e8 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20    { 175, 5 },.  
2d9e9 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 175, 4 },.  { 
2d9ea 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37  175, 4 },.  { 17
2d9eb 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37 2c  5, 5 },.  { 227,
2d9ec 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 34   5 },.  { 227, 4
2d9ed 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 32 20 7d   },.  { 228, 2 }
2d9ee 2c 0a 20 20 7b 20 32 32 38 2c 20 30 20 7d 2c 0a  ,.  { 228, 0 },.
2d9ef 20 20 7b 20 32 32 36 2c 20 31 20 7d 2c 0a 20 20    { 226, 1 },.  
2d9f0 7b 20 32 32 36 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 226, 0 },.  { 
2d9f1 32 32 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32  221, 1 },.  { 22
2d9f2 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 36 2c  1, 0 },.  { 216,
2d9f3 20 33 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 31   3 },.  { 216, 1
2d9f4 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 31 20   },.  { 147, 11 
2d9f5 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 31 20 7d 2c  },.  { 229, 1 },
2d9f6 0a 20 20 7b 20 32 32 39 2c 20 30 20 7d 2c 0a 20  .  { 229, 0 },. 
2d9f7 20 7b 20 31 37 39 2c 20 30 20 7d 2c 0a 20 20 7b   { 179, 0 },.  {
2d9f8 20 31 37 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   179, 3 },.  { 1
2d9f9 38 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 37  87, 5 },.  { 187
2d9fa 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 30 2c 20  , 3 },.  { 230, 
2d9fb 30 20 7d 2c 0a 20 20 7b 20 32 33 30 2c 20 32 20  0 },.  { 230, 2 
2d9fc 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c  },.  { 147, 4 },
2d9fd 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20  .  { 147, 1 },. 
2d9fe 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b   { 147, 2 },.  {
2d9ff 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   147, 5 },.  { 1
2da00 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 5 },.  { 147
2da01 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 5 },.  { 147, 
2da02 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20  5 },.  { 147, 6 
2da03 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c  },.  { 147, 3 },
2da04 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20  .  { 231, 1 },. 
2da05 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 231, 1 },.  {
2da06 20 31 37 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   170, 2 },.  { 1
2da07 37 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 33  71, 2 },.  { 233
2da08 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20  , 1 },.  { 232, 
2da09 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 30 20  1 },.  { 232, 0 
2da0a 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c  },.  { 147, 5 },
2da0b 0a 20 20 7b 20 32 33 34 2c 20 31 31 20 7d 2c 0a  .  { 234, 11 },.
2da0c 20 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20    { 236, 1 },.  
2da0d 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 236, 1 },.  { 
2da0e 32 33 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33  236, 2 },.  { 23
2da0f 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 37 2c  6, 0 },.  { 237,
2da10 20 31 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 31   1 },.  { 237, 1
2da11 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 33 20 7d   },.  { 237, 3 }
2da12 2c 0a 20 20 7b 20 32 33 38 2c 20 30 20 7d 2c 0a  ,.  { 238, 0 },.
2da13 20 20 7b 20 32 33 38 2c 20 33 20 7d 2c 0a 20 20    { 238, 3 },.  
2da14 7b 20 32 33 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 239, 0 },.  { 
2da15 32 33 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33  239, 2 },.  { 23
2da16 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 35 2c  5, 3 },.  { 235,
2da17 20 32 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 36   2 },.  { 240, 6
2da18 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 38 20 7d   },.  { 240, 8 }
2da19 2c 0a 20 20 7b 20 32 34 30 2c 20 35 20 7d 2c 0a  ,.  { 240, 5 },.
2da1a 20 20 7b 20 32 34 30 2c 20 34 20 7d 2c 0a 20 20    { 240, 4 },.  
2da1b 7b 20 32 34 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 240, 1 },.  { 
2da1c 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37  175, 4 },.  { 17
2da1d 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 39 31 2c  5, 6 },.  { 191,
2da1e 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31   1 },.  { 191, 1
2da1f 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31 20 7d   },.  { 191, 1 }
2da20 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a  ,.  { 147, 4 },.
2da21 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20    { 147, 6 },.  
2da22 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
2da23 32 34 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34  242, 0 },.  { 24
2da24 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34 31 2c  2, 2 },.  { 241,
2da25 20 31 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 30   1 },.  { 241, 0
2da26 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d   },.  { 147, 1 }
2da27 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a  ,.  { 147, 3 },.
2da28 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20    { 147, 1 },.  
2da29 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
2da2a 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34  147, 6 },.  { 14
2da2b 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 34 33 2c  7, 6 },.  { 243,
2da2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 34 2c 20 30   1 },.  { 244, 0
2da2d 20 7d 2c 0a 20 20 7b 20 32 34 34 2c 20 31 20 7d   },.  { 244, 1 }
2da2e 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a  ,.  { 147, 1 },.
2da2f 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20    { 147, 4 },.  
2da30 7b 20 32 34 35 2c 20 37 20 7d 2c 0a 20 20 7b 20  { 245, 7 },.  { 
2da31 32 34 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34  246, 1 },.  { 24
2da32 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 37 2c  6, 3 },.  { 247,
2da33 20 30 20 7d 2c 0a 20 20 7b 20 32 34 37 2c 20 32   0 },.  { 247, 2
2da34 20 7d 2c 0a 20 20 7b 20 32 34 38 2c 20 31 20 7d   },.  { 248, 1 }
2da35 2c 0a 20 20 7b 20 32 34 38 2c 20 33 20 7d 2c 0a  ,.  { 248, 3 },.
2da36 20 20 7b 20 32 34 39 2c 20 31 20 7d 2c 0a 20 20    { 249, 1 },.  
2da37 7b 20 32 35 30 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 250, 0 },.  { 
2da38 32 35 30 2c 20 32 20 7d 2c 0a 7d 3b 0a 0a 73 74  250, 2 },.};..st
2da39 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63 63  atic void yy_acc
2da3a 65 70 74 28 79 79 50 61 72 73 65 72 2a 29 3b 20  ept(yyParser*); 
2da3b 20 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c   /* Forward Decl
2da3c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a  aration */../*.*
2da3d 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65 64 75  * Perform a redu
2da3e 63 65 20 61 63 74 69 6f 6e 20 61 6e 64 20 74 68  ce action and th
2da3f 65 20 73 68 69 66 74 20 74 68 61 74 20 6d 75 73  e shift that mus
2da40 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a  t immediately.**
2da41 20 66 6f 6c 6c 6f 77 20 74 68 65 20 72 65 64 75   follow the redu
2da42 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ce..*/.static vo
2da43 69 64 20 79 79 5f 72 65 64 75 63 65 28 0a 20 20  id yy_reduce(.  
2da44 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72  yyParser *yypPar
2da45 73 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ser,         /* 
2da46 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20  The parser */.  
2da47 69 6e 74 20 79 79 72 75 6c 65 6e 6f 20 20 20 20  int yyruleno    
2da48 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2da49 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 75  Number of the ru
2da4a 6c 65 20 62 79 20 77 68 69 63 68 20 74 6f 20 72  le by which to r
2da4b 65 64 75 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  educe */.){.  in
2da4c 74 20 79 79 67 6f 74 6f 3b 20 20 20 20 20 20 20  t yygoto;       
2da4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2da4e 20 54 68 65 20 6e 65 78 74 20 73 74 61 74 65 20   The next state 
2da4f 2a 2f 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20  */.  int yyact; 
2da50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2da51 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74       /* The next
2da52 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 59 59 4d   action */.  YYM
2da53 49 4e 4f 52 54 59 50 45 20 79 79 67 6f 74 6f 6d  INORTYPE yygotom
2da54 69 6e 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  inor;        /* 
2da55 54 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20 72  The LHS of the r
2da56 75 6c 65 20 72 65 64 75 63 65 64 20 2a 2f 0a 20  ule reduced */. 
2da57 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79   yyStackEntry *y
2da58 79 6d 73 70 3b 20 20 20 20 20 20 20 20 20 20 20  ymsp;           
2da59 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74   /* The top of t
2da5a 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63  he parser's stac
2da5b 6b 20 2a 2f 0a 20 20 69 6e 74 20 79 79 73 69 7a  k */.  int yysiz
2da5c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
2da5d 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74         /* Amount
2da5e 20 74 6f 20 70 6f 70 20 74 68 65 20 73 74 61 63   to pop the stac
2da5f 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61  k */.  sqlite3Pa
2da60 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20  rserARG_FETCH;. 
2da61 20 79 79 6d 73 70 20 3d 20 26 79 79 70 50 61 72   yymsp = &yypPar
2da62 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70  ser->yystack[yyp
2da63 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a  Parser->yyidx];.
2da64 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
2da65 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
2da66 20 26 26 20 79 79 72 75 6c 65 6e 6f 3e 3d 30 20   && yyruleno>=0 
2da67 0a 20 20 20 20 20 20 20 20 26 26 20 79 79 72 75  .        && yyru
2da68 6c 65 6e 6f 3c 28 69 6e 74 29 28 73 69 7a 65 6f  leno<(int)(sizeo
2da69 66 28 79 79 52 75 6c 65 4e 61 6d 65 29 2f 73 69  f(yyRuleName)/si
2da6a 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65 5b  zeof(yyRuleName[
2da6b 30 5d 29 29 20 29 7b 0a 20 20 20 20 66 70 72 69  0])) ){.    fpri
2da6c 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
2da6d 20 22 25 73 52 65 64 75 63 65 20 5b 25 73 5d 2e   "%sReduce [%s].
2da6e 5c 6e 22 2c 20 79 79 54 72 61 63 65 50 72 6f 6d  \n", yyTraceProm
2da6f 70 74 2c 0a 20 20 20 20 20 20 79 79 52 75 6c 65  pt,.      yyRule
2da70 4e 61 6d 65 5b 79 79 72 75 6c 65 6e 6f 5d 29 3b  Name[yyruleno]);
2da71 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  .  }.#endif /* N
2da72 44 45 42 55 47 20 2a 2f 0a 0a 20 20 2f 2a 20 53  DEBUG */..  /* S
2da73 69 6c 65 6e 63 65 20 63 6f 6d 70 6c 61 69 6e 74  ilence complaint
2da74 73 20 66 72 6f 6d 20 70 75 72 69 66 79 20 61 62  s from purify ab
2da75 6f 75 74 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20  out yygotominor 
2da76 62 65 69 6e 67 20 75 6e 69 6e 69 74 69 61 6c 69  being uninitiali
2da77 7a 65 64 0a 20 20 2a 2a 20 69 6e 20 73 6f 6d 65  zed.  ** in some
2da78 20 63 61 73 65 73 20 77 68 65 6e 20 69 74 20 69   cases when it i
2da79 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68  s copied into th
2da7a 65 20 73 74 61 63 6b 20 61 66 74 65 72 20 74 68  e stack after th
2da7b 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a  e following.  **
2da7c 20 73 77 69 74 63 68 2e 20 20 79 79 67 6f 74 6f   switch.  yygoto
2da7d 6d 69 6e 6f 72 20 69 73 20 75 6e 69 6e 69 74 69  minor is uniniti
2da7e 61 6c 69 7a 65 64 20 77 68 65 6e 20 61 20 72 75  alized when a ru
2da7f 6c 65 20 72 65 64 75 63 65 73 20 74 68 61 74 20  le reduces that 
2da80 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 73 65  does.  ** not se
2da81 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 69  t the value of i
2da82 74 73 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64  ts left-hand sid
2da83 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 20 20  e nonterminal.  
2da84 4c 65 61 76 69 6e 67 20 74 68 65 0a 20 20 2a 2a  Leaving the.  **
2da85 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 6f   value of the no
2da86 6e 74 65 72 6d 69 6e 61 6c 20 75 6e 69 6e 69 74  nterminal uninit
2da87 69 61 6c 69 7a 65 64 20 69 73 20 75 74 74 65 72  ialized is utter
2da88 6c 79 20 68 61 72 6d 6c 65 73 73 20 61 73 20 6c  ly harmless as l
2da89 6f 6e 67 0a 20 20 2a 2a 20 61 73 20 74 68 65 20  ong.  ** as the 
2da8a 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 75  value is never u
2da8b 73 65 64 2e 20 20 53 6f 20 72 65 61 6c 6c 79 20  sed.  So really 
2da8c 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74  the only thing t
2da8d 68 69 73 20 63 6f 64 65 0a 20 20 2a 2a 20 61 63  his code.  ** ac
2da8e 63 6f 6d 70 6c 69 73 68 65 73 20 69 73 20 74 6f  complishes is to
2da8f 20 71 75 69 65 74 65 6e 20 70 75 72 69 66 79 2e   quieten purify.
2da90 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30    .  **.  ** 200
2da91 37 2d 30 31 2d 31 36 3a 20 20 54 68 65 20 77 69  7-01-16:  The wi
2da92 72 65 73 68 61 72 6b 20 70 72 6f 6a 65 63 74 20  reshark project 
2da93 28 77 77 77 2e 77 69 72 65 73 68 61 72 6b 2e 6f  (www.wireshark.o
2da94 72 67 29 20 72 65 70 6f 72 74 73 20 74 68 61 74  rg) reports that
2da95 0a 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 74 68  .  ** without th
2da96 69 73 20 63 6f 64 65 2c 20 74 68 65 69 72 20 70  is code, their p
2da97 61 72 73 65 72 20 73 65 67 66 61 75 6c 74 73 2e  arser segfaults.
2da98 20 20 49 27 6d 20 6e 6f 74 20 73 75 72 65 20 77    I'm not sure w
2da99 68 61 74 20 74 68 65 72 65 0a 20 20 2a 2a 20 70  hat there.  ** p
2da9a 61 72 73 65 72 20 69 73 20 64 6f 69 6e 67 20 74  arser is doing t
2da9b 6f 20 6d 61 6b 65 20 74 68 69 73 20 68 61 70 70  o make this happ
2da9c 65 6e 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  en.  This is the
2da9d 20 73 65 63 6f 6e 64 20 62 75 67 20 72 65 70 6f   second bug repo
2da9e 72 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 77 69 72  rt.  ** from wir
2da9f 65 73 68 61 72 6b 20 74 68 69 73 20 77 65 65 6b  eshark this week
2daa0 2e 20 20 43 6c 65 61 72 6c 79 20 74 68 65 79 20  .  Clearly they 
2daa1 61 72 65 20 73 74 72 65 73 73 69 6e 67 20 4c 65  are stressing Le
2daa2 6d 6f 6e 20 69 6e 20 77 61 79 73 0a 20 20 2a 2a  mon in ways.  **
2daa3 20 74 68 61 74 20 69 74 20 68 61 73 20 6e 6f 74   that it has not
2daa4 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79   been previously
2daa5 20 73 74 72 65 73 73 65 64 2e 2e 2e 20 20 28 53   stressed...  (S
2daa6 51 4c 69 74 65 20 74 69 63 6b 65 74 20 23 32 31  QLite ticket #21
2daa7 37 32 29 0a 20 20 2a 2f 0a 20 20 2f 2a 6d 65 6d  72).  */.  /*mem
2daa8 73 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  set(&yygotominor
2daa9 2c 20 30 2c 20 73 69 7a 65 6f 66 28 79 79 67 6f  , 0, sizeof(yygo
2daaa 74 6f 6d 69 6e 6f 72 29 29 3b 2a 2f 0a 20 20 79  tominor));*/.  y
2daab 79 67 6f 74 6f 6d 69 6e 6f 72 20 3d 20 79 79 7a  ygotominor = yyz
2daac 65 72 6f 6d 69 6e 6f 72 3b 0a 0a 0a 20 20 73 77  erominor;...  sw
2daad 69 74 63 68 28 20 79 79 72 75 6c 65 6e 6f 20 29  itch( yyruleno )
2daae 7b 0a 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67  {.  /* Beginning
2daaf 20 68 65 72 65 20 61 72 65 20 74 68 65 20 72 65   here are the re
2dab0 64 75 63 74 69 6f 6e 20 63 61 73 65 73 2e 20 20  duction cases.  
2dab1 41 20 74 79 70 69 63 61 6c 20 65 78 61 6d 70 6c  A typical exampl
2dab2 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a  e.  ** follows:.
2dab3 20 20 2a 2a 20 20 20 63 61 73 65 20 30 3a 0a 20    **   case 0:. 
2dab4 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e 65   **  #line <line
2dab5 6e 6f 3e 20 3c 67 72 61 6d 6d 61 72 66 69 6c 65  no> <grammarfile
2dab6 3e 0a 20 20 2a 2a 20 20 20 20 20 7b 20 2e 2e 2e  >.  **     { ...
2dab7 20 7d 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20   }           // 
2dab8 55 73 65 72 20 73 75 70 70 6c 69 65 64 20 63 6f  User supplied co
2dab9 64 65 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c  de.  **  #line <
2daba 6c 69 6e 65 6e 6f 3e 20 3c 74 68 69 73 66 69 6c  lineno> <thisfil
2dabb 65 3e 0a 20 20 2a 2a 20 20 20 20 20 62 72 65 61  e>.  **     brea
2dabc 6b 3b 0a 20 20 2a 2f 0a 20 20 20 20 20 20 63 61  k;.  */.      ca
2dabd 73 65 20 30 3a 20 2f 2a 20 69 6e 70 75 74 20 3a  se 0: /* input :
2dabe 3a 3d 20 63 6d 64 6c 69 73 74 20 2a 2f 0a 20 20  := cmdlist */.  
2dabf 20 20 20 20 63 61 73 65 20 31 3a 20 2f 2a 20 63      case 1: /* c
2dac0 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69  mdlist ::= cmdli
2dac1 73 74 20 65 63 6d 64 20 2a 2f 0a 20 20 20 20 20  st ecmd */.     
2dac2 20 63 61 73 65 20 32 3a 20 2f 2a 20 63 6d 64 6c   case 2: /* cmdl
2dac3 69 73 74 20 3a 3a 3d 20 65 63 6d 64 20 2a 2f 0a  ist ::= ecmd */.
2dac4 20 20 20 20 20 20 63 61 73 65 20 33 3a 20 2f 2a        case 3: /*
2dac5 20 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 20 2a   ecmd ::= SEMI *
2dac6 2f 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 20  /.      case 4: 
2dac7 2f 2a 20 65 63 6d 64 20 3a 3a 3d 20 65 78 70 6c  /* ecmd ::= expl
2dac8 61 69 6e 20 63 6d 64 78 20 53 45 4d 49 20 2a 2f  ain cmdx SEMI */
2dac9 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 3a 20  .      case 10: 
2daca 2f 2a 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d  /* trans_opt ::=
2dacb 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2dacc 31 3a 20 2f 2a 20 74 72 61 6e 73 5f 6f 70 74 20  1: /* trans_opt 
2dacd 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20  ::= TRANSACTION 
2dace 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 32  */.      case 12
2dacf 3a 20 2f 2a 20 74 72 61 6e 73 5f 6f 70 74 20 3a  : /* trans_opt :
2dad0 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e  := TRANSACTION n
2dad1 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  m */.      case 
2dad2 32 30 3a 20 2f 2a 20 73 61 76 65 70 6f 69 6e 74  20: /* savepoint
2dad3 5f 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49  _opt ::= SAVEPOI
2dad4 4e 54 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  NT */.      case
2dad5 20 32 31 3a 20 2f 2a 20 73 61 76 65 70 6f 69 6e   21: /* savepoin
2dad6 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20  t_opt ::= */.   
2dad7 20 20 20 63 61 73 65 20 32 35 3a 20 2f 2a 20 63     case 25: /* c
2dad8 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61  md ::= create_ta
2dad9 62 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65  ble create_table
2dada 5f 61 72 67 73 20 2a 2f 0a 20 20 20 20 20 20 63  _args */.      c
2dadb 61 73 65 20 33 34 3a 20 2f 2a 20 63 6f 6c 75 6d  ase 34: /* colum
2dadc 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e  nlist ::= column
2dadd 6c 69 73 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d  list COMMA colum
2dade 6e 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  n */.      case 
2dadf 33 35 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 6c 69 73  35: /* columnlis
2dae0 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  t ::= column */.
2dae1 20 20 20 20 20 20 63 61 73 65 20 34 34 3a 20 2f        case 44: /
2dae2 2a 20 74 79 70 65 20 3a 3a 3d 20 2a 2f 0a 20 20  * type ::= */.  
2dae3 20 20 20 20 63 61 73 65 20 35 31 3a 20 2f 2a 20      case 51: /* 
2dae4 73 69 67 6e 65 64 20 3a 3a 3d 20 70 6c 75 73 5f  signed ::= plus_
2dae5 6e 75 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  num */.      cas
2dae6 65 20 35 32 3a 20 2f 2a 20 73 69 67 6e 65 64 20  e 52: /* signed 
2dae7 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f  ::= minus_num */
2dae8 0a 20 20 20 20 20 20 63 61 73 65 20 35 33 3a 20  .      case 53: 
2dae9 2f 2a 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20  /* carglist ::= 
2daea 63 61 72 67 6c 69 73 74 20 63 61 72 67 20 2a 2f  carglist carg */
2daeb 0a 20 20 20 20 20 20 63 61 73 65 20 35 34 3a 20  .      case 54: 
2daec 2f 2a 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20  /* carglist ::= 
2daed 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 35 35  */.      case 55
2daee 3a 20 2f 2a 20 63 61 72 67 20 3a 3a 3d 20 43 4f  : /* carg ::= CO
2daef 4e 53 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e  NSTRAINT nm ccon
2daf0 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  s */.      case 
2daf1 35 36 3a 20 2f 2a 20 63 61 72 67 20 3a 3a 3d 20  56: /* carg ::= 
2daf2 63 63 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63  ccons */.      c
2daf3 61 73 65 20 36 32 3a 20 2f 2a 20 63 63 6f 6e 73  ase 62: /* ccons
2daf4 20 3a 3a 3d 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66   ::= NULL onconf
2daf5 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38   */.      case 8
2daf6 39 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 20 3a  9: /* conslist :
2daf7 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d  := conslist COMM
2daf8 41 20 74 63 6f 6e 73 20 2a 2f 0a 20 20 20 20 20  A tcons */.     
2daf9 20 63 61 73 65 20 39 30 3a 20 2f 2a 20 63 6f 6e   case 90: /* con
2dafa 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69  slist ::= consli
2dafb 73 74 20 74 63 6f 6e 73 20 2a 2f 0a 20 20 20 20  st tcons */.    
2dafc 20 20 63 61 73 65 20 39 31 3a 20 2f 2a 20 63 6f    case 91: /* co
2dafd 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73  nslist ::= tcons
2dafe 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39   */.      case 9
2daff 32 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20  2: /* tcons ::= 
2db00 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20 2a 2f  CONSTRAINT nm */
2db01 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 36 3a  .      case 266:
2db02 20 2f 2a 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d   /* plus_opt ::=
2db03 20 50 4c 55 53 20 2a 2f 0a 20 20 20 20 20 20 63   PLUS */.      c
2db04 61 73 65 20 32 36 37 3a 20 2f 2a 20 70 6c 75 73  ase 267: /* plus
2db05 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2db06 20 20 63 61 73 65 20 32 37 37 3a 20 2f 2a 20 66    case 277: /* f
2db07 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a  oreach_clause ::
2db08 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2db09 32 37 38 3a 20 2f 2a 20 66 6f 72 65 61 63 68 5f  278: /* foreach_
2db0a 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45  clause ::= FOR E
2db0b 41 43 48 20 52 4f 57 20 2a 2f 0a 20 20 20 20 20  ACH ROW */.     
2db0c 20 63 61 73 65 20 32 39 38 3a 20 2f 2a 20 64 61   case 298: /* da
2db0d 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a  tabase_kw_opt ::
2db0e 3d 20 44 41 54 41 42 41 53 45 20 2a 2f 0a 20 20  = DATABASE */.  
2db0f 20 20 20 20 63 61 73 65 20 32 39 39 3a 20 2f 2a      case 299: /*
2db10 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74   database_kw_opt
2db11 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2db12 73 65 20 33 30 37 3a 20 2f 2a 20 6b 77 63 6f 6c  se 307: /* kwcol
2db13 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  umn_opt ::= */. 
2db14 20 20 20 20 20 63 61 73 65 20 33 30 38 3a 20 2f       case 308: /
2db15 2a 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a  * kwcolumn_opt :
2db16 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 20 2a 2f 0a 20  := COLUMNKW */. 
2db17 20 20 20 20 20 63 61 73 65 20 33 31 32 3a 20 2f       case 312: /
2db18 2a 20 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a  * vtabarglist ::
2db19 3d 20 76 74 61 62 61 72 67 20 2a 2f 0a 20 20 20  = vtabarg */.   
2db1a 20 20 20 63 61 73 65 20 33 31 33 3a 20 2f 2a 20     case 313: /* 
2db1b 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20  vtabarglist ::= 
2db1c 76 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d  vtabarglist COMM
2db1d 41 20 76 74 61 62 61 72 67 20 2a 2f 0a 20 20 20  A vtabarg */.   
2db1e 20 20 20 63 61 73 65 20 33 31 35 3a 20 2f 2a 20     case 315: /* 
2db1f 76 74 61 62 61 72 67 20 3a 3a 3d 20 76 74 61 62  vtabarg ::= vtab
2db20 61 72 67 20 76 74 61 62 61 72 67 74 6f 6b 65 6e  arg vtabargtoken
2db21 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
2db22 31 39 3a 20 2f 2a 20 61 6e 79 6c 69 73 74 20 3a  19: /* anylist :
2db23 3a 3d 20 2a 2f 0a 7b 0a 7d 0a 20 20 20 20 20 20  := */.{.}.      
2db24 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2db25 61 73 65 20 35 3a 20 2f 2a 20 65 78 70 6c 61 69  ase 5: /* explai
2db26 6e 20 3a 3a 3d 20 2a 2f 0a 7b 20 73 71 6c 69 74  n ::= */.{ sqlit
2db27 65 33 42 65 67 69 6e 50 61 72 73 65 28 70 50 61  e3BeginParse(pPa
2db28 72 73 65 2c 20 30 29 3b 20 7d 0a 20 20 20 20 20  rse, 0); }.     
2db29 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2db2a 63 61 73 65 20 36 3a 20 2f 2a 20 65 78 70 6c 61  case 6: /* expla
2db2b 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 2a  in ::= EXPLAIN *
2db2c 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e  /.{ sqlite3Begin
2db2d 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 31 29  Parse(pParse, 1)
2db2e 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
2db2f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 3a  k;.      case 7:
2db30 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20   /* explain ::= 
2db31 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c  EXPLAIN QUERY PL
2db32 41 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42  AN */.{ sqlite3B
2db33 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65  eginParse(pParse
2db34 2c 20 32 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  , 2); }.        
2db35 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2db36 65 20 38 3a 20 2f 2a 20 63 6d 64 78 20 3a 3a 3d  e 8: /* cmdx ::=
2db37 20 63 6d 64 20 2a 2f 0a 7b 20 73 71 6c 69 74 65   cmd */.{ sqlite
2db38 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 70 50  3FinishCoding(pP
2db39 61 72 73 65 29 3b 20 7d 0a 20 20 20 20 20 20 20  arse); }.       
2db3a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2db3b 73 65 20 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  se 9: /* cmd ::=
2db3c 20 42 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65   BEGIN transtype
2db3d 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73   trans_opt */.{s
2db3e 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73  qlite3BeginTrans
2db3f 61 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79  action(pParse, y
2db40 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2db41 79 31 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20  y194);}.        
2db42 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2db43 65 20 31 33 3a 20 2f 2a 20 74 72 61 6e 73 74 79  e 13: /* transty
2db44 70 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  pe ::= */.{yygot
2db45 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 54  ominor.yy194 = T
2db46 4b 5f 44 45 46 45 52 52 45 44 3b 7d 0a 20 20 20  K_DEFERRED;}.   
2db47 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2db48 20 20 63 61 73 65 20 31 34 3a 20 2f 2a 20 74 72    case 14: /* tr
2db49 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44 45 46 45  anstype ::= DEFE
2db4a 52 52 45 44 20 2a 2f 0a 20 20 20 20 20 20 63 61  RRED */.      ca
2db4b 73 65 20 31 35 3a 20 2f 2a 20 74 72 61 6e 73 74  se 15: /* transt
2db4c 79 70 65 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54  ype ::= IMMEDIAT
2db4d 45 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  E */.      case 
2db4e 31 36 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65  16: /* transtype
2db4f 20 3a 3a 3d 20 45 58 43 4c 55 53 49 56 45 20 2a   ::= EXCLUSIVE *
2db50 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 34  /.      case 114
2db51 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74  : /* multiselect
2db52 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 2a 2f  _op ::= UNION */
2db53 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 36 3a  .      case 116:
2db54 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f   /* multiselect_
2db55 6f 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e  op ::= EXCEPT|IN
2db56 54 45 52 53 45 43 54 20 2a 2f 0a 7b 79 79 67 6f  TERSECT */.{yygo
2db57 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20  tominor.yy194 = 
2db58 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d  yymsp[0].major;}
2db59 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2db5a 20 20 20 20 20 20 63 61 73 65 20 31 37 3a 20 2f        case 17: /
2db5b 2a 20 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54  * cmd ::= COMMIT
2db5c 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 20 20   trans_opt */.  
2db5d 20 20 20 20 63 61 73 65 20 31 38 3a 20 2f 2a 20      case 18: /* 
2db5e 63 6d 64 20 3a 3a 3d 20 45 4e 44 20 74 72 61 6e  cmd ::= END tran
2db5f 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65  s_opt */.{sqlite
2db60 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69  3CommitTransacti
2db61 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20  on(pParse);}.   
2db62 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2db63 20 20 63 61 73 65 20 31 39 3a 20 2f 2a 20 63 6d    case 19: /* cm
2db64 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74  d ::= ROLLBACK t
2db65 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c  rans_opt */.{sql
2db66 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e  ite3RollbackTran
2db67 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65 29 3b  saction(pParse);
2db68 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2db69 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 3a 20  .      case 22: 
2db6a 2f 2a 20 63 6d 64 20 3a 3a 3d 20 53 41 56 45 50  /* cmd ::= SAVEP
2db6b 4f 49 4e 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73  OINT nm */.{.  s
2db6c 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28  qlite3Savepoint(
2db6d 70 50 61 72 73 65 2c 20 53 41 56 45 50 4f 49 4e  pParse, SAVEPOIN
2db6e 54 5f 42 45 47 49 4e 2c 20 26 79 79 6d 73 70 5b  T_BEGIN, &yymsp[
2db6f 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
2db70 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2db71 20 20 20 20 20 20 63 61 73 65 20 32 33 3a 20 2f        case 23: /
2db72 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 4c 45 41 53  * cmd ::= RELEAS
2db73 45 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20  E savepoint_opt 
2db74 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  nm */.{.  sqlite
2db75 33 53 61 76 65 70 6f 69 6e 74 28 70 50 61 72 73  3Savepoint(pPars
2db76 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  e, SAVEPOINT_REL
2db77 45 41 53 45 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  EASE, &yymsp[0].
2db78 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
2db79 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2db7a 20 20 20 63 61 73 65 20 32 34 3a 20 2f 2a 20 63     case 24: /* c
2db7b 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20  md ::= ROLLBACK 
2db7c 74 72 61 6e 73 5f 6f 70 74 20 54 4f 20 73 61 76  trans_opt TO sav
2db7d 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f  epoint_opt nm */
2db7e 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65  .{.  sqlite3Save
2db7f 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41  point(pParse, SA
2db80 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
2db81 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2db82 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
2db83 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2db84 61 73 65 20 32 36 3a 20 2f 2a 20 63 72 65 61 74  ase 26: /* creat
2db85 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 63 72 65 61  e_table ::= crea
2db86 74 65 6b 77 20 74 65 6d 70 20 54 41 42 4c 45 20  tekw temp TABLE 
2db87 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64  ifnotexists nm d
2db88 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 73 71 6c 69  bnm */.{.   sqli
2db89 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70 50  te3StartTable(pP
2db8a 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
2db8b 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2db8c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79  [0].minor.yy0,yy
2db8d 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2db8e 31 39 34 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d 32  194,0,0,yymsp[-2
2db8f 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 0a  ].minor.yy194);.
2db90 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2db91 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 3a 20  .      case 27: 
2db92 2f 2a 20 63 72 65 61 74 65 6b 77 20 3a 3a 3d 20  /* createkw ::= 
2db93 43 52 45 41 54 45 20 2a 2f 0a 7b 0a 20 20 70 50  CREATE */.{.  pP
2db94 61 72 73 65 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73  arse->db->lookas
2db95 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30  ide.bEnabled = 0
2db96 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
2db97 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  yy0 = yymsp[0].m
2db98 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20 20 20 20  inor.yy0;.}.    
2db99 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2db9a 20 63 61 73 65 20 32 38 3a 20 2f 2a 20 69 66 6e   case 28: /* ifn
2db9b 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 2a 2f 0a  otexists ::= */.
2db9c 20 20 20 20 20 20 63 61 73 65 20 33 31 3a 20 2f        case 31: /
2db9d 2a 20 74 65 6d 70 20 3a 3a 3d 20 2a 2f 0a 20 20  * temp ::= */.  
2db9e 20 20 20 20 63 61 73 65 20 37 30 3a 20 2f 2a 20      case 70: /* 
2db9f 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 2a 2f 0a 20  autoinc ::= */. 
2dba0 20 20 20 20 20 63 61 73 65 20 38 34 3a 20 2f 2a       case 84: /*
2dba1 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
2dba2 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  red_opt ::= */. 
2dba3 20 20 20 20 20 63 61 73 65 20 38 36 3a 20 2f 2a       case 86: /*
2dba4 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
2dba5 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54  red_opt ::= INIT
2dba6 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20  IALLY IMMEDIATE 
2dba7 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39 37  */.      case 97
2dba8 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c  : /* defer_subcl
2dba9 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  ause_opt ::= */.
2dbaa 20 20 20 20 20 20 63 61 73 65 20 31 30 38 3a 20        case 108: 
2dbab 2f 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20  /* ifexists ::= 
2dbac 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  */.      case 11
2dbad 39 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a  9: /* distinct :
2dbae 3a 3d 20 41 4c 4c 20 2a 2f 0a 20 20 20 20 20 20  := ALL */.      
2dbaf 63 61 73 65 20 31 32 30 3a 20 2f 2a 20 64 69 73  case 120: /* dis
2dbb0 74 69 6e 63 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20  tinct ::= */.   
2dbb1 20 20 20 63 61 73 65 20 32 32 32 3a 20 2f 2a 20     case 222: /* 
2dbb2 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42  between_op ::= B
2dbb3 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20  ETWEEN */.      
2dbb4 63 61 73 65 20 32 32 35 3a 20 2f 2a 20 69 6e 5f  case 225: /* in_
2dbb5 6f 70 20 3a 3a 3d 20 49 4e 20 2a 2f 0a 7b 79 79  op ::= IN */.{yy
2dbb6 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20  gotominor.yy194 
2dbb7 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
2dbb8 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2dbb9 32 39 3a 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73  29: /* ifnotexis
2dbba 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58  ts ::= IF NOT EX
2dbbb 49 53 54 53 20 2a 2f 0a 20 20 20 20 20 20 63 61  ISTS */.      ca
2dbbc 73 65 20 33 30 3a 20 2f 2a 20 74 65 6d 70 20 3a  se 30: /* temp :
2dbbd 3a 3d 20 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20  := TEMP */.     
2dbbe 20 63 61 73 65 20 37 31 3a 20 2f 2a 20 61 75 74   case 71: /* aut
2dbbf 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43  oinc ::= AUTOINC
2dbc0 52 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  R */.      case 
2dbc1 38 35 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65  85: /* init_defe
2dbc2 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
2dbc3 3d 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  = INITIALLY DEFE
2dbc4 52 52 45 44 20 2a 2f 0a 20 20 20 20 20 20 63 61  RRED */.      ca
2dbc5 73 65 20 31 30 37 3a 20 2f 2a 20 69 66 65 78 69  se 107: /* ifexi
2dbc6 73 74 73 20 3a 3a 3d 20 49 46 20 45 58 49 53 54  sts ::= IF EXIST
2dbc7 53 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  S */.      case 
2dbc8 31 31 38 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74  118: /* distinct
2dbc9 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54 20 2a 2f   ::= DISTINCT */
2dbca 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 33 3a  .      case 223:
2dbcb 20 2f 2a 20 62 65 74 77 65 65 6e 5f 6f 70 20 3a   /* between_op :
2dbcc 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45 4e 20 2a  := NOT BETWEEN *
2dbcd 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 36  /.      case 226
2dbce 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 4e  : /* in_op ::= N
2dbcf 4f 54 20 49 4e 20 2a 2f 0a 7b 79 79 67 6f 74 6f  OT IN */.{yygoto
2dbd0 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 31 3b  minor.yy194 = 1;
2dbd1 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2dbd2 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 3a 20  .      case 32: 
2dbd3 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  /* create_table_
2dbd4 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75  args ::= LP colu
2dbd5 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f  mnlist conslist_
2dbd6 6f 70 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71  opt RP */.{.  sq
2dbd7 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50  lite3EndTable(pP
2dbd8 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
2dbd9 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2dbda 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29  [0].minor.yy0,0)
2dbdb 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2dbdc 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 33  k;.      case 33
2dbdd 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c  : /* create_tabl
2dbde 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65  e_args ::= AS se
2dbdf 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  lect */.{.  sqli
2dbe0 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72  te3EndTable(pPar
2dbe1 73 65 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e  se,0,0,yymsp[0].
2dbe2 6d 69 6e 6f 72 2e 79 79 32 34 33 29 3b 0a 20 20  minor.yy243);.  
2dbe3 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2dbe4 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
2dbe5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2dbe6 79 32 34 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y243);.}.       
2dbe7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2dbe8 73 65 20 33 36 3a 20 2f 2a 20 63 6f 6c 75 6d 6e  se 36: /* column
2dbe9 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79   ::= columnid ty
2dbea 70 65 20 63 61 72 67 6c 69 73 74 20 2a 2f 0a 7b  pe carglist */.{
2dbeb 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2dbec 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  y0.z = yymsp[-2]
2dbed 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
2dbee 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
2dbef 6e 20 3d 20 28 69 6e 74 29 28 70 50 61 72 73 65  n = (int)(pParse
2dbf0 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79  ->sLastToken.z-y
2dbf1 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2dbf2 79 30 2e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e  y0.z) + pParse->
2dbf3 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a  sLastToken.n;.}.
2dbf4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2dbf5 20 20 20 20 20 63 61 73 65 20 33 37 3a 20 2f 2a       case 37: /*
2dbf6 20 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d   columnid ::= nm
2dbf7 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41   */.{.  sqlite3A
2dbf8 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  ddColumn(pParse,
2dbf9 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2dbfa 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  yy0);.  yygotomi
2dbfb 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b  nor.yy0 = yymsp[
2dbfc 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a  0].minor.yy0;.}.
2dbfd 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2dbfe 20 20 20 20 20 63 61 73 65 20 33 38 3a 20 2f 2a       case 38: /*
2dbff 20 69 64 20 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20   id ::= ID */.  
2dc00 20 20 20 20 63 61 73 65 20 33 39 3a 20 2f 2a 20      case 39: /* 
2dc01 69 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 2a  id ::= INDEXED *
2dc02 2f 0a 20 20 20 20 20 20 63 61 73 65 20 34 30 3a  /.      case 40:
2dc03 20 2f 2a 20 69 64 73 20 3a 3a 3d 20 49 44 7c 53   /* ids ::= ID|S
2dc04 54 52 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 63  TRING */.      c
2dc05 61 73 65 20 34 31 3a 20 2f 2a 20 6e 6d 20 3a 3a  ase 41: /* nm ::
2dc06 3d 20 69 64 20 2a 2f 0a 20 20 20 20 20 20 63 61  = id */.      ca
2dc07 73 65 20 34 32 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d  se 42: /* nm ::=
2dc08 20 53 54 52 49 4e 47 20 2a 2f 0a 20 20 20 20 20   STRING */.     
2dc09 20 63 61 73 65 20 34 33 3a 20 2f 2a 20 6e 6d 20   case 43: /* nm 
2dc0a 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 20  ::= JOIN_KW */. 
2dc0b 20 20 20 20 20 63 61 73 65 20 34 36 3a 20 2f 2a       case 46: /*
2dc0c 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74   typetoken ::= t
2dc0d 79 70 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  ypename */.     
2dc0e 20 63 61 73 65 20 34 39 3a 20 2f 2a 20 74 79 70   case 49: /* typ
2dc0f 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73 20 2a 2f  ename ::= ids */
2dc10 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 36 3a  .      case 126:
2dc11 20 2f 2a 20 61 73 20 3a 3a 3d 20 41 53 20 6e 6d   /* as ::= AS nm
2dc12 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2dc13 32 37 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 69 64  27: /* as ::= id
2dc14 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  s */.      case 
2dc15 31 33 37 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d  137: /* dbnm ::=
2dc16 20 44 4f 54 20 6e 6d 20 2a 2f 0a 20 20 20 20 20   DOT nm */.     
2dc17 20 63 61 73 65 20 31 34 36 3a 20 2f 2a 20 69 6e   case 146: /* in
2dc18 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e  dexed_opt ::= IN
2dc19 44 45 58 45 44 20 42 59 20 6e 6d 20 2a 2f 0a 20  DEXED BY nm */. 
2dc1a 20 20 20 20 20 63 61 73 65 20 32 35 31 3a 20 2f       case 251: /
2dc1b 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f  * collate ::= CO
2dc1c 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 20 20 20  LLATE ids */.   
2dc1d 20 20 20 63 61 73 65 20 32 36 31 3a 20 2f 2a 20     case 261: /* 
2dc1e 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e  nmnum ::= plus_n
2dc1f 75 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  um */.      case
2dc20 20 32 36 32 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a   262: /* nmnum :
2dc21 3a 3d 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63  := nm */.      c
2dc22 61 73 65 20 32 36 33 3a 20 2f 2a 20 70 6c 75 73  ase 263: /* plus
2dc23 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70  _num ::= plus_op
2dc24 74 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  t number */.    
2dc25 20 20 63 61 73 65 20 32 36 34 3a 20 2f 2a 20 6d    case 264: /* m
2dc26 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49 4e  inus_num ::= MIN
2dc27 55 53 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20  US number */.   
2dc28 20 20 20 63 61 73 65 20 32 36 35 3a 20 2f 2a 20     case 265: /* 
2dc29 6e 75 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47  number ::= INTEG
2dc2a 45 52 7c 46 4c 4f 41 54 20 2a 2f 0a 7b 79 79 67  ER|FLOAT */.{yyg
2dc2b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79  otominor.yy0 = y
2dc2c 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2dc2d 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2dc2e 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 35  k;.      case 45
2dc2f 3a 20 2f 2a 20 74 79 70 65 20 3a 3a 3d 20 74 79  : /* type ::= ty
2dc30 70 65 74 6f 6b 65 6e 20 2a 2f 0a 7b 73 71 6c 69  petoken */.{sqli
2dc31 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  te3AddColumnType
2dc32 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30  (pParse,&yymsp[0
2dc33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
2dc34 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2dc35 20 20 20 20 63 61 73 65 20 34 37 3a 20 2f 2a 20      case 47: /* 
2dc36 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
2dc37 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64  pename LP signed
2dc38 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
2dc39 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79  ominor.yy0.z = y
2dc3a 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
2dc3b 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y0.z;.  yygotomi
2dc3c 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74  nor.yy0.n = (int
2dc3d 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  )(&yymsp[0].mino
2dc3e 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d  r.yy0.z[yymsp[0]
2dc3f 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20  .minor.yy0.n] - 
2dc40 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2dc41 79 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20 20 20 20  yy0.z);.}.      
2dc42 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2dc43 61 73 65 20 34 38 3a 20 2f 2a 20 74 79 70 65 74  ase 48: /* typet
2dc44 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d  oken ::= typenam
2dc45 65 20 4c 50 20 73 69 67 6e 65 64 20 43 4f 4d 4d  e LP signed COMM
2dc46 41 20 73 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b  A signed RP */.{
2dc47 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2dc48 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 35 5d  y0.z = yymsp[-5]
2dc49 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
2dc4a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
2dc4b 6e 20 3d 20 28 69 6e 74 29 28 26 79 79 6d 73 70  n = (int)(&yymsp
2dc4c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b  [0].minor.yy0.z[
2dc4d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2dc4e 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 35  y0.n] - yymsp[-5
2dc4f 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 0a  ].minor.yy0.z);.
2dc50 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2dc51 0a 20 20 20 20 20 20 63 61 73 65 20 35 30 3a 20  .      case 50: 
2dc52 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  /* typename ::= 
2dc53 74 79 70 65 6e 61 6d 65 20 69 64 73 20 2a 2f 0a  typename ids */.
2dc54 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
2dc55 2e 7a 3d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  .z=yymsp[-1].min
2dc56 6f 72 2e 79 79 30 2e 7a 3b 20 79 79 67 6f 74 6f  or.yy0.z; yygoto
2dc57 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 79 79 6d 73  minor.yy0.n=yyms
2dc58 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
2dc59 2b 28 69 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e  +(int)(yymsp[0].
2dc5a 6d 69 6e 6f 72 2e 79 79 30 2e 7a 2d 79 79 6d 73  minor.yy0.z-yyms
2dc5b 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-1].minor.yy0.
2dc5c 7a 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  z);}.        bre
2dc5d 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35  ak;.      case 5
2dc5e 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  7: /* ccons ::= 
2dc5f 44 45 46 41 55 4c 54 20 74 65 72 6d 20 2a 2f 0a  DEFAULT term */.
2dc60 20 20 20 20 20 20 63 61 73 65 20 35 39 3a 20 2f        case 59: /
2dc61 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41  * ccons ::= DEFA
2dc62 55 4c 54 20 50 4c 55 53 20 74 65 72 6d 20 2a 2f  ULT PLUS term */
2dc63 0a 7b 73 71 6c 69 74 65 33 41 64 64 44 65 66 61  .{sqlite3AddDefa
2dc64 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c  ultValue(pParse,
2dc65 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2dc66 79 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  y72);}.        b
2dc67 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2dc68 20 35 38 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   58: /* ccons ::
2dc69 3d 20 44 45 46 41 55 4c 54 20 4c 50 20 65 78 70  = DEFAULT LP exp
2dc6a 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  r RP */.{sqlite3
2dc6b 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28  AddDefaultValue(
2dc6c 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d  pParse,yymsp[-1]
2dc6d 2e 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 7d 0a 20  .minor.yy72);}. 
2dc6e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2dc6f 20 20 20 20 63 61 73 65 20 36 30 3a 20 2f 2a 20      case 60: /* 
2dc70 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
2dc71 54 20 4d 49 4e 55 53 20 74 65 72 6d 20 2a 2f 0a  T MINUS term */.
2dc72 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71  {.  Expr *p = sq
2dc73 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2dc74 65 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c 20 79 79  e, TK_UMINUS, yy
2dc75 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37  msp[0].minor.yy7
2dc76 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69  2, 0, 0);.  sqli
2dc77 74 65 33 45 78 70 72 53 70 61 6e 28 70 2c 26 79  te3ExprSpan(p,&y
2dc78 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2dc79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2dc7a 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 29 3b 0a  or.yy72->span);.
2dc7b 20 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61    sqlite3AddDefa
2dc7c 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c  ultValue(pParse,
2dc7d 70 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  p);.}.        br
2dc7e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2dc7f 36 31 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  61: /* ccons ::=
2dc80 20 44 45 46 41 55 4c 54 20 69 64 20 2a 2f 0a 7b   DEFAULT id */.{
2dc81 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c  .  Expr *p = sql
2dc82 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2dc83 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20 30 2c 20  , TK_STRING, 0, 
2dc84 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
2dc85 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74  or.yy0);.  sqlit
2dc86 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75  e3AddDefaultValu
2dc87 65 28 70 50 61 72 73 65 2c 70 29 3b 0a 7d 0a 20  e(pParse,p);.}. 
2dc88 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2dc89 20 20 20 20 63 61 73 65 20 36 33 3a 20 2f 2a 20      case 63: /* 
2dc8a 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f 54 20 4e 55  ccons ::= NOT NU
2dc8b 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71  LL onconf */.{sq
2dc8c 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28  lite3AddNotNull(
2dc8d 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
2dc8e 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a  .minor.yy194);}.
2dc8f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2dc90 20 20 20 20 20 63 61 73 65 20 36 34 3a 20 2f 2a       case 64: /*
2dc91 20 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41   ccons ::= PRIMA
2dc92 52 59 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72  RY KEY sortorder
2dc93 20 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 20   onconf autoinc 
2dc94 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 50 72  */.{sqlite3AddPr
2dc95 69 6d 61 72 79 4b 65 79 28 70 50 61 72 73 65 2c  imaryKey(pParse,
2dc96 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  0,yymsp[-1].mino
2dc97 72 2e 79 79 31 39 34 2c 79 79 6d 73 70 5b 30 5d  r.yy194,yymsp[0]
2dc98 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 79 79 6d  .minor.yy194,yym
2dc99 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2dc9a 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  94);}.        br
2dc9b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2dc9c 36 35 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  65: /* ccons ::=
2dc9d 20 55 4e 49 51 55 45 20 6f 6e 63 6f 6e 66 20 2a   UNIQUE onconf *
2dc9e 2f 0a 7b 73 71 6c 69 74 65 33 43 72 65 61 74 65  /.{sqlite3Create
2dc9f 49 6e 64 65 78 28 70 50 61 72 73 65 2c 30 2c 30  Index(pParse,0,0
2dca0 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  ,0,0,yymsp[0].mi
2dca1 6e 6f 72 2e 79 79 31 39 34 2c 30 2c 30 2c 30 2c  nor.yy194,0,0,0,
2dca2 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2dca3 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
2dca4 36 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  6: /* ccons ::= 
2dca5 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50  CHECK LP expr RP
2dca6 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43   */.{sqlite3AddC
2dca7 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28 70  heckConstraint(p
2dca8 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e  Parse,yymsp[-1].
2dca9 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 7d 0a 20 20  minor.yy72);}.  
2dcaa 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2dcab 20 20 20 63 61 73 65 20 36 37 3a 20 2f 2a 20 63     case 67: /* c
2dcac 63 6f 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e  cons ::= REFEREN
2dcad 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f  CES nm idxlist_o
2dcae 70 74 20 72 65 66 61 72 67 73 20 2a 2f 0a 7b 73  pt refargs */.{s
2dcaf 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65  qlite3CreateFore
2dcb0 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 30 2c  ignKey(pParse,0,
2dcb1 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
2dcb2 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  .yy0,yymsp[-1].m
2dcb3 69 6e 6f 72 2e 79 79 31 34 38 2c 79 79 6d 73 70  inor.yy148,yymsp
2dcb4 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29  [0].minor.yy194)
2dcb5 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2dcb6 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 38 3a  ;.      case 68:
2dcb7 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 64 65   /* ccons ::= de
2dcb8 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f  fer_subclause */
2dcb9 0a 7b 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f  .{sqlite3DeferFo
2dcba 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c  reignKey(pParse,
2dcbb 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2dcbc 79 31 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20  y194);}.        
2dcbd 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2dcbe 65 20 36 39 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 69: /* ccons :
2dcbf 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a  := COLLATE ids *
2dcc0 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  /.{sqlite3AddCol
2dcc1 6c 61 74 65 54 79 70 65 28 70 50 61 72 73 65 2c  lateType(pParse,
2dcc2 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
2dcc3 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2dcc4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2dcc5 65 20 37 32 3a 20 2f 2a 20 72 65 66 61 72 67 73  e 72: /* refargs
2dcc6 20 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f   ::= */.{ yygoto
2dcc7 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45  minor.yy194 = OE
2dcc8 5f 52 65 73 74 72 69 63 74 20 2a 20 30 78 30 31  _Restrict * 0x01
2dcc9 30 31 30 31 3b 20 7d 0a 20 20 20 20 20 20 20 20  0101; }.        
2dcca 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2dccb 65 20 37 33 3a 20 2f 2a 20 72 65 66 61 72 67 73  e 73: /* refargs
2dccc 20 3a 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66   ::= refargs ref
2dccd 61 72 67 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  arg */.{ yygotom
2dcce 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 28 79 79  inor.yy194 = (yy
2dccf 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2dcd0 31 39 34 20 26 20 7e 79 79 6d 73 70 5b 30 5d 2e  194 & ~yymsp[0].
2dcd1 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 6d 61 73 6b  minor.yy497.mask
2dcd2 29 20 7c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  ) | yymsp[0].min
2dcd3 6f 72 2e 79 79 34 39 37 2e 76 61 6c 75 65 3b 20  or.yy497.value; 
2dcd4 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2dcd5 0a 20 20 20 20 20 20 63 61 73 65 20 37 34 3a 20  .      case 74: 
2dcd6 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41  /* refarg ::= MA
2dcd7 54 43 48 20 6e 6d 20 2a 2f 0a 7b 20 79 79 67 6f  TCH nm */.{ yygo
2dcd8 74 6f 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 76 61  tominor.yy497.va
2dcd9 6c 75 65 20 3d 20 30 3b 20 20 20 20 20 79 79 67  lue = 0;     yyg
2dcda 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 6d  otominor.yy497.m
2dcdb 61 73 6b 20 3d 20 30 78 30 30 30 30 30 30 3b 20  ask = 0x000000; 
2dcdc 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2dcdd 0a 20 20 20 20 20 20 63 61 73 65 20 37 35 3a 20  .      case 75: 
2dcde 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e  /* refarg ::= ON
2dcdf 20 44 45 4c 45 54 45 20 72 65 66 61 63 74 20 2a   DELETE refact *
2dce0 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2dce1 79 79 34 39 37 2e 76 61 6c 75 65 20 3d 20 79 79  yy497.value = yy
2dce2 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2dce3 39 34 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69  94;     yygotomi
2dce4 6e 6f 72 2e 79 79 34 39 37 2e 6d 61 73 6b 20 3d  nor.yy497.mask =
2dce5 20 30 78 30 30 30 30 66 66 3b 20 7d 0a 20 20 20   0x0000ff; }.   
2dce6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2dce7 20 20 63 61 73 65 20 37 36 3a 20 2f 2a 20 72 65    case 76: /* re
2dce8 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41  farg ::= ON UPDA
2dce9 54 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79  TE refact */.{ y
2dcea 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 39 37  ygotominor.yy497
2dceb 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30  .value = yymsp[0
2dcec 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 3c 3c 38  ].minor.yy194<<8
2dced 3b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;  yygotominor.y
2dcee 79 34 39 37 2e 6d 61 73 6b 20 3d 20 30 78 30 30  y497.mask = 0x00
2dcef 66 66 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20  ff00; }.        
2dcf0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2dcf1 65 20 37 37 3a 20 2f 2a 20 72 65 66 61 72 67 20  e 77: /* refarg 
2dcf2 3a 3a 3d 20 4f 4e 20 49 4e 53 45 52 54 20 72 65  ::= ON INSERT re
2dcf3 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  fact */.{ yygoto
2dcf4 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 76 61 6c 75  minor.yy497.valu
2dcf5 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e = yymsp[0].min
2dcf6 6f 72 2e 79 79 31 39 34 3c 3c 31 36 3b 20 79 79  or.yy194<<16; yy
2dcf7 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 39 37 2e  gotominor.yy497.
2dcf8 6d 61 73 6b 20 3d 20 30 78 66 66 30 30 30 30 3b  mask = 0xff0000;
2dcf9 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2dcfa 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 38 3a  ;.      case 78:
2dcfb 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 53   /* refact ::= S
2dcfc 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 20 79 79 67  ET NULL */.{ yyg
2dcfd 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d  otominor.yy194 =
2dcfe 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b 20 7d 0a 20   OE_SetNull; }. 
2dcff 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2dd00 20 20 20 20 63 61 73 65 20 37 39 3a 20 2f 2a 20      case 79: /* 
2dd01 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 44  refact ::= SET D
2dd02 45 46 41 55 4c 54 20 2a 2f 0a 7b 20 79 79 67 6f  EFAULT */.{ yygo
2dd03 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20  tominor.yy194 = 
2dd04 4f 45 5f 53 65 74 44 66 6c 74 3b 20 7d 0a 20 20  OE_SetDflt; }.  
2dd05 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2dd06 20 20 20 63 61 73 65 20 38 30 3a 20 2f 2a 20 72     case 80: /* r
2dd07 65 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41 44  efact ::= CASCAD
2dd08 45 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  E */.{ yygotomin
2dd09 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45 5f 43 61  or.yy194 = OE_Ca
2dd0a 73 63 61 64 65 3b 20 7d 0a 20 20 20 20 20 20 20  scade; }.       
2dd0b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2dd0c 73 65 20 38 31 3a 20 2f 2a 20 72 65 66 61 63 74  se 81: /* refact
2dd0d 20 3a 3a 3d 20 52 45 53 54 52 49 43 54 20 2a 2f   ::= RESTRICT */
2dd0e 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
2dd0f 79 31 39 34 20 3d 20 4f 45 5f 52 65 73 74 72 69  y194 = OE_Restri
2dd10 63 74 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ct; }.        br
2dd11 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2dd12 38 32 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62  82: /* defer_sub
2dd13 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44  clause ::= NOT D
2dd14 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64  EFERRABLE init_d
2dd15 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
2dd16 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 38   */.      case 8
2dd17 33 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63  3: /* defer_subc
2dd18 6c 61 75 73 65 20 3a 3a 3d 20 44 45 46 45 52 52  lause ::= DEFERR
2dd19 41 42 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72  ABLE init_deferr
2dd1a 65 64 5f 70 72 65 64 5f 6f 70 74 20 2a 2f 0a 20  ed_pred_opt */. 
2dd1b 20 20 20 20 20 63 61 73 65 20 39 38 3a 20 2f 2a       case 98: /*
2dd1c 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
2dd1d 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65 72 5f 73  _opt ::= defer_s
2dd1e 75 62 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  ubclause */.    
2dd1f 20 20 63 61 73 65 20 31 30 30 3a 20 2f 2a 20 6f    case 100: /* o
2dd20 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e  nconf ::= ON CON
2dd21 46 4c 49 43 54 20 72 65 73 6f 6c 76 65 74 79 70  FLICT resolvetyp
2dd22 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  e */.      case 
2dd23 31 30 32 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a  102: /* orconf :
2dd24 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74 79 70  := OR resolvetyp
2dd25 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  e */.      case 
2dd26 31 30 33 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74  103: /* resolvet
2dd27 79 70 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70  ype ::= raisetyp
2dd28 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  e */.      case 
2dd29 31 37 35 3a 20 2f 2a 20 69 6e 73 65 72 74 5f 63  175: /* insert_c
2dd2a 6d 64 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72  md ::= INSERT or
2dd2b 63 6f 6e 66 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  conf */.{yygotom
2dd2c 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 79 79 6d  inor.yy194 = yym
2dd2d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  sp[0].minor.yy19
2dd2e 34 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  4;}.        brea
2dd2f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 37  k;.      case 87
2dd30 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f 70  : /* conslist_op
2dd31 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  t ::= */.{yygoto
2dd32 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b  minor.yy0.n = 0;
2dd33 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
2dd34 2e 7a 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  .z = 0;}.       
2dd35 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2dd36 73 65 20 38 38 3a 20 2f 2a 20 63 6f 6e 73 6c 69  se 88: /* consli
2dd37 73 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41  st_opt ::= COMMA
2dd38 20 63 6f 6e 73 6c 69 73 74 20 2a 2f 0a 7b 79 79   conslist */.{yy
2dd39 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20  gotominor.yy0 = 
2dd3a 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2dd3b 79 79 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  yy0;}.        br
2dd3c 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2dd3d 39 33 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d  93: /* tcons ::=
2dd3e 20 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20   PRIMARY KEY LP 
2dd3f 69 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20  idxlist autoinc 
2dd40 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71  RP onconf */.{sq
2dd41 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b  lite3AddPrimaryK
2dd42 65 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  ey(pParse,yymsp[
2dd43 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c  -3].minor.yy148,
2dd44 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2dd45 79 31 39 34 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y194,yymsp[-2].m
2dd46 69 6e 6f 72 2e 79 79 31 39 34 2c 30 29 3b 7d 0a  inor.yy194,0);}.
2dd47 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2dd48 20 20 20 20 20 63 61 73 65 20 39 34 3a 20 2f 2a       case 94: /*
2dd49 20 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55   tcons ::= UNIQU
2dd4a 45 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20  E LP idxlist RP 
2dd4b 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74  onconf */.{sqlit
2dd4c 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50  e3CreateIndex(pP
2dd4d 61 72 73 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70  arse,0,0,0,yymsp
2dd4e 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38  [-2].minor.yy148
2dd4f 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
2dd50 79 79 31 39 34 2c 30 2c 30 2c 30 2c 30 29 3b 7d  yy194,0,0,0,0);}
2dd51 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2dd52 20 20 20 20 20 20 63 61 73 65 20 39 35 3a 20 2f        case 95: /
2dd53 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43  * tcons ::= CHEC
2dd54 4b 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e 63  K LP expr RP onc
2dd55 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  onf */.{sqlite3A
2dd56 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e  ddCheckConstrain
2dd57 74 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  t(pParse,yymsp[-
2dd58 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 7d  2].minor.yy72);}
2dd59 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2dd5a 20 20 20 20 20 20 63 61 73 65 20 39 36 3a 20 2f        case 96: /
2dd5b 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 46 4f 52 45  * tcons ::= FORE
2dd5c 49 47 4e 20 4b 45 59 20 4c 50 20 69 64 78 6c 69  IGN KEY LP idxli
2dd5d 73 74 20 52 50 20 52 45 46 45 52 45 4e 43 45 53  st RP REFERENCES
2dd5e 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
2dd5f 72 65 66 61 72 67 73 20 64 65 66 65 72 5f 73 75  refargs defer_su
2dd60 62 63 6c 61 75 73 65 5f 6f 70 74 20 2a 2f 0a 7b  bclause_opt */.{
2dd61 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61  .    sqlite3Crea
2dd62 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61  teForeignKey(pPa
2dd63 72 73 65 2c 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d  rse, yymsp[-6].m
2dd64 69 6e 6f 72 2e 79 79 31 34 38 2c 20 26 79 79 6d  inor.yy148, &yym
2dd65 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2dd66 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
2dd67 72 2e 79 79 31 34 38 2c 20 79 79 6d 73 70 5b 2d  r.yy148, yymsp[-
2dd68 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b  1].minor.yy194);
2dd69 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 66 65  .    sqlite3Defe
2dd6a 72 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61 72  rForeignKey(pPar
2dd6b 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
2dd6c 6f 72 2e 79 79 31 39 34 29 3b 0a 7d 0a 20 20 20  or.yy194);.}.   
2dd6d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2dd6e 20 20 63 61 73 65 20 39 39 3a 20 2f 2a 20 6f 6e    case 99: /* on
2dd6f 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  conf ::= */.    
2dd70 20 20 63 61 73 65 20 31 30 31 3a 20 2f 2a 20 6f    case 101: /* o
2dd71 72 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  rconf ::= */.{yy
2dd72 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20  gotominor.yy194 
2dd73 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 7d 0a 20  = OE_Default;}. 
2dd74 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2dd75 20 20 20 20 63 61 73 65 20 31 30 34 3a 20 2f 2a      case 104: /*
2dd76 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d   resolvetype ::=
2dd77 20 49 47 4e 4f 52 45 20 2a 2f 0a 7b 79 79 67 6f   IGNORE */.{yygo
2dd78 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20  tominor.yy194 = 
2dd79 4f 45 5f 49 67 6e 6f 72 65 3b 7d 0a 20 20 20 20  OE_Ignore;}.    
2dd7a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2dd7b 20 63 61 73 65 20 31 30 35 3a 20 2f 2a 20 72 65   case 105: /* re
2dd7c 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 52 45  solvetype ::= RE
2dd7d 50 4c 41 43 45 20 2a 2f 0a 20 20 20 20 20 20 63  PLACE */.      c
2dd7e 61 73 65 20 31 37 36 3a 20 2f 2a 20 69 6e 73 65  ase 176: /* inse
2dd7f 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41  rt_cmd ::= REPLA
2dd80 43 45 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  CE */.{yygotomin
2dd81 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45 5f 52 65  or.yy194 = OE_Re
2dd82 70 6c 61 63 65 3b 7d 0a 20 20 20 20 20 20 20 20  place;}.        
2dd83 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2dd84 65 20 31 30 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 106: /* cmd ::
2dd85 3d 20 44 52 4f 50 20 54 41 42 4c 45 20 69 66 65  = DROP TABLE ife
2dd86 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a  xists fullname *
2dd87 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72 6f  /.{.  sqlite3Dro
2dd88 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 79  pTable(pParse, y
2dd89 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2dd8a 31 38 35 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 31  185, 0, yymsp[-1
2dd8b 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 0a  ].minor.yy194);.
2dd8c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2dd8d 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 39 3a  .      case 109:
2dd8e 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61   /* cmd ::= crea
2dd8f 74 65 6b 77 20 74 65 6d 70 20 56 49 45 57 20 69  tekw temp VIEW i
2dd90 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62  fnotexists nm db
2dd91 6e 6d 20 41 53 20 73 65 6c 65 63 74 20 2a 2f 0a  nm AS select */.
2dd92 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74  {.  sqlite3Creat
2dd93 65 56 69 65 77 28 70 50 61 72 73 65 2c 20 26 79  eView(pParse, &y
2dd94 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
2dd95 79 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  y0, &yymsp[-3].m
2dd96 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
2dd97 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-2].minor.yy0, 
2dd98 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2dd99 79 32 34 33 2c 20 79 79 6d 73 70 5b 2d 36 5d 2e  y243, yymsp[-6].
2dd9a 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 20 79 79 6d  minor.yy194, yym
2dd9b 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
2dd9c 39 34 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  94);.}.        b
2dd9d 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2dd9e 20 31 31 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   110: /* cmd ::=
2dd9f 20 44 52 4f 50 20 56 49 45 57 20 69 66 65 78 69   DROP VIEW ifexi
2dda0 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a  sts fullname */.
2dda1 7b 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54  {.  sqlite3DropT
2dda2 61 62 6c 65 28 70 50 61 72 73 65 2c 20 79 79 6d  able(pParse, yym
2dda3 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38  sp[0].minor.yy18
2dda4 35 2c 20 31 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  5, 1, yymsp[-1].
2dda5 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 0a 7d 0a  minor.yy194);.}.
2dda6 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2dda7 20 20 20 20 20 63 61 73 65 20 31 31 31 3a 20 2f       case 111: /
2dda8 2a 20 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74  * cmd ::= select
2dda9 20 2a 2f 0a 7b 0a 20 20 53 65 6c 65 63 74 44 65   */.{.  SelectDe
2ddaa 73 74 20 64 65 73 74 20 3d 20 7b 53 52 54 5f 4f  st dest = {SRT_O
2ddab 75 74 70 75 74 2c 20 30 2c 20 30 2c 20 30 2c 20  utput, 0, 0, 0, 
2ddac 30 7d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  0};.  sqlite3Sel
2ddad 65 63 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ect(pParse, yyms
2ddae 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  p[0].minor.yy243
2ddaf 2c 20 26 64 65 73 74 29 3b 0a 20 20 73 71 6c 69  , &dest);.  sqli
2ddb0 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2ddb1 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
2ddb2 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  p[0].minor.yy243
2ddb3 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2ddb4 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2ddb5 31 32 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a  12: /* select ::
2ddb6 3d 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b  = oneselect */.{
2ddb7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34  yygotominor.yy24
2ddb8 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  3 = yymsp[0].min
2ddb9 6f 72 2e 79 79 32 34 33 3b 7d 0a 20 20 20 20 20  or.yy243;}.     
2ddba 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ddbb 63 61 73 65 20 31 31 33 3a 20 2f 2a 20 73 65 6c  case 113: /* sel
2ddbc 65 63 74 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d  ect ::= select m
2ddbd 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e  ultiselect_op on
2ddbe 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 69  eselect */.{.  i
2ddbf 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  f( yymsp[0].mino
2ddc0 72 2e 79 79 32 34 33 20 29 7b 0a 20 20 20 20 79  r.yy243 ){.    y
2ddc1 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2ddc2 32 34 33 2d 3e 6f 70 20 3d 20 28 75 38 29 79 79  243->op = (u8)yy
2ddc3 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2ddc4 31 39 34 3b 0a 20 20 20 20 79 79 6d 73 70 5b 30  194;.    yymsp[0
2ddc5 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 2d 3e 70  ].minor.yy243->p
2ddc6 50 72 69 6f 72 20 3d 20 79 79 6d 73 70 5b 2d 32  Prior = yymsp[-2
2ddc7 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 3b 0a 20  ].minor.yy243;. 
2ddc8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2ddc9 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2ddca 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
2ddcb 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  p[-2].minor.yy24
2ddcc 33 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  3);.  }.  yygoto
2ddcd 6d 69 6e 6f 72 2e 79 79 32 34 33 20 3d 20 79 79  minor.yy243 = yy
2ddce 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32  msp[0].minor.yy2
2ddcf 34 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  43;.}.        br
2ddd0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ddd1 31 31 35 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c  115: /* multisel
2ddd2 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e  ect_op ::= UNION
2ddd3 20 41 4c 4c 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ALL */.{yygotom
2ddd4 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 54 4b 5f  inor.yy194 = TK_
2ddd5 41 4c 4c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ALL;}.        br
2ddd6 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ddd7 31 31 37 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63  117: /* oneselec
2ddd8 74 20 3a 3a 3d 20 53 45 4c 45 43 54 20 64 69 73  t ::= SELECT dis
2ddd9 74 69 6e 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74  tinct selcollist
2ddda 20 66 72 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20   from where_opt 
2dddb 67 72 6f 75 70 62 79 5f 6f 70 74 20 68 61 76 69  groupby_opt havi
2dddc 6e 67 5f 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f  ng_opt orderby_o
2dddd 70 74 20 6c 69 6d 69 74 5f 6f 70 74 20 2a 2f 0a  pt limit_opt */.
2ddde 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2dddf 79 79 32 34 33 20 3d 20 73 71 6c 69 74 65 33 53  yy243 = sqlite3S
2dde0 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
2dde1 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
2dde2 79 79 31 34 38 2c 79 79 6d 73 70 5b 2d 35 5d 2e  yy148,yymsp[-5].
2dde3 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 79 79 6d 73  minor.yy185,yyms
2dde4 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  p[-4].minor.yy72
2dde5 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  ,yymsp[-3].minor
2dde6 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b 2d 32 5d  .yy148,yymsp[-2]
2dde7 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 79 79 6d 73  .minor.yy72,yyms
2dde8 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2dde9 38 2c 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f  8,yymsp[-7].mino
2ddea 72 2e 79 79 31 39 34 2c 79 79 6d 73 70 5b 30 5d  r.yy194,yymsp[0]
2ddeb 2e 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4c 69  .minor.yy354.pLi
2ddec 6d 69 74 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  mit,yymsp[0].min
2dded 6f 72 2e 79 79 33 35 34 2e 70 4f 66 66 73 65 74  or.yy354.pOffset
2ddee 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2ddef 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2ddf0 32 31 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20  21: /* sclp ::= 
2ddf1 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41  selcollist COMMA
2ddf2 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2ddf3 34 37 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f  47: /* idxlist_o
2ddf4 70 74 20 3a 3a 3d 20 4c 50 20 69 64 78 6c 69 73  pt ::= LP idxlis
2ddf5 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  t RP */.{yygotom
2ddf6 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 79 79 6d  inor.yy148 = yym
2ddf7 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2ddf8 34 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  48;}.        bre
2ddf9 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2ddfa 32 32 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20  22: /* sclp ::= 
2ddfb 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  */.      case 15
2ddfc 30 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70  0: /* orderby_op
2ddfd 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  t ::= */.      c
2ddfe 61 73 65 20 31 35 38 3a 20 2f 2a 20 67 72 6f 75  ase 158: /* grou
2ddff 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  pby_opt ::= */. 
2de00 20 20 20 20 20 63 61 73 65 20 32 34 30 3a 20 2f       case 240: /
2de01 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 2a  * exprlist ::= *
2de02 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 36  /.      case 246
2de03 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74  : /* idxlist_opt
2de04 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
2de05 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 30 3b 7d  inor.yy148 = 0;}
2de06 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2de07 20 20 20 20 20 20 63 61 73 65 20 31 32 33 3a 20        case 123: 
2de08 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a  /* selcollist ::
2de09 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73 20 2a  = sclp expr as *
2de0a 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e  /.{.   yygotomin
2de0b 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69 74  or.yy148 = sqlit
2de0c 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2de0d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32  (pParse,yymsp[-2
2de0e 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 79 79  ].minor.yy148,yy
2de0f 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2de10 37 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  72,yymsp[0].mino
2de11 72 2e 79 79 30 2e 6e 3f 26 79 79 6d 73 70 5b 30  r.yy0.n?&yymsp[0
2de12 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a 30 29 3b 0a  ].minor.yy0:0);.
2de13 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2de14 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 34 3a  .      case 124:
2de15 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a   /* selcollist :
2de16 3a 3d 20 73 63 6c 70 20 53 54 41 52 20 2a 2f 0a  := sclp STAR */.
2de17 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71  {.  Expr *p = sq
2de18 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2de19 65 2c 20 54 4b 5f 41 4c 4c 2c 20 30 2c 20 30 2c  e, TK_ALL, 0, 0,
2de1a 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e   0);.  yygotomin
2de1b 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69 74  or.yy148 = sqlit
2de1c 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2de1d 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
2de1e 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20  1].minor.yy148, 
2de1f 70 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  p, 0);.}.       
2de20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2de21 73 65 20 31 32 35 3a 20 2f 2a 20 73 65 6c 63 6f  se 125: /* selco
2de22 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 6e  llist ::= sclp n
2de23 6d 20 44 4f 54 20 53 54 41 52 20 2a 2f 0a 7b 0a  m DOT STAR */.{.
2de24 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d    Expr *pRight =
2de25 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2de26 61 72 73 65 2c 20 54 4b 5f 41 4c 4c 2c 20 30 2c  arse, TK_ALL, 0,
2de27 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69   0, &yymsp[0].mi
2de28 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72  nor.yy0);.  Expr
2de29 20 2a 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65   *pLeft = sqlite
2de2a 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2de2b 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d  K_ID, 0, 0, &yym
2de2c 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2de2d 29 3b 0a 20 20 45 78 70 72 20 2a 70 44 6f 74 20  );.  Expr *pDot 
2de2e 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2de2f 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70  Parse, TK_DOT, p
2de30 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29  Left, pRight, 0)
2de31 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
2de32 79 79 31 34 38 20 3d 20 73 71 6c 69 74 65 33 45  yy148 = sqlite3E
2de33 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2de34 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d  arse,yymsp[-3].m
2de35 69 6e 6f 72 2e 79 79 31 34 38 2c 20 70 44 6f 74  inor.yy148, pDot
2de36 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  , 0);.}.        
2de37 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2de38 65 20 31 32 38 3a 20 2f 2a 20 61 73 20 3a 3a 3d  e 128: /* as ::=
2de39 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2de3a 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20  .yy0.n = 0;}.   
2de3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2de3c 20 20 63 61 73 65 20 31 32 39 3a 20 2f 2a 20 66    case 129: /* f
2de3d 72 6f 6d 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f  rom ::= */.{yygo
2de3e 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20  tominor.yy185 = 
2de3f 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2de40 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ero(pParse->db, 
2de41 73 69 7a 65 6f 66 28 2a 79 79 67 6f 74 6f 6d 69  sizeof(*yygotomi
2de42 6e 6f 72 2e 79 79 31 38 35 29 29 3b 7d 0a 20 20  nor.yy185));}.  
2de43 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2de44 20 20 20 63 61 73 65 20 31 33 30 3a 20 2f 2a 20     case 130: /* 
2de45 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65  from ::= FROM se
2de46 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20  ltablist */.{.  
2de47 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38  yygotominor.yy18
2de48 35 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  5 = yymsp[0].min
2de49 6f 72 2e 79 79 31 38 35 3b 0a 20 20 73 71 6c 69  or.yy185;.  sqli
2de4a 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
2de4b 6f 69 6e 54 79 70 65 28 79 79 67 6f 74 6f 6d 69  oinType(yygotomi
2de4c 6e 6f 72 2e 79 79 31 38 35 29 3b 0a 7d 0a 20 20  nor.yy185);.}.  
2de4d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2de4e 20 20 20 63 61 73 65 20 31 33 31 3a 20 2f 2a 20     case 131: /* 
2de4f 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20 73  stl_prefix ::= s
2de50 65 6c 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f 70  eltablist joinop
2de51 20 2a 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d   */.{.   yygotom
2de52 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20 79 79 6d  inor.yy185 = yym
2de53 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2de54 38 35 3b 0a 20 20 20 69 66 28 20 79 79 67 6f 74  85;.   if( yygot
2de55 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 26 26 20  ominor.yy185 && 
2de56 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38  yygotominor.yy18
2de57 35 2d 3e 6e 53 72 63 3e 30 20 29 20 79 79 67 6f  5->nSrc>0 ) yygo
2de58 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 2d 3e 61  tominor.yy185->a
2de59 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  [yygotominor.yy1
2de5a 38 35 2d 3e 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e  85->nSrc-1].join
2de5b 74 79 70 65 20 3d 20 28 75 38 29 79 79 6d 73 70  type = (u8)yymsp
2de5c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 3b  [0].minor.yy194;
2de5d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2de5e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 32  ;.      case 132
2de5f 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20  : /* stl_prefix 
2de60 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2de61 6e 6f 72 2e 79 79 31 38 35 20 3d 20 30 3b 7d 0a  nor.yy185 = 0;}.
2de62 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2de63 20 20 20 20 20 63 61 73 65 20 31 33 33 3a 20 2f       case 133: /
2de64 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d  * seltablist ::=
2de65 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64   stl_prefix nm d
2de66 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f  bnm as indexed_o
2de67 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f  pt on_opt using_
2de68 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  opt */.{.  yygot
2de69 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20 73  ominor.yy185 = s
2de6a 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
2de6b 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72  endFromTerm(pPar
2de6c 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  se,yymsp[-6].min
2de6d 6f 72 2e 79 79 31 38 35 2c 26 79 79 6d 73 70 5b  or.yy185,&yymsp[
2de6e 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -5].minor.yy0,&y
2de6f 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2de70 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  y0,&yymsp[-3].mi
2de71 6e 6f 72 2e 79 79 30 2c 30 2c 79 79 6d 73 70 5b  nor.yy0,0,yymsp[
2de72 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 79  -1].minor.yy72,y
2de73 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2de74 32 35 34 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  254);.  sqlite3S
2de75 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28  rcListIndexedBy(
2de76 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
2de77 6e 6f 72 2e 79 79 31 38 35 2c 20 26 79 79 6d 73  nor.yy185, &yyms
2de78 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-2].minor.yy0)
2de79 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2de7a 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
2de7b 34 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74  4: /* seltablist
2de7c 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
2de7d 4c 50 20 73 65 6c 65 63 74 20 52 50 20 61 73 20  LP select RP as 
2de7e 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74  on_opt using_opt
2de7f 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f   */.{.    yygoto
2de80 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20 73 71  minor.yy185 = sq
2de81 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
2de82 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73  ndFromTerm(pPars
2de83 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f  e,yymsp[-6].mino
2de84 72 2e 79 79 31 38 35 2c 30 2c 30 2c 26 79 79 6d  r.yy185,0,0,&yym
2de85 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2de86 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
2de87 2e 79 79 32 34 33 2c 79 79 6d 73 70 5b 2d 31 5d  .yy243,yymsp[-1]
2de88 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 79 79 6d 73  .minor.yy72,yyms
2de89 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 34  p[0].minor.yy254
2de8a 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62  );.  }.        b
2de8b 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2de8c 20 31 33 35 3a 20 2f 2a 20 73 65 6c 74 61 62 6c   135: /* seltabl
2de8d 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66  ist ::= stl_pref
2de8e 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73 74  ix LP seltablist
2de8f 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73   RP as on_opt us
2de90 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20  ing_opt */.{.   
2de91 20 69 66 28 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d   if( yymsp[-6].m
2de92 69 6e 6f 72 2e 79 79 31 38 35 3d 3d 30 20 26 26  inor.yy185==0 &&
2de93 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2de94 2e 79 79 30 2e 6e 3d 3d 30 20 26 26 20 79 79 6d  .yy0.n==0 && yym
2de95 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-1].minor.yy7
2de96 32 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b 30 5d  2==0 && yymsp[0]
2de97 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 3d 3d 30 20  .minor.yy254==0 
2de98 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d  ){.      yygotom
2de99 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20 79 79 6d  inor.yy185 = yym
2de9a 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
2de9b 38 35 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  85;.    }else{. 
2de9c 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 75       Select *pSu
2de9d 62 71 75 65 72 79 3b 0a 20 20 20 20 20 20 73 71  bquery;.      sq
2de9e 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66  lite3SrcListShif
2de9f 74 4a 6f 69 6e 54 79 70 65 28 79 79 6d 73 70 5b  tJoinType(yymsp[
2dea0 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 29  -4].minor.yy185)
2dea1 3b 0a 20 20 20 20 20 20 70 53 75 62 71 75 65 72  ;.      pSubquer
2dea2 79 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  y = sqlite3Selec
2dea3 74 4e 65 77 28 70 50 61 72 73 65 2c 30 2c 79 79  tNew(pParse,0,yy
2dea4 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2dea5 31 38 35 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c  185,0,0,0,0,0,0,
2dea6 30 29 3b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f  0);.      yygoto
2dea7 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20 73 71  minor.yy185 = sq
2dea8 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
2dea9 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73  ndFromTerm(pPars
2deaa 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f  e,yymsp[-6].mino
2deab 72 2e 79 79 31 38 35 2c 30 2c 30 2c 26 79 79 6d  r.yy185,0,0,&yym
2deac 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2dead 2c 70 53 75 62 71 75 65 72 79 2c 79 79 6d 73 70  ,pSubquery,yymsp
2deae 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c  [-1].minor.yy72,
2deaf 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2deb0 79 32 35 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  y254);.    }.  }
2deb1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2deb2 20 20 20 20 20 20 63 61 73 65 20 31 33 36 3a 20        case 136: 
2deb3 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 2a 2f 0a 20  /* dbnm ::= */. 
2deb4 20 20 20 20 20 63 61 73 65 20 31 34 35 3a 20 2f       case 145: /
2deb5 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a  * indexed_opt ::
2deb6 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
2deb7 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74  r.yy0.z=0; yygot
2deb8 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 30 3b 7d  ominor.yy0.n=0;}
2deb9 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2deba 20 20 20 20 20 20 63 61 73 65 20 31 33 38 3a 20        case 138: 
2debb 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20  /* fullname ::= 
2debc 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 79 79 67 6f  nm dbnm */.{yygo
2debd 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20  tominor.yy185 = 
2debe 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
2debf 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  pend(pParse->db,
2dec0 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
2dec1 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
2dec2 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
2dec3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2dec4 20 20 20 63 61 73 65 20 31 33 39 3a 20 2f 2a 20     case 139: /* 
2dec5 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41  joinop ::= COMMA
2dec6 7c 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74  |JOIN */.{ yygot
2dec7 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4a  ominor.yy194 = J
2dec8 54 5f 49 4e 4e 45 52 3b 20 7d 0a 20 20 20 20 20  T_INNER; }.     
2dec9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2deca 63 61 73 65 20 31 34 30 3a 20 2f 2a 20 6a 6f 69  case 140: /* joi
2decb 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20  nop ::= JOIN_KW 
2decc 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  JOIN */.{ yygoto
2decd 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 73 71  minor.yy194 = sq
2dece 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 70 50  lite3JoinType(pP
2decf 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
2ded0 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 20  minor.yy0,0,0); 
2ded1 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ded2 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 31 3a  .      case 141:
2ded3 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a   /* joinop ::= J
2ded4 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 20 2a  OIN_KW nm JOIN *
2ded5 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2ded6 79 79 31 39 34 20 3d 20 73 71 6c 69 74 65 33 4a  yy194 = sqlite3J
2ded7 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26  oinType(pParse,&
2ded8 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2ded9 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  yy0,&yymsp[-1].m
2deda 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 20 7d 0a 20  inor.yy0,0); }. 
2dedb 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2dedc 20 20 20 20 63 61 73 65 20 31 34 32 3a 20 2f 2a      case 142: /*
2dedd 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e   joinop ::= JOIN
2dede 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a  _KW nm nm JOIN *
2dedf 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2dee0 79 79 31 39 34 20 3d 20 73 71 6c 69 74 65 33 4a  yy194 = sqlite3J
2dee1 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26  oinType(pParse,&
2dee2 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2dee3 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  yy0,&yymsp[-2].m
2dee4 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2dee5 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20  -1].minor.yy0); 
2dee6 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2dee7 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 33 3a  .      case 143:
2dee8 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f   /* on_opt ::= O
2dee9 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  N expr */.      
2deea 63 61 73 65 20 31 35 34 3a 20 2f 2a 20 73 6f 72  case 154: /* sor
2deeb 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72 20 2a  titem ::= expr *
2deec 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 31  /.      case 161
2deed 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20  : /* having_opt 
2deee 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78 70 72 20  ::= HAVING expr 
2deef 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  */.      case 16
2def0 38 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20  8: /* where_opt 
2def1 3a 3a 3d 20 57 48 45 52 45 20 65 78 70 72 20 2a  ::= WHERE expr *
2def2 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 33  /.      case 183
2def3 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 74 65  : /* expr ::= te
2def4 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  rm */.      case
2def5 20 32 31 31 3a 20 2f 2a 20 65 73 63 61 70 65 20   211: /* escape 
2def6 3a 3a 3d 20 45 53 43 41 50 45 20 65 78 70 72 20  ::= ESCAPE expr 
2def7 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  */.      case 23
2def8 35 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20  5: /* case_else 
2def9 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72 20 2a 2f  ::= ELSE expr */
2defa 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 37 3a  .      case 237:
2defb 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e 64   /* case_operand
2defc 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a 7b 79 79   ::= expr */.{yy
2defd 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d  gotominor.yy72 =
2defe 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2deff 79 79 37 32 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy72;}.        b
2df00 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2df01 20 31 34 34 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20   144: /* on_opt 
2df02 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2df03 65 20 31 36 30 3a 20 2f 2a 20 68 61 76 69 6e 67  e 160: /* having
2df04 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2df05 20 20 63 61 73 65 20 31 36 37 3a 20 2f 2a 20 77    case 167: /* w
2df06 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  here_opt ::= */.
2df07 20 20 20 20 20 20 63 61 73 65 20 32 31 32 3a 20        case 212: 
2df08 2f 2a 20 65 73 63 61 70 65 20 3a 3a 3d 20 2a 2f  /* escape ::= */
2df09 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 36 3a  .      case 236:
2df0a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a   /* case_else ::
2df0b 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2df0c 32 33 38 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65  238: /* case_ope
2df0d 72 61 6e 64 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  rand ::= */.{yyg
2df0e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20  otominor.yy72 = 
2df0f 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2df10 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
2df11 37 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70  7: /* indexed_op
2df12 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45  t ::= NOT INDEXE
2df13 44 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  D */.{yygotomino
2df14 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74  r.yy0.z=0; yygot
2df15 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 31 3b 7d  ominor.yy0.n=1;}
2df16 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2df17 20 20 20 20 20 20 63 61 73 65 20 31 34 38 3a 20        case 148: 
2df18 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d  /* using_opt ::=
2df19 20 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c   USING LP inscol
2df1a 6c 69 73 74 20 52 50 20 2a 2f 0a 20 20 20 20 20  list RP */.     
2df1b 20 63 61 73 65 20 31 38 30 3a 20 2f 2a 20 69 6e   case 180: /* in
2df1c 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  scollist_opt ::=
2df1d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52   LP inscollist R
2df1e 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  P */.{yygotomino
2df1f 72 2e 79 79 32 35 34 20 3d 20 79 79 6d 73 70 5b  r.yy254 = yymsp[
2df20 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 3b  -1].minor.yy254;
2df21 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2df22 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 39 3a  .      case 149:
2df23 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a   /* using_opt ::
2df24 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2df25 31 37 39 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69  179: /* inscolli
2df26 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79  st_opt ::= */.{y
2df27 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 34  ygotominor.yy254
2df28 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
2df29 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2df2a 20 31 35 31 3a 20 2f 2a 20 6f 72 64 65 72 62 79   151: /* orderby
2df2b 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42  _opt ::= ORDER B
2df2c 59 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20  Y sortlist */.  
2df2d 20 20 20 20 63 61 73 65 20 31 35 39 3a 20 2f 2a      case 159: /*
2df2e 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d   groupby_opt ::=
2df2f 20 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72 6c   GROUP BY nexprl
2df30 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ist */.      cas
2df31 65 20 32 33 39 3a 20 2f 2a 20 65 78 70 72 6c 69  e 239: /* exprli
2df32 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74  st ::= nexprlist
2df33 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2df34 2e 79 79 31 34 38 20 3d 20 79 79 6d 73 70 5b 30  .yy148 = yymsp[0
2df35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 3b 7d 0a  ].minor.yy148;}.
2df36 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2df37 20 20 20 20 20 63 61 73 65 20 31 35 32 3a 20 2f       case 152: /
2df38 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73  * sortlist ::= s
2df39 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f  ortlist COMMA so
2df3a 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72  rtitem sortorder
2df3b 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
2df3c 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69  nor.yy148 = sqli
2df3d 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2df3e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
2df3f 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 79  3].minor.yy148,y
2df40 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2df41 79 37 32 2c 30 29 3b 0a 20 20 69 66 28 20 79 79  y72,0);.  if( yy
2df42 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20  gotominor.yy148 
2df43 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
2df44 31 34 38 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e  148->a[yygotomin
2df45 6f 72 2e 79 79 31 34 38 2d 3e 6e 45 78 70 72 2d  or.yy148->nExpr-
2df46 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28  1].sortOrder = (
2df47 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  u8)yymsp[0].mino
2df48 72 2e 79 79 31 39 34 3b 0a 7d 0a 20 20 20 20 20  r.yy194;.}.     
2df49 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2df4a 63 61 73 65 20 31 35 33 3a 20 2f 2a 20 73 6f 72  case 153: /* sor
2df4b 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74  tlist ::= sortit
2df4c 65 6d 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a  em sortorder */.
2df4d 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2df4e 79 79 31 34 38 20 3d 20 73 71 6c 69 74 65 33 45  yy148 = sqlite3E
2df4f 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2df50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d  arse,0,yymsp[-1]
2df51 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 30 29 3b 0a  .minor.yy72,0);.
2df52 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2df53 72 2e 79 79 31 34 38 20 26 26 20 79 79 67 6f 74  r.yy148 && yygot
2df54 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 2d 3e 61 20  ominor.yy148->a 
2df55 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
2df56 31 34 38 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72  148->a[0].sortOr
2df57 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b  der = (u8)yymsp[
2df58 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 3b 0a  0].minor.yy194;.
2df59 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2df5a 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 35 3a  .      case 155:
2df5b 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a   /* sortorder ::
2df5c 3d 20 41 53 43 20 2a 2f 0a 20 20 20 20 20 20 63  = ASC */.      c
2df5d 61 73 65 20 31 35 37 3a 20 2f 2a 20 73 6f 72 74  ase 157: /* sort
2df5e 6f 72 64 65 72 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  order ::= */.{yy
2df5f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20  gotominor.yy194 
2df60 3d 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 3b  = SQLITE_SO_ASC;
2df61 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2df62 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 36 3a  .      case 156:
2df63 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a   /* sortorder ::
2df64 3d 20 44 45 53 43 20 2a 2f 0a 7b 79 79 67 6f 74  = DESC */.{yygot
2df65 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 53  ominor.yy194 = S
2df66 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 3b 7d 0a  QLITE_SO_DESC;}.
2df67 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2df68 20 20 20 20 20 63 61 73 65 20 31 36 32 3a 20 2f       case 162: /
2df69 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20  * limit_opt ::= 
2df6a 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2df6b 79 79 33 35 34 2e 70 4c 69 6d 69 74 20 3d 20 30  yy354.pLimit = 0
2df6c 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
2df6d 33 35 34 2e 70 4f 66 66 73 65 74 20 3d 20 30 3b  354.pOffset = 0;
2df6e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2df6f 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 33 3a  .      case 163:
2df70 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a   /* limit_opt ::
2df71 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 2a 2f 0a  = LIMIT expr */.
2df72 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
2df73 35 34 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73  54.pLimit = yyms
2df74 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b  p[0].minor.yy72;
2df75 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
2df76 35 34 2e 70 4f 66 66 73 65 74 20 3d 20 30 3b 7d  54.pOffset = 0;}
2df77 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2df78 20 20 20 20 20 20 63 61 73 65 20 31 36 34 3a 20        case 164: 
2df79 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  /* limit_opt ::=
2df7a 20 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46 53   LIMIT expr OFFS
2df7b 45 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  ET expr */.{yygo
2df7c 74 6f 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4c  tominor.yy354.pL
2df7d 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  imit = yymsp[-2]
2df7e 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b 20 79 79 67  .minor.yy72; yyg
2df7f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 70  otominor.yy354.p
2df80 4f 66 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 30  Offset = yymsp[0
2df81 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b 7d 0a 20  ].minor.yy72;}. 
2df82 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2df83 20 20 20 20 63 61 73 65 20 31 36 35 3a 20 2f 2a      case 165: /*
2df84 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c   limit_opt ::= L
2df85 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20  IMIT expr COMMA 
2df86 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  expr */.{yygotom
2df87 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4f 66 66 73  inor.yy354.pOffs
2df88 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  et = yymsp[-2].m
2df89 69 6e 6f 72 2e 79 79 37 32 3b 20 79 79 67 6f 74  inor.yy72; yygot
2df8a 6f 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4c 69  ominor.yy354.pLi
2df8b 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  mit = yymsp[0].m
2df8c 69 6e 6f 72 2e 79 79 37 32 3b 7d 0a 20 20 20 20  inor.yy72;}.    
2df8d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2df8e 20 63 61 73 65 20 31 36 36 3a 20 2f 2a 20 63 6d   case 166: /* cm
2df8f 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f  d ::= DELETE FRO
2df90 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78  M fullname index
2df91 65 64 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70 74  ed_opt where_opt
2df92 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53   */.{.  sqlite3S
2df93 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28  rcListIndexedBy(
2df94 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32  pParse, yymsp[-2
2df95 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 20 26  ].minor.yy185, &
2df96 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2df97 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  yy0);.  sqlite3D
2df98 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65  eleteFrom(pParse
2df99 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
2df9a 2e 79 79 31 38 35 2c 79 79 6d 73 70 5b 30 5d 2e  .yy185,yymsp[0].
2df9b 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 0a 7d 0a 20  minor.yy72);.}. 
2df9c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2df9d 20 20 20 20 63 61 73 65 20 31 36 39 3a 20 2f 2a      case 169: /*
2df9e 20 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20   cmd ::= UPDATE 
2df9f 6f 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20  orconf fullname 
2dfa0 69 6e 64 65 78 65 64 5f 6f 70 74 20 53 45 54 20  indexed_opt SET 
2dfa1 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70  setlist where_op
2dfa2 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  t */.{.  sqlite3
2dfa3 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79  SrcListIndexedBy
2dfa4 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
2dfa5 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 20  4].minor.yy185, 
2dfa6 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
2dfa7 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  .yy0);.  sqlite3
2dfa8 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e  ExprListCheckLen
2dfa9 67 74 68 28 70 50 61 72 73 65 2c 79 79 6d 73 70  gth(pParse,yymsp
2dfaa 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38  [-1].minor.yy148
2dfab 2c 22 73 65 74 20 6c 69 73 74 22 29 3b 20 0a 20  ,"set list"); . 
2dfac 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 70   sqlite3Update(p
2dfad 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e  Parse,yymsp[-4].
2dfae 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 79 79 6d 73  minor.yy185,yyms
2dfaf 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2dfb0 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8,yymsp[0].minor
2dfb1 2e 79 79 37 32 2c 79 79 6d 73 70 5b 2d 35 5d 2e  .yy72,yymsp[-5].
2dfb2 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 0a 7d 0a  minor.yy194);.}.
2dfb3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2dfb4 20 20 20 20 20 63 61 73 65 20 31 37 30 3a 20 2f       case 170: /
2dfb5 2a 20 73 65 74 6c 69 73 74 20 3a 3a 3d 20 73 65  * setlist ::= se
2dfb6 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 45  tlist COMMA nm E
2dfb7 51 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74  Q expr */.{yygot
2dfb8 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73  ominor.yy148 = s
2dfb9 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2dfba 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
2dfbb 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-4].minor.yy14
2dfbc 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8,yymsp[0].minor
2dfbd 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 32 5d  .yy72,&yymsp[-2]
2dfbe 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
2dfbf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2dfc0 20 20 20 63 61 73 65 20 31 37 31 3a 20 2f 2a 20     case 171: /* 
2dfc1 73 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45  setlist ::= nm E
2dfc2 51 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74  Q expr */.{yygot
2dfc3 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73  ominor.yy148 = s
2dfc4 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2dfc5 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 79 79  pend(pParse,0,yy
2dfc6 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37  msp[0].minor.yy7
2dfc7 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  2,&yymsp[-2].min
2dfc8 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2dfc9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2dfca 61 73 65 20 31 37 32 3a 20 2f 2a 20 63 6d 64 20  ase 172: /* cmd 
2dfcb 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49  ::= insert_cmd I
2dfcc 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73  NTO fullname ins
2dfcd 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55  collist_opt VALU
2dfce 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52  ES LP itemlist R
2dfcf 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73  P */.{sqlite3Ins
2dfd0 65 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ert(pParse, yyms
2dfd1 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38  p[-5].minor.yy18
2dfd2 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  5, yymsp[-1].min
2dfd3 6f 72 2e 79 79 31 34 38 2c 20 30 2c 20 79 79 6d  or.yy148, 0, yym
2dfd4 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-4].minor.yy2
2dfd5 35 34 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69  54, yymsp[-7].mi
2dfd6 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a 20 20 20  nor.yy194);}.   
2dfd7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2dfd8 20 20 63 61 73 65 20 31 37 33 3a 20 2f 2a 20 63    case 173: /* c
2dfd9 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
2dfda 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20  d INTO fullname 
2dfdb 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
2dfdc 65 6c 65 63 74 20 2a 2f 0a 7b 73 71 6c 69 74 65  elect */.{sqlite
2dfdd 33 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20  3Insert(pParse, 
2dfde 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2dfdf 79 79 31 38 35 2c 20 30 2c 20 79 79 6d 73 70 5b  yy185, 0, yymsp[
2dfe0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 2c 20  0].minor.yy243, 
2dfe1 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2dfe2 79 79 32 35 34 2c 20 79 79 6d 73 70 5b 2d 34 5d  yy254, yymsp[-4]
2dfe3 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a  .minor.yy194);}.
2dfe4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2dfe5 20 20 20 20 20 63 61 73 65 20 31 37 34 3a 20 2f       case 174: /
2dfe6 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74  * cmd ::= insert
2dfe7 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61  _cmd INTO fullna
2dfe8 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  me inscollist_op
2dfe9 74 20 44 45 46 41 55 4c 54 20 56 41 4c 55 45 53  t DEFAULT VALUES
2dfea 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65   */.{sqlite3Inse
2dfeb 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  rt(pParse, yymsp
2dfec 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35  [-3].minor.yy185
2dfed 2c 20 30 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 32  , 0, 0, yymsp[-2
2dfee 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 2c 20 79  ].minor.yy254, y
2dfef 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
2dff0 79 31 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20  y194);}.        
2dff1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2dff2 65 20 31 37 37 3a 20 2f 2a 20 69 74 65 6d 6c 69  e 177: /* itemli
2dff3 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69 73 74 20  st ::= itemlist 
2dff4 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a 20 20  COMMA expr */.  
2dff5 20 20 20 20 63 61 73 65 20 32 34 31 3a 20 2f 2a      case 241: /*
2dff6 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e   nexprlist ::= n
2dff7 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d 41 20 65  exprlist COMMA e
2dff8 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  xpr */.{yygotomi
2dff9 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69  nor.yy148 = sqli
2dffa 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2dffb 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
2dffc 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 79  2].minor.yy148,y
2dffd 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2dffe 37 32 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  72,0);}.        
2dfff 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e000 65 20 31 37 38 3a 20 2f 2a 20 69 74 65 6d 6c 69  e 178: /* itemli
2e001 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a 20  st ::= expr */. 
2e002 20 20 20 20 20 63 61 73 65 20 32 34 32 3a 20 2f       case 242: /
2e003 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20  * nexprlist ::= 
2e004 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  expr */.{yygotom
2e005 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c  inor.yy148 = sql
2e006 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2e007 6e 64 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73  nd(pParse,0,yyms
2e008 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c  p[0].minor.yy72,
2e009 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2e00a 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e00b 38 31 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73  81: /* inscollis
2e00c 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73 74  t ::= inscollist
2e00d 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f 0a 7b 79 79   COMMA nm */.{yy
2e00e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 34 20  gotominor.yy254 
2e00f 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41  = sqlite3IdListA
2e010 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
2e011 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
2e012 2e 79 79 32 35 34 2c 26 79 79 6d 73 70 5b 30 5d  .yy254,&yymsp[0]
2e013 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
2e014 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e015 20 20 20 63 61 73 65 20 31 38 32 3a 20 2f 2a 20     case 182: /* 
2e016 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e  inscollist ::= n
2e017 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  m */.{yygotomino
2e018 72 2e 79 79 32 35 34 20 3d 20 73 71 6c 69 74 65  r.yy254 = sqlite
2e019 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 70 50  3IdListAppend(pP
2e01a 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73  arse->db,0,&yyms
2e01b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2e01c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e01d 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 34 3a  .      case 184:
2e01e 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20   /* expr ::= LP 
2e01f 65 78 70 72 20 52 50 20 2a 2f 0a 7b 79 79 67 6f  expr RP */.{yygo
2e020 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 79  tominor.yy72 = y
2e021 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e022 79 37 32 3b 20 73 71 6c 69 74 65 33 45 78 70 72  y72; sqlite3Expr
2e023 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2e024 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 32 5d  .yy72,&yymsp[-2]
2e025 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2e026 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2e027 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2e028 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 35  ;.      case 185
2e029 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 4e 55  : /* term ::= NU
2e02a 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  LL */.      case
2e02b 20 31 39 30 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a   190: /* term ::
2e02c 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c  = INTEGER|FLOAT|
2e02d 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 63 61  BLOB */.      ca
2e02e 73 65 20 31 39 31 3a 20 2f 2a 20 74 65 72 6d 20  se 191: /* term 
2e02f 3a 3a 3d 20 53 54 52 49 4e 47 20 2a 2f 0a 7b 79  ::= STRING */.{y
2e030 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2e031 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2e032 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
2e033 6d 61 6a 6f 72 2c 20 30 2c 20 30 2c 20 26 79 79  major, 0, 0, &yy
2e034 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2e035 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2e036 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  k;.      case 18
2e037 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 69  6: /* expr ::= i
2e038 64 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  d */.      case 
2e039 31 38 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  187: /* expr ::=
2e03a 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 7b 79 79 67   JOIN_KW */.{yyg
2e03b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20  otominor.yy72 = 
2e03c 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2e03d 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
2e03e 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2e03f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
2e040 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e041 73 65 20 31 38 38 3a 20 2f 2a 20 65 78 70 72 20  se 188: /* expr 
2e042 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f  ::= nm DOT nm */
2e043 0a 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 31  .{.  Expr *temp1
2e044 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2e045 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
2e046 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  , 0, &yymsp[-2].
2e047 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78  minor.yy0);.  Ex
2e048 70 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69  pr *temp2 = sqli
2e049 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2e04a 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79   TK_ID, 0, 0, &y
2e04b 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e04c 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
2e04d 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2e04e 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2e04f 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d  _DOT, temp1, tem
2e050 70 32 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20  p2, 0);.}.      
2e051 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e052 61 73 65 20 31 38 39 3a 20 2f 2a 20 65 78 70 72  ase 189: /* expr
2e053 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44   ::= nm DOT nm D
2e054 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70  OT nm */.{.  Exp
2e055 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74  r *temp1 = sqlit
2e056 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2e057 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
2e058 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2e059 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  0);.  Expr *temp
2e05a 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2e05b 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
2e05c 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
2e05d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
2e05e 78 70 72 20 2a 74 65 6d 70 33 20 3d 20 73 71 6c  xpr *temp3 = sql
2e05f 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2e060 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
2e061 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e062 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d  y0);.  Expr *tem
2e063 70 34 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p4 = sqlite3PExp
2e064 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
2e065 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70 33 2c 20  , temp2, temp3, 
2e066 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
2e067 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2e068 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2e069 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d  _DOT, temp1, tem
2e06a 70 34 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20 20  p4, 0);.}.      
2e06b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e06c 61 73 65 20 31 39 32 3a 20 2f 2a 20 65 78 70 72  ase 192: /* expr
2e06d 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 20 2a 2f   ::= REGISTER */
2e06e 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2e06f 37 32 20 3d 20 73 71 6c 69 74 65 33 52 65 67 69  72 = sqlite3Regi
2e070 73 74 65 72 45 78 70 72 28 70 50 61 72 73 65 2c  sterExpr(pParse,
2e071 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
2e072 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2e073 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e074 65 20 31 39 33 3a 20 2f 2a 20 65 78 70 72 20 3a  e 193: /* expr :
2e075 3a 3d 20 56 41 52 49 41 42 4c 45 20 2a 2f 0a 7b  := VARIABLE */.{
2e076 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  .  Token *pToken
2e077 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   = &yymsp[0].min
2e078 6f 72 2e 79 79 30 3b 0a 20 20 45 78 70 72 20 2a  or.yy0;.  Expr *
2e079 70 45 78 70 72 20 3d 20 79 79 67 6f 74 6f 6d 69  pExpr = yygotomi
2e07a 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74  nor.yy72 = sqlit
2e07b 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2e07c 54 4b 5f 56 41 52 49 41 42 4c 45 2c 20 30 2c 20  TK_VARIABLE, 0, 
2e07d 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 73 71  0, pToken);.  sq
2e07e 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56  lite3ExprAssignV
2e07f 61 72 4e 75 6d 62 65 72 28 70 50 61 72 73 65 2c  arNumber(pParse,
2e080 20 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20   pExpr);.}.     
2e081 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e082 63 61 73 65 20 31 39 34 3a 20 2f 2a 20 65 78 70  case 194: /* exp
2e083 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4c 4c 41  r ::= expr COLLA
2e084 54 45 20 69 64 73 20 2a 2f 0a 7b 0a 20 20 79 79  TE ids */.{.  yy
2e085 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d  gotominor.yy72 =
2e086 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43   sqlite3ExprSetC
2e087 6f 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73  oll(pParse, yyms
2e088 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  p[-2].minor.yy72
2e089 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2e08a 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
2e08b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e08c 61 73 65 20 31 39 35 3a 20 2f 2a 20 65 78 70 72  ase 195: /* expr
2e08d 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20 65 78 70   ::= CAST LP exp
2e08e 72 20 41 53 20 74 79 70 65 74 6f 6b 65 6e 20 52  r AS typetoken R
2e08f 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  P */.{.  yygotom
2e090 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69  inor.yy72 = sqli
2e091 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2e092 20 54 4b 5f 43 41 53 54 2c 20 79 79 6d 73 70 5b   TK_CAST, yymsp[
2e093 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20  -3].minor.yy72, 
2e094 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0, &yymsp[-1].mi
2e095 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
2e096 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f  te3ExprSpan(yygo
2e097 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79  tominor.yy72,&yy
2e098 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
2e099 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
2e09a 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
2e09b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e09c 61 73 65 20 31 39 36 3a 20 2f 2a 20 65 78 70 72  ase 196: /* expr
2e09d 20 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73 74 69   ::= ID LP disti
2e09e 6e 63 74 20 65 78 70 72 6c 69 73 74 20 52 50 20  nct exprlist RP 
2e09f 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73 70  */.{.  if( yymsp
2e0a0 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38  [-1].minor.yy148
2e0a1 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69   && yymsp[-1].mi
2e0a2 6e 6f 72 2e 79 79 31 34 38 2d 3e 6e 45 78 70 72  nor.yy148->nExpr
2e0a3 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43  >SQLITE_MAX_FUNC
2e0a4 54 49 4f 4e 5f 41 52 47 20 29 7b 0a 20 20 20 20  TION_ARG ){.    
2e0a5 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2e0a6 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e  pParse, "too man
2e0a7 79 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 66  y arguments on f
2e0a8 75 6e 63 74 69 6f 6e 20 25 54 22 2c 20 26 79 79  unction %T", &yy
2e0a9 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2e0aa 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  0);.  }.  yygoto
2e0ab 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c  minor.yy72 = sql
2e0ac 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
2e0ad 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
2e0ae 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20  1].minor.yy148, 
2e0af 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
2e0b0 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  .yy0);.  sqlite3
2e0b1 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
2e0b2 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70  inor.yy72,&yymsp
2e0b3 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-4].minor.yy0,&
2e0b4 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e0b5 79 30 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  y0);.  if( yymsp
2e0b6 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34  [-2].minor.yy194
2e0b7 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   && yygotominor.
2e0b8 79 79 37 32 20 29 7b 0a 20 20 20 20 79 79 67 6f  yy72 ){.    yygo
2e0b9 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 66 6c  tominor.yy72->fl
2e0ba 61 67 73 20 7c 3d 20 45 50 5f 44 69 73 74 69 6e  ags |= EP_Distin
2e0bb 63 74 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20  ct;.  }.}.      
2e0bc 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e0bd 61 73 65 20 31 39 37 3a 20 2f 2a 20 65 78 70 72  ase 197: /* expr
2e0be 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52 20   ::= ID LP STAR 
2e0bf 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  RP */.{.  yygoto
2e0c0 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c  minor.yy72 = sql
2e0c1 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
2e0c2 28 70 50 61 72 73 65 2c 20 30 2c 20 26 79 79 6d  (pParse, 0, &yym
2e0c3 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2e0c4 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2e0c5 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2e0c6 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 33 5d  .yy72,&yymsp[-3]
2e0c7 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2e0c8 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2e0c9 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2e0ca 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 38  ;.      case 198
2e0cb 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 43 54  : /* term ::= CT
2e0cc 49 4d 45 5f 4b 57 20 2a 2f 0a 7b 0a 20 20 2f 2a  IME_KW */.{.  /*
2e0cd 20 54 68 65 20 43 55 52 52 45 4e 54 5f 54 49 4d   The CURRENT_TIM
2e0ce 45 2c 20 43 55 52 52 45 4e 54 5f 44 41 54 45 2c  E, CURRENT_DATE,
2e0cf 20 61 6e 64 20 43 55 52 52 45 4e 54 5f 54 49 4d   and CURRENT_TIM
2e0d0 45 53 54 41 4d 50 20 76 61 6c 75 65 73 20 61 72  ESTAMP values ar
2e0d1 65 0a 20 20 2a 2a 20 74 72 65 61 74 65 64 20 61  e.  ** treated a
2e0d2 73 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  s functions that
2e0d3 20 72 65 74 75 72 6e 20 63 6f 6e 73 74 61 6e 74   return constant
2e0d4 73 20 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  s */.  yygotomin
2e0d5 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65  or.yy72 = sqlite
2e0d6 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50  3ExprFunction(pP
2e0d7 61 72 73 65 2c 20 30 2c 26 79 79 6d 73 70 5b 30  arse, 0,&yymsp[0
2e0d8 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
2e0d9 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
2e0da 79 79 37 32 20 29 7b 0a 20 20 20 20 79 79 67 6f  yy72 ){.    yygo
2e0db 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 6f 70  tominor.yy72->op
2e0dc 20 3d 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43   = TK_CONST_FUNC
2e0dd 3b 20 20 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  ;  .    yygotomi
2e0de 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 20 3d  nor.yy72->span =
2e0df 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e0e0 79 79 30 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20  yy0;.  }.}.     
2e0e1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e0e2 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 65 78 70  case 199: /* exp
2e0e3 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20 65  r ::= expr AND e
2e0e4 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
2e0e5 65 20 32 30 30 3a 20 2f 2a 20 65 78 70 72 20 3a  e 200: /* expr :
2e0e6 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70 72 20  := expr OR expr 
2e0e7 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 30  */.      case 20
2e0e8 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  1: /* expr ::= e
2e0e9 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20  xpr LT|GT|GE|LE 
2e0ea 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
2e0eb 73 65 20 32 30 32 3a 20 2f 2a 20 65 78 70 72 20  se 202: /* expr 
2e0ec 3a 3a 3d 20 65 78 70 72 20 45 51 7c 4e 45 20 65  ::= expr EQ|NE e
2e0ed 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
2e0ee 65 20 32 30 33 3a 20 2f 2a 20 65 78 70 72 20 3a  e 203: /* expr :
2e0ef 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44 7c 42  := expr BITAND|B
2e0f0 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53 48 49  ITOR|LSHIFT|RSHI
2e0f1 46 54 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  FT expr */.     
2e0f2 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20 65 78   case 204: /* ex
2e0f3 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53  pr ::= expr PLUS
2e0f4 7c 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 0a 20  |MINUS expr */. 
2e0f5 20 20 20 20 20 63 61 73 65 20 32 30 35 3a 20 2f       case 205: /
2e0f6 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
2e0f7 53 54 41 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65  STAR|SLASH|REM e
2e0f8 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
2e0f9 65 20 32 30 36 3a 20 2f 2a 20 65 78 70 72 20 3a  e 206: /* expr :
2e0fa 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54 20 65  := expr CONCAT e
2e0fb 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  xpr */.{yygotomi
2e0fc 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74  nor.yy72 = sqlit
2e0fd 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 79  e3PExpr(pParse,y
2e0fe 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 79  ymsp[-1].major,y
2e0ff 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e100 79 37 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y72,yymsp[0].min
2e101 6f 72 2e 79 79 37 32 2c 30 29 3b 7d 0a 20 20 20  or.yy72,0);}.   
2e102 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e103 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 6c    case 207: /* l
2e104 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b  ikeop ::= LIKE_K
2e105 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  W */.      case 
2e106 32 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a  209: /* likeop :
2e107 3a 3d 20 4d 41 54 43 48 20 2a 2f 0a 7b 79 79 67  := MATCH */.{yyg
2e108 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 2e 65  otominor.yy392.e
2e109 4f 70 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70  Operator = yymsp
2e10a 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79  [0].minor.yy0; y
2e10b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32  ygotominor.yy392
2e10c 2e 6e 6f 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20  .not = 0;}.     
2e10d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e10e 63 61 73 65 20 32 30 38 3a 20 2f 2a 20 6c 69 6b  case 208: /* lik
2e10f 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45  eop ::= NOT LIKE
2e110 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  _KW */.      cas
2e111 65 20 32 31 30 3a 20 2f 2a 20 6c 69 6b 65 6f 70  e 210: /* likeop
2e112 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43 48 20 2a   ::= NOT MATCH *
2e113 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2e114 79 33 39 32 2e 65 4f 70 65 72 61 74 6f 72 20 3d  y392.eOperator =
2e115 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e116 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  yy0; yygotominor
2e117 2e 79 79 33 39 32 2e 6e 6f 74 20 3d 20 31 3b 7d  .yy392.not = 1;}
2e118 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e119 20 20 20 20 20 20 63 61 73 65 20 32 31 33 3a 20        case 213: 
2e11a 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
2e11b 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73 63   likeop expr esc
2e11c 61 70 65 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c  ape */.{.  ExprL
2e11d 69 73 74 20 2a 70 4c 69 73 74 3b 0a 20 20 70 4c  ist *pList;.  pL
2e11e 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
2e11f 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2e120 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  se,0, yymsp[-1].
2e121 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a  minor.yy72, 0);.
2e122 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
2e123 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2e124 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79  pParse,pList, yy
2e125 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2e126 37 32 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79  72, 0);.  if( yy
2e127 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37  msp[0].minor.yy7
2e128 32 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d  2 ){.    pList =
2e129 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2e12a 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c  Append(pParse,pL
2e12b 69 73 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  ist, yymsp[0].mi
2e12c 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a 20 20  nor.yy72, 0);.  
2e12d 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
2e12e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 45 78  yy72 = sqlite3Ex
2e12f 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  prFunction(pPars
2e130 65 2c 20 70 4c 69 73 74 2c 20 26 79 79 6d 73 70  e, pList, &yymsp
2e131 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  [-2].minor.yy392
2e132 2e 65 4f 70 65 72 61 74 6f 72 29 3b 0a 20 20 69  .eOperator);.  i
2e133 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  f( yymsp[-2].min
2e134 6f 72 2e 79 79 33 39 32 2e 6e 6f 74 20 29 20 79  or.yy392.not ) y
2e135 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2e136 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2e137 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79  Parse, TK_NOT, y
2e138 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c  ygotominor.yy72,
2e139 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
2e13a 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
2e13b 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 26 79 79 6d  minor.yy72, &yym
2e13c 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-3].minor.yy7
2e13d 32 2d 3e 73 70 61 6e 2c 20 26 79 79 6d 73 70 5b  2->span, &yymsp[
2e13e 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e  -1].minor.yy72->
2e13f 73 70 61 6e 29 3b 0a 20 20 69 66 28 20 79 79 67  span);.  if( yyg
2e140 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 20  otominor.yy72 ) 
2e141 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2e142 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e  ->flags |= EP_In
2e143 66 69 78 46 75 6e 63 3b 0a 7d 0a 20 20 20 20 20  fixFunc;.}.     
2e144 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e145 63 61 73 65 20 32 31 34 3a 20 2f 2a 20 65 78 70  case 214: /* exp
2e146 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55 4c  r ::= expr ISNUL
2e147 4c 7c 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20  L|NOTNULL */.{. 
2e148 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2e149 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2e14a 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30  (pParse, yymsp[0
2e14b 5d 2e 6d 61 6a 6f 72 2c 20 79 79 6d 73 70 5b 2d  ].major, yymsp[-
2e14c 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30  1].minor.yy72, 0
2e14d 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , 0);.  sqlite3E
2e14e 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2e14f 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b  nor.yy72,&yymsp[
2e150 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e  -1].minor.yy72->
2e151 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  span,&yymsp[0].m
2e152 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
2e153 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e154 20 20 63 61 73 65 20 32 31 35 3a 20 2f 2a 20 65    case 215: /* e
2e155 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20  xpr ::= expr IS 
2e156 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  NULL */.{.  yygo
2e157 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73  tominor.yy72 = s
2e158 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2e159 73 65 2c 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 79  se, TK_ISNULL, y
2e15a 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e15b 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71  y72, 0, 0);.  sq
2e15c 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79  lite3ExprSpan(yy
2e15d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26  gotominor.yy72,&
2e15e 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e15f 79 79 37 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73  yy72->span,&yyms
2e160 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2e161 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2e162 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 36  ;.      case 216
2e163 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
2e164 70 72 20 4e 4f 54 20 4e 55 4c 4c 20 2a 2f 0a 7b  pr NOT NULL */.{
2e165 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2e166 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  y72 = sqlite3PEx
2e167 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f  pr(pParse, TK_NO
2e168 54 4e 55 4c 4c 2c 20 79 79 6d 73 70 5b 2d 32 5d  TNULL, yymsp[-2]
2e169 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20  .minor.yy72, 0, 
2e16a 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
2e16b 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2e16c 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 32  r.yy72,&yymsp[-2
2e16d 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70  ].minor.yy72->sp
2e16e 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  an,&yymsp[0].min
2e16f 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
2e170 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e171 63 61 73 65 20 32 31 37 3a 20 2f 2a 20 65 78 70  case 217: /* exp
2e172 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f  r ::= expr IS NO
2e173 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79 79  T NULL */.{.  yy
2e174 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d  gotominor.yy72 =
2e175 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2e176 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  arse, TK_NOTNULL
2e177 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  , yymsp[-3].mino
2e178 72 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20  r.yy72, 0, 0);. 
2e179 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
2e17a 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  (yygotominor.yy7
2e17b 32 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  2,&yymsp[-3].min
2e17c 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 2c 26 79  or.yy72->span,&y
2e17d 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e17e 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2e17f 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e180 32 31 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  218: /* expr ::=
2e181 20 4e 4f 54 20 65 78 70 72 20 2a 2f 0a 20 20 20   NOT expr */.   
2e182 20 20 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20     case 219: /* 
2e183 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20  expr ::= BITNOT 
2e184 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  expr */.{.  yygo
2e185 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73  tominor.yy72 = s
2e186 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2e187 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61  se, yymsp[-1].ma
2e188 6a 6f 72 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  jor, yymsp[0].mi
2e189 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b  nor.yy72, 0, 0);
2e18a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70  .  sqlite3ExprSp
2e18b 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2e18c 79 37 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y72,&yymsp[-1].m
2e18d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2e18e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 73  0].minor.yy72->s
2e18f 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  pan);.}.        
2e190 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e191 65 20 32 32 30 3a 20 2f 2a 20 65 78 70 72 20 3a  e 220: /* expr :
2e192 3a 3d 20 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f  := MINUS expr */
2e193 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
2e194 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50  .yy72 = sqlite3P
2e195 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2e196 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d  UMINUS, yymsp[0]
2e197 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20  .minor.yy72, 0, 
2e198 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
2e199 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2e19a 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 31  r.yy72,&yymsp[-1
2e19b 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2e19c 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  sp[0].minor.yy72
2e19d 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20  ->span);.}.     
2e19e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e19f 63 61 73 65 20 32 32 31 3a 20 2f 2a 20 65 78 70  case 221: /* exp
2e1a0 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78 70 72 20  r ::= PLUS expr 
2e1a1 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2e1a2 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65  or.yy72 = sqlite
2e1a3 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2e1a4 4b 5f 55 50 4c 55 53 2c 20 79 79 6d 73 70 5b 30  K_UPLUS, yymsp[0
2e1a5 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c  ].minor.yy72, 0,
2e1a6 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78   0);.  sqlite3Ex
2e1a7 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e  prSpan(yygotomin
2e1a8 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d  or.yy72,&yymsp[-
2e1a9 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  1].minor.yy0,&yy
2e1aa 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37  msp[0].minor.yy7
2e1ab 32 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20  2->span);.}.    
2e1ac 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e1ad 20 63 61 73 65 20 32 32 34 3a 20 2f 2a 20 65 78   case 224: /* ex
2e1ae 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74 77  pr ::= expr betw
2e1af 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20  een_op expr AND 
2e1b0 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  expr */.{.  Expr
2e1b1 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 73 71  List *pList = sq
2e1b2 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2e1b3 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79  end(pParse,0, yy
2e1b4 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2e1b5 37 32 2c 20 30 29 3b 0a 20 20 70 4c 69 73 74 20  72, 0);.  pList 
2e1b6 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2e1b7 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70  tAppend(pParse,p
2e1b8 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  List, yymsp[0].m
2e1b9 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a 20  inor.yy72, 0);. 
2e1ba 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2e1bb 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2e1bc 28 70 50 61 72 73 65 2c 20 54 4b 5f 42 45 54 57  (pParse, TK_BETW
2e1bd 45 45 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  EEN, yymsp[-4].m
2e1be 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20 30 29  inor.yy72, 0, 0)
2e1bf 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ;.  if( yygotomi
2e1c0 6e 6f 72 2e 79 79 37 32 20 29 7b 0a 20 20 20 20  nor.yy72 ){.    
2e1c1 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2e1c2 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73  ->x.pList = pLis
2e1c3 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
2e1c4 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2e1c5 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2e1c6 2c 20 70 4c 69 73 74 29 3b 0a 20 20 7d 20 0a 20  , pList);.  } . 
2e1c7 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d   if( yymsp[-3].m
2e1c8 69 6e 6f 72 2e 79 79 31 39 34 20 29 20 79 79 67  inor.yy194 ) yyg
2e1c9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20  otominor.yy72 = 
2e1ca 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2e1cb 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
2e1cc 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30  otominor.yy72, 0
2e1cd 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , 0);.  sqlite3E
2e1ce 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2e1cf 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b  nor.yy72,&yymsp[
2e1d0 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e  -4].minor.yy72->
2e1d1 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  span,&yymsp[0].m
2e1d2 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 29  inor.yy72->span)
2e1d3 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2e1d4 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
2e1d5 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  7: /* expr ::= e
2e1d6 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 65 78 70  xpr in_op LP exp
2e1d7 72 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  rlist RP */.{.  
2e1d8 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2e1d9 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
2e1da 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c  r(pParse, TK_IN,
2e1db 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
2e1dc 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  .yy72, 0, 0);.  
2e1dd 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2e1de 72 2e 79 79 37 32 20 29 7b 0a 20 20 20 20 20 20  r.yy72 ){.      
2e1df 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2e1e0 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79 79 6d 73  ->x.pList = yyms
2e1e1 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2e1e2 38 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  8;.      sqlite3
2e1e3 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
2e1e4 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
2e1e5 72 2e 79 79 37 32 29 3b 0a 20 20 20 20 7d 65 6c  r.yy72);.    }el
2e1e6 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2e1e7 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2e1e8 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
2e1e9 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2e1ea 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  8);.    }.    if
2e1eb 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ( yymsp[-3].mino
2e1ec 72 2e 79 79 31 39 34 20 29 20 79 79 67 6f 74 6f  r.yy194 ) yygoto
2e1ed 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c  minor.yy72 = sql
2e1ee 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2e1ef 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f  , TK_NOT, yygoto
2e1f0 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20 30  minor.yy72, 0, 0
2e1f1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
2e1f2 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e  prSpan(yygotomin
2e1f3 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d  or.yy72,&yymsp[-
2e1f4 34 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 73  4].minor.yy72->s
2e1f5 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  pan,&yymsp[0].mi
2e1f6 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20  nor.yy0);.  }.  
2e1f7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e1f8 20 20 20 63 61 73 65 20 32 32 38 3a 20 2f 2a 20     case 228: /* 
2e1f9 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65  expr ::= LP sele
2e1fa 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 79  ct RP */.{.    y
2e1fb 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2e1fc 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2e1fd 50 61 72 73 65 2c 20 54 4b 5f 53 45 4c 45 43 54  Parse, TK_SELECT
2e1fe 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
2e1ff 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
2e200 79 79 37 32 20 29 7b 0a 20 20 20 20 20 20 79 79  yy72 ){.      yy
2e201 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e  gotominor.yy72->
2e202 78 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73  x.pSelect = yyms
2e203 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  p[-1].minor.yy24
2e204 33 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65 74  3;.      ExprSet
2e205 50 72 6f 70 65 72 74 79 28 79 79 67 6f 74 6f 6d  Property(yygotom
2e206 69 6e 6f 72 2e 79 79 37 32 2c 20 45 50 5f 78 49  inor.yy72, EP_xI
2e207 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  sSelect);.      
2e208 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
2e209 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67  ight(pParse, yyg
2e20a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 0a  otominor.yy72);.
2e20b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2e20c 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2e20d 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2e20e 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2e20f 2e 79 79 32 34 33 29 3b 0a 20 20 20 20 7d 0a 20  .yy243);.    }. 
2e210 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70     sqlite3ExprSp
2e211 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2e212 79 37 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y72,&yymsp[-2].m
2e213 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
2e214 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
2e215 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2e216 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 39  ;.      case 229
2e217 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
2e218 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65  pr in_op LP sele
2e219 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 79  ct RP */.{.    y
2e21a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2e21b 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2e21c 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79  Parse, TK_IN, yy
2e21d 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2e21e 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69  72, 0, 0);.    i
2e21f 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2e220 79 37 32 20 29 7b 0a 20 20 20 20 20 20 79 79 67  y72 ){.      yyg
2e221 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 78  otominor.yy72->x
2e222 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70  .pSelect = yymsp
2e223 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  [-1].minor.yy243
2e224 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 50  ;.      ExprSetP
2e225 72 6f 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69  roperty(yygotomi
2e226 6e 6f 72 2e 79 79 37 32 2c 20 45 50 5f 78 49 73  nor.yy72, EP_xIs
2e227 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73  Select);.      s
2e228 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
2e229 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f  ght(pParse, yygo
2e22a 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 0a 20  tominor.yy72);. 
2e22b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2e22c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2e22d 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
2e22e 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2e22f 79 79 32 34 33 29 3b 0a 20 20 20 20 7d 0a 20 20  yy243);.    }.  
2e230 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e    if( yymsp[-3].
2e231 6d 69 6e 6f 72 2e 79 79 31 39 34 20 29 20 79 79  minor.yy194 ) yy
2e232 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d  gotominor.yy72 =
2e233 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2e234 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79  arse, TK_NOT, yy
2e235 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 20  gotominor.yy72, 
2e236 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
2e237 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2e238 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d  ominor.yy72,&yym
2e239 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-4].minor.yy7
2e23a 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30  2->span,&yymsp[0
2e23b 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
2e23c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e23d 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 30 3a  .      case 230:
2e23e 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
2e23f 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 20  r in_op nm dbnm 
2e240 2a 2f 0a 7b 0a 20 20 20 20 53 72 63 4c 69 73 74  */.{.    SrcList
2e241 20 2a 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33   *pSrc = sqlite3
2e242 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50  SrcListAppend(pP
2e243 61 72 73 65 2d 3e 64 62 2c 20 30 2c 26 79 79 6d  arse->db, 0,&yym
2e244 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2e245 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2e246 2e 79 79 30 29 3b 0a 20 20 20 20 79 79 67 6f 74  .yy0);.    yygot
2e247 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71  ominor.yy72 = sq
2e248 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2e249 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b  e, TK_IN, yymsp[
2e24a 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20  -3].minor.yy72, 
2e24b 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79  0, 0);.    if( y
2e24c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2e24d 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d  ){.      yygotom
2e24e 69 6e 6f 72 2e 79 79 37 32 2d 3e 78 2e 70 53 65  inor.yy72->x.pSe
2e24f 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
2e250 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20  lectNew(pParse, 
2e251 30 2c 70 53 72 63 2c 30 2c 30 2c 30 2c 30 2c 30  0,pSrc,0,0,0,0,0
2e252 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 45 78 70  ,0,0);.      Exp
2e253 72 53 65 74 50 72 6f 70 65 72 74 79 28 79 79 67  rSetProperty(yyg
2e254 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 45  otominor.yy72, E
2e255 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20  P_xIsSelect);.  
2e256 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
2e257 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
2e258 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2e259 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  2);.    }else{. 
2e25a 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
2e25b 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
2e25c 2d 3e 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 20  ->db, pSrc);.   
2e25d 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70   }.    if( yymsp
2e25e 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34  [-2].minor.yy194
2e25f 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
2e260 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  y72 = sqlite3PEx
2e261 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f  pr(pParse, TK_NO
2e262 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  T, yygotominor.y
2e263 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  y72, 0, 0);.    
2e264 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
2e265 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2e266 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
2e267 72 2e 79 79 37 32 2d 3e 73 70 61 6e 2c 79 79 6d  r.yy72->span,yym
2e268 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
2e269 7a 3f 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  z?&yymsp[0].mino
2e26a 72 2e 79 79 30 3a 26 79 79 6d 73 70 5b 2d 31 5d  r.yy0:&yymsp[-1]
2e26b 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d  .minor.yy0);.  }
2e26c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e26d 20 20 20 20 20 20 63 61 73 65 20 32 33 31 3a 20        case 231: 
2e26e 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 45 58 49 53  /* expr ::= EXIS
2e26f 54 53 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20  TS LP select RP 
2e270 2a 2f 0a 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  */.{.    Expr *p
2e271 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   = yygotominor.y
2e272 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78  y72 = sqlite3PEx
2e273 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 58  pr(pParse, TK_EX
2e274 49 53 54 53 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  ISTS, 0, 0, 0);.
2e275 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
2e276 20 20 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 20     p->x.pSelect 
2e277 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
2e278 72 2e 79 79 32 34 33 3b 0a 20 20 20 20 20 20 45  r.yy243;.      E
2e279 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 79  xprSetProperty(y
2e27a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c  ygotominor.yy72,
2e27b 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a   EP_xIsSelect);.
2e27c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2e27d 72 53 70 61 6e 28 70 2c 26 79 79 6d 73 70 5b 2d  rSpan(p,&yymsp[-
2e27e 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  3].minor.yy0,&yy
2e27f 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2e280 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2e281 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
2e282 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
2e283 72 2e 79 79 37 32 29 3b 0a 20 20 20 20 7d 65 6c  r.yy72);.    }el
2e284 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2e285 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50  3SelectDelete(pP
2e286 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b  arse->db, yymsp[
2e287 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 29  -1].minor.yy243)
2e288 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 20 20  ;.    }.  }.    
2e289 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e28a 20 63 61 73 65 20 32 33 32 3a 20 2f 2a 20 65 78   case 232: /* ex
2e28b 70 72 20 3a 3a 3d 20 43 41 53 45 20 63 61 73 65  pr ::= CASE case
2e28c 5f 6f 70 65 72 61 6e 64 20 63 61 73 65 5f 65 78  _operand case_ex
2e28d 70 72 6c 69 73 74 20 63 61 73 65 5f 65 6c 73 65  prlist case_else
2e28e 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f   END */.{.  yygo
2e28f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73  tominor.yy72 = s
2e290 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2e291 73 65 2c 20 54 4b 5f 43 41 53 45 2c 20 79 79 6d  se, TK_CASE, yym
2e292 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-3].minor.yy7
2e293 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  2, yymsp[-1].min
2e294 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a 20 20 69  or.yy72, 0);.  i
2e295 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2e296 79 37 32 20 29 7b 0a 20 20 20 20 79 79 67 6f 74  y72 ){.    yygot
2e297 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 78 2e 70  ominor.yy72->x.p
2e298 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  List = yymsp[-2]
2e299 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 3b 0a 20 20  .minor.yy148;.  
2e29a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
2e29b 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79  Height(pParse, y
2e29c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 29  ygotominor.yy72)
2e29d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
2e29e 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2e29f 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2e2a0 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2e2a1 2e 79 79 31 34 38 29 3b 0a 20 20 7d 0a 20 20 73  .yy148);.  }.  s
2e2a2 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
2e2a3 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c  ygotominor.yy72,
2e2a4 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f   &yymsp[-4].mino
2e2a5 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d  r.yy0, &yymsp[0]
2e2a6 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
2e2a7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e2a8 20 20 20 20 63 61 73 65 20 32 33 33 3a 20 2f 2a      case 233: /*
2e2a9 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a   case_exprlist :
2e2aa 3a 3d 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74  := case_exprlist
2e2ab 20 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20   WHEN expr THEN 
2e2ac 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  expr */.{.  yygo
2e2ad 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20  tominor.yy148 = 
2e2ae 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2e2af 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d  ppend(pParse,yym
2e2b0 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
2e2b1 34 38 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  48, yymsp[-2].mi
2e2b2 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a 20 20  nor.yy72, 0);.  
2e2b3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
2e2b4 38 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  8 = sqlite3ExprL
2e2b5 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2e2b6 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  ,yygotominor.yy1
2e2b7 34 38 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  48, yymsp[0].min
2e2b8 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a 7d 0a 20  or.yy72, 0);.}. 
2e2b9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e2ba 20 20 20 20 63 61 73 65 20 32 33 34 3a 20 2f 2a      case 234: /*
2e2bb 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a   case_exprlist :
2e2bc 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  := WHEN expr THE
2e2bd 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79  N expr */.{.  yy
2e2be 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20  gotominor.yy148 
2e2bf 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2e2c0 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
2e2c1 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
2e2c2 72 2e 79 79 37 32 2c 20 30 29 3b 0a 20 20 79 79  r.yy72, 0);.  yy
2e2c3 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20  gotominor.yy148 
2e2c4 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2e2c5 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79  tAppend(pParse,y
2e2c6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38  ygotominor.yy148
2e2c7 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2e2c8 2e 79 79 37 32 2c 20 30 29 3b 0a 7d 0a 20 20 20  .yy72, 0);.}.   
2e2c9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e2ca 20 20 63 61 73 65 20 32 34 33 3a 20 2f 2a 20 63    case 243: /* c
2e2cb 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20  md ::= createkw 
2e2cc 75 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45 58  uniqueflag INDEX
2e2cd 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2e2ce 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64  dbnm ON nm LP id
2e2cf 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  xlist RP */.{.  
2e2d0 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
2e2d1 65 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  ex(pParse, &yyms
2e2d2 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-6].minor.yy0,
2e2d3 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f   &yymsp[-5].mino
2e2d4 72 2e 79 79 30 2c 20 0a 20 20 20 20 20 20 20 20  r.yy0, .        
2e2d5 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2e2d6 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
2e2d7 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26  d(pParse->db,0,&
2e2d8 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2e2d9 79 79 30 2c 30 29 2c 20 79 79 6d 73 70 5b 2d 31  yy0,0), yymsp[-1
2e2da 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20 79  ].minor.yy148, y
2e2db 79 6d 73 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-9].minor.y
2e2dc 79 31 39 34 2c 0a 20 20 20 20 20 20 20 20 20 20  y194,.          
2e2dd 20 20 20 20 20 20 20 20 20 20 20 20 26 79 79 6d              &yym
2e2de 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79  sp[-10].minor.yy
2e2df 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
2e2e0 6f 72 2e 79 79 30 2c 20 53 51 4c 49 54 45 5f 53  or.yy0, SQLITE_S
2e2e1 4f 5f 41 53 43 2c 20 79 79 6d 73 70 5b 2d 37 5d  O_ASC, yymsp[-7]
2e2e2 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 0a 7d  .minor.yy194);.}
2e2e3 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e2e4 20 20 20 20 20 20 63 61 73 65 20 32 34 34 3a 20        case 244: 
2e2e5 2f 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a  /* uniqueflag ::
2e2e6 3d 20 55 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20  = UNIQUE */.    
2e2e7 20 20 63 61 73 65 20 32 39 31 3a 20 2f 2a 20 72    case 291: /* r
2e2e8 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f  aisetype ::= ABO
2e2e9 52 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  RT */.{yygotomin
2e2ea 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45 5f 41 62  or.yy194 = OE_Ab
2e2eb 6f 72 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ort;}.        br
2e2ec 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e2ed 32 34 35 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c  245: /* uniquefl
2e2ee 61 67 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  ag ::= */.{yygot
2e2ef 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4f  ominor.yy194 = O
2e2f0 45 5f 4e 6f 6e 65 3b 7d 0a 20 20 20 20 20 20 20  E_None;}.       
2e2f1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e2f2 73 65 20 32 34 38 3a 20 2f 2a 20 69 64 78 6c 69  se 248: /* idxli
2e2f3 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43  st ::= idxlist C
2e2f4 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74 65 20  OMMA nm collate 
2e2f5 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20  sortorder */.{. 
2e2f6 20 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20   Expr *p = 0;.  
2e2f7 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  if( yymsp[-1].mi
2e2f8 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20  nor.yy0.n>0 ){. 
2e2f9 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 45     p = sqlite3PE
2e2fa 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43  xpr(pParse, TK_C
2e2fb 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20 30 29 3b  OLUMN, 0, 0, 0);
2e2fc 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2e2fd 53 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20  SetColl(pParse, 
2e2fe 70 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  p, &yymsp[-1].mi
2e2ff 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20  nor.yy0);.  }.  
2e300 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
2e301 38 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  8 = sqlite3ExprL
2e302 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2e303 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
2e304 2e 79 79 31 34 38 2c 20 70 2c 20 26 79 79 6d 73  .yy148, p, &yyms
2e305 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-2].minor.yy0)
2e306 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
2e307 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70  istCheckLength(p
2e308 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
2e309 6f 72 2e 79 79 31 34 38 2c 20 22 69 6e 64 65 78  or.yy148, "index
2e30a 22 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  ");.  if( yygoto
2e30b 6d 69 6e 6f 72 2e 79 79 31 34 38 20 29 20 79 79  minor.yy148 ) yy
2e30c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 2d  gotominor.yy148-
2e30d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  >a[yygotominor.y
2e30e 79 31 34 38 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73  y148->nExpr-1].s
2e30f 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79  ortOrder = (u8)y
2e310 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e311 31 39 34 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  194;.}.        b
2e312 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e313 20 32 34 39 3a 20 2f 2a 20 69 64 78 6c 69 73 74   249: /* idxlist
2e314 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65 20   ::= nm collate 
2e315 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20  sortorder */.{. 
2e316 20 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20   Expr *p = 0;.  
2e317 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  if( yymsp[-1].mi
2e318 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20  nor.yy0.n>0 ){. 
2e319 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 45     p = sqlite3PE
2e31a 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43  xpr(pParse, TK_C
2e31b 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20 30 29 3b  OLUMN, 0, 0, 0);
2e31c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2e31d 53 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20  SetColl(pParse, 
2e31e 70 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  p, &yymsp[-1].mi
2e31f 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20  nor.yy0);.  }.  
2e320 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
2e321 38 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  8 = sqlite3ExprL
2e322 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2e323 2c 30 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 32  ,0, p, &yymsp[-2
2e324 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
2e325 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43  sqlite3ExprListC
2e326 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73  heckLength(pPars
2e327 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  e, yygotominor.y
2e328 79 31 34 38 2c 20 22 69 6e 64 65 78 22 29 3b 0a  y148, "index");.
2e329 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2e32a 72 2e 79 79 31 34 38 20 29 20 79 79 67 6f 74 6f  r.yy148 ) yygoto
2e32b 6d 69 6e 6f 72 2e 79 79 31 34 38 2d 3e 61 5b 79  minor.yy148->a[y
2e32c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38  ygotominor.yy148
2e32d 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f  ->nExpr-1].sortO
2e32e 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70  rder = (u8)yymsp
2e32f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 3b  [0].minor.yy194;
2e330 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2e331 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 30  ;.      case 250
2e332 3a 20 2f 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d  : /* collate ::=
2e333 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2e334 2e 79 79 30 2e 7a 20 3d 20 30 3b 20 79 79 67 6f  .yy0.z = 0; yygo
2e335 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20  tominor.yy0.n = 
2e336 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2e337 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  k;.      case 25
2e338 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52  2: /* cmd ::= DR
2e339 4f 50 20 49 4e 44 45 58 20 69 66 65 78 69 73 74  OP INDEX ifexist
2e33a 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 73  s fullname */.{s
2e33b 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28  qlite3DropIndex(
2e33c 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
2e33d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 20 79 79  .minor.yy185, yy
2e33e 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2e33f 31 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  194);}.        b
2e340 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e341 20 32 35 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   253: /* cmd ::=
2e342 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20 20 20   VACUUM */.     
2e343 20 63 61 73 65 20 32 35 34 3a 20 2f 2a 20 63 6d   case 254: /* cm
2e344 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 20  d ::= VACUUM nm 
2e345 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 61 63 75 75  */.{sqlite3Vacuu
2e346 6d 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20  m(pParse);}.    
2e347 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e348 20 63 61 73 65 20 32 35 35 3a 20 2f 2a 20 63 6d   case 255: /* cm
2e349 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
2e34a 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 20 2a 2f  dbnm EQ nmnum */
2e34b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 36 3a  .      case 256:
2e34c 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47   /* cmd ::= PRAG
2e34d 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 4f 4e  MA nm dbnm EQ ON
2e34e 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2e34f 35 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50  57: /* cmd ::= P
2e350 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
2e351 20 44 45 4c 45 54 45 20 2a 2f 0a 7b 73 71 6c 69   DELETE */.{sqli
2e352 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65  te3Pragma(pParse
2e353 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
2e354 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d  r.yy0,&yymsp[-2]
2e355 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2e356 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30  p[0].minor.yy0,0
2e357 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2e358 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  k;.      case 25
2e359 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52  8: /* cmd ::= PR
2e35a 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
2e35b 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0a 7b 0a 20  minus_num */.{. 
2e35c 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70   sqlite3Pragma(p
2e35d 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d  Parse,&yymsp[-3]
2e35e 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2e35f 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
2e360 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2e361 79 79 30 2c 31 29 3b 0a 7d 0a 20 20 20 20 20 20  yy0,1);.}.      
2e362 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e363 61 73 65 20 32 35 39 3a 20 2f 2a 20 63 6d 64 20  ase 259: /* cmd 
2e364 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
2e365 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52 50 20 2a  nm LP nmnum RP *
2e366 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61  /.{sqlite3Pragma
2e367 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
2e368 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  4].minor.yy0,&yy
2e369 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2e36a 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
2e36b 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20 20 20  or.yy0,0);}.    
2e36c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e36d 20 63 61 73 65 20 32 36 30 3a 20 2f 2a 20 63 6d   case 260: /* cm
2e36e 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
2e36f 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  dbnm */.{sqlite3
2e370 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79  Pragma(pParse,&y
2e371 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e372 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2e373 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 7d 0a 20 20  or.yy0,0,0);}.  
2e374 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e375 20 20 20 63 61 73 65 20 32 36 38 3a 20 2f 2a 20     case 268: /* 
2e376 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  cmd ::= createkw
2e377 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45   trigger_decl BE
2e378 47 49 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f  GIN trigger_cmd_
2e379 6c 69 73 74 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20  list END */.{.  
2e37a 54 6f 6b 65 6e 20 61 6c 6c 3b 0a 20 20 61 6c 6c  Token all;.  all
2e37b 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  .z = yymsp[-3].m
2e37c 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 61 6c  inor.yy0.z;.  al
2e37d 6c 2e 6e 20 3d 20 28 69 6e 74 29 28 79 79 6d 73  l.n = (int)(yyms
2e37e 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
2e37f 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   - yymsp[-3].min
2e380 6f 72 2e 79 79 30 2e 7a 29 20 2b 20 79 79 6d 73  or.yy0.z) + yyms
2e381 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
2e382 3b 0a 20 20 73 71 6c 69 74 65 33 46 69 6e 69 73  ;.  sqlite3Finis
2e383 68 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  hTrigger(pParse,
2e384 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2e385 2e 79 79 31 34 35 2c 20 26 61 6c 6c 29 3b 0a 7d  .yy145, &all);.}
2e386 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e387 20 20 20 20 20 20 63 61 73 65 20 32 36 39 3a 20        case 269: 
2e388 2f 2a 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20  /* trigger_decl 
2e389 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47 45 52  ::= temp TRIGGER
2e38a 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2e38b 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74 69 6d  dbnm trigger_tim
2e38c 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  e trigger_event 
2e38d 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65  ON fullname fore
2e38e 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f  ach_clause when_
2e38f 63 6c 61 75 73 65 20 2a 2f 0a 7b 0a 20 20 73 71  clause */.{.  sq
2e390 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65  lite3BeginTrigge
2e391 72 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70  r(pParse, &yymsp
2e392 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-7].minor.yy0, 
2e393 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  &yymsp[-6].minor
2e394 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e  .yy0, yymsp[-5].
2e395 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 20 79 79 6d  minor.yy194, yym
2e396 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
2e397 33 32 2e 61 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  32.a, yymsp[-4].
2e398 6d 69 6e 6f 72 2e 79 79 33 33 32 2e 62 2c 20 79  minor.yy332.b, y
2e399 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e39a 79 31 38 35 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  y185, yymsp[0].m
2e39b 69 6e 6f 72 2e 79 79 37 32 2c 20 79 79 6d 73 70  inor.yy72, yymsp
2e39c 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  [-10].minor.yy19
2e39d 34 2c 20 79 79 6d 73 70 5b 2d 38 5d 2e 6d 69 6e  4, yymsp[-8].min
2e39e 6f 72 2e 79 79 31 39 34 29 3b 0a 20 20 79 79 67  or.yy194);.  yyg
2e39f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 28  otominor.yy0 = (
2e3a0 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
2e3a1 79 79 30 2e 6e 3d 3d 30 3f 79 79 6d 73 70 5b 2d  yy0.n==0?yymsp[-
2e3a2 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a 79 79 6d  7].minor.yy0:yym
2e3a3 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-6].minor.yy0
2e3a4 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2e3a5 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2e3a6 37 30 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74  70: /* trigger_t
2e3a7 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 20 2a  ime ::= BEFORE *
2e3a8 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 33  /.      case 273
2e3a9 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d  : /* trigger_tim
2e3aa 65 20 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74  e ::= */.{ yygot
2e3ab 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 54  ominor.yy194 = T
2e3ac 4b 5f 42 45 46 4f 52 45 3b 20 7d 0a 20 20 20 20  K_BEFORE; }.    
2e3ad 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e3ae 20 63 61 73 65 20 32 37 31 3a 20 2f 2a 20 74 72   case 271: /* tr
2e3af 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41  igger_time ::= A
2e3b0 46 54 45 52 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  FTER */.{ yygoto
2e3b1 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 54 4b  minor.yy194 = TK
2e3b2 5f 41 46 54 45 52 3b 20 20 7d 0a 20 20 20 20 20  _AFTER;  }.     
2e3b3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e3b4 63 61 73 65 20 32 37 32 3a 20 2f 2a 20 74 72 69  case 272: /* tri
2e3b5 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e  gger_time ::= IN
2e3b6 53 54 45 41 44 20 4f 46 20 2a 2f 0a 7b 20 79 79  STEAD OF */.{ yy
2e3b7 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20  gotominor.yy194 
2e3b8 3d 20 54 4b 5f 49 4e 53 54 45 41 44 3b 7d 0a 20  = TK_INSTEAD;}. 
2e3b9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e3ba 20 20 20 20 63 61 73 65 20 32 37 34 3a 20 2f 2a      case 274: /*
2e3bb 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a   trigger_event :
2e3bc 3a 3d 20 44 45 4c 45 54 45 7c 49 4e 53 45 52 54  := DELETE|INSERT
2e3bd 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2e3be 37 35 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65  75: /* trigger_e
2e3bf 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20  vent ::= UPDATE 
2e3c0 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e3c1 79 79 33 33 32 2e 61 20 3d 20 79 79 6d 73 70 5b  yy332.a = yymsp[
2e3c2 30 5d 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f 74 6f  0].major; yygoto
2e3c3 6d 69 6e 6f 72 2e 79 79 33 33 32 2e 62 20 3d 20  minor.yy332.b = 
2e3c4 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2e3c5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
2e3c6 36 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76  6: /* trigger_ev
2e3c7 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f  ent ::= UPDATE O
2e3c8 46 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a  F inscollist */.
2e3c9 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
2e3ca 33 32 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54 45  32.a = TK_UPDATE
2e3cb 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
2e3cc 33 33 32 2e 62 20 3d 20 79 79 6d 73 70 5b 30 5d  332.b = yymsp[0]
2e3cd 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 3b 7d 0a 20  .minor.yy254;}. 
2e3ce 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e3cf 20 20 20 20 63 61 73 65 20 32 37 39 3a 20 2f 2a      case 279: /*
2e3d0 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d   when_clause ::=
2e3d1 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2e3d2 39 36 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a  96: /* key_opt :
2e3d3 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  := */.{ yygotomi
2e3d4 6e 6f 72 2e 79 79 37 32 20 3d 20 30 3b 20 7d 0a  nor.yy72 = 0; }.
2e3d5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e3d6 20 20 20 20 20 63 61 73 65 20 32 38 30 3a 20 2f       case 280: /
2e3d7 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a  * when_clause ::
2e3d8 3d 20 57 48 45 4e 20 65 78 70 72 20 2a 2f 0a 20  = WHEN expr */. 
2e3d9 20 20 20 20 20 63 61 73 65 20 32 39 37 3a 20 2f       case 297: /
2e3da 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45  * key_opt ::= KE
2e3db 59 20 65 78 70 72 20 2a 2f 0a 7b 20 79 79 67 6f  Y expr */.{ yygo
2e3dc 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 79  tominor.yy72 = y
2e3dd 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e3de 37 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  72; }.        br
2e3df 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e3e0 32 38 31 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  281: /* trigger_
2e3e1 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69  cmd_list ::= tri
2e3e2 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72  gger_cmd_list tr
2e3e3 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a  igger_cmd SEMI *
2e3e4 2f 0a 7b 0a 2f 2a 0a 20 20 69 66 28 20 79 79 6d  /.{./*.  if( yym
2e3e5 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2e3e6 34 35 20 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b  45 ){.    yymsp[
2e3e7 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 2d  -2].minor.yy145-
2e3e8 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20  >pLast->pNext = 
2e3e9 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2e3ea 79 79 31 34 35 3b 0a 20 20 7d 65 6c 73 65 7b 0a  yy145;.  }else{.
2e3eb 20 20 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69      yymsp[-2].mi
2e3ec 6e 6f 72 2e 79 79 31 34 35 20 3d 20 79 79 6d 73  nor.yy145 = yyms
2e3ed 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2e3ee 35 3b 0a 20 20 7d 0a 2a 2f 0a 20 20 61 73 73 65  5;.  }.*/.  asse
2e3ef 72 74 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  rt( yymsp[-2].mi
2e3f0 6e 6f 72 2e 79 79 31 34 35 21 3d 30 20 29 3b 0a  nor.yy145!=0 );.
2e3f1 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f    yymsp[-2].mino
2e3f2 72 2e 79 79 31 34 35 2d 3e 70 4c 61 73 74 2d 3e  r.yy145->pLast->
2e3f3 70 4e 65 78 74 20 3d 20 79 79 6d 73 70 5b 2d 31  pNext = yymsp[-1
2e3f4 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 3b 0a 20  ].minor.yy145;. 
2e3f5 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2e3f6 2e 79 79 31 34 35 2d 3e 70 4c 61 73 74 20 3d 20  .yy145->pLast = 
2e3f7 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2e3f8 79 79 31 34 35 3b 0a 20 20 79 79 67 6f 74 6f 6d  yy145;.  yygotom
2e3f9 69 6e 6f 72 2e 79 79 31 34 35 20 3d 20 79 79 6d  inor.yy145 = yym
2e3fa 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2e3fb 34 35 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  45;.}.        br
2e3fc 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e3fd 32 38 32 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  282: /* trigger_
2e3fe 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69  cmd_list ::= tri
2e3ff 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f  gger_cmd SEMI */
2e400 0a 7b 20 0a 20 20 2f 2a 20 69 66 28 20 79 79 6d  .{ .  /* if( yym
2e401 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2e402 34 35 20 29 20 2a 2f 0a 20 20 61 73 73 65 72 74  45 ) */.  assert
2e403 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ( yymsp[-1].mino
2e404 72 2e 79 79 31 34 35 21 3d 30 20 29 3b 0a 20 20  r.yy145!=0 );.  
2e405 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2e406 79 79 31 34 35 2d 3e 70 4c 61 73 74 20 3d 20 79  yy145->pLast = y
2e407 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e408 79 31 34 35 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y145;.  yygotomi
2e409 6e 6f 72 2e 79 79 31 34 35 20 3d 20 79 79 6d 73  nor.yy145 = yyms
2e40a 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2e40b 35 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  5;.}.        bre
2e40c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2e40d 38 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63  83: /* trigger_c
2e40e 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72  md ::= UPDATE or
2e40f 63 6f 6e 66 20 6e 6d 20 53 45 54 20 73 65 74 6c  conf nm SET setl
2e410 69 73 74 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f  ist where_opt */
2e411 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
2e412 79 31 34 35 20 3d 20 73 71 6c 69 74 65 33 54 72  y145 = sqlite3Tr
2e413 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70 28  iggerUpdateStep(
2e414 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d  pParse->db, &yym
2e415 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2e416 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2e417 72 2e 79 79 31 34 38 2c 20 79 79 6d 73 70 5b 30  r.yy148, yymsp[0
2e418 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 79 79  ].minor.yy72, yy
2e419 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2e41a 31 39 34 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  194); }.        
2e41b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e41c 65 20 32 38 34 3a 20 2f 2a 20 74 72 69 67 67 65  e 284: /* trigge
2e41d 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74  r_cmd ::= insert
2e41e 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73  _cmd INTO nm ins
2e41f 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55  collist_opt VALU
2e420 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52  ES LP itemlist R
2e421 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  P */.{yygotomino
2e422 72 2e 79 79 31 34 35 20 3d 20 73 71 6c 69 74 65  r.yy145 = sqlite
2e423 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74  3TriggerInsertSt
2e424 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26  ep(pParse->db, &
2e425 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
2e426 79 79 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  yy0, yymsp[-4].m
2e427 69 6e 6f 72 2e 79 79 32 35 34 2c 20 79 79 6d 73  inor.yy254, yyms
2e428 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2e429 38 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e  8, 0, yymsp[-7].
2e42a 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a 20  minor.yy194);}. 
2e42b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e42c 20 20 20 20 63 61 73 65 20 32 38 35 3a 20 2f 2a      case 285: /*
2e42d 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
2e42e 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
2e42f 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f   nm inscollist_o
2e430 70 74 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79  pt select */.{yy
2e431 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 35 20  gotominor.yy145 
2e432 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
2e433 49 6e 73 65 72 74 53 74 65 70 28 70 50 61 72 73  InsertStep(pPars
2e434 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 32  e->db, &yymsp[-2
2e435 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d  ].minor.yy0, yym
2e436 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-1].minor.yy2
2e437 35 34 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e  54, 0, yymsp[0].
2e438 6d 69 6e 6f 72 2e 79 79 32 34 33 2c 20 79 79 6d  minor.yy243, yym
2e439 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
2e43a 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  94);}.        br
2e43b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e43c 32 38 36 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  286: /* trigger_
2e43d 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46  cmd ::= DELETE F
2e43e 52 4f 4d 20 6e 6d 20 77 68 65 72 65 5f 6f 70 74  ROM nm where_opt
2e43f 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2e440 2e 79 79 31 34 35 20 3d 20 73 71 6c 69 74 65 33  .yy145 = sqlite3
2e441 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65  TriggerDeleteSte
2e442 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79  p(pParse->db, &y
2e443 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e444 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0, yymsp[0].min
2e445 6f 72 2e 79 79 37 32 29 3b 7d 0a 20 20 20 20 20  or.yy72);}.     
2e446 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e447 63 61 73 65 20 32 38 37 3a 20 2f 2a 20 74 72 69  case 287: /* tri
2e448 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c  gger_cmd ::= sel
2e449 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ect */.{yygotomi
2e44a 6e 6f 72 2e 79 79 31 34 35 20 3d 20 73 71 6c 69  nor.yy145 = sqli
2e44b 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74  te3TriggerSelect
2e44c 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
2e44d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e44e 79 79 32 34 33 29 3b 20 7d 0a 20 20 20 20 20 20  yy243); }.      
2e44f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e450 61 73 65 20 32 38 38 3a 20 2f 2a 20 65 78 70 72  ase 288: /* expr
2e451 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 49 47   ::= RAISE LP IG
2e452 4e 4f 52 45 20 52 50 20 2a 2f 0a 7b 0a 20 20 79  NORE RP */.{.  y
2e453 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2e454 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2e455 50 61 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c  Parse, TK_RAISE,
2e456 20 30 2c 20 30 2c 20 30 29 3b 20 0a 20 20 69 66   0, 0, 0); .  if
2e457 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2e458 37 32 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f  72 ){.    yygoto
2e459 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 61 66 66 69  minor.yy72->affi
2e45a 6e 69 74 79 20 3d 20 4f 45 5f 49 67 6e 6f 72 65  nity = OE_Ignore
2e45b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2e45c 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2e45d 72 2e 79 79 37 32 2c 20 26 79 79 6d 73 70 5b 2d  r.yy72, &yymsp[-
2e45e 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  3].minor.yy0, &y
2e45f 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e460 30 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20  0);.  }.}.      
2e461 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e462 61 73 65 20 32 38 39 3a 20 2f 2a 20 65 78 70 72  ase 289: /* expr
2e463 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61   ::= RAISE LP ra
2e464 69 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d  isetype COMMA nm
2e465 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
2e466 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71  ominor.yy72 = sq
2e467 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2e468 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20  e, TK_RAISE, 0, 
2e469 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0, &yymsp[-1].mi
2e46a 6e 6f 72 2e 79 79 30 29 3b 20 0a 20 20 69 66 28  nor.yy0); .  if(
2e46b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2e46c 32 20 29 20 7b 0a 20 20 20 20 79 79 67 6f 74 6f  2 ) {.    yygoto
2e46d 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 61 66 66 69  minor.yy72->affi
2e46e 6e 69 74 79 20 3d 20 28 63 68 61 72 29 79 79 6d  nity = (char)yym
2e46f 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
2e470 39 34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  94;.    sqlite3E
2e471 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2e472 6e 6f 72 2e 79 79 37 32 2c 20 26 79 79 6d 73 70  nor.yy72, &yymsp
2e473 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-5].minor.yy0, 
2e474 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2e475 79 79 30 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20  yy0);.  }.}.    
2e476 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e477 20 63 61 73 65 20 32 39 30 3a 20 2f 2a 20 72 61   case 290: /* ra
2e478 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c  isetype ::= ROLL
2e479 42 41 43 4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  BACK */.{yygotom
2e47a 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45 5f  inor.yy194 = OE_
2e47b 52 6f 6c 6c 62 61 63 6b 3b 7d 0a 20 20 20 20 20  Rollback;}.     
2e47c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e47d 63 61 73 65 20 32 39 32 3a 20 2f 2a 20 72 61 69  case 292: /* rai
2e47e 73 65 74 79 70 65 20 3a 3a 3d 20 46 41 49 4c 20  setype ::= FAIL 
2e47f 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e480 79 79 31 39 34 20 3d 20 4f 45 5f 46 61 69 6c 3b  yy194 = OE_Fail;
2e481 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e482 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 33 3a  .      case 293:
2e483 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50   /* cmd ::= DROP
2e484 20 54 52 49 47 47 45 52 20 69 66 65 78 69 73 74   TRIGGER ifexist
2e485 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a  s fullname */.{.
2e486 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69    sqlite3DropTri
2e487 67 67 65 72 28 70 50 61 72 73 65 2c 79 79 6d 73  gger(pParse,yyms
2e488 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35  p[0].minor.yy185
2e489 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2e48a 2e 79 79 31 39 34 29 3b 0a 7d 0a 20 20 20 20 20  .yy194);.}.     
2e48b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e48c 63 61 73 65 20 32 39 34 3a 20 2f 2a 20 63 6d 64  case 294: /* cmd
2e48d 20 3a 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61   ::= ATTACH data
2e48e 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72  base_kw_opt expr
2e48f 20 41 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74   AS expr key_opt
2e490 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41   */.{.  sqlite3A
2e491 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 79 79  ttach(pParse, yy
2e492 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2e493 37 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  72, yymsp[-1].mi
2e494 6e 6f 72 2e 79 79 37 32 2c 20 79 79 6d 73 70 5b  nor.yy72, yymsp[
2e495 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 0a  0].minor.yy72);.
2e496 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e497 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 35 3a  .      case 295:
2e498 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41   /* cmd ::= DETA
2e499 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  CH database_kw_o
2e49a 70 74 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73  pt expr */.{.  s
2e49b 71 6c 69 74 65 33 44 65 74 61 63 68 28 70 50 61  qlite3Detach(pPa
2e49c 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
2e49d 6e 6f 72 2e 79 79 37 32 29 3b 0a 7d 0a 20 20 20  nor.yy72);.}.   
2e49e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e49f 20 20 63 61 73 65 20 33 30 30 3a 20 2f 2a 20 63    case 300: /* c
2e4a0 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 2a  md ::= REINDEX *
2e4a1 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65  /.{sqlite3Reinde
2e4a2 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b  x(pParse, 0, 0);
2e4a3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e4a4 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 31 3a  .      case 301:
2e4a5 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e   /* cmd ::= REIN
2e4a6 44 45 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b  DEX nm dbnm */.{
2e4a7 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70  sqlite3Reindex(p
2e4a8 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31  Parse, &yymsp[-1
2e4a9 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
2e4aa 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2e4ab 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2e4ac 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
2e4ad 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e  2: /* cmd ::= AN
2e4ae 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74 65  ALYZE */.{sqlite
2e4af 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c  3Analyze(pParse,
2e4b0 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20   0, 0);}.       
2e4b1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e4b2 73 65 20 33 30 33 3a 20 2f 2a 20 63 6d 64 20 3a  se 303: /* cmd :
2e4b3 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62  := ANALYZE nm db
2e4b4 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 6e  nm */.{sqlite3An
2e4b5 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 26 79  alyze(pParse, &y
2e4b6 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e4b7 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
2e4b8 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
2e4b9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e4ba 63 61 73 65 20 33 30 34 3a 20 2f 2a 20 63 6d 64  case 304: /* cmd
2e4bb 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45   ::= ALTER TABLE
2e4bc 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45   fullname RENAME
2e4bd 20 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71   TO nm */.{.  sq
2e4be 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65  lite3AlterRename
2e4bf 54 61 62 6c 65 28 70 50 61 72 73 65 2c 79 79 6d  Table(pParse,yym
2e4c0 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
2e4c1 38 35 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  85,&yymsp[0].min
2e4c2 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
2e4c3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e4c4 63 61 73 65 20 33 30 35 3a 20 2f 2a 20 63 6d 64  case 305: /* cmd
2e4c5 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45   ::= ALTER TABLE
2e4c6 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c   add_column_full
2e4c7 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d  name ADD kwcolum
2e4c8 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  n_opt column */.
2e4c9 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  {.  sqlite3Alter
2e4ca 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28  FinishAddColumn(
2e4cb 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30  pParse, &yymsp[0
2e4cc 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
2e4cd 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e4ce 20 20 20 20 20 63 61 73 65 20 33 30 36 3a 20 2f       case 306: /
2e4cf 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c  * add_column_ful
2e4d0 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61  lname ::= fullna
2e4d1 6d 65 20 2a 2f 0a 7b 0a 20 20 70 50 61 72 73 65  me */.{.  pParse
2e4d2 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ->db->lookaside.
2e4d3 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20  bEnabled = 0;.  
2e4d4 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69  sqlite3AlterBegi
2e4d5 6e 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73  nAddColumn(pPars
2e4d6 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e, yymsp[0].mino
2e4d7 72 2e 79 79 31 38 35 29 3b 0a 7d 0a 20 20 20 20  r.yy185);.}.    
2e4d8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e4d9 20 63 61 73 65 20 33 30 39 3a 20 2f 2a 20 63 6d   case 309: /* cm
2e4da 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61  d ::= create_vta
2e4db 62 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61  b */.{sqlite3Vta
2e4dc 62 46 69 6e 69 73 68 50 61 72 73 65 28 70 50 61  bFinishParse(pPa
2e4dd 72 73 65 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20  rse,0);}.       
2e4de 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e4df 73 65 20 33 31 30 3a 20 2f 2a 20 63 6d 64 20 3a  se 310: /* cmd :
2e4e0 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 20 4c  := create_vtab L
2e4e1 50 20 76 74 61 62 61 72 67 6c 69 73 74 20 52 50  P vtabarglist RP
2e4e2 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62   */.{sqlite3Vtab
2e4e3 46 69 6e 69 73 68 50 61 72 73 65 28 70 50 61 72  FinishParse(pPar
2e4e4 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se,&yymsp[0].min
2e4e5 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2e4e6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e4e7 61 73 65 20 33 31 31 3a 20 2f 2a 20 63 72 65 61  ase 311: /* crea
2e4e8 74 65 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65 61  te_vtab ::= crea
2e4e9 74 65 6b 77 20 56 49 52 54 55 41 4c 20 54 41 42  tekw VIRTUAL TAB
2e4ea 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47  LE nm dbnm USING
2e4eb 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c   nm */.{.    sql
2e4ec 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 72  ite3VtabBeginPar
2e4ed 73 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  se(pParse, &yyms
2e4ee 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
2e4ef 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
2e4f0 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d  r.yy0, &yymsp[0]
2e4f1 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
2e4f2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e4f3 20 20 20 20 63 61 73 65 20 33 31 34 3a 20 2f 2a      case 314: /*
2e4f4 20 76 74 61 62 61 72 67 20 3a 3a 3d 20 2a 2f 0a   vtabarg ::= */.
2e4f5 7b 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 49  {sqlite3VtabArgI
2e4f6 6e 69 74 28 70 50 61 72 73 65 29 3b 7d 0a 20 20  nit(pParse);}.  
2e4f7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e4f8 20 20 20 63 61 73 65 20 33 31 36 3a 20 2f 2a 20     case 316: /* 
2e4f9 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d  vtabargtoken ::=
2e4fa 20 41 4e 59 20 2a 2f 0a 20 20 20 20 20 20 63 61   ANY */.      ca
2e4fb 73 65 20 33 31 37 3a 20 2f 2a 20 76 74 61 62 61  se 317: /* vtaba
2e4fc 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61  rgtoken ::= lp a
2e4fd 6e 79 6c 69 73 74 20 52 50 20 2a 2f 0a 20 20 20  nylist RP */.   
2e4fe 20 20 20 63 61 73 65 20 33 31 38 3a 20 2f 2a 20     case 318: /* 
2e4ff 6c 70 20 3a 3a 3d 20 4c 50 20 2a 2f 0a 20 20 20  lp ::= LP */.   
2e500 20 20 20 63 61 73 65 20 33 32 30 3a 20 2f 2a 20     case 320: /* 
2e501 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c  anylist ::= anyl
2e502 69 73 74 20 41 4e 59 20 2a 2f 0a 7b 73 71 6c 69  ist ANY */.{sqli
2e503 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64  te3VtabArgExtend
2e504 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30  (pParse,&yymsp[0
2e505 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
2e506 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e507 7d 3b 0a 20 20 79 79 67 6f 74 6f 20 3d 20 79 79  };.  yygoto = yy
2e508 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e  RuleInfo[yyrulen
2e509 6f 5d 2e 6c 68 73 3b 0a 20 20 79 79 73 69 7a 65  o].lhs;.  yysize
2e50a 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79   = yyRuleInfo[yy
2e50b 72 75 6c 65 6e 6f 5d 2e 6e 72 68 73 3b 0a 20 20  ruleno].nrhs;.  
2e50c 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
2e50d 20 2d 3d 20 79 79 73 69 7a 65 3b 0a 20 20 79 79   -= yysize;.  yy
2e50e 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65  act = yy_find_re
2e50f 64 75 63 65 5f 61 63 74 69 6f 6e 28 79 79 6d 73  duce_action(yyms
2e510 70 5b 2d 79 79 73 69 7a 65 5d 2e 73 74 61 74 65  p[-yysize].state
2e511 6e 6f 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79  no,(YYCODETYPE)y
2e512 79 67 6f 74 6f 29 3b 0a 20 20 69 66 28 20 79 79  ygoto);.  if( yy
2e513 61 63 74 20 3c 20 59 59 4e 53 54 41 54 45 20 29  act < YYNSTATE )
2e514 7b 0a 23 69 66 64 65 66 20 4e 44 45 42 55 47 0a  {.#ifdef NDEBUG.
2e515 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65      /* If we are
2e516 20 6e 6f 74 20 64 65 62 75 67 67 69 6e 67 20 61   not debugging a
2e517 6e 64 20 74 68 65 20 72 65 64 75 63 65 20 61 63  nd the reduce ac
2e518 74 69 6f 6e 20 70 6f 70 70 65 64 20 61 74 20 6c  tion popped at l
2e519 65 61 73 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20  east.    ** one 
2e51a 65 6c 65 6d 65 6e 74 20 6f 66 66 20 74 68 65 20  element off the 
2e51b 73 74 61 63 6b 2c 20 74 68 65 6e 20 77 65 20 63  stack, then we c
2e51c 61 6e 20 70 75 73 68 20 74 68 65 20 6e 65 77 20  an push the new 
2e51d 65 6c 65 6d 65 6e 74 20 62 61 63 6b 0a 20 20 20  element back.   
2e51e 20 2a 2a 20 6f 6e 74 6f 20 74 68 65 20 73 74 61   ** onto the sta
2e51f 63 6b 20 68 65 72 65 2c 20 61 6e 64 20 73 6b 69  ck here, and ski
2e520 70 20 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72  p the stack over
2e521 66 6c 6f 77 20 74 65 73 74 20 69 6e 20 79 79 5f  flow test in yy_
2e522 73 68 69 66 74 28 29 2e 0a 20 20 20 20 2a 2a 20  shift()..    ** 
2e523 54 68 61 74 20 67 69 76 65 73 20 61 20 73 69 67  That gives a sig
2e524 6e 69 66 69 63 61 6e 74 20 73 70 65 65 64 20 69  nificant speed i
2e525 6d 70 72 6f 76 65 6d 65 6e 74 2e 20 2a 2f 0a 20  mprovement. */. 
2e526 20 20 20 69 66 28 20 79 79 73 69 7a 65 20 29 7b     if( yysize ){
2e527 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72  .      yypParser
2e528 2d 3e 79 79 69 64 78 2b 2b 3b 0a 20 20 20 20 20  ->yyidx++;.     
2e529 20 79 79 6d 73 70 20 2d 3d 20 79 79 73 69 7a 65   yymsp -= yysize
2e52a 2d 31 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d  -1;.      yymsp-
2e52b 3e 73 74 61 74 65 6e 6f 20 3d 20 28 59 59 41 43  >stateno = (YYAC
2e52c 54 49 4f 4e 54 59 50 45 29 79 79 61 63 74 3b 0a  TIONTYPE)yyact;.
2e52d 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 6d 61 6a        yymsp->maj
2e52e 6f 72 20 3d 20 28 59 59 43 4f 44 45 54 59 50 45  or = (YYCODETYPE
2e52f 29 79 79 67 6f 74 6f 3b 0a 20 20 20 20 20 20 79  )yygoto;.      y
2e530 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d 20 79 79  ymsp->minor = yy
2e531 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 7d  gotominor;.    }
2e532 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
2e533 7b 0a 20 20 20 20 20 20 79 79 5f 73 68 69 66 74  {.      yy_shift
2e534 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
2e535 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f 74 6f 6d  ,yygoto,&yygotom
2e536 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  inor);.    }.  }
2e537 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
2e538 28 20 79 79 61 63 74 20 3d 3d 20 59 59 4e 53 54  ( yyact == YYNST
2e539 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 2b 20  ATE + YYNRULE + 
2e53a 31 20 29 3b 0a 20 20 20 20 79 79 5f 61 63 63 65  1 );.    yy_acce
2e53b 70 74 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  pt(yypParser);. 
2e53c 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
2e53d 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65  following code e
2e53e 78 65 63 75 74 65 73 20 77 68 65 6e 20 74 68 65  xecutes when the
2e53f 20 70 61 72 73 65 20 66 61 69 6c 73 0a 2a 2f 0a   parse fails.*/.
2e540 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 70  static void yy_p
2e541 61 72 73 65 5f 66 61 69 6c 65 64 28 0a 20 20 79  arse_failed(.  y
2e542 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2e543 65 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er           /* 
2e544 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b  The parser */.){
2e545 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2e546 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64  ARG_FETCH;.#ifnd
2e547 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
2e548 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
2e549 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
2e54a 63 65 46 49 4c 45 2c 22 25 73 46 61 69 6c 21 5c  ceFILE,"%sFail!\
2e54b 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
2e54c 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
2e54d 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72  while( yypParser
2e54e 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
2e54f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
2e550 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f  (yypParser);.  /
2e551 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69  * Here code is i
2e552 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69  nserted which wi
2e553 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ll be executed w
2e554 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a  henever the.  **
2e555 20 70 61 72 73 65 72 20 66 61 69 6c 73 20 2a 2f   parser fails */
2e556 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2e557 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
2e558 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
2e559 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
2e55a 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69  ra_argument vari
2e55b 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a  able */.}../*.**
2e55c 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
2e55d 6f 64 65 20 65 78 65 63 75 74 65 73 20 77 68 65  ode executes whe
2e55e 6e 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72  n a syntax error
2e55f 20 66 69 72 73 74 20 6f 63 63 75 72 73 2e 0a 2a   first occurs..*
2e560 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  /.static void yy
2e561 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 0a 20  _syntax_error(. 
2e562 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61   yyParser *yypPa
2e563 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  rser,           
2e564 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2e565 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20  .  int yymajor, 
2e566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e567 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74    /* The major t
2e568 79 70 65 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ype of the error
2e569 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d 49   token */.  YYMI
2e56a 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 20  NORTYPE yyminor 
2e56b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2e56c 65 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f 66 20  e minor type of 
2e56d 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20  the error token 
2e56e 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50  */.){.  sqlite3P
2e56f 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a  arserARG_FETCH;.
2e570 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20 28 79  #define TOKEN (y
2e571 79 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20 20 55  yminor.yy0)..  U
2e572 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
2e573 79 79 6d 61 6a 6f 72 29 3b 20 20 2f 2a 20 53 69  yymajor);  /* Si
2e574 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69  lence some compi
2e575 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0a  ler warnings */.
2e576 20 20 61 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e    assert( TOKEN.
2e577 7a 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20  z[0] );  /* The 
2e578 74 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79 73  tokenizer always
2e579 20 67 69 76 65 73 20 75 73 20 61 20 74 6f 6b 65   gives us a toke
2e57a 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72  n */.  sqlite3Er
2e57b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2e57c 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e  near \"%T\": syn
2e57d 74 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b  tax error", &TOK
2e57e 45 4e 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70  EN);.  pParse->p
2e57f 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20  arseError = 1;. 
2e580 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2e581 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70  G_STORE; /* Supp
2e582 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f  ress warning abo
2e583 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61  ut unused %extra
2e584 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62  _argument variab
2e585 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  le */.}../*.** T
2e586 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
2e587 65 78 65 63 75 74 65 64 20 77 68 65 6e 20 74 68  executed when th
2e588 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73  e parser accepts
2e589 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2e58a 79 79 5f 61 63 63 65 70 74 28 0a 20 20 79 79 50  yy_accept(.  yyP
2e58b 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
2e58c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2e58d 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20  e parser */.){. 
2e58e 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2e58f 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66  G_FETCH;.#ifndef
2e590 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
2e591 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
2e592 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2e593 46 49 4c 45 2c 22 25 73 41 63 63 65 70 74 21 5c  FILE,"%sAccept!\
2e594 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
2e595 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
2e596 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72  while( yypParser
2e597 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
2e598 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
2e599 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f  (yypParser);.  /
2e59a 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69  * Here code is i
2e59b 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69  nserted which wi
2e59c 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ll be executed w
2e59d 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a  henever the.  **
2e59e 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73 20   parser accepts 
2e59f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
2e5a0 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20  erARG_STORE; /* 
2e5a1 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67  Suppress warning
2e5a2 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65   about unused %e
2e5a3 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61  xtra_argument va
2e5a4 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20  riable */.}../* 
2e5a5 54 68 65 20 6d 61 69 6e 20 70 61 72 73 65 72 20  The main parser 
2e5a6 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68 65 20  program..** The 
2e5a7 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
2e5a8 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
2e5a9 20 73 74 72 75 63 74 75 72 65 20 6f 62 74 61 69   structure obtai
2e5aa 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c  ned from.** "sql
2e5ab 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 22  ite3ParserAlloc"
2e5ac 20 77 68 69 63 68 20 64 65 73 63 72 69 62 65 73   which describes
2e5ad 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
2e5ae 74 65 20 6f 66 20 74 68 65 20 70 61 72 73 65 72  te of the parser
2e5af 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
2e5b0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2e5b1 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  major token numb
2e5b2 65 72 2e 20 20 54 68 65 20 74 68 69 72 64 20 69  er.  The third i
2e5b3 73 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74  s.** the minor t
2e5b4 6f 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75 72 74  oken.  The fourt
2e5b5 68 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d  h optional argum
2e5b6 65 6e 74 20 69 73 20 77 68 61 74 65 76 65 72 20  ent is whatever 
2e5b7 74 68 65 0a 2a 2a 20 75 73 65 72 20 77 61 6e 74  the.** user want
2e5b8 73 20 28 61 6e 64 20 73 70 65 63 69 66 69 65 64  s (and specified
2e5b9 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 29   in the grammar)
2e5ba 20 61 6e 64 20 69 73 20 61 76 61 69 6c 61 62 6c   and is availabl
2e5bb 65 20 66 6f 72 0a 2a 2a 20 75 73 65 20 62 79 20  e for.** use by 
2e5bc 74 68 65 20 61 63 74 69 6f 6e 20 72 6f 75 74 69  the action routi
2e5bd 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74  nes..**.** Input
2e5be 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  s:.** <ul>.** <l
2e5bf 69 3e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  i> A pointer to 
2e5c0 74 68 65 20 70 61 72 73 65 72 20 28 61 6e 20 6f  the parser (an o
2e5c1 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 2e  paque structure.
2e5c2 29 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 61  ).** <li> The ma
2e5c3 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72  jor token number
2e5c4 2e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 69  ..** <li> The mi
2e5c5 6e 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72  nor token number
2e5c6 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70 74  ..** <li> An opt
2e5c7 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20  ion argument of 
2e5c8 61 20 67 72 61 6d 6d 61 72 2d 73 70 65 63 69 66  a grammar-specif
2e5c9 69 65 64 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75  ied type..** </u
2e5ca 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73  l>.**.** Outputs
2e5cb 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51  :.** None..*/.SQ
2e5cc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2e5cd 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28  d sqlite3Parser(
2e5ce 0a 20 20 76 6f 69 64 20 2a 79 79 70 2c 20 20 20  .  void *yyp,   
2e5cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e5d0 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2e5d1 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20  .  int yymajor, 
2e5d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e5d3 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b  /* The major tok
2e5d4 65 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 2a  en code number *
2e5d5 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
2e5d6 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d 69 6e  rTOKENTYPE yymin
2e5d7 6f 72 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  or       /* The 
2e5d8 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 74 6f  value for the to
2e5d9 6b 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ken */.  sqlite3
2e5da 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20  ParserARG_PDECL 
2e5db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e5dc 20 4f 70 74 69 6f 6e 61 6c 20 25 65 78 74 72 61   Optional %extra
2e5dd 5f 61 72 67 75 6d 65 6e 74 20 70 61 72 61 6d 65  _argument parame
2e5de 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49  ter */.){.  YYMI
2e5df 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 75  NORTYPE yyminoru
2e5e0 6e 69 6f 6e 3b 0a 20 20 69 6e 74 20 79 79 61 63  nion;.  int yyac
2e5e1 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
2e5e2 20 54 68 65 20 70 61 72 73 65 72 20 61 63 74 69   The parser acti
2e5e3 6f 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65  on. */.  int yye
2e5e4 6e 64 6f 66 69 6e 70 75 74 3b 20 20 20 20 20 2f  ndofinput;     /
2e5e5 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65  * True if we are
2e5e6 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69   at the end of i
2e5e7 6e 70 75 74 20 2a 2f 0a 23 69 66 64 65 66 20 59  nput */.#ifdef Y
2e5e8 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69  YERRORSYMBOL.  i
2e5e9 6e 74 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20  nt yyerrorhit = 
2e5ea 30 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  0;   /* True if 
2e5eb 79 79 6d 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f  yymajor has invo
2e5ec 6b 65 64 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ked an error */.
2e5ed 23 65 6e 64 69 66 0a 20 20 79 79 50 61 72 73 65  #endif.  yyParse
2e5ee 72 20 2a 79 79 70 50 61 72 73 65 72 3b 20 20 2f  r *yypParser;  /
2e5ef 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
2e5f0 0a 20 20 2f 2a 20 28 72 65 29 69 6e 69 74 69 61  .  /* (re)initia
2e5f1 6c 69 7a 65 20 74 68 65 20 70 61 72 73 65 72 2c  lize the parser,
2e5f2 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f   if necessary */
2e5f3 0a 20 20 79 79 70 50 61 72 73 65 72 20 3d 20 28  .  yypParser = (
2e5f4 79 79 50 61 72 73 65 72 2a 29 79 79 70 3b 0a 20  yyParser*)yyp;. 
2e5f5 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
2e5f6 79 79 69 64 78 3c 30 20 29 7b 0a 23 69 66 20 59  yyidx<0 ){.#if Y
2e5f7 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20  YSTACKDEPTH<=0. 
2e5f8 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
2e5f9 2d 3e 79 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b  ->yystksz <=0 ){
2e5fa 0a 20 20 20 20 20 20 2f 2a 6d 65 6d 73 65 74 28  .      /*memset(
2e5fb 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30  &yyminorunion, 0
2e5fc 2c 20 73 69 7a 65 6f 66 28 79 79 6d 69 6e 6f 72  , sizeof(yyminor
2e5fd 75 6e 69 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20 20  union));*/.     
2e5fe 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20   yyminorunion = 
2e5ff 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20  yyzerominor;.   
2e600 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c     yyStackOverfl
2e601 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20 26 79  ow(yypParser, &y
2e602 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
2e603 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2e604 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 79 79 70  }.#endif.    yyp
2e605 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20  Parser->yyidx = 
2e606 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72  0;.    yypParser
2e607 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b  ->yyerrcnt = -1;
2e608 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  .    yypParser->
2e609 79 79 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65  yystack[0].state
2e60a 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 79 79 70 50  no = 0;.    yypP
2e60b 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30  arser->yystack[0
2e60c 5d 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d  ].major = 0;.  }
2e60d 0a 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e  .  yyminorunion.
2e60e 79 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20  yy0 = yyminor;. 
2e60f 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20   yyendofinput = 
2e610 28 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20  (yymajor==0);.  
2e611 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2e612 5f 53 54 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66  _STORE;..#ifndef
2e613 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
2e614 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
2e615 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2e616 46 49 4c 45 2c 22 25 73 49 6e 70 75 74 20 25 73  FILE,"%sInput %s
2e617 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70  \n",yyTracePromp
2e618 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79  t,yyTokenName[yy
2e619 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e  major]);.  }.#en
2e61a 64 69 66 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79  dif..  do{.    y
2e61b 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73  yact = yy_find_s
2e61c 68 69 66 74 5f 61 63 74 69 6f 6e 28 79 79 70 50  hift_action(yypP
2e61d 61 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50  arser,(YYCODETYP
2e61e 45 29 79 79 6d 61 6a 6f 72 29 3b 0a 20 20 20 20  E)yymajor);.    
2e61f 69 66 28 20 79 79 61 63 74 3c 59 59 4e 53 54 41  if( yyact<YYNSTA
2e620 54 45 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  TE ){.      asse
2e621 72 74 28 20 21 79 79 65 6e 64 6f 66 69 6e 70 75  rt( !yyendofinpu
2e622 74 20 29 3b 20 20 2f 2a 20 49 6d 70 6f 73 73 69  t );  /* Impossi
2e623 62 6c 65 20 74 6f 20 73 68 69 66 74 20 74 68 65  ble to shift the
2e624 20 24 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20   $ token */.    
2e625 20 20 79 79 5f 73 68 69 66 74 28 79 79 70 50 61    yy_shift(yypPa
2e626 72 73 65 72 2c 79 79 61 63 74 2c 79 79 6d 61 6a  rser,yyact,yymaj
2e627 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
2e628 29 3b 0a 20 20 20 20 20 20 79 79 70 50 61 72 73  );.      yypPars
2e629 65 72 2d 3e 79 79 65 72 72 63 6e 74 2d 2d 3b 0a  er->yyerrcnt--;.
2e62a 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20        yymajor = 
2e62b 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 7d 65  YYNOCODE;.    }e
2e62c 6c 73 65 20 69 66 28 20 79 79 61 63 74 20 3c 20  lse if( yyact < 
2e62d 59 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55  YYNSTATE + YYNRU
2e62e 4c 45 20 29 7b 0a 20 20 20 20 20 20 79 79 5f 72  LE ){.      yy_r
2e62f 65 64 75 63 65 28 79 79 70 50 61 72 73 65 72 2c  educe(yypParser,
2e630 79 79 61 63 74 2d 59 59 4e 53 54 41 54 45 29 3b  yyact-YYNSTATE);
2e631 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2e632 20 20 61 73 73 65 72 74 28 20 79 79 61 63 74 20    assert( yyact 
2e633 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49  == YY_ERROR_ACTI
2e634 4f 4e 20 29 3b 0a 23 69 66 64 65 66 20 59 59 45  ON );.#ifdef YYE
2e635 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20  RRORSYMBOL.     
2e636 20 69 6e 74 20 79 79 6d 78 3b 0a 23 65 6e 64 69   int yymx;.#endi
2e637 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  f.#ifndef NDEBUG
2e638 0a 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61  .      if( yyTra
2e639 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20  ceFILE ){.      
2e63a 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
2e63b 65 46 49 4c 45 2c 22 25 73 53 79 6e 74 61 78 20  eFILE,"%sSyntax 
2e63c 45 72 72 6f 72 21 5c 6e 22 2c 79 79 54 72 61 63  Error!\n",yyTrac
2e63d 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 20 20  ePrompt);.      
2e63e 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  }.#endif.#ifdef 
2e63f 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20  YYERRORSYMBOL.  
2e640 20 20 20 20 2f 2a 20 41 20 73 79 6e 74 61 78 20      /* A syntax 
2e641 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
2e642 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65  ed..      ** The
2e643 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20   response to an 
2e644 65 72 72 6f 72 20 64 65 70 65 6e 64 73 20 75 70  error depends up
2e645 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  on whether or no
2e646 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 67  t the.      ** g
2e647 72 61 6d 6d 61 72 20 64 65 66 69 6e 65 73 20 61  rammar defines a
2e648 6e 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 22 45  n error token "E
2e649 52 52 4f 52 22 2e 20 20 0a 20 20 20 20 20 20 2a  RROR".  .      *
2e64a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20  *.      ** This 
2e64b 69 73 20 77 68 61 74 20 77 65 20 64 6f 20 69 66  is what we do if
2e64c 20 74 68 65 20 67 72 61 6d 6d 61 72 20 64 6f 65   the grammar doe
2e64d 73 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a  s define ERROR:.
2e64e 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2e64f 2a 20 20 2a 20 43 61 6c 6c 20 74 68 65 20 25 73  *  * Call the %s
2e650 79 6e 74 61 78 5f 65 72 72 6f 72 20 66 75 6e 63  yntax_error func
2e651 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  tion..      **. 
2e652 20 20 20 20 20 2a 2a 20 20 2a 20 42 65 67 69 6e       **  * Begin
2e653 20 70 6f 70 70 69 6e 67 20 74 68 65 20 73 74 61   popping the sta
2e654 63 6b 20 75 6e 74 69 6c 20 77 65 20 65 6e 74 65  ck until we ente
2e655 72 20 61 20 73 74 61 74 65 20 77 68 65 72 65 0a  r a state where.
2e656 20 20 20 20 20 20 2a 2a 20 20 20 20 69 74 20 69        **    it i
2e657 73 20 6c 65 67 61 6c 20 74 6f 20 73 68 69 66 74  s legal to shift
2e658 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f   the error symbo
2e659 6c 2c 20 74 68 65 6e 20 73 68 69 66 74 0a 20 20  l, then shift.  
2e65a 20 20 20 20 2a 2a 20 20 20 20 74 68 65 20 65 72      **    the er
2e65b 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a 20 20 20 20  ror symbol..    
2e65c 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a    **.      **  *
2e65d 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 63   Set the error c
2e65e 6f 75 6e 74 20 74 6f 20 74 68 72 65 65 2e 0a 20  ount to three.. 
2e65f 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
2e660 20 20 2a 20 42 65 67 69 6e 20 61 63 63 65 70 74    * Begin accept
2e661 69 6e 67 20 61 6e 64 20 73 68 69 66 74 69 6e 67  ing and shifting
2e662 20 6e 65 77 20 74 6f 6b 65 6e 73 2e 20 20 4e 6f   new tokens.  No
2e663 20 6e 65 77 20 65 72 72 6f 72 0a 20 20 20 20 20   new error.     
2e664 20 2a 2a 20 20 20 20 70 72 6f 63 65 73 73 69 6e   **    processin
2e665 67 20 77 69 6c 6c 20 6f 63 63 75 72 20 75 6e 74  g will occur unt
2e666 69 6c 20 74 68 72 65 65 20 74 6f 6b 65 6e 73 20  il three tokens 
2e667 68 61 76 65 20 62 65 65 6e 0a 20 20 20 20 20 20  have been.      
2e668 2a 2a 20 20 20 20 73 68 69 66 74 65 64 20 73 75  **    shifted su
2e669 63 63 65 73 73 66 75 6c 6c 79 2e 0a 20 20 20 20  ccessfully..    
2e66a 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2f 0a 20 20    **.      */.  
2e66b 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65      if( yypParse
2e66c 72 2d 3e 79 79 65 72 72 63 6e 74 3c 30 20 29 7b  r->yyerrcnt<0 ){
2e66d 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74  .        yy_synt
2e66e 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73  ax_error(yypPars
2e66f 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e  er,yymajor,yymin
2e670 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20  orunion);.      
2e671 7d 0a 20 20 20 20 20 20 79 79 6d 78 20 3d 20 79  }.      yymx = y
2e672 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  ypParser->yystac
2e673 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  k[yypParser->yyi
2e674 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20 20 20 20 20  dx].major;.     
2e675 20 69 66 28 20 79 79 6d 78 3d 3d 59 59 45 52 52   if( yymx==YYERR
2e676 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20 79 79 65 72  ORSYMBOL || yyer
2e677 72 6f 72 68 69 74 20 29 7b 0a 23 69 66 6e 64 65  rorhit ){.#ifnde
2e678 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20  f NDEBUG.       
2e679 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
2e67a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70   ){.          fp
2e67b 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2e67c 45 2c 22 25 73 44 69 73 63 61 72 64 20 69 6e 70  E,"%sDiscard inp
2e67d 75 74 20 74 6f 6b 65 6e 20 25 73 5c 6e 22 2c 0a  ut token %s\n",.
2e67e 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79 54               yyT
2e67f 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b  racePrompt,yyTok
2e680 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29  enName[yymajor])
2e681 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64  ;.        }.#end
2e682 69 66 0a 20 20 20 20 20 20 20 20 79 79 5f 64 65  if.        yy_de
2e683 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72 73  structor(yypPars
2e684 65 72 2c 20 28 59 59 43 4f 44 45 54 59 50 45 29  er, (YYCODETYPE)
2e685 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
2e686 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20  union);.        
2e687 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f  yymajor = YYNOCO
2e688 44 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  DE;.      }else{
2e689 0a 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  .         while(
2e68a 0a 20 20 20 20 20 20 20 20 20 20 79 79 70 50 61  .          yypPa
2e68b 72 73 65 72 2d 3e 79 79 69 64 78 20 3e 3d 20 30  rser->yyidx >= 0
2e68c 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 79 79   &&.          yy
2e68d 6d 78 20 21 3d 20 59 59 45 52 52 4f 52 53 59 4d  mx != YYERRORSYM
2e68e 42 4f 4c 20 26 26 0a 20 20 20 20 20 20 20 20 20  BOL &&.         
2e68f 20 28 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e   (yyact = yy_fin
2e690 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28  d_reduce_action(
2e691 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e692 20 20 20 20 20 20 20 20 20 79 79 70 50 61 72 73           yypPars
2e693 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50  er->yystack[yypP
2e694 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74  arser->yyidx].st
2e695 61 74 65 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20  ateno,.         
2e696 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 59                 Y
2e697 59 45 52 52 4f 52 53 59 4d 42 4f 4c 29 29 20 3e  YERRORSYMBOL)) >
2e698 3d 20 59 59 4e 53 54 41 54 45 0a 20 20 20 20 20  = YYNSTATE.     
2e699 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
2e69a 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74  yy_pop_parser_st
2e69b 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a  ack(yypParser);.
2e69c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2e69d 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
2e69e 3e 79 79 69 64 78 20 3c 20 30 20 7c 7c 20 79 79  >yyidx < 0 || yy
2e69f 6d 61 6a 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20  major==0 ){.    
2e6a0 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75 63        yy_destruc
2e6a1 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28 59  tor(yypParser,(Y
2e6a2 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f  YCODETYPE)yymajo
2e6a3 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29  r,&yyminorunion)
2e6a4 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 70  ;.          yy_p
2e6a5 61 72 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50  arse_failed(yypP
2e6a6 61 72 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20  arser);.        
2e6a7 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f    yymajor = YYNO
2e6a8 43 4f 44 45 3b 0a 20 20 20 20 20 20 20 20 7d 65  CODE;.        }e
2e6a9 6c 73 65 20 69 66 28 20 79 79 6d 78 21 3d 59 59  lse if( yymx!=YY
2e6aa 45 52 52 4f 52 53 59 4d 42 4f 4c 20 29 7b 0a 20  ERRORSYMBOL ){. 
2e6ab 20 20 20 20 20 20 20 20 20 59 59 4d 49 4e 4f 52           YYMINOR
2e6ac 54 59 50 45 20 75 32 3b 0a 20 20 20 20 20 20 20  TYPE u2;.       
2e6ad 20 20 20 75 32 2e 59 59 45 52 52 53 59 4d 44 54     u2.YYERRSYMDT
2e6ae 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
2e6af 79 79 5f 73 68 69 66 74 28 79 79 70 50 61 72 73  yy_shift(yypPars
2e6b0 65 72 2c 79 79 61 63 74 2c 59 59 45 52 52 4f 52  er,yyact,YYERROR
2e6b1 53 59 4d 42 4f 4c 2c 26 75 32 29 3b 0a 20 20 20  SYMBOL,&u2);.   
2e6b2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2e6b3 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e       yypParser->
2e6b4 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20  yyerrcnt = 3;.  
2e6b5 20 20 20 20 79 79 65 72 72 6f 72 68 69 74 20 3d      yyerrorhit =
2e6b6 20 31 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 59 59   1;.#else  /* YY
2e6b7 45 52 52 4f 52 53 59 4d 42 4f 4c 20 69 73 20 6e  ERRORSYMBOL is n
2e6b8 6f 74 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20 20  ot defined */.  
2e6b9 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 77      /* This is w
2e6ba 68 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65  hat we do if the
2e6bb 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 6e 6f   grammar does no
2e6bc 74 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a  t define ERROR:.
2e6bd 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2e6be 2a 20 20 2a 20 52 65 70 6f 72 74 20 61 6e 20 65  *  * Report an e
2e6bf 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61 6e  rror message, an
2e6c0 64 20 74 68 72 6f 77 20 61 77 61 79 20 74 68 65  d throw away the
2e6c1 20 69 6e 70 75 74 20 74 6f 6b 65 6e 2e 0a 20 20   input token..  
2e6c2 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
2e6c3 20 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20   * If the input 
2e6c4 74 6f 6b 65 6e 20 69 73 20 24 2c 20 74 68 65 6e  token is $, then
2e6c5 20 66 61 69 6c 20 74 68 65 20 70 61 72 73 65 2e   fail the parse.
2e6c6 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
2e6c7 2a 2a 20 41 73 20 62 65 66 6f 72 65 2c 20 73 75  ** As before, su
2e6c8 62 73 65 71 75 65 6e 74 20 65 72 72 6f 72 20 6d  bsequent error m
2e6c9 65 73 73 61 67 65 73 20 61 72 65 20 73 75 70 70  essages are supp
2e6ca 72 65 73 73 65 64 20 75 6e 74 69 6c 0a 20 20 20  ressed until.   
2e6cb 20 20 20 2a 2a 20 74 68 72 65 65 20 69 6e 70 75     ** three inpu
2e6cc 74 20 74 6f 6b 65 6e 73 20 68 61 76 65 20 62 65  t tokens have be
2e6cd 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  en successfully 
2e6ce 73 68 69 66 74 65 64 2e 0a 20 20 20 20 20 20 2a  shifted..      *
2e6cf 2f 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50  /.      if( yypP
2e6d0 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c  arser->yyerrcnt<
2e6d1 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79  =0 ){.        yy
2e6d2 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79  _syntax_error(yy
2e6d3 70 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c  pParser,yymajor,
2e6d4 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20  yyminorunion);. 
2e6d5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 70       }.      yyp
2e6d6 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74  Parser->yyerrcnt
2e6d7 20 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 5f 64   = 3;.      yy_d
2e6d8 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72  estructor(yypPar
2e6d9 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29  ser,(YYCODETYPE)
2e6da 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
2e6db 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 69 66  union);.      if
2e6dc 28 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29  ( yyendofinput )
2e6dd 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 70 61 72  {.        yy_par
2e6de 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72  se_failed(yypPar
2e6df 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ser);.      }.  
2e6e0 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
2e6e1 4e 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66 0a 20  NOCODE;.#endif. 
2e6e2 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 79     }.  }while( y
2e6e3 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44 45  ymajor!=YYNOCODE
2e6e4 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
2e6e5 79 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72 65 74  yidx>=0 );.  ret
2e6e6 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  urn;.}../*******
2e6e7 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
2e6e8 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  arse.c *********
2e6e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e6ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e6eb 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2e6ec 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2e6ed 6c 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  le tokenize.c **
2e6ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e6ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e6f0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2e6f1 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
2e6f2 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2e6f3 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2e6f4 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2e6f5 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2e6f6 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2e6f7 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2e6f8 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2e6f9 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2e6fa 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2e6fb 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2e6fc 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2e6fd 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2e6fe 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2e6ff 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2e700 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2e701 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2e702 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2e703 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2e704 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e705 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e706 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e707 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2e708 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66  * An tokenizer f
2e709 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  or SQL.**.** Thi
2e70a 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2e70b 43 20 63 6f 64 65 20 74 68 61 74 20 73 70 6c 69  C code that spli
2e70c 74 73 20 61 6e 20 53 51 4c 20 69 6e 70 75 74 20  ts an SQL input 
2e70d 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 0a 2a  string up into.*
2e70e 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 6f 6b  * individual tok
2e70f 65 6e 73 20 61 6e 64 20 73 65 6e 64 73 20 74 68  ens and sends th
2e710 6f 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62  ose tokens one-b
2e711 79 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20 74 68  y-one over to th
2e712 65 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f 72 20  e.** parser for 
2e713 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20  analysis..**.** 
2e714 24 49 64 3a 20 74 6f 6b 65 6e 69 7a 65 2e 63 2c  $Id: tokenize.c,
2e715 76 20 31 2e 31 35 35 20 32 30 30 39 2f 30 33 2f  v 1.155 2009/03/
2e716 33 31 20 30 33 3a 34 31 3a 35 37 20 73 68 61 6e  31 03:41:57 shan
2e717 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  e Exp $.*/../*.*
2e718 2a 20 54 68 65 20 63 68 61 72 4d 61 70 28 29 20  * The charMap() 
2e719 6d 61 63 72 6f 20 6d 61 70 73 20 61 6c 70 68 61  macro maps alpha
2e71a 62 65 74 69 63 20 63 68 61 72 61 63 74 65 72 73  betic characters
2e71b 20 69 6e 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c   into their.** l
2e71c 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20  ower-case ASCII 
2e71d 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20  equivalent.  On 
2e71e 41 53 43 49 49 20 6d 61 63 68 69 6e 65 73 2c 20  ASCII machines, 
2e71f 74 68 69 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20  this is just.** 
2e720 61 6e 20 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65  an upper-to-lowe
2e721 72 20 63 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20  r case map.  On 
2e722 45 42 43 44 49 43 20 6d 61 63 68 69 6e 65 73 20  EBCDIC machines 
2e723 77 65 20 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20  we also need.** 
2e724 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 65 6e  to adjust the en
2e725 63 6f 64 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c  coding.  Only al
2e726 70 68 61 62 65 74 69 63 20 63 68 61 72 61 63 74  phabetic charact
2e727 65 72 73 20 61 6e 64 20 75 6e 64 65 72 73 63 6f  ers and undersco
2e728 72 65 73 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62  res.** need to b
2e729 65 20 74 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f  e translated..*/
2e72a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41  .#ifdef SQLITE_A
2e72b 53 43 49 49 0a 23 20 64 65 66 69 6e 65 20 63 68  SCII.# define ch
2e72c 61 72 4d 61 70 28 58 29 20 73 71 6c 69 74 65 33  arMap(X) sqlite3
2e72d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e  UpperToLower[(un
2e72e 73 69 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 23  signed char)X].#
2e72f 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
2e730 49 54 45 5f 45 42 43 44 49 43 0a 23 20 64 65 66  ITE_EBCDIC.# def
2e731 69 6e 65 20 63 68 61 72 4d 61 70 28 58 29 20 65  ine charMap(X) e
2e732 62 63 64 69 63 54 6f 41 73 63 69 69 5b 28 75 6e  bcdicToAscii[(un
2e733 73 69 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 63  signed char)X].c
2e734 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2e735 61 72 20 65 62 63 64 69 63 54 6f 41 73 63 69 69  ar ebcdicToAscii
2e736 5b 5d 20 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20  [] = {./* 0   1 
2e737 20 20 32 20 20 20 33 20 20 20 34 20 20 20 35 20    2   3   4   5 
2e738 20 20 36 20 20 20 37 20 20 20 38 20 20 20 39 20    6   7   8   9 
2e739 20 20 41 20 20 20 42 20 20 20 43 20 20 20 44 20    A   B   C   D 
2e73a 20 20 45 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c    E   F */.   0,
2e73b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e73c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e73d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e73e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
2e73f 20 30 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   0x */.   0,  0,
2e740 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e741 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e742 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e743 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20    0,  0,  /* 1x 
2e744 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c  */.   0,  0,  0,
2e745 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e746 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e747 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e748 20 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20    0,  /* 2x */. 
2e749 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e74a 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e74b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e74c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e74d 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c    /* 3x */.   0,
2e74e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e74f 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e750 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e751 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
2e752 20 34 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c   4x */.   0,  0,
2e753 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e754 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e755 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e756 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20    0,  0,  /* 5x 
2e757 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c  */.   0,  0,  0,
2e758 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e759 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e75a 20 20 30 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c    0,  0, 95,  0,
2e75b 20 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20    0,  /* 6x */. 
2e75c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e75d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e75e 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e75f 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e760 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c    /* 7x */.   0,
2e761 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c   97, 98, 99,100,
2e762 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c  101,102,103,104,
2e763 31 30 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c  105,  0,  0,  0,
2e764 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
2e765 20 38 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c   8x */.   0,106,
2e766 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c  107,108,109,110,
2e767 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c  111,112,113,114,
2e768 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e769 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20    0,  0,  /* 9x 
2e76a 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c  */.   0,  0,115,
2e76b 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c  116,117,118,119,
2e76c 31 32 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c  120,121,122,  0,
2e76d 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e76e 20 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20    0,  /* Ax */. 
2e76f 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e770 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e771 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e772 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e773 20 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c    /* Bx */.   0,
2e774 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c   97, 98, 99,100,
2e775 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c  101,102,103,104,
2e776 31 30 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c  105,  0,  0,  0,
2e777 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a    0,  0,  0,  /*
2e778 20 43 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c   Cx */.   0,106,
2e779 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c  107,108,109,110,
2e77a 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c  111,112,113,114,
2e77b 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e77c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20    0,  0,  /* Dx 
2e77d 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c  */.   0,  0,115,
2e77e 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c  116,117,118,119,
2e77f 31 32 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c  120,121,122,  0,
2e780 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e781 20 20 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20    0,  /* Ex */. 
2e782 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e783 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e784 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e785 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c    0,  0,  0,  0,
2e786 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65    /* Fx */.};.#e
2e787 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
2e788 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f  sqlite3KeywordCo
2e789 64 65 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b  de function look
2e78a 73 20 75 70 20 61 6e 20 69 64 65 6e 74 69 66 69  s up an identifi
2e78b 65 72 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  er to determine 
2e78c 69 66 0a 2a 2a 20 69 74 20 69 73 20 61 20 6b 65  if.** it is a ke
2e78d 79 77 6f 72 64 2e 20 20 49 66 20 69 74 20 69 73  yword.  If it is
2e78e 20 61 20 6b 65 79 77 6f 72 64 2c 20 74 68 65 20   a keyword, the 
2e78f 74 6f 6b 65 6e 20 63 6f 64 65 20 6f 66 20 74 68  token code of th
2e790 61 74 20 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a  at keyword is .*
2e791 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20  * returned.  If 
2e792 74 68 65 20 69 6e 70 75 74 20 69 73 20 6e 6f 74  the input is not
2e793 20 61 20 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49   a keyword, TK_I
2e794 44 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  D is returned..*
2e795 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65  *.** The impleme
2e796 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ntation of this 
2e797 72 6f 75 74 69 6e 65 20 77 61 73 20 67 65 6e 65  routine was gene
2e798 72 61 74 65 64 20 62 79 20 61 20 70 72 6f 67 72  rated by a progr
2e799 61 6d 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64  am,.** mkkeyword
2e79a 68 61 73 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20  hash.h, located 
2e79b 69 6e 20 74 68 65 20 74 6f 6f 6c 20 73 75 62 64  in the tool subd
2e79c 69 72 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20  irectory of the 
2e79d 64 69 73 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a  distribution..**
2e79e 20 54 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74   The output of t
2e79f 68 65 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68  he mkkeywordhash
2e7a0 2e 63 20 70 72 6f 67 72 61 6d 20 69 73 20 77 72  .c program is wr
2e7a1 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c  itten into a fil
2e7a2 65 0a 2a 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f  e.** named keywo
2e7a3 72 64 68 61 73 68 2e 68 20 61 6e 64 20 74 68 65  rdhash.h and the
2e7a4 6e 20 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20  n included into 
2e7a5 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65  this source file
2e7a6 20 62 79 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c   by.** the #incl
2e7a7 75 64 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a  ude below..*/./*
2e7a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
2e7a9 63 6c 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73  clude keywordhas
2e7aa 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  h.h in the middl
2e7ab 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  e of tokenize.c 
2e7ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2e7ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2e7ae 67 69 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64  gin file keyword
2e7af 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  hash.h *********
2e7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e7b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2e7b2 2a 2a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  **** This file c
2e7b3 6f 6e 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69  ontains automati
2e7b4 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20  cally generated 
2e7b5 63 6f 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  code ******.**.*
2e7b6 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
2e7b7 69 73 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e  is file has been
2e7b8 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67   automatically g
2e7b9 65 6e 65 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a  enerated by.**.*
2e7ba 2a 20 20 20 20 20 24 48 65 61 64 65 72 3a 20 2f  *     $Header: /
2e7bb 73 71 6c 69 74 65 2f 73 71 6c 69 74 65 2f 74 6f  sqlite/sqlite/to
2e7bc 6f 6c 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68  ol/mkkeywordhash
2e7bd 2e 63 2c 76 20 31 2e 33 37 20 32 30 30 39 2f 30  .c,v 1.37 2009/0
2e7be 32 2f 30 31 20 30 30 3a 30 30 3a 34 36 20 64 72  2/01 00:00:46 dr
2e7bf 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68  h Exp $.**.** Th
2e7c0 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
2e7c1 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
2e7c2 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64   function that d
2e7c3 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
2e7c4 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20 61 20 67 69  r.** or not a gi
2e7c5 76 65 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69  ven identifier i
2e7c6 73 20 72 65 61 6c 6c 79 20 61 6e 20 53 51 4c 20  s really an SQL 
2e7c7 6b 65 79 77 6f 72 64 2e 20 20 54 68 65 20 73 61  keyword.  The sa
2e7c8 6d 65 20 74 68 69 6e 67 0a 2a 2a 20 6d 69 67 68  me thing.** migh
2e7c9 74 20 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64  t be implemented
2e7ca 20 6d 6f 72 65 20 64 69 72 65 63 74 6c 79 20 75   more directly u
2e7cb 73 69 6e 67 20 61 20 68 61 6e 64 2d 77 72 69 74  sing a hand-writ
2e7cc 74 65 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ten hash table..
2e7cd 2a 2a 20 42 75 74 20 62 79 20 75 73 69 6e 67 20  ** But by using 
2e7ce 74 68 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  this automatical
2e7cf 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  ly generated cod
2e7d0 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  e, the size of t
2e7d1 68 65 20 63 6f 64 65 0a 2a 2a 20 69 73 20 73 75  he code.** is su
2e7d2 62 73 74 61 6e 74 69 61 6c 6c 79 20 72 65 64 75  bstantially redu
2e7d3 63 65 64 2e 20 20 54 68 69 73 20 69 73 20 69 6d  ced.  This is im
2e7d4 70 6f 72 74 61 6e 74 20 66 6f 72 20 65 6d 62 65  portant for embe
2e7d5 64 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  dded application
2e7d6 73 0a 2a 2a 20 6f 6e 20 70 6c 61 74 66 6f 72 6d  s.** on platform
2e7d7 73 20 77 69 74 68 20 6c 69 6d 69 74 65 64 20 6d  s with limited m
2e7d8 65 6d 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20 48 61 73  emory..*/./* Has
2e7d9 68 20 73 63 6f 72 65 3a 20 31 37 31 20 2a 2f 0a  h score: 171 */.
2e7da 73 74 61 74 69 63 20 69 6e 74 20 6b 65 79 77 6f  static int keywo
2e7db 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 63 68 61  rdCode(const cha
2e7dc 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *z, int n){.  
2e7dd 2f 2a 20 7a 54 65 78 74 5b 5d 20 65 6e 63 6f 64  /* zText[] encod
2e7de 65 73 20 38 30 31 20 62 79 74 65 73 20 6f 66 20  es 801 bytes of 
2e7df 6b 65 79 77 6f 72 64 73 20 69 6e 20 35 34 31 20  keywords in 541 
2e7e0 62 79 74 65 73 20 2a 2f 0a 20 20 2f 2a 20 20 20  bytes */.  /*   
2e7e1 52 45 49 4e 44 45 58 45 44 45 53 43 41 50 45 41  REINDEXEDESCAPEA
2e7e2 43 48 45 43 4b 45 59 42 45 46 4f 52 45 49 47 4e  CHECKEYBEFOREIGN
2e7e3 4f 52 45 47 45 58 50 4c 41 49 4e 53 54 45 41 44  OREGEXPLAINSTEAD
2e7e4 44 41 54 41 42 41 53 45 4c 45 43 54 20 20 20 20  DATABASELECT    
2e7e5 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 41 42 4c     */.  /*   ABL
2e7e6 45 46 54 48 45 4e 44 45 46 45 52 52 41 42 4c 45  EFTHENDEFERRABLE
2e7e7 4c 53 45 58 43 45 50 54 52 41 4e 53 41 43 54 49  LSEXCEPTRANSACTI
2e7e8 4f 4e 41 54 55 52 41 4c 54 45 52 41 49 53 45 58  ONATURALTERAISEX
2e7e9 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 20  CLUSIVE         
2e7ea 2a 2f 0a 20 20 2f 2a 20 20 20 58 49 53 54 53 41  */.  /*   XISTSA
2e7eb 56 45 50 4f 49 4e 54 45 52 53 45 43 54 52 49 47  VEPOINTERSECTRIG
2e7ec 47 45 52 45 46 45 52 45 4e 43 45 53 43 4f 4e 53  GEREFERENCESCONS
2e7ed 54 52 41 49 4e 54 4f 46 46 53 45 54 45 4d 50 4f  TRAINTOFFSETEMPO
2e7ee 52 41 52 59 20 20 20 20 20 20 20 20 20 2a 2f 0a  RARY         */.
2e7ef 20 20 2f 2a 20 20 20 55 4e 49 51 55 45 52 59 41    /*   UNIQUERYA
2e7f0 54 54 41 43 48 41 56 49 4e 47 52 4f 55 50 44 41  TTACHAVINGROUPDA
2e7f1 54 45 42 45 47 49 4e 4e 45 52 45 4c 45 41 53 45  TEBEGINNERELEASE
2e7f2 42 45 54 57 45 45 4e 4f 54 4e 55 4c 4c 49 4b 45  BETWEENOTNULLIKE
2e7f3 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f            */.  /
2e7f4 2a 20 20 20 43 41 53 43 41 44 45 4c 45 54 45 43  *   CASCADELETEC
2e7f5 41 53 45 43 4f 4c 4c 41 54 45 43 52 45 41 54 45  ASECOLLATECREATE
2e7f6 43 55 52 52 45 4e 54 5f 44 41 54 45 44 45 54 41  CURRENT_DATEDETA
2e7f7 43 48 49 4d 4d 45 44 49 41 54 45 4a 4f 49 4e 20  CHIMMEDIATEJOIN 
2e7f8 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20         */.  /*  
2e7f9 20 53 45 52 54 4d 41 54 43 48 50 4c 41 4e 41 4c   SERTMATCHPLANAL
2e7fa 59 5a 45 50 52 41 47 4d 41 42 4f 52 54 56 41 4c  YZEPRAGMABORTVAL
2e7fb 55 45 53 56 49 52 54 55 41 4c 49 4d 49 54 57 48  UESVIRTUALIMITWH
2e7fc 45 4e 57 48 45 52 45 4e 41 4d 45 20 20 20 20 20  ENWHERENAME     
2e7fd 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 41 46      */.  /*   AF
2e7fe 54 45 52 45 50 4c 41 43 45 41 4e 44 45 46 41 55  TEREPLACEANDEFAU
2e7ff 4c 54 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 43  LTAUTOINCREMENTC
2e800 41 53 54 43 4f 4c 55 4d 4e 43 4f 4d 4d 49 54 43  ASTCOLUMNCOMMITC
2e801 4f 4e 46 4c 49 43 54 43 52 4f 53 53 20 20 20 20  ONFLICTCROSS    
2e802 20 2a 2f 0a 20 20 2f 2a 20 20 20 43 55 52 52 45   */.  /*   CURRE
2e803 4e 54 5f 54 49 4d 45 53 54 41 4d 50 52 49 4d 41  NT_TIMESTAMPRIMA
2e804 52 59 44 45 46 45 52 52 45 44 49 53 54 49 4e 43  RYDEFERREDISTINC
2e805 54 44 52 4f 50 46 41 49 4c 46 52 4f 4d 46 55 4c  TDROPFAILFROMFUL
2e806 4c 47 4c 4f 42 59 49 46 20 20 20 20 20 20 2a 2f  LGLOBYIF      */
2e807 0a 20 20 2f 2a 20 20 20 49 53 4e 55 4c 4c 4f 52  .  /*   ISNULLOR
2e808 44 45 52 45 53 54 52 49 43 54 4f 55 54 45 52 49  DERESTRICTOUTERI
2e809 47 48 54 52 4f 4c 4c 42 41 43 4b 52 4f 57 55 4e  GHTROLLBACKROWUN
2e80a 49 4f 4e 55 53 49 4e 47 56 41 43 55 55 4d 56 49  IONUSINGVACUUMVI
2e80b 45 57 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  EW         */.  
2e80c 2f 2a 20 20 20 49 4e 49 54 49 41 4c 4c 59 20 20  /*   INITIALLY  
2e80d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e80e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e80f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e810 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 73 74 61          */.  sta
2e811 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
2e812 54 65 78 74 5b 35 34 30 5d 20 3d 20 7b 0a 20 20  Text[540] = {.  
2e813 20 20 27 52 27 2c 27 45 27 2c 27 49 27 2c 27 4e    'R','E','I','N
2e814 27 2c 27 44 27 2c 27 45 27 2c 27 58 27 2c 27 45  ','D','E','X','E
2e815 27 2c 27 44 27 2c 27 45 27 2c 27 53 27 2c 27 43  ','D','E','S','C
2e816 27 2c 27 41 27 2c 27 50 27 2c 27 45 27 2c 27 41  ','A','P','E','A
2e817 27 2c 27 43 27 2c 27 48 27 2c 0a 20 20 20 20 27  ','C','H',.    '
2e818 45 27 2c 27 43 27 2c 27 4b 27 2c 27 45 27 2c 27  E','C','K','E','
2e819 59 27 2c 27 42 27 2c 27 45 27 2c 27 46 27 2c 27  Y','B','E','F','
2e81a 4f 27 2c 27 52 27 2c 27 45 27 2c 27 49 27 2c 27  O','R','E','I','
2e81b 47 27 2c 27 4e 27 2c 27 4f 27 2c 27 52 27 2c 27  G','N','O','R','
2e81c 45 27 2c 27 47 27 2c 0a 20 20 20 20 27 45 27 2c  E','G',.    'E',
2e81d 27 58 27 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c  'X','P','L','A',
2e81e 27 49 27 2c 27 4e 27 2c 27 53 27 2c 27 54 27 2c  'I','N','S','T',
2e81f 27 45 27 2c 27 41 27 2c 27 44 27 2c 27 44 27 2c  'E','A','D','D',
2e820 27 41 27 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c  'A','T','A','B',
2e821 27 41 27 2c 0a 20 20 20 20 27 53 27 2c 27 45 27  'A',.    'S','E'
2e822 2c 27 4c 27 2c 27 45 27 2c 27 43 27 2c 27 54 27  ,'L','E','C','T'
2e823 2c 27 41 27 2c 27 42 27 2c 27 4c 27 2c 27 45 27  ,'A','B','L','E'
2e824 2c 27 46 27 2c 27 54 27 2c 27 48 27 2c 27 45 27  ,'F','T','H','E'
2e825 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 46 27  ,'N','D','E','F'
2e826 2c 0a 20 20 20 20 27 45 27 2c 27 52 27 2c 27 52  ,.    'E','R','R
2e827 27 2c 27 41 27 2c 27 42 27 2c 27 4c 27 2c 27 45  ','A','B','L','E
2e828 27 2c 27 4c 27 2c 27 53 27 2c 27 45 27 2c 27 58  ','L','S','E','X
2e829 27 2c 27 43 27 2c 27 45 27 2c 27 50 27 2c 27 54  ','C','E','P','T
2e82a 27 2c 27 52 27 2c 27 41 27 2c 27 4e 27 2c 0a 20  ','R','A','N',. 
2e82b 20 20 20 27 53 27 2c 27 41 27 2c 27 43 27 2c 27     'S','A','C','
2e82c 54 27 2c 27 49 27 2c 27 4f 27 2c 27 4e 27 2c 27  T','I','O','N','
2e82d 41 27 2c 27 54 27 2c 27 55 27 2c 27 52 27 2c 27  A','T','U','R','
2e82e 41 27 2c 27 4c 27 2c 27 54 27 2c 27 45 27 2c 27  A','L','T','E','
2e82f 52 27 2c 27 41 27 2c 27 49 27 2c 0a 20 20 20 20  R','A','I',.    
2e830 27 53 27 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c  'S','E','X','C',
2e831 27 4c 27 2c 27 55 27 2c 27 53 27 2c 27 49 27 2c  'L','U','S','I',
2e832 27 56 27 2c 27 45 27 2c 27 58 27 2c 27 49 27 2c  'V','E','X','I',
2e833 27 53 27 2c 27 54 27 2c 27 53 27 2c 27 41 27 2c  'S','T','S','A',
2e834 27 56 27 2c 27 45 27 2c 0a 20 20 20 20 27 50 27  'V','E',.    'P'
2e835 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 54 27  ,'O','I','N','T'
2e836 2c 27 45 27 2c 27 52 27 2c 27 53 27 2c 27 45 27  ,'E','R','S','E'
2e837 2c 27 43 27 2c 27 54 27 2c 27 52 27 2c 27 49 27  ,'C','T','R','I'
2e838 2c 27 47 27 2c 27 47 27 2c 27 45 27 2c 27 52 27  ,'G','G','E','R'
2e839 2c 27 45 27 2c 0a 20 20 20 20 27 46 27 2c 27 45  ,'E',.    'F','E
2e83a 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 43  ','R','E','N','C
2e83b 27 2c 27 45 27 2c 27 53 27 2c 27 43 27 2c 27 4f  ','E','S','C','O
2e83c 27 2c 27 4e 27 2c 27 53 27 2c 27 54 27 2c 27 52  ','N','S','T','R
2e83d 27 2c 27 41 27 2c 27 49 27 2c 27 4e 27 2c 27 54  ','A','I','N','T
2e83e 27 2c 0a 20 20 20 20 27 4f 27 2c 27 46 27 2c 27  ',.    'O','F','
2e83f 46 27 2c 27 53 27 2c 27 45 27 2c 27 54 27 2c 27  F','S','E','T','
2e840 45 27 2c 27 4d 27 2c 27 50 27 2c 27 4f 27 2c 27  E','M','P','O','
2e841 52 27 2c 27 41 27 2c 27 52 27 2c 27 59 27 2c 27  R','A','R','Y','
2e842 55 27 2c 27 4e 27 2c 27 49 27 2c 27 51 27 2c 0a  U','N','I','Q',.
2e843 20 20 20 20 27 55 27 2c 27 45 27 2c 27 52 27 2c      'U','E','R',
2e844 27 59 27 2c 27 41 27 2c 27 54 27 2c 27 54 27 2c  'Y','A','T','T',
2e845 27 41 27 2c 27 43 27 2c 27 48 27 2c 27 41 27 2c  'A','C','H','A',
2e846 27 56 27 2c 27 49 27 2c 27 4e 27 2c 27 47 27 2c  'V','I','N','G',
2e847 27 52 27 2c 27 4f 27 2c 27 55 27 2c 0a 20 20 20  'R','O','U',.   
2e848 20 27 50 27 2c 27 44 27 2c 27 41 27 2c 27 54 27   'P','D','A','T'
2e849 2c 27 45 27 2c 27 42 27 2c 27 45 27 2c 27 47 27  ,'E','B','E','G'
2e84a 2c 27 49 27 2c 27 4e 27 2c 27 4e 27 2c 27 45 27  ,'I','N','N','E'
2e84b 2c 27 52 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27  ,'R','E','L','E'
2e84c 2c 27 41 27 2c 27 53 27 2c 0a 20 20 20 20 27 45  ,'A','S',.    'E
2e84d 27 2c 27 42 27 2c 27 45 27 2c 27 54 27 2c 27 57  ','B','E','T','W
2e84e 27 2c 27 45 27 2c 27 45 27 2c 27 4e 27 2c 27 4f  ','E','E','N','O
2e84f 27 2c 27 54 27 2c 27 4e 27 2c 27 55 27 2c 27 4c  ','T','N','U','L
2e850 27 2c 27 4c 27 2c 27 49 27 2c 27 4b 27 2c 27 45  ','L','I','K','E
2e851 27 2c 27 43 27 2c 0a 20 20 20 20 27 41 27 2c 27  ','C',.    'A','
2e852 53 27 2c 27 43 27 2c 27 41 27 2c 27 44 27 2c 27  S','C','A','D','
2e853 45 27 2c 27 4c 27 2c 27 45 27 2c 27 54 27 2c 27  E','L','E','T','
2e854 45 27 2c 27 43 27 2c 27 41 27 2c 27 53 27 2c 27  E','C','A','S','
2e855 45 27 2c 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27  E','C','O','L','
2e856 4c 27 2c 0a 20 20 20 20 27 41 27 2c 27 54 27 2c  L',.    'A','T',
2e857 27 45 27 2c 27 43 27 2c 27 52 27 2c 27 45 27 2c  'E','C','R','E',
2e858 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c  'A','T','E','C',
2e859 27 55 27 2c 27 52 27 2c 27 52 27 2c 27 45 27 2c  'U','R','R','E',
2e85a 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27 44 27 2c  'N','T','_','D',
2e85b 0a 20 20 20 20 27 41 27 2c 27 54 27 2c 27 45 27  .    'A','T','E'
2e85c 2c 27 44 27 2c 27 45 27 2c 27 54 27 2c 27 41 27  ,'D','E','T','A'
2e85d 2c 27 43 27 2c 27 48 27 2c 27 49 27 2c 27 4d 27  ,'C','H','I','M'
2e85e 2c 27 4d 27 2c 27 45 27 2c 27 44 27 2c 27 49 27  ,'M','E','D','I'
2e85f 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c 0a 20 20  ,'A','T','E',.  
2e860 20 20 27 4a 27 2c 27 4f 27 2c 27 49 27 2c 27 4e    'J','O','I','N
2e861 27 2c 27 53 27 2c 27 45 27 2c 27 52 27 2c 27 54  ','S','E','R','T
2e862 27 2c 27 4d 27 2c 27 41 27 2c 27 54 27 2c 27 43  ','M','A','T','C
2e863 27 2c 27 48 27 2c 27 50 27 2c 27 4c 27 2c 27 41  ','H','P','L','A
2e864 27 2c 27 4e 27 2c 27 41 27 2c 0a 20 20 20 20 27  ','N','A',.    '
2e865 4c 27 2c 27 59 27 2c 27 5a 27 2c 27 45 27 2c 27  L','Y','Z','E','
2e866 50 27 2c 27 52 27 2c 27 41 27 2c 27 47 27 2c 27  P','R','A','G','
2e867 4d 27 2c 27 41 27 2c 27 42 27 2c 27 4f 27 2c 27  M','A','B','O','
2e868 52 27 2c 27 54 27 2c 27 56 27 2c 27 41 27 2c 27  R','T','V','A','
2e869 4c 27 2c 27 55 27 2c 0a 20 20 20 20 27 45 27 2c  L','U',.    'E',
2e86a 27 53 27 2c 27 56 27 2c 27 49 27 2c 27 52 27 2c  'S','V','I','R',
2e86b 27 54 27 2c 27 55 27 2c 27 41 27 2c 27 4c 27 2c  'T','U','A','L',
2e86c 27 49 27 2c 27 4d 27 2c 27 49 27 2c 27 54 27 2c  'I','M','I','T',
2e86d 27 57 27 2c 27 48 27 2c 27 45 27 2c 27 4e 27 2c  'W','H','E','N',
2e86e 27 57 27 2c 0a 20 20 20 20 27 48 27 2c 27 45 27  'W',.    'H','E'
2e86f 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 41 27  ,'R','E','N','A'
2e870 2c 27 4d 27 2c 27 45 27 2c 27 41 27 2c 27 46 27  ,'M','E','A','F'
2e871 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 45 27  ,'T','E','R','E'
2e872 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c 27 43 27  ,'P','L','A','C'
2e873 2c 0a 20 20 20 20 27 45 27 2c 27 41 27 2c 27 4e  ,.    'E','A','N
2e874 27 2c 27 44 27 2c 27 45 27 2c 27 46 27 2c 27 41  ','D','E','F','A
2e875 27 2c 27 55 27 2c 27 4c 27 2c 27 54 27 2c 27 41  ','U','L','T','A
2e876 27 2c 27 55 27 2c 27 54 27 2c 27 4f 27 2c 27 49  ','U','T','O','I
2e877 27 2c 27 4e 27 2c 27 43 27 2c 27 52 27 2c 0a 20  ','N','C','R',. 
2e878 20 20 20 27 45 27 2c 27 4d 27 2c 27 45 27 2c 27     'E','M','E','
2e879 4e 27 2c 27 54 27 2c 27 43 27 2c 27 41 27 2c 27  N','T','C','A','
2e87a 53 27 2c 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27  S','T','C','O','
2e87b 4c 27 2c 27 55 27 2c 27 4d 27 2c 27 4e 27 2c 27  L','U','M','N','
2e87c 43 27 2c 27 4f 27 2c 27 4d 27 2c 0a 20 20 20 20  C','O','M',.    
2e87d 27 4d 27 2c 27 49 27 2c 27 54 27 2c 27 43 27 2c  'M','I','T','C',
2e87e 27 4f 27 2c 27 4e 27 2c 27 46 27 2c 27 4c 27 2c  'O','N','F','L',
2e87f 27 49 27 2c 27 43 27 2c 27 54 27 2c 27 43 27 2c  'I','C','T','C',
2e880 27 52 27 2c 27 4f 27 2c 27 53 27 2c 27 53 27 2c  'R','O','S','S',
2e881 27 43 27 2c 27 55 27 2c 0a 20 20 20 20 27 52 27  'C','U',.    'R'
2e882 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27  ,'R','E','N','T'
2e883 2c 27 5f 27 2c 27 54 27 2c 27 49 27 2c 27 4d 27  ,'_','T','I','M'
2e884 2c 27 45 27 2c 27 53 27 2c 27 54 27 2c 27 41 27  ,'E','S','T','A'
2e885 2c 27 4d 27 2c 27 50 27 2c 27 52 27 2c 27 49 27  ,'M','P','R','I'
2e886 2c 27 4d 27 2c 0a 20 20 20 20 27 41 27 2c 27 52  ,'M',.    'A','R
2e887 27 2c 27 59 27 2c 27 44 27 2c 27 45 27 2c 27 46  ','Y','D','E','F
2e888 27 2c 27 45 27 2c 27 52 27 2c 27 52 27 2c 27 45  ','E','R','R','E
2e889 27 2c 27 44 27 2c 27 49 27 2c 27 53 27 2c 27 54  ','D','I','S','T
2e88a 27 2c 27 49 27 2c 27 4e 27 2c 27 43 27 2c 27 54  ','I','N','C','T
2e88b 27 2c 0a 20 20 20 20 27 44 27 2c 27 52 27 2c 27  ',.    'D','R','
2e88c 4f 27 2c 27 50 27 2c 27 46 27 2c 27 41 27 2c 27  O','P','F','A','
2e88d 49 27 2c 27 4c 27 2c 27 46 27 2c 27 52 27 2c 27  I','L','F','R','
2e88e 4f 27 2c 27 4d 27 2c 27 46 27 2c 27 55 27 2c 27  O','M','F','U','
2e88f 4c 27 2c 27 4c 27 2c 27 47 27 2c 27 4c 27 2c 0a  L','L','G','L',.
2e890 20 20 20 20 27 4f 27 2c 27 42 27 2c 27 59 27 2c      'O','B','Y',
2e891 27 49 27 2c 27 46 27 2c 27 49 27 2c 27 53 27 2c  'I','F','I','S',
2e892 27 4e 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c  'N','U','L','L',
2e893 27 4f 27 2c 27 52 27 2c 27 44 27 2c 27 45 27 2c  'O','R','D','E',
2e894 27 52 27 2c 27 45 27 2c 27 53 27 2c 0a 20 20 20  'R','E','S',.   
2e895 20 27 54 27 2c 27 52 27 2c 27 49 27 2c 27 43 27   'T','R','I','C'
2e896 2c 27 54 27 2c 27 4f 27 2c 27 55 27 2c 27 54 27  ,'T','O','U','T'
2e897 2c 27 45 27 2c 27 52 27 2c 27 49 27 2c 27 47 27  ,'E','R','I','G'
2e898 2c 27 48 27 2c 27 54 27 2c 27 52 27 2c 27 4f 27  ,'H','T','R','O'
2e899 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20 20 20 27 42  ,'L','L',.    'B
2e89a 27 2c 27 41 27 2c 27 43 27 2c 27 4b 27 2c 27 52  ','A','C','K','R
2e89b 27 2c 27 4f 27 2c 27 57 27 2c 27 55 27 2c 27 4e  ','O','W','U','N
2e89c 27 2c 27 49 27 2c 27 4f 27 2c 27 4e 27 2c 27 55  ','I','O','N','U
2e89d 27 2c 27 53 27 2c 27 49 27 2c 27 4e 27 2c 27 47  ','S','I','N','G
2e89e 27 2c 27 56 27 2c 0a 20 20 20 20 27 41 27 2c 27  ','V',.    'A','
2e89f 43 27 2c 27 55 27 2c 27 55 27 2c 27 4d 27 2c 27  C','U','U','M','
2e8a0 56 27 2c 27 49 27 2c 27 45 27 2c 27 57 27 2c 27  V','I','E','W','
2e8a1 49 27 2c 27 4e 27 2c 27 49 27 2c 27 54 27 2c 27  I','N','I','T','
2e8a2 49 27 2c 27 41 27 2c 27 4c 27 2c 27 4c 27 2c 27  I','A','L','L','
2e8a3 59 27 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69  Y',.  };.  stati
2e8a4 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
2e8a5 20 63 68 61 72 20 61 48 61 73 68 5b 31 32 37 5d   char aHash[127]
2e8a6 20 3d 20 7b 0a 20 20 20 20 20 20 37 30 2c 20 20   = {.      70,  
2e8a7 39 39 2c 20 31 31 32 2c 20 20 36 38 2c 20 20 20  99, 112,  68,   
2e8a8 30 2c 20 20 34 33 2c 20 20 20 30 2c 20 20 20 30  0,  43,   0,   0
2e8a9 2c 20 20 37 36 2c 20 20 20 30 2c 20 20 37 31 2c  ,  76,   0,  71,
2e8aa 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
2e8ab 20 34 31 2c 20 20 31 32 2c 20 20 37 32 2c 20 20   41,  12,  72,  
2e8ac 31 35 2c 20 20 20 30 2c 20 31 31 31 2c 20 20 37  15,   0, 111,  7
2e8ad 39 2c 20 20 34 39 2c 20 31 30 36 2c 20 20 20 30  9,  49, 106,   0
2e8ae 2c 20 20 31 39 2c 20 20 20 30 2c 20 20 20 30 2c  ,  19,   0,   0,
2e8af 0a 20 20 20 20 20 31 31 36 2c 20 20 20 30 2c 20  .     116,   0, 
2e8b0 31 31 34 2c 20 31 30 39 2c 20 20 20 30 2c 20 20  114, 109,   0,  
2e8b1 32 32 2c 20 20 38 37 2c 20 20 20 30 2c 20 20 20  22,  87,   0,   
2e8b2 39 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 36 34  9,   0,   0,  64
2e8b3 2c 20 20 36 35 2c 0a 20 20 20 20 20 20 20 30 2c  ,  65,.       0,
2e8b4 20 20 36 33 2c 20 20 20 36 2c 20 20 20 30 2c 20    63,   6,   0, 
2e8b5 20 34 37 2c 20 20 38 34 2c 20 20 39 36 2c 20 20   47,  84,  96,  
2e8b6 20 30 2c 20 31 31 33 2c 20 20 39 35 2c 20 20 20   0, 113,  95,   
2e8b7 30 2c 20 20 20 30 2c 20 20 34 34 2c 0a 20 20 20  0,   0,  44,.   
2e8b8 20 20 20 20 30 2c 20 20 39 37 2c 20 20 32 34 2c      0,  97,  24,
2e8b9 20 20 20 30 2c 20 20 31 37 2c 20 20 20 30 2c 20     0,  17,   0, 
2e8ba 31 31 37 2c 20 20 34 38 2c 20 20 32 33 2c 20 20  117,  48,  23,  
2e8bb 20 30 2c 20 20 20 35 2c 20 31 30 34 2c 20 20 32   0,   5, 104,  2
2e8bc 35 2c 0a 20 20 20 20 20 20 39 30 2c 20 20 20 30  5,.      90,   0
2e8bd 2c 20 20 20 30 2c 20 31 31 39 2c 20 31 30 30 2c  ,   0, 119, 100,
2e8be 20 20 35 35 2c 20 31 31 38 2c 20 20 35 32 2c 20    55, 118,  52, 
2e8bf 20 20 37 2c 20 20 35 30 2c 20 20 20 30 2c 20 20    7,  50,   0,  
2e8c0 38 35 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 39  85,   0,.      9
2e8c1 34 2c 20 20 32 36 2c 20 20 20 30 2c 20 20 39 33  4,  26,   0,  93
2e8c2 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2e8c3 20 20 38 39 2c 20 20 38 36 2c 20 20 39 31 2c 20    89,  86,  91, 
2e8c4 20 38 32 2c 20 31 30 33 2c 20 20 31 34 2c 0a 20   82, 103,  14,. 
2e8c5 20 20 20 20 20 33 38 2c 20 31 30 32 2c 20 20 20       38, 102,   
2e8c6 30 2c 20 20 37 35 2c 20 20 20 30 2c 20 20 31 38  0,  75,   0,  18
2e8c7 2c 20 20 38 33 2c 20 31 30 35 2c 20 20 33 31 2c  ,  83, 105,  31,
2e8c8 20 20 20 30 2c 20 31 31 35 2c 20 20 37 34 2c 20     0, 115,  74, 
2e8c9 31 30 37 2c 0a 20 20 20 20 20 20 35 37 2c 20 20  107,.      57,  
2e8ca 34 35 2c 20 20 37 38 2c 20 20 20 30 2c 20 20 20  45,  78,   0,   
2e8cb 30 2c 20 20 38 38 2c 20 20 33 39 2c 20 20 20 30  0,  88,  39,   0
2e8cc 2c 20 31 31 30 2c 20 20 20 30 2c 20 20 33 35 2c  , 110,   0,  35,
2e8cd 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
2e8ce 20 32 38 2c 20 20 20 30 2c 20 20 38 30 2c 20 20   28,   0,  80,  
2e8cf 35 33 2c 20 20 35 38 2c 20 20 20 30 2c 20 20 32  53,  58,   0,  2
2e8d0 30 2c 20 20 35 36 2c 20 20 20 30 2c 20 20 35 31  0,  56,   0,  51
2e8d1 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  ,.  };.  static 
2e8d2 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2e8d3 68 61 72 20 61 4e 65 78 74 5b 31 31 39 5d 20 3d  har aNext[119] =
2e8d4 20 7b 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30   {.       0,   0
2e8d5 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 34 2c  ,   0,   0,   4,
2e8d6 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2e8d7 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2e8d8 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20   0,   0,.       
2e8d9 30 2c 20 20 20 32 2c 20 20 20 30 2c 20 20 20 30  0,   2,   0,   0
2e8da 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2e8db 20 20 20 30 2c 20 20 31 33 2c 20 20 20 30 2c 20     0,  13,   0, 
2e8dc 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20    0,   0,   0,. 
2e8dd 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20        0,   0,   
2e8de 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2e8df 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2e8e0 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2e8e1 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20    0,.       0,  
2e8e2 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33   0,   0,   0,  3
2e8e3 32 2c 20 20 32 31 2c 20 20 20 30 2c 20 20 20 30  2,  21,   0,   0
2e8e4 2c 20 20 20 30 2c 20 20 34 32 2c 20 20 20 33 2c  ,   0,  42,   3,
2e8e5 20 20 34 36 2c 20 20 20 30 2c 0a 20 20 20 20 20    46,   0,.     
2e8e6 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2e8e7 32 39 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33  29,   0,   0,  3
2e8e8 37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  7,   0,   0,   0
2e8e9 2c 20 20 20 31 2c 20 20 36 30 2c 20 20 20 30 2c  ,   1,  60,   0,
2e8ea 0a 20 20 20 20 20 20 20 30 2c 20 20 36 31 2c 20  .       0,  61, 
2e8eb 20 20 30 2c 20 20 34 30 2c 20 20 20 30 2c 20 20    0,  40,   0,  
2e8ec 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2e8ed 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 35 39  0,   0,   0,  59
2e8ee 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c  ,   0,.       0,
2e8ef 20 20 20 30 2c 20 20 20 30 2c 20 20 33 30 2c 20     0,   0,  30, 
2e8f0 20 35 34 2c 20 20 31 36 2c 20 20 33 33 2c 20 20   54,  16,  33,  
2e8f1 31 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  10,   0,   0,   
2e8f2 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  0,   0,   0,.   
2e8f3 20 20 20 20 30 2c 20 20 20 30 2c 20 20 31 31 2c      0,   0,  11,
2e8f4 20 20 36 36 2c 20 20 37 33 2c 20 20 20 30 2c 20    66,  73,   0, 
2e8f5 20 20 38 2c 20 20 20 30 2c 20 20 39 38 2c 20 20    8,   0,  98,  
2e8f6 39 32 2c 20 20 20 30 2c 20 31 30 31 2c 20 20 20  92,   0, 101,   
2e8f7 30 2c 0a 20 20 20 20 20 20 38 31 2c 20 20 20 30  0,.      81,   0
2e8f8 2c 20 20 36 39 2c 20 20 20 30 2c 20 20 20 30 2c  ,  69,   0,   0,
2e8f9 20 31 30 38 2c 20 20 32 37 2c 20 20 33 36 2c 20   108,  27,  36, 
2e8fa 20 36 37 2c 20 20 37 37 2c 20 20 20 30 2c 20 20   67,  77,   0,  
2e8fb 33 34 2c 20 20 36 32 2c 0a 20 20 20 20 20 20 20  34,  62,.       
2e8fc 30 2c 20 20 20 30 2c 0a 20 20 7d 3b 0a 20 20 73  0,   0,.  };.  s
2e8fd 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
2e8fe 67 6e 65 64 20 63 68 61 72 20 61 4c 65 6e 5b 31  gned char aLen[1
2e8ff 31 39 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 37  19] = {.       7
2e900 2c 20 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c  ,   7,   5,   4,
2e901 20 20 20 36 2c 20 20 20 34 2c 20 20 20 35 2c 20     6,   4,   5, 
2e902 20 20 33 2c 20 20 20 36 2c 20 20 20 37 2c 20 20    3,   6,   7,  
2e903 20 33 2c 20 20 20 36 2c 20 20 20 36 2c 0a 20 20   3,   6,   6,.  
2e904 20 20 20 20 20 37 2c 20 20 20 37 2c 20 20 20 33       7,   7,   3
2e905 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 36 2c  ,   8,   2,   6,
2e906 20 20 20 35 2c 20 20 20 34 2c 20 20 20 34 2c 20     5,   4,   4, 
2e907 20 20 33 2c 20 20 31 30 2c 20 20 20 34 2c 20 20    3,  10,   4,  
2e908 20 36 2c 0a 20 20 20 20 20 20 31 31 2c 20 20 20   6,.      11,   
2e909 32 2c 20 20 20 37 2c 20 20 20 35 2c 20 20 20 35  2,   7,   5,   5
2e90a 2c 20 20 20 39 2c 20 20 20 36 2c 20 20 20 39 2c  ,   9,   6,   9,
2e90b 20 20 20 39 2c 20 20 20 37 2c 20 20 31 30 2c 20     9,   7,  10, 
2e90c 20 31 30 2c 20 20 20 34 2c 0a 20 20 20 20 20 20   10,   4,.      
2e90d 20 36 2c 20 20 20 32 2c 20 20 20 33 2c 20 20 20   6,   2,   3,   
2e90e 34 2c 20 20 20 39 2c 20 20 20 32 2c 20 20 20 36  4,   9,   2,   6
2e90f 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20 36 2c  ,   5,   6,   6,
2e910 20 20 20 35 2c 20 20 20 36 2c 20 20 20 35 2c 0a     5,   6,   5,.
2e911 20 20 20 20 20 20 20 35 2c 20 20 20 37 2c 20 20         5,   7,  
2e912 20 37 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20   7,   7,   3,   
2e913 34 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20 33  4,   4,   7,   3
2e914 2c 20 20 20 36 2c 20 20 20 34 2c 20 20 20 37 2c  ,   6,   4,   7,
2e915 20 20 20 36 2c 0a 20 20 20 20 20 20 31 32 2c 20     6,.      12, 
2e916 20 20 36 2c 20 20 20 39 2c 20 20 20 34 2c 20 20    6,   9,   4,  
2e917 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20   6,   5,   4,   
2e918 37 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36  7,   6,   5,   6
2e919 2c 20 20 20 37 2c 20 20 20 35 2c 0a 20 20 20 20  ,   7,   5,.    
2e91a 20 20 20 34 2c 20 20 20 35 2c 20 20 20 36 2c 20     4,   5,   6, 
2e91b 20 20 35 2c 20 20 20 37 2c 20 20 20 33 2c 20 20    5,   7,   3,  
2e91c 20 37 2c 20 20 31 33 2c 20 20 20 32 2c 20 20 20   7,  13,   2,   
2e91d 32 2c 20 20 20 34 2c 20 20 20 36 2c 20 20 20 36  2,   4,   6,   6
2e91e 2c 0a 20 20 20 20 20 20 20 38 2c 20 20 20 35 2c  ,.       8,   5,
2e91f 20 20 31 37 2c 20 20 31 32 2c 20 20 20 37 2c 20    17,  12,   7, 
2e920 20 20 38 2c 20 20 20 38 2c 20 20 20 32 2c 20 20    8,   8,   2,  
2e921 20 34 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20   4,   4,   4,   
2e922 34 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 32  4,   4,.       2
2e923 2c 20 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c  ,   2,   6,   5,
2e924 20 20 20 38 2c 20 20 20 35 2c 20 20 20 35 2c 20     8,   5,   5, 
2e925 20 20 38 2c 20 20 20 33 2c 20 20 20 35 2c 20 20    8,   3,   5,  
2e926 20 35 2c 20 20 20 36 2c 20 20 20 34 2c 0a 20 20   5,   6,   4,.  
2e927 20 20 20 20 20 39 2c 20 20 20 33 2c 0a 20 20 7d       9,   3,.  }
2e928 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
2e929 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20   unsigned short 
2e92a 69 6e 74 20 61 4f 66 66 73 65 74 5b 31 31 39 5d  int aOffset[119]
2e92b 20 3d 20 7b 0a 20 20 20 20 20 20 20 30 2c 20 20   = {.       0,  
2e92c 20 32 2c 20 20 20 32 2c 20 20 20 38 2c 20 20 20   2,   2,   8,   
2e92d 39 2c 20 20 31 34 2c 20 20 31 36 2c 20 20 32 30  9,  14,  16,  20
2e92e 2c 20 20 32 33 2c 20 20 32 35 2c 20 20 32 35 2c  ,  23,  25,  25,
2e92f 20 20 32 39 2c 20 20 33 33 2c 0a 20 20 20 20 20    29,  33,.     
2e930 20 33 36 2c 20 20 34 31 2c 20 20 34 36 2c 20 20   36,  41,  46,  
2e931 34 38 2c 20 20 35 33 2c 20 20 35 34 2c 20 20 35  48,  53,  54,  5
2e932 39 2c 20 20 36 32 2c 20 20 36 35 2c 20 20 36 37  9,  62,  65,  67
2e933 2c 20 20 36 39 2c 20 20 37 38 2c 20 20 38 31 2c  ,  69,  78,  81,
2e934 0a 20 20 20 20 20 20 38 36 2c 20 20 39 35 2c 20  .      86,  95, 
2e935 20 39 36 2c 20 31 30 31 2c 20 31 30 35 2c 20 31   96, 101, 105, 1
2e936 30 39 2c 20 31 31 37 2c 20 31 32 32 2c 20 31 32  09, 117, 122, 12
2e937 38 2c 20 31 33 36 2c 20 31 34 32 2c 20 31 35 32  8, 136, 142, 152
2e938 2c 20 31 35 39 2c 0a 20 20 20 20 20 31 36 32 2c  , 159,.     162,
2e939 20 31 36 32 2c 20 31 36 35 2c 20 31 36 37 2c 20   162, 165, 167, 
2e93a 31 36 37 2c 20 31 37 31 2c 20 31 37 36 2c 20 31  167, 171, 176, 1
2e93b 37 39 2c 20 31 38 34 2c 20 31 38 39 2c 20 31 39  79, 184, 189, 19
2e93c 34 2c 20 31 39 37 2c 20 32 30 33 2c 0a 20 20 20  4, 197, 203,.   
2e93d 20 20 32 30 36 2c 20 32 31 30 2c 20 32 31 37 2c    206, 210, 217,
2e93e 20 32 32 33 2c 20 32 32 33 2c 20 32 32 36 2c 20   223, 223, 226, 
2e93f 32 32 39 2c 20 32 33 33 2c 20 32 33 34 2c 20 32  229, 233, 234, 2
2e940 33 38 2c 20 32 34 34 2c 20 32 34 38 2c 20 32 35  38, 244, 248, 25
2e941 35 2c 0a 20 20 20 20 20 32 36 31 2c 20 32 37 33  5,.     261, 273
2e942 2c 20 32 37 39 2c 20 32 38 38 2c 20 32 39 30 2c  , 279, 288, 290,
2e943 20 32 39 36 2c 20 33 30 31 2c 20 33 30 33 2c 20   296, 301, 303, 
2e944 33 31 30 2c 20 33 31 35 2c 20 33 32 30 2c 20 33  310, 315, 320, 3
2e945 32 36 2c 20 33 33 32 2c 0a 20 20 20 20 20 33 33  26, 332,.     33
2e946 37 2c 20 33 34 31 2c 20 33 34 34 2c 20 33 35 30  7, 341, 344, 350
2e947 2c 20 33 35 34 2c 20 33 36 31 2c 20 33 36 33 2c  , 354, 361, 363,
2e948 20 33 37 30 2c 20 33 37 32 2c 20 33 37 34 2c 20   370, 372, 374, 
2e949 33 38 33 2c 20 33 38 37 2c 20 33 39 33 2c 0a 20  383, 387, 393,. 
2e94a 20 20 20 20 33 39 39 2c 20 34 30 37 2c 20 34 31      399, 407, 41
2e94b 32 2c 20 34 31 32 2c 20 34 32 38 2c 20 34 33 35  2, 412, 428, 435
2e94c 2c 20 34 34 32 2c 20 34 34 33 2c 20 34 35 30 2c  , 442, 443, 450,
2e94d 20 34 35 34 2c 20 34 35 38 2c 20 34 36 32 2c 20   454, 458, 462, 
2e94e 34 36 36 2c 0a 20 20 20 20 20 34 36 39 2c 20 34  466,.     469, 4
2e94f 37 31 2c 20 34 37 33 2c 20 34 37 39 2c 20 34 38  71, 473, 479, 48
2e950 33 2c 20 34 39 31 2c 20 34 39 35 2c 20 35 30 30  3, 491, 495, 500
2e951 2c 20 35 30 38 2c 20 35 31 31 2c 20 35 31 36 2c  , 508, 511, 516,
2e952 20 35 32 31 2c 20 35 32 37 2c 0a 20 20 20 20 20   521, 527,.     
2e953 35 33 31 2c 20 35 33 36 2c 0a 20 20 7d 3b 0a 20  531, 536,.  };. 
2e954 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
2e955 73 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 64  signed char aCod
2e956 65 5b 31 31 39 5d 20 3d 20 7b 0a 20 20 20 20 54  e[119] = {.    T
2e957 4b 5f 52 45 49 4e 44 45 58 2c 20 20 20 20 54 4b  K_REINDEX,    TK
2e958 5f 49 4e 44 45 58 45 44 2c 20 20 20 20 54 4b 5f  _INDEXED,    TK_
2e959 49 4e 44 45 58 2c 20 20 20 20 20 20 54 4b 5f 44  INDEX,      TK_D
2e95a 45 53 43 2c 20 20 20 20 20 20 20 54 4b 5f 45 53  ESC,       TK_ES
2e95b 43 41 50 45 2c 20 20 20 20 20 0a 20 20 20 20 54  CAPE,     .    T
2e95c 4b 5f 45 41 43 48 2c 20 20 20 20 20 20 20 54 4b  K_EACH,       TK
2e95d 5f 43 48 45 43 4b 2c 20 20 20 20 20 20 54 4b 5f  _CHECK,      TK_
2e95e 4b 45 59 2c 20 20 20 20 20 20 20 20 54 4b 5f 42  KEY,        TK_B
2e95f 45 46 4f 52 45 2c 20 20 20 20 20 54 4b 5f 46 4f  EFORE,     TK_FO
2e960 52 45 49 47 4e 2c 20 20 20 20 0a 20 20 20 20 54  REIGN,    .    T
2e961 4b 5f 46 4f 52 2c 20 20 20 20 20 20 20 20 54 4b  K_FOR,        TK
2e962 5f 49 47 4e 4f 52 45 2c 20 20 20 20 20 54 4b 5f  _IGNORE,     TK_
2e963 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 45  LIKE_KW,    TK_E
2e964 58 50 4c 41 49 4e 2c 20 20 20 20 54 4b 5f 49 4e  XPLAIN,    TK_IN
2e965 53 54 45 41 44 2c 20 20 20 20 0a 20 20 20 20 54  STEAD,    .    T
2e966 4b 5f 41 44 44 2c 20 20 20 20 20 20 20 20 54 4b  K_ADD,        TK
2e967 5f 44 41 54 41 42 41 53 45 2c 20 20 20 54 4b 5f  _DATABASE,   TK_
2e968 41 53 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 53  AS,         TK_S
2e969 45 4c 45 43 54 2c 20 20 20 20 20 54 4b 5f 54 41  ELECT,     TK_TA
2e96a 42 4c 45 2c 20 20 20 20 20 20 0a 20 20 20 20 54  BLE,      .    T
2e96b 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
2e96c 5f 54 48 45 4e 2c 20 20 20 20 20 20 20 54 4b 5f  _THEN,       TK_
2e96d 45 4e 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44  END,        TK_D
2e96e 45 46 45 52 52 41 42 4c 45 2c 20 54 4b 5f 45 4c  EFERRABLE, TK_EL
2e96f 53 45 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  SE,       .    T
2e970 4b 5f 45 58 43 45 50 54 2c 20 20 20 20 20 54 4b  K_EXCEPT,     TK
2e971 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 54 4b 5f  _TRANSACTION,TK_
2e972 4f 4e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 4a  ON,         TK_J
2e973 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 41 4c  OIN_KW,    TK_AL
2e974 54 45 52 2c 20 20 20 20 20 20 0a 20 20 20 20 54  TER,      .    T
2e975 4b 5f 52 41 49 53 45 2c 20 20 20 20 20 20 54 4b  K_RAISE,      TK
2e976 5f 45 58 43 4c 55 53 49 56 45 2c 20 20 54 4b 5f  _EXCLUSIVE,  TK_
2e977 45 58 49 53 54 53 2c 20 20 20 20 20 54 4b 5f 53  EXISTS,     TK_S
2e978 41 56 45 50 4f 49 4e 54 2c 20 20 54 4b 5f 49 4e  AVEPOINT,  TK_IN
2e979 54 45 52 53 45 43 54 2c 20 20 0a 20 20 20 20 54  TERSECT,  .    T
2e97a 4b 5f 54 52 49 47 47 45 52 2c 20 20 20 20 54 4b  K_TRIGGER,    TK
2e97b 5f 52 45 46 45 52 45 4e 43 45 53 2c 20 54 4b 5f  _REFERENCES, TK_
2e97c 43 4f 4e 53 54 52 41 49 4e 54 2c 20 54 4b 5f 49  CONSTRAINT, TK_I
2e97d 4e 54 4f 2c 20 20 20 20 20 20 20 54 4b 5f 4f 46  NTO,       TK_OF
2e97e 46 53 45 54 2c 20 20 20 20 20 0a 20 20 20 20 54  FSET,     .    T
2e97f 4b 5f 4f 46 2c 20 20 20 20 20 20 20 20 20 54 4b  K_OF,         TK
2e980 5f 53 45 54 2c 20 20 20 20 20 20 20 20 54 4b 5f  _SET,        TK_
2e981 54 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f 54  TEMP,       TK_T
2e982 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f 4f 52  EMP,       TK_OR
2e983 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 54  ,         .    T
2e984 4b 5f 55 4e 49 51 55 45 2c 20 20 20 20 20 54 4b  K_UNIQUE,     TK
2e985 5f 51 55 45 52 59 2c 20 20 20 20 20 20 54 4b 5f  _QUERY,      TK_
2e986 41 54 54 41 43 48 2c 20 20 20 20 20 54 4b 5f 48  ATTACH,     TK_H
2e987 41 56 49 4e 47 2c 20 20 20 20 20 54 4b 5f 47 52  AVING,     TK_GR
2e988 4f 55 50 2c 20 20 20 20 20 20 0a 20 20 20 20 54  OUP,      .    T
2e989 4b 5f 55 50 44 41 54 45 2c 20 20 20 20 20 54 4b  K_UPDATE,     TK
2e98a 5f 42 45 47 49 4e 2c 20 20 20 20 20 20 54 4b 5f  _BEGIN,      TK_
2e98b 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 52  JOIN_KW,    TK_R
2e98c 45 4c 45 41 53 45 2c 20 20 20 20 54 4b 5f 42 45  ELEASE,    TK_BE
2e98d 54 57 45 45 4e 2c 20 20 20 20 0a 20 20 20 20 54  TWEEN,    .    T
2e98e 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 20 20 20 54 4b  K_NOTNULL,    TK
2e98f 5f 4e 4f 54 2c 20 20 20 20 20 20 20 20 54 4b 5f  _NOT,        TK_
2e990 4e 55 4c 4c 2c 20 20 20 20 20 20 20 54 4b 5f 4c  NULL,       TK_L
2e991 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 43 41  IKE_KW,    TK_CA
2e992 53 43 41 44 45 2c 20 20 20 20 0a 20 20 20 20 54  SCADE,    .    T
2e993 4b 5f 41 53 43 2c 20 20 20 20 20 20 20 20 54 4b  K_ASC,        TK
2e994 5f 44 45 4c 45 54 45 2c 20 20 20 20 20 54 4b 5f  _DELETE,     TK_
2e995 43 41 53 45 2c 20 20 20 20 20 20 20 54 4b 5f 43  CASE,       TK_C
2e996 4f 4c 4c 41 54 45 2c 20 20 20 20 54 4b 5f 43 52  OLLATE,    TK_CR
2e997 45 41 54 45 2c 20 20 20 20 20 0a 20 20 20 20 54  EATE,     .    T
2e998 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b  K_CTIME_KW,   TK
2e999 5f 44 45 54 41 43 48 2c 20 20 20 20 20 54 4b 5f  _DETACH,     TK_
2e99a 49 4d 4d 45 44 49 41 54 45 2c 20 20 54 4b 5f 4a  IMMEDIATE,  TK_J
2e99b 4f 49 4e 2c 20 20 20 20 20 20 20 54 4b 5f 49 4e  OIN,       TK_IN
2e99c 53 45 52 54 2c 20 20 20 20 20 0a 20 20 20 20 54  SERT,     .    T
2e99d 4b 5f 4d 41 54 43 48 2c 20 20 20 20 20 20 54 4b  K_MATCH,      TK
2e99e 5f 50 4c 41 4e 2c 20 20 20 20 20 20 20 54 4b 5f  _PLAN,       TK_
2e99f 41 4e 41 4c 59 5a 45 2c 20 20 20 20 54 4b 5f 50  ANALYZE,    TK_P
2e9a0 52 41 47 4d 41 2c 20 20 20 20 20 54 4b 5f 41 42  RAGMA,     TK_AB
2e9a1 4f 52 54 2c 20 20 20 20 20 20 0a 20 20 20 20 54  ORT,      .    T
2e9a2 4b 5f 56 41 4c 55 45 53 2c 20 20 20 20 20 54 4b  K_VALUES,     TK
2e9a3 5f 56 49 52 54 55 41 4c 2c 20 20 20 20 54 4b 5f  _VIRTUAL,    TK_
2e9a4 4c 49 4d 49 54 2c 20 20 20 20 20 20 54 4b 5f 57  LIMIT,      TK_W
2e9a5 48 45 4e 2c 20 20 20 20 20 20 20 54 4b 5f 57 48  HEN,       TK_WH
2e9a6 45 52 45 2c 20 20 20 20 20 20 0a 20 20 20 20 54  ERE,      .    T
2e9a7 4b 5f 52 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b  K_RENAME,     TK
2e9a8 5f 41 46 54 45 52 2c 20 20 20 20 20 20 54 4b 5f  _AFTER,      TK_
2e9a9 52 45 50 4c 41 43 45 2c 20 20 20 20 54 4b 5f 41  REPLACE,    TK_A
2e9aa 4e 44 2c 20 20 20 20 20 20 20 20 54 4b 5f 44 45  ND,        TK_DE
2e9ab 46 41 55 4c 54 2c 20 20 20 20 0a 20 20 20 20 54  FAULT,    .    T
2e9ac 4b 5f 41 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b  K_AUTOINCR,   TK
2e9ad 5f 54 4f 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _TO,         TK_
2e9ae 49 4e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 43  IN,         TK_C
2e9af 41 53 54 2c 20 20 20 20 20 20 20 54 4b 5f 43 4f  AST,       TK_CO
2e9b0 4c 55 4d 4e 4b 57 2c 20 20 20 0a 20 20 20 20 54  LUMNKW,   .    T
2e9b1 4b 5f 43 4f 4d 4d 49 54 2c 20 20 20 20 20 54 4b  K_COMMIT,     TK
2e9b2 5f 43 4f 4e 46 4c 49 43 54 2c 20 20 20 54 4b 5f  _CONFLICT,   TK_
2e9b3 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 43  JOIN_KW,    TK_C
2e9b4 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f 43 54  TIME_KW,   TK_CT
2e9b5 49 4d 45 5f 4b 57 2c 20 20 20 0a 20 20 20 20 54  IME_KW,   .    T
2e9b6 4b 5f 50 52 49 4d 41 52 59 2c 20 20 20 20 54 4b  K_PRIMARY,    TK
2e9b7 5f 44 45 46 45 52 52 45 44 2c 20 20 20 54 4b 5f  _DEFERRED,   TK_
2e9b8 44 49 53 54 49 4e 43 54 2c 20 20 20 54 4b 5f 49  DISTINCT,   TK_I
2e9b9 53 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 44 52  S,         TK_DR
2e9ba 4f 50 2c 20 20 20 20 20 20 20 0a 20 20 20 20 54  OP,       .    T
2e9bb 4b 5f 46 41 49 4c 2c 20 20 20 20 20 20 20 54 4b  K_FAIL,       TK
2e9bc 5f 46 52 4f 4d 2c 20 20 20 20 20 20 20 54 4b 5f  _FROM,       TK_
2e9bd 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 4c  JOIN_KW,    TK_L
2e9be 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f 42 59  IKE_KW,    TK_BY
2e9bf 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 54  ,         .    T
2e9c0 4b 5f 49 46 2c 20 20 20 20 20 20 20 20 20 54 4b  K_IF,         TK
2e9c1 5f 49 53 4e 55 4c 4c 2c 20 20 20 20 20 54 4b 5f  _ISNULL,     TK_
2e9c2 4f 52 44 45 52 2c 20 20 20 20 20 20 54 4b 5f 52  ORDER,      TK_R
2e9c3 45 53 54 52 49 43 54 2c 20 20 20 54 4b 5f 4a 4f  ESTRICT,   TK_JO
2e9c4 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20 20 54  IN_KW,    .    T
2e9c5 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
2e9c6 5f 52 4f 4c 4c 42 41 43 4b 2c 20 20 20 54 4b 5f  _ROLLBACK,   TK_
2e9c7 52 4f 57 2c 20 20 20 20 20 20 20 20 54 4b 5f 55  ROW,        TK_U
2e9c8 4e 49 4f 4e 2c 20 20 20 20 20 20 54 4b 5f 55 53  NION,      TK_US
2e9c9 49 4e 47 2c 20 20 20 20 20 20 0a 20 20 20 20 54  ING,      .    T
2e9ca 4b 5f 56 41 43 55 55 4d 2c 20 20 20 20 20 54 4b  K_VACUUM,     TK
2e9cb 5f 56 49 45 57 2c 20 20 20 20 20 20 20 54 4b 5f  _VIEW,       TK_
2e9cc 49 4e 49 54 49 41 4c 4c 59 2c 20 20 54 4b 5f 41  INITIALLY,  TK_A
2e9cd 4c 4c 2c 20 20 20 20 20 20 20 20 0a 20 20 7d 3b  LL,        .  };
2e9ce 0a 20 20 69 6e 74 20 68 2c 20 69 3b 0a 20 20 69  .  int h, i;.  i
2e9cf 66 28 20 6e 3c 32 20 29 20 72 65 74 75 72 6e 20  f( n<2 ) return 
2e9d0 54 4b 5f 49 44 3b 0a 20 20 68 20 3d 20 28 28 63  TK_ID;.  h = ((c
2e9d1 68 61 72 4d 61 70 28 7a 5b 30 5d 29 2a 34 29 20  harMap(z[0])*4) 
2e9d2 5e 0a 20 20 20 20 20 20 28 63 68 61 72 4d 61 70  ^.      (charMap
2e9d3 28 7a 5b 6e 2d 31 5d 29 2a 33 29 20 5e 0a 20 20  (z[n-1])*3) ^.  
2e9d4 20 20 20 20 6e 29 20 25 20 31 32 37 3b 0a 20 20      n) % 127;.  
2e9d5 66 6f 72 28 69 3d 28 28 69 6e 74 29 61 48 61 73  for(i=((int)aHas
2e9d6 68 5b 68 5d 29 2d 31 3b 20 69 3e 3d 30 3b 20 69  h[h])-1; i>=0; i
2e9d7 3d 28 28 69 6e 74 29 61 4e 65 78 74 5b 69 5d 29  =((int)aNext[i])
2e9d8 2d 31 29 7b 0a 20 20 20 20 69 66 28 20 61 4c 65  -1){.    if( aLe
2e9d9 6e 5b 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74  n[i]==n && sqlit
2e9da 65 33 53 74 72 4e 49 43 6d 70 28 26 7a 54 65 78  e3StrNICmp(&zTex
2e9db 74 5b 61 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c  t[aOffset[i]],z,
2e9dc 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 74  n)==0 ){.      t
2e9dd 65 73 74 63 61 73 65 28 20 69 3d 3d 30 20 29 3b  estcase( i==0 );
2e9de 20 2f 2a 20 54 4b 5f 52 45 49 4e 44 45 58 20 2a   /* TK_REINDEX *
2e9df 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2e9e0 28 20 69 3d 3d 31 20 29 3b 20 2f 2a 20 54 4b 5f  ( i==1 ); /* TK_
2e9e1 49 4e 44 45 58 45 44 20 2a 2f 0a 20 20 20 20 20  INDEXED */.     
2e9e2 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 20   testcase( i==2 
2e9e3 29 3b 20 2f 2a 20 54 4b 5f 49 4e 44 45 58 20 2a  ); /* TK_INDEX *
2e9e4 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2e9e5 28 20 69 3d 3d 33 20 29 3b 20 2f 2a 20 54 4b 5f  ( i==3 ); /* TK_
2e9e6 44 45 53 43 20 2a 2f 0a 20 20 20 20 20 20 74 65  DESC */.      te
2e9e7 73 74 63 61 73 65 28 20 69 3d 3d 34 20 29 3b 20  stcase( i==4 ); 
2e9e8 2f 2a 20 54 4b 5f 45 53 43 41 50 45 20 2a 2f 0a  /* TK_ESCAPE */.
2e9e9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2e9ea 69 3d 3d 35 20 29 3b 20 2f 2a 20 54 4b 5f 45 41  i==5 ); /* TK_EA
2e9eb 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CH */.      test
2e9ec 63 61 73 65 28 20 69 3d 3d 36 20 29 3b 20 2f 2a  case( i==6 ); /*
2e9ed 20 54 4b 5f 43 48 45 43 4b 20 2a 2f 0a 20 20 20   TK_CHECK */.   
2e9ee 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2e9ef 37 20 29 3b 20 2f 2a 20 54 4b 5f 4b 45 59 20 2a  7 ); /* TK_KEY *
2e9f0 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2e9f1 28 20 69 3d 3d 38 20 29 3b 20 2f 2a 20 54 4b 5f  ( i==8 ); /* TK_
2e9f2 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20  BEFORE */.      
2e9f3 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 20 29  testcase( i==9 )
2e9f4 3b 20 2f 2a 20 54 4b 5f 46 4f 52 45 49 47 4e 20  ; /* TK_FOREIGN 
2e9f5 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2e9f6 65 28 20 69 3d 3d 31 30 20 29 3b 20 2f 2a 20 54  e( i==10 ); /* T
2e9f7 4b 5f 46 4f 52 20 2a 2f 0a 20 20 20 20 20 20 74  K_FOR */.      t
2e9f8 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 20 29  estcase( i==11 )
2e9f9 3b 20 2f 2a 20 54 4b 5f 49 47 4e 4f 52 45 20 2a  ; /* TK_IGNORE *
2e9fa 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2e9fb 28 20 69 3d 3d 31 32 20 29 3b 20 2f 2a 20 54 4b  ( i==12 ); /* TK
2e9fc 5f 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20  _LIKE_KW */.    
2e9fd 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
2e9fe 33 20 29 3b 20 2f 2a 20 54 4b 5f 45 58 50 4c 41  3 ); /* TK_EXPLA
2e9ff 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IN */.      test
2ea00 63 61 73 65 28 20 69 3d 3d 31 34 20 29 3b 20 2f  case( i==14 ); /
2ea01 2a 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a  * TK_INSTEAD */.
2ea02 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ea03 69 3d 3d 31 35 20 29 3b 20 2f 2a 20 54 4b 5f 41  i==15 ); /* TK_A
2ea04 44 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  DD */.      test
2ea05 63 61 73 65 28 20 69 3d 3d 31 36 20 29 3b 20 2f  case( i==16 ); /
2ea06 2a 20 54 4b 5f 44 41 54 41 42 41 53 45 20 2a 2f  * TK_DATABASE */
2ea07 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ea08 20 69 3d 3d 31 37 20 29 3b 20 2f 2a 20 54 4b 5f   i==17 ); /* TK_
2ea09 41 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  AS */.      test
2ea0a 63 61 73 65 28 20 69 3d 3d 31 38 20 29 3b 20 2f  case( i==18 ); /
2ea0b 2a 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20  * TK_SELECT */. 
2ea0c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2ea0d 3d 3d 31 39 20 29 3b 20 2f 2a 20 54 4b 5f 54 41  ==19 ); /* TK_TA
2ea0e 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  BLE */.      tes
2ea0f 74 63 61 73 65 28 20 69 3d 3d 32 30 20 29 3b 20  tcase( i==20 ); 
2ea10 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f  /* TK_JOIN_KW */
2ea11 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ea12 20 69 3d 3d 32 31 20 29 3b 20 2f 2a 20 54 4b 5f   i==21 ); /* TK_
2ea13 54 48 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  THEN */.      te
2ea14 73 74 63 61 73 65 28 20 69 3d 3d 32 32 20 29 3b  stcase( i==22 );
2ea15 20 2f 2a 20 54 4b 5f 45 4e 44 20 2a 2f 0a 20 20   /* TK_END */.  
2ea16 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2ea17 3d 32 33 20 29 3b 20 2f 2a 20 54 4b 5f 44 45 46  =23 ); /* TK_DEF
2ea18 45 52 52 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20  ERRABLE */.     
2ea19 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 34   testcase( i==24
2ea1a 20 29 3b 20 2f 2a 20 54 4b 5f 45 4c 53 45 20 2a   ); /* TK_ELSE *
2ea1b 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2ea1c 28 20 69 3d 3d 32 35 20 29 3b 20 2f 2a 20 54 4b  ( i==25 ); /* TK
2ea1d 5f 45 58 43 45 50 54 20 2a 2f 0a 20 20 20 20 20  _EXCEPT */.     
2ea1e 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 36   testcase( i==26
2ea1f 20 29 3b 20 2f 2a 20 54 4b 5f 54 52 41 4e 53 41   ); /* TK_TRANSA
2ea20 43 54 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74  CTION */.      t
2ea21 65 73 74 63 61 73 65 28 20 69 3d 3d 32 37 20 29  estcase( i==27 )
2ea22 3b 20 2f 2a 20 54 4b 5f 4f 4e 20 2a 2f 0a 20 20  ; /* TK_ON */.  
2ea23 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2ea24 3d 32 38 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49  =28 ); /* TK_JOI
2ea25 4e 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65  N_KW */.      te
2ea26 73 74 63 61 73 65 28 20 69 3d 3d 32 39 20 29 3b  stcase( i==29 );
2ea27 20 2f 2a 20 54 4b 5f 41 4c 54 45 52 20 2a 2f 0a   /* TK_ALTER */.
2ea28 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ea29 69 3d 3d 33 30 20 29 3b 20 2f 2a 20 54 4b 5f 52  i==30 ); /* TK_R
2ea2a 41 49 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  AISE */.      te
2ea2b 73 74 63 61 73 65 28 20 69 3d 3d 33 31 20 29 3b  stcase( i==31 );
2ea2c 20 2f 2a 20 54 4b 5f 45 58 43 4c 55 53 49 56 45   /* TK_EXCLUSIVE
2ea2d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ea2e 73 65 28 20 69 3d 3d 33 32 20 29 3b 20 2f 2a 20  se( i==32 ); /* 
2ea2f 54 4b 5f 45 58 49 53 54 53 20 2a 2f 0a 20 20 20  TK_EXISTS */.   
2ea30 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2ea31 33 33 20 29 3b 20 2f 2a 20 54 4b 5f 53 41 56 45  33 ); /* TK_SAVE
2ea32 50 4f 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74  POINT */.      t
2ea33 65 73 74 63 61 73 65 28 20 69 3d 3d 33 34 20 29  estcase( i==34 )
2ea34 3b 20 2f 2a 20 54 4b 5f 49 4e 54 45 52 53 45 43  ; /* TK_INTERSEC
2ea35 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
2ea36 61 73 65 28 20 69 3d 3d 33 35 20 29 3b 20 2f 2a  ase( i==35 ); /*
2ea37 20 54 4b 5f 54 52 49 47 47 45 52 20 2a 2f 0a 20   TK_TRIGGER */. 
2ea38 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2ea39 3d 3d 33 36 20 29 3b 20 2f 2a 20 54 4b 5f 52 45  ==36 ); /* TK_RE
2ea3a 46 45 52 45 4e 43 45 53 20 2a 2f 0a 20 20 20 20  FERENCES */.    
2ea3b 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
2ea3c 37 20 29 3b 20 2f 2a 20 54 4b 5f 43 4f 4e 53 54  7 ); /* TK_CONST
2ea3d 52 41 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74  RAINT */.      t
2ea3e 65 73 74 63 61 73 65 28 20 69 3d 3d 33 38 20 29  estcase( i==38 )
2ea3f 3b 20 2f 2a 20 54 4b 5f 49 4e 54 4f 20 2a 2f 0a  ; /* TK_INTO */.
2ea40 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ea41 69 3d 3d 33 39 20 29 3b 20 2f 2a 20 54 4b 5f 4f  i==39 ); /* TK_O
2ea42 46 46 53 45 54 20 2a 2f 0a 20 20 20 20 20 20 74  FFSET */.      t
2ea43 65 73 74 63 61 73 65 28 20 69 3d 3d 34 30 20 29  estcase( i==40 )
2ea44 3b 20 2f 2a 20 54 4b 5f 4f 46 20 2a 2f 0a 20 20  ; /* TK_OF */.  
2ea45 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2ea46 3d 34 31 20 29 3b 20 2f 2a 20 54 4b 5f 53 45 54  =41 ); /* TK_SET
2ea47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ea48 73 65 28 20 69 3d 3d 34 32 20 29 3b 20 2f 2a 20  se( i==42 ); /* 
2ea49 54 4b 5f 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20  TK_TEMP */.     
2ea4a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 33   testcase( i==43
2ea4b 20 29 3b 20 2f 2a 20 54 4b 5f 54 45 4d 50 20 2a   ); /* TK_TEMP *
2ea4c 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2ea4d 28 20 69 3d 3d 34 34 20 29 3b 20 2f 2a 20 54 4b  ( i==44 ); /* TK
2ea4e 5f 4f 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  _OR */.      tes
2ea4f 74 63 61 73 65 28 20 69 3d 3d 34 35 20 29 3b 20  tcase( i==45 ); 
2ea50 2f 2a 20 54 4b 5f 55 4e 49 51 55 45 20 2a 2f 0a  /* TK_UNIQUE */.
2ea51 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ea52 69 3d 3d 34 36 20 29 3b 20 2f 2a 20 54 4b 5f 51  i==46 ); /* TK_Q
2ea53 55 45 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65  UERY */.      te
2ea54 73 74 63 61 73 65 28 20 69 3d 3d 34 37 20 29 3b  stcase( i==47 );
2ea55 20 2f 2a 20 54 4b 5f 41 54 54 41 43 48 20 2a 2f   /* TK_ATTACH */
2ea56 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ea57 20 69 3d 3d 34 38 20 29 3b 20 2f 2a 20 54 4b 5f   i==48 ); /* TK_
2ea58 48 41 56 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20  HAVING */.      
2ea59 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 39 20  testcase( i==49 
2ea5a 29 3b 20 2f 2a 20 54 4b 5f 47 52 4f 55 50 20 2a  ); /* TK_GROUP *
2ea5b 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2ea5c 28 20 69 3d 3d 35 30 20 29 3b 20 2f 2a 20 54 4b  ( i==50 ); /* TK
2ea5d 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20 20 20 20  _UPDATE */.     
2ea5e 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 31   testcase( i==51
2ea5f 20 29 3b 20 2f 2a 20 54 4b 5f 42 45 47 49 4e 20   ); /* TK_BEGIN 
2ea60 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2ea61 65 28 20 69 3d 3d 35 32 20 29 3b 20 2f 2a 20 54  e( i==52 ); /* T
2ea62 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 20 20 20  K_JOIN_KW */.   
2ea63 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2ea64 35 33 20 29 3b 20 2f 2a 20 54 4b 5f 52 45 4c 45  53 ); /* TK_RELE
2ea65 41 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ASE */.      tes
2ea66 74 63 61 73 65 28 20 69 3d 3d 35 34 20 29 3b 20  tcase( i==54 ); 
2ea67 2f 2a 20 54 4b 5f 42 45 54 57 45 45 4e 20 2a 2f  /* TK_BETWEEN */
2ea68 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ea69 20 69 3d 3d 35 35 20 29 3b 20 2f 2a 20 54 4b 5f   i==55 ); /* TK_
2ea6a 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20  NOTNULL */.     
2ea6b 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 36   testcase( i==56
2ea6c 20 29 3b 20 2f 2a 20 54 4b 5f 4e 4f 54 20 2a 2f   ); /* TK_NOT */
2ea6d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ea6e 20 69 3d 3d 35 37 20 29 3b 20 2f 2a 20 54 4b 5f   i==57 ); /* TK_
2ea6f 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65  NULL */.      te
2ea70 73 74 63 61 73 65 28 20 69 3d 3d 35 38 20 29 3b  stcase( i==58 );
2ea71 20 2f 2a 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 2a   /* TK_LIKE_KW *
2ea72 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2ea73 28 20 69 3d 3d 35 39 20 29 3b 20 2f 2a 20 54 4b  ( i==59 ); /* TK
2ea74 5f 43 41 53 43 41 44 45 20 2a 2f 0a 20 20 20 20  _CASCADE */.    
2ea75 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
2ea76 30 20 29 3b 20 2f 2a 20 54 4b 5f 41 53 43 20 2a  0 ); /* TK_ASC *
2ea77 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2ea78 28 20 69 3d 3d 36 31 20 29 3b 20 2f 2a 20 54 4b  ( i==61 ); /* TK
2ea79 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 20 20 20  _DELETE */.     
2ea7a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 32   testcase( i==62
2ea7b 20 29 3b 20 2f 2a 20 54 4b 5f 43 41 53 45 20 2a   ); /* TK_CASE *
2ea7c 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2ea7d 28 20 69 3d 3d 36 33 20 29 3b 20 2f 2a 20 54 4b  ( i==63 ); /* TK
2ea7e 5f 43 4f 4c 4c 41 54 45 20 2a 2f 0a 20 20 20 20  _COLLATE */.    
2ea7f 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
2ea80 34 20 29 3b 20 2f 2a 20 54 4b 5f 43 52 45 41 54  4 ); /* TK_CREAT
2ea81 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
2ea82 61 73 65 28 20 69 3d 3d 36 35 20 29 3b 20 2f 2a  ase( i==65 ); /*
2ea83 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a   TK_CTIME_KW */.
2ea84 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ea85 69 3d 3d 36 36 20 29 3b 20 2f 2a 20 54 4b 5f 44  i==66 ); /* TK_D
2ea86 45 54 41 43 48 20 2a 2f 0a 20 20 20 20 20 20 74  ETACH */.      t
2ea87 65 73 74 63 61 73 65 28 20 69 3d 3d 36 37 20 29  estcase( i==67 )
2ea88 3b 20 2f 2a 20 54 4b 5f 49 4d 4d 45 44 49 41 54  ; /* TK_IMMEDIAT
2ea89 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
2ea8a 61 73 65 28 20 69 3d 3d 36 38 20 29 3b 20 2f 2a  ase( i==68 ); /*
2ea8b 20 54 4b 5f 4a 4f 49 4e 20 2a 2f 0a 20 20 20 20   TK_JOIN */.    
2ea8c 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
2ea8d 39 20 29 3b 20 2f 2a 20 54 4b 5f 49 4e 53 45 52  9 ); /* TK_INSER
2ea8e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
2ea8f 61 73 65 28 20 69 3d 3d 37 30 20 29 3b 20 2f 2a  ase( i==70 ); /*
2ea90 20 54 4b 5f 4d 41 54 43 48 20 2a 2f 0a 20 20 20   TK_MATCH */.   
2ea91 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2ea92 37 31 20 29 3b 20 2f 2a 20 54 4b 5f 50 4c 41 4e  71 ); /* TK_PLAN
2ea93 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ea94 73 65 28 20 69 3d 3d 37 32 20 29 3b 20 2f 2a 20  se( i==72 ); /* 
2ea95 54 4b 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 20 20  TK_ANALYZE */.  
2ea96 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2ea97 3d 37 33 20 29 3b 20 2f 2a 20 54 4b 5f 50 52 41  =73 ); /* TK_PRA
2ea98 47 4d 41 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  GMA */.      tes
2ea99 74 63 61 73 65 28 20 69 3d 3d 37 34 20 29 3b 20  tcase( i==74 ); 
2ea9a 2f 2a 20 54 4b 5f 41 42 4f 52 54 20 2a 2f 0a 20  /* TK_ABORT */. 
2ea9b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2ea9c 3d 3d 37 35 20 29 3b 20 2f 2a 20 54 4b 5f 56 41  ==75 ); /* TK_VA
2ea9d 4c 55 45 53 20 2a 2f 0a 20 20 20 20 20 20 74 65  LUES */.      te
2ea9e 73 74 63 61 73 65 28 20 69 3d 3d 37 36 20 29 3b  stcase( i==76 );
2ea9f 20 2f 2a 20 54 4b 5f 56 49 52 54 55 41 4c 20 2a   /* TK_VIRTUAL *
2eaa0 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2eaa1 28 20 69 3d 3d 37 37 20 29 3b 20 2f 2a 20 54 4b  ( i==77 ); /* TK
2eaa2 5f 4c 49 4d 49 54 20 2a 2f 0a 20 20 20 20 20 20  _LIMIT */.      
2eaa3 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 38 20  testcase( i==78 
2eaa4 29 3b 20 2f 2a 20 54 4b 5f 57 48 45 4e 20 2a 2f  ); /* TK_WHEN */
2eaa5 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2eaa6 20 69 3d 3d 37 39 20 29 3b 20 2f 2a 20 54 4b 5f   i==79 ); /* TK_
2eaa7 57 48 45 52 45 20 2a 2f 0a 20 20 20 20 20 20 74  WHERE */.      t
2eaa8 65 73 74 63 61 73 65 28 20 69 3d 3d 38 30 20 29  estcase( i==80 )
2eaa9 3b 20 2f 2a 20 54 4b 5f 52 45 4e 41 4d 45 20 2a  ; /* TK_RENAME *
2eaaa 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2eaab 28 20 69 3d 3d 38 31 20 29 3b 20 2f 2a 20 54 4b  ( i==81 ); /* TK
2eaac 5f 41 46 54 45 52 20 2a 2f 0a 20 20 20 20 20 20  _AFTER */.      
2eaad 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 32 20  testcase( i==82 
2eaae 29 3b 20 2f 2a 20 54 4b 5f 52 45 50 4c 41 43 45  ); /* TK_REPLACE
2eaaf 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2eab0 73 65 28 20 69 3d 3d 38 33 20 29 3b 20 2f 2a 20  se( i==83 ); /* 
2eab1 54 4b 5f 41 4e 44 20 2a 2f 0a 20 20 20 20 20 20  TK_AND */.      
2eab2 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 34 20  testcase( i==84 
2eab3 29 3b 20 2f 2a 20 54 4b 5f 44 45 46 41 55 4c 54  ); /* TK_DEFAULT
2eab4 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2eab5 73 65 28 20 69 3d 3d 38 35 20 29 3b 20 2f 2a 20  se( i==85 ); /* 
2eab6 54 4b 5f 41 55 54 4f 49 4e 43 52 20 2a 2f 0a 20  TK_AUTOINCR */. 
2eab7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2eab8 3d 3d 38 36 20 29 3b 20 2f 2a 20 54 4b 5f 54 4f  ==86 ); /* TK_TO
2eab9 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2eaba 73 65 28 20 69 3d 3d 38 37 20 29 3b 20 2f 2a 20  se( i==87 ); /* 
2eabb 54 4b 5f 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74  TK_IN */.      t
2eabc 65 73 74 63 61 73 65 28 20 69 3d 3d 38 38 20 29  estcase( i==88 )
2eabd 3b 20 2f 2a 20 54 4b 5f 43 41 53 54 20 2a 2f 0a  ; /* TK_CAST */.
2eabe 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2eabf 69 3d 3d 38 39 20 29 3b 20 2f 2a 20 54 4b 5f 43  i==89 ); /* TK_C
2eac0 4f 4c 55 4d 4e 4b 57 20 2a 2f 0a 20 20 20 20 20  OLUMNKW */.     
2eac1 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 30   testcase( i==90
2eac2 20 29 3b 20 2f 2a 20 54 4b 5f 43 4f 4d 4d 49 54   ); /* TK_COMMIT
2eac3 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2eac4 73 65 28 20 69 3d 3d 39 31 20 29 3b 20 2f 2a 20  se( i==91 ); /* 
2eac5 54 4b 5f 43 4f 4e 46 4c 49 43 54 20 2a 2f 0a 20  TK_CONFLICT */. 
2eac6 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2eac7 3d 3d 39 32 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f  ==92 ); /* TK_JO
2eac8 49 4e 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74  IN_KW */.      t
2eac9 65 73 74 63 61 73 65 28 20 69 3d 3d 39 33 20 29  estcase( i==93 )
2eaca 3b 20 2f 2a 20 54 4b 5f 43 54 49 4d 45 5f 4b 57  ; /* TK_CTIME_KW
2eacb 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2eacc 73 65 28 20 69 3d 3d 39 34 20 29 3b 20 2f 2a 20  se( i==94 ); /* 
2eacd 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 20  TK_CTIME_KW */. 
2eace 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2eacf 3d 3d 39 35 20 29 3b 20 2f 2a 20 54 4b 5f 50 52  ==95 ); /* TK_PR
2ead0 49 4d 41 52 59 20 2a 2f 0a 20 20 20 20 20 20 74  IMARY */.      t
2ead1 65 73 74 63 61 73 65 28 20 69 3d 3d 39 36 20 29  estcase( i==96 )
2ead2 3b 20 2f 2a 20 54 4b 5f 44 45 46 45 52 52 45 44  ; /* TK_DEFERRED
2ead3 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ead4 73 65 28 20 69 3d 3d 39 37 20 29 3b 20 2f 2a 20  se( i==97 ); /* 
2ead5 54 4b 5f 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20  TK_DISTINCT */. 
2ead6 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2ead7 3d 3d 39 38 20 29 3b 20 2f 2a 20 54 4b 5f 49 53  ==98 ); /* TK_IS
2ead8 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2ead9 73 65 28 20 69 3d 3d 39 39 20 29 3b 20 2f 2a 20  se( i==99 ); /* 
2eada 54 4b 5f 44 52 4f 50 20 2a 2f 0a 20 20 20 20 20  TK_DROP */.     
2eadb 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
2eadc 30 20 29 3b 20 2f 2a 20 54 4b 5f 46 41 49 4c 20  0 ); /* TK_FAIL 
2eadd 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2eade 65 28 20 69 3d 3d 31 30 31 20 29 3b 20 2f 2a 20  e( i==101 ); /* 
2eadf 54 4b 5f 46 52 4f 4d 20 2a 2f 0a 20 20 20 20 20  TK_FROM */.     
2eae0 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
2eae1 32 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f  2 ); /* TK_JOIN_
2eae2 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  KW */.      test
2eae3 63 61 73 65 28 20 69 3d 3d 31 30 33 20 29 3b 20  case( i==103 ); 
2eae4 2f 2a 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 2a 2f  /* TK_LIKE_KW */
2eae5 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2eae6 20 69 3d 3d 31 30 34 20 29 3b 20 2f 2a 20 54 4b   i==104 ); /* TK
2eae7 5f 42 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  _BY */.      tes
2eae8 74 63 61 73 65 28 20 69 3d 3d 31 30 35 20 29 3b  tcase( i==105 );
2eae9 20 2f 2a 20 54 4b 5f 49 46 20 2a 2f 0a 20 20 20   /* TK_IF */.   
2eaea 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2eaeb 31 30 36 20 29 3b 20 2f 2a 20 54 4b 5f 49 53 4e  106 ); /* TK_ISN
2eaec 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ULL */.      tes
2eaed 74 63 61 73 65 28 20 69 3d 3d 31 30 37 20 29 3b  tcase( i==107 );
2eaee 20 2f 2a 20 54 4b 5f 4f 52 44 45 52 20 2a 2f 0a   /* TK_ORDER */.
2eaef 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2eaf0 69 3d 3d 31 30 38 20 29 3b 20 2f 2a 20 54 4b 5f  i==108 ); /* TK_
2eaf1 52 45 53 54 52 49 43 54 20 2a 2f 0a 20 20 20 20  RESTRICT */.    
2eaf2 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
2eaf3 30 39 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e  09 ); /* TK_JOIN
2eaf4 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  _KW */.      tes
2eaf5 74 63 61 73 65 28 20 69 3d 3d 31 31 30 20 29 3b  tcase( i==110 );
2eaf6 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a   /* TK_JOIN_KW *
2eaf7 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2eaf8 28 20 69 3d 3d 31 31 31 20 29 3b 20 2f 2a 20 54  ( i==111 ); /* T
2eaf9 4b 5f 52 4f 4c 4c 42 41 43 4b 20 2a 2f 0a 20 20  K_ROLLBACK */.  
2eafa 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2eafb 3d 31 31 32 20 29 3b 20 2f 2a 20 54 4b 5f 52 4f  =112 ); /* TK_RO
2eafc 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  W */.      testc
2eafd 61 73 65 28 20 69 3d 3d 31 31 33 20 29 3b 20 2f  ase( i==113 ); /
2eafe 2a 20 54 4b 5f 55 4e 49 4f 4e 20 2a 2f 0a 20 20  * TK_UNION */.  
2eaff 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2eb00 3d 31 31 34 20 29 3b 20 2f 2a 20 54 4b 5f 55 53  =114 ); /* TK_US
2eb01 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ING */.      tes
2eb02 74 63 61 73 65 28 20 69 3d 3d 31 31 35 20 29 3b  tcase( i==115 );
2eb03 20 2f 2a 20 54 4b 5f 56 41 43 55 55 4d 20 2a 2f   /* TK_VACUUM */
2eb04 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2eb05 20 69 3d 3d 31 31 36 20 29 3b 20 2f 2a 20 54 4b   i==116 ); /* TK
2eb06 5f 56 49 45 57 20 2a 2f 0a 20 20 20 20 20 20 74  _VIEW */.      t
2eb07 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 37 20  estcase( i==117 
2eb08 29 3b 20 2f 2a 20 54 4b 5f 49 4e 49 54 49 41 4c  ); /* TK_INITIAL
2eb09 4c 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  LY */.      test
2eb0a 63 61 73 65 28 20 69 3d 3d 31 31 38 20 29 3b 20  case( i==118 ); 
2eb0b 2f 2a 20 54 4b 5f 41 4c 4c 20 2a 2f 0a 20 20 20  /* TK_ALL */.   
2eb0c 20 20 20 72 65 74 75 72 6e 20 61 43 6f 64 65 5b     return aCode[
2eb0d 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  i];.    }.  }.  
2eb0e 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d 0a  return TK_ID;.}.
2eb0f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2eb10 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72  nt sqlite3Keywor
2eb11 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69  dCode(const unsi
2eb12 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e  gned char *z, in
2eb13 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 6b  t n){.  return k
2eb14 65 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72  eywordCode((char
2eb15 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  *)z, n);.}../***
2eb16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2eb17 6f 66 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68  of keywordhash.h
2eb18 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2eb19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2eb1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2eb1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
2eb1c 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
2eb1d 6c 65 66 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65  left off in toke
2eb1e 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  nize.c *********
2eb1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a  **********/.../*
2eb20 0a 2a 2a 20 49 66 20 58 20 69 73 20 61 20 63 68  .** If X is a ch
2eb21 61 72 61 63 74 65 72 20 74 68 61 74 20 63 61 6e  aracter that can
2eb22 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20 69   be used in an i
2eb23 64 65 6e 74 69 66 69 65 72 20 74 68 65 6e 0a 2a  dentifier then.*
2eb24 2a 20 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c  * IdChar(X) will
2eb25 20 62 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72   be true.  Other
2eb26 77 69 73 65 20 69 74 20 69 73 20 66 61 6c 73 65  wise it is false
2eb27 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49  ..**.** For ASCI
2eb28 49 2c 20 61 6e 79 20 63 68 61 72 61 63 74 65 72  I, any character
2eb29 20 77 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f   with the high-o
2eb2a 72 64 65 72 20 62 69 74 20 73 65 74 20 69 73 0a  rder bit set is.
2eb2b 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e  ** allowed in an
2eb2c 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f   identifier.  Fo
2eb2d 72 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65  r 7-bit characte
2eb2e 72 73 2c 20 0a 2a 2a 20 73 71 6c 69 74 65 33 49  rs, .** sqlite3I
2eb2f 73 49 64 43 68 61 72 5b 58 5d 20 6d 75 73 74 20  sIdChar[X] must 
2eb30 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  be 1..**.** For 
2eb31 45 42 43 44 49 43 2c 20 74 68 65 20 72 75 6c 65  EBCDIC, the rule
2eb32 73 20 61 72 65 20 6d 6f 72 65 20 63 6f 6d 70 6c  s are more compl
2eb33 65 78 20 62 75 74 20 68 61 76 65 20 74 68 65 20  ex but have the 
2eb34 73 61 6d 65 0a 2a 2a 20 65 6e 64 20 72 65 73 75  same.** end resu
2eb35 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74  lt..**.** Ticket
2eb36 20 23 31 30 36 36 2e 20 20 74 68 65 20 53 51 4c   #1066.  the SQL
2eb37 20 73 74 61 6e 64 61 72 64 20 64 6f 65 73 20 6e   standard does n
2eb38 6f 74 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20  ot allow '$' in 
2eb39 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66  the.** middle of
2eb3a 20 69 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75   identfiers.  Bu
2eb3b 74 20 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65  t many SQL imple
2eb3c 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a  mentations do. .
2eb3d 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61  ** SQLite will a
2eb3e 6c 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e  llow '$' in iden
2eb3f 74 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70  tifiers for comp
2eb40 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75  atibility..** Bu
2eb41 74 20 74 68 65 20 66 65 61 74 75 72 65 20 69 73  t the feature is
2eb42 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a   undocumented..*
2eb43 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
2eb44 41 53 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49  ASCII.SQLITE_PRI
2eb45 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
2eb46 73 71 6c 69 74 65 33 49 73 41 73 63 69 69 49 64  sqlite3IsAsciiId
2eb47 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30  Char[] = {./* x0
2eb48 20 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20   x1 x2 x3 x4 x5 
2eb49 78 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78  x6 x7 x8 x9 xA x
2eb4a 42 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f  B xC xD xE xF */
2eb4b 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c  .    0, 0, 0, 0,
2eb4c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 0, 0, 0, 0, 
2eb4d 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2eb4e 2c 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20  , 0,  /* 2x */. 
2eb4f 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31     1, 1, 1, 1, 1
2eb50 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb51 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2eb52 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20  0,  /* 3x */.   
2eb53 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2eb54 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2eb55 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb56 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31    /* 4x */.    1
2eb57 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb58 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2eb59 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20  0, 0, 0, 0, 1,  
2eb5a 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 5x */.    0, 
2eb5b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2eb5c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb5d 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
2eb5e 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c   6x */.    1, 1,
2eb5f 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2eb60 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
2eb61 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37  , 0, 0, 0,  /* 7
2eb62 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  x */.};.#define 
2eb63 49 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d  IdChar(C)  (((c=
2eb64 43 29 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28  C)&0x80)!=0 || (
2eb65 63 3e 30 78 31 66 20 26 26 20 73 71 6c 69 74 65  c>0x1f && sqlite
2eb66 33 49 73 41 73 63 69 69 49 64 43 68 61 72 5b 63  3IsAsciiIdChar[c
2eb67 2d 30 78 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a  -0x20])).#endif.
2eb68 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42  #ifdef SQLITE_EB
2eb69 43 44 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56  CDIC.SQLITE_PRIV
2eb6a 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73  ATE const char s
2eb6b 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64  qlite3IsEbcdicId
2eb6c 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30  Char[] = {./* x0
2eb6d 20 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20   x1 x2 x3 x4 x5 
2eb6e 78 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78  x6 x7 x8 x9 xA x
2eb6f 42 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f  B xC xD xE xF */
2eb70 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c  .    0, 0, 1, 1,
2eb71 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2eb72 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
2eb73 2c 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20  , 0,  /* 4x */. 
2eb74 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
2eb75 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb76 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 1, 0, 0, 0, 
2eb77 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20  0,  /* 5x */.   
2eb78 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 0, 1, 1, 1, 
2eb79 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
2eb7a 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c  , 0, 0, 1, 0, 0,
2eb7b 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 30    /* 6x */.    0
2eb7c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb7d 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
2eb7e 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20  0, 0, 0, 0, 0,  
2eb7f 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 7x */.    0, 
2eb80 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2eb81 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
2eb82 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a   1, 1, 1, 0,  /*
2eb83 20 38 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c   8x */.    0, 1,
2eb84 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2eb85 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31  1, 1, 1, 0, 0, 1
2eb86 2c 20 30 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 39  , 0, 1, 0,  /* 9
2eb87 78 20 2a 2f 0a 20 20 20 20 31 2c 20 30 2c 20 31  x */.    1, 0, 1
2eb88 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb89 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
2eb8a 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 41 78 20  1, 1, 0,  /* Ax 
2eb8b 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  */.    0, 0, 0, 
2eb8c 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2eb8d 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2eb8e 20 30 2c 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f   0, 0,  /* Bx */
2eb8f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c  .    0, 1, 1, 1,
2eb90 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2eb91 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 0, 1, 1, 1, 1
2eb92 2c 20 31 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20  , 1,  /* Cx */. 
2eb93 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
2eb94 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb95 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2eb96 31 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20  1,  /* Dx */.   
2eb97 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 0, 1, 1, 1, 
2eb98 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
2eb99 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb9a 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 31    /* Ex */.    1
2eb9b 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2eb9c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
2eb9d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20  1, 1, 1, 1, 0,  
2eb9e 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66  /* Fx */.};.#def
2eb9f 69 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28  ine IdChar(C)  (
2eba0 28 28 63 3d 43 29 3e 3d 30 78 34 32 20 26 26 20  ((c=C)>=0x42 && 
2eba1 73 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49  sqlite3IsEbcdicI
2eba2 64 43 68 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a  dChar[c-0x40])).
2eba3 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52  #endif.../*.** R
2eba4 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68  eturn the length
2eba5 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 68   of the token th
2eba6 61 74 20 62 65 67 69 6e 73 20 61 74 20 7a 5b 30  at begins at z[0
2eba7 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65  ]. .** Store the
2eba8 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a   token type in *
2eba9 74 6f 6b 65 6e 54 79 70 65 20 62 65 66 6f 72 65  tokenType before
2ebaa 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53   returning..*/.S
2ebab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2ebac 74 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65  t sqlite3GetToke
2ebad 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  n(const unsigned
2ebae 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74   char *z, int *t
2ebaf 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74  okenType){.  int
2ebb0 20 69 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28   i, c;.  switch(
2ebb1 20 2a 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20   *z ){.    case 
2ebb2 27 20 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20  ' ': case '\t': 
2ebb3 63 61 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20  case '\n': case 
2ebb4 27 5c 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a  '\f': case '\r':
2ebb5 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31   {.      for(i=1
2ebb6 3b 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ; sqlite3Isspace
2ebb7 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
2ebb8 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2ebb9 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20  = TK_SPACE;.    
2ebba 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
2ebbb 7d 0a 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20  }.    case '-': 
2ebbc 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d  {.      if( z[1]
2ebbd 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20  =='-' ){.       
2ebbe 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69   for(i=2; (c=z[i
2ebbf 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 6e 27  ])!=0 && c!='\n'
2ebc0 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20  ; i++){}.       
2ebc1 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2ebc2 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20 20 20  _SPACE;.        
2ebc3 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20  return i;.      
2ebc4 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  }.      *tokenTy
2ebc5 70 65 20 3d 20 54 4b 5f 4d 49 4e 55 53 3b 0a 20  pe = TK_MINUS;. 
2ebc6 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2ebc7 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 28     }.    case '(
2ebc8 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
2ebc9 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 50 3b 0a 20  nType = TK_LP;. 
2ebca 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2ebcb 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 29     }.    case ')
2ebcc 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
2ebcd 6e 54 79 70 65 20 3d 20 54 4b 5f 52 50 3b 0a 20  nType = TK_RP;. 
2ebce 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2ebcf 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3b     }.    case ';
2ebd0 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
2ebd1 6e 54 79 70 65 20 3d 20 54 4b 5f 53 45 4d 49 3b  nType = TK_SEMI;
2ebd2 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2ebd3 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2ebd4 27 2b 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '+': {.      *to
2ebd5 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 50 4c 55  kenType = TK_PLU
2ebd6 53 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  S;.      return 
2ebd7 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
2ebd8 65 20 27 2a 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '*': {.      *
2ebd9 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
2ebda 54 41 52 3b 0a 20 20 20 20 20 20 72 65 74 75 72  TAR;.      retur
2ebdb 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
2ebdc 61 73 65 20 27 2f 27 3a 20 7b 0a 20 20 20 20 20  ase '/': {.     
2ebdd 20 69 66 28 20 7a 5b 31 5d 21 3d 27 2a 27 20 7c   if( z[1]!='*' |
2ebde 7c 20 7a 5b 32 5d 3d 3d 30 20 29 7b 0a 20 20 20  | z[2]==0 ){.   
2ebdf 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2ebe0 3d 20 54 4b 5f 53 4c 41 53 48 3b 0a 20 20 20 20  = TK_SLASH;.    
2ebe1 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2ebe2 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28      }.      for(
2ebe3 69 3d 33 2c 20 63 3d 7a 5b 32 5d 3b 20 28 63 21  i=3, c=z[2]; (c!
2ebe4 3d 27 2a 27 20 7c 7c 20 7a 5b 69 5d 21 3d 27 2f  ='*' || z[i]!='/
2ebe5 27 29 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d  ') && (c=z[i])!=
2ebe6 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20  0; i++){}.      
2ebe7 69 66 28 20 63 20 29 20 69 2b 2b 3b 0a 20 20 20  if( c ) i++;.   
2ebe8 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2ebe9 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20  TK_SPACE;.      
2ebea 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
2ebeb 20 20 20 20 63 61 73 65 20 27 25 27 3a 20 7b 0a      case '%': {.
2ebec 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2ebed 20 3d 20 54 4b 5f 52 45 4d 3b 0a 20 20 20 20 20   = TK_REM;.     
2ebee 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2ebef 0a 20 20 20 20 63 61 73 65 20 27 3d 27 3a 20 7b  .    case '=': {
2ebf0 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
2ebf1 65 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20  e = TK_EQ;.     
2ebf2 20 72 65 74 75 72 6e 20 31 20 2b 20 28 7a 5b 31   return 1 + (z[1
2ebf3 5d 3d 3d 27 3d 27 29 3b 0a 20 20 20 20 7d 0a 20  ]=='=');.    }. 
2ebf4 20 20 20 63 61 73 65 20 27 3c 27 3a 20 7b 0a 20     case '<': {. 
2ebf5 20 20 20 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d       if( (c=z[1]
2ebf6 29 3d 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20  )=='=' ){.      
2ebf7 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2ebf8 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 20 20 72 65  K_LE;.        re
2ebf9 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65  turn 2;.      }e
2ebfa 6c 73 65 20 69 66 28 20 63 3d 3d 27 3e 27 20 29  lse if( c=='>' )
2ebfb 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
2ebfc 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20  Type = TK_NE;.  
2ebfd 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
2ebfe 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2ebff 63 3d 3d 27 3c 27 20 29 7b 0a 20 20 20 20 20 20  c=='<' ){.      
2ec00 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2ec01 4b 5f 4c 53 48 49 46 54 3b 0a 20 20 20 20 20 20  K_LSHIFT;.      
2ec02 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
2ec03 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2ec04 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2ec05 5f 4c 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74  _LT;.        ret
2ec06 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
2ec07 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3e     }.    case '>
2ec08 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28  ': {.      if( (
2ec09 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a  c=z[1])=='=' ){.
2ec0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2ec0b 70 65 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20  pe = TK_GE;.    
2ec0c 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
2ec0d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
2ec0e 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='>' ){.        
2ec0f 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2ec10 52 53 48 49 46 54 3b 0a 20 20 20 20 20 20 20 20  RSHIFT;.        
2ec11 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
2ec12 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a  }else{.        *
2ec13 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47  tokenType = TK_G
2ec14 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  T;.        retur
2ec15 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
2ec16 20 7d 0a 20 20 20 20 63 61 73 65 20 27 21 27 3a   }.    case '!':
2ec17 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31   {.      if( z[1
2ec18 5d 21 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20  ]!='=' ){.      
2ec19 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2ec1a 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
2ec1b 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20     return 2;.   
2ec1c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2ec1d 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2ec1e 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20 72 65  K_NE;.        re
2ec1f 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a  turn 2;.      }.
2ec20 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2ec21 7c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  |': {.      if( 
2ec22 7a 5b 31 5d 21 3d 27 7c 27 20 29 7b 0a 20 20 20  z[1]!='|' ){.   
2ec23 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2ec24 3d 20 54 4b 5f 42 49 54 4f 52 3b 0a 20 20 20 20  = TK_BITOR;.    
2ec25 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2ec26 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ec27 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2ec28 54 4b 5f 43 4f 4e 43 41 54 3b 0a 20 20 20 20 20  TK_CONCAT;.     
2ec29 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20     return 2;.   
2ec2a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63     }.    }.    c
2ec2b 61 73 65 20 27 2c 27 3a 20 7b 0a 20 20 20 20 20  ase ',': {.     
2ec2c 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2ec2d 5f 43 4f 4d 4d 41 3b 0a 20 20 20 20 20 20 72 65  _COMMA;.      re
2ec2e 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2ec2f 20 20 63 61 73 65 20 27 26 27 3a 20 7b 0a 20 20    case '&': {.  
2ec30 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2ec31 20 54 4b 5f 42 49 54 41 4e 44 3b 0a 20 20 20 20   TK_BITAND;.    
2ec32 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2ec33 7d 0a 20 20 20 20 63 61 73 65 20 27 7e 27 3a 20  }.    case '~': 
2ec34 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
2ec35 70 65 20 3d 20 54 4b 5f 42 49 54 4e 4f 54 3b 0a  pe = TK_BITNOT;.
2ec36 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2ec37 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2ec38 60 27 3a 0a 20 20 20 20 63 61 73 65 20 27 5c 27  `':.    case '\'
2ec39 27 3a 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a  ':.    case '"':
2ec3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 6c   {.      int del
2ec3b 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20 20  im = z[0];.     
2ec3c 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69   for(i=1; (c=z[i
2ec3d 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  ])!=0; i++){.   
2ec3e 20 20 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69       if( c==deli
2ec3f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  m ){.          i
2ec40 66 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d  f( z[i+1]==delim
2ec41 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2ec42 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  i++;.          }
2ec43 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2ec44 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
2ec45 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
2ec46 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2ec47 20 63 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20   c=='\'' ){.    
2ec48 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2ec49 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20   TK_STRING;.    
2ec4a 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a      return i+1;.
2ec4b 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2ec4c 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c!=0 ){.        
2ec4d 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2ec4e 49 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  ID;.        retu
2ec4f 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65  rn i+1;.      }e
2ec50 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  lse{.        *to
2ec51 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c  kenType = TK_ILL
2ec52 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65  EGAL;.        re
2ec53 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a  turn i;.      }.
2ec54 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2ec55 2e 27 3a 20 7b 0a 23 69 66 6e 64 65 66 20 53 51  .': {.#ifndef SQ
2ec56 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
2ec57 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69  NG_POINT.      i
2ec58 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67  f( !sqlite3Isdig
2ec59 69 74 28 7a 5b 31 5d 29 20 29 0a 23 65 6e 64 69  it(z[1]) ).#endi
2ec5a 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  f.      {.      
2ec5b 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2ec5c 4b 5f 44 4f 54 3b 0a 20 20 20 20 20 20 20 20 72  K_DOT;.        r
2ec5d 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
2ec5e 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
2ec5f 20 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20   next character 
2ec60 69 73 20 61 20 64 69 67 69 74 2c 20 74 68 69 73  is a digit, this
2ec61 20 69 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70   is a floating p
2ec62 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6e 75  oint.      ** nu
2ec63 6d 62 65 72 20 74 68 61 74 20 62 65 67 69 6e 73  mber that begins
2ec64 20 77 69 74 68 20 22 2e 22 2e 20 20 46 61 6c 6c   with ".".  Fall
2ec65 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e   thru into the n
2ec66 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20  ext case */.    
2ec67 7d 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 20  }.    case '0': 
2ec68 63 61 73 65 20 27 31 27 3a 20 63 61 73 65 20 27  case '1': case '
2ec69 32 27 3a 20 63 61 73 65 20 27 33 27 3a 20 63 61  2': case '3': ca
2ec6a 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65  se '4':.    case
2ec6b 20 27 35 27 3a 20 63 61 73 65 20 27 36 27 3a 20   '5': case '6': 
2ec6c 63 61 73 65 20 27 37 27 3a 20 63 61 73 65 20 27  case '7': case '
2ec6d 38 27 3a 20 63 61 73 65 20 27 39 27 3a 20 7b 0a  8': case '9': {.
2ec6e 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2ec6f 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20   = TK_INTEGER;. 
2ec70 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71       for(i=0; sq
2ec71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69  lite3Isdigit(z[i
2ec72 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69 66 6e 64  ]); i++){}.#ifnd
2ec73 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
2ec74 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
2ec75 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2e      if( z[i]=='.
2ec76 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 2b 2b  ' ){.        i++
2ec77 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
2ec78 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
2ec79 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d 0a  z[i]) ){ i++; }.
2ec7a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2ec7b 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a 20  pe = TK_FLOAT;. 
2ec7c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2ec7d 20 28 7a 5b 69 5d 3d 3d 27 65 27 20 7c 7c 20 7a   (z[i]=='e' || z
2ec7e 5b 69 5d 3d 3d 27 45 27 29 20 26 26 0a 20 20 20  [i]=='E') &&.   
2ec7f 20 20 20 20 20 20 20 20 28 20 73 71 6c 69 74 65          ( sqlite
2ec80 33 49 73 64 69 67 69 74 28 7a 5b 69 2b 31 5d 29  3Isdigit(z[i+1])
2ec81 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c   .            ||
2ec82 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b 27 20 7c   ((z[i+1]=='+' |
2ec83 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27 29 20 26  | z[i+1]=='-') &
2ec84 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  & sqlite3Isdigit
2ec85 28 7a 5b 69 2b 32 5d 29 29 0a 20 20 20 20 20 20  (z[i+2])).      
2ec86 20 20 20 20 20 29 0a 20 20 20 20 20 20 29 7b 0a       ).      ){.
2ec87 20 20 20 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a          i += 2;.
2ec88 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 73          while( s
2ec89 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b  qlite3Isdigit(z[
2ec8a 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20  i]) ){ i++; }.  
2ec8b 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2ec8c 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a 20 20 20   = TK_FLOAT;.   
2ec8d 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
2ec8e 20 20 77 68 69 6c 65 28 20 49 64 43 68 61 72 28    while( IdChar(
2ec8f 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20  z[i]) ){.       
2ec90 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2ec91 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20  _ILLEGAL;.      
2ec92 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20    i++;.      }. 
2ec93 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
2ec94 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 5b     }.    case '[
2ec95 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ': {.      for(i
2ec96 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27  =1, c=z[0]; c!='
2ec97 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d  ]' && (c=z[i])!=
2ec98 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20  0; i++){}.      
2ec99 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 63 3d 3d  *tokenType = c==
2ec9a 27 5d 27 20 3f 20 54 4b 5f 49 44 20 3a 20 54 4b  ']' ? TK_ID : TK
2ec9b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20  _ILLEGAL;.      
2ec9c 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
2ec9d 20 20 20 20 63 61 73 65 20 27 3f 27 3a 20 7b 0a      case '?': {.
2ec9e 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2ec9f 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a   = TK_VARIABLE;.
2eca0 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 73        for(i=1; s
2eca1 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b  qlite3Isdigit(z[
2eca2 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  i]); i++){}.    
2eca3 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
2eca4 7d 0a 20 20 20 20 63 61 73 65 20 27 23 27 3a 20  }.    case '#': 
2eca5 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  {.      for(i=1;
2eca6 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
2eca7 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20  z[i]); i++){}.  
2eca8 20 20 20 20 69 66 28 20 69 3e 31 20 29 7b 0a 20      if( i>1 ){. 
2eca9 20 20 20 20 20 20 20 2f 2a 20 50 61 72 61 6d 65         /* Parame
2ecaa 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ters of the form
2ecab 20 23 4e 4e 4e 20 28 77 68 65 72 65 20 4e 4e 4e   #NNN (where NNN
2ecac 20 69 73 20 61 20 6e 75 6d 62 65 72 29 20 61 72   is a number) ar
2ecad 65 20 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a  e used.        *
2ecae 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20  * internally by 
2ecaf 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2ecb0 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  se.  */.        
2ecb1 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2ecb2 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20  REGISTER;.      
2ecb3 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
2ecb4 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c    }.      /* Fal
2ecb5 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74  l through into t
2ecb6 68 65 20 6e 65 78 74 20 63 61 73 65 20 69 66 20  he next case if 
2ecb7 74 68 65 20 27 23 27 20 69 73 20 6e 6f 74 20 66  the '#' is not f
2ecb8 6f 6c 6c 6f 77 65 64 20 62 79 0a 20 20 20 20 20  ollowed by.     
2ecb9 20 2a 2a 20 61 20 64 69 67 69 74 2e 20 54 72 79   ** a digit. Try
2ecba 20 74 6f 20 6d 61 74 63 68 20 23 41 41 41 41 20   to match #AAAA 
2ecbb 77 68 65 72 65 20 41 41 41 41 20 69 73 20 61 20  where AAAA is a 
2ecbc 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 2e 20  parameter name. 
2ecbd 2a 2f 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  */.    }.#ifndef
2ecbe 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c   SQLITE_OMIT_TCL
2ecbf 5f 56 41 52 49 41 42 4c 45 0a 20 20 20 20 63 61  _VARIABLE.    ca
2ecc0 73 65 20 27 24 27 3a 0a 23 65 6e 64 69 66 0a 20  se '$':.#endif. 
2ecc1 20 20 20 63 61 73 65 20 27 40 27 3a 20 20 2f 2a     case '@':  /*
2ecc2 20 46 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69   For compatibili
2ecc3 74 79 20 77 69 74 68 20 4d 53 20 53 51 4c 20 53  ty with MS SQL S
2ecc4 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 63 61 73  erver */.    cas
2ecc5 65 20 27 3a 27 3a 20 7b 0a 20 20 20 20 20 20 69  e ':': {.      i
2ecc6 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  nt n = 0;.      
2ecc7 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2ecc8 56 41 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20  VARIABLE;.      
2ecc9 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d  for(i=1; (c=z[i]
2ecca 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
2eccb 20 20 20 20 69 66 28 20 49 64 43 68 61 72 28 63      if( IdChar(c
2eccc 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
2eccd 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ++;.#ifndef SQLI
2ecce 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49  TE_OMIT_TCL_VARI
2eccf 41 42 4c 45 0a 20 20 20 20 20 20 20 20 7d 65 6c  ABLE.        }el
2ecd0 73 65 20 69 66 28 20 63 3d 3d 27 28 27 20 26 26  se if( c=='(' &&
2ecd1 20 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20   n>0 ){.        
2ecd2 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20    do{.          
2ecd3 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
2ecd4 20 7d 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d   }while( (c=z[i]
2ecd5 29 21 3d 30 20 26 26 20 21 73 71 6c 69 74 65 33  )!=0 && !sqlite3
2ecd6 49 73 73 70 61 63 65 28 63 29 20 26 26 20 63 21  Isspace(c) && c!
2ecd7 3d 27 29 27 20 29 3b 0a 20 20 20 20 20 20 20 20  =')' );.        
2ecd8 20 20 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a    if( c==')' ){.
2ecd9 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
2ecda 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
2ecdb 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 74  {.            *t
2ecdc 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c  okenType = TK_IL
2ecdd 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20  LEGAL;.         
2ecde 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65   }.          bre
2ecdf 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
2ece0 65 20 69 66 28 20 63 3d 3d 27 3a 27 20 26 26 20  e if( c==':' && 
2ece1 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20  z[i+1]==':' ){. 
2ece2 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65           i++;.#e
2ece3 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7d 65 6c  ndif.        }el
2ece4 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72  se{.          br
2ece5 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
2ece6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2ece7 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79   n==0 ) *tokenTy
2ece8 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
2ece9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
2ecea 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
2eceb 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
2ecec 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65  LITERAL.    case
2eced 20 27 78 27 3a 20 63 61 73 65 20 27 58 27 3a 20   'x': case 'X': 
2ecee 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d  {.      if( z[1]
2ecef 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20  =='\'' ){.      
2ecf0 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2ecf1 4b 5f 42 4c 4f 42 3b 0a 20 20 20 20 20 20 20 20  K_BLOB;.        
2ecf2 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69 5d  for(i=2; (c=z[i]
2ecf3 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 27 27 3b  )!=0 && c!='\'';
2ecf4 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
2ecf5 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 78   if( !sqlite3Isx
2ecf6 64 69 67 69 74 28 63 29 20 29 7b 0a 20 20 20 20  digit(c) ){.    
2ecf7 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2ecf8 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
2ecf9 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2ecfa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
2ecfb 66 28 20 69 25 32 20 7c 7c 20 21 63 20 29 20 2a  f( i%2 || !c ) *
2ecfc 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2ecfd 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20  LLEGAL;.        
2ecfe 69 66 28 20 63 20 29 20 69 2b 2b 3b 0a 20 20 20  if( c ) i++;.   
2ecff 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
2ed00 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
2ed01 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74  Otherwise fall t
2ed02 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
2ed03 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d  xt case */.    }
2ed04 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 65 66 61  .#endif.    defa
2ed05 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  ult: {.      if(
2ed06 20 21 49 64 43 68 61 72 28 2a 7a 29 20 29 7b 0a   !IdChar(*z) ){.
2ed07 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2ed08 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
2ed09 28 69 3d 31 3b 20 49 64 43 68 61 72 28 7a 5b 69  (i=1; IdChar(z[i
2ed0a 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  ]); i++){}.     
2ed0b 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 6b 65   *tokenType = ke
2ed0c 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a  ywordCode((char*
2ed0d 29 7a 2c 20 69 29 3b 0a 20 20 20 20 20 20 72 65  )z, i);.      re
2ed0e 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
2ed0f 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d  }.  *tokenType =
2ed10 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 72   TK_ILLEGAL;.  r
2ed11 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
2ed12 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65 72  * Run the parser
2ed13 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 53 51   on the given SQ
2ed14 4c 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 70  L string.  The p
2ed15 61 72 73 65 72 20 73 74 72 75 63 74 75 72 65 20  arser structure 
2ed16 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e 2e  is.** passed in.
2ed17 20 20 41 6e 20 53 51 4c 49 54 45 5f 20 73 74 61    An SQLITE_ sta
2ed18 74 75 73 20 63 6f 64 65 20 69 73 20 72 65 74 75  tus code is retu
2ed19 72 6e 65 64 2e 20 20 49 66 20 61 6e 20 65 72 72  rned.  If an err
2ed1a 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 74 68 65  or occurs.** the
2ed1b 6e 20 61 6e 20 61 6e 64 20 61 74 74 65 6d 70 74  n an and attempt
2ed1c 20 69 73 20 6d 61 64 65 20 74 6f 20 77 72 69 74   is made to writ
2ed1d 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
2ed1e 67 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f  ge into .** memo
2ed1f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
2ed20 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
2ed21 29 20 61 6e 64 20 74 6f 20 6d 61 6b 65 20 2a 70  ) and to make *p
2ed22 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20 74 6f  zErrMsg point to
2ed23 20 74 68 61 74 0a 2a 2a 20 65 72 72 6f 72 20 6d   that.** error m
2ed24 65 73 73 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54  essage..*/.SQLIT
2ed25 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2ed26 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 50  lite3RunParser(P
2ed27 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
2ed28 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
2ed29 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29  char **pzErrMsg)
2ed2a 7b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30  {.  int nErr = 0
2ed2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ed2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2ed2d 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65   errors encounte
2ed2e 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  red */.  int i; 
2ed2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed30 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2ed31 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 76 6f   counter */.  vo
2ed32 69 64 20 2a 70 45 6e 67 69 6e 65 3b 20 20 20 20  id *pEngine;    
2ed33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ed34 20 54 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72   The LEMON-gener
2ed35 61 74 65 64 20 4c 41 4c 52 28 31 29 20 70 61 72  ated LALR(1) par
2ed36 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 6b  ser */.  int tok
2ed37 65 6e 54 79 70 65 3b 20 20 20 20 20 20 20 20 20  enType;         
2ed38 20 20 20 20 20 20 20 20 20 2f 2a 20 74 79 70 65           /* type
2ed39 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74 6f 6b   of the next tok
2ed3a 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73 74  en */.  int last
2ed3b 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20 2d 31  TokenParsed = -1
2ed3c 3b 20 20 20 20 20 20 20 2f 2a 20 74 79 70 65 20  ;       /* type 
2ed3d 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  of the previous 
2ed3e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 75 38 20 65 6e  token */.  u8 en
2ed3f 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 20 20  ableLookaside;  
2ed40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 61             /* Sa
2ed41 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d  ved value of db-
2ed42 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
2ed43 6c 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  led */.  sqlite3
2ed44 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2ed45 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  b;       /* The 
2ed46 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2ed47 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53  ion */.  int mxS
2ed48 71 6c 4c 65 6e 3b 20 20 20 20 20 20 20 20 20 20  qlLen;          
2ed49 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20           /* Max 
2ed4a 6c 65 6e 67 74 68 20 6f 66 20 61 6e 20 53 51 4c  length of an SQL
2ed4b 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 0a 20 20 6d   string */...  m
2ed4c 78 53 71 6c 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c  xSqlLen = db->aL
2ed4d 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
2ed4e 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20  T_SQL_LENGTH];. 
2ed4f 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56   if( db->activeV
2ed50 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  dbeCnt==0 ){.   
2ed51 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
2ed52 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20  upted = 0;.  }. 
2ed53 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
2ed54 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 72 73  LITE_OK;.  pPars
2ed55 65 2d 3e 7a 54 61 69 6c 20 3d 20 70 50 61 72 73  e->zTail = pPars
2ed56 65 2d 3e 7a 53 71 6c 20 3d 20 7a 53 71 6c 3b 0a  e->zSql = zSql;.
2ed57 20 20 69 20 3d 20 30 3b 0a 20 20 61 73 73 65 72    i = 0;.  asser
2ed58 74 28 20 70 7a 45 72 72 4d 73 67 21 3d 30 20 29  t( pzErrMsg!=0 )
2ed59 3b 0a 20 20 70 45 6e 67 69 6e 65 20 3d 20 73 71  ;.  pEngine = sq
2ed5a 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63  lite3ParserAlloc
2ed5b 28 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f  ((void*(*)(size_
2ed5c 74 29 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  t))sqlite3Malloc
2ed5d 29 3b 0a 20 20 69 66 28 20 70 45 6e 67 69 6e 65  );.  if( pEngine
2ed5e 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  ==0 ){.    db->m
2ed5f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2ed60 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2ed61 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
2ed62 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2ed63 73 4c 61 73 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d  sLastToken.dyn==
2ed64 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2ed65 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2ed66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
2ed67 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
2ed68 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73  gger==0 );.  ass
2ed69 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61  ert( pParse->nVa
2ed6a 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  r==0 );.  assert
2ed6b 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78  ( pParse->nVarEx
2ed6c 70 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pr==0 );.  asser
2ed6d 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  t( pParse->nVarE
2ed6e 78 70 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20  xprAlloc==0 );. 
2ed6f 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
2ed70 3e 61 70 56 61 72 45 78 70 72 3d 3d 30 20 29 3b  >apVarExpr==0 );
2ed71 0a 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69  .  enableLookasi
2ed72 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  de = db->lookasi
2ed73 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 69  de.bEnabled;.  i
2ed74 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  f( db->lookaside
2ed75 2e 70 53 74 61 72 74 20 29 20 64 62 2d 3e 6c 6f  .pStart ) db->lo
2ed76 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
2ed77 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 21   = 1;.  while( !
2ed78 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2ed79 20 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30 20 29   && zSql[i]!=0 )
2ed7a 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e  {.    assert( i>
2ed7b 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65  =0 );.    pParse
2ed7c 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20 3d  ->sLastToken.z =
2ed7d 20 28 75 38 2a 29 26 7a 53 71 6c 5b 69 5d 3b 0a   (u8*)&zSql[i];.
2ed7e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
2ed7f 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 64  se->sLastToken.d
2ed80 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61  yn==0 );.    pPa
2ed81 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e  rse->sLastToken.
2ed82 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f  n = sqlite3GetTo
2ed83 6b 65 6e 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ken((unsigned ch
2ed84 61 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c 26 74 6f  ar*)&zSql[i],&to
2ed85 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20 20 69 20  kenType);.    i 
2ed86 2b 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  += pParse->sLast
2ed87 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 69 66 28  Token.n;.    if(
2ed88 20 69 3e 6d 78 53 71 6c 4c 65 6e 20 29 7b 0a 20   i>mxSqlLen ){. 
2ed89 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20       pParse->rc 
2ed8a 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b  = SQLITE_TOOBIG;
2ed8b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2ed8c 20 20 7d 0a 20 20 20 20 73 77 69 74 63 68 28 20    }.    switch( 
2ed8d 74 6f 6b 65 6e 54 79 70 65 20 29 7b 0a 20 20 20  tokenType ){.   
2ed8e 20 20 20 63 61 73 65 20 54 4b 5f 53 50 41 43 45     case TK_SPACE
2ed8f 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  : {.        if( 
2ed90 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
2ed91 70 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  pted ){.        
2ed92 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
2ed93 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
2ed94 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2ed95 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72  e3SetString(pzEr
2ed96 72 4d 73 67 2c 20 64 62 2c 20 22 69 6e 74 65 72  rMsg, db, "inter
2ed97 72 75 70 74 22 29 3b 0a 20 20 20 20 20 20 20 20  rupt");.        
2ed98 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72    goto abort_par
2ed99 73 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  se;.        }.  
2ed9a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ed9b 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
2ed9c 54 4b 5f 49 4c 4c 45 47 41 4c 3a 20 7b 0a 20 20  TK_ILLEGAL: {.  
2ed9d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2ed9e 72 65 65 28 64 62 2c 20 2a 70 7a 45 72 72 4d 73  ree(db, *pzErrMs
2ed9f 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45  g);.        *pzE
2eda0 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d  rrMsg = sqlite3M
2eda1 50 72 69 6e 74 66 28 64 62 2c 20 22 75 6e 72 65  Printf(db, "unre
2eda2 63 6f 67 6e 69 7a 65 64 20 74 6f 6b 65 6e 3a 20  cognized token: 
2eda3 5c 22 25 54 5c 22 22 2c 0a 20 20 20 20 20 20 20  \"%T\"",.       
2eda4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eda5 20 26 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54   &pParse->sLastT
2eda6 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e  oken);.        n
2eda7 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 67  Err++;.        g
2eda8 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b  oto abort_parse;
2eda9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
2edaa 61 73 65 20 54 4b 5f 53 45 4d 49 3a 20 7b 0a 20  ase TK_SEMI: {. 
2edab 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a         pParse->z
2edac 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b  Tail = &zSql[i];
2edad 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c  .        /* Fall
2edae 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 64   thru into the d
2edaf 65 66 61 75 6c 74 20 63 61 73 65 20 2a 2f 0a 20  efault case */. 
2edb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66       }.      def
2edb1 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20  ault: {.        
2edb2 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 70 45  sqlite3Parser(pE
2edb3 6e 67 69 6e 65 2c 20 74 6f 6b 65 6e 54 79 70 65  ngine, tokenType
2edb4 2c 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54  , pParse->sLastT
2edb5 6f 6b 65 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20  oken, pParse);. 
2edb6 20 20 20 20 20 20 20 6c 61 73 74 54 6f 6b 65 6e         lastToken
2edb7 50 61 72 73 65 64 20 3d 20 74 6f 6b 65 6e 54 79  Parsed = tokenTy
2edb8 70 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  pe;.        if( 
2edb9 70 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49  pParse->rc!=SQLI
2edba 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
2edbb 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61     goto abort_pa
2edbc 72 73 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rse;.        }. 
2edbd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2edbe 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2edbf 61 62 6f 72 74 5f 70 61 72 73 65 3a 0a 20 20 69  abort_parse:.  i
2edc0 66 28 20 7a 53 71 6c 5b 69 5d 3d 3d 30 20 26 26  f( zSql[i]==0 &&
2edc1 20 6e 45 72 72 3d 3d 30 20 26 26 20 70 50 61 72   nErr==0 && pPar
2edc2 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  se->rc==SQLITE_O
2edc3 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 61 73  K ){.    if( las
2edc4 74 54 6f 6b 65 6e 50 61 72 73 65 64 21 3d 54 4b  tTokenParsed!=TK
2edc5 5f 53 45 4d 49 20 29 7b 0a 20 20 20 20 20 20 73  _SEMI ){.      s
2edc6 71 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e  qlite3Parser(pEn
2edc7 67 69 6e 65 2c 20 54 4b 5f 53 45 4d 49 2c 20 70  gine, TK_SEMI, p
2edc8 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
2edc9 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20  n, pParse);.    
2edca 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20    pParse->zTail 
2edcb 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20  = &zSql[i];.    
2edcc 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72  }.    sqlite3Par
2edcd 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 30 2c 20  ser(pEngine, 0, 
2edce 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b  pParse->sLastTok
2edcf 65 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 7d  en, pParse);.  }
2edd0 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d  .#ifdef YYTRACKM
2edd1 41 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 73  AXSTACKDEPTH.  s
2edd2 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28  qlite3StatusSet(
2edd3 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
2edd4 52 53 45 52 5f 53 54 41 43 4b 2c 0a 20 20 20 20  RSER_STACK,.    
2edd5 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 53    sqlite3ParserS
2edd6 74 61 63 6b 50 65 61 6b 28 70 45 6e 67 69 6e 65  tackPeak(pEngine
2edd7 29 0a 20 20 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  ).  );.#endif /*
2edd8 20 59 59 44 45 42 55 47 20 2a 2f 0a 20 20 73 71   YYDEBUG */.  sq
2edd9 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28  lite3ParserFree(
2edda 70 45 6e 67 69 6e 65 2c 20 73 71 6c 69 74 65 33  pEngine, sqlite3
2eddb 5f 66 72 65 65 29 3b 0a 20 20 64 62 2d 3e 6c 6f  _free);.  db->lo
2eddc 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
2eddd 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69   = enableLookasi
2edde 64 65 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  de;.  if( db->ma
2eddf 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2ede0 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
2ede1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
2ede2 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72  .  if( pParse->r
2ede3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
2ede4 70 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49  pParse->rc!=SQLI
2ede5 54 45 5f 44 4f 4e 45 20 26 26 20 70 50 61 72 73  TE_DONE && pPars
2ede6 65 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b  e->zErrMsg==0 ){
2ede7 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
2ede8 74 72 69 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a  tring(&pParse->z
2ede9 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22  ErrMsg, db, "%s"
2edea 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  , sqlite3ErrStr(
2edeb 70 50 61 72 73 65 2d 3e 72 63 29 29 3b 0a 20 20  pParse->rc));.  
2edec 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
2eded 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 69  zErrMsg ){.    i
2edee 66 28 20 2a 70 7a 45 72 72 4d 73 67 3d 3d 30 20  f( *pzErrMsg==0 
2edef 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  ){.      *pzErrM
2edf0 73 67 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72  sg = pParse->zEr
2edf1 72 4d 73 67 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rMsg;.    }else{
2edf2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2edf3 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d  Free(db, pParse-
2edf4 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d  >zErrMsg);.    }
2edf5 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45 72  .    pParse->zEr
2edf6 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 6e 45  rMsg = 0;.    nE
2edf7 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rr++;.  }.  if( 
2edf8 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 26 26  pParse->pVdbe &&
2edf9 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20   pParse->nErr>0 
2edfa 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  && pParse->neste
2edfb 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  d==0 ){.    sqli
2edfc 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70 50  te3VdbeDelete(pP
2edfd 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20  arse->pVdbe);.  
2edfe 20 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20    pParse->pVdbe 
2edff 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  = 0;.  }.#ifndef
2ee00 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
2ee01 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20  RED_CACHE.  if( 
2ee02 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
2ee03 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
2ee04 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
2ee05 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 29 3b 0a  e->aTableLock);.
2ee06 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 61 62      pParse->aTab
2ee07 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20  leLock = 0;.    
2ee08 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
2ee09 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64  ck = 0;.  }.#end
2ee0a 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
2ee0b 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2ee0c 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 44 62 46  BLE.  sqlite3DbF
2ee0d 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ree(db, pParse->
2ee0e 61 70 56 74 61 62 4c 6f 63 6b 29 3b 0a 23 65 6e  apVtabLock);.#en
2ee0f 64 69 66 0a 0a 20 20 69 66 28 20 21 49 4e 5f 44  dif..  if( !IN_D
2ee10 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20  ECLARE_VTAB ){. 
2ee11 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 50 61     /* If the pPa
2ee12 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 62  rse->declareVtab
2ee13 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 64 6f   flag is set, do
2ee14 20 6e 6f 74 20 64 65 6c 65 74 65 20 61 6e 79 20   not delete any 
2ee15 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 73 74  table .    ** st
2ee16 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75 70  ructure built up
2ee17 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77   in pParse->pNew
2ee18 54 61 62 6c 65 2e 20 54 68 65 20 63 61 6c 6c 69  Table. The calli
2ee19 6e 67 20 63 6f 64 65 20 28 73 65 65 20 76 74 61  ng code (see vta
2ee1a 62 2e 63 29 0a 20 20 20 20 2a 2a 20 77 69 6c 6c  b.c).    ** will
2ee1b 20 74 61 6b 65 20 72 65 73 70 6f 6e 73 69 62 69   take responsibi
2ee1c 6c 69 74 79 20 66 6f 72 20 66 72 65 65 69 6e 67  lity for freeing
2ee1d 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
2ee1e 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ture..    */.   
2ee1f 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
2ee20 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e 65 77  ble(pParse->pNew
2ee21 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20 73  Table);.  }..  s
2ee22 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
2ee23 67 65 72 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ger(db, pParse->
2ee24 70 4e 65 77 54 72 69 67 67 65 72 29 3b 0a 20 20  pNewTrigger);.  
2ee25 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2ee26 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45  , pParse->apVarE
2ee27 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  xpr);.  sqlite3D
2ee28 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  bFree(db, pParse
2ee29 2d 3e 61 41 6c 69 61 73 29 3b 0a 20 20 77 68 69  ->aAlias);.  whi
2ee2a 6c 65 28 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d  le( pParse->pZom
2ee2b 62 69 65 54 61 62 20 29 7b 0a 20 20 20 20 54 61  bieTab ){.    Ta
2ee2c 62 6c 65 20 2a 70 20 3d 20 70 50 61 72 73 65 2d  ble *p = pParse-
2ee2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a 20 20 20  >pZombieTab;.   
2ee2e 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65   pParse->pZombie
2ee2f 54 61 62 20 3d 20 70 2d 3e 70 4e 65 78 74 5a 6f  Tab = p->pNextZo
2ee30 6d 62 69 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  mbie;.    sqlite
2ee31 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 29 3b  3DeleteTable(p);
2ee32 0a 20 20 7d 0a 20 20 69 66 28 20 6e 45 72 72 3e  .  }.  if( nErr>
2ee33 30 20 26 26 20 28 70 50 61 72 73 65 2d 3e 72 63  0 && (pParse->rc
2ee34 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
2ee35 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
2ee36 45 5f 44 4f 4e 45 29 20 29 7b 0a 20 20 20 20 70  E_DONE) ){.    p
2ee37 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
2ee38 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
2ee39 72 65 74 75 72 6e 20 6e 45 72 72 3b 0a 7d 0a 0a  return nErr;.}..
2ee3a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2ee3b 45 6e 64 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e  End of tokenize.
2ee3c 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2ee3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ee3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2ee3f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2ee40 42 65 67 69 6e 20 66 69 6c 65 20 63 6f 6d 70 6c  Begin file compl
2ee41 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
2ee42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ee43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2ee44 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
2ee45 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
2ee46 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2ee47 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2ee48 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2ee49 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2ee4a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2ee4b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2ee4c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2ee4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2ee4e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2ee4f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2ee50 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2ee51 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2ee52 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2ee53 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2ee54 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2ee55 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2ee56 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2ee57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ee58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ee59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ee5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ee5b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b  ******.** An tok
2ee5c 65 6e 69 7a 65 72 20 66 6f 72 20 53 51 4c 0a 2a  enizer for SQL.*
2ee5d 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
2ee5e 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 74  ontains C code t
2ee5f 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
2ee60 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  he sqlite3_compl
2ee61 65 74 65 28 29 20 41 50 49 2e 0a 2a 2a 20 54 68  ete() API..** Th
2ee62 69 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  is code used to 
2ee63 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 74  be part of the t
2ee64 6f 6b 65 6e 69 7a 65 72 2e 63 20 73 6f 75 72 63  okenizer.c sourc
2ee65 65 20 66 69 6c 65 2e 20 20 42 75 74 20 62 79 0a  e file.  But by.
2ee66 2a 2a 20 73 65 70 61 72 61 74 69 6e 67 20 69 74  ** separating it
2ee67 20 6f 75 74 2c 20 74 68 65 20 63 6f 64 65 20 77   out, the code w
2ee68 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63  ill be automatic
2ee69 61 6c 6c 79 20 6f 6d 69 74 74 65 64 20 66 72 6f  ally omitted fro
2ee6a 6d 0a 2a 2a 20 73 74 61 74 69 63 20 6c 69 6e 6b  m.** static link
2ee6b 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 75 73  s that do not us
2ee6c 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  e it..**.** $Id:
2ee6d 20 63 6f 6d 70 6c 65 74 65 2e 63 2c 76 20 31 2e   complete.c,v 1.
2ee6e 37 20 32 30 30 38 2f 30 36 2f 31 33 20 31 38 3a  7 2008/06/13 18:
2ee6f 32 34 3a 32 37 20 64 72 68 20 45 78 70 20 24 0a  24:27 drh Exp $.
2ee70 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2ee71 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a  E_OMIT_COMPLETE.
2ee72 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 64  ./*.** This is d
2ee73 65 66 69 6e 65 64 20 69 6e 20 74 6f 6b 65 6e 69  efined in tokeni
2ee74 7a 65 2e 63 2e 20 20 57 65 20 6a 75 73 74 20 68  ze.c.  We just h
2ee75 61 76 65 20 74 6f 20 69 6d 70 6f 72 74 20 74 68  ave to import th
2ee76 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f  e definition..*/
2ee77 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ee78 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 23 69 66  AMALGAMATION.#if
2ee79 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49  def SQLITE_ASCII
2ee7a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ee7b 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
2ee7c 65 33 49 73 41 73 63 69 69 49 64 43 68 61 72 5b  e3IsAsciiIdChar[
2ee7d 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61  ];.#define IdCha
2ee7e 72 28 43 29 20 20 28 28 28 63 3d 43 29 26 30 78  r(C)  (((c=C)&0x
2ee7f 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31  80)!=0 || (c>0x1
2ee80 66 20 26 26 20 73 71 6c 69 74 65 33 49 73 41 73  f && sqlite3IsAs
2ee81 63 69 69 49 64 43 68 61 72 5b 63 2d 30 78 32 30  ciiIdChar[c-0x20
2ee82 5d 29 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  ])).#endif.#ifde
2ee83 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a  f SQLITE_EBCDIC.
2ee84 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
2ee85 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65  onst char sqlite
2ee86 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72 5b  3IsEbcdicIdChar[
2ee87 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61  ];.#define IdCha
2ee88 72 28 43 29 20 20 28 28 28 63 3d 43 29 3e 3d 30  r(C)  (((c=C)>=0
2ee89 78 34 32 20 26 26 20 73 71 6c 69 74 65 33 49 73  x42 && sqlite3Is
2ee8a 45 62 63 64 69 63 49 64 43 68 61 72 5b 63 2d 30  EbcdicIdChar[c-0
2ee8b 78 34 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 65  x40])).#endif.#e
2ee8c 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41  ndif /* SQLITE_A
2ee8d 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a  MALGAMATION */..
2ee8e 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79 70  ./*.** Token typ
2ee8f 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73  es used by the s
2ee90 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
2ee91 29 20 72 6f 75 74 69 6e 65 2e 20 20 53 65 65 20  ) routine.  See 
2ee92 74 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 63 6f  the header.** co
2ee93 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 61 74 20 70  mments on that p
2ee94 72 6f 63 65 64 75 72 65 20 66 6f 72 20 61 64 64  rocedure for add
2ee95 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2ee96 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ion..*/.#define 
2ee97 74 6b 53 45 4d 49 20 20 20 20 30 0a 23 64 65 66  tkSEMI    0.#def
2ee98 69 6e 65 20 74 6b 57 53 20 20 20 20 20 20 31 0a  ine tkWS      1.
2ee99 23 64 65 66 69 6e 65 20 74 6b 4f 54 48 45 52 20  #define tkOTHER 
2ee9a 20 20 32 0a 23 64 65 66 69 6e 65 20 74 6b 45 58    2.#define tkEX
2ee9b 50 4c 41 49 4e 20 33 0a 23 64 65 66 69 6e 65 20  PLAIN 3.#define 
2ee9c 74 6b 43 52 45 41 54 45 20 20 34 0a 23 64 65 66  tkCREATE  4.#def
2ee9d 69 6e 65 20 74 6b 54 45 4d 50 20 20 20 20 35 0a  ine tkTEMP    5.
2ee9e 23 64 65 66 69 6e 65 20 74 6b 54 52 49 47 47 45  #define tkTRIGGE
2ee9f 52 20 36 0a 23 64 65 66 69 6e 65 20 74 6b 45 4e  R 6.#define tkEN
2eea0 44 20 20 20 20 20 37 0a 0a 2f 2a 0a 2a 2a 20 52  D     7../*.** R
2eea1 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
2eea2 65 20 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69  e given SQL stri
2eea3 6e 67 20 65 6e 64 73 20 69 6e 20 61 20 73 65 6d  ng ends in a sem
2eea4 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 70  icolon..**.** Sp
2eea5 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 20 69  ecial handling i
2eea6 73 20 72 65 71 75 69 72 65 20 66 6f 72 20 43 52  s require for CR
2eea7 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61  EATE TRIGGER sta
2eea8 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 57 68 65 6e  tements..** When
2eea9 65 76 65 72 20 74 68 65 20 43 52 45 41 54 45 20  ever the CREATE 
2eeaa 54 52 49 47 47 45 52 20 6b 65 79 77 6f 72 64 73  TRIGGER keywords
2eeab 20 61 72 65 20 73 65 65 6e 2c 20 74 68 65 20 73   are seen, the s
2eeac 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6d 75 73 74  tatement.** must
2eead 20 65 6e 64 20 77 69 74 68 20 22 3b 45 4e 44 3b   end with ";END;
2eeae 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d  "..**.** This im
2eeaf 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65  plementation use
2eeb0 73 20 61 20 73 74 61 74 65 20 6d 61 63 68 69 6e  s a state machin
2eeb1 65 20 77 69 74 68 20 37 20 73 74 61 74 65 73 3a  e with 7 states:
2eeb2 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29 20 53 54 41  .**.**   (0) STA
2eeb3 52 54 20 20 20 20 20 41 74 20 74 68 65 20 62 65  RT     At the be
2eeb4 67 69 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20 6f  ginning or end o
2eeb5 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  f an SQL stateme
2eeb6 6e 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  nt.  This routin
2eeb7 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
2eeb8 20 20 20 20 20 72 65 74 75 72 6e 73 20 31 20 69       returns 1 i
2eeb9 66 20 69 74 20 65 6e 64 73 20 69 6e 20 74 68 65  f it ends in the
2eeba 20 53 54 41 52 54 20 73 74 61 74 65 20 61 6e 64   START state and
2eebb 20 30 20 69 66 20 69 74 20 65 6e 64 73 0a 2a 2a   0 if it ends.**
2eebc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eebd 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 73 74   in any other st
2eebe 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29  ate..**.**   (1)
2eebf 20 4e 4f 52 4d 41 4c 20 20 20 20 57 65 20 61 72   NORMAL    We ar
2eec0 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  e in the middle 
2eec1 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 77 68 69  of statement whi
2eec2 63 68 20 65 6e 64 73 20 77 69 74 68 20 61 20 73  ch ends with a s
2eec3 69 6e 67 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20  ingle.**        
2eec4 20 20 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c           semicol
2eec5 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20  on..**.**   (2) 
2eec6 45 58 50 4c 41 49 4e 20 20 20 54 68 65 20 6b 65  EXPLAIN   The ke
2eec7 79 77 6f 72 64 20 45 58 50 4c 41 49 4e 20 68 61  yword EXPLAIN ha
2eec8 73 20 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74  s been seen at t
2eec9 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
2eeca 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2eecb 20 20 20 20 61 20 73 74 61 74 65 6d 65 6e 74 2e      a statement.
2eecc 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 43 52 45  .**.**   (3) CRE
2eecd 41 54 45 20 20 20 20 54 68 65 20 6b 65 79 77 6f  ATE    The keywo
2eece 72 64 20 43 52 45 41 54 45 20 68 61 73 20 62 65  rd CREATE has be
2eecf 65 6e 20 73 65 65 6e 20 61 74 20 74 68 65 20 62  en seen at the b
2eed0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 0a 2a 2a  eginning of a.**
2eed1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eed2 20 73 74 61 74 65 6d 65 6e 74 2c 20 70 6f 73 73   statement, poss
2eed3 69 62 6c 79 20 70 72 65 63 65 65 64 65 64 20 62  ibly preceeded b
2eed4 79 20 45 58 50 4c 41 49 4e 20 61 6e 64 2f 6f 72  y EXPLAIN and/or
2eed5 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a 20   followed by.** 
2eed6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eed7 54 45 4d 50 20 6f 72 20 54 45 4d 50 4f 52 41 52  TEMP or TEMPORAR
2eed8 59 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 54 52  Y.**.**   (4) TR
2eed9 49 47 47 45 52 20 20 20 57 65 20 61 72 65 20 69  IGGER   We are i
2eeda 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
2eedb 61 20 74 72 69 67 67 65 72 20 64 65 66 69 6e 69  a trigger defini
2eedc 74 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62  tion that must b
2eedd 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
2eede 20 20 20 20 20 65 6e 64 65 64 20 62 79 20 61 20       ended by a 
2eedf 73 65 6d 69 63 6f 6c 6f 6e 2c 20 74 68 65 20 6b  semicolon, the k
2eee0 65 79 77 6f 72 64 20 45 4e 44 2c 20 61 6e 64 20  eyword END, and 
2eee1 61 6e 6f 74 68 65 72 20 73 65 6d 69 63 6f 6c 6f  another semicolo
2eee2 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 53  n..**.**   (5) S
2eee3 45 4d 49 20 20 20 20 20 20 57 65 27 76 65 20 73  EMI      We've s
2eee4 65 65 6e 20 74 68 65 20 66 69 72 73 74 20 73 65  een the first se
2eee5 6d 69 63 6f 6c 6f 6e 20 69 6e 20 74 68 65 20 22  micolon in the "
2eee6 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75  ;END;" that occu
2eee7 72 73 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20  rs at.**        
2eee8 20 20 20 20 20 20 20 20 20 74 68 65 20 65 6e 64           the end
2eee9 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 64 65   of a trigger de
2eeea 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  finition..**.** 
2eeeb 20 20 28 36 29 20 45 4e 44 20 20 20 20 20 20 20    (6) END       
2eeec 57 65 27 76 65 20 73 65 65 6e 20 74 68 65 20 22  We've seen the "
2eeed 3b 45 4e 44 22 20 6f 66 20 74 68 65 20 22 3b 45  ;END" of the ";E
2eeee 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73  ND;" that occurs
2eeef 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 20   at the end.**  
2eef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
2eef1 66 20 61 20 74 72 69 67 67 65 72 20 64 69 66 69  f a trigger difi
2eef2 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 72  nition..**.** Tr
2eef3 61 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65  ansitions betwee
2eef4 6e 20 73 74 61 74 65 73 20 61 62 6f 76 65 20 61  n states above a
2eef5 72 65 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  re determined by
2eef6 20 74 6f 6b 65 6e 73 20 65 78 74 72 61 63 74 65   tokens extracte
2eef7 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e  d.** from the in
2eef8 70 75 74 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  put.  The follow
2eef9 69 6e 67 20 74 6f 6b 65 6e 73 20 61 72 65 20 73  ing tokens are s
2eefa 69 67 6e 69 66 69 63 61 6e 74 3a 0a 2a 2a 0a 2a  ignificant:.**.*
2eefb 2a 20 20 20 28 30 29 20 74 6b 53 45 4d 49 20 20  *   (0) tkSEMI  
2eefc 20 20 20 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 2e      A semicolon.
2eefd 0a 2a 2a 20 20 20 28 31 29 20 74 6b 57 53 20 20  .**   (1) tkWS  
2eefe 20 20 20 20 20 20 57 68 69 74 65 73 70 61 63 65        Whitespace
2eeff 0a 2a 2a 20 20 20 28 32 29 20 74 6b 4f 54 48 45  .**   (2) tkOTHE
2ef00 52 20 20 20 20 20 41 6e 79 20 6f 74 68 65 72 20  R     Any other 
2ef01 53 51 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a 20 20 20  SQL token..**   
2ef02 28 33 29 20 74 6b 45 58 50 4c 41 49 4e 20 20 20  (3) tkEXPLAIN   
2ef03 54 68 65 20 22 65 78 70 6c 61 69 6e 22 20 6b 65  The "explain" ke
2ef04 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 34 29 20  yword..**   (4) 
2ef05 74 6b 43 52 45 41 54 45 20 20 20 20 54 68 65 20  tkCREATE    The 
2ef06 22 63 72 65 61 74 65 22 20 6b 65 79 77 6f 72 64  "create" keyword
2ef07 2e 0a 2a 2a 20 20 20 28 35 29 20 74 6b 54 45 4d  ..**   (5) tkTEM
2ef08 50 20 20 20 20 20 20 54 68 65 20 22 74 65 6d 70  P      The "temp
2ef09 22 20 6f 72 20 22 74 65 6d 70 6f 72 61 72 79 22  " or "temporary"
2ef0a 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28   keyword..**   (
2ef0b 36 29 20 74 6b 54 52 49 47 47 45 52 20 20 20 54  6) tkTRIGGER   T
2ef0c 68 65 20 22 74 72 69 67 67 65 72 22 20 6b 65 79  he "trigger" key
2ef0d 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 37 29 20 74  word..**   (7) t
2ef0e 6b 45 4e 44 20 20 20 20 20 20 20 54 68 65 20 22  kEND       The "
2ef0f 65 6e 64 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a  end" keyword..**
2ef10 0a 2a 2a 20 57 68 69 74 65 73 70 61 63 65 20 6e  .** Whitespace n
2ef11 65 76 65 72 20 63 61 75 73 65 73 20 61 20 73 74  ever causes a st
2ef12 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 61  ate transition a
2ef13 6e 64 20 69 73 20 61 6c 77 61 79 73 20 69 67 6e  nd is always ign
2ef14 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77  ored..**.** If w
2ef15 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 53  e compile with S
2ef16 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2ef17 45 52 2c 20 61 6c 6c 20 6f 66 20 74 68 65 20 63  ER, all of the c
2ef18 6f 6d 70 75 74 61 74 69 6f 6e 20 6e 65 65 64 65  omputation neede
2ef19 64 0a 2a 2a 20 74 6f 20 72 65 63 6f 67 6e 69 7a  d.** to recogniz
2ef1a 65 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74  e the end of a t
2ef1b 72 69 67 67 65 72 20 63 61 6e 20 62 65 20 6f 6d  rigger can be om
2ef1c 69 74 74 65 64 2e 20 20 41 6c 6c 20 77 65 20 68  itted.  All we h
2ef1d 61 76 65 20 74 6f 20 64 6f 0a 2a 2a 20 69 73 20  ave to do.** is 
2ef1e 6c 6f 6f 6b 20 66 6f 72 20 61 20 73 65 6d 69 63  look for a semic
2ef1f 6f 6c 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f 74  olon that is not
2ef20 20 70 61 72 74 20 6f 66 20 61 6e 20 73 74 72 69   part of an stri
2ef21 6e 67 20 6f 72 20 63 6f 6d 6d 65 6e 74 2e 0a 2a  ng or comment..*
2ef22 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ef23 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
2ef24 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  e(const char *zS
2ef25 71 6c 29 7b 0a 20 20 75 38 20 73 74 61 74 65 20  ql){.  u8 state 
2ef26 3d 20 30 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e  = 0;   /* Curren
2ef27 74 20 73 74 61 74 65 2c 20 75 73 69 6e 67 20 6e  t state, using n
2ef28 75 6d 62 65 72 73 20 64 65 66 69 6e 65 64 20 69  umbers defined i
2ef29 6e 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74  n header comment
2ef2a 20 2a 2f 0a 20 20 75 38 20 74 6f 6b 65 6e 3b 20   */.  u8 token; 
2ef2b 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
2ef2c 66 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  f the next token
2ef2d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
2ef2e 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2ef2f 0a 20 20 2f 2a 20 41 20 63 6f 6d 70 6c 65 78 20  .  /* A complex 
2ef30 73 74 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e  statement machin
2ef31 65 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 74  e used to detect
2ef32 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 43 52   the end of a CR
2ef33 45 41 54 45 20 54 52 49 47 47 45 52 0a 20 20 2a  EATE TRIGGER.  *
2ef34 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  * statement.  Th
2ef35 69 73 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c  is is the normal
2ef36 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 73 74   case..  */.  st
2ef37 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72  atic const u8 tr
2ef38 61 6e 73 5b 37 5d 5b 38 5d 20 3d 20 7b 0a 20 20  ans[7][8] = {.  
2ef39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef3a 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20     /* Token:    
2ef3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef3d 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
2ef3e 20 20 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20      /* State:   
2ef3f 20 20 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53      **  SEMI  WS
2ef40 20 20 4f 54 48 45 52 20 45 58 50 4c 41 49 4e 20    OTHER EXPLAIN 
2ef41 20 43 52 45 41 54 45 20 20 54 45 4d 50 20 20 54   CREATE  TEMP  T
2ef42 52 49 47 47 45 52 20 20 45 4e 44 20 20 2a 2f 0a  RIGGER  END  */.
2ef43 20 20 20 20 20 2f 2a 20 30 20 20 20 53 54 41 52       /* 0   STAR
2ef44 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30  T: */ {    0,  0
2ef45 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20 32 2c  ,     1,      2,
2ef46 20 20 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20        3,    1,  
2ef47 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c       1,   1,  },
2ef48 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d  .     /* 1  NORM
2ef49 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20  AL: */ {    0,  
2ef4a 31 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20 31  1,     1,      1
2ef4b 2c 20 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20  ,      1,    1, 
2ef4c 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d        1,   1,  }
2ef4d 2c 0a 20 20 20 20 20 2f 2a 20 32 20 45 58 50 4c  ,.     /* 2 EXPL
2ef4e 41 49 4e 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20  AIN: */ {    0, 
2ef4f 20 32 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20   2,     1,      
2ef50 31 2c 20 20 20 20 20 20 33 2c 20 20 20 20 31 2c  1,      3,    1,
2ef51 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20         1,   1,  
2ef52 7d 2c 0a 20 20 20 20 20 2f 2a 20 33 20 20 43 52  },.     /* 3  CR
2ef53 45 41 54 45 3a 20 2a 2f 20 7b 20 20 20 20 30 2c  EATE: */ {    0,
2ef54 20 20 33 2c 20 20 20 20 20 31 2c 20 20 20 20 20    3,     1,     
2ef55 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 33   1,      1,    3
2ef56 2c 20 20 20 20 20 20 20 34 2c 20 20 20 31 2c 20  ,       4,   1, 
2ef57 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 34 20 54 52   },.     /* 4 TR
2ef58 49 47 47 45 52 3a 20 2a 2f 20 7b 20 20 20 20 35  IGGER: */ {    5
2ef59 2c 20 20 34 2c 20 20 20 20 20 34 2c 20 20 20 20  ,  4,     4,    
2ef5a 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20    4,      4,    
2ef5b 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c  4,       4,   4,
2ef5c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 35 20 20    },.     /* 5  
2ef5d 20 20 53 45 4d 49 3a 20 2a 2f 20 7b 20 20 20 20    SEMI: */ {    
2ef5e 35 2c 20 20 35 2c 20 20 20 20 20 34 2c 20 20 20  5,  5,     4,   
2ef5f 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20     4,      4,   
2ef60 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20 36   4,       4,   6
2ef61 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 36 20  ,  },.     /* 6 
2ef62 20 20 20 20 45 4e 44 3a 20 2a 2f 20 7b 20 20 20      END: */ {   
2ef63 20 30 2c 20 20 36 2c 20 20 20 20 20 34 2c 20 20   0,  6,     4,  
2ef64 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20      4,      4,  
2ef65 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20    4,       4,   
2ef66 34 2c 20 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6c 73  4,  },.  };.#els
2ef67 65 0a 20 20 2f 2a 20 49 66 20 74 72 69 67 67 65  e.  /* If trigge
2ef68 72 73 20 61 72 65 20 6e 6f 74 20 73 75 70 70 6f  rs are not suppo
2ef69 72 65 64 20 62 79 20 74 68 69 73 20 63 6f 6d 70  red by this comp
2ef6a 69 6c 65 20 74 68 65 6e 20 74 68 65 20 73 74 61  ile then the sta
2ef6b 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 0a 20  tement machine. 
2ef6c 20 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65   ** used to dete
2ef6d 63 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  ct the end of a 
2ef6e 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6d 75 63  statement is muc
2ef6f 68 20 73 69 6d 70 6c 69 65 72 0a 20 20 2a 2f 0a  h simplier.  */.
2ef70 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2ef71 38 20 74 72 61 6e 73 5b 32 5d 5b 33 5d 20 3d 20  8 trans[2][3] = 
2ef72 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2ef73 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a         /* Token:
2ef74 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
2ef75 20 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20     /* State:    
2ef76 20 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20     **  SEMI  WS 
2ef77 20 4f 54 48 45 52 20 2a 2f 0a 20 20 20 20 20 2f   OTHER */.     /
2ef78 2a 20 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20  * 0   START: */ 
2ef79 7b 20 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20  {    0,  0,     
2ef7a 31 2c 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20  1, },.     /* 1 
2ef7b 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20   NORMAL: */ {   
2ef7c 20 30 2c 20 20 31 2c 20 20 20 20 20 31 2c 20 7d   0,  1,     1, }
2ef7d 2c 0a 20 20 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a  ,.  };.#endif /*
2ef7e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
2ef7f 47 47 45 52 20 2a 2f 0a 0a 20 20 77 68 69 6c 65  GGER */..  while
2ef80 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 73  ( *zSql ){.    s
2ef81 77 69 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a  witch( *zSql ){.
2ef82 20 20 20 20 20 20 63 61 73 65 20 27 3b 27 3a 20        case ';': 
2ef83 7b 20 20 2f 2a 20 41 20 73 65 6d 69 63 6f 6c 6f  {  /* A semicolo
2ef84 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b  n */.        tok
2ef85 65 6e 20 3d 20 74 6b 53 45 4d 49 3b 0a 20 20 20  en = tkSEMI;.   
2ef86 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ef87 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27    }.      case '
2ef88 20 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27   ':.      case '
2ef89 5c 72 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20  \r':.      case 
2ef8a 27 5c 74 27 3a 0a 20 20 20 20 20 20 63 61 73 65  '\t':.      case
2ef8b 20 27 5c 6e 27 3a 0a 20 20 20 20 20 20 63 61 73   '\n':.      cas
2ef8c 65 20 27 5c 66 27 3a 20 7b 20 20 2f 2a 20 57 68  e '\f': {  /* Wh
2ef8d 69 74 65 20 73 70 61 63 65 20 69 73 20 69 67 6e  ite space is ign
2ef8e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  ored */.        
2ef8f 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20  token = tkWS;.  
2ef90 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ef91 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
2ef92 27 2f 27 3a 20 7b 20 20 20 2f 2a 20 43 2d 73 74  '/': {   /* C-st
2ef93 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a  yle comments */.
2ef94 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c          if( zSql
2ef95 5b 31 5d 21 3d 27 2a 27 20 29 7b 0a 20 20 20 20  [1]!='*' ){.    
2ef96 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2ef97 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20  OTHER;.         
2ef98 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2ef99 7d 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 20 2b  }.        zSql +
2ef9a 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69  = 2;.        whi
2ef9b 6c 65 28 20 7a 53 71 6c 5b 30 5d 20 26 26 20 28  le( zSql[0] && (
2ef9c 7a 53 71 6c 5b 30 5d 21 3d 27 2a 27 20 7c 7c 20  zSql[0]!='*' || 
2ef9d 7a 53 71 6c 5b 31 5d 21 3d 27 2f 27 29 20 29 7b  zSql[1]!='/') ){
2ef9e 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20   zSql++; }.     
2ef9f 20 20 20 69 66 28 20 7a 53 71 6c 5b 30 5d 3d 3d     if( zSql[0]==
2efa0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
2efa1 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20        zSql++;.  
2efa2 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2efa3 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  WS;.        brea
2efa4 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
2efa5 20 63 61 73 65 20 27 2d 27 3a 20 7b 20 20 20 2f   case '-': {   /
2efa6 2a 20 53 51 4c 2d 73 74 79 6c 65 20 63 6f 6d 6d  * SQL-style comm
2efa7 65 6e 74 73 20 66 72 6f 6d 20 22 2d 2d 22 20 74  ents from "--" t
2efa8 6f 20 65 6e 64 20 6f 66 20 6c 69 6e 65 20 2a 2f  o end of line */
2efa9 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 71  .        if( zSq
2efaa 6c 5b 31 5d 21 3d 27 2d 27 20 29 7b 0a 20 20 20  l[1]!='-' ){.   
2efab 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2efac 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
2efad 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
2efae 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65   }.        while
2efaf 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c  ( *zSql && *zSql
2efb0 21 3d 27 5c 6e 27 20 29 7b 20 7a 53 71 6c 2b 2b  !='\n' ){ zSql++
2efb1 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
2efb2 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72  *zSql==0 ) retur
2efb3 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 20 20 20 20  n state==0;.    
2efb4 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53      token = tkWS
2efb5 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
2efb6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
2efb7 61 73 65 20 27 5b 27 3a 20 7b 20 20 20 2f 2a 20  ase '[': {   /* 
2efb8 4d 69 63 72 6f 73 6f 66 74 2d 73 74 79 6c 65 20  Microsoft-style 
2efb9 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20 5b  identifiers in [
2efba 2e 2e 2e 5d 20 2a 2f 0a 20 20 20 20 20 20 20 20  ...] */.        
2efbb 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20  zSql++;.        
2efbc 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20  while( *zSql && 
2efbd 2a 7a 53 71 6c 21 3d 27 5d 27 20 29 7b 20 7a 53  *zSql!=']' ){ zS
2efbe 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20  ql++; }.        
2efbf 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72  if( *zSql==0 ) r
2efc0 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20  eturn 0;.       
2efc1 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2efc2 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
2efc3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
2efc4 61 73 65 20 27 60 27 3a 20 20 20 20 20 2f 2a 20  ase '`':     /* 
2efc5 47 72 61 76 65 2d 61 63 63 65 6e 74 20 71 75 6f  Grave-accent quo
2efc6 74 65 64 20 73 79 6d 62 6f 6c 73 20 75 73 65 64  ted symbols used
2efc7 20 62 79 20 4d 79 53 51 4c 20 2a 2f 0a 20 20 20   by MySQL */.   
2efc8 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 20     case '"':    
2efc9 20 2f 2a 20 73 69 6e 67 6c 65 2d 20 61 6e 64 20   /* single- and 
2efca 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74  double-quoted st
2efcb 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20 20 20 63  rings */.      c
2efcc 61 73 65 20 27 5c 27 27 3a 20 7b 0a 20 20 20 20  ase '\'': {.    
2efcd 20 20 20 20 69 6e 74 20 63 20 3d 20 2a 7a 53 71      int c = *zSq
2efce 6c 3b 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b  l;.        zSql+
2efcf 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  +;.        while
2efd0 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c  ( *zSql && *zSql
2efd1 21 3d 63 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d  !=c ){ zSql++; }
2efd2 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53  .        if( *zS
2efd3 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ql==0 ) return 0
2efd4 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20  ;.        token 
2efd5 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
2efd6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2efd7 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
2efd8 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63   {.        int c
2efd9 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 64  ;.        if( Id
2efda 43 68 61 72 28 28 75 38 29 2a 7a 53 71 6c 29 20  Char((u8)*zSql) 
2efdb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
2efdc 4b 65 79 77 6f 72 64 73 20 61 6e 64 20 75 6e 71  Keywords and unq
2efdd 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65 72  uoted identifier
2efde 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69  s */.          i
2efdf 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20 20 20 20  nt nId;.        
2efe0 20 20 66 6f 72 28 6e 49 64 3d 31 3b 20 49 64 43    for(nId=1; IdC
2efe1 68 61 72 28 7a 53 71 6c 5b 6e 49 64 5d 29 3b 20  har(zSql[nId]); 
2efe2 6e 49 64 2b 2b 29 7b 7d 0a 23 69 66 64 65 66 20  nId++){}.#ifdef 
2efe3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2efe4 47 45 52 0a 20 20 20 20 20 20 20 20 20 20 74 6f  GER.          to
2efe5 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 23  ken = tkOTHER;.#
2efe6 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 73  else.          s
2efe7 77 69 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a  witch( *zSql ){.
2efe8 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
2efe9 20 27 63 27 3a 20 63 61 73 65 20 27 43 27 3a 20   'c': case 'C': 
2efea 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2efeb 69 66 28 20 6e 49 64 3d 3d 36 20 26 26 20 73 71  if( nId==6 && sq
2efec 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53  lite3StrNICmp(zS
2efed 71 6c 2c 20 22 63 72 65 61 74 65 22 2c 20 36 29  ql, "create", 6)
2efee 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2efef 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2eff0 6b 43 52 45 41 54 45 3b 0a 20 20 20 20 20 20 20  kCREATE;.       
2eff1 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
2eff2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
2eff3 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
2eff4 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
2eff5 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
2eff6 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
2eff7 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61  }.            ca
2eff8 73 65 20 27 74 27 3a 20 63 61 73 65 20 27 54 27  se 't': case 'T'
2eff9 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
2effa 20 20 69 66 28 20 6e 49 64 3d 3d 37 20 26 26 20    if( nId==7 && 
2effb 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2effc 7a 53 71 6c 2c 20 22 74 72 69 67 67 65 72 22 2c  zSql, "trigger",
2effd 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   7)==0 ){.      
2effe 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2efff 3d 20 74 6b 54 52 49 47 47 45 52 3b 0a 20 20 20  = tkTRIGGER;.   
2f000 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
2f001 20 69 66 28 20 6e 49 64 3d 3d 34 20 26 26 20 73   if( nId==4 && s
2f002 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
2f003 53 71 6c 2c 20 22 74 65 6d 70 22 2c 20 34 29 3d  Sql, "temp", 4)=
2f004 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2f005 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2f006 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20 20 20  TEMP;.          
2f007 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49      }else if( nI
2f008 64 3d 3d 39 20 26 26 20 73 71 6c 69 74 65 33 53  d==9 && sqlite3S
2f009 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74  trNICmp(zSql, "t
2f00a 65 6d 70 6f 72 61 72 79 22 2c 20 39 29 3d 3d 30  emporary", 9)==0
2f00b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2f00c 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 45      token = tkTE
2f00d 4d 50 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  MP;.            
2f00e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2f00f 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
2f010 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
2f011 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f012 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2f013 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2f014 20 20 20 20 20 20 20 20 20 63 61 73 65 20 27 65           case 'e
2f015 27 3a 20 20 63 61 73 65 20 27 45 27 3a 20 7b 0a  ':  case 'E': {.
2f016 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
2f017 28 20 6e 49 64 3d 3d 33 20 26 26 20 73 71 6c 69  ( nId==3 && sqli
2f018 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c  te3StrNICmp(zSql
2f019 2c 20 22 65 6e 64 22 2c 20 33 29 3d 3d 30 20 29  , "end", 3)==0 )
2f01a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2f01b 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45 4e 44 3b    token = tkEND;
2f01c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
2f01d 65 6c 73 65 0a 23 69 66 6e 64 65 66 20 53 51 4c  else.#ifndef SQL
2f01e 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
2f01f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
2f020 66 28 20 6e 49 64 3d 3d 37 20 26 26 20 73 71 6c  f( nId==7 && sql
2f021 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71  ite3StrNICmp(zSq
2f022 6c 2c 20 22 65 78 70 6c 61 69 6e 22 2c 20 37 29  l, "explain", 7)
2f023 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2f024 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2f025 6b 45 58 50 4c 41 49 4e 3b 0a 20 20 20 20 20 20  kEXPLAIN;.      
2f026 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65          }else.#e
2f027 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 20  ndif.           
2f028 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20     {.           
2f029 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
2f02a 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  THER;.          
2f02b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2f02c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2f02d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2f02e 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
2f02f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
2f030 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
2f031 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
2f032 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
2f033 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65  }.          }.#e
2f034 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2f035 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 20  MIT_TRIGGER */. 
2f036 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d           zSql +=
2f037 20 6e 49 64 2d 31 3b 0a 20 20 20 20 20 20 20 20   nId-1;.        
2f038 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2f039 20 2f 2a 20 4f 70 65 72 61 74 6f 72 73 20 61 6e   /* Operators an
2f03a 64 20 73 70 65 63 69 61 6c 20 73 79 6d 62 6f 6c  d special symbol
2f03b 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 74  s */.          t
2f03c 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
2f03d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f03e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2f03f 0a 20 20 20 20 7d 0a 20 20 20 20 73 74 61 74 65  .    }.    state
2f040 20 3d 20 74 72 61 6e 73 5b 73 74 61 74 65 5d 5b   = trans[state][
2f041 74 6f 6b 65 6e 5d 3b 0a 20 20 20 20 7a 53 71 6c  token];.    zSql
2f042 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
2f043 20 73 74 61 74 65 3d 3d 30 3b 0a 7d 0a 0a 23 69   state==0;.}..#i
2f044 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f045 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68  T_UTF16./*.** Th
2f046 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
2f047 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71  e same as the sq
2f048 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29  lite3_complete()
2f049 20 72 6f 75 74 69 6e 65 20 64 65 73 63 72 69 62   routine describ
2f04a 65 64 0a 2a 2a 20 61 62 6f 76 65 2c 20 65 78 63  ed.** above, exc
2f04b 65 70 74 20 74 68 61 74 20 74 68 65 20 70 61 72  ept that the par
2f04c 61 6d 65 74 65 72 20 69 73 20 72 65 71 75 69 72  ameter is requir
2f04d 65 64 20 74 6f 20 62 65 20 55 54 46 2d 31 36 20  ed to be UTF-16 
2f04e 65 6e 63 6f 64 65 64 2c 20 6e 6f 74 0a 2a 2a 20  encoded, not.** 
2f04f 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45  UTF-8..*/.SQLITE
2f050 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2f051 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73  _complete16(cons
2f052 74 20 76 6f 69 64 20 2a 7a 53 71 6c 29 7b 0a 20  t void *zSql){. 
2f053 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2f054 70 56 61 6c 3b 0a 20 20 63 68 61 72 20 63 6f 6e  pVal;.  char con
2f055 73 74 20 2a 7a 53 71 6c 38 3b 0a 20 20 69 6e 74  st *zSql8;.  int
2f056 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
2f057 45 4d 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  EM;..#ifndef SQL
2f058 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
2f059 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  T.  rc = sqlite3
2f05a 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
2f05b 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
2f05c 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70 56   rc;.#endif.  pV
2f05d 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
2f05e 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c 69 74  eNew(0);.  sqlit
2f05f 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56  e3ValueSetStr(pV
2f060 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c 2c 20 53 51  al, -1, zSql, SQ
2f061 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
2f062 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2f063 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69  ;.  zSql8 = sqli
2f064 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
2f065 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
2f066 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a  .  if( zSql8 ){.
2f067 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2f068 5f 63 6f 6d 70 6c 65 74 65 28 7a 53 71 6c 38 29  _complete(zSql8)
2f069 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
2f06a 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
2f06b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
2f06c 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a  alueFree(pVal);.
2f06d 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2f06e 41 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a  ApiExit(0, rc);.
2f06f 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2f070 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
2f071 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2f072 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 20  E_OMIT_COMPLETE 
2f073 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
2f074 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 6f 6d 70 6c  *** End of compl
2f075 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
2f076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f077 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f078 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2f079 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d  *** Begin file m
2f07a 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ain.c **********
2f07b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f07c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f07d 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
2f07e 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
2f07f 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2f080 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2f081 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2f082 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2f083 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2f084 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2f085 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2f086 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2f087 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2f088 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2f089 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2f08a 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2f08b 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2f08c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2f08d 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2f08e 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2f08f 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2f090 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2f091 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f092 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61  **********.** Ma
2f095 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  in file for the 
2f096 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
2f097 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
2f098 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d   this file.** im
2f099 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67  plement the prog
2f09a 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65  rammer interface
2f09b 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e   to the library.
2f09c 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a    Routines in.**
2f09d 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65   other files are
2f09e 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
2f09f 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20  e by SQLite and 
2f0a0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
2f0a1 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65   accessed by use
2f0a2 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  rs of the librar
2f0a3 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 61  y..**.** $Id: ma
2f0a4 69 6e 2e 63 2c 76 20 31 2e 35 33 34 20 32 30 30  in.c,v 1.534 200
2f0a5 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 32  9/03/23 04:33:32
2f0a6 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
2f0a7 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51   $.*/..#ifdef SQ
2f0a8 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
2f0a9 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2f0aa 20 49 6e 63 6c 75 64 65 20 66 74 73 33 2e 68 20   Include fts3.h 
2f0ab 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
2f0ac 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
2f0ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2f0ae 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2f0af 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
2f0b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2f0b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2f0b3 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20  ./*.** 2006 Oct 
2f0b4 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  10.**.** The aut
2f0b5 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2f0b6 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2f0b7 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2f0b8 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
2f0b9 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2f0ba 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
2f0bb 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
2f0bc 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
2f0bd 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
2f0be 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
2f0bf 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
2f0c0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
2f0c1 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
2f0c2 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
2f0c3 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
2f0c4 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2f0c5 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2f0c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0ca 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
2f0cb 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73  s header file is
2f0cc 20 75 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d   used by program
2f0cd 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c  s that want to l
2f0ce 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a  ink against the.
2f0cf 2a 2a 20 46 54 53 33 20 6c 69 62 72 61 72 79 2e  ** FTS3 library.
2f0d0 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73    All it does is
2f0d1 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c   declare the sql
2f0d2 69 74 65 33 46 74 73 33 49 6e 69 74 28 29 20 69  ite3Fts3Init() i
2f0d3 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69  nterface..*/..#i
2f0d4 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b  f 0.extern "C" {
2f0d5 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70  .#endif  /* __cp
2f0d6 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49  lusplus */..SQLI
2f0d7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2f0d8 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 73  qlite3Fts3Init(s
2f0d9 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69  qlite3 *db);..#i
2f0da 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e  f 0.}  /* extern
2f0db 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20   "C" */.#endif  
2f0dc 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a  /* __cplusplus *
2f0dd 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2f0de 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 2e 68  ** End of fts3.h
2f0df 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2f0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0e2 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2f0e3 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
2f0e4 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
2f0e5 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  in main.c ******
2f0e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0e7 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  */.#endif.#ifdef
2f0e8 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52   SQLITE_ENABLE_R
2f0e9 54 52 45 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  TREE./**********
2f0ea 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 72 74 72  **** Include rtr
2f0eb 65 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  ee.h in the midd
2f0ec 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a  le of main.c ***
2f0ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0ee 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2f0ef 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2f0f0 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  rtree.h ********
2f0f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0f3 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
2f0f4 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65  May 26.**.** The
2f0f5 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
2f0f6 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
2f0f7 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2f0f8 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2f0f9 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2f0fa 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2f0fb 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2f0fc 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2f0fd 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2f0fe 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2f0ff 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
2f100 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
2f101 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2f102 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2f103 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2f104 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2f105 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
2f106 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f107 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f108 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f109 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f10a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
2f10b 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
2f10c 65 20 69 73 20 75 73 65 64 20 62 79 20 70 72 6f  e is used by pro
2f10d 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20  grams that want 
2f10e 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  to link against 
2f10f 74 68 65 0a 2a 2a 20 52 54 52 45 45 20 6c 69 62  the.** RTREE lib
2f110 72 61 72 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f  rary.  All it do
2f111 65 73 20 69 73 20 64 65 63 6c 61 72 65 20 74 68  es is declare th
2f112 65 20 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e  e sqlite3RtreeIn
2f113 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a  it() interface..
2f114 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e  */..#if 0.extern
2f115 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f   "C" {.#endif  /
2f116 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f  * __cplusplus */
2f117 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2f118 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74 72 65   int sqlite3Rtre
2f119 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  eInit(sqlite3 *d
2f11a 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a  b);..#if 0.}  /*
2f11b 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23   extern "C" */.#
2f11c 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75  endif  /* __cplu
2f11d 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  splus */../*****
2f11e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2f11f 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   rtree.h *******
2f120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f121 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f122 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2f123 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
2f124 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
2f125 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63  ft off in main.c
2f126 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2f127 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
2f128 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
2f129 4e 41 42 4c 45 5f 49 43 55 0a 2f 2a 2a 2a 2a 2a  NABLE_ICU./*****
2f12a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
2f12b 65 20 73 71 6c 69 74 65 69 63 75 2e 68 20 69 6e  e sqliteicu.h in
2f12c 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d   the middle of m
2f12d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ain.c **********
2f12e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2f12f 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2f130 66 69 6c 65 20 73 71 6c 69 74 65 69 63 75 2e 68  file sqliteicu.h
2f131 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2f132 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f133 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2f134 32 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a  2008 May 26.**.*
2f135 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2f136 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2f137 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2f138 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2f139 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2f13a 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2f13b 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2f13c 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2f13d 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2f13e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2f13f 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2f140 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2f141 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2f142 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2f143 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2f144 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2f145 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2f146 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2f147 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f148 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f149 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f14a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
2f14b 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
2f14c 72 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 62  r file is used b
2f14d 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20  y programs that 
2f14e 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61  want to link aga
2f14f 69 6e 73 74 20 74 68 65 0a 2a 2a 20 49 43 55 20  inst the.** ICU 
2f150 65 78 74 65 6e 73 69 6f 6e 2e 20 20 41 6c 6c 20  extension.  All 
2f151 69 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61  it does is decla
2f152 72 65 20 74 68 65 20 73 71 6c 69 74 65 33 49 63  re the sqlite3Ic
2f153 75 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63  uInit() interfac
2f154 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74  e..*/..#if 0.ext
2f155 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66  ern "C" {.#endif
2f156 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73    /* __cplusplus
2f157 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
2f158 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
2f159 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a  cuInit(sqlite3 *
2f15a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f  db);..#if 0.}  /
2f15b 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a  * extern "C" */.
2f15c 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c  #endif  /* __cpl
2f15d 75 73 70 6c 75 73 20 2a 2f 0a 0a 0a 2f 2a 2a 2a  usplus */.../***
2f15e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2f15f 6f 66 20 73 71 6c 69 74 65 69 63 75 2e 68 20 2a  of sqliteicu.h *
2f160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f161 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f162 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2f163 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
2f164 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
2f165 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e  left off in main
2f166 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2f167 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
2f168 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 65  if../*.** The ve
2f169 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62  rsion of the lib
2f16a 72 61 72 79 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  rary.*/.#ifndef 
2f16b 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
2f16c 49 4f 4e 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  ION.SQLITE_API c
2f16d 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65  onst char sqlite
2f16e 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51  3_version[] = SQ
2f16f 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 23 65  LITE_VERSION;.#e
2f170 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20  ndif.SQLITE_API 
2f171 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
2f172 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76  te3_libversion(v
2f173 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 71 6c  oid){ return sql
2f174 69 74 65 33 5f 76 65 72 73 69 6f 6e 3b 20 7d 0a  ite3_version; }.
2f175 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2f176 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
2f177 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 7b 20  n_number(void){ 
2f178 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 56 45  return SQLITE_VE
2f179 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 3b 20 7d 0a  RSION_NUMBER; }.
2f17a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2f17b 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66  qlite3_threadsaf
2f17c 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  e(void){ return 
2f17d 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
2f17e 45 3b 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e  E; }..#if !defin
2f17f 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
2f180 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 64  RACE) && defined
2f181 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49  (SQLITE_ENABLE_I
2f182 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 49 66  OTRACE)./*.** If
2f183 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
2f184 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20  unction pointer 
2f185 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20  is not NULL and 
2f186 69 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41  if.** SQLITE_ENA
2f187 42 4c 45 5f 49 4f 54 52 41 43 45 20 69 73 20 65  BLE_IOTRACE is e
2f188 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 6d 65 73  nabled, then mes
2f189 73 61 67 65 73 20 64 65 73 63 72 69 62 69 6e 67  sages describing
2f18a 0a 2a 2a 20 49 2f 4f 20 61 63 74 69 76 65 20 61  .** I/O active a
2f18b 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67  re written using
2f18c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
2f18d 20 54 68 65 73 65 20 6d 65 73 73 61 67 65 73 0a   These messages.
2f18e 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  ** are intended 
2f18f 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 61 63  for debugging ac
2f190 74 69 76 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2f 0a  tivity only..*/.
2f191 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2f192 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 49 6f 54  oid (*sqlite3IoT
2f193 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68 61 72  race)(const char
2f194 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b 0a 23 65 6e  *, ...) = 0;.#en
2f195 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  dif../*.** If th
2f196 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62  e following glob
2f197 61 6c 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e  al variable poin
2f198 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77  ts to a string w
2f199 68 69 63 68 20 69 73 20 74 68 65 0a 2a 2a 20 6e  hich is the.** n
2f19a 61 6d 65 20 6f 66 20 61 20 64 69 72 65 63 74 6f  ame of a directo
2f19b 72 79 2c 20 74 68 65 6e 20 74 68 61 74 20 64 69  ry, then that di
2f19c 72 65 63 74 6f 72 79 20 77 69 6c 6c 20 62 65 20  rectory will be 
2f19d 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a  used to store.**
2f19e 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
2f19f 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
2f1a0 20 74 68 65 20 22 50 52 41 47 4d 41 20 74 65 6d   the "PRAGMA tem
2f1a1 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72  p_store_director
2f1a2 79 22 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 2e 0a  y" SQL command..
2f1a3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  */.SQLITE_API ch
2f1a4 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d 70  ar *sqlite3_temp
2f1a5 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a  _directory = 0;.
2f1a6 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
2f1a7 65 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a  e SQLite.  .**.*
2f1a8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
2f1a9 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 74 6f  ust be called to
2f1aa 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
2f1ab 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2f1ac 6e 2c 0a 2a 2a 20 56 46 53 2c 20 61 6e 64 20 6d  n,.** VFS, and m
2f1ad 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 73 20  utex subsystems 
2f1ae 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e 67 20 61  prior to doing a
2f1af 6e 79 20 73 65 72 69 6f 75 73 20 77 6f 72 6b 20  ny serious work 
2f1b0 77 69 74 68 0a 2a 2a 20 53 51 4c 69 74 65 2e 20  with.** SQLite. 
2f1b1 20 42 75 74 20 61 73 20 6c 6f 6e 67 20 61 73 20   But as long as 
2f1b2 79 6f 75 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 69  you do not compi
2f1b3 6c 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f  le with SQLITE_O
2f1b4 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 2a 2a 20  MIT_AUTOINIT.** 
2f1b5 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
2f1b6 6c 20 62 65 20 63 61 6c 6c 65 64 20 61 75 74 6f  l be called auto
2f1b7 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 6b 65 79  matically by key
2f1b8 20 72 6f 75 74 69 6e 65 73 20 73 75 63 68 20 61   routines such a
2f1b9 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  s.** sqlite3_ope
2f1ba 6e 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69  n().  .**.** Thi
2f1bb 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
2f1bc 6f 2d 6f 70 20 65 78 63 65 70 74 20 6f 6e 20 69  o-op except on i
2f1bd 74 73 20 76 65 72 79 20 66 69 72 73 74 20 63 61  ts very first ca
2f1be 6c 6c 20 66 6f 72 20 74 68 65 20 70 72 6f 63 65  ll for the proce
2f1bf 73 73 2c 0a 2a 2a 20 6f 72 20 66 6f 72 20 74 68  ss,.** or for th
2f1c0 65 20 66 69 72 73 74 20 63 61 6c 6c 20 61 66 74  e first call aft
2f1c1 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  er a call to sql
2f1c2 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 2e 0a 2a  ite3_shutdown..*
2f1c3 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
2f1c4 68 72 65 61 64 20 74 6f 20 63 61 6c 6c 20 74 68  hread to call th
2f1c5 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20  is routine runs 
2f1c6 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  the initializati
2f1c7 6f 6e 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74  on to.** complet
2f1c8 69 6f 6e 2e 20 20 49 66 20 73 75 62 73 65 71 75  ion.  If subsequ
2f1c9 65 6e 74 20 74 68 72 65 61 64 73 20 63 61 6c 6c  ent threads call
2f1ca 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 62 65   this routine be
2f1cb 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 2a  fore the first.*
2f1cc 2a 20 74 68 72 65 61 64 20 68 61 73 20 66 69 6e  * thread has fin
2f1cd 69 73 68 65 64 20 74 68 65 20 69 6e 69 74 69 61  ished the initia
2f1ce 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63 65 73 73  lization process
2f1cf 2c 20 74 68 65 6e 20 74 68 65 20 73 75 62 73 65  , then the subse
2f1d0 71 75 65 6e 74 0a 2a 2a 20 74 68 72 65 61 64 73  quent.** threads
2f1d1 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74 69   must block unti
2f1d2 6c 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65  l the first thre
2f1d3 61 64 20 66 69 6e 69 73 68 65 73 20 77 69 74 68  ad finishes with
2f1d4 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74   the initializat
2f1d5 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ion..**.** The f
2f1d6 69 72 73 74 20 74 68 72 65 61 64 20 6d 69 67 68  irst thread migh
2f1d7 74 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  t call this rout
2f1d8 69 6e 65 20 72 65 63 75 72 73 69 76 65 6c 79 2e  ine recursively.
2f1d9 20 20 52 65 63 75 72 73 69 76 65 0a 2a 2a 20 63    Recursive.** c
2f1da 61 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75  alls to this rou
2f1db 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  tine should not 
2f1dc 62 6c 6f 63 6b 2c 20 6f 66 20 63 6f 75 72 73 65  block, of course
2f1dd 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74 68 65  .  Otherwise the
2f1de 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  .** initializati
2f1df 6f 6e 20 70 72 6f 63 65 73 73 20 77 6f 75 6c 64  on process would
2f1e0 20 6e 65 76 65 72 20 63 6f 6d 70 6c 65 74 65 2e   never complete.
2f1e1 0a 2a 2a 0a 2a 2a 20 4c 65 74 20 58 20 62 65 20  .**.** Let X be 
2f1e2 74 68 65 20 66 69 72 73 74 20 74 68 72 65 61 64  the first thread
2f1e3 20 74 6f 20 65 6e 74 65 72 20 74 68 69 73 20 72   to enter this r
2f1e4 6f 75 74 69 6e 65 2e 20 20 4c 65 74 20 59 20 62  outine.  Let Y b
2f1e5 65 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  e some other.** 
2f1e6 74 68 72 65 61 64 2e 20 20 54 68 65 6e 20 77 68  thread.  Then wh
2f1e7 69 6c 65 20 74 68 65 20 69 6e 69 74 69 61 6c 20  ile the initial 
2f1e8 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68  invocation of th
2f1e9 69 73 20 72 6f 75 74 69 6e 65 20 62 79 20 58 20  is routine by X 
2f1ea 69 73 0a 2a 2a 20 69 6e 63 6f 6d 70 6c 65 74 65  is.** incomplete
2f1eb 2c 20 69 74 20 69 73 20 72 65 71 75 69 72 65 64  , it is required
2f1ec 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   that:.**.**    
2f1ed 2a 20 20 43 61 6c 6c 73 20 74 6f 20 74 68 69 73  *  Calls to this
2f1ee 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 59 20   routine from Y 
2f1ef 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74 69 6c  must block until
2f1f0 20 74 68 65 20 6f 75 74 65 72 2d 6d 6f 73 74 0a   the outer-most.
2f1f1 2a 2a 20 20 20 20 20 20 20 63 61 6c 6c 20 62 79  **       call by
2f1f2 20 58 20 63 6f 6d 70 6c 65 74 65 73 2e 0a 2a 2a   X completes..**
2f1f3 0a 2a 2a 20 20 20 20 2a 20 20 52 65 63 75 72 73  .**    *  Recurs
2f1f4 69 76 65 20 63 61 6c 6c 73 20 74 6f 20 74 68 69  ive calls to thi
2f1f5 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 74  s routine from t
2f1f6 68 72 65 61 64 20 58 20 72 65 74 75 72 6e 20 69  hread X return i
2f1f7 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 20 20  mmediately.**   
2f1f8 20 20 20 20 77 69 74 68 6f 75 74 20 62 6c 6f 63      without bloc
2f1f9 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  king..*/.SQLITE_
2f1fa 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2f1fb 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29  initialize(void)
2f1fc 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
2f1fd 78 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20 20  x *pMaster;     
2f1fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1ff 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 73 74 61   /* The main sta
2f200 74 69 63 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69  tic mutex */.  i
2f201 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
2f202 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f203 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2f204 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 23  esult code */..#
2f205 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2f206 54 5f 57 53 44 0a 20 20 72 63 20 3d 20 73 71 6c  T_WSD.  rc = sql
2f207 69 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 34 30  ite3_wsd_init(40
2f208 39 36 2c 20 32 34 29 3b 0a 20 20 69 66 28 20 72  96, 24);.  if( r
2f209 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
2f20a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
2f20b 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
2f20c 49 66 20 53 51 4c 69 74 65 20 69 73 20 61 6c 72  If SQLite is alr
2f20d 65 61 64 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20  eady completely 
2f20e 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65  initialized, the
2f20f 6e 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a  n this call.  **
2f210 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74   to sqlite3_init
2f211 69 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64 20  ialize() should 
2f212 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74  be a no-op.  But
2f213 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74   the initializat
2f214 69 6f 6e 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65  ion.  ** must be
2f215 20 63 6f 6d 70 6c 65 74 65 2e 20 20 53 6f 20 69   complete.  So i
2f216 73 49 6e 69 74 20 6d 75 73 74 20 6e 6f 74 20 62  sInit must not b
2f217 65 20 73 65 74 20 75 6e 74 69 6c 20 74 68 65 20  e set until the 
2f218 76 65 72 79 20 65 6e 64 0a 20 20 2a 2a 20 6f 66  very end.  ** of
2f219 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 20   this routine.. 
2f21a 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2f21b 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
2f21c 49 6e 69 74 20 29 20 72 65 74 75 72 6e 20 53 51  Init ) return SQ
2f21d 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4d  LITE_OK;..  /* M
2f21e 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 75 74  ake sure the mut
2f21f 65 78 20 73 75 62 73 79 73 74 65 6d 20 69 73 20  ex subsystem is 
2f220 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 49 66  initialized.  If
2f221 20 75 6e 61 62 6c 65 20 74 6f 20 0a 20 20 2a 2a   unable to .  **
2f222 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   initialize the 
2f223 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2c  mutex subsystem,
2f224 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20 77 69   return early wi
2f225 74 68 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20  th the error..  
2f226 2a 2a 20 49 66 20 74 68 65 20 73 79 73 74 65 6d  ** If the system
2f227 20 69 73 20 73 6f 20 73 69 63 6b 20 74 68 61 74   is so sick that
2f228 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 74   we are unable t
2f229 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74  o allocate a mut
2f22a 65 78 2c 0a 20 20 2a 2a 20 74 68 65 72 65 20 69  ex,.  ** there i
2f22b 73 20 6e 6f 74 20 6d 75 63 68 20 53 51 4c 69 74  s not much SQLit
2f22c 65 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 62 65  e is going to be
2f22d 20 61 62 6c 65 20 74 6f 20 64 6f 2e 0a 20 20 2a   able to do..  *
2f22e 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 75 74 65 78  *.  ** The mutex
2f22f 20 73 75 62 73 79 73 74 65 6d 20 6d 75 73 74 20   subsystem must 
2f230 74 61 6b 65 20 63 61 72 65 20 6f 66 20 73 65 72  take care of ser
2f231 69 61 6c 69 7a 69 6e 67 20 69 74 73 20 6f 77 6e  ializing its own
2f232 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61  .  ** initializa
2f233 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  tion..  */.  rc 
2f234 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e  = sqlite3MutexIn
2f235 69 74 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29  it();.  if( rc )
2f236 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
2f237 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
2f238 20 6d 61 6c 6c 6f 63 28 29 20 73 79 73 74 65 6d   malloc() system
2f239 20 61 6e 64 20 74 68 65 20 72 65 63 75 72 73 69   and the recursi
2f23a 76 65 20 70 49 6e 69 74 4d 75 74 65 78 20 6d 75  ve pInitMutex mu
2f23b 74 65 78 2e 0a 20 20 2a 2a 20 54 68 69 73 20 6f  tex..  ** This o
2f23c 70 65 72 61 74 69 6f 6e 20 69 73 20 70 72 6f 74  peration is prot
2f23d 65 63 74 65 64 20 62 79 20 74 68 65 20 53 54 41  ected by the STA
2f23e 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
2f23f 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a  .  Note that.  *
2f240 2a 20 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 69  * MutexAlloc() i
2f241 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 73  s called for a s
2f242 74 61 74 69 63 20 6d 75 74 65 78 20 70 72 69 6f  tatic mutex prio
2f243 72 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 69 6e  r to initializin
2f244 67 20 74 68 65 0a 20 20 2a 2a 20 6d 61 6c 6c 6f  g the.  ** mallo
2f245 63 20 73 75 62 73 79 73 74 65 6d 20 2d 20 74 68  c subsystem - th
2f246 69 73 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20  is implies that 
2f247 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  the allocation o
2f248 66 20 61 20 73 74 61 74 69 63 0a 20 20 2a 2a 20  f a static.  ** 
2f249 6d 75 74 65 78 20 6d 75 73 74 20 6e 6f 74 20 72  mutex must not r
2f24a 65 71 75 69 72 65 20 73 75 70 70 6f 72 74 20 66  equire support f
2f24b 72 6f 6d 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73  rom the malloc s
2f24c 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20  ubsystem..  */. 
2f24d 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74   pMaster = sqlit
2f24e 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
2f24f 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
2f250 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c 69  _MASTER);.  sqli
2f251 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2f252 70 4d 61 73 74 65 72 29 3b 0a 20 20 69 66 28 20  pMaster);.  if( 
2f253 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  !sqlite3GlobalCo
2f254 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69  nfig.isMallocIni
2f255 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  t ){.    rc = sq
2f256 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28  lite3MallocInit(
2f257 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
2f258 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2f259 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2f25a 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e  onfig.isMallocIn
2f25b 69 74 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20  it = 1;.    if( 
2f25c 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  !sqlite3GlobalCo
2f25d 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20  nfig.pInitMutex 
2f25e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2f25f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e  GlobalConfig.pIn
2f260 69 74 4d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  itMutex = sqlite
2f261 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
2f262 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
2f263 56 45 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  VE);.      if( s
2f264 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2f265 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 26 26  ig.bCoreMutex &&
2f266 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   !sqlite3GlobalC
2f267 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
2f268 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
2f269 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2f26a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2f26b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2f26c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
2f26d 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2f26e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 2b 2b 3b  nRefInitMutex++;
2f26f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
2f270 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74  utex_leave(pMast
2f271 65 72 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 75 6e  er);..  /* If un
2f272 61 62 6c 65 20 74 6f 20 69 6e 69 74 69 61 6c 69  able to initiali
2f273 7a 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73 75  ze the malloc su
2f274 62 73 79 73 74 65 6d 2c 20 74 68 65 6e 20 72 65  bsystem, then re
2f275 74 75 72 6e 20 65 61 72 6c 79 2e 0a 20 20 2a 2a  turn early..  **
2f276 20 54 68 65 72 65 20 69 73 20 6c 69 74 74 6c 65   There is little
2f277 20 68 6f 70 65 20 6f 66 20 67 65 74 74 69 6e 67   hope of getting
2f278 20 53 51 4c 69 74 65 20 74 6f 20 72 75 6e 20 69   SQLite to run i
2f279 66 20 74 68 65 20 6d 61 6c 6c 6f 63 0a 20 20 2a  f the malloc.  *
2f27a 2a 20 73 75 62 73 79 73 74 65 6d 20 63 61 6e 6e  * subsystem cann
2f27b 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ot be initialize
2f27c 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  d..  */.  if( rc
2f27d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
2f27e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
2f27f 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74 68 65 20 72  }..  /* Do the r
2f280 65 73 74 20 6f 66 20 74 68 65 20 69 6e 69 74 69  est of the initi
2f281 61 6c 69 7a 61 74 69 6f 6e 20 75 6e 64 65 72 20  alization under 
2f282 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d 75  the recursive mu
2f283 74 65 78 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74  tex so.  ** that
2f284 20 77 65 20 77 69 6c 6c 20 62 65 20 61 62 6c 65   we will be able
2f285 20 74 6f 20 68 61 6e 64 6c 65 20 72 65 63 75 72   to handle recur
2f286 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a  sive calls into.
2f287 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69    ** sqlite3_ini
2f288 74 69 61 6c 69 7a 65 28 29 2e 20 20 54 68 65 20  tialize().  The 
2f289 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20  recursive calls 
2f28a 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d 65 20 74 68  normally come th
2f28b 72 6f 75 67 68 0a 20 20 2a 2a 20 73 71 6c 69 74  rough.  ** sqlit
2f28c 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 77 68 65  e3_os_init() whe
2f28d 6e 20 69 74 20 69 6e 76 6f 6b 65 73 20 73 71 6c  n it invokes sql
2f28e 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
2f28f 72 28 29 2c 20 62 75 74 20 6f 74 68 65 72 0a 20  r(), but other. 
2f290 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20 63 61   ** recursive ca
2f291 6c 6c 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 62  lls might also b
2f292 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f  e possible..  */
2f293 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2f294 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 47 6c  _enter(sqlite3Gl
2f295 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74  obalConfig.pInit
2f296 4d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 73 71  Mutex);.  if( sq
2f297 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2f298 67 2e 69 73 49 6e 69 74 3d 3d 30 20 26 26 20 73  g.isInit==0 && s
2f299 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2f29a 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 3d 3d 30  ig.inProgress==0
2f29b 20 29 7b 0a 20 20 20 20 46 75 6e 63 44 65 66 48   ){.    FuncDefH
2f29c 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47 4c  ash *pHash = &GL
2f29d 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73 68  OBAL(FuncDefHash
2f29e 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46  , sqlite3GlobalF
2f29f 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20 20 73  unctions);.    s
2f2a0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2f2a1 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 20 3d 20  ig.inProgress = 
2f2a2 31 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 48  1;.    memset(pH
2f2a3 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ash, 0, sizeof(s
2f2a4 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
2f2a5 74 69 6f 6e 73 29 29 3b 0a 20 20 20 20 73 71 6c  tions));.    sql
2f2a6 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62  ite3RegisterGlob
2f2a7 61 6c 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a 20  alFunctions();. 
2f2a8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
2f2a9 6f 73 5f 69 6e 69 74 28 29 3b 0a 20 20 20 20 69  os_init();.    i
2f2aa 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2f2ab 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
2f2ac 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e 69 74  qlite3PcacheInit
2f2ad 69 61 6c 69 7a 65 28 29 3b 0a 20 20 20 20 20 20  ialize();.      
2f2ae 73 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 66  sqlite3PCacheBuf
2f2af 66 65 72 53 65 74 75 70 28 20 73 71 6c 69 74 65  ferSetup( sqlite
2f2b0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50  3GlobalConfig.pP
2f2b1 61 67 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  age, .          
2f2b2 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2f2b3 66 69 67 2e 73 7a 50 61 67 65 2c 20 73 71 6c 69  fig.szPage, sqli
2f2b4 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2f2b5 6e 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  nPage);.    }.  
2f2b6 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2f2b7 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73  onfig.inProgress
2f2b8 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
2f2b9 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
2f2ba 49 6e 69 74 20 3d 20 28 72 63 3d 3d 53 51 4c 49  Init = (rc==SQLI
2f2bb 54 45 5f 4f 4b 20 3f 20 31 20 3a 20 30 29 3b 0a  TE_OK ? 1 : 0);.
2f2bc 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
2f2bd 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
2f2be 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
2f2bf 6e 69 74 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a  nitMutex);..  /*
2f2c0 20 47 6f 20 62 61 63 6b 20 75 6e 64 65 72 20 74   Go back under t
2f2c1 68 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 20  he static mutex 
2f2c2 61 6e 64 20 63 6c 65 61 6e 20 75 70 20 74 68 65  and clean up the
2f2c3 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a 2a 20   recursive.  ** 
2f2c4 6d 75 74 65 78 20 74 6f 20 70 72 65 76 65 6e 74  mutex to prevent
2f2c5 20 61 20 72 65 73 6f 75 72 63 65 20 6c 65 61 6b   a resource leak
2f2c6 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
2f2c7 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61  _mutex_enter(pMa
2f2c8 73 74 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ster);.  sqlite3
2f2c9 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65  GlobalConfig.nRe
2f2ca 66 49 6e 69 74 4d 75 74 65 78 2d 2d 3b 0a 20 20  fInitMutex--;.  
2f2cb 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
2f2cc 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74  lConfig.nRefInit
2f2cd 4d 75 74 65 78 3c 3d 30 20 29 7b 0a 20 20 20 20  Mutex<=0 ){.    
2f2ce 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 47  assert( sqlite3G
2f2cf 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66  lobalConfig.nRef
2f2d0 49 6e 69 74 4d 75 74 65 78 3d 3d 30 20 29 3b 0a  InitMutex==0 );.
2f2d1 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
2f2d2 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 47 6c  x_free(sqlite3Gl
2f2d3 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74  obalConfig.pInit
2f2d4 4d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69  Mutex);.    sqli
2f2d5 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2f2d6 70 49 6e 69 74 4d 75 74 65 78 20 3d 20 30 3b 0a  pInitMutex = 0;.
2f2d7 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
2f2d8 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65  tex_leave(pMaste
2f2d9 72 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f  r);..  /* The fo
2f2da 6c 6c 6f 77 69 6e 67 20 69 73 20 6a 75 73 74 20  llowing is just 
2f2db 61 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 74  a sanity check t
2f2dc 6f 20 6d 61 6b 65 20 73 75 72 65 20 53 51 4c 69  o make sure SQLi
2f2dd 74 65 20 68 61 73 0a 20 20 2a 2a 20 62 65 65 6e  te has.  ** been
2f2de 20 63 6f 6d 70 69 6c 65 64 20 63 6f 72 72 65 63   compiled correc
2f2df 74 6c 79 2e 20 20 49 74 20 69 73 20 69 6d 70 6f  tly.  It is impo
2f2e0 72 74 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 69  rtant to run thi
2f2e1 73 20 63 6f 64 65 2c 20 62 75 74 0a 20 20 2a 2a  s code, but.  **
2f2e2 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74   we don't want t
2f2e3 6f 20 72 75 6e 20 69 74 20 74 6f 6f 20 6f 66 74  o run it too oft
2f2e4 65 6e 20 61 6e 64 20 73 6f 61 6b 20 75 70 20 43  en and soak up C
2f2e5 50 55 20 63 79 63 6c 65 73 20 66 6f 72 20 6e 6f  PU cycles for no
2f2e6 0a 20 20 2a 2a 20 72 65 61 73 6f 6e 2e 20 20 53  .  ** reason.  S
2f2e7 6f 20 77 65 20 72 75 6e 20 69 74 20 6f 6e 63 65  o we run it once
2f2e8 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69   during initiali
2f2e9 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66  zation..  */.#if
2f2ea 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 69 66 6e  ndef NDEBUG.#ifn
2f2eb 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2f2ec 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
2f2ed 20 2f 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e   /* This section
2f2ee 20 6f 66 20 63 6f 64 65 27 73 20 6f 6e 6c 79 20   of code's only 
2f2ef 22 6f 75 74 70 75 74 22 20 69 73 20 76 69 61 20  "output" is via 
2f2f0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
2f2f1 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 20 28 20 72  nts. */.  if ( r
2f2f2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2f2f3 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28 75      u64 x = (((u
2f2f4 36 34 29 31 29 3c 3c 36 33 29 2d 31 3b 0a 20 20  64)1)<<63)-1;.  
2f2f5 20 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20 20    double y;.    
2f2f6 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29  assert(sizeof(x)
2f2f7 3d 3d 38 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ==8);.    assert
2f2f8 28 73 69 7a 65 6f 66 28 78 29 3d 3d 73 69 7a 65  (sizeof(x)==size
2f2f9 6f 66 28 79 29 29 3b 0a 20 20 20 20 6d 65 6d 63  of(y));.    memc
2f2fa 70 79 28 26 79 2c 20 26 78 2c 20 38 29 3b 0a 20  py(&y, &x, 8);. 
2f2fb 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
2f2fc 65 33 49 73 4e 61 4e 28 79 29 20 29 3b 0a 20 20  e3IsNaN(y) );.  
2f2fd 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  }.#endif.#endif.
2f2fe 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2f2ff 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20  ./*.** Undo the 
2f300 65 66 66 65 63 74 73 20 6f 66 20 73 71 6c 69 74  effects of sqlit
2f301 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e  e3_initialize().
2f302 20 20 4d 75 73 74 20 6e 6f 74 20 62 65 20 63 61    Must not be ca
2f303 6c 6c 65 64 20 77 68 69 6c 65 0a 2a 2a 20 74 68  lled while.** th
2f304 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64  ere are outstand
2f305 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ing database con
2f306 6e 65 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f  nections or memo
2f307 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f  ry allocations o
2f308 72 0a 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20 70  r.** while any p
2f309 61 72 74 20 6f 66 20 53 51 4c 69 74 65 20 69 73  art of SQLite is
2f30a 20 6f 74 68 65 72 77 69 73 65 20 69 6e 20 75 73   otherwise in us
2f30b 65 20 69 6e 20 61 6e 79 20 74 68 72 65 61 64 2e  e in any thread.
2f30c 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
2f30d 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73  e is not threads
2f30e 61 66 65 2e 20 20 4e 6f 74 20 62 79 20 61 20 6c  afe.  Not by a l
2f30f 6f 6e 67 20 73 68 6f 74 2e 0a 2a 2f 0a 53 51 4c  ong shot..*/.SQL
2f310 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2f311 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76 6f 69  te3_shutdown(voi
2f312 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f  d){.  sqlite3Glo
2f313 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c  balConfig.isMall
2f314 6f 63 49 6e 69 74 20 3d 20 30 3b 0a 20 20 73 71  ocInit = 0;.  sq
2f315 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64  lite3PcacheShutd
2f316 6f 77 6e 28 29 3b 0a 20 20 69 66 28 20 73 71 6c  own();.  if( sql
2f317 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2f318 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 73  .isInit ){.    s
2f319 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 3b  qlite3_os_end();
2f31a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d 61  .  }.  sqlite3Ma
2f31b 6c 6c 6f 63 45 6e 64 28 29 3b 0a 20 20 73 71 6c  llocEnd();.  sql
2f31c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 29 3b 0a  ite3MutexEnd();.
2f31d 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2f31e 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 3d 20 30  onfig.isInit = 0
2f31f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2f320 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
2f321 68 69 73 20 41 50 49 20 61 6c 6c 6f 77 73 20 61  his API allows a
2f322 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f 20 6d  pplications to m
2f323 6f 64 69 66 79 20 74 68 65 20 67 6c 6f 62 61 6c  odify the global
2f324 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
2f325 66 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20  f.** the SQLite 
2f326 6c 69 62 72 61 72 79 20 61 74 20 72 75 6e 2d 74  library at run-t
2f327 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ime..**.** This 
2f328 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f  routine should o
2f329 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68  nly be called wh
2f32a 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  en there are no 
2f32b 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 64  outstanding.** d
2f32c 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2f32d 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c  ons or memory al
2f32e 6c 6f 63 61 74 69 6f 6e 73 2e 20 20 54 68 69 73  locations.  This
2f32f 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 0a   routine is not.
2f330 2a 2a 20 74 68 72 65 61 64 73 61 66 65 2e 20 20  ** threadsafe.  
2f331 46 61 69 6c 75 72 65 20 74 6f 20 68 65 65 64 20  Failure to heed 
2f332 74 68 65 73 65 20 77 61 72 6e 69 6e 67 73 20 63  these warnings c
2f333 61 6e 20 6c 65 61 64 20 74 6f 20 75 6e 70 72 65  an lead to unpre
2f334 64 69 63 74 61 62 6c 65 0a 2a 2a 20 62 65 68 61  dictable.** beha
2f335 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  vior..*/.SQLITE_
2f336 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2f337 63 6f 6e 66 69 67 28 69 6e 74 20 6f 70 2c 20 2e  config(int op, .
2f338 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
2f339 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  p;.  int rc = SQ
2f33a 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 73  LITE_OK;..  /* s
2f33b 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
2f33c 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c  shall return SQL
2f33d 49 54 45 5f 4d 49 53 55 53 45 20 69 66 20 69 74  ITE_MISUSE if it
2f33e 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c   is invoked whil
2f33f 65 0a 20 20 2a 2a 20 74 68 65 20 53 51 4c 69 74  e.  ** the SQLit
2f340 65 20 6c 69 62 72 61 72 79 20 69 73 20 69 6e 20  e library is in 
2f341 75 73 65 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71  use. */.  if( sq
2f342 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2f343 67 2e 69 73 49 6e 69 74 20 29 20 72 65 74 75 72  g.isInit ) retur
2f344 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
2f345 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ..  va_start(ap,
2f346 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20   op);.  switch( 
2f347 6f 70 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 4d 75  op ){..    /* Mu
2f348 74 65 78 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  tex configuratio
2f349 6e 20 6f 70 74 69 6f 6e 73 20 61 72 65 20 6f 6e  n options are on
2f34a 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ly available in 
2f34b 61 20 74 68 72 65 61 64 73 61 66 65 0a 20 20 20  a threadsafe.   
2f34c 20 2a 2a 20 63 6f 6d 70 69 6c 65 2e 20 0a 20 20   ** compile. .  
2f34d 20 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f    */.#if SQLITE_
2f34e 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 63  THREADSAFE.    c
2f34f 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
2f350 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 3a 20  G_SINGLETHREAD: 
2f351 7b 0a 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62  {.      /* Disab
2f352 6c 65 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20  le all mutexing 
2f353 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2f354 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
2f355 72 65 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20  reMutex = 0;.   
2f356 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2f357 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65  Config.bFullMute
2f358 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65  x = 0;.      bre
2f359 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2f35a 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
2f35b 5f 4d 55 4c 54 49 54 48 52 45 41 44 3a 20 7b 0a  _MULTITHREAD: {.
2f35c 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65        /* Disable
2f35d 20 6d 75 74 65 78 69 6e 67 20 6f 66 20 64 61 74   mutexing of dat
2f35e 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2f35f 73 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 45 6e  s */.      /* En
2f360 61 62 6c 65 20 6d 75 74 65 78 69 6e 67 20 6f 66  able mutexing of
2f361 20 63 6f 72 65 20 64 61 74 61 20 73 74 72 75 63   core data struc
2f362 74 75 72 65 73 20 2a 2f 0a 20 20 20 20 20 20 73  tures */.      s
2f363 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2f364 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20  ig.bCoreMutex = 
2f365 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  1;.      sqlite3
2f366 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75  GlobalConfig.bFu
2f367 6c 6c 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20  llMutex = 0;.   
2f368 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2f369 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f36a 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45  CONFIG_SERIALIZE
2f36b 44 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45 6e  D: {.      /* En
2f36c 61 62 6c 65 20 61 6c 6c 20 6d 75 74 65 78 69 6e  able all mutexin
2f36d 67 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  g */.      sqlit
2f36e 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
2f36f 43 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20  CoreMutex = 1;. 
2f370 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2f371 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75  alConfig.bFullMu
2f372 74 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 62  tex = 1;.      b
2f373 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2f374 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
2f375 49 47 5f 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20  IG_MUTEX: {.    
2f376 20 20 2f 2a 20 53 70 65 63 69 66 79 20 61 6e 20    /* Specify an 
2f377 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 75 74 65  alternative mute
2f378 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
2f379 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2f37a 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
2f37b 74 65 78 20 3d 20 2a 76 61 5f 61 72 67 28 61 70  tex = *va_arg(ap
2f37c 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  , sqlite3_mutex_
2f37d 6d 65 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20  methods*);.     
2f37e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2f37f 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
2f380 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3a 20 7b  NFIG_GETMUTEX: {
2f381 0a 20 20 20 20 20 20 2f 2a 20 52 65 74 72 69 65  .      /* Retrie
2f382 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d  ve the current m
2f383 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
2f384 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 2a 76 61  ion */.      *va
2f385 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33  _arg(ap, sqlite3
2f386 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29  _mutex_methods*)
2f387 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
2f388 43 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 20 20  Config.mutex;.  
2f389 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2f38a 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20 63 61  .#endif...    ca
2f38b 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
2f38c 5f 4d 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20  _MALLOC: {.     
2f38d 20 2f 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61   /* Specify an a
2f38e 6c 74 65 72 6e 61 74 69 76 65 20 6d 61 6c 6c 6f  lternative mallo
2f38f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  c implementation
2f390 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2f391 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20  3GlobalConfig.m 
2f392 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71  = *va_arg(ap, sq
2f393 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
2f394 73 2a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  s*);.      break
2f395 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2f396 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47   SQLITE_CONFIG_G
2f397 45 54 4d 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20  ETMALLOC: {.    
2f398 20 20 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68    /* Retrieve th
2f399 65 20 63 75 72 72 65 6e 74 20 6d 61 6c 6c 6f 63  e current malloc
2f39a 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  () implementatio
2f39b 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73  n */.      if( s
2f39c 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2f39d 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20  ig.m.xMalloc==0 
2f39e 29 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44  ) sqlite3MemSetD
2f39f 65 66 61 75 6c 74 28 29 3b 0a 20 20 20 20 20 20  efault();.      
2f3a0 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69  *va_arg(ap, sqli
2f3a1 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a  te3_mem_methods*
2f3a2 29 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ) = sqlite3Globa
2f3a3 6c 43 6f 6e 66 69 67 2e 6d 3b 0a 20 20 20 20 20  lConfig.m;.     
2f3a4 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2f3a5 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
2f3a6 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 3a 20  NFIG_MEMSTATUS: 
2f3a7 7b 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c  {.      /* Enabl
2f3a8 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65  e or disable the
2f3a9 20 6d 61 6c 6c 6f 63 20 73 74 61 74 75 73 20 63   malloc status c
2f3aa 6f 6c 6c 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20  ollection */.   
2f3ab 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2f3ac 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20  Config.bMemstat 
2f3ad 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
2f3ae 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2f3af 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
2f3b0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52  QLITE_CONFIG_SCR
2f3b1 41 54 43 48 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  ATCH: {.      /*
2f3b2 20 44 65 73 69 67 6e 61 74 65 20 61 20 62 75 66   Designate a buf
2f3b3 66 65 72 20 66 6f 72 20 73 63 72 61 74 63 68 20  fer for scratch 
2f3b4 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a  memory space */.
2f3b5 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
2f3b6 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
2f3b7 63 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ch = va_arg(ap, 
2f3b8 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71  void*);.      sq
2f3b9 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2f3ba 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 76 61  g.szScratch = va
2f3bb 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
2f3bc 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2f3bd 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63  alConfig.nScratc
2f3be 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  h = va_arg(ap, i
2f3bf 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  nt);.      break
2f3c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2f3c1 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
2f3c2 41 47 45 43 41 43 48 45 3a 20 7b 0a 20 20 20 20  AGECACHE: {.    
2f3c3 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65 20 61    /* Designate a
2f3c4 20 62 75 66 66 65 72 20 66 6f 72 20 73 63 72 61   buffer for scra
2f3c5 74 63 68 20 6d 65 6d 6f 72 79 20 73 70 61 63 65  tch memory space
2f3c6 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2f3c7 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50  3GlobalConfig.pP
2f3c8 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  age = va_arg(ap,
2f3c9 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73   void*);.      s
2f3ca 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2f3cb 69 67 2e 73 7a 50 61 67 65 20 3d 20 76 61 5f 61  ig.szPage = va_a
2f3cc 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
2f3cd 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2f3ce 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 3d 20 76  Config.nPage = v
2f3cf 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
2f3d0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2f3d1 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c   }..    case SQL
2f3d2 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
2f3d3 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70  E: {.      /* Sp
2f3d4 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61  ecify an alterna
2f3d5 74 69 76 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c  tive malloc impl
2f3d6 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  ementation */.  
2f3d7 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2f3d8 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 20 3d  lConfig.pcache =
2f3d9 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c   *va_arg(ap, sql
2f3da 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
2f3db 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62 72 65  ods*);.      bre
2f3dc 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63  ak;.    }..    c
2f3dd 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
2f3de 47 5f 47 45 54 50 43 41 43 48 45 3a 20 7b 0a 20  G_GETPCACHE: {. 
2f3df 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2f3e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
2f3e1 63 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a  che.xInit==0 ){.
2f3e2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
2f3e3 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28  CacheSetDefault(
2f3e4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2f3e5 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c   *va_arg(ap, sql
2f3e6 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
2f3e7 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65 33 47  ods*) = sqlite3G
2f3e8 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
2f3e9 68 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  he;.      break;
2f3ea 0a 20 20 20 20 7d 0a 0a 23 69 66 20 64 65 66 69  .    }..#if defi
2f3eb 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
2f3ec 45 5f 4d 45 4d 53 59 53 33 29 20 7c 7c 20 64 65  E_MEMSYS3) || de
2f3ed 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
2f3ee 42 4c 45 5f 4d 45 4d 53 59 53 35 29 0a 20 20 20  BLE_MEMSYS5).   
2f3ef 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
2f3f0 46 49 47 5f 48 45 41 50 3a 20 7b 0a 20 20 20 20  FIG_HEAP: {.    
2f3f1 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65 20 61    /* Designate a
2f3f2 20 62 75 66 66 65 72 20 66 6f 72 20 68 65 61 70   buffer for heap
2f3f3 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f   memory space */
2f3f4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
2f3f5 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70  obalConfig.pHeap
2f3f6 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
2f3f7 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  id*);.      sqli
2f3f8 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2f3f9 6e 48 65 61 70 20 3d 20 76 61 5f 61 72 67 28 61  nHeap = va_arg(a
2f3fa 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73  p, int);.      s
2f3fb 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2f3fc 69 67 2e 6d 6e 52 65 71 20 3d 20 76 61 5f 61 72  ig.mnReq = va_ar
2f3fd 67 28 61 70 2c 20 69 6e 74 29 3b 0a 0a 20 20 20  g(ap, int);..   
2f3fe 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c     if( sqlite3Gl
2f3ff 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70  obalConfig.pHeap
2f400 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
2f401 2a 20 49 66 20 74 68 65 20 68 65 61 70 20 70 6f  * If the heap po
2f402 69 6e 74 65 72 20 69 73 20 4e 55 4c 4c 2c 20 74  inter is NULL, t
2f403 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68 65 20  hen restore the 
2f404 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74  malloc implement
2f405 61 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  ation.        **
2f406 20 62 61 63 6b 20 74 6f 20 4e 55 4c 4c 20 70 6f   back to NULL po
2f407 69 6e 74 65 72 73 20 74 6f 6f 2e 20 20 54 68 69  inters too.  Thi
2f408 73 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65  s will cause the
2f409 20 6d 61 6c 6c 6f 63 20 74 6f 20 67 6f 0a 20 20   malloc to go.  
2f40a 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f        ** back to
2f40b 20 69 74 73 20 64 65 66 61 75 6c 74 20 69 6d 70   its default imp
2f40c 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 6e  lementation when
2f40d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
2f40e 69 7a 65 28 29 20 69 73 0a 20 20 20 20 20 20 20  ize() is.       
2f40f 20 2a 2a 20 72 75 6e 2e 0a 20 20 20 20 20 20 20   ** run..       
2f410 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d 65 6d 73   */.        mems
2f411 65 74 28 26 73 71 6c 69 74 65 33 47 6c 6f 62 61  et(&sqlite3Globa
2f412 6c 43 6f 6e 66 69 67 2e 6d 2c 20 30 2c 20 73 69  lConfig.m, 0, si
2f413 7a 65 6f 66 28 73 71 6c 69 74 65 33 47 6c 6f 62  zeof(sqlite3Glob
2f414 61 6c 43 6f 6e 66 69 67 2e 6d 29 29 3b 0a 20 20  alConfig.m));.  
2f415 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2f416 20 20 20 2f 2a 20 54 68 65 20 68 65 61 70 20 70     /* The heap p
2f417 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55  ointer is not NU
2f418 4c 4c 2c 20 74 68 65 6e 20 69 6e 73 74 61 6c 6c  LL, then install
2f419 20 6f 6e 65 20 6f 66 20 74 68 65 0a 20 20 20 20   one of the.    
2f41a 20 20 20 20 2a 2a 20 6d 65 6d 35 2e 63 2f 6d 65      ** mem5.c/me
2f41b 6d 33 2e 63 20 6d 65 74 68 6f 64 73 2e 20 49 66  m3.c methods. If
2f41c 20 6e 65 69 74 68 65 72 20 45 4e 41 42 4c 45 5f   neither ENABLE_
2f41d 4d 45 4d 53 59 53 33 20 6e 6f 72 0a 20 20 20 20  MEMSYS3 nor.    
2f41e 20 20 20 20 2a 2a 20 45 4e 41 42 4c 45 5f 4d 45      ** ENABLE_ME
2f41f 4d 53 59 53 35 20 69 73 20 64 65 66 69 6e 65 64  MSYS5 is defined
2f420 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
2f421 72 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  r..        ** th
2f422 65 20 64 65 66 61 75 6c 74 20 63 61 73 65 20 61  e default case a
2f423 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  nd return an err
2f424 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23  or..        */.#
2f425 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
2f426 42 4c 45 5f 4d 45 4d 53 59 53 33 0a 20 20 20 20  BLE_MEMSYS3.    
2f427 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2f428 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c  lConfig.m = *sql
2f429 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73  ite3MemGetMemsys
2f42a 33 28 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64  3();.#endif.#ifd
2f42b 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2f42c 5f 4d 45 4d 53 59 53 35 0a 20 20 20 20 20 20 20  _MEMSYS5.       
2f42d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2f42e 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65  nfig.m = *sqlite
2f42f 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 29  3MemGetMemsys5()
2f430 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d  ;.#endif.      }
2f431 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2f432 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
2f433 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
2f434 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b 0a  IG_LOOKASIDE: {.
2f435 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
2f436 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b  balConfig.szLook
2f437 61 73 69 64 65 20 3d 20 76 61 5f 61 72 67 28 61  aside = va_arg(a
2f438 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73  p, int);.      s
2f439 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2f43a 69 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65 20 3d 20  ig.nLookaside = 
2f43b 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
2f43c 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2f43d 20 20 7d 0a 0a 20 20 20 20 64 65 66 61 75 6c 74    }..    default
2f43e 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  : {.      rc = S
2f43f 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2f440 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2f441 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29    }.  va_end(ap)
2f442 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2f443 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 74  ../*.** Set up t
2f444 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66  he lookaside buf
2f445 66 65 72 73 20 66 6f 72 20 61 20 64 61 74 61 62  fers for a datab
2f446 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
2f447 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
2f448 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  _OK on success. 
2f449 20 0a 2a 2a 20 49 66 20 6c 6f 6f 6b 61 73 69 64   .** If lookasid
2f44a 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 63 74  e is already act
2f44b 69 76 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ive, return SQLI
2f44c 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 54  TE_BUSY..**.** T
2f44d 68 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72 20  he sz parameter 
2f44e 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2f44f 20 62 79 74 65 73 20 69 6e 20 65 61 63 68 20 6c   bytes in each l
2f450 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74 2e 0a 2a  ookaside slot..*
2f451 2a 20 54 68 65 20 63 6e 74 20 70 61 72 61 6d 65  * The cnt parame
2f452 74 65 72 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ter is the numbe
2f453 72 20 6f 66 20 73 6c 6f 74 73 2e 20 20 49 66 20  r of slots.  If 
2f454 70 53 74 61 72 74 20 69 73 20 4e 55 4c 4c 20 74  pStart is NULL t
2f455 68 65 0a 2a 2a 20 73 70 61 63 65 20 66 6f 72 20  he.** space for 
2f456 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  the lookaside me
2f457 6d 6f 72 79 20 69 73 20 6f 62 74 61 69 6e 65 64  mory is obtained
2f458 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
2f459 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 70 53  lloc()..** If pS
2f45a 74 61 72 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  tart is not NULL
2f45b 20 74 68 65 6e 20 69 74 20 69 73 20 73 7a 2a 63   then it is sz*c
2f45c 6e 74 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  nt bytes of memo
2f45d 72 79 20 74 6f 20 75 73 65 20 66 6f 72 0a 2a 2a  ry to use for.**
2f45e 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 6d   the lookaside m
2f45f 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  emory..*/.static
2f460 20 69 6e 74 20 73 65 74 75 70 4c 6f 6f 6b 61 73   int setupLookas
2f461 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ide(sqlite3 *db,
2f462 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74   void *pBuf, int
2f463 20 73 7a 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20   sz, int cnt){. 
2f464 20 76 6f 69 64 20 2a 70 53 74 61 72 74 3b 0a 20   void *pStart;. 
2f465 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   if( db->lookasi
2f466 64 65 2e 6e 4f 75 74 20 29 7b 0a 20 20 20 20 72  de.nOut ){.    r
2f467 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
2f468 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 72 65 65  Y;.  }.  /* Free
2f469 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 6c 6f   any existing lo
2f46a 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 66  okaside buffer f
2f46b 6f 72 20 74 68 69 73 20 68 61 6e 64 6c 65 20 62  or this handle b
2f46c 65 66 6f 72 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63  efore.  ** alloc
2f46d 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 20  ating a new one 
2f46e 73 6f 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65  so we don't have
2f46f 20 74 6f 20 68 61 76 65 20 73 70 61 63 65 20 66   to have space f
2f470 6f 72 20 0a 20 20 2a 2a 20 62 6f 74 68 20 61 74  or .  ** both at
2f471 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a   the same time..
2f472 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6c    */.  if( db->l
2f473 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63  ookaside.bMalloc
2f474 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
2f475 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f 6b 61  3_free(db->looka
2f476 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a 20 20  side.pStart);.  
2f477 7d 0a 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20  }.  /* The size 
2f478 6f 66 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 73  of a lookaside s
2f479 6c 6f 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  lot needs to be 
2f47a 6c 61 72 67 65 72 20 74 68 61 6e 20 61 20 70 6f  larger than a po
2f47b 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 62 65  inter.  ** to be
2f47c 20 75 73 65 66 75 6c 2e 0a 20 20 2a 2f 0a 20 20   useful..  */.  
2f47d 69 66 28 20 73 7a 3c 3d 28 69 6e 74 29 73 69 7a  if( sz<=(int)siz
2f47e 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f  eof(LookasideSlo
2f47f 74 2a 29 20 29 20 73 7a 20 3d 20 30 3b 0a 20 20  t*) ) sz = 0;.  
2f480 69 66 28 20 63 6e 74 3c 30 20 29 20 63 6e 74 20  if( cnt<0 ) cnt 
2f481 3d 20 30 3b 0a 20 20 69 66 28 20 73 7a 3d 3d 30  = 0;.  if( sz==0
2f482 20 7c 7c 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20   || cnt==0 ){.  
2f483 20 20 73 7a 20 3d 20 30 3b 0a 20 20 20 20 70 53    sz = 0;.    pS
2f484 74 61 72 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  tart = 0;.  }els
2f485 65 20 69 66 28 20 70 42 75 66 3d 3d 30 20 29 7b  e if( pBuf==0 ){
2f486 0a 20 20 20 20 73 7a 20 3d 20 52 4f 55 4e 44 38  .    sz = ROUND8
2f487 28 73 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (sz);.    sqlite
2f488 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
2f489 6f 63 28 29 3b 0a 20 20 20 20 70 53 74 61 72 74  oc();.    pStart
2f48a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
2f48b 28 20 73 7a 2a 63 6e 74 20 29 3b 0a 20 20 20 20  ( sz*cnt );.    
2f48c 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
2f48d 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 65 6c 73  Malloc();.  }els
2f48e 65 7b 0a 20 20 20 20 73 7a 20 3d 20 52 4f 55 4e  e{.    sz = ROUN
2f48f 44 44 4f 57 4e 38 28 73 7a 29 3b 0a 20 20 20 20  DDOWN8(sz);.    
2f490 70 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20  pStart = pBuf;. 
2f491 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   }.  db->lookasi
2f492 64 65 2e 70 53 74 61 72 74 20 3d 20 70 53 74 61  de.pStart = pSta
2f493 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  rt;.  db->lookas
2f494 69 64 65 2e 70 46 72 65 65 20 3d 20 30 3b 0a 20  ide.pFree = 0;. 
2f495 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73   db->lookaside.s
2f496 7a 20 3d 20 28 75 31 36 29 73 7a 3b 0a 20 20 69  z = (u16)sz;.  i
2f497 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20 20  f( pStart ){.   
2f498 20 69 6e 74 20 69 3b 0a 20 20 20 20 4c 6f 6f 6b   int i;.    Look
2f499 61 73 69 64 65 53 6c 6f 74 20 2a 70 3b 0a 20 20  asideSlot *p;.  
2f49a 20 20 61 73 73 65 72 74 28 20 73 7a 20 3e 20 73    assert( sz > s
2f49b 69 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65 53  izeof(LookasideS
2f49c 6c 6f 74 2a 29 20 29 3b 0a 20 20 20 20 70 20 3d  lot*) );.    p =
2f49d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a   (LookasideSlot*
2f49e 29 70 53 74 61 72 74 3b 0a 20 20 20 20 66 6f 72  )pStart;.    for
2f49f 28 69 3d 63 6e 74 2d 31 3b 20 69 3e 3d 30 3b 20  (i=cnt-1; i>=0; 
2f4a0 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  i--){.      p->p
2f4a1 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  Next = db->looka
2f4a2 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 20  side.pFree;.    
2f4a3 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
2f4a4 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20  pFree = p;.     
2f4a5 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53   p = (LookasideS
2f4a6 6c 6f 74 2a 29 26 28 28 75 38 2a 29 70 29 5b 73  lot*)&((u8*)p)[s
2f4a7 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  z];.    }.    db
2f4a8 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64  ->lookaside.pEnd
2f4a9 20 3d 20 70 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = p;.    db->lo
2f4aa 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
2f4ab 20 3d 20 31 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = 1;.    db->lo
2f4ac 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65  okaside.bMalloce
2f4ad 64 20 3d 20 70 42 75 66 3d 3d 30 20 3f 31 3a 30  d = pBuf==0 ?1:0
2f4ae 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
2f4af 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e  b->lookaside.pEn
2f4b0 64 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c  d = 0;.    db->l
2f4b1 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
2f4b2 64 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c  d = 0;.    db->l
2f4b3 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63  ookaside.bMalloc
2f4b4 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ed = 0;.  }.  re
2f4b5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2f4b6 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2f4b7 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69  the mutex associ
2f4b8 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74 61  ated with a data
2f4b9 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
2f4ba 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
2f4bb 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
2f4bc 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73  lite3_db_mutex(s
2f4bd 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72  qlite3 *db){.  r
2f4be 65 74 75 72 6e 20 64 62 2d 3e 6d 75 74 65 78 3b  eturn db->mutex;
2f4bf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67  .}../*.** Config
2f4c0 75 72 61 74 69 6f 6e 20 73 65 74 74 69 6e 67 73  uration settings
2f4c1 20 66 6f 72 20 61 6e 20 69 6e 64 69 76 69 64 75   for an individu
2f4c2 61 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  al database conn
2f4c3 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45  ection.*/.SQLITE
2f4c4 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2f4c5 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74  _db_config(sqlit
2f4c6 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 70 2c 20  e3 *db, int op, 
2f4c7 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
2f4c8 61 70 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  ap;.  int rc;.  
2f4c9 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29  va_start(ap, op)
2f4ca 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  ;.  switch( op )
2f4cb 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
2f4cc 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  E_DBCONFIG_LOOKA
2f4cd 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20 76 6f  SIDE: {.      vo
2f4ce 69 64 20 2a 70 42 75 66 20 3d 20 76 61 5f 61 72  id *pBuf = va_ar
2f4cf 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20  g(ap, void*);.  
2f4d0 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61 5f      int sz = va_
2f4d1 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
2f4d2 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 76 61      int cnt = va
2f4d3 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
2f4d4 20 20 20 20 20 72 63 20 3d 20 73 65 74 75 70 4c       rc = setupL
2f4d5 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 42 75  ookaside(db, pBu
2f4d6 66 2c 20 73 7a 2c 20 63 6e 74 29 3b 0a 20 20 20  f, sz, cnt);.   
2f4d7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2f4d8 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
2f4d9 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
2f4da 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72  _ERROR;.      br
2f4db 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
2f4dc 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72   va_end(ap);.  r
2f4dd 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
2f4de 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
2f4df 69 66 20 74 68 65 20 62 75 66 66 65 72 20 7a 5b  if the buffer z[
2f4e0 30 2e 2e 6e 2d 31 5d 20 63 6f 6e 74 61 69 6e 73  0..n-1] contains
2f4e1 20 61 6c 6c 20 73 70 61 63 65 73 2e 0a 2a 2f 0a   all spaces..*/.
2f4e2 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 53 70  static int allSp
2f4e3 61 63 65 73 28 63 6f 6e 73 74 20 63 68 61 72 20  aces(const char 
2f4e4 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 77 68  *z, int n){.  wh
2f4e5 69 6c 65 28 20 6e 3e 30 20 26 26 20 7a 5b 6e 2d  ile( n>0 && z[n-
2f4e6 31 5d 3d 3d 27 20 27 20 29 7b 20 6e 2d 2d 3b 20  1]==' ' ){ n--; 
2f4e7 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3d 3d 30 3b  }.  return n==0;
2f4e8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
2f4e9 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f  s the default co
2f4ea 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
2f4eb 20 6e 61 6d 65 64 20 22 42 49 4e 41 52 59 22 20   named "BINARY" 
2f4ec 77 68 69 63 68 20 69 73 20 61 6c 77 61 79 73 0a  which is always.
2f4ed 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a  ** available..**
2f4ee 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 64 46 6c  .** If the padFl
2f4ef 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  ag argument is n
2f4f0 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 73 70 61  ot NULL then spa
2f4f1 63 65 20 70 61 64 64 69 6e 67 20 61 74 20 74 68  ce padding at th
2f4f2 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 73 74 72 69  e end.** of stri
2f4f3 6e 67 73 20 69 73 20 69 67 6e 6f 72 65 64 2e 20  ngs is ignored. 
2f4f4 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 73   This implements
2f4f5 20 74 68 65 20 52 54 52 49 4d 20 63 6f 6c 6c 61   the RTRIM colla
2f4f6 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
2f4f7 69 6e 74 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 28  int binCollFunc(
2f4f8 0a 20 20 76 6f 69 64 20 2a 70 61 64 46 6c 61 67  .  void *padFlag
2f4f9 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63  ,.  int nKey1, c
2f4fa 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
2f4fb 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63  ,.  int nKey2, c
2f4fc 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
2f4fd 0a 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 3b  .){.  int rc, n;
2f4fe 0a 20 20 6e 20 3d 20 6e 4b 65 79 31 3c 6e 4b 65  .  n = nKey1<nKe
2f4ff 79 32 20 3f 20 6e 4b 65 79 31 20 3a 20 6e 4b 65  y2 ? nKey1 : nKe
2f500 79 32 3b 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d  y2;.  rc = memcm
2f501 70 28 70 4b 65 79 31 2c 20 70 4b 65 79 32 2c 20  p(pKey1, pKey2, 
2f502 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  n);.  if( rc==0 
2f503 29 7b 0a 20 20 20 20 69 66 28 20 70 61 64 46 6c  ){.    if( padFl
2f504 61 67 0a 20 20 20 20 20 26 26 20 61 6c 6c 53 70  ag.     && allSp
2f505 61 63 65 73 28 28 28 63 68 61 72 2a 29 70 4b 65  aces(((char*)pKe
2f506 79 31 29 2b 6e 2c 20 6e 4b 65 79 31 2d 6e 29 0a  y1)+n, nKey1-n).
2f507 20 20 20 20 20 26 26 20 61 6c 6c 53 70 61 63 65       && allSpace
2f508 73 28 28 28 63 68 61 72 2a 29 70 4b 65 79 32 29  s(((char*)pKey2)
2f509 2b 6e 2c 20 6e 4b 65 79 32 2d 6e 29 0a 20 20 20  +n, nKey2-n).   
2f50a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 61   ){.      /* Lea
2f50b 76 65 20 72 63 20 75 6e 63 68 61 6e 67 65 64 20  ve rc unchanged 
2f50c 61 74 20 30 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  at 0 */.    }els
2f50d 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 4b  e{.      rc = nK
2f50e 65 79 31 20 2d 20 6e 4b 65 79 32 3b 0a 20 20 20  ey1 - nKey2;.   
2f50f 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2f510 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 6f  rc;.}../*.** Ano
2f511 74 68 65 72 20 62 75 69 6c 74 2d 69 6e 20 63 6f  ther built-in co
2f512 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2f513 3a 20 4e 4f 43 41 53 45 2e 20 0a 2a 2a 0a 2a 2a  : NOCASE. .**.**
2f514 20 54 68 69 73 20 63 6f 6c 6c 61 74 69 6e 67 20   This collating 
2f515 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e 74 65  sequence is inte
2f516 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 20  nded to be used 
2f517 66 6f 72 20 22 63 61 73 65 20 69 6e 64 65 70 65  for "case indepe
2f518 6e 64 61 6e 74 0a 2a 2a 20 63 6f 6d 70 61 72 69  ndant.** compari
2f519 73 6f 6e 22 2e 20 53 51 4c 69 74 65 27 73 20 6b  son". SQLite's k
2f51a 6e 6f 77 6c 65 64 67 65 20 6f 66 20 75 70 70 65  nowledge of uppe
2f51b 72 20 61 6e 64 20 6c 6f 77 65 72 20 63 61 73 65  r and lower case
2f51c 20 65 71 75 69 76 61 6c 65 6e 74 73 0a 2a 2a 20   equivalents.** 
2f51d 65 78 74 65 6e 64 73 20 6f 6e 6c 79 20 74 6f 20  extends only to 
2f51e 74 68 65 20 32 36 20 63 68 61 72 61 63 74 65 72  the 26 character
2f51f 73 20 75 73 65 64 20 69 6e 20 74 68 65 20 45 6e  s used in the En
2f520 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 2e 0a  glish language..
2f521 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 6d 6f 6d  **.** At the mom
2f522 65 6e 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c  ent there is onl
2f523 79 20 61 20 55 54 46 2d 38 20 69 6d 70 6c 65 6d  y a UTF-8 implem
2f524 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  entation..*/.sta
2f525 74 69 63 20 69 6e 74 20 6e 6f 63 61 73 65 43 6f  tic int nocaseCo
2f526 6c 6c 61 74 69 6e 67 46 75 6e 63 28 0a 20 20 76  llatingFunc(.  v
2f527 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 0a 20 20  oid *NotUsed,.  
2f528 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74  int nKey1, const
2f529 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20   void *pKey1,.  
2f52a 69 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74  int nKey2, const
2f52b 20 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a   void *pKey2.){.
2f52c 20 20 69 6e 74 20 72 20 3d 20 73 71 6c 69 74 65    int r = sqlite
2f52d 33 53 74 72 4e 49 43 6d 70 28 0a 20 20 20 20 20  3StrNICmp(.     
2f52e 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
2f52f 4b 65 79 31 2c 20 28 63 6f 6e 73 74 20 63 68 61  Key1, (const cha
2f530 72 20 2a 29 70 4b 65 79 32 2c 20 28 6e 4b 65 79  r *)pKey2, (nKey
2f531 31 3c 6e 4b 65 79 32 29 3f 6e 4b 65 79 31 3a 6e  1<nKey2)?nKey1:n
2f532 4b 65 79 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f  Key2);.  UNUSED_
2f533 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
2f534 64 29 3b 0a 20 20 69 66 28 20 30 3d 3d 72 20 29  d);.  if( 0==r )
2f535 7b 0a 20 20 20 20 72 20 3d 20 6e 4b 65 79 31 2d  {.    r = nKey1-
2f536 6e 4b 65 79 32 3b 0a 20 20 7d 0a 20 20 72 65 74  nKey2;.  }.  ret
2f537 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn r;.}../*.** 
2f538 52 65 74 75 72 6e 20 74 68 65 20 52 4f 57 49 44  Return the ROWID
2f539 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
2f53a 65 6e 74 20 69 6e 73 65 72 74 0a 2a 2f 0a 53 51  ent insert.*/.SQ
2f53b 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f  LITE_API sqlite_
2f53c 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61  int64 sqlite3_la
2f53d 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
2f53e 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2f53f 72 65 74 75 72 6e 20 64 62 2d 3e 6c 61 73 74 52  return db->lastR
2f540 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  owid;.}../*.** R
2f541 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
2f542 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74   of changes in t
2f543 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63  he most recent c
2f544 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 65  all to sqlite3_e
2f545 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  xec()..*/.SQLITE
2f546 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2f547 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33  _changes(sqlite3
2f548 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20   *db){.  return 
2f549 64 62 2d 3e 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a  db->nChange;.}..
2f54a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
2f54b 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67   number of chang
2f54c 65 73 20 73 69 6e 63 65 20 74 68 65 20 64 61 74  es since the dat
2f54d 61 62 61 73 65 20 68 61 6e 64 6c 65 20 77 61 73  abase handle was
2f54e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49   opened..*/.SQLI
2f54f 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2f550 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
2f551 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2f552 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e 54 6f 74   return db->nTot
2f553 61 6c 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a  alChange;.}../*.
2f554 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 6f 70 65  ** Close all ope
2f555 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 54 68  n savepoints. Th
2f556 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79  is function only
2f557 20 6d 61 6e 69 70 75 6c 61 74 65 73 20 66 69 65   manipulates fie
2f558 6c 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  lds of the.** da
2f559 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6f 62  tabase handle ob
2f55a 6a 65 63 74 2c 20 69 74 20 64 6f 65 73 20 6e 6f  ject, it does no
2f55b 74 20 63 6c 6f 73 65 20 61 6e 79 20 73 61 76 65  t close any save
2f55c 70 6f 69 6e 74 73 20 74 68 61 74 20 6d 61 79 20  points that may 
2f55d 62 65 20 6f 70 65 6e 0a 2a 2a 20 61 74 20 74 68  be open.** at th
2f55e 65 20 62 2d 74 72 65 65 2f 70 61 67 65 72 20 6c  e b-tree/pager l
2f55f 65 76 65 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  evel..*/.SQLITE_
2f560 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2f561 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69  ite3CloseSavepoi
2f562 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nts(sqlite3 *db)
2f563 7b 0a 20 20 77 68 69 6c 65 28 20 64 62 2d 3e 70  {.  while( db->p
2f564 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
2f565 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70   Savepoint *pTmp
2f566 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e   = db->pSavepoin
2f567 74 3b 0a 20 20 20 20 64 62 2d 3e 70 53 61 76 65  t;.    db->pSave
2f568 70 6f 69 6e 74 20 3d 20 70 54 6d 70 2d 3e 70 4e  point = pTmp->pN
2f569 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
2f56a 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70 29  DbFree(db, pTmp)
2f56b 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 6e 53 61 76  ;.  }.  db->nSav
2f56c 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 64 62  epoint = 0;.  db
2f56d 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20 30  ->nStatement = 0
2f56e 3b 0a 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61  ;.  db->isTransa
2f56f 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d  ctionSavepoint =
2f570 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f   0;.}../*.** Clo
2f571 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 53  se an existing S
2f572 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 0a 2a  QLite database.*
2f573 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2f574 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73   sqlite3_close(s
2f575 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 48  qlite3 *db){.  H
2f576 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 6e  ashElem *i;.  in
2f577 74 20 6a 3b 0a 0a 20 20 69 66 28 20 21 64 62 20  t j;..  if( !db 
2f578 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2f579 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
2f57a 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
2f57b 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
2f57c 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
2f57d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
2f57e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
2f57f 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2f580 74 65 78 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  tex);..#ifdef SQ
2f581 4c 49 54 45 5f 53 53 45 0a 20 20 7b 0a 20 20 20  LITE_SSE.  {.   
2f582 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
2f583 69 74 65 33 53 73 65 43 6c 65 61 6e 75 70 28 73  ite3SseCleanup(s
2f584 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 73 71  qlite3*);.    sq
2f585 6c 69 74 65 33 53 73 65 43 6c 65 61 6e 75 70 28  lite3SseCleanup(
2f586 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  db);.  }.#endif 
2f587 0a 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74  ..  sqlite3Reset
2f588 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2f589 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  b, 0);..  /* If 
2f58a 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
2f58b 20 6f 70 65 6e 2c 20 74 68 65 20 52 65 73 65 74   open, the Reset
2f58c 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 29  InternalSchema()
2f58d 20 63 61 6c 6c 20 61 62 6f 76 65 0a 20 20 2a 2a   call above.  **
2f58e 20 77 69 6c 6c 20 6e 6f 74 20 68 61 76 65 20 63   will not have c
2f58f 61 6c 6c 65 64 20 74 68 65 20 78 44 69 73 63 6f  alled the xDisco
2f590 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20 6f  nnect() method o
2f591 6e 20 61 6e 79 20 76 69 72 74 75 61 6c 0a 20 20  n any virtual.  
2f592 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  ** tables in the
2f593 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61   db->aVTrans[] a
2f594 72 72 61 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  rray. The follow
2f595 69 6e 67 20 73 71 6c 69 74 65 33 56 74 61 62 52  ing sqlite3VtabR
2f596 6f 6c 6c 62 61 63 6b 28 29 0a 20 20 2a 2a 20 63  ollback().  ** c
2f597 61 6c 6c 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 20  all will do so. 
2f598 57 65 20 6e 65 65 64 20 74 6f 20 64 6f 20 74 68  We need to do th
2f599 69 73 20 62 65 66 6f 72 65 20 74 68 65 20 63 68  is before the ch
2f59a 65 63 6b 20 66 6f 72 20 61 63 74 69 76 65 0a 20  eck for active. 
2f59b 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   ** SQL statemen
2f59c 74 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65  ts below, as the
2f59d 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65   v-table impleme
2f59e 6e 74 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 73  ntation may be s
2f59f 74 6f 72 69 6e 67 0a 20 20 2a 2a 20 73 6f 6d 65  toring.  ** some
2f5a0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2f5a1 65 6e 74 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e  ents internally.
2f5a2 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
2f5a3 74 61 62 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b  tabRollback(db);
2f5a4 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
2f5a5 61 72 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64  are any outstand
2f5a6 69 6e 67 20 56 4d 73 2c 20 72 65 74 75 72 6e 20  ing VMs, return 
2f5a7 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a  SQLITE_BUSY. */.
2f5a8 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20    if( db->pVdbe 
2f5a9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2f5aa 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42  ror(db, SQLITE_B
2f5ab 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22 75  USY, .        "u
2f5ac 6e 61 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64  nable to close d
2f5ad 75 65 20 74 6f 20 75 6e 66 69 6e 61 6c 69 73 65  ue to unfinalise
2f5ae 64 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a  d statements");.
2f5af 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
2f5b0 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2f5b1 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  x);.    return S
2f5b2 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
2f5b3 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2f5b4 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
2f5b5 4f 72 4f 6b 28 64 62 29 20 29 3b 0a 0a 20 20 66  OrOk(db) );..  f
2f5b6 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44  or(j=0; j<db->nD
2f5b7 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 42 74 72  b; j++){.    Btr
2f5b8 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
2f5b9 62 5b 6a 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[j].pBt;.    if
2f5ba 28 20 70 42 74 20 26 26 20 73 71 6c 69 74 65 33  ( pBt && sqlite3
2f5bb 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28  BtreeIsInBackup(
2f5bc 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71  pBt) ){.      sq
2f5bd 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
2f5be 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20  QLITE_BUSY, .   
2f5bf 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74         "unable t
2f5c0 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f 20 75  o close due to u
2f5c1 6e 66 69 6e 69 73 68 65 64 20 62 61 63 6b 75 70  nfinished backup
2f5c2 20 6f 70 65 72 61 74 69 6f 6e 22 29 3b 0a 20 20   operation");.  
2f5c3 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
2f5c4 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2f5c5 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  x);.      return
2f5c6 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
2f5c7 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72    }.  }..  /* Fr
2f5c8 65 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69  ee any outstandi
2f5c9 6e 67 20 53 61 76 65 70 6f 69 6e 74 20 73 74 72  ng Savepoint str
2f5ca 75 63 74 75 72 65 73 2e 20 2a 2f 0a 20 20 73 71  uctures. */.  sq
2f5cb 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f  lite3CloseSavepo
2f5cc 69 6e 74 73 28 64 62 29 3b 0a 0a 20 20 66 6f 72  ints(db);..  for
2f5cd 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b  (j=0; j<db->nDb;
2f5ce 20 6a 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63   j++){.    struc
2f5cf 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d  t Db *pDb = &db-
2f5d0 3e 61 44 62 5b 6a 5d 3b 0a 20 20 20 20 69 66 28  >aDb[j];.    if(
2f5d1 20 70 44 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20   pDb->pBt ){.   
2f5d2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
2f5d3 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a  lose(pDb->pBt);.
2f5d4 20 20 20 20 20 20 70 44 62 2d 3e 70 42 74 20 3d        pDb->pBt =
2f5d5 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6a 21   0;.      if( j!
2f5d6 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  =1 ){.        pD
2f5d7 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a  b->pSchema = 0;.
2f5d8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2f5d9 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74  }.  sqlite3Reset
2f5da 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2f5db 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 65 6c  b, 0);..  /* Tel
2f5dc 6c 20 74 68 65 20 63 6f 64 65 20 69 6e 20 6e 6f  l the code in no
2f5dd 74 69 66 79 2e 63 20 74 68 61 74 20 74 68 65 20  tify.c that the 
2f5de 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e 6f 20 6c 6f  connection no lo
2f5df 6e 67 65 72 20 68 6f 6c 64 73 20 61 6e 79 0a 20  nger holds any. 
2f5e0 20 2a 2a 20 6c 6f 63 6b 73 20 61 6e 64 20 64 6f   ** locks and do
2f5e1 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61  es not require a
2f5e2 6e 79 20 66 75 72 74 68 65 72 20 75 6e 6c 6f 63  ny further unloc
2f5e3 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
2f5e4 6b 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ks..  */.  sqlit
2f5e5 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73  e3ConnectionClos
2f5e6 65 64 28 64 62 29 3b 0a 0a 20 20 61 73 73 65 72  ed(db);..  asser
2f5e7 74 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b  t( db->nDb<=2 );
2f5e8 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
2f5e9 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db==db->aDbStati
2f5ea 63 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20  c );.  for(j=0; 
2f5eb 6a 3c 41 72 72 61 79 53 69 7a 65 28 64 62 2d 3e  j<ArraySize(db->
2f5ec 61 46 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29 7b 0a  aFunc.a); j++){.
2f5ed 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65      FuncDef *pNe
2f5ee 78 74 2c 20 2a 70 48 61 73 68 2c 20 2a 70 3b 0a  xt, *pHash, *p;.
2f5ef 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e 61 46      for(p=db->aF
2f5f0 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b 20 70 3d 70  unc.a[j]; p; p=p
2f5f1 48 61 73 68 29 7b 0a 20 20 20 20 20 20 70 48 61  Hash){.      pHa
2f5f2 73 68 20 3d 20 70 2d 3e 70 48 61 73 68 3b 0a 20  sh = p->pHash;. 
2f5f3 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b       while( p ){
2f5f4 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d  .        pNext =
2f5f5 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20   p->pNext;.     
2f5f6 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2f5f7 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20  (db, p);.       
2f5f8 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20   p = pNext;.    
2f5f9 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
2f5fa 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
2f5fb 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c  First(&db->aColl
2f5fc 53 65 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74  Seq); i; i=sqlit
2f5fd 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
2f5fe 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
2f5ff 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73  l = (CollSeq *)s
2f600 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
2f601 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ;.    /* Invoke 
2f602 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72 73 20  any destructors 
2f603 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 63  registered for c
2f604 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2f605 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a 2f 0a  e user data. */.
2f606 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33      for(j=0; j<3
2f607 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
2f608 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 20  ( pColl[j].xDel 
2f609 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
2f60a 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b 6a  [j].xDel(pColl[j
2f60b 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20  ].pUser);.      
2f60c 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
2f60d 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
2f60e 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  oll);.  }.  sqli
2f60f 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 64 62  te3HashClear(&db
2f610 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66  ->aCollSeq);.#if
2f611 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f612 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2f613 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
2f614 46 69 72 73 74 28 26 64 62 2d 3e 61 4d 6f 64 75  First(&db->aModu
2f615 6c 65 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65  le); i; i=sqlite
2f616 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
2f617 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20 3d    Module *pMod =
2f618 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74   (Module *)sqlit
2f619 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20  eHashData(i);.  
2f61a 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 44 65 73    if( pMod->xDes
2f61b 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 4d  troy ){.      pM
2f61c 6f 64 2d 3e 78 44 65 73 74 72 6f 79 28 70 4d 6f  od->xDestroy(pMo
2f61d 64 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a  d->pAux);.    }.
2f61e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2f61f 65 28 64 62 2c 20 70 4d 6f 64 29 3b 0a 20 20 7d  e(db, pMod);.  }
2f620 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  .  sqlite3HashCl
2f621 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  ear(&db->aModule
2f622 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c  );.#endif..  sql
2f623 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
2f624 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a 20  LITE_OK, 0); /* 
2f625 44 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20  Deallocates any 
2f626 63 61 63 68 65 64 20 65 72 72 6f 72 20 73 74 72  cached error str
2f627 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 66 28 20 64  ings. */.  if( d
2f628 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20 73  b->pErr ){.    s
2f629 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
2f62a 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d 0a 20  db->pErr);.  }. 
2f62b 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74   sqlite3CloseExt
2f62c 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20  ensions(db);..  
2f62d 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
2f62e 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a  TE_MAGIC_ERROR;.
2f62f 0a 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 2d 64  .  /* The temp-d
2f630 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69  atabase schema i
2f631 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69 66 66  s allocated diff
2f632 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65  erently from the
2f633 20 6f 74 68 65 72 20 73 63 68 65 6d 61 0a 20 20   other schema.  
2f634 2a 2a 20 6f 62 6a 65 63 74 73 20 28 75 73 69 6e  ** objects (usin
2f635 67 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  g sqliteMalloc()
2f636 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65   directly, inste
2f637 61 64 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72  ad of sqlite3Btr
2f638 65 65 53 63 68 65 6d 61 28 29 29 2e 0a 20 20 2a  eeSchema())..  *
2f639 2a 20 53 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  * So it needs to
2f63a 20 62 65 20 66 72 65 65 64 20 68 65 72 65 2e 20   be freed here. 
2f63b 54 6f 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72 6f  Todo: Why not ro
2f63c 6c 6c 20 74 68 65 20 74 65 6d 70 20 73 63 68 65  ll the temp sche
2f63d 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  ma into.  ** the
2f63e 20 73 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c 6c   same sqliteMall
2f63f 6f 63 28 29 20 61 73 20 74 68 65 20 6f 6e 65 20  oc() as the one 
2f640 74 68 61 74 20 61 6c 6c 6f 63 61 74 65 73 20 74  that allocates t
2f641 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 2a  he database .  *
2f642 2a 20 73 74 72 75 63 74 75 72 65 3f 0a 20 20 2a  * structure?.  *
2f643 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  /.  sqlite3DbFre
2f644 65 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 31 5d  e(db, db->aDb[1]
2f645 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c  .pSchema);.  sql
2f646 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2f647 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
2f648 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
2f649 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b 0a  E_MAGIC_CLOSED;.
2f64a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2f64b 66 72 65 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  free(db->mutex);
2f64c 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6c  .  assert( db->l
2f64d 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3d 3d 30  ookaside.nOut==0
2f64e 20 29 3b 20 20 2f 2a 20 46 61 69 6c 73 20 6f 6e   );  /* Fails on
2f64f 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d   a lookaside mem
2f650 6f 72 79 20 6c 65 61 6b 20 2a 2f 0a 20 20 69 66  ory leak */.  if
2f651 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
2f652 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20  bMalloced ){.   
2f653 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62   sqlite3_free(db
2f654 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61  ->lookaside.pSta
2f655 72 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  rt);.  }.  sqlit
2f656 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20 20 72  e3_free(db);.  r
2f657 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2f658 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61  .}../*.** Rollba
2f659 63 6b 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20  ck all database 
2f65a 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  files..*/.SQLITE
2f65b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2f65c 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c  lite3RollbackAll
2f65d 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2f65e 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 6e   int i;.  int in
2f65f 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 61 73 73  Trans = 0;.  ass
2f660 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
2f661 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
2f662 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  x) );.  sqlite3B
2f663 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
2f664 28 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ();.  for(i=0; i
2f665 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
2f666 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
2f667 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  i].pBt ){.      
2f668 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  if( sqlite3Btree
2f669 49 73 49 6e 54 72 61 6e 73 28 64 62 2d 3e 61 44  IsInTrans(db->aD
2f66a 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20  b[i].pBt) ){.   
2f66b 20 20 20 20 20 69 6e 54 72 61 6e 73 20 3d 20 31       inTrans = 1
2f66c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2f66d 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
2f66e 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b 69 5d 2e  back(db->aDb[i].
2f66f 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  pBt);.      db->
2f670 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e 73 20 3d  aDb[i].inTrans =
2f671 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
2f672 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
2f673 61 63 6b 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  ack(db);.  sqlit
2f674 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
2f675 63 28 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  c();..  if( db->
2f676 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
2f677 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20  ernChanges ){.  
2f678 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50    sqlite3ExpireP
2f679 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
2f67a 73 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74  s(db);.    sqlit
2f67b 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
2f67c 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
2f67d 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 68  }..  /* If one h
2f67e 61 73 20 62 65 65 6e 20 63 6f 6e 66 69 67 75 72  as been configur
2f67f 65 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 72  ed, invoke the r
2f680 6f 6c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63 61 6c  ollback-hook cal
2f681 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 64  lback */.  if( d
2f682 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  b->xRollbackCall
2f683 62 61 63 6b 20 26 26 20 28 69 6e 54 72 61 6e 73  back && (inTrans
2f684 20 7c 7c 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d   || !db->autoCom
2f685 6d 69 74 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e  mit) ){.    db->
2f686 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
2f687 6b 28 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41  k(db->pRollbackA
2f688 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  rg);.  }.}../*.*
2f689 2a 20 52 65 74 75 72 6e 20 61 20 73 74 61 74 69  * Return a stati
2f68a 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65  c string that de
2f68b 73 63 72 69 62 65 73 20 74 68 65 20 6b 69 6e 64  scribes the kind
2f68c 20 6f 66 20 65 72 72 6f 72 20 73 70 65 63 69 66   of error specif
2f68d 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72  ied in the.** ar
2f68e 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
2f68f 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
2f690 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72  char *sqlite3Err
2f691 53 74 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 63  Str(int rc){.  c
2f692 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20  onst char *z;.  
2f693 73 77 69 74 63 68 28 20 72 63 20 26 20 30 78 66  switch( rc & 0xf
2f694 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  f ){.    case SQ
2f695 4c 49 54 45 5f 52 4f 57 3a 0a 20 20 20 20 63 61  LITE_ROW:.    ca
2f696 73 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a  se SQLITE_DONE:.
2f697 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f698 4f 4b 3a 20 20 20 20 20 20 20 20 20 7a 20 3d 20  OK:         z = 
2f699 22 6e 6f 74 20 61 6e 20 65 72 72 6f 72 22 3b 20  "not an error"; 
2f69a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f69b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f69c 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f69d 45 52 52 4f 52 3a 20 20 20 20 20 20 7a 20 3d 20  ERROR:      z = 
2f69e 22 53 51 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72  "SQL logic error
2f69f 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61   or missing data
2f6a0 62 61 73 65 22 3b 20 20 20 62 72 65 61 6b 3b 0a  base";   break;.
2f6a1 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6a2 50 45 52 4d 3a 20 20 20 20 20 20 20 7a 20 3d 20  PERM:       z = 
2f6a3 22 61 63 63 65 73 73 20 70 65 72 6d 69 73 73 69  "access permissi
2f6a4 6f 6e 20 64 65 6e 69 65 64 22 3b 20 20 20 20 20  on denied";     
2f6a5 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6a6 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6a7 41 42 4f 52 54 3a 20 20 20 20 20 20 7a 20 3d 20  ABORT:      z = 
2f6a8 22 63 61 6c 6c 62 61 63 6b 20 72 65 71 75 65 73  "callback reques
2f6a9 74 65 64 20 71 75 65 72 79 20 61 62 6f 72 74 22  ted query abort"
2f6aa 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
2f6ab 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6ac 42 55 53 59 3a 20 20 20 20 20 20 20 7a 20 3d 20  BUSY:       z = 
2f6ad 22 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63  "database is loc
2f6ae 6b 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20  ked";           
2f6af 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6b0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6b1 4c 4f 43 4b 45 44 3a 20 20 20 20 20 7a 20 3d 20  LOCKED:     z = 
2f6b2 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20  "database table 
2f6b3 69 73 20 6c 6f 63 6b 65 64 22 3b 20 20 20 20 20  is locked";     
2f6b4 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6b5 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6b6 4e 4f 4d 45 4d 3a 20 20 20 20 20 20 7a 20 3d 20  NOMEM:      z = 
2f6b7 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 3b  "out of memory";
2f6b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f6b9 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6ba 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6bb 52 45 41 44 4f 4e 4c 59 3a 20 20 20 7a 20 3d 20  READONLY:   z = 
2f6bc 22 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74  "attempt to writ
2f6bd 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74  e a readonly dat
2f6be 61 62 61 73 65 22 3b 20 20 62 72 65 61 6b 3b 0a  abase";  break;.
2f6bf 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6c0 49 4e 54 45 52 52 55 50 54 3a 20 20 7a 20 3d 20  INTERRUPT:  z = 
2f6c1 22 69 6e 74 65 72 72 75 70 74 65 64 22 3b 20 20  "interrupted";  
2f6c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f6c3 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6c4 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6c5 49 4f 45 52 52 3a 20 20 20 20 20 20 7a 20 3d 20  IOERR:      z = 
2f6c6 22 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22  "disk I/O error"
2f6c7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f6c8 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6c9 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6ca 43 4f 52 52 55 50 54 3a 20 20 20 20 7a 20 3d 20  CORRUPT:    z = 
2f6cb 22 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69  "database disk i
2f6cc 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65  mage is malforme
2f6cd 64 22 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  d";      break;.
2f6ce 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6cf 46 55 4c 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20  FULL:       z = 
2f6d0 22 64 61 74 61 62 61 73 65 20 6f 72 20 64 69 73  "database or dis
2f6d1 6b 20 69 73 20 66 75 6c 6c 22 3b 20 20 20 20 20  k is full";     
2f6d2 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6d3 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6d4 43 41 4e 54 4f 50 45 4e 3a 20 20 20 7a 20 3d 20  CANTOPEN:   z = 
2f6d5 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  "unable to open 
2f6d6 64 61 74 61 62 61 73 65 20 66 69 6c 65 22 3b 20  database file"; 
2f6d7 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6d8 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6d9 45 4d 50 54 59 3a 20 20 20 20 20 20 7a 20 3d 20  EMPTY:      z = 
2f6da 22 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20  "table contains 
2f6db 6e 6f 20 64 61 74 61 22 3b 20 20 20 20 20 20 20  no data";       
2f6dc 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6dd 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6de 53 43 48 45 4d 41 3a 20 20 20 20 20 7a 20 3d 20  SCHEMA:     z = 
2f6df 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61  "database schema
2f6e0 20 68 61 73 20 63 68 61 6e 67 65 64 22 3b 20 20   has changed";  
2f6e1 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6e2 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6e3 54 4f 4f 42 49 47 3a 20 20 20 20 20 7a 20 3d 20  TOOBIG:     z = 
2f6e4 22 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20  "String or BLOB 
2f6e5 65 78 63 65 65 64 65 64 20 73 69 7a 65 20 6c 69  exceeded size li
2f6e6 6d 69 74 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  mit";    break;.
2f6e7 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6e8 43 4f 4e 53 54 52 41 49 4e 54 3a 20 7a 20 3d 20  CONSTRAINT: z = 
2f6e9 22 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c  "constraint fail
2f6ea 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed";            
2f6eb 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6ec 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6ed 4d 49 53 4d 41 54 43 48 3a 20 20 20 7a 20 3d 20  MISMATCH:   z = 
2f6ee 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d 61 74  "datatype mismat
2f6ef 63 68 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ch";            
2f6f0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f6f1 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6f2 4d 49 53 55 53 45 3a 20 20 20 20 20 7a 20 3d 20  MISUSE:     z = 
2f6f3 22 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65  "library routine
2f6f4 20 63 61 6c 6c 65 64 20 6f 75 74 20 6f 66 20 73   called out of s
2f6f5 65 71 75 65 6e 63 65 22 3b 62 72 65 61 6b 3b 0a  equence";break;.
2f6f6 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6f7 4e 4f 4c 46 53 3a 20 20 20 20 20 20 7a 20 3d 20  NOLFS:      z = 
2f6f8 22 6c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70  "large file supp
2f6f9 6f 72 74 20 69 73 20 64 69 73 61 62 6c 65 64 22  ort is disabled"
2f6fa 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
2f6fb 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f6fc 41 55 54 48 3a 20 20 20 20 20 20 20 7a 20 3d 20  AUTH:       z = 
2f6fd 22 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64  "authorization d
2f6fe 65 6e 69 65 64 22 3b 20 20 20 20 20 20 20 20 20  enied";         
2f6ff 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f700 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f701 46 4f 52 4d 41 54 3a 20 20 20 20 20 7a 20 3d 20  FORMAT:     z = 
2f702 22 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62  "auxiliary datab
2f703 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72  ase format error
2f704 22 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ";       break;.
2f705 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f706 52 41 4e 47 45 3a 20 20 20 20 20 20 7a 20 3d 20  RANGE:      z = 
2f707 22 62 69 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20  "bind or column 
2f708 69 6e 64 65 78 20 6f 75 74 20 6f 66 20 72 61 6e  index out of ran
2f709 67 65 22 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a  ge";     break;.
2f70a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2f70b 4e 4f 54 41 44 42 3a 20 20 20 20 20 7a 20 3d 20  NOTADB:     z = 
2f70c 22 66 69 6c 65 20 69 73 20 65 6e 63 72 79 70 74  "file is encrypt
2f70d 65 64 20 6f 72 20 69 73 20 6e 6f 74 20 61 20 64  ed or is not a d
2f70e 61 74 61 62 61 73 65 22 3b 62 72 65 61 6b 3b 0a  atabase";break;.
2f70f 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20      default:    
2f710 20 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20              z = 
2f711 22 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f 72 22 3b  "unknown error";
2f712 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f713 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f714 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a    }.  return z;.
2f715 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2f716 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
2f717 20 61 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b   a busy callback
2f718 20 74 68 61 74 20 73 6c 65 65 70 73 20 61 6e 64   that sleeps and
2f719 20 74 72 69 65 73 0a 2a 2a 20 61 67 61 69 6e 20   tries.** again 
2f71a 75 6e 74 69 6c 20 61 20 74 69 6d 65 6f 75 74 20  until a timeout 
2f71b 76 61 6c 75 65 20 69 73 20 72 65 61 63 68 65 64  value is reached
2f71c 2e 20 20 54 68 65 20 74 69 6d 65 6f 75 74 20 76  .  The timeout v
2f71d 61 6c 75 65 20 69 73 0a 2a 2a 20 61 6e 20 69 6e  alue is.** an in
2f71e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20  teger number of 
2f71f 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 70 61 73  milliseconds pas
2f720 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 66 69  sed in as the fi
2f721 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e  rst.** argument.
2f722 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2f723 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79  qliteDefaultBusy
2f724 43 61 6c 6c 62 61 63 6b 28 0a 20 76 6f 69 64 20  Callback(. void 
2f725 2a 70 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  *ptr,           
2f726 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2f727 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 69  connection */. i
2f728 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20 20 20  nt count        
2f729 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2f72a 72 20 6f 66 20 74 69 6d 65 73 20 74 61 62 6c 65  r of times table
2f72b 20 68 61 73 20 62 65 65 6e 20 62 75 73 79 20 2a   has been busy *
2f72c 2f 0a 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  /.){.#if SQLITE_
2f72d 4f 53 5f 57 49 4e 20 7c 7c 20 28 64 65 66 69 6e  OS_WIN || (defin
2f72e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20  ed(HAVE_USLEEP) 
2f72f 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50 29 0a  && HAVE_USLEEP).
2f730 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2f731 38 20 64 65 6c 61 79 73 5b 5d 20 3d 0a 20 20 20  8 delays[] =.   
2f732 20 20 7b 20 31 2c 20 32 2c 20 35 2c 20 31 30 2c    { 1, 2, 5, 10,
2f733 20 31 35 2c 20 32 30 2c 20 32 35 2c 20 32 35 2c   15, 20, 25, 25,
2f734 20 20 32 35 2c 20 20 35 30 2c 20 20 35 30 2c 20    25,  50,  50, 
2f735 31 30 30 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  100 };.  static 
2f736 63 6f 6e 73 74 20 75 38 20 74 6f 74 61 6c 73 5b  const u8 totals[
2f737 5d 20 3d 0a 20 20 20 20 20 7b 20 30 2c 20 31 2c  ] =.     { 0, 1,
2f738 20 33 2c 20 20 38 2c 20 31 38 2c 20 33 33 2c 20   3,  8, 18, 33, 
2f739 35 33 2c 20 37 38 2c 20 31 30 33 2c 20 31 32 38  53, 78, 103, 128
2f73a 2c 20 31 37 38 2c 20 32 32 38 20 7d 3b 0a 23 20  , 178, 228 };.# 
2f73b 64 65 66 69 6e 65 20 4e 44 45 4c 41 59 20 28 73  define NDELAY (s
2f73c 69 7a 65 6f 66 28 64 65 6c 61 79 73 29 2f 73 69  izeof(delays)/si
2f73d 7a 65 6f 66 28 64 65 6c 61 79 73 5b 30 5d 29 29  zeof(delays[0]))
2f73e 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2f73f 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b   (sqlite3 *)ptr;
2f740 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d  .  int timeout =
2f741 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74   db->busyTimeout
2f742 3b 0a 20 20 69 6e 74 20 64 65 6c 61 79 2c 20 70  ;.  int delay, p
2f743 72 69 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  rior;..  assert(
2f744 20 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 69   count>=0 );.  i
2f745 66 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45 4c 41  f( count < NDELA
2f746 59 20 29 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d  Y ){.    delay =
2f747 20 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d 3b 0a   delays[count];.
2f748 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74 61      prior = tota
2f749 6c 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d 65 6c  ls[count];.  }el
2f74a 73 65 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20  se{.    delay = 
2f74b 64 65 6c 61 79 73 5b 4e 44 45 4c 41 59 2d 31 5d  delays[NDELAY-1]
2f74c 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f  ;.    prior = to
2f74d 74 61 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d 20 2b  tals[NDELAY-1] +
2f74e 20 64 65 6c 61 79 2a 28 63 6f 75 6e 74 2d 28 4e   delay*(count-(N
2f74f 44 45 4c 41 59 2d 31 29 29 3b 0a 20 20 7d 0a 20  DELAY-1));.  }. 
2f750 20 69 66 28 20 70 72 69 6f 72 20 2b 20 64 65 6c   if( prior + del
2f751 61 79 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a  ay > timeout ){.
2f752 20 20 20 20 64 65 6c 61 79 20 3d 20 74 69 6d 65      delay = time
2f753 6f 75 74 20 2d 20 70 72 69 6f 72 3b 0a 20 20 20  out - prior;.   
2f754 20 69 66 28 20 64 65 6c 61 79 3c 3d 30 20 29 20   if( delay<=0 ) 
2f755 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2f756 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64  sqlite3OsSleep(d
2f757 62 2d 3e 70 56 66 73 2c 20 64 65 6c 61 79 2a 31  b->pVfs, delay*1
2f758 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 31  000);.  return 1
2f759 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65  ;.#else.  sqlite
2f75a 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33  3 *db = (sqlite3
2f75b 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69   *)ptr;.  int ti
2f75c 6d 65 6f 75 74 20 3d 20 28 28 73 71 6c 69 74 65  meout = ((sqlite
2f75d 33 20 2a 29 70 74 72 29 2d 3e 62 75 73 79 54 69  3 *)ptr)->busyTi
2f75e 6d 65 6f 75 74 3b 0a 20 20 69 66 28 20 28 63 6f  meout;.  if( (co
2f75f 75 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20 74 69  unt+1)*1000 > ti
2f760 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 72 65 74  meout ){.    ret
2f761 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  urn 0;.  }.  sql
2f762 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e  ite3OsSleep(db->
2f763 70 56 66 73 2c 20 31 30 30 30 30 30 30 29 3b 0a  pVfs, 1000000);.
2f764 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64    return 1;.#end
2f765 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  if.}../*.** Invo
2f766 6b 65 20 74 68 65 20 67 69 76 65 6e 20 62 75 73  ke the given bus
2f767 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a  y handler..**.**
2f768 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2f769 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20   called when an 
2f76a 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 65 64  operation failed
2f76b 20 77 69 74 68 20 61 20 6c 6f 63 6b 2e 0a 2a 2a   with a lock..**
2f76c 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
2f76d 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
2f76e 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 72  o, the lock is r
2f76f 65 74 72 69 65 64 2e 20 20 49 66 20 69 74 0a 2a  etried.  If it.*
2f770 2a 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 65  * returns 0, the
2f771 20 6f 70 65 72 61 74 69 6f 6e 20 61 62 6f 72 74   operation abort
2f772 73 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45  s with an SQLITE
2f773 5f 42 55 53 59 20 65 72 72 6f 72 2e 0a 2a 2f 0a  _BUSY error..*/.
2f774 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2f775 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65  nt sqlite3Invoke
2f776 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79  BusyHandler(Busy
2f777 48 61 6e 64 6c 65 72 20 2a 70 29 7b 0a 20 20 69  Handler *p){.  i
2f778 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 4e 45 56  nt rc;.  if( NEV
2f779 45 52 28 70 3d 3d 30 29 20 7c 7c 20 70 2d 3e 78  ER(p==0) || p->x
2f77a 46 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 42  Func==0 || p->nB
2f77b 75 73 79 3c 30 20 29 20 72 65 74 75 72 6e 20 30  usy<0 ) return 0
2f77c 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46 75 6e  ;.  rc = p->xFun
2f77d 63 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e 42  c(p->pArg, p->nB
2f77e 75 73 79 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  usy);.  if( rc==
2f77f 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73  0 ){.    p->nBus
2f780 79 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b  y = -1;.  }else{
2f781 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b 2b 3b  .    p->nBusy++;
2f782 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2f783 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ; .}../*.** This
2f784 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
2f785 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  e busy callback 
2f786 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61  for an Sqlite da
2f787 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  tabase to the.**
2f788 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20   given callback 
2f789 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2f78a 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74  e given argument
2f78b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2f78c 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
2f78d 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69  _handler(.  sqli
2f78e 74 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28  te3 *db,.  int (
2f78f 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 2c 69 6e  *xBusy)(void*,in
2f790 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  t),.  void *pArg
2f791 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  .){.  sqlite3_mu
2f792 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2f793 74 65 78 29 3b 0a 20 20 64 62 2d 3e 62 75 73 79  tex);.  db->busy
2f794 48 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20 3d 20  Handler.xFunc = 
2f795 78 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62 75 73  xBusy;.  db->bus
2f796 79 48 61 6e 64 6c 65 72 2e 70 41 72 67 20 3d 20  yHandler.pArg = 
2f797 70 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75 73 79  pArg;.  db->busy
2f798 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20  Handler.nBusy = 
2f799 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  0;.  sqlite3_mut
2f79a 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2f79b 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
2f79c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e  LITE_OK;.}..#ifn
2f79d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2f79e 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
2f79f 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  K./*.** This rou
2f7a0 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 72  tine sets the pr
2f7a1 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
2f7a2 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61  for an Sqlite da
2f7a3 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  tabase to the.**
2f7a4 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20   given callback 
2f7a5 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2f7a6 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74  e given argument
2f7a7 2e 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63  . The progress c
2f7a8 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a 2a 20  allback will.** 
2f7a9 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 72 79  be invoked every
2f7aa 20 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e 0a 2a   nOps opcodes..*
2f7ab 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
2f7ac 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  d sqlite3_progre
2f7ad 73 73 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71  ss_handler(.  sq
2f7ae 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 69 6e  lite3 *db, .  in
2f7af 74 20 6e 4f 70 73 2c 0a 20 20 69 6e 74 20 28 2a  t nOps,.  int (*
2f7b0 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 2a  xProgress)(void*
2f7b1 29 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  ), .  void *pArg
2f7b2 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  .){.  sqlite3_mu
2f7b3 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2f7b4 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 4f 70 73  tex);.  if( nOps
2f7b5 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 78 50  >0 ){.    db->xP
2f7b6 72 6f 67 72 65 73 73 20 3d 20 78 50 72 6f 67 72  rogress = xProgr
2f7b7 65 73 73 3b 0a 20 20 20 20 64 62 2d 3e 6e 50 72  ess;.    db->nPr
2f7b8 6f 67 72 65 73 73 4f 70 73 20 3d 20 6e 4f 70 73  ogressOps = nOps
2f7b9 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f 67 72  ;.    db->pProgr
2f7ba 65 73 73 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  essArg = pArg;. 
2f7bb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e   }else{.    db->
2f7bc 78 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20  xProgress = 0;. 
2f7bd 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73     db->nProgress
2f7be 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  Ops = 0;.    db-
2f7bf 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d 20  >pProgressArg = 
2f7c0 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
2f7c1 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
2f7c2 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69  >mutex);.}.#endi
2f7c3 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  f.../*.** This r
2f7c4 6f 75 74 69 6e 65 20 69 6e 73 74 61 6c 6c 73 20  outine installs 
2f7c5 61 20 64 65 66 61 75 6c 74 20 62 75 73 79 20 68  a default busy h
2f7c6 61 6e 64 6c 65 72 20 74 68 61 74 20 77 61 69 74  andler that wait
2f7c7 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 70 65  s for the.** spe
2f7c8 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66  cified number of
2f7c9 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 62 65   milliseconds be
2f7ca 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 30  fore returning 0
2f7cb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2f7cc 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
2f7cd 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33  _timeout(sqlite3
2f7ce 20 2a 64 62 2c 20 69 6e 74 20 6d 73 29 7b 0a 20   *db, int ms){. 
2f7cf 20 69 66 28 20 6d 73 3e 30 20 29 7b 0a 20 20 20   if( ms>0 ){.   
2f7d0 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74   db->busyTimeout
2f7d1 20 3d 20 6d 73 3b 0a 20 20 20 20 73 71 6c 69 74   = ms;.    sqlit
2f7d2 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
2f7d3 64 62 2c 20 73 71 6c 69 74 65 44 65 66 61 75 6c  db, sqliteDefaul
2f7d4 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 2c 20 28  tBusyCallback, (
2f7d5 76 6f 69 64 2a 29 64 62 29 3b 0a 20 20 7d 65 6c  void*)db);.  }el
2f7d6 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
2f7d7 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c  busy_handler(db,
2f7d8 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65   0, 0);.  }.  re
2f7d9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2f7da 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 75 73 65 20 61  }../*.** Cause a
2f7db 6e 79 20 70 65 6e 64 69 6e 67 20 6f 70 65 72 61  ny pending opera
2f7dc 74 69 6f 6e 20 74 6f 20 73 74 6f 70 20 61 74 20  tion to stop at 
2f7dd 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70  its earliest opp
2f7de 6f 72 74 75 6e 69 74 79 2e 0a 2a 2f 0a 53 51 4c  ortunity..*/.SQL
2f7df 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2f7e0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 73  ite3_interrupt(s
2f7e1 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64  qlite3 *db){.  d
2f7e2 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
2f7e3 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a  ted = 1;.}.../*.
2f7e4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2f7e5 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 20   is exactly the 
2f7e6 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65 33 5f  same as sqlite3_
2f7e7 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
2f7e8 29 2c 20 65 78 63 65 70 74 0a 2a 2a 20 74 68 61  ), except.** tha
2f7e9 74 20 69 74 20 69 73 20 64 65 73 69 67 6e 65 64  t it is designed
2f7ea 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79   to be called by
2f7eb 20 69 6e 74 65 72 6e 61 6c 20 63 6f 64 65 2e 20   internal code. 
2f7ec 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 69  The difference i
2f7ed 73 0a 2a 2a 20 74 68 61 74 20 69 66 20 61 20 6d  s.** that if a m
2f7ee 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 69 6e  alloc() fails in
2f7ef 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2f7f0 66 75 6e 63 74 69 6f 6e 28 29 2c 20 61 6e 20 65  function(), an e
2f7f1 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20  rror code.** is 
2f7f2 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
2f7f3 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c   mallocFailed fl
2f7f4 61 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2f 0a  ag cleared. .*/.
2f7f5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2f7f6 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  nt sqlite3Create
2f7f7 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 20  Func(.  sqlite3 
2f7f8 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  *db,.  const cha
2f7f9 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  r *zFunctionName
2f7fa 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20  ,.  int nArg,.  
2f7fb 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20  int enc,.  void 
2f7fc 2a 70 55 73 65 72 44 61 74 61 2c 0a 20 20 76 6f  *pUserData,.  vo
2f7fd 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
2f7fe 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
2f7ff 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
2f800 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74  *),.  void (*xSt
2f801 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
2f802 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
2f803 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
2f804 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
2f805 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
2f806 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a  {.  FuncDef *p;.
2f807 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20    int nName;..  
2f808 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
2f809 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
2f80a 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 7a  utex) );.  if( z
2f80b 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d 30 20  FunctionName==0 
2f80c 7c 7c 0a 20 20 20 20 20 20 28 78 46 75 6e 63 20  ||.      (xFunc 
2f80d 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c 20 78 53  && (xFinal || xS
2f80e 74 65 70 29 29 20 7c 7c 20 0a 20 20 20 20 20 20  tep)) || .      
2f80f 28 21 78 46 75 6e 63 20 26 26 20 28 78 46 69 6e  (!xFunc && (xFin
2f810 61 6c 20 26 26 20 21 78 53 74 65 70 29 29 20 7c  al && !xStep)) |
2f811 7c 0a 20 20 20 20 20 20 28 21 78 46 75 6e 63 20  |.      (!xFunc 
2f812 26 26 20 28 21 78 46 69 6e 61 6c 20 26 26 20 78  && (!xFinal && x
2f813 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20 20 20  Step)) ||.      
2f814 28 6e 41 72 67 3c 2d 31 20 7c 7c 20 6e 41 72 67  (nArg<-1 || nArg
2f815 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43  >SQLITE_MAX_FUNC
2f816 54 49 4f 4e 5f 41 52 47 29 20 7c 7c 0a 20 20 20  TION_ARG) ||.   
2f817 20 20 20 28 32 35 35 3c 28 6e 4e 61 6d 65 20 3d     (255<(nName =
2f818 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64   sqlite3Strlen(d
2f819 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  b, zFunctionName
2f81a 29 29 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ))) ){.    sqlit
2f81b 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
2f81c 54 45 5f 45 52 52 4f 52 2c 20 22 62 61 64 20 70  TE_ERROR, "bad p
2f81d 61 72 61 6d 65 74 65 72 73 22 29 3b 0a 20 20 20  arameters");.   
2f81e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2f81f 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 0a 23 69 66  RROR;.  }.  .#if
2f820 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f821 5f 55 54 46 31 36 0a 20 20 2f 2a 20 49 66 20 53  _UTF16.  /* If S
2f822 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 73  QLITE_UTF16 is s
2f823 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
2f824 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c 20 74  encoding type, t
2f825 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a 20 20  ransform this.  
2f826 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53 51 4c  ** to one of SQL
2f827 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53  ITE_UTF16LE or S
2f828 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 75 73  QLITE_UTF16BE us
2f829 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c  ing the.  ** SQL
2f82a 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
2f82b 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f 55 54  macro. SQLITE_UT
2f82c 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65 64 20  F16 is not used 
2f82d 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2a  internally..  **
2f82e 0a 20 20 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  .  ** If SQLITE_
2f82f 41 4e 59 20 69 73 20 73 70 65 63 69 66 69 65 64  ANY is specified
2f830 2c 20 61 64 64 20 74 68 72 65 65 20 76 65 72 73  , add three vers
2f831 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 75 6e 63  ions of the func
2f832 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65  tion.  ** to the
2f833 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 20 20 2a   hash table..  *
2f834 2f 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c  /.  if( enc==SQL
2f835 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20 20 20  ITE_UTF16 ){.   
2f836 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54   enc = SQLITE_UT
2f837 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 65 6c  F16NATIVE;.  }el
2f838 73 65 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49  se if( enc==SQLI
2f839 54 45 5f 41 4e 59 20 29 7b 0a 20 20 20 20 69 6e  TE_ANY ){.    in
2f83a 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73  t rc;.    rc = s
2f83b 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
2f83c 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
2f83d 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45  me, nArg, SQLITE
2f83e 5f 55 54 46 38 2c 0a 20 20 20 20 20 20 20 20 20  _UTF8,.         
2f83f 70 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e 63  pUserData, xFunc
2f840 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
2f841 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
2f842 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2f843 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65   rc = sqlite3Cre
2f844 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e  ateFunc(db, zFun
2f845 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c  ctionName, nArg,
2f846 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c   SQLITE_UTF16LE,
2f847 0a 20 20 20 20 20 20 20 20 20 20 70 55 73 65 72  .          pUser
2f848 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53 74  Data, xFunc, xSt
2f849 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 20  ep, xFinal);.   
2f84a 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
2f84b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2f84c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
2f84d 20 7d 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c   }.    enc = SQL
2f84e 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 7d  ITE_UTF16BE;.  }
2f84f 0a 23 65 6c 73 65 0a 20 20 65 6e 63 20 3d 20 53  .#else.  enc = S
2f850 51 4c 49 54 45 5f 55 54 46 38 3b 0a 23 65 6e 64  QLITE_UTF8;.#end
2f851 69 66 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b  if.  .  /* Check
2f852 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   if an existing 
2f853 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e  function is bein
2f854 67 20 6f 76 65 72 72 69 64 64 65 6e 20 6f 72 20  g overridden or 
2f855 64 65 6c 65 74 65 64 2e 20 49 66 20 73 6f 2c 0a  deleted. If so,.
2f856 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61    ** and there a
2f857 72 65 20 61 63 74 69 76 65 20 56 4d 73 2c 20 74  re active VMs, t
2f858 68 65 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54  hen return SQLIT
2f859 45 5f 42 55 53 59 2e 20 49 66 20 61 20 66 75 6e  E_BUSY. If a fun
2f85a 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 62 65  ction.  ** is be
2f85b 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 2f 64  ing overridden/d
2f85c 65 6c 65 74 65 64 20 62 75 74 20 74 68 65 72 65  eleted but there
2f85d 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 56   are no active V
2f85e 4d 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0a 20 20  Ms, allow the.  
2f85f 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  ** operation to 
2f860 63 6f 6e 74 69 6e 75 65 20 62 75 74 20 69 6e 76  continue but inv
2f861 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 63  alidate all prec
2f862 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
2f863 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 73  ts..  */.  p = s
2f864 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
2f865 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  on(db, zFunction
2f866 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
2f867 67 2c 20 28 75 38 29 65 6e 63 2c 20 30 29 3b 0a  g, (u8)enc, 0);.
2f868 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 69 50    if( p && p->iP
2f869 72 65 66 45 6e 63 3d 3d 65 6e 63 20 26 26 20 70  refEnc==enc && p
2f86a 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 29 7b 0a  ->nArg==nArg ){.
2f86b 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69      if( db->acti
2f86c 76 65 56 64 62 65 43 6e 74 20 29 7b 0a 20 20 20  veVdbeCnt ){.   
2f86d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
2f86e 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c  db, SQLITE_BUSY,
2f86f 20 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c   .        "unabl
2f870 65 20 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69  e to delete/modi
2f871 66 79 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  fy user-function
2f872 20 64 75 65 20 74 6f 20 61 63 74 69 76 65 20 73   due to active s
2f873 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20  tatements");.   
2f874 20 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e     assert( !db->
2f875 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
2f876 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
2f877 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65  ITE_BUSY;.    }e
2f878 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
2f879 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
2f87a 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
2f87b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d      }.  }..  p =
2f87c 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
2f87d 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69  tion(db, zFuncti
2f87e 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e  onName, nName, n
2f87f 41 72 67 2c 20 28 75 38 29 65 6e 63 2c 20 31 29  Arg, (u8)enc, 1)
2f880 3b 0a 20 20 61 73 73 65 72 74 28 70 20 7c 7c 20  ;.  assert(p || 
2f881 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2f882 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  );.  if( !p ){. 
2f883 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2f884 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d  _NOMEM;.  }.  p-
2f885 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 70 2d  >flags = 0;.  p-
2f886 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b 0a  >xFunc = xFunc;.
2f887 20 20 70 2d 3e 78 53 74 65 70 20 3d 20 78 53 74    p->xStep = xSt
2f888 65 70 3b 0a 20 20 70 2d 3e 78 46 69 6e 61 6c 69  ep;.  p->xFinali
2f889 7a 65 20 3d 20 78 46 69 6e 61 6c 3b 0a 20 20 70  ze = xFinal;.  p
2f88a 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20 70 55  ->pUserData = pU
2f88b 73 65 72 44 61 74 61 3b 0a 20 20 70 2d 3e 6e 41  serData;.  p->nA
2f88c 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b 0a  rg = (u16)nArg;.
2f88d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2f88e 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  OK;.}../*.** Cre
2f88f 61 74 65 20 6e 65 77 20 75 73 65 72 20 66 75 6e  ate new user fun
2f890 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ctions..*/.SQLIT
2f891 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2f892 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
2f893 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
2f894 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2f895 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20  zFunctionName,. 
2f896 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74   int nArg,.  int
2f897 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20 2a 70 2c   enc,.  void *p,
2f898 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  .  void (*xFunc)
2f899 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2f89a 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
2f89b 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  lue **),.  void 
2f89c 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33  (*xStep)(sqlite3
2f89d 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
2f89e 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c  lite3_value **),
2f89f 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  .  void (*xFinal
2f8a0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
2f8a1 74 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  t*).){.  int rc;
2f8a2 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2f8a3 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2f8a4 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
2f8a5 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
2f8a6 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e  zFunctionName, n
2f8a7 41 72 67 2c 20 65 6e 63 2c 20 70 2c 20 78 46 75  Arg, enc, p, xFu
2f8a8 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  nc, xStep, xFina
2f8a9 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  l);.  rc = sqlit
2f8aa 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
2f8ab 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
2f8ac 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2f8ad 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
2f8ae 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
2f8af 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
2f8b0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2f8b1 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
2f8b2 63 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74  ction16(.  sqlit
2f8b3 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
2f8b4 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e  void *zFunctionN
2f8b5 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c  ame,.  int nArg,
2f8b6 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
2f8b7 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20 20 76 6f  .  void *p,.  vo
2f8b8 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
2f8b9 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
2f8ba 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
2f8bb 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65  ),.  void (*xSte
2f8bc 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
2f8bd 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
2f8be 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64  value**),.  void
2f8bf 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
2f8c0 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a  e3_context*).){.
2f8c1 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72    int rc;.  char
2f8c2 20 2a 7a 46 75 6e 63 38 3b 0a 20 20 73 71 6c 69   *zFunc8;.  sqli
2f8c3 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2f8c4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
2f8c5 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
2f8c6 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 46 75  cFailed );.  zFu
2f8c7 6e 63 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66  nc8 = sqlite3Utf
2f8c8 31 36 74 6f 38 28 64 62 2c 20 7a 46 75 6e 63 74  16to8(db, zFunct
2f8c9 69 6f 6e 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20  ionName, -1);.  
2f8ca 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  rc = sqlite3Crea
2f8cb 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63  teFunc(db, zFunc
2f8cc 38 2c 20 6e 41 72 67 2c 20 65 54 65 78 74 52 65  8, nArg, eTextRe
2f8cd 70 2c 20 70 2c 20 78 46 75 6e 63 2c 20 78 53 74  p, p, xFunc, xSt
2f8ce 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 73  ep, xFinal);.  s
2f8cf 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2f8d0 20 7a 46 75 6e 63 38 29 3b 0a 20 20 72 63 20 3d   zFunc8);.  rc =
2f8d1 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
2f8d2 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
2f8d3 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2f8d4 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2f8d5 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
2f8d6 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65  .../*.** Declare
2f8d7 20 74 68 61 74 20 61 20 66 75 6e 63 74 69 6f 6e   that a function
2f8d8 20 68 61 73 20 62 65 65 6e 20 6f 76 65 72 6c 6f   has been overlo
2f8d9 61 64 65 64 20 62 79 20 61 20 76 69 72 74 75 61  aded by a virtua
2f8da 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  l table..**.** I
2f8db 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 61  f the function a
2f8dc 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 73  lready exists as
2f8dd 20 61 20 72 65 67 75 6c 61 72 20 67 6c 6f 62 61   a regular globa
2f8de 6c 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e  l function, then
2f8df 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
2f8e0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66   is a no-op.  If
2f8e1 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f   the function do
2f8e2 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74 68  es not exist, th
2f8e3 65 6e 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e  en create.** a n
2f8e4 65 77 20 6f 6e 65 20 74 68 61 74 20 61 6c 77 61  ew one that alwa
2f8e5 79 73 20 74 68 72 6f 77 73 20 61 20 72 75 6e 2d  ys throws a run-
2f8e6 74 69 6d 65 20 65 72 72 6f 72 2e 20 20 0a 2a 2a  time error.  .**
2f8e7 0a 2a 2a 20 57 68 65 6e 20 76 69 72 74 75 61 6c  .** When virtual
2f8e8 20 74 61 62 6c 65 73 20 69 6e 74 65 6e 64 20 74   tables intend t
2f8e9 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 6f 76 65  o provide an ove
2f8ea 72 6c 6f 61 64 65 64 20 66 75 6e 63 74 69 6f 6e  rloaded function
2f8eb 2c 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75 6c 64  , they.** should
2f8ec 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
2f8ed 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ne to make sure 
2f8ee 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74  the global funct
2f8ef 69 6f 6e 20 65 78 69 73 74 73 2e 0a 2a 2a 20 41  ion exists..** A
2f8f0 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e   global function
2f8f1 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f   must exist in o
2f8f2 72 64 65 72 20 66 6f 72 20 6e 61 6d 65 20 72 65  rder for name re
2f8f3 73 6f 6c 75 74 69 6f 6e 20 74 6f 20 77 6f 72 6b  solution to work
2f8f4 0a 2a 2a 20 70 72 6f 70 65 72 6c 79 2e 0a 2a 2f  .** properly..*/
2f8f5 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2f8f6 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
2f8f7 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c  _function(.  sql
2f8f8 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73  ite3 *db,.  cons
2f8f9 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20  t char *zName,. 
2f8fa 20 69 6e 74 20 6e 41 72 67 0a 29 7b 0a 20 20 69   int nArg.){.  i
2f8fb 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
2f8fc 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61  e3Strlen(db, zNa
2f8fd 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  me);.  int rc;. 
2f8fe 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2f8ff 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2f900 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
2f901 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  ndFunction(db, z
2f902 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
2f903 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g, SQLITE_UTF8, 
2f904 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  0)==0 ){.    sql
2f905 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
2f906 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  b, zName, nArg, 
2f907 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20  SQLITE_UTF8,.   
2f908 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f909 20 20 20 30 2c 20 73 71 6c 69 74 65 33 49 6e 76     0, sqlite3Inv
2f90a 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c 20 30 2c  alidFunction, 0,
2f90b 20 30 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20   0);.  }.  rc = 
2f90c 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2f90d 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20  b, SQLITE_OK);. 
2f90e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2f90f 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2f910 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2f911 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2f912 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a  OMIT_TRACE./*.**
2f913 20 52 65 67 69 73 74 65 72 20 61 20 74 72 61 63   Register a trac
2f914 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  e function.  The
2f915 20 70 41 72 67 20 66 72 6f 6d 20 74 68 65 20 70   pArg from the p
2f916 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74  reviously regist
2f917 65 72 65 64 20 74 72 61 63 65 0a 2a 2a 20 69 73  ered trace.** is
2f918 20 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a 0a   returned.  .**.
2f919 2a 2a 20 41 20 4e 55 4c 4c 20 74 72 61 63 65 20  ** A NULL trace 
2f91a 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74  function means t
2f91b 68 61 74 20 6e 6f 20 74 72 61 63 69 6e 67 20 69  hat no tracing i
2f91c 73 20 65 78 65 63 75 74 65 73 2e 20 20 41 20 6e  s executes.  A n
2f91d 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74 72 61 63 65  on-NULL.** trace
2f91e 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2f91f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74   a function that
2f920 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 20 74   is invoked at t
2f921 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68  he start of each
2f922 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  .** SQL statemen
2f923 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
2f924 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 74   void *sqlite3_t
2f925 72 61 63 65 28 73 71 6c 69 74 65 33 20 2a 64 62  race(sqlite3 *db
2f926 2c 20 76 6f 69 64 20 28 2a 78 54 72 61 63 65 29  , void (*xTrace)
2f927 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
2f928 72 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72 67 29  r*), void *pArg)
2f929 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a  {.  void *pOld;.
2f92a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2f92b 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2f92c 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70  ;.  pOld = db->p
2f92d 54 72 61 63 65 41 72 67 3b 0a 20 20 64 62 2d 3e  TraceArg;.  db->
2f92e 78 54 72 61 63 65 20 3d 20 78 54 72 61 63 65 3b  xTrace = xTrace;
2f92f 0a 20 20 64 62 2d 3e 70 54 72 61 63 65 41 72 67  .  db->pTraceArg
2f930 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
2f931 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2f932 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2f933 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 2f 2a 0a 2a  urn pOld;.}./*.*
2f934 2a 20 52 65 67 69 73 74 65 72 20 61 20 70 72 6f  * Register a pro
2f935 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20  file function.  
2f936 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74 68  The pArg from th
2f937 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  e previously reg
2f938 69 73 74 65 72 65 64 20 0a 2a 2a 20 70 72 6f 66  istered .** prof
2f939 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ile function is 
2f93a 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a 0a 2a  returned.  .**.*
2f93b 2a 20 41 20 4e 55 4c 4c 20 70 72 6f 66 69 6c 65  * A NULL profile
2f93c 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73 20   function means 
2f93d 74 68 61 74 20 6e 6f 20 70 72 6f 66 69 6c 69 6e  that no profilin
2f93e 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20 20  g is executes.  
2f93f 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 70 72  A non-NULL.** pr
2f940 6f 66 69 6c 65 20 69 73 20 61 20 70 6f 69 6e 74  ofile is a point
2f941 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
2f942 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64   that is invoked
2f943 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   at the conclusi
2f944 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68 20 53 51  on of.** each SQ
2f945 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  L statement that
2f946 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49   is run..*/.SQLI
2f947 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
2f948 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 0a 20 20  ite3_profile(.  
2f949 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76  sqlite3 *db,.  v
2f94a 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28  oid (*xProfile)(
2f94b 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
2f94c 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  *,sqlite_uint64)
2f94d 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29  ,.  void *pArg.)
2f94e 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a  {.  void *pOld;.
2f94f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2f950 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2f951 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70  ;.  pOld = db->p
2f952 50 72 6f 66 69 6c 65 41 72 67 3b 0a 20 20 64 62  ProfileArg;.  db
2f953 2d 3e 78 50 72 6f 66 69 6c 65 20 3d 20 78 50 72  ->xProfile = xPr
2f954 6f 66 69 6c 65 3b 0a 20 20 64 62 2d 3e 70 50 72  ofile;.  db->pPr
2f955 6f 66 69 6c 65 41 72 67 20 3d 20 70 41 72 67 3b  ofileArg = pArg;
2f956 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2f957 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2f958 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64  );.  return pOld
2f959 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
2f95a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20  LITE_OMIT_TRACE 
2f95b 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50 45 52 49 4d  */../*** EXPERIM
2f95c 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  ENTAL ***.**.** 
2f95d 52 65 67 69 73 74 65 72 20 61 20 66 75 6e 63 74  Register a funct
2f95e 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65  ion to be invoke
2f95f 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63  d when a transac
2f960 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a  tion comments..*
2f961 2a 20 49 66 20 74 68 65 20 69 6e 76 6f 6b 65 64  * If the invoked
2f962 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2f963 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
2f964 20 74 68 65 20 63 6f 6d 6d 69 74 20 62 65 63 6f   the commit beco
2f965 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c 6c 62 61 63  mes a.** rollbac
2f966 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  k..*/.SQLITE_API
2f967 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
2f968 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20 20 73 71  ommit_hook(.  sq
2f969 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2f96a 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63          /* Attac
2f96b 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68  h the hook to th
2f96c 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  is database */. 
2f96d 20 69 6e 74 20 28 2a 78 43 61 6c 6c 62 61 63 6b   int (*xCallback
2f96e 29 28 76 6f 69 64 2a 29 2c 20 20 2f 2a 20 46 75  )(void*),  /* Fu
2f96f 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65  nction to invoke
2f970 20 6f 6e 20 65 61 63 68 20 63 6f 6d 6d 69 74 20   on each commit 
2f971 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20  */.  void *pArg 
2f972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f973 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
2f974 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  e function */.){
2f975 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20  .  void *pOld;. 
2f976 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2f977 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2f978 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70 43  .  pOld = db->pC
2f979 6f 6d 6d 69 74 41 72 67 3b 0a 20 20 64 62 2d 3e  ommitArg;.  db->
2f97a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20  xCommitCallback 
2f97b 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64  = xCallback;.  d
2f97c 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 20 3d 20  b->pCommitArg = 
2f97d 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pArg;.  sqlite3_
2f97e 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2f97f 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2f980 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pOld;.}../*.** 
2f981 52 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c 62  Register a callb
2f982 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65  ack to be invoke
2f983 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 72 6f  d each time a ro
2f984 77 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a  w is updated,.**
2f985 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c   inserted or del
2f986 65 74 65 64 20 75 73 69 6e 67 20 74 68 69 73 20  eted using this 
2f987 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2f988 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ion..*/.SQLITE_A
2f989 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
2f98a 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20  _update_hook(.  
2f98b 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2f98c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74            /* Att
2f98d 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20  ach the hook to 
2f98e 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
2f98f 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62  .  void (*xCallb
2f990 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  ack)(void*,int,c
2f991 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72  har const *,char
2f992 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 5f   const *,sqlite_
2f993 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a  int64),.  void *
2f994 70 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20  pArg            
2f995 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
2f996 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
2f997 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52  */.){.  void *pR
2f998 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  et;.  sqlite3_mu
2f999 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2f99a 74 65 78 29 3b 0a 20 20 70 52 65 74 20 3d 20 64  tex);.  pRet = d
2f99b 62 2d 3e 70 55 70 64 61 74 65 41 72 67 3b 0a 20  b->pUpdateArg;. 
2f99c 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c   db->xUpdateCall
2f99d 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
2f99e 3b 0a 20 20 64 62 2d 3e 70 55 70 64 61 74 65 41  ;.  db->pUpdateA
2f99f 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c  rg = pArg;.  sql
2f9a0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2f9a1 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2f9a2 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f  eturn pRet;.}../
2f9a3 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
2f9a4 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69  callback to be i
2f9a5 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65  nvoked each time
2f9a6 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
2f9a7 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b  s rolled.** back
2f9a8 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73   by this databas
2f9a9 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
2f9aa 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2f9ab 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61   *sqlite3_rollba
2f9ac 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74  ck_hook(.  sqlit
2f9ad 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2f9ae 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74       /* Attach t
2f9af 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20  he hook to this 
2f9b0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f  database */.  vo
2f9b1 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28  id (*xCallback)(
2f9b2 76 6f 69 64 2a 29 2c 20 2f 2a 20 43 61 6c 6c 62  void*), /* Callb
2f9b3 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ack function */.
2f9b4 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20 20 20    void *pArg    
2f9b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2f9b6 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66  rgument to the f
2f9b7 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
2f9b8 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20 73 71  void *pRet;.  sq
2f9b9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
2f9ba 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
2f9bb 70 52 65 74 20 3d 20 64 62 2d 3e 70 52 6f 6c 6c  pRet = db->pRoll
2f9bc 62 61 63 6b 41 72 67 3b 0a 20 20 64 62 2d 3e 78  backArg;.  db->x
2f9bd 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b  RollbackCallback
2f9be 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20   = xCallback;.  
2f9bf 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67  db->pRollbackArg
2f9c0 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
2f9c1 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2f9c2 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2f9c3 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a  urn pRet;.}../*.
2f9c4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2f9c5 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65  is called to cre
2f9c6 61 74 65 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e  ate a connection
2f9c7 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 42   to a database B
2f9c8 54 72 65 65 0a 2a 2a 20 64 72 69 76 65 72 2e 20  Tree.** driver. 
2f9c9 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   If zFilename is
2f9ca 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66   the name of a f
2f9cb 69 6c 65 2c 20 74 68 65 6e 20 74 68 61 74 20 66  ile, then that f
2f9cc 69 6c 65 20 69 73 0a 2a 2a 20 6f 70 65 6e 65 64  ile is.** opened
2f9cd 20 61 6e 64 20 75 73 65 64 2e 20 20 49 66 20 7a   and used.  If z
2f9ce 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20  Filename is the 
2f9cf 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d  magic name ":mem
2f9d0 6f 72 79 3a 22 20 74 68 65 6e 0a 2a 2a 20 74 68  ory:" then.** th
2f9d1 65 20 64 61 74 61 62 61 73 65 20 69 73 20 73 74  e database is st
2f9d2 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 28  ored in memory (
2f9d3 61 6e 64 20 69 73 20 74 68 75 73 20 66 6f 72 67  and is thus forg
2f9d4 6f 74 74 65 6e 20 61 73 20 73 6f 6f 6e 20 61 73  otten as soon as
2f9d5 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  .** the connecti
2f9d6 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 29 20 20  on is closed.)  
2f9d7 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  If zFilename is 
2f9d8 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 61  NULL then the da
2f9d9 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 61 20 22  tabase.** is a "
2f9da 76 69 72 74 75 61 6c 22 20 64 61 74 61 62 61 73  virtual" databas
2f9db 65 20 66 6f 72 20 74 72 61 6e 73 69 65 6e 74 20  e for transient 
2f9dc 75 73 65 20 6f 6e 6c 79 20 61 6e 64 20 69 73 20  use only and is 
2f9dd 64 65 6c 65 74 65 64 20 61 73 0a 2a 2a 20 73 6f  deleted as.** so
2f9de 6f 6e 20 61 73 20 74 68 65 20 63 6f 6e 6e 65 63  on as the connec
2f9df 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a  tion is closed..
2f9e0 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c 20  **.** A virtual 
2f9e1 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65 20  database can be 
2f9e2 65 69 74 68 65 72 20 61 20 64 69 73 6b 20 66 69  either a disk fi
2f9e3 6c 65 20 28 74 68 61 74 20 69 73 20 61 75 74 6f  le (that is auto
2f9e4 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 64 65 6c  matically.** del
2f9e5 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 66 69  eted when the fi
2f9e6 6c 65 20 69 73 20 63 6c 6f 73 65 64 29 20 6f 72  le is closed) or
2f9e7 20 69 74 20 61 6e 20 62 65 20 68 65 6c 64 20 65   it an be held e
2f9e8 6e 74 69 72 65 6c 79 20 69 6e 20 6d 65 6d 6f 72  ntirely in memor
2f9e9 79 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20  y,.** depending 
2f9ea 6f 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  on the values of
2f9eb 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 4d 50   the SQLITE_TEMP
2f9ec 5f 53 54 4f 52 45 20 63 6f 6d 70 69 6c 65 2d 74  _STORE compile-t
2f9ed 69 6d 65 20 6d 61 63 72 6f 20 61 6e 64 20 74 68  ime macro and th
2f9ee 65 0a 2a 2a 20 64 62 2d 3e 74 65 6d 70 5f 73 74  e.** db->temp_st
2f9ef 6f 72 65 20 76 61 72 69 61 62 6c 65 2c 20 61 63  ore variable, ac
2f9f0 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66  cording to the f
2f9f1 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 3a 0a  ollowing chart:.
2f9f2 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 54  **.**   SQLITE_T
2f9f3 45 4d 50 5f 53 54 4f 52 45 20 20 20 20 20 64 62  EMP_STORE     db
2f9f4 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 20 20 20  ->temp_store    
2f9f5 20 4c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 65 6d   Location of tem
2f9f6 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 0a  porary database.
2f9f7 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
2f9f8 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d  ------     -----
2f9f9 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d  ---------     --
2f9fa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f9fb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
2f9fc 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
2f9fd 20 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20          any     
2f9fe 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 0a             file.
2f9ff 2a 2a 20 20 20 31 20 20 20 20 20 20 20 20 20 20  **   1          
2fa00 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20             1    
2fa01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
2fa02 6c 65 0a 2a 2a 20 20 20 31 20 20 20 20 20 20 20  le.**   1       
2fa03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 20                2 
2fa04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa05 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 31 20 20   memory.**   1  
2fa06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa07 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
2fa08 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a 20 20 20        file.**   
2fa09 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
2fa0a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20        1         
2fa0b 20 20 20 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a           file.**
2fa0c 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
2fa0d 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20           2      
2fa0e 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f              memo
2fa0f 72 79 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  ry.**   2       
2fa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
2fa11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa12 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 33 20 20   memory.**   3  
2fa13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa14 20 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20     any          
2fa15 20 20 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2f 0a        memory.*/.
2fa16 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2fa17 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
2fa18 61 63 74 6f 72 79 28 0a 20 20 63 6f 6e 73 74 20  actory(.  const 
2fa19 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2fa1a 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74 61      /* Main data
2fa1b 62 61 73 65 20 77 68 65 6e 20 6f 70 65 6e 69 6e  base when openin
2fa1c 67 20 61 75 78 20 6f 74 68 65 72 77 69 73 65 20  g aux otherwise 
2fa1d 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  0 */.  const cha
2fa1e 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
2fa1f 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2fa20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  file containing 
2fa21 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61  the BTree databa
2fa22 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d 69 74  se */.  int omit
2fa23 4a 6f 75 72 6e 61 6c 2c 20 20 20 20 20 20 20 20  Journal,        
2fa24 20 20 2f 2a 20 69 66 20 54 52 55 45 20 74 68 65    /* if TRUE the
2fa25 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c  n do not journal
2fa26 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
2fa27 69 6e 74 20 6e 43 61 63 68 65 2c 20 20 20 20 20  int nCache,     
2fa28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77            /* How
2fa29 20 6d 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74   many pages in t
2fa2a 68 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f  he page cache */
2fa2b 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c  .  int vfsFlags,
2fa2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2fa2d 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  Flags passed thr
2fa2e 6f 75 67 68 20 74 6f 20 76 66 73 4f 70 65 6e 20  ough to vfsOpen 
2fa2f 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42  */.  Btree **ppB
2fa30 74 72 65 65 20 20 20 20 20 20 20 20 20 20 20 2f  tree           /
2fa31 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77  * Pointer to new
2fa32 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72   Btree object wr
2fa33 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 7b  itten here */.){
2fa34 0a 20 20 69 6e 74 20 62 74 46 6c 61 67 73 20 3d  .  int btFlags =
2fa35 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20   0;.  int rc;.  
2fa36 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2fa37 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
2fa38 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
2fa39 73 65 72 74 28 20 70 70 42 74 72 65 65 20 21 3d  sert( ppBtree !=
2fa3a 20 30 29 3b 0a 20 20 69 66 28 20 6f 6d 69 74 4a   0);.  if( omitJ
2fa3b 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 62 74  ournal ){.    bt
2fa3c 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4f  Flags |= BTREE_O
2fa3d 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 7d  MIT_JOURNAL;.  }
2fa3e 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
2fa3f 20 26 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64   & SQLITE_NoRead
2fa40 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 62 74 46 6c  lock ){.    btFl
2fa41 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4e 4f 5f  ags |= BTREE_NO_
2fa42 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20  READLOCK;.  }.  
2fa43 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  if( zFilename==0
2fa44 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54   ){.#if SQLITE_T
2fa45 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20  EMP_STORE==0.   
2fa46 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a   /* Do nothing *
2fa47 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  /.#endif.#ifndef
2fa48 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d   SQLITE_OMIT_MEM
2fa49 4f 52 59 44 42 0a 23 69 66 20 53 51 4c 49 54 45  ORYDB.#if SQLITE
2fa4a 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a 20  _TEMP_STORE==1. 
2fa4b 20 20 20 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f     if( db->temp_
2fa4c 73 74 6f 72 65 3d 3d 32 20 29 20 7a 46 69 6c 65  store==2 ) zFile
2fa4d 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79 3a  name = ":memory:
2fa4e 22 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  ";.#endif.#if SQ
2fa4f 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d  LITE_TEMP_STORE=
2fa50 3d 32 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 74  =2.    if( db->t
2fa51 65 6d 70 5f 73 74 6f 72 65 21 3d 31 20 29 20 7a  emp_store!=1 ) z
2fa52 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d  Filename = ":mem
2fa53 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 69  ory:";.#endif.#i
2fa54 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  f SQLITE_TEMP_ST
2fa55 4f 52 45 3d 3d 33 0a 20 20 20 20 7a 46 69 6c 65  ORE==3.    zFile
2fa56 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79 3a  name = ":memory:
2fa57 22 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  ";.#endif.#endif
2fa58 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2fa59 4d 45 4d 4f 52 59 44 42 20 2a 2f 0a 20 20 7d 0a  MEMORYDB */.  }.
2fa5a 0a 20 20 69 66 28 20 28 76 66 73 46 6c 61 67 73  .  if( (vfsFlags
2fa5b 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d   & SQLITE_OPEN_M
2fa5c 41 49 4e 5f 44 42 29 21 3d 30 20 26 26 20 28 7a  AIN_DB)!=0 && (z
2fa5d 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 2a  Filename==0 || *
2fa5e 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 29 20 29 7b  zFilename==0) ){
2fa5f 0a 20 20 20 20 76 66 73 46 6c 61 67 73 20 3d 20  .    vfsFlags = 
2fa60 28 76 66 73 46 6c 61 67 73 20 26 20 7e 53 51 4c  (vfsFlags & ~SQL
2fa61 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
2fa62 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ) | SQLITE_OPEN_
2fa63 54 45 4d 50 5f 44 42 3b 0a 20 20 7d 0a 20 20 72  TEMP_DB;.  }.  r
2fa64 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
2fa65 4f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20  Open(zFilename, 
2fa66 28 73 71 6c 69 74 65 33 20 2a 29 64 62 2c 20 70  (sqlite3 *)db, p
2fa67 70 42 74 72 65 65 2c 20 62 74 46 6c 61 67 73 2c  pBtree, btFlags,
2fa68 20 76 66 73 46 6c 61 67 73 29 3b 0a 0a 20 20 2f   vfsFlags);..  /
2fa69 2a 20 49 66 20 74 68 65 20 42 2d 54 72 65 65 20  * If the B-Tree 
2fa6a 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79  was successfully
2fa6b 20 6f 70 65 6e 65 64 2c 20 73 65 74 20 74 68 65   opened, set the
2fa6c 20 70 61 67 65 72 2d 63 61 63 68 65 20 73 69 7a   pager-cache siz
2fa6d 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 64 65  e to the.  ** de
2fa6e 66 61 75 6c 74 20 76 61 6c 75 65 2e 20 45 78 63  fault value. Exc
2fa6f 65 70 74 2c 20 69 66 20 74 68 65 20 63 61 6c 6c  ept, if the call
2fa70 20 74 6f 20 42 74 72 65 65 4f 70 65 6e 28 29 20   to BtreeOpen() 
2fa71 72 65 74 75 72 6e 65 64 20 61 20 68 61 6e 64 6c  returned a handl
2fa72 65 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f 6e 20 61  e.  ** open on a
2fa73 6e 20 65 78 69 73 74 69 6e 67 20 73 68 61 72 65  n existing share
2fa74 64 20 70 61 67 65 72 2d 63 61 63 68 65 2c 20 64  d pager-cache, d
2fa75 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  o not change the
2fa76 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 20 20   pager-cache .  
2fa77 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20  ** size..  */.  
2fa78 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2fa79 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42  K && 0==sqlite3B
2fa7a 74 72 65 65 53 63 68 65 6d 61 28 2a 70 70 42 74  treeSchema(*ppBt
2fa7b 72 65 65 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20  ree, 0, 0) ){.  
2fa7c 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
2fa7d 74 43 61 63 68 65 53 69 7a 65 28 2a 70 70 42 74  tCacheSize(*ppBt
2fa7e 72 65 65 2c 20 6e 43 61 63 68 65 29 3b 0a 20 20  ree, nCache);.  
2fa7f 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
2fa80 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 55  ../*.** Return U
2fa81 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 45 6e 67  TF-8 encoded Eng
2fa82 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 65 78  lish language ex
2fa83 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65  planation of the
2fa84 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20   most recent.** 
2fa85 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
2fa86 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
2fa87 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  *sqlite3_errmsg(
2fa88 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2fa89 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20  const char *z;. 
2fa8a 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20   if( !db ){.    
2fa8b 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 72  return sqlite3Er
2fa8c 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f 4d 45  rStr(SQLITE_NOME
2fa8d 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73  M);.  }.  if( !s
2fa8e 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
2fa8f 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b  kSickOrOk(db) ){
2fa90 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
2fa91 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54 45  te3ErrStr(SQLITE
2fa92 5f 4d 49 53 55 53 45 29 3b 0a 20 20 7d 0a 20 20  _MISUSE);.  }.  
2fa93 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
2fa94 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
2fa95 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2fa96 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 7a 20  Failed ){.    z 
2fa97 3d 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  = sqlite3ErrStr(
2fa98 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20  SQLITE_NOMEM);. 
2fa99 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
2fa9a 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76  (char*)sqlite3_v
2fa9b 61 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45  alue_text(db->pE
2fa9c 72 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  rr);.    assert(
2fa9d 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
2fa9e 65 64 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d  ed );.    if( z=
2fa9f 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20  =0 ){.      z = 
2faa0 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 64 62  sqlite3ErrStr(db
2faa1 2d 3e 65 72 72 43 6f 64 65 29 3b 0a 20 20 20 20  ->errCode);.    
2faa2 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
2faa3 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2faa4 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2faa5 20 7a 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53   z;.}..#ifndef S
2faa6 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2faa7 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54  ./*.** Return UT
2faa8 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 45 6e 67  F-16 encoded Eng
2faa9 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 65 78  lish language ex
2faaa 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65  planation of the
2faab 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20   most recent.** 
2faac 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
2faad 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
2faae 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31  *sqlite3_errmsg1
2faaf 36 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  6(sqlite3 *db){.
2fab0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2fab1 31 36 20 6f 75 74 4f 66 4d 65 6d 5b 5d 20 3d 20  16 outOfMem[] = 
2fab2 7b 0a 20 20 20 20 27 6f 27 2c 20 27 75 27 2c 20  {.    'o', 'u', 
2fab3 27 74 27 2c 20 27 20 27 2c 20 27 6f 27 2c 20 27  't', ' ', 'o', '
2fab4 66 27 2c 20 27 20 27 2c 20 27 6d 27 2c 20 27 65  f', ' ', 'm', 'e
2fab5 27 2c 20 27 6d 27 2c 20 27 6f 27 2c 20 27 72 27  ', 'm', 'o', 'r'
2fab6 2c 20 27 79 27 2c 20 30 0a 20 20 7d 3b 0a 20 20  , 'y', 0.  };.  
2fab7 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 31 36  static const u16
2fab8 20 6d 69 73 75 73 65 5b 5d 20 3d 20 7b 0a 20 20   misuse[] = {.  
2fab9 20 20 27 6c 27 2c 20 27 69 27 2c 20 27 62 27 2c    'l', 'i', 'b',
2faba 20 27 72 27 2c 20 27 61 27 2c 20 27 72 27 2c 20   'r', 'a', 'r', 
2fabb 27 79 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27  'y', ' ', .    '
2fabc 72 27 2c 20 27 6f 27 2c 20 27 75 27 2c 20 27 74  r', 'o', 'u', 't
2fabd 27 2c 20 27 69 27 2c 20 27 6e 27 2c 20 27 65 27  ', 'i', 'n', 'e'
2fabe 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 63 27 2c  , ' ', .    'c',
2fabf 20 27 61 27 2c 20 27 6c 27 2c 20 27 6c 27 2c 20   'a', 'l', 'l', 
2fac0 27 65 27 2c 20 27 64 27 2c 20 27 20 27 2c 20 0a  'e', 'd', ' ', .
2fac1 20 20 20 20 27 6f 27 2c 20 27 75 27 2c 20 27 74      'o', 'u', 't
2fac2 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 6f 27  ', ' ', .    'o'
2fac3 2c 20 27 66 27 2c 20 27 20 27 2c 20 0a 20 20 20  , 'f', ' ', .   
2fac4 20 27 73 27 2c 20 27 65 27 2c 20 27 71 27 2c 20   's', 'e', 'q', 
2fac5 27 75 27 2c 20 27 65 27 2c 20 27 6e 27 2c 20 27  'u', 'e', 'n', '
2fac6 63 27 2c 20 27 65 27 2c 20 30 0a 20 20 7d 3b 0a  c', 'e', 0.  };.
2fac7 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
2fac8 3b 0a 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20  ;.  if( !db ){. 
2fac9 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 20     return (void 
2faca 2a 29 6f 75 74 4f 66 4d 65 6d 3b 0a 20 20 7d 0a  *)outOfMem;.  }.
2facb 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61    if( !sqlite3Sa
2facc 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f  fetyCheckSickOrO
2facd 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  k(db) ){.    ret
2face 75 72 6e 20 28 76 6f 69 64 20 2a 29 6d 69 73 75  urn (void *)misu
2facf 73 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  se;.  }.  sqlite
2fad0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
2fad1 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
2fad2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2fad3 20 29 7b 0a 20 20 20 20 7a 20 3d 20 28 76 6f 69   ){.    z = (voi
2fad4 64 20 2a 29 6f 75 74 4f 66 4d 65 6d 3b 0a 20 20  d *)outOfMem;.  
2fad5 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73  }else{.    z = s
2fad6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2fad7 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20  t16(db->pErr);. 
2fad8 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20     if( z==0 ){. 
2fad9 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
2fada 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72  eSetStr(db->pErr
2fadb 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 45 72 72  , -1, sqlite3Err
2fadc 53 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29  Str(db->errCode)
2fadd 2c 0a 20 20 20 20 20 20 20 20 20 20 20 53 51 4c  ,.           SQL
2fade 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
2fadf 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
2fae0 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
2fae1 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70 45 72  e_text16(db->pEr
2fae2 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a  r);.    }.    /*
2fae3 20 41 20 6d 61 6c 6c 6f 63 28 29 20 6d 61 79 20   A malloc() may 
2fae4 68 61 76 65 20 66 61 69 6c 65 64 20 77 69 74 68  have failed with
2fae5 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73  in the call to s
2fae6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2fae7 74 31 36 28 29 0a 20 20 20 20 2a 2a 20 61 62 6f  t16().    ** abo
2fae8 76 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 74  ve. If this is t
2fae9 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68  he case, then th
2faea 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  e db->mallocFail
2faeb 65 64 20 66 6c 61 67 20 6e 65 65 64 73 20 74 6f  ed flag needs to
2faec 0a 20 20 20 20 2a 2a 20 62 65 20 63 6c 65 61 72  .    ** be clear
2faed 65 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  ed before return
2faee 69 6e 67 2e 20 44 6f 20 74 68 69 73 20 64 69 72  ing. Do this dir
2faef 65 63 74 6c 79 2c 20 69 6e 73 74 65 61 64 20 6f  ectly, instead o
2faf0 66 20 76 69 61 0a 20 20 20 20 2a 2a 20 73 71 6c  f via.    ** sql
2faf1 69 74 65 33 41 70 69 45 78 69 74 28 29 2c 20 74  ite3ApiExit(), t
2faf2 6f 20 61 76 6f 69 64 20 73 65 74 74 69 6e 67 20  o avoid setting 
2faf3 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
2faf4 64 6c 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  dle error messag
2faf5 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 62  e..    */.    db
2faf6 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2faf7 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
2faf8 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
2faf9 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
2fafa 72 6e 20 7a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn z;.}.#endif /
2fafb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
2fafc 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  F16 */../*.** Re
2fafd 74 75 72 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  turn the most re
2fafe 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20  cent error code 
2faff 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 6e 20  generated by an 
2fb00 53 51 4c 69 74 65 20 72 6f 75 74 69 6e 65 2e 20  SQLite routine. 
2fb01 49 66 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 70 61  If NULL is.** pa
2fb02 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e  ssed to this fun
2fb03 63 74 69 6f 6e 2c 20 77 65 20 61 73 73 75 6d 65  ction, we assume
2fb04 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
2fb05 65 64 20 64 75 72 69 6e 67 20 73 71 6c 69 74 65  ed during sqlite
2fb06 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c  3_open()..*/.SQL
2fb07 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2fb08 74 65 33 5f 65 72 72 63 6f 64 65 28 73 71 6c 69  te3_errcode(sqli
2fb09 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20  te3 *db){.  if( 
2fb0a 64 62 20 26 26 20 21 73 71 6c 69 74 65 33 53 61  db && !sqlite3Sa
2fb0b 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f  fetyCheckSickOrO
2fb0c 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  k(db) ){.    ret
2fb0d 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
2fb0e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62  E;.  }.  if( !db
2fb0f 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2fb10 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  iled ){.    retu
2fb11 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2fb12 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 64 62  .  }.  return db
2fb13 2d 3e 65 72 72 43 6f 64 65 20 26 20 64 62 2d 3e  ->errCode & db->
2fb14 65 72 72 4d 61 73 6b 3b 0a 7d 0a 53 51 4c 49 54  errMask;.}.SQLIT
2fb15 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2fb16 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f  3_extended_errco
2fb17 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  de(sqlite3 *db){
2fb18 0a 20 20 69 66 28 20 64 62 20 26 26 20 21 73 71  .  if( db && !sq
2fb19 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
2fb1a 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a  SickOrOk(db) ){.
2fb1b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2fb1c 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
2fb1d 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d  if( !db || db->m
2fb1e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2fb1f 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2fb20 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65  _NOMEM;.  }.  re
2fb21 74 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65  turn db->errCode
2fb22 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
2fb23 65 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6e  e a new collatin
2fb24 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 64  g function for d
2fb25 61 74 61 62 61 73 65 20 22 64 62 22 2e 20 20 54  atabase "db".  T
2fb26 68 65 20 6e 61 6d 65 20 69 73 20 7a 4e 61 6d 65  he name is zName
2fb27 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 6e 63 6f  .** and the enco
2fb28 64 69 6e 67 20 69 73 20 65 6e 63 2e 0a 2a 2f 0a  ding is enc..*/.
2fb29 73 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74  static int creat
2fb2a 65 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71  eCollation(.  sq
2fb2b 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f  lite3* db, .  co
2fb2c 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
2fb2d 20 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20   .  int enc, .  
2fb2e 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e  void* pCtx,.  in
2fb2f 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69  t(*xCompare)(voi
2fb30 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2fb31 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
2fb32 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65  d*),.  void(*xDe
2fb33 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 43  l)(void*).){.  C
2fb34 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
2fb35 20 69 6e 74 20 65 6e 63 32 3b 0a 20 20 69 6e 74   int enc2;.  int
2fb36 20 6e 4e 61 6d 65 3b 0a 20 20 0a 20 20 61 73 73   nName;.  .  ass
2fb37 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
2fb38 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
2fb39 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 53  x) );..  /* If S
2fb3a 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 73  QLITE_UTF16 is s
2fb3b 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
2fb3c 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c 20 74  encoding type, t
2fb3d 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a 20 20  ransform this.  
2fb3e 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53 51 4c  ** to one of SQL
2fb3f 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53  ITE_UTF16LE or S
2fb40 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 75 73  QLITE_UTF16BE us
2fb41 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c  ing the.  ** SQL
2fb42 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
2fb43 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f 55 54  macro. SQLITE_UT
2fb44 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65 64 20  F16 is not used 
2fb45 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f  internally..  */
2fb46 0a 20 20 65 6e 63 32 20 3d 20 65 6e 63 20 26 20  .  enc2 = enc & 
2fb47 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
2fb48 49 47 4e 45 44 3b 0a 20 20 69 66 28 20 65 6e 63  IGNED;.  if( enc
2fb49 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20  2==SQLITE_UTF16 
2fb4a 29 7b 0a 20 20 20 20 65 6e 63 32 20 3d 20 53 51  ){.    enc2 = SQ
2fb4b 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
2fb4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 65 6e 63  ;.  }.  if( (enc
2fb4d 32 26 7e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20  2&~3)!=0 ){.    
2fb4e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
2fb4f 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  SUSE;.  }..  /* 
2fb50 43 68 65 63 6b 20 69 66 20 74 68 69 73 20 63 61  Check if this ca
2fb51 6c 6c 20 69 73 20 72 65 6d 6f 76 69 6e 67 20 6f  ll is removing o
2fb52 72 20 72 65 70 6c 61 63 69 6e 67 20 61 6e 20 65  r replacing an e
2fb53 78 69 73 74 69 6e 67 20 63 6f 6c 6c 61 74 69 6f  xisting collatio
2fb54 6e 20 0a 20 20 2a 2a 20 73 65 71 75 65 6e 63 65  n .  ** sequence
2fb55 2e 20 49 66 20 73 6f 2c 20 61 6e 64 20 74 68 65  . If so, and the
2fb56 72 65 20 61 72 65 20 61 63 74 69 76 65 20 56 4d  re are active VM
2fb57 73 2c 20 72 65 74 75 72 6e 20 62 75 73 79 2e 20  s, return busy. 
2fb58 49 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 61 72  If there.  ** ar
2fb59 65 20 6e 6f 20 61 63 74 69 76 65 20 56 4d 73 2c  e no active VMs,
2fb5a 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20   invalidate any 
2fb5b 70 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61  pre-compiled sta
2fb5c 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20  tements..  */.  
2fb5d 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
2fb5e 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 6d 65 29  trlen(db, zName)
2fb5f 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ;.  pColl = sqli
2fb60 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
2fb61 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e 61  b, (u8)enc2, zNa
2fb62 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20  me, nName, 0);. 
2fb63 20 69 66 28 20 70 43 6f 6c 6c 20 26 26 20 70 43   if( pColl && pC
2fb64 6f 6c 6c 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20  oll->xCmp ){.   
2fb65 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56   if( db->activeV
2fb66 64 62 65 43 6e 74 20 29 7b 0a 20 20 20 20 20 20  dbeCnt ){.      
2fb67 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2fb68 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20   SQLITE_BUSY, . 
2fb69 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74         "unable t
2fb6a 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20  o delete/modify 
2fb6b 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2fb6c 63 65 20 64 75 65 20 74 6f 20 61 63 74 69 76 65  ce due to active
2fb6d 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20   statements");. 
2fb6e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2fb6f 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20  TE_BUSY;.    }. 
2fb70 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
2fb71 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
2fb72 74 73 28 64 62 29 3b 0a 0a 20 20 20 20 2f 2a 20  ts(db);..    /* 
2fb73 49 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  If collation seq
2fb74 75 65 6e 63 65 20 70 43 6f 6c 6c 20 77 61 73 20  uence pColl was 
2fb75 63 72 65 61 74 65 64 20 64 69 72 65 63 74 6c 79  created directly
2fb76 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 20 20   by a call to.  
2fb77 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 65    ** sqlite3_cre
2fb78 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 20 61  ate_collation, a
2fb79 6e 64 20 6e 6f 74 20 67 65 6e 65 72 61 74 65 64  nd not generated
2fb7a 20 62 79 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71   by synthCollSeq
2fb7b 28 29 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20  (),.    ** then 
2fb7c 61 6e 79 20 63 6f 70 69 65 73 20 6d 61 64 65 20  any copies made 
2fb7d 62 79 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28  by synthCollSeq(
2fb7e 29 20 6e 65 65 64 20 74 6f 20 62 65 20 69 6e 76  ) need to be inv
2fb7f 61 6c 69 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a  alidated..    **
2fb80 20 41 6c 73 6f 2c 20 63 6f 6c 6c 61 74 69 6f 6e   Also, collation
2fb81 20 64 65 73 74 72 75 63 74 6f 72 20 2d 20 43 6f   destructor - Co
2fb82 6c 6c 53 65 71 2e 78 44 65 6c 28 29 20 2d 20 66  llSeq.xDel() - f
2fb83 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6e 65 65 64  unction may need
2fb84 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 63 61  .    ** to be ca
2fb85 6c 6c 65 64 2e 0a 20 20 20 20 2a 2f 20 0a 20 20  lled..    */ .  
2fb86 20 20 69 66 28 20 28 70 43 6f 6c 6c 2d 3e 65 6e    if( (pColl->en
2fb87 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31  c & ~SQLITE_UTF1
2fb88 36 5f 41 4c 49 47 4e 45 44 29 3d 3d 65 6e 63 32  6_ALIGNED)==enc2
2fb89 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65   ){.      CollSe
2fb8a 71 20 2a 61 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  q *aColl = sqlit
2fb8b 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e  e3HashFind(&db->
2fb8c 61 43 6f 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c  aCollSeq, zName,
2fb8d 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69   nName);.      i
2fb8e 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt j;.      for(
2fb8f 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a  j=0; j<3; j++){.
2fb90 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
2fb91 2a 70 20 3d 20 26 61 43 6f 6c 6c 5b 6a 5d 3b 0a  *p = &aColl[j];.
2fb92 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65          if( p->e
2fb93 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29  nc==pColl->enc )
2fb94 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
2fb95 70 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 20  p->xDel ){.     
2fb96 20 20 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 70         p->xDel(p
2fb97 2d 3e 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20  ->pUser);.      
2fb98 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2fb99 70 2d 3e 78 43 6d 70 20 3d 20 30 3b 0a 20 20 20  p->xCmp = 0;.   
2fb9a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2fb9b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 43 6f 6c     }.  }..  pCol
2fb9c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
2fb9d 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75 38 29 65  ollSeq(db, (u8)e
2fb9e 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  nc2, zName, nNam
2fb9f 65 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 43 6f  e, 1);.  if( pCo
2fba0 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 2d  ll ){.    pColl-
2fba1 3e 78 43 6d 70 20 3d 20 78 43 6f 6d 70 61 72 65  >xCmp = xCompare
2fba2 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 70 55 73  ;.    pColl->pUs
2fba3 65 72 20 3d 20 70 43 74 78 3b 0a 20 20 20 20 70  er = pCtx;.    p
2fba4 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20 78 44 65  Coll->xDel = xDe
2fba5 6c 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 65 6e  l;.    pColl->en
2fba6 63 20 3d 20 28 75 38 29 28 65 6e 63 32 20 7c 20  c = (u8)(enc2 | 
2fba7 28 65 6e 63 20 26 20 53 51 4c 49 54 45 5f 55 54  (enc & SQLITE_UT
2fba8 46 31 36 5f 41 4c 49 47 4e 45 44 29 29 3b 0a 20  F16_ALIGNED));. 
2fba9 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f   }.  sqlite3Erro
2fbaa 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  r(db, SQLITE_OK,
2fbab 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51   0);.  return SQ
2fbac 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
2fbad 2a 2a 20 54 68 69 73 20 61 72 72 61 79 20 64 65  ** This array de
2fbae 66 69 6e 65 73 20 68 61 72 64 20 75 70 70 65 72  fines hard upper
2fbaf 20 62 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d 69 74   bounds on limit
2fbb0 20 76 61 6c 75 65 73 2e 20 20 54 68 65 0a 2a 2a   values.  The.**
2fbb1 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 6d 75 73   initializer mus
2fbb2 74 20 62 65 20 6b 65 70 74 20 69 6e 20 73 79 6e  t be kept in syn
2fbb3 63 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  c with the SQLIT
2fbb4 45 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23 64 65  E_LIMIT_*.** #de
2fbb5 66 69 6e 65 73 20 69 6e 20 73 71 6c 69 74 65 33  fines in sqlite3
2fbb6 2e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  .h..*/.static co
2fbb7 6e 73 74 20 69 6e 74 20 61 48 61 72 64 4c 69 6d  nst int aHardLim
2fbb8 69 74 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c 49 54  it[] = {.  SQLIT
2fbb9 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a 20 20  E_MAX_LENGTH,.  
2fbba 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
2fbbb 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f  ENGTH,.  SQLITE_
2fbbc 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20 53 51  MAX_COLUMN,.  SQ
2fbbd 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
2fbbe 50 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  PTH,.  SQLITE_MA
2fbbf 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  X_COMPOUND_SELEC
2fbc0 54 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  T,.  SQLITE_MAX_
2fbc1 56 44 42 45 5f 4f 50 2c 0a 20 20 53 51 4c 49 54  VDBE_OP,.  SQLIT
2fbc2 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41  E_MAX_FUNCTION_A
2fbc3 52 47 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  RG,.  SQLITE_MAX
2fbc4 5f 41 54 54 41 43 48 45 44 2c 0a 20 20 53 51 4c  _ATTACHED,.  SQL
2fbc5 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54  ITE_MAX_LIKE_PAT
2fbc6 54 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20 20 53  TERN_LENGTH,.  S
2fbc7 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42  QLITE_MAX_VARIAB
2fbc8 4c 45 5f 4e 55 4d 42 45 52 2c 0a 7d 3b 0a 0a 2f  LE_NUMBER,.};../
2fbc9 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  *.** Make sure t
2fbca 68 65 20 68 61 72 64 20 6c 69 6d 69 74 73 20 61  he hard limits a
2fbcb 72 65 20 73 65 74 20 74 6f 20 72 65 61 73 6f 6e  re set to reason
2fbcc 61 62 6c 65 20 76 61 6c 75 65 73 0a 2a 2f 0a 23  able values.*/.#
2fbcd 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  if SQLITE_MAX_LE
2fbce 4e 47 54 48 3c 31 30 30 0a 23 20 65 72 72 6f 72  NGTH<100.# error
2fbcf 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
2fbd0 54 48 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65  TH must be at le
2fbd1 61 73 74 20 31 30 30 0a 23 65 6e 64 69 66 0a 23  ast 100.#endif.#
2fbd2 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  if SQLITE_MAX_SQ
2fbd3 4c 5f 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20 65  L_LENGTH<100.# e
2fbd4 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
2fbd5 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20  SQL_LENGTH must 
2fbd6 62 65 20 61 74 20 6c 65 61 73 74 20 31 30 30 0a  be at least 100.
2fbd7 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
2fbd8 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48  E_MAX_SQL_LENGTH
2fbd9 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47  >SQLITE_MAX_LENG
2fbda 54 48 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  TH.# error SQLIT
2fbdb 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48  E_MAX_SQL_LENGTH
2fbdc 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 67 72 65   must not be gre
2fbdd 61 74 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ater than SQLITE
2fbde 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 65 6e 64  _MAX_LENGTH.#end
2fbdf 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
2fbe0 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  X_COMPOUND_SELEC
2fbe1 54 3c 32 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  T<2.# error SQLI
2fbe2 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f  TE_MAX_COMPOUND_
2fbe3 53 45 4c 45 43 54 20 6d 75 73 74 20 62 65 20 61  SELECT must be a
2fbe4 74 20 6c 65 61 73 74 20 32 0a 23 65 6e 64 69 66  t least 2.#endif
2fbe5 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
2fbe6 56 44 42 45 5f 4f 50 3c 34 30 0a 23 20 65 72 72  VDBE_OP<40.# err
2fbe7 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44  or SQLITE_MAX_VD
2fbe8 42 45 5f 4f 50 20 6d 75 73 74 20 62 65 20 61 74  BE_OP must be at
2fbe9 20 6c 65 61 73 74 20 34 30 0a 23 65 6e 64 69 66   least 40.#endif
2fbea 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
2fbeb 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c 30 20 7c  FUNCTION_ARG<0 |
2fbec 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e  | SQLITE_MAX_FUN
2fbed 43 54 49 4f 4e 5f 41 52 47 3e 31 30 30 30 0a 23  CTION_ARG>1000.#
2fbee 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
2fbef 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 6d  X_FUNCTION_ARG m
2fbf0 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 30  ust be between 0
2fbf1 20 61 6e 64 20 31 30 30 30 0a 23 65 6e 64 69 66   and 1000.#endif
2fbf2 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
2fbf3 41 54 54 41 43 48 45 44 3c 30 20 7c 7c 20 53 51  ATTACHED<0 || SQ
2fbf4 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45  LITE_MAX_ATTACHE
2fbf5 44 3e 33 30 0a 23 20 65 72 72 6f 72 20 53 51 4c  D>30.# error SQL
2fbf6 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44  ITE_MAX_ATTACHED
2fbf7 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
2fbf8 20 30 20 61 6e 64 20 33 30 0a 23 65 6e 64 69 66   0 and 30.#endif
2fbf9 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
2fbfa 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
2fbfb 47 54 48 3c 31 0a 23 20 65 72 72 6f 72 20 53 51  GTH<1.# error SQ
2fbfc 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41  LITE_MAX_LIKE_PA
2fbfd 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 6d 75 73  TTERN_LENGTH mus
2fbfe 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a  t be at least 1.
2fbff 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
2fc00 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e  E_MAX_VARIABLE_N
2fc01 55 4d 42 45 52 3c 31 0a 23 20 65 72 72 6f 72 20  UMBER<1.# error 
2fc02 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
2fc03 42 4c 45 5f 4e 55 4d 42 45 52 20 6d 75 73 74 20  BLE_NUMBER must 
2fc04 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65  be at least 1.#e
2fc05 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
2fc06 4d 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32 37 36 37  MAX_COLUMN>32767
2fc07 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
2fc08 4d 41 58 5f 43 4f 4c 55 4d 4e 20 6d 75 73 74 20  MAX_COLUMN must 
2fc09 6e 6f 74 20 65 78 63 65 65 64 20 33 32 37 36 37  not exceed 32767
2fc0a 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
2fc0b 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
2fc0c 20 6f 66 20 61 20 6c 69 6d 69 74 2e 20 20 52 65   of a limit.  Re
2fc0d 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76 61 6c  port the old val
2fc0e 75 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 76  ue..** If an inv
2fc0f 61 6c 69 64 20 6c 69 6d 69 74 20 69 6e 64 65 78  alid limit index
2fc10 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 72 65   is supplied, re
2fc11 70 6f 72 74 20 2d 31 2e 0a 2a 2a 20 4d 61 6b 65  port -1..** Make
2fc12 20 6e 6f 20 63 68 61 6e 67 65 73 20 62 75 74 20   no changes but 
2fc13 73 74 69 6c 6c 20 72 65 70 6f 72 74 20 74 68 65  still report the
2fc14 20 6f 6c 64 20 76 61 6c 75 65 20 69 66 20 74 68   old value if th
2fc15 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 69  e.** new limit i
2fc16 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a  s negative..**.*
2fc17 2a 20 41 20 6e 65 77 20 6c 6f 77 65 72 20 6c 69  * A new lower li
2fc18 6d 69 74 20 64 6f 65 73 20 6e 6f 74 20 73 68 72  mit does not shr
2fc19 69 6e 6b 20 65 78 69 73 74 69 6e 67 20 63 6f 6e  ink existing con
2fc1a 73 74 72 75 63 74 73 2e 0a 2a 2a 20 49 74 20 6d  structs..** It m
2fc1b 65 72 65 6c 79 20 70 72 65 76 65 6e 74 73 20 6e  erely prevents n
2fc1c 65 77 20 63 6f 6e 73 74 72 75 63 74 73 20 74 68  ew constructs th
2fc1d 61 74 20 65 78 63 65 65 64 20 74 68 65 20 6c 69  at exceed the li
2fc1e 6d 69 74 0a 2a 2a 20 66 72 6f 6d 20 66 6f 72 6d  mit.** from form
2fc1f 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ing..*/.SQLITE_A
2fc20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
2fc21 69 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  imit(sqlite3 *db
2fc22 2c 20 69 6e 74 20 6c 69 6d 69 74 49 64 2c 20 69  , int limitId, i
2fc23 6e 74 20 6e 65 77 4c 69 6d 69 74 29 7b 0a 20 20  nt newLimit){.  
2fc24 69 6e 74 20 6f 6c 64 4c 69 6d 69 74 3b 0a 20 20  int oldLimit;.  
2fc25 69 66 28 20 6c 69 6d 69 74 49 64 3c 30 20 7c 7c  if( limitId<0 ||
2fc26 20 6c 69 6d 69 74 49 64 3e 3d 53 51 4c 49 54 45   limitId>=SQLITE
2fc27 5f 4e 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20  _N_LIMIT ){.    
2fc28 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20  return -1;.  }. 
2fc29 20 6f 6c 64 4c 69 6d 69 74 20 3d 20 64 62 2d 3e   oldLimit = db->
2fc2a 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 3b  aLimit[limitId];
2fc2b 0a 20 20 69 66 28 20 6e 65 77 4c 69 6d 69 74 3e  .  if( newLimit>
2fc2c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65  =0 ){.    if( ne
2fc2d 77 4c 69 6d 69 74 3e 61 48 61 72 64 4c 69 6d 69  wLimit>aHardLimi
2fc2e 74 5b 6c 69 6d 69 74 49 64 5d 20 29 7b 0a 20 20  t[limitId] ){.  
2fc2f 20 20 20 20 6e 65 77 4c 69 6d 69 74 20 3d 20 61      newLimit = a
2fc30 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69 74 49  HardLimit[limitI
2fc31 64 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  d];.    }.    db
2fc32 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64  ->aLimit[limitId
2fc33 5d 20 3d 20 6e 65 77 4c 69 6d 69 74 3b 0a 20 20  ] = newLimit;.  
2fc34 7d 0a 20 20 72 65 74 75 72 6e 20 6f 6c 64 4c 69  }.  return oldLi
2fc35 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  mit;.}../*.** Th
2fc36 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  is routine does 
2fc37 74 68 65 20 77 6f 72 6b 20 6f 66 20 6f 70 65 6e  the work of open
2fc38 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 20 6f  ing a database o
2fc39 6e 20 62 65 68 61 6c 66 20 6f 66 0a 2a 2a 20 73  n behalf of.** s
2fc3a 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e  qlite3_open() an
2fc3b 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  d sqlite3_open16
2fc3c 28 29 2e 20 54 68 65 20 64 61 74 61 62 61 73 65  (). The database
2fc3d 20 66 69 6c 65 6e 61 6d 65 20 22 7a 46 69 6c 65   filename "zFile
2fc3e 6e 61 6d 65 22 20 20 0a 2a 2a 20 69 73 20 55 54  name"  .** is UT
2fc3f 46 2d 38 20 65 6e 63 6f 64 65 64 2e 0a 2a 2f 0a  F-8 encoded..*/.
2fc40 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44  static int openD
2fc41 61 74 61 62 61 73 65 28 0a 20 20 63 6f 6e 73 74  atabase(.  const
2fc42 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
2fc43 2c 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69  , /* Database fi
2fc44 6c 65 6e 61 6d 65 20 55 54 46 2d 38 20 65 6e 63  lename UTF-8 enc
2fc45 6f 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  oded */.  sqlite
2fc46 33 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20  3 **ppDb,       
2fc47 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 65   /* OUT: Returne
2fc48 64 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  d database handl
2fc49 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
2fc4a 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 2f 2a  flags,        /*
2fc4b 20 4f 70 65 72 61 74 69 6f 6e 61 6c 20 66 6c 61   Operational fla
2fc4c 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  gs */.  const ch
2fc4d 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20 2f  ar *zVfs       /
2fc4e 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 56 46  * Name of the VF
2fc4f 53 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a 20  S to use */.){. 
2fc50 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
2fc51 69 6e 74 20 72 63 3b 0a 20 20 43 6f 6c 6c 53 65  int rc;.  CollSe
2fc52 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20  q *pColl;.  int 
2fc53 69 73 54 68 72 65 61 64 73 61 66 65 3b 0a 0a 23  isThreadsafe;..#
2fc54 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2fc55 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63  IT_AUTOINIT.  rc
2fc56 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69   = sqlite3_initi
2fc57 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72  alize();.  if( r
2fc58 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23  c ) return rc;.#
2fc59 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c  endif..  if( sql
2fc5a 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2fc5b 2e 62 43 6f 72 65 4d 75 74 65 78 3d 3d 30 20 29  .bCoreMutex==0 )
2fc5c 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64 73 61  {.    isThreadsa
2fc5d 66 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  fe = 0;.  }else 
2fc5e 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
2fc5f 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20  TE_OPEN_NOMUTEX 
2fc60 29 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64 73  ){.    isThreads
2fc61 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  afe = 0;.  }else
2fc62 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
2fc63 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54  ITE_OPEN_FULLMUT
2fc64 45 58 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65  EX ){.    isThre
2fc65 61 64 73 61 66 65 20 3d 20 31 3b 0a 20 20 7d 65  adsafe = 1;.  }e
2fc66 6c 73 65 7b 0a 20 20 20 20 69 73 54 68 72 65 61  lse{.    isThrea
2fc67 64 73 61 66 65 20 3d 20 73 71 6c 69 74 65 33 47  dsafe = sqlite3G
2fc68 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c  lobalConfig.bFul
2fc69 6c 4d 75 74 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f  lMutex;.  }..  /
2fc6a 2a 20 52 65 6d 6f 76 65 20 68 61 72 6d 66 75 6c  * Remove harmful
2fc6b 20 62 69 74 73 20 66 72 6f 6d 20 74 68 65 20 66   bits from the f
2fc6c 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 2a  lags parameter *
2fc6d 2f 0a 20 20 66 6c 61 67 73 20 26 3d 20 20 7e 28  /.  flags &=  ~(
2fc6e 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
2fc6f 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20  ETEONCLOSE |.   
2fc70 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
2fc71 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
2fc72 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
2fc73 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d   SQLITE_OPEN_TEM
2fc74 50 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20 20  P_DB | .        
2fc75 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
2fc76 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20  EN_TRANSIENT_DB 
2fc77 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  | .             
2fc78 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41    SQLITE_OPEN_MA
2fc79 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20  IN_JOURNAL | .  
2fc7a 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
2fc7b 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f  ITE_OPEN_TEMP_JO
2fc7c 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20  URNAL | .       
2fc7d 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
2fc7e 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 7c  PEN_SUBJOURNAL |
2fc7f 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2fc80 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53   SQLITE_OPEN_MAS
2fc81 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 0a 20 20  TER_JOURNAL |.  
2fc82 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
2fc83 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
2fc84 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   |.             
2fc85 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55    SQLITE_OPEN_FU
2fc86 4c 4c 4d 55 54 45 58 0a 20 20 20 20 20 20 20 20  LLMUTEX.        
2fc87 20 20 20 20 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c       );..  /* Al
2fc88 6c 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74  locate the sqlit
2fc89 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  e data structure
2fc8a 20 2a 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74   */.  db = sqlit
2fc8b 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69  e3MallocZero( si
2fc8c 7a 65 6f 66 28 73 71 6c 69 74 65 33 29 20 29 3b  zeof(sqlite3) );
2fc8d 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 29 20 67  .  if( db==0 ) g
2fc8e 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a  oto opendb_out;.
2fc8f 20 20 69 66 28 20 69 73 54 68 72 65 61 64 73 61    if( isThreadsa
2fc90 66 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 75  fe ){.    db->mu
2fc91 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
2fc92 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
2fc93 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b  UTEX_RECURSIVE);
2fc94 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 75 74  .    if( db->mut
2fc95 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ex==0 ){.      s
2fc96 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62 29 3b  qlite3_free(db);
2fc97 0a 20 20 20 20 20 20 64 62 20 3d 20 30 3b 0a 20  .      db = 0;. 
2fc98 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62       goto opendb
2fc99 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  _out;.    }.  }.
2fc9a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2fc9b 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2fc9c 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20  ;.  db->errMask 
2fc9d 3d 20 30 78 66 66 3b 0a 20 20 64 62 2d 3e 70 72  = 0xff;.  db->pr
2fc9e 69 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 30 3b  iorNewRowid = 0;
2fc9f 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 32 3b 0a  .  db->nDb = 2;.
2fca0 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
2fca1 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b  LITE_MAGIC_BUSY;
2fca2 0a 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d  .  db->aDb = db-
2fca3 3e 61 44 62 53 74 61 74 69 63 3b 0a 0a 20 20 61  >aDbStatic;..  a
2fca4 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 64 62  ssert( sizeof(db
2fca5 2d 3e 61 4c 69 6d 69 74 29 3d 3d 73 69 7a 65 6f  ->aLimit)==sizeo
2fca6 66 28 61 48 61 72 64 4c 69 6d 69 74 29 20 29 3b  f(aHardLimit) );
2fca7 0a 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 4c  .  memcpy(db->aL
2fca8 69 6d 69 74 2c 20 61 48 61 72 64 4c 69 6d 69 74  imit, aHardLimit
2fca9 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c 69  , sizeof(db->aLi
2fcaa 6d 69 74 29 29 3b 0a 20 20 64 62 2d 3e 61 75 74  mit));.  db->aut
2fcab 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 64  oCommit = 1;.  d
2fcac 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 20 3d  b->nextAutovac =
2fcad 20 2d 31 3b 0a 20 20 64 62 2d 3e 6e 65 78 74 50   -1;.  db->nextP
2fcae 61 67 65 73 69 7a 65 20 3d 20 30 3b 0a 20 20 64  agesize = 0;.  d
2fcaf 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
2fcb0 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
2fcb1 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41  .#if SQLITE_DEFA
2fcb2 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3c  ULT_FILE_FORMAT<
2fcb3 34 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  4.              
2fcb4 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c 65 67 61     | SQLITE_Lega
2fcb5 63 79 46 69 6c 65 46 6d 74 0a 23 65 6e 64 69 66  cyFileFmt.#endif
2fcb6 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
2fcb7 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45 58 54 45 4e  NABLE_LOAD_EXTEN
2fcb8 53 49 4f 4e 0a 20 20 20 20 20 20 20 20 20 20 20  SION.           
2fcb9 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c        | SQLITE_L
2fcba 6f 61 64 45 78 74 65 6e 73 69 6f 6e 0a 23 65 6e  oadExtension.#en
2fcbb 64 69 66 0a 20 20 20 20 20 20 3b 0a 20 20 73 71  dif.      ;.  sq
2fcbc 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 64  lite3HashInit(&d
2fcbd 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 30 29 3b  b->aCollSeq, 0);
2fcbe 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2fcbf 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2fcc0 45 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 49  E.  sqlite3HashI
2fcc1 6e 69 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  nit(&db->aModule
2fcc2 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  , 0);.#endif..  
2fcc3 64 62 2d 3e 70 56 66 73 20 3d 20 73 71 6c 69 74  db->pVfs = sqlit
2fcc4 65 33 5f 76 66 73 5f 66 69 6e 64 28 7a 56 66 73  e3_vfs_find(zVfs
2fcc5 29 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e 70 56  );.  if( !db->pV
2fcc6 66 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  fs ){.    rc = S
2fcc7 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2fcc8 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
2fcc9 2c 20 72 63 2c 20 22 6e 6f 20 73 75 63 68 20 76  , rc, "no such v
2fcca 66 73 3a 20 25 73 22 2c 20 7a 56 66 73 29 3b 0a  fs: %s", zVfs);.
2fccb 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
2fccc 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  out;.  }..  /* A
2fccd 64 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  dd the default c
2fcce 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2fccf 65 20 42 49 4e 41 52 59 2e 20 42 49 4e 41 52 59  e BINARY. BINARY
2fcd0 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20   works for both 
2fcd1 55 54 46 2d 38 0a 20 20 2a 2a 20 61 6e 64 20 55  UTF-8.  ** and U
2fcd2 54 46 2d 31 36 2c 20 73 6f 20 61 64 64 20 61 20  TF-16, so add a 
2fcd3 76 65 72 73 69 6f 6e 20 66 6f 72 20 65 61 63 68  version for each
2fcd4 20 74 6f 20 61 76 6f 69 64 20 61 6e 79 20 75 6e   to avoid any un
2fcd5 6e 65 63 65 73 73 61 72 79 0a 20 20 2a 2a 20 63  necessary.  ** c
2fcd6 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 54 68 65 20  onversions. The 
2fcd7 6f 6e 6c 79 20 65 72 72 6f 72 20 74 68 61 74 20  only error that 
2fcd8 63 61 6e 20 6f 63 63 75 72 20 68 65 72 65 20 69  can occur here i
2fcd9 73 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  s a malloc() fai
2fcda 6c 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 63 72 65  lure..  */.  cre
2fcdb 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
2fcdc 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54   "BINARY", SQLIT
2fcdd 45 5f 55 54 46 38 2c 20 30 2c 20 62 69 6e 43 6f  E_UTF8, 0, binCo
2fcde 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72  llFunc, 0);.  cr
2fcdf 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
2fce0 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49  , "BINARY", SQLI
2fce1 54 45 5f 55 54 46 31 36 42 45 2c 20 30 2c 20 62  TE_UTF16BE, 0, b
2fce2 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a  inCollFunc, 0);.
2fce3 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f    createCollatio
2fce4 6e 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20  n(db, "BINARY", 
2fce5 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20  SQLITE_UTF16LE, 
2fce6 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20  0, binCollFunc, 
2fce7 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c  0);.  createColl
2fce8 61 74 69 6f 6e 28 64 62 2c 20 22 52 54 52 49 4d  ation(db, "RTRIM
2fce9 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  ", SQLITE_UTF8, 
2fcea 28 76 6f 69 64 2a 29 31 2c 20 62 69 6e 43 6f 6c  (void*)1, binCol
2fceb 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 69 66 28  lFunc, 0);.  if(
2fcec 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2fced 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70  d ){.    goto op
2fcee 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20  endb_out;.  }.  
2fcef 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d 20  db->pDfltColl = 
2fcf0 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
2fcf1 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54  eq(db, SQLITE_UT
2fcf2 46 38 2c 20 22 42 49 4e 41 52 59 22 2c 20 36 2c  F8, "BINARY", 6,
2fcf3 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64   0);.  assert( d
2fcf4 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 21 3d 30 20  b->pDfltColl!=0 
2fcf5 29 3b 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 61 64  );..  /* Also ad
2fcf6 64 20 61 20 55 54 46 2d 38 20 63 61 73 65 2d 69  d a UTF-8 case-i
2fcf7 6e 73 65 6e 73 69 74 69 76 65 20 63 6f 6c 6c 61  nsensitive colla
2fcf8 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20 2a  tion sequence. *
2fcf9 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  /.  createCollat
2fcfa 69 6f 6e 28 64 62 2c 20 22 4e 4f 43 41 53 45 22  ion(db, "NOCASE"
2fcfb 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30  , SQLITE_UTF8, 0
2fcfc 2c 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e  , nocaseCollatin
2fcfd 67 46 75 6e 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a  gFunc, 0);..  /*
2fcfe 20 53 65 74 20 66 6c 61 67 73 20 6f 6e 20 74 68   Set flags on th
2fcff 65 20 62 75 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61  e built-in colla
2fd00 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 2a  ting sequences *
2fd01 2f 0a 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  /.  db->pDfltCol
2fd02 6c 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  l->type = SQLITE
2fd03 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 3b 0a 20 20  _COLL_BINARY;.  
2fd04 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  pColl = sqlite3F
2fd05 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53  indCollSeq(db, S
2fd06 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 4e 4f 43  QLITE_UTF8, "NOC
2fd07 41 53 45 22 2c 20 36 2c 20 30 29 3b 0a 20 20 69  ASE", 6, 0);.  i
2fd08 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
2fd09 70 43 6f 6c 6c 2d 3e 74 79 70 65 20 3d 20 53 51  pColl->type = SQ
2fd0a 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45  LITE_COLL_NOCASE
2fd0b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e  ;.  }..  /* Open
2fd0c 20 74 68 65 20 62 61 63 6b 65 6e 64 20 64 61 74   the backend dat
2fd0d 61 62 61 73 65 20 64 72 69 76 65 72 20 2a 2f 0a  abase driver */.
2fd0e 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20    db->openFlags 
2fd0f 3d 20 66 6c 61 67 73 3b 0a 20 20 72 63 20 3d 20  = flags;.  rc = 
2fd10 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74  sqlite3BtreeFact
2fd11 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65 6e 61 6d  ory(db, zFilenam
2fd12 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46  e, 0, SQLITE_DEF
2fd13 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c  AULT_CACHE_SIZE,
2fd14 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2fd15 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6c 61               fla
2fd16 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs | SQLITE_OPEN
2fd17 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20  _MAIN_DB,.      
2fd18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fd19 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 30 5d       &db->aDb[0]
2fd1a 2e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 21  .pBt);.  if( rc!
2fd1b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2fd1c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2fd1d 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
2fd1e 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
2fd1f 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
2fd20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
2fd21 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20  db, rc, 0);.    
2fd22 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b  goto opendb_out;
2fd23 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 5b 30  .  }.  db->aDb[0
2fd24 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69  ].pSchema = sqli
2fd25 74 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c  te3SchemaGet(db,
2fd26 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29   db->aDb[0].pBt)
2fd27 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ;.  db->aDb[1].p
2fd28 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33  Schema = sqlite3
2fd29 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 30 29  SchemaGet(db, 0)
2fd2a 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 64 65 66  ;...  /* The def
2fd2b 61 75 6c 74 20 73 61 66 65 74 79 5f 6c 65 76 65  ault safety_leve
2fd2c 6c 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  l for the main d
2fd2d 61 74 61 62 61 73 65 20 69 73 20 27 66 75 6c 6c  atabase is 'full
2fd2e 27 3b 20 66 6f 72 20 74 68 65 20 74 65 6d 70 0a  '; for the temp.
2fd2f 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 74    ** database it
2fd30 20 69 73 20 27 4e 4f 4e 45 27 2e 20 54 68 69 73   is 'NONE'. This
2fd31 20 6d 61 74 63 68 65 73 20 74 68 65 20 70 61 67   matches the pag
2fd32 65 72 20 6c 61 79 65 72 20 64 65 66 61 75 6c 74  er layer default
2fd33 73 2e 20 20 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e  s.  .  */.  db->
2fd34 61 44 62 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 22  aDb[0].zName = "
2fd35 6d 61 69 6e 22 3b 0a 20 20 64 62 2d 3e 61 44 62  main";.  db->aDb
2fd36 5b 30 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c  [0].safety_level
2fd37 20 3d 20 33 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 3;.#ifndef SQ
2fd38 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42  LITE_OMIT_TEMPDB
2fd39 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e  .  db->aDb[1].zN
2fd3a 61 6d 65 20 3d 20 22 74 65 6d 70 22 3b 0a 20 20  ame = "temp";.  
2fd3b 64 62 2d 3e 61 44 62 5b 31 5d 2e 73 61 66 65 74  db->aDb[1].safet
2fd3c 79 5f 6c 65 76 65 6c 20 3d 20 31 3b 0a 23 65 6e  y_level = 1;.#en
2fd3d 64 69 66 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63  dif..  db->magic
2fd3e 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
2fd3f 4f 50 45 4e 3b 0a 20 20 69 66 28 20 64 62 2d 3e  OPEN;.  if( db->
2fd40 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2fd41 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
2fd42 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  out;.  }..  /* R
2fd43 65 67 69 73 74 65 72 20 61 6c 6c 20 62 75 69 6c  egister all buil
2fd44 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2c 20  t-in functions, 
2fd45 62 75 74 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d  but do not attem
2fd46 70 74 20 74 6f 20 72 65 61 64 20 74 68 65 0a 20  pt to read the. 
2fd47 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68   ** database sch
2fd48 65 6d 61 20 79 65 74 2e 20 54 68 69 73 20 69 73  ema yet. This is
2fd49 20 64 65 6c 61 79 65 64 20 75 6e 74 69 6c 20 74   delayed until t
2fd4a 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
2fd4b 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  e database.  ** 
2fd4c 69 73 20 61 63 63 65 73 73 65 64 2e 0a 20 20 2a  is accessed..  *
2fd4d 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  /.  sqlite3Error
2fd4e 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  (db, SQLITE_OK, 
2fd4f 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 67  0);.  sqlite3Reg
2fd50 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63  isterBuiltinFunc
2fd51 74 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 2f 2a  tions(db);..  /*
2fd52 20 4c 6f 61 64 20 61 75 74 6f 6d 61 74 69 63 20   Load automatic 
2fd53 65 78 74 65 6e 73 69 6f 6e 73 20 2d 20 65 78 74  extensions - ext
2fd54 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 68 61 76  ensions that hav
2fd55 65 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  e been registere
2fd56 64 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65  d.  ** using the
2fd57 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 6d 61 74   sqlite3_automat
2fd58 69 63 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 41  ic_extension() A
2fd59 50 49 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64  PI..  */.  (void
2fd5a 29 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64  )sqlite3AutoLoad
2fd5b 45 78 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a  Extensions(db);.
2fd5c 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 65 72    if( sqlite3_er
2fd5d 72 63 6f 64 65 28 64 62 29 21 3d 53 51 4c 49 54  rcode(db)!=SQLIT
2fd5e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
2fd5f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d   opendb_out;.  }
2fd60 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2fd61 45 4e 41 42 4c 45 5f 46 54 53 31 0a 20 20 69 66  ENABLE_FTS1.  if
2fd62 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
2fd63 6c 65 64 20 29 7b 0a 20 20 20 20 65 78 74 65 72  led ){.    exter
2fd64 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  n int sqlite3Fts
2fd65 31 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 29 3b  1Init(sqlite3*);
2fd66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2fd67 33 46 74 73 31 49 6e 69 74 28 64 62 29 3b 0a 20  3Fts1Init(db);. 
2fd68 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65   }.#endif..#ifde
2fd69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
2fd6a 46 54 53 32 0a 20 20 69 66 28 20 21 64 62 2d 3e  FTS2.  if( !db->
2fd6b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20  mallocFailed && 
2fd6c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2fd6d 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20  .    extern int 
2fd6e 73 71 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28  sqlite3Fts2Init(
2fd6f 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72  sqlite3*);.    r
2fd70 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 32 49  c = sqlite3Fts2I
2fd71 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e  nit(db);.  }.#en
2fd72 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
2fd73 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 0a 20  TE_ENABLE_FTS3. 
2fd74 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
2fd75 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51  Failed && rc==SQ
2fd76 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
2fd77 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49  c = sqlite3Fts3I
2fd78 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e  nit(db);.  }.#en
2fd79 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
2fd7a 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 20 20  TE_ENABLE_ICU.  
2fd7b 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
2fd7c 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c  ailed && rc==SQL
2fd7d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
2fd7e 20 3d 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69   = sqlite3IcuIni
2fd7f 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
2fd80 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
2fd81 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a 20 20  _ENABLE_RTREE.  
2fd82 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
2fd83 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c  ailed && rc==SQL
2fd84 49 54 45 5f 4f 4b 29 7b 0a 20 20 20 20 72 63 20  ITE_OK){.    rc 
2fd85 3d 20 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e  = sqlite3RtreeIn
2fd86 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64  it(db);.  }.#end
2fd87 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 45 72 72  if..  sqlite3Err
2fd88 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 0a  or(db, rc, 0);..
2fd89 20 20 2f 2a 20 2d 44 53 51 4c 49 54 45 5f 44 45    /* -DSQLITE_DE
2fd8a 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f  FAULT_LOCKING_MO
2fd8b 44 45 3d 31 20 6d 61 6b 65 73 20 45 58 43 4c 55  DE=1 makes EXCLU
2fd8c 53 49 56 45 20 74 68 65 20 64 65 66 61 75 6c 74  SIVE the default
2fd8d 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f   locking.  ** mo
2fd8e 64 65 2e 20 20 2d 44 53 51 4c 49 54 45 5f 44 45  de.  -DSQLITE_DE
2fd8f 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f  FAULT_LOCKING_MO
2fd90 44 45 3d 30 20 6d 61 6b 65 20 4e 4f 52 4d 41 4c  DE=0 make NORMAL
2fd91 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63   the default loc
2fd92 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20  king.  ** mode. 
2fd93 20 44 6f 69 6e 67 20 6e 6f 74 68 69 6e 67 20 61   Doing nothing a
2fd94 74 20 61 6c 6c 20 61 6c 73 6f 20 6d 61 6b 65 73  t all also makes
2fd95 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65 66 61   NORMAL the defa
2fd96 75 6c 74 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66  ult..  */.#ifdef
2fd97 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
2fd98 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 0a 20 20 64  LOCKING_MODE.  d
2fd99 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20  b->dfltLockMode 
2fd9a 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  = SQLITE_DEFAULT
2fd9b 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3b 0a 20  _LOCKING_MODE;. 
2fd9c 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
2fd9d 6b 69 6e 67 4d 6f 64 65 28 73 71 6c 69 74 65 33  kingMode(sqlite3
2fd9e 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61  BtreePager(db->a
2fd9f 44 62 5b 30 5d 2e 70 42 74 29 2c 0a 20 20 20 20  Db[0].pBt),.    
2fda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fda1 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46        SQLITE_DEF
2fda2 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44  AULT_LOCKING_MOD
2fda3 45 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  E);.#endif..  /*
2fda4 20 45 6e 61 62 6c 65 20 74 68 65 20 6c 6f 6f 6b   Enable the look
2fda5 61 73 69 64 65 2d 6d 61 6c 6c 6f 63 20 73 75 62  aside-malloc sub
2fda6 73 79 73 74 65 6d 20 2a 2f 0a 20 20 73 65 74 75  system */.  setu
2fda7 70 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 30  pLookaside(db, 0
2fda8 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  , sqlite3GlobalC
2fda9 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64  onfig.szLookasid
2fdaa 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
2fdab 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2fdac 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
2fdad 4c 6f 6f 6b 61 73 69 64 65 29 3b 0a 0a 6f 70 65  Lookaside);..ope
2fdae 6e 64 62 5f 6f 75 74 3a 0a 20 20 69 66 28 20 64  ndb_out:.  if( d
2fdaf 62 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  b ){.    assert(
2fdb0 20 64 62 2d 3e 6d 75 74 65 78 21 3d 30 20 7c 7c   db->mutex!=0 ||
2fdb1 20 69 73 54 68 72 65 61 64 73 61 66 65 3d 3d 30   isThreadsafe==0
2fdb2 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   || sqlite3Globa
2fdb3 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74  lConfig.bFullMut
2fdb4 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  ex==0 );.    sql
2fdb5 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2fdb6 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  (db->mutex);.  }
2fdb7 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2fdb8 65 72 72 63 6f 64 65 28 64 62 29 3b 0a 20 20 69  errcode(db);.  i
2fdb9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
2fdba 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  MEM ){.    sqlit
2fdbb 65 33 5f 63 6c 6f 73 65 28 64 62 29 3b 0a 20 20  e3_close(db);.  
2fdbc 20 20 64 62 20 3d 20 30 3b 0a 20 20 7d 65 6c 73    db = 0;.  }els
2fdbd 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45  e if( rc!=SQLITE
2fdbe 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  _OK ){.    db->m
2fdbf 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
2fdc0 47 49 43 5f 53 49 43 4b 3b 0a 20 20 7d 0a 20 20  GIC_SICK;.  }.  
2fdc1 2a 70 70 44 62 20 3d 20 64 62 3b 0a 20 20 72 65  *ppDb = db;.  re
2fdc2 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45  turn sqlite3ApiE
2fdc3 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 0a 2f  xit(0, rc);.}../
2fdc4 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20  *.** Open a new 
2fdc5 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  database handle.
2fdc6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2fdc7 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  nt sqlite3_open(
2fdc8 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2fdc9 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c  Filename, .  sql
2fdca 69 74 65 33 20 2a 2a 70 70 44 62 20 0a 29 7b 0a  ite3 **ppDb .){.
2fdcb 20 20 72 65 74 75 72 6e 20 6f 70 65 6e 44 61 74    return openDat
2fdcc 61 62 61 73 65 28 7a 46 69 6c 65 6e 61 6d 65 2c  abase(zFilename,
2fdcd 20 70 70 44 62 2c 0a 20 20 20 20 20 20 20 20 20   ppDb,.         
2fdce 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
2fdcf 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
2fdd0 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  TE | SQLITE_OPEN
2fdd1 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a 7d 0a 53  _CREATE, 0);.}.S
2fdd2 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2fdd3 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20  lite3_open_v2(. 
2fdd4 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
2fdd5 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61  ename,   /* Data
2fdd6 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55  base filename (U
2fdd7 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74  TF-8) */.  sqlit
2fdd8 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20  e3 **ppDb,      
2fdd9 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74     /* OUT: SQLit
2fdda 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  e db handle */. 
2fddb 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20   int flags,     
2fddc 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
2fddd 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
2fdde 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f  r *zVfs        /
2fddf 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f  * Name of VFS mo
2fde0 64 75 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29  dule to use */.)
2fde1 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70 65 6e 44  {.  return openD
2fde2 61 74 61 62 61 73 65 28 66 69 6c 65 6e 61 6d 65  atabase(filename
2fde3 2c 20 70 70 44 62 2c 20 66 6c 61 67 73 2c 20 7a  , ppDb, flags, z
2fde4 56 66 73 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  Vfs);.}..#ifndef
2fde5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
2fde6 31 36 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  16./*.** Open a 
2fde7 6e 65 77 20 64 61 74 61 62 61 73 65 20 68 61 6e  new database han
2fde8 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
2fde9 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
2fdea 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 76  pen16(.  const v
2fdeb 6f 69 64 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  oid *zFilename, 
2fdec 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44  .  sqlite3 **ppD
2fded 62 0a 29 7b 0a 20 20 63 68 61 72 20 63 6f 6e 73  b.){.  char cons
2fdee 74 20 2a 7a 46 69 6c 65 6e 61 6d 65 38 3b 20 20  t *zFilename8;  
2fdef 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 6e   /* zFilename en
2fdf0 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 69  coded in UTF-8 i
2fdf1 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d 31 36  nstead of UTF-16
2fdf2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61   */.  sqlite3_va
2fdf3 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74  lue *pVal;.  int
2fdf4 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
2fdf5 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 61  zFilename );.  a
2fdf6 73 73 65 72 74 28 20 70 70 44 62 20 29 3b 0a 20  ssert( ppDb );. 
2fdf7 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23 69 66 6e   *ppDb = 0;.#ifn
2fdf8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2fdf9 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20  AUTOINIT.  rc = 
2fdfa 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
2fdfb 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ze();.  if( rc )
2fdfc 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64   return rc;.#end
2fdfd 69 66 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c 69  if.  pVal = sqli
2fdfe 74 65 33 56 61 6c 75 65 4e 65 77 28 30 29 3b 0a  te3ValueNew(0);.
2fdff 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
2fe00 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a  tStr(pVal, -1, z
2fe01 46 69 6c 65 6e 61 6d 65 2c 20 53 51 4c 49 54 45  Filename, SQLITE
2fe02 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51  _UTF16NATIVE, SQ
2fe03 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
2fe04 7a 46 69 6c 65 6e 61 6d 65 38 20 3d 20 73 71 6c  zFilename8 = sql
2fe05 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56  ite3ValueText(pV
2fe06 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  al, SQLITE_UTF8)
2fe07 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d  ;.  if( zFilenam
2fe08 65 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f  e8 ){.    rc = o
2fe09 70 65 6e 44 61 74 61 62 61 73 65 28 7a 46 69 6c  penDatabase(zFil
2fe0a 65 6e 61 6d 65 38 2c 20 70 70 44 62 2c 0a 20 20  ename8, ppDb,.  
2fe0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe0c 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
2fe0d 52 45 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49  READWRITE | SQLI
2fe0e 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c 20  TE_OPEN_CREATE, 
2fe0f 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
2fe10 2a 70 70 44 62 20 7c 7c 20 72 63 3d 3d 53 51 4c  *ppDb || rc==SQL
2fe11 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
2fe12 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2fe13 4f 4b 20 26 26 20 21 44 62 48 61 73 50 72 6f 70  OK && !DbHasProp
2fe14 65 72 74 79 28 2a 70 70 44 62 2c 20 30 2c 20 44  erty(*ppDb, 0, D
2fe15 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20  B_SchemaLoaded) 
2fe16 29 7b 0a 20 20 20 20 20 20 45 4e 43 28 2a 70 70  ){.      ENC(*pp
2fe17 44 62 29 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  Db) = SQLITE_UTF
2fe18 31 36 4e 41 54 49 56 45 3b 0a 20 20 20 20 7d 0a  16NATIVE;.    }.
2fe19 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
2fe1a 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
2fe1b 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 61 6c    }.  sqlite3Val
2fe1c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 0a 20  ueFree(pVal);.. 
2fe1d 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
2fe1e 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d  piExit(0, rc);.}
2fe1f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2fe20 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
2fe21 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
2fe22 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20  a new collation 
2fe23 73 65 71 75 65 6e 63 65 20 77 69 74 68 20 74 68  sequence with th
2fe24 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
2fe25 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  e db..*/.SQLITE_
2fe26 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2fe27 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2fe28 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c  (.  sqlite3* db,
2fe29 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
2fe2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e  zName, .  int en
2fe2b 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78  c, .  void* pCtx
2fe2c 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72  ,.  int(*xCompar
2fe2d 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  e)(void*,int,con
2fe2e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
2fe2f 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69  st void*).){.  i
2fe30 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
2fe31 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
2fe32 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72  >mutex);.  asser
2fe33 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
2fe34 69 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 63  iled );.  rc = c
2fe35 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
2fe36 62 2c 20 7a 4e 61 6d 65 2c 20 65 6e 63 2c 20 70  b, zName, enc, p
2fe37 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30  Ctx, xCompare, 0
2fe38 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
2fe39 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
2fe3a 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2fe3b 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2fe3c 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
2fe3d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  .}../*.** Regist
2fe3e 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69  er a new collati
2fe3f 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68  on sequence with
2fe40 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
2fe41 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49  ndle db..*/.SQLI
2fe42 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2fe43 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2fe44 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  ion_v2(.  sqlite
2fe45 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20  3* db, .  const 
2fe46 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
2fe47 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64  int enc, .  void
2fe48 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78  * pCtx,.  int(*x
2fe49 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69  Compare)(void*,i
2fe4a 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
2fe4b 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c  nt,const void*),
2fe4c 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29 28 76  .  void(*xDel)(v
2fe4d 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  oid*).){.  int r
2fe4e 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
2fe4f 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2fe50 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ex);.  assert( !
2fe51 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2fe52 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74   );.  rc = creat
2fe53 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a  eCollation(db, z
2fe54 4e 61 6d 65 2c 20 65 6e 63 2c 20 70 43 74 78 2c  Name, enc, pCtx,
2fe55 20 78 43 6f 6d 70 61 72 65 2c 20 78 44 65 6c 29   xCompare, xDel)
2fe56 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
2fe57 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
2fe58 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2fe59 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2fe5a 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2fe5b 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2fe5c 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
2fe5d 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65  ** Register a ne
2fe5e 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  w collation sequ
2fe5f 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61  ence with the da
2fe60 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
2fe61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2fe62 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
2fe63 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a  te_collation16(.
2fe64 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a    sqlite3* db, .
2fe65 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e    const void *zN
2fe66 61 6d 65 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 20  ame,.  int enc, 
2fe67 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20  .  void* pCtx,. 
2fe68 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28   int(*xCompare)(
2fe69 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
2fe6a 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
2fe6b 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20  void*).){.  int 
2fe6c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
2fe6d 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 38 3b 0a    char *zName8;.
2fe6e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2fe6f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2fe70 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
2fe71 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
2fe72 0a 20 20 7a 4e 61 6d 65 38 20 3d 20 73 71 6c 69  .  zName8 = sqli
2fe73 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20  te3Utf16to8(db, 
2fe74 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69 66  zName, -1);.  if
2fe75 28 20 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20 20  ( zName8 ){.    
2fe76 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61  rc = createColla
2fe77 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38 2c  tion(db, zName8,
2fe78 20 65 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f 6d   enc, pCtx, xCom
2fe79 70 61 72 65 2c 20 30 29 3b 0a 20 20 20 20 73 71  pare, 0);.    sq
2fe7a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2fe7b 7a 4e 61 6d 65 38 29 3b 0a 20 20 7d 0a 20 20 72  zName8);.  }.  r
2fe7c 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
2fe7d 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
2fe7e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2fe7f 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2fe80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
2fe81 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2fe82 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a  IT_UTF16 */../*.
2fe83 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 6f  ** Register a co
2fe84 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2fe85 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63   factory callbac
2fe86 6b 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  k with the datab
2fe87 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62  ase handle.** db
2fe88 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72  . Replace any pr
2fe89 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c  eviously install
2fe8a 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
2fe8b 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a 2a  uence factory..*
2fe8c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2fe8d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
2fe8e 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c  on_needed(.  sql
2fe8f 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f 69  ite3 *db, .  voi
2fe90 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  d *pCollNeededAr
2fe91 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c  g, .  void(*xCol
2fe92 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a 2c 73  lNeeded)(void*,s
2fe93 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
2fe94 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a  tRep,const char*
2fe95 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ).){.  sqlite3_m
2fe96 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
2fe97 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f  utex);.  db->xCo
2fe98 6c 6c 4e 65 65 64 65 64 20 3d 20 78 43 6f 6c 6c  llNeeded = xColl
2fe99 4e 65 65 64 65 64 3b 0a 20 20 64 62 2d 3e 78 43  Needed;.  db->xC
2fe9a 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d 20 30 3b  ollNeeded16 = 0;
2fe9b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  .  db->pCollNeed
2fe9c 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65  edArg = pCollNee
2fe9d 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  dedArg;.  sqlite
2fe9e 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
2fe9f 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
2fea0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2fea1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2fea2 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
2fea3 20 52 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c   Register a coll
2fea4 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
2fea5 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20  actory callback 
2fea6 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
2fea7 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20  e handle.** db. 
2fea8 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76  Replace any prev
2fea9 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64  iously installed
2feaa 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2feab 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a  nce factory..*/.
2feac 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2fead 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2feae 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c  _needed16(.  sql
2feaf 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f 69  ite3 *db, .  voi
2feb0 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  d *pCollNeededAr
2feb1 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c  g, .  void(*xCol
2feb2 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64 2a  lNeeded16)(void*
2feb3 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
2feb4 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
2feb5 64 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33  d*).){.  sqlite3
2feb6 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
2feb7 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78  >mutex);.  db->x
2feb8 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 30 3b 0a  CollNeeded = 0;.
2feb9 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65    db->xCollNeede
2feba 64 31 36 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65  d16 = xCollNeede
2febb 64 31 36 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c  d16;.  db->pColl
2febc 4e 65 65 64 65 64 41 72 67 20 3d 20 70 43 6f 6c  NeededArg = pCol
2febd 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71  lNeededArg;.  sq
2febe 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2febf 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2fec0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2fec1 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
2fec2 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
2fec3 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
2fec4 54 45 5f 4f 4d 49 54 5f 47 4c 4f 42 41 4c 52 45  TE_OMIT_GLOBALRE
2fec5 43 4f 56 45 52 0a 23 69 66 6e 64 65 66 20 53 51  COVER.#ifndef SQ
2fec6 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
2fec7 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ATED./*.** This 
2fec8 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 77 20  function is now 
2fec9 61 6e 20 61 6e 61 63 68 72 6f 6e 69 73 6d 2e 20  an anachronism. 
2feca 49 74 20 75 73 65 64 20 74 6f 20 62 65 20 75 73  It used to be us
2fecb 65 64 20 74 6f 20 72 65 63 6f 76 65 72 20 66 72  ed to recover fr
2fecc 6f 6d 20 61 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29  om a.** malloc()
2fecd 20 66 61 69 6c 75 72 65 2c 20 62 75 74 20 53 51   failure, but SQ
2fece 4c 69 74 65 20 6e 6f 77 20 64 6f 65 73 20 74 68  Lite now does th
2fecf 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
2fed0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2fed1 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62  int sqlite3_glob
2fed2 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29  al_recover(void)
2fed3 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  {.  return SQLIT
2fed4 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 23  E_OK;.}.#endif.#
2fed5 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73  endif../*.** Tes
2fed6 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72  t to see whether
2fed7 20 6f 72 20 6e 6f 74 20 74 68 65 20 64 61 74 61   or not the data
2fed8 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2fed9 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74  is in autocommit
2feda 0a 2a 2a 20 6d 6f 64 65 2e 20 20 52 65 74 75 72  .** mode.  Retur
2fedb 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73 20  n TRUE if it is 
2fedc 61 6e 64 20 46 41 4c 53 45 20 69 66 20 6e 6f 74  and FALSE if not
2fedd 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  .  Autocommit mo
2fede 64 65 20 69 73 20 6f 6e 0a 2a 2a 20 62 79 20 64  de is on.** by d
2fedf 65 66 61 75 6c 74 2e 20 20 41 75 74 6f 63 6f 6d  efault.  Autocom
2fee0 6d 69 74 20 69 73 20 64 69 73 61 62 6c 65 64 20  mit is disabled 
2fee1 62 79 20 61 20 42 45 47 49 4e 20 73 74 61 74 65  by a BEGIN state
2fee2 6d 65 6e 74 20 61 6e 64 20 72 65 65 6e 61 62 6c  ment and reenabl
2fee3 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6e 65 78  ed.** by the nex
2fee4 74 20 43 4f 4d 4d 49 54 20 6f 72 20 52 4f 4c 4c  t COMMIT or ROLL
2fee5 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  BACK..**.*******
2fee6 20 54 48 49 53 20 49 53 20 41 4e 20 45 58 50 45   THIS IS AN EXPE
2fee7 52 49 4d 45 4e 54 41 4c 20 41 50 49 20 41 4e 44  RIMENTAL API AND
2fee8 20 49 53 20 53 55 42 4a 45 43 54 20 54 4f 20 43   IS SUBJECT TO C
2fee9 48 41 4e 47 45 20 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a  HANGE ******.*/.
2feea 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2feeb 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63  qlite3_get_autoc
2feec 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64  ommit(sqlite3 *d
2feed 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d  b){.  return db-
2feee 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 7d 0a 0a  >autoCommit;.}..
2feef 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
2fef0 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  BUG./*.** The fo
2fef1 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
2fef2 69 73 20 73 75 62 74 69 74 75 74 65 64 20 66 6f  is subtituted fo
2fef3 72 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54  r constant SQLIT
2fef4 45 5f 43 4f 52 52 55 50 54 20 69 6e 0a 2a 2a 20  E_CORRUPT in.** 
2fef5 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64 73  debugging builds
2fef6 2e 20 20 54 68 69 73 20 70 72 6f 76 69 64 65 73  .  This provides
2fef7 20 61 20 77 61 79 20 74 6f 20 73 65 74 20 61 20   a way to set a 
2fef8 62 72 65 61 6b 70 6f 69 6e 74 20 66 6f 72 20 77  breakpoint for w
2fef9 68 65 6e 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  hen.** corruptio
2fefa 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63  n is first detec
2fefb 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
2fefc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2fefd 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 7b  e3Corrupt(void){
2fefe 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2feff 5f 43 4f 52 52 55 50 54 3b 0a 7d 0a 23 65 6e 64  _CORRUPT;.}.#end
2ff00 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
2ff01 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
2ff02 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  ED./*.** This is
2ff03 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 72   a convenience r
2ff04 6f 75 74 69 6e 65 20 74 68 61 74 20 6d 61 6b 65  outine that make
2ff05 73 20 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20  s sure that all 
2ff06 74 68 72 65 61 64 2d 73 70 65 63 69 66 69 63 0a  thread-specific.
2ff07 2a 2a 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  ** data for this
2ff08 20 74 68 72 65 61 64 20 68 61 73 20 62 65 65 6e   thread has been
2ff09 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a   deallocated..**
2ff0a 0a 2a 2a 20 53 51 4c 69 74 65 20 6e 6f 20 6c 6f  .** SQLite no lo
2ff0b 6e 67 65 72 20 75 73 65 73 20 74 68 72 65 61 64  nger uses thread
2ff0c 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 20 73  -specific data s
2ff0d 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
2ff0e 73 20 6e 6f 77 20 61 0a 2a 2a 20 6e 6f 2d 6f 70  s now a.** no-op
2ff0f 2e 20 20 49 74 20 69 73 20 72 65 74 61 69 6e 65  .  It is retaine
2ff10 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c  d for historical
2ff11 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
2ff12 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
2ff13 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  id sqlite3_threa
2ff14 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 29 7b  d_cleanup(void){
2ff15 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
2ff16 20 52 65 74 75 72 6e 20 6d 65 74 61 20 69 6e 66   Return meta inf
2ff17 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
2ff18 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e   specific column
2ff19 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 74   of a database t
2ff1a 61 62 6c 65 2e 0a 2a 2a 20 53 65 65 20 63 6f 6d  able..** See com
2ff1b 6d 65 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 2e  ment in sqlite3.
2ff1c 68 20 28 73 71 6c 69 74 65 2e 68 2e 69 6e 29 20  h (sqlite.h.in) 
2ff1d 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
2ff1e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
2ff1f 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
2ff20 44 41 54 41 0a 53 51 4c 49 54 45 5f 41 50 49 20  DATA.SQLITE_API 
2ff21 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c  int sqlite3_tabl
2ff22 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74  e_column_metadat
2ff23 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  a(.  sqlite3 *db
2ff24 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2ff25 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68   /* Connection h
2ff26 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
2ff27 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20   char *zDbName, 
2ff28 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2ff29 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20  se name or NULL 
2ff2a 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2ff2b 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20  *zTableName,    
2ff2c 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a   /* Table name *
2ff2d 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2ff2e 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20  zColumnName,    
2ff2f 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  /* Column name *
2ff30 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
2ff31 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20  *pzDataType,    
2ff32 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61  /* OUTPUT: Decla
2ff33 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f  red data type */
2ff34 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a  .  char const **
2ff35 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f  pzCollSeq,     /
2ff36 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74  * OUTPUT: Collat
2ff37 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
2ff38 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74  e */.  int *pNot
2ff39 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Null,           
2ff3a 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
2ff3b 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63  ue if NOT NULL c
2ff3c 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73  onstraint exists
2ff3d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d   */.  int *pPrim
2ff3e 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20  aryKey,         
2ff3f 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
2ff40 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74  e if column part
2ff41 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20   of PK */.  int 
2ff42 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20  *pAutoinc       
2ff43 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
2ff44 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  T: True if colum
2ff45 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d  n is auto-increm
2ff46 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ent */.){.  int 
2ff47 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  rc;.  char *zErr
2ff48 4d 73 67 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65  Msg = 0;.  Table
2ff49 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 43 6f   *pTab = 0;.  Co
2ff4a 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b 0a  lumn *pCol = 0;.
2ff4b 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 0a 20 20 63    int iCol;..  c
2ff4c 68 61 72 20 63 6f 6e 73 74 20 2a 7a 44 61 74 61  har const *zData
2ff4d 54 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72  Type = 0;.  char
2ff4e 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 53 65 71   const *zCollSeq
2ff4f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 6f 74 6e   = 0;.  int notn
2ff50 75 6c 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 70  ull = 0;.  int p
2ff51 72 69 6d 61 72 79 6b 65 79 20 3d 20 30 3b 0a 20  rimarykey = 0;. 
2ff52 20 69 6e 74 20 61 75 74 6f 69 6e 63 20 3d 20 30   int autoinc = 0
2ff53 3b 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  ;..  /* Ensure t
2ff54 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
2ff55 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64  ma has been load
2ff56 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
2ff57 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2ff58 6d 75 74 65 78 29 3b 0a 20 20 28 76 6f 69 64 29  mutex);.  (void)
2ff59 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
2ff5a 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  db);.  sqlite3Bt
2ff5b 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
2ff5c 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49  .  rc = sqlite3I
2ff5d 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 4d 73 67  nit(db, &zErrMsg
2ff5e 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
2ff5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 67 6f  OK!=rc ){.    go
2ff60 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  to error_out;.  
2ff61 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  }..  /* Locate t
2ff62 68 65 20 74 61 62 6c 65 20 69 6e 20 71 75 65 73  he table in ques
2ff63 74 69 6f 6e 20 2a 2f 0a 20 20 70 54 61 62 20 3d  tion */.  pTab =
2ff64 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
2ff65 65 28 64 62 2c 20 7a 54 61 62 6c 65 4e 61 6d 65  e(db, zTableName
2ff66 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 69 66  , zDbName);.  if
2ff67 28 20 21 70 54 61 62 20 7c 7c 20 70 54 61 62 2d  ( !pTab || pTab-
2ff68 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
2ff69 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 67 6f  pTab = 0;.    go
2ff6a 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  to error_out;.  
2ff6b 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65  }..  /* Find the
2ff6c 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63   column for whic
2ff6d 68 20 69 6e 66 6f 20 69 73 20 72 65 71 75 65 73  h info is reques
2ff6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ted */.  if( sql
2ff6f 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c  ite3IsRowid(zCol
2ff70 75 6d 6e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  umnName) ){.    
2ff71 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b  iCol = pTab->iPK
2ff72 65 79 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c  ey;.    if( iCol
2ff73 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 43 6f  >=0 ){.      pCo
2ff74 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  l = &pTab->aCol[
2ff75 69 43 6f 6c 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  iCol];.    }.  }
2ff76 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 43  else{.    for(iC
2ff77 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d  ol=0; iCol<pTab-
2ff78 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a  >nCol; iCol++){.
2ff79 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54        pCol = &pT
2ff7a 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a  ab->aCol[iCol];.
2ff7b 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c        if( 0==sql
2ff7c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c  ite3StrICmp(pCol
2ff7d 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e  ->zName, zColumn
2ff7e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Name) ){.       
2ff7f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2ff80 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43      }.    if( iC
2ff81 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  ol==pTab->nCol )
2ff82 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30  {.      pTab = 0
2ff83 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72  ;.      goto err
2ff84 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  or_out;.    }.  
2ff85 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  }..  /* The foll
2ff86 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 74 6f 72  owing block stor
2ff87 65 73 20 74 68 65 20 6d 65 74 61 20 69 6e 66 6f  es the meta info
2ff88 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69 6c  rmation that wil
2ff89 6c 20 62 65 20 72 65 74 75 72 6e 65 64 0a 20 20  l be returned.  
2ff8a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ** to the caller
2ff8b 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   in local variab
2ff8c 6c 65 73 20 7a 44 61 74 61 54 79 70 65 2c 20 7a  les zDataType, z
2ff8d 43 6f 6c 6c 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c  CollSeq, notnull
2ff8e 2c 20 70 72 69 6d 61 72 79 6b 65 79 0a 20 20 2a  , primarykey.  *
2ff8f 2a 20 61 6e 64 20 61 75 74 6f 69 6e 63 2e 20 41  * and autoinc. A
2ff90 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
2ff91 72 65 20 61 72 65 20 74 77 6f 20 70 6f 73 73 69  re are two possi
2ff92 62 69 6c 69 74 69 65 73 3a 0a 20 20 2a 2a 20 0a  bilities:.  ** .
2ff93 20 20 2a 2a 20 20 20 20 20 31 2e 20 54 68 65 20    **     1. The 
2ff94 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e  specified column
2ff95 20 6e 61 6d 65 20 77 61 73 20 72 6f 77 69 64 22   name was rowid"
2ff96 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77  , "oid" or "_row
2ff97 69 64 5f 22 20 0a 20 20 2a 2a 20 20 20 20 20 20  id_" .  **      
2ff98 20 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e    and there is n
2ff99 6f 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63  o explicitly dec
2ff9a 6c 61 72 65 64 20 49 50 4b 20 63 6f 6c 75 6d 6e  lared IPK column
2ff9b 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  . .  **.  **    
2ff9c 20 32 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73   2. The table is
2ff9d 20 6e 6f 74 20 61 20 76 69 65 77 20 61 6e 64 20   not a view and 
2ff9e 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  the column name 
2ff9f 69 64 65 6e 74 69 66 69 65 64 20 61 6e 20 0a 20  identified an . 
2ffa0 20 2a 2a 20 20 20 20 20 20 20 20 65 78 70 6c 69   **        expli
2ffa1 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63  citly declared c
2ffa2 6f 6c 75 6d 6e 2e 20 43 6f 70 79 20 6d 65 74 61  olumn. Copy meta
2ffa3 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
2ffa4 6d 20 2a 70 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20  m *pCol..  */ . 
2ffa5 20 69 66 28 20 70 43 6f 6c 20 29 7b 0a 20 20 20   if( pCol ){.   
2ffa6 20 7a 44 61 74 61 54 79 70 65 20 3d 20 70 43 6f   zDataType = pCo
2ffa7 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20 20 20 7a 43  l->zType;.    zC
2ffa8 6f 6c 6c 53 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a  ollSeq = pCol->z
2ffa9 43 6f 6c 6c 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c  Coll;.    notnul
2ffaa 6c 20 3d 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c  l = pCol->notNul
2ffab 6c 21 3d 30 3b 0a 20 20 20 20 70 72 69 6d 61 72  l!=0;.    primar
2ffac 79 6b 65 79 20 20 3d 20 70 43 6f 6c 2d 3e 69 73  ykey  = pCol->is
2ffad 50 72 69 6d 4b 65 79 21 3d 30 3b 0a 20 20 20 20  PrimKey!=0;.    
2ffae 61 75 74 6f 69 6e 63 20 3d 20 70 54 61 62 2d 3e  autoinc = pTab->
2ffaf 69 50 4b 65 79 3d 3d 69 43 6f 6c 20 26 26 20 28  iPKey==iCol && (
2ffb0 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
2ffb1 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e   TF_Autoincremen
2ffb2 74 29 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  t)!=0;.  }else{.
2ffb3 20 20 20 20 7a 44 61 74 61 54 79 70 65 20 3d 20      zDataType = 
2ffb4 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 70  "INTEGER";.    p
2ffb5 72 69 6d 61 72 79 6b 65 79 20 3d 20 31 3b 0a 20  rimarykey = 1;. 
2ffb6 20 7d 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 53   }.  if( !zCollS
2ffb7 65 71 20 29 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53  eq ){.    zCollS
2ffb8 65 71 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20  eq = "BINARY";. 
2ffb9 20 7d 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20   }..error_out:. 
2ffba 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
2ffbb 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 28 76 6f  veAll(db);.  (vo
2ffbc 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2ffbd 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 57  Off(db);..  /* W
2ffbe 68 65 74 68 65 72 20 74 68 65 20 66 75 6e 63 74  hether the funct
2ffbf 69 6f 6e 20 63 61 6c 6c 20 73 75 63 63 65 65 64  ion call succeed
2ffc0 65 64 20 6f 72 20 66 61 69 6c 65 64 2c 20 73 65  ed or failed, se
2ffc1 74 20 74 68 65 20 6f 75 74 70 75 74 20 70 61 72  t the output par
2ffc2 61 6d 65 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20  ameters.  ** to 
2ffc3 77 68 61 74 65 76 65 72 20 74 68 65 69 72 20 6c  whatever their l
2ffc4 6f 63 61 6c 20 63 6f 75 6e 74 65 72 70 61 72 74  ocal counterpart
2ffc5 73 20 63 6f 6e 74 61 69 6e 2e 20 49 66 20 61 6e  s contain. If an
2ffc6 20 65 72 72 6f 72 20 64 69 64 20 6f 63 63 75 72   error did occur
2ffc7 2c 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 73 20  ,.  ** this has 
2ffc8 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 7a 65  the effect of ze
2ffc9 72 6f 69 6e 67 20 61 6c 6c 20 6f 75 74 70 75 74  roing all output
2ffca 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a   parameters..  *
2ffcb 2f 0a 20 20 69 66 28 20 70 7a 44 61 74 61 54 79  /.  if( pzDataTy
2ffcc 70 65 20 29 20 2a 70 7a 44 61 74 61 54 79 70 65  pe ) *pzDataType
2ffcd 20 3d 20 7a 44 61 74 61 54 79 70 65 3b 0a 20 20   = zDataType;.  
2ffce 69 66 28 20 70 7a 43 6f 6c 6c 53 65 71 20 29 20  if( pzCollSeq ) 
2ffcf 2a 70 7a 43 6f 6c 6c 53 65 71 20 3d 20 7a 43 6f  *pzCollSeq = zCo
2ffd0 6c 6c 53 65 71 3b 0a 20 20 69 66 28 20 70 4e 6f  llSeq;.  if( pNo
2ffd1 74 4e 75 6c 6c 20 29 20 2a 70 4e 6f 74 4e 75 6c  tNull ) *pNotNul
2ffd2 6c 20 3d 20 6e 6f 74 6e 75 6c 6c 3b 0a 20 20 69  l = notnull;.  i
2ffd3 66 28 20 70 50 72 69 6d 61 72 79 4b 65 79 20 29  f( pPrimaryKey )
2ffd4 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 20 3d 20   *pPrimaryKey = 
2ffd5 70 72 69 6d 61 72 79 6b 65 79 3b 0a 20 20 69 66  primarykey;.  if
2ffd6 28 20 70 41 75 74 6f 69 6e 63 20 29 20 2a 70 41  ( pAutoinc ) *pA
2ffd7 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 69 6e 63  utoinc = autoinc
2ffd8 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  ;..  if( SQLITE_
2ffd9 4f 4b 3d 3d 72 63 20 26 26 20 21 70 54 61 62 20  OK==rc && !pTab 
2ffda 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
2ffdb 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67  Free(db, zErrMsg
2ffdc 29 3b 0a 20 20 20 20 7a 45 72 72 4d 73 67 20 3d  );.    zErrMsg =
2ffdd 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2ffde 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62  db, "no such tab
2ffdf 6c 65 20 63 6f 6c 75 6d 6e 3a 20 25 73 2e 25 73  le column: %s.%s
2ffe0 22 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 0a 20  ", zTableName,. 
2ffe1 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 4e 61         zColumnNa
2ffe2 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  me);.    rc = SQ
2ffe3 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
2ffe4 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2ffe5 62 2c 20 72 63 2c 20 28 7a 45 72 72 4d 73 67 3f  b, rc, (zErrMsg?
2ffe6 22 25 73 22 3a 30 29 2c 20 7a 45 72 72 4d 73 67  "%s":0), zErrMsg
2ffe7 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2ffe8 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b  ee(db, zErrMsg);
2ffe9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
2ffea 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
2ffeb 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2ffec 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
2ffed 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2ffee 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
2ffef 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c  leep for a littl
2fff0 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e  e while.  Return
2fff1 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
2fff2 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 53 51  ime slept..*/.SQ
2fff3 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2fff4 69 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 20 6d  ite3_sleep(int m
2fff5 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  s){.  sqlite3_vf
2fff6 73 20 2a 70 56 66 73 3b 0a 20 20 69 6e 74 20 72  s *pVfs;.  int r
2fff7 63 3b 0a 20 20 70 56 66 73 20 3d 20 73 71 6c 69  c;.  pVfs = sqli
2fff8 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 3b  te3_vfs_find(0);
2fff9 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29  .  if( pVfs==0 )
2fffa 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a   return 0;..  /*
2fffb 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   This function w
2fffc 6f 72 6b 73 20 69 6e 20 6d 69 6c 6c 69 73 65 63  orks in millisec
2fffd 6f 6e 64 73 2c 20 62 75 74 20 74 68 65 20 75 6e  onds, but the un
2fffe 64 65 72 6c 79 69 6e 67 20 4f 73 53 6c 65 65 70  derlying OsSleep
2ffff 28 29 20 0a 20 20 2a 2a 20 41 50 49 20 75 73 65  () .  ** API use
30000 73 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2e 20  s microseconds. 
30001 48 65 6e 63 65 20 74 68 65 20 31 30 30 30 27 73  Hence the 1000's
30002 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 28 73  ..  */.  rc = (s
30003 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 70 56  qlite3OsSleep(pV
30004 66 73 2c 20 31 30 30 30 2a 6d 73 29 2f 31 30 30  fs, 1000*ms)/100
30005 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  0);.  return rc;
30006 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65  .}../*.** Enable
30007 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20   or disable the 
30008 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
30009 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  codes..*/.SQLITE
3000a 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3000b 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  _extended_result
3000c 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 20 2a  _codes(sqlite3 *
3000d 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a  db, int onoff){.
3000e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
3000f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
30010 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20  ;.  db->errMask 
30011 3d 20 6f 6e 6f 66 66 20 3f 20 30 78 66 66 66 66  = onoff ? 0xffff
30012 66 66 66 66 20 3a 20 30 78 66 66 3b 0a 20 20 73  ffff : 0xff;.  s
30013 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
30014 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
30015 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
30016 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  K;.}../*.** Invo
30017 6b 65 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74  ke the xFileCont
30018 72 6f 6c 20 6d 65 74 68 6f 64 20 6f 6e 20 61 20  rol method on a 
30019 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
3001a 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ase..*/.SQLITE_A
3001b 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
3001c 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69  ile_control(sqli
3001d 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
3001e 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e  har *zDbName, in
3001f 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67  t op, void *pArg
30020 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
30021 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 6e  LITE_ERROR;.  in
30022 74 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33  t iDb;.  sqlite3
30023 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
30024 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 7a  >mutex);.  if( z
30025 44 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  DbName==0 ){.   
30026 20 69 44 62 20 3d 20 30 3b 0a 20 20 7d 65 6c 73   iDb = 0;.  }els
30027 65 7b 0a 20 20 20 20 66 6f 72 28 69 44 62 3d 30  e{.    for(iDb=0
30028 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69  ; iDb<db->nDb; i
30029 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  Db++){.      if(
3002a 20 73 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b   strcmp(db->aDb[
3002b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 4e  iDb].zName, zDbN
3002c 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b  ame)==0 ) break;
3002d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
3002e 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 7b 0a   iDb<db->nDb ){.
3002f 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 72 65      Btree *pBtre
30030 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  e = db->aDb[iDb]
30031 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42  .pBt;.    if( pB
30032 74 72 65 65 20 29 7b 0a 20 20 20 20 20 20 50 61  tree ){.      Pa
30033 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20  ger *pPager;.   
30034 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20     sqlite3_file 
30035 2a 66 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  *fd;.      sqlit
30036 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74  e3BtreeEnter(pBt
30037 72 65 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67  ree);.      pPag
30038 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
30039 65 50 61 67 65 72 28 70 42 74 72 65 65 29 3b 0a  ePager(pBtree);.
3003a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
3003b 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 20 20 20  ager!=0 );.     
3003c 20 66 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67   fd = sqlite3Pag
3003d 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a  erFile(pPager);.
3003e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 64        assert( fd
3003f 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  !=0 );.      if(
30040 20 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b   fd->pMethods ){
30041 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
30042 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
30043 6f 6c 28 66 64 2c 20 6f 70 2c 20 70 41 72 67 29  ol(fd, op, pArg)
30044 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
30045 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
30046 65 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 7d  e(pBtree);.    }
30047 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
30048 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
30049 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
3004a 72 63 3b 20 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rc;   .}../*.** 
3004b 49 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  Interface to the
3004c 20 74 65 73 74 69 6e 67 20 6c 6f 67 69 63 2e 0a   testing logic..
3004d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3004e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  t sqlite3_test_c
3004f 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e  ontrol(int op, .
30050 2e 2e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ..){.  int rc = 
30051 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
30052 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
30053 45 53 54 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  EST.  va_list ap
30054 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
30055 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20   op);.  switch( 
30056 6f 70 20 29 7b 0a 0a 20 20 20 20 2f 2a 0a 20 20  op ){..    /*.  
30057 20 20 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75    ** Save the cu
30058 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74  rrent state of t
30059 68 65 20 50 52 4e 47 2e 0a 20 20 20 20 2a 2f 0a  he PRNG..    */.
3005a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
3005b 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53 41  TESTCTRL_PRNG_SA
3005c 56 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  VE: {.      sqli
3005d 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65  te3PrngSaveState
3005e 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ();.      break;
3005f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
30060 20 20 20 2a 2a 20 52 65 73 74 6f 72 65 20 74 68     ** Restore th
30061 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 50  e state of the P
30062 52 4e 47 20 74 6f 20 74 68 65 20 6c 61 73 74 20  RNG to the last 
30063 73 74 61 74 65 20 73 61 76 65 64 20 75 73 69 6e  state saved usin
30064 67 0a 20 20 20 20 2a 2a 20 50 52 4e 47 5f 53 41  g.    ** PRNG_SA
30065 56 45 2e 20 20 49 66 20 50 52 4e 47 5f 53 41 56  VE.  If PRNG_SAV
30066 45 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f  E has never befo
30067 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20  re been called, 
30068 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73  then.    ** this
30069 20 76 65 72 62 20 61 63 74 73 20 6c 69 6b 65 20   verb acts like 
3006a 50 52 4e 47 5f 52 45 53 45 54 2e 0a 20 20 20 20  PRNG_RESET..    
3006b 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
3006c 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47  TE_TESTCTRL_PRNG
3006d 5f 52 45 53 54 4f 52 45 3a 20 7b 0a 20 20 20 20  _RESTORE: {.    
3006e 20 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73    sqlite3PrngRes
3006f 74 6f 72 65 53 74 61 74 65 28 29 3b 0a 20 20 20  toreState();.   
30070 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
30071 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52  .    /*.    ** R
30072 65 73 65 74 20 74 68 65 20 50 52 4e 47 20 62 61  eset the PRNG ba
30073 63 6b 20 74 6f 20 69 74 73 20 75 6e 69 6e 69 74  ck to its uninit
30074 69 61 6c 69 7a 65 64 20 73 74 61 74 65 2e 20 20  ialized state.  
30075 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 0a 20 20  The next call.  
30076 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f    ** to sqlite3_
30077 72 61 6e 64 6f 6d 6e 65 73 73 28 29 20 77 69 6c  randomness() wil
30078 6c 20 72 65 73 65 65 64 20 74 68 65 20 50 52 4e  l reseed the PRN
30079 47 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65  G using a single
3007a 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20   call.    ** to 
3007b 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20  the xRandomness 
3007c 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65  method of the de
3007d 66 61 75 6c 74 20 56 46 53 2e 0a 20 20 20 20 2a  fault VFS..    *
3007e 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
3007f 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f  E_TESTCTRL_PRNG_
30080 52 45 53 45 54 3a 20 7b 0a 20 20 20 20 20 20 73  RESET: {.      s
30081 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 53  qlite3PrngResetS
30082 74 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72  tate();.      br
30083 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
30084 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74  /*.    **  sqlit
30085 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
30086 42 49 54 56 45 43 5f 54 45 53 54 2c 20 73 69 7a  BITVEC_TEST, siz
30087 65 2c 20 70 72 6f 67 72 61 6d 29 0a 20 20 20 20  e, program).    
30088 2a 2a 0a 20 20 20 20 2a 2a 20 52 75 6e 20 61 20  **.    ** Run a 
30089 74 65 73 74 20 61 67 61 69 6e 73 74 20 61 20 42  test against a B
3008a 69 74 76 65 63 20 6f 62 6a 65 63 74 20 6f 66 20  itvec object of 
3008b 73 69 7a 65 2e 20 20 54 68 65 20 70 72 6f 67 72  size.  The progr
3008c 61 6d 20 61 72 67 75 6d 65 6e 74 0a 20 20 20 20  am argument.    
3008d 2a 2a 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f  ** is an array o
3008e 66 20 69 6e 74 65 67 65 72 73 20 74 68 61 74 20  f integers that 
3008f 64 65 66 69 6e 65 73 20 74 68 65 20 74 65 73 74  defines the test
30090 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 6f 6e 20  .  Return -1 on 
30091 61 0a 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79 20  a.    ** memory 
30092 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
30093 2c 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2c 20  , 0 on success, 
30094 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20  or non-zero for 
30095 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2a  an error..    **
30096 20 53 65 65 20 74 68 65 20 73 71 6c 69 74 65 33   See the sqlite3
30097 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73  BitvecBuiltinTes
30098 74 28 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  t() for addition
30099 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
3009a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
3009b 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3009c 42 49 54 56 45 43 5f 54 45 53 54 3a 20 7b 0a 20  BITVEC_TEST: {. 
3009d 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61       int sz = va
3009e 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
3009f 20 20 20 20 20 69 6e 74 20 2a 61 50 72 6f 67 20       int *aProg 
300a0 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
300a1 2a 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  *);.      rc = s
300a2 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c  qlite3BitvecBuil
300a3 74 69 6e 54 65 73 74 28 73 7a 2c 20 61 50 72 6f  tinTest(sz, aPro
300a4 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  g);.      break;
300a5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
300a6 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74     **  sqlite3_t
300a7 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 42 45 4e 49  est_control(BENI
300a8 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 2c  GN_MALLOC_HOOKS,
300a9 20 78 42 65 67 69 6e 2c 20 78 45 6e 64 29 0a 20   xBegin, xEnd). 
300aa 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 65 67     **.    ** Reg
300ab 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63  ister hooks to c
300ac 61 6c 6c 20 74 6f 20 69 6e 64 69 63 61 74 65 20  all to indicate 
300ad 77 68 69 63 68 20 6d 61 6c 6c 6f 63 28 29 20 66  which malloc() f
300ae 61 69 6c 75 72 65 73 20 0a 20 20 20 20 2a 2a 20  ailures .    ** 
300af 61 72 65 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20  are benign..    
300b0 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
300b1 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49  TE_TESTCTRL_BENI
300b2 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 3a  GN_MALLOC_HOOKS:
300b3 20 7b 0a 20 20 20 20 20 20 74 79 70 65 64 65 66   {.      typedef
300b4 20 76 6f 69 64 20 28 2a 76 6f 69 64 5f 66 75 6e   void (*void_fun
300b5 63 74 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20  ction)(void);.  
300b6 20 20 20 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f      void_functio
300b7 6e 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a  n xBenignBegin;.
300b8 20 20 20 20 20 20 76 6f 69 64 5f 66 75 6e 63 74        void_funct
300b9 69 6f 6e 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a  ion xBenignEnd;.
300ba 20 20 20 20 20 20 78 42 65 6e 69 67 6e 42 65 67        xBenignBeg
300bb 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  in = va_arg(ap, 
300bc 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a  void_function);.
300bd 20 20 20 20 20 20 78 42 65 6e 69 67 6e 45 6e 64        xBenignEnd
300be 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
300bf 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a 20 20  id_function);.  
300c0 20 20 20 20 73 71 6c 69 74 65 33 42 65 6e 69 67      sqlite3Benig
300c1 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 78 42 65  nMallocHooks(xBe
300c2 6e 69 67 6e 42 65 67 69 6e 2c 20 78 42 65 6e 69  nignBegin, xBeni
300c3 67 6e 45 6e 64 29 3b 0a 20 20 20 20 20 20 62 72  gnEnd);.      br
300c4 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
300c5 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74  /*.    **  sqlit
300c6 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
300c7 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 75 6e  PENDING_BYTE, un
300c8 73 69 67 6e 65 64 20 69 6e 74 20 58 29 0a 20 20  signed int X).  
300c9 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 53 65 74 20    **.    ** Set 
300ca 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65  the PENDING byte
300cb 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e   to the value in
300cc 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 69   the argument, i
300cd 66 20 58 3e 30 2e 0a 20 20 20 20 2a 2a 20 4d 61  f X>0..    ** Ma
300ce 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66  ke no changes if
300cf 20 58 3d 3d 30 2e 20 20 52 65 74 75 72 6e 20 74   X==0.  Return t
300d0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
300d1 70 65 6e 64 69 6e 67 20 62 79 74 65 0a 20 20 20  pending byte.   
300d2 20 2a 2a 20 61 73 20 69 74 20 65 78 69 73 74 69   ** as it existi
300d3 6e 67 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ng before this r
300d4 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65  outine was calle
300d5 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d..    **.    **
300d6 20 49 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61   IMPORTANT:  Cha
300d7 6e 67 69 6e 67 20 74 68 65 20 50 45 4e 44 49 4e  nging the PENDIN
300d8 47 20 62 79 74 65 20 66 72 6f 6d 20 30 78 34 30  G byte from 0x40
300d9 30 30 30 30 30 30 20 72 65 73 75 6c 74 73 20 69  000000 results i
300da 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f  n.    ** an inco
300db 6d 70 61 74 69 62 6c 65 20 64 61 74 61 62 61 73  mpatible databas
300dc 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20  e file format.  
300dd 43 68 61 6e 67 69 6e 67 20 74 68 65 20 50 45 4e  Changing the PEN
300de 44 49 4e 47 20 62 79 74 65 0a 20 20 20 20 2a 2a  DING byte.    **
300df 20 77 68 69 6c 65 20 61 6e 79 20 64 61 74 61 62   while any datab
300e0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
300e1 73 20 6f 70 65 6e 20 72 65 73 75 6c 74 73 20 69  s open results i
300e2 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a  n undefined and.
300e3 20 20 20 20 2a 2a 20 64 69 6c 65 74 65 72 69 6f      ** dileterio
300e4 75 73 20 62 65 68 61 76 69 6f 72 2e 0a 20 20 20  us behavior..   
300e5 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c   */.    case SQL
300e6 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e  ITE_TESTCTRL_PEN
300e7 44 49 4e 47 5f 42 59 54 45 3a 20 7b 0a 20 20 20  DING_BYTE: {.   
300e8 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
300e9 6e 65 77 56 61 6c 20 3d 20 76 61 5f 61 72 67 28  newVal = va_arg(
300ea 61 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ap, unsigned int
300eb 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
300ec 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65  lite3PendingByte
300ed 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77 56  ;.      if( newV
300ee 61 6c 20 29 20 73 71 6c 69 74 65 33 50 65 6e 64  al ) sqlite3Pend
300ef 69 6e 67 42 79 74 65 20 3d 20 6e 65 77 56 61 6c  ingByte = newVal
300f0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
300f1 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e     }.  }.  va_en
300f2 64 28 61 70 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  d(ap);.#endif /*
300f3 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
300f4 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 20 20 72  LTIN_TEST */.  r
300f5 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a  eturn rc;.}../**
300f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
300f7 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   of main.c *****
300f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
300f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
300fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
300fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
300fc 69 6e 20 66 69 6c 65 20 6e 6f 74 69 66 79 2e 63  in file notify.c
300fd 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
300fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
300ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
30100 2a 2a 20 32 30 30 39 20 4d 61 72 63 68 20 33 0a  ** 2009 March 3.
30101 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
30102 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
30103 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
30104 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
30105 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
30106 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
30107 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
30108 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
30109 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
3010a 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
3010b 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
3010c 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
3010d 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
3010e 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
3010f 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
30110 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
30111 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
30112 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
30113 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30114 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30115 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30116 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
30117 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
30118 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c  ontains the impl
30119 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
3011a 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  e sqlite3_unlock
3011b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2a 20 41 50 49  _notify().** API
3011c 20 6d 65 74 68 6f 64 20 61 6e 64 20 69 74 73 20   method and its 
3011d 61 73 73 6f 63 69 61 74 65 64 20 66 75 6e 63 74  associated funct
3011e 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20  ionality..**.** 
3011f 24 49 64 3a 20 6e 6f 74 69 66 79 2e 63 2c 76 20  $Id: notify.c,v 
30120 31 2e 32 20 32 30 30 39 2f 30 33 2f 32 35 20 31  1.2 2009/03/25 1
30121 36 3a 35 31 3a 34 33 20 64 72 68 20 45 78 70 20  6:51:43 drh Exp 
30122 24 0a 2a 2f 0a 0a 2f 2a 20 4f 6d 69 74 20 74 68  $.*/../* Omit th
30123 69 73 20 65 6e 74 69 72 65 20 66 69 6c 65 20 69  is entire file i
30124 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
30125 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 20 69 73  UNLOCK_NOTIFY is
30126 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 20 2a 2f   not defined. */
30127 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
30128 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54  NABLE_UNLOCK_NOT
30129 49 46 59 0a 0a 2f 2a 0a 2a 2a 20 50 75 62 6c 69  IFY../*.** Publi
3012a 63 20 69 6e 74 65 72 66 61 63 65 73 3a 0a 2a 2a  c interfaces:.**
3012b 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  .**   sqlite3Con
3012c 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 29  nectionBlocked()
3012d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  .**   sqlite3Con
3012e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
3012f 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f  ).**   sqlite3Co
30130 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 29  nnectionClosed()
30131 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 5f 75 6e  .**   sqlite3_un
30132 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2f  lock_notify().*/
30133 0a 0a 23 64 65 66 69 6e 65 20 61 73 73 65 72 74  ..#define assert
30134 4d 75 74 65 78 48 65 6c 64 28 29 20 5c 0a 20 20  MutexHeld() \.  
30135 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
30136 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
30137 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
30138 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
30139 5f 4d 41 53 54 45 52 29 29 20 29 0a 0a 2f 2a 0a  _MASTER)) )../*.
3013a 2a 2a 20 48 65 61 64 20 6f 66 20 61 20 6c 69 6e  ** Head of a lin
3013b 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  ked list of all 
3013c 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 73 20  sqlite3 objects 
3013d 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73 20  created by this 
3013e 70 72 6f 63 65 73 73 0a 2a 2a 20 66 6f 72 20 77  process.** for w
3013f 68 69 63 68 20 65 69 74 68 65 72 20 73 71 6c 69  hich either sqli
30140 74 65 33 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e  te3.pBlockingCon
30141 6e 65 63 74 69 6f 6e 20 6f 72 20 73 71 6c 69 74  nection or sqlit
30142 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  e3.pUnlockConnec
30143 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e  tion.** is not N
30144 55 4c 4c 2e 20 54 68 69 73 20 76 61 72 69 61 62  ULL. This variab
30145 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 61 63 63 65  le may only acce
30146 73 73 65 64 20 77 68 69 6c 65 20 74 68 65 20 53  ssed while the S
30147 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20  TATIC_MASTER.** 
30148 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a  mutex is held..*
30149 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
3014a 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c   *SQLITE_WSD sql
3014b 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 20  ite3BlockedList 
3014c 3d 20 30 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44  = 0;..#ifndef ND
3014d 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EBUG./*.** This 
3014e 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 63 6f  function is a co
3014f 6d 70 6c 65 78 20 61 73 73 65 72 74 28 29 20 74  mplex assert() t
30150 68 61 74 20 76 65 72 69 66 69 65 73 20 74 68 65  hat verifies the
30151 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 70   following .** p
30152 72 6f 70 65 72 74 69 65 73 20 6f 66 20 74 68 65  roperties of the
30153 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
30154 69 6f 6e 73 20 6c 69 73 74 3a 0a 2a 2a 0a 2a 2a  ions list:.**.**
30155 20 20 20 31 29 20 45 61 63 68 20 65 6e 74 72 79     1) Each entry
30156 20 69 6e 20 74 68 65 20 6c 69 73 74 20 68 61 73   in the list has
30157 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75   a non-NULL valu
30158 65 20 66 6f 72 20 65 69 74 68 65 72 20 0a 2a 2a  e for either .**
30159 20 20 20 20 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e        pUnlockCon
3015a 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63  nection or pBloc
3015b 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2c 20  kingConnection, 
3015c 6f 72 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 20  or both..**.**  
3015d 20 32 29 20 41 6c 6c 20 65 6e 74 72 69 65 73 20   2) All entries 
3015e 69 6e 20 74 68 65 20 6c 69 73 74 20 74 68 61 74  in the list that
3015f 20 73 68 61 72 65 20 61 20 63 6f 6d 6d 6f 6e 20   share a common 
30160 76 61 6c 75 65 20 66 6f 72 20 0a 2a 2a 20 20 20  value for .**   
30161 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79     xUnlockNotify
30162 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67   are grouped tog
30163 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 33  ether..**.**   3
30164 29 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e  ) If the argumen
30165 74 20 64 62 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  t db is not NULL
30166 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74  , then none of t
30167 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  he entries in th
30168 65 0a 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 65  e.**      blocke
30169 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69  d connections li
3016a 73 74 20 68 61 76 65 20 70 55 6e 6c 6f 63 6b 43  st have pUnlockC
3016b 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c  onnection or pBl
3016c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
3016d 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20  .**      set to 
3016e 64 62 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  db. This is used
3016f 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 63 6f   when closing co
30170 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 2a 2f 0a  nnection db..*/.
30171 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
30172 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73 28  kListProperties(
30173 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
30174 73 71 6c 69 74 65 33 20 2a 70 3b 0a 20 20 66 6f  sqlite3 *p;.  fo
30175 72 28 70 3d 73 71 6c 69 74 65 33 42 6c 6f 63 6b  r(p=sqlite3Block
30176 65 64 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e  edList; p; p=p->
30177 70 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20  pNextBlocked){. 
30178 20 20 20 69 6e 74 20 73 65 65 6e 20 3d 20 30 3b     int seen = 0;
30179 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 70 32  .    sqlite3 *p2
3017a 3b 0a 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79  ;..    /* Verify
3017b 20 70 72 6f 70 65 72 74 79 20 28 31 29 20 2a 2f   property (1) */
3017c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
3017d 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
3017e 6e 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e  n || p->pBlockin
3017f 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 29 3b 0a 0a  gConnection );..
30180 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 70 72      /* Verify pr
30181 6f 70 65 72 74 79 20 28 32 29 20 2a 2f 0a 20 20  operty (2) */.  
30182 20 20 66 6f 72 28 70 32 3d 73 71 6c 69 74 65 33    for(p2=sqlite3
30183 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 70 32 21  BlockedList; p2!
30184 3d 70 3b 20 70 32 3d 70 32 2d 3e 70 4e 65 78 74  =p; p2=p2->pNext
30185 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20 20 20  Blocked){.      
30186 69 66 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e  if( p2->xUnlockN
30187 6f 74 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63  otify==p->xUnloc
30188 6b 4e 6f 74 69 66 79 20 29 20 73 65 65 6e 20 3d  kNotify ) seen =
30189 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   1;.      assert
3018a 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  ( p2->xUnlockNot
3018b 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e  ify==p->xUnlockN
3018c 6f 74 69 66 79 20 7c 7c 20 21 73 65 65 6e 20 29  otify || !seen )
3018d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
3018e 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 55 6e 6c  db==0 || p->pUnl
3018f 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64  ockConnection!=d
30190 62 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  b );.      asser
30191 74 28 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70  t( db==0 || p->p
30192 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
30193 6f 6e 21 3d 64 62 20 29 3b 0a 20 20 20 20 7d 0a  on!=db );.    }.
30194 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65    }.}.#else.# de
30195 66 69 6e 65 20 63 68 65 63 6b 4c 69 73 74 50 72  fine checkListPr
30196 6f 70 65 72 74 69 65 73 28 78 29 0a 23 65 6e 64  operties(x).#end
30197 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  if../*.** Remove
30198 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 66   connection db f
30199 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  rom the blocked 
3019a 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
3019b 2e 20 49 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  . If connection.
3019c 2a 2a 20 64 62 20 69 73 20 6e 6f 74 20 63 75 72  ** db is not cur
3019d 72 65 6e 74 6c 79 20 61 20 70 61 72 74 20 6f 66  rently a part of
3019e 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20   the list, this 
3019f 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
301a0 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  -op..*/.static v
301a1 6f 69 64 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c  oid removeFromBl
301a2 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c 69 74 65  ockedList(sqlite
301a3 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65  3 *db){.  sqlite
301a4 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74  3 **pp;.  assert
301a5 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 66  MutexHeld();.  f
301a6 6f 72 28 70 70 3d 26 73 71 6c 69 74 65 33 42 6c  or(pp=&sqlite3Bl
301a7 6f 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70 3b 20  ockedList; *pp; 
301a8 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65  pp = &(*pp)->pNe
301a9 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20  xtBlocked){.    
301aa 69 66 28 20 2a 70 70 3d 3d 64 62 20 29 7b 0a 20  if( *pp==db ){. 
301ab 20 20 20 20 20 2a 70 70 20 3d 20 28 2a 70 70 29       *pp = (*pp)
301ac 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0a  ->pNextBlocked;.
301ad 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
301ae 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
301af 41 64 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64  Add connection d
301b0 62 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 65 64  b to the blocked
301b1 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73   connections lis
301b2 74 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  t. It is assumed
301b3 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 6e  .** that it is n
301b4 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70 61 72  ot already a par
301b5 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 0a 2a  t of the list..*
301b6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64  /.static void ad
301b7 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28 73  dToBlockedList(s
301b8 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73  qlite3 *db){.  s
301b9 71 6c 69 74 65 33 20 2a 2a 70 70 3b 0a 20 20 61  qlite3 **pp;.  a
301ba 73 73 65 72 74 4d 75 74 65 78 48 65 6c 64 28 29  ssertMutexHeld()
301bb 3b 0a 20 20 66 6f 72 28 0a 20 20 20 20 70 70 3d  ;.  for(.    pp=
301bc 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c  &sqlite3BlockedL
301bd 69 73 74 3b 20 0a 20 20 20 20 2a 70 70 20 26 26  ist; .    *pp &&
301be 20 28 2a 70 70 29 2d 3e 78 55 6e 6c 6f 63 6b 4e   (*pp)->xUnlockN
301bf 6f 74 69 66 79 21 3d 64 62 2d 3e 78 55 6e 6c 6f  otify!=db->xUnlo
301c0 63 6b 4e 6f 74 69 66 79 3b 20 0a 20 20 20 20 70  ckNotify; .    p
301c1 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42  p=&(*pp)->pNextB
301c2 6c 6f 63 6b 65 64 0a 20 20 29 3b 0a 20 20 64 62  locked.  );.  db
301c3 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d  ->pNextBlocked =
301c4 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20 64 62   *pp;.  *pp = db
301c5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69  ;.}../*.** Obtai
301c6 6e 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53  n the STATIC_MAS
301c7 54 45 52 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74  TER mutex..*/.st
301c8 61 74 69 63 20 76 6f 69 64 20 65 6e 74 65 72 4d  atic void enterM
301c9 75 74 65 78 28 29 7b 0a 20 20 73 71 6c 69 74 65  utex(){.  sqlite
301ca 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
301cb 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
301cc 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
301cd 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20  TIC_MASTER));.  
301ce 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74  checkListPropert
301cf 69 65 73 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ies(0);.}../*.**
301d0 20 52 65 6c 65 61 73 65 20 74 68 65 20 53 54 41   Release the STA
301d1 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
301d2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
301d3 20 6c 65 61 76 65 4d 75 74 65 78 28 29 7b 0a 20   leaveMutex(){. 
301d4 20 61 73 73 65 72 74 4d 75 74 65 78 48 65 6c 64   assertMutexHeld
301d5 28 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74 50  ();.  checkListP
301d6 72 6f 70 65 72 74 69 65 73 28 30 29 3b 0a 20 20  roperties(0);.  
301d7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
301d8 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
301d9 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
301da 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
301db 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  ));.}../*.** Reg
301dc 69 73 74 65 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d  ister an unlock-
301dd 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2e  notify callback.
301de 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
301df 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  nt sqlite3_unloc
301e0 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c 69  k_notify(.  sqli
301e1 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20  te3 *db,.  void 
301e2 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69 64 20  (*xNotify)(void 
301e3 2a 2a 2c 20 69 6e 74 29 2c 0a 20 20 76 6f 69 64  **, int),.  void
301e4 20 2a 70 41 72 67 0a 29 7b 0a 20 20 69 6e 74 20   *pArg.){.  int 
301e5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
301e6 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
301e7 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
301e8 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28  );.  enterMutex(
301e9 29 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 64 62 2d  );..  if( 0==db-
301ea 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
301eb 74 69 6f 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54  tion ){.    /* T
301ec 68 65 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61 6e  he blocking tran
301ed 73 61 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e  saction has been
301ee 20 63 6f 6e 63 6c 75 64 65 64 2e 20 4f 72 20 74   concluded. Or t
301ef 68 65 72 65 20 6e 65 76 65 72 20 77 61 73 20 61  here never was a
301f0 20 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 69 6e   .    ** blockin
301f1 67 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49  g transaction. I
301f2 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 69  n either case, i
301f3 6e 76 6f 6b 65 20 74 68 65 20 6e 6f 74 69 66 79  nvoke the notify
301f4 20 63 61 6c 6c 62 61 63 6b 0a 20 20 20 20 2a 2a   callback.    **
301f5 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20   immediately. . 
301f6 20 20 20 2a 2f 0a 20 20 20 20 78 4e 6f 74 69 66     */.    xNotif
301f7 79 28 26 70 41 72 67 2c 20 31 29 3b 0a 20 20 7d  y(&pArg, 1);.  }
301f8 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
301f9 33 20 2a 70 3b 0a 0a 20 20 20 20 66 6f 72 28 70  3 *p;..    for(p
301fa 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f  =db->pBlockingCo
301fb 6e 6e 65 63 74 69 6f 6e 3b 20 70 20 26 26 20 70  nnection; p && p
301fc 21 3d 64 62 3b 20 70 3d 70 2d 3e 70 55 6e 6c 6f  !=db; p=p->pUnlo
301fd 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 29 3b 0a 20  ckConnection);. 
301fe 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
301ff 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f    rc = SQLITE_LO
30200 43 4b 45 44 3b 20 20 20 20 20 20 20 20 20 20 20  CKED;           
30201 20 20 20 2f 2a 20 44 65 61 64 6c 6f 63 6b 20 64     /* Deadlock d
30202 65 74 65 63 74 65 64 2e 20 2a 2f 0a 20 20 20 20  etected. */.    
30203 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d  }else{.      db-
30204 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69  >pUnlockConnecti
30205 6f 6e 20 3d 20 64 62 2d 3e 70 42 6c 6f 63 6b 69  on = db->pBlocki
30206 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 0a 20 20  ngConnection;.  
30207 20 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e      db->xUnlockN
30208 6f 74 69 66 79 20 3d 20 78 4e 6f 74 69 66 79 3b  otify = xNotify;
30209 0a 20 20 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f  .      db->pUnlo
3020a 63 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  ckArg = pArg;.  
3020b 20 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c      removeFromBl
3020c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20  ockedList(db);. 
3020d 20 20 20 20 20 61 64 64 54 6f 42 6c 6f 63 6b 65       addToBlocke
3020e 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20 7d  dList(db);.    }
3020f 0a 20 20 7d 0a 0a 20 20 6c 65 61 76 65 4d 75 74  .  }..  leaveMut
30210 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ex();.  assert( 
30211 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
30212 64 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  d );.  sqlite3Er
30213 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 72 63 3f  ror(db, rc, (rc?
30214 22 64 61 74 61 62 61 73 65 20 69 73 20 64 65 61  "database is dea
30215 64 6c 6f 63 6b 65 64 22 3a 30 29 29 3b 0a 20 20  dlocked":0));.  
30216 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
30217 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
30218 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
30219 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
3021a 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ion is called wh
3021b 69 6c 65 20 73 74 65 70 70 69 6e 67 20 6f 72 20  ile stepping or 
3021c 70 72 65 70 61 72 69 6e 67 20 61 20 73 74 61 74  preparing a stat
3021d 65 6d 65 6e 74 20 0a 2a 2a 20 61 73 73 6f 63 69  ement .** associ
3021e 61 74 65 64 20 77 69 74 68 20 63 6f 6e 6e 65 63  ated with connec
3021f 74 69 6f 6e 20 64 62 2e 20 54 68 65 20 6f 70 65  tion db. The ope
30220 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 74 75  ration will retu
30221 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
30222 0a 2a 2a 20 74 6f 20 74 68 65 20 75 73 65 72 20  .** to the user 
30223 62 65 63 61 75 73 65 20 69 74 20 72 65 71 75 69  because it requi
30224 72 65 73 20 61 20 6c 6f 63 6b 20 74 68 61 74 20  res a lock that 
30225 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 76 61 69  will not be avai
30226 6c 61 62 6c 65 0a 2a 2a 20 75 6e 74 69 6c 20 63  lable.** until c
30227 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 6c 6f 63 6b  onnection pBlock
30228 65 72 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73  er concludes its
30229 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
3022a 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
3022b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
3022c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c  ite3ConnectionBl
3022d 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64  ocked(sqlite3 *d
3022e 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f  b, sqlite3 *pBlo
3022f 63 6b 65 72 29 7b 0a 20 20 65 6e 74 65 72 4d 75  cker){.  enterMu
30230 74 65 78 28 29 3b 0a 20 20 69 66 28 20 64 62 2d  tex();.  if( db-
30231 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
30232 74 69 6f 6e 3d 3d 30 20 26 26 20 64 62 2d 3e 70  tion==0 && db->p
30233 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
30234 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 54 6f  ==0 ){.    addTo
30235 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b  BlockedList(db);
30236 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 42 6c 6f 63  .  }.  db->pBloc
30237 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  kingConnection =
30238 20 70 42 6c 6f 63 6b 65 72 3b 0a 20 20 6c 65 61   pBlocker;.  lea
30239 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a  veMutex();.}../*
3023a 0a 2a 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74  .** The transact
3023b 69 6f 6e 20 6f 70 65 6e 65 64 20 62 79 20 64 61  ion opened by da
3023c 74 61 62 61 73 65 20 64 62 20 68 61 73 20 6a 75  tabase db has ju
3023d 73 74 20 66 69 6e 69 73 68 65 64 2e 20 4c 6f 63  st finished. Loc
3023e 6b 73 20 68 65 6c 64 20 0a 2a 2a 20 62 79 20 64  ks held .** by d
3023f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
30240 6f 6e 20 64 62 20 68 61 76 65 20 62 65 65 6e 20  on db have been 
30241 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  released..**.** 
30242 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6c 6f  This function lo
30243 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 68  ops through each
30244 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62 6c   entry in the bl
30245 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
30246 73 0a 2a 2a 20 6c 69 73 74 20 61 6e 64 20 64 6f  s.** list and do
30247 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
30248 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66 20  :.**.**   1) If 
30249 74 68 65 20 73 71 6c 69 74 65 33 2e 70 42 6c 6f  the sqlite3.pBlo
3024a 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20  ckingConnection 
3024b 6d 65 6d 62 65 72 20 6f 66 20 61 20 6c 69 73 74  member of a list
3024c 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 20 20 20   entry is.**    
3024d 20 20 73 65 74 20 74 6f 20 64 62 2c 20 74 68 65    set to db, the
3024e 6e 20 73 65 74 20 70 42 6c 6f 63 6b 69 6e 67 43  n set pBlockingC
3024f 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a  onnection=0..**.
30250 2a 2a 20 20 20 32 29 20 49 66 20 74 68 65 20 73  **   2) If the s
30251 71 6c 69 74 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f  qlite3.pUnlockCo
30252 6e 6e 65 63 74 69 6f 6e 20 6d 65 6d 62 65 72 20  nnection member 
30253 6f 66 20 61 20 6c 69 73 74 20 65 6e 74 72 79 20  of a list entry 
30254 69 73 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 74  is.**      set t
30255 6f 20 64 62 2c 20 74 68 65 6e 20 69 6e 76 6f 6b  o db, then invok
30256 65 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65 64  e the configured
30257 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
30258 61 6c 6c 62 61 63 6b 20 61 6e 64 0a 2a 2a 20 20  allback and.**  
30259 20 20 20 20 73 65 74 20 70 55 6e 6c 6f 63 6b 43      set pUnlockC
3025a 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a  onnection=0..**.
3025b 2a 2a 20 20 20 33 29 20 49 66 20 74 68 65 20 74  **   3) If the t
3025c 77 6f 20 73 74 65 70 73 20 61 62 6f 76 65 20 6d  wo steps above m
3025d 65 61 6e 20 74 68 61 74 20 70 42 6c 6f 63 6b 69  ean that pBlocki
3025e 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20  ngConnection==0 
3025f 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 55 6e 6c  and.**      pUnl
30260 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30  ockConnection==0
30261 2c 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74  , remove the ent
30262 72 79 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63  ry from the bloc
30263 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a  ked connections.
30264 2a 2a 20 20 20 20 20 20 6c 69 73 74 2e 0a 2a 2f  **      list..*/
30265 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
30266 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e  void sqlite3Conn
30267 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73  ectionUnlocked(s
30268 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 76  qlite3 *db){.  v
30269 6f 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74  oid (*xUnlockNot
3026a 69 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e  ify)(void **, in
3026b 74 29 20 3d 20 30 3b 20 2f 2a 20 55 6e 6c 6f 63  t) = 0; /* Unloc
3026c 6b 2d 6e 6f 74 69 66 79 20 63 62 20 74 6f 20 69  k-notify cb to i
3026d 6e 76 6f 6b 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  nvoke */.  int n
3026e 41 72 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Arg = 0;        
3026f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30270 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
30271 20 65 6e 74 72 69 65 73 20 69 6e 20 61 41 72 67   entries in aArg
30272 5b 5d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  [] */.  sqlite3 
30273 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 20  **pp;           
30274 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30275 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
30276 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 76 6f 69 64  iable */..  void
30277 20 2a 61 53 74 61 74 69 63 5b 31 36 5d 3b 0a 20   *aStatic[16];. 
30278 20 76 6f 69 64 20 2a 2a 61 41 72 67 20 3d 20 61   void **aArg = a
30279 53 74 61 74 69 63 3b 0a 20 20 76 6f 69 64 20 2a  Static;.  void *
3027a 2a 61 44 79 6e 20 3d 20 30 3b 0a 0a 20 20 65 6e  *aDyn = 0;..  en
3027b 74 65 72 4d 75 74 65 78 28 29 3b 20 20 20 20 20  terMutex();     
3027c 20 20 20 20 2f 2a 20 45 6e 74 65 72 20 53 54 41      /* Enter STA
3027d 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
3027e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c   */..  /* This l
3027f 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f  oop runs once fo
30280 72 20 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20  r each entry in 
30281 74 68 65 20 62 6c 6f 63 6b 65 64 2d 63 6f 6e 6e  the blocked-conn
30282 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f  ections list. */
30283 0a 20 20 66 6f 72 28 70 70 3d 26 73 71 6c 69 74  .  for(pp=&sqlit
30284 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 2a  e3BlockedList; *
30285 70 70 3b 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 20  pp; /* no-op */ 
30286 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
30287 70 20 3d 20 2a 70 70 3b 0a 0a 20 20 20 20 2f 2a  p = *pp;..    /*
30288 20 53 74 65 70 20 31 2e 20 2a 2f 0a 20 20 20 20   Step 1. */.    
30289 69 66 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67  if( p->pBlocking
3028a 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29  Connection==db )
3028b 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 6c 6f 63  {.      p->pBloc
3028c 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  kingConnection =
3028d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
3028e 2a 20 53 74 65 70 20 32 2e 20 2a 2f 0a 20 20 20  * Step 2. */.   
3028f 20 69 66 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43   if( p->pUnlockC
30290 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29 7b  onnection==db ){
30291 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
30292 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  ->xUnlockNotify 
30293 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  );.      if( p->
30294 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 21 3d 78  xUnlockNotify!=x
30295 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 26 26 20  UnlockNotify && 
30296 6e 41 72 67 21 3d 30 20 29 7b 0a 20 20 20 20 20  nArg!=0 ){.     
30297 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79     xUnlockNotify
30298 28 61 41 72 67 2c 20 6e 41 72 67 29 3b 0a 20 20  (aArg, nArg);.  
30299 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a        nArg = 0;.
3029a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73        }..      s
3029b 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
3029c 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
3029d 20 61 73 73 65 72 74 28 20 61 41 72 67 3d 3d 61   assert( aArg==a
3029e 44 79 6e 20 7c 7c 20 28 61 44 79 6e 3d 3d 30 20  Dyn || (aDyn==0 
3029f 26 26 20 61 41 72 67 3d 3d 61 53 74 61 74 69 63  && aArg==aStatic
302a0 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ) );.      asser
302a1 74 28 20 6e 41 72 67 3c 3d 28 69 6e 74 29 41 72  t( nArg<=(int)Ar
302a2 72 61 79 53 69 7a 65 28 61 53 74 61 74 69 63 29  raySize(aStatic)
302a3 20 7c 7c 20 61 41 72 67 3d 3d 61 44 79 6e 20 29   || aArg==aDyn )
302a4 3b 0a 20 20 20 20 20 20 69 66 28 20 28 21 61 44  ;.      if( (!aD
302a5 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74  yn && nArg==(int
302a6 29 41 72 72 61 79 53 69 7a 65 28 61 53 74 61 74  )ArraySize(aStat
302a7 69 63 29 29 0a 20 20 20 20 20 20 20 7c 7c 20 28  ic)).       || (
302a8 61 44 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69  aDyn && nArg==(i
302a9 6e 74 29 28 73 71 6c 69 74 65 33 44 62 4d 61 6c  nt)(sqlite3DbMal
302aa 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 44 79 6e  locSize(db, aDyn
302ab 29 2f 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29  )/sizeof(void*))
302ac 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
302ad 20 20 20 2f 2a 20 54 68 65 20 61 41 72 67 5b 5d     /* The aArg[]
302ae 20 61 72 72 61 79 20 6e 65 65 64 73 20 74 6f 20   array needs to 
302af 67 72 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20  grow. */.       
302b0 20 76 6f 69 64 20 2a 2a 70 4e 65 77 20 3d 20 28   void **pNew = (
302b1 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 4d  void **)sqlite3M
302b2 61 6c 6c 6f 63 28 6e 41 72 67 2a 73 69 7a 65 6f  alloc(nArg*sizeo
302b3 66 28 76 6f 69 64 20 2a 29 2a 32 29 3b 0a 20 20  f(void *)*2);.  
302b4 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29        if( pNew )
302b5 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63  {.          memc
302b6 70 79 28 70 4e 65 77 2c 20 61 41 72 67 2c 20 6e  py(pNew, aArg, n
302b7 41 72 67 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20  Arg*sizeof(void 
302b8 2a 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  *));.          s
302b9 71 6c 69 74 65 33 5f 66 72 65 65 28 61 44 79 6e  qlite3_free(aDyn
302ba 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 44 79  );.          aDy
302bb 6e 20 3d 20 61 41 72 67 20 3d 20 70 4e 65 77 3b  n = aArg = pNew;
302bc 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
302bd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
302be 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68  s occurs when th
302bf 65 20 61 72 72 61 79 20 6f 66 20 63 6f 6e 74 65  e array of conte
302c0 78 74 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74  xt pointers that
302c1 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20   need to.       
302c2 20 20 20 2a 2a 20 62 65 20 70 61 73 73 65 64 20     ** be passed 
302c3 74 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  to the unlock-no
302c4 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  tify callback is
302c5 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
302c6 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 53  .          ** aS
302c7 74 61 74 69 63 5b 5d 20 61 72 72 61 79 20 61 6c  tatic[] array al
302c8 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73  located on the s
302c9 74 61 63 6b 20 61 6e 64 20 74 68 65 20 61 74 74  tack and the att
302ca 65 6d 70 74 20 74 6f 20 0a 20 20 20 20 20 20 20  empt to .       
302cb 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 61     ** allocate a
302cc 20 6c 61 72 67 65 72 20 61 72 72 61 79 20 66 72   larger array fr
302cd 6f 6d 20 74 68 65 20 68 65 61 70 20 68 61 73 20  om the heap has 
302ce 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 20 20  failed..        
302cf 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a    **.          *
302d0 2a 20 54 68 69 73 20 69 73 20 61 20 64 69 66 66  * This is a diff
302d1 69 63 75 6c 74 20 73 69 74 75 61 74 69 6f 6e 20  icult situation 
302d2 74 6f 20 68 61 6e 64 6c 65 2e 20 52 65 74 75 72  to handle. Retur
302d3 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 0a 20 20  ning an error.  
302d4 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 20          ** code 
302d5 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73  to the caller is
302d6 20 69 6e 73 75 66 66 69 63 69 65 6e 74 2c 20 61   insufficient, a
302d7 73 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72  s even if an err
302d8 6f 72 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20  or code.        
302d9 20 20 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64    ** is returned
302da 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
302db 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64   on connection d
302dc 62 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 0a  b will still be.
302dd 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6c 6f            ** clo
302de 73 65 64 20 61 6e 64 20 74 68 65 20 75 6e 6c 6f  sed and the unlo
302df 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
302e0 63 6b 73 20 6f 6e 20 62 6c 6f 63 6b 65 64 20 63  cks on blocked c
302e1 6f 6e 6e 65 63 74 69 6f 6e 73 0a 20 20 20 20 20  onnections.     
302e2 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 67 6f 20       ** will go 
302e3 75 6e 69 73 73 75 65 64 2e 20 54 68 69 73 20 6d  unissued. This m
302e4 69 67 68 74 20 63 61 75 73 65 20 74 68 65 20 61  ight cause the a
302e5 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 77 61  pplication to wa
302e6 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  it.          ** 
302e7 69 6e 64 65 66 69 6e 69 74 65 6c 79 20 66 6f 72  indefinitely for
302e8 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
302e9 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20  y callback that 
302ea 77 69 6c 6c 20 6e 65 76 65 72 20 0a 20 20 20 20  will never .    
302eb 20 20 20 20 20 20 2a 2a 20 61 72 72 69 76 65 2e        ** arrive.
302ec 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  .          **.  
302ed 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 73 74 65          ** Inste
302ee 61 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 75  ad, invoke the u
302ef 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
302f0 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 63  lback with the c
302f1 6f 6e 74 65 78 74 0a 20 20 20 20 20 20 20 20 20  ontext.         
302f2 20 2a 2a 20 61 72 72 61 79 20 61 6c 72 65 61 64   ** array alread
302f3 79 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 57  y accumulated. W
302f4 65 20 63 61 6e 20 74 68 65 6e 20 63 6c 65 61 72  e can then clear
302f5 20 74 68 65 20 61 72 72 61 79 20 61 6e 64 0a 20   the array and. 
302f6 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 67 69           ** begi
302f7 6e 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 61  n accumulating a
302f8 6e 79 20 66 75 72 74 68 65 72 20 63 6f 6e 74 65  ny further conte
302f9 78 74 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68  xt pointers with
302fa 6f 75 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a  out .          *
302fb 2a 20 72 65 71 75 69 72 69 6e 67 20 61 6e 79 20  * requiring any 
302fc 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69  dynamic allocati
302fd 6f 6e 2e 20 54 68 69 73 20 69 73 20 73 75 62 2d  on. This is sub-
302fe 6f 70 74 69 6d 61 6c 20 62 65 63 61 75 73 65 0a  optimal because.
302ff 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20            ** it 
30300 6d 65 61 6e 73 20 74 68 61 74 20 69 6e 73 74 65  means that inste
30301 61 64 20 6f 66 20 6f 6e 65 20 63 61 6c 6c 62 61  ad of one callba
30302 63 6b 20 77 69 74 68 20 61 20 6c 61 72 67 65 20  ck with a large 
30303 61 72 72 61 79 20 6f 66 0a 20 20 20 20 20 20 20  array of.       
30304 20 20 20 2a 2a 20 63 6f 6e 74 65 78 74 20 70 6f     ** context po
30305 69 6e 74 65 72 73 20 74 68 65 20 61 70 70 6c 69  inters the appli
30306 63 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 63 65  cation will rece
30307 69 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a  ive two or more.
30308 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6c            ** cal
30309 6c 62 61 63 6b 73 20 77 69 74 68 20 73 6d 61 6c  lbacks with smal
3030a 6c 65 72 20 61 72 72 61 79 73 20 6f 66 20 63 6f  ler arrays of co
3030b 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 2c 20  ntext pointers, 
3030c 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 20  which will.     
3030d 20 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20 74       ** reduce t
3030e 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  he applications 
3030f 61 62 69 6c 69 74 79 20 74 6f 20 70 72 69 6f 72  ability to prior
30310 69 74 69 7a 65 20 6d 75 6c 74 69 70 6c 65 20 0a  itize multiple .
30311 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e            ** con
30312 6e 65 63 74 69 6f 6e 73 2e 20 42 75 74 20 69 74  nections. But it
30313 20 69 73 20 74 68 65 20 62 65 73 74 20 74 68 61   is the best tha
30314 74 20 63 61 6e 20 62 65 20 64 6f 6e 65 20 75 6e  t can be done un
30315 64 65 72 20 74 68 65 0a 20 20 20 20 20 20 20 20  der the.        
30316 20 20 2a 2a 20 63 69 72 63 75 6d 73 74 61 6e 63    ** circumstanc
30317 65 73 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  es..          */
30318 0a 20 20 20 20 20 20 20 20 20 20 78 55 6e 6c 6f  .          xUnlo
30319 63 6b 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e  ckNotify(aArg, n
3031a 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Arg);.          
3031b 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  nArg = 0;.      
3031c 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
3031d 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
3031e 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 20  gnMalloc();..   
3031f 20 20 20 61 41 72 67 5b 6e 41 72 67 2b 2b 5d 20     aArg[nArg++] 
30320 3d 20 70 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 3b  = p->pUnlockArg;
30321 0a 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f  .      xUnlockNo
30322 74 69 66 79 20 3d 20 70 2d 3e 78 55 6e 6c 6f 63  tify = p->xUnloc
30323 6b 4e 6f 74 69 66 79 3b 0a 20 20 20 20 20 20 70  kNotify;.      p
30324 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
30325 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ion = 0;.      p
30326 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  ->xUnlockNotify 
30327 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 55  = 0;.      p->pU
30328 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20  nlockArg = 0;.  
30329 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70    }..    /* Step
3032a 20 33 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70   3. */.    if( p
3032b 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
3032c 63 74 69 6f 6e 3d 3d 30 20 26 26 20 70 2d 3e 70  ction==0 && p->p
3032d 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
3032e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
3032f 52 65 6d 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f  Remove connectio
30330 6e 20 70 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f  n p from the blo
30331 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
30332 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20   list. */.      
30333 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 42 6c  *pp = p->pNextBl
30334 6f 63 6b 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e  ocked;.      p->
30335 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 30  pNextBlocked = 0
30336 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
30337 20 20 20 70 70 20 3d 20 26 70 2d 3e 70 4e 65 78     pp = &p->pNex
30338 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 7d 0a  tBlocked;.    }.
30339 20 20 7d 0a 0a 20 20 69 66 28 20 6e 41 72 67 21    }..  if( nArg!
3033a 3d 30 20 29 7b 0a 20 20 20 20 78 55 6e 6c 6f 63  =0 ){.    xUnloc
3033b 6b 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41  kNotify(aArg, nA
3033c 72 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  rg);.  }.  sqlit
3033d 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a 20  e3_free(aDyn);. 
3033e 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20   leaveMutex();  
3033f 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20         /* Leave 
30340 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75  STATIC_MASTER mu
30341 74 65 78 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex */.}../*.** 
30342 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 77  This is called w
30343 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
30344 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73   connection pass
30345 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
30346 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 63  t is .** being c
30347 6c 6f 73 65 64 2e 20 54 68 65 20 63 6f 6e 6e 65  losed. The conne
30348 63 74 69 6f 6e 20 69 73 20 72 65 6d 6f 76 65 64  ction is removed
30349 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65   from the blocke
3034a 64 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  d list..*/.SQLIT
3034b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
3034c 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
3034d 43 6c 6f 73 65 64 28 73 71 6c 69 74 65 33 20 2a  Closed(sqlite3 *
3034e 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 6f  db){.  sqlite3Co
3034f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64  nnectionUnlocked
30350 28 64 62 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74  (db);.  enterMut
30351 65 78 28 29 3b 0a 20 20 72 65 6d 6f 76 65 46 72  ex();.  removeFr
30352 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62  omBlockedList(db
30353 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74 50 72  );.  checkListPr
30354 6f 70 65 72 74 69 65 73 28 64 62 29 3b 0a 20 20  operties(db);.  
30355 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a  leaveMutex();.}.
30356 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
30357 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6e  ******* End of n
30358 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  otify.c ********
30359 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3035a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3035b 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
3035c 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
3035d 6c 65 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a  le fts3.c ******
3035e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3035f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30360 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
30361 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20  06 Oct 10.**.** 
30362 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
30363 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
30364 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
30365 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
30366 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
30367 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
30368 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
30369 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
3036a 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
3036b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
3036c 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
3036d 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
3036e 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
3036f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
30370 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
30371 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
30372 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
30373 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30374 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30375 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30376 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30377 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
30378 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 53  .** This is an S
30379 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70  QLite module imp
3037a 6c 65 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74  lementing full-t
3037b 65 78 74 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a  ext search..*/..
3037c 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
3037d 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
3037e 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
3037f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
30380 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
30381 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
30382 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
30383 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
30384 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
30385 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
30386 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
30387 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
30388 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
30389 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
3038a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
3038b 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
3038c 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
3038d 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
3038e 2f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  /../* TODO(shess
3038f 29 20 43 6f 6e 73 69 64 65 72 20 65 78 70 6f 72  ) Consider expor
30390 74 69 6e 67 20 74 68 69 73 20 63 6f 6d 6d 65 6e  ting this commen
30391 74 20 74 6f 20 61 6e 20 48 54 4d 4c 20 66 69 6c  t to an HTML fil
30392 65 20 6f 72 20 74 68 65 0a 2a 2a 20 77 69 6b 69  e or the.** wiki
30393 2e 0a 2a 2f 0a 2f 2a 20 54 68 65 20 66 75 6c 6c  ..*/./* The full
30394 2d 74 65 78 74 20 69 6e 64 65 78 20 69 73 20 73  -text index is s
30395 74 6f 72 65 64 20 69 6e 20 61 20 73 65 72 69 65  tored in a serie
30396 73 20 6f 66 20 62 2b 74 72 65 65 20 28 2d 6c 69  s of b+tree (-li
30397 6b 65 29 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ke).** structure
30398 73 20 63 61 6c 6c 65 64 20 73 65 67 6d 65 6e 74  s called segment
30399 73 20 77 68 69 63 68 20 6d 61 70 20 74 65 72 6d  s which map term
3039a 73 20 74 6f 20 64 6f 63 6c 69 73 74 73 2e 20 20  s to doclists.  
3039b 54 68 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  The.** structure
3039c 73 20 61 72 65 20 6c 69 6b 65 20 62 2b 74 72 65  s are like b+tre
3039d 65 73 20 69 6e 20 6c 61 79 6f 75 74 2c 20 62 75  es in layout, bu
3039e 74 20 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65  t are constructe
3039f 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f  d from the.** bo
303a0 74 74 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69 6d  ttom up in optim
303a1 61 6c 20 66 61 73 68 69 6f 6e 20 61 6e 64 20 61  al fashion and a
303a2 72 65 20 6e 6f 74 20 75 70 64 61 74 61 62 6c 65  re not updatable
303a3 2e 20 20 53 69 6e 63 65 20 74 72 65 65 73 0a 2a  .  Since trees.*
303a4 2a 20 61 72 65 20 62 75 69 6c 74 20 66 72 6f 6d  * are built from
303a5 20 74 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c 20   the bottom up, 
303a6 74 68 69 6e 67 73 20 77 69 6c 6c 20 62 65 20 64  things will be d
303a7 65 73 63 72 69 62 65 64 20 66 72 6f 6d 20 74 68  escribed from th
303a8 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e 0a  e.** bottom up..
303a9 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 56 61 72 69 6e  **.**.**** Varin
303aa 74 73 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 62  ts ****.** The b
303ab 61 73 69 63 20 75 6e 69 74 20 6f 66 20 65 6e 63  asic unit of enc
303ac 6f 64 69 6e 67 20 69 73 20 61 20 76 61 72 69 61  oding is a varia
303ad 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
303ae 65 72 20 63 61 6c 6c 65 64 20 61 0a 2a 2a 20 76  er called a.** v
303af 61 72 69 6e 74 2e 20 20 57 65 20 65 6e 63 6f 64  arint.  We encod
303b0 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  e variable-lengt
303b1 68 20 69 6e 74 65 67 65 72 73 20 69 6e 20 6c 69  h integers in li
303b2 74 74 6c 65 2d 65 6e 64 69 61 6e 20 6f 72 64 65  ttle-endian orde
303b3 72 0a 2a 2a 20 75 73 69 6e 67 20 73 65 76 65 6e  r.** using seven
303b4 20 62 69 74 73 20 2a 20 70 65 72 20 62 79 74 65   bits * per byte
303b5 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
303b6 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20  ** KEY:.**      
303b7 20 20 20 41 20 3d 20 30 78 78 78 78 78 78 78 20     A = 0xxxxxxx 
303b8 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74     7 bits of dat
303b9 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62  a and one flag b
303ba 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20  it.**         B 
303bb 3d 20 31 78 78 78 78 78 78 78 20 20 20 20 37 20  = 1xxxxxxx    7 
303bc 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
303bd 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
303be 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a  .**  7 bits - A.
303bf 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a  ** 14 bits - BA.
303c0 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42 41  ** 21 bits - BBA
303c1 0a 2a 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a  .** and so on..*
303c2 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 69 64 65  *.** This is ide
303c3 6e 74 69 63 61 6c 20 74 6f 20 68 6f 77 20 73 71  ntical to how sq
303c4 6c 69 74 65 20 65 6e 63 6f 64 65 73 20 76 61 72  lite encodes var
303c5 69 6e 74 73 20 28 73 65 65 20 75 74 69 6c 2e 63  ints (see util.c
303c6 29 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f  )..**.**.**** Do
303c7 63 75 6d 65 6e 74 20 6c 69 73 74 73 20 2a 2a 2a  cument lists ***
303c8 2a 0a 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20 28  *.** A doclist (
303c9 64 6f 63 75 6d 65 6e 74 20 6c 69 73 74 29 20 68  document list) h
303ca 6f 6c 64 73 20 61 20 64 6f 63 69 64 2d 73 6f 72  olds a docid-sor
303cb 74 65 64 20 6c 69 73 74 20 6f 66 20 68 69 74 73  ted list of hits
303cc 20 66 6f 72 20 61 0a 2a 2a 20 67 69 76 65 6e 20   for a.** given 
303cd 74 65 72 6d 2e 20 20 44 6f 63 6c 69 73 74 73 20  term.  Doclists 
303ce 68 6f 6c 64 20 64 6f 63 69 64 73 2c 20 61 6e 64  hold docids, and
303cf 20 63 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   can optionally 
303d0 61 73 73 6f 63 69 61 74 65 0a 2a 2a 20 74 6f 6b  associate.** tok
303d1 65 6e 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64  en positions and
303d2 20 6f 66 66 73 65 74 73 20 77 69 74 68 20 64 6f   offsets with do
303d3 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c  cids..**.** A DL
303d4 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45  _POSITIONS_OFFSE
303d5 54 53 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74  TS doclist is st
303d6 6f 72 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ored like this:.
303d7 2a 2a 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a  **.** array {.**
303d8 20 20 20 76 61 72 69 6e 74 20 64 6f 63 69 64 3b     varint docid;
303d9 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 20 20 20  .**   array {   
303da 20 20 20 20 20 20 20 20 20 20 20 20 20 28 70 6f               (po
303db 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20  sition list for 
303dc 63 6f 6c 75 6d 6e 20 30 29 0a 2a 2a 20 20 20 20  column 0).**    
303dd 20 76 61 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e   varint position
303de 3b 20 20 20 20 20 28 64 65 6c 74 61 20 66 72 6f  ;     (delta fro
303df 6d 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74  m previous posit
303e0 69 6f 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53  ion plus POS_BAS
303e1 45 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74  E).**     varint
303e2 20 73 74 61 72 74 4f 66 66 73 65 74 3b 20 20 28   startOffset;  (
303e3 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
303e4 6f 75 73 20 73 74 61 72 74 4f 66 66 73 65 74 29  ous startOffset)
303e5 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 65  .**     varint e
303e6 6e 64 4f 66 66 73 65 74 3b 20 20 20 20 28 64 65  ndOffset;    (de
303e7 6c 74 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66  lta from startOf
303e8 66 73 65 74 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20  fset).**   }.** 
303e9 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20    array {.**    
303ea 20 76 61 72 69 6e 74 20 50 4f 53 5f 43 4f 4c 55   varint POS_COLU
303eb 4d 4e 3b 20 20 20 28 6d 61 72 6b 73 20 73 74 61  MN;   (marks sta
303ec 72 74 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c  rt of position l
303ed 69 73 74 20 66 6f 72 20 6e 65 77 20 63 6f 6c 75  ist for new colu
303ee 6d 6e 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e  mn).**     varin
303ef 74 20 63 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  t column;       
303f0 28 69 6e 64 65 78 20 6f 66 20 6e 65 77 20 63 6f  (index of new co
303f1 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20 20 61 72 72  lumn).**     arr
303f2 61 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20 76 61  ay {.**       va
303f3 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20  rint position;  
303f4 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65   (delta from pre
303f5 76 69 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70  vious position p
303f6 6c 75 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a  lus POS_BASE).**
303f7 20 20 20 20 20 20 20 76 61 72 69 6e 74 20 73 74         varint st
303f8 61 72 74 4f 66 66 73 65 74 3b 28 64 65 6c 74 61  artOffset;(delta
303f9 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73   from previous s
303fa 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20  tartOffset).**  
303fb 20 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f       varint endO
303fc 66 66 73 65 74 3b 20 20 28 64 65 6c 74 61 20 66  ffset;  (delta f
303fd 72 6f 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29  rom startOffset)
303fe 0a 2a 2a 20 20 20 20 20 7d 0a 2a 2a 20 20 20 7d  .**     }.**   }
303ff 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 50 4f 53  .**   varint POS
30400 5f 45 4e 44 3b 20 20 20 20 20 20 20 20 28 6d 61  _END;        (ma
30401 72 6b 73 20 65 6e 64 20 6f 66 20 70 6f 73 69 74  rks end of posit
30402 69 6f 6e 73 20 66 6f 72 20 74 68 69 73 20 64 6f  ions for this do
30403 63 75 6d 65 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a  cument..** }.**.
30404 2a 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b  ** Here, array {
30405 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20   X } means zero 
30406 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e  or more occurren
30407 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65  ces of X, adjace
30408 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e  nt in.** memory.
30409 20 20 41 20 22 70 6f 73 69 74 69 6f 6e 22 20 69    A "position" i
3040a 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 61 20  s an index of a 
3040b 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f 6b  token in the tok
3040c 65 6e 20 73 74 72 65 61 6d 0a 2a 2a 20 67 65 6e  en stream.** gen
3040d 65 72 61 74 65 64 20 62 79 20 74 68 65 20 74 6f  erated by the to
3040e 6b 65 6e 69 7a 65 72 2c 20 77 68 69 6c 65 20 61  kenizer, while a
3040f 6e 20 22 6f 66 66 73 65 74 22 20 69 73 20 61 20  n "offset" is a 
30410 62 79 74 65 20 6f 66 66 73 65 74 2c 0a 2a 2a 20  byte offset,.** 
30411 62 6f 74 68 20 62 61 73 65 64 20 61 74 20 30 2e  both based at 0.
30412 20 20 4e 6f 74 65 20 74 68 61 74 20 50 4f 53 5f    Note that POS_
30413 45 4e 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c 55  END and POS_COLU
30414 4d 4e 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a  MN occur in the.
30415 2a 2a 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20  ** same logical 
30416 70 6c 61 63 65 20 61 73 20 74 68 65 20 70 6f 73  place as the pos
30417 69 74 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20 61  ition element, a
30418 6e 64 20 61 63 74 20 61 73 20 73 65 6e 74 69 6e  nd act as sentin
30419 61 6c 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61 20  als.** ending a 
3041a 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72  position list ar
3041b 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f  ray..**.** A DL_
3041c 50 4f 53 49 54 49 4f 4e 53 20 64 6f 63 6c 69 73  POSITIONS doclis
3041d 74 20 6f 6d 69 74 73 20 74 68 65 20 73 74 61 72  t omits the star
3041e 74 4f 66 66 73 65 74 20 61 6e 64 20 65 6e 64 4f  tOffset and endO
3041f 66 66 73 65 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ffset.** informa
30420 74 69 6f 6e 2e 20 20 41 20 44 4c 5f 44 4f 43 49  tion.  A DL_DOCI
30421 44 53 20 64 6f 63 6c 69 73 74 20 6f 6d 69 74 73  DS doclist omits
30422 20 62 6f 74 68 20 74 68 65 20 70 6f 73 69 74 69   both the positi
30423 6f 6e 20 61 6e 64 0a 2a 2a 20 6f 66 66 73 65 74  on and.** offset
30424 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62 65   information, be
30425 63 6f 6d 69 6e 67 20 61 6e 20 61 72 72 61 79 20  coming an array 
30426 6f 66 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65  of varint-encode
30427 64 20 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20  d docids..**.** 
30428 4f 6e 2d 64 69 73 6b 20 64 61 74 61 20 69 73 20  On-disk data is 
30429 73 74 6f 72 65 64 20 61 73 20 74 79 70 65 20 44  stored as type D
3042a 4c 5f 44 45 46 41 55 4c 54 2c 20 73 6f 20 77 65  L_DEFAULT, so we
3042b 20 64 6f 6e 27 74 20 73 65 72 69 61 6c 69 7a 65   don't serialize
3042c 0a 2a 2a 20 74 68 65 20 74 79 70 65 2e 20 20 44  .** the type.  D
3042d 75 65 20 74 6f 20 68 6f 77 20 64 65 6c 65 74 69  ue to how deleti
3042e 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  on is implemente
3042f 64 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  d in the segment
30430 61 74 69 6f 6e 0a 2a 2a 20 73 79 73 74 65 6d 2c  ation.** system,
30431 20 6f 6e 2d 64 69 73 6b 20 64 6f 63 6c 69 73 74   on-disk doclist
30432 73 20 4d 55 53 54 20 73 74 6f 72 65 20 61 74 20  s MUST store at 
30433 6c 65 61 73 74 20 70 6f 73 69 74 69 6f 6e 73 2e  least positions.
30434 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d  .**.**.**** Segm
30435 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 2a  ent leaf nodes *
30436 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c  ***.** Segment l
30437 65 61 66 20 6e 6f 64 65 73 20 73 74 6f 72 65 20  eaf nodes store 
30438 74 65 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73  terms and doclis
30439 74 73 2c 20 6f 72 64 65 72 65 64 20 62 79 20 74  ts, ordered by t
3043a 65 72 6d 2e 20 20 4c 65 61 66 0a 2a 2a 20 6e 6f  erm.  Leaf.** no
3043b 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20  des are written 
3043c 75 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72  using LeafWriter
3043d 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67  , and read using
3043e 20 4c 65 61 66 52 65 61 64 65 72 20 28 74 6f 0a   LeafReader (to.
3043f 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f 75  ** iterate throu
30440 67 68 20 61 20 73 69 6e 67 6c 65 20 6c 65 61 66  gh a single leaf
30441 20 6e 6f 64 65 27 73 20 64 61 74 61 29 20 61 6e   node's data) an
30442 64 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 28  d LeavesReader (
30443 74 6f 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68  to.** iterate th
30444 72 6f 75 67 68 20 61 20 73 65 67 6d 65 6e 74 27  rough a segment'
30445 73 20 65 6e 74 69 72 65 20 6c 65 61 66 20 6c 61  s entire leaf la
30446 79 65 72 29 2e 20 20 4c 65 61 66 20 6e 6f 64 65  yer).  Leaf node
30447 73 20 68 61 76 65 0a 2a 2a 20 74 68 65 20 66 6f  s have.** the fo
30448 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69  rmat:.**.** vari
30449 6e 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20  nt iHeight;     
3044a 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74 20          (height 
3044b 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c  from leaf level,
3044c 20 61 6c 77 61 79 73 20 30 29 0a 2a 2a 20 76 61   always 0).** va
3044d 72 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20  rint nTerm;     
3044e 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
3044f 68 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29  h of first term)
30450 0a 2a 2a 20 63 68 61 72 20 70 54 65 72 6d 5b 6e  .** char pTerm[n
30451 54 65 72 6d 5d 3b 20 20 20 20 20 20 20 20 20 20  Term];          
30452 28 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73  (content of firs
30453 74 20 74 65 72 6d 29 0a 2a 2a 20 76 61 72 69 6e  t term).** varin
30454 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20  t nDoclist;     
30455 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f         (length o
30456 66 20 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61  f term's associa
30457 74 65 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20  ted doclist).** 
30458 63 68 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44  char pDoclist[nD
30459 6f 63 6c 69 73 74 5d 3b 20 20 20 20 28 63 6f 6e  oclist];    (con
3045a 74 65 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29  tent of doclist)
3045b 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20  .** array {.**  
3045c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3045d 20 20 20 20 20 20 20 20 20 20 20 28 66 75 72 74             (furt
3045e 68 65 72 20 74 65 72 6d 73 20 61 72 65 20 64 65  her terms are de
3045f 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a 20  lta-encoded).** 
30460 20 20 76 61 72 69 6e 74 20 6e 50 72 65 66 69 78    varint nPrefix
30461 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e  ;           (len
30462 67 74 68 20 6f 66 20 70 72 65 66 69 78 20 73 68  gth of prefix sh
30463 61 72 65 64 20 77 69 74 68 20 70 72 65 76 69 6f  ared with previo
30464 75 73 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61  us term).**   va
30465 72 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20  rint nSuffix;   
30466 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
30467 6f 66 20 75 6e 73 68 61 72 65 64 20 73 75 66 66  of unshared suff
30468 69 78 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54  ix).**   char pT
30469 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69  ermSuffix[nSuffi
3046a 78 5d 3b 28 75 6e 73 68 61 72 65 64 20 73 75 66  x];(unshared suf
3046b 66 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d  fix of next term
3046c 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 44  ).**   varint nD
3046d 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20  oclist;         
3046e 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d   (length of term
3046f 27 73 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f  's associated do
30470 63 6c 69 73 74 29 0a 2a 2a 20 20 20 63 68 61 72  clist).**   char
30471 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69   pDoclist[nDocli
30472 73 74 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20 6f  st];  (content o
30473 66 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 7d 0a  f doclist).** }.
30474 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72 61  **.** Here, arra
30475 79 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65  y { X } means ze
30476 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72  ro or more occur
30477 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a  rences of X, adj
30478 61 63 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f  acent in.** memo
30479 72 79 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 20 6e  ry..**.** Leaf n
3047a 6f 64 65 73 20 61 72 65 20 62 72 6f 6b 65 6e 20  odes are broken 
3047b 69 6e 74 6f 20 62 6c 6f 63 6b 73 20 77 68 69 63  into blocks whic
3047c 68 20 61 72 65 20 73 74 6f 72 65 64 20 63 6f 6e  h are stored con
3047d 74 69 67 75 6f 75 73 6c 79 20 69 6e 0a 2a 2a 20  tiguously in.** 
3047e 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74  the %_segments t
3047f 61 62 6c 65 20 69 6e 20 73 6f 72 74 65 64 20 6f  able in sorted o
30480 72 64 65 72 2e 20 20 54 68 69 73 20 6d 65 61 6e  rder.  This mean
30481 73 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20  s that when the 
30482 65 6e 64 0a 2a 2a 20 6f 66 20 61 20 6e 6f 64 65  end.** of a node
30483 20 69 73 20 72 65 61 63 68 65 64 2c 20 74 68 65   is reached, the
30484 20 6e 65 78 74 20 74 65 72 6d 20 69 73 20 69 6e   next term is in
30485 20 74 68 65 20 6e 6f 64 65 20 77 69 74 68 20 74   the node with t
30486 68 65 20 6e 65 78 74 0a 2a 2a 20 67 72 65 61 74  he next.** great
30487 65 72 20 6e 6f 64 65 20 69 64 2e 0a 2a 2a 0a 2a  er node id..**.*
30488 2a 20 4e 65 77 20 64 61 74 61 20 69 73 20 73 70  * New data is sp
30489 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 6c  illed to a new l
3048a 65 61 66 20 6e 6f 64 65 20 77 68 65 6e 20 74 68  eaf node when th
3048b 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 0a 2a  e current node.*
3048c 2a 20 65 78 63 65 65 64 73 20 4c 45 41 46 5f 4d  * exceeds LEAF_M
3048d 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75 6c  AX bytes (defaul
3048e 74 20 32 30 34 38 29 2e 20 20 4e 65 77 20 64 61  t 2048).  New da
3048f 74 61 20 77 68 69 63 68 20 69 74 73 65 6c 66 20  ta which itself 
30490 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  is.** larger tha
30491 6e 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e  n STANDALONE_MIN
30492 20 28 64 65 66 61 75 6c 74 20 31 30 32 34 29 20   (default 1024) 
30493 69 73 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73  is placed in a s
30494 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64  tandalone.** nod
30495 65 20 28 61 20 6c 65 61 66 20 6e 6f 64 65 20 77  e (a leaf node w
30496 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 65 72  ith a single ter
30497 6d 20 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e 20  m and doclist). 
30498 20 54 68 65 20 67 6f 61 6c 20 6f 66 0a 2a 2a 20   The goal of.** 
30499 74 68 65 73 65 20 73 65 74 74 69 6e 67 73 20 69  these settings i
3049a 73 20 74 6f 20 70 61 63 6b 20 74 6f 67 65 74 68  s to pack togeth
3049b 65 72 20 67 72 6f 75 70 73 20 6f 66 20 73 6d 61  er groups of sma
3049c 6c 6c 20 64 6f 63 6c 69 73 74 73 20 77 68 69 6c  ll doclists whil
3049d 65 0a 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20 65  e.** making it e
3049e 66 66 69 63 69 65 6e 74 20 74 6f 20 64 69 72 65  fficient to dire
3049f 63 74 6c 79 20 61 63 63 65 73 73 20 6c 61 72 67  ctly access larg
304a0 65 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65  e doclists.  The
304a1 0a 2a 2a 20 61 73 73 75 6d 70 74 69 6f 6e 20 69  .** assumption i
304a2 73 20 74 68 61 74 20 6c 61 72 67 65 20 64 6f 63  s that large doc
304a3 6c 69 73 74 73 20 72 65 70 72 65 73 65 6e 74 20  lists represent 
304a4 74 65 72 6d 73 20 77 68 69 63 68 20 61 72 65 20  terms which are 
304a5 6d 6f 72 65 0a 2a 2a 20 6c 69 6b 65 6c 79 20 74  more.** likely t
304a6 6f 20 62 65 20 71 75 65 72 79 20 74 61 72 67 65  o be query targe
304a7 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73  ts..**.** TODO(s
304a8 68 65 73 73 29 20 49 74 20 6d 61 79 20 62 65 20  hess) It may be 
304a9 75 73 65 66 75 6c 20 66 6f 72 20 62 6c 6f 63 6b  useful for block
304aa 69 6e 67 20 64 65 63 69 73 69 6f 6e 73 20 74 6f  ing decisions to
304ab 20 62 65 20 6d 6f 72 65 0a 2a 2a 20 64 79 6e 61   be more.** dyna
304ac 6d 69 63 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e  mic.  For instan
304ad 63 65 2c 20 69 74 20 6d 61 79 20 6d 61 6b 65 20  ce, it may make 
304ae 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 68 61  more sense to ha
304af 76 65 20 61 20 32 2e 35 6b 20 6c 65 61 66 0a 2a  ve a 2.5k leaf.*
304b0 2a 20 6e 6f 64 65 20 72 61 74 68 65 72 20 74 68  * node rather th
304b1 61 6e 20 73 70 6c 69 74 74 69 6e 67 20 69 6e 74  an splitting int
304b2 6f 20 32 6b 20 61 6e 64 20 2e 35 6b 20 6e 6f 64  o 2k and .5k nod
304b3 65 73 2e 20 20 4d 79 20 69 6e 74 75 69 74 69 6f  es.  My intuitio
304b4 6e 20 69 73 0a 2a 2a 20 74 68 61 74 20 74 68 69  n is.** that thi
304b5 73 20 6d 69 67 68 74 20 65 78 74 65 6e 64 20 74  s might extend t
304b6 68 72 6f 75 67 68 20 32 78 20 6f 72 20 34 78 20  hrough 2x or 4x 
304b7 74 68 65 20 70 61 67 65 73 69 7a 65 2e 0a 2a 2a  the pagesize..**
304b8 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74  .**.**** Segment
304b9 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
304ba 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20  ****.** Segment 
304bb 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 73  interior nodes s
304bc 74 6f 72 65 20 62 6c 6f 63 6b 69 64 73 20 66 6f  tore blockids fo
304bd 72 20 73 75 62 74 72 65 65 20 6e 6f 64 65 73 20  r subtree nodes 
304be 61 6e 64 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20  and terms.** to 
304bf 64 65 73 63 72 69 62 65 20 77 68 61 74 20 64 61  describe what da
304c0 74 61 20 69 73 20 73 74 6f 72 65 64 20 62 79 20  ta is stored by 
304c1 74 68 65 20 65 61 63 68 20 73 75 62 74 72 65 65  the each subtree
304c2 2e 20 20 49 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e  .  Interior.** n
304c3 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e  odes are written
304c4 20 75 73 69 6e 67 20 49 6e 74 65 72 69 6f 72 57   using InteriorW
304c5 72 69 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20  riter, and read 
304c6 75 73 69 6e 67 0a 2a 2a 20 49 6e 74 65 72 69 6f  using.** Interio
304c7 72 52 65 61 64 65 72 2e 20 20 49 6e 74 65 72 69  rReader.  Interi
304c8 6f 72 57 72 69 74 65 72 73 20 61 72 65 20 63 72  orWriters are cr
304c9 65 61 74 65 64 20 61 73 20 6e 65 65 64 65 64 20  eated as needed 
304ca 77 68 65 6e 0a 2a 2a 20 53 65 67 6d 65 6e 74 57  when.** SegmentW
304cb 72 69 74 65 72 20 63 72 65 61 74 65 73 20 6e 65  riter creates ne
304cc 77 20 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f 72  w leaf nodes, or
304cd 20 77 68 65 6e 20 61 6e 20 69 6e 74 65 72 69 6f   when an interio
304ce 72 20 6e 6f 64 65 0a 2a 2a 20 69 74 73 65 6c 66  r node.** itself
304cf 20 67 72 6f 77 73 20 74 6f 6f 20 62 69 67 20 61   grows too big a
304d0 6e 64 20 6d 75 73 74 20 62 65 20 73 70 6c 69 74  nd must be split
304d1 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66  .  The format of
304d2 20 69 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64   interior.** nod
304d3 65 73 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74  es:.**.** varint
304d4 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20   iHeight;       
304d5 20 20 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d      (height from
304d6 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77   leaf level, alw
304d7 61 79 73 20 3e 30 29 0a 2a 2a 20 76 61 72 69 6e  ays >0).** varin
304d8 74 20 69 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20  t iBlockid;     
304d9 20 20 20 20 20 28 62 6c 6f 63 6b 20 69 64 20 6f       (block id o
304da 66 20 6e 6f 64 65 27 73 20 6c 65 66 74 6d 6f 73  f node's leftmos
304db 74 20 73 75 62 74 72 65 65 29 0a 2a 2a 20 6f 70  t subtree).** op
304dc 74 69 6f 6e 61 6c 20 7b 0a 2a 2a 20 20 20 76 61  tional {.**   va
304dd 72 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20  rint nTerm;     
304de 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
304df 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
304e0 20 20 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65    char pTerm[nTe
304e1 72 6d 5d 3b 20 20 20 20 20 20 28 63 6f 6e 74 65  rm];      (conte
304e2 6e 74 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  nt of first term
304e3 29 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a  ).**   array {.*
304e4 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
304e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304e6 20 28 66 75 72 74 68 65 72 20 74 65 72 6d 73 20   (further terms 
304e7 61 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65  are delta-encode
304e8 64 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74  d).**     varint
304e9 20 6e 50 72 65 66 69 78 3b 20 20 20 20 20 20 20   nPrefix;       
304ea 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
304eb 73 68 61 72 65 64 20 70 72 65 66 69 78 20 77 69  shared prefix wi
304ec 74 68 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d  th previous term
304ed 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20  ).**     varint 
304ee 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20  nSuffix;        
304ef 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75      (length of u
304f0 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 29 0a  nshared suffix).
304f1 2a 2a 20 20 20 20 20 63 68 61 72 20 70 54 65 72  **     char pTer
304f2 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d  mSuffix[nSuffix]
304f3 3b 20 28 75 6e 73 68 61 72 65 64 20 73 75 66 66  ; (unshared suff
304f4 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29  ix of next term)
304f5 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a  .**   }.** }.**.
304f6 2a 2a 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e 61  ** Here, optiona
304f7 6c 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61 6e  l { X } means an
304f8 20 6f 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65 6e   optional elemen
304f9 74 2c 20 77 68 69 6c 65 20 61 72 72 61 79 20 7b  t, while array {
304fa 20 58 20 7d 0a 2a 2a 20 6d 65 61 6e 73 20 7a 65   X }.** means ze
304fb 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72  ro or more occur
304fc 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a  rences of X, adj
304fd 61 63 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 79 2e  acent in memory.
304fe 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 72 69  .**.** An interi
304ff 6f 72 20 6e 6f 64 65 20 65 6e 63 6f 64 65 73 20  or node encodes 
30500 6e 20 74 65 72 6d 73 20 73 65 70 61 72 61 74 69  n terms separati
30501 6e 67 20 6e 2b 31 20 73 75 62 74 72 65 65 73 2e  ng n+1 subtrees.
30502 20 20 54 68 65 0a 2a 2a 20 73 75 62 74 72 65 65    The.** subtree
30503 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 6e 74   blocks are cont
30504 69 67 75 6f 75 73 2c 20 73 6f 20 6f 6e 6c 79 20  iguous, so only 
30505 74 68 65 20 66 69 72 73 74 20 73 75 62 74 72 65  the first subtre
30506 65 27 73 20 62 6c 6f 63 6b 69 64 0a 2a 2a 20 69  e's blockid.** i
30507 73 20 65 6e 63 6f 64 65 64 2e 20 20 54 68 65 20  s encoded.  The 
30508 73 75 62 74 72 65 65 20 61 74 20 69 42 6c 6f 63  subtree at iBloc
30509 6b 69 64 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e  kid will contain
3050a 20 61 6c 6c 20 74 65 72 6d 73 20 6c 65 73 73 0a   all terms less.
3050b 2a 2a 20 74 68 61 6e 20 74 68 65 20 66 69 72 73  ** than the firs
3050c 74 20 74 65 72 6d 20 65 6e 63 6f 64 65 64 20 28  t term encoded (
3050d 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20 69 66 20  or all terms if 
3050e 6e 6f 20 74 65 72 6d 20 69 73 20 65 6e 63 6f 64  no term is encod
3050f 65 64 29 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ed)..** Otherwis
30510 65 2c 20 66 6f 72 20 74 65 72 6d 73 20 67 72 65  e, for terms gre
30511 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
30512 61 6c 20 74 6f 20 70 54 65 72 6d 5b 69 5d 20 62  al to pTerm[i] b
30513 75 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ut less.** than 
30514 70 54 65 72 6d 5b 69 2b 31 5d 2c 20 74 68 65 20  pTerm[i+1], the 
30515 73 75 62 74 72 65 65 20 66 6f 72 20 74 68 61 74  subtree for that
30516 20 74 65 72 6d 20 77 69 6c 6c 20 62 65 20 72 6f   term will be ro
30517 6f 74 65 64 20 61 74 0a 2a 2a 20 69 42 6c 6f 63  oted at.** iBloc
30518 6b 69 64 2b 69 2e 20 20 49 6e 74 65 72 69 6f 72  kid+i.  Interior
30519 20 6e 6f 64 65 73 20 6f 6e 6c 79 20 73 74 6f 72   nodes only stor
3051a 65 20 65 6e 6f 75 67 68 20 74 65 72 6d 20 64 61  e enough term da
3051b 74 61 20 74 6f 0a 2a 2a 20 64 69 73 74 69 6e 67  ta to.** disting
3051c 75 69 73 68 20 61 64 6a 61 63 65 6e 74 20 63 68  uish adjacent ch
3051d 69 6c 64 72 65 6e 20 28 69 66 20 74 68 65 20 72  ildren (if the r
3051e 69 67 68 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66  ightmost term of
3051f 20 74 68 65 20 6c 65 66 74 0a 2a 2a 20 63 68 69   the left.** chi
30520 6c 64 20 69 73 20 22 73 6f 6d 65 74 68 69 6e 67  ld is "something
30521 22 2c 20 61 6e 64 20 74 68 65 20 6c 65 66 74 6d  ", and the leftm
30522 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ost term of the 
30523 72 69 67 68 74 20 63 68 69 6c 64 20 69 73 0a 2a  right child is.*
30524 2a 20 22 77 69 63 6b 65 64 22 2c 20 6f 6e 6c 79  * "wicked", only
30525 20 22 77 22 20 69 73 20 73 74 6f 72 65 64 29 2e   "w" is stored).
30526 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20  .**.** New data 
30527 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20  is spilled to a 
30528 6e 65 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  new interior nod
30529 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 68 65  e at the same he
3052a 69 67 68 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65  ight when.** the
3052b 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 78   current node ex
3052c 63 65 65 64 73 20 49 4e 54 45 52 49 4f 52 5f 4d  ceeds INTERIOR_M
3052d 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75 6c  AX bytes (defaul
3052e 74 20 32 30 34 38 29 2e 0a 2a 2a 20 49 4e 54 45  t 2048)..** INTE
3052f 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 28  RIOR_MIN_TERMS (
30530 64 65 66 61 75 6c 74 20 37 29 20 6b 65 65 70 73  default 7) keeps
30531 20 6c 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f   large terms fro
30532 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0a 2a  m monopolizing.*
30533 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  * interior nodes
30534 20 61 6e 64 20 6d 61 6b 69 6e 67 20 74 68 65 20   and making the 
30535 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79 2e  tree too skinny.
30536 20 20 54 68 65 20 69 6e 74 65 72 69 6f 72 20 6e    The interior n
30537 6f 64 65 73 0a 2a 2a 20 61 74 20 61 20 67 69 76  odes.** at a giv
30538 65 6e 20 68 65 69 67 68 74 20 61 72 65 20 6e 61  en height are na
30539 74 75 72 61 6c 6c 79 20 74 72 61 63 6b 65 64 20  turally tracked 
3053a 62 79 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  by interior node
3053b 73 20 61 74 0a 2a 2a 20 68 65 69 67 68 74 2b 31  s at.** height+1
3053c 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  , and so on..**.
3053d 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20  **.**** Segment 
3053e 64 69 72 65 63 74 6f 72 79 20 2a 2a 2a 2a 0a 2a  directory ****.*
3053f 2a 20 54 68 65 20 73 65 67 6d 65 6e 74 20 64 69  * The segment di
30540 72 65 63 74 6f 72 79 20 69 6e 20 74 61 62 6c 65  rectory in table
30541 20 25 5f 73 65 67 64 69 72 20 73 74 6f 72 65 73   %_segdir stores
30542 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f   meta-informatio
30543 6e 20 66 6f 72 0a 2a 2a 20 6d 65 72 67 69 6e 67  n for.** merging
30544 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20 73 65   and deleting se
30545 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f  gments, and also
30546 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f   the root node o
30547 66 20 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74  f the.** segment
30548 27 73 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54  's tree..**.** T
30549 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20  he root node is 
3054a 74 68 65 20 74 6f 70 20 6e 6f 64 65 20 6f 66 20  the top node of 
3054b 74 68 65 20 73 65 67 6d 65 6e 74 27 73 20 74 72  the segment's tr
3054c 65 65 20 61 66 74 65 72 20 65 6e 63 6f 64 69 6e  ee after encodin
3054d 67 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20  g.** the entire 
3054e 73 65 67 6d 65 6e 74 2c 20 72 65 73 74 72 69 63  segment, restric
3054f 74 65 64 20 74 6f 20 52 4f 4f 54 5f 4d 41 58 20  ted to ROOT_MAX 
30550 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20 31  bytes (default 1
30551 30 32 34 29 2e 0a 2a 2a 20 54 68 69 73 20 63 6f  024)..** This co
30552 75 6c 64 20 62 65 20 65 69 74 68 65 72 20 61 20  uld be either a 
30553 6c 65 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e 20  leaf node or an 
30554 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20  interior node.  
30555 49 66 20 74 68 65 20 74 6f 70 0a 2a 2a 20 6e 6f  If the top.** no
30556 64 65 20 72 65 71 75 69 72 65 73 20 6d 6f 72 65  de requires more
30557 20 74 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20 62   than ROOT_MAX b
30558 79 74 65 73 2c 20 69 74 20 69 73 20 66 6c 75 73  ytes, it is flus
30559 68 65 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74  hed to %_segment
3055a 73 0a 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20 72  s.** and a new r
3055b 6f 6f 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  oot interior nod
3055c 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 28  e is generated (
3055d 77 68 69 63 68 20 73 68 6f 75 6c 64 20 61 6c 77  which should alw
3055e 61 79 73 20 66 69 74 0a 2a 2a 20 77 69 74 68 69  ays fit.** withi
3055f 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61 75  n ROOT_MAX becau
30560 73 65 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64 73  se it only needs
30561 20 73 70 61 63 65 20 66 6f 72 20 32 20 76 61 72   space for 2 var
30562 69 6e 74 73 2c 20 74 68 65 0a 2a 2a 20 68 65 69  ints, the.** hei
30563 67 68 74 20 61 6e 64 20 74 68 65 20 62 6c 6f 63  ght and the bloc
30564 6b 69 64 20 6f 66 20 74 68 65 20 70 72 65 76 69  kid of the previ
30565 6f 75 73 20 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a  ous root)..**.**
30566 20 54 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d   The meta-inform
30567 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 65 67  ation in the seg
30568 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69  ment directory i
30569 73 3a 0a 2a 2a 20 20 20 6c 65 76 65 6c 20 20 20  s:.**   level   
3056a 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 73 65              - se
3056b 67 6d 65 6e 74 20 6c 65 76 65 6c 20 28 73 65 65  gment level (see
3056c 20 62 65 6c 6f 77 29 0a 2a 2a 20 20 20 69 64 78   below).**   idx
3056d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3056e 20 2d 20 69 6e 64 65 78 20 77 69 74 68 69 6e 20   - index within 
3056f 6c 65 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20  level.**        
30570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
30571 20 28 6c 65 76 65 6c 2c 69 64 78 20 75 6e 69 71   (level,idx uniq
30572 75 65 6c 79 20 69 64 65 6e 74 69 66 79 20 61 20  uely identify a 
30573 73 65 67 6d 65 6e 74 29 0a 2a 2a 20 20 20 73 74  segment).**   st
30574 61 72 74 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20  art_block       
30575 20 20 2d 20 66 69 72 73 74 20 6c 65 61 66 20 6e    - first leaf n
30576 6f 64 65 0a 2a 2a 20 20 20 6c 65 61 76 65 73 5f  ode.**   leaves_
30577 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 2d 20 6c  end_block    - l
30578 61 73 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a 2a  ast leaf node.**
30579 20 20 20 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20     end_block    
3057a 20 20 20 20 20 20 20 2d 20 6c 61 73 74 20 62 6c         - last bl
3057b 6f 63 6b 20 28 69 6e 63 6c 75 64 69 6e 67 20 69  ock (including i
3057c 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 29 0a 2a  nterior nodes).*
3057d 2a 20 20 20 72 6f 6f 74 20 20 20 20 20 20 20 20  *   root        
3057e 20 20 20 20 20 20 20 20 2d 20 63 6f 6e 74 65 6e          - conten
3057f 74 73 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65 0a  ts of root node.
30580 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f  **.** If the roo
30581 74 20 6e 6f 64 65 20 69 73 20 61 20 6c 65 61 66  t node is a leaf
30582 20 6e 6f 64 65 2c 20 74 68 65 6e 20 73 74 61 72   node, then star
30583 74 5f 62 6c 6f 63 6b 2c 0a 2a 2a 20 6c 65 61 76  t_block,.** leav
30584 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e  es_end_block, an
30585 64 20 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65 20  d end_block are 
30586 61 6c 6c 20 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a  all 0..**.**.***
30587 2a 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 69 6e  * Segment mergin
30588 67 20 2a 2a 2a 2a 0a 2a 2a 20 54 6f 20 61 6d 6f  g ****.** To amo
30589 72 74 69 7a 65 20 75 70 64 61 74 65 20 63 6f 73  rtize update cos
3058a 74 73 2c 20 73 65 67 6d 65 6e 74 73 20 61 72 65  ts, segments are
3058b 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 6c 65   grouped into le
3058c 76 65 6c 73 20 61 6e 64 0a 2a 2a 20 6d 65 72 67  vels and.** merg
3058d 65 64 20 69 6e 20 62 61 74 63 68 65 73 2e 20 20  ed in batches.  
3058e 45 61 63 68 20 69 6e 63 72 65 61 73 65 20 69 6e  Each increase in
3058f 20 6c 65 76 65 6c 20 72 65 70 72 65 73 65 6e 74   level represent
30590 73 20 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79 0a  s exponentially.
30591 2a 2a 20 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74  ** more document
30592 73 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 6f 63  s..**.** New doc
30593 75 6d 65 6e 74 73 20 28 61 63 74 75 61 6c 6c 79  uments (actually
30594 2c 20 64 6f 63 75 6d 65 6e 74 20 75 70 64 61 74  , document updat
30595 65 73 29 20 61 72 65 20 74 6f 6b 65 6e 69 7a 65  es) are tokenize
30596 64 20 61 6e 64 0a 2a 2a 20 77 72 69 74 74 65 6e  d and.** written
30597 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 28 75   individually (u
30598 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72 29  sing LeafWriter)
30599 20 74 6f 20 61 20 6c 65 76 65 6c 20 30 20 73 65   to a level 0 se
3059a 67 6d 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 69  gment, with.** i
3059b 6e 63 72 65 6d 65 6e 74 69 6e 67 20 69 64 78 2e  ncrementing idx.
3059c 20 20 57 68 65 6e 20 69 64 78 20 72 65 61 63 68    When idx reach
3059d 65 73 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 28  es MERGE_COUNT (
3059e 64 65 66 61 75 6c 74 20 31 36 29 2c 20 61 6c 6c  default 16), all
3059f 0a 2a 2a 20 6c 65 76 65 6c 20 30 20 73 65 67 6d  .** level 0 segm
305a0 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65 64 20  ents are merged 
305a1 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65  into a single le
305a2 76 65 6c 20 31 20 73 65 67 6d 65 6e 74 2e 20 20  vel 1 segment.  
305a3 4c 65 76 65 6c 20 31 0a 2a 2a 20 69 73 20 70 6f  Level 1.** is po
305a4 70 75 6c 61 74 65 64 20 6c 69 6b 65 20 6c 65 76  pulated like lev
305a5 65 6c 20 30 2c 20 61 6e 64 20 65 76 65 6e 74 75  el 0, and eventu
305a6 61 6c 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54  ally MERGE_COUNT
305a7 20 6c 65 76 65 6c 20 31 0a 2a 2a 20 73 65 67 6d   level 1.** segm
305a8 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65 64 20  ents are merged 
305a9 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65  to a single leve
305aa 6c 20 32 20 73 65 67 6d 65 6e 74 20 28 72 65 70  l 2 segment (rep
305ab 72 65 73 65 6e 74 69 6e 67 0a 2a 2a 20 4d 45 52  resenting.** MER
305ac 47 45 5f 43 4f 55 4e 54 5e 32 20 75 70 64 61 74  GE_COUNT^2 updat
305ad 65 73 29 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a  es), and so on..
305ae 2a 2a 0a 2a 2a 20 41 20 73 65 67 6d 65 6e 74 20  **.** A segment 
305af 6d 65 72 67 65 20 74 72 61 76 65 72 73 65 73 20  merge traverses 
305b0 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20  all segments at 
305b1 61 20 67 69 76 65 6e 20 6c 65 76 65 6c 20 69 6e  a given level in
305b2 0a 2a 2a 20 70 61 72 61 6c 6c 65 6c 2c 20 70 65  .** parallel, pe
305b3 72 66 6f 72 6d 69 6e 67 20 61 20 73 74 72 61 69  rforming a strai
305b4 67 68 74 66 6f 72 77 61 72 64 20 73 6f 72 74 65  ghtforward sorte
305b5 64 20 6d 65 72 67 65 2e 20 20 53 69 6e 63 65 20  d merge.  Since 
305b6 73 65 67 6d 65 6e 74 0a 2a 2a 20 6c 65 61 66 20  segment.** leaf 
305b7 6e 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65  nodes are writte
305b8 6e 20 69 6e 20 74 6f 20 74 68 65 20 25 5f 73 65  n in to the %_se
305b9 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 6e 20  gments table in 
305ba 6f 72 64 65 72 2c 20 74 68 69 73 0a 2a 2a 20 6d  order, this.** m
305bb 65 72 67 65 20 74 72 61 76 65 72 73 65 73 20 74  erge traverses t
305bc 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 71  he underlying sq
305bd 6c 69 74 65 20 64 69 73 6b 20 73 74 72 75 63 74  lite disk struct
305be 75 72 65 73 20 65 66 66 69 63 69 65 6e 74 6c 79  ures efficiently
305bf 2e 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6d  ..** After the m
305c0 65 72 67 65 2c 20 61 6c 6c 20 73 65 67 6d 65 6e  erge, all segmen
305c1 74 20 62 6c 6f 63 6b 73 20 66 72 6f 6d 20 74 68  t blocks from th
305c2 65 20 6d 65 72 67 65 64 20 6c 65 76 65 6c 20 61  e merged level a
305c3 72 65 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a  re.** deleted..*
305c4 2a 0a 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54  *.** MERGE_COUNT
305c5 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f 66   controls how of
305c6 74 65 6e 20 77 65 20 6d 65 72 67 65 20 73 65 67  ten we merge seg
305c7 6d 65 6e 74 73 2e 20 20 31 36 20 73 65 65 6d 73  ments.  16 seems
305c8 20 74 6f 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68   to be.** somewh
305c9 61 74 20 6f 66 20 61 20 73 77 65 65 74 20 73 70  at of a sweet sp
305ca 6f 74 20 66 6f 72 20 69 6e 73 65 72 74 69 6f 6e  ot for insertion
305cb 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 33   performance.  3
305cc 32 20 61 6e 64 20 36 34 20 73 68 6f 77 0a 2a 2a  2 and 64 show.**
305cd 20 76 65 72 79 20 73 69 6d 69 6c 61 72 20 70 65   very similar pe
305ce 72 66 6f 72 6d 61 6e 63 65 20 6e 75 6d 62 65 72  rformance number
305cf 73 20 74 6f 20 31 36 20 6f 6e 20 69 6e 73 65 72  s to 16 on inser
305d0 74 69 6f 6e 2c 20 74 68 6f 75 67 68 20 74 68 65  tion, though the
305d1 79 27 72 65 0a 2a 2a 20 61 20 74 69 6e 79 20 62  y're.** a tiny b
305d2 69 74 20 73 6c 6f 77 65 72 20 28 70 65 72 68 61  it slower (perha
305d3 70 73 20 64 75 65 20 74 6f 20 6d 6f 72 65 20 6f  ps due to more o
305d4 76 65 72 68 65 61 64 20 69 6e 20 6d 65 72 67 65  verhead in merge
305d5 2d 74 69 6d 65 0a 2a 2a 20 73 6f 72 74 69 6e 67  -time.** sorting
305d6 29 2e 20 20 38 20 69 73 20 61 62 6f 75 74 20 32  ).  8 is about 2
305d7 30 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31  0% slower than 1
305d8 36 2c 20 34 20 61 62 6f 75 74 20 35 30 25 20 73  6, 4 about 50% s
305d9 6c 6f 77 65 72 20 74 68 61 6e 0a 2a 2a 20 31 36  lower than.** 16
305da 2c 20 32 20 61 62 6f 75 74 20 36 36 25 20 73 6c  , 2 about 66% sl
305db 6f 77 65 72 20 74 68 61 6e 20 31 36 2e 0a 2a 2a  ower than 16..**
305dc 0a 2a 2a 20 41 74 20 71 75 65 72 79 20 74 69 6d  .** At query tim
305dd 65 2c 20 68 69 67 68 20 4d 45 52 47 45 5f 43 4f  e, high MERGE_CO
305de 55 4e 54 20 69 6e 63 72 65 61 73 65 73 20 74 68  UNT increases th
305df 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d  e number of segm
305e0 65 6e 74 73 0a 2a 2a 20 77 68 69 63 68 20 6e 65  ents.** which ne
305e1 65 64 20 74 6f 20 62 65 20 73 63 61 6e 6e 65 64  ed to be scanned
305e2 20 61 6e 64 20 6d 65 72 67 65 64 2e 20 20 46 6f   and merged.  Fo
305e3 72 20 69 6e 73 74 61 6e 63 65 2c 20 77 69 74 68  r instance, with
305e4 20 31 30 30 6b 20 64 6f 63 73 0a 2a 2a 20 69 6e   100k docs.** in
305e5 73 65 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  serted:.**.**   
305e6 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 20 20 73   MERGE_COUNT   s
305e7 65 67 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20  egments.**      
305e8 20 31 36 20 20 20 20 20 20 20 20 20 20 20 32 35   16           25
305e9 0a 2a 2a 20 20 20 20 20 20 20 20 38 20 20 20 20  .**        8    
305ea 20 20 20 20 20 20 20 31 32 0a 2a 2a 20 20 20 20         12.**    
305eb 20 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20      4           
305ec 31 30 0a 2a 2a 20 20 20 20 20 20 20 20 32 20 20  10.**        2  
305ed 20 20 20 20 20 20 20 20 20 20 36 0a 2a 2a 0a 2a            6.**.*
305ee 2a 20 54 68 69 73 20 61 70 70 65 61 72 73 20 74  * This appears t
305ef 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 6d 6f  o have only a mo
305f0 64 65 72 61 74 65 20 69 6d 70 61 63 74 20 6f 6e  derate impact on
305f1 20 71 75 65 72 69 65 73 20 66 6f 72 20 76 65 72   queries for ver
305f2 79 0a 2a 2a 20 66 72 65 71 75 65 6e 74 20 74 65  y.** frequent te
305f3 72 6d 73 20 28 77 68 69 63 68 20 61 72 65 20 73  rms (which are s
305f4 6f 6d 65 77 68 61 74 20 64 6f 6d 69 6e 61 74 65  omewhat dominate
305f5 64 20 62 79 20 73 65 67 6d 65 6e 74 20 6d 65 72  d by segment mer
305f6 67 65 0a 2a 2a 20 63 6f 73 74 73 29 2c 20 61 6e  ge.** costs), an
305f7 64 20 69 6e 66 72 65 71 75 65 6e 74 20 61 6e 64  d infrequent and
305f8 20 6e 6f 6e 2d 65 78 69 73 74 65 6e 74 20 74 65   non-existent te
305f9 72 6d 73 20 73 74 69 6c 6c 20 73 65 65 6d 20 74  rms still seem t
305fa 6f 20 62 65 20 66 61 73 74 0a 2a 2a 20 65 76 65  o be fast.** eve
305fb 6e 20 77 69 74 68 20 6d 61 6e 79 20 73 65 67 6d  n with many segm
305fc 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f  ents..**.** TODO
305fd 28 73 68 65 73 73 29 20 54 68 61 74 20 73 61 69  (shess) That sai
305fe 64 2c 20 69 74 20 77 6f 75 6c 64 20 62 65 20 6e  d, it would be n
305ff 69 63 65 20 74 6f 20 68 61 76 65 20 61 20 62 65  ice to have a be
30600 74 74 65 72 20 71 75 65 72 79 2d 73 69 64 65 0a  tter query-side.
30601 2a 2a 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20  ** argument for 
30602 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6f 66 20 31  MERGE_COUNT of 1
30603 36 2e 20 20 41 6c 73 6f 2c 20 69 74 20 69 73 20  6.  Also, it is 
30604 70 6f 73 73 69 62 6c 65 2f 6c 69 6b 65 6c 79 20  possible/likely 
30605 74 68 61 74 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61  that.** optimiza
30606 74 69 6f 6e 73 20 74 6f 20 74 68 69 6e 67 73 20  tions to things 
30607 6c 69 6b 65 20 64 6f 63 6c 69 73 74 20 6d 65 72  like doclist mer
30608 67 69 6e 67 20 77 69 6c 6c 20 73 77 69 6e 67 20  ging will swing 
30609 74 68 65 20 73 77 65 65 74 0a 2a 2a 20 73 70 6f  the sweet.** spo
3060a 74 20 61 72 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a  t around..**.**.
3060b 2a 2a 0a 2a 2a 2a 2a 20 48 61 6e 64 6c 69 6e 67  **.**** Handling
3060c 20 6f 66 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e   of deletions an
3060d 64 20 75 70 64 61 74 65 73 20 2a 2a 2a 2a 0a 2a  d updates ****.*
3060e 2a 20 53 69 6e 63 65 20 77 65 27 72 65 20 75 73  * Since we're us
3060f 69 6e 67 20 61 20 73 65 67 6d 65 6e 74 65 64 20  ing a segmented 
30610 73 74 72 75 63 74 75 72 65 2c 20 77 69 74 68 20  structure, with 
30611 6e 6f 20 64 6f 63 69 64 2d 6f 72 69 65 6e 74 65  no docid-oriente
30612 64 0a 2a 2a 20 69 6e 64 65 78 20 69 6e 74 6f 20  d.** index into 
30613 74 68 65 20 74 65 72 6d 20 69 6e 64 65 78 2c 20  the term index, 
30614 77 65 20 63 6c 65 61 72 6c 79 20 63 61 6e 6e 6f  we clearly canno
30615 74 20 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20  t simply update 
30616 74 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 64 65  the term.** inde
30617 78 20 77 68 65 6e 20 61 20 64 6f 63 75 6d 65 6e  x when a documen
30618 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20  t is deleted or 
30619 75 70 64 61 74 65 64 2e 20 20 46 6f 72 20 64 65  updated.  For de
3061a 6c 65 74 69 6f 6e 73 2c 20 77 65 0a 2a 2a 20 77  letions, we.** w
3061b 72 69 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f  rite an empty do
3061c 63 6c 69 73 74 20 28 76 61 72 69 6e 74 28 64 6f  clist (varint(do
3061d 63 69 64 29 20 76 61 72 69 6e 74 28 50 4f 53 5f  cid) varint(POS_
3061e 45 4e 44 29 29 2c 20 66 6f 72 20 75 70 64 61 74  END)), for updat
3061f 65 73 0a 2a 2a 20 77 65 20 73 69 6d 70 6c 79 20  es.** we simply 
30620 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 64 6f  write the new do
30621 63 6c 69 73 74 2e 20 20 53 65 67 6d 65 6e 74 20  clist.  Segment 
30622 6d 65 72 67 65 73 20 6f 76 65 72 77 72 69 74 65  merges overwrite
30623 20 6f 6c 64 65 72 0a 2a 2a 20 64 61 74 61 20 66   older.** data f
30624 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
30625 64 6f 63 69 64 20 77 69 74 68 20 6e 65 77 65 72  docid with newer
30626 20 64 61 74 61 2c 20 73 6f 20 64 65 6c 65 74 65   data, so delete
30627 73 20 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a 20  s or updates.** 
30628 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20  will eventually 
30629 6f 76 65 72 74 61 6b 65 20 74 68 65 20 65 61 72  overtake the ear
3062a 6c 69 65 72 20 64 61 74 61 20 61 6e 64 20 6b 6e  lier data and kn
3062b 6f 63 6b 20 69 74 20 6f 75 74 2e 20 20 54 68 65  ock it out.  The
3062c 0a 2a 2a 20 71 75 65 72 79 20 6c 6f 67 69 63 20  .** query logic 
3062d 6c 69 6b 65 77 69 73 65 20 6d 65 72 67 65 73 20  likewise merges 
3062e 64 6f 63 6c 69 73 74 73 20 73 6f 20 74 68 61 74  doclists so that
3062f 20 6e 65 77 65 72 20 64 61 74 61 20 6b 6e 6f 63   newer data knoc
30630 6b 73 20 6f 75 74 0a 2a 2a 20 6f 6c 64 65 72 20  ks out.** older 
30631 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f  data..**.** TODO
30632 28 73 68 65 73 73 29 20 50 72 6f 76 69 64 65 20  (shess) Provide 
30633 61 20 56 41 43 55 55 4d 20 74 79 70 65 20 6f 70  a VACUUM type op
30634 65 72 61 74 69 6f 6e 20 74 6f 20 63 6c 65 61 72  eration to clear
30635 20 6f 75 74 20 61 6c 6c 0a 2a 2a 20 64 65 6c 65   out all.** dele
30636 74 69 6f 6e 73 20 61 6e 64 20 64 75 70 6c 69 63  tions and duplic
30637 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 77 6f  ations.  This wo
30638 75 6c 64 20 62 61 73 69 63 61 6c 6c 79 20 62 65  uld basically be
30639 20 61 20 66 6f 72 63 65 64 20 6d 65 72 67 65 0a   a forced merge.
3063a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ** into a single
3063b 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 0a 23 69   segment..*/..#i
3063c 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
3063d 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3063e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3063f 5f 46 54 53 33 29 0a 0a 23 69 66 20 64 65 66 69  _FTS3)..#if defi
30640 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
30641 45 5f 46 54 53 33 29 20 26 26 20 21 64 65 66 69  E_FTS3) && !defi
30642 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
30643 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
30644 5f 43 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a  _CORE 1.#endif..
30645 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
30646 20 49 6e 63 6c 75 64 65 20 66 74 73 33 5f 65 78   Include fts3_ex
30647 70 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  pr.h in the midd
30648 6c 65 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a  le of fts3.c ***
30649 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3064a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3064b 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
3064c 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.h ********
3064d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3064e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3064f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20  ./*.** 2008 Nov 
30650 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  28.**.** The aut
30651 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
30652 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
30653 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
30654 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
30655 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
30656 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
30657 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
30658 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
30659 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
3065a 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
3065b 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
3065c 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
3065d 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
3065e 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
3065f 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
30660 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
30661 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
30662 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30663 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30664 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30665 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30666 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a  ******.**.*/../*
30667 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
30668 63 6c 75 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e  clude fts3_token
30669 69 7a 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69  izer.h in the mi
3066a 64 64 6c 65 20 6f 66 20 66 74 73 33 5f 65 78 70  ddle of fts3_exp
3066b 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  r.h ********/./*
3066c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
3066d 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f  gin file fts3_to
3066e 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a  kenizer.h ******
3066f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
30671 0a 2a 2a 20 32 30 30 36 20 4a 75 6c 79 20 31 30  .** 2006 July 10
30672 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
30673 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
30674 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
30675 75 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  urce code..**.**
30676 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30677 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30678 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30679 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3067a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e  *******.** Defin
3067b 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  es the interface
3067c 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75   to tokenizers u
3067d 73 65 64 20 62 79 20 66 75 6c 6c 74 65 78 74 2d  sed by fulltext-
3067e 73 65 61 72 63 68 2e 20 20 54 68 65 72 65 0a 2a  search.  There.*
3067f 2a 20 61 72 65 20 74 68 72 65 65 20 62 61 73 69  * are three basi
30680 63 20 63 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a  c components:.**
30681 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  .** sqlite3_toke
30682 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20  nizer_module is 
30683 61 20 73 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69  a singleton defi
30684 6e 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a  ning the tokeniz
30685 65 72 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  er.** interface 
30686 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  functions.  This
30687 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 20   is essentially 
30688 74 68 65 20 63 6c 61 73 73 20 73 74 72 75 63 74  the class struct
30689 75 72 65 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e  ure for.** token
3068a 69 7a 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c  izers..**.** sql
3068b 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69  ite3_tokenizer i
3068c 73 20 75 73 65 64 20 74 6f 20 64 65 66 69 6e 65  s used to define
3068d 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 6f   a particular to
3068e 6b 65 6e 69 7a 65 72 2c 20 70 65 72 68 61 70 73  kenizer, perhaps
3068f 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 75  .** including cu
30690 73 74 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f  stomization info
30691 72 6d 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  rmation defined 
30692 61 74 20 63 72 65 61 74 69 6f 6e 20 74 69 6d 65  at creation time
30693 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ..**.** sqlite3_
30694 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
30695 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79   is generated by
30696 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20   a tokenizer to 
30697 67 65 6e 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65  generate.** toke
30698 6e 73 20 66 72 6f 6d 20 61 20 70 61 72 74 69 63  ns from a partic
30699 75 6c 61 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23  ular input..*/.#
3069a 69 66 6e 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b  ifndef _FTS3_TOK
3069b 45 4e 49 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e  ENIZER_H_.#defin
3069c 65 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  e _FTS3_TOKENIZE
3069d 52 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  R_H_../* TODO(sh
3069e 65 73 73 29 20 4f 6e 6c 79 20 75 73 65 64 20 66  ess) Only used f
3069f 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64  or SQLITE_OK and
306a0 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20   SQLITE_DONE at 
306a1 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66  this time..** If
306a2 20 74 6f 6b 65 6e 69 7a 65 72 73 20 61 72 65 20   tokenizers are 
306a3 74 6f 20 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f  to be allowed to
306a4 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28   call sqlite3_*(
306a5 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65  ) functions, the
306a6 6e 0a 2a 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65  n.** we will nee
306a7 64 20 61 20 77 61 79 20 74 6f 20 72 65 67 69 73  d a way to regis
306a8 74 65 72 20 74 68 65 20 41 50 49 20 63 6f 6e 73  ter the API cons
306a9 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a  istently..*/../*
306aa 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 73 20 75  .** Structures u
306ab 73 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e  sed by the token
306ac 69 7a 65 72 20 69 6e 74 65 72 66 61 63 65 2e 20  izer interface. 
306ad 57 68 65 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e  When a new token
306ae 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  izer.** implemen
306af 74 61 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74  tation is regist
306b0 65 72 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72  ered, the caller
306b1 20 70 72 6f 76 69 64 65 73 20 61 20 70 6f 69 6e   provides a poin
306b2 74 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c  ter to.** an sql
306b3 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
306b4 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  odule containing
306b5 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
306b6 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e   callback.** fun
306b7 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65  ctions that make
306b8 20 75 70 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74   up an implement
306b9 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  ation..**.** Whe
306ba 6e 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 20  n an fts3 table 
306bb 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 70  is created, it p
306bc 61 73 73 65 73 20 61 6e 79 20 61 72 67 75 6d 65  asses any argume
306bd 6e 74 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a  nts passed to.**
306be 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63   the tokenizer c
306bf 6c 61 75 73 65 20 6f 66 20 74 68 65 20 43 52 45  lause of the CRE
306c0 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
306c1 45 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74  E statement to t
306c2 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f  he.** sqlite3_to
306c3 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78  kenizer_module.x
306c4 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f  Create() functio
306c5 6e 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  n of the request
306c6 65 64 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20  ed tokenizer.** 
306c7 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
306c8 54 68 65 20 78 43 72 65 61 74 65 28 29 20 66 75  The xCreate() fu
306c9 6e 63 74 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72  nction in turn r
306ca 65 74 75 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71  eturns an .** sq
306cb 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
306cc 73 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73  structure repres
306cd 65 6e 74 69 6e 67 20 74 68 65 20 73 70 65 63 69  enting the speci
306ce 66 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f  fic tokenizer to
306cf 0a 2a 2a 20 62 65 20 75 73 65 64 20 66 6f 72 20  .** be used for 
306d0 74 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 28  the fts3 table (
306d1 63 75 73 74 6f 6d 69 7a 65 64 20 62 79 20 74 68  customized by th
306d2 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75  e tokenizer clau
306d3 73 65 20 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a  se arguments)..*
306d4 2a 0a 2a 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65  *.** To tokenize
306d5 20 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72   an input buffer
306d6 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f  , the sqlite3_to
306d7 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78  kenizer_module.x
306d8 4f 70 65 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  Open().** method
306d9 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 72   is called. It r
306da 65 74 75 72 6e 73 20 61 6e 20 73 71 6c 69 74 65  eturns an sqlite
306db 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
306dc 6f 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61  or object.** tha
306dd 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  t may be used to
306de 20 74 6f 6b 65 6e 69 7a 65 20 61 20 73 70 65 63   tokenize a spec
306df 69 66 69 63 20 69 6e 70 75 74 20 62 75 66 66 65  ific input buffe
306e0 72 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68  r based on.** th
306e1 65 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72  e tokenization r
306e2 75 6c 65 73 20 73 75 70 70 6c 69 65 64 20 62 79  ules supplied by
306e3 20 61 20 73 70 65 63 69 66 69 63 20 73 71 6c 69   a specific sqli
306e4 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  te3_tokenizer.**
306e5 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65   object..*/.type
306e6 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
306e7 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
306e8 75 6c 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule sqlite3_toke
306e9 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79  nizer_module;.ty
306ea 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
306eb 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73  ite3_tokenizer s
306ec 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
306ed 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
306ee 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
306ef 65 72 5f 63 75 72 73 6f 72 20 73 71 6c 69 74 65  er_cursor sqlite
306f0 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
306f1 6f 72 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69  or;..struct sqli
306f2 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
306f3 64 75 6c 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a  dule {..  /*.  *
306f4 2a 20 53 74 72 75 63 74 75 72 65 20 76 65 72 73  * Structure vers
306f5 69 6f 6e 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61  ion. Should alwa
306f6 79 73 20 62 65 20 73 65 74 20 74 6f 20 30 2e 0a  ys be set to 0..
306f7 20 20 2a 2f 0a 20 20 69 6e 74 20 69 56 65 72 73    */.  int iVers
306f8 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ion;..  /*.  ** 
306f9 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b  Create a new tok
306fa 65 6e 69 7a 65 72 2e 20 54 68 65 20 76 61 6c 75  enizer. The valu
306fb 65 73 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d  es in the argv[]
306fc 20 61 72 72 61 79 20 61 72 65 20 74 68 65 0a 20   array are the. 
306fd 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 61   ** arguments pa
306fe 73 73 65 64 20 74 6f 20 74 68 65 20 22 74 6f 6b  ssed to the "tok
306ff 65 6e 69 7a 65 72 22 20 63 6c 61 75 73 65 20 6f  enizer" clause o
30700 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52  f the CREATE VIR
30701 54 55 41 4c 0a 20 20 2a 2a 20 54 41 42 4c 45 20  TUAL.  ** TABLE 
30702 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63  statement that c
30703 72 65 61 74 65 64 20 74 68 65 20 66 74 73 33 20  reated the fts3 
30704 74 61 62 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70  table. For examp
30705 6c 65 2c 20 69 66 0a 20 20 2a 2a 20 74 68 65 20  le, if.  ** the 
30706 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73  following SQL is
30707 20 65 78 65 63 75 74 65 64 3a 0a 20 20 2a 2a 0a   executed:.  **.
30708 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 2e 2e    **   CREATE ..
30709 20 55 53 49 4e 47 20 66 74 73 33 28 20 2e 2e 2e   USING fts3( ...
3070a 20 2c 20 74 6f 6b 65 6e 69 7a 65 72 20 3c 74 6f   , tokenizer <to
3070b 6b 65 6e 69 7a 65 72 2d 6e 61 6d 65 3e 20 61 72  kenizer-name> ar
3070c 67 31 20 61 72 67 32 29 0a 20 20 2a 2a 0a 20 20  g1 arg2).  **.  
3070d 2a 2a 20 74 68 65 6e 20 61 72 67 63 20 69 73 20  ** then argc is 
3070e 73 65 74 20 74 6f 20 32 2c 20 61 6e 64 20 74 68  set to 2, and th
3070f 65 20 61 72 67 76 5b 5d 20 61 72 72 61 79 20 63  e argv[] array c
30710 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73  ontains pointers
30711 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 73 74 72  .  ** to the str
30712 69 6e 67 73 20 22 61 72 67 31 22 20 61 6e 64 20  ings "arg1" and 
30713 22 61 72 67 32 22 2e 0a 20 20 2a 2a 0a 20 20 2a  "arg2"..  **.  *
30714 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20 73 68  * This method sh
30715 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68  ould return eith
30716 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29  er SQLITE_OK (0)
30717 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65  , or an SQLite e
30718 72 72 6f 72 20 0a 20 20 2a 2a 20 63 6f 64 65 2e  rror .  ** code.
30719 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73   If SQLITE_OK is
3071a 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20   returned, then 
3071b 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 73 68 6f  *ppTokenizer sho
3071c 75 6c 64 20 62 65 20 73 65 74 0a 20 20 2a 2a 20  uld be set.  ** 
3071d 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
3071e 6e 65 77 6c 79 20 63 72 65 61 74 65 64 20 74 6f  newly created to
3071f 6b 65 6e 69 7a 65 72 20 73 74 72 75 63 74 75 72  kenizer structur
30720 65 2e 20 54 68 65 20 67 65 6e 65 72 69 63 0a 20  e. The generic. 
30721 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65   ** sqlite3_toke
30722 6e 69 7a 65 72 2e 70 4d 6f 64 75 6c 65 20 76 61  nizer.pModule va
30723 72 69 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f  riable should no
30724 74 20 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64  t be initialised
30725 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 63 61   by.  ** this ca
30726 6c 6c 62 61 63 6b 2e 20 54 68 65 20 63 61 6c 6c  llback. The call
30727 65 72 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 0a 20  er will do so.. 
30728 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 72 65   */.  int (*xCre
30729 61 74 65 29 28 0a 20 20 20 20 69 6e 74 20 61 72  ate)(.    int ar
3072a 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gc,             
3072b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3072c 20 53 69 7a 65 20 6f 66 20 61 72 67 76 20 61 72   Size of argv ar
3072d 72 61 79 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  ray */.    const
3072e 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
3072f 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  v,             /
30730 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75  * Tokenizer argu
30731 6d 65 6e 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a  ment strings */.
30732 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65      sqlite3_toke
30733 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69  nizer **ppTokeni
30734 7a 65 72 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  zer     /* OUT: 
30735 43 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65  Created tokenize
30736 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a  r */.  );..  /*.
30737 20 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20    ** Destroy an 
30738 65 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a  existing tokeniz
30739 65 72 2e 20 54 68 65 20 66 74 73 33 20 6d 6f 64  er. The fts3 mod
3073a 75 6c 65 20 63 61 6c 6c 73 20 74 68 69 73 20 6d  ule calls this m
3073b 65 74 68 6f 64 0a 20 20 2a 2a 20 65 78 61 63 74  ethod.  ** exact
3073c 6c 79 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ly once for each
3073d 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
3073e 20 74 6f 20 78 43 72 65 61 74 65 28 29 2e 0a 20   to xCreate().. 
3073f 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 44 65 73   */.  int (*xDes
30740 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 74 6f  troy)(sqlite3_to
30741 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
30742 7a 65 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  zer);..  /*.  **
30743 20 43 72 65 61 74 65 20 61 20 74 6f 6b 65 6e 69   Create a tokeni
30744 7a 65 72 20 63 75 72 73 6f 72 20 74 6f 20 74 6f  zer cursor to to
30745 6b 65 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20  kenize an input 
30746 62 75 66 66 65 72 2e 20 54 68 65 20 63 61 6c 6c  buffer. The call
30747 65 72 0a 20 20 2a 2a 20 69 73 20 72 65 73 70 6f  er.  ** is respo
30748 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72  nsible for ensur
30749 69 6e 67 20 74 68 61 74 20 74 68 65 20 69 6e 70  ing that the inp
3074a 75 74 20 62 75 66 66 65 72 20 72 65 6d 61 69 6e  ut buffer remain
3074b 73 20 76 61 6c 69 64 0a 20 20 2a 2a 20 75 6e 74  s valid.  ** unt
3074c 69 6c 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  il the cursor is
3074d 20 63 6c 6f 73 65 64 20 28 75 73 69 6e 67 20 74   closed (using t
3074e 68 65 20 78 43 6c 6f 73 65 28 29 20 6d 65 74 68  he xClose() meth
3074f 6f 64 29 2e 20 0a 20 20 2a 2f 0a 20 20 69 6e 74  od). .  */.  int
30750 20 28 2a 78 4f 70 65 6e 29 28 0a 20 20 20 20 73   (*xOpen)(.    s
30751 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
30752 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20   *pTokenizer,   
30753 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
30754 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 63   object */.    c
30755 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75  onst char *pInpu
30756 74 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  t, int nBytes,  
30757 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 62 75 66      /* Input buf
30758 66 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  fer */.    sqlit
30759 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
3075a 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20  sor **ppCursor  
3075b 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20  /* OUT: Created 
3075c 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  tokenizer cursor
3075d 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20   */.  );..  /*. 
3075e 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65   ** Destroy an e
3075f 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65  xisting tokenize
30760 72 20 63 75 72 73 6f 72 2e 20 54 68 65 20 66 74  r cursor. The ft
30761 73 33 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20  s3 module calls 
30762 74 68 69 73 20 0a 20 20 2a 2a 20 6d 65 74 68 6f  this .  ** metho
30763 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66  d exactly once f
30764 6f 72 20 65 61 63 68 20 73 75 63 63 65 73 73 66  or each successf
30765 75 6c 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e  ul call to xOpen
30766 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  ()..  */.  int (
30767 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33  *xClose)(sqlite3
30768 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
30769 72 20 2a 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20  r *pCursor);..  
3076a 2f 2a 0a 20 20 2a 2a 20 52 65 74 72 69 65 76 65  /*.  ** Retrieve
3076b 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20   the next token 
3076c 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a  from the tokeniz
3076d 65 72 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f  er cursor pCurso
3076e 72 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 65 74  r. This.  ** met
3076f 68 6f 64 20 73 68 6f 75 6c 64 20 65 69 74 68 65  hod should eithe
30770 72 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r return SQLITE_
30771 4f 4b 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  OK and set the v
30772 61 6c 75 65 73 20 6f 66 20 74 68 65 0a 20 20 2a  alues of the.  *
30773 2a 20 22 4f 55 54 22 20 76 61 72 69 61 62 6c 65  * "OUT" variable
30774 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 65 6c  s identified bel
30775 6f 77 2c 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f  ow, or SQLITE_DO
30776 4e 45 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  NE to indicate t
30777 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64  hat.  ** the end
30778 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 68   of the buffer h
30779 61 73 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c  as been reached,
3077a 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72   or an SQLite er
3077b 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20  ror code..  **. 
3077c 20 2a 2a 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f   ** *ppToken sho
3077d 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 70 6f  uld be set to po
3077e 69 6e 74 20 61 74 20 61 20 62 75 66 66 65 72 20  int at a buffer 
3077f 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 0a  containing the .
30780 20 20 2a 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20    ** normalized 
30781 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 74  version of the t
30782 6f 6b 65 6e 20 28 69 2e 65 2e 20 61 66 74 65 72  oken (i.e. after
30783 20 61 6e 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e   any case-foldin
30784 67 20 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 74  g and/or.  ** st
30785 65 6d 6d 69 6e 67 20 68 61 73 20 62 65 65 6e 20  emming has been 
30786 70 65 72 66 6f 72 6d 65 64 29 2e 20 2a 70 6e 42  performed). *pnB
30787 79 74 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73  ytes should be s
30788 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68  et to the length
30789 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 62 75  .  ** of this bu
3078a 66 66 65 72 20 69 6e 20 62 79 74 65 73 2e 20 54  ffer in bytes. T
3078b 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 74 68  he input text th
3078c 61 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 65  at generated the
3078d 20 74 6f 6b 65 6e 20 69 73 0a 20 20 2a 2a 20 69   token is.  ** i
3078e 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
3078f 20 62 79 74 65 20 6f 66 66 73 65 74 73 20 72 65   byte offsets re
30790 74 75 72 6e 65 64 20 69 6e 20 2a 70 69 53 74 61  turned in *piSta
30791 72 74 4f 66 66 73 65 74 20 61 6e 64 0a 20 20 2a  rtOffset and.  *
30792 2a 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2e 20  * *piEndOffset. 
30793 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 73  *piStartOffset s
30794 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
30795 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
30796 20 66 69 72 73 74 0a 20 20 2a 2a 20 62 79 74 65   first.  ** byte
30797 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e   of the token in
30798 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65   the input buffe
30799 72 2e 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20  r. *piEndOffset 
3079a 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a 20 20  should be set.  
3079b 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20  ** to the index 
3079c 6f 66 20 74 68 65 20 66 69 72 73 74 20 62 79 74  of the first byt
3079d 65 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20  e just past the 
3079e 65 6e 64 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  end of the token
3079f 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 69 6e 70   in.  ** the inp
307a0 75 74 20 62 75 66 66 65 72 2e 0a 20 20 2a 2a 0a  ut buffer..  **.
307a1 20 20 2a 2a 20 54 68 65 20 62 75 66 66 65 72 20    ** The buffer 
307a2 2a 70 70 54 6f 6b 65 6e 20 69 73 20 73 65 74 20  *ppToken is set 
307a3 74 6f 20 70 6f 69 6e 74 20 61 74 20 69 73 20 6d  to point at is m
307a4 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 74 6f  anaged by the to
307a5 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6d 70  kenizer.  ** imp
307a6 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 74 20  lementation. It 
307a7 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
307a8 20 74 6f 20 62 65 20 76 61 6c 69 64 20 75 6e 74   to be valid unt
307a9 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  il the next call
307aa 0a 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74 28 29  .  ** to xNext()
307ab 20 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20 0a 20   or xClose(). . 
307ac 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
307ad 65 73 73 29 20 63 75 72 72 65 6e 74 20 69 6d 70  ess) current imp
307ae 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75  lementation requ
307af 69 72 65 73 20 70 49 6e 70 75 74 20 74 6f 20 62  ires pInput to b
307b0 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  e.  ** nul-termi
307b1 6e 61 74 65 64 2e 20 20 54 68 69 73 20 73 68 6f  nated.  This sho
307b2 75 6c 64 20 65 69 74 68 65 72 20 62 65 20 66 69  uld either be fi
307b3 78 65 64 2c 20 6f 72 20 70 49 6e 70 75 74 2f 6e  xed, or pInput/n
307b4 42 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f 75 6c  Bytes.  ** shoul
307b5 64 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74  d be converted t
307b6 6f 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20  o zInput..  */. 
307b7 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 0a 20   int (*xNext)(. 
307b8 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
307b9 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
307ba 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b 65 6e  rsor,   /* Token
307bb 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
307bc 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a     const char **
307bd 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a 70 6e  ppToken, int *pn
307be 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54 3a 20  Bytes,  /* OUT: 
307bf 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78 74 20  Normalized text 
307c0 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  for token */.   
307c1 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
307c2 73 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 42 79  set,  /* OUT: By
307c3 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  te offset of tok
307c4 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
307c5 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
307c6 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 2f  iEndOffset,    /
307c7 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66 66 73  * OUT: Byte offs
307c8 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 74 6f 6b  et of end of tok
307c9 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
307ca 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
307cb 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 2f  iPosition      /
307cc 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
307cd 20 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e 65 64   tokens returned
307ce 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 6e 65   before this one
307cf 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73 74 72   */.  );.};..str
307d0 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  uct sqlite3_toke
307d1 6e 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20  nizer {.  const 
307d2 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
307d3 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  r_module *pModul
307d4 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  e;  /* The modul
307d5 65 20 66 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e  e for this token
307d6 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b  izer */.  /* Tok
307d7 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
307d8 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
307d9 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
307da 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
307db 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  ;..struct sqlite
307dc 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
307dd 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
307de 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
307df 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54  izer;       /* T
307e0 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69  okenizer for thi
307e1 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 2f  s cursor. */.  /
307e2 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  * Tokenizer impl
307e3 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c  ementations will
307e4 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61   typically add a
307e5 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
307e6 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66 20 2f   */.};..#endif /
307e7 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  * _FTS3_TOKENIZE
307e8 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  R_H_ */../******
307e9 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
307ea 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68  fts3_tokenizer.h
307eb 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
307ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
307ed 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
307ee 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
307ef 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
307f0 74 20 6f 66 66 20 69 6e 20 66 74 73 33 5f 65 78  t off in fts3_ex
307f1 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pr.h ***********
307f2 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
307f3 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65  The following de
307f4 73 63 72 69 62 65 73 20 74 68 65 20 73 79 6e 74  scribes the synt
307f5 61 78 20 73 75 70 70 6f 72 74 65 64 20 62 79 20  ax supported by 
307f6 74 68 65 20 66 74 73 33 20 4d 41 54 43 48 0a 2a  the fts3 MATCH.*
307f7 2a 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 61 20  * operator in a 
307f8 73 69 6d 69 6c 61 72 20 66 6f 72 6d 61 74 20 74  similar format t
307f9 6f 20 74 68 61 74 20 75 73 65 64 20 62 79 20 74  o that used by t
307fa 68 65 20 6c 65 6d 6f 6e 20 70 61 72 73 65 72 0a  he lemon parser.
307fb 2a 2a 20 67 65 6e 65 72 61 74 6f 72 2e 20 54 68  ** generator. Th
307fc 69 73 20 6d 6f 64 75 6c 65 20 64 6f 65 73 20 6e  is module does n
307fd 6f 74 20 75 73 65 20 61 63 74 75 61 6c 6c 79 20  ot use actually 
307fe 6c 65 6d 6f 6e 2c 20 69 74 20 75 73 65 73 20 61  lemon, it uses a
307ff 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61 72 73 65  .** custom parse
30800 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 71 75 65 72 79  r..**.**   query
30801 20 3a 3a 3d 20 61 6e 64 65 78 70 72 20 28 4f 52   ::= andexpr (OR
30802 20 61 6e 64 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a   andexpr)*..**.*
30803 2a 20 20 20 61 6e 64 65 78 70 72 20 3a 3a 3d 20  *   andexpr ::= 
30804 6e 6f 74 65 78 70 72 20 28 41 4e 44 3f 20 6e 6f  notexpr (AND? no
30805 74 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20  texpr)*..**.**  
30806 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 6e 65 61   notexpr ::= nea
30807 72 65 78 70 72 20 28 4e 4f 54 20 6e 65 61 72 65  rexpr (NOT neare
30808 78 70 72 7c 2d 54 4f 4b 45 4e 29 2a 2e 0a 2a 2a  xpr|-TOKEN)*..**
30809 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 4c     notexpr ::= L
3080a 50 20 71 75 65 72 79 20 52 50 2e 0a 2a 2a 0a 2a  P query RP..**.*
3080b 2a 20 20 20 6e 65 61 72 65 78 70 72 20 3a 3a 3d  *   nearexpr ::=
3080c 20 70 68 72 61 73 65 20 28 4e 45 41 52 20 64 69   phrase (NEAR di
3080d 73 74 61 6e 63 65 5f 6f 70 74 20 6e 65 61 72 65  stance_opt neare
3080e 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 64  xpr)*..**.**   d
3080f 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20  istance_opt ::= 
30810 2e 0a 2a 2a 20 20 20 64 69 73 74 61 6e 63 65 5f  ..**   distance_
30811 6f 70 74 20 3a 3a 3d 20 2f 20 49 4e 54 45 47 45  opt ::= / INTEGE
30812 52 2e 0a 2a 2a 0a 2a 2a 20 20 20 70 68 72 61 73  R..**.**   phras
30813 65 20 3a 3a 3d 20 54 4f 4b 45 4e 2e 0a 2a 2a 20  e ::= TOKEN..** 
30814 20 20 70 68 72 61 73 65 20 3a 3a 3d 20 43 4f 4c    phrase ::= COL
30815 55 4d 4e 3a 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20  UMN:TOKEN..**   
30816 70 68 72 61 73 65 20 3a 3a 3d 20 22 54 4f 4b 45  phrase ::= "TOKE
30817 4e 20 54 4f 4b 45 4e 20 54 4f 4b 45 4e 2e 2e 2e  N TOKEN TOKEN...
30818 22 2e 0a 2a 2f 0a 0a 74 79 70 65 64 65 66 20 73  "..*/..typedef s
30819 74 72 75 63 74 20 46 74 73 33 45 78 70 72 20 46  truct Fts3Expr F
3081a 74 73 33 45 78 70 72 3b 0a 74 79 70 65 64 65 66  ts3Expr;.typedef
3081b 20 73 74 72 75 63 74 20 46 74 73 33 50 68 72 61   struct Fts3Phra
3081c 73 65 20 46 74 73 33 50 68 72 61 73 65 3b 0a 0a  se Fts3Phrase;..
3081d 2f 2a 0a 2a 2a 20 41 20 22 70 68 72 61 73 65 22  /*.** A "phrase"
3081e 20 69 73 20 61 20 73 65 71 75 65 6e 63 65 20 6f   is a sequence o
3081f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f  f one or more to
30820 6b 65 6e 73 20 74 68 61 74 20 6d 75 73 74 20 6d  kens that must m
30821 61 74 63 68 20 69 6e 0a 2a 2a 20 73 65 71 75 65  atch in.** seque
30822 6e 63 65 2e 20 20 41 20 73 69 6e 67 6c 65 20 74  nce.  A single t
30823 6f 6b 65 6e 20 69 73 20 74 68 65 20 62 61 73 65  oken is the base
30824 20 63 61 73 65 20 61 6e 64 20 74 68 65 20 6d 6f   case and the mo
30825 73 74 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 2e 0a  st common case..
30826 2a 2a 20 46 6f 72 20 61 20 73 65 71 75 65 6e 63  ** For a sequenc
30827 65 20 6f 66 20 74 6f 6b 65 6e 73 20 63 6f 6e 74  e of tokens cont
30828 61 69 6e 65 64 20 69 6e 20 22 2e 2e 2e 22 2c 20  ained in "...", 
30829 6e 54 6f 6b 65 6e 20 77 69 6c 6c 20 62 65 20 74  nToken will be t
3082a 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20  he number.** of 
3082b 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 73 74  tokens in the st
3082c 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ring..*/.struct 
3082d 46 74 73 33 50 68 72 61 73 65 20 7b 0a 20 20 69  Fts3Phrase {.  i
3082e 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20  nt nToken;      
3082f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
30830 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 70   tokens in the p
30831 68 72 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  hrase */.  int i
30832 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
30833 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
30834 6d 6e 20 74 68 69 73 20 70 68 72 61 73 65 20 6d  mn this phrase m
30835 75 73 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 69  ust match */.  i
30836 6e 74 20 69 73 4e 6f 74 3b 20 20 20 20 20 20 20  nt isNot;       
30837 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 70 72      /* Phrase pr
30838 65 66 69 78 65 64 20 62 79 20 75 6e 61 72 79 20  efixed by unary 
30839 6e 6f 74 20 28 2d 29 20 6f 70 65 72 61 74 6f 72  not (-) operator
3083a 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 50 68 72   */.  struct Phr
3083b 61 73 65 54 6f 6b 65 6e 20 7b 0a 20 20 20 20 63  aseToken {.    c
3083c 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
3083d 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
3083e 74 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  the token */.   
3083f 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
30840 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
30841 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62 75 66   of bytes in buf
30842 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  fer pointed to b
30843 79 20 7a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  y z */.    int i
30844 73 50 72 65 66 69 78 3b 20 20 20 20 20 20 20 20  sPrefix;        
30845 20 2f 2a 20 54 72 75 65 20 69 66 20 74 6f 6b 65   /* True if toke
30846 6e 20 65 6e 64 73 20 69 6e 20 77 69 74 68 20 61  n ends in with a
30847 20 22 2a 22 20 63 68 61 72 61 63 74 65 72 20 2a   "*" character *
30848 2f 0a 20 20 7d 20 61 54 6f 6b 65 6e 5b 31 5d 3b  /.  } aToken[1];
30849 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
3084a 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 74  entry for each t
3084b 6f 6b 65 6e 20 69 6e 20 74 68 65 20 70 68 72 61  oken in the phra
3084c 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  se */.};../*.** 
3084d 41 20 74 72 65 65 20 6f 66 20 74 68 65 73 65 20  A tree of these 
3084e 6f 62 6a 65 63 74 73 20 66 6f 72 6d 73 20 74 68  objects forms th
3084f 65 20 52 48 53 20 6f 66 20 61 20 4d 41 54 43 48  e RHS of a MATCH
30850 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74   operator..*/.st
30851 72 75 63 74 20 46 74 73 33 45 78 70 72 20 7b 0a  ruct Fts3Expr {.
30852 20 20 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20    int eType;    
30853 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30854 4f 6e 65 20 6f 66 20 74 68 65 20 46 54 53 51 55  One of the FTSQU
30855 45 52 59 5f 58 58 58 20 76 61 6c 75 65 73 20 64  ERY_XXX values d
30856 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a  efined below */.
30857 20 20 69 6e 74 20 6e 4e 65 61 72 3b 20 20 20 20    int nNear;    
30858 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30859 56 61 6c 69 64 20 69 66 20 65 54 79 70 65 3d 3d  Valid if eType==
3085a 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 2a 2f  FTSQUERY_NEAR */
3085b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 50 61  .  Fts3Expr *pPa
3085c 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  rent;         /*
3085d 20 70 50 61 72 65 6e 74 2d 3e 70 4c 65 66 74 3d   pParent->pLeft=
3085e 3d 74 68 69 73 20 6f 72 20 70 50 61 72 65 6e 74  =this or pParent
3085f 2d 3e 70 52 69 67 68 74 3d 3d 74 68 69 73 20 2a  ->pRight==this *
30860 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c  /.  Fts3Expr *pL
30861 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  eft;           /
30862 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  * Left operand *
30863 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 52  /.  Fts3Expr *pR
30864 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f  ight;          /
30865 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20  * Right operand 
30866 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73 65 20  */.  Fts3Phrase 
30867 2a 70 50 68 72 61 73 65 3b 20 20 20 20 20 20 20  *pPhrase;       
30868 2f 2a 20 56 61 6c 69 64 20 69 66 20 65 54 79 70  /* Valid if eTyp
30869 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
3086a 53 45 20 2a 2f 0a 7d 3b 0a 0a 53 51 4c 49 54 45  SE */.};..SQLITE
3086b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3086c 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
3086d 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
3086e 7a 65 72 20 2a 2c 20 63 68 61 72 20 2a 2a 2c 20  zer *, char **, 
3086f 69 6e 74 2c 20 69 6e 74 2c 20 0a 20 20 20 20 20  int, int, .     
30870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30871 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
30872 2c 20 69 6e 74 2c 20 46 74 73 33 45 78 70 72 20  , int, Fts3Expr 
30873 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
30874 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
30875 46 74 73 33 45 78 70 72 46 72 65 65 28 46 74 73  Fts3ExprFree(Fts
30876 33 45 78 70 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  3Expr *);../*.**
30877 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65   Candidate value
30878 73 20 66 6f 72 20 46 74 73 33 51 75 65 72 79 2e  s for Fts3Query.
30879 65 54 79 70 65 2e 20 4e 6f 74 65 20 74 68 61 74  eType. Note that
3087a 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
3087b 65 20 66 69 72 73 74 0a 2a 2a 20 66 6f 75 72 20  e first.** four 
3087c 76 61 6c 75 65 73 20 69 73 20 69 6e 20 6f 72 64  values is in ord
3087d 65 72 20 6f 66 20 70 72 65 63 65 64 65 6e 63 65  er of precedence
3087e 20 77 68 65 6e 20 70 61 72 73 69 6e 67 20 65 78   when parsing ex
3087f 70 72 65 73 73 69 6f 6e 73 2e 20 46 6f 72 20 0a  pressions. For .
30880 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  ** example, the 
30881 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
30882 20 20 20 22 61 20 4f 52 20 62 20 41 4e 44 20 63     "a OR b AND c
30883 20 4e 4f 54 20 64 20 4e 45 41 52 20 65 22 0a 2a   NOT d NEAR e".*
30884 2a 0a 2a 2a 20 69 73 20 65 71 75 69 76 61 6c 65  *.** is equivale
30885 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 22  nt to:.**.**   "
30886 61 20 4f 52 20 28 62 20 41 4e 44 20 28 63 20 4e  a OR (b AND (c N
30887 4f 54 20 28 64 20 4e 45 41 52 20 65 29 29 29 22  OT (d NEAR e)))"
30888 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 51  .*/.#define FTSQ
30889 55 45 52 59 5f 4e 45 41 52 20 20 20 31 0a 23 64  UERY_NEAR   1.#d
3088a 65 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4e  efine FTSQUERY_N
3088b 4f 54 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20  OT    2.#define 
3088c 46 54 53 51 55 45 52 59 5f 41 4e 44 20 20 20 20  FTSQUERY_AND    
3088d 33 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  3.#define FTSQUE
3088e 52 59 5f 4f 52 20 20 20 20 20 34 0a 23 64 65 66  RY_OR     4.#def
3088f 69 6e 65 20 46 54 53 51 55 45 52 59 5f 50 48 52  ine FTSQUERY_PHR
30890 41 53 45 20 35 0a 0a 23 69 66 64 65 66 20 53 51  ASE 5..#ifdef SQ
30891 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
30892 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
30893 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69  lite3Fts3ExprIni
30894 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 73  tTestInterface(s
30895 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e  qlite3 *db);.#en
30896 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
30897 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
30898 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.h ********
30899 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3089a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3089b 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
3089c 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
3089d 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
3089e 66 20 69 6e 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  f in fts3.c ****
3089f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
308a0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
308a1 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73  **** Include fts
308a2 33 5f 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20  3_hash.h in the 
308a3 6d 69 64 64 6c 65 20 6f 66 20 66 74 73 33 2e 63  middle of fts3.c
308a4 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
308a5 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
308a6 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
308a7 66 74 73 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a  fts3_hash.h ****
308a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
308a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
308aa 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
308ab 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a  September 22.**.
308ac 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
308ad 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
308ae 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
308af 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
308b0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
308b1 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
308b2 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
308b3 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
308b4 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
308b5 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
308b6 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
308b7 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
308b8 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
308b9 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
308ba 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
308bb 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
308bc 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
308bd 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
308be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
308bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
308c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
308c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
308c2 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65  his is the heade
308c3 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67  r file for the g
308c4 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
308c5 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a  e implemenation.
308c6 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  ** used in SQLit
308c7 65 2e 20 20 57 65 27 76 65 20 6d 6f 64 69 66 69  e.  We've modifi
308c8 65 64 20 69 74 20 73 6c 69 67 68 74 6c 79 20 74  ed it slightly t
308c9 6f 20 73 65 72 76 65 20 61 73 20 61 20 73 74 61  o serve as a sta
308ca 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 68 61 73 68 20  ndalone.** hash 
308cb 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
308cc 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75 6c  tion for the ful
308cd 6c 2d 74 65 78 74 20 69 6e 64 65 78 69 6e 67 20  l-text indexing 
308ce 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2f 0a 23 69  module..**.*/.#i
308cf 66 6e 64 65 66 20 5f 46 54 53 33 5f 48 41 53 48  fndef _FTS3_HASH
308d0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 46 54 53  _H_.#define _FTS
308d1 33 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f  3_HASH_H_../* Fo
308d2 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
308d3 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 73  ns of structures
308d4 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
308d5 75 63 74 20 66 74 73 33 48 61 73 68 20 66 74 73  uct fts3Hash fts
308d6 33 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73  3Hash;.typedef s
308d7 74 72 75 63 74 20 66 74 73 33 48 61 73 68 45 6c  truct fts3HashEl
308d8 65 6d 20 66 74 73 33 48 61 73 68 45 6c 65 6d 3b  em fts3HashElem;
308d9 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20  ../* A complete 
308da 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e  hash table is an
308db 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
308dc 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
308dd 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74  ture..** The int
308de 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73  ernals of this s
308df 74 72 75 63 74 75 72 65 20 61 72 65 20 69 6e 74  tructure are int
308e0 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
308e1 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20  ue -- client.** 
308e2 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  code should not 
308e3 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73  attempt to acces
308e4 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20  s or modify the 
308e5 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73  fields of this s
308e6 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65  tructure.** dire
308e7 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68  ctly.  Change th
308e8 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c  is structure onl
308e9 79 20 62 79 20 75 73 69 6e 67 20 74 68 65 20 72  y by using the r
308ea 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a  outines below..*
308eb 2a 20 48 6f 77 65 76 65 72 2c 20 6d 61 6e 79 20  * However, many 
308ec 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64 75 72  of the "procedur
308ed 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f  es" and "functio
308ee 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e  ns" for modifyin
308ef 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69  g and.** accessi
308f0 6e 67 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ng this structur
308f1 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63  e are really mac
308f2 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74  ros, so we can't
308f3 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20   really make.** 
308f4 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
308f5 70 61 71 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  paque..*/.struct
308f6 20 66 74 73 33 48 61 73 68 20 7b 0a 20 20 63 68   fts3Hash {.  ch
308f7 61 72 20 6b 65 79 43 6c 61 73 73 3b 20 20 20 20  ar keyClass;    
308f8 20 20 20 20 20 20 2f 2a 20 48 41 53 48 5f 49 4e        /* HASH_IN
308f9 54 2c 20 5f 50 4f 49 4e 54 45 52 2c 20 5f 53 54  T, _POINTER, _ST
308fa 52 49 4e 47 2c 20 5f 42 49 4e 41 52 59 20 2a 2f  RING, _BINARY */
308fb 0a 20 20 63 68 61 72 20 63 6f 70 79 4b 65 79 3b  .  char copyKey;
308fc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
308fd 75 65 20 69 66 20 63 6f 70 79 20 6f 66 20 6b 65  ue if copy of ke
308fe 79 20 6d 61 64 65 20 6f 6e 20 69 6e 73 65 72 74  y made on insert
308ff 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b   */.  int count;
30900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30901 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
30902 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  es in this table
30903 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c   */.  fts3HashEl
30904 65 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 2f 2a  em *first;    /*
30905 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   The first eleme
30906 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  nt of the array 
30907 2a 2f 0a 20 20 69 6e 74 20 68 74 73 69 7a 65 3b  */.  int htsize;
30908 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30909 4e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74  Number of bucket
3090a 73 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  s in the hash ta
3090b 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ble */.  struct 
3090c 5f 66 74 73 33 68 74 20 7b 20 20 20 20 20 20 20  _fts3ht {       
3090d 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 62   /* the hash tab
3090e 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f  le */.    int co
3090f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  unt;            
30910 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
30911 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69  entries with thi
30912 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 66 74  s hash */.    ft
30913 73 33 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69  s3HashElem *chai
30914 6e 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  n;     /* Pointe
30915 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79  r to first entry
30916 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20   with this hash 
30917 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a  */.  } *ht;.};..
30918 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20  /* Each element 
30919 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
3091a 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
3091b 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
3091c 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  g .** structure.
3091d 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61    All elements a
3091e 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73  re stored on a s
3091f 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e  ingle doubly-lin
30920 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ked list..**.** 
30921 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75  Again, this stru
30922 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65  cture is intende
30923 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20  d to be opaque, 
30924 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 65 61  but it can't rea
30925 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65  lly.** be opaque
30926 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 75   because it is u
30927 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a  sed by macros..*
30928 2f 0a 73 74 72 75 63 74 20 66 74 73 33 48 61 73  /.struct fts3Has
30929 68 45 6c 65 6d 20 7b 0a 20 20 66 74 73 33 48 61  hElem {.  fts3Ha
3092a 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70  shElem *next, *p
3092b 72 65 76 3b 20 2f 2a 20 4e 65 78 74 20 61 6e 64  rev; /* Next and
3092c 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e   previous elemen
3092d 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ts in the table 
3092e 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b  */.  void *data;
3092f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30930 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69 61 74  /* Data associat
30931 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65  ed with this ele
30932 6d 65 6e 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ment */.  void *
30933 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20  pKey; int nKey; 
30934 20 20 20 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f       /* Key asso
30935 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
30936 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a   element */.};..
30937 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  /*.** There are 
30938 32 20 64 69 66 66 65 72 65 6e 74 20 6d 6f 64 65  2 different mode
30939 73 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 66  s of operation f
3093a 6f 72 20 61 20 68 61 73 68 20 74 61 62 6c 65 3a  or a hash table:
3093b 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41  .**.**   FTS3_HA
3093c 53 48 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20  SH_STRING       
3093d 20 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20   pKey points to 
3093e 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73  a string that is
3093f 20 6e 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e 67   nKey bytes long
30940 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
30941 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69                (i
30942 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6e 75 6c  ncluding the nul
30943 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66  l-terminator, if
30944 20 61 6e 79 29 2e 20 20 43 61 73 65 0a 2a 2a 20   any).  Case.** 
30945 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30946 20 20 20 20 20 20 20 20 20 20 69 73 20 72 65 73            is res
30947 70 65 63 74 65 64 20 69 6e 20 63 6f 6d 70 61 72  pected in compar
30948 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46  isons..**.**   F
30949 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20  TS3_HASH_BINARY 
3094a 20 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e         pKey poin
3094b 74 73 20 74 6f 20 62 69 6e 61 72 79 20 64 61 74  ts to binary dat
3094c 61 20 6e 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e  a nKey bytes lon
3094d 67 2e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  g. .**          
3094e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3094f 20 6d 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65   memcmp() is use
30950 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 6b 65 79  d to compare key
30951 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 70 79 20  s..**.** A copy 
30952 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 6d 61  of the key is ma
30953 64 65 20 69 66 20 74 68 65 20 63 6f 70 79 4b 65  de if the copyKe
30954 79 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 66  y parameter to f
30955 74 73 33 48 61 73 68 49 6e 69 74 20 69 73 20 31  ts3HashInit is 1
30956 2e 20 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  .  .*/.#define F
30957 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20  TS3_HASH_STRING 
30958 20 20 20 31 0a 23 64 65 66 69 6e 65 20 46 54 53     1.#define FTS
30959 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 20 20  3_HASH_BINARY   
3095a 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73   2../*.** Access
3095b 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64   routines.  To d
3095c 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20  elete, insert a 
3095d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
3095e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3095f 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
30960 48 61 73 68 49 6e 69 74 28 66 74 73 33 48 61 73  HashInit(fts3Has
30961 68 2a 2c 20 69 6e 74 20 6b 65 79 74 79 70 65 2c  h*, int keytype,
30962 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 3b 0a 53   int copyKey);.S
30963 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
30964 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
30965 61 73 68 49 6e 73 65 72 74 28 66 74 73 33 48 61  ashInsert(fts3Ha
30966 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  sh*, const void 
30967 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c  *pKey, int nKey,
30968 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53   void *pData);.S
30969 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3096a 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
3096b 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 66 74  ashFind(const ft
3096c 73 33 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76  s3Hash*, const v
3096d 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
3096e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Key);.SQLITE_PRI
3096f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
30970 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 66  3Fts3HashClear(f
30971 74 73 33 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a  ts3Hash*);../*.*
30972 2a 20 53 68 6f 72 74 68 61 6e 64 20 66 6f 72 20  * Shorthand for 
30973 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 62  the functions ab
30974 6f 76 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ove.*/.#define f
30975 74 73 33 48 61 73 68 49 6e 69 74 20 20 20 73 71  ts3HashInit   sq
30976 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69  lite3Fts3HashIni
30977 74 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  t.#define fts3Ha
30978 73 68 49 6e 73 65 72 74 20 73 71 6c 69 74 65 33  shInsert sqlite3
30979 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 0a 23  Fts3HashInsert.#
3097a 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 46  define fts3HashF
3097b 69 6e 64 20 20 20 73 71 6c 69 74 65 33 46 74 73  ind   sqlite3Fts
3097c 33 48 61 73 68 46 69 6e 64 0a 23 64 65 66 69 6e  3HashFind.#defin
3097d 65 20 66 74 73 33 48 61 73 68 43 6c 65 61 72 20  e fts3HashClear 
3097e 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
3097f 43 6c 65 61 72 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  Clear../*.** Mac
30980 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20  ros for looping 
30981 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  over all element
30982 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c  s of a hash tabl
30983 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73  e.  The idiom is
30984 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  .** like this:.*
30985 2a 0a 2a 2a 20 20 20 66 74 73 33 48 61 73 68 20  *.**   fts3Hash 
30986 68 3b 0a 2a 2a 20 20 20 66 74 73 33 48 61 73 68  h;.**   fts3Hash
30987 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e  Elem *p;.**   ..
30988 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d 66 74 73  ..**   for(p=fts
30989 33 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20  3HashFirst(&h); 
3098a 70 3b 20 70 3d 66 74 73 33 48 61 73 68 4e 65 78  p; p=fts3HashNex
3098b 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f  t(p)){.**     So
3098c 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44 61  meStructure *pDa
3098d 74 61 20 3d 20 66 74 73 33 48 61 73 68 44 61 74  ta = fts3HashDat
3098e 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20  a(p);.**     // 
3098f 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74  do something wit
30990 68 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a  h pData.**   }.*
30991 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
30992 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48 29  shFirst(H)  ((H)
30993 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65  ->first).#define
30994 20 66 74 73 33 48 61 73 68 4e 65 78 74 28 45 29   fts3HashNext(E)
30995 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23     ((E)->next).#
30996 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 44  define fts3HashD
30997 61 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64  ata(E)   ((E)->d
30998 61 74 61 29 0a 23 64 65 66 69 6e 65 20 66 74 73  ata).#define fts
30999 33 48 61 73 68 4b 65 79 28 45 29 20 20 20 20 28  3HashKey(E)    (
3099a 28 45 29 2d 3e 70 4b 65 79 29 0a 23 64 65 66 69  (E)->pKey).#defi
3099b 6e 65 20 66 74 73 33 48 61 73 68 4b 65 79 73 69  ne fts3HashKeysi
3099c 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79  ze(E) ((E)->nKey
3099d 29 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20  )../*.** Number 
3099e 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20  of entries in a 
3099f 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64  hash table.*/.#d
309a0 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 43 6f  efine fts3HashCo
309a1 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f  unt(H)  ((H)->co
309a2 75 6e 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  unt)..#endif /* 
309a3 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 20 2a 2f  _FTS3_HASH_H_ */
309a4 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
309a5 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61  * End of fts3_ha
309a6 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
309a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
309a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
309a9 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
309aa 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
309ab 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
309ac 6e 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  n fts3.c *******
309ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
309ae 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
309af 5f 43 4f 52 45 20 0a 20 20 53 51 4c 49 54 45 5f  _CORE .  SQLITE_
309b0 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
309b1 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 54 4f 44 4f  #endif.../* TODO
309b2 28 73 68 65 73 73 29 20 4d 41 4e 2c 20 74 68 69  (shess) MAN, thi
309b3 73 20 74 68 69 6e 67 20 6e 65 65 64 73 20 73 6f  s thing needs so
309b4 6d 65 20 72 65 66 61 63 74 6f 72 69 6e 67 2e 20  me refactoring. 
309b5 20 41 74 20 6d 69 6e 69 6d 75 6d 2c 20 69 74 0a   At minimum, it.
309b6 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65  ** would be nice
309b7 20 74 6f 20 6f 72 64 65 72 20 74 68 65 20 66 69   to order the fi
309b8 6c 65 20 62 65 74 74 65 72 2c 20 70 65 72 68 61  le better, perha
309b9 70 73 20 73 6f 6d 65 74 68 69 6e 67 20 61 6c 6f  ps something alo
309ba 6e 67 20 74 68 65 0a 2a 2a 20 6c 69 6e 65 73 20  ng the.** lines 
309bb 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 2d 20 75 74 69  of:.**.**  - uti
309bc 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  lity functions.*
309bd 2a 20 20 2d 20 74 61 62 6c 65 20 73 65 74 75 70  *  - table setup
309be 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d   functions.**  -
309bf 20 74 61 62 6c 65 20 75 70 64 61 74 65 20 66 75   table update fu
309c0 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61  nctions.**  - ta
309c1 62 6c 65 20 71 75 65 72 79 20 66 75 6e 63 74 69  ble query functi
309c2 6f 6e 73 0a 2a 2a 0a 2a 2a 20 50 75 74 20 74 68  ons.**.** Put th
309c3 65 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e  e query function
309c4 73 20 6c 61 73 74 20 62 65 63 61 75 73 65 20 74  s last because t
309c5 68 65 79 27 72 65 20 6c 69 6b 65 6c 79 20 74 6f  hey're likely to
309c6 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 79   reference.** ty
309c7 70 65 64 65 66 73 20 6f 72 20 66 75 6e 63 74 69  pedefs or functi
309c8 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62  ons from the tab
309c9 6c 65 20 75 70 64 61 74 65 20 73 65 63 74 69 6f  le update sectio
309ca 6e 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 23 20 64  n..*/..#if 0.# d
309cb 65 66 69 6e 65 20 46 54 53 54 52 41 43 45 28 41  efine FTSTRACE(A
309cc 29 20 20 70 72 69 6e 74 66 20 41 3b 20 66 66 6c  )  printf A; ffl
309cd 75 73 68 28 73 74 64 6f 75 74 29 0a 23 65 6c 73  ush(stdout).#els
309ce 65 0a 23 20 64 65 66 69 6e 65 20 46 54 53 54 52  e.# define FTSTR
309cf 41 43 45 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f  ACE(A).#endif../
309d0 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65  * It is not safe
309d1 20 74 6f 20 63 61 6c 6c 20 69 73 73 70 61 63 65   to call isspace
309d2 28 29 2c 20 74 6f 6c 6f 77 65 72 28 29 2c 20 6f  (), tolower(), o
309d3 72 20 69 73 61 6c 6e 75 6d 28 29 20 6f 6e 0a 2a  r isalnum() on.*
309d4 2a 20 68 69 2d 62 69 74 2d 73 65 74 20 63 68 61  * hi-bit-set cha
309d5 72 61 63 74 65 72 73 2e 20 20 54 68 69 73 20 69  racters.  This i
309d6 73 20 74 68 65 20 73 61 6d 65 20 73 6f 6c 75 74  s the same solut
309d7 69 6f 6e 20 75 73 65 64 20 69 6e 20 74 68 65 0a  ion used in the.
309d8 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f  ** tokenizer..*/
309d9 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
309da 54 68 65 20 73 6e 69 70 70 65 74 2d 67 65 6e 65  The snippet-gene
309db 72 61 74 69 6f 6e 20 63 6f 64 65 20 73 68 6f 75  ration code shou
309dc 6c 64 20 62 65 20 75 73 69 6e 67 20 74 68 65 0a  ld be using the.
309dd 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 2d 67 65 6e  ** tokenizer-gen
309de 65 72 61 74 65 64 20 74 6f 6b 65 6e 73 20 72 61  erated tokens ra
309df 74 68 65 72 20 74 68 61 6e 20 64 6f 69 6e 67 20  ther than doing 
309e0 69 74 73 20 6f 77 6e 20 6c 6f 63 61 6c 0a 2a 2a  its own local.**
309e1 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a   tokenization..*
309e2 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
309e3 20 49 73 20 5f 5f 69 73 61 73 63 69 69 28 29 20   Is __isascii() 
309e4 61 20 70 6f 72 74 61 62 6c 65 20 76 65 72 73 69  a portable versi
309e5 6f 6e 20 6f 66 20 28 63 26 30 78 38 30 29 3d 3d  on of (c&0x80)==
309e6 30 3f 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  0? */.static int
309e7 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 63 68   safe_isspace(ch
309e8 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  ar c){.  return 
309e9 28 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73  (c&0x80)==0 ? is
309ea 73 70 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a  space(c) : 0;.}.
309eb 73 74 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f  static int safe_
309ec 74 6f 6c 6f 77 65 72 28 63 68 61 72 20 63 29 7b  tolower(char c){
309ed 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38  .  return (c&0x8
309ee 30 29 3d 3d 30 20 3f 20 74 6f 6c 6f 77 65 72 28  0)==0 ? tolower(
309ef 63 29 20 3a 20 63 3b 0a 7d 0a 73 74 61 74 69 63  c) : c;.}.static
309f0 20 69 6e 74 20 73 61 66 65 5f 69 73 61 6c 6e 75   int safe_isalnu
309f1 6d 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74  m(char c){.  ret
309f2 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20  urn (c&0x80)==0 
309f3 3f 20 69 73 61 6c 6e 75 6d 28 63 29 20 3a 20 30  ? isalnum(c) : 0
309f4 3b 0a 7d 0a 0a 74 79 70 65 64 65 66 20 65 6e 75  ;.}..typedef enu
309f5 6d 20 44 6f 63 4c 69 73 74 54 79 70 65 20 7b 0a  m DocListType {.
309f6 20 20 44 4c 5f 44 4f 43 49 44 53 2c 20 20 20 20    DL_DOCIDS,    
309f7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 6f 63            /* doc
309f8 69 64 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 44 4c  ids only */.  DL
309f9 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 20 20 20 20  _POSITIONS,     
309fa 20 20 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20        /* docids 
309fb 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20  + positions */. 
309fc 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46   DL_POSITIONS_OF
309fd 46 53 45 54 53 20 20 20 20 2f 2a 20 64 6f 63 69  FSETS    /* doci
309fe 64 73 20 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2b  ds + positions +
309ff 20 6f 66 66 73 65 74 73 20 2a 2f 0a 7d 20 44 6f   offsets */.} Do
30a00 63 4c 69 73 74 54 79 70 65 3b 0a 0a 2f 2a 0a 2a  cListType;../*.*
30a01 2a 20 42 79 20 64 65 66 61 75 6c 74 2c 20 6f 6e  * By default, on
30a02 6c 79 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64  ly positions and
30a03 20 6e 6f 74 20 6f 66 66 73 65 74 73 20 61 72 65   not offsets are
30a04 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
30a05 6f 63 6c 69 73 74 73 2e 0a 2a 2a 20 54 6f 20 63  oclists..** To c
30a06 68 61 6e 67 65 20 74 68 69 73 20 73 6f 20 74 68  hange this so th
30a07 61 74 20 6f 66 66 73 65 74 73 20 61 72 65 20 73  at offsets are s
30a08 74 6f 72 65 64 20 74 6f 6f 2c 20 63 6f 6d 70 69  tored too, compi
30a09 6c 65 20 77 69 74 68 0a 2a 2a 0a 2a 2a 20 20 20  le with.**.**   
30a0a 20 20 20 20 20 20 20 2d 44 44 4c 5f 44 45 46 41         -DDL_DEFA
30a0b 55 4c 54 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  ULT=DL_POSITIONS
30a0c 5f 4f 46 46 53 45 54 53 0a 2a 2a 0a 2a 2a 20 49  _OFFSETS.**.** I
30a0d 66 20 44 4c 5f 44 45 46 41 55 4c 54 20 69 73 20  f DL_DEFAULT is 
30a0e 73 65 74 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53  set to DL_DOCIDS
30a0f 2c 20 79 6f 75 72 20 74 61 62 6c 65 20 63 61 6e  , your table can
30a10 20 6f 6e 6c 79 20 62 65 20 69 6e 73 65 72 74 65   only be inserte
30a11 64 0a 2a 2a 20 69 6e 74 6f 20 28 6e 6f 20 64 65  d.** into (no de
30a12 6c 65 74 65 73 20 6f 72 20 75 70 64 61 74 65 73  letes or updates
30a13 29 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 44 4c  )..*/.#ifndef DL
30a14 5f 44 45 46 41 55 4c 54 0a 23 20 64 65 66 69 6e  _DEFAULT.# defin
30a15 65 20 44 4c 5f 44 45 46 41 55 4c 54 20 44 4c 5f  e DL_DEFAULT DL_
30a16 50 4f 53 49 54 49 4f 4e 53 0a 23 65 6e 64 69 66  POSITIONS.#endif
30a17 0a 0a 65 6e 75 6d 20 7b 0a 20 20 50 4f 53 5f 45  ..enum {.  POS_E
30a18 4e 44 20 3d 20 30 2c 20 20 20 20 20 20 20 20 2f  ND = 0,        /
30a19 2a 20 65 6e 64 20 6f 66 20 74 68 69 73 20 70 6f  * end of this po
30a1a 73 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20  sition list */. 
30a1b 20 50 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 20 20 20   POS_COLUMN,    
30a1c 20 20 20 20 20 2f 2a 20 66 6f 6c 6c 6f 77 65 64       /* followed
30a1d 20 62 79 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e   by new column n
30a1e 75 6d 62 65 72 20 2a 2f 0a 20 20 50 4f 53 5f 42  umber */.  POS_B
30a1f 41 53 45 0a 7d 3b 0a 0a 2f 2a 20 4d 45 52 47 45  ASE.};../* MERGE
30a20 5f 43 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20  _COUNT controls 
30a21 68 6f 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72  how often we mer
30a22 67 65 20 73 65 67 6d 65 6e 74 73 20 28 73 65 65  ge segments (see
30a23 20 63 6f 6d 6d 65 6e 74 20 61 74 0a 2a 2a 20 74   comment at.** t
30a24 6f 70 20 6f 66 20 66 69 6c 65 29 2e 0a 2a 2f 0a  op of file)..*/.
30a25 23 64 65 66 69 6e 65 20 4d 45 52 47 45 5f 43 4f  #define MERGE_CO
30a26 55 4e 54 20 31 36 0a 0a 2f 2a 20 75 74 69 6c 69  UNT 16../* utili
30a27 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  ty functions */.
30a28 0a 2f 2a 20 43 4c 45 41 52 28 29 20 61 6e 64 20  ./* CLEAR() and 
30a29 53 43 52 41 4d 42 4c 45 28 29 20 61 62 73 74 72  SCRAMBLE() abstr
30a2a 61 63 74 20 6d 65 6d 73 65 74 28 29 20 6f 6e 20  act memset() on 
30a2b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
30a2c 69 6e 67 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20  ingle.** record 
30a2d 74 6f 20 70 72 65 76 65 6e 74 20 65 72 72 6f 72  to prevent error
30a2e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
30a2f 2a 0a 2a 2a 20 6d 79 5f 66 75 6e 63 74 69 6f 6e  *.** my_function
30a30 28 53 6f 6d 65 54 79 70 65 20 2a 62 29 7b 0a 2a  (SomeType *b){.*
30a31 2a 20 20 20 6d 65 6d 73 65 74 28 62 2c 20 27 5c  *   memset(b, '\
30a32 30 27 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 20  0', sizeof(b)); 
30a33 20 2f 2f 20 73 69 7a 65 6f 66 28 62 29 21 3d 73   // sizeof(b)!=s
30a34 69 7a 65 6f 66 28 2a 62 29 0a 2a 2a 20 7d 0a 2a  izeof(*b).** }.*
30a35 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
30a36 20 4f 62 76 69 6f 75 73 20 63 61 6e 64 69 64 61   Obvious candida
30a37 74 65 73 20 66 6f 72 20 61 20 68 65 61 64 65 72  tes for a header
30a38 20 66 69 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e   file. */.#defin
30a39 65 20 43 4c 45 41 52 28 62 29 20 6d 65 6d 73 65  e CLEAR(b) memse
30a3a 74 28 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f  t(b, '\0', sizeo
30a3b 66 28 2a 28 62 29 29 29 0a 0a 23 69 66 6e 64 65  f(*(b)))..#ifnde
30a3c 66 20 4e 44 45 42 55 47 0a 23 20 20 64 65 66 69  f NDEBUG.#  defi
30a3d 6e 65 20 53 43 52 41 4d 42 4c 45 28 62 29 20 6d  ne SCRAMBLE(b) m
30a3e 65 6d 73 65 74 28 62 2c 20 30 78 35 35 2c 20 73  emset(b, 0x55, s
30a3f 69 7a 65 6f 66 28 2a 28 62 29 29 29 0a 23 65 6c  izeof(*(b))).#el
30a40 73 65 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52  se.#  define SCR
30a41 41 4d 42 4c 45 28 62 29 0a 23 65 6e 64 69 66 0a  AMBLE(b).#endif.
30a42 0a 2f 2a 20 57 65 20 6d 61 79 20 6e 65 65 64 20  ./* We may need 
30a43 75 70 20 74 6f 20 56 41 52 49 4e 54 5f 4d 41 58  up to VARINT_MAX
30a44 20 62 79 74 65 73 20 74 6f 20 73 74 6f 72 65 20   bytes to store 
30a45 61 6e 20 65 6e 63 6f 64 65 64 20 36 34 2d 62 69  an encoded 64-bi
30a46 74 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 23 64  t integer. */.#d
30a47 65 66 69 6e 65 20 56 41 52 49 4e 54 5f 4d 41 58  efine VARINT_MAX
30a48 20 31 30 0a 0a 2f 2a 20 57 72 69 74 65 20 61 20   10../* Write a 
30a49 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d  64-bit variable-
30a4a 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74  length integer t
30a4b 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e  o memory startin
30a4c 67 20 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 54 68  g at p[0].. * Th
30a4d 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61  e length of data
30a4e 20 77 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65   written will be
30a4f 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 56   between 1 and V
30a50 41 52 49 4e 54 5f 4d 41 58 20 62 79 74 65 73 2e  ARINT_MAX bytes.
30a51 0a 20 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  . * The number o
30a52 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20  f bytes written 
30a53 69 73 20 72 65 74 75 72 6e 65 64 2e 20 2a 2f 0a  is returned. */.
30a54 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50  static int fts3P
30a55 75 74 56 61 72 69 6e 74 28 63 68 61 72 20 2a 70  utVarint(char *p
30a56 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76  , sqlite_int64 v
30a57 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
30a58 61 72 20 2a 71 20 3d 20 28 75 6e 73 69 67 6e 65  ar *q = (unsigne
30a59 64 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73  d char *) p;.  s
30a5a 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 76 75 20  qlite_uint64 vu 
30a5b 3d 20 76 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 2a  = v;.  do{.    *
30a5c 71 2b 2b 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  q++ = (unsigned 
30a5d 63 68 61 72 29 20 28 28 76 75 20 26 20 30 78 37  char) ((vu & 0x7
30a5e 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20  f) | 0x80);.    
30a5f 76 75 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69  vu >>= 7;.  }whi
30a60 6c 65 28 20 76 75 21 3d 30 20 29 3b 0a 20 20 71  le( vu!=0 );.  q
30a61 5b 2d 31 5d 20 26 3d 20 30 78 37 66 3b 20 20 2f  [-1] &= 0x7f;  /
30a62 2a 20 74 75 72 6e 20 6f 66 66 20 68 69 67 68 20  * turn off high 
30a63 62 69 74 20 69 6e 20 66 69 6e 61 6c 20 62 79 74  bit in final byt
30a64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 71  e */.  assert( q
30a65 20 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   - (unsigned cha
30a66 72 20 2a 29 70 20 3c 3d 20 56 41 52 49 4e 54 5f  r *)p <= VARINT_
30a67 4d 41 58 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  MAX );.  return 
30a68 28 69 6e 74 29 20 28 71 20 2d 20 28 75 6e 73 69  (int) (q - (unsi
30a69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 3b 0a  gned char *)p);.
30a6a 7d 0a 0a 2f 2a 20 52 65 61 64 20 61 20 36 34 2d  }../* Read a 64-
30a6b 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
30a6c 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  gth integer from
30a6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
30a6e 20 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 52 65 74   at p[0].. * Ret
30a6f 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
30a70 66 20 62 79 74 65 73 20 72 65 61 64 2c 20 6f 72  f bytes read, or
30a71 20 30 20 6f 6e 20 65 72 72 6f 72 2e 0a 20 2a 20   0 on error.. * 
30a72 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f  The value is sto
30a73 72 65 64 20 69 6e 20 2a 76 2e 20 2a 2f 0a 73 74  red in *v. */.st
30a74 61 74 69 63 20 69 6e 74 20 66 74 73 33 47 65 74  atic int fts3Get
30a75 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 63 68 61  Varint(const cha
30a76 72 20 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e 74  r *p, sqlite_int
30a77 36 34 20 2a 76 29 7b 0a 20 20 63 6f 6e 73 74 20  64 *v){.  const 
30a78 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71  unsigned char *q
30a79 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e   = (const unsign
30a7a 65 64 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20  ed char *) p;.  
30a7b 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 78 20  sqlite_uint64 x 
30a7c 3d 20 30 2c 20 79 20 3d 20 31 3b 0a 20 20 77 68  = 0, y = 1;.  wh
30a7d 69 6c 65 28 20 28 2a 71 20 26 20 30 78 38 30 29  ile( (*q & 0x80)
30a7e 20 3d 3d 20 30 78 38 30 20 29 7b 0a 20 20 20 20   == 0x80 ){.    
30a7f 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 20 26  x += y * (*q++ &
30a80 20 30 78 37 66 29 3b 0a 20 20 20 20 79 20 3c 3c   0x7f);.    y <<
30a81 3d 20 37 3b 0a 20 20 20 20 69 66 28 20 71 20 2d  = 7;.    if( q -
30a82 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
30a83 2a 29 70 20 3e 3d 20 56 41 52 49 4e 54 5f 4d 41  *)p >= VARINT_MA
30a84 58 20 29 7b 20 20 2f 2a 20 62 61 64 20 64 61 74  X ){  /* bad dat
30a85 61 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  a */.      asser
30a86 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 72 65  t( 0 );.      re
30a87 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
30a88 7d 0a 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71  }.  x += y * (*q
30a89 2b 2b 29 3b 0a 20 20 2a 76 20 3d 20 28 73 71 6c  ++);.  *v = (sql
30a8a 69 74 65 5f 69 6e 74 36 34 29 20 78 3b 0a 20 20  ite_int64) x;.  
30a8b 72 65 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20  return (int) (q 
30a8c 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  - (unsigned char
30a8d 20 2a 29 70 29 3b 0a 7d 0a 0a 73 74 61 74 69 63   *)p);.}..static
30a8e 20 69 6e 74 20 66 74 73 33 47 65 74 56 61 72 69   int fts3GetVari
30a8f 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20  nt32(const char 
30a90 2a 70 2c 20 69 6e 74 20 2a 70 69 29 7b 0a 20 73  *p, int *pi){. s
30a91 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 3b 0a 20  qlite_int64 i;. 
30a92 69 6e 74 20 72 65 74 20 3d 20 66 74 73 33 47 65  int ret = fts3Ge
30a93 74 56 61 72 69 6e 74 28 70 2c 20 26 69 29 3b 0a  tVarint(p, &i);.
30a94 20 2a 70 69 20 3d 20 28 69 6e 74 29 20 69 3b 0a   *pi = (int) i;.
30a95 20 61 73 73 65 72 74 28 20 2a 70 69 3d 3d 69 20   assert( *pi==i 
30a96 29 3b 0a 20 72 65 74 75 72 6e 20 72 65 74 3b 0a  );. return ret;.
30a97 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
30a98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30a99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30a9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30a9b 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 61 74 61  *******/./* Data
30a9c 42 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74  Buffer is used t
30a9d 6f 20 63 6f 6c 6c 65 63 74 20 64 61 74 61 20 69  o collect data i
30a9e 6e 74 6f 20 61 20 62 75 66 66 65 72 20 69 6e 20  nto a buffer in 
30a9f 70 69 65 63 65 6d 65 61 6c 0a 2a 2a 20 66 61 73  piecemeal.** fas
30aa0 68 69 6f 6e 2e 20 20 49 74 20 69 6d 70 6c 65 6d  hion.  It implem
30aa1 65 6e 74 73 20 74 68 65 20 75 73 75 61 6c 20 64  ents the usual d
30aa2 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65  istinction betwe
30aa3 65 6e 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20  en amount of.** 
30aa4 64 61 74 61 20 63 75 72 72 65 6e 74 6c 79 20 73  data currently s
30aa5 74 6f 72 65 64 20 28 6e 44 61 74 61 29 20 61 6e  tored (nData) an
30aa6 64 20 62 75 66 66 65 72 20 63 61 70 61 63 69 74  d buffer capacit
30aa7 79 20 28 6e 43 61 70 61 63 69 74 79 29 2e 0a 2a  y (nCapacity)..*
30aa8 2a 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 49  *.** dataBufferI
30aa9 6e 69 74 20 2d 20 63 72 65 61 74 65 20 61 20 62  nit - create a b
30aaa 75 66 66 65 72 20 77 69 74 68 20 67 69 76 65 6e  uffer with given
30aab 20 69 6e 69 74 69 61 6c 20 63 61 70 61 63 69 74   initial capacit
30aac 79 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72  y..** dataBuffer
30aad 52 65 73 65 74 20 2d 20 66 6f 72 67 65 74 20 62  Reset - forget b
30aae 75 66 66 65 72 27 73 20 64 61 74 61 2c 20 72 65  uffer's data, re
30aaf 74 61 69 6e 69 6e 67 20 63 61 70 61 63 69 74 79  taining capacity
30ab0 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 44  ..** dataBufferD
30ab1 65 73 74 72 6f 79 20 2d 20 66 72 65 65 20 62 75  estroy - free bu
30ab2 66 66 65 72 27 73 20 64 61 74 61 2e 0a 2a 2a 20  ffer's data..** 
30ab3 64 61 74 61 42 75 66 66 65 72 53 77 61 70 20 2d  dataBufferSwap -
30ab4 20 73 77 61 70 20 63 6f 6e 74 65 6e 74 73 20 6f   swap contents o
30ab5 66 20 74 77 6f 20 62 75 66 66 65 72 73 2e 0a 2a  f two buffers..*
30ab6 2a 20 64 61 74 61 42 75 66 66 65 72 45 78 70 61  * dataBufferExpa
30ab7 6e 64 20 2d 20 65 78 70 61 6e 64 20 63 61 70 61  nd - expand capa
30ab8 63 69 74 79 20 77 69 74 68 6f 75 74 20 61 64 64  city without add
30ab9 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74  ing data..** dat
30aba 61 42 75 66 66 65 72 41 70 70 65 6e 64 20 2d 20  aBufferAppend - 
30abb 61 70 70 65 6e 64 20 64 61 74 61 2e 0a 2a 2a 20  append data..** 
30abc 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
30abd 32 20 2d 20 61 70 70 65 6e 64 20 74 77 6f 20 70  2 - append two p
30abe 69 65 63 65 73 20 6f 66 20 64 61 74 61 20 61 74  ieces of data at
30abf 20 6f 6e 63 65 2e 0a 2a 2a 20 64 61 74 61 42 75   once..** dataBu
30ac0 66 66 65 72 52 65 70 6c 61 63 65 20 2d 20 72 65  fferReplace - re
30ac1 70 6c 61 63 65 20 62 75 66 66 65 72 27 73 20 64  place buffer's d
30ac2 61 74 61 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ata..*/.typedef 
30ac3 73 74 72 75 63 74 20 44 61 74 61 42 75 66 66 65  struct DataBuffe
30ac4 72 20 7b 0a 20 20 63 68 61 72 20 2a 70 44 61 74  r {.  char *pDat
30ac5 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  a;          /* P
30ac6 6f 69 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63  ointer to malloc
30ac7 27 65 64 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20  'ed buffer. */. 
30ac8 20 69 6e 74 20 6e 43 61 70 61 63 69 74 79 3b 20   int nCapacity; 
30ac9 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
30aca 66 20 70 44 61 74 61 20 62 75 66 66 65 72 2e 20  f pData buffer. 
30acb 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20  */.  int nData; 
30acc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
30acd 64 20 6f 66 20 64 61 74 61 20 6c 6f 61 64 65 64  d of data loaded
30ace 20 69 6e 74 6f 20 70 44 61 74 61 2e 20 2a 2f 0a   into pData. */.
30acf 7d 20 44 61 74 61 42 75 66 66 65 72 3b 0a 0a 73  } DataBuffer;..s
30ad0 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
30ad1 75 66 66 65 72 49 6e 69 74 28 44 61 74 61 42 75  ufferInit(DataBu
30ad2 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69  ffer *pBuffer, i
30ad3 6e 74 20 6e 43 61 70 61 63 69 74 79 29 7b 0a 20  nt nCapacity){. 
30ad4 20 61 73 73 65 72 74 28 20 6e 43 61 70 61 63 69   assert( nCapaci
30ad5 74 79 3e 3d 30 20 29 3b 0a 20 20 70 42 75 66 66  ty>=0 );.  pBuff
30ad6 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20  er->nData = 0;. 
30ad7 20 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63   pBuffer->nCapac
30ad8 69 74 79 20 3d 20 6e 43 61 70 61 63 69 74 79 3b  ity = nCapacity;
30ad9 0a 20 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74  .  pBuffer->pDat
30ada 61 20 3d 20 6e 43 61 70 61 63 69 74 79 3d 3d 30  a = nCapacity==0
30adb 20 3f 20 4e 55 4c 4c 20 3a 20 73 71 6c 69 74 65   ? NULL : sqlite
30adc 33 5f 6d 61 6c 6c 6f 63 28 6e 43 61 70 61 63 69  3_malloc(nCapaci
30add 74 79 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ty);.}.static vo
30ade 69 64 20 64 61 74 61 42 75 66 66 65 72 52 65 73  id dataBufferRes
30adf 65 74 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  et(DataBuffer *p
30ae0 42 75 66 66 65 72 29 7b 0a 20 20 70 42 75 66 66  Buffer){.  pBuff
30ae1 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 7d  er->nData = 0;.}
30ae2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
30ae3 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 44  aBufferDestroy(D
30ae4 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
30ae5 65 72 29 7b 0a 20 20 69 66 28 20 70 42 75 66 66  er){.  if( pBuff
30ae6 65 72 2d 3e 70 44 61 74 61 21 3d 4e 55 4c 4c 20  er->pData!=NULL 
30ae7 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70  ) sqlite3_free(p
30ae8 42 75 66 66 65 72 2d 3e 70 44 61 74 61 29 3b 0a  Buffer->pData);.
30ae9 20 20 53 43 52 41 4d 42 4c 45 28 70 42 75 66 66    SCRAMBLE(pBuff
30aea 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
30aeb 69 64 20 64 61 74 61 42 75 66 66 65 72 53 77 61  id dataBufferSwa
30aec 70 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42  p(DataBuffer *pB
30aed 75 66 66 65 72 31 2c 20 44 61 74 61 42 75 66 66  uffer1, DataBuff
30aee 65 72 20 2a 70 42 75 66 66 65 72 32 29 7b 0a 20  er *pBuffer2){. 
30aef 20 44 61 74 61 42 75 66 66 65 72 20 74 6d 70 20   DataBuffer tmp 
30af0 3d 20 2a 70 42 75 66 66 65 72 31 3b 0a 20 20 2a  = *pBuffer1;.  *
30af1 70 42 75 66 66 65 72 31 20 3d 20 2a 70 42 75 66  pBuffer1 = *pBuf
30af2 66 65 72 32 3b 0a 20 20 2a 70 42 75 66 66 65 72  fer2;.  *pBuffer
30af3 32 20 3d 20 74 6d 70 3b 0a 7d 0a 73 74 61 74 69  2 = tmp;.}.stati
30af4 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
30af5 72 45 78 70 61 6e 64 28 44 61 74 61 42 75 66 66  rExpand(DataBuff
30af6 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74  er *pBuffer, int
30af7 20 6e 41 64 64 43 61 70 61 63 69 74 79 29 7b 0a   nAddCapacity){.
30af8 20 20 61 73 73 65 72 74 28 20 6e 41 64 64 43 61    assert( nAddCa
30af9 70 61 63 69 74 79 3e 30 20 29 3b 0a 20 20 2f 2a  pacity>0 );.  /*
30afa 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
30afb 73 69 64 65 72 20 65 78 70 61 6e 64 69 6e 67 20  sider expanding 
30afc 6d 6f 72 65 20 61 67 67 72 65 73 73 69 76 65 6c  more aggressivel
30afd 79 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  y.  Note that th
30afe 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e  e.  ** underlyin
30aff 67 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65  g malloc impleme
30b00 6e 74 61 74 69 6f 6e 20 6d 61 79 20 74 61 6b 65  ntation may take
30b01 20 63 61 72 65 20 6f 66 20 73 75 63 68 20 74 68   care of such th
30b02 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 75 73  ings for.  ** us
30b03 20 61 6c 72 65 61 64 79 2e 0a 20 20 2a 2f 0a 20   already..  */. 
30b04 20 69 66 28 20 70 42 75 66 66 65 72 2d 3e 6e 44   if( pBuffer->nD
30b05 61 74 61 2b 6e 41 64 64 43 61 70 61 63 69 74 79  ata+nAddCapacity
30b06 3e 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63  >pBuffer->nCapac
30b07 69 74 79 20 29 7b 0a 20 20 20 20 70 42 75 66 66  ity ){.    pBuff
30b08 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d 20  er->nCapacity = 
30b09 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e  pBuffer->nData+n
30b0a 41 64 64 43 61 70 61 63 69 74 79 3b 0a 20 20 20  AddCapacity;.   
30b0b 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20   pBuffer->pData 
30b0c 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
30b0d 63 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61  c(pBuffer->pData
30b0e 2c 20 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61  , pBuffer->nCapa
30b0f 63 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  city);.  }.}.sta
30b10 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
30b11 66 65 72 41 70 70 65 6e 64 28 44 61 74 61 42 75  ferAppend(DataBu
30b12 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20  ffer *pBuffer,. 
30b13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30b14 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
30b15 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c  t char *pSource,
30b16 20 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20   int nSource){. 
30b17 20 61 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65   assert( nSource
30b18 3e 30 20 26 26 20 70 53 6f 75 72 63 65 21 3d 4e  >0 && pSource!=N
30b19 55 4c 4c 20 29 3b 0a 20 20 64 61 74 61 42 75 66  ULL );.  dataBuf
30b1a 66 65 72 45 78 70 61 6e 64 28 70 42 75 66 66 65  ferExpand(pBuffe
30b1b 72 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 20 20 6d  r, nSource);.  m
30b1c 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70  emcpy(pBuffer->p
30b1d 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44  Data+pBuffer->nD
30b1e 61 74 61 2c 20 70 53 6f 75 72 63 65 2c 20 6e 53  ata, pSource, nS
30b1f 6f 75 72 63 65 29 3b 0a 20 20 70 42 75 66 66 65  ource);.  pBuffe
30b20 72 2d 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75  r->nData += nSou
30b21 72 63 65 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  rce;.}.static vo
30b22 69 64 20 64 61 74 61 42 75 66 66 65 72 41 70 70  id dataBufferApp
30b23 65 6e 64 32 28 44 61 74 61 42 75 66 66 65 72 20  end2(DataBuffer 
30b24 2a 70 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20  *pBuffer,.      
30b25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30b26 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
30b27 61 72 20 2a 70 53 6f 75 72 63 65 31 2c 20 69 6e  ar *pSource1, in
30b28 74 20 6e 53 6f 75 72 63 65 31 2c 0a 20 20 20 20  t nSource1,.    
30b29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30b2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
30b2b 63 68 61 72 20 2a 70 53 6f 75 72 63 65 32 2c 20  char *pSource2, 
30b2c 69 6e 74 20 6e 53 6f 75 72 63 65 32 29 7b 0a 20  int nSource2){. 
30b2d 20 61 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65   assert( nSource
30b2e 31 3e 30 20 26 26 20 70 53 6f 75 72 63 65 31 21  1>0 && pSource1!
30b2f 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72  =NULL );.  asser
30b30 74 28 20 6e 53 6f 75 72 63 65 32 3e 30 20 26 26  t( nSource2>0 &&
30b31 20 70 53 6f 75 72 63 65 32 21 3d 4e 55 4c 4c 20   pSource2!=NULL 
30b32 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 45  );.  dataBufferE
30b33 78 70 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e  xpand(pBuffer, n
30b34 53 6f 75 72 63 65 31 2b 6e 53 6f 75 72 63 65 32  Source1+nSource2
30b35 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66  );.  memcpy(pBuf
30b36 66 65 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66  fer->pData+pBuff
30b37 65 72 2d 3e 6e 44 61 74 61 2c 20 70 53 6f 75 72  er->nData, pSour
30b38 63 65 31 2c 20 6e 53 6f 75 72 63 65 31 29 3b 0a  ce1, nSource1);.
30b39 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72    memcpy(pBuffer
30b3a 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d  ->pData+pBuffer-
30b3b 3e 6e 44 61 74 61 2b 6e 53 6f 75 72 63 65 31 2c  >nData+nSource1,
30b3c 20 70 53 6f 75 72 63 65 32 2c 20 6e 53 6f 75 72   pSource2, nSour
30b3d 63 65 32 29 3b 0a 20 20 70 42 75 66 66 65 72 2d  ce2);.  pBuffer-
30b3e 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63  >nData += nSourc
30b3f 65 31 2b 6e 53 6f 75 72 63 65 32 3b 0a 7d 0a 73  e1+nSource2;.}.s
30b40 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
30b41 75 66 66 65 72 52 65 70 6c 61 63 65 28 44 61 74  ufferReplace(Dat
30b42 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
30b43 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
30b44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30b45 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75  const char *pSou
30b46 72 63 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65  rce, int nSource
30b47 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72 52  ){.  dataBufferR
30b48 65 73 65 74 28 70 42 75 66 66 65 72 29 3b 0a 20  eset(pBuffer);. 
30b49 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
30b4a 64 28 70 42 75 66 66 65 72 2c 20 70 53 6f 75 72  d(pBuffer, pSour
30b4b 63 65 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 7d 0a  ce, nSource);.}.
30b4c 0a 2f 2a 20 53 74 72 69 6e 67 42 75 66 66 65 72  ./* StringBuffer
30b4d 20 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69   is a null-termi
30b4e 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  nated version of
30b4f 20 44 61 74 61 42 75 66 66 65 72 2e 20 2a 2f 0a   DataBuffer. */.
30b50 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
30b51 74 72 69 6e 67 42 75 66 66 65 72 20 7b 0a 20 20  tringBuffer {.  
30b52 44 61 74 61 42 75 66 66 65 72 20 62 3b 20 20 20  DataBuffer b;   
30b53 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 6c           /* Incl
30b54 75 64 65 73 20 6e 75 6c 6c 20 74 65 72 6d 69 6e  udes null termin
30b55 61 74 6f 72 2e 20 2a 2f 0a 7d 20 53 74 72 69 6e  ator. */.} Strin
30b56 67 42 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63  gBuffer;..static
30b57 20 76 6f 69 64 20 69 6e 69 74 53 74 72 69 6e 67   void initString
30b58 42 75 66 66 65 72 28 53 74 72 69 6e 67 42 75 66  Buffer(StringBuf
30b59 66 65 72 20 2a 73 62 29 7b 0a 20 20 64 61 74 61  fer *sb){.  data
30b5a 42 75 66 66 65 72 49 6e 69 74 28 26 73 62 2d 3e  BufferInit(&sb->
30b5b 62 2c 20 31 30 30 29 3b 0a 20 20 64 61 74 61 42  b, 100);.  dataB
30b5c 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 73 62  ufferReplace(&sb
30b5d 2d 3e 62 2c 20 22 22 2c 20 31 29 3b 0a 7d 0a 73  ->b, "", 1);.}.s
30b5e 74 61 74 69 63 20 69 6e 74 20 73 74 72 69 6e 67  tatic int string
30b5f 42 75 66 66 65 72 4c 65 6e 67 74 68 28 53 74 72  BufferLength(Str
30b60 69 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a  ingBuffer *sb){.
30b61 20 20 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 6e    return sb->b.n
30b62 44 61 74 61 2d 31 3b 0a 7d 0a 73 74 61 74 69 63  Data-1;.}.static
30b63 20 63 68 61 72 20 2a 73 74 72 69 6e 67 42 75 66   char *stringBuf
30b64 66 65 72 44 61 74 61 28 53 74 72 69 6e 67 42 75  ferData(StringBu
30b65 66 66 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74  ffer *sb){.  ret
30b66 75 72 6e 20 73 62 2d 3e 62 2e 70 44 61 74 61 3b  urn sb->b.pData;
30b67 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  .}.static void s
30b68 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74 72  tringBufferDestr
30b69 6f 79 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  oy(StringBuffer 
30b6a 2a 73 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66  *sb){.  dataBuff
30b6b 65 72 44 65 73 74 72 6f 79 28 26 73 62 2d 3e 62  erDestroy(&sb->b
30b6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
30b6d 64 20 6e 61 70 70 65 6e 64 28 53 74 72 69 6e 67  d nappend(String
30b6e 42 75 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73  Buffer *sb, cons
30b6f 74 20 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 69  t char *zFrom, i
30b70 6e 74 20 6e 46 72 6f 6d 29 7b 0a 20 20 61 73 73  nt nFrom){.  ass
30b71 65 72 74 28 20 73 62 2d 3e 62 2e 6e 44 61 74 61  ert( sb->b.nData
30b72 3e 30 20 29 3b 0a 20 20 69 66 28 20 6e 46 72 6f  >0 );.  if( nFro
30b73 6d 3e 30 20 29 7b 0a 20 20 20 20 73 62 2d 3e 62  m>0 ){.    sb->b
30b74 2e 6e 44 61 74 61 2d 2d 3b 0a 20 20 20 20 64 61  .nData--;.    da
30b75 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
30b76 26 73 62 2d 3e 62 2c 20 7a 46 72 6f 6d 2c 20 6e  &sb->b, zFrom, n
30b77 46 72 6f 6d 2c 20 22 22 2c 20 31 29 3b 0a 20 20  From, "", 1);.  
30b78 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
30b79 61 70 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66  append(StringBuf
30b7a 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63  fer *sb, const c
30b7b 68 61 72 20 2a 7a 46 72 6f 6d 29 7b 0a 20 20 6e  har *zFrom){.  n
30b7c 61 70 70 65 6e 64 28 73 62 2c 20 7a 46 72 6f 6d  append(sb, zFrom
30b7d 2c 20 73 74 72 6c 65 6e 28 7a 46 72 6f 6d 29 29  , strlen(zFrom))
30b7e 3b 0a 7d 0a 0a 2f 2a 20 41 70 70 65 6e 64 20 61  ;.}../* Append a
30b7f 20 6c 69 73 74 20 6f 66 20 73 74 72 69 6e 67 73   list of strings
30b80 20 73 65 70 61 72 61 74 65 64 20 62 79 20 63 6f   separated by co
30b81 6d 6d 61 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20  mmas. */.static 
30b82 76 6f 69 64 20 61 70 70 65 6e 64 4c 69 73 74 28  void appendList(
30b83 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62  StringBuffer *sb
30b84 2c 20 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20 63  , int nString, c
30b85 68 61 72 20 2a 2a 61 7a 53 74 72 69 6e 67 29 7b  har **azString){
30b86 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
30b87 69 3d 30 3b 20 69 3c 6e 53 74 72 69 6e 67 3b 20  i=0; i<nString; 
30b88 2b 2b 69 29 7b 0a 20 20 20 20 69 66 28 20 69 3e  ++i){.    if( i>
30b89 30 20 29 20 61 70 70 65 6e 64 28 73 62 2c 20 22  0 ) append(sb, "
30b8a 2c 20 22 29 3b 0a 20 20 20 20 61 70 70 65 6e 64  , ");.    append
30b8b 28 73 62 2c 20 61 7a 53 74 72 69 6e 67 5b 69 5d  (sb, azString[i]
30b8c 29 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63  );.  }.}..static
30b8d 20 69 6e 74 20 65 6e 64 73 49 6e 57 68 69 74 65   int endsInWhite
30b8e 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66 66  Space(StringBuff
30b8f 65 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  er *p){.  return
30b90 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e   stringBufferLen
30b91 67 74 68 28 70 29 3e 30 20 26 26 0a 20 20 20 20  gth(p)>0 &&.    
30b92 73 61 66 65 5f 69 73 73 70 61 63 65 28 73 74 72  safe_isspace(str
30b93 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 70 29  ingBufferData(p)
30b94 5b 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e  [stringBufferLen
30b95 67 74 68 28 70 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f  gth(p)-1]);.}../
30b96 2a 20 49 66 20 74 68 65 20 53 74 72 69 6e 67 42  * If the StringB
30b97 75 66 66 65 72 20 65 6e 64 73 20 69 6e 20 73 6f  uffer ends in so
30b98 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68  mething other th
30b99 61 6e 20 77 68 69 74 65 20 73 70 61 63 65 2c 20  an white space, 
30b9a 61 64 64 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20  add a.** single 
30b9b 73 70 61 63 65 20 63 68 61 72 61 63 74 65 72 20  space character 
30b9c 74 6f 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73  to the end..*/.s
30b9d 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e  tatic void appen
30b9e 64 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69  dWhiteSpace(Stri
30b9f 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20  ngBuffer *p){.  
30ba0 69 66 28 20 73 74 72 69 6e 67 42 75 66 66 65 72  if( stringBuffer
30ba1 4c 65 6e 67 74 68 28 70 29 3d 3d 30 20 29 20 72  Length(p)==0 ) r
30ba2 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 65 6e  eturn;.  if( !en
30ba3 64 73 49 6e 57 68 69 74 65 53 70 61 63 65 28 70  dsInWhiteSpace(p
30ba4 29 20 29 20 61 70 70 65 6e 64 28 70 2c 20 22 20  ) ) append(p, " 
30ba5 22 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65  ");.}../* Remove
30ba6 20 77 68 69 74 65 20 73 70 61 63 65 20 66 72 6f   white space fro
30ba7 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  m the end of the
30ba8 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 2f   StringBuffer */
30ba9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 69  .static void tri
30baa 6d 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69  mWhiteSpace(Stri
30bab 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20  ngBuffer *p){.  
30bac 77 68 69 6c 65 28 20 65 6e 64 73 49 6e 57 68 69  while( endsInWhi
30bad 74 65 53 70 61 63 65 28 70 29 20 29 7b 0a 20 20  teSpace(p) ){.  
30bae 20 20 70 2d 3e 62 2e 70 44 61 74 61 5b 2d 2d 70    p->b.pData[--p
30baf 2d 3e 62 2e 6e 44 61 74 61 2d 31 5d 20 3d 20 27  ->b.nData-1] = '
30bb0 5c 30 27 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a  \0';.  }.}../***
30bb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bb5 2f 0a 2f 2a 20 44 4c 52 65 61 64 65 72 20 69 73  /./* DLReader is
30bb6 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 6f   used to read do
30bb7 63 75 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20  cument elements 
30bb8 66 72 6f 6d 20 61 20 64 6f 63 6c 69 73 74 2e 20  from a doclist. 
30bb9 20 54 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   The.** current 
30bba 64 6f 63 69 64 20 69 73 20 63 61 63 68 65 64 2c  docid is cached,
30bbb 20 73 6f 20 64 6c 72 44 6f 63 69 64 28 29 20 69   so dlrDocid() i
30bbc 73 20 66 61 73 74 2e 20 20 44 4c 52 65 61 64 65  s fast.  DLReade
30bbd 72 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6f 77  r does not.** ow
30bbe 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 62 75  n the doclist bu
30bbf 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 64 6c 72 41  ffer..**.** dlrA
30bc0 74 45 6e 64 20 2d 20 74 72 75 65 20 69 66 20 74  tEnd - true if t
30bc1 68 65 72 65 27 73 20 6e 6f 20 6d 6f 72 65 20 64  here's no more d
30bc2 61 74 61 20 74 6f 20 72 65 61 64 2e 0a 2a 2a 20  ata to read..** 
30bc3 64 6c 72 44 6f 63 69 64 20 2d 20 64 6f 63 69 64  dlrDocid - docid
30bc4 20 6f 66 20 63 75 72 72 65 6e 74 20 64 6f 63 75   of current docu
30bc5 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44  ment..** dlrDocD
30bc6 61 74 61 20 2d 20 64 6f 63 6c 69 73 74 20 64 61  ata - doclist da
30bc7 74 61 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64  ta for current d
30bc8 6f 63 75 6d 65 6e 74 20 28 69 6e 63 6c 75 64 69  ocument (includi
30bc9 6e 67 20 64 6f 63 69 64 29 2e 0a 2a 2a 20 64 6c  ng docid)..** dl
30bca 72 44 6f 63 44 61 74 61 42 79 74 65 73 20 2d 20  rDocDataBytes - 
30bcb 6c 65 6e 67 74 68 20 6f 66 20 73 61 6d 65 2e 0a  length of same..
30bcc 2a 2a 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74  ** dlrAllDataByt
30bcd 65 73 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 61  es - length of a
30bce 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 64 61 74  ll remaining dat
30bcf 61 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61  a..** dlrPosData
30bd0 20 2d 20 70 6f 73 69 74 69 6f 6e 20 64 61 74 61   - position data
30bd1 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63   for current doc
30bd2 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 50 6f 73  ument..** dlrPos
30bd3 44 61 74 61 4c 65 6e 20 2d 20 6c 65 6e 67 74 68  DataLen - length
30bd4 20 6f 66 20 70 6f 73 20 64 61 74 61 20 66 6f 72   of pos data for
30bd5 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e   current documen
30bd6 74 20 28 69 6e 63 6c 20 50 4f 53 5f 45 4e 44 29  t (incl POS_END)
30bd7 2e 0a 2a 2a 20 64 6c 72 53 74 65 70 20 2d 20 73  ..** dlrStep - s
30bd8 74 65 70 20 74 6f 20 63 75 72 72 65 6e 74 20 64  tep to current d
30bd9 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 49  ocument..** dlrI
30bda 6e 69 74 20 2d 20 69 6e 69 74 69 61 6c 20 66 6f  nit - initial fo
30bdb 72 20 64 6f 63 6c 69 73 74 20 6f 66 20 67 69 76  r doclist of giv
30bdc 65 6e 20 74 79 70 65 20 61 67 61 69 6e 73 74 20  en type against 
30bdd 67 69 76 65 6e 20 64 61 74 61 2e 0a 2a 2a 20 64  given data..** d
30bde 6c 72 44 65 73 74 72 6f 79 20 2d 20 63 6c 65 61  lrDestroy - clea
30bdf 6e 20 75 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 65  n up..**.** Expe
30be0 63 74 65 64 20 75 73 61 67 65 20 69 73 20 73 6f  cted usage is so
30be1 6d 65 74 68 69 6e 67 20 6c 69 6b 65 3a 0a 2a 2a  mething like:.**
30be2 0a 2a 2a 20 20 20 44 4c 52 65 61 64 65 72 20 72  .**   DLReader r
30be3 65 61 64 65 72 3b 0a 2a 2a 20 20 20 64 6c 72 49  eader;.**   dlrI
30be4 6e 69 74 28 26 72 65 61 64 65 72 2c 20 70 44 61  nit(&reader, pDa
30be5 74 61 2c 20 6e 44 61 74 61 29 3b 0a 2a 2a 20 20  ta, nData);.**  
30be6 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e   while( !dlrAtEn
30be7 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a 2a 2a  d(&reader) ){.**
30be8 20 20 20 20 20 2f 2f 20 63 61 6c 6c 73 20 74 6f       // calls to
30be9 20 64 6c 72 44 6f 63 69 64 28 29 20 61 6e 64 20   dlrDocid() and 
30bea 6b 69 6e 2e 0a 2a 2a 20 20 20 20 20 64 6c 72 53  kin..**     dlrS
30beb 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a 2a 2a  tep(&reader);.**
30bec 20 20 20 7d 0a 2a 2a 20 20 20 64 6c 72 44 65 73     }.**   dlrDes
30bed 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 2a  troy(&reader);.*
30bee 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
30bef 20 44 4c 52 65 61 64 65 72 20 7b 0a 20 20 44 6f   DLReader {.  Do
30bf0 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 3b  cListType iType;
30bf1 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
30bf2 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74  Data;.  int nDat
30bf3 61 3b 0a 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  a;..  sqlite_int
30bf4 36 34 20 69 44 6f 63 69 64 3b 0a 20 20 69 6e 74  64 iDocid;.  int
30bf5 20 6e 45 6c 65 6d 65 6e 74 3b 0a 7d 20 44 4c 52   nElement;.} DLR
30bf6 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69  eader;..static i
30bf7 6e 74 20 64 6c 72 41 74 45 6e 64 28 44 4c 52 65  nt dlrAtEnd(DLRe
30bf8 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
30bf9 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
30bfa 72 2d 3e 6e 44 61 74 61 3e 3d 30 20 29 3b 0a 20  r->nData>=0 );. 
30bfb 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
30bfc 3e 6e 44 61 74 61 3d 3d 30 3b 0a 7d 0a 73 74 61  >nData==0;.}.sta
30bfd 74 69 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  tic sqlite_int64
30bfe 20 64 6c 72 44 6f 63 69 64 28 44 4c 52 65 61 64   dlrDocid(DLRead
30bff 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
30c00 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e  assert( !dlrAtEn
30c01 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
30c02 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
30c03 69 44 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63  iDocid;.}.static
30c04 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72   const char *dlr
30c05 44 6f 63 44 61 74 61 28 44 4c 52 65 61 64 65 72  DocData(DLReader
30c06 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
30c07 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28  sert( !dlrAtEnd(
30c08 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
30c09 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44  turn pReader->pD
30c0a 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  ata;.}.static in
30c0b 74 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65  t dlrDocDataByte
30c0c 73 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61  s(DLReader *pRea
30c0d 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
30c0e 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
30c0f 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
30c10 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74  Reader->nElement
30c11 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64  ;.}.static int d
30c12 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28 44  lrAllDataBytes(D
30c13 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
30c14 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  ){.  assert( !dl
30c15 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
30c16 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
30c17 64 65 72 2d 3e 6e 44 61 74 61 3b 0a 7d 0a 2f 2a  der->nData;.}./*
30c18 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
30c19 73 69 64 65 72 20 61 64 64 69 6e 67 20 61 20 66  sider adding a f
30c1a 69 65 6c 64 20 74 6f 20 74 72 61 63 6b 20 69 44  ield to track iD
30c1b 6f 63 69 64 20 76 61 72 69 6e 74 20 6c 65 6e 67  ocid varint leng
30c1c 74 68 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 74 68  th.** to make th
30c1d 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e  ese two function
30c1e 73 20 66 61 73 74 65 72 2e 20 20 54 68 69 73 20  s faster.  This 
30c1f 6d 69 67 68 74 20 6d 61 74 74 65 72 20 28 61 20  might matter (a 
30c20 74 69 6e 79 20 62 69 74 29 0a 2a 2a 20 66 6f 72  tiny bit).** for
30c21 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 73 74 61   queries..*/.sta
30c22 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
30c23 64 6c 72 50 6f 73 44 61 74 61 28 44 4c 52 65 61  dlrPosData(DLRea
30c24 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
30c25 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
30c26 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  ummy;.  int n = 
30c27 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 52  fts3GetVarint(pR
30c28 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69  eader->pData, &i
30c29 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74  Dummy);.  assert
30c2a 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !dlrAtEnd(pRea
30c2b 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
30c2c 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b   pReader->pData+
30c2d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  n;.}.static int 
30c2e 64 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28 44 4c  dlrPosDataLen(DL
30c2f 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
30c30 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
30c31 20 69 44 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e   iDummy;.  int n
30c32 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
30c33 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
30c34 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73   &iDummy);.  ass
30c35 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70  ert( !dlrAtEnd(p
30c36 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
30c37 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c  urn pReader->nEl
30c38 65 6d 65 6e 74 2d 6e 3b 0a 7d 0a 73 74 61 74 69  ement-n;.}.stati
30c39 63 20 76 6f 69 64 20 64 6c 72 53 74 65 70 28 44  c void dlrStep(D
30c3a 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
30c3b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  ){.  assert( !dl
30c3c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
30c3d 29 3b 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 61  );..  /* Skip pa
30c3e 73 74 20 63 75 72 72 65 6e 74 20 64 6f 63 6c 69  st current docli
30c3f 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20  st element. */. 
30c40 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72   assert( pReader
30c41 2d 3e 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52 65 61  ->nElement<=pRea
30c42 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
30c43 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b  pReader->pData +
30c44 3d 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d  = pReader->nElem
30c45 65 6e 74 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  ent;.  pReader->
30c46 6e 44 61 74 61 20 2d 3d 20 70 52 65 61 64 65 72  nData -= pReader
30c47 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a 0a 20 20 2f  ->nElement;..  /
30c48 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6d 6f  * If there is mo
30c49 72 65 20 64 61 74 61 2c 20 72 65 61 64 20 74 68  re data, read th
30c4a 65 20 6e 65 78 74 20 64 6f 63 6c 69 73 74 20 65  e next doclist e
30c4b 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 66 28  lement. */.  if(
30c4c 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 21   pReader->nData!
30c4d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
30c4e 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c  _int64 iDocidDel
30c4f 74 61 3b 0a 20 20 20 20 69 6e 74 20 69 44 75 6d  ta;.    int iDum
30c50 6d 79 2c 20 6e 20 3d 20 66 74 73 33 47 65 74 56  my, n = fts3GetV
30c51 61 72 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 70  arint(pReader->p
30c52 44 61 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c  Data, &iDocidDel
30c53 74 61 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72  ta);.    pReader
30c54 2d 3e 69 44 6f 63 69 64 20 2b 3d 20 69 44 6f 63  ->iDocid += iDoc
30c55 69 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 66 28  idDelta;.    if(
30c56 20 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3e   pReader->iType>
30c57 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b  =DL_POSITIONS ){
30c58 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
30c59 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  <pReader->nData 
30c5a 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
30c5b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b  1 ){.        n +
30c5c 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
30c5d 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
30c5e 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  +n, &iDummy);.  
30c5f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c        assert( n<
30c60 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
30c61 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
30c62 44 75 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29  Dummy==POS_END )
30c63 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
30c64 69 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f  if( iDummy==POS_
30c65 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
30c66 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
30c67 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
30c68 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d  ->pData+n, &iDum
30c69 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  my);.          a
30c6a 73 73 65 72 74 28 20 6e 3c 70 52 65 61 64 65 72  ssert( n<pReader
30c6b 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20  ->nData );.     
30c6c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 65     }else if( pRe
30c6d 61 64 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f  ader->iType==DL_
30c6e 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
30c6f 53 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  S ){.          n
30c70 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
30c71 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
30c72 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  ta+n, &iDummy);.
30c73 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66            n += f
30c74 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
30c75 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
30c76 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20   &iDummy);.     
30c77 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70       assert( n<p
30c78 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
30c79 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
30c7a 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65   }.    }.    pRe
30c7b 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d  ader->nElement =
30c7c 20 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   n;.    assert( 
30c7d 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e  pReader->nElemen
30c7e 74 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  t<=pReader->nDat
30c7f 61 20 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  a );.  }.}.stati
30c80 63 20 76 6f 69 64 20 64 6c 72 49 6e 69 74 28 44  c void dlrInit(D
30c81 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
30c82 2c 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54  , DocListType iT
30c83 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ype,.           
30c84 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
30c85 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
30c86 6e 44 61 74 61 29 7b 0a 20 20 61 73 73 65 72 74  nData){.  assert
30c87 28 20 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26  ( pData!=NULL &&
30c88 20 6e 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 70   nData!=0 );.  p
30c89 52 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20  Reader->iType = 
30c8a 69 54 79 70 65 3b 0a 20 20 70 52 65 61 64 65 72  iType;.  pReader
30c8b 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 3b  ->pData = pData;
30c8c 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  .  pReader->nDat
30c8d 61 20 3d 20 6e 44 61 74 61 3b 0a 20 20 70 52 65  a = nData;.  pRe
30c8e 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d  ader->nElement =
30c8f 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69   0;.  pReader->i
30c90 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a  Docid = 0;..  /*
30c91 20 4c 6f 61 64 20 74 68 65 20 66 69 72 73 74 20   Load the first 
30c92 65 6c 65 6d 65 6e 74 27 73 20 64 61 74 61 2e 20  element's data. 
30c93 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
30c94 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 2e 20   first element. 
30c95 2a 2f 0a 20 20 64 6c 72 53 74 65 70 28 70 52 65  */.  dlrStep(pRe
30c96 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
30c97 76 6f 69 64 20 64 6c 72 44 65 73 74 72 6f 79 28  void dlrDestroy(
30c98 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  DLReader *pReade
30c99 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70  r){.  SCRAMBLE(p
30c9a 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e  Reader);.}..#ifn
30c9b 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65  def NDEBUG./* Ve
30c9c 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64 6f  rify that the do
30c9d 63 6c 69 73 74 20 63 61 6e 20 62 65 20 76 61 6c  clist can be val
30c9e 69 64 6c 79 20 64 65 63 6f 64 65 64 2e 20 20 41  idly decoded.  A
30c9f 6c 73 6f 20 72 65 74 75 72 6e 73 20 74 68 65 0a  lso returns the.
30ca0 2a 2a 20 6c 61 73 74 20 64 6f 63 69 64 20 66 6f  ** last docid fo
30ca1 75 6e 64 20 62 65 63 61 75 73 65 20 69 74 20 69  und because it i
30ca2 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 69 6e 20  s convenient in 
30ca3 6f 74 68 65 72 20 61 73 73 65 72 74 69 6f 6e 73  other assertions
30ca4 20 66 6f 72 0a 2a 2a 20 44 4c 57 72 69 74 65 72   for.** DLWriter
30ca5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
30ca6 20 64 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65   docListValidate
30ca7 28 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79  (DocListType iTy
30ca8 70 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  pe, const char *
30ca9 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
30caa 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
30cab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
30cac 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 4c 61 73  lite_int64 *pLas
30cad 74 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74  tDocid){.  sqlit
30cae 65 5f 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63  e_int64 iPrevDoc
30caf 69 64 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  id = 0;.  assert
30cb0 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61  ( nData>0 );.  a
30cb1 73 73 65 72 74 28 20 70 44 61 74 61 21 3d 30 20  ssert( pData!=0 
30cb2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  );.  assert( pDa
30cb3 74 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29  ta+nData>pData )
30cb4 3b 0a 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61  ;.  while( nData
30cb5 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  !=0 ){.    sqlit
30cb6 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65  e_int64 iDocidDe
30cb7 6c 74 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  lta;.    int n =
30cb8 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70   fts3GetVarint(p
30cb9 44 61 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c  Data, &iDocidDel
30cba 74 61 29 3b 0a 20 20 20 20 69 50 72 65 76 44 6f  ta);.    iPrevDo
30cbb 63 69 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c  cid += iDocidDel
30cbc 74 61 3b 0a 20 20 20 20 69 66 28 20 69 54 79 70  ta;.    if( iTyp
30cbd 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20  e>DL_DOCIDS ){. 
30cbe 20 20 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 3b       int iDummy;
30cbf 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 31 20  .      while( 1 
30cc0 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  ){.        n += 
30cc1 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
30cc2 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
30cc3 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
30cc4 44 75 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29  Dummy==POS_END )
30cc5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
30cc6 69 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f  if( iDummy==POS_
30cc7 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
30cc8 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
30cc9 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e  Varint32(pData+n
30cca 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
30ccb 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 54      }else if( iT
30ccc 79 70 65 3e 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  ype>DL_POSITIONS
30ccd 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20   ){.          n 
30cce 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
30ccf 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  32(pData+n, &iDu
30cd0 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  mmy);.          
30cd1 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69  n += fts3GetVari
30cd2 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69  nt32(pData+n, &i
30cd3 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20  Dummy);.        
30cd4 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
30cd5 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20  ( n<=nData );.  
30cd6 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
30cd7 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61  assert( n<=nData
30cd8 20 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d   );.    pData +=
30cd9 20 6e 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d   n;.    nData -=
30cda 20 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c   n;.  }.  if( pL
30cdb 61 73 74 44 6f 63 69 64 20 29 20 2a 70 4c 61 73  astDocid ) *pLas
30cdc 74 44 6f 63 69 64 20 3d 20 69 50 72 65 76 44 6f  tDocid = iPrevDo
30cdd 63 69 64 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 41  cid;.}.#define A
30cde 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c  SSERT_VALID_DOCL
30cdf 49 53 54 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 20  IST(i, p, n, o) 
30ce0 64 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28  docListValidate(
30ce1 69 2c 20 70 2c 20 6e 2c 20 6f 29 0a 23 65 6c 73  i, p, n, o).#els
30ce2 65 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54  e.#define ASSERT
30ce3 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69  _VALID_DOCLIST(i
30ce4 2c 20 70 2c 20 6e 2c 20 6f 29 20 61 73 73 65 72  , p, n, o) asser
30ce5 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a 2f  t( 1 ).#endif../
30ce6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30ce7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30ce8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30ce9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30cea 2a 2a 2a 2f 0a 2f 2a 20 44 4c 57 72 69 74 65 72  ***/./* DLWriter
30ceb 20 69 73 20 75 73 65 64 20 74 6f 20 77 72 69 74   is used to writ
30cec 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74  e doclist data t
30ced 6f 20 61 20 44 61 74 61 42 75 66 66 65 72 2e 20  o a DataBuffer. 
30cee 20 44 4c 57 72 69 74 65 72 0a 2a 2a 20 61 6c 77   DLWriter.** alw
30cef 61 79 73 20 61 70 70 65 6e 64 73 20 74 6f 20 74  ays appends to t
30cf0 68 65 20 62 75 66 66 65 72 20 61 6e 64 20 64 6f  he buffer and do
30cf1 65 73 20 6e 6f 74 20 6f 77 6e 20 69 74 2e 0a 2a  es not own it..*
30cf2 2a 0a 2a 2a 20 64 6c 77 49 6e 69 74 20 2d 20 69  *.** dlwInit - i
30cf3 6e 69 74 69 61 6c 69 7a 65 20 74 6f 20 77 72 69  nitialize to wri
30cf4 74 65 20 61 20 67 69 76 65 6e 20 74 79 70 65 20  te a given type 
30cf5 64 6f 63 6c 69 73 74 74 6f 20 61 20 62 75 66 66  doclistto a buff
30cf6 65 72 2e 0a 2a 2a 20 64 6c 77 44 65 73 74 72 6f  er..** dlwDestro
30cf7 79 20 2d 20 63 6c 65 61 72 20 74 68 65 20 77 72  y - clear the wr
30cf8 69 74 65 72 27 73 20 6d 65 6d 6f 72 79 2e 20 20  iter's memory.  
30cf9 44 6f 65 73 20 6e 6f 74 20 66 72 65 65 20 62 75  Does not free bu
30cfa 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 41 70 70 65  ffer..** dlwAppe
30cfb 6e 64 20 2d 20 61 70 70 65 6e 64 20 72 61 77 20  nd - append raw 
30cfc 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20  doclist data to 
30cfd 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 43 6f  buffer..** dlwCo
30cfe 70 79 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 64  py - copy next d
30cff 6f 63 6c 69 73 74 20 66 72 6f 6d 20 72 65 61 64  oclist from read
30d00 65 72 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a 2a  er to writer..**
30d01 20 64 6c 77 41 64 64 20 2d 20 63 6f 6e 73 74 72   dlwAdd - constr
30d02 75 63 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d  uct doclist elem
30d03 65 6e 74 20 61 6e 64 20 61 70 70 65 6e 64 20 74  ent and append t
30d04 6f 20 62 75 66 66 65 72 2e 0a 2a 2a 20 20 20 20  o buffer..**    
30d05 4f 6e 6c 79 20 61 70 70 6c 79 20 64 6c 77 41 64  Only apply dlwAd
30d06 64 28 29 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53  d() to DL_DOCIDS
30d07 20 64 6f 63 6c 69 73 74 73 20 28 65 6c 73 65 20   doclists (else 
30d08 75 73 65 20 50 4c 57 72 69 74 65 72 29 2e 0a 2a  use PLWriter)..*
30d09 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
30d0a 20 44 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 6f   DLWriter {.  Do
30d0b 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 3b  cListType iType;
30d0c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 62  .  DataBuffer *b
30d0d 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
30d0e 20 69 50 72 65 76 44 6f 63 69 64 3b 0a 23 69 66   iPrevDocid;.#if
30d0f 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 6e  ndef NDEBUG.  in
30d10 74 20 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  t has_iPrevDocid
30d11 3b 0a 23 65 6e 64 69 66 0a 7d 20 44 4c 57 72 69  ;.#endif.} DLWri
30d12 74 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  ter;..static voi
30d13 64 20 64 6c 77 49 6e 69 74 28 44 4c 57 72 69 74  d dlwInit(DLWrit
30d14 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44 6f 63  er *pWriter, Doc
30d15 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 20  ListType iType, 
30d16 44 61 74 61 42 75 66 66 65 72 20 2a 62 29 7b 0a  DataBuffer *b){.
30d17 20 20 70 57 72 69 74 65 72 2d 3e 62 20 3d 20 62    pWriter->b = b
30d18 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 54 79  ;.  pWriter->iTy
30d19 70 65 20 3d 20 69 54 79 70 65 3b 0a 20 20 70 57  pe = iType;.  pW
30d1a 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
30d1b 64 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e  d = 0;.#ifndef N
30d1c 44 45 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d  DEBUG.  pWriter-
30d1d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20  >has_iPrevDocid 
30d1e 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74  = 0;.#endif.}.st
30d1f 61 74 69 63 20 76 6f 69 64 20 64 6c 77 44 65 73  atic void dlwDes
30d20 74 72 6f 79 28 44 4c 57 72 69 74 65 72 20 2a 70  troy(DLWriter *p
30d21 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d  Writer){.  SCRAM
30d22 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a  BLE(pWriter);.}.
30d23 2f 2a 20 69 46 69 72 73 74 44 6f 63 69 64 20 69  /* iFirstDocid i
30d24 73 20 74 68 65 20 66 69 72 73 74 20 64 6f 63 69  s the first doci
30d25 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  d in the doclist
30d26 20 69 6e 20 70 44 61 74 61 2e 20 20 49 74 20 69   in pData.  It i
30d27 73 0a 2a 2a 20 6e 65 65 64 65 64 20 62 65 63 61  s.** needed beca
30d28 75 73 65 20 70 44 61 74 61 20 6d 61 79 20 70 6f  use pData may po
30d29 69 6e 74 20 77 69 74 68 69 6e 20 61 20 6c 61 72  int within a lar
30d2a 67 65 72 20 64 6f 63 6c 69 73 74 2c 20 69 6e 20  ger doclist, in 
30d2b 77 68 69 63 68 0a 2a 2a 20 63 61 73 65 20 74 68  which.** case th
30d2c 65 20 66 69 72 73 74 20 69 74 65 6d 20 77 6f 75  e first item wou
30d2d 6c 64 20 62 65 20 64 65 6c 74 61 2d 65 6e 63 6f  ld be delta-enco
30d2e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 69 4c 61 73 74  ded..**.** iLast
30d2f 44 6f 63 69 64 20 69 73 20 74 68 65 20 66 69 6e  Docid is the fin
30d30 61 6c 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20  al docid in the 
30d31 64 6f 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61  doclist in pData
30d32 2e 20 20 49 74 20 69 73 0a 2a 2a 20 6e 65 65 64  .  It is.** need
30d33 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ed to create the
30d34 20 6e 65 77 20 69 50 72 65 76 44 6f 63 69 64 20   new iPrevDocid 
30d35 66 6f 72 20 66 75 74 75 72 65 20 64 65 6c 74 61  for future delta
30d36 2d 65 6e 63 6f 64 69 6e 67 2e 20 20 54 68 65 0a  -encoding.  The.
30d37 2a 2a 20 63 6f 64 65 20 63 6f 75 6c 64 20 64 65  ** code could de
30d38 63 6f 64 65 20 74 68 65 20 70 61 73 73 65 64 20  code the passed 
30d39 64 6f 63 6c 69 73 74 20 74 6f 20 72 65 63 72 65  doclist to recre
30d3a 61 74 65 20 69 4c 61 73 74 44 6f 63 69 64 2c 20  ate iLastDocid, 
30d3b 62 75 74 0a 2a 2a 20 74 68 65 20 6f 6e 6c 79 20  but.** the only 
30d3c 63 75 72 72 65 6e 74 20 75 73 65 72 20 28 64 6f  current user (do
30d3d 63 4c 69 73 74 4d 65 72 67 65 29 20 61 6c 72 65  cListMerge) alre
30d3e 61 64 79 20 68 61 73 20 64 65 63 6f 64 65 64 20  ady has decoded 
30d3f 74 68 69 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  this.** informat
30d40 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  ion..*/./* TODO(
30d41 73 68 65 73 73 29 20 54 68 69 73 20 68 61 73 20  shess) This has 
30d42 62 65 63 6f 6d 65 20 6a 75 73 74 20 61 20 68 65  become just a he
30d43 6c 70 65 72 20 66 6f 72 20 64 6f 63 4c 69 73 74  lper for docList
30d44 4d 65 72 67 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64  Merge..** Consid
30d45 65 72 20 61 20 72 65 66 61 63 74 6f 72 20 74 6f  er a refactor to
30d46 20 6d 61 6b 65 20 74 68 69 73 20 63 6c 65 61 6e   make this clean
30d47 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
30d48 69 64 20 64 6c 77 41 70 70 65 6e 64 28 44 4c 57  id dlwAppend(DLW
30d49 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a  riter *pWriter,.
30d4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d4b 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
30d4c 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61   *pData, int nDa
30d4d 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
30d4e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
30d4f 5f 69 6e 74 36 34 20 69 46 69 72 73 74 44 6f 63  _int64 iFirstDoc
30d50 69 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  id, sqlite_int64
30d51 20 69 4c 61 73 74 44 6f 63 69 64 29 7b 0a 20 20   iLastDocid){.  
30d52 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
30d53 63 69 64 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  cid = 0;.  char 
30d54 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  c[VARINT_MAX];. 
30d55 20 69 6e 74 20 6e 46 69 72 73 74 4f 6c 64 2c 20   int nFirstOld, 
30d56 6e 46 69 72 73 74 4e 65 77 3b 20 20 20 20 20 2f  nFirstNew;     /
30d57 2a 20 4f 6c 64 20 61 6e 64 20 6e 65 77 20 76 61  * Old and new va
30d58 72 69 6e 74 20 6c 65 6e 20 6f 66 20 66 69 72 73  rint len of firs
30d59 74 20 64 6f 63 69 64 2e 20 2a 2f 0a 23 69 66 6e  t docid. */.#ifn
30d5a 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 71 6c  def NDEBUG.  sql
30d5b 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73 74 44  ite_int64 iLastD
30d5c 6f 63 69 64 44 65 6c 74 61 3b 0a 23 65 6e 64 69  ocidDelta;.#endi
30d5d 66 0a 0a 20 20 2f 2a 20 52 65 63 6f 64 65 20 74  f..  /* Recode t
30d5e 68 65 20 69 6e 69 74 69 61 6c 20 64 6f 63 69 64  he initial docid
30d5f 20 61 73 20 64 65 6c 74 61 20 66 72 6f 6d 20 69   as delta from i
30d60 50 72 65 76 44 6f 63 69 64 2e 20 2a 2f 0a 20 20  PrevDocid. */.  
30d61 6e 46 69 72 73 74 4f 6c 64 20 3d 20 66 74 73 33  nFirstOld = fts3
30d62 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c  GetVarint(pData,
30d63 20 26 69 44 6f 63 69 64 29 3b 0a 20 20 61 73 73   &iDocid);.  ass
30d64 65 72 74 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e  ert( nFirstOld<n
30d65 44 61 74 61 20 7c 7c 20 28 6e 46 69 72 73 74 4f  Data || (nFirstO
30d66 6c 64 3d 3d 6e 44 61 74 61 20 26 26 20 70 57 72  ld==nData && pWr
30d67 69 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f  iter->iType==DL_
30d68 44 4f 43 49 44 53 29 20 29 3b 0a 20 20 6e 46 69  DOCIDS) );.  nFi
30d69 72 73 74 4e 65 77 20 3d 20 66 74 73 33 50 75 74  rstNew = fts3Put
30d6a 56 61 72 69 6e 74 28 63 2c 20 69 46 69 72 73 74  Varint(c, iFirst
30d6b 44 6f 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 69  Docid-pWriter->i
30d6c 50 72 65 76 44 6f 63 69 64 29 3b 0a 0a 20 20 2f  PrevDocid);..  /
30d6d 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
30d6e 65 20 69 6e 63 6f 6d 69 6e 67 20 64 6f 63 6c 69  e incoming docli
30d6f 73 74 20 69 73 20 76 61 6c 69 64 20 41 4e 44 20  st is valid AND 
30d70 74 68 61 74 20 69 74 20 65 6e 64 73 20 77 69 74  that it ends wit
30d71 68 0a 20 20 2a 2a 20 74 68 65 20 65 78 70 65 63  h.  ** the expec
30d72 74 65 64 20 64 6f 63 69 64 2e 20 20 54 68 69 73  ted docid.  This
30d73 20 69 73 20 65 73 73 65 6e 74 69 61 6c 20 62 65   is essential be
30d74 63 61 75 73 65 20 77 65 27 6c 6c 20 74 72 75 73  cause we'll trus
30d75 74 20 74 68 69 73 0a 20 20 2a 2a 20 64 6f 63 69  t this.  ** doci
30d76 64 20 69 6e 20 66 75 74 75 72 65 20 64 65 6c 74  d in future delt
30d77 61 2d 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f  a-encoding..  */
30d78 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  .  ASSERT_VALID_
30d79 44 4f 43 4c 49 53 54 28 70 57 72 69 74 65 72 2d  DOCLIST(pWriter-
30d7a 3e 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e  >iType, pData, n
30d7b 44 61 74 61 2c 20 26 69 4c 61 73 74 44 6f 63 69  Data, &iLastDoci
30d7c 64 44 65 6c 74 61 29 3b 0a 20 20 61 73 73 65 72  dDelta);.  asser
30d7d 74 28 20 69 4c 61 73 74 44 6f 63 69 64 3d 3d 69  t( iLastDocid==i
30d7e 46 69 72 73 74 44 6f 63 69 64 2d 69 44 6f 63 69  FirstDocid-iDoci
30d7f 64 2b 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74  d+iLastDocidDelt
30d80 61 20 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e  a );..  /* Appen
30d81 64 20 72 65 63 6f 64 65 64 20 69 6e 69 74 69 61  d recoded initia
30d82 6c 20 64 6f 63 69 64 20 61 6e 64 20 65 76 65 72  l docid and ever
30d83 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 52 65  ything else.  Re
30d84 73 74 20 6f 66 20 64 6f 63 69 64 73 0a 20 20 2a  st of docids.  *
30d85 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  * should have be
30d86 65 6e 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64  en delta-encoded
30d87 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 69   from previous i
30d88 6e 69 74 69 61 6c 20 64 6f 63 69 64 2e 0a 20 20  nitial docid..  
30d89 2a 2f 0a 20 20 69 66 28 20 6e 46 69 72 73 74 4f  */.  if( nFirstO
30d8a 6c 64 3c 6e 44 61 74 61 20 29 7b 0a 20 20 20 20  ld<nData ){.    
30d8b 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
30d8c 32 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c  2(pWriter->b, c,
30d8d 20 6e 46 69 72 73 74 4e 65 77 2c 0a 20 20 20 20   nFirstNew,.    
30d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d8f 20 20 70 44 61 74 61 2b 6e 46 69 72 73 74 4f 6c    pData+nFirstOl
30d90 64 2c 20 6e 44 61 74 61 2d 6e 46 69 72 73 74 4f  d, nData-nFirstO
30d91 6c 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ld);.  }else{.  
30d92 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
30d93 6e 64 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63  nd(pWriter->b, c
30d94 2c 20 6e 46 69 72 73 74 4e 65 77 29 3b 0a 20 20  , nFirstNew);.  
30d95 7d 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72  }.  pWriter->iPr
30d96 65 76 44 6f 63 69 64 20 3d 20 69 4c 61 73 74 44  evDocid = iLastD
30d97 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ocid;.}.static v
30d98 6f 69 64 20 64 6c 77 43 6f 70 79 28 44 4c 57 72  oid dlwCopy(DLWr
30d99 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44  iter *pWriter, D
30d9a 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
30d9b 29 7b 0a 20 20 64 6c 77 41 70 70 65 6e 64 28 70  ){.  dlwAppend(p
30d9c 57 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 44 61  Writer, dlrDocDa
30d9d 74 61 28 70 52 65 61 64 65 72 29 2c 20 64 6c 72  ta(pReader), dlr
30d9e 44 6f 63 44 61 74 61 42 79 74 65 73 28 70 52 65  DocDataBytes(pRe
30d9f 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20  ader),.         
30da0 20 20 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61     dlrDocid(pRea
30da1 64 65 72 29 2c 20 64 6c 72 44 6f 63 69 64 28 70  der), dlrDocid(p
30da2 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74  Reader));.}.stat
30da3 69 63 20 76 6f 69 64 20 64 6c 77 41 64 64 28 44  ic void dlwAdd(D
30da4 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
30da5 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
30da6 44 6f 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63  Docid){.  char c
30da7 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20  [VARINT_MAX];.  
30da8 69 6e 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56  int n = fts3PutV
30da9 61 72 69 6e 74 28 63 2c 20 69 44 6f 63 69 64 2d  arint(c, iDocid-
30daa 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f  pWriter->iPrevDo
30dab 63 69 64 29 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69  cid);..  /* Doci
30dac 64 73 20 6d 75 73 74 20 61 73 63 65 6e 64 2e 20  ds must ascend. 
30dad 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70 57  */.  assert( !pW
30dae 72 69 74 65 72 2d 3e 68 61 73 5f 69 50 72 65 76  riter->has_iPrev
30daf 44 6f 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e  Docid || iDocid>
30db0 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f  pWriter->iPrevDo
30db1 63 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  cid );.  assert(
30db2 20 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65 3d   pWriter->iType=
30db3 3d 44 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 0a 20  =DL_DOCIDS );.. 
30db4 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
30db5 64 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c  d(pWriter->b, c,
30db6 20 6e 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e   n);.  pWriter->
30db7 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f  iPrevDocid = iDo
30db8 63 69 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  cid;.#ifndef NDE
30db9 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68  BUG.  pWriter->h
30dba 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20  as_iPrevDocid = 
30dbb 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a  1;.#endif.}../**
30dbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dbf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30dc0 2a 2f 0a 2f 2a 20 50 4c 52 65 61 64 65 72 20 69  */./* PLReader i
30dc1 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64  s used to read d
30dc2 61 74 61 20 66 72 6f 6d 20 61 20 64 6f 63 75 6d  ata from a docum
30dc3 65 6e 74 27 73 20 70 6f 73 69 74 69 6f 6e 20 6c  ent's position l
30dc4 69 73 74 2e 20 20 41 73 0a 2a 2a 20 74 68 65 20  ist.  As.** the 
30dc5 63 61 6c 6c 65 72 20 73 74 65 70 73 20 74 68 72  caller steps thr
30dc6 6f 75 67 68 20 74 68 65 20 6c 69 73 74 2c 20 64  ough the list, d
30dc7 61 74 61 20 69 73 20 63 61 63 68 65 64 20 73 6f  ata is cached so
30dc8 20 74 68 61 74 20 76 61 72 69 6e 74 73 0a 2a 2a   that varints.**
30dc9 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 62 65   only need to be
30dca 20 64 65 63 6f 64 65 64 20 6f 6e 63 65 2e 0a 2a   decoded once..*
30dcb 2a 0a 2a 2a 20 70 6c 72 49 6e 69 74 2c 20 70 6c  *.** plrInit, pl
30dcc 72 44 65 73 74 72 6f 79 20 2d 20 63 72 65 61 74  rDestroy - creat
30dcd 65 2f 64 65 73 74 72 6f 79 20 61 20 72 65 61 64  e/destroy a read
30dce 65 72 2e 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e  er..** plrColumn
30dcf 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 2c 20 70  , plrPosition, p
30dd0 6c 72 53 74 61 72 74 4f 66 66 73 65 74 2c 20 70  lrStartOffset, p
30dd1 6c 72 45 6e 64 4f 66 66 73 65 74 20 2d 20 61 63  lrEndOffset - ac
30dd2 63 65 73 73 6f 72 73 0a 2a 2a 20 70 6c 72 41 74  cessors.** plrAt
30dd3 45 6e 64 20 2d 20 61 74 20 65 6e 64 20 6f 66 20  End - at end of 
30dd4 73 74 72 65 61 6d 2c 20 6f 6e 6c 79 20 63 61 6c  stream, only cal
30dd5 6c 20 70 6c 72 44 65 73 74 72 6f 79 20 6f 6e 63  l plrDestroy onc
30dd6 65 20 74 72 75 65 2e 0a 2a 2a 20 70 6c 72 53 74  e true..** plrSt
30dd7 65 70 20 2d 20 73 74 65 70 20 74 6f 20 74 68 65  ep - step to the
30dd8 20 6e 65 78 74 20 65 6c 65 6d 65 6e 74 2e 0a 2a   next element..*
30dd9 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
30dda 20 50 4c 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a   PLReader {.  /*
30ddb 20 54 68 65 73 65 20 72 65 66 65 72 20 74 6f 20   These refer to 
30ddc 74 68 65 20 6e 65 78 74 20 70 6f 73 69 74 69 6f  the next positio
30ddd 6e 27 73 20 64 61 74 61 2e 20 20 6e 44 61 74 61  n's data.  nData
30dde 20 77 69 6c 6c 20 72 65 61 63 68 20 30 20 77 68   will reach 0 wh
30ddf 65 6e 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  en.  ** reading 
30de0 74 68 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f  the last positio
30de1 6e 2c 20 73 6f 20 70 6c 72 53 74 65 70 28 29 20  n, so plrStep() 
30de2 73 69 67 6e 61 6c 73 20 45 4f 46 20 62 79 20 73  signals EOF by s
30de3 65 74 74 69 6e 67 0a 20 20 2a 2a 20 70 44 61 74  etting.  ** pDat
30de4 61 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a  a to NULL..  */.
30de5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44    const char *pD
30de6 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61  ata;.  int nData
30de7 3b 0a 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  ;..  DocListType
30de8 20 69 54 79 70 65 3b 0a 20 20 69 6e 74 20 69 43   iType;.  int iC
30de9 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f  olumn;         /
30dea 2a 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  * the last colum
30deb 6e 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20  n read */.  int 
30dec 69 50 6f 73 69 74 69 6f 6e 3b 20 20 20 20 20 20  iPosition;      
30ded 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73   /* the last pos
30dee 69 74 69 6f 6e 20 72 65 61 64 20 2a 2f 0a 20 20  ition read */.  
30def 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74  int iStartOffset
30df0 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
30df1 20 73 74 61 72 74 20 6f 66 66 73 65 74 20 72 65   start offset re
30df2 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64  ad */.  int iEnd
30df3 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  Offset;      /* 
30df4 74 68 65 20 6c 61 73 74 20 65 6e 64 20 6f 66 66  the last end off
30df5 73 65 74 20 72 65 61 64 20 2a 2f 0a 7d 20 50 4c  set read */.} PL
30df6 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20  Reader;..static 
30df7 69 6e 74 20 70 6c 72 41 74 45 6e 64 28 50 4c 52  int plrAtEnd(PLR
30df8 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
30df9 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
30dfa 72 2d 3e 70 44 61 74 61 3d 3d 4e 55 4c 4c 3b 0a  r->pData==NULL;.
30dfb 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
30dfc 43 6f 6c 75 6d 6e 28 50 4c 52 65 61 64 65 72 20  Column(PLReader 
30dfd 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
30dfe 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70  ert( !plrAtEnd(p
30dff 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
30e00 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 43 6f  urn pReader->iCo
30e01 6c 75 6d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69  lumn;.}.static i
30e02 6e 74 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 50  nt plrPosition(P
30e03 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
30e04 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c  ){.  assert( !pl
30e05 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
30e06 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
30e07 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 3b 0a  der->iPosition;.
30e08 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
30e09 53 74 61 72 74 4f 66 66 73 65 74 28 50 4c 52 65  StartOffset(PLRe
30e0a 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
30e0b 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41 74    assert( !plrAt
30e0c 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
30e0d 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
30e0e 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  ->iStartOffset;.
30e0f 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72  }.static int plr
30e10 45 6e 64 4f 66 66 73 65 74 28 50 4c 52 65 61 64  EndOffset(PLRead
30e11 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
30e12 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
30e13 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
30e14 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
30e15 69 45 6e 64 4f 66 66 73 65 74 3b 0a 7d 0a 73 74  iEndOffset;.}.st
30e16 61 74 69 63 20 76 6f 69 64 20 70 6c 72 53 74 65  atic void plrSte
30e17 70 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61  p(PLReader *pRea
30e18 64 65 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e  der){.  int i, n
30e19 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c  ;..  assert( !pl
30e1a 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
30e1b 29 3b 0a 0a 20 20 69 66 28 20 70 52 65 61 64 65  );..  if( pReade
30e1c 72 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20  r->nData==0 ){. 
30e1d 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74     pReader->pDat
30e1e 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65  a = NULL;.    re
30e1f 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 6e 20 3d  turn;.  }..  n =
30e20 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
30e21 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
30e22 20 26 69 29 3b 0a 20 20 69 66 28 20 69 3d 3d 50   &i);.  if( i==P
30e23 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  OS_COLUMN ){.   
30e24 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72   n += fts3GetVar
30e25 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
30e26 44 61 74 61 2b 6e 2c 20 26 70 52 65 61 64 65 72  Data+n, &pReader
30e27 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  ->iColumn);.    
30e28 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69  pReader->iPositi
30e29 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 70 52 65 61  on = 0;.    pRea
30e2a 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65  der->iStartOffse
30e2b 74 20 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20  t = 0;.    n += 
30e2c 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
30e2d 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
30e2e 2c 20 26 69 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20  , &i);.  }.  /* 
30e2f 53 68 6f 75 6c 64 20 6e 65 76 65 72 20 73 65 65  Should never see
30e30 20 61 64 6a 61 63 65 6e 74 20 63 6f 6c 75 6d 6e   adjacent column
30e31 20 63 68 61 6e 67 65 73 2e 20 2a 2f 0a 20 20 61   changes. */.  a
30e32 73 73 65 72 74 28 20 69 21 3d 50 4f 53 5f 43 4f  ssert( i!=POS_CO
30e33 4c 55 4d 4e 20 29 3b 0a 0a 20 20 69 66 28 20 69  LUMN );..  if( i
30e34 3d 3d 50 4f 53 5f 45 4e 44 20 29 7b 0a 20 20 20  ==POS_END ){.   
30e35 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
30e36 3d 20 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72  = 0;.    pReader
30e37 2d 3e 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a  ->pData = NULL;.
30e38 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
30e39 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73  .  pReader->iPos
30e3a 69 74 69 6f 6e 20 2b 3d 20 69 2d 50 4f 53 5f 42  ition += i-POS_B
30e3b 41 53 45 3b 0a 20 20 69 66 28 20 70 52 65 61 64  ASE;.  if( pRead
30e3c 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  er->iType==DL_PO
30e3d 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
30e3e 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  ){.    n += fts3
30e3f 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
30e40 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69  der->pData+n, &i
30e41 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
30e42 69 53 74 61 72 74 4f 66 66 73 65 74 20 2b 3d 20  iStartOffset += 
30e43 69 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  i;.    n += fts3
30e44 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
30e45 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69  der->pData+n, &i
30e46 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
30e47 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 70 52 65  iEndOffset = pRe
30e48 61 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73  ader->iStartOffs
30e49 65 74 2b 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65  et+i;.  }.  asse
30e4a 72 74 28 20 6e 3c 3d 70 52 65 61 64 65 72 2d 3e  rt( n<=pReader->
30e4b 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64  nData );.  pRead
30e4c 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 3b 0a  er->pData += n;.
30e4d 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
30e4e 20 2d 3d 20 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63   -= n;.}..static
30e4f 20 76 6f 69 64 20 70 6c 72 49 6e 69 74 28 50 4c   void plrInit(PL
30e50 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c  Reader *pReader,
30e51 20 44 4c 52 65 61 64 65 72 20 2a 70 44 4c 52 65   DLReader *pDLRe
30e52 61 64 65 72 29 7b 0a 20 20 70 52 65 61 64 65 72  ader){.  pReader
30e53 2d 3e 70 44 61 74 61 20 3d 20 64 6c 72 50 6f 73  ->pData = dlrPos
30e54 44 61 74 61 28 70 44 4c 52 65 61 64 65 72 29 3b  Data(pDLReader);
30e55 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  .  pReader->nDat
30e56 61 20 3d 20 64 6c 72 50 6f 73 44 61 74 61 4c 65  a = dlrPosDataLe
30e57 6e 28 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20  n(pDLReader);.  
30e58 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d  pReader->iType =
30e59 20 70 44 4c 52 65 61 64 65 72 2d 3e 69 54 79 70   pDLReader->iTyp
30e5a 65 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 43  e;.  pReader->iC
30e5b 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 52 65  olumn = 0;.  pRe
30e5c 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20  ader->iPosition 
30e5d 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  = 0;.  pReader->
30e5e 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30  iStartOffset = 0
30e5f 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e  ;.  pReader->iEn
30e60 64 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70  dOffset = 0;.  p
30e61 6c 72 53 74 65 70 28 70 52 65 61 64 65 72 29 3b  lrStep(pReader);
30e62 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  .}.static void p
30e63 6c 72 44 65 73 74 72 6f 79 28 50 4c 52 65 61 64  lrDestroy(PLRead
30e64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
30e65 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72  SCRAMBLE(pReader
30e66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
30e67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30e68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30e69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30e6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50  **********/./* P
30e6b 4c 57 72 69 74 65 72 20 69 73 20 75 73 65 64 20  LWriter is used 
30e6c 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20  in constructing 
30e6d 61 20 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73  a document's pos
30e6e 69 74 69 6f 6e 20 6c 69 73 74 2e 20 20 41 73 20  ition list.  As 
30e6f 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65  a.** convenience
30e70 2c 20 69 66 20 69 54 79 70 65 20 69 73 20 44 4c  , if iType is DL
30e71 5f 44 4f 43 49 44 53 2c 20 50 4c 57 72 69 74 65  _DOCIDS, PLWrite
30e72 72 20 62 65 63 6f 6d 65 73 20 61 20 6e 6f 2d 6f  r becomes a no-o
30e73 70 2e 0a 2a 2a 20 50 4c 57 72 69 74 65 72 20 77  p..** PLWriter w
30e74 72 69 74 65 73 20 74 6f 20 74 68 65 20 61 73 73  rites to the ass
30e75 6f 63 69 61 74 65 64 20 44 4c 57 72 69 74 65 72  ociated DLWriter
30e76 27 73 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  's buffer..**.**
30e77 20 70 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 20   plwInit - init 
30e78 66 6f 72 20 77 72 69 74 69 6e 67 20 61 20 64 6f  for writing a do
30e79 63 75 6d 65 6e 74 27 73 20 70 6f 73 6c 69 73 74  cument's poslist
30e7a 2e 0a 2a 2a 20 70 6c 77 44 65 73 74 72 6f 79 20  ..** plwDestroy 
30e7b 2d 20 63 6c 65 61 72 20 61 20 77 72 69 74 65 72  - clear a writer
30e7c 2e 0a 2a 2a 20 70 6c 77 41 64 64 20 2d 20 61 70  ..** plwAdd - ap
30e7d 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e  pend position an
30e7e 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61  d offset informa
30e7f 74 69 6f 6e 2e 0a 2a 2a 20 70 6c 77 43 6f 70 79  tion..** plwCopy
30e80 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 70 6f 73   - copy next pos
30e81 69 74 69 6f 6e 27 73 20 64 61 74 61 20 66 72 6f  ition's data fro
30e82 6d 20 72 65 61 64 65 72 20 74 6f 20 77 72 69 74  m reader to writ
30e83 65 72 2e 0a 2a 2a 20 70 6c 77 54 65 72 6d 69 6e  er..** plwTermin
30e84 61 74 65 20 2d 20 61 64 64 20 61 6e 79 20 6e 65  ate - add any ne
30e85 63 65 73 73 61 72 79 20 64 6f 63 6c 69 73 74 20  cessary doclist 
30e86 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a  terminator..**.*
30e87 2a 20 43 61 6c 6c 69 6e 67 20 70 6c 77 41 64 64  * Calling plwAdd
30e88 28 29 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d  () after plwTerm
30e89 69 6e 61 74 65 28 29 20 6d 61 79 20 72 65 73 75  inate() may resu
30e8a 6c 74 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a  lt in a corrupt.
30e8b 2a 2a 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f  ** doclist..*/./
30e8c 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55 6e  * TODO(shess) Un
30e8d 74 69 6c 20 77 65 27 76 65 20 77 72 69 74 74 65  til we've writte
30e8e 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65  n the second ite
30e8f 6d 2c 20 77 65 20 63 61 6e 20 63 61 63 68 65 20  m, we can cache 
30e90 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 69 74 65  the.** first ite
30e91 6d 27 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  m's information.
30e92 20 20 54 68 65 6e 20 77 65 27 64 20 68 61 76 65    Then we'd have
30e93 20 74 68 72 65 65 20 73 74 61 74 65 73 3a 0a 2a   three states:.*
30e94 2a 0a 2a 2a 20 2d 20 69 6e 69 74 69 61 6c 69 7a  *.** - initializ
30e95 65 64 20 77 69 74 68 20 64 6f 63 69 64 2c 20 6e  ed with docid, n
30e96 6f 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 20  o positions..** 
30e97 2d 20 64 6f 63 69 64 20 61 6e 64 20 6f 6e 65 20  - docid and one 
30e98 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 20 2d 20 64  position..** - d
30e99 6f 63 69 64 20 61 6e 64 20 6d 75 6c 74 69 70 6c  ocid and multipl
30e9a 65 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a  e positions..**.
30e9b 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 6c 61 73 74  ** Only the last
30e9c 20 73 74 61 74 65 20 6e 65 65 64 73 20 74 6f 20   state needs to 
30e9d 61 63 74 75 61 6c 6c 79 20 77 72 69 74 65 20 74  actually write t
30e9e 6f 20 64 6c 77 2d 3e 62 2c 20 77 68 69 63 68 20  o dlw->b, which 
30e9f 77 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 6e 20 69  would.** be an i
30ea0 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 74 68  mprovement in th
30ea1 65 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 63 61  e DLCollector ca
30ea2 73 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  se..*/.typedef s
30ea3 74 72 75 63 74 20 50 4c 57 72 69 74 65 72 20 7b  truct PLWriter {
30ea4 0a 20 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77  .  DLWriter *dlw
30ea5 3b 0a 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  ;..  int iColumn
30ea6 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
30ea7 20 63 6f 6c 75 6d 6e 20 77 72 69 74 74 65 6e 20   column written 
30ea8 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73 3b 20 20  */.  int iPos;  
30ea9 20 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74       /* the last
30eaa 20 70 6f 73 69 74 69 6f 6e 20 77 72 69 74 74 65   position writte
30eab 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  n */.  int iOffs
30eac 65 74 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61  et;    /* the la
30ead 73 74 20 73 74 61 72 74 20 6f 66 66 73 65 74 20  st start offset 
30eae 77 72 69 74 74 65 6e 20 2a 2f 0a 7d 20 50 4c 57  written */.} PLW
30eaf 72 69 74 65 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28  riter;../* TODO(
30eb0 73 68 65 73 73 29 20 49 6e 20 74 68 65 20 63 61  shess) In the ca
30eb1 73 65 20 77 68 65 72 65 20 74 68 65 20 70 61 72  se where the par
30eb2 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 74  ent is reading t
30eb3 68 65 73 65 20 76 61 6c 75 65 73 0a 2a 2a 20 66  hese values.** f
30eb4 72 6f 6d 20 61 20 50 4c 52 65 61 64 65 72 2c 20  rom a PLReader, 
30eb5 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a  we could optimiz
30eb6 65 20 74 6f 20 61 20 63 6f 70 79 20 69 66 20 74  e to a copy if t
30eb7 68 61 74 20 50 4c 52 65 61 64 65 72 20 68 61 73  hat PLReader has
30eb8 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70  .** the same typ
30eb9 65 20 61 73 20 70 57 72 69 74 65 72 2e 0a 2a 2f  e as pWriter..*/
30eba 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77  .static void plw
30ebb 41 64 64 28 50 4c 57 72 69 74 65 72 20 2a 70 57  Add(PLWriter *pW
30ebc 72 69 74 65 72 2c 20 69 6e 74 20 69 43 6f 6c 75  riter, int iColu
30ebd 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20  mn, int iPos,.  
30ebe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ebf 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
30ec0 74 2c 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65  t, int iEndOffse
30ec1 74 29 7b 0a 20 20 2f 2a 20 57 6f 72 73 74 2d 63  t){.  /* Worst-c
30ec2 61 73 65 20 73 70 61 63 65 20 66 6f 72 20 50 4f  ase space for PO
30ec3 53 5f 43 4f 4c 55 4d 4e 2c 20 69 43 6f 6c 75 6d  S_COLUMN, iColum
30ec4 6e 2c 20 69 50 6f 73 44 65 6c 74 61 2c 0a 20 20  n, iPosDelta,.  
30ec5 2a 2a 20 69 53 74 61 72 74 4f 66 66 73 65 74 44  ** iStartOffsetD
30ec6 65 6c 74 61 2c 20 61 6e 64 20 69 45 6e 64 4f 66  elta, and iEndOf
30ec7 66 73 65 74 44 65 6c 74 61 2e 0a 20 20 2a 2f 0a  fsetDelta..  */.
30ec8 20 20 63 68 61 72 20 63 5b 35 2a 56 41 52 49 4e    char c[5*VARIN
30ec9 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 20  T_MAX];.  int n 
30eca 3d 20 30 3b 0a 0a 20 20 2f 2a 20 42 61 6e 20 70  = 0;..  /* Ban p
30ecb 6c 77 41 64 64 28 29 20 61 66 74 65 72 20 70 6c  lwAdd() after pl
30ecc 77 54 65 72 6d 69 6e 61 74 65 28 29 2e 20 2a 2f  wTerminate(). */
30ecd 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
30ece 65 72 2d 3e 69 50 6f 73 21 3d 2d 31 20 29 3b 0a  er->iPos!=-1 );.
30ecf 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
30ed0 64 6c 77 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44  dlw->iType==DL_D
30ed1 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e 3b 0a  OCIDS ) return;.
30ed2 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 21 3d  .  if( iColumn!=
30ed3 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pWriter->iColumn
30ed4 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73   ){.    n += fts
30ed5 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20  3PutVarint(c+n, 
30ed6 50 4f 53 5f 43 4f 4c 55 4d 4e 29 3b 0a 20 20 20  POS_COLUMN);.   
30ed7 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72   n += fts3PutVar
30ed8 69 6e 74 28 63 2b 6e 2c 20 69 43 6f 6c 75 6d 6e  int(c+n, iColumn
30ed9 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
30eda 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d  iColumn = iColum
30edb 6e 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  n;.    pWriter->
30edc 69 50 6f 73 20 3d 20 30 3b 0a 20 20 20 20 70 57  iPos = 0;.    pW
30edd 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d  riter->iOffset =
30ede 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
30edf 28 20 69 50 6f 73 3e 3d 70 57 72 69 74 65 72 2d  ( iPos>=pWriter-
30ee0 3e 69 50 6f 73 20 29 3b 0a 20 20 6e 20 2b 3d 20  >iPos );.  n += 
30ee1 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
30ee2 6e 2c 20 50 4f 53 5f 42 41 53 45 2b 28 69 50 6f  n, POS_BASE+(iPo
30ee3 73 2d 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 29  s-pWriter->iPos)
30ee4 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50  );.  pWriter->iP
30ee5 6f 73 20 3d 20 69 50 6f 73 3b 0a 20 20 69 66 28  os = iPos;.  if(
30ee6 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69   pWriter->dlw->i
30ee7 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  Type==DL_POSITIO
30ee8 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20  NS_OFFSETS ){.  
30ee9 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72 74    assert( iStart
30eea 4f 66 66 73 65 74 3e 3d 70 57 72 69 74 65 72 2d  Offset>=pWriter-
30eeb 3e 69 4f 66 66 73 65 74 20 29 3b 0a 20 20 20 20  >iOffset );.    
30eec 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
30eed 6e 74 28 63 2b 6e 2c 20 69 53 74 61 72 74 4f 66  nt(c+n, iStartOf
30eee 66 73 65 74 2d 70 57 72 69 74 65 72 2d 3e 69 4f  fset-pWriter->iO
30eef 66 66 73 65 74 29 3b 0a 20 20 20 20 70 57 72 69  ffset);.    pWri
30ef0 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69  ter->iOffset = i
30ef1 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20  StartOffset;.   
30ef2 20 61 73 73 65 72 74 28 20 69 45 6e 64 4f 66 66   assert( iEndOff
30ef3 73 65 74 3e 3d 69 53 74 61 72 74 4f 66 66 73 65  set>=iStartOffse
30ef4 74 20 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  t );.    n += ft
30ef5 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c  s3PutVarint(c+n,
30ef6 20 69 45 6e 64 4f 66 66 73 65 74 2d 69 53 74 61   iEndOffset-iSta
30ef7 72 74 4f 66 66 73 65 74 29 3b 0a 20 20 7d 0a 20  rtOffset);.  }. 
30ef8 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
30ef9 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e  d(pWriter->dlw->
30efa 62 2c 20 63 2c 20 6e 29 3b 0a 7d 0a 73 74 61 74  b, c, n);.}.stat
30efb 69 63 20 76 6f 69 64 20 70 6c 77 43 6f 70 79 28  ic void plwCopy(
30efc 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  PLWriter *pWrite
30efd 72 2c 20 50 4c 52 65 61 64 65 72 20 2a 70 52 65  r, PLReader *pRe
30efe 61 64 65 72 29 7b 0a 20 20 70 6c 77 41 64 64 28  ader){.  plwAdd(
30eff 70 57 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75  pWriter, plrColu
30f00 6d 6e 28 70 52 65 61 64 65 72 29 2c 20 70 6c 72  mn(pReader), plr
30f01 50 6f 73 69 74 69 6f 6e 28 70 52 65 61 64 65 72  Position(pReader
30f02 29 2c 0a 20 20 20 20 20 20 20 20 20 70 6c 72 53  ),.         plrS
30f03 74 61 72 74 4f 66 66 73 65 74 28 70 52 65 61 64  tartOffset(pRead
30f04 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65  er), plrEndOffse
30f05 74 28 70 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73  t(pReader));.}.s
30f06 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 49 6e  tatic void plwIn
30f07 69 74 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72  it(PLWriter *pWr
30f08 69 74 65 72 2c 20 44 4c 57 72 69 74 65 72 20 2a  iter, DLWriter *
30f09 64 6c 77 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  dlw, sqlite_int6
30f0a 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 63 68 61  4 iDocid){.  cha
30f0b 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  r c[VARINT_MAX];
30f0c 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 70 57 72  .  int n;..  pWr
30f0d 69 74 65 72 2d 3e 64 6c 77 20 3d 20 64 6c 77 3b  iter->dlw = dlw;
30f0e 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75  ..  /* Docids mu
30f0f 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20  st ascend. */.  
30f10 61 73 73 65 72 74 28 20 21 70 57 72 69 74 65 72  assert( !pWriter
30f11 2d 3e 64 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76  ->dlw->has_iPrev
30f12 44 6f 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e  Docid || iDocid>
30f13 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50  pWriter->dlw->iP
30f14 72 65 76 44 6f 63 69 64 20 29 3b 0a 20 20 6e 20  revDocid );.  n 
30f15 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
30f16 63 2c 20 69 44 6f 63 69 64 2d 70 57 72 69 74 65  c, iDocid-pWrite
30f17 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f 63  r->dlw->iPrevDoc
30f18 69 64 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  id);.  dataBuffe
30f19 72 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d  rAppend(pWriter-
30f1a 3e 64 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a  >dlw->b, c, n);.
30f1b 20 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e    pWriter->dlw->
30f1c 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f  iPrevDocid = iDo
30f1d 63 69 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  cid;.#ifndef NDE
30f1e 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 64  BUG.  pWriter->d
30f1f 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63  lw->has_iPrevDoc
30f20 69 64 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a  id = 1;.#endif..
30f21 20 20 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75    pWriter->iColu
30f22 6d 6e 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65  mn = 0;.  pWrite
30f23 72 2d 3e 69 50 6f 73 20 3d 20 30 3b 0a 20 20 70  r->iPos = 0;.  p
30f24 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20  Writer->iOffset 
30f25 3d 20 30 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f 28 73  = 0;.}./* TODO(s
30f26 68 65 73 73 29 20 53 68 6f 75 6c 64 20 70 6c 77  hess) Should plw
30f27 44 65 73 74 72 6f 79 28 29 20 61 6c 73 6f 20 74  Destroy() also t
30f28 65 72 6d 69 6e 61 74 65 20 74 68 65 20 64 6f 63  erminate the doc
30f29 6c 69 73 74 3f 20 20 42 75 74 0a 2a 2a 20 74 68  list?  But.** th
30f2a 65 6e 20 70 6c 77 44 65 73 74 72 6f 79 28 29 20  en plwDestroy() 
30f2b 77 6f 75 6c 64 20 6e 6f 20 6c 6f 6e 67 65 72 20  would no longer 
30f2c 62 65 20 6a 75 73 74 20 61 20 64 65 73 74 72 75  be just a destru
30f2d 63 74 6f 72 2c 20 69 74 20 77 6f 75 6c 64 0a 2a  ctor, it would.*
30f2e 2a 20 61 6c 73 6f 20 62 65 20 64 6f 69 6e 67 20  * also be doing 
30f2f 77 6f 72 6b 2c 20 77 68 69 63 68 20 69 73 6e 27  work, which isn'
30f30 74 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74  t consistent wit
30f31 68 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 69 64  h the overall id
30f32 69 6f 6d 2e 0a 2a 2a 20 41 6e 6f 74 68 65 72 20  iom..** Another 
30f33 6f 70 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20  option would be 
30f34 66 6f 72 20 70 6c 77 41 64 64 28 29 20 74 6f 20  for plwAdd() to 
30f35 61 6c 77 61 79 73 20 61 70 70 65 6e 64 20 61 6e  always append an
30f36 79 20 6e 65 63 65 73 73 61 72 79 0a 2a 2a 20 74  y necessary.** t
30f37 65 72 6d 69 6e 61 74 6f 72 2c 20 73 6f 20 74 68  erminator, so th
30f38 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 69 73  at the output is
30f39 20 61 6c 77 61 79 73 20 63 6f 72 72 65 63 74 2e   always correct.
30f3a 20 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64    But that would
30f3b 0a 2a 2a 20 61 64 64 20 69 6e 63 72 65 6d 65 6e  .** add incremen
30f3c 74 61 6c 20 77 6f 72 6b 20 74 6f 20 74 68 65 20  tal work to the 
30f3d 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 69 74 68  common case with
30f3e 20 74 68 65 20 6f 6e 6c 79 20 62 65 6e 65 66 69   the only benefi
30f3f 74 20 62 65 69 6e 67 0a 2a 2a 20 41 50 49 20 65  t being.** API e
30f40 6c 65 67 61 6e 63 65 2e 20 20 50 75 6e 74 20 66  legance.  Punt f
30f41 6f 72 20 6e 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69  or now..*/.stati
30f42 63 20 76 6f 69 64 20 70 6c 77 54 65 72 6d 69 6e  c void plwTermin
30f43 61 74 65 28 50 4c 57 72 69 74 65 72 20 2a 70 57  ate(PLWriter *pW
30f44 72 69 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57  riter){.  if( pW
30f45 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70  riter->dlw->iTyp
30f46 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20  e>DL_DOCIDS ){. 
30f47 20 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54     char c[VARINT
30f48 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e  _MAX];.    int n
30f49 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
30f4a 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20  (c, POS_END);.  
30f4b 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
30f4c 6e 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d  nd(pWriter->dlw-
30f4d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a 23  >b, c, n);.  }.#
30f4e 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
30f4f 2f 2a 20 4d 61 72 6b 20 61 73 20 74 65 72 6d 69  /* Mark as termi
30f50 6e 61 74 65 64 20 66 6f 72 20 61 73 73 65 72 74  nated for assert
30f51 20 69 6e 20 70 6c 77 41 64 64 28 29 2e 20 2a 2f   in plwAdd(). */
30f52 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73  .  pWriter->iPos
30f53 20 3d 20 2d 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a   = -1;.#endif.}.
30f54 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 44  static void plwD
30f55 65 73 74 72 6f 79 28 50 4c 57 72 69 74 65 72 20  estroy(PLWriter 
30f56 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52  *pWriter){.  SCR
30f57 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a  AMBLE(pWriter);.
30f58 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
30f59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f5c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f  *******/./* DLCo
30f5d 6c 6c 65 63 74 6f 72 20 77 72 61 70 73 20 50 4c  llector wraps PL
30f5e 57 72 69 74 65 72 20 61 6e 64 20 44 4c 57 72 69  Writer and DLWri
30f5f 74 65 72 20 74 6f 20 70 72 6f 76 69 64 65 20 61  ter to provide a
30f60 0a 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2d  .** dynamically-
30f61 61 6c 6c 6f 63 61 74 65 64 20 64 6f 63 6c 69 73  allocated doclis
30f62 74 20 61 72 65 61 20 74 6f 20 75 73 65 20 64 75  t area to use du
30f63 72 69 6e 67 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  ring tokenizatio
30f64 6e 2e 0a 2a 2a 0a 2a 2a 20 64 6c 63 4e 65 77 20  n..**.** dlcNew 
30f65 2d 20 6d 61 6c 6c 6f 63 20 75 70 20 61 6e 64 20  - malloc up and 
30f66 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 63 6f 6c  initialize a col
30f67 6c 65 63 74 6f 72 2e 0a 2a 2a 20 64 6c 63 44 65  lector..** dlcDe
30f68 6c 65 74 65 20 2d 20 64 65 73 74 72 6f 79 20 61  lete - destroy a
30f69 20 63 6f 6c 6c 65 63 74 6f 72 20 61 6e 64 20 61   collector and a
30f6a 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 69 74 65  ll contained ite
30f6b 6d 73 2e 0a 2a 2a 20 64 6c 63 41 64 64 50 6f 73  ms..** dlcAddPos
30f6c 20 2d 20 61 70 70 65 6e 64 20 70 6f 73 69 74 69   - append positi
30f6d 6f 6e 20 61 6e 64 20 6f 66 66 73 65 74 20 69 6e  on and offset in
30f6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 64 6c  formation..** dl
30f6f 63 41 64 64 44 6f 63 6c 69 73 74 20 2d 20 61 64  cAddDoclist - ad
30f70 64 20 74 68 65 20 63 6f 6c 6c 65 63 74 65 64 20  d the collected 
30f71 64 6f 63 6c 69 73 74 20 74 6f 20 74 68 65 20 67  doclist to the g
30f72 69 76 65 6e 20 62 75 66 66 65 72 2e 0a 2a 2a 20  iven buffer..** 
30f73 64 6c 63 4e 65 78 74 20 2d 20 74 65 72 6d 69 6e  dlcNext - termin
30f74 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
30f75 64 6f 63 75 6d 65 6e 74 20 61 6e 64 20 6f 70 65  document and ope
30f76 6e 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 74 79  n another..*/.ty
30f77 70 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 43  pedef struct DLC
30f78 6f 6c 6c 65 63 74 6f 72 20 7b 0a 20 20 44 61 74  ollector {.  Dat
30f79 61 42 75 66 66 65 72 20 62 3b 0a 20 20 44 4c 57  aBuffer b;.  DLW
30f7a 72 69 74 65 72 20 64 6c 77 3b 0a 20 20 50 4c 57  riter dlw;.  PLW
30f7b 72 69 74 65 72 20 70 6c 77 3b 0a 7d 20 44 4c 43  riter plw;.} DLC
30f7c 6f 6c 6c 65 63 74 6f 72 3b 0a 0a 2f 2a 20 54 4f  ollector;../* TO
30f7d 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 63  DO(shess) This c
30f7e 6f 75 6c 64 20 61 6c 73 6f 20 62 65 20 64 6f 6e  ould also be don
30f7f 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 70 6c 77  e by calling plw
30f80 54 65 72 6d 69 6e 61 74 65 28 29 20 61 6e 64 0a  Terminate() and.
30f81 2a 2a 20 64 61 74 61 42 75 66 66 65 72 41 70 70  ** dataBufferApp
30f82 65 6e 64 28 29 2e 20 20 49 20 74 72 69 65 64 20  end().  I tried 
30f83 74 68 61 74 2c 20 65 78 70 65 63 74 69 6e 67 20  that, expecting 
30f84 6e 6f 6d 69 6e 61 6c 20 70 65 72 66 6f 72 6d 61  nominal performa
30f85 6e 63 65 0a 2a 2a 20 64 69 66 66 65 72 65 6e 63  nce.** differenc
30f86 65 73 2c 20 62 75 74 20 69 74 20 73 65 65 6d 65  es, but it seeme
30f87 64 20 74 6f 20 70 72 65 74 74 79 20 72 65 6c 69  d to pretty reli
30f88 61 62 6c 79 20 62 65 20 77 6f 72 74 68 20 31 25  ably be worth 1%
30f89 20 74 6f 20 63 6f 64 65 0a 2a 2a 20 69 74 20 74   to code.** it t
30f8a 68 69 73 20 77 61 79 2e 20 20 49 20 73 75 73 70  his way.  I susp
30f8b 65 63 74 20 69 74 20 69 73 20 74 68 65 20 69 6e  ect it is the in
30f8c 63 72 65 6d 65 6e 74 61 6c 20 6d 61 6c 6c 6f 63  cremental malloc
30f8d 20 6f 76 65 72 68 65 61 64 20 28 73 6f 6d 65 0a   overhead (some.
30f8e 2a 2a 20 70 65 72 63 65 6e 74 61 67 65 20 6f 66  ** percentage of
30f8f 20 74 68 65 20 70 6c 77 54 65 72 6d 69 6e 61 74   the plwTerminat
30f90 65 28 29 20 63 61 6c 6c 73 20 77 69 6c 6c 20 63  e() calls will c
30f91 61 75 73 65 20 61 20 72 65 61 6c 6c 6f 63 29 2c  ause a realloc),
30f92 20 73 6f 0a 2a 2a 20 74 68 69 73 20 6d 69 67 68   so.** this migh
30f93 74 20 62 65 20 77 6f 72 74 68 20 72 65 76 69 73  t be worth revis
30f94 69 74 69 6e 67 20 69 66 20 74 68 65 20 44 61 74  iting if the Dat
30f95 61 42 75 66 66 65 72 20 69 6d 70 6c 65 6d 65 6e  aBuffer implemen
30f96 74 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65  tation.** change
30f97 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
30f98 64 20 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 28  d dlcAddDoclist(
30f99 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f  DLCollector *pCo
30f9a 6c 6c 65 63 74 6f 72 2c 20 44 61 74 61 42 75 66  llector, DataBuf
30f9b 66 65 72 20 2a 62 29 7b 0a 20 20 69 66 28 20 70  fer *b){.  if( p
30f9c 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2e 69  Collector->dlw.i
30f9d 54 79 70 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29  Type>DL_DOCIDS )
30f9e 7b 0a 20 20 20 20 63 68 61 72 20 63 5b 56 41 52  {.    char c[VAR
30f9f 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e  INT_MAX];.    in
30fa0 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72  t n = fts3PutVar
30fa1 69 6e 74 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b  int(c, POS_END);
30fa2 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
30fa3 70 70 65 6e 64 32 28 62 2c 20 70 43 6f 6c 6c 65  ppend2(b, pColle
30fa4 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70  ctor->b.pData, p
30fa5 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61  Collector->b.nDa
30fa6 74 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 65 6c  ta, c, n);.  }el
30fa7 73 65 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66  se{.    dataBuff
30fa8 65 72 41 70 70 65 6e 64 28 62 2c 20 70 43 6f 6c  erAppend(b, pCol
30fa9 6c 65 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c  lector->b.pData,
30faa 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e   pCollector->b.n
30fab 44 61 74 61 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  Data);.  }.}.sta
30fac 74 69 63 20 76 6f 69 64 20 64 6c 63 4e 65 78 74  tic void dlcNext
30fad 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43  (DLCollector *pC
30fae 6f 6c 6c 65 63 74 6f 72 2c 20 73 71 6c 69 74 65  ollector, sqlite
30faf 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a  _int64 iDocid){.
30fb0 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26    plwTerminate(&
30fb1 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29  pCollector->plw)
30fb2 3b 0a 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26  ;.  plwDestroy(&
30fb3 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29  pCollector->plw)
30fb4 3b 0a 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f  ;.  plwInit(&pCo
30fb5 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70  llector->plw, &p
30fb6 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20  Collector->dlw, 
30fb7 69 44 6f 63 69 64 29 3b 0a 7d 0a 73 74 61 74 69  iDocid);.}.stati
30fb8 63 20 76 6f 69 64 20 64 6c 63 41 64 64 50 6f 73  c void dlcAddPos
30fb9 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43  (DLCollector *pC
30fba 6f 6c 6c 65 63 74 6f 72 2c 20 69 6e 74 20 69 43  ollector, int iC
30fbb 6f 6c 75 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c  olumn, int iPos,
30fbc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
30fbd 20 20 20 20 20 20 20 69 6e 74 20 69 53 74 61 72         int iStar
30fbe 74 4f 66 66 73 65 74 2c 20 69 6e 74 20 69 45 6e  tOffset, int iEn
30fbf 64 4f 66 66 73 65 74 29 7b 0a 20 20 70 6c 77 41  dOffset){.  plwA
30fc0 64 64 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  dd(&pCollector->
30fc1 70 6c 77 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50  plw, iColumn, iP
30fc2 6f 73 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74  os, iStartOffset
30fc3 2c 20 69 45 6e 64 4f 66 66 73 65 74 29 3b 0a 7d  , iEndOffset);.}
30fc4 0a 0a 73 74 61 74 69 63 20 44 4c 43 6f 6c 6c 65  ..static DLColle
30fc5 63 74 6f 72 20 2a 64 6c 63 4e 65 77 28 73 71 6c  ctor *dlcNew(sql
30fc6 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
30fc7 2c 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54  , DocListType iT
30fc8 79 70 65 29 7b 0a 20 20 44 4c 43 6f 6c 6c 65 63  ype){.  DLCollec
30fc9 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 20  tor *pCollector 
30fca 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
30fcb 28 73 69 7a 65 6f 66 28 44 4c 43 6f 6c 6c 65 63  (sizeof(DLCollec
30fcc 74 6f 72 29 29 3b 0a 20 20 64 61 74 61 42 75 66  tor));.  dataBuf
30fcd 66 65 72 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63  ferInit(&pCollec
30fce 74 6f 72 2d 3e 62 2c 20 30 29 3b 0a 20 20 64 6c  tor->b, 0);.  dl
30fcf 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f  wInit(&pCollecto
30fd0 72 2d 3e 64 6c 77 2c 20 69 54 79 70 65 2c 20 26  r->dlw, iType, &
30fd1 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a  pCollector->b);.
30fd2 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c    plwInit(&pColl
30fd3 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f  ector->plw, &pCo
30fd4 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44  llector->dlw, iD
30fd5 6f 63 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  ocid);.  return 
30fd6 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 0a 73 74  pCollector;.}.st
30fd7 61 74 69 63 20 76 6f 69 64 20 64 6c 63 44 65 6c  atic void dlcDel
30fd8 65 74 65 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20  ete(DLCollector 
30fd9 2a 70 43 6f 6c 6c 65 63 74 6f 72 29 7b 0a 20 20  *pCollector){.  
30fda 70 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c  plwDestroy(&pCol
30fdb 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20  lector->plw);.  
30fdc 64 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c  dlwDestroy(&pCol
30fdd 6c 65 63 74 6f 72 2d 3e 64 6c 77 29 3b 0a 20 20  lector->dlw);.  
30fde 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
30fdf 79 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62  y(&pCollector->b
30fe0 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 43  );.  SCRAMBLE(pC
30fe1 6f 6c 6c 65 63 74 6f 72 29 3b 0a 20 20 73 71 6c  ollector);.  sql
30fe2 69 74 65 33 5f 66 72 65 65 28 70 43 6f 6c 6c 65  ite3_free(pColle
30fe3 63 74 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f  ctor);.}.../* Co
30fe4 70 79 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64  py the doclist d
30fe5 61 74 61 20 6f 66 20 69 54 79 70 65 20 69 6e 20  ata of iType in 
30fe6 70 44 61 74 61 2f 6e 44 61 74 61 20 69 6e 74 6f  pData/nData into
30fe7 20 2a 6f 75 74 2c 20 74 72 69 6d 6d 69 6e 67 0a   *out, trimming.
30fe8 2a 2a 20 75 6e 6e 65 63 65 73 73 61 72 79 20 64  ** unnecessary d
30fe9 61 74 61 20 61 73 20 77 65 20 67 6f 2e 20 20 4f  ata as we go.  O
30fea 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 6d 61 74 63  nly columns matc
30feb 68 69 6e 67 20 69 43 6f 6c 75 6d 6e 20 61 72 65  hing iColumn are
30fec 0a 2a 2a 20 63 6f 70 69 65 64 2c 20 61 6c 6c 20  .** copied, all 
30fed 63 6f 6c 75 6d 6e 73 20 63 6f 70 69 65 64 20 69  columns copied i
30fee 66 20 69 43 6f 6c 75 6d 6e 20 69 73 20 2d 31 2e  f iColumn is -1.
30fef 20 20 45 6c 65 6d 65 6e 74 73 20 77 69 74 68 20    Elements with 
30ff0 6e 6f 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 20 63  no.** matching c
30ff1 6f 6c 75 6d 6e 73 20 61 72 65 20 64 72 6f 70 70  olumns are dropp
30ff2 65 64 2e 20 20 54 68 65 20 6f 75 74 70 75 74 20  ed.  The output 
30ff3 69 73 20 61 6e 20 69 4f 75 74 54 79 70 65 20 64  is an iOutType d
30ff4 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f  oclist..*/./* NO
30ff5 54 45 28 73 68 65 73 73 29 20 54 68 69 73 20 63  TE(shess) This c
30ff6 6f 64 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69  ode is only vali
30ff7 64 20 61 66 74 65 72 20 61 6c 6c 20 64 6f 63 6c  d after all docl
30ff8 69 73 74 73 20 61 72 65 20 6d 65 72 67 65 64 2e  ists are merged.
30ff9 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 72  .** If this is r
30ffa 75 6e 20 62 65 66 6f 72 65 20 6d 65 72 67 65 73  un before merges
30ffb 2c 20 74 68 65 6e 20 64 6f 63 6c 69 73 74 20 69  , then doclist i
30ffc 74 65 6d 73 20 77 68 69 63 68 20 72 65 70 72 65  tems which repre
30ffd 73 65 6e 74 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e  sent.** deletion
30ffe 20 77 69 6c 6c 20 62 65 20 74 72 69 6d 6d 65 64   will be trimmed
30fff 2c 20 61 6e 64 20 77 69 6c 6c 20 74 68 75 73 20  , and will thus 
31000 6e 6f 74 20 65 66 66 65 63 74 20 61 20 64 65 6c  not effect a del
31001 65 74 69 6f 6e 0a 2a 2a 20 64 75 72 69 6e 67 20  etion.** during 
31002 74 68 65 20 6d 65 72 67 65 2e 0a 2a 2f 0a 73 74  the merge..*/.st
31003 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73  atic void docLis
31004 74 54 72 69 6d 28 44 6f 63 4c 69 73 74 54 79 70  tTrim(DocListTyp
31005 65 20 69 54 79 70 65 2c 20 63 6f 6e 73 74 20 63  e iType, const c
31006 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
31007 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
31008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
31009 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 44 6f 63 4c  nt iColumn, DocL
3100a 69 73 74 54 79 70 65 20 69 4f 75 74 54 79 70 65  istType iOutType
3100b 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75  , DataBuffer *ou
3100c 74 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64  t){.  DLReader d
3100d 6c 52 65 61 64 65 72 3b 0a 20 20 44 4c 57 72 69  lReader;.  DLWri
3100e 74 65 72 20 64 6c 57 72 69 74 65 72 3b 0a 0a 20  ter dlWriter;.. 
3100f 20 61 73 73 65 72 74 28 20 69 4f 75 74 54 79 70   assert( iOutTyp
31010 65 3c 3d 69 54 79 70 65 20 29 3b 0a 0a 20 20 64  e<=iType );..  d
31011 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72  lrInit(&dlReader
31012 2c 20 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20  , iType, pData, 
31013 6e 44 61 74 61 29 3b 0a 20 20 64 6c 77 49 6e 69  nData);.  dlwIni
31014 74 28 26 64 6c 57 72 69 74 65 72 2c 20 69 4f 75  t(&dlWriter, iOu
31015 74 54 79 70 65 2c 20 6f 75 74 29 3b 0a 0a 20 20  tType, out);..  
31016 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64  while( !dlrAtEnd
31017 28 26 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20  (&dlReader) ){. 
31018 20 20 20 50 4c 52 65 61 64 65 72 20 70 6c 52 65     PLReader plRe
31019 61 64 65 72 3b 0a 20 20 20 20 50 4c 57 72 69 74  ader;.    PLWrit
3101a 65 72 20 70 6c 57 72 69 74 65 72 3b 0a 20 20 20  er plWriter;.   
3101b 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a   int match = 0;.
3101c 0a 20 20 20 20 70 6c 72 49 6e 69 74 28 26 70 6c  .    plrInit(&pl
3101d 52 65 61 64 65 72 2c 20 26 64 6c 52 65 61 64 65  Reader, &dlReade
3101e 72 29 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20  r);..    while( 
3101f 21 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61  !plrAtEnd(&plRea
31020 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20 69 66  der) ){.      if
31021 28 20 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c  ( iColumn==-1 ||
31022 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65   plrColumn(&plRe
31023 61 64 65 72 29 3d 3d 69 43 6f 6c 75 6d 6e 20 29  ader)==iColumn )
31024 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6d  {.        if( !m
31025 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20  atch ){.        
31026 20 20 70 6c 77 49 6e 69 74 28 26 70 6c 57 72 69    plwInit(&plWri
31027 74 65 72 2c 20 26 64 6c 57 72 69 74 65 72 2c 20  ter, &dlWriter, 
31028 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65 61 64  dlrDocid(&dlRead
31029 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  er));.          
3102a 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 20  match = 1;.     
3102b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 77     }.        plw
3102c 41 64 64 28 26 70 6c 57 72 69 74 65 72 2c 20 70  Add(&plWriter, p
3102d 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64  lrColumn(&plRead
3102e 65 72 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e  er), plrPosition
3102f 28 26 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20  (&plReader),.   
31030 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72 53              plrS
31031 74 61 72 74 4f 66 66 73 65 74 28 26 70 6c 52 65  tartOffset(&plRe
31032 61 64 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66  ader), plrEndOff
31033 73 65 74 28 26 70 6c 52 65 61 64 65 72 29 29 3b  set(&plReader));
31034 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
31035 6c 72 53 74 65 70 28 26 70 6c 52 65 61 64 65 72  lrStep(&plReader
31036 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
31037 20 6d 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20   match ){.      
31038 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26 70 6c  plwTerminate(&pl
31039 57 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 70  Writer);.      p
3103a 6c 77 44 65 73 74 72 6f 79 28 26 70 6c 57 72 69  lwDestroy(&plWri
3103b 74 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ter);.    }..   
3103c 20 70 6c 72 44 65 73 74 72 6f 79 28 26 70 6c 52   plrDestroy(&plR
3103d 65 61 64 65 72 29 3b 0a 20 20 20 20 64 6c 72 53  eader);.    dlrS
3103e 74 65 70 28 26 64 6c 52 65 61 64 65 72 29 3b 0a  tep(&dlReader);.
3103f 20 20 7d 0a 20 20 64 6c 77 44 65 73 74 72 6f 79    }.  dlwDestroy
31040 28 26 64 6c 57 72 69 74 65 72 29 3b 0a 20 20 64  (&dlWriter);.  d
31041 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61  lrDestroy(&dlRea
31042 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64  der);.}../* Used
31043 20 62 79 20 64 6f 63 4c 69 73 74 4d 65 72 67 65   by docListMerge
31044 28 29 20 74 6f 20 6b 65 65 70 20 64 6f 63 6c 69  () to keep docli
31045 73 74 73 20 69 6e 20 74 68 65 20 61 73 63 65 6e  sts in the ascen
31046 64 69 6e 67 20 6f 72 64 65 72 20 62 79 0a 2a 2a  ding order by.**
31047 20 64 6f 63 69 64 2c 20 74 68 65 6e 20 61 73 63   docid, then asc
31048 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20  ending order by 
31049 61 67 65 20 28 73 6f 20 74 68 65 20 6e 65 77 65  age (so the newe
3104a 73 74 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e  st comes first).
3104b 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
3104c 63 74 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64  ct OrderedDLRead
3104d 65 72 20 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  er {.  DLReader 
3104e 2a 70 52 65 61 64 65 72 3b 0a 0a 20 20 2f 2a 20  *pReader;..  /* 
3104f 54 4f 44 4f 28 73 68 65 73 73 29 20 49 66 20 77  TODO(shess) If w
31050 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 64 6f  e assume that do
31051 63 4c 69 73 74 4d 65 72 67 65 20 70 52 65 61 64  cListMerge pRead
31052 65 72 73 20 69 73 20 6f 72 64 65 72 65 64 20 62  ers is ordered b
31053 79 0a 20 20 2a 2a 20 61 67 65 20 28 77 68 69 63  y.  ** age (whic
31054 68 20 77 65 20 64 6f 29 2c 20 74 68 65 6e 20 77  h we do), then w
31055 65 20 63 6f 75 6c 64 20 75 73 65 20 70 52 65 61  e could use pRea
31056 64 65 72 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20  der comparisons 
31057 74 6f 20 62 72 65 61 6b 0a 20 20 2a 2a 20 74 69  to break.  ** ti
31058 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69  es..  */.  int i
31059 64 78 3b 0a 7d 20 4f 72 64 65 72 65 64 44 4c 52  dx;.} OrderedDLR
3105a 65 61 64 65 72 3b 0a 0a 2f 2a 20 4f 72 64 65 72  eader;../* Order
3105b 20 65 6f 66 20 74 6f 20 65 6e 64 2c 20 74 68 65   eof to end, the
3105c 6e 20 62 79 20 64 6f 63 69 64 20 61 73 63 2c 20  n by docid asc, 
3105d 69 64 78 20 64 65 73 63 2e 20 2a 2f 0a 73 74 61  idx desc. */.sta
3105e 74 69 63 20 69 6e 74 20 6f 72 64 65 72 65 64 44  tic int orderedD
3105f 4c 52 65 61 64 65 72 43 6d 70 28 4f 72 64 65 72  LReaderCmp(Order
31060 65 64 44 4c 52 65 61 64 65 72 20 2a 72 31 2c 20  edDLReader *r1, 
31061 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 20  OrderedDLReader 
31062 2a 72 32 29 7b 0a 20 20 69 66 28 20 64 6c 72 41  *r2){.  if( dlrA
31063 74 45 6e 64 28 72 31 2d 3e 70 52 65 61 64 65 72  tEnd(r1->pReader
31064 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72  ) ){.    if( dlr
31065 41 74 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65  AtEnd(r2->pReade
31066 72 29 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  r) ) return 0;  
31067 2f 2a 20 42 6f 74 68 20 61 74 45 6e 64 28 29 2e  /* Both atEnd().
31068 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
31069 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3106a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3106b 2a 20 4f 6e 6c 79 20 72 31 20 61 74 45 6e 64 28  * Only r1 atEnd(
3106c 29 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ). */.  }.  if( 
3106d 64 6c 72 41 74 45 6e 64 28 72 32 2d 3e 70 52 65  dlrAtEnd(r2->pRe
3106e 61 64 65 72 29 20 29 20 72 65 74 75 72 6e 20 2d  ader) ) return -
3106f 31 3b 20 20 20 2f 2a 20 4f 6e 6c 79 20 72 32 20  1;   /* Only r2 
31070 61 74 45 6e 64 28 29 2e 20 2a 2f 0a 0a 20 20 69  atEnd(). */..  i
31071 66 28 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e  f( dlrDocid(r1->
31072 70 52 65 61 64 65 72 29 3c 64 6c 72 44 6f 63 69  pReader)<dlrDoci
31073 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29  d(r2->pReader) )
31074 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
31075 28 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70  ( dlrDocid(r1->p
31076 52 65 61 64 65 72 29 3e 64 6c 72 44 6f 63 69 64  Reader)>dlrDocid
31077 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20  (r2->pReader) ) 
31078 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 2f 2a 20  return 1;..  /* 
31079 44 65 73 63 65 6e 64 69 6e 67 20 6f 6e 20 69 64  Descending on id
3107a 78 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  x. */.  return r
3107b 32 2d 3e 69 64 78 2d 72 31 2d 3e 69 64 78 3b 0a  2->idx-r1->idx;.
3107c 7d 0a 0a 2f 2a 20 42 75 62 62 6c 65 20 70 5b 30  }../* Bubble p[0
3107d 5d 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  ] to appropriate
3107e 20 70 6c 61 63 65 20 69 6e 20 70 5b 31 2e 2e 6e   place in p[1..n
3107f 2d 31 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68  -1].  Assumes th
31080 61 74 0a 2a 2a 20 70 5b 31 2e 2e 6e 2d 31 5d 20  at.** p[1..n-1] 
31081 69 73 20 61 6c 72 65 61 64 79 20 73 6f 72 74 65  is already sorte
31082 64 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  d..*/./* TODO(sh
31083 65 73 73 29 20 49 73 20 74 68 69 73 20 66 72 65  ess) Is this fre
31084 71 75 65 6e 74 20 65 6e 6f 75 67 68 20 74 6f 20  quent enough to 
31085 77 61 72 72 61 6e 74 20 61 20 62 69 6e 61 72 79  warrant a binary
31086 20 73 65 61 72 63 68 3f 0a 2a 2a 20 42 65 66 6f   search?.** Befo
31087 72 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  re implementing 
31088 74 68 61 74 2c 20 69 6e 73 74 72 75 6d 65 6e 74  that, instrument
31089 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 68 65   the code to che
3108a 63 6b 2e 20 20 49 6e 20 6d 6f 73 74 0a 2a 2a 20  ck.  In most.** 
3108b 63 75 72 72 65 6e 74 20 75 73 61 67 65 2c 20 49  current usage, I
3108c 20 65 78 70 65 63 74 20 74 68 61 74 20 70 5b 30   expect that p[0
3108d 5d 20 77 69 6c 6c 20 62 65 20 6c 65 73 73 20 74  ] will be less t
3108e 68 61 6e 20 70 5b 31 5d 20 61 20 76 65 72 79 0a  han p[1] a very.
3108f 2a 2a 20 68 69 67 68 20 70 72 6f 70 6f 72 74 69  ** high proporti
31090 6f 6e 20 6f 66 20 74 68 65 20 74 69 6d 65 2e 0a  on of the time..
31091 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
31092 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 52 65  rderedDLReaderRe
31093 6f 72 64 65 72 28 4f 72 64 65 72 65 64 44 4c 52  order(OrderedDLR
31094 65 61 64 65 72 20 2a 70 2c 20 69 6e 74 20 6e 29  eader *p, int n)
31095 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26  {.  while( n>1 &
31096 26 20 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65  & orderedDLReade
31097 72 43 6d 70 28 70 2c 20 70 2b 31 29 3e 30 20 29  rCmp(p, p+1)>0 )
31098 7b 0a 20 20 20 20 4f 72 64 65 72 65 64 44 4c 52  {.    OrderedDLR
31099 65 61 64 65 72 20 74 6d 70 20 3d 20 70 5b 30 5d  eader tmp = p[0]
3109a 3b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 70 5b 31  ;.    p[0] = p[1
3109b 5d 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20 74 6d  ];.    p[1] = tm
3109c 70 3b 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 20 20  p;.    n--;.    
3109d 70 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 47  p++;.  }.}../* G
3109e 69 76 65 6e 20 61 6e 20 61 72 72 61 79 20 6f 66  iven an array of
3109f 20 64 6f 63 6c 69 73 74 20 72 65 61 64 65 72 73   doclist readers
310a0 2c 20 6d 65 72 67 65 20 74 68 65 69 72 20 64 6f  , merge their do
310a1 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 73 0a 2a  clist elements.*
310a2 2a 20 69 6e 74 6f 20 6f 75 74 20 69 6e 20 73 6f  * into out in so
310a3 72 74 65 64 20 6f 72 64 65 72 20 28 62 79 20 64  rted order (by d
310a4 6f 63 69 64 29 2c 20 64 72 6f 70 70 69 6e 67 20  ocid), dropping 
310a5 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c  elements from ol
310a6 64 65 72 0a 2a 2a 20 72 65 61 64 65 72 73 20 77  der.** readers w
310a7 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 64  hen there is a d
310a8 75 70 6c 69 63 61 74 65 20 64 6f 63 69 64 2e 20  uplicate docid. 
310a9 20 70 52 65 61 64 65 72 73 20 69 73 20 61 73 73   pReaders is ass
310aa 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 6f 72  umed to be.** or
310ab 64 65 72 65 64 20 62 79 20 61 67 65 2c 20 6f 6c  dered by age, ol
310ac 64 65 73 74 20 66 69 72 73 74 2e 0a 2a 2f 0a 2f  dest first..*/./
310ad 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 6e 52  * TODO(shess) nR
310ae 65 61 64 65 72 73 20 6d 75 73 74 20 62 65 20 3c  eaders must be <
310af 3d 20 4d 45 52 47 45 5f 43 4f 55 4e 54 2e 20 20  = MERGE_COUNT.  
310b0 54 68 69 73 20 73 68 6f 75 6c 64 20 70 72 6f 62  This should prob
310b1 61 62 6c 79 0a 2a 2a 20 62 65 20 66 69 78 65 64  ably.** be fixed
310b2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
310b3 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 44 61   docListMerge(Da
310b4 74 61 42 75 66 66 65 72 20 2a 6f 75 74 2c 0a 20  taBuffer *out,. 
310b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
310b6 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72          DLReader
310b7 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20   *pReaders, int 
310b8 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 4f 72 64  nReaders){.  Ord
310b9 65 72 65 64 44 4c 52 65 61 64 65 72 20 72 65 61  eredDLReader rea
310ba 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54  ders[MERGE_COUNT
310bb 5d 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72  ];.  DLWriter wr
310bc 69 74 65 72 3b 0a 20 20 69 6e 74 20 69 2c 20 6e  iter;.  int i, n
310bd 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
310be 70 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e  pStart = 0;.  in
310bf 74 20 6e 53 74 61 72 74 20 3d 20 30 3b 0a 20 20  t nStart = 0;.  
310c0 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 46 69  sqlite_int64 iFi
310c1 72 73 74 44 6f 63 69 64 20 3d 20 30 2c 20 69 4c  rstDocid = 0, iL
310c2 61 73 74 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20  astDocid = 0;.. 
310c3 20 61 73 73 65 72 74 28 20 6e 52 65 61 64 65 72   assert( nReader
310c4 73 3e 30 20 29 3b 0a 20 20 69 66 28 20 6e 52 65  s>0 );.  if( nRe
310c5 61 64 65 72 73 3d 3d 31 20 29 7b 0a 20 20 20 20  aders==1 ){.    
310c6 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
310c7 28 6f 75 74 2c 20 64 6c 72 44 6f 63 44 61 74 61  (out, dlrDocData
310c8 28 70 52 65 61 64 65 72 73 29 2c 20 64 6c 72 41  (pReaders), dlrA
310c9 6c 6c 44 61 74 61 42 79 74 65 73 28 70 52 65 61  llDataBytes(pRea
310ca 64 65 72 73 29 29 3b 0a 20 20 20 20 72 65 74 75  ders));.    retu
310cb 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  rn;.  }..  asser
310cc 74 28 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52  t( nReaders<=MER
310cd 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 6e 20  GE_COUNT );.  n 
310ce 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
310cf 69 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29  i<nReaders; i++)
310d0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52  {.    assert( pR
310d1 65 61 64 65 72 73 5b 69 5d 2e 69 54 79 70 65 3d  eaders[i].iType=
310d2 3d 70 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79  =pReaders[0].iTy
310d3 70 65 20 29 3b 0a 20 20 20 20 72 65 61 64 65 72  pe );.    reader
310d4 73 5b 69 5d 2e 70 52 65 61 64 65 72 20 3d 20 70  s[i].pReader = p
310d5 52 65 61 64 65 72 73 2b 69 3b 0a 20 20 20 20 72  Readers+i;.    r
310d6 65 61 64 65 72 73 5b 69 5d 2e 69 64 78 20 3d 20  eaders[i].idx = 
310d7 69 3b 0a 20 20 20 20 6e 20 2b 3d 20 64 6c 72 41  i;.    n += dlrA
310d8 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70 52 65  llDataBytes(&pRe
310d9 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aders[i]);.  }. 
310da 20 2f 2a 20 43 6f 6e 73 65 72 76 61 74 69 76 65   /* Conservative
310db 6c 79 20 73 69 7a 65 20 6f 75 74 70 75 74 20 74  ly size output t
310dc 6f 20 73 75 6d 20 6f 66 20 69 6e 70 75 74 73 2e  o sum of inputs.
310dd 20 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20    Output should 
310de 65 6e 64 0a 20 20 2a 2a 20 75 70 20 73 74 72 69  end.  ** up stri
310df 63 74 6c 79 20 73 6d 61 6c 6c 65 72 20 74 68 61  ctly smaller tha
310e0 6e 20 69 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20 20  n input..  */.  
310e1 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64  dataBufferExpand
310e2 28 6f 75 74 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20  (out, n);..  /* 
310e3 47 65 74 20 74 68 65 20 72 65 61 64 65 72 73 20  Get the readers 
310e4 69 6e 74 6f 20 73 6f 72 74 65 64 20 6f 72 64 65  into sorted orde
310e5 72 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69  r. */.  while( i
310e6 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 6f 72 64 65  -->0 ){.    orde
310e7 72 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64  redDLReaderReord
310e8 65 72 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52  er(readers+i, nR
310e9 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a  eaders-i);.  }..
310ea 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65    dlwInit(&write
310eb 72 2c 20 70 52 65 61 64 65 72 73 5b 30 5d 2e 69  r, pReaders[0].i
310ec 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20 20 77 68  Type, out);.  wh
310ed 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 72  ile( !dlrAtEnd(r
310ee 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65  eaders[0].pReade
310ef 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  r) ){.    sqlite
310f0 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 20 3d 20  _int64 iDocid = 
310f1 64 6c 72 44 6f 63 69 64 28 72 65 61 64 65 72 73  dlrDocid(readers
310f2 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20  [0].pReader);.. 
310f3 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
310f4 20 61 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20   a continuation 
310f5 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 62  of the current b
310f6 75 66 66 65 72 20 74 6f 20 63 6f 70 79 2c 20 65  uffer to copy, e
310f7 78 74 65 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61  xtend.    ** tha
310f8 74 20 62 75 66 66 65 72 2e 20 20 6d 65 6d 63 70  t buffer.  memcp
310f9 79 28 29 20 73 65 65 6d 73 20 74 6f 20 62 65 20  y() seems to be 
310fa 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 69  more efficient i
310fb 66 20 69 74 20 68 61 73 20 61 0a 20 20 20 20 2a  f it has a.    *
310fc 2a 20 6c 6f 74 73 20 6f 66 20 64 61 74 61 20 74  * lots of data t
310fd 6f 20 63 6f 70 79 2e 0a 20 20 20 20 2a 2f 0a 20  o copy..    */. 
310fe 20 20 20 69 66 28 20 64 6c 72 44 6f 63 44 61 74     if( dlrDocDat
310ff 61 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65  a(readers[0].pRe
31100 61 64 65 72 29 3d 3d 70 53 74 61 72 74 2b 6e 53  ader)==pStart+nS
31101 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 6e 53  tart ){.      nS
31102 74 61 72 74 20 2b 3d 20 64 6c 72 44 6f 63 44 61  tart += dlrDocDa
31103 74 61 42 79 74 65 73 28 72 65 61 64 65 72 73 5b  taBytes(readers[
31104 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  0].pReader);.   
31105 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
31106 28 20 70 53 74 61 72 74 21 3d 30 20 29 7b 0a 20  ( pStart!=0 ){. 
31107 20 20 20 20 20 20 20 64 6c 77 41 70 70 65 6e 64         dlwAppend
31108 28 26 77 72 69 74 65 72 2c 20 70 53 74 61 72 74  (&writer, pStart
31109 2c 20 6e 53 74 61 72 74 2c 20 69 46 69 72 73 74  , nStart, iFirst
3110a 44 6f 63 69 64 2c 20 69 4c 61 73 74 44 6f 63 69  Docid, iLastDoci
3110b 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  d);.      }.    
3110c 20 20 70 53 74 61 72 74 20 3d 20 64 6c 72 44 6f    pStart = dlrDo
3110d 63 44 61 74 61 28 72 65 61 64 65 72 73 5b 30 5d  cData(readers[0]
3110e 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20  .pReader);.     
3110f 20 6e 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63   nStart = dlrDoc
31110 44 61 74 61 42 79 74 65 73 28 72 65 61 64 65 72  DataBytes(reader
31111 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20  s[0].pReader);. 
31112 20 20 20 20 20 69 46 69 72 73 74 44 6f 63 69 64       iFirstDocid
31113 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 7d   = iDocid;.    }
31114 0a 20 20 20 20 69 4c 61 73 74 44 6f 63 69 64 20  .    iLastDocid 
31115 3d 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 64 6c  = iDocid;.    dl
31116 72 53 74 65 70 28 72 65 61 64 65 72 73 5b 30 5d  rStep(readers[0]
31117 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20  .pReader);..    
31118 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 6f 66 20 74  /* Drop all of t
31119 68 65 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74  he older element
3111a 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
3111b 64 6f 63 69 64 2e 20 2a 2f 0a 20 20 20 20 66 6f  docid. */.    fo
3111c 72 28 69 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72  r(i=1; i<nReader
3111d 73 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  s &&.           
3111e 20 20 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64    !dlrAtEnd(read
3111f 65 72 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 20  ers[i].pReader) 
31120 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  &&.             
31121 64 6c 72 44 6f 63 69 64 28 72 65 61 64 65 72 73  dlrDocid(readers
31122 5b 69 5d 2e 70 52 65 61 64 65 72 29 3d 3d 69 44  [i].pReader)==iD
31123 6f 63 69 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ocid; i++){.    
31124 20 20 64 6c 72 53 74 65 70 28 72 65 61 64 65 72    dlrStep(reader
31125 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20  s[i].pReader);. 
31126 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 74     }..    /* Get
31127 20 74 68 65 20 72 65 61 64 65 72 73 20 62 61 63   the readers bac
31128 6b 20 69 6e 74 6f 20 6f 72 64 65 72 2e 20 2a 2f  k into order. */
31129 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e  .    while( i-->
3112a 30 20 29 7b 0a 20 20 20 20 20 20 6f 72 64 65 72  0 ){.      order
3112b 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65  edDLReaderReorde
3112c 72 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65  r(readers+i, nRe
3112d 61 64 65 72 73 2d 69 29 3b 0a 20 20 20 20 7d 0a  aders-i);.    }.
3112e 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f    }..  /* Copy o
3112f 76 65 72 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e  ver any remainin
31130 67 20 65 6c 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20  g elements. */. 
31131 20 69 66 28 20 6e 53 74 61 72 74 3e 30 20 29 20   if( nStart>0 ) 
31132 64 6c 77 41 70 70 65 6e 64 28 26 77 72 69 74 65  dlwAppend(&write
31133 72 2c 20 70 53 74 61 72 74 2c 20 6e 53 74 61 72  r, pStart, nStar
31134 74 2c 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20  t, iFirstDocid, 
31135 69 4c 61 73 74 44 6f 63 69 64 29 3b 0a 20 20 64  iLastDocid);.  d
31136 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
31137 72 29 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72  r);.}../* Helper
31138 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 6f   function for po
31139 73 4c 69 73 74 55 6e 69 6f 6e 28 29 2e 20 20 43  sListUnion().  C
3113a 6f 6d 70 61 72 65 73 20 74 68 65 20 63 75 72 72  ompares the curr
3113b 65 6e 74 20 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20  ent position.** 
3113c 62 65 74 77 65 65 6e 20 6c 65 66 74 20 61 6e 64  between left and
3113d 20 72 69 67 68 74 2c 20 72 65 74 75 72 6e 69 6e   right, returnin
3113e 67 20 61 73 20 73 74 61 6e 64 61 72 64 20 43 20  g as standard C 
3113f 69 64 69 6f 6d 20 6f 66 20 3c 30 20 69 66 0a 2a  idiom of <0 if.*
31140 2a 20 6c 65 66 74 3c 72 69 67 68 74 2c 20 3e 30  * left<right, >0
31141 20 69 66 20 6c 65 66 74 3e 72 69 67 68 74 2c 20   if left>right, 
31142 61 6e 64 20 30 20 69 66 20 6c 65 66 74 3d 3d 72  and 0 if left==r
31143 69 67 68 74 2e 20 20 22 45 6e 64 22 20 61 6c 77  ight.  "End" alw
31144 61 79 73 0a 2a 2a 20 63 6f 6d 70 61 72 65 73 20  ays.** compares 
31145 67 72 65 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  greater..*/.stat
31146 69 63 20 69 6e 74 20 70 6f 73 4c 69 73 74 43 6d  ic int posListCm
31147 70 28 50 4c 52 65 61 64 65 72 20 2a 70 4c 65 66  p(PLReader *pLef
31148 74 2c 20 50 4c 52 65 61 64 65 72 20 2a 70 52 69  t, PLReader *pRi
31149 67 68 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ght){.  assert( 
3114a 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 52  pLeft->iType==pR
3114b 69 67 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a 20  ight->iType );. 
3114c 20 69 66 28 20 70 4c 65 66 74 2d 3e 69 54 79 70   if( pLeft->iTyp
3114d 65 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72  e==DL_DOCIDS ) r
3114e 65 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20  eturn 0;..  if( 
3114f 70 6c 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20  plrAtEnd(pLeft) 
31150 29 20 72 65 74 75 72 6e 20 70 6c 72 41 74 45 6e  ) return plrAtEn
31151 64 28 70 52 69 67 68 74 29 20 3f 20 30 20 3a 20  d(pRight) ? 0 : 
31152 31 3b 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e  1;.  if( plrAtEn
31153 64 28 70 52 69 67 68 74 29 20 29 20 72 65 74 75  d(pRight) ) retu
31154 72 6e 20 2d 31 3b 0a 0a 20 20 69 66 28 20 70 6c  rn -1;..  if( pl
31155 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c 70  rColumn(pLeft)<p
31156 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29  lrColumn(pRight)
31157 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
31158 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c  if( plrColumn(pL
31159 65 66 74 29 3e 70 6c 72 43 6f 6c 75 6d 6e 28 70  eft)>plrColumn(p
3115a 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
3115b 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 50 6f 73  1;..  if( plrPos
3115c 69 74 69 6f 6e 28 70 4c 65 66 74 29 3c 70 6c 72  ition(pLeft)<plr
3115d 50 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29  Position(pRight)
3115e 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
3115f 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28  if( plrPosition(
31160 70 4c 65 66 74 29 3e 70 6c 72 50 6f 73 69 74 69  pLeft)>plrPositi
31161 6f 6e 28 70 52 69 67 68 74 29 20 29 20 72 65 74  on(pRight) ) ret
31162 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 70 4c 65  urn 1;.  if( pLe
31163 66 74 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  ft->iType==DL_PO
31164 53 49 54 49 4f 4e 53 20 29 20 72 65 74 75 72 6e  SITIONS ) return
31165 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c 72 53 74   0;..  if( plrSt
31166 61 72 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29  artOffset(pLeft)
31167 3c 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28  <plrStartOffset(
31168 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
31169 20 2d 31 3b 0a 20 20 69 66 28 20 70 6c 72 53 74   -1;.  if( plrSt
3116a 61 72 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29  artOffset(pLeft)
3116b 3e 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28  >plrStartOffset(
3116c 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
3116d 20 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 45 6e   1;..  if( plrEn
3116e 64 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70  dOffset(pLeft)<p
3116f 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52 69 67  lrEndOffset(pRig
31170 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  ht) ) return -1;
31171 0a 20 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66  .  if( plrEndOff
31172 73 65 74 28 70 4c 65 66 74 29 3e 70 6c 72 45 6e  set(pLeft)>plrEn
31173 64 4f 66 66 73 65 74 28 70 52 69 67 68 74 29 20  dOffset(pRight) 
31174 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 72  ) return 1;..  r
31175 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57  eturn 0;.}../* W
31176 72 69 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f  rite the union o
31177 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73  f position lists
31178 20 69 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52   in pLeft and pR
31179 69 67 68 74 20 74 6f 20 70 4f 75 74 2e 0a 2a 2a  ight to pOut..**
3117a 20 22 55 6e 69 6f 6e 22 20 69 6e 20 74 68 69 73   "Union" in this
3117b 20 63 61 73 65 20 6d 65 61 6e 69 6e 67 20 22 41   case meaning "A
3117c 6c 6c 20 75 6e 69 71 75 65 20 70 6f 73 69 74 69  ll unique positi
3117d 6f 6e 20 74 75 70 6c 65 73 22 2e 20 20 53 68 6f  on tuples".  Sho
3117e 75 6c 64 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68  uld.** work with
3117f 20 61 6e 79 20 64 6f 63 6c 69 73 74 20 74 79 70   any doclist typ
31180 65 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 20 69  e, though both i
31181 6e 70 75 74 73 20 61 6e 64 20 74 68 65 20 6f 75  nputs and the ou
31182 74 70 75 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  tput.** should b
31183 65 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 2e  e the same type.
31184 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
31185 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28 44 4c 52  posListUnion(DLR
31186 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 44 4c  eader *pLeft, DL
31187 52 65 61 64 65 72 20 2a 70 52 69 67 68 74 2c 20  Reader *pRight, 
31188 44 4c 57 72 69 74 65 72 20 2a 70 4f 75 74 29 7b  DLWriter *pOut){
31189 0a 20 20 50 4c 52 65 61 64 65 72 20 6c 65 66 74  .  PLReader left
3118a 2c 20 72 69 67 68 74 3b 0a 20 20 50 4c 57 72 69  , right;.  PLWri
3118b 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 61  ter writer;..  a
3118c 73 73 65 72 74 28 20 64 6c 72 44 6f 63 69 64 28  ssert( dlrDocid(
3118d 70 4c 65 66 74 29 3d 3d 64 6c 72 44 6f 63 69 64  pLeft)==dlrDocid
3118e 28 70 52 69 67 68 74 29 20 29 3b 0a 20 20 61 73  (pRight) );.  as
3118f 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79  sert( pLeft->iTy
31190 70 65 3d 3d 70 52 69 67 68 74 2d 3e 69 54 79 70  pe==pRight->iTyp
31191 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
31192 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 4f 75  Left->iType==pOu
31193 74 2d 3e 69 54 79 70 65 20 29 3b 0a 0a 20 20 70  t->iType );..  p
31194 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 70 4c  lrInit(&left, pL
31195 65 66 74 29 3b 0a 20 20 70 6c 72 49 6e 69 74 28  eft);.  plrInit(
31196 26 72 69 67 68 74 2c 20 70 52 69 67 68 74 29 3b  &right, pRight);
31197 0a 20 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74  .  plwInit(&writ
31198 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63  er, pOut, dlrDoc
31199 69 64 28 70 4c 65 66 74 29 29 3b 0a 0a 20 20 77  id(pLeft));..  w
3119a 68 69 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28  hile( !plrAtEnd(
3119b 26 6c 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74  &left) || !plrAt
3119c 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
3119d 20 20 20 69 6e 74 20 63 20 3d 20 70 6f 73 4c 69     int c = posLi
3119e 73 74 43 6d 70 28 26 6c 65 66 74 2c 20 26 72 69  stCmp(&left, &ri
3119f 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 63 3c  ght);.    if( c<
311a0 30 20 29 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f  0 ){.      plwCo
311a1 70 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66  py(&writer, &lef
311a2 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65  t);.      plrSte
311a3 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65  p(&left);.    }e
311a4 6c 73 65 20 69 66 28 20 63 3e 30 20 29 7b 0a 20  lse if( c>0 ){. 
311a5 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72       plwCopy(&wr
311a6 69 74 65 72 2c 20 26 72 69 67 68 74 29 3b 0a 20  iter, &right);. 
311a7 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72 69       plrStep(&ri
311a8 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ght);.    }else{
311a9 0a 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26  .      plwCopy(&
311aa 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a  writer, &left);.
311ab 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c        plrStep(&l
311ac 65 66 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53  eft);.      plrS
311ad 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
311ae 20 7d 0a 20 20 7d 0a 0a 20 20 70 6c 77 54 65 72   }.  }..  plwTer
311af 6d 69 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b  minate(&writer);
311b0 0a 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77  .  plwDestroy(&w
311b1 72 69 74 65 72 29 3b 0a 20 20 70 6c 72 44 65 73  riter);.  plrDes
311b2 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70  troy(&left);.  p
311b3 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74  lrDestroy(&right
311b4 29 3b 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20 74  );.}../* Write t
311b5 68 65 20 75 6e 69 6f 6e 20 6f 66 20 64 6f 63 6c  he union of docl
311b6 69 73 74 73 20 69 6e 20 70 4c 65 66 74 20 61 6e  ists in pLeft an
311b7 64 20 70 52 69 67 68 74 20 74 6f 20 70 4f 75 74  d pRight to pOut
311b8 2e 20 20 46 6f 72 0a 2a 2a 20 64 6f 63 69 64 73  .  For.** docids
311b9 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 65 74 77 65   in common betwe
311ba 65 6e 20 74 68 65 20 69 6e 70 75 74 73 2c 20 74  en the inputs, t
311bb 68 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 20  he union of the 
311bc 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 6c 69 73 74  position.** list
311bd 73 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49  s is written.  I
311be 6e 70 75 74 73 20 61 6e 64 20 6f 75 74 70 75 74  nputs and output
311bf 73 20 61 72 65 20 61 6c 77 61 79 73 20 74 79 70  s are always typ
311c0 65 20 44 4c 5f 44 45 46 41 55 4c 54 2e 0a 2a 2f  e DL_DEFAULT..*/
311c1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
311c2 4c 69 73 74 55 6e 69 6f 6e 28 0a 20 20 63 6f 6e  ListUnion(.  con
311c3 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20  st char *pLeft, 
311c4 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e  int nLeft,.  con
311c5 73 74 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c  st char *pRight,
311c6 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 44   int nRight,.  D
311c7 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75 74 20  ataBuffer *pOut 
311c8 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
311c9 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69  e combined docli
311ca 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  st here */.){.  
311cb 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72  DLReader left, r
311cc 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74 65 72  ight;.  DLWriter
311cd 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20   writer;..  if( 
311ce 6e 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20  nLeft==0 ){.    
311cf 69 66 28 20 6e 52 69 67 68 74 21 3d 30 29 20 64  if( nRight!=0) d
311d0 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
311d1 70 4f 75 74 2c 20 70 52 69 67 68 74 2c 20 6e 52  pOut, pRight, nR
311d2 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72  ight);.    retur
311d3 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 69  n;.  }.  if( nRi
311d4 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61  ght==0 ){.    da
311d5 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70  taBufferAppend(p
311d6 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66  Out, pLeft, nLef
311d7 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
311d8 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26    }..  dlrInit(&
311d9 6c 65 66 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54  left, DL_DEFAULT
311da 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
311db 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68  .  dlrInit(&righ
311dc 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  t, DL_DEFAULT, p
311dd 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a  Right, nRight);.
311de 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65    dlwInit(&write
311df 72 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  r, DL_DEFAULT, p
311e0 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
311e1 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
311e2 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 72   || !dlrAtEnd(&r
311e3 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28  ight) ){.    if(
311e4 20 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74   dlrAtEnd(&right
311e5 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f  ) ){.      dlwCo
311e6 70 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66  py(&writer, &lef
311e7 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  t);.      dlrSte
311e8 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65  p(&left);.    }e
311e9 6c 73 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64  lse if( dlrAtEnd
311ea 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  (&left) ){.     
311eb 20 64 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72   dlwCopy(&writer
311ec 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
311ed 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
311ee 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
311ef 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c  dlrDocid(&left)<
311f0 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
311f1 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70   ){.      dlwCop
311f2 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74  y(&writer, &left
311f3 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
311f4 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
311f5 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
311f6 26 6c 65 66 74 29 3e 64 6c 72 44 6f 63 69 64 28  &left)>dlrDocid(
311f7 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
311f8 20 64 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72   dlwCopy(&writer
311f9 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
311fa 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
311fb 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
311fc 20 20 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28     posListUnion(
311fd 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20 26  &left, &right, &
311fe 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 64  writer);.      d
311ff 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
31200 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69       dlrStep(&ri
31201 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ght);.    }.  }.
31202 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c  .  dlrDestroy(&l
31203 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72  eft);.  dlrDestr
31204 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c  oy(&right);.  dl
31205 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  wDestroy(&writer
31206 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69  );.}../* .** Thi
31207 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
31208 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
31209 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3120a 20 6f 66 20 70 68 72 61 73 65 20 61 6e 64 0a 2a   of phrase and.*
3120b 2a 20 4e 45 41 52 20 6d 61 74 63 68 69 6e 67 2e  * NEAR matching.
3120c 0a 2a 2a 0a 2a 2a 20 70 4c 65 66 74 20 61 6e 64  .**.** pLeft and
3120d 20 70 52 69 67 68 74 20 61 72 65 20 44 4c 52 65   pRight are DLRe
3120e 61 64 65 72 73 20 70 6f 73 69 74 69 6f 6e 65 64  aders positioned
3120f 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 6f 63   to the same doc
31210 69 64 20 69 6e 0a 2a 2a 20 6c 69 73 74 73 20 6f  id in.** lists o
31211 66 20 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49  f type DL_POSITI
31212 4f 4e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ON. This functio
31213 6e 20 77 72 69 74 65 73 20 61 6e 20 65 6e 74 72  n writes an entr
31214 79 20 74 6f 20 74 68 65 0a 2a 2a 20 44 4c 57 72  y to the.** DLWr
31215 69 74 65 72 20 70 4f 75 74 20 66 6f 72 20 65 61  iter pOut for ea
31216 63 68 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70  ch position in p
31217 52 69 67 68 74 20 74 68 61 74 20 69 73 20 6c 65  Right that is le
31218 73 73 20 74 68 61 6e 0a 2a 2a 20 28 6e 4e 65 61  ss than.** (nNea
31219 72 2b 31 29 20 67 72 65 61 74 65 72 20 28 62 75  r+1) greater (bu
3121a 74 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 6f  t not equal to o
3121b 72 20 73 6d 61 6c 6c 65 72 29 20 74 68 61 6e 20  r smaller) than 
3121c 61 20 70 6f 73 69 74 69 6f 6e 20 0a 2a 2a 20 69  a position .** i
3121d 6e 20 70 4c 65 66 74 2e 20 46 6f 72 20 65 78 61  n pLeft. For exa
3121e 6d 70 6c 65 2c 20 69 66 20 6e 4e 65 61 72 20 69  mple, if nNear i
3121f 73 20 30 2c 20 61 6e 64 20 74 68 65 20 70 6f 73  s 0, and the pos
31220 69 74 69 6f 6e 73 20 63 6f 6e 74 61 69 6e 65 64  itions contained
31221 0a 2a 2a 20 62 79 20 70 4c 65 66 74 20 61 6e 64  .** by pLeft and
31222 20 70 52 69 67 68 74 20 61 72 65 3a 0a 2a 2a 0a   pRight are:.**.
31223 2a 2a 20 20 20 20 70 4c 65 66 74 3a 20 20 35 20  **    pLeft:  5 
31224 31 30 20 31 35 20 32 30 0a 2a 2a 20 20 20 20 70  10 15 20.**    p
31225 52 69 67 68 74 3a 20 36 20 20 39 20 31 37 20 32  Right: 6  9 17 2
31226 31 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65  1.**.** then the
31227 20 64 6f 63 69 64 20 69 73 20 61 64 64 65 64 20   docid is added 
31228 74 6f 20 70 4f 75 74 2e 20 49 66 20 70 4f 75 74  to pOut. If pOut
31229 20 69 73 20 6f 66 20 74 79 70 65 20 44 4c 5f 50   is of type DL_P
3122a 4f 53 49 54 49 4f 4e 53 2c 0a 2a 2a 20 74 68 65  OSITIONS,.** the
3122b 6e 20 61 20 70 6f 73 69 74 69 6f 6e 69 64 73 20  n a positionids 
3122c 22 36 22 20 61 6e 64 20 22 32 31 22 20 61 72 65  "6" and "21" are
3122d 20 61 6c 73 6f 20 61 64 64 65 64 20 74 6f 20 70   also added to p
3122e 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f  Out..**.** If bo
3122f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 69  olean argument i
31230 73 53 61 76 65 4c 65 66 74 20 69 73 20 74 72 75  sSaveLeft is tru
31231 65 2c 20 74 68 65 6e 20 70 6f 73 69 74 69 6f 6e  e, then position
31232 69 64 73 20 61 72 65 20 63 6f 70 69 65 64 0a 2a  ids are copied.*
31233 2a 20 66 72 6f 6d 20 70 4c 65 66 74 20 69 6e 73  * from pLeft ins
31234 74 65 61 64 20 6f 66 20 70 52 69 67 68 74 2e 20  tead of pRight. 
31235 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61  In the example a
31236 62 6f 76 65 2c 20 74 68 65 20 70 6f 73 69 74 69  bove, the positi
31237 6f 6e 73 20 22 35 22 0a 2a 2a 20 61 6e 64 20 22  ons "5".** and "
31238 32 30 22 20 77 6f 75 6c 64 20 62 65 20 61 64 64  20" would be add
31239 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 22 36  ed instead of "6
3123a 22 20 61 6e 64 20 22 32 31 22 2e 0a 2a 2f 0a 73  " and "21"..*/.s
3123b 74 61 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69  tatic void posLi
3123c 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 0a 20  stPhraseMerge(. 
3123d 20 44 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74   DLReader *pLeft
3123e 2c 20 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70  , .  DLReader *p
3123f 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65  Right,.  int nNe
31240 61 72 2c 0a 20 20 69 6e 74 20 69 73 53 61 76 65  ar,.  int isSave
31241 4c 65 66 74 2c 0a 20 20 44 4c 57 72 69 74 65 72  Left,.  DLWriter
31242 20 2a 70 4f 75 74 0a 29 7b 0a 20 20 50 4c 52 65   *pOut.){.  PLRe
31243 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
31244 3b 0a 20 20 50 4c 57 72 69 74 65 72 20 77 72 69  ;.  PLWriter wri
31245 74 65 72 3b 0a 20 20 69 6e 74 20 6d 61 74 63 68  ter;.  int match
31246 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
31247 20 64 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29   dlrDocid(pLeft)
31248 3d 3d 64 6c 72 44 6f 63 69 64 28 70 52 69 67 68  ==dlrDocid(pRigh
31249 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  t) );.  assert( 
3124a 70 4f 75 74 2d 3e 69 54 79 70 65 21 3d 44 4c 5f  pOut->iType!=DL_
3124b 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
3124c 53 20 29 3b 0a 0a 20 20 70 6c 72 49 6e 69 74 28  S );..  plrInit(
3124d 26 6c 65 66 74 2c 20 70 4c 65 66 74 29 3b 0a 20  &left, pLeft);. 
3124e 20 70 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c   plrInit(&right,
3124f 20 70 52 69 67 68 74 29 3b 0a 0a 20 20 77 68 69   pRight);..  whi
31250 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c  le( !plrAtEnd(&l
31251 65 66 74 29 20 26 26 20 21 70 6c 72 41 74 45 6e  eft) && !plrAtEn
31252 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
31253 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26   if( plrColumn(&
31254 6c 65 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28  left)<plrColumn(
31255 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
31256 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b   plrStep(&left);
31257 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
31258 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 3e  lrColumn(&left)>
31259 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74  plrColumn(&right
3125a 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74  ) ){.      plrSt
3125b 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
3125c 7d 65 6c 73 65 20 69 66 28 20 70 6c 72 50 6f 73  }else if( plrPos
3125d 69 74 69 6f 6e 28 26 6c 65 66 74 29 3e 3d 70 6c  ition(&left)>=pl
3125e 72 50 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74  rPosition(&right
3125f 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74  ) ){.      plrSt
31260 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
31261 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
31262 20 28 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 72   (plrPosition(&r
31263 69 67 68 74 29 2d 70 6c 72 50 6f 73 69 74 69 6f  ight)-plrPositio
31264 6e 28 26 6c 65 66 74 29 29 3c 3d 28 6e 4e 65 61  n(&left))<=(nNea
31265 72 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  r+1) ){.        
31266 69 66 28 20 21 6d 61 74 63 68 20 29 7b 0a 20 20  if( !match ){.  
31267 20 20 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28          plwInit(
31268 26 77 72 69 74 65 72 2c 20 70 4f 75 74 2c 20 64  &writer, pOut, d
31269 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29 29 3b  lrDocid(pLeft));
3126a 0a 20 20 20 20 20 20 20 20 20 20 6d 61 74 63 68  .          match
3126b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
3126c 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 53          if( !isS
3126d 61 76 65 4c 65 66 74 20 29 7b 0a 20 20 20 20 20  aveLeft ){.     
3126e 20 20 20 20 20 70 6c 77 41 64 64 28 26 77 72 69       plwAdd(&wri
3126f 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26  ter, plrColumn(&
31270 72 69 67 68 74 29 2c 20 70 6c 72 50 6f 73 69 74  right), plrPosit
31271 69 6f 6e 28 26 72 69 67 68 74 29 2c 20 30 2c 20  ion(&right), 0, 
31272 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  0);.        }els
31273 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  e{.          plw
31274 41 64 64 28 26 77 72 69 74 65 72 2c 20 70 6c 72  Add(&writer, plr
31275 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 2c 20 70  Column(&left), p
31276 6c 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66 74  lrPosition(&left
31277 29 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  ), 0, 0);.      
31278 20 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 72 53    }.        plrS
31279 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
3127a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3127b 20 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29    plrStep(&left)
3127c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
3127d 20 20 7d 0a 0a 20 20 69 66 28 20 6d 61 74 63 68    }..  if( match
3127e 20 29 7b 0a 20 20 20 20 70 6c 77 54 65 72 6d 69   ){.    plwTermi
3127f 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20  nate(&writer);. 
31280 20 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77     plwDestroy(&w
31281 72 69 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 70  riter);.  }..  p
31282 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
31283 3b 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26  ;.  plrDestroy(&
31284 72 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  right);.}../*.**
31285 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c   Compare the val
31286 75 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ues pointed to b
31287 79 20 74 68 65 20 50 4c 52 65 61 64 65 72 73 20  y the PLReaders 
31288 70 61 73 73 65 64 20 61 73 20 61 72 67 75 6d 65  passed as argume
31289 6e 74 73 2e 20 0a 2a 2a 20 52 65 74 75 72 6e 20  nts. .** Return 
3128a 2d 31 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  -1 if the value 
3128b 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 4c  pointed to by pL
3128c 65 66 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65  eft is considere
3128d 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 74  d less than.** t
3128e 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  he value pointed
3128f 20 74 6f 20 62 79 20 70 52 69 67 68 74 2c 20 2b   to by pRight, +
31290 31 20 69 66 20 69 74 20 69 73 20 63 6f 6e 73 69  1 if it is consi
31291 64 65 72 65 64 20 67 72 65 61 74 65 72 0a 2a 2a  dered greater.**
31292 20 74 68 61 6e 20 69 74 2c 20 6f 72 20 30 20 69   than it, or 0 i
31293 66 20 69 74 20 69 73 20 65 71 75 61 6c 2e 20 69  f it is equal. i
31294 2e 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 2a  .e..**.**     (*
31295 70 4c 65 66 74 20 2d 20 2a 70 52 69 67 68 74 29  pLeft - *pRight)
31296 0a 2a 2a 0a 2a 2a 20 41 20 50 4c 52 65 61 64 65  .**.** A PLReade
31297 72 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65  r that is in the
31298 20 45 4f 46 20 63 6f 6e 64 69 74 69 6f 6e 20 69   EOF condition i
31299 73 20 63 6f 6e 73 69 64 65 72 65 64 20 67 72 65  s considered gre
3129a 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 79  ater than.** any
3129b 20 6f 74 68 65 72 2e 20 49 66 20 6e 65 69 74 68   other. If neith
3129c 65 72 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69  er argument is i
3129d 6e 20 45 4f 46 20 73 74 61 74 65 2c 20 74 68 65  n EOF state, the
3129e 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
3129f 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e 28 29 20  .** plrColumn() 
312a0 69 73 20 75 73 65 64 2e 20 49 66 20 74 68 65 20  is used. If the 
312a1 70 6c 72 43 6f 6c 75 6d 6e 28 29 20 76 61 6c 75  plrColumn() valu
312a2 65 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68  es are equal, th
312a3 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20  e.** comparison 
312a4 69 73 20 6f 6e 20 74 68 65 20 62 61 73 69 73 20  is on the basis 
312a5 6f 66 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 29  of plrPosition()
312a6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
312a7 70 6c 72 43 6f 6d 70 61 72 65 28 50 4c 52 65 61  plrCompare(PLRea
312a8 64 65 72 20 2a 70 4c 65 66 74 2c 20 50 4c 52 65  der *pLeft, PLRe
312a9 61 64 65 72 20 2a 70 52 69 67 68 74 29 7b 0a 20  ader *pRight){. 
312aa 20 61 73 73 65 72 74 28 21 70 6c 72 41 74 45 6e   assert(!plrAtEn
312ab 64 28 70 4c 65 66 74 29 20 7c 7c 20 21 70 6c 72  d(pLeft) || !plr
312ac 41 74 45 6e 64 28 70 52 69 67 68 74 29 29 3b 0a  AtEnd(pRight));.
312ad 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28  .  if( plrAtEnd(
312ae 70 52 69 67 68 74 29 20 7c 7c 20 70 6c 72 41 74  pRight) || plrAt
312af 45 6e 64 28 70 4c 65 66 74 29 20 29 7b 0a 20 20  End(pLeft) ){.  
312b0 20 20 72 65 74 75 72 6e 20 28 70 6c 72 41 74 45    return (plrAtE
312b1 6e 64 28 70 52 69 67 68 74 29 20 3f 20 2d 31 20  nd(pRight) ? -1 
312b2 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  : 1);.  }.  if( 
312b3 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29  plrColumn(pLeft)
312b4 21 3d 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67  !=plrColumn(pRig
312b5 68 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ht) ){.    retur
312b6 6e 20 28 28 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c  n ((plrColumn(pL
312b7 65 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70  eft)<plrColumn(p
312b8 52 69 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31  Right)) ? -1 : 1
312b9 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72  );.  }.  if( plr
312ba 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74 29 21  Position(pLeft)!
312bb 3d 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 69  =plrPosition(pRi
312bc 67 68 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ght) ){.    retu
312bd 72 6e 20 28 28 70 6c 72 50 6f 73 69 74 69 6f 6e  rn ((plrPosition
312be 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74  (pLeft)<plrPosit
312bf 69 6f 6e 28 70 52 69 67 68 74 29 29 20 3f 20 2d  ion(pRight)) ? -
312c0 31 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65  1 : 1);.  }.  re
312c1 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 65  turn 0;.}../* We
312c2 20 68 61 76 65 20 74 77 6f 20 64 6f 63 6c 69 73   have two doclis
312c3 74 73 20 77 69 74 68 20 70 6f 73 69 74 69 6f 6e  ts with position
312c4 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52  s:  pLeft and pR
312c5 69 67 68 74 2e 20 44 65 70 65 6e 64 69 6e 67 0a  ight. Depending.
312c6 2a 2a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ** on the value 
312c7 6f 66 20 74 68 65 20 6e 4e 65 61 72 20 70 61 72  of the nNear par
312c8 61 6d 65 74 65 72 2c 20 70 65 72 66 6f 72 6d 20  ameter, perform 
312c9 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65 0a  either a phrase.
312ca 2a 2a 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  ** intersection 
312cb 28 69 66 20 6e 4e 65 61 72 3d 3d 30 29 20 6f 72  (if nNear==0) or
312cc 20 61 20 4e 45 41 52 20 69 6e 74 65 72 73 65 63   a NEAR intersec
312cd 74 69 6f 6e 20 28 69 66 20 6e 4e 65 61 72 3e 30  tion (if nNear>0
312ce 29 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74  ).** and write t
312cf 68 65 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20  he results into 
312d0 70 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 68  pOut..**.** A ph
312d1 72 61 73 65 20 69 6e 74 65 72 73 65 63 74 69 6f  rase intersectio
312d2 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f  n means that two
312d3 20 64 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20   documents only 
312d4 6d 61 74 63 68 0a 2a 2a 20 69 66 20 70 4c 65 66  match.** if pLef
312d5 74 2e 69 50 6f 73 2b 31 3d 3d 70 52 69 67 68 74  t.iPos+1==pRight
312d6 2e 69 50 6f 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e  .iPos..**.** A N
312d7 45 41 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  EAR intersection
312d8 20 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20   means that two 
312d9 64 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d  documents only m
312da 61 74 63 68 20 69 66 20 0a 2a 2a 20 28 61 62 73  atch if .** (abs
312db 28 70 4c 65 66 74 2e 69 50 6f 73 2d 70 52 69 67  (pLeft.iPos-pRig
312dc 68 74 2e 69 50 6f 73 29 3c 6e 4e 65 61 72 29 2e  ht.iPos)<nNear).
312dd 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 4e 45 41 52  .**.** If a NEAR
312de 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73   intersection is
312df 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e   requested, then
312e0 20 74 68 65 20 6e 50 68 72 61 73 65 20 61 72 67   the nPhrase arg
312e1 75 6d 65 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20  ument should.** 
312e2 62 65 20 70 61 73 73 65 64 20 74 68 65 20 6e 75  be passed the nu
312e3 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69  mber of tokens i
312e4 6e 20 74 68 65 20 74 77 6f 20 6f 70 65 72 61 6e  n the two operan
312e5 64 73 20 74 6f 20 74 68 65 20 4e 45 41 52 20 6f  ds to the NEAR o
312e6 70 65 72 61 74 6f 72 0a 2a 2a 20 63 6f 6d 62 69  perator.** combi
312e7 6e 65 64 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ned. For example
312e8 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 51 75  :.**.**       Qu
312e9 65 72 79 20 73 79 6e 74 61 78 20 20 20 20 20 20  ery syntax      
312ea 20 20 20 20 20 20 20 20 20 6e 50 68 72 61 73 65           nPhrase
312eb 0a 2a 2a 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  .**      -------
312ec 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
312ed 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
312ee 20 20 20 20 20 20 20 22 41 20 42 20 43 22 20 4e         "A B C" N
312ef 45 41 52 20 22 44 20 45 22 20 20 20 20 20 20 20  EAR "D E"       
312f0 20 20 35 0a 2a 2a 20 20 20 20 20 20 20 41 20 4e    5.**       A N
312f1 45 41 52 20 42 20 20 20 20 20 20 20 20 20 20 20  EAR B           
312f2 20 20 20 20 20 20 20 20 32 0a 2a 2a 0a 2a 2a 20          2.**.** 
312f3 69 54 79 70 65 20 63 6f 6e 74 72 6f 6c 73 20 74  iType controls t
312f4 68 65 20 74 79 70 65 20 6f 66 20 64 61 74 61 20  he type of data 
312f5 77 72 69 74 74 65 6e 20 74 6f 20 70 4f 75 74 2e  written to pOut.
312f6 20 20 49 66 20 69 54 79 70 65 20 69 73 0a 2a 2a    If iType is.**
312f7 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 74   DL_POSITIONS, t
312f8 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 61 72 65  he positions are
312f9 20 74 68 6f 73 65 20 66 72 6f 6d 20 70 52 69 67   those from pRig
312fa 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ht..*/.static vo
312fb 69 64 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65  id docListPhrase
312fc 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63  Merge(.  const c
312fd 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20  har *pLeft, int 
312fe 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63  nLeft,.  const c
312ff 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74  har *pRight, int
31300 20 6e 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e   nRight,.  int n
31301 4e 65 61 72 2c 20 20 20 20 20 20 20 20 20 20 20  Near,           
31302 20 2f 2a 20 30 20 66 6f 72 20 61 20 70 68 72 61   /* 0 for a phra
31303 73 65 20 6d 65 72 67 65 2c 20 6e 6f 6e 2d 7a 65  se merge, non-ze
31304 72 6f 20 66 6f 72 20 61 20 4e 45 41 52 20 6d 65  ro for a NEAR me
31305 72 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 68  rge */.  int nPh
31306 72 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  rase,          /
31307 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  * Number of toke
31308 6e 73 20 69 6e 20 6c 65 66 74 2b 72 69 67 68 74  ns in left+right
31309 20 6f 70 65 72 61 6e 64 73 20 74 6f 20 4e 45 41   operands to NEA
3130a 52 20 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79  R */.  DocListTy
3130b 70 65 20 69 54 79 70 65 2c 20 20 20 20 2f 2a 20  pe iType,    /* 
3130c 54 79 70 65 20 6f 66 20 64 6f 63 6c 69 73 74 20  Type of doclist 
3130d 74 6f 20 77 72 69 74 65 20 74 6f 20 70 4f 75 74  to write to pOut
3130e 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
3130f 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
31310 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
31311 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
31312 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
31313 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
31314 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
31315 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
31316 7c 7c 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72  || nRight==0 ) r
31317 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
31318 28 20 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49  ( iType!=DL_POSI
31319 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b  TIONS_OFFSETS );
3131a 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66  ..  dlrInit(&lef
3131b 74 2c 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c  t, DL_POSITIONS,
3131c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a   pLeft, nLeft);.
3131d 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74    dlrInit(&right
3131e 2c 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20  , DL_POSITIONS, 
3131f 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
31320 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
31321 65 72 2c 20 69 54 79 70 65 2c 20 70 4f 75 74 29  er, iType, pOut)
31322 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72  ;..  while( !dlr
31323 41 74 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20  AtEnd(&left) && 
31324 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74  !dlrAtEnd(&right
31325 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72  ) ){.    if( dlr
31326 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72  Docid(&left)<dlr
31327 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b  Docid(&right) ){
31328 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
31329 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  left);.    }else
3132a 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72   if( dlrDocid(&r
3132b 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26  ight)<dlrDocid(&
3132c 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
3132d 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
3132e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3132f 20 69 66 28 20 6e 4e 65 61 72 3d 3d 30 20 29 7b   if( nNear==0 ){
31330 0a 20 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74  .        posList
31331 50 68 72 61 73 65 4d 65 72 67 65 28 26 6c 65 66  PhraseMerge(&lef
31332 74 2c 20 26 72 69 67 68 74 2c 20 30 2c 20 30 2c  t, &right, 0, 0,
31333 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20   &writer);.     
31334 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
31335 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63 63  /* This case occ
31336 75 72 73 20 77 68 65 6e 20 74 77 6f 20 74 65 72  urs when two ter
31337 6d 73 20 28 73 69 6d 70 6c 65 20 74 65 72 6d 73  ms (simple terms
31338 20 6f 72 20 70 68 72 61 73 65 73 29 20 61 72 65   or phrases) are
31339 0a 20 20 20 20 20 20 20 20 20 2a 20 63 6f 6e 6e  .         * conn
3133a 65 63 74 65 64 20 62 79 20 61 20 4e 45 41 52 20  ected by a NEAR 
3133b 6f 70 65 72 61 74 6f 72 2c 20 73 70 61 6e 20 28  operator, span (
3133c 6e 4e 65 61 72 2b 31 29 2e 20 69 2e 65 2e 0a 20  nNear+1). i.e.. 
3133d 20 20 20 20 20 20 20 20 2a 0a 20 20 20 20 20 20          *.      
3133e 20 20 20 2a 20 20 20 20 20 27 22 74 65 72 72 69     *     '"terri
3133f 62 6c 65 20 63 6f 6d 70 61 6e 79 22 20 4e 45 41  ble company" NEA
31340 52 20 77 69 64 67 65 74 27 0a 20 20 20 20 20 20  R widget'.      
31341 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 61     */.        Da
31342 74 61 42 75 66 66 65 72 20 6f 6e 65 20 3d 20 7b  taBuffer one = {
31343 30 2c 20 30 2c 20 30 7d 3b 0a 20 20 20 20 20 20  0, 0, 0};.      
31344 20 20 44 61 74 61 42 75 66 66 65 72 20 74 77 6f    DataBuffer two
31345 20 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20   = {0, 0, 0};.. 
31346 20 20 20 20 20 20 20 44 4c 57 72 69 74 65 72 20         DLWriter 
31347 64 6c 77 72 69 74 65 72 32 3b 0a 20 20 20 20 20  dlwriter2;.     
31348 20 20 20 44 4c 52 65 61 64 65 72 20 64 72 31 20     DLReader dr1 
31349 3d 20 7b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  = {0, 0, 0, 0, 0
3134a 7d 3b 20 0a 20 20 20 20 20 20 20 20 44 4c 52 65  }; .        DLRe
3134b 61 64 65 72 20 64 72 32 20 3d 20 7b 30 2c 20 30  ader dr2 = {0, 0
3134c 2c 20 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20  , 0, 0, 0};..   
3134d 20 20 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c       dlwInit(&dl
3134e 77 72 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20  writer2, iType, 
3134f 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 70  &one);.        p
31350 6f 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67  osListPhraseMerg
31351 65 28 26 72 69 67 68 74 2c 20 26 6c 65 66 74 2c  e(&right, &left,
31352 20 6e 4e 65 61 72 2d 33 2b 6e 50 68 72 61 73 65   nNear-3+nPhrase
31353 2c 20 31 2c 20 26 64 6c 77 72 69 74 65 72 32 29  , 1, &dlwriter2)
31354 3b 0a 20 20 20 20 20 20 20 20 64 6c 77 49 6e 69  ;.        dlwIni
31355 74 28 26 64 6c 77 72 69 74 65 72 32 2c 20 69 54  t(&dlwriter2, iT
31356 79 70 65 2c 20 26 74 77 6f 29 3b 0a 20 20 20 20  ype, &two);.    
31357 20 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73      posListPhras
31358 65 4d 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72  eMerge(&left, &r
31359 69 67 68 74 2c 20 6e 4e 65 61 72 2d 31 2c 20 30  ight, nNear-1, 0
3135a 2c 20 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 0a  , &dlwriter2);..
3135b 20 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e          if( one.
3135c 6e 44 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26  nData) dlrInit(&
3135d 64 72 31 2c 20 69 54 79 70 65 2c 20 6f 6e 65 2e  dr1, iType, one.
3135e 70 44 61 74 61 2c 20 6f 6e 65 2e 6e 44 61 74 61  pData, one.nData
3135f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 74  );.        if( t
31360 77 6f 2e 6e 44 61 74 61 29 20 64 6c 72 49 6e 69  wo.nData) dlrIni
31361 74 28 26 64 72 32 2c 20 69 54 79 70 65 2c 20 74  t(&dr2, iType, t
31362 77 6f 2e 70 44 61 74 61 2c 20 74 77 6f 2e 6e 44  wo.pData, two.nD
31363 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  ata);..        i
31364 66 28 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72  f( !dlrAtEnd(&dr
31365 31 29 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28  1) || !dlrAtEnd(
31366 26 64 72 32 29 20 29 7b 0a 20 20 20 20 20 20 20  &dr2) ){.       
31367 20 20 20 50 4c 52 65 61 64 65 72 20 70 72 31 20     PLReader pr1 
31368 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 20 20 20  = {0};.         
31369 20 50 4c 52 65 61 64 65 72 20 70 72 32 20 3d 20   PLReader pr2 = 
3136a 7b 30 7d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  {0};..          
3136b 50 4c 57 72 69 74 65 72 20 70 6c 77 72 69 74 65  PLWriter plwrite
3136c 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  r;.          plw
3136d 49 6e 69 74 28 26 70 6c 77 72 69 74 65 72 2c 20  Init(&plwriter, 
3136e 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69  &writer, dlrDoci
3136f 64 28 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29  d(dlrAtEnd(&dr1)
31370 3f 26 64 72 32 3a 26 64 72 31 29 29 3b 0a 0a 20  ?&dr2:&dr1));.. 
31371 20 20 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65           if( one
31372 2e 6e 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74  .nData ) plrInit
31373 28 26 70 72 31 2c 20 26 64 72 31 29 3b 0a 20 20  (&pr1, &dr1);.  
31374 20 20 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e          if( two.
31375 6e 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28  nData ) plrInit(
31376 26 70 72 32 2c 20 26 64 72 32 29 3b 0a 20 20 20  &pr2, &dr2);.   
31377 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 70         while( !p
31378 6c 72 41 74 45 6e 64 28 26 70 72 31 29 20 7c 7c  lrAtEnd(&pr1) ||
31379 20 21 70 6c 72 41 74 45 6e 64 28 26 70 72 32 29   !plrAtEnd(&pr2)
3137a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
3137b 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 70  int iCompare = p
3137c 6c 72 43 6f 6d 70 61 72 65 28 26 70 72 31 2c 20  lrCompare(&pr1, 
3137d 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr2);.         
3137e 20 20 20 73 77 69 74 63 68 28 20 69 43 6f 6d 70     switch( iComp
3137f 61 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  are ){.         
31380 20 20 20 20 20 63 61 73 65 20 2d 31 3a 0a 20 20       case -1:.  
31381 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
31382 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65 72 2c  wCopy(&plwriter,
31383 20 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20   &pr1);.        
31384 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28          plrStep(
31385 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr1);.         
31386 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31387 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
31388 20 31 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20   1:.            
31389 20 20 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77      plwCopy(&plw
3138a 72 69 74 65 72 2c 20 26 70 72 32 29 3b 0a 20 20  riter, &pr2);.  
3138b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
3138c 72 53 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20  rStep(&pr2);.   
3138d 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
3138e 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
3138f 20 20 63 61 73 65 20 30 3a 0a 20 20 20 20 20 20    case 0:.      
31390 20 20 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70            plwCop
31391 79 28 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72  y(&plwriter, &pr
31392 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
31393 20 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 31      plrStep(&pr1
31394 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
31395 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 32 29     plrStep(&pr2)
31396 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
31397 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
31398 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
31399 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77   }.          plw
3139a 54 65 72 6d 69 6e 61 74 65 28 26 70 6c 77 72 69  Terminate(&plwri
3139b 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ter);.        }.
3139c 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
3139d 65 72 44 65 73 74 72 6f 79 28 26 6f 6e 65 29 3b  erDestroy(&one);
3139e 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
3139f 66 65 72 44 65 73 74 72 6f 79 28 26 74 77 6f 29  ferDestroy(&two)
313a0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
313a1 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a  dlrStep(&left);.
313a2 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72        dlrStep(&r
313a3 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
313a4 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ..  dlrDestroy(&
313a5 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74  left);.  dlrDest
313a6 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64  roy(&right);.  d
313a7 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
313a8 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76  r);.}../* We hav
313a9 65 20 74 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20  e two DL_DOCIDS 
313aa 64 6f 63 6c 69 73 74 73 3a 20 20 70 4c 65 66 74  doclists:  pLeft
313ab 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20   and pRight..** 
313ac 57 72 69 74 65 20 74 68 65 20 69 6e 74 65 72 73  Write the inters
313ad 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  ection of these 
313ae 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e 74  two doclists int
313af 6f 20 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20 44  o pOut as a.** D
313b0 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74  L_DOCIDS doclist
313b1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
313b2 20 64 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67 65   docListAndMerge
313b3 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
313b4 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
313b5 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
313b6 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
313b7 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72  ht,.  DataBuffer
313b8 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
313b9 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
313ba 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
313bb 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
313bc 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
313bd 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
313be 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
313bf 7c 7c 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72  || nRight==0 ) r
313c0 65 74 75 72 6e 3b 0a 0a 20 20 64 6c 72 49 6e 69  eturn;..  dlrIni
313c1 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49  t(&left, DL_DOCI
313c2 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74  DS, pLeft, nLeft
313c3 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69  );.  dlrInit(&ri
313c4 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  ght, DL_DOCIDS, 
313c5 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
313c6 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
313c7 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  er, DL_DOCIDS, p
313c8 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
313c9 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
313ca 20 26 26 20 21 64 6c 72 41 74 45 6e 64 28 26 72   && !dlrAtEnd(&r
313cb 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28  ight) ){.    if(
313cc 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
313cd 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  <dlrDocid(&right
313ce 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74  ) ){.      dlrSt
313cf 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d  ep(&left);.    }
313d0 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69  else if( dlrDoci
313d1 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63  d(&right)<dlrDoc
313d2 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20  id(&left) ){.   
313d3 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
313d4 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
313d5 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69       dlwAdd(&wri
313d6 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c  ter, dlrDocid(&l
313d7 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  eft));.      dlr
313d8 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
313d9 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
313da 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
313db 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66   dlrDestroy(&lef
313dc 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79  t);.  dlrDestroy
313dd 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44  (&right);.  dlwD
313de 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
313df 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74  .}../* We have t
313e0 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  wo DL_DOCIDS doc
313e1 6c 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e  lists:  pLeft an
313e2 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69  d pRight..** Wri
313e3 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  te the union of 
313e4 74 68 65 73 65 20 74 77 6f 20 64 6f 63 6c 69 73  these two doclis
313e5 74 73 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20  ts into pOut as 
313e6 61 0a 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 20 64  a.** DL_DOCIDS d
313e7 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  oclist..*/.stati
313e8 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 4f 72  c void docListOr
313e9 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63  Merge(.  const c
313ea 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20  har *pLeft, int 
313eb 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63  nLeft,.  const c
313ec 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74  har *pRight, int
313ed 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42   nRight,.  DataB
313ee 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20  uffer *pOut     
313ef 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
313f0 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68  mbined doclist h
313f1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65  ere */.){.  DLRe
313f2 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
313f3 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69  ;.  DLWriter wri
313f4 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66  ter;..  if( nLef
313f5 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t==0 ){.    if( 
313f6 6e 52 69 67 68 74 21 3d 30 20 29 20 64 61 74 61  nRight!=0 ) data
313f7 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75  BufferAppend(pOu
313f8 74 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68  t, pRight, nRigh
313f9 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
313fa 20 20 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74    }.  if( nRight
313fb 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42  ==0 ){.    dataB
313fc 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74  ufferAppend(pOut
313fd 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
313fe 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
313ff 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66  ..  dlrInit(&lef
31400 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4c  t, DL_DOCIDS, pL
31401 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64  eft, nLeft);.  d
31402 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44  lrInit(&right, D
31403 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69 67 68 74  L_DOCIDS, pRight
31404 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77  , nRight);.  dlw
31405 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c  Init(&writer, DL
31406 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b 0a  _DOCIDS, pOut);.
31407 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
31408 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20 21 64  End(&left) || !d
31409 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
3140a 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74  ){.    if( dlrAt
3140b 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
3140c 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69       dlwAdd(&wri
3140d 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c  ter, dlrDocid(&l
3140e 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  eft));.      dlr
3140f 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
31410 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 41 74   }else if( dlrAt
31411 45 6e 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20  End(&left) ){.  
31412 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
31413 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69  er, dlrDocid(&ri
31414 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  ght));.      dlr
31415 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
31416 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44    }else if( dlrD
31417 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44  ocid(&left)<dlrD
31418 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a  ocid(&right) ){.
31419 20 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72        dlwAdd(&wr
3141a 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26  iter, dlrDocid(&
3141b 6c 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c  left));.      dl
3141c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
3141d 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44    }else if( dlrD
3141e 6f 63 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72  ocid(&right)<dlr
3141f 44 6f 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a  Docid(&left) ){.
31420 20 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72        dlwAdd(&wr
31421 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26  iter, dlrDocid(&
31422 72 69 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64  right));.      d
31423 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
31424 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
31425 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
31426 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
31427 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
31428 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64  (&left);.      d
31429 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
3142a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72      }.  }..  dlr
3142b 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a  Destroy(&left);.
3142c 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69    dlrDestroy(&ri
3142d 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72  ght);.  dlwDestr
3142e 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a  oy(&writer);.}..
3142f 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20 44  /* We have two D
31430 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74  L_DOCIDS doclist
31431 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52  s:  pLeft and pR
31432 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 69  ight..** Write i
31433 6e 74 6f 20 70 4f 75 74 20 61 73 20 44 4c 5f 44  nto pOut as DL_D
31434 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20 63 6f  OCIDS doclist co
31435 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 64 6f 63  ntaining all doc
31436 75 6d 65 6e 74 73 20 74 68 61 74 0a 2a 2a 20 6f  uments that.** o
31437 63 63 75 72 20 69 6e 20 70 4c 65 66 74 20 62 75  ccur in pLeft bu
31438 74 20 6e 6f 74 20 69 6e 20 70 52 69 67 68 74 2e  t not in pRight.
31439 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3143a 64 6f 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72  docListExceptMer
3143b 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ge(.  const char
3143c 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65   *pLeft, int nLe
3143d 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ft,.  const char
3143e 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52   *pRight, int nR
3143f 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66  ight,.  DataBuff
31440 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a  er *pOut      /*
31441 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69   Write the combi
31442 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65  ned doclist here
31443 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65   */.){.  DLReade
31444 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20  r left, right;. 
31445 20 44 4c 57 72 69 74 65 72 20 77 72 69 74 65 72   DLWriter writer
31446 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d  ;..  if( nLeft==
31447 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
31448 28 20 6e 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20  ( nRight==0 ){. 
31449 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
3144a 65 6e 64 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c  end(pOut, pLeft,
3144b 20 6e 4c 65 66 74 29 3b 0a 20 20 20 20 72 65 74   nLeft);.    ret
3144c 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49  urn;.  }..  dlrI
3144d 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f  nit(&left, DL_DO
3144e 43 49 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65  CIDS, pLeft, nLe
3144f 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26  ft);.  dlrInit(&
31450 72 69 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53  right, DL_DOCIDS
31451 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74  , pRight, nRight
31452 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72  );.  dlwInit(&wr
31453 69 74 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c  iter, DL_DOCIDS,
31454 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65   pOut);..  while
31455 28 20 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66  ( !dlrAtEnd(&lef
31456 74 29 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28  t) ){.    while(
31457 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68   !dlrAtEnd(&righ
31458 74 29 20 26 26 20 64 6c 72 44 6f 63 69 64 28 26  t) && dlrDocid(&
31459 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28  right)<dlrDocid(
3145a 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20  &left) ){.      
3145b 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  dlrStep(&right);
3145c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
3145d 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
3145e 7c 7c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  || dlrDocid(&lef
3145f 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67  t)<dlrDocid(&rig
31460 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77  ht) ){.      dlw
31461 41 64 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72  Add(&writer, dlr
31462 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20  Docid(&left));. 
31463 20 20 20 7d 0a 20 20 20 20 64 6c 72 53 74 65 70     }.    dlrStep
31464 28 26 6c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20  (&left);.  }..  
31465 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74  dlrDestroy(&left
31466 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28  );.  dlrDestroy(
31467 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65  &right);.  dlwDe
31468 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
31469 7d 0a 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  }..static char *
3146a 73 74 72 69 6e 67 5f 64 75 70 5f 6e 28 63 6f 6e  string_dup_n(con
3146b 73 74 20 63 68 61 72 20 2a 73 2c 20 69 6e 74 20  st char *s, int 
3146c 6e 29 7b 0a 20 20 63 68 61 72 20 2a 73 74 72 20  n){.  char *str 
3146d 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
3146e 28 6e 20 2b 20 31 29 3b 0a 20 20 6d 65 6d 63 70  (n + 1);.  memcp
3146f 79 28 73 74 72 2c 20 73 2c 20 6e 29 3b 0a 20 20  y(str, s, n);.  
31470 73 74 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20  str[n] = '\0';. 
31471 20 72 65 74 75 72 6e 20 73 74 72 3b 0a 7d 0a 0a   return str;.}..
31472 2f 2a 20 44 75 70 6c 69 63 61 74 65 20 61 20 73  /* Duplicate a s
31473 74 72 69 6e 67 3b 20 74 68 65 20 63 61 6c 6c 65  tring; the calle
31474 72 20 6d 75 73 74 20 66 72 65 65 28 29 20 74 68  r must free() th
31475 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
31476 67 2e 0a 20 2a 20 28 57 65 20 64 6f 6e 27 74 20  g.. * (We don't 
31477 75 73 65 20 73 74 72 64 75 70 28 29 20 73 69 6e  use strdup() sin
31478 63 65 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72  ce it is not par
31479 74 20 6f 66 20 74 68 65 20 73 74 61 6e 64 61 72  t of the standar
3147a 64 20 43 20 6c 69 62 72 61 72 79 20 61 6e 64 0a  d C library and.
3147b 20 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 76   * may not be av
3147c 61 69 6c 61 62 6c 65 20 65 76 65 72 79 77 68 65  ailable everywhe
3147d 72 65 2e 29 20 2a 2f 0a 73 74 61 74 69 63 20 63  re.) */.static c
3147e 68 61 72 20 2a 73 74 72 69 6e 67 5f 64 75 70 28  har *string_dup(
3147f 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 29 7b 0a  const char *s){.
31480 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 5f    return string_
31481 64 75 70 5f 6e 28 73 2c 20 73 74 72 6c 65 6e 28  dup_n(s, strlen(
31482 73 29 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 6d 61  s));.}../* Forma
31483 74 20 61 20 73 74 72 69 6e 67 2c 20 72 65 70 6c  t a string, repl
31484 61 63 69 6e 67 20 65 61 63 68 20 6f 63 63 75 72  acing each occur
31485 72 65 6e 63 65 20 6f 66 20 74 68 65 20 25 20 63  rence of the % c
31486 68 61 72 61 63 74 65 72 20 77 69 74 68 0a 20 2a  haracter with. *
31487 20 7a 44 62 2e 7a 4e 61 6d 65 2e 20 20 54 68 69   zDb.zName.  Thi
31488 73 20 6d 61 79 20 62 65 20 6d 6f 72 65 20 63 6f  s may be more co
31489 6e 76 65 6e 69 65 6e 74 20 74 68 61 6e 20 73 71  nvenient than sq
3148a 6c 69 74 65 5f 6d 70 72 69 6e 74 66 28 29 0a 20  lite_mprintf(). 
3148b 2a 20 77 68 65 6e 20 6f 6e 65 20 73 74 72 69 6e  * when one strin
3148c 67 20 69 73 20 75 73 65 64 20 72 65 70 65 61 74  g is used repeat
3148d 65 64 6c 79 20 69 6e 20 61 20 66 6f 72 6d 61 74  edly in a format
3148e 20 73 74 72 69 6e 67 2e 0a 20 2a 20 54 68 65 20   string.. * The 
3148f 63 61 6c 6c 65 72 20 6d 75 73 74 20 66 72 65 65  caller must free
31490 28 29 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  () the returned 
31491 73 74 72 69 6e 67 2e 20 2a 2f 0a 73 74 61 74 69  string. */.stati
31492 63 20 63 68 61 72 20 2a 73 74 72 69 6e 67 5f 66  c char *string_f
31493 6f 72 6d 61 74 28 63 6f 6e 73 74 20 63 68 61 72  ormat(const char
31494 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 20 20 20   *zFormat,.     
31495 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31496 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
31497 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61   *zDb, const cha
31498 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 63 6f 6e  r *zName){.  con
31499 73 74 20 63 68 61 72 20 2a 70 3b 0a 20 20 73 69  st char *p;.  si
3149a 7a 65 5f 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20  ze_t len = 0;.  
3149b 73 69 7a 65 5f 74 20 6e 44 62 20 3d 20 73 74 72  size_t nDb = str
3149c 6c 65 6e 28 7a 44 62 29 3b 0a 20 20 73 69 7a 65  len(zDb);.  size
3149d 5f 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65  _t nName = strle
3149e 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 73 69 7a 65  n(zName);.  size
3149f 5f 74 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d  _t nFullTableNam
314a0 65 20 3d 20 6e 44 62 2b 31 2b 6e 4e 61 6d 65 3b  e = nDb+1+nName;
314a1 0a 20 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b  .  char *result;
314a2 0a 20 20 63 68 61 72 20 2a 72 3b 0a 0a 20 20 2f  .  char *r;..  /
314a3 2a 20 66 69 72 73 74 20 63 6f 6d 70 75 74 65 20  * first compute 
314a4 6c 65 6e 67 74 68 20 6e 65 65 64 65 64 20 2a 2f  length needed */
314a5 0a 20 20 66 6f 72 28 70 20 3d 20 7a 46 6f 72 6d  .  for(p = zForm
314a6 61 74 20 3b 20 2a 70 20 3b 20 2b 2b 70 29 7b 0a  at ; *p ; ++p){.
314a7 20 20 20 20 6c 65 6e 20 2b 3d 20 28 2a 70 3d 3d      len += (*p==
314a8 27 25 27 20 3f 20 6e 46 75 6c 6c 54 61 62 6c 65  '%' ? nFullTable
314a9 4e 61 6d 65 20 3a 20 31 29 3b 0a 20 20 7d 0a 20  Name : 1);.  }. 
314aa 20 6c 65 6e 20 2b 3d 20 31 3b 20 20 2f 2a 20 66   len += 1;  /* f
314ab 6f 72 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74  or null terminat
314ac 6f 72 20 2a 2f 0a 0a 20 20 72 20 3d 20 72 65 73  or */..  r = res
314ad 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ult = sqlite3_ma
314ae 6c 6c 6f 63 28 6c 65 6e 29 3b 0a 20 20 66 6f 72  lloc(len);.  for
314af 28 70 20 3d 20 7a 46 6f 72 6d 61 74 3b 20 2a 70  (p = zFormat; *p
314b0 3b 20 2b 2b 70 29 7b 0a 20 20 20 20 69 66 28 20  ; ++p){.    if( 
314b1 2a 70 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20  *p=='%' ){.     
314b2 20 6d 65 6d 63 70 79 28 72 2c 20 7a 44 62 2c 20   memcpy(r, zDb, 
314b3 6e 44 62 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d  nDb);.      r +=
314b4 20 6e 44 62 3b 0a 20 20 20 20 20 20 2a 72 2b 2b   nDb;.      *r++
314b5 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 6d 65   = '.';.      me
314b6 6d 63 70 79 28 72 2c 20 7a 4e 61 6d 65 2c 20 6e  mcpy(r, zName, n
314b7 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 20 2b  Name);.      r +
314b8 3d 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 65  = nName;.    } e
314b9 6c 73 65 20 7b 0a 20 20 20 20 20 20 2a 72 2b 2b  lse {.      *r++
314ba 20 3d 20 2a 70 3b 0a 20 20 20 20 7d 0a 20 20 7d   = *p;.    }.  }
314bb 0a 20 20 2a 72 2b 2b 20 3d 20 27 5c 30 27 3b 0a  .  *r++ = '\0';.
314bc 20 20 61 73 73 65 72 74 28 20 72 20 3d 3d 20 72    assert( r == r
314bd 65 73 75 6c 74 20 2b 20 6c 65 6e 20 29 3b 0a 20  esult + len );. 
314be 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 3b 0a   return result;.
314bf 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  }..static int sq
314c0 6c 5f 65 78 65 63 28 73 71 6c 69 74 65 33 20 2a  l_exec(sqlite3 *
314c1 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
314c2 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  zDb, const char 
314c3 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  *zName,.        
314c4 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
314c5 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29  t char *zFormat)
314c6 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61  {.  char *zComma
314c7 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d  nd = string_form
314c8 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c  at(zFormat, zDb,
314c9 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72   zName);.  int r
314ca 63 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  c;.  FTSTRACE(("
314cb 46 54 53 33 20 73 71 6c 3a 20 25 73 5c 6e 22 2c  FTS3 sql: %s\n",
314cc 20 7a 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20 20 72   zCommand));.  r
314cd 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
314ce 28 64 62 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 4e  (db, zCommand, N
314cf 55 4c 4c 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 20  ULL, 0, NULL);. 
314d0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
314d1 6f 6d 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72  ommand);.  retur
314d2 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
314d3 69 6e 74 20 73 71 6c 5f 70 72 65 70 61 72 65 28  int sql_prepare(
314d4 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
314d5 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 63 6f  st char *zDb, co
314d6 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
314d7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
314d8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
314d9 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 63  stmt **ppStmt, c
314da 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
314db 61 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f  at){.  char *zCo
314dc 6d 6d 61 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66  mmand = string_f
314dd 6f 72 6d 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a  ormat(zFormat, z
314de 44 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e  Db, zName);.  in
314df 74 20 72 63 3b 0a 20 20 46 54 53 54 52 41 43 45  t rc;.  FTSTRACE
314e0 28 28 22 46 54 53 33 20 70 72 65 70 61 72 65 3a  (("FTS3 prepare:
314e1 20 25 73 5c 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64   %s\n", zCommand
314e2 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ));.  rc = sqlit
314e3 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
314e4 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 2d 31 2c 20  , zCommand, -1, 
314e5 70 70 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20  ppStmt, NULL);. 
314e6 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
314e7 6f 6d 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72  ommand);.  retur
314e8 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 65 6e 64 20  n rc;.}../* end 
314e9 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e  utility function
314ea 73 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  s */../* Forward
314eb 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 74 79   reference */.ty
314ec 70 65 64 65 66 20 73 74 72 75 63 74 20 66 75 6c  pedef struct ful
314ed 6c 74 65 78 74 5f 76 74 61 62 20 66 75 6c 6c 74  ltext_vtab fullt
314ee 65 78 74 5f 76 74 61 62 3b 0a 0a 2f 2a 0a 2a 2a  ext_vtab;../*.**
314ef 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
314f0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
314f1 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
314f2 61 63 6b 20 6f 66 20 67 65 6e 65 72 61 74 65 64  ack of generated
314f3 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 2d 77 6f 72  .** matching-wor
314f4 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61  d offset informa
314f5 74 69 6f 6e 20 61 6e 64 20 73 6e 69 70 70 65 74  tion and snippet
314f6 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
314f7 72 75 63 74 20 53 6e 69 70 70 65 74 20 7b 0a 20  ruct Snippet {. 
314f8 20 69 6e 74 20 6e 4d 61 74 63 68 3b 20 20 20 20   int nMatch;    
314f9 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
314fa 20 6f 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20   of matches */. 
314fb 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   int nAlloc;    
314fc 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63 61   /* Space alloca
314fd 74 65 64 20 66 6f 72 20 61 4d 61 74 63 68 5b 5d  ted for aMatch[]
314fe 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 6e 69   */.  struct sni
314ff 70 70 65 74 4d 61 74 63 68 20 7b 20 2f 2a 20 4f  ppetMatch { /* O
31500 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ne entry for eac
31501 68 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20  h matching term 
31502 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 6e 53 74  */.    char snSt
31503 61 74 75 73 3b 20 20 20 20 20 20 20 2f 2a 20 53  atus;       /* S
31504 74 61 74 75 73 20 66 6c 61 67 20 66 6f 72 20 75  tatus flag for u
31505 73 65 20 77 68 69 6c 65 20 63 6f 6e 73 74 72 75  se while constru
31506 63 74 69 6e 67 20 73 6e 69 70 70 65 74 73 20 2a  cting snippets *
31507 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20  /.    short int 
31508 69 43 6f 6c 3b 20 20 20 20 20 20 2f 2a 20 54 68  iCol;      /* Th
31509 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 63 6f  e column that co
3150a 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 74 63 68  ntains the match
3150b 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e   */.    short in
3150c 74 20 69 54 65 72 6d 3b 20 20 20 20 20 2f 2a 20  t iTerm;     /* 
3150d 54 68 65 20 69 6e 64 65 78 20 69 6e 20 51 75 65  The index in Que
3150e 72 79 2e 70 54 65 72 6d 73 5b 5d 20 6f 66 20 74  ry.pTerms[] of t
3150f 68 65 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d  he matching term
31510 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 6f 6b   */.    int iTok
31511 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  en;          /* 
31512 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  The index of the
31513 20 6d 61 74 63 68 69 6e 67 20 64 6f 63 75 6d 65   matching docume
31514 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  nt token */.    
31515 73 68 6f 72 74 20 69 6e 74 20 6e 42 79 74 65 3b  short int nByte;
31516 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
31517 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 74  f bytes in the t
31518 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  erm */.    int i
31519 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20  Start;          
3151a 2f 2a 20 54 68 65 20 6f 66 66 73 65 74 20 74 6f  /* The offset to
3151b 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61   the first chara
3151c 63 74 65 72 20 6f 66 20 74 68 65 20 74 65 72 6d  cter of the term
3151d 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74 63 68 3b   */.  } *aMatch;
3151e 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 73 20        /* Points 
3151f 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  to space obtaine
31520 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 2a 2f  d from malloc */
31521 0a 20 20 63 68 61 72 20 2a 7a 4f 66 66 73 65 74  .  char *zOffset
31522 3b 20 20 2f 2a 20 54 65 78 74 20 72 65 6e 64 65  ;  /* Text rende
31523 72 69 6e 67 20 6f 66 20 61 4d 61 74 63 68 5b 5d  ring of aMatch[]
31524 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66 66 73 65   */.  int nOffse
31525 74 3b 20 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28  t;    /* strlen(
31526 7a 4f 66 66 73 65 74 29 20 2a 2f 0a 20 20 63 68  zOffset) */.  ch
31527 61 72 20 2a 7a 53 6e 69 70 70 65 74 3b 20 2f 2a  ar *zSnippet; /*
31528 20 53 6e 69 70 70 65 74 20 74 65 78 74 20 2a 2f   Snippet text */
31529 0a 20 20 69 6e 74 20 6e 53 6e 69 70 70 65 74 3b  .  int nSnippet;
3152a 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 53 6e     /* strlen(zSn
3152b 69 70 70 65 74 29 20 2a 2f 0a 7d 20 53 6e 69 70  ippet) */.} Snip
3152c 70 65 74 3b 0a 0a 0a 74 79 70 65 64 65 66 20 65  pet;...typedef e
3152d 6e 75 6d 20 51 75 65 72 79 54 79 70 65 20 7b 0a  num QueryType {.
3152e 20 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43 2c    QUERY_GENERIC,
3152f 20 20 20 2f 2a 20 74 61 62 6c 65 20 73 63 61 6e     /* table scan
31530 20 2a 2f 0a 20 20 51 55 45 52 59 5f 44 4f 43 49   */.  QUERY_DOCI
31531 44 2c 20 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75 70  D,     /* lookup
31532 20 62 79 20 64 6f 63 69 64 20 2a 2f 0a 20 20 51   by docid */.  Q
31533 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 20 20  UERY_FULLTEXT   
31534 2f 2a 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58  /* QUERY_FULLTEX
31535 54 20 2b 20 5b 69 5d 20 69 73 20 61 20 66 75 6c  T + [i] is a ful
31536 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20 66 6f  l-text search fo
31537 72 20 63 6f 6c 75 6d 6e 20 69 2a 2f 0a 7d 20 51  r column i*/.} Q
31538 75 65 72 79 54 79 70 65 3b 0a 0a 74 79 70 65 64  ueryType;..typed
31539 65 66 20 65 6e 75 6d 20 66 75 6c 6c 74 65 78 74  ef enum fulltext
3153a 5f 73 74 61 74 65 6d 65 6e 74 20 7b 0a 20 20 43  _statement {.  C
3153b 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54  ONTENT_INSERT_ST
3153c 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 53 45  MT,.  CONTENT_SE
3153d 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e  LECT_STMT,.  CON
3153e 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54  TENT_UPDATE_STMT
3153f 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45  ,.  CONTENT_DELE
31540 54 45 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45  TE_STMT,.  CONTE
31541 4e 54 5f 45 58 49 53 54 53 5f 53 54 4d 54 2c 0a  NT_EXISTS_STMT,.
31542 0a 20 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f  .  BLOCK_INSERT_
31543 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 53 45  STMT,.  BLOCK_SE
31544 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f  LECT_STMT,.  BLO
31545 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a  CK_DELETE_STMT,.
31546 20 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41    BLOCK_DELETE_A
31547 4c 4c 5f 53 54 4d 54 2c 0a 0a 20 20 53 45 47 44  LL_STMT,..  SEGD
31548 49 52 5f 4d 41 58 5f 49 4e 44 45 58 5f 53 54 4d  IR_MAX_INDEX_STM
31549 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 45 54 5f  T,.  SEGDIR_SET_
3154a 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53  STMT,.  SEGDIR_S
3154b 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 53 54 4d 54  ELECT_LEVEL_STMT
3154c 2c 0a 20 20 53 45 47 44 49 52 5f 53 50 41 4e 5f  ,.  SEGDIR_SPAN_
3154d 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 44  STMT,.  SEGDIR_D
3154e 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20 53 45  ELETE_STMT,.  SE
3154f 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d  GDIR_SELECT_SEGM
31550 45 4e 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44  ENT_STMT,.  SEGD
31551 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54  IR_SELECT_ALL_ST
31552 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 44 45 4c  MT,.  SEGDIR_DEL
31553 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 20 20  ETE_ALL_STMT,.  
31554 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f 53 54 4d  SEGDIR_COUNT_STM
31555 54 2c 0a 0a 20 20 4d 41 58 5f 53 54 4d 54 20 20  T,..  MAX_STMT  
31556 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31557 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 61 74 20     /* Always at 
31558 65 6e 64 21 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65  end! */.} fullte
31559 78 74 5f 73 74 61 74 65 6d 65 6e 74 3b 0a 0a 2f  xt_statement;../
3155a 2a 20 54 68 65 73 65 20 6d 75 73 74 20 65 78 61  * These must exa
3155b 63 74 6c 79 20 6d 61 74 63 68 20 74 68 65 20 65  ctly match the e
3155c 6e 75 6d 20 61 62 6f 76 65 2e 20 2a 2f 0a 2f 2a  num above. */./*
3155d 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 49 73   TODO(shess): Is
3155e 20 74 68 65 72 65 20 73 6f 6d 65 20 72 69 73 6b   there some risk
3155f 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e   that a statemen
31560 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69  t will be used i
31561 6e 20 74 77 6f 0a 2a 2a 20 63 75 72 73 6f 72 73  n two.** cursors
31562 20 61 74 20 6f 6e 63 65 2c 20 65 2e 67 2e 20 20   at once, e.g.  
31563 69 66 20 61 20 71 75 65 72 79 20 6a 6f 69 6e 73  if a query joins
31564 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
31565 20 74 6f 20 69 74 73 65 6c 66 3f 0a 2a 2a 20 49   to itself?.** I
31566 66 20 73 6f 20 70 65 72 68 61 70 73 20 77 65 20  f so perhaps we 
31567 73 68 6f 75 6c 64 20 6d 6f 76 65 20 73 6f 6d 65  should move some
31568 20 6f 66 20 74 68 65 73 65 20 74 6f 20 74 68 65   of these to the
31569 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a   cursor object..
3156a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
3156b 63 68 61 72 20 2a 63 6f 6e 73 74 20 66 75 6c 6c  char *const full
3156c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b  text_zStatement[
3156d 4d 41 58 5f 53 54 4d 54 5d 20 3d 20 7b 0a 20 20  MAX_STMT] = {.  
3156e 2f 2a 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  /* CONTENT_INSER
3156f 54 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67  T */ NULL,  /* g
31570 65 6e 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74  enerated in cont
31571 65 6e 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65  entInsertStateme
31572 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e  nt() */.  /* CON
31573 54 45 4e 54 5f 53 45 4c 45 43 54 20 2a 2f 20 4e  TENT_SELECT */ N
31574 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74  ULL,  /* generat
31575 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74 53 65 6c  ed in contentSel
31576 65 63 74 53 74 61 74 65 6d 65 6e 74 28 29 20 2a  ectStatement() *
31577 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 55  /.  /* CONTENT_U
31578 50 44 41 54 45 20 2a 2f 20 4e 55 4c 4c 2c 20 20  PDATE */ NULL,  
31579 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  /* generated in 
3157a 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61  contentUpdateSta
3157b 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a  tement() */.  /*
3157c 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 20   CONTENT_DELETE 
3157d 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20  */ "delete from 
3157e 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20  %_content where 
3157f 64 6f 63 69 64 20 3d 20 3f 22 2c 0a 20 20 2f 2a  docid = ?",.  /*
31580 20 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 20   CONTENT_EXISTS 
31581 2a 2f 20 22 73 65 6c 65 63 74 20 64 6f 63 69 64  */ "select docid
31582 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20   from %_content 
31583 6c 69 6d 69 74 20 31 22 2c 0a 0a 20 20 2f 2a 20  limit 1",..  /* 
31584 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 20 2a 2f 0a  BLOCK_INSERT */.
31585 20 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25    "insert into %
31586 5f 73 65 67 6d 65 6e 74 73 20 28 62 6c 6f 63 6b  _segments (block
31587 69 64 2c 20 62 6c 6f 63 6b 29 20 76 61 6c 75 65  id, block) value
31588 73 20 28 6e 75 6c 6c 2c 20 3f 29 22 2c 0a 20 20  s (null, ?)",.  
31589 2f 2a 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 20  /* BLOCK_SELECT 
3158a 2a 2f 20 22 73 65 6c 65 63 74 20 62 6c 6f 63 6b  */ "select block
3158b 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73   from %_segments
3158c 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20 3d   where blockid =
3158d 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f   ?",.  /* BLOCK_
3158e 44 45 4c 45 54 45 20 2a 2f 20 22 64 65 6c 65 74  DELETE */ "delet
3158f 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74  e from %_segment
31590 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20  s where blockid 
31591 62 65 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f 22  between ? and ?"
31592 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45 4c  ,.  /* BLOCK_DEL
31593 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c 65  ETE_ALL */ "dele
31594 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e  te from %_segmen
31595 74 73 22 2c 0a 0a 20 20 2f 2a 20 53 45 47 44 49  ts",..  /* SEGDI
31596 52 5f 4d 41 58 5f 49 4e 44 45 58 20 2a 2f 20 22  R_MAX_INDEX */ "
31597 73 65 6c 65 63 74 20 6d 61 78 28 69 64 78 29 20  select max(idx) 
31598 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 77 68  from %_segdir wh
31599 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a  ere level = ?",.
3159a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 54 20    /* SEGDIR_SET 
3159b 2a 2f 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20  */ "insert into 
3159c 25 5f 73 65 67 64 69 72 20 76 61 6c 75 65 73 20  %_segdir values 
3159d 28 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20  (?, ?, ?, ?, ?, 
3159e 3f 29 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52  ?)",.  /* SEGDIR
3159f 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 20 2a 2f  _SELECT_LEVEL */
315a0 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61 72 74  .  "select start
315a1 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65  _block, leaves_e
315a2 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66  nd_block, root f
315a3 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22 0a 20  rom %_segdir ". 
315a4 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d   " where level =
315a5 20 3f 20 6f 72 64 65 72 20 62 79 20 69 64 78 22   ? order by idx"
315a6 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 50  ,.  /* SEGDIR_SP
315a7 41 4e 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20  AN */.  "select 
315a8 6d 69 6e 28 73 74 61 72 74 5f 62 6c 6f 63 6b 29  min(start_block)
315a9 2c 20 6d 61 78 28 65 6e 64 5f 62 6c 6f 63 6b 29  , max(end_block)
315aa 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22   from %_segdir "
315ab 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c  .  " where level
315ac 20 3d 20 3f 20 61 6e 64 20 73 74 61 72 74 5f 62   = ? and start_b
315ad 6c 6f 63 6b 20 3c 3e 20 30 22 2c 0a 20 20 2f 2a  lock <> 0",.  /*
315ae 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 20 2a   SEGDIR_DELETE *
315af 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 25  / "delete from %
315b0 5f 73 65 67 64 69 72 20 77 68 65 72 65 20 6c 65  _segdir where le
315b1 76 65 6c 20 3d 20 3f 22 2c 0a 0a 20 20 2f 2a 20  vel = ?",..  /* 
315b2 4e 4f 54 45 28 73 68 65 73 73 29 3a 20 54 68 65  NOTE(shess): The
315b3 20 66 69 72 73 74 20 74 68 72 65 65 20 72 65 73   first three res
315b4 75 6c 74 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ults of the foll
315b5 6f 77 69 6e 67 20 74 77 6f 0a 20 20 2a 2a 20 73  owing two.  ** s
315b6 74 61 74 65 6d 65 6e 74 73 20 6d 75 73 74 20 6d  tatements must m
315b7 61 74 63 68 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20  atch..  */.  /* 
315b8 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45  SEGDIR_SELECT_SE
315b9 47 4d 45 4e 54 20 2a 2f 0a 20 20 22 73 65 6c 65  GMENT */.  "sele
315ba 63 74 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20  ct start_block, 
315bb 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
315bc 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25 5f 73 65  , root from %_se
315bd 67 64 69 72 20 22 0a 20 20 22 20 77 68 65 72 65  gdir ".  " where
315be 20 6c 65 76 65 6c 20 3d 20 3f 20 61 6e 64 20 69   level = ? and i
315bf 64 78 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 53 45  dx = ?",.  /* SE
315c0 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 20  GDIR_SELECT_ALL 
315c1 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61  */.  "select sta
315c2 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73  rt_block, leaves
315c3 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74  _end_block, root
315c4 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22   from %_segdir "
315c5 0a 20 20 22 20 6f 72 64 65 72 20 62 79 20 6c 65  .  " order by le
315c6 76 65 6c 20 64 65 73 63 2c 20 69 64 78 20 61 73  vel desc, idx as
315c7 63 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  c",.  /* SEGDIR_
315c8 44 45 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64  DELETE_ALL */ "d
315c9 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67  elete from %_seg
315ca 64 69 72 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49  dir",.  /* SEGDI
315cb 52 5f 43 4f 55 4e 54 20 2a 2f 20 22 73 65 6c 65  R_COUNT */ "sele
315cc 63 74 20 63 6f 75 6e 74 28 2a 29 2c 20 69 66 6e  ct count(*), ifn
315cd 75 6c 6c 28 6d 61 78 28 6c 65 76 65 6c 29 2c 30  ull(max(level),0
315ce 29 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 22  ) from %_segdir"
315cf 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f  ,.};../*.** A co
315d0 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 66 75  nnection to a fu
315d1 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 69 73 20  lltext index is 
315d2 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
315d3 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
315d4 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
315d5 78 43 72 65 61 74 65 20 61 6e 64 20 78 43 6f 6e  xCreate and xCon
315d6 6e 65 63 74 20 6d 65 74 68 6f 64 73 20 63 72 65  nect methods cre
315d7 61 74 65 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a  ate an instance.
315d8 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ** of this struc
315d9 74 75 72 65 20 61 6e 64 20 78 44 65 73 74 72 6f  ture and xDestro
315da 79 20 61 6e 64 20 78 44 69 73 63 6f 6e 6e 65 63  y and xDisconnec
315db 74 20 66 72 65 65 20 74 68 61 74 20 69 6e 73 74  t free that inst
315dc 61 6e 63 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68  ance..** All oth
315dd 65 72 20 6d 65 74 68 6f 64 73 20 72 65 63 65 69  er methods recei
315de 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ve a pointer to 
315df 74 68 65 20 73 74 72 75 63 74 75 72 65 20 61 73  the structure as
315e0 20 6f 6e 65 20 6f 66 20 74 68 65 69 72 0a 2a 2a   one of their.**
315e1 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 73   arguments..*/.s
315e2 74 72 75 63 74 20 66 75 6c 6c 74 65 78 74 5f 76  truct fulltext_v
315e3 74 61 62 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  tab {.  sqlite3_
315e4 76 74 61 62 20 62 61 73 65 3b 20 20 20 20 20 20  vtab base;      
315e5 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
315e6 20 63 6c 61 73 73 20 75 73 65 64 20 62 79 20 53   class used by S
315e7 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20  QLite core */.  
315e8 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
315e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
315ea 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
315eb 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
315ec 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
315ed 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
315ee 20 20 2f 2a 20 6c 6f 67 69 63 61 6c 20 64 61 74    /* logical dat
315ef 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  abase name */.  
315f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
315f1 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
315f2 20 2f 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c   /* virtual tabl
315f3 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  e name */.  int 
315f4 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  nColumn;        
315f5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
315f6 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
315f7 73 20 69 6e 20 76 69 72 74 75 61 6c 20 74 61 62  s in virtual tab
315f8 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
315f9 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  zColumn;        
315fa 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75           /* colu
315fb 6d 6e 20 6e 61 6d 65 73 2e 20 20 6d 61 6c 6c 6f  mn names.  mallo
315fc 63 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ced */.  char **
315fd 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b  azContentColumn;
315fe 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c            /* col
315ff 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 63 6f 6e  umn names in con
31600 74 65 6e 74 20 74 61 62 6c 65 3b 20 6d 61 6c 6c  tent table; mall
31601 6f 63 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  oced */.  sqlite
31602 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
31603 6b 65 6e 69 7a 65 72 3b 20 20 20 2f 2a 20 74 6f  kenizer;   /* to
31604 6b 65 6e 69 7a 65 72 20 66 6f 72 20 69 6e 73 65  kenizer for inse
31605 72 74 73 20 61 6e 64 20 71 75 65 72 69 65 73 20  rts and queries 
31606 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 63 6f 6d 70  */..  /* Precomp
31607 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  iled statements 
31608 77 68 69 63 68 20 77 65 20 6b 65 65 70 20 61 73  which we keep as
31609 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 61 62   long as the tab
3160a 6c 65 20 69 73 0a 20 20 2a 2a 20 6f 70 65 6e 2e  le is.  ** open.
3160b 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
3160c 73 74 6d 74 20 2a 70 46 75 6c 6c 74 65 78 74 53  stmt *pFulltextS
3160d 74 61 74 65 6d 65 6e 74 73 5b 4d 41 58 5f 53 54  tatements[MAX_ST
3160e 4d 54 5d 3b 0a 0a 20 20 2f 2a 20 50 72 65 63 6f  MT];..  /* Preco
3160f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74  mpiled statement
31610 73 20 75 73 65 64 20 66 6f 72 20 73 65 67 6d 65  s used for segme
31611 6e 74 20 6d 65 72 67 65 73 2e 20 20 57 65 20 72  nt merges.  We r
31612 75 6e 20 61 0a 20 20 2a 2a 20 73 65 70 61 72 61  un a.  ** separa
31613 74 65 20 73 65 6c 65 63 74 20 61 63 72 6f 73 73  te select across
31614 20 74 68 65 20 6c 65 61 66 20 6c 65 76 65 6c 20   the leaf level 
31615 6f 66 20 65 61 63 68 20 74 72 65 65 20 62 65 69  of each tree bei
31616 6e 67 20 6d 65 72 67 65 64 2e 0a 20 20 2a 2f 0a  ng merged..  */.
31617 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
31618 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
31619 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20  [MERGE_COUNT];. 
3161a 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e   /* The statemen
3161b 74 20 75 73 65 64 20 74 6f 20 70 72 65 70 61 72  t used to prepar
3161c 65 20 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  e pLeafSelectStm
3161d 74 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4c  ts. */.#define L
3161e 45 41 46 5f 53 45 4c 45 43 54 20 5c 0a 20 20 22  EAF_SELECT \.  "
3161f 73 65 6c 65 63 74 20 62 6c 6f 63 6b 20 66 72 6f  select block fro
31620 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65  m %_segments whe
31621 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65  re blockid betwe
31622 65 6e 20 3f 20 61 6e 64 20 3f 20 6f 72 64 65 72  en ? and ? order
31623 20 62 79 20 62 6c 6f 63 6b 69 64 22 0a 0a 20 20   by blockid"..  
31624 2f 2a 20 54 68 65 73 65 20 62 75 66 66 65 72 20  /* These buffer 
31625 70 65 6e 64 69 6e 67 20 69 6e 64 65 78 20 75 70  pending index up
31626 64 61 74 65 73 20 64 75 72 69 6e 67 20 74 72 61  dates during tra
31627 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20  nsactions..  ** 
31628 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 65 73 74  nPendingData est
31629 69 6d 61 74 65 73 20 74 68 65 20 6d 65 6d 6f 72  imates the memor
3162a 79 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 65  y size of the pe
3162b 6e 64 69 6e 67 20 64 61 74 61 2e 20 20 49 74 0a  nding data.  It.
3162c 20 20 2a 2a 20 64 6f 65 73 6e 27 74 20 69 6e 63    ** doesn't inc
3162d 6c 75 64 65 20 74 68 65 20 68 61 73 68 2d 62 75  lude the hash-bu
3162e 63 6b 65 74 20 6f 76 65 72 68 65 61 64 2c 20 6e  cket overhead, n
3162f 6f 72 20 61 6e 79 20 6d 61 6c 6c 6f 63 0a 20 20  or any malloc.  
31630 2a 2a 20 6f 76 65 72 68 65 61 64 2e 20 20 57 68  ** overhead.  Wh
31631 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 20  en nPendingData 
31632 65 78 63 65 65 64 73 20 6b 50 65 6e 64 69 6e 67  exceeds kPending
31633 54 68 72 65 73 68 6f 6c 64 2c 20 74 68 65 0a 20  Threshold, the. 
31634 20 2a 2a 20 62 75 66 66 65 72 20 69 73 20 66 6c   ** buffer is fl
31635 75 73 68 65 64 20 65 76 65 6e 20 62 65 66 6f 72  ushed even befor
31636 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
31637 6e 20 63 6c 6f 73 65 73 2e 0a 20 20 2a 2a 20 70  n closes..  ** p
31638 65 6e 64 69 6e 67 54 65 72 6d 73 20 73 74 6f 72  endingTerms stor
31639 65 73 20 74 68 65 20 64 61 74 61 2c 20 61 6e 64  es the data, and
3163a 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 77   is only valid w
3163b 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61  hen nPendingData
3163c 0a 20 20 2a 2a 20 69 73 20 3e 3d 30 20 28 6e 50  .  ** is >=0 (nP
3163d 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 6d 65 61  endingData<0 mea
3163e 6e 73 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  ns pendingTerms 
3163f 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 20 20 2a  has not been.  *
31640 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 29 2e 20  * initialized). 
31641 20 69 50 72 65 76 44 6f 63 69 64 20 69 73 20 74   iPrevDocid is t
31642 68 65 20 6c 61 73 74 20 64 6f 63 69 64 20 77 72  he last docid wr
31643 69 74 74 65 6e 2c 20 75 73 65 64 20 74 6f 20 6d  itten, used to m
31644 61 6b 65 0a 20 20 2a 2a 20 63 65 72 74 61 69 6e  ake.  ** certain
31645 20 77 65 27 72 65 20 69 6e 73 65 72 74 69 6e 67   we're inserting
31646 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
31647 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 65  ..  */.  int nPe
31648 6e 64 69 6e 67 44 61 74 61 3b 0a 23 64 65 66 69  ndingData;.#defi
31649 6e 65 20 6b 50 65 6e 64 69 6e 67 54 68 72 65 73  ne kPendingThres
3164a 68 6f 6c 64 20 28 31 2a 31 30 32 34 2a 31 30 32  hold (1*1024*102
3164b 34 29 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  4).  sqlite_int6
3164c 34 20 69 50 72 65 76 44 6f 63 69 64 3b 0a 20 20  4 iPrevDocid;.  
3164d 66 74 73 33 48 61 73 68 20 70 65 6e 64 69 6e 67  fts3Hash pending
3164e 54 65 72 6d 73 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  Terms;.};../*.**
3164f 20 57 68 65 6e 20 74 68 65 20 63 6f 72 65 20 77   When the core w
31650 61 6e 74 73 20 74 6f 20 64 6f 20 61 20 71 75 65  ants to do a que
31651 72 79 2c 20 69 74 20 63 72 65 61 74 65 20 61 20  ry, it create a 
31652 63 75 72 73 6f 72 20 75 73 69 6e 67 20 61 0a 2a  cursor using a.*
31653 2a 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e 2e  * call to xOpen.
31654 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
31655 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
31656 6f 66 20 61 20 63 75 72 73 6f 72 2e 20 20 49 74  of a cursor.  It
31657 0a 2a 2a 20 69 73 20 64 65 73 74 72 6f 79 65 64  .** is destroyed
31658 20 62 79 20 78 43 6c 6f 73 65 2e 0a 2a 2f 0a 74   by xClose..*/.t
31659 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66 75  ypedef struct fu
3165a 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 7b 0a  lltext_cursor {.
3165b 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63    sqlite3_vtab_c
3165c 75 72 73 6f 72 20 62 61 73 65 3b 20 20 20 20 20  ursor base;     
3165d 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73     /* Base class
3165e 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
3165f 63 6f 72 65 20 2a 2f 0a 20 20 51 75 65 72 79 54  core */.  QueryT
31660 79 70 65 20 69 43 75 72 73 6f 72 54 79 70 65 3b  ype iCursorType;
31661 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
31662 70 79 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e  py of sqlite3_in
31663 64 65 78 5f 69 6e 66 6f 2e 69 64 78 4e 75 6d 20  dex_info.idxNum 
31664 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
31665 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 20 20  t *pStmt;       
31666 20 20 20 20 20 20 2f 2a 20 50 72 65 70 61 72 65        /* Prepare
31667 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 75  d statement in u
31668 73 65 20 62 79 20 74 68 65 20 63 75 72 73 6f 72  se by the cursor
31669 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20   */.  int eof;  
3166a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3166b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
3166c 66 20 61 74 20 45 6e 64 20 4f 66 20 52 65 73 75  f at End Of Resu
3166d 6c 74 73 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  lts */.  Fts3Exp
3166e 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
3166f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
31670 73 65 64 20 4d 41 54 43 48 20 71 75 65 72 79 20  sed MATCH query 
31671 73 74 72 69 6e 67 20 2a 2f 0a 20 20 53 6e 69 70  string */.  Snip
31672 70 65 74 20 73 6e 69 70 70 65 74 3b 20 20 20 20  pet snippet;    
31673 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31674 43 61 63 68 65 64 20 73 6e 69 70 70 65 74 20 66  Cached snippet f
31675 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
31676 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  ow */.  int iCol
31677 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
31678 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
31679 6d 6e 20 62 65 69 6e 67 20 73 65 61 72 63 68 65  mn being searche
3167a 64 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65  d */.  DataBuffe
3167b 72 20 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20  r result;       
3167c 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69          /* Docli
3167d 73 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20  st results from 
3167e 66 75 6c 6c 74 65 78 74 51 75 65 72 79 20 2a 2f  fulltextQuery */
3167f 0a 20 20 44 4c 52 65 61 64 65 72 20 72 65 61 64  .  DLReader read
31680 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
31681 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65      /* Result re
31682 61 64 65 72 20 69 66 20 72 65 73 75 6c 74 20 6e  ader if result n
31683 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 7d 20 66 75  ot empty */.} fu
31684 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 3b 0a 0a  lltext_cursor;..
31685 73 74 61 74 69 63 20 66 75 6c 6c 74 65 78 74 5f  static fulltext_
31686 76 74 61 62 20 2a 63 75 72 73 6f 72 5f 76 74 61  vtab *cursor_vta
31687 62 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f  b(fulltext_curso
31688 72 20 2a 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  r *c){.  return 
31689 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
3168a 29 20 63 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b  ) c->base.pVtab;
3168b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .}..static const
3168c 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
3168d 66 74 73 33 4d 6f 64 75 6c 65 3b 20 20 20 2f 2a  fts3Module;   /*
3168e 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   forward declara
3168f 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 20 52 65 74 75  tion */../* Retu
31690 72 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  rn a dynamically
31691 20 67 65 6e 65 72 61 74 65 64 20 73 74 61 74 65   generated state
31692 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d  ment of the form
31693 0a 20 2a 20 20 20 69 6e 73 65 72 74 20 69 6e 74  . *   insert int
31694 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63  o %_content (doc
31695 69 64 2c 20 2e 2e 2e 29 20 76 61 6c 75 65 73 20  id, ...) values 
31696 28 3f 2c 20 2e 2e 2e 29 0a 20 2a 2f 0a 73 74 61  (?, ...). */.sta
31697 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
31698 63 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61  contentInsertSta
31699 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
3169a 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69  vtab *v){.  Stri
3169b 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
3169c 6e 74 20 69 3b 0a 0a 20 20 69 6e 69 74 53 74 72  nt i;..  initStr
3169d 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a  ingBuffer(&sb);.
3169e 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 69    append(&sb, "i
3169f 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e  nsert into %_con
316a0 74 65 6e 74 20 28 64 6f 63 69 64 2c 20 22 29 3b  tent (docid, ");
316a1 0a 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73  .  appendList(&s
316a2 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76  b, v->nColumn, v
316a3 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
316a4 6e 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62  n);.  append(&sb
316a5 2c 20 22 29 20 76 61 6c 75 65 73 20 28 3f 22 29  , ") values (?")
316a6 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76  ;.  for(i=0; i<v
316a7 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 0a  ->nColumn; ++i).
316a8 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20      append(&sb, 
316a9 22 2c 20 3f 22 29 3b 0a 20 20 61 70 70 65 6e 64  ", ?");.  append
316aa 28 26 73 62 2c 20 22 29 22 29 3b 0a 20 20 72 65  (&sb, ")");.  re
316ab 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65  turn stringBuffe
316ac 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f  rData(&sb);.}../
316ad 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d  * Return a dynam
316ae 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
316af 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68   statement of th
316b0 65 20 66 6f 72 6d 0a 20 2a 20 20 20 73 65 6c 65  e form. *   sele
316b1 63 74 20 3c 63 6f 6e 74 65 6e 74 20 63 6f 6c 75  ct <content colu
316b2 6d 6e 73 3e 20 66 72 6f 6d 20 25 5f 63 6f 6e 74  mns> from %_cont
316b3 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64 20  ent where docid 
316b4 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63  = ?. */.static c
316b5 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65  onst char *conte
316b6 6e 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e  ntSelectStatemen
316b7 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
316b8 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66  *v){.  StringBuf
316b9 66 65 72 20 73 62 3b 0a 20 20 69 6e 69 74 53 74  fer sb;.  initSt
316ba 72 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b  ringBuffer(&sb);
316bb 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22  .  append(&sb, "
316bc 53 45 4c 45 43 54 20 22 29 3b 0a 20 20 61 70 70  SELECT ");.  app
316bd 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d 3e  endList(&sb, v->
316be 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f  nColumn, v->azCo
316bf 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ntentColumn);.  
316c0 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 46 52  append(&sb, " FR
316c1 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 20 57 48 45  OM %_content WHE
316c2 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a  RE docid = ?");.
316c3 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42    return stringB
316c4 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a  ufferData(&sb);.
316c5 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 64  }../* Return a d
316c6 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65 72  ynamically gener
316c7 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6f  ated statement o
316c8 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20 20  f the form. *   
316c9 75 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74  update %_content
316ca 20 73 65 74 20 5b 63 6f 6c 5f 30 5d 20 3d 20 3f   set [col_0] = ?
316cb 2c 20 5b 63 6f 6c 5f 31 5d 20 3d 20 3f 2c 20 2e  , [col_1] = ?, .
316cc 2e 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  ... *           
316cd 20 20 20 20 20 20 20 20 20 77 68 65 72 65 20 64           where d
316ce 6f 63 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74 61  ocid = ?. */.sta
316cf 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
316d0 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61  contentUpdateSta
316d1 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  tement(fulltext_
316d2 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69  vtab *v){.  Stri
316d3 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
316d4 6e 74 20 69 3b 0a 0a 20 20 69 6e 69 74 53 74 72  nt i;..  initStr
316d5 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a  ingBuffer(&sb);.
316d6 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 75    append(&sb, "u
316d7 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20  pdate %_content 
316d8 73 65 74 20 22 29 3b 0a 20 20 66 6f 72 28 69 3d  set ");.  for(i=
316d9 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b  0; i<v->nColumn;
316da 20 2b 2b 69 29 20 7b 0a 20 20 20 20 69 66 28 20   ++i) {.    if( 
316db 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 61 70 70  i>0 ){.      app
316dc 65 6e 64 28 26 73 62 2c 20 22 2c 20 22 29 3b 0a  end(&sb, ", ");.
316dd 20 20 20 20 7d 0a 20 20 20 20 61 70 70 65 6e 64      }.    append
316de 28 26 73 62 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65  (&sb, v->azConte
316df 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20  ntColumn[i]);.  
316e0 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20    append(&sb, " 
316e1 3d 20 3f 22 29 3b 0a 20 20 7d 0a 20 20 61 70 70  = ?");.  }.  app
316e2 65 6e 64 28 26 73 62 2c 20 22 20 77 68 65 72 65  end(&sb, " where
316e3 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a 20 20   docid = ?");.  
316e4 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66  return stringBuf
316e5 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a  ferData(&sb);.}.
316e6 0a 2f 2a 20 50 75 74 73 20 61 20 66 72 65 73 68  ./* Puts a fresh
316e7 6c 79 2d 70 72 65 70 61 72 65 64 20 73 74 61 74  ly-prepared stat
316e8 65 6d 65 6e 74 20 64 65 74 65 72 6d 69 6e 65 64  ement determined
316e9 20 62 79 20 69 53 74 6d 74 20 69 6e 20 2a 70 70   by iStmt in *pp
316ea 53 74 6d 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  Stmt..** If the 
316eb 69 6e 64 69 63 61 74 65 64 20 73 74 61 74 65 6d  indicated statem
316ec 65 6e 74 20 68 61 73 20 6e 65 76 65 72 20 62 65  ent has never be
316ed 65 6e 20 70 72 65 70 61 72 65 64 2c 20 69 74 20  en prepared, it 
316ee 69 73 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 61  is prepared.** a
316ef 6e 64 20 63 61 63 68 65 64 2c 20 6f 74 68 65 72  nd cached, other
316f0 77 69 73 65 20 74 68 65 20 63 61 63 68 65 64 20  wise the cached 
316f1 76 65 72 73 69 6f 6e 20 69 73 20 72 65 73 65 74  version is reset
316f2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
316f3 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
316f4 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
316f5 2a 76 2c 20 66 75 6c 6c 74 65 78 74 5f 73 74 61  *v, fulltext_sta
316f6 74 65 6d 65 6e 74 20 69 53 74 6d 74 2c 0a 20 20  tement iStmt,.  
316f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316f8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
316f9 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
316fa 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74  ){.  assert( iSt
316fb 6d 74 3c 4d 41 58 5f 53 54 4d 54 20 29 3b 0a 20  mt<MAX_STMT );. 
316fc 20 69 66 28 20 76 2d 3e 70 46 75 6c 6c 74 65 78   if( v->pFulltex
316fd 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d  tStatements[iStm
316fe 74 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  t]==NULL ){.    
316ff 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6d  const char *zStm
31700 74 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  t;.    int rc;. 
31701 20 20 20 73 77 69 74 63 68 28 20 69 53 74 6d 74     switch( iStmt
31702 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 43   ){.      case C
31703 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54  ONTENT_INSERT_ST
31704 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d  MT:.        zStm
31705 74 20 3d 20 63 6f 6e 74 65 6e 74 49 6e 73 65 72  t = contentInser
31706 74 53 74 61 74 65 6d 65 6e 74 28 76 29 3b 20 62  tStatement(v); b
31707 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31708 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f   CONTENT_SELECT_
31709 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53  STMT:.        zS
3170a 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 53 65 6c  tmt = contentSel
3170b 65 63 74 53 74 61 74 65 6d 65 6e 74 28 76 29 3b  ectStatement(v);
3170c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3170d 73 65 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54  se CONTENT_UPDAT
3170e 45 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20  E_STMT:.        
3170f 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 55  zStmt = contentU
31710 70 64 61 74 65 53 74 61 74 65 6d 65 6e 74 28 76  pdateStatement(v
31711 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
31712 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20  default:.       
31713 20 7a 53 74 6d 74 20 3d 20 66 75 6c 6c 74 65 78   zStmt = fulltex
31714 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 69 53 74  t_zStatement[iSt
31715 6d 74 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  mt];.    }.    r
31716 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65 28  c = sql_prepare(
31717 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76  v->db, v->zDb, v
31718 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70 46 75  ->zName, &v->pFu
31719 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
3171a 5b 69 53 74 6d 74 5d 2c 0a 20 20 20 20 20 20 20  [iStmt],.       
3171b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3171c 20 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 69 66    zStmt);.    if
3171d 28 20 7a 53 74 6d 74 20 21 3d 20 66 75 6c 6c 74  ( zStmt != fullt
3171e 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 69  ext_zStatement[i
3171f 53 74 6d 74 5d 29 20 73 71 6c 69 74 65 33 5f 66  Stmt]) sqlite3_f
31720 72 65 65 28 28 76 6f 69 64 20 2a 29 20 7a 53 74  ree((void *) zSt
31721 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  mt);.    if( rc!
31722 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
31723 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65  urn rc;.  } else
31724 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20   {.    int rc = 
31725 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 76 2d  sqlite3_reset(v-
31726 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
31727 65 6e 74 73 5b 69 53 74 6d 74 5d 29 3b 0a 20 20  ents[iStmt]);.  
31728 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
31729 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
3172a 0a 20 20 7d 0a 0a 20 20 2a 70 70 53 74 6d 74 20  .  }..  *ppStmt 
3172b 3d 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74  = v->pFulltextSt
3172c 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 3b  atements[iStmt];
3172d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3172e 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20  _OK;.}../* Like 
3172f 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2c 20  sqlite3_step(), 
31730 62 75 74 20 63 6f 6e 76 65 72 74 20 53 51 4c 49  but convert SQLI
31731 54 45 5f 44 4f 4e 45 20 74 6f 20 53 51 4c 49 54  TE_DONE to SQLIT
31732 45 5f 4f 4b 20 61 6e 64 0a 2a 2a 20 53 51 4c 49  E_OK and.** SQLI
31733 54 45 5f 52 4f 57 20 74 6f 20 53 51 4c 49 54 45  TE_ROW to SQLITE
31734 5f 45 52 52 4f 52 2e 20 20 55 73 65 66 75 6c 20  _ERROR.  Useful 
31735 66 6f 72 20 73 74 61 74 65 6d 65 6e 74 73 20 6c  for statements l
31736 69 6b 65 20 55 50 44 41 54 45 2c 0a 2a 2a 20 77  ike UPDATE,.** w
31737 68 65 72 65 20 77 65 20 65 78 70 65 63 74 20 6e  here we expect n
31738 6f 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74  o results..*/.st
31739 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 73 69 6e  atic int sql_sin
3173a 67 6c 65 5f 73 74 65 70 28 73 71 6c 69 74 65 33  gle_step(sqlite3
3173b 5f 73 74 6d 74 20 2a 73 29 7b 0a 20 20 69 6e 74  _stmt *s){.  int
3173c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
3173d 65 70 28 73 29 3b 0a 20 20 72 65 74 75 72 6e 20  ep(s);.  return 
3173e 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  (rc==SQLITE_DONE
3173f 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  ) ? SQLITE_OK : 
31740 72 63 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 73  rc;.}../* Like s
31741 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
31742 28 29 2c 20 62 75 74 20 66 6f 72 20 73 70 65 63  (), but for spec
31743 69 61 6c 20 72 65 70 6c 69 63 61 74 65 64 20 4c  ial replicated L
31744 45 41 46 5f 53 45 4c 45 43 54 0a 2a 2a 20 73 74  EAF_SELECT.** st
31745 61 74 65 6d 65 6e 74 73 2e 20 20 69 64 78 20 2d  atements.  idx -
31746 31 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 63  1 is a special c
31747 61 73 65 20 66 6f 72 20 61 6e 20 75 6e 63 61 63  ase for an uncac
31748 68 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 0a 2a  hed version of.*
31749 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
3174a 28 75 73 65 64 20 69 6e 20 74 68 65 20 6f 70 74  (used in the opt
3174b 69 6d 69 7a 65 20 69 6d 70 6c 65 6d 65 6e 74 61  imize implementa
3174c 74 69 6f 6e 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  tion)..*/./* TOD
3174d 4f 28 73 68 65 73 73 29 20 57 72 69 74 65 20 76  O(shess) Write v
3174e 65 72 73 69 6f 6e 20 66 6f 72 20 67 65 6e 65 72  ersion for gener
3174f 69 63 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e  ic statements an
31750 64 20 74 68 65 6e 20 73 68 61 72 65 0a 2a 2a 20  d then share.** 
31751 74 68 61 74 20 62 65 74 77 65 65 6e 20 74 68 65  that between the
31752 20 63 61 63 68 65 64 2d 73 74 61 74 65 6d 65 6e   cached-statemen
31753 74 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  t functions..*/.
31754 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 67  static int sql_g
31755 65 74 5f 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e  et_leaf_statemen
31756 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
31757 2a 76 2c 20 69 6e 74 20 69 64 78 2c 0a 20 20 20  *v, int idx,.   
31758 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31759 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3175a 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
3175b 53 74 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Stmt){.  assert(
3175c 20 69 64 78 3e 3d 2d 31 20 26 26 20 69 64 78 3c   idx>=-1 && idx<
3175d 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 20  MERGE_COUNT );. 
3175e 20 69 66 28 20 69 64 78 3d 3d 2d 31 20 29 7b 0a   if( idx==-1 ){.
3175f 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 70      return sql_p
31760 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d  repare(v->db, v-
31761 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20  >zDb, v->zName, 
31762 70 70 53 74 6d 74 2c 20 4c 45 41 46 5f 53 45 4c  ppStmt, LEAF_SEL
31763 45 43 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ECT);.  }else if
31764 28 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74  ( v->pLeafSelect
31765 53 74 6d 74 73 5b 69 64 78 5d 3d 3d 4e 55 4c 4c  Stmts[idx]==NULL
31766 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d   ){.    int rc =
31767 20 73 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e   sql_prepare(v->
31768 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a  db, v->zDb, v->z
31769 4e 61 6d 65 2c 20 26 76 2d 3e 70 4c 65 61 66 53  Name, &v->pLeafS
3176a 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78 5d 2c  electStmts[idx],
3176b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3176c 20 20 20 20 20 20 20 20 20 20 4c 45 41 46 5f 53            LEAF_S
3176d 45 4c 45 43 54 29 3b 0a 20 20 20 20 69 66 28 20  ELECT);.    if( 
3176e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3176f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c  return rc;.  }el
31770 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  se{.    int rc =
31771 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 76   sqlite3_reset(v
31772 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  ->pLeafSelectStm
31773 74 73 5b 69 64 78 5d 29 3b 0a 20 20 20 20 69 66  ts[idx]);.    if
31774 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
31775 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
31776 0a 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 76 2d  ..  *ppStmt = v-
31777 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74  >pLeafSelectStmt
31778 73 5b 69 64 78 5d 3b 0a 20 20 72 65 74 75 72 6e  s[idx];.  return
31779 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
3177a 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f  * insert into %_
3177b 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c 20  content (docid, 
3177c 2e 2e 2e 29 20 76 61 6c 75 65 73 20 28 5b 64 6f  ...) values ([do
3177d 63 69 64 5d 2c 20 5b 70 56 61 6c 75 65 73 5d 29  cid], [pValues])
3177e 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 63 69 64  .** If the docid
3177f 20 63 6f 6e 74 61 69 6e 73 20 53 51 4c 20 4e 55   contains SQL NU
31780 4c 4c 2c 20 74 68 65 6e 20 61 20 75 6e 69 71 75  LL, then a uniqu
31781 65 20 64 6f 63 69 64 20 77 69 6c 6c 20 62 65 0a  e docid will be.
31782 2a 2a 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f  ** generated..*/
31783 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74  .static int cont
31784 65 6e 74 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74  ent_insert(fullt
31785 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
31786 69 74 65 33 5f 76 61 6c 75 65 20 2a 64 6f 63 69  ite3_value *doci
31787 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
31788 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
31789 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
3178a 6c 75 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  lues){.  sqlite3
3178b 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
3178c 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71  i;.  int rc = sq
3178d 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28  l_get_statement(
3178e 76 2c 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  v, CONTENT_INSER
3178f 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  T_STMT, &s);.  i
31790 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
31791 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
31792 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
31793 6e 64 5f 76 61 6c 75 65 28 73 2c 20 31 2c 20 64  nd_value(s, 1, d
31794 6f 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ocid);.  if( rc!
31795 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
31796 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69  urn rc;..  for(i
31797 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e  =0; i<v->nColumn
31798 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 72 63 20 3d  ; ++i){.    rc =
31799 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
3179a 6c 75 65 28 73 2c 20 32 2b 69 2c 20 70 56 61 6c  lue(s, 2+i, pVal
3179b 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  ues[i]);.    if(
3179c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3179d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
3179e 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
3179f 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
317a0 0a 2f 2a 20 75 70 64 61 74 65 20 25 5f 63 6f 6e  ./* update %_con
317a1 74 65 6e 74 20 73 65 74 20 63 6f 6c 30 20 3d 20  tent set col0 = 
317a2 70 56 61 6c 75 65 73 5b 30 5d 2c 20 63 6f 6c 31  pValues[0], col1
317a3 20 3d 20 70 56 61 6c 75 65 73 5b 31 5d 2c 20 2e   = pValues[1], .
317a4 2e 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  ... *           
317a5 20 20 20 20 20 20 20 77 68 65 72 65 20 64 6f 63         where doc
317a6 69 64 20 3d 20 5b 69 44 6f 63 69 64 5d 20 2a 2f  id = [iDocid] */
317a7 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74  .static int cont
317a8 65 6e 74 5f 75 70 64 61 74 65 28 66 75 6c 6c 74  ent_update(fullt
317a9 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
317aa 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
317ab 6c 75 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lues,.          
317ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
317ad 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
317ae 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  cid){.  sqlite3_
317af 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 69  stmt *s;.  int i
317b0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
317b1 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
317b2 2c 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45  , CONTENT_UPDATE
317b3 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
317b4 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
317b5 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
317b6 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43  for(i=0; i<v->nC
317b7 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20  olumn; ++i){.   
317b8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
317b9 6e 64 5f 76 61 6c 75 65 28 73 2c 20 31 2b 69 2c  nd_value(s, 1+i,
317ba 20 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20   pValues[i]);.  
317bb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
317bc 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
317bd 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c  .  }..  rc = sql
317be 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
317bf 73 2c 20 31 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  s, 1+v->nColumn,
317c0 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66 28 20   iDocid);.  if( 
317c1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
317c2 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65  return rc;..  re
317c3 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f  turn sql_single_
317c4 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 73 74 61 74  step(s);.}..stat
317c5 69 63 20 76 6f 69 64 20 66 72 65 65 53 74 72 69  ic void freeStri
317c6 6e 67 41 72 72 61 79 28 69 6e 74 20 6e 53 74 72  ngArray(int nStr
317c7 69 6e 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ing, const char 
317c8 2a 2a 70 53 74 72 69 6e 67 29 7b 0a 20 20 69 6e  **pString){.  in
317c9 74 20 69 3b 0a 0a 20 20 66 6f 72 20 28 69 3d 30  t i;..  for (i=0
317ca 20 3b 20 69 20 3c 20 6e 53 74 72 69 6e 67 20 3b   ; i < nString ;
317cb 20 2b 2b 69 29 20 7b 0a 20 20 20 20 69 66 28 20   ++i) {.    if( 
317cc 70 53 74 72 69 6e 67 5b 69 5d 21 3d 4e 55 4c 4c  pString[i]!=NULL
317cd 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28   ) sqlite3_free(
317ce 28 76 6f 69 64 20 2a 29 20 70 53 74 72 69 6e 67  (void *) pString
317cf 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  [i]);.  }.  sqli
317d0 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a  te3_free((void *
317d1 29 20 70 53 74 72 69 6e 67 29 3b 0a 7d 0a 0a 2f  ) pString);.}../
317d2 2a 20 73 65 6c 65 63 74 20 2a 20 66 72 6f 6d 20  * select * from 
317d3 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20  %_content where 
317d4 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69 64 5d  docid = [iDocid]
317d5 0a 20 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  . * The caller m
317d6 75 73 74 20 64 65 6c 65 74 65 20 74 68 65 20 72  ust delete the r
317d7 65 74 75 72 6e 65 64 20 61 72 72 61 79 20 61 6e  eturned array an
317d8 64 20 61 6c 6c 20 73 74 72 69 6e 67 73 20 69 6e  d all strings in
317d9 20 69 74 2e 0a 20 2a 20 6e 75 6c 6c 20 66 69 65   it.. * null fie
317da 6c 64 73 20 77 69 6c 6c 20 62 65 20 4e 55 4c 4c  lds will be NULL
317db 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64   in the returned
317dc 20 61 72 72 61 79 2e 0a 20 2a 0a 20 2a 20 54 4f   array.. *. * TO
317dd 44 4f 3a 20 50 65 72 68 61 70 73 20 77 65 20 73  DO: Perhaps we s
317de 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 70 6f 69  hould return poi
317df 6e 74 65 72 2f 6c 65 6e 67 74 68 20 73 74 72 69  nter/length stri
317e0 6e 67 73 20 68 65 72 65 20 66 6f 72 20 63 6f 6e  ngs here for con
317e1 73 69 73 74 65 6e 63 79 0a 20 2a 20 77 69 74 68  sistency. * with
317e2 20 6f 74 68 65 72 20 63 6f 64 65 20 77 68 69 63   other code whic
317e3 68 20 75 73 65 73 20 70 6f 69 6e 74 65 72 2f 6c  h uses pointer/l
317e4 65 6e 67 74 68 2e 20 2a 2f 0a 73 74 61 74 69 63  ength. */.static
317e5 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 73 65 6c   int content_sel
317e6 65 63 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ect(fulltext_vta
317e7 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
317e8 36 34 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20  64 iDocid,.     
317e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
317ea 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
317eb 2a 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 73  ***pValues){.  s
317ec 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
317ed 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 76    const char **v
317ee 61 6c 75 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a  alues;.  int i;.
317ef 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2a 70 56    int rc;..  *pV
317f0 61 6c 75 65 73 20 3d 20 4e 55 4c 4c 3b 0a 0a 20  alues = NULL;.. 
317f1 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
317f2 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45  atement(v, CONTE
317f3 4e 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20  NT_SELECT_STMT, 
317f4 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
317f5 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
317f6 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
317f7 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
317f8 28 73 2c 20 31 2c 20 69 44 6f 63 69 64 29 3b 0a  (s, 1, iDocid);.
317f9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
317fa 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
317fb 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
317fc 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
317fd 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc!=SQLITE_ROW )
317fe 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 76   return rc;..  v
317ff 61 6c 75 65 73 20 3d 20 28 63 6f 6e 73 74 20 63  alues = (const c
31800 68 61 72 20 2a 2a 29 20 73 71 6c 69 74 65 33 5f  har **) sqlite3_
31801 6d 61 6c 6c 6f 63 28 76 2d 3e 6e 43 6f 6c 75 6d  malloc(v->nColum
31802 6e 20 2a 20 73 69 7a 65 6f 66 28 63 6f 6e 73 74  n * sizeof(const
31803 20 63 68 61 72 20 2a 29 29 3b 0a 20 20 66 6f 72   char *));.  for
31804 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75  (i=0; i<v->nColu
31805 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69 66  mn; ++i){.    if
31806 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  ( sqlite3_column
31807 5f 74 79 70 65 28 73 2c 20 69 29 3d 3d 53 51 4c  _type(s, i)==SQL
31808 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
31809 20 20 76 61 6c 75 65 73 5b 69 5d 20 3d 20 4e 55    values[i] = NU
3180a 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  LL;.    }else{. 
3180b 20 20 20 20 20 76 61 6c 75 65 73 5b 69 5d 20 3d       values[i] =
3180c 20 73 74 72 69 6e 67 5f 64 75 70 28 28 63 68 61   string_dup((cha
3180d 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r*)sqlite3_colum
3180e 6e 5f 74 65 78 74 28 73 2c 20 69 29 29 3b 0a 20  n_text(s, i));. 
3180f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57     }.  }..  /* W
31810 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e  e expect only on
31811 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20  e row.  We must 
31812 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20  execute another 
31813 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20  sqlite3_step(). 
31814 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20    * to complete 
31815 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f  the iteration; o
31816 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62  therwise the tab
31817 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c  le will remain l
31818 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d  ocked. */.  rc =
31819 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
3181a 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
3181b 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2a  TE_DONE ){.    *
3181c 70 56 61 6c 75 65 73 20 3d 20 76 61 6c 75 65 73  pValues = values
3181d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
3181e 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 66  ITE_OK;.  }..  f
3181f 72 65 65 53 74 72 69 6e 67 41 72 72 61 79 28 76  reeStringArray(v
31820 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 61 6c 75 65  ->nColumn, value
31821 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  s);.  return rc;
31822 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74 65 20 66 72  .}../* delete fr
31823 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65  om %_content whe
31824 72 65 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63  re docid = [iDoc
31825 69 64 20 5d 20 2a 2f 0a 73 74 61 74 69 63 20 69  id ] */.static i
31826 6e 74 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 65 74  nt content_delet
31827 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
31828 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
31829 20 69 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69   iDocid){.  sqli
3182a 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
3182b 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
3182c 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e  statement(v, CON
3182d 54 45 4e 54 5f 44 45 4c 45 54 45 5f 53 54 4d 54  TENT_DELETE_STMT
3182e 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
3182f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
31830 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
31831 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
31832 36 34 28 73 2c 20 31 2c 20 69 44 6f 63 69 64 29  64(s, 1, iDocid)
31833 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
31834 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
31835 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  c;..  return sql
31836 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b  _single_step(s);
31837 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53  .}../* Returns S
31838 51 4c 49 54 45 5f 52 4f 57 20 69 66 20 61 6e 79  QLITE_ROW if any
31839 20 72 6f 77 73 20 65 78 69 73 74 20 69 6e 20 25   rows exist in %
3183a 5f 63 6f 6e 74 65 6e 74 2c 20 53 51 4c 49 54 45  _content, SQLITE
3183b 5f 44 4f 4e 45 20 69 66 0a 2a 2a 20 6e 6f 20 72  _DONE if.** no r
3183c 6f 77 73 20 65 78 69 73 74 2c 20 61 6e 64 20 61  ows exist, and a
3183d 6e 79 20 65 72 72 6f 72 20 69 6e 20 63 61 73 65  ny error in case
3183e 20 6f 66 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   of failure..*/.
3183f 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65  static int conte
31840 6e 74 5f 65 78 69 73 74 73 28 66 75 6c 6c 74 65  nt_exists(fullte
31841 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 73  xt_vtab *v){.  s
31842 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
31843 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
31844 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
31845 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 5f 53  CONTENT_EXISTS_S
31846 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
31847 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
31848 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
31849 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
3184a 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
3184b 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
3184c 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  n rc;..  /* We e
3184d 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
3184e 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
3184f 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
31850 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
31851 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
31852 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
31853 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
31854 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b  will remain lock
31855 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ed. */.  rc = sq
31856 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
31857 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
31858 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51  DONE ) return SQ
31859 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 69 66 28 20  LITE_ROW;.  if( 
3185a 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
3185b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
3185c 52 52 4f 52 3b 0a 20 20 72 65 74 75 72 6e 20 72  RROR;.  return r
3185d 63 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20  c;.}../* insert 
3185e 69 6e 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 20  into %_segments 
3185f 76 61 6c 75 65 73 20 28 5b 70 44 61 74 61 5d 29  values ([pData])
31860 0a 2a 2a 20 20 20 72 65 74 75 72 6e 73 20 61 73  .**   returns as
31861 73 69 67 6e 65 64 20 62 6c 6f 63 6b 69 64 20 69  signed blockid i
31862 6e 20 2a 70 69 42 6c 6f 63 6b 69 64 0a 2a 2f 0a  n *piBlockid.*/.
31863 73 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 63 6b  static int block
31864 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74  _insert(fulltext
31865 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20  _vtab *v, const 
31866 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74  char *pData, int
31867 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20   nData,.        
31868 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31869 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
3186a 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69  Blockid){.  sqli
3186b 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
3186c 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
3186d 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f  statement(v, BLO
3186e 43 4b 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c 20  CK_INSERT_STMT, 
3186f 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
31870 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
31871 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
31872 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
31873 73 2c 20 31 2c 20 70 44 61 74 61 2c 20 6e 44 61  s, 1, pData, nDa
31874 74 61 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ta, SQLITE_STATI
31875 43 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  C);.  if( rc!=SQ
31876 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
31877 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
31878 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
31879 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
3187a 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
3187b 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20  TE_ERROR;.  if( 
3187c 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
3187d 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
3187e 2f 2a 20 62 6c 6f 63 6b 69 64 20 63 6f 6c 75 6d  /* blockid colum
3187f 6e 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66 6f  n is an alias fo
31880 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a 70  r rowid. */.  *p
31881 69 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74  iBlockid = sqlit
31882 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
31883 6f 77 69 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72  owid(v->db);.  r
31884 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
31885 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74 65 20 66 72  .}../* delete fr
31886 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 0a 2a 2a  om %_segments.**
31887 20 20 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64     where blockid
31888 20 62 65 74 77 65 65 6e 20 5b 69 53 74 61 72 74   between [iStart
31889 42 6c 6f 63 6b 69 64 5d 20 61 6e 64 20 5b 69 45  Blockid] and [iE
3188a 6e 64 42 6c 6f 63 6b 69 64 5d 0a 2a 2a 0a 2a 2a  ndBlockid].**.**
3188b 20 44 65 6c 65 74 65 73 20 74 68 65 20 72 61 6e   Deletes the ran
3188c 67 65 20 6f 66 20 62 6c 6f 63 6b 73 2c 20 69 6e  ge of blocks, in
3188d 63 6c 75 73 69 76 65 2c 20 75 73 65 64 20 74 6f  clusive, used to
3188e 20 64 65 6c 65 74 65 20 74 68 65 20 62 6c 6f 63   delete the bloc
3188f 6b 73 0a 2a 2a 20 77 68 69 63 68 20 66 6f 72 6d  ks.** which form
31890 20 61 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 73   a segment..*/.s
31891 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 63 6b 5f  tatic int block_
31892 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f  delete(fulltext_
31893 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20  vtab *v,.       
31894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31895 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53   sqlite_int64 iS
31896 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 73 71 6c  tartBlockid, sql
31897 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c  ite_int64 iEndBl
31898 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  ockid){.  sqlite
31899 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
3189a 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
3189b 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b  atement(v, BLOCK
3189c 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73  _DELETE_STMT, &s
3189d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
3189e 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
3189f 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
318a0 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
318a1 2c 20 31 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b  , 1, iStartBlock
318a2 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
318a3 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
318a4 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
318a5 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
318a6 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c 6f 63 6b  (s, 2, iEndBlock
318a7 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
318a8 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
318a9 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
318aa 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
318ab 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e  s);.}../* Return
318ac 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 77 69 74  s SQLITE_ROW wit
318ad 68 20 2a 70 69 64 78 20 73 65 74 20 74 6f 20 74  h *pidx set to t
318ae 68 65 20 6d 61 78 69 6d 75 6d 20 73 65 67 6d 65  he maximum segme
318af 6e 74 20 69 64 78 20 66 6f 75 6e 64 0a 2a 2a 20  nt idx found.** 
318b0 61 74 20 69 4c 65 76 65 6c 2e 20 20 52 65 74 75  at iLevel.  Retu
318b1 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rns SQLITE_DONE 
318b2 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  if there are no 
318b3 73 65 67 6d 65 6e 74 73 20 61 74 0a 2a 2a 20 69  segments at.** i
318b4 4c 65 76 65 6c 2e 20 20 4f 74 68 65 72 77 69 73  Level.  Otherwis
318b5 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  e returns an err
318b6 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
318b7 74 20 73 65 67 64 69 72 5f 6d 61 78 5f 69 6e 64  t segdir_max_ind
318b8 65 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ex(fulltext_vtab
318b9 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *v, int iLevel,
318ba 20 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20 73   int *pidx){.  s
318bb 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
318bc 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
318bd 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
318be 53 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58  SEGDIR_MAX_INDEX
318bf 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
318c0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
318c1 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
318c2 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
318c3 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65 76  d_int(s, 1, iLev
318c4 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  el);.  if( rc!=S
318c5 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
318c6 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
318c7 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
318c8 20 2f 2a 20 53 68 6f 75 6c 64 20 61 6c 77 61 79   /* Should alway
318c9 73 20 67 65 74 20 61 74 20 6c 65 61 73 74 20 6f  s get at least o
318ca 6e 65 20 72 6f 77 20 64 75 65 20 74 6f 20 68 6f  ne row due to ho
318cb 77 20 6d 61 78 28 29 20 77 6f 72 6b 73 2e 20 2a  w max() works. *
318cc 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
318cd 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e  TE_DONE ) return
318ce 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
318cf 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
318d0 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OW ) return rc;.
318d1 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6d 65 61 6e 73  .  /* NULL means
318d2 20 74 68 61 74 20 74 68 65 72 65 20 77 65 72 65   that there were
318d3 20 6e 6f 20 69 6e 70 75 74 73 20 74 6f 20 6d 61   no inputs to ma
318d4 78 28 29 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51  x(). */.  if( SQ
318d5 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74  LITE_NULL==sqlit
318d6 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73  e3_column_type(s
318d7 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  , 0) ){.    rc =
318d8 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
318d9 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
318da 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
318db 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
318dc 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
318dd 20 7d 0a 0a 20 20 2a 70 69 64 78 20 3d 20 73 71   }..  *pidx = sq
318de 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
318df 28 73 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 57 65  (s, 0);..  /* We
318e0 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65   expect only one
318e1 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65   row.  We must e
318e2 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73  xecute another s
318e3 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20  qlite3_step().  
318e4 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74   * to complete t
318e5 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74  he iteration; ot
318e6 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c  herwise the tabl
318e7 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f  e will remain lo
318e8 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  cked. */.  rc = 
318e9 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
318ea 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
318eb 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53  E_ROW ) return S
318ec 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69  QLITE_ERROR;.  i
318ed 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  f( rc!=SQLITE_DO
318ee 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  NE ) return rc;.
318ef 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
318f0 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72  ROW;.}../* inser
318f1 74 20 69 6e 74 6f 20 25 5f 73 65 67 64 69 72 20  t into %_segdir 
318f2 76 61 6c 75 65 73 20 28 0a 2a 2a 20 20 20 5b 69  values (.**   [i
318f3 4c 65 76 65 6c 5d 2c 20 5b 69 64 78 5d 2c 0a 2a  Level], [idx],.*
318f4 2a 20 20 20 5b 69 53 74 61 72 74 42 6c 6f 63 6b  *   [iStartBlock
318f5 69 64 5d 2c 20 5b 69 4c 65 61 76 65 73 45 6e 64  id], [iLeavesEnd
318f6 42 6c 6f 63 6b 69 64 5d 2c 20 5b 69 45 6e 64 42  Blockid], [iEndB
318f7 6c 6f 63 6b 69 64 5d 2c 0a 2a 2a 20 20 20 5b 70  lockid],.**   [p
318f8 52 6f 6f 74 44 61 74 61 5d 0a 2a 2a 20 29 0a 2a  RootData].** ).*
318f9 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
318fa 64 69 72 5f 73 65 74 28 66 75 6c 6c 74 65 78 74  dir_set(fulltext
318fb 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
318fc 65 76 65 6c 2c 20 69 6e 74 20 69 64 78 2c 0a 20  evel, int idx,. 
318fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318fe 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
318ff 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  4 iStartBlockid,
31900 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
31901 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
31902 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 42 6c  t64 iLeavesEndBl
31903 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
31904 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
31905 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c  ite_int64 iEndBl
31906 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
31907 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
31908 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61  st char *pRootDa
31909 74 61 2c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74  ta, int nRootDat
3190a 61 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  a){.  sqlite3_st
3190b 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
3190c 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
3190d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45  ent(v, SEGDIR_SE
3190e 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  T_STMT, &s);.  i
3190f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
31910 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
31911 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
31912 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65  nd_int(s, 1, iLe
31913 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  vel);.  if( rc!=
31914 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
31915 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
31916 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
31917 73 2c 20 32 2c 20 69 64 78 29 3b 0a 20 20 69 66  s, 2, idx);.  if
31918 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
31919 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
3191a 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
3191b 64 5f 69 6e 74 36 34 28 73 2c 20 33 2c 20 69 53  d_int64(s, 3, iS
3191c 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  tartBlockid);.  
3191d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3191e 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
3191f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
31920 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 34 2c 20  ind_int64(s, 4, 
31921 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69  iLeavesEndBlocki
31922 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
31923 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
31924 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
31925 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
31926 73 2c 20 35 2c 20 69 45 6e 64 42 6c 6f 63 6b 69  s, 5, iEndBlocki
31927 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
31928 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
31929 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
3192a 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73  ite3_bind_blob(s
3192b 2c 20 36 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20  , 6, pRootData, 
3192c 6e 52 6f 6f 74 44 61 74 61 2c 20 53 51 4c 49 54  nRootData, SQLIT
3192d 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28  E_STATIC);.  if(
3192e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3192f 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
31930 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65  eturn sql_single
31931 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20  _step(s);.}../* 
31932 51 75 65 72 69 65 73 20 25 5f 73 65 67 64 69 72  Queries %_segdir
31933 20 66 6f 72 20 74 68 65 20 62 6c 6f 63 6b 20 73   for the block s
31934 70 61 6e 20 6f 66 20 74 68 65 20 73 65 67 6d 65  pan of the segme
31935 6e 74 73 20 69 6e 20 6c 65 76 65 6c 0a 2a 2a 20  nts in level.** 
31936 69 4c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 73  iLevel.  Returns
31937 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 66 20   SQLITE_DONE if 
31938 74 68 65 72 65 20 61 72 65 20 6e 6f 20 62 6c 6f  there are no blo
31939 63 6b 73 20 66 6f 72 20 69 4c 65 76 65 6c 2c 0a  cks for iLevel,.
3193a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 20 69 66  ** SQLITE_ROW if
3193b 20 74 68 65 72 65 20 61 72 65 20 62 6c 6f 63 6b   there are block
3193c 73 2c 20 65 6c 73 65 20 61 6e 20 65 72 72 6f 72  s, else an error
3193d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3193e 73 65 67 64 69 72 5f 73 70 61 6e 28 66 75 6c 6c  segdir_span(full
3193f 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
31940 74 20 69 4c 65 76 65 6c 2c 0a 20 20 20 20 20 20  t iLevel,.      
31941 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31942 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
31943 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20  iStartBlockid,. 
31944 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31945 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
31946 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  64 *piEndBlockid
31947 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
31948 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
31949 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
3194a 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 50 41  nt(v, SEGDIR_SPA
3194b 4e 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  N_STMT, &s);.  i
3194c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3194d 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
3194e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
3194f 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65  nd_int(s, 1, iLe
31950 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  vel);.  if( rc!=
31951 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
31952 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
31953 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
31954 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
31955 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53  _DONE ) return S
31956 51 4c 49 54 45 5f 44 4f 4e 45 3b 20 20 2f 2a 20  QLITE_DONE;  /* 
31957 53 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70  Should never hap
31958 70 65 6e 20 2a 2f 0a 20 20 69 66 28 20 72 63 21  pen */.  if( rc!
31959 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
3195a 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54  turn rc;..  /* T
3195b 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61  his happens if a
3195c 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 74  ll segments at t
3195d 68 69 73 20 6c 65 76 65 6c 20 61 72 65 20 65 6e  his level are en
3195e 74 69 72 65 6c 79 20 69 6e 6c 69 6e 65 2e 20 2a  tirely inline. *
3195f 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e  /.  if( SQLITE_N
31960 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 63 6f 6c  ULL==sqlite3_col
31961 75 6d 6e 5f 74 79 70 65 28 73 2c 20 30 29 20 29  umn_type(s, 0) )
31962 7b 0a 20 20 20 20 2f 2a 20 57 65 20 65 78 70 65  {.    /* We expe
31963 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e  ct only one row.
31964 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74    We must execut
31965 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65  e another sqlite
31966 33 5f 73 74 65 70 28 29 0a 20 20 20 20 20 2a 20  3_step().     * 
31967 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20  to complete the 
31968 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72  iteration; other
31969 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77  wise the table w
3196a 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65  ill remain locke
3196b 64 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63  d. */.    int rc
3196c 32 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  2 = sqlite3_step
3196d 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32  (s);.    if( rc2
3196e 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  ==SQLITE_ROW ) r
3196f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
31970 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  OR;.    return r
31971 63 32 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 53 74  c2;.  }..  *piSt
31972 61 72 74 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c  artBlockid = sql
31973 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
31974 34 28 73 2c 20 30 29 3b 0a 20 20 2a 70 69 45 6e  4(s, 0);.  *piEn
31975 64 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74  dBlockid = sqlit
31976 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
31977 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57 65 20  s, 1);..  /* We 
31978 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20  expect only one 
31979 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78  row.  We must ex
3197a 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71  ecute another sq
3197b 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20  lite3_step().   
3197c 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  * to complete th
3197d 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68  e iteration; oth
3197e 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65  erwise the table
3197f 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63   will remain loc
31980 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73  ked. */.  rc = s
31981 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
31982 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
31983 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51  _ROW ) return SQ
31984 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66  LITE_ERROR;.  if
31985 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
31986 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  E ) return rc;. 
31987 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
31988 4f 57 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65  OW;.}../* Delete
31989 20 74 68 65 20 73 65 67 6d 65 6e 74 20 62 6c 6f   the segment blo
3198a 63 6b 73 20 61 6e 64 20 73 65 67 6d 65 6e 74 20  cks and segment 
3198b 64 69 72 65 63 74 6f 72 79 20 72 65 63 6f 72 64  directory record
3198c 73 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 73 65 67  s for all.** seg
3198d 6d 65 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 2e  ments at iLevel.
3198e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
3198f 65 67 64 69 72 5f 64 65 6c 65 74 65 28 66 75 6c  egdir_delete(ful
31990 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
31991 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20 73 71  nt iLevel){.  sq
31992 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
31993 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53   sqlite_int64 iS
31994 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e  tartBlockid, iEn
31995 64 42 6c 6f 63 6b 69 64 3b 0a 20 20 69 6e 74 20  dBlockid;.  int 
31996 72 63 20 3d 20 73 65 67 64 69 72 5f 73 70 61 6e  rc = segdir_span
31997 28 76 2c 20 69 4c 65 76 65 6c 2c 20 26 69 53 74  (v, iLevel, &iSt
31998 61 72 74 42 6c 6f 63 6b 69 64 2c 20 26 69 45 6e  artBlockid, &iEn
31999 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28  dBlockid);.  if(
3199a 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc!=SQLITE_ROW 
3199b 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  && rc!=SQLITE_DO
3199c 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  NE ) return rc;.
3199d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3199e 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72 63 20  E_ROW ){.    rc 
3199f 3d 20 62 6c 6f 63 6b 5f 64 65 6c 65 74 65 28 76  = block_delete(v
319a0 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  , iStartBlockid,
319a1 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20   iEndBlockid);. 
319a2 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
319a3 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
319a4 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65  ;.  }..  /* Dele
319a5 74 65 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64  te the segment d
319a6 69 72 65 63 74 6f 72 79 20 69 74 73 65 6c 66 2e  irectory itself.
319a7 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67   */.  rc = sql_g
319a8 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
319a9 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 53 54  SEGDIR_DELETE_ST
319aa 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
319ab 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
319ac 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
319ad 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
319ae 6e 74 36 34 28 73 2c 20 31 2c 20 69 4c 65 76 65  nt64(s, 1, iLeve
319af 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  l);.  if( rc!=SQ
319b0 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
319b1 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73   rc;..  return s
319b2 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
319b3 29 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20  );.}../* Delete 
319b4 65 6e 74 69 72 65 20 66 74 73 20 69 6e 64 65 78  entire fts index
319b5 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  , SQLITE_OK on s
319b6 75 63 63 65 73 73 2c 20 72 65 6c 65 76 61 6e 74  uccess, relevant
319b7 20 65 72 72 6f 72 20 6f 6e 0a 2a 2a 20 66 61 69   error on.** fai
319b8 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lure..*/.static 
319b9 69 6e 74 20 73 65 67 64 69 72 5f 64 65 6c 65 74  int segdir_delet
319ba 65 5f 61 6c 6c 28 66 75 6c 6c 74 65 78 74 5f 76  e_all(fulltext_v
319bb 74 61 62 20 2a 76 29 7b 0a 20 20 73 71 6c 69 74  tab *v){.  sqlit
319bc 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e  e3_stmt *s;.  in
319bd 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73  t rc = sql_get_s
319be 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
319bf 49 52 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54  IR_DELETE_ALL_ST
319c0 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
319c1 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
319c2 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
319c3 3d 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65  = sql_single_ste
319c4 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  p(s);.  if( rc!=
319c5 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
319c6 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
319c7 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
319c8 28 76 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45  (v, BLOCK_DELETE
319c9 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  _ALL_STMT, &s);.
319ca 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
319cb 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
319cc 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73  ..  return sql_s
319cd 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d  ingle_step(s);.}
319ce 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c  ../* Returns SQL
319cf 49 54 45 5f 4f 4b 20 77 69 74 68 20 2a 70 6e 53  ITE_OK with *pnS
319d0 65 67 6d 65 6e 74 73 20 73 65 74 20 74 6f 20 74  egments set to t
319d1 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
319d2 72 69 65 73 20 69 6e 0a 2a 2a 20 25 5f 73 65 67  ries in.** %_seg
319d3 64 69 72 20 61 6e 64 20 2a 70 69 4d 61 78 4c 65  dir and *piMaxLe
319d4 76 65 6c 20 73 65 74 20 74 6f 20 74 68 65 20 68  vel set to the h
319d5 69 67 68 65 73 74 20 6c 65 76 65 6c 20 77 68 69  ighest level whi
319d6 63 68 20 68 61 73 20 61 0a 2a 2a 20 73 65 67 6d  ch has a.** segm
319d7 65 6e 74 2e 20 20 4f 74 68 65 72 77 69 73 65 20  ent.  Otherwise 
319d8 72 65 74 75 72 6e 73 20 74 68 65 20 53 51 4c 69  returns the SQLi
319d9 74 65 20 65 72 72 6f 72 20 77 68 69 63 68 20 63  te error which c
319da 61 75 73 65 64 20 66 61 69 6c 75 72 65 2e 0a 2a  aused failure..*
319db 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
319dc 64 69 72 5f 63 6f 75 6e 74 28 66 75 6c 6c 74 65  dir_count(fullte
319dd 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
319de 2a 70 6e 53 65 67 6d 65 6e 74 73 2c 20 69 6e 74  *pnSegments, int
319df 20 2a 70 69 4d 61 78 4c 65 76 65 6c 29 7b 0a 20   *piMaxLevel){. 
319e0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
319e1 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
319e2 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
319e3 2c 20 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f 53  , SEGDIR_COUNT_S
319e4 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
319e5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
319e6 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
319e7 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
319e8 73 29 3b 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  s);.  /* TODO(sh
319e9 65 73 73 29 3a 20 54 68 69 73 20 63 61 73 65 20  ess): This case 
319ea 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 70 6f  should not be po
319eb 73 73 69 62 6c 65 3f 20 20 53 68 6f 75 6c 64 20  ssible?  Should 
319ec 73 74 72 6f 6e 67 65 72 0a 20 20 2a 2a 20 6d 65  stronger.  ** me
319ed 61 73 75 72 65 73 20 62 65 20 74 61 6b 65 6e 20  asures be taken 
319ee 69 66 20 69 74 20 68 61 70 70 65 6e 73 3f 0a 20  if it happens?. 
319ef 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
319f0 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
319f1 20 2a 70 6e 53 65 67 6d 65 6e 74 73 20 3d 20 30   *pnSegments = 0
319f2 3b 0a 20 20 20 20 2a 70 69 4d 61 78 4c 65 76 65  ;.    *piMaxLeve
319f3 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72  l = 0;.    retur
319f4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
319f5 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
319f6 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72  E_ROW ) return r
319f7 63 3b 0a 0a 20 20 2a 70 6e 53 65 67 6d 65 6e 74  c;..  *pnSegment
319f8 73 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  s = sqlite3_colu
319f9 6d 6e 5f 69 6e 74 28 73 2c 20 30 29 3b 0a 20 20  mn_int(s, 0);.  
319fa 2a 70 69 4d 61 78 4c 65 76 65 6c 20 3d 20 73 71  *piMaxLevel = sq
319fb 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
319fc 28 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57 65  (s, 1);..  /* We
319fd 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65   expect only one
319fe 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65   row.  We must e
319ff 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73  xecute another s
31a00 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20  qlite3_step().  
31a01 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74   * to complete t
31a02 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74  he iteration; ot
31a03 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c  herwise the tabl
31a04 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f  e will remain lo
31a05 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  cked. */.  rc = 
31a06 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
31a07 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
31a08 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
31a09 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
31a0a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc==SQLITE_ROW 
31a0b 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
31a0c 45 52 52 4f 52 3b 0a 20 20 72 65 74 75 72 6e 20  ERROR;.  return 
31a0d 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73  rc;.}../* TODO(s
31a0e 68 65 73 73 29 20 63 6c 65 61 72 50 65 6e 64 69  hess) clearPendi
31a0f 6e 67 54 65 72 6d 73 28 29 20 69 73 20 66 61 72  ngTerms() is far
31a10 20 64 6f 77 6e 20 74 68 65 20 66 69 6c 65 20 62   down the file b
31a11 65 63 61 75 73 65 0a 2a 2a 20 77 72 69 74 65 5a  ecause.** writeZ
31a12 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 69 73 20  eroSegment() is 
31a13 66 61 72 20 64 6f 77 6e 20 74 68 65 20 66 69 6c  far down the fil
31a14 65 20 62 65 63 61 75 73 65 20 4c 65 61 66 57 72  e because LeafWr
31a15 69 74 65 72 20 69 73 20 66 61 72 0a 2a 2a 20 64  iter is far.** d
31a16 6f 77 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 43  own the file.  C
31a17 6f 6e 73 69 64 65 72 20 72 65 66 61 63 74 6f 72  onsider refactor
31a18 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74 6f 20  ing the code to 
31a19 6d 6f 76 65 20 74 68 65 20 6e 6f 6e 2d 76 74 61  move the non-vta
31a1a 62 0a 2a 2a 20 63 6f 64 65 20 61 62 6f 76 65 20  b.** code above 
31a1b 74 68 65 20 76 74 61 62 20 63 6f 64 65 20 73 6f  the vtab code so
31a1c 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 6e   that we don't n
31a1d 65 65 64 20 74 68 69 73 20 66 6f 72 77 61 72 64  eed this forward
31a1e 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a  .** reference..*
31a1f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65  /.static int cle
31a20 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  arPendingTerms(f
31a21 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
31a22 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68  ;../*.** Free th
31a23 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f  e memory used to
31a24 20 63 6f 6e 74 61 69 6e 20 61 20 66 75 6c 6c 74   contain a fullt
31a25 65 78 74 5f 76 74 61 62 20 73 74 72 75 63 74 75  ext_vtab structu
31a26 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  re..*/.static vo
31a27 69 64 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  id fulltext_vtab
31a28 5f 64 65 73 74 72 6f 79 28 66 75 6c 6c 74 65 78  _destroy(fulltex
31a29 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 6e  t_vtab *v){.  in
31a2a 74 20 69 53 74 6d 74 2c 20 69 3b 0a 0a 20 20 46  t iStmt, i;..  F
31a2b 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 44  TSTRACE(("FTS3 D
31a2c 65 73 74 72 6f 79 20 25 70 5c 6e 22 2c 20 76 29  estroy %p\n", v)
31a2d 29 3b 0a 20 20 66 6f 72 28 20 69 53 74 6d 74 3d  );.  for( iStmt=
31a2e 30 3b 20 69 53 74 6d 74 3c 4d 41 58 5f 53 54 4d  0; iStmt<MAX_STM
31a2f 54 3b 20 69 53 74 6d 74 2b 2b 20 29 7b 0a 20 20  T; iStmt++ ){.  
31a30 20 20 69 66 28 20 76 2d 3e 70 46 75 6c 6c 74 65    if( v->pFullte
31a31 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74  xtStatements[iSt
31a32 6d 74 5d 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  mt]!=NULL ){.   
31a33 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
31a34 69 7a 65 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74  ize(v->pFulltext
31a35 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74  Statements[iStmt
31a36 5d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 70 46 75  ]);.      v->pFu
31a37 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
31a38 5b 69 53 74 6d 74 5d 20 3d 20 4e 55 4c 4c 3b 0a  [iStmt] = NULL;.
31a39 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72      }.  }..  for
31a3a 28 20 69 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43  ( i=0; i<MERGE_C
31a3b 4f 55 4e 54 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  OUNT; i++ ){.   
31a3c 20 69 66 28 20 76 2d 3e 70 4c 65 61 66 53 65 6c   if( v->pLeafSel
31a3d 65 63 74 53 74 6d 74 73 5b 69 5d 21 3d 4e 55 4c  ectStmts[i]!=NUL
31a3e 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L ){.      sqlit
31a3f 65 33 5f 66 69 6e 61 6c 69 7a 65 28 76 2d 3e 70  e3_finalize(v->p
31a40 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b  LeafSelectStmts[
31a41 69 5d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 70 4c  i]);.      v->pL
31a42 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69  eafSelectStmts[i
31a43 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a  ] = NULL;.    }.
31a44 20 20 7d 0a 0a 20 20 69 66 28 20 76 2d 3e 70 54    }..  if( v->pT
31a45 6f 6b 65 6e 69 7a 65 72 21 3d 4e 55 4c 4c 20 29  okenizer!=NULL )
31a46 7b 0a 20 20 20 20 76 2d 3e 70 54 6f 6b 65 6e 69  {.    v->pTokeni
31a47 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44  zer->pModule->xD
31a48 65 73 74 72 6f 79 28 76 2d 3e 70 54 6f 6b 65 6e  estroy(v->pToken
31a49 69 7a 65 72 29 3b 0a 20 20 20 20 76 2d 3e 70 54  izer);.    v->pT
31a4a 6f 6b 65 6e 69 7a 65 72 20 3d 20 4e 55 4c 4c 3b  okenizer = NULL;
31a4b 0a 20 20 7d 0a 0a 20 20 63 6c 65 61 72 50 65 6e  .  }..  clearPen
31a4c 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 0a 20  dingTerms(v);.. 
31a4d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d   sqlite3_free(v-
31a4e 3e 61 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 66 6f  >azColumn);.  fo
31a4f 72 28 69 20 3d 20 30 3b 20 69 20 3c 20 76 2d 3e  r(i = 0; i < v->
31a50 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a  nColumn; ++i) {.
31a51 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
31a52 28 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  (v->azContentCol
31a53 75 6d 6e 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73  umn[i]);.  }.  s
31a54 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d 3e 61  qlite3_free(v->a
31a55 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b  zContentColumn);
31a56 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
31a57 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b  v);.}../*.** Tok
31a58 65 6e 20 74 79 70 65 73 20 66 6f 72 20 70 61 72  en types for par
31a59 73 69 6e 67 20 74 68 65 20 61 72 67 75 6d 65 6e  sing the argumen
31a5a 74 73 20 74 6f 20 78 43 6f 6e 6e 65 63 74 20 6f  ts to xConnect o
31a5b 72 20 78 43 72 65 61 74 65 2e 0a 2a 2f 0a 23 64  r xCreate..*/.#d
31a5c 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 45 4f 46 20  efine TOKEN_EOF 
31a5d 20 20 20 20 20 20 20 20 30 20 20 20 20 2f 2a 20          0    /* 
31a5e 45 6e 64 20 6f 66 20 66 69 6c 65 20 2a 2f 0a 23  End of file */.#
31a5f 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 53 50 41  define TOKEN_SPA
31a60 43 45 20 20 20 20 20 20 20 31 20 20 20 20 2f 2a  CE       1    /*
31a61 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 77 68 69   Any kind of whi
31a62 74 65 73 70 61 63 65 20 2a 2f 0a 23 64 65 66 69  tespace */.#defi
31a63 6e 65 20 54 4f 4b 45 4e 5f 49 44 20 20 20 20 20  ne TOKEN_ID     
31a64 20 20 20 20 20 32 20 20 20 20 2f 2a 20 41 6e 20       2    /* An 
31a65 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 23 64  identifier */.#d
31a66 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 53 54 52 49  efine TOKEN_STRI
31a67 4e 47 20 20 20 20 20 20 33 20 20 20 20 2f 2a 20  NG      3    /* 
31a68 41 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c  A string literal
31a69 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45   */.#define TOKE
31a6a 4e 5f 50 55 4e 43 54 20 20 20 20 20 20 20 34 20  N_PUNCT       4 
31a6b 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 70     /* A single p
31a6c 75 6e 63 74 75 61 74 69 6f 6e 20 63 68 61 72 61  unctuation chara
31a6d 63 74 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  cter */../*.** I
31a6e 66 20 58 20 69 73 20 61 20 63 68 61 72 61 63 74  f X is a charact
31a6f 65 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  er that can be u
31a70 73 65 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69  sed in an identi
31a71 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20 66 74 73  fier then.** fts
31a72 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62  IdChar(X) will b
31a73 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  e true.  Otherwi
31a74 73 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a  se it is false..
31a75 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c  **.** For ASCII,
31a76 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77   any character w
31a77 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64  ith the high-ord
31a78 65 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a  er bit set is.**
31a79 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69   allowed in an i
31a7a 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20  dentifier.  For 
31a7b 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 73  7-bit characters
31a7c 2c 20 0a 2a 2a 20 69 73 46 74 73 49 64 43 68 61  , .** isFtsIdCha
31a7d 72 5b 58 5d 20 6d 75 73 74 20 62 65 20 31 2e 0a  r[X] must be 1..
31a7e 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 30  **.** Ticket #10
31a7f 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73 74 61  66.  the SQL sta
31a80 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74 20 61  ndard does not a
31a81 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68 65 0a  llow '$' in the.
31a82 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69 64 65  ** middle of ide
31a83 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20 6d 61  ntfiers.  But ma
31a84 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65 6e 74  ny SQL implement
31a85 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20 53  ations do. .** S
31a86 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f 77  QLite will allow
31a87 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69 66 69   '$' in identifi
31a88 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74 69 62  ers for compatib
31a89 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 74 68  ility..** But th
31a8a 65 20 66 65 61 74 75 72 65 20 69 73 20 75 6e 64  e feature is und
31a8b 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 73 74  ocumented..*/.st
31a8c 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
31a8d 69 73 46 74 73 49 64 43 68 61 72 5b 5d 20 3d 20  isFtsIdChar[] = 
31a8e 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33  {./* x0 x1 x2 x3
31a8f 20 78 34 20 78 35 20 78 36 20 78 37 20 78 38 20   x4 x5 x6 x7 x8 
31a90 78 39 20 78 41 20 78 42 20 78 43 20 78 44 20 78  x9 xA xB xC xD x
31a91 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30  E xF */.    0, 0
31a92 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c  , 0, 0, 1, 0, 0,
31a93 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
31a94 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
31a95 32 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  2x */.    1, 1, 
31a96 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31a97 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
31a98 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78   0, 0, 0,  /* 3x
31a99 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
31a9a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31a9b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31a9c 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a  , 1, 1,  /* 4x *
31a9d 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
31a9e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31a9f 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
31aa0 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  0, 1,  /* 5x */.
31aa1 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
31aa2 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31aa3 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31aa4 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   1,  /* 6x */.  
31aa5 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
31aa6 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31aa7 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
31aa8 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23  ,  /* 7x */.};.#
31aa9 64 65 66 69 6e 65 20 66 74 73 49 64 43 68 61 72  define ftsIdChar
31aaa 28 43 29 20 20 28 28 28 63 3d 43 29 26 30 78 38  (C)  (((c=C)&0x8
31aab 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66  0)!=0 || (c>0x1f
31aac 20 26 26 20 69 73 46 74 73 49 64 43 68 61 72 5b   && isFtsIdChar[
31aad 63 2d 30 78 32 30 5d 29 29 0a 0a 0a 2f 2a 0a 2a  c-0x20])).../*.*
31aae 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e  * Return the len
31aaf 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  gth of the token
31ab0 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 74 20   that begins at 
31ab1 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65 20  z[0]. .** Store 
31ab2 74 68 65 20 74 6f 6b 65 6e 20 74 79 70 65 20 69  the token type i
31ab3 6e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62 65 66  n *tokenType bef
31ab4 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
31ab5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
31ab6 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 63  GetToken(const c
31ab7 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b  har *z, int *tok
31ab8 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69  enType){.  int i
31ab9 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a  , c;.  switch( *
31aba 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 30 3a  z ){.    case 0:
31abb 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
31abc 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 45 4f 46 3b  ype = TOKEN_EOF;
31abd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
31abe 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
31abf 27 20 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20  ' ': case '\t': 
31ac0 63 61 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20  case '\n': case 
31ac1 27 5c 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a  '\f': case '\r':
31ac2 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31   {.      for(i=1
31ac3 3b 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 7a  ; safe_isspace(z
31ac4 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
31ac5 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31ac6 54 4f 4b 45 4e 5f 53 50 41 43 45 3b 0a 20 20 20  TOKEN_SPACE;.   
31ac7 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
31ac8 20 7d 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a   }.    case '`':
31ac9 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0a  .    case '\'':.
31aca 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 7b 0a      case '"': {.
31acb 20 20 20 20 20 20 69 6e 74 20 64 65 6c 69 6d 20        int delim 
31acc 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20 20 20 66 6f  = z[0];.      fo
31acd 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
31ace 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
31acf 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29    if( c==delim )
31ad0 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
31ad1 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b  z[i+1]==delim ){
31ad2 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
31ad3 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
31ad4 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  e{.            b
31ad5 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
31ad6 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
31ad7 20 20 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e    }.      *token
31ad8 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 53 54 52  Type = TOKEN_STR
31ad9 49 4e 47 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ING;.      retur
31ada 6e 20 69 20 2b 20 28 63 21 3d 30 29 3b 0a 20 20  n i + (c!=0);.  
31adb 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 5b 27    }.    case '['
31adc 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  : {.      for(i=
31add 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27 5d  1, c=z[0]; c!=']
31ade 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30  ' && (c=z[i])!=0
31adf 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a  ; i++){}.      *
31ae0 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45  tokenType = TOKE
31ae1 4e 5f 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75  N_ID;.      retu
31ae2 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn i;.    }.    
31ae3 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
31ae4 20 69 66 28 20 21 66 74 73 49 64 43 68 61 72 28   if( !ftsIdChar(
31ae5 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  *z) ){.        b
31ae6 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
31ae7 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 66 74 73      for(i=1; fts
31ae8 49 64 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b  IdChar(z[i]); i+
31ae9 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65  +){}.      *toke
31aea 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 49 44  nType = TOKEN_ID
31aeb 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
31aec 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74  ;.    }.  }.  *t
31aed 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e  okenType = TOKEN
31aee 5f 50 55 4e 43 54 3b 0a 20 20 72 65 74 75 72 6e  _PUNCT;.  return
31aef 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 74   1;.}../*.** A t
31af0 6f 6b 65 6e 20 65 78 74 72 61 63 74 65 64 20 66  oken extracted f
31af1 72 6f 6d 20 61 20 73 74 72 69 6e 67 20 69 73 20  rom a string is 
31af2 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
31af3 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
31af4 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
31af5 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73  pedef struct Fts
31af6 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20  Token {.  const 
31af7 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 2f  char *z;       /
31af8 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b  * Pointer to tok
31af9 65 6e 20 74 65 78 74 2e 20 20 4e 6f 74 20 27 5c  en text.  Not '\
31afa 30 30 30 27 20 74 65 72 6d 69 6e 61 74 65 64 20  000' terminated 
31afb 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20 6e  */.  short int n
31afc 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e  ;         /* Len
31afd 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  gth of the token
31afe 20 74 65 78 74 20 69 6e 20 62 79 74 65 73 2e 20   text in bytes. 
31aff 2a 2f 0a 7d 20 46 74 73 54 6f 6b 65 6e 3b 0a 0a  */.} FtsToken;..
31b00 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 69 6e  /*.** Given a in
31b01 70 75 74 20 73 74 72 69 6e 67 20 28 77 68 69 63  put string (whic
31b02 68 20 69 73 20 72 65 61 6c 6c 79 20 6f 6e 65 20  h is really one 
31b03 6f 66 20 74 68 65 20 61 72 67 76 5b 5d 20 70 61  of the argv[] pa
31b04 72 61 6d 65 74 65 72 73 0a 2a 2a 20 70 61 73 73  rameters.** pass
31b05 65 64 20 69 6e 74 6f 20 78 43 6f 6e 6e 65 63 74  ed into xConnect
31b06 20 6f 72 20 78 43 72 65 61 74 65 29 20 73 70 6c   or xCreate) spl
31b07 69 74 20 74 68 65 20 73 74 72 69 6e 67 20 75 70  it the string up
31b08 20 69 6e 74 6f 20 74 6f 6b 65 6e 73 2e 0a 2a 2a   into tokens..**
31b09 20 52 65 74 75 72 6e 20 61 6e 20 61 72 72 61 79   Return an array
31b0a 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
31b0b 27 5c 30 30 30 27 20 74 65 72 6d 69 6e 61 74 65  '\000' terminate
31b0c 64 20 73 74 72 69 6e 67 73 2c 20 6f 6e 65 20 73  d strings, one s
31b0d 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 65 61 63  tring.** for eac
31b0e 68 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63 65  h non-whitespace
31b0f 20 74 6f 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68   token..**.** Th
31b10 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61 79  e returned array
31b11 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64 20 62   is terminated b
31b12 79 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20  y a single NULL 
31b13 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 53  pointer..**.** S
31b14 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
31b15 20 72 65 74 75 72 6e 65 64 20 61 72 72 61 79 20   returned array 
31b16 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
31b17 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d 61 6c   a single.** mal
31b18 6c 6f 63 20 61 6e 64 20 73 68 6f 75 6c 64 20 62  loc and should b
31b19 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73 69  e freed by passi
31b1a 6e 67 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ng the return va
31b1b 6c 75 65 20 74 6f 20 66 72 65 65 28 29 2e 0a 2a  lue to free()..*
31b1c 2a 20 54 68 65 20 69 6e 64 69 76 69 64 75 61 6c  * The individual
31b1d 20 73 74 72 69 6e 67 73 20 77 69 74 68 69 6e 20   strings within 
31b1e 74 68 65 20 74 6f 6b 65 6e 20 6c 69 73 74 20 61  the token list a
31b1f 72 65 20 61 6c 6c 20 61 20 70 61 72 74 20 6f 66  re all a part of
31b20 0a 2a 2a 20 74 68 65 20 73 69 6e 67 6c 65 20 6d  .** the single m
31b21 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
31b22 20 61 6e 64 20 77 69 6c 6c 20 61 6c 6c 20 62 65   and will all be
31b23 20 66 72 65 65 64 20 61 74 20 6f 6e 63 65 2e 0a   freed at once..
31b24 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
31b25 2a 74 6f 6b 65 6e 69 7a 65 53 74 72 69 6e 67 28  *tokenizeString(
31b26 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
31b27 6e 74 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a 20 20  nt *pnToken){.  
31b28 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a  int nToken = 0;.
31b29 20 20 46 74 73 54 6f 6b 65 6e 20 2a 61 54 6f 6b    FtsToken *aTok
31b2a 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  en = sqlite3_mal
31b2b 6c 6f 63 28 20 73 74 72 6c 65 6e 28 7a 29 20 2a  loc( strlen(z) *
31b2c 20 73 69 7a 65 6f 66 28 61 54 6f 6b 65 6e 5b 30   sizeof(aToken[0
31b2d 5d 29 20 29 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  ]) );.  int n = 
31b2e 31 3b 0a 20 20 69 6e 74 20 65 2c 20 69 3b 0a 20  1;.  int e, i;. 
31b2f 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 20 3d   int totalSize =
31b30 20 30 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 54   0;.  char **azT
31b31 6f 6b 65 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 43  oken;.  char *zC
31b32 6f 70 79 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e  opy;.  while( n>
31b33 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73  0 ){.    n = fts
31b34 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 65 29 3b  GetToken(z, &e);
31b35 0a 20 20 20 20 69 66 28 20 65 21 3d 54 4f 4b 45  .    if( e!=TOKE
31b36 4e 5f 53 50 41 43 45 20 29 7b 0a 20 20 20 20 20  N_SPACE ){.     
31b37 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e   aToken[nToken].
31b38 7a 20 3d 20 7a 3b 0a 20 20 20 20 20 20 61 54 6f  z = z;.      aTo
31b39 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e 6e 20 3d 20  ken[nToken].n = 
31b3a 6e 3b 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 2b  n;.      nToken+
31b3b 2b 3b 0a 20 20 20 20 20 20 74 6f 74 61 6c 53 69  +;.      totalSi
31b3c 7a 65 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20 7d  ze += n+1;.    }
31b3d 0a 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 7d  .    z += n;.  }
31b3e 0a 20 20 61 7a 54 6f 6b 65 6e 20 3d 20 28 63 68  .  azToken = (ch
31b3f 61 72 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar**)sqlite3_mal
31b40 6c 6f 63 28 20 6e 54 6f 6b 65 6e 2a 73 69 7a 65  loc( nToken*size
31b41 6f 66 28 63 68 61 72 2a 29 20 2b 20 74 6f 74 61  of(char*) + tota
31b42 6c 53 69 7a 65 20 29 3b 0a 20 20 7a 43 6f 70 79  lSize );.  zCopy
31b43 20 3d 20 28 63 68 61 72 2a 29 26 61 7a 54 6f 6b   = (char*)&azTok
31b44 65 6e 5b 6e 54 6f 6b 65 6e 5d 3b 0a 20 20 6e 54  en[nToken];.  nT
31b45 6f 6b 65 6e 2d 2d 3b 0a 20 20 66 6f 72 28 69 3d  oken--;.  for(i=
31b46 30 3b 20 69 3c 6e 54 6f 6b 65 6e 3b 20 69 2b 2b  0; i<nToken; i++
31b47 29 7b 0a 20 20 20 20 61 7a 54 6f 6b 65 6e 5b 69  ){.    azToken[i
31b48 5d 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20 6e  ] = zCopy;.    n
31b49 20 3d 20 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 3b 0a   = aToken[i].n;.
31b4a 20 20 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79      memcpy(zCopy
31b4b 2c 20 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 2c 20 6e  , aToken[i].z, n
31b4c 29 3b 0a 20 20 20 20 7a 43 6f 70 79 5b 6e 5d 20  );.    zCopy[n] 
31b4d 3d 20 30 3b 0a 20 20 20 20 7a 43 6f 70 79 20 2b  = 0;.    zCopy +
31b4e 3d 20 6e 2b 31 3b 0a 20 20 7d 0a 20 20 61 7a 54  = n+1;.  }.  azT
31b4f 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 20 3d 20 30  oken[nToken] = 0
31b50 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
31b51 28 61 54 6f 6b 65 6e 29 3b 0a 20 20 2a 70 6e 54  (aToken);.  *pnT
31b52 6f 6b 65 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20  oken = nToken;. 
31b53 20 72 65 74 75 72 6e 20 61 7a 54 6f 6b 65 6e 3b   return azToken;
31b54 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
31b55 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71  t an SQL-style q
31b56 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74  uoted string int
31b57 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e  o a normal strin
31b58 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a  g by removing.**
31b59 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72 61   the quote chara
31b5a 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e 76  cters.  The conv
31b5b 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69  ersion is done i
31b5c 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68 65  n-place.  If the
31b5d 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 6e  .** input does n
31b5e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
31b5f 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c  quote character,
31b60 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
31b61 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70  ne.** is a no-op
31b62 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 73  ..**.** Examples
31b63 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 61 62 63  :.**.**     "abc
31b64 22 20 20 20 62 65 63 6f 6d 65 73 20 20 20 61 62  "   becomes   ab
31b65 63 0a 2a 2a 20 20 20 20 20 27 78 79 7a 27 20 20  c.**     'xyz'  
31b66 20 62 65 63 6f 6d 65 73 20 20 20 78 79 7a 0a 2a   becomes   xyz.*
31b67 2a 20 20 20 20 20 5b 70 71 72 5d 20 20 20 62 65  *     [pqr]   be
31b68 63 6f 6d 65 73 20 20 20 70 71 72 0a 2a 2a 20 20  comes   pqr.**  
31b69 20 20 20 60 6d 6e 6f 60 20 20 20 62 65 63 6f 6d     `mno`   becom
31b6a 65 73 20 20 20 6d 6e 6f 0a 2a 2f 0a 73 74 61 74  es   mno.*/.stat
31b6b 69 63 20 76 6f 69 64 20 64 65 71 75 6f 74 65 53  ic void dequoteS
31b6c 74 72 69 6e 67 28 63 68 61 72 20 2a 7a 29 7b 0a  tring(char *z){.
31b6d 20 20 69 6e 74 20 71 75 6f 74 65 3b 0a 20 20 69    int quote;.  i
31b6e 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a  nt i, j;.  if( z
31b6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
31b70 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20  quote = z[0];.  
31b71 73 77 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b  switch( quote ){
31b72 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20  .    case '\'': 
31b73 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
31b74 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20   '"':   break;. 
31b75 20 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 62     case '`':   b
31b76 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20  reak;           
31b77 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51       /* For MySQ
31b78 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  L compatibility 
31b79 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a  */.    case '[':
31b7a 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20     quote = ']'; 
31b7b 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20   break;  /* For 
31b7c 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d  MS SqlServer com
31b7d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20  patibility */.  
31b7e 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65    default:    re
31b7f 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
31b80 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20  i=1, j=0; z[i]; 
31b81 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b  i++){.    if( z[
31b82 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20  i]==quote ){.   
31b83 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71     if( z[i+1]==q
31b84 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  uote ){.        
31b85 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a  z[j++] = quote;.
31b86 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
31b87 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
31b88 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20    z[j++] = 0;.  
31b89 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31b8a 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
31b8b 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
31b8c 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  [i];.    }.  }.}
31b8d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ../*.** The inpu
31b8e 74 20 61 7a 49 6e 20 69 73 20 61 20 4e 55 4c 4c  t azIn is a NULL
31b8f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6c 69 73 74  -terminated list
31b90 20 6f 66 20 74 6f 6b 65 6e 73 2e 20 20 52 65 6d   of tokens.  Rem
31b91 6f 76 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  ove the first.**
31b92 20 74 6f 6b 65 6e 20 61 6e 64 20 61 6c 6c 20 70   token and all p
31b93 75 6e 63 74 75 61 74 69 6f 6e 20 74 6f 6b 65 6e  unctuation token
31b94 73 2e 20 20 52 65 6d 6f 76 65 20 74 68 65 20 71  s.  Remove the q
31b95 75 6f 74 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 72  uotes from.** ar
31b96 6f 75 6e 64 20 73 74 72 69 6e 67 20 6c 69 74 65  ound string lite
31b97 72 61 6c 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 0a 2a  ral tokens..**.*
31b98 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  * Example:.**.**
31b99 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20       input:     
31b9a 20 74 6f 6b 65 6e 69 7a 65 20 63 68 69 6e 65 73   tokenize chines
31b9b 65 20 28 20 27 73 69 6d 70 6c 69 66 65 64 27 20  e ( 'simplifed' 
31b9c 2c 20 27 6d 69 78 65 64 27 20 29 0a 2a 2a 20 20  , 'mixed' ).**  
31b9d 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 20 63     output:     c
31b9e 68 69 6e 65 73 65 20 73 69 6d 70 6c 69 66 65 64  hinese simplifed
31b9f 20 6d 69 78 65 64 0a 2a 2a 0a 2a 2a 20 41 6e 6f   mixed.**.** Ano
31ba0 74 68 65 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a  ther example:.**
31ba1 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20  .**     input:  
31ba2 20 20 20 20 64 65 6c 69 6d 69 74 65 72 73 20 28      delimiters (
31ba3 20 27 5b 27 20 2c 20 27 5d 27 20 2c 20 27 2e 2e   '[' , ']' , '..
31ba4 2e 27 20 29 0a 2a 2a 20 20 20 20 20 6f 75 74 70  .' ).**     outp
31ba5 75 74 3a 20 20 20 20 20 5b 20 5d 20 2e 2e 2e 0a  ut:     [ ] ....
31ba6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
31ba7 6f 6b 65 6e 4c 69 73 74 54 6f 49 64 4c 69 73 74  okenListToIdList
31ba8 28 63 68 61 72 20 2a 2a 61 7a 49 6e 29 7b 0a 20  (char **azIn){. 
31ba9 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28   int i, j;.  if(
31baa 20 61 7a 49 6e 20 29 7b 0a 20 20 20 20 66 6f 72   azIn ){.    for
31bab 28 69 3d 30 2c 20 6a 3d 2d 31 3b 20 61 7a 49 6e  (i=0, j=-1; azIn
31bac 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  [i]; i++){.     
31bad 20 69 66 28 20 73 61 66 65 5f 69 73 61 6c 6e 75   if( safe_isalnu
31bae 6d 28 61 7a 49 6e 5b 69 5d 5b 30 5d 29 20 7c 7c  m(azIn[i][0]) ||
31baf 20 61 7a 49 6e 5b 69 5d 5b 31 5d 20 29 7b 0a 20   azIn[i][1] ){. 
31bb0 20 20 20 20 20 20 20 64 65 71 75 6f 74 65 53 74         dequoteSt
31bb1 72 69 6e 67 28 61 7a 49 6e 5b 69 5d 29 3b 0a 20  ring(azIn[i]);. 
31bb2 20 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20         if( j>=0 
31bb3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 7a 49  ){.          azI
31bb4 6e 5b 6a 5d 20 3d 20 61 7a 49 6e 5b 69 5d 3b 0a  n[j] = azIn[i];.
31bb5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
31bb6 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20    j++;.      }. 
31bb7 20 20 20 7d 0a 20 20 20 20 61 7a 49 6e 5b 6a 5d     }.    azIn[j]
31bb8 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a   = 0;.  }.}.../*
31bb9 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72  .** Find the fir
31bba 73 74 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20  st alphanumeric 
31bbb 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 74 72  token in the str
31bbc 69 6e 67 20 7a 49 6e 2e 20 20 4e 75 6c 6c 2d 74  ing zIn.  Null-t
31bbd 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 69 73  erminate.** this
31bbe 20 74 6f 6b 65 6e 2e 20 20 52 65 6d 6f 76 65 20   token.  Remove 
31bbf 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61  any quotation ma
31bc0 72 6b 73 2e 20 20 41 6e 64 20 72 65 74 75 72 6e  rks.  And return
31bc1 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
31bc2 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a   the result..*/.
31bc3 73 74 61 74 69 63 20 63 68 61 72 20 2a 66 69 72  static char *fir
31bc4 73 74 54 6f 6b 65 6e 28 63 68 61 72 20 2a 7a 49  stToken(char *zI
31bc5 6e 2c 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  n, char **pzTail
31bc6 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 74 74 79 70  ){.  int n, ttyp
31bc7 65 3b 0a 20 20 77 68 69 6c 65 28 31 29 7b 0a 20  e;.  while(1){. 
31bc8 20 20 20 6e 20 3d 20 66 74 73 47 65 74 54 6f 6b     n = ftsGetTok
31bc9 65 6e 28 7a 49 6e 2c 20 26 74 74 79 70 65 29 3b  en(zIn, &ttype);
31bca 0a 20 20 20 20 69 66 28 20 74 74 79 70 65 3d 3d  .    if( ttype==
31bcb 54 4f 4b 45 4e 5f 53 50 41 43 45 20 29 7b 0a 20  TOKEN_SPACE ){. 
31bcc 20 20 20 20 20 7a 49 6e 20 2b 3d 20 6e 3b 0a 20       zIn += n;. 
31bcd 20 20 20 7d 65 6c 73 65 20 69 66 28 20 74 74 79     }else if( tty
31bce 70 65 3d 3d 54 4f 4b 45 4e 5f 45 4f 46 20 29 7b  pe==TOKEN_EOF ){
31bcf 0a 20 20 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d  .      *pzTail =
31bd0 20 7a 49 6e 3b 0a 20 20 20 20 20 20 72 65 74 75   zIn;.      retu
31bd1 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rn 0;.    }else{
31bd2 0a 20 20 20 20 20 20 7a 49 6e 5b 6e 5d 20 3d 20  .      zIn[n] = 
31bd3 30 3b 0a 20 20 20 20 20 20 2a 70 7a 54 61 69 6c  0;.      *pzTail
31bd4 20 3d 20 26 7a 49 6e 5b 31 5d 3b 0a 20 20 20 20   = &zIn[1];.    
31bd5 20 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28    dequoteString(
31bd6 7a 49 6e 29 3b 0a 20 20 20 20 20 20 72 65 74 75  zIn);.      retu
31bd7 72 6e 20 7a 49 6e 3b 0a 20 20 20 20 7d 0a 20 20  rn zIn;.    }.  
31bd8 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44  }.  /*NOTREACHED
31bd9 2a 2f 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  */.}../* Return 
31bda 74 72 75 65 20 69 66 2e 2e 2e 0a 2a 2a 0a 2a 2a  true if....**.**
31bdb 20 20 20 2a 20 20 73 20 62 65 67 69 6e 73 20 77     *  s begins w
31bdc 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 20 74  ith the string t
31bdd 2c 20 69 67 6e 6f 72 69 6e 67 20 63 61 73 65 0a  , ignoring case.
31bde 2a 2a 20 20 20 2a 20 20 73 20 69 73 20 6c 6f 6e  **   *  s is lon
31bdf 67 65 72 20 74 68 61 6e 20 74 0a 2a 2a 20 20 20  ger than t.**   
31be0 2a 20 20 54 68 65 20 66 69 72 73 74 20 63 68 61  *  The first cha
31be1 72 61 63 74 65 72 20 6f 66 20 73 20 62 65 79 6f  racter of s beyo
31be2 6e 64 20 74 20 69 73 20 6e 6f 74 20 61 20 61 6c  nd t is not a al
31be3 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a 20 0a 2a  phanumeric.** .*
31be4 2a 20 49 67 6e 6f 72 65 20 6c 65 61 64 69 6e 67  * Ignore leading
31be5 20 73 70 61 63 65 20 69 6e 20 2a 73 2e 0a 2a 2a   space in *s..**
31be6 0a 2a 2a 20 54 6f 20 70 75 74 20 69 74 20 61 6e  .** To put it an
31be7 6f 74 68 65 72 20 77 61 79 2c 20 72 65 74 75 72  other way, retur
31be8 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 69  n true if the fi
31be9 72 73 74 20 74 6f 6b 65 6e 20 6f 66 0a 2a 2a 20  rst token of.** 
31bea 73 5b 5d 20 69 73 20 74 5b 5d 2e 0a 2a 2f 0a 73  s[] is t[]..*/.s
31beb 74 61 74 69 63 20 69 6e 74 20 73 74 61 72 74 73  tatic int starts
31bec 57 69 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20  With(const char 
31bed 2a 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *s, const char *
31bee 74 29 7b 0a 20 20 77 68 69 6c 65 28 20 73 61 66  t){.  while( saf
31bef 65 5f 69 73 73 70 61 63 65 28 2a 73 29 20 29 7b  e_isspace(*s) ){
31bf0 20 73 2b 2b 3b 20 7d 0a 20 20 77 68 69 6c 65 28   s++; }.  while(
31bf1 20 2a 74 20 29 7b 0a 20 20 20 20 69 66 28 20 73   *t ){.    if( s
31bf2 61 66 65 5f 74 6f 6c 6f 77 65 72 28 2a 73 2b 2b  afe_tolower(*s++
31bf3 29 21 3d 73 61 66 65 5f 74 6f 6c 6f 77 65 72 28  )!=safe_tolower(
31bf4 2a 74 2b 2b 29 20 29 20 72 65 74 75 72 6e 20 30  *t++) ) return 0
31bf5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a  ;.  }.  return *
31bf6 73 21 3d 27 5f 27 20 26 26 20 21 73 61 66 65 5f  s!='_' && !safe_
31bf7 69 73 61 6c 6e 75 6d 28 2a 73 29 3b 0a 7d 0a 0a  isalnum(*s);.}..
31bf8 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
31bf9 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
31bfa 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ure defines the 
31bfb 22 73 70 65 63 22 20 6f 66 20 61 0a 2a 2a 20 66  "spec" of a.** f
31bfc 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 2e 20  ull text index. 
31bfd 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
31bfe 69 73 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20  is populated by 
31bff 70 61 72 73 65 53 70 65 63 0a 2a 2a 20 61 6e 64  parseSpec.** and
31c00 20 75 73 65 20 62 79 20 66 75 6c 6c 74 65 78 74   use by fulltext
31c01 43 6f 6e 6e 65 63 74 20 61 6e 64 20 66 75 6c 6c  Connect and full
31c02 74 65 78 74 43 72 65 61 74 65 2e 0a 2a 2f 0a 74  textCreate..*/.t
31c03 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
31c04 62 6c 65 53 70 65 63 20 7b 0a 20 20 63 6f 6e 73  bleSpec {.  cons
31c05 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
31c06 20 20 20 20 20 2f 2a 20 4c 6f 67 69 63 61 6c 20       /* Logical 
31c07 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  database name */
31c08 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
31c09 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e  Name;       /* N
31c0a 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d  ame of the full-
31c0b 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20  text index */.  
31c0c 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
31c0d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
31c0e 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f  er of columns to
31c0f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20   be indexed */. 
31c10 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 75 6d 6e   char **azColumn
31c11 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69  ;         /* Ori
31c12 67 69 6e 61 6c 20 6e 61 6d 65 73 20 6f 66 20 63  ginal names of c
31c13 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64  olumns to be ind
31c14 65 78 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  exed */.  char *
31c15 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  *azContentColumn
31c16 3b 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d  ;  /* Column nam
31c17 65 73 20 66 6f 72 20 25 5f 63 6f 6e 74 65 6e 74  es for %_content
31c18 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 54   */.  char **azT
31c19 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f  okenizer;      /
31c1a 2a 20 4e 61 6d 65 20 6f 66 20 74 6f 6b 65 6e 69  * Name of tokeni
31c1b 7a 65 72 20 61 6e 64 20 69 74 73 20 61 72 67 75  zer and its argu
31c1c 6d 65 6e 74 73 20 2a 2f 0a 7d 20 54 61 62 6c 65  ments */.} Table
31c1d 53 70 65 63 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  Spec;../*.** Rec
31c1e 6c 61 69 6d 20 61 6c 6c 20 6f 66 20 74 68 65 20  laim all of the 
31c1f 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61  memory used by a
31c20 20 54 61 62 6c 65 53 70 65 63 0a 2a 2f 0a 73 74   TableSpec.*/.st
31c21 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 54  atic void clearT
31c22 61 62 6c 65 53 70 65 63 28 54 61 62 6c 65 53 70  ableSpec(TableSp
31c23 65 63 20 2a 70 29 20 7b 0a 20 20 73 71 6c 69 74  ec *p) {.  sqlit
31c24 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 43 6f 6c  e3_free(p->azCol
31c25 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  umn);.  sqlite3_
31c26 66 72 65 65 28 70 2d 3e 61 7a 43 6f 6e 74 65 6e  free(p->azConten
31c27 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69  tColumn);.  sqli
31c28 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 54 6f  te3_free(p->azTo
31c29 6b 65 6e 69 7a 65 72 29 3b 0a 7d 0a 0a 2f 2a 20  kenizer);.}../* 
31c2a 50 61 72 73 65 20 61 20 43 52 45 41 54 45 20 56  Parse a CREATE V
31c2b 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
31c2c 74 65 6d 65 6e 74 2c 20 77 68 69 63 68 20 6c 6f  tement, which lo
31c2d 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20  oks like this:. 
31c2e 2a 0a 20 2a 20 43 52 45 41 54 45 20 56 49 52 54  *. * CREATE VIRT
31c2f 55 41 4c 20 54 41 42 4c 45 20 65 6d 61 69 6c 0a  UAL TABLE email.
31c30 20 2a 20 20 20 20 20 20 20 20 55 53 49 4e 47 20   *        USING 
31c31 66 74 73 33 28 73 75 62 6a 65 63 74 2c 20 62 6f  fts3(subject, bo
31c32 64 79 2c 20 74 6f 6b 65 6e 69 7a 65 20 6d 79 74  dy, tokenize myt
31c33 6f 6b 65 6e 69 7a 65 72 28 6d 79 61 72 67 29 29  okenizer(myarg))
31c34 0a 20 2a 0a 20 2a 20 57 65 20 72 65 74 75 72 6e  . *. * We return
31c35 20 70 61 72 73 65 64 20 69 6e 66 6f 72 6d 61 74   parsed informat
31c36 69 6f 6e 20 69 6e 20 61 20 54 61 62 6c 65 53 70  ion in a TableSp
31c37 65 63 20 73 74 72 75 63 74 75 72 65 2e 0a 20 2a  ec structure.. *
31c38 20 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   . */.static int
31c39 20 70 61 72 73 65 53 70 65 63 28 54 61 62 6c 65   parseSpec(Table
31c3a 53 70 65 63 20 2a 70 53 70 65 63 2c 20 69 6e 74  Spec *pSpec, int
31c3b 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
31c3c 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20  r *const*argv,. 
31c3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31c3e 20 20 20 20 63 68 61 72 2a 2a 70 7a 45 72 72 29      char**pzErr)
31c3f 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  {.  int i, n;.  
31c40 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 75 6d 6d 79  char *z, *zDummy
31c41 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 41 72 67  ;.  char **azArg
31c42 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
31c43 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 20  zTokenizer = 0; 
31c44 20 20 20 2f 2a 20 61 72 67 76 5b 5d 20 65 6e 74     /* argv[] ent
31c45 72 79 20 64 65 73 63 72 69 62 69 6e 67 20 74 68  ry describing th
31c46 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 0a  e tokenizer */..
31c47 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3e 3d    assert( argc>=
31c48 33 20 29 3b 0a 20 20 2f 2a 20 43 75 72 72 65 6e  3 );.  /* Curren
31c49 74 20 69 6e 74 65 72 66 61 63 65 3a 0a 20 20 2a  t interface:.  *
31c4a 2a 20 61 72 67 76 5b 30 5d 20 2d 20 6d 6f 64 75  * argv[0] - modu
31c4b 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67  le name.  ** arg
31c4c 76 5b 31 5d 20 2d 20 64 61 74 61 62 61 73 65 20  v[1] - database 
31c4d 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 32  name.  ** argv[2
31c4e 5d 20 2d 20 74 61 62 6c 65 20 6e 61 6d 65 0a 20  ] - table name. 
31c4f 20 2a 2a 20 61 72 67 76 5b 33 2e 2e 5d 20 2d 20   ** argv[3..] - 
31c50 63 6f 6c 75 6d 6e 73 2c 20 6f 70 74 69 6f 6e 61  columns, optiona
31c51 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  lly followed by 
31c52 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65 63 69 66  tokenizer specif
31c53 69 63 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  ication.  **    
31c54 20 20 20 20 20 20 20 20 20 61 6e 64 20 73 6e 69           and sni
31c55 70 70 65 74 20 64 65 6c 69 6d 69 74 65 72 73 20  ppet delimiters 
31c56 73 70 65 63 69 66 69 63 61 74 69 6f 6e 2e 0a 20  specification.. 
31c57 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61   */..  /* Make a
31c58 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6d   copy of the com
31c59 70 6c 65 74 65 20 61 72 67 76 5b 5d 5b 5d 20 61  plete argv[][] a
31c5a 72 72 61 79 20 69 6e 20 61 20 73 69 6e 67 6c 65  rray in a single
31c5b 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 20 20 2a   allocation..  *
31c5c 2a 20 54 68 65 20 61 72 67 76 5b 5d 5b 5d 20 61  * The argv[][] a
31c5d 72 72 61 79 20 69 73 20 72 65 61 64 2d 6f 6e 6c  rray is read-onl
31c5e 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 2e  y and transient.
31c5f 20 20 57 65 20 63 61 6e 20 77 72 69 74 65 20 74    We can write t
31c60 6f 20 74 68 65 0a 20 20 2a 2a 20 63 6f 70 79 20  o the.  ** copy 
31c61 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 64 69  in order to modi
31c62 66 79 20 74 68 69 6e 67 73 20 61 6e 64 20 74 68  fy things and th
31c63 65 20 63 6f 70 79 20 69 73 20 70 65 72 73 69 73  e copy is persis
31c64 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 43 4c 45  tent..  */.  CLE
31c65 41 52 28 70 53 70 65 63 29 3b 0a 20 20 66 6f 72  AR(pSpec);.  for
31c66 28 69 3d 6e 3d 30 3b 20 69 3c 61 72 67 63 3b 20  (i=n=0; i<argc; 
31c67 69 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 73  i++){.    n += s
31c68 74 72 6c 65 6e 28 61 72 67 76 5b 69 5d 29 20 2b  trlen(argv[i]) +
31c69 20 31 3b 0a 20 20 7d 0a 20 20 61 7a 41 72 67 20   1;.  }.  azArg 
31c6a 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
31c6b 28 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  ( sizeof(char*)*
31c6c 61 72 67 63 20 2b 20 6e 20 29 3b 0a 20 20 69 66  argc + n );.  if
31c6d 28 20 61 7a 41 72 67 3d 3d 30 20 29 7b 0a 20 20  ( azArg==0 ){.  
31c6e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
31c6f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d  NOMEM;.  }.  z =
31c70 20 28 63 68 61 72 2a 29 26 61 7a 41 72 67 5b 61   (char*)&azArg[a
31c71 72 67 63 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  rgc];.  for(i=0;
31c72 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20   i<argc; i++){. 
31c73 20 20 20 61 7a 41 72 67 5b 69 5d 20 3d 20 7a 3b     azArg[i] = z;
31c74 0a 20 20 20 20 73 74 72 63 70 79 28 7a 2c 20 61  .    strcpy(z, a
31c75 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 7a 20 2b  rgv[i]);.    z +
31c76 3d 20 73 74 72 6c 65 6e 28 7a 29 2b 31 3b 0a 20  = strlen(z)+1;. 
31c77 20 7d 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66   }..  /* Identif
31c78 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  y the column nam
31c79 65 73 20 61 6e 64 20 74 68 65 20 74 6f 6b 65 6e  es and the token
31c7a 69 7a 65 72 20 61 6e 64 20 64 65 6c 69 6d 69 74  izer and delimit
31c7b 65 72 20 61 72 67 75 6d 65 6e 74 73 0a 20 20 2a  er arguments.  *
31c7c 2a 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d 5b  * in the argv[][
31c7d 5d 20 61 72 72 61 79 2e 0a 20 20 2a 2f 0a 20 20  ] array..  */.  
31c7e 70 53 70 65 63 2d 3e 7a 44 62 20 3d 20 61 7a 41  pSpec->zDb = azA
31c7f 72 67 5b 31 5d 3b 0a 20 20 70 53 70 65 63 2d 3e  rg[1];.  pSpec->
31c80 7a 4e 61 6d 65 20 3d 20 61 7a 41 72 67 5b 32 5d  zName = azArg[2]
31c81 3b 0a 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  ;.  pSpec->nColu
31c82 6d 6e 20 3d 20 30 3b 0a 20 20 70 53 70 65 63 2d  mn = 0;.  pSpec-
31c83 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 61 7a 41 72  >azColumn = azAr
31c84 67 3b 0a 20 20 7a 54 6f 6b 65 6e 69 7a 65 72 20  g;.  zTokenizer 
31c85 3d 20 22 74 6f 6b 65 6e 69 7a 65 20 73 69 6d 70  = "tokenize simp
31c86 6c 65 22 3b 0a 20 20 66 6f 72 28 69 3d 33 3b 20  le";.  for(i=3; 
31c87 69 3c 61 72 67 63 3b 20 2b 2b 69 29 7b 0a 20 20  i<argc; ++i){.  
31c88 20 20 69 66 28 20 73 74 61 72 74 73 57 69 74 68    if( startsWith
31c89 28 61 7a 41 72 67 5b 69 5d 2c 22 74 6f 6b 65 6e  (azArg[i],"token
31c8a 69 7a 65 22 29 20 29 7b 0a 20 20 20 20 20 20 7a  ize") ){.      z
31c8b 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 61 7a 41 72  Tokenizer = azAr
31c8c 67 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  g[i];.    }else{
31c8d 0a 20 20 20 20 20 20 7a 20 3d 20 61 7a 41 72 67  .      z = azArg
31c8e 5b 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 5d  [pSpec->nColumn]
31c8f 20 3d 20 66 69 72 73 74 54 6f 6b 65 6e 28 61 7a   = firstToken(az
31c90 41 72 67 5b 69 5d 2c 20 26 7a 44 75 6d 6d 79 29  Arg[i], &zDummy)
31c91 3b 0a 20 20 20 20 20 20 70 53 70 65 63 2d 3e 6e  ;.      pSpec->n
31c92 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 7d 0a  Column++;.    }.
31c93 20 20 7d 0a 20 20 69 66 28 20 70 53 70 65 63 2d    }.  if( pSpec-
31c94 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20  >nColumn==0 ){. 
31c95 20 20 20 61 7a 41 72 67 5b 30 5d 20 3d 20 22 63     azArg[0] = "c
31c96 6f 6e 74 65 6e 74 22 3b 0a 20 20 20 20 70 53 70  ontent";.    pSp
31c97 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b  ec->nColumn = 1;
31c98 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
31c99 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 6c 69  Construct the li
31c9a 73 74 20 6f 66 20 63 6f 6e 74 65 6e 74 20 63 6f  st of content co
31c9b 6c 75 6d 6e 20 6e 61 6d 65 73 2e 0a 20 20 2a 2a  lumn names..  **
31c9c 0a 20 20 2a 2a 20 45 61 63 68 20 63 6f 6e 74 65  .  ** Each conte
31c9d 6e 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77  nt column name w
31c9e 69 6c 6c 20 62 65 20 6f 66 20 74 68 65 20 66 6f  ill be of the fo
31c9f 72 6d 20 63 4e 4e 41 41 41 41 0a 20 20 2a 2a 20  rm cNNAAAA.  ** 
31ca0 77 68 65 72 65 20 4e 4e 20 69 73 20 74 68 65 20  where NN is the 
31ca1 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 61 6e  column number an
31ca2 64 20 41 41 41 41 20 69 73 20 74 68 65 20 73 61  d AAAA is the sa
31ca3 6e 69 74 69 7a 65 64 0a 20 20 2a 2a 20 63 6f 6c  nitized.  ** col
31ca4 75 6d 6e 20 6e 61 6d 65 2e 20 20 22 73 61 6e 69  umn name.  "sani
31ca5 74 69 7a 65 64 22 20 6d 65 61 6e 73 20 74 68 61  tized" means tha
31ca6 74 20 73 70 65 63 69 61 6c 20 63 68 61 72 61 63  t special charac
31ca7 74 65 72 73 20 61 72 65 0a 20 20 2a 2a 20 63 6f  ters are.  ** co
31ca8 6e 76 65 72 74 65 64 20 74 6f 20 22 5f 22 2e 20  nverted to "_". 
31ca9 20 54 68 65 20 63 4e 4e 20 70 72 65 66 69 78 20   The cNN prefix 
31caa 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20  guarantees that 
31cab 61 6c 6c 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20  all column.  ** 
31cac 6e 61 6d 65 73 20 61 72 65 20 75 6e 69 71 75 65  names are unique
31cad 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
31cae 41 41 41 41 20 73 75 66 66 69 78 20 69 73 20 6e  AAAA suffix is n
31caf 6f 74 20 73 74 72 69 63 74 6c 79 20 6e 65 63 65  ot strictly nece
31cb0 73 73 61 72 79 2e 20 20 49 74 20 69 73 20 69 6e  ssary.  It is in
31cb1 63 6c 75 64 65 64 0a 20 20 2a 2a 20 66 6f 72 20  cluded.  ** for 
31cb2 74 68 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  the convenience 
31cb3 6f 66 20 70 65 6f 70 6c 65 20 77 68 6f 20 6d 69  of people who mi
31cb4 67 68 74 20 65 78 61 6d 69 6e 65 20 74 68 65 20  ght examine the 
31cb5 67 65 6e 65 72 61 74 65 64 0a 20 20 2a 2a 20 25  generated.  ** %
31cb6 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 61  _content table a
31cb7 6e 64 20 77 6f 6e 64 65 72 20 77 68 61 74 20 74  nd wonder what t
31cb8 68 65 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75  he columns are u
31cb9 73 65 64 20 66 6f 72 2e 0a 20 20 2a 2f 0a 20 20  sed for..  */.  
31cba 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74  pSpec->azContent
31cbb 43 6f 6c 75 6d 6e 20 3d 20 73 71 6c 69 74 65 33  Column = sqlite3
31cbc 5f 6d 61 6c 6c 6f 63 28 20 70 53 70 65 63 2d 3e  _malloc( pSpec->
31cbd 6e 43 6f 6c 75 6d 6e 20 2a 20 73 69 7a 65 6f 66  nColumn * sizeof
31cbe 28 63 68 61 72 20 2a 29 20 29 3b 0a 20 20 69 66  (char *) );.  if
31cbf 28 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65  ( pSpec->azConte
31cc0 6e 74 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20  ntColumn==0 ){. 
31cc1 20 20 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65     clearTableSpe
31cc2 63 28 70 53 70 65 63 29 3b 0a 20 20 20 20 72 65  c(pSpec);.    re
31cc3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
31cc4 4d 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  M;.  }.  for(i=0
31cc5 3b 20 69 3c 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  ; i<pSpec->nColu
31cc6 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68  mn; i++){.    ch
31cc7 61 72 20 2a 70 3b 0a 20 20 20 20 70 53 70 65 63  ar *p;.    pSpec
31cc8 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
31cc9 6e 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 5f 6d  n[i] = sqlite3_m
31cca 70 72 69 6e 74 66 28 22 63 25 64 25 73 22 2c 20  printf("c%d%s", 
31ccb 69 2c 20 61 7a 41 72 67 5b 69 5d 29 3b 0a 20 20  i, azArg[i]);.  
31ccc 20 20 66 6f 72 20 28 70 20 3d 20 70 53 70 65 63    for (p = pSpec
31ccd 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
31cce 6e 5b 69 5d 3b 20 2a 70 20 3b 20 2b 2b 70 29 20  n[i]; *p ; ++p) 
31ccf 7b 0a 20 20 20 20 20 20 69 66 28 20 21 73 61 66  {.      if( !saf
31cd0 65 5f 69 73 61 6c 6e 75 6d 28 2a 70 29 20 29 20  e_isalnum(*p) ) 
31cd1 2a 70 20 3d 20 27 5f 27 3b 0a 20 20 20 20 7d 0a  *p = '_';.    }.
31cd2 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50    }..  /*.  ** P
31cd3 61 72 73 65 20 74 68 65 20 74 6f 6b 65 6e 69 7a  arse the tokeniz
31cd4 65 72 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e  er specification
31cd5 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20   string..  */.  
31cd6 70 53 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a  pSpec->azTokeniz
31cd7 65 72 20 3d 20 74 6f 6b 65 6e 69 7a 65 53 74 72  er = tokenizeStr
31cd8 69 6e 67 28 7a 54 6f 6b 65 6e 69 7a 65 72 2c 20  ing(zTokenizer, 
31cd9 26 6e 29 3b 0a 20 20 74 6f 6b 65 6e 4c 69 73 74  &n);.  tokenList
31cda 54 6f 49 64 4c 69 73 74 28 70 53 70 65 63 2d 3e  ToIdList(pSpec->
31cdb 61 7a 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20  azTokenizer);.. 
31cdc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
31cdd 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  K;.}../*.** Gene
31cde 72 61 74 65 20 61 20 43 52 45 41 54 45 20 54 41  rate a CREATE TA
31cdf 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68  BLE statement th
31ce0 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
31ce1 20 73 63 68 65 6d 61 20 6f 66 0a 2a 2a 20 74 68   schema of.** th
31ce2 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
31ce3 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
31ce4 65 72 20 74 6f 20 74 68 69 73 20 73 63 68 65 6d  er to this schem
31ce5 61 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  a string..**.** 
31ce6 53 70 61 63 65 20 69 73 20 6f 62 74 61 69 6e 65  Space is obtaine
31ce7 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
31ce8 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 68 6f  printf() and sho
31ce9 75 6c 64 20 62 65 20 66 72 65 65 64 0a 2a 2a 20  uld be freed.** 
31cea 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72  using sqlite3_fr
31ceb 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ee()..*/.static 
31cec 63 68 61 72 20 2a 66 75 6c 6c 74 65 78 74 53 63  char *fulltextSc
31ced 68 65 6d 61 28 0a 20 20 69 6e 74 20 6e 43 6f 6c  hema(.  int nCol
31cee 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  umn,            
31cef 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
31cf0 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  of columns */.  
31cf1 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
31cf2 74 2a 20 61 7a 43 6f 6c 75 6d 6e 2c 20 20 2f 2a  t* azColumn,  /*
31cf3 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   List of columns
31cf4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
31cf5 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 20 20 20   *zTableName    
31cf6 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
31cf7 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  he table */.){. 
31cf8 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a   int i;.  char *
31cf9 7a 53 63 68 65 6d 61 2c 20 2a 7a 4e 65 78 74 3b  zSchema, *zNext;
31cfa 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
31cfb 53 65 70 20 3d 20 22 28 22 3b 0a 20 20 7a 53 63  Sep = "(";.  zSc
31cfc 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d  hema = sqlite3_m
31cfd 70 72 69 6e 74 66 28 22 43 52 45 41 54 45 20 54  printf("CREATE T
31cfe 41 42 4c 45 20 78 22 29 3b 0a 20 20 66 6f 72 28  ABLE x");.  for(
31cff 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20  i=0; i<nColumn; 
31d00 69 2b 2b 29 7b 0a 20 20 20 20 7a 4e 65 78 74 20  i++){.    zNext 
31d01 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
31d02 66 28 22 25 73 25 73 25 51 22 2c 20 7a 53 63 68  f("%s%s%Q", zSch
31d03 65 6d 61 2c 20 7a 53 65 70 2c 20 61 7a 43 6f 6c  ema, zSep, azCol
31d04 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 73 71 6c  umn[i]);.    sql
31d05 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68 65 6d  ite3_free(zSchem
31d06 61 29 3b 0a 20 20 20 20 7a 53 63 68 65 6d 61 20  a);.    zSchema 
31d07 3d 20 7a 4e 65 78 74 3b 0a 20 20 20 20 7a 53 65  = zNext;.    zSe
31d08 70 20 3d 20 22 2c 22 3b 0a 20 20 7d 0a 20 20 7a  p = ",";.  }.  z
31d09 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Next = sqlite3_m
31d0a 70 72 69 6e 74 66 28 22 25 73 2c 25 51 20 48 49  printf("%s,%Q HI
31d0b 44 44 45 4e 22 2c 20 7a 53 63 68 65 6d 61 2c 20  DDEN", zSchema, 
31d0c 7a 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 73  zTableName);.  s
31d0d 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68  qlite3_free(zSch
31d0e 65 6d 61 29 3b 0a 20 20 7a 53 63 68 65 6d 61 20  ema);.  zSchema 
31d0f 3d 20 7a 4e 65 78 74 3b 0a 20 20 7a 4e 65 78 74  = zNext;.  zNext
31d10 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
31d11 74 66 28 22 25 73 2c 64 6f 63 69 64 20 48 49 44  tf("%s,docid HID
31d12 44 45 4e 29 22 2c 20 7a 53 63 68 65 6d 61 29 3b  DEN)", zSchema);
31d13 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
31d14 7a 53 63 68 65 6d 61 29 3b 0a 20 20 72 65 74 75  zSchema);.  retu
31d15 72 6e 20 7a 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a  rn zNext;.}../*.
31d16 2a 2a 20 42 75 69 6c 64 20 61 20 6e 65 77 20 73  ** Build a new s
31d17 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
31d18 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20  cture that will 
31d19 64 65 73 63 72 69 62 65 20 74 68 65 0a 2a 2a 20  describe the.** 
31d1a 66 75 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 64  fulltext index d
31d1b 65 66 69 6e 65 64 20 62 79 20 73 70 65 63 2e 0a  efined by spec..
31d1c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
31d1d 6e 73 74 72 75 63 74 56 74 61 62 28 0a 20 20 73  nstructVtab(.  s
31d1e 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
31d1f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
31d20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
31d21 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
31d22 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c  fts3Hash *pHash,
31d23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
31d24 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  h table containi
31d25 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2a 2f  ng tokenizers */
31d26 0a 20 20 54 61 62 6c 65 53 70 65 63 20 2a 73 70  .  TableSpec *sp
31d27 65 63 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ec,          /* 
31d28 50 61 72 73 65 64 20 73 70 65 63 20 69 6e 66 6f  Parsed spec info
31d29 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 70 61 72  rmation from par
31d2a 73 65 53 70 65 63 28 29 20 2a 2f 0a 20 20 73 71  seSpec() */.  sq
31d2b 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56  lite3_vtab **ppV
31d2c 54 61 62 2c 20 20 20 20 2f 2a 20 57 72 69 74 65  Tab,    /* Write
31d2d 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 76   the resulting v
31d2e 74 61 62 20 73 74 72 75 63 74 75 72 65 20 68 65  tab structure he
31d2f 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  re */.  char **p
31d30 7a 45 72 72 20 20 20 20 20 20 20 20 20 20 20 20  zErr            
31d31 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 79 20 65    /* Write any e
31d32 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72  rror message her
31d33 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
31d34 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 75 6c  ;.  int n;.  ful
31d35 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20  ltext_vtab *v = 
31d36 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  0;.  const sqlit
31d37 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
31d38 75 6c 65 20 2a 6d 20 3d 20 4e 55 4c 4c 3b 0a 20  ule *m = NULL;. 
31d39 20 63 68 61 72 20 2a 73 63 68 65 6d 61 3b 0a 0a   char *schema;..
31d3a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
31d3b 6f 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ok;         /* N
31d3c 61 6d 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72  ame of tokenizer
31d3d 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73   to use for this
31d3e 20 66 74 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20   fts table */.  
31d3f 69 6e 74 20 6e 54 6f 6b 3b 20 20 20 20 20 20 20  int nTok;       
31d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
31d41 67 74 68 20 6f 66 20 7a 54 6f 6b 2c 20 69 6e 63  gth of zTok, inc
31d42 6c 75 64 69 6e 67 20 6e 75 6c 20 74 65 72 6d 69  luding nul termi
31d43 6e 61 74 6f 72 20 2a 2f 0a 0a 20 20 76 20 3d 20  nator */..  v = 
31d44 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
31d45 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ) sqlite3_malloc
31d46 28 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74  (sizeof(fulltext
31d47 5f 76 74 61 62 29 29 3b 0a 20 20 69 66 28 20 76  _vtab));.  if( v
31d48 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
31d49 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 43 4c 45  ITE_NOMEM;.  CLE
31d4a 41 52 28 76 29 3b 0a 20 20 2f 2a 20 73 71 6c 69  AR(v);.  /* sqli
31d4b 74 65 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69  te will initiali
31d4c 7a 65 20 76 2d 3e 62 61 73 65 20 2a 2f 0a 20 20  ze v->base */.  
31d4d 76 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 76 2d  v->db = db;.  v-
31d4e 3e 7a 44 62 20 3d 20 73 70 65 63 2d 3e 7a 44 62  >zDb = spec->zDb
31d4f 3b 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 64  ;       /* Freed
31d50 20 77 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20 69   when azColumn i
31d51 73 20 66 72 65 65 64 20 2a 2f 0a 20 20 76 2d 3e  s freed */.  v->
31d52 7a 4e 61 6d 65 20 3d 20 73 70 65 63 2d 3e 7a 4e  zName = spec->zN
31d53 61 6d 65 3b 20 20 20 2f 2a 20 46 72 65 65 64 20  ame;   /* Freed 
31d54 77 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20 69 73  when azColumn is
31d55 20 66 72 65 65 64 20 2a 2f 0a 20 20 76 2d 3e 6e   freed */.  v->n
31d56 43 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 6e  Column = spec->n
31d57 43 6f 6c 75 6d 6e 3b 0a 20 20 76 2d 3e 61 7a 43  Column;.  v->azC
31d58 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20 73  ontentColumn = s
31d59 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f  pec->azContentCo
31d5a 6c 75 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e 61 7a  lumn;.  spec->az
31d5b 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20  ContentColumn = 
31d5c 30 3b 0a 20 20 76 2d 3e 61 7a 43 6f 6c 75 6d 6e  0;.  v->azColumn
31d5d 20 3d 20 73 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d   = spec->azColum
31d5e 6e 3b 0a 20 20 73 70 65 63 2d 3e 61 7a 43 6f 6c  n;.  spec->azCol
31d5f 75 6d 6e 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  umn = 0;..  if( 
31d60 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  spec->azTokenize
31d61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  r==0 ){.    retu
31d62 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
31d63 0a 20 20 7d 0a 0a 20 20 7a 54 6f 6b 20 3d 20 73  .  }..  zTok = s
31d64 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72  pec->azTokenizer
31d65 5b 30 5d 3b 20 0a 20 20 69 66 28 20 21 7a 54 6f  [0]; .  if( !zTo
31d66 6b 20 29 7b 0a 20 20 20 20 7a 54 6f 6b 20 3d 20  k ){.    zTok = 
31d67 22 73 69 6d 70 6c 65 22 3b 0a 20 20 7d 0a 20 20  "simple";.  }.  
31d68 6e 54 6f 6b 20 3d 20 73 74 72 6c 65 6e 28 7a 54  nTok = strlen(zT
31d69 6f 6b 29 2b 31 3b 0a 0a 20 20 6d 20 3d 20 28 73  ok)+1;..  m = (s
31d6a 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
31d6b 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  _module *)sqlite
31d6c 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48  3Fts3HashFind(pH
31d6d 61 73 68 2c 20 7a 54 6f 6b 2c 20 6e 54 6f 6b 29  ash, zTok, nTok)
31d6e 3b 0a 20 20 69 66 28 20 21 6d 20 29 7b 0a 20 20  ;.  if( !m ){.  
31d6f 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
31d70 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
31d71 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
31d72 73 22 2c 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65  s", spec->azToke
31d73 6e 69 7a 65 72 5b 30 5d 29 3b 0a 20 20 20 20 72  nizer[0]);.    r
31d74 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
31d75 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 3b 0a  ;.    goto err;.
31d76 20 20 7d 0a 0a 20 20 66 6f 72 28 6e 3d 30 3b 20    }..  for(n=0; 
31d77 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  spec->azTokenize
31d78 72 5b 6e 5d 3b 20 6e 2b 2b 29 7b 7d 0a 20 20 69  r[n]; n++){}.  i
31d79 66 28 20 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d  f( n ){.    rc =
31d7a 20 6d 2d 3e 78 43 72 65 61 74 65 28 6e 2d 31 2c   m->xCreate(n-1,
31d7b 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e   (const char*con
31d7c 73 74 2a 29 26 73 70 65 63 2d 3e 61 7a 54 6f 6b  st*)&spec->azTok
31d7d 65 6e 69 7a 65 72 5b 31 5d 2c 0a 20 20 20 20 20  enizer[1],.     
31d7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
31d7f 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a  v->pTokenizer);.
31d80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
31d81 3d 20 6d 2d 3e 78 43 72 65 61 74 65 28 30 2c 20  = m->xCreate(0, 
31d82 30 2c 20 26 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65  0, &v->pTokenize
31d83 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  r);.  }.  if( rc
31d84 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
31d85 74 6f 20 65 72 72 3b 0a 20 20 76 2d 3e 70 54 6f  to err;.  v->pTo
31d86 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
31d87 20 3d 20 6d 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f   = m;..  /* TODO
31d88 3a 20 76 65 72 69 66 79 20 74 68 65 20 65 78 69  : verify the exi
31d89 73 74 65 6e 63 65 20 6f 66 20 62 61 63 6b 69 6e  stence of backin
31d8a 67 20 74 61 62 6c 65 73 20 66 6f 6f 5f 63 6f 6e  g tables foo_con
31d8b 74 65 6e 74 2c 20 66 6f 6f 5f 74 65 72 6d 20 2a  tent, foo_term *
31d8c 2f 0a 0a 20 20 73 63 68 65 6d 61 20 3d 20 66 75  /..  schema = fu
31d8d 6c 6c 74 65 78 74 53 63 68 65 6d 61 28 76 2d 3e  lltextSchema(v->
31d8e 6e 43 6f 6c 75 6d 6e 2c 20 28 63 6f 6e 73 74 20  nColumn, (const 
31d8f 63 68 61 72 2a 63 6f 6e 73 74 2a 29 76 2d 3e 61  char*const*)v->a
31d90 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20  zColumn,.       
31d91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31d92 20 20 20 73 70 65 63 2d 3e 7a 4e 61 6d 65 29 3b     spec->zName);
31d93 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
31d94 64 65 63 6c 61 72 65 5f 76 74 61 62 28 64 62 2c  declare_vtab(db,
31d95 20 73 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69   schema);.  sqli
31d96 74 65 33 5f 66 72 65 65 28 73 63 68 65 6d 61 29  te3_free(schema)
31d97 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
31d98 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
31d99 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 76 2d 3e 70  ;..  memset(v->p
31d9a 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
31d9b 74 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 76 2d  ts, 0, sizeof(v-
31d9c 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
31d9d 65 6e 74 73 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e  ents));..  /* In
31d9e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20  dicate that the 
31d9f 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 6c 69  buffer is not li
31da0 76 65 2e 20 2a 2f 0a 20 20 76 2d 3e 6e 50 65 6e  ve. */.  v->nPen
31da1 64 69 6e 67 44 61 74 61 20 3d 20 2d 31 3b 0a 0a  dingData = -1;..
31da2 20 20 2a 70 70 56 54 61 62 20 3d 20 26 76 2d 3e    *ppVTab = &v->
31da3 62 61 73 65 3b 0a 20 20 46 54 53 54 52 41 43 45  base;.  FTSTRACE
31da4 28 28 22 46 54 53 33 20 43 6f 6e 6e 65 63 74 20  (("FTS3 Connect 
31da5 25 70 5c 6e 22 2c 20 76 29 29 3b 0a 0a 20 20 72  %p\n", v));..  r
31da6 65 74 75 72 6e 20 72 63 3b 0a 0a 65 72 72 3a 0a  eturn rc;..err:.
31da7 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 5f    fulltext_vtab_
31da8 64 65 73 74 72 6f 79 28 76 29 3b 0a 20 20 72 65  destroy(v);.  re
31da9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
31daa 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43  ic int fulltextC
31dab 6f 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69 74 65  onnect(.  sqlite
31dac 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70  3 *db,.  void *p
31dad 41 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  Aux,.  int argc,
31dae 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
31daf 73 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74  st*argv,.  sqlit
31db0 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62  e3_vtab **ppVTab
31db1 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ,.  char **pzErr
31db2 0a 29 7b 0a 20 20 54 61 62 6c 65 53 70 65 63 20  .){.  TableSpec 
31db3 73 70 65 63 3b 0a 20 20 69 6e 74 20 72 63 20 3d  spec;.  int rc =
31db4 20 70 61 72 73 65 53 70 65 63 28 26 73 70 65 63   parseSpec(&spec
31db5 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70 7a  , argc, argv, pz
31db6 45 72 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Err);.  if( rc!=
31db7 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
31db8 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 63  rn rc;..  rc = c
31db9 6f 6e 73 74 72 75 63 74 56 74 61 62 28 64 62 2c  onstructVtab(db,
31dba 20 28 66 74 73 33 48 61 73 68 20 2a 29 70 41 75   (fts3Hash *)pAu
31dbb 78 2c 20 26 73 70 65 63 2c 20 70 70 56 54 61 62  x, &spec, ppVTab
31dbc 2c 20 70 7a 45 72 72 29 3b 0a 20 20 63 6c 65 61  , pzErr);.  clea
31dbd 72 54 61 62 6c 65 53 70 65 63 28 26 73 70 65 63  rTableSpec(&spec
31dbe 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
31dbf 7d 0a 0a 2f 2a 20 54 68 65 20 25 5f 63 6f 6e 74  }../* The %_cont
31dc0 65 6e 74 20 74 61 62 6c 65 20 68 6f 6c 64 73 20  ent table holds 
31dc1 74 68 65 20 74 65 78 74 20 6f 66 20 65 61 63 68  the text of each
31dc2 20 64 6f 63 75 6d 65 6e 74 2c 20 77 69 74 68 0a   document, with.
31dc3 2a 2a 20 74 68 65 20 64 6f 63 69 64 20 63 6f 6c  ** the docid col
31dc4 75 6d 6e 20 65 78 70 6f 73 65 64 20 61 73 20 74  umn exposed as t
31dc5 68 65 20 53 51 4c 69 74 65 20 72 6f 77 69 64 20  he SQLite rowid 
31dc6 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a  for the table..*
31dc7 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
31dc8 20 54 68 69 73 20 63 6f 6d 6d 65 6e 74 20 6e 65   This comment ne
31dc9 65 64 73 20 65 6c 61 62 6f 72 61 74 69 6f 6e 20  eds elaboration 
31dca 74 6f 20 6d 61 74 63 68 20 74 68 65 20 75 70 64  to match the upd
31dcb 61 74 65 64 0a 2a 2a 20 63 6f 64 65 2e 20 20 57  ated.** code.  W
31dcc 6f 72 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20  ork it into the 
31dcd 74 6f 70 2d 6f 66 2d 66 69 6c 65 20 63 6f 6d 6d  top-of-file comm
31dce 65 6e 74 20 61 74 20 74 68 61 74 20 74 69 6d 65  ent at that time
31dcf 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31dd0 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65 28 73  fulltextCreate(s
31dd1 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
31dd2 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20   *pAux,.        
31dd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31dd4 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
31dd5 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
31dd6 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20  argv,.          
31dd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31dd8 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
31dd9 70 56 54 61 62 2c 20 63 68 61 72 20 2a 2a 70 7a  pVTab, char **pz
31dda 45 72 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Err){.  int rc;.
31ddb 20 20 54 61 62 6c 65 53 70 65 63 20 73 70 65 63    TableSpec spec
31ddc 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66 65 72  ;.  StringBuffer
31ddd 20 73 63 68 65 6d 61 3b 0a 20 20 46 54 53 54 52   schema;.  FTSTR
31dde 41 43 45 28 28 22 46 54 53 33 20 43 72 65 61 74  ACE(("FTS3 Creat
31ddf 65 5c 6e 22 29 29 3b 0a 0a 20 20 72 63 20 3d 20  e\n"));..  rc = 
31de0 70 61 72 73 65 53 70 65 63 28 26 73 70 65 63 2c  parseSpec(&spec,
31de1 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70 7a 45   argc, argv, pzE
31de2 72 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  rr);.  if( rc!=S
31de3 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
31de4 6e 20 72 63 3b 0a 0a 20 20 69 6e 69 74 53 74 72  n rc;..  initStr
31de5 69 6e 67 42 75 66 66 65 72 28 26 73 63 68 65 6d  ingBuffer(&schem
31de6 61 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63  a);.  append(&sc
31de7 68 65 6d 61 2c 20 22 43 52 45 41 54 45 20 54 41  hema, "CREATE TA
31de8 42 4c 45 20 25 5f 63 6f 6e 74 65 6e 74 28 22 29  BLE %_content(")
31de9 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63 68 65  ;.  append(&sche
31dea 6d 61 2c 20 22 20 20 64 6f 63 69 64 20 49 4e 54  ma, "  docid INT
31deb 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
31dec 2c 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69 73  ,");.  appendLis
31ded 74 28 26 73 63 68 65 6d 61 2c 20 73 70 65 63 2e  t(&schema, spec.
31dee 6e 43 6f 6c 75 6d 6e 2c 20 73 70 65 63 2e 61 7a  nColumn, spec.az
31def 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a  ContentColumn);.
31df0 20 20 61 70 70 65 6e 64 28 26 73 63 68 65 6d 61    append(&schema
31df1 2c 20 22 29 22 29 3b 0a 20 20 72 63 20 3d 20 73  , ")");.  rc = s
31df2 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70 65 63  ql_exec(db, spec
31df3 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65  .zDb, spec.zName
31df4 2c 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 61  , stringBufferDa
31df5 74 61 28 26 73 63 68 65 6d 61 29 29 3b 0a 20 20  ta(&schema));.  
31df6 73 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74  stringBufferDest
31df7 72 6f 79 28 26 73 63 68 65 6d 61 29 3b 0a 20 20  roy(&schema);.  
31df8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
31df9 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20  K ) goto out;.. 
31dfa 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64   rc = sql_exec(d
31dfb 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65  b, spec.zDb, spe
31dfc 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  c.zName,.       
31dfd 20 20 20 20 20 20 20 20 20 22 63 72 65 61 74 65           "create
31dfe 20 74 61 62 6c 65 20 25 5f 73 65 67 6d 65 6e 74   table %_segment
31dff 73 28 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  s(".            
31e00 20 20 20 20 22 20 20 62 6c 6f 63 6b 69 64 20 49      "  blockid I
31e01 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
31e02 45 59 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20  EY,".           
31e03 20 20 20 20 20 22 20 20 62 6c 6f 63 6b 20 62 6c       "  block bl
31e04 6f 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  ob".            
31e05 20 20 20 20 22 29 3b 22 0a 20 20 20 20 20 20 20      ");".       
31e06 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 69 66           );.  if
31e07 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
31e08 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72  ) goto out;..  r
31e09 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c  c = sql_exec(db,
31e0a 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e   spec.zDb, spec.
31e0b 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
31e0c 20 20 20 20 20 20 20 22 63 72 65 61 74 65 20 74         "create t
31e0d 61 62 6c 65 20 25 5f 73 65 67 64 69 72 28 22 0a  able %_segdir(".
31e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e0f 22 20 20 6c 65 76 65 6c 20 69 6e 74 65 67 65 72  "  level integer
31e10 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,".             
31e11 20 20 20 22 20 20 69 64 78 20 69 6e 74 65 67 65     "  idx intege
31e12 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  r,".            
31e13 20 20 20 20 22 20 20 73 74 61 72 74 5f 62 6c 6f      "  start_blo
31e14 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20  ck integer,".   
31e15 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
31e16 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
31e17 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20 20 20   integer,".     
31e18 20 20 20 20 20 20 20 20 20 20 20 22 20 20 65 6e             "  en
31e19 64 5f 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c  d_block integer,
31e1a 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
31e1b 20 20 22 20 20 72 6f 6f 74 20 62 6c 6f 62 2c 22    "  root blob,"
31e1c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
31e1d 20 22 20 20 70 72 69 6d 61 72 79 20 6b 65 79 28   "  primary key(
31e1e 6c 65 76 65 6c 2c 20 69 64 78 29 22 0a 20 20 20  level, idx)".   
31e1f 20 20 20 20 20 20 20 20 20 20 20 20 20 22 29 3b               ");
31e20 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ");.  if( rc!=SQ
31e21 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f  LITE_OK ) goto o
31e22 75 74 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 73  ut;..  rc = cons
31e23 74 72 75 63 74 56 74 61 62 28 64 62 2c 20 28 66  tructVtab(db, (f
31e24 74 73 33 48 61 73 68 20 2a 29 70 41 75 78 2c 20  ts3Hash *)pAux, 
31e25 26 73 70 65 63 2c 20 70 70 56 54 61 62 2c 20 70  &spec, ppVTab, p
31e26 7a 45 72 72 29 3b 0a 0a 6f 75 74 3a 0a 20 20 63  zErr);..out:.  c
31e27 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28 26 73  learTableSpec(&s
31e28 70 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pec);.  return r
31e29 63 3b 0a 7d 0a 0a 2f 2a 20 44 65 63 69 64 65 20  c;.}../* Decide 
31e2a 68 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 61 6e  how to handle an
31e2b 20 53 51 4c 20 71 75 65 72 79 2e 20 2a 2f 0a 73   SQL query. */.s
31e2c 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
31e2d 78 74 42 65 73 74 49 6e 64 65 78 28 73 71 6c 69  xtBestIndex(sqli
31e2e 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
31e2f 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
31e30 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 66  nfo *pInfo){.  f
31e31 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20  ulltext_vtab *v 
31e32 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  = (fulltext_vtab
31e33 20 2a 29 70 56 54 61 62 3b 0a 20 20 69 6e 74 20   *)pVTab;.  int 
31e34 69 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  i;.  FTSTRACE(("
31e35 46 54 53 33 20 42 65 73 74 49 6e 64 65 78 5c 6e  FTS3 BestIndex\n
31e36 22 29 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  "));..  for(i=0;
31e37 20 69 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74   i<pInfo->nConst
31e38 72 61 69 6e 74 3b 20 2b 2b 69 29 7b 0a 20 20 20  raint; ++i){.   
31e39 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71   const struct sq
31e3a 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
31e3b 74 72 61 69 6e 74 20 2a 70 43 6f 6e 73 74 72 61  traint *pConstra
31e3c 69 6e 74 3b 0a 20 20 20 20 70 43 6f 6e 73 74 72  int;.    pConstr
31e3d 61 69 6e 74 20 3d 20 26 70 49 6e 66 6f 2d 3e 61  aint = &pInfo->a
31e3e 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 3b 0a 20  Constraint[i];. 
31e3f 20 20 20 69 66 28 20 70 43 6f 6e 73 74 72 61 69     if( pConstrai
31e40 6e 74 2d 3e 75 73 61 62 6c 65 20 29 20 7b 0a 20  nt->usable ) {. 
31e41 20 20 20 20 20 69 66 28 20 28 70 43 6f 6e 73 74       if( (pConst
31e42 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d  raint->iColumn==
31e43 2d 31 20 7c 7c 20 70 43 6f 6e 73 74 72 61 69 6e  -1 || pConstrain
31e44 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e  t->iColumn==v->n
31e45 43 6f 6c 75 6d 6e 2b 31 29 20 26 26 0a 20 20 20  Column+1) &&.   
31e46 20 20 20 20 20 20 20 70 43 6f 6e 73 74 72 61 69         pConstrai
31e47 6e 74 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49  nt->op==SQLITE_I
31e48 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
31e49 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49  EQ ){.        pI
31e4a 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55  nfo->idxNum = QU
31e4b 45 52 59 5f 44 4f 43 49 44 3b 20 20 20 20 20 20  ERY_DOCID;      
31e4c 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79 20 64 6f 63  /* lookup by doc
31e4d 69 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 54  id */.        FT
31e4e 53 54 52 41 43 45 28 28 22 46 54 53 33 20 51 55  STRACE(("FTS3 QU
31e4f 45 52 59 5f 44 4f 43 49 44 5c 6e 22 29 29 3b 0a  ERY_DOCID\n"));.
31e50 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28        } else if(
31e51 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43   pConstraint->iC
31e52 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 43 6f 6e  olumn>=0 && pCon
31e53 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e  straint->iColumn
31e54 3c 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 0a  <=v->nColumn &&.
31e55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e56 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 6f 70   pConstraint->op
31e57 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
31e58 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20  ONSTRAINT_MATCH 
31e59 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 75  ){.        /* fu
31e5a 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20 2a  ll-text search *
31e5b 2f 0a 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d  /.        pInfo-
31e5c 3e 69 64 78 4e 75 6d 20 3d 20 51 55 45 52 59 5f  >idxNum = QUERY_
31e5d 46 55 4c 4c 54 45 58 54 20 2b 20 70 43 6f 6e 73  FULLTEXT + pCons
31e5e 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3b  traint->iColumn;
31e5f 0a 20 20 20 20 20 20 20 20 46 54 53 54 52 41 43  .        FTSTRAC
31e60 45 28 28 22 46 54 53 33 20 51 55 45 52 59 5f 46  E(("FTS3 QUERY_F
31e61 55 4c 4c 54 45 58 54 20 25 64 5c 6e 22 2c 20 70  ULLTEXT %d\n", p
31e62 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c  Constraint->iCol
31e63 75 6d 6e 29 29 3b 0a 20 20 20 20 20 20 7d 20 65  umn));.      } e
31e64 6c 73 65 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20  lse continue;.. 
31e65 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e       pInfo->aCon
31e66 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
31e67 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20  argvIndex = 1;. 
31e68 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e       pInfo->aCon
31e69 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
31e6a 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20 20 20  omit = 1;..     
31e6b 20 2f 2a 20 41 6e 20 61 72 62 69 74 72 61 72 79   /* An arbitrary
31e6c 20 76 61 6c 75 65 20 66 6f 72 20 6e 6f 77 2e 0a   value for now..
31e6d 20 20 20 20 20 20 20 2a 20 54 4f 44 4f 3a 20 50         * TODO: P
31e6e 65 72 68 61 70 73 20 64 6f 63 69 64 20 6d 61 74  erhaps docid mat
31e6f 63 68 65 73 20 73 68 6f 75 6c 64 20 62 65 20 63  ches should be c
31e70 6f 6e 73 69 64 65 72 65 64 20 63 68 65 61 70 65  onsidered cheape
31e71 72 20 74 68 61 6e 0a 20 20 20 20 20 20 20 2a 20  r than.       * 
31e72 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68  full-text search
31e73 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e  es. */.      pIn
31e74 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
31e75 74 20 3d 20 31 2e 30 3b 20 20 20 0a 0a 20 20 20  t = 1.0;   ..   
31e76 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
31e77 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
31e78 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d   pInfo->idxNum =
31e79 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43 3b 0a   QUERY_GENERIC;.
31e7a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
31e7b 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  OK;.}..static in
31e7c 74 20 66 75 6c 6c 74 65 78 74 44 69 73 63 6f 6e  t fulltextDiscon
31e7d 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76 74 61  nect(sqlite3_vta
31e7e 62 20 2a 70 56 54 61 62 29 7b 0a 20 20 46 54 53  b *pVTab){.  FTS
31e7f 54 52 41 43 45 28 28 22 46 54 53 33 20 44 69 73  TRACE(("FTS3 Dis
31e80 63 6f 6e 6e 65 63 74 20 25 70 5c 6e 22 2c 20 70  connect %p\n", p
31e81 56 54 61 62 29 29 3b 0a 20 20 66 75 6c 6c 74 65  VTab));.  fullte
31e82 78 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28  xt_vtab_destroy(
31e83 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
31e84 29 70 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72  )pVTab);.  retur
31e85 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
31e86 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
31e87 65 78 74 44 65 73 74 72 6f 79 28 73 71 6c 69 74  extDestroy(sqlit
31e88 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 7b  e3_vtab *pVTab){
31e89 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
31e8a 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *v = (fulltext_
31e8b 76 74 61 62 20 2a 29 70 56 54 61 62 3b 0a 20 20  vtab *)pVTab;.  
31e8c 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52  int rc;..  FTSTR
31e8d 41 43 45 28 28 22 46 54 53 33 20 44 65 73 74 72  ACE(("FTS3 Destr
31e8e 6f 79 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 29  oy %p\n", pVTab)
31e8f 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78  );.  rc = sql_ex
31e90 65 63 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62  ec(v->db, v->zDb
31e91 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20  , v->zName,.    
31e92 20 20 20 20 20 20 20 20 20 20 20 20 22 64 72 6f              "dro
31e93 70 20 74 61 62 6c 65 20 69 66 20 65 78 69 73 74  p table if exist
31e94 73 20 25 5f 63 6f 6e 74 65 6e 74 3b 22 0a 20 20  s %_content;".  
31e95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 64                "d
31e96 72 6f 70 20 74 61 62 6c 65 20 69 66 20 65 78 69  rop table if exi
31e97 73 74 73 20 25 5f 73 65 67 6d 65 6e 74 73 3b 22  sts %_segments;"
31e98 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
31e99 20 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66 20   "drop table if 
31e9a 65 78 69 73 74 73 20 25 5f 73 65 67 64 69 72 3b  exists %_segdir;
31e9b 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
31e9c 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53    );.  if( rc!=S
31e9d 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
31e9e 6e 20 72 63 3b 0a 0a 20 20 66 75 6c 6c 74 65 78  n rc;..  fulltex
31e9f 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28 28  t_vtab_destroy((
31ea0 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
31ea1 70 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72 6e  pVTab);.  return
31ea2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73   SQLITE_OK;.}..s
31ea3 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
31ea4 78 74 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  xtOpen(sqlite3_v
31ea5 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
31ea6 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
31ea7 2a 2a 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 66  **ppCursor){.  f
31ea8 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
31ea9 63 3b 0a 0a 20 20 63 20 3d 20 28 66 75 6c 6c 74  c;..  c = (fullt
31eaa 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 73 71  ext_cursor *) sq
31eab 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
31eac 65 6f 66 28 66 75 6c 6c 74 65 78 74 5f 63 75 72  eof(fulltext_cur
31ead 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 63 20 29  sor));.  if( c )
31eae 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 63 2c 20  {.    memset(c, 
31eaf 30 2c 20 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65  0, sizeof(fullte
31eb0 78 74 5f 63 75 72 73 6f 72 29 29 3b 0a 20 20 20  xt_cursor));.   
31eb1 20 2f 2a 20 73 71 6c 69 74 65 20 77 69 6c 6c 20   /* sqlite will 
31eb2 69 6e 69 74 69 61 6c 69 7a 65 20 63 2d 3e 62 61  initialize c->ba
31eb3 73 65 20 2a 2f 0a 20 20 20 20 2a 70 70 43 75 72  se */.    *ppCur
31eb4 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a  sor = &c->base;.
31eb5 20 20 20 20 46 54 53 54 52 41 43 45 28 28 22 46      FTSTRACE(("F
31eb6 54 53 33 20 4f 70 65 6e 20 25 70 3a 20 25 70 5c  TS3 Open %p: %p\
31eb7 6e 22 2c 20 70 56 54 61 62 2c 20 63 29 29 3b 0a  n", pVTab, c));.
31eb8 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
31eb9 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
31eba 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
31ebb 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 7d 0a 0a 2f  _NOMEM;.  }.}../
31ebc 2a 20 46 72 65 65 20 61 6c 6c 20 6f 66 20 74 68  * Free all of th
31ebd 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  e dynamically al
31ebe 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 68  located memory h
31ebf 65 6c 64 20 62 79 20 74 68 65 0a 2a 2a 20 53 6e  eld by the.** Sn
31ec0 69 70 70 65 74 0a 2a 2f 0a 73 74 61 74 69 63 20  ippet.*/.static 
31ec1 76 6f 69 64 20 73 6e 69 70 70 65 74 43 6c 65 61  void snippetClea
31ec2 72 28 53 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20  r(Snippet *p){. 
31ec3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d   sqlite3_free(p-
31ec4 3e 61 4d 61 74 63 68 29 3b 0a 20 20 73 71 6c 69  >aMatch);.  sqli
31ec5 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 4f 66 66  te3_free(p->zOff
31ec6 73 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  set);.  sqlite3_
31ec7 66 72 65 65 28 70 2d 3e 7a 53 6e 69 70 70 65 74  free(p->zSnippet
31ec8 29 3b 0a 20 20 43 4c 45 41 52 28 70 29 3b 0a 7d  );.  CLEAR(p);.}
31ec9 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  ../*.** Append a
31eca 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 74 6f   single entry to
31ecb 20 74 68 65 20 70 2d 3e 61 4d 61 74 63 68 5b 5d   the p->aMatch[]
31ecc 20 6c 6f 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   log..*/.static 
31ecd 76 6f 69 64 20 73 6e 69 70 70 65 74 41 70 70 65  void snippetAppe
31ece 6e 64 4d 61 74 63 68 28 0a 20 20 53 6e 69 70 70  ndMatch(.  Snipp
31ecf 65 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  et *p,          
31ed0 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74       /* Append t
31ed1 68 65 20 65 6e 74 72 79 20 74 6f 20 74 68 69 73  he entry to this
31ed2 20 73 6e 69 70 70 65 74 20 2a 2f 0a 20 20 69 6e   snippet */.  in
31ed3 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 54 65 72  t iCol, int iTer
31ed4 6d 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  m,      /* The c
31ed5 6f 6c 75 6d 6e 20 61 6e 64 20 71 75 65 72 79 20  olumn and query 
31ed6 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 54  term */.  int iT
31ed7 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  oken,           
31ed8 20 20 20 20 2f 2a 20 4d 61 74 63 68 69 6e 67 20      /* Matching 
31ed9 74 6f 6b 65 6e 20 69 6e 20 64 6f 63 75 6d 65 6e  token in documen
31eda 74 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72  t */.  int iStar
31edb 74 2c 20 69 6e 74 20 6e 42 79 74 65 20 20 20 20  t, int nByte    
31edc 20 2f 2a 20 4f 66 66 73 65 74 20 61 6e 64 20 73   /* Offset and s
31edd 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 74 63 68  ize of the match
31ede 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
31edf 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74    struct snippet
31ee0 4d 61 74 63 68 20 2a 70 4d 61 74 63 68 3b 0a 20  Match *pMatch;. 
31ee1 20 69 66 28 20 70 2d 3e 6e 4d 61 74 63 68 2b 31   if( p->nMatch+1
31ee2 3e 3d 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  >=p->nAlloc ){. 
31ee3 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70     p->nAlloc = p
31ee4 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 31 30 3b  ->nAlloc*2 + 10;
31ee5 0a 20 20 20 20 70 2d 3e 61 4d 61 74 63 68 20 3d  .    p->aMatch =
31ee6 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
31ee7 28 70 2d 3e 61 4d 61 74 63 68 2c 20 70 2d 3e 6e  (p->aMatch, p->n
31ee8 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d 3e  Alloc*sizeof(p->
31ee9 61 4d 61 74 63 68 5b 30 5d 29 20 29 3b 0a 20 20  aMatch[0]) );.  
31eea 20 20 69 66 28 20 70 2d 3e 61 4d 61 74 63 68 3d    if( p->aMatch=
31eeb 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e  =0 ){.      p->n
31eec 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20  Match = 0;.     
31eed 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a   p->nAlloc = 0;.
31eee 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
31eef 20 20 7d 0a 20 20 7d 0a 20 20 69 20 3d 20 70 2d    }.  }.  i = p-
31ef0 3e 6e 4d 61 74 63 68 2b 2b 3b 0a 20 20 70 4d 61  >nMatch++;.  pMa
31ef1 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61 74 63 68  tch = &p->aMatch
31ef2 5b 69 5d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69  [i];.  pMatch->i
31ef3 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 70 4d  Col = iCol;.  pM
31ef4 61 74 63 68 2d 3e 69 54 65 72 6d 20 3d 20 69 54  atch->iTerm = iT
31ef5 65 72 6d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69  erm;.  pMatch->i
31ef6 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b 0a  Token = iToken;.
31ef7 20 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72 74    pMatch->iStart
31ef8 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 70 4d 61   = iStart;.  pMa
31ef9 74 63 68 2d 3e 6e 42 79 74 65 20 3d 20 6e 42 79  tch->nByte = nBy
31efa 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a  te;.}../*.** Siz
31efb 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
31efc 66 6f 72 20 74 68 65 20 63 69 72 63 75 6c 61 72  for the circular
31efd 20 62 75 66 66 65 72 20 75 73 65 64 20 69 6e 20   buffer used in 
31efe 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66  snippetOffsetsOf
31eff 43 6f 6c 75 6d 6e 28 29 0a 2a 2f 0a 23 64 65 66  Column().*/.#def
31f00 69 6e 65 20 46 54 53 33 5f 52 4f 54 4f 52 5f 53  ine FTS3_ROTOR_S
31f01 5a 20 20 20 28 33 32 29 0a 23 64 65 66 69 6e 65  Z   (32).#define
31f02 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b   FTS3_ROTOR_MASK
31f03 20 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d   (FTS3_ROTOR_SZ-
31f04 31 29 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  1)../*.** Functi
31f05 6f 6e 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  on to iterate th
31f06 72 6f 75 67 68 20 74 68 65 20 74 6f 6b 65 6e 73  rough the tokens
31f07 20 6f 66 20 61 20 63 6f 6d 70 69 6c 65 64 20 65   of a compiled e
31f08 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
31f09 20 45 78 63 65 70 74 2c 20 73 6b 69 70 20 61 6c   Except, skip al
31f0a 6c 20 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65 20  l tokens on the 
31f0b 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
31f0c 6f 66 20 61 20 4e 4f 54 20 6f 70 65 72 61 74 6f  of a NOT operato
31f0d 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  r..** This funct
31f0e 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 66  ion is used to f
31f0f 69 6e 64 20 74 6f 6b 65 6e 73 20 61 73 20 70 61  ind tokens as pa
31f10 72 74 20 6f 66 20 73 6e 69 70 70 65 74 20 61 6e  rt of snippet an
31f11 64 20 6f 66 66 73 65 74 0a 2a 2a 20 67 65 6e 65  d offset.** gene
31f12 72 61 74 69 6f 6e 20 61 6e 64 20 77 65 20 64 6f  ration and we do
31f13 20 6e 74 20 77 61 6e 74 20 73 6e 69 70 70 65 74   nt want snippet
31f14 73 20 61 6e 64 20 6f 66 66 73 65 74 73 20 74 6f  s and offsets to
31f15 20 72 65 70 6f 72 74 20 6d 61 74 63 68 65 73 0a   report matches.
31f16 2a 2a 20 66 6f 72 20 74 6f 6b 65 6e 73 20 6f 6e  ** for tokens on
31f17 20 74 68 65 20 52 48 53 20 6f 66 20 61 20 4e 4f   the RHS of a NO
31f18 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  T..*/.static int
31f19 20 66 74 73 33 4e 65 78 74 45 78 70 72 54 6f 6b   fts3NextExprTok
31f1a 65 6e 28 46 74 73 33 45 78 70 72 20 2a 2a 70 70  en(Fts3Expr **pp
31f1b 45 78 70 72 2c 20 69 6e 74 20 2a 70 69 54 6f 6b  Expr, int *piTok
31f1c 65 6e 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  en){.  Fts3Expr 
31f1d 2a 70 20 3d 20 2a 70 70 45 78 70 72 3b 0a 20 20  *p = *ppExpr;.  
31f1e 69 6e 74 20 69 54 6f 6b 65 6e 20 3d 20 2a 70 69  int iToken = *pi
31f1f 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 69 54 6f  Token;.  if( iTo
31f20 6b 65 6e 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ken<0 ){.    /* 
31f21 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
31f22 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73   expression p is
31f23 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20   the root of an 
31f24 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
31f25 0a 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 6f 20  .    ** Move to 
31f26 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20  the first token 
31f27 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
31f28 6e 20 74 72 65 65 2e 0a 20 20 20 20 2a 2f 0a 20  n tree..    */. 
31f29 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4c 65     while( p->pLe
31f2a 66 74 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  ft ){.      p = 
31f2b 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 0a  p->pLeft;.    }.
31f2c 20 20 20 20 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a      iToken = 0;.
31f2d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
31f2e 65 72 74 28 70 20 26 26 20 70 2d 3e 65 54 79 70  ert(p && p->eTyp
31f2f 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
31f30 53 45 20 29 3b 0a 20 20 20 20 69 66 28 20 69 54  SE );.    if( iT
31f31 6f 6b 65 6e 3c 28 70 2d 3e 70 50 68 72 61 73 65  oken<(p->pPhrase
31f32 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20  ->nToken-1) ){. 
31f33 20 20 20 20 20 69 54 6f 6b 65 6e 2b 2b 3b 0a 20       iToken++;. 
31f34 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
31f35 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20  iToken = 0;.    
31f36 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 61 72    while( p->pPar
31f37 65 6e 74 20 26 26 20 70 2d 3e 70 50 61 72 65 6e  ent && p->pParen
31f38 74 2d 3e 70 4c 65 66 74 21 3d 70 20 29 7b 0a 20  t->pLeft!=p ){. 
31f39 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
31f3a 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
31f3b 74 3d 3d 70 20 29 3b 0a 20 20 20 20 20 20 20 20  t==p );.        
31f3c 70 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b 0a  p = p->pParent;.
31f3d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 20        }.      p 
31f3e 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  = p->pParent;.  
31f3f 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
31f40 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
31f41 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20  pRight!=0 );.   
31f42 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67       p = p->pRig
31f43 68 74 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ht;.        whil
31f44 65 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a 20  e( p->pLeft ){. 
31f45 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e           p = p->
31f46 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 7d  pLeft;.        }
31f47 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
31f48 20 7d 0a 0a 20 20 2a 70 70 45 78 70 72 20 3d 20   }..  *ppExpr = 
31f49 70 3b 0a 20 20 2a 70 69 54 6f 6b 65 6e 20 3d 20  p;.  *piToken = 
31f4a 69 54 6f 6b 65 6e 3b 0a 20 20 72 65 74 75 72 6e  iToken;.  return
31f4b 20 70 3f 31 3a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   p?1:0;.}../*.**
31f4c 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
31f4d 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  the expression n
31f4e 6f 64 65 20 70 45 78 70 72 20 69 73 20 6c 6f 63  ode pExpr is loc
31f4f 61 74 65 64 20 62 65 6e 65 61 74 68 20 74 68 65  ated beneath the
31f50 0a 2a 2a 20 52 48 53 20 6f 66 20 61 20 4e 4f 54  .** RHS of a NOT
31f51 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74   operator..*/.st
31f52 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 78 70  atic int fts3Exp
31f53 72 42 65 6e 65 61 74 68 4e 6f 74 28 46 74 73 33  rBeneathNot(Fts3
31f54 45 78 70 72 20 2a 70 29 7b 0a 20 20 46 74 73 33  Expr *p){.  Fts3
31f55 45 78 70 72 20 2a 70 50 61 72 65 6e 74 3b 0a 20  Expr *pParent;. 
31f56 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
31f57 20 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50   pParent = p->pP
31f58 61 72 65 6e 74 3b 0a 20 20 20 20 69 66 28 20 70  arent;.    if( p
31f59 50 61 72 65 6e 74 20 26 26 20 70 50 61 72 65 6e  Parent && pParen
31f5a 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  t->eType==FTSQUE
31f5b 52 59 5f 4e 4f 54 20 26 26 20 70 50 61 72 65 6e  RY_NOT && pParen
31f5c 74 2d 3e 70 52 69 67 68 74 3d 3d 70 20 29 7b 0a  t->pRight==p ){.
31f5d 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
31f5e 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 50      }.    p = pP
31f5f 61 72 65 6e 74 3b 0a 20 20 7d 0a 20 20 72 65 74  arent;.  }.  ret
31f60 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
31f61 41 64 64 20 65 6e 74 72 69 65 73 20 74 6f 20 70  Add entries to p
31f62 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b  Snippet->aMatch[
31f63 5d 20 66 6f 72 20 65 76 65 72 79 20 6d 61 74 63  ] for every matc
31f64 68 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 67  h that occurs ag
31f65 61 69 6e 73 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e  ainst.** documen
31f66 74 20 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31  t zDoc[0..nDoc-1
31f67 5d 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 65  ] which is store
31f68 64 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c  d in column iCol
31f69 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  umn..*/.static v
31f6a 6f 69 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65  oid snippetOffse
31f6b 74 73 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 66 75  tsOfColumn(.  fu
31f6c 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
31f6d 43 75 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Cur,         /* 
31f6e 54 68 65 20 66 75 6c 6c 74 65 73 74 20 73 65 61  The fulltest sea
31f6f 72 63 68 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  rch cursor */.  
31f70 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
31f71 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t,             /
31f72 2a 20 54 68 65 20 53 6e 69 70 70 65 74 20 6f 62  * The Snippet ob
31f73 6a 65 63 74 20 74 6f 20 62 65 20 66 69 6c 6c 65  ject to be fille
31f74 64 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43  d in */.  int iC
31f75 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
31f76 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
31f77 78 20 6f 66 20 66 75 6c 6c 74 65 78 74 20 74 61  x of fulltext ta
31f78 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  ble column */.  
31f79 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63  const char *zDoc
31f7a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
31f7b 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 66 75  * Text of the fu
31f7c 6c 6c 74 65 78 74 20 74 61 62 6c 65 20 63 6f 6c  lltext table col
31f7d 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f  umn */.  int nDo
31f7e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
31f7f 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
31f80 68 20 6f 66 20 7a 44 6f 63 20 69 6e 20 62 79 74  h of zDoc in byt
31f81 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  es */.){.  const
31f82 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
31f83 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 54 4d 6f 64  er_module *pTMod
31f84 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b  ule;  /* The tok
31f85 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f  enizer module */
31f86 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
31f87 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
31f88 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
31f89 20 54 68 65 20 73 70 65 63 69 66 69 63 20 74 6f   The specific to
31f8a 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 73 71 6c  kenizer */.  sql
31f8b 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
31f8c 75 72 73 6f 72 20 2a 70 54 43 75 72 73 6f 72 3b  ursor *pTCursor;
31f8d 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
31f8e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
31f8f 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
31f90 70 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 20  pVtab;          
31f91 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6c        /* The ful
31f92 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a  l text index */.
31f93 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
31f94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f95 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
31f96 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
31f97 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  he index */.  in
31f98 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
31f99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f9a 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
31f9b 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ers */.  int rc;
31f9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
31f9e 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
31f9f 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d    unsigned int m
31fa0 61 74 63 68 2c 20 70 72 65 76 4d 61 74 63 68 3b  atch, prevMatch;
31fa1 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65         /* Phrase
31fa2 20 73 65 61 72 63 68 20 62 69 74 6d 61 73 6b 73   search bitmasks
31fa3 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
31fa4 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20   *zToken;       
31fa5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
31fa6 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 74 68  xt token from th
31fa7 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20  e tokenizer */. 
31fa8 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20   int nToken;    
31fa9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31faa 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
31fab 20 7a 54 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74   zToken */.  int
31fac 20 69 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69   iBegin, iEnd, i
31fad 50 6f 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Pos;            
31fae 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20    /* Offsets of 
31faf 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e  beginning and en
31fb0 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66  d */..  /* The f
31fb1 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
31fb2 65 73 20 6b 65 65 70 20 61 20 63 69 72 63 75 6c  es keep a circul
31fb3 61 72 20 62 75 66 66 65 72 20 6f 66 20 74 68 65  ar buffer of the
31fb4 20 6c 61 73 74 0a 20 20 2a 2a 20 66 65 77 20 74   last.  ** few t
31fb5 6f 6b 65 6e 73 20 2a 2f 0a 20 20 75 6e 73 69 67  okens */.  unsig
31fb6 6e 65 64 20 69 6e 74 20 69 52 6f 74 6f 72 20 3d  ned int iRotor =
31fb7 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
31fb8 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72  /* Index of curr
31fb9 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69  ent token */.  i
31fba 6e 74 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 46  nt iRotorBegin[F
31fbb 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20  TS3_ROTOR_SZ];  
31fbc 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67      /* Beginning
31fbd 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
31fbe 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 74 6f 72   */.  int iRotor
31fbf 4c 65 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53  Len[FTS3_ROTOR_S
31fc0 5a 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  Z];        /* Le
31fc1 6e 67 74 68 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  ngth of token */
31fc2 0a 0a 20 20 70 56 74 61 62 20 3d 20 63 75 72 73  ..  pVtab = curs
31fc3 6f 72 5f 76 74 61 62 28 70 43 75 72 29 3b 0a 20  or_vtab(pCur);. 
31fc4 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 56 74 61 62   nColumn = pVtab
31fc5 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 70 54 6f  ->nColumn;.  pTo
31fc6 6b 65 6e 69 7a 65 72 20 3d 20 70 56 74 61 62 2d  kenizer = pVtab-
31fc7 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70  >pTokenizer;.  p
31fc8 54 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e  TModule = pToken
31fc9 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  izer->pModule;. 
31fca 20 72 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e   rc = pTModule->
31fcb 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72  xOpen(pTokenizer
31fcc 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 26 70  , zDoc, nDoc, &p
31fcd 54 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20  TCursor);.  if( 
31fce 72 63 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  rc ) return;.  p
31fcf 54 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69  TCursor->pTokeni
31fd0 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72  zer = pTokenizer
31fd1 3b 0a 0a 20 20 70 72 65 76 4d 61 74 63 68 20 3d  ;..  prevMatch =
31fd2 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 21 70 54   0;.  while( !pT
31fd3 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 54  Module->xNext(pT
31fd4 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c  Cursor, &zToken,
31fd5 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69   &nToken, &iBegi
31fd6 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29  n, &iEnd, &iPos)
31fd7 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78 70 72   ){.    Fts3Expr
31fd8 20 2a 70 49 74 65 72 20 3d 20 70 43 75 72 2d 3e   *pIter = pCur->
31fd9 70 45 78 70 72 3b 0a 20 20 20 20 69 6e 74 20 69  pExpr;.    int i
31fda 49 74 65 72 20 3d 20 2d 31 3b 0a 20 20 20 20 69  Iter = -1;.    i
31fdb 52 6f 74 6f 72 42 65 67 69 6e 5b 69 52 6f 74 6f  RotorBegin[iRoto
31fdc 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53  r&FTS3_ROTOR_MAS
31fdd 4b 5d 20 3d 20 69 42 65 67 69 6e 3b 0a 20 20 20  K] = iBegin;.   
31fde 20 69 52 6f 74 6f 72 4c 65 6e 5b 69 52 6f 74 6f   iRotorLen[iRoto
31fdf 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53  r&FTS3_ROTOR_MAS
31fe0 4b 5d 20 3d 20 69 45 6e 64 2d 69 42 65 67 69 6e  K] = iEnd-iBegin
31fe1 3b 0a 20 20 20 20 6d 61 74 63 68 20 3d 20 30 3b  ;.    match = 0;
31fe2 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
31fe3 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31  (FTS3_ROTOR_SZ-1
31fe4 29 20 26 26 20 66 74 73 33 4e 65 78 74 45 78 70  ) && fts3NextExp
31fe5 72 54 6f 6b 65 6e 28 26 70 49 74 65 72 2c 20 26  rToken(&pIter, &
31fe6 69 49 74 65 72 29 3b 20 69 2b 2b 29 7b 0a 20 20  iIter); i++){.  
31fe7 20 20 20 20 69 6e 74 20 6e 50 68 72 61 73 65 3b      int nPhrase;
31fe8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fe9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
31fea 20 74 6f 6b 65 6e 73 20 69 6e 20 63 75 72 72 65   tokens in curre
31feb 6e 74 20 70 68 72 61 73 65 20 2a 2f 0a 20 20 20  nt phrase */.   
31fec 20 20 20 73 74 72 75 63 74 20 50 68 72 61 73 65     struct Phrase
31fed 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 20 20  Token *pToken;  
31fee 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 74 6f     /* Current to
31fef 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ken */.      int
31ff0 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   iCol;          
31ff1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31ff2 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 2a 2f 0a  Column index */.
31ff3 0a 20 20 20 20 20 20 69 66 28 20 66 74 73 33 45  .      if( fts3E
31ff4 78 70 72 42 65 6e 65 61 74 68 4e 6f 74 28 70 49  xprBeneathNot(pI
31ff5 74 65 72 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ter) ) continue;
31ff6 0a 20 20 20 20 20 20 6e 50 68 72 61 73 65 20 3d  .      nPhrase =
31ff7 20 70 49 74 65 72 2d 3e 70 50 68 72 61 73 65 2d   pIter->pPhrase-
31ff8 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70  >nToken;.      p
31ff9 54 6f 6b 65 6e 20 3d 20 26 70 49 74 65 72 2d 3e  Token = &pIter->
31ffa 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
31ffb 69 49 74 65 72 5d 3b 0a 20 20 20 20 20 20 69 43  iIter];.      iC
31ffc 6f 6c 20 3d 20 70 49 74 65 72 2d 3e 70 50 68 72  ol = pIter->pPhr
31ffd 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  ase->iColumn;.  
31ffe 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20      if( iCol>=0 
31fff 26 26 20 69 43 6f 6c 3c 6e 43 6f 6c 75 6d 6e 20  && iCol<nColumn 
32000 26 26 20 69 43 6f 6c 21 3d 69 43 6f 6c 75 6d 6e  && iCol!=iColumn
32001 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
32002 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 6e     if( pToken->n
32003 3e 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e  >nToken ) contin
32004 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  ue;.      if( !p
32005 54 6f 6b 65 6e 2d 3e 69 73 50 72 65 66 69 78 20  Token->isPrefix 
32006 26 26 20 70 54 6f 6b 65 6e 2d 3e 6e 3c 6e 54 6f  && pToken->n<nTo
32007 6b 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ken ) continue;.
32008 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
32009 6f 6b 65 6e 2d 3e 6e 3c 3d 6e 54 6f 6b 65 6e 20  oken->n<=nToken 
3200a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  );.      if( mem
3200b 63 6d 70 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 7a  cmp(pToken->z, z
3200c 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 6e  Token, pToken->n
3200d 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
3200e 20 20 20 20 69 66 28 20 69 49 74 65 72 3e 30 20      if( iIter>0 
3200f 26 26 20 28 70 72 65 76 4d 61 74 63 68 20 26 20  && (prevMatch & 
32010 28 31 3c 3c 69 29 29 3d 3d 30 20 29 20 63 6f 6e  (1<<i))==0 ) con
32011 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 6d 61 74  tinue;.      mat
32012 63 68 20 7c 3d 20 31 3c 3c 69 3b 0a 20 20 20 20  ch |= 1<<i;.    
32013 20 20 69 66 28 20 69 3d 3d 28 46 54 53 33 5f 52    if( i==(FTS3_R
32014 4f 54 4f 52 5f 53 5a 2d 32 29 20 7c 7c 20 6e 50  OTOR_SZ-2) || nP
32015 68 72 61 73 65 3d 3d 69 49 74 65 72 2b 31 20 29  hrase==iIter+1 )
32016 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  {.        for(j=
32017 6e 50 68 72 61 73 65 2d 31 3b 20 6a 3e 3d 30 3b  nPhrase-1; j>=0;
32018 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20   j--){.         
32019 20 69 6e 74 20 6b 20 3d 20 28 69 52 6f 74 6f 72   int k = (iRotor
3201a 2d 6a 29 20 26 20 46 54 53 33 5f 52 4f 54 4f 52  -j) & FTS3_ROTOR
3201b 5f 4d 41 53 4b 3b 0a 20 20 20 20 20 20 20 20 20  _MASK;.         
3201c 20 73 6e 69 70 70 65 74 41 70 70 65 6e 64 4d 61   snippetAppendMa
3201d 74 63 68 28 70 53 6e 69 70 70 65 74 2c 20 69 43  tch(pSnippet, iC
3201e 6f 6c 75 6d 6e 2c 20 69 2d 6a 2c 20 69 50 6f 73  olumn, i-j, iPos
3201f 2d 6a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  -j,.            
32020 20 20 20 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b      iRotorBegin[
32021 6b 5d 2c 20 69 52 6f 74 6f 72 4c 65 6e 5b 6b 5d  k], iRotorLen[k]
32022 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
32023 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
32024 72 65 76 4d 61 74 63 68 20 3d 20 6d 61 74 63 68  revMatch = match
32025 3c 3c 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72 2b  <<1;.    iRotor+
32026 2b 3b 0a 20 20 7d 0a 20 20 70 54 4d 6f 64 75 6c  +;.  }.  pTModul
32027 65 2d 3e 78 43 6c 6f 73 65 28 70 54 43 75 72 73  e->xClose(pTCurs
32028 6f 72 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  or);  .}../*.** 
32029 52 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66  Remove entries f
3202a 72 6f 6d 20 74 68 65 20 70 53 6e 69 70 70 65 74  rom the pSnippet
3202b 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 61 63   structure to ac
3202c 63 6f 75 6e 74 20 66 6f 72 20 74 68 65 20 4e 45  count for the NE
3202d 41 52 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 20  AR.** operator. 
3202e 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c  When this is cal
3202f 6c 65 64 2c 20 70 53 6e 69 70 70 65 74 20 63 6f  led, pSnippet co
32030 6e 74 61 69 6e 73 20 74 68 65 20 6c 69 73 74 20  ntains the list 
32031 6f 66 20 74 6f 6b 65 6e 20 0a 2a 2a 20 6f 66 66  of token .** off
32032 73 65 74 73 20 70 72 6f 64 75 63 65 64 20 62 79  sets produced by
32033 20 74 72 65 61 74 69 6e 67 20 61 6c 6c 20 4e 45   treating all NE
32034 41 52 20 6f 70 65 72 61 74 6f 72 73 20 61 73 20  AR operators as 
32035 41 4e 44 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  AND operators..*
32036 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
32037 72 65 6d 6f 76 65 73 20 61 6e 79 20 65 6e 74 72  removes any entr
32038 69 65 73 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ies that should 
32039 6e 6f 74 20 62 65 20 70 72 65 73 65 6e 74 20 61  not be present a
3203a 66 74 65 72 0a 2a 2a 20 61 63 63 6f 75 6e 74 69  fter.** accounti
3203b 6e 67 20 66 6f 72 20 74 68 65 20 4e 45 41 52 20  ng for the NEAR 
3203c 72 65 73 74 72 69 63 74 69 6f 6e 2e 20 46 6f 72  restriction. For
3203d 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
3203e 20 71 75 65 72 69 65 64 0a 2a 2a 20 64 6f 63 75   queried.** docu
3203f 6d 65 6e 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ment is:.**.**  
32040 20 20 20 22 41 20 42 20 43 20 44 20 45 20 41 22     "A B C D E A"
32041 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 71  .**.** and the q
32042 75 65 72 79 20 69 73 3a 0a 2a 2a 20 0a 2a 2a 20  uery is:.** .** 
32043 20 20 20 20 41 20 4e 45 41 52 2f 30 20 45 0a 2a      A NEAR/0 E.*
32044 2a 0a 2a 2a 20 74 68 65 6e 20 77 68 65 6e 20 74  *.** then when t
32045 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
32046 63 61 6c 6c 65 64 20 74 68 65 20 53 6e 69 70 70  called the Snipp
32047 65 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f 6b 65  et contains toke
32048 6e 20 6f 66 66 73 65 74 73 0a 2a 2a 20 30 2c 20  n offsets.** 0, 
32049 34 20 61 6e 64 20 35 2e 20 54 68 69 73 20 66 75  4 and 5. This fu
3204a 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65 73 20 74  nction removes t
3204b 68 65 20 22 30 22 20 65 6e 74 72 79 20 28 62 65  he "0" entry (be
3204c 63 61 75 73 65 20 74 68 65 20 66 69 72 73 74 20  cause the first 
3204d 41 0a 2a 2a 20 69 73 20 6e 6f 74 20 6e 65 61 72  A.** is not near
3204e 20 65 6e 6f 75 67 68 20 74 6f 20 61 6e 20 45 29   enough to an E)
3204f 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
32050 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
32051 6c 6c 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20  lled, the value 
32052 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 61  pointed to by pa
32053 72 61 6d 65 74 65 72 20 70 69 4c 65 66 74 20 69  rameter piLeft i
32054 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72  s.** the integer
32055 20 69 64 20 6f 66 20 74 68 65 20 6c 65 66 74 2d   id of the left-
32056 6d 6f 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68  most token in th
32057 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
32058 65 20 68 65 61 64 65 64 20 62 79 0a 2a 2a 20 70  e headed by.** p
32059 45 78 70 72 2e 20 54 68 69 73 20 66 75 6e 63 74  Expr. This funct
3205a 69 6f 6e 20 69 6e 63 72 65 6d 65 6e 74 73 20 2a  ion increments *
3205b 70 69 4c 65 66 74 20 62 79 20 74 68 65 20 74 6f  piLeft by the to
3205c 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f  tal number of to
3205d 6b 65 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 65  kens.** in the e
3205e 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68  xpression tree h
3205f 65 61 64 65 64 20 62 79 20 70 45 78 70 72 2e 0a  eaded by pExpr..
32060 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69  **.** Return 1 i
32061 66 20 61 6e 79 20 74 72 69 6d 6d 69 6e 67 20 6f  f any trimming o
32062 63 63 75 72 73 2e 20 20 52 65 74 75 72 6e 20 30  ccurs.  Return 0
32063 20 69 66 20 6e 6f 20 74 72 69 6d 6d 69 6e 67 20   if no trimming 
32064 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a  is required..*/.
32065 73 74 61 74 69 63 20 69 6e 74 20 74 72 69 6d 53  static int trimS
32066 6e 69 70 70 65 74 4f 66 66 73 65 74 73 28 0a 20  nippetOffsets(. 
32067 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72   Fts3Expr *pExpr
32068 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 65  ,      /* The se
32069 61 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20  arch expression 
3206a 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 2a 70 53  */.  Snippet *pS
3206b 6e 69 70 70 65 74 2c 20 20 20 20 2f 2a 20 54 68  nippet,    /* Th
3206c 65 20 73 65 74 20 6f 66 20 73 6e 69 70 70 65 74  e set of snippet
3206d 20 6f 66 66 73 65 74 73 20 74 6f 20 62 65 20 74   offsets to be t
3206e 72 69 6d 6d 65 64 20 2a 2f 0a 20 20 69 6e 74 20  rimmed */.  int 
3206f 2a 70 69 4c 65 66 74 20 20 20 20 20 20 20 20 20  *piLeft         
32070 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6c 65    /* Index of le
32071 66 74 2d 6d 6f 73 74 20 74 6f 6b 65 6e 20 69 6e  ft-most token in
32072 20 70 45 78 70 72 20 2a 2f 0a 29 7b 0a 20 20 69   pExpr */.){.  i
32073 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20  f( pExpr ){.    
32074 69 66 28 20 74 72 69 6d 53 6e 69 70 70 65 74 4f  if( trimSnippetO
32075 66 66 73 65 74 73 28 70 45 78 70 72 2d 3e 70 4c  ffsets(pExpr->pL
32076 65 66 74 2c 20 70 53 6e 69 70 70 65 74 2c 20 70  eft, pSnippet, p
32077 69 4c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20  iLeft) ){.      
32078 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
32079 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 45 78  .    switch( pEx
3207a 70 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20 20  pr->eType ){.   
3207b 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
3207c 5f 50 48 52 41 53 45 3a 0a 20 20 20 20 20 20 20  _PHRASE:.       
3207d 20 2a 70 69 4c 65 66 74 20 2b 3d 20 70 45 78 70   *piLeft += pExp
3207e 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  r->pPhrase->nTok
3207f 65 6e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  en;.        brea
32080 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 46 54  k;.      case FT
32081 53 51 55 45 52 59 5f 4e 45 41 52 3a 20 7b 0a 20  SQUERY_NEAR: {. 
32082 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69         /* The ri
32083 67 68 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66  ght-hand-side of
32084 20 61 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72   a NEAR operator
32085 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 68 72   is always a phr
32086 61 73 65 2e 20 54 68 65 0a 20 20 20 20 20 20 20  ase. The.       
32087 20 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69   ** left-hand-si
32088 64 65 20 69 73 20 65 69 74 68 65 72 20 61 20 70  de is either a p
32089 68 72 61 73 65 20 6f 72 20 61 6e 20 65 78 70 72  hrase or an expr
3208a 65 73 73 69 6f 6e 20 74 72 65 65 20 74 68 61 74  ession tree that
3208b 20 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20   is .        ** 
3208c 69 74 73 65 6c 66 20 68 65 61 64 65 64 20 62 79  itself headed by
3208d 20 61 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72   a NEAR operator
3208e 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
3208f 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 73 0a  initializations.
32090 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6c          ** set l
32091 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 69 4c  ocal variable iL
32092 65 66 74 20 74 6f 20 74 68 65 20 74 6f 6b 65 6e  eft to the token
32093 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c   number of the l
32094 65 66 74 2d 6d 6f 73 74 0a 20 20 20 20 20 20 20  eft-most.       
32095 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65   ** token in the
32096 20 72 69 67 68 74 2d 68 61 6e 64 20 70 68 72 61   right-hand phra
32097 73 65 2c 20 61 6e 64 20 69 52 69 67 68 74 20 74  se, and iRight t
32098 6f 20 74 68 65 20 72 69 67 68 74 20 6d 6f 73 74  o the right most
32099 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65  .        ** toke
3209a 6e 20 69 6e 20 74 68 65 20 73 61 6d 65 20 70 68  n in the same ph
3209b 72 61 73 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c  rase. For exampl
3209c 65 2c 20 69 66 20 77 65 20 68 61 64 3a 0a 20 20  e, if we had:.  
3209d 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
3209e 20 2a 2a 20 20 20 20 20 3c 63 6f 6c 3e 20 4d 41   **     <col> MA
3209f 54 43 48 20 27 22 61 62 63 20 64 65 66 22 20 4e  TCH '"abc def" N
320a0 45 41 52 2f 32 20 22 67 68 69 20 6a 6b 6c 22 27  EAR/2 "ghi jkl"'
320a1 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
320a2 20 20 20 20 2a 2a 20 74 68 65 6e 20 69 4c 65 66      ** then iLef
320a3 74 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f  t will be set to
320a4 20 32 20 28 74 6f 6b 65 6e 20 6e 75 6d 62 65 72   2 (token number
320a5 20 6f 66 20 67 68 69 29 20 61 6e 64 20 6e 54 6f   of ghi) and nTo
320a6 6b 65 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 20  ken will.       
320a7 20 2a 2a 20 62 65 20 73 65 74 20 74 6f 20 34 2e   ** be set to 4.
320a8 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
320a9 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c      Fts3Expr *pL
320aa 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  eft = pExpr->pLe
320ab 66 74 3b 0a 20 20 20 20 20 20 20 20 46 74 73 33  ft;.        Fts3
320ac 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70  Expr *pRight = p
320ad 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Expr->pRight;.  
320ae 20 20 20 20 20 20 69 6e 74 20 69 4c 65 66 74 20        int iLeft 
320af 3d 20 2a 70 69 4c 65 66 74 3b 0a 20 20 20 20 20  = *piLeft;.     
320b0 20 20 20 69 6e 74 20 6e 4e 65 61 72 20 3d 20 70     int nNear = p
320b1 45 78 70 72 2d 3e 6e 4e 65 61 72 3b 0a 20 20 20  Expr->nNear;.   
320b2 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20       int nToken 
320b3 3d 20 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73  = pRight->pPhras
320b4 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  e->nToken;.     
320b5 20 20 20 69 6e 74 20 6a 6a 2c 20 69 69 3b 0a 20     int jj, ii;. 
320b6 20 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74         if( pLeft
320b7 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
320b8 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20  Y_NEAR ){.      
320b9 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4c 65 66      pLeft = pLef
320ba 74 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20  t->pRight;.     
320bb 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73     }.        ass
320bc 65 72 74 28 20 70 52 69 67 68 74 2d 3e 65 54 79  ert( pRight->eTy
320bd 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe==FTSQUERY_PHR
320be 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ASE );.        a
320bf 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 65 54  ssert( pLeft->eT
320c0 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
320c1 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  RASE );.        
320c2 6e 54 6f 6b 65 6e 20 2b 3d 20 70 4c 65 66 74 2d  nToken += pLeft-
320c3 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
320c4 3b 0a 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ;..        for(i
320c5 69 3d 30 3b 20 69 69 3c 70 53 6e 69 70 70 65 74  i=0; ii<pSnippet
320c6 2d 3e 6e 4d 61 74 63 68 3b 20 69 69 2b 2b 29 7b  ->nMatch; ii++){
320c7 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63  .          struc
320c8 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a  t snippetMatch *
320c9 70 20 3d 20 26 70 53 6e 69 70 70 65 74 2d 3e 61  p = &pSnippet->a
320ca 4d 61 74 63 68 5b 69 69 5d 3b 0a 20 20 20 20 20  Match[ii];.     
320cb 20 20 20 20 20 69 66 28 20 70 2d 3e 69 54 65 72       if( p->iTer
320cc 6d 3d 3d 69 4c 65 66 74 20 29 7b 0a 20 20 20 20  m==iLeft ){.    
320cd 20 20 20 20 20 20 20 20 69 6e 74 20 69 73 4f 6b          int isOk
320ce 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
320cf 20 20 2f 2a 20 53 6e 69 70 70 65 74 20 69 69 20    /* Snippet ii 
320d0 69 73 20 61 6e 20 6f 63 63 75 72 65 6e 63 65 20  is an occurence 
320d1 6f 66 20 71 75 65 72 79 20 74 65 72 6d 20 69 4c  of query term iL
320d2 65 66 74 20 69 6e 20 74 68 65 20 64 6f 63 75 6d  eft in the docum
320d3 65 6e 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20  ent..           
320d4 20 2a 2a 20 49 74 20 6f 63 63 75 72 73 20 61 74   ** It occurs at
320d5 20 70 6f 73 69 74 69 6f 6e 20 28 70 2d 3e 69 54   position (p->iT
320d6 6f 6b 65 6e 29 20 6f 66 20 74 68 65 20 64 6f 63  oken) of the doc
320d7 75 6d 65 6e 74 2e 20 57 65 20 6e 6f 77 0a 20 20  ument. We now.  
320d8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 65 61            ** sea
320d9 72 63 68 20 66 6f 72 20 61 6e 20 69 6e 73 74 61  rch for an insta
320da 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 20 28 69 4c  nce of token (iL
320db 65 66 74 2d 31 29 20 73 6f 6d 65 77 68 65 72 65  eft-1) somewhere
320dc 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 20   in the .       
320dd 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 28 70       ** range (p
320de 2d 3e 69 54 6f 6b 65 6e 20 2d 20 6e 4e 65 61 72  ->iToken - nNear
320df 29 2e 2e 2e 28 70 2d 3e 69 54 6f 6b 65 6e 20 2b  )...(p->iToken +
320e0 20 6e 4e 65 61 72 20 2b 20 6e 54 6f 6b 65 6e 29   nNear + nToken)
320e1 20 77 69 74 68 69 6e 20 0a 20 20 20 20 20 20 20   within .       
320e2 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65 74 20       ** the set 
320e3 6f 66 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20  of snippetMatch 
320e4 73 74 72 75 63 74 75 72 65 73 2e 20 49 66 20 6f  structures. If o
320e5 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 70 72 6f  ne is found, pro
320e6 63 65 65 64 2e 20 0a 20 20 20 20 20 20 20 20 20  ceed. .         
320e7 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 63 61 6e     ** If one can
320e8 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 74 68  not be found, th
320e9 65 6e 20 72 65 6d 6f 76 65 20 73 6e 69 70 70 65  en remove snippe
320ea 74 73 20 69 69 2e 2e 28 69 69 2b 4e 2d 31 29 20  ts ii..(ii+N-1) 
320eb 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
320ec 66 72 6f 6d 20 74 68 65 20 6d 61 74 63 68 69 6e  from the matchin
320ed 67 20 73 6e 69 70 70 65 74 73 2c 20 77 68 65 72  g snippets, wher
320ee 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
320ef 72 20 6f 66 20 74 6f 6b 65 6e 73 20 0a 20 20 20  r of tokens .   
320f0 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 70           ** in p
320f1 68 72 61 73 65 20 70 52 69 67 68 74 2d 3e 70 50  hrase pRight->pP
320f2 68 72 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20  hrase..         
320f3 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
320f4 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b    for(jj=0; isOk
320f5 3d 3d 30 20 26 26 20 6a 6a 3c 70 53 6e 69 70 70  ==0 && jj<pSnipp
320f6 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b  et->nMatch; jj++
320f7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
320f8 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d   struct snippetM
320f9 61 74 63 68 20 2a 70 32 20 3d 20 26 70 53 6e 69  atch *p2 = &pSni
320fa 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d  ppet->aMatch[jj]
320fb 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
320fc 69 66 28 20 70 32 2d 3e 69 54 65 72 6d 3d 3d 28  if( p2->iTerm==(
320fd 69 4c 65 66 74 2d 31 29 20 29 7b 0a 20 20 20 20  iLeft-1) ){.    
320fe 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
320ff 70 32 2d 3e 69 54 6f 6b 65 6e 3e 3d 28 70 2d 3e  p2->iToken>=(p->
32100 69 54 6f 6b 65 6e 2d 6e 4e 65 61 72 2d 31 29 20  iToken-nNear-1) 
32101 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32102 20 20 26 26 20 70 32 2d 3e 69 54 6f 6b 65 6e 3c    && p2->iToken<
32103 28 70 2d 3e 69 54 6f 6b 65 6e 2b 6e 4e 65 61 72  (p->iToken+nNear
32104 2b 6e 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20  +nToken) .      
32105 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20            ){.   
32106 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
32107 73 4f 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  sOk = 1;.       
32108 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
32109 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3210a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3210b 20 20 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29       if( !isOk )
3210c 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
3210d 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20 20 20  int kk;.        
3210e 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d 30 3b 20        for(kk=0; 
3210f 6b 6b 3c 70 52 69 67 68 74 2d 3e 70 50 68 72 61  kk<pRight->pPhra
32110 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b  se->nToken; kk++
32111 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
32112 20 20 20 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61     pSnippet->aMa
32113 74 63 68 5b 6b 6b 2b 69 69 5d 2e 69 54 65 72 6d  tch[kk+ii].iTerm
32114 20 3d 20 2d 32 3b 0a 20 20 20 20 20 20 20 20 20   = -2;.         
32115 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
32116 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
32117 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
32118 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32119 20 20 20 69 66 28 20 70 2d 3e 69 54 65 72 6d 3d     if( p->iTerm=
3211a 3d 28 69 4c 65 66 74 2d 31 29 20 29 7b 0a 20 20  =(iLeft-1) ){.  
3211b 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 73            int is
3211c 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  Ok = 0;.        
3211d 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 69 73      for(jj=0; is
3211e 4f 6b 3d 3d 30 20 26 26 20 6a 6a 3c 70 53 6e 69  Ok==0 && jj<pSni
3211f 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 6a 6a  ppet->nMatch; jj
32120 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
32121 20 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65     struct snippe
32122 74 4d 61 74 63 68 20 2a 70 32 20 3d 20 26 70 53  tMatch *p2 = &pS
32123 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a  nippet->aMatch[j
32124 6a 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  j];.            
32125 20 20 69 66 28 20 70 32 2d 3e 69 54 65 72 6d 3d    if( p2->iTerm=
32126 3d 69 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  =iLeft ){.      
32127 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 32            if( p2
32128 2d 3e 69 54 6f 6b 65 6e 3c 3d 28 70 2d 3e 69 54  ->iToken<=(p->iT
32129 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 31 29 20 0a 20  oken+nNear+1) . 
3212a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3212b 26 26 20 70 32 2d 3e 69 54 6f 6b 65 6e 3e 28 70  && p2->iToken>(p
3212c 2d 3e 69 54 6f 6b 65 6e 2d 6e 4e 65 61 72 2d 6e  ->iToken-nNear-n
3212d 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20 20 20  Token) .        
3212e 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
3212f 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 4f               isO
32130 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  k = 1;.         
32131 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32132 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32133 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
32134 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a     if( !isOk ){.
32135 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
32136 74 20 6b 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  t kk;.          
32137 20 20 20 20 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b      for(kk=0; kk
32138 3c 70 4c 65 66 74 2d 3e 70 50 68 72 61 73 65 2d  <pLeft->pPhrase-
32139 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a  >nToken; kk++){.
3213a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3213b 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
3213c 5b 69 69 2d 6b 6b 5d 2e 69 54 65 72 6d 20 3d 20  [ii-kk].iTerm = 
3213d 2d 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -2;.            
3213e 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
3213f 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
32140 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
32141 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
32142 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32143 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
32144 20 20 69 66 28 20 74 72 69 6d 53 6e 69 70 70 65    if( trimSnippe
32145 74 4f 66 66 73 65 74 73 28 70 45 78 70 72 2d 3e  tOffsets(pExpr->
32146 70 52 69 67 68 74 2c 20 70 53 6e 69 70 70 65 74  pRight, pSnippet
32147 2c 20 70 69 4c 65 66 74 29 20 29 7b 0a 20 20 20  , piLeft) ){.   
32148 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
32149 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
3214a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  0;.}../*.** Comp
3214b 75 74 65 20 61 6c 6c 20 6f 66 66 73 65 74 73 20  ute all offsets 
3214c 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
3214d 72 6f 77 20 6f 66 20 74 68 65 20 71 75 65 72 79  row of the query
3214e 2e 20 20 0a 2a 2a 20 49 66 20 74 68 65 20 6f 66  .  .** If the of
3214f 66 73 65 74 73 20 68 61 76 65 20 61 6c 72 65 61  fsets have alrea
32150 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64  dy been computed
32151 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
32152 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
32153 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65  atic void snippe
32154 74 41 6c 6c 4f 66 66 73 65 74 73 28 66 75 6c 6c  tAllOffsets(full
32155 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70 29 7b  text_cursor *p){
32156 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 0a  .  int nColumn;.
32157 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69    int iColumn, i
32158 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20  ;.  int iFirst, 
32159 69 4c 61 73 74 3b 0a 20 20 69 6e 74 20 69 54 65  iLast;.  int iTe
3215a 72 6d 20 3d 20 30 3b 0a 20 20 66 75 6c 6c 74 65  rm = 0;.  fullte
3215b 78 74 5f 76 74 61 62 20 2a 70 46 74 73 20 3d 20  xt_vtab *pFts = 
3215c 63 75 72 73 6f 72 5f 76 74 61 62 28 70 29 3b 0a  cursor_vtab(p);.
3215d 0a 20 20 69 66 28 20 70 2d 3e 73 6e 69 70 70 65  .  if( p->snippe
3215e 74 2e 6e 4d 61 74 63 68 20 7c 7c 20 70 2d 3e 70  t.nMatch || p->p
3215f 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Expr==0 ){.    r
32160 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 43 6f  eturn;.  }.  nCo
32161 6c 75 6d 6e 20 3d 20 70 46 74 73 2d 3e 6e 43 6f  lumn = pFts->nCo
32162 6c 75 6d 6e 3b 0a 20 20 69 43 6f 6c 75 6d 6e 20  lumn;.  iColumn 
32163 3d 20 28 70 2d 3e 69 43 75 72 73 6f 72 54 79 70  = (p->iCursorTyp
32164 65 20 2d 20 51 55 45 52 59 5f 46 55 4c 4c 54 45  e - QUERY_FULLTE
32165 58 54 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75  XT);.  if( iColu
32166 6d 6e 3c 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3e  mn<0 || iColumn>
32167 3d 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  =nColumn ){.    
32168 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74 63  /* Look for matc
32169 68 65 73 20 6f 76 65 72 20 61 6c 6c 20 63 6f 6c  hes over all col
3216a 75 6d 6e 73 20 6f 66 20 74 68 65 20 66 75 6c 6c  umns of the full
3216b 2d 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20  -text index */. 
3216c 20 20 20 69 46 69 72 73 74 20 3d 20 30 3b 0a 20     iFirst = 0;. 
3216d 20 20 20 69 4c 61 73 74 20 3d 20 6e 43 6f 6c 75     iLast = nColu
3216e 6d 6e 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  mn-1;.  }else{. 
3216f 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 6d     /* Look for m
32170 61 74 63 68 65 73 20 69 6e 20 74 68 65 20 69 43  atches in the iC
32171 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20  olumn-th column 
32172 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 6c  of the index onl
32173 79 20 2a 2f 0a 20 20 20 20 69 46 69 72 73 74 20  y */.    iFirst 
32174 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69  = iColumn;.    i
32175 4c 61 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a  Last = iColumn;.
32176 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 46 69 72    }.  for(i=iFir
32177 73 74 3b 20 69 3c 3d 69 4c 61 73 74 3b 20 69 2b  st; i<=iLast; i+
32178 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  +){.    const ch
32179 61 72 20 2a 7a 44 6f 63 3b 0a 20 20 20 20 69 6e  ar *zDoc;.    in
3217a 74 20 6e 44 6f 63 3b 0a 20 20 20 20 7a 44 6f 63  t nDoc;.    zDoc
3217b 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
3217c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
3217d 65 78 74 28 70 2d 3e 70 53 74 6d 74 2c 20 69 2b  ext(p->pStmt, i+
3217e 31 29 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73  1);.    nDoc = s
3217f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
32180 74 65 73 28 70 2d 3e 70 53 74 6d 74 2c 20 69 2b  tes(p->pStmt, i+
32181 31 29 3b 0a 20 20 20 20 73 6e 69 70 70 65 74 4f  1);.    snippetO
32182 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 70  ffsetsOfColumn(p
32183 2c 20 26 70 2d 3e 73 6e 69 70 70 65 74 2c 20 69  , &p->snippet, i
32184 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 29 3b 0a 20  , zDoc, nDoc);. 
32185 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 74 72 69   }..  while( tri
32186 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73 28  mSnippetOffsets(
32187 70 2d 3e 70 45 78 70 72 2c 20 26 70 2d 3e 73 6e  p->pExpr, &p->sn
32188 69 70 70 65 74 2c 20 26 69 54 65 72 6d 29 20 29  ippet, &iTerm) )
32189 7b 0a 20 20 20 20 69 54 65 72 6d 20 3d 20 30 3b  {.    iTerm = 0;
3218a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
3218b 6e 76 65 72 74 20 74 68 65 20 69 6e 66 6f 72 6d  nvert the inform
3218c 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 61 4d 61  ation in the aMa
3218d 74 63 68 5b 5d 20 61 72 72 61 79 20 6f 66 20 74  tch[] array of t
3218e 68 65 20 73 6e 69 70 70 65 74 0a 2a 2a 20 69 6e  he snippet.** in
3218f 74 6f 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4f  to the string zO
32190 66 66 73 65 74 5b 30 2e 2e 6e 4f 66 66 73 65 74  ffset[0..nOffset
32191 2d 31 5d 2e 20 54 68 69 73 20 73 74 72 69 6e 67  -1]. This string
32192 20 69 73 20 75 73 65 64 20 61 73 0a 2a 2a 20 74   is used as.** t
32193 68 65 20 72 65 74 75 72 6e 20 6f 66 20 74 68 65  he return of the
32194 20 53 51 4c 20 6f 66 66 73 65 74 73 28 29 20 66   SQL offsets() f
32195 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
32196 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74 4f  ic void snippetO
32197 66 66 73 65 74 54 65 78 74 28 53 6e 69 70 70 65  ffsetText(Snippe
32198 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t *p){.  int i;.
32199 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
3219a 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62   StringBuffer sb
3219b 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30  ;.  char zBuf[20
3219c 30 5d 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 4f 66  0];.  if( p->zOf
3219d 66 73 65 74 20 29 20 72 65 74 75 72 6e 3b 0a 20  fset ) return;. 
3219e 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65   initStringBuffe
3219f 72 28 26 73 62 29 3b 0a 20 20 66 6f 72 28 69 3d  r(&sb);.  for(i=
321a0 30 3b 20 69 3c 70 2d 3e 6e 4d 61 74 63 68 3b 20  0; i<p->nMatch; 
321a1 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74  i++){.    struct
321a2 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70   snippetMatch *p
321a3 4d 61 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61 74  Match = &p->aMat
321a4 63 68 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  ch[i];.    if( p
321a5 4d 61 74 63 68 2d 3e 69 54 65 72 6d 3e 3d 30 20  Match->iTerm>=0 
321a6 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 73  ){.      /* If s
321a7 6e 69 70 70 65 74 4d 61 74 63 68 2e 69 54 65 72  nippetMatch.iTer
321a8 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 30  m is less than 0
321a9 2c 20 74 68 65 6e 20 74 68 65 20 6d 61 74 63 68  , then the match
321aa 20 77 61 73 20 0a 20 20 20 20 20 20 2a 2a 20 64   was .      ** d
321ab 69 73 63 61 72 64 65 64 20 61 73 20 70 61 72 74  iscarded as part
321ac 20 6f 66 20 70 72 6f 63 65 73 73 69 6e 67 20 74   of processing t
321ad 68 65 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72  he NEAR operator
321ae 20 28 73 65 65 20 74 68 65 20 0a 20 20 20 20 20   (see the .     
321af 20 2a 2a 20 74 72 69 6d 53 6e 69 70 70 65 74 4f   ** trimSnippetO
321b0 66 66 73 65 74 73 46 6f 72 4e 65 61 72 28 29 20  ffsetsForNear() 
321b1 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 64 65 74  function for det
321b2 61 69 6c 73 29 2e 20 49 67 6e 6f 72 65 20 0a 20  ails). Ignore . 
321b3 20 20 20 20 20 2a 2a 20 69 74 20 69 6e 20 74 68       ** it in th
321b4 69 73 20 63 61 73 65 0a 20 20 20 20 20 20 2a 2f  is case.      */
321b5 0a 20 20 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d  .      zBuf[0] =
321b6 20 27 20 27 3b 0a 20 20 20 20 20 20 73 71 6c 69   ' ';.      sqli
321b7 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
321b8 65 6f 66 28 7a 42 75 66 29 2d 31 2c 20 26 7a 42  eof(zBuf)-1, &zB
321b9 75 66 5b 63 6e 74 3e 30 5d 2c 20 22 25 64 20 25  uf[cnt>0], "%d %
321ba 64 20 25 64 20 25 64 22 2c 0a 20 20 20 20 20 20  d %d %d",.      
321bb 20 20 20 20 70 4d 61 74 63 68 2d 3e 69 43 6f 6c      pMatch->iCol
321bc 2c 20 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 2c  , pMatch->iTerm,
321bd 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72 74 2c   pMatch->iStart,
321be 20 70 4d 61 74 63 68 2d 3e 6e 42 79 74 65 29 3b   pMatch->nByte);
321bf 0a 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73  .      append(&s
321c0 62 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 20 20  b, zBuf);.      
321c1 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  cnt++;.    }.  }
321c2 0a 20 20 70 2d 3e 7a 4f 66 66 73 65 74 20 3d 20  .  p->zOffset = 
321c3 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
321c4 28 26 73 62 29 3b 0a 20 20 70 2d 3e 6e 4f 66 66  (&sb);.  p->nOff
321c5 73 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66  set = stringBuff
321c6 65 72 4c 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d  erLength(&sb);.}
321c7 0a 0a 2f 2a 0a 2a 2a 20 7a 44 6f 63 5b 30 2e 2e  ../*.** zDoc[0..
321c8 6e 44 6f 63 2d 31 5d 20 69 73 20 70 68 72 61 73  nDoc-1] is phras
321c9 65 20 6f 66 20 74 65 78 74 2e 20 20 61 4d 61 74  e of text.  aMat
321ca 63 68 5b 30 2e 2e 6e 4d 61 74 63 68 2d 31 5d 20  ch[0..nMatch-1] 
321cb 61 72 65 20 61 20 73 65 74 0a 2a 2a 20 6f 66 20  are a set.** of 
321cc 6d 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 73  matching words s
321cd 6f 6d 65 20 6f 66 20 77 68 69 63 68 20 6d 69 67  ome of which mig
321ce 68 74 20 62 65 20 69 6e 20 7a 44 6f 63 2e 20 20  ht be in zDoc.  
321cf 7a 44 6f 63 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a  zDoc is column.*
321d0 2a 20 6e 75 6d 62 65 72 20 69 43 6f 6c 2e 0a 2a  * number iCol..*
321d1 2a 0a 2a 2a 20 69 42 72 65 61 6b 20 69 73 20 73  *.** iBreak is s
321d2 75 67 67 65 73 74 65 64 20 73 70 6f 74 20 69 6e  uggested spot in
321d3 20 7a 44 6f 63 20 77 68 65 72 65 20 77 65 20 63   zDoc where we c
321d4 6f 75 6c 64 20 62 65 67 69 6e 20 6f 72 20 65 6e  ould begin or en
321d5 64 20 61 6e 0a 2a 2a 20 65 78 63 65 72 70 74 2e  d an.** excerpt.
321d6 20 20 52 65 74 75 72 6e 20 61 20 76 61 6c 75 65    Return a value
321d7 20 73 69 6d 69 6c 61 72 20 74 6f 20 69 42 72 65   similar to iBre
321d8 61 6b 20 62 75 74 20 70 6f 73 73 69 62 6c 79 20  ak but possibly 
321d9 61 64 6a 75 73 74 65 64 0a 2a 2a 20 74 6f 20 62  adjusted.** to b
321da 65 20 61 20 6c 69 74 74 6c 65 20 6c 65 66 74 20  e a little left 
321db 6f 72 20 72 69 67 68 74 20 73 6f 20 74 68 61 74  or right so that
321dc 20 74 68 65 20 62 72 65 61 6b 20 70 6f 69 6e 74   the break point
321dd 20 69 73 20 62 65 74 74 65 72 2e 0a 2a 2f 0a 73   is better..*/.s
321de 74 61 74 69 63 20 69 6e 74 20 77 6f 72 64 42 6f  tatic int wordBo
321df 75 6e 64 61 72 79 28 0a 20 20 69 6e 74 20 69 42  undary(.  int iB
321e0 72 65 61 6b 2c 20 20 20 20 20 20 20 20 20 20 20  reak,           
321e1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
321e2 75 67 67 65 73 74 65 64 20 62 72 65 61 6b 20 70  uggested break p
321e3 6f 69 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  oint */.  const 
321e4 63 68 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20 20  char *zDoc,     
321e5 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 75 6d          /* Docum
321e6 65 6e 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  ent text */.  in
321e7 74 20 6e 44 6f 63 2c 20 20 20 20 20 20 20 20 20  t nDoc,         
321e8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
321e9 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
321ea 6e 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 20 20 73 74  n zDoc[] */.  st
321eb 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
321ec 68 20 2a 61 4d 61 74 63 68 2c 20 20 2f 2a 20 4d  h *aMatch,  /* M
321ed 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 2a 2f  atching words */
321ee 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 2c 20 20  .  int nMatch,  
321ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321f0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
321f1 74 72 69 65 73 20 69 6e 20 61 4d 61 74 63 68 5b  tries in aMatch[
321f2 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 20  ] */.  int iCol 
321f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321f4 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75       /* The colu
321f5 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 7a 44  mn number for zD
321f6 6f 63 5b 5d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oc[] */.){.  int
321f7 20 69 3b 0a 20 20 69 66 28 20 69 42 72 65 61 6b   i;.  if( iBreak
321f8 3c 3d 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75  <=10 ){.    retu
321f9 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
321fa 69 42 72 65 61 6b 3e 3d 6e 44 6f 63 2d 31 30 20  iBreak>=nDoc-10 
321fb 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 44  ){.    return nD
321fc 6f 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  oc;.  }.  for(i=
321fd 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26 26 20 61  0; i<nMatch && a
321fe 4d 61 74 63 68 5b 69 5d 2e 69 43 6f 6c 3c 69 43  Match[i].iCol<iC
321ff 6f 6c 3b 20 69 2b 2b 29 7b 7d 0a 20 20 77 68 69  ol; i++){}.  whi
32200 6c 65 28 20 69 3c 6e 4d 61 74 63 68 20 26 26 20  le( i<nMatch && 
32201 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74  aMatch[i].iStart
32202 2b 61 4d 61 74 63 68 5b 69 5d 2e 6e 42 79 74 65  +aMatch[i].nByte
32203 3c 69 42 72 65 61 6b 20 29 7b 20 69 2b 2b 3b 20  <iBreak ){ i++; 
32204 7d 0a 20 20 69 66 28 20 69 3c 6e 4d 61 74 63 68  }.  if( i<nMatch
32205 20 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74   ){.    if( aMat
32206 63 68 5b 69 5d 2e 69 53 74 61 72 74 3c 69 42 72  ch[i].iStart<iBr
32207 65 61 6b 2b 31 30 20 29 7b 0a 20 20 20 20 20 20  eak+10 ){.      
32208 72 65 74 75 72 6e 20 61 4d 61 74 63 68 5b 69 5d  return aMatch[i]
32209 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a 20  .iStart;.    }. 
3220a 20 20 20 69 66 28 20 69 3e 30 20 26 26 20 61 4d     if( i>0 && aM
3220b 61 74 63 68 5b 69 2d 31 5d 2e 69 53 74 61 72 74  atch[i-1].iStart
3220c 2b 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 6e 42 79  +aMatch[i-1].nBy
3220d 74 65 3e 3d 69 42 72 65 61 6b 20 29 7b 0a 20 20  te>=iBreak ){.  
3220e 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 74 63      return aMatc
3220f 68 5b 69 2d 31 5d 2e 69 53 74 61 72 74 3b 0a 20  h[i-1].iStart;. 
32210 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
32211 3d 31 3b 20 69 3c 3d 31 30 3b 20 69 2b 2b 29 7b  =1; i<=10; i++){
32212 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 69 73  .    if( safe_is
32213 73 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61  space(zDoc[iBrea
32214 6b 2d 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72  k-i]) ){.      r
32215 65 74 75 72 6e 20 69 42 72 65 61 6b 20 2d 20 69  eturn iBreak - i
32216 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   + 1;.    }.    
32217 69 66 28 20 73 61 66 65 5f 69 73 73 70 61 63 65  if( safe_isspace
32218 28 7a 44 6f 63 5b 69 42 72 65 61 6b 2b 69 5d 29  (zDoc[iBreak+i])
32219 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
3221a 20 69 42 72 65 61 6b 20 2b 20 69 20 2b 20 31 3b   iBreak + i + 1;
3221b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
3221c 75 72 6e 20 69 42 72 65 61 6b 3b 0a 7d 0a 0a 0a  urn iBreak;.}...
3221d 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
3221e 61 6c 75 65 73 20 66 6f 72 20 53 6e 69 70 70 65  alues for Snippe
3221f 74 2e 61 4d 61 74 63 68 5b 5d 2e 73 6e 53 74 61  t.aMatch[].snSta
32220 74 75 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  tus.*/.#define S
32221 4e 49 50 50 45 54 5f 49 47 4e 4f 52 45 20 20 30  NIPPET_IGNORE  0
32222 20 20 20 2f 2a 20 49 74 20 69 73 20 6f 6b 20 74     /* It is ok t
32223 6f 20 6f 6d 69 74 20 74 68 69 73 20 6d 61 74 63  o omit this matc
32224 68 20 66 72 6f 6d 20 74 68 65 20 73 6e 69 70 70  h from the snipp
32225 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 4e  et */.#define SN
32226 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 31 20  IPPET_DESIRED 1 
32227 20 20 2f 2a 20 57 65 20 77 61 6e 74 20 74 6f 20    /* We want to 
32228 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6d 61 74  include this mat
32229 63 68 20 69 6e 20 74 68 65 20 73 6e 69 70 70 65  ch in the snippe
3222a 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  t */../*.** Gene
3222b 72 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66  rate the text of
3222c 20 61 20 73 6e 69 70 70 65 74 2e 0a 2a 2f 0a 73   a snippet..*/.s
3222d 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70  tatic void snipp
3222e 65 74 54 65 78 74 28 0a 20 20 66 75 6c 6c 74 65  etText(.  fullte
3222f 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  xt_cursor *pCurs
32230 6f 72 2c 20 20 20 2f 2a 20 54 68 65 20 63 75 72  or,   /* The cur
32231 73 6f 72 20 77 65 20 6e 65 65 64 20 74 68 65 20  sor we need the 
32232 73 6e 69 70 70 65 74 20 66 6f 72 20 2a 2f 0a 20  snippet for */. 
32233 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
32234 61 72 74 4d 61 72 6b 2c 20 20 20 20 20 2f 2a 20  artMark,     /* 
32235 4d 61 72 6b 75 70 20 74 6f 20 61 70 70 65 61 72  Markup to appear
32236 20 62 65 66 6f 72 65 20 65 61 63 68 20 6d 61 74   before each mat
32237 63 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ch */.  const ch
32238 61 72 20 2a 7a 45 6e 64 4d 61 72 6b 2c 20 20 20  ar *zEndMark,   
32239 20 20 20 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f      /* Markup to
3223a 20 61 70 70 65 61 72 20 61 66 74 65 72 20 65 61   appear after ea
3223b 63 68 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f  ch match */.  co
3223c 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70  nst char *zEllip
3223d 73 69 73 20 20 20 20 20 20 20 2f 2a 20 45 6c 6c  sis       /* Ell
3223e 69 70 73 69 73 20 6d 61 72 6b 20 2a 2f 0a 29 7b  ipsis mark */.){
3223f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73  .  int i, j;.  s
32240 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74  truct snippetMat
32241 63 68 20 2a 61 4d 61 74 63 68 3b 0a 20 20 69 6e  ch *aMatch;.  in
32242 74 20 6e 4d 61 74 63 68 3b 0a 20 20 69 6e 74 20  t nMatch;.  int 
32243 6e 44 65 73 69 72 65 64 3b 0a 20 20 53 74 72 69  nDesired;.  Stri
32244 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69  ngBuffer sb;.  i
32245 6e 74 20 74 61 69 6c 43 6f 6c 3b 0a 20 20 69 6e  nt tailCol;.  in
32246 74 20 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20 20  t tailOffset;.  
32247 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74 20  int iCol;.  int 
32248 6e 44 6f 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68  nDoc;.  const ch
32249 61 72 20 2a 7a 44 6f 63 3b 0a 20 20 69 6e 74 20  ar *zDoc;.  int 
3224a 69 53 74 61 72 74 2c 20 69 45 6e 64 3b 0a 20 20  iStart, iEnd;.  
3224b 69 6e 74 20 74 61 69 6c 45 6c 6c 69 70 73 69 73  int tailEllipsis
3224c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4d 61 74   = 0;.  int iMat
3224d 63 68 3b 0a 20 20 0a 0a 20 20 73 71 6c 69 74 65  ch;.  ..  sqlite
3224e 33 5f 66 72 65 65 28 70 43 75 72 73 6f 72 2d 3e  3_free(pCursor->
3224f 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74  snippet.zSnippet
32250 29 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e  );.  pCursor->sn
32251 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d  ippet.zSnippet =
32252 20 30 3b 0a 20 20 61 4d 61 74 63 68 20 3d 20 70   0;.  aMatch = p
32253 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
32254 61 4d 61 74 63 68 3b 0a 20 20 6e 4d 61 74 63 68  aMatch;.  nMatch
32255 20 3d 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70   = pCursor->snip
32256 70 65 74 2e 6e 4d 61 74 63 68 3b 0a 20 20 69 6e  pet.nMatch;.  in
32257 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26  itStringBuffer(&
32258 73 62 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  sb);..  for(i=0;
32259 20 69 3c 6e 4d 61 74 63 68 3b 20 69 2b 2b 29 7b   i<nMatch; i++){
3225a 0a 20 20 20 20 61 4d 61 74 63 68 5b 69 5d 2e 73  .    aMatch[i].s
3225b 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
3225c 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 7d 0a 20 20  T_IGNORE;.  }.  
3225d 6e 44 65 73 69 72 65 64 20 3d 20 30 3b 0a 20 20  nDesired = 0;.  
3225e 66 6f 72 28 69 3d 30 3b 20 69 3c 46 54 53 33 5f  for(i=0; i<FTS3_
3225f 52 4f 54 4f 52 5f 53 5a 3b 20 69 2b 2b 29 7b 0a  ROTOR_SZ; i++){.
32260 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e      for(j=0; j<n
32261 4d 61 74 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20 20  Match; j++){.   
32262 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 6a 5d     if( aMatch[j]
32263 2e 69 54 65 72 6d 3d 3d 69 20 29 7b 0a 20 20 20  .iTerm==i ){.   
32264 20 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73       aMatch[j].s
32265 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
32266 54 5f 44 45 53 49 52 45 44 3b 0a 20 20 20 20 20  T_DESIRED;.     
32267 20 20 20 6e 44 65 73 69 72 65 64 2b 2b 3b 0a 20     nDesired++;. 
32268 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32269 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
3226a 0a 20 20 69 4d 61 74 63 68 20 3d 20 30 3b 0a 20  .  iMatch = 0;. 
3226b 20 74 61 69 6c 43 6f 6c 20 3d 20 2d 31 3b 0a 20   tailCol = -1;. 
3226c 20 74 61 69 6c 4f 66 66 73 65 74 20 3d 20 30 3b   tailOffset = 0;
3226d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d  .  for(i=0; i<nM
3226e 61 74 63 68 20 26 26 20 6e 44 65 73 69 72 65 64  atch && nDesired
3226f 3e 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  >0; i++){.    if
32270 28 20 61 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74  ( aMatch[i].snSt
32271 61 74 75 73 21 3d 53 4e 49 50 50 45 54 5f 44 45  atus!=SNIPPET_DE
32272 53 49 52 45 44 20 29 20 63 6f 6e 74 69 6e 75 65  SIRED ) continue
32273 3b 0a 20 20 20 20 6e 44 65 73 69 72 65 64 2d 2d  ;.    nDesired--
32274 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 61 4d 61  ;.    iCol = aMa
32275 74 63 68 5b 69 5d 2e 69 43 6f 6c 3b 0a 20 20 20  tch[i].iCol;.   
32276 20 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63   zDoc = (const c
32277 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  har*)sqlite3_col
32278 75 6d 6e 5f 74 65 78 74 28 70 43 75 72 73 6f 72  umn_text(pCursor
32279 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29  ->pStmt, iCol+1)
3227a 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c  ;.    nDoc = sql
3227b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
3227c 73 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d 74  s(pCursor->pStmt
3227d 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 69  , iCol+1);.    i
3227e 53 74 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69  Start = aMatch[i
3227f 5d 2e 69 53 74 61 72 74 20 2d 20 34 30 3b 0a 20  ].iStart - 40;. 
32280 20 20 20 69 53 74 61 72 74 20 3d 20 77 6f 72 64     iStart = word
32281 42 6f 75 6e 64 61 72 79 28 69 53 74 61 72 74 2c  Boundary(iStart,
32282 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 61 4d 61   zDoc, nDoc, aMa
32283 74 63 68 2c 20 6e 4d 61 74 63 68 2c 20 69 43 6f  tch, nMatch, iCo
32284 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 53 74 61  l);.    if( iSta
32285 72 74 3c 3d 31 30 20 29 7b 0a 20 20 20 20 20 20  rt<=10 ){.      
32286 69 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 20 20  iStart = 0;.    
32287 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d  }.    if( iCol==
32288 74 61 69 6c 43 6f 6c 20 26 26 20 69 53 74 61 72  tailCol && iStar
32289 74 3c 3d 74 61 69 6c 4f 66 66 73 65 74 2b 32 30  t<=tailOffset+20
3228a 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74   ){.      iStart
3228b 20 3d 20 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20   = tailOffset;. 
3228c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 69 43     }.    if( (iC
3228d 6f 6c 21 3d 74 61 69 6c 43 6f 6c 20 26 26 20 74  ol!=tailCol && t
3228e 61 69 6c 43 6f 6c 3e 3d 30 29 20 7c 7c 20 69 53  ailCol>=0) || iS
3228f 74 61 72 74 21 3d 74 61 69 6c 4f 66 66 73 65 74  tart!=tailOffset
32290 20 29 7b 0a 20 20 20 20 20 20 74 72 69 6d 57 68   ){.      trimWh
32291 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20  iteSpace(&sb);. 
32292 20 20 20 20 20 61 70 70 65 6e 64 57 68 69 74 65       appendWhite
32293 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20  Space(&sb);.    
32294 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45    append(&sb, zE
32295 6c 6c 69 70 73 69 73 29 3b 0a 20 20 20 20 20 20  llipsis);.      
32296 61 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65  appendWhiteSpace
32297 28 26 73 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20  (&sb);.    }.   
32298 20 69 45 6e 64 20 3d 20 61 4d 61 74 63 68 5b 69   iEnd = aMatch[i
32299 5d 2e 69 53 74 61 72 74 20 2b 20 61 4d 61 74 63  ].iStart + aMatc
3229a 68 5b 69 5d 2e 6e 42 79 74 65 20 2b 20 34 30 3b  h[i].nByte + 40;
3229b 0a 20 20 20 20 69 45 6e 64 20 3d 20 77 6f 72 64  .    iEnd = word
3229c 42 6f 75 6e 64 61 72 79 28 69 45 6e 64 2c 20 7a  Boundary(iEnd, z
3229d 44 6f 63 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63  Doc, nDoc, aMatc
3229e 68 2c 20 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29  h, nMatch, iCol)
3229f 3b 0a 20 20 20 20 69 66 28 20 69 45 6e 64 3e 3d  ;.    if( iEnd>=
322a0 6e 44 6f 63 2d 31 30 20 29 7b 0a 20 20 20 20 20  nDoc-10 ){.     
322a1 20 69 45 6e 64 20 3d 20 6e 44 6f 63 3b 0a 20 20   iEnd = nDoc;.  
322a2 20 20 20 20 74 61 69 6c 45 6c 6c 69 70 73 69 73      tailEllipsis
322a3 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
322a4 0a 20 20 20 20 20 20 74 61 69 6c 45 6c 6c 69 70  .      tailEllip
322a5 73 69 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  sis = 1;.    }. 
322a6 20 20 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68     while( iMatch
322a7 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63  <nMatch && aMatc
322a8 68 5b 69 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c 69  h[iMatch].iCol<i
322a9 43 6f 6c 20 29 7b 20 69 4d 61 74 63 68 2b 2b 3b  Col ){ iMatch++;
322aa 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53   }.    while( iS
322ab 74 61 72 74 3c 69 45 6e 64 20 29 7b 0a 20 20 20  tart<iEnd ){.   
322ac 20 20 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68     while( iMatch
322ad 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63  <nMatch && aMatc
322ae 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74  h[iMatch].iStart
322af 3c 69 53 74 61 72 74 0a 20 20 20 20 20 20 20 20  <iStart.        
322b0 20 20 20 20 20 26 26 20 61 4d 61 74 63 68 5b 69       && aMatch[i
322b1 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c 3d 69 43 6f  Match].iCol<=iCo
322b2 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4d 61  l ){.        iMa
322b3 74 63 68 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  tch++;.      }. 
322b4 20 20 20 20 20 69 66 28 20 69 4d 61 74 63 68 3c       if( iMatch<
322b5 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68  nMatch && aMatch
322b6 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3c  [iMatch].iStart<
322b7 69 45 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20  iEnd.           
322b8 20 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74    && aMatch[iMat
322b9 63 68 5d 2e 69 43 6f 6c 3d 3d 69 43 6f 6c 20 29  ch].iCol==iCol )
322ba 7b 0a 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e  {.        nappen
322bb 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74  d(&sb, &zDoc[iSt
322bc 61 72 74 5d 2c 20 61 4d 61 74 63 68 5b 69 4d 61  art], aMatch[iMa
322bd 74 63 68 5d 2e 69 53 74 61 72 74 20 2d 20 69 53  tch].iStart - iS
322be 74 61 72 74 29 3b 0a 20 20 20 20 20 20 20 20 69  tart);.        i
322bf 53 74 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69  Start = aMatch[i
322c0 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3b 0a 20  Match].iStart;. 
322c1 20 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73         append(&s
322c2 62 2c 20 7a 53 74 61 72 74 4d 61 72 6b 29 3b 0a  b, zStartMark);.
322c3 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28          nappend(
322c4 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72  &sb, &zDoc[iStar
322c5 74 5d 2c 20 61 4d 61 74 63 68 5b 69 4d 61 74 63  t], aMatch[iMatc
322c6 68 5d 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  h].nByte);.     
322c7 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a     append(&sb, z
322c8 45 6e 64 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20  EndMark);.      
322c9 20 20 69 53 74 61 72 74 20 2b 3d 20 61 4d 61 74    iStart += aMat
322ca 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42 79 74 65  ch[iMatch].nByte
322cb 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
322cc 69 4d 61 74 63 68 2b 31 3b 20 6a 3c 6e 4d 61 74  iMatch+1; j<nMat
322cd 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ch; j++){.      
322ce 20 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 6a      if( aMatch[j
322cf 5d 2e 69 54 65 72 6d 3d 3d 61 4d 61 74 63 68 5b  ].iTerm==aMatch[
322d0 69 4d 61 74 63 68 5d 2e 69 54 65 72 6d 0a 20 20  iMatch].iTerm.  
322d1 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61              && a
322d2 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75  Match[j].snStatu
322d3 73 3d 3d 53 4e 49 50 50 45 54 5f 44 45 53 49 52  s==SNIPPET_DESIR
322d4 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ED ){.          
322d5 20 20 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20    nDesired--;.  
322d6 20 20 20 20 20 20 20 20 20 20 61 4d 61 74 63 68            aMatch
322d7 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53  [j].snStatus = S
322d8 4e 49 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a 20  NIPPET_IGNORE;. 
322d9 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
322da 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
322db 7b 0a 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e  {.        nappen
322dc 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74  d(&sb, &zDoc[iSt
322dd 61 72 74 5d 2c 20 69 45 6e 64 20 2d 20 69 53 74  art], iEnd - iSt
322de 61 72 74 29 3b 0a 20 20 20 20 20 20 20 20 69 53  art);.        iS
322df 74 61 72 74 20 3d 20 69 45 6e 64 3b 0a 20 20 20  tart = iEnd;.   
322e0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74     }.    }.    t
322e1 61 69 6c 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20  ailCol = iCol;. 
322e2 20 20 20 74 61 69 6c 4f 66 66 73 65 74 20 3d 20     tailOffset = 
322e3 69 45 6e 64 3b 0a 20 20 7d 0a 20 20 74 72 69 6d  iEnd;.  }.  trim
322e4 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
322e5 0a 20 20 69 66 28 20 74 61 69 6c 45 6c 6c 69 70  .  if( tailEllip
322e6 73 69 73 20 29 7b 0a 20 20 20 20 61 70 70 65 6e  sis ){.    appen
322e7 64 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29  dWhiteSpace(&sb)
322e8 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62  ;.    append(&sb
322e9 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20  , zEllipsis);.  
322ea 7d 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69  }.  pCursor->sni
322eb 70 70 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d 20  ppet.zSnippet = 
322ec 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
322ed 28 26 73 62 29 3b 0a 20 20 70 43 75 72 73 6f 72  (&sb);.  pCursor
322ee 2d 3e 73 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70  ->snippet.nSnipp
322ef 65 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65  et = stringBuffe
322f0 72 4c 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d 0a  rLength(&sb);.}.
322f1 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68  ../*.** Close th
322f2 65 20 63 75 72 73 6f 72 2e 20 20 46 6f 72 20 61  e cursor.  For a
322f3 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
322f4 61 74 69 6f 6e 20 73 65 65 20 74 68 65 20 64 6f  ation see the do
322f5 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f  cumentation.** o
322f6 6e 20 74 68 65 20 78 43 6c 6f 73 65 20 6d 65 74  n the xClose met
322f7 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74 75  hod of the virtu
322f8 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 72 66 61  al table interfa
322f9 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
322fa 74 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 28  t fulltextClose(
322fb 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
322fc 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
322fd 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
322fe 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *c = (fulltext_
322ff 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
32300 72 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  r;.  FTSTRACE(("
32301 46 54 53 33 20 43 6c 6f 73 65 20 25 70 5c 6e 22  FTS3 Close %p\n"
32302 2c 20 63 29 29 3b 0a 20 20 73 71 6c 69 74 65 33  , c));.  sqlite3
32303 5f 66 69 6e 61 6c 69 7a 65 28 63 2d 3e 70 53 74  _finalize(c->pSt
32304 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74  mt);.  sqlite3Ft
32305 73 33 45 78 70 72 46 72 65 65 28 63 2d 3e 70 45  s3ExprFree(c->pE
32306 78 70 72 29 3b 0a 20 20 73 6e 69 70 70 65 74 43  xpr);.  snippetC
32307 6c 65 61 72 28 26 63 2d 3e 73 6e 69 70 70 65 74  lear(&c->snippet
32308 29 3b 0a 20 20 69 66 28 20 63 2d 3e 72 65 73 75  );.  if( c->resu
32309 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20  lt.nData!=0 ){. 
3230a 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 63     dlrDestroy(&c
3230b 2d 3e 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  ->reader);.  }. 
3230c 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
3230d 6f 79 28 26 63 2d 3e 72 65 73 75 6c 74 29 3b 0a  oy(&c->result);.
3230e 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
3230f 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
32310 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63  TE_OK;.}..static
32311 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 4e 65 78   int fulltextNex
32312 74 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  t(sqlite3_vtab_c
32313 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
32314 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
32315 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78  or *c = (fulltex
32316 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  t_cursor *) pCur
32317 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  sor;.  int rc;..
32318 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
32319 33 20 4e 65 78 74 20 25 70 5c 6e 22 2c 20 70 43  3 Next %p\n", pC
3231a 75 72 73 6f 72 29 29 3b 0a 20 20 73 6e 69 70 70  ursor));.  snipp
3231b 65 74 43 6c 65 61 72 28 26 63 2d 3e 73 6e 69 70  etClear(&c->snip
3231c 70 65 74 29 3b 0a 20 20 69 66 28 20 63 2d 3e 69  pet);.  if( c->i
3231d 43 75 72 73 6f 72 54 79 70 65 20 3c 20 51 55 45  CursorType < QUE
3231e 52 59 5f 46 55 4c 4c 54 45 58 54 20 29 7b 0a 20  RY_FULLTEXT ){. 
3231f 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
32320 29 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f  ) Handle SQLITE_
32321 53 43 48 45 4d 41 20 41 4e 44 20 53 51 4c 49 54  SCHEMA AND SQLIT
32322 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 72  E_BUSY. */.    r
32323 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
32324 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20  (c->pStmt);.    
32325 73 77 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20  switch( rc ){.  
32326 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
32327 52 4f 57 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e  ROW:.        c->
32328 65 6f 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  eof = 0;.       
32329 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3232a 4b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  K;.      case SQ
3232b 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20 20 20 20 20  LITE_DONE:.     
3232c 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20     c->eof = 1;. 
3232d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
3232e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 64  LITE_OK;.      d
3232f 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20  efault:.        
32330 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20  c->eof = 1;.    
32331 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
32332 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 20     }.  } else { 
32333 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20 71 75   /* full-text qu
32334 65 72 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ery */.    rc = 
32335 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 63 2d  sqlite3_reset(c-
32336 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28  >pStmt);.    if(
32337 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32338 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
32339 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e   if( c->result.n
3233a 44 61 74 61 3d 3d 30 20 7c 7c 20 64 6c 72 41 74  Data==0 || dlrAt
3233b 45 6e 64 28 26 63 2d 3e 72 65 61 64 65 72 29 20  End(&c->reader) 
3233c 29 7b 0a 20 20 20 20 20 20 63 2d 3e 65 6f 66 20  ){.      c->eof 
3233d 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
3233e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
3233f 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
32340 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 63  te3_bind_int64(c
32341 2d 3e 70 53 74 6d 74 2c 20 31 2c 20 64 6c 72 44  ->pStmt, 1, dlrD
32342 6f 63 69 64 28 26 63 2d 3e 72 65 61 64 65 72 29  ocid(&c->reader)
32343 29 3b 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26  );.    dlrStep(&
32344 63 2d 3e 72 65 61 64 65 72 29 3b 0a 20 20 20 20  c->reader);.    
32345 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
32346 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
32347 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
32348 29 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f  ) Handle SQLITE_
32349 53 43 48 45 4d 41 20 41 4e 44 20 53 51 4c 49 54  SCHEMA AND SQLIT
3234a 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 72  E_BUSY. */.    r
3234b 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
3234c 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20  (c->pStmt);.    
3234d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
3234e 4f 57 20 29 7b 20 20 20 2f 2a 20 74 68 65 20 63  OW ){   /* the c
3234f 61 73 65 20 77 65 20 65 78 70 65 63 74 20 2a 2f  ase we expect */
32350 0a 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20  .      c->eof = 
32351 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
32352 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
32353 0a 20 20 20 20 2f 2a 20 61 6e 20 65 72 72 6f 72  .    /* an error
32354 20 6f 63 63 75 72 72 65 64 3b 20 61 62 6f 72 74   occurred; abort
32355 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 72   */.    return r
32356 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f  c==SQLITE_DONE ?
32357 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20   SQLITE_ERROR : 
32358 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 54  rc;.  }.}.../* T
32359 4f 44 4f 28 73 68 65 73 73 29 20 49 66 20 77 65  ODO(shess) If we
3235a 20 70 75 73 68 65 64 20 4c 65 61 66 52 65 61 64   pushed LeafRead
3235b 65 72 20 74 6f 20 74 68 65 20 74 6f 70 20 6f 66  er to the top of
3235c 20 74 68 65 20 66 69 6c 65 2c 20 6f 72 20 74 6f   the file, or to
3235d 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 66 69 6c 65  .** another file
3235e 2c 20 74 65 72 6d 5f 73 65 6c 65 63 74 28 29 20  , term_select() 
3235f 63 6f 75 6c 64 20 62 65 20 70 75 73 68 65 64 20  could be pushed 
32360 61 62 6f 76 65 0a 2a 2a 20 64 6f 63 4c 69 73 74  above.** docList
32361 4f 66 54 65 72 6d 28 29 2e 0a 2a 2f 0a 73 74 61  OfTerm()..*/.sta
32362 74 69 63 20 69 6e 74 20 74 65 72 6d 53 65 6c 65  tic int termSele
32363 63 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ct(fulltext_vtab
32364 20 2a 76 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e   *v, int iColumn
32365 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32366 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
32367 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
32368 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
32369 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
3236a 20 20 20 20 20 20 20 20 20 20 44 6f 63 4c 69 73            DocLis
3236b 74 54 79 70 65 20 69 54 79 70 65 2c 20 44 61 74  tType iType, Dat
3236c 61 42 75 66 66 65 72 20 2a 6f 75 74 29 3b 0a 0a  aBuffer *out);..
3236d 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  /* .** Return a 
3236e 44 6f 63 4c 69 73 74 20 63 6f 72 72 65 73 70 6f  DocList correspo
3236f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 68 72  nding to the phr
32370 61 73 65 20 2a 70 50 68 72 61 73 65 2e 0a 2a 2a  ase *pPhrase..**
32371 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 69 6e  .** The resultin
32372 67 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c  g DL_DOCIDS docl
32373 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ist is stored in
32374 20 70 52 65 73 75 6c 74 2c 20 77 68 69 63 68 20   pResult, which 
32375 69 73 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65  is.** overwritte
32376 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
32377 20 64 6f 63 4c 69 73 74 4f 66 50 68 72 61 73 65   docListOfPhrase
32378 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  (.  fulltext_vta
32379 62 20 2a 70 54 61 62 2c 20 20 20 2f 2a 20 54 68  b *pTab,   /* Th
3237a 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65  e full text inde
3237b 78 20 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73  x */.  Fts3Phras
3237c 65 20 2a 70 50 68 72 61 73 65 2c 20 20 20 2f 2a  e *pPhrase,   /*
3237d 20 50 68 72 61 73 65 20 74 6f 20 72 65 74 75 72   Phrase to retur
3237e 6e 20 61 20 64 6f 63 6c 69 73 74 20 63 6f 72 72  n a doclist corr
3237f 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 2a 2f 0a  esponding to */.
32380 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 65 4c    DocListType eL
32381 69 73 74 54 79 70 65 2c 20 2f 2a 20 45 69 74 68  istType, /* Eith
32382 65 72 20 44 4c 5f 44 4f 43 49 44 53 20 6f 72 20  er DL_DOCIDS or 
32383 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 2a 2f 0a  DL_POSITIONS */.
32384 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 52    DataBuffer *pR
32385 65 73 75 6c 74 20 20 20 20 2f 2a 20 57 72 69 74  esult    /* Writ
32386 65 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72  e the result her
32387 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 69  e */.){.  int ii
32388 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
32389 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 43  ITE_OK;.  int iC
3238a 6f 6c 20 3d 20 70 50 68 72 61 73 65 2d 3e 69 43  ol = pPhrase->iC
3238b 6f 6c 75 6d 6e 3b 0a 20 20 44 6f 63 4c 69 73 74  olumn;.  DocList
3238c 54 79 70 65 20 65 54 79 70 65 20 3d 20 65 4c 69  Type eType = eLi
3238d 73 74 54 79 70 65 3b 0a 20 20 61 73 73 65 72 74  stType;.  assert
3238e 28 20 65 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ( eType==DL_POSI
3238f 54 49 4f 4e 53 20 7c 7c 20 65 54 79 70 65 3d 3d  TIONS || eType==
32390 44 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 20 20 69  DL_DOCIDS );.  i
32391 66 28 20 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  f( pPhrase->nTok
32392 65 6e 3e 31 20 29 7b 0a 20 20 20 20 65 54 79 70  en>1 ){.    eTyp
32393 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  e = DL_POSITIONS
32394 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73  ;.  }..  /* This
32395 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76   code should nev
32396 65 72 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  er be called wit
32397 68 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  h buffered updat
32398 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  es. */.  assert(
32399 20 70 54 61 62 2d 3e 6e 50 65 6e 64 69 6e 67 44   pTab->nPendingD
3239a 61 74 61 3c 30 20 29 3b 0a 0a 20 20 66 6f 72 28  ata<0 );..  for(
3239b 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  ii=0; rc==SQLITE
3239c 5f 4f 4b 20 26 26 20 69 69 3c 70 50 68 72 61 73  _OK && ii<pPhras
3239d 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 69 2b 2b 29  e->nToken; ii++)
3239e 7b 0a 20 20 20 20 44 61 74 61 42 75 66 66 65 72  {.    DataBuffer
3239f 20 74 6d 70 3b 0a 20 20 20 20 73 74 72 75 63 74   tmp;.    struct
323a0 20 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 20   PhraseToken *p 
323a1 3d 20 26 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  = &pPhrase->aTok
323a2 65 6e 5b 69 69 5d 3b 0a 20 20 20 20 72 63 20 3d  en[ii];.    rc =
323a3 20 74 65 72 6d 53 65 6c 65 63 74 28 70 54 61 62   termSelect(pTab
323a4 2c 20 69 43 6f 6c 2c 20 70 2d 3e 7a 2c 20 70 2d  , iCol, p->z, p-
323a5 3e 6e 2c 20 70 2d 3e 69 73 50 72 65 66 69 78 2c  >n, p->isPrefix,
323a6 20 65 54 79 70 65 2c 20 26 74 6d 70 29 3b 0a 20   eType, &tmp);. 
323a7 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
323a8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66  E_OK ){.      if
323a9 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ( ii==0 ){.     
323aa 20 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 74 6d     *pResult = tm
323ab 70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  p;.      }else{.
323ac 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66          DataBuff
323ad 65 72 20 72 65 73 20 3d 20 2a 70 52 65 73 75 6c  er res = *pResul
323ae 74 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  t;.        dataB
323af 75 66 66 65 72 49 6e 69 74 28 70 52 65 73 75 6c  ufferInit(pResul
323b0 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  t, 0);.        i
323b1 66 28 20 69 69 3d 3d 28 70 50 68 72 61 73 65 2d  f( ii==(pPhrase-
323b2 3e 6e 54 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20 20  >nToken-1) ){.  
323b3 20 20 20 20 20 20 20 20 65 54 79 70 65 20 3d 20          eType = 
323b4 65 4c 69 73 74 54 79 70 65 3b 0a 20 20 20 20 20  eListType;.     
323b5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 6f 63     }.        doc
323b6 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
323b7 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 2e 70  .          res.p
323b8 44 61 74 61 2c 20 72 65 73 2e 6e 44 61 74 61 2c  Data, res.nData,
323b9 20 74 6d 70 2e 70 44 61 74 61 2c 20 74 6d 70 2e   tmp.pData, tmp.
323ba 6e 44 61 74 61 2c 20 30 2c 20 30 2c 20 65 54 79  nData, 0, 0, eTy
323bb 70 65 2c 20 70 52 65 73 75 6c 74 0a 20 20 20 20  pe, pResult.    
323bc 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 64      );.        d
323bd 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
323be 28 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  (&res);.        
323bf 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
323c0 79 28 26 74 6d 70 29 3b 0a 20 20 20 20 20 20 7d  y(&tmp);.      }
323c1 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
323c2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
323c3 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 66  * Evaluate the f
323c4 75 6c 6c 2d 74 65 78 74 20 65 78 70 72 65 73 73  ull-text express
323c5 69 6f 6e 20 70 45 78 70 72 20 61 67 61 69 6e 73  ion pExpr agains
323c6 74 20 66 74 73 33 20 74 61 62 6c 65 20 70 54 61  t fts3 table pTa
323c7 62 2e 20 57 72 69 74 65 0a 2a 2a 20 74 68 65 20  b. Write.** the 
323c8 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 70 52 65  results into pRe
323c9 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
323ca 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 0a 20   evalFts3Expr(. 
323cb 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
323cc 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  pTab,           
323cd 2f 2a 20 46 74 73 33 20 56 69 72 74 75 61 6c 20  /* Fts3 Virtual 
323ce 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 2a 2f 0a  table object */.
323cf 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70    Fts3Expr *pExp
323d0 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
323d1 20 2f 2a 20 50 61 72 73 65 64 20 66 74 73 33 20   /* Parsed fts3 
323d2 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
323d3 44 61 74 61 42 75 66 66 65 72 20 2a 70 52 65 73  DataBuffer *pRes
323d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
323d5 2a 20 4f 55 54 3a 20 57 72 69 74 65 20 72 65 73  * OUT: Write res
323d6 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78 70 72  ults of the expr
323d7 65 73 73 69 6f 6e 20 68 65 72 65 20 2a 2f 0a 29  ession here */.)
323d8 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
323d9 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 6e  ITE_OK;..  /* In
323da 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 75 74  itialize the out
323db 70 75 74 20 62 75 66 66 65 72 2e 20 49 66 20 74  put buffer. If t
323dc 68 69 73 20 69 73 20 61 6e 20 65 6d 70 74 79 20  his is an empty 
323dd 71 75 65 72 79 20 28 70 45 78 70 72 3d 3d 30 29  query (pExpr==0)
323de 2c 20 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20  , .  ** this is 
323df 61 6c 6c 20 74 68 61 74 20 6e 65 65 64 73 20 74  all that needs t
323e0 6f 20 62 65 20 64 6f 6e 65 2e 20 45 6d 70 74 79  o be done. Empty
323e1 20 71 75 65 72 69 65 73 20 70 72 6f 64 75 63 65   queries produce
323e2 20 65 6d 70 74 79 20 0a 20 20 2a 2a 20 72 65 73   empty .  ** res
323e3 75 6c 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20  ult sets..  */. 
323e4 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
323e5 70 52 65 73 2c 20 30 29 3b 0a 0a 20 20 69 66 28  pRes, 0);..  if(
323e6 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66   pExpr ){.    if
323e7 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d  ( pExpr->eType==
323e8 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
323e9 29 7b 0a 20 20 20 20 20 20 44 6f 63 4c 69 73 74  ){.      DocList
323ea 54 79 70 65 20 65 54 79 70 65 20 3d 20 44 4c 5f  Type eType = DL_
323eb 44 4f 43 49 44 53 3b 0a 20 20 20 20 20 20 69 66  DOCIDS;.      if
323ec 28 20 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74  ( pExpr->pParent
323ed 20 26 26 20 70 45 78 70 72 2d 3e 70 50 61 72 65   && pExpr->pPare
323ee 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  nt->eType==FTSQU
323ef 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20  ERY_NEAR ){.    
323f0 20 20 20 20 65 54 79 70 65 20 3d 20 44 4c 5f 50      eType = DL_P
323f1 4f 53 49 54 49 4f 4e 53 3b 0a 20 20 20 20 20 20  OSITIONS;.      
323f2 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 64 6f 63  }.      rc = doc
323f3 4c 69 73 74 4f 66 50 68 72 61 73 65 28 70 54 61  ListOfPhrase(pTa
323f4 62 2c 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73  b, pExpr->pPhras
323f5 65 2c 20 65 54 79 70 65 2c 20 70 52 65 73 29 3b  e, eType, pRes);
323f6 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
323f7 20 20 44 61 74 61 42 75 66 66 65 72 20 6c 68 73    DataBuffer lhs
323f8 3b 0a 20 20 20 20 20 20 44 61 74 61 42 75 66 66  ;.      DataBuff
323f9 65 72 20 72 68 73 3b 0a 0a 20 20 20 20 20 20 64  er rhs;..      d
323fa 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 72  ataBufferInit(&r
323fb 68 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  hs, 0);.      if
323fc 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
323fd 20 3d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28   = evalFts3Expr(
323fe 70 54 61 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65  pTab, pExpr->pLe
323ff 66 74 2c 20 26 6c 68 73 29 29 20 0a 20 20 20 20  ft, &lhs)) .    
32400 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
32401 3d 28 72 63 20 3d 20 65 76 61 6c 46 74 73 33 45  =(rc = evalFts3E
32402 78 70 72 28 70 54 61 62 2c 20 70 45 78 70 72 2d  xpr(pTab, pExpr-
32403 3e 70 52 69 67 68 74 2c 20 26 72 68 73 29 29 20  >pRight, &rhs)) 
32404 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
32405 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
32406 3e 65 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20  >eType ){.      
32407 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
32408 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20  Y_NEAR: {.      
32409 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e        int nToken
3240a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 46 74  ;.            Ft
3240b 73 33 45 78 70 72 20 2a 70 4c 65 66 74 3b 0a 20  s3Expr *pLeft;. 
3240c 20 20 20 20 20 20 20 20 20 20 20 44 6f 63 4c 69             DocLi
3240d 73 74 54 79 70 65 20 65 54 79 70 65 20 3d 20 44  stType eType = D
3240e 4c 5f 44 4f 43 49 44 53 3b 0a 20 20 20 20 20 20  L_DOCIDS;.      
3240f 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
32410 3e 70 50 61 72 65 6e 74 20 26 26 20 70 45 78 70  >pParent && pExp
32411 72 2d 3e 70 50 61 72 65 6e 74 2d 3e 65 54 79 70  r->pParent->eTyp
32412 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
32413 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
32414 20 20 65 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53    eType = DL_POS
32415 49 54 49 4f 4e 53 3b 0a 20 20 20 20 20 20 20 20  ITIONS;.        
32416 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
32417 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d    pLeft = pExpr-
32418 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20  >pLeft;.        
32419 20 20 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74      while( pLeft
3241a 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
3241b 59 5f 4e 45 41 52 20 29 7b 20 0a 20 20 20 20 20  Y_NEAR ){ .     
3241c 20 20 20 20 20 20 20 20 20 70 4c 65 66 74 3d 70           pLeft=p
3241d 4c 65 66 74 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Left->pRight;.  
3241e 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
3241f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
32420 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 65  pExpr->pRight->e
32421 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50  Type==FTSQUERY_P
32422 48 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20  HRASE );.       
32423 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65       assert( pLe
32424 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  ft->eType==FTSQU
32425 45 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20  ERY_PHRASE );.  
32426 20 20 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e            nToken
32427 20 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72 61 73   = pLeft->pPhras
32428 65 2d 3e 6e 54 6f 6b 65 6e 20 2b 20 70 45 78 70  e->nToken + pExp
32429 72 2d 3e 70 52 69 67 68 74 2d 3e 70 50 68 72 61  r->pRight->pPhra
3242a 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20  se->nToken;.    
3242b 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 50          docListP
3242c 68 72 61 73 65 4d 65 72 67 65 28 6c 68 73 2e 70  hraseMerge(lhs.p
3242d 44 61 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c  Data, lhs.nData,
3242e 20 72 68 73 2e 70 44 61 74 61 2c 20 72 68 73 2e   rhs.pData, rhs.
3242f 6e 44 61 74 61 2c 20 0a 20 20 20 20 20 20 20 20  nData, .        
32430 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6e          pExpr->n
32431 4e 65 61 72 2b 31 2c 20 6e 54 6f 6b 65 6e 2c 20  Near+1, nToken, 
32432 65 54 79 70 65 2c 20 70 52 65 73 0a 20 20 20 20  eType, pRes.    
32433 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
32434 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32435 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
32436 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
32437 59 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 20  Y_NOT: {.       
32438 20 20 20 20 20 64 6f 63 4c 69 73 74 45 78 63 65       docListExce
32439 70 74 4d 65 72 67 65 28 6c 68 73 2e 70 44 61 74  ptMerge(lhs.pDat
3243a 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72 68  a, lhs.nData, rh
3243b 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e 44 61  s.pData, rhs.nDa
3243c 74 61 2c 70 52 65 73 29 3b 0a 20 20 20 20 20 20  ta,pRes);.      
3243d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3243e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3243f 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
32440 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 20 20  _AND: {.        
32441 20 20 20 20 64 6f 63 4c 69 73 74 41 6e 64 4d 65      docListAndMe
32442 72 67 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c  rge(lhs.pData, l
32443 68 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44  hs.nData, rhs.pD
32444 61 74 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 20  ata, rhs.nData, 
32445 70 52 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20  pRes);.         
32446 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32447 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
32448 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52  case FTSQUERY_OR
32449 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
3244a 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67 65 28 6c  docListOrMerge(l
3244b 68 73 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44  hs.pData, lhs.nD
3244c 61 74 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20  ata, rhs.pData, 
3244d 72 68 73 2e 6e 44 61 74 61 2c 20 70 52 65 73 29  rhs.nData, pRes)
3244e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
3244f 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
32450 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
32451 20 7d 0a 20 20 20 20 20 20 64 61 74 61 42 75 66   }.      dataBuf
32452 66 65 72 44 65 73 74 72 6f 79 28 26 6c 68 73 29  ferDestroy(&lhs)
32453 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
32454 65 72 44 65 73 74 72 6f 79 28 26 72 68 73 29 3b  erDestroy(&rhs);
32455 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
32456 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54  turn rc;.}../* T
32457 4f 44 4f 28 73 68 65 73 73 29 20 52 65 66 61 63  ODO(shess) Refac
32458 74 6f 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20  tor the code to 
32459 72 65 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77  remove this forw
3245a 61 72 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61  ard decl. */.sta
3245b 74 69 63 20 69 6e 74 20 66 6c 75 73 68 50 65 6e  tic int flushPen
3245c 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65  dingTerms(fullte
3245d 78 74 5f 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a  xt_vtab *v);../*
3245e 20 50 65 72 66 6f 72 6d 20 61 20 66 75 6c 6c 2d   Perform a full-
3245f 74 65 78 74 20 71 75 65 72 79 20 75 73 69 6e 67  text query using
32460 20 74 68 65 20 73 65 61 72 63 68 20 65 78 70 72   the search expr
32461 65 73 73 69 6f 6e 20 69 6e 0a 2a 2a 20 7a 49 6e  ession in.** zIn
32462 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d  put[0..nInput-1]
32463 2e 20 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74  .  Return a list
32464 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 64 6f 63   of matching doc
32465 75 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 70 52 65  uments.** in pRe
32466 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 51 75 65 72  sult..**.** Quer
32467 69 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 63  ies must match c
32468 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 20 20  olumn iColumn.  
32469 4f 72 20 69 66 20 69 43 6f 6c 75 6d 6e 3e 3d 6e  Or if iColumn>=n
3246a 43 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 65 79 20 61  Column.** they a
3246b 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61  re allowed to ma
3246c 74 63 68 20 61 67 61 69 6e 73 74 20 61 6e 79 20  tch against any 
3246d 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
3246e 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 51 75  c int fulltextQu
3246f 65 72 79 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ery(.  fulltext_
32470 76 74 61 62 20 2a 76 2c 20 20 20 20 20 20 2f 2a  vtab *v,      /*
32471 20 54 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69   The full text i
32472 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ndex */.  int iC
32473 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
32474 20 2f 2a 20 4d 61 74 63 68 20 61 67 61 69 6e 73   /* Match agains
32475 74 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 62 79  t this column by
32476 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 63 6f   default */.  co
32477 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
32478 2c 20 20 20 20 2f 2a 20 54 68 65 20 71 75 65 72  ,    /* The quer
32479 79 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e  y string */.  in
3247a 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20  t nInput,       
3247b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3247c 66 20 62 79 74 65 73 20 69 6e 20 7a 49 6e 70 75  f bytes in zInpu
3247d 74 5b 5d 20 2a 2f 0a 20 20 44 61 74 61 42 75 66  t[] */.  DataBuf
3247e 66 65 72 20 2a 70 52 65 73 75 6c 74 2c 20 20 20  fer *pResult,   
3247f 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73  /* Write the res
32480 75 6c 74 20 64 6f 63 6c 69 73 74 20 68 65 72 65  ult doclist here
32481 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
32482 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20 2f  *ppExpr        /
32483 2a 20 50 75 74 20 70 61 72 73 65 64 20 71 75 65  * Put parsed que
32484 72 79 20 73 74 72 69 6e 67 20 68 65 72 65 20 2a  ry string here *
32485 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  /.){.  int rc;..
32486 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
32487 20 49 6e 73 74 65 61 64 20 6f 66 20 66 6c 75 73   Instead of flus
32488 68 69 6e 67 20 70 65 6e 64 69 6e 67 54 65 72 6d  hing pendingTerm
32489 73 2c 20 77 65 20 63 6f 75 6c 64 20 71 75 65 72  s, we could quer
3248a 79 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72  y for.  ** the r
3248b 65 6c 65 76 61 6e 74 20 74 65 72 6d 20 61 6e 64  elevant term and
3248c 20 6d 65 72 67 65 20 74 68 65 20 64 6f 63 6c 69   merge the docli
3248d 73 74 20 69 6e 74 6f 20 77 68 61 74 20 77 65 20  st into what we 
3248e 72 65 63 65 69 76 65 20 66 72 6f 6d 0a 20 20 2a  receive from.  *
3248f 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  * the database. 
32490 20 57 61 69 74 20 61 6e 64 20 73 65 65 20 69 66   Wait and see if
32491 20 74 68 69 73 20 69 73 20 61 20 63 6f 6d 6d 6f   this is a commo
32492 6e 20 69 73 73 75 65 2c 20 66 69 72 73 74 2e 0a  n issue, first..
32493 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 67 6f 6f 64    **.  ** A good
32494 20 72 65 61 73 6f 6e 20 6e 6f 74 20 74 6f 20 66   reason not to f
32495 6c 75 73 68 20 69 73 20 74 6f 20 6e 6f 74 20 67  lush is to not g
32496 65 6e 65 72 61 74 65 20 75 70 64 61 74 65 2d 72  enerate update-r
32497 65 6c 61 74 65 64 0a 20 20 2a 2a 20 65 72 72 6f  elated.  ** erro
32498 72 20 63 6f 64 65 73 20 66 72 6f 6d 20 68 65 72  r codes from her
32499 65 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 46 6c  e..  */..  /* Fl
3249a 75 73 68 20 61 6e 79 20 62 75 66 66 65 72 65 64  ush any buffered
3249b 20 75 70 64 61 74 65 73 20 62 65 66 6f 72 65 20   updates before 
3249c 65 78 65 63 75 74 69 6e 67 20 74 68 65 20 71 75  executing the qu
3249d 65 72 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66  ery. */.  rc = f
3249e 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73  lushPendingTerms
3249f 28 76 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  (v);.  if( rc!=S
324a0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
324a1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
324a2 20 20 2f 2a 20 50 61 72 73 65 20 74 68 65 20 71    /* Parse the q
324a3 75 65 72 79 20 70 61 73 73 65 64 20 74 6f 20 74  uery passed to t
324a4 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f  he MATCH operato
324a5 72 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  r. */.  rc = sql
324a6 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73  ite3Fts3ExprPars
324a7 65 28 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2c  e(v->pTokenizer,
324a8 20 0a 20 20 20 20 20 20 76 2d 3e 61 7a 43 6f 6c   .      v->azCol
324a9 75 6d 6e 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  umn, v->nColumn,
324aa 20 69 43 6f 6c 75 6d 6e 2c 20 7a 49 6e 70 75 74   iColumn, zInput
324ab 2c 20 6e 49 6e 70 75 74 2c 20 70 70 45 78 70 72  , nInput, ppExpr
324ac 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  .  );.  if( rc!=
324ad 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
324ae 20 61 73 73 65 72 74 28 20 30 3d 3d 28 2a 70 70   assert( 0==(*pp
324af 45 78 70 72 29 20 29 3b 0a 20 20 20 20 72 65 74  Expr) );.    ret
324b0 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72  urn rc;.  }..  r
324b1 65 74 75 72 6e 20 65 76 61 6c 46 74 73 33 45 78  eturn evalFts3Ex
324b2 70 72 28 76 2c 20 2a 70 70 45 78 70 72 2c 20 70  pr(v, *ppExpr, p
324b3 52 65 73 75 6c 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Result);.}../*.*
324b4 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 78 46  * This is the xF
324b5 69 6c 74 65 72 20 69 6e 74 65 72 66 61 63 65 20  ilter interface 
324b6 66 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20  for the virtual 
324b7 74 61 62 6c 65 2e 20 20 53 65 65 0a 2a 2a 20 74  table.  See.** t
324b8 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
324b9 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20   xFilter method 
324ba 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  documentation fo
324bb 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  r additional.** 
324bc 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
324bd 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d 51 55  ** If idxNum==QU
324be 45 52 59 5f 47 45 4e 45 52 49 43 20 74 68 65 6e  ERY_GENERIC then
324bf 20 64 6f 20 61 20 66 75 6c 6c 20 74 61 62 6c 65   do a full table
324c0 20 73 63 61 6e 20 61 67 61 69 6e 73 74 0a 2a 2a   scan against.**
324c1 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
324c2 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  able..**.** If i
324c3 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 44 4f 43  dxNum==QUERY_DOC
324c4 49 44 20 74 68 65 6e 20 64 6f 20 61 20 64 6f 63  ID then do a doc
324c5 69 64 20 6c 6f 6f 6b 75 70 20 66 6f 72 20 61 20  id lookup for a 
324c6 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20  single entry.** 
324c7 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  in the %_content
324c8 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
324c9 20 69 64 78 4e 75 6d 3e 3d 51 55 45 52 59 5f 46   idxNum>=QUERY_F
324ca 55 4c 4c 54 45 58 54 20 74 68 65 6e 20 75 73 65  ULLTEXT then use
324cb 20 74 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69   the full text i
324cc 6e 64 65 78 2e 20 20 54 68 65 0a 2a 2a 20 63 6f  ndex.  The.** co
324cd 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 6c 65 66 74  lumn on the left
324ce 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
324cf 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72  e MATCH operator
324d0 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75   is column.** nu
324d1 6d 62 65 72 20 69 64 78 4e 75 6d 2d 51 55 45 52  mber idxNum-QUER
324d2 59 5f 46 55 4c 4c 54 45 58 54 2c 20 30 20 69 6e  Y_FULLTEXT, 0 in
324d3 64 65 78 65 64 2e 20 20 61 72 67 76 5b 30 5d 20  dexed.  argv[0] 
324d4 69 73 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  is the right-han
324d5 64 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65  d.** side of the
324d6 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e   MATCH operator.
324d7 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
324d8 73 29 20 55 70 67 72 61 64 65 20 74 68 65 20 63  s) Upgrade the c
324d9 75 72 73 6f 72 20 69 6e 69 74 69 61 6c 69 7a 61  ursor initializa
324da 74 69 6f 6e 20 61 6e 64 20 64 65 73 74 72 75 63  tion and destruc
324db 74 69 6f 6e 20 74 6f 0a 2a 2a 20 61 63 63 6f 75  tion to.** accou
324dc 6e 74 20 66 6f 72 20 66 75 6c 6c 74 65 78 74 46  nt for fulltextF
324dd 69 6c 74 65 72 28 29 20 62 65 69 6e 67 20 63 61  ilter() being ca
324de 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  lled multiple ti
324df 6d 65 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 61  mes on the.** sa
324e0 6d 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20  me cursor.  The 
324e1 63 75 72 72 65 6e 74 20 73 6f 6c 75 74 69 6f 6e  current solution
324e2 20 69 73 20 76 65 72 79 20 66 72 61 67 69 6c 65   is very fragile
324e3 2e 20 20 41 70 70 6c 79 20 66 69 78 20 74 6f 0a  .  Apply fix to.
324e4 2a 2a 20 66 74 73 33 20 61 73 20 61 70 70 72 6f  ** fts3 as appro
324e5 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  priate..*/.stati
324e6 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 46 69  c int fulltextFi
324e7 6c 74 65 72 28 0a 20 20 73 71 6c 69 74 65 33 5f  lter(.  sqlite3_
324e8 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
324e9 72 73 6f 72 2c 20 20 20 20 20 2f 2a 20 54 68 65  rsor,     /* The
324ea 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72   cursor used for
324eb 20 74 68 69 73 20 71 75 65 72 79 20 2a 2f 0a 20   this query */. 
324ec 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
324ed 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
324ee 20 20 20 2f 2a 20 57 68 69 63 68 20 69 6e 64 65     /* Which inde
324ef 78 69 6e 67 20 73 63 68 65 6d 65 20 74 6f 20 75  xing scheme to u
324f0 73 65 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63  se */.  int argc
324f1 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
324f2 2a 2a 61 72 67 76 20 20 20 20 2f 2a 20 41 72 67  **argv    /* Arg
324f3 75 6d 65 6e 74 73 20 66 6f 72 20 74 68 65 20 69  uments for the i
324f4 6e 64 65 78 69 6e 67 20 73 63 68 65 6d 65 20 2a  ndexing scheme *
324f5 2f 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  /.){.  fulltext_
324f6 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c  cursor *c = (ful
324f7 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20  ltext_cursor *) 
324f8 70 43 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c 74  pCursor;.  fullt
324f9 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 63 75  ext_vtab *v = cu
324fa 72 73 6f 72 5f 76 74 61 62 28 63 29 3b 0a 20 20  rsor_vtab(c);.  
324fb 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52  int rc;..  FTSTR
324fc 41 43 45 28 28 22 46 54 53 33 20 46 69 6c 74 65  ACE(("FTS3 Filte
324fd 72 20 25 70 5c 6e 22 2c 70 43 75 72 73 6f 72 29  r %p\n",pCursor)
324fe 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
324ff 63 75 72 73 6f 72 20 68 61 73 20 61 20 73 74 61  cursor has a sta
32500 74 65 6d 65 6e 74 20 74 68 61 74 20 77 61 73 20  tement that was 
32501 6e 6f 74 20 70 72 65 70 61 72 65 64 20 61 63 63  not prepared acc
32502 6f 72 64 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69  ording to.  ** i
32503 64 78 4e 75 6d 2c 20 63 6c 65 61 72 20 69 74 2e  dxNum, clear it.
32504 20 20 49 20 62 65 6c 69 65 76 65 20 61 6c 6c 20    I believe all 
32505 63 61 6c 6c 73 20 74 6f 20 66 75 6c 6c 74 65 78  calls to fulltex
32506 74 46 69 6c 74 65 72 20 77 69 74 68 20 61 0a 20  tFilter with a. 
32507 20 2a 2a 20 67 69 76 65 6e 20 63 75 72 73 6f 72   ** given cursor
32508 20 77 69 6c 6c 20 68 61 76 65 20 74 68 65 20 73   will have the s
32509 61 6d 65 20 69 64 78 4e 75 6d 20 2c 20 62 75 74  ame idxNum , but
3250a 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74   in this case it
3250b 27 73 0a 20 20 2a 2a 20 65 61 73 79 20 74 6f 20  's.  ** easy to 
3250c 62 65 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20 20  be safe..  */.  
3250d 69 66 28 20 63 2d 3e 70 53 74 6d 74 20 26 26 20  if( c->pStmt && 
3250e 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 21 3d  c->iCursorType!=
3250f 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20 73 71  idxNum ){.    sq
32510 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 63  lite3_finalize(c
32511 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 63 2d  ->pStmt);.    c-
32512 3e 70 53 74 6d 74 20 3d 20 4e 55 4c 4c 3b 0a 20  >pStmt = NULL;. 
32513 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 66   }..  /* Get a f
32514 72 65 73 68 20 73 74 61 74 65 6d 65 6e 74 20 61  resh statement a
32515 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 69 64  ppropriate to id
32516 78 4e 75 6d 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f  xNum. */.  /* TO
32517 44 4f 28 73 68 65 73 73 29 3a 20 41 64 64 20 61  DO(shess): Add a
32518 20 70 72 65 70 61 72 65 64 2d 73 74 61 74 65 6d   prepared-statem
32519 65 6e 74 20 63 61 63 68 65 20 69 6e 20 74 68 65  ent cache in the
3251a 20 76 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20   vt structure.. 
3251b 20 2a 2a 20 54 68 65 20 63 61 63 68 65 20 6d 75   ** The cache mu
3251c 73 74 20 68 61 6e 64 6c 65 20 6d 75 6c 74 69 70  st handle multip
3251d 6c 65 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e  le open cursors.
3251e 20 20 45 61 73 69 65 72 20 74 6f 20 63 61 63 68    Easier to cach
3251f 65 20 74 68 65 0a 20 20 2a 2a 20 73 74 61 74 65  e the.  ** state
32520 6d 65 6e 74 20 76 61 72 69 61 6e 74 73 20 61 74  ment variants at
32521 20 74 68 65 20 76 74 20 74 6f 20 72 65 64 75 63   the vt to reduc
32522 65 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63  e malloc/realloc
32523 2f 66 72 65 65 20 68 65 72 65 2e 0a 20 20 2a 2a  /free here..  **
32524 20 4f 72 20 77 65 20 63 6f 75 6c 64 20 68 61 76   Or we could hav
32525 65 20 61 20 53 74 72 69 6e 67 42 75 66 66 65 72  e a StringBuffer
32526 20 76 61 72 69 61 6e 74 20 77 68 69 63 68 20 61   variant which a
32527 6c 6c 6f 77 65 64 20 73 74 61 63 6b 0a 20 20 2a  llowed stack.  *
32528 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 66  * construction f
32529 6f 72 20 73 6d 61 6c 6c 20 76 61 6c 75 65 73 2e  or small values.
3252a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 2d 3e  .  */.  if( !c->
3252b 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 53 74 72  pStmt ){.    Str
3252c 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20  ingBuffer sb;.  
3252d 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66    initStringBuff
3252e 65 72 28 26 73 62 29 3b 0a 20 20 20 20 61 70 70  er(&sb);.    app
3252f 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45 43 54  end(&sb, "SELECT
32530 20 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20 20 20   docid, ");.    
32531 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20  appendList(&sb, 
32532 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61  v->nColumn, v->a
32533 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b  zContentColumn);
32534 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c  .    append(&sb,
32535 20 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e   " FROM %_conten
32536 74 22 29 3b 0a 20 20 20 20 69 66 28 20 69 64 78  t");.    if( idx
32537 4e 75 6d 21 3d 51 55 45 52 59 5f 47 45 4e 45 52  Num!=QUERY_GENER
32538 49 43 20 29 20 61 70 70 65 6e 64 28 26 73 62 2c  IC ) append(&sb,
32539 20 22 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d   " WHERE docid =
3253a 20 3f 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 73   ?");.    rc = s
3253b 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
3253c 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
3253d 6d 65 2c 20 26 63 2d 3e 70 53 74 6d 74 2c 0a 20  me, &c->pStmt,. 
3253e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3253f 20 20 20 20 73 74 72 69 6e 67 42 75 66 66 65 72      stringBuffer
32540 44 61 74 61 28 26 73 62 29 29 3b 0a 20 20 20 20  Data(&sb));.    
32541 73 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74  stringBufferDest
32542 72 6f 79 28 26 73 62 29 3b 0a 20 20 20 20 69 66  roy(&sb);.    if
32543 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
32544 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
32545 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20   c->iCursorType 
32546 3d 20 69 64 78 4e 75 6d 3b 0a 20 20 7d 65 6c 73  = idxNum;.  }els
32547 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  e{.    sqlite3_r
32548 65 73 65 74 28 63 2d 3e 70 53 74 6d 74 29 3b 0a  eset(c->pStmt);.
32549 20 20 20 20 61 73 73 65 72 74 28 20 63 2d 3e 69      assert( c->i
3254a 43 75 72 73 6f 72 54 79 70 65 3d 3d 69 64 78 4e  CursorType==idxN
3254b 75 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  um );.  }..  swi
3254c 74 63 68 28 20 69 64 78 4e 75 6d 20 29 7b 0a 20  tch( idxNum ){. 
3254d 20 20 20 63 61 73 65 20 51 55 45 52 59 5f 47 45     case QUERY_GE
3254e 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 62 72 65  NERIC:.      bre
3254f 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 51 55  ak;..    case QU
32550 45 52 59 5f 44 4f 43 49 44 3a 0a 20 20 20 20 20  ERY_DOCID:.     
32551 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
32552 6e 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d  nd_int64(c->pStm
32553 74 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61  t, 1, sqlite3_va
32554 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
32555 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ]));.      if( r
32556 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
32557 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
32558 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61  break;..    defa
32559 75 6c 74 3a 20 20 20 2f 2a 20 66 75 6c 6c 2d 74  ult:   /* full-t
3255a 65 78 74 20 73 65 61 72 63 68 20 2a 2f 0a 20 20  ext search */.  
3255b 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43    {.      int iC
3255c 6f 6c 20 3d 20 69 64 78 4e 75 6d 2d 51 55 45 52  ol = idxNum-QUER
3255d 59 5f 46 55 4c 4c 54 45 58 54 3b 0a 20 20 20 20  Y_FULLTEXT;.    
3255e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 51    const char *zQ
3255f 75 65 72 79 20 3d 20 28 63 6f 6e 73 74 20 63 68  uery = (const ch
32560 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
32561 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
32562 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
32563 69 64 78 4e 75 6d 3c 3d 51 55 45 52 59 5f 46 55  idxNum<=QUERY_FU
32564 4c 4c 54 45 58 54 2b 76 2d 3e 6e 43 6f 6c 75 6d  LLTEXT+v->nColum
32565 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  n);.      assert
32566 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 20  ( argc==1 );.   
32567 20 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74     if( c->result
32568 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20  .nData!=0 ){.   
32569 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73       /* This cas
3256a 65 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 65  e happens if the
3256b 20 73 61 6d 65 20 63 75 72 73 6f 72 20 69 73 20   same cursor is 
3256c 75 73 65 64 20 72 65 70 65 61 74 65 64 6c 79 2e  used repeatedly.
3256d 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 6c 72 44   */.        dlrD
3256e 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 61 64 65  estroy(&c->reade
3256f 72 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61  r);.        data
32570 42 75 66 66 65 72 52 65 73 65 74 28 26 63 2d 3e  BufferReset(&c->
32571 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d  result);.      }
32572 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 61  else{.        da
32573 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 63 2d  taBufferInit(&c-
32574 3e 72 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 20  >result, 0);.   
32575 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
32576 66 75 6c 6c 74 65 78 74 51 75 65 72 79 28 76 2c  fulltextQuery(v,
32577 20 69 43 6f 6c 2c 20 7a 51 75 65 72 79 2c 20 2d   iCol, zQuery, -
32578 31 2c 20 26 63 2d 3e 72 65 73 75 6c 74 2c 20 26  1, &c->result, &
32579 63 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  c->pExpr);.     
3257a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3257b 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
3257c 20 20 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73        if( c->res
3257d 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a  ult.nData!=0 ){.
3257e 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28          dlrInit(
3257f 26 63 2d 3e 72 65 61 64 65 72 2c 20 44 4c 5f 44  &c->reader, DL_D
32580 4f 43 49 44 53 2c 20 63 2d 3e 72 65 73 75 6c 74  OCIDS, c->result
32581 2e 70 44 61 74 61 2c 20 63 2d 3e 72 65 73 75 6c  .pData, c->resul
32582 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  t.nData);.      
32583 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
32584 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
32585 72 6e 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 28  rn fulltextNext(
32586 70 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20  pCursor);.}../* 
32587 54 68 69 73 20 69 73 20 74 68 65 20 78 45 6f 66  This is the xEof
32588 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76   method of the v
32589 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
3258a 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a  he SQLite core.*
3258b 2a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  * calls this rou
3258c 74 69 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74  tine to find out
3258d 20 69 66 20 69 74 20 68 61 73 20 72 65 61 63 68   if it has reach
3258e 65 64 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a  ed the end of.**
3258f 20 61 20 71 75 65 72 79 27 73 20 72 65 73 75 6c   a query's resul
32590 74 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  ts set..*/.stati
32591 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 45 6f  c int fulltextEo
32592 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  f(sqlite3_vtab_c
32593 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
32594 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
32595 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78  or *c = (fulltex
32596 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  t_cursor *) pCur
32597 73 6f 72 3b 0a 20 20 72 65 74 75 72 6e 20 63 2d  sor;.  return c-
32598 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73  >eof;.}../* This
32599 20 69 73 20 74 68 65 20 78 43 6f 6c 75 6d 6e 20   is the xColumn 
3259a 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69  method of the vi
3259b 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
3259c 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65  e SQLite.** core
3259d 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68   calls this meth
3259e 6f 64 20 64 75 72 69 6e 67 20 61 20 71 75 65 72  od during a quer
3259f 79 20 77 68 65 6e 20 69 74 20 6e 65 65 64 73 20  y when it needs 
325a0 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
325a1 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68  a column from th
325a2 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
325a3 20 20 54 68 69 73 20 6d 65 74 68 6f 64 20 6e 65    This method ne
325a4 65 64 73 20 74 6f 20 75 73 65 0a 2a 2a 20 6f 6e  eds to use.** on
325a5 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  e of the sqlite3
325a6 5f 72 65 73 75 6c 74 5f 2a 28 29 20 72 6f 75 74  _result_*() rout
325a7 69 6e 65 73 20 74 6f 20 73 74 6f 72 65 20 74 68  ines to store th
325a8 65 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 76  e requested.** v
325a9 61 6c 75 65 20 62 61 63 6b 20 69 6e 20 74 68 65  alue back in the
325aa 20 70 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74   pContext..*/.st
325ab 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
325ac 74 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f  tColumn(sqlite3_
325ad 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
325ae 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  rsor,.          
325af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325b0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
325b1 2a 70 43 6f 6e 74 65 78 74 2c 20 69 6e 74 20 69  *pContext, int i
325b2 64 78 43 6f 6c 29 7b 0a 20 20 66 75 6c 6c 74 65  dxCol){.  fullte
325b3 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  xt_cursor *c = (
325b4 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
325b5 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 66 75  *) pCursor;.  fu
325b6 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
325b7 20 63 75 72 73 6f 72 5f 76 74 61 62 28 63 29 3b   cursor_vtab(c);
325b8 0a 0a 20 20 69 66 28 20 69 64 78 43 6f 6c 3c 76  ..  if( idxCol<v
325b9 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
325ba 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
325bb 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63  pVal = sqlite3_c
325bc 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 63 2d 3e 70  olumn_value(c->p
325bd 53 74 6d 74 2c 20 69 64 78 43 6f 6c 2b 31 29 3b  Stmt, idxCol+1);
325be 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
325bf 75 6c 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65  ult_value(pConte
325c0 78 74 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 65 6c  xt, pVal);.  }el
325c1 73 65 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76  se if( idxCol==v
325c2 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
325c3 20 2f 2a 20 54 68 65 20 65 78 74 72 61 20 63 6f   /* The extra co
325c4 6c 75 6d 6e 20 77 68 6f 73 65 20 6e 61 6d 65 20  lumn whose name 
325c5 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
325c6 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a  he table..    **
325c7 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 62 20 77   Return a blob w
325c8 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65  hich is a pointe
325c9 72 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 0a  r to the cursor.
325ca 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
325cb 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70  e3_result_blob(p
325cc 43 6f 6e 74 65 78 74 2c 20 26 63 2c 20 73 69 7a  Context, &c, siz
325cd 65 6f 66 28 63 29 2c 20 53 51 4c 49 54 45 5f 54  eof(c), SQLITE_T
325ce 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c  RANSIENT);.  }el
325cf 73 65 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76  se if( idxCol==v
325d0 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20 29 7b 0a 20  ->nColumn+1 ){. 
325d1 20 20 20 2f 2a 20 54 68 65 20 64 6f 63 69 64 20     /* The docid 
325d2 63 6f 6c 75 6d 6e 2c 20 77 68 69 63 68 20 69 73  column, which is
325d3 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f   an alias for ro
325d4 77 69 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  wid. */.    sqli
325d5 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
325d6 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
325d7 5f 76 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c  _value(c->pStmt,
325d8 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
325d9 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 70 43  _result_value(pC
325da 6f 6e 74 65 78 74 2c 20 70 56 61 6c 29 3b 0a 20  ontext, pVal);. 
325db 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
325dc 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  TE_OK;.}../* Thi
325dd 73 20 69 73 20 74 68 65 20 78 52 6f 77 69 64 20  s is the xRowid 
325de 6d 65 74 68 6f 64 2e 20 20 54 68 65 20 53 51 4c  method.  The SQL
325df 69 74 65 20 63 6f 72 65 20 63 61 6c 6c 73 20 74  ite core calls t
325e0 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a 2a  his routine to.*
325e1 2a 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72  * retrieve the r
325e2 6f 77 69 64 20 66 6f 72 20 74 68 65 20 63 75 72  owid for the cur
325e3 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
325e4 72 65 73 75 6c 74 20 73 65 74 2e 20 20 66 74 73  result set.  fts
325e5 33 0a 2a 2a 20 65 78 70 6f 73 65 73 20 25 5f 63  3.** exposes %_c
325e6 6f 6e 74 65 6e 74 2e 64 6f 63 69 64 20 61 73 20  ontent.docid as 
325e7 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68  the rowid for th
325e8 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
325e9 20 20 54 68 65 0a 2a 2a 20 72 6f 77 69 64 20 73    The.** rowid s
325ea 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e  hould be written
325eb 20 74 6f 20 2a 70 52 6f 77 69 64 2e 0a 2a 2f 0a   to *pRowid..*/.
325ec 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
325ed 65 78 74 52 6f 77 69 64 28 73 71 6c 69 74 65 33  extRowid(sqlite3
325ee 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43  _vtab_cursor *pC
325ef 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e  ursor, sqlite_in
325f0 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20  t64 *pRowid){.  
325f1 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
325f2 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63  *c = (fulltext_c
325f3 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
325f4 3b 0a 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 73  ;..  *pRowid = s
325f5 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
325f6 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29  t64(c->pStmt, 0)
325f7 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
325f8 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20  E_OK;.}../* Add 
325f9 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 5b 7a 54  all terms in [zT
325fa 65 78 74 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54  ext] to pendingT
325fb 65 72 6d 73 20 74 61 62 6c 65 2e 20 20 49 66 20  erms table.  If 
325fc 5b 69 43 6f 6c 75 6d 6e 5d 20 3e 20 30 2c 0a 2a  [iColumn] > 0,.*
325fd 2a 20 77 65 20 61 6c 73 6f 20 73 74 6f 72 65 20  * we also store 
325fe 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66  positions and of
325ff 66 73 65 74 73 20 69 6e 20 74 68 65 20 68 61 73  fsets in the has
32600 68 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68  h table using th
32601 61 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d  at.** column num
32602 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ber..*/.static i
32603 6e 74 20 62 75 69 6c 64 54 65 72 6d 73 28 66 75  nt buildTerms(fu
32604 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
32605 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
32606 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cid,.           
32607 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
32608 20 63 68 61 72 20 2a 7a 54 65 78 74 2c 20 69 6e   char *zText, in
32609 74 20 69 43 6f 6c 75 6d 6e 29 7b 0a 20 20 73 71  t iColumn){.  sq
3260a 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3260b 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 76 2d  *pTokenizer = v-
3260c 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73  >pTokenizer;.  s
3260d 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3260e 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
3260f 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
32610 70 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54  pToken;.  int nT
32611 6f 6b 65 6e 42 79 74 65 73 3b 0a 20 20 69 6e 74  okenBytes;.  int
32612 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69   iStartOffset, i
32613 45 6e 64 4f 66 66 73 65 74 2c 20 69 50 6f 73 69  EndOffset, iPosi
32614 74 69 6f 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  tion;.  int rc;.
32615 0a 20 20 72 63 20 3d 20 70 54 6f 6b 65 6e 69 7a  .  rc = pTokeniz
32616 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70  er->pModule->xOp
32617 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a  en(pTokenizer, z
32618 54 65 78 74 2c 20 2d 31 2c 20 26 70 43 75 72 73  Text, -1, &pCurs
32619 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  or);.  if( rc!=S
3261a 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3261b 6e 20 72 63 3b 0a 0a 20 20 70 43 75 72 73 6f 72  n rc;..  pCursor
3261c 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70  ->pTokenizer = p
3261d 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 77 68 69  Tokenizer;.  whi
3261e 6c 65 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  le( SQLITE_OK==(
3261f 72 63 3d 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70  rc=pTokenizer->p
32620 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43  Module->xNext(pC
32621 75 72 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  ursor,.         
32622 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32623 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32624 20 20 20 20 20 20 20 20 20 20 26 70 54 6f 6b 65            &pToke
32625 6e 2c 20 26 6e 54 6f 6b 65 6e 42 79 74 65 73 2c  n, &nTokenBytes,
32626 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32627 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32628 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32629 20 20 20 20 26 69 53 74 61 72 74 4f 66 66 73 65      &iStartOffse
3262a 74 2c 20 26 69 45 6e 64 4f 66 66 73 65 74 2c 0a  t, &iEndOffset,.
3262b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3262c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3262d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3262e 20 20 20 26 69 50 6f 73 69 74 69 6f 6e 29 29 20     &iPosition)) 
3262f 29 7b 0a 20 20 20 20 44 4c 43 6f 6c 6c 65 63 74  ){.    DLCollect
32630 6f 72 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e  or *p;.    int n
32631 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
32632 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
32633 6f 66 20 64 6f 63 6c 69 73 74 20 62 65 66 6f 72  of doclist befor
32634 65 20 6f 75 72 20 75 70 64 61 74 65 2e 20 2a 2f  e our update. */
32635 0a 0a 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f  ..    /* Positio
32636 6e 73 20 63 61 6e 27 74 20 62 65 20 6e 65 67 61  ns can't be nega
32637 74 69 76 65 3b 20 77 65 20 75 73 65 20 2d 31 20  tive; we use -1 
32638 61 73 20 61 20 74 65 72 6d 69 6e 61 74 6f 72 0a  as a terminator.
32639 20 20 20 20 20 2a 20 69 6e 74 65 72 6e 61 6c 6c       * internall
3263a 79 2e 20 20 54 6f 6b 65 6e 20 63 61 6e 27 74 20  y.  Token can't 
3263b 62 65 20 4e 55 4c 4c 20 6f 72 20 65 6d 70 74 79  be NULL or empty
3263c 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 6f  . */.    if( iPo
3263d 73 69 74 69 6f 6e 3c 30 20 7c 7c 20 70 54 6f 6b  sition<0 || pTok
3263e 65 6e 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 6e 54  en == NULL || nT
3263f 6f 6b 65 6e 42 79 74 65 73 20 3d 3d 20 30 20 29  okenBytes == 0 )
32640 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
32641 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
32642 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
32643 20 20 20 70 20 3d 20 66 74 73 33 48 61 73 68 46     p = fts3HashF
32644 69 6e 64 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  ind(&v->pendingT
32645 65 72 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20 6e 54  erms, pToken, nT
32646 6f 6b 65 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  okenBytes);.    
32647 69 66 28 20 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20  if( p==NULL ){. 
32648 20 20 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a       nData = 0;.
32649 20 20 20 20 20 20 70 20 3d 20 64 6c 63 4e 65 77        p = dlcNew
3264a 28 69 44 6f 63 69 64 2c 20 44 4c 5f 44 45 46 41  (iDocid, DL_DEFA
3264b 55 4c 54 29 3b 0a 20 20 20 20 20 20 66 74 73 33  ULT);.      fts3
3264c 48 61 73 68 49 6e 73 65 72 74 28 26 76 2d 3e 70  HashInsert(&v->p
3264d 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 70 54 6f  endingTerms, pTo
3264e 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42 79 74 65 73  ken, nTokenBytes
3264f 2c 20 70 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  , p);..      /* 
32650 4f 76 65 72 68 65 61 64 20 66 6f 72 20 6f 75 72  Overhead for our
32651 20 68 61 73 68 20 74 61 62 6c 65 20 65 6e 74 72   hash table entr
32652 79 2c 20 74 68 65 20 6b 65 79 2c 20 61 6e 64 20  y, the key, and 
32653 74 68 65 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20  the value. */.  
32654 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44      v->nPendingD
32655 61 74 61 20 2b 3d 20 73 69 7a 65 6f 66 28 73 74  ata += sizeof(st
32656 72 75 63 74 20 66 74 73 33 48 61 73 68 45 6c 65  ruct fts3HashEle
32657 6d 29 2b 73 69 7a 65 6f 66 28 2a 70 29 2b 6e 54  m)+sizeof(*p)+nT
32658 6f 6b 65 6e 42 79 74 65 73 3b 0a 20 20 20 20 7d  okenBytes;.    }
32659 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 44 61 74  else{.      nDat
3265a 61 20 3d 20 70 2d 3e 62 2e 6e 44 61 74 61 3b 0a  a = p->b.nData;.
3265b 20 20 20 20 20 20 69 66 28 20 70 2d 3e 64 6c 77        if( p->dlw
3265c 2e 69 50 72 65 76 44 6f 63 69 64 21 3d 69 44 6f  .iPrevDocid!=iDo
3265d 63 69 64 20 29 20 64 6c 63 4e 65 78 74 28 70 2c  cid ) dlcNext(p,
3265e 20 69 44 6f 63 69 64 29 3b 0a 20 20 20 20 7d 0a   iDocid);.    }.
3265f 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3e      if( iColumn>
32660 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6c 63 41  =0 ){.      dlcA
32661 64 64 50 6f 73 28 70 2c 20 69 43 6f 6c 75 6d 6e  ddPos(p, iColumn
32662 2c 20 69 50 6f 73 69 74 69 6f 6e 2c 20 69 53 74  , iPosition, iSt
32663 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f  artOffset, iEndO
32664 66 66 73 65 74 29 3b 0a 20 20 20 20 7d 0a 0a 20  ffset);.    }.. 
32665 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65     /* Accumulate
32666 20 64 61 74 61 20 61 64 64 65 64 20 62 79 20 64   data added by d
32667 6c 63 4e 65 77 20 6f 72 20 64 6c 63 4e 65 78 74  lcNew or dlcNext
32668 2c 20 61 6e 64 20 64 6c 63 41 64 64 50 6f 73 2e  , and dlcAddPos.
32669 20 2a 2f 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64   */.    v->nPend
3266a 69 6e 67 44 61 74 61 20 2b 3d 20 70 2d 3e 62 2e  ingData += p->b.
3266b 6e 44 61 74 61 2d 6e 44 61 74 61 3b 0a 20 20 7d  nData-nData;.  }
3266c 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ..  /* TODO(shes
3266d 73 29 20 43 68 65 63 6b 20 72 65 74 75 72 6e 3f  s) Check return?
3266e 20 20 53 68 6f 75 6c 64 20 74 68 69 73 20 62 65    Should this be
3266f 20 61 62 6c 65 20 74 6f 20 63 61 75 73 65 20 65   able to cause e
32670 72 72 6f 72 73 20 61 74 0a 20 20 2a 2a 20 74 68  rrors at.  ** th
32671 69 73 20 70 6f 69 6e 74 3f 20 20 41 63 74 75 61  is point?  Actua
32672 6c 6c 79 2c 20 73 61 6d 65 20 71 75 65 73 74 69  lly, same questi
32673 6f 6e 20 61 62 6f 75 74 20 73 71 6c 69 74 65 33  on about sqlite3
32674 5f 66 69 6e 61 6c 69 7a 65 28 29 2c 0a 20 20 2a  _finalize(),.  *
32675 2a 20 74 68 6f 75 67 68 20 6f 6e 65 20 63 6f 75  * though one cou
32676 6c 64 20 61 72 67 75 65 20 74 68 61 74 20 66 61  ld argue that fa
32677 69 6c 75 72 65 20 74 68 65 72 65 20 6d 65 61 6e  ilure there mean
32678 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20  s that the data 
32679 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 64 75 72 61  is.  ** not dura
3267a 62 6c 65 2e 20 20 2a 70 6f 6e 64 65 72 2a 0a 20  ble.  *ponder*. 
3267b 20 2a 2f 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72   */.  pTokenizer
3267c 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73  ->pModule->xClos
3267d 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66  e(pCursor);.  if
3267e 28 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3d 3d  ( SQLITE_DONE ==
3267f 20 72 63 20 29 20 72 65 74 75 72 6e 20 53 51 4c   rc ) return SQL
32680 49 54 45 5f 4f 4b 3b 0a 20 20 72 65 74 75 72 6e  ITE_OK;.  return
32681 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 64   rc;.}../* Add d
32682 6f 63 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c 20  oclists for all 
32683 74 65 72 6d 73 20 69 6e 20 5b 70 56 61 6c 75 65  terms in [pValue
32684 73 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72  s] to pendingTer
32685 6d 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74 61  ms table. */.sta
32686 74 69 63 20 69 6e 74 20 69 6e 73 65 72 74 54 65  tic int insertTe
32687 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
32688 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
32689 36 34 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20  64 iDocid,.     
3268a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3268b 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
3268c 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e  **pValues){.  in
3268d 74 20 69 3b 0a 20 20 66 6f 72 28 69 20 3d 20 30  t i;.  for(i = 0
3268e 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e  ; i < v->nColumn
3268f 20 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 63 68 61   ; ++i){.    cha
32690 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 68 61 72  r *zText = (char
32691 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
32692 74 65 78 74 28 70 56 61 6c 75 65 73 5b 69 5d 29  text(pValues[i])
32693 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62  ;.    int rc = b
32694 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69 44 6f  uildTerms(v, iDo
32695 63 69 64 2c 20 7a 54 65 78 74 2c 20 69 29 3b 0a  cid, zText, i);.
32696 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
32697 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
32698 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  c;.  }.  return 
32699 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
3269a 20 41 64 64 20 65 6d 70 74 79 20 64 6f 63 6c 69   Add empty docli
3269b 73 74 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d  sts for all term
3269c 73 20 69 6e 20 74 68 65 20 67 69 76 65 6e 20 72  s in the given r
3269d 6f 77 27 73 20 63 6f 6e 74 65 6e 74 20 74 6f 0a  ow's content to.
3269e 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e  ** pendingTerms.
3269f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
326a0 65 6c 65 74 65 54 65 72 6d 73 28 66 75 6c 6c 74  eleteTerms(fullt
326a1 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
326a2 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
326a3 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
326a4 2a 2a 70 56 61 6c 75 65 73 3b 0a 20 20 69 6e 74  **pValues;.  int
326a5 20 69 2c 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f   i, rc;..  /* TO
326a6 44 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64  DO(shess) Should
326a7 20 77 65 20 61 6c 6c 6f 77 20 73 75 63 68 20 74   we allow such t
326a8 61 62 6c 65 73 20 61 74 20 61 6c 6c 3f 20 2a 2f  ables at all? */
326a9 0a 20 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c  .  if( DL_DEFAUL
326aa 54 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72  T==DL_DOCIDS ) r
326ab 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
326ac 4f 52 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74  OR;..  rc = cont
326ad 65 6e 74 5f 73 65 6c 65 63 74 28 76 2c 20 69 44  ent_select(v, iD
326ae 6f 63 69 64 2c 20 26 70 56 61 6c 75 65 73 29 3b  ocid, &pValues);
326af 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
326b0 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
326b1 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30 20 3b  ;..  for(i = 0 ;
326b2 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b   i < v->nColumn;
326b3 20 2b 2b 69 29 20 7b 0a 20 20 20 20 72 63 20 3d   ++i) {.    rc =
326b4 20 62 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69   buildTerms(v, i
326b5 44 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 5b 69  Docid, pValues[i
326b6 5d 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20  ], -1);.    if( 
326b7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
326b8 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 66 72  break;.  }..  fr
326b9 65 65 53 74 72 69 6e 67 41 72 72 61 79 28 76 2d  eeStringArray(v-
326ba 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 56 61 6c 75 65  >nColumn, pValue
326bb 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  s);.  return SQL
326bc 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 4f  ITE_OK;.}../* TO
326bd 44 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74  DO(shess) Refact
326be 6f 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72  or the code to r
326bf 65 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77 61  emove this forwa
326c0 72 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61 74  rd decl. */.stat
326c1 69 63 20 69 6e 74 20 69 6e 69 74 50 65 6e 64 69  ic int initPendi
326c2 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
326c3 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65  _vtab *v, sqlite
326c4 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 3b 0a  _int64 iDocid);.
326c5 0a 2f 2a 20 49 6e 73 65 72 74 20 61 20 72 6f 77  ./* Insert a row
326c6 20 69 6e 74 6f 20 74 68 65 20 25 5f 63 6f 6e 74   into the %_cont
326c7 65 6e 74 20 74 61 62 6c 65 3b 20 73 65 74 20 2a  ent table; set *
326c8 70 69 44 6f 63 69 64 20 74 6f 20 62 65 20 74 68  piDocid to be th
326c9 65 20 49 44 20 6f 66 20 74 68 65 0a 2a 2a 20 6e  e ID of the.** n
326ca 65 77 20 72 6f 77 2e 20 20 41 64 64 20 64 6f 63  ew row.  Add doc
326cb 6c 69 73 74 73 20 66 6f 72 20 74 65 72 6d 73 20  lists for terms 
326cc 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e  to pendingTerms.
326cd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
326ce 6e 64 65 78 5f 69 6e 73 65 72 74 28 66 75 6c 6c  ndex_insert(full
326cf 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
326d0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65  lite3_value *pRe
326d1 71 75 65 73 74 44 6f 63 69 64 2c 0a 20 20 20 20  questDocid,.    
326d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
326d3 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
326d4 65 20 2a 2a 70 56 61 6c 75 65 73 2c 20 73 71 6c  e **pValues, sql
326d5 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 44 6f 63  ite_int64 *piDoc
326d6 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  id){.  int rc;..
326d7 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 69    rc = content_i
326d8 6e 73 65 72 74 28 76 2c 20 70 52 65 71 75 65 73  nsert(v, pReques
326d9 74 44 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29  tDocid, pValues)
326da 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e  ;  /* execute an
326db 20 53 51 4c 20 49 4e 53 45 52 54 20 2a 2f 0a 20   SQL INSERT */. 
326dc 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
326dd 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
326de 0a 20 20 2f 2a 20 64 6f 63 69 64 20 63 6f 6c 75  .  /* docid colu
326df 6d 6e 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66  mn is an alias f
326e0 6f 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a  or rowid. */.  *
326e1 70 69 44 6f 63 69 64 20 3d 20 73 71 6c 69 74 65  piDocid = sqlite
326e2 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
326e3 77 69 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72 63  wid(v->db);.  rc
326e4 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65   = initPendingTe
326e5 72 6d 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 29  rms(v, *piDocid)
326e6 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
326e7 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
326e8 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e 73  c;..  return ins
326e9 65 72 74 54 65 72 6d 73 28 76 2c 20 2a 70 69 44  ertTerms(v, *piD
326ea 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29 3b 0a  ocid, pValues);.
326eb 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20 61 20 72  }../* Delete a r
326ec 6f 77 20 66 72 6f 6d 20 74 68 65 20 25 5f 63 6f  ow from the %_co
326ed 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 61 64 64  ntent table; add
326ee 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20   empty doclists 
326ef 66 6f 72 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20  for terms.** to 
326f0 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f  pendingTerms..*/
326f1 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65  .static int inde
326f2 78 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78  x_delete(fulltex
326f3 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
326f4 65 5f 69 6e 74 36 34 20 69 52 6f 77 29 7b 0a 20  e_int64 iRow){. 
326f5 20 69 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65   int rc = initPe
326f6 6e 64 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52  ndingTerms(v, iR
326f7 6f 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ow);.  if( rc!=S
326f8 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
326f9 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 64 65  n rc;..  rc = de
326fa 6c 65 74 65 54 65 72 6d 73 28 76 2c 20 69 52 6f  leteTerms(v, iRo
326fb 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  w);.  if( rc!=SQ
326fc 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
326fd 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 63   rc;..  return c
326fe 6f 6e 74 65 6e 74 5f 64 65 6c 65 74 65 28 76 2c  ontent_delete(v,
326ff 20 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65 63   iRow);  /* exec
32700 75 74 65 20 61 6e 20 53 51 4c 20 44 45 4c 45 54  ute an SQL DELET
32701 45 20 2a 2f 0a 7d 0a 0a 2f 2a 20 55 70 64 61 74  E */.}../* Updat
32702 65 20 61 20 72 6f 77 20 69 6e 20 74 68 65 20 25  e a row in the %
32703 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20  _content table; 
32704 61 64 64 20 64 65 6c 65 74 65 20 64 6f 63 6c 69  add delete docli
32705 73 74 73 20 74 6f 0a 2a 2a 20 70 65 6e 64 69 6e  sts to.** pendin
32706 67 54 65 72 6d 73 20 66 6f 72 20 6f 6c 64 20 74  gTerms for old t
32707 65 72 6d 73 20 6e 6f 74 20 69 6e 20 74 68 65 20  erms not in the 
32708 6e 65 77 20 64 61 74 61 2c 20 61 64 64 20 69 6e  new data, add in
32709 73 65 72 74 20 64 6f 63 6c 69 73 74 73 0a 2a 2a  sert doclists.**
3270a 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   to pendingTerms
3270b 20 66 6f 72 20 74 65 72 6d 73 20 69 6e 20 74 68   for terms in th
3270c 65 20 6e 65 77 20 64 61 74 61 2e 0a 2a 2f 0a 73  e new data..*/.s
3270d 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f  tatic int index_
3270e 75 70 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f  update(fulltext_
3270f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
32710 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 20 20  int64 iRow,.    
32711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32712 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
32713 65 20 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20  e **pValues){.  
32714 69 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65 6e  int rc = initPen
32715 64 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52 6f  dingTerms(v, iRo
32716 77 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  w);.  if( rc!=SQ
32717 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
32718 20 72 63 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72   rc;..  /* Gener
32719 61 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63  ate an empty doc
3271a 6c 69 73 74 20 66 6f 72 20 65 61 63 68 20 74 65  list for each te
3271b 72 6d 20 74 68 61 74 20 70 72 65 76 69 6f 75 73  rm that previous
3271c 6c 79 20 61 70 70 65 61 72 65 64 20 69 6e 20 74  ly appeared in t
3271d 68 69 73 0a 20 20 20 2a 20 72 6f 77 2e 20 2a 2f  his.   * row. */
3271e 0a 20 20 72 63 20 3d 20 64 65 6c 65 74 65 54 65  .  rc = deleteTe
3271f 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20  rms(v, iRow);.  
32720 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
32721 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
32722 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 75    rc = content_u
32723 70 64 61 74 65 28 76 2c 20 70 56 61 6c 75 65 73  pdate(v, pValues
32724 2c 20 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65  , iRow);  /* exe
32725 63 75 74 65 20 61 6e 20 53 51 4c 20 55 50 44 41  cute an SQL UPDA
32726 54 45 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d  TE */.  if( rc!=
32727 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
32728 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e 6f 77  rn rc;..  /* Now
32729 20 61 64 64 20 70 6f 73 69 74 69 6f 6e 73 20 66   add positions f
3272a 6f 72 20 74 65 72 6d 73 20 77 68 69 63 68 20 61  or terms which a
3272b 70 70 65 61 72 20 69 6e 20 74 68 65 20 75 70 64  ppear in the upd
3272c 61 74 65 64 20 72 6f 77 2e 20 2a 2f 0a 20 20 72  ated row. */.  r
3272d 65 74 75 72 6e 20 69 6e 73 65 72 74 54 65 72 6d  eturn insertTerm
3272e 73 28 76 2c 20 69 52 6f 77 2c 20 70 56 61 6c 75  s(v, iRow, pValu
3272f 65 73 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  es);.}../*******
32730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32731 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32732 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32733 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
32734 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20   InteriorWriter 
32735 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65  is used to colle
32736 63 74 20 74 65 72 6d 73 20 61 6e 64 20 62 6c 6f  ct terms and blo
32737 63 6b 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e  ck references in
32738 74 6f 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e  to.** interior n
32739 6f 64 65 73 20 69 6e 20 25 5f 73 65 67 6d 65 6e  odes in %_segmen
3273a 74 73 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74  ts.  See comment
3273b 61 72 79 20 61 74 20 74 6f 70 20 6f 66 20 66 69  ary at top of fi
3273c 6c 65 20 66 6f 72 0a 2a 2a 20 66 6f 72 6d 61 74  le for.** format
3273d 2e 0a 2a 2f 0a 0a 2f 2a 20 48 6f 77 20 6c 61 72  ..*/../* How lar
3273e 67 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ge interior node
3273f 73 20 63 61 6e 20 67 72 6f 77 2e 20 2a 2f 0a 23  s can grow. */.#
32740 64 65 66 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f  define INTERIOR_
32741 4d 41 58 20 32 30 34 38 0a 0a 2f 2a 20 4d 69 6e  MAX 2048../* Min
32742 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74  imum number of t
32743 65 72 6d 73 20 70 65 72 20 69 6e 74 65 72 69 6f  erms per interio
32744 72 20 6e 6f 64 65 20 28 65 78 63 65 70 74 20 74  r node (except t
32745 68 65 20 72 6f 6f 74 29 2e 20 54 68 69 73 0a 2a  he root). This.*
32746 2a 20 70 72 65 76 65 6e 74 73 20 6c 61 72 67 65  * prevents large
32747 20 74 65 72 6d 73 20 66 72 6f 6d 20 6d 61 6b 69   terms from maki
32748 6e 67 20 74 68 65 20 74 72 65 65 20 74 6f 6f 20  ng the tree too 
32749 73 6b 69 6e 6e 79 20 2d 20 6d 75 73 74 20 62 65  skinny - must be
3274a 20 3e 30 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74   >0.** so that t
3274b 68 65 20 74 72 65 65 20 61 6c 77 61 79 73 20 6d  he tree always m
3274c 61 6b 65 73 20 70 72 6f 67 72 65 73 73 2e 20 20  akes progress.  
3274d 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6d 69  Note that the mi
3274e 6e 20 74 72 65 65 0a 2a 2a 20 66 61 6e 6f 75 74  n tree.** fanout
3274f 20 77 69 6c 6c 20 62 65 20 49 4e 54 45 52 49 4f   will be INTERIO
32750 52 5f 4d 49 4e 5f 54 45 52 4d 53 2b 31 2e 0a 2a  R_MIN_TERMS+1..*
32751 2f 0a 23 64 65 66 69 6e 65 20 49 4e 54 45 52 49  /.#define INTERI
32752 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 37 0a 23  OR_MIN_TERMS 7.#
32753 69 66 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f  if INTERIOR_MIN_
32754 54 45 52 4d 53 3c 31 0a 23 20 65 72 72 6f 72 20  TERMS<1.# error 
32755 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
32756 4d 53 20 6d 75 73 74 20 62 65 20 67 72 65 61 74  MS must be great
32757 65 72 20 74 68 61 6e 20 30 2e 0a 23 65 6e 64 69  er than 0..#endi
32758 66 0a 0a 2f 2a 20 52 4f 4f 54 5f 4d 41 58 20 63  f../* ROOT_MAX c
32759 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6d 75 63 68  ontrols how much
3275a 20 64 61 74 61 20 69 73 20 73 74 6f 72 65 64 20   data is stored 
3275b 69 6e 6c 69 6e 65 20 69 6e 20 74 68 65 20 73 65  inline in the se
3275c 67 6d 65 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6f  gment.** directo
3275d 72 79 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  ry..*/./* TODO(s
3275e 68 65 73 73 29 20 50 75 73 68 20 52 4f 4f 54 5f  hess) Push ROOT_
3275f 4d 41 58 20 64 6f 77 6e 20 74 6f 20 77 68 6f 65  MAX down to whoe
32760 76 65 72 20 69 73 20 77 72 69 74 69 6e 67 20 74  ver is writing t
32761 68 69 6e 67 73 2e 20 20 49 74 27 73 0a 2a 2a 20  hings.  It's.** 
32762 6f 6e 6c 79 20 68 65 72 65 20 73 6f 20 74 68 61  only here so tha
32763 74 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  t interiorWriter
32764 52 6f 6f 74 49 6e 66 6f 28 29 20 61 6e 64 20 6c  RootInfo() and l
32765 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66  eafWriterRootInf
32766 6f 28 29 0a 2a 2a 20 63 61 6e 20 62 6f 74 68 20  o().** can both 
32767 73 65 65 20 69 74 2c 20 62 75 74 20 69 66 20 74  see it, but if t
32768 68 65 20 63 61 6c 6c 65 72 20 70 61 73 73 65 64  he caller passed
32769 20 69 74 20 69 6e 2c 20 77 65 20 77 6f 75 6c 64   it in, we would
3276a 6e 27 74 20 65 76 65 6e 0a 2a 2a 20 6e 65 65 64  n't even.** need
3276b 20 61 20 64 65 66 69 6e 65 2e 0a 2a 2f 0a 23 64   a define..*/.#d
3276c 65 66 69 6e 65 20 52 4f 4f 54 5f 4d 41 58 20 31  efine ROOT_MAX 1
3276d 30 32 34 0a 23 69 66 20 52 4f 4f 54 5f 4d 41 58  024.#if ROOT_MAX
3276e 3c 56 41 52 49 4e 54 5f 4d 41 58 2a 32 0a 23 20  <VARINT_MAX*2.# 
3276f 65 72 72 6f 72 20 52 4f 4f 54 5f 4d 41 58 20 6d  error ROOT_MAX m
32770 75 73 74 20 68 61 76 65 20 65 6e 6f 75 67 68 20  ust have enough 
32771 73 70 61 63 65 20 66 6f 72 20 61 20 68 65 61 64  space for a head
32772 65 72 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49  er..#endif../* I
32773 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 73 74 6f  nteriorBlock sto
32774 72 65 73 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73  res a linked-lis
32775 74 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 62 6c  t of interior bl
32776 6f 63 6b 73 20 77 68 69 6c 65 20 61 20 6c 6f 77  ocks while a low
32777 65 72 0a 2a 2a 20 6c 61 79 65 72 20 69 73 20 62  er.** layer is b
32778 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
32779 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
3277a 75 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  uct InteriorBloc
3277b 6b 20 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72  k {.  DataBuffer
3277c 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20   term;          
3277d 20 2f 2a 20 4c 65 66 74 6d 6f 73 74 20 74 65 72   /* Leftmost ter
3277e 6d 20 69 6e 20 62 6c 6f 63 6b 27 73 20 73 75 62  m in block's sub
3277f 74 72 65 65 2e 20 2a 2f 0a 20 20 44 61 74 61 42  tree. */.  DataB
32780 75 66 66 65 72 20 64 61 74 61 3b 20 20 20 20 20  uffer data;     
32781 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
32782 61 74 65 64 20 64 61 74 61 20 66 6f 72 20 74 68  ated data for th
32783 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 73 74  e block. */.  st
32784 72 75 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f  ruct InteriorBlo
32785 63 6b 20 2a 6e 65 78 74 3b 0a 7d 20 49 6e 74 65  ck *next;.} Inte
32786 72 69 6f 72 42 6c 6f 63 6b 3b 0a 0a 73 74 61 74  riorBlock;..stat
32787 69 63 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  ic InteriorBlock
32788 20 2a 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e   *interiorBlockN
32789 65 77 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20  ew(int iHeight, 
3278a 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 43 68  sqlite_int64 iCh
3278b 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20  ildBlock,.      
3278c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3278d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3278e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
3278f 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a  rm, int nTerm){.
32790 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
32791 2a 62 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33  *block = sqlite3
32792 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49  _malloc(sizeof(I
32793 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 29 29 3b 0a  nteriorBlock));.
32794 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f    char c[VARINT_
32795 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  MAX+VARINT_MAX];
32796 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28  .  int n;..  if(
32797 20 62 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 6d 65   block ){.    me
32798 6d 73 65 74 28 62 6c 6f 63 6b 2c 20 30 2c 20 73  mset(block, 0, s
32799 69 7a 65 6f 66 28 2a 62 6c 6f 63 6b 29 29 3b 0a  izeof(*block));.
3279a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
3279b 69 74 28 26 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2c  it(&block->term,
3279c 20 30 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66   0);.    dataBuf
3279d 66 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f 63  ferReplace(&bloc
3279e 6b 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20  k->term, pTerm, 
3279f 6e 54 65 72 6d 29 3b 0a 0a 20 20 20 20 6e 20 3d  nTerm);..    n =
327a0 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
327a1 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20  , iHeight);.    
327a2 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
327a3 6e 74 28 63 2b 6e 2c 20 69 43 68 69 6c 64 42 6c  nt(c+n, iChildBl
327a4 6f 63 6b 29 3b 0a 20 20 20 20 64 61 74 61 42 75  ock);.    dataBu
327a5 66 66 65 72 49 6e 69 74 28 26 62 6c 6f 63 6b 2d  fferInit(&block-
327a6 3e 64 61 74 61 2c 20 49 4e 54 45 52 49 4f 52 5f  >data, INTERIOR_
327a7 4d 41 58 29 3b 0a 20 20 20 20 64 61 74 61 42 75  MAX);.    dataBu
327a8 66 66 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f  fferReplace(&blo
327a9 63 6b 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 29 3b  ck->data, c, n);
327aa 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 62 6c  .  }.  return bl
327ab 6f 63 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ock;.}..#ifndef 
327ac 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79  NDEBUG./* Verify
327ad 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20 69   that the data i
327ae 73 20 72 65 61 64 61 62 6c 65 20 61 73 20 61 6e  s readable as an
327af 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20   interior node. 
327b0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
327b1 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69  nteriorBlockVali
327b2 64 61 74 65 28 49 6e 74 65 72 69 6f 72 42 6c 6f  date(InteriorBlo
327b3 63 6b 20 2a 70 42 6c 6f 63 6b 29 7b 0a 20 20 63  ck *pBlock){.  c
327b4 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
327b5 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e   = pBlock->data.
327b6 70 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61  pData;.  int nDa
327b7 74 61 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74  ta = pBlock->dat
327b8 61 2e 6e 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e  a.nData;.  int n
327b9 2c 20 69 44 75 6d 6d 79 3b 0a 20 20 73 71 6c 69  , iDummy;.  sqli
327ba 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
327bb 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44  d;..  assert( nD
327bc 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ata>0 );.  asser
327bd 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20  t( pData!=0 );. 
327be 20 61 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e   assert( pData+n
327bf 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20  Data>pData );.. 
327c0 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64 20 77 69   /* Must lead wi
327c1 74 68 20 68 65 69 67 68 74 20 6f 66 20 6e 6f 64  th height of nod
327c2 65 20 61 73 20 61 20 76 61 72 69 6e 74 28 6e 29  e as a varint(n)
327c3 2c 20 6e 3e 30 20 2a 2f 0a 20 20 6e 20 3d 20 66  , n>0 */.  n = f
327c4 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
327c5 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
327c6 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b    assert( n>0 );
327c7 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d  .  assert( iDumm
327c8 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  y>0 );.  assert(
327c9 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44   n<nData );.  pD
327ca 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74  ata += n;.  nDat
327cb 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75  a -= n;..  /* Mu
327cc 73 74 20 63 6f 6e 74 61 69 6e 20 69 42 6c 6f 63  st contain iBloc
327cd 6b 69 64 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  kid. */.  n = ft
327ce 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74  s3GetVarint(pDat
327cf 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  a, &iBlockid);. 
327d0 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
327d1 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61    assert( n<=nDa
327d2 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d  ta );.  pData +=
327d3 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e   n;.  nData -= n
327d4 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20  ;..  /* Zero or 
327d5 6d 6f 72 65 20 74 65 72 6d 73 20 6f 66 20 70 6f  more terms of po
327d6 73 69 74 69 76 65 20 6c 65 6e 67 74 68 20 2a 2f  sitive length */
327d7 0a 20 20 69 66 28 20 6e 44 61 74 61 21 3d 30 20  .  if( nData!=0 
327d8 29 7b 0a 20 20 20 20 2f 2a 20 46 69 72 73 74 20  ){.    /* First 
327d9 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 65 6c 74  term is not delt
327da 61 2d 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  a-encoded. */.  
327db 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
327dc 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
327dd 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72  ummy);.    asser
327de 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73  t( n>0 );.    as
327df 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29  sert( iDummy>0 )
327e0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b  ;.    assert( n+
327e1 69 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20 61  iDummy>0);.    a
327e2 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
327e3 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44  =nData );.    pD
327e4 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
327e5 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b  .    nData -= n+
327e6 69 44 75 6d 6d 79 3b 0a 0a 20 20 20 20 2f 2a 20  iDummy;..    /* 
327e7 46 6f 6c 6c 6f 77 69 6e 67 20 74 65 72 6d 73 20  Following terms 
327e8 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 20 2a  delta-encoded. *
327e9 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 44 61  /.    while( nDa
327ea 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ta!=0 ){.      /
327eb 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 68 61 72  * Length of shar
327ec 65 64 20 70 72 65 66 69 78 2e 20 2a 2f 0a 20 20  ed prefix. */.  
327ed 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56      n = fts3GetV
327ee 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26  arint32(pData, &
327ef 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 61  iDummy);.      a
327f0 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
327f1 20 20 20 20 61 73 73 65 72 74 28 20 69 44 75 6d      assert( iDum
327f2 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  my>=0 );.      a
327f3 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
327f4 3b 0a 20 20 20 20 20 20 70 44 61 74 61 20 2b 3d  ;.      pData +=
327f5 20 6e 3b 0a 20 20 20 20 20 20 6e 44 61 74 61 20   n;.      nData 
327f6 2d 3d 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  -= n;..      /* 
327f7 4c 65 6e 67 74 68 20 61 6e 64 20 64 61 74 61 20  Length and data 
327f8 6f 66 20 64 69 73 74 69 6e 63 74 20 73 75 66 66  of distinct suff
327f9 69 78 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 20 3d  ix. */.      n =
327fa 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
327fb 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29  (pData, &iDummy)
327fc 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
327fd 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  n>0 );.      ass
327fe 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
327ff 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
32800 2b 69 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20  +iDummy>0);.    
32801 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
32802 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20  my<=nData );.   
32803 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44     pData += n+iD
32804 75 6d 6d 79 3b 0a 20 20 20 20 20 20 6e 44 61 74  ummy;.      nDat
32805 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a -= n+iDummy;. 
32806 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 64 65 66 69     }.  }.}.#defi
32807 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
32808 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 78  INTERIOR_BLOCK(x
32809 29 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56  ) interiorBlockV
3280a 61 6c 69 64 61 74 65 28 78 29 0a 23 65 6c 73 65  alidate(x).#else
3280b 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f  .#define ASSERT_
3280c 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
3280d 4c 4f 43 4b 28 78 29 20 61 73 73 65 72 74 28 20  LOCK(x) assert( 
3280e 31 20 29 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65  1 ).#endif..type
3280f 64 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 72  def struct Inter
32810 69 6f 72 57 72 69 74 65 72 20 7b 0a 20 20 69 6e  iorWriter {.  in
32811 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20  t iHeight;      
32812 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32813 66 72 6f 6d 20 30 20 61 74 20 6c 65 61 76 65 73  from 0 at leaves
32814 2e 20 2a 2f 0a 20 20 49 6e 74 65 72 69 6f 72 42  . */.  InteriorB
32815 6c 6f 63 6b 20 2a 66 69 72 73 74 2c 20 2a 6c 61  lock *first, *la
32816 73 74 3b 0a 20 20 73 74 72 75 63 74 20 49 6e 74  st;.  struct Int
32817 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70 61 72  eriorWriter *par
32818 65 6e 74 57 72 69 74 65 72 3b 0a 0a 20 20 44 61  entWriter;..  Da
32819 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20  taBuffer term;  
3281a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3281b 4c 61 73 74 20 74 65 72 6d 20 77 72 69 74 74 65  Last term writte
3281c 6e 20 74 6f 20 62 6c 6f 63 6b 20 22 6c 61 73 74  n to block "last
3281d 22 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ". */.  sqlite_i
3281e 6e 74 36 34 20 69 4f 70 65 6e 69 6e 67 43 68 69  nt64 iOpeningChi
3281f 6c 64 42 6c 6f 63 6b 3b 20 2f 2a 20 46 69 72 73  ldBlock; /* Firs
32820 74 20 63 68 69 6c 64 20 62 6c 6f 63 6b 20 69 6e  t child block in
32821 20 62 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a   block "last". *
32822 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  /.#ifndef NDEBUG
32823 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
32824 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 3b  iLastChildBlock;
32825 20 20 2f 2a 20 66 6f 72 20 63 6f 6e 73 69 73 74    /* for consist
32826 65 6e 63 79 20 63 68 65 63 6b 73 2e 20 2a 2f 0a  ency checks. */.
32827 23 65 6e 64 69 66 0a 7d 20 49 6e 74 65 72 69 6f  #endif.} Interio
32828 72 57 72 69 74 65 72 3b 0a 0a 2f 2a 20 49 6e 69  rWriter;../* Ini
32829 74 69 61 6c 69 7a 65 20 61 6e 20 69 6e 74 65 72  tialize an inter
3282a 69 6f 72 20 6e 6f 64 65 20 77 68 65 72 65 20 70  ior node where p
3282b 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 6d 61 72 6b  Term[nTerm] mark
3282c 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 0a 2a  s the leftmost.*
3282d 2a 20 74 65 72 6d 20 69 6e 20 74 68 65 20 74 72  * term in the tr
3282e 65 65 2e 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ee.  iChildBlock
3282f 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74   is the leftmost
32830 20 63 68 69 6c 64 20 62 6c 6f 63 6b 20 61 74 20   child block at 
32831 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6c 65 76 65  the.** next leve
32832 6c 20 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e  l down the tree.
32833 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
32834 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e  interiorWriterIn
32835 69 74 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20  it(int iHeight, 
32836 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
32837 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20  m, int nTerm,.  
32838 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32839 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
3283a 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64  ite_int64 iChild
3283b 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20  Block,.         
3283c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3283d 20 20 20 20 20 20 49 6e 74 65 72 69 6f 72 57 72        InteriorWr
3283e 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
3283f 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
32840 2a 62 6c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74  *block;.  assert
32841 28 20 69 48 65 69 67 68 74 3e 30 20 29 3b 0a 20  ( iHeight>0 );. 
32842 20 43 4c 45 41 52 28 70 57 72 69 74 65 72 29 3b   CLEAR(pWriter);
32843 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 48 65  ..  pWriter->iHe
32844 69 67 68 74 20 3d 20 69 48 65 69 67 68 74 3b 0a  ight = iHeight;.
32845 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e    pWriter->iOpen
32846 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20  ingChildBlock = 
32847 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a 23 69 66  iChildBlock;.#if
32848 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57  ndef NDEBUG.  pW
32849 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c  riter->iLastChil
3284a 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64 42  dBlock = iChildB
3284b 6c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 20 20 62  lock;.#endif.  b
3284c 6c 6f 63 6b 20 3d 20 69 6e 74 65 72 69 6f 72 42  lock = interiorB
3284d 6c 6f 63 6b 4e 65 77 28 69 48 65 69 67 68 74 2c  lockNew(iHeight,
3284e 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 20 70 54   iChildBlock, pT
3284f 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 70  erm, nTerm);.  p
32850 57 72 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70  Writer->last = p
32851 57 72 69 74 65 72 2d 3e 66 69 72 73 74 20 3d 20  Writer->first = 
32852 62 6c 6f 63 6b 3b 0a 20 20 41 53 53 45 52 54 5f  block;.  ASSERT_
32853 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
32854 4c 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61  LOCK(pWriter->la
32855 73 74 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  st);.  dataBuffe
32856 72 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e  rInit(&pWriter->
32857 74 65 72 6d 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 20  term, 0);.}../* 
32858 41 70 70 65 6e 64 20 74 68 65 20 63 68 69 6c 64  Append the child
32859 20 6e 6f 64 65 20 72 6f 6f 74 65 64 20 61 74 20   node rooted at 
3285a 69 43 68 69 6c 64 42 6c 6f 63 6b 20 74 6f 20 74  iChildBlock to t
3285b 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  he interior node
3285c 2c 0a 2a 2a 20 77 69 74 68 20 70 54 65 72 6d 5b  ,.** with pTerm[
3285d 6e 54 65 72 6d 5d 20 61 73 20 74 68 65 20 6c 65  nTerm] as the le
3285e 66 74 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20 69  ftmost term in i
3285f 43 68 69 6c 64 42 6c 6f 63 6b 27 73 20 73 75 62  ChildBlock's sub
32860 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tree..*/.static 
32861 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 57 72 69  void interiorWri
32862 74 65 72 41 70 70 65 6e 64 28 49 6e 74 65 72 69  terAppend(Interi
32863 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  orWriter *pWrite
32864 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
32865 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32866 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
32867 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
32868 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32869 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3286a 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
3286b 69 43 68 69 6c 64 42 6c 6f 63 6b 29 7b 0a 20 20  iChildBlock){.  
3286c 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
3286d 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
3286e 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20   int n, nPrefix 
3286f 3d 20 30 3b 0a 0a 20 20 41 53 53 45 52 54 5f 56  = 0;..  ASSERT_V
32870 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c  ALID_INTERIOR_BL
32871 4f 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73  OCK(pWriter->las
32872 74 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 69  t);..  /* The fi
32873 72 73 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e  rst term written
32874 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f   into an interio
32875 72 20 6e 6f 64 65 20 69 73 20 61 63 74 75 61 6c  r node is actual
32876 6c 79 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74  ly.  ** associat
32877 65 64 20 77 69 74 68 20 74 68 65 20 73 65 63 6f  ed with the seco
32878 6e 64 20 63 68 69 6c 64 20 61 64 64 65 64 20 28  nd child added (
32879 74 68 65 20 66 69 72 73 74 20 63 68 69 6c 64 20  the first child 
3287a 77 61 73 20 61 64 64 65 64 0a 20 20 2a 2a 20 69  was added.  ** i
3287b 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  n interiorWriter
3287c 49 6e 69 74 2c 20 6f 72 20 69 6e 20 74 68 65 20  Init, or in the 
3287d 69 66 20 63 6c 61 75 73 65 20 61 74 20 74 68 65  if clause at the
3287e 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 0a   bottom of this.
3287f 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 29 2e 20    ** function). 
32880 20 54 68 61 74 20 74 65 72 6d 20 67 65 74 73 20   That term gets 
32881 65 6e 63 6f 64 65 64 20 73 74 72 61 69 67 68 74  encoded straight
32882 20 75 70 2c 20 77 69 74 68 20 6e 50 72 65 66 69   up, with nPrefi
32883 78 20 6c 65 66 74 0a 20 20 2a 2a 20 61 74 20 30  x left.  ** at 0
32884 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 72  ..  */.  if( pWr
32885 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  iter->term.nData
32886 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66  ==0 ){.    n = f
32887 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
32888 6e 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  nTerm);.  }else{
32889 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 50 72 65  .    while( nPre
3288a 66 69 78 3c 70 57 72 69 74 65 72 2d 3e 74 65 72  fix<pWriter->ter
3288b 6d 2e 6e 44 61 74 61 20 26 26 0a 20 20 20 20 20  m.nData &&.     
3288c 20 20 20 20 20 20 70 54 65 72 6d 5b 6e 50 72 65        pTerm[nPre
3288d 66 69 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e 74  fix]==pWriter->t
3288e 65 72 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66 69  erm.pData[nPrefi
3288f 78 5d 20 29 7b 0a 20 20 20 20 20 20 6e 50 72 65  x] ){.      nPre
32890 66 69 78 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  fix++;.    }..  
32891 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72    n = fts3PutVar
32892 69 6e 74 28 63 2c 20 6e 50 72 65 66 69 78 29 3b  int(c, nPrefix);
32893 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
32894 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65  tVarint(c+n, nTe
32895 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20 7d  rm-nPrefix);.  }
32896 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
32897 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 4c 61 73  .  pWriter->iLas
32898 74 43 68 69 6c 64 42 6c 6f 63 6b 2b 2b 3b 0a 23  tChildBlock++;.#
32899 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20  endif.  assert( 
3289a 70 57 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68  pWriter->iLastCh
3289b 69 6c 64 42 6c 6f 63 6b 3d 3d 69 43 68 69 6c 64  ildBlock==iChild
3289c 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 2f 2a 20 4f  Block );..  /* O
3289d 76 65 72 66 6c 6f 77 20 74 6f 20 61 20 6e 65 77  verflow to a new
3289e 20 62 6c 6f 63 6b 20 69 66 20 74 68 65 20 6e 65   block if the ne
3289f 77 20 74 65 72 6d 20 6d 61 6b 65 73 20 74 68 65  w term makes the
328a0 20 63 75 72 72 65 6e 74 20 62 6c 6f 63 6b 0a 20   current block. 
328a1 20 2a 2a 20 74 6f 6f 20 62 69 67 2c 20 61 6e 64   ** too big, and
328a2 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 6c 6f   the current blo
328a3 63 6b 20 61 6c 72 65 61 64 79 20 68 61 73 20 65  ck already has e
328a4 6e 6f 75 67 68 20 74 65 72 6d 73 2e 0a 20 20 2a  nough terms..  *
328a5 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  /.  if( pWriter-
328a6 3e 6c 61 73 74 2d 3e 64 61 74 61 2e 6e 44 61 74  >last->data.nDat
328a7 61 2b 6e 2b 6e 54 65 72 6d 2d 6e 50 72 65 66 69  a+n+nTerm-nPrefi
328a8 78 3e 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 26  x>INTERIOR_MAX &
328a9 26 0a 20 20 20 20 20 20 69 43 68 69 6c 64 42 6c  &.      iChildBl
328aa 6f 63 6b 2d 70 57 72 69 74 65 72 2d 3e 69 4f 70  ock-pWriter->iOp
328ab 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 3e  eningChildBlock>
328ac 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
328ad 4d 53 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65  MS ){.    pWrite
328ae 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78 74 20 3d 20  r->last->next = 
328af 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77  interiorBlockNew
328b0 28 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68  (pWriter->iHeigh
328b1 74 2c 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a  t, iChildBlock,.
328b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328b4 20 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d             pTerm
328b5 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 70 57  , nTerm);.    pW
328b6 72 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70 57  riter->last = pW
328b7 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78  riter->last->nex
328b8 74 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  t;.    pWriter->
328b9 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f  iOpeningChildBlo
328ba 63 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ck = iChildBlock
328bb 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
328bc 52 65 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e  Reset(&pWriter->
328bd 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  term);.  }else{.
328be 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
328bf 70 65 6e 64 32 28 26 70 57 72 69 74 65 72 2d 3e  pend2(&pWriter->
328c0 6c 61 73 74 2d 3e 64 61 74 61 2c 20 63 2c 20 6e  last->data, c, n
328c1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
328c2 20 20 20 20 20 20 20 20 70 54 65 72 6d 2b 6e 50          pTerm+nP
328c3 72 65 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72  refix, nTerm-nPr
328c4 65 66 69 78 29 3b 0a 20 20 20 20 64 61 74 61 42  efix);.    dataB
328c5 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 57  ufferReplace(&pW
328c6 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65  riter->term, pTe
328c7 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d 0a  rm, nTerm);.  }.
328c8 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49    ASSERT_VALID_I
328c9 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57  NTERIOR_BLOCK(pW
328ca 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 7d 0a  riter->last);.}.
328cb 0a 2f 2a 20 46 72 65 65 20 74 68 65 20 73 70 61  ./* Free the spa
328cc 63 65 20 75 73 65 64 20 62 79 20 70 57 72 69 74  ce used by pWrit
328cd 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  er, including th
328ce 65 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66  e linked-list of
328cf 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63  .** InteriorBloc
328d0 6b 73 2c 20 61 6e 64 20 70 61 72 65 6e 74 57 72  ks, and parentWr
328d1 69 74 65 72 2c 20 69 66 20 70 72 65 73 65 6e 74  iter, if present
328d2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
328d3 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 44 65  interiorWriterDe
328d4 73 74 72 6f 79 28 49 6e 74 65 72 69 6f 72 57 72  stroy(InteriorWr
328d5 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
328d6 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
328d7 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72  *block = pWriter
328d8 2d 3e 66 69 72 73 74 3b 0a 0a 20 20 77 68 69 6c  ->first;..  whil
328d9 65 28 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 20 29  e( block!=NULL )
328da 7b 0a 20 20 20 20 49 6e 74 65 72 69 6f 72 42 6c  {.    InteriorBl
328db 6f 63 6b 20 2a 62 20 3d 20 62 6c 6f 63 6b 3b 0a  ock *b = block;.
328dc 20 20 20 20 62 6c 6f 63 6b 20 3d 20 62 6c 6f 63      block = bloc
328dd 6b 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 64 61 74  k->next;.    dat
328de 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
328df 62 2d 3e 74 65 72 6d 29 3b 0a 20 20 20 20 64 61  b->term);.    da
328e0 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
328e1 26 62 2d 3e 64 61 74 61 29 3b 0a 20 20 20 20 73  &b->data);.    s
328e2 71 6c 69 74 65 33 5f 66 72 65 65 28 62 29 3b 0a  qlite3_free(b);.
328e3 20 20 7d 0a 20 20 69 66 28 20 70 57 72 69 74 65    }.  if( pWrite
328e4 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 21  r->parentWriter!
328e5 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74  =NULL ){.    int
328e6 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74 72  eriorWriterDestr
328e7 6f 79 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65  oy(pWriter->pare
328e8 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20 73  ntWriter);.    s
328e9 71 6c 69 74 65 33 5f 66 72 65 65 28 70 57 72 69  qlite3_free(pWri
328ea 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65  ter->parentWrite
328eb 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42 75  r);.  }.  dataBu
328ec 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72  fferDestroy(&pWr
328ed 69 74 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53  iter->term);.  S
328ee 43 52 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29  CRAMBLE(pWriter)
328ef 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
328f0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70  E_OK;.}../* If p
328f1 57 72 69 74 65 72 20 63 61 6e 20 66 69 74 20 65  Writer can fit e
328f2 6e 74 69 72 65 6c 79 20 69 6e 20 52 4f 4f 54 5f  ntirely in ROOT_
328f3 4d 41 58 2c 20 72 65 74 75 72 6e 20 69 74 20 61  MAX, return it a
328f4 73 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 0a  s the root info.
328f5 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 6c 65 61  ** directly, lea
328f6 76 69 6e 67 20 2a 70 69 45 6e 64 42 6c 6f 63 6b  ving *piEndBlock
328f7 69 64 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 4f  id unchanged.  O
328f8 74 68 65 72 77 69 73 65 2c 20 66 6c 75 73 68 0a  therwise, flush.
328f9 2a 2a 20 70 57 72 69 74 65 72 20 74 6f 20 25 5f  ** pWriter to %_
328fa 73 65 67 6d 65 6e 74 73 2c 20 62 75 69 6c 64 69  segments, buildi
328fb 6e 67 20 61 20 6e 65 77 20 6c 61 79 65 72 20 6f  ng a new layer o
328fc 66 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  f interior nodes
328fd 2c 20 61 6e 64 0a 2a 2a 20 72 65 63 75 72 73 69  , and.** recursi
328fe 76 65 6c 79 20 61 73 6b 20 66 6f 72 20 74 68 65  vely ask for the
328ff 69 72 20 72 6f 6f 74 20 69 6e 74 6f 2e 0a 2a 2f  ir root into..*/
32900 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65  .static int inte
32901 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e  riorWriterRootIn
32902 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  fo(fulltext_vtab
32903 20 2a 76 2c 20 49 6e 74 65 72 69 6f 72 57 72 69   *v, InteriorWri
32904 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
32905 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32906 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32907 63 68 61 72 20 2a 2a 70 70 52 6f 6f 74 49 6e 66  char **ppRootInf
32908 6f 2c 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 49 6e  o, int *pnRootIn
32909 66 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  fo,.            
3290a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3290b 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
3290c 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  64 *piEndBlockid
3290d 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f  ){.  InteriorBlo
3290e 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69  ck *block = pWri
3290f 74 65 72 2d 3e 66 69 72 73 74 3b 0a 20 20 73 71  ter->first;.  sq
32910 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
32911 6b 69 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  kid = 0;.  int r
32912 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  c;..  /* If we c
32913 61 6e 20 66 69 74 20 74 68 65 20 73 65 67 6d 65  an fit the segme
32914 6e 74 20 69 6e 6c 69 6e 65 20 2a 2f 0a 20 20 69  nt inline */.  i
32915 66 28 20 62 6c 6f 63 6b 3d 3d 70 57 72 69 74 65  f( block==pWrite
32916 72 2d 3e 6c 61 73 74 20 26 26 20 62 6c 6f 63 6b  r->last && block
32917 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f  ->data.nData<ROO
32918 54 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2a 70 70  T_MAX ){.    *pp
32919 52 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b  RootInfo = block
3291a 2d 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20  ->data.pData;.  
3291b 20 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20    *pnRootInfo = 
3291c 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74  block->data.nDat
3291d 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  a;.    return SQ
3291e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
3291f 2f 2a 20 46 6c 75 73 68 20 74 68 65 20 66 69 72  /* Flush the fir
32920 73 74 20 62 6c 6f 63 6b 20 74 6f 20 25 5f 73 65  st block to %_se
32921 67 6d 65 6e 74 73 2c 20 61 6e 64 20 63 72 65 61  gments, and crea
32922 74 65 20 61 20 6e 65 77 20 6c 65 76 65 6c 20 6f  te a new level o
32923 66 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20  f.  ** interior 
32924 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 41 53 53  node..  */.  ASS
32925 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
32926 4f 52 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b  OR_BLOCK(block);
32927 0a 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e  .  rc = block_in
32928 73 65 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64  sert(v, block->d
32929 61 74 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b  ata.pData, block
3292a 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69  ->data.nData, &i
3292b 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20  Blockid);.  if( 
3292c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3292d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 69  return rc;.  *pi
3292e 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c  EndBlockid = iBl
3292f 6f 63 6b 69 64 3b 0a 0a 20 20 70 57 72 69 74 65  ockid;..  pWrite
32930 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 20  r->parentWriter 
32931 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
32932 28 73 69 7a 65 6f 66 28 2a 70 57 72 69 74 65 72  (sizeof(*pWriter
32933 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 29  ->parentWriter))
32934 3b 0a 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74  ;.  interiorWrit
32935 65 72 49 6e 69 74 28 70 57 72 69 74 65 72 2d 3e  erInit(pWriter->
32936 69 48 65 69 67 68 74 2b 31 2c 0a 20 20 20 20 20  iHeight+1,.     
32937 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32938 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74  block->term.pDat
32939 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e  a, block->term.n
3293a 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
3293b 20 20 20 20 20 20 20 20 20 20 20 69 42 6c 6f 63             iBloc
3293c 6b 69 64 2c 20 70 57 72 69 74 65 72 2d 3e 70 61  kid, pWriter->pa
3293d 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 0a 20 20  rentWriter);..  
3293e 2f 2a 20 46 6c 75 73 68 20 61 64 64 69 74 69 6f  /* Flush additio
3293f 6e 61 6c 20 62 6c 6f 63 6b 73 20 61 6e 64 20 61  nal blocks and a
32940 70 70 65 6e 64 20 74 6f 20 74 68 65 20 68 69 67  ppend to the hig
32941 68 65 72 20 69 6e 74 65 72 69 6f 72 0a 20 20 2a  her interior.  *
32942 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 66  * node..  */.  f
32943 6f 72 28 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e  or(block=block->
32944 6e 65 78 74 3b 20 62 6c 6f 63 6b 21 3d 4e 55 4c  next; block!=NUL
32945 4c 3b 20 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e  L; block=block->
32946 6e 65 78 74 29 7b 0a 20 20 20 20 41 53 53 45 52  next){.    ASSER
32947 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52  T_VALID_INTERIOR
32948 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20  _BLOCK(block);. 
32949 20 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e     rc = block_in
3294a 73 65 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64  sert(v, block->d
3294b 61 74 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b  ata.pData, block
3294c 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69  ->data.nData, &i
3294d 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66  Blockid);.    if
3294e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3294f 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
32950 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d   *piEndBlockid =
32951 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 20 20   iBlockid;..    
32952 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70  interiorWriterAp
32953 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 70 61  pend(pWriter->pa
32954 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20  rentWriter,.    
32955 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32956 20 20 20 20 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d       block->term
32957 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 74  .pData, block->t
32958 65 72 6d 2e 6e 44 61 74 61 2c 20 69 42 6c 6f 63  erm.nData, iBloc
32959 6b 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  kid);.  }..  /* 
3295a 50 61 72 65 6e 74 20 6e 6f 64 65 20 67 65 74 73  Parent node gets
3295b 20 74 68 65 20 63 68 61 6e 63 65 20 74 6f 20 62   the chance to b
3295c 65 20 74 68 65 20 72 6f 6f 74 2e 20 2a 2f 0a 20  e the root. */. 
3295d 20 72 65 74 75 72 6e 20 69 6e 74 65 72 69 6f 72   return interior
3295e 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76  WriterRootInfo(v
3295f 2c 20 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e  , pWriter->paren
32960 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  tWriter,.       
32961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32962 20 20 20 20 20 20 20 20 20 70 70 52 6f 6f 74 49           ppRootI
32963 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66 6f 2c  nfo, pnRootInfo,
32964 20 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a   piEndBlockid);.
32965 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
32966 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32967 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32968 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32969 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74 65 72 69 6f  ****/./* Interio
3296a 72 52 65 61 64 65 72 20 69 73 20 75 73 65 64 20  rReader is used 
3296b 74 6f 20 72 65 61 64 20 6f 66 66 20 74 68 65 20  to read off the 
3296c 64 61 74 61 20 66 72 6f 6d 20 61 6e 20 69 6e 74  data from an int
3296d 65 72 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 28 73  erior node.** (s
3296e 65 65 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74 6f  ee comment at to
3296f 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 74 68  p of file for th
32970 65 20 66 6f 72 6d 61 74 29 2e 0a 2a 2f 0a 74 79  e format)..*/.ty
32971 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 74  pedef struct Int
32972 65 72 69 6f 72 52 65 61 64 65 72 20 7b 0a 20 20  eriorReader {.  
32973 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
32974 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  a;.  int nData;.
32975 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
32976 72 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rm;          /* 
32977 70 72 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 66  previous term, f
32978 6f 72 20 64 65 63 6f 64 69 6e 67 20 74 65 72 6d  or decoding term
32979 20 64 65 6c 74 61 2e 20 2a 2f 0a 0a 20 20 73 71   delta. */..  sq
3297a 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63  lite_int64 iBloc
3297b 6b 69 64 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 52  kid;.} InteriorR
3297c 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 76  eader;..static v
3297d 6f 69 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64  oid interiorRead
3297e 65 72 44 65 73 74 72 6f 79 28 49 6e 74 65 72 69  erDestroy(Interi
3297f 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
32980 72 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72  r){.  dataBuffer
32981 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72  Destroy(&pReader
32982 2d 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d  ->term);.  SCRAM
32983 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a  BLE(pReader);.}.
32984 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
32985 54 68 65 20 61 73 73 65 72 74 69 6f 6e 73 20 61  The assertions a
32986 72 65 20 67 72 65 61 74 2c 20 62 75 74 20 77 68  re great, but wh
32987 61 74 20 69 66 20 77 65 27 72 65 20 69 6e 20 4e  at if we're in N
32988 44 45 42 55 47 0a 2a 2a 20 61 6e 64 20 74 68 65  DEBUG.** and the
32989 20 62 6c 6f 62 20 69 73 20 65 6d 70 74 79 20 6f   blob is empty o
3298a 72 20 6f 74 68 65 72 77 69 73 65 20 63 6f 6e 74  r otherwise cont
3298b 61 69 6e 73 20 73 75 73 70 65 63 74 20 64 61 74  ains suspect dat
3298c 61 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  a?.*/.static voi
3298d 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  d interiorReader
3298e 49 6e 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20  Init(const char 
3298f 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
32990 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
32991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32992 20 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72    InteriorReader
32993 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e   *pReader){.  in
32994 74 20 6e 2c 20 6e 54 65 72 6d 3b 0a 0a 20 20 2f  t n, nTerm;..  /
32995 2a 20 52 65 71 75 69 72 65 20 61 74 20 6c 65 61  * Require at lea
32996 73 74 20 74 68 65 20 6c 65 61 64 69 6e 67 20 66  st the leading f
32997 6c 61 67 20 62 79 74 65 20 2a 2f 0a 20 20 61 73  lag byte */.  as
32998 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b  sert( nData>0 );
32999 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
3299a 5b 30 5d 21 3d 27 5c 30 27 20 29 3b 0a 0a 20 20  [0]!='\0' );..  
3299b 43 4c 45 41 52 28 70 52 65 61 64 65 72 29 3b 0a  CLEAR(pReader);.
3299c 0a 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65  .  /* Decode the
3299d 20 62 61 73 65 20 62 6c 6f 63 6b 69 64 2c 20 61   base blockid, a
3299e 6e 64 20 73 65 74 20 74 68 65 20 63 75 72 73 6f  nd set the curso
3299f 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 74  r to the first t
329a0 65 72 6d 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74  erm. */.  n = ft
329a1 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74  s3GetVarint(pDat
329a2 61 2b 31 2c 20 26 70 52 65 61 64 65 72 2d 3e 69  a+1, &pReader->i
329a3 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65  Blockid);.  asse
329a4 72 74 28 20 31 2b 6e 3c 3d 6e 44 61 74 61 20 29  rt( 1+n<=nData )
329a5 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  ;.  pReader->pDa
329a6 74 61 20 3d 20 70 44 61 74 61 2b 31 2b 6e 3b 0a  ta = pData+1+n;.
329a7 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
329a8 20 3d 20 6e 44 61 74 61 2d 28 31 2b 6e 29 3b 0a   = nData-(1+n);.
329a9 0a 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 2d 63  .  /* A single-c
329aa 68 69 6c 64 20 69 6e 74 65 72 69 6f 72 20 6e 6f  hild interior no
329ab 64 65 20 28 73 75 63 68 20 61 73 20 77 68 65 6e  de (such as when
329ac 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 61 73   a leaf node was
329ad 20 74 6f 6f 0a 20 20 2a 2a 20 6c 61 72 67 65 20   too.  ** large 
329ae 66 6f 72 20 74 68 65 20 73 65 67 6d 65 6e 74 20  for the segment 
329af 64 69 72 65 63 74 6f 72 79 29 20 77 6f 6e 27 74  directory) won't
329b0 20 68 61 76 65 20 61 6e 79 20 74 65 72 6d 73 2e   have any terms.
329b1 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  .  ** Otherwise,
329b2 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 73   decode the firs
329b3 74 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69  t term..  */.  i
329b4 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  f( pReader->nDat
329b5 61 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61  a==0 ){.    data
329b6 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65 61  BufferInit(&pRea
329b7 64 65 72 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 20  der->term, 0);. 
329b8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 20 3d 20   }else{.    n = 
329b9 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
329ba 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
329bb 26 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74  &nTerm);.    dat
329bc 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65  aBufferInit(&pRe
329bd 61 64 65 72 2d 3e 74 65 72 6d 2c 20 6e 54 65 72  ader->term, nTer
329be 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  m);.    dataBuff
329bf 65 72 52 65 70 6c 61 63 65 28 26 70 52 65 61 64  erReplace(&pRead
329c0 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65  er->term, pReade
329c1 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 54 65 72  r->pData+n, nTer
329c2 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  m);.    assert( 
329c3 6e 2b 6e 54 65 72 6d 3c 3d 70 52 65 61 64 65 72  n+nTerm<=pReader
329c4 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70  ->nData );.    p
329c5 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d  Reader->pData +=
329c6 20 6e 2b 6e 54 65 72 6d 3b 0a 20 20 20 20 70 52   n+nTerm;.    pR
329c7 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20  eader->nData -= 
329c8 6e 2b 6e 54 65 72 6d 3b 0a 20 20 7d 0a 7d 0a 0a  n+nTerm;.  }.}..
329c9 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
329ca 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 49  iorReaderAtEnd(I
329cb 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
329cc 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
329cd 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
329ce 6e 44 61 74 61 3d 3d 30 3b 0a 7d 0a 0a 73 74 61  nData==0;.}..sta
329cf 74 69 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  tic sqlite_int64
329d0 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
329d1 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 49 6e  urrentBlockid(In
329d2 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52  teriorReader *pR
329d3 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  eader){.  return
329d4 20 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b   pReader->iBlock
329d5 69 64 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  id;.}..static in
329d6 74 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  t interiorReader
329d7 54 65 72 6d 42 79 74 65 73 28 49 6e 74 65 72 69  TermBytes(Interi
329d8 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
329d9 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69  r){.  assert( !i
329da 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45  nteriorReaderAtE
329db 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
329dc 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
329dd 3e 74 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73  >term.nData;.}.s
329de 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
329df 20 2a 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72   *interiorReader
329e0 54 65 72 6d 28 49 6e 74 65 72 69 6f 72 52 65 61  Term(InteriorRea
329e1 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
329e2 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69   assert( !interi
329e3 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52  orReaderAtEnd(pR
329e4 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
329e5 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  rn pReader->term
329e6 2e 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 53 74  .pData;.}../* St
329e7 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68  ep forward to th
329e8 65 20 6e 65 78 74 20 74 65 72 6d 20 69 6e 20 74  e next term in t
329e9 68 65 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74  he node. */.stat
329ea 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72  ic void interior
329eb 52 65 61 64 65 72 53 74 65 70 28 49 6e 74 65 72  ReaderStep(Inter
329ec 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64  iorReader *pRead
329ed 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
329ee 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74  interiorReaderAt
329ef 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
329f0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6c 61 73  .  /* If the las
329f1 74 20 74 65 72 6d 20 68 61 73 20 62 65 65 6e 20  t term has been 
329f2 72 65 61 64 2c 20 73 69 67 6e 61 6c 20 65 6f 66  read, signal eof
329f3 2c 20 65 6c 73 65 20 63 6f 6e 73 74 72 75 63 74  , else construct
329f4 20 74 68 65 0a 20 20 2a 2a 20 6e 65 78 74 20 74   the.  ** next t
329f5 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  erm..  */.  if( 
329f6 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d  pReader->nData==
329f7 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
329f8 66 65 72 52 65 73 65 74 28 26 70 52 65 61 64 65  ferReset(&pReade
329f9 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73  r->term);.  }els
329fa 65 7b 0a 20 20 20 20 69 6e 74 20 6e 2c 20 6e 50  e{.    int n, nP
329fb 72 65 66 69 78 2c 20 6e 53 75 66 66 69 78 3b 0a  refix, nSuffix;.
329fc 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
329fd 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
329fe 2d 3e 70 44 61 74 61 2c 20 26 6e 50 72 65 66 69  ->pData, &nPrefi
329ff 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  x);.    n += fts
32a00 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
32a01 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
32a02 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20 2f  nSuffix);..    /
32a03 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 63  * Truncate the c
32a04 75 72 72 65 6e 74 20 74 65 72 6d 20 61 6e 64 20  urrent term and 
32a05 61 70 70 65 6e 64 20 73 75 66 66 69 78 20 64 61  append suffix da
32a06 74 61 2e 20 2a 2f 0a 20 20 20 20 70 52 65 61 64  ta. */.    pRead
32a07 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 3d  er->term.nData =
32a08 20 6e 50 72 65 66 69 78 3b 0a 20 20 20 20 64 61   nPrefix;.    da
32a09 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26  taBufferAppend(&
32a0a 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70  pReader->term, p
32a0b 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
32a0c 20 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20   nSuffix);..    
32a0d 61 73 73 65 72 74 28 20 6e 2b 6e 53 75 66 66 69  assert( n+nSuffi
32a0e 78 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  x<=pReader->nDat
32a0f 61 20 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72  a );.    pReader
32a10 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75  ->pData += n+nSu
32a11 66 66 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65  ffix;.    pReade
32a12 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53  r->nData -= n+nS
32a13 75 66 66 69 78 3b 0a 20 20 7d 0a 20 20 70 52 65  uffix;.  }.  pRe
32a14 61 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 2b 2b  ader->iBlockid++
32a15 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6d 70 61 72 65 20  ;.}../* Compare 
32a16 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d  the current term
32a17 20 74 6f 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d   to pTerm[nTerm]
32a18 2c 20 72 65 74 75 72 6e 69 6e 67 20 73 74 72 63  , returning strc
32a19 6d 70 2d 73 74 79 6c 65 0a 2a 2a 20 72 65 73 75  mp-style.** resu
32a1a 6c 74 73 2e 20 20 49 66 20 69 73 50 72 65 66 69  lts.  If isPrefi
32a1b 78 2c 20 65 71 75 61 6c 69 74 79 20 6d 65 61 6e  x, equality mean
32a1c 73 20 65 71 75 61 6c 20 74 68 72 6f 75 67 68 20  s equal through 
32a1d 6e 54 65 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a  nTerm bytes..*/.
32a1e 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
32a1f 69 6f 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70  iorReaderTermCmp
32a20 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20  (InteriorReader 
32a21 2a 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20  *pReader,.      
32a22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a23 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
32a24 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
32a25 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
32a26 72 65 66 69 78 29 7b 0a 20 20 63 6f 6e 73 74 20  refix){.  const 
32a27 63 68 61 72 20 2a 70 52 65 61 64 65 72 54 65 72  char *pReaderTer
32a28 6d 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64  m = interiorRead
32a29 65 72 54 65 72 6d 28 70 52 65 61 64 65 72 29 3b  erTerm(pReader);
32a2a 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 54 65  .  int nReaderTe
32a2b 72 6d 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61  rm = interiorRea
32a2c 64 65 72 54 65 72 6d 42 79 74 65 73 28 70 52 65  derTermBytes(pRe
32a2d 61 64 65 72 29 3b 0a 20 20 69 6e 74 20 63 2c 20  ader);.  int c, 
32a2e 6e 20 3d 20 6e 52 65 61 64 65 72 54 65 72 6d 3c  n = nReaderTerm<
32a2f 6e 54 65 72 6d 20 3f 20 6e 52 65 61 64 65 72 54  nTerm ? nReaderT
32a30 65 72 6d 20 3a 20 6e 54 65 72 6d 3b 0a 0a 20 20  erm : nTerm;..  
32a31 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  if( n==0 ){.    
32a32 69 66 28 20 6e 52 65 61 64 65 72 54 65 72 6d 3e  if( nReaderTerm>
32a33 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  0 ) return -1;. 
32a34 20 20 20 69 66 28 20 6e 54 65 72 6d 3e 30 20 29     if( nTerm>0 )
32a35 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 72   return 1;.    r
32a36 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
32a37 63 20 3d 20 6d 65 6d 63 6d 70 28 70 52 65 61 64  c = memcmp(pRead
32a38 65 72 54 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e  erTerm, pTerm, n
32a39 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
32a3a 72 65 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20  return c;.  if( 
32a3b 69 73 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e  isPrefix && n==n
32a3c 54 65 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  Term ) return 0;
32a3d 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 61 64 65  .  return nReade
32a3e 72 54 65 72 6d 20 2d 20 6e 54 65 72 6d 3b 0a 7d  rTerm - nTerm;.}
32a3f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
32a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a43 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66 57 72 69 74  ***/./* LeafWrit
32a44 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  er is used to co
32a45 6c 6c 65 63 74 20 74 65 72 6d 73 20 61 6e 64 20  llect terms and 
32a46 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69  associated docli
32a47 73 74 20 64 61 74 61 0a 2a 2a 20 69 6e 74 6f 20  st data.** into 
32a48 6c 65 61 66 20 62 6c 6f 63 6b 73 20 69 6e 20 25  leaf blocks in %
32a49 5f 73 65 67 6d 65 6e 74 73 20 28 73 65 65 20 74  _segments (see t
32a4a 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 66  op of file for f
32a4b 6f 72 6d 61 74 20 69 6e 66 6f 29 2e 0a 2a 2a 20  ormat info)..** 
32a4c 45 78 70 65 63 74 65 64 20 75 73 61 67 65 20 69  Expected usage i
32a4d 73 3a 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 57 72 69  s:.**.** LeafWri
32a4e 74 65 72 20 77 72 69 74 65 72 3b 0a 2a 2a 20 6c  ter writer;.** l
32a4f 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 30 2c  eafWriterInit(0,
32a50 20 30 2c 20 26 77 72 69 74 65 72 29 3b 0a 2a 2a   0, &writer);.**
32a51 20 77 68 69 6c 65 28 20 73 6f 72 74 65 64 5f 74   while( sorted_t
32a52 65 72 6d 73 5f 6c 65 66 74 5f 74 6f 5f 70 72 6f  erms_left_to_pro
32a53 63 65 73 73 20 29 7b 0a 2a 2a 20 20 20 2f 2f 20  cess ){.**   // 
32a54 64 61 74 61 20 69 73 20 64 6f 63 6c 69 73 74 20  data is doclist 
32a55 64 61 74 61 20 66 6f 72 20 74 68 61 74 20 74 65  data for that te
32a56 72 6d 2e 0a 2a 2a 20 20 20 72 63 20 3d 20 6c 65  rm..**   rc = le
32a57 61 66 57 72 69 74 65 72 53 74 65 70 28 76 2c 20  afWriterStep(v, 
32a58 26 77 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20  &writer, pTerm, 
32a59 6e 54 65 72 6d 2c 20 70 44 61 74 61 2c 20 6e 44  nTerm, pData, nD
32a5a 61 74 61 29 3b 0a 2a 2a 20 20 20 69 66 28 20 72  ata);.**   if( r
32a5b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
32a5c 6f 74 6f 20 65 72 72 3b 0a 2a 2a 20 7d 0a 2a 2a  oto err;.** }.**
32a5d 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
32a5e 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69  Finalize(v, &wri
32a5f 74 65 72 29 3b 0a 2a 2a 65 72 72 3a 0a 2a 2a 20  ter);.**err:.** 
32a60 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f  leafWriterDestro
32a61 79 28 26 77 72 69 74 65 72 29 3b 0a 2a 2a 20 72  y(&writer);.** r
32a62 65 74 75 72 6e 20 72 63 3b 0a 2a 2a 0a 2a 2a 20  eturn rc;.**.** 
32a63 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28 29  leafWriterStep()
32a64 20 6d 61 79 20 77 72 69 74 65 20 61 20 63 6f 6c   may write a col
32a65 6c 65 63 74 65 64 20 6c 65 61 66 20 6f 75 74 20  lected leaf out 
32a66 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a  to %_segments..*
32a67 2a 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61  * leafWriterFina
32a68 6c 69 7a 65 28 29 20 66 69 6e 69 73 68 65 73 20  lize() finishes 
32a69 77 72 69 74 69 6e 67 20 61 6e 79 20 62 75 66 66  writing any buff
32a6a 65 72 65 64 20 64 61 74 61 20 61 6e 64 20 73 74  ered data and st
32a6b 6f 72 65 73 0a 2a 2a 20 61 20 72 6f 6f 74 20 6e  ores.** a root n
32a6c 6f 64 65 20 69 6e 20 25 5f 73 65 67 64 69 72 2e  ode in %_segdir.
32a6d 20 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74    leafWriterDest
32a6e 72 6f 79 28 29 20 66 72 65 65 73 20 61 6c 6c 20  roy() frees all 
32a6f 62 75 66 66 65 72 73 20 61 6e 64 0a 2a 2a 20 49  buffers and.** I
32a70 6e 74 65 72 69 6f 72 57 72 69 74 65 72 73 20 61  nteriorWriters a
32a71 6c 6c 6f 63 61 74 65 64 20 61 73 20 70 61 72 74  llocated as part
32a72 20 6f 66 20 77 72 69 74 69 6e 67 20 74 68 69 73   of writing this
32a73 20 73 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   segment..**.** 
32a74 54 4f 44 4f 28 73 68 65 73 73 29 20 44 6f 63 75  TODO(shess) Docu
32a75 6d 65 6e 74 20 6c 65 61 66 57 72 69 74 65 72 53  ment leafWriterS
32a76 74 65 70 4d 65 72 67 65 28 29 2e 0a 2a 2f 0a 0a  tepMerge()..*/..
32a77 2f 2a 20 50 75 74 20 74 65 72 6d 73 20 77 69 74  /* Put terms wit
32a78 68 20 64 61 74 61 20 74 68 69 73 20 62 69 67 20  h data this big 
32a79 69 6e 20 74 68 65 69 72 20 6f 77 6e 20 62 6c 6f  in their own blo
32a7a 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ck. */.#define S
32a7b 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 31 30  TANDALONE_MIN 10
32a7c 32 34 0a 0a 2f 2a 20 4b 65 65 70 20 6c 65 61 66  24../* Keep leaf
32a7d 20 62 6c 6f 63 6b 73 20 62 65 6c 6f 77 20 74 68   blocks below th
32a7e 69 73 20 73 69 7a 65 2e 20 2a 2f 0a 23 64 65 66  is size. */.#def
32a7f 69 6e 65 20 4c 45 41 46 5f 4d 41 58 20 32 30 34  ine LEAF_MAX 204
32a80 38 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  8..typedef struc
32a81 74 20 4c 65 61 66 57 72 69 74 65 72 20 7b 0a 20  t LeafWriter {. 
32a82 20 69 6e 74 20 69 4c 65 76 65 6c 3b 0a 20 20 69   int iLevel;.  i
32a83 6e 74 20 69 64 78 3b 0a 20 20 73 71 6c 69 74 65  nt idx;.  sqlite
32a84 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
32a85 63 6b 69 64 3b 20 20 20 20 20 2f 2a 20 6e 65 65  ckid;     /* nee
32a86 64 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  ded to create th
32a87 65 20 72 6f 6f 74 20 69 6e 66 6f 20 2a 2f 0a 20  e root info */. 
32a88 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
32a89 6e 64 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20  ndBlockid;      
32a8a 20 2f 2a 20 77 68 65 6e 20 77 65 27 72 65 20 64   /* when we're d
32a8b 6f 6e 65 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a  one writing. */.
32a8c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65  .  DataBuffer te
32a8d 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
32a8e 20 20 20 2f 2a 20 70 72 65 76 69 6f 75 73 20 65     /* previous e
32a8f 6e 63 6f 64 65 64 20 74 65 72 6d 20 2a 2f 0a 20  ncoded term */. 
32a90 20 44 61 74 61 42 75 66 66 65 72 20 64 61 74 61   DataBuffer data
32a91 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
32a92 20 2f 2a 20 65 6e 63 6f 64 69 6e 67 20 62 75 66   /* encoding buf
32a93 66 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 62 79 74  fer */..  /* byt
32a94 65 73 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  es of first term
32a95 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
32a96 6e 6f 64 65 20 77 68 69 63 68 20 64 69 73 74 69  node which disti
32a97 6e 67 75 69 73 68 65 73 20 74 68 61 74 0a 20 20  nguishes that.  
32a98 2a 2a 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65  ** term from the
32a99 20 6c 61 73 74 20 74 65 72 6d 20 6f 66 20 74 68   last term of th
32a9a 65 20 70 72 65 76 69 6f 75 73 20 6e 6f 64 65 2e  e previous node.
32a9b 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  .  */.  int nTer
32a9c 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 49 6e  mDistinct;..  In
32a9d 74 65 72 69 6f 72 57 72 69 74 65 72 20 70 61 72  teriorWriter par
32a9e 65 6e 74 57 72 69 74 65 72 3b 20 20 20 20 2f 2a  entWriter;    /*
32a9f 20 69 66 20 77 65 20 6f 76 65 72 66 6c 6f 77 20   if we overflow 
32aa0 2a 2f 0a 20 20 69 6e 74 20 68 61 73 5f 70 61 72  */.  int has_par
32aa1 65 6e 74 3b 0a 7d 20 4c 65 61 66 57 72 69 74 65  ent;.} LeafWrite
32aa2 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
32aa3 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69  leafWriterInit(i
32aa4 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69  nt iLevel, int i
32aa5 64 78 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  dx, LeafWriter *
32aa6 70 57 72 69 74 65 72 29 7b 0a 20 20 43 4c 45 41  pWriter){.  CLEA
32aa7 52 28 70 57 72 69 74 65 72 29 3b 0a 20 20 70 57  R(pWriter);.  pW
32aa8 72 69 74 65 72 2d 3e 69 4c 65 76 65 6c 20 3d 20  riter->iLevel = 
32aa9 69 4c 65 76 65 6c 3b 0a 20 20 70 57 72 69 74 65  iLevel;.  pWrite
32aaa 72 2d 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20  r->idx = idx;.. 
32aab 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
32aac 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20  &pWriter->term, 
32aad 33 32 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74  32);..  /* Start
32aae 20 6f 75 74 20 77 69 74 68 20 61 20 72 65 61 73   out with a reas
32aaf 6f 6e 61 62 6c 79 20 73 69 7a 65 64 20 62 6c 6f  onably sized blo
32ab0 63 6b 2c 20 74 68 6f 75 67 68 20 69 74 20 63 61  ck, though it ca
32ab1 6e 20 67 72 6f 77 2e 20 2a 2f 0a 20 20 64 61 74  n grow. */.  dat
32ab2 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 57 72  aBufferInit(&pWr
32ab3 69 74 65 72 2d 3e 64 61 74 61 2c 20 4c 45 41 46  iter->data, LEAF
32ab4 5f 4d 41 58 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  _MAX);.}..#ifnde
32ab5 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69  f NDEBUG./* Veri
32ab6 66 79 20 74 68 61 74 20 74 68 65 20 64 61 74 61  fy that the data
32ab7 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 73 20   is readable as 
32ab8 61 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a  a leaf node. */.
32ab9 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66  static void leaf
32aba 4e 6f 64 65 56 61 6c 69 64 61 74 65 28 63 6f 6e  NodeValidate(con
32abb 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
32abc 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 69 6e  int nData){.  in
32abd 74 20 6e 2c 20 69 44 75 6d 6d 79 3b 0a 0a 20 20  t n, iDummy;..  
32abe 69 66 28 20 6e 44 61 74 61 3d 3d 30 20 29 20 72  if( nData==0 ) r
32abf 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28  eturn;.  assert(
32ac0 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73   nData>0 );.  as
32ac1 73 65 72 74 28 20 70 44 61 74 61 21 3d 30 20 29  sert( pData!=0 )
32ac2 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74  ;.  assert( pDat
32ac3 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b  a+nData>pData );
32ac4 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64  ..  /* Must lead
32ac5 20 77 69 74 68 20 61 20 76 61 72 69 6e 74 28 30   with a varint(0
32ac6 29 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  ) */.  n = fts3G
32ac7 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
32ac8 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
32ac9 73 65 72 74 28 20 69 44 75 6d 6d 79 3d 3d 30 20  sert( iDummy==0 
32aca 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30  );.  assert( n>0
32acb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c   );.  assert( n<
32acc 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61 74 61  nData );.  pData
32acd 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d   += n;.  nData -
32ace 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64 69  = n;..  /* Leadi
32acf 6e 67 20 74 65 72 6d 20 6c 65 6e 67 74 68 20 61  ng term length a
32ad0 6e 64 20 64 61 74 61 20 6d 75 73 74 20 66 69 74  nd data must fit
32ad1 20 69 6e 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20   in buffer. */. 
32ad2 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
32ad3 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
32ad4 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
32ad5 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
32ad6 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61   iDummy>0 );.  a
32ad7 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e  ssert( n+iDummy>
32ad8 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
32ad9 2b 69 44 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b  +iDummy<nData );
32ada 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44  .  pData += n+iD
32adb 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d  ummy;.  nData -=
32adc 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a   n+iDummy;..  /*
32add 20 4c 65 61 64 69 6e 67 20 74 65 72 6d 27 73 20   Leading term's 
32ade 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68 20 61  doclist length a
32adf 6e 64 20 64 61 74 61 20 6d 75 73 74 20 66 69 74  nd data must fit
32ae0 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47  . */.  n = fts3G
32ae1 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
32ae2 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
32ae3 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61  sert( n>0 );.  a
32ae4 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20  ssert( iDummy>0 
32ae5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69  );.  assert( n+i
32ae6 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73  Dummy>0 );.  ass
32ae7 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
32ae8 44 61 74 61 20 29 3b 0a 20 20 41 53 53 45 52 54  Data );.  ASSERT
32ae9 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44  _VALID_DOCLIST(D
32aea 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61  L_DEFAULT, pData
32aeb 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c  +n, iDummy, NULL
32aec 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b  );.  pData += n+
32aed 69 44 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20  iDummy;.  nData 
32aee 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20  -= n+iDummy;..  
32aef 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
32af0 72 61 69 6c 69 6e 67 20 74 65 72 6d 73 20 61 6e  railing terms an
32af1 64 20 64 6f 63 6c 69 73 74 73 20 61 6c 73 6f 20  d doclists also 
32af2 61 72 65 20 72 65 61 64 61 62 6c 65 2e 20 2a 2f  are readable. */
32af3 0a 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21  .  while( nData!
32af4 3d 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74  =0 ){.    n = ft
32af5 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
32af6 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  ata, &iDummy);. 
32af7 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
32af8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
32af9 75 6d 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 61  ummy>=0 );.    a
32afa 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29  ssert( n<nData )
32afb 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
32afc 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e  ;.    nData -= n
32afd 3b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65  ;.    n = fts3Ge
32afe 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
32aff 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61   &iDummy);.    a
32b00 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
32b01 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
32b02 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
32b03 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  ( n+iDummy>0 );.
32b04 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
32b05 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20  ummy<nData );.  
32b06 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75    pData += n+iDu
32b07 6d 6d 79 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d  mmy;.    nData -
32b08 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 20  = n+iDummy;..   
32b09 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
32b0a 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
32b0b 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74  mmy);.    assert
32b0c 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ( n>0 );.    ass
32b0d 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
32b0e 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69  .    assert( n+i
32b0f 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
32b10 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
32b11 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 41 53  =nData );.    AS
32b12 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49  SERT_VALID_DOCLI
32b13 53 54 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  ST(DL_DEFAULT, p
32b14 44 61 74 61 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20  Data+n, iDummy, 
32b15 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 44 61 74 61  NULL);.    pData
32b16 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20   += n+iDummy;.  
32b17 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75    nData -= n+iDu
32b18 6d 6d 79 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69  mmy;.  }.}.#defi
32b19 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
32b1a 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20  LEAF_NODE(p, n) 
32b1b 6c 65 61 66 4e 6f 64 65 56 61 6c 69 64 61 74 65  leafNodeValidate
32b1c 28 70 2c 20 6e 29 0a 23 65 6c 73 65 0a 23 64 65  (p, n).#else.#de
32b1d 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49  fine ASSERT_VALI
32b1e 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e  D_LEAF_NODE(p, n
32b1f 29 20 61 73 73 65 72 74 28 20 31 20 29 0a 23 65  ) assert( 1 ).#e
32b20 6e 64 69 66 0a 0a 2f 2a 20 46 6c 75 73 68 20 74  ndif../* Flush t
32b21 68 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 20  he current leaf 
32b22 6e 6f 64 65 20 74 6f 20 25 5f 73 65 67 6d 65 6e  node to %_segmen
32b23 74 73 2c 20 61 6e 64 20 61 64 64 69 6e 67 20 74  ts, and adding t
32b24 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20  he resulting.** 
32b25 62 6c 6f 63 6b 69 64 20 61 6e 64 20 74 68 65 20  blockid and the 
32b26 73 74 61 72 74 69 6e 67 20 74 65 72 6d 20 74 6f  starting term to
32b27 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f   the interior no
32b28 64 65 20 77 68 69 63 68 20 77 69 6c 6c 0a 2a 2a  de which will.**
32b29 20 63 6f 6e 74 61 69 6e 20 69 74 2e 0a 2a 2f 0a   contain it..*/.
32b2a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
32b2b 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75  riterInternalFlu
32b2c 73 68 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  sh(fulltext_vtab
32b2d 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20   *v, LeafWriter 
32b2e 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
32b2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b30 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
32b31 20 69 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74   iData, int nDat
32b32 61 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  a){.  sqlite_int
32b33 36 34 20 69 42 6c 6f 63 6b 69 64 20 3d 20 30 3b  64 iBlockid = 0;
32b34 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
32b35 53 74 61 72 74 69 6e 67 54 65 72 6d 3b 0a 20 20  StartingTerm;.  
32b36 69 6e 74 20 6e 53 74 61 72 74 69 6e 67 54 65 72  int nStartingTer
32b37 6d 2c 20 72 63 2c 20 6e 3b 0a 0a 20 20 2f 2a 20  m, rc, n;..  /* 
32b38 4d 75 73 74 20 68 61 76 65 20 74 68 65 20 6c 65  Must have the le
32b39 61 64 69 6e 67 20 76 61 72 69 6e 74 28 30 29 20  ading varint(0) 
32b3a 66 6c 61 67 2c 20 70 6c 75 73 20 61 74 20 6c 65  flag, plus at le
32b3b 61 73 74 20 73 6f 6d 65 0a 20 20 2a 2a 20 76 61  ast some.  ** va
32b3c 6c 69 64 2d 6c 6f 6f 6b 69 6e 67 20 64 61 74 61  lid-looking data
32b3d 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
32b3e 20 6e 44 61 74 61 3e 32 20 29 3b 0a 20 20 61 73   nData>2 );.  as
32b3f 73 65 72 74 28 20 69 44 61 74 61 3e 3d 30 20 29  sert( iData>=0 )
32b40 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 61 74  ;.  assert( iDat
32b41 61 2b 6e 44 61 74 61 3c 3d 70 57 72 69 74 65 72  a+nData<=pWriter
32b42 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 29 3b 0a  ->data.nData );.
32b43 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c    ASSERT_VALID_L
32b44 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72  EAF_NODE(pWriter
32b45 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
32b46 74 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20 72  ta, nData);..  r
32b47 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74  c = block_insert
32b48 28 76 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74  (v, pWriter->dat
32b49 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e  a.pData+iData, n
32b4a 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29  Data, &iBlockid)
32b4b 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
32b4c 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
32b4d 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c  c;.  assert( iBl
32b4e 6f 63 6b 69 64 21 3d 30 20 29 3b 0a 0a 20 20 2f  ockid!=0 );..  /
32b4f 2a 20 52 65 63 6f 6e 73 74 72 75 63 74 20 74 68  * Reconstruct th
32b50 65 20 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20  e first term in 
32b51 74 68 65 20 6c 65 61 66 20 66 6f 72 20 70 75 72  the leaf for pur
32b52 70 6f 73 65 73 20 6f 66 20 62 75 69 6c 64 69 6e  poses of buildin
32b53 67 0a 20 20 2a 2a 20 74 68 65 20 69 6e 74 65 72  g.  ** the inter
32b54 69 6f 72 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ior node..  */. 
32b55 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
32b56 6e 74 33 32 28 70 57 72 69 74 65 72 2d 3e 64 61  nt32(pWriter->da
32b57 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31  ta.pData+iData+1
32b58 2c 20 26 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  , &nStartingTerm
32b59 29 3b 0a 20 20 70 53 74 61 72 74 69 6e 67 54 65  );.  pStartingTe
32b5a 72 6d 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61  rm = pWriter->da
32b5b 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31  ta.pData+iData+1
32b5c 2b 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57  +n;.  assert( pW
32b5d 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
32b5e 61 3e 69 44 61 74 61 2b 31 2b 6e 2b 6e 53 74 61  a>iData+1+n+nSta
32b5f 72 74 69 6e 67 54 65 72 6d 20 29 3b 0a 20 20 61  rtingTerm );.  a
32b60 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
32b61 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3e 30 20  nTermDistinct>0 
32b62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72  );.  assert( pWr
32b63 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69  iter->nTermDisti
32b64 6e 63 74 3c 3d 6e 53 74 61 72 74 69 6e 67 54 65  nct<=nStartingTe
32b65 72 6d 20 29 3b 0a 20 20 6e 53 74 61 72 74 69 6e  rm );.  nStartin
32b66 67 54 65 72 6d 20 3d 20 70 57 72 69 74 65 72 2d  gTerm = pWriter-
32b67 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a  >nTermDistinct;.
32b68 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
32b69 68 61 73 5f 70 61 72 65 6e 74 20 29 7b 0a 20 20  has_parent ){.  
32b6a 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72    interiorWriter
32b6b 41 70 70 65 6e 64 28 26 70 57 72 69 74 65 72 2d  Append(&pWriter-
32b6c 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20  >parentWriter,. 
32b6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b6e 20 20 20 20 20 20 20 20 70 53 74 61 72 74 69 6e          pStartin
32b6f 67 54 65 72 6d 2c 20 6e 53 74 61 72 74 69 6e 67  gTerm, nStarting
32b70 54 65 72 6d 2c 20 69 42 6c 6f 63 6b 69 64 29 3b  Term, iBlockid);
32b71 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
32b72 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74  teriorWriterInit
32b73 28 31 2c 20 70 53 74 61 72 74 69 6e 67 54 65 72  (1, pStartingTer
32b74 6d 2c 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  m, nStartingTerm
32b75 2c 20 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  , iBlockid,.    
32b76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b77 20 20 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72     &pWriter->par
32b78 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20  entWriter);.    
32b79 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72  pWriter->has_par
32b7a 65 6e 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  ent = 1;.  }..  
32b7b 2f 2a 20 54 72 61 63 6b 20 74 68 65 20 73 70 61  /* Track the spa
32b7c 6e 20 6f 66 20 74 68 69 73 20 73 65 67 6d 65 6e  n of this segmen
32b7d 74 27 73 20 6c 65 61 66 20 6e 6f 64 65 73 2e 20  t's leaf nodes. 
32b7e 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72  */.  if( pWriter
32b7f 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 3d 3d 30  ->iEndBlockid==0
32b80 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d   ){.    pWriter-
32b81 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70  >iEndBlockid = p
32b82 57 72 69 74 65 72 2d 3e 69 53 74 61 72 74 42 6c  Writer->iStartBl
32b83 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64  ockid = iBlockid
32b84 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
32b85 57 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63  Writer->iEndBloc
32b86 6b 69 64 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72  kid++;.    asser
32b87 74 28 20 69 42 6c 6f 63 6b 69 64 3d 3d 70 57 72  t( iBlockid==pWr
32b88 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
32b89 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  d );.  }..  retu
32b8a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
32b8b 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57  static int leafW
32b8c 72 69 74 65 72 46 6c 75 73 68 28 66 75 6c 6c 74  riterFlush(fullt
32b8d 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
32b8e 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
32b8f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c 65  ){.  int rc = le
32b90 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c  afWriterInternal
32b91 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72  Flush(v, pWriter
32b92 2c 20 30 2c 20 70 57 72 69 74 65 72 2d 3e 64 61  , 0, pWriter->da
32b93 74 61 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28  ta.nData);.  if(
32b94 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32b95 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
32b96 2a 20 52 65 2d 69 6e 69 74 69 61 6c 69 7a 65 20  * Re-initialize 
32b97 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
32b98 72 2e 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66  r. */.  dataBuff
32b99 65 72 52 65 73 65 74 28 26 70 57 72 69 74 65 72  erReset(&pWriter
32b9a 2d 3e 64 61 74 61 29 3b 0a 0a 20 20 72 65 74 75  ->data);..  retu
32b9b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
32b9c 0a 2f 2a 20 46 65 74 63 68 20 74 68 65 20 72 6f  ./* Fetch the ro
32b9d 6f 74 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20  ot info for the 
32b9e 73 65 67 6d 65 6e 74 2e 20 20 49 66 20 74 68 65  segment.  If the
32b9f 20 65 6e 74 69 72 65 20 6c 65 61 66 20 66 69 74   entire leaf fit
32ba0 73 0a 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f 54  s.** within ROOT
32ba1 5f 4d 41 58 2c 20 74 68 65 6e 20 69 74 20 77 69  _MAX, then it wi
32ba2 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 64  ll be returned d
32ba3 69 72 65 63 74 6c 79 2c 20 6f 74 68 65 72 77 69  irectly, otherwi
32ba4 73 65 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 62 65  se it.** will be
32ba5 20 66 6c 75 73 68 65 64 20 61 6e 64 20 74 68 65   flushed and the
32ba6 20 72 6f 6f 74 20 69 6e 66 6f 20 77 69 6c 6c 20   root info will 
32ba7 62 65 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  be returned from
32ba8 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f 72   the.** interior
32ba9 20 6e 6f 64 65 2e 20 20 2a 70 69 45 6e 64 42 6c   node.  *piEndBl
32baa 6f 63 6b 69 64 20 69 73 20 73 65 74 20 74 6f 20  ockid is set to 
32bab 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f 66 20 74  the blockid of t
32bac 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 74 65 72  he last.** inter
32bad 69 6f 72 20 6f 72 20 6c 65 61 66 20 6e 6f 64 65  ior or leaf node
32bae 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b   written to disk
32baf 20 28 30 20 69 66 20 6e 6f 6e 65 20 61 72 65 20   (0 if none are 
32bb0 77 72 69 74 74 65 6e 20 61 74 0a 2a 2a 20 61 6c  written at.** al
32bb1 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  l)..*/.static in
32bb2 74 20 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74  t leafWriterRoot
32bb3 49 6e 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74  Info(fulltext_vt
32bb4 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65  ab *v, LeafWrite
32bb5 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
32bb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bb7 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
32bb8 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e 74  *ppRootInfo, int
32bb9 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20 20   *pnRootInfo,.  
32bba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bbb 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
32bbc 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42  te_int64 *piEndB
32bbd 6c 6f 63 6b 69 64 29 7b 0a 20 20 2f 2a 20 77 65  lockid){.  /* we
32bbe 20 63 61 6e 20 66 69 74 20 74 68 65 20 73 65 67   can fit the seg
32bbf 6d 65 6e 74 20 65 6e 74 69 72 65 6c 79 20 69 6e  ment entirely in
32bc0 6c 69 6e 65 20 2a 2f 0a 20 20 69 66 28 20 21 70  line */.  if( !p
32bc1 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65  Writer->has_pare
32bc2 6e 74 20 26 26 20 70 57 72 69 74 65 72 2d 3e 64  nt && pWriter->d
32bc3 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d  ata.nData<ROOT_M
32bc4 41 58 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f  AX ){.    *ppRoo
32bc5 74 49 6e 66 6f 20 3d 20 70 57 72 69 74 65 72 2d  tInfo = pWriter-
32bc6 3e 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 20  >data.pData;.   
32bc7 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20 70   *pnRootInfo = p
32bc8 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
32bc9 74 61 3b 0a 20 20 20 20 2a 70 69 45 6e 64 42 6c  ta;.    *piEndBl
32bca 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20 20 20 72  ockid = 0;.    r
32bcb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
32bcc 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6c 75 73 68  .  }..  /* Flush
32bcd 20 72 65 6d 61 69 6e 69 6e 67 20 6c 65 61 66 20   remaining leaf 
32bce 64 61 74 61 2e 20 2a 2f 0a 20 20 69 66 28 20 70  data. */.  if( p
32bcf 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
32bd0 74 61 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  ta>0 ){.    int 
32bd1 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46  rc = leafWriterF
32bd2 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 29  lush(v, pWriter)
32bd3 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
32bd4 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
32bd5 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57   rc;.  }..  /* W
32bd6 65 20 6d 75 73 74 20 68 61 76 65 20 66 6c 75 73  e must have flus
32bd7 68 65 64 20 61 20 6c 65 61 66 20 61 74 20 73 6f  hed a leaf at so
32bd8 6d 65 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 61  me point. */.  a
32bd9 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
32bda 68 61 73 5f 70 61 72 65 6e 74 20 29 3b 0a 0a 20  has_parent );.. 
32bdb 20 2f 2a 20 54 65 6e 61 74 69 76 65 6c 79 20 73   /* Tenatively s
32bdc 65 74 20 74 68 65 20 65 6e 64 20 6c 65 61 66 20  et the end leaf 
32bdd 62 6c 6f 63 6b 69 64 20 61 73 20 74 68 65 20 65  blockid as the e
32bde 6e 64 20 62 6c 6f 63 6b 69 64 2e 20 20 49 66 20  nd blockid.  If 
32bdf 74 68 65 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f  the.  ** interio
32be0 72 20 6e 6f 64 65 20 63 61 6e 20 62 65 20 72 65  r node can be re
32be1 74 75 72 6e 65 64 20 69 6e 6c 69 6e 65 2c 20 74  turned inline, t
32be2 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20  his will be the 
32be3 66 69 6e 61 6c 0a 20 20 2a 2a 20 62 6c 6f 63 6b  final.  ** block
32be4 69 64 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74  id, otherwise it
32be5 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69   will be overwri
32be6 74 74 65 6e 20 62 79 0a 20 20 2a 2a 20 69 6e 74  tten by.  ** int
32be7 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49  eriorWriterRootI
32be8 6e 66 6f 28 29 2e 0a 20 20 2a 2f 0a 20 20 2a 70  nfo()..  */.  *p
32be9 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70 57  iEndBlockid = pW
32bea 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b  riter->iEndBlock
32beb 69 64 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e  id;..  return in
32bec 74 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74  teriorWriterRoot
32bed 49 6e 66 6f 28 76 2c 20 26 70 57 72 69 74 65 72  Info(v, &pWriter
32bee 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a  ->parentWriter,.
32bef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32bf1 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 70 6e 52 6f  ppRootInfo, pnRo
32bf2 6f 74 49 6e 66 6f 2c 20 70 69 45 6e 64 42 6c 6f  otInfo, piEndBlo
32bf3 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6c  ckid);.}../* Col
32bf4 6c 65 63 74 20 74 68 65 20 72 6f 6f 74 49 6e 66  lect the rootInf
32bf5 6f 20 64 61 74 61 20 61 6e 64 20 73 74 6f 72 65  o data and store
32bf6 20 69 74 20 69 6e 74 6f 20 74 68 65 20 73 65 67   it into the seg
32bf7 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 2e 0a  ment directory..
32bf8 2a 2a 20 54 68 69 73 20 68 61 73 20 74 68 65 20  ** This has the 
32bf9 65 66 66 65 63 74 20 6f 66 20 66 6c 75 73 68 69  effect of flushi
32bfa 6e 67 20 74 68 65 20 73 65 67 6d 65 6e 74 27 73  ng the segment's
32bfb 20 6c 65 61 66 20 64 61 74 61 20 74 6f 0a 2a 2a   leaf data to.**
32bfc 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64   %_segments, and
32bfd 20 61 6c 73 6f 20 66 6c 75 73 68 69 6e 67 20 61   also flushing a
32bfe 6e 79 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ny interior node
32bff 73 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e  s to %_segments.
32c00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
32c01 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
32c02 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
32c03 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  *v, LeafWriter *
32c04 70 57 72 69 74 65 72 29 7b 0a 20 20 73 71 6c 69  pWriter){.  sqli
32c05 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  te_int64 iEndBlo
32c06 63 6b 69 64 3b 0a 20 20 63 68 61 72 20 2a 70 52  ckid;.  char *pR
32c07 6f 6f 74 49 6e 66 6f 3b 0a 20 20 69 6e 74 20 72  ootInfo;.  int r
32c08 63 2c 20 6e 52 6f 6f 74 49 6e 66 6f 3b 0a 0a 20  c, nRootInfo;.. 
32c09 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
32c0a 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72 69  RootInfo(v, pWri
32c0b 74 65 72 2c 20 26 70 52 6f 6f 74 49 6e 66 6f 2c  ter, &pRootInfo,
32c0c 20 26 6e 52 6f 6f 74 49 6e 66 6f 2c 20 26 69 45   &nRootInfo, &iE
32c0d 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66  ndBlockid);.  if
32c0e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
32c0f 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
32c10 2f 2a 20 44 6f 6e 27 74 20 62 6f 74 68 65 72 20  /* Don't bother 
32c11 73 74 6f 72 69 6e 67 20 61 6e 20 65 6e 74 69 72  storing an entir
32c12 65 6c 79 20 65 6d 70 74 79 20 73 65 67 6d 65 6e  ely empty segmen
32c13 74 2e 20 2a 2f 0a 20 20 69 66 28 20 69 45 6e 64  t. */.  if( iEnd
32c14 42 6c 6f 63 6b 69 64 3d 3d 30 20 26 26 20 6e 52  Blockid==0 && nR
32c15 6f 6f 74 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74  ootInfo==0 ) ret
32c16 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
32c17 20 20 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f    return segdir_
32c18 73 65 74 28 76 2c 20 70 57 72 69 74 65 72 2d 3e  set(v, pWriter->
32c19 69 4c 65 76 65 6c 2c 20 70 57 72 69 74 65 72 2d  iLevel, pWriter-
32c1a 3e 69 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20  >idx,.          
32c1b 20 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65            pWrite
32c1c 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  r->iStartBlockid
32c1d 2c 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42  , pWriter->iEndB
32c1e 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20  lockid,.        
32c1f 20 20 20 20 20 20 20 20 20 20 20 20 69 45 6e 64              iEnd
32c20 42 6c 6f 63 6b 69 64 2c 20 70 52 6f 6f 74 49 6e  Blockid, pRootIn
32c21 66 6f 2c 20 6e 52 6f 6f 74 49 6e 66 6f 29 3b 0a  fo, nRootInfo);.
32c22 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  }..static void l
32c23 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79  eafWriterDestroy
32c24 28 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72  (LeafWriter *pWr
32c25 69 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72  iter){.  if( pWr
32c26 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74  iter->has_parent
32c27 20 29 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65   ) interiorWrite
32c28 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74 65  rDestroy(&pWrite
32c29 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29  r->parentWriter)
32c2a 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
32c2b 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e  stroy(&pWriter->
32c2c 74 65 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66  term);.  dataBuf
32c2d 66 65 72 44 65 73 74 72 6f 79 28 26 70 57 72 69  ferDestroy(&pWri
32c2e 74 65 72 2d 3e 64 61 74 61 29 3b 0a 7d 0a 0a 2f  ter->data);.}../
32c2f 2a 20 45 6e 63 6f 64 65 20 61 20 74 65 72 6d 20  * Encode a term 
32c30 69 6e 74 6f 20 74 68 65 20 6c 65 61 66 57 72 69  into the leafWri
32c31 74 65 72 2c 20 64 65 6c 74 61 2d 65 6e 63 6f 64  ter, delta-encod
32c32 69 6e 67 20 61 73 20 61 70 70 72 6f 70 72 69 61  ing as appropria
32c33 74 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74  te..** Returns t
32c34 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
32c35 20 6e 65 77 20 74 65 72 6d 20 77 68 69 63 68 20   new term which 
32c36 64 69 73 74 69 6e 67 75 69 73 68 65 73 20 69 74  distinguishes it
32c37 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 70 72 65   from the.** pre
32c38 76 69 6f 75 73 20 74 65 72 6d 2c 20 77 68 69 63  vious term, whic
32c39 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  h can be used to
32c3a 20 73 65 74 20 6e 54 65 72 6d 44 69 73 74 69 6e   set nTermDistin
32c3b 63 74 20 77 68 65 6e 20 61 20 6e 6f 64 65 0a 2a  ct when a node.*
32c3c 2a 20 62 6f 75 6e 64 61 72 79 20 69 73 20 63 72  * boundary is cr
32c3d 6f 73 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ossed..*/.static
32c3e 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 45   int leafWriterE
32c3f 6e 63 6f 64 65 54 65 72 6d 28 4c 65 61 66 57 72  ncodeTerm(LeafWr
32c40 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20  iter *pWriter,. 
32c41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
32c43 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
32c44 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20  , int nTerm){.  
32c45 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
32c46 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
32c47 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20   int n, nPrefix 
32c48 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
32c49 6e 54 65 72 6d 3e 30 20 29 3b 0a 20 20 77 68 69  nTerm>0 );.  whi
32c4a 6c 65 28 20 6e 50 72 65 66 69 78 3c 70 57 72 69  le( nPrefix<pWri
32c4b 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20  ter->term.nData 
32c4c 26 26 0a 20 20 20 20 20 20 20 20 20 70 54 65 72  &&.         pTer
32c4d 6d 5b 6e 50 72 65 66 69 78 5d 3d 3d 70 57 72 69  m[nPrefix]==pWri
32c4e 74 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 5b  ter->term.pData[
32c4f 6e 50 72 65 66 69 78 5d 20 29 7b 0a 20 20 20 20  nPrefix] ){.    
32c50 6e 50 72 65 66 69 78 2b 2b 3b 0a 20 20 20 20 2f  nPrefix++;.    /
32c51 2a 20 46 61 69 6c 69 6e 67 20 74 68 69 73 20 69  * Failing this i
32c52 6d 70 6c 69 65 73 20 74 68 61 74 20 74 68 65 20  mplies that the 
32c53 74 65 72 6d 73 20 77 65 72 65 6e 27 74 20 69 6e  terms weren't in
32c54 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 61   order. */.    a
32c55 73 73 65 72 74 28 20 6e 50 72 65 66 69 78 3c 6e  ssert( nPrefix<n
32c56 54 65 72 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  Term );.  }..  i
32c57 66 28 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  f( pWriter->data
32c58 2e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  .nData==0 ){.   
32c59 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 6e   /* Encode the n
32c5a 6f 64 65 20 68 65 61 64 65 72 20 61 6e 64 20 6c  ode header and l
32c5b 65 61 64 69 6e 67 20 74 65 72 6d 20 61 73 3a 0a  eading term as:.
32c5c 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 30      **  varint(0
32c5d 29 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74  ).    **  varint
32c5e 28 6e 54 65 72 6d 29 0a 20 20 20 20 2a 2a 20 20  (nTerm).    **  
32c5f 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
32c60 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d  ].    */.    n =
32c61 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
32c62 2c 20 27 5c 30 27 29 3b 0a 20 20 20 20 6e 20 2b  , '\0');.    n +
32c63 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
32c64 63 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  c+n, nTerm);.   
32c65 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
32c66 64 32 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  d2(&pWriter->dat
32c67 61 2c 20 63 2c 20 6e 2c 20 70 54 65 72 6d 2c 20  a, c, n, pTerm, 
32c68 6e 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  nTerm);.  }else{
32c69 0a 20 20 20 20 2f 2a 20 44 65 6c 74 61 2d 65 6e  .    /* Delta-en
32c6a 63 6f 64 65 20 74 68 65 20 74 65 72 6d 20 61 73  code the term as
32c6b 3a 0a 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74  :.    **  varint
32c6c 28 6e 50 72 65 66 69 78 29 0a 20 20 20 20 2a 2a  (nPrefix).    **
32c6d 20 20 76 61 72 69 6e 74 28 6e 53 75 66 66 69 78    varint(nSuffix
32c6e 29 0a 20 20 20 20 2a 2a 20 20 63 68 61 72 20 70  ).    **  char p
32c6f 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66  TermSuffix[nSuff
32c70 69 78 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  ix].    */.    n
32c71 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
32c72 28 63 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20  (c, nPrefix);.  
32c73 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61    n += fts3PutVa
32c74 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d  rint(c+n, nTerm-
32c75 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 64 61  nPrefix);.    da
32c76 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
32c77 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20  &pWriter->data, 
32c78 63 2c 20 6e 2c 20 70 54 65 72 6d 2b 6e 50 72 65  c, n, pTerm+nPre
32c79 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66  fix, nTerm-nPref
32c7a 69 78 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42  ix);.  }.  dataB
32c7b 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70 57  ufferReplace(&pW
32c7c 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65  riter->term, pTe
32c7d 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72  rm, nTerm);..  r
32c7e 65 74 75 72 6e 20 6e 50 72 65 66 69 78 2b 31 3b  eturn nPrefix+1;
32c7f 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 74 6f 20 61  .}../* Used to a
32c80 76 6f 69 64 20 61 20 6d 65 6d 6d 6f 76 65 20 77  void a memmove w
32c81 68 65 6e 20 61 20 6c 61 72 67 65 20 61 6d 6f 75  hen a large amou
32c82 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61  nt of doclist da
32c83 74 61 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20  ta is in.** the 
32c84 62 75 66 66 65 72 2e 20 20 54 68 69 73 20 63 6f  buffer.  This co
32c85 6e 73 74 72 75 63 74 73 20 61 20 6e 6f 64 65 20  nstructs a node 
32c86 61 6e 64 20 74 65 72 6d 20 68 65 61 64 65 72 20  and term header 
32c87 62 65 66 6f 72 65 0a 2a 2a 20 69 44 6f 63 6c 69  before.** iDocli
32c88 73 74 44 61 74 61 20 61 6e 64 20 66 6c 75 73 68  stData and flush
32c89 65 73 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  es the resulting
32c8a 20 63 6f 6d 70 6c 65 74 65 20 6e 6f 64 65 20 75   complete node u
32c8b 73 69 6e 67 0a 2a 2a 20 6c 65 61 66 57 72 69 74  sing.** leafWrit
32c8c 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28  erInternalFlush(
32c8d 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
32c8e 20 6c 65 61 66 57 72 69 74 65 72 49 6e 6c 69 6e   leafWriterInlin
32c8f 65 46 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f  eFlush(fulltext_
32c90 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69  vtab *v, LeafWri
32c91 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
32c92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
32c94 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
32c95 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20  , int nTerm,.   
32c96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
32c98 74 20 69 44 6f 63 6c 69 73 74 44 61 74 61 29 7b  t iDoclistData){
32c99 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54  .  char c[VARINT
32c9a 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d  _MAX+VARINT_MAX]
32c9b 3b 0a 20 20 69 6e 74 20 69 44 61 74 61 2c 20 6e  ;.  int iData, n
32c9c 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
32c9d 28 63 2c 20 30 29 3b 0a 20 20 6e 20 2b 3d 20 66  (c, 0);.  n += f
32c9e 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e  ts3PutVarint(c+n
32c9f 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20  , nTerm);..  /* 
32ca0 54 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77  There should alw
32ca1 61 79 73 20 62 65 20 72 6f 6f 6d 20 66 6f 72 20  ays be room for 
32ca2 74 68 65 20 68 65 61 64 65 72 2e 20 20 45 76 65  the header.  Eve
32ca3 6e 20 69 66 20 70 54 65 72 6d 20 73 68 61 72 65  n if pTerm share
32ca4 64 0a 20 20 2a 2a 20 61 20 73 75 62 73 74 61 6e  d.  ** a substan
32ca5 74 69 61 6c 20 70 72 65 66 69 78 20 77 69 74 68  tial prefix with
32ca6 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 65   the previous te
32ca7 72 6d 2c 20 74 68 65 20 65 6e 74 69 72 65 20 70  rm, the entire p
32ca8 72 65 66 69 78 0a 20 20 2a 2a 20 63 6f 75 6c 64  refix.  ** could
32ca9 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20   be constructed 
32caa 66 72 6f 6d 20 65 61 72 6c 69 65 72 20 64 61 74  from earlier dat
32cab 61 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74  a in the doclist
32cac 2c 20 73 6f 20 74 68 65 72 65 0a 20 20 2a 2a 20  , so there.  ** 
32cad 73 68 6f 75 6c 64 20 62 65 20 72 6f 6f 6d 2e 0a  should be room..
32cae 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
32caf 44 6f 63 6c 69 73 74 44 61 74 61 3e 3d 6e 2b 6e  DoclistData>=n+n
32cb0 54 65 72 6d 20 29 3b 0a 0a 20 20 69 44 61 74 61  Term );..  iData
32cb1 20 3d 20 69 44 6f 63 6c 69 73 74 44 61 74 61 2d   = iDoclistData-
32cb2 28 6e 2b 6e 54 65 72 6d 29 3b 0a 20 20 6d 65 6d  (n+nTerm);.  mem
32cb3 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74  cpy(pWriter->dat
32cb4 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 63  a.pData+iData, c
32cb5 2c 20 6e 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  , n);.  memcpy(p
32cb6 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
32cb7 74 61 2b 69 44 61 74 61 2b 6e 2c 20 70 54 65 72  ta+iData+n, pTer
32cb8 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72 65  m, nTerm);..  re
32cb9 74 75 72 6e 20 6c 65 61 66 57 72 69 74 65 72 49  turn leafWriterI
32cba 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20  nternalFlush(v, 
32cbb 70 57 72 69 74 65 72 2c 20 69 44 61 74 61 2c 20  pWriter, iData, 
32cbc 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
32cbd 61 74 61 2d 69 44 61 74 61 29 3b 0a 7d 0a 0a 2f  ata-iData);.}../
32cbe 2a 20 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65  * Push pTerm[nTe
32cbf 72 6d 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74  rm] along with t
32cc0 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  he doclist data 
32cc1 74 6f 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65  to the leaf laye
32cc2 72 20 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e  r of.** %_segmen
32cc3 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ts..*/.static in
32cc4 74 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  t leafWriterStep
32cc5 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76  Merge(fulltext_v
32cc6 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
32cc7 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
32cc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cc9 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
32cca 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
32ccb 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20  nt nTerm,.      
32ccc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ccd 20 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65           DLReade
32cce 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
32ccf 20 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 63 68   nReaders){.  ch
32cd0 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
32cd1 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
32cd2 6e 74 20 69 54 65 72 6d 44 61 74 61 20 3d 20 70  nt iTermData = p
32cd3 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
32cd4 74 61 2c 20 69 44 6f 63 6c 69 73 74 44 61 74 61  ta, iDoclistData
32cd5 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 44 61 74 61  ;.  int i, nData
32cd6 2c 20 6e 2c 20 6e 41 63 74 75 61 6c 44 61 74 61  , n, nActualData
32cd7 2c 20 6e 41 63 74 75 61 6c 2c 20 72 63 2c 20 6e  , nActual, rc, n
32cd8 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20  TermDistinct;.. 
32cd9 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45   ASSERT_VALID_LE
32cda 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d  AF_NODE(pWriter-
32cdb 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 70 57 72  >data.pData, pWr
32cdc 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
32cdd 29 3b 0a 20 20 6e 54 65 72 6d 44 69 73 74 69 6e  );.  nTermDistin
32cde 63 74 20 3d 20 6c 65 61 66 57 72 69 74 65 72 45  ct = leafWriterE
32cdf 6e 63 6f 64 65 54 65 72 6d 28 70 57 72 69 74 65  ncodeTerm(pWrite
32ce0 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29  r, pTerm, nTerm)
32ce1 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72  ;..  /* Remember
32ce2 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 69   nTermDistinct i
32ce3 66 20 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20  f opening a new 
32ce4 6e 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 69  node. */.  if( i
32ce5 54 65 72 6d 44 61 74 61 3d 3d 30 20 29 20 70 57  TermData==0 ) pW
32ce6 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
32ce7 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
32ce8 69 6e 63 74 3b 0a 0a 20 20 69 44 6f 63 6c 69 73  inct;..  iDoclis
32ce9 74 44 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d  tData = pWriter-
32cea 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 0a 20 20  >data.nData;..  
32ceb 2f 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20  /* Estimate the 
32cec 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6d 65  length of the me
32ced 72 67 65 64 20 64 6f 63 6c 69 73 74 20 73 6f 20  rged doclist so 
32cee 77 65 20 63 61 6e 20 6c 65 61 76 65 20 73 70 61  we can leave spa
32cef 63 65 0a 20 20 2a 2a 20 74 6f 20 65 6e 63 6f 64  ce.  ** to encod
32cf0 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  e it..  */.  for
32cf1 28 69 3d 30 2c 20 6e 44 61 74 61 3d 30 3b 20 69  (i=0, nData=0; i
32cf2 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b  <nReaders; i++){
32cf3 0a 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 64 6c  .    nData += dl
32cf4 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70  rAllDataBytes(&p
32cf5 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d  Readers[i]);.  }
32cf6 0a 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61  .  n = fts3PutVa
32cf7 72 69 6e 74 28 63 2c 20 6e 44 61 74 61 29 3b 0a  rint(c, nData);.
32cf8 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
32cf9 6e 64 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  nd(&pWriter->dat
32cfa 61 2c 20 63 2c 20 6e 29 3b 0a 0a 20 20 64 6f 63  a, c, n);..  doc
32cfb 4c 69 73 74 4d 65 72 67 65 28 26 70 57 72 69 74  ListMerge(&pWrit
32cfc 65 72 2d 3e 64 61 74 61 2c 20 70 52 65 61 64 65  er->data, pReade
32cfd 72 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20  rs, nReaders);. 
32cfe 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f   ASSERT_VALID_DO
32cff 43 4c 49 53 54 28 44 4c 5f 44 45 46 41 55 4c 54  CLIST(DL_DEFAULT
32d00 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32d01 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
32d02 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f  ->data.pData+iDo
32d03 63 6c 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20  clistData+n,.   
32d04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d05 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
32d06 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74  a.nData-iDoclist
32d07 44 61 74 61 2d 6e 2c 20 4e 55 4c 4c 29 3b 0a 0a  Data-n, NULL);..
32d08 20 20 2f 2a 20 54 68 65 20 61 63 74 75 61 6c 20    /* The actual 
32d09 61 6d 6f 75 6e 74 20 6f 66 20 64 6f 63 6c 69 73  amount of doclis
32d0a 74 20 64 61 74 61 20 61 74 20 74 68 69 73 20 70  t data at this p
32d0b 6f 69 6e 74 20 63 6f 75 6c 64 20 62 65 20 73 6d  oint could be sm
32d0c 61 6c 6c 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20  aller.  ** than 
32d0d 74 68 65 20 6c 65 6e 67 74 68 20 77 65 20 65 6e  the length we en
32d0e 63 6f 64 65 64 2e 20 20 41 64 64 69 74 69 6f 6e  coded.  Addition
32d0f 61 6c 6c 79 2c 20 74 68 65 20 73 70 61 63 65 20  ally, the space 
32d10 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a  required to.  **
32d11 20 65 6e 63 6f 64 65 20 74 68 69 73 20 6c 65 6e   encode this len
32d12 67 74 68 20 63 6f 75 6c 64 20 62 65 20 73 6d 61  gth could be sma
32d13 6c 6c 65 72 2e 20 20 46 6f 72 20 73 6d 61 6c 6c  ller.  For small
32d14 20 64 6f 63 6c 69 73 74 73 2c 20 74 68 69 73 20   doclists, this 
32d15 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 61 20 62 69  is.  ** not a bi
32d16 67 20 64 65 61 6c 2c 20 77 65 20 63 61 6e 20 6a  g deal, we can j
32d17 75 73 74 20 75 73 65 20 6d 65 6d 6d 6f 76 65 28  ust use memmove(
32d18 29 20 74 6f 20 61 64 6a 75 73 74 20 74 68 69 6e  ) to adjust thin
32d19 67 73 2e 0a 20 20 2a 2f 0a 20 20 6e 41 63 74 75  gs..  */.  nActu
32d1a 61 6c 44 61 74 61 20 3d 20 70 57 72 69 74 65 72  alData = pWriter
32d1b 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44  ->data.nData-(iD
32d1c 6f 63 6c 69 73 74 44 61 74 61 2b 6e 29 3b 0a 20  oclistData+n);. 
32d1d 20 6e 41 63 74 75 61 6c 20 3d 20 66 74 73 33 50   nActual = fts3P
32d1e 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 41 63 74  utVarint(c, nAct
32d1f 75 61 6c 44 61 74 61 29 3b 0a 20 20 61 73 73 65  ualData);.  asse
32d20 72 74 28 20 6e 41 63 74 75 61 6c 44 61 74 61 3c  rt( nActualData<
32d21 3d 6e 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65  =nData );.  asse
32d22 72 74 28 20 6e 41 63 74 75 61 6c 3c 3d 6e 20 29  rt( nActual<=n )
32d23 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e  ;..  /* If the n
32d24 65 77 20 64 6f 63 6c 69 73 74 20 69 73 20 62 69  ew doclist is bi
32d25 67 20 65 6e 6f 75 67 68 20 66 6f 72 20 66 6f 72  g enough for for
32d26 63 65 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20  ce a standalone 
32d27 6c 65 61 66 0a 20 20 2a 2a 20 6e 6f 64 65 2c 20  leaf.  ** node, 
32d28 77 65 20 63 61 6e 20 69 6d 6d 65 64 69 61 74 65  we can immediate
32d29 6c 79 20 66 6c 75 73 68 20 69 74 20 69 6e 6c 69  ly flush it inli
32d2a 6e 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  ne without doing
32d2b 20 74 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76   the.  ** memmov
32d2c 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54  e()..  */.  /* T
32d2d 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
32d2e 74 65 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61  test matches lea
32d2f 66 57 72 69 74 65 72 53 74 65 70 28 29 2c 20 77  fWriterStep(), w
32d30 68 69 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20  hich does this. 
32d31 20 2a 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20   ** test before 
32d32 69 74 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73  it knows the cos
32d33 74 20 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f  t to varint-enco
32d34 64 65 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a  de the term and.
32d35 20 20 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e    ** doclist len
32d36 67 74 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70  gths.  At some p
32d37 6f 69 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a  oint, change to.
32d38 20 20 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61    ** pWriter->da
32d39 74 61 2e 6e 44 61 74 61 2d 69 54 65 72 6d 44 61  ta.nData-iTermDa
32d3a 74 61 3e 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49  ta>STANDALONE_MI
32d3b 4e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 54  N..  */.  if( nT
32d3c 65 72 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e  erm+nActualData>
32d3d 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 29  STANDALONE_MIN )
32d3e 7b 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 6c 65  {.    /* Push le
32d3f 61 66 20 6e 6f 64 65 20 66 72 6f 6d 20 62 65 66  af node from bef
32d40 6f 72 65 20 74 68 69 73 20 74 65 72 6d 2e 20 2a  ore this term. *
32d41 2f 0a 20 20 20 20 69 66 28 20 69 54 65 72 6d 44  /.    if( iTermD
32d42 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 72  ata>0 ){.      r
32d43 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e  c = leafWriterIn
32d44 74 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70  ternalFlush(v, p
32d45 57 72 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d  Writer, 0, iTerm
32d46 44 61 74 61 29 3b 0a 20 20 20 20 20 20 69 66 28  Data);.      if(
32d47 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32d48 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
32d49 20 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72     pWriter->nTer
32d4a 6d 44 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72  mDistinct = nTer
32d4b 6d 44 69 73 74 69 6e 63 74 3b 0a 20 20 20 20 7d  mDistinct;.    }
32d4c 0a 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65  ..    /* Fix the
32d4d 20 65 6e 63 6f 64 65 64 20 64 6f 63 6c 69 73 74   encoded doclist
32d4e 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a 20 20 20 20   length. */.    
32d4f 69 44 6f 63 6c 69 73 74 44 61 74 61 20 2b 3d 20  iDoclistData += 
32d50 6e 20 2d 20 6e 41 63 74 75 61 6c 3b 0a 20 20 20  n - nActual;.   
32d51 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d   memcpy(pWriter-
32d52 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
32d53 6c 69 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63  listData, c, nAc
32d54 74 75 61 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50  tual);..    /* P
32d55 75 73 68 20 74 68 65 20 73 74 61 6e 64 61 6c 6f  ush the standalo
32d56 6e 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f  ne leaf node. */
32d57 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72  .    rc = leafWr
32d58 69 74 65 72 49 6e 6c 69 6e 65 46 6c 75 73 68 28  iterInlineFlush(
32d59 76 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72  v, pWriter, pTer
32d5a 6d 2c 20 6e 54 65 72 6d 2c 20 69 44 6f 63 6c 69  m, nTerm, iDocli
32d5b 73 74 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28  stData);.    if(
32d5c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32d5d 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
32d5e 20 2f 2a 20 4c 65 61 76 65 20 74 68 65 20 6e 6f   /* Leave the no
32d5f 64 65 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20  de empty. */.   
32d60 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
32d61 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29  (&pWriter->data)
32d62 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ;..    return rc
32d63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74  ;.  }..  /* At t
32d64 68 69 73 20 70 6f 69 6e 74 2c 20 77 65 20 6b 6e  his point, we kn
32d65 6f 77 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c  ow that the docl
32d66 69 73 74 20 77 61 73 20 73 6d 61 6c 6c 2c 20 73  ist was small, s
32d67 6f 20 64 6f 20 74 68 65 0a 20 20 2a 2a 20 6d 65  o do the.  ** me
32d68 6d 6d 6f 76 65 20 69 66 20 69 6e 64 69 63 61 74  mmove if indicat
32d69 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  ed..  */.  if( n
32d6a 41 63 74 75 61 6c 3c 6e 20 29 7b 0a 20 20 20 20  Actual<n ){.    
32d6b 6d 65 6d 6d 6f 76 65 28 70 57 72 69 74 65 72 2d  memmove(pWriter-
32d6c 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63  >data.pData+iDoc
32d6d 6c 69 73 74 44 61 74 61 2b 6e 41 63 74 75 61 6c  listData+nActual
32d6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ,.            pW
32d6f 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
32d70 61 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e  a+iDoclistData+n
32d71 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 57  ,.            pW
32d72 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
32d73 61 2d 28 69 44 6f 63 6c 69 73 74 44 61 74 61 2b  a-(iDoclistData+
32d74 6e 29 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72  n));.    pWriter
32d75 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20  ->data.nData -= 
32d76 6e 2d 6e 41 63 74 75 61 6c 3b 0a 20 20 7d 0a 0a  n-nActual;.  }..
32d77 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 77 72 69    /* Replace wri
32d78 74 74 65 6e 20 6c 65 6e 67 74 68 20 77 69 74 68  tten length with
32d79 20 61 63 74 75 61 6c 20 6c 65 6e 67 74 68 2e 20   actual length. 
32d7a 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 70 57 72 69  */.  memcpy(pWri
32d7b 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
32d7c 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 20 63 2c  iDoclistData, c,
32d7d 20 6e 41 63 74 75 61 6c 29 3b 0a 0a 20 20 2f 2a   nActual);..  /*
32d7e 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
32d7f 74 6f 6f 20 6c 61 72 67 65 2c 20 62 72 65 61 6b  too large, break
32d80 20 74 68 69 6e 67 73 20 75 70 2e 20 2a 2f 0a 20   things up. */. 
32d81 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
32d82 54 68 69 73 20 74 65 73 74 20 6d 61 74 63 68 65  This test matche
32d83 73 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  s leafWriterStep
32d84 28 29 2c 20 77 68 69 63 68 20 64 6f 65 73 20 74  (), which does t
32d85 68 69 73 0a 20 20 2a 2a 20 74 65 73 74 20 62 65  his.  ** test be
32d86 66 6f 72 65 20 69 74 20 6b 6e 6f 77 73 20 74 68  fore it knows th
32d87 65 20 63 6f 73 74 20 74 6f 20 76 61 72 69 6e 74  e cost to varint
32d88 2d 65 6e 63 6f 64 65 20 74 68 65 20 74 65 72 6d  -encode the term
32d89 20 61 6e 64 0a 20 20 2a 2a 20 64 6f 63 6c 69 73   and.  ** doclis
32d8a 74 20 6c 65 6e 67 74 68 73 2e 20 20 41 74 20 73  t lengths.  At s
32d8b 6f 6d 65 20 70 6f 69 6e 74 2c 20 63 68 61 6e 67  ome point, chang
32d8c 65 20 74 6f 0a 20 20 2a 2a 20 70 57 72 69 74 65  e to.  ** pWrite
32d8d 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 4c 45  r->data.nData>LE
32d8e 41 46 5f 4d 41 58 2e 0a 20 20 2a 2f 0a 20 20 69  AF_MAX..  */.  i
32d8f 66 28 20 69 54 65 72 6d 44 61 74 61 2b 6e 54 65  f( iTermData+nTe
32d90 72 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e 4c  rm+nActualData>L
32d91 45 41 46 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2f  EAF_MAX ){.    /
32d92 2a 20 46 6c 75 73 68 20 6f 75 74 20 74 68 65 20  * Flush out the 
32d93 6c 65 61 64 69 6e 67 20 64 61 74 61 20 61 73 20  leading data as 
32d94 61 20 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 72 63  a node */.    rc
32d95 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74   = leafWriterInt
32d96 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57  ernalFlush(v, pW
32d97 72 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d 44  riter, 0, iTermD
32d98 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ata);.    if( rc
32d99 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
32d9a 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 70 57  turn rc;..    pW
32d9b 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
32d9c 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
32d9d 69 6e 63 74 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  inct;..    /* Re
32d9e 62 75 69 6c 64 20 68 65 61 64 65 72 20 75 73 69  build header usi
32d9f 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ng the current t
32da0 65 72 6d 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66  erm */.    n = f
32da1 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 57 72  ts3PutVarint(pWr
32da2 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
32da3 2c 20 30 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66  , 0);.    n += f
32da4 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 57 72  ts3PutVarint(pWr
32da5 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
32da6 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20  +n, nTerm);.    
32da7 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e  memcpy(pWriter->
32da8 64 61 74 61 2e 70 44 61 74 61 2b 6e 2c 20 70 54  data.pData+n, pT
32da9 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20  erm, nTerm);.   
32daa 20 6e 20 2b 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20   n += nTerm;..  
32dab 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c    /* There shoul
32dac 64 20 61 6c 77 61 79 73 20 62 65 20 72 6f 6f 6d  d always be room
32dad 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 70 72  , because the pr
32dae 65 76 69 6f 75 73 20 65 6e 63 6f 64 69 6e 67 0a  evious encoding.
32daf 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20      ** included 
32db0 61 6c 6c 20 64 61 74 61 20 6e 65 63 65 73 73 61  all data necessa
32db1 72 79 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20  ry to construct 
32db2 74 68 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f  the term..    */
32db3 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 69  .    assert( n<i
32db4 44 6f 63 6c 69 73 74 44 61 74 61 20 29 3b 0a 20  DoclistData );. 
32db5 20 20 20 2f 2a 20 53 6f 20 6c 6f 6e 67 20 61 73     /* So long as
32db6 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20   STANDALONE_MIN 
32db7 69 73 20 68 61 6c 66 20 6f 72 20 6c 65 73 73 20  is half or less 
32db8 6f 66 20 4c 45 41 46 5f 4d 41 58 2c 20 74 68 65  of LEAF_MAX, the
32db9 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  .    ** followin
32dba 67 20 6d 65 6d 63 70 79 28 29 20 69 73 20 73 61  g memcpy() is sa
32dbb 66 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74  fe (as opposed t
32dbc 6f 20 6e 65 65 64 69 6e 67 20 61 20 6d 65 6d 6d  o needing a memm
32dbd 6f 76 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ove)..    */.   
32dbe 20 61 73 73 65 72 74 28 20 32 2a 53 54 41 4e 44   assert( 2*STAND
32dbf 41 4c 4f 4e 45 5f 4d 49 4e 3c 3d 4c 45 41 46 5f  ALONE_MIN<=LEAF_
32dc0 4d 41 58 20 29 3b 0a 20 20 20 20 61 73 73 65 72  MAX );.    asser
32dc1 74 28 20 6e 2b 70 57 72 69 74 65 72 2d 3e 64 61  t( n+pWriter->da
32dc2 74 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73  ta.nData-iDoclis
32dc3 74 44 61 74 61 3c 69 44 6f 63 6c 69 73 74 44 61  tData<iDoclistDa
32dc4 74 61 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ta );.    memcpy
32dc5 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
32dc6 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20  Data+n,.        
32dc7 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61     pWriter->data
32dc8 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
32dc9 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
32dca 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
32dcb 61 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61  ata-iDoclistData
32dcc 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
32dcd 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20 69 44  data.nData -= iD
32dce 6f 63 6c 69 73 74 44 61 74 61 2d 6e 3b 0a 20 20  oclistData-n;.  
32dcf 7d 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44  }.  ASSERT_VALID
32dd0 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74  _LEAF_NODE(pWrit
32dd1 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20  er->data.pData, 
32dd2 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
32dd3 61 74 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ata);..  return 
32dd4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
32dd5 20 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65 72   Push pTerm[nTer
32dd6 6d 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  m] along with th
32dd7 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74  e doclist data t
32dd8 6f 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65 72  o the leaf layer
32dd9 20 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74   of.** %_segment
32dda 73 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  s..*/./* TODO(sh
32ddb 65 73 73 29 20 52 65 76 69 73 65 20 77 72 69 74  ess) Revise writ
32ddc 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 73  eZeroSegment() s
32ddd 6f 20 74 68 61 74 20 64 6f 63 6c 69 73 74 73 20  o that doclists 
32dde 61 72 65 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74  are.** construct
32ddf 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 20 70  ed directly in p
32de0 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 0a 2a 2f  Writer->data..*/
32de1 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
32de2 57 72 69 74 65 72 53 74 65 70 28 66 75 6c 6c 74  WriterStep(fullt
32de3 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
32de4 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
32de5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32de6 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
32de7 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
32de8 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20  nt nTerm,.      
32de9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32dea 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
32deb 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
32dec 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44  ){.  int rc;.  D
32ded 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  LReader reader;.
32dee 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64  .  dlrInit(&read
32def 65 72 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  er, DL_DEFAULT, 
32df0 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20  pData, nData);. 
32df1 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72   rc = leafWriter
32df2 53 74 65 70 4d 65 72 67 65 28 76 2c 20 70 57 72  StepMerge(v, pWr
32df3 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  iter, pTerm, nTe
32df4 72 6d 2c 20 26 72 65 61 64 65 72 2c 20 31 29 3b  rm, &reader, 1);
32df5 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
32df6 65 61 64 65 72 29 3b 0a 0a 20 20 72 65 74 75 72  eader);..  retur
32df7 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a  n rc;.}.../*****
32df8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32df9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2f 0a 2f 2a 20  ***********/./* 
32dfc 4c 65 61 66 52 65 61 64 65 72 20 69 73 20 75 73  LeafReader is us
32dfd 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 6f 76  ed to iterate ov
32dfe 65 72 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c  er an individual
32dff 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 74   leaf node. */.t
32e00 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 65  ypedef struct Le
32e01 61 66 52 65 61 64 65 72 20 7b 0a 20 20 44 61 74  afReader {.  Dat
32e02 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20  aBuffer term;   
32e03 20 20 20 20 20 20 20 2f 2a 20 63 6f 70 79 20 6f         /* copy o
32e04 66 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20  f current term. 
32e05 2a 2f 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  */..  const char
32e06 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20   *pData;        
32e07 2f 2a 20 64 61 74 61 20 66 6f 72 20 63 75 72 72  /* data for curr
32e08 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 69  ent term. */.  i
32e09 6e 74 20 6e 44 61 74 61 3b 0a 7d 20 4c 65 61 66  nt nData;.} Leaf
32e0a 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20  Reader;..static 
32e0b 76 6f 69 64 20 6c 65 61 66 52 65 61 64 65 72 44  void leafReaderD
32e0c 65 73 74 72 6f 79 28 4c 65 61 66 52 65 61 64 65  estroy(LeafReade
32e0d 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 64  r *pReader){.  d
32e0e 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
32e0f 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 29  (&pReader->term)
32e10 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  ;.  SCRAMBLE(pRe
32e11 61 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  ader);.}..static
32e12 20 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 41   int leafReaderA
32e13 74 45 6e 64 28 4c 65 61 66 52 65 61 64 65 72 20  tEnd(LeafReader 
32e14 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
32e15 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  urn pReader->nDa
32e16 74 61 3c 3d 30 3b 0a 7d 0a 0a 2f 2a 20 41 63 63  ta<=0;.}../* Acc
32e17 65 73 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  ess the current 
32e18 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20  term. */.static 
32e19 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 54 65  int leafReaderTe
32e1a 72 6d 42 79 74 65 73 28 4c 65 61 66 52 65 61 64  rmBytes(LeafRead
32e1b 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
32e1c 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
32e1d 74 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74  term.nData;.}.st
32e1e 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
32e1f 2a 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d 28  *leafReaderTerm(
32e20 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
32e21 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
32e22 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
32e23 61 74 61 3e 30 20 29 3b 0a 20 20 72 65 74 75 72  ata>0 );.  retur
32e24 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
32e25 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 41 63 63  pData;.}../* Acc
32e26 65 73 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20  ess the doclist 
32e27 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  data for the cur
32e28 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74  rent term. */.st
32e29 61 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61  atic int leafRea
32e2a 64 65 72 44 61 74 61 42 79 74 65 73 28 4c 65 61  derDataBytes(Lea
32e2b 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
32e2c 29 7b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  ){.  int nData;.
32e2d 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
32e2e 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20  r->term.nData>0 
32e2f 29 3b 0a 20 20 66 74 73 33 47 65 74 56 61 72 69  );.  fts3GetVari
32e30 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
32e31 61 74 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20  ata, &nData);.  
32e32 72 65 74 75 72 6e 20 6e 44 61 74 61 3b 0a 7d 0a  return nData;.}.
32e33 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
32e34 72 20 2a 6c 65 61 66 52 65 61 64 65 72 44 61 74  r *leafReaderDat
32e35 61 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  a(LeafReader *pR
32e36 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c  eader){.  int n,
32e37 20 6e 44 61 74 61 3b 0a 20 20 61 73 73 65 72 74   nData;.  assert
32e38 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  ( pReader->term.
32e39 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 6e 20 3d  nData>0 );.  n =
32e3a 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
32e3b 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
32e3c 20 26 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75   &nData);.  retu
32e3d 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  rn pReader->pDat
32e3e 61 2b 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  a+n;.}..static v
32e3f 6f 69 64 20 6c 65 61 66 52 65 61 64 65 72 49 6e  oid leafReaderIn
32e40 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  it(const char *p
32e41 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
32e42 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32e43 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
32e44 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
32e45 7b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 6e  {.  int nTerm, n
32e46 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  ;..  assert( nDa
32e47 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>0 );.  assert
32e48 28 20 70 44 61 74 61 5b 30 5d 3d 3d 27 5c 30 27  ( pData[0]=='\0'
32e49 20 29 3b 0a 0a 20 20 43 4c 45 41 52 28 70 52 65   );..  CLEAR(pRe
32e4a 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  ader);..  /* Rea
32e4b 64 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  d the first term
32e4c 2c 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20 68  , skipping the h
32e4d 65 61 64 65 72 20 62 79 74 65 2e 20 2a 2f 0a 20  eader byte. */. 
32e4e 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
32e4f 6e 74 33 32 28 70 44 61 74 61 2b 31 2c 20 26 6e  nt32(pData+1, &n
32e50 54 65 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66  Term);.  dataBuf
32e51 66 65 72 49 6e 69 74 28 26 70 52 65 61 64 65 72  ferInit(&pReader
32e52 2d 3e 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a  ->term, nTerm);.
32e53 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c    dataBufferRepl
32e54 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 74 65  ace(&pReader->te
32e55 72 6d 2c 20 70 44 61 74 61 2b 31 2b 6e 2c 20 6e  rm, pData+1+n, n
32e56 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20 50 6f 73  Term);..  /* Pos
32e57 69 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20  ition after the 
32e58 66 69 72 73 74 20 74 65 72 6d 2e 20 2a 2f 0a 20  first term. */. 
32e59 20 61 73 73 65 72 74 28 20 31 2b 6e 2b 6e 54 65   assert( 1+n+nTe
32e5a 72 6d 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 52  rm<nData );.  pR
32e5b 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70  eader->pData = p
32e5c 44 61 74 61 2b 31 2b 6e 2b 6e 54 65 72 6d 3b 0a  Data+1+n+nTerm;.
32e5d 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
32e5e 20 3d 20 6e 44 61 74 61 2d 31 2d 6e 2d 6e 54 65   = nData-1-n-nTe
32e5f 72 6d 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 74  rm;.}../* Step t
32e60 68 65 20 72 65 61 64 65 72 20 66 6f 72 77 61 72  he reader forwar
32e61 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65  d to the next te
32e62 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rm. */.static vo
32e63 69 64 20 6c 65 61 66 52 65 61 64 65 72 53 74 65  id leafReaderSte
32e64 70 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  p(LeafReader *pR
32e65 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c  eader){.  int n,
32e66 20 6e 44 61 74 61 2c 20 6e 50 72 65 66 69 78 2c   nData, nPrefix,
32e67 20 6e 53 75 66 66 69 78 3b 0a 20 20 61 73 73 65   nSuffix;.  asse
32e68 72 74 28 20 21 6c 65 61 66 52 65 61 64 65 72 41  rt( !leafReaderA
32e69 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
32e6a 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 72 65 76  ..  /* Skip prev
32e6b 69 6f 75 73 20 65 6e 74 72 79 27 73 20 64 61 74  ious entry's dat
32e6c 61 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6e 20  a block. */.  n 
32e6d 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
32e6e 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
32e6f 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20 61 73 73  , &nData);.  ass
32e70 65 72 74 28 20 6e 2b 6e 44 61 74 61 3c 3d 70 52  ert( n+nData<=pR
32e71 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
32e72 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
32e73 20 2b 3d 20 6e 2b 6e 44 61 74 61 3b 0a 20 20 70   += n+nData;.  p
32e74 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d  Reader->nData -=
32e75 20 6e 2b 6e 44 61 74 61 3b 0a 0a 20 20 69 66 28   n+nData;..  if(
32e76 20 21 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e   !leafReaderAtEn
32e77 64 28 70 52 65 61 64 65 72 29 20 29 7b 0a 20 20  d(pReader) ){.  
32e78 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74    /* Construct t
32e79 68 65 20 6e 65 77 20 74 65 72 6d 20 75 73 69 6e  he new term usin
32e7a 67 20 61 20 70 72 65 66 69 78 20 66 72 6f 6d 20  g a prefix from 
32e7b 74 68 65 20 6f 6c 64 20 74 65 72 6d 20 70 6c 75  the old term plu
32e7c 73 20 61 0a 20 20 20 20 2a 2a 20 73 75 66 66 69  s a.    ** suffi
32e7d 78 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20  x from the leaf 
32e7e 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  data..    */.   
32e7f 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
32e80 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
32e81 61 74 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a  ata, &nPrefix);.
32e82 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
32e83 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
32e84 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66  ->pData+n, &nSuf
32e85 66 69 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74  fix);.    assert
32e86 28 20 6e 2b 6e 53 75 66 66 69 78 3c 70 52 65 61  ( n+nSuffix<pRea
32e87 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
32e88 20 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e    pReader->term.
32e89 6e 44 61 74 61 20 3d 20 6e 50 72 65 66 69 78 3b  nData = nPrefix;
32e8a 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
32e8b 70 70 65 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  ppend(&pReader->
32e8c 74 65 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70  term, pReader->p
32e8d 44 61 74 61 2b 6e 2c 20 6e 53 75 66 66 69 78 29  Data+n, nSuffix)
32e8e 3b 0a 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  ;..    pReader->
32e8f 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66  pData += n+nSuff
32e90 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d  ix;.    pReader-
32e91 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66  >nData -= n+nSuf
32e92 66 69 78 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 73  fix;.  }.}../* s
32e93 74 72 63 6d 70 2d 73 74 79 6c 65 20 63 6f 6d 70  trcmp-style comp
32e94 61 72 69 73 6f 6e 20 6f 66 20 70 52 65 61 64 65  arison of pReade
32e95 72 27 73 20 63 75 72 72 65 6e 74 20 74 65 72 6d  r's current term
32e96 20 61 67 61 69 6e 73 74 20 70 54 65 72 6d 2e 0a   against pTerm..
32e97 2a 2a 20 49 66 20 69 73 50 72 65 66 69 78 2c 20  ** If isPrefix, 
32e98 65 71 75 61 6c 69 74 79 20 6d 65 61 6e 73 20 65  equality means e
32e99 71 75 61 6c 20 74 68 72 6f 75 67 68 20 6e 54 65  qual through nTe
32e9a 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  rm bytes..*/.sta
32e9b 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64  tic int leafRead
32e9c 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 66 52 65  erTermCmp(LeafRe
32e9d 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20  ader *pReader,. 
32e9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e9f 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
32ea0 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
32ea1 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
32ea2 50 72 65 66 69 78 29 7b 0a 20 20 69 6e 74 20 63  Prefix){.  int c
32ea3 2c 20 6e 20 3d 20 70 52 65 61 64 65 72 2d 3e 74  , n = pReader->t
32ea4 65 72 6d 2e 6e 44 61 74 61 3c 6e 54 65 72 6d 20  erm.nData<nTerm 
32ea5 3f 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  ? pReader->term.
32ea6 6e 44 61 74 61 20 3a 20 6e 54 65 72 6d 3b 0a 20  nData : nTerm;. 
32ea7 20 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20   if( n==0 ){.   
32ea8 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 74 65   if( pReader->te
32ea9 72 6d 2e 6e 44 61 74 61 3e 30 20 29 20 72 65 74  rm.nData>0 ) ret
32eaa 75 72 6e 20 2d 31 3b 0a 20 20 20 20 69 66 28 6e  urn -1;.    if(n
32eab 54 65 72 6d 3e 30 20 29 20 72 65 74 75 72 6e 20  Term>0 ) return 
32eac 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  1;.    return 0;
32ead 0a 20 20 7d 0a 0a 20 20 63 20 3d 20 6d 65 6d 63  .  }..  c = memc
32eae 6d 70 28 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  mp(pReader->term
32eaf 2e 70 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e  .pData, pTerm, n
32eb0 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20  );.  if( c!=0 ) 
32eb1 72 65 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20  return c;.  if( 
32eb2 69 73 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e  isPrefix && n==n
32eb3 54 65 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  Term ) return 0;
32eb4 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
32eb5 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 2d 20  r->term.nData - 
32eb6 6e 54 65 72 6d 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a  nTerm;.}.../****
32eb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32eb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32eb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32eba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
32ebb 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 77 72   LeavesReader wr
32ebc 61 70 73 20 4c 65 61 66 52 65 61 64 65 72 20 74  aps LeafReader t
32ebd 6f 20 61 6c 6c 6f 77 20 69 74 65 72 61 74 69 6e  o allow iteratin
32ebe 67 20 6f 76 65 72 20 74 68 65 20 65 6e 74 69 72  g over the entir
32ebf 65 0a 2a 2a 20 6c 65 61 66 20 6c 61 79 65 72 20  e.** leaf layer 
32ec0 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a  of the tree..*/.
32ec1 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c  typedef struct L
32ec2 65 61 76 65 73 52 65 61 64 65 72 20 7b 0a 20 20  eavesReader {.  
32ec3 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20  int idx;        
32ec4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
32ec5 65 78 20 77 69 74 68 69 6e 20 74 68 65 20 73 65  ex within the se
32ec6 67 6d 65 6e 74 2e 20 2a 2f 0a 0a 20 20 73 71 6c  gment. */..  sql
32ec7 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
32ec8 3b 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d  ;      /* Statem
32ec9 65 6e 74 20 77 65 27 72 65 20 73 74 72 65 61 6d  ent we're stream
32eca 69 6e 67 20 6c 65 61 76 65 73 20 66 72 6f 6d 2e  ing leaves from.
32ecb 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20   */.  int eof;  
32ecc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ecd 2f 2a 20 77 65 27 76 65 20 73 65 65 6e 20 53 51  /* we've seen SQ
32ece 4c 49 54 45 5f 44 4f 4e 45 20 66 72 6f 6d 20 70  LITE_DONE from p
32ecf 53 74 6d 74 2e 20 2a 2f 0a 0a 20 20 4c 65 61 66  Stmt. */..  Leaf
32ed0 52 65 61 64 65 72 20 6c 65 61 66 52 65 61 64 65  Reader leafReade
32ed1 72 3b 20 20 20 20 2f 2a 20 72 65 61 64 65 72 20  r;    /* reader 
32ed2 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
32ed3 6c 65 61 66 2e 20 2a 2f 0a 20 20 44 61 74 61 42  leaf. */.  DataB
32ed4 75 66 66 65 72 20 72 6f 6f 74 44 61 74 61 3b 20  uffer rootData; 
32ed5 20 20 20 20 20 2f 2a 20 72 6f 6f 74 20 64 61 74       /* root dat
32ed6 61 20 66 6f 72 20 69 6e 6c 69 6e 65 2e 20 2a 2f  a for inline. */
32ed7 0a 7d 20 4c 65 61 76 65 73 52 65 61 64 65 72 3b  .} LeavesReader;
32ed8 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20  ../* Access the 
32ed9 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f  current term. */
32eda 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
32edb 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  esReaderTermByte
32edc 73 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  s(LeavesReader *
32edd 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
32ede 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f  rt( !pReader->eo
32edf 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65  f );.  return le
32ee0 61 66 52 65 61 64 65 72 54 65 72 6d 42 79 74 65  afReaderTermByte
32ee1 73 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  s(&pReader->leaf
32ee2 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69  Reader);.}.stati
32ee3 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65  c const char *le
32ee4 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 4c  avesReaderTerm(L
32ee5 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
32ee6 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
32ee7 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29   !pReader->eof )
32ee8 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52  ;.  return leafR
32ee9 65 61 64 65 72 54 65 72 6d 28 26 70 52 65 61 64  eaderTerm(&pRead
32eea 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b  er->leafReader);
32eeb 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68  .}../* Access th
32eec 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66  e doclist data f
32eed 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
32eee 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  erm. */.static i
32eef 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 44  nt leavesReaderD
32ef0 61 74 61 42 79 74 65 73 28 4c 65 61 76 65 73 52  ataBytes(LeavesR
32ef1 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
32ef2 0a 20 20 61 73 73 65 72 74 28 20 21 70 52 65 61  .  assert( !pRea
32ef3 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65  der->eof );.  re
32ef4 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 44  turn leafReaderD
32ef5 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65  ataBytes(&pReade
32ef6 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
32ef7 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  }.static const c
32ef8 68 61 72 20 2a 6c 65 61 76 65 73 52 65 61 64 65  har *leavesReade
32ef9 72 44 61 74 61 28 4c 65 61 76 65 73 52 65 61 64  rData(LeavesRead
32efa 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
32efb 61 73 73 65 72 74 28 20 21 70 52 65 61 64 65 72  assert( !pReader
32efc 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72  ->eof );.  retur
32efd 6e 20 6c 65 61 66 52 65 61 64 65 72 44 61 74 61  n leafReaderData
32efe 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
32eff 65 61 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69  eader);.}..stati
32f00 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
32f01 65 72 41 74 45 6e 64 28 4c 65 61 76 65 73 52 65  erAtEnd(LeavesRe
32f02 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
32f03 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
32f04 2d 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 6c 6f 61  ->eof;.}../* loa
32f05 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 29  dSegmentLeaves()
32f06 20 6d 61 79 20 6e 6f 74 20 72 65 61 64 20 61 6c   may not read al
32f07 6c 20 74 68 65 20 77 61 79 20 74 6f 20 53 51 4c  l the way to SQL
32f08 49 54 45 5f 44 4f 4e 45 2c 20 74 68 75 73 0a 2a  ITE_DONE, thus.*
32f09 2a 20 6c 65 61 76 69 6e 67 20 74 68 65 20 73 74  * leaving the st
32f0a 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 6f  atement handle o
32f0b 70 65 6e 2c 20 77 68 69 63 68 20 6c 6f 63 6b 73  pen, which locks
32f0c 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f   the table..*/./
32f0d 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
32f0e 69 73 20 22 73 6f 6c 75 74 69 6f 6e 22 20 69 73  is "solution" is
32f0f 20 6e 6f 74 20 73 61 74 69 73 66 61 63 74 6f 72   not satisfactor
32f10 79 2e 20 20 52 65 61 6c 6c 79 2c 20 74 68 65 72  y.  Really, ther
32f11 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63  e.** should be c
32f12 68 65 63 6b 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  heck-in function
32f13 20 66 6f 72 20 61 6c 6c 20 73 74 61 74 65 6d 65   for all stateme
32f14 6e 74 20 68 61 6e 64 6c 65 73 20 77 68 69 63 68  nt handles which
32f15 0a 2a 2a 20 61 72 72 61 6e 67 65 73 20 74 6f 20  .** arranges to 
32f16 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73  call sqlite3_res
32f17 65 74 28 29 2e 20 20 54 68 69 73 20 6d 6f 73 74  et().  This most
32f18 20 6c 69 6b 65 6c 79 20 77 69 6c 6c 20 72 65 71   likely will req
32f19 75 69 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 63 61  uire.** modifica
32f1a 74 69 6f 6e 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  tion to control 
32f1b 66 6c 6f 77 20 61 6c 6c 20 6f 76 65 72 20 74 68  flow all over th
32f1c 65 20 70 6c 61 63 65 2c 20 74 68 6f 75 67 68 2c  e place, though,
32f1d 20 73 6f 20 66 6f 72 20 6e 6f 77 0a 2a 2a 20 6a   so for now.** j
32f1e 75 73 74 20 70 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ust punt..**.** 
32f1f 4e 6f 74 65 20 74 68 65 20 74 68 65 20 63 75 72  Note the the cur
32f20 72 65 6e 74 20 73 79 73 74 65 6d 20 61 73 73 75  rent system assu
32f21 6d 65 73 20 74 68 61 74 20 73 65 67 6d 65 6e 74  mes that segment
32f22 20 6d 65 72 67 65 73 20 77 69 6c 6c 20 72 75 6e   merges will run
32f23 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f   to.** completio
32f24 6e 2c 20 77 68 69 63 68 20 69 73 20 77 68 79 20  n, which is why 
32f25 74 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20  this particular 
32f26 70 72 6f 62 61 62 6c 79 20 68 61 73 6e 27 74 20  probably hasn't 
32f27 61 72 69 73 65 6e 20 69 6e 0a 2a 2a 20 74 68 69  arisen in.** thi
32f28 73 20 63 61 73 65 2e 20 20 50 72 6f 62 61 62 6c  s case.  Probabl
32f29 79 20 61 20 62 72 69 74 74 6c 65 20 61 73 73 75  y a brittle assu
32f2a 6d 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  mption..*/.stati
32f2b 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
32f2c 65 72 52 65 73 65 74 28 4c 65 61 76 65 73 52 65  erReset(LeavesRe
32f2d 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
32f2e 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
32f2f 5f 72 65 73 65 74 28 70 52 65 61 64 65 72 2d 3e  _reset(pReader->
32f30 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  pStmt);.}..stati
32f31 63 20 76 6f 69 64 20 6c 65 61 76 65 73 52 65 61  c void leavesRea
32f32 64 65 72 44 65 73 74 72 6f 79 28 4c 65 61 76 65  derDestroy(Leave
32f33 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
32f34 29 7b 0a 20 20 2f 2a 20 49 66 20 69 64 78 20 69  ){.  /* If idx i
32f35 73 20 2d 31 2c 20 74 68 61 74 20 6d 65 61 6e 73  s -1, that means
32f36 20 77 65 27 72 65 20 75 73 69 6e 67 20 61 20 6e   we're using a n
32f37 6f 6e 2d 63 61 63 68 65 64 20 73 74 61 74 65 6d  on-cached statem
32f38 65 6e 74 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20  ent.  ** handle 
32f39 69 6e 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 28  in the optimize(
32f3a 29 20 63 61 73 65 2c 20 73 6f 20 77 65 20 6e 65  ) case, so we ne
32f3b 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74  ed to release it
32f3c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65  ..  */.  if( pRe
32f3d 61 64 65 72 2d 3e 70 53 74 6d 74 21 3d 4e 55 4c  ader->pStmt!=NUL
32f3e 4c 20 26 26 20 70 52 65 61 64 65 72 2d 3e 69 64  L && pReader->id
32f3f 78 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c  x==-1 ){.    sql
32f40 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
32f41 65 61 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 20  eader->pStmt);. 
32f42 20 7d 0a 20 20 6c 65 61 66 52 65 61 64 65 72 44   }.  leafReaderD
32f43 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d  estroy(&pReader-
32f44 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20  >leafReader);.  
32f45 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
32f46 79 28 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  y(&pReader->root
32f47 44 61 74 61 29 3b 0a 20 20 53 43 52 41 4d 42 4c  Data);.  SCRAMBL
32f48 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f  E(pReader);.}../
32f49 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 52 65  * Initialize pRe
32f4a 61 64 65 72 20 77 69 74 68 20 74 68 65 20 67 69  ader with the gi
32f4b 76 65 6e 20 72 6f 6f 74 20 64 61 74 61 20 28 69  ven root data (i
32f4c 66 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3d  f iStartBlockid=
32f4d 3d 30 0a 2a 2a 20 74 68 65 20 6c 65 61 66 20 64  =0.** the leaf d
32f4e 61 74 61 20 77 61 73 20 65 6e 74 69 72 65 6c 79  ata was entirely
32f4f 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68   contained in th
32f50 65 20 72 6f 6f 74 29 2c 20 6f 72 20 66 72 6f 6d  e root), or from
32f51 20 74 68 65 0a 2a 2a 20 73 74 72 65 61 6d 20 6f   the.** stream o
32f52 66 20 62 6c 6f 63 6b 73 20 62 65 74 77 65 65 6e  f blocks between
32f53 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61   iStartBlockid a
32f54 6e 64 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20  nd iEndBlockid, 
32f55 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2f 0a 73 74  inclusive..*/.st
32f56 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52  atic int leavesR
32f57 65 61 64 65 72 49 6e 69 74 28 66 75 6c 6c 74 65  eaderInit(fullte
32f58 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
32f59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f5a 20 20 20 20 20 20 20 20 69 6e 74 20 69 64 78 2c          int idx,
32f5b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32f5c 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
32f5d 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74  ite_int64 iStart
32f5e 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20  Blockid,.       
32f5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f60 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
32f61 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20  4 iEndBlockid,. 
32f62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f63 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
32f64 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61   char *pRootData
32f65 2c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 2c  , int nRootData,
32f66 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32f67 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61               Lea
32f68 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
32f69 65 72 29 7b 0a 20 20 43 4c 45 41 52 28 70 52 65  er){.  CLEAR(pRe
32f6a 61 64 65 72 29 3b 0a 20 20 70 52 65 61 64 65 72  ader);.  pReader
32f6b 2d 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20 20  ->idx = idx;..  
32f6c 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
32f6d 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74  pReader->rootDat
32f6e 61 2c 20 30 29 3b 0a 20 20 69 66 28 20 69 53 74  a, 0);.  if( iSt
32f6f 61 72 74 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b  artBlockid==0 ){
32f70 0a 20 20 20 20 2f 2a 20 45 6e 74 69 72 65 20 6c  .    /* Entire l
32f71 65 61 66 20 6c 65 76 65 6c 20 66 69 74 20 69 6e  eaf level fit in
32f72 20 72 6f 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20   root data. */. 
32f73 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
32f74 6c 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 72  lace(&pReader->r
32f75 6f 6f 74 44 61 74 61 2c 20 70 52 6f 6f 74 44 61  ootData, pRootDa
32f76 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 29 3b 0a  ta, nRootData);.
32f77 20 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e      leafReaderIn
32f78 69 74 28 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  it(pReader->root
32f79 44 61 74 61 2e 70 44 61 74 61 2c 20 70 52 65 61  Data.pData, pRea
32f7a 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 6e 44  der->rootData.nD
32f7b 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
32f7c 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72          &pReader
32f7d 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
32f7e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
32f7f 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 20  te3_stmt *s;.   
32f80 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
32f81 74 5f 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e 74  t_leaf_statement
32f82 28 76 2c 20 69 64 78 2c 20 26 73 29 3b 0a 20 20  (v, idx, &s);.  
32f83 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32f84 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32f85 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
32f86 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
32f87 20 31 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69   1, iStartBlocki
32f88 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  d);.    if( rc!=
32f89 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
32f8a 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d  rn rc;..    rc =
32f8b 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
32f8c 74 36 34 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c  t64(s, 2, iEndBl
32f8d 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20  ockid);.    if( 
32f8e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
32f8f 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
32f90 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
32f91 70 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  p(s);.    if( rc
32f92 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
32f93 0a 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e  .      pReader->
32f94 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  eof = 1;.      r
32f95 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
32f96 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
32f97 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
32f98 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
32f99 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 20 3d  pReader->pStmt =
32f9a 20 73 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64   s;.    leafRead
32f9b 65 72 49 6e 69 74 28 73 71 6c 69 74 65 33 5f 63  erInit(sqlite3_c
32f9c 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 65 61 64  olumn_blob(pRead
32f9d 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20  er->pStmt, 0),. 
32f9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f9f 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
32fa0 5f 62 79 74 65 73 28 70 52 65 61 64 65 72 2d 3e  _bytes(pReader->
32fa1 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20  pStmt, 0),.     
32fa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
32fa3 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64  Reader->leafRead
32fa4 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  er);.  }.  retur
32fa5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
32fa6 2f 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 72  /* Step the curr
32fa7 65 6e 74 20 6c 65 61 66 20 66 6f 72 77 61 72 64  ent leaf forward
32fa8 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72   to the next ter
32fa9 6d 2e 20 20 49 66 20 77 65 20 72 65 61 63 68 20  m.  If we reach 
32faa 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68  the.** end of th
32fab 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 2c 20  e current leaf, 
32fac 73 74 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20  step forward to 
32fad 74 68 65 20 6e 65 78 74 20 6c 65 61 66 20 62 6c  the next leaf bl
32fae 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ock..*/.static i
32faf 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 53  nt leavesReaderS
32fb0 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  tep(fulltext_vta
32fb1 62 20 2a 76 2c 20 4c 65 61 76 65 73 52 65 61 64  b *v, LeavesRead
32fb2 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
32fb3 61 73 73 65 72 74 28 20 21 6c 65 61 76 65 73 52  assert( !leavesR
32fb4 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61 64  eaderAtEnd(pRead
32fb5 65 72 29 20 29 3b 0a 20 20 6c 65 61 66 52 65 61  er) );.  leafRea
32fb6 64 65 72 53 74 65 70 28 26 70 52 65 61 64 65 72  derStep(&pReader
32fb7 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 0a  ->leafReader);..
32fb8 20 20 69 66 28 20 6c 65 61 66 52 65 61 64 65 72    if( leafReader
32fb9 41 74 45 6e 64 28 26 70 52 65 61 64 65 72 2d 3e  AtEnd(&pReader->
32fba 6c 65 61 66 52 65 61 64 65 72 29 20 29 7b 0a 20  leafReader) ){. 
32fbb 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69     int rc;.    i
32fbc 66 28 20 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74  f( pReader->root
32fbd 44 61 74 61 2e 70 44 61 74 61 20 29 7b 0a 20 20  Data.pData ){.  
32fbe 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66      pReader->eof
32fbf 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
32fc0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
32fc1 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
32fc2 69 74 65 33 5f 73 74 65 70 28 70 52 65 61 64 65  ite3_step(pReade
32fc3 72 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69  r->pStmt);.    i
32fc4 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
32fc5 57 20 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64  W ){.      pRead
32fc6 65 72 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20  er->eof = 1;.   
32fc7 20 20 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51     return rc==SQ
32fc8 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
32fc9 54 45 5f 4f 4b 20 3a 20 72 63 3b 0a 20 20 20 20  TE_OK : rc;.    
32fca 7d 0a 20 20 20 20 6c 65 61 66 52 65 61 64 65 72  }.    leafReader
32fcb 44 65 73 74 72 6f 79 28 26 70 52 65 61 64 65 72  Destroy(&pReader
32fcc 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20  ->leafReader);. 
32fcd 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69     leafReaderIni
32fce 74 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t(sqlite3_column
32fcf 5f 62 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70  _blob(pReader->p
32fd0 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20  Stmt, 0),.      
32fd1 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
32fd2 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
32fd3 73 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  s(pReader->pStmt
32fd4 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0),.          
32fd5 20 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65           &pReade
32fd6 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
32fd7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
32fd8 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4f 72  ITE_OK;.}../* Or
32fd9 64 65 72 20 4c 65 61 76 65 73 52 65 61 64 65 72  der LeavesReader
32fda 73 20 62 79 20 74 68 65 69 72 20 74 65 72 6d 2c  s by their term,
32fdb 20 69 67 6e 6f 72 69 6e 67 20 69 64 78 2e 20 20   ignoring idx.  
32fdc 52 65 61 64 65 72 73 20 61 74 20 65 6f 66 0a 2a  Readers at eof.*
32fdd 2a 20 61 6c 77 61 79 73 20 73 6f 72 74 20 74 6f  * always sort to
32fde 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
32fdf 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
32fe0 61 64 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 76  aderTermCmp(Leav
32fe1 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c  esReader *lr1, L
32fe2 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32  eavesReader *lr2
32fe3 29 7b 0a 20 20 69 66 28 20 6c 65 61 76 65 73 52  ){.  if( leavesR
32fe4 65 61 64 65 72 41 74 45 6e 64 28 6c 72 31 29 20  eaderAtEnd(lr1) 
32fe5 29 7b 0a 20 20 20 20 69 66 28 20 6c 65 61 76 65  ){.    if( leave
32fe6 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c 72 32  sReaderAtEnd(lr2
32fe7 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
32fe8 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
32fe9 20 20 69 66 28 20 6c 65 61 76 65 73 52 65 61 64    if( leavesRead
32fea 65 72 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72  erAtEnd(lr2) ) r
32feb 65 74 75 72 6e 20 2d 31 3b 0a 0a 20 20 72 65 74  eturn -1;..  ret
32fec 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65  urn leafReaderTe
32fed 72 6d 43 6d 70 28 26 6c 72 31 2d 3e 6c 65 61 66  rmCmp(&lr1->leaf
32fee 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20  Reader,.        
32fef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ff0 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 54     leavesReaderT
32ff1 65 72 6d 28 6c 72 32 29 2c 20 6c 65 61 76 65 73  erm(lr2), leaves
32ff2 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
32ff3 6c 72 32 29 2c 0a 20 20 20 20 20 20 20 20 20 20  lr2),.          
32ff4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ff5 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 53 69 6d 69 6c   0);.}../* Simil
32ff6 61 72 20 74 6f 20 6c 65 61 76 65 73 52 65 61 64  ar to leavesRead
32ff7 65 72 54 65 72 6d 43 6d 70 28 29 2c 20 77 69 74  erTermCmp(), wit
32ff8 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 6f 72 64  h additional ord
32ff9 65 72 69 6e 67 20 62 79 20 69 64 78 0a 2a 2a 20  ering by idx.** 
32ffa 73 6f 20 74 68 61 74 20 6f 6c 64 65 72 20 73 65  so that older se
32ffb 67 6d 65 6e 74 73 20 73 6f 72 74 20 62 65 66 6f  gments sort befo
32ffc 72 65 20 6e 65 77 65 72 20 73 65 67 6d 65 6e 74  re newer segment
32ffd 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
32ffe 20 6c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70   leavesReaderCmp
32fff 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c  (LeavesReader *l
33000 72 31 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72  r1, LeavesReader
33001 20 2a 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20   *lr2){.  int c 
33002 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
33003 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b  rmCmp(lr1, lr2);
33004 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65  .  if( c!=0 ) re
33005 74 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e  turn c;.  return
33006 20 6c 72 31 2d 3e 69 64 78 2d 6c 72 32 2d 3e 69   lr1->idx-lr2->i
33007 64 78 3b 0a 7d 0a 0a 2f 2a 20 41 73 73 75 6d 65  dx;.}../* Assume
33008 20 74 68 61 74 20 70 4c 72 5b 31 5d 2e 2e 70 4c   that pLr[1]..pL
33009 72 5b 6e 4c 72 5d 20 61 72 65 20 73 6f 72 74 65  r[nLr] are sorte
3300a 64 2e 20 20 42 75 62 62 6c 65 20 70 4c 72 5b 30  d.  Bubble pLr[0
3300b 5d 20 69 6e 74 6f 20 69 74 73 0a 2a 2a 20 73 6f  ] into its.** so
3300c 72 74 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  rted position..*
3300d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
3300e 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
3300f 72 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  r(LeavesReader *
33010 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20  pLr, int nLr){. 
33011 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26   while( nLr>1 &&
33012 20 6c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70   leavesReaderCmp
33013 28 70 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29  (pLr, pLr+1)>0 )
33014 7b 0a 20 20 20 20 4c 65 61 76 65 73 52 65 61 64  {.    LeavesRead
33015 65 72 20 74 6d 70 20 3d 20 70 4c 72 5b 30 5d 3b  er tmp = pLr[0];
33016 0a 20 20 20 20 70 4c 72 5b 30 5d 20 3d 20 70 4c  .    pLr[0] = pL
33017 72 5b 31 5d 3b 0a 20 20 20 20 70 4c 72 5b 31 5d  r[1];.    pLr[1]
33018 20 3d 20 74 6d 70 3b 0a 20 20 20 20 6e 4c 72 2d   = tmp;.    nLr-
33019 2d 3b 0a 20 20 20 20 70 4c 72 2b 2b 3b 0a 20 20  -;.    pLr++;.  
3301a 7d 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69  }.}../* Initiali
3301b 7a 65 73 20 70 52 65 61 64 65 72 73 20 77 69 74  zes pReaders wit
3301c 68 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66  h the segments f
3301d 72 6f 6d 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c  rom level iLevel
3301e 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 74  , returning.** t
3301f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67  he number of seg
33020 6d 65 6e 74 73 20 69 6e 20 2a 70 69 52 65 61 64  ments in *piRead
33021 65 72 73 2e 20 20 4c 65 61 76 65 73 20 70 52 65  ers.  Leaves pRe
33022 61 64 65 72 73 20 69 6e 20 73 6f 72 74 65 64 0a  aders in sorted.
33023 2a 2a 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61  ** order..*/.sta
33024 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65  tic int leavesRe
33025 61 64 65 72 73 49 6e 69 74 28 66 75 6c 6c 74 65  adersInit(fullte
33026 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
33027 69 4c 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20  iLevel,.        
33028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33029 20 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65       LeavesReade
3302a 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
3302b 20 2a 70 69 52 65 61 64 65 72 73 29 7b 0a 20 20   *piReaders){.  
3302c 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
3302d 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 73  .  int i, rc = s
3302e 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
3302f 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
33030 54 5f 4c 45 56 45 4c 5f 53 54 4d 54 2c 20 26 73  T_LEVEL_STMT, &s
33031 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
33032 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
33033 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
33034 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20  te3_bind_int(s, 
33035 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66  1, iLevel);.  if
33036 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
33037 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
33038 69 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  i = 0;.  while( 
33039 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74  (rc = sqlite3_st
3303a 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52  ep(s))==SQLITE_R
3303b 4f 57 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OW ){.    sqlite
3303c 5f 69 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20  _int64 iStart = 
3303d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
3303e 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20  nt64(s, 0);.    
3303f 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
33040 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
33041 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
33042 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
33043 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69  pRootData = sqli
33044 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
33045 73 2c 20 32 29 3b 0a 20 20 20 20 69 6e 74 20 6e  s, 2);.    int n
33046 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74  RootData = sqlit
33047 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
33048 73 2c 20 32 29 3b 0a 0a 20 20 20 20 61 73 73 65  s, 2);..    asse
33049 72 74 28 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  rt( i<MERGE_COUN
3304a 54 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 6c 65  T );.    rc = le
3304b 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76  avesReaderInit(v
3304c 2c 20 69 2c 20 69 53 74 61 72 74 2c 20 69 45 6e  , i, iStart, iEn
3304d 64 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52  d, pRootData, nR
3304e 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20 20 20 20  ootData,.       
3304f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33050 20 20 20 26 70 52 65 61 64 65 72 73 5b 69 5d 29     &pReaders[i])
33051 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
33052 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b  LITE_OK ) break;
33053 0a 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20  ..    i++;.  }. 
33054 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33055 44 4f 4e 45 20 29 7b 0a 20 20 20 20 77 68 69 6c  DONE ){.    whil
33056 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20  e( i-->0 ){.    
33057 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65    leavesReaderDe
33058 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 73 5b  stroy(&pReaders[
33059 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  i]);.    }.    r
3305a 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
3305b 20 2a 70 69 52 65 61 64 65 72 73 20 3d 20 69 3b   *piReaders = i;
3305c 0a 0a 20 20 2f 2a 20 4c 65 61 76 65 20 6f 75 72  ..  /* Leave our
3305d 20 72 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20   results sorted 
3305e 62 79 20 74 65 72 6d 2c 20 74 68 65 6e 20 61 67  by term, then ag
3305f 65 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69  e. */.  while( i
33060 2d 2d 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 73  -- ){.    leaves
33061 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 70 52  ReaderReorder(pR
33062 65 61 64 65 72 73 2b 69 2c 20 2a 70 69 52 65 61  eaders+i, *piRea
33063 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 20 20 72  ders-i);.  }.  r
33064 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33065 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65 20 64 6f 63  .}../* Merge doc
33066 6c 69 73 74 73 20 66 72 6f 6d 20 70 52 65 61 64  lists from pRead
33067 65 72 73 5b 6e 52 65 61 64 65 72 73 5d 20 69 6e  ers[nReaders] in
33068 74 6f 20 61 20 73 69 6e 67 6c 65 20 64 6f 63 6c  to a single docl
33069 69 73 74 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73  ist, which.** is
3306a 20 77 72 69 74 74 65 6e 20 74 6f 20 70 57 72 69   written to pWri
3306b 74 65 72 2e 20 20 41 73 73 75 6d 65 73 20 70 52  ter.  Assumes pR
3306c 65 61 64 65 72 73 20 69 73 20 6f 72 64 65 72 65  eaders is ordere
3306d 64 20 6f 6c 64 65 73 74 20 74 6f 0a 2a 2a 20 6e  d oldest to.** n
3306e 65 77 65 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  ewest..*/./* TOD
3306f 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65  O(shess) Conside
33070 72 20 70 75 74 74 69 6e 67 20 74 68 69 73 20 69  r putting this i
33071 6e 6c 69 6e 65 20 69 6e 20 73 65 67 6d 65 6e 74  nline in segment
33072 4d 65 72 67 65 28 29 2e 20 2a 2f 0a 73 74 61 74  Merge(). */.stat
33073 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
33074 64 65 72 73 4d 65 72 67 65 28 66 75 6c 6c 74 65  dersMerge(fullte
33075 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
33076 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33077 20 20 20 20 20 20 20 20 20 20 4c 65 61 76 65 73            Leaves
33078 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73  Reader *pReaders
33079 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a  , int nReaders,.
3307a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3307b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65                Le
3307c 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
3307d 72 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64  r){.  DLReader d
3307e 6c 52 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43  lReaders[MERGE_C
3307f 4f 55 4e 54 5d 3b 0a 20 20 63 6f 6e 73 74 20 63  OUNT];.  const c
33080 68 61 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61  har *pTerm = lea
33081 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 70 52  vesReaderTerm(pR
33082 65 61 64 65 72 73 29 3b 0a 20 20 69 6e 74 20 69  eaders);.  int i
33083 2c 20 6e 54 65 72 6d 20 3d 20 6c 65 61 76 65 73  , nTerm = leaves
33084 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
33085 70 52 65 61 64 65 72 73 29 3b 0a 0a 20 20 61 73  pReaders);..  as
33086 73 65 72 74 28 20 6e 52 65 61 64 65 72 73 3c 3d  sert( nReaders<=
33087 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 0a  MERGE_COUNT );..
33088 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65    for(i=0; i<nRe
33089 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  aders; i++){.   
3308a 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64   dlrInit(&dlRead
3308b 65 72 73 5b 69 5d 2c 20 44 4c 5f 44 45 46 41 55  ers[i], DL_DEFAU
3308c 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
3308d 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61  leavesReaderData
3308e 28 70 52 65 61 64 65 72 73 2b 69 29 2c 0a 20 20  (pReaders+i),.  
3308f 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73            leaves
33090 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28  ReaderDataBytes(
33091 70 52 65 61 64 65 72 73 2b 69 29 29 3b 0a 20 20  pReaders+i));.  
33092 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66  }..  return leaf
33093 57 72 69 74 65 72 53 74 65 70 4d 65 72 67 65 28  WriterStepMerge(
33094 76 2c 20 70 57 72 69 74 65 72 2c 20 70 54 65 72  v, pWriter, pTer
33095 6d 2c 20 6e 54 65 72 6d 2c 20 64 6c 52 65 61 64  m, nTerm, dlRead
33096 65 72 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a  ers, nReaders);.
33097 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  }../* Forward re
33098 66 20 64 75 65 20 74 6f 20 6d 75 74 75 61 6c 20  f due to mutual 
33099 72 65 63 75 72 73 69 6f 6e 20 77 69 74 68 20 73  recursion with s
3309a 65 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 29  egdirNextIndex()
3309b 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
3309c 73 65 67 6d 65 6e 74 4d 65 72 67 65 28 66 75 6c  segmentMerge(ful
3309d 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
3309e 6e 74 20 69 4c 65 76 65 6c 29 3b 0a 0a 2f 2a 20  nt iLevel);../* 
3309f 50 75 74 20 74 68 65 20 6e 65 78 74 20 61 76 61  Put the next ava
330a0 69 6c 61 62 6c 65 20 69 6e 64 65 78 20 61 74 20  ilable index at 
330a1 69 4c 65 76 65 6c 20 69 6e 74 6f 20 2a 70 69 64  iLevel into *pid
330a2 78 2e 20 20 49 66 20 69 4c 65 76 65 6c 0a 2a 2a  x.  If iLevel.**
330a3 20 61 6c 72 65 61 64 79 20 68 61 73 20 4d 45 52   already has MER
330a4 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74  GE_COUNT segment
330a5 73 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72 67  s, they are merg
330a6 65 64 20 74 6f 20 61 20 68 69 67 68 65 72 0a 2a  ed to a higher.*
330a7 2a 20 6c 65 76 65 6c 20 74 6f 20 6d 61 6b 65 20  * level to make 
330a8 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  room..*/.static 
330a9 69 6e 74 20 73 65 67 64 69 72 4e 65 78 74 49 6e  int segdirNextIn
330aa 64 65 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  dex(fulltext_vta
330ab 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c  b *v, int iLevel
330ac 2c 20 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20  , int *pidx){.  
330ad 69 6e 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f  int rc = segdir_
330ae 6d 61 78 5f 69 6e 64 65 78 28 76 2c 20 69 4c 65  max_index(v, iLe
330af 76 65 6c 2c 20 70 69 64 78 29 3b 0a 20 20 69 66  vel, pidx);.  if
330b0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
330b1 45 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  E ){            
330b2 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73    /* No segments
330b3 20 61 74 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20   at iLevel. */. 
330b4 20 20 20 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20     *pidx = 0;.  
330b5 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
330b6 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
330b7 69 66 28 20 2a 70 69 64 78 3d 3d 28 4d 45 52 47  if( *pidx==(MERG
330b8 45 5f 43 4f 55 4e 54 2d 31 29 20 29 7b 0a 20 20  E_COUNT-1) ){.  
330b9 20 20 20 20 72 63 20 3d 20 73 65 67 6d 65 6e 74      rc = segment
330ba 4d 65 72 67 65 28 76 2c 20 69 4c 65 76 65 6c 29  Merge(v, iLevel)
330bb 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
330bc 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
330bd 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 2a 70 69  rn rc;.      *pi
330be 64 78 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  dx = 0;.    }els
330bf 65 7b 0a 20 20 20 20 20 20 28 2a 70 69 64 78 29  e{.      (*pidx)
330c0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ++;.    }.  }els
330c1 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  e{.    return rc
330c2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
330c3 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
330c4 4d 65 72 67 65 20 4d 45 52 47 45 5f 43 4f 55 4e  Merge MERGE_COUN
330c5 54 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c  T segments at iL
330c6 65 76 65 6c 20 69 6e 74 6f 20 61 20 6e 65 77 20  evel into a new 
330c7 73 65 67 6d 65 6e 74 20 61 74 0a 2a 2a 20 69 4c  segment at.** iL
330c8 65 76 65 6c 2b 31 2e 20 20 49 66 20 69 4c 65 76  evel+1.  If iLev
330c9 65 6c 2b 31 20 69 73 20 61 6c 72 65 61 64 79 20  el+1 is already 
330ca 66 75 6c 6c 20 6f 66 20 73 65 67 6d 65 6e 74 73  full of segments
330cb 2c 20 74 68 6f 73 65 20 77 69 6c 6c 20 62 65 0a  , those will be.
330cc 2a 2a 20 6d 65 72 67 65 64 20 74 6f 20 6d 61 6b  ** merged to mak
330cd 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69  e room..*/.stati
330ce 63 20 69 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72  c int segmentMer
330cf 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ge(fulltext_vtab
330d0 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29   *v, int iLevel)
330d1 7b 0a 20 20 4c 65 61 66 57 72 69 74 65 72 20 77  {.  LeafWriter w
330d2 72 69 74 65 72 3b 0a 20 20 4c 65 61 76 65 73 52  riter;.  LeavesR
330d3 65 61 64 65 72 20 6c 72 73 5b 4d 45 52 47 45 5f  eader lrs[MERGE_
330d4 43 4f 55 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 2c  COUNT];.  int i,
330d5 20 72 63 2c 20 69 64 78 20 3d 20 30 3b 0a 0a 20   rc, idx = 0;.. 
330d6 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68   /* Determine th
330d7 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65  e next available
330d8 20 73 65 67 6d 65 6e 74 20 69 6e 64 65 78 20 61   segment index a
330d9 74 20 74 68 65 20 6e 65 78 74 20 6c 65 76 65 6c  t the next level
330da 2c 0a 20 20 2a 2a 20 6d 65 72 67 69 6e 67 20 61  ,.  ** merging a
330db 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a  s necessary..  *
330dc 2f 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72 4e  /.  rc = segdirN
330dd 65 78 74 49 6e 64 65 78 28 76 2c 20 69 4c 65 76  extIndex(v, iLev
330de 65 6c 2b 31 2c 20 26 69 64 78 29 3b 0a 20 20 69  el+1, &idx);.  i
330df 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
330e0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
330e1 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
330e2 54 68 69 73 20 61 73 73 75 6d 65 73 20 74 68 61  This assumes tha
330e3 74 20 77 65 27 6c 6c 20 61 6c 77 61 79 73 20 73  t we'll always s
330e4 65 65 20 65 78 61 63 74 6c 79 0a 20 20 2a 2a 20  ee exactly.  ** 
330e5 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d  MERGE_COUNT segm
330e6 65 6e 74 73 20 74 6f 20 6d 65 72 67 65 20 61 74  ents to merge at
330e7 20 61 20 67 69 76 65 6e 20 6c 65 76 65 6c 2e 20   a given level. 
330e8 20 54 68 61 74 20 77 69 6c 6c 20 62 65 0a 20 20   That will be.  
330e9 2a 2a 20 62 72 6f 6b 65 6e 20 69 66 20 77 65 20  ** broken if we 
330ea 61 6c 6c 6f 77 20 74 68 65 20 64 65 76 65 6c 6f  allow the develo
330eb 70 65 72 20 74 6f 20 72 65 71 75 65 73 74 20 70  per to request p
330ec 72 65 65 6d 70 74 69 76 65 20 6f 72 0a 20 20 2a  reemptive or.  *
330ed 2a 20 64 65 66 65 72 72 65 64 20 6d 65 72 67 69  * deferred mergi
330ee 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65  ng..  */.  memse
330ef 74 28 26 6c 72 73 2c 20 27 5c 30 27 2c 20 73 69  t(&lrs, '\0', si
330f0 7a 65 6f 66 28 6c 72 73 29 29 3b 0a 20 20 72 63  zeof(lrs));.  rc
330f1 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 73   = leavesReaders
330f2 49 6e 69 74 28 76 2c 20 69 4c 65 76 65 6c 2c 20  Init(v, iLevel, 
330f3 6c 72 73 2c 20 26 69 29 3b 0a 20 20 69 66 28 20  lrs, &i);.  if( 
330f4 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
330f5 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73  return rc;.  ass
330f6 65 72 74 28 20 69 3d 3d 4d 45 52 47 45 5f 43 4f  ert( i==MERGE_CO
330f7 55 4e 54 20 29 3b 0a 0a 20 20 6c 65 61 66 57 72  UNT );..  leafWr
330f8 69 74 65 72 49 6e 69 74 28 69 4c 65 76 65 6c 2b  iterInit(iLevel+
330f9 31 2c 20 69 64 78 2c 20 26 77 72 69 74 65 72 29  1, idx, &writer)
330fa 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 6c 65  ;..  /* Since le
330fb 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
330fc 72 28 29 20 70 75 73 68 65 73 20 72 65 61 64 65  r() pushes reade
330fd 72 73 20 61 74 20 65 6f 66 20 74 6f 20 74 68 65  rs at eof to the
330fe 20 65 6e 64 2c 0a 20 20 2a 2a 20 77 68 65 6e 20   end,.  ** when 
330ff 74 68 65 20 66 69 72 73 74 20 72 65 61 64 65 72  the first reader
33100 20 69 73 20 65 6d 70 74 79 2c 20 61 6c 6c 20 77   is empty, all w
33101 69 6c 6c 20 62 65 20 65 6d 70 74 79 2e 0a 20 20  ill be empty..  
33102 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21 6c 65 61  */.  while( !lea
33103 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c  vesReaderAtEnd(l
33104 72 73 29 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69  rs) ){.    /* Fi
33105 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
33106 79 20 72 65 61 64 65 72 73 20 73 68 61 72 65 20  y readers share 
33107 74 68 65 69 72 20 6e 65 78 74 20 74 65 72 6d 2e  their next term.
33108 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   */.    for(i=1;
33109 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 26   i<MERGE_COUNT &
3310a 26 20 21 6c 65 61 76 65 73 52 65 61 64 65 72 41  & !leavesReaderA
3310b 74 45 6e 64 28 6c 72 73 2b 69 29 3b 20 69 2b 2b  tEnd(lrs+i); i++
3310c 29 7b 0a 20 20 20 20 20 20 69 66 28 20 30 21 3d  ){.      if( 0!=
3310d 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
3310e 43 6d 70 28 6c 72 73 2c 20 6c 72 73 2b 69 29 20  Cmp(lrs, lrs+i) 
3310f 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a  ) break;.    }..
33110 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52      rc = leavesR
33111 65 61 64 65 72 73 4d 65 72 67 65 28 76 2c 20 6c  eadersMerge(v, l
33112 72 73 2c 20 69 2c 20 26 77 72 69 74 65 72 29 3b  rs, i, &writer);
33113 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
33114 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
33115 72 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20  r;..    /* Step 
33116 66 6f 72 77 61 72 64 20 74 68 6f 73 65 20 74 68  forward those th
33117 61 74 20 77 65 72 65 20 6d 65 72 67 65 64 2e 20  at were merged. 
33118 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d  */.    while( i-
33119 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ->0 ){.      rc 
3311a 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74  = leavesReaderSt
3311b 65 70 28 76 2c 20 6c 72 73 2b 69 29 3b 0a 20 20  ep(v, lrs+i);.  
3311c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
3311d 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
3311e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6f 72  ;..      /* Reor
3311f 64 65 72 20 62 79 20 74 65 72 6d 2c 20 74 68 65  der by term, the
33120 6e 20 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 20  n by age. */.   
33121 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 52     leavesReaderR
33122 65 6f 72 64 65 72 28 6c 72 73 2b 69 2c 20 4d 45  eorder(lrs+i, ME
33123 52 47 45 5f 43 4f 55 4e 54 2d 69 29 3b 0a 20 20  RGE_COUNT-i);.  
33124 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69    }.  }..  for(i
33125 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =0; i<MERGE_COUN
33126 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61  T; i++){.    lea
33127 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
33128 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 0a  (&lrs[i]);.  }..
33129 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
3312a 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72  rFinalize(v, &wr
3312b 69 74 65 72 29 3b 0a 20 20 6c 65 61 66 57 72 69  iter);.  leafWri
3312c 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69 74  terDestroy(&writ
3312d 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  er);.  if( rc!=S
3312e 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3312f 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65  n rc;..  /* Dele
33130 74 65 20 74 68 65 20 6d 65 72 67 65 64 20 73 65  te the merged se
33131 67 6d 65 6e 74 20 64 61 74 61 2e 20 2a 2f 0a 20  gment data. */. 
33132 20 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f 64   return segdir_d
33133 65 6c 65 74 65 28 76 2c 20 69 4c 65 76 65 6c 29  elete(v, iLevel)
33134 3b 0a 0a 20 65 72 72 3a 0a 20 20 66 6f 72 28 69  ;.. err:.  for(i
33135 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e  =0; i<MERGE_COUN
33136 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61  T; i++){.    lea
33137 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
33138 28 26 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  (&lrs[i]);.  }. 
33139 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72   leafWriterDestr
3313a 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72  oy(&writer);.  r
3313b 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
3313c 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 75  Accumulate the u
3313d 6e 69 6f 6e 20 6f 66 20 2a 61 63 63 20 61 6e 64  nion of *acc and
3313e 20 2a 70 44 61 74 61 20 69 6e 74 6f 20 2a 61 63   *pData into *ac
3313f 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  c. */.static voi
33140 64 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c  d docListAccumul
33141 61 74 65 55 6e 69 6f 6e 28 44 61 74 61 42 75 66  ateUnion(DataBuf
33142 66 65 72 20 2a 61 63 63 2c 0a 20 20 20 20 20 20  fer *acc,.      
33143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33144 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
33145 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
33146 69 6e 74 20 6e 44 61 74 61 29 20 7b 0a 20 20 44  int nData) {.  D
33147 61 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20  ataBuffer tmp = 
33148 2a 61 63 63 3b 0a 20 20 64 61 74 61 42 75 66 66  *acc;.  dataBuff
33149 65 72 49 6e 69 74 28 61 63 63 2c 20 74 6d 70 2e  erInit(acc, tmp.
3314a 6e 44 61 74 61 2b 6e 44 61 74 61 29 3b 0a 20 20  nData+nData);.  
3314b 64 6f 63 4c 69 73 74 55 6e 69 6f 6e 28 74 6d 70  docListUnion(tmp
3314c 2e 70 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74  .pData, tmp.nDat
3314d 61 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  a, pData, nData,
3314e 20 61 63 63 29 3b 0a 20 20 64 61 74 61 42 75 66   acc);.  dataBuf
3314f 66 65 72 44 65 73 74 72 6f 79 28 26 74 6d 70 29  ferDestroy(&tmp)
33150 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ;.}../* TODO(she
33151 73 73 29 20 49 74 20 6d 69 67 68 74 20 62 65 20  ss) It might be 
33152 69 6e 74 65 72 65 73 74 69 6e 67 20 74 6f 20 65  interesting to e
33153 78 70 6c 6f 72 65 20 64 69 66 66 65 72 65 6e 74  xplore different
33154 20 6d 65 72 67 65 0a 2a 2a 20 73 74 72 61 74 65   merge.** strate
33155 67 69 65 73 2c 20 68 65 72 65 2e 20 20 46 6f 72  gies, here.  For
33156 20 69 6e 73 74 61 6e 63 65 2c 20 73 69 6e 63 65   instance, since
33157 20 74 68 69 73 20 69 73 20 61 20 73 6f 72 74 65   this is a sorte
33158 64 20 6d 65 72 67 65 2c 20 77 65 0a 2a 2a 20 63  d merge, we.** c
33159 6f 75 6c 64 20 65 61 73 69 6c 79 20 6d 65 72 67  ould easily merg
3315a 65 20 6d 61 6e 79 20 64 6f 63 6c 69 73 74 73 20  e many doclists 
3315b 69 6e 20 70 61 72 61 6c 6c 65 6c 2e 20 20 57 69  in parallel.  Wi
3315c 74 68 20 73 6f 6d 65 0a 2a 2a 20 63 6f 6d 70 72  th some.** compr
3315d 65 68 65 6e 73 69 6f 6e 20 6f 66 20 74 68 65 20  ehension of the 
3315e 73 74 6f 72 61 67 65 20 66 6f 72 6d 61 74 2c 20  storage format, 
3315f 77 65 20 63 6f 75 6c 64 20 6d 65 72 67 65 20 61  we could merge a
33160 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 64 6f 63  ll of the.** doc
33161 6c 69 73 74 73 20 77 69 74 68 69 6e 20 61 20 6c  lists within a l
33162 65 61 66 20 6e 6f 64 65 20 64 69 72 65 63 74 6c  eaf node directl
33163 79 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20  y from the leaf 
33164 6e 6f 64 65 27 73 20 73 74 6f 72 61 67 65 2e 0a  node's storage..
33165 2a 2a 20 49 74 20 6d 61 79 20 62 65 20 77 6f 72  ** It may be wor
33166 74 68 77 68 69 6c 65 20 74 6f 20 6d 65 72 67 65  thwhile to merge
33167 20 73 6d 61 6c 6c 65 72 20 64 6f 63 6c 69 73 74   smaller doclist
33168 73 20 62 65 66 6f 72 65 20 6c 61 72 67 65 72 0a  s before larger.
33169 2a 2a 20 64 6f 63 6c 69 73 74 73 2c 20 73 69 6e  ** doclists, sin
3316a 63 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 74  ce they can be t
3316b 72 61 76 65 72 73 65 64 20 6d 6f 72 65 20 71 75  raversed more qu
3316c 69 63 6b 6c 79 20 2d 20 62 75 74 20 74 68 65 0a  ickly - but the.
3316d 2a 2a 20 72 65 73 75 6c 74 73 20 6d 61 79 20 68  ** results may h
3316e 61 76 65 20 6c 65 73 73 20 6f 76 65 72 6c 61 70  ave less overlap
3316f 2c 20 6d 61 6b 69 6e 67 20 74 68 65 6d 20 6d 6f  , making them mo
33170 72 65 20 65 78 70 65 6e 73 69 76 65 20 69 6e 20  re expensive in 
33171 61 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 77  a.** different w
33172 61 79 2e 0a 2a 2f 0a 0a 2f 2a 20 53 63 61 6e 20  ay..*/../* Scan 
33173 70 52 65 61 64 65 72 20 66 6f 72 20 70 54 65 72  pReader for pTer
33174 6d 2f 6e 54 65 72 6d 2c 20 61 6e 64 20 6d 65 72  m/nTerm, and mer
33175 67 65 20 74 68 65 20 74 65 72 6d 27 73 20 64 6f  ge the term's do
33176 63 6c 69 73 74 20 6f 76 65 72 0a 2a 2a 20 2a 6f  clist over.** *o
33177 75 74 20 28 61 6e 79 20 64 6f 63 6c 69 73 74 73  ut (any doclists
33178 20 77 69 74 68 20 64 75 70 6c 69 63 61 74 65 20   with duplicate 
33179 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69 74 65  docids overwrite
3317a 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e   those in *out).
3317b 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e  .** Internal fun
3317c 63 74 69 6f 6e 20 66 6f 72 20 6c 6f 61 64 53 65  ction for loadSe
3317d 67 6d 65 6e 74 4c 65 61 66 28 29 2e 0a 2a 2f 0a  gmentLeaf()..*/.
3317e 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53  static int loadS
3317f 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28  egmentLeavesInt(
33180 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
33181 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  , LeavesReader *
33182 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20  pReader,.       
33183 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33184 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
33185 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
33186 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65  nTerm, int isPre
33187 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fix,.           
33188 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33189 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
3318a 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20 64 6f 63 6c  *out){.  /* docl
3318b 69 73 74 20 64 61 74 61 20 69 73 20 61 63 63 75  ist data is accu
3318c 6d 75 6c 61 74 65 64 20 69 6e 74 6f 20 70 42 75  mulated into pBu
3318d 66 66 65 72 73 20 73 69 6d 69 6c 61 72 20 74 6f  ffers similar to
3318e 20 68 6f 77 20 6f 6e 65 20 64 6f 65 73 0a 20 20   how one does.  
3318f 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 69 6e 20  ** increment in 
33190 62 69 6e 61 72 79 20 61 72 69 74 68 6d 65 74 69  binary arithmeti
33191 63 2e 20 20 49 66 20 69 6e 64 65 78 20 30 20 69  c.  If index 0 i
33192 73 20 65 6d 70 74 79 2c 20 74 68 65 20 64 61 74  s empty, the dat
33193 61 20 69 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64  a is.  ** stored
33194 20 74 68 65 72 65 2e 20 20 49 66 20 74 68 65 72   there.  If ther
33195 65 20 69 73 20 64 61 74 61 20 74 68 65 72 65 2c  e is data there,
33196 20 69 74 20 69 73 20 6d 65 72 67 65 64 20 61 6e   it is merged an
33197 64 20 74 68 65 0a 20 20 2a 2a 20 72 65 73 75 6c  d the.  ** resul
33198 74 73 20 63 61 72 72 69 65 64 20 69 6e 74 6f 20  ts carried into 
33199 70 6f 73 69 74 69 6f 6e 20 31 2c 20 77 69 74 68  position 1, with
3319a 20 66 75 72 74 68 65 72 20 6d 65 72 67 65 2d 61   further merge-a
3319b 6e 64 2d 63 61 72 72 79 0a 20 20 2a 2a 20 75 6e  nd-carry.  ** un
3319c 74 69 6c 20 61 6e 20 65 6d 70 74 79 20 70 6f 73  til an empty pos
3319d 69 74 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2e 0a  ition is found..
3319e 20 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65    */.  DataBuffe
3319f 72 20 2a 70 42 75 66 66 65 72 73 20 3d 20 4e 55  r *pBuffers = NU
331a0 4c 4c 3b 0a 20 20 69 6e 74 20 6e 42 75 66 66 65  LL;.  int nBuffe
331a1 72 73 20 3d 20 30 2c 20 6e 4d 61 78 42 75 66 66  rs = 0, nMaxBuff
331a2 65 72 73 20 3d 20 30 2c 20 72 63 3b 0a 0a 20 20  ers = 0, rc;..  
331a3 61 73 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20  assert( nTerm>0 
331a4 29 3b 0a 0a 20 20 66 6f 72 28 72 63 3d 53 51 4c  );..  for(rc=SQL
331a5 49 54 45 5f 4f 4b 3b 20 72 63 3d 3d 53 51 4c 49  ITE_OK; rc==SQLI
331a6 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65 73  TE_OK && !leaves
331a7 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61  ReaderAtEnd(pRea
331a8 64 65 72 29 3b 0a 20 20 20 20 20 20 72 63 3d 6c  der);.      rc=l
331a9 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28  eavesReaderStep(
331aa 76 2c 20 70 52 65 61 64 65 72 29 29 7b 0a 20 20  v, pReader)){.  
331ab 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
331ac 20 52 65 61 6c 6c 79 20 77 61 6e 74 20 6c 65 61   Really want lea
331ad 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
331ae 28 29 2c 20 62 75 74 20 74 68 61 74 20 6e 61 6d  (), but that nam
331af 65 20 69 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65  e is.    ** alre
331b0 61 64 79 20 74 61 6b 65 6e 20 74 6f 20 63 6f 6d  ady taken to com
331b1 70 61 72 65 20 74 68 65 20 74 65 72 6d 73 20 6f  pare the terms o
331b2 66 20 74 77 6f 20 4c 65 61 76 65 73 52 65 61 64  f two LeavesRead
331b3 65 72 73 2e 20 20 54 68 69 6e 6b 0a 20 20 20 20  ers.  Think.    
331b4 2a 2a 20 6f 6e 20 61 20 62 65 74 74 65 72 20 6e  ** on a better n
331b5 61 6d 65 2e 20 20 5b 4d 65 61 6e 77 68 69 6c 65  ame.  [Meanwhile
331b6 2c 20 62 72 65 61 6b 20 65 6e 63 61 70 73 75 6c  , break encapsul
331b7 61 74 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61  ation rather tha
331b8 6e 0a 20 20 20 20 2a 2a 20 75 73 65 20 61 20 63  n.    ** use a c
331b9 6f 6e 66 75 73 69 6e 67 20 6e 61 6d 65 2e 5d 0a  onfusing name.].
331ba 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63      */.    int c
331bb 20 3d 20 6c 65 61 66 52 65 61 64 65 72 54 65 72   = leafReaderTer
331bc 6d 43 6d 70 28 26 70 52 65 61 64 65 72 2d 3e 6c  mCmp(&pReader->l
331bd 65 61 66 52 65 61 64 65 72 2c 20 70 54 65 72 6d  eafReader, pTerm
331be 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
331bf 78 29 3b 0a 20 20 20 20 69 66 28 20 63 3e 30 20  x);.    if( c>0 
331c0 29 20 62 72 65 61 6b 3b 20 20 20 20 20 20 2f 2a  ) break;      /*
331c1 20 50 61 73 74 20 61 6e 79 20 70 6f 73 73 69 62   Past any possib
331c2 6c 65 20 6d 61 74 63 68 65 73 2e 20 2a 2f 0a 20  le matches. */. 
331c3 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20     if( c==0 ){. 
331c4 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
331c5 2a 70 44 61 74 61 20 3d 20 6c 65 61 76 65 73 52  *pData = leavesR
331c6 65 61 64 65 72 44 61 74 61 28 70 52 65 61 64 65  eaderData(pReade
331c7 72 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 42  r);.      int iB
331c8 75 66 66 65 72 2c 20 6e 44 61 74 61 20 3d 20 6c  uffer, nData = l
331c9 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42  eavesReaderDataB
331ca 79 74 65 73 28 70 52 65 61 64 65 72 29 3b 0a 0a  ytes(pReader);..
331cb 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68        /* Find th
331cc 65 20 66 69 72 73 74 20 65 6d 70 74 79 20 62 75  e first empty bu
331cd 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 66  ffer. */.      f
331ce 6f 72 28 69 42 75 66 66 65 72 3d 30 3b 20 69 42  or(iBuffer=0; iB
331cf 75 66 66 65 72 3c 6e 42 75 66 66 65 72 73 3b 20  uffer<nBuffers; 
331d0 2b 2b 69 42 75 66 66 65 72 29 7b 0a 20 20 20 20  ++iBuffer){.    
331d1 20 20 20 20 69 66 28 20 30 3d 3d 70 42 75 66 66      if( 0==pBuff
331d2 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61  ers[iBuffer].nDa
331d3 74 61 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ta ) break;.    
331d4 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75    }..      /* Ou
331d5 74 20 6f 66 20 62 75 66 66 65 72 73 2c 20 61 64  t of buffers, ad
331d6 64 20 61 6e 20 65 6d 70 74 79 20 6f 6e 65 2e 20  d an empty one. 
331d7 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 42 75  */.      if( iBu
331d8 66 66 65 72 3d 3d 6e 42 75 66 66 65 72 73 20 29  ffer==nBuffers )
331d9 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 42  {.        if( nB
331da 75 66 66 65 72 73 3d 3d 6e 4d 61 78 42 75 66 66  uffers==nMaxBuff
331db 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ers ){.         
331dc 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 3b 0a   DataBuffer *p;.
331dd 20 20 20 20 20 20 20 20 20 20 6e 4d 61 78 42 75            nMaxBu
331de 66 66 65 72 73 20 2b 3d 20 32 30 3b 0a 0a 20 20  ffers += 20;..  
331df 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6e 75 61          /* Manua
331e0 6c 20 72 65 61 6c 6c 6f 63 20 73 6f 20 77 65 20  l realloc so we 
331e1 63 61 6e 20 68 61 6e 64 6c 65 20 4e 55 4c 4c 20  can handle NULL 
331e2 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a  appropriately. *
331e3 2f 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20  /.          p = 
331e4 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
331e5 4d 61 78 42 75 66 66 65 72 73 2a 73 69 7a 65 6f  MaxBuffers*sizeo
331e6 66 28 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20  f(*pBuffers));. 
331e7 20 20 20 20 20 20 20 20 20 69 66 28 20 70 3d 3d           if( p==
331e8 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  NULL ){.        
331e9 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
331ea 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
331eb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
331ec 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
331ed 20 69 66 28 20 6e 42 75 66 66 65 72 73 3e 30 20   if( nBuffers>0 
331ee 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
331ef 73 73 65 72 74 28 70 42 75 66 66 65 72 73 21 3d  ssert(pBuffers!=
331f0 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 20  NULL);.         
331f1 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 42 75     memcpy(p, pBu
331f2 66 66 65 72 73 2c 20 6e 42 75 66 66 65 72 73 2a  ffers, nBuffers*
331f3 73 69 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73  sizeof(*pBuffers
331f4 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ));.            
331f5 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75  sqlite3_free(pBu
331f6 66 66 65 72 73 29 3b 0a 20 20 20 20 20 20 20 20  ffers);.        
331f7 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 42    }.          pB
331f8 75 66 66 65 72 73 20 3d 20 70 3b 0a 20 20 20 20  uffers = p;.    
331f9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61      }.        da
331fa 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 28 70  taBufferInit(&(p
331fb 42 75 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73  Buffers[nBuffers
331fc 5d 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ]), 0);.        
331fd 6e 42 75 66 66 65 72 73 2b 2b 3b 0a 20 20 20 20  nBuffers++;.    
331fe 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74    }..      /* At
331ff 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6d 75 73   this point, mus
33200 74 20 68 61 76 65 20 61 6e 20 65 6d 70 74 79 20  t have an empty 
33201 61 74 20 69 42 75 66 66 65 72 2e 20 2a 2f 0a 20  at iBuffer. */. 
33202 20 20 20 20 20 61 73 73 65 72 74 28 69 42 75 66       assert(iBuf
33203 66 65 72 3c 6e 42 75 66 66 65 72 73 20 26 26 20  fer<nBuffers && 
33204 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
33205 5d 2e 6e 44 61 74 61 3d 3d 30 29 3b 0a 0a 20 20  ].nData==0);..  
33206 20 20 20 20 2f 2a 20 49 66 20 65 6d 70 74 79 20      /* If empty 
33207 77 61 73 20 66 69 72 73 74 20 62 75 66 66 65 72  was first buffer
33208 2c 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 6d 65  , no need for me
33209 72 67 65 20 6c 6f 67 69 63 2e 20 2a 2f 0a 20 20  rge logic. */.  
3320a 20 20 20 20 69 66 28 20 69 42 75 66 66 65 72 3d      if( iBuffer=
3320b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 61  =0 ){.        da
3320c 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
3320d 26 28 70 42 75 66 66 65 72 73 5b 30 5d 29 2c 20  &(pBuffers[0]), 
3320e 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20  pData, nData);. 
3320f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
33210 20 20 20 20 2f 2a 20 70 41 63 63 20 69 73 20 74      /* pAcc is t
33211 68 65 20 65 6d 70 74 79 20 62 75 66 66 65 72 20  he empty buffer 
33212 74 68 65 20 6d 65 72 67 65 64 20 64 61 74 61 20  the merged data 
33213 77 69 6c 6c 20 65 6e 64 20 75 70 20 69 6e 2e 20  will end up in. 
33214 2a 2f 0a 20 20 20 20 20 20 20 20 44 61 74 61 42  */.        DataB
33215 75 66 66 65 72 20 2a 70 41 63 63 20 3d 20 26 28  uffer *pAcc = &(
33216 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72  pBuffers[iBuffer
33217 5d 29 3b 0a 20 20 20 20 20 20 20 20 44 61 74 61  ]);.        Data
33218 42 75 66 66 65 72 20 2a 70 20 3d 20 26 28 70 42  Buffer *p = &(pB
33219 75 66 66 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20  uffers[0]);..   
3321a 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 70       /* Handle p
3321b 6f 73 69 74 69 6f 6e 20 30 20 73 70 65 63 69 61  osition 0 specia
3321c 6c 6c 79 20 74 6f 20 61 76 6f 69 64 20 6e 65 65  lly to avoid nee
3321d 64 20 74 6f 20 70 72 69 6d 65 20 70 41 63 63 0a  d to prime pAcc.
3321e 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20          ** with 
3321f 70 44 61 74 61 2f 6e 44 61 74 61 2e 0a 20 20 20  pData/nData..   
33220 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
33221 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28 70  dataBufferSwap(p
33222 2c 20 70 41 63 63 29 3b 0a 20 20 20 20 20 20 20  , pAcc);.       
33223 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61   docListAccumula
33224 74 65 55 6e 69 6f 6e 28 70 41 63 63 2c 20 70 44  teUnion(pAcc, pD
33225 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20  ata, nData);..  
33226 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
33227 61 74 65 20 72 65 6d 61 69 6e 69 6e 67 20 64 6f  ate remaining do
33228 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 41 63 63  clists into pAcc
33229 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72  . */.        for
3322a 28 2b 2b 70 3b 20 70 3c 70 41 63 63 3b 20 2b 2b  (++p; p<pAcc; ++
3322b 70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f  p){.          do
3322c 63 4c 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55  cListAccumulateU
3322d 6e 69 6f 6e 28 70 41 63 63 2c 20 70 2d 3e 70 44  nion(pAcc, p->pD
3322e 61 74 61 2c 20 70 2d 3e 6e 44 61 74 61 29 3b 0a  ata, p->nData);.
3322f 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 61  .          /* da
33230 74 61 42 75 66 66 65 72 52 65 73 65 74 28 29 20  taBufferReset() 
33231 63 6f 75 6c 64 20 61 6c 6c 6f 77 20 61 20 6c 61  could allow a la
33232 72 67 65 20 64 6f 63 6c 69 73 74 20 74 6f 20 62  rge doclist to b
33233 6c 6f 77 20 75 70 0a 20 20 20 20 20 20 20 20 20  low up.         
33234 20 2a 2a 20 6f 75 72 20 6d 65 6d 6f 72 79 20 72   ** our memory r
33235 65 71 75 69 72 65 6d 65 6e 74 73 2e 0a 20 20 20  equirements..   
33236 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
33237 20 20 20 20 69 66 28 20 70 2d 3e 6e 43 61 70 61      if( p->nCapa
33238 63 69 74 79 3c 31 30 32 34 20 29 7b 0a 20 20 20  city<1024 ){.   
33239 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66           dataBuf
3323a 66 65 72 52 65 73 65 74 28 70 29 3b 0a 20 20 20  ferReset(p);.   
3323b 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
3323c 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75            dataBu
3323d 66 66 65 72 44 65 73 74 72 6f 79 28 70 29 3b 0a  fferDestroy(p);.
3323e 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
3323f 42 75 66 66 65 72 49 6e 69 74 28 70 2c 20 30 29  BufferInit(p, 0)
33240 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
33241 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
33242 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
33243 55 6e 69 6f 6e 20 61 6c 6c 20 74 68 65 20 64 6f  Union all the do
33244 63 6c 69 73 74 73 20 74 6f 67 65 74 68 65 72 20  clists together 
33245 69 6e 74 6f 20 2a 6f 75 74 2e 20 2a 2f 0a 20 20  into *out. */.  
33246 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57  /* TODO(shess) W
33247 68 61 74 20 69 66 20 2a 6f 75 74 20 69 73 20 62  hat if *out is b
33248 69 67 3f 20 20 53 69 67 68 2e 20 2a 2f 0a 20 20  ig?  Sigh. */.  
33249 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3324a 4b 20 26 26 20 6e 42 75 66 66 65 72 73 3e 30 20  K && nBuffers>0 
3324b 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 66 66  ){.    int iBuff
3324c 65 72 3b 0a 20 20 20 20 66 6f 72 28 69 42 75 66  er;.    for(iBuf
3324d 66 65 72 3d 30 3b 20 69 42 75 66 66 65 72 3c 6e  fer=0; iBuffer<n
3324e 42 75 66 66 65 72 73 3b 20 2b 2b 69 42 75 66 66  Buffers; ++iBuff
3324f 65 72 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  er){.      if( p
33250 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d  Buffers[iBuffer]
33251 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20  .nData>0 ){.    
33252 20 20 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61      if( out->nDa
33253 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ta==0 ){.       
33254 20 20 20 64 61 74 61 42 75 66 66 65 72 53 77 61     dataBufferSwa
33255 70 28 6f 75 74 2c 20 26 28 70 42 75 66 66 65 72  p(out, &(pBuffer
33256 73 5b 69 42 75 66 66 65 72 5d 29 29 3b 0a 20 20  s[iBuffer]));.  
33257 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
33258 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 41 63         docListAc
33259 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 6f 75  cumulateUnion(ou
3325a 74 2c 20 70 42 75 66 66 65 72 73 5b 69 42 75 66  t, pBuffers[iBuf
3325b 66 65 72 5d 2e 70 44 61 74 61 2c 0a 20 20 20 20  fer].pData,.    
3325c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3325d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 42 75               pBu
3325e 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e  ffers[iBuffer].n
3325f 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 7d  Data);.        }
33260 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
33261 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 6e 42 75   }..  while( nBu
33262 66 66 65 72 73 2d 2d 20 29 7b 0a 20 20 20 20 64  ffers-- ){.    d
33263 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
33264 28 26 28 70 42 75 66 66 65 72 73 5b 6e 42 75 66  (&(pBuffers[nBuf
33265 66 65 72 73 5d 29 29 3b 0a 20 20 7d 0a 20 20 69  fers]));.  }.  i
33266 66 28 20 70 42 75 66 66 65 72 73 21 3d 4e 55 4c  f( pBuffers!=NUL
33267 4c 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65  L ) sqlite3_free
33268 28 70 42 75 66 66 65 72 73 29 3b 0a 0a 20 20 72  (pBuffers);..  r
33269 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
3326a 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74  Call loadSegment
3326b 4c 65 61 76 65 73 49 6e 74 28 29 20 77 69 74 68  LeavesInt() with
3326c 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61 73 20   pData/nData as 
3326d 69 6e 70 75 74 2e 20 2a 2f 0a 73 74 61 74 69 63  input. */.static
3326e 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74   int loadSegment
3326f 4c 65 61 66 28 66 75 6c 6c 74 65 78 74 5f 76 74  Leaf(fulltext_vt
33270 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61  ab *v, const cha
33271 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
33272 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
33273 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33274 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
33275 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
33276 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20  t isPrefix,.    
33277 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33278 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65         DataBuffe
33279 72 20 2a 6f 75 74 29 7b 0a 20 20 4c 65 61 76 65  r *out){.  Leave
3327a 73 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  sReader reader;.
3327b 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
3327c 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a  ert( nData>1 );.
3327d 20 20 61 73 73 65 72 74 28 20 2a 70 44 61 74 61    assert( *pData
3327e 3d 3d 27 5c 30 27 20 29 3b 0a 20 20 72 63 20 3d  =='\0' );.  rc =
3327f 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69   leavesReaderIni
33280 74 28 76 2c 20 30 2c 20 30 2c 20 30 2c 20 70 44  t(v, 0, 0, 0, pD
33281 61 74 61 2c 20 6e 44 61 74 61 2c 20 26 72 65 61  ata, nData, &rea
33282 64 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  der);.  if( rc!=
33283 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
33284 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c  rn rc;..  rc = l
33285 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
33286 49 6e 74 28 76 2c 20 26 72 65 61 64 65 72 2c 20  Int(v, &reader, 
33287 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
33288 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20  Prefix, out);.  
33289 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73 65  leavesReaderRese
3328a 74 28 26 72 65 61 64 65 72 29 3b 0a 20 20 6c 65  t(&reader);.  le
3328b 61 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f  avesReaderDestro
3328c 79 28 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65  y(&reader);.  re
3328d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43  turn rc;.}../* C
3328e 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c  all loadSegmentL
3328f 65 61 76 65 73 49 6e 74 28 29 20 77 69 74 68 20  eavesInt() with 
33290 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 73 20 66  the leaf nodes f
33291 72 6f 6d 20 69 53 74 61 72 74 4c 65 61 66 20 74  rom iStartLeaf t
33292 6f 0a 2a 2a 20 69 45 6e 64 4c 65 61 66 20 28 69  o.** iEndLeaf (i
33293 6e 63 6c 75 73 69 76 65 29 20 61 73 20 69 6e 70  nclusive) as inp
33294 75 74 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68  ut, and merge th
33295 65 20 72 65 73 75 6c 74 69 6e 67 20 64 6f 63 6c  e resulting docl
33296 69 73 74 20 69 6e 74 6f 0a 2a 2a 20 6f 75 74 2e  ist into.** out.
33297 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
33298 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
33299 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
3329a 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
3329b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3329c 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74  sqlite_int64 iSt
3329d 61 72 74 4c 65 61 66 2c 20 73 71 6c 69 74 65 5f  artLeaf, sqlite_
3329e 69 6e 74 36 34 20 69 45 6e 64 4c 65 61 66 2c 0a  int64 iEndLeaf,.
3329f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332a0 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
332a1 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
332a2 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
332a3 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
332a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332a5 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
332a6 20 2a 6f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63   *out){.  int rc
332a7 3b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65 72  ;.  LeavesReader
332a8 20 72 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65   reader;..  asse
332a9 72 74 28 20 69 53 74 61 72 74 4c 65 61 66 3c 3d  rt( iStartLeaf<=
332aa 69 45 6e 64 4c 65 61 66 20 29 3b 0a 20 20 72 63  iEndLeaf );.  rc
332ab 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 49   = leavesReaderI
332ac 6e 69 74 28 76 2c 20 30 2c 20 69 53 74 61 72 74  nit(v, 0, iStart
332ad 4c 65 61 66 2c 20 69 45 6e 64 4c 65 61 66 2c 20  Leaf, iEndLeaf, 
332ae 4e 55 4c 4c 2c 20 30 2c 20 26 72 65 61 64 65 72  NULL, 0, &reader
332af 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
332b0 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
332b1 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64  rc;..  rc = load
332b2 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74  SegmentLeavesInt
332b3 28 76 2c 20 26 72 65 61 64 65 72 2c 20 70 54 65  (v, &reader, pTe
332b4 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
332b5 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61  fix, out);.  lea
332b6 76 65 73 52 65 61 64 65 72 52 65 73 65 74 28 26  vesReaderReset(&
332b7 72 65 61 64 65 72 29 3b 0a 20 20 6c 65 61 76 65  reader);.  leave
332b8 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
332b9 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72  reader);.  retur
332ba 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 61 6b 69  n rc;.}../* Taki
332bb 6e 67 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61  ng pData/nData a
332bc 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
332bd 64 65 2c 20 66 69 6e 64 20 74 68 65 20 73 65 71  de, find the seq
332be 75 65 6e 63 65 20 6f 66 20 63 68 69 6c 64 0a 2a  uence of child.*
332bf 2a 20 6e 6f 64 65 73 20 77 68 69 63 68 20 63 6f  * nodes which co
332c0 75 6c 64 20 69 6e 63 6c 75 64 65 20 70 54 65 72  uld include pTer
332c1 6d 2f 6e 54 65 72 6d 2f 69 73 50 72 65 66 69 78  m/nTerm/isPrefix
332c2 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  .  Note that the
332c3 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  .** interior nod
332c4 65 20 74 65 72 6d 73 20 6c 6f 67 69 63 61 6c 6c  e terms logicall
332c5 79 20 63 6f 6d 65 20 62 65 74 77 65 65 6e 20 74  y come between t
332c6 68 65 20 62 6c 6f 63 6b 73 2c 20 73 6f 20 74 68  he blocks, so th
332c7 65 72 65 20 69 73 0a 2a 2a 20 6f 6e 65 20 6d 6f  ere is.** one mo
332c8 72 65 20 62 6c 6f 63 6b 69 64 20 74 68 61 6e 20  re blockid than 
332c9 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73 20  there are terms 
332ca 28 74 68 61 74 20 62 6c 6f 63 6b 20 63 6f 6e 74  (that block cont
332cb 61 69 6e 73 20 74 65 72 6d 73 20 3e 3d 0a 2a 2a  ains terms >=.**
332cc 20 74 68 65 20 6c 61 73 74 20 69 6e 74 65 72 69   the last interi
332cd 6f 72 2d 6e 6f 64 65 20 74 65 72 6d 29 2e 0a 2a  or-node term)..*
332ce 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
332cf 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64   The calling cod
332d0 65 20 6d 61 79 20 61 6c 72 65 61 64 79 20 6b 6e  e may already kn
332d1 6f 77 20 74 68 61 74 20 74 68 65 20 65 6e 64 20  ow that the end 
332d2 63 68 69 6c 64 20 69 73 0a 2a 2a 20 6e 6f 74 20  child is.** not 
332d3 77 6f 72 74 68 20 63 61 6c 63 75 6c 61 74 69 6e  worth calculatin
332d4 67 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 65  g, because the e
332d5 6e 64 20 6d 61 79 20 62 65 20 69 6e 20 61 20 6c  nd may be in a l
332d6 61 74 65 72 20 73 69 62 6c 69 6e 67 0a 2a 2a 20  ater sibling.** 
332d7 6e 6f 64 65 2e 20 20 43 6f 6e 73 69 64 65 72 20  node.  Consider 
332d8 77 68 65 74 68 65 72 20 62 72 65 61 6b 69 6e 67  whether breaking
332d9 20 73 79 6d 6d 65 74 72 79 20 69 73 20 77 6f 72   symmetry is wor
332da 74 68 77 68 69 6c 65 2e 20 20 49 20 73 75 73 70  thwhile.  I susp
332db 65 63 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74  ect.** it is not
332dc 20 77 6f 72 74 68 77 68 69 6c 65 2e 0a 2a 2f 0a   worthwhile..*/.
332dd 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43  static void getC
332de 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
332df 67 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  g(const char *pD
332e0 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
332e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332e3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
332e4 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
332e5 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
332e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332e8 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
332e9 53 74 61 72 74 43 68 69 6c 64 2c 0a 20 20 20 20  StartChild,.    
332ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
332ec 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e  lite_int64 *piEn
332ed 64 43 68 69 6c 64 29 7b 0a 20 20 49 6e 74 65 72  dChild){.  Inter
332ee 69 6f 72 52 65 61 64 65 72 20 72 65 61 64 65 72  iorReader reader
332ef 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  ;..  assert( nDa
332f0 74 61 3e 31 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>1 );.  assert
332f1 28 20 2a 70 44 61 74 61 21 3d 27 5c 30 27 20 29  ( *pData!='\0' )
332f2 3b 0a 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64  ;.  interiorRead
332f3 65 72 49 6e 69 74 28 70 44 61 74 61 2c 20 6e 44  erInit(pData, nD
332f4 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 0a  ata, &reader);..
332f5 20 20 2f 2a 20 53 63 61 6e 20 66 6f 72 20 74 68    /* Scan for th
332f6 65 20 66 69 72 73 74 20 63 68 69 6c 64 20 77 68  e first child wh
332f7 69 63 68 20 63 6f 75 6c 64 20 63 6f 6e 74 61 69  ich could contai
332f8 6e 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 2a  n pTerm/nTerm. *
332f9 2f 0a 20 20 77 68 69 6c 65 28 20 21 69 6e 74 65  /.  while( !inte
332fa 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28  riorReaderAtEnd(
332fb 26 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  &reader) ){.    
332fc 69 66 28 20 69 6e 74 65 72 69 6f 72 52 65 61 64  if( interiorRead
332fd 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64 65  erTermCmp(&reade
332fe 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  r, pTerm, nTerm,
332ff 20 30 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20   0)>0 ) break;. 
33300 20 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65     interiorReade
33301 72 53 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a  rStep(&reader);.
33302 20 20 7d 0a 20 20 2a 70 69 53 74 61 72 74 43 68    }.  *piStartCh
33303 69 6c 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65  ild = interiorRe
33304 61 64 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b  aderCurrentBlock
33305 69 64 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20  id(&reader);..  
33306 2f 2a 20 4b 65 65 70 20 73 63 61 6e 6e 69 6e 67  /* Keep scanning
33307 20 74 6f 20 66 69 6e 64 20 61 20 74 65 72 6d 20   to find a term 
33308 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 75 72  greater than our
33309 20 74 65 72 6d 2c 20 75 73 69 6e 67 20 70 72 65   term, using pre
3330a 66 69 78 0a 20 20 2a 2a 20 63 6f 6d 70 61 72 69  fix.  ** compari
3330b 73 6f 6e 20 69 66 20 69 6e 64 69 63 61 74 65 64  son if indicated
3330c 2e 20 20 49 66 20 69 73 50 72 65 66 69 78 20 69  .  If isPrefix i
3330d 73 20 66 61 6c 73 65 2c 20 74 68 69 73 20 77 69  s false, this wi
3330e 6c 6c 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 73  ll be the.  ** s
3330f 61 6d 65 20 62 6c 6f 63 6b 69 64 20 61 73 20 74  ame blockid as t
33310 68 65 20 73 74 61 72 74 69 6e 67 20 62 6c 6f 63  he starting bloc
33311 6b 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  k..  */.  while(
33312 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72   !interiorReader
33313 41 74 45 6e 64 28 26 72 65 61 64 65 72 29 20 29  AtEnd(&reader) )
33314 7b 0a 20 20 20 20 69 66 28 20 69 6e 74 65 72 69  {.    if( interi
33315 6f 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  orReaderTermCmp(
33316 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20  &reader, pTerm, 
33317 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 29  nTerm, isPrefix)
33318 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  >0 ) break;.    
33319 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 53 74  interiorReaderSt
3331a 65 70 28 26 72 65 61 64 65 72 29 3b 0a 20 20 7d  ep(&reader);.  }
3331b 0a 20 20 2a 70 69 45 6e 64 43 68 69 6c 64 20 3d  .  *piEndChild =
3331c 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
3331d 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72  urrentBlockid(&r
3331e 65 61 64 65 72 29 3b 0a 0a 20 20 69 6e 74 65 72  eader);..  inter
3331f 69 6f 72 52 65 61 64 65 72 44 65 73 74 72 6f 79  iorReaderDestroy
33320 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (&reader);..  /*
33321 20 43 68 69 6c 64 72 65 6e 20 6d 75 73 74 20 61   Children must a
33322 73 63 65 6e 64 2c 20 61 6e 64 20 69 66 20 21 70  scend, and if !p
33323 72 65 66 69 78 2c 20 62 6f 74 68 20 6d 75 73 74  refix, both must
33324 20 62 65 20 74 68 65 20 73 61 6d 65 2e 20 2a 2f   be the same. */
33325 0a 20 20 61 73 73 65 72 74 28 20 2a 70 69 45 6e  .  assert( *piEn
33326 64 43 68 69 6c 64 3e 3d 2a 70 69 53 74 61 72 74  dChild>=*piStart
33327 43 68 69 6c 64 20 29 3b 0a 20 20 61 73 73 65 72  Child );.  asser
33328 74 28 20 69 73 50 72 65 66 69 78 20 7c 7c 20 2a  t( isPrefix || *
33329 70 69 53 74 61 72 74 43 68 69 6c 64 3d 3d 2a 70  piStartChild==*p
3332a 69 45 6e 64 43 68 69 6c 64 20 29 3b 0a 7d 0a 0a  iEndChild );.}..
3332b 2f 2a 20 52 65 61 64 20 62 6c 6f 63 6b 20 61 74  /* Read block at
3332c 20 69 42 6c 6f 63 6b 69 64 20 61 6e 64 20 70 61   iBlockid and pa
3332d 73 73 20 69 74 20 77 69 74 68 20 6f 74 68 65 72  ss it with other
3332e 20 70 61 72 61 6d 73 20 74 6f 0a 2a 2a 20 67 65   params to.** ge
3332f 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
33330 69 6e 67 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ing()..*/.static
33331 20 69 6e 74 20 6c 6f 61 64 41 6e 64 47 65 74 43   int loadAndGetC
33332 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
33333 67 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  g(.  fulltext_vt
33334 61 62 20 2a 76 2c 0a 20 20 73 71 6c 69 74 65 5f  ab *v,.  sqlite_
33335 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 2c 0a  int64 iBlockid,.
33336 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
33337 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
33338 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
33339 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
3333a 53 74 61 72 74 43 68 69 6c 64 2c 20 73 71 6c 69  StartChild, sqli
3333b 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43  te_int64 *piEndC
3333c 68 69 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65  hild.){.  sqlite
3333d 33 5f 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c  3_stmt *s = NULL
3333e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
3333f 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 21  ssert( iBlockid!
33340 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
33341 70 54 65 72 6d 21 3d 4e 55 4c 4c 20 29 3b 0a 20  pTerm!=NULL );. 
33342 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 21 3d   assert( nTerm!=
33343 30 20 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  0 );        /* T
33344 4f 44 4f 28 73 68 65 73 73 29 20 57 68 79 20 6e  ODO(shess) Why n
33345 6f 74 20 61 6c 6c 6f 77 20 74 68 69 73 3f 20 2a  ot allow this? *
33346 2f 0a 20 20 61 73 73 65 72 74 28 20 70 69 53 74  /.  assert( piSt
33347 61 72 74 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29  artChild!=NULL )
33348 3b 0a 20 20 61 73 73 65 72 74 28 20 70 69 45 6e  ;.  assert( piEn
33349 64 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a  dChild!=NULL );.
3334a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  .  rc = sql_get_
3334b 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f  statement(v, BLO
3334c 43 4b 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20  CK_SELECT_STMT, 
3334d 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
3334e 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3334f 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
33350 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
33351 28 73 2c 20 31 2c 20 69 42 6c 6f 63 6b 69 64 29  (s, 1, iBlockid)
33352 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
33353 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
33354 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
33355 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
33356 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
33357 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  E ) return SQLIT
33358 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72  E_ERROR;.  if( r
33359 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
3335a 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 67 65  return rc;..  ge
3335b 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
3335c 69 6e 67 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ing(sqlite3_colu
3335d 6d 6e 5f 62 6c 6f 62 28 73 2c 20 30 29 2c 20 73  mn_blob(s, 0), s
3335e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
3335f 74 65 73 28 73 2c 20 30 29 2c 0a 20 20 20 20 20  tes(s, 0),.     
33360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33361 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c     pTerm, nTerm,
33362 20 69 73 50 72 65 66 69 78 2c 20 70 69 53 74 61   isPrefix, piSta
33363 72 74 43 68 69 6c 64 2c 20 70 69 45 6e 64 43 68  rtChild, piEndCh
33364 69 6c 64 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  ild);..  /* We e
33365 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
33366 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
33367 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
33368 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
33369 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
3336a 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
3336b 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
3336c 77 69 6c 6c 20 72 65 6d 61 69 6e 0a 20 20 20 2a  will remain.   *
3336d 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63   locked. */.  rc
3336e 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
3336f 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
33370 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
33371 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
33372 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33373 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72  _DONE ) return r
33374 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  c;..  return SQL
33375 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 72  ITE_OK;.}../* Tr
33376 61 76 65 72 73 65 20 74 68 65 20 74 72 65 65 20  averse the tree 
33377 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 70  represented by p
33378 44 61 74 61 5b 6e 44 61 74 61 5d 20 6c 6f 6f 6b  Data[nData] look
33379 69 6e 67 20 66 6f 72 0a 2a 2a 20 70 54 65 72 6d  ing for.** pTerm
3337a 5b 6e 54 65 72 6d 5d 2c 20 70 6c 61 63 69 6e 67  [nTerm], placing
3337b 20 69 74 73 20 64 6f 63 6c 69 73 74 20 69 6e 74   its doclist int
3337c 6f 20 2a 6f 75 74 2e 20 20 54 68 69 73 20 69 73  o *out.  This is
3337d 20 69 6e 74 65 72 6e 61 6c 20 74 6f 0a 2a 2a 20   internal to.** 
3337e 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20 74 6f  loadSegment() to
3337f 20 6d 61 6b 65 20 65 72 72 6f 72 2d 68 61 6e 64   make error-hand
33380 6c 69 6e 67 20 63 6c 65 61 6e 65 72 2e 0a 2a 2f  ling cleaner..*/
33381 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64  .static int load
33382 53 65 67 6d 65 6e 74 49 6e 74 28 66 75 6c 6c 74  SegmentInt(fullt
33383 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e  ext_vtab *v, con
33384 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
33385 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
33386 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33387 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
33388 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20  4 iLeavesEnd,.  
33389 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3338a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
3338b 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
3338c 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
3338d 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
3338e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 61                Da
3338f 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a  taBuffer *out){.
33390 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
33391 65 20 77 68 65 72 65 20 72 6f 6f 74 20 69 73 20  e where root is 
33392 61 20 6c 65 61 66 2e 20 2a 2f 0a 20 20 69 66 28  a leaf. */.  if(
33393 20 2a 70 44 61 74 61 3d 3d 27 5c 30 27 20 29 7b   *pData=='\0' ){
33394 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64  .    return load
33395 53 65 67 6d 65 6e 74 4c 65 61 66 28 76 2c 20 70  SegmentLeaf(v, p
33396 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65  Data, nData, pTe
33397 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
33398 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 7d 65 6c  fix, out);.  }el
33399 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a  se{.    int rc;.
3339a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
3339b 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45   iStartChild, iE
3339c 6e 64 43 68 69 6c 64 3b 0a 0a 20 20 20 20 2f 2a  ndChild;..    /*
3339d 20 50 72 6f 63 65 73 73 20 70 44 61 74 61 20 61   Process pData a
3339e 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
3339f 64 65 2c 20 74 68 65 6e 20 6c 6f 6f 70 20 64 6f  de, then loop do
333a0 77 6e 20 74 68 65 20 74 72 65 65 0a 20 20 20 20  wn the tree.    
333a1 2a 2a 20 75 6e 74 69 6c 20 77 65 20 66 69 6e 64  ** until we find
333a2 20 74 68 65 20 73 65 74 20 6f 66 20 6c 65 61 66   the set of leaf
333a3 20 6e 6f 64 65 73 20 74 6f 20 73 63 61 6e 20 66   nodes to scan f
333a4 6f 72 20 74 68 65 20 74 65 72 6d 2e 0a 20 20 20  or the term..   
333a5 20 2a 2f 0a 20 20 20 20 67 65 74 43 68 69 6c 64   */.    getChild
333a6 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 70 44  renContaining(pD
333a7 61 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65 72  ata, nData, pTer
333a8 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
333a9 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
333aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69                &i
333ab 53 74 61 72 74 43 68 69 6c 64 2c 20 26 69 45 6e  StartChild, &iEn
333ac 64 43 68 69 6c 64 29 3b 0a 20 20 20 20 77 68 69  dChild);.    whi
333ad 6c 65 28 20 69 53 74 61 72 74 43 68 69 6c 64 3e  le( iStartChild>
333ae 69 4c 65 61 76 65 73 45 6e 64 20 29 7b 0a 20 20  iLeavesEnd ){.  
333af 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
333b0 20 69 4e 65 78 74 53 74 61 72 74 2c 20 69 4e 65   iNextStart, iNe
333b1 78 74 45 6e 64 3b 0a 20 20 20 20 20 20 72 63 20  xtEnd;.      rc 
333b2 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c  = loadAndGetChil
333b3 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76  drenContaining(v
333b4 2c 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20 70  , iStartChild, p
333b5 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50  Term, nTerm, isP
333b6 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
333b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
333b9 69 4e 65 78 74 53 74 61 72 74 2c 20 26 69 4e 65  iNextStart, &iNe
333ba 78 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 69 66  xtEnd);.      if
333bb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
333bc 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
333bd 20 20 20 20 2f 2a 20 49 66 20 77 65 27 76 65 20      /* If we've 
333be 62 72 61 6e 63 68 65 64 2c 20 66 6f 6c 6c 6f 77  branched, follow
333bf 20 74 68 65 20 65 6e 64 20 62 72 61 6e 63 68 2c   the end branch,
333c0 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20 20 20 69   too. */.      i
333c1 66 28 20 69 53 74 61 72 74 43 68 69 6c 64 21 3d  f( iStartChild!=
333c2 69 45 6e 64 43 68 69 6c 64 20 29 7b 0a 20 20 20  iEndChild ){.   
333c3 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
333c4 34 20 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20 20  4 iDummy;.      
333c5 20 20 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65    rc = loadAndGe
333c6 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e  tChildrenContain
333c7 69 6e 67 28 76 2c 20 69 45 6e 64 43 68 69 6c 64  ing(v, iEndChild
333c8 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
333c9 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
333ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333cc 20 20 20 20 26 69 44 75 6d 6d 79 2c 20 26 69 4e      &iDummy, &iN
333cd 65 78 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 20  extEnd);.       
333ce 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
333cf 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
333d0 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61        }..      a
333d1 73 73 65 72 74 28 20 69 4e 65 78 74 53 74 61 72  ssert( iNextStar
333d2 74 3c 3d 69 4e 65 78 74 45 6e 64 20 29 3b 0a 20  t<=iNextEnd );. 
333d3 20 20 20 20 20 69 53 74 61 72 74 43 68 69 6c 64       iStartChild
333d4 20 3d 20 69 4e 65 78 74 53 74 61 72 74 3b 0a 20   = iNextStart;. 
333d5 20 20 20 20 20 69 45 6e 64 43 68 69 6c 64 20 3d       iEndChild =
333d6 20 69 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20 7d   iNextEnd;.    }
333d7 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53 74  .    assert( iSt
333d8 61 72 74 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65  artChild<=iLeave
333d9 73 45 6e 64 20 29 3b 0a 20 20 20 20 61 73 73 65  sEnd );.    asse
333da 72 74 28 20 69 45 6e 64 43 68 69 6c 64 3c 3d 69  rt( iEndChild<=i
333db 4c 65 61 76 65 73 45 6e 64 20 29 3b 0a 0a 20 20  LeavesEnd );..  
333dc 20 20 2f 2a 20 53 63 61 6e 20 74 68 72 6f 75 67    /* Scan throug
333dd 68 20 74 68 65 20 6c 65 61 66 20 73 65 67 6d 65  h the leaf segme
333de 6e 74 73 20 66 6f 72 20 64 6f 63 6c 69 73 74 73  nts for doclists
333df 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
333e0 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65  loadSegmentLeave
333e1 73 28 76 2c 20 69 53 74 61 72 74 43 68 69 6c 64  s(v, iStartChild
333e2 2c 20 69 45 6e 64 43 68 69 6c 64 2c 0a 20 20 20  , iEndChild,.   
333e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333e4 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c            pTerm,
333e5 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
333e6 2c 20 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  , out);.  }.}../
333e7 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65  * Call loadSegme
333e8 6e 74 49 6e 74 28 29 20 74 6f 20 63 6f 6c 6c 65  ntInt() to colle
333e9 63 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66  ct the doclist f
333ea 6f 72 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2c 20  or pTerm/nTerm, 
333eb 74 68 65 6e 0a 2a 2a 20 6d 65 72 67 65 20 69 74  then.** merge it
333ec 73 20 64 6f 63 6c 69 73 74 20 6f 76 65 72 20 2a  s doclist over *
333ed 6f 75 74 20 28 61 6e 79 20 64 75 70 6c 69 63 61  out (any duplica
333ee 74 65 20 64 6f 63 6c 69 73 74 73 20 72 65 61 64  te doclists read
333ef 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 65 67   from the.** seg
333f0 6d 65 6e 74 20 72 6f 6f 74 65 64 20 61 74 20 70  ment rooted at p
333f1 44 61 74 61 20 77 69 6c 6c 20 6f 76 65 72 77 72  Data will overwr
333f2 69 74 65 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75  ite those in *ou
333f3 74 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  t)..*/./* TODO(s
333f4 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20 63  hess) Consider c
333f5 68 61 6e 67 69 6e 67 20 74 68 69 73 20 74 6f 20  hanging this to 
333f6 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65  determine the de
333f7 70 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 65  pth of the.** le
333f8 61 76 65 73 20 75 73 69 6e 67 20 65 69 74 68 65  aves using eithe
333f9 72 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72  r the first char
333fa 61 63 74 65 72 73 20 6f 66 20 69 6e 74 65 72 69  acters of interi
333fb 6f 72 20 6e 6f 64 65 73 20 28 77 68 65 6e 0a 2a  or nodes (when.*
333fc 2a 20 3d 3d 31 2c 20 77 65 27 72 65 20 6f 6e 65  * ==1, we're one
333fd 20 6c 65 76 65 6c 20 61 62 6f 76 65 20 74 68 65   level above the
333fe 20 6c 65 61 76 65 73 29 2c 20 6f 72 20 74 68 65   leaves), or the
333ff 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
33400 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20   of.** the root 
33401 28 77 68 69 63 68 20 77 69 6c 6c 20 64 65 73 63  (which will desc
33402 72 69 62 65 20 74 68 65 20 68 65 69 67 68 74 20  ribe the height 
33403 6f 66 20 74 68 65 20 74 72 65 65 20 64 69 72 65  of the tree dire
33404 63 74 6c 79 29 2e 0a 2a 2a 20 45 69 74 68 65 72  ctly)..** Either
33405 20 66 65 65 6c 73 20 73 6f 6d 65 77 68 61 74 20   feels somewhat 
33406 74 72 69 63 6b 79 20 74 6f 20 6d 65 2e 0a 2a 2f  tricky to me..*/
33407 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
33408 54 68 65 20 63 75 72 72 65 6e 74 20 6d 65 72 67  The current merg
33409 65 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62  e is likely to b
3340a 65 20 73 6c 6f 77 20 66 6f 72 20 6c 61 72 67 65  e slow for large
3340b 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 20 28 74 68  .** doclists (th
3340c 6f 75 67 68 20 69 74 20 73 68 6f 75 6c 64 20 70  ough it should p
3340d 72 6f 63 65 73 73 20 66 72 6f 6d 20 6e 65 77 65  rocess from newe
3340e 73 74 2f 73 6d 61 6c 6c 65 73 74 20 74 6f 0a 2a  st/smallest to.*
3340f 2a 20 6f 6c 64 65 73 74 2f 6c 61 72 67 65 73 74  * oldest/largest
33410 2c 20 73 6f 20 69 74 20 6d 61 79 20 6e 6f 74 20  , so it may not 
33411 62 65 20 74 68 61 74 20 62 61 64 29 2e 20 20 49  be that bad).  I
33412 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 66 75  t might be usefu
33413 6c 20 74 6f 0a 2a 2a 20 6d 6f 64 69 66 79 20 74  l to.** modify t
33414 68 69 6e 67 73 20 74 6f 20 61 6c 6c 6f 77 20 66  hings to allow f
33415 6f 72 20 4e 2d 77 61 79 20 6d 65 72 67 69 6e 67  or N-way merging
33416 2e 20 20 54 68 69 73 20 63 6f 75 6c 64 20 65 69  .  This could ei
33417 74 68 65 72 20 62 65 0a 2a 2a 20 77 69 74 68 69  ther be.** withi
33418 6e 20 61 20 73 65 67 6d 65 6e 74 2c 20 77 69 74  n a segment, wit
33419 68 20 70 61 69 72 77 69 73 65 20 6d 65 72 67 65  h pairwise merge
3341a 73 20 61 63 72 6f 73 73 20 73 65 67 6d 65 6e 74  s across segment
3341b 73 2c 20 6f 72 20 61 63 72 6f 73 73 0a 2a 2a 20  s, or across.** 
3341c 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20  all segments at 
3341d 6f 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  once..*/.static 
3341e 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28  int loadSegment(
3341f 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
33420 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
33421 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
33422 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33423 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
33424 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a  t64 iLeavesEnd,.
33425 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33426 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
33427 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
33428 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
33429 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
3342a 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75            DataBu
3342b 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 44 61  ffer *out){.  Da
3342c 74 61 42 75 66 66 65 72 20 72 65 73 75 6c 74 3b  taBuffer result;
3342d 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
3342e 73 65 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b  sert( nData>1 );
3342f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65  ..  /* This code
33430 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65   should never be
33431 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66   called with buf
33432 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a  fered updates. *
33433 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e  /.  assert( v->n
33434 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b  PendingData<0 );
33435 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ..  dataBufferIn
33436 69 74 28 26 72 65 73 75 6c 74 2c 20 30 29 3b 0a  it(&result, 0);.
33437 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65    rc = loadSegme
33438 6e 74 49 6e 74 28 76 2c 20 70 44 61 74 61 2c 20  ntInt(v, pData, 
33439 6e 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e  nData, iLeavesEn
3343a 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
3343b 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20           pTerm, 
3343c 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c  nTerm, isPrefix,
3343d 20 26 72 65 73 75 6c 74 29 3b 0a 20 20 69 66 28   &result);.  if(
3343e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
3343f 26 20 72 65 73 75 6c 74 2e 6e 44 61 74 61 3e 30  & result.nData>0
33440 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 75 74 2d   ){.    if( out-
33441 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
33442 20 20 20 44 61 74 61 42 75 66 66 65 72 20 74 6d     DataBuffer tm
33443 70 20 3d 20 2a 6f 75 74 3b 0a 20 20 20 20 20 20  p = *out;.      
33444 2a 6f 75 74 20 3d 20 72 65 73 75 6c 74 3b 0a 20  *out = result;. 
33445 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 74 6d       result = tm
33446 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  p;.    }else{.  
33447 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 6d      DataBuffer m
33448 65 72 67 65 64 3b 0a 20 20 20 20 20 20 44 4c 52  erged;.      DLR
33449 65 61 64 65 72 20 72 65 61 64 65 72 73 5b 32 5d  eader readers[2]
3344a 3b 0a 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74  ;..      dlrInit
3344b 28 26 72 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c  (&readers[0], DL
3344c 5f 44 45 46 41 55 4c 54 2c 20 6f 75 74 2d 3e 70  _DEFAULT, out->p
3344d 44 61 74 61 2c 20 6f 75 74 2d 3e 6e 44 61 74 61  Data, out->nData
3344e 29 3b 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74  );.      dlrInit
3344f 28 26 72 65 61 64 65 72 73 5b 31 5d 2c 20 44 4c  (&readers[1], DL
33450 5f 44 45 46 41 55 4c 54 2c 20 72 65 73 75 6c 74  _DEFAULT, result
33451 2e 70 44 61 74 61 2c 20 72 65 73 75 6c 74 2e 6e  .pData, result.n
33452 44 61 74 61 29 3b 0a 20 20 20 20 20 20 64 61 74  Data);.      dat
33453 61 42 75 66 66 65 72 49 6e 69 74 28 26 6d 65 72  aBufferInit(&mer
33454 67 65 64 2c 20 6f 75 74 2d 3e 6e 44 61 74 61 2b  ged, out->nData+
33455 72 65 73 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20  result.nData);. 
33456 20 20 20 20 20 64 6f 63 4c 69 73 74 4d 65 72 67       docListMerg
33457 65 28 26 6d 65 72 67 65 64 2c 20 72 65 61 64 65  e(&merged, reade
33458 72 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 64 61  rs, 2);.      da
33459 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
3345a 6f 75 74 29 3b 0a 20 20 20 20 20 20 2a 6f 75 74  out);.      *out
3345b 20 3d 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20   = merged;.     
3345c 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61   dlrDestroy(&rea
3345d 64 65 72 73 5b 30 5d 29 3b 0a 20 20 20 20 20 20  ders[0]);.      
3345e 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61 64  dlrDestroy(&read
3345f 65 72 73 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  ers[1]);.    }. 
33460 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44   }.  dataBufferD
33461 65 73 74 72 6f 79 28 26 72 65 73 75 6c 74 29 3b  estroy(&result);
33462 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
33463 0a 2f 2a 20 53 63 61 6e 20 74 68 65 20 64 61 74  ./* Scan the dat
33464 61 62 61 73 65 20 61 6e 64 20 6d 65 72 67 65 20  abase and merge 
33465 74 6f 67 65 74 68 65 72 20 74 68 65 20 70 6f 73  together the pos
33466 74 69 6e 67 20 6c 69 73 74 73 20 66 6f 72 20 74  ting lists for t
33467 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 74 6f 20  he term.** into 
33468 2a 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  *out..*/.static 
33469 69 6e 74 20 74 65 72 6d 53 65 6c 65 63 74 28 0a  int termSelect(.
3346a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
3346b 2a 76 2c 20 0a 20 20 69 6e 74 20 69 43 6f 6c 75  *v, .  int iColu
3346c 6d 6e 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  mn,.  const char
3346d 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
3346e 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rm,             
3346f 2f 2a 20 54 65 72 6d 20 74 6f 20 71 75 65 72 79  /* Term to query
33470 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 73   for */.  int is
33471 50 72 65 66 69 78 2c 20 20 20 20 20 20 20 20 20  Prefix,         
33472 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33473 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
33474 61 20 70 72 65 66 69 78 20 73 65 61 72 63 68 20  a prefix search 
33475 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  */.  DocListType
33476 20 69 54 79 70 65 2c 20 0a 20 20 44 61 74 61 42   iType, .  DataB
33477 75 66 66 65 72 20 2a 6f 75 74 20 20 20 20 20 20  uffer *out      
33478 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33479 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65       /* Write re
3347a 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b  sults here */.){
3347b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 64 6f  .  DataBuffer do
3347c 63 6c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33  clist;.  sqlite3
3347d 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
3347e 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
3347f 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
33480 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54  _SELECT_ALL_STMT
33481 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
33482 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
33483 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68  urn rc;..  /* Th
33484 69 73 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e  is code should n
33485 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64 20 77  ever be called w
33486 69 74 68 20 62 75 66 66 65 72 65 64 20 75 70 64  ith buffered upd
33487 61 74 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ates. */.  asser
33488 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  t( v->nPendingDa
33489 74 61 3c 30 20 29 3b 0a 0a 20 20 64 61 74 61 42  ta<0 );..  dataB
3348a 75 66 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69  ufferInit(&docli
3348b 73 74 2c 20 30 29 3b 0a 20 20 64 61 74 61 42 75  st, 0);.  dataBu
3348c 66 66 65 72 49 6e 69 74 28 6f 75 74 2c 20 30 29  fferInit(out, 0)
3348d 3b 0a 0a 20 20 2f 2a 20 54 72 61 76 65 72 73 65  ;..  /* Traverse
3348e 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66 72   the segments fr
3348f 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77  om oldest to new
33490 65 73 74 20 73 6f 20 74 68 61 74 20 6e 65 77 65  est so that newe
33491 72 20 64 6f 63 6c 69 73 74 0a 20 20 2a 2a 20 65  r doclist.  ** e
33492 6c 65 6d 65 6e 74 73 20 66 6f 72 20 67 69 76 65  lements for give
33493 6e 20 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69  n docids overwri
33494 74 65 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74  te older element
33495 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  s..  */.  while(
33496 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73   (rc = sqlite3_s
33497 74 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f  tep(s))==SQLITE_
33498 52 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ROW ){.    const
33499 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 73   char *pData = s
3349a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
3349b 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f  ob(s, 2);.    co
3349c 6e 73 74 20 69 6e 74 20 6e 44 61 74 61 20 3d 20  nst int nData = 
3349d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
3349e 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20 20 20  ytes(s, 2);.    
3349f 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e 74  const sqlite_int
334a0 36 34 20 69 4c 65 61 76 65 73 45 6e 64 20 3d 20  64 iLeavesEnd = 
334a1 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
334a2 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20  nt64(s, 1);.    
334a3 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74  rc = loadSegment
334a4 28 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  (v, pData, nData
334a5 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c 20 70 54  , iLeavesEnd, pT
334a6 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
334a7 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20  efix,.          
334a8 20 20 20 20 20 20 20 20 20 20 20 26 64 6f 63 6c             &docl
334a9 69 73 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ist);.    if( rc
334aa 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
334ab 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 20 20 69 66  to err;.  }.  if
334ac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
334ad 45 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6f 63  E ){.    if( doc
334ae 6c 69 73 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b  list.nData!=0 ){
334af 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  .      /* TODO(s
334b0 68 65 73 73 29 20 54 68 65 20 6f 6c 64 20 74 65  hess) The old te
334b1 72 6d 5f 73 65 6c 65 63 74 5f 61 6c 6c 28 29 20  rm_select_all() 
334b2 63 6f 64 65 20 61 70 70 6c 69 65 64 20 74 68 65  code applied the
334b3 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a   column.      **
334b4 20 72 65 73 74 72 69 63 74 20 61 73 20 77 65 20   restrict as we 
334b5 6d 65 72 67 65 64 20 73 65 67 6d 65 6e 74 73 2c  merged segments,
334b6 20 6c 65 61 64 69 6e 67 20 74 6f 20 73 6d 61 6c   leading to smal
334b7 6c 65 72 20 62 75 66 66 65 72 73 2e 0a 20 20 20  ler buffers..   
334b8 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 70 72     ** This is pr
334b9 6f 62 61 62 6c 79 20 77 6f 72 74 68 77 68 69 6c  obably worthwhil
334ba 65 20 74 6f 20 62 72 69 6e 67 20 62 61 63 6b 2c  e to bring back,
334bb 20 6f 6e 63 65 20 74 68 65 20 6e 65 77 20 73 74   once the new st
334bc 6f 72 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 73  orage.      ** s
334bd 79 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64  ystem is checked
334be 20 69 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   in..      */.  
334bf 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d      if( iColumn=
334c0 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 69 43 6f  =v->nColumn) iCo
334c1 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20  lumn = -1;.     
334c2 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f   docListTrim(DL_
334c3 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74  DEFAULT, doclist
334c4 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e  .pData, doclist.
334c5 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
334c6 20 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e           iColumn
334c7 2c 20 69 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20  , iType, out);. 
334c8 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51     }.    rc = SQ
334c9 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 65  LITE_OK;.  }.. e
334ca 72 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72  rr:.  dataBuffer
334cb 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74  Destroy(&doclist
334cc 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
334cd 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
334ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334d1 2a 2a 2a 2a 2f 0a 2f 2a 20 55 73 65 64 20 74 6f  ****/./* Used to
334d2 20 68 6f 6c 64 20 68 61 73 68 74 61 62 6c 65 20   hold hashtable 
334d3 64 61 74 61 20 66 6f 72 20 73 6f 72 74 69 6e 67  data for sorting
334d4 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  . */.typedef str
334d5 75 63 74 20 54 65 72 6d 44 61 74 61 20 7b 0a 20  uct TermData {. 
334d6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
334d7 72 6d 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b  rm;.  int nTerm;
334d8 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a  .  DLCollector *
334d9 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 20 54 65  pCollector;.} Te
334da 72 6d 44 61 74 61 3b 0a 0a 2f 2a 20 4f 72 64 65  rmData;../* Orde
334db 72 73 20 54 65 72 6d 44 61 74 61 20 65 6c 65 6d  rs TermData elem
334dc 65 6e 74 73 20 69 6e 20 73 74 72 63 6d 70 20 66  ents in strcmp f
334dd 61 73 68 69 6f 6e 20 28 20 3c 30 20 66 6f 72 20  ashion ( <0 for 
334de 6c 65 73 73 2d 74 68 61 6e 2c 20 30 0a 2a 2a 20  less-than, 0.** 
334df 66 6f 72 20 65 71 75 61 6c 2c 20 3e 30 20 66 6f  for equal, >0 fo
334e0 72 20 67 72 65 61 74 65 72 2d 74 68 61 6e 29 2e  r greater-than).
334e1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
334e2 65 72 6d 44 61 74 61 43 6d 70 28 63 6f 6e 73 74  ermDataCmp(const
334e3 20 76 6f 69 64 20 2a 61 76 2c 20 63 6f 6e 73 74   void *av, const
334e4 20 76 6f 69 64 20 2a 62 76 29 7b 0a 20 20 63 6f   void *bv){.  co
334e5 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 61 20  nst TermData *a 
334e6 3d 20 28 63 6f 6e 73 74 20 54 65 72 6d 44 61 74  = (const TermDat
334e7 61 20 2a 29 61 76 3b 0a 20 20 63 6f 6e 73 74 20  a *)av;.  const 
334e8 54 65 72 6d 44 61 74 61 20 2a 62 20 3d 20 28 63  TermData *b = (c
334e9 6f 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29  onst TermData *)
334ea 62 76 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 61 2d  bv;.  int n = a-
334eb 3e 6e 54 65 72 6d 3c 62 2d 3e 6e 54 65 72 6d 20  >nTerm<b->nTerm 
334ec 3f 20 61 2d 3e 6e 54 65 72 6d 20 3a 20 62 2d 3e  ? a->nTerm : b->
334ed 6e 54 65 72 6d 3b 0a 20 20 69 6e 74 20 63 20 3d  nTerm;.  int c =
334ee 20 6d 65 6d 63 6d 70 28 61 2d 3e 70 54 65 72 6d   memcmp(a->pTerm
334ef 2c 20 62 2d 3e 70 54 65 72 6d 2c 20 6e 29 3b 0a  , b->pTerm, n);.
334f0 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65 74    if( c!=0 ) ret
334f1 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20  urn c;.  return 
334f2 61 2d 3e 6e 54 65 72 6d 2d 62 2d 3e 6e 54 65 72  a->nTerm-b->nTer
334f3 6d 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 70  m;.}../* Order p
334f4 54 65 72 6d 73 20 64 61 74 61 20 62 79 20 74 65  Terms data by te
334f5 72 6d 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61  rm, then write a
334f6 20 6e 65 77 20 6c 65 76 65 6c 20 30 20 73 65 67   new level 0 seg
334f7 6d 65 6e 74 20 75 73 69 6e 67 0a 2a 2a 20 4c 65  ment using.** Le
334f8 61 66 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61  afWriter..*/.sta
334f9 74 69 63 20 69 6e 74 20 77 72 69 74 65 5a 65 72  tic int writeZer
334fa 6f 53 65 67 6d 65 6e 74 28 66 75 6c 6c 74 65 78  oSegment(fulltex
334fb 74 5f 76 74 61 62 20 2a 76 2c 20 66 74 73 33 48  t_vtab *v, fts3H
334fc 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20 20  ash *pTerms){.  
334fd 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b  fts3HashElem *e;
334fe 0a 20 20 69 6e 74 20 69 64 78 2c 20 72 63 2c 20  .  int idx, rc, 
334ff 69 2c 20 6e 3b 0a 20 20 54 65 72 6d 44 61 74 61  i, n;.  TermData
33500 20 2a 70 44 61 74 61 3b 0a 20 20 4c 65 61 66 57   *pData;.  LeafW
33501 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20  riter writer;.  
33502 44 61 74 61 42 75 66 66 65 72 20 64 6c 3b 0a 0a  DataBuffer dl;..
33503 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74    /* Determine t
33504 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 61 74  he next index at
33505 20 6c 65 76 65 6c 20 30 2c 20 6d 65 72 67 69 6e   level 0, mergin
33506 67 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20  g as necessary. 
33507 2a 2f 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72  */.  rc = segdir
33508 4e 65 78 74 49 6e 64 65 78 28 76 2c 20 30 2c 20  NextIndex(v, 0, 
33509 26 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21  &idx);.  if( rc!
3350a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3350b 75 72 6e 20 72 63 3b 0a 0a 20 20 6e 20 3d 20 66  urn rc;..  n = f
3350c 74 73 33 48 61 73 68 43 6f 75 6e 74 28 70 54 65  ts3HashCount(pTe
3350d 72 6d 73 29 3b 0a 20 20 70 44 61 74 61 20 3d 20  rms);.  pData = 
3350e 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
3350f 2a 73 69 7a 65 6f 66 28 54 65 72 6d 44 61 74 61  *sizeof(TermData
33510 29 29 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30  ));..  for(i = 0
33511 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 46 69  , e = fts3HashFi
33512 72 73 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20  rst(pTerms); e; 
33513 69 2b 2b 2c 20 65 20 3d 20 66 74 73 33 48 61 73  i++, e = fts3Has
33514 68 4e 65 78 74 28 65 29 29 7b 0a 20 20 20 20 61  hNext(e)){.    a
33515 73 73 65 72 74 28 20 69 3c 6e 20 29 3b 0a 20 20  ssert( i<n );.  
33516 20 20 70 44 61 74 61 5b 69 5d 2e 70 54 65 72 6d    pData[i].pTerm
33517 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28 65   = fts3HashKey(e
33518 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e  );.    pData[i].
33519 6e 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68  nTerm = fts3Hash
3351a 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20  Keysize(e);.    
3351b 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63  pData[i].pCollec
3351c 74 6f 72 20 3d 20 66 74 73 33 48 61 73 68 44 61  tor = fts3HashDa
3351d 74 61 28 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ta(e);.  }.  ass
3351e 65 72 74 28 20 69 3d 3d 6e 20 29 3b 0a 0a 20 20  ert( i==n );..  
3351f 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 53  /* TODO(shess) S
33520 68 6f 75 6c 64 20 77 65 20 61 6c 6c 6f 77 20 75  hould we allow u
33521 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c  ser-defined coll
33522 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2c  ation sequences,
33523 0a 20 20 2a 2a 20 68 65 72 65 3f 20 20 49 20 74  .  ** here?  I t
33524 68 69 6e 6b 20 77 65 20 6f 6e 6c 79 20 6e 65 65  hink we only nee
33525 64 20 74 68 61 74 20 6f 6e 63 65 20 77 65 20 73  d that once we s
33526 75 70 70 6f 72 74 20 70 72 65 66 69 78 20 73 65  upport prefix se
33527 61 72 63 68 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  arches..  */.  i
33528 66 28 20 6e 3e 31 20 29 20 71 73 6f 72 74 28 70  f( n>1 ) qsort(p
33529 44 61 74 61 2c 20 6e 2c 20 73 69 7a 65 6f 66 28  Data, n, sizeof(
3352a 2a 70 44 61 74 61 29 2c 20 74 65 72 6d 44 61 74  *pData), termDat
3352b 61 43 6d 70 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44  aCmp);..  /* TOD
3352c 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74 6f  O(shess) Refacto
3352d 72 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e  r so that we can
3352e 20 77 72 69 74 65 20 64 69 72 65 63 74 6c 79 20   write directly 
3352f 74 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 20  to the segment. 
33530 20 2a 2a 20 44 61 74 61 42 75 66 66 65 72 2c 20   ** DataBuffer, 
33531 61 73 20 68 61 70 70 65 6e 73 20 66 6f 72 20 73  as happens for s
33532 65 67 6d 65 6e 74 20 6d 65 72 67 65 73 2e 0a 20  egment merges.. 
33533 20 2a 2f 0a 20 20 6c 65 61 66 57 72 69 74 65 72   */.  leafWriter
33534 49 6e 69 74 28 30 2c 20 69 64 78 2c 20 26 77 72  Init(0, idx, &wr
33535 69 74 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66  iter);.  dataBuf
33536 66 65 72 49 6e 69 74 28 26 64 6c 2c 20 30 29 3b  ferInit(&dl, 0);
33537 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b  .  for(i=0; i<n;
33538 20 69 2b 2b 29 7b 0a 20 20 20 20 64 61 74 61 42   i++){.    dataB
33539 75 66 66 65 72 52 65 73 65 74 28 26 64 6c 29 3b  ufferReset(&dl);
3353a 0a 20 20 20 20 64 6c 63 41 64 64 44 6f 63 6c 69  .    dlcAddDocli
3353b 73 74 28 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c  st(pData[i].pCol
3353c 6c 65 63 74 6f 72 2c 20 26 64 6c 29 3b 0a 20 20  lector, &dl);.  
3353d 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
3353e 72 53 74 65 70 28 76 2c 20 26 77 72 69 74 65 72  rStep(v, &writer
3353f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33540 20 20 20 20 20 20 20 20 20 20 70 44 61 74 61 5b            pData[
33541 69 5d 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b  i].pTerm, pData[
33542 69 5d 2e 6e 54 65 72 6d 2c 20 64 6c 2e 70 44 61  i].nTerm, dl.pDa
33543 74 61 2c 20 64 6c 2e 6e 44 61 74 61 29 3b 0a 20  ta, dl.nData);. 
33544 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
33545 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
33546 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6c 65 61 66  .  }.  rc = leaf
33547 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76  WriterFinalize(v
33548 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 65 72  , &writer);.. er
33549 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  r:.  dataBufferD
3354a 65 73 74 72 6f 79 28 26 64 6c 29 3b 0a 20 20 73  estroy(&dl);.  s
3354b 71 6c 69 74 65 33 5f 66 72 65 65 28 70 44 61 74  qlite3_free(pDat
3354c 61 29 3b 0a 20 20 6c 65 61 66 57 72 69 74 65 72  a);.  leafWriter
3354d 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
3354e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
3354f 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54  ../* If pendingT
33550 65 72 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66  erms has data, f
33551 72 65 65 20 69 74 2e 20 2a 2f 0a 73 74 61 74 69  ree it. */.stati
33552 63 20 69 6e 74 20 63 6c 65 61 72 50 65 6e 64 69  c int clearPendi
33553 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
33554 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 66 28  _vtab *v){.  if(
33555 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
33556 3e 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 48  >=0 ){.    fts3H
33557 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 20 20  ashElem *e;.    
33558 66 6f 72 28 65 3d 66 74 73 33 48 61 73 68 46 69  for(e=fts3HashFi
33559 72 73 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54  rst(&v->pendingT
3355a 65 72 6d 73 29 3b 20 65 3b 20 65 3d 66 74 73 33  erms); e; e=fts3
3355b 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20 20  HashNext(e)){.  
3355c 20 20 20 20 64 6c 63 44 65 6c 65 74 65 28 66 74      dlcDelete(ft
3355d 73 33 48 61 73 68 44 61 74 61 28 65 29 29 3b 0a  s3HashData(e));.
3355e 20 20 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61      }.    fts3Ha
3355f 73 68 43 6c 65 61 72 28 26 76 2d 3e 70 65 6e 64  shClear(&v->pend
33560 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20 20 20 76  ingTerms);.    v
33561 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d  ->nPendingData =
33562 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   -1;.  }.  retur
33563 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
33564 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72  /* If pendingTer
33565 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66 6c 75  ms has data, flu
33566 73 68 20 69 74 20 74 6f 20 61 20 6c 65 76 65 6c  sh it to a level
33567 2d 7a 65 72 6f 20 73 65 67 6d 65 6e 74 2c 20 61  -zero segment, a
33568 6e 64 0a 2a 2a 20 66 72 65 65 20 69 74 2e 0a 2a  nd.** free it..*
33569 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75  /.static int flu
3356a 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  shPendingTerms(f
3356b 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
3356c 7b 0a 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64  {.  if( v->nPend
3356d 69 6e 67 44 61 74 61 3e 3d 30 20 29 7b 0a 20 20  ingData>=0 ){.  
3356e 20 20 69 6e 74 20 72 63 20 3d 20 77 72 69 74 65    int rc = write
3356f 5a 65 72 6f 53 65 67 6d 65 6e 74 28 76 2c 20 26  ZeroSegment(v, &
33570 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29  v->pendingTerms)
33571 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
33572 4c 49 54 45 5f 4f 4b 20 29 20 63 6c 65 61 72 50  LITE_OK ) clearP
33573 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
33574 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
33575 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
33576 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20  TE_OK;.}../* If 
33577 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20  pendingTerms is 
33578 22 74 6f 6f 20 62 69 67 22 2c 20 6f 72 20 64 6f  "too big", or do
33579 63 69 64 20 69 73 20 6f 75 74 20 6f 66 20 6f 72  cid is out of or
3357a 64 65 72 2c 20 66 6c 75 73 68 20 69 74 2e 0a 2a  der, flush it..*
3357b 2a 20 52 65 67 61 72 64 6c 65 73 73 2c 20 62 65  * Regardless, be
3357c 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 70 65   certain that pe
3357d 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20 69 6e  ndingTerms is in
3357e 69 74 69 61 6c 69 7a 65 64 20 66 6f 72 20 75 73  itialized for us
3357f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
33580 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d   initPendingTerm
33581 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
33582 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
33583 20 69 44 6f 63 69 64 29 7b 0a 20 20 2f 2a 20 54   iDocid){.  /* T
33584 4f 44 4f 28 73 68 65 73 73 29 20 45 78 70 6c 6f  ODO(shess) Explo
33585 72 65 20 77 68 65 74 68 65 72 20 70 61 72 74 69  re whether parti
33586 61 6c 6c 79 20 66 6c 75 73 68 69 6e 67 20 74 68  ally flushing th
33587 65 20 62 75 66 66 65 72 20 6f 6e 0a 20 20 2a 2a  e buffer on.  **
33588 20 66 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f   forced-flush wo
33589 75 6c 64 20 70 72 6f 76 69 64 65 20 62 65 74 74  uld provide bett
3358a 65 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20  er performance. 
3358b 20 49 20 73 75 73 70 65 63 74 20 74 68 61 74 20   I suspect that 
3358c 69 66 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72  if.  ** we order
3358d 65 64 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20  ed the doclists 
3358e 62 79 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73  by size and flus
3358f 68 65 64 20 74 68 65 20 6c 61 72 67 65 73 74 20  hed the largest 
33590 75 6e 74 69 6c 20 74 68 65 0a 20 20 2a 2a 20 62  until the.  ** b
33591 75 66 66 65 72 20 77 61 73 20 68 61 6c 66 20 65  uffer was half e
33592 6d 70 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64  mpty, that would
33593 20 6c 65 74 20 74 68 65 20 6c 65 73 73 20 66 72   let the less fr
33594 65 71 75 65 6e 74 20 74 65 72 6d 73 0a 20 20 2a  equent terms.  *
33595 2a 20 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67 65  * generate longe
33596 72 20 64 6f 63 6c 69 73 74 73 2e 0a 20 20 2a 2f  r doclists..  */
33597 0a 20 20 69 66 28 20 69 44 6f 63 69 64 3c 3d 76  .  if( iDocid<=v
33598 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20  ->iPrevDocid || 
33599 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e  v->nPendingData>
3359a 6b 50 65 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c  kPendingThreshol
3359b 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  d ){.    int rc 
3359c 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  = flushPendingTe
3359d 72 6d 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20  rms(v);.    if( 
3359e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3359f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
335a0 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67   if( v->nPending
335a1 44 61 74 61 3c 30 20 29 7b 0a 20 20 20 20 66 74  Data<0 ){.    ft
335a2 73 33 48 61 73 68 49 6e 69 74 28 26 76 2d 3e 70  s3HashInit(&v->p
335a3 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 46 54 53  endingTerms, FTS
335a4 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31  3_HASH_STRING, 1
335a5 29 3b 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69  );.    v->nPendi
335a6 6e 67 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 0a  ngData = 0;.  }.
335a7 20 20 76 2d 3e 69 50 72 65 76 44 6f 63 69 64 20    v->iPrevDocid 
335a8 3d 20 69 44 6f 63 69 64 3b 0a 20 20 72 65 74 75  = iDocid;.  retu
335a9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
335aa 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ./* This functio
335ab 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
335ac 20 78 55 70 64 61 74 65 20 63 61 6c 6c 62 61 63   xUpdate callbac
335ad 6b 3b 20 69 74 20 69 73 20 74 68 65 20 74 6f 70  k; it is the top
335ae 2d 6c 65 76 65 6c 20 65 6e 74 72 79 0a 20 2a 20  -level entry. * 
335af 70 6f 69 6e 74 20 66 6f 72 20 69 6e 73 65 72 74  point for insert
335b0 69 6e 67 2c 20 64 65 6c 65 74 69 6e 67 20 6f 72  ing, deleting or
335b1 20 75 70 64 61 74 69 6e 67 20 61 20 72 6f 77 20   updating a row 
335b2 69 6e 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 74  in a full-text t
335b3 61 62 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20  able. */.static 
335b4 69 6e 74 20 66 75 6c 6c 74 65 78 74 55 70 64 61  int fulltextUpda
335b5 74 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  te(sqlite3_vtab 
335b6 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67  *pVtab, int nArg
335b7 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
335b8 2a 2a 70 70 41 72 67 2c 0a 20 20 20 20 20 20 20  **ppArg,.       
335b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335ba 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
335bb 2a 70 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c  *pRowid){.  full
335bc 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28  text_vtab *v = (
335bd 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
335be 20 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63   pVtab;.  int rc
335bf 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ;..  FTSTRACE(("
335c0 46 54 53 33 20 55 70 64 61 74 65 20 25 70 5c 6e  FTS3 Update %p\n
335c1 22 2c 20 70 56 74 61 62 29 29 3b 0a 0a 20 20 69  ", pVtab));..  i
335c2 66 28 20 6e 41 72 67 3c 32 20 29 7b 0a 20 20 20  f( nArg<2 ){.   
335c3 20 72 63 20 3d 20 69 6e 64 65 78 5f 64 65 6c 65   rc = index_dele
335c4 74 65 28 76 2c 20 73 71 6c 69 74 65 33 5f 76 61  te(v, sqlite3_va
335c5 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b  lue_int64(ppArg[
335c6 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63  0]));.    if( rc
335c7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
335c8 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 6a 75       /* If we ju
335c9 73 74 20 64 65 6c 65 74 65 64 20 74 68 65 20 6c  st deleted the l
335ca 61 73 74 20 72 6f 77 20 69 6e 20 74 68 65 20 74  ast row in the t
335cb 61 62 6c 65 2c 20 63 6c 65 61 72 20 6f 75 74 20  able, clear out 
335cc 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64  the.      ** ind
335cd 65 78 20 64 61 74 61 2e 0a 20 20 20 20 20 20 2a  ex data..      *
335ce 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e  /.      rc = con
335cf 74 65 6e 74 5f 65 78 69 73 74 73 28 76 29 3b 0a  tent_exists(v);.
335d0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
335d1 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
335d2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
335d3 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  OK;.      }else 
335d4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
335d5 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ONE ){.        /
335d6 2a 20 43 6c 65 61 72 20 74 68 65 20 70 65 6e 64  * Clear the pend
335d7 69 6e 67 20 74 65 72 6d 73 20 73 6f 20 77 65 20  ing terms so we 
335d8 64 6f 6e 27 74 20 66 6c 75 73 68 20 61 20 75 73  don't flush a us
335d9 65 6c 65 73 73 20 6c 65 76 65 6c 2d 30 0a 20 20  eless level-0.  
335da 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74        ** segment
335db 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
335dc 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20  ction closes..  
335dd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
335de 20 72 63 20 3d 20 63 6c 65 61 72 50 65 6e 64 69   rc = clearPendi
335df 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20  ngTerms(v);.    
335e0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
335e1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
335e2 20 20 20 72 63 20 3d 20 73 65 67 64 69 72 5f 64     rc = segdir_d
335e3 65 6c 65 74 65 5f 61 6c 6c 28 76 29 3b 0a 20 20  elete_all(v);.  
335e4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
335e5 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 69      }.  } else i
335e6 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
335e7 5f 74 79 70 65 28 70 70 41 72 67 5b 30 5d 29 20  _type(ppArg[0]) 
335e8 21 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  != SQLITE_NULL )
335e9 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 75 70 64 61  {.    /* An upda
335ea 74 65 3a 0a 20 20 20 20 20 2a 20 70 70 41 72 67  te:.     * ppArg
335eb 5b 30 5d 20 3d 20 6f 6c 64 20 72 6f 77 69 64 0a  [0] = old rowid.
335ec 20 20 20 20 20 2a 20 70 70 41 72 67 5b 31 5d 20       * ppArg[1] 
335ed 3d 20 6e 65 77 20 72 6f 77 69 64 0a 20 20 20 20  = new rowid.    
335ee 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d   * ppArg[2..2+v-
335ef 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61  >nColumn-1] = va
335f0 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70 41 72  lues.     * ppAr
335f1 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20  g[2+v->nColumn] 
335f2 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61 67 69  = value for magi
335f3 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e  c column (we ign
335f4 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20 20 2a  ore this).     *
335f5 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
335f6 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66  umn+1] = value f
335f7 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f  or docid.     */
335f8 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
335f9 34 20 72 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  4 rowid = sqlite
335fa 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70  3_value_int64(pp
335fb 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  Arg[0]);.    if(
335fc 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
335fd 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20 21 3d  ype(ppArg[1]) !=
335fe 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20   SQLITE_INTEGER 
335ff 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ||.        sqlit
33600 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70  e3_value_int64(p
33601 70 41 72 67 5b 31 5d 29 20 21 3d 20 72 6f 77 69  pArg[1]) != rowi
33602 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  d ){.      rc = 
33603 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f  SQLITE_ERROR;  /
33604 2a 20 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77  * we don't allow
33605 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72 6f   changing the ro
33606 77 69 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65  wid */.    }else
33607 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
33608 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b 32 2b  ue_type(ppArg[2+
33609 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21  v->nColumn+1]) !
3360a 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
3360b 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
3360c 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
3360d 69 6e 74 36 34 28 70 70 41 72 67 5b 32 2b 76 2d  int64(ppArg[2+v-
3360e 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20  >nColumn+1]) != 
3360f 72 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72  rowid ){.      r
33610 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
33611 3b 20 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20 61  ;  /* we don't a
33612 6c 6c 6f 77 20 63 68 61 6e 67 69 6e 67 20 74 68  llow changing th
33613 65 20 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 7d  e docid */.    }
33614 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
33615 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e  rt( nArg==2+v->n
33616 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 20  Column+2);.     
33617 20 72 63 20 3d 20 69 6e 64 65 78 5f 75 70 64 61   rc = index_upda
33618 74 65 28 76 2c 20 72 6f 77 69 64 2c 20 26 70 70  te(v, rowid, &pp
33619 41 72 67 5b 32 5d 29 3b 0a 20 20 20 20 7d 0a 20  Arg[2]);.    }. 
3361a 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a   } else {.    /*
3361b 20 41 6e 20 69 6e 73 65 72 74 3a 0a 20 20 20 20   An insert:.    
3361c 20 2a 20 70 70 41 72 67 5b 31 5d 20 3d 20 72 65   * ppArg[1] = re
3361d 71 75 65 73 74 65 64 20 72 6f 77 69 64 0a 20 20  quested rowid.  
3361e 20 20 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b     * ppArg[2..2+
3361f 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20  v->nColumn-1] = 
33620 76 61 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70  values.     * pp
33621 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e  Arg[2+v->nColumn
33622 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61  ] = value for ma
33623 67 69 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69  gic column (we i
33624 67 6e 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20  gnore this).    
33625 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43   * ppArg[2+v->nC
33626 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65  olumn+1] = value
33627 20 66 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20   for docid.     
33628 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  */.    sqlite3_v
33629 61 6c 75 65 20 2a 70 52 65 71 75 65 73 74 44 6f  alue *pRequestDo
3362a 63 69 64 20 3d 20 70 70 41 72 67 5b 32 2b 76 2d  cid = ppArg[2+v-
3362b 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b 0a 20 20 20  >nColumn+1];.   
3362c 20 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32   assert( nArg==2
3362d 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a  +v->nColumn+2);.
3362e 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e      if( SQLITE_N
3362f 55 4c 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f 76  ULL != sqlite3_v
33630 61 6c 75 65 5f 74 79 70 65 28 70 52 65 71 75 65  alue_type(pReque
33631 73 74 44 6f 63 69 64 29 20 26 26 0a 20 20 20 20  stDocid) &&.    
33632 20 20 20 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20      SQLITE_NULL 
33633 21 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  != sqlite3_value
33634 5f 74 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20  _type(ppArg[1]) 
33635 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f  ){.      /* TODO
33636 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72  (shess) Consider
33637 20 61 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 74   allowing this t
33638 6f 20 77 6f 72 6b 20 69 66 20 74 68 65 20 76 61  o work if the va
33639 6c 75 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a  lues are.      *
3363a 2a 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 49 27  * identical.  I'
3363b 6d 20 69 6e 63 6c 69 6e 65 64 20 74 6f 20 64 69  m inclined to di
3363c 73 63 6f 75 72 61 67 65 20 74 68 61 74 20 75 73  scourage that us
3363d 61 67 65 2c 20 74 68 6f 75 67 68 2c 0a 20 20 20  age, though,.   
3363e 20 20 20 2a 2a 20 67 69 76 65 6e 20 74 68 61 74     ** given that
3363f 20 62 6f 74 68 20 72 6f 77 69 64 20 61 6e 64 20   both rowid and 
33640 64 6f 63 69 64 20 61 72 65 20 73 70 65 63 69 61  docid are specia
33641 6c 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 65 74 74  l columns.  Bett
33642 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 6f 75 6c  er.      ** woul
33643 64 20 62 65 20 74 6f 20 64 65 66 69 6e 65 20 6f  d be to define o
33644 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  ne or the other 
33645 61 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 77  as the default w
33646 69 6e 6e 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20  inner,.      ** 
33647 62 75 74 20 73 68 6f 75 6c 64 20 69 74 20 62 65  but should it be
33648 20 66 74 73 33 2d 63 65 6e 74 72 69 63 20 28 64   fts3-centric (d
33649 6f 63 69 64 29 20 6f 72 20 53 51 4c 69 74 65 2d  ocid) or SQLite-
3364a 63 65 6e 74 72 69 63 0a 20 20 20 20 20 20 2a 2a  centric.      **
3364b 20 28 72 6f 77 69 64 29 3f 0a 20 20 20 20 20 20   (rowid)?.      
3364c 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  */.      rc = SQ
3364d 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
3364e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
3364f 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 3d 3d 20   SQLITE_NULL == 
33650 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
33651 70 65 28 70 52 65 71 75 65 73 74 44 6f 63 69 64  pe(pRequestDocid
33652 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65  ) ){.        pRe
33653 71 75 65 73 74 44 6f 63 69 64 20 3d 20 70 70 41  questDocid = ppA
33654 72 67 5b 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  rg[1];.      }. 
33655 20 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f       rc = index_
33656 69 6e 73 65 72 74 28 76 2c 20 70 52 65 71 75 65  insert(v, pReque
33657 73 74 44 6f 63 69 64 2c 20 26 70 70 41 72 67 5b  stDocid, &ppArg[
33658 32 5d 2c 20 70 52 6f 77 69 64 29 3b 0a 20 20 20  2], pRowid);.   
33659 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
3365a 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   rc;.}..static i
3365b 6e 74 20 66 75 6c 6c 74 65 78 74 53 79 6e 63 28  nt fulltextSync(
3365c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
3365d 74 61 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45  tab){.  FTSTRACE
3365e 28 28 22 46 54 53 33 20 78 53 79 6e 63 28 29 5c  (("FTS3 xSync()\
3365f 6e 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 66  n"));.  return f
33660 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73  lushPendingTerms
33661 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  ((fulltext_vtab 
33662 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a 73 74 61  *)pVtab);.}..sta
33663 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
33664 42 65 67 69 6e 28 73 71 6c 69 74 65 33 5f 76 74  Begin(sqlite3_vt
33665 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75  ab *pVtab){.  fu
33666 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
33667 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20   (fulltext_vtab 
33668 2a 29 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54  *) pVtab;.  FTST
33669 52 41 43 45 28 28 22 46 54 53 33 20 78 42 65 67  RACE(("FTS3 xBeg
3366a 69 6e 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a  in()\n"));..  /*
3366b 20 41 6e 79 20 62 75 66 66 65 72 65 64 20 75 70   Any buffered up
3366c 64 61 74 65 73 20 73 68 6f 75 6c 64 20 68 61 76  dates should hav
3366d 65 20 62 65 65 6e 20 63 6c 65 61 72 65 64 20 62  e been cleared b
3366e 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 20  y the previous. 
3366f 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
33670 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
33671 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  v->nPendingData<
33672 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c  0 );.  return cl
33673 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  earPendingTerms(
33674 76 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  v);.}..static in
33675 74 20 66 75 6c 6c 74 65 78 74 43 6f 6d 6d 69 74  t fulltextCommit
33676 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
33677 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78  Vtab){.  fulltex
33678 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c  t_vtab *v = (ful
33679 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56  ltext_vtab *) pV
3367a 74 61 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28  tab;.  FTSTRACE(
3367b 28 22 46 54 53 33 20 78 43 6f 6d 6d 69 74 28 29  ("FTS3 xCommit()
3367c 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 42 75 66  \n"));..  /* Buf
3367d 66 65 72 65 64 20 75 70 64 61 74 65 73 20 73 68  fered updates sh
3367e 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63  ould have been c
3367f 6c 65 61 72 65 64 20 62 79 20 66 75 6c 6c 74 65  leared by fullte
33680 78 74 53 79 6e 63 28 29 2e 20 2a 2f 0a 20 20 61  xtSync(). */.  a
33681 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69  ssert( v->nPendi
33682 6e 67 44 61 74 61 3c 30 20 29 3b 0a 20 20 72 65  ngData<0 );.  re
33683 74 75 72 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e  turn clearPendin
33684 67 54 65 72 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74  gTerms(v);.}..st
33685 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
33686 74 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65  tRollback(sqlite
33687 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
33688 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
33689 33 20 78 52 6f 6c 6c 62 61 63 6b 28 29 5c 6e 22  3 xRollback()\n"
3368a 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c 65  ));.  return cle
3368b 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 28  arPendingTerms((
3368c 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
3368d 70 56 74 61 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pVtab);.}../*.**
3368e 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
3368f 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 28 29  of the snippet()
33690 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54   function for FT
33691 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  S3.*/.static voi
33692 64 20 73 6e 69 70 70 65 74 46 75 6e 63 28 0a 20  d snippetFunc(. 
33693 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
33694 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e   *pContext,.  in
33695 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
33696 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
33697 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  {.  fulltext_cur
33698 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20  sor *pCursor;.  
33699 69 66 28 20 61 72 67 63 3c 31 20 29 20 72 65 74  if( argc<1 ) ret
3369a 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  urn;.  if( sqlit
3369b 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
3369c 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42  gv[0])!=SQLITE_B
3369d 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c  LOB ||.      sql
3369e 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
3369f 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f  (argv[0])!=sizeo
336a0 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20  f(pCursor) ){.  
336a1 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
336a2 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  _error(pContext,
336a3 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20   "illegal first 
336a4 61 72 67 75 6d 65 6e 74 20 74 6f 20 68 74 6d 6c  argument to html
336a5 5f 73 6e 69 70 70 65 74 22 2c 2d 31 29 3b 0a 20  _snippet",-1);. 
336a6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73   }else{.    cons
336a7 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d  t char *zStart =
336a8 20 22 3c 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73   "<b>";.    cons
336a9 74 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 22  t char *zEnd = "
336aa 3c 2f 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73 74  </b>";.    const
336ab 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73   char *zEllipsis
336ac 20 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b   = "<b>...</b>";
336ad 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75  .    memcpy(&pCu
336ae 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61  rsor, sqlite3_va
336af 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d  lue_blob(argv[0]
336b0 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  ), sizeof(pCurso
336b1 72 29 29 3b 0a 20 20 20 20 69 66 28 20 61 72 67  r));.    if( arg
336b2 63 3e 3d 32 20 29 7b 0a 20 20 20 20 20 20 7a 53  c>=2 ){.      zS
336b3 74 61 72 74 20 3d 20 28 63 6f 6e 73 74 20 63 68  tart = (const ch
336b4 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
336b5 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
336b6 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e  .      if( argc>
336b7 3d 33 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 45  =3 ){.        zE
336b8 6e 64 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  nd = (const char
336b9 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
336ba 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  text(argv[2]);. 
336bb 20 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3e         if( argc>
336bc 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =4 ){.          
336bd 7a 45 6c 6c 69 70 73 69 73 20 3d 20 28 63 6f 6e  zEllipsis = (con
336be 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33  st char*)sqlite3
336bf 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
336c0 5b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  [3]);.        }.
336c1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
336c2 20 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73    snippetAllOffs
336c3 65 74 73 28 70 43 75 72 73 6f 72 29 3b 0a 20 20  ets(pCursor);.  
336c4 20 20 73 6e 69 70 70 65 74 54 65 78 74 28 70 43    snippetText(pC
336c5 75 72 73 6f 72 2c 20 7a 53 74 61 72 74 2c 20 7a  ursor, zStart, z
336c6 45 6e 64 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b  End, zEllipsis);
336c7 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
336c8 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
336c9 74 2c 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70  t, pCursor->snip
336ca 70 65 74 2e 7a 53 6e 69 70 70 65 74 2c 0a 20 20  pet.zSnippet,.  
336cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336cc 20 20 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 73        pCursor->s
336cd 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70 65 74 2c  nippet.nSnippet,
336ce 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
336cf 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
336d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
336d1 74 68 65 20 6f 66 66 73 65 74 73 28 29 20 66 75  the offsets() fu
336d2 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a  nction for FTS3.
336d3 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
336d4 6e 69 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e  nippetOffsetsFun
336d5 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
336d6 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a  text *pContext,.
336d7 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
336d8 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
336d9 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  gv.){.  fulltext
336da 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
336db 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 29  ;.  if( argc<1 )
336dc 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73   return;.  if( s
336dd 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
336de 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49  e(argv[0])!=SQLI
336df 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20  TE_BLOB ||.     
336e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
336e1 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73  ytes(argv[0])!=s
336e2 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29  izeof(pCursor) )
336e3 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
336e4 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74  sult_error(pCont
336e5 65 78 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69  ext, "illegal fi
336e6 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
336e7 6f 66 66 73 65 74 73 22 2c 2d 31 29 3b 0a 20 20  offsets",-1);.  
336e8 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70  }else{.    memcp
336e9 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69  y(&pCursor, sqli
336ea 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
336eb 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28  rgv[0]), sizeof(
336ec 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 73  pCursor));.    s
336ed 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73  nippetAllOffsets
336ee 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73  (pCursor);.    s
336ef 6e 69 70 70 65 74 4f 66 66 73 65 74 54 65 78 74  nippetOffsetText
336f0 28 26 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70  (&pCursor->snipp
336f1 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  et);.    sqlite3
336f2 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
336f3 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
336f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
336f5 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
336f6 7a 4f 66 66 73 65 74 2c 20 70 43 75 72 73 6f 72  zOffset, pCursor
336f7 2d 3e 73 6e 69 70 70 65 74 2e 6e 4f 66 66 73 65  ->snippet.nOffse
336f8 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
336f9 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
336fa 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d  E_STATIC);.  }.}
336fb 0a 0a 2f 2a 20 4f 70 74 4c 65 61 76 65 73 52 65  ../* OptLeavesRe
336fc 61 64 65 72 20 69 73 20 6e 65 61 72 6c 79 20 69  ader is nearly i
336fd 64 65 6e 74 69 63 61 6c 20 74 6f 20 4c 65 61 76  dentical to Leav
336fe 65 73 52 65 61 64 65 72 2c 20 65 78 63 65 70 74  esReader, except
336ff 20 74 68 61 74 0a 2a 2a 20 77 68 65 72 65 20 4c   that.** where L
33700 65 61 76 65 73 52 65 61 64 65 72 20 69 73 20 67  eavesReader is g
33701 65 61 72 65 64 20 74 6f 77 61 72 64 73 20 74 68  eared towards th
33702 65 20 6d 65 72 67 69 6e 67 20 6f 66 20 63 6f 6d  e merging of com
33703 70 6c 65 74 65 0a 2a 2a 20 73 65 67 6d 65 6e 74  plete.** segment
33704 20 6c 65 76 65 6c 73 20 28 77 69 74 68 20 65 78   levels (with ex
33705 61 63 74 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e  actly MERGE_COUN
33706 54 20 73 65 67 6d 65 6e 74 73 29 2c 20 4f 70 74  T segments), Opt
33707 4c 65 61 76 65 73 52 65 61 64 65 72 0a 2a 2a 20  LeavesReader.** 
33708 69 73 20 67 65 61 72 65 64 20 74 6f 77 61 72 64  is geared toward
33709 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
3370a 20 6f 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   of the optimize
3370b 28 29 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 64  () function, and
3370c 0a 2a 2a 20 63 61 6e 20 6d 65 72 67 65 20 61 6c  .** can merge al
3370d 6c 20 73 65 67 6d 65 6e 74 73 20 73 69 6d 75 6c  l segments simul
3370e 74 61 6e 65 6f 75 73 6c 79 2e 20 20 54 68 69 73  taneously.  This
3370f 20 76 65 72 73 69 6f 6e 20 6d 61 79 20 62 65 0a   version may be.
33710 2a 2a 20 73 6f 6d 65 77 68 61 74 20 6c 65 73 73  ** somewhat less
33711 20 65 66 66 69 63 69 65 6e 74 20 74 68 61 6e 20   efficient than 
33712 4c 65 61 76 65 73 52 65 61 64 65 72 20 62 65 63  LeavesReader bec
33713 61 75 73 65 20 69 74 20 6d 65 72 67 65 73 20 69  ause it merges i
33714 6e 74 6f 20 61 6e 0a 2a 2a 20 61 63 63 75 6d 75  nto an.** accumu
33715 6c 61 74 6f 72 20 72 61 74 68 65 72 20 74 68 61  lator rather tha
33716 6e 20 64 6f 69 6e 67 20 61 6e 20 4e 2d 77 61 79  n doing an N-way
33717 20 6d 65 72 67 65 2c 20 62 75 74 20 73 69 6e 63   merge, but sinc
33718 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 69 7a  e segment.** siz
33719 65 20 67 72 6f 77 73 20 65 78 70 6f 6e 65 6e 74  e grows exponent
3371a 69 61 6c 6c 79 20 28 73 6f 20 73 65 67 6d 65 6e  ially (so segmen
3371b 74 20 63 6f 75 6e 74 20 6c 6f 67 72 69 74 68 6d  t count logrithm
3371c 69 63 61 6c 6c 79 29 20 74 68 69 73 20 69 73 0a  ically) this is.
3371d 2a 2a 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20  ** probably not 
3371e 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 70 72 6f  an immediate pro
3371f 62 6c 65 6d 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  blem..*/./* TODO
33720 28 73 68 65 73 73 29 3a 20 50 72 6f 76 65 20 74  (shess): Prove t
33721 68 61 74 20 61 73 73 65 72 74 69 6f 6e 2c 20 6f  hat assertion, o
33722 72 20 65 78 74 65 6e 64 20 74 68 65 20 6d 65 72  r extend the mer
33723 67 65 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 6d 65  ge code to.** me
33724 72 67 65 20 74 72 65 65 20 66 61 73 68 69 6f 6e  rge tree fashion
33725 20 28 6c 69 6b 65 20 74 68 65 20 70 72 65 66 69   (like the prefi
33726 78 2d 73 65 61 72 63 68 69 6e 67 20 63 6f 64 65  x-searching code
33727 20 64 6f 65 73 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f   does)..*/./* TO
33728 44 4f 28 73 68 65 73 73 29 3a 20 4f 70 74 4c 65  DO(shess): OptLe
33729 61 76 65 73 52 65 61 64 65 72 20 61 6e 64 20 4c  avesReader and L
3372a 65 61 76 65 73 52 65 61 64 65 72 20 63 6f 75 6c  eavesReader coul
3372b 64 20 70 72 6f 62 61 62 6c 79 20 62 65 0a 2a 2a  d probably be.**
3372c 20 6d 65 72 67 65 64 20 77 69 74 68 20 6c 69 74   merged with lit
3372d 74 6c 65 20 6f 72 20 6e 6f 20 6c 6f 73 73 20 6f  tle or no loss o
3372e 66 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 66 6f  f performance fo
3372f 72 20 4c 65 61 76 65 73 52 65 61 64 65 72 2e 20  r LeavesReader. 
33730 20 54 68 65 0a 2a 2a 20 6d 65 72 67 65 64 20 63   The.** merged c
33731 6f 64 65 20 77 6f 75 6c 64 20 6e 65 65 64 20 74  ode would need t
33732 6f 20 68 61 6e 64 6c 65 20 3e 4d 45 52 47 45 5f  o handle >MERGE_
33733 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20  COUNT segments, 
33734 61 6e 64 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73  and would.** als
33735 6f 20 6e 65 65 64 20 74 6f 20 62 65 20 61 62 6c  o need to be abl
33736 65 20 74 6f 20 6f 70 74 69 6f 6e 61 6c 6c 79 20  e to optionally 
33737 6f 70 74 69 6d 69 7a 65 20 61 77 61 79 20 64 65  optimize away de
33738 6c 65 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  letes..*/.typede
33739 66 20 73 74 72 75 63 74 20 4f 70 74 4c 65 61 76  f struct OptLeav
3373a 65 73 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20  esReader {.  /* 
3373b 53 65 67 6d 65 6e 74 20 6e 75 6d 62 65 72 2c 20  Segment number, 
3373c 74 6f 20 6f 72 64 65 72 20 72 65 61 64 65 72 73  to order readers
3373d 20 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 69 6e   by age. */.  in
3373e 74 20 73 65 67 6d 65 6e 74 3b 0a 20 20 4c 65 61  t segment;.  Lea
3373f 76 65 73 52 65 61 64 65 72 20 72 65 61 64 65 72  vesReader reader
33740 3b 0a 7d 20 4f 70 74 4c 65 61 76 65 73 52 65 61  ;.} OptLeavesRea
33741 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  der;..static int
33742 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
33743 41 74 45 6e 64 28 4f 70 74 4c 65 61 76 65 73 52  AtEnd(OptLeavesR
33744 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
33745 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73  .  return leaves
33746 52 65 61 64 65 72 41 74 45 6e 64 28 26 70 52 65  ReaderAtEnd(&pRe
33747 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d  ader->reader);.}
33748 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c  .static int optL
33749 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42  eavesReaderTermB
3374a 79 74 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65  ytes(OptLeavesRe
3374b 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
3374c 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52    return leavesR
3374d 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 26  eaderTermBytes(&
3374e 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29  pReader->reader)
3374f 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.}.static const
33750 20 63 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73   char *optLeaves
33751 52 65 61 64 65 72 44 61 74 61 28 4f 70 74 4c 65  ReaderData(OptLe
33752 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61  avesReader *pRea
33753 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  der){.  return l
33754 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
33755 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72  &pReader->reader
33756 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
33757 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
33758 61 74 61 42 79 74 65 73 28 4f 70 74 4c 65 61 76  ataBytes(OptLeav
33759 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
3375a 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  r){.  return lea
3375b 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
3375c 65 73 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61  es(&pReader->rea
3375d 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63  der);.}.static c
3375e 6f 6e 73 74 20 63 68 61 72 20 2a 6f 70 74 4c 65  onst char *optLe
3375f 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 4f  avesReaderTerm(O
33760 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
33761 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
33762 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  rn leavesReaderT
33763 65 72 6d 28 26 70 52 65 61 64 65 72 2d 3e 72 65  erm(&pReader->re
33764 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
33765 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61  int optLeavesRea
33766 64 65 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74  derStep(fulltext
33767 5f 76 74 61 62 20 2a 76 2c 20 4f 70 74 4c 65 61  _vtab *v, OptLea
33768 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
33769 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65  er){.  return le
3376a 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76  avesReaderStep(v
3376b 2c 20 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64  , &pReader->read
3376c 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  er);.}.static in
3376d 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  t optLeavesReade
3376e 72 54 65 72 6d 43 6d 70 28 4f 70 74 4c 65 61 76  rTermCmp(OptLeav
3376f 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f  esReader *lr1, O
33770 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
33771 6c 72 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c  lr2){.  return l
33772 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43  eavesReaderTermC
33773 6d 70 28 26 6c 72 31 2d 3e 72 65 61 64 65 72 2c  mp(&lr1->reader,
33774 20 26 6c 72 32 2d 3e 72 65 61 64 65 72 29 3b 0a   &lr2->reader);.
33775 7d 0a 2f 2a 20 4f 72 64 65 72 20 62 79 20 74 65  }./* Order by te
33776 72 6d 20 61 73 63 65 6e 64 69 6e 67 2c 20 73 65  rm ascending, se
33777 67 6d 65 6e 74 20 61 73 63 65 6e 64 69 6e 67 20  gment ascending 
33778 28 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73  (oldest to newes
33779 74 29 2c 20 77 69 74 68 0a 2a 2a 20 65 78 68 61  t), with.** exha
3377a 75 73 74 65 64 20 72 65 61 64 65 72 73 20 74 6f  usted readers to
3377b 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
3377c 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65  tic int optLeave
3377d 73 52 65 61 64 65 72 43 6d 70 28 4f 70 74 4c 65  sReaderCmp(OptLe
3377e 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c  avesReader *lr1,
3377f 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   OptLeavesReader
33780 20 2a 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20   *lr2){.  int c 
33781 3d 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  = optLeavesReade
33782 72 54 65 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72  rTermCmp(lr1, lr
33783 32 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29  2);.  if( c!=0 )
33784 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 72 65 74   return c;.  ret
33785 75 72 6e 20 6c 72 31 2d 3e 73 65 67 6d 65 6e 74  urn lr1->segment
33786 2d 6c 72 32 2d 3e 73 65 67 6d 65 6e 74 3b 0a 7d  -lr2->segment;.}
33787 0a 2f 2a 20 42 75 62 62 6c 65 20 70 4c 72 5b 30  ./* Bubble pLr[0
33788 5d 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65  ] to appropriate
33789 20 70 6c 61 63 65 20 69 6e 20 70 4c 72 5b 31 2e   place in pLr[1.
3378a 2e 6e 4c 72 2d 31 5d 2e 20 20 41 73 73 75 6d 65  .nLr-1].  Assume
3378b 73 20 74 68 61 74 0a 2a 2a 20 70 4c 72 5b 31 2e  s that.** pLr[1.
3378c 2e 6e 4c 72 2d 31 5d 20 69 73 20 61 6c 72 65 61  .nLr-1] is alrea
3378d 64 79 20 73 6f 72 74 65 64 2e 0a 2a 2f 0a 73 74  dy sorted..*/.st
3378e 61 74 69 63 20 76 6f 69 64 20 6f 70 74 4c 65 61  atic void optLea
3378f 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72  vesReaderReorder
33790 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  (OptLeavesReader
33791 20 2a 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b   *pLr, int nLr){
33792 0a 20 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20  .  while( nLr>1 
33793 26 26 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64  && optLeavesRead
33794 65 72 43 6d 70 28 70 4c 72 2c 20 70 4c 72 2b 31  erCmp(pLr, pLr+1
33795 29 3e 30 20 29 7b 0a 20 20 20 20 4f 70 74 4c 65  )>0 ){.    OptLe
33796 61 76 65 73 52 65 61 64 65 72 20 74 6d 70 20 3d  avesReader tmp =
33797 20 70 4c 72 5b 30 5d 3b 0a 20 20 20 20 70 4c 72   pLr[0];.    pLr
33798 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d 3b 0a 20 20  [0] = pLr[1];.  
33799 20 20 70 4c 72 5b 31 5d 20 3d 20 74 6d 70 3b 0a    pLr[1] = tmp;.
3379a 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20 20 20 20 70      nLr--;.    p
3379b 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  Lr++;.  }.}../* 
3379c 6f 70 74 69 6d 69 7a 65 28 29 20 68 65 6c 70 65  optimize() helpe
3379d 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 50 75 74  r function.  Put
3379e 20 74 68 65 20 72 65 61 64 65 72 73 20 69 6e 20   the readers in 
3379f 6f 72 64 65 72 20 61 6e 64 20 69 74 65 72 61 74  order and iterat
337a0 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  e.** through the
337a1 6d 2c 20 6d 65 72 67 69 6e 67 20 64 6f 63 6c 69  m, merging docli
337a2 73 74 73 20 66 6f 72 20 6d 61 74 63 68 69 6e 67  sts for matching
337a3 20 74 65 72 6d 73 20 69 6e 74 6f 20 70 57 72 69   terms into pWri
337a4 74 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20  ter..** Returns 
337a5 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
337a6 63 65 73 73 2c 20 6f 72 20 74 68 65 20 53 51 4c  cess, or the SQL
337a7 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 77  ite error code w
337a8 68 69 63 68 0a 2a 2a 20 70 72 65 76 65 6e 74 65  hich.** prevente
337a9 64 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74  d success..*/.st
337aa 61 74 69 63 20 69 6e 74 20 6f 70 74 69 6d 69 7a  atic int optimiz
337ab 65 49 6e 74 65 72 6e 61 6c 28 66 75 6c 6c 74 65  eInternal(fullte
337ac 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20  xt_vtab *v,.    
337ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
337ae 20 20 20 20 20 20 20 20 4f 70 74 4c 65 61 76 65          OptLeave
337af 73 52 65 61 64 65 72 20 2a 72 65 61 64 65 72 73  sReader *readers
337b0 2c 20 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a  , int nReaders,.
337b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
337b2 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
337b3 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
337b4 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20  {.  int i, rc = 
337b5 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 44 61 74  SQLITE_OK;.  Dat
337b6 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 2c  aBuffer doclist,
337b7 20 6d 65 72 67 65 64 2c 20 74 6d 70 3b 0a 0a 20   merged, tmp;.. 
337b8 20 2f 2a 20 4f 72 64 65 72 20 74 68 65 20 72 65   /* Order the re
337b9 61 64 65 72 73 2e 20 2a 2f 0a 20 20 69 20 3d 20  aders. */.  i = 
337ba 6e 52 65 61 64 65 72 73 3b 0a 20 20 77 68 69 6c  nReaders;.  whil
337bb 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20  e( i-- > 0 ){.  
337bc 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65    optLeavesReade
337bd 72 52 65 6f 72 64 65 72 28 26 72 65 61 64 65 72  rReorder(&reader
337be 73 5b 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69  s[i], nReaders-i
337bf 29 3b 0a 20 20 7d 0a 0a 20 20 64 61 74 61 42 75  );.  }..  dataBu
337c0 66 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73  fferInit(&doclis
337c1 74 2c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 20 20  t, LEAF_MAX);.  
337c2 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
337c3 6d 65 72 67 65 64 2c 20 4c 45 41 46 5f 4d 41 58  merged, LEAF_MAX
337c4 29 3b 0a 0a 20 20 2f 2a 20 45 78 68 61 75 73 74  );..  /* Exhaust
337c5 65 64 20 72 65 61 64 65 72 73 20 62 75 62 62 6c  ed readers bubbl
337c6 65 20 74 6f 20 74 68 65 20 65 6e 64 2c 20 73 6f  e to the end, so
337c7 20 77 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   when the first 
337c8 72 65 61 64 65 72 20 69 73 0a 20 20 2a 2a 20 61  reader is.  ** a
337c9 74 20 65 6f 66 2c 20 61 6c 6c 20 61 72 65 20 61  t eof, all are a
337ca 74 20 65 6f 66 2e 0a 20 20 2a 2f 0a 20 20 77 68  t eof..  */.  wh
337cb 69 6c 65 28 20 21 6f 70 74 4c 65 61 76 65 73 52  ile( !optLeavesR
337cc 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64  eaderAtEnd(&read
337cd 65 72 73 5b 30 5d 29 20 29 7b 0a 0a 20 20 20 20  ers[0]) ){..    
337ce 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
337cf 77 20 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73  w many readers s
337d0 68 61 72 65 20 74 68 65 20 6e 65 78 74 20 74 65  hare the next te
337d1 72 6d 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  rm. */.    for(i
337d2 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26  =1; i<nReaders &
337d3 26 20 21 6f 70 74 4c 65 61 76 65 73 52 65 61 64  & !optLeavesRead
337d4 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72 73  erAtEnd(&readers
337d5 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  [i]); i++){.    
337d6 20 20 69 66 28 20 30 21 3d 6f 70 74 4c 65 61 76    if( 0!=optLeav
337d7 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  esReaderTermCmp(
337d8 26 72 65 61 64 65 72 73 5b 30 5d 2c 20 26 72 65  &readers[0], &re
337d9 61 64 65 72 73 5b 69 5d 29 20 29 20 62 72 65 61  aders[i]) ) brea
337da 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
337db 20 53 70 65 63 69 61 6c 2d 63 61 73 65 20 66 6f   Special-case fo
337dc 72 20 6e 6f 20 6d 65 72 67 65 2e 20 2a 2f 0a 20  r no merge. */. 
337dd 20 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a 20     if( i==1 ){. 
337de 20 20 20 20 20 2f 2a 20 54 72 69 6d 20 64 65 6c       /* Trim del
337df 65 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20  etions from the 
337e0 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20  doclist. */.    
337e1 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65    dataBufferRese
337e2 74 28 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20  t(&merged);.    
337e3 20 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c    docListTrim(DL
337e4 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20  _DEFAULT,.      
337e5 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c              optL
337e6 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28  eavesReaderData(
337e7 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20  &readers[0]),.  
337e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
337e9 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
337ea 61 74 61 42 79 74 65 73 28 26 72 65 61 64 65 72  ataBytes(&reader
337eb 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20  s[0]),.         
337ec 20 20 20 20 20 20 20 20 20 2d 31 2c 20 44 4c 5f           -1, DL_
337ed 44 45 46 41 55 4c 54 2c 20 26 6d 65 72 67 65 64  DEFAULT, &merged
337ee 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
337ef 20 20 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52      DLReader dlR
337f0 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55  eaders[MERGE_COU
337f1 4e 54 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  NT];.      int i
337f2 52 65 61 64 65 72 2c 20 6e 52 65 61 64 65 72 73  Reader, nReaders
337f3 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 50 72 69 6d  ;..      /* Prim
337f4 65 20 74 68 65 20 70 69 70 65 6c 69 6e 65 20 77  e the pipeline w
337f5 69 74 68 20 74 68 65 20 66 69 72 73 74 20 72 65  ith the first re
337f6 61 64 65 72 27 73 20 64 6f 63 6c 69 73 74 2e 20  ader's doclist. 
337f7 20 41 66 74 65 72 0a 20 20 20 20 20 20 2a 2a 20   After.      ** 
337f8 6f 6e 65 20 70 61 73 73 20 69 6e 64 65 78 20 30  one pass index 0
337f9 20 77 69 6c 6c 20 72 65 66 65 72 65 6e 63 65 20   will reference 
337fa 74 68 65 20 61 63 63 75 6d 75 6c 61 74 65 64 20  the accumulated 
337fb 64 6f 63 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a  doclist..      *
337fc 2f 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28  /.      dlrInit(
337fd 26 64 6c 52 65 61 64 65 72 73 5b 30 5d 2c 20 44  &dlReaders[0], D
337fe 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20  L_DEFAULT,.     
337ff 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
33800 65 73 52 65 61 64 65 72 44 61 74 61 28 26 72 65  esReaderData(&re
33801 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20  aders[0]),.     
33802 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76           optLeav
33803 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74 65  esReaderDataByte
33804 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29 29 3b  s(&readers[0]));
33805 0a 20 20 20 20 20 20 69 52 65 61 64 65 72 20 3d  .      iReader =
33806 20 31 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72   1;..      asser
33807 74 28 20 69 52 65 61 64 65 72 3c 69 20 29 3b 20  t( iReader<i ); 
33808 20 2f 2a 20 4d 75 73 74 20 65 78 65 63 75 74 65   /* Must execute
33809 20 74 68 65 20 6c 6f 6f 70 20 61 74 20 6c 65 61   the loop at lea
3380a 73 74 20 6f 6e 63 65 2e 20 2a 2f 0a 20 20 20 20  st once. */.    
3380b 20 20 77 68 69 6c 65 28 20 69 52 65 61 64 65 72    while( iReader
3380c 3c 69 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  <i ){.        /*
3380d 20 4d 65 72 67 65 20 31 36 20 69 6e 70 75 74 73   Merge 16 inputs
3380e 20 70 65 72 20 70 61 73 73 2e 20 2a 2f 0a 20 20   per pass. */.  
3380f 20 20 20 20 20 20 66 6f 72 28 20 6e 52 65 61 64        for( nRead
33810 65 72 73 3d 31 3b 20 69 52 65 61 64 65 72 3c 69  ers=1; iReader<i
33811 20 26 26 20 6e 52 65 61 64 65 72 73 3c 4d 45 52   && nReaders<MER
33812 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 20 20 20 20  GE_COUNT;.      
33813 20 20 20 20 20 20 20 69 52 65 61 64 65 72 2b 2b         iReader++
33814 2c 20 6e 52 65 61 64 65 72 73 2b 2b 20 29 7b 0a  , nReaders++ ){.
33815 20 20 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69            dlrIni
33816 74 28 26 64 6c 52 65 61 64 65 72 73 5b 6e 52 65  t(&dlReaders[nRe
33817 61 64 65 72 73 5d 2c 20 44 4c 5f 44 45 46 41 55  aders], DL_DEFAU
33818 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LT,.            
33819 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52        optLeavesR
3381a 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64 65  eaderData(&reade
3381b 72 73 5b 69 52 65 61 64 65 72 5d 29 2c 0a 20 20  rs[iReader]),.  
3381c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3381d 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
3381e 61 74 61 42 79 74 65 73 28 26 72 65 61 64 65 72  ataBytes(&reader
3381f 73 5b 69 52 65 61 64 65 72 5d 29 29 3b 0a 20 20  s[iReader]));.  
33820 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
33821 20 2f 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69 73   /* Merge doclis
33822 74 73 20 61 6e 64 20 73 77 61 70 20 72 65 73 75  ts and swap resu
33823 6c 74 20 69 6e 74 6f 20 61 63 63 75 6d 75 6c 61  lt into accumula
33824 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tor. */.        
33825 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
33826 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 20 20  &merged);.      
33827 20 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26    docListMerge(&
33828 6d 65 72 67 65 64 2c 20 64 6c 52 65 61 64 65 72  merged, dlReader
33829 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20 20  s, nReaders);.  
3382a 20 20 20 20 20 20 74 6d 70 20 3d 20 6d 65 72 67        tmp = merg
3382b 65 64 3b 0a 20 20 20 20 20 20 20 20 6d 65 72 67  ed;.        merg
3382c 65 64 20 3d 20 64 6f 63 6c 69 73 74 3b 0a 20 20  ed = doclist;.  
3382d 20 20 20 20 20 20 64 6f 63 6c 69 73 74 20 3d 20        doclist = 
3382e 74 6d 70 3b 0a 0a 20 20 20 20 20 20 20 20 77 68  tmp;..        wh
3382f 69 6c 65 28 20 6e 52 65 61 64 65 72 73 2d 2d 20  ile( nReaders-- 
33830 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  > 0 ){.         
33831 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52   dlrDestroy(&dlR
33832 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73 5d  eaders[nReaders]
33833 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  );.        }..  
33834 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
33835 61 74 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20  ated doclist to 
33836 72 65 61 64 65 72 20 30 20 66 6f 72 20 6e 65 78  reader 0 for nex
33837 74 20 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20  t pass. */.     
33838 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65     dlrInit(&dlRe
33839 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46  aders[0], DL_DEF
3383a 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44  AULT, doclist.pD
3383b 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61  ata, doclist.nDa
3383c 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  ta);.      }..  
3383d 20 20 20 20 2f 2a 20 44 65 73 74 72 6f 79 20 72      /* Destroy r
3383e 65 61 64 65 72 20 74 68 61 74 20 77 61 73 20 6c  eader that was l
3383f 65 66 74 20 69 6e 20 74 68 65 20 70 69 70 65 6c  eft in the pipel
33840 69 6e 65 2e 20 2a 2f 0a 20 20 20 20 20 20 64 6c  ine. */.      dl
33841 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64  rDestroy(&dlRead
33842 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20  ers[0]);..      
33843 2f 2a 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e  /* Trim deletion
33844 73 20 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69  s from the docli
33845 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74  st. */.      dat
33846 61 42 75 66 66 65 72 52 65 73 65 74 28 26 6d 65  aBufferReset(&me
33847 72 67 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63  rged);.      doc
33848 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41  ListTrim(DL_DEFA
33849 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61  ULT, doclist.pDa
3384a 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74  ta, doclist.nDat
3384b 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
3384c 20 20 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41       -1, DL_DEFA
3384d 55 4c 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20  ULT, &merged);. 
3384e 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c     }..    /* Onl
3384f 79 20 70 61 73 73 20 64 6f 63 6c 69 73 74 73 20  y pass doclists 
33850 77 69 74 68 20 68 69 74 73 20 28 73 6b 69 70 20  with hits (skip 
33851 69 66 20 61 6c 6c 20 68 69 74 73 20 64 65 6c 65  if all hits dele
33852 74 65 64 29 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ted). */.    if(
33853 20 6d 65 72 67 65 64 2e 6e 44 61 74 61 3e 30 20   merged.nData>0 
33854 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65  ){.      rc = le
33855 61 66 57 72 69 74 65 72 53 74 65 70 28 76 2c 20  afWriterStep(v, 
33856 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
33857 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33858 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64     optLeavesRead
33859 65 72 54 65 72 6d 28 26 72 65 61 64 65 72 73 5b  erTerm(&readers[
3385a 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0]),.           
3385b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
3385c 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54 65  ptLeavesReaderTe
3385d 72 6d 42 79 74 65 73 28 26 72 65 61 64 65 72 73  rmBytes(&readers
3385e 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  [0]),.          
3385f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33860 6d 65 72 67 65 64 2e 70 44 61 74 61 2c 20 6d 65  merged.pData, me
33861 72 67 65 64 2e 6e 44 61 74 61 29 3b 0a 20 20 20  rged.nData);.   
33862 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
33863 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
33864 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
33865 74 65 70 20 6d 65 72 67 65 64 20 72 65 61 64 65  tep merged reade
33866 72 73 20 74 6f 20 6e 65 78 74 20 74 65 72 6d 20  rs to next term 
33867 61 6e 64 20 72 65 6f 72 64 65 72 2e 20 2a 2f 0a  and reorder. */.
33868 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e      while( i-- >
33869 20 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d   0 ){.      rc =
3386a 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
3386b 53 74 65 70 28 76 2c 20 26 72 65 61 64 65 72 73  Step(v, &readers
3386c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [i]);.      if( 
3386d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3386e 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 20  goto err;..     
3386f 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
33870 52 65 6f 72 64 65 72 28 26 72 65 61 64 65 72 73  Reorder(&readers
33871 5b 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69 29  [i], nReaders-i)
33872 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 65 72  ;.    }.  }.. er
33873 72 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44  r:.  dataBufferD
33874 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29  estroy(&doclist)
33875 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
33876 73 74 72 6f 79 28 26 6d 65 72 67 65 64 29 3b 0a  stroy(&merged);.
33877 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
33878 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 20 6f 70 74  /* Implement opt
33879 69 6d 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e  imize() function
3387a 20 66 6f 72 20 46 54 53 33 2e 20 20 6f 70 74 69   for FTS3.  opti
3387b 6d 69 7a 65 28 74 29 20 6d 65 72 67 65 73 20 61  mize(t) merges a
3387c 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20 69  ll.** segments i
3387d 6e 20 74 68 65 20 66 74 73 20 69 6e 64 65 78 20  n the fts index 
3387e 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65  into a single se
3387f 67 6d 65 6e 74 2e 20 20 27 74 27 20 69 73 20 74  gment.  't' is t
33880 68 65 20 6d 61 67 69 63 0a 2a 2a 20 74 61 62 6c  he magic.** tabl
33881 65 2d 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 2e 0a  e-named column..
33882 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
33883 70 74 69 6d 69 7a 65 46 75 6e 63 28 73 71 6c 69  ptimizeFunc(sqli
33884 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
33885 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
33886 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33887 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
33888 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
33889 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
3388a 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69  or *pCursor;.  i
3388b 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20  f( argc>1 ){.   
3388c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3388d 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  error(pContext, 
3388e 22 65 78 63 65 73 73 20 61 72 67 75 6d 65 6e 74  "excess argument
3388f 73 20 74 6f 20 6f 70 74 69 6d 69 7a 65 28 29 22  s to optimize()"
33890 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ,-1);.  }else if
33891 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
33892 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53  type(argv[0])!=S
33893 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20  QLITE_BLOB ||.  
33894 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
33895 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
33896 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70  gv[0])!=sizeof(p
33897 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73  Cursor) ){.    s
33898 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
33899 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 69  ror(pContext, "i
3389a 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72 67  llegal first arg
3389b 75 6d 65 6e 74 20 74 6f 20 6f 70 74 69 6d 69 7a  ument to optimiz
3389c 65 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  e",-1);.  }else{
3389d 0a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  .    fulltext_vt
3389e 61 62 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20 69  ab *v;.    int i
3389f 2c 20 72 63 2c 20 69 4d 61 78 4c 65 76 65 6c 3b  , rc, iMaxLevel;
338a0 0a 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65  .    OptLeavesRe
338a1 61 64 65 72 20 2a 72 65 61 64 65 72 73 3b 0a 20  ader *readers;. 
338a2 20 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b     int nReaders;
338a3 0a 20 20 20 20 4c 65 61 66 57 72 69 74 65 72 20  .    LeafWriter 
338a4 77 72 69 74 65 72 3b 0a 20 20 20 20 73 71 6c 69  writer;.    sqli
338a5 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 0a 20 20  te3_stmt *s;..  
338a6 20 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f    memcpy(&pCurso
338a7 72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r, sqlite3_value
338a8 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20  _blob(argv[0]), 
338a9 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29  sizeof(pCursor))
338aa 3b 0a 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72  ;.    v = cursor
338ab 5f 76 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a  _vtab(pCursor);.
338ac 0a 20 20 20 20 2f 2a 20 46 6c 75 73 68 20 61 6e  .    /* Flush an
338ad 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  y buffered updat
338ae 65 73 20 62 65 66 6f 72 65 20 6f 70 74 69 6d 69  es before optimi
338af 7a 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20  zing. */.    rc 
338b0 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65  = flushPendingTe
338b1 72 6d 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20  rms(v);.    if( 
338b2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
338b3 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 72  goto err;..    r
338b4 63 20 3d 20 73 65 67 64 69 72 5f 63 6f 75 6e 74  c = segdir_count
338b5 28 76 2c 20 26 6e 52 65 61 64 65 72 73 2c 20 26  (v, &nReaders, &
338b6 69 4d 61 78 4c 65 76 65 6c 29 3b 0a 20 20 20 20  iMaxLevel);.    
338b7 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
338b8 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20  K ) goto err;.  
338b9 20 20 69 66 28 20 6e 52 65 61 64 65 72 73 3d 3d    if( nReaders==
338ba 30 20 7c 7c 20 6e 52 65 61 64 65 72 73 3d 3d 31  0 || nReaders==1
338bb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
338bc 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
338bd 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65 78 20 61  ontext, "Index a
338be 6c 72 65 61 64 79 20 6f 70 74 69 6d 61 6c 22 2c  lready optimal",
338bf 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20   -1,.           
338c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
338c1 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
338c2 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
338c3 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   }..    rc = sql
338c4 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
338c5 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
338c6 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ALL_STMT, &s);. 
338c7 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
338c8 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
338c9 0a 0a 20 20 20 20 72 65 61 64 65 72 73 20 3d 20  ..    readers = 
338ca 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
338cb 52 65 61 64 65 72 73 2a 73 69 7a 65 6f 66 28 72  Readers*sizeof(r
338cc 65 61 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20  eaders[0]));.   
338cd 20 69 66 28 20 72 65 61 64 65 72 73 3d 3d 4e 55   if( readers==NU
338ce 4c 4c 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a  LL ) goto err;..
338cf 20 20 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74      /* Note that
338d0 20 74 68 65 72 65 20 77 69 6c 6c 20 61 6c 72 65   there will alre
338d1 61 64 79 20 62 65 20 61 20 73 65 67 6d 65 6e 74  ady be a segment
338d2 20 61 74 20 74 68 69 73 20 70 6f 73 69 74 69 6f   at this positio
338d3 6e 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77  n.    ** until w
338d4 65 20 63 61 6c 6c 20 73 65 67 64 69 72 5f 64 65  e call segdir_de
338d5 6c 65 74 65 28 29 20 6f 6e 20 69 4d 61 78 4c 65  lete() on iMaxLe
338d6 76 65 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  vel..    */.    
338d7 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28 69  leafWriterInit(i
338d8 4d 61 78 4c 65 76 65 6c 2c 20 30 2c 20 26 77 72  MaxLevel, 0, &wr
338d9 69 74 65 72 29 3b 0a 0a 20 20 20 20 69 20 3d 20  iter);..    i = 
338da 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 72  0;.    while( (r
338db 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
338dc 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (s))==SQLITE_ROW
338dd 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
338de 5f 69 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20  _int64 iStart = 
338df 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
338e0 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20  nt64(s, 0);.    
338e1 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
338e2 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  End = sqlite3_co
338e3 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29  lumn_int64(s, 1)
338e4 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
338e5 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20  ar *pRootData = 
338e6 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
338e7 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20  lob(s, 2);.     
338e8 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d   int nRootData =
338e9 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
338ea 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20  bytes(s, 2);..  
338eb 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 52      assert( i<nR
338ec 65 61 64 65 72 73 20 29 3b 0a 20 20 20 20 20 20  eaders );.      
338ed 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rc = leavesReade
338ee 72 49 6e 69 74 28 76 2c 20 2d 31 2c 20 69 53 74  rInit(v, -1, iSt
338ef 61 72 74 2c 20 69 45 6e 64 2c 20 70 52 6f 6f 74  art, iEnd, pRoot
338f0 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c  Data, nRootData,
338f1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
338f2 20 20 20 20 20 20 20 20 20 20 20 20 20 26 72 65               &re
338f3 61 64 65 72 73 5b 69 5d 2e 72 65 61 64 65 72 29  aders[i].reader)
338f4 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
338f5 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61  SQLITE_OK ) brea
338f6 6b 3b 0a 0a 20 20 20 20 20 20 72 65 61 64 65 72  k;..      reader
338f7 73 5b 69 5d 2e 73 65 67 6d 65 6e 74 20 3d 20 69  s[i].segment = i
338f8 3b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20  ;.      i++;.   
338f9 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65   }..    /* If we
338fa 20 6d 61 6e 61 67 65 64 20 74 6f 20 73 75 63 63   managed to succ
338fb 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 74 68  essfully read th
338fc 65 6d 20 61 6c 6c 2c 20 6f 70 74 69 6d 69 7a 65  em all, optimize
338fd 20 74 68 65 6d 2e 20 2a 2f 0a 20 20 20 20 69 66   them. */.    if
338fe 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
338ff 45 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  E ){.      asser
33900 74 28 20 69 3d 3d 6e 52 65 61 64 65 72 73 20 29  t( i==nReaders )
33901 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6f 70 74  ;.      rc = opt
33902 69 6d 69 7a 65 49 6e 74 65 72 6e 61 6c 28 76 2c  imizeInternal(v,
33903 20 72 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65   readers, nReade
33904 72 73 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20  rs, &writer);.  
33905 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28 20    }..    while( 
33906 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 20  i-- > 0 ){.     
33907 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73   leavesReaderDes
33908 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b 69 5d  troy(&readers[i]
33909 2e 72 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a  .reader);.    }.
3390a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
3390b 28 72 65 61 64 65 72 73 29 3b 0a 0a 20 20 20 20  (readers);..    
3390c 2f 2a 20 49 66 20 77 65 27 76 65 20 73 75 63 63  /* If we've succ
3390d 65 73 73 66 75 6c 6c 79 20 67 6f 74 74 65 6e 20  essfully gotten 
3390e 74 6f 20 68 65 72 65 2c 20 64 65 6c 65 74 65 20  to here, delete 
3390f 74 68 65 20 6f 6c 64 20 73 65 67 6d 65 6e 74 73  the old segments
33910 0a 20 20 20 20 2a 2a 20 61 6e 64 20 66 6c 75 73  .    ** and flus
33911 68 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 73  h the interior s
33912 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20  tructure of the 
33913 6e 65 77 20 73 65 67 6d 65 6e 74 2e 0a 20 20 20  new segment..   
33914 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
33915 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
33916 20 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 3d     for( i=0; i<=
33917 69 4d 61 78 4c 65 76 65 6c 3b 20 69 2b 2b 20 29  iMaxLevel; i++ )
33918 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
33919 65 67 64 69 72 5f 64 65 6c 65 74 65 28 76 2c 20  egdir_delete(v, 
3391a 69 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  i);.        if( 
3391b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3391c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a  break;.      }..
3391d 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
3391e 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 6c  LITE_OK ) rc = l
3391f 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a  eafWriterFinaliz
33920 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b 0a 20  e(v, &writer);. 
33921 20 20 20 7d 0a 0a 20 20 20 20 6c 65 61 66 57 72     }..    leafWr
33922 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69  iterDestroy(&wri
33923 74 65 72 29 3b 0a 0a 20 20 20 20 69 66 28 20 72  ter);..    if( r
33924 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
33925 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 73 71  oto err;..    sq
33926 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
33927 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64  t(pContext, "Ind
33928 65 78 20 6f 70 74 69 6d 69 7a 65 64 22 2c 20 2d  ex optimized", -
33929 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
3392a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a  );.    return;..
3392b 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
3392c 73 29 3a 20 45 72 72 6f 72 2d 68 61 6e 64 6c 69  s): Error-handli
3392d 6e 67 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69  ng needs to be i
3392e 6d 70 72 6f 76 65 64 20 61 6c 6f 6e 67 20 74 68  mproved along th
3392f 65 0a 20 20 20 20 2a 2a 20 6c 69 6e 65 73 20 6f  e.    ** lines o
33930 66 20 74 68 65 20 64 75 6d 70 5f 20 66 75 6e 63  f the dump_ func
33931 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 65  tions..    */. e
33932 72 72 3a 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  rr:.    {.      
33933 63 68 61 72 20 62 75 66 5b 35 31 32 5d 3b 0a 20  char buf[512];. 
33934 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
33935 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
33936 29 2c 20 62 75 66 2c 20 22 45 72 72 6f 72 20 69  ), buf, "Error i
33937 6e 20 6f 70 74 69 6d 69 7a 65 3a 20 25 73 22 2c  n optimize: %s",
33938 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33939 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
3393a 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 5f 63  errmsg(sqlite3_c
3393b 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
3393c 28 70 43 6f 6e 74 65 78 74 29 29 29 3b 0a 20 20  (pContext)));.  
3393d 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3393e 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78  lt_error(pContex
3393f 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a 20 20 20  t, buf, -1);.   
33940 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66   }.  }.}..#ifdef
33941 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20   SQLITE_TEST./* 
33942 47 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  Generate an erro
33943 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c  r of the form "<
33944 70 72 65 66 69 78 3e 3a 20 3c 6d 73 67 3e 22 2e  prefix>: <msg>".
33945 20 20 49 66 20 6d 73 67 20 69 73 20 4e 55 4c 4c    If msg is NULL
33946 2c 0a 2a 2a 20 70 75 6c 6c 20 74 68 65 20 65 72  ,.** pull the er
33947 72 6f 72 20 66 72 6f 6d 20 74 68 65 20 63 6f 6e  ror from the con
33948 74 65 78 74 27 73 20 64 62 20 68 61 6e 64 6c 65  text's db handle
33949 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
3394a 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 73   generateError(s
3394b 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
3394c 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20  pContext,.      
3394d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3394e 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
3394f 70 72 65 66 69 78 2c 20 63 6f 6e 73 74 20 63 68  prefix, const ch
33950 61 72 20 2a 6d 73 67 29 7b 0a 20 20 63 68 61 72  ar *msg){.  char
33951 20 62 75 66 5b 35 31 32 5d 3b 0a 20 20 69 66 28   buf[512];.  if(
33952 20 6d 73 67 3d 3d 4e 55 4c 4c 20 29 20 6d 73 67   msg==NULL ) msg
33953 20 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73   = sqlite3_errms
33954 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  g(sqlite3_contex
33955 74 5f 64 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e  t_db_handle(pCon
33956 74 65 78 74 29 29 3b 0a 20 20 73 71 6c 69 74 65  text));.  sqlite
33957 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
33958 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 73  f(buf), buf, "%s
33959 3a 20 25 73 22 2c 20 70 72 65 66 69 78 2c 20 6d  : %s", prefix, m
3395a 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  sg);.  sqlite3_r
3395b 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e  esult_error(pCon
3395c 74 65 78 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a  text, buf, -1);.
3395d 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e  }../* Helper fun
3395e 63 74 69 6f 6e 20 74 6f 20 63 6f 6c 6c 65 63 74  ction to collect
3395f 20 74 68 65 20 73 65 74 20 6f 66 20 74 65 72 6d   the set of term
33960 73 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  s in the segment
33961 20 69 6e 74 6f 0a 2a 2a 20 70 54 65 72 6d 73 2e   into.** pTerms.
33962 20 20 54 68 65 20 73 65 67 6d 65 6e 74 20 69 73    The segment is
33963 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 20   defined by the 
33964 6c 65 61 66 20 6e 6f 64 65 73 20 62 65 74 77 65  leaf nodes betwe
33965 65 6e 0a 2a 2a 20 69 53 74 61 72 74 42 6c 6f 63  en.** iStartBloc
33966 6b 69 64 20 61 6e 64 20 69 45 6e 64 42 6c 6f 63  kid and iEndBloc
33967 6b 69 64 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20  kid, inclusive, 
33968 6f 72 20 62 79 20 74 68 65 20 63 6f 6e 74 65 6e  or by the conten
33969 74 73 20 6f 66 0a 2a 2a 20 70 52 6f 6f 74 44 61  ts of.** pRootDa
3396a 74 61 20 69 66 20 69 53 74 61 72 74 42 6c 6f 63  ta if iStartBloc
3396b 6b 69 64 20 69 73 20 30 20 28 69 6e 20 77 68 69  kid is 0 (in whi
3396c 63 68 20 63 61 73 65 20 74 68 65 20 65 6e 74 69  ch case the enti
3396d 72 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 66 69  re segment.** fi
3396e 74 20 69 6e 20 61 20 6c 65 61 66 29 2e 0a 2a 2f  t in a leaf)..*/
3396f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c  .static int coll
33970 65 63 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28  ectSegmentTerms(
33971 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
33972 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  , sqlite3_stmt *
33973 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
33974 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33975 20 20 66 74 73 33 48 61 73 68 20 2a 70 54 65 72    fts3Hash *pTer
33976 6d 73 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  ms){.  const sql
33977 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74  ite_int64 iStart
33978 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65  Blockid = sqlite
33979 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73  3_column_int64(s
3397a 2c 20 30 29 3b 0a 20 20 63 6f 6e 73 74 20 73 71  , 0);.  const sq
3397b 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42  lite_int64 iEndB
3397c 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33  lockid = sqlite3
3397d 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c  _column_int64(s,
3397e 20 31 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61   1);.  const cha
3397f 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73  r *pRootData = s
33980 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
33981 6f 62 28 73 2c 20 32 29 3b 0a 20 20 63 6f 6e 73  ob(s, 2);.  cons
33982 74 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20  t int nRootData 
33983 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
33984 5f 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20  _bytes(s, 2);.  
33985 4c 65 61 76 65 73 52 65 61 64 65 72 20 72 65 61  LeavesReader rea
33986 64 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  der;.  int rc = 
33987 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74  leavesReaderInit
33988 28 76 2c 20 30 2c 20 69 53 74 61 72 74 42 6c 6f  (v, 0, iStartBlo
33989 63 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69  ckid, iEndBlocki
3398a 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
3398b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
3398c 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44  RootData, nRootD
3398d 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 20  ata, &reader);. 
3398e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3398f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
33990 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
33991 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76  LITE_OK && !leav
33992 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 26 72  esReaderAtEnd(&r
33993 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 63 6f  eader) ){.    co
33994 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20  nst char *pTerm 
33995 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65  = leavesReaderTe
33996 72 6d 28 26 72 65 61 64 65 72 29 3b 0a 20 20 20  rm(&reader);.   
33997 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d   const int nTerm
33998 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54   = leavesReaderT
33999 65 72 6d 42 79 74 65 73 28 26 72 65 61 64 65 72  ermBytes(&reader
3399a 29 3b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64  );.    void *old
3399b 56 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 46  Value = sqlite3F
3399c 74 73 33 48 61 73 68 46 69 6e 64 28 70 54 65 72  ts3HashFind(pTer
3399d 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  ms, pTerm, nTerm
3399e 29 3b 0a 20 20 20 20 76 6f 69 64 20 2a 6e 65 77  );.    void *new
3399f 56 61 6c 75 65 20 3d 20 28 76 6f 69 64 20 2a 29  Value = (void *)
339a0 28 28 63 68 61 72 20 2a 29 6f 6c 64 56 61 6c 75  ((char *)oldValu
339a1 65 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 72  e+1);..    /* Fr
339a2 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 62  om the comment b
339a3 65 66 6f 72 65 20 73 71 6c 69 74 65 33 46 74 73  efore sqlite3Fts
339a4 33 48 61 73 68 49 6e 73 65 72 74 20 69 6e 20 66  3HashInsert in f
339a5 74 73 33 5f 68 61 73 68 2e 63 2c 0a 20 20 20 20  ts3_hash.c,.    
339a6 2a 2a 20 74 68 65 20 64 61 74 61 20 76 61 6c 75  ** the data valu
339a7 65 20 70 61 73 73 65 64 20 69 73 20 72 65 74 75  e passed is retu
339a8 72 6e 65 64 20 69 6e 20 63 61 73 65 20 6f 66 20  rned in case of 
339a9 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2e 0a  malloc failure..
339aa 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
339ab 65 77 56 61 6c 75 65 3d 3d 73 71 6c 69 74 65 33  ewValue==sqlite3
339ac 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70  Fts3HashInsert(p
339ad 54 65 72 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54  Terms, pTerm, nT
339ae 65 72 6d 2c 20 6e 65 77 56 61 6c 75 65 29 20 29  erm, newValue) )
339af 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
339b0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
339b1 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
339b2 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74 65   leavesReaderSte
339b3 70 28 76 2c 20 26 72 65 61 64 65 72 29 3b 0a 20  p(v, &reader);. 
339b4 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76     }.  }..  leav
339b5 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
339b6 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75  &reader);.  retu
339b7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c  rn rc;.}../* Hel
339b8 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
339b9 62 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74  build the result
339ba 20 73 74 72 69 6e 67 20 66 6f 72 20 64 75 6d 70   string for dump
339bb 5f 74 65 72 6d 73 28 29 2e 20 2a 2f 0a 73 74 61  _terms(). */.sta
339bc 74 69 63 20 69 6e 74 20 67 65 6e 65 72 61 74 65  tic int generate
339bd 54 65 72 6d 73 52 65 73 75 6c 74 28 73 71 6c 69  TermsResult(sqli
339be 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
339bf 6e 74 65 78 74 2c 20 66 74 73 33 48 61 73 68 20  ntext, fts3Hash 
339c0 2a 70 54 65 72 6d 73 29 7b 0a 20 20 69 6e 74 20  *pTerms){.  int 
339c1 69 54 65 72 6d 2c 20 6e 54 65 72 6d 73 2c 20 6e  iTerm, nTerms, n
339c2 52 65 73 75 6c 74 42 79 74 65 73 2c 20 69 42 79  ResultBytes, iBy
339c3 74 65 3b 0a 20 20 63 68 61 72 20 2a 72 65 73 75  te;.  char *resu
339c4 6c 74 3b 0a 20 20 54 65 72 6d 44 61 74 61 20 2a  lt;.  TermData *
339c5 70 44 61 74 61 3b 0a 20 20 66 74 73 33 48 61 73  pData;.  fts3Has
339c6 68 45 6c 65 6d 20 2a 65 3b 0a 0a 20 20 2f 2a 20  hElem *e;..  /* 
339c7 49 74 65 72 61 74 65 20 70 54 65 72 6d 73 20 74  Iterate pTerms t
339c8 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20 61 72  o generate an ar
339c9 72 61 79 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ray of terms in 
339ca 70 44 61 74 61 20 66 6f 72 0a 20 20 2a 2a 20 73  pData for.  ** s
339cb 6f 72 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6e  orting..  */.  n
339cc 54 65 72 6d 73 20 3d 20 66 74 73 33 48 61 73 68  Terms = fts3Hash
339cd 43 6f 75 6e 74 28 70 54 65 72 6d 73 29 3b 0a 20  Count(pTerms);. 
339ce 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 73 3e   assert( nTerms>
339cf 30 20 29 3b 0a 20 20 70 44 61 74 61 20 3d 20 73  0 );.  pData = s
339d0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 54  qlite3_malloc(nT
339d1 65 72 6d 73 2a 73 69 7a 65 6f 66 28 54 65 72 6d  erms*sizeof(Term
339d2 44 61 74 61 29 29 3b 0a 20 20 69 66 28 20 70 44  Data));.  if( pD
339d3 61 74 61 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ata==NULL ) retu
339d4 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
339d5 0a 0a 20 20 6e 52 65 73 75 6c 74 42 79 74 65 73  ..  nResultBytes
339d6 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72   = 0;.  for(iTer
339d7 6d 20 3d 20 30 2c 20 65 20 3d 20 66 74 73 33 48  m = 0, e = fts3H
339d8 61 73 68 46 69 72 73 74 28 70 54 65 72 6d 73 29  ashFirst(pTerms)
339d9 3b 20 65 3b 20 69 54 65 72 6d 2b 2b 2c 20 65 20  ; e; iTerm++, e 
339da 3d 20 66 74 73 33 48 61 73 68 4e 65 78 74 28 65  = fts3HashNext(e
339db 29 29 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 42  )){.    nResultB
339dc 79 74 65 73 20 2b 3d 20 66 74 73 33 48 61 73 68  ytes += fts3Hash
339dd 4b 65 79 73 69 7a 65 28 65 29 2b 31 3b 20 20 20  Keysize(e)+1;   
339de 2f 2a 20 54 65 72 6d 20 70 6c 75 73 20 74 72 61  /* Term plus tra
339df 69 6c 69 6e 67 20 73 70 61 63 65 20 2a 2f 0a 20  iling space */. 
339e0 20 20 20 61 73 73 65 72 74 28 20 69 54 65 72 6d     assert( iTerm
339e1 3c 6e 54 65 72 6d 73 20 29 3b 0a 20 20 20 20 70  <nTerms );.    p
339e2 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70 54 65 72  Data[iTerm].pTer
339e3 6d 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28  m = fts3HashKey(
339e4 65 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 54  e);.    pData[iT
339e5 65 72 6d 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73  erm].nTerm = fts
339e6 33 48 61 73 68 4b 65 79 73 69 7a 65 28 65 29 3b  3HashKeysize(e);
339e7 0a 20 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d  .    pData[iTerm
339e8 5d 2e 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 66  ].pCollector = f
339e9 74 73 33 48 61 73 68 44 61 74 61 28 65 29 3b 20  ts3HashData(e); 
339ea 20 2f 2a 20 75 6e 75 73 65 64 20 2a 2f 0a 20 20   /* unused */.  
339eb 7d 0a 20 20 61 73 73 65 72 74 28 20 69 54 65 72  }.  assert( iTer
339ec 6d 3d 3d 6e 54 65 72 6d 73 20 29 3b 0a 0a 20 20  m==nTerms );..  
339ed 61 73 73 65 72 74 28 20 6e 52 65 73 75 6c 74 42  assert( nResultB
339ee 79 74 65 73 3e 30 20 29 3b 20 20 20 2f 2a 20 6e  ytes>0 );   /* n
339ef 54 65 72 6d 73 3e 30 2c 20 6e 52 65 73 75 6c 74  Terms>0, nResult
339f0 73 42 79 74 65 73 20 6d 75 73 74 20 62 65 2c 20  sBytes must be, 
339f1 74 6f 6f 2e 20 2a 2f 0a 20 20 72 65 73 75 6c 74  too. */.  result
339f2 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
339f3 63 28 6e 52 65 73 75 6c 74 42 79 74 65 73 29 3b  c(nResultBytes);
339f4 0a 20 20 69 66 28 20 72 65 73 75 6c 74 3d 3d 4e  .  if( result==N
339f5 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ULL ){.    sqlit
339f6 65 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a  e3_free(pData);.
339f7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
339f8 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
339f9 69 66 28 20 6e 54 65 72 6d 73 3e 31 20 29 20 71  if( nTerms>1 ) q
339fa 73 6f 72 74 28 70 44 61 74 61 2c 20 6e 54 65 72  sort(pData, nTer
339fb 6d 73 2c 20 73 69 7a 65 6f 66 28 2a 70 44 61 74  ms, sizeof(*pDat
339fc 61 29 2c 20 74 65 72 6d 44 61 74 61 43 6d 70 29  a), termDataCmp)
339fd 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ;..  /* Read the
339fe 20 74 65 72 6d 73 20 69 6e 20 6f 72 64 65 72 20   terms in order 
339ff 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65 73  to build the res
33a00 75 6c 74 2e 20 2a 2f 0a 20 20 69 42 79 74 65 20  ult. */.  iByte 
33a01 3d 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d  = 0;.  for(iTerm
33a02 3d 30 3b 20 69 54 65 72 6d 3c 6e 54 65 72 6d 73  =0; iTerm<nTerms
33a03 3b 20 2b 2b 69 54 65 72 6d 29 7b 0a 20 20 20 20  ; ++iTerm){.    
33a04 6d 65 6d 63 70 79 28 72 65 73 75 6c 74 2b 69 42  memcpy(result+iB
33a05 79 74 65 2c 20 70 44 61 74 61 5b 69 54 65 72 6d  yte, pData[iTerm
33a06 5d 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b 69  ].pTerm, pData[i
33a07 54 65 72 6d 5d 2e 6e 54 65 72 6d 29 3b 0a 20 20  Term].nTerm);.  
33a08 20 20 69 42 79 74 65 20 2b 3d 20 70 44 61 74 61    iByte += pData
33a09 5b 69 54 65 72 6d 5d 2e 6e 54 65 72 6d 3b 0a 20  [iTerm].nTerm;. 
33a0a 20 20 20 72 65 73 75 6c 74 5b 69 42 79 74 65 2b     result[iByte+
33a0b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 7d 0a 20 20  +] = ' ';.  }.  
33a0c 61 73 73 65 72 74 28 20 69 42 79 74 65 3d 3d 6e  assert( iByte==n
33a0d 52 65 73 75 6c 74 42 79 74 65 73 20 29 3b 0a 20  ResultBytes );. 
33a0e 20 61 73 73 65 72 74 28 20 72 65 73 75 6c 74 5b   assert( result[
33a0f 6e 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d 3d  nResultBytes-1]=
33a10 3d 27 20 27 20 29 3b 0a 20 20 72 65 73 75 6c 74  =' ' );.  result
33a11 5b 6e 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d  [nResultBytes-1]
33a12 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 2f 2a 20 50   = '\0';..  /* P
33a13 61 73 73 65 73 20 61 77 61 79 20 6f 77 6e 65 72  asses away owner
33a14 73 68 69 70 20 6f 66 20 72 65 73 75 6c 74 2e 20  ship of result. 
33a15 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  */.  sqlite3_res
33a16 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
33a17 74 2c 20 72 65 73 75 6c 74 2c 20 6e 52 65 73 75  t, result, nResu
33a18 6c 74 42 79 74 65 73 2d 31 2c 20 73 71 6c 69 74  ltBytes-1, sqlit
33a19 65 33 5f 66 72 65 65 29 3b 0a 20 20 73 71 6c 69  e3_free);.  sqli
33a1a 74 65 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b  te3_free(pData);
33a1b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
33a1c 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65  _OK;.}../* Imple
33a1d 6d 65 6e 74 73 20 64 75 6d 70 5f 74 65 72 6d 73  ments dump_terms
33a1e 28 29 20 66 6f 72 20 75 73 65 20 69 6e 20 69 6e  () for use in in
33a1f 73 70 65 63 74 69 6e 67 20 74 68 65 20 66 74 73  specting the fts
33a20 33 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20  3 index from.** 
33a21 74 65 73 74 73 2e 20 20 54 45 58 54 20 72 65 73  tests.  TEXT res
33a22 75 6c 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ult containing t
33a23 68 65 20 6f 72 64 65 72 65 64 20 6c 69 73 74 20  he ordered list 
33a24 6f 66 20 74 65 72 6d 73 20 6a 6f 69 6e 65 64 20  of terms joined 
33a25 62 79 0a 2a 2a 20 73 70 61 63 65 73 2e 20 20 64  by.** spaces.  d
33a26 75 6d 70 5f 74 65 72 6d 73 28 74 2c 20 6c 65 76  ump_terms(t, lev
33a27 65 6c 2c 20 69 64 78 29 20 64 75 6d 70 73 20 74  el, idx) dumps t
33a28 68 65 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65  he terms for the
33a29 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 70 65 63   segment.** spec
33a2a 69 66 69 65 64 20 62 79 20 6c 65 76 65 6c 2c 20  ified by level, 
33a2b 69 64 78 20 28 69 6e 20 25 5f 73 65 67 64 69 72  idx (in %_segdir
33a2c 29 2c 20 77 68 69 6c 65 20 64 75 6d 70 5f 74 65  ), while dump_te
33a2d 72 6d 73 28 74 29 20 64 75 6d 70 73 0a 2a 2a 20  rms(t) dumps.** 
33a2e 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 74 68 65  all terms in the
33a2f 20 69 6e 64 65 78 2e 20 20 49 6e 20 62 6f 74 68   index.  In both
33a30 20 63 61 73 65 73 20 74 20 69 73 20 74 68 65 20   cases t is the 
33a31 66 74 73 20 74 61 62 6c 65 27 73 20 6d 61 67 69  fts table's magi
33a32 63 0a 2a 2a 20 74 61 62 6c 65 2d 6e 61 6d 65 64  c.** table-named
33a33 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74   column..*/.stat
33a34 69 63 20 76 6f 69 64 20 64 75 6d 70 54 65 72 6d  ic void dumpTerm
33a35 73 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  sFunc(.  sqlite3
33a36 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
33a37 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  xt,.  int argc, 
33a38 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
33a39 61 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65  argv.){.  fullte
33a3a 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  xt_cursor *pCurs
33a3b 6f 72 3b 0a 20 20 69 66 28 20 61 72 67 63 21 3d  or;.  if( argc!=
33a3c 33 20 26 26 20 61 72 67 63 21 3d 31 20 29 7b 0a  3 && argc!=1 ){.
33a3d 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
33a3e 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
33a3f 70 5f 74 65 72 6d 73 22 2c 20 22 69 6e 63 6f 72  p_terms", "incor
33a40 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 22 29  rect arguments")
33a41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
33a42 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
33a43 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54  (argv[0])!=SQLIT
33a44 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20  E_BLOB ||.      
33a45 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
33a46 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
33a47 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73  ])!=sizeof(pCurs
33a48 6f 72 29 20 29 7b 0a 20 20 20 20 67 65 6e 65 72  or) ){.    gener
33a49 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
33a4a 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c  t, "dump_terms",
33a4b 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20   "illegal first 
33a4c 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65  argument");.  }e
33a4d 6c 73 65 7b 0a 20 20 20 20 66 75 6c 6c 74 65 78  lse{.    fulltex
33a4e 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20 66  t_vtab *v;.    f
33a4f 74 73 33 48 61 73 68 20 74 65 72 6d 73 3b 0a 20  ts3Hash terms;. 
33a50 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
33a51 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69  *s = NULL;.    i
33a52 6e 74 20 72 63 3b 0a 0a 20 20 20 20 6d 65 6d 63  nt rc;..    memc
33a53 70 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c  py(&pCursor, sql
33a54 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
33a55 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66  argv[0]), sizeof
33a56 28 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20  (pCursor));.    
33a57 76 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28  v = cursor_vtab(
33a58 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20 2f  pCursor);..    /
33a59 2a 20 49 66 20 70 61 73 73 65 64 20 6f 6e 6c 79  * If passed only
33a5a 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f 6c 75   the cursor colu
33a5b 6d 6e 2c 20 67 65 74 20 61 6c 6c 20 73 65 67 6d  mn, get all segm
33a5c 65 6e 74 73 2e 20 20 4f 74 68 65 72 77 69 73 65  ents.  Otherwise
33a5d 0a 20 20 20 20 2a 2a 20 67 65 74 20 74 68 65 20  .    ** get the 
33a5e 73 65 67 6d 65 6e 74 20 64 65 73 63 72 69 62 65  segment describe
33a5f 64 20 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d by the followi
33a60 6e 67 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  ng two arguments
33a61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
33a62 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20   argc==1 ){.    
33a63 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73    rc = sql_get_s
33a64 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44  tatement(v, SEGD
33a65 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54  IR_SELECT_ALL_ST
33a66 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 7d 65 6c  MT, &s);.    }el
33a67 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
33a68 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
33a69 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43  (v, SEGDIR_SELEC
33a6a 54 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20  T_SEGMENT_STMT, 
33a6b 26 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  &s);.      if( r
33a6c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
33a6d 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
33a6e 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
33a6f 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   1, sqlite3_valu
33a70 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 29 3b  e_int(argv[1]));
33a71 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
33a72 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
33a73 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
33a74 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
33a75 20 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   2, sqlite3_valu
33a76 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b  e_int(argv[2]));
33a77 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
33a78 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
33a79 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
33a7a 29 7b 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74  ){.      generat
33a7b 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
33a7c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 4e   "dump_terms", N
33a7d 55 4c 4c 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ULL);.      retu
33a7e 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rn;.    }..    /
33a7f 2a 20 43 6f 6c 6c 65 63 74 20 74 68 65 20 74 65  * Collect the te
33a80 72 6d 73 20 66 6f 72 20 65 61 63 68 20 73 65 67  rms for each seg
33a81 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  ment. */.    sql
33a82 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
33a83 28 26 74 65 72 6d 73 2c 20 46 54 53 33 5f 48 41  (&terms, FTS3_HA
33a84 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20  SH_STRING, 1);. 
33a85 20 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20     while( (rc = 
33a86 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29  sqlite3_step(s))
33a87 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
33a88 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6c 6c 65        rc = colle
33a89 63 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28 76  ctSegmentTerms(v
33a8a 2c 20 73 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20  , s, &terms);.  
33a8b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
33a8c 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20  TE_OK ) break;. 
33a8d 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
33a8e 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  !=SQLITE_DONE ){
33a8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
33a90 65 73 65 74 28 73 29 3b 0a 20 20 20 20 20 20 67  eset(s);.      g
33a91 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
33a92 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72  ntext, "dump_ter
33a93 6d 73 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  ms", NULL);.    
33a94 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e  }else{.      con
33a95 73 74 20 69 6e 74 20 6e 54 65 72 6d 73 20 3d 20  st int nTerms = 
33a96 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28 26 74  fts3HashCount(&t
33a97 65 72 6d 73 29 3b 0a 20 20 20 20 20 20 69 66 28  erms);.      if(
33a98 20 6e 54 65 72 6d 73 3e 30 20 29 7b 0a 20 20 20   nTerms>0 ){.   
33a99 20 20 20 20 20 72 63 20 3d 20 67 65 6e 65 72 61       rc = genera
33a9a 74 65 54 65 72 6d 73 52 65 73 75 6c 74 28 70 43  teTermsResult(pC
33a9b 6f 6e 74 65 78 74 2c 20 26 74 65 72 6d 73 29 3b  ontext, &terms);
33a9c 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
33a9d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
33a9e 0a 20 20 20 20 20 20 20 20 20 20 67 65 6e 65 72  .          gener
33a9f 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
33aa0 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c  t, "dump_terms",
33aa1 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22   "out of memory"
33aa2 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
33aa3 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
33aa4 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
33aa5 4b 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  K );.        }. 
33aa6 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61       }else if( a
33aa7 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 20 20  rgc==3 ){.      
33aa8 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69    /* The specifi
33aa9 63 20 73 65 67 6d 65 6e 74 20 61 73 6b 65 64 20  c segment asked 
33aaa 66 6f 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  for could not be
33aab 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20   found. */.     
33aac 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
33aad 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
33aae 5f 74 65 72 6d 73 22 2c 20 22 73 65 67 6d 65 6e  _terms", "segmen
33aaf 74 20 6e 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20  t not found");. 
33ab0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
33ab1 20 20 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e      /* No segmen
33ab2 74 73 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20  ts found. */.   
33ab3 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65       /* TODO(she
33ab4 73 73 29 3a 20 49 74 20 73 68 6f 75 6c 64 20 62  ss): It should b
33ab5 65 20 69 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20  e impossible to 
33ab6 72 65 61 63 68 20 74 68 69 73 2e 20 20 54 68 69  reach this.  Thi
33ab7 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73  s.        ** cas
33ab8 65 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65  e can only happe
33ab9 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 74  n for an empty t
33aba 61 62 6c 65 2c 20 69 6e 20 77 68 69 63 68 20 63  able, in which c
33abb 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53  ase.        ** S
33abc 51 4c 69 74 65 20 68 61 73 20 6e 6f 20 72 6f 77  QLite has no row
33abd 73 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66  s to call this f
33abe 75 6e 63 74 69 6f 6e 20 6f 6e 2e 0a 20 20 20 20  unction on..    
33abf 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73      */.        s
33ac0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
33ac1 6c 6c 28 70 43 6f 6e 74 65 78 74 29 3b 0a 20 20  ll(pContext);.  
33ac2 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
33ac3 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43  sqlite3Fts3HashC
33ac4 6c 65 61 72 28 26 74 65 72 6d 73 29 3b 0a 20 20  lear(&terms);.  
33ac5 7d 0a 7d 0a 0a 2f 2a 20 45 78 70 61 6e 64 20 74  }.}../* Expand t
33ac6 68 65 20 44 4c 5f 44 45 46 41 55 4c 54 20 64 6f  he DL_DEFAULT do
33ac7 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61 20 69  clist in pData i
33ac8 6e 74 6f 20 61 20 74 65 78 74 20 72 65 73 75 6c  nto a text resul
33ac9 74 20 69 6e 0a 2a 2a 20 70 43 6f 6e 74 65 78 74  t in.** pContext
33aca 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
33acb 20 63 72 65 61 74 65 44 6f 63 6c 69 73 74 52 65   createDoclistRe
33acc 73 75 6c 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e  sult(sqlite3_con
33acd 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a  text *pContext,.
33ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33acf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ad0 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
33ad1 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20  a, int nData){. 
33ad2 20 44 61 74 61 42 75 66 66 65 72 20 64 75 6d 70   DataBuffer dump
33ad3 3b 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52  ;.  DLReader dlR
33ad4 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  eader;..  assert
33ad5 28 20 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26  ( pData!=NULL &&
33ad6 20 6e 44 61 74 61 3e 30 20 29 3b 0a 0a 20 20 64   nData>0 );..  d
33ad7 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64  ataBufferInit(&d
33ad8 75 6d 70 2c 20 30 29 3b 0a 20 20 64 6c 72 49 6e  ump, 0);.  dlrIn
33ad9 69 74 28 26 64 6c 52 65 61 64 65 72 2c 20 44 4c  it(&dlReader, DL
33ada 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2c  _DEFAULT, pData,
33adb 20 6e 44 61 74 61 29 3b 0a 20 20 66 6f 72 28 20   nData);.  for( 
33adc 3b 20 21 64 6c 72 41 74 45 6e 64 28 26 64 6c 52  ; !dlrAtEnd(&dlR
33add 65 61 64 65 72 29 3b 20 64 6c 72 53 74 65 70 28  eader); dlrStep(
33ade 26 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20  &dlReader) ){.  
33adf 20 20 63 68 61 72 20 62 75 66 5b 32 35 36 5d 3b    char buf[256];
33ae0 0a 20 20 20 20 50 4c 52 65 61 64 65 72 20 70 6c  .    PLReader pl
33ae1 52 65 61 64 65 72 3b 0a 0a 20 20 20 20 70 6c 72  Reader;..    plr
33ae2 49 6e 69 74 28 26 70 6c 52 65 61 64 65 72 2c 20  Init(&plReader, 
33ae3 26 64 6c 52 65 61 64 65 72 29 3b 0a 20 20 20 20  &dlReader);.    
33ae4 69 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d  if( DL_DEFAULT==
33ae5 44 4c 5f 44 4f 43 49 44 53 20 7c 7c 20 70 6c 72  DL_DOCIDS || plr
33ae6 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29  AtEnd(&plReader)
33ae7 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
33ae8 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
33ae9 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b 25  f(buf), buf, "[%
33aea 6c 6c 64 5d 20 22 2c 20 64 6c 72 44 6f 63 69 64  lld] ", dlrDocid
33aeb 28 26 64 6c 52 65 61 64 65 72 29 29 3b 0a 20 20  (&dlReader));.  
33aec 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
33aed 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c  pend(&dump, buf,
33aee 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 20   strlen(buf));. 
33aef 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
33af0 69 6e 74 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c  int iColumn = pl
33af1 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65  rColumn(&plReade
33af2 72 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  r);..      sqlit
33af3 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
33af4 6f 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b  of(buf), buf, "[
33af5 25 6c 6c 64 20 25 64 5b 22 2c 0a 20 20 20 20 20  %lld %d[",.     
33af6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33af7 20 20 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65    dlrDocid(&dlRe
33af8 61 64 65 72 29 2c 20 69 43 6f 6c 75 6d 6e 29 3b  ader), iColumn);
33af9 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65  .      dataBuffe
33afa 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 62  rAppend(&dump, b
33afb 75 66 2c 20 73 74 72 6c 65 6e 28 62 75 66 29 29  uf, strlen(buf))
33afc 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 20 3b 20  ;..      for( ; 
33afd 21 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61  !plrAtEnd(&plRea
33afe 64 65 72 29 3b 20 70 6c 72 53 74 65 70 28 26 70  der); plrStep(&p
33aff 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  lReader) ){.    
33b00 20 20 20 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d      if( plrColum
33b01 6e 28 26 70 6c 52 65 61 64 65 72 29 21 3d 69 43  n(&plReader)!=iC
33b02 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
33b03 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72     iColumn = plr
33b04 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
33b05 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
33b06 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
33b07 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c 20  zeof(buf), buf, 
33b08 22 5d 20 25 64 5b 22 2c 20 69 43 6f 6c 75 6d 6e  "] %d[", iColumn
33b09 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
33b0a 65 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e  ert( dump.nData>
33b0b 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64  0 );.          d
33b0c 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20  ump.nData--;    
33b0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b0e 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74 72   /* Overwrite tr
33b0f 61 69 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f  ailing space. */
33b10 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
33b11 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75  t( dump.pData[du
33b12 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b  mp.nData]==' ');
33b13 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42  .          dataB
33b14 75 66 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d  ufferAppend(&dum
33b15 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62  p, buf, strlen(b
33b16 75 66 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  uf));.        }.
33b17 20 20 20 20 20 20 20 20 69 66 28 20 44 4c 5f 44          if( DL_D
33b18 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54  EFAULT==DL_POSIT
33b19 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a  IONS_OFFSETS ){.
33b1a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
33b1b 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
33b1c 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 64  f(buf), buf, "%d
33b1d 2c 25 64 2c 25 64 20 22 2c 0a 20 20 20 20 20 20  ,%d,%d ",.      
33b1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b1f 20 20 20 20 20 70 6c 72 50 6f 73 69 74 69 6f 6e       plrPosition
33b20 28 26 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20  (&plReader),.   
33b21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b22 20 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74          plrStart
33b23 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64 65 72  Offset(&plReader
33b24 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  ), plrEndOffset(
33b25 26 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20  &plReader));.   
33b26 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 44       }else if( D
33b27 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f  L_DEFAULT==DL_PO
33b28 53 49 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20  SITIONS ){.     
33b29 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
33b2a 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
33b2b 29 2c 20 62 75 66 2c 20 22 25 64 20 22 2c 20 70  ), buf, "%d ", p
33b2c 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52 65  lrPosition(&plRe
33b2d 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20  ader));.        
33b2e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
33b2f 20 61 73 73 65 72 74 28 20 4e 55 4c 4c 3d 3d 22   assert( NULL=="
33b30 55 6e 68 61 6e 64 6c 65 64 20 44 4c 5f 44 45 46  Unhandled DL_DEF
33b31 41 55 4c 54 20 76 61 6c 75 65 22 29 3b 0a 20 20  AULT value");.  
33b32 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
33b33 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
33b34 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72  (&dump, buf, str
33b35 6c 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 20  len(buf));.     
33b36 20 7d 0a 20 20 20 20 20 20 70 6c 72 44 65 73 74   }.      plrDest
33b37 72 6f 79 28 26 70 6c 52 65 61 64 65 72 29 3b 0a  roy(&plReader);.
33b38 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
33b39 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20  ump.nData>0 );. 
33b3a 20 20 20 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d       dump.nData-
33b3b 2d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  -;              
33b3c 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72         /* Overwr
33b3d 69 74 65 20 74 72 61 69 6c 69 6e 67 20 73 70 61  ite trailing spa
33b3e 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  ce. */.      ass
33b3f 65 72 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b  ert( dump.pData[
33b40 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27  dump.nData]==' '
33b41 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  );.      dataBuf
33b42 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c  ferAppend(&dump,
33b43 20 22 5d 5d 20 22 2c 20 33 29 3b 0a 20 20 20 20   "]] ", 3);.    
33b44 7d 0a 20 20 7d 0a 20 20 64 6c 72 44 65 73 74 72  }.  }.  dlrDestr
33b45 6f 79 28 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a  oy(&dlReader);..
33b46 20 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e    assert( dump.n
33b47 44 61 74 61 3e 30 20 29 3b 0a 20 20 64 75 6d 70  Data>0 );.  dump
33b48 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20  .nData--;       
33b49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33b4a 20 4f 76 65 72 77 72 69 74 65 20 74 72 61 69 6c   Overwrite trail
33b4b 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20  ing space. */.  
33b4c 61 73 73 65 72 74 28 20 64 75 6d 70 2e 70 44 61  assert( dump.pDa
33b4d 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d  ta[dump.nData]==
33b4e 27 20 27 29 3b 0a 20 20 64 75 6d 70 2e 70 44 61  ' ');.  dump.pDa
33b4f 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 20 3d  ta[dump.nData] =
33b50 20 27 5c 30 27 3b 0a 20 20 61 73 73 65 72 74 28   '\0';.  assert(
33b51 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b   dump.nData>0 );
33b52 0a 0a 20 20 2f 2a 20 50 61 73 73 65 73 20 6f 77  ..  /* Passes ow
33b53 6e 65 72 73 68 69 70 20 6f 66 20 64 75 6d 70 27  nership of dump'
33b54 73 20 62 75 66 66 65 72 20 74 6f 20 70 43 6f 6e  s buffer to pCon
33b55 74 65 78 74 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  text. */.  sqlit
33b56 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
33b57 43 6f 6e 74 65 78 74 2c 20 64 75 6d 70 2e 70 44  Context, dump.pD
33b58 61 74 61 2c 20 64 75 6d 70 2e 6e 44 61 74 61 2c  ata, dump.nData,
33b59 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
33b5a 20 20 64 75 6d 70 2e 70 44 61 74 61 20 3d 20 4e    dump.pData = N
33b5b 55 4c 4c 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74  ULL;.  dump.nDat
33b5c 61 20 3d 20 64 75 6d 70 2e 6e 43 61 70 61 63 69  a = dump.nCapaci
33b5d 74 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6d  ty = 0;.}../* Im
33b5e 70 6c 65 6d 65 6e 74 73 20 64 75 6d 70 5f 64 6f  plements dump_do
33b5f 63 6c 69 73 74 28 29 20 66 6f 72 20 75 73 65 20  clist() for use 
33b60 69 6e 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68  in inspecting th
33b61 65 20 66 74 73 33 20 69 6e 64 65 78 20 66 72 6f  e fts3 index fro
33b62 6d 0a 2a 2a 20 74 65 73 74 73 2e 20 20 54 45 58  m.** tests.  TEX
33b63 54 20 72 65 73 75 6c 74 20 63 6f 6e 74 61 69 6e  T result contain
33b64 69 6e 67 20 61 20 73 74 72 69 6e 67 20 72 65 70  ing a string rep
33b65 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
33b66 68 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f  he.** doclist fo
33b67 72 20 74 68 65 20 69 6e 64 69 63 61 74 65 64 20  r the indicated 
33b68 74 65 72 6d 2e 20 20 64 75 6d 70 5f 64 6f 63 6c  term.  dump_docl
33b69 69 73 74 28 74 2c 20 74 65 72 6d 2c 20 6c 65 76  ist(t, term, lev
33b6a 65 6c 2c 20 69 64 78 29 0a 2a 2a 20 64 75 6d 70  el, idx).** dump
33b6b 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f  s the doclist fo
33b6c 72 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65 20  r term from the 
33b6d 73 65 67 6d 65 6e 74 20 73 70 65 63 69 66 69 65  segment specifie
33b6e 64 20 62 79 20 6c 65 76 65 6c 2c 20 69 64 78 0a  d by level, idx.
33b6f 2a 2a 20 28 69 6e 20 25 5f 73 65 67 64 69 72 29  ** (in %_segdir)
33b70 2c 20 77 68 69 6c 65 20 64 75 6d 70 5f 64 6f 63  , while dump_doc
33b71 6c 69 73 74 28 74 2c 20 74 65 72 6d 29 20 64 75  list(t, term) du
33b72 6d 70 73 20 74 68 65 20 6c 6f 67 69 63 61 6c 0a  mps the logical.
33b73 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74  ** doclist for t
33b74 68 65 20 74 65 72 6d 20 61 63 72 6f 73 73 20 61  he term across a
33b75 6c 6c 20 73 65 67 6d 65 6e 74 73 2e 20 20 54 68  ll segments.  Th
33b76 65 20 70 65 72 2d 73 65 67 6d 65 6e 74 20 64 6f  e per-segment do
33b77 63 6c 69 73 74 0a 2a 2a 20 63 61 6e 20 63 6f 6e  clist.** can con
33b78 74 61 69 6e 20 64 65 6c 65 74 69 6f 6e 73 2c 20  tain deletions, 
33b79 77 68 69 6c 65 20 74 68 65 20 66 75 6c 6c 2d 69  while the full-i
33b7a 6e 64 65 78 20 64 6f 63 6c 69 73 74 20 77 69 6c  ndex doclist wil
33b7b 6c 20 6e 6f 74 0a 2a 2a 20 28 64 65 6c 65 74 69  l not.** (deleti
33b7c 6f 6e 73 20 61 72 65 20 6f 6d 69 74 74 65 64 29  ons are omitted)
33b7d 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 75 6c 74 20 66  ..**.** Result f
33b7e 6f 72 6d 61 74 73 20 64 69 66 66 65 72 20 77 69  ormats differ wi
33b7f 74 68 20 74 68 65 20 73 65 74 74 69 6e 67 20 6f  th the setting o
33b80 66 20 44 4c 5f 44 45 46 41 55 4c 54 53 2e 20 20  f DL_DEFAULTS.  
33b81 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  Examples:.**.** 
33b82 44 4c 5f 44 4f 43 49 44 53 3a 20 5b 31 5d 20 5b  DL_DOCIDS: [1] [
33b83 33 5d 20 5b 37 5d 0a 2a 2a 20 44 4c 5f 50 4f 53  3] [7].** DL_POS
33b84 49 54 49 4f 4e 53 3a 20 5b 31 20 30 5b 30 20 34  ITIONS: [1 0[0 4
33b85 5d 20 31 5b 31 37 5d 5d 20 5b 33 20 31 5b 35 5d  ] 1[17]] [3 1[5]
33b86 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  ].** DL_POSITION
33b87 53 5f 4f 46 46 53 45 54 53 3a 20 5b 31 20 30 5b  S_OFFSETS: [1 0[
33b88 30 2c 30 2c 33 20 34 2c 32 33 2c 32 36 5d 20 31  0,0,3 4,23,26] 1
33b89 5b 31 37 2c 31 30 32 2c 31 30 35 5d 5d 20 5b 33  [17,102,105]] [3
33b8a 20 31 5b 35 2c 32 30 2c 32 33 5d 5d 0a 2a 2a 0a   1[5,20,23]].**.
33b8b 2a 2a 20 49 6e 20 65 61 63 68 20 63 61 73 65 20  ** In each case 
33b8c 74 68 65 20 6e 75 6d 62 65 72 20 61 66 74 65 72  the number after
33b8d 20 74 68 65 20 6f 75 74 65 72 20 27 5b 27 20 69   the outer '[' i
33b8e 73 20 74 68 65 20 64 6f 63 69 64 2e 20 20 49 6e  s the docid.  In
33b8f 20 74 68 65 0a 2a 2a 20 6c 61 74 74 65 72 20 74   the.** latter t
33b90 77 6f 20 63 61 73 65 73 2c 20 74 68 65 20 6e 75  wo cases, the nu
33b91 6d 62 65 72 20 62 65 66 6f 72 65 20 74 68 65 20  mber before the 
33b92 69 6e 6e 65 72 20 27 5b 27 20 69 73 20 74 68 65  inner '[' is the
33b93 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 73 73 6f 63   column.** assoc
33b94 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 76  iated with the v
33b95 61 6c 75 65 73 20 77 69 74 68 69 6e 2e 20 20 46  alues within.  F
33b96 6f 72 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20  or DL_POSITIONS 
33b97 74 68 65 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77  the numbers.** w
33b98 69 74 68 69 6e 20 61 72 65 20 74 68 65 20 70 6f  ithin are the po
33b99 73 69 74 69 6f 6e 73 2c 20 66 6f 72 20 44 4c 5f  sitions, for DL_
33b9a 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
33b9b 53 20 74 68 65 79 20 61 72 65 20 74 68 65 0a 2a  S they are the.*
33b9c 2a 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20  * position, the 
33b9d 73 74 61 72 74 20 6f 66 66 73 65 74 2c 20 61 6e  start offset, an
33b9e 64 20 74 68 65 20 65 6e 64 20 6f 66 66 73 65 74  d the end offset
33b9f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
33ba0 20 64 75 6d 70 44 6f 63 6c 69 73 74 46 75 6e 63   dumpDoclistFunc
33ba1 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
33ba2 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20  ext *pContext,. 
33ba3 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
33ba4 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
33ba5 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
33ba6 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20  rsor *pCursor;. 
33ba7 20 69 66 28 20 61 72 67 63 21 3d 32 20 26 26 20   if( argc!=2 && 
33ba8 61 72 67 63 21 3d 34 20 29 7b 0a 20 20 20 20 67  argc!=4 ){.    g
33ba9 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
33baa 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63  ntext, "dump_doc
33bab 6c 69 73 74 22 2c 20 22 69 6e 63 6f 72 72 65 63  list", "incorrec
33bac 74 20 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20  t arguments");. 
33bad 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
33bae 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
33baf 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42  gv[0])!=SQLITE_B
33bb0 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  LOB ||.         
33bb1 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
33bb2 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21  _bytes(argv[0])!
33bb3 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29  =sizeof(pCursor)
33bb4 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65   ){.    generate
33bb5 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
33bb6 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20  "dump_doclist", 
33bb7 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61  "illegal first a
33bb8 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c  rgument");.  }el
33bb9 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  se if( sqlite3_v
33bba 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
33bbb 5d 29 3d 3d 4e 55 4c 4c 20 7c 7c 0a 20 20 20 20  ])==NULL ||.    
33bbc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
33bbd 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
33bbe 31 5d 29 5b 30 5d 3d 3d 27 5c 30 27 20 29 7b 0a  1])[0]=='\0' ){.
33bbf 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
33bc0 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
33bc1 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22 65 6d 70  p_doclist", "emp
33bc2 74 79 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  ty second argume
33bc3 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nt");.  }else{. 
33bc4 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
33bc5 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 63 68  Term = (const ch
33bc6 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
33bc7 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
33bc8 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  ;.    const int 
33bc9 6e 54 65 72 6d 20 3d 20 73 74 72 6c 65 6e 28 70  nTerm = strlen(p
33bca 54 65 72 6d 29 3b 0a 20 20 20 20 66 75 6c 6c 74  Term);.    fullt
33bcb 65 78 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20  ext_vtab *v;.   
33bcc 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 44 61 74   int rc;.    Dat
33bcd 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74 3b  aBuffer doclist;
33bce 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43  ..    memcpy(&pC
33bcf 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76  ursor, sqlite3_v
33bd0 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
33bd1 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73  ]), sizeof(pCurs
33bd2 6f 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63 75  or));.    v = cu
33bd3 72 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73 6f  rsor_vtab(pCurso
33bd4 72 29 3b 0a 0a 20 20 20 20 64 61 74 61 42 75 66  r);..    dataBuf
33bd5 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74  ferInit(&doclist
33bd6 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 74 65  , 0);..    /* te
33bd7 72 6d 53 65 6c 65 63 74 28 29 20 79 69 65 6c 64  rmSelect() yield
33bd8 73 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63  s the same logic
33bd9 61 6c 20 64 6f 63 6c 69 73 74 20 74 68 61 74 20  al doclist that 
33bda 71 75 65 72 69 65 73 20 61 72 65 0a 20 20 20 20  queries are.    
33bdb 2a 2a 20 72 75 6e 20 61 67 61 69 6e 73 74 2e 0a  ** run against..
33bdc 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61      */.    if( a
33bdd 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  rgc==2 ){.      
33bde 72 63 20 3d 20 74 65 72 6d 53 65 6c 65 63 74 28  rc = termSelect(
33bdf 76 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 70  v, v->nColumn, p
33be0 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c 20  Term, nTerm, 0, 
33be1 44 4c 5f 44 45 46 41 55 4c 54 2c 20 26 64 6f 63  DL_DEFAULT, &doc
33be2 6c 69 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  list);.    }else
33be3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
33be4 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a  stmt *s = NULL;.
33be5 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 6f 75  .      /* Get ou
33be6 72 20 73 70 65 63 69 66 69 63 20 73 65 67 6d 65  r specific segme
33be7 6e 74 27 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  nt's information
33be8 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  . */.      rc = 
33be9 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
33bea 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45  t(v, SEGDIR_SELE
33beb 43 54 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c  CT_SEGMENT_STMT,
33bec 20 26 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20   &s);.      if( 
33bed 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
33bee 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
33bef 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73  lite3_bind_int(s
33bf0 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  , 1, sqlite3_val
33bf1 75 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29  ue_int(argv[2]))
33bf2 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
33bf3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
33bf4 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
33bf5 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73  lite3_bind_int(s
33bf6 2c 20 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  , 2, sqlite3_val
33bf7 75 65 5f 69 6e 74 28 61 72 67 76 5b 33 5d 29 29  ue_int(argv[3]))
33bf8 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
33bf9 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72    }..      if( r
33bfa 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
33bfb 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
33bfc 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20  ite3_step(s);.. 
33bfd 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
33bfe 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
33bff 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
33c00 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69  erDestroy(&docli
33c01 73 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67  st);.          g
33c02 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
33c03 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63  ntext, "dump_doc
33c04 6c 69 73 74 22 2c 20 22 73 65 67 6d 65 6e 74 20  list", "segment 
33c05 6e 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20  not found");.   
33c06 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
33c07 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
33c08 20 20 2f 2a 20 46 6f 75 6e 64 20 61 20 73 65 67    /* Found a seg
33c09 6d 65 6e 74 2c 20 6c 6f 61 64 20 69 74 20 69 6e  ment, load it in
33c0a 74 6f 20 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20  to doclist. */. 
33c0b 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
33c0c 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
33c0d 20 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c         const sql
33c0e 69 74 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65  ite_int64 iLeave
33c0f 73 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63  sEnd = sqlite3_c
33c10 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31  olumn_int64(s, 1
33c11 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  );.          con
33c12 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d  st char *pData =
33c13 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
33c14 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20  blob(s, 2);.    
33c15 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
33c16 6e 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f  nData = sqlite3_
33c17 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20  column_bytes(s, 
33c18 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  2);..          /
33c19 2a 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20  * loadSegment() 
33c1a 69 73 20 75 73 65 64 20 62 79 20 74 65 72 6d 53  is used by termS
33c1b 65 6c 65 63 74 28 29 20 74 6f 20 6c 6f 61 64 20  elect() to load 
33c1c 65 61 63 68 0a 20 20 20 20 20 20 20 20 20 20 2a  each.          *
33c1d 2a 20 73 65 67 6d 65 6e 74 27 73 20 64 61 74 61  * segment's data
33c1e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
33c1f 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f           rc = lo
33c20 61 64 53 65 67 6d 65 6e 74 28 76 2c 20 70 44 61  adSegment(v, pDa
33c21 74 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61 76  ta, nData, iLeav
33c22 65 73 45 6e 64 2c 20 70 54 65 72 6d 2c 20 6e 54  esEnd, pTerm, nT
33c23 65 72 6d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  erm, 0,.        
33c24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c25 20 20 20 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20     &doclist);.  
33c26 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
33c27 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
33c28 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
33c29 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a  lite3_step(s);..
33c2a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
33c2b 68 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 6d  hould not have m
33c2c 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6d 61 74  ore than one mat
33c2d 63 68 69 6e 67 20 73 65 67 6d 65 6e 74 2e 20 2a  ching segment. *
33c2e 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  /.            if
33c2f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
33c30 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  E ){.           
33c31 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
33c32 28 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  (s);.           
33c33 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
33c34 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a  troy(&doclist);.
33c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 65                ge
33c36 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
33c37 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c  text, "dump_docl
33c38 69 73 74 22 2c 20 22 69 6e 76 61 6c 69 64 20 73  ist", "invalid s
33c39 65 67 64 69 72 22 29 3b 0a 20 20 20 20 20 20 20  egdir");.       
33c3a 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
33c3b 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
33c3c 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
33c3d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
33c3e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
33c3f 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73 71       }..      sq
33c40 6c 69 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a  lite3_reset(s);.
33c41 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72      }..    if( r
33c42 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
33c43 20 20 20 20 20 20 69 66 28 20 64 6f 63 6c 69 73        if( doclis
33c44 74 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20  t.nData>0 ){.   
33c45 20 20 20 20 20 63 72 65 61 74 65 44 6f 63 6c 69       createDocli
33c46 73 74 52 65 73 75 6c 74 28 70 43 6f 6e 74 65 78  stResult(pContex
33c47 74 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61  t, doclist.pData
33c48 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29  , doclist.nData)
33c49 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
33c4a 20 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73         /* TODO(s
33c4b 68 65 73 73 29 3a 20 54 68 69 73 20 63 61 6e 20  hess): This can 
33c4c 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 74 65  happen if the te
33c4d 72 6d 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e  rm is not presen
33c4e 74 2c 20 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a  t, or.        **
33c4f 20 69 66 20 61 6c 6c 20 69 6e 73 74 61 6e 63 65   if all instance
33c50 73 20 6f 66 20 74 68 65 20 74 65 72 6d 20 68 61  s of the term ha
33c51 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20  ve been deleted 
33c52 61 6e 64 20 74 68 69 73 20 69 73 0a 20 20 20 20  and this is.    
33c53 20 20 20 20 2a 2a 20 61 6e 20 61 6c 6c 2d 69 6e      ** an all-in
33c54 64 65 78 20 64 75 6d 70 2e 20 20 49 74 20 6d 61  dex dump.  It ma
33c55 79 20 62 65 20 69 6e 74 65 72 65 73 74 69 6e 67  y be interesting
33c56 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 0a   to distinguish.
33c57 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 73 65          ** these
33c58 20 63 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20   cases..        
33c59 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
33c5a 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
33c5b 43 6f 6e 74 65 78 74 2c 20 22 22 2c 20 30 2c 20  Context, "", 0, 
33c5c 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
33c5d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
33c5e 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45  e if( rc==SQLITE
33c5f 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
33c60 2f 2a 20 48 61 6e 64 6c 65 20 6f 75 74 2d 6f 66  /* Handle out-of
33c61 2d 6d 65 6d 6f 72 79 20 63 61 73 65 73 20 73 70  -memory cases sp
33c62 65 63 69 61 6c 6c 79 20 62 65 63 61 75 73 65 20  ecially because 
33c63 69 66 20 74 68 65 79 20 61 72 65 0a 20 20 20 20  if they are.    
33c64 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 69    ** generated i
33c65 6e 20 66 74 73 33 20 63 6f 64 65 20 74 68 65 79  n fts3 code they
33c66 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 66 6c   may not be refl
33c67 65 63 74 65 64 20 69 6e 20 74 68 65 20 64 62 0a  ected in the db.
33c68 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2e        ** handle.
33c69 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
33c6a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
33c6b 48 61 6e 64 6c 65 20 74 68 69 73 20 6d 6f 72 65  Handle this more
33c6c 20 63 6f 6d 70 72 65 68 65 6e 73 69 76 65 6c 79   comprehensively
33c6d 2e 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74  ..      ** sqlit
33c6e 65 33 45 72 72 53 74 72 28 29 20 68 61 73 20 77  e3ErrStr() has w
33c6f 68 61 74 20 49 20 6e 65 65 64 2c 20 62 75 74 20  hat I need, but 
33c70 69 73 20 69 6e 74 65 72 6e 61 6c 2e 0a 20 20 20  is internal..   
33c71 20 20 20 2a 2f 0a 20 20 20 20 20 20 67 65 6e 65     */.      gene
33c72 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
33c73 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  xt, "dump_doclis
33c74 74 22 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  t", "out of memo
33c75 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ry");.    }else{
33c76 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45  .      generateE
33c77 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
33c78 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 4e  dump_doclist", N
33c79 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ULL);.    }..   
33c7a 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
33c7b 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20  oy(&doclist);.  
33c7c 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
33c7d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
33c7e 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 46  mplements the xF
33c7f 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68  indFunction meth
33c80 6f 64 20 66 6f 72 20 74 68 65 20 46 54 53 33 0a  od for the FTS3.
33c81 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
33c82 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
33c83 66 75 6c 6c 74 65 78 74 46 69 6e 64 46 75 6e 63  fulltextFindFunc
33c84 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  tion(.  sqlite3_
33c85 76 74 61 62 20 2a 70 56 74 61 62 2c 0a 20 20 69  vtab *pVtab,.  i
33c86 6e 74 20 6e 41 72 67 2c 0a 20 20 63 6f 6e 73 74  nt nArg,.  const
33c87 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20   char *zName,.  
33c88 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28  void (**pxFunc)(
33c89 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
33c8a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
33c8b 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 2a  ue**),.  void **
33c8c 70 70 41 72 67 0a 29 7b 0a 20 20 69 66 28 20 73  ppArg.){.  if( s
33c8d 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 73 6e 69  trcmp(zName,"sni
33c8e 70 70 65 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20  ppet")==0 ){.   
33c8f 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e 69 70 70   *pxFunc = snipp
33c90 65 74 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75  etFunc;.    retu
33c91 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
33c92 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22  ( strcmp(zName,"
33c93 6f 66 66 73 65 74 73 22 29 3d 3d 30 20 29 7b 0a  offsets")==0 ){.
33c94 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e      *pxFunc = sn
33c95 69 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e 63  ippetOffsetsFunc
33c96 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
33c97 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
33c98 6d 70 28 7a 4e 61 6d 65 2c 22 6f 70 74 69 6d 69  mp(zName,"optimi
33c99 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  ze")==0 ){.    *
33c9a 70 78 46 75 6e 63 20 3d 20 6f 70 74 69 6d 69 7a  pxFunc = optimiz
33c9b 65 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72  eFunc;.    retur
33c9c 6e 20 31 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  n 1;.#ifdef SQLI
33c9d 54 45 5f 54 45 53 54 0a 20 20 20 20 2f 2a 20 4e  TE_TEST.    /* N
33c9e 4f 54 45 28 73 68 65 73 73 29 3a 20 54 68 65 73  OTE(shess): Thes
33c9f 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
33ca0 70 72 65 73 65 6e 74 20 6f 6e 6c 79 20 66 6f 72  present only for
33ca1 20 74 65 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20   testing.    ** 
33ca2 70 75 72 70 6f 73 65 73 2e 20 20 4e 6f 20 70 61  purposes.  No pa
33ca3 72 74 69 63 75 6c 61 72 20 65 66 66 6f 72 74 20  rticular effort 
33ca4 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 74 69 6d  is made to optim
33ca5 69 7a 65 20 74 68 65 69 72 0a 20 20 20 20 2a 2a  ize their.    **
33ca6 20 65 78 65 63 75 74 69 6f 6e 20 6f 72 20 68 6f   execution or ho
33ca7 77 20 74 68 65 79 20 62 75 69 6c 64 20 74 68 65  w they build the
33ca8 69 72 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20  ir results..    
33ca9 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  */.  }else if( s
33caa 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 64 75 6d  trcmp(zName,"dum
33cab 70 5f 74 65 72 6d 73 22 29 3d 3d 30 20 29 7b 0a  p_terms")==0 ){.
33cac 20 20 20 20 2f 2a 20 66 70 72 69 6e 74 66 28 73      /* fprintf(s
33cad 74 64 65 72 72 2c 20 22 46 6f 75 6e 64 20 64 75  tderr, "Found du
33cae 6d 70 5f 74 65 72 6d 73 5c 6e 22 29 3b 20 2a 2f  mp_terms\n"); */
33caf 0a 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 64  .    *pxFunc = d
33cb0 75 6d 70 54 65 72 6d 73 46 75 6e 63 3b 0a 20 20  umpTermsFunc;.  
33cb1 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
33cb2 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a  lse if( strcmp(z
33cb3 4e 61 6d 65 2c 22 64 75 6d 70 5f 64 6f 63 6c 69  Name,"dump_docli
33cb4 73 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  st")==0 ){.    /
33cb5 2a 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72  * fprintf(stderr
33cb6 2c 20 22 46 6f 75 6e 64 20 64 75 6d 70 5f 64 6f  , "Found dump_do
33cb7 63 6c 69 73 74 5c 6e 22 29 3b 20 2a 2f 0a 20 20  clist\n"); */.  
33cb8 20 20 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d 70    *pxFunc = dump
33cb9 44 6f 63 6c 69 73 74 46 75 6e 63 3b 0a 20 20 20  DoclistFunc;.   
33cba 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69   return 1;.#endi
33cbb 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  f.  }.  return 0
33cbc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6e 61 6d  ;.}../*.** Renam
33cbd 65 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 2e  e an fts3 table.
33cbe 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
33cbf 75 6c 6c 74 65 78 74 52 65 6e 61 6d 65 28 0a 20  ulltextRename(. 
33cc0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
33cc1 56 74 61 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Vtab,.  const ch
33cc2 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 66  ar *zName.){.  f
33cc3 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 20  ulltext_vtab *p 
33cc4 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  = (fulltext_vtab
33cc5 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20   *)pVtab;.  int 
33cc6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
33cc7 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20  M;.  char *zSql 
33cc8 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
33cc9 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41  f(.    "ALTER TA
33cca 42 4c 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65  BLE %Q.'%q_conte
33ccb 6e 74 27 20 20 52 45 4e 41 4d 45 20 54 4f 20 27  nt'  RENAME TO '
33ccc 25 71 5f 63 6f 6e 74 65 6e 74 27 3b 22 0a 20 20  %q_content';".  
33ccd 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
33cce 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 20  Q.'%q_segments' 
33ccf 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73 65  RENAME TO '%q_se
33cd0 67 6d 65 6e 74 73 27 3b 22 0a 20 20 20 20 22 41  gments';".    "A
33cd1 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
33cd2 71 5f 73 65 67 64 69 72 27 20 20 20 52 45 4e 41  q_segdir'   RENA
33cd3 4d 45 20 54 4f 20 27 25 71 5f 73 65 67 64 69 72  ME TO '%q_segdir
33cd4 27 3b 22 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62  ';".    , p->zDb
33cd5 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  , p->zName, zNam
33cd6 65 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c  e .    , p->zDb,
33cd7 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65   p->zName, zName
33cd8 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20   .    , p->zDb, 
33cd9 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a  p->zName, zName.
33cda 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20    );.  if( zSql 
33cdb 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
33cdc 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20  te3_exec(p->db, 
33cdd 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  zSql, 0, 0, 0);.
33cde 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
33cdf 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65  (zSql);.  }.  re
33ce0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
33ce1 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
33ce2 5f 6d 6f 64 75 6c 65 20 66 74 73 33 4d 6f 64 75  _module fts3Modu
33ce3 6c 65 20 3d 20 7b 0a 20 20 2f 2a 20 69 56 65 72  le = {.  /* iVer
33ce4 73 69 6f 6e 20 20 20 20 20 20 2a 2f 20 30 2c 0a  sion      */ 0,.
33ce5 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 20 20    /* xCreate    
33ce6 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 72     */ fulltextCr
33ce7 65 61 74 65 2c 0a 20 20 2f 2a 20 78 43 6f 6e 6e  eate,.  /* xConn
33ce8 65 63 74 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c  ect      */ full
33ce9 74 65 78 74 43 6f 6e 6e 65 63 74 2c 0a 20 20 2f  textConnect,.  /
33cea 2a 20 78 42 65 73 74 49 6e 64 65 78 20 20 20 20  * xBestIndex    
33ceb 2a 2f 20 66 75 6c 6c 74 65 78 74 42 65 73 74 49  */ fulltextBestI
33cec 6e 64 65 78 2c 0a 20 20 2f 2a 20 78 44 69 73 63  ndex,.  /* xDisc
33ced 6f 6e 6e 65 63 74 20 20 20 2a 2f 20 66 75 6c 6c  onnect   */ full
33cee 74 65 78 74 44 69 73 63 6f 6e 6e 65 63 74 2c 0a  textDisconnect,.
33cef 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 20 20    /* xDestroy   
33cf0 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 44 65     */ fulltextDe
33cf1 73 74 72 6f 79 2c 0a 20 20 2f 2a 20 78 4f 70 65  stroy,.  /* xOpe
33cf2 6e 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  n         */ ful
33cf3 6c 74 65 78 74 4f 70 65 6e 2c 0a 20 20 2f 2a 20  ltextOpen,.  /* 
33cf4 78 43 6c 6f 73 65 20 20 20 20 20 20 20 20 2a 2f  xClose        */
33cf5 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 2c 0a   fulltextClose,.
33cf6 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 20 20 20    /* xFilter    
33cf7 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69     */ fulltextFi
33cf8 6c 74 65 72 2c 0a 20 20 2f 2a 20 78 4e 65 78 74  lter,.  /* xNext
33cf9 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c           */ full
33cfa 74 65 78 74 4e 65 78 74 2c 0a 20 20 2f 2a 20 78  textNext,.  /* x
33cfb 45 6f 66 20 20 20 20 20 20 20 20 20 20 2a 2f 20  Eof          */ 
33cfc 66 75 6c 6c 74 65 78 74 45 6f 66 2c 0a 20 20 2f  fulltextEof,.  /
33cfd 2a 20 78 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20  * xColumn       
33cfe 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75 6d  */ fulltextColum
33cff 6e 2c 0a 20 20 2f 2a 20 78 52 6f 77 69 64 20 20  n,.  /* xRowid  
33d00 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
33d01 74 52 6f 77 69 64 2c 0a 20 20 2f 2a 20 78 55 70  tRowid,.  /* xUp
33d02 64 61 74 65 20 20 20 20 20 20 20 2a 2f 20 66 75  date       */ fu
33d03 6c 6c 74 65 78 74 55 70 64 61 74 65 2c 0a 20 20  lltextUpdate,.  
33d04 2f 2a 20 78 42 65 67 69 6e 20 20 20 20 20 20 20  /* xBegin       
33d05 20 2a 2f 20 66 75 6c 6c 74 65 78 74 42 65 67 69   */ fulltextBegi
33d06 6e 2c 0a 20 20 2f 2a 20 78 53 79 6e 63 20 20 20  n,.  /* xSync   
33d07 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
33d08 74 53 79 6e 63 2c 0a 20 20 2f 2a 20 78 43 6f 6d  tSync,.  /* xCom
33d09 6d 69 74 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  mit       */ ful
33d0a 6c 74 65 78 74 43 6f 6d 6d 69 74 2c 0a 20 20 2f  ltextCommit,.  /
33d0b 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 20 20 20 20  * xRollback     
33d0c 2a 2f 20 66 75 6c 6c 74 65 78 74 52 6f 6c 6c 62  */ fulltextRollb
33d0d 61 63 6b 2c 0a 20 20 2f 2a 20 78 46 69 6e 64 46  ack,.  /* xFindF
33d0e 75 6e 63 74 69 6f 6e 20 2a 2f 20 66 75 6c 6c 74  unction */ fullt
33d0f 65 78 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 2c  extFindFunction,
33d10 0a 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20 2a 2f  .  /* xRename */
33d11 20 20 20 20 20 20 20 66 75 6c 6c 74 65 78 74 52         fulltextR
33d12 65 6e 61 6d 65 2c 0a 7d 3b 0a 0a 73 74 61 74 69  ename,.};..stati
33d13 63 20 76 6f 69 64 20 68 61 73 68 44 65 73 74 72  c void hashDestr
33d14 6f 79 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 66  oy(void *p){.  f
33d15 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 20 3d  ts3Hash *pHash =
33d16 20 28 66 74 73 33 48 61 73 68 20 2a 29 70 3b 0a   (fts3Hash *)p;.
33d17 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73    sqlite3Fts3Has
33d18 68 43 6c 65 61 72 28 70 48 61 73 68 29 3b 0a 20  hClear(pHash);. 
33d19 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48   sqlite3_free(pH
33d1a 61 73 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ash);.}../*.** T
33d1b 68 65 20 66 74 73 33 20 62 75 69 6c 74 2d 69 6e  he fts3 built-in
33d1c 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2d 20 22 73   tokenizers - "s
33d1d 69 6d 70 6c 65 22 20 61 6e 64 20 22 70 6f 72 74  imple" and "port
33d1e 65 72 22 20 2d 20 61 72 65 20 69 6d 70 6c 65 6d  er" - are implem
33d1f 65 6e 74 65 64 0a 2a 2a 20 69 6e 20 66 69 6c 65  ented.** in file
33d20 73 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  s fts3_tokenizer
33d21 31 2e 63 20 61 6e 64 20 66 74 73 33 5f 70 6f 72  1.c and fts3_por
33d22 74 65 72 2e 63 20 72 65 73 70 65 63 74 69 76 65  ter.c respective
33d23 6c 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ly. The followin
33d24 67 0a 2a 2a 20 74 77 6f 20 66 6f 72 77 61 72 64  g.** two forward
33d25 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 72   declarations ar
33d26 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20  e for functions 
33d27 64 65 63 6c 61 72 65 64 20 69 6e 20 74 68 65 73  declared in thes
33d28 65 20 66 69 6c 65 73 0a 2a 2a 20 75 73 65 64 20  e files.** used 
33d29 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20  to retrieve the 
33d2a 72 65 73 70 65 63 74 69 76 65 20 69 6d 70 6c 65  respective imple
33d2b 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  mentations..**.*
33d2c 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  * Calling sqlite
33d2d 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e  3Fts3SimpleToken
33d2e 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73 65 74  izerModule() set
33d2f 73 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e  s the value poin
33d30 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65  ted.** to by the
33d31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 6f 69   argument to poi
33d32 6e 74 20 61 20 74 68 65 20 22 73 69 6d 70 6c 65  nt a the "simple
33d33 22 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  " tokenizer impl
33d34 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 46  ementation..** F
33d35 75 6e 63 74 69 6f 6e 20 2e 2e 2e 50 6f 72 74 65  unction ...Porte
33d36 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
33d37 28 29 20 73 65 74 73 20 2a 70 4d 6f 64 75 6c 65  () sets *pModule
33d38 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
33d39 0a 2a 2a 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e  .** porter token
33d3a 69 7a 65 72 2f 73 74 65 6d 6d 65 72 20 69 6d 70  izer/stemmer imp
33d3b 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a  lementation..*/.
33d3c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
33d3d 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
33d3e 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
33d3f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
33d40 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
33d41 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
33d42 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
33d43 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 50  oid sqlite3Fts3P
33d44 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f  orterTokenizerMo
33d45 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
33d46 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
33d47 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
33d48 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
33d49 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 49  oid sqlite3Fts3I
33d4a 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  cuTokenizerModul
33d4b 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
33d4c 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
33d4d 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 53 51  **ppModule);..SQ
33d4e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
33d4f 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
33d50 48 61 73 68 54 61 62 6c 65 28 73 71 6c 69 74 65  HashTable(sqlite
33d51 33 20 2a 2c 20 66 74 73 33 48 61 73 68 20 2a 2c  3 *, fts3Hash *,
33d52 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
33d53 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 73  ./*.** Initialis
33d54 65 20 74 68 65 20 66 74 73 33 20 65 78 74 65 6e  e the fts3 exten
33d55 73 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 65 78  sion. If this ex
33d56 74 65 6e 73 69 6f 6e 20 69 73 20 62 75 69 6c 74  tension is built
33d57 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74   as part.** of t
33d58 68 65 20 73 71 6c 69 74 65 20 6c 69 62 72 61 72  he sqlite librar
33d59 79 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  y, then this fun
33d5a 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
33d5b 64 69 72 65 63 74 6c 79 20 62 79 0a 2a 2a 20 53  directly by.** S
33d5c 51 4c 69 74 65 2e 20 49 66 20 66 74 73 33 20 69  QLite. If fts3 i
33d5d 73 20 62 75 69 6c 74 20 61 73 20 61 20 64 79 6e  s built as a dyn
33d5e 61 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 61 62 6c  amically loadabl
33d5f 65 20 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69  e extension, thi
33d60 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  s.** function is
33d61 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 73   called by the s
33d62 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e  qlite3_extension
33d63 5f 69 6e 69 74 28 29 20 65 6e 74 72 79 20 70 6f  _init() entry po
33d64 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  int..*/.SQLITE_P
33d65 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
33d66 65 33 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74  e3Fts3Init(sqlit
33d67 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72  e3 *db){.  int r
33d68 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
33d69 20 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   fts3Hash *pHash
33d6a 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71   = 0;.  const sq
33d6b 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
33d6c 6d 6f 64 75 6c 65 20 2a 70 53 69 6d 70 6c 65 20  module *pSimple 
33d6d 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  = 0;.  const sql
33d6e 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
33d6f 6f 64 75 6c 65 20 2a 70 50 6f 72 74 65 72 20 3d  odule *pPorter =
33d70 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   0;.  const sqli
33d71 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
33d72 64 75 6c 65 20 2a 70 49 63 75 20 3d 20 30 3b 0a  dule *pIcu = 0;.
33d73 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 69  .  sqlite3Fts3Si
33d74 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
33d75 75 6c 65 28 26 70 53 69 6d 70 6c 65 29 3b 0a 20  ule(&pSimple);. 
33d76 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74   sqlite3Fts3Port
33d77 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  erTokenizerModul
33d78 65 28 26 70 50 6f 72 74 65 72 29 3b 0a 23 69 66  e(&pPorter);.#if
33d79 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
33d7a 45 5f 49 43 55 0a 20 20 73 71 6c 69 74 65 33 46  E_ICU.  sqlite3F
33d7b 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d  ts3IcuTokenizerM
33d7c 6f 64 75 6c 65 28 26 70 49 63 75 29 3b 0a 23 65  odule(&pIcu);.#e
33d7d 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  ndif..  /* Alloc
33d7e 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ate and initiali
33d7f 73 65 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c  se the hash-tabl
33d80 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
33d81 74 6f 6b 65 6e 69 7a 65 72 73 2e 20 2a 2f 0a 20  tokenizers. */. 
33d82 20 70 48 61 73 68 20 3d 20 73 71 6c 69 74 65 33   pHash = sqlite3
33d83 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66  _malloc(sizeof(f
33d84 74 73 33 48 61 73 68 29 29 3b 0a 20 20 69 66 28  ts3Hash));.  if(
33d85 20 21 70 48 61 73 68 20 29 7b 0a 20 20 20 20 72   !pHash ){.    r
33d86 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
33d87 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
33d88 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
33d89 69 74 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48  it(pHash, FTS3_H
33d8a 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a  ASH_STRING, 1);.
33d8b 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74    }..  /* Load t
33d8c 68 65 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65  he built-in toke
33d8d 6e 69 7a 65 72 73 20 69 6e 74 6f 20 74 68 65 20  nizers into the 
33d8e 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
33d8f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
33d90 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  K ){.    if( sql
33d91 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
33d92 72 74 28 70 48 61 73 68 2c 20 22 73 69 6d 70 6c  rt(pHash, "simpl
33d93 65 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70  e", 7, (void *)p
33d94 53 69 6d 70 6c 65 29 0a 20 20 20 20 20 7c 7c 20  Simple).     || 
33d95 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
33d96 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 70 6f  nsert(pHash, "po
33d97 72 74 65 72 22 2c 20 37 2c 20 28 76 6f 69 64 20  rter", 7, (void 
33d98 2a 29 70 50 6f 72 74 65 72 29 20 0a 20 20 20 20  *)pPorter) .    
33d99 20 7c 7c 20 28 70 49 63 75 20 26 26 20 73 71 6c   || (pIcu && sql
33d9a 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
33d9b 72 74 28 70 48 61 73 68 2c 20 22 69 63 75 22 2c  rt(pHash, "icu",
33d9c 20 34 2c 20 28 76 6f 69 64 20 2a 29 70 49 63 75   4, (void *)pIcu
33d9d 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  )).    ){.      
33d9e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
33d9f 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69  M;.    }.  }..#i
33da0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
33da1 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  .  sqlite3Fts3Ex
33da2 70 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66  prInitTestInterf
33da3 61 63 65 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a  ace(db);.#endif.
33da4 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65  .  /* Create the
33da5 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 77   virtual table w
33da6 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68  rapper around th
33da7 65 20 68 61 73 68 2d 74 61 62 6c 65 20 61 6e 64  e hash-table and
33da8 20 6f 76 65 72 6c 6f 61 64 20 0a 20 20 2a 2a 20   overload .  ** 
33da9 74 68 65 20 74 77 6f 20 73 63 61 6c 61 72 20 66  the two scalar f
33daa 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 69  unctions. If thi
33dab 73 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  s is successful,
33dac 20 72 65 67 69 73 74 65 72 20 74 68 65 0a 20 20   register the.  
33dad 2a 2a 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 73  ** module with s
33dae 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  qlite..  */.  if
33daf 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
33db0 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  .   && SQLITE_OK
33db1 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 46  ==(rc = sqlite3F
33db2 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65  ts3InitHashTable
33db3 28 64 62 2c 20 70 48 61 73 68 2c 20 22 66 74 73  (db, pHash, "fts
33db4 33 5f 74 6f 6b 65 6e 69 7a 65 72 22 29 29 0a 20  3_tokenizer")). 
33db5 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
33db6 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76  (rc = sqlite3_ov
33db7 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
33db8 64 62 2c 20 22 73 6e 69 70 70 65 74 22 2c 20 2d  db, "snippet", -
33db9 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45  1)).   && SQLITE
33dba 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
33dbb 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
33dbc 74 69 6f 6e 28 64 62 2c 20 22 6f 66 66 73 65 74  tion(db, "offset
33dbd 73 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53  s", -1)).   && S
33dbe 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
33dbf 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
33dc0 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f  _function(db, "o
33dc1 70 74 69 6d 69 7a 65 22 2c 20 2d 31 29 29 0a 23  ptimize", -1)).#
33dc2 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
33dc3 54 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f  T.   && SQLITE_O
33dc4 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
33dc5 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
33dc6 6f 6e 28 64 62 2c 20 22 64 75 6d 70 5f 74 65 72  on(db, "dump_ter
33dc7 6d 73 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20  ms", -1)).   && 
33dc8 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
33dc9 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
33dca 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22  d_function(db, "
33dcb 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 2d  dump_doclist", -
33dcc 31 29 29 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a  1)).#endif.  ){.
33dcd 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
33dce 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
33dcf 5f 76 32 28 0a 20 20 20 20 20 20 20 20 64 62 2c  _v2(.        db,
33dd0 20 22 66 74 73 33 22 2c 20 26 66 74 73 33 4d 6f   "fts3", &fts3Mo
33dd1 64 75 6c 65 2c 20 28 76 6f 69 64 20 2a 29 70 48  dule, (void *)pH
33dd2 61 73 68 2c 20 68 61 73 68 44 65 73 74 72 6f 79  ash, hashDestroy
33dd3 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  .    );.  }..  /
33dd4 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  * An error has o
33dd5 63 63 75 72 72 65 64 2e 20 44 65 6c 65 74 65 20  ccurred. Delete 
33dd6 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61  the hash table a
33dd7 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  nd return the er
33dd8 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 61  ror code. */.  a
33dd9 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
33dda 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 48  E_OK );.  if( pH
33ddb 61 73 68 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ash ){.    sqlit
33ddc 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28  e3Fts3HashClear(
33ddd 70 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c 69  pHash);.    sqli
33dde 74 65 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b  te3_free(pHash);
33ddf 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
33de0 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54 45  ;.}..#if !SQLITE
33de1 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50 49  _CORE.SQLITE_API
33de2 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74   int sqlite3_ext
33de3 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73  ension_init(.  s
33de4 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63  qlite3 *db, .  c
33de5 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a  har **pzErrMsg,.
33de6 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
33de7 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70 41  api_routines *pA
33de8 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f 45  pi.){.  SQLITE_E
33de9 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70  XTENSION_INIT2(p
33dea 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73 71  Api).  return sq
33deb 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 64 62  lite3Fts3Init(db
33dec 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  );.}.#endif..#en
33ded 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
33dee 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
33def 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
33df0 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
33df1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
33df2 45 6e 64 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a  End of fts3.c **
33df3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33df4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33df5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
33df6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
33df7 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
33df8 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  expr.c *********
33df9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33dfa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
33dfb 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20 32  /*.** 2008 Nov 2
33dfc 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
33dfd 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
33dfe 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
33dff 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
33e00 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
33e01 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
33e02 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
33e03 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
33e04 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
33e05 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
33e06 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
33e07 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
33e08 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
33e09 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
33e0a 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
33e0b 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
33e0c 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
33e0d 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
33e0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e12 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
33e13 20 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 73   module contains
33e14 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65   code that imple
33e15 6d 65 6e 74 73 20 61 20 70 61 72 73 65 72 20 66  ments a parser f
33e16 6f 72 20 66 74 73 33 20 71 75 65 72 79 20 73 74  or fts3 query st
33e17 72 69 6e 67 73 0a 2a 2a 20 28 74 68 65 20 72 69  rings.** (the ri
33e18 67 68 74 2d 68 61 6e 64 20 61 72 67 75 6d 65 6e  ght-hand argumen
33e19 74 20 74 6f 20 74 68 65 20 4d 41 54 43 48 20 6f  t to the MATCH o
33e1a 70 65 72 61 74 6f 72 29 2e 20 42 65 63 61 75 73  perator). Becaus
33e1b 65 20 74 68 65 20 73 75 70 70 6f 72 74 65 64 20  e the supported 
33e1c 0a 2a 2a 20 73 79 6e 74 61 78 20 69 73 20 72 65  .** syntax is re
33e1d 6c 61 74 69 76 65 6c 79 20 73 69 6d 70 6c 65 2c  latively simple,
33e1e 20 74 68 65 20 77 68 6f 6c 65 20 74 6f 6b 65 6e   the whole token
33e1f 69 7a 65 72 2f 70 61 72 73 65 72 20 73 79 73 74  izer/parser syst
33e20 65 6d 20 69 73 0a 2a 2a 20 68 61 6e 64 2d 63 6f  em is.** hand-co
33e21 64 65 64 2e 20 54 68 65 20 70 75 62 6c 69 63 20  ded. The public 
33e22 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 69  interface to thi
33e23 73 20 6d 6f 64 75 6c 65 20 69 73 20 64 65 63 6c  s module is decl
33e24 61 72 65 64 20 69 6e 20 73 6f 75 72 63 65 0a 2a  ared in source.*
33e25 2a 20 63 6f 64 65 20 66 69 6c 65 20 22 66 74 73  * code file "fts
33e26 33 5f 65 78 70 72 2e 68 22 2e 0a 2a 2f 0a 23 69  3_expr.h"..*/.#i
33e27 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
33e28 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
33e29 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
33e2a 5f 46 54 53 33 29 0a 0a 2f 2a 0a 2a 2a 20 42 79  _FTS3)../*.** By
33e2b 20 64 65 66 61 75 6c 74 2c 20 74 68 69 73 20 6d   default, this m
33e2c 6f 64 75 6c 65 20 70 61 72 73 65 73 20 74 68 65  odule parses the
33e2d 20 6c 65 67 61 63 79 20 73 79 6e 74 61 78 20 74   legacy syntax t
33e2e 68 61 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a  hat has been .**
33e2f 20 74 72 61 64 69 74 69 6f 6e 61 6c 6c 79 20 75   traditionally u
33e30 73 65 64 20 62 79 20 66 74 73 33 2e 20 4f 72 2c  sed by fts3. Or,
33e31 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
33e32 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53  E_FTS3_PARENTHES
33e33 49 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  IS.** is defined
33e34 2c 20 74 68 65 6e 20 69 74 20 75 73 65 73 20 74  , then it uses t
33e35 68 65 20 6e 65 77 20 73 79 6e 74 61 78 2e 20 54  he new syntax. T
33e36 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 62  he differences b
33e37 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  etween.** the ne
33e38 77 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 73 79  w and the old sy
33e39 6e 74 61 78 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a  ntaxes are:.**.*
33e3a 2a 20 20 61 29 20 54 68 65 20 6e 65 77 20 73 79  *  a) The new sy
33e3b 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 70 61  ntax supports pa
33e3c 72 65 6e 74 68 65 73 69 73 2e 20 54 68 65 20 6f  renthesis. The o
33e3d 6c 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a  ld does not..**.
33e3e 2a 2a 20 20 62 29 20 54 68 65 20 6e 65 77 20 73  **  b) The new s
33e3f 79 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74  yntax supports t
33e40 68 65 20 41 4e 44 20 61 6e 64 20 4e 4f 54 20 6f  he AND and NOT o
33e41 70 65 72 61 74 6f 72 73 2e 20 54 68 65 20 6f 6c  perators. The ol
33e42 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a  d does not..**.*
33e43 2a 20 20 63 29 20 54 68 65 20 6f 6c 64 20 73 79  *  c) The old sy
33e44 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74 68  ntax supports th
33e45 65 20 22 2d 22 20 74 6f 6b 65 6e 20 71 75 61 6c  e "-" token qual
33e46 69 66 69 65 72 2e 20 54 68 69 73 20 69 73 20 6e  ifier. This is n
33e47 6f 74 20 0a 2a 2a 20 20 20 20 20 73 75 70 70 6f  ot .**     suppo
33e48 72 74 65 64 20 62 79 20 74 68 65 20 6e 65 77 20  rted by the new 
33e49 73 79 6e 74 61 78 20 28 69 74 20 69 73 20 72 65  syntax (it is re
33e4a 70 6c 61 63 65 64 20 62 79 20 74 68 65 20 4e 4f  placed by the NO
33e4b 54 20 6f 70 65 72 61 74 6f 72 29 2e 0a 2a 2a 0a  T operator)..**.
33e4c 2a 2a 20 20 64 29 20 57 68 65 6e 20 75 73 69 6e  **  d) When usin
33e4d 67 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78  g the old syntax
33e4e 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61 74 6f  , the OR operato
33e4f 72 20 68 61 73 20 61 20 67 72 65 61 74 65 72 20  r has a greater 
33e50 70 72 65 63 65 64 65 6e 63 65 0a 2a 2a 20 20 20  precedence.**   
33e51 20 20 74 68 61 6e 20 61 6e 20 69 6d 70 6c 69 63    than an implic
33e52 69 74 20 41 4e 44 2e 20 57 68 65 6e 20 75 73 69  it AND. When usi
33e53 6e 67 20 74 68 65 20 6e 65 77 2c 20 62 6f 74 68  ng the new, both
33e54 20 69 6d 70 6c 69 63 69 74 79 20 61 6e 64 20 65   implicity and e
33e55 78 70 6c 69 63 69 74 0a 2a 2a 20 20 20 20 20 41  xplicit.**     A
33e56 4e 44 20 6f 70 65 72 61 74 6f 72 73 20 68 61 76  ND operators hav
33e57 65 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65  e a higher prece
33e58 64 65 6e 63 65 20 74 68 61 6e 20 4f 52 2e 0a 2a  dence than OR..*
33e59 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64  *.** If compiled
33e5a 20 77 69 74 68 20 53 51 4c 49 54 45 5f 54 45 53   with SQLITE_TES
33e5b 54 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  T defined, then 
33e5c 74 68 69 73 20 6d 6f 64 75 6c 65 20 65 78 70 6f  this module expo
33e5d 72 74 73 20 74 68 65 0a 2a 2a 20 73 79 6d 62 6f  rts the.** symbo
33e5e 6c 20 22 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  l "int sqlite3_f
33e5f 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
33e60 74 68 65 73 65 73 22 2e 20 53 65 74 74 69 6e 67  theses". Setting
33e61 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a   this variable.*
33e62 2a 20 74 6f 20 7a 65 72 6f 20 63 61 75 73 65 73  * to zero causes
33e63 20 74 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 75   the module to u
33e64 73 65 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61  se the old synta
33e65 78 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 20  x. If it is set 
33e66 74 6f 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20  to .** non-zero 
33e67 74 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20 69  the new syntax i
33e68 73 20 61 63 74 69 76 61 74 65 64 2e 20 54 68 69  s activated. Thi
33e69 73 20 69 73 20 73 6f 20 62 6f 74 68 20 73 79 6e  s is so both syn
33e6a 74 61 78 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20  taxes can.** be 
33e6b 74 65 73 74 65 64 20 75 73 69 6e 67 20 61 20 73  tested using a s
33e6c 69 6e 67 6c 65 20 62 75 69 6c 64 20 6f 66 20 74  ingle build of t
33e6d 65 73 74 66 69 78 74 75 72 65 2e 0a 2a 2f 0a 23  estfixture..*/.#
33e6e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
33e6f 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
33e70 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
33e71 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
33e72 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 23 20 69 66   = 0;.#else.# if
33e73 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
33e74 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53  E_FTS3_PARENTHES
33e75 49 53 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71  IS .#  define sq
33e76 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
33e77 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 31 0a  e_parentheses 1.
33e78 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
33e79 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
33e7a 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
33e7b 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69   0.# endif.#endi
33e7c 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74  f../*.** Default
33e7d 20 73 70 61 6e 20 66 6f 72 20 4e 45 41 52 20 6f   span for NEAR o
33e7e 70 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65  perators..*/.#de
33e7f 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 54 53 33  fine SQLITE_FTS3
33e80 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50 41  _DEFAULT_NEAR_PA
33e81 52 41 4d 20 31 30 0a 0a 0a 74 79 70 65 64 65 66  RAM 10...typedef
33e82 20 73 74 72 75 63 74 20 50 61 72 73 65 43 6f 6e   struct ParseCon
33e83 74 65 78 74 20 50 61 72 73 65 43 6f 6e 74 65 78  text ParseContex
33e84 74 3b 0a 73 74 72 75 63 74 20 50 61 72 73 65 43  t;.struct ParseC
33e85 6f 6e 74 65 78 74 20 7b 0a 20 20 73 71 6c 69 74  ontext {.  sqlit
33e86 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
33e87 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f  okenizer;      /
33e88 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  * Tokenizer modu
33e89 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
33e8a 61 72 20 2a 2a 61 7a 43 6f 6c 3b 20 20 20 20 20  ar **azCol;     
33e8b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
33e8c 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e  rray of column n
33e8d 61 6d 65 73 20 66 6f 72 20 66 74 73 33 20 74 61  ames for fts3 ta
33e8e 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
33e8f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
33e90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33e91 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
33e92 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a  s in azCol[] */.
33e93 20 20 69 6e 74 20 69 44 65 66 61 75 6c 74 43 6f    int iDefaultCo
33e94 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
33e95 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
33e96 20 63 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79   column to query
33e97 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f   */.  sqlite3_co
33e98 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20 20 20  ntext *pCtx;    
33e99 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
33e9a 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
33e9b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   here */.  int n
33e9c 4e 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Nest;           
33e9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33e9e 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74  * Number of nest
33e9f 65 64 20 62 72 61 63 6b 65 74 73 20 2a 2f 0a 7d  ed brackets */.}
33ea0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ;../*.** This fu
33ea1 6e 63 74 69 6f 6e 20 69 73 20 65 71 75 69 76 61  nction is equiva
33ea2 6c 65 6e 74 20 74 6f 20 74 68 65 20 73 74 61 6e  lent to the stan
33ea3 64 61 72 64 20 69 73 73 70 61 63 65 28 29 20 66  dard isspace() f
33ea4 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20  unction. .**.** 
33ea5 54 68 65 20 73 74 61 6e 64 61 72 64 20 69 73 73  The standard iss
33ea6 70 61 63 65 28 29 20 63 61 6e 20 62 65 20 61 77  pace() can be aw
33ea7 6b 77 61 72 64 20 74 6f 20 75 73 65 20 73 61 66  kward to use saf
33ea8 65 6c 79 2c 20 62 65 63 61 75 73 65 20 61 6c 74  ely, because alt
33ea9 68 6f 75 67 68 20 69 74 0a 2a 2a 20 69 73 20 64  hough it.** is d
33eaa 65 66 69 6e 65 64 20 74 6f 20 61 63 63 65 70 74  efined to accept
33eab 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20   an argument of 
33eac 74 79 70 65 20 69 6e 74 2c 20 69 74 73 20 62 65  type int, its be
33ead 68 61 76 69 6f 75 72 20 77 68 65 6e 20 70 61 73  haviour when pas
33eae 73 65 64 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65  sed.** an intege
33eaf 72 20 74 68 61 74 20 66 61 6c 6c 73 20 6f 75 74  r that falls out
33eb0 73 69 64 65 20 6f 66 20 74 68 65 20 72 61 6e 67  side of the rang
33eb1 65 20 6f 66 20 74 68 65 20 75 6e 73 69 67 6e 65  e of the unsigne
33eb2 64 20 63 68 61 72 20 74 79 70 65 0a 2a 2a 20 69  d char type.** i
33eb3 73 20 75 6e 64 65 66 69 6e 65 64 20 28 61 6e 64  s undefined (and
33eb4 20 73 6f 6d 65 74 69 6d 65 73 2c 20 22 75 6e 64   sometimes, "und
33eb5 65 66 69 6e 65 64 22 20 6d 65 61 6e 73 20 73 65  efined" means se
33eb6 67 66 61 75 6c 74 29 2e 20 54 68 69 73 20 77 72  gfault). This wr
33eb7 61 70 70 65 72 0a 2a 2a 20 69 73 20 64 65 66 69  apper.** is defi
33eb8 6e 65 64 20 74 6f 20 61 63 63 65 70 74 20 61 6e  ned to accept an
33eb9 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70   argument of typ
33eba 65 20 63 68 61 72 2c 20 61 6e 64 20 61 6c 77 61  e char, and alwa
33ebb 79 73 20 72 65 74 75 72 6e 73 20 30 20 66 6f 72  ys returns 0 for
33ebc 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 73 20 74  .** any values t
33ebd 68 61 74 20 66 61 6c 6c 20 6f 75 74 73 69 64 65  hat fall outside
33ebe 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66   of the range of
33ebf 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 63 68   the unsigned ch
33ec0 61 72 20 74 79 70 65 20 28 69 2e 65 2e 0a 2a 2a  ar type (i.e..**
33ec1 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 73   negative values
33ec2 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
33ec3 20 66 74 73 33 69 73 73 70 61 63 65 28 63 68 61   fts3isspace(cha
33ec4 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  r c){.  return (
33ec5 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73  c&0x80)==0 ? iss
33ec6 70 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 0a  pace(c) : 0;.}..
33ec7 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
33ec8 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
33ec9 6d 20 62 75 66 66 65 72 20 7a 20 28 6c 65 6e 67  m buffer z (leng
33eca 74 68 20 6e 29 20 75 73 69 6e 67 20 74 68 65 20  th n) using the 
33ecb 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 61 6e 64  tokenizer.** and
33ecc 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69   other informati
33ecd 6f 6e 20 28 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  on (column names
33ece 20 65 74 63 2e 29 20 69 6e 20 70 50 61 72 73 65   etc.) in pParse
33ecf 2e 20 43 72 65 61 74 65 20 61 6e 20 46 74 73 33  . Create an Fts3
33ed0 45 78 70 72 0a 2a 2a 20 73 74 72 75 63 74 75 72  Expr.** structur
33ed1 65 20 6f 66 20 74 79 70 65 20 46 54 53 51 55 45  e of type FTSQUE
33ed2 52 59 5f 50 48 52 41 53 45 20 63 6f 6e 74 61 69  RY_PHRASE contai
33ed3 6e 69 6e 67 20 61 20 70 68 72 61 73 65 20 63 6f  ning a phrase co
33ed4 6e 73 69 73 74 69 6e 67 20 6f 66 20 74 68 69 73  nsisting of this
33ed5 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e  .** single token
33ed6 20 61 6e 64 20 73 65 74 20 2a 70 70 45 78 70 72   and set *ppExpr
33ed7 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 69 74 2e   to point to it.
33ed8 20 49 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   If the end of t
33ed9 68 65 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20  he buffer is.** 
33eda 72 65 61 63 68 65 64 20 62 65 66 6f 72 65 20 61  reached before a
33edb 20 74 6f 6b 65 6e 20 69 73 20 66 6f 75 6e 64 2c   token is found,
33edc 20 73 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20   set *ppExpr to 
33edd 7a 65 72 6f 2e 20 49 74 20 69 73 20 74 68 65 0a  zero. It is the.
33ede 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  ** responsibilit
33edf 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20  y of the caller 
33ee0 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 64 65  to eventually de
33ee1 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 61 6c 6c  allocate the all
33ee2 6f 63 61 74 65 64 20 0a 2a 2a 20 46 74 73 33 45  ocated .** Fts3E
33ee3 78 70 72 20 73 74 72 75 63 74 75 72 65 20 28 69  xpr structure (i
33ee4 66 20 61 6e 79 29 20 62 79 20 70 61 73 73 69 6e  f any) by passin
33ee5 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  g it to sqlite3_
33ee6 66 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65  free()..**.** Re
33ee7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
33ee8 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  f successful, or
33ee9 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
33eea 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
33eeb 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2e 0a 2a  tion.** fails..*
33eec 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
33eed 4e 65 78 74 54 6f 6b 65 6e 28 0a 20 20 50 61 72  NextToken(.  Par
33eee 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73  seContext *pPars
33eef 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
33ef0 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65       /* fts3 que
33ef1 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  ry parse context
33ef2 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20   */.  int iCol, 
33ef3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ef4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33ef5 20 56 61 6c 75 65 20 66 6f 72 20 46 74 73 33 50   Value for Fts3P
33ef6 68 72 61 73 65 2e 69 43 6f 6c 75 6d 6e 20 2a 2f  hrase.iColumn */
33ef7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
33ef8 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20  , int n,        
33ef9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
33efa 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
33efb 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
33efc 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
33efd 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
33efe 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
33eff 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  int *pnConsumed 
33f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f01 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
33f02 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
33f03 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20  consumed */.){. 
33f04 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
33f05 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  er *pTokenizer =
33f06 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69   pParse->pTokeni
33f07 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  zer;.  sqlite3_t
33f08 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
33f09 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d  const *pModule =
33f0a 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
33f0b 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  dule;.  int rc;.
33f0c 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
33f0d 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
33f0e 73 6f 72 3b 0a 20 20 46 74 73 33 45 78 70 72 20  sor;.  Fts3Expr 
33f0f 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 69 6e 74  *pRet = 0;.  int
33f10 20 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a   nConsumed = 0;.
33f11 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  .  rc = pModule-
33f12 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65  >xOpen(pTokenize
33f13 72 2c 20 7a 2c 20 6e 2c 20 26 70 43 75 72 73 6f  r, z, n, &pCurso
33f14 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
33f15 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63  LITE_OK ){.    c
33f16 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65  onst char *zToke
33f17 6e 3b 0a 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65  n;.    int nToke
33f18 6e 2c 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c  n, iStart, iEnd,
33f19 20 69 50 6f 73 69 74 69 6f 6e 3b 0a 20 20 20 20   iPosition;.    
33f1a 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20  int nByte;      
33f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f1c 20 20 20 20 20 20 20 20 20 2f 2a 20 74 6f 74 61           /* tota
33f1d 6c 20 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63  l space to alloc
33f1e 61 74 65 20 2a 2f 0a 0a 20 20 20 20 70 43 75 72  ate */..    pCur
33f1f 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  sor->pTokenizer 
33f20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  = pTokenizer;.  
33f21 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
33f22 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20 26  xNext(pCursor, &
33f23 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c  zToken, &nToken,
33f24 20 26 69 53 74 61 72 74 2c 20 26 69 45 6e 64 2c   &iStart, &iEnd,
33f25 20 26 69 50 6f 73 69 74 69 6f 6e 29 3b 0a 0a 20   &iPosition);.. 
33f26 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
33f27 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 42  E_OK ){.      nB
33f28 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73  yte = sizeof(Fts
33f29 33 45 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28  3Expr) + sizeof(
33f2a 46 74 73 33 50 68 72 61 73 65 29 20 2b 20 6e 54  Fts3Phrase) + nT
33f2b 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70 52 65 74  oken;.      pRet
33f2c 20 3d 20 28 46 74 73 33 45 78 70 72 20 2a 29 73   = (Fts3Expr *)s
33f2d 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
33f2e 79 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  yte);.      if( 
33f2f 21 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20  !pRet ){.       
33f30 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
33f31 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EM;.      }else{
33f32 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
33f33 70 52 65 74 2c 20 30 2c 20 6e 42 79 74 65 29 3b  pRet, 0, nByte);
33f34 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65  .        pRet->e
33f35 54 79 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f  Type = FTSQUERY_
33f36 50 48 52 41 53 45 3b 0a 20 20 20 20 20 20 20 20  PHRASE;.        
33f37 70 52 65 74 2d 3e 70 50 68 72 61 73 65 20 3d 20  pRet->pPhrase = 
33f38 28 46 74 73 33 50 68 72 61 73 65 20 2a 29 26 70  (Fts3Phrase *)&p
33f39 52 65 74 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  Ret[1];.        
33f3a 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  pRet->pPhrase->n
33f3b 54 6f 6b 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20  Token = 1;.     
33f3c 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65     pRet->pPhrase
33f3d 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c  ->iColumn = iCol
33f3e 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ;.        pRet->
33f3f 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
33f40 30 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20  0].n = nToken;. 
33f41 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
33f42 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e  rase->aToken[0].
33f43 7a 20 3d 20 28 63 68 61 72 20 2a 29 26 70 52 65  z = (char *)&pRe
33f44 74 2d 3e 70 50 68 72 61 73 65 5b 31 5d 3b 0a 20  t->pPhrase[1];. 
33f45 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 52         memcpy(pR
33f46 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f  et->pPhrase->aTo
33f47 6b 65 6e 5b 30 5d 2e 7a 2c 20 7a 54 6f 6b 65 6e  ken[0].z, zToken
33f48 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20  , nToken);..    
33f49 20 20 20 20 69 66 28 20 69 45 6e 64 3c 6e 20 26      if( iEnd<n &
33f4a 26 20 7a 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20 29  & z[iEnd]=='*' )
33f4b 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65 74  {.          pRet
33f4c 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
33f4d 6e 5b 30 5d 2e 69 73 50 72 65 66 69 78 20 3d 20  n[0].isPrefix = 
33f4e 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e  1;.          iEn
33f4f 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  d++;.        }. 
33f50 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
33f51 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
33f52 70 61 72 65 6e 74 68 65 73 65 73 20 26 26 20 69  parentheses && i
33f53 53 74 61 72 74 3e 30 20 26 26 20 7a 5b 69 53 74  Start>0 && z[iSt
33f54 61 72 74 2d 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20  art-1]=='-' ){. 
33f55 20 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70           pRet->p
33f56 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20 3d 20  Phrase->isNot = 
33f57 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
33f58 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 6f 6e 73     }.      nCons
33f59 75 6d 65 64 20 3d 20 69 45 6e 64 3b 0a 20 20 20  umed = iEnd;.   
33f5a 20 7d 0a 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d   }..    pModule-
33f5b 3e 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29  >xClose(pCursor)
33f5c 3b 0a 20 20 7d 0a 20 20 0a 20 20 2a 70 6e 43 6f  ;.  }.  .  *pnCo
33f5d 6e 73 75 6d 65 64 20 3d 20 6e 43 6f 6e 73 75 6d  nsumed = nConsum
33f5e 65 64 3b 0a 20 20 2a 70 70 45 78 70 72 20 3d 20  ed;.  *ppExpr = 
33f5f 70 52 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 72  pRet;.  return r
33f60 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 6c  c;.}.../*.** Enl
33f61 61 72 67 65 20 61 20 6d 65 6d 6f 72 79 20 61 6c  arge a memory al
33f62 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20 61 6e  location.  If an
33f63 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 61   out-of-memory a
33f64 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73  llocation occurs
33f65 2c 0a 2a 2a 20 74 68 65 6e 20 66 72 65 65 20 74  ,.** then free t
33f66 68 65 20 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f  he old allocatio
33f67 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 2a 66 74 73 33  n..*/.void *fts3
33f68 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 6f  ReallocOrFree(vo
33f69 69 64 20 2a 70 4f 72 69 67 2c 20 69 6e 74 20 6e  id *pOrig, int n
33f6a 4e 65 77 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52  New){.  void *pR
33f6b 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  et = sqlite3_rea
33f6c 6c 6c 6f 63 28 70 4f 72 69 67 2c 20 6e 4e 65 77  lloc(pOrig, nNew
33f6d 29 3b 0a 20 20 69 66 28 20 21 70 52 65 74 20 29  );.  if( !pRet )
33f6e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
33f6f 65 65 28 70 4f 72 69 67 29 3b 0a 20 20 7d 0a 20  ee(pOrig);.  }. 
33f70 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
33f71 0a 2f 2a 0a 2a 2a 20 42 75 66 66 65 72 20 7a 49  ./*.** Buffer zI
33f72 6e 70 75 74 2c 20 6c 65 6e 67 74 68 20 6e 49 6e  nput, length nIn
33f73 70 75 74 2c 20 63 6f 6e 74 61 69 6e 73 20 74 68  put, contains th
33f74 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20  e contents of a 
33f75 71 75 6f 74 65 64 20 73 74 72 69 6e 67 0a 2a 2a  quoted string.**
33f76 20 74 68 61 74 20 61 70 70 65 61 72 65 64 20 61   that appeared a
33f77 73 20 70 61 72 74 20 6f 66 20 61 6e 20 66 74 73  s part of an fts
33f78 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  3 query expressi
33f79 6f 6e 2e 20 4e 65 69 74 68 65 72 20 71 75 6f 74  on. Neither quot
33f7a 65 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69  e character.** i
33f7b 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  s included in th
33f7c 65 20 62 75 66 66 65 72 2e 20 54 68 69 73 20 66  e buffer. This f
33f7d 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
33f7e 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 74 68 65   to tokenize the
33f7f 20 65 6e 74 69 72 65 0a 2a 2a 20 69 6e 70 75 74   entire.** input
33f80 20 62 75 66 66 65 72 20 61 6e 64 20 63 72 65 61   buffer and crea
33f81 74 65 20 61 6e 20 46 74 73 33 45 78 70 72 20 73  te an Fts3Expr s
33f82 74 72 75 63 74 75 72 65 20 6f 66 20 74 79 70 65  tructure of type
33f83 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
33f84 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20   .** containing 
33f85 74 68 65 20 72 65 73 75 6c 74 73 2e 0a 2a 2a 0a  the results..**.
33f86 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
33f87 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
33f88 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45  eturned and *ppE
33f89 78 70 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74  xpr set to point
33f8a 20 61 74 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63   at the.** alloc
33f8b 61 74 65 64 20 46 74 73 33 45 78 70 72 20 73 74  ated Fts3Expr st
33f8c 72 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69  ructure. Otherwi
33f8d 73 65 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  se, either SQLIT
33f8e 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 20 6f 66 20  E_NOMEM (out of 
33f8f 6d 65 6d 6f 72 79 0a 2a 2a 20 65 72 72 6f 72 29  memory.** error)
33f90 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   or SQLITE_ERROR
33f91 20 28 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 65   (tokenization e
33f92 72 72 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65  rror) is returne
33f93 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73 65  d and *ppExpr se
33f94 74 0a 2a 2a 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74  t.** to 0..*/.st
33f95 61 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74  atic int getNext
33f96 53 74 72 69 6e 67 28 0a 20 20 50 61 72 73 65 43  String(.  ParseC
33f97 6f 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20  ontext *pParse, 
33f98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f99 20 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20    /* fts3 query 
33f9a 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
33f9b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
33f9c 49 6e 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75  Input, int nInpu
33f9d 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  t,         /* In
33f9e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
33f9f 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
33fa0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
33fa1 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
33fa2 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b  expression */.){
33fa3 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
33fa4 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
33fa5 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65   = pParse->pToke
33fa6 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33  nizer;.  sqlite3
33fa7 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
33fa8 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65  e const *pModule
33fa9 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70   = pTokenizer->p
33faa 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63  Module;.  int rc
33fab 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 20  ;.  Fts3Expr *p 
33fac 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
33fad 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
33fae 2a 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  *pCursor = 0;.  
33faf 63 68 61 72 20 2a 7a 54 65 6d 70 20 3d 20 30 3b  char *zTemp = 0;
33fb0 0a 20 20 69 6e 74 20 6e 54 65 6d 70 20 3d 20 30  .  int nTemp = 0
33fb1 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  ;..  rc = pModul
33fb2 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  e->xOpen(pTokeni
33fb3 7a 65 72 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  zer, zInput, nIn
33fb4 70 75 74 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a  put, &pCursor);.
33fb5 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
33fb6 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  _OK ){.    int i
33fb7 69 3b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e  i;.    pCursor->
33fb8 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
33fb9 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20 66 6f 72  kenizer;.    for
33fba 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  (ii=0; rc==SQLIT
33fbb 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  E_OK; ii++){.   
33fbc 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
33fbd 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 6e 74  Token;.      int
33fbe 20 6e 54 6f 6b 65 6e 2c 20 69 42 65 67 69 6e 2c   nToken, iBegin,
33fbf 20 69 45 6e 64 2c 20 69 50 6f 73 3b 0a 20 20 20   iEnd, iPos;.   
33fc0 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d     rc = pModule-
33fc1 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20  >xNext(pCursor, 
33fc2 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e  &zToken, &nToken
33fc3 2c 20 26 69 42 65 67 69 6e 2c 20 26 69 45 6e 64  , &iBegin, &iEnd
33fc4 2c 20 26 69 50 6f 73 29 3b 0a 20 20 20 20 20 20  , &iPos);.      
33fc5 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
33fc6 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  K ){.        int
33fc7 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
33fc8 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65  Fts3Expr) + size
33fc9 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a  of(Fts3Phrase);.
33fca 20 20 20 20 20 20 20 20 70 20 3d 20 66 74 73 33          p = fts3
33fcb 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2c  ReallocOrFree(p,
33fcc 20 6e 42 79 74 65 2b 69 69 2a 73 69 7a 65 6f 66   nByte+ii*sizeof
33fcd 28 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f  (struct PhraseTo
33fce 6b 65 6e 29 29 3b 0a 20 20 20 20 20 20 20 20 7a  ken));.        z
33fcf 54 65 6d 70 20 3d 20 66 74 73 33 52 65 61 6c 6c  Temp = fts3Reall
33fd0 6f 63 4f 72 46 72 65 65 28 7a 54 65 6d 70 2c 20  ocOrFree(zTemp, 
33fd1 6e 54 65 6d 70 20 2b 20 6e 54 6f 6b 65 6e 29 3b  nTemp + nToken);
33fd2 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 20  .        if( !p 
33fd3 7c 7c 20 21 7a 54 65 6d 70 20 29 7b 0a 20 20 20  || !zTemp ){.   
33fd4 20 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d         goto no_m
33fd5 65 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  em;.        }.  
33fd6 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20        if( ii==0 
33fd7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  ){.          mem
33fd8 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 29  set(p, 0, nByte)
33fd9 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  ;.          p->p
33fda 50 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68  Phrase = (Fts3Ph
33fdb 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20  rase *)&p[1];.  
33fdc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
33fdd 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74  p->pPhrase = (Ft
33fde 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d  s3Phrase *)&p[1]
33fdf 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68  ;.        p->pPh
33fe0 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 69  rase->nToken = i
33fe1 69 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  i+1;.        p->
33fe2 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
33fe3 69 69 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a  ii].n = nToken;.
33fe4 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
33fe5 7a 54 65 6d 70 5b 6e 54 65 6d 70 5d 2c 20 7a 54  zTemp[nTemp], zT
33fe6 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20  oken, nToken);. 
33fe7 20 20 20 20 20 20 20 6e 54 65 6d 70 20 2b 3d 20         nTemp += 
33fe8 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  nToken;.        
33fe9 69 66 28 20 69 45 6e 64 3c 6e 49 6e 70 75 74 20  if( iEnd<nInput 
33fea 26 26 20 7a 49 6e 70 75 74 5b 69 45 6e 64 5d 3d  && zInput[iEnd]=
33feb 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
33fec 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54    p->pPhrase->aT
33fed 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69  oken[ii].isPrefi
33fee 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  x = 1;.        }
33fef 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
33ff0 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  p->pPhrase->aTok
33ff1 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69 78 20  en[ii].isPrefix 
33ff2 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
33ff3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
33ff4 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73    pModule->xClos
33ff5 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  e(pCursor);.    
33ff6 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 7d  pCursor = 0;.  }
33ff7 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
33ff8 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69  TE_DONE ){.    i
33ff9 6e 74 20 6a 6a 3b 0a 20 20 20 20 63 68 61 72 20  nt jj;.    char 
33ffa 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e  *zNew;.    int n
33ffb 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  New = 0;.    int
33ffc 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
33ffd 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65  Fts3Expr) + size
33ffe 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a  of(Fts3Phrase);.
33fff 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 28 70 3f      nByte += (p?
34000 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  (p->pPhrase->nTo
34001 6b 65 6e 2d 31 29 3a 30 29 20 2a 20 73 69 7a 65  ken-1):0) * size
34002 6f 66 28 73 74 72 75 63 74 20 50 68 72 61 73 65  of(struct Phrase
34003 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 70 20 3d 20  Token);.    p = 
34004 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65  fts3ReallocOrFre
34005 65 28 70 2c 20 6e 42 79 74 65 20 2b 20 6e 54 65  e(p, nByte + nTe
34006 6d 70 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20  mp);.    if( !p 
34007 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
34008 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
34009 69 66 28 20 7a 54 65 6d 70 20 29 7b 0a 20 20 20  if( zTemp ){.   
3400a 20 20 20 7a 4e 65 77 20 3d 20 26 28 28 28 63 68     zNew = &(((ch
3400b 61 72 20 2a 29 70 29 5b 6e 42 79 74 65 5d 29 3b  ar *)p)[nByte]);
3400c 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e  .      memcpy(zN
3400d 65 77 2c 20 7a 54 65 6d 70 2c 20 6e 54 65 6d 70  ew, zTemp, nTemp
3400e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
3400f 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c      memset(p, 0,
34010 20 6e 42 79 74 65 2b 6e 54 65 6d 70 29 3b 0a 20   nByte+nTemp);. 
34011 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50 68 72     }.    p->pPhr
34012 61 73 65 20 3d 20 28 46 74 73 33 50 68 72 61 73  ase = (Fts3Phras
34013 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 66  e *)&p[1];.    f
34014 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 70 2d 3e 70  or(jj=0; jj<p->p
34015 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20  Phrase->nToken; 
34016 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e  jj++){.      p->
34017 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
34018 6a 6a 5d 2e 7a 20 3d 20 26 7a 4e 65 77 5b 6e 4e  jj].z = &zNew[nN
34019 65 77 5d 3b 0a 20 20 20 20 20 20 6e 4e 65 77 20  ew];.      nNew 
3401a 2b 3d 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61  += p->pPhrase->a
3401b 54 6f 6b 65 6e 5b 6a 6a 5d 2e 6e 3b 0a 20 20 20  Token[jj].n;.   
3401c 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
3401d 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20  ree(zTemp);.    
3401e 70 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  p->eType = FTSQU
3401f 45 52 59 5f 50 48 52 41 53 45 3b 0a 20 20 20 20  ERY_PHRASE;.    
34020 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c  p->pPhrase->iCol
34021 75 6d 6e 20 3d 20 70 50 61 72 73 65 2d 3e 69 44  umn = pParse->iD
34022 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 72  efaultCol;.    r
34023 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
34024 20 7d 0a 0a 20 20 2a 70 70 45 78 70 72 20 3d 20   }..  *ppExpr = 
34025 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  p;.  return rc;.
34026 6e 6f 5f 6d 65 6d 3a 0a 0a 20 20 69 66 28 20 70  no_mem:..  if( p
34027 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 70 4d  Cursor ){.    pM
34028 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43  odule->xClose(pC
34029 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 73 71  ursor);.  }.  sq
3402a 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 6d 70  lite3_free(zTemp
3402b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
3402c 65 28 70 29 3b 0a 20 20 2a 70 70 45 78 70 72 20  e(p);.  *ppExpr 
3402d 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  = 0;.  return SQ
3402e 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f  LITE_NOMEM;.}../
3402f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 67 65  *.** Function ge
34030 74 4e 65 78 74 4e 6f 64 65 28 29 2c 20 77 68 69  tNextNode(), whi
34031 63 68 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  ch is called by 
34032 66 74 73 33 45 78 70 72 50 61 72 73 65 28 29 2c  fts3ExprParse(),
34033 20 6d 61 79 20 69 74 73 65 6c 66 0a 2a 2a 20 63   may itself.** c
34034 61 6c 6c 20 66 74 73 33 45 78 70 72 50 61 72 73  all fts3ExprPars
34035 65 28 29 2e 20 53 6f 20 74 68 69 73 20 66 6f 72  e(). So this for
34036 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
34037 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f   is required..*/
34038 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
34039 45 78 70 72 50 61 72 73 65 28 50 61 72 73 65 43  ExprParse(ParseC
3403a 6f 6e 74 65 78 74 20 2a 2c 20 63 6f 6e 73 74 20  ontext *, const 
3403b 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74 73  char *, int, Fts
3403c 33 45 78 70 72 20 2a 2a 2c 20 69 6e 74 20 2a 29  3Expr **, int *)
3403d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 75 74  ;../*.** The out
3403e 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70 70  put variable *pp
3403f 45 78 70 72 20 69 73 20 70 6f 70 75 6c 61 74 65  Expr is populate
34040 64 20 77 69 74 68 20 61 6e 20 61 6c 6c 6f 63 61  d with an alloca
34041 74 65 64 20 46 74 73 33 45 78 70 72 20 0a 2a 2a  ted Fts3Expr .**
34042 20 73 74 72 75 63 74 75 72 65 2c 20 6f 72 20 73   structure, or s
34043 65 74 20 74 6f 20 30 20 69 66 20 74 68 65 20 65  et to 0 if the e
34044 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  nd of the input 
34045 62 75 66 66 65 72 20 69 73 20 72 65 61 63 68 65  buffer is reache
34046 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73  d..**.** Returns
34047 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
34048 20 63 6f 64 65 2e 20 53 51 4c 49 54 45 5f 4f 4b   code. SQLITE_OK
34049 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   if everything w
3404a 6f 72 6b 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  orks, SQLITE_NOM
3404b 45 4d 0a 2a 2a 20 69 66 20 61 20 6d 61 6c 6c 6f  EM.** if a mallo
3404c 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73  c failure occurs
3404d 2c 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f  , or SQLITE_ERRO
3404e 52 20 69 66 20 61 20 70 61 72 73 65 20 65 72 72  R if a parse err
3404f 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
34050 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  d..** If SQLITE_
34051 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65  ERROR is returne
34052 64 2c 20 70 43 6f 6e 74 65 78 74 20 69 73 20 70  d, pContext is p
34053 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e  opulated with an
34054 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
34055 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
34056 74 4e 65 78 74 4e 6f 64 65 28 0a 20 20 50 61 72  tNextNode(.  Par
34057 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73  seContext *pPars
34058 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
34059 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65       /* fts3 que
3405a 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  ry parse context
3405b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3405c 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20   *z, int n,     
3405d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3405e 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f   Input string */
3405f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70  .  Fts3Expr **pp
34060 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
34061 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
34062 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  T: expression */
34063 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d  .  int *pnConsum
34064 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ed              
34065 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
34066 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
34067 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29  es consumed */.)
34068 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
34069 20 73 74 72 75 63 74 20 46 74 73 33 4b 65 79 77   struct Fts3Keyw
3406a 6f 72 64 20 7b 0a 20 20 20 20 63 68 61 72 20 7a  ord {.    char z
3406b 5b 34 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  [4];            
3406c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3406d 2f 2a 20 4b 65 79 77 6f 72 64 20 74 65 78 74 20  /* Keyword text 
3406e 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  */.    unsigned 
3406f 63 68 61 72 20 6e 3b 20 20 20 20 20 20 20 20 20  char n;         
34070 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34071 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6b 65  Length of the ke
34072 79 77 6f 72 64 20 2a 2f 0a 20 20 20 20 75 6e 73  yword */.    uns
34073 69 67 6e 65 64 20 63 68 61 72 20 70 61 72 65 6e  igned char paren
34074 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20  Only;           
34075 20 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 64     /* Only valid
34076 20 69 6e 20 70 61 72 65 6e 20 6d 6f 64 65 20 2a   in paren mode *
34077 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  /.    unsigned c
34078 68 61 72 20 65 54 79 70 65 3b 20 20 20 20 20 20  har eType;      
34079 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b              /* K
3407a 65 79 77 6f 72 64 20 63 6f 64 65 20 2a 2f 0a 20  eyword code */. 
3407b 20 7d 20 61 4b 65 79 77 6f 72 64 5b 5d 20 3d 20   } aKeyword[] = 
3407c 7b 0a 20 20 20 20 7b 20 22 4f 52 22 20 2c 20 20  {.    { "OR" ,  
3407d 32 2c 20 30 2c 20 46 54 53 51 55 45 52 59 5f 4f  2, 0, FTSQUERY_O
3407e 52 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 41 4e  R   },.    { "AN
3407f 44 22 2c 20 20 33 2c 20 31 2c 20 46 54 53 51 55  D",  3, 1, FTSQU
34080 45 52 59 5f 41 4e 44 20 20 7d 2c 0a 20 20 20 20  ERY_AND  },.    
34081 7b 20 22 4e 4f 54 22 2c 20 20 33 2c 20 31 2c 20  { "NOT",  3, 1, 
34082 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20 7d 2c  FTSQUERY_NOT  },
34083 0a 20 20 20 20 7b 20 22 4e 45 41 52 22 2c 20 34  .    { "NEAR", 4
34084 2c 20 30 2c 20 46 54 53 51 55 45 52 59 5f 4e 45  , 0, FTSQUERY_NE
34085 41 52 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20  AR }.  };.  int 
34086 69 69 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a  ii;.  int iCol;.
34087 20 20 69 6e 74 20 69 43 6f 6c 4c 65 6e 3b 0a 20    int iColLen;. 
34088 20 69 6e 74 20 72 63 3b 0a 20 20 46 74 73 33 45   int rc;.  Fts3E
34089 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 0a  xpr *pRet = 0;..
3408a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
3408b 6e 70 75 74 20 3d 20 7a 3b 0a 20 20 69 6e 74 20  nput = z;.  int 
3408c 6e 49 6e 70 75 74 20 3d 20 6e 3b 0a 0a 20 20 2f  nInput = n;..  /
3408d 2a 20 53 6b 69 70 20 6f 76 65 72 20 61 6e 79 20  * Skip over any 
3408e 77 68 69 74 65 73 70 61 63 65 20 62 65 66 6f 72  whitespace befor
3408f 65 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 61  e checking for a
34090 20 6b 65 79 77 6f 72 64 2c 20 61 6e 20 6f 70 65   keyword, an ope
34091 6e 20 6f 72 0a 20 20 2a 2a 20 63 6c 6f 73 65 20  n or.  ** close 
34092 62 72 61 63 6b 65 74 2c 20 6f 72 20 61 20 71 75  bracket, or a qu
34093 6f 74 65 64 20 73 74 72 69 6e 67 2e 20 0a 20 20  oted string. .  
34094 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 49 6e 70  */.  while( nInp
34095 75 74 3e 30 20 26 26 20 66 74 73 33 69 73 73 70  ut>0 && fts3issp
34096 61 63 65 28 2a 7a 49 6e 70 75 74 29 20 29 7b 0a  ace(*zInput) ){.
34097 20 20 20 20 6e 49 6e 70 75 74 2d 2d 3b 0a 20 20      nInput--;.  
34098 20 20 7a 49 6e 70 75 74 2b 2b 3b 0a 20 20 7d 0a    zInput++;.  }.
34099 20 20 69 66 28 20 6e 49 6e 70 75 74 3d 3d 30 20    if( nInput==0 
3409a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3409b 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 0a  LITE_DONE;.  }..
3409c 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65 20 61    /* See if we a
3409d 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
3409e 61 20 6b 65 79 77 6f 72 64 2e 20 2a 2f 0a 20 20  a keyword. */.  
3409f 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 69 6e  for(ii=0; ii<(in
340a0 74 29 28 73 69 7a 65 6f 66 28 61 4b 65 79 77 6f  t)(sizeof(aKeywo
340a1 72 64 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63  rd)/sizeof(struc
340a2 74 20 46 74 73 33 4b 65 79 77 6f 72 64 29 29 3b  t Fts3Keyword));
340a3 20 69 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73   ii++){.    cons
340a4 74 20 73 74 72 75 63 74 20 46 74 73 33 4b 65 79  t struct Fts3Key
340a5 77 6f 72 64 20 2a 70 4b 65 79 20 3d 20 26 61 4b  word *pKey = &aK
340a6 65 79 77 6f 72 64 5b 69 69 5d 3b 0a 0a 20 20 20  eyword[ii];..   
340a7 20 69 66 28 20 28 70 4b 65 79 2d 3e 70 61 72 65   if( (pKey->pare
340a8 6e 4f 6e 6c 79 20 26 20 7e 73 71 6c 69 74 65 33  nOnly & ~sqlite3
340a9 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
340aa 65 6e 74 68 65 73 65 73 29 21 3d 30 20 29 7b 0a  entheses)!=0 ){.
340ab 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
340ac 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 6e      }..    if( n
340ad 49 6e 70 75 74 3e 3d 70 4b 65 79 2d 3e 6e 20 26  Input>=pKey->n &
340ae 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 49 6e 70  & 0==memcmp(zInp
340af 75 74 2c 20 70 4b 65 79 2d 3e 7a 2c 20 70 4b 65  ut, pKey->z, pKe
340b0 79 2d 3e 6e 29 20 29 7b 0a 20 20 20 20 20 20 69  y->n) ){.      i
340b1 6e 74 20 6e 4e 65 61 72 20 3d 20 53 51 4c 49 54  nt nNear = SQLIT
340b2 45 5f 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e  E_FTS3_DEFAULT_N
340b3 45 41 52 5f 50 41 52 41 4d 3b 0a 20 20 20 20 20  EAR_PARAM;.     
340b4 20 69 6e 74 20 6e 4b 65 79 20 3d 20 70 4b 65 79   int nKey = pKey
340b5 2d 3e 6e 3b 0a 20 20 20 20 20 20 63 68 61 72 20  ->n;.      char 
340b6 63 4e 65 78 74 3b 0a 0a 20 20 20 20 20 20 2f 2a  cNext;..      /*
340b7 20 49 66 20 74 68 69 73 20 69 73 20 61 20 22 4e   If this is a "N
340b8 45 41 52 22 20 6b 65 79 77 6f 72 64 2c 20 63 68  EAR" keyword, ch
340b9 65 63 6b 20 66 6f 72 20 61 6e 20 65 78 70 6c 69  eck for an expli
340ba 63 69 74 20 6e 65 61 72 6e 65 73 73 2e 20 2a 2f  cit nearness. */
340bb 0a 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 2d  .      if( pKey-
340bc 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
340bd 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20  _NEAR ){.       
340be 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 34   assert( nKey==4
340bf 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
340c0 7a 49 6e 70 75 74 5b 34 5d 3d 3d 27 2f 27 20 26  zInput[4]=='/' &
340c1 26 20 7a 49 6e 70 75 74 5b 35 5d 3e 3d 27 30 27  & zInput[5]>='0'
340c2 20 26 26 20 7a 49 6e 70 75 74 5b 35 5d 3c 3d 27   && zInput[5]<='
340c3 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  9' ){.          
340c4 6e 4e 65 61 72 20 3d 20 30 3b 0a 20 20 20 20 20  nNear = 0;.     
340c5 20 20 20 20 20 66 6f 72 28 6e 4b 65 79 3d 35 3b       for(nKey=5;
340c6 20 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3e 3d 27   zInput[nKey]>='
340c7 30 27 20 26 26 20 7a 49 6e 70 75 74 5b 6e 4b 65  0' && zInput[nKe
340c8 79 5d 3c 3d 27 39 27 3b 20 6e 4b 65 79 2b 2b 29  y]<='9'; nKey++)
340c9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e  {.            nN
340ca 65 61 72 20 3d 20 6e 4e 65 61 72 20 2a 20 31 30  ear = nNear * 10
340cb 20 2b 20 28 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d   + (zInput[nKey]
340cc 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20   - '0');.       
340cd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
340ce 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
340cf 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
340d0 68 69 73 20 69 73 20 70 72 6f 62 61 62 6c 79 20  his is probably 
340d1 61 20 6b 65 79 77 6f 72 64 2e 20 42 75 74 20 66  a keyword. But f
340d2 6f 72 20 74 68 61 74 20 74 6f 20 62 65 20 74 72  or that to be tr
340d3 75 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ue,.      ** the
340d4 20 6e 65 78 74 20 62 79 74 65 20 6d 75 73 74 20   next byte must 
340d5 63 6f 6e 74 61 69 6e 20 65 69 74 68 65 72 20 77  contain either w
340d6 68 69 74 65 73 70 61 63 65 2c 20 61 6e 20 6f 70  hitespace, an op
340d7 65 6e 20 6f 72 20 63 6c 6f 73 65 0a 20 20 20 20  en or close.    
340d8 20 20 2a 2a 20 70 61 72 65 6e 74 68 65 73 69 73    ** parenthesis
340d9 2c 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 63  , a quote charac
340da 74 65 72 2c 20 6f 72 20 45 4f 46 2e 20 0a 20 20  ter, or EOF. .  
340db 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 4e 65      */.      cNe
340dc 78 74 20 3d 20 7a 49 6e 70 75 74 5b 6e 4b 65 79  xt = zInput[nKey
340dd 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 66 74 73  ];.      if( fts
340de 33 69 73 73 70 61 63 65 28 63 4e 65 78 74 29 20  3isspace(cNext) 
340df 0a 20 20 20 20 20 20 20 7c 7c 20 63 4e 65 78 74  .       || cNext
340e0 3d 3d 27 22 27 20 7c 7c 20 63 4e 65 78 74 3d 3d  =='"' || cNext==
340e1 27 28 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 29  '(' || cNext==')
340e2 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 30 0a 20 20  ' || cNext==0.  
340e3 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70      ){.        p
340e4 52 65 74 20 3d 20 28 46 74 73 33 45 78 70 72 20  Ret = (Fts3Expr 
340e5 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
340e6 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72  (sizeof(Fts3Expr
340e7 29 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  ));.        mems
340e8 65 74 28 70 52 65 74 2c 20 30 2c 20 73 69 7a 65  et(pRet, 0, size
340e9 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
340ea 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65 54 79         pRet->eTy
340eb 70 65 20 3d 20 70 4b 65 79 2d 3e 65 54 79 70 65  pe = pKey->eType
340ec 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ;.        pRet->
340ed 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72 3b 0a 20  nNear = nNear;. 
340ee 20 20 20 20 20 20 20 2a 70 70 45 78 70 72 20 3d         *ppExpr =
340ef 20 70 52 65 74 3b 0a 20 20 20 20 20 20 20 20 2a   pRet;.        *
340f0 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49  pnConsumed = (zI
340f1 6e 70 75 74 20 2d 20 7a 29 20 2b 20 6e 4b 65 79  nput - z) + nKey
340f2 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
340f3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
340f4 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 75    }..      /* Tu
340f5 72 6e 73 20 6f 75 74 20 74 68 61 74 20 77 61 73  rns out that was
340f6 6e 27 74 20 61 20 6b 65 79 77 6f 72 64 20 61 66  n't a keyword af
340f7 74 65 72 20 61 6c 6c 2e 20 54 68 69 73 20 68 61  ter all. This ha
340f8 70 70 65 6e 73 20 69 66 20 74 68 65 0a 20 20 20  ppens if the.   
340f9 20 20 20 2a 2a 20 75 73 65 72 20 68 61 73 20 73     ** user has s
340fa 75 70 70 6c 69 65 64 20 61 20 74 6f 6b 65 6e 20  upplied a token 
340fb 73 75 63 68 20 61 73 20 22 4f 52 61 63 6c 65 22  such as "ORacle"
340fc 2e 20 43 6f 6e 74 69 6e 75 65 2e 0a 20 20 20 20  . Continue..    
340fd 20 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a    */.    }.  }..
340fe 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61    /* Check for a
340ff 6e 20 6f 70 65 6e 20 62 72 61 63 6b 65 74 2e 20  n open bracket. 
34100 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
34101 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
34102 65 6e 74 68 65 73 65 73 20 29 7b 0a 20 20 20 20  entheses ){.    
34103 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d 27 28 27  if( *zInput=='('
34104 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43   ){.      int nC
34105 6f 6e 73 75 6d 65 64 3b 0a 20 20 20 20 20 20 69  onsumed;.      i
34106 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 70 50 61  nt rc;.      pPa
34107 72 73 65 2d 3e 6e 4e 65 73 74 2b 2b 3b 0a 20 20  rse->nNest++;.  
34108 20 20 20 20 72 63 20 3d 20 66 74 73 33 45 78 70      rc = fts3Exp
34109 72 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 26  rParse(pParse, &
3410a 7a 49 6e 70 75 74 5b 31 5d 2c 20 6e 49 6e 70 75  zInput[1], nInpu
3410b 74 2d 31 2c 20 70 70 45 78 70 72 2c 20 26 6e 43  t-1, ppExpr, &nC
3410c 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 20 20 20 20  onsumed);.      
3410d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3410e 4b 20 26 26 20 21 2a 70 70 45 78 70 72 20 29 7b  K && !*ppExpr ){
3410f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
34110 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20  LITE_DONE;.     
34111 20 7d 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73   }.      *pnCons
34112 75 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d  umed = (zInput -
34113 20 7a 29 20 2b 20 31 20 2b 20 6e 43 6f 6e 73 75   z) + 1 + nConsu
34114 6d 65 64 3b 0a 20 20 20 20 20 20 72 65 74 75 72  med;.      retur
34115 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  n rc;.    }.  . 
34116 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20     /* Check for 
34117 61 20 63 6c 6f 73 65 20 62 72 61 63 6b 65 74 2e  a close bracket.
34118 20 2a 2f 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e   */.    if( *zIn
34119 70 75 74 3d 3d 27 29 27 20 29 7b 0a 20 20 20 20  put==')' ){.    
3411a 20 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2d    pParse->nNest-
3411b 2d 3b 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73  -;.      *pnCons
3411c 75 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d  umed = (zInput -
3411d 20 7a 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 72   z) + 1;.      r
3411e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
3411f 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  E;.    }.  }..  
34120 2f 2a 20 53 65 65 20 69 66 20 77 65 20 61 72 65  /* See if we are
34121 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20   dealing with a 
34122 71 75 6f 74 65 64 20 70 68 72 61 73 65 2e 20 49  quoted phrase. I
34123 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
34124 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 73 65  se, then.  ** se
34125 61 72 63 68 20 66 6f 72 20 74 68 65 20 63 6c 6f  arch for the clo
34126 73 69 6e 67 20 71 75 6f 74 65 20 61 6e 64 20 70  sing quote and p
34127 61 73 73 20 74 68 65 20 77 68 6f 6c 65 20 73 74  ass the whole st
34128 72 69 6e 67 20 74 6f 20 67 65 74 4e 65 78 74 53  ring to getNextS
34129 74 72 69 6e 67 28 29 0a 20 20 2a 2a 20 66 6f 72  tring().  ** for
3412a 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 54 68 69   processing. Thi
3412b 73 20 69 73 20 65 61 73 79 20 74 6f 20 64 6f 2c  s is easy to do,
3412c 20 61 73 20 66 74 73 33 20 68 61 73 20 6e 6f 20   as fts3 has no 
3412d 73 79 6e 74 61 78 20 66 6f 72 20 65 73 63 61 70  syntax for escap
3412e 69 6e 67 0a 20 20 2a 2a 20 61 20 71 75 6f 74 65  ing.  ** a quote
3412f 20 63 68 61 72 61 63 74 65 72 20 65 6d 62 65 64   character embed
34130 64 65 64 20 69 6e 20 61 20 73 74 72 69 6e 67 2e  ded in a string.
34131 0a 20 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 49 6e  .  */.  if( *zIn
34132 70 75 74 3d 3d 27 22 27 20 29 7b 0a 20 20 20 20  put=='"' ){.    
34133 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e 49 6e  for(ii=1; ii<nIn
34134 70 75 74 20 26 26 20 7a 49 6e 70 75 74 5b 69 69  put && zInput[ii
34135 5d 21 3d 27 22 27 3b 20 69 69 2b 2b 29 3b 0a 20  ]!='"'; ii++);. 
34136 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d     *pnConsumed =
34137 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20   (zInput - z) + 
34138 69 69 20 2b 20 31 3b 0a 20 20 20 20 69 66 28 20  ii + 1;.    if( 
34139 69 69 3d 3d 6e 49 6e 70 75 74 20 29 7b 0a 20 20  ii==nInput ){.  
3413a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3413b 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
3413c 20 20 20 72 65 74 75 72 6e 20 67 65 74 4e 65 78     return getNex
3413d 74 53 74 72 69 6e 67 28 70 50 61 72 73 65 2c 20  tString(pParse, 
3413e 26 7a 49 6e 70 75 74 5b 31 5d 2c 20 69 69 2d 31  &zInput[1], ii-1
3413f 2c 20 70 70 45 78 70 72 29 3b 0a 20 20 7d 0a 0a  , ppExpr);.  }..
34140 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c  .  /* If control
34141 20 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70   flows to this p
34142 6f 69 6e 74 2c 20 74 68 69 73 20 6d 75 73 74 20  oint, this must 
34143 62 65 20 61 20 72 65 67 75 6c 61 72 20 74 6f 6b  be a regular tok
34144 65 6e 2c 20 6f 72 20 0a 20 20 2a 2a 20 74 68 65  en, or .  ** the
34145 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75   end of the inpu
34146 74 2e 20 52 65 61 64 20 61 20 72 65 67 75 6c 61  t. Read a regula
34147 72 20 74 6f 6b 65 6e 20 75 73 69 6e 67 20 74 68  r token using th
34148 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e sqlite3_tokeni
34149 7a 65 72 0a 20 20 2a 2a 20 69 6e 74 65 72 66 61  zer.  ** interfa
3414a 63 65 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67  ce. Before doing
3414b 20 73 6f 2c 20 66 69 67 75 72 65 20 6f 75 74 20   so, figure out 
3414c 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  if there is an e
3414d 78 70 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f 6c  xplicit.  ** col
3414e 75 6d 6e 20 73 70 65 63 69 66 69 65 72 20 66 6f  umn specifier fo
3414f 72 20 74 68 65 20 74 6f 6b 65 6e 2e 20 0a 20 20  r the token. .  
34150 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 53 74  **.  ** TODO: St
34151 72 61 6e 67 65 6c 79 2c 20 69 74 20 69 73 20 6e  rangely, it is n
34152 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61  ot possible to a
34153 73 73 6f 63 69 61 74 65 20 61 20 63 6f 6c 75 6d  ssociate a colum
34154 6e 20 73 70 65 63 69 66 69 65 72 0a 20 20 2a 2a  n specifier.  **
34155 20 77 69 74 68 20 61 20 71 75 6f 74 65 64 20 70   with a quoted p
34156 68 72 61 73 65 2c 20 6f 6e 6c 79 20 77 69 74 68  hrase, only with
34157 20 61 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 2e   a single token.
34158 20 4e 6f 74 20 73 75 72 65 20 69 66 20 74 68 69   Not sure if thi
34159 73 20 77 61 73 0a 20 20 2a 2a 20 61 6e 20 69 6d  s was.  ** an im
3415a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 72 74  plementation art
3415b 69 66 61 63 74 20 6f 72 20 61 6e 20 69 6e 74 65  ifact or an inte
3415c 6e 74 69 6f 6e 61 6c 20 64 65 63 69 73 69 6f 6e  ntional decision
3415d 20 77 68 65 6e 20 66 74 73 33 20 77 61 73 0a 20   when fts3 was. 
3415e 20 2a 2a 20 66 69 72 73 74 20 69 6d 70 6c 65 6d   ** first implem
3415f 65 6e 74 65 64 2e 20 57 68 69 63 68 65 76 65 72  ented. Whichever
34160 20 69 74 20 77 61 73 2c 20 74 68 69 73 20 6d 6f   it was, this mo
34161 64 75 6c 65 20 64 75 70 6c 69 63 61 74 65 73 20  dule duplicates 
34162 74 68 65 20 0a 20 20 2a 2a 20 6c 69 6d 69 74 61  the .  ** limita
34163 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 43 6f  tion..  */.  iCo
34164 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 44 65 66  l = pParse->iDef
34165 61 75 6c 74 43 6f 6c 3b 0a 20 20 69 43 6f 6c 4c  aultCol;.  iColL
34166 65 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69  en = 0;.  for(ii
34167 3d 30 3b 20 69 69 3c 70 50 61 72 73 65 2d 3e 6e  =0; ii<pParse->n
34168 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  Col; ii++){.    
34169 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 72  const char *zStr
3416a 20 3d 20 70 50 61 72 73 65 2d 3e 61 7a 43 6f 6c   = pParse->azCol
3416b 5b 69 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 53  [ii];.    int nS
3416c 74 72 20 3d 20 73 74 72 6c 65 6e 28 7a 53 74 72  tr = strlen(zStr
3416d 29 3b 0a 20 20 20 20 69 66 28 20 6e 49 6e 70 75  );.    if( nInpu
3416e 74 3e 6e 53 74 72 20 26 26 20 7a 49 6e 70 75 74  t>nStr && zInput
3416f 5b 6e 53 74 72 5d 3d 3d 27 3a 27 20 26 26 20 6d  [nStr]==':' && m
34170 65 6d 63 6d 70 28 7a 53 74 72 2c 20 7a 49 6e 70  emcmp(zStr, zInp
34171 75 74 2c 20 6e 53 74 72 29 3d 3d 30 20 29 7b 0a  ut, nStr)==0 ){.
34172 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 69 69 3b        iCol = ii;
34173 0a 20 20 20 20 20 20 69 43 6f 6c 4c 65 6e 20 3d  .      iColLen =
34174 20 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b   ((zInput - z) +
34175 20 6e 53 74 72 20 2b 20 31 29 3b 0a 20 20 20 20   nStr + 1);.    
34176 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
34177 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 4e 65 78   }.  rc = getNex
34178 74 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c 20 69  tToken(pParse, i
34179 43 6f 6c 2c 20 26 7a 5b 69 43 6f 6c 4c 65 6e 5d  Col, &z[iColLen]
3417a 2c 20 6e 2d 69 43 6f 6c 4c 65 6e 2c 20 70 70 45  , n-iColLen, ppE
3417b 78 70 72 2c 20 70 6e 43 6f 6e 73 75 6d 65 64 29  xpr, pnConsumed)
3417c 3b 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  ;.  *pnConsumed 
3417d 2b 3d 20 69 43 6f 6c 4c 65 6e 3b 0a 20 20 72 65  += iColLen;.  re
3417e 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
3417f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
34180 73 20 61 6e 20 46 74 73 33 45 78 70 72 20 73 74  s an Fts3Expr st
34181 72 75 63 74 75 72 65 20 66 6f 72 20 61 20 62 69  ructure for a bi
34182 6e 61 72 79 20 6f 70 65 72 61 74 6f 72 20 28 61  nary operator (a
34183 6e 79 20 74 79 70 65 0a 2a 2a 20 65 78 63 65 70  ny type.** excep
34184 74 20 61 6e 20 46 54 53 51 55 45 52 59 5f 50 48  t an FTSQUERY_PH
34185 52 41 53 45 29 2e 20 52 65 74 75 72 6e 20 61 6e  RASE). Return an
34186 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 72   integer value r
34187 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 0a  epresenting the.
34188 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20 6f 66  ** precedence of
34189 20 74 68 65 20 6f 70 65 72 61 74 6f 72 2e 20 4c   the operator. L
3418a 6f 77 65 72 20 76 61 6c 75 65 73 20 68 61 76 65  ower values have
3418b 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65 64   a higher preced
3418c 65 6e 63 65 20 28 69 2e 65 2e 0a 2a 2a 20 67 72  ence (i.e..** gr
3418d 6f 75 70 20 6d 6f 72 65 20 74 69 67 68 74 6c 79  oup more tightly
3418e 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ). For example, 
3418f 69 6e 20 74 68 65 20 43 20 6c 61 6e 67 75 61 67  in the C languag
34190 65 2c 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74  e, the == operat
34191 6f 72 0a 2a 2a 20 67 72 6f 75 70 73 20 6d 6f 72  or.** groups mor
34192 65 20 74 69 67 68 74 6c 79 20 74 68 61 6e 20 7c  e tightly than |
34193 7c 2c 20 61 6e 64 20 77 6f 75 6c 64 20 74 68 65  |, and would the
34194 72 65 66 6f 72 65 20 68 61 76 65 20 61 20 68 69  refore have a hi
34195 67 68 65 72 20 70 72 65 63 65 64 65 6e 63 65 2e  gher precedence.
34196 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 75 73 69 6e  .**.** When usin
34197 67 20 74 68 65 20 6e 65 77 20 66 74 73 33 20 71  g the new fts3 q
34198 75 65 72 79 20 73 79 6e 74 61 78 20 28 77 68 65  uery syntax (whe
34199 6e 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  n SQLITE_ENABLE_
3419a 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53  FTS3_PARENTHESIS
3419b 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 29 2c  .** is defined),
3419c 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
3419d 65 20 6f 70 65 72 61 74 6f 72 73 20 69 6e 20 70  e operators in p
3419e 72 65 63 65 64 65 6e 63 65 20 66 72 6f 6d 20 68  recedence from h
3419f 69 67 68 65 73 74 20 74 6f 0a 2a 2a 20 6c 6f 77  ighest to.** low
341a0 65 73 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  est is:.**.**   
341a1 4e 45 41 52 0a 2a 2a 20 20 20 4e 4f 54 0a 2a 2a  NEAR.**   NOT.**
341a2 20 20 20 41 4e 44 20 28 69 6e 63 6c 75 64 69 6e     AND (includin
341a3 67 20 69 6d 70 6c 69 63 69 74 20 41 4e 44 73 29  g implicit ANDs)
341a4 0a 2a 2a 20 20 20 4f 52 0a 2a 2a 0a 2a 2a 20 4e  .**   OR.**.** N
341a5 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 75 73  ote that when us
341a6 69 6e 67 20 74 68 65 20 6f 6c 64 20 71 75 65 72  ing the old quer
341a7 79 20 73 79 6e 74 61 78 2c 20 74 68 65 20 4f 52  y syntax, the OR
341a8 20 6f 70 65 72 61 74 6f 72 20 68 61 73 20 61 20   operator has a 
341a9 68 69 67 68 65 72 0a 2a 2a 20 70 72 65 63 65 64  higher.** preced
341aa 65 6e 63 65 20 74 68 61 6e 20 74 68 65 20 41 4e  ence than the AN
341ab 44 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73  D operator..*/.s
341ac 74 61 74 69 63 20 69 6e 74 20 6f 70 50 72 65 63  tatic int opPrec
341ad 65 64 65 6e 63 65 28 46 74 73 33 45 78 70 72 20  edence(Fts3Expr 
341ae 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
341af 2d 3e 65 54 79 70 65 21 3d 46 54 53 51 55 45 52  ->eType!=FTSQUER
341b0 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 69 66  Y_PHRASE );.  if
341b1 28 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65  ( sqlite3_fts3_e
341b2 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
341b3 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  s ){.    return 
341b4 70 2d 3e 65 54 79 70 65 3b 0a 20 20 7d 65 6c 73  p->eType;.  }els
341b5 65 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d  e if( p->eType==
341b6 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b  FTSQUERY_NEAR ){
341b7 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
341b8 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 54   }else if( p->eT
341b9 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4f 52  ype==FTSQUERY_OR
341ba 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
341bb 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
341bc 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
341bd 52 59 5f 41 4e 44 20 29 3b 0a 20 20 72 65 74 75  RY_AND );.  retu
341be 72 6e 20 33 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 3;.}../*.** A
341bf 72 67 75 6d 65 6e 74 20 70 70 48 65 61 64 20 63  rgument ppHead c
341c0 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
341c1 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  r to the current
341c2 20 68 65 61 64 20 6f 66 20 61 20 71 75 65 72 79   head of a query
341c3 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20   .** expression 
341c4 74 72 65 65 20 62 65 69 6e 67 20 70 61 72 73 65  tree being parse
341c5 64 2e 20 70 50 72 65 76 20 69 73 20 74 68 65 20  d. pPrev is the 
341c6 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
341c7 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a  most recently.**
341c8 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
341c9 68 65 20 74 72 65 65 2e 20 54 68 69 73 20 66 75  he tree. This fu
341ca 6e 63 74 69 6f 6e 20 61 64 64 73 20 70 4e 65 77  nction adds pNew
341cb 2c 20 77 68 69 63 68 20 69 73 20 61 6c 77 61 79  , which is alway
341cc 73 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 6f 70  s a binary.** op
341cd 65 72 61 74 6f 72 20 6e 6f 64 65 2c 20 69 6e 74  erator node, int
341ce 6f 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  o the expression
341cf 20 74 72 65 65 20 62 61 73 65 64 20 6f 6e 20 74   tree based on t
341d0 68 65 20 72 65 6c 61 74 69 76 65 20 70 72 65 63  he relative prec
341d1 65 64 65 6e 63 65 0a 2a 2a 20 6f 66 20 70 4e 65  edence.** of pNe
341d2 77 20 61 6e 64 20 74 68 65 20 65 78 69 73 74 69  w and the existi
341d3 6e 67 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 20  ng nodes of the 
341d4 74 72 65 65 2e 20 54 68 69 73 20 6d 61 79 20 72  tree. This may r
341d5 65 73 75 6c 74 20 69 6e 20 74 68 65 20 68 65 61  esult in the hea
341d6 64 0a 2a 2a 20 6f 66 20 74 68 65 20 74 72 65 65  d.** of the tree
341d7 20 63 68 61 6e 67 69 6e 67 2c 20 69 6e 20 77 68   changing, in wh
341d8 69 63 68 20 63 61 73 65 20 2a 70 70 48 65 61 64  ich case *ppHead
341d9 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
341da 65 77 20 72 6f 6f 74 20 6e 6f 64 65 2e 0a 2a 2f  ew root node..*/
341db 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73  .static void ins
341dc 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f  ertBinaryOperato
341dd 72 28 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  r(.  Fts3Expr **
341de 70 70 48 65 61 64 2c 20 20 20 20 20 20 20 2f 2a  ppHead,       /*
341df 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
341e0 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 61 20 74  root node of a t
341e1 72 65 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  ree */.  Fts3Exp
341e2 72 20 2a 70 50 72 65 76 2c 20 20 20 20 20 20 20  r *pPrev,       
341e3 20 20 2f 2a 20 4e 6f 64 65 20 6d 6f 73 74 20 72    /* Node most r
341e4 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64  ecently inserted
341e5 20 69 6e 74 6f 20 74 68 65 20 74 72 65 65 20 2a   into the tree *
341e6 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 4e  /.  Fts3Expr *pN
341e7 65 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ew           /* 
341e8 4e 65 77 20 62 69 6e 61 72 79 20 6e 6f 64 65 20  New binary node 
341e9 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 65  to insert into e
341ea 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 2a  xpression tree *
341eb 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  /.){.  Fts3Expr 
341ec 2a 70 53 70 6c 69 74 20 3d 20 70 50 72 65 76 3b  *pSplit = pPrev;
341ed 0a 20 20 77 68 69 6c 65 28 20 70 53 70 6c 69 74  .  while( pSplit
341ee 2d 3e 70 50 61 72 65 6e 74 20 26 26 20 6f 70 50  ->pParent && opP
341ef 72 65 63 65 64 65 6e 63 65 28 70 53 70 6c 69 74  recedence(pSplit
341f0 2d 3e 70 50 61 72 65 6e 74 29 3c 3d 6f 70 50 72  ->pParent)<=opPr
341f1 65 63 65 64 65 6e 63 65 28 70 4e 65 77 29 20 29  ecedence(pNew) )
341f2 7b 0a 20 20 20 20 70 53 70 6c 69 74 20 3d 20 70  {.    pSplit = p
341f3 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b 0a  Split->pParent;.
341f4 20 20 7d 0a 0a 20 20 69 66 28 20 70 53 70 6c 69    }..  if( pSpli
341f5 74 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20  t->pParent ){.  
341f6 20 20 61 73 73 65 72 74 28 20 70 53 70 6c 69 74    assert( pSplit
341f7 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
341f8 74 3d 3d 70 53 70 6c 69 74 20 29 3b 0a 20 20 20  t==pSplit );.   
341f9 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74   pSplit->pParent
341fa 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e 65 77 3b  ->pRight = pNew;
341fb 0a 20 20 20 20 70 4e 65 77 2d 3e 70 50 61 72 65  .    pNew->pPare
341fc 6e 74 20 3d 20 70 53 70 6c 69 74 2d 3e 70 50 61  nt = pSplit->pPa
341fd 72 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rent;.  }else{. 
341fe 20 20 20 2a 70 70 48 65 61 64 20 3d 20 70 4e 65     *ppHead = pNe
341ff 77 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70  w;.  }.  pNew->p
34200 4c 65 66 74 20 3d 20 70 53 70 6c 69 74 3b 0a 20  Left = pSplit;. 
34201 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74   pSplit->pParent
34202 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a   = pNew;.}../*.*
34203 2a 20 50 61 72 73 65 20 74 68 65 20 66 74 73 33  * Parse the fts3
34204 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
34205 6e 20 66 6f 75 6e 64 20 69 6e 20 62 75 66 66 65  n found in buffe
34206 72 20 7a 2c 20 6c 65 6e 67 74 68 20 6e 2e 20 54  r z, length n. T
34207 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
34208 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20 77  returns either w
34209 68 65 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 74  hen the end of t
3420a 68 65 20 62 75 66 66 65 72 20 69 73 20 72 65 61  he buffer is rea
3420b 63 68 65 64 20 6f 72 20 61 6e 20 75 6e 6d 61 74  ched or an unmat
3420c 63 68 65 64 20 0a 2a 2a 20 63 6c 6f 73 69 6e 67  ched .** closing
3420d 20 62 72 61 63 6b 65 74 20 2d 20 27 29 27 20 2d   bracket - ')' -
3420e 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e   is encountered.
3420f 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
34210 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  sful, SQLITE_OK 
34211 69 73 20 72 65 74 75 72 6e 65 64 2c 20 2a 70 70  is returned, *pp
34212 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 70  Expr is set to p
34213 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70  oint to the.** p
34214 61 72 73 65 64 20 66 6f 72 6d 20 6f 66 20 74 68  arsed form of th
34215 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  e expression and
34216 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 69 73 20   *pnConsumed is 
34217 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
34218 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 72 65  r of.** bytes re
34219 61 64 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a  ad from buffer z
3421a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 2a 70 70  . Otherwise, *pp
3421b 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 30  Expr is set to 0
3421c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4e 4f 4d 45   and SQLITE_NOME
3421d 4d 0a 2a 2a 20 28 6f 75 74 20 6f 66 20 6d 65 6d  M.** (out of mem
3421e 6f 72 79 20 65 72 72 6f 72 29 20 6f 72 20 53 51  ory error) or SQ
3421f 4c 49 54 45 5f 45 52 52 4f 52 20 28 70 61 72 73  LITE_ERROR (pars
34220 65 20 65 72 72 6f 72 29 20 69 73 20 72 65 74 75  e error) is retu
34221 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
34222 69 6e 74 20 66 74 73 33 45 78 70 72 50 61 72 73  int fts3ExprPars
34223 65 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  e(.  ParseContex
34224 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
34225 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34226 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
34227 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
34228 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
34229 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
3422a 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66        /* Text of
3422b 20 4d 41 54 43 48 20 71 75 65 72 79 20 2a 2f 0a   MATCH query */.
3422c 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45    Fts3Expr **ppE
3422d 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  xpr,            
3422e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
3422f 3a 20 50 61 72 73 65 64 20 71 75 65 72 79 20 73  : Parsed query s
34230 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
34231 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20  t *pnConsumed   
34232 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34233 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
34234 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f  mber of bytes co
34235 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 46  nsumed */.){.  F
34236 74 73 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20  ts3Expr *pRet = 
34237 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  0;.  Fts3Expr *p
34238 50 72 65 76 20 3d 20 30 3b 0a 20 20 46 74 73 33  Prev = 0;.  Fts3
34239 45 78 70 72 20 2a 70 4e 6f 74 42 72 61 6e 63 68  Expr *pNotBranch
3423a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
3423b 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 75 73 65 64      /* Only used
3423c 20 69 6e 20 6c 65 67 61 63 79 20 70 61 72 73 65   in legacy parse
3423d 20 6d 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   mode */.  int n
3423e 49 6e 20 3d 20 6e 3b 0a 20 20 63 6f 6e 73 74 20  In = n;.  const 
3423f 63 68 61 72 20 2a 7a 49 6e 20 3d 20 7a 3b 0a 20  char *zIn = z;. 
34240 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
34241 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73 52 65 71  _OK;.  int isReq
34242 75 69 72 65 50 68 72 61 73 65 20 3d 20 31 3b 0a  uirePhrase = 1;.
34243 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
34244 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 46  LITE_OK ){.    F
34245 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  ts3Expr *p = 0;.
34246 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20      int nByte = 
34247 30 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 4e  0;.    rc = getN
34248 65 78 74 4e 6f 64 65 28 70 50 61 72 73 65 2c 20  extNode(pParse, 
34249 7a 49 6e 2c 20 6e 49 6e 2c 20 26 70 2c 20 26 6e  zIn, nIn, &p, &n
3424a 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Byte);.    if( r
3424b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3424c 20 20 20 20 20 20 69 6e 74 20 69 73 50 68 72 61        int isPhra
3424d 73 65 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 21  se;..      if( !
3424e 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61  sqlite3_fts3_ena
3424f 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20  ble_parentheses 
34250 0a 20 20 20 20 20 20 20 26 26 20 70 2d 3e 65 54  .       && p->eT
34251 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
34252 52 41 53 45 20 26 26 20 70 2d 3e 70 50 68 72 61  RASE && p->pPhra
34253 73 65 2d 3e 69 73 4e 6f 74 20 0a 20 20 20 20 20  se->isNot .     
34254 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
34255 72 65 61 74 65 20 61 6e 20 69 6d 70 6c 69 63 69  reate an implici
34256 74 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 20  t NOT operator. 
34257 2a 2f 0a 20 20 20 20 20 20 20 20 46 74 73 33 45  */.        Fts3E
34258 78 70 72 20 2a 70 4e 6f 74 20 3d 20 73 71 6c 69  xpr *pNot = sqli
34259 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
3425a 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
3425b 20 20 20 20 20 20 69 66 28 20 21 70 4e 6f 74 20        if( !pNot 
3425c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
3425d 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
3425e 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  (p);.          r
3425f 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
34260 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
34261 20 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a   exprparse_out;.
34262 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
34263 20 20 6d 65 6d 73 65 74 28 70 4e 6f 74 2c 20 30    memset(pNot, 0
34264 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70  , sizeof(Fts3Exp
34265 72 29 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f  r));.        pNo
34266 74 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  t->eType = FTSQU
34267 45 52 59 5f 4e 4f 54 3b 0a 20 20 20 20 20 20 20  ERY_NOT;.       
34268 20 70 4e 6f 74 2d 3e 70 52 69 67 68 74 20 3d 20   pNot->pRight = 
34269 70 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  p;.        if( p
3426a 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0a 20 20 20  NotBranch ){.   
3426b 20 20 20 20 20 20 20 70 4e 6f 74 42 72 61 6e 63         pNotBranc
3426c 68 2d 3e 70 4c 65 66 74 20 3d 20 70 3b 0a 20 20  h->pLeft = p;.  
3426d 20 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e 70 52          pNot->pR
3426e 69 67 68 74 20 3d 20 70 4e 6f 74 42 72 61 6e 63  ight = pNotBranc
3426f 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  h;.        }.   
34270 20 20 20 20 20 70 4e 6f 74 42 72 61 6e 63 68 20       pNotBranch 
34271 3d 20 70 4e 6f 74 3b 0a 20 20 20 20 20 20 7d 65  = pNot;.      }e
34272 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
34273 20 65 54 79 70 65 20 3d 20 70 2d 3e 65 54 79 70   eType = p->eTyp
34274 65 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e;.        asser
34275 74 28 20 65 54 79 70 65 21 3d 46 54 53 51 55 45  t( eType!=FTSQUE
34276 52 59 5f 50 48 52 41 53 45 20 7c 7c 20 21 70 2d  RY_PHRASE || !p-
34277 3e 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20  >pPhrase->isNot 
34278 29 3b 0a 20 20 20 20 20 20 20 20 69 73 50 68 72  );.        isPhr
34279 61 73 65 20 3d 20 28 65 54 79 70 65 3d 3d 46 54  ase = (eType==FT
3427a 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 7c 7c  SQUERY_PHRASE ||
3427b 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 20   p->pLeft);..   
3427c 20 20 20 20 20 2f 2a 20 54 68 65 20 69 73 52 65       /* The isRe
3427d 71 75 69 72 65 50 68 72 61 73 65 20 76 61 72 69  quirePhrase vari
3427e 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74  able is set to t
3427f 72 75 65 20 69 66 20 61 20 70 68 72 61 73 65 20  rue if a phrase 
34280 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  or.        ** an
34281 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74   expression cont
34282 61 69 6e 65 64 20 69 6e 20 70 61 72 65 6e 74 68  ained in parenth
34283 65 73 69 73 20 69 73 20 72 65 71 75 69 72 65 64  esis is required
34284 2e 20 49 66 20 61 0a 20 20 20 20 20 20 20 20 2a  . If a.        *
34285 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 6f  * binary operato
34286 72 20 28 41 4e 44 2c 20 4f 52 2c 20 4e 4f 54 20  r (AND, OR, NOT 
34287 6f 72 20 4e 45 41 52 29 20 69 73 20 65 6e 63 6f  or NEAR) is enco
34288 75 6e 74 65 64 20 77 68 65 6e 0a 20 20 20 20 20  unted when.     
34289 20 20 20 2a 2a 20 69 73 52 65 71 75 69 72 65 50     ** isRequireP
3428a 68 72 61 73 65 20 69 73 20 73 65 74 2c 20 74 68  hrase is set, th
3428b 69 73 20 69 73 20 61 20 73 79 6e 74 61 78 20 65  is is a syntax e
3428c 72 72 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f  rror..        */
3428d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73  .        if( !is
3428e 50 68 72 61 73 65 20 26 26 20 69 73 52 65 71 75  Phrase && isRequ
3428f 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20 20  irePhrase ){.   
34290 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74         sqlite3Ft
34291 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20  s3ExprFree(p);. 
34292 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
34293 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
34294 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70        goto exprp
34295 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20  arse_out;.      
34296 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 69    }.  .        i
34297 66 28 20 69 73 50 68 72 61 73 65 20 26 26 20 21  f( isPhrase && !
34298 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
34299 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
3429a 49 6e 73 65 72 74 20 61 6e 20 69 6d 70 6c 69 63  Insert an implic
3429b 69 74 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e  it AND operator.
3429c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 46 74   */.          Ft
3429d 73 33 45 78 70 72 20 2a 70 41 6e 64 3b 0a 20 20  s3Expr *pAnd;.  
3429e 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
3429f 70 52 65 74 20 26 26 20 70 50 72 65 76 20 29 3b  pRet && pPrev );
342a0 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64 20  .          pAnd 
342a1 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
342a2 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72  (sizeof(Fts3Expr
342a3 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ));.          if
342a4 28 20 21 70 41 6e 64 20 29 7b 0a 20 20 20 20 20  ( !pAnd ){.     
342a5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74         sqlite3Ft
342a6 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20  s3ExprFree(p);. 
342a7 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
342a8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
342a9 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
342aa 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20  xprparse_out;.  
342ab 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
342ac 20 20 20 20 6d 65 6d 73 65 74 28 70 41 6e 64 2c      memset(pAnd,
342ad 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 45   0, sizeof(Fts3E
342ae 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20 20  xpr));.         
342af 20 70 41 6e 64 2d 3e 65 54 79 70 65 20 3d 20 46   pAnd->eType = F
342b0 54 53 51 55 45 52 59 5f 41 4e 44 3b 0a 20 20 20  TSQUERY_AND;.   
342b1 20 20 20 20 20 20 20 69 6e 73 65 72 74 42 69 6e         insertBin
342b2 61 72 79 4f 70 65 72 61 74 6f 72 28 26 70 52 65  aryOperator(&pRe
342b3 74 2c 20 70 50 72 65 76 2c 20 70 41 6e 64 29 3b  t, pPrev, pAnd);
342b4 0a 20 20 20 20 20 20 20 20 20 20 70 50 72 65 76  .          pPrev
342b5 20 3d 20 70 41 6e 64 3b 0a 20 20 20 20 20 20 20   = pAnd;.       
342b6 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   }..        /* T
342b7 68 69 73 20 74 65 73 74 20 63 61 74 63 68 65 73  his test catches
342b8 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6d 61 6b   attempts to mak
342b9 65 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  e either operand
342ba 20 6f 66 20 61 20 4e 45 41 52 0a 20 20 20 20 20   of a NEAR.     
342bb 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20 73     ** operator s
342bc 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74  omething other t
342bd 68 61 6e 20 61 20 70 68 72 61 73 65 2e 20 46 6f  han a phrase. Fo
342be 72 20 65 78 61 6d 70 6c 65 2c 20 65 69 74 68 65  r example, eithe
342bf 72 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20  r of.        ** 
342c0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20  the following:. 
342c1 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
342c2 20 20 2a 2a 20 20 20 20 28 62 72 61 63 6b 65 74    **    (bracket
342c3 65 64 20 65 78 70 72 65 73 73 69 6f 6e 29 20 4e  ed expression) N
342c4 45 41 52 20 70 68 72 61 73 65 0a 20 20 20 20 20  EAR phrase.     
342c5 20 20 20 2a 2a 20 20 20 20 70 68 72 61 73 65 20     **    phrase 
342c6 4e 45 41 52 20 28 62 72 61 63 6b 65 74 65 64 20  NEAR (bracketed 
342c7 65 78 70 72 65 73 73 69 6f 6e 29 0a 20 20 20 20  expression).    
342c8 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
342c9 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  * Return an erro
342ca 72 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 65  r in either case
342cb 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
342cc 20 20 20 20 20 69 66 28 20 70 50 72 65 76 20 26       if( pPrev &
342cd 26 20 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  & (.            
342ce 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  (eType==FTSQUERY
342cf 5f 4e 45 41 52 20 26 26 20 21 69 73 50 68 72 61  _NEAR && !isPhra
342d0 73 65 20 26 26 20 70 50 72 65 76 2d 3e 65 54 79  se && pPrev->eTy
342d1 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe!=FTSQUERY_PHR
342d2 41 53 45 29 0a 20 20 20 20 20 20 20 20 20 7c 7c  ASE).         ||
342d3 20 28 65 54 79 70 65 21 3d 46 54 53 51 55 45 52   (eType!=FTSQUER
342d4 59 5f 50 48 52 41 53 45 20 26 26 20 69 73 50 68  Y_PHRASE && isPh
342d5 72 61 73 65 20 26 26 20 70 50 72 65 76 2d 3e 65  rase && pPrev->e
342d6 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e  Type==FTSQUERY_N
342d7 45 41 52 29 0a 20 20 20 20 20 20 20 20 29 29 7b  EAR).        )){
342d8 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
342d9 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
342da 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
342db 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
342dc 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
342dd 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20  xprparse_out;.  
342de 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20        }.  .     
342df 20 20 20 69 66 28 20 69 73 50 68 72 61 73 65 20     if( isPhrase 
342e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
342e1 20 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20   pRet ){.       
342e2 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72       assert( pPr
342e3 65 76 20 26 26 20 70 50 72 65 76 2d 3e 70 4c 65  ev && pPrev->pLe
342e4 66 74 20 26 26 20 70 50 72 65 76 2d 3e 70 52 69  ft && pPrev->pRi
342e5 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ght==0 );.      
342e6 20 20 20 20 20 20 70 50 72 65 76 2d 3e 70 52 69        pPrev->pRi
342e7 67 68 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  ght = p;.       
342e8 20 20 20 20 20 70 2d 3e 70 50 61 72 65 6e 74 20       p->pParent 
342e9 3d 20 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20  = pPrev;.       
342ea 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
342eb 20 20 20 20 20 20 70 52 65 74 20 3d 20 70 3b 0a        pRet = p;.
342ec 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
342ed 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
342ee 20 20 20 20 20 69 6e 73 65 72 74 42 69 6e 61 72       insertBinar
342ef 79 4f 70 65 72 61 74 6f 72 28 26 70 52 65 74 2c  yOperator(&pRet,
342f0 20 70 50 72 65 76 2c 20 70 29 3b 0a 20 20 20 20   pPrev, p);.    
342f1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 73      }.        is
342f2 52 65 71 75 69 72 65 50 68 72 61 73 65 20 3d 20  RequirePhrase = 
342f3 21 69 73 50 68 72 61 73 65 3b 0a 20 20 20 20 20  !isPhrase;.     
342f4 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
342f5 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 20 20   nByte>0 );.    
342f6 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  }.    assert( rc
342f7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28  !=SQLITE_OK || (
342f8 6e 42 79 74 65 3e 30 20 26 26 20 6e 42 79 74 65  nByte>0 && nByte
342f9 3c 3d 6e 49 6e 29 20 29 3b 0a 20 20 20 20 6e 49  <=nIn) );.    nI
342fa 6e 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20  n -= nByte;.    
342fb 7a 49 6e 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20  zIn += nByte;.  
342fc 20 20 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d    pPrev = p;.  }
342fd 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
342fe 54 45 5f 44 4f 4e 45 20 26 26 20 70 52 65 74 20  TE_DONE && pRet 
342ff 26 26 20 69 73 52 65 71 75 69 72 65 50 68 72 61  && isRequirePhra
34300 73 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  se ){.    rc = S
34301 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
34302 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
34303 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 72  TE_DONE ){.    r
34304 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
34305 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f     if( !sqlite3_
34306 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65  fts3_enable_pare
34307 6e 74 68 65 73 65 73 20 26 26 20 70 4e 6f 74 42  ntheses && pNotB
34308 72 61 6e 63 68 20 29 7b 0a 20 20 20 20 20 20 69  ranch ){.      i
34309 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20 20  f( !pRet ){.    
3430a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3430b 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 65 6c  ERROR;.      }el
3430c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4e 6f 74  se{.        pNot
3430d 42 72 61 6e 63 68 2d 3e 70 4c 65 66 74 20 3d 20  Branch->pLeft = 
3430e 70 52 65 74 3b 0a 20 20 20 20 20 20 20 20 70 52  pRet;.        pR
3430f 65 74 20 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b  et = pNotBranch;
34310 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
34311 20 7d 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64   }.  *pnConsumed
34312 20 3d 20 6e 20 2d 20 6e 49 6e 3b 0a 0a 65 78 70   = n - nIn;..exp
34313 72 70 61 72 73 65 5f 6f 75 74 3a 0a 20 20 69 66  rparse_out:.  if
34314 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34315 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  ){.    sqlite3Ft
34316 73 33 45 78 70 72 46 72 65 65 28 70 52 65 74 29  s3ExprFree(pRet)
34317 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ;.    sqlite3Fts
34318 33 45 78 70 72 46 72 65 65 28 70 4e 6f 74 42 72  3ExprFree(pNotBr
34319 61 6e 63 68 29 3b 0a 20 20 20 20 70 52 65 74 20  anch);.    pRet 
3431a 3d 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 70 45 78  = 0;.  }.  *ppEx
3431b 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65 74  pr = pRet;.  ret
3431c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
3431d 20 50 61 72 61 6d 65 74 65 72 73 20 7a 20 61 6e   Parameters z an
3431e 64 20 6e 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f  d n contain a po
3431f 69 6e 74 65 72 20 74 6f 20 61 6e 64 20 6c 65 6e  inter to and len
34320 67 74 68 20 6f 66 20 61 20 62 75 66 66 65 72 20  gth of a buffer 
34321 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 6e  containing.** an
34322 20 66 74 73 33 20 71 75 65 72 79 20 65 78 70 72   fts3 query expr
34323 65 73 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69  ession, respecti
34324 76 65 6c 79 2e 20 54 68 69 73 20 66 75 6e 63 74  vely. This funct
34325 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
34326 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 71 75 65  parse the.** que
34327 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  ry expression an
34328 64 20 63 72 65 61 74 65 20 61 20 74 72 65 65 20  d create a tree 
34329 6f 66 20 46 74 73 33 45 78 70 72 20 73 74 72 75  of Fts3Expr stru
3432a 63 74 75 72 65 73 20 72 65 70 72 65 73 65 6e 74  ctures represent
3432b 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 73 65  ing the.** parse
3432c 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 66  d expression. If
3432d 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 70   successful, *pp
3432e 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 70  Expr is set to p
3432f 6f 69 6e 74 20 74 6f 20 74 68 65 20 68 65 61 64  oint to the head
34330 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 73 65  .** of the parse
34331 64 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  d expression tre
34332 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  e and SQLITE_OK 
34333 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
34334 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75  an error.** occu
34335 72 73 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  rs, either SQLIT
34336 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 2d 6f 66 2d  E_NOMEM (out-of-
34337 6d 65 6d 6f 72 79 20 65 72 72 6f 72 29 20 6f 72  memory error) or
34338 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 70   SQLITE_ERROR (p
34339 61 72 73 65 0a 2a 2a 20 65 72 72 6f 72 29 20 69  arse.** error) i
3433a 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
3433b 70 70 45 78 70 72 20 69 73 20 73 65 74 20 74 6f  ppExpr is set to
3433c 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72   0..**.** If par
3433d 61 6d 65 74 65 72 20 6e 20 69 73 20 61 20 6e 65  ameter n is a ne
3433e 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74  gative number, t
3433f 68 65 6e 20 7a 20 69 73 20 61 73 73 75 6d 65 64  hen z is assumed
34340 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 0a 2a   to point to a.*
34341 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  * nul-terminated
34342 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20   string and the 
34343 6c 65 6e 67 74 68 20 69 73 20 64 65 74 65 72 6d  length is determ
34344 69 6e 65 64 20 75 73 69 6e 67 20 73 74 72 6c 65  ined using strle
34345 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  n()..**.** The f
34346 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20  irst parameter, 
34347 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 69 73 20 70  pTokenizer, is p
34348 61 73 73 65 64 20 74 68 65 20 66 74 73 33 20 74  assed the fts3 t
34349 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20  okenizer module 
3434a 74 6f 0a 2a 2a 20 75 73 65 20 74 6f 20 6e 6f 72  to.** use to nor
3434b 6d 61 6c 69 7a 65 20 71 75 65 72 79 20 74 6f 6b  malize query tok
3434c 65 6e 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e  ens while parsin
3434d 67 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  g the expression
3434e 2e 20 54 68 65 20 61 7a 43 6f 6c 5b 5d 0a 2a 2a  . The azCol[].**
3434f 20 61 72 72 61 79 2c 20 77 68 69 63 68 20 69 73   array, which is
34350 20 61 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74   assumed to cont
34351 61 69 6e 20 6e 43 6f 6c 20 65 6e 74 72 69 65 73  ain nCol entries
34352 2c 20 73 68 6f 75 6c 64 20 63 6f 6e 74 61 69 6e  , should contain
34353 20 74 68 65 20 6e 61 6d 65 73 0a 2a 2a 20 6f 66   the names.** of
34354 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 20   each column in 
34355 74 68 65 20 74 61 72 67 65 74 20 66 74 73 33 20  the target fts3 
34356 74 61 62 6c 65 2c 20 69 6e 20 6f 72 64 65 72 20  table, in order 
34357 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67  from left to rig
34358 68 74 2e 20 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 6e  ht. .** Column n
34359 61 6d 65 73 20 6d 75 73 74 20 62 65 20 6e 75 6c  ames must be nul
3435a 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
3435b 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ngs..**.** The i
3435c 44 65 66 61 75 6c 74 43 6f 6c 20 70 61 72 61 6d  DefaultCol param
3435d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 70  eter should be p
3435e 61 73 73 65 64 20 74 68 65 20 69 6e 64 65 78 20  assed the index 
3435f 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
34360 75 6d 6e 0a 2a 2a 20 74 68 61 74 20 61 70 70 65  umn.** that appe
34361 61 72 73 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d  ars on the left-
34362 68 61 6e 64 2d 73 69 64 65 20 6f 66 20 74 68 65  hand-side of the
34363 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20   MATCH operator 
34364 28 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20  (the default.** 
34365 63 6f 6c 75 6d 6e 20 74 6f 20 6d 61 74 63 68 20  column to match 
34366 61 67 61 69 6e 73 74 20 66 6f 72 20 74 6f 6b 65  against for toke
34367 6e 73 20 66 6f 72 20 77 68 69 63 68 20 61 20 63  ns for which a c
34368 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 73 20 6e 6f  olumn name is no
34369 74 20 65 78 70 6c 69 63 69 74 6c 79 0a 2a 2a 20  t explicitly.** 
3436a 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72  specified as par
3436b 74 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 73  t of the query s
3436c 74 72 69 6e 67 29 2c 20 6f 72 20 2d 31 20 69 66  tring), or -1 if
3436d 20 74 6f 6b 65 6e 73 20 6d 61 79 20 62 79 20 64   tokens may by d
3436e 65 66 61 75 6c 74 0a 2a 2a 20 6d 61 74 63 68 20  efault.** match 
3436f 61 6e 79 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  any table column
34370 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
34371 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
34372 74 73 33 45 78 70 72 50 61 72 73 65 28 0a 20 20  ts3ExprParse(.  
34373 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34374 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20  r *pTokenizer,  
34375 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
34376 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61   module */.  cha
34377 72 20 2a 2a 61 7a 43 6f 6c 2c 20 20 20 20 20 20  r **azCol,      
34378 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34379 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c   /* Array of col
3437a 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 66 74  umn names for ft
3437b 73 33 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  s3 table */.  in
3437c 74 20 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  t nCol,         
3437d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3437e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
3437f 6e 74 72 69 65 73 20 69 6e 20 61 7a 43 6f 6c 5b  ntries in azCol[
34380 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65 66 61  ] */.  int iDefa
34381 75 6c 74 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  ultCol,         
34382 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
34383 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20 74 6f 20  fault column to 
34384 71 75 65 72 79 20 2a 2f 0a 20 20 63 6f 6e 73 74  query */.  const
34385 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
34386 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34387 2a 20 54 65 78 74 20 6f 66 20 4d 41 54 43 48 20  * Text of MATCH 
34388 71 75 65 72 79 20 2a 2f 0a 20 20 46 74 73 33 45  query */.  Fts3E
34389 78 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20  xpr **ppExpr    
3438a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3438b 2a 20 4f 55 54 3a 20 50 61 72 73 65 64 20 71 75  * OUT: Parsed qu
3438c 65 72 79 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ery structure */
3438d 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 72 73 65  .){.  int nParse
3438e 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50  d;.  int rc;.  P
3438f 61 72 73 65 43 6f 6e 74 65 78 74 20 73 50 61 72  arseContext sPar
34390 73 65 3b 0a 20 20 73 50 61 72 73 65 2e 70 54 6f  se;.  sParse.pTo
34391 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
34392 69 7a 65 72 3b 0a 20 20 73 50 61 72 73 65 2e 61  izer;.  sParse.a
34393 7a 43 6f 6c 20 3d 20 28 63 6f 6e 73 74 20 63 68  zCol = (const ch
34394 61 72 20 2a 2a 29 61 7a 43 6f 6c 3b 0a 20 20 73  ar **)azCol;.  s
34395 50 61 72 73 65 2e 6e 43 6f 6c 20 3d 20 6e 43 6f  Parse.nCol = nCo
34396 6c 3b 0a 20 20 73 50 61 72 73 65 2e 69 44 65 66  l;.  sParse.iDef
34397 61 75 6c 74 43 6f 6c 20 3d 20 69 44 65 66 61 75  aultCol = iDefau
34398 6c 74 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65 2e  ltCol;.  sParse.
34399 6e 4e 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28  nNest = 0;.  if(
3439a 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 70   z==0 ){.    *pp
3439b 45 78 70 72 20 3d 20 30 3b 0a 20 20 20 20 72 65  Expr = 0;.    re
3439c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3439d 20 20 7d 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b    }.  if( n<0 ){
3439e 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28  .    n = strlen(
3439f 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66  z);.  }.  rc = f
343a0 74 73 33 45 78 70 72 50 61 72 73 65 28 26 73 50  ts3ExprParse(&sP
343a1 61 72 73 65 2c 20 7a 2c 20 6e 2c 20 70 70 45 78  arse, z, n, ppEx
343a2 70 72 2c 20 26 6e 50 61 72 73 65 64 29 3b 0a 0a  pr, &nParsed);..
343a3 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6d    /* Check for m
343a4 69 73 6d 61 74 63 68 65 64 20 70 61 72 65 6e 74  ismatched parent
343a5 68 65 73 69 73 20 2a 2f 0a 20 20 69 66 28 20 72  hesis */.  if( r
343a6 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
343a7 73 50 61 72 73 65 2e 6e 4e 65 73 74 20 29 7b 0a  sParse.nNest ){.
343a8 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
343a9 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74  ERROR;.    sqlit
343aa 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 2a  e3Fts3ExprFree(*
343ab 70 70 45 78 70 72 29 3b 0a 20 20 20 20 2a 70 70  ppExpr);.    *pp
343ac 45 78 70 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  Expr = 0;.  }.. 
343ad 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
343ae 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 72 73  *.** Free a pars
343af 65 64 20 66 74 73 33 20 71 75 65 72 79 20 65 78  ed fts3 query ex
343b0 70 72 65 73 73 69 6f 6e 20 61 6c 6c 6f 63 61 74  pression allocat
343b1 65 64 20 62 79 20 73 71 6c 69 74 65 33 46 74 73  ed by sqlite3Fts
343b2 33 45 78 70 72 50 61 72 73 65 28 29 2e 0a 2a 2f  3ExprParse()..*/
343b3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
343b4 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
343b5 45 78 70 72 46 72 65 65 28 46 74 73 33 45 78 70  ExprFree(Fts3Exp
343b6 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  r *p){.  if( p )
343b7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
343b8 33 45 78 70 72 46 72 65 65 28 70 2d 3e 70 4c 65  3ExprFree(p->pLe
343b9 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ft);.    sqlite3
343ba 46 74 73 33 45 78 70 72 46 72 65 65 28 70 2d 3e  Fts3ExprFree(p->
343bb 70 52 69 67 68 74 29 3b 0a 20 20 20 20 73 71 6c  pRight);.    sql
343bc 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
343bd 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.}../**********
343be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
343bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
343c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
343c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
343c2 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
343c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
343c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
343c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
343c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
343c7 0a 2a 2a 20 45 76 65 72 79 74 68 69 6e 67 20 61  .** Everything a
343c8 66 74 65 72 20 74 68 69 73 20 70 6f 69 6e 74 20  fter this point 
343c9 69 73 20 6a 75 73 74 20 74 65 73 74 20 63 6f 64  is just test cod
343ca 65 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  e..*/..#ifdef SQ
343cb 4c 49 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a  LITE_TEST.../*.*
343cc 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 71 75  * Function to qu
343cd 65 72 79 20 74 68 65 20 68 61 73 68 2d 74 61 62  ery the hash-tab
343ce 6c 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 73  le of tokenizers
343cf 20 28 73 65 65 20 52 45 41 44 4d 45 2e 74 6f 6b   (see README.tok
343d0 65 6e 69 7a 65 72 73 29 2e 0a 2a 2f 0a 73 74 61  enizers)..*/.sta
343d1 74 69 63 20 69 6e 74 20 71 75 65 72 79 54 65 73  tic int queryTes
343d2 74 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71  tTokenizer(.  sq
343d3 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 6f  lite3 *db, .  co
343d4 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
343d5 20 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74    .  const sqlit
343d6 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
343d7 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e  ule **pp.){.  in
343d8 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
343d9 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63  stmt *pStmt;.  c
343da 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d  onst char zSql[]
343db 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f   = "SELECT fts3_
343dc 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a  tokenizer(?)";..
343dd 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72 63 20    *pp = 0;.  rc 
343de 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
343df 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
343e0 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
343e1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
343e2 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
343e3 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69   rc;.  }..  sqli
343e4 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53  te3_bind_text(pS
343e5 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d  tmt, 1, zName, -
343e6 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
343e7 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
343e8 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
343e9 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  p(pStmt) ){.    
343ea 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  if( sqlite3_colu
343eb 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30  mn_type(pStmt, 0
343ec 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  )==SQLITE_BLOB )
343ed 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  {.      memcpy(p
343ee 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  p, sqlite3_colum
343ef 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29  n_blob(pStmt, 0)
343f0 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a  , sizeof(*pp));.
343f1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
343f2 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
343f3 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
343f4 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
343f5 69 6f 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74  ion is part of t
343f6 68 65 20 74 65 73 74 20 69 6e 74 65 72 66 61 63  he test interfac
343f7 65 20 66 6f 72 20 74 68 65 20 71 75 65 72 79 20  e for the query 
343f8 70 61 72 73 65 72 2e 20 49 74 0a 2a 2a 20 77 72  parser. It.** wr
343f9 69 74 65 73 20 61 20 74 65 78 74 20 72 65 70 72  ites a text repr
343fa 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
343fb 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  e query expressi
343fc 6f 6e 20 70 45 78 70 72 20 69 6e 74 6f 20 74 68  on pExpr into th
343fd 65 0a 2a 2a 20 62 75 66 66 65 72 20 70 6f 69 6e  e.** buffer poin
343fe 74 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65  ted to by argume
343ff 6e 74 20 7a 42 75 66 2e 20 49 74 20 69 73 20 61  nt zBuf. It is a
34400 73 73 75 6d 65 64 20 74 68 61 74 20 7a 42 75 66  ssumed that zBuf
34401 20 69 73 20 6c 61 72 67 65 20 0a 2a 2a 20 65 6e   is large .** en
34402 6f 75 67 68 20 74 6f 20 73 74 6f 72 65 20 74 68  ough to store th
34403 65 20 72 65 71 75 69 72 65 64 20 74 65 78 74 20  e required text 
34404 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a  representation..
34405 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65  */.static void e
34406 78 70 72 54 6f 53 74 72 69 6e 67 28 46 74 73 33  xprToString(Fts3
34407 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
34408 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 77 69 74  r *zBuf){.  swit
34409 63 68 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65  ch( pExpr->eType
3440a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 46 54 53   ){.    case FTS
3440b 51 55 45 52 59 5f 50 48 52 41 53 45 3a 20 7b 0a  QUERY_PHRASE: {.
3440c 20 20 20 20 20 20 46 74 73 33 50 68 72 61 73 65        Fts3Phrase
3440d 20 2a 70 50 68 72 61 73 65 20 3d 20 70 45 78 70   *pPhrase = pExp
3440e 72 2d 3e 70 50 68 72 61 73 65 3b 0a 20 20 20 20  r->pPhrase;.    
3440f 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 7a    int i;.      z
34410 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a  Buf += sprintf(z
34411 42 75 66 2c 20 22 50 48 52 41 53 45 20 25 64 20  Buf, "PHRASE %d 
34412 25 64 22 2c 20 70 50 68 72 61 73 65 2d 3e 69 43  %d", pPhrase->iC
34413 6f 6c 75 6d 6e 2c 20 70 50 68 72 61 73 65 2d 3e  olumn, pPhrase->
34414 69 73 4e 6f 74 29 3b 0a 20 20 20 20 20 20 66 6f  isNot);.      fo
34415 72 28 69 3d 30 3b 20 69 3c 70 50 68 72 61 73 65  r(i=0; i<pPhrase
34416 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a  ->nToken; i++){.
34417 20 20 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20          zBuf += 
34418 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 22 20 25  sprintf(zBuf," %
34419 2e 2a 73 22 2c 70 50 68 72 61 73 65 2d 3e 61 54  .*s",pPhrase->aT
3441a 6f 6b 65 6e 5b 69 5d 2e 6e 2c 70 50 68 72 61 73  oken[i].n,pPhras
3441b 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 29 3b  e->aToken[i].z);
3441c 0a 20 20 20 20 20 20 20 20 7a 42 75 66 20 2b 3d  .        zBuf +=
3441d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 22 25   sprintf(zBuf,"%
3441e 73 22 2c 20 28 70 50 68 72 61 73 65 2d 3e 61 54  s", (pPhrase->aT
3441f 6f 6b 65 6e 5b 69 5d 2e 69 73 50 72 65 66 69 78  oken[i].isPrefix
34420 3f 22 2b 22 3a 22 22 29 29 3b 0a 20 20 20 20 20  ?"+":""));.     
34421 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b   }.      return;
34422 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65  .    }..    case
34423 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52 3a 0a   FTSQUERY_NEAR:.
34424 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70        zBuf += sp
34425 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4e 45 41  rintf(zBuf, "NEA
34426 52 2f 25 64 20 22 2c 20 70 45 78 70 72 2d 3e 6e  R/%d ", pExpr->n
34427 4e 65 61 72 29 3b 0a 20 20 20 20 20 20 62 72 65  Near);.      bre
34428 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46 54 53  ak;.    case FTS
34429 51 55 45 52 59 5f 4e 4f 54 3a 0a 20 20 20 20 20  QUERY_NOT:.     
3442a 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
3442b 28 7a 42 75 66 2c 20 22 4e 4f 54 20 22 29 3b 0a  (zBuf, "NOT ");.
3442c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3442d 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 41   case FTSQUERY_A
3442e 4e 44 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b  ND:.      zBuf +
3442f 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20  = sprintf(zBuf, 
34430 22 41 4e 44 20 22 29 3b 0a 20 20 20 20 20 20 62  "AND ");.      b
34431 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46  reak;.    case F
34432 54 53 51 55 45 52 59 5f 4f 52 3a 0a 20 20 20 20  TSQUERY_OR:.    
34433 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
34434 66 28 7a 42 75 66 2c 20 22 4f 52 20 22 29 3b 0a  f(zBuf, "OR ");.
34435 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d        break;.  }
34436 0a 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69  ..  zBuf += spri
34437 6e 74 66 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a  ntf(zBuf, "{");.
34438 20 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70    exprToString(p
34439 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 7a 42 75  Expr->pLeft, zBu
3443a 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74  f);.  zBuf += st
3443b 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42  rlen(zBuf);.  zB
3443c 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
3443d 75 66 2c 20 22 7d 20 22 29 3b 0a 0a 20 20 7a 42  uf, "} ");..  zB
3443e 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
3443f 75 66 2c 20 22 7b 22 29 3b 0a 20 20 65 78 70 72  uf, "{");.  expr
34440 54 6f 53 74 72 69 6e 67 28 70 45 78 70 72 2d 3e  ToString(pExpr->
34441 70 52 69 67 68 74 2c 20 7a 42 75 66 29 3b 0a 20  pRight, zBuf);. 
34442 20 7a 42 75 66 20 2b 3d 20 73 74 72 6c 65 6e 28   zBuf += strlen(
34443 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d  zBuf);.  zBuf +=
34444 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
34445 7d 22 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  }");.}../*.** Th
34446 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  is is the implem
34447 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 63  entation of a sc
34448 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f  alar SQL functio
34449 6e 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 74  n used to test t
3444a 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  he .** expressio
3444b 6e 20 70 61 72 73 65 72 2e 20 49 74 20 73 68 6f  n parser. It sho
3444c 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 61 73  uld be called as
3444d 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
3444e 20 20 66 74 73 33 5f 65 78 70 72 74 65 73 74 28    fts3_exprtest(
3444f 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c 20 3c 65 78  <tokenizer>, <ex
34450 70 72 3e 2c 20 3c 63 6f 6c 75 6d 6e 20 31 3e 2c  pr>, <column 1>,
34451 20 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65   ...);.**.** The
34452 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c   first argument,
34453 20 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c 20 69 73   <tokenizer>, is
34454 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
34455 20 66 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20   fts3 tokenizer 
34456 75 73 65 64 0a 2a 2a 20 74 6f 20 70 61 72 73 65  used.** to parse
34457 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
34458 73 73 69 6f 6e 20 28 73 65 65 20 52 45 41 44 4d  ssion (see READM
34459 45 2e 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 20 54  E.tokenizers). T
3445a 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
3445b 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 71 75 65  nt.** is the que
3445c 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  ry expression to
3445d 20 70 61 72 73 65 2e 20 45 61 63 68 20 73 75 62   parse. Each sub
3445e 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74  sequent argument
3445f 20 69 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20   is the name.** 
34460 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  of a column of t
34461 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 74 68  he fts3 table th
34462 61 74 20 74 68 65 20 71 75 65 72 79 20 65 78 70  at the query exp
34463 72 65 73 73 69 6f 6e 20 6d 61 79 20 72 65 66 65  ression may refe
34464 72 20 74 6f 2e 0a 2a 2a 20 46 6f 72 20 65 78 61  r to..** For exa
34465 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  mple:.**.**   SE
34466 4c 45 43 54 20 66 74 73 33 5f 65 78 70 72 74 65  LECT fts3_exprte
34467 73 74 28 27 73 69 6d 70 6c 65 27 2c 20 27 42 69  st('simple', 'Bi
34468 6c 6c 20 63 6f 6c 32 3a 42 6c 6f 67 67 73 27 2c  ll col2:Bloggs',
34469 20 27 63 6f 6c 31 27 2c 20 27 63 6f 6c 32 27 29   'col1', 'col2')
3446a 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.*/.static void
3446b 20 66 74 73 33 45 78 70 72 54 65 73 74 28 0a 20   fts3ExprTest(. 
3446c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
3446d 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
3446e 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
3446f 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
34470 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
34471 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
34472 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 30 3b 0a  t *pModule = 0;.
34473 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
34474 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20  zer *pTokenizer 
34475 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  = 0;.  int rc;. 
34476 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 20 3d 20   char **azCol = 
34477 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  0;.  const char 
34478 2a 7a 45 78 70 72 3b 0a 20 20 69 6e 74 20 6e 45  *zExpr;.  int nE
34479 78 70 72 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  xpr;.  int nCol;
3447a 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 46 74 73  .  int ii;.  Fts
3447b 33 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20  3Expr *pExpr;.  
3447c 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
3447d 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
3447e 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
3447f 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3c 33 20  ;..  if( argc<3 
34480 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
34481 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
34482 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 22 55  ext, .        "U
34483 73 61 67 65 3a 20 66 74 73 33 5f 65 78 70 72 74  sage: fts3_exprt
34484 65 73 74 28 74 6f 6b 65 6e 69 7a 65 72 2c 20 65  est(tokenizer, e
34485 78 70 72 2c 20 63 6f 6c 31 2c 20 2e 2e 2e 22 2c  xpr, col1, ...",
34486 20 2d 31 0a 20 20 20 20 29 3b 0a 20 20 20 20 72   -1.    );.    r
34487 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 72 63  eturn;.  }..  rc
34488 20 3d 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65   = queryTestToke
34489 6e 69 7a 65 72 28 64 62 2c 0a 20 20 20 20 20 20  nizer(db,.      
3448a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3448b 20 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20      (const char 
3448c 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
3448d 74 65 78 74 28 61 72 67 76 5b 30 5d 29 2c 20 26  text(argv[0]), &
3448e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20  pModule);.  if( 
3448f 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
34490 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
34491 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
34492 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
34493 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
34494 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ut;.  }else if( 
34495 21 70 4d 6f 64 75 6c 65 20 29 7b 0a 20 20 20 20  !pModule ){.    
34496 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
34497 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4e  rror(context, "N
34498 6f 20 73 75 63 68 20 74 6f 6b 65 6e 69 7a 65 72  o such tokenizer
34499 20 6d 6f 64 75 6c 65 22 2c 20 2d 31 29 3b 0a 20   module", -1);. 
3449a 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74     goto exprtest
3449b 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  _out;.  }..  rc 
3449c 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61  = pModule->xCrea
3449d 74 65 28 30 2c 20 30 2c 20 26 70 54 6f 6b 65 6e  te(0, 0, &pToken
3449e 69 7a 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28  izer);.  assert(
3449f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
344a0 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  M || rc==SQLITE_
344a1 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  OK );.  if( rc==
344a2 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
344a3 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
344a4 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63  lt_error_nomem(c
344a5 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74  ontext);.    got
344a6 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a  o exprtest_out;.
344a7 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72    }.  pTokenizer
344a8 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64  ->pModule = pMod
344a9 75 6c 65 3b 0a 0a 20 20 7a 45 78 70 72 20 3d 20  ule;..  zExpr = 
344aa 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
344ab 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
344ac 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 6e 45 78  (argv[1]);.  nEx
344ad 70 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pr = sqlite3_val
344ae 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d  ue_bytes(argv[1]
344af 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 61 72 67 63  );.  nCol = argc
344b0 2d 32 3b 0a 20 20 61 7a 43 6f 6c 20 3d 20 28 63  -2;.  azCol = (c
344b1 68 61 72 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d  har **)sqlite3_m
344b2 61 6c 6c 6f 63 28 6e 43 6f 6c 2a 73 69 7a 65 6f  alloc(nCol*sizeo
344b3 66 28 63 68 61 72 20 2a 29 29 3b 0a 20 20 69 66  f(char *));.  if
344b4 28 20 21 61 7a 43 6f 6c 20 29 7b 0a 20 20 20 20  ( !azCol ){.    
344b5 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
344b6 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
344b7 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  xt);.    goto ex
344b8 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a  prtest_out;.  }.
344b9 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
344ba 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  Col; ii++){.    
344bb 61 7a 43 6f 6c 5b 69 69 5d 20 3d 20 28 63 68 61  azCol[ii] = (cha
344bc 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
344bd 65 5f 74 65 78 74 28 61 72 67 76 5b 69 69 2b 32  e_text(argv[ii+2
344be 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  ]);.  }..  rc = 
344bf 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50  sqlite3Fts3ExprP
344c0 61 72 73 65 28 0a 20 20 20 20 20 20 70 54 6f 6b  arse(.      pTok
344c1 65 6e 69 7a 65 72 2c 20 61 7a 43 6f 6c 2c 20 6e  enizer, azCol, n
344c2 43 6f 6c 2c 20 6e 43 6f 6c 2c 20 7a 45 78 70 72  Col, nCol, zExpr
344c3 2c 20 6e 45 78 70 72 2c 20 26 70 45 78 70 72 0a  , nExpr, &pExpr.
344c4 20 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53    );.  if( rc==S
344c5 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
344c6 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
344c7 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
344c8 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f  ntext);.    goto
344c9 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20   exprtest_out;. 
344ca 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
344cb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
344cc 63 68 61 72 20 7a 42 75 66 5b 34 30 39 36 5d 3b  char zBuf[4096];
344cd 0a 20 20 20 20 65 78 70 72 54 6f 53 74 72 69 6e  .    exprToStrin
344ce 67 28 70 45 78 70 72 2c 20 7a 42 75 66 29 3b 0a  g(pExpr, zBuf);.
344cf 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
344d0 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
344d1 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54   zBuf, -1, SQLIT
344d2 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
344d3 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
344d4 72 46 72 65 65 28 70 45 78 70 72 29 3b 0a 20 20  rFree(pExpr);.  
344d5 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
344d6 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
344d7 63 6f 6e 74 65 78 74 2c 20 22 45 72 72 6f 72 20  context, "Error 
344d8 70 61 72 73 69 6e 67 20 65 78 70 72 65 73 73 69  parsing expressi
344d9 6f 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 0a 65  on", -1);.  }..e
344da 78 70 72 74 65 73 74 5f 6f 75 74 3a 0a 20 20 69  xprtest_out:.  i
344db 66 28 20 70 4d 6f 64 75 6c 65 20 26 26 20 70 54  f( pModule && pT
344dc 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a 20 20 20 20  okenizer ){.    
344dd 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 44  rc = pModule->xD
344de 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65  estroy(pTokenize
344df 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  r);.  }.  sqlite
344e0 33 5f 66 72 65 65 28 61 7a 43 6f 6c 29 3b 0a 7d  3_free(azCol);.}
344e1 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
344e2 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
344e3 73 73 69 6f 6e 20 70 61 72 73 65 72 20 74 65 73  ssion parser tes
344e4 74 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f  t function fts3_
344e5 65 78 70 72 74 65 73 74 28 29 20 0a 2a 2a 20 77  exprtest() .** w
344e6 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ith database con
344e7 6e 65 63 74 69 6f 6e 20 64 62 2e 20 0a 2a 2f 0a  nection db. .*/.
344e8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
344e9 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 45  oid sqlite3Fts3E
344ea 78 70 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72  xprInitTestInter
344eb 66 61 63 65 28 73 71 6c 69 74 65 33 2a 20 64 62  face(sqlite3* db
344ec 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  ){.  sqlite3_cre
344ed 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  ate_function(.  
344ee 20 20 20 20 64 62 2c 20 22 66 74 73 33 5f 65 78      db, "fts3_ex
344ef 70 72 74 65 73 74 22 2c 20 2d 31 2c 20 53 51 4c  prtest", -1, SQL
344f0 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 66 74 73  ITE_UTF8, 0, fts
344f1 33 45 78 70 72 54 65 73 74 2c 20 30 2c 20 30 0a  3ExprTest, 0, 0.
344f2 20 20 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 23    );.}..#endif.#
344f3 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
344f4 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
344f5 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
344f6 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
344f7 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
344f8 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 65 78  * End of fts3_ex
344f9 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pr.c ***********
344fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
344fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
344fc 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
344fd 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
344fe 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a  3_hash.c *******
344ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34501 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
34502 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20  tember 22.**.** 
34503 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
34504 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
34505 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
34506 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
34507 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
34508 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
34509 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
3450a 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
3450b 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
3450c 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
3450d 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
3450e 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
3450f 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
34510 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
34511 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
34512 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
34513 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
34514 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34515 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34516 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34517 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34518 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
34519 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   is the implemen
3451a 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69  tation of generi
3451b 63 20 68 61 73 68 2d 74 61 62 6c 65 73 20 75 73  c hash-tables us
3451c 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a  ed in SQLite..**
3451d 20 57 65 27 76 65 20 6d 6f 64 69 66 69 65 64 20   We've modified 
3451e 69 74 20 73 6c 69 67 68 74 6c 79 20 74 6f 20 73  it slightly to s
3451f 65 72 76 65 20 61 73 20 61 20 73 74 61 6e 64 61  erve as a standa
34520 6c 6f 6e 65 20 68 61 73 68 20 74 61 62 6c 65 0a  lone hash table.
34521 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
34522 6e 20 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74  n for the full-t
34523 65 78 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64  ext indexing mod
34524 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ule..*/../*.** T
34525 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
34526 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d  file is only com
34527 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20  piled if:.**.** 
34528 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
34529 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
3452a 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e  uilt as an exten
3452b 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69  sion.**       (i
3452c 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
3452d 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20  ITE_CORE is not 
3452e 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a  defined), or.**.
3452f 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
34530 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
34531 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65  g built into the
34532 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20   core of.**     
34533 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69    SQLite (in whi
34534 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45  ch case SQLITE_E
34535 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65  NABLE_FTS3 is de
34536 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21  fined)..*/.#if !
34537 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
34538 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
34539 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
3453a 53 33 29 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c  S3)..../*.** Mal
3453b 6c 6f 63 20 61 6e 64 20 46 72 65 65 20 66 75 6e  loc and Free fun
3453c 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63  ctions.*/.static
3453d 20 76 6f 69 64 20 2a 66 74 73 33 48 61 73 68 4d   void *fts3HashM
3453e 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20  alloc(int n){.  
3453f 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65  void *p = sqlite
34540 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69  3_malloc(n);.  i
34541 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73  f( p ){.    mems
34542 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d  et(p, 0, n);.  }
34543 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 73  .  return p;.}.s
34544 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 48  tatic void fts3H
34545 61 73 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29  ashFree(void *p)
34546 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  {.  sqlite3_free
34547 28 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 75 72 6e 20  (p);.}../* Turn 
34548 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f  bulk memory into
34549 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62   a hash table ob
3454a 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69  ject by initiali
3454b 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c  zing the.** fiel
3454c 64 73 20 6f 66 20 74 68 65 20 48 61 73 68 20 73  ds of the Hash s
3454d 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
3454e 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e  "pNew" is a poin
3454f 74 65 72 20 74 6f 20 74 68 65 20 68 61 73 68 20  ter to the hash 
34550 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f  table that is to
34551 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
34552 0a 2a 2a 20 6b 65 79 43 6c 61 73 73 20 69 73 20  .** keyClass is 
34553 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e 73 74  one of the const
34554 61 6e 74 73 20 0a 2a 2a 20 46 54 53 33 5f 48 41  ants .** FTS3_HA
34555 53 48 5f 42 49 4e 41 52 59 20 6f 72 20 46 54 53  SH_BINARY or FTS
34556 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2e 20 20  3_HASH_STRING.  
34557 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6b 65 79  The value of key
34558 43 6c 61 73 73 20 0a 2a 2a 20 64 65 74 65 72 6d  Class .** determ
34559 69 6e 65 73 20 77 68 61 74 20 6b 69 6e 64 20 6f  ines what kind o
3455a 66 20 6b 65 79 20 74 68 65 20 68 61 73 68 20 74  f key the hash t
3455b 61 62 6c 65 20 77 69 6c 6c 20 75 73 65 2e 20 20  able will use.  
3455c 22 63 6f 70 79 4b 65 79 22 20 69 73 0a 2a 2a 20  "copyKey" is.** 
3455d 74 72 75 65 20 69 66 20 74 68 65 20 68 61 73 68  true if the hash
3455e 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 6d 61   table should ma
3455f 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61  ke its own priva
34560 74 65 20 63 6f 70 79 20 6f 66 20 6b 65 79 73 20  te copy of keys 
34561 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20  and.** false if 
34562 69 74 20 73 68 6f 75 6c 64 20 6a 75 73 74 20 75  it should just u
34563 73 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  se the supplied 
34564 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
34565 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
34566 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
34567 6e 69 74 28 66 74 73 33 48 61 73 68 20 2a 70 4e  nit(fts3Hash *pN
34568 65 77 2c 20 69 6e 74 20 6b 65 79 43 6c 61 73 73  ew, int keyClass
34569 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a  , int copyKey){.
3456a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d    assert( pNew!=
3456b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b  0 );.  assert( k
3456c 65 79 43 6c 61 73 73 3e 3d 46 54 53 33 5f 48 41  eyClass>=FTS3_HA
3456d 53 48 5f 53 54 52 49 4e 47 20 26 26 20 6b 65 79  SH_STRING && key
3456e 43 6c 61 73 73 3c 3d 46 54 53 33 5f 48 41 53 48  Class<=FTS3_HASH
3456f 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20 70 4e 65  _BINARY );.  pNe
34570 77 2d 3e 6b 65 79 43 6c 61 73 73 20 3d 20 6b 65  w->keyClass = ke
34571 79 43 6c 61 73 73 3b 0a 20 20 70 4e 65 77 2d 3e  yClass;.  pNew->
34572 63 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b 65  copyKey = copyKe
34573 79 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74  y;.  pNew->first
34574 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f   = 0;.  pNew->co
34575 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  unt = 0;.  pNew-
34576 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70  >htsize = 0;.  p
34577 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a  New->ht = 0;.}..
34578 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e  /* Remove all en
34579 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73  tries from a has
3457a 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69  h table.  Reclai
3457b 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  m all memory..**
3457c 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   Call this routi
3457d 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68  ne to delete a h
3457e 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20  ash table or to 
3457f 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62  reset a hash tab
34580 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70  le.** to the emp
34581 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c  ty state..*/.SQL
34582 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
34583 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
34584 43 6c 65 61 72 28 66 74 73 33 48 61 73 68 20 2a  Clear(fts3Hash *
34585 70 48 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45  pH){.  fts3HashE
34586 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20  lem *elem;      
34587 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
34588 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65  g over all eleme
34589 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  nts of the table
3458a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
3458b 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d  H!=0 );.  elem =
3458c 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48   pH->first;.  pH
3458d 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 66  ->first = 0;.  f
3458e 74 73 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e  ts3HashFree(pH->
3458f 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20  ht);.  pH->ht = 
34590 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20  0;.  pH->htsize 
34591 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 6c  = 0;.  while( el
34592 65 6d 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61  em ){.    fts3Ha
34593 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65  shElem *next_ele
34594 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
34595 20 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79      if( pH->copy
34596 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65  Key && elem->pKe
34597 79 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 48  y ){.      fts3H
34598 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b  ashFree(elem->pK
34599 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  ey);.    }.    f
3459a 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d  ts3HashFree(elem
3459b 29 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65  );.    elem = ne
3459c 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70  xt_elem;.  }.  p
3459d 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a  H->count = 0;.}.
3459e 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20  ./*.** Hash and 
3459f 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
345a0 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f  ions when the mo
345a1 64 65 20 69 73 20 46 54 53 33 5f 48 41 53 48 5f  de is FTS3_HASH_
345a2 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 63  STRING.*/.static
345a3 20 69 6e 74 20 66 74 73 33 53 74 72 48 61 73 68   int fts3StrHash
345a4 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65  (const void *pKe
345a5 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20  y, int nKey){.  
345a6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
345a7 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b  (const char *)pK
345a8 65 79 3b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b  ey;.  int h = 0;
345a9 0a 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29  .  if( nKey<=0 )
345aa 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 20 73 74   nKey = (int) st
345ab 72 6c 65 6e 28 7a 29 3b 0a 20 20 77 68 69 6c 65  rlen(z);.  while
345ac 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20  ( nKey > 0  ){. 
345ad 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20     h = (h<<3) ^ 
345ae 68 20 5e 20 2a 7a 2b 2b 3b 0a 20 20 20 20 6e 4b  h ^ *z++;.    nK
345af 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ey--;.  }.  retu
345b0 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66  rn h & 0x7ffffff
345b1 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  f;.}.static int 
345b2 66 74 73 33 53 74 72 43 6f 6d 70 61 72 65 28 63  fts3StrCompare(c
345b3 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
345b4 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20  , int n1, const 
345b5 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74  void *pKey2, int
345b6 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d   n2){.  if( n1!=
345b7 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  n2 ) return 1;. 
345b8 20 72 65 74 75 72 6e 20 73 74 72 6e 63 6d 70 28   return strncmp(
345b9 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65  (const char*)pKe
345ba 79 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  y1,(const char*)
345bb 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a  pKey2,n1);.}../*
345bc 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d  .** Hash and com
345bd 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
345be 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20  s when the mode 
345bf 69 73 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e  is FTS3_HASH_BIN
345c0 41 52 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ARY.*/.static in
345c1 74 20 66 74 73 33 42 69 6e 48 61 73 68 28 63 6f  t fts3BinHash(co
345c2 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
345c3 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74  int nKey){.  int
345c4 20 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20   h = 0;.  const 
345c5 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  char *z = (const
345c6 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20   char *)pKey;.  
345c7 77 68 69 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e 20  while( nKey-- > 
345c8 30 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c  0 ){.    h = (h<
345c9 3c 33 29 20 5e 20 68 20 5e 20 2a 28 7a 2b 2b 29  <3) ^ h ^ *(z++)
345ca 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68  ;.  }.  return h
345cb 20 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d   & 0x7fffffff;.}
345cc 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
345cd 42 69 6e 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74  BinCompare(const
345ce 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e   void *pKey1, in
345cf 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64  t n1, const void
345d0 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29   *pKey2, int n2)
345d1 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29  {.  if( n1!=n2 )
345d2 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74   return 1;.  ret
345d3 75 72 6e 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31  urn memcmp(pKey1
345d4 2c 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f  ,pKey2,n1);.}../
345d5 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
345d6 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70  inter to the app
345d7 72 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75  ropriate hash fu
345d8 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65  nction given the
345d9 20 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a   key class..**.*
345da 2a 20 54 68 65 20 43 20 73 79 6e 74 61 78 20 69  * The C syntax i
345db 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
345dc 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 79 20 62  definition may b
345dd 65 20 75 6e 66 61 6d 69 6c 61 72 20 74 6f 20 73  e unfamilar to s
345de 6f 6d 65 20 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d  ome .** programm
345df 65 72 73 2c 20 73 6f 20 77 65 20 70 72 6f 76 69  ers, so we provi
345e0 64 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  de the following
345e1 20 61 64 64 69 74 69 6f 6e 61 6c 20 65 78 70 6c   additional expl
345e2 61 6e 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54  anation:.**.** T
345e3 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66  he name of the f
345e4 75 6e 63 74 69 6f 6e 20 69 73 20 22 66 74 73 48  unction is "ftsH
345e5 61 73 68 46 75 6e 63 74 69 6f 6e 22 2e 20 20 54  ashFunction".  T
345e6 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65  he function take
345e7 73 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 70 61  s a.** single pa
345e8 72 61 6d 65 74 65 72 20 22 6b 65 79 43 6c 61 73  rameter "keyClas
345e9 73 22 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  s".  The return 
345ea 76 61 6c 75 65 20 6f 66 20 66 74 73 48 61 73 68  value of ftsHash
345eb 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20 69 73  Function().** is
345ec 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
345ed 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20  other function. 
345ee 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74   Specifically, t
345ef 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
345f0 2a 2a 20 6f 66 20 66 74 73 48 61 73 68 46 75 6e  ** of ftsHashFun
345f1 63 74 69 6f 6e 28 29 20 69 73 20 61 20 70 6f 69  ction() is a poi
345f2 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
345f3 6f 6e 20 74 68 61 74 20 74 61 6b 65 73 20 74 77  on that takes tw
345f4 6f 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  o parameters.** 
345f5 77 69 74 68 20 74 79 70 65 73 20 22 63 6f 6e 73  with types "cons
345f6 74 20 76 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e  t void*" and "in
345f7 74 22 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  t" and returns a
345f8 6e 20 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74  n "int"..*/.stat
345f9 69 63 20 69 6e 74 20 28 2a 66 74 73 48 61 73 68  ic int (*ftsHash
345fa 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79  Function(int key
345fb 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76 6f  Class))(const vo
345fc 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20  id*,int){.  if( 
345fd 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48  keyClass==FTS3_H
345fe 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20  ASH_STRING ){.  
345ff 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 53 74    return &fts3St
34600 72 48 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rHash;.  }else{.
34601 20 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43      assert( keyC
34602 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f  lass==FTS3_HASH_
34603 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65  BINARY );.    re
34604 74 75 72 6e 20 26 66 74 73 33 42 69 6e 48 61 73  turn &fts3BinHas
34605 68 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  h;.  }.}../*.** 
34606 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
34607 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69   to the appropri
34608 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  ate hash functio
34609 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20  n given the key 
3460a 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  class..**.** For
3460b 20 68 65 6c 70 20 69 6e 20 69 6e 74 65 72 70 72   help in interpr
3460c 65 74 65 64 20 74 68 65 20 6f 62 73 63 75 72 65  eted the obscure
3460d 20 43 20 63 6f 64 65 20 69 6e 20 74 68 65 20 66   C code in the f
3460e 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
3460f 6f 6e 2c 0a 2a 2a 20 73 65 65 20 74 68 65 20 68  on,.** see the h
34610 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e  eader comment on
34611 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 66 75   the previous fu
34612 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
34613 63 20 69 6e 74 20 28 2a 66 74 73 43 6f 6d 70 61  c int (*ftsCompa
34614 72 65 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b  reFunction(int k
34615 65 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20  eyClass))(const 
34616 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
34617 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66  void*,int){.  if
34618 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33  ( keyClass==FTS3
34619 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a  _HASH_STRING ){.
3461a 20 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33      return &fts3
3461b 53 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 65  StrCompare;.  }e
3461c 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
3461d 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
3461e 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20  HASH_BINARY );. 
3461f 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42     return &fts3B
34620 69 6e 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 0a 7d  inCompare;.  }.}
34621 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65  ../* Link an ele
34622 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61  ment into the ha
34623 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74  sh table.*/.stat
34624 69 63 20 76 6f 69 64 20 66 74 73 33 48 61 73 68  ic void fts3Hash
34625 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20  InsertElement(. 
34626 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20   fts3Hash *pH,  
34627 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
34628 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74   complete hash t
34629 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  able */.  struct
3462a 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79   _fts3ht *pEntry
3462b 2c 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20  ,  /* The entry 
3462c 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20  into which pNew 
3462d 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  is inserted */. 
3462e 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70   fts3HashElem *p
3462f 4e 65 77 20 20 20 20 20 20 20 2f 2a 20 54 68 65  New       /* The
34630 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69   element to be i
34631 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20  nserted */.){.  
34632 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 48  fts3HashElem *pH
34633 65 61 64 3b 20 20 20 20 20 2f 2a 20 46 69 72 73  ead;     /* Firs
34634 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64  t element alread
34635 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20  y in pEntry */. 
34636 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d   pHead = pEntry-
34637 3e 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 48  >chain;.  if( pH
34638 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d  ead ){.    pNew-
34639 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20  >next = pHead;. 
3463a 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20     pNew->prev = 
3463b 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20  pHead->prev;.   
3463c 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76   if( pHead->prev
3463d 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d   ){ pHead->prev-
3463e 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a  >next = pNew; }.
3463f 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20      else        
34640 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74       { pH->first
34641 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70   = pNew; }.    p
34642 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65  Head->prev = pNe
34643 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  w;.  }else{.    
34644 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d  pNew->next = pH-
34645 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20  >first;.    if( 
34646 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d  pH->first ){ pH-
34647 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70  >first->prev = p
34648 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d  New; }.    pNew-
34649 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70  >prev = 0;.    p
3464a 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b  H->first = pNew;
3464b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63  .  }.  pEntry->c
3464c 6f 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79  ount++;.  pEntry
3464d 2d 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a  ->chain = pNew;.
3464e 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68  }.../* Resize th
3464f 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20  e hash table so 
34650 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73  that it cantains
34651 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b   "new_size" buck
34652 65 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a  ets..** "new_siz
34653 65 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77  e" must be a pow
34654 65 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68 61  er of 2.  The ha
34655 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66  sh table might f
34656 61 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a  ail .** to resiz
34657 65 20 69 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  e if sqliteMallo
34658 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74  c() fails..*/.st
34659 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 52 65  atic void fts3Re
3465a 68 61 73 68 28 66 74 73 33 48 61 73 68 20 2a 70  hash(fts3Hash *p
3465b 48 2c 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29  H, int new_size)
3465c 7b 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33  {.  struct _fts3
3465d 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20 20  ht *new_ht;     
3465e 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
3465f 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
34660 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c  fts3HashElem *el
34661 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20  em, *next_elem; 
34662 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
34663 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 65 6c  over existing el
34664 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  ements */.  int 
34665 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76  (*xHash)(const v
34666 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 20 2f 2a 20  oid*,int);   /* 
34667 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  The hash functio
34668 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  n */..  assert( 
34669 28 6e 65 77 5f 73 69 7a 65 20 26 20 28 6e 65 77  (new_size & (new
3466a 5f 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a  _size-1))==0 );.
3466b 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75    new_ht = (stru
3466c 63 74 20 5f 66 74 73 33 68 74 20 2a 29 66 74 73  ct _fts3ht *)fts
3466d 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 65 77  3HashMalloc( new
3466e 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72  _size*sizeof(str
3466f 75 63 74 20 5f 66 74 73 33 68 74 29 20 29 3b 0a  uct _fts3ht) );.
34670 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20    if( new_ht==0 
34671 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 74 73 33  ) return;.  fts3
34672 48 61 73 68 46 72 65 65 28 70 48 2d 3e 68 74 29  HashFree(pH->ht)
34673 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77  ;.  pH->ht = new
34674 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a  _ht;.  pH->htsiz
34675 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20  e = new_size;.  
34676 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46  xHash = ftsHashF
34677 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43  unction(pH->keyC
34678 6c 61 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c 65  lass);.  for(ele
34679 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d  m=pH->first, pH-
3467a 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20  >first=0; elem; 
3467b 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d  elem = next_elem
3467c 29 7b 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 28  ){.    int h = (
3467d 2a 78 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70 4b  *xHash)(elem->pK
3467e 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20  ey, elem->nKey) 
3467f 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b 0a  & (new_size-1);.
34680 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20      next_elem = 
34681 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20  elem->next;.    
34682 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c  fts3HashInsertEl
34683 65 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68  ement(pH, &new_h
34684 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d  t[h], elem);.  }
34685 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63  .}../* This func
34686 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e  tion (for intern
34687 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63  al use only) loc
34688 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20  ates an element 
34689 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61  in an.** hash ta
3468a 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 65 73  ble that matches
3468b 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20   the given key. 
3468c 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68   The hash for th
3468d 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c  is key has.** al
3468e 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75  ready been compu
3468f 74 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  ted and is passe
34690 64 20 61 73 20 74 68 65 20 34 74 68 20 70 61 72  d as the 4th par
34691 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  ameter..*/.stati
34692 63 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a  c fts3HashElem *
34693 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42  fts3FindElementB
34694 79 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 66  yHash(.  const f
34695 74 73 33 48 61 73 68 20 2a 70 48 2c 20 2f 2a 20  ts3Hash *pH, /* 
34696 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61  The pH to be sea
34697 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  rched */.  const
34698 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f   void *pKey,   /
34699 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65  * The key we are
3469a 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a   searching for *
3469b 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20 20  /.  int nKey,.  
3469c 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20  int h           
3469d 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20      /* The hash 
3469e 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f  for this key. */
3469f 0a 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c  .){.  fts3HashEl
346a0 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20  em *elem;       
346a1 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20       /* Used to 
346a2 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c  loop thru the el
346a3 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20  ement list */.  
346a4 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
346a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
346a6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
346a7 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73  ents left to tes
346a8 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f  t */.  int (*xCo
346a9 6d 70 61 72 65 29 28 63 6f 6e 73 74 20 76 6f 69  mpare)(const voi
346aa 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
346ab 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 63 6f 6d  d*,int);  /* com
346ac 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
346ad 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68   */..  if( pH->h
346ae 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  t ){.    struct 
346af 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 20  _fts3ht *pEntry 
346b0 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20  = &pH->ht[h];.  
346b1 20 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d    elem = pEntry-
346b2 3e 63 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e  >chain;.    coun
346b3 74 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  t = pEntry->coun
346b4 74 3b 0a 20 20 20 20 78 43 6f 6d 70 61 72 65 20  t;.    xCompare 
346b5 3d 20 66 74 73 43 6f 6d 70 61 72 65 46 75 6e 63  = ftsCompareFunc
346b6 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73  tion(pH->keyClas
346b7 73 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63  s);.    while( c
346b8 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65 6d 20 29  ount-- && elem )
346b9 7b 0a 20 20 20 20 20 20 69 66 28 20 28 2a 78 43  {.      if( (*xC
346ba 6f 6d 70 61 72 65 29 28 65 6c 65 6d 2d 3e 70 4b  ompare)(elem->pK
346bb 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70 4b  ey,elem->nKey,pK
346bc 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a  ey,nKey)==0 ){ .
346bd 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 65          return e
346be 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  lem;.      }.   
346bf 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e     elem = elem->
346c0 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  next;.    }.  }.
346c1 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
346c2 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c  * Remove a singl
346c3 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  e entry from the
346c4 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 65   hash table give
346c5 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
346c6 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61  hat.** element a
346c7 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65  nd a hash on the
346c8 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0a   element's key..
346c9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
346ca 74 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74  ts3RemoveElement
346cb 42 79 48 61 73 68 28 0a 20 20 66 74 73 33 48 61  ByHash(.  fts3Ha
346cc 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20  sh *pH,         
346cd 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69  /* The pH contai
346ce 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20  ning "elem" */. 
346cf 20 66 74 73 33 48 61 73 68 45 6c 65 6d 2a 20 65   fts3HashElem* e
346d0 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c  lem,   /* The el
346d1 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f  ement to be remo
346d2 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20  ved from the pH 
346d3 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20  */.  int h      
346d4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
346d5 73 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  sh value for the
346d6 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20   element */.){. 
346d7 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20   struct _fts3ht 
346d8 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 65  *pEntry;.  if( e
346d9 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 20  lem->prev ){.   
346da 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78   elem->prev->nex
346db 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20  t = elem->next; 
346dc 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48  .  }else{.    pH
346dd 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d 3e  ->first = elem->
346de 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  next;.  }.  if( 
346df 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 20  elem->next ){.  
346e0 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72    elem->next->pr
346e1 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 3b  ev = elem->prev;
346e2 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20  .  }.  pEntry = 
346e3 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 66  &pH->ht[h];.  if
346e4 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d  ( pEntry->chain=
346e5 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45 6e  =elem ){.    pEn
346e6 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65  try->chain = ele
346e7 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 70  m->next;.  }.  p
346e8 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a  Entry->count--;.
346e9 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f    if( pEntry->co
346ea 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70 45  unt<=0 ){.    pE
346eb 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b  ntry->chain = 0;
346ec 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 63  .  }.  if( pH->c
346ed 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e  opyKey && elem->
346ee 70 4b 65 79 20 29 7b 0a 20 20 20 20 66 74 73 33  pKey ){.    fts3
346ef 48 61 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70  HashFree(elem->p
346f0 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 66 74 73 33  Key);.  }.  fts3
346f1 48 61 73 68 46 72 65 65 28 20 65 6c 65 6d 20 29  HashFree( elem )
346f2 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b  ;.  pH->count--;
346f3 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74  .  if( pH->count
346f4 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  <=0 ){.    asser
346f5 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20  t( pH->first==0 
346f6 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
346f7 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20  H->count==0 );. 
346f8 20 20 20 66 74 73 33 48 61 73 68 43 6c 65 61 72     fts3HashClear
346f9 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  (pH);.  }.}../* 
346fa 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74  Attempt to locat
346fb 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20  e an element of 
346fc 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70  the hash table p
346fd 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20  H with a key.** 
346fe 74 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65  that matches pKe
346ff 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20  y,nKey.  Return 
34700 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69  the data for thi
34701 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20  s element if it 
34702 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20  is.** found, or 
34703 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
34704 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51   no match..*/.SQ
34705 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
34706 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61  d *sqlite3Fts3Ha
34707 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 66 74 73  shFind(const fts
34708 33 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74  3Hash *pH, const
34709 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
3470a 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b   nKey){.  int h;
3470b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3470c 20 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65   /* A hash on ke
3470d 79 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45  y */.  fts3HashE
3470e 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a  lem *elem;    /*
3470f 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61   The element tha
34710 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f  t matches key */
34711 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28  .  int (*xHash)(
34712 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29  const void*,int)
34713 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66  ;  /* The hash f
34714 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  unction */..  if
34715 28 20 70 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e 68  ( pH==0 || pH->h
34716 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
34717 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48 61  .  xHash = ftsHa
34718 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b  shFunction(pH->k
34719 65 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73 65  eyClass);.  asse
3471a 72 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b 0a  rt( xHash!=0 );.
3471b 20 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28 70    h = (*xHash)(p
3471c 4b 65 79 2c 6e 4b 65 79 29 3b 0a 20 20 61 73 73  Key,nKey);.  ass
3471d 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65  ert( (pH->htsize
3471e 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
3471f 29 29 3d 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20  ))==0 );.  elem 
34720 3d 20 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e  = fts3FindElemen
34721 74 42 79 48 61 73 68 28 70 48 2c 70 4b 65 79 2c  tByHash(pH,pKey,
34722 6e 4b 65 79 2c 20 68 20 26 20 28 70 48 2d 3e 68  nKey, h & (pH->h
34723 74 73 69 7a 65 2d 31 29 29 3b 0a 20 20 72 65 74  tsize-1));.  ret
34724 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d  urn elem ? elem-
34725 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  >data : 0;.}../*
34726 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65   Insert an eleme
34727 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68  nt into the hash
34728 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20   table pH.  The 
34729 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79  key is pKey,nKey
3472a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61  .** and the data
3472b 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a   is "data"..**.*
3472c 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20  * If no element 
3472d 65 78 69 73 74 73 20 77 69 74 68 20 61 20 6d 61  exists with a ma
3472e 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e  tching key, then
3472f 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e   a new.** elemen
34730 74 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 41  t is created.  A
34731 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79   copy of the key
34732 20 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20   is made if the 
34733 63 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20  copyKey.** flag 
34734 69 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73  is set.  NULL is
34735 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
34736 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d   If another elem
34737 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ent already exis
34738 74 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  ts with the same
34739 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a   key, then the.*
3473a 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61  * new data repla
3473b 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61  ces the old data
3473c 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74   and the old dat
3473d 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  a is returned..*
3473e 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74  * The key is not
3473f 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20   copied in this 
34740 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20  instance.  If a 
34741 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68  malloc fails, th
34742 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61  en.** the new da
34743 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  ta is returned a
34744 6e 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  nd the hash tabl
34745 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  e is unchanged..
34746 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61  **.** If the "da
34747 74 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ta" parameter to
34748 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
34749 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  s NULL, then the
3474a 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72  .** element corr
3474b 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65  esponding to "ke
3474c 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72  y" is removed fr
3474d 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
3474e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
3474f 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
34750 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
34751 28 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48  (.  fts3Hash *pH
34752 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
34753 68 61 73 68 20 74 61 62 6c 65 20 74 6f 20 69 6e  hash table to in
34754 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63  sert into */.  c
34755 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
34756 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a      /* The key *
34757 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20  /.  int nKey,   
34758 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
34759 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
3475a 68 65 20 6b 65 79 20 2a 2f 0a 20 20 76 6f 69 64  he key */.  void
3475b 20 2a 64 61 74 61 20 20 20 20 20 20 20 20 20 20   *data          
3475c 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a   /* The data */.
3475d 29 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20 20  ){.  int hraw;  
3475e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3475f 2a 20 52 61 77 20 68 61 73 68 20 76 61 6c 75 65  * Raw hash value
34760 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20   of the key */. 
34761 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
34762 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68             /* th
34763 65 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65  e hash of the ke
34764 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61  y modulo hash ta
34765 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 66 74  ble size */.  ft
34766 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d  s3HashElem *elem
34767 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20  ;       /* Used 
34768 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65  to loop thru the
34769 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f   element list */
3476a 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20  .  fts3HashElem 
3476b 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20  *new_elem;   /* 
3476c 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65  New element adde
3476d 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 20  d to the pH */. 
3476e 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f   int (*xHash)(co
3476f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
34770 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e   /* The hash fun
34771 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65  ction */..  asse
34772 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 78  rt( pH!=0 );.  x
34773 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75  Hash = ftsHashFu
34774 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c  nction(pH->keyCl
34775 61 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ass);.  assert( 
34776 78 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 72  xHash!=0 );.  hr
34777 61 77 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b  aw = (*xHash)(pK
34778 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 61 73 73  ey, nKey);.  ass
34779 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65  ert( (pH->htsize
3477a 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
3477b 29 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68  ))==0 );.  h = h
3477c 72 61 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a  raw & (pH->htsiz
3477d 65 2d 31 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66  e-1);.  elem = f
3477e 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79  ts3FindElementBy
3477f 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65  Hash(pH,pKey,nKe
34780 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d  y,h);.  if( elem
34781 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c   ){.    void *ol
34782 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64  d_data = elem->d
34783 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74  ata;.    if( dat
34784 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74  a==0 ){.      ft
34785 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42  s3RemoveElementB
34786 79 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29  yHash(pH,elem,h)
34787 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
34788 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20     elem->data = 
34789 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20  data;.    }.    
3478a 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61 3b  return old_data;
3478b 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61 3d  .  }.  if( data=
3478c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
3478d 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d   if( pH->htsize=
3478e 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 52 65  =0 ){.    fts3Re
3478f 68 61 73 68 28 70 48 2c 38 29 3b 0a 20 20 20 20  hash(pH,8);.    
34790 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d  if( pH->htsize==
34791 30 20 29 7b 0a 20 20 20 20 20 20 70 48 2d 3e 63  0 ){.      pH->c
34792 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  ount = 0;.      
34793 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20  return data;.   
34794 20 7d 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65   }.  }.  new_ele
34795 6d 20 3d 20 28 66 74 73 33 48 61 73 68 45 6c 65  m = (fts3HashEle
34796 6d 2a 29 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f  m*)fts3HashMallo
34797 63 28 20 73 69 7a 65 6f 66 28 66 74 73 33 48 61  c( sizeof(fts3Ha
34798 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28  shElem) );.  if(
34799 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72   new_elem==0 ) r
3479a 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 69 66  eturn data;.  if
3479b 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26  ( pH->copyKey &&
3479c 20 70 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20 20   pKey!=0 ){.    
3479d 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d  new_elem->pKey =
3479e 20 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28   fts3HashMalloc(
3479f 20 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28   nKey );.    if(
347a0 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 3d   new_elem->pKey=
347a1 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33  =0 ){.      fts3
347a2 48 61 73 68 46 72 65 65 28 6e 65 77 5f 65 6c 65  HashFree(new_ele
347a3 6d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  m);.      return
347a4 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20   data;.    }.   
347a5 20 6d 65 6d 63 70 79 28 28 76 6f 69 64 2a 29 6e   memcpy((void*)n
347a6 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 70  ew_elem->pKey, p
347a7 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d 65  Key, nKey);.  }e
347a8 6c 73 65 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65  lse{.    new_ele
347a9 6d 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 2a  m->pKey = (void*
347aa 29 70 4b 65 79 3b 0a 20 20 7d 0a 20 20 6e 65 77  )pKey;.  }.  new
347ab 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b  _elem->nKey = nK
347ac 65 79 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b  ey;.  pH->count+
347ad 2b 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75  +;.  if( pH->cou
347ae 6e 74 20 3e 20 70 48 2d 3e 68 74 73 69 7a 65 20  nt > pH->htsize 
347af 29 7b 0a 20 20 20 20 66 74 73 33 52 65 68 61 73  ){.    fts3Rehas
347b0 68 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a  h(pH,pH->htsize*
347b1 32 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  2);.  }.  assert
347b2 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29  ( pH->htsize>0 )
347b3 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 48 2d  ;.  assert( (pH-
347b4 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68  >htsize & (pH->h
347b5 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a  tsize-1))==0 );.
347b6 20 20 68 20 3d 20 68 72 61 77 20 26 20 28 70 48    h = hraw & (pH
347b7 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20 66  ->htsize-1);.  f
347b8 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65  ts3HashInsertEle
347b9 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74  ment(pH, &pH->ht
347ba 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a  [h], new_elem);.
347bb 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61    new_elem->data
347bc 20 3d 20 64 61 74 61 3b 0a 20 20 72 65 74 75 72   = data;.  retur
347bd 6e 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  n 0;.}..#endif /
347be 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
347bf 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
347c0 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
347c1 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
347c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
347c3 66 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a  f fts3_hash.c **
347c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
347c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
347c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
347c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
347c8 20 66 69 6c 65 20 66 74 73 33 5f 70 6f 72 74 65   file fts3_porte
347c9 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
347ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
347cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
347cc 20 32 30 30 36 20 53 65 70 74 65 6d 62 65 72 20   2006 September 
347cd 33 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  30.**.** The aut
347ce 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
347cf 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
347d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
347d1 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
347d2 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
347d3 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
347d4 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
347d5 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
347d6 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
347d7 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
347d8 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
347d9 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
347da 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
347db 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
347dc 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
347dd 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
347de 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
347df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
347e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
347e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
347e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
347e3 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
347e4 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d  ion of the full-
347e5 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65  text-search toke
347e6 6e 69 7a 65 72 20 74 68 61 74 20 69 6d 70 6c 65  nizer that imple
347e7 6d 65 6e 74 73 0a 2a 2a 20 61 20 50 6f 72 74 65  ments.** a Porte
347e8 72 20 73 74 65 6d 6d 65 72 2e 0a 2a 2f 0a 0a 2f  r stemmer..*/../
347e9 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
347ea 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
347eb 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a  ly compiled if:.
347ec 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
347ed 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
347ee 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e  eing built as an
347ef 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20   extension.**   
347f0 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61      (in which ca
347f1 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69  se SQLITE_CORE i
347f2 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20  s not defined), 
347f3 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54  or.**.**     * T
347f4 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69  he FTS3 module i
347f5 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e  s being built in
347f6 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a  to the core of.*
347f7 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28  *       SQLite (
347f8 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51  in which case SQ
347f9 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
347fa 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f   is defined)..*/
347fb 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
347fc 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
347fd 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
347fe 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a 2f 2a  BLE_FTS3)...../*
347ff 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72 69 76 65  .** Class derive
34800 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 74  d from sqlite3_t
34801 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 74 79 70 65  okenizer.*/.type
34802 64 65 66 20 73 74 72 75 63 74 20 70 6f 72 74 65  def struct porte
34803 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20  r_tokenizer {.  
34804 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34805 72 20 62 61 73 65 3b 20 20 20 20 20 20 2f 2a 20  r base;      /* 
34806 42 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 7d 20  Base class */.} 
34807 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
34808 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64  ;../*.** Class d
34809 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69  erived from sqli
3480a 74 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  t3_tokenizer_cur
3480b 73 6f 72 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  sor.*/.typedef s
3480c 74 72 75 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b  truct porter_tok
3480d 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a  enizer_cursor {.
3480e 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3480f 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  zer_cursor base;
34810 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34811 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20  Input;          
34812 2f 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20  /* input we are 
34813 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20  tokenizing */.  
34814 69 6e 74 20 6e 49 6e 70 75 74 3b 20 20 20 20 20  int nInput;     
34815 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34816 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75  size of the inpu
34817 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  t */.  int iOffs
34818 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  et;             
34819 20 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70      /* current p
3481a 6f 73 69 74 69 6f 6e 20 69 6e 20 7a 49 6e 70 75  osition in zInpu
3481b 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65  t */.  int iToke
3481c 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
3481d 20 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20      /* index of 
3481e 6e 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65  next token to be
3481f 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63   returned */.  c
34820 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20  har *zToken;    
34821 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
34822 74 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65  torage for curre
34823 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  nt token */.  in
34824 74 20 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20  t nAllocated;   
34825 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70             /* sp
34826 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f  ace allocated to
34827 20 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a   zToken buffer *
34828 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  /.} porter_token
34829 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f  izer_cursor;.../
3482a 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
3482b 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20  ation */.static 
3482c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
3482d 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 70  kenizer_module p
3482e 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f  orterTokenizerMo
3482f 64 75 6c 65 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72  dule;.../*.** Cr
34830 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e  eate a new token
34831 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a  izer instance..*
34832 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72  /.static int por
34833 74 65 72 43 72 65 61 74 65 28 0a 20 20 69 6e 74  terCreate(.  int
34834 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
34835 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
34836 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
34837 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a  izer **ppTokeniz
34838 65 72 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74  er.){.  porter_t
34839 6f 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a 20 20 74  okenizer *t;.  t
3483a 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e   = (porter_token
3483b 69 7a 65 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  izer *) sqlite3_
3483c 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74  malloc(sizeof(*t
3483d 29 29 3b 0a 20 20 69 66 28 20 74 3d 3d 4e 55 4c  ));.  if( t==NUL
3483e 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
3483f 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65  E_NOMEM;.  memse
34840 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  t(t, 0, sizeof(*
34841 74 29 29 3b 0a 20 20 2a 70 70 54 6f 6b 65 6e 69  t));.  *ppTokeni
34842 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b 0a  zer = &t->base;.
34843 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
34844 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73  OK;.}../*.** Des
34845 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72  troy a tokenizer
34846 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
34847 6f 72 74 65 72 44 65 73 74 72 6f 79 28 73 71 6c  orterDestroy(sql
34848 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
34849 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 73  pTokenizer){.  s
3484a 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f 6b  qlite3_free(pTok
3484b 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75 72  enizer);.  retur
3484c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
3484d 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f  /*.** Prepare to
3484e 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e   begin tokenizin
3484f 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  g a particular s
34850 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75  tring.  The inpu
34851 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62  t.** string to b
34852 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 7a  e tokenized is z
34853 49 6e 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d  Input[0..nInput-
34854 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a  1].  A cursor.**
34855 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65   used to increme
34856 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20  ntally tokenize 
34857 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 72  this string is r
34858 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a  eturned in .** *
34859 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61  ppCursor..*/.sta
3485a 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 4f 70  tic int porterOp
3485b 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  en(.  sqlite3_to
3485c 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
3485d 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  zer,         /* 
3485e 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  The tokenizer */
3485f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34860 49 6e 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75  Input, int nInpu
34861 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72  t,        /* Str
34862 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69  ing to be tokeni
34863 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  zed */.  sqlite3
34864 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
34865 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20  r **ppCursor    
34866 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61  /* OUT: Tokeniza
34867 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29  tion cursor */.)
34868 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  {.  porter_token
34869 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b 0a  izer_cursor *c;.
3486a 0a 20 20 63 20 3d 20 28 70 6f 72 74 65 72 5f 74  .  c = (porter_t
3486b 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
3486c 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
3486d 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a 20  c(sizeof(*c));. 
3486e 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20 72   if( c==NULL ) r
3486f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
34870 45 4d 3b 0a 0a 20 20 63 2d 3e 7a 49 6e 70 75 74  EM;..  c->zInput
34871 20 3d 20 7a 49 6e 70 75 74 3b 0a 20 20 69 66 28   = zInput;.  if(
34872 20 7a 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20   zInput==0 ){.  
34873 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 30 3b    c->nInput = 0;
34874 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 6e  .  }else if( nIn
34875 70 75 74 3c 30 20 29 7b 0a 20 20 20 20 63 2d 3e  put<0 ){.    c->
34876 6e 49 6e 70 75 74 20 3d 20 28 69 6e 74 29 73 74  nInput = (int)st
34877 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20  rlen(zInput);.  
34878 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e 49  }else{.    c->nI
34879 6e 70 75 74 20 3d 20 6e 49 6e 70 75 74 3b 0a 20  nput = nInput;. 
3487a 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74 20   }.  c->iOffset 
3487b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
3487c 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20 74 6f       /* start to
3487d 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68 65 20  kenizing at the 
3487e 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63  beginning */.  c
3487f 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20  ->iToken = 0;.  
34880 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c  c->zToken = NULL
34881 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
34882 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c 6f  /* no space allo
34883 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a 20  cated, yet. */. 
34884 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d   c->nAllocated =
34885 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72   0;..  *ppCursor
34886 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72   = &c->base;.  r
34887 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
34888 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
34889 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
3488a 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79  ursor previously
3488b 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61 6c   opened by a cal
3488c 6c 20 74 6f 0a 2a 2a 20 70 6f 72 74 65 72 4f 70  l to.** porterOp
3488d 65 6e 28 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73  en() above..*/.s
3488e 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72  tatic int porter
3488f 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 74 6f  Close(sqlite3_to
34890 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
34891 70 43 75 72 73 6f 72 29 7b 0a 20 20 70 6f 72 74  pCursor){.  port
34892 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
34893 73 6f 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72  sor *c = (porter
34894 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
34895 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
34896 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 2d 3e  sqlite3_free(c->
34897 7a 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74  zToken);.  sqlit
34898 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20 72 65  e3_free(c);.  re
34899 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3489a 7d 0a 2f 2a 0a 2a 2a 20 56 6f 77 65 6c 20 6f 72  }./*.** Vowel or
3489b 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2f 0a 73 74   consonant.*/.st
3489c 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
3489d 63 54 79 70 65 5b 5d 20 3d 20 7b 0a 20 20 20 30  cType[] = {.   0
3489e 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c  , 1, 1, 1, 0, 1,
3489f 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 1, 0, 1, 1, 
348a0 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
348a1 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 0a 20 20  , 1, 1, 1, 0,.  
348a2 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 31 0a 7d   1, 1, 1, 2, 1.}
348a3 3b 0a 0a 2f 2a 0a 2a 2a 20 69 73 43 6f 6e 73 6f  ;../*.** isConso
348a4 6e 61 6e 74 28 29 20 61 6e 64 20 69 73 56 6f 77  nant() and isVow
348a5 65 6c 28 29 20 64 65 74 65 72 6d 69 6e 65 20 69  el() determine i
348a6 66 20 74 68 65 69 72 20 66 69 72 73 74 20 63 68  f their first ch
348a7 61 72 61 63 74 65 72 20 69 6e 0a 2a 2a 20 74 68  aracter in.** th
348a8 65 20 73 74 72 69 6e 67 20 74 68 65 79 20 70 6f  e string they po
348a9 69 6e 74 20 74 6f 20 69 73 20 61 20 63 6f 6e 73  int to is a cons
348aa 6f 6e 61 6e 74 20 6f 72 20 61 20 76 6f 77 65 6c  onant or a vowel
348ab 2c 20 61 63 63 6f 72 64 69 6e 67 0a 2a 2a 20 74  , according.** t
348ac 6f 20 50 6f 72 74 65 72 20 72 75 6c 73 2e 20 20  o Porter ruls.  
348ad 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 6e 73 6f 6e 61  .**.** A consona
348ae 74 65 20 69 73 20 61 6e 79 20 6c 65 74 74 65 72  te is any letter
348af 20 6f 74 68 65 72 20 74 68 61 6e 20 27 61 27 2c   other than 'a',
348b0 20 27 65 27 2c 20 27 69 27 2c 20 27 6f 27 2c 20   'e', 'i', 'o', 
348b1 6f 72 20 27 75 27 2e 0a 2a 2a 20 27 59 27 20 69  or 'u'..** 'Y' i
348b2 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e  s a consonant un
348b3 6c 65 73 73 20 69 74 20 66 6f 6c 6c 6f 77 73 20  less it follows 
348b4 61 6e 6f 74 68 65 72 20 63 6f 6e 73 6f 6e 61 6e  another consonan
348b5 74 2c 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63  t,.** in which c
348b6 61 73 65 20 69 74 20 69 73 20 61 20 76 6f 77 65  ase it is a vowe
348b7 6c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 73  l..**.** In thes
348b8 65 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 6c  e routine, the l
348b9 65 74 74 65 72 73 20 61 72 65 20 69 6e 20 72 65  etters are in re
348ba 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 53 6f  verse order.  So
348bb 20 74 68 65 20 27 79 27 20 72 75 6c 65 0a 2a 2a   the 'y' rule.**
348bc 20 69 73 20 74 68 61 74 20 27 79 27 20 69 73 20   is that 'y' is 
348bd 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65  a consonant unle
348be 73 73 20 69 74 20 69 73 20 66 6f 6c 6c 6f 77 65  ss it is followe
348bf 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  d by another.** 
348c0 63 6f 6e 73 6f 6e 65 6e 74 2e 0a 2a 2f 0a 73 74  consonent..*/.st
348c1 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77 65 6c  atic int isVowel
348c2 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 73  (const char*);.s
348c3 74 61 74 69 63 20 69 6e 74 20 69 73 43 6f 6e 73  tatic int isCons
348c4 6f 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72  onant(const char
348c5 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20   *z){.  int j;. 
348c6 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20   char x = *z;.  
348c7 69 66 28 20 78 3d 3d 30 20 29 20 72 65 74 75 72  if( x==0 ) retur
348c8 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 78  n 0;.  assert( x
348c9 3e 3d 27 61 27 20 26 26 20 78 3c 3d 27 7a 27 20  >='a' && x<='z' 
348ca 29 3b 0a 20 20 6a 20 3d 20 63 54 79 70 65 5b 78  );.  j = cType[x
348cb 2d 27 61 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32  -'a'];.  if( j<2
348cc 20 29 20 72 65 74 75 72 6e 20 6a 3b 0a 20 20 72   ) return j;.  r
348cd 65 74 75 72 6e 20 7a 5b 31 5d 3d 3d 30 20 7c 7c  eturn z[1]==0 ||
348ce 20 69 73 56 6f 77 65 6c 28 7a 20 2b 20 31 29 3b   isVowel(z + 1);
348cf 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  .}.static int is
348d0 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72  Vowel(const char
348d1 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20   *z){.  int j;. 
348d2 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20   char x = *z;.  
348d3 69 66 28 20 78 3d 3d 30 20 29 20 72 65 74 75 72  if( x==0 ) retur
348d4 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 78  n 0;.  assert( x
348d5 3e 3d 27 61 27 20 26 26 20 78 3c 3d 27 7a 27 20  >='a' && x<='z' 
348d6 29 3b 0a 20 20 6a 20 3d 20 63 54 79 70 65 5b 78  );.  j = cType[x
348d7 2d 27 61 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32  -'a'];.  if( j<2
348d8 20 29 20 72 65 74 75 72 6e 20 31 2d 6a 3b 0a 20   ) return 1-j;. 
348d9 20 72 65 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e   return isConson
348da 61 6e 74 28 7a 20 2b 20 31 29 3b 0a 7d 0a 0a 2f  ant(z + 1);.}../
348db 2a 0a 2a 2a 20 4c 65 74 20 61 6e 79 20 73 65 71  *.** Let any seq
348dc 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20  uence of one or 
348dd 6d 6f 72 65 20 76 6f 77 65 6c 73 20 62 65 20 72  more vowels be r
348de 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 56 20  epresented by V 
348df 61 6e 64 20 6c 65 74 0a 2a 2a 20 43 20 62 65 20  and let.** C be 
348e0 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20  sequence of one 
348e1 6f 72 20 6d 6f 72 65 20 63 6f 6e 73 6f 6e 61 6e  or more consonan
348e2 74 73 2e 20 20 54 68 65 6e 20 65 76 65 72 79 20  ts.  Then every 
348e3 77 6f 72 64 20 63 61 6e 20 62 65 0a 2a 2a 20 72  word can be.** r
348e4 65 70 72 65 73 65 6e 74 65 64 20 61 73 3a 0a 2a  epresented as:.*
348e5 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  *.**           [
348e6 43 5d 20 28 56 43 29 7b 6d 7d 20 5b 56 5d 0a 2a  C] (VC){m} [V].*
348e7 2a 0a 2a 2a 20 49 6e 20 70 72 6f 73 65 3a 20 20  *.** In prose:  
348e8 41 20 77 6f 72 64 20 69 73 20 61 6e 20 6f 70 74  A word is an opt
348e9 69 6f 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20  ional consonant 
348ea 66 6f 6c 6c 6f 77 65 64 20 62 79 20 7a 65 72 6f  followed by zero
348eb 20 6f 72 0a 2a 2a 20 76 6f 77 65 6c 2d 63 6f 6e   or.** vowel-con
348ec 73 6f 6e 61 6e 74 20 70 61 69 72 73 20 66 6f 6c  sonant pairs fol
348ed 6c 6f 77 65 64 20 62 79 20 61 6e 20 6f 70 74 69  lowed by an opti
348ee 6f 6e 61 6c 20 76 6f 77 65 6c 2e 20 20 22 6d 22  onal vowel.  "m"
348ef 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   is the.** numbe
348f0 72 20 6f 66 20 76 6f 77 65 6c 20 63 6f 6e 73 6f  r of vowel conso
348f1 6e 61 6e 74 20 70 61 69 72 73 2e 20 20 54 68 69  nant pairs.  Thi
348f2 73 20 72 6f 75 74 69 6e 65 20 63 6f 6d 70 75 74  s routine comput
348f3 65 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  es the value.** 
348f4 6f 66 20 6d 20 66 6f 72 20 74 68 65 20 66 69 72  of m for the fir
348f5 73 74 20 69 20 62 79 74 65 73 20 6f 66 20 61 20  st i bytes of a 
348f6 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  word..**.** Retu
348f7 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 6d  rn true if the m
348f8 2d 76 61 6c 75 65 20 66 6f 72 20 7a 20 69 73 20  -value for z is 
348f9 31 20 6f 72 20 6d 6f 72 65 2e 20 20 49 6e 20 6f  1 or more.  In o
348fa 74 68 65 72 20 77 6f 72 64 73 2c 0a 2a 2a 20 72  ther words,.** r
348fb 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 7a 20  eturn true if z 
348fc 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73  contains at leas
348fd 74 20 6f 6e 65 20 76 6f 77 65 6c 20 74 68 61 74  t one vowel that
348fe 20 69 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20   is followed.** 
348ff 62 79 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a  by a consonant..
34900 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f  **.** In this ro
34901 75 74 69 6e 65 20 7a 5b 5d 20 69 73 20 69 6e 20  utine z[] is in 
34902 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20  reverse order.  
34903 53 6f 20 77 65 20 61 72 65 20 72 65 61 6c 6c 79  So we are really
34904 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20   looking.** for 
34905 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 6f  an instance of o
34906 66 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f  f a consonant fo
34907 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 6f 77 65  llowed by a vowe
34908 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
34909 20 6d 5f 67 74 5f 30 28 63 6f 6e 73 74 20 63 68   m_gt_0(const ch
3490a 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
3490b 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
3490c 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
3490d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
3490e 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
3490f 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
34910 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a    return *z!=0;.
34911 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20  }../* Like mgt0 
34912 61 62 6f 76 65 20 65 78 63 65 70 74 20 77 65 20  above except we 
34913 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  are looking for 
34914 61 20 76 61 6c 75 65 20 6f 66 20 6d 20 77 68 69  a value of m whi
34915 63 68 20 69 73 0a 2a 2a 20 65 78 61 63 74 6c 79  ch is.** exactly
34916 20 31 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   1.*/.static int
34917 20 6d 5f 65 71 5f 31 28 63 6f 6e 73 74 20 63 68   m_eq_1(const ch
34918 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
34919 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
3491a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
3491b 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
3491c 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
3491d 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
3491e 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65    if( *z==0 ) re
3491f 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28  turn 0;.  while(
34920 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
34921 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
34922 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
34923 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
34924 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
34925 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a    return *z==0;.
34926 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20  }../* Like mgt0 
34927 61 62 6f 76 65 20 65 78 63 65 70 74 20 77 65 20  above except we 
34928 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  are looking for 
34929 61 20 76 61 6c 75 65 20 6f 66 20 6d 3e 31 20 69  a value of m>1 i
3492a 6e 73 74 65 61 64 0a 2a 2a 20 6f 72 20 6d 3e 30  nstead.** or m>0
3492b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
3492c 5f 67 74 5f 31 28 63 6f 6e 73 74 20 63 68 61 72  _gt_1(const char
3492d 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69   *z){.  while( i
3492e 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
3492f 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
34930 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
34931 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
34932 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
34933 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75  if( *z==0 ) retu
34934 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69  rn 0;.  while( i
34935 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
34936 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
34937 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
34938 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
34939 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
3493a 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a  return *z!=0;.}.
3493b 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
3493c 55 45 20 69 66 20 74 68 65 72 65 20 69 73 20 61  UE if there is a
3493d 20 76 6f 77 65 6c 20 61 6e 79 77 68 65 72 65 20   vowel anywhere 
3493e 77 69 74 68 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d  within z[0..n-1]
3493f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
34940 61 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68  asVowel(const ch
34941 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
34942 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20   isConsonant(z) 
34943 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75  ){ z++; }.  retu
34944 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  rn *z!=0;.}../*.
34945 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
34946 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64 73 20  f the word ends 
34947 69 6e 20 61 20 64 6f 75 62 6c 65 20 63 6f 6e 73  in a double cons
34948 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  onant..**.** The
34949 20 74 65 78 74 20 69 73 20 72 65 76 65 72 73 65   text is reverse
3494a 64 20 68 65 72 65 2e 20 53 6f 20 77 65 20 61 72  d here. So we ar
3494b 65 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67  e really looking
3494c 20 61 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74   at.** the first
3494d 20 74 77 6f 20 63 68 61 72 61 63 74 65 72 73 20   two characters 
3494e 6f 66 20 7a 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69  of z[]..*/.stati
3494f 63 20 69 6e 74 20 64 6f 75 62 6c 65 43 6f 6e 73  c int doubleCons
34950 6f 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72  onant(const char
34951 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 69   *z){.  return i
34952 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26  sConsonant(z) &&
34953 20 7a 5b 30 5d 3d 3d 7a 5b 31 5d 20 26 26 20 69   z[0]==z[1] && i
34954 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 31 29 3b  sConsonant(z+1);
34955 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
34956 20 54 52 55 45 20 69 66 20 74 68 65 20 77 6f 72   TRUE if the wor
34957 64 20 65 6e 64 73 20 77 69 74 68 20 74 68 72 65  d ends with thre
34958 65 20 6c 65 74 74 65 72 73 20 77 68 69 63 68 0a  e letters which.
34959 2a 2a 20 61 72 65 20 63 6f 6e 73 6f 6e 61 6e 74  ** are consonant
3495a 2d 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 65 6e 74  -vowel-consonent
3495b 20 61 6e 64 20 77 68 65 72 65 20 74 68 65 20 66   and where the f
3495c 69 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a  inal consonant.*
3495d 2a 20 69 73 20 6e 6f 74 20 27 77 27 2c 20 27 78  * is not 'w', 'x
3495e 27 2c 20 6f 72 20 27 79 27 2e 0a 2a 2a 0a 2a 2a  ', or 'y'..**.**
3495f 20 54 68 65 20 77 6f 72 64 20 69 73 20 72 65 76   The word is rev
34960 65 72 73 65 64 20 68 65 72 65 2e 20 20 53 6f 20  ersed here.  So 
34961 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 63 68  we are really ch
34962 65 63 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  ecking the.** fi
34963 72 73 74 20 74 68 72 65 65 20 6c 65 74 74 65 72  rst three letter
34964 73 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20  s and the first 
34965 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e  one cannot be in
34966 20 5b 77 78 79 5d 2e 0a 2a 2f 0a 73 74 61 74 69   [wxy]..*/.stati
34967 63 20 69 6e 74 20 73 74 61 72 5f 6f 68 28 63 6f  c int star_oh(co
34968 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
34969 72 65 74 75 72 6e 0a 20 20 20 20 7a 5b 30 5d 21  return.    z[0]!
3496a 3d 30 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e  =0 && isConsonan
3496b 74 28 7a 29 20 26 26 0a 20 20 20 20 7a 5b 30 5d  t(z) &&.    z[0]
3496c 21 3d 27 77 27 20 26 26 20 7a 5b 30 5d 21 3d 27  !='w' && z[0]!='
3496d 78 27 20 26 26 20 7a 5b 30 5d 21 3d 27 79 27 20  x' && z[0]!='y' 
3496e 26 26 0a 20 20 20 20 7a 5b 31 5d 21 3d 30 20 26  &&.    z[1]!=0 &
3496f 26 20 69 73 56 6f 77 65 6c 28 7a 2b 31 29 20 26  & isVowel(z+1) &
34970 26 0a 20 20 20 20 7a 5b 32 5d 21 3d 30 20 26 26  &.    z[2]!=0 &&
34971 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 32   isConsonant(z+2
34972 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  );.}../*.** If t
34973 68 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69 74  he word ends wit
34974 68 20 7a 46 72 6f 6d 20 61 6e 64 20 78 43 6f 6e  h zFrom and xCon
34975 64 28 29 20 69 73 20 74 72 75 65 20 66 6f 72 20  d() is true for 
34976 74 68 65 20 73 74 65 6d 0a 2a 2a 20 6f 66 20 74  the stem.** of t
34977 68 65 20 77 6f 72 64 20 74 68 61 74 20 70 72 65  he word that pre
34978 63 65 65 64 73 20 74 68 65 20 7a 46 72 6f 6d 20  ceeds the zFrom 
34979 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 63 68 61  ending, then cha
3497a 6e 67 65 20 74 68 65 20 0a 2a 2a 20 65 6e 64 69  nge the .** endi
3497b 6e 67 20 74 6f 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a  ng to zTo..**.**
3497c 20 54 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20   The input word 
3497d 2a 70 7a 20 61 6e 64 20 7a 46 72 6f 6d 20 61 72  *pz and zFrom ar
3497e 65 20 62 6f 74 68 20 69 6e 20 72 65 76 65 72 73  e both in revers
3497f 65 20 6f 72 64 65 72 2e 20 20 7a 54 6f 0a 2a 2a  e order.  zTo.**
34980 20 69 73 20 69 6e 20 6e 6f 72 6d 61 6c 20 6f 72   is in normal or
34981 64 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 74 75  der. .**.** Retu
34982 72 6e 20 54 52 55 45 20 69 66 20 7a 46 72 6f 6d  rn TRUE if zFrom
34983 20 6d 61 74 63 68 65 73 2e 20 20 52 65 74 75 72   matches.  Retur
34984 6e 20 46 41 4c 53 45 20 69 66 20 7a 46 72 6f 6d  n FALSE if zFrom
34985 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74   does not.** mat
34986 63 68 2e 20 20 4e 6f 74 20 74 68 61 74 20 54 52  ch.  Not that TR
34987 55 45 20 69 73 20 72 65 74 75 72 6e 65 64 20 65  UE is returned e
34988 76 65 6e 20 69 66 20 78 43 6f 6e 64 28 29 20 66  ven if xCond() f
34989 61 69 6c 73 20 61 6e 64 0a 2a 2a 20 6e 6f 20 73  ails and.** no s
3498a 75 62 73 74 69 74 75 74 69 6f 6e 20 6f 63 63 75  ubstitution occu
3498b 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rs..*/.static in
3498c 74 20 73 74 65 6d 28 0a 20 20 63 68 61 72 20 2a  t stem(.  char *
3498d 2a 70 7a 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pz,            
3498e 20 2f 2a 20 54 68 65 20 77 6f 72 64 20 62 65 69   /* The word bei
3498f 6e 67 20 73 74 65 6d 6d 65 64 20 28 52 65 76 65  ng stemmed (Reve
34990 72 73 65 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74  rsed) */.  const
34991 20 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 20 20   char *zFrom,   
34992 20 20 2f 2a 20 49 66 20 74 68 65 20 65 6e 64 69    /* If the endi
34993 6e 67 20 6d 61 74 63 68 65 73 20 74 68 69 73 2e  ng matches this.
34994 2e 2e 20 28 52 65 76 65 72 73 65 64 29 20 2a 2f  .. (Reversed) */
34995 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34996 54 6f 2c 20 20 20 20 20 20 20 2f 2a 20 2e 2e 2e  To,       /* ...
34997 20 63 68 61 6e 67 65 20 74 68 65 20 65 6e 64 69   change the endi
34998 6e 67 20 74 6f 20 74 68 69 73 20 28 6e 6f 74 20  ng to this (not 
34999 72 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20 69  reversed) */.  i
3499a 6e 74 20 28 2a 78 43 6f 6e 64 29 28 63 6f 6e 73  nt (*xCond)(cons
3499b 74 20 63 68 61 72 2a 29 20 20 20 2f 2a 20 43 6f  t char*)   /* Co
3499c 6e 64 69 74 69 6f 6e 20 74 68 61 74 20 6d 75 73  ndition that mus
3499d 74 20 62 65 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  t be true */.){.
3499e 20 20 63 68 61 72 20 2a 7a 20 3d 20 2a 70 7a 3b    char *z = *pz;
3499f 0a 20 20 77 68 69 6c 65 28 20 2a 7a 46 72 6f 6d  .  while( *zFrom
349a0 20 26 26 20 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20 29   && *zFrom==*z )
349a1 7b 20 7a 2b 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b 20  { z++; zFrom++; 
349a2 7d 0a 20 20 69 66 28 20 2a 7a 46 72 6f 6d 21 3d  }.  if( *zFrom!=
349a3 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
349a4 69 66 28 20 78 43 6f 6e 64 20 26 26 20 21 78 43  if( xCond && !xC
349a5 6f 6e 64 28 7a 29 20 29 20 72 65 74 75 72 6e 20  ond(z) ) return 
349a6 31 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 54 6f  1;.  while( *zTo
349a7 20 29 7b 0a 20 20 20 20 2a 28 2d 2d 7a 29 20 3d   ){.    *(--z) =
349a8 20 2a 28 7a 54 6f 2b 2b 29 3b 0a 20 20 7d 0a 20   *(zTo++);.  }. 
349a9 20 2a 70 7a 20 3d 20 7a 3b 0a 20 20 72 65 74 75   *pz = z;.  retu
349aa 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 1;.}../*.** T
349ab 68 69 73 20 69 73 20 74 68 65 20 66 61 6c 6c 62  his is the fallb
349ac 61 63 6b 20 73 74 65 6d 6d 65 72 20 75 73 65 64  ack stemmer used
349ad 20 77 68 65 6e 20 74 68 65 20 70 6f 72 74 65 72   when the porter
349ae 20 73 74 65 6d 6d 65 72 20 69 73 0a 2a 2a 20 69   stemmer is.** i
349af 6e 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 54  nappropriate.  T
349b0 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 69 73  he input word is
349b1 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
349b2 20 6f 75 74 70 75 74 20 77 69 74 68 0a 2a 2a 20   output with.** 
349b3 55 53 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f  US-ASCII case fo
349b4 6c 64 69 6e 67 2e 20 20 49 66 20 74 68 65 20 69  lding.  If the i
349b5 6e 70 75 74 20 77 6f 72 64 20 69 73 20 74 6f 6f  nput word is too
349b6 20 6c 6f 6e 67 20 28 6d 6f 72 65 0a 2a 2a 20 74   long (more.** t
349b7 68 61 6e 20 32 30 20 62 79 74 65 73 20 69 66 20  han 20 bytes if 
349b8 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64  it contains no d
349b9 69 67 69 74 73 20 6f 72 20 6d 6f 72 65 20 74 68  igits or more th
349ba 61 6e 20 36 20 62 79 74 65 73 20 69 66 0a 2a 2a  an 6 bytes if.**
349bb 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 64 69 67   it contains dig
349bc 69 74 73 29 20 74 68 65 6e 20 77 6f 72 64 20 69  its) then word i
349bd 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 32  s truncated to 2
349be 30 20 6f 72 20 36 20 62 79 74 65 73 0a 2a 2a 20  0 or 6 bytes.** 
349bf 62 79 20 74 61 6b 69 6e 67 20 31 30 20 6f 72 20  by taking 10 or 
349c0 33 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  3 bytes from the
349c1 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65   beginning and e
349c2 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nd..*/.static vo
349c3 69 64 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28  id copy_stemmer(
349c4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c  const char *zIn,
349c5 20 69 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a   int nIn, char *
349c6 7a 4f 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74  zOut, int *pnOut
349c7 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6d 78 2c 20  ){.  int i, mx, 
349c8 6a 3b 0a 20 20 69 6e 74 20 68 61 73 44 69 67 69  j;.  int hasDigi
349c9 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  t = 0;.  for(i=0
349ca 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 29 7b 0a 20  ; i<nIn; i++){. 
349cb 20 20 20 69 6e 74 20 63 20 3d 20 7a 49 6e 5b 69     int c = zIn[i
349cc 5d 3b 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41  ];.    if( c>='A
349cd 27 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20  ' && c<='Z' ){. 
349ce 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63       zOut[i] = c
349cf 20 2d 20 27 41 27 20 2b 20 27 61 27 3b 0a 20 20   - 'A' + 'a';.  
349d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
349d1 66 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d  f( c>='0' && c<=
349d2 27 39 27 20 29 20 68 61 73 44 69 67 69 74 20 3d  '9' ) hasDigit =
349d3 20 31 3b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69   1;.      zOut[i
349d4 5d 20 3d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  ] = c;.    }.  }
349d5 0a 20 20 6d 78 20 3d 20 68 61 73 44 69 67 69 74  .  mx = hasDigit
349d6 20 3f 20 33 20 3a 20 31 30 3b 0a 20 20 69 66 28   ? 3 : 10;.  if(
349d7 20 6e 49 6e 3e 6d 78 2a 32 20 29 7b 0a 20 20 20   nIn>mx*2 ){.   
349d8 20 66 6f 72 28 6a 3d 6d 78 2c 20 69 3d 6e 49 6e   for(j=mx, i=nIn
349d9 2d 6d 78 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c  -mx; i<nIn; i++,
349da 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 4f 75   j++){.      zOu
349db 74 5b 6a 5d 20 3d 20 7a 4f 75 74 5b 69 5d 3b 0a  t[j] = zOut[i];.
349dc 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 6a 3b      }.    i = j;
349dd 0a 20 20 7d 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d  .  }.  zOut[i] =
349de 20 30 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69   0;.  *pnOut = i
349df 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 6d  ;.}.../*.** Stem
349e0 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20   the input word 
349e1 7a 49 6e 5b 30 2e 2e 6e 49 6e 2d 31 5d 2e 20 20  zIn[0..nIn-1].  
349e2 53 74 6f 72 65 20 74 68 65 20 6f 75 74 70 75 74  Store the output
349e3 20 69 6e 20 7a 4f 75 74 2e 0a 2a 2a 20 7a 4f 75   in zOut..** zOu
349e4 74 20 69 73 20 61 74 20 6c 65 61 73 74 20 62 69  t is at least bi
349e5 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  g enough to hold
349e6 20 6e 49 6e 20 62 79 74 65 73 2e 20 20 57 72 69   nIn bytes.  Wri
349e7 74 65 20 74 68 65 20 61 63 74 75 61 6c 0a 2a 2a  te the actual.**
349e8 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74   size of the out
349e9 70 75 74 20 77 6f 72 64 20 28 65 78 63 6c 75 73  put word (exclus
349ea 69 76 65 20 6f 66 20 74 68 65 20 27 5c 30 27 20  ive of the '\0' 
349eb 74 65 72 6d 69 6e 61 74 6f 72 29 20 69 6e 74 6f  terminator) into
349ec 20 2a 70 6e 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41   *pnOut..**.** A
349ed 6e 79 20 75 70 70 65 72 2d 63 61 73 65 20 63 68  ny upper-case ch
349ee 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20  aracters in the 
349ef 55 53 2d 41 53 43 49 49 20 63 68 61 72 61 63 74  US-ASCII charact
349f0 65 72 20 73 65 74 20 28 5b 41 2d 5a 5d 29 0a 2a  er set ([A-Z]).*
349f1 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  * are converted 
349f2 74 6f 20 6c 6f 77 65 72 20 63 61 73 65 2e 20 20  to lower case.  
349f3 55 70 70 65 72 2d 63 61 73 65 20 55 54 46 20 63  Upper-case UTF c
349f4 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 2a 2a  haracters are.**
349f5 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a   unchanged..**.*
349f6 2a 20 57 6f 72 64 73 20 74 68 61 74 20 61 72 65  * Words that are
349f7 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 62 6f   longer than abo
349f8 75 74 20 32 30 20 62 79 74 65 73 20 61 72 65 20  ut 20 bytes are 
349f9 73 74 65 6d 6d 65 64 20 62 79 20 72 65 74 61 69  stemmed by retai
349fa 6e 69 6e 67 0a 2a 2a 20 61 20 66 65 77 20 62 79  ning.** a few by
349fb 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67  tes from the beg
349fc 69 6e 6e 69 6e 67 20 61 6e 64 20 74 68 65 20 65  inning and the e
349fd 6e 64 20 6f 66 20 74 68 65 20 77 6f 72 64 2e 20  nd of the word. 
349fe 20 49 66 20 74 68 65 0a 2a 2a 20 77 6f 72 64 20   If the.** word 
349ff 63 6f 6e 74 61 69 6e 73 20 64 69 67 69 74 73 2c  contains digits,
34a00 20 33 20 62 79 74 65 73 20 61 72 65 20 74 61 6b   3 bytes are tak
34a01 65 6e 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  en from the begi
34a02 6e 6e 69 6e 67 20 61 6e 64 0a 2a 2a 20 33 20 62  nning and.** 3 b
34a03 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e  ytes from the en
34a04 64 2e 20 20 46 6f 72 20 6c 6f 6e 67 20 77 6f 72  d.  For long wor
34a05 64 73 20 77 69 74 68 6f 75 74 20 64 69 67 69 74  ds without digit
34a06 73 2c 20 31 30 20 62 79 74 65 73 0a 2a 2a 20 61  s, 10 bytes.** a
34a07 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 65 61  re taken from ea
34a08 63 68 20 65 6e 64 2e 20 20 55 53 2d 41 53 43 49  ch end.  US-ASCI
34a09 49 20 63 61 73 65 20 66 6f 6c 64 69 6e 67 20 73  I case folding s
34a0a 74 69 6c 6c 20 61 70 70 6c 69 65 73 2e 0a 2a 2a  till applies..**
34a0b 20 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75   .** If the inpu
34a0c 74 20 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20  t word contains 
34a0d 6e 6f 74 20 64 69 67 69 74 73 20 62 75 74 20 64  not digits but d
34a0e 6f 65 73 20 63 68 61 72 61 63 74 65 72 73 20 6e  oes characters n
34a0f 6f 74 20 0a 2a 2a 20 69 6e 20 5b 61 2d 7a 41 2d  ot .** in [a-zA-
34a10 5a 5d 20 74 68 65 6e 20 6e 6f 20 73 74 65 6d 6d  Z] then no stemm
34a11 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65 64  ing is attempted
34a12 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
34a13 65 20 6a 75 73 74 20 0a 2a 2a 20 63 6f 70 69 65  e just .** copie
34a14 73 20 74 68 65 20 69 6e 70 75 74 20 69 6e 74 6f  s the input into
34a15 20 74 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20   the input into 
34a16 74 68 65 20 6f 75 74 70 75 74 20 77 69 74 68 20  the output with 
34a17 55 53 2d 41 53 43 49 49 0a 2a 2a 20 63 61 73 65  US-ASCII.** case
34a18 20 66 6f 6c 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   folding..**.** 
34a19 53 74 65 6d 6d 69 6e 67 20 6e 65 76 65 72 20 69  Stemming never i
34a1a 6e 63 72 65 61 73 65 73 20 74 68 65 20 6c 65 6e  ncreases the len
34a1b 67 74 68 20 6f 66 20 74 68 65 20 77 6f 72 64 2e  gth of the word.
34a1c 20 20 53 6f 20 74 68 65 72 65 20 69 73 0a 2a 2a    So there is.**
34a1d 20 6e 6f 20 63 68 61 6e 63 65 20 6f 66 20 6f 76   no chance of ov
34a1e 65 72 66 6c 6f 77 69 6e 67 20 74 68 65 20 7a 4f  erflowing the zO
34a1f 75 74 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74  ut buffer..*/.st
34a20 61 74 69 63 20 76 6f 69 64 20 70 6f 72 74 65 72  atic void porter
34a21 5f 73 74 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63  _stemmer(const c
34a22 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49  har *zIn, int nI
34a23 6e 2c 20 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69  n, char *zOut, i
34a24 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e  nt *pnOut){.  in
34a25 74 20 69 2c 20 6a 2c 20 63 3b 0a 20 20 63 68 61  t i, j, c;.  cha
34a26 72 20 7a 52 65 76 65 72 73 65 5b 32 38 5d 3b 0a  r zReverse[28];.
34a27 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 32 3b 0a    char *z, *z2;.
34a28 20 20 69 66 28 20 6e 49 6e 3c 33 20 7c 7c 20 6e    if( nIn<3 || n
34a29 49 6e 3e 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65  In>=sizeof(zReve
34a2a 72 73 65 29 2d 37 20 29 7b 0a 20 20 20 20 2f 2a  rse)-7 ){.    /*
34a2b 20 54 68 65 20 77 6f 72 64 20 69 73 20 74 6f 6f   The word is too
34a2c 20 62 69 67 20 6f 72 20 74 6f 6f 20 73 6d 61 6c   big or too smal
34a2d 6c 20 66 6f 72 20 74 68 65 20 70 6f 72 74 65 72  l for the porter
34a2e 20 73 74 65 6d 6d 65 72 2e 0a 20 20 20 20 2a 2a   stemmer..    **
34a2f 20 46 61 6c 6c 62 61 63 6b 20 74 6f 20 74 68 65   Fallback to the
34a30 20 63 6f 70 79 20 73 74 65 6d 6d 65 72 20 2a 2f   copy stemmer */
34a31 0a 20 20 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65  .    copy_stemme
34a32 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74  r(zIn, nIn, zOut
34a33 2c 20 70 6e 4f 75 74 29 3b 0a 20 20 20 20 72 65  , pnOut);.    re
34a34 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
34a35 69 3d 30 2c 20 6a 3d 73 69 7a 65 6f 66 28 7a 52  i=0, j=sizeof(zR
34a36 65 76 65 72 73 65 29 2d 36 3b 20 69 3c 6e 49 6e  everse)-6; i<nIn
34a37 3b 20 69 2b 2b 2c 20 6a 2d 2d 29 7b 0a 20 20 20  ; i++, j--){.   
34a38 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20   c = zIn[i];.   
34a39 20 69 66 28 20 63 3e 3d 27 41 27 20 26 26 20 63   if( c>='A' && c
34a3a 3c 3d 27 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a  <='Z' ){.      z
34a3b 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63 20 2b  Reverse[j] = c +
34a3c 20 27 61 27 20 2d 20 27 41 27 3b 0a 20 20 20 20   'a' - 'A';.    
34a3d 7d 65 6c 73 65 20 69 66 28 20 63 3e 3d 27 61 27  }else if( c>='a'
34a3e 20 26 26 20 63 3c 3d 27 7a 27 20 29 7b 0a 20 20   && c<='z' ){.  
34a3f 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20      zReverse[j] 
34a40 3d 20 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = c;.    }else{.
34a41 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 73 65        /* The use
34a42 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20   of a character 
34a43 6e 6f 74 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20  not in [a-zA-Z] 
34a44 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 66 61  means that we fa
34a45 6c 6c 62 61 63 6b 0a 20 20 20 20 20 20 2a 2a 20  llback.      ** 
34a46 74 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d  to the copy stem
34a47 6d 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 70  mer */.      cop
34a48 79 5f 73 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e  y_stemmer(zIn, n
34a49 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29  In, zOut, pnOut)
34a4a 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
34a4b 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73      }.  }.  mems
34a4c 65 74 28 26 7a 52 65 76 65 72 73 65 5b 73 69 7a  et(&zReverse[siz
34a4d 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 35 5d  eof(zReverse)-5]
34a4e 2c 20 30 2c 20 35 29 3b 0a 20 20 7a 20 3d 20 26  , 0, 5);.  z = &
34a4f 7a 52 65 76 65 72 73 65 5b 6a 2b 31 5d 3b 0a 0a  zReverse[j+1];..
34a50 0a 20 20 2f 2a 20 53 74 65 70 20 31 61 20 2a 2f  .  /* Step 1a */
34a51 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27  .  if( z[0]=='s'
34a52 20 29 7b 0a 20 20 20 20 69 66 28 0a 20 20 20 20   ){.    if(.    
34a53 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 65 73 73   !stem(&z, "sess
34a54 22 2c 20 22 73 73 22 2c 20 30 29 20 26 26 0a 20  ", "ss", 0) &&. 
34a55 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20 22 73      !stem(&z, "s
34a56 65 69 22 2c 20 22 69 22 2c 20 30 29 20 20 26 26  ei", "i", 0)  &&
34a57 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20  .     !stem(&z, 
34a58 22 73 73 22 2c 20 22 73 73 22 2c 20 30 29 0a 20  "ss", "ss", 0). 
34a59 20 20 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b     ){.      z++;
34a5a 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
34a5b 20 53 74 65 70 20 31 62 20 2a 2f 20 20 0a 20 20   Step 1b */  .  
34a5c 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 73 74  z2 = z;.  if( st
34a5d 65 6d 28 26 7a 2c 20 22 64 65 65 22 2c 20 22 65  em(&z, "dee", "e
34a5e 65 22 2c 20 6d 5f 67 74 5f 30 29 20 29 7b 0a 20  e", m_gt_0) ){. 
34a5f 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67     /* Do nothing
34a60 2e 20 20 54 68 65 20 77 6f 72 6b 20 77 61 73 20  .  The work was 
34a61 61 6c 6c 20 69 6e 20 74 68 65 20 74 65 73 74 20  all in the test 
34a62 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0a  */.  }else if( .
34a63 20 20 20 20 20 28 73 74 65 6d 28 26 7a 2c 20 22       (stem(&z, "
34a64 67 6e 69 22 2c 20 22 22 2c 20 68 61 73 56 6f 77  gni", "", hasVow
34a65 65 6c 29 20 7c 7c 20 73 74 65 6d 28 26 7a 2c 20  el) || stem(&z, 
34a66 22 64 65 22 2c 20 22 22 2c 20 68 61 73 56 6f 77  "de", "", hasVow
34a67 65 6c 29 29 0a 20 20 20 20 20 20 26 26 20 7a 21  el)).      && z!
34a68 3d 7a 32 0a 20 20 29 7b 0a 20 20 20 20 20 69 66  =z2.  ){.     if
34a69 28 20 73 74 65 6d 28 26 7a 2c 20 22 74 61 22 2c  ( stem(&z, "ta",
34a6a 20 22 61 74 65 22 2c 20 30 29 20 7c 7c 0a 20 20   "ate", 0) ||.  
34a6b 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
34a6c 22 6c 62 22 2c 20 22 62 6c 65 22 2c 20 30 29 20  "lb", "ble", 0) 
34a6d 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
34a6e 28 26 7a 2c 20 22 7a 69 22 2c 20 22 69 7a 65 22  (&z, "zi", "ize"
34a6f 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f  , 0) ){.       /
34a70 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54  * Do nothing.  T
34a71 68 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20  he work was all 
34a72 69 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20  in the test */. 
34a73 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6f      }else if( do
34a74 75 62 6c 65 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ubleConsonant(z)
34a75 20 26 26 20 28 2a 7a 21 3d 27 6c 27 20 26 26 20   && (*z!='l' && 
34a76 2a 7a 21 3d 27 73 27 20 26 26 20 2a 7a 21 3d 27  *z!='s' && *z!='
34a77 7a 27 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 2b  z') ){.       z+
34a78 2b 3b 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66  +;.     }else if
34a79 28 20 6d 5f 65 71 5f 31 28 7a 29 20 26 26 20 73  ( m_eq_1(z) && s
34a7a 74 61 72 5f 6f 68 28 7a 29 20 29 7b 0a 20 20 20  tar_oh(z) ){.   
34a7b 20 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 27 65 27      *(--z) = 'e'
34a7c 3b 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  ;.     }.  }..  
34a7d 2f 2a 20 53 74 65 70 20 31 63 20 2a 2f 0a 20 20  /* Step 1c */.  
34a7e 69 66 28 20 7a 5b 30 5d 3d 3d 27 79 27 20 26 26  if( z[0]=='y' &&
34a7f 20 68 61 73 56 6f 77 65 6c 28 7a 2b 31 29 20 29   hasVowel(z+1) )
34a80 7b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 27 69 27  {.    z[0] = 'i'
34a81 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
34a82 20 32 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20   2 */.  switch( 
34a83 7a 5b 31 5d 20 29 7b 0a 20 20 20 63 61 73 65 20  z[1] ){.   case 
34a84 27 61 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'a':.     stem(&
34a85 7a 2c 20 22 6c 61 6e 6f 69 74 61 22 2c 20 22 61  z, "lanoita", "a
34a86 74 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  te", m_gt_0) ||.
34a87 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
34a88 61 6e 6f 69 74 22 2c 20 22 74 69 6f 6e 22 2c 20  anoit", "tion", 
34a89 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
34a8a 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27  eak;.   case 'c'
34a8b 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
34a8c 22 69 63 6e 65 22 2c 20 22 65 6e 63 65 22 2c 20  "icne", "ence", 
34a8d 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
34a8e 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 61 22 2c  stem(&z, "icna",
34a8f 20 22 61 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29   "ance", m_gt_0)
34a90 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
34a91 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20   case 'e':.     
34a92 73 74 65 6d 28 26 7a 2c 20 22 72 65 7a 69 22 2c  stem(&z, "rezi",
34a93 20 22 69 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ize", m_gt_0);
34a94 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
34a95 63 61 73 65 20 27 67 27 3a 0a 20 20 20 20 20 73  case 'g':.     s
34a96 74 65 6d 28 26 7a 2c 20 22 69 67 6f 6c 22 2c 20  tem(&z, "igol", 
34a97 22 6c 6f 67 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  "log", m_gt_0);.
34a98 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
34a99 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74  ase 'l':.     st
34a9a 65 6d 28 26 7a 2c 20 22 69 6c 62 22 2c 20 22 62  em(&z, "ilb", "b
34a9b 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  le", m_gt_0) ||.
34a9c 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
34a9d 6c 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74  lla", "al", m_gt
34a9e 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
34a9f 28 26 7a 2c 20 22 69 6c 74 6e 65 22 2c 20 22 65  (&z, "iltne", "e
34aa0 6e 74 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  nt", m_gt_0) ||.
34aa1 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
34aa2 6c 65 22 2c 20 22 65 22 2c 20 6d 5f 67 74 5f 30  le", "e", m_gt_0
34aa3 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
34aa4 7a 2c 20 22 69 6c 73 75 6f 22 2c 20 22 6f 75 73  z, "ilsuo", "ous
34aa5 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
34aa6 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
34aa7 27 6f 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'o':.     stem(&
34aa8 7a 2c 20 22 6e 6f 69 74 61 7a 69 22 2c 20 22 69  z, "noitazi", "i
34aa9 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ze", m_gt_0) ||.
34aaa 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e       stem(&z, "n
34aab 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f  oita", "ate", m_
34aac 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
34aad 65 6d 28 26 7a 2c 20 22 72 6f 74 61 22 2c 20 22  em(&z, "rota", "
34aae 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ate", m_gt_0);. 
34aaf 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
34ab0 73 65 20 27 73 27 3a 0a 20 20 20 20 20 73 74 65  se 's':.     ste
34ab1 6d 28 26 7a 2c 20 22 6d 73 69 6c 61 22 2c 20 22  m(&z, "msila", "
34ab2 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
34ab3 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
34ab4 73 65 6e 65 76 69 22 2c 20 22 69 76 65 22 2c 20  senevi", "ive", 
34ab5 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
34ab6 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 6c 75  stem(&z, "ssenlu
34ab7 66 22 2c 20 22 66 75 6c 22 2c 20 6d 5f 67 74 5f  f", "ful", m_gt_
34ab8 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
34ab9 26 7a 2c 20 22 73 73 65 6e 73 75 6f 22 2c 20 22  &z, "ssensuo", "
34aba 6f 75 73 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ous", m_gt_0);. 
34abb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
34abc 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73 74 65  se 't':.     ste
34abd 6d 28 26 7a 2c 20 22 69 74 69 6c 61 22 2c 20 22  m(&z, "itila", "
34abe 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
34abf 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
34ac0 74 69 76 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f  tivi", "ive", m_
34ac1 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
34ac2 65 6d 28 26 7a 2c 20 22 69 74 69 6c 69 62 22 2c  em(&z, "itilib",
34ac3 20 22 62 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ble", m_gt_0);
34ac4 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  .     break;.  }
34ac5 0a 0a 20 20 2f 2a 20 53 74 65 70 20 33 20 2a 2f  ..  /* Step 3 */
34ac6 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 5d 20  .  switch( z[0] 
34ac7 29 7b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  ){.   case 'e':.
34ac8 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65       stem(&z, "e
34ac9 74 61 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67  taci", "ic", m_g
34aca 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65  t_0) ||.     ste
34acb 6d 28 26 7a 2c 20 22 65 76 69 74 61 22 2c 20 22  m(&z, "evita", "
34acc 22 2c 20 6d 5f 67 74 5f 30 29 20 20 20 7c 7c 0a  ", m_gt_0)   ||.
34acd 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65       stem(&z, "e
34ace 7a 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67  zila", "al", m_g
34acf 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
34ad0 3b 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0a 20  ;.   case 'i':. 
34ad1 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74      stem(&z, "it
34ad2 69 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74  ici", "ic", m_gt
34ad3 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
34ad4 0a 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20  .   case 'l':.  
34ad5 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 63     stem(&z, "lac
34ad6 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30  i", "ic", m_gt_0
34ad7 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
34ad8 7a 2c 20 22 6c 75 66 22 2c 20 22 22 2c 20 6d 5f  z, "luf", "", m_
34ad9 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
34ada 6b 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a  k;.   case 's':.
34adb 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
34adc 73 65 6e 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30  sen", "", m_gt_0
34add 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
34ade 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 34 20   }..  /* Step 4 
34adf 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31  */.  switch( z[1
34ae0 5d 20 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27  ] ){.   case 'a'
34ae1 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
34ae2 3d 27 6c 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='l' && m_gt_1(z
34ae3 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +2) ){.       z 
34ae4 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 2;.     }.   
34ae5 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
34ae6 20 27 63 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'c':.     if( z
34ae7 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d  [0]=='e' && z[2]
34ae8 3d 3d 27 6e 27 20 26 26 20 28 7a 5b 33 5d 3d 3d  =='n' && (z[3]==
34ae9 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 65 27  'a' || z[3]=='e'
34aea 29 20 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34  )  && m_gt_1(z+4
34aeb 29 20 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  )  ){.       z +
34aec 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 4;.     }.    
34aed 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
34aee 27 65 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'e':.     if( z[
34aef 30 5d 3d 3d 27 72 27 20 26 26 20 6d 5f 67 74 5f  0]=='r' && m_gt_
34af0 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  1(z+2) ){.      
34af1 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a   z += 2;.     }.
34af2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
34af3 61 73 65 20 27 69 27 3a 0a 20 20 20 20 20 69 66  ase 'i':.     if
34af4 28 20 7a 5b 30 5d 3d 3d 27 63 27 20 26 26 20 6d  ( z[0]=='c' && m
34af5 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20  _gt_1(z+2) ){.  
34af6 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20       z += 2;.   
34af7 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
34af8 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20 20     case 'l':.   
34af9 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
34afa 26 26 20 7a 5b 32 5d 3d 3d 27 62 27 20 26 26 20  && z[2]=='b' && 
34afb 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b  (z[3]=='a' || z[
34afc 33 5d 3d 3d 27 69 27 29 20 26 26 20 6d 5f 67 74  3]=='i') && m_gt
34afd 5f 31 28 7a 2b 34 29 20 29 7b 0a 20 20 20 20 20  _1(z+4) ){.     
34afe 20 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d    z += 4;.     }
34aff 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
34b00 63 61 73 65 20 27 6e 27 3a 0a 20 20 20 20 20 69  case 'n':.     i
34b01 66 28 20 7a 5b 30 5d 3d 3d 27 74 27 20 29 7b 0a  f( z[0]=='t' ){.
34b02 20 20 20 20 20 20 20 69 66 28 20 7a 5b 32 5d 3d         if( z[2]=
34b03 3d 27 61 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='a' ){.        
34b04 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 33 29   if( m_gt_1(z+3)
34b05 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 7a   ){.           z
34b06 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20   += 3;.         
34b07 7d 0a 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  }.       }else i
34b08 66 28 20 7a 5b 32 5d 3d 3d 27 65 27 20 29 7b 0a  f( z[2]=='e' ){.
34b09 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a           stem(&z
34b0a 2c 20 22 74 6e 65 6d 65 22 2c 20 22 22 2c 20 6d  , "tneme", "", m
34b0b 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20  _gt_1) ||.      
34b0c 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65     stem(&z, "tne
34b0d 6d 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20  m", "", m_gt_1) 
34b0e 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
34b0f 28 26 7a 2c 20 22 74 6e 65 22 2c 20 22 22 2c 20  (&z, "tne", "", 
34b10 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 20 20  m_gt_1);.       
34b11 7d 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  }.     }.     br
34b12 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27  eak;.   case 'o'
34b13 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
34b14 3d 27 75 27 20 29 7b 0a 20 20 20 20 20 20 20 69  ='u' ){.       i
34b15 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29  f( m_gt_1(z+2) )
34b16 7b 0a 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20  {.         z += 
34b17 32 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20  2;.       }.    
34b18 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 33 5d 3d   }else if( z[3]=
34b19 3d 27 73 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 74  ='s' || z[3]=='t
34b1a 27 20 29 7b 0a 20 20 20 20 20 20 20 73 74 65 6d  ' ){.       stem
34b1b 28 26 7a 2c 20 22 6e 6f 69 22 2c 20 22 22 2c 20  (&z, "noi", "", 
34b1c 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 7d 0a  m_gt_1);.     }.
34b1d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
34b1e 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 69 66  ase 's':.     if
34b1f 28 20 7a 5b 30 5d 3d 3d 27 6d 27 20 26 26 20 7a  ( z[0]=='m' && z
34b20 5b 32 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74  [2]=='i' && m_gt
34b21 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
34b22 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d    z += 3;.     }
34b23 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
34b24 63 61 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73  case 't':.     s
34b25 74 65 6d 28 26 7a 2c 20 22 65 74 61 22 2c 20 22  tem(&z, "eta", "
34b26 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20  ", m_gt_1) ||.  
34b27 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69     stem(&z, "iti
34b28 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a  ", "", m_gt_1);.
34b29 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
34b2a 61 73 65 20 27 75 27 3a 0a 20 20 20 20 20 69 66  ase 'u':.     if
34b2b 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 26 26 20 7a  ( z[0]=='s' && z
34b2c 5b 32 5d 3d 3d 27 6f 27 20 26 26 20 6d 5f 67 74  [2]=='o' && m_gt
34b2d 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
34b2e 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d    z += 3;.     }
34b2f 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
34b30 63 61 73 65 20 27 76 27 3a 0a 20 20 20 63 61 73  case 'v':.   cas
34b31 65 20 27 7a 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'z':.     if( 
34b32 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32  z[0]=='e' && z[2
34b33 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='i' && m_gt_1
34b34 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+3) ){.       
34b35 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20  z += 3;.     }. 
34b36 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
34b37 20 20 2f 2a 20 53 74 65 70 20 35 61 20 2a 2f 0a    /* Step 5a */.
34b38 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
34b39 29 7b 0a 20 20 20 20 69 66 28 20 6d 5f 67 74 5f  ){.    if( m_gt_
34b3a 31 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20  1(z+1) ){.      
34b3b 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  z++;.    }else i
34b3c 66 28 20 6d 5f 65 71 5f 31 28 7a 2b 31 29 20 26  f( m_eq_1(z+1) &
34b3d 26 20 21 73 74 61 72 5f 6f 68 28 7a 2b 31 29 20  & !star_oh(z+1) 
34b3e 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  ){.      z++;.  
34b3f 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74    }.  }..  /* St
34b40 65 70 20 35 62 20 2a 2f 0a 20 20 69 66 28 20 6d  ep 5b */.  if( m
34b41 5f 67 74 5f 31 28 7a 29 20 26 26 20 7a 5b 30 5d  _gt_1(z) && z[0]
34b42 3d 3d 27 6c 27 20 26 26 20 7a 5b 31 5d 3d 3d 27  =='l' && z[1]=='
34b43 6c 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  l' ){.    z++;. 
34b44 20 7d 0a 0a 20 20 2f 2a 20 7a 5b 5d 20 69 73 20   }..  /* z[] is 
34b45 6e 6f 77 20 74 68 65 20 73 74 65 6d 6d 65 64 20  now the stemmed 
34b46 77 6f 72 64 20 69 6e 20 72 65 76 65 72 73 65 20  word in reverse 
34b47 6f 72 64 65 72 2e 20 20 46 6c 69 70 20 69 74 20  order.  Flip it 
34b48 62 61 63 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64  back.  ** around
34b49 20 69 6e 74 6f 20 66 6f 72 77 61 72 64 20 6f 72   into forward or
34b4a 64 65 72 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a  der and return..
34b4b 20 20 2a 2f 0a 20 20 2a 70 6e 4f 75 74 20 3d 20    */.  *pnOut = 
34b4c 69 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  i = strlen(z);. 
34b4d 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20   zOut[i] = 0;.  
34b4e 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20  while( *z ){.   
34b4f 20 7a 4f 75 74 5b 2d 2d 69 5d 20 3d 20 2a 28 7a   zOut[--i] = *(z
34b50 2b 2b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ++);.  }.}../*.*
34b51 2a 20 43 68 61 72 61 63 74 65 72 73 20 74 68 61  * Characters tha
34b52 74 20 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66  t can be part of
34b53 20 61 20 74 6f 6b 65 6e 2e 20 20 57 65 20 61 73   a token.  We as
34b54 73 75 6d 65 20 61 6e 79 20 63 68 61 72 61 63 74  sume any charact
34b55 65 72 0a 2a 2a 20 77 68 6f 73 65 20 76 61 6c 75  er.** whose valu
34b56 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  e is greater tha
34b57 6e 20 30 78 38 30 20 28 61 6e 79 20 55 54 46 20  n 0x80 (any UTF 
34b58 63 68 61 72 61 63 74 65 72 29 20 63 61 6e 20 62  character) can b
34b59 65 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 74  e.** part of a t
34b5a 6f 6b 65 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20  oken.  In other 
34b5b 77 6f 72 64 73 2c 20 64 65 6c 69 6d 69 74 65 72  words, delimiter
34b5c 73 20 61 6c 6c 20 6d 75 73 74 20 68 61 76 65 0a  s all must have.
34b5d 2a 2a 20 76 61 6c 75 65 73 20 6f 66 20 30 78 37  ** values of 0x7
34b5e 66 20 6f 72 20 6c 6f 77 65 72 2e 0a 2a 2f 0a 73  f or lower..*/.s
34b5f 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
34b60 20 70 6f 72 74 65 72 49 64 43 68 61 72 5b 5d 20   porterIdChar[] 
34b61 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
34b62 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
34b63 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
34b64 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 31 2c   xE xF */.    1,
34b65 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
34b66 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
34b67 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
34b68 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 3x */.    0, 1
34b69 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
34b6a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
34b6b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
34b6c 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  4x */.    1, 1, 
34b6d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
34b6e 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
34b6f 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78   0, 0, 1,  /* 5x
34b70 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
34b71 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
34b72 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
34b73 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a  , 1, 1,  /* 6x *
34b74 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
34b75 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
34b76 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
34b77 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a  0, 0,  /* 7x */.
34b78 7d 3b 0a 23 64 65 66 69 6e 65 20 69 73 44 65 6c  };.#define isDel
34b79 69 6d 28 43 29 20 28 28 28 63 68 3d 43 29 26 30  im(C) (((ch=C)&0
34b7a 78 38 30 29 3d 3d 30 20 26 26 20 28 63 68 3c 30  x80)==0 && (ch<0
34b7b 78 33 30 20 7c 7c 20 21 70 6f 72 74 65 72 49 64  x30 || !porterId
34b7c 43 68 61 72 5b 63 68 2d 30 78 33 30 5d 29 29 0a  Char[ch-0x30])).
34b7d 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74  ./*.** Extract t
34b7e 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72  he next token fr
34b7f 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  om a tokenizatio
34b80 6e 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63  n cursor.  The c
34b81 75 72 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61  ursor must.** ha
34b82 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62  ve been opened b
34b83 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
34b84 6f 20 70 6f 72 74 65 72 4f 70 65 6e 28 29 2e 0a  o porterOpen()..
34b85 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f  */.static int po
34b86 72 74 65 72 4e 65 78 74 28 0a 20 20 73 71 6c 69  rterNext(.  sqli
34b87 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
34b88 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
34b89 2f 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e  /* Cursor return
34b8a 65 64 20 62 79 20 70 6f 72 74 65 72 4f 70 65 6e  ed by porterOpen
34b8b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
34b8c 20 2a 2a 70 7a 54 6f 6b 65 6e 2c 20 20 20 20 20   **pzToken,     
34b8d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
34b8e 3a 20 2a 70 7a 54 6f 6b 65 6e 20 69 73 20 74 68  : *pzToken is th
34b8f 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a  e token text */.
34b90 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20    int *pnBytes, 
34b91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b92 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
34b93 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
34b94 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
34b95 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  *piStartOffset, 
34b96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b97 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67  /* OUT: Starting
34b98 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
34b99 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64   */.  int *piEnd
34b9a 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
34b9b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
34b9c 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20  : Ending offset 
34b9d 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  of token */.  in
34b9e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20  t *piPosition   
34b9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34ba0 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69    /* OUT: Positi
34ba1 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f  on integer of to
34ba2 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74  ken */.){.  port
34ba3 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
34ba4 73 6f 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72  sor *c = (porter
34ba5 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
34ba6 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
34ba7 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
34ba8 63 2d 3e 7a 49 6e 70 75 74 3b 0a 0a 20 20 77 68  c->zInput;..  wh
34ba9 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c  ile( c->iOffset<
34baa 63 2d 3e 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20  c->nInput ){.   
34bab 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
34bac 74 2c 20 63 68 3b 0a 0a 20 20 20 20 2f 2a 20 53  t, ch;..    /* S
34bad 63 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74  can past delimit
34bae 65 72 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f  er characters */
34baf 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  .    while( c->i
34bb0 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74  Offset<c->nInput
34bb1 20 26 26 20 69 73 44 65 6c 69 6d 28 7a 5b 63 2d   && isDelim(z[c-
34bb2 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20  >iOffset]) ){.  
34bb3 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b      c->iOffset++
34bb4 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
34bb5 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69  Count non-delimi
34bb6 74 65 72 20 63 68 61 72 61 63 74 65 72 73 2e 20  ter characters. 
34bb7 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66  */.    iStartOff
34bb8 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74  set = c->iOffset
34bb9 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e  ;.    while( c->
34bba 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75  iOffset<c->nInpu
34bbb 74 20 26 26 20 21 69 73 44 65 6c 69 6d 28 7a 5b  t && !isDelim(z[
34bbc 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a  c->iOffset]) ){.
34bbd 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74        c->iOffset
34bbe 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
34bbf 66 28 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53  f( c->iOffset>iS
34bc0 74 61 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20  tartOffset ){.  
34bc1 20 20 20 20 69 6e 74 20 6e 20 3d 20 63 2d 3e 69      int n = c->i
34bc2 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66  Offset-iStartOff
34bc3 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  set;.      if( n
34bc4 3e 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 29  >c->nAllocated )
34bc5 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 41 6c  {.        c->nAl
34bc6 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a  located = n+20;.
34bc7 20 20 20 20 20 20 20 20 63 2d 3e 7a 54 6f 6b 65          c->zToke
34bc8 6e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  n = sqlite3_real
34bc9 6c 6f 63 28 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 63  loc(c->zToken, c
34bca 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20  ->nAllocated);. 
34bcb 20 20 20 20 20 20 20 69 66 28 20 63 2d 3e 7a 54         if( c->zT
34bcc 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74  oken==NULL ) ret
34bcd 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
34bce 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
34bcf 70 6f 72 74 65 72 5f 73 74 65 6d 6d 65 72 28 26  porter_stemmer(&
34bd0 7a 5b 69 53 74 61 72 74 4f 66 66 73 65 74 5d 2c  z[iStartOffset],
34bd1 20 6e 2c 20 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70   n, c->zToken, p
34bd2 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 2a  nBytes);.      *
34bd3 70 7a 54 6f 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f  pzToken = c->zTo
34bd4 6b 65 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53 74  ken;.      *piSt
34bd5 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61  artOffset = iSta
34bd6 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  rtOffset;.      
34bd7 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 63  *piEndOffset = c
34bd8 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
34bd9 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63   *piPosition = c
34bda 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20  ->iToken++;.    
34bdb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
34bdc 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
34bdd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
34bde 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  NE;.}../*.** The
34bdf 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
34be0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
34be1 74 68 65 20 70 6f 72 74 65 72 2d 73 74 65 6d 6d  the porter-stemm
34be2 65 72 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a  er tokenizer.*/.
34be3 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
34be4 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
34be5 6f 64 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65  odule porterToke
34be6 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a  nizerModule = {.
34be7 20 20 30 2c 0a 20 20 70 6f 72 74 65 72 43 72 65    0,.  porterCre
34be8 61 74 65 2c 0a 20 20 70 6f 72 74 65 72 44 65 73  ate,.  porterDes
34be9 74 72 6f 79 2c 0a 20 20 70 6f 72 74 65 72 4f 70  troy,.  porterOp
34bea 65 6e 2c 0a 20 20 70 6f 72 74 65 72 43 6c 6f 73  en,.  porterClos
34beb 65 2c 0a 20 20 70 6f 72 74 65 72 4e 65 78 74 2c  e,.  porterNext,
34bec 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .};../*.** Alloc
34bed 61 74 65 20 61 20 6e 65 77 20 70 6f 72 74 65 72  ate a new porter
34bee 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74   tokenizer.  Ret
34bef 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
34bf0 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65   the new.** toke
34bf1 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75  nizer in *ppModu
34bf2 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  le.*/.SQLITE_PRI
34bf3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
34bf4 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e  3Fts3PorterToken
34bf5 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71  izerModule(.  sq
34bf6 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
34bf7 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
34bf8 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d  Module.){.  *ppM
34bf9 6f 64 75 6c 65 20 3d 20 26 70 6f 72 74 65 72 54  odule = &porterT
34bfa 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a  okenizerModule;.
34bfb 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
34bfc 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
34bfd 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
34bfe 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
34bff 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
34c00 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
34c01 33 5f 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a  3_porter.c *****
34c02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c04 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
34c05 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
34c06 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
34c07 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
34c08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c09 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
34c0a 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54   June 22.**.** T
34c0b 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
34c0c 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
34c0d 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
34c0e 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
34c0f 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
34c10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
34c11 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
34c12 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
34c13 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
34c14 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
34c15 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
34c16 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
34c17 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
34c18 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
34c19 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
34c1a 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
34c1b 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
34c1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
34c21 2a 2a 20 54 68 69 73 20 69 73 20 70 61 72 74 20  ** This is part 
34c22 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64  of an SQLite mod
34c23 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  ule implementing
34c24 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63   full-text searc
34c25 68 2e 0a 2a 2a 20 54 68 69 73 20 70 61 72 74 69  h..** This parti
34c26 63 75 6c 61 72 20 66 69 6c 65 20 69 6d 70 6c 65  cular file imple
34c27 6d 65 6e 74 73 20 74 68 65 20 67 65 6e 65 72 69  ments the generi
34c28 63 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65  c tokenizer inte
34c29 72 66 61 63 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  rface..*/../*.**
34c2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
34c2b 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
34c2c 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
34c2d 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
34c2e 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
34c2f 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
34c30 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
34c31 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
34c32 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
34c33 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
34c34 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
34c35 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
34c36 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
34c37 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
34c38 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
34c39 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
34c3a 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
34c3b 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66  defined)..*/.#if
34c3c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
34c3d 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
34c3e 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
34c3f 46 54 53 33 29 0a 0a 23 69 66 6e 64 65 66 20 53  FTS3)..#ifndef S
34c40 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c  QLITE_CORE.  SQL
34c41 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
34c42 49 54 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  IT1.#endif.../*.
34c43 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
34c44 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61  n of the SQL sca
34c45 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  lar function for
34c46 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 75   accessing the u
34c47 6e 64 65 72 6c 79 69 6e 67 20 0a 2a 2a 20 68 61  nderlying .** ha
34c48 73 68 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66  sh table. This f
34c49 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
34c4a 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  alled as follows
34c4b 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  :.**.**   SELECT
34c4c 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
34c4d 28 3c 6b 65 79 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a  (<key-name>);.**
34c4e 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74     SELECT <funct
34c4f 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e  ion-name>(<key-n
34c50 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29  ame>, <pointer>)
34c51 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66  ;.**.** where <f
34c52 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73  unction-name> is
34c53 20 74 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64   the name passed
34c54 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
34c55 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
34c56 65 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  e sqlite3Fts3Ini
34c57 74 48 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e  tHashTable() fun
34c58 63 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73  ction (e.g. 'fts
34c59 33 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 2e 0a 2a  3_tokenizer')..*
34c5a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 3c 70 6f 69  *.** If the <poi
34c5b 6e 74 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69  nter> argument i
34c5c 73 20 73 70 65 63 69 66 69 65 64 2c 20 69 74 20  s specified, it 
34c5d 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62 20 76  must be a blob v
34c5e 61 6c 75 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69  alue.** containi
34c5f 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
34c60 62 65 20 73 74 6f 72 65 64 20 61 73 20 74 68 65  be stored as the
34c61 20 68 61 73 68 20 64 61 74 61 20 63 6f 72 72 65   hash data corre
34c62 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 74  sponding.** to t
34c63 68 65 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e  he string <key-n
34c64 61 6d 65 3e 2e 20 49 66 20 3c 70 6f 69 6e 74 65  ame>. If <pointe
34c65 72 3e 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  r> is not specif
34c66 69 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ied, then.** the
34c67 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d   string <key-nam
34c68 65 3e 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20  e> must already 
34c69 65 78 69 73 74 20 69 6e 20 74 68 65 20 68 61 73  exist in the has
34c6a 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73   table. Otherwis
34c6b 65 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69  e,.** an error i
34c6c 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
34c6d 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
34c6e 20 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61   the <pointer> a
34c6f 72 67 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69  rgument is speci
34c70 66 69 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20  fied, the value 
34c71 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 61  returned.** is a
34c72 20 62 6c 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67   blob containing
34c73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 73 74 6f   the pointer sto
34c74 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68 20  red as the hash 
34c75 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
34c76 6e 67 0a 2a 2a 20 74 6f 20 73 74 72 69 6e 67 20  ng.** to string 
34c77 3c 6b 65 79 2d 6e 61 6d 65 3e 20 28 61 66 74 65  <key-name> (afte
34c78 72 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  r the hash-table
34c79 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 66 20   is updated, if 
34c7a 61 70 70 6c 69 63 61 62 6c 65 29 2e 0a 2a 2f 0a  applicable)..*/.
34c7b 73 74 61 74 69 63 20 76 6f 69 64 20 73 63 61 6c  static void scal
34c7c 61 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  arFunc(.  sqlite
34c7d 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
34c7e 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
34c7f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
34c80 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 74 73 33  **argv.){.  fts3
34c81 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 76  Hash *pHash;.  v
34c82 6f 69 64 20 2a 70 50 74 72 20 3d 20 30 3b 0a 20  oid *pPtr = 0;. 
34c83 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
34c84 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
34c85 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73  nt nName;..  ass
34c86 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20  ert( argc==1 || 
34c87 61 72 67 63 3d 3d 32 20 29 3b 0a 0a 20 20 70 48  argc==2 );..  pH
34c88 61 73 68 20 3d 20 28 66 74 73 33 48 61 73 68 20  ash = (fts3Hash 
34c89 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  *)sqlite3_user_d
34c8a 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20  ata(context);.. 
34c8b 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
34c8c 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
34c8d 5b 30 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20  [0]);.  nName = 
34c8e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
34c8f 74 65 73 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a  tes(argv[0])+1;.
34c90 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29  .  if( argc==2 )
34c91 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4f 6c 64  {.    void *pOld
34c92 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71  ;.    int n = sq
34c93 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
34c94 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  s(argv[1]);.    
34c95 69 66 28 20 6e 21 3d 73 69 7a 65 6f 66 28 70 50  if( n!=sizeof(pP
34c96 74 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tr) ){.      sql
34c97 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
34c98 72 28 63 6f 6e 74 65 78 74 2c 20 22 61 72 67 75  r(context, "argu
34c99 6d 65 6e 74 20 74 79 70 65 20 6d 69 73 6d 61 74  ment type mismat
34c9a 63 68 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ch", -1);.      
34c9b 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
34c9c 20 20 70 50 74 72 20 3d 20 2a 28 76 6f 69 64 20    pPtr = *(void 
34c9d 2a 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  **)sqlite3_value
34c9e 5f 62 6c 6f 62 28 61 72 67 76 5b 31 5d 29 3b 0a  _blob(argv[1]);.
34c9f 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74      pOld = sqlit
34ca0 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
34ca1 28 70 48 61 73 68 2c 20 28 76 6f 69 64 20 2a 29  (pHash, (void *)
34ca2 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 50  zName, nName, pP
34ca3 74 72 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  tr);.    if( pOl
34ca4 64 3d 3d 70 50 74 72 20 29 7b 0a 20 20 20 20 20  d==pPtr ){.     
34ca5 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
34ca6 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22  error(context, "
34ca7 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20  out of memory", 
34ca8 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  -1);.      retur
34ca9 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  n;.    }.  }else
34caa 7b 0a 20 20 20 20 70 50 74 72 20 3d 20 73 71 6c  {.    pPtr = sql
34cab 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
34cac 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  (pHash, zName, n
34cad 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Name);.    if( !
34cae 70 50 74 72 20 29 7b 0a 20 20 20 20 20 20 63 68  pPtr ){.      ch
34caf 61 72 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74  ar *zErr = sqlit
34cb0 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
34cb1 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
34cb2 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
34cb3 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
34cb4 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
34cb5 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20 20  zErr, -1);.     
34cb6 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
34cb7 72 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  rr);.      retur
34cb8 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
34cb9 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
34cba 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 76 6f  lob(context, (vo
34cbb 69 64 20 2a 29 26 70 50 74 72 2c 20 73 69 7a 65  id *)&pPtr, size
34cbc 6f 66 28 70 50 74 72 29 2c 20 53 51 4c 49 54 45  of(pPtr), SQLITE
34cbd 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a  _TRANSIENT);.}..
34cbe 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
34cbf 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ST.../*.** Imple
34cc0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73  mentation of a s
34cc1 70 65 63 69 61 6c 20 53 51 4c 20 73 63 61 6c 61  pecial SQL scala
34cc2 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74  r function for t
34cc3 65 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72  esting tokenizer
34cc4 73 20 0a 2a 2a 20 64 65 73 69 67 6e 65 64 20 74  s .** designed t
34cc5 6f 20 62 65 20 75 73 65 64 20 69 6e 20 63 6f 6e  o be used in con
34cc6 63 65 72 74 20 77 69 74 68 20 74 68 65 20 54 63  cert with the Tc
34cc7 6c 20 74 65 73 74 69 6e 67 20 66 72 61 6d 65 77  l testing framew
34cc8 6f 72 6b 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e  ork. This.** fun
34cc9 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61  ction must be ca
34cca 6c 6c 65 64 20 77 69 74 68 20 74 77 6f 20 61 72  lled with two ar
34ccb 67 75 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  guments:.**.**  
34ccc 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f   SELECT <functio
34ccd 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d  n-name>(<key-nam
34cce 65 3e 2c 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e  e>, <input-strin
34ccf 67 3e 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54  g>);.**   SELECT
34cd0 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
34cd1 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f  (<key-name>, <po
34cd2 69 6e 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77  inter>);.**.** w
34cd3 68 65 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  here <function-n
34cd4 61 6d 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65  ame> is the name
34cd5 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
34cd6 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a  econd argument.*
34cd7 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  * to the sqlite3
34cd8 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c  Fts3InitHashTabl
34cd9 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e  e() function (e.
34cda 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
34cdb 65 72 27 29 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e  er').** concaten
34cdc 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 74  ated with the st
34cdd 72 69 6e 67 20 27 5f 74 65 73 74 27 20 28 65 2e  ring '_test' (e.
34cde 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
34cdf 65 72 5f 74 65 73 74 27 29 2e 0a 2a 2a 0a 2a 2a  er_test')..**.**
34ce0 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
34ce1 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68  e is a string th
34ce2 61 74 20 6d 61 79 20 62 65 20 69 6e 74 65 72 70  at may be interp
34ce3 72 65 74 65 64 20 61 73 20 61 20 54 63 6c 0a 2a  reted as a Tcl.*
34ce4 2a 20 6c 69 73 74 2e 20 46 6f 72 20 65 61 63 68  * list. For each
34ce5 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 3c 69   token in the <i
34ce6 6e 70 75 74 2d 73 74 72 69 6e 67 3e 2c 20 74 68  nput-string>, th
34ce7 72 65 65 20 65 6c 65 6d 65 6e 74 73 20 61 72 65  ree elements are
34ce8 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65  .** added to the
34ce9 20 72 65 74 75 72 6e 65 64 20 6c 69 73 74 2e 20   returned list. 
34cea 54 68 65 20 66 69 72 73 74 20 69 73 20 74 68 65  The first is the
34ceb 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 2c   token position,
34cec 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20   the .** second 
34ced 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78  is the token tex
34cee 74 20 28 66 6f 6c 64 65 64 2c 20 73 74 65 6d 6d  t (folded, stemm
34cef 65 64 2c 20 65 74 63 2e 29 20 61 6e 64 20 74 68  ed, etc.) and th
34cf0 65 20 74 68 69 72 64 20 69 73 20 74 68 65 0a 2a  e third is the.*
34cf1 2a 20 73 75 62 73 74 72 69 6e 67 20 6f 66 20 3c  * substring of <
34cf2 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e 20 61 73  input-string> as
34cf3 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
34cf4 65 20 74 6f 6b 65 6e 2e 20 46 6f 72 20 65 78 61  e token. For exa
34cf5 6d 70 6c 65 2c 20 0a 2a 2a 20 75 73 69 6e 67 20  mple, .** using 
34cf6 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 22 73 69  the built-in "si
34cf7 6d 70 6c 65 22 20 74 6f 6b 65 6e 69 7a 65 72 3a  mple" tokenizer:
34cf8 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
34cf9 66 74 73 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65  fts_tokenizer_te
34cfa 73 74 28 27 73 69 6d 70 6c 65 27 2c 20 27 49 20  st('simple', 'I 
34cfb 64 6f 6e 27 74 20 73 65 65 20 68 6f 77 27 29 3b  don't see how');
34cfc 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75  .**.** will retu
34cfd 72 6e 20 74 68 65 20 73 74 72 69 6e 67 3a 0a 2a  rn the string:.*
34cfe 2a 0a 2a 2a 20 20 20 22 7b 30 20 69 20 49 20 31  *.**   "{0 i I 1
34cff 20 64 6f 6e 74 20 64 6f 6e 27 74 20 32 20 73 65   dont don't 2 se
34d00 65 20 73 65 65 20 33 20 68 6f 77 20 68 6f 77 7d  e see 3 how how}
34d01 22 0a 2a 2a 20 20 20 0a 2a 2f 0a 73 74 61 74 69  ".**   .*/.stati
34d02 63 20 76 6f 69 64 20 74 65 73 74 46 75 6e 63 28  c void testFunc(
34d03 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
34d04 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
34d05 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
34d06 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
34d07 29 7b 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  ){.  fts3Hash *p
34d08 48 61 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Hash;.  sqlite3_
34d09 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
34d0a 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74   *p;.  sqlite3_t
34d0b 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
34d0c 69 7a 65 72 20 3d 20 30 3b 0a 20 20 73 71 6c 69  izer = 0;.  sqli
34d0d 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
34d0e 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 30 3b 0a  rsor *pCsr = 0;.
34d0f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34d10 45 72 72 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73  Err = 0;..  cons
34d11 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20  t char *zName;. 
34d12 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 63 6f   int nName;.  co
34d13 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
34d14 3b 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a  ;.  int nInput;.
34d15 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
34d16 41 72 67 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73  Arg = 0;..  cons
34d17 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a  t char *zToken;.
34d18 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20    int nToken;.  
34d19 69 6e 74 20 69 53 74 61 72 74 3b 0a 20 20 69 6e  int iStart;.  in
34d1a 74 20 69 45 6e 64 3b 0a 20 20 69 6e 74 20 69 50  t iEnd;.  int iP
34d1b 6f 73 3b 0a 0a 20 20 54 63 6c 5f 4f 62 6a 20 2a  os;..  Tcl_Obj *
34d1c 70 52 65 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  pRet;..  assert(
34d1d 20 61 72 67 63 3d 3d 32 20 7c 7c 20 61 72 67 63   argc==2 || argc
34d1e 3d 3d 33 20 29 3b 0a 0a 20 20 6e 4e 61 6d 65 20  ==3 );..  nName 
34d1f 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
34d20 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
34d21 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74    zName = (const
34d22 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
34d23 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
34d24 30 5d 29 3b 0a 20 20 6e 49 6e 70 75 74 20 3d 20  0]);.  nInput = 
34d25 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
34d26 74 65 73 28 61 72 67 76 5b 61 72 67 63 2d 31 5d  tes(argv[argc-1]
34d27 29 3b 0a 20 20 7a 49 6e 70 75 74 20 3d 20 28 63  );.  zInput = (c
34d28 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
34d29 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
34d2a 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 0a 20  rgv[argc-1]);.. 
34d2b 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a   if( argc==3 ){.
34d2c 20 20 20 20 7a 41 72 67 20 3d 20 28 63 6f 6e 73      zArg = (cons
34d2d 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
34d2e 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
34d2f 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 70 48 61  [1]);.  }..  pHa
34d30 73 68 20 3d 20 28 66 74 73 33 48 61 73 68 20 2a  sh = (fts3Hash *
34d31 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61  )sqlite3_user_da
34d32 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 70  ta(context);.  p
34d33 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65   = (sqlite3_toke
34d34 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73  nizer_module *)s
34d35 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
34d36 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  nd(pHash, zName,
34d37 20 6e 4e 61 6d 65 2b 31 29 3b 0a 0a 20 20 69 66   nName+1);..  if
34d38 28 20 21 70 20 29 7b 0a 20 20 20 20 63 68 61 72  ( !p ){.    char
34d39 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33   *zErr = sqlite3
34d3a 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77  _mprintf("unknow
34d3b 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22  n tokenizer: %s"
34d3c 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71  , zName);.    sq
34d3d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
34d3e 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72  or(context, zErr
34d3f 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , -1);.    sqlit
34d40 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 20  e3_free(zErr);. 
34d41 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
34d42 20 20 70 52 65 74 20 3d 20 54 63 6c 5f 4e 65 77    pRet = Tcl_New
34d43 4f 62 6a 28 29 3b 0a 20 20 54 63 6c 5f 49 6e 63  Obj();.  Tcl_Inc
34d44 72 52 65 66 43 6f 75 6e 74 28 70 52 65 74 29 3b  rRefCount(pRet);
34d45 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
34d46 4b 21 3d 70 2d 3e 78 43 72 65 61 74 65 28 7a 41  K!=p->xCreate(zA
34d47 72 67 20 3f 20 31 20 3a 20 30 2c 20 26 7a 41 72  rg ? 1 : 0, &zAr
34d48 67 2c 20 26 70 54 6f 6b 65 6e 69 7a 65 72 29 20  g, &pTokenizer) 
34d49 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65  ){.    zErr = "e
34d4a 72 72 6f 72 20 69 6e 20 78 43 72 65 61 74 65 28  rror in xCreate(
34d4b 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e  )";.    goto fin
34d4c 69 73 68 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65  ish;.  }.  pToke
34d4d 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d  nizer->pModule =
34d4e 20 70 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45   p;.  if( SQLITE
34d4f 5f 4f 4b 21 3d 70 2d 3e 78 4f 70 65 6e 28 70 54  _OK!=p->xOpen(pT
34d50 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75 74  okenizer, zInput
34d51 2c 20 6e 49 6e 70 75 74 2c 20 26 70 43 73 72 29  , nInput, &pCsr)
34d52 20 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22   ){.    zErr = "
34d53 65 72 72 6f 72 20 69 6e 20 78 4f 70 65 6e 28 29  error in xOpen()
34d54 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69  ";.    goto fini
34d55 73 68 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e  sh;.  }.  pCsr->
34d56 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
34d57 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20 77 68 69 6c  kenizer;..  whil
34d58 65 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 2d  e( SQLITE_OK==p-
34d59 3e 78 4e 65 78 74 28 70 43 73 72 2c 20 26 7a 54  >xNext(pCsr, &zT
34d5a 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26  oken, &nToken, &
34d5b 69 53 74 61 72 74 2c 20 26 69 45 6e 64 2c 20 26  iStart, &iEnd, &
34d5c 69 50 6f 73 29 20 29 7b 0a 20 20 20 20 54 63 6c  iPos) ){.    Tcl
34d5d 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c  _ListObjAppendEl
34d5e 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20 54  ement(0, pRet, T
34d5f 63 6c 5f 4e 65 77 49 6e 74 4f 62 6a 28 69 50 6f  cl_NewIntObj(iPo
34d60 73 29 29 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73  s));.    Tcl_Lis
34d61 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e  tObjAppendElemen
34d62 74 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e  t(0, pRet, Tcl_N
34d63 65 77 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b  ewStringObj(zTok
34d64 65 6e 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20  en, nToken));.  
34d65 20 20 7a 54 6f 6b 65 6e 20 3d 20 26 7a 49 6e 70    zToken = &zInp
34d66 75 74 5b 69 53 74 61 72 74 5d 3b 0a 20 20 20 20  ut[iStart];.    
34d67 6e 54 6f 6b 65 6e 20 3d 20 69 45 6e 64 2d 69 53  nToken = iEnd-iS
34d68 74 61 72 74 3b 0a 20 20 20 20 54 63 6c 5f 4c 69  tart;.    Tcl_Li
34d69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65  stObjAppendEleme
34d6a 6e 74 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f  nt(0, pRet, Tcl_
34d6b 4e 65 77 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f  NewStringObj(zTo
34d6c 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20  ken, nToken));. 
34d6d 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45   }..  if( SQLITE
34d6e 5f 4f 4b 21 3d 70 2d 3e 78 43 6c 6f 73 65 28 70  _OK!=p->xClose(p
34d6f 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72  Csr) ){.    zErr
34d70 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43 6c   = "error in xCl
34d71 6f 73 65 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f  ose()";.    goto
34d72 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 69   finish;.  }.  i
34d73 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d  f( SQLITE_OK!=p-
34d74 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e  >xDestroy(pToken
34d75 69 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72  izer) ){.    zEr
34d76 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 44  r = "error in xD
34d77 65 73 74 72 6f 79 28 29 22 3b 0a 20 20 20 20 67  estroy()";.    g
34d78 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a  oto finish;.  }.
34d79 0a 66 69 6e 69 73 68 3a 0a 20 20 69 66 28 20 7a  .finish:.  if( z
34d7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Err ){.    sqlit
34d7b 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
34d7c 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
34d7d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
34d7e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
34d7f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 54 63  text(context, Tc
34d80 6c 5f 47 65 74 53 74 72 69 6e 67 28 70 52 65 74  l_GetString(pRet
34d81 29 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52  ), -1, SQLITE_TR
34d82 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 20 20  ANSIENT);.  }.  
34d83 54 63 6c 5f 44 65 63 72 52 65 66 43 6f 75 6e 74  Tcl_DecrRefCount
34d84 28 70 52 65 74 29 3b 0a 7d 0a 0a 73 74 61 74 69  (pRet);.}..stati
34d85 63 0a 69 6e 74 20 72 65 67 69 73 74 65 72 54 6f  c.int registerTo
34d86 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  kenizer(.  sqlit
34d87 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20  e3 *db, .  char 
34d88 2a 7a 4e 61 6d 65 2c 20 0a 20 20 63 6f 6e 73 74  *zName, .  const
34d89 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34d8a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 0a 29 7b 0a  er_module *p.){.
34d8b 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
34d8c 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
34d8d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53  .  const char zS
34d8e 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66  ql[] = "SELECT f
34d8f 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 2c  ts3_tokenizer(?,
34d90 20 3f 29 22 3b 0a 0a 20 20 72 63 20 3d 20 73 71   ?)";..  rc = sq
34d91 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
34d92 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
34d93 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28  pStmt, 0);.  if(
34d94 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
34d95 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
34d96 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
34d97 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c  bind_text(pStmt,
34d98 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53   1, zName, -1, S
34d99 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
34d9a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
34d9b 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 26 70 2c  ob(pStmt, 2, &p,
34d9c 20 73 69 7a 65 6f 66 28 70 29 2c 20 53 51 4c 49   sizeof(p), SQLI
34d9d 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71  TE_STATIC);.  sq
34d9e 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
34d9f 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  );..  return sql
34da0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
34da1 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a  tmt);.}..static.
34da2 69 6e 74 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  int queryTokeniz
34da3 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  er(.  sqlite3 *d
34da4 62 2c 20 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  b, .  char *zNam
34da5 65 2c 20 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e,  .  const sql
34da6 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
34da7 6f 64 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20  odule **pp.){.  
34da8 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
34da9 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20  3_stmt *pStmt;. 
34daa 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c   const char zSql
34dab 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73  [] = "SELECT fts
34dac 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b  3_tokenizer(?)";
34dad 0a 0a 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72  ..  *pp = 0;.  r
34dae 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  c = sqlite3_prep
34daf 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c  are_v2(db, zSql,
34db0 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b   -1, &pStmt, 0);
34db1 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
34db2 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
34db3 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71  rn rc;.  }..  sq
34db4 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28  lite3_bind_text(
34db5 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c  pStmt, 1, zName,
34db6 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54   -1, SQLITE_STAT
34db7 49 43 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  IC);.  if( SQLIT
34db8 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
34db9 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20  tep(pStmt) ){.  
34dba 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f    if( sqlite3_co
34dbb 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c  lumn_type(pStmt,
34dbc 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42   0)==SQLITE_BLOB
34dbd 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
34dbe 28 70 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c  (pp, sqlite3_col
34dbf 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  umn_blob(pStmt, 
34dc0 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29  0), sizeof(*pp))
34dc1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
34dc2 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69  eturn sqlite3_fi
34dc3 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d  nalize(pStmt);.}
34dc4 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
34dc5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
34dc6 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  3SimpleTokenizer
34dc7 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74  Module(sqlite3_t
34dc8 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
34dc9 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29  const**ppModule)
34dca 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  ;../*.** Impleme
34dcb 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
34dcc 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66  calar function f
34dcd 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e  ts3_tokenizer_in
34dce 74 65 72 6e 61 6c 5f 74 65 73 74 28 29 2e 0a 2a  ternal_test()..*
34dcf 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
34dd0 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
34dd1 69 6e 67 20 6f 6e 6c 79 2c 20 69 74 20 69 73 20  ing only, it is 
34dd2 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  not included in 
34dd3 74 68 65 0a 2a 2a 20 62 75 69 6c 64 20 75 6e 6c  the.** build unl
34dd4 65 73 73 20 53 51 4c 49 54 45 5f 54 45 53 54 20  ess SQLITE_TEST 
34dd5 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a  is defined..**.*
34dd6 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66  * The purpose of
34dd7 20 74 68 69 73 20 69 73 20 74 6f 20 74 65 73 74   this is to test
34dd8 20 74 68 61 74 20 74 68 65 20 66 74 73 33 5f 74   that the fts3_t
34dd9 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74  okenizer() funct
34dda 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73  ion.** can be us
34ddb 65 64 20 61 73 20 64 65 73 69 67 6e 65 64 20 62  ed as designed b
34ddc 79 20 74 68 65 20 43 2d 63 6f 64 65 20 69 6e 20  y the C-code in 
34ddd 74 68 65 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  the queryTokeniz
34dde 65 72 20 61 6e 64 0a 2a 2a 20 72 65 67 69 73 74  er and.** regist
34ddf 65 72 54 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75  erTokenizer() fu
34de0 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 2e 20 54  nctions above. T
34de1 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f  hese two functio
34de2 6e 73 20 61 72 65 20 72 65 70 65 61 74 65 64 0a  ns are repeated.
34de3 2a 2a 20 69 6e 20 74 68 65 20 52 45 41 44 4d 45  ** in the README
34de4 2e 74 6f 6b 65 6e 69 7a 65 72 20 66 69 6c 65 20  .tokenizer file 
34de5 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 20 73  as an example, s
34de6 6f 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  o it is importan
34de7 74 20 74 6f 0a 2a 2a 20 74 65 73 74 20 74 68 65  t to.** test the
34de8 6d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 75 6e 20  m..**.** To run 
34de9 74 68 65 20 74 65 73 74 73 2c 20 65 76 61 6c 75  the tests, evalu
34dea 61 74 65 20 74 68 65 20 66 74 73 33 5f 74 6f 6b  ate the fts3_tok
34deb 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f  enizer_internal_
34dec 74 65 73 74 28 29 20 73 63 61 6c 61 72 0a 2a 2a  test() scalar.**
34ded 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e   function with n
34dee 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20 41 6e 20  o arguments. An 
34def 61 73 73 65 72 74 28 29 20 77 69 6c 6c 20 66 61  assert() will fa
34df0 69 6c 20 69 66 20 61 20 70 72 6f 62 6c 65 6d 20  il if a problem 
34df1 69 73 0a 2a 2a 20 64 65 74 65 63 74 65 64 2e 20  is.** detected. 
34df2 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  i.e.:.**.**     
34df3 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65  SELECT fts3_toke
34df4 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74  nizer_internal_t
34df5 65 73 74 28 29 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61  est();.**.*/.sta
34df6 74 69 63 20 76 6f 69 64 20 69 6e 74 54 65 73 74  tic void intTest
34df7 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
34df8 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
34df9 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
34dfa 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
34dfb 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 72 63  argv.){.  int rc
34dfc 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
34dfd 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
34dfe 6c 65 20 2a 70 31 3b 0a 20 20 63 6f 6e 73 74 20  le *p1;.  const 
34dff 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34e00 72 5f 6d 6f 64 75 6c 65 20 2a 70 32 3b 0a 20 20  r_module *p2;.  
34e01 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73  sqlite3 *db = (s
34e02 71 6c 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33  qlite3 *)sqlite3
34e03 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
34e04 78 74 29 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20  xt);..  /* Test 
34e05 74 68 65 20 71 75 65 72 79 20 66 75 6e 63 74 69  the query functi
34e06 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46  on */.  sqlite3F
34e07 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  ts3SimpleTokeniz
34e08 65 72 4d 6f 64 75 6c 65 28 26 70 31 29 3b 0a 20  erModule(&p1);. 
34e09 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e   rc = queryToken
34e0a 69 7a 65 72 28 64 62 2c 20 22 73 69 6d 70 6c 65  izer(db, "simple
34e0b 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72  ", &p2);.  asser
34e0c 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
34e0d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31   );.  assert( p1
34e0e 3d 3d 70 32 20 29 3b 0a 20 20 72 63 20 3d 20 71  ==p2 );.  rc = q
34e0f 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62  ueryTokenizer(db
34e10 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a  , "nosuchtokeniz
34e11 65 72 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73  er", &p2);.  ass
34e12 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
34e13 45 52 52 4f 52 20 29 3b 0a 20 20 61 73 73 65 72  ERROR );.  asser
34e14 74 28 20 70 32 3d 3d 30 20 29 3b 0a 20 20 61 73  t( p2==0 );.  as
34e15 73 65 72 74 28 20 30 3d 3d 73 74 72 63 6d 70 28  sert( 0==strcmp(
34e16 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
34e17 62 29 2c 20 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b  b), "unknown tok
34e18 65 6e 69 7a 65 72 3a 20 6e 6f 73 75 63 68 74 6f  enizer: nosuchto
34e19 6b 65 6e 69 7a 65 72 22 29 20 29 3b 0a 0a 20 20  kenizer") );..  
34e1a 2f 2a 20 54 65 73 74 20 74 68 65 20 73 74 6f 72  /* Test the stor
34e1b 61 67 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  age function */.
34e1c 20 20 72 63 20 3d 20 72 65 67 69 73 74 65 72 54    rc = registerT
34e1d 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f  okenizer(db, "no
34e1e 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20  suchtokenizer", 
34e1f 70 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  p1);.  assert( r
34e20 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
34e21 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65    rc = queryToke
34e22 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63  nizer(db, "nosuc
34e23 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32  htokenizer", &p2
34e24 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
34e25 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
34e26 61 73 73 65 72 74 28 20 70 32 3d 3d 70 31 20 29  assert( p2==p1 )
34e27 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  ;..  sqlite3_res
34e28 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
34e29 2c 20 22 6f 6b 22 2c 20 2d 31 2c 20 53 51 4c 49  , "ok", -1, SQLI
34e2a 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 23  TE_STATIC);.}..#
34e2b 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  endif../*.** Set
34e2c 20 75 70 20 53 51 4c 20 6f 62 6a 65 63 74 73 20   up SQL objects 
34e2d 69 6e 20 64 61 74 61 62 61 73 65 20 64 62 20 75  in database db u
34e2e 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
34e2f 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a  e contents of.**
34e30 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
34e31 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72  pointed to by ar
34e32 67 75 6d 65 6e 74 20 70 48 61 73 68 2e 20 54 68  gument pHash. Th
34e33 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 75 73  e hash table mus
34e34 74 0a 2a 2a 20 62 65 65 6e 20 69 6e 69 74 69 61  t.** been initia
34e35 6c 69 73 65 64 20 74 6f 20 75 73 65 20 73 74 72  lised to use str
34e36 69 6e 67 20 6b 65 79 73 2c 20 61 6e 64 20 74 6f  ing keys, and to
34e37 20 74 61 6b 65 20 61 20 70 72 69 76 61 74 65 20   take a private 
34e38 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74 68 65 20  copy .** of the 
34e39 6b 65 79 20 77 68 65 6e 20 61 20 76 61 6c 75 65  key when a value
34e3a 20 69 73 20 69 6e 73 65 72 74 65 64 2e 20 69 2e   is inserted. i.
34e3b 65 2e 20 62 79 20 61 20 63 61 6c 6c 20 73 69 6d  e. by a call sim
34e3c 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20  ilar to:.**.**  
34e3d 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73    sqlite3Fts3Has
34e3e 68 49 6e 69 74 28 70 48 61 73 68 2c 20 46 54 53  hInit(pHash, FTS
34e3f 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31  3_HASH_STRING, 1
34e40 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  );.**.** This fu
34e41 6e 63 74 69 6f 6e 20 61 64 64 73 20 61 20 73 63  nction adds a sc
34e42 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 28 73  alar function (s
34e43 65 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  ee header commen
34e44 74 20 61 62 6f 76 65 0a 2a 2a 20 73 63 61 6c 61  t above.** scala
34e45 72 46 75 6e 63 28 29 20 69 6e 20 74 68 69 73 20  rFunc() in this 
34e46 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73  file for details
34e47 29 20 61 6e 64 2c 20 69 66 20 45 4e 41 42 4c 45  ) and, if ENABLE
34e48 5f 54 41 42 4c 45 20 69 73 0a 2a 2a 20 64 65 66  _TABLE is.** def
34e49 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 61 74  ined at compilat
34e4a 69 6f 6e 20 74 69 6d 65 2c 20 61 20 74 65 6d 70  ion time, a temp
34e4b 6f 72 61 72 79 20 76 69 72 74 75 61 6c 20 74 61  orary virtual ta
34e4c 62 6c 65 20 28 73 65 65 20 68 65 61 64 65 72 20  ble (see header 
34e4d 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76  .** comment abov
34e4e 65 20 73 74 72 75 63 74 20 48 61 73 68 54 61 62  e struct HashTab
34e4f 6c 65 56 74 61 62 29 20 74 6f 20 74 68 65 20 64  leVtab) to the d
34e50 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
34e51 42 6f 74 68 20 0a 2a 2a 20 70 72 6f 76 69 64 65  Both .** provide
34e52 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65   read/write acce
34e53 73 73 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e  ss to the conten
34e54 74 73 20 6f 66 20 2a 70 48 61 73 68 2e 0a 2a 2a  ts of *pHash..**
34e55 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72  .** The third ar
34e56 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
34e57 75 6e 63 74 69 6f 6e 2c 20 7a 4e 61 6d 65 2c 20  unction, zName, 
34e58 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 6e  is used as the n
34e59 61 6d 65 0a 2a 2a 20 6f 66 20 62 6f 74 68 20 74  ame.** of both t
34e5a 68 65 20 73 63 61 6c 61 72 20 61 6e 64 2c 20 69  he scalar and, i
34e5b 66 20 63 72 65 61 74 65 64 2c 20 74 68 65 20 76  f created, the v
34e5c 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  irtual table..*/
34e5d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
34e5e 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49  int sqlite3Fts3I
34e5f 6e 69 74 48 61 73 68 54 61 62 6c 65 28 0a 20 20  nitHashTable(.  
34e60 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
34e61 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c  fts3Hash *pHash,
34e62 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
34e63 7a 4e 61 6d 65 0a 29 7b 0a 20 20 69 6e 74 20 72  zName.){.  int r
34e64 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
34e65 20 76 6f 69 64 20 2a 70 20 3d 20 28 76 6f 69 64   void *p = (void
34e66 20 2a 29 70 48 61 73 68 3b 0a 20 20 63 6f 6e 73   *)pHash;.  cons
34e67 74 20 69 6e 74 20 61 6e 79 20 3d 20 53 51 4c 49  t int any = SQLI
34e68 54 45 5f 41 4e 59 3b 0a 20 20 63 68 61 72 20 2a  TE_ANY;.  char *
34e69 7a 54 65 73 74 20 3d 20 30 3b 0a 20 20 63 68 61  zTest = 0;.  cha
34e6a 72 20 2a 7a 54 65 73 74 32 20 3d 20 30 3b 0a 0a  r *zTest2 = 0;..
34e6b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
34e6c 53 54 0a 20 20 76 6f 69 64 20 2a 70 64 62 20 3d  ST.  void *pdb =
34e6d 20 28 76 6f 69 64 20 2a 29 64 62 3b 0a 20 20 7a   (void *)db;.  z
34e6e 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Test = sqlite3_m
34e6f 70 72 69 6e 74 66 28 22 25 73 5f 74 65 73 74 22  printf("%s_test"
34e70 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7a 54 65 73  , zName);.  zTes
34e71 74 32 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  t2 = sqlite3_mpr
34e72 69 6e 74 66 28 22 25 73 5f 69 6e 74 65 72 6e 61  intf("%s_interna
34e73 6c 5f 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b  l_test", zName);
34e74 0a 20 20 69 66 28 20 21 7a 54 65 73 74 20 7c 7c  .  if( !zTest ||
34e75 20 21 7a 54 65 73 74 32 20 29 7b 0a 20 20 20 20   !zTest2 ){.    
34e76 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
34e77 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  M;.  }.#endif.. 
34e78 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
34e79 4f 4b 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73  OK.   || (rc = s
34e7a 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
34e7b 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65  nction(db, zName
34e7c 2c 20 31 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61  , 1, any, p, sca
34e7d 6c 61 72 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a  larFunc, 0, 0)).
34e7e 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
34e7f 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
34e80 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 32  ion(db, zName, 2
34e81 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72  , any, p, scalar
34e82 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 23 69 66  Func, 0, 0)).#if
34e83 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
34e84 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69     || (rc = sqli
34e85 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
34e86 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c 20 32  ion(db, zTest, 2
34e87 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74 46 75  , any, p, testFu
34e88 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c  nc, 0, 0)).   ||
34e89 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
34e8a 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
34e8b 62 2c 20 7a 54 65 73 74 2c 20 33 2c 20 61 6e 79  b, zTest, 3, any
34e8c 2c 20 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30  , p, testFunc, 0
34e8d 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20  , 0)).   || (rc 
34e8e 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
34e8f 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54  _function(db, zT
34e90 65 73 74 32 2c 20 30 2c 20 61 6e 79 2c 20 70 64  est2, 0, any, pd
34e91 62 2c 20 69 6e 74 54 65 73 74 46 75 6e 63 2c 20  b, intTestFunc, 
34e92 30 2c 20 30 29 29 0a 23 65 6e 64 69 66 0a 20 20  0, 0)).#endif.  
34e93 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  );..  sqlite3_fr
34e94 65 65 28 7a 54 65 73 74 29 3b 0a 20 20 73 71 6c  ee(zTest);.  sql
34e95 69 74 65 33 5f 66 72 65 65 28 7a 54 65 73 74 32  ite3_free(zTest2
34e96 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
34e97 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
34e98 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
34e99 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
34e9a 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
34e9b 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
34e9c 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
34e9d 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a  3_tokenizer.c **
34e9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34ea0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
34ea1 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
34ea2 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31   fts3_tokenizer1
34ea3 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
34ea4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34ea5 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
34ea6 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68   Oct 10.**.** Th
34ea7 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
34ea8 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
34ea9 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
34eaa 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
34eab 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
34eac 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
34ead 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
34eae 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
34eaf 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
34eb0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
34eb1 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
34eb2 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
34eb3 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
34eb4 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
34eb5 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
34eb6 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
34eb7 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
34eb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34eb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34eba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34ebb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34ebc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
34ebd 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
34ebe 20 6f 66 20 74 68 65 20 22 73 69 6d 70 6c 65 22   of the "simple"
34ebf 20 66 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63   full-text-searc
34ec0 68 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a  h tokenizer..*/.
34ec1 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  ./*.** The code 
34ec2 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
34ec3 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66  only compiled if
34ec4 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  :.**.**     * Th
34ec5 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
34ec6 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20   being built as 
34ec7 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20  an extension.** 
34ec8 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20        (in which 
34ec9 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  case SQLITE_CORE
34eca 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
34ecb 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  , or.**.**     *
34ecc 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
34ecd 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
34ece 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66  into the core of
34ecf 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65  .**       SQLite
34ed0 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
34ed1 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
34ed2 53 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a  S3 is defined)..
34ed3 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
34ed4 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
34ed5 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
34ed6 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a  NABLE_FTS3).....
34ed7 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
34ed8 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
34ed9 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
34eda 6e 69 7a 65 72 20 62 61 73 65 3b 0a 20 20 63 68  nizer base;.  ch
34edb 61 72 20 64 65 6c 69 6d 5b 31 32 38 5d 3b 20 20  ar delim[128];  
34edc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c             /* fl
34edd 61 67 20 41 53 43 49 49 20 64 65 6c 69 6d 69 74  ag ASCII delimit
34ede 65 72 73 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f  ers */.} simple_
34edf 74 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 74 79 70 65  tokenizer;..type
34ee0 64 65 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c  def struct simpl
34ee1 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  e_tokenizer_curs
34ee2 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
34ee3 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
34ee4 62 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68  base;.  const ch
34ee5 61 72 20 2a 70 49 6e 70 75 74 3b 20 20 20 20 20  ar *pInput;     
34ee6 20 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65       /* input we
34ee7 20 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20   are tokenizing 
34ee8 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b  */.  int nBytes;
34ee9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34eea 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65    /* size of the
34eeb 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20   input */.  int 
34eec 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  iOffset;        
34eed 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72           /* curr
34eee 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20  ent position in 
34eef 70 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  pInput */.  int 
34ef0 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  iToken;         
34ef1 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65           /* inde
34ef2 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20  x of next token 
34ef3 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a  to be returned *
34ef4 2f 0a 20 20 63 68 61 72 20 2a 70 54 6f 6b 65 6e  /.  char *pToken
34ef5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
34ef6 20 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20   /* storage for 
34ef7 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  current token */
34ef8 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 41 6c 6c  .  int nTokenAll
34ef9 6f 63 61 74 65 64 3b 20 20 20 20 20 20 20 20 20  ocated;         
34efa 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  /* space allocat
34efb 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66  ed to zToken buf
34efc 66 65 72 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f  fer */.} simple_
34efd 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
34efe 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  ;.../* Forward d
34eff 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74  eclaration */.st
34f00 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
34f01 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
34f02 75 6c 65 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69  ule simpleTokeni
34f03 7a 65 72 4d 6f 64 75 6c 65 3b 0a 0a 73 74 61 74  zerModule;..stat
34f04 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 6c  ic int simpleDel
34f05 69 6d 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  im(simple_tokeni
34f06 7a 65 72 20 2a 74 2c 20 75 6e 73 69 67 6e 65 64  zer *t, unsigned
34f07 20 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75   char c){.  retu
34f08 72 6e 20 63 3c 30 78 38 30 20 26 26 20 74 2d 3e  rn c<0x80 && t->
34f09 64 65 6c 69 6d 5b 63 5d 3b 0a 7d 0a 0a 2f 2a 0a  delim[c];.}../*.
34f0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
34f0b 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e  tokenizer instan
34f0c 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
34f0d 74 20 73 69 6d 70 6c 65 43 72 65 61 74 65 28 0a  t simpleCreate(.
34f0e 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
34f0f 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
34f10 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argv,.  sqlite3_
34f11 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f  tokenizer **ppTo
34f12 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 73 69 6d  kenizer.){.  sim
34f13 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74  ple_tokenizer *t
34f14 3b 0a 0a 20 20 74 20 3d 20 28 73 69 6d 70 6c 65  ;..  t = (simple
34f15 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71  _tokenizer *) sq
34f16 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
34f17 65 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66 28 20  eof(*t));.  if( 
34f18 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  t==NULL ) return
34f19 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
34f1a 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73 69   memset(t, 0, si
34f1b 7a 65 6f 66 28 2a 74 29 29 3b 0a 0a 20 20 2f 2a  zeof(*t));..  /*
34f1c 20 54 4f 44 4f 28 73 68 65 73 73 29 20 44 65 6c   TODO(shess) Del
34f1d 69 6d 69 74 65 72 73 20 6e 65 65 64 20 74 6f 20  imiters need to 
34f1e 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20  remain the same 
34f1f 66 72 6f 6d 20 72 75 6e 20 74 6f 20 72 75 6e 2c  from run to run,
34f20 0a 20 20 2a 2a 20 65 6c 73 65 20 77 65 20 6e 65  .  ** else we ne
34f21 65 64 20 74 6f 20 72 65 69 6e 64 65 78 2e 20 20  ed to reindex.  
34f22 4f 6e 65 20 73 6f 6c 75 74 69 6f 6e 20 77 6f 75  One solution wou
34f23 6c 64 20 62 65 20 61 20 6d 65 74 61 2d 74 61 62  ld be a meta-tab
34f24 6c 65 20 74 6f 0a 20 20 2a 2a 20 74 72 61 63 6b  le to.  ** track
34f25 20 73 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f   such informatio
34f26 6e 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  n in the databas
34f27 65 2c 20 74 68 65 6e 20 77 65 27 64 20 6f 6e 6c  e, then we'd onl
34f28 79 20 77 61 6e 74 20 74 68 69 73 0a 20 20 2a 2a  y want this.  **
34f29 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20   information on 
34f2a 74 68 65 20 69 6e 69 74 69 61 6c 20 63 72 65 61  the initial crea
34f2b 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61  te..  */.  if( a
34f2c 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74  rgc>1 ){.    int
34f2d 20 69 2c 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61   i, n = strlen(a
34f2e 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 66 6f 72  rgv[1]);.    for
34f2f 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b  (i=0; i<n; i++){
34f30 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
34f31 63 68 61 72 20 63 68 20 3d 20 61 72 67 76 5b 31  char ch = argv[1
34f32 5d 5b 69 5d 3b 0a 20 20 20 20 20 20 2f 2a 20 57  ][i];.      /* W
34f33 65 20 65 78 70 6c 69 63 69 74 6c 79 20 64 6f 6e  e explicitly don
34f34 27 74 20 73 75 70 70 6f 72 74 20 55 54 46 2d 38  't support UTF-8
34f35 20 64 65 6c 69 6d 69 74 65 72 73 20 66 6f 72 20   delimiters for 
34f36 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  now. */.      if
34f37 28 20 63 68 3e 3d 30 78 38 30 20 29 7b 0a 20 20  ( ch>=0x80 ){.  
34f38 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
34f39 65 65 28 74 29 3b 0a 20 20 20 20 20 20 20 20 72  ee(t);.        r
34f3a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
34f3b 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OR;.      }.    
34f3c 20 20 74 2d 3e 64 65 6c 69 6d 5b 63 68 5d 20 3d    t->delim[ch] =
34f3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c   1;.    }.  } el
34f3e 73 65 20 7b 0a 20 20 20 20 2f 2a 20 4d 61 72 6b  se {.    /* Mark
34f3f 20 6e 6f 6e 2d 61 6c 70 68 61 6e 75 6d 65 72 69   non-alphanumeri
34f40 63 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65  c ASCII characte
34f41 72 73 20 61 73 20 64 65 6c 69 6d 69 74 65 72 73  rs as delimiters
34f42 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   */.    int i;. 
34f43 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 30 78     for(i=1; i<0x
34f44 38 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  80; i++){.      
34f45 74 2d 3e 64 65 6c 69 6d 5b 69 5d 20 3d 20 21 69  t->delim[i] = !i
34f46 73 61 6c 6e 75 6d 28 69 29 3b 0a 20 20 20 20 7d  salnum(i);.    }
34f47 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  .  }..  *ppToken
34f48 69 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b  izer = &t->base;
34f49 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
34f4a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
34f4b 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
34f4c 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  r.*/.static int 
34f4d 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 28 73 71  simpleDestroy(sq
34f4e 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
34f4f 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20  *pTokenizer){.  
34f50 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f  sqlite3_free(pTo
34f51 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75  kenizer);.  retu
34f52 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
34f53 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74  ./*.** Prepare t
34f54 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69  o begin tokenizi
34f55 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
34f56 73 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70  string.  The inp
34f57 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20  ut.** string to 
34f58 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20  be tokenized is 
34f59 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73  pInput[0..nBytes
34f5a 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a  -1].  A cursor.*
34f5b 2a 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d  * used to increm
34f5c 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65  entally tokenize
34f5d 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20   this string is 
34f5e 72 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20  returned in .** 
34f5f 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74  *ppCursor..*/.st
34f60 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4f  atic int simpleO
34f61 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
34f62 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
34f63 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
34f64 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
34f65 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
34f66 70 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74  pInput, int nByt
34f67 65 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74  es,        /* St
34f68 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e  ring to be token
34f69 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ized */.  sqlite
34f6a 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
34f6b 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20  or **ppCursor   
34f6c 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a   /* OUT: Tokeniz
34f6d 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a  ation cursor */.
34f6e 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ){.  simple_toke
34f6f 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b  nizer_cursor *c;
34f70 0a 0a 20 20 63 20 3d 20 28 73 69 6d 70 6c 65 5f  ..  c = (simple_
34f71 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
34f72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   *) sqlite3_mall
34f73 6f 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a  oc(sizeof(*c));.
34f74 20 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20    if( c==NULL ) 
34f75 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
34f76 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 70 49 6e 70 75  MEM;..  c->pInpu
34f77 74 20 3d 20 70 49 6e 70 75 74 3b 0a 20 20 69 66  t = pInput;.  if
34f78 28 20 70 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20  ( pInput==0 ){. 
34f79 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 30     c->nBytes = 0
34f7a 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 42  ;.  }else if( nB
34f7b 79 74 65 73 3c 30 20 29 7b 0a 20 20 20 20 63 2d  ytes<0 ){.    c-
34f7c 3e 6e 42 79 74 65 73 20 3d 20 28 69 6e 74 29 73  >nBytes = (int)s
34f7d 74 72 6c 65 6e 28 70 49 6e 70 75 74 29 3b 0a 20  trlen(pInput);. 
34f7e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e   }else{.    c->n
34f7f 42 79 74 65 73 20 3d 20 6e 42 79 74 65 73 3b 0a  Bytes = nBytes;.
34f80 20 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74    }.  c->iOffset
34f81 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
34f82 20 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20 74        /* start t
34f83 6f 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68 65  okenizing at the
34f84 20 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20   beginning */.  
34f85 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20  c->iToken = 0;. 
34f86 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 4e 55 4c   c->pToken = NUL
34f87 4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L;              
34f88 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c   /* no space all
34f89 6f 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a  ocated, yet. */.
34f8a 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63    c->nTokenAlloc
34f8b 61 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70  ated = 0;..  *pp
34f8c 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73  Cursor = &c->bas
34f8d 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  e;.  return SQLI
34f8e 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
34f8f 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61  Close a tokeniza
34f90 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76  tion cursor prev
34f91 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79  iously opened by
34f92 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 69   a call to.** si
34f93 6d 70 6c 65 4f 70 65 6e 28 29 20 61 62 6f 76 65  mpleOpen() above
34f94 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
34f95 73 69 6d 70 6c 65 43 6c 6f 73 65 28 73 71 6c 69  simpleClose(sqli
34f96 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
34f97 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a  rsor *pCursor){.
34f98 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a    simple_tokeniz
34f99 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  er_cursor *c = (
34f9a 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
34f9b 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  _cursor *) pCurs
34f9c 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  or;.  sqlite3_fr
34f9d 65 65 28 63 2d 3e 70 54 6f 6b 65 6e 29 3b 0a 20  ee(c->pToken);. 
34f9e 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29   sqlite3_free(c)
34f9f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
34fa0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  E_OK;.}../*.** E
34fa1 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20  xtract the next 
34fa2 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b  token from a tok
34fa3 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
34fa4 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75  .  The cursor mu
34fa5 73 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20  st.** have been 
34fa6 6f 70 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f  opened by a prio
34fa7 72 20 63 61 6c 6c 20 74 6f 20 73 69 6d 70 6c 65  r call to simple
34fa8 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Open()..*/.stati
34fa9 63 20 69 6e 74 20 73 69 6d 70 6c 65 4e 65 78 74  c int simpleNext
34faa 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
34fab 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
34fac 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f  ursor,  /* Curso
34fad 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 69  r returned by si
34fae 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f  mpleOpen */.  co
34faf 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b  nst char **ppTok
34fb0 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  en,             
34fb1 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f 6b    /* OUT: *ppTok
34fb2 65 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20  en is the token 
34fb3 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  text */.  int *p
34fb4 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
34fb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34fb6 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
34fb7 62 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a  bytes in token *
34fb8 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74  /.  int *piStart
34fb9 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
34fba 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
34fbb 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20  Starting offset 
34fbc 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  of token */.  in
34fbd 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20  t *piEndOffset, 
34fbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fbf 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67    /* OUT: Ending
34fc0 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
34fc1 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73   */.  int *piPos
34fc2 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  ition           
34fc3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
34fc4 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67  : Position integ
34fc5 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29  er of token */.)
34fc6 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  {.  simple_token
34fc7 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d  izer_cursor *c =
34fc8 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a   (simple_tokeniz
34fc9 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75  er_cursor *) pCu
34fca 72 73 6f 72 3b 0a 20 20 73 69 6d 70 6c 65 5f 74  rsor;.  simple_t
34fcb 6f 6b 65 6e 69 7a 65 72 20 2a 74 20 3d 20 28 73  okenizer *t = (s
34fcc 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
34fcd 2a 29 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b  *) pCursor->pTok
34fce 65 6e 69 7a 65 72 3b 0a 20 20 75 6e 73 69 67 6e  enizer;.  unsign
34fcf 65 64 20 63 68 61 72 20 2a 70 20 3d 20 28 75 6e  ed char *p = (un
34fd0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 63 2d  signed char *)c-
34fd1 3e 70 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c  >pInput;..  whil
34fd2 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
34fd3 3e 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 69  >nBytes ){.    i
34fd4 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b  nt iStartOffset;
34fd5 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 70 61  ..    /* Scan pa
34fd6 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63 68 61  st delimiter cha
34fd7 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 20 20 77  racters */.    w
34fd8 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74  hile( c->iOffset
34fd9 3c 63 2d 3e 6e 42 79 74 65 73 20 26 26 20 73 69  <c->nBytes && si
34fda 6d 70 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63  mpleDelim(t, p[c
34fdb 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20  ->iOffset]) ){. 
34fdc 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b       c->iOffset+
34fdd 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  +;.    }..    /*
34fde 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d   Count non-delim
34fdf 69 74 65 72 20 63 68 61 72 61 63 74 65 72 73 2e  iter characters.
34fe0 20 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f 66   */.    iStartOf
34fe1 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65  fset = c->iOffse
34fe2 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d  t;.    while( c-
34fe3 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74  >iOffset<c->nByt
34fe4 65 73 20 26 26 20 21 73 69 6d 70 6c 65 44 65 6c  es && !simpleDel
34fe5 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73  im(t, p[c->iOffs
34fe6 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 2d  et]) ){.      c-
34fe7 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20  >iOffset++;.    
34fe8 7d 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 69 4f  }..    if( c->iO
34fe9 66 66 73 65 74 3e 69 53 74 61 72 74 4f 66 66 73  ffset>iStartOffs
34fea 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  et ){.      int 
34feb 69 2c 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73 65  i, n = c->iOffse
34fec 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  t-iStartOffset;.
34fed 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e 6e        if( n>c->n
34fee 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 29  TokenAllocated )
34fef 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 54 6f  {.        c->nTo
34ff0 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e  kenAllocated = n
34ff1 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e  +20;.        c->
34ff2 70 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  pToken = sqlite3
34ff3 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 70 54 6f 6b  _realloc(c->pTok
34ff4 65 6e 2c 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c  en, c->nTokenAll
34ff5 6f 63 61 74 65 64 29 3b 0a 20 20 20 20 20 20 20  ocated);.       
34ff6 20 69 66 28 20 63 2d 3e 70 54 6f 6b 65 6e 3d 3d   if( c->pToken==
34ff7 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
34ff8 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
34ff9 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d    }.      for(i=
34ffa 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<n; i++){.  
34ffb 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68        /* TODO(sh
34ffc 65 73 73 29 20 54 68 69 73 20 6e 65 65 64 73 20  ess) This needs 
34ffd 65 78 70 61 6e 73 69 6f 6e 20 74 6f 20 68 61 6e  expansion to han
34ffe 64 6c 65 20 55 54 46 2d 38 0a 20 20 20 20 20 20  dle UTF-8.      
34fff 20 20 2a 2a 20 63 61 73 65 2d 69 6e 73 65 6e 73    ** case-insens
35000 69 74 69 76 69 74 79 2e 0a 20 20 20 20 20 20 20  itivity..       
35001 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 73 69   */.        unsi
35002 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20 70  gned char ch = p
35003 5b 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69 5d  [iStartOffset+i]
35004 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 70 54 6f  ;.        c->pTo
35005 6b 65 6e 5b 69 5d 20 3d 20 63 68 3c 30 78 38 30  ken[i] = ch<0x80
35006 20 3f 20 74 6f 6c 6f 77 65 72 28 63 68 29 20 3a   ? tolower(ch) :
35007 20 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   ch;.      }.   
35008 20 20 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 63 2d     *ppToken = c-
35009 3e 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 2a  >pToken;.      *
3500a 70 6e 42 79 74 65 73 20 3d 20 6e 3b 0a 20 20 20  pnBytes = n;.   
3500b 20 20 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65     *piStartOffse
3500c 74 20 3d 20 69 53 74 61 72 74 4f 66 66 73 65 74  t = iStartOffset
3500d 3b 0a 20 20 20 20 20 20 2a 70 69 45 6e 64 4f 66  ;.      *piEndOf
3500e 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65  fset = c->iOffse
3500f 74 3b 0a 20 20 20 20 20 20 2a 70 69 50 6f 73 69  t;.      *piPosi
35010 74 69 6f 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e  tion = c->iToken
35011 2b 2b 3b 0a 0a 20 20 20 20 20 20 72 65 74 75 72  ++;..      retur
35012 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
35013 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
35014 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a  SQLITE_DONE;.}..
35015 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66  /*.** The set of
35016 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69   routines that i
35017 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 69 6d  mplement the sim
35018 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f  ple tokenizer.*/
35019 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
3501a 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3501b 6d 6f 64 75 6c 65 20 73 69 6d 70 6c 65 54 6f 6b  module simpleTok
3501c 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b  enizerModule = {
3501d 0a 20 20 30 2c 0a 20 20 73 69 6d 70 6c 65 43 72  .  0,.  simpleCr
3501e 65 61 74 65 2c 0a 20 20 73 69 6d 70 6c 65 44 65  eate,.  simpleDe
3501f 73 74 72 6f 79 2c 0a 20 20 73 69 6d 70 6c 65 4f  stroy,.  simpleO
35020 70 65 6e 2c 0a 20 20 73 69 6d 70 6c 65 43 6c 6f  pen,.  simpleClo
35021 73 65 2c 0a 20 20 73 69 6d 70 6c 65 4e 65 78 74  se,.  simpleNext
35022 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ,.};../*.** Allo
35023 63 61 74 65 20 61 20 6e 65 77 20 73 69 6d 70 6c  cate a new simpl
35024 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65  e tokenizer.  Re
35025 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
35026 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b  o the new.** tok
35027 65 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64  enizer in *ppMod
35028 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ule.*/.SQLITE_PR
35029 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
3502a 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
3502b 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73  nizerModule(.  s
3502c 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3502d 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
3502e 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70  pModule.){.  *pp
3502f 4d 6f 64 75 6c 65 20 3d 20 26 73 69 6d 70 6c 65  Module = &simple
35030 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b  TokenizerModule;
35031 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  .}..#endif /* !d
35032 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
35033 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
35034 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
35035 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  3) */../********
35036 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
35037 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20  s3_tokenizer1.c 
35038 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35039 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3503a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
3503b 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
3503c 65 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a  e rtree.c ******
3503d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3503e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3503f 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
35040 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
35041 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
35042 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
35043 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
35044 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
35045 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
35046 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
35047 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
35048 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
35049 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
3504a 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
3504b 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
3504c 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
3504d 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
3504e 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
3504f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
35050 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
35051 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
35052 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
35053 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35054 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35055 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35056 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
35057 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
35058 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 69 6d 70  ins code for imp
35059 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
3505a 74 68 65 20 72 2d 74 72 65 65 20 61 6e 64 20 72  the r-tree and r
3505b 2a 2d 74 72 65 65 0a 2a 2a 20 61 6c 67 6f 72 69  *-tree.** algori
3505c 74 68 6d 73 20 70 61 63 6b 61 67 65 64 20 61 73  thms packaged as
3505d 20 61 6e 20 53 51 4c 69 74 65 20 76 69 72 74 75   an SQLite virtu
3505e 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e  al table module.
3505f 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 74 72 65  .**.** $Id: rtre
35060 65 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 38 2f  e.c,v 1.12 2008/
35061 31 32 2f 32 32 20 31 35 3a 30 34 3a 33 32 20 64  12/22 15:04:32 d
35062 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
35063 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  .*/..#if !define
35064 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
35065 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
35066 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 29 0a 0a  _ENABLE_RTREE)..
35067 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  /*.** This file 
35068 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6d 70 6c  contains an impl
35069 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20  ementation of a 
3506a 63 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72  couple of differ
3506b 65 6e 74 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20  ent variants.** 
3506c 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 61 6c  of the r-tree al
3506d 67 6f 72 69 74 68 6d 2e 20 53 65 65 20 74 68 65  gorithm. See the
3506e 20 52 45 41 44 4d 45 20 66 69 6c 65 20 66 6f 72   README file for
3506f 20 66 75 72 74 68 65 72 20 64 65 74 61 69 6c 73   further details
35070 2e 20 54 68 65 20 0a 2a 2a 20 73 61 6d 65 20 64  . The .** same d
35071 61 74 61 2d 73 74 72 75 63 74 75 72 65 20 69 73  ata-structure is
35072 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 2c 20 62   used for all, b
35073 75 74 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d  ut the algorithm
35074 73 20 66 6f 72 20 69 6e 73 65 72 74 20 61 6e 64  s for insert and
35075 0a 2a 2a 20 64 65 6c 65 74 65 20 6f 70 65 72 61  .** delete opera
35076 74 69 6f 6e 73 20 76 61 72 79 2e 20 54 68 65 20  tions vary. The 
35077 76 61 72 69 61 6e 74 73 20 75 73 65 64 20 61 72  variants used ar
35078 65 20 73 65 6c 65 63 74 65 64 20 61 74 20 63 6f  e selected at co
35079 6d 70 69 6c 65 20 74 69 6d 65 20 0a 2a 2a 20 62  mpile time .** b
3507a 79 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 66  y defining the f
3507b 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 73  ollowing symbols
3507c 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69 74 68 65 72 2c  :.*/../* Either,
3507d 20 62 6f 74 68 20 6f 72 20 6e 6f 6e 65 20 6f 66   both or none of
3507e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
3507f 61 79 20 62 65 20 73 65 74 20 74 6f 20 61 63 74  ay be set to act
35080 69 76 61 74 65 20 0a 2a 2a 20 72 2a 74 72 65 65  ivate .** r*tree
35081 20 76 61 72 69 61 6e 74 20 61 6c 67 6f 72 69 74   variant algorit
35082 68 6d 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  hms..*/.#define 
35083 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
35084 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 20  E_CHOOSESUBTREE 
35085 30 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e  0.#define VARIAN
35086 54 5f 52 53 54 41 52 54 52 45 45 5f 52 45 49 4e  T_RSTARTREE_REIN
35087 53 45 52 54 20 20 20 20 20 20 31 0a 0a 2f 2a 20  SERT      1../* 
35088 0a 2a 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20  .** Exactly one 
35089 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
3508a 20 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20   must be set to 
3508b 31 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41  1..*/.#define VA
3508c 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55  RIANT_GUTTMAN_QU
3508d 41 44 52 41 54 49 43 5f 53 50 4c 49 54 20 30 0a  ADRATIC_SPLIT 0.
3508e 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f  #define VARIANT_
3508f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53  GUTTMAN_LINEAR_S
35090 50 4c 49 54 20 20 20 20 30 0a 23 64 65 66 69 6e  PLIT    0.#defin
35091 65 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54  e VARIANT_RSTART
35092 52 45 45 5f 53 50 4c 49 54 20 20 20 20 20 20 20  REE_SPLIT       
35093 20 20 31 0a 0a 23 64 65 66 69 6e 65 20 56 41 52    1..#define VAR
35094 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c  IANT_GUTTMAN_SPL
35095 49 54 20 5c 0a 20 20 20 20 20 20 20 20 28 56 41  IT \.        (VA
35096 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49  RIANT_GUTTMAN_LI
35097 4e 45 41 52 5f 53 50 4c 49 54 7c 7c 56 41 52 49  NEAR_SPLIT||VARI
35098 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44  ANT_GUTTMAN_QUAD
35099 52 41 54 49 43 5f 53 50 4c 49 54 29 0a 0a 23 69  RATIC_SPLIT)..#i
3509a 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
3509b 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49  N_QUADRATIC_SPLI
3509c 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  T.  #define Pick
3509d 4e 65 78 74 20 51 75 61 64 72 61 74 69 63 50 69  Next QuadraticPi
3509e 63 6b 4e 65 78 74 0a 20 20 23 64 65 66 69 6e 65  ckNext.  #define
3509f 20 50 69 63 6b 53 65 65 64 73 20 51 75 61 64 72   PickSeeds Quadr
350a0 61 74 69 63 50 69 63 6b 53 65 65 64 73 0a 20 20  aticPickSeeds.  
350a1 23 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65  #define AssignCe
350a2 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65 47 75 74  lls splitNodeGut
350a3 74 6d 61 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20  tman.#endif.#if 
350a4 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
350a5 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 20 20 23  LINEAR_SPLIT.  #
350a6 64 65 66 69 6e 65 20 50 69 63 6b 4e 65 78 74 20  define PickNext 
350a7 4c 69 6e 65 61 72 50 69 63 6b 4e 65 78 74 0a 20  LinearPickNext. 
350a8 20 23 64 65 66 69 6e 65 20 50 69 63 6b 53 65 65   #define PickSee
350a9 64 73 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65  ds LinearPickSee
350aa 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73  ds.  #define Ass
350ab 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f  ignCells splitNo
350ac 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66  deGuttman.#endif
350ad 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54  .#if VARIANT_RST
350ae 41 52 54 52 45 45 5f 53 50 4c 49 54 0a 20 20 23  ARTREE_SPLIT.  #
350af 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65 6c  define AssignCel
350b0 6c 73 20 73 70 6c 69 74 4e 6f 64 65 53 74 61 72  ls splitNodeStar
350b1 74 72 65 65 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  tree.#endif...#i
350b2 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52  fndef SQLITE_COR
350b3 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  E.  SQLITE_EXTEN
350b4 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65  SION_INIT1.#else
350b5 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65  .#endif...#ifnde
350b6 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
350b7 41 54 49 4f 4e 0a 74 79 70 65 64 65 66 20 73 71  ATION.typedef sq
350b8 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 36 34 3b  lite3_int64 i64;
350b9 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
350ba 64 20 63 68 61 72 20 75 38 3b 0a 74 79 70 65 64  d char u8;.typed
350bb 65 66 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  ef unsigned int 
350bc 75 33 32 3b 0a 23 65 6e 64 69 66 0a 0a 74 79 70  u32;.#endif..typ
350bd 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65  edef struct Rtre
350be 65 20 52 74 72 65 65 3b 0a 74 79 70 65 64 65 66  e Rtree;.typedef
350bf 20 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72   struct RtreeCur
350c0 73 6f 72 20 52 74 72 65 65 43 75 72 73 6f 72 3b  sor RtreeCursor;
350c1 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
350c2 52 74 72 65 65 4e 6f 64 65 20 52 74 72 65 65 4e  RtreeNode RtreeN
350c3 6f 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ode;.typedef str
350c4 75 63 74 20 52 74 72 65 65 43 65 6c 6c 20 52 74  uct RtreeCell Rt
350c5 72 65 65 43 65 6c 6c 3b 0a 74 79 70 65 64 65 66  reeCell;.typedef
350c6 20 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e   struct RtreeCon
350c7 73 74 72 61 69 6e 74 20 52 74 72 65 65 43 6f 6e  straint RtreeCon
350c8 73 74 72 61 69 6e 74 3b 0a 74 79 70 65 64 65 66  straint;.typedef
350c9 20 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72   union RtreeCoor
350ca 64 20 52 74 72 65 65 43 6f 6f 72 64 3b 0a 0a 2f  d RtreeCoord;../
350cb 2a 20 54 68 65 20 72 74 72 65 65 20 6d 61 79 20  * The rtree may 
350cc 68 61 76 65 20 62 65 74 77 65 65 6e 20 31 20 61  have between 1 a
350cd 6e 64 20 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d  nd RTREE_MAX_DIM
350ce 45 4e 53 49 4f 4e 53 20 64 69 6d 65 6e 73 69 6f  ENSIONS dimensio
350cf 6e 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ns. */.#define R
350d0 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49  TREE_MAX_DIMENSI
350d1 4f 4e 53 20 35 0a 0a 2f 2a 20 53 69 7a 65 20 6f  ONS 5../* Size o
350d2 66 20 68 61 73 68 20 74 61 62 6c 65 20 52 74 72  f hash table Rtr
350d3 65 65 2e 61 48 61 73 68 2e 20 54 68 69 73 20 68  ee.aHash. This h
350d4 61 73 68 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  ash table is not
350d5 20 65 78 70 65 63 74 65 64 20 74 6f 0a 2a 2a 20   expected to.** 
350d6 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 76 65 72  ever contain ver
350d7 79 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 2c 20  y many entries, 
350d8 73 6f 20 61 20 66 69 78 65 64 20 6e 75 6d 62 65  so a fixed numbe
350d9 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 73 20  r of buckets is 
350da 0a 2a 2a 20 75 73 65 64 2e 0a 2a 2f 0a 23 64 65  .** used..*/.#de
350db 66 69 6e 65 20 48 41 53 48 53 49 5a 45 20 31 32  fine HASHSIZE 12
350dc 38 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72  8../* .** An rtr
350dd 65 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  ee virtual-table
350de 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75   object..*/.stru
350df 63 74 20 52 74 72 65 65 20 7b 0a 20 20 73 71 6c  ct Rtree {.  sql
350e0 69 74 65 33 5f 76 74 61 62 20 62 61 73 65 3b 0a  ite3_vtab base;.
350e1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
350e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
350e3 20 48 6f 73 74 20 64 61 74 61 62 61 73 65 20 63   Host database c
350e4 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69  onnection */.  i
350e5 6e 74 20 69 4e 6f 64 65 53 69 7a 65 3b 20 20 20  nt iNodeSize;   
350e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
350e7 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 65  ze in bytes of e
350e8 61 63 68 20 6e 6f 64 65 20 69 6e 20 74 68 65 20  ach node in the 
350e9 6e 6f 64 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  node table */.  
350ea 69 6e 74 20 6e 44 69 6d 3b 20 20 20 20 20 20 20  int nDim;       
350eb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
350ec 75 6d 62 65 72 20 6f 66 20 64 69 6d 65 6e 73 69  umber of dimensi
350ed 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  ons */.  int nBy
350ee 74 65 73 50 65 72 43 65 6c 6c 3b 20 20 20 20 20  tesPerCell;     
350ef 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 63 6f       /* Bytes co
350f0 6e 73 75 6d 65 64 20 70 65 72 20 63 65 6c 6c 20  nsumed per cell 
350f1 2a 2f 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b  */.  int iDepth;
350f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
350f3 20 2f 2a 20 43 75 72 72 65 6e 74 20 64 65 70 74   /* Current dept
350f4 68 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  h of the r-tree 
350f5 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63  structure */.  c
350f6 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
350f7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
350f8 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 63  me of database c
350f9 6f 6e 74 61 69 6e 69 6e 67 20 72 2d 74 72 65 65  ontaining r-tree
350fa 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
350fb 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
350fc 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
350fd 6f 66 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20  of r-tree table 
350fe 2a 2f 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  */ .  RtreeNode 
350ff 2a 61 48 61 73 68 5b 48 41 53 48 53 49 5a 45 5d  *aHash[HASHSIZE]
35100 3b 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20  ; /* Hash table 
35101 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64  of in-memory nod
35102 65 73 2e 20 2a 2f 20 0a 20 20 69 6e 74 20 6e 42  es. */ .  int nB
35103 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  usy;            
35104 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
35105 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73   number of users
35106 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
35107 72 65 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 69 73 74  re */..  /* List
35108 20 6f 66 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65   of nodes remove
35109 64 20 64 75 72 69 6e 67 20 61 20 43 6f 6e 64 65  d during a Conde
3510a 6e 73 65 54 72 65 65 20 6f 70 65 72 61 74 69 6f  nseTree operatio
3510b 6e 2e 20 4c 69 73 74 20 69 73 0a 20 20 2a 2a 20  n. List is.  ** 
3510c 6c 69 6e 6b 65 64 20 74 6f 67 65 74 68 65 72 20  linked together 
3510d 76 69 61 20 74 68 65 20 70 6f 69 6e 74 65 72 20  via the pointer 
3510e 6e 6f 72 6d 61 6c 6c 79 20 75 73 65 64 20 66 6f  normally used fo
3510f 72 20 68 61 73 68 20 63 68 61 69 6e 73 20 2d 0a  r hash chains -.
35110 20 20 2a 2a 20 52 74 72 65 65 4e 6f 64 65 2e 70    ** RtreeNode.p
35111 4e 65 78 74 2e 20 52 74 72 65 65 4e 6f 64 65 2e  Next. RtreeNode.
35112 69 4e 6f 64 65 20 73 74 6f 72 65 73 20 74 68 65  iNode stores the
35113 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 73 75   depth of the su
35114 62 2d 74 72 65 65 20 0a 20 20 2a 2a 20 68 65 61  b-tree .  ** hea
35115 64 65 64 20 62 79 20 74 68 65 20 6e 6f 64 65 20  ded by the node 
35116 28 6c 65 61 66 20 6e 6f 64 65 73 20 68 61 76 65  (leaf nodes have
35117 20 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65   RtreeNode.iNode
35118 3d 3d 30 29 2e 0a 20 20 2a 2f 0a 20 20 52 74 72  ==0)..  */.  Rtr
35119 65 65 4e 6f 64 65 20 2a 70 44 65 6c 65 74 65 64  eeNode *pDeleted
3511a 3b 0a 20 20 69 6e 74 20 69 52 65 69 6e 73 65 72  ;.  int iReinser
3511b 74 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  tHeight;        
3511c 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 73 75 62  /* Height of sub
3511d 2d 74 72 65 65 73 20 52 65 69 6e 73 65 72 74 28  -trees Reinsert(
3511e 29 20 68 61 73 20 72 75 6e 20 6f 6e 20 2a 2f 0a  ) has run on */.
3511f 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73  .  /* Statements
35120 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64   to read/write/d
35121 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66  elete a record f
35122 72 6f 6d 20 78 78 78 5f 6e 6f 64 65 20 2a 2f 0a  rom xxx_node */.
35123 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
35124 70 52 65 61 64 4e 6f 64 65 3b 0a 20 20 73 71 6c  pReadNode;.  sql
35125 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74  ite3_stmt *pWrit
35126 65 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 74 65 33  eNode;.  sqlite3
35127 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65 4e 6f  _stmt *pDeleteNo
35128 64 65 3b 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d  de;..  /* Statem
35129 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72 69  ents to read/wri
3512a 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63 6f  te/delete a reco
3512b 72 64 20 66 72 6f 6d 20 78 78 78 5f 72 6f 77 69  rd from xxx_rowi
3512c 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  d */.  sqlite3_s
3512d 74 6d 74 20 2a 70 52 65 61 64 52 6f 77 69 64 3b  tmt *pReadRowid;
3512e 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
3512f 2a 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20  *pWriteRowid;.  
35130 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44  sqlite3_stmt *pD
35131 65 6c 65 74 65 52 6f 77 69 64 3b 0a 0a 20 20 2f  eleteRowid;..  /
35132 2a 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  * Statements to 
35133 72 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74  read/write/delet
35134 65 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  e a record from 
35135 78 78 78 5f 70 61 72 65 6e 74 20 2a 2f 0a 20 20  xxx_parent */.  
35136 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 52  sqlite3_stmt *pR
35137 65 61 64 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c  eadParent;.  sql
35138 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74  ite3_stmt *pWrit
35139 65 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74  eParent;.  sqlit
3513a 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65  e3_stmt *pDelete
3513b 50 61 72 65 6e 74 3b 0a 0a 20 20 69 6e 74 20 65  Parent;..  int e
3513c 43 6f 6f 72 64 54 79 70 65 3b 0a 7d 3b 0a 0a 2f  CoordType;.};../
3513d 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
3513e 73 20 66 6f 72 20 65 43 6f 6f 72 64 54 79 70 65  s for eCoordType
3513f 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54 52  : */.#define RTR
35140 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20  EE_COORD_REAL32 
35141 30 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  0.#define RTREE_
35142 43 4f 4f 52 44 5f 49 4e 54 33 32 20 20 31 0a 0a  COORD_INT32  1..
35143 2f 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75  /*.** The minimu
35144 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  m number of cell
35145 73 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 61 20  s allowed for a 
35146 6e 6f 64 65 20 69 73 20 61 20 74 68 69 72 64 20  node is a third 
35147 6f 66 20 74 68 65 20 0a 2a 2a 20 6d 61 78 69 6d  of the .** maxim
35148 75 6d 2e 20 49 6e 20 47 75 74 6d 61 6e 27 73 20  um. In Gutman's 
35149 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  notation:.**.** 
3514a 20 20 20 20 6d 20 3d 20 4d 2f 33 0a 2a 2a 0a 2a      m = M/3.**.*
3514b 2a 20 49 66 20 61 6e 20 52 2a 2d 74 72 65 65 20  * If an R*-tree 
3514c 22 52 65 69 6e 73 65 72 74 22 20 6f 70 65 72 61  "Reinsert" opera
3514d 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64  tion is required
3514e 2c 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65  , the same numbe
3514f 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 61 72  r of.** cells ar
35150 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  e removed from t
35151 68 65 20 6f 76 65 72 66 75 6c 6c 20 6e 6f 64 65  he overfull node
35152 20 61 6e 64 20 72 65 69 6e 73 65 72 74 65 64 20   and reinserted 
35153 69 6e 74 6f 20 74 68 65 20 74 72 65 65 2e 0a 2a  into the tree..*
35154 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
35155 4d 49 4e 43 45 4c 4c 53 28 70 29 20 28 28 28 28  MINCELLS(p) ((((
35156 70 29 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 34 29  p)->iNodeSize-4)
35157 2f 28 70 29 2d 3e 6e 42 79 74 65 73 50 65 72 43  /(p)->nBytesPerC
35158 65 6c 6c 29 2f 33 29 0a 23 64 65 66 69 6e 65 20  ell)/3).#define 
35159 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 28 70  RTREE_REINSERT(p
3515a 29 20 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53  ) RTREE_MINCELLS
3515b 28 70 29 0a 23 64 65 66 69 6e 65 20 52 54 52 45  (p).#define RTRE
3515c 45 5f 4d 41 58 43 45 4c 4c 53 20 35 31 0a 0a 2f  E_MAXCELLS 51../
3515d 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 63  * .** An rtree c
3515e 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f  ursor object..*/
3515f 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72  .struct RtreeCur
35160 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sor {.  sqlite3_
35161 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65  vtab_cursor base
35162 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ;.  RtreeNode *p
35163 4e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Node;           
35164 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 63 75        /* Node cu
35165 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c  rsor is currentl
35166 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f  y pointing at */
35167 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20  .  int iCell;   
35168 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35169 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
3516a 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e   current cell in
3516b 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20   pNode */.  int 
3516c 69 53 74 72 61 74 65 67 79 3b 20 20 20 20 20 20  iStrategy;      
3516d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3516e 20 43 6f 70 79 20 6f 66 20 69 64 78 4e 75 6d 20   Copy of idxNum 
3516f 73 65 61 72 63 68 20 70 61 72 61 6d 65 74 65 72  search parameter
35170 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74   */.  int nConst
35171 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  raint;          
35172 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
35173 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
35174 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20  aConstraint */. 
35175 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74   RtreeConstraint
35176 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20   *aConstraint;  
35177 20 20 20 2f 2a 20 53 65 61 72 63 68 20 63 6f 6e     /* Search con
35178 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a  straints. */.};.
35179 0a 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72  .union RtreeCoor
3517a 64 20 7b 0a 20 20 66 6c 6f 61 74 20 66 3b 0a 20  d {.  float f;. 
3517b 20 69 6e 74 20 69 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a   int i;.};../*.*
3517c 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
3517d 73 20 61 6e 20 52 74 72 65 65 43 6f 6f 72 64 2e  s an RtreeCoord.
3517e 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75   Return the valu
3517f 65 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20  e stored within 
35180 74 68 65 20 52 74 72 65 65 43 6f 6f 72 64 0a 2a  the RtreeCoord.*
35181 2a 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 61  * formatted as a
35182 20 64 6f 75 62 6c 65 2e 20 54 68 69 73 20 6d 61   double. This ma
35183 63 72 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74  cro assumes that
35184 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20   local variable 
35185 70 52 74 72 65 65 20 70 6f 69 6e 74 73 0a 2a 2a  pRtree points.**
35186 20 74 6f 20 74 68 65 20 52 74 72 65 65 20 73 74   to the Rtree st
35187 72 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74  ructure associat
35188 65 64 20 77 69 74 68 20 74 68 65 20 52 74 72 65  ed with the Rtre
35189 65 43 6f 6f 72 64 2e 0a 2a 2f 0a 23 64 65 66 69  eCoord..*/.#defi
3518a 6e 65 20 44 43 4f 4f 52 44 28 63 6f 6f 72 64 29  ne DCOORD(coord)
3518b 20 28 20 20 20 20 20 20 20 20 20 20 20 20 20 20   (              
3518c 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
3518d 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64   (pRtree->eCoord
3518e 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52  Type==RTREE_COOR
3518f 44 5f 52 45 41 4c 33 32 29 20 3f 20 20 20 20 20  D_REAL32) ?     
35190 20 5c 0a 20 20 20 20 28 28 64 6f 75 62 6c 65 29   \.    ((double)
35191 63 6f 6f 72 64 2e 66 29 20 3a 20 20 20 20 20 20  coord.f) :      
35192 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35193 20 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f 75       \.    ((dou
35194 62 6c 65 29 63 6f 6f 72 64 2e 69 29 20 20 20 20  ble)coord.i)    
35195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35196 20 20 20 20 20 20 20 20 20 5c 0a 29 0a 0a 2f 2a           \.)../*
35197 0a 2a 2a 20 41 20 73 65 61 72 63 68 20 63 6f 6e  .** A search con
35198 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 72 75  straint..*/.stru
35199 63 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  ct RtreeConstrai
3519a 6e 74 20 7b 0a 20 20 69 6e 74 20 69 43 6f 6f 72  nt {.  int iCoor
3519b 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
3519c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
3519d 78 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 65 64  x of constrained
3519e 20 63 6f 6f 72 64 69 6e 61 74 65 20 2a 2f 0a 20   coordinate */. 
3519f 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20   int op;        
351a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
351a1 20 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 69     /* Constraini
351a2 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ng operation */.
351a3 20 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 3b    double rValue;
351a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
351a5 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e      /* Constrain
351a6 74 20 76 61 6c 75 65 2e 20 2a 2f 0a 7d 3b 0a 0a  t value. */.};..
351a7 2f 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75  /* Possible valu
351a8 65 73 20 66 6f 72 20 52 74 72 65 65 43 6f 6e 73  es for RtreeCons
351a9 74 72 61 69 6e 74 2e 6f 70 20 2a 2f 0a 23 64 65  traint.op */.#de
351aa 66 69 6e 65 20 52 54 52 45 45 5f 45 51 20 30 78  fine RTREE_EQ 0x
351ab 34 31 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  41.#define RTREE
351ac 5f 4c 45 20 30 78 34 32 0a 23 64 65 66 69 6e 65  _LE 0x42.#define
351ad 20 52 54 52 45 45 5f 4c 54 20 30 78 34 33 0a 23   RTREE_LT 0x43.#
351ae 64 65 66 69 6e 65 20 52 54 52 45 45 5f 47 45 20  define RTREE_GE 
351af 30 78 34 34 0a 23 64 65 66 69 6e 65 20 52 54 52  0x44.#define RTR
351b0 45 45 5f 47 54 20 30 78 34 35 0a 0a 2f 2a 20 0a  EE_GT 0x45../* .
351b1 2a 2a 20 41 6e 20 72 74 72 65 65 20 73 74 72 75  ** An rtree stru
351b2 63 74 75 72 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a  cture node..**.*
351b3 2a 20 44 61 74 61 20 66 6f 72 6d 61 74 20 28 52  * Data format (R
351b4 74 72 65 65 4e 6f 64 65 2e 7a 44 61 74 61 29 3a  treeNode.zData):
351b5 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 49 66 20 74  .**.**   1. If t
351b6 68 65 20 6e 6f 64 65 20 69 73 20 74 68 65 20 72  he node is the r
351b7 6f 6f 74 20 6e 6f 64 65 20 28 6e 6f 64 65 20 31  oot node (node 1
351b8 29 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73  ), then the firs
351b9 74 20 32 20 62 79 74 65 73 0a 2a 2a 20 20 20 20  t 2 bytes.**    
351ba 20 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 63 6f    of the node co
351bb 6e 74 61 69 6e 20 74 68 65 20 74 72 65 65 20 64  ntain the tree d
351bc 65 70 74 68 20 61 73 20 61 20 62 69 67 2d 65 6e  epth as a big-en
351bd 64 69 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  dian integer..**
351be 20 20 20 20 20 20 46 6f 72 20 6e 6f 6e 2d 72 6f        For non-ro
351bf 6f 74 20 6e 6f 64 65 73 2c 20 74 68 65 20 66 69  ot nodes, the fi
351c0 72 73 74 20 32 20 62 79 74 65 73 20 61 72 65 20  rst 2 bytes are 
351c1 6c 65 66 74 20 75 6e 75 73 65 64 2e 0a 2a 2a 0a  left unused..**.
351c2 2a 2a 20 20 20 32 2e 20 54 68 65 20 6e 65 78 74  **   2. The next
351c3 20 32 20 62 79 74 65 73 20 63 6f 6e 74 61 69 6e   2 bytes contain
351c4 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
351c5 6e 74 72 69 65 73 20 63 75 72 72 65 6e 74 6c 79  ntries currently
351c6 20 0a 2a 2a 20 20 20 20 20 20 73 74 6f 72 65 64   .**      stored
351c7 20 69 6e 20 74 68 65 20 6e 6f 64 65 2e 0a 2a 2a   in the node..**
351c8 0a 2a 2a 20 20 20 33 2e 20 54 68 65 20 72 65 6d  .**   3. The rem
351c9 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6e 6f  ainder of the no
351ca 64 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  de contains the 
351cb 6e 6f 64 65 20 65 6e 74 72 69 65 73 2e 20 45 61  node entries. Ea
351cc 63 68 20 65 6e 74 72 79 0a 2a 2a 20 20 20 20 20  ch entry.**     
351cd 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73   consists of a s
351ce 69 6e 67 6c 65 20 38 2d 62 79 74 65 20 69 6e 74  ingle 8-byte int
351cf 65 67 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79  eger followed by
351d0 20 61 6e 20 65 76 65 6e 20 6e 75 6d 62 65 72 0a   an even number.
351d1 2a 2a 20 20 20 20 20 20 6f 66 20 34 2d 62 79 74  **      of 4-byt
351d2 65 20 63 6f 6f 72 64 69 6e 61 74 65 73 2e 20 46  e coordinates. F
351d3 6f 72 20 6c 65 61 66 20 6e 6f 64 65 73 20 74 68  or leaf nodes th
351d4 65 20 69 6e 74 65 67 65 72 20 69 73 20 74 68 65  e integer is the
351d5 20 72 6f 77 69 64 0a 2a 2a 20 20 20 20 20 20 6f   rowid.**      o
351d6 66 20 61 20 72 65 63 6f 72 64 2e 20 46 6f 72 20  f a record. For 
351d7 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 73 20 69  internal nodes i
351d8 74 20 69 73 20 74 68 65 20 6e 6f 64 65 20 6e 75  t is the node nu
351d9 6d 62 65 72 20 6f 66 20 61 0a 2a 2a 20 20 20 20  mber of a.**    
351da 20 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 2a 2f    child page..*/
351db 0a 73 74 72 75 63 74 20 52 74 72 65 65 4e 6f 64  .struct RtreeNod
351dc 65 20 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e {.  RtreeNode 
351dd 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20  *pParent;       
351de 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e          /* Paren
351df 74 20 6e 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20  t node */.  i64 
351e0 69 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 6e 52 65  iNode;.  int nRe
351e1 66 3b 0a 20 20 69 6e 74 20 69 73 44 69 72 74 79  f;.  int isDirty
351e2 3b 0a 20 20 75 38 20 2a 7a 44 61 74 61 3b 0a 20  ;.  u8 *zData;. 
351e3 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 65 78   RtreeNode *pNex
351e4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
351e5 20 20 20 2f 2a 20 4e 65 78 74 20 6e 6f 64 65 20     /* Next node 
351e6 69 6e 20 74 68 69 73 20 68 61 73 68 20 63 68 61  in this hash cha
351e7 69 6e 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65  in */.};.#define
351e8 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 72 65   NCELL(pNode) re
351e9 61 64 49 6e 74 31 36 28 26 28 70 4e 6f 64 65 29  adInt16(&(pNode)
351ea 2d 3e 7a 44 61 74 61 5b 32 5d 29 0a 0a 2f 2a 20  ->zData[2])../* 
351eb 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 74 6f  .** Structure to
351ec 20 73 74 6f 72 65 20 61 20 64 65 73 65 72 69 61   store a deseria
351ed 6c 69 7a 65 64 20 72 74 72 65 65 20 72 65 63 6f  lized rtree reco
351ee 72 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  rd..*/.struct Rt
351ef 72 65 65 43 65 6c 6c 20 7b 0a 20 20 69 36 34 20  reeCell {.  i64 
351f0 69 52 6f 77 69 64 3b 0a 20 20 52 74 72 65 65 43  iRowid;.  RtreeC
351f1 6f 6f 72 64 20 61 43 6f 6f 72 64 5b 52 54 52 45  oord aCoord[RTRE
351f2 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53  E_MAX_DIMENSIONS
351f3 2a 32 5d 3b 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66  *2];.};..#ifndef
351f4 20 4d 41 58 0a 23 20 64 65 66 69 6e 65 20 4d 41   MAX.# define MA
351f5 58 28 78 2c 79 29 20 28 28 78 29 20 3c 20 28 79  X(x,y) ((x) < (y
351f6 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29 0a 23  ) ? (y) : (x)).#
351f7 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4d 49  endif.#ifndef MI
351f8 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78  N.# define MIN(x
351f9 2c 79 29 20 28 28 78 29 20 3e 20 28 79 29 20 3f  ,y) ((x) > (y) ?
351fa 20 28 79 29 20 3a 20 28 78 29 29 0a 23 65 6e 64   (y) : (x)).#end
351fb 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  if../*.** Functi
351fc 6f 6e 73 20 74 6f 20 64 65 73 65 72 69 61 6c 69  ons to deseriali
351fd 7a 65 20 61 20 31 36 20 62 69 74 20 69 6e 74 65  ze a 16 bit inte
351fe 67 65 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c  ger, 32 bit real
351ff 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36   number and.** 6
35200 34 20 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54  4 bit integer. T
35201 68 65 20 64 65 73 65 72 69 61 6c 69 7a 65 64 20  he deserialized 
35202 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65  value is returne
35203 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
35204 20 72 65 61 64 49 6e 74 31 36 28 75 38 20 2a 70   readInt16(u8 *p
35205 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30  ){.  return (p[0
35206 5d 3c 3c 38 29 20 2b 20 70 5b 31 5d 3b 0a 7d 0a  ]<<8) + p[1];.}.
35207 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 61 64  static void read
35208 43 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72  Coord(u8 *p, Rtr
35209 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29  eeCoord *pCoord)
3520a 7b 0a 20 20 75 33 32 20 69 20 3d 20 28 0a 20 20  {.  u32 i = (.  
3520b 20 20 28 28 28 75 33 32 29 70 5b 30 5d 29 20 3c    (((u32)p[0]) <
3520c 3c 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28  < 24) + .    (((
3520d 75 33 32 29 70 5b 31 5d 29 20 3c 3c 20 31 36 29  u32)p[1]) << 16)
3520e 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
3520f 5b 32 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20  [2]) <<  8) + . 
35210 20 20 20 28 28 28 75 33 32 29 70 5b 33 5d 29 20     (((u32)p[3]) 
35211 3c 3c 20 20 30 29 0a 20 20 29 3b 0a 20 20 2a 28  <<  0).  );.  *(
35212 75 33 32 20 2a 29 70 43 6f 6f 72 64 20 3d 20 69  u32 *)pCoord = i
35213 3b 0a 7d 0a 73 74 61 74 69 63 20 69 36 34 20 72  ;.}.static i64 r
35214 65 61 64 49 6e 74 36 34 28 75 38 20 2a 70 29 7b  eadInt64(u8 *p){
35215 0a 20 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20  .  return (.    
35216 28 28 28 69 36 34 29 70 5b 30 5d 29 20 3c 3c 20  (((i64)p[0]) << 
35217 35 36 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  56) + .    (((i6
35218 34 29 70 5b 31 5d 29 20 3c 3c 20 34 38 29 20 2b  4)p[1]) << 48) +
35219 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 32   .    (((i64)p[2
3521a 5d 29 20 3c 3c 20 34 30 29 20 2b 20 0a 20 20 20  ]) << 40) + .   
3521b 20 28 28 28 69 36 34 29 70 5b 33 5d 29 20 3c 3c   (((i64)p[3]) <<
3521c 20 33 32 29 20 2b 20 0a 20 20 20 20 28 28 28 69   32) + .    (((i
3521d 36 34 29 70 5b 34 5d 29 20 3c 3c 20 32 34 29 20  64)p[4]) << 24) 
3521e 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
3521f 35 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20 20  5]) << 16) + .  
35220 20 20 28 28 28 69 36 34 29 70 5b 36 5d 29 20 3c    (((i64)p[6]) <
35221 3c 20 20 38 29 20 2b 20 0a 20 20 20 20 28 28 28  <  8) + .    (((
35222 69 36 34 29 70 5b 37 5d 29 20 3c 3c 20 20 30 29  i64)p[7]) <<  0)
35223 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  .  );.}../*.** F
35224 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 65 72 69  unctions to seri
35225 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74 20 69  alize a 16 bit i
35226 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74 20 72  nteger, 32 bit r
35227 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a  eal number and.*
35228 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67 65 72  * 64 bit integer
35229 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  . The value retu
3522a 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62  rned is the numb
3522b 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74  er of bytes writ
3522c 74 65 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 61 72  ten.** to the ar
3522d 67 75 6d 65 6e 74 20 62 75 66 66 65 72 20 28 61  gument buffer (a
3522e 6c 77 61 79 73 20 32 2c 20 34 20 61 6e 64 20 38  lways 2, 4 and 8
3522f 20 72 65 73 70 65 63 74 69 76 65 6c 79 29 2e 0a   respectively)..
35230 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  */.static int wr
35231 69 74 65 49 6e 74 31 36 28 75 38 20 2a 70 2c 20  iteInt16(u8 *p, 
35232 69 6e 74 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d  int i){.  p[0] =
35233 20 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20   (i>> 8)&0xFF;. 
35234 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 20 30 29 26   p[1] = (i>> 0)&
35235 30 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 32  0xFF;.  return 2
35236 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ;.}.static int w
35237 72 69 74 65 43 6f 6f 72 64 28 75 38 20 2a 70 2c  riteCoord(u8 *p,
35238 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f   RtreeCoord *pCo
35239 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20  ord){.  u32 i;. 
3523a 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
3523b 52 74 72 65 65 43 6f 6f 72 64 29 3d 3d 34 20 29  RtreeCoord)==4 )
3523c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
3523d 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20  of(u32)==4 );.  
3523e 69 20 3d 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f  i = *(u32 *)pCoo
3523f 72 64 3b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e  rd;.  p[0] = (i>
35240 3e 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 31  >24)&0xFF;.  p[1
35241 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46  ] = (i>>16)&0xFF
35242 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 20  ;.  p[2] = (i>> 
35243 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20  8)&0xFF;.  p[3] 
35244 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a  = (i>> 0)&0xFF;.
35245 20 20 72 65 74 75 72 6e 20 34 3b 0a 7d 0a 73 74    return 4;.}.st
35246 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e  atic int writeIn
35247 74 36 34 28 75 38 20 2a 70 2c 20 69 36 34 20 69  t64(u8 *p, i64 i
35248 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e  ){.  p[0] = (i>>
35249 35 36 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d  56)&0xFF;.  p[1]
3524a 20 3d 20 28 69 3e 3e 34 38 29 26 30 78 46 46 3b   = (i>>48)&0xFF;
3524b 0a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 34 30  .  p[2] = (i>>40
3524c 29 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d  )&0xFF;.  p[3] =
3524d 20 28 69 3e 3e 33 32 29 26 30 78 46 46 3b 0a 20   (i>>32)&0xFF;. 
3524e 20 70 5b 34 5d 20 3d 20 28 69 3e 3e 32 34 29 26   p[4] = (i>>24)&
3524f 30 78 46 46 3b 0a 20 20 70 5b 35 5d 20 3d 20 28  0xFF;.  p[5] = (
35250 69 3e 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70  i>>16)&0xFF;.  p
35251 5b 36 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [6] = (i>> 8)&0x
35252 46 46 3b 0a 20 20 70 5b 37 5d 20 3d 20 28 69 3e  FF;.  p[7] = (i>
35253 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
35254 75 72 6e 20 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 8;.}../*.** 
35255 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65  Increment the re
35256 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66  ference count of
35257 20 6e 6f 64 65 20 70 2e 0a 2a 2f 0a 73 74 61 74   node p..*/.stat
35258 69 63 20 76 6f 69 64 20 6e 6f 64 65 52 65 66 65  ic void nodeRefe
35259 72 65 6e 63 65 28 52 74 72 65 65 4e 6f 64 65 20  rence(RtreeNode 
3525a 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  *p){.  if( p ){.
3525b 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20      p->nRef++;. 
3525c 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61   }.}../*.** Clea
3525d 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  r the content of
3525e 20 6e 6f 64 65 20 70 20 28 73 65 74 20 61 6c 6c   node p (set all
3525f 20 62 79 74 65 73 20 74 6f 20 30 78 30 30 29 2e   bytes to 0x00).
35260 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
35261 6e 6f 64 65 5a 65 72 6f 28 52 74 72 65 65 20 2a  nodeZero(Rtree *
35262 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
35263 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  e *p){.  if( p )
35264 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 2d  {.    memset(&p-
35265 3e 7a 44 61 74 61 5b 32 5d 2c 20 30 2c 20 70 52  >zData[2], 0, pR
35266 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d  tree->iNodeSize-
35267 32 29 3b 0a 20 20 20 20 70 2d 3e 69 73 44 69 72  2);.    p->isDir
35268 74 79 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ty = 1;.  }.}../
35269 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 6e 6f 64  *.** Given a nod
3526a 65 20 6e 75 6d 62 65 72 20 69 4e 6f 64 65 2c 20  e number iNode, 
3526b 72 65 74 75 72 6e 20 74 68 65 20 63 6f 72 72 65  return the corre
3526c 73 70 6f 6e 64 69 6e 67 20 6b 65 79 20 74 6f 20  sponding key to 
3526d 75 73 65 0a 2a 2a 20 69 6e 20 74 68 65 20 52 74  use.** in the Rt
3526e 72 65 65 2e 61 48 61 73 68 20 74 61 62 6c 65 2e  ree.aHash table.
3526f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
35270 6f 64 65 48 61 73 68 28 69 36 34 20 69 4e 6f 64  odeHash(i64 iNod
35271 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 0a 20  e){.  return (. 
35272 20 20 20 28 69 4e 6f 64 65 3e 3e 35 36 29 20 5e     (iNode>>56) ^
35273 20 28 69 4e 6f 64 65 3e 3e 34 38 29 20 5e 20 28   (iNode>>48) ^ (
35274 69 4e 6f 64 65 3e 3e 34 30 29 20 5e 20 28 69 4e  iNode>>40) ^ (iN
35275 6f 64 65 3e 3e 33 32 29 20 5e 20 0a 20 20 20 20  ode>>32) ^ .    
35276 28 69 4e 6f 64 65 3e 3e 32 34 29 20 5e 20 28 69  (iNode>>24) ^ (i
35277 4e 6f 64 65 3e 3e 31 36 29 20 5e 20 28 69 4e 6f  Node>>16) ^ (iNo
35278 64 65 3e 3e 20 38 29 20 5e 20 28 69 4e 6f 64 65  de>> 8) ^ (iNode
35279 3e 3e 20 30 29 0a 20 20 29 20 25 20 48 41 53 48  >> 0).  ) % HASH
3527a 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  SIZE;.}../*.** S
3527b 65 61 72 63 68 20 74 68 65 20 6e 6f 64 65 20 68  earch the node h
3527c 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 6e 6f  ash table for no
3527d 64 65 20 69 4e 6f 64 65 2e 20 49 66 20 66 6f 75  de iNode. If fou
3527e 6e 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  nd, return a poi
3527f 6e 74 65 72 0a 2a 2a 20 74 6f 20 69 74 2e 20 4f  nter.** to it. O
35280 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
35281 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74   0..*/.static Rt
35282 72 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 48 61 73  reeNode *nodeHas
35283 68 4c 6f 6f 6b 75 70 28 52 74 72 65 65 20 2a 70  hLookup(Rtree *p
35284 52 74 72 65 65 2c 20 69 36 34 20 69 4e 6f 64 65  Rtree, i64 iNode
35285 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
35286 70 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4e 6f  p;.  assert( iNo
35287 64 65 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70  de!=0 );.  for(p
35288 3d 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e  =pRtree->aHash[n
35289 6f 64 65 48 61 73 68 28 69 4e 6f 64 65 29 5d 3b  odeHash(iNode)];
3528a 20 70 20 26 26 20 70 2d 3e 69 4e 6f 64 65 21 3d   p && p->iNode!=
3528b 69 4e 6f 64 65 3b 20 70 3d 70 2d 3e 70 4e 65 78  iNode; p=p->pNex
3528c 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  t);.  return p;.
3528d 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 6e 6f 64  }../*.** Add nod
3528e 65 20 70 4e 6f 64 65 20 74 6f 20 74 68 65 20 6e  e pNode to the n
3528f 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ode hash table..
35290 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
35291 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 52 74  odeHashInsert(Rt
35292 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
35293 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
35294 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
35295 20 20 20 69 6e 74 20 69 48 61 73 68 3b 0a 20 20     int iHash;.  
35296 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d    assert( pNode-
35297 3e 70 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 20  >pNext==0 );.   
35298 20 69 48 61 73 68 20 3d 20 6e 6f 64 65 48 61 73   iHash = nodeHas
35299 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  h(pNode->iNode);
3529a 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  .    pNode->pNex
3529b 74 20 3d 20 70 52 74 72 65 65 2d 3e 61 48 61 73  t = pRtree->aHas
3529c 68 5b 69 48 61 73 68 5d 3b 0a 20 20 20 20 70 52  h[iHash];.    pR
3529d 74 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61 73  tree->aHash[iHas
3529e 68 5d 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 7d 0a  h] = pNode;.  }.
3529f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
352a0 6e 6f 64 65 20 70 4e 6f 64 65 20 66 72 6f 6d 20  node pNode from 
352a1 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61  the node hash ta
352a2 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
352a3 6f 69 64 20 6e 6f 64 65 48 61 73 68 44 65 6c 65  oid nodeHashDele
352a4 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  te(Rtree *pRtree
352a5 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
352a6 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65  de){.  RtreeNode
352a7 20 2a 2a 70 70 3b 0a 20 20 69 66 28 20 70 4e 6f   **pp;.  if( pNo
352a8 64 65 2d 3e 69 4e 6f 64 65 21 3d 30 20 29 7b 0a  de->iNode!=0 ){.
352a9 20 20 20 20 70 70 20 3d 20 26 70 52 74 72 65 65      pp = &pRtree
352aa 2d 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68  ->aHash[nodeHash
352ab 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 5d 3b  (pNode->iNode)];
352ac 0a 20 20 20 20 66 6f 72 28 20 3b 20 28 2a 70 70  .    for( ; (*pp
352ad 29 21 3d 70 4e 6f 64 65 3b 20 70 70 20 3d 20 26  )!=pNode; pp = &
352ae 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 7b 20 61  (*pp)->pNext){ a
352af 73 73 65 72 74 28 2a 70 70 29 3b 20 7d 0a 20 20  ssert(*pp); }.  
352b0 20 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e 70    *pp = pNode->p
352b1 4e 65 78 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  Next;.    pNode-
352b2 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d 0a  >pNext = 0;.  }.
352b3 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
352b4 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 65 77  e and return new
352b5 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e 20 49 6e   r-tree node. In
352b6 69 74 69 61 6c 6c 79 2c 20 28 52 74 72 65 65 4e  itially, (RtreeN
352b7 6f 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2c 0a 2a  ode.iNode==0),.*
352b8 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61  * indicating tha
352b9 74 20 6e 6f 64 65 20 68 61 73 20 6e 6f 74 20 79  t node has not y
352ba 65 74 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64  et been assigned
352bb 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20   a node number. 
352bc 49 74 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65  It is.** assigne
352bd 64 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20  d a node number 
352be 77 68 65 6e 20 6e 6f 64 65 57 72 69 74 65 28 29  when nodeWrite()
352bf 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 77 72   is called to wr
352c0 69 74 65 20 74 68 65 0a 2a 2a 20 6e 6f 64 65 20  ite the.** node 
352c1 63 6f 6e 74 65 6e 74 73 20 6f 75 74 20 74 6f 20  contents out to 
352c2 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  the database..*/
352c3 0a 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f 64  .static RtreeNod
352c4 65 20 2a 6e 6f 64 65 4e 65 77 28 52 74 72 65 65  e *nodeNew(Rtree
352c5 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
352c6 6f 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e  ode *pParent, in
352c7 74 20 7a 65 72 6f 29 7b 0a 20 20 52 74 72 65 65  t zero){.  Rtree
352c8 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20 70  Node *pNode;.  p
352c9 4e 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64  Node = (RtreeNod
352ca 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  e *)sqlite3_mall
352cb 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e  oc(sizeof(RtreeN
352cc 6f 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69  ode) + pRtree->i
352cd 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28  NodeSize);.  if(
352ce 20 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 6d 65   pNode ){.    me
352cf 6d 73 65 74 28 70 4e 6f 64 65 2c 20 30 2c 20 73  mset(pNode, 0, s
352d0 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29  izeof(RtreeNode)
352d1 20 2b 20 28 7a 65 72 6f 3f 70 52 74 72 65 65 2d   + (zero?pRtree-
352d2 3e 69 4e 6f 64 65 53 69 7a 65 3a 30 29 29 3b 0a  >iNodeSize:0));.
352d3 20 20 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61      pNode->zData
352d4 20 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b   = (u8 *)&pNode[
352d5 31 5d 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e  1];.    pNode->n
352d6 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4e 6f  Ref = 1;.    pNo
352d7 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50  de->pParent = pP
352d8 61 72 65 6e 74 3b 0a 20 20 20 20 70 4e 6f 64 65  arent;.    pNode
352d9 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20  ->isDirty = 1;. 
352da 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
352db 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 20  (pParent);.  }. 
352dc 20 72 65 74 75 72 6e 20 70 4e 6f 64 65 3b 0a 7d   return pNode;.}
352dd 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61  ../*.** Obtain a
352de 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e   reference to an
352df 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e 0a 2a 2f   r-tree node..*/
352e0 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
352e1 41 63 71 75 69 72 65 28 0a 20 20 52 74 72 65 65  Acquire(.  Rtree
352e2 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20   *pRtree,       
352e3 20 20 20 20 20 20 2f 2a 20 52 2d 74 72 65 65 20        /* R-tree 
352e4 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69  structure */.  i
352e5 36 34 20 69 4e 6f 64 65 2c 20 20 20 20 20 20 20  64 iNode,       
352e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64            /* Nod
352e7 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 61 64  e number to load
352e8 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20   */.  RtreeNode 
352e9 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20  *pParent,       
352ea 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 70   /* Either the p
352eb 61 72 65 6e 74 20 6e 6f 64 65 20 6f 72 20 4e 55  arent node or NU
352ec 4c 4c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  LL */.  RtreeNod
352ed 65 20 2a 2a 70 70 4e 6f 64 65 20 20 20 20 20 20  e **ppNode      
352ee 20 20 20 2f 2a 20 4f 55 54 3a 20 41 63 71 75 69     /* OUT: Acqui
352ef 72 65 64 20 6e 6f 64 65 20 2a 2f 0a 29 7b 0a 20  red node */.){. 
352f0 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65   int rc;.  Rtree
352f1 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 0a 20 20  Node *pNode;..  
352f2 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20  /* Check if the 
352f3 72 65 71 75 65 73 74 65 64 20 6e 6f 64 65 20 69  requested node i
352f4 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
352f5 20 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20   hash table. If 
352f6 73 6f 2c 0a 20 20 2a 2a 20 69 6e 63 72 65 61 73  so,.  ** increas
352f7 65 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20  e its reference 
352f8 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e  count and return
352f9 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
352fa 28 70 4e 6f 64 65 20 3d 20 6e 6f 64 65 48 61 73  (pNode = nodeHas
352fb 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20  hLookup(pRtree, 
352fc 69 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 61  iNode)) ){.    a
352fd 73 73 65 72 74 28 20 21 70 50 61 72 65 6e 74 20  ssert( !pParent 
352fe 7c 7c 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72 65  || !pNode->pPare
352ff 6e 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 70 50 61  nt || pNode->pPa
35300 72 65 6e 74 3d 3d 70 50 61 72 65 6e 74 20 29 3b  rent==pParent );
35301 0a 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74  .    if( pParent
35302 20 26 26 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72   && !pNode->pPar
35303 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ent ){.      nod
35304 65 52 65 66 65 72 65 6e 63 65 28 70 50 61 72 65  eReference(pPare
35305 6e 74 29 3b 0a 20 20 20 20 20 20 70 4e 6f 64 65  nt);.      pNode
35306 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72  ->pParent = pPar
35307 65 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ent;.    }.    p
35308 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  Node->nRef++;.  
35309 20 20 2a 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64    *ppNode = pNod
3530a 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e;.    return SQ
3530b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
3530c 70 4e 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f  pNode = (RtreeNo
3530d 64 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  de *)sqlite3_mal
3530e 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
3530f 4e 6f 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e  Node) + pRtree->
35310 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66  iNodeSize);.  if
35311 28 20 21 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20  ( !pNode ){.    
35312 2a 70 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20  *ppNode = 0;.   
35313 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
35314 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64  OMEM;.  }.  pNod
35315 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61  e->pParent = pPa
35316 72 65 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 7a  rent;.  pNode->z
35317 44 61 74 61 20 3d 20 28 75 38 20 2a 29 26 70 4e  Data = (u8 *)&pN
35318 6f 64 65 5b 31 5d 3b 0a 20 20 70 4e 6f 64 65 2d  ode[1];.  pNode-
35319 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 6f  >nRef = 1;.  pNo
3531a 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 69 4e 6f 64  de->iNode = iNod
3531b 65 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69  e;.  pNode->isDi
3531c 72 74 79 20 3d 20 30 3b 0a 20 20 70 4e 6f 64 65  rty = 0;.  pNode
3531d 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20  ->pNext = 0;..  
3531e 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3531f 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  64(pRtree->pRead
35320 4e 6f 64 65 2c 20 31 2c 20 69 4e 6f 64 65 29 3b  Node, 1, iNode);
35321 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
35322 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65  step(pRtree->pRe
35323 61 64 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 72  adNode);.  if( r
35324 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
35325 0a 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  .    const u8 *z
35326 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 63  Blob = sqlite3_c
35327 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 74 72 65  olumn_blob(pRtre
35328 65 2d 3e 70 52 65 61 64 4e 6f 64 65 2c 20 30 29  e->pReadNode, 0)
35329 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 6f  ;.    memcpy(pNo
3532a 64 65 2d 3e 7a 44 61 74 61 2c 20 7a 42 6c 6f 62  de->zData, zBlob
3532b 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  , pRtree->iNodeS
3532c 69 7a 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ize);.    nodeRe
3532d 66 65 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29  ference(pParent)
3532e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
3532f 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64  qlite3_free(pNod
35330 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 20 3d 20  e);.    pNode = 
35331 30 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4e 6f 64  0;.  }..  *ppNod
35332 65 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 63 20  e = pNode;.  rc 
35333 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
35334 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
35335 65 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  e);..  if( rc==S
35336 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4e 6f 64  QLITE_OK && iNod
35337 65 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 74 72  e==1 ){.    pRtr
35338 65 65 2d 3e 69 44 65 70 74 68 20 3d 20 72 65 61  ee->iDepth = rea
35339 64 49 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44  dInt16(pNode->zD
3533a 61 74 61 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  ata);.  }..  ass
3533b 65 72 74 28 20 28 72 63 3d 3d 53 51 4c 49 54 45  ert( (rc==SQLITE
3533c 5f 4f 4b 20 26 26 20 70 4e 6f 64 65 29 20 7c 7c  _OK && pNode) ||
3533d 20 28 70 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63   (pNode==0 && rc
3533e 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b 0a  !=SQLITE_OK) );.
3533f 20 20 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74    nodeHashInsert
35340 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
35341 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
35342 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65 72 77 72 69 74  ../*.** Overwrit
35343 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20  e cell iCell of 
35344 6e 6f 64 65 20 70 4e 6f 64 65 20 77 69 74 68 20  node pNode with 
35345 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
35346 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  pCell..*/.static
35347 20 76 6f 69 64 20 6e 6f 64 65 4f 76 65 72 77 72   void nodeOverwr
35348 69 74 65 43 65 6c 6c 28 0a 20 20 52 74 72 65 65  iteCell(.  Rtree
35349 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72   *pRtree, .  Rtr
3534a 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20  eeNode *pNode,  
3534b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
3534c 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c  ell, .  int iCel
3534d 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  l.){.  int ii;. 
3534e 20 75 38 20 2a 70 20 3d 20 26 70 4e 6f 64 65 2d   u8 *p = &pNode-
3534f 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65  >zData[4 + pRtre
35350 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
35351 2a 69 43 65 6c 6c 5d 3b 0a 20 20 70 20 2b 3d 20  *iCell];.  p += 
35352 77 72 69 74 65 49 6e 74 36 34 28 70 2c 20 70 43  writeInt64(p, pC
35353 65 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20  ell->iRowid);.  
35354 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52  for(ii=0; ii<(pR
35355 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69  tree->nDim*2); i
35356 69 2b 2b 29 7b 0a 20 20 20 20 70 20 2b 3d 20 77  i++){.    p += w
35357 72 69 74 65 43 6f 6f 72 64 28 70 2c 20 26 70 43  riteCoord(p, &pC
35358 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29  ell->aCoord[ii])
35359 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 69  ;.  }.  pNode->i
3535a 73 44 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f  sDirty = 1;.}../
3535b 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 63 65 6c 6c  *.** Remove cell
3535c 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69   the cell with i
3535d 6e 64 65 78 20 69 43 65 6c 6c 20 66 72 6f 6d 20  ndex iCell from 
3535e 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73  node pNode..*/.s
3535f 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 44  tatic void nodeD
35360 65 6c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20  eleteCell(Rtree 
35361 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
35362 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69  de *pNode, int i
35363 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 44 73  Cell){.  u8 *pDs
35364 74 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  t = &pNode->zDat
35365 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  a[4 + pRtree->nB
35366 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
35367 6c 5d 3b 0a 20 20 75 38 20 2a 70 53 72 63 20 3d  l];.  u8 *pSrc =
35368 20 26 70 44 73 74 5b 70 52 74 72 65 65 2d 3e 6e   &pDst[pRtree->n
35369 42 79 74 65 73 50 65 72 43 65 6c 6c 5d 3b 0a 20  BytesPerCell];. 
3536a 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 4e 43   int nByte = (NC
3536b 45 4c 4c 28 70 4e 6f 64 65 29 20 2d 20 69 43 65  ELL(pNode) - iCe
3536c 6c 6c 20 2d 20 31 29 20 2a 20 70 52 74 72 65 65  ll - 1) * pRtree
3536d 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b  ->nBytesPerCell;
3536e 0a 20 20 6d 65 6d 6d 6f 76 65 28 70 44 73 74 2c  .  memmove(pDst,
3536f 20 70 53 72 63 2c 20 6e 42 79 74 65 29 3b 0a 20   pSrc, nByte);. 
35370 20 77 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f   writeInt16(&pNo
35371 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 4e 43  de->zData[2], NC
35372 45 4c 4c 28 70 4e 6f 64 65 29 2d 31 29 3b 0a 20  ELL(pNode)-1);. 
35373 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20   pNode->isDirty 
35374 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  = 1;.}../*.** In
35375 73 65 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  sert the content
35376 73 20 6f 66 20 63 65 6c 6c 20 70 43 65 6c 6c 20  s of cell pCell 
35377 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  into node pNode.
35378 20 49 66 20 74 68 65 20 69 6e 73 65 72 74 0a 2a   If the insert.*
35379 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  * is successful,
3537a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3537b 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  K..**.** If ther
3537c 65 20 69 73 20 6e 6f 74 20 65 6e 6f 75 67 68 20  e is not enough 
3537d 66 72 65 65 20 73 70 61 63 65 20 69 6e 20 70 4e  free space in pN
3537e 6f 64 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ode, return SQLI
3537f 54 45 5f 46 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74  TE_FULL..*/.stat
35380 69 63 20 69 6e 74 0a 6e 6f 64 65 49 6e 73 65 72  ic int.nodeInser
35381 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a  tCell(.  Rtree *
35382 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
35383 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20  Node *pNode, .  
35384 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c  RtreeCell *pCell
35385 20 0a 29 7b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c   .){.  int nCell
35386 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35387 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
35388 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
35389 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e  in pNode */.  in
3538a 74 20 6e 4d 61 78 43 65 6c 6c 3b 20 20 20 20 20  t nMaxCell;     
3538b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
3538c 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
3538d 20 63 65 6c 6c 73 20 66 6f 72 20 70 4e 6f 64 65   cells for pNode
3538e 20 2a 2f 0a 0a 20 20 6e 4d 61 78 43 65 6c 6c 20   */..  nMaxCell 
3538f 3d 20 28 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  = (pRtree->iNode
35390 53 69 7a 65 2d 34 29 2f 70 52 74 72 65 65 2d 3e  Size-4)/pRtree->
35391 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20  nBytesPerCell;. 
35392 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
35393 4e 6f 64 65 29 3b 0a 0a 20 20 61 73 73 65 72 74  Node);..  assert
35394 28 6e 43 65 6c 6c 3c 3d 6e 4d 61 78 43 65 6c 6c  (nCell<=nMaxCell
35395 29 3b 0a 0a 20 20 69 66 28 20 6e 43 65 6c 6c 3c  );..  if( nCell<
35396 6e 4d 61 78 43 65 6c 6c 20 29 7b 0a 20 20 20 20  nMaxCell ){.    
35397 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c  nodeOverwriteCel
35398 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
35399 20 70 43 65 6c 6c 2c 20 6e 43 65 6c 6c 29 3b 0a   pCell, nCell);.
3539a 20 20 20 20 77 72 69 74 65 49 6e 74 31 36 28 26      writeInt16(&
3539b 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c  pNode->zData[2],
3539c 20 6e 43 65 6c 6c 2b 31 29 3b 0a 20 20 20 20 70   nCell+1);.    p
3539d 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
3539e 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  1;.  }..  return
3539f 20 28 6e 43 65 6c 6c 3d 3d 6e 4d 61 78 43 65 6c   (nCell==nMaxCel
353a0 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  l);.}../*.** If 
353a1 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69 72 74  the node is dirt
353a2 79 2c 20 77 72 69 74 65 20 69 74 20 6f 75 74 20  y, write it out 
353a3 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
353a4 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e  .*/.static int.n
353a5 6f 64 65 57 72 69 74 65 28 52 74 72 65 65 20 2a  odeWrite(Rtree *
353a6 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
353a7 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74  e *pNode){.  int
353a8 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
353a9 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 73  .  if( pNode->is
353aa 44 69 72 74 79 20 29 7b 0a 20 20 20 20 73 71 6c  Dirty ){.    sql
353ab 69 74 65 33 5f 73 74 6d 74 20 2a 70 20 3d 20 70  ite3_stmt *p = p
353ac 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64  Rtree->pWriteNod
353ad 65 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65  e;.    if( pNode
353ae 2d 3e 69 4e 6f 64 65 20 29 7b 0a 20 20 20 20 20  ->iNode ){.     
353af 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
353b0 74 36 34 28 70 2c 20 31 2c 20 70 4e 6f 64 65 2d  t64(p, 1, pNode-
353b1 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c  >iNode);.    }el
353b2 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
353b3 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 2c 20 31  3_bind_null(p, 1
353b4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
353b5 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70  ite3_bind_blob(p
353b6 2c 20 32 2c 20 70 4e 6f 64 65 2d 3e 7a 44 61 74  , 2, pNode->zDat
353b7 61 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  a, pRtree->iNode
353b8 53 69 7a 65 2c 20 53 51 4c 49 54 45 5f 53 54 41  Size, SQLITE_STA
353b9 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
353ba 33 5f 73 74 65 70 28 70 29 3b 0a 20 20 20 20 70  3_step(p);.    p
353bb 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
353bc 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  0;.    rc = sqli
353bd 74 65 33 5f 72 65 73 65 74 28 70 29 3b 0a 20 20  te3_reset(p);.  
353be 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
353bf 64 65 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c  de==0 && rc==SQL
353c0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
353c1 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 73  pNode->iNode = s
353c2 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
353c3 72 74 5f 72 6f 77 69 64 28 70 52 74 72 65 65 2d  rt_rowid(pRtree-
353c4 3e 64 62 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65  >db);.      node
353c5 48 61 73 68 49 6e 73 65 72 74 28 70 52 74 72 65  HashInsert(pRtre
353c6 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d  e, pNode);.    }
353c7 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
353c8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
353c9 73 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  se a reference t
353ca 6f 20 61 20 6e 6f 64 65 2e 20 49 66 20 74 68 65  o a node. If the
353cb 20 6e 6f 64 65 20 69 73 20 64 69 72 74 79 20 61   node is dirty a
353cc 6e 64 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  nd the reference
353cd 0a 2a 2a 20 63 6f 75 6e 74 20 64 72 6f 70 73 20  .** count drops 
353ce 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20 6e 6f 64  to zero, the nod
353cf 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  e data is writte
353d0 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  n to the databas
353d1 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
353d2 0a 6e 6f 64 65 52 65 6c 65 61 73 65 28 52 74 72  .nodeRelease(Rtr
353d3 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
353d4 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20  eNode *pNode){. 
353d5 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
353d6 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65  _OK;.  if( pNode
353d7 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
353d8 70 4e 6f 64 65 2d 3e 6e 52 65 66 3e 30 20 29 3b  pNode->nRef>0 );
353d9 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  .    pNode->nRef
353da 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64  --;.    if( pNod
353db 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  e->nRef==0 ){.  
353dc 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69      if( pNode->i
353dd 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 20  Node==1 ){.     
353de 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74     pRtree->iDept
353df 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  h = -1;.      }.
353e0 20 20 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d        if( pNode-
353e1 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  >pParent ){.    
353e2 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c      rc = nodeRel
353e3 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  ease(pRtree, pNo
353e4 64 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  de->pParent);.  
353e5 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
353e6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
353e7 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  .        rc = no
353e8 64 65 57 72 69 74 65 28 70 52 74 72 65 65 2c 20  deWrite(pRtree, 
353e9 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a  pNode);.      }.
353ea 20 20 20 20 20 20 6e 6f 64 65 48 61 73 68 44 65        nodeHashDe
353eb 6c 65 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  lete(pRtree, pNo
353ec 64 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  de);.      sqlit
353ed 65 33 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a  e3_free(pNode);.
353ee 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
353ef 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
353f0 52 65 74 75 72 6e 20 74 68 65 20 36 34 2d 62 69  Return the 64-bi
353f1 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  t integer value 
353f2 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
353f3 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 0a 2a 2a  cell iCell of.**
353f4 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20   node pNode. If 
353f5 70 4e 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20  pNode is a leaf 
353f6 6e 6f 64 65 2c 20 74 68 69 73 20 69 73 20 61 20  node, this is a 
353f7 72 6f 77 69 64 2e 20 49 66 20 69 74 20 69 73 0a  rowid. If it is.
353f8 2a 2a 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e  ** an internal n
353f9 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 36 34  ode, then the 64
353fa 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20  -bit integer is 
353fb 61 20 63 68 69 6c 64 20 70 61 67 65 20 6e 75 6d  a child page num
353fc 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ber..*/.static i
353fd 36 34 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28  64 nodeGetRowid(
353fe 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
353ff 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  , .  RtreeNode *
35400 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 43  pNode, .  int iC
35401 65 6c 6c 0a 29 7b 0a 20 20 61 73 73 65 72 74 28  ell.){.  assert(
35402 20 69 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 4e 6f   iCell<NCELL(pNo
35403 64 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  de) );.  return 
35404 72 65 61 64 49 6e 74 36 34 28 26 70 4e 6f 64 65  readInt64(&pNode
35405 2d 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72  ->zData[4 + pRtr
35406 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
35407 6c 2a 69 43 65 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a  l*iCell]);.}../*
35408 0a 2a 2a 20 52 65 74 75 72 6e 20 63 6f 6f 72 64  .** Return coord
35409 69 6e 61 74 65 20 69 43 6f 6f 72 64 20 66 72 6f  inate iCoord fro
3540a 6d 20 63 65 6c 6c 20 69 43 65 6c 6c 20 69 6e 20  m cell iCell in 
3540b 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73  node pNode..*/.s
3540c 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47  tatic void nodeG
3540d 65 74 43 6f 6f 72 64 28 0a 20 20 52 74 72 65 65  etCoord(.  Rtree
3540e 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72   *pRtree, .  Rtr
3540f 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a  eeNode *pNode, .
35410 20 20 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 69    int iCell,.  i
35411 6e 74 20 69 43 6f 6f 72 64 2c 0a 20 20 52 74 72  nt iCoord,.  Rtr
35412 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 20  eeCoord *pCoord 
35413 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
35414 63 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75  ce to write resu
35415 6c 74 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 72 65  lt to */.){.  re
35416 61 64 43 6f 6f 72 64 28 26 70 4e 6f 64 65 2d 3e  adCoord(&pNode->
35417 7a 44 61 74 61 5b 31 32 20 2b 20 70 52 74 72 65  zData[12 + pRtre
35418 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
35419 2a 69 43 65 6c 6c 20 2b 20 34 2a 69 43 6f 6f 72  *iCell + 4*iCoor
3541a 64 5d 2c 20 70 43 6f 6f 72 64 29 3b 0a 7d 0a 0a  d], pCoord);.}..
3541b 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a  /*.** Deserializ
3541c 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20  e cell iCell of 
3541d 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 50 6f 70 75  node pNode. Popu
3541e 6c 61 74 65 20 74 68 65 20 73 74 72 75 63 74 75  late the structu
3541f 72 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  re pointed.** to
35420 20 62 79 20 70 43 65 6c 6c 20 77 69 74 68 20 74   by pCell with t
35421 68 65 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73  he results..*/.s
35422 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47  tatic void nodeG
35423 65 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20  etCell(.  Rtree 
35424 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
35425 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20  eNode *pNode, . 
35426 20 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 52 74   int iCell,.  Rt
35427 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 0a 29  reeCell *pCell.)
35428 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 70 43  {.  int ii;.  pC
35429 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 3d 20 6e 6f  ell->iRowid = no
3542a 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65  deGetRowid(pRtre
3542b 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 29  e, pNode, iCell)
3542c 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
3542d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 3b  <pRtree->nDim*2;
3542e 20 69 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 64 65   ii++){.    node
3542f 47 65 74 43 6f 6f 72 64 28 70 52 74 72 65 65 2c  GetCoord(pRtree,
35430 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 69   pNode, iCell, i
35431 69 2c 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72  i, &pCell->aCoor
35432 64 5b 69 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a  d[ii]);.  }.}...
35433 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
35434 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66  ration for the f
35435 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65  unction that doe
35436 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a  s the work of.**
35437 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
35438 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65 61 74  le module xCreat
35439 65 28 29 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74  e() and xConnect
3543a 28 29 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 73  () methods..*/.s
3543b 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49  tatic int rtreeI
3543c 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit(.  sqlite3 *
3543d 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 63  , void *, int, c
3543e 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
3543f 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  *, sqlite3_vtab 
35440 2a 2a 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74  **, char **, int
35441 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  .);../* .** Rtre
35442 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
35443 6d 6f 64 75 6c 65 20 78 43 72 65 61 74 65 20 6d  module xCreate m
35444 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
35445 20 69 6e 74 20 72 74 72 65 65 43 72 65 61 74 65   int rtreeCreate
35446 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
35447 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20  .  void *pAux,. 
35448 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
35449 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
3544a 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  v,.  sqlite3_vta
3544b 62 20 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68  b **ppVtab,.  ch
3544c 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20  ar **pzErr.){.  
3544d 72 65 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74  return rtreeInit
3544e 28 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c  (db, pAux, argc,
3544f 20 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70   argv, ppVtab, p
35450 7a 45 72 72 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20  zErr, 1);.}../* 
35451 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
35452 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
35453 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a  Connect method..
35454 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
35455 72 65 65 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71  reeConnect(.  sq
35456 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69  lite3 *db,.  voi
35457 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61  d *pAux,.  int a
35458 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
35459 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73  *const*argv,.  s
3545a 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
3545b 56 74 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70  Vtab,.  char **p
3545c 7a 45 72 72 0a 29 7b 0a 20 20 72 65 74 75 72 6e  zErr.){.  return
3545d 20 72 74 72 65 65 49 6e 69 74 28 64 62 2c 20 70   rtreeInit(db, p
3545e 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  Aux, argc, argv,
3545f 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72 2c 20   ppVtab, pzErr, 
35460 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  0);.}../*.** Inc
35461 72 65 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65  rement the r-tre
35462 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
35463 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
35464 64 20 72 74 72 65 65 52 65 66 65 72 65 6e 63 65  d rtreeReference
35465 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b  (Rtree *pRtree){
35466 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79  .  pRtree->nBusy
35467 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63  ++;.}../*.** Dec
35468 72 65 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65  rement the r-tre
35469 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
3546a 74 2e 20 57 68 65 6e 20 74 68 65 20 72 65 66 65  t. When the refe
3546b 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63  rence count reac
3546c 68 65 73 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20  hes.** zero the 
3546d 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c  structure is del
3546e 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eted..*/.static 
3546f 76 6f 69 64 20 72 74 72 65 65 52 65 6c 65 61 73  void rtreeReleas
35470 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29  e(Rtree *pRtree)
35471 7b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73  {.  pRtree->nBus
35472 79 2d 2d 3b 0a 20 20 69 66 28 20 70 52 74 72 65  y--;.  if( pRtre
35473 65 2d 3e 6e 42 75 73 79 3d 3d 30 20 29 7b 0a 20  e->nBusy==0 ){. 
35474 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
35475 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61  ize(pRtree->pRea
35476 64 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  dNode);.    sqli
35477 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74  te3_finalize(pRt
35478 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 29  ree->pWriteNode)
35479 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
3547a 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
3547b 44 65 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20 20  DeleteNode);.   
3547c 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
3547d 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52  e(pRtree->pReadR
3547e 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
3547f 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
35480 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29  ee->pWriteRowid)
35481 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
35482 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
35483 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20  DeleteRowid);.  
35484 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
35485 7a 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ze(pRtree->pRead
35486 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c  Parent);.    sql
35487 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
35488 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65  tree->pWritePare
35489 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
3548a 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
3548b 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29  ->pDeleteParent)
3548c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
3548d 65 65 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a  ee(pRtree);.  }.
3548e 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
3548f 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35490 64 75 6c 65 20 78 44 69 73 63 6f 6e 6e 65 63 74  dule xDisconnect
35491 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
35492 69 63 20 69 6e 74 20 72 74 72 65 65 44 69 73 63  ic int rtreeDisc
35493 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76  onnect(sqlite3_v
35494 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 72  tab *pVtab){.  r
35495 74 72 65 65 52 65 6c 65 61 73 65 28 28 52 74 72  treeRelease((Rtr
35496 65 65 20 2a 29 70 56 74 61 62 29 3b 0a 20 20 72  ee *)pVtab);.  r
35497 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
35498 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  .}../* .** Rtree
35499 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
3549a 6f 64 75 6c 65 20 78 44 65 73 74 72 6f 79 20 6d  odule xDestroy m
3549b 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
3549c 20 69 6e 74 20 72 74 72 65 65 44 65 73 74 72 6f   int rtreeDestro
3549d 79 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  y(sqlite3_vtab *
3549e 70 56 74 61 62 29 7b 0a 20 20 52 74 72 65 65 20  pVtab){.  Rtree 
3549f 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
354a0 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20   *)pVtab;.  int 
354a1 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 43 72 65  rc;.  char *zCre
354a2 61 74 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  ate = sqlite3_mp
354a3 72 69 6e 74 66 28 0a 20 20 20 20 22 44 52 4f 50  rintf(.    "DROP
354a4 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f   TABLE '%q'.'%q_
354a5 6e 6f 64 65 27 3b 22 0a 20 20 20 20 22 44 52 4f  node';".    "DRO
354a6 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71  P TABLE '%q'.'%q
354a7 5f 72 6f 77 69 64 27 3b 22 0a 20 20 20 20 22 44  _rowid';".    "D
354a8 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27  ROP TABLE '%q'.'
354a9 25 71 5f 70 61 72 65 6e 74 27 3b 22 2c 0a 20 20  %q_parent';",.  
354aa 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70    pRtree->zDb, p
354ab 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20  Rtree->zName, . 
354ac 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20     pRtree->zDb, 
354ad 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 0a 20  pRtree->zName,. 
354ae 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20     pRtree->zDb, 
354af 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 0a 20 20  pRtree->zName.  
354b0 29 3b 0a 20 20 69 66 28 20 21 7a 43 72 65 61 74  );.  if( !zCreat
354b1 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  e ){.    rc = SQ
354b2 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
354b3 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  lse{.    rc = sq
354b4 6c 69 74 65 33 5f 65 78 65 63 28 70 52 74 72 65  lite3_exec(pRtre
354b5 65 2d 3e 64 62 2c 20 7a 43 72 65 61 74 65 2c 20  e->db, zCreate, 
354b6 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  0, 0, 0);.    sq
354b7 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61  lite3_free(zCrea
354b8 74 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  te);.  }.  if( r
354b9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
354ba 20 20 20 20 72 74 72 65 65 52 65 6c 65 61 73 65      rtreeRelease
354bb 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 0a 20  (pRtree);.  }.. 
354bc 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
354bd 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
354be 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
354bf 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 2e 0a 2a   xOpen method..*
354c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
354c1 65 65 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  eeOpen(sqlite3_v
354c2 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
354c3 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
354c4 2a 2a 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 69  **ppCursor){.  i
354c5 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  nt rc = SQLITE_N
354c6 4f 4d 45 4d 3b 0a 20 20 52 74 72 65 65 43 75 72  OMEM;.  RtreeCur
354c7 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20 20 70 43  sor *pCsr;..  pC
354c8 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f  sr = (RtreeCurso
354c9 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
354ca 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43  oc(sizeof(RtreeC
354cb 75 72 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 70  ursor));.  if( p
354cc 43 73 72 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  Csr ){.    memse
354cd 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f  t(pCsr, 0, sizeo
354ce 66 28 52 74 72 65 65 43 75 72 73 6f 72 29 29 3b  f(RtreeCursor));
354cf 0a 20 20 20 20 70 43 73 72 2d 3e 62 61 73 65 2e  .    pCsr->base.
354d0 70 56 74 61 62 20 3d 20 70 56 54 61 62 3b 0a 20  pVtab = pVTab;. 
354d1 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
354d2 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 43 75 72 73  K;.  }.  *ppCurs
354d3 6f 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74  or = (sqlite3_vt
354d4 61 62 5f 63 75 72 73 6f 72 20 2a 29 70 43 73 72  ab_cursor *)pCsr
354d5 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  ;..  return rc;.
354d6 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
354d7 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
354d8 64 75 6c 65 20 78 43 6c 6f 73 65 20 6d 65 74 68  dule xClose meth
354d9 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
354da 74 20 72 74 72 65 65 43 6c 6f 73 65 28 73 71 6c  t rtreeClose(sql
354db 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
354dc 20 2a 63 75 72 29 7b 0a 20 20 52 74 72 65 65 20   *cur){.  Rtree 
354dd 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
354de 20 2a 29 28 63 75 72 2d 3e 70 56 74 61 62 29 3b   *)(cur->pVtab);
354df 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72  .  int rc;.  Rtr
354e0 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
354e1 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
354e2 63 75 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  cur;.  sqlite3_f
354e3 72 65 65 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74  ree(pCsr->aConst
354e4 72 61 69 6e 74 29 3b 0a 20 20 72 63 20 3d 20 6e  raint);.  rc = n
354e5 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
354e6 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b  e, pCsr->pNode);
354e7 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
354e8 70 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  pCsr);.  return 
354e9 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72  rc;.}../*.** Rtr
354ea 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ee virtual table
354eb 20 6d 6f 64 75 6c 65 20 78 45 6f 66 20 6d 65 74   module xEof met
354ec 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  hod..**.** Retur
354ed 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68  n non-zero if th
354ee 65 20 63 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f  e cursor does no
354ef 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  t currently poin
354f0 74 20 74 6f 20 61 20 76 61 6c 69 64 20 0a 2a 2a  t to a valid .**
354f1 20 72 65 63 6f 72 64 20 28 69 2e 65 20 69 66 20   record (i.e if 
354f2 74 68 65 20 73 63 61 6e 20 68 61 73 20 66 69 6e  the scan has fin
354f3 69 73 68 65 64 29 2c 20 6f 72 20 7a 65 72 6f 20  ished), or zero 
354f4 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
354f5 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 45 6f  atic int rtreeEo
354f6 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  f(sqlite3_vtab_c
354f7 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52  ursor *cur){.  R
354f8 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72  treeCursor *pCsr
354f9 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
354fa 2a 29 63 75 72 3b 0a 20 20 72 65 74 75 72 6e 20  *)cur;.  return 
354fb 28 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 30 29  (pCsr->pNode==0)
354fc 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 43 75 72 73  ;.}../* .** Curs
354fd 6f 72 20 70 43 75 72 73 6f 72 20 63 75 72 72 65  or pCursor curre
354fe 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 61  ntly points to a
354ff 20 63 65 6c 6c 20 69 6e 20 61 20 6e 6f 6e 2d 6c   cell in a non-l
35500 65 61 66 20 70 61 67 65 2e 0a 2a 2a 20 52 65 74  eaf page..** Ret
35501 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
35502 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
35503 62 79 20 74 68 65 20 63 65 6c 6c 20 69 73 20 66  by the cell is f
35504 69 6c 74 65 72 65 64 0a 2a 2a 20 28 65 78 63 6c  iltered.** (excl
35505 75 64 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e  uded) by the con
35506 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20  straints in the 
35507 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72  pCursor->aConstr
35508 61 69 6e 74 5b 5d 20 0a 2a 2a 20 61 72 72 61 79  aint[] .** array
35509 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
3550a 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
3550b 69 6e 74 20 74 65 73 74 52 74 72 65 65 43 65 6c  int testRtreeCel
3550c 6c 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  l(Rtree *pRtree,
3550d 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
3550e 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43  ursor){.  RtreeC
3550f 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20  ell cell;.  int 
35510 69 69 3b 0a 20 20 69 6e 74 20 62 52 65 73 20 3d  ii;.  int bRes =
35511 20 30 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65   0;..  nodeGetCe
35512 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  ll(pRtree, pCurs
35513 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73  or->pNode, pCurs
35514 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  or->iCell, &cell
35515 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 62  );.  for(ii=0; b
35516 52 65 73 3d 3d 30 20 26 26 20 69 69 3c 70 43 75  Res==0 && ii<pCu
35517 72 73 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e  rsor->nConstrain
35518 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74  t; ii++){.    Rt
35519 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70  reeConstraint *p
3551a 20 3d 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f   = &pCursor->aCo
3551b 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20  nstraint[ii];.  
3551c 20 20 64 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 69    double cell_mi
3551d 6e 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e  n = DCOORD(cell.
3551e 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72  aCoord[(p->iCoor
3551f 64 3e 3e 31 29 2a 32 5d 29 3b 0a 20 20 20 20 64  d>>1)*2]);.    d
35520 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 61 78 20 3d  ouble cell_max =
35521 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f   DCOORD(cell.aCo
35522 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e  ord[(p->iCoord>>
35523 31 29 2a 32 2b 31 5d 29 3b 0a 0a 20 20 20 20 61  1)*2+1]);..    a
35524 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52  ssert(p->op==RTR
35525 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_LE || p->op==
35526 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f  RTREE_LT || p->o
35527 70 3d 3d 52 54 52 45 45 5f 47 45 20 0a 20 20 20  p==RTREE_GE .   
35528 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52       || p->op==R
35529 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70  TREE_GT || p->op
3552a 3d 3d 52 54 52 45 45 5f 45 51 0a 20 20 20 20 29  ==RTREE_EQ.    )
3552b 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  ;..    switch( p
3552c 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61  ->op ){.      ca
3552d 73 65 20 52 54 52 45 45 5f 4c 45 3a 20 63 61 73  se RTREE_LE: cas
3552e 65 20 52 54 52 45 45 5f 4c 54 3a 20 62 52 65 73  e RTREE_LT: bRes
3552f 20 3d 20 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c   = p->rValue<cel
35530 6c 5f 6d 69 6e 3b 20 62 72 65 61 6b 3b 0a 20 20  l_min; break;.  
35531 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 47      case RTREE_G
35532 45 3a 20 63 61 73 65 20 52 54 52 45 45 5f 47 54  E: case RTREE_GT
35533 3a 20 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c  : bRes = p->rVal
35534 75 65 3e 63 65 6c 6c 5f 6d 61 78 3b 20 62 72 65  ue>cell_max; bre
35535 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52  ak;.      case R
35536 54 52 45 45 5f 45 51 3a 20 0a 20 20 20 20 20 20  TREE_EQ: .      
35537 20 20 62 52 65 73 20 3d 20 28 70 2d 3e 72 56 61    bRes = (p->rVa
35538 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78 20 7c 7c 20  lue>cell_max || 
35539 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d  p->rValue<cell_m
3553a 69 6e 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  in);.        bre
3553b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ak;.    }.  }.. 
3553c 20 72 65 74 75 72 6e 20 62 52 65 73 3b 0a 7d 0a   return bRes;.}.
3553d 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./* .** Return t
3553e 72 75 65 20 69 66 20 74 68 65 20 63 65 6c 6c 20  rue if the cell 
3553f 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72  that cursor pCur
35540 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  sor currently po
35541 69 6e 74 73 20 74 6f 0a 2a 2a 20 77 6f 75 6c 64  ints to.** would
35542 20 62 65 20 66 69 6c 74 65 72 65 64 20 28 65 78   be filtered (ex
35543 63 6c 75 64 65 64 29 20 62 79 20 74 68 65 20 63  cluded) by the c
35544 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68  onstraints in th
35545 65 20 0a 2a 2a 20 70 43 75 72 73 6f 72 2d 3e 61  e .** pCursor->a
35546 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72  Constraint[] arr
35547 61 79 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68  ay, or false oth
35548 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  erwise..**.** Th
35549 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
3554a 6d 65 73 20 74 68 61 74 20 74 68 65 20 63 65 6c  mes that the cel
3554b 6c 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c  l is part of a l
3554c 65 61 66 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  eaf node..*/.sta
3554d 74 69 63 20 69 6e 74 20 74 65 73 74 52 74 72 65  tic int testRtre
3554e 65 45 6e 74 72 79 28 52 74 72 65 65 20 2a 70 52  eEntry(Rtree *pR
3554f 74 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f  tree, RtreeCurso
35550 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52  r *pCursor){.  R
35551 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
35552 20 69 6e 74 20 69 69 3b 0a 0a 20 20 6e 6f 64 65   int ii;..  node
35553 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
35554 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
35555 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20  pCursor->iCell, 
35556 26 63 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69  &cell);.  for(ii
35557 3d 30 3b 20 69 69 3c 70 43 75 72 73 6f 72 2d 3e  =0; ii<pCursor->
35558 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b  nConstraint; ii+
35559 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f 6e  +){.    RtreeCon
3555a 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 43  straint *p = &pC
3555b 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69  ursor->aConstrai
3555c 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75 62  nt[ii];.    doub
3555d 6c 65 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52  le coord = DCOOR
3555e 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 70 2d  D(cell.aCoord[p-
3555f 3e 69 43 6f 6f 72 64 5d 29 3b 0a 20 20 20 20 69  >iCoord]);.    i
35560 6e 74 20 72 65 73 3b 0a 20 20 20 20 61 73 73 65  nt res;.    asse
35561 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  rt(p->op==RTREE_
35562 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  LE || p->op==RTR
35563 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_LT || p->op==
35564 52 54 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20  RTREE_GE .      
35565 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45    || p->op==RTRE
35566 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52  E_GT || p->op==R
35567 54 52 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 20  TREE_EQ.    );. 
35568 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70     switch( p->op
35569 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52   ){.      case R
3556a 54 52 45 45 5f 4c 45 3a 20 72 65 73 20 3d 20 28  TREE_LE: res = (
3556b 63 6f 6f 72 64 3c 3d 70 2d 3e 72 56 61 6c 75 65  coord<=p->rValue
3556c 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
3556d 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a 20 72  case RTREE_LT: r
3556e 65 73 20 3d 20 28 63 6f 6f 72 64 3c 70 2d 3e 72  es = (coord<p->r
3556f 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0a  Value);  break;.
35570 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45        case RTREE
35571 5f 47 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72  _GE: res = (coor
35572 64 3e 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62  d>=p->rValue); b
35573 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
35574 20 52 54 52 45 45 5f 47 54 3a 20 72 65 73 20 3d   RTREE_GT: res =
35575 20 28 63 6f 6f 72 64 3e 70 2d 3e 72 56 61 6c 75   (coord>p->rValu
35576 65 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  e);  break;.    
35577 20 20 63 61 73 65 20 52 54 52 45 45 5f 45 51 3a    case RTREE_EQ:
35578 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3d 3d 70   res = (coord==p
35579 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b  ->rValue); break
3557a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
3557b 20 21 72 65 73 20 29 20 72 65 74 75 72 6e 20 31   !res ) return 1
3557c 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
3557d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 73  0;.}../*.** Curs
3557e 6f 72 20 70 43 75 72 73 6f 72 20 63 75 72 72 65  or pCursor curre
3557f 6e 74 6c 79 20 70 6f 69 6e 74 73 20 61 74 20 61  ntly points at a
35580 20 6e 6f 64 65 20 74 68 61 74 20 68 65 61 64 73   node that heads
35581 20 61 20 73 75 62 2d 74 72 65 65 20 6f 66 0a 2a   a sub-tree of.*
35582 2a 20 68 65 69 67 68 74 20 69 48 65 69 67 68 74  * height iHeight
35583 20 28 69 66 20 69 48 65 69 67 68 74 3d 3d 30 2c   (if iHeight==0,
35584 20 74 68 65 6e 20 74 68 65 20 6e 6f 64 65 20 69   then the node i
35585 73 20 61 20 6c 65 61 66 29 2e 20 44 65 73 63 65  s a leaf). Desce
35586 6e 64 0a 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 74  nd.** to point t
35587 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  o the left-most 
35588 63 65 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 2d  cell of the sub-
35589 74 72 65 65 20 74 68 61 74 20 6d 61 74 63 68 65  tree that matche
3558a 73 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 66 69 67  s the .** config
3558b 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73  ured constraints
3558c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3558d 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 0a 20  descendToCell(. 
3558e 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
3558f 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
35590 70 43 75 72 73 6f 72 2c 20 0a 20 20 69 6e 74 20  pCursor, .  int 
35591 69 48 65 69 67 68 74 2c 0a 20 20 69 6e 74 20 2a  iHeight,.  int *
35592 70 45 6f 66 20 20 20 20 20 20 20 20 20 20 20 20  pEof            
35593 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74       /* OUT: Set
35594 20 74 6f 20 74 72 75 65 20 69 66 20 63 61 6e 6e   to true if cann
35595 6f 74 20 64 65 73 63 65 6e 64 20 2a 2f 0a 29 7b  ot descend */.){
35596 0a 20 20 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20  .  int isEof;.  
35597 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69  int rc;.  int ii
35598 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ;.  RtreeNode *p
35599 43 68 69 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33  Child;.  sqlite3
3559a 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 3b 0a 0a  _int64 iRowid;..
3559b 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 53 61    RtreeNode *pSa
3559c 76 65 64 4e 6f 64 65 20 3d 20 70 43 75 72 73 6f  vedNode = pCurso
3559d 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20 69 6e 74 20  r->pNode;.  int 
3559e 69 53 61 76 65 64 43 65 6c 6c 20 3d 20 70 43 75  iSavedCell = pCu
3559f 72 73 6f 72 2d 3e 69 43 65 6c 6c 3b 0a 0a 20 20  rsor->iCell;..  
355a0 61 73 73 65 72 74 28 20 69 48 65 69 67 68 74 3e  assert( iHeight>
355a1 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 69 48 65  =0 );..  if( iHe
355a2 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ight==0 ){.    i
355a3 73 45 6f 66 20 3d 20 74 65 73 74 52 74 72 65 65  sEof = testRtree
355a4 45 6e 74 72 79 28 70 52 74 72 65 65 2c 20 70 43  Entry(pRtree, pC
355a5 75 72 73 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  ursor);.  }else{
355a6 0a 20 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73  .    isEof = tes
355a7 74 52 74 72 65 65 43 65 6c 6c 28 70 52 74 72 65  tRtreeCell(pRtre
355a8 65 2c 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  e, pCursor);.  }
355a9 0a 20 20 69 66 28 20 69 73 45 6f 66 20 7c 7c 20  .  if( isEof || 
355aa 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20  iHeight==0 ){.  
355ab 20 20 2a 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b    *pEof = isEof;
355ac 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
355ad 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 52  TE_OK;.  }..  iR
355ae 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
355af 77 69 64 28 70 52 74 72 65 65 2c 20 70 43 75 72  wid(pRtree, pCur
355b0 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72  sor->pNode, pCur
355b1 73 6f 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 72  sor->iCell);.  r
355b2 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
355b3 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20  pRtree, iRowid, 
355b4 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
355b5 26 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20  &pChild);.  if( 
355b6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
355b7 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
355b8 20 20 7d 0a 0a 20 20 6e 6f 64 65 52 65 6c 65 61    }..  nodeRelea
355b9 73 65 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  se(pRtree, pCurs
355ba 6f 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 70 43  or->pNode);.  pC
355bb 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d 20 70  ursor->pNode = p
355bc 43 68 69 6c 64 3b 0a 20 20 69 73 45 6f 66 20 3d  Child;.  isEof =
355bd 20 31 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   1;.  for(ii=0; 
355be 69 73 45 6f 66 20 26 26 20 69 69 3c 4e 43 45 4c  isEof && ii<NCEL
355bf 4c 28 70 43 68 69 6c 64 29 3b 20 69 69 2b 2b 29  L(pChild); ii++)
355c0 7b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69  {.    pCursor->i
355c1 43 65 6c 6c 20 3d 20 69 69 3b 0a 20 20 20 20 72  Cell = ii;.    r
355c2 63 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c  c = descendToCel
355c3 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f  l(pRtree, pCurso
355c4 72 2c 20 69 48 65 69 67 68 74 2d 31 2c 20 26 69  r, iHeight-1, &i
355c5 73 45 6f 66 29 3b 0a 20 20 20 20 69 66 28 20 72  sEof);.    if( r
355c6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
355c7 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
355c8 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
355c9 28 20 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 61  ( isEof ){.    a
355ca 73 73 65 72 74 28 20 70 43 75 72 73 6f 72 2d 3e  ssert( pCursor->
355cb 70 4e 6f 64 65 3d 3d 70 43 68 69 6c 64 20 29 3b  pNode==pChild );
355cc 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
355cd 63 65 28 70 53 61 76 65 64 4e 6f 64 65 29 3b 0a  ce(pSavedNode);.
355ce 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
355cf 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 29 3b  pRtree, pChild);
355d0 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e  .    pCursor->pN
355d1 6f 64 65 20 3d 20 70 53 61 76 65 64 4e 6f 64 65  ode = pSavedNode
355d2 3b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69  ;.    pCursor->i
355d3 43 65 6c 6c 20 3d 20 69 53 61 76 65 64 43 65 6c  Cell = iSavedCel
355d4 6c 3b 0a 20 20 7d 0a 0a 20 20 2a 70 45 6f 66 20  l;.  }..  *pEof 
355d5 3d 20 69 73 45 6f 66 3b 0a 20 20 72 65 74 75 72  = isEof;.  retur
355d6 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
355d7 2f 2a 0a 2a 2a 20 4f 6e 65 20 6f 66 20 74 68 65  /*.** One of the
355d8 20 63 65 6c 6c 73 20 69 6e 20 6e 6f 64 65 20 70   cells in node p
355d9 4e 6f 64 65 20 69 73 20 67 75 61 72 61 6e 74 65  Node is guarante
355da 65 64 20 74 6f 20 68 61 76 65 20 61 20 36 34 2d  ed to have a 64-
355db 62 69 74 20 0a 2a 2a 20 69 6e 74 65 67 65 72 20  bit .** integer 
355dc 76 61 6c 75 65 20 65 71 75 61 6c 20 74 6f 20 69  value equal to i
355dd 52 6f 77 69 64 2e 20 52 65 74 75 72 6e 20 74 68  Rowid. Return th
355de 65 20 69 6e 64 65 78 20 6f 66 20 74 68 69 73 20  e index of this 
355df 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cell..*/.static 
355e0 69 6e 74 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  int nodeRowidInd
355e1 65 78 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ex(Rtree *pRtree
355e2 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
355e3 64 65 2c 20 69 36 34 20 69 52 6f 77 69 64 29 7b  de, i64 iRowid){
355e4 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72  .  int ii;.  for
355e5 28 69 69 3d 30 3b 20 6e 6f 64 65 47 65 74 52 6f  (ii=0; nodeGetRo
355e6 77 69 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  wid(pRtree, pNod
355e7 65 2c 20 69 69 29 21 3d 69 52 6f 77 69 64 3b 20  e, ii)!=iRowid; 
355e8 69 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72  ii++){.    asser
355e9 74 28 20 69 69 3c 28 4e 43 45 4c 4c 28 70 4e 6f  t( ii<(NCELL(pNo
355ea 64 65 29 2d 31 29 20 29 3b 0a 20 20 7d 0a 20 20  de)-1) );.  }.  
355eb 72 65 74 75 72 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a  return ii;.}../*
355ec 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69  .** Return the i
355ed 6e 64 65 78 20 6f 66 20 74 68 65 20 63 65 6c 6c  ndex of the cell
355ee 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f   containing a po
355ef 69 6e 74 65 72 20 74 6f 20 6e 6f 64 65 20 70 4e  inter to node pN
355f0 6f 64 65 0a 2a 2a 20 69 6e 20 69 74 73 20 70 61  ode.** in its pa
355f1 72 65 6e 74 2e 20 49 66 20 70 4e 6f 64 65 20 69  rent. If pNode i
355f2 73 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2c  s the root node,
355f3 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 73   return -1..*/.s
355f4 74 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 50 61  tatic int nodePa
355f5 72 65 6e 74 49 6e 64 65 78 28 52 74 72 65 65 20  rentIndex(Rtree 
355f6 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
355f7 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74  de *pNode){.  Rt
355f8 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
355f9 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
355fa 74 3b 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74  t;.  if( pParent
355fb 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e   ){.    return n
355fc 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 70 52  odeRowidIndex(pR
355fd 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 70  tree, pParent, p
355fe 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
355ff 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d  }.  return -1;.}
35600 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
35601 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
35602 75 6c 65 20 78 4e 65 78 74 20 6d 65 74 68 6f 64  ule xNext method
35603 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35604 72 74 72 65 65 4e 65 78 74 28 73 71 6c 69 74 65  rtreeNext(sqlite
35605 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
35606 56 74 61 62 43 75 72 73 6f 72 29 7b 0a 20 20 52  VtabCursor){.  R
35607 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
35608 52 74 72 65 65 20 2a 29 28 70 56 74 61 62 43 75  Rtree *)(pVtabCu
35609 72 73 6f 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20  rsor->pVtab);.  
3560a 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
3560b 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
3560c 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a   *)pVtabCursor;.
3560d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3560e 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 43 73  E_OK;..  if( pCs
3560f 72 2d 3e 69 53 74 72 61 74 65 67 79 3d 3d 31 20  r->iStrategy==1 
35610 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 22  ){.    /* This "
35611 73 63 61 6e 22 20 69 73 20 61 20 64 69 72 65 63  scan" is a direc
35612 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69  t lookup by rowi
35613 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e  d. There is no n
35614 65 78 74 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20  ext entry. */.  
35615 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
35616 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64  tree, pCsr->pNod
35617 65 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e  e);.    pCsr->pN
35618 6f 64 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  ode = 0;.  }..  
35619 65 6c 73 65 20 69 66 28 20 70 43 73 72 2d 3e 70  else if( pCsr->p
3561a 4e 6f 64 65 20 29 7b 0a 20 20 20 20 2f 2a 20 4d  Node ){.    /* M
3561b 6f 76 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ove to the next 
3561c 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
3561d 65 73 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65  es the configure
3561e 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a  d constraints. *
3561f 2f 0a 20 20 20 20 69 6e 74 20 69 48 65 69 67 68  /.    int iHeigh
35620 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  t = 0;.    while
35621 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 29 7b  ( pCsr->pNode ){
35622 0a 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65  .      RtreeNode
35623 20 2a 70 4e 6f 64 65 20 3d 20 70 43 73 72 2d 3e   *pNode = pCsr->
35624 70 4e 6f 64 65 3b 0a 20 20 20 20 20 20 69 6e 74  pNode;.      int
35625 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
35626 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 66 6f 72  Node);.      for
35627 28 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 3b 20  (pCsr->iCell++; 
35628 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c  pCsr->iCell<nCel
35629 6c 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b  l; pCsr->iCell++
3562a 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
3562b 73 45 6f 66 3b 0a 20 20 20 20 20 20 20 20 72 63  sEof;.        rc
3562c 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c   = descendToCell
3562d 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c 20 69  (pRtree, pCsr, i
3562e 48 65 69 67 68 74 2c 20 26 69 73 45 6f 66 29 3b  Height, &isEof);
3562f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
35630 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 69  =SQLITE_OK || !i
35631 73 45 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20  sEof ){.        
35632 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
35633 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
35634 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
35635 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
35636 74 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69  t;.      pCsr->i
35637 43 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e  Cell = nodeParen
35638 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70  tIndex(pRtree, p
35639 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Node);.      nod
3563a 65 52 65 66 65 72 65 6e 63 65 28 70 43 73 72 2d  eReference(pCsr-
3563b 3e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e  >pNode);.      n
3563c 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
3563d 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e, pNode);.     
3563e 20 69 48 65 69 67 68 74 2b 2b 3b 0a 20 20 20 20   iHeight++;.    
3563f 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
35640 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  rc;.}../* .** Rt
35641 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
35642 65 20 6d 6f 64 75 6c 65 20 78 52 6f 77 69 64 20  e module xRowid 
35643 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69  method..*/.stati
35644 63 20 69 6e 74 20 72 74 72 65 65 52 6f 77 69 64  c int rtreeRowid
35645 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
35646 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f  rsor *pVtabCurso
35647 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  r, sqlite_int64 
35648 2a 70 52 6f 77 69 64 29 7b 0a 20 20 52 74 72 65  *pRowid){.  Rtre
35649 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72  e *pRtree = (Rtr
3564a 65 65 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72  ee *)pVtabCursor
3564b 2d 3e 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65  ->pVtab;.  Rtree
3564c 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
3564d 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56  RtreeCursor *)pV
3564e 74 61 62 43 75 72 73 6f 72 3b 0a 0a 20 20 61 73  tabCursor;..  as
3564f 73 65 72 74 28 70 43 73 72 2d 3e 70 4e 6f 64 65  sert(pCsr->pNode
35650 29 3b 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 6e  );.  *pRowid = n
35651 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72  odeGetRowid(pRtr
35652 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c  ee, pCsr->pNode,
35653 20 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 0a   pCsr->iCell);..
35654 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
35655 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  OK;.}../* .** Rt
35656 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
35657 65 20 6d 6f 64 75 6c 65 20 78 43 6f 6c 75 6d 6e  e module xColumn
35658 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
35659 69 63 20 69 6e 74 20 72 74 72 65 65 43 6f 6c 75  ic int rtreeColu
3565a 6d 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  mn(sqlite3_vtab_
3565b 63 75 72 73 6f 72 20 2a 63 75 72 2c 20 73 71 6c  cursor *cur, sql
3565c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74  ite3_context *ct
3565d 78 2c 20 69 6e 74 20 69 29 7b 0a 20 20 52 74 72  x, int i){.  Rtr
3565e 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
3565f 72 65 65 20 2a 29 63 75 72 2d 3e 70 56 74 61 62  ree *)cur->pVtab
35660 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20  ;.  RtreeCursor 
35661 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75  *pCsr = (RtreeCu
35662 72 73 6f 72 20 2a 29 63 75 72 3b 0a 0a 20 20 69  rsor *)cur;..  i
35663 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 69  f( i==0 ){.    i
35664 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  64 iRowid = node
35665 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
35666 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43   pCsr->pNode, pC
35667 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 20 20  sr->iCell);.    
35668 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
35669 6e 74 36 34 28 63 74 78 2c 20 69 52 6f 77 69 64  nt64(ctx, iRowid
3566a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
3566b 52 74 72 65 65 43 6f 6f 72 64 20 63 3b 0a 20 20  RtreeCoord c;.  
3566c 20 20 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70    nodeGetCoord(p
3566d 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f  Rtree, pCsr->pNo
3566e 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2c  de, pCsr->iCell,
3566f 20 69 2d 31 2c 20 26 63 29 3b 0a 20 20 20 20 69   i-1, &c);.    i
35670 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72  f( pRtree->eCoor
35671 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f  dType==RTREE_COO
35672 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20  RD_REAL32 ){.   
35673 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
35674 74 5f 64 6f 75 62 6c 65 28 63 74 78 2c 20 63 2e  t_double(ctx, c.
35675 66 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  f);.    }else{. 
35676 20 20 20 20 20 61 73 73 65 72 74 28 20 70 52 74       assert( pRt
35677 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
35678 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  =RTREE_COORD_INT
35679 33 32 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  32 );.      sqli
3567a 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
3567b 74 78 2c 20 63 2e 69 29 3b 0a 20 20 20 20 7d 0a  tx, c.i);.    }.
3567c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
3567d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a  LITE_OK;.}../* .
3567e 2a 2a 20 55 73 65 20 6e 6f 64 65 41 63 71 75 69  ** Use nodeAcqui
3567f 72 65 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 74  re() to obtain t
35680 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 63 6f 6e  he leaf node con
35681 74 61 69 6e 69 6e 67 20 74 68 65 20 72 65 63 6f  taining the reco
35682 72 64 20 77 69 74 68 20 0a 2a 2a 20 72 6f 77 69  rd with .** rowi
35683 64 20 69 52 6f 77 69 64 2e 20 49 66 20 73 75 63  d iRowid. If suc
35684 63 65 73 73 66 75 6c 2c 20 73 65 74 20 2a 70 70  cessful, set *pp
35685 4c 65 61 66 20 74 6f 20 70 6f 69 6e 74 20 74 6f  Leaf to point to
35686 20 74 68 65 20 6e 6f 64 65 20 61 6e 64 0a 2a 2a   the node and.**
35687 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35688 4b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  K. If there is n
35689 6f 20 73 75 63 68 20 72 65 63 6f 72 64 20 69 6e  o such record in
3568a 20 74 68 65 20 74 61 62 6c 65 2c 20 73 65 74 0a   the table, set.
3568b 2a 2a 20 2a 70 70 4c 65 61 66 20 74 6f 20 30 20  ** *ppLeaf to 0 
3568c 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
3568d 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f  E_OK. If an erro
3568e 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 2a 70  r occurs, set *p
3568f 70 4c 65 61 66 0a 2a 2a 20 74 6f 20 7a 65 72 6f  pLeaf.** to zero
35690 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 53   and return an S
35691 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
35692 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35693 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 52 74 72  findLeafNode(Rtr
35694 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20  ee *pRtree, i64 
35695 69 52 6f 77 69 64 2c 20 52 74 72 65 65 4e 6f 64  iRowid, RtreeNod
35696 65 20 2a 2a 70 70 4c 65 61 66 29 7b 0a 20 20 69  e **ppLeaf){.  i
35697 6e 74 20 72 63 3b 0a 20 20 2a 70 70 4c 65 61 66  nt rc;.  *ppLeaf
35698 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
35699 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
3569a 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31  e->pReadRowid, 1
3569b 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 69 66 28  , iRowid);.  if(
3569c 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
3569d 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
3569e 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
3569f 0a 20 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d  .    i64 iNode =
356a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
356a1 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
356a2 65 61 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20  eadRowid, 0);.  
356a3 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69    rc = nodeAcqui
356a4 72 65 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65  re(pRtree, iNode
356a5 2c 20 30 2c 20 70 70 4c 65 61 66 29 3b 0a 20 20  , 0, ppLeaf);.  
356a6 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
356a7 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
356a8 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  id);.  }else{.  
356a9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
356aa 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
356ab 61 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 20 20  adRowid);.  }.  
356ac 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
356ad 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
356ae 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
356af 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e   xFilter method.
356b0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
356b1 74 72 65 65 46 69 6c 74 65 72 28 0a 20 20 73 71  treeFilter(.  sq
356b2 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
356b3 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20  r *pVtabCursor, 
356b4 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63  .  int idxNum, c
356b5 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74  onst char *idxSt
356b6 72 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73  r,.  int argc, s
356b7 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
356b8 72 67 76 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a  rgv.){.  Rtree *
356b9 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
356ba 2a 29 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  *)pVtabCursor->p
356bb 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72  Vtab;.  RtreeCur
356bc 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72  sor *pCsr = (Rtr
356bd 65 65 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62  eeCursor *)pVtab
356be 43 75 72 73 6f 72 3b 0a 0a 20 20 52 74 72 65 65  Cursor;..  Rtree
356bf 4e 6f 64 65 20 2a 70 52 6f 6f 74 20 3d 20 30 3b  Node *pRoot = 0;
356c0 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
356c1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
356c2 0a 0a 20 20 72 74 72 65 65 52 65 66 65 72 65 6e  ..  rtreeReferen
356c3 63 65 28 70 52 74 72 65 65 29 3b 0a 0a 20 20 73  ce(pRtree);..  s
356c4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
356c5 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a  ->aConstraint);.
356c6 20 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61    pCsr->aConstra
356c7 69 6e 74 20 3d 20 30 3b 0a 20 20 70 43 73 72 2d  int = 0;.  pCsr-
356c8 3e 69 53 74 72 61 74 65 67 79 20 3d 20 69 64 78  >iStrategy = idx
356c9 4e 75 6d 3b 0a 0a 20 20 69 66 28 20 69 64 78 4e  Num;..  if( idxN
356ca 75 6d 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  um==1 ){.    /* 
356cb 53 70 65 63 69 61 6c 20 63 61 73 65 20 2d 20 6c  Special case - l
356cc 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20  ookup by rowid. 
356cd 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  */.    RtreeNode
356ce 20 2a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20   *pLeaf;        
356cf 2f 2a 20 4c 65 61 66 20 6f 6e 20 77 68 69 63 68  /* Leaf on which
356d0 20 74 68 65 20 72 65 71 75 69 72 65 64 20 63 65   the required ce
356d1 6c 6c 20 72 65 73 69 64 65 73 20 2a 2f 0a 20 20  ll resides */.  
356d2 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20 73    i64 iRowid = s
356d3 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
356d4 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  64(argv[0]);.   
356d5 20 72 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f   rc = findLeafNo
356d6 64 65 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69  de(pRtree, iRowi
356d7 64 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20  d, &pLeaf);.    
356d8 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4c  pCsr->pNode = pL
356d9 65 61 66 3b 20 0a 20 20 20 20 69 66 28 20 70 4c  eaf; .    if( pL
356da 65 61 66 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  eaf && rc==SQLIT
356db 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43  E_OK ){.      pC
356dc 73 72 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  sr->iCell = node
356dd 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72 65  RowidIndex(pRtre
356de 65 2c 20 70 4c 65 61 66 2c 20 69 52 6f 77 69 64  e, pLeaf, iRowid
356df 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
356e0 7b 0a 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20  {.    /* Normal 
356e1 63 61 73 65 20 2d 20 72 2d 74 72 65 65 20 73 63  case - r-tree sc
356e2 61 6e 2e 20 53 65 74 20 75 70 20 74 68 65 20 52  an. Set up the R
356e3 74 72 65 65 43 75 72 73 6f 72 2e 61 43 6f 6e 73  treeCursor.aCons
356e4 74 72 61 69 6e 74 20 61 72 72 61 79 20 0a 20 20  traint array .  
356e5 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 63 6f    ** with the co
356e6 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61  nfigured constra
356e7 69 6e 74 73 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  ints. .    */.  
356e8 20 20 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a    if( argc>0 ){.
356e9 20 20 20 20 20 20 70 43 73 72 2d 3e 61 43 6f 6e        pCsr->aCon
356ea 73 74 72 61 69 6e 74 20 3d 20 73 71 6c 69 74 65  straint = sqlite
356eb 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
356ec 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 29  RtreeConstraint)
356ed 2a 61 72 67 63 29 3b 0a 20 20 20 20 20 20 70 43  *argc);.      pC
356ee 73 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20  sr->nConstraint 
356ef 3d 20 61 72 67 63 3b 0a 20 20 20 20 20 20 69 66  = argc;.      if
356f0 28 20 21 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72  ( !pCsr->aConstr
356f1 61 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  aint ){.        
356f2 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
356f3 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  M;.      }else{.
356f4 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
356f5 28 69 64 78 53 74 72 3d 3d 30 20 26 26 20 61 72  (idxStr==0 && ar
356f6 67 63 3d 3d 30 29 20 7c 7c 20 73 74 72 6c 65 6e  gc==0) || strlen
356f7 28 69 64 78 53 74 72 29 3d 3d 61 72 67 63 2a 32  (idxStr)==argc*2
356f8 20 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   );.        for(
356f9 69 69 3d 30 3b 20 69 69 3c 61 72 67 63 3b 20 69  ii=0; ii<argc; i
356fa 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
356fb 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20  RtreeConstraint 
356fc 2a 70 20 3d 20 26 70 43 73 72 2d 3e 61 43 6f 6e  *p = &pCsr->aCon
356fd 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  straint[ii];.   
356fe 20 20 20 20 20 20 20 70 2d 3e 6f 70 20 3d 20 69         p->op = i
356ff 64 78 53 74 72 5b 69 69 2a 32 5d 3b 0a 20 20 20  dxStr[ii*2];.   
35700 20 20 20 20 20 20 20 70 2d 3e 69 43 6f 6f 72 64         p->iCoord
35701 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 2b 31   = idxStr[ii*2+1
35702 5d 2d 27 61 27 3b 0a 20 20 20 20 20 20 20 20 20  ]-'a';.         
35703 20 70 2d 3e 72 56 61 6c 75 65 20 3d 20 73 71 6c   p->rValue = sql
35704 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
35705 65 28 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20  e(argv[ii]);.   
35706 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
35707 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20     }.  .    if( 
35708 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
35709 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f  .      pCsr->pNo
3570a 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  de = 0;.      rc
3570b 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70   = nodeAcquire(p
3570c 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26 70 52  Rtree, 1, 0, &pR
3570d 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oot);.    }.    
3570e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3570f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  K ){.      int i
35710 73 45 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  sEof = 1;.      
35711 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
35712 4c 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20  L(pRoot);.      
35713 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 52  pCsr->pNode = pR
35714 6f 6f 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  oot;.      for(p
35715 43 73 72 2d 3e 69 43 65 6c 6c 3d 30 3b 20 72 63  Csr->iCell=0; rc
35716 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
35717 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c  Csr->iCell<nCell
35718 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29  ; pCsr->iCell++)
35719 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
3571a 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70  ( pCsr->pNode==p
3571b 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20  Root );.        
3571c 72 63 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65  rc = descendToCe
3571d 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c  ll(pRtree, pCsr,
3571e 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2c   pRtree->iDepth,
3571f 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20 20 20   &isEof);.      
35720 20 20 69 66 28 20 21 69 73 45 6f 66 20 29 7b 0a    if( !isEof ){.
35721 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
35722 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
35723 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d   }.      if( rc=
35724 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 73  =SQLITE_OK && is
35725 45 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Eof ){.        a
35726 73 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f  ssert( pCsr->pNo
35727 64 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20  de==pRoot );.   
35728 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65       nodeRelease
35729 28 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b  (pRtree, pRoot);
3572a 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e 70  .        pCsr->p
3572b 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Node = 0;.      
3572c 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
3572d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
3572e 20 21 70 43 73 72 2d 3e 70 4e 6f 64 65 20 7c 7c   !pCsr->pNode ||
3572f 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 4e 43 45   pCsr->iCell<NCE
35730 4c 4c 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 20  LL(pCsr->pNode) 
35731 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
35732 72 74 72 65 65 52 65 6c 65 61 73 65 28 70 52 74  rtreeRelease(pRt
35733 72 65 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ree);.  return r
35734 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65  c;.}../*.** Rtre
35735 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
35736 6d 6f 64 75 6c 65 20 78 42 65 73 74 49 6e 64 65  module xBestInde
35737 78 20 6d 65 74 68 6f 64 2e 20 54 68 65 72 65 20  x method. There 
35738 61 72 65 20 74 68 72 65 65 0a 2a 2a 20 74 61 62  are three.** tab
35739 6c 65 20 73 63 61 6e 20 73 74 72 61 74 65 67 69  le scan strategi
3573a 65 73 20 74 6f 20 63 68 6f 6f 73 65 20 66 72 6f  es to choose fro
3573b 6d 20 28 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d  m (in order from
3573c 20 6d 6f 73 74 20 74 6f 20 0a 2a 2a 20 6c 65 61   most to .** lea
3573d 73 74 20 64 65 73 69 72 61 62 6c 65 29 3a 0a 2a  st desirable):.*
3573e 2a 0a 2a 2a 20 20 20 69 64 78 4e 75 6d 20 20 20  *.**   idxNum   
3573f 20 20 69 64 78 53 74 72 20 20 20 20 20 20 20 20    idxStr        
35740 53 74 72 61 74 65 67 79 0a 2a 2a 20 20 20 2d 2d  Strategy.**   --
35741 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
35742 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
35743 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
35744 2a 20 20 20 20 20 31 20 20 20 20 20 20 20 20 55  *     1        U
35745 6e 75 73 65 64 20 20 20 20 20 20 20 20 44 69 72  nused        Dir
35746 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f  ect lookup by ro
35747 77 69 64 2e 0a 2a 2a 20 20 20 20 20 32 20 20 20  wid..**     2   
35748 20 20 20 20 20 53 65 65 20 62 65 6c 6f 77 20 20       See below  
35749 20 20 20 52 2d 74 72 65 65 20 71 75 65 72 79 2e     R-tree query.
3574a 0a 2a 2a 20 20 20 20 20 33 20 20 20 20 20 20 20  .**     3       
3574b 20 55 6e 75 73 65 64 20 20 20 20 20 20 20 20 46   Unused        F
3574c 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a  ull table scan..
3574d 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
3574e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3574f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
35750 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 49 66 20 73  -----.**.** If s
35751 74 72 61 74 65 67 79 20 31 20 6f 72 20 33 20 69  trategy 1 or 3 i
35752 73 20 75 73 65 64 2c 20 74 68 65 6e 20 69 64 78  s used, then idx
35753 53 74 72 20 69 73 20 6e 6f 74 20 6d 65 61 6e 69  Str is not meani
35754 6e 67 66 75 6c 2e 20 49 66 20 73 74 72 61 74 65  ngful. If strate
35755 67 79 0a 2a 2a 20 32 20 69 73 20 75 73 65 64 2c  gy.** 2 is used,
35756 20 69 64 78 53 74 72 20 69 73 20 66 6f 72 6d 61   idxStr is forma
35757 74 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20  tted to contain 
35758 32 20 62 79 74 65 73 20 66 6f 72 20 65 61 63 68  2 bytes for each
35759 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20   .** constraint 
3575a 75 73 65 64 2e 20 54 68 65 20 66 69 72 73 74 20  used. The first 
3575b 74 77 6f 20 62 79 74 65 73 20 6f 66 20 69 64 78  two bytes of idx
3575c 53 74 72 20 63 6f 72 72 65 73 70 6f 6e 64 20 74  Str correspond t
3575d 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72  o .** the constr
3575e 61 69 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 5f  aint in sqlite3_
3575f 69 6e 64 65 78 5f 69 6e 66 6f 2e 61 43 6f 6e 73  index_info.aCons
35760 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69  traintUsage[] wi
35761 74 68 0a 2a 2a 20 28 61 72 67 76 49 6e 64 65 78  th.** (argvIndex
35762 3d 3d 31 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20  ==1) etc..**.** 
35763 54 68 65 20 66 69 72 73 74 20 6f 66 20 65 61 63  The first of eac
35764 68 20 70 61 69 72 20 6f 66 20 62 79 74 65 73 20  h pair of bytes 
35765 69 6e 20 69 64 78 53 74 72 20 69 64 65 6e 74 69  in idxStr identi
35766 66 69 65 73 20 74 68 65 20 63 6f 6e 73 74 72 61  fies the constra
35767 69 6e 74 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  int.** operator 
35768 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
35769 2a 20 20 20 4f 70 65 72 61 74 6f 72 20 20 20 20  *   Operator    
3576a 42 79 74 65 20 56 61 6c 75 65 0a 2a 2a 20 20 20  Byte Value.**   
3576b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3576c 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 3d  ------.**      =
3576d 20 20 20 20 20 20 20 20 30 78 34 31 20 28 27 41          0x41 ('A
3576e 27 29 0a 2a 2a 20 20 20 20 20 3c 3d 20 20 20 20  ').**     <=    
3576f 20 20 20 20 30 78 34 32 20 28 27 42 27 29 0a 2a      0x42 ('B').*
35770 2a 20 20 20 20 20 20 3c 20 20 20 20 20 20 20 20  *      <        
35771 30 78 34 33 20 28 27 43 27 29 0a 2a 2a 20 20 20  0x43 ('C').**   
35772 20 20 3e 3d 20 20 20 20 20 20 20 20 30 78 34 34    >=        0x44
35773 20 28 27 44 27 29 0a 2a 2a 20 20 20 20 20 20 3e   ('D').**      >
35774 20 20 20 20 20 20 20 20 30 78 34 35 20 28 27 45          0x45 ('E
35775 27 29 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ').**   --------
35776 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
35777 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
35778 6f 66 20 65 61 63 68 20 70 61 69 72 20 6f 66 20  of each pair of 
35779 62 79 74 65 73 20 69 64 65 6e 74 69 66 69 65 73  bytes identifies
3577a 20 74 68 65 20 63 6f 6f 72 64 69 6e 61 74 65 20   the coordinate 
3577b 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 77 68 69  column.** to whi
3577c 63 68 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  ch the constrain
3577d 74 20 61 70 70 6c 69 65 73 2e 20 54 68 65 20 6c  t applies. The l
3577e 65 66 74 6d 6f 73 74 20 63 6f 6f 72 64 69 6e 61  eftmost coordina
3577f 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20  te column.** is 
35780 27 61 27 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  'a', the second 
35781 66 72 6f 6d 20 74 68 65 20 6c 65 66 74 20 27 62  from the left 'b
35782 27 20 65 74 63 2e 0a 2a 2f 0a 73 74 61 74 69 63  ' etc..*/.static
35783 20 69 6e 74 20 72 74 72 65 65 42 65 73 74 49 6e   int rtreeBestIn
35784 64 65 78 28 73 71 6c 69 74 65 33 5f 76 74 61 62  dex(sqlite3_vtab
35785 20 2a 74 61 62 2c 20 73 71 6c 69 74 65 33 5f 69   *tab, sqlite3_i
35786 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49  ndex_info *pIdxI
35787 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  nfo){.  int rc =
35788 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
35789 74 20 69 69 2c 20 63 43 6f 6c 3b 0a 0a 20 20 69  t ii, cCol;..  i
3578a 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 63  nt iIdx = 0;.  c
3578b 68 61 72 20 7a 49 64 78 53 74 72 5b 52 54 52 45  har zIdxStr[RTRE
3578c 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53  E_MAX_DIMENSIONS
3578d 2a 38 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28  *8+1];.  memset(
3578e 7a 49 64 78 53 74 72 2c 20 30 2c 20 73 69 7a 65  zIdxStr, 0, size
3578f 6f 66 28 7a 49 64 78 53 74 72 29 29 3b 0a 0a 20  of(zIdxStr));.. 
35790 20 61 73 73 65 72 74 28 20 70 49 64 78 49 6e 66   assert( pIdxInf
35791 6f 2d 3e 69 64 78 53 74 72 3d 3d 30 20 29 3b 0a  o->idxStr==0 );.
35792 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
35793 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72  IdxInfo->nConstr
35794 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  aint; ii++){.   
35795 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
35796 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
35797 20 2a 70 20 3d 20 26 70 49 64 78 49 6e 66 6f 2d   *p = &pIdxInfo-
35798 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d  >aConstraint[ii]
35799 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73  ;..    if( p->us
3579a 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75  able && p->iColu
3579b 6d 6e 3d 3d 30 20 26 26 20 70 2d 3e 6f 70 3d 3d  mn==0 && p->op==
3579c 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
3579d 53 54 52 41 49 4e 54 5f 45 51 20 29 7b 0a 20 20  STRAINT_EQ ){.  
3579e 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61      /* We have a
3579f 6e 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  n equality const
357a0 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 72 6f 77  raint on the row
357a1 69 64 2e 20 55 73 65 20 73 74 72 61 74 65 67 79  id. Use strategy
357a2 20 31 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74   1. */.      int
357a3 20 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a   jj;.      for(j
357a4 6a 3d 30 3b 20 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b  j=0; jj<ii; jj++
357a5 29 7b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49  ){.        pIdxI
357a6 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
357a7 55 73 61 67 65 5b 6a 6a 5d 2e 61 72 67 76 49 6e  Usage[jj].argvIn
357a8 64 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dex = 0;.       
357a9 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73   pIdxInfo->aCons
357aa 74 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e  traintUsage[jj].
357ab 6f 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  omit = 0;.      
357ac 7d 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f  }.      pIdxInfo
357ad 2d 3e 69 64 78 4e 75 6d 20 3d 20 31 3b 0a 20 20  ->idxNum = 1;.  
357ae 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43      pIdxInfo->aC
357af 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69  onstraintUsage[i
357b0 69 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 31  i].argvIndex = 1
357b1 3b 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f  ;.      pIdxInfo
357b2 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
357b3 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 31 3b  ge[jj].omit = 1;
357b4 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  ..      /* This 
357b5 73 74 72 61 74 65 67 79 20 69 6e 76 6f 6c 76 65  strategy involve
357b6 73 20 61 20 74 77 6f 20 72 6f 77 69 64 20 6c 6f  s a two rowid lo
357b7 6f 6b 75 70 73 20 6f 6e 20 61 6e 20 42 2d 54 72  okups on an B-Tr
357b8 65 65 20 73 74 72 75 63 74 75 72 65 73 0a 20 20  ee structures.  
357b9 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 6e 20      ** and then 
357ba 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20  a linear search 
357bb 6f 66 20 61 6e 20 52 2d 54 72 65 65 20 6e 6f 64  of an R-Tree nod
357bc 65 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  e. This should b
357bd 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73  e .      ** cons
357be 69 64 65 72 65 64 20 61 6c 6d 6f 73 74 20 61 73  idered almost as
357bf 20 71 75 69 63 6b 20 61 73 20 61 20 64 69 72 65   quick as a dire
357c0 63 74 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 20  ct rowid lookup 
357c1 28 66 6f 72 20 77 68 69 63 68 20 0a 20 20 20 20  (for which .    
357c2 20 20 2a 2a 20 73 71 6c 69 74 65 20 75 73 65 73    ** sqlite uses
357c3 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 73   an internal cos
357c4 74 20 6f 66 20 30 2e 30 29 2e 0a 20 20 20 20 20  t of 0.0)..     
357c5 20 2a 2f 20 0a 20 20 20 20 20 20 70 49 64 78 49   */ .      pIdxI
357c6 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
357c7 73 74 20 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20  st = 10.0;.     
357c8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
357c9 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  K;.    }..    if
357ca 28 20 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70  ( p->usable && p
357cb 2d 3e 69 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20  ->iColumn>0 ){. 
357cc 20 20 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 0a       u8 op = 0;.
357cd 20 20 20 20 20 20 73 77 69 74 63 68 28 20 70 2d        switch( p-
357ce 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63  >op ){.        c
357cf 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ase SQLITE_INDEX
357d0 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 3a 20  _CONSTRAINT_EQ: 
357d1 6f 70 20 3d 20 52 54 52 45 45 5f 45 51 3b 20 62  op = RTREE_EQ; b
357d2 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
357d3 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  se SQLITE_INDEX_
357d4 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 3a 20 6f  CONSTRAINT_GT: o
357d5 70 20 3d 20 52 54 52 45 45 5f 47 54 3b 20 62 72  p = RTREE_GT; br
357d6 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
357d7 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
357d8 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 3a 20 6f 70  ONSTRAINT_LE: op
357d9 20 3d 20 52 54 52 45 45 5f 4c 45 3b 20 62 72 65   = RTREE_LE; bre
357da 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
357db 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
357dc 4e 53 54 52 41 49 4e 54 5f 4c 54 3a 20 6f 70 20  NSTRAINT_LT: op 
357dd 3d 20 52 54 52 45 45 5f 4c 54 3b 20 62 72 65 61  = RTREE_LT; brea
357de 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
357df 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
357e0 53 54 52 41 49 4e 54 5f 47 45 3a 20 6f 70 20 3d  STRAINT_GE: op =
357e1 20 52 54 52 45 45 5f 47 45 3b 20 62 72 65 61 6b   RTREE_GE; break
357e2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
357e3 69 66 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20  if( op ){.      
357e4 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
357e5 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 63  his particular c
357e6 6f 6e 73 74 72 61 69 6e 74 20 68 61 73 20 6e 6f  onstraint has no
357e7 74 20 62 65 65 6e 20 75 73 65 64 20 62 65 66 6f  t been used befo
357e8 72 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49  re..        ** I
357e9 66 20 69 74 20 68 61 73 20 62 65 65 6e 20 75 73  f it has been us
357ea 65 64 20 62 65 66 6f 72 65 2c 20 69 67 6e 6f 72  ed before, ignor
357eb 65 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a  e it..        **
357ec 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3c 3d  .        ** A <=
357ed 20 6f 72 20 3c 20 63 61 6e 20 62 65 20 75 73 65   or < can be use
357ee 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  d if there is a 
357ef 70 72 69 6f 72 20 3e 3d 20 6f 72 20 3e 2e 0a 20  prior >= or >.. 
357f0 20 20 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f         ** A >= o
357f1 72 20 3e 20 63 61 6e 20 62 65 20 75 73 65 64 20  r > can be used 
357f2 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
357f3 69 6f 72 20 3c 20 6f 72 20 3c 3d 2e 0a 20 20 20  ior < or <=..   
357f4 20 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20       ** A <= or 
357f5 3c 20 69 73 20 64 69 73 71 75 61 6c 69 66 69 65  < is disqualifie
357f6 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  d if there is a 
357f7 70 72 69 6f 72 20 3c 3d 2c 20 3c 2c 20 6f 72 20  prior <=, <, or 
357f8 3d 3d 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41  ==..        ** A
357f9 20 3e 3d 20 6f 72 20 3e 20 69 73 20 64 69 73 71   >= or > is disq
357fa 75 61 6c 69 66 69 65 64 20 69 66 20 74 68 65 72  ualified if ther
357fb 65 20 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 2c  e is a prior >=,
357fc 20 3e 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20   >, or ==..     
357fd 20 20 20 2a 2a 20 41 20 3d 3d 20 69 73 20 64 69     ** A == is di
357fe 73 71 75 61 6c 69 66 65 64 20 69 66 20 74 68 65  squalifed if the
357ff 72 65 20 69 73 20 61 6e 79 20 70 72 69 6f 72 20  re is any prior 
35800 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20  constraint..    
35801 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
35802 6e 74 20 6a 2c 20 6f 70 6d 73 6b 3b 0a 20 20 20  nt j, opmsk;.   
35803 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
35804 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
35805 63 6f 6d 70 61 74 69 62 6c 65 5b 5d 20 3d 20 7b  compatible[] = {
35806 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 32 2c 20   0, 0, 1, 1, 2, 
35807 32 20 7d 3b 0a 20 20 20 20 20 20 20 20 61 73 73  2 };.        ass
35808 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b  ert( compatible[
35809 52 54 52 45 45 5f 45 51 20 26 20 37 5d 3d 3d 30  RTREE_EQ & 7]==0
3580a 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
3580b 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52  rt( compatible[R
3580c 54 52 45 45 5f 4c 54 20 26 20 37 5d 3d 3d 31 20  TREE_LT & 7]==1 
3580d 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
3580e 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54  t( compatible[RT
3580f 52 45 45 5f 4c 45 20 26 20 37 5d 3d 3d 31 20 29  REE_LE & 7]==1 )
35810 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
35811 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52  ( compatible[RTR
35812 45 45 5f 47 54 20 26 20 37 5d 3d 3d 32 20 29 3b  EE_GT & 7]==2 );
35813 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
35814 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45   compatible[RTRE
35815 45 5f 47 45 20 26 20 37 5d 3d 3d 32 20 29 3b 0a  E_GE & 7]==2 );.
35816 20 20 20 20 20 20 20 20 63 43 6f 6c 20 3d 20 70          cCol = p
35817 2d 3e 69 43 6f 6c 75 6d 6e 20 2d 20 31 20 2b 20  ->iColumn - 1 + 
35818 27 61 27 3b 0a 20 20 20 20 20 20 20 20 6f 70 6d  'a';.        opm
35819 73 6b 20 3d 20 63 6f 6d 70 61 74 69 62 6c 65 5b  sk = compatible[
3581a 6f 70 20 26 20 37 5d 3b 0a 20 20 20 20 20 20 20  op & 7];.       
3581b 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69 49 64 78   for(j=0; j<iIdx
3581c 3b 20 6a 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20  ; j+=2){.       
3581d 20 20 20 69 66 28 20 7a 49 64 78 53 74 72 5b 6a     if( zIdxStr[j
3581e 2b 31 5d 3d 3d 63 43 6f 6c 20 26 26 20 28 63 6f  +1]==cCol && (co
3581f 6d 70 61 74 69 62 6c 65 5b 7a 49 64 78 53 74 72  mpatible[zIdxStr
35820 5b 6a 5d 20 26 20 37 5d 20 26 20 6f 70 6d 73 6b  [j] & 7] & opmsk
35821 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
35822 20 20 20 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20      op = 0;.    
35823 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
35824 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
35825 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
35826 20 20 20 69 66 28 20 6f 70 20 29 7b 0a 20 20 20     if( op ){.   
35827 20 20 20 20 20 61 73 73 65 72 74 28 20 69 49 64       assert( iId
35828 78 3c 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72  x<sizeof(zIdxStr
35829 29 2d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 7a  )-1 );.        z
3582a 49 64 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d  IdxStr[iIdx++] =
3582b 20 6f 70 3b 0a 20 20 20 20 20 20 20 20 7a 49 64   op;.        zId
3582c 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 63  xStr[iIdx++] = c
3582d 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 64  Col;.        pId
3582e 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
3582f 6e 74 55 73 61 67 65 5b 69 69 5d 2e 61 72 67 76  ntUsage[ii].argv
35830 49 6e 64 65 78 20 3d 20 28 69 49 64 78 2f 32 29  Index = (iIdx/2)
35831 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49 6e  ;.        pIdxIn
35832 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55  fo->aConstraintU
35833 73 61 67 65 5b 69 69 5d 2e 6f 6d 69 74 20 3d 20  sage[ii].omit = 
35834 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
35835 0a 20 20 7d 0a 0a 20 20 70 49 64 78 49 6e 66 6f  .  }..  pIdxInfo
35836 2d 3e 69 64 78 4e 75 6d 20 3d 20 32 3b 0a 20 20  ->idxNum = 2;.  
35837 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f  pIdxInfo->needTo
35838 46 72 65 65 49 64 78 53 74 72 20 3d 20 31 3b 0a  FreeIdxStr = 1;.
35839 20 20 69 66 28 20 69 49 64 78 3e 30 20 26 26 20    if( iIdx>0 && 
3583a 30 3d 3d 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64  0==(pIdxInfo->id
3583b 78 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  xStr = sqlite3_m
3583c 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 49 64  printf("%s", zId
3583d 78 53 74 72 29 29 20 29 7b 0a 20 20 20 20 72 65  xStr)) ){.    re
3583e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
3583f 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  M;.  }.  assert(
35840 20 69 49 64 78 3e 3d 30 20 29 3b 0a 20 20 70 49   iIdx>=0 );.  pI
35841 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65  dxInfo->estimate
35842 64 43 6f 73 74 20 3d 20 28 32 30 30 30 30 30 30  dCost = (2000000
35843 2e 30 20 2f 20 28 64 6f 75 62 6c 65 29 28 69 49  .0 / (double)(iI
35844 64 78 20 2b 20 31 29 29 3b 0a 20 20 72 65 74 75  dx + 1));.  retu
35845 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
35846 52 65 74 75 72 6e 20 74 68 65 20 4e 2d 64 69 6d  Return the N-dim
35847 65 6e 73 69 6f 6e 61 6c 20 76 6f 6c 75 6d 6e 20  ensional volumn 
35848 6f 66 20 74 68 65 20 63 65 6c 6c 20 73 74 6f 72  of the cell stor
35849 65 64 20 69 6e 20 2a 70 2e 0a 2a 2f 0a 73 74 61  ed in *p..*/.sta
3584a 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 41 72  tic float cellAr
3584b 65 61 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ea(Rtree *pRtree
3584c 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b  , RtreeCell *p){
3584d 0a 20 20 66 6c 6f 61 74 20 61 72 65 61 20 3d 20  .  float area = 
3584e 31 2e 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  1.0;.  int ii;. 
3584f 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70   for(ii=0; ii<(p
35850 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20  Rtree->nDim*2); 
35851 69 69 2b 3d 32 29 7b 0a 20 20 20 20 61 72 65 61  ii+=2){.    area
35852 20 3d 20 61 72 65 61 20 2a 20 28 44 43 4f 4f 52   = area * (DCOOR
35853 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31  D(p->aCoord[ii+1
35854 5d 29 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61  ]) - DCOORD(p->a
35855 43 6f 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d  Coord[ii]));.  }
35856 0a 20 20 72 65 74 75 72 6e 20 61 72 65 61 3b 0a  .  return area;.
35857 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
35858 74 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74  the margin lengt
35859 68 20 6f 66 20 63 65 6c 6c 20 70 2e 20 54 68 65  h of cell p. The
3585a 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 69   margin length i
3585b 73 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20  s the sum.** of 
3585c 74 68 65 20 6f 62 6a 65 63 74 73 20 73 69 7a 65  the objects size
3585d 20 69 6e 20 65 61 63 68 20 64 69 6d 65 6e 73 69   in each dimensi
3585e 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c  on..*/.static fl
3585f 6f 61 74 20 63 65 6c 6c 4d 61 72 67 69 6e 28 52  oat cellMargin(R
35860 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
35861 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66  reeCell *p){.  f
35862 6c 6f 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e  loat margin = 0.
35863 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66  0;.  int ii;.  f
35864 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
35865 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
35866 2b 3d 32 29 7b 0a 20 20 20 20 6d 61 72 67 69 6e  +=2){.    margin
35867 20 2b 3d 20 28 44 43 4f 4f 52 44 28 70 2d 3e 61   += (DCOORD(p->a
35868 43 6f 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20 44  Coord[ii+1]) - D
35869 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b  COORD(p->aCoord[
3586a 69 69 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ii]));.  }.  ret
3586b 75 72 6e 20 6d 61 72 67 69 6e 3b 0a 7d 0a 0a 2f  urn margin;.}../
3586c 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 75  *.** Store the u
3586d 6e 69 6f 6e 20 6f 66 20 63 65 6c 6c 73 20 70 31  nion of cells p1
3586e 20 61 6e 64 20 70 32 20 69 6e 20 70 31 2e 0a 2a   and p2 in p1..*
3586f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 65  /.static void ce
35870 6c 6c 55 6e 69 6f 6e 28 52 74 72 65 65 20 2a 70  llUnion(Rtree *p
35871 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
35872 20 2a 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20   *p1, RtreeCell 
35873 2a 70 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  *p2){.  int ii;.
35874 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43    if( pRtree->eC
35875 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f  oordType==RTREE_
35876 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a  COORD_REAL32 ){.
35877 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
35878 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
35879 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20  ); ii+=2){.     
3587a 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e   p1->aCoord[ii].
3587b 66 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f  f = MIN(p1->aCoo
3587c 72 64 5b 69 69 5d 2e 66 2c 20 70 32 2d 3e 61 43  rd[ii].f, p2->aC
3587d 6f 6f 72 64 5b 69 69 5d 2e 66 29 3b 0a 20 20 20  oord[ii].f);.   
3587e 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69     p1->aCoord[ii
3587f 2b 31 5d 2e 66 20 3d 20 4d 41 58 28 70 31 2d 3e  +1].f = MAX(p1->
35880 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 2c 20  aCoord[ii+1].f, 
35881 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  p2->aCoord[ii+1]
35882 2e 66 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  .f);.    }.  }el
35883 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30  se{.    for(ii=0
35884 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; ii<(pRtree->nD
35885 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20  im*2); ii+=2){. 
35886 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b       p1->aCoord[
35887 69 69 5d 2e 69 20 3d 20 4d 49 4e 28 70 31 2d 3e  ii].i = MIN(p1->
35888 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 2c 20 70 32  aCoord[ii].i, p2
35889 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 29 3b  ->aCoord[ii].i);
3588a 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72  .      p1->aCoor
3588b 64 5b 69 69 2b 31 5d 2e 69 20 3d 20 4d 41 58 28  d[ii+1].i = MAX(
3588c 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  p1->aCoord[ii+1]
3588d 2e 69 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69  .i, p2->aCoord[i
3588e 69 2b 31 5d 2e 69 29 3b 0a 20 20 20 20 7d 0a 20  i+1].i);.    }. 
3588f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
35890 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 61  rn true if the a
35891 72 65 61 20 63 6f 76 65 72 65 64 20 62 79 20 70  rea covered by p
35892 32 20 69 73 20 61 20 73 75 62 73 65 74 20 6f 66  2 is a subset of
35893 20 74 68 65 20 61 72 65 61 20 63 6f 76 65 72 65   the area covere
35894 64 0a 2a 2a 20 62 79 20 70 31 2e 20 46 61 6c 73  d.** by p1. Fals
35895 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a  e otherwise..*/.
35896 73 74 61 74 69 63 20 69 6e 74 20 63 65 6c 6c 43  static int cellC
35897 6f 6e 74 61 69 6e 73 28 52 74 72 65 65 20 2a 70  ontains(Rtree *p
35898 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
35899 20 2a 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20   *p1, RtreeCell 
3589a 2a 70 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  *p2){.  int ii;.
3589b 20 20 69 6e 74 20 69 73 49 6e 74 20 3d 20 28 70    int isInt = (p
3589c 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70  Rtree->eCoordTyp
3589d 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49  e==RTREE_COORD_I
3589e 4e 54 33 32 29 3b 0a 20 20 66 6f 72 28 69 69 3d  NT32);.  for(ii=
3589f 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e  0; ii<(pRtree->n
358a0 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a  Dim*2); ii+=2){.
358a1 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a      RtreeCoord *
358a2 61 31 20 3d 20 26 70 31 2d 3e 61 43 6f 6f 72 64  a1 = &p1->aCoord
358a3 5b 69 69 5d 3b 0a 20 20 20 20 52 74 72 65 65 43  [ii];.    RtreeC
358a4 6f 6f 72 64 20 2a 61 32 20 3d 20 26 70 32 2d 3e  oord *a2 = &p2->
358a5 61 43 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20  aCoord[ii];.    
358a6 69 66 28 20 28 21 69 73 49 6e 74 20 26 26 20 28  if( (!isInt && (
358a7 61 32 5b 30 5d 2e 66 3c 61 31 5b 30 5d 2e 66 20  a2[0].f<a1[0].f 
358a8 7c 7c 20 61 32 5b 31 5d 2e 66 3e 61 31 5b 31 5d  || a2[1].f>a1[1]
358a9 2e 66 29 29 20 0a 20 20 20 20 20 7c 7c 20 28 20  .f)) .     || ( 
358aa 69 73 49 6e 74 20 26 26 20 28 61 32 5b 30 5d 2e  isInt && (a2[0].
358ab 69 3c 61 31 5b 30 5d 2e 69 20 7c 7c 20 61 32 5b  i<a1[0].i || a2[
358ac 31 5d 2e 69 3e 61 31 5b 31 5d 2e 69 29 29 20 0a  1].i>a1[1].i)) .
358ad 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74      ){.      ret
358ae 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
358af 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
358b0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
358b1 20 61 6d 6f 75 6e 74 20 63 65 6c 6c 20 70 20 77   amount cell p w
358b2 6f 75 6c 64 20 67 72 6f 77 20 62 79 20 69 66 20  ould grow by if 
358b3 69 74 20 77 65 72 65 20 75 6e 69 6f 6e 65 64 20  it were unioned 
358b4 77 69 74 68 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73  with pCell..*/.s
358b5 74 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c  tatic float cell
358b6 47 72 6f 77 74 68 28 52 74 72 65 65 20 2a 70 52  Growth(Rtree *pR
358b7 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20  tree, RtreeCell 
358b8 2a 70 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  *p, RtreeCell *p
358b9 43 65 6c 6c 29 7b 0a 20 20 66 6c 6f 61 74 20 61  Cell){.  float a
358ba 72 65 61 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c  rea;.  RtreeCell
358bb 20 63 65 6c 6c 3b 0a 20 20 6d 65 6d 63 70 79 28   cell;.  memcpy(
358bc 26 63 65 6c 6c 2c 20 70 2c 20 73 69 7a 65 6f 66  &cell, p, sizeof
358bd 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
358be 61 72 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28  area = cellArea(
358bf 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29 3b 0a  pRtree, &cell);.
358c0 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72    cellUnion(pRtr
358c1 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c  ee, &cell, pCell
358c2 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 63 65 6c  );.  return (cel
358c3 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63  lArea(pRtree, &c
358c4 65 6c 6c 29 2d 61 72 65 61 29 3b 0a 7d 0a 0a 23  ell)-area);.}..#
358c5 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
358c6 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52  TREE_CHOOSESUBTR
358c7 45 45 20 7c 7c 20 56 41 52 49 41 4e 54 5f 52 53  EE || VARIANT_RS
358c8 54 41 52 54 52 45 45 5f 53 50 4c 49 54 0a 73 74  TARTREE_SPLIT.st
358c9 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f  atic float cellO
358ca 76 65 72 6c 61 70 28 0a 20 20 52 74 72 65 65 20  verlap(.  Rtree 
358cb 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
358cc 65 43 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72  eCell *p, .  Rtr
358cd 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a  eeCell *aCell, .
358ce 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20    int nCell, .  
358cf 69 6e 74 20 69 45 78 63 6c 75 64 65 0a 29 7b 0a  int iExclude.){.
358d0 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6c 6f 61    int ii;.  floa
358d1 74 20 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b  t overlap = 0.0;
358d2 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
358d3 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20  nCell; ii++){.  
358d4 20 20 69 66 28 20 69 69 21 3d 69 45 78 63 6c 75    if( ii!=iExclu
358d5 64 65 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  de ){.      int 
358d6 6a 6a 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  jj;.      float 
358d7 6f 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 66  o = 1.0;.      f
358d8 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 28 70 52 74  or(jj=0; jj<(pRt
358d9 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 6a 6a  ree->nDim*2); jj
358da 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 64 6f  +=2){.        do
358db 75 62 6c 65 20 78 31 3b 0a 20 20 20 20 20 20 20  uble x1;.       
358dc 20 64 6f 75 62 6c 65 20 78 32 3b 0a 0a 20 20 20   double x2;..   
358dd 20 20 20 20 20 78 31 20 3d 20 4d 41 58 28 44 43       x1 = MAX(DC
358de 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a  OORD(p->aCoord[j
358df 6a 5d 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c  j]), DCOORD(aCel
358e0 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 5d  l[ii].aCoord[jj]
358e1 29 29 3b 0a 20 20 20 20 20 20 20 20 78 32 20 3d  ));.        x2 =
358e2 20 4d 49 4e 28 44 43 4f 4f 52 44 28 70 2d 3e 61   MIN(DCOORD(p->a
358e3 43 6f 6f 72 64 5b 6a 6a 2b 31 5d 29 2c 20 44 43  Coord[jj+1]), DC
358e4 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61  OORD(aCell[ii].a
358e5 43 6f 6f 72 64 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a  Coord[jj+1]));..
358e6 20 20 20 20 20 20 20 20 69 66 28 20 78 32 3c 78          if( x2<x
358e7 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f  1 ){.          o
358e8 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 20 20   = 0.0;.        
358e9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
358ea 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
358eb 20 20 6f 20 3d 20 6f 20 2a 20 28 78 32 2d 78 31    o = o * (x2-x1
358ec 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
358ed 20 20 20 7d 0a 20 20 20 20 20 20 6f 76 65 72 6c     }.      overl
358ee 61 70 20 2b 3d 20 6f 3b 0a 20 20 20 20 7d 0a 20  ap += o;.    }. 
358ef 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 76 65 72   }.  return over
358f0 6c 61 70 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  lap;.}.#endif..#
358f1 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
358f2 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52  TREE_CHOOSESUBTR
358f3 45 45 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20  EE.static float 
358f4 63 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72  cellOverlapEnlar
358f5 67 65 6d 65 6e 74 28 0a 20 20 52 74 72 65 65 20  gement(.  Rtree 
358f6 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
358f7 65 43 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72  eCell *p, .  Rtr
358f8 65 65 43 65 6c 6c 20 2a 70 49 6e 73 65 72 74 2c  eeCell *pInsert,
358f9 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61   .  RtreeCell *a
358fa 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
358fb 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c  ll, .  int iExcl
358fc 75 64 65 0a 29 7b 0a 20 20 66 6c 6f 61 74 20 62  ude.){.  float b
358fd 65 66 6f 72 65 3b 0a 20 20 66 6c 6f 61 74 20 61  efore;.  float a
358fe 66 74 65 72 3b 0a 20 20 62 65 66 6f 72 65 20 3d  fter;.  before =
358ff 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74   cellOverlap(pRt
35900 72 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e  ree, p, aCell, n
35901 43 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b  Cell, iExclude);
35902 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74  .  cellUnion(pRt
35903 72 65 65 2c 20 70 2c 20 70 49 6e 73 65 72 74 29  ree, p, pInsert)
35904 3b 0a 20 20 61 66 74 65 72 20 3d 20 63 65 6c 6c  ;.  after = cell
35905 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20  Overlap(pRtree, 
35906 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  p, aCell, nCell,
35907 20 69 45 78 63 6c 75 64 65 29 3b 0a 20 20 72 65   iExclude);.  re
35908 74 75 72 6e 20 61 66 74 65 72 2d 62 65 66 6f 72  turn after-befor
35909 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  e;.}.#endif.../*
3590a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
3590b 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
3590c 20 43 68 6f 6f 73 65 4c 65 61 66 20 61 6c 67 6f   ChooseLeaf algo
3590d 72 69 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61  rithm from Gutma
3590e 6e 5b 38 34 5d 2e 0a 2a 2a 20 43 68 6f 6f 73 65  n[84]..** Choose
3590f 53 75 62 54 72 65 65 20 69 6e 20 72 2a 74 72 65  SubTree in r*tre
35910 65 20 74 65 72 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a  e terminology..*
35911 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 43 68 6f  /.static int Cho
35912 6f 73 65 4c 65 61 66 28 0a 20 20 52 74 72 65 65  oseLeaf(.  Rtree
35913 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20   *pRtree,       
35914 20 20 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65          /* Rtree
35915 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65   table */.  Rtre
35916 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 20 20  eCell *pCell,   
35917 20 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c           /* Cell
35918 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20   to insert into 
35919 72 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69  rtree */.  int i
3591a 48 65 69 67 68 74 2c 20 20 20 20 20 20 20 20 20  Height,         
3591b 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68          /* Heigh
3591c 74 20 6f 66 20 73 75 62 2d 74 72 65 65 20 72 6f  t of sub-tree ro
3591d 6f 74 65 64 20 61 74 20 70 43 65 6c 6c 20 2a 2f  oted at pCell */
3591e 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  .  RtreeNode **p
3591f 70 4c 65 61 66 20 20 20 20 20 20 20 20 20 20 20  pLeaf           
35920 2f 2a 20 4f 55 54 3a 20 53 65 6c 65 63 74 65 64  /* OUT: Selected
35921 20 6c 65 61 66 20 70 61 67 65 20 2a 2f 0a 29 7b   leaf page */.){
35922 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
35923 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65   ii;.  RtreeNode
35924 20 2a 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20   *pNode;.  rc = 
35925 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
35926 65 65 2c 20 31 2c 20 30 2c 20 26 70 4e 6f 64 65  ee, 1, 0, &pNode
35927 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
35928 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
35929 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 69 44 65   ii<(pRtree->iDe
3592a 70 74 68 2d 69 48 65 69 67 68 74 29 3b 20 69 69  pth-iHeight); ii
3592b 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 65  ++){.    int iCe
3592c 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ll;.    sqlite3_
3592d 69 6e 74 36 34 20 69 42 65 73 74 3b 0a 0a 20 20  int64 iBest;..  
3592e 20 20 66 6c 6f 61 74 20 66 4d 69 6e 47 72 6f 77    float fMinGrow
3592f 74 68 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d  th;.    float fM
35930 69 6e 41 72 65 61 3b 0a 20 20 20 20 66 6c 6f 61  inArea;.    floa
35931 74 20 66 4d 69 6e 4f 76 65 72 6c 61 70 3b 0a 0a  t fMinOverlap;..
35932 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20      int nCell = 
35933 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20  NCELL(pNode);.  
35934 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
35935 3b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  ;.    RtreeNode 
35936 2a 70 43 68 69 6c 64 3b 0a 0a 20 20 20 20 52 74  *pChild;..    Rt
35937 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 20 3d  reeCell *aCell =
35938 20 30 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54   0;..#if VARIANT
35939 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53  _RSTARTREE_CHOOS
3593a 45 53 55 42 54 52 45 45 0a 20 20 20 20 69 66 28  ESUBTREE.    if(
3593b 20 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44   ii==(pRtree->iD
3593c 65 70 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20  epth-1) ){.     
3593d 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 61   int jj;.      a
3593e 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Cell = sqlite3_m
3593f 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72  alloc(sizeof(Rtr
35940 65 65 43 65 6c 6c 29 2a 6e 43 65 6c 6c 29 3b 0a  eeCell)*nCell);.
35941 20 20 20 20 20 20 69 66 28 20 21 61 43 65 6c 6c        if( !aCell
35942 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
35943 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
35944 20 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61         nodeRelea
35945 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  se(pRtree, pNode
35946 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65  );.        pNode
35947 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f   = 0;.        co
35948 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a  ntinue;.      }.
35949 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20        for(jj=0; 
3594a 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b  jj<nCell; jj++){
3594b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 47 65 74  .        nodeGet
3594c 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
3594d 64 65 2c 20 6a 6a 2c 20 26 61 43 65 6c 6c 5b 6a  de, jj, &aCell[j
3594e 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  j]);.      }.   
3594f 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
35950 2a 20 53 65 6c 65 63 74 20 74 68 65 20 63 68 69  * Select the chi
35951 6c 64 20 6e 6f 64 65 20 77 68 69 63 68 20 77 69  ld node which wi
35952 6c 6c 20 62 65 20 65 6e 6c 61 72 67 65 64 20 74  ll be enlarged t
35953 68 65 20 6c 65 61 73 74 20 69 66 20 70 43 65 6c  he least if pCel
35954 6c 0a 20 20 20 20 2a 2a 20 69 73 20 69 6e 73 65  l.    ** is inse
35955 72 74 65 64 20 69 6e 74 6f 20 69 74 2e 20 52 65  rted into it. Re
35956 73 6f 6c 76 65 20 74 69 65 73 20 62 79 20 63 68  solve ties by ch
35957 6f 6f 73 69 6e 67 20 74 68 65 20 65 6e 74 72 79  oosing the entry
35958 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65   with.    ** the
35959 20 73 6d 61 6c 6c 65 73 74 20 61 72 65 61 2e 0a   smallest area..
3595a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
3595b 43 65 6c 6c 3d 30 3b 20 69 43 65 6c 6c 3c 6e 43  Cell=0; iCell<nC
3595c 65 6c 6c 3b 20 69 43 65 6c 6c 2b 2b 29 7b 0a 20  ell; iCell++){. 
3595d 20 20 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74       float growt
3595e 68 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 61  h;.      float a
3595f 72 65 61 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  rea;.      float
35960 20 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a   overlap = 0.0;.
35961 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c        nodeGetCel
35962 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
35963 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a   iCell, &cell);.
35964 20 20 20 20 20 20 67 72 6f 77 74 68 20 3d 20 63        growth = c
35965 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65  ellGrowth(pRtree
35966 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b  , &cell, pCell);
35967 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20 63 65  .      area = ce
35968 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26  llArea(pRtree, &
35969 63 65 6c 6c 29 3b 0a 23 69 66 20 56 41 52 49 41  cell);.#if VARIA
3596a 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f  NT_RSTARTREE_CHO
3596b 4f 53 45 53 55 42 54 52 45 45 0a 20 20 20 20 20  OSESUBTREE.     
3596c 20 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65 65   if( ii==(pRtree
3596d 2d 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0a 20  ->iDepth-1) ){. 
3596e 20 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d         overlap =
3596f 20 63 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61   cellOverlapEnla
35970 72 67 65 6d 65 6e 74 28 70 52 74 72 65 65 2c 26  rgement(pRtree,&
35971 63 65 6c 6c 2c 70 43 65 6c 6c 2c 61 43 65 6c 6c  cell,pCell,aCell
35972 2c 6e 43 65 6c 6c 2c 69 43 65 6c 6c 29 3b 0a 20  ,nCell,iCell);. 
35973 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
35974 20 20 20 20 69 66 28 20 28 69 43 65 6c 6c 3d 3d      if( (iCell==
35975 30 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f  0) .       || (o
35976 76 65 72 6c 61 70 3c 66 4d 69 6e 4f 76 65 72 6c  verlap<fMinOverl
35977 61 70 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28  ap) .       || (
35978 6f 76 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76 65  overlap==fMinOve
35979 72 6c 61 70 20 26 26 20 67 72 6f 77 74 68 3c 66  rlap && growth<f
3597a 4d 69 6e 47 72 6f 77 74 68 29 0a 20 20 20 20 20  MinGrowth).     
3597b 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66    || (overlap==f
3597c 4d 69 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72  MinOverlap && gr
3597d 6f 77 74 68 3d 3d 66 4d 69 6e 47 72 6f 77 74 68  owth==fMinGrowth
3597e 20 26 26 20 61 72 65 61 3c 66 4d 69 6e 41 72 65   && area<fMinAre
3597f 61 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  a).      ){.    
35980 20 20 20 20 66 4d 69 6e 4f 76 65 72 6c 61 70 20      fMinOverlap 
35981 3d 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20  = overlap;.     
35982 20 20 20 66 4d 69 6e 47 72 6f 77 74 68 20 3d 20     fMinGrowth = 
35983 67 72 6f 77 74 68 3b 0a 20 20 20 20 20 20 20 20  growth;.        
35984 66 4d 69 6e 41 72 65 61 20 3d 20 61 72 65 61 3b  fMinArea = area;
35985 0a 20 20 20 20 20 20 20 20 69 42 65 73 74 20 3d  .        iBest =
35986 20 63 65 6c 6c 2e 69 52 6f 77 69 64 3b 0a 20 20   cell.iRowid;.  
35987 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
35988 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43   sqlite3_free(aC
35989 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e  ell);.    rc = n
3598a 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65  odeAcquire(pRtre
3598b 65 2c 20 69 42 65 73 74 2c 20 70 4e 6f 64 65 2c  e, iBest, pNode,
3598c 20 26 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 6e   &pChild);.    n
3598d 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
3598e 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70  e, pNode);.    p
3598f 4e 6f 64 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20  Node = pChild;. 
35990 20 7d 0a 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20   }..  *ppLeaf = 
35991 70 4e 6f 64 65 3b 0a 20 20 72 65 74 75 72 6e 20  pNode;.  return 
35992 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 63  rc;.}../*.** A c
35993 65 6c 6c 20 77 69 74 68 20 74 68 65 20 73 61 6d  ell with the sam
35994 65 20 63 6f 6e 74 65 6e 74 20 61 73 20 70 43 65  e content as pCe
35995 6c 6c 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ll has just been
35996 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a   inserted into.*
35997 2a 20 74 68 65 20 6e 6f 64 65 20 70 4e 6f 64 65  * the node pNode
35998 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
35999 75 70 64 61 74 65 73 20 74 68 65 20 62 6f 75 6e  updates the boun
3599a 64 69 6e 67 20 62 6f 78 20 63 65 6c 6c 73 20 69  ding box cells i
3599b 6e 0a 2a 2a 20 61 6c 6c 20 61 6e 63 65 73 74 6f  n.** all ancesto
3599c 72 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  r elements..*/.s
3599d 74 61 74 69 63 20 76 6f 69 64 20 41 64 6a 75 73  tatic void Adjus
3599e 74 54 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a  tTree(.  Rtree *
3599f 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pRtree,         
359a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 74             /* Rt
359a1 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52  ree table */.  R
359a2 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
359a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359a4 20 2f 2a 20 41 64 6a 75 73 74 20 61 6e 63 65 73   /* Adjust ances
359a5 74 72 79 20 6f 66 20 74 68 69 73 20 6e 6f 64 65  try of this node
359a6 2e 20 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c  . */.  RtreeCell
359a7 20 2a 70 43 65 6c 6c 20 20 20 20 20 20 20 20 20   *pCell         
359a8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
359a9 20 63 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69   cell was just i
359aa 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20  nserted */.){.  
359ab 52 74 72 65 65 4e 6f 64 65 20 2a 70 20 3d 20 70  RtreeNode *p = p
359ac 4e 6f 64 65 3b 0a 20 20 77 68 69 6c 65 28 20 70  Node;.  while( p
359ad 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20  ->pParent ){.   
359ae 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
359af 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
359b0 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61  pParent = p->pPa
359b1 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43  rent;.    int iC
359b2 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
359b3 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 29  Index(pRtree, p)
359b4 3b 0a 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  ;..    nodeGetCe
359b5 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ll(pRtree, pPare
359b6 6e 74 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  nt, iCell, &cell
359b7 29 3b 0a 20 20 20 20 69 66 28 20 21 63 65 6c 6c  );.    if( !cell
359b8 43 6f 6e 74 61 69 6e 73 28 70 52 74 72 65 65 2c  Contains(pRtree,
359b9 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 20 29   &cell, pCell) )
359ba 7b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f  {.      cellUnio
359bb 6e 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c  n(pRtree, &cell,
359bc 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 6e   pCell);.      n
359bd 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c  odeOverwriteCell
359be 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74  (pRtree, pParent
359bf 2c 20 26 63 65 6c 6c 2c 20 69 43 65 6c 6c 29 3b  , &cell, iCell);
359c0 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 70 20 3d  .    }. .    p =
359c1 20 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 7d 0a   pParent;.  }.}.
359c2 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70  ./*.** Write map
359c3 70 69 6e 67 20 28 69 52 6f 77 69 64 2d 3e 69 4e  ping (iRowid->iN
359c4 6f 64 65 29 20 74 6f 20 74 68 65 20 3c 72 74 72  ode) to the <rtr
359c5 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e  ee>_rowid table.
359c6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
359c7 6f 77 69 64 57 72 69 74 65 28 52 74 72 65 65 20  owidWrite(Rtree 
359c8 2a 70 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33  *pRtree, sqlite3
359c9 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 2c 20 73  _int64 iRowid, s
359ca 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f  qlite3_int64 iNo
359cb 64 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  de){.  sqlite3_b
359cc 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
359cd 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20 31  ->pWriteRowid, 1
359ce 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c  , iRowid);.  sql
359cf 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
359d0 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
359d1 77 69 64 2c 20 32 2c 20 69 4e 6f 64 65 29 3b 0a  wid, 2, iNode);.
359d2 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
359d3 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
359d4 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  id);.  return sq
359d5 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
359d6 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29  ee->pWriteRowid)
359d7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
359d8 20 6d 61 70 70 69 6e 67 20 28 69 4e 6f 64 65 2d   mapping (iNode-
359d9 3e 69 50 61 72 29 20 74 6f 20 74 68 65 20 3c 72  >iPar) to the <r
359da 74 72 65 65 3e 5f 70 61 72 65 6e 74 20 74 61 62  tree>_parent tab
359db 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
359dc 74 20 70 61 72 65 6e 74 57 72 69 74 65 28 52 74  t parentWrite(Rt
359dd 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c  ree *pRtree, sql
359de 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65  ite3_int64 iNode
359df 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
359e0 69 50 61 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  iPar){.  sqlite3
359e1 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
359e2 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
359e3 2c 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73  , 1, iNode);.  s
359e4 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
359e5 34 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  4(pRtree->pWrite
359e6 50 61 72 65 6e 74 2c 20 32 2c 20 69 50 61 72 29  Parent, 2, iPar)
359e7 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ;.  sqlite3_step
359e8 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
359e9 61 72 65 6e 74 29 3b 0a 20 20 72 65 74 75 72 6e  arent);.  return
359ea 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
359eb 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72  Rtree->pWritePar
359ec 65 6e 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  ent);.}..static 
359ed 69 6e 74 20 72 74 72 65 65 49 6e 73 65 72 74 43  int rtreeInsertC
359ee 65 6c 6c 28 52 74 72 65 65 20 2a 2c 20 52 74 72  ell(Rtree *, Rtr
359ef 65 65 4e 6f 64 65 20 2a 2c 20 52 74 72 65 65 43  eeNode *, RtreeC
359f0 65 6c 6c 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69  ell *, int);..#i
359f1 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
359f2 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 2f  N_LINEAR_SPLIT./
359f3 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
359f4 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6e 65 61  ion of the linea
359f5 72 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65  r variant of the
359f6 20 50 69 63 6b 4e 65 78 74 28 29 20 66 75 6e 63   PickNext() func
359f7 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74  tion from.** Gut
359f8 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61  tman[84]..*/.sta
359f9 74 69 63 20 52 74 72 65 65 43 65 6c 6c 20 2a 4c  tic RtreeCell *L
359fa 69 6e 65 61 72 50 69 63 6b 4e 65 78 74 28 0a 20  inearPickNext(. 
359fb 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a   Rtree *pRtree,.
359fc 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
359fd 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
359fe 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  , .  RtreeCell *
359ff 70 4c 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72  pLeftBox, .  Rtr
35a00 65 65 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f  eeCell *pRightBo
35a01 78 2c 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64  x,.  int *aiUsed
35a02 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  .){.  int ii;.  
35a03 66 6f 72 28 69 69 3d 30 3b 20 61 69 55 73 65 64  for(ii=0; aiUsed
35a04 5b 69 69 5d 3b 20 69 69 2b 2b 29 3b 0a 20 20 61  [ii]; ii++);.  a
35a05 69 55 73 65 64 5b 69 69 5d 20 3d 20 31 3b 0a 20  iUsed[ii] = 1;. 
35a06 20 72 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69   return &aCell[i
35a07 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  i];.}../*.** Imp
35a08 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
35a09 68 65 20 6c 69 6e 65 61 72 20 76 61 72 69 61 6e  he linear varian
35a0a 74 20 6f 66 20 74 68 65 20 50 69 63 6b 53 65 65  t of the PickSee
35a0b 64 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 72  ds() function fr
35a0c 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34  om.** Guttman[84
35a0d 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ]..*/.static voi
35a0e 64 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64  d LinearPickSeed
35a0f 73 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  s(.  Rtree *pRtr
35a10 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
35a11 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
35a12 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69  Cell, .  int *pi
35a13 4c 65 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74  LeftSeed, .  int
35a14 20 2a 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b   *piRightSeed.){
35a15 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
35a16 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20  iLeftSeed = 0;. 
35a17 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20   int iRightSeed 
35a18 3d 20 31 3b 0a 20 20 66 6c 6f 61 74 20 6d 61 78  = 1;.  float max
35a19 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68  NormalInnerWidth
35a1a 20 3d 20 30 2e 30 3b 0a 0a 20 20 2f 2a 20 50 69   = 0.0;..  /* Pi
35a1b 63 6b 20 74 77 6f 20 22 73 65 65 64 22 20 63 65  ck two "seed" ce
35a1c 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 61 72 72  lls from the arr
35a1d 61 79 20 6f 66 20 63 65 6c 6c 73 2e 20 54 68 65  ay of cells. The
35a1e 20 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 0a   algorithm used.
35a1f 20 20 2a 2a 20 68 65 72 65 20 69 73 20 74 68 65    ** here is the
35a20 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73   LinearPickSeeds
35a21 20 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20   algorithm from 
35a22 47 75 74 6d 61 6e 5b 31 39 38 34 5d 2e 20 54 68  Gutman[1984]. Th
35a23 65 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20  e .  ** indices 
35a24 6f 66 20 74 68 65 20 74 77 6f 20 73 65 65 64 20  of the two seed 
35a25 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 61 72 72  cells in the arr
35a26 61 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e  ay are stored in
35a27 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20 76 61 72 69   local.  ** vari
35a28 61 62 6c 65 73 20 69 4c 65 66 74 53 65 65 6b 20  ables iLeftSeek 
35a29 61 6e 64 20 69 52 69 67 68 74 53 65 65 64 2e 0a  and iRightSeed..
35a2a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
35a2b 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20  i<pRtree->nDim; 
35a2c 69 2b 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20  i++){.    float 
35a2d 78 31 20 3d 20 61 43 65 6c 6c 5b 30 5d 2e 61 43  x1 = aCell[0].aC
35a2e 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20 20 20 20 66  oord[i*2];.    f
35a2f 6c 6f 61 74 20 78 32 20 3d 20 61 43 65 6c 6c 5b  loat x2 = aCell[
35a30 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d  0].aCoord[i*2+1]
35a31 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78 33 20 3d  ;.    float x3 =
35a32 20 78 31 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78   x1;.    float x
35a33 34 20 3d 20 78 32 3b 0a 20 20 20 20 69 6e 74 20  4 = x2;.    int 
35a34 6a 6a 3b 0a 0a 20 20 20 20 69 6e 74 20 69 43 65  jj;..    int iCe
35a35 6c 6c 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20  llLeft = 0;.    
35a36 69 6e 74 20 69 43 65 6c 6c 52 69 67 68 74 20 3d  int iCellRight =
35a37 20 30 3b 0a 0a 20 20 20 20 66 6f 72 28 6a 6a 3d   0;..    for(jj=
35a38 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b  1; jj<nCell; jj+
35a39 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
35a3a 6c 65 66 74 20 3d 20 61 43 65 6c 6c 5b 6a 6a 5d  left = aCell[jj]
35a3b 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20 20  .aCoord[i*2];.  
35a3c 20 20 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20      float right 
35a3d 3d 20 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f  = aCell[jj].aCoo
35a3e 72 64 5b 69 2a 32 2b 31 5d 3b 0a 0a 20 20 20 20  rd[i*2+1];..    
35a3f 20 20 69 66 28 20 6c 65 66 74 3c 78 31 20 29 20    if( left<x1 ) 
35a40 78 31 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20  x1 = left;.     
35a41 20 69 66 28 20 72 69 67 68 74 3e 78 34 20 29 20   if( right>x4 ) 
35a42 78 34 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20  x4 = right;.    
35a43 20 20 69 66 28 20 6c 65 66 74 3e 78 33 20 29 7b    if( left>x3 ){
35a44 0a 20 20 20 20 20 20 20 20 78 33 20 3d 20 6c 65  .        x3 = le
35a45 66 74 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c  ft;.        iCel
35a46 6c 52 69 67 68 74 20 3d 20 6a 6a 3b 0a 20 20 20  lRight = jj;.   
35a47 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
35a48 69 67 68 74 3c 78 32 20 29 7b 0a 20 20 20 20 20  ight<x2 ){.     
35a49 20 20 20 78 32 20 3d 20 72 69 67 68 74 3b 0a 20     x2 = right;. 
35a4a 20 20 20 20 20 20 20 69 43 65 6c 6c 4c 65 66 74         iCellLeft
35a4b 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a 20   = jj;.      }. 
35a4c 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 78 34     }..    if( x4
35a4d 21 3d 78 31 20 29 7b 0a 20 20 20 20 20 20 66 6c  !=x1 ){.      fl
35a4e 6f 61 74 20 6e 6f 72 6d 61 6c 77 69 64 74 68 20  oat normalwidth 
35a4f 3d 20 28 78 33 20 2d 20 78 32 29 20 2f 20 28 78  = (x3 - x2) / (x
35a50 34 20 2d 20 78 31 29 3b 0a 20 20 20 20 20 20 69  4 - x1);.      i
35a51 66 28 20 6e 6f 72 6d 61 6c 77 69 64 74 68 3e 6d  f( normalwidth>m
35a52 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64  axNormalInnerWid
35a53 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c  th ){.        iL
35a54 65 66 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 4c  eftSeed = iCellL
35a55 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 52 69  eft;.        iRi
35a56 67 68 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 52  ghtSeed = iCellR
35a57 69 67 68 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ight;.      }.  
35a58 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c 65    }.  }..  *piLe
35a59 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74 53 65  ftSeed = iLeftSe
35a5a 65 64 3b 0a 20 20 2a 70 69 52 69 67 68 74 53 65  ed;.  *piRightSe
35a5b 65 64 20 3d 20 69 52 69 67 68 74 53 65 65 64 3b  ed = iRightSeed;
35a5c 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41 52  .}.#endif /* VAR
35a5d 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
35a5e 45 41 52 5f 53 50 4c 49 54 20 2a 2f 0a 0a 23 69  EAR_SPLIT */..#i
35a5f 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
35a60 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49  N_QUADRATIC_SPLI
35a61 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  T./*.** Implemen
35a62 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75  tation of the qu
35a63 61 64 72 61 74 69 63 20 76 61 72 69 61 6e 74 20  adratic variant 
35a64 6f 66 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28  of the PickNext(
35a65 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a  ) function from.
35a66 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a  ** Guttman[84]..
35a67 2a 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 43  */.static RtreeC
35a68 65 6c 6c 20 2a 51 75 61 64 72 61 74 69 63 50 69  ell *QuadraticPi
35a69 63 6b 4e 65 78 74 28 0a 20 20 52 74 72 65 65 20  ckNext(.  Rtree 
35a6a 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
35a6b 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
35a6c 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 52 74  int nCell, .  Rt
35a6d 72 65 65 43 65 6c 6c 20 2a 70 4c 65 66 74 42 6f  reeCell *pLeftBo
35a6e 78 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  x, .  RtreeCell 
35a6f 2a 70 52 69 67 68 74 42 6f 78 2c 0a 20 20 69 6e  *pRightBox,.  in
35a70 74 20 2a 61 69 55 73 65 64 0a 29 7b 0a 20 20 23  t *aiUsed.){.  #
35a71 64 65 66 69 6e 65 20 46 41 42 53 28 61 29 20 28  define FABS(a) (
35a72 28 61 29 3c 30 2e 30 3f 2d 31 2e 30 2a 28 61 29  (a)<0.0?-1.0*(a)
35a73 3a 28 61 29 29 0a 0a 20 20 69 6e 74 20 69 53 65  :(a))..  int iSe
35a74 6c 65 63 74 20 3d 20 2d 31 3b 0a 20 20 66 6c 6f  lect = -1;.  flo
35a75 61 74 20 66 44 69 66 66 3b 0a 20 20 69 6e 74 20  at fDiff;.  int 
35a76 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ii;.  for(ii=0; 
35a77 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
35a78 0a 20 20 20 20 69 66 28 20 61 69 55 73 65 64 5b  .    if( aiUsed[
35a79 69 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ii]==0 ){.      
35a7a 66 6c 6f 61 74 20 6c 65 66 74 20 3d 20 63 65 6c  float left = cel
35a7b 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20  lGrowth(pRtree, 
35a7c 70 4c 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c  pLeftBox, &aCell
35a7d 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f  [ii]);.      flo
35a7e 61 74 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 47  at right = cellG
35a7f 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c  rowth(pRtree, pL
35a80 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69  eftBox, &aCell[i
35a81 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  i]);.      float
35a82 20 64 69 66 66 20 3d 20 46 41 42 53 28 72 69 67   diff = FABS(rig
35a83 68 74 2d 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  ht-left);.      
35a84 69 66 28 20 69 53 65 6c 65 63 74 3c 30 20 7c 7c  if( iSelect<0 ||
35a85 20 64 69 66 66 3e 66 44 69 66 66 20 29 7b 0a 20   diff>fDiff ){. 
35a86 20 20 20 20 20 20 20 66 44 69 66 66 20 3d 20 64         fDiff = d
35a87 69 66 66 3b 0a 20 20 20 20 20 20 20 20 69 53 65  iff;.        iSe
35a88 6c 65 63 74 20 3d 20 69 69 3b 0a 20 20 20 20 20  lect = ii;.     
35a89 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61   }.    }.  }.  a
35a8a 69 55 73 65 64 5b 69 53 65 6c 65 63 74 5d 20 3d  iUsed[iSelect] =
35a8b 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43   1;.  return &aC
35a8c 65 6c 6c 5b 69 53 65 6c 65 63 74 5d 3b 0a 7d 0a  ell[iSelect];.}.
35a8d 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
35a8e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61  ation of the qua
35a8f 64 72 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f  dratic variant o
35a90 66 20 74 68 65 20 50 69 63 6b 53 65 65 64 73 28  f the PickSeeds(
35a91 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a  ) function from.
35a92 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a  ** Guttman[84]..
35a93 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 51  */.static void Q
35a94 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65 64  uadraticPickSeed
35a95 73 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  s(.  Rtree *pRtr
35a96 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
35a97 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
35a98 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69  Cell, .  int *pi
35a99 4c 65 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74  LeftSeed, .  int
35a9a 20 2a 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b   *piRightSeed.){
35a9b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
35a9c 20 6a 6a 3b 0a 0a 20 20 69 6e 74 20 69 4c 65 66   jj;..  int iLef
35a9d 74 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74  tSeed = 0;.  int
35a9e 20 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b   iRightSeed = 1;
35a9f 0a 20 20 66 6c 6f 61 74 20 66 57 61 73 74 65 20  .  float fWaste 
35aa0 3d 20 30 2e 30 3b 0a 0a 20 20 66 6f 72 28 69 69  = 0.0;..  for(ii
35aa1 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69  =0; ii<nCell; ii
35aa2 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d  ++){.    for(jj=
35aa3 69 69 2b 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20  ii+1; jj<nCell; 
35aa4 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f  jj++){.      flo
35aa5 61 74 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 41  at right = cellA
35aa6 72 65 61 28 70 52 74 72 65 65 2c 20 26 61 43 65  rea(pRtree, &aCe
35aa7 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66  ll[jj]);.      f
35aa8 6c 6f 61 74 20 67 72 6f 77 74 68 20 3d 20 63 65  loat growth = ce
35aa9 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c  llGrowth(pRtree,
35aaa 20 26 61 43 65 6c 6c 5b 69 69 5d 2c 20 26 61 43   &aCell[ii], &aC
35aab 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20  ell[jj]);.      
35aac 66 6c 6f 61 74 20 77 61 73 74 65 20 3d 20 67 72  float waste = gr
35aad 6f 77 74 68 20 2d 20 72 69 67 68 74 3b 0a 0a 20  owth - right;.. 
35aae 20 20 20 20 20 69 66 28 20 77 61 73 74 65 3e 66       if( waste>f
35aaf 57 61 73 74 65 20 29 7b 0a 20 20 20 20 20 20 20  Waste ){.       
35ab0 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69 69 3b   iLeftSeed = ii;
35ab1 0a 20 20 20 20 20 20 20 20 69 52 69 67 68 74 53  .        iRightS
35ab2 65 65 64 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20  eed = jj;.      
35ab3 20 20 66 57 61 73 74 65 20 3d 20 77 61 73 74 65    fWaste = waste
35ab4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
35ab5 20 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65    }..  *piLeftSe
35ab6 65 64 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a  ed = iLeftSeed;.
35ab7 20 20 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d    *piRightSeed =
35ab8 20 69 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23   iRightSeed;.}.#
35ab9 65 6e 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54  endif /* VARIANT
35aba 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54  _GUTTMAN_QUADRAT
35abb 49 43 5f 53 50 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a  IC_SPLIT */../*.
35abc 2a 2a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64  ** Arguments aId
35abd 78 2c 20 61 44 69 73 74 61 6e 63 65 20 61 6e 64  x, aDistance and
35abe 20 61 53 70 61 72 65 20 61 6c 6c 20 70 6f 69 6e   aSpare all poin
35abf 74 20 74 6f 20 61 72 72 61 79 73 20 6f 66 20 73  t to arrays of s
35ac0 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65  ize.** nIdx. The
35ac1 20 61 49 64 78 20 61 72 72 61 79 20 63 6f 6e 74   aIdx array cont
35ac2 61 69 6e 73 20 74 68 65 20 73 65 74 20 6f 66 20  ains the set of 
35ac3 69 6e 74 65 67 65 72 73 20 66 72 6f 6d 20 30 20  integers from 0 
35ac4 74 6f 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20  to .** (nIdx-1) 
35ac5 69 6e 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72  in no particular
35ac6 20 6f 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e   order. This fun
35ac7 63 74 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20  ction sorts the 
35ac8 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49 64  values.** in aId
35ac9 78 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  x according to t
35aca 68 65 20 69 6e 64 65 78 65 64 20 76 61 6c 75 65  he indexed value
35acb 73 20 69 6e 20 61 44 69 73 74 61 6e 63 65 2e 20  s in aDistance. 
35acc 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20  For.** example, 
35acd 61 73 73 75 6d 69 6e 67 20 74 68 65 20 69 6e 70  assuming the inp
35ace 75 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64  uts:.**.**   aId
35acf 78 20 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20  x      = { 0,   
35ad0 31 2c 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a  1,   2,   3 }.**
35ad1 20 20 20 61 44 69 73 74 61 6e 63 65 20 3d 20 7b     aDistance = {
35ad2 20 35 2e 30 2c 20 32 2e 30 2c 20 37 2e 30 2c 20   5.0, 2.0, 7.0, 
35ad3 36 2e 30 20 7d 0a 2a 2a 0a 2a 2a 20 74 68 69 73  6.0 }.**.** this
35ad4 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74   function sets t
35ad5 68 65 20 61 49 64 78 20 61 72 72 61 79 20 74 6f  he aIdx array to
35ad6 20 63 6f 6e 74 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20   contain:.**.** 
35ad7 20 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20    aIdx      = { 
35ad8 30 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33  0,   1,   2,   3
35ad9 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 53 70   }.**.** The aSp
35ada 61 72 65 20 61 72 72 61 79 20 69 73 20 75 73 65  are array is use
35adb 64 20 61 73 20 74 65 6d 70 6f 72 61 72 79 20 77  d as temporary w
35adc 6f 72 6b 69 6e 67 20 73 70 61 63 65 20 62 79 20  orking space by 
35add 74 68 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 61  the.** sorting a
35ade 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61  lgorithm..*/.sta
35adf 74 69 63 20 76 6f 69 64 20 53 6f 72 74 42 79 44  tic void SortByD
35ae0 69 73 74 61 6e 63 65 28 0a 20 20 69 6e 74 20 2a  istance(.  int *
35ae1 61 49 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64  aIdx, .  int nId
35ae2 78 2c 20 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69  x, .  float *aDi
35ae3 73 74 61 6e 63 65 2c 20 0a 20 20 69 6e 74 20 2a  stance, .  int *
35ae4 61 53 70 61 72 65 0a 29 7b 0a 20 20 69 66 28 20  aSpare.){.  if( 
35ae5 6e 49 64 78 3e 31 20 29 7b 0a 20 20 20 20 69 6e  nIdx>1 ){.    in
35ae6 74 20 69 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20  t iLeft = 0;.   
35ae7 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20 30 3b   int iRight = 0;
35ae8 0a 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 20  ..    int nLeft 
35ae9 3d 20 6e 49 64 78 2f 32 3b 0a 20 20 20 20 69 6e  = nIdx/2;.    in
35aea 74 20 6e 52 69 67 68 74 20 3d 20 6e 49 64 78 2d  t nRight = nIdx-
35aeb 6e 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74 20 2a  nLeft;.    int *
35aec 61 4c 65 66 74 20 3d 20 61 49 64 78 3b 0a 20 20  aLeft = aIdx;.  
35aed 20 20 69 6e 74 20 2a 61 52 69 67 68 74 20 3d 20    int *aRight = 
35aee 26 61 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20  &aIdx[nLeft];.. 
35aef 20 20 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63     SortByDistanc
35af0 65 28 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20  e(aLeft, nLeft, 
35af1 61 44 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72  aDistance, aSpar
35af2 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69  e);.    SortByDi
35af3 73 74 61 6e 63 65 28 61 52 69 67 68 74 2c 20 6e  stance(aRight, n
35af4 52 69 67 68 74 2c 20 61 44 69 73 74 61 6e 63 65  Right, aDistance
35af5 2c 20 61 53 70 61 72 65 29 3b 0a 0a 20 20 20 20  , aSpare);..    
35af6 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c 20 61  memcpy(aSpare, a
35af7 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74  Left, sizeof(int
35af8 29 2a 6e 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c  )*nLeft);.    aL
35af9 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0a 0a 20  eft = aSpare;.. 
35afa 20 20 20 77 68 69 6c 65 28 20 69 4c 65 66 74 3c     while( iLeft<
35afb 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67 68 74 3c  nLeft || iRight<
35afc 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  nRight ){.      
35afd 69 66 28 20 69 4c 65 66 74 3d 3d 6e 4c 65 66 74  if( iLeft==nLeft
35afe 20 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78   ){.        aIdx
35aff 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d  [iLeft+iRight] =
35b00 20 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b   aRight[iRight];
35b01 0a 20 20 20 20 20 20 20 20 69 52 69 67 68 74 2b  .        iRight+
35b02 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  +;.      }else i
35b03 66 28 20 69 52 69 67 68 74 3d 3d 6e 52 69 67 68  f( iRight==nRigh
35b04 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64  t ){.        aId
35b05 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20  x[iLeft+iRight] 
35b06 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a  = aLeft[iLeft];.
35b07 20 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b          iLeft++;
35b08 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
35b09 20 20 20 20 20 20 66 6c 6f 61 74 20 66 4c 65 66        float fLef
35b0a 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 4c  t = aDistance[aL
35b0b 65 66 74 5b 69 4c 65 66 74 5d 5d 3b 0a 20 20 20  eft[iLeft]];.   
35b0c 20 20 20 20 20 66 6c 6f 61 74 20 66 52 69 67 68       float fRigh
35b0d 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 52  t = aDistance[aR
35b0e 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 3b 0a 20  ight[iRight]];. 
35b0f 20 20 20 20 20 20 20 69 66 28 20 66 4c 65 66 74         if( fLeft
35b10 3c 66 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  <fRight ){.     
35b11 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
35b12 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b  iRight] = aLeft[
35b13 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20 20  iLeft];.        
35b14 20 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20    iLeft++;.     
35b15 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
35b16 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69      aIdx[iLeft+i
35b17 52 69 67 68 74 5d 20 3d 20 61 52 69 67 68 74 5b  Right] = aRight[
35b18 69 52 69 67 68 74 5d 3b 0a 20 20 20 20 20 20 20  iRight];.       
35b19 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20     iRight++;.   
35b1a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
35b1b 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20     }..#if 0.    
35b1c 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
35b1d 65 20 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f  e sort worked */
35b1e 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74  .    {.      int
35b1f 20 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a   jj;.      for(j
35b20 6a 3d 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a  j=1; jj<nIdx; jj
35b21 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f  ++){.        flo
35b22 61 74 20 6c 65 66 74 20 3d 20 61 44 69 73 74 61  at left = aDista
35b23 6e 63 65 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 3b  nce[aIdx[jj-1]];
35b24 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 72  .        float r
35b25 69 67 68 74 20 3d 20 61 44 69 73 74 61 6e 63 65  ight = aDistance
35b26 5b 61 49 64 78 5b 6a 6a 5d 5d 3b 0a 20 20 20 20  [aIdx[jj]];.    
35b27 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 66 74      assert( left
35b28 3c 3d 72 69 67 68 74 20 29 3b 0a 20 20 20 20 20  <=right );.     
35b29 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
35b2a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67    }.}../*.** Arg
35b2b 75 6d 65 6e 74 73 20 61 49 64 78 2c 20 61 43 65  uments aIdx, aCe
35b2c 6c 6c 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c  ll and aSpare al
35b2d 6c 20 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79  l point to array
35b2e 73 20 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64  s of size.** nId
35b2f 78 2e 20 54 68 65 20 61 49 64 78 20 61 72 72 61  x. The aIdx arra
35b30 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73  y contains the s
35b31 65 74 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66  et of integers f
35b32 72 6f 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49  rom 0 to .** (nI
35b33 64 78 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74  dx-1) in no part
35b34 69 63 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68  icular order. Th
35b35 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74  is function sort
35b36 73 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20  s the values.** 
35b37 69 6e 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e  in aIdx accordin
35b38 67 20 74 6f 20 64 69 6d 65 6e 73 69 6f 6e 20 69  g to dimension i
35b39 44 69 6d 20 6f 66 20 74 68 65 20 63 65 6c 6c 73  Dim of the cells
35b3a 20 69 6e 20 61 43 65 6c 6c 2e 20 54 68 65 0a 2a   in aCell. The.*
35b3b 2a 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20  * minimum value 
35b3c 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69  of dimension iDi
35b3d 6d 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  m is considered 
35b3e 66 69 72 73 74 2c 20 74 68 65 0a 2a 2a 20 6d 61  first, the.** ma
35b3f 78 69 6d 75 6d 20 75 73 65 64 20 74 6f 20 62 72  ximum used to br
35b40 65 61 6b 20 74 69 65 73 2e 0a 2a 2a 0a 2a 2a 20  eak ties..**.** 
35b41 54 68 65 20 61 53 70 61 72 65 20 61 72 72 61 79  The aSpare array
35b42 20 69 73 20 75 73 65 64 20 61 73 20 74 65 6d 70   is used as temp
35b43 6f 72 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70  orary working sp
35b44 61 63 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f  ace by the.** so
35b45 72 74 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e  rting algorithm.
35b46 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
35b47 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28  SortByDimension(
35b48 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
35b49 2c 0a 20 20 69 6e 74 20 2a 61 49 64 78 2c 20 0a  ,.  int *aIdx, .
35b4a 20 20 69 6e 74 20 6e 49 64 78 2c 20 0a 20 20 69    int nIdx, .  i
35b4b 6e 74 20 69 44 69 6d 2c 20 0a 20 20 52 74 72 65  nt iDim, .  Rtre
35b4c 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20  eCell *aCell, . 
35b4d 20 69 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a   int *aSpare.){.
35b4e 20 20 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a    if( nIdx>1 ){.
35b4f 0a 20 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d  .    int iLeft =
35b50 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67   0;.    int iRig
35b51 68 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74  ht = 0;..    int
35b52 20 6e 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b   nLeft = nIdx/2;
35b53 0a 20 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20  .    int nRight 
35b54 3d 20 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20  = nIdx-nLeft;.  
35b55 20 20 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61    int *aLeft = a
35b56 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52  Idx;.    int *aR
35b57 69 67 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65  ight = &aIdx[nLe
35b58 66 74 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79  ft];..    SortBy
35b59 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
35b5a 2c 20 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20  , aLeft, nLeft, 
35b5b 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70  iDim, aCell, aSp
35b5c 61 72 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79  are);.    SortBy
35b5d 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
35b5e 2c 20 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74  , aRight, nRight
35b5f 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61  , iDim, aCell, a
35b60 53 70 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d  Spare);..    mem
35b61 63 70 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66  cpy(aSpare, aLef
35b62 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  t, sizeof(int)*n
35b63 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74  Left);.    aLeft
35b64 20 3d 20 61 53 70 61 72 65 3b 0a 20 20 20 20 77   = aSpare;.    w
35b65 68 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66  hile( iLeft<nLef
35b66 74 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67  t || iRight<nRig
35b67 68 74 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62  ht ){.      doub
35b68 6c 65 20 78 6c 65 66 74 31 20 3d 20 44 43 4f 4f  le xleft1 = DCOO
35b69 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69  RD(aCell[aLeft[i
35b6a 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44  Left]].aCoord[iD
35b6b 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f  im*2]);.      do
35b6c 75 62 6c 65 20 78 6c 65 66 74 32 20 3d 20 44 43  uble xleft2 = DC
35b6d 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74  OORD(aCell[aLeft
35b6e 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b  [iLeft]].aCoord[
35b6f 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20  iDim*2+1]);.    
35b70 20 20 64 6f 75 62 6c 65 20 78 72 69 67 68 74 31    double xright1
35b71 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
35b72 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e  aRight[iRight]].
35b73 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b  aCoord[iDim*2]);
35b74 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72  .      double xr
35b75 69 67 68 74 32 20 3d 20 44 43 4f 4f 52 44 28 61  ight2 = DCOORD(a
35b76 43 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67  Cell[aRight[iRig
35b77 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ht]].aCoord[iDim
35b78 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66  *2+1]);.      if
35b79 28 20 28 69 4c 65 66 74 21 3d 6e 4c 65 66 74 29  ( (iLeft!=nLeft)
35b7a 20 26 26 20 28 28 69 52 69 67 68 74 3d 3d 6e 52   && ((iRight==nR
35b7b 69 67 68 74 29 0a 20 20 20 20 20 20 20 7c 7c 20  ight).       || 
35b7c 28 78 6c 65 66 74 31 3c 78 72 69 67 68 74 31 29  (xleft1<xright1)
35b7d 0a 20 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66  .       || (xlef
35b7e 74 31 3d 3d 78 72 69 67 68 74 31 20 26 26 20 78  t1==xright1 && x
35b7f 6c 65 66 74 32 3c 78 72 69 67 68 74 32 29 0a 20  left2<xright2). 
35b80 20 20 20 20 20 29 29 7b 0a 20 20 20 20 20 20 20       )){.       
35b81 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
35b82 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66  ht] = aLeft[iLef
35b83 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66  t];.        iLef
35b84 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  t++;.      }else
35b85 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b 69  {.        aIdx[i
35b86 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
35b87 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20  Right[iRight];. 
35b88 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b         iRight++;
35b89 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
35b8a 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 43 68 65  #if 0.    /* Che
35b8b 63 6b 20 74 68 61 74 20 74 68 65 20 73 6f 72 74  ck that the sort
35b8c 20 77 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20 7b   worked */.    {
35b8d 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20  .      int jj;. 
35b8e 20 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a       for(jj=1; j
35b8f 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a 20  j<nIdx; jj++){. 
35b90 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65         float xle
35b91 66 74 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78  ft1 = aCell[aIdx
35b92 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69  [jj-1]].aCoord[i
35b93 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20  Dim*2];.        
35b94 66 6c 6f 61 74 20 78 6c 65 66 74 32 20 3d 20 61  float xleft2 = a
35b95 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d  Cell[aIdx[jj-1]]
35b96 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31  .aCoord[iDim*2+1
35b97 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ];.        float
35b98 20 78 72 69 67 68 74 31 20 3d 20 61 43 65 6c 6c   xright1 = aCell
35b99 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72  [aIdx[jj]].aCoor
35b9a 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20  d[iDim*2];.     
35b9b 20 20 20 66 6c 6f 61 74 20 78 72 69 67 68 74 32     float xright2
35b9c 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a   = aCell[aIdx[jj
35b9d 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
35b9e 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73  +1];.        ass
35b9f 65 72 74 28 20 78 6c 65 66 74 31 3c 3d 78 72 69  ert( xleft1<=xri
35ba0 67 68 74 31 20 26 26 20 28 78 6c 65 66 74 31 3c  ght1 && (xleft1<
35ba1 78 72 69 67 68 74 31 20 7c 7c 20 78 6c 65 66 74  xright1 || xleft
35ba2 32 3c 3d 78 72 69 67 68 74 32 29 20 29 3b 0a 20  2<=xright2) );. 
35ba3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
35ba4 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 56  dif.  }.}..#if V
35ba5 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
35ba6 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70  _SPLIT./*.** Imp
35ba7 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
35ba8 68 65 20 52 2a 2d 74 72 65 65 20 76 61 72 69 61  he R*-tree varia
35ba9 6e 74 20 6f 66 20 53 70 6c 69 74 4e 6f 64 65 20  nt of SplitNode 
35baa 66 72 6f 6d 20 42 65 63 6b 6d 61 6e 5b 31 39 39  from Beckman[199
35bab 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  0]..*/.static in
35bac 74 20 73 70 6c 69 74 4e 6f 64 65 53 74 61 72 74  t splitNodeStart
35bad 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ree(.  Rtree *pR
35bae 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c  tree,.  RtreeCel
35baf 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20  l *aCell,.  int 
35bb0 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f  nCell,.  RtreeNo
35bb1 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72  de *pLeft,.  Rtr
35bb2 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a  eeNode *pRight,.
35bb3 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62    RtreeCell *pBb
35bb4 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43  oxLeft,.  RtreeC
35bb5 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a  ell *pBboxRight.
35bb6 29 7b 0a 20 20 69 6e 74 20 2a 2a 61 61 53 6f 72  ){.  int **aaSor
35bb7 74 65 64 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61  ted;.  int *aSpa
35bb8 72 65 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20  re;.  int ii;.. 
35bb9 20 69 6e 74 20 69 42 65 73 74 44 69 6d 3b 0a 20   int iBestDim;. 
35bba 20 69 6e 74 20 69 42 65 73 74 53 70 6c 69 74 3b   int iBestSplit;
35bbb 0a 20 20 66 6c 6f 61 74 20 66 42 65 73 74 4d 61  .  float fBestMa
35bbc 72 67 69 6e 3b 0a 0a 20 20 69 6e 74 20 6e 42 79  rgin;..  int nBy
35bbd 74 65 20 3d 20 28 70 52 74 72 65 65 2d 3e 6e 44  te = (pRtree->nD
35bbe 69 6d 2b 31 29 2a 28 73 69 7a 65 6f 66 28 69 6e  im+1)*(sizeof(in
35bbf 74 2a 29 2b 6e 43 65 6c 6c 2a 73 69 7a 65 6f 66  t*)+nCell*sizeof
35bc0 28 69 6e 74 29 29 3b 0a 0a 20 20 61 61 53 6f 72  (int));..  aaSor
35bc1 74 65 64 20 3d 20 28 69 6e 74 20 2a 2a 29 73 71  ted = (int **)sq
35bc2 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79  lite3_malloc(nBy
35bc3 74 65 29 3b 0a 20 20 69 66 28 20 21 61 61 53 6f  te);.  if( !aaSo
35bc4 72 74 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  rted ){.    retu
35bc5 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
35bc6 0a 20 20 7d 0a 0a 20 20 61 53 70 61 72 65 20 3d  .  }..  aSpare =
35bc7 20 26 28 28 69 6e 74 20 2a 29 26 61 61 53 6f 72   &((int *)&aaSor
35bc8 74 65 64 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ted[pRtree->nDim
35bc9 5d 29 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  ])[pRtree->nDim*
35bca 6e 43 65 6c 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74  nCell];.  memset
35bcb 28 61 61 53 6f 72 74 65 64 2c 20 30 2c 20 6e 42  (aaSorted, 0, nB
35bcc 79 74 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30  yte);.  for(ii=0
35bcd 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69  ; ii<pRtree->nDi
35bce 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  m; ii++){.    in
35bcf 74 20 6a 6a 3b 0a 20 20 20 20 61 61 53 6f 72 74  t jj;.    aaSort
35bd0 65 64 5b 69 69 5d 20 3d 20 26 28 28 69 6e 74 20  ed[ii] = &((int 
35bd1 2a 29 26 61 61 53 6f 72 74 65 64 5b 70 52 74 72  *)&aaSorted[pRtr
35bd2 65 65 2d 3e 6e 44 69 6d 5d 29 5b 69 69 2a 6e 43  ee->nDim])[ii*nC
35bd3 65 6c 6c 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 6a  ell];.    for(jj
35bd4 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a  =0; jj<nCell; jj
35bd5 2b 2b 29 7b 0a 20 20 20 20 20 20 61 61 53 6f 72  ++){.      aaSor
35bd6 74 65 64 5b 69 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a  ted[ii][jj] = jj
35bd7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 53 6f 72 74  ;.    }.    Sort
35bd8 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72  ByDimension(pRtr
35bd9 65 65 2c 20 61 61 53 6f 72 74 65 64 5b 69 69 5d  ee, aaSorted[ii]
35bda 2c 20 6e 43 65 6c 6c 2c 20 69 69 2c 20 61 43 65  , nCell, ii, aCe
35bdb 6c 6c 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 7d  ll, aSpare);.  }
35bdc 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
35bdd 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69  <pRtree->nDim; i
35bde 69 2b 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20  i++){.    float 
35bdf 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20  margin = 0.0;.  
35be0 20 20 66 6c 6f 61 74 20 66 42 65 73 74 4f 76 65    float fBestOve
35be1 72 6c 61 70 3b 0a 20 20 20 20 66 6c 6f 61 74 20  rlap;.    float 
35be2 66 42 65 73 74 41 72 65 61 3b 0a 20 20 20 20 69  fBestArea;.    i
35be3 6e 74 20 69 42 65 73 74 4c 65 66 74 3b 0a 20 20  nt iBestLeft;.  
35be4 20 20 69 6e 74 20 6e 4c 65 66 74 3b 0a 0a 20 20    int nLeft;..  
35be5 20 20 66 6f 72 28 0a 20 20 20 20 20 20 6e 4c 65    for(.      nLe
35be6 66 74 3d 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c  ft=RTREE_MINCELL
35be7 53 28 70 52 74 72 65 65 29 3b 20 0a 20 20 20 20  S(pRtree); .    
35be8 20 20 6e 4c 65 66 74 3c 3d 28 6e 43 65 6c 6c 2d    nLeft<=(nCell-
35be9 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
35bea 52 74 72 65 65 29 29 3b 20 0a 20 20 20 20 20 20  Rtree)); .      
35beb 6e 4c 65 66 74 2b 2b 0a 20 20 20 20 29 7b 0a 20  nLeft++.    ){. 
35bec 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 6c       RtreeCell l
35bed 65 66 74 3b 0a 20 20 20 20 20 20 52 74 72 65 65  eft;.      Rtree
35bee 43 65 6c 6c 20 72 69 67 68 74 3b 0a 20 20 20 20  Cell right;.    
35bef 20 20 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20    int kk;.      
35bf0 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 3b 0a 20  float overlap;. 
35bf1 20 20 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b       float area;
35bf2 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ..      memcpy(&
35bf3 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  left, &aCell[aaS
35bf4 6f 72 74 65 64 5b 69 69 5d 5b 30 5d 5d 2c 20 73  orted[ii][0]], s
35bf5 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
35bf6 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
35bf7 26 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61  &right, &aCell[a
35bf8 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6e 43 65 6c  aSorted[ii][nCel
35bf9 6c 2d 31 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74  l-1]], sizeof(Rt
35bfa 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20  reeCell));.     
35bfb 20 66 6f 72 28 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e   for(kk=1; kk<(n
35bfc 43 65 6c 6c 2d 31 29 3b 20 6b 6b 2b 2b 29 7b 0a  Cell-1); kk++){.
35bfd 20 20 20 20 20 20 20 20 69 66 28 20 6b 6b 3c 6e          if( kk<n
35bfe 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Left ){.        
35bff 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72    cellUnion(pRtr
35c00 65 65 2c 20 26 6c 65 66 74 2c 20 26 61 43 65 6c  ee, &left, &aCel
35c01 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b  l[aaSorted[ii][k
35c02 6b 5d 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  k]]);.        }e
35c03 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
35c04 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c  ellUnion(pRtree,
35c05 20 26 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b   &right, &aCell[
35c06 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d  aaSorted[ii][kk]
35c07 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ]);.        }.  
35c08 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 61 72 67      }.      marg
35c09 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e  in += cellMargin
35c0a 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74 29 3b  (pRtree, &left);
35c0b 0a 20 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d  .      margin +=
35c0c 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72   cellMargin(pRtr
35c0d 65 65 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20  ee, &right);.   
35c0e 20 20 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c     overlap = cel
35c0f 6c 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c  lOverlap(pRtree,
35c10 20 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20   &left, &right, 
35c11 31 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 61 72  1, -1);.      ar
35c12 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52  ea = cellArea(pR
35c13 74 72 65 65 2c 20 26 6c 65 66 74 29 20 2b 20 63  tree, &left) + c
35c14 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20  ellArea(pRtree, 
35c15 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69  &right);.      i
35c16 66 28 20 28 6e 4c 65 66 74 3d 3d 52 54 52 45 45  f( (nLeft==RTREE
35c17 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
35c18 29 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76  )).       || (ov
35c19 65 72 6c 61 70 3c 66 42 65 73 74 4f 76 65 72 6c  erlap<fBestOverl
35c1a 61 70 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f  ap).       || (o
35c1b 76 65 72 6c 61 70 3d 3d 66 42 65 73 74 4f 76 65  verlap==fBestOve
35c1c 72 6c 61 70 20 26 26 20 61 72 65 61 3c 66 42 65  rlap && area<fBe
35c1d 73 74 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b  stArea).      ){
35c1e 0a 20 20 20 20 20 20 20 20 69 42 65 73 74 4c 65  .        iBestLe
35c1f 66 74 20 3d 20 6e 4c 65 66 74 3b 0a 20 20 20 20  ft = nLeft;.    
35c20 20 20 20 20 66 42 65 73 74 4f 76 65 72 6c 61 70      fBestOverlap
35c21 20 3d 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20   = overlap;.    
35c22 20 20 20 20 66 42 65 73 74 41 72 65 61 20 3d 20      fBestArea = 
35c23 61 72 65 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20  area;.      }.  
35c24 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 69 3d    }..    if( ii=
35c25 3d 30 20 7c 7c 20 6d 61 72 67 69 6e 3c 66 42 65  =0 || margin<fBe
35c26 73 74 4d 61 72 67 69 6e 20 29 7b 0a 20 20 20 20  stMargin ){.    
35c27 20 20 69 42 65 73 74 44 69 6d 20 3d 20 69 69 3b    iBestDim = ii;
35c28 0a 20 20 20 20 20 20 66 42 65 73 74 4d 61 72 67  .      fBestMarg
35c29 69 6e 20 3d 20 6d 61 72 67 69 6e 3b 0a 20 20 20  in = margin;.   
35c2a 20 20 20 69 42 65 73 74 53 70 6c 69 74 20 3d 20     iBestSplit = 
35c2b 69 42 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 7d  iBestLeft;.    }
35c2c 0a 20 20 7d 0a 0a 20 20 6d 65 6d 63 70 79 28 70  .  }..  memcpy(p
35c2d 42 62 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c  BboxLeft, &aCell
35c2e 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73 74 44  [aaSorted[iBestD
35c2f 69 6d 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66 28  im][0]], sizeof(
35c30 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d  RtreeCell));.  m
35c31 65 6d 63 70 79 28 70 42 62 6f 78 52 69 67 68 74  emcpy(pBboxRight
35c32 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65  , &aCell[aaSorte
35c33 64 5b 69 42 65 73 74 44 69 6d 5d 5b 69 42 65 73  d[iBestDim][iBes
35c34 74 53 70 6c 69 74 5d 5d 2c 20 73 69 7a 65 6f 66  tSplit]], sizeof
35c35 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
35c36 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65  for(ii=0; ii<nCe
35c37 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52  ll; ii++){.    R
35c38 74 72 65 65 4e 6f 64 65 20 2a 70 54 61 72 67 65  treeNode *pTarge
35c39 74 20 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c  t = (ii<iBestSpl
35c3a 69 74 29 3f 70 4c 65 66 74 3a 70 52 69 67 68 74  it)?pLeft:pRight
35c3b 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  ;.    RtreeCell 
35c3c 2a 70 42 62 6f 78 20 3d 20 28 69 69 3c 69 42 65  *pBbox = (ii<iBe
35c3d 73 74 53 70 6c 69 74 29 3f 70 42 62 6f 78 4c 65  stSplit)?pBboxLe
35c3e 66 74 3a 70 42 62 6f 78 52 69 67 68 74 3b 0a 20  ft:pBboxRight;. 
35c3f 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43     RtreeCell *pC
35c40 65 6c 6c 20 3d 20 26 61 43 65 6c 6c 5b 61 61 53  ell = &aCell[aaS
35c41 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b  orted[iBestDim][
35c42 69 69 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49 6e  ii]];.    nodeIn
35c43 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
35c44 20 70 54 61 72 67 65 74 2c 20 70 43 65 6c 6c 29   pTarget, pCell)
35c45 3b 0a 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28  ;.    cellUnion(
35c46 70 52 74 72 65 65 2c 20 70 42 62 6f 78 2c 20 70  pRtree, pBbox, p
35c47 43 65 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71  Cell);.  }..  sq
35c48 6c 69 74 65 33 5f 66 72 65 65 28 61 61 53 6f 72  lite3_free(aaSor
35c49 74 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ted);.  return S
35c4a 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
35c4b 69 66 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  if..#if VARIANT_
35c4c 47 55 54 54 4d 41 4e 5f 53 50 4c 49 54 0a 2f 2a  GUTTMAN_SPLIT./*
35c4d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
35c4e 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61  on of the regula
35c4f 72 20 52 2d 74 72 65 65 20 53 70 6c 69 74 4e 6f  r R-tree SplitNo
35c50 64 65 20 66 72 6f 6d 20 47 75 74 74 6d 61 6e 5b  de from Guttman[
35c51 31 39 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  1984]..*/.static
35c52 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65 47 75   int splitNodeGu
35c53 74 74 6d 61 6e 28 0a 20 20 52 74 72 65 65 20 2a  ttman(.  Rtree *
35c54 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43  pRtree,.  RtreeC
35c55 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e  ell *aCell,.  in
35c56 74 20 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65  t nCell,.  Rtree
35c57 4e 6f 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52  Node *pLeft,.  R
35c58 74 72 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74  treeNode *pRight
35c59 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ,.  RtreeCell *p
35c5a 42 62 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65  BboxLeft,.  Rtre
35c5b 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68  eCell *pBboxRigh
35c5c 74 0a 29 7b 0a 20 20 69 6e 74 20 69 4c 65 66 74  t.){.  int iLeft
35c5d 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Seed = 0;.  int 
35c5e 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a  iRightSeed = 1;.
35c5f 20 20 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 20    int *aiUsed;. 
35c60 20 69 6e 74 20 69 3b 0a 0a 20 20 61 69 55 73 65   int i;..  aiUse
35c61 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  d = sqlite3_mall
35c62 6f 63 28 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  oc(sizeof(int)*n
35c63 43 65 6c 6c 29 3b 0a 20 20 6d 65 6d 73 65 74 28  Cell);.  memset(
35c64 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f  aiUsed, 0, sizeo
35c65 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 0a  f(int)*nCell);..
35c66 20 20 50 69 63 6b 53 65 65 64 73 28 70 52 74 72    PickSeeds(pRtr
35c67 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c  ee, aCell, nCell
35c68 2c 20 26 69 4c 65 66 74 53 65 65 64 2c 20 26 69  , &iLeftSeed, &i
35c69 52 69 67 68 74 53 65 65 64 29 3b 0a 0a 20 20 6d  RightSeed);..  m
35c6a 65 6d 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c  emcpy(pBboxLeft,
35c6b 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65   &aCell[iLeftSee
35c6c 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  d], sizeof(Rtree
35c6d 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79  Cell));.  memcpy
35c6e 28 70 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43  (pBboxRight, &aC
35c6f 65 6c 6c 5b 69 52 69 67 68 74 53 65 65 64 5d 2c  ell[iRightSeed],
35c70 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c   sizeof(RtreeCel
35c71 6c 29 29 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72  l));.  nodeInser
35c72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c  tCell(pRtree, pL
35c73 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66  eft, &aCell[iLef
35c74 74 53 65 65 64 5d 29 3b 0a 20 20 6e 6f 64 65 49  tSeed]);.  nodeI
35c75 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
35c76 2c 20 70 52 69 67 68 74 2c 20 26 61 43 65 6c 6c  , pRight, &aCell
35c77 5b 69 52 69 67 68 74 53 65 65 64 5d 29 3b 0a 20  [iRightSeed]);. 
35c78 20 61 69 55 73 65 64 5b 69 4c 65 66 74 53 65 65   aiUsed[iLeftSee
35c79 64 5d 20 3d 20 31 3b 0a 20 20 61 69 55 73 65 64  d] = 1;.  aiUsed
35c7a 5b 69 52 69 67 68 74 53 65 65 64 5d 20 3d 20 31  [iRightSeed] = 1
35c7b 3b 0a 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c 6c  ;..  for(i=nCell
35c7c 2d 32 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20  -2; i>0; i--){. 
35c7d 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4e     RtreeCell *pN
35c7e 65 78 74 3b 0a 20 20 20 20 70 4e 65 78 74 20 3d  ext;.    pNext =
35c7f 20 50 69 63 6b 4e 65 78 74 28 70 52 74 72 65 65   PickNext(pRtree
35c80 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20  , aCell, nCell, 
35c81 70 42 62 6f 78 4c 65 66 74 2c 20 70 42 62 6f 78  pBboxLeft, pBbox
35c82 52 69 67 68 74 2c 20 61 69 55 73 65 64 29 3b 0a  Right, aiUsed);.
35c83 20 20 20 20 66 6c 6f 61 74 20 64 69 66 66 20 3d      float diff =
35c84 20 20 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f    .      cellGro
35c85 77 74 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f  wth(pRtree, pBbo
35c86 78 4c 65 66 74 2c 20 70 4e 65 78 74 29 20 2d 20  xLeft, pNext) - 
35c87 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74  .      cellGrowt
35c88 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52  h(pRtree, pBboxR
35c89 69 67 68 74 2c 20 70 4e 65 78 74 29 0a 20 20 20  ight, pNext).   
35c8a 20 3b 0a 20 20 20 20 69 66 28 20 28 52 54 52 45   ;.    if( (RTRE
35c8b 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65  E_MINCELLS(pRtre
35c8c 65 29 2d 4e 43 45 4c 4c 28 70 52 69 67 68 74 29  e)-NCELL(pRight)
35c8d 3d 3d 69 29 0a 20 20 20 20 20 7c 7c 20 28 64 69  ==i).     || (di
35c8e 66 66 3e 30 2e 30 20 26 26 20 28 52 54 52 45 45  ff>0.0 && (RTREE
35c8f 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
35c90 29 2d 4e 43 45 4c 4c 28 70 4c 65 66 74 29 21 3d  )-NCELL(pLeft)!=
35c91 69 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  i)).    ){.     
35c92 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28   nodeInsertCell(
35c93 70 52 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20  pRtree, pRight, 
35c94 70 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 63 65  pNext);.      ce
35c95 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20  llUnion(pRtree, 
35c96 70 42 62 6f 78 52 69 67 68 74 2c 20 70 4e 65 78  pBboxRight, pNex
35c97 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
35c98 20 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43       nodeInsertC
35c99 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66  ell(pRtree, pLef
35c9a 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20  t, pNext);.     
35c9b 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
35c9c 65 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 4e  e, pBboxLeft, pN
35c9d 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ext);.    }.  }.
35c9e 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
35c9f 61 69 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  aiUsed);.  retur
35ca0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
35ca1 65 6e 64 69 66 0a 0a 73 74 61 74 69 63 20 69 6e  endif..static in
35ca2 74 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  t updateMapping(
35ca3 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
35ca4 2c 20 0a 20 20 69 36 34 20 69 52 6f 77 69 64 2c  , .  i64 iRowid,
35ca5 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70   .  RtreeNode *p
35ca6 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 48 65  Node, .  int iHe
35ca7 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 28 2a  ight.){.  int (*
35ca8 78 53 65 74 4d 61 70 70 69 6e 67 29 28 52 74 72  xSetMapping)(Rtr
35ca9 65 65 20 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  ee *, sqlite3_in
35caa 74 36 34 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  t64, sqlite3_int
35cab 36 34 29 3b 0a 20 20 78 53 65 74 4d 61 70 70 69  64);.  xSetMappi
35cac 6e 67 20 3d 20 28 28 69 48 65 69 67 68 74 3d 3d  ng = ((iHeight==
35cad 30 29 3f 72 6f 77 69 64 57 72 69 74 65 3a 70 61  0)?rowidWrite:pa
35cae 72 65 6e 74 57 72 69 74 65 29 3b 0a 20 20 69 66  rentWrite);.  if
35caf 28 20 69 48 65 69 67 68 74 3e 30 20 29 7b 0a 20  ( iHeight>0 ){. 
35cb0 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43     RtreeNode *pC
35cb1 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c  hild = nodeHashL
35cb2 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 69 52  ookup(pRtree, iR
35cb3 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70  owid);.    if( p
35cb4 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e  Child ){.      n
35cb5 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
35cb6 65 2c 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65  e, pChild->pPare
35cb7 6e 74 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52  nt);.      nodeR
35cb8 65 66 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b  eference(pNode);
35cb9 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70  .      pChild->p
35cba 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a  Parent = pNode;.
35cbb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
35cbc 72 6e 20 78 53 65 74 4d 61 70 70 69 6e 67 28 70  rn xSetMapping(p
35cbd 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70  Rtree, iRowid, p
35cbe 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 7d 0a  Node->iNode);.}.
35cbf 0a 73 74 61 74 69 63 20 69 6e 74 20 53 70 6c 69  .static int Spli
35cc0 74 4e 6f 64 65 28 0a 20 20 52 74 72 65 65 20 2a  tNode(.  Rtree *
35cc1 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 4e  pRtree,.  RtreeN
35cc2 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20 20 52 74  ode *pNode,.  Rt
35cc3 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a  reeCell *pCell,.
35cc4 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b    int iHeight.){
35cc5 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
35cc6 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d  newCellIsRight =
35cc7 20 30 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20   0;..  int rc = 
35cc8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
35cc9 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
35cca 4e 6f 64 65 29 3b 0a 20 20 52 74 72 65 65 43 65  Node);.  RtreeCe
35ccb 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20 69 6e 74  ll *aCell;.  int
35ccc 20 2a 61 69 55 73 65 64 3b 0a 0a 20 20 52 74 72   *aiUsed;..  Rtr
35ccd 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 20 3d 20  eeNode *pLeft = 
35cce 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  0;.  RtreeNode *
35ccf 70 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 52  pRight = 0;..  R
35cd0 74 72 65 65 43 65 6c 6c 20 6c 65 66 74 62 62 6f  treeCell leftbbo
35cd1 78 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 72  x;.  RtreeCell r
35cd2 69 67 68 74 62 62 6f 78 3b 0a 0a 20 20 2f 2a 20  ightbbox;..  /* 
35cd3 41 6c 6c 6f 63 61 74 65 20 61 6e 20 61 72 72 61  Allocate an arra
35cd4 79 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 69  y and populate i
35cd5 74 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66  t with a copy of
35cd6 20 70 43 65 6c 6c 20 61 6e 64 20 0a 20 20 2a 2a   pCell and .  **
35cd7 20 61 6c 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20   all cells from 
35cd8 6e 6f 64 65 20 70 4c 65 66 74 2e 20 54 68 65 6e  node pLeft. Then
35cd9 20 7a 65 72 6f 20 74 68 65 20 6f 72 69 67 69 6e   zero the origin
35cda 61 6c 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  al node..  */.  
35cdb 61 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f  aCell = sqlite3_
35cdc 6d 61 6c 6c 6f 63 28 28 73 69 7a 65 6f 66 28 52  malloc((sizeof(R
35cdd 74 72 65 65 43 65 6c 6c 29 2b 73 69 7a 65 6f 66  treeCell)+sizeof
35cde 28 69 6e 74 29 29 2a 28 6e 43 65 6c 6c 2b 31 29  (int))*(nCell+1)
35cdf 29 3b 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20  );.  if( !aCell 
35ce0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
35ce1 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
35ce2 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74  to splitnode_out
35ce3 3b 0a 20 20 7d 0a 20 20 61 69 55 73 65 64 20 3d  ;.  }.  aiUsed =
35ce4 20 28 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e   (int *)&aCell[n
35ce5 43 65 6c 6c 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65  Cell+1];.  memse
35ce6 74 28 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a  t(aiUsed, 0, siz
35ce7 65 6f 66 28 69 6e 74 29 2a 28 6e 43 65 6c 6c 2b  eof(int)*(nCell+
35ce8 31 29 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  1));.  for(i=0; 
35ce9 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20  i<nCell; i++){. 
35cea 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70     nodeGetCell(p
35ceb 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 2c  Rtree, pNode, i,
35cec 20 26 61 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20 7d   &aCell[i]);.  }
35ced 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72  .  nodeZero(pRtr
35cee 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 6d 65  ee, pNode);.  me
35cef 6d 63 70 79 28 26 61 43 65 6c 6c 5b 6e 43 65 6c  mcpy(&aCell[nCel
35cf0 6c 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f  l], pCell, sizeo
35cf1 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20  f(RtreeCell));. 
35cf2 20 6e 43 65 6c 6c 2b 2b 3b 0a 0a 20 20 69 66 28   nCell++;..  if(
35cf3 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31   pNode->iNode==1
35cf4 20 29 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d   ){.    pRight =
35cf5 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c   nodeNew(pRtree,
35cf6 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20 20 20 20   pNode, 1);.    
35cf7 70 4c 65 66 74 20 3d 20 6e 6f 64 65 4e 65 77 28  pLeft = nodeNew(
35cf8 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 31  pRtree, pNode, 1
35cf9 29 3b 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69  );.    pRtree->i
35cfa 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 70 4e 6f  Depth++;.    pNo
35cfb 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
35cfc 0a 20 20 20 20 77 72 69 74 65 49 6e 74 31 36 28  .    writeInt16(
35cfd 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 70 52  pNode->zData, pR
35cfe 74 72 65 65 2d 3e 69 44 65 70 74 68 29 3b 0a 20  tree->iDepth);. 
35cff 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 65 66   }else{.    pLef
35d00 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 70  t = pNode;.    p
35d01 52 69 67 68 74 20 3d 20 6e 6f 64 65 4e 65 77 28  Right = nodeNew(
35d02 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2d 3e 70  pRtree, pLeft->p
35d03 50 61 72 65 6e 74 2c 20 31 29 3b 0a 20 20 20 20  Parent, 1);.    
35d04 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 4c  nodeReference(pL
35d05 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  eft);.  }..  if(
35d06 20 21 70 4c 65 66 74 20 7c 7c 20 21 70 52 69 67   !pLeft || !pRig
35d07 68 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ht ){.    rc = S
35d08 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
35d09 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
35d0a 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 73  out;.  }..  mems
35d0b 65 74 28 70 4c 65 66 74 2d 3e 7a 44 61 74 61 2c  et(pLeft->zData,
35d0c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64   0, pRtree->iNod
35d0d 65 53 69 7a 65 29 3b 0a 20 20 6d 65 6d 73 65 74  eSize);.  memset
35d0e 28 70 52 69 67 68 74 2d 3e 7a 44 61 74 61 2c 20  (pRight->zData, 
35d0f 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  0, pRtree->iNode
35d10 53 69 7a 65 29 3b 0a 0a 20 20 72 63 20 3d 20 41  Size);..  rc = A
35d11 73 73 69 67 6e 43 65 6c 6c 73 28 70 52 74 72 65  ssignCells(pRtre
35d12 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  e, aCell, nCell,
35d13 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20   pLeft, pRight, 
35d14 26 6c 65 66 74 62 62 6f 78 2c 20 26 72 69 67 68  &leftbbox, &righ
35d15 74 62 62 6f 78 29 3b 0a 20 20 69 66 28 20 72 63  tbbox);.  if( rc
35d16 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
35d17 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64     goto splitnod
35d18 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e_out;.  }..  /*
35d19 20 45 6e 73 75 72 65 20 62 6f 74 68 20 63 68 69   Ensure both chi
35d1a 6c 64 20 6e 6f 64 65 73 20 68 61 76 65 20 6e 6f  ld nodes have no
35d1b 64 65 20 6e 75 6d 62 65 72 73 20 61 73 73 69 67  de numbers assig
35d1c 6e 65 64 20 74 6f 20 74 68 65 6d 2e 20 2a 2f 0a  ned to them. */.
35d1d 20 20 69 66 28 20 28 30 3d 3d 70 52 69 67 68 74    if( (0==pRight
35d1e 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51 4c 49 54  ->iNode && SQLIT
35d1f 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65  E_OK!=(rc = node
35d20 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 52  Write(pRtree, pR
35d21 69 67 68 74 29 29 29 0a 20 20 20 7c 7c 20 28 30  ight))).   || (0
35d22 3d 3d 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 20 26  ==pLeft->iNode &
35d23 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  & SQLITE_OK!=(rc
35d24 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74   = nodeWrite(pRt
35d25 72 65 65 2c 20 70 4c 65 66 74 29 29 29 0a 20 20  ree, pLeft))).  
35d26 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  ){.    goto spli
35d27 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  tnode_out;.  }..
35d28 20 20 72 69 67 68 74 62 62 6f 78 2e 69 52 6f 77    rightbbox.iRow
35d29 69 64 20 3d 20 70 52 69 67 68 74 2d 3e 69 4e 6f  id = pRight->iNo
35d2a 64 65 3b 0a 20 20 6c 65 66 74 62 62 6f 78 2e 69  de;.  leftbbox.i
35d2b 52 6f 77 69 64 20 3d 20 70 4c 65 66 74 2d 3e 69  Rowid = pLeft->i
35d2c 4e 6f 64 65 3b 0a 0a 20 20 69 66 28 20 70 4e 6f  Node;..  if( pNo
35d2d 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a  de->iNode==1 ){.
35d2e 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e      rc = rtreeIn
35d2f 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
35d30 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 2c   pLeft->pParent,
35d31 20 26 6c 65 66 74 62 62 6f 78 2c 20 69 48 65 69   &leftbbox, iHei
35d32 67 68 74 2b 31 29 3b 0a 20 20 20 20 69 66 28 20  ght+1);.    if( 
35d33 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
35d34 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  .      goto spli
35d35 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  tnode_out;.    }
35d36 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74  .  }else{.    Rt
35d37 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
35d38 20 3d 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e   = pLeft->pParen
35d39 74 3b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  t;.    int iCell
35d3a 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
35d3b 65 78 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  ex(pRtree, pLeft
35d3c 29 3b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77  );.    nodeOverw
35d3d 72 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c  riteCell(pRtree,
35d3e 20 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62   pParent, &leftb
35d3f 62 6f 78 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20  box, iCell);.   
35d40 20 41 64 6a 75 73 74 54 72 65 65 28 70 52 74 72   AdjustTree(pRtr
35d41 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c 65  ee, pParent, &le
35d42 66 74 62 62 6f 78 29 3b 0a 20 20 7d 0a 20 20 69  ftbbox);.  }.  i
35d43 66 28 20 28 72 63 20 3d 20 72 74 72 65 65 49 6e  f( (rc = rtreeIn
35d44 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
35d45 20 70 52 69 67 68 74 2d 3e 70 50 61 72 65 6e 74   pRight->pParent
35d46 2c 20 26 72 69 67 68 74 62 62 6f 78 2c 20 69 48  , &rightbbox, iH
35d47 65 69 67 68 74 2b 31 29 29 20 29 7b 0a 20 20 20  eight+1)) ){.   
35d48 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
35d49 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28  out;.  }..  for(
35d4a 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 52 69  i=0; i<NCELL(pRi
35d4b 67 68 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ght); i++){.    
35d4c 69 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64  i64 iRowid = nod
35d4d 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
35d4e 2c 20 70 52 69 67 68 74 2c 20 69 29 3b 0a 20 20  , pRight, i);.  
35d4f 20 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61 70    rc = updateMap
35d50 70 69 6e 67 28 70 52 74 72 65 65 2c 20 69 52 6f  ping(pRtree, iRo
35d51 77 69 64 2c 20 70 52 69 67 68 74 2c 20 69 48 65  wid, pRight, iHe
35d52 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 69  ight);.    if( i
35d53 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52  Rowid==pCell->iR
35d54 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 6e 65  owid ){.      ne
35d55 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 31  wCellIsRight = 1
35d56 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
35d57 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
35d58 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  .      goto spli
35d59 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  tnode_out;.    }
35d5a 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 6f 64 65  .  }.  if( pNode
35d5b 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20  ->iNode==1 ){.  
35d5c 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45    for(i=0; i<NCE
35d5d 4c 4c 28 70 4c 65 66 74 29 3b 20 69 2b 2b 29 7b  LL(pLeft); i++){
35d5e 0a 20 20 20 20 20 20 69 36 34 20 69 52 6f 77 69  .      i64 iRowi
35d5f 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64  d = nodeGetRowid
35d60 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20  (pRtree, pLeft, 
35d61 69 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 75  i);.      rc = u
35d62 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74  pdateMapping(pRt
35d63 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 4c 65  ree, iRowid, pLe
35d64 66 74 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  ft, iHeight);.  
35d65 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
35d66 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
35d67 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
35d68 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  out;.      }.   
35d69 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e   }.  }else if( n
35d6a 65 77 43 65 6c 6c 49 73 52 69 67 68 74 3d 3d 30  ewCellIsRight==0
35d6b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 75 70 64   ){.    rc = upd
35d6c 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65  ateMapping(pRtre
35d6d 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  e, pCell->iRowid
35d6e 2c 20 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74  , pLeft, iHeight
35d6f 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  );.  }..  if( rc
35d70 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
35d71 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65     rc = nodeRele
35d72 61 73 65 28 70 52 74 72 65 65 2c 20 70 52 69 67  ase(pRtree, pRig
35d73 68 74 29 3b 0a 20 20 20 20 70 52 69 67 68 74 20  ht);.    pRight 
35d74 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  = 0;.  }.  if( r
35d75 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
35d76 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c      rc = nodeRel
35d77 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
35d78 66 74 29 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d  ft);.    pLeft =
35d79 20 30 3b 0a 20 20 7d 0a 0a 73 70 6c 69 74 6e 6f   0;.  }..splitno
35d7a 64 65 5f 6f 75 74 3a 0a 20 20 6e 6f 64 65 52 65  de_out:.  nodeRe
35d7b 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52  lease(pRtree, pR
35d7c 69 67 68 74 29 3b 0a 20 20 6e 6f 64 65 52 65 6c  ight);.  nodeRel
35d7d 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
35d7e 66 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ft);.  sqlite3_f
35d7f 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20 72 65  ree(aCell);.  re
35d80 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
35d81 69 63 20 69 6e 74 20 66 69 78 4c 65 61 66 50 61  ic int fixLeafPa
35d82 72 65 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72  rent(Rtree *pRtr
35d83 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
35d84 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20 72 63 20  Leaf){.  int rc 
35d85 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
35d86 66 28 20 70 4c 65 61 66 2d 3e 69 4e 6f 64 65 21  f( pLeaf->iNode!
35d87 3d 31 20 26 26 20 70 4c 65 61 66 2d 3e 70 50 61  =1 && pLeaf->pPa
35d88 72 65 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73  rent==0 ){.    s
35d89 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
35d8a 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50  4(pRtree->pReadP
35d8b 61 72 65 6e 74 2c 20 31 2c 20 70 4c 65 61 66 2d  arent, 1, pLeaf-
35d8c 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 69 66 28  >iNode);.    if(
35d8d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
35d8e 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
35d8f 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  t)==SQLITE_ROW )
35d90 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4e 6f 64  {.      i64 iNod
35d91 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
35d92 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d  mn_int64(pRtree-
35d93 3e 70 52 65 61 64 50 61 72 65 6e 74 2c 20 30 29  >pReadParent, 0)
35d94 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64  ;.      rc = nod
35d95 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
35d96 20 69 4e 6f 64 65 2c 20 30 2c 20 26 70 4c 65 61   iNode, 0, &pLea
35d97 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20  f->pParent);.   
35d98 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
35d99 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
35d9a 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
35d9b 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d  e3_reset(pRtree-
35d9c 3e 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20  >pReadParent);. 
35d9d 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
35d9e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
35d9f 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74   = fixLeafParent
35da0 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2d 3e  (pRtree, pLeaf->
35da1 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a  pParent);.    }.
35da2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
35da3 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .}..static int d
35da4 65 6c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20  eleteCell(Rtree 
35da5 2a 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20  *, RtreeNode *, 
35da6 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 73 74 61 74  int, int);..stat
35da7 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 4e 6f 64  ic int removeNod
35da8 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
35da9 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
35daa 65 2c 20 69 6e 74 20 69 48 65 69 67 68 74 29 7b  e, int iHeight){
35dab 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72  .  int rc;.  Rtr
35dac 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b  eeNode *pParent;
35dad 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 0a 0a 20  .  int iCell;.. 
35dae 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e   assert( pNode->
35daf 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a  nRef==1 );..  /*
35db0 20 52 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72   Remove the entr
35db1 79 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  y in the parent 
35db2 63 65 6c 6c 2e 20 2a 2f 0a 20 20 69 43 65 6c 6c  cell. */.  iCell
35db3 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
35db4 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ex(pRtree, pNode
35db5 29 3b 0a 20 20 70 50 61 72 65 6e 74 20 3d 20 70  );.  pParent = p
35db6 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  Node->pParent;. 
35db7 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20   pNode->pParent 
35db8 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  = 0;.  if( SQLIT
35db9 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 64 65 6c 65  E_OK!=(rc = dele
35dba 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
35dbb 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20 69  Parent, iCell, i
35dbc 48 65 69 67 68 74 2b 31 29 29 20 0a 20 20 20 7c  Height+1)) .   |
35dbd 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  | SQLITE_OK!=(rc
35dbe 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
35dbf 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 29 29  Rtree, pParent))
35dc0 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
35dc1 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52   rc;.  }..  /* R
35dc2 65 6d 6f 76 65 20 74 68 65 20 78 78 78 5f 6e 6f  emove the xxx_no
35dc3 64 65 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 73  de entry. */.  s
35dc4 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
35dc5 34 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  4(pRtree->pDelet
35dc6 65 4e 6f 64 65 2c 20 31 2c 20 70 4e 6f 64 65 2d  eNode, 1, pNode-
35dc7 3e 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74  >iNode);.  sqlit
35dc8 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e  e3_step(pRtree->
35dc9 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20  pDeleteNode);.  
35dca 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
35dcb 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
35dcc 65 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  et(pRtree->pDele
35dcd 74 65 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20  teNode)) ){.    
35dce 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
35dcf 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
35dd0 78 78 78 5f 70 61 72 65 6e 74 20 65 6e 74 72 79  xxx_parent entry
35dd1 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62  . */.  sqlite3_b
35dd2 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
35dd3 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74 2c  ->pDeleteParent,
35dd4 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65   1, pNode->iNode
35dd5 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65  );.  sqlite3_ste
35dd6 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  p(pRtree->pDelet
35dd7 65 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20  eParent);.  if( 
35dd8 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
35dd9 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
35dda 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
35ddb 72 65 6e 74 29 29 20 29 7b 0a 20 20 20 20 72 65  rent)) ){.    re
35ddc 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 0a  turn rc;.  }.  .
35ddd 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
35dde 6e 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 69 6e  node from the in
35ddf 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62  -memory hash tab
35de0 6c 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69  le and link it i
35de1 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 52 74 72  nto.  ** the Rtr
35de2 65 65 2e 70 44 65 6c 65 74 65 64 20 6c 69 73 74  ee.pDeleted list
35de3 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 77  . Its contents w
35de4 69 6c 6c 20 62 65 20 72 65 2d 69 6e 73 65 72 74  ill be re-insert
35de5 65 64 20 6c 61 74 65 72 20 6f 6e 2e 0a 20 20 2a  ed later on..  *
35de6 2f 0a 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65  /.  nodeHashDele
35de7 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
35de8 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  );.  pNode->iNod
35de9 65 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 70  e = iHeight;.  p
35dea 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70 52  Node->pNext = pR
35deb 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b 0a  tree->pDeleted;.
35dec 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b    pNode->nRef++;
35ded 0a 20 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  .  pRtree->pDele
35dee 74 65 64 20 3d 20 70 4e 6f 64 65 3b 0a 0a 20 20  ted = pNode;..  
35def 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
35df0 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
35df1 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28   fixBoundingBox(
35df2 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
35df3 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29  treeNode *pNode)
35df4 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  {.  RtreeNode *p
35df5 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e  Parent = pNode->
35df6 70 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70  pParent;.  if( p
35df7 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e  Parent ){.    in
35df8 74 20 69 69 3b 20 0a 20 20 20 20 69 6e 74 20 6e  t ii; .    int n
35df9 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
35dfa 64 65 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65  de);.    RtreeCe
35dfb 6c 6c 20 62 6f 78 3b 20 20 20 20 20 20 20 20 20  ll box;         
35dfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dfd 20 20 20 2f 2a 20 42 6f 75 6e 64 69 6e 67 20 62     /* Bounding b
35dfe 6f 78 20 66 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a  ox for pNode */.
35dff 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
35e00 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 30  pRtree, pNode, 0
35e01 2c 20 26 62 6f 78 29 3b 0a 20 20 20 20 66 6f 72  , &box);.    for
35e02 28 69 69 3d 31 3b 20 69 69 3c 6e 43 65 6c 6c 3b  (ii=1; ii<nCell;
35e03 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 52 74   ii++){.      Rt
35e04 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20  reeCell cell;.  
35e05 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
35e06 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
35e07 69 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 20  i, &cell);.     
35e08 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
35e09 65 2c 20 26 62 6f 78 2c 20 26 63 65 6c 6c 29 3b  e, &box, &cell);
35e0a 0a 20 20 20 20 7d 0a 20 20 20 20 62 6f 78 2e 69  .    }.    box.i
35e0b 52 6f 77 69 64 20 3d 20 70 4e 6f 64 65 2d 3e 69  Rowid = pNode->i
35e0c 4e 6f 64 65 3b 0a 20 20 20 20 69 69 20 3d 20 6e  Node;.    ii = n
35e0d 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70  odeParentIndex(p
35e0e 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
35e0f 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65     nodeOverwrite
35e10 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
35e11 72 65 6e 74 2c 20 26 62 6f 78 2c 20 69 69 29 3b  rent, &box, ii);
35e12 0a 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e 67  .    fixBounding
35e13 42 6f 78 28 70 52 74 72 65 65 2c 20 70 50 61 72  Box(pRtree, pPar
35e14 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ent);.  }.}../*.
35e15 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 63 65  ** Delete the ce
35e16 6c 6c 20 61 74 20 69 6e 64 65 78 20 69 43 65 6c  ll at index iCel
35e17 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l of node pNode.
35e18 20 41 66 74 65 72 20 72 65 6d 6f 76 69 6e 67 20   After removing 
35e19 74 68 65 0a 2a 2a 20 63 65 6c 6c 2c 20 61 64 6a  the.** cell, adj
35e1a 75 73 74 20 74 68 65 20 72 2d 74 72 65 65 20 64  ust the r-tree d
35e1b 61 74 61 20 73 74 72 75 63 74 75 72 65 20 69 66  ata structure if
35e1c 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74   required..*/.st
35e1d 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65 43  atic int deleteC
35e1e 65 6c 6c 28 52 74 72 65 65 20 2a 70 52 74 72 65  ell(Rtree *pRtre
35e1f 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  e, RtreeNode *pN
35e20 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c 6c 2c 20  ode, int iCell, 
35e21 69 6e 74 20 69 48 65 69 67 68 74 29 7b 0a 20 20  int iHeight){.  
35e22 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28 20 53  int rc;..  if( S
35e23 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
35e24 66 69 78 4c 65 61 66 50 61 72 65 6e 74 28 70 52  fixLeafParent(pR
35e25 74 72 65 65 2c 20 70 4e 6f 64 65 29 29 20 29 7b  tree, pNode)) ){
35e26 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
35e27 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65    }..  /* Remove
35e28 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74   the cell from t
35e29 68 65 20 6e 6f 64 65 2e 20 54 68 69 73 20 63 61  he node. This ca
35e2a 6c 6c 20 6a 75 73 74 20 6d 6f 76 65 73 20 62 79  ll just moves by
35e2b 74 65 73 20 61 72 6f 75 6e 64 0a 20 20 2a 2a 20  tes around.  ** 
35e2c 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f  the in-memory no
35e2d 64 65 20 69 6d 61 67 65 2c 20 73 6f 20 69 74 20  de image, so it 
35e2e 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 2a  cannot fail..  *
35e2f 2f 0a 20 20 6e 6f 64 65 44 65 6c 65 74 65 43 65  /.  nodeDeleteCe
35e30 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
35e31 2c 20 69 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20  , iCell);..  /* 
35e32 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 6e  If the node is n
35e33 6f 74 20 74 68 65 20 74 72 65 65 20 72 6f 6f 74  ot the tree root
35e34 20 61 6e 64 20 6e 6f 77 20 68 61 73 20 6c 65 73   and now has les
35e35 73 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d  s than the minim
35e36 75 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f  um.  ** number o
35e37 66 20 63 65 6c 6c 73 2c 20 72 65 6d 6f 76 65 20  f cells, remove 
35e38 69 74 20 66 72 6f 6d 20 74 68 65 20 74 72 65 65  it from the tree
35e39 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 70 64  . Otherwise, upd
35e3a 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c  ate the.  ** cel
35e3b 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  l in the parent 
35e3c 6e 6f 64 65 20 73 6f 20 74 68 61 74 20 69 74 20  node so that it 
35e3d 74 69 67 68 74 6c 79 20 63 6f 6e 74 61 69 6e 73  tightly contains
35e3e 20 74 68 65 20 75 70 64 61 74 65 64 0a 20 20 2a   the updated.  *
35e3f 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69  * node..  */.  i
35e40 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21  f( pNode->iNode!
35e41 3d 31 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  =1 ){.    RtreeN
35e42 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
35e43 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  Node->pParent;. 
35e44 20 20 20 69 66 28 20 28 70 50 61 72 65 6e 74 2d     if( (pParent-
35e45 3e 69 4e 6f 64 65 21 3d 31 20 7c 7c 20 4e 43 45  >iNode!=1 || NCE
35e46 4c 4c 28 70 50 61 72 65 6e 74 29 21 3d 31 29 20  LL(pParent)!=1) 
35e47 0a 20 20 20 20 20 26 26 20 28 4e 43 45 4c 4c 28  .     && (NCELL(
35e48 70 4e 6f 64 65 29 3c 52 54 52 45 45 5f 4d 49 4e  pNode)<RTREE_MIN
35e49 43 45 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20  CELLS(pRtree)). 
35e4a 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d     ){.      rc =
35e4b 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72   removeNode(pRtr
35e4c 65 65 2c 20 70 4e 6f 64 65 2c 20 69 48 65 69 67  ee, pNode, iHeig
35e4d 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ht);.    }else{.
35e4e 20 20 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e        fixBoundin
35e4f 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  gBox(pRtree, pNo
35e50 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  de);.    }.  }..
35e51 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
35e52 73 74 61 74 69 63 20 69 6e 74 20 52 65 69 6e 73  static int Reins
35e53 65 72 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ert(.  Rtree *pR
35e54 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f  tree, .  RtreeNo
35e55 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74  de *pNode, .  Rt
35e56 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20  reeCell *pCell, 
35e57 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29  .  int iHeight.)
35e58 7b 0a 20 20 69 6e 74 20 2a 61 4f 72 64 65 72 3b  {.  int *aOrder;
35e59 0a 20 20 69 6e 74 20 2a 61 53 70 61 72 65 3b 0a  .  int *aSpare;.
35e5a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
35e5b 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69  ll;.  float *aDi
35e5c 73 74 61 6e 63 65 3b 0a 20 20 69 6e 74 20 6e 43  stance;.  int nC
35e5d 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 61 43 65  ell;.  float aCe
35e5e 6e 74 65 72 43 6f 6f 72 64 5b 52 54 52 45 45 5f  nterCoord[RTREE_
35e5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 5d 3b  MAX_DIMENSIONS];
35e60 0a 20 20 69 6e 74 20 69 44 69 6d 3b 0a 20 20 69  .  int iDim;.  i
35e61 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20  nt ii;.  int rc 
35e62 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
35e63 6d 65 6d 73 65 74 28 61 43 65 6e 74 65 72 43 6f  memset(aCenterCo
35e64 6f 72 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66  ord, 0, sizeof(f
35e65 6c 6f 61 74 29 2a 52 54 52 45 45 5f 4d 41 58 5f  loat)*RTREE_MAX_
35e66 44 49 4d 45 4e 53 49 4f 4e 53 29 3b 0a 0a 20 20  DIMENSIONS);..  
35e67 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e  nCell = NCELL(pN
35e68 6f 64 65 29 2b 31 3b 0a 0a 20 20 2f 2a 20 41 6c  ode)+1;..  /* Al
35e69 6c 6f 63 61 74 65 20 74 68 65 20 62 75 66 66 65  locate the buffe
35e6a 72 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  rs used by this 
35e6b 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 61  operation. The a
35e6c 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 0a 20 20 2a  llocation is.  *
35e6d 2a 20 72 65 6c 69 6e 71 75 69 73 68 65 64 20 62  * relinquished b
35e6e 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
35e6f 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a  ion returns..  *
35e70 2f 0a 20 20 61 43 65 6c 6c 20 3d 20 28 52 74 72  /.  aCell = (Rtr
35e71 65 65 43 65 6c 6c 20 2a 29 73 71 6c 69 74 65 33  eeCell *)sqlite3
35e72 5f 6d 61 6c 6c 6f 63 28 6e 43 65 6c 6c 20 2a 20  _malloc(nCell * 
35e73 28 0a 20 20 20 20 73 69 7a 65 6f 66 28 52 74 72  (.    sizeof(Rtr
35e74 65 65 43 65 6c 6c 29 20 2b 20 20 20 20 20 20 20  eeCell) +       
35e75 20 20 2f 2a 20 61 43 65 6c 6c 20 61 72 72 61 79    /* aCell array
35e76 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 69   */.    sizeof(i
35e77 6e 74 29 20 20 20 20 20 20 20 2b 20 20 20 20 20  nt)       +     
35e78 20 20 20 20 2f 2a 20 61 4f 72 64 65 72 20 61 72      /* aOrder ar
35e79 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ray */.    sizeo
35e7a 66 28 69 6e 74 29 20 20 20 20 20 20 20 2b 20 20  f(int)       +  
35e7b 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 72 65         /* aSpare
35e7c 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69   array */.    si
35e7d 7a 65 6f 66 28 66 6c 6f 61 74 29 20 20 20 20 20  zeof(float)     
35e7e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 44 69            /* aDi
35e7f 73 74 61 6e 63 65 20 61 72 72 61 79 20 2a 2f 0a  stance array */.
35e80 20 20 29 29 3b 0a 20 20 69 66 28 20 21 61 43 65    ));.  if( !aCe
35e81 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
35e82 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
35e83 20 7d 0a 20 20 61 4f 72 64 65 72 20 20 20 20 3d   }.  aOrder    =
35e84 20 28 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e   (int *)&aCell[n
35e85 43 65 6c 6c 5d 3b 0a 20 20 61 53 70 61 72 65 20  Cell];.  aSpare 
35e86 20 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 4f 72     = (int *)&aOr
35e87 64 65 72 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 44  der[nCell];.  aD
35e88 69 73 74 61 6e 63 65 20 3d 20 28 66 6c 6f 61 74  istance = (float
35e89 20 2a 29 26 61 53 70 61 72 65 5b 6e 43 65 6c 6c   *)&aSpare[nCell
35e8a 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ];..  for(ii=0; 
35e8b 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
35e8c 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 28 6e 43  .    if( ii==(nC
35e8d 65 6c 6c 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ell-1) ){.      
35e8e 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c 5b 69 69  memcpy(&aCell[ii
35e8f 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66  ], pCell, sizeof
35e90 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
35e91 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
35e92 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65  odeGetCell(pRtre
35e93 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 61  e, pNode, ii, &a
35e94 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 7d  Cell[ii]);.    }
35e95 0a 20 20 20 20 61 4f 72 64 65 72 5b 69 69 5d 20  .    aOrder[ii] 
35e96 3d 20 69 69 3b 0a 20 20 20 20 66 6f 72 28 69 44  = ii;.    for(iD
35e97 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65  im=0; iDim<pRtre
35e98 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29  e->nDim; iDim++)
35e99 7b 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72 43  {.      aCenterC
35e9a 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43  oord[iDim] += DC
35e9b 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61  OORD(aCell[ii].a
35e9c 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a  Coord[iDim*2]);.
35e9d 20 20 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f        aCenterCoo
35e9e 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f  rd[iDim] += DCOO
35e9f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
35ea0 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a  ord[iDim*2+1]);.
35ea1 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
35ea2 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74  iDim=0; iDim<pRt
35ea3 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b  ree->nDim; iDim+
35ea4 2b 29 7b 0a 20 20 20 20 61 43 65 6e 74 65 72 43  +){.    aCenterC
35ea5 6f 6f 72 64 5b 69 44 69 6d 5d 20 3d 20 61 43 65  oord[iDim] = aCe
35ea6 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 2f  nterCoord[iDim]/
35ea7 28 28 66 6c 6f 61 74 29 6e 43 65 6c 6c 2a 32 2e  ((float)nCell*2.
35ea8 30 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69  0);.  }..  for(i
35ea9 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
35eaa 69 2b 2b 29 7b 0a 20 20 20 20 61 44 69 73 74 61  i++){.    aDista
35eab 6e 63 65 5b 69 69 5d 20 3d 20 30 2e 30 3b 0a 20  nce[ii] = 0.0;. 
35eac 20 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69     for(iDim=0; i
35ead 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d  Dim<pRtree->nDim
35eae 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 20  ; iDim++){.     
35eaf 20 66 6c 6f 61 74 20 63 6f 6f 72 64 20 3d 20 44   float coord = D
35eb0 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
35eb1 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
35eb2 29 20 2d 20 0a 20 20 20 20 20 20 20 20 20 20 44  ) - .          D
35eb3 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
35eb4 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b  aCoord[iDim*2]);
35eb5 0a 20 20 20 20 20 20 61 44 69 73 74 61 6e 63 65  .      aDistance
35eb6 5b 69 69 5d 20 2b 3d 20 28 63 6f 6f 72 64 2d 61  [ii] += (coord-a
35eb7 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d  CenterCoord[iDim
35eb8 5d 29 2a 28 63 6f 6f 72 64 2d 61 43 65 6e 74 65  ])*(coord-aCente
35eb9 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29 3b 0a 20  rCoord[iDim]);. 
35eba 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 53 6f 72 74     }.  }..  Sort
35ebb 42 79 44 69 73 74 61 6e 63 65 28 61 4f 72 64 65  ByDistance(aOrde
35ebc 72 2c 20 6e 43 65 6c 6c 2c 20 61 44 69 73 74 61  r, nCell, aDista
35ebd 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a 20 20  nce, aSpare);.  
35ebe 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72 65 65 2c  nodeZero(pRtree,
35ebf 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28   pNode);..  for(
35ec0 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  ii=0; rc==SQLITE
35ec1 5f 4f 4b 20 26 26 20 69 69 3c 28 6e 43 65 6c 6c  _OK && ii<(nCell
35ec2 2d 28 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53  -(RTREE_MINCELLS
35ec3 28 70 52 74 72 65 65 29 2b 31 29 29 3b 20 69 69  (pRtree)+1)); ii
35ec4 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 65  ++){.    RtreeCe
35ec5 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61  ll *p = &aCell[a
35ec6 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20  Order[ii]];.    
35ec7 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
35ec8 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 29  Rtree, pNode, p)
35ec9 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 6f  ;.    if( p->iRo
35eca 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77  wid==pCell->iRow
35ecb 69 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  id ){.      if( 
35ecc 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20  iHeight==0 ){.  
35ecd 20 20 20 20 20 20 72 63 20 3d 20 72 6f 77 69 64        rc = rowid
35ece 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 2d  Write(pRtree, p-
35ecf 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e  >iRowid, pNode->
35ed0 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 65  iNode);.      }e
35ed1 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20  lse{.        rc 
35ed2 3d 20 70 61 72 65 6e 74 57 72 69 74 65 28 70 52  = parentWrite(pR
35ed3 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77 69 64 2c  tree, p->iRowid,
35ed4 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a   pNode->iNode);.
35ed5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
35ed6 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
35ed7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 66 69 78  TE_OK ){.    fix
35ed8 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52 74 72  BoundingBox(pRtr
35ed9 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 7d 0a  ee, pNode);.  }.
35eda 20 20 66 6f 72 28 3b 20 72 63 3d 3d 53 51 4c 49    for(; rc==SQLI
35edb 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65 6c  TE_OK && ii<nCel
35edc 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a  l; ii++){.    /*
35edd 20 46 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20   Find a node to 
35ede 73 74 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20  store this cell 
35edf 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  in. pNode->iNode
35ee0 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
35ee1 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68  ins.    ** the h
35ee2 65 69 67 68 74 20 6f 66 20 74 68 65 20 73 75 62  eight of the sub
35ee3 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20  -tree headed by 
35ee4 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f  the cell..    */
35ee5 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
35ee6 70 49 6e 73 65 72 74 3b 0a 20 20 20 20 52 74 72  pInsert;.    Rtr
35ee7 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65  eeCell *p = &aCe
35ee8 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a  ll[aOrder[ii]];.
35ee9 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c      rc = ChooseL
35eea 65 61 66 28 70 52 74 72 65 65 2c 20 70 2c 20 69  eaf(pRtree, p, i
35eeb 48 65 69 67 68 74 2c 20 26 70 49 6e 73 65 72 74  Height, &pInsert
35eec 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
35eed 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
35eee 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
35eef 20 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72   rc = rtreeInser
35ef0 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 49  tCell(pRtree, pI
35ef1 6e 73 65 72 74 2c 20 70 2c 20 69 48 65 69 67 68  nsert, p, iHeigh
35ef2 74 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20  t);.      rc2 = 
35ef3 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
35ef4 65 65 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20  ee, pInsert);.  
35ef5 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
35ef6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
35ef7 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20   rc = rc2;.     
35ef8 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
35ef9 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65  sqlite3_free(aCe
35efa 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ll);.  return rc
35efb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72  ;.}../*.** Inser
35efc 74 20 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e 74  t cell pCell int
35efd 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 4e 6f  o node pNode. No
35efe 64 65 20 70 4e 6f 64 65 20 69 73 20 74 68 65 20  de pNode is the 
35eff 68 65 61 64 20 6f 66 20 61 20 0a 2a 2a 20 73 75  head of a .** su
35f00 62 74 72 65 65 20 69 48 65 69 67 68 74 20 68 69  btree iHeight hi
35f01 67 68 20 28 6c 65 61 66 20 6e 6f 64 65 73 20 68  gh (leaf nodes h
35f02 61 76 65 20 69 48 65 69 67 68 74 3d 3d 30 29 2e  ave iHeight==0).
35f03 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
35f04 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 0a  treeInsertCell(.
35f05 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
35f06 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
35f07 6f 64 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ode,.  RtreeCell
35f08 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69   *pCell,.  int i
35f09 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  Height.){.  int 
35f0a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
35f0b 20 20 69 66 28 20 69 48 65 69 67 68 74 3e 30 20    if( iHeight>0 
35f0c 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  ){.    RtreeNode
35f0d 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48   *pChild = nodeH
35f0e 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65  ashLookup(pRtree
35f0f 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 29  , pCell->iRowid)
35f10 3b 0a 20 20 20 20 69 66 28 20 70 43 68 69 6c 64  ;.    if( pChild
35f11 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65   ){.      nodeRe
35f12 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43  lease(pRtree, pC
35f13 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  hild->pParent);.
35f14 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65        nodeRefere
35f15 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  nce(pNode);.    
35f16 20 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e    pChild->pParen
35f17 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 7d  t = pNode;.    }
35f18 0a 20 20 7d 0a 20 20 69 66 28 20 6e 6f 64 65 49  .  }.  if( nodeI
35f19 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
35f1a 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 20  , pNode, pCell) 
35f1b 29 7b 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  ){.#if VARIANT_R
35f1c 53 54 41 52 54 52 45 45 5f 52 45 49 4e 53 45 52  STARTREE_REINSER
35f1d 54 0a 20 20 20 20 69 66 28 20 69 48 65 69 67 68  T.    if( iHeigh
35f1e 74 3c 3d 70 52 74 72 65 65 2d 3e 69 52 65 69 6e  t<=pRtree->iRein
35f1f 73 65 72 74 48 65 69 67 68 74 20 7c 7c 20 70 4e  sertHeight || pN
35f20 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 29 7b 0a  ode->iNode==1){.
35f21 20 20 20 20 20 20 72 63 20 3d 20 53 70 6c 69 74        rc = Split
35f22 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Node(pRtree, pNo
35f23 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69 67  de, pCell, iHeig
35f24 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ht);.    }else{.
35f25 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52        pRtree->iR
35f26 65 69 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20  einsertHeight = 
35f27 69 48 65 69 67 68 74 3b 0a 20 20 20 20 20 20 72  iHeight;.      r
35f28 63 20 3d 20 52 65 69 6e 73 65 72 74 28 70 52 74  c = Reinsert(pRt
35f29 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
35f2a 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20  l, iHeight);.   
35f2b 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20   }.#else.    rc 
35f2c 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70 52 74 72  = SplitNode(pRtr
35f2d 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c  ee, pNode, pCell
35f2e 2c 20 69 48 65 69 67 68 74 29 3b 0a 23 65 6e 64  , iHeight);.#end
35f2f 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  if.  }else{.    
35f30 41 64 6a 75 73 74 54 72 65 65 28 70 52 74 72 65  AdjustTree(pRtre
35f31 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29  e, pNode, pCell)
35f32 3b 0a 20 20 20 20 69 66 28 20 69 48 65 69 67 68  ;.    if( iHeigh
35f33 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  t==0 ){.      rc
35f34 20 3d 20 72 6f 77 69 64 57 72 69 74 65 28 70 52   = rowidWrite(pR
35f35 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f  tree, pCell->iRo
35f36 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  wid, pNode->iNod
35f37 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
35f38 20 20 20 20 20 72 63 20 3d 20 70 61 72 65 6e 74       rc = parent
35f39 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 43  Write(pRtree, pC
35f3a 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f  ell->iRowid, pNo
35f3b 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
35f3c 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
35f3d 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  c;.}..static int
35f3e 20 72 65 69 6e 73 65 72 74 4e 6f 64 65 43 6f 6e   reinsertNodeCon
35f3f 74 65 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72  tent(Rtree *pRtr
35f40 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
35f41 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b  Node){.  int ii;
35f42 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
35f43 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65  TE_OK;.  int nCe
35f44 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
35f45 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
35f46 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
35f47 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29   ii<nCell; ii++)
35f48 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  {.    RtreeNode 
35f49 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20 20 52 74  *pInsert;.    Rt
35f4a 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20  reeCell cell;.  
35f4b 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
35f4c 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
35f4d 20 26 63 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a   &cell);..    /*
35f4e 20 46 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20   Find a node to 
35f4f 73 74 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20  store this cell 
35f50 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  in. pNode->iNode
35f51 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
35f52 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68  ins.    ** the h
35f53 65 69 67 68 74 20 6f 66 20 74 68 65 20 73 75 62  eight of the sub
35f54 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20  -tree headed by 
35f55 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f  the cell..    */
35f56 0a 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65  .    rc = Choose
35f57 4c 65 61 66 28 70 52 74 72 65 65 2c 20 26 63 65  Leaf(pRtree, &ce
35f58 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  ll, pNode->iNode
35f59 2c 20 26 70 49 6e 73 65 72 74 29 3b 0a 20 20 20  , &pInsert);.   
35f5a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
35f5b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
35f5c 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rc2;.      rc = 
35f5d 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28  rtreeInsertCell(
35f5e 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c  pRtree, pInsert,
35f5f 20 26 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69   &cell, pNode->i
35f60 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 72 63 32  Node);.      rc2
35f61 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
35f62 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 29 3b  Rtree, pInsert);
35f63 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
35f64 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
35f65 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
35f66 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
35f67 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
35f68 2f 2a 0a 2a 2a 20 53 65 6c 65 63 74 20 61 20 63  /*.** Select a c
35f69 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 20  urrently unused 
35f6a 72 6f 77 69 64 20 66 6f 72 20 61 20 6e 65 77 20  rowid for a new 
35f6b 72 2d 74 72 65 65 20 72 65 63 6f 72 64 2e 0a 2a  r-tree record..*
35f6c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 77  /.static int new
35f6d 52 6f 77 69 64 28 52 74 72 65 65 20 2a 70 52 74  Rowid(Rtree *pRt
35f6e 72 65 65 2c 20 69 36 34 20 2a 70 69 52 6f 77 69  ree, i64 *piRowi
35f6f 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  d){.  int rc;.  
35f70 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
35f71 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  l(pRtree->pWrite
35f72 52 6f 77 69 64 2c 20 31 29 3b 0a 20 20 73 71 6c  Rowid, 1);.  sql
35f73 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70  ite3_bind_null(p
35f74 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
35f75 69 64 2c 20 32 29 3b 0a 20 20 73 71 6c 69 74 65  id, 2);.  sqlite
35f76 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70  3_step(pRtree->p
35f77 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 72  WriteRowid);.  r
35f78 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
35f79 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  t(pRtree->pWrite
35f7a 52 6f 77 69 64 29 3b 0a 20 20 2a 70 69 52 6f 77  Rowid);.  *piRow
35f7b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73  id = sqlite3_las
35f7c 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70  t_insert_rowid(p
35f7d 52 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 72 65  Rtree->db);.  re
35f7e 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
35f7f 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69  def NDEBUG.stati
35f80 63 20 69 6e 74 20 68 61 73 68 49 73 45 6d 70 74  c int hashIsEmpt
35f81 79 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 29  y(Rtree *pRtree)
35f82 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f  {.  int ii;.  fo
35f83 72 28 69 69 3d 30 3b 20 69 69 3c 48 41 53 48 53  r(ii=0; ii<HASHS
35f84 49 5a 45 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  IZE; ii++){.    
35f85 61 73 73 65 72 74 28 20 21 70 52 74 72 65 65 2d  assert( !pRtree-
35f86 3e 61 48 61 73 68 5b 69 69 5d 20 29 3b 0a 20 20  >aHash[ii] );.  
35f87 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
35f88 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
35f89 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64  e xUpdate method
35f8a 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75 6c   for rtree modul
35f8b 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  e virtual tables
35f8c 2e 0a 2a 2f 0a 69 6e 74 20 72 74 72 65 65 55 70  ..*/.int rtreeUp
35f8d 64 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  date(.  sqlite3_
35f8e 76 74 61 62 20 2a 70 56 74 61 62 2c 20 0a 20 20  vtab *pVtab, .  
35f8f 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 73 71  int nData, .  sq
35f90 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 7a  lite3_value **az
35f91 44 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 5f  Data, .  sqlite_
35f92 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 0a 29 7b  int64 *pRowid.){
35f93 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
35f94 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74 61   = (Rtree *)pVta
35f95 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  b;.  int rc = SQ
35f96 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65  LITE_OK;..  rtre
35f97 65 52 65 66 65 72 65 6e 63 65 28 70 52 74 72 65  eReference(pRtre
35f98 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 44  e);..  assert(nD
35f99 61 74 61 3e 3d 31 29 3b 0a 20 20 61 73 73 65 72  ata>=1);.  asser
35f9a 74 28 68 61 73 68 49 73 45 6d 70 74 79 28 70 52  t(hashIsEmpty(pR
35f9b 74 72 65 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  tree));..  /* If
35f9c 20 61 7a 44 61 74 61 5b 30 5d 20 69 73 20 6e 6f   azData[0] is no
35f9d 74 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61  t an SQL NULL va
35f9e 6c 75 65 2c 20 69 74 20 69 73 20 74 68 65 20 72  lue, it is the r
35f9f 6f 77 69 64 20 6f 66 20 61 0a 20 20 2a 2a 20 72  owid of a.  ** r
35fa0 65 63 6f 72 64 20 74 6f 20 64 65 6c 65 74 65 20  ecord to delete 
35fa1 66 72 6f 6d 20 74 68 65 20 72 2d 74 72 65 65 20  from the r-tree 
35fa2 74 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f  table. The follo
35fa3 77 69 6e 67 20 62 6c 6f 63 6b 20 64 6f 65 73 0a  wing block does.
35fa4 20 20 2a 2a 20 6a 75 73 74 20 74 68 61 74 2e 0a    ** just that..
35fa5 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
35fa6 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a  e3_value_type(az
35fa7 44 61 74 61 5b 30 5d 29 21 3d 53 51 4c 49 54 45  Data[0])!=SQLITE
35fa8 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 36 34  _NULL ){.    i64
35fa9 20 69 44 65 6c 65 74 65 3b 20 20 20 20 20 20 20   iDelete;       
35faa 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
35fab 72 6f 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20  rowid to delete 
35fac 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  */.    RtreeNode
35fad 20 2a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20   *pLeaf;        
35fae 20 20 20 2f 2a 20 4c 65 61 66 20 6e 6f 64 65 20     /* Leaf node 
35faf 63 6f 6e 74 61 69 6e 69 6e 67 20 72 65 63 6f 72  containing recor
35fb0 64 20 69 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20  d iDelete */.   
35fb1 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20 20   int iCell;     
35fb2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35fb3 49 6e 64 65 78 20 6f 66 20 69 44 65 6c 65 74 65  Index of iDelete
35fb4 20 63 65 6c 6c 20 69 6e 20 70 4c 65 61 66 20 2a   cell in pLeaf *
35fb5 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  /.    RtreeNode 
35fb6 2a 70 52 6f 6f 74 3b 0a 0a 20 20 20 20 2f 2a 20  *pRoot;..    /* 
35fb7 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  Obtain a referen
35fb8 63 65 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e  ce to the root n
35fb9 6f 64 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 73  ode to initialis
35fba 65 20 52 74 72 65 65 2e 69 44 65 70 74 68 20 2a  e Rtree.iDepth *
35fbb 2f 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41  /.    rc = nodeA
35fbc 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 31  cquire(pRtree, 1
35fbd 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b 0a 0a 20  , 0, &pRoot);.. 
35fbe 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72     /* Obtain a r
35fbf 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
35fc0 6c 65 61 66 20 6e 6f 64 65 20 74 68 61 74 20 63  leaf node that c
35fc1 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72  ontains the entr
35fc2 79 20 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20  y .    ** about 
35fc3 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 0a  to be deleted. .
35fc4 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
35fc5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
35fc6 20 20 20 20 20 20 69 44 65 6c 65 74 65 20 3d 20        iDelete = 
35fc7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
35fc8 74 36 34 28 61 7a 44 61 74 61 5b 30 5d 29 3b 0a  t64(azData[0]);.
35fc9 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c        rc = findL
35fca 65 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  eafNode(pRtree, 
35fcb 69 44 65 6c 65 74 65 2c 20 26 70 4c 65 61 66 29  iDelete, &pLeaf)
35fcc 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
35fcd 44 65 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20  Delete the cell 
35fce 69 6e 20 71 75 65 73 74 69 6f 6e 20 66 72 6f 6d  in question from
35fcf 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20   the leaf node. 
35fd0 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
35fd1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
35fd2 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
35fd3 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77   iCell = nodeRow
35fd4 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20  idIndex(pRtree, 
35fd5 70 4c 65 61 66 2c 20 69 44 65 6c 65 74 65 29 3b  pLeaf, iDelete);
35fd6 0a 20 20 20 20 20 20 72 63 20 3d 20 64 65 6c 65  .      rc = dele
35fd7 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
35fd8 4c 65 61 66 2c 20 69 43 65 6c 6c 2c 20 30 29 3b  Leaf, iCell, 0);
35fd9 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64  .      rc2 = nod
35fda 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
35fdb 20 70 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 69   pLeaf);.      i
35fdc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
35fdd 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
35fde 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20   rc2;.      }.  
35fdf 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65    }..    /* Dele
35fe0 74 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  te the correspon
35fe1 64 69 6e 67 20 65 6e 74 72 79 20 69 6e 20 74 68  ding entry in th
35fe2 65 20 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20  e <rtree>_rowid 
35fe3 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66  table. */.    if
35fe4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
35fe5 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
35fe6 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
35fe7 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
35fe8 2c 20 31 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20  , 1, iDelete);. 
35fe9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65       sqlite3_ste
35fea 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  p(pRtree->pDelet
35feb 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 72  eRowid);.      r
35fec 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
35fed 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  t(pRtree->pDelet
35fee 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a  eRowid);.    }..
35fef 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
35ff0 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6e 6f  the root node no
35ff1 77 20 68 61 73 20 65 78 61 63 74 6c 79 20 6f 6e  w has exactly on
35ff2 65 20 63 68 69 6c 64 2e 20 49 66 20 73 6f 2c 20  e child. If so, 
35ff3 72 65 6d 6f 76 65 0a 20 20 20 20 2a 2a 20 69 74  remove.    ** it
35ff4 2c 20 73 63 68 65 64 75 6c 65 20 74 68 65 20 63  , schedule the c
35ff5 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
35ff6 68 69 6c 64 20 66 6f 72 20 72 65 69 6e 73 65 72  hild for reinser
35ff7 74 69 6f 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a  tion and .    **
35ff8 20 72 65 64 75 63 65 20 74 68 65 20 74 72 65 65   reduce the tree
35ff9 20 68 65 69 67 68 74 20 62 79 20 6f 6e 65 2e 0a   height by one..
35ffa 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
35ffb 69 73 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  is is equivalent
35ffc 20 74 6f 20 63 6f 70 79 69 6e 67 20 74 68 65 20   to copying the 
35ffd 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
35ffe 63 68 69 6c 64 20 69 6e 74 6f 0a 20 20 20 20 2a  child into.    *
35fff 2a 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20  * the root node 
36000 28 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74  (the operation t
36001 68 61 74 20 47 75 74 6d 61 6e 27 73 20 70 61 70  hat Gutman's pap
36002 65 72 20 73 61 79 73 20 74 6f 20 70 65 72 66 6f  er says to perfo
36003 72 6d 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68  rm .    ** in th
36004 69 73 20 73 63 65 6e 61 72 69 6f 29 2e 0a 20 20  is scenario)..  
36005 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
36006 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 52  =SQLITE_OK && pR
36007 74 72 65 65 2d 3e 69 44 65 70 74 68 3e 30 20 29  tree->iDepth>0 )
36008 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  {.      if( rc==
36009 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 4e 43 45  SQLITE_OK && NCE
3600a 4c 4c 28 70 52 6f 6f 74 29 3d 3d 31 20 29 7b 0a  LL(pRoot)==1 ){.
3600b 20 20 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64          RtreeNod
3600c 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 20 20 20  e *pChild;.     
3600d 20 20 20 69 36 34 20 69 43 68 69 6c 64 20 3d 20     i64 iChild = 
3600e 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
3600f 72 65 65 2c 20 70 52 6f 6f 74 2c 20 30 29 3b 0a  ree, pRoot, 0);.
36010 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64          rc = nod
36011 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
36012 20 69 43 68 69 6c 64 2c 20 70 52 6f 6f 74 2c 20   iChild, pRoot, 
36013 26 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20  &pChild);.      
36014 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
36015 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
36016 20 72 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65   rc = removeNode
36017 28 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2c  (pRtree, pChild,
36018 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d   pRtree->iDepth-
36019 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
3601a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
3601b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
3601c 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65       pRtree->iDe
3601d 70 74 68 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20  pth--;.         
3601e 20 77 72 69 74 65 49 6e 74 31 36 28 70 52 6f 6f   writeInt16(pRoo
3601f 74 2d 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65  t->zData, pRtree
36020 2d 3e 69 44 65 70 74 68 29 3b 0a 20 20 20 20 20  ->iDepth);.     
36021 20 20 20 20 20 70 52 6f 6f 74 2d 3e 69 73 44 69       pRoot->isDi
36022 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  rty = 1;.       
36023 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
36024 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 69 6e 73 65  ..    /* Re-inse
36025 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rt the contents 
36026 6f 66 20 61 6e 79 20 75 6e 64 65 72 66 75 6c 6c  of any underfull
36027 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 66   nodes removed f
36028 72 6f 6d 20 74 68 65 20 74 72 65 65 2e 20 2a 2f  rom the tree. */
36029 0a 20 20 20 20 66 6f 72 28 70 4c 65 61 66 3d 70  .    for(pLeaf=p
3602a 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b  Rtree->pDeleted;
3602b 20 70 4c 65 61 66 3b 20 70 4c 65 61 66 3d 70 52   pLeaf; pLeaf=pR
3602c 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 29 7b  tree->pDeleted){
3602d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
3602e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3602f 20 20 20 20 72 63 20 3d 20 72 65 69 6e 73 65 72      rc = reinser
36030 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 74  tNodeContent(pRt
36031 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20  ree, pLeaf);.   
36032 20 20 20 7d 0a 20 20 20 20 20 20 70 52 74 72 65     }.      pRtre
36033 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d 20 70 4c  e->pDeleted = pL
36034 65 61 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  eaf->pNext;.    
36035 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
36036 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Leaf);.    }..  
36037 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65    /* Release the
36038 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
36039 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20 2a 2f 0a  e root node. */.
3603a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3603b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
3603c 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
3603d 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a  pRtree, pRoot);.
3603e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3603f 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
36040 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20  ree, pRoot);.   
36041 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
36042 74 68 65 20 61 7a 44 61 74 61 5b 5d 20 61 72 72  the azData[] arr
36043 61 79 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65  ay contains more
36044 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e   than one elemen
36045 74 2c 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a  t, elements.  **
36046 20 28 61 7a 44 61 74 61 5b 32 5d 2e 2e 61 7a 44   (azData[2]..azD
36047 61 74 61 5b 61 72 67 63 2d 31 5d 29 20 63 6f 6e  ata[argc-1]) con
36048 74 61 69 6e 20 61 20 6e 65 77 20 72 65 63 6f 72  tain a new recor
36049 64 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f  d to insert into
3604a 0a 20 20 2a 2a 20 74 68 65 20 72 2d 74 72 65 65  .  ** the r-tree
3604b 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f   structure..  */
3604c 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3604d 45 5f 4f 4b 20 26 26 20 6e 44 61 74 61 3e 31 20  E_OK && nData>1 
3604e 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  ){.    /* Insert
3604f 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e   a new record in
36050 74 6f 20 74 68 65 20 72 2d 74 72 65 65 20 2a 2f  to the r-tree */
36051 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63  .    RtreeCell c
36052 65 6c 6c 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b  ell;.    int ii;
36053 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
36054 70 4c 65 61 66 3b 0a 0a 20 20 20 20 2f 2a 20 50  pLeaf;..    /* P
36055 6f 70 75 6c 61 74 65 20 74 68 65 20 63 65 6c 6c  opulate the cell
36056 2e 61 43 6f 6f 72 64 5b 5d 20 61 72 72 61 79 2e  .aCoord[] array.
36057 20 54 68 65 20 66 69 72 73 74 20 63 6f 6f 72 64   The first coord
36058 69 6e 61 74 65 20 69 73 20 61 7a 44 61 74 61 5b  inate is azData[
36059 33 5d 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  3]. */.    asser
3605a 74 28 20 6e 44 61 74 61 3d 3d 28 70 52 74 72 65  t( nData==(pRtre
3605b 65 2d 3e 6e 44 69 6d 2a 32 20 2b 20 33 29 20 29  e->nDim*2 + 3) )
3605c 3b 0a 20 20 20 20 69 66 28 20 70 52 74 72 65 65  ;.    if( pRtree
3605d 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
3605e 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
3605f 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 69   ){.      for(ii
36060 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e  =0; ii<(pRtree->
36061 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b  nDim*2); ii+=2){
36062 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43  .        cell.aC
36063 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 28 66 6c  oord[ii].f = (fl
36064 6f 61 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  oat)sqlite3_valu
36065 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b  e_double(azData[
36066 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20  ii+3]);.        
36067 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31  cell.aCoord[ii+1
36068 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73 71 6c  ].f = (float)sql
36069 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
3606a 65 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b  e(azData[ii+4]);
3606b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 65 6c  .        if( cel
3606c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 3e 63  l.aCoord[ii].f>c
3606d 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  ell.aCoord[ii+1]
3606e 2e 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  .f ){.          
3606f 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53  rc = SQLITE_CONS
36070 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20  TRAINT;.        
36071 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e    goto constrain
36072 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
36073 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
36074 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20        for(ii=0; 
36075 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
36076 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
36077 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64       cell.aCoord
36078 5b 69 69 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33  [ii].i = sqlite3
36079 5f 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74  _value_int(azDat
3607a 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20  a[ii+3]);.      
3607b 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69    cell.aCoord[ii
3607c 2b 31 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f  +1].i = sqlite3_
3607d 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61  value_int(azData
3607e 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20  [ii+4]);.       
3607f 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64   if( cell.aCoord
36080 5b 69 69 5d 2e 69 3e 63 65 6c 6c 2e 61 43 6f 6f  [ii].i>cell.aCoo
36081 72 64 5b 69 69 2b 31 5d 2e 69 20 29 7b 0a 20 20  rd[ii+1].i ){.  
36082 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
36083 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
36084 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63            goto c
36085 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20  onstraint;.     
36086 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
36087 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72   }..    /* Figur
36088 65 20 6f 75 74 20 74 68 65 20 72 6f 77 69 64 20  e out the rowid 
36089 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 20  of the new row. 
3608a 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
3608b 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a  e3_value_type(az
3608c 44 61 74 61 5b 32 5d 29 3d 3d 53 51 4c 49 54 45  Data[2])==SQLITE
3608d 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72  _NULL ){.      r
3608e 63 20 3d 20 6e 65 77 52 6f 77 69 64 28 70 52 74  c = newRowid(pRt
3608f 72 65 65 2c 20 26 63 65 6c 6c 2e 69 52 6f 77 69  ree, &cell.iRowi
36090 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
36091 20 20 20 20 20 63 65 6c 6c 2e 69 52 6f 77 69 64       cell.iRowid
36092 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
36093 5f 69 6e 74 36 34 28 61 7a 44 61 74 61 5b 32 5d  _int64(azData[2]
36094 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
36095 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
36096 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20  ee->pReadRowid, 
36097 31 2c 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b  1, cell.iRowid);
36098 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
36099 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
3609a 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61  tep(pRtree->pRea
3609b 64 52 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 20  dRowid) ){.     
3609c 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
3609d 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
3609e 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 72 63  wid);.        rc
3609f 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52   = SQLITE_CONSTR
360a0 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 67 6f  AINT;.        go
360a1 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20  to constraint;. 
360a2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
360a3 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
360a4 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
360a5 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  id);.    }..    
360a6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
360a7 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
360a8 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65  ChooseLeaf(pRtre
360a9 65 2c 20 26 63 65 6c 6c 2c 20 30 2c 20 26 70 4c  e, &cell, 0, &pL
360aa 65 61 66 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  eaf);.    }.    
360ab 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
360ac 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  K ){.      int r
360ad 63 32 3b 0a 20 20 20 20 20 20 70 52 74 72 65 65  c2;.      pRtree
360ae 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67 68  ->iReinsertHeigh
360af 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 63  t = -1;.      rc
360b0 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65   = rtreeInsertCe
360b1 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  ll(pRtree, pLeaf
360b2 2c 20 26 63 65 6c 6c 2c 20 30 29 3b 0a 20 20 20  , &cell, 0);.   
360b3 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c     rc2 = nodeRel
360b4 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
360b5 61 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  af);.      if( r
360b6 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
360b7 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32          rc = rc2
360b8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
360b9 20 20 7d 0a 0a 63 6f 6e 73 74 72 61 69 6e 74 3a    }..constraint:
360ba 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  .  rtreeRelease(
360bb 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74 75 72  pRtree);.  retur
360bc 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
360bd 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f  he xRename metho
360be 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75  d for rtree modu
360bf 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  le virtual table
360c0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
360c1 20 72 74 72 65 65 52 65 6e 61 6d 65 28 73 71 6c   rtreeRename(sql
360c2 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
360c3 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
360c4 65 77 4e 61 6d 65 29 7b 0a 20 20 52 74 72 65 65  ewName){.  Rtree
360c5 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65   *pRtree = (Rtre
360c6 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74  e *)pVtab;.  int
360c7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
360c8 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  EM;.  char *zSql
360c9 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
360ca 74 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54  tf(.    "ALTER T
360cb 41 42 4c 45 20 25 51 2e 27 25 71 5f 6e 6f 64 65  ABLE %Q.'%q_node
360cc 27 20 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22  '   RENAME TO \"
360cd 25 77 5f 6e 6f 64 65 5c 22 3b 22 0a 20 20 20 20  %w_node\";".    
360ce 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e  "ALTER TABLE %Q.
360cf 27 25 71 5f 70 61 72 65 6e 74 27 20 52 45 4e 41  '%q_parent' RENA
360d0 4d 45 20 54 4f 20 5c 22 25 77 5f 70 61 72 65 6e  ME TO \"%w_paren
360d1 74 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52  t\";".    "ALTER
360d2 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 72 6f   TABLE %Q.'%q_ro
360d3 77 69 64 27 20 20 52 45 4e 41 4d 45 20 54 4f 20  wid'  RENAME TO 
360d4 5c 22 25 77 5f 72 6f 77 69 64 5c 22 3b 22 0a 20  \"%w_rowid\";". 
360d5 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62     , pRtree->zDb
360d6 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c  , pRtree->zName,
360d7 20 7a 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20 2c   zNewName .    ,
360d8 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
360d9 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65  tree->zName, zNe
360da 77 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74  wName .    , pRt
360db 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65  ree->zDb, pRtree
360dc 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d  ->zName, zNewNam
360dd 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71  e.  );.  if( zSq
360de 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
360df 6c 69 74 65 33 5f 65 78 65 63 28 70 52 74 72 65  lite3_exec(pRtre
360e0 65 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20  e->db, zSql, 0, 
360e1 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
360e2 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
360e3 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
360e4 7d 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  }..static sqlite
360e5 33 5f 6d 6f 64 75 6c 65 20 72 74 72 65 65 4d 6f  3_module rtreeMo
360e6 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20  dule = {.  0,   
360e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360e8 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
360e9 6e 20 2a 2f 0a 20 20 72 74 72 65 65 43 72 65 61  n */.  rtreeCrea
360ea 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
360eb 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2d 20     /* xCreate - 
360ec 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 2a  create a table *
360ed 2f 0a 20 20 72 74 72 65 65 43 6f 6e 6e 65 63 74  /.  rtreeConnect
360ee 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
360ef 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 2d 20 63 6f  /* xConnect - co
360f0 6e 6e 65 63 74 20 74 6f 20 61 6e 20 65 78 69 73  nnect to an exis
360f1 74 69 6e 67 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ting table */.  
360f2 72 74 72 65 65 42 65 73 74 49 6e 64 65 78 2c 20  rtreeBestIndex, 
360f3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
360f4 42 65 73 74 49 6e 64 65 78 20 2d 20 44 65 74 65  BestIndex - Dete
360f5 72 6d 69 6e 65 20 73 65 61 72 63 68 20 73 74 72  rmine search str
360f6 61 74 65 67 79 20 2a 2f 0a 20 20 72 74 72 65 65  ategy */.  rtree
360f7 44 69 73 63 6f 6e 6e 65 63 74 2c 20 20 20 20 20  Disconnect,     
360f8 20 20 20 20 20 20 20 2f 2a 20 78 44 69 73 63 6f         /* xDisco
360f9 6e 6e 65 63 74 20 2d 20 44 69 73 63 6f 6e 6e 65  nnect - Disconne
360fa 63 74 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  ct from a table 
360fb 2a 2f 0a 20 20 72 74 72 65 65 44 65 73 74 72 6f  */.  rtreeDestro
360fc 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
360fd 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2d 20 44   /* xDestroy - D
360fe 72 6f 70 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20  rop a table */. 
360ff 20 72 74 72 65 65 4f 70 65 6e 2c 20 20 20 20 20   rtreeOpen,     
36100 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36101 78 4f 70 65 6e 20 2d 20 6f 70 65 6e 20 61 20 63  xOpen - open a c
36102 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65  ursor */.  rtree
36103 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
36104 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
36105 20 2d 20 63 6c 6f 73 65 20 61 20 63 75 72 73 6f   - close a curso
36106 72 20 2a 2f 0a 20 20 72 74 72 65 65 46 69 6c 74  r */.  rtreeFilt
36107 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
36108 20 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 2d 20     /* xFilter - 
36109 63 6f 6e 66 69 67 75 72 65 20 73 63 61 6e 20 63  configure scan c
3610a 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
3610b 72 74 72 65 65 4e 65 78 74 2c 20 20 20 20 20 20  rtreeNext,      
3610c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3610d 4e 65 78 74 20 2d 20 61 64 76 61 6e 63 65 20 61  Next - advance a
3610e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72   cursor */.  rtr
3610f 65 65 45 6f 66 2c 20 20 20 20 20 20 20 20 20 20  eeEof,          
36110 20 20 20 20 20 20 20 20 20 2f 2a 20 78 45 6f 66           /* xEof
36111 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f 6c 75 6d   */.  rtreeColum
36112 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
36113 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 2d 20 72    /* xColumn - r
36114 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20 72 74  ead data */.  rt
36115 72 65 65 52 6f 77 69 64 2c 20 20 20 20 20 20 20  reeRowid,       
36116 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 6f            /* xRo
36117 77 69 64 20 2d 20 72 65 61 64 20 64 61 74 61 20  wid - read data 
36118 2a 2f 0a 20 20 72 74 72 65 65 55 70 64 61 74 65  */.  rtreeUpdate
36119 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3611a 20 2f 2a 20 78 55 70 64 61 74 65 20 2d 20 77 72   /* xUpdate - wr
3611b 69 74 65 20 64 61 74 61 20 2a 2f 0a 20 20 30 2c  ite data */.  0,
3611c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3611d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 42 65            /* xBe
3611e 67 69 6e 20 2d 20 62 65 67 69 6e 20 74 72 61 6e  gin - begin tran
3611f 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20  saction */.  0, 
36120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36121 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e           /* xSyn
36122 63 20 2d 20 73 79 6e 63 20 74 72 61 6e 73 61 63  c - sync transac
36123 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20  tion */.  0,    
36124 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36125 20 20 20 20 20 20 2f 2a 20 78 43 6f 6d 6d 69 74        /* xCommit
36126 20 2d 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61   - commit transa
36127 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20  ction */.  0,   
36128 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36129 20 20 20 20 20 20 20 2f 2a 20 78 52 6f 6c 6c 62         /* xRollb
3612a 61 63 6b 20 2d 20 72 6f 6c 6c 62 61 63 6b 20 74  ack - rollback t
3612b 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
3612c 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
3612d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3612e 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2d 20 66  FindFunction - f
3612f 75 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64  unction overload
36130 69 6e 67 20 2a 2f 0a 20 20 72 74 72 65 65 52 65  ing */.  rtreeRe
36131 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
36132 20 20 20 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20       /* xRename 
36133 2d 20 72 65 6e 61 6d 65 20 74 68 65 20 74 61 62  - rename the tab
36134 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 61 74 69 63  le */.};..static
36135 20 69 6e 74 20 72 74 72 65 65 53 71 6c 49 6e 69   int rtreeSqlIni
36136 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
36137 65 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a  ee, .  sqlite3 *
36138 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
36139 72 20 2a 7a 44 62 2c 20 0a 20 20 63 6f 6e 73 74  r *zDb, .  const
3613a 20 63 68 61 72 20 2a 7a 50 72 65 66 69 78 2c 20   char *zPrefix, 
3613b 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 0a  .  int isCreate.
3613c 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
3613d 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 23 64 65 66  LITE_OK;..  #def
3613e 69 6e 65 20 4e 5f 53 54 41 54 45 4d 45 4e 54 20  ine N_STATEMENT 
3613f 39 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  9.  static const
36140 20 63 68 61 72 20 2a 61 7a 53 71 6c 5b 4e 5f 53   char *azSql[N_S
36141 54 41 54 45 4d 45 4e 54 5d 20 3d 20 7b 0a 20 20  TATEMENT] = {.  
36142 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
36143 69 74 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65  ite the xxx_node
36144 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53   table */.    "S
36145 45 4c 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20  ELECT data FROM 
36146 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57  '%q'.'%q_node' W
36147 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31  HERE nodeno = :1
36148 22 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f  ",.    "INSERT O
36149 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27  R REPLACE INTO '
3614a 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41  %q'.'%q_node' VA
3614b 4c 55 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20  LUES(:1, :2)",. 
3614c 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
3614d 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57  '%q'.'%q_node' W
3614e 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31  HERE nodeno = :1
3614f 22 2c 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  ",..    /* Read 
36150 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 78 78  and write the xx
36151 78 5f 72 6f 77 69 64 20 74 61 62 6c 65 20 2a 2f  x_rowid table */
36152 0a 20 20 20 20 22 53 45 4c 45 43 54 20 6e 6f 64  .    "SELECT nod
36153 65 6e 6f 20 46 52 4f 4d 20 27 25 71 27 2e 27 25  eno FROM '%q'.'%
36154 71 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20 72  q_rowid' WHERE r
36155 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 20 20 20 20  owid = :1",.    
36156 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41  "INSERT OR REPLA
36157 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71  CE INTO '%q'.'%q
36158 5f 72 6f 77 69 64 27 20 56 41 4c 55 45 53 28 3a  _rowid' VALUES(:
36159 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
3615a 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
3615b 25 71 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20  %q_rowid' WHERE 
3615c 72 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 0a 20 20  rowid = :1",..  
3615d 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
3615e 69 74 65 20 74 68 65 20 78 78 78 5f 70 61 72 65  ite the xxx_pare
3615f 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  nt table */.    
36160 22 53 45 4c 45 43 54 20 70 61 72 65 6e 74 6e 6f  "SELECT parentno
36161 64 65 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71  de FROM '%q'.'%q
36162 5f 70 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e  _parent' WHERE n
36163 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20  odeno = :1",.   
36164 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c   "INSERT OR REPL
36165 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25  ACE INTO '%q'.'%
36166 71 5f 70 61 72 65 6e 74 27 20 56 41 4c 55 45 53  q_parent' VALUES
36167 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22  (:1, :2)",.    "
36168 44 45 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27  DELETE FROM '%q'
36169 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 57 48 45  .'%q_parent' WHE
3616a 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 0a  RE nodeno = :1".
3616b 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73    };.  sqlite3_s
3616c 74 6d 74 20 2a 2a 61 70 70 53 74 6d 74 5b 4e 5f  tmt **appStmt[N_
3616d 53 54 41 54 45 4d 45 4e 54 5d 3b 0a 20 20 69 6e  STATEMENT];.  in
3616e 74 20 69 3b 0a 0a 20 20 70 52 74 72 65 65 2d 3e  t i;..  pRtree->
3616f 64 62 20 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20  db = db;..  if( 
36170 69 73 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20  isCreate ){.    
36171 63 68 61 72 20 2a 7a 43 72 65 61 74 65 20 3d 20  char *zCreate = 
36172 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
36173 0a 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c  ."CREATE TABLE \
36174 22 25 77 5c 22 2e 5c 22 25 77 5f 6e 6f 64 65 5c  "%w\".\"%w_node\
36175 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52  "(nodeno INTEGER
36176 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 64 61   PRIMARY KEY, da
36177 74 61 20 42 4c 4f 42 29 3b 22 0a 22 43 52 45 41  ta BLOB);"."CREA
36178 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e  TE TABLE \"%w\".
36179 5c 22 25 77 5f 72 6f 77 69 64 5c 22 28 72 6f 77  \"%w_rowid\"(row
3617a 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
3617b 52 59 20 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49  RY KEY, nodeno I
3617c 4e 54 45 47 45 52 29 3b 22 0a 22 43 52 45 41 54  NTEGER);"."CREAT
3617d 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c  E TABLE \"%w\".\
3617e 22 25 77 5f 70 61 72 65 6e 74 5c 22 28 6e 6f 64  "%w_parent\"(nod
3617f 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d  eno INTEGER PRIM
36180 41 52 59 20 4b 45 59 2c 20 70 61 72 65 6e 74 6e  ARY KEY, parentn
36181 6f 64 65 20 49 4e 54 45 47 45 52 29 3b 22 0a 22  ode INTEGER);"."
36182 49 4e 53 45 52 54 20 49 4e 54 4f 20 27 25 71 27  INSERT INTO '%q'
36183 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45  .'%q_node' VALUE
36184 53 28 31 2c 20 7a 65 72 6f 62 6c 6f 62 28 25 64  S(1, zeroblob(%d
36185 29 29 22 2c 0a 20 20 20 20 20 20 7a 44 62 2c 20  ))",.      zDb, 
36186 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50  zPrefix, zDb, zP
36187 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65  refix, zDb, zPre
36188 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69  fix, zDb, zPrefi
36189 78 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  x, pRtree->iNode
3618a 53 69 7a 65 0a 20 20 20 20 29 3b 0a 20 20 20 20  Size.    );.    
3618b 69 66 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a  if( !zCreate ){.
3618c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
3618d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
3618e 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
3618f 33 5f 65 78 65 63 28 64 62 2c 20 7a 43 72 65 61  3_exec(db, zCrea
36190 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  te, 0, 0, 0);.  
36191 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
36192 43 72 65 61 74 65 29 3b 0a 20 20 20 20 69 66 28  Create);.    if(
36193 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
36194 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
36195 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
36196 61 70 70 53 74 6d 74 5b 30 5d 20 3d 20 26 70 52  appStmt[0] = &pR
36197 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 3b  tree->pReadNode;
36198 0a 20 20 61 70 70 53 74 6d 74 5b 31 5d 20 3d 20  .  appStmt[1] = 
36199 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e  &pRtree->pWriteN
3619a 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 32  ode;.  appStmt[2
3619b 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65  ] = &pRtree->pDe
3619c 6c 65 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53  leteNode;.  appS
3619d 74 6d 74 5b 33 5d 20 3d 20 26 70 52 74 72 65 65  tmt[3] = &pRtree
3619e 2d 3e 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20  ->pReadRowid;.  
3619f 61 70 70 53 74 6d 74 5b 34 5d 20 3d 20 26 70 52  appStmt[4] = &pR
361a0 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
361a1 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 35 5d 20  d;.  appStmt[5] 
361a2 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  = &pRtree->pDele
361a3 74 65 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74  teRowid;.  appSt
361a4 6d 74 5b 36 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[6] = &pRtree-
361a5 3e 70 52 65 61 64 50 61 72 65 6e 74 3b 0a 20 20  >pReadParent;.  
361a6 61 70 70 53 74 6d 74 5b 37 5d 20 3d 20 26 70 52  appStmt[7] = &pR
361a7 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65  tree->pWritePare
361a8 6e 74 3b 0a 20 20 61 70 70 53 74 6d 74 5b 38 5d  nt;.  appStmt[8]
361a9 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c   = &pRtree->pDel
361aa 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20 66 6f  eteParent;..  fo
361ab 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 54 41 54 45  r(i=0; i<N_STATE
361ac 4d 45 4e 54 20 26 26 20 72 63 3d 3d 53 51 4c 49  MENT && rc==SQLI
361ad 54 45 5f 4f 4b 3b 20 69 2b 2b 29 7b 0a 20 20 20  TE_OK; i++){.   
361ae 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71   char *zSql = sq
361af 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a  lite3_mprintf(az
361b0 53 71 6c 5b 69 5d 2c 20 7a 44 62 2c 20 7a 50 72  Sql[i], zDb, zPr
361b1 65 66 69 78 29 3b 0a 20 20 20 20 69 66 28 20 7a  efix);.    if( z
361b2 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Sql ){.      rc 
361b3 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
361b4 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
361b5 31 2c 20 61 70 70 53 74 6d 74 5b 69 5d 2c 20 30  1, appStmt[i], 0
361b6 29 3b 20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ); .    }else{. 
361b7 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
361b8 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
361b9 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
361ba 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Sql);.  }..  ret
361bb 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
361bc 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 71 75   This routine qu
361bd 65 72 69 65 73 20 64 61 74 61 62 61 73 65 20 68  eries database h
361be 61 6e 64 6c 65 20 64 62 20 66 6f 72 20 74 68 65  andle db for the
361bf 20 70 61 67 65 2d 73 69 7a 65 20 75 73 65 64 20   page-size used 
361c0 62 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 7a  by.** database z
361c1 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  Db. If successfu
361c2 6c 2c 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  l, the page-size
361c3 20 69 6e 20 62 79 74 65 73 20 69 73 20 77 72 69   in bytes is wri
361c4 74 74 65 6e 20 74 6f 0a 2a 2a 20 2a 70 69 50 61  tten to.** *piPa
361c5 67 65 53 69 7a 65 20 61 6e 64 20 53 51 4c 49 54  geSize and SQLIT
361c6 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 4f  E_OK returned. O
361c7 74 68 65 72 77 69 73 65 2c 20 61 6e 64 20 61 6e  therwise, and an
361c8 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a   SQLite error .*
361c9 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  * code is return
361ca 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
361cb 74 20 67 65 74 50 61 67 65 53 69 7a 65 28 73 71  t getPageSize(sq
361cc 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
361cd 20 63 68 61 72 20 2a 7a 44 62 2c 20 69 6e 74 20   char *zDb, int 
361ce 2a 70 69 50 61 67 65 53 69 7a 65 29 7b 0a 20 20  *piPageSize){.  
361cf 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
361d0 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a  NOMEM;.  char *z
361d1 53 71 6c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  Sql;.  sqlite3_s
361d2 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a  tmt *pStmt = 0;.
361d3 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
361d4 33 5f 6d 70 72 69 6e 74 66 28 22 50 52 41 47 4d  3_mprintf("PRAGM
361d5 41 20 25 51 2e 70 61 67 65 5f 73 69 7a 65 22 2c  A %Q.page_size",
361d6 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 21 7a 53   zDb);.  if( !zS
361d7 71 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ql ){.    return
361d8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
361d9 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
361da 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
361db 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
361dc 6d 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  mt, 0);.  sqlite
361dd 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
361de 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
361df 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
361e0 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53  rc;.  }..  if( S
361e1 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
361e2 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
361e3 7b 0a 20 20 20 20 2a 70 69 50 61 67 65 53 69 7a  {.    *piPageSiz
361e4 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
361e5 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30 29  mn_int(pStmt, 0)
361e6 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
361e7 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
361e8 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  pStmt);.}../* .*
361e9 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
361ea 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
361eb 61 74 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74 68  ation of both th
361ec 65 20 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20 78  e xConnect and x
361ed 43 72 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f 64  Create.** method
361ee 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  s of the r-tree 
361ef 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
361f0 2a 0a 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20  *.**   argv[0]  
361f1 20 2d 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a   -> module name.
361f2 2a 2a 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d  **   argv[1]   -
361f3 3e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a  > database name.
361f4 2a 2a 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d  **   argv[2]   -
361f5 3e 20 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a 20  > table name.** 
361f6 20 20 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 63    argv[...] -> c
361f7 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 2e 2e 0a 2a  olumn names....*
361f8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
361f9 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  eeInit(.  sqlite
361fa 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
361fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
361fc 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
361fd 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
361fe 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20  pAux,           
361ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36200 20 4f 6e 65 20 6f 66 20 74 68 65 20 52 54 52 45   One of the RTRE
36201 45 5f 43 4f 4f 52 44 5f 2a 20 63 6f 6e 73 74 61  E_COORD_* consta
36202 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67  nts */.  int arg
36203 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
36204 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20 2f 2a 20  onst*argv,   /* 
36205 50 61 72 61 6d 65 74 65 72 73 20 74 6f 20 43 52  Parameters to CR
36206 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
36207 6d 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ment */.  sqlite
36208 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
36209 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3620a 20 4f 55 54 3a 20 4e 65 77 20 76 69 72 74 75 61   OUT: New virtua
3620b 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  l table */.  cha
3620c 72 20 2a 2a 70 7a 45 72 72 2c 20 20 20 20 20 20  r **pzErr,      
3620d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3620e 20 2f 2a 20 4f 55 54 3a 20 45 72 72 6f 72 20 6d   /* OUT: Error m
3620f 65 73 73 61 67 65 2c 20 69 66 20 61 6e 79 20 2a  essage, if any *
36210 2f 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65  /.  int isCreate
36211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36212 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
36213 66 6f 72 20 78 43 72 65 61 74 65 2c 20 66 61 6c  for xCreate, fal
36214 73 65 20 66 6f 72 20 78 43 6f 6e 6e 65 63 74 20  se for xConnect 
36215 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
36216 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
36217 74 20 69 50 61 67 65 53 69 7a 65 20 3d 20 30 3b  t iPageSize = 0;
36218 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
36219 3b 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20  ;.  int nDb;    
3621a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
3621b 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 61 72  gth of string ar
3621c 67 76 5b 31 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  gv[1] */.  int n
3621d 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
3621e 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74   /* Length of st
3621f 72 69 6e 67 20 61 72 67 76 5b 32 5d 20 2a 2f 0a  ring argv[2] */.
36220 20 20 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65    int eCoordType
36221 20 3d 20 28 69 6e 74 29 70 41 75 78 3b 0a 0a 20   = (int)pAux;.. 
36222 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 45 72   const char *aEr
36223 72 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  rMsg[] = {.    0
36224 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36225 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36226 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36227 20 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20       /* 0 */.   
36228 20 22 57 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f   "Wrong number o
36229 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e  f columns for an
3622a 20 72 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20   rtree table",  
3622b 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20         /* 1 */. 
3622c 20 20 20 22 54 6f 6f 20 66 65 77 20 63 6f 6c 75     "Too few colu
3622d 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65  mns for an rtree
3622e 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20   table",        
3622f 20 20 20 20 20 20 20 20 20 2f 2a 20 32 20 2a 2f           /* 2 */
36230 0a 20 20 20 20 22 54 6f 6f 20 6d 61 6e 79 20 63  .    "Too many c
36231 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74  olumns for an rt
36232 72 65 65 20 74 61 62 6c 65 22 20 20 20 20 20 20  ree table"      
36233 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 20             /* 3 
36234 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69  */.  };..  int i
36235 45 72 72 20 3d 20 28 61 72 67 63 3c 36 29 20 3f  Err = (argc<6) ?
36236 20 32 20 3a 20 61 72 67 63 3e 28 52 54 52 45 45   2 : argc>(RTREE
36237 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a  _MAX_DIMENSIONS*
36238 32 2b 34 29 20 3f 20 33 20 3a 20 61 72 67 63 25  2+4) ? 3 : argc%
36239 32 3b 0a 20 20 69 66 28 20 61 45 72 72 4d 73 67  2;.  if( aErrMsg
3623a 5b 69 45 72 72 5d 20 29 7b 0a 20 20 20 20 2a 70  [iErr] ){.    *p
3623b 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
3623c 70 72 69 6e 74 66 28 22 25 73 22 2c 20 61 45 72  printf("%s", aEr
3623d 72 4d 73 67 5b 69 45 72 72 5d 29 3b 0a 20 20 20  rMsg[iErr]);.   
3623e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
3623f 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  RROR;.  }..  rc 
36240 3d 20 67 65 74 50 61 67 65 53 69 7a 65 28 64 62  = getPageSize(db
36241 2c 20 61 72 67 76 5b 31 5d 2c 20 26 69 50 61 67  , argv[1], &iPag
36242 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63  eSize);.  if( rc
36243 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
36244 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
36245 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
36246 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61   the sqlite3_vta
36247 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  b structure */. 
36248 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e 28 61 72   nDb = strlen(ar
36249 67 76 5b 31 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20  gv[1]);.  nName 
3624a 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 32 5d  = strlen(argv[2]
3624b 29 3b 0a 20 20 70 52 74 72 65 65 20 3d 20 28 52  );.  pRtree = (R
3624c 74 72 65 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d  tree *)sqlite3_m
3624d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72  alloc(sizeof(Rtr
3624e 65 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29  ee)+nDb+nName+2)
3624f 3b 0a 20 20 69 66 28 20 21 70 52 74 72 65 65 20  ;.  if( !pRtree 
36250 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
36251 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
36252 20 20 6d 65 6d 73 65 74 28 70 52 74 72 65 65 2c    memset(pRtree,
36253 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
36254 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a  )+nDb+nName+2);.
36255 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 20    pRtree->nBusy 
36256 3d 20 31 3b 0a 20 20 70 52 74 72 65 65 2d 3e 62  = 1;.  pRtree->b
36257 61 73 65 2e 70 4d 6f 64 75 6c 65 20 3d 20 26 72  ase.pModule = &r
36258 74 72 65 65 4d 6f 64 75 6c 65 3b 0a 20 20 70 52  treeModule;.  pR
36259 74 72 65 65 2d 3e 7a 44 62 20 3d 20 28 63 68 61  tree->zDb = (cha
3625a 72 20 2a 29 26 70 52 74 72 65 65 5b 31 5d 3b 0a  r *)&pRtree[1];.
3625b 20 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 20    pRtree->zName 
3625c 3d 20 26 70 52 74 72 65 65 2d 3e 7a 44 62 5b 6e  = &pRtree->zDb[n
3625d 44 62 2b 31 5d 3b 0a 20 20 70 52 74 72 65 65 2d  Db+1];.  pRtree-
3625e 3e 6e 44 69 6d 20 3d 20 28 61 72 67 63 2d 34 29  >nDim = (argc-4)
3625f 2f 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42  /2;.  pRtree->nB
36260 79 74 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20  ytesPerCell = 8 
36261 2b 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 34  + pRtree->nDim*4
36262 2a 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 65 43  *2;.  pRtree->eC
36263 6f 6f 72 64 54 79 70 65 20 3d 20 65 43 6f 6f 72  oordType = eCoor
36264 64 54 79 70 65 3b 0a 20 20 6d 65 6d 63 70 79 28  dType;.  memcpy(
36265 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 61 72 67  pRtree->zDb, arg
36266 76 5b 31 5d 2c 20 6e 44 62 29 3b 0a 20 20 6d 65  v[1], nDb);.  me
36267 6d 63 70 79 28 70 52 74 72 65 65 2d 3e 7a 4e 61  mcpy(pRtree->zNa
36268 6d 65 2c 20 61 72 67 76 5b 32 5d 2c 20 6e 4e 61  me, argv[2], nNa
36269 6d 65 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  me);..  /* Figur
3626a 65 20 6f 75 74 20 74 68 65 20 6e 6f 64 65 20 73  e out the node s
3626b 69 7a 65 20 74 6f 20 75 73 65 2e 20 42 79 20 64  ize to use. By d
3626c 65 66 61 75 6c 74 2c 20 75 73 65 20 36 34 20 62  efault, use 64 b
3626d 79 74 65 73 20 6c 65 73 73 20 74 68 61 6e 0a 20  ytes less than. 
3626e 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
3626f 20 70 61 67 65 2d 73 69 7a 65 2e 20 54 68 69 73   page-size. This
36270 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 65 61   ensures that ea
36271 63 68 20 6e 6f 64 65 20 69 73 20 73 74 6f 72 65  ch node is store
36272 64 20 6f 6e 0a 20 20 2a 2a 20 61 20 73 69 6e 67  d on.  ** a sing
36273 6c 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  le database page
36274 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
36275 68 65 20 64 61 74 61 62 61 73 64 20 70 61 67 65  he databasd page
36276 2d 73 69 7a 65 20 69 73 20 73 6f 20 6c 61 72 67  -size is so larg
36277 65 20 74 68 61 74 20 6d 6f 72 65 20 74 68 61 6e  e that more than
36278 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 0a   RTREE_MAXCELLS.
36279 20 20 2a 2a 20 65 6e 74 72 69 65 73 20 77 6f 75    ** entries wou
3627a 6c 64 20 66 69 74 20 69 6e 20 61 20 73 69 6e 67  ld fit in a sing
3627b 6c 65 20 6e 6f 64 65 2c 20 75 73 65 20 61 20 73  le node, use a s
3627c 6d 61 6c 6c 65 72 20 6e 6f 64 65 2d 73 69 7a 65  maller node-size
3627d 2e 0a 20 20 2a 2f 0a 20 20 70 52 74 72 65 65 2d  ..  */.  pRtree-
3627e 3e 69 4e 6f 64 65 53 69 7a 65 20 3d 20 69 50 61  >iNodeSize = iPa
3627f 67 65 53 69 7a 65 2d 36 34 3b 0a 20 20 69 66 28  geSize-64;.  if(
36280 20 28 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74   (4+pRtree->nByt
36281 65 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f  esPerCell*RTREE_
36282 4d 41 58 43 45 4c 4c 53 29 3c 70 52 74 72 65 65  MAXCELLS)<pRtree
36283 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20  ->iNodeSize ){. 
36284 20 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65     pRtree->iNode
36285 53 69 7a 65 20 3d 20 34 2b 70 52 74 72 65 65 2d  Size = 4+pRtree-
36286 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52  >nBytesPerCell*R
36287 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 3b 0a 20  TREE_MAXCELLS;. 
36288 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f   }..  /* Create/
36289 43 6f 6e 6e 65 63 74 20 74 6f 20 74 68 65 20 75  Connect to the u
3628a 6e 64 65 72 6c 79 69 6e 67 20 72 65 6c 61 74 69  nderlying relati
3628b 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73 63  onal database sc
3628c 68 65 6d 61 2e 20 49 66 0a 20 20 2a 2a 20 74 68  hema. If.  ** th
3628d 61 74 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  at is successful
3628e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 64  , call sqlite3_d
3628f 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 74 6f  eclare_vtab() to
36290 20 63 6f 6e 66 69 67 75 72 65 0a 20 20 2a 2a 20   configure.  ** 
36291 74 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65  the r-tree table
36292 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2f 0a 20 20   schema..  */.  
36293 69 66 28 20 28 72 63 20 3d 20 72 74 72 65 65 53  if( (rc = rtreeS
36294 71 6c 49 6e 69 74 28 70 52 74 72 65 65 2c 20 64  qlInit(pRtree, d
36295 62 2c 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 76  b, argv[1], argv
36296 5b 32 5d 2c 20 69 73 43 72 65 61 74 65 29 29 20  [2], isCreate)) 
36297 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  ){.    *pzErr = 
36298 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
36299 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72  "%s", sqlite3_er
3629a 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 7d 65 6c  rmsg(db));.  }el
3629b 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53  se{.    char *zS
3629c 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
3629d 69 6e 74 66 28 22 43 52 45 41 54 45 20 54 41 42  intf("CREATE TAB
3629e 4c 45 20 78 28 25 73 22 2c 20 61 72 67 76 5b 33  LE x(%s", argv[3
3629f 5d 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  ]);.    char *zT
362a0 6d 70 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a  mp;.    int ii;.
362a1 20 20 20 20 66 6f 72 28 69 69 3d 34 3b 20 7a 53      for(ii=4; zS
362a2 71 6c 20 26 26 20 69 69 3c 61 72 67 63 3b 20 69  ql && ii<argc; i
362a3 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 54 6d 70  i++){.      zTmp
362a4 20 3d 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 7a   = zSql;.      z
362a5 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
362a6 72 69 6e 74 66 28 22 25 73 2c 20 25 73 22 2c 20  rintf("%s, %s", 
362a7 7a 54 6d 70 2c 20 61 72 67 76 5b 69 69 5d 29 3b  zTmp, argv[ii]);
362a8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
362a9 72 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d  ree(zTmp);.    }
362aa 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  .    if( zSql ){
362ab 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53  .      zTmp = zS
362ac 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d  ql;.      zSql =
362ad 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
362ae 28 22 25 73 29 3b 22 2c 20 7a 54 6d 70 29 3b 0a  ("%s);", zTmp);.
362af 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
362b0 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d 0a  ee(zTmp);.    }.
362b1 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 7c 7c      if( !zSql ||
362b2 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
362b3 5f 76 74 61 62 28 64 62 2c 20 7a 53 71 6c 29 20  _vtab(db, zSql) 
362b4 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
362b5 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
362b6 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
362b7 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20  ee(zSql);.  }.. 
362b8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
362b9 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 70 56 74 61  OK ){.    *ppVta
362ba 62 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61  b = (sqlite3_vta
362bb 62 20 2a 29 70 52 74 72 65 65 3b 0a 20 20 7d 65  b *)pRtree;.  }e
362bc 6c 73 65 7b 0a 20 20 20 20 72 74 72 65 65 52 65  lse{.    rtreeRe
362bd 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20  lease(pRtree);. 
362be 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
362bf 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  }.../*.** Implem
362c0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 63  entation of a sc
362c1 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 68  alar function th
362c2 61 74 20 64 65 63 6f 64 65 73 20 72 2d 74 72 65  at decodes r-tre
362c3 65 20 6e 6f 64 65 73 20 74 6f 0a 2a 2a 20 68 75  e nodes to.** hu
362c4 6d 61 6e 20 72 65 61 64 61 62 6c 65 20 73 74 72  man readable str
362c5 69 6e 67 73 2e 20 54 68 69 73 20 63 61 6e 20 62  ings. This can b
362c6 65 20 75 73 65 64 20 66 6f 72 20 64 65 62 75 67  e used for debug
362c7 67 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69  ging and analysi
362c8 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61  s..**.** The sca
362c9 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  lar function tak
362ca 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  es two arguments
362cb 2c 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61  , a blob of data
362cc 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61   containing.** a
362cd 6e 20 72 2d 74 72 65 65 20 6e 6f 64 65 2c 20 61  n r-tree node, a
362ce 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nd the number of
362cf 20 64 69 6d 65 6e 73 69 6f 6e 73 20 74 68 65 20   dimensions the 
362d0 72 2d 74 72 65 65 20 69 6e 64 65 78 65 73 2e 0a  r-tree indexes..
362d1 2a 2a 20 46 6f 72 20 61 20 74 77 6f 2d 64 69 6d  ** For a two-dim
362d2 65 6e 73 69 6f 6e 61 6c 20 72 2d 74 72 65 65 20  ensional r-tree 
362d3 73 74 72 75 63 74 75 72 65 20 63 61 6c 6c 65 64  structure called
362d4 20 22 72 74 22 2c 20 74 6f 20 64 65 73 65 72 69   "rt", to deseri
362d5 61 6c 69 7a 65 0a 2a 2a 20 61 6c 6c 20 6e 6f 64  alize.** all nod
362d6 65 73 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20  es, a statement 
362d7 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  like:.**.**   SE
362d8 4c 45 43 54 20 72 74 72 65 65 6e 6f 64 65 28 32  LECT rtreenode(2
362d9 2c 20 64 61 74 61 29 20 46 52 4f 4d 20 72 74 5f  , data) FROM rt_
362da 6e 6f 64 65 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20  node;.**.** The 
362db 68 75 6d 61 6e 20 72 65 61 64 61 62 6c 65 20 73  human readable s
362dc 74 72 69 6e 67 20 74 61 6b 65 73 20 74 68 65 20  tring takes the 
362dd 66 6f 72 6d 20 6f 66 20 61 20 54 63 6c 20 6c 69  form of a Tcl li
362de 73 74 20 77 69 74 68 20 6f 6e 65 0a 2a 2a 20 65  st with one.** e
362df 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 63 65  ntry for each ce
362e0 6c 6c 20 69 6e 20 74 68 65 20 72 2d 74 72 65 65  ll in the r-tree
362e1 20 6e 6f 64 65 2e 20 45 61 63 68 20 65 6e 74 72   node. Each entr
362e2 79 20 69 73 20 69 74 73 65 6c 66 20 61 0a 2a 2a  y is itself a.**
362e3 20 6c 69 73 74 2c 20 63 6f 6e 74 61 69 6e 69 6e   list, containin
362e4 67 20 74 68 65 20 38 2d 62 79 74 65 20 72 6f 77  g the 8-byte row
362e5 69 64 2f 70 61 67 65 6e 6f 20 66 6f 6c 6c 6f 77  id/pageno follow
362e6 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 3c 6e  ed by the .** <n
362e7 75 6d 2d 64 69 6d 65 6e 73 69 6f 6e 3e 2a 32 20  um-dimension>*2 
362e8 63 6f 6f 72 64 69 6e 61 74 65 73 2e 0a 2a 2f 0a  coordinates..*/.
362e9 73 74 61 74 69 63 20 76 6f 69 64 20 72 74 72 65  static void rtre
362ea 65 6e 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f  enode(sqlite3_co
362eb 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e 74 20  ntext *ctx, int 
362ec 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  nArg, sqlite3_va
362ed 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20  lue **apArg){.  
362ee 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b  char *zText = 0;
362ef 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 6e 6f 64  .  RtreeNode nod
362f0 65 3b 0a 20 20 52 74 72 65 65 20 74 72 65 65 3b  e;.  Rtree tree;
362f1 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20 20 6d 65  .  int ii;..  me
362f2 6d 73 65 74 28 26 6e 6f 64 65 2c 20 30 2c 20 73  mset(&node, 0, s
362f3 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29  izeof(RtreeNode)
362f4 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 74 72 65  );.  memset(&tre
362f5 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72  e, 0, sizeof(Rtr
362f6 65 65 29 29 3b 0a 20 20 74 72 65 65 2e 6e 44 69  ee));.  tree.nDi
362f7 6d 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  m = sqlite3_valu
362f8 65 5f 69 6e 74 28 61 70 41 72 67 5b 30 5d 29 3b  e_int(apArg[0]);
362f9 0a 20 20 74 72 65 65 2e 6e 42 79 74 65 73 50 65  .  tree.nBytesPe
362fa 72 43 65 6c 6c 20 3d 20 38 20 2b 20 38 20 2a 20  rCell = 8 + 8 * 
362fb 74 72 65 65 2e 6e 44 69 6d 3b 0a 20 20 6e 6f 64  tree.nDim;.  nod
362fc 65 2e 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29  e.zData = (u8 *)
362fd 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
362fe 6f 62 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20  ob(apArg[1]);.. 
362ff 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 4e 43   for(ii=0; ii<NC
36300 45 4c 4c 28 26 6e 6f 64 65 29 3b 20 69 69 2b 2b  ELL(&node); ii++
36301 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 43 65 6c  ){.    char zCel
36302 6c 5b 35 31 32 5d 3b 0a 20 20 20 20 69 6e 74 20  l[512];.    int 
36303 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 52  nCell = 0;.    R
36304 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
36305 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20     int jj;..    
36306 6e 6f 64 65 47 65 74 43 65 6c 6c 28 26 74 72 65  nodeGetCell(&tre
36307 65 2c 20 26 6e 6f 64 65 2c 20 69 69 2c 20 26 63  e, &node, ii, &c
36308 65 6c 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ell);.    sqlite
36309 33 5f 73 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e  3_snprintf(512-n
3630a 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c  Cell,&zCell[nCel
3630b 6c 5d 2c 22 25 64 22 2c 20 63 65 6c 6c 2e 69 52  l],"%d", cell.iR
3630c 6f 77 69 64 29 3b 0a 20 20 20 20 6e 43 65 6c 6c  owid);.    nCell
3630d 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29   = strlen(zCell)
3630e 3b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20  ;.    for(jj=0; 
3630f 6a 6a 3c 74 72 65 65 2e 6e 44 69 6d 2a 32 3b 20  jj<tree.nDim*2; 
36310 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c  jj++){.      sql
36311 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 31  ite3_snprintf(51
36312 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e  2-nCell,&zCell[n
36313 43 65 6c 6c 5d 2c 22 20 25 66 22 2c 28 64 6f 75  Cell]," %f",(dou
36314 62 6c 65 29 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b  ble)cell.aCoord[
36315 6a 6a 5d 2e 66 29 3b 0a 20 20 20 20 20 20 6e 43  jj].f);.      nC
36316 65 6c 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65  ell = strlen(zCe
36317 6c 6c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ll);.    }..    
36318 69 66 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20  if( zText ){.   
36319 20 20 20 63 68 61 72 20 2a 7a 54 65 78 74 4e 65     char *zTextNe
3631a 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  w = sqlite3_mpri
3631b 6e 74 66 28 22 25 73 20 7b 25 73 7d 22 2c 20 7a  ntf("%s {%s}", z
3631c 54 65 78 74 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20  Text, zCell);.  
3631d 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
3631e 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7a  (zText);.      z
3631f 54 65 78 74 20 3d 20 7a 54 65 78 74 4e 65 77 3b  Text = zTextNew;
36320 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
36321 20 20 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65    zText = sqlite
36322 33 5f 6d 70 72 69 6e 74 66 28 22 7b 25 73 7d 22  3_mprintf("{%s}"
36323 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a  , zCell);.    }.
36324 20 20 7d 0a 20 20 0a 20 20 73 71 6c 69 74 65 33    }.  .  sqlite3
36325 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 74 78  _result_text(ctx
36326 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 73 71 6c  , zText, -1, sql
36327 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 73  ite3_free);.}..s
36328 74 61 74 69 63 20 76 6f 69 64 20 72 74 72 65 65  tatic void rtree
36329 64 65 70 74 68 28 73 71 6c 69 74 65 33 5f 63 6f  depth(sqlite3_co
3632a 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e 74 20  ntext *ctx, int 
3632b 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  nArg, sqlite3_va
3632c 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20  lue **apArg){.  
3632d 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
3632e 65 5f 74 79 70 65 28 61 70 41 72 67 5b 30 5d 29  e_type(apArg[0])
3632f 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 0a 20  !=SQLITE_BLOB . 
36330 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c    || sqlite3_val
36331 75 65 5f 62 79 74 65 73 28 61 70 41 72 67 5b 30  ue_bytes(apArg[0
36332 5d 29 3c 32 0a 20 20 29 7b 0a 20 20 20 20 73 71  ])<2.  ){.    sq
36333 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
36334 6f 72 28 63 74 78 2c 20 22 49 6e 76 61 6c 69 64  or(ctx, "Invalid
36335 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 74 72   argument to rtr
36336 65 65 64 65 70 74 68 28 29 22 2c 20 2d 31 29 3b  eedepth()", -1);
36337 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75   .  }else{.    u
36338 38 20 2a 7a 42 6c 6f 62 20 3d 20 28 75 38 20 2a  8 *zBlob = (u8 *
36339 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  )sqlite3_value_b
3633a 6c 6f 62 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20  lob(apArg[0]);. 
3633b 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3633c 74 5f 69 6e 74 28 63 74 78 2c 20 72 65 61 64 49  t_int(ctx, readI
3633d 6e 74 31 36 28 7a 42 6c 6f 62 29 29 3b 0a 20 20  nt16(zBlob));.  
3633e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  }.}../*.** Regis
3633f 74 65 72 20 74 68 65 20 72 2d 74 72 65 65 20 6d  ter the r-tree m
36340 6f 64 75 6c 65 20 77 69 74 68 20 64 61 74 61 62  odule with datab
36341 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 20 54  ase handle db. T
36342 68 69 73 20 63 72 65 61 74 65 73 20 74 68 65 0a  his creates the.
36343 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
36344 20 6d 6f 64 75 6c 65 20 22 72 74 72 65 65 22 20   module "rtree" 
36345 61 6e 64 20 74 68 65 20 64 65 62 75 67 67 69 6e  and the debuggin
36346 67 2f 61 6e 61 6c 79 73 69 73 20 73 63 61 6c 61  g/analysis scala
36347 72 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 22  r .** function "
36348 72 74 72 65 65 6e 6f 64 65 22 2e 0a 2a 2f 0a 53  rtreenode"..*/.S
36349 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
3634a 74 20 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e  t sqlite3RtreeIn
3634b 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  it(sqlite3 *db){
3634c 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
3634d 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 72 63  TE_OK;..  if( rc
3634e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3634f 20 20 20 69 6e 74 20 75 74 66 38 20 3d 20 53 51     int utf8 = SQ
36350 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 72  LITE_UTF8;.    r
36351 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61  c = sqlite3_crea
36352 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  te_function(db, 
36353 22 72 74 72 65 65 6e 6f 64 65 22 2c 20 32 2c 20  "rtreenode", 2, 
36354 75 74 66 38 2c 20 30 2c 20 72 74 72 65 65 6e 6f  utf8, 0, rtreeno
36355 64 65 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20  de, 0, 0);.  }. 
36356 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
36357 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 75 74  OK ){.    int ut
36358 66 38 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  f8 = SQLITE_UTF8
36359 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
3635a 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
3635b 6f 6e 28 64 62 2c 20 22 72 74 72 65 65 64 65 70  on(db, "rtreedep
3635c 74 68 22 2c 20 31 2c 20 75 74 66 38 2c 20 30 2c  th", 1, utf8, 0,
3635d 72 74 72 65 65 64 65 70 74 68 2c 20 30 2c 20 30  rtreedepth, 0, 0
3635e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
3635f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
36360 20 20 76 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69    void *c = (voi
36361 64 20 2a 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f  d *)RTREE_COORD_
36362 52 45 41 4c 33 32 3b 0a 20 20 20 20 72 63 20 3d  REAL32;.    rc =
36363 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
36364 6d 6f 64 75 6c 65 5f 76 32 28 64 62 2c 20 22 72  module_v2(db, "r
36365 74 72 65 65 22 2c 20 26 72 74 72 65 65 4d 6f 64  tree", &rtreeMod
36366 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a  ule, c, 0);.  }.
36367 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
36368 5f 4f 4b 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  _OK ){.    void 
36369 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29 52 54 52  *c = (void *)RTR
3636a 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 3b 0a  EE_COORD_INT32;.
3636b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3636c 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  _create_module_v
3636d 32 28 64 62 2c 20 22 72 74 72 65 65 5f 69 33 32  2(db, "rtree_i32
3636e 22 2c 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 2c  ", &rtreeModule,
3636f 20 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 72   c, 0);.  }..  r
36370 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
36371 20 21 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51   !SQLITE_CORE.SQ
36372 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
36373 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
36374 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit(.  sqlite3 *
36375 64 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  db,.  char **pzE
36376 72 72 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73  rrMsg,.  const s
36377 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
36378 6e 65 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53  nes *pApi.){.  S
36379 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f  QLITE_EXTENSION_
3637a 49 4e 49 54 32 28 70 41 70 69 29 0a 20 20 72 65  INIT2(pApi).  re
3637b 74 75 72 6e 20 73 71 6c 69 74 65 33 52 74 72 65  turn sqlite3Rtre
3637c 65 49 6e 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e  eInit(db);.}.#en
3637d 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  dif..#endif../**
3637e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
3637f 20 6f 66 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a   of rtree.c ****
36380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36381 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36382 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
36383 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
36384 69 6e 20 66 69 6c 65 20 69 63 75 2e 63 20 2a 2a  in file icu.c **
36385 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
36388 2a 2a 20 32 30 30 37 20 4d 61 79 20 36 0a 2a 2a  ** 2007 May 6.**
36389 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
3638a 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
3638b 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
3638c 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
3638d 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
3638e 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
3638f 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
36390 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
36391 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
36392 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
36393 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
36394 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
36395 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
36396 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
36397 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
36398 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
36399 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
3639a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
3639b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3639c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3639d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3639e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
3639f 24 49 64 3a 20 69 63 75 2e 63 2c 76 20 31 2e 37  $Id: icu.c,v 1.7
363a0 20 32 30 30 37 2f 31 32 2f 31 33 20 32 31 3a 35   2007/12/13 21:5
363a1 34 3a 31 31 20 64 72 68 20 45 78 70 20 24 0a 2a  4:11 drh Exp $.*
363a2 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
363a3 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 69 6e 74  mplements an int
363a4 65 67 72 61 74 69 6f 6e 20 62 65 74 77 65 65 6e  egration between
363a5 20 74 68 65 20 49 43 55 20 6c 69 62 72 61 72 79   the ICU library
363a6 20 0a 2a 2a 20 28 22 49 6e 74 65 72 6e 61 74 69   .** ("Internati
363a7 6f 6e 61 6c 20 43 6f 6d 70 6f 6e 65 6e 74 73 20  onal Components 
363a8 66 6f 72 20 55 6e 69 63 6f 64 65 22 2c 20 61 6e  for Unicode", an
363a9 20 6f 70 65 6e 2d 73 6f 75 72 63 65 20 6c 69 62   open-source lib
363aa 72 61 72 79 20 0a 2a 2a 20 66 6f 72 20 68 61 6e  rary .** for han
363ab 64 6c 69 6e 67 20 75 6e 69 63 6f 64 65 20 64 61  dling unicode da
363ac 74 61 29 20 61 6e 64 20 53 51 4c 69 74 65 2e 20  ta) and SQLite. 
363ad 54 68 65 20 69 6e 74 65 67 72 61 74 69 6f 6e 20  The integration 
363ae 75 73 65 73 20 0a 2a 2a 20 49 43 55 20 74 6f 20  uses .** ICU to 
363af 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c  provide the foll
363b0 6f 77 69 6e 67 20 74 6f 20 53 51 4c 69 74 65 3a  owing to SQLite:
363b1 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d  .**.**   * An im
363b2 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
363b3 74 68 65 20 53 51 4c 20 72 65 67 65 78 70 28 29  the SQL regexp()
363b4 20 66 75 6e 63 74 69 6f 6e 20 28 61 6e 64 20 68   function (and h
363b5 65 6e 63 65 20 52 45 47 45 58 50 0a 2a 2a 20 20  ence REGEXP.**  
363b6 20 20 20 6f 70 65 72 61 74 6f 72 29 20 75 73 69     operator) usi
363b7 6e 67 20 74 68 65 20 49 43 55 20 75 72 65 67 65  ng the ICU urege
363b8 78 5f 58 58 28 29 20 41 50 49 73 2e 0a 2a 2a 0a  x_XX() APIs..**.
363b9 2a 2a 20 20 20 2a 20 49 6d 70 6c 65 6d 65 6e 74  **   * Implement
363ba 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 53 51  ations of the SQ
363bb 4c 20 73 63 61 6c 61 72 20 75 70 70 65 72 28 29  L scalar upper()
363bc 20 61 6e 64 20 6c 6f 77 65 72 28 29 20 66 75 6e   and lower() fun
363bd 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 66 6f  ctions.**     fo
363be 72 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2e 0a  r case mapping..
363bf 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6e 74 65 67 72  **.**   * Integr
363c0 61 74 69 6f 6e 20 6f 66 20 49 43 55 20 61 6e 64  ation of ICU and
363c1 20 53 51 4c 69 74 65 20 63 6f 6c 6c 61 74 69 6f   SQLite collatio
363c2 6e 20 73 65 71 65 6e 63 65 73 2e 0a 2a 2a 0a 2a  n seqences..**.*
363c3 2a 20 20 20 2a 20 41 6e 20 69 6d 70 6c 65 6d 65  *   * An impleme
363c4 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c  ntation of the L
363c5 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 68 61  IKE operator tha
363c6 74 20 75 73 65 73 20 49 43 55 20 74 6f 20 0a 2a  t uses ICU to .*
363c7 2a 20 20 20 20 20 70 72 6f 76 69 64 65 20 63 61  *     provide ca
363c8 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 6d  se-independent m
363c9 61 74 63 68 69 6e 67 2e 0a 2a 2f 0a 0a 23 69 66  atching..*/..#if
363ca 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
363cb 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
363cc 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
363cd 49 43 55 29 0a 0a 2f 2a 20 49 6e 63 6c 75 64 65  ICU)../* Include
363ce 20 49 43 55 20 68 65 61 64 65 72 73 20 2a 2f 0a   ICU headers */.
363cf 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
363d0 65 2f 75 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63  e/utypes.h>.#inc
363d1 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 72  lude <unicode/ur
363d2 65 67 65 78 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  egex.h>.#include
363d3 20 3c 75 6e 69 63 6f 64 65 2f 75 73 74 72 69 6e   <unicode/ustrin
363d4 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75  g.h>.#include <u
363d5 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e 68 3e 0a 0a  nicode/ucol.h>..
363d6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
363d7 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f 45 58  CORE.  SQLITE_EX
363d8 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65  TENSION_INIT1.#e
363d9 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  lse.#endif../*.*
363da 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
363db 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74   (in bytes) of t
363dc 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20  he pattern in a 
363dd 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20  LIKE or GLOB.** 
363de 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66  operator..*/.#if
363df 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
363e0 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
363e1 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
363e2 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54  ITE_MAX_LIKE_PAT
363e3 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30  TERN_LENGTH 5000
363e4 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
363e5 56 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74  Version of sqlit
363e6 65 33 5f 66 72 65 65 28 29 20 74 68 61 74 20 69  e3_free() that i
363e7 73 20 61 6c 77 61 79 73 20 61 20 66 75 6e 63 74  s always a funct
363e8 69 6f 6e 2c 20 6e 65 76 65 72 20 61 20 6d 61 63  ion, never a mac
363e9 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ro..*/.static vo
363ea 69 64 20 78 46 72 65 65 28 76 6f 69 64 20 2a 70  id xFree(void *p
363eb 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
363ec 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  e(p);.}../*.** C
363ed 6f 6d 70 61 72 65 20 74 77 6f 20 55 54 46 2d 38  ompare two UTF-8
363ee 20 73 74 72 69 6e 67 73 20 66 6f 72 20 65 71 75   strings for equ
363ef 61 6c 69 74 79 20 77 68 65 72 65 20 74 68 65 20  ality where the 
363f0 66 69 72 73 74 20 73 74 72 69 6e 67 20 69 73 0a  first string is.
363f1 2a 2a 20 61 20 22 4c 49 4b 45 22 20 65 78 70 72  ** a "LIKE" expr
363f2 65 73 73 69 6f 6e 2e 20 52 65 74 75 72 6e 20 74  ession. Return t
363f3 72 75 65 20 28 31 29 20 69 66 20 74 68 65 79 20  rue (1) if they 
363f4 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64  are the same and
363f5 20 0a 2a 2a 20 66 61 6c 73 65 20 28 30 29 20 69   .** false (0) i
363f6 66 20 74 68 65 79 20 61 72 65 20 64 69 66 66 65  f they are diffe
363f7 72 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rent..*/.static 
363f8 69 6e 74 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61  int icuLikeCompa
363f9 72 65 28 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74  re(.  const uint
363fa 38 5f 74 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20  8_t *zPattern,  
363fb 20 2f 2a 20 4c 49 4b 45 20 70 61 74 74 65 72 6e   /* LIKE pattern
363fc 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74   */.  const uint
363fd 38 5f 74 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20  8_t *zString,   
363fe 20 2f 2a 20 54 68 65 20 55 54 46 2d 38 20 73 74   /* The UTF-8 st
363ff 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20  ring to compare 
36400 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 63 6f 6e  against */.  con
36401 73 74 20 55 43 68 61 72 33 32 20 75 45 73 63 20  st UChar32 uEsc 
36402 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
36403 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20  scape character 
36404 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63  */.){.  static c
36405 6f 6e 73 74 20 69 6e 74 20 4d 41 54 43 48 5f 4f  onst int MATCH_O
36406 4e 45 20 3d 20 28 55 43 68 61 72 33 32 29 27 5f  NE = (UChar32)'_
36407 27 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ';.  static cons
36408 74 20 69 6e 74 20 4d 41 54 43 48 5f 41 4c 4c 20  t int MATCH_ALL 
36409 3d 20 28 55 43 68 61 72 33 32 29 27 25 27 3b 0a  = (UChar32)'%';.
3640a 0a 20 20 69 6e 74 20 69 50 61 74 74 65 72 6e 20  .  int iPattern 
3640b 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 43 75  = 0;       /* Cu
3640c 72 72 65 6e 74 20 62 79 74 65 20 69 6e 64 65 78  rrent byte index
3640d 20 69 6e 20 7a 50 61 74 74 65 72 6e 20 2a 2f 0a   in zPattern */.
3640e 20 20 69 6e 74 20 69 53 74 72 69 6e 67 20 3d 20    int iString = 
3640f 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  0;        /* Cur
36410 72 65 6e 74 20 62 79 74 65 20 69 6e 64 65 78 20  rent byte index 
36411 69 6e 20 7a 53 74 72 69 6e 67 20 2a 2f 0a 0a 20  in zString */.. 
36412 20 69 6e 74 20 70 72 65 76 45 73 63 61 70 65 20   int prevEscape 
36413 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  = 0;     /* True
36414 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
36415 20 63 68 61 72 61 63 74 65 72 20 77 61 73 20 75   character was u
36416 45 73 63 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28  Esc */..  while(
36417 20 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65   zPattern[iPatte
36418 72 6e 5d 21 3d 30 20 29 7b 0a 0a 20 20 20 20 2f  rn]!=0 ){..    /
36419 2a 20 52 65 61 64 20 28 61 6e 64 20 63 6f 6e 73  * Read (and cons
3641a 75 6d 65 29 20 74 68 65 20 6e 65 78 74 20 63 68  ume) the next ch
3641b 61 72 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65  aracter from the
3641c 20 69 6e 70 75 74 20 70 61 74 74 65 72 6e 2e 20   input pattern. 
3641d 2a 2f 0a 20 20 20 20 55 43 68 61 72 33 32 20 75  */.    UChar32 u
3641e 50 61 74 74 65 72 6e 3b 0a 20 20 20 20 55 38 5f  Pattern;.    U8_
3641f 4e 45 58 54 5f 55 4e 53 41 46 45 28 7a 50 61 74  NEXT_UNSAFE(zPat
36420 74 65 72 6e 2c 20 69 50 61 74 74 65 72 6e 2c 20  tern, iPattern, 
36421 75 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 61  uPattern);.    a
36422 73 73 65 72 74 28 75 50 61 74 74 65 72 6e 21 3d  ssert(uPattern!=
36423 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 72  0);..    /* Ther
36424 65 20 61 72 65 20 6e 6f 77 20 34 20 70 6f 73 73  e are now 4 poss
36425 69 62 69 6c 69 74 69 65 73 3a 0a 20 20 20 20 2a  ibilities:.    *
36426 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 31 2e 20  *.    **     1. 
36427 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75  uPattern is an u
36428 6e 65 73 63 61 70 65 64 20 6d 61 74 63 68 2d 61  nescaped match-a
36429 6c 6c 20 63 68 61 72 61 63 74 65 72 20 22 25 22  ll character "%"
3642a 2c 0a 20 20 20 20 2a 2a 20 20 20 20 20 32 2e 20  ,.    **     2. 
3642b 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75  uPattern is an u
3642c 6e 65 73 63 61 70 65 64 20 6d 61 74 63 68 2d 6f  nescaped match-o
3642d 6e 65 20 63 68 61 72 61 63 74 65 72 20 22 5f 22  ne character "_"
3642e 2c 0a 20 20 20 20 2a 2a 20 20 20 20 20 33 2e 20  ,.    **     3. 
3642f 75 50 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75  uPattern is an u
36430 6e 65 73 63 61 70 65 64 20 65 73 63 61 70 65 20  nescaped escape 
36431 63 68 61 72 61 63 74 65 72 2c 20 6f 72 0a 20 20  character, or.  
36432 20 20 2a 2a 20 20 20 20 20 34 2e 20 75 50 61 74    **     4. uPat
36433 74 65 72 6e 20 69 73 20 74 6f 20 62 65 20 68 61  tern is to be ha
36434 6e 64 6c 65 64 20 61 73 20 61 6e 20 6f 72 64 69  ndled as an ordi
36435 6e 61 72 79 20 63 68 61 72 61 63 74 65 72 0a 20  nary character. 
36436 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70     */.    if( !p
36437 72 65 76 45 73 63 61 70 65 20 26 26 20 75 50 61  revEscape && uPa
36438 74 74 65 72 6e 3d 3d 4d 41 54 43 48 5f 41 4c 4c  ttern==MATCH_ALL
36439 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73   ){.      /* Cas
3643a 65 20 31 2e 20 2a 2f 0a 20 20 20 20 20 20 75 69  e 1. */.      ui
3643b 6e 74 38 5f 74 20 63 3b 0a 0a 20 20 20 20 20 20  nt8_t c;..      
3643c 2f 2a 20 53 6b 69 70 20 61 6e 79 20 4d 41 54 43  /* Skip any MATC
3643d 48 5f 41 4c 4c 20 6f 72 20 4d 41 54 43 48 5f 4f  H_ALL or MATCH_O
3643e 4e 45 20 63 68 61 72 61 63 74 65 72 73 20 74 68  NE characters th
3643f 61 74 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 20 20  at follow a.    
36440 20 20 2a 2a 20 4d 41 54 43 48 5f 41 4c 4c 2e 20    ** MATCH_ALL. 
36441 46 6f 72 20 65 61 63 68 20 4d 41 54 43 48 5f 4f  For each MATCH_O
36442 4e 45 2c 20 73 6b 69 70 20 6f 6e 65 20 63 68 61  NE, skip one cha
36443 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 0a 20  racter in the . 
36444 20 20 20 20 20 2a 2a 20 74 65 73 74 20 73 74 72       ** test str
36445 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ing..      */.  
36446 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 50      while( (c=zP
36447 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d  attern[iPattern]
36448 29 20 3d 3d 20 4d 41 54 43 48 5f 41 4c 4c 20 7c  ) == MATCH_ALL |
36449 7c 20 63 20 3d 3d 20 4d 41 54 43 48 5f 4f 4e 45  | c == MATCH_ONE
3644a 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
3644b 63 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a  c==MATCH_ONE ){.
3644c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 53            if( zS
3644d 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d  tring[iString]==
3644e 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
3644f 20 20 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31          U8_FWD_1
36450 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c  _UNSAFE(zString,
36451 20 69 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20   iString);.     
36452 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 50 61     }.        iPa
36453 74 74 65 72 6e 2b 2b 3b 0a 20 20 20 20 20 20 7d  ttern++;.      }
36454 0a 0a 20 20 20 20 20 20 69 66 28 20 7a 50 61 74  ..      if( zPat
36455 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 3d 3d  tern[iPattern]==
36456 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20  0 ) return 1;.. 
36457 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53 74 72       while( zStr
36458 69 6e 67 5b 69 53 74 72 69 6e 67 5d 20 29 7b 0a  ing[iString] ){.
36459 20 20 20 20 20 20 20 20 69 66 28 20 69 63 75 4c          if( icuL
3645a 69 6b 65 43 6f 6d 70 61 72 65 28 26 7a 50 61 74  ikeCompare(&zPat
3645b 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 2c 20  tern[iPattern], 
3645c 26 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67  &zString[iString
3645d 5d 2c 20 75 45 73 63 29 20 29 7b 0a 20 20 20 20  ], uEsc) ){.    
3645e 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
3645f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
36460 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46    U8_FWD_1_UNSAF
36461 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69  E(zString, iStri
36462 6e 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ng);.      }.   
36463 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20     return 0;..  
36464 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65    }else if( !pre
36465 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74  vEscape && uPatt
36466 65 72 6e 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29  ern==MATCH_ONE )
36467 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
36468 32 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  2. */.      if( 
36469 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d  zString[iString]
3646a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
3646b 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55        U8_FWD_1_U
3646c 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69  NSAFE(zString, i
3646d 53 74 72 69 6e 67 29 3b 0a 0a 20 20 20 20 7d 65  String);..    }e
3646e 6c 73 65 20 69 66 28 20 21 70 72 65 76 45 73 63  lse if( !prevEsc
3646f 61 70 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d  ape && uPattern=
36470 3d 75 45 73 63 29 7b 0a 20 20 20 20 20 20 2f 2a  =uEsc){.      /*
36471 20 43 61 73 65 20 33 2e 20 2a 2f 0a 20 20 20 20   Case 3. */.    
36472 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 31    prevEscape = 1
36473 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ;..    }else{.  
36474 20 20 20 20 2f 2a 20 43 61 73 65 20 34 2e 20 2a      /* Case 4. *
36475 2f 0a 20 20 20 20 20 20 55 43 68 61 72 33 32 20  /.      UChar32 
36476 75 53 74 72 69 6e 67 3b 0a 20 20 20 20 20 20 55  uString;.      U
36477 38 5f 4e 45 58 54 5f 55 4e 53 41 46 45 28 7a 53  8_NEXT_UNSAFE(zS
36478 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 2c 20  tring, iString, 
36479 75 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20  uString);.      
3647a 75 53 74 72 69 6e 67 20 3d 20 75 5f 66 6f 6c 64  uString = u_fold
3647b 43 61 73 65 28 75 53 74 72 69 6e 67 2c 20 55 5f  Case(uString, U_
3647c 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c  FOLD_CASE_DEFAUL
3647d 54 29 3b 0a 20 20 20 20 20 20 75 50 61 74 74 65  T);.      uPatte
3647e 72 6e 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28  rn = u_foldCase(
3647f 75 50 61 74 74 65 72 6e 2c 20 55 5f 46 4f 4c 44  uPattern, U_FOLD
36480 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a  _CASE_DEFAULT);.
36481 20 20 20 20 20 20 69 66 28 20 75 53 74 72 69 6e        if( uStrin
36482 67 21 3d 75 50 61 74 74 65 72 6e 20 29 7b 0a 20  g!=uPattern ){. 
36483 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
36484 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
36485 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 0a 20  revEscape = 0;. 
36486 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
36487 72 6e 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69  rn zString[iStri
36488 6e 67 5d 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ng]==0;.}../*.**
36489 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
3648a 6f 66 20 74 68 65 20 6c 69 6b 65 28 29 20 53 51  of the like() SQ
3648b 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69  L function.  Thi
3648c 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  s function imple
3648d 6d 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75 69  ments.** the bui
3648e 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61  ld-in LIKE opera
3648f 74 6f 72 2e 20 20 54 68 65 20 66 69 72 73 74 20  tor.  The first 
36490 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
36491 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 0a  function is the.
36492 2a 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 20 74  ** pattern and t
36493 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
36494 6e 74 20 69 73 20 74 68 65 20 73 74 72 69 6e 67  nt is the string
36495 2e 20 20 53 6f 2c 20 74 68 65 20 53 51 4c 20 73  .  So, the SQL s
36496 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a  tatements:.**.**
36497 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42 0a         A LIKE B.
36498 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65  **.** is impleme
36499 6e 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c 20  nted as like(B, 
3649a 41 29 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  A). If there is 
3649b 61 6e 20 65 73 63 61 70 65 20 63 68 61 72 61 63  an escape charac
3649c 74 65 72 20 45 2c 20 0a 2a 2a 0a 2a 2a 20 20 20  ter E, .**.**   
3649d 20 20 20 20 41 20 4c 49 4b 45 20 42 20 45 53 43      A LIKE B ESC
3649e 41 50 45 20 45 0a 2a 2a 0a 2a 2a 20 69 73 20 6d  APE E.**.** is m
3649f 61 70 70 65 64 20 74 6f 20 6c 69 6b 65 28 42 2c  apped to like(B,
364a0 20 41 2c 20 45 29 2e 0a 2a 2f 0a 73 74 61 74 69   A, E)..*/.stati
364a1 63 20 76 6f 69 64 20 69 63 75 4c 69 6b 65 46 75  c void icuLikeFu
364a2 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
364a3 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
364a4 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20  .  int argc, .  
364a5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
364a6 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
364a7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
364a8 41 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  A = sqlite3_valu
364a9 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
364aa 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
364ab 64 20 63 68 61 72 20 2a 7a 42 20 3d 20 73 71 6c  d char *zB = sql
364ac 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
364ad 61 72 67 76 5b 31 5d 29 3b 0a 20 20 55 43 68 61  argv[1]);.  UCha
364ae 72 33 32 20 75 45 73 63 20 3d 20 30 3b 0a 0a 20  r32 uEsc = 0;.. 
364af 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c 65   /* Limit the le
364b0 6e 67 74 68 20 6f 66 20 74 68 65 20 4c 49 4b 45  ngth of the LIKE
364b1 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e   or GLOB pattern
364b2 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65   to avoid proble
364b3 6d 73 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20  ms.  ** of deep 
364b4 72 65 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a  recursion and N*
364b5 4e 20 62 65 68 61 76 69 6f 72 20 69 6e 20 70 61  N behavior in pa
364b6 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a  tternCompare()..
364b7 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
364b8 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
364b9 72 67 76 5b 30 5d 29 3e 53 51 4c 49 54 45 5f 4d  rgv[0])>SQLITE_M
364ba 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
364bb 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71  LENGTH ){.    sq
364bc 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
364bd 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b  or(context, "LIK
364be 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72  E or GLOB patter
364bf 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20  n too complex", 
364c0 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  -1);.    return;
364c1 0a 20 20 7d 0a 0a 0a 20 20 69 66 28 20 61 72 67  .  }...  if( arg
364c2 63 3d 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54  c==3 ){.    /* T
364c3 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63  he escape charac
364c4 74 65 72 20 73 74 72 69 6e 67 20 6d 75 73 74 20  ter string must 
364c5 63 6f 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e  consist of a sin
364c6 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61 63  gle UTF-8 charac
364c7 74 65 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65  ter..    ** Othe
364c8 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e  rwise, return an
364c9 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20   error..    */. 
364ca 20 20 20 69 6e 74 20 6e 45 3d 20 73 71 6c 69 74     int nE= sqlit
364cb 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
364cc 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 63 6f 6e  rgv[2]);.    con
364cd 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
364ce 20 2a 7a 45 20 3d 20 73 71 6c 69 74 65 33 5f 76   *zE = sqlite3_v
364cf 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32  alue_text(argv[2
364d0 5d 29 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20  ]);.    int i = 
364d1 30 3b 0a 20 20 20 20 69 66 28 20 7a 45 3d 3d 30  0;.    if( zE==0
364d2 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 55   ) return;.    U
364d3 38 5f 4e 45 58 54 28 7a 45 2c 20 69 2c 20 6e 45  8_NEXT(zE, i, nE
364d4 2c 20 75 45 73 63 29 3b 0a 20 20 20 20 69 66 28  , uEsc);.    if(
364d5 20 69 21 3d 6e 45 29 7b 0a 20 20 20 20 20 20 73   i!=nE){.      s
364d6 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
364d7 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20  ror(context, .  
364d8 20 20 20 20 20 20 20 20 22 45 53 43 41 50 45 20          "ESCAPE 
364d9 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20  expression must 
364da 62 65 20 61 20 73 69 6e 67 6c 65 20 63 68 61 72  be a single char
364db 61 63 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20  acter", -1);.   
364dc 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
364dd 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 41 20 26  .  }..  if( zA &
364de 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 71 6c 69  & zB ){.    sqli
364df 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
364e0 6f 6e 74 65 78 74 2c 20 69 63 75 4c 69 6b 65 43  ontext, icuLikeC
364e1 6f 6d 70 61 72 65 28 7a 41 2c 20 7a 42 2c 20 75  ompare(zA, zB, u
364e2 45 73 63 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  Esc));.  }.}../*
364e3 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
364e4 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
364e5 20 61 6e 20 49 43 55 20 66 75 6e 63 74 69 6f 6e   an ICU function
364e6 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
364e7 68 69 6e 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65  hin.** the imple
364e8 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20  mentation of an 
364e9 53 51 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74  SQL scalar funct
364ea 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65  ion returns an e
364eb 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rror..**.** The 
364ec 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
364ed 63 6f 6e 74 65 78 74 20 70 61 73 73 65 64 20 61  context passed a
364ee 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
364ef 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 6c 6f 61 64  ment is .** load
364f0 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ed with an error
364f1 20 6d 65 73 73 61 67 65 20 62 61 73 65 64 20 6f   message based o
364f2 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
364f3 74 77 6f 20 61 72 67 73 2e 0a 2a 2f 0a 73 74 61  two args..*/.sta
364f4 74 69 63 20 76 6f 69 64 20 69 63 75 46 75 6e 63  tic void icuFunc
364f5 74 69 6f 6e 45 72 72 6f 72 28 0a 20 20 73 71 6c  tionError(.  sql
364f6 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
364f7 74 78 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  tx,       /* SQL
364f8 69 74 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74  ite scalar funct
364f9 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ion context */. 
364fa 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
364fb 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  me,           /*
364fc 20 4e 61 6d 65 20 6f 66 20 49 43 55 20 66 75 6e   Name of ICU fun
364fd 63 74 69 6f 6e 20 74 68 61 74 20 66 61 69 6c 65  ction that faile
364fe 64 20 2a 2f 0a 20 20 55 45 72 72 6f 72 43 6f 64  d */.  UErrorCod
364ff 65 20 65 20 20 20 20 20 20 20 20 20 20 20 20 20  e e             
36500 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64      /* Error cod
36501 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 49 43  e returned by IC
36502 55 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  U function */.){
36503 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 32 38  .  char zBuf[128
36504 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  ];.  sqlite3_snp
36505 72 69 6e 74 66 28 31 32 38 2c 20 7a 42 75 66 2c  rintf(128, zBuf,
36506 20 22 49 43 55 20 65 72 72 6f 72 3a 20 25 73 28   "ICU error: %s(
36507 29 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 2c 20 75  ): %s", zName, u
36508 5f 65 72 72 6f 72 4e 61 6d 65 28 65 29 29 3b 0a  _errorName(e));.
36509 20 20 7a 42 75 66 5b 31 32 37 5d 20 3d 20 27 5c    zBuf[127] = '\
3650a 30 27 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  0';.  sqlite3_re
3650b 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 74 78 2c  sult_error(pCtx,
3650c 20 7a 42 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f   zBuf, -1);.}../
3650d 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f  *.** Function to
3650e 20 64 65 6c 65 74 65 20 63 6f 6d 70 69 6c 65 64   delete compiled
3650f 20 72 65 67 65 78 70 20 6f 62 6a 65 63 74 73 2e   regexp objects.
36510 20 52 65 67 69 73 74 65 72 65 64 20 61 73 0a 2a   Registered as.*
36511 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66  * a destructor f
36512 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 71 6c  unction with sql
36513 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
36514 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
36515 69 64 20 69 63 75 52 65 67 65 78 70 44 65 6c 65  id icuRegexpDele
36516 74 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 55  te(void *p){.  U
36517 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f  RegularExpressio
36518 6e 20 2a 70 45 78 70 72 20 3d 20 28 55 52 65 67  n *pExpr = (UReg
36519 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e 20 2a  ularExpression *
3651a 29 70 3b 0a 20 20 75 72 65 67 65 78 5f 63 6c 6f  )p;.  uregex_clo
3651b 73 65 28 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a  se(pExpr);.}../*
3651c 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
3651d 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 52 45 47  on of SQLite REG
3651e 45 58 50 20 6f 70 65 72 61 74 6f 72 2e 20 54 68  EXP operator. Th
3651f 69 73 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69  is scalar functi
36520 6f 6e 20 74 61 6b 65 73 0a 2a 2a 20 74 77 6f 20  on takes.** two 
36521 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 66  arguments. The f
36522 69 72 73 74 20 69 73 20 61 20 72 65 67 75 6c 61  irst is a regula
36523 72 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 74  r expression pat
36524 74 65 72 6e 20 74 6f 20 63 6f 6d 70 69 6c 65 0a  tern to compile.
36525 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73  ** the second is
36526 20 61 20 73 74 72 69 6e 67 20 74 6f 20 6d 61 74   a string to mat
36527 63 68 20 61 67 61 69 6e 73 74 20 74 68 61 74 20  ch against that 
36528 70 61 74 74 65 72 6e 2e 20 49 66 20 65 69 74 68  pattern. If eith
36529 65 72 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  er .** argument 
3652a 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c 20  is an SQL NULL, 
3652b 74 68 65 6e 20 4e 55 4c 4c 20 49 73 20 72 65 74  then NULL Is ret
3652c 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
3652d 2c 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  , the result.** 
3652e 69 73 20 31 20 69 66 20 74 68 65 20 73 74 72 69  is 1 if the stri
3652f 6e 67 20 6d 61 74 63 68 65 73 20 74 68 65 20 70  ng matches the p
36530 61 74 74 65 72 6e 2c 20 6f 72 20 30 20 6f 74 68  attern, or 0 oth
36531 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 51  erwise..**.** SQ
36532 4c 69 74 65 20 6d 61 70 73 20 74 68 65 20 72 65  Lite maps the re
36533 67 65 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20  gexp() function 
36534 74 6f 20 74 68 65 20 72 65 67 65 78 70 28 29 20  to the regexp() 
36535 6f 70 65 72 61 74 6f 72 20 73 75 63 68 0a 2a 2a  operator such.**
36536 20 74 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77   that the follow
36537 69 6e 67 20 74 77 6f 20 61 72 65 20 65 71 75 69  ing two are equi
36538 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  valent:.**.**   
36539 20 20 7a 53 74 72 69 6e 67 20 52 45 47 45 58 50    zString REGEXP
3653a 20 7a 50 61 74 74 65 72 6e 0a 2a 2a 20 20 20 20   zPattern.**    
3653b 20 72 65 67 65 78 70 28 7a 50 61 74 74 65 72 6e   regexp(zPattern
3653c 2c 20 7a 53 74 72 69 6e 67 29 0a 2a 2a 0a 2a 2a  , zString).**.**
3653d 20 55 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77   Uses the follow
3653e 69 6e 67 20 49 43 55 20 72 65 67 65 78 70 20 41  ing ICU regexp A
3653f 50 49 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75  PIs:.**.**     u
36540 72 65 67 65 78 5f 6f 70 65 6e 28 29 0a 2a 2a 20  regex_open().** 
36541 20 20 20 20 75 72 65 67 65 78 5f 6d 61 74 63 68      uregex_match
36542 65 73 28 29 0a 2a 2a 20 20 20 20 20 75 72 65 67  es().**     ureg
36543 65 78 5f 63 6c 6f 73 65 28 29 0a 2a 2f 0a 73 74  ex_close().*/.st
36544 61 74 69 63 20 76 6f 69 64 20 69 63 75 52 65 67  atic void icuReg
36545 65 78 70 46 75 6e 63 28 73 71 6c 69 74 65 33 5f  expFunc(sqlite3_
36546 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20  context *p, int 
36547 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  nArg, sqlite3_va
36548 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20  lue **apArg){.  
36549 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75  UErrorCode statu
3654a 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52  s = U_ZERO_ERROR
3654b 3b 0a 20 20 55 52 65 67 75 6c 61 72 45 78 70 72  ;.  URegularExpr
3654c 65 73 73 69 6f 6e 20 2a 70 45 78 70 72 3b 0a 20  ession *pExpr;. 
3654d 20 55 42 6f 6f 6c 20 72 65 73 3b 0a 20 20 63 6f   UBool res;.  co
3654e 6e 73 74 20 55 43 68 61 72 20 2a 7a 53 74 72 69  nst UChar *zStri
3654f 6e 67 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ng = sqlite3_val
36550 75 65 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b  ue_text16(apArg[
36551 31 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  1]);..  /* If th
36552 65 20 6c 65 66 74 20 68 61 6e 64 20 73 69 64 65  e left hand side
36553 20 6f 66 20 74 68 65 20 72 65 67 65 78 70 20 6f   of the regexp o
36554 70 65 72 61 74 6f 72 20 69 73 20 4e 55 4c 4c 2c  perator is NULL,
36555 20 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20   .  ** then the 
36556 72 65 73 75 6c 74 20 69 73 20 61 6c 73 6f 20 4e  result is also N
36557 55 4c 4c 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  ULL. .  */.  if(
36558 20 21 7a 53 74 72 69 6e 67 20 29 7b 0a 20 20 20   !zString ){.   
36559 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
3655a 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 5f  pExpr = sqlite3_
3655b 67 65 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30  get_auxdata(p, 0
3655c 29 3b 0a 20 20 69 66 28 20 21 70 45 78 70 72 20  );.  if( !pExpr 
3655d 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 55 43 68  ){.    const UCh
3655e 61 72 20 2a 7a 50 61 74 74 65 72 6e 20 3d 20 73  ar *zPattern = s
3655f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
36560 74 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20  t16(apArg[0]);. 
36561 20 20 20 69 66 28 20 21 7a 50 61 74 74 65 72 6e     if( !zPattern
36562 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
36563 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70  ;.    }.    pExp
36564 72 20 3d 20 75 72 65 67 65 78 5f 6f 70 65 6e 28  r = uregex_open(
36565 7a 50 61 74 74 65 72 6e 2c 20 2d 31 2c 20 30 2c  zPattern, -1, 0,
36566 20 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 0a 20   0, &status);.. 
36567 20 20 20 69 66 28 20 55 5f 53 55 43 43 45 53 53     if( U_SUCCESS
36568 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20  (status) ){.    
36569 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75    sqlite3_set_au
3656a 78 64 61 74 61 28 70 2c 20 30 2c 20 70 45 78 70  xdata(p, 0, pExp
3656b 72 2c 20 69 63 75 52 65 67 65 78 70 44 65 6c 65  r, icuRegexpDele
3656c 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  te);.    }else{.
3656d 20 20 20 20 20 20 61 73 73 65 72 74 28 21 70 45        assert(!pE
3656e 78 70 72 29 3b 0a 20 20 20 20 20 20 69 63 75 46  xpr);.      icuF
3656f 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20  unctionError(p, 
36570 22 75 72 65 67 65 78 5f 6f 70 65 6e 22 2c 20 73  "uregex_open", s
36571 74 61 74 75 73 29 3b 0a 20 20 20 20 20 20 72 65  tatus);.      re
36572 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
36573 0a 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20  .  /* Configure 
36574 74 68 65 20 74 65 78 74 20 74 68 61 74 20 74 68  the text that th
36575 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73  e regular expres
36576 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e  sion operates on
36577 2e 20 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73 65  . */.  uregex_se
36578 74 54 65 78 74 28 70 45 78 70 72 2c 20 7a 53 74  tText(pExpr, zSt
36579 72 69 6e 67 2c 20 2d 31 2c 20 26 73 74 61 74 75  ring, -1, &statu
3657a 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43  s);.  if( !U_SUC
3657b 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
3657c 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45      icuFunctionE
3657d 72 72 6f 72 28 70 2c 20 22 75 72 65 67 65 78 5f  rror(p, "uregex_
3657e 73 65 74 54 65 78 74 22 2c 20 73 74 61 74 75 73  setText", status
3657f 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
36580 20 7d 0a 0a 20 20 2f 2a 20 41 74 74 65 6d 70 74   }..  /* Attempt
36581 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20   the match */.  
36582 72 65 73 20 3d 20 75 72 65 67 65 78 5f 6d 61 74  res = uregex_mat
36583 63 68 65 73 28 70 45 78 70 72 2c 20 30 2c 20 26  ches(pExpr, 0, &
36584 73 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21  status);.  if( !
36585 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73  U_SUCCESS(status
36586 29 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63  ) ){.    icuFunc
36587 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72  tionError(p, "ur
36588 65 67 65 78 5f 6d 61 74 63 68 65 73 22 2c 20 73  egex_matches", s
36589 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75  tatus);.    retu
3658a 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  rn;.  }..  /* Se
3658b 74 20 74 68 65 20 74 65 78 74 20 74 68 61 74 20  t the text that 
3658c 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72  the regular expr
3658d 65 73 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20  ession operates 
3658e 6f 6e 20 74 6f 20 61 20 4e 55 4c 4c 0a 20 20 2a  on to a NULL.  *
3658f 2a 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20  * pointer. This 
36590 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6e 65  is not really ne
36591 63 65 73 73 61 72 79 2c 20 62 75 74 20 69 74 20  cessary, but it 
36592 69 73 20 74 69 64 69 65 72 20 74 68 61 6e 20 0a  is tidier than .
36593 20 20 2a 2a 20 6c 65 61 76 69 6e 67 20 74 68 65    ** leaving the
36594 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73   regular express
36595 69 6f 6e 20 6f 62 6a 65 63 74 20 63 6f 6e 66 69  ion object confi
36596 67 75 72 65 64 20 77 69 74 68 20 61 6e 20 69 6e  gured with an in
36597 76 61 6c 69 64 0a 20 20 2a 2a 20 70 6f 69 6e 74  valid.  ** point
36598 65 72 20 61 66 74 65 72 20 74 68 69 73 20 66 75  er after this fu
36599 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a  nction returns..
3659a 20 20 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73 65    */.  uregex_se
3659b 74 54 65 78 74 28 70 45 78 70 72 2c 20 30 2c 20  tText(pExpr, 0, 
3659c 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 0a 20 20  0, &status);..  
3659d 2f 2a 20 52 65 74 75 72 6e 20 31 20 6f 72 20 30  /* Return 1 or 0
3659e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72  . */.  sqlite3_r
3659f 65 73 75 6c 74 5f 69 6e 74 28 70 2c 20 72 65 73  esult_int(p, res
365a0 20 3f 20 31 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a   ? 1 : 0);.}../*
365a1 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
365a2 6f 6e 73 20 6f 66 20 73 63 61 6c 61 72 20 66 75  ons of scalar fu
365a3 6e 63 74 69 6f 6e 73 20 66 6f 72 20 63 61 73 65  nctions for case
365a4 20 6d 61 70 70 69 6e 67 20 2d 20 75 70 70 65 72   mapping - upper
365a5 28 29 20 61 6e 64 20 0a 2a 2a 20 6c 6f 77 65 72  () and .** lower
365a6 28 29 2e 20 46 75 6e 63 74 69 6f 6e 20 75 70 70  (). Function upp
365a7 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 69 74  er() converts it
365a8 73 20 69 6e 70 75 74 20 74 6f 20 75 70 70 65 72  s input to upper
365a9 2d 63 61 73 65 20 28 41 42 43 29 2e 0a 2a 2a 20  -case (ABC)..** 
365aa 46 75 6e 63 74 69 6f 6e 20 6c 6f 77 65 72 28 29  Function lower()
365ab 20 63 6f 6e 76 65 72 74 73 20 74 6f 20 6c 6f 77   converts to low
365ac 65 72 2d 63 61 73 65 20 28 61 62 63 29 2e 0a 2a  er-case (abc)..*
365ad 2a 0a 2a 2a 20 49 43 55 20 70 72 6f 76 69 64 65  *.** ICU provide
365ae 73 20 74 77 6f 20 74 79 70 65 73 20 6f 66 20 63  s two types of c
365af 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 22 67 65  ase mapping, "ge
365b0 6e 65 72 61 6c 22 20 63 61 73 65 20 6d 61 70 70  neral" case mapp
365b1 69 6e 67 20 61 6e 64 0a 2a 2a 20 22 6c 61 6e 67  ing and.** "lang
365b2 75 61 67 65 20 73 70 65 63 69 66 69 63 22 2e 20  uage specific". 
365b3 52 65 66 65 72 20 74 6f 20 49 43 55 20 64 6f 63  Refer to ICU doc
365b4 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74  umentation for t
365b5 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 0a 2a  he differences.*
365b6 2a 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77  * between the tw
365b7 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 74 69 6c  o..**.** To util
365b8 69 73 65 20 22 67 65 6e 65 72 61 6c 22 20 63 61  ise "general" ca
365b9 73 65 20 6d 61 70 70 69 6e 67 2c 20 74 68 65 20  se mapping, the 
365ba 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72  upper() or lower
365bb 28 29 20 73 63 61 6c 61 72 20 0a 2a 2a 20 66 75  () scalar .** fu
365bc 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f  nctions are invo
365bd 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 61 72 67  ked with one arg
365be 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ument:.**.**    
365bf 20 75 70 70 65 72 28 27 41 42 43 27 29 20 2d 3e   upper('ABC') ->
365c0 20 27 61 62 63 27 0a 2a 2a 20 20 20 20 20 6c 6f   'abc'.**     lo
365c1 77 65 72 28 27 61 62 63 27 29 20 2d 3e 20 27 41  wer('abc') -> 'A
365c2 42 43 27 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 63 63  BC'.**.** To acc
365c3 65 73 73 20 49 43 55 20 22 6c 61 6e 67 75 61 67  ess ICU "languag
365c4 65 20 73 70 65 63 69 66 69 63 22 20 63 61 73 65  e specific" case
365c5 20 6d 61 70 70 69 6e 67 2c 20 75 70 70 65 72 28   mapping, upper(
365c6 29 20 6f 72 20 6c 6f 77 65 72 28 29 0a 2a 2a 20  ) or lower().** 
365c7 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65  should be invoke
365c8 64 20 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d  d with two argum
365c9 65 6e 74 73 2e 20 54 68 65 20 73 65 63 6f 6e 64  ents. The second
365ca 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
365cb 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 74 68 65 20   name.** of the 
365cc 6c 6f 63 61 6c 65 20 74 6f 20 75 73 65 2e 20 50  locale to use. P
365cd 61 73 73 69 6e 67 20 61 6e 20 65 6d 70 74 79 20  assing an empty 
365ce 73 74 72 69 6e 67 20 28 22 22 29 20 6f 72 20 53  string ("") or S
365cf 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 2a 2a  QL NULL value.**
365d0 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
365d1 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73  rgument is the s
365d2 61 6d 65 20 61 73 20 69 6e 76 6f 6b 69 6e 67 20  ame as invoking 
365d3 74 68 65 20 31 20 61 72 67 75 6d 65 6e 74 20 76  the 1 argument v
365d4 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 75 70 70  ersion.** of upp
365d5 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29 2e  er() or lower().
365d6 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72  .**.**     lower
365d7 28 27 49 27 2c 20 27 65 6e 5f 75 73 27 29 20 2d  ('I', 'en_us') -
365d8 3e 20 27 69 27 0a 2a 2a 20 20 20 20 20 6c 6f 77  > 'i'.**     low
365d9 65 72 28 27 49 27 2c 20 27 74 72 5f 74 72 27 29  er('I', 'tr_tr')
365da 20 2d 3e 20 27 c4 b1 27 20 28 73 6d 61 6c 6c 20   -> '..' (small 
365db 64 6f 74 6c 65 73 73 20 69 29 0a 2a 2a 0a 2a 2a  dotless i).**.**
365dc 20 68 74 74 70 3a 2f 2f 77 77 77 2e 69 63 75 2d   http://www.icu-
365dd 70 72 6f 6a 65 63 74 2e 6f 72 67 2f 75 73 65 72  project.org/user
365de 67 75 69 64 65 2f 70 6f 73 69 78 2e 68 74 6d 6c  guide/posix.html
365df 23 63 61 73 65 5f 6d 61 70 70 69 6e 67 73 0a 2a  #case_mappings.*
365e0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
365e1 75 43 61 73 65 46 75 6e 63 31 36 28 73 71 6c 69  uCaseFunc16(sqli
365e2 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20  te3_context *p, 
365e3 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
365e4 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
365e5 7b 0a 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20  {.  const UChar 
365e6 2a 7a 49 6e 70 75 74 3b 0a 20 20 55 43 68 61 72  *zInput;.  UChar
365e7 20 2a 7a 4f 75 74 70 75 74 3b 0a 20 20 69 6e 74   *zOutput;.  int
365e8 20 6e 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e   nInput;.  int n
365e9 4f 75 74 70 75 74 3b 0a 0a 20 20 55 45 72 72 6f  Output;..  UErro
365ea 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55  rCode status = U
365eb 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 63  _ZERO_ERROR;.  c
365ec 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 61  onst char *zLoca
365ed 6c 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  le = 0;..  asser
365ee 74 28 6e 41 72 67 3d 3d 31 20 7c 7c 20 6e 41 72  t(nArg==1 || nAr
365ef 67 3d 3d 32 29 3b 0a 20 20 69 66 28 20 6e 41 72  g==2);.  if( nAr
365f0 67 3d 3d 32 20 29 7b 0a 20 20 20 20 7a 4c 6f 63  g==2 ){.    zLoc
365f1 61 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ale = (const cha
365f2 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
365f3 65 5f 74 65 78 74 28 61 70 41 72 67 5b 31 5d 29  e_text(apArg[1])
365f4 3b 0a 20 20 7d 0a 0a 20 20 7a 49 6e 70 75 74 20  ;.  }..  zInput 
365f5 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
365f6 74 65 78 74 31 36 28 61 70 41 72 67 5b 30 5d 29  text16(apArg[0])
365f7 3b 0a 20 20 69 66 28 20 21 7a 49 6e 70 75 74 20  ;.  if( !zInput 
365f8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
365f9 20 7d 0a 20 20 6e 49 6e 70 75 74 20 3d 20 73 71   }.  nInput = sq
365fa 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
365fb 73 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 0a  s16(apArg[0]);..
365fc 20 20 6e 4f 75 74 70 75 74 20 3d 20 6e 49 6e 70    nOutput = nInp
365fd 75 74 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7a 4f  ut * 2 + 2;.  zO
365fe 75 74 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f  utput = sqlite3_
365ff 6d 61 6c 6c 6f 63 28 6e 4f 75 74 70 75 74 29 3b  malloc(nOutput);
36600 0a 20 20 69 66 28 20 21 7a 4f 75 74 70 75 74 20  .  if( !zOutput 
36601 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
36602 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65   }..  if( sqlite
36603 33 5f 75 73 65 72 5f 64 61 74 61 28 70 29 20 29  3_user_data(p) )
36604 7b 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 70 70  {.    u_strToUpp
36605 65 72 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74  er(zOutput, nOut
36606 70 75 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e  put/2, zInput, n
36607 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65  Input/2, zLocale
36608 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 7d 65  , &status);.  }e
36609 6c 73 65 7b 0a 20 20 20 20 75 5f 73 74 72 54 6f  lse{.    u_strTo
3660a 4c 6f 77 65 72 28 7a 4f 75 74 70 75 74 2c 20 6e  Lower(zOutput, n
3660b 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75 74  Output/2, zInput
3660c 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63  , nInput/2, zLoc
3660d 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a 20  ale, &status);. 
3660e 20 7d 0a 0a 20 20 69 66 28 20 21 55 5f 53 55 43   }..  if( !U_SUC
3660f 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
36610 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45      icuFunctionE
36611 72 72 6f 72 28 70 2c 20 22 75 5f 73 74 72 54 6f  rror(p, "u_strTo
36612 4c 6f 77 65 72 28 29 2f 75 5f 73 74 72 54 6f 55  Lower()/u_strToU
36613 70 70 65 72 22 2c 20 73 74 61 74 75 73 29 3b 0a  pper", status);.
36614 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
36615 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
36616 74 5f 74 65 78 74 31 36 28 70 2c 20 7a 4f 75 74  t_text16(p, zOut
36617 70 75 74 2c 20 2d 31 2c 20 78 46 72 65 65 29 3b  put, -1, xFree);
36618 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74  .}../*.** Collat
36619 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 64 65 73  ion sequence des
3661a 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e  tructor function
3661b 2e 20 54 68 65 20 70 43 74 78 20 61 72 67 75 6d  . The pCtx argum
3661c 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a  ent points to.**
3661d 20 61 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72   a UCollator str
3661e 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c  ucture previousl
3661f 79 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e  y allocated usin
36620 67 20 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a  g ucol_open()..*
36621 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
36622 75 43 6f 6c 6c 61 74 69 6f 6e 44 65 6c 28 76 6f  uCollationDel(vo
36623 69 64 20 2a 70 43 74 78 29 7b 0a 20 20 55 43 6f  id *pCtx){.  UCo
36624 6c 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55 43 6f  llator *p = (UCo
36625 6c 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a 20  llator *)pCtx;. 
36626 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70 29 3b 0a   ucol_close(p);.
36627 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69  }../*.** Collati
36628 6f 6e 20 73 65 71 75 65 6e 63 65 20 63 6f 6d 70  on sequence comp
36629 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e  arison function.
3662a 20 54 68 65 20 70 43 74 78 20 61 72 67 75 6d 65   The pCtx argume
3662b 6e 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  nt points to.** 
3662c 61 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75  a UCollator stru
3662d 63 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79  cture previously
3662e 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67   allocated using
3662f 20 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f   ucol_open()..*/
36630 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 43  .static int icuC
36631 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c 28 0a 20 20  ollationColl(.  
36632 76 6f 69 64 20 2a 70 43 74 78 2c 0a 20 20 69 6e  void *pCtx,.  in
36633 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74  t nLeft,.  const
36634 20 76 6f 69 64 20 2a 7a 4c 65 66 74 2c 0a 20 20   void *zLeft,.  
36635 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 63 6f  int nRight,.  co
36636 6e 73 74 20 76 6f 69 64 20 2a 7a 52 69 67 68 74  nst void *zRight
36637 0a 29 7b 0a 20 20 55 43 6f 6c 6c 61 74 69 6f 6e  .){.  UCollation
36638 52 65 73 75 6c 74 20 72 65 73 3b 0a 20 20 55 43  Result res;.  UC
36639 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55 43  ollator *p = (UC
3663a 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a  ollator *)pCtx;.
3663b 20 20 72 65 73 20 3d 20 75 63 6f 6c 5f 73 74 72    res = ucol_str
3663c 63 6f 6c 6c 28 70 2c 20 28 55 43 68 61 72 20 2a  coll(p, (UChar *
3663d 29 7a 4c 65 66 74 2c 20 6e 4c 65 66 74 2f 32 2c  )zLeft, nLeft/2,
3663e 20 28 55 43 68 61 72 20 2a 29 7a 52 69 67 68 74   (UChar *)zRight
3663f 2c 20 6e 52 69 67 68 74 2f 32 29 3b 0a 20 20 73  , nRight/2);.  s
36640 77 69 74 63 68 28 20 72 65 73 20 29 7b 0a 20 20  witch( res ){.  
36641 20 20 63 61 73 65 20 55 43 4f 4c 5f 4c 45 53 53    case UCOL_LESS
36642 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a  :    return -1;.
36643 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f 47 52      case UCOL_GR
36644 45 41 54 45 52 3a 20 72 65 74 75 72 6e 20 2b 31  EATER: return +1
36645 3b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f  ;.    case UCOL_
36646 45 51 55 41 4c 3a 20 20 20 72 65 74 75 72 6e 20  EQUAL:   return 
36647 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
36648 21 22 55 6e 65 78 70 65 63 74 65 64 20 72 65 74  !"Unexpected ret
36649 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 75  urn value from u
3664a 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28 29 22 29 3b  col_strcoll()");
3664b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
3664c 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
3664d 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c  tion of the scal
3664e 61 72 20 66 75 6e 63 74 69 6f 6e 20 69 63 75 5f  ar function icu_
3664f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  load_collation()
36650 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 63 61  ..**.** This sca
36651 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  lar function is 
36652 75 73 65 64 20 74 6f 20 61 64 64 20 49 43 55 20  used to add ICU 
36653 63 6f 6c 6c 61 74 69 6f 6e 20 62 61 73 65 64 20  collation based 
36654 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 74 79  collation .** ty
36655 70 65 73 20 74 6f 20 61 6e 20 53 51 4c 69 74 65  pes to an SQLite
36656 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
36657 74 69 6f 6e 2e 20 49 74 20 69 73 20 69 6e 74 65  tion. It is inte
36658 6e 64 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65  nded to be calle
36659 64 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  d.** as follows:
3665a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
3665b 54 20 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61  T icu_load_colla
3665c 74 69 6f 6e 28 3c 6c 6f 63 61 6c 65 3e 2c 20 3c  tion(<locale>, <
3665d 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 29  collation-name>)
3665e 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 3c 6c  ;.**.** Where <l
3665f 6f 63 61 6c 65 3e 20 69 73 20 61 20 73 74 72 69  ocale> is a stri
36660 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e  ng containing an
36661 20 49 43 55 20 6c 6f 63 61 6c 65 20 69 64 65 6e   ICU locale iden
36662 74 69 66 69 65 72 20 28 69 2e 65 2e 0a 2a 2a 20  tifier (i.e..** 
36663 22 65 6e 5f 41 55 22 2c 20 22 74 72 5f 54 52 22  "en_AU", "tr_TR"
36664 20 65 74 63 2e 29 20 61 6e 64 20 3c 63 6f 6c 6c   etc.) and <coll
36665 61 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74  ation-name> is t
36666 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a  he name of the.*
36667 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  * collation sequ
36668 65 6e 63 65 20 74 6f 20 63 72 65 61 74 65 2e 0a  ence to create..
36669 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
3666a 63 75 4c 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e 28  cuLoadCollation(
3666b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
3666c 78 74 20 2a 70 2c 20 0a 20 20 69 6e 74 20 6e 41  xt *p, .  int nA
3666d 72 67 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76  rg, .  sqlite3_v
3666e 61 6c 75 65 20 2a 2a 61 70 41 72 67 0a 29 7b 0a  alue **apArg.){.
3666f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
36670 28 73 71 6c 69 74 65 33 20 2a 29 73 71 6c 69 74  (sqlite3 *)sqlit
36671 65 33 5f 75 73 65 72 5f 64 61 74 61 28 70 29 3b  e3_user_data(p);
36672 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74  .  UErrorCode st
36673 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52  atus = U_ZERO_ER
36674 52 4f 52 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ROR;.  const cha
36675 72 20 2a 7a 4c 6f 63 61 6c 65 3b 20 20 20 20 20  r *zLocale;     
36676 20 2f 2a 20 4c 6f 63 61 6c 65 20 69 64 65 6e 74   /* Locale ident
36677 69 66 69 65 72 20 2d 20 28 65 67 2e 20 22 6a 70  ifier - (eg. "jp
36678 5f 4a 50 22 29 20 2a 2f 0a 20 20 63 6f 6e 73 74  _JP") */.  const
36679 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
3667a 20 20 20 20 20 2f 2a 20 53 51 4c 20 43 6f 6c 6c       /* SQL Coll
3667b 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e  ation sequence n
3667c 61 6d 65 20 28 65 67 2e 20 22 6a 61 70 61 6e 65  ame (eg. "japane
3667d 73 65 22 29 20 2a 2f 0a 20 20 55 43 6f 6c 6c 61  se") */.  UColla
3667e 74 6f 72 20 2a 70 55 43 6f 6c 6c 61 74 6f 72 3b  tor *pUCollator;
3667f 20 20 20 20 2f 2a 20 49 43 55 20 6c 69 62 72 61      /* ICU libra
36680 72 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 6f 62 6a  ry collation obj
36681 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ect */.  int rc;
36682 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36683 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
36684 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63  e from sqlite3_c
36685 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
36686 78 28 29 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  x() */..  assert
36687 28 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 7a 4c 6f  (nArg==2);.  zLo
36688 63 61 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  cale = (const ch
36689 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
3668a 75 65 5f 74 65 78 74 28 61 70 41 72 67 5b 30 5d  ue_text(apArg[0]
3668b 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f  );.  zName = (co
3668c 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
3668d 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70  e3_value_text(ap
3668e 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 69 66 28 20  Arg[1]);..  if( 
3668f 21 7a 4c 6f 63 61 6c 65 20 7c 7c 20 21 7a 4e 61  !zLocale || !zNa
36690 6d 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  me ){.    return
36691 3b 0a 20 20 7d 0a 0a 20 20 70 55 43 6f 6c 6c 61  ;.  }..  pUColla
36692 74 6f 72 20 3d 20 75 63 6f 6c 5f 6f 70 65 6e 28  tor = ucol_open(
36693 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73  zLocale, &status
36694 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43  );.  if( !U_SUCC
36695 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20  ESS(status) ){. 
36696 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72     icuFunctionEr
36697 72 6f 72 28 70 2c 20 22 75 63 6f 6c 5f 6f 70 65  ror(p, "ucol_ope
36698 6e 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  n", status);.   
36699 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
3669a 73 73 65 72 74 28 70 29 3b 0a 0a 20 20 72 63 20  ssert(p);..  rc 
3669b 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
3669c 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 64 62  _collation_v2(db
3669d 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f  , zName, SQLITE_
3669e 55 54 46 31 36 2c 20 28 76 6f 69 64 20 2a 29 70  UTF16, (void *)p
3669f 55 43 6f 6c 6c 61 74 6f 72 2c 20 0a 20 20 20 20  UCollator, .    
366a0 20 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f    icuCollationCo
366a1 6c 6c 2c 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e  ll, icuCollation
366a2 44 65 6c 0a 20 20 29 3b 0a 20 20 69 66 28 20 72  Del.  );.  if( r
366a3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
366a4 20 20 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70      ucol_close(p
366a5 55 43 6f 6c 6c 61 74 6f 72 29 3b 0a 20 20 20 20  UCollator);.    
366a6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
366a7 72 72 6f 72 28 70 2c 20 22 45 72 72 6f 72 20 72  rror(p, "Error r
366a8 65 67 69 73 74 65 72 69 6e 67 20 63 6f 6c 6c 61  egistering colla
366a9 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 22 2c 20  tion function", 
366aa 2d 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  -1);.  }.}../*.*
366ab 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20 49  * Register the I
366ac 43 55 20 65 78 74 65 6e 73 69 6f 6e 20 66 75 6e  CU extension fun
366ad 63 74 69 6f 6e 73 20 77 69 74 68 20 64 61 74 61  ctions with data
366ae 62 61 73 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49  base db..*/.SQLI
366af 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
366b0 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 73 71  qlite3IcuInit(sq
366b1 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 74  lite3 *db){.  st
366b2 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 20 7b  ruct IcuScalar {
366b3 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
366b4 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
366b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
366b6 2a 20 46 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  * Function name 
366b7 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b  */.    int nArg;
366b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
366b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
366ba 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
366bb 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 69  guments */.    i
366bc 6e 74 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20  nt enc;         
366bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
366be 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69           /* Opti
366bf 6d 61 6c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  mal text encodin
366c0 67 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70  g */.    void *p
366c1 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20  Context;        
366c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
366c3 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 75 73     /* sqlite3_us
366c4 65 72 5f 64 61 74 61 28 29 20 63 6f 6e 74 65 78  er_data() contex
366c5 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a  t */.    void (*
366c6 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
366c7 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
366c8 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20  te3_value**);.  
366c9 7d 20 73 63 61 6c 61 72 73 5b 5d 20 3d 20 7b 0a  } scalars[] = {.
366ca 20 20 20 20 7b 22 72 65 67 65 78 70 22 2c 2d 31      {"regexp",-1
366cb 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 20 20  , SQLITE_ANY,   
366cc 20 20 20 20 20 20 20 30 2c 20 69 63 75 52 65 67         0, icuReg
366cd 65 78 70 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b  expFunc},..    {
366ce 22 6c 6f 77 65 72 22 2c 20 20 31 2c 20 53 51 4c  "lower",  1, SQL
366cf 49 54 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20  ITE_UTF16,      
366d0 20 20 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63    0, icuCaseFunc
366d1 31 36 7d 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72  16},.    {"lower
366d2 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  ",  2, SQLITE_UT
366d3 46 31 36 2c 20 20 20 20 20 20 20 20 30 2c 20 69  F16,        0, i
366d4 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20  cuCaseFunc16},. 
366d5 20 20 20 7b 22 75 70 70 65 72 22 2c 20 20 31 2c     {"upper",  1,
366d6 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28   SQLITE_UTF16, (
366d7 76 6f 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65  void*)1, icuCase
366d8 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75  Func16},.    {"u
366d9 70 70 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54  pper",  2, SQLIT
366da 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64 2a 29  E_UTF16, (void*)
366db 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  1, icuCaseFunc16
366dc 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22  },..    {"lower"
366dd 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  ,  1, SQLITE_UTF
366de 38 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69 63  8,         0, ic
366df 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20  uCaseFunc16},.  
366e0 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 32 2c 20    {"lower",  2, 
366e1 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
366e2 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65 46       0, icuCaseF
366e3 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70  unc16},.    {"up
366e4 70 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45  per",  1, SQLITE
366e5 5f 55 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31  _UTF8,  (void*)1
366e6 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
366e7 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c 20  ,.    {"upper", 
366e8 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
366e9 20 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75 43    (void*)1, icuC
366ea 61 73 65 46 75 6e 63 31 36 7d 2c 0a 0a 20 20 20  aseFunc16},..   
366eb 20 7b 22 6c 69 6b 65 22 2c 20 20 20 32 2c 20 53   {"like",   2, S
366ec 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20  QLITE_UTF8,     
366ed 20 20 20 20 30 2c 20 69 63 75 4c 69 6b 65 46 75      0, icuLikeFu
366ee 6e 63 7d 2c 0a 20 20 20 20 7b 22 6c 69 6b 65 22  nc},.    {"like"
366ef 2c 20 20 20 33 2c 20 53 51 4c 49 54 45 5f 55 54  ,   3, SQLITE_UT
366f0 46 38 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69  F8,         0, i
366f1 63 75 4c 69 6b 65 46 75 6e 63 7d 2c 0a 0a 20 20  cuLikeFunc},..  
366f2 20 20 7b 22 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c    {"icu_load_col
366f3 6c 61 74 69 6f 6e 22 2c 20 20 32 2c 20 53 51 4c  lation",  2, SQL
366f4 49 54 45 5f 55 54 46 38 2c 20 28 76 6f 69 64 2a  ITE_UTF8, (void*
366f5 29 64 62 2c 20 69 63 75 4c 6f 61 64 43 6f 6c 6c  )db, icuLoadColl
366f6 61 74 69 6f 6e 7d 2c 0a 20 20 7d 3b 0a 0a 20 20  ation},.  };..  
366f7 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
366f8 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  OK;.  int i;..  
366f9 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
366fa 49 54 45 5f 4f 4b 20 26 26 20 69 3c 28 73 69 7a  ITE_OK && i<(siz
366fb 65 6f 66 28 73 63 61 6c 61 72 73 29 2f 73 69 7a  eof(scalars)/siz
366fc 65 6f 66 28 73 74 72 75 63 74 20 49 63 75 53 63  eof(struct IcuSc
366fd 61 6c 61 72 29 29 3b 20 69 2b 2b 29 7b 0a 20 20  alar)); i++){.  
366fe 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61 6c    struct IcuScal
366ff 61 72 20 2a 70 20 3d 20 26 73 63 61 6c 61 72 73  ar *p = &scalars
36700 5b 69 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  [i];.    rc = sq
36701 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
36702 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 64  ction(.        d
36703 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d 3e  b, p->zName, p->
36704 6e 41 72 67 2c 20 70 2d 3e 65 6e 63 2c 20 70 2d  nArg, p->enc, p-
36705 3e 70 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 78 46  >pContext, p->xF
36706 75 6e 63 2c 20 30 2c 20 30 0a 20 20 20 20 29 3b  unc, 0, 0.    );
36707 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
36708 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54  c;.}..#if !SQLIT
36709 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50  E_CORE.SQLITE_AP
3670a 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  I int sqlite3_ex
3670b 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20  tension_init(.  
3670c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
3670d 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
3670e 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
3670f 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70  _api_routines *p
36710 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f  Api.){.  SQLITE_
36711 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28  EXTENSION_INIT2(
36712 70 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73  pApi).  return s
36713 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 64 62  qlite3IcuInit(db
36714 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  );.}.#endif..#en
36715 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
36716 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 69 63 75 2e  **** End of icu.
36717 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
36718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36719 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3671a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
3671b 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
3671c 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a  fts3_icu.c *****
3671d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3671e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3671f 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
36720 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  June 22.**.** Th
36721 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
36722 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
36723 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
36724 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
36725 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
36726 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
36727 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
36728 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
36729 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
3672a 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
3672b 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
3672c 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
3672d 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
3672e 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
3672f 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
36730 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
36731 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
36732 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36733 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36734 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36735 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36736 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
36737 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
36738 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 66   tokenizer for f
36739 74 73 33 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ts3 based on the
3673a 20 49 43 55 20 6c 69 62 72 61 72 79 2e 0a 2a 2a   ICU library..**
3673b 20 0a 2a 2a 20 24 49 64 3a 20 66 74 73 33 5f 69   .** $Id: fts3_i
3673c 63 75 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f  cu.c,v 1.3 2008/
3673d 30 39 2f 30 31 20 31 38 3a 33 34 3a 32 30 20 64  09/01 18:34:20 d
3673e 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
3673f 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  .*/..#if !define
36740 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
36741 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
36742 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 23 69  _ENABLE_FTS3).#i
36743 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
36744 4c 45 5f 49 43 55 0a 0a 0a 23 69 6e 63 6c 75 64  LE_ICU...#includ
36745 65 20 3c 75 6e 69 63 6f 64 65 2f 75 62 72 6b 2e  e <unicode/ubrk.
36746 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
36747 63 6f 64 65 2f 75 74 66 31 36 2e 68 3e 0a 0a 74  code/utf16.h>..t
36748 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 63  ypedef struct Ic
36749 75 54 6f 6b 65 6e 69 7a 65 72 20 49 63 75 54 6f  uTokenizer IcuTo
3674a 6b 65 6e 69 7a 65 72 3b 0a 74 79 70 65 64 65 66  kenizer;.typedef
3674b 20 73 74 72 75 63 74 20 49 63 75 43 75 72 73 6f   struct IcuCurso
3674c 72 20 49 63 75 43 75 72 73 6f 72 3b 0a 0a 73 74  r IcuCursor;..st
3674d 72 75 63 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65  ruct IcuTokenize
3674e 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
3674f 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b 0a 20 20  kenizer base;.  
36750 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 0a 7d  char *zLocale;.}
36751 3b 0a 0a 73 74 72 75 63 74 20 49 63 75 43 75 72  ;..struct IcuCur
36752 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sor {.  sqlite3_
36753 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
36754 20 62 61 73 65 3b 0a 0a 20 20 55 42 72 65 61 6b   base;..  UBreak
36755 49 74 65 72 61 74 6f 72 20 2a 70 49 74 65 72 3b  Iterator *pIter;
36756 20 20 20 20 20 20 2f 2a 20 49 43 55 20 62 72 65        /* ICU bre
36757 61 6b 2d 69 74 65 72 61 74 6f 72 20 6f 62 6a 65  ak-iterator obje
36758 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61  ct */.  int nCha
36759 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
3675a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3675b 20 55 43 68 61 72 20 65 6c 65 6d 65 6e 74 73 20   UChar elements 
3675c 69 6e 20 70 49 6e 70 75 74 20 2a 2f 0a 20 20 55  in pInput */.  U
3675d 43 68 61 72 20 2a 61 43 68 61 72 3b 20 20 20 20  Char *aChar;    
3675e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
3675f 70 79 20 6f 66 20 69 6e 70 75 74 20 75 73 69 6e  py of input usin
36760 67 20 75 74 66 2d 31 36 20 65 6e 63 6f 64 69 6e  g utf-16 encodin
36761 67 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4f 66 66  g */.  int *aOff
36762 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  set;            
36763 20 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66     /* Offsets of
36764 20 65 61 63 68 20 63 68 61 72 61 63 74 65 72 20   each character 
36765 69 6e 20 75 74 66 2d 38 20 69 6e 70 75 74 20 2a  in utf-8 input *
36766 2f 0a 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72  /..  int nBuffer
36767 3b 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 66 65  ;.  char *zBuffe
36768 72 3b 0a 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e  r;..  int iToken
36769 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  ;.};../*.** Crea
3676a 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a  te a new tokeniz
3676b 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a  er instance..*/.
3676c 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 43 72  static int icuCr
3676d 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63  eate(.  int argc
3676e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3676f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36770 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
36771 73 20 69 6e 20 61 72 67 76 5b 5d 20 2a 2f 0a 20  s in argv[] */. 
36772 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f   const char * co
36773 6e 73 74 20 2a 61 72 67 76 2c 20 20 20 20 20 20  nst *argv,      
36774 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a        /* Tokeniz
36775 65 72 20 63 72 65 61 74 69 6f 6e 20 61 72 67 75  er creation argu
36776 6d 65 6e 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74  ments */.  sqlit
36777 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70  e3_tokenizer **p
36778 70 54 6f 6b 65 6e 69 7a 65 72 20 20 20 20 20 20  pTokenizer      
36779 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20  /* OUT: Created 
3677a 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 29 7b 0a  tokenizer */.){.
3677b 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a    IcuTokenizer *
3677c 70 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a  p;.  int n = 0;.
3677d 0a 20 20 69 66 28 20 61 72 67 63 3e 30 20 29 7b  .  if( argc>0 ){
3677e 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28  .    n = strlen(
3677f 61 72 67 76 5b 30 5d 29 2b 31 3b 0a 20 20 7d 0a  argv[0])+1;.  }.
36780 20 20 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69    p = (IcuTokeni
36781 7a 65 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  zer *)sqlite3_ma
36782 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49 63 75 54  lloc(sizeof(IcuT
36783 6f 6b 65 6e 69 7a 65 72 29 2b 6e 29 3b 0a 20 20  okenizer)+n);.  
36784 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
36785 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
36786 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  M;.  }.  memset(
36787 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 49 63 75  p, 0, sizeof(Icu
36788 54 6f 6b 65 6e 69 7a 65 72 29 29 3b 0a 0a 20 20  Tokenizer));..  
36789 69 66 28 20 6e 20 29 7b 0a 20 20 20 20 70 2d 3e  if( n ){.    p->
3678a 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 68 61 72 20  zLocale = (char 
3678b 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d  *)&p[1];.    mem
3678c 63 70 79 28 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20  cpy(p->zLocale, 
3678d 61 72 67 76 5b 30 5d 2c 20 6e 29 3b 0a 20 20 7d  argv[0], n);.  }
3678e 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  ..  *ppTokenizer
3678f 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65   = (sqlite3_toke
36790 6e 69 7a 65 72 20 2a 29 70 3b 0a 0a 20 20 72 65  nizer *)p;..  re
36791 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
36792 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79  }../*.** Destroy
36793 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a   a tokenizer.*/.
36794 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 44 65  static int icuDe
36795 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f  stroy(sqlite3_to
36796 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
36797 7a 65 72 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e  zer){.  IcuToken
36798 69 7a 65 72 20 2a 70 20 3d 20 28 49 63 75 54 6f  izer *p = (IcuTo
36799 6b 65 6e 69 7a 65 72 20 2a 29 70 54 6f 6b 65 6e  kenizer *)pToken
3679a 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f  izer;.  sqlite3_
3679b 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72  free(p);.  retur
3679c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
3679d 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f  /*.** Prepare to
3679e 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e   begin tokenizin
3679f 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  g a particular s
367a0 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75  tring.  The inpu
367a1 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62  t.** string to b
367a2 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 70  e tokenized is p
367a3 49 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73 2d  Input[0..nBytes-
367a4 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a  1].  A cursor.**
367a5 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65   used to increme
367a6 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20  ntally tokenize 
367a7 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 72  this string is r
367a8 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a  eturned in .** *
367a9 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61  ppCursor..*/.sta
367aa 74 69 63 20 69 6e 74 20 69 63 75 4f 70 65 6e 28  tic int icuOpen(
367ab 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
367ac 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
367ad 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
367ae 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20   tokenizer */.  
367af 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70  const char *zInp
367b0 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ut,             
367b1 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20         /* Input 
367b2 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  string */.  int 
367b3 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20  nInput,         
367b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
367b5 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
367b6 7a 49 6e 70 75 74 20 69 6e 20 62 79 74 65 73 20  zInput in bytes 
367b7 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
367b8 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a  enizer_cursor **
367b9 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f  ppCursor    /* O
367ba 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e  UT: Tokenization
367bb 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20   cursor */.){.  
367bc 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20  IcuTokenizer *p 
367bd 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  = (IcuTokenizer 
367be 2a 29 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  *)pTokenizer;.  
367bf 49 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 3b  IcuCursor *pCsr;
367c0 0a 0a 20 20 63 6f 6e 73 74 20 69 6e 74 33 32 5f  ..  const int32_
367c1 74 20 6f 70 74 20 3d 20 55 5f 46 4f 4c 44 5f 43  t opt = U_FOLD_C
367c2 41 53 45 5f 44 45 46 41 55 4c 54 3b 0a 20 20 55  ASE_DEFAULT;.  U
367c3 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73  ErrorCode status
367c4 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b   = U_ZERO_ERROR;
367c5 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 0a 0a 20  .  int nChar;.. 
367c6 20 55 43 68 61 72 33 32 20 63 3b 0a 20 20 69 6e   UChar32 c;.  in
367c7 74 20 69 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20  t iInput = 0;.  
367c8 69 6e 74 20 69 4f 75 74 20 3d 20 30 3b 0a 0a 20  int iOut = 0;.. 
367c9 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a   *ppCursor = 0;.
367ca 0a 20 20 69 66 28 20 6e 49 6e 70 75 74 3c 30 20  .  if( nInput<0 
367cb 29 7b 0a 20 20 20 20 6e 49 6e 70 75 74 20 3d 20  ){.    nInput = 
367cc 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a  strlen(zInput);.
367cd 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20 6e 49    }.  nChar = nI
367ce 6e 70 75 74 2b 31 3b 0a 20 20 70 43 73 72 20 3d  nput+1;.  pCsr =
367cf 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 73 71   (IcuCursor *)sq
367d0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 0a 20 20  lite3_malloc(.  
367d1 20 20 20 20 73 69 7a 65 6f 66 28 49 63 75 43 75      sizeof(IcuCu
367d2 72 73 6f 72 29 20 2b 20 20 20 20 20 20 20 20 20  rsor) +         
367d3 20 20 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72         /* IcuCur
367d4 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 6e 43 68  sor */.      nCh
367d5 61 72 20 2a 20 73 69 7a 65 6f 66 28 55 43 68 61  ar * sizeof(UCha
367d6 72 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20  r) +            
367d7 2f 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 43 68  /* IcuCursor.aCh
367d8 61 72 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 28 6e  ar[] */.      (n
367d9 43 68 61 72 2b 31 29 20 2a 20 73 69 7a 65 6f 66  Char+1) * sizeof
367da 28 69 6e 74 29 20 20 20 20 20 20 20 20 20 20 20  (int)           
367db 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 4f   /* IcuCursor.aO
367dc 66 66 73 65 74 5b 5d 20 2a 2f 0a 20 20 29 3b 0a  ffset[] */.  );.
367dd 20 20 69 66 28 20 21 70 43 73 72 20 29 7b 0a 20    if( !pCsr ){. 
367de 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
367df 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65  _NOMEM;.  }.  me
367e0 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69  mset(pCsr, 0, si
367e1 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72 29 29  zeof(IcuCursor))
367e2 3b 0a 20 20 70 43 73 72 2d 3e 61 43 68 61 72 20  ;.  pCsr->aChar 
367e3 3d 20 28 55 43 68 61 72 20 2a 29 26 70 43 73 72  = (UChar *)&pCsr
367e4 5b 31 5d 3b 0a 20 20 70 43 73 72 2d 3e 61 4f 66  [1];.  pCsr->aOf
367e5 66 73 65 74 20 3d 20 28 69 6e 74 20 2a 29 26 70  fset = (int *)&p
367e6 43 73 72 2d 3e 61 43 68 61 72 5b 6e 43 68 61 72  Csr->aChar[nChar
367e7 5d 3b 0a 0a 20 20 70 43 73 72 2d 3e 61 4f 66 66  ];..  pCsr->aOff
367e8 73 65 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70  set[iOut] = iInp
367e9 75 74 3b 0a 20 20 55 38 5f 4e 45 58 54 28 7a 49  ut;.  U8_NEXT(zI
367ea 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49  nput, iInput, nI
367eb 6e 70 75 74 2c 20 63 29 3b 20 0a 20 20 77 68 69  nput, c); .  whi
367ec 6c 65 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 69  le( c>0 ){.    i
367ed 6e 74 20 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a  nt isError = 0;.
367ee 20 20 20 20 63 20 3d 20 75 5f 66 6f 6c 64 43 61      c = u_foldCa
367ef 73 65 28 63 2c 20 6f 70 74 29 3b 0a 20 20 20 20  se(c, opt);.    
367f0 55 31 36 5f 41 50 50 45 4e 44 28 70 43 73 72 2d  U16_APPEND(pCsr-
367f1 3e 61 43 68 61 72 2c 20 69 4f 75 74 2c 20 6e 43  >aChar, iOut, nC
367f2 68 61 72 2c 20 63 2c 20 69 73 45 72 72 6f 72 29  har, c, isError)
367f3 3b 0a 20 20 20 20 69 66 28 20 69 73 45 72 72 6f  ;.    if( isErro
367f4 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
367f5 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20  e3_free(pCsr);. 
367f6 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
367f7 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
367f8 20 20 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65      pCsr->aOffse
367f9 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74  t[iOut] = iInput
367fa 3b 0a 0a 20 20 20 20 69 66 28 20 69 49 6e 70 75  ;..    if( iInpu
367fb 74 3c 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20  t<nInput ){.    
367fc 20 20 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75 74    U8_NEXT(zInput
367fd 2c 20 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74  , iInput, nInput
367fe 2c 20 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , c);.    }else{
367ff 0a 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20  .      c = 0;.  
36800 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 43 73 72 2d    }.  }..  pCsr-
36801 3e 70 49 74 65 72 20 3d 20 75 62 72 6b 5f 6f 70  >pIter = ubrk_op
36802 65 6e 28 55 42 52 4b 5f 57 4f 52 44 2c 20 70 2d  en(UBRK_WORD, p-
36803 3e 7a 4c 6f 63 61 6c 65 2c 20 70 43 73 72 2d 3e  >zLocale, pCsr->
36804 61 43 68 61 72 2c 20 69 4f 75 74 2c 20 26 73 74  aChar, iOut, &st
36805 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f  atus);.  if( !U_
36806 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
36807 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
36808 72 65 65 28 70 43 73 72 29 3b 0a 20 20 20 20 72  ree(pCsr);.    r
36809 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
3680a 4f 52 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e  OR;.  }.  pCsr->
3680b 6e 43 68 61 72 20 3d 20 69 4f 75 74 3b 0a 0a 20  nChar = iOut;.. 
3680c 20 75 62 72 6b 5f 66 69 72 73 74 28 70 43 73 72   ubrk_first(pCsr
3680d 2d 3e 70 49 74 65 72 29 3b 0a 20 20 2a 70 70 43  ->pIter);.  *ppC
3680e 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74 65 33  ursor = (sqlite3
3680f 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
36810 72 20 2a 29 70 43 73 72 3b 0a 20 20 72 65 74 75  r *)pCsr;.  retu
36811 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
36812 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74  ./*.** Close a t
36813 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
36814 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70  or previously op
36815 65 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  ened by a call t
36816 6f 20 69 63 75 4f 70 65 6e 28 29 2e 0a 2a 2f 0a  o icuOpen()..*/.
36817 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 43 6c  static int icuCl
36818 6f 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ose(sqlite3_toke
36819 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
3681a 75 72 73 6f 72 29 7b 0a 20 20 49 63 75 43 75 72  ursor){.  IcuCur
3681b 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 49 63 75  sor *pCsr = (Icu
3681c 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72  Cursor *)pCursor
3681d 3b 0a 20 20 75 62 72 6b 5f 63 6c 6f 73 65 28 70  ;.  ubrk_close(p
3681e 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 73  Csr->pIter);.  s
3681f 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
36820 2d 3e 7a 42 75 66 66 65 72 29 3b 0a 20 20 73 71  ->zBuffer);.  sq
36821 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 29  lite3_free(pCsr)
36822 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
36823 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  E_OK;.}../*.** E
36824 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20  xtract the next 
36825 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b  token from a tok
36826 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
36827 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36828 69 63 75 4e 65 78 74 28 0a 20 20 73 71 6c 69 74  icuNext(.  sqlit
36829 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
3682a 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f  sor *pCursor,  /
3682b 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65  * Cursor returne
3682c 64 20 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20  d by simpleOpen 
3682d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3682e 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20  **ppToken,      
3682f 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
36830 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65   *ppToken is the
36831 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20   token text */. 
36832 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20   int *pnBytes,  
36833 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36834 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d       /* OUT: Num
36835 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
36836 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
36837 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20  piStartOffset,  
36838 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36839 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20  * OUT: Starting 
3683a 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20  offset of token 
3683b 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f  */.  int *piEndO
3683c 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
3683d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3683e 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f   Ending offset o
3683f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  f token */.  int
36840 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20   *piPosition    
36841 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36842 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f   /* OUT: Positio
36843 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b  n integer of tok
36844 65 6e 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 43 75  en */.){.  IcuCu
36845 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 49 63  rsor *pCsr = (Ic
36846 75 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f  uCursor *)pCurso
36847 72 3b 0a 0a 20 20 69 6e 74 20 69 53 74 61 72 74  r;..  int iStart
36848 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 45 6e 64   = 0;.  int iEnd
36849 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 42 79 74   = 0;.  int nByt
3684a 65 20 3d 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28  e = 0;..  while(
3684b 20 69 53 74 61 72 74 3d 3d 69 45 6e 64 20 29 7b   iStart==iEnd ){
3684c 0a 20 20 20 20 55 43 68 61 72 33 32 20 63 3b 0a  .    UChar32 c;.
3684d 0a 20 20 20 20 69 53 74 61 72 74 20 3d 20 75 62  .    iStart = ub
3684e 72 6b 5f 63 75 72 72 65 6e 74 28 70 43 73 72 2d  rk_current(pCsr-
3684f 3e 70 49 74 65 72 29 3b 0a 20 20 20 20 69 45 6e  >pIter);.    iEn
36850 64 20 3d 20 75 62 72 6b 5f 6e 65 78 74 28 70 43  d = ubrk_next(pC
36851 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20 20  sr->pIter);.    
36852 69 66 28 20 69 45 6e 64 3d 3d 55 42 52 4b 5f 44  if( iEnd==UBRK_D
36853 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ONE ){.      ret
36854 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
36855 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c  .    }..    whil
36856 65 28 20 69 53 74 61 72 74 3c 69 45 6e 64 20 29  e( iStart<iEnd )
36857 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 57 68 69  {.      int iWhi
36858 74 65 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 20  te = iStart;.   
36859 20 20 20 55 38 5f 4e 45 58 54 28 70 43 73 72 2d     U8_NEXT(pCsr-
3685a 3e 61 43 68 61 72 2c 20 69 57 68 69 74 65 2c 20  >aChar, iWhite, 
3685b 70 43 73 72 2d 3e 6e 43 68 61 72 2c 20 63 29 3b  pCsr->nChar, c);
3685c 0a 20 20 20 20 20 20 69 66 28 20 75 5f 69 73 73  .      if( u_iss
3685d 70 61 63 65 28 63 29 20 29 7b 0a 20 20 20 20 20  pace(c) ){.     
3685e 20 20 20 69 53 74 61 72 74 20 3d 20 69 57 68 69     iStart = iWhi
3685f 74 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  te;.      }else{
36860 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
36861 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
36862 20 20 61 73 73 65 72 74 28 69 53 74 61 72 74 3c    assert(iStart<
36863 3d 69 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 64  =iEnd);.  }..  d
36864 6f 20 7b 0a 20 20 20 20 55 45 72 72 6f 72 43 6f  o {.    UErrorCo
36865 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45  de status = U_ZE
36866 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 20 20 69 66  RO_ERROR;.    if
36867 28 20 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20  ( nByte ){.     
36868 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71   char *zNew = sq
36869 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 43  lite3_realloc(pC
3686a 73 72 2d 3e 7a 42 75 66 66 65 72 2c 20 6e 42 79  sr->zBuffer, nBy
3686b 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  te);.      if( !
3686c 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  zNew ){.        
3686d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
3686e 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
3686f 20 20 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72     pCsr->zBuffer
36870 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 70   = zNew;.      p
36871 43 73 72 2d 3e 6e 42 75 66 66 65 72 20 3d 20 6e  Csr->nBuffer = n
36872 42 79 74 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  Byte;.    }..   
36873 20 75 5f 73 74 72 54 6f 55 54 46 38 28 0a 20 20   u_strToUTF8(.  
36874 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42 75 66        pCsr->zBuf
36875 66 65 72 2c 20 70 43 73 72 2d 3e 6e 42 75 66 66  fer, pCsr->nBuff
36876 65 72 2c 20 26 6e 42 79 74 65 2c 20 20 20 20 2f  er, &nByte,    /
36877 2a 20 4f 75 74 70 75 74 20 76 61 72 73 20 2a 2f  * Output vars */
36878 0a 20 20 20 20 20 20 20 20 26 70 43 73 72 2d 3e  .        &pCsr->
36879 61 43 68 61 72 5b 69 53 74 61 72 74 5d 2c 20 69  aChar[iStart], i
3687a 45 6e 64 2d 69 53 74 61 72 74 2c 20 20 20 20 20  End-iStart,     
3687b 20 20 2f 2a 20 49 6e 70 75 74 20 76 61 72 73 20    /* Input vars 
3687c 2a 2f 0a 20 20 20 20 20 20 20 20 26 73 74 61 74  */.        &stat
3687d 75 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  us              
3687e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3687f 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 73 75      /* Output su
36880 63 63 65 73 73 2f 66 61 69 6c 75 72 65 20 2a 2f  ccess/failure */
36881 0a 20 20 20 20 29 3b 0a 20 20 7d 20 77 68 69 6c  .    );.  } whil
36882 65 28 20 6e 42 79 74 65 3e 70 43 73 72 2d 3e 6e  e( nByte>pCsr->n
36883 42 75 66 66 65 72 20 29 3b 0a 0a 20 20 2a 70 70  Buffer );..  *pp
36884 54 6f 6b 65 6e 20 3d 20 70 43 73 72 2d 3e 7a 42  Token = pCsr->zB
36885 75 66 66 65 72 3b 0a 20 20 2a 70 6e 42 79 74 65  uffer;.  *pnByte
36886 73 20 3d 20 6e 42 79 74 65 3b 0a 20 20 2a 70 69  s = nByte;.  *pi
36887 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 70 43  StartOffset = pC
36888 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 53 74 61  sr->aOffset[iSta
36889 72 74 5d 3b 0a 20 20 2a 70 69 45 6e 64 4f 66 66  rt];.  *piEndOff
3688a 73 65 74 20 3d 20 70 43 73 72 2d 3e 61 4f 66 66  set = pCsr->aOff
3688b 73 65 74 5b 69 45 6e 64 5d 3b 0a 20 20 2a 70 69  set[iEnd];.  *pi
3688c 50 6f 73 69 74 69 6f 6e 20 3d 20 70 43 73 72 2d  Position = pCsr-
3688d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 72 65  >iToken++;..  re
3688e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3688f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74  }../*.** The set
36890 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68 61   of routines tha
36891 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  t implement the 
36892 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72  simple tokenizer
36893 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
36894 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
36895 65 72 5f 6d 6f 64 75 6c 65 20 69 63 75 54 6f 6b  er_module icuTok
36896 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b  enizerModule = {
36897 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
36898 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36899 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
3689a 20 69 63 75 43 72 65 61 74 65 2c 20 20 20 20 20   icuCreate,     
3689b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3689c 20 78 43 72 65 61 74 65 20 20 2a 2f 0a 20 20 69   xCreate  */.  i
3689d 63 75 44 65 73 74 72 6f 79 2c 20 20 20 20 20 20  cuDestroy,      
3689e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3689f 43 72 65 61 74 65 20 20 2a 2f 0a 20 20 69 63 75  Create  */.  icu
368a0 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
368a1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70            /* xOp
368a2 65 6e 20 20 20 20 2a 2f 0a 20 20 69 63 75 43 6c  en    */.  icuCl
368a3 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ose,            
368a4 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
368a5 65 20 20 20 2a 2f 0a 20 20 69 63 75 4e 65 78 74  e   */.  icuNext
368a6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
368a7 20 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20 20        /* xNext  
368a8 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53    */.};../*.** S
368a9 65 74 20 2a 70 70 4d 6f 64 75 6c 65 20 74 6f 20  et *ppModule to 
368aa 70 6f 69 6e 74 20 61 74 20 74 68 65 20 69 6d 70  point at the imp
368ab 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
368ac 68 65 20 49 43 55 20 74 6f 6b 65 6e 69 7a 65 72  he ICU tokenizer
368ad 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
368ae 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
368af 46 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72  Fts3IcuTokenizer
368b0 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  Module(.  sqlite
368b1 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
368b2 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75  le const**ppModu
368b3 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c  le.){.  *ppModul
368b4 65 20 3d 20 26 69 63 75 54 6f 6b 65 6e 69 7a 65  e = &icuTokenize
368b5 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64  rModule;.}..#end
368b6 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51  if /* defined(SQ
368b7 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 29  LITE_ENABLE_ICU)
368b8 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64   */.#endif /* !d
368b9 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
368ba 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
368bb 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
368bc 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  3) */../********
368bd 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
368be 73 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a  s3_icu.c *******
368bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
368c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
368c1 2a 2a 2a 2a 2a 2f 0a                             *****/.